Hex Artifact Content
Not logged in

Artifact aec3efbd92c6447389d2be17b16b0d029011a5e6:

File src/sqlite3.c part of check-in [d8590e093f] - Merge in the latest SQLite updates. by drh on 2007-07-24 12:54:24.

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 34 2e 31 2e 20 20 42  ersion 3.4.1.  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 32 37 30 32 20 6c 69 6e 65 73  st.** 2702 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 37 2d 32 34 20 31 32 3a 35 33  2007-07-24 12:53
04c0: 3a 32 36 20 55 54 43 2e 0a 2a 2f 0a 23 64 65 66  :26 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 31 38 20  te.h.in,v 1.218 
0b90: 32 30 30 37 2f 30 37 2f 31 39 20 31 32 3a 34 31  2007/07/19 12:41
0ba0: 3a 34 30 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :40 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 2f 2a 0a 2a  " {.#endif../*.*
0c70: 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 73  * Make sure thes
0c80: 65 20 73 79 6d 62 6f 6c 73 20 77 68 65 72 65 20  e symbols where 
0c90: 6e 6f 74 20 64 65 66 69 6e 65 64 20 62 79 20 73  not defined by s
0ca0: 6f 6d 65 20 70 72 65 76 69 6f 75 73 20 68 65 61  ome previous hea
0cb0: 64 65 72 0a 2a 2a 20 66 69 6c 65 2e 0a 2a 2f 0a  der.** file..*/.
0cc0: 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 56 45  #ifdef SQLITE_VE
0cd0: 52 53 49 4f 4e 0a 23 20 75 6e 64 65 66 20 53 51  RSION.# undef SQ
0ce0: 4c 49 54 45 5f 56 45 52 53 49 4f 4e 0a 23 65 6e  LITE_VERSION.#en
0cf0: 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54  dif.#ifdef SQLIT
0d00: 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52  E_VERSION_NUMBER
0d10: 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f  .# undef SQLITE_
0d20: 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 0a 23  VERSION_NUMBER.#
0d30: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  endif../*.** CAP
0d40: 49 33 52 45 46 3a 20 43 6f 6d 70 69 6c 65 2d 54  I3REF: Compile-T
0d50: 69 6d 65 20 4c 69 62 72 61 72 79 20 56 65 72 73  ime Library Vers
0d60: 69 6f 6e 20 4e 75 6d 62 65 72 73 0a 2a 2a 0a 2a  ion Numbers.**.*
0d70: 2a 20 54 68 65 20 76 65 72 73 69 6f 6e 20 6f 66  * The version of
0d80: 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72   the SQLite libr
0d90: 61 72 79 20 69 73 20 63 6f 6e 74 61 69 6e 65 64  ary is contained
0da0: 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 2e   in the sqlite3.
0db0: 68 0a 2a 2a 20 68 65 61 64 65 72 20 66 69 6c 65  h.** header file
0dc0: 20 69 6e 20 61 20 23 64 65 66 69 6e 65 20 6e 61   in a #define na
0dd0: 6d 65 64 20 53 51 4c 49 54 45 5f 56 45 52 53 49  med SQLITE_VERSI
0de0: 4f 4e 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f  ON.  The SQLITE_
0df0: 56 45 52 53 49 4f 4e 0a 2a 2a 20 6d 61 63 72 6f  VERSION.** macro
0e00: 20 72 65 73 6f 6c 76 65 73 20 74 6f 20 61 20 73   resolves to a s
0e10: 74 72 69 6e 67 20 63 6f 6e 73 74 61 6e 74 2e 0a  tring constant..
0e20: 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74  **.** The format
0e30: 20 6f 66 20 74 68 65 20 76 65 72 73 69 6f 6e 20   of the version 
0e40: 73 74 72 69 6e 67 20 69 73 20 22 58 2e 59 2e 5a  string is "X.Y.Z
0e50: 22 2c 20 77 68 65 72 65 0a 2a 2a 20 58 20 69 73  ", where.** X is
0e60: 20 74 68 65 20 6d 61 6a 6f 72 20 76 65 72 73 69   the major versi
0e70: 6f 6e 20 6e 75 6d 62 65 72 2c 20 59 20 69 73 20  on number, Y is 
0e80: 74 68 65 20 6d 69 6e 6f 72 20 76 65 72 73 69 6f  the minor versio
0e90: 6e 20 6e 75 6d 62 65 72 20 61 6e 64 20 5a 0a 2a  n number and Z.*
0ea0: 2a 20 69 73 20 74 68 65 20 72 65 6c 65 61 73 65  * is the release
0eb0: 20 6e 75 6d 62 65 72 2e 20 20 54 68 65 20 58 2e   number.  The X.
0ec0: 59 2e 5a 20 6d 69 67 68 74 20 62 65 20 66 6f 6c  Y.Z might be fol
0ed0: 6c 6f 77 65 64 20 62 79 20 22 61 6c 70 68 61 22  lowed by "alpha"
0ee0: 20 6f 72 20 22 62 65 74 61 22 2e 0a 2a 2a 20 46   or "beta"..** F
0ef0: 6f 72 20 65 78 61 6d 70 6c 65 20 22 33 2e 31 2e  or example "3.1.
0f00: 31 62 65 74 61 22 2e 0a 2a 2a 0a 2a 2a 20 54 68  1beta"..**.** Th
0f10: 65 20 58 20 76 61 6c 75 65 20 69 73 20 61 6c 77  e X value is alw
0f20: 61 79 73 20 33 20 69 6e 20 53 51 4c 69 74 65 2e  ays 3 in SQLite.
0f30: 20 20 54 68 65 20 58 20 76 61 6c 75 65 20 6f 6e    The X value on
0f40: 6c 79 20 63 68 61 6e 67 65 73 20 77 68 65 6e 0a  ly changes when.
0f50: 2a 2a 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d  ** backwards com
0f60: 70 61 74 69 62 69 6c 69 74 79 20 69 73 20 62 72  patibility is br
0f70: 6f 6b 65 6e 20 61 6e 64 20 77 65 20 69 6e 74 65  oken and we inte
0f80: 6e 64 20 74 6f 20 6e 65 76 65 72 20 62 72 65 61  nd to never brea
0f90: 6b 0a 2a 2a 20 62 61 63 6b 77 61 72 64 73 20 63  k.** backwards c
0fa0: 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 20 20 54  ompatibility.  T
0fb0: 68 65 20 59 20 76 61 6c 75 65 20 6f 6e 6c 79 20  he Y value only 
0fc0: 63 68 61 6e 67 65 73 20 77 68 65 6e 0a 2a 2a 20  changes when.** 
0fd0: 74 68 65 72 65 20 61 72 65 20 6d 61 6a 6f 72 20  there are major 
0fe0: 66 65 61 74 75 72 65 20 65 6e 68 61 6e 63 65 6d  feature enhancem
0ff0: 65 6e 74 73 20 74 68 61 74 20 61 72 65 20 66 6f  ents that are fo
1000: 72 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 6c  rwards compatibl
1010: 65 0a 2a 2a 20 62 75 74 20 6e 6f 74 20 62 61 63  e.** but not bac
1020: 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 6c  kwards compatibl
1030: 65 2e 20 20 54 68 65 20 5a 20 76 61 6c 75 65 20  e.  The Z value 
1040: 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77  is incremented w
1050: 69 74 68 0a 2a 2a 20 65 61 63 68 20 72 65 6c 65  ith.** each rele
1060: 61 73 65 20 62 75 74 20 72 65 73 65 74 73 20 62  ase but resets b
1070: 61 63 6b 20 74 6f 20 30 20 77 68 65 6e 20 59 20  ack to 0 when Y 
1080: 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e 0a  is incremented..
1090: 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45  **.** The SQLITE
10a0: 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 20  _VERSION_NUMBER 
10b0: 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 77 69  is an integer wi
10c0: 74 68 20 74 68 65 20 76 61 6c 75 65 20 0a 2a 2a  th the value .**
10d0: 20 28 58 2a 31 30 30 30 30 30 30 20 2b 20 59 2a   (X*1000000 + Y*
10e0: 31 30 30 30 20 2b 20 5a 29 2e 20 46 6f 72 20 65  1000 + Z). For e
10f0: 78 61 6d 70 6c 65 2c 20 66 6f 72 20 76 65 72 73  xample, for vers
1100: 69 6f 6e 20 22 33 2e 31 2e 31 62 65 74 61 22 2c  ion "3.1.1beta",
1110: 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 56 45 52 53   .** SQLITE_VERS
1120: 49 4f 4e 5f 4e 55 4d 42 45 52 20 69 73 20 73 65  ION_NUMBER is se
1130: 74 20 74 6f 20 33 30 30 31 30 30 31 2e 20 54 6f  t to 3001001. To
1140: 20 64 65 74 65 63 74 20 69 66 20 74 68 65 79 20   detect if they 
1150: 61 72 65 20 75 73 69 6e 67 20 0a 2a 2a 20 76 65  are using .** ve
1160: 72 73 69 6f 6e 20 33 2e 31 2e 31 20 6f 72 20 67  rsion 3.1.1 or g
1170: 72 65 61 74 65 72 20 61 74 20 63 6f 6d 70 69 6c  reater at compil
1180: 65 20 74 69 6d 65 2c 20 70 72 6f 67 72 61 6d 73  e time, programs
1190: 20 6d 61 79 20 75 73 65 20 74 68 65 20 74 65 73   may use the tes
11a0: 74 20 0a 2a 2a 20 28 53 51 4c 49 54 45 5f 56 45  t .** (SQLITE_VE
11b0: 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 3e 3d 33 30  RSION_NUMBER>=30
11c0: 30 31 30 30 31 29 2e 0a 2a 2a 0a 2a 2a 20 53 65  01001)..**.** Se
11d0: 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33  e also: [sqlite3
11e0: 5f 6c 69 62 76 65 72 73 69 6f 6e 28 29 5d 20 61  _libversion()] a
11f0: 6e 64 20 5b 73 71 6c 69 74 65 33 5f 6c 69 62 76  nd [sqlite3_libv
1200: 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 29 5d  ersion_number()]
1210: 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ..*/.#define SQL
1220: 49 54 45 5f 56 45 52 53 49 4f 4e 20 20 20 20 20  ITE_VERSION     
1230: 20 20 20 20 22 33 2e 34 2e 31 22 0a 23 64 65 66      "3.4.1".#def
1240: 69 6e 65 20 53 51 4c 49 54 45 5f 56 45 52 53 49  ine SQLITE_VERSI
1250: 4f 4e 5f 4e 55 4d 42 45 52 20 33 30 30 34 30 30  ON_NUMBER 300400
1260: 31 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  1../*.** CAPI3RE
1270: 46 3a 20 52 75 6e 2d 54 69 6d 65 20 4c 69 62 72  F: Run-Time Libr
1280: 61 72 79 20 56 65 72 73 69 6f 6e 20 4e 75 6d 62  ary Version Numb
1290: 65 72 73 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  ers.**.** These 
12a0: 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20  routines return 
12b0: 76 61 6c 75 65 73 20 65 71 75 69 76 61 6c 65 6e  values equivalen
12c0: 74 20 74 6f 20 74 68 65 20 68 65 61 64 65 72 20  t to the header 
12d0: 63 6f 6e 73 74 61 6e 74 73 0a 2a 2a 20 5b 53 51  constants.** [SQ
12e0: 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5d 20 61 6e  LITE_VERSION] an
12f0: 64 20 5b 53 51 4c 49 54 45 5f 56 45 52 53 49 4f  d [SQLITE_VERSIO
1300: 4e 5f 4e 55 4d 42 45 52 5d 2e 20 20 54 68 65 20  N_NUMBER].  The 
1310: 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64 0a  values returned.
1320: 2a 2a 20 62 79 20 74 68 69 73 20 72 6f 75 74 69  ** by this routi
1330: 6e 65 73 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20  nes should only 
1340: 62 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f  be different fro
1350: 6d 20 74 68 65 20 68 65 61 64 65 72 20 76 61 6c  m the header val
1360: 75 65 73 0a 2a 2a 20 69 66 20 79 6f 75 20 63 6f  ues.** if you co
1370: 6d 70 69 6c 65 20 79 6f 75 72 20 70 72 6f 67 72  mpile your progr
1380: 61 6d 20 75 73 69 6e 67 20 61 6e 20 73 71 6c 69  am using an sqli
1390: 74 65 33 2e 68 20 68 65 61 64 65 72 20 66 72 6f  te3.h header fro
13a0: 6d 20 61 0a 2a 2a 20 64 69 66 66 65 72 65 6e 74  m a.** different
13b0: 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69   version of SQLi
13c0: 74 65 20 74 68 61 74 20 74 68 65 20 76 65 72 73  te that the vers
13d0: 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 62 72 61  ion of the libra
13e0: 72 79 20 79 6f 75 0a 2a 2a 20 6c 69 6e 6b 20 61  ry you.** link a
13f0: 67 61 69 6e 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  gainst..**.** Th
1400: 65 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f  e sqlite3_versio
1410: 6e 5b 5d 20 73 74 72 69 6e 67 20 63 6f 6e 73 74  n[] string const
1420: 61 6e 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ant contains the
1430: 20 74 65 78 74 20 6f 66 20 74 68 65 0a 2a 2a 20   text of the.** 
1440: 5b 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5d  [SQLITE_VERSION]
1450: 20 73 74 72 69 6e 67 2e 20 20 54 68 65 20 73 71   string.  The sq
1460: 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e  lite3_libversion
1470: 28 29 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  () function retu
1480: 72 6e 73 0a 2a 2a 20 61 20 70 6f 69 6e 65 72 20  rns.** a poiner 
1490: 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76  to the sqlite3_v
14a0: 65 72 73 69 6f 6e 5b 5d 20 73 74 72 69 6e 67 20  ersion[] string 
14b0: 63 6f 6e 73 74 61 6e 74 2e 20 20 54 68 65 20 66  constant.  The f
14c0: 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 70 72  unction.** is pr
14d0: 6f 76 69 64 65 64 20 66 6f 72 20 44 4c 4c 20 75  ovided for DLL u
14e0: 73 65 72 73 20 77 68 6f 20 63 61 6e 20 6f 6e 6c  sers who can onl
14f0: 79 20 61 63 63 65 73 73 20 66 75 6e 63 74 69 6f  y access functio
1500: 6e 73 20 61 6e 64 20 6e 6f 74 0a 2a 2a 20 63 6f  ns and not.** co
1510: 6e 73 74 61 6e 74 73 20 77 69 74 68 69 6e 20 74  nstants within t
1520: 68 65 20 44 4c 4c 2e 0a 2a 2f 0a 65 78 74 65 72  he DLL..*/.exter
1530: 6e 20 63 6f 6e 73 74 20 63 68 61 72 20 73 71 6c  n const char sql
1540: 69 74 65 33 5f 76 65 72 73 69 6f 6e 5b 5d 3b 0a  ite3_version[];.
1550: 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
1560: 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6c   char *sqlite3_l
1570: 69 62 76 65 72 73 69 6f 6e 28 76 6f 69 64 29 3b  ibversion(void);
1580: 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c 69 62  .int sqlite3_lib
1590: 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 76  version_number(v
15a0: 6f 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  oid);../*.** CAP
15b0: 49 33 52 45 46 3a 20 44 61 74 61 62 61 73 65 20  I3REF: Database 
15c0: 43 6f 6e 6e 65 63 74 69 6f 6e 20 48 61 6e 64 6c  Connection Handl
15d0: 65 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 6f 70 65  e.**.** Each ope
15e0: 6e 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73  n SQLite databas
15f0: 65 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 64  e is represented
1600: 20 62 79 20 70 6f 69 6e 74 65 72 20 74 6f 20 61   by pointer to a
1610: 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
1620: 65 0a 2a 2a 20 6f 70 61 71 75 65 20 73 74 72 75  e.** opaque stru
1630: 63 74 75 72 65 20 6e 61 6d 65 64 20 22 73 71 6c  cture named "sql
1640: 69 74 65 33 22 2e 20 20 49 74 20 69 73 20 75 73  ite3".  It is us
1650: 65 66 75 6c 20 74 6f 20 74 68 69 6e 6b 20 6f 66  eful to think of
1660: 20 61 6e 20 73 71 6c 69 74 65 33 0a 2a 2a 20 70   an sqlite3.** p
1670: 6f 69 6e 74 65 72 20 61 73 20 61 6e 20 6f 62 6a  ointer as an obj
1680: 65 63 74 2e 20 20 54 68 65 20 5b 73 71 6c 69 74  ect.  The [sqlit
1690: 65 33 5f 6f 70 65 6e 5d 20 69 6e 74 65 72 66 61  e3_open] interfa
16a0: 63 65 20 69 73 20 69 74 73 20 63 6f 6e 73 74 72  ce is its constr
16b0: 75 63 74 6f 72 0a 2a 2a 20 61 6e 64 20 5b 73 71  uctor.** and [sq
16c0: 6c 69 74 65 33 5f 63 6c 6f 73 65 5d 20 69 73 20  lite3_close] is 
16d0: 69 74 73 20 64 65 73 74 72 75 63 74 6f 72 2e 20  its destructor. 
16e0: 20 54 68 65 72 65 20 61 72 65 20 6d 61 6e 79 20   There are many 
16f0: 6f 74 68 65 72 20 69 6e 74 65 72 66 61 63 65 73  other interfaces
1700: 0a 2a 2a 20 28 73 75 63 68 20 61 73 20 5b 73 71  .** (such as [sq
1710: 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
1720: 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61  ], [sqlite3_crea
1730: 74 65 5f 66 75 6e 63 74 69 6f 6e 5d 2c 20 61 6e  te_function], an
1740: 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 75  d.** [sqlite3_bu
1750: 73 79 5f 74 69 6d 65 6f 75 74 5d 20 74 6f 20 6e  sy_timeout] to n
1760: 61 6d 65 20 62 75 74 20 74 68 72 65 65 29 20 74  ame but three) t
1770: 68 61 74 20 61 72 65 20 6d 65 74 68 6f 64 73 20  hat are methods 
1780: 6f 6e 20 74 68 69 73 0a 2a 2a 20 6f 62 6a 65 63  on this.** objec
1790: 74 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  t..*/.typedef st
17a0: 72 75 63 74 20 73 71 6c 69 74 65 33 20 73 71 6c  ruct sqlite3 sql
17b0: 69 74 65 33 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41  ite3;.../*.** CA
17c0: 50 49 33 52 45 46 3a 20 36 34 2d 42 69 74 20 49  PI3REF: 64-Bit I
17d0: 6e 74 65 67 65 72 20 54 79 70 65 73 0a 2a 2a 0a  nteger Types.**.
17e0: 2a 2a 20 53 6f 6d 65 20 63 6f 6d 70 69 6c 65 72  ** Some compiler
17f0: 73 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74  s do not support
1800: 20 74 68 65 20 22 6c 6f 6e 67 20 6c 6f 6e 67 22   the "long long"
1810: 20 64 61 74 61 74 79 70 65 2e 20 20 53 6f 20 77   datatype.  So w
1820: 65 20 68 61 76 65 0a 2a 2a 20 74 6f 20 64 6f 20  e have.** to do 
1830: 63 6f 6d 70 69 6c 65 72 2d 73 70 65 63 69 66 69  compiler-specifi
1840: 63 20 74 79 70 65 64 65 66 73 20 66 6f 72 20 36  c typedefs for 6
1850: 34 2d 62 69 74 20 73 69 67 6e 65 64 20 61 6e 64  4-bit signed and
1860: 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65   unsigned intege
1870: 72 73 2e 0a 2a 2a 0a 2a 2a 20 4d 61 6e 79 20 53  rs..**.** Many S
1880: 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 20  QLite interface 
1890: 66 75 6e 63 74 69 6f 6e 73 20 72 65 71 75 69 72  functions requir
18a0: 65 20 61 20 36 34 2d 62 69 74 20 69 6e 74 65 67  e a 64-bit integ
18b0: 65 72 20 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2a  er arguments..**
18c0: 20 54 68 6f 73 65 20 69 6e 74 65 72 66 61 63 65   Those interface
18d0: 73 20 61 72 65 20 64 65 63 6c 61 72 65 64 20 75  s are declared u
18e0: 73 69 6e 67 20 74 68 69 73 20 74 79 70 65 64 65  sing this typede
18f0: 66 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  f..*/.#ifdef SQL
1900: 49 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 0a 20  ITE_INT64_TYPE. 
1910: 20 74 79 70 65 64 65 66 20 53 51 4c 49 54 45 5f   typedef SQLITE_
1920: 49 4e 54 36 34 5f 54 59 50 45 20 73 71 6c 69 74  INT64_TYPE sqlit
1930: 65 5f 69 6e 74 36 34 3b 0a 20 20 74 79 70 65 64  e_int64;.  typed
1940: 65 66 20 75 6e 73 69 67 6e 65 64 20 53 51 4c 49  ef unsigned SQLI
1950: 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 20 73 71  TE_INT64_TYPE sq
1960: 6c 69 74 65 5f 75 69 6e 74 36 34 3b 0a 23 65 6c  lite_uint64;.#el
1970: 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f  if defined(_MSC_
1980: 56 45 52 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  VER) || defined(
1990: 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a 20 20  __BORLANDC__).  
19a0: 74 79 70 65 64 65 66 20 5f 5f 69 6e 74 36 34 20  typedef __int64 
19b0: 73 71 6c 69 74 65 5f 69 6e 74 36 34 3b 0a 20 20  sqlite_int64;.  
19c0: 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64  typedef unsigned
19d0: 20 5f 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 5f   __int64 sqlite_
19e0: 75 69 6e 74 36 34 3b 0a 23 65 6c 73 65 0a 20 20  uint64;.#else.  
19f0: 74 79 70 65 64 65 66 20 6c 6f 6e 67 20 6c 6f 6e  typedef long lon
1a00: 67 20 69 6e 74 20 73 71 6c 69 74 65 5f 69 6e 74  g int sqlite_int
1a10: 36 34 3b 0a 20 20 74 79 70 65 64 65 66 20 75 6e  64;.  typedef un
1a20: 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67  signed long long
1a30: 20 69 6e 74 20 73 71 6c 69 74 65 5f 75 69 6e 74   int sqlite_uint
1a40: 36 34 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  64;.#endif../*.*
1a50: 2a 20 49 66 20 63 6f 6d 70 69 6c 69 6e 67 20 66  * If compiling f
1a60: 6f 72 20 61 20 70 72 6f 63 65 73 73 6f 72 20 74  or a processor t
1a70: 68 61 74 20 6c 61 63 6b 73 20 66 6c 6f 61 74 69  hat lacks floati
1a80: 6e 67 20 70 6f 69 6e 74 20 73 75 70 70 6f 72 74  ng point support
1a90: 2c 0a 2a 2a 20 73 75 62 73 74 69 74 75 74 65 20  ,.** substitute 
1aa0: 69 6e 74 65 67 65 72 20 66 6f 72 20 66 6c 6f 61  integer for floa
1ab0: 74 69 6e 67 2d 70 6f 69 6e 74 0a 2a 2f 0a 23 69  ting-point.*/.#i
1ac0: 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
1ad0: 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a  _FLOATING_POINT.
1ae0: 23 20 64 65 66 69 6e 65 20 64 6f 75 62 6c 65 20  # define double 
1af0: 73 71 6c 69 74 65 5f 69 6e 74 36 34 0a 23 65 6e  sqlite_int64.#en
1b00: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  dif../*.** CAPI3
1b10: 52 45 46 3a 20 43 6c 6f 73 69 6e 67 20 41 20 44  REF: Closing A D
1b20: 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69  atabase Connecti
1b30: 6f 6e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 20 74 68  on.**.** Call th
1b40: 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68  is function with
1b50: 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
1b60: 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 77  structure that w
1b70: 61 73 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a  as previously.**
1b80: 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 5b   returned from [
1b90: 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 20  sqlite3_open()] 
1ba0: 61 6e 64 20 74 68 65 20 63 6f 72 72 65 73 70 6f  and the correspo
1bb0: 6e 64 69 6e 67 20 64 61 74 61 62 61 73 65 20 77  nding database w
1bc0: 69 6c 6c 20 62 79 0a 2a 2a 20 63 6c 6f 73 65 64  ill by.** closed
1bd0: 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 53 51 4c 20  ..**.** All SQL 
1be0: 73 74 61 74 65 6d 65 6e 74 73 20 70 72 65 70 61  statements prepa
1bf0: 72 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  red using [sqlit
1c00: 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
1c10: 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   or.** [sqlite3_
1c20: 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d 20  prepare16_v2()] 
1c30: 6d 75 73 74 20 62 65 20 64 65 73 74 72 6f 79 65  must be destroye
1c40: 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33  d using [sqlite3
1c50: 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 0a 2a 2a 20  _finalize()].** 
1c60: 62 65 66 6f 72 65 20 74 68 69 73 20 72 6f 75 74  before this rout
1c70: 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 4f  ine is called. O
1c80: 74 68 65 72 77 69 73 65 2c 20 53 51 4c 49 54 45  therwise, SQLITE
1c90: 5f 42 55 53 59 20 69 73 20 72 65 74 75 72 6e 65  _BUSY is returne
1ca0: 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 64 61 74  d and the.** dat
1cb0: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1cc0: 20 72 65 6d 61 69 6e 73 20 6f 70 65 6e 2e 0a 2a   remains open..*
1cd0: 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1ce0: 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 73   sqlite3_close(s
1cf0: 71 6c 69 74 65 33 20 2a 29 3b 0a 0a 2f 2a 0a 2a  qlite3 *);../*.*
1d00: 2a 20 54 68 65 20 74 79 70 65 20 66 6f 72 20 61  * The type for a
1d10: 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69   callback functi
1d20: 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 6c  on..** This is l
1d30: 65 67 61 63 79 20 61 6e 64 20 64 65 70 72 65 63  egacy and deprec
1d40: 61 74 65 64 2e 20 20 49 74 20 69 73 20 69 6e 63  ated.  It is inc
1d50: 6c 75 64 65 64 20 66 6f 72 20 68 69 73 74 6f 72  luded for histor
1d60: 69 63 61 6c 0a 2a 2a 20 63 6f 6d 70 61 74 69 62  ical.** compatib
1d70: 69 6c 69 74 79 20 61 6e 64 20 69 73 20 6e 6f 74  ility and is not
1d80: 20 64 6f 63 75 6d 65 6e 74 65 64 2e 0a 2a 2f 0a   documented..*/.
1d90: 74 79 70 65 64 65 66 20 69 6e 74 20 28 2a 73 71  typedef int (*sq
1da0: 6c 69 74 65 33 5f 63 61 6c 6c 62 61 63 6b 29 28  lite3_callback)(
1db0: 76 6f 69 64 2a 2c 69 6e 74 2c 63 68 61 72 2a 2a  void*,int,char**
1dc0: 2c 20 63 68 61 72 2a 2a 29 3b 0a 0a 2f 2a 0a 2a  , char**);../*.*
1dd0: 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 6e 65 2d  * CAPI3REF: One-
1de0: 53 74 65 70 20 51 75 65 72 79 20 45 78 65 63 75  Step Query Execu
1df0: 74 69 6f 6e 20 49 6e 74 65 72 66 61 63 65 0a 2a  tion Interface.*
1e00: 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66  *.** This interf
1e10: 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 64  ace is used to d
1e20: 6f 20 61 20 6f 6e 65 2d 74 69 6d 65 20 65 76 61  o a one-time eva
1e30: 6c 75 61 74 61 74 69 6f 6e 20 6f 66 20 7a 65 72  luatation of zer
1e40: 6f 0a 2a 2a 20 6f 72 20 6d 6f 72 65 20 53 51 4c  o.** or more SQL
1e50: 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 55 54   statements.  UT
1e60: 46 2d 38 20 74 65 78 74 20 6f 66 20 74 68 65 20  F-8 text of the 
1e70: 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 74  SQL statements t
1e80: 6f 0a 2a 2a 20 62 65 20 65 76 61 6c 75 74 65 64  o.** be evaluted
1e90: 20 69 73 20 70 61 73 73 65 64 20 69 6e 20 61 73   is passed in as
1ea0: 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61   the second para
1eb0: 6d 65 74 65 72 2e 20 20 54 68 65 20 73 74 61 74  meter.  The stat
1ec0: 65 6d 65 6e 74 73 0a 2a 2a 20 61 72 65 20 70 72  ements.** are pr
1ed0: 65 70 61 72 65 64 20 6f 6e 65 20 62 79 20 6f 6e  epared one by on
1ee0: 65 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33  e using [sqlite3
1ef0: 5f 70 72 65 70 61 72 65 28 29 5d 2c 20 65 76 61  _prepare()], eva
1f00: 6c 75 61 74 65 64 0a 2a 2a 20 75 73 69 6e 67 20  luated.** using 
1f10: 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
1f20: 2c 20 74 68 65 6e 20 64 65 73 74 72 6f 79 65 64  , then destroyed
1f30: 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f   using [sqlite3_
1f40: 66 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a 0a  finalize()]..**.
1f50: 2a 2a 20 49 66 20 6f 6e 65 20 6f 72 20 6d 6f 72  ** If one or mor
1f60: 65 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74 61  e of the SQL sta
1f70: 74 65 6d 65 6e 74 73 20 61 72 65 20 71 75 65 72  tements are quer
1f80: 69 65 73 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65  ies, then.** the
1f90: 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69   callback functi
1fa0: 6f 6e 20 73 70 65 63 69 66 69 65 64 20 62 79 20  on specified by 
1fb0: 74 68 65 20 33 72 64 20 70 61 72 61 6d 65 74 65  the 3rd paramete
1fc0: 72 20 69 73 0a 2a 2a 20 69 6e 76 6f 6b 65 64 20  r is.** invoked 
1fd0: 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 72 6f  once for each ro
1fe0: 77 20 6f 66 20 74 68 65 20 71 75 65 72 79 20 72  w of the query r
1ff0: 65 73 75 6c 74 2e 20 20 54 68 69 73 20 63 61 6c  esult.  This cal
2000: 6c 62 61 63 6b 0a 2a 2a 20 73 68 6f 75 6c 64 20  lback.** should 
2010: 6e 6f 72 6d 61 6c 6c 79 20 72 65 74 75 72 6e 20  normally return 
2020: 30 2e 20 20 49 66 20 74 68 65 20 63 61 6c 6c 62  0.  If the callb
2030: 61 63 6b 20 72 65 74 75 72 6e 73 20 61 20 6e 6f  ack returns a no
2040: 6e 2d 7a 65 72 6f 0a 2a 2a 20 76 61 6c 75 65 20  n-zero.** value 
2050: 74 68 65 6e 20 74 68 65 20 71 75 65 72 79 20 69  then the query i
2060: 73 20 61 62 6f 72 74 65 64 2c 20 61 6c 6c 20 73  s aborted, all s
2070: 75 62 73 65 71 75 65 6e 74 20 53 51 4c 20 73 74  ubsequent SQL st
2080: 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 61 72 65 20  atements.** are 
2090: 73 6b 69 70 70 65 64 20 61 6e 64 20 74 68 65 20  skipped and the 
20a0: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 66  sqlite3_exec() f
20b0: 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
20c0: 74 68 65 20 53 51 4c 49 54 45 5f 41 42 4f 52 54  the SQLITE_ABORT
20d0: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 34 74 68 20  ..**.** The 4th 
20e0: 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 69  parameter to thi
20f0: 73 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 61  s interface is a
2100: 6e 20 61 72 62 69 74 72 61 72 79 20 70 6f 69 6e  n arbitrary poin
2110: 74 65 72 20 74 68 61 74 20 69 73 0a 2a 2a 20 70  ter that is.** p
2120: 61 73 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f  assed through to
2130: 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75   the callback fu
2140: 6e 63 74 69 6f 6e 20 61 73 20 69 74 73 20 66 69  nction as its fi
2150: 72 73 74 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a  rst parameter..*
2160: 2a 0a 2a 2a 20 54 68 65 20 32 6e 64 20 70 61 72  *.** The 2nd par
2170: 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 63 61  ameter to the ca
2180: 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20  llback function 
2190: 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
21a0: 0a 2a 2a 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74  .** columns in t
21b0: 68 65 20 71 75 65 72 79 20 72 65 73 75 6c 74 2e  he query result.
21c0: 20 20 54 68 65 20 33 72 64 20 70 61 72 61 6d 65    The 3rd parame
21d0: 74 65 72 20 74 6f 20 74 68 65 20 63 61 6c 6c 62  ter to the callb
21e0: 61 63 6b 0a 2a 2a 20 69 73 20 61 6e 20 61 72 72  ack.** is an arr
21f0: 61 79 20 6f 66 20 73 74 72 69 6e 67 73 20 68 6f  ay of strings ho
2200: 6c 64 69 6e 67 20 74 68 65 20 76 61 6c 75 65 73  lding the values
2210: 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e   for each column
2220: 0a 2a 2a 20 61 73 20 65 78 74 72 61 63 74 65 64  .** as extracted
2230: 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f   using [sqlite3_
2240: 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 5d 2e 0a  column_text()]..
2250: 2a 2a 20 54 68 65 20 34 74 68 20 70 61 72 61 6d  ** The 4th param
2260: 65 74 65 72 20 74 6f 20 74 68 65 20 63 61 6c 6c  eter to the call
2270: 62 61 63 6b 20 69 73 20 61 6e 20 61 72 72 61 79  back is an array
2280: 20 6f 66 20 73 74 72 69 6e 67 73 0a 2a 2a 20 6f   of strings.** o
2290: 62 74 61 69 6e 65 64 20 75 73 69 6e 67 20 5b 73  btained using [s
22a0: 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61  qlite3_column_na
22b0: 6d 65 28 29 5d 20 61 6e 64 20 68 6f 6c 64 69 6e  me()] and holdin
22c0: 67 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 73 20 6f  g.** the names o
22d0: 66 20 65 61 63 68 20 63 6f 6c 75 6d 6e 2e 0a 2a  f each column..*
22e0: 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63  *.** The callbac
22f0: 6b 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62  k function may b
2300: 65 20 4e 55 4c 4c 2c 20 65 76 65 6e 20 66 6f 72  e NULL, even for
2310: 20 71 75 65 72 69 65 73 2e 20 20 41 20 4e 55 4c   queries.  A NUL
2320: 4c 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 69 73  L.** callback is
2330: 20 6e 6f 74 20 61 6e 20 65 72 72 6f 72 2e 20 20   not an error.  
2340: 49 74 20 6a 75 73 74 20 6d 65 61 6e 73 20 74 68  It just means th
2350: 61 74 20 6e 6f 20 63 61 6c 6c 62 61 63 6b 0a 2a  at no callback.*
2360: 2a 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65  * will be invoke
2370: 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65  d..**.** If an e
2380: 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c  rror occurs whil
2390: 65 20 70 61 72 73 69 6e 67 20 6f 72 20 65 76 61  e parsing or eva
23a0: 6c 75 61 74 69 6e 67 20 74 68 65 20 53 51 4c 20  luating the SQL 
23b0: 28 62 75 74 0a 2a 2a 20 6e 6f 74 20 77 68 69 6c  (but.** not whil
23c0: 65 20 65 78 65 63 75 74 69 6e 67 20 74 68 65 20  e executing the 
23d0: 63 61 6c 6c 62 61 63 6b 29 20 74 68 65 6e 20 61  callback) then a
23e0: 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 65 72  n appropriate er
23f0: 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 69  ror.** message i
2400: 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 6d  s written into m
2410: 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66  emory obtained f
2420: 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c  rom [sqlite3_mal
2430: 6c 6f 63 28 29 5d 20 61 6e 64 0a 2a 2a 20 2a 65  loc()] and.** *e
2440: 72 72 6d 73 67 20 69 73 20 6d 61 64 65 20 74 6f  rrmsg is made to
2450: 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 20 6d   point to that m
2460: 65 73 73 61 67 65 2e 20 20 54 68 65 20 63 61 6c  essage.  The cal
2470: 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a  ling function.**
2480: 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20   is responsible 
2490: 66 6f 72 20 66 72 65 65 69 6e 67 20 74 68 65 20  for freeing the 
24a0: 6d 65 6d 6f 72 79 20 74 68 61 74 20 68 6f 6c 64  memory that hold
24b0: 73 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 6d  s the error.** m
24c0: 65 73 73 61 67 65 2e 20 20 20 55 73 65 20 5b 73  essage.   Use [s
24d0: 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 20 66  qlite3_free()] f
24e0: 6f 72 20 74 68 69 73 2e 20 20 49 66 20 65 72 72  or this.  If err
24f0: 6d 73 67 3d 3d 4e 55 4c 4c 2c 0a 2a 2a 20 74 68  msg==NULL,.** th
2500: 65 6e 20 6e 6f 20 65 72 72 6f 72 20 6d 65 73 73  en no error mess
2510: 61 67 65 20 69 73 20 65 76 65 72 20 77 72 69 74  age is ever writ
2520: 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  ten..**.** The r
2530: 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 69  eturn value is i
2540: 73 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 74  s SQLITE_OK if t
2550: 68 65 72 65 20 61 72 65 20 6e 6f 20 65 72 72 6f  here are no erro
2560: 72 73 20 61 6e 64 0a 2a 2a 20 73 6f 6d 65 20 6f  rs and.** some o
2570: 74 68 65 72 20 5b 53 51 4c 49 54 45 5f 4f 4b 20  ther [SQLITE_OK 
2580: 7c 20 72 65 74 75 72 6e 20 63 6f 64 65 5d 20 69  | return code] i
2590: 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 65 72  f there is an er
25a0: 72 6f 72 2e 20 20 0a 2a 2a 20 54 68 65 20 70 61  ror.  .** The pa
25b0: 72 74 69 63 75 6c 61 72 20 72 65 74 75 72 6e 20  rticular return 
25c0: 76 61 6c 75 65 20 64 65 70 65 6e 64 73 20 6f 6e  value depends on
25d0: 20 74 68 65 20 74 79 70 65 20 6f 66 20 65 72 72   the type of err
25e0: 6f 72 2e 20 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54  or. .**.*/.SQLIT
25f0: 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
2600: 33 5f 65 78 65 63 28 0a 20 20 73 71 6c 69 74 65  3_exec(.  sqlite
2610: 33 2a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  3*,             
2620: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2630: 20 20 20 20 20 2f 2a 20 41 6e 20 6f 70 65 6e 20       /* An open 
2640: 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 63 6f  database */.  co
2650: 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 2c 20 20  nst char *sql,  
2660: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2670: 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20           /* SQL 
2680: 74 6f 20 62 65 20 65 76 61 6c 75 74 65 64 20 2a  to be evaluted *
2690: 2f 0a 20 20 69 6e 74 20 28 2a 63 61 6c 6c 62 61  /.  int (*callba
26a0: 63 6b 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 68  ck)(void*,int,ch
26b0: 61 72 2a 2a 2c 63 68 61 72 2a 2a 29 2c 20 20 2f  ar**,char**),  /
26c0: 2a 20 43 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  * Callback funct
26d0: 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 2c  ion */.  void *,
26e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2700: 20 20 20 20 2f 2a 20 31 73 74 20 61 72 67 75 6d      /* 1st argum
2710: 65 6e 74 20 74 6f 20 63 61 6c 6c 62 61 63 6b 20  ent to callback 
2720: 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 65 72 72 6d  */.  char **errm
2730: 73 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20  sg              
2740: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2750: 2f 2a 20 45 72 72 6f 72 20 6d 73 67 20 77 72 69  /* Error msg wri
2760: 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 29 3b 0a  tten here */.);.
2770: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
2780: 20 52 65 73 75 6c 74 20 43 6f 64 65 73 0a 2a 2a   Result Codes.**
2790: 20 4b 45 59 57 4f 52 44 53 3a 20 53 51 4c 49 54   KEYWORDS: SQLIT
27a0: 45 5f 4f 4b 0a 2a 2a 0a 2a 2a 20 4d 61 6e 79 20  E_OK.**.** Many 
27b0: 53 51 4c 69 74 65 20 66 75 6e 63 74 69 6f 6e 73  SQLite functions
27c0: 20 72 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67   return an integ
27d0: 65 72 20 72 65 73 75 6c 74 20 63 6f 64 65 20 66  er result code f
27e0: 72 6f 6d 20 74 68 65 20 73 65 74 20 73 68 6f 77  rom the set show
27f0: 6e 0a 2a 2a 20 61 62 6f 76 65 20 69 6e 20 6f 72  n.** above in or
2800: 64 65 72 20 74 6f 20 69 6e 64 69 63 61 74 65 73  der to indicates
2810: 20 73 75 63 63 65 73 73 20 6f 72 20 66 61 69 6c   success or fail
2820: 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  ure..**.** The r
2830: 65 73 75 6c 74 20 63 6f 64 65 73 20 61 62 6f 76  esult codes abov
2840: 65 20 61 72 65 20 74 68 65 20 6f 6e 6c 79 20 6f  e are the only o
2850: 6e 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 20  nes returned by 
2860: 53 51 4c 69 74 65 20 69 6e 20 69 74 73 0a 2a 2a  SQLite in its.**
2870: 20 64 65 66 61 75 6c 74 20 63 6f 6e 66 69 67 75   default configu
2880: 72 61 74 69 6f 6e 2e 20 20 48 6f 77 65 76 65 72  ration.  However
2890: 2c 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 65  , the [sqlite3_e
28a0: 78 74 65 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63  xtended_result_c
28b0: 6f 64 65 73 28 29 5d 0a 2a 2a 20 41 50 49 20 63  odes()].** API c
28c0: 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73 65  an be used to se
28d0: 74 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e  t a database con
28e0: 6e 65 63 74 6f 69 6e 20 74 6f 20 72 65 74 75 72  nectoin to retur
28f0: 6e 20 6d 6f 72 65 20 64 65 74 61 69 6c 65 64 0a  n more detailed.
2900: 2a 2a 20 72 65 73 75 6c 74 20 63 6f 64 65 73 2e  ** result codes.
2910: 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  .**.** See also:
2920: 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52   [SQLITE_IOERR_R
2930: 45 41 44 20 7c 20 65 78 74 65 6e 64 65 64 20 72  EAD | extended r
2940: 65 73 75 6c 74 20 63 6f 64 65 73 5d 0a 2a 2a 0a  esult codes].**.
2950: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
2960: 45 5f 4f 4b 20 20 20 20 20 20 20 20 20 20 20 30  E_OK           0
2970: 20 20 20 2f 2a 20 53 75 63 63 65 73 73 66 75 6c     /* Successful
2980: 20 72 65 73 75 6c 74 20 2a 2f 0a 2f 2a 20 62 65   result */./* be
2990: 67 69 6e 6e 69 6e 67 2d 6f 66 2d 65 72 72 6f 72  ginning-of-error
29a0: 2d 63 6f 64 65 73 20 2a 2f 0a 23 64 65 66 69 6e  -codes */.#defin
29b0: 65 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 20  e SQLITE_ERROR  
29c0: 20 20 20 20 20 20 31 20 20 20 2f 2a 20 53 51 4c        1   /* SQL
29d0: 20 65 72 72 6f 72 20 6f 72 20 6d 69 73 73 69 6e   error or missin
29e0: 67 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 23 64  g database */.#d
29f0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54  efine SQLITE_INT
2a00: 45 52 4e 41 4c 20 20 20 20 20 32 20 20 20 2f 2a  ERNAL     2   /*
2a10: 20 4e 4f 54 20 55 53 45 44 2e 20 49 6e 74 65 72   NOT USED. Inter
2a20: 6e 61 6c 20 6c 6f 67 69 63 20 65 72 72 6f 72 20  nal logic error 
2a30: 69 6e 20 53 51 4c 69 74 65 20 2a 2f 0a 23 64 65  in SQLite */.#de
2a40: 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 45 52 4d  fine SQLITE_PERM
2a50: 20 20 20 20 20 20 20 20 20 33 20 20 20 2f 2a 20           3   /* 
2a60: 41 63 63 65 73 73 20 70 65 72 6d 69 73 73 69 6f  Access permissio
2a70: 6e 20 64 65 6e 69 65 64 20 2a 2f 0a 23 64 65 66  n denied */.#def
2a80: 69 6e 65 20 53 51 4c 49 54 45 5f 41 42 4f 52 54  ine SQLITE_ABORT
2a90: 20 20 20 20 20 20 20 20 34 20 20 20 2f 2a 20 43          4   /* C
2aa0: 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 20  allback routine 
2ab0: 72 65 71 75 65 73 74 65 64 20 61 6e 20 61 62 6f  requested an abo
2ac0: 72 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  rt */.#define SQ
2ad0: 4c 49 54 45 5f 42 55 53 59 20 20 20 20 20 20 20  LITE_BUSY       
2ae0: 20 20 35 20 20 20 2f 2a 20 54 68 65 20 64 61 74    5   /* The dat
2af0: 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 6c 6f  abase file is lo
2b00: 63 6b 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20  cked */.#define 
2b10: 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 20 20  SQLITE_LOCKED   
2b20: 20 20 20 20 36 20 20 20 2f 2a 20 41 20 74 61 62      6   /* A tab
2b30: 6c 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  le in the databa
2b40: 73 65 20 69 73 20 6c 6f 63 6b 65 64 20 2a 2f 0a  se is locked */.
2b50: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e  #define SQLITE_N
2b60: 4f 4d 45 4d 20 20 20 20 20 20 20 20 37 20 20 20  OMEM        7   
2b70: 2f 2a 20 41 20 6d 61 6c 6c 6f 63 28 29 20 66 61  /* A malloc() fa
2b80: 69 6c 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20  iled */.#define 
2b90: 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20  SQLITE_READONLY 
2ba0: 20 20 20 20 38 20 20 20 2f 2a 20 41 74 74 65 6d      8   /* Attem
2bb0: 70 74 20 74 6f 20 77 72 69 74 65 20 61 20 72 65  pt to write a re
2bc0: 61 64 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 20  adonly database 
2bd0: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
2be0: 45 5f 49 4e 54 45 52 52 55 50 54 20 20 20 20 39  E_INTERRUPT    9
2bf0: 20 20 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20     /* Operation 
2c00: 74 65 72 6d 69 6e 61 74 65 64 20 62 79 20 73 71  terminated by sq
2c10: 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28  lite3_interrupt(
2c20: 29 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  )*/.#define SQLI
2c30: 54 45 5f 49 4f 45 52 52 20 20 20 20 20 20 20 31  TE_IOERR       1
2c40: 30 20 20 20 2f 2a 20 53 6f 6d 65 20 6b 69 6e 64  0   /* Some kind
2c50: 20 6f 66 20 64 69 73 6b 20 49 2f 4f 20 65 72 72   of disk I/O err
2c60: 6f 72 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a 23  or occurred */.#
2c70: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f  define SQLITE_CO
2c80: 52 52 55 50 54 20 20 20 20 20 31 31 20 20 20 2f  RRUPT     11   /
2c90: 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 64  * The database d
2ca0: 69 73 6b 20 69 6d 61 67 65 20 69 73 20 6d 61 6c  isk image is mal
2cb0: 66 6f 72 6d 65 64 20 2a 2f 0a 23 64 65 66 69 6e  formed */.#defin
2cc0: 65 20 53 51 4c 49 54 45 5f 4e 4f 54 46 4f 55 4e  e SQLITE_NOTFOUN
2cd0: 44 20 20 20 20 31 32 20 20 20 2f 2a 20 4e 4f 54  D    12   /* NOT
2ce0: 20 55 53 45 44 2e 20 54 61 62 6c 65 20 6f 72 20   USED. Table or 
2cf0: 72 65 63 6f 72 64 20 6e 6f 74 20 66 6f 75 6e 64  record not found
2d00: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
2d10: 54 45 5f 46 55 4c 4c 20 20 20 20 20 20 20 20 31  TE_FULL        1
2d20: 33 20 20 20 2f 2a 20 49 6e 73 65 72 74 69 6f 6e  3   /* Insertion
2d30: 20 66 61 69 6c 65 64 20 62 65 63 61 75 73 65 20   failed because 
2d40: 64 61 74 61 62 61 73 65 20 69 73 20 66 75 6c 6c  database is full
2d50: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
2d60: 54 45 5f 43 41 4e 54 4f 50 45 4e 20 20 20 20 31  TE_CANTOPEN    1
2d70: 34 20 20 20 2f 2a 20 55 6e 61 62 6c 65 20 74 6f  4   /* Unable to
2d80: 20 6f 70 65 6e 20 74 68 65 20 64 61 74 61 62 61   open the databa
2d90: 73 65 20 66 69 6c 65 20 2a 2f 0a 23 64 65 66 69  se file */.#defi
2da0: 6e 65 20 53 51 4c 49 54 45 5f 50 52 4f 54 4f 43  ne SQLITE_PROTOC
2db0: 4f 4c 20 20 20 20 31 35 20 20 20 2f 2a 20 4e 4f  OL    15   /* NO
2dc0: 54 20 55 53 45 44 2e 20 44 61 74 61 62 61 73 65  T USED. Database
2dd0: 20 6c 6f 63 6b 20 70 72 6f 74 6f 63 6f 6c 20 65   lock protocol e
2de0: 72 72 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20  rror */.#define 
2df0: 53 51 4c 49 54 45 5f 45 4d 50 54 59 20 20 20 20  SQLITE_EMPTY    
2e00: 20 20 20 31 36 20 20 20 2f 2a 20 44 61 74 61 62     16   /* Datab
2e10: 61 73 65 20 69 73 20 65 6d 70 74 79 20 2a 2f 0a  ase is empty */.
2e20: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53  #define SQLITE_S
2e30: 43 48 45 4d 41 20 20 20 20 20 20 31 37 20 20 20  CHEMA      17   
2e40: 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
2e50: 73 63 68 65 6d 61 20 63 68 61 6e 67 65 64 20 2a  schema changed *
2e60: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
2e70: 5f 54 4f 4f 42 49 47 20 20 20 20 20 20 31 38 20  _TOOBIG      18 
2e80: 20 20 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20 42    /* String or B
2e90: 4c 4f 42 20 65 78 63 65 65 64 73 20 73 69 7a 65  LOB exceeds size
2ea0: 20 6c 69 6d 69 74 20 2a 2f 0a 23 64 65 66 69 6e   limit */.#defin
2eb0: 65 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41  e SQLITE_CONSTRA
2ec0: 49 4e 54 20 20 31 39 20 20 20 2f 2a 20 41 62 6f  INT  19   /* Abo
2ed0: 72 74 20 64 75 65 20 74 6f 20 63 6f 6e 74 72 61  rt due to contra
2ee0: 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20 2a 2f  int violation */
2ef0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
2f00: 4d 49 53 4d 41 54 43 48 20 20 20 20 32 30 20 20  MISMATCH    20  
2f10: 20 2f 2a 20 44 61 74 61 20 74 79 70 65 20 6d 69   /* Data type mi
2f20: 73 6d 61 74 63 68 20 2a 2f 0a 23 64 65 66 69 6e  smatch */.#defin
2f30: 65 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 20  e SQLITE_MISUSE 
2f40: 20 20 20 20 20 32 31 20 20 20 2f 2a 20 4c 69 62       21   /* Lib
2f50: 72 61 72 79 20 75 73 65 64 20 69 6e 63 6f 72 72  rary used incorr
2f60: 65 63 74 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65  ectly */.#define
2f70: 20 53 51 4c 49 54 45 5f 4e 4f 4c 46 53 20 20 20   SQLITE_NOLFS   
2f80: 20 20 20 20 32 32 20 20 20 2f 2a 20 55 73 65 73      22   /* Uses
2f90: 20 4f 53 20 66 65 61 74 75 72 65 73 20 6e 6f 74   OS features not
2fa0: 20 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 68 6f   supported on ho
2fb0: 73 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  st */.#define SQ
2fc0: 4c 49 54 45 5f 41 55 54 48 20 20 20 20 20 20 20  LITE_AUTH       
2fd0: 20 32 33 20 20 20 2f 2a 20 41 75 74 68 6f 72 69   23   /* Authori
2fe0: 7a 61 74 69 6f 6e 20 64 65 6e 69 65 64 20 2a 2f  zation denied */
2ff0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
3000: 46 4f 52 4d 41 54 20 20 20 20 20 20 32 34 20 20  FORMAT      24  
3010: 20 2f 2a 20 41 75 78 69 6c 69 61 72 79 20 64 61   /* Auxiliary da
3020: 74 61 62 61 73 65 20 66 6f 72 6d 61 74 20 65 72  tabase format er
3030: 72 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ror */.#define S
3040: 51 4c 49 54 45 5f 52 41 4e 47 45 20 20 20 20 20  QLITE_RANGE     
3050: 20 20 32 35 20 20 20 2f 2a 20 32 6e 64 20 70 61    25   /* 2nd pa
3060: 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74  rameter to sqlit
3070: 65 33 5f 62 69 6e 64 20 6f 75 74 20 6f 66 20 72  e3_bind out of r
3080: 61 6e 67 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ange */.#define 
3090: 53 51 4c 49 54 45 5f 4e 4f 54 41 44 42 20 20 20  SQLITE_NOTADB   
30a0: 20 20 20 32 36 20 20 20 2f 2a 20 46 69 6c 65 20     26   /* File 
30b0: 6f 70 65 6e 65 64 20 74 68 61 74 20 69 73 20 6e  opened that is n
30c0: 6f 74 20 61 20 64 61 74 61 62 61 73 65 20 66 69  ot a database fi
30d0: 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  le */.#define SQ
30e0: 4c 49 54 45 5f 52 4f 57 20 20 20 20 20 20 20 20  LITE_ROW        
30f0: 20 31 30 30 20 20 2f 2a 20 73 71 6c 69 74 65 33   100  /* sqlite3
3100: 5f 73 74 65 70 28 29 20 68 61 73 20 61 6e 6f 74  _step() has anot
3110: 68 65 72 20 72 6f 77 20 72 65 61 64 79 20 2a 2f  her row ready */
3120: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
3130: 44 4f 4e 45 20 20 20 20 20 20 20 20 31 30 31 20  DONE        101 
3140: 20 2f 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70   /* sqlite3_step
3150: 28 29 20 68 61 73 20 66 69 6e 69 73 68 65 64 20  () has finished 
3160: 65 78 65 63 75 74 69 6e 67 20 2a 2f 0a 2f 2a 20  executing */./* 
3170: 65 6e 64 2d 6f 66 2d 65 72 72 6f 72 2d 63 6f 64  end-of-error-cod
3180: 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  es */../*.** CAP
3190: 49 33 52 45 46 3a 20 45 78 74 65 6e 64 65 64 20  I3REF: Extended 
31a0: 52 65 73 75 6c 74 20 43 6f 64 65 73 0a 2a 2a 0a  Result Codes.**.
31b0: 2a 2a 20 49 6e 20 69 74 73 20 64 65 66 61 75 6c  ** In its defaul
31c0: 74 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2c  t configuration,
31d0: 20 53 51 4c 69 74 65 20 41 50 49 20 72 6f 75 74   SQLite API rout
31e0: 69 6e 65 73 20 72 65 74 75 72 6e 20 6f 6e 65 20  ines return one 
31f0: 6f 66 20 32 36 20 69 6e 74 65 67 65 72 0a 2a 2a  of 26 integer.**
3200: 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 64 65   result codes de
3210: 73 63 72 69 62 65 64 20 61 74 20 72 65 73 75 6c  scribed at resul
3220: 74 2d 63 6f 64 65 73 2e 20 20 48 6f 77 65 76 65  t-codes.  Howeve
3230: 72 2c 20 65 78 70 65 72 69 65 6e 63 65 20 68 61  r, experience ha
3240: 73 20 73 68 6f 77 6e 20 74 68 61 74 0a 2a 2a 20  s shown that.** 
3250: 6d 61 6e 79 20 6f 66 20 74 68 65 73 65 20 72 65  many of these re
3260: 73 75 6c 74 20 63 6f 64 65 73 20 61 72 65 20 74  sult codes are t
3270: 6f 6f 20 63 6f 75 72 73 65 2d 67 72 61 69 6e 65  oo course-graine
3280: 64 2e 20 20 54 68 65 79 20 64 6f 20 6e 6f 74 20  d.  They do not 
3290: 70 72 6f 76 69 64 65 20 61 73 0a 2a 2a 20 6d 75  provide as.** mu
32a0: 63 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  ch information a
32b0: 62 6f 75 74 20 70 72 6f 62 6c 65 6d 73 20 61 73  bout problems as
32c0: 20 75 73 65 72 73 20 6d 69 67 68 74 20 6c 69 6b   users might lik
32d0: 65 2e 20 20 49 6e 20 61 6e 20 65 66 66 6f 72 74  e.  In an effort
32e0: 20 74 6f 0a 2a 2a 20 61 64 64 72 65 73 73 20 74   to.** address t
32f0: 68 69 73 2c 20 6e 65 77 65 72 20 76 65 72 73 69  his, newer versi
3300: 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 28 76  ons of SQLite (v
3310: 65 72 73 69 6f 6e 20 33 2e 33 2e 38 20 61 6e 64  ersion 3.3.8 and
3320: 20 6c 61 74 65 72 29 20 69 6e 63 6c 75 64 65 0a   later) include.
3330: 2a 2a 20 73 75 70 70 6f 72 74 20 66 6f 72 20 61  ** support for a
3340: 64 64 69 74 69 6f 6e 61 6c 20 72 65 73 75 6c 74  dditional result
3350: 20 63 6f 64 65 73 20 74 68 61 74 20 70 72 6f 76   codes that prov
3360: 69 64 65 20 6d 6f 72 65 20 64 65 74 61 69 6c 65  ide more detaile
3370: 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a  d information.**
3380: 20 61 62 6f 75 74 20 65 72 72 6f 72 73 2e 20 20   about errors.  
3390: 54 68 65 20 65 78 74 65 6e 64 65 64 20 72 65 73  The extended res
33a0: 75 6c 74 20 63 6f 64 65 73 20 61 72 65 20 65 6e  ult codes are en
33b0: 61 62 6c 65 64 20 28 6f 72 20 64 69 73 61 62 6c  abled (or disabl
33c0: 65 64 29 20 66 6f 72 20 0a 2a 2a 20 65 61 63 68  ed) for .** each
33d0: 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e   database.** con
33e0: 6e 65 63 74 69 6f 6e 20 75 73 69 6e 67 20 74 68  nection using th
33f0: 65 20 5b 73 71 6c 69 74 65 33 5f 65 78 74 65 6e  e [sqlite3_exten
3400: 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73  ded_result_codes
3410: 28 29 5d 20 41 50 49 2e 0a 2a 2a 20 0a 2a 2a 20  ()] API..** .** 
3420: 53 6f 6d 65 20 6f 66 20 74 68 65 20 61 76 61 69  Some of the avai
3430: 6c 61 62 6c 65 20 65 78 74 65 6e 64 65 64 20 72  lable extended r
3440: 65 73 75 6c 74 20 63 6f 64 65 73 20 61 72 65 20  esult codes are 
3450: 6c 69 73 74 65 64 20 61 62 6f 76 65 2e 0a 2a 2a  listed above..**
3460: 20 57 65 20 65 78 70 65 63 74 20 74 68 65 20 6e   We expect the n
3470: 75 6d 62 65 72 20 6f 66 20 65 78 74 65 6e 64 65  umber of extende
3480: 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 77  d result codes w
3490: 69 6c 6c 20 62 65 20 65 78 70 61 6e 64 0a 2a 2a  ill be expand.**
34a0: 20 6f 76 65 72 20 74 69 6d 65 2e 20 20 53 6f 66   over time.  Sof
34b0: 74 77 61 72 65 20 74 68 61 74 20 75 73 65 73 20  tware that uses 
34c0: 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20  extended result 
34d0: 63 6f 64 65 73 20 73 68 6f 75 6c 64 20 65 78 70  codes should exp
34e0: 65 63 74 0a 2a 2a 20 74 6f 20 73 65 65 20 6e 65  ect.** to see ne
34f0: 77 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 69  w result codes i
3500: 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65  n future release
3510: 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 20  s of SQLite..** 
3520: 0a 2a 2a 20 54 68 65 20 73 79 6d 62 6f 6c 69 63  .** The symbolic
3530: 20 6e 61 6d 65 20 66 6f 72 20 61 6e 20 65 78 74   name for an ext
3540: 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64  ended result cod
3550: 65 20 61 6c 77 61 79 73 20 63 6f 6e 74 61 69 6e  e always contain
3560: 73 20 61 20 72 65 6c 61 74 65 64 0a 2a 2a 20 70  s a related.** p
3570: 72 69 6d 61 72 79 20 72 65 73 75 6c 74 20 63 6f  rimary result co
3580: 64 65 20 61 73 20 61 20 70 72 65 66 69 78 2e 20  de as a prefix. 
3590: 20 50 72 69 6d 61 72 79 20 72 65 73 75 6c 74 20   Primary result 
35a0: 63 6f 64 65 73 20 63 6f 6e 74 61 69 6e 20 61 20  codes contain a 
35b0: 73 69 6e 67 6c 65 0a 2a 2a 20 22 5f 22 20 63 68  single.** "_" ch
35c0: 61 72 61 63 74 65 72 2e 20 20 45 78 74 65 6e 64  aracter.  Extend
35d0: 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20  ed result codes 
35e0: 63 6f 6e 74 61 69 6e 20 74 77 6f 20 6f 72 20 6d  contain two or m
35f0: 6f 72 65 20 22 5f 22 20 63 68 61 72 61 63 74 65  ore "_" characte
3600: 72 73 2e 0a 2a 2a 20 54 68 65 20 6e 75 6d 65 72  rs..** The numer
3610: 69 63 20 76 61 6c 75 65 20 6f 66 20 61 6e 20 65  ic value of an e
3620: 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63  xtended result c
3630: 6f 64 65 20 63 61 6e 20 62 65 20 63 6f 6e 76 65  ode can be conve
3640: 72 74 65 64 20 74 6f 20 69 74 73 0a 2a 2a 20 63  rted to its.** c
3650: 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 70 72 69  orresponding pri
3660: 6d 61 72 79 20 72 65 73 75 6c 74 20 63 6f 64 65  mary result code
3670: 20 62 79 20 6d 61 73 6b 69 6e 67 20 6f 66 66 20   by masking off 
3680: 74 68 65 20 6c 6f 77 65 72 20 38 20 62 79 74 65  the lower 8 byte
3690: 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c  s..**.** The SQL
36a0: 49 54 45 5f 4f 4b 20 72 65 73 75 6c 74 20 63 6f  ITE_OK result co
36b0: 64 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65  de will never be
36c0: 20 65 78 74 65 6e 64 65 64 2e 20 20 49 74 20 77   extended.  It w
36d0: 69 6c 6c 20 61 6c 77 61 79 73 0a 2a 2a 20 62 65  ill always.** be
36e0: 20 65 78 61 63 74 6c 79 20 7a 65 72 6f 2e 0a 2a   exactly zero..*
36f0: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
3700: 5f 49 4f 45 52 52 5f 52 45 41 44 20 20 20 20 20  _IOERR_READ     
3710: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45       (SQLITE_IOE
3720: 52 52 20 7c 20 28 31 3c 3c 38 29 29 0a 23 64 65  RR | (1<<8)).#de
3730: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52  fine SQLITE_IOER
3740: 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 20 20 20  R_SHORT_READ    
3750: 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20  (SQLITE_IOERR | 
3760: 28 32 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20  (2<<8)).#define 
3770: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52 49  SQLITE_IOERR_WRI
3780: 54 45 20 20 20 20 20 20 20 20 20 28 53 51 4c 49  TE         (SQLI
3790: 54 45 5f 49 4f 45 52 52 20 7c 20 28 33 3c 3c 38  TE_IOERR | (3<<8
37a0: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
37b0: 45 5f 49 4f 45 52 52 5f 46 53 59 4e 43 20 20 20  E_IOERR_FSYNC   
37c0: 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f        (SQLITE_IO
37d0: 45 52 52 20 7c 20 28 34 3c 3c 38 29 29 0a 23 64  ERR | (4<<8)).#d
37e0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45  efine SQLITE_IOE
37f0: 52 52 5f 44 49 52 5f 46 53 59 4e 43 20 20 20 20  RR_DIR_FSYNC    
3800: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c   (SQLITE_IOERR |
3810: 20 28 35 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65   (5<<8)).#define
3820: 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52   SQLITE_IOERR_TR
3830: 55 4e 43 41 54 45 20 20 20 20 20 20 28 53 51 4c  UNCATE      (SQL
3840: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 36 3c 3c  ITE_IOERR | (6<<
3850: 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  8)).#define SQLI
3860: 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 20 20  TE_IOERR_FSTAT  
3870: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49         (SQLITE_I
3880: 4f 45 52 52 20 7c 20 28 37 3c 3c 38 29 29 0a 23  OERR | (7<<8)).#
3890: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f  define SQLITE_IO
38a0: 45 52 52 5f 55 4e 4c 4f 43 4b 20 20 20 20 20 20  ERR_UNLOCK      
38b0: 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20    (SQLITE_IOERR 
38c0: 7c 20 28 38 3c 3c 38 29 29 0a 23 64 65 66 69 6e  | (8<<8)).#defin
38d0: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52  e SQLITE_IOERR_R
38e0: 44 4c 4f 43 4b 20 20 20 20 20 20 20 20 28 53 51  DLOCK        (SQ
38f0: 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 39 3c  LITE_IOERR | (9<
3900: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c  <8)).#define SQL
3910: 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45  ITE_IOERR_DELETE
3920: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f          (SQLITE_
3930: 49 4f 45 52 52 20 7c 20 28 31 30 3c 3c 38 29 29  IOERR | (10<<8))
3940: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
3950: 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 20 20  IOERR_BLOCKED   
3960: 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52      (SQLITE_IOER
3970: 52 20 7c 20 28 31 31 3c 3c 38 29 29 0a 0a 2f 2a  R | (11<<8))../*
3980: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 6e  .** CAPI3REF: En
3990: 61 62 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 20  able Or Disable 
39a0: 45 78 74 65 6e 64 65 64 20 52 65 73 75 6c 74 20  Extended Result 
39b0: 43 6f 64 65 73 0a 2a 2a 0a 2a 2a 20 54 68 69 73  Codes.**.** This
39c0: 20 72 6f 75 74 69 6e 65 20 65 6e 61 62 6c 65 73   routine enables
39d0: 20 6f 72 20 64 69 73 61 62 6c 65 73 20 74 68 65   or disables the
39e0: 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52  .** [SQLITE_IOER
39f0: 52 5f 52 45 41 44 20 7c 20 65 78 74 65 6e 64 65  R_READ | extende
3a00: 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 20  d result codes] 
3a10: 66 65 61 74 75 72 65 2e 0a 2a 2a 20 42 79 20 64  feature..** By d
3a20: 65 66 61 75 6c 74 2c 20 53 51 4c 69 74 65 20 41  efault, SQLite A
3a30: 50 49 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75  PI routines retu
3a40: 72 6e 20 6f 6e 65 20 6f 66 20 6f 6e 6c 79 20 32  rn one of only 2
3a50: 36 20 69 6e 74 65 67 65 72 0a 2a 2a 20 5b 53 51  6 integer.** [SQ
3a60: 4c 49 54 45 5f 4f 4b 20 7c 20 72 65 73 75 6c 74  LITE_OK | result
3a70: 20 63 6f 64 65 73 5d 2e 20 20 57 68 65 6e 20 65   codes].  When e
3a80: 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63  xtended result c
3a90: 6f 64 65 73 0a 2a 2a 20 61 72 65 20 65 6e 61 62  odes.** are enab
3aa0: 6c 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74  led by this rout
3ab0: 69 6e 65 2c 20 74 68 65 20 72 65 70 65 74 6f 69  ine, the repetoi
3ac0: 72 65 20 6f 66 20 72 65 73 75 6c 74 20 63 6f 64  re of result cod
3ad0: 65 73 20 63 61 6e 20 62 65 0a 2a 2a 20 6d 75 63  es can be.** muc
3ae0: 68 20 6c 61 72 67 65 72 20 61 6e 64 20 63 61 6e  h larger and can
3af0: 20 28 68 6f 70 65 66 75 6c 6c 79 29 20 70 72 6f   (hopefully) pro
3b00: 76 69 64 65 20 6d 6f 72 65 20 64 65 74 61 69 6c  vide more detail
3b10: 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a  ed information.*
3b20: 2a 20 61 62 6f 75 74 20 74 68 65 20 63 61 75 73  * about the caus
3b30: 65 20 6f 66 20 61 6e 20 65 72 72 6f 72 2e 0a 2a  e of an error..*
3b40: 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20  *.** The second 
3b50: 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 62 6f  argument is a bo
3b60: 6f 6c 65 61 6e 20 76 61 6c 75 65 20 74 68 61 74  olean value that
3b70: 20 74 75 72 6e 73 20 65 78 74 65 6e 64 65 64 20   turns extended 
3b80: 72 65 73 75 6c 74 0a 2a 2a 20 63 6f 64 65 73 20  result.** codes 
3b90: 6f 6e 20 61 6e 64 20 6f 66 66 2e 20 20 45 78 74  on and off.  Ext
3ba0: 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64  ended result cod
3bb0: 65 73 20 61 72 65 20 6f 66 66 20 62 79 20 64 65  es are off by de
3bc0: 66 61 75 6c 74 20 66 6f 72 0a 2a 2a 20 62 61 63  fault for.** bac
3bd0: 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69  kwards compatibi
3be0: 6c 69 74 79 20 77 69 74 68 20 6f 6c 64 65 72 20  lity with older 
3bf0: 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69  versions of SQLi
3c00: 74 65 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74  te..*/.int sqlit
3c10: 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75  e3_extended_resu
3c20: 6c 74 5f 63 6f 64 65 73 28 73 71 6c 69 74 65 33  lt_codes(sqlite3
3c30: 2a 2c 20 69 6e 74 20 6f 6e 6f 66 66 29 3b 0a 0a  *, int onoff);..
3c40: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
3c50: 4c 61 73 74 20 49 6e 73 65 72 74 20 52 6f 77 69  Last Insert Rowi
3c60: 64 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 65 6e 74  d.**.** Each ent
3c70: 72 79 20 69 6e 20 61 6e 20 53 51 4c 69 74 65 20  ry in an SQLite 
3c80: 74 61 62 6c 65 20 68 61 73 20 61 20 75 6e 69 71  table has a uniq
3c90: 75 65 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64  ue 64-bit signed
3ca0: 20 69 6e 74 65 67 65 72 20 6b 65 79 0a 2a 2a 20   integer key.** 
3cb0: 63 61 6c 6c 65 64 20 74 68 65 20 22 72 6f 77 69  called the "rowi
3cc0: 64 22 2e 20 54 68 65 20 72 6f 77 69 64 20 69 73  d". The rowid is
3cd0: 20 61 6c 77 61 79 73 20 61 76 61 69 6c 61 62 6c   always availabl
3ce0: 65 20 61 73 20 61 6e 20 75 6e 64 65 63 6c 61 72  e as an undeclar
3cf0: 65 64 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 61 6d  ed.** column nam
3d00: 65 64 20 52 4f 57 49 44 2c 20 4f 49 44 2c 20 6f  ed ROWID, OID, o
3d10: 72 20 5f 52 4f 57 49 44 5f 2e 20 20 49 66 20 74  r _ROWID_.  If t
3d20: 68 65 20 74 61 62 6c 65 20 68 61 73 20 61 20 63  he table has a c
3d30: 6f 6c 75 6d 6e 20 6f 66 0a 2a 2a 20 74 79 70 65  olumn of.** type
3d40: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
3d50: 20 4b 45 59 20 74 68 65 6e 20 74 68 61 74 20 63   KEY then that c
3d60: 6f 6c 75 6d 6e 20 69 73 20 61 6e 6f 74 68 65 72  olumn is another
3d70: 20 61 6e 20 61 6c 69 61 73 20 66 6f 72 20 74 68   an alias for th
3d80: 65 0a 2a 2a 20 72 6f 77 69 64 2e 0a 2a 2a 0a 2a  e.** rowid..**.*
3d90: 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72  * This routine r
3da0: 65 74 75 72 6e 73 20 74 68 65 20 72 6f 77 69 64  eturns the rowid
3db0: 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63   of the most rec
3dc0: 65 6e 74 20 49 4e 53 45 52 54 20 69 6e 74 6f 0a  ent INSERT into.
3dd0: 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ** the database 
3de0: 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
3df0: 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 67 69 76  e connection giv
3e00: 65 6e 20 69 6e 20 74 68 65 20 66 69 72 73 74 20  en in the first 
3e10: 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 20 20 49  .** argument.  I
3e20: 66 20 6e 6f 20 69 6e 73 65 72 74 73 20 68 61 76  f no inserts hav
3e30: 65 20 65 76 65 72 20 6f 63 63 75 72 72 65 64 20  e ever occurred 
3e40: 6f 6e 20 74 68 69 73 20 64 61 74 61 62 61 73 65  on this database
3e50: 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20  .** connection, 
3e60: 7a 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64  zero is returned
3e70: 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 4e  ..**.** If an IN
3e80: 53 45 52 54 20 6f 63 63 75 72 73 20 77 69 74 68  SERT occurs with
3e90: 69 6e 20 61 20 74 72 69 67 67 65 72 2c 20 74 68  in a trigger, th
3ea0: 65 6e 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20  en the rowid of 
3eb0: 74 68 65 0a 2a 2a 20 69 6e 73 65 72 74 65 64 20  the.** inserted 
3ec0: 72 6f 77 20 69 73 20 72 65 74 75 72 6e 65 64 20  row is returned 
3ed0: 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  by this routine 
3ee0: 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 74  as long as the t
3ef0: 72 69 67 67 65 72 0a 2a 2a 20 69 73 20 72 75 6e  rigger.** is run
3f00: 6e 69 6e 67 2e 20 20 42 75 74 20 6f 6e 63 65 20  ning.  But once 
3f10: 74 68 65 20 74 72 69 67 67 65 72 20 74 65 72 6d  the trigger term
3f20: 69 6e 61 74 65 73 2c 20 74 68 65 20 76 61 6c 75  inates, the valu
3f30: 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79  e returned.** by
3f40: 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   this routine re
3f50: 76 65 72 74 73 20 74 6f 20 74 68 65 20 6c 61 73  verts to the las
3f60: 74 20 76 61 6c 75 65 20 69 6e 73 65 72 74 65 64  t value inserted
3f70: 20 62 65 66 6f 72 65 20 74 68 65 0a 2a 2a 20 74   before the.** t
3f80: 72 69 67 67 65 72 20 66 69 72 65 64 2e 0a 2a 2f  rigger fired..*/
3f90: 0a 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 71  .sqlite_int64 sq
3fa0: 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72  lite3_last_inser
3fb0: 74 5f 72 6f 77 69 64 28 73 71 6c 69 74 65 33 2a  t_rowid(sqlite3*
3fc0: 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
3fd0: 45 46 3a 20 43 6f 75 6e 74 20 54 68 65 20 4e 75  EF: Count The Nu
3fe0: 6d 62 65 72 20 4f 66 20 52 6f 77 73 20 4d 6f 64  mber Of Rows Mod
3ff0: 69 66 69 65 64 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ified.**.** This
4000: 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
4010: 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
4020: 64 61 74 61 62 61 73 65 20 72 6f 77 73 20 74 68  database rows th
4030: 61 74 20 77 65 72 65 20 63 68 61 6e 67 65 64 0a  at were changed.
4040: 2a 2a 20 28 6f 72 20 69 6e 73 65 72 74 65 64 20  ** (or inserted 
4050: 6f 72 20 64 65 6c 65 74 65 64 29 20 62 79 20 74  or deleted) by t
4060: 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 53  he most recent S
4070: 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 4f  QL statement.  O
4080: 6e 6c 79 0a 2a 2a 20 63 68 61 6e 67 65 73 20 74  nly.** changes t
4090: 68 61 74 20 61 72 65 20 64 69 72 65 63 74 6c 79  hat are directly
40a0: 20 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68   specified by th
40b0: 65 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45  e INSERT, UPDATE
40c0: 2c 20 6f 72 0a 2a 2a 20 44 45 4c 45 54 45 20 73  , or.** DELETE s
40d0: 74 61 74 65 6d 65 6e 74 20 61 72 65 20 63 6f 75  tatement are cou
40e0: 6e 74 65 64 2e 20 20 41 75 78 69 6c 69 61 72 79  nted.  Auxiliary
40f0: 20 63 68 61 6e 67 65 73 20 63 61 75 73 65 64 20   changes caused 
4100: 62 79 0a 2a 2a 20 74 72 69 67 67 65 72 73 20 61  by.** triggers a
4110: 72 65 20 6e 6f 74 20 63 6f 75 6e 74 65 64 2e 20  re not counted. 
4120: 20 55 73 65 20 74 68 65 20 5b 73 71 6c 69 74 65   Use the [sqlite
4130: 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28  3_total_changes(
4140: 29 5d 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 74  )] function.** t
4150: 6f 20 66 69 6e 64 20 74 68 65 20 74 6f 74 61 6c  o find the total
4160: 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 6e 67   number of chang
4170: 65 73 20 69 6e 63 6c 75 64 69 6e 67 20 63 68 61  es including cha
4180: 6e 67 65 73 20 63 61 75 73 65 64 20 62 79 20 74  nges caused by t
4190: 72 69 67 67 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 57  riggers..**.** W
41a0: 69 74 68 69 6e 20 74 68 65 20 62 6f 64 79 20 6f  ithin the body o
41b0: 66 20 61 20 74 72 69 67 67 65 72 2c 20 74 68 65  f a trigger, the
41c0: 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73   sqlite3_changes
41d0: 28 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e  () interface can
41e0: 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64 20 74 6f   be.** called to
41f0: 20 66 69 6e 64 20 74 68 65 20 6e 75 6d 62 65 72   find the number
4200: 20 6f 66 0a 2a 2a 20 63 68 61 6e 67 65 73 20 69   of.** changes i
4210: 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  n the most recen
4220: 74 6c 79 20 63 6f 6d 70 6c 65 74 65 64 20 49 4e  tly completed IN
4230: 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72  SERT, UPDATE, or
4240: 20 44 45 4c 45 54 45 0a 2a 2a 20 73 74 61 74 65   DELETE.** state
4250: 6d 65 6e 74 20 77 69 74 68 69 6e 20 74 68 65 20  ment within the 
4260: 62 6f 64 79 20 6f 66 20 74 68 65 20 74 72 69 67  body of the trig
4270: 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 63  ger..**.** All c
4280: 68 61 6e 67 65 73 20 61 72 65 20 63 6f 75 6e 74  hanges are count
4290: 65 64 2c 20 65 76 65 6e 20 69 66 20 74 68 65 79  ed, even if they
42a0: 20 77 65 72 65 20 6c 61 74 65 72 20 75 6e 64 6f   were later undo
42b0: 6e 65 20 62 79 20 61 0a 2a 2a 20 52 4f 4c 4c 42  ne by a.** ROLLB
42c0: 41 43 4b 20 6f 72 20 41 42 4f 52 54 2e 20 20 45  ACK or ABORT.  E
42d0: 78 63 65 70 74 2c 20 63 68 61 6e 67 65 73 20 61  xcept, changes a
42e0: 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 63  ssociated with c
42f0: 72 65 61 74 69 6e 67 20 61 6e 64 0a 2a 2a 20 64  reating and.** d
4300: 72 6f 70 70 69 6e 67 20 74 61 62 6c 65 73 20 61  ropping tables a
4310: 72 65 20 6e 6f 74 20 63 6f 75 6e 74 65 64 2e 0a  re not counted..
4320: 2a 2a 0a 2a 2a 20 49 66 20 61 20 63 61 6c 6c 62  **.** If a callb
4330: 61 63 6b 20 69 6e 76 6f 6b 65 73 20 5b 73 71 6c  ack invokes [sql
4340: 69 74 65 33 5f 65 78 65 63 28 29 5d 20 6f 72 20  ite3_exec()] or 
4350: 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
4360: 20 72 65 63 75 72 73 69 76 65 6c 79 2c 0a 2a 2a   recursively,.**
4370: 20 74 68 65 6e 20 74 68 65 20 63 68 61 6e 67 65   then the change
4380: 73 20 69 6e 20 74 68 65 20 69 6e 6e 65 72 2c 20  s in the inner, 
4390: 72 65 63 75 72 73 69 76 65 20 63 61 6c 6c 20 61  recursive call a
43a0: 72 65 20 63 6f 75 6e 74 65 64 20 74 6f 67 65 74  re counted toget
43b0: 68 65 72 0a 2a 2a 20 77 69 74 68 20 74 68 65 20  her.** with the 
43c0: 63 68 61 6e 67 65 73 20 69 6e 20 74 68 65 20 6f  changes in the o
43d0: 75 74 65 72 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a  uter call..**.**
43e0: 20 53 51 4c 69 74 65 20 69 6d 70 6c 65 6d 65 6e   SQLite implemen
43f0: 74 73 20 74 68 65 20 63 6f 6d 6d 61 6e 64 20 22  ts the command "
4400: 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c  DELETE FROM tabl
4410: 65 22 20 77 69 74 68 6f 75 74 20 61 20 57 48 45  e" without a WHE
4420: 52 45 20 63 6c 61 75 73 65 0a 2a 2a 20 62 79 20  RE clause.** by 
4430: 64 72 6f 70 70 69 6e 67 20 61 6e 64 20 72 65 63  dropping and rec
4440: 72 65 61 74 69 6e 67 20 74 68 65 20 74 61 62 6c  reating the tabl
4450: 65 2e 20 20 28 54 68 69 73 20 69 73 20 6d 75 63  e.  (This is muc
4460: 68 20 66 61 73 74 65 72 20 74 68 61 6e 20 67 6f  h faster than go
4470: 69 6e 67 0a 2a 2a 20 74 68 72 6f 75 67 68 20 61  ing.** through a
4480: 6e 64 20 64 65 6c 65 74 69 6e 67 20 69 6e 64 69  nd deleting indi
4490: 76 69 64 75 61 6c 20 65 6c 65 6d 65 6e 74 73 20  vidual elements 
44a0: 66 72 6f 6d 20 74 68 65 20 74 61 62 6c 65 2e 29  from the table.)
44b0: 20 20 42 65 63 61 75 73 65 20 6f 66 0a 2a 2a 20    Because of.** 
44c0: 74 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f  this optimizatio
44d0: 6e 2c 20 74 68 65 20 63 68 61 6e 67 65 20 63 6f  n, the change co
44e0: 75 6e 74 20 66 6f 72 20 22 44 45 4c 45 54 45 20  unt for "DELETE 
44f0: 46 52 4f 4d 20 74 61 62 6c 65 22 20 77 69 6c 6c  FROM table" will
4500: 20 62 65 0a 2a 2a 20 7a 65 72 6f 20 72 65 67 61   be.** zero rega
4510: 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 6e 75  rdless of the nu
4520: 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73  mber of elements
4530: 20 74 68 61 74 20 77 65 72 65 20 6f 72 69 67 69   that were origi
4540: 6e 61 6c 6c 79 20 69 6e 20 74 68 65 0a 2a 2a 20  nally in the.** 
4550: 74 61 62 6c 65 2e 20 54 6f 20 67 65 74 20 61 6e  table. To get an
4560: 20 61 63 63 75 72 61 74 65 20 63 6f 75 6e 74 20   accurate count 
4570: 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  of the number of
4580: 20 72 6f 77 73 20 64 65 6c 65 74 65 64 2c 20 75   rows deleted, u
4590: 73 65 0a 2a 2a 20 22 44 45 4c 45 54 45 20 46 52  se.** "DELETE FR
45a0: 4f 4d 20 74 61 62 6c 65 20 57 48 45 52 45 20 31  OM table WHERE 1
45b0: 22 20 69 6e 73 74 65 61 64 2e 0a 2a 2f 0a 53 51  " instead..*/.SQ
45c0: 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
45d0: 69 74 65 33 5f 63 68 61 6e 67 65 73 28 73 71 6c  ite3_changes(sql
45e0: 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  ite3*);../*.** C
45f0: 41 50 49 33 52 45 46 3a 20 54 6f 74 61 6c 20 4e  API3REF: Total N
4600: 75 6d 62 65 72 20 4f 66 20 52 6f 77 73 20 4d 6f  umber Of Rows Mo
4610: 64 69 66 69 65 64 0a 2a 2a 2a 0a 2a 2a 20 54 68  dified.***.** Th
4620: 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  is function retu
4630: 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  rns the number o
4640: 66 20 64 61 74 61 62 61 73 65 20 72 6f 77 73 20  f database rows 
4650: 74 68 61 74 20 68 61 76 65 20 62 65 65 6e 0a 2a  that have been.*
4660: 2a 20 6d 6f 64 69 66 69 65 64 20 62 79 20 49 4e  * modified by IN
4670: 53 45 52 54 2c 20 55 50 44 41 54 45 20 6f 72 20  SERT, UPDATE or 
4680: 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74  DELETE statement
4690: 73 20 73 69 6e 63 65 20 74 68 65 20 64 61 74 61  s since the data
46a0: 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a 2a 20 77  base handle.** w
46b0: 61 73 20 6f 70 65 6e 65 64 2e 20 54 68 69 73 20  as opened. This 
46c0: 69 6e 63 6c 75 64 65 73 20 55 50 44 41 54 45 2c  includes UPDATE,
46d0: 20 49 4e 53 45 52 54 20 61 6e 64 20 44 45 4c 45   INSERT and DELE
46e0: 54 45 20 73 74 61 74 65 6d 65 6e 74 73 20 65 78  TE statements ex
46f0: 65 63 75 74 65 64 0a 2a 2a 20 61 73 20 70 61 72  ecuted.** as par
4700: 74 20 6f 66 20 74 72 69 67 67 65 72 20 70 72 6f  t of trigger pro
4710: 67 72 61 6d 73 2e 20 41 6c 6c 20 63 68 61 6e 67  grams. All chang
4720: 65 73 20 61 72 65 20 63 6f 75 6e 74 65 64 20 61  es are counted a
4730: 73 20 73 6f 6f 6e 20 61 73 20 74 68 65 0a 2a 2a  s soon as the.**
4740: 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20   statement that 
4750: 6d 61 6b 65 73 20 74 68 65 6d 20 69 73 20 63 6f  makes them is co
4760: 6d 70 6c 65 74 65 64 20 28 77 68 65 6e 20 74 68  mpleted (when th
4770: 65 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64  e statement hand
4780: 6c 65 20 69 73 0a 2a 2a 20 70 61 73 73 65 64 20  le is.** passed 
4790: 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65  to [sqlite3_rese
47a0: 74 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 5f  t()] or [sqlite_
47b0: 66 69 6e 61 6c 69 73 65 28 29 5d 29 2e 0a 2a 2a  finalise()])..**
47c0: 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 65  .** See also the
47d0: 20 5b 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65   [sqlite3_change
47e0: 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a  ()] interface..*
47f0: 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6d 70 6c  *.** SQLite impl
4800: 65 6d 65 6e 74 73 20 74 68 65 20 63 6f 6d 6d 61  ements the comma
4810: 6e 64 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20  nd "DELETE FROM 
4820: 74 61 62 6c 65 22 20 77 69 74 68 6f 75 74 20 61  table" without a
4830: 20 57 48 45 52 45 20 63 6c 61 75 73 65 0a 2a 2a   WHERE clause.**
4840: 20 62 79 20 64 72 6f 70 70 69 6e 67 20 61 6e 64   by dropping and
4850: 20 72 65 63 72 65 61 74 69 6e 67 20 74 68 65 20   recreating the 
4860: 74 61 62 6c 65 2e 20 20 28 54 68 69 73 20 69 73  table.  (This is
4870: 20 6d 75 63 68 20 66 61 73 74 65 72 20 74 68 61   much faster tha
4880: 6e 20 67 6f 69 6e 67 0a 2a 2a 20 74 68 72 6f 75  n going.** throu
4890: 67 68 20 61 6e 64 20 64 65 6c 65 74 69 6e 67 20  gh and deleting 
48a0: 69 6e 64 69 76 69 64 75 61 6c 20 65 6c 65 6d 65  individual eleme
48b0: 6e 74 73 20 66 6f 72 6d 20 74 68 65 20 74 61 62  nts form the tab
48c0: 6c 65 2e 29 20 20 42 65 63 61 75 73 65 20 6f 66  le.)  Because of
48d0: 0a 2a 2a 20 74 68 69 73 20 6f 70 74 69 6d 69 7a  .** this optimiz
48e0: 61 74 69 6f 6e 2c 20 74 68 65 20 63 68 61 6e 67  ation, the chang
48f0: 65 20 63 6f 75 6e 74 20 66 6f 72 20 22 44 45 4c  e count for "DEL
4900: 45 54 45 20 46 52 4f 4d 20 74 61 62 6c 65 22 20  ETE FROM table" 
4910: 77 69 6c 6c 20 62 65 0a 2a 2a 20 7a 65 72 6f 20  will be.** zero 
4920: 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68  regardless of th
4930: 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d  e number of elem
4940: 65 6e 74 73 20 74 68 61 74 20 77 65 72 65 20 6f  ents that were o
4950: 72 69 67 69 6e 61 6c 6c 79 20 69 6e 20 74 68 65  riginally in the
4960: 0a 2a 2a 20 74 61 62 6c 65 2e 20 54 6f 20 67 65  .** table. To ge
4970: 74 20 61 6e 20 61 63 63 75 72 61 74 65 20 63 6f  t an accurate co
4980: 75 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65  unt of the numbe
4990: 72 20 6f 66 20 72 6f 77 73 20 64 65 6c 65 74 65  r of rows delete
49a0: 64 2c 20 75 73 65 0a 2a 2a 20 22 44 45 4c 45 54  d, use.** "DELET
49b0: 45 20 46 52 4f 4d 20 74 61 62 6c 65 20 57 48 45  E FROM table WHE
49c0: 52 45 20 31 22 20 69 6e 73 74 65 61 64 2e 0a 2a  RE 1" instead..*
49d0: 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 6f  /.int sqlite3_to
49e0: 74 61 6c 5f 63 68 61 6e 67 65 73 28 73 71 6c 69  tal_changes(sqli
49f0: 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  te3*);../*.** CA
4a00: 50 49 33 52 45 46 3a 20 49 6e 74 65 72 72 75 70  PI3REF: Interrup
4a10: 74 20 41 20 4c 6f 6e 67 2d 52 75 6e 6e 69 6e 67  t A Long-Running
4a20: 20 51 75 65 72 79 0a 2a 2a 0a 2a 2a 20 54 68 69   Query.**.** Thi
4a30: 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 75 73 65  s function cause
4a40: 73 20 61 6e 79 20 70 65 6e 64 69 6e 67 20 64 61  s any pending da
4a50: 74 61 62 61 73 65 20 6f 70 65 72 61 74 69 6f 6e  tabase operation
4a60: 20 74 6f 20 61 62 6f 72 74 20 61 6e 64 0a 2a 2a   to abort and.**
4a70: 20 72 65 74 75 72 6e 20 61 74 20 69 74 73 20 65   return at its e
4a80: 61 72 6c 69 65 73 74 20 6f 70 70 6f 72 74 75 6e  arliest opportun
4a90: 69 74 79 2e 20 20 54 68 69 73 20 72 6f 75 74 69  ity.  This routi
4aa0: 6e 65 20 69 73 20 74 79 70 69 63 61 6c 6c 79 0a  ne is typically.
4ab0: 2a 2a 20 63 61 6c 6c 65 64 20 69 6e 20 72 65 73  ** called in res
4ac0: 70 6f 6e 73 65 20 74 6f 20 61 20 75 73 65 72 20  ponse to a user 
4ad0: 61 63 74 69 6f 6e 20 73 75 63 68 20 61 73 20 70  action such as p
4ae0: 72 65 73 73 69 6e 67 20 22 43 61 6e 63 65 6c 22  ressing "Cancel"
4af0: 0a 2a 2a 20 6f 72 20 43 74 72 6c 2d 43 20 77 68  .** or Ctrl-C wh
4b00: 65 72 65 20 74 68 65 20 75 73 65 72 20 77 61 6e  ere the user wan
4b10: 74 73 20 61 20 6c 6f 6e 67 20 71 75 65 72 79 20  ts a long query 
4b20: 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 68 61 6c  operation to hal
4b30: 74 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79  t.** immediately
4b40: 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 73 61  ..**.** It is sa
4b50: 66 65 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20  fe to call this 
4b60: 72 6f 75 74 69 6e 65 20 66 72 6f 6d 20 61 20 74  routine from a t
4b70: 68 72 65 61 64 20 64 69 66 66 65 72 65 6e 74 20  hread different 
4b80: 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 74 68 72 65  from the.** thre
4b90: 61 64 20 74 68 61 74 20 69 73 20 63 75 72 72 65  ad that is curre
4ba0: 6e 74 6c 79 20 72 75 6e 6e 69 6e 67 20 74 68 65  ntly running the
4bb0: 20 64 61 74 61 62 61 73 65 20 6f 70 65 72 61 74   database operat
4bc0: 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53  ion..**.** The S
4bd0: 51 4c 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 61  QL operation tha
4be0: 74 20 69 73 20 69 6e 74 65 72 72 75 70 74 65 64  t is interrupted
4bf0: 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 5b 53 51   will return [SQ
4c00: 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 5d 2e  LITE_INTERRUPT].
4c10: 0a 2a 2a 20 49 66 20 61 6e 20 69 6e 74 65 72 72  .** If an interr
4c20: 75 70 74 65 64 20 6f 70 65 72 61 74 69 6f 6e 20  upted operation 
4c30: 77 61 73 20 61 6e 20 75 70 64 61 74 65 20 74 68  was an update th
4c40: 61 74 20 69 73 20 69 6e 73 69 64 65 20 61 6e 0a  at is inside an.
4c50: 2a 2a 20 65 78 70 6c 69 63 69 74 20 74 72 61 6e  ** explicit tran
4c60: 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68  saction, then th
4c70: 65 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61 63  e entire transac
4c80: 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 72 6f 6c  tion will be rol
4c90: 6c 65 64 0a 2a 2a 20 62 61 63 6b 20 61 75 74 6f  led.** back auto
4ca0: 6d 61 74 69 63 61 6c 6c 79 2e 0a 2a 2f 0a 53 51  matically..*/.SQ
4cb0: 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
4cc0: 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28  lite3_interrupt(
4cd0: 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a  sqlite3*);../*.*
4ce0: 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 74 65  * CAPI3REF: Dete
4cf0: 72 6d 69 6e 65 20 49 66 20 41 6e 20 53 51 4c 20  rmine If An SQL 
4d00: 53 74 61 74 65 6d 65 6e 74 20 49 73 20 43 6f 6d  Statement Is Com
4d10: 70 6c 65 74 65 0a 2a 2a 0a 2a 2a 20 54 68 65 73  plete.**.** Thes
4d20: 65 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75  e functions retu
4d30: 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 67  rn true if the g
4d40: 69 76 65 6e 20 69 6e 70 75 74 20 73 74 72 69 6e  iven input strin
4d50: 67 20 63 6f 6d 70 72 69 73 65 73 0a 2a 2a 20 6f  g comprises.** o
4d60: 6e 65 20 6f 72 20 6d 6f 72 65 20 63 6f 6d 70 6c  ne or more compl
4d70: 65 74 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  ete SQL statemen
4d80: 74 73 2e 20 46 6f 72 20 74 68 65 20 73 71 6c 69  ts. For the sqli
4d90: 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 29 20 63  te3_complete() c
4da0: 61 6c 6c 2c 0a 2a 2a 20 74 68 65 20 70 61 72 61  all,.** the para
4db0: 6d 65 74 65 72 20 6d 75 73 74 20 62 65 20 61 20  meter must be a 
4dc0: 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 55  nul-terminated U
4dd0: 54 46 2d 38 20 73 74 72 69 6e 67 2e 20 46 6f 72  TF-8 string. For
4de0: 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70  .** sqlite3_comp
4df0: 6c 65 74 65 31 36 28 29 2c 20 61 20 6e 75 6c 2d  lete16(), a nul-
4e00: 74 65 72 6d 69 6e 61 74 65 64 20 6d 61 63 68 69  terminated machi
4e10: 6e 65 20 62 79 74 65 20 6f 72 64 65 72 20 55 54  ne byte order UT
4e20: 46 2d 31 36 20 73 74 72 69 6e 67 0a 2a 2a 20 69  F-16 string.** i
4e30: 73 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a  s required..**.*
4e40: 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
4e50: 20 61 72 65 20 75 73 65 66 75 6c 20 66 6f 72 20   are useful for 
4e60: 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20 69 6e 70  command-line inp
4e70: 75 74 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  ut to determine 
4e80: 69 66 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e  if the.** curren
4e90: 74 6c 79 20 65 6e 74 65 72 65 64 20 74 65 78 74  tly entered text
4ea0: 20 66 6f 72 6d 73 20 6f 6e 65 20 6f 72 20 6d 6f   forms one or mo
4eb0: 72 65 20 63 6f 6d 70 6c 65 74 65 20 53 51 4c 20  re complete SQL 
4ec0: 73 74 61 74 65 6d 65 6e 74 73 20 6f 72 0a 2a 2a  statements or.**
4ed0: 20 69 66 20 61 64 64 69 74 69 6f 6e 61 6c 20 69   if additional i
4ee0: 6e 70 75 74 20 69 73 20 6e 65 65 64 65 64 20 62  nput is needed b
4ef0: 65 66 6f 72 65 20 73 65 6e 64 69 6e 67 20 74 68  efore sending th
4f00: 65 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 74  e statements int
4f10: 6f 0a 2a 2a 20 53 51 4c 69 74 65 20 66 6f 72 20  o.** SQLite for 
4f20: 70 61 72 73 69 6e 67 2e 20 54 68 65 20 61 6c 67  parsing. The alg
4f30: 6f 72 69 74 68 6d 20 69 73 20 73 69 6d 70 6c 65  orithm is simple
4f40: 2e 20 20 49 66 20 74 68 65 20 0a 2a 2a 20 6c 61  .  If the .** la
4f50: 73 74 20 74 6f 6b 65 6e 20 6f 74 68 65 72 20 74  st token other t
4f60: 68 61 6e 20 73 70 61 63 65 73 20 61 6e 64 20 63  han spaces and c
4f70: 6f 6d 6d 65 6e 74 73 20 69 73 20 61 20 73 65 6d  omments is a sem
4f80: 69 63 6f 6c 6f 6e 2c 20 74 68 65 6e 20 72 65 74  icolon, then ret
4f90: 75 72 6e 20 0a 2a 2a 20 74 72 75 65 2e 20 20 41  urn .** true.  A
4fa0: 63 74 75 61 6c 6c 79 2c 20 74 68 65 20 61 6c 67  ctually, the alg
4fb0: 6f 72 69 74 68 6d 20 69 73 20 61 20 6c 69 74 74  orithm is a litt
4fc0: 6c 65 20 6d 6f 72 65 20 63 6f 6d 70 6c 69 63 61  le more complica
4fd0: 74 65 64 20 74 68 61 6e 20 74 68 61 74 0a 2a 2a  ted than that.**
4fe0: 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 64 65 61   in order to dea
4ff0: 6c 20 77 69 74 68 20 74 72 69 67 67 65 72 73 2c  l with triggers,
5000: 20 62 75 74 20 74 68 65 20 62 61 73 69 63 20 69   but the basic i
5010: 64 65 61 20 69 73 20 74 68 65 20 73 61 6d 65 3a  dea is the same:
5020: 20 20 74 68 65 0a 2a 2a 20 73 74 61 74 65 6d 65    the.** stateme
5030: 6e 74 20 69 73 20 6e 6f 74 20 63 6f 6d 70 6c 65  nt is not comple
5040: 74 65 20 75 6e 6c 65 73 73 20 69 74 20 65 6e 64  te unless it end
5050: 73 20 69 6e 20 61 20 73 65 6d 69 63 6f 6c 6f 6e  s in a semicolon
5060: 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
5070: 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70  int sqlite3_comp
5080: 6c 65 74 65 28 63 6f 6e 73 74 20 63 68 61 72 20  lete(const char 
5090: 2a 73 71 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50  *sql);.SQLITE_AP
50a0: 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f  I int sqlite3_co
50b0: 6d 70 6c 65 74 65 31 36 28 63 6f 6e 73 74 20 76  mplete16(const v
50c0: 6f 69 64 20 2a 73 71 6c 29 3b 0a 0a 2f 2a 0a 2a  oid *sql);../*.*
50d0: 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 67 69  * CAPI3REF: Regi
50e0: 73 74 65 72 20 41 20 43 61 6c 6c 62 61 63 6b 20  ster A Callback 
50f0: 54 6f 20 48 61 6e 64 6c 65 20 53 51 4c 49 54 45  To Handle SQLITE
5100: 5f 42 55 53 59 20 45 72 72 6f 72 73 0a 2a 2a 0a  _BUSY Errors.**.
5110: 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
5120: 69 64 65 6e 74 69 66 69 65 73 20 61 20 63 61 6c  identifies a cal
5130: 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 74  lback function t
5140: 68 61 74 20 6d 69 67 68 74 20 62 65 20 69 6e 76  hat might be inv
5150: 6f 6b 65 64 0a 2a 2a 20 77 68 65 6e 65 76 65 72  oked.** whenever
5160: 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d   an attempt is m
5170: 61 64 65 20 74 6f 20 6f 70 65 6e 20 61 20 64 61  ade to open a da
5180: 74 61 62 61 73 65 20 74 61 62 6c 65 20 0a 2a 2a  tabase table .**
5190: 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20 74 68   that another th
51a0: 72 65 61 64 20 6f 72 20 70 72 6f 63 65 73 73 20  read or process 
51b0: 68 61 73 20 6c 6f 63 6b 65 64 2e 0a 2a 2a 20 49  has locked..** I
51c0: 66 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62  f the busy callb
51d0: 61 63 6b 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65  ack is NULL, the
51e0: 6e 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 0a  n [SQLITE_BUSY].
51f0: 2a 2a 20 28 6f 72 20 73 6f 6d 65 74 69 6d 65 73  ** (or sometimes
5200: 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42   [SQLITE_IOERR_B
5210: 4c 4f 43 4b 45 44 5d 29 0a 2a 2a 20 69 73 20 72  LOCKED]).** is r
5220: 65 74 75 72 6e 65 64 20 69 6d 6d 65 64 69 61 74  eturned immediat
5230: 65 6c 79 20 75 70 6f 6e 20 65 6e 63 6f 75 6e 74  ely upon encount
5240: 65 72 69 6e 67 20 74 68 65 20 6c 6f 63 6b 2e 0a  ering the lock..
5250: 2a 2a 20 49 66 20 74 68 65 20 62 75 73 79 20 63  ** If the busy c
5260: 61 6c 6c 62 61 63 6b 20 69 73 20 6e 6f 74 20 4e  allback is not N
5270: 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a  ULL, then the.**
5280: 20 63 61 6c 6c 62 61 63 6b 20 77 69 6c 6c 20 62   callback will b
5290: 65 20 69 6e 76 6f 6b 65 64 20 77 69 74 68 20 74  e invoked with t
52a0: 77 6f 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 54  wo arguments.  T
52b0: 68 65 0a 2a 2a 20 66 69 72 73 74 20 61 72 67 75  he.** first argu
52c0: 6d 65 6e 74 20 74 6f 20 74 68 65 20 68 61 6e 64  ment to the hand
52d0: 6c 65 72 20 69 73 20 61 20 63 6f 70 79 20 6f 66  ler is a copy of
52e0: 20 74 68 65 20 76 6f 69 64 2a 20 70 6f 69 6e 74   the void* point
52f0: 65 72 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 74  er which.** is t
5300: 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e  he third argumen
5310: 74 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e  t to this routin
5320: 65 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61  e.  The second a
5330: 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 74 68  rgument to.** th
5340: 65 20 68 61 6e 64 6c 65 72 20 69 73 20 74 68 65  e handler is the
5350: 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73   number of times
5360: 20 74 68 61 74 20 74 68 65 20 62 75 73 79 20 68   that the busy h
5370: 61 6e 64 6c 65 72 20 68 61 73 0a 2a 2a 20 62 65  andler has.** be
5380: 65 6e 20 69 6e 76 6f 6b 65 64 20 66 6f 72 20 74  en invoked for t
5390: 68 69 73 20 6c 6f 63 6b 69 6e 67 20 65 76 65 6e  his locking even
53a0: 74 2e 20 49 66 20 74 68 65 0a 2a 2a 20 62 75 73  t. If the.** bus
53b0: 79 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72  y callback retur
53c0: 6e 73 20 30 2c 20 74 68 65 6e 20 6e 6f 20 61 64  ns 0, then no ad
53d0: 64 69 74 69 6f 6e 61 6c 20 61 74 74 65 6d 70 74  ditional attempt
53e0: 73 20 61 72 65 20 6d 61 64 65 20 74 6f 0a 2a 2a  s are made to.**
53f0: 20 61 63 63 65 73 73 20 74 68 65 20 64 61 74 61   access the data
5400: 62 61 73 65 20 61 6e 64 20 5b 53 51 4c 49 54 45  base and [SQLITE
5410: 5f 42 55 53 59 5d 20 6f 72 20 5b 53 51 4c 49 54  _BUSY] or [SQLIT
5420: 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d  E_IOERR_BLOCKED]
5430: 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
5440: 20 49 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b   If the callback
5450: 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72   returns non-zer
5460: 6f 2c 20 74 68 65 6e 20 61 6e 6f 74 68 65 72 20  o, then another 
5470: 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20  attempt is made 
5480: 74 6f 20 6f 70 65 6e 20 74 68 65 0a 2a 2a 20 64  to open the.** d
5490: 61 74 61 62 61 73 65 20 66 6f 72 20 72 65 61 64  atabase for read
54a0: 69 6e 67 20 61 6e 64 20 74 68 65 20 63 79 63 6c  ing and the cycl
54b0: 65 20 72 65 70 65 61 74 73 2e 0a 2a 2a 0a 2a 2a  e repeats..**.**
54c0: 20 54 68 65 20 70 72 65 73 65 6e 63 65 20 6f 66   The presence of
54d0: 20 61 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20   a busy handler 
54e0: 64 6f 65 73 20 6e 6f 74 20 67 75 61 72 61 6e 74  does not guarant
54f0: 65 65 20 74 68 61 74 0a 2a 2a 20 69 74 20 77 69  ee that.** it wi
5500: 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68  ll be invoked wh
5510: 65 6e 20 74 68 65 72 65 20 69 73 20 6c 6f 63 6b  en there is lock
5520: 20 63 6f 6e 74 65 6e 74 69 6f 6e 2e 0a 2a 2a 20   contention..** 
5530: 49 66 20 53 51 4c 69 74 65 20 64 65 74 65 72 6d  If SQLite determ
5540: 69 6e 65 73 20 74 68 61 74 20 69 6e 76 6f 6b 69  ines that invoki
5550: 6e 67 20 74 68 65 20 62 75 73 79 20 68 61 6e 64  ng the busy hand
5560: 6c 65 72 20 63 6f 75 6c 64 20 72 65 73 75 6c 74  ler could result
5570: 20 69 6e 0a 2a 2a 20 61 20 64 65 61 64 6c 6f 63   in.** a deadloc
5580: 6b 2c 20 69 74 20 77 69 6c 6c 20 72 65 74 75 72  k, it will retur
5590: 6e 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20  n [SQLITE_BUSY] 
55a0: 69 6e 73 74 65 61 64 2e 0a 2a 2a 20 43 6f 6e 73  instead..** Cons
55b0: 69 64 65 72 20 61 20 73 63 65 6e 61 72 69 6f 20  ider a scenario 
55c0: 77 68 65 72 65 20 6f 6e 65 20 70 72 6f 63 65 73  where one proces
55d0: 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 72  s is holding a r
55e0: 65 61 64 20 6c 6f 63 6b 20 74 68 61 74 0a 2a 2a  ead lock that.**
55f0: 20 69 74 20 69 73 20 74 72 79 69 6e 67 20 74 6f   it is trying to
5600: 20 70 72 6f 6d 6f 74 65 20 74 6f 20 61 20 72 65   promote to a re
5610: 73 65 72 76 65 64 20 6c 6f 63 6b 20 61 6e 64 0a  served lock and.
5620: 2a 2a 20 61 20 73 65 63 6f 6e 64 20 70 72 6f 63  ** a second proc
5630: 65 73 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 61  ess is holding a
5640: 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 74   reserved lock t
5650: 68 61 74 20 69 74 20 69 73 20 74 72 79 69 6e 67  hat it is trying
5660: 0a 2a 2a 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74  .** to promote t
5670: 6f 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c  o an exclusive l
5680: 6f 63 6b 2e 20 20 54 68 65 20 66 69 72 73 74 20  ock.  The first 
5690: 70 72 6f 63 65 73 73 20 63 61 6e 6e 6f 74 20 70  process cannot p
56a0: 72 6f 63 65 65 64 0a 2a 2a 20 62 65 63 61 75 73  roceed.** becaus
56b0: 65 20 69 74 20 69 73 20 62 6c 6f 63 6b 65 64 20  e it is blocked 
56c0: 62 79 20 74 68 65 20 73 65 63 6f 6e 64 20 61 6e  by the second an
56d0: 64 20 74 68 65 20 73 65 63 6f 6e 64 20 70 72 6f  d the second pro
56e0: 63 65 73 73 20 63 61 6e 6e 6f 74 0a 2a 2a 20 70  cess cannot.** p
56f0: 72 6f 63 65 65 64 20 62 65 63 61 75 73 65 20 69  roceed because i
5700: 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 20  t is blocked by 
5710: 74 68 65 20 66 69 72 73 74 2e 20 20 49 66 20 62  the first.  If b
5720: 6f 74 68 20 70 72 6f 63 65 73 73 65 73 0a 2a 2a  oth processes.**
5730: 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79   invoke the busy
5740: 20 68 61 6e 64 6c 65 72 73 2c 20 6e 65 69 74 68   handlers, neith
5750: 65 72 20 77 69 6c 6c 20 6d 61 6b 65 20 61 6e 79  er will make any
5760: 20 70 72 6f 67 72 65 73 73 2e 20 20 54 68 65 72   progress.  Ther
5770: 65 66 6f 72 65 2c 0a 2a 2a 20 53 51 4c 69 74 65  efore,.** SQLite
5780: 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45   returns [SQLITE
5790: 5f 42 55 53 59 5d 20 66 6f 72 20 74 68 65 20 66  _BUSY] for the f
57a0: 69 72 73 74 20 70 72 6f 63 65 73 73 2c 20 68 6f  irst process, ho
57b0: 70 69 6e 67 20 74 68 61 74 20 74 68 69 73 0a 2a  ping that this.*
57c0: 2a 20 77 69 6c 6c 20 69 6e 64 75 63 65 20 74 68  * will induce th
57d0: 65 20 66 69 72 73 74 20 70 72 6f 63 65 73 73 20  e first process 
57e0: 74 6f 20 72 65 6c 65 61 73 65 20 69 74 73 20 72  to release its r
57f0: 65 61 64 20 6c 6f 63 6b 20 61 6e 64 20 61 6c 6c  ead lock and all
5800: 6f 77 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64  ow.** the second
5810: 20 70 72 6f 63 65 73 73 20 74 6f 20 70 72 6f 63   process to proc
5820: 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64  eed..**.** The d
5830: 65 66 61 75 6c 74 20 62 75 73 79 20 63 61 6c 6c  efault busy call
5840: 62 61 63 6b 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a  back is NULL..**
5850: 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f  .** The [SQLITE_
5860: 42 55 53 59 5d 20 65 72 72 6f 72 20 69 73 20 63  BUSY] error is c
5870: 6f 6e 76 65 72 74 65 64 20 74 6f 20 5b 53 51 4c  onverted to [SQL
5880: 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45  ITE_IOERR_BLOCKE
5890: 44 5d 20 77 68 65 6e 0a 2a 2a 20 53 51 4c 69 74  D] when.** SQLit
58a0: 65 20 69 73 20 69 6e 20 74 68 65 20 6d 69 64 64  e is in the midd
58b0: 6c 65 20 6f 66 20 61 20 6c 61 72 67 65 20 74 72  le of a large tr
58c0: 61 6e 73 61 63 74 69 6f 6e 20 77 68 65 72 65 20  ansaction where 
58d0: 61 6c 6c 20 74 68 65 0a 2a 2a 20 63 68 61 6e 67  all the.** chang
58e0: 65 73 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 20  es will not fit 
58f0: 69 6e 74 6f 20 74 68 65 20 69 6e 2d 6d 65 6d 6f  into the in-memo
5900: 72 79 20 63 61 63 68 65 2e 20 20 53 51 4c 69 74  ry cache.  SQLit
5910: 65 20 77 69 6c 6c 0a 2a 2a 20 61 6c 72 65 61 64  e will.** alread
5920: 79 20 68 6f 6c 64 20 61 20 52 45 53 45 52 56 45  y hold a RESERVE
5930: 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61  D lock on the da
5940: 74 61 62 61 73 65 20 66 69 6c 65 2c 20 62 75 74  tabase file, but
5950: 20 69 74 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20   it needs.** to 
5960: 70 72 6f 6d 6f 74 65 20 74 68 69 73 20 6c 6f 63  promote this loc
5970: 6b 20 74 6f 20 45 58 43 4c 55 53 49 56 45 20 73  k to EXCLUSIVE s
5980: 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 73 70  o that it can sp
5990: 69 6c 6c 20 63 61 63 68 65 0a 2a 2a 20 70 61 67  ill cache.** pag
59a0: 65 73 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61  es into the data
59b0: 62 61 73 65 20 66 69 6c 65 20 77 69 74 68 6f 75  base file withou
59c0: 74 20 68 61 72 6d 20 74 6f 20 63 6f 6e 63 75 72  t harm to concur
59d0: 72 65 6e 74 0a 2a 2a 20 72 65 61 64 65 72 73 2e  rent.** readers.
59e0: 20 20 49 66 20 69 74 20 69 73 20 75 6e 61 62 6c    If it is unabl
59f0: 65 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 68 65  e to promote the
5a00: 20 6c 6f 63 6b 2c 20 74 68 65 6e 20 74 68 65 20   lock, then the 
5a10: 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 63 61 63  in-memory.** cac
5a20: 68 65 20 77 69 6c 6c 20 62 65 20 6c 65 66 74 20  he will be left 
5a30: 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65  in an inconsiste
5a40: 6e 74 20 73 74 61 74 65 20 61 6e 64 20 73 6f 20  nt state and so 
5a50: 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64  the error.** cod
5a60: 65 20 69 73 20 70 72 6f 6d 6f 74 65 64 20 66 72  e is promoted fr
5a70: 6f 6d 20 74 68 65 20 72 65 6c 61 74 69 76 65 6c  om the relativel
5a80: 79 20 62 65 6e 69 67 6e 20 5b 53 51 4c 49 54 45  y benign [SQLITE
5a90: 5f 42 55 53 59 5d 20 74 6f 0a 2a 2a 20 74 68 65  _BUSY] to.** the
5aa0: 20 6d 6f 72 65 20 73 65 76 65 72 65 20 5b 53 51   more severe [SQ
5ab0: 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b  LITE_IOERR_BLOCK
5ac0: 45 44 5d 2e 20 20 54 68 69 73 20 65 72 72 6f 72  ED].  This error
5ad0: 20 63 6f 64 65 20 70 72 6f 6d 6f 74 69 6f 6e 0a   code promotion.
5ae0: 2a 2a 20 66 6f 72 63 65 73 20 61 6e 20 61 75 74  ** forces an aut
5af0: 6f 6d 61 74 69 63 20 72 6f 6c 6c 62 61 63 6b 20  omatic rollback 
5b00: 6f 66 20 74 68 65 20 63 68 61 6e 67 65 73 2e 20  of the changes. 
5b10: 53 65 65 20 74 68 65 0a 2a 2a 20 3c 61 20 68 72  See the.** <a hr
5b20: 65 66 3d 22 68 74 74 70 3a 2f 2f 77 77 77 2e 73  ef="http://www.s
5b30: 71 6c 69 74 65 2e 6f 72 67 2f 63 76 73 74 72 61  qlite.org/cvstra
5b40: 63 2f 77 69 6b 69 3f 70 3d 43 6f 72 72 75 70 74  c/wiki?p=Corrupt
5b50: 69 6f 6e 46 6f 6c 6c 6f 77 69 6e 67 42 75 73 79  ionFollowingBusy
5b60: 45 72 72 6f 72 22 3e 0a 2a 2a 20 43 6f 72 72 75  Error">.** Corru
5b70: 70 74 69 6f 6e 46 6f 6c 6c 6f 77 69 6e 67 42 75  ptionFollowingBu
5b80: 73 79 45 72 72 6f 72 3c 2f 61 3e 20 77 69 6b 69  syError</a> wiki
5b90: 20 70 61 67 65 20 66 6f 72 20 61 20 64 69 73 63   page for a disc
5ba0: 75 73 73 69 6f 6e 20 6f 66 20 77 68 79 0a 2a 2a  ussion of why.**
5bb0: 20 74 68 69 73 20 69 73 20 69 6d 70 6f 72 74 61   this is importa
5bc0: 6e 74 2e 0a 2a 2a 09 0a 2a 2a 20 53 71 6c 69 74  nt..**..** Sqlit
5bd0: 65 20 69 73 20 72 65 2d 65 6e 74 72 61 6e 74 2c  e is re-entrant,
5be0: 20 73 6f 20 74 68 65 20 62 75 73 79 20 68 61 6e   so the busy han
5bf0: 64 6c 65 72 20 6d 61 79 20 73 74 61 72 74 20 61  dler may start a
5c00: 20 6e 65 77 20 71 75 65 72 79 2e 20 0a 2a 2a 20   new query. .** 
5c10: 28 49 74 20 69 73 20 6e 6f 74 20 63 6c 65 61 72  (It is not clear
5c20: 20 77 68 79 20 61 6e 79 6f 6e 65 20 77 6f 75 6c   why anyone woul
5c30: 64 20 65 76 65 72 79 20 77 61 6e 74 20 74 6f 20  d every want to 
5c40: 64 6f 20 74 68 69 73 2c 20 62 75 74 20 69 74 0a  do this, but it.
5c50: 2a 2a 20 69 73 20 61 6c 6c 6f 77 65 64 2c 20 69  ** is allowed, i
5c60: 6e 20 74 68 65 6f 72 79 2e 29 20 20 42 75 74 20  n theory.)  But 
5c70: 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72  the busy handler
5c80: 20 6d 61 79 20 6e 6f 74 20 63 6c 6f 73 65 20 74   may not close t
5c90: 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20  he.** database. 
5ca0: 20 43 6c 6f 73 69 6e 67 20 74 68 65 20 64 61 74   Closing the dat
5cb0: 61 62 61 73 65 20 66 72 6f 6d 20 61 20 62 75 73  abase from a bus
5cc0: 79 20 68 61 6e 64 6c 65 72 20 77 69 6c 6c 20 64  y handler will d
5cd0: 65 6c 65 74 65 20 0a 2a 2a 20 64 61 74 61 20 73  elete .** data s
5ce0: 74 72 75 63 74 75 72 65 73 20 6f 75 74 20 66 72  tructures out fr
5cf0: 6f 6d 20 75 6e 64 65 72 20 74 68 65 20 65 78 65  om under the exe
5d00: 63 75 74 69 6e 67 20 71 75 65 72 79 20 61 6e 64  cuting query and
5d10: 20 77 69 6c 6c 20 0a 2a 2a 20 70 72 6f 62 61 62   will .** probab
5d20: 6c 79 20 72 65 73 75 6c 74 20 69 6e 20 61 20 73  ly result in a s
5d30: 65 67 6d 65 6e 74 61 74 69 6f 6e 20 66 61 75 6c  egmentation faul
5d40: 74 20 6f 72 20 6f 74 68 65 72 20 72 75 6e 74 69  t or other runti
5d50: 6d 65 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20  me error..**.** 
5d60: 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 20 62  There can only b
5d70: 65 20 61 20 73 69 6e 67 6c 65 20 62 75 73 79 20  e a single busy 
5d80: 68 61 6e 64 6c 65 72 20 64 65 66 69 6e 65 64 20  handler defined 
5d90: 66 6f 72 20 65 61 63 68 20 64 61 74 61 62 61 73  for each databas
5da0: 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  e.** connection.
5db0: 20 20 53 65 74 74 69 6e 67 20 61 20 6e 65 77 20    Setting a new 
5dc0: 62 75 73 79 20 68 61 6e 64 6c 65 72 20 63 6c 65  busy handler cle
5dd0: 61 72 73 20 61 6e 79 20 70 72 65 76 69 6f 75 73  ars any previous
5de0: 20 6f 6e 65 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68   one..** Note th
5df0: 61 74 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69  at calling [sqli
5e00: 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74  te3_busy_timeout
5e10: 28 29 5d 20 77 69 6c 6c 20 61 6c 73 6f 20 73 65  ()] will also se
5e20: 74 20 6f 72 20 63 6c 65 61 72 0a 2a 2a 20 74 68  t or clear.** th
5e30: 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 2e 0a  e busy handler..
5e40: 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  */.int sqlite3_b
5e50: 75 73 79 5f 68 61 6e 64 6c 65 72 28 73 71 6c 69  usy_handler(sqli
5e60: 74 65 33 2a 2c 20 69 6e 74 28 2a 29 28 76 6f 69  te3*, int(*)(voi
5e70: 64 2a 2c 69 6e 74 29 2c 20 76 6f 69 64 2a 29 3b  d*,int), void*);
5e80: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
5e90: 3a 20 53 65 74 20 41 20 42 75 73 79 20 54 69 6d  : Set A Busy Tim
5ea0: 65 6f 75 74 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  eout.**.** This 
5eb0: 72 6f 75 74 69 6e 65 20 73 65 74 73 20 61 20 62  routine sets a b
5ec0: 75 73 79 20 68 61 6e 64 6c 65 72 20 74 68 61 74  usy handler that
5ed0: 20 73 6c 65 65 70 73 20 66 6f 72 20 61 20 77 68   sleeps for a wh
5ee0: 69 6c 65 20 77 68 65 6e 20 61 0a 2a 2a 20 74 61  ile when a.** ta
5ef0: 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64 2e 20 20  ble is locked.  
5f00: 54 68 65 20 68 61 6e 64 6c 65 72 20 77 69 6c 6c  The handler will
5f10: 20 73 6c 65 65 70 20 6d 75 6c 74 69 70 6c 65 20   sleep multiple 
5f20: 74 69 6d 65 73 20 75 6e 74 69 6c 20 0a 2a 2a 20  times until .** 
5f30: 61 74 20 6c 65 61 73 74 20 22 6d 73 22 20 6d 69  at least "ms" mi
5f40: 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c  lliseconds of sl
5f50: 65 65 70 69 6e 67 20 68 61 76 65 20 62 65 65 6e  eeping have been
5f60: 20 64 6f 6e 65 2e 20 20 41 66 74 65 72 0a 2a 2a   done.  After.**
5f70: 20 22 6d 73 22 20 6d 69 6c 6c 69 73 65 63 6f 6e   "ms" millisecon
5f80: 64 73 20 6f 66 20 73 6c 65 65 70 69 6e 67 2c 20  ds of sleeping, 
5f90: 74 68 65 20 68 61 6e 64 6c 65 72 20 72 65 74 75  the handler retu
5fa0: 72 6e 73 20 30 20 77 68 69 63 68 0a 2a 2a 20 63  rns 0 which.** c
5fb0: 61 75 73 65 73 20 5b 73 71 6c 69 74 65 33 5f 73  auses [sqlite3_s
5fc0: 74 65 70 28 29 5d 20 74 6f 20 72 65 74 75 72 6e  tep()] to return
5fd0: 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f   [SQLITE_BUSY] o
5fe0: 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  r [SQLITE_IOERR_
5ff0: 42 4c 4f 43 4b 45 44 5d 2e 0a 2a 2a 0a 2a 2a 20  BLOCKED]..**.** 
6000: 43 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75  Calling this rou
6010: 74 69 6e 65 20 77 69 74 68 20 61 6e 20 61 72 67  tine with an arg
6020: 75 6d 65 6e 74 20 6c 65 73 73 20 74 68 61 6e 20  ument less than 
6030: 6f 72 20 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f  or equal to zero
6040: 0a 2a 2a 20 74 75 72 6e 73 20 6f 66 66 20 61 6c  .** turns off al
6050: 6c 20 62 75 73 79 20 68 61 6e 64 6c 65 72 73 2e  l busy handlers.
6060: 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 63 61 6e  .**.** There can
6070: 20 6f 6e 6c 79 20 62 65 20 61 20 73 69 6e 67 6c   only be a singl
6080: 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 66  e busy handler f
6090: 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  or a particular 
60a0: 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e  database.** conn
60b0: 65 63 74 69 6f 6e 2e 20 20 49 66 20 61 6e 6f 74  ection.  If anot
60c0: 68 65 72 20 62 75 73 79 20 68 61 6e 64 6c 65 72  her busy handler
60d0: 20 77 61 73 20 64 65 66 69 6e 65 64 20 20 0a 2a   was defined  .*
60e0: 2a 20 28 75 73 69 6e 67 20 5b 73 71 6c 69 74 65  * (using [sqlite
60f0: 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 29  3_busy_handler()
6100: 5d 29 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c  ]) prior to call
6110: 69 6e 67 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74  ing.** this rout
6120: 69 6e 65 2c 20 74 68 61 74 20 6f 74 68 65 72 20  ine, that other 
6130: 62 75 73 79 20 68 61 6e 64 6c 65 72 20 69 73 20  busy handler is 
6140: 63 6c 65 61 72 65 64 2e 0a 2a 2f 0a 69 6e 74 20  cleared..*/.int 
6150: 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d  sqlite3_busy_tim
6160: 65 6f 75 74 28 73 71 6c 69 74 65 33 2a 2c 20 69  eout(sqlite3*, i
6170: 6e 74 20 6d 73 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  nt ms);../*.** C
6180: 41 50 49 33 52 45 46 3a 20 43 6f 6e 76 65 6e 69  API3REF: Conveni
6190: 65 6e 63 65 20 52 6f 75 74 69 6e 65 73 20 46 6f  ence Routines Fo
61a0: 72 20 52 75 6e 6e 69 6e 67 20 51 75 65 72 69 65  r Running Querie
61b0: 73 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6e 65 78  s.**.** This nex
61c0: 74 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 63  t routine is a c
61d0: 6f 6e 76 65 6e 69 65 6e 63 65 20 77 72 61 70 70  onvenience wrapp
61e0: 65 72 20 61 72 6f 75 6e 64 20 5b 73 71 6c 69 74  er around [sqlit
61f0: 65 33 5f 65 78 65 63 28 29 5d 2e 0a 2a 2a 20 49  e3_exec()]..** I
6200: 6e 73 74 65 61 64 20 6f 66 20 69 6e 76 6f 6b 69  nstead of invoki
6210: 6e 67 20 61 20 75 73 65 72 2d 73 75 70 70 6c 69  ng a user-suppli
6220: 65 64 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20  ed callback for 
6230: 65 61 63 68 20 72 6f 77 20 6f 66 20 74 68 65 0a  each row of the.
6240: 2a 2a 20 72 65 73 75 6c 74 2c 20 74 68 69 73 20  ** result, this 
6250: 72 6f 75 74 69 6e 65 20 72 65 6d 65 6d 62 65 72  routine remember
6260: 73 20 65 61 63 68 20 72 6f 77 20 6f 66 20 74 68  s each row of th
6270: 65 20 72 65 73 75 6c 74 20 69 6e 20 6d 65 6d 6f  e result in memo
6280: 72 79 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 20 66  ry.** obtained f
6290: 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c  rom [sqlite3_mal
62a0: 6c 6f 63 28 29 5d 2c 20 74 68 65 6e 20 72 65 74  loc()], then ret
62b0: 75 72 6e 73 20 61 6c 6c 20 6f 66 20 74 68 65 20  urns all of the 
62c0: 72 65 73 75 6c 74 20 61 66 74 65 72 20 74 68 65  result after the
62d0: 0a 2a 2a 20 71 75 65 72 79 20 68 61 73 20 66 69  .** query has fi
62e0: 6e 69 73 68 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 41  nished. .**.** A
62f0: 73 20 61 6e 20 65 78 61 6d 70 6c 65 2c 20 73 75  s an example, su
6300: 70 70 6f 73 65 20 74 68 65 20 71 75 65 72 79 20  ppose the query 
6310: 72 65 73 75 6c 74 20 77 68 65 72 65 20 74 68 69  result where thi
6320: 73 20 74 61 62 6c 65 3a 0a 2a 2a 0a 2a 2a 20 3c  s table:.**.** <
6330: 70 72 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20 4e  pre>.**        N
6340: 61 6d 65 20 20 20 20 20 20 20 20 7c 20 41 67 65  ame        | Age
6350: 0a 2a 2a 20 20 20 20 20 20 20 20 2d 2d 2d 2d 2d  .**        -----
6360: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6370: 2d 2d 0a 2a 2a 20 20 20 20 20 20 20 20 41 6c 69  --.**        Ali
6380: 63 65 20 20 20 20 20 20 20 7c 20 34 33 0a 2a 2a  ce       | 43.**
6390: 20 20 20 20 20 20 20 20 42 6f 62 20 20 20 20 20          Bob     
63a0: 20 20 20 20 7c 20 32 38 0a 2a 2a 20 20 20 20 20      | 28.**     
63b0: 20 20 20 43 69 6e 64 79 20 20 20 20 20 20 20 7c     Cindy       |
63c0: 20 32 31 0a 2a 2a 20 3c 2f 70 72 65 3e 0a 2a 2a   21.** </pre>.**
63d0: 0a 2a 2a 20 49 66 20 74 68 65 20 33 72 64 20 61  .** If the 3rd a
63e0: 72 67 75 6d 65 6e 74 20 77 65 72 65 20 26 61 7a  rgument were &az
63f0: 52 65 73 75 6c 74 20 74 68 65 6e 20 61 66 74 65  Result then afte
6400: 72 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72  r the function r
6410: 65 74 75 72 6e 73 0a 2a 2a 20 61 7a 52 65 73 75  eturns.** azResu
6420: 6c 74 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20  lt will contain 
6430: 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 64 61  the following da
6440: 74 61 3a 0a 2a 2a 0a 2a 2a 20 3c 70 72 65 3e 0a  ta:.**.** <pre>.
6450: 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75  **        azResu
6460: 6c 74 5b 30 5d 20 3d 20 22 4e 61 6d 65 22 3b 0a  lt[0] = "Name";.
6470: 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75  **        azResu
6480: 6c 74 5b 31 5d 20 3d 20 22 41 67 65 22 3b 0a 2a  lt[1] = "Age";.*
6490: 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c  *        azResul
64a0: 74 5b 32 5d 20 3d 20 22 41 6c 69 63 65 22 3b 0a  t[2] = "Alice";.
64b0: 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75  **        azResu
64c0: 6c 74 5b 33 5d 20 3d 20 22 34 33 22 3b 0a 2a 2a  lt[3] = "43";.**
64d0: 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74          azResult
64e0: 5b 34 5d 20 3d 20 22 42 6f 62 22 3b 0a 2a 2a 20  [4] = "Bob";.** 
64f0: 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 5b         azResult[
6500: 35 5d 20 3d 20 22 32 38 22 3b 0a 2a 2a 20 20 20  5] = "28";.**   
6510: 20 20 20 20 20 61 7a 52 65 73 75 6c 74 5b 36 5d       azResult[6]
6520: 20 3d 20 22 43 69 6e 64 79 22 3b 0a 2a 2a 20 20   = "Cindy";.**  
6530: 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 5b 37        azResult[7
6540: 5d 20 3d 20 22 32 31 22 3b 0a 2a 2a 20 3c 2f 70  ] = "21";.** </p
6550: 72 65 3e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 69 63 65  re>.**.** Notice
6560: 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 61   that there is a
6570: 6e 20 65 78 74 72 61 20 72 6f 77 20 6f 66 20 64  n extra row of d
6580: 61 74 61 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ata containing t
6590: 68 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 68 65 61  he column.** hea
65a0: 64 65 72 73 2e 20 20 42 75 74 20 74 68 65 20 2a  ders.  But the *
65b0: 6e 72 6f 77 20 72 65 74 75 72 6e 20 76 61 6c 75  nrow return valu
65c0: 65 20 69 73 20 73 74 69 6c 6c 20 33 2e 20 20 2a  e is still 3.  *
65d0: 6e 63 6f 6c 75 6d 6e 20 69 73 0a 2a 2a 20 73 65  ncolumn is.** se
65e0: 74 20 74 6f 20 32 2e 20 20 49 6e 20 67 65 6e 65  t to 2.  In gene
65f0: 72 61 6c 2c 20 74 68 65 20 6e 75 6d 62 65 72 20  ral, the number 
6600: 6f 66 20 76 61 6c 75 65 73 20 69 6e 73 65 72 74  of values insert
6610: 65 64 20 69 6e 74 6f 20 61 7a 52 65 73 75 6c 74  ed into azResult
6620: 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 28 28 2a 6e  .** will be ((*n
6630: 72 6f 77 29 20 2b 20 31 29 2a 28 2a 6e 63 6f 6c  row) + 1)*(*ncol
6640: 75 6d 6e 29 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65  umn)..**.** Afte
6650: 72 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75  r the calling fu
6660: 6e 63 74 69 6f 6e 20 68 61 73 20 66 69 6e 69 73  nction has finis
6670: 68 65 64 20 75 73 69 6e 67 20 74 68 65 20 72 65  hed using the re
6680: 73 75 6c 74 2c 20 69 74 20 73 68 6f 75 6c 64 20  sult, it should 
6690: 0a 2a 2a 20 70 61 73 73 20 74 68 65 20 72 65 73  .** pass the res
66a0: 75 6c 74 20 64 61 74 61 20 70 6f 69 6e 74 65 72  ult data pointer
66b0: 20 74 6f 20 73 71 6c 69 74 65 33 5f 66 72 65 65   to sqlite3_free
66c0: 5f 74 61 62 6c 65 28 29 20 69 6e 20 6f 72 64 65  _table() in orde
66d0: 72 20 74 6f 20 0a 2a 2a 20 72 65 6c 65 61 73 65  r to .** release
66e0: 20 74 68 65 20 6d 65 6d 6f 72 79 20 74 68 61 74   the memory that
66f0: 20 77 61 73 20 6d 61 6c 6c 6f 63 2d 65 64 2e 20   was malloc-ed. 
6700: 20 42 65 63 61 75 73 65 20 6f 66 20 74 68 65 20   Because of the 
6710: 77 61 79 20 74 68 65 20 0a 2a 2a 20 5b 73 71 6c  way the .** [sql
6720: 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 68  ite3_malloc()] h
6730: 61 70 70 65 6e 73 2c 20 74 68 65 20 63 61 6c 6c  appens, the call
6740: 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73  ing function mus
6750: 74 20 6e 6f 74 20 74 72 79 20 74 6f 20 63 61 6c  t not try to cal
6760: 6c 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66  l .** [sqlite3_f
6770: 72 65 65 28 29 5d 20 64 69 72 65 63 74 6c 79 2e  ree()] directly.
6780: 20 20 4f 6e 6c 79 20 5b 73 71 6c 69 74 65 33 5f    Only [sqlite3_
6790: 66 72 65 65 5f 74 61 62 6c 65 28 29 5d 20 69 73  free_table()] is
67a0: 20 61 62 6c 65 20 74 6f 20 72 65 6c 65 61 73 65   able to release
67b0: 20 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20   .** the memory 
67c0: 70 72 6f 70 65 72 6c 79 20 61 6e 64 20 73 61 66  properly and saf
67d0: 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  ely..**.** The r
67e0: 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74  eturn value of t
67f0: 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74  his routine is t
6800: 68 65 20 73 61 6d 65 20 61 73 20 66 72 6f 6d 20  he same as from 
6810: 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d  [sqlite3_exec()]
6820: 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33  ..*/.int sqlite3
6830: 5f 67 65 74 5f 74 61 62 6c 65 28 0a 20 20 73 71  _get_table(.  sq
6840: 6c 69 74 65 33 2a 2c 20 20 20 20 20 20 20 20 20  lite3*,         
6850: 20 20 20 20 20 2f 2a 20 41 6e 20 6f 70 65 6e 20       /* An open 
6860: 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 63 6f  database */.  co
6870: 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 2c 20 20  nst char *sql,  
6880: 20 20 20 20 20 2f 2a 20 53 51 4c 20 74 6f 20 62       /* SQL to b
6890: 65 20 65 78 65 63 75 74 65 64 20 2a 2f 0a 20 20  e executed */.  
68a0: 63 68 61 72 20 2a 2a 2a 72 65 73 75 6c 74 70 2c  char ***resultp,
68b0: 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74         /* Result
68c0: 20 77 72 69 74 74 65 6e 20 74 6f 20 61 20 63 68   written to a ch
68d0: 61 72 20 2a 5b 5d 20 20 74 68 61 74 20 74 68 69  ar *[]  that thi
68e0: 73 20 70 6f 69 6e 74 73 20 74 6f 20 2a 2f 0a 20  s points to */. 
68f0: 20 69 6e 74 20 2a 6e 72 6f 77 2c 20 20 20 20 20   int *nrow,     
6900: 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
6910: 72 20 6f 66 20 72 65 73 75 6c 74 20 72 6f 77 73  r of result rows
6920: 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f   written here */
6930: 0a 20 20 69 6e 74 20 2a 6e 63 6f 6c 75 6d 6e 2c  .  int *ncolumn,
6940: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
6950: 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20 63 6f  ber of result co
6960: 6c 75 6d 6e 73 20 77 72 69 74 74 65 6e 20 68 65  lumns written he
6970: 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 65  re */.  char **e
6980: 72 72 6d 73 67 20 20 20 20 20 20 20 20 20 20 2f  rrmsg          /
6990: 2a 20 45 72 72 6f 72 20 6d 73 67 20 77 72 69 74  * Error msg writ
69a0: 74 65 6e 20 68 65 72 65 20 2a 2f 0a 29 3b 0a 76  ten here */.);.v
69b0: 6f 69 64 20 73 71 6c 69 74 65 33 5f 66 72 65 65  oid sqlite3_free
69c0: 5f 74 61 62 6c 65 28 63 68 61 72 20 2a 2a 72 65  _table(char **re
69d0: 73 75 6c 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  sult);../*.** CA
69e0: 50 49 33 52 45 46 3a 20 46 6f 72 6d 61 74 74 65  PI3REF: Formatte
69f0: 64 20 53 74 72 69 6e 67 20 50 72 69 6e 74 69 6e  d String Printin
6a00: 67 20 46 75 6e 63 74 69 6f 6e 73 0a 2a 2a 0a 2a  g Functions.**.*
6a10: 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
6a20: 20 61 72 65 20 77 6f 72 6b 61 6c 69 6b 65 73 20   are workalikes 
6a30: 6f 66 20 74 68 65 20 22 70 72 69 6e 74 66 28 29  of the "printf()
6a40: 22 20 66 61 6d 69 6c 79 20 6f 66 20 66 75 6e 63  " family of func
6a50: 74 69 6f 6e 73 0a 2a 2a 20 66 72 6f 6d 20 74 68  tions.** from th
6a60: 65 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69 62  e standard C lib
6a70: 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  rary..**.** The 
6a80: 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
6a90: 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 76 6d  ) and sqlite3_vm
6aa0: 70 72 69 6e 74 66 28 29 20 72 6f 75 74 69 6e 65  printf() routine
6ab0: 73 20 77 72 69 74 65 20 74 68 65 69 72 0a 2a 2a  s write their.**
6ac0: 20 72 65 73 75 6c 74 73 20 69 6e 74 6f 20 6d 65   results into me
6ad0: 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72  mory obtained fr
6ae0: 6f 6d 20 5b 73 71 6c 69 74 65 5f 6d 61 6c 6c 6f  om [sqlite_mallo
6af0: 63 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73 74 72  c()]..** The str
6b00: 69 6e 67 73 20 72 65 74 75 72 6e 65 64 20 62 79  ings returned by
6b10: 20 74 68 65 73 65 20 74 77 6f 20 72 6f 75 74 69   these two routi
6b20: 6e 65 73 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a  nes should be.**
6b30: 20 72 65 6c 65 61 73 65 64 20 62 79 20 5b 73 71   released by [sq
6b40: 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 20 20  lite3_free()].  
6b50: 42 6f 74 68 20 72 6f 75 74 69 6e 65 73 20 72 65  Both routines re
6b60: 74 75 72 6e 20 61 0a 2a 2a 20 4e 55 4c 4c 20 70  turn a.** NULL p
6b70: 6f 69 6e 74 65 72 20 69 66 20 5b 73 71 6c 69 74  ointer if [sqlit
6b80: 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 69 73 20  e3_malloc()] is 
6b90: 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61  unable to alloca
6ba0: 74 65 20 65 6e 6f 75 67 68 0a 2a 2a 20 6d 65 6d  te enough.** mem
6bb0: 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ory to hold the 
6bc0: 72 65 73 75 6c 74 69 6e 67 20 73 74 72 69 6e 67  resulting string
6bd0: 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 73 71 6c 69 74  ..**.** In sqlit
6be0: 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 20 72 6f  e3_snprintf() ro
6bf0: 75 74 69 6e 65 20 69 73 20 73 69 6d 69 6c 61 72  utine is similar
6c00: 20 74 6f 20 22 73 6e 70 72 69 6e 74 66 28 29 22   to "snprintf()"
6c10: 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 73 74 61   from.** the sta
6c20: 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72 79 2e  ndard C library.
6c30: 20 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20    The result is 
6c40: 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65  written into the
6c50: 0a 2a 2a 20 62 75 66 66 65 72 20 73 75 70 70 6c  .** buffer suppl
6c60: 69 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e  ied as the secon
6c70: 64 20 70 61 72 61 6d 65 74 65 72 20 77 68 6f 73  d parameter whos
6c80: 65 20 73 69 7a 65 20 69 73 20 67 69 76 65 6e 20  e size is given 
6c90: 62 79 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20  by.** the first 
6ca0: 70 61 72 61 6d 65 74 65 72 2e 20 20 4e 6f 74 65  parameter.  Note
6cb0: 20 74 68 61 74 20 74 68 65 20 6f 72 64 65 72 20   that the order 
6cc0: 6f 66 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20  of the.** first 
6cd0: 74 77 6f 20 70 61 72 61 6d 65 74 65 72 73 20 69  two parameters i
6ce0: 73 20 72 65 76 65 72 73 65 64 20 66 72 6f 6d 20  s reversed from 
6cf0: 73 6e 70 72 69 6e 74 66 28 29 2e 20 20 54 68 69  snprintf().  Thi
6d00: 73 20 69 73 20 61 6e 0a 2a 2a 20 68 69 73 74 6f  s is an.** histo
6d10: 72 69 63 61 6c 20 61 63 63 69 64 65 6e 74 20 74  rical accident t
6d20: 68 61 74 20 63 61 6e 6e 6f 74 20 62 65 20 66 69  hat cannot be fi
6d30: 78 65 64 20 77 69 74 68 6f 75 74 20 62 72 65 61  xed without brea
6d40: 6b 69 6e 67 0a 2a 2a 20 62 61 63 6b 77 61 72 64  king.** backward
6d50: 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e  s compatibility.
6d60: 20 20 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61 74    Note also that
6d70: 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
6d80: 66 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61  f().** returns a
6d90: 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 73 20   pointer to its 
6da0: 62 75 66 66 65 72 20 69 6e 73 74 65 61 64 20 6f  buffer instead o
6db0: 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a  f the number of.
6dc0: 2a 2a 20 63 68 61 72 61 63 74 65 72 73 20 61 63  ** characters ac
6dd0: 74 75 61 6c 6c 79 20 77 72 69 74 74 65 6e 20 69  tually written i
6de0: 6e 74 6f 20 74 68 65 20 62 75 66 66 65 72 2e 20  nto the buffer. 
6df0: 20 57 65 20 61 64 6d 69 74 20 74 68 61 74 0a 2a   We admit that.*
6e00: 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * the number of 
6e10: 63 68 61 72 61 63 74 65 72 73 20 77 72 69 74 74  characters writt
6e20: 65 6e 20 77 6f 75 6c 64 20 62 65 20 61 20 6d 6f  en would be a mo
6e30: 72 65 20 75 73 65 66 75 6c 20 72 65 74 75 72 6e  re useful return
6e40: 0a 2a 2a 20 76 61 6c 75 65 20 62 75 74 20 77 65  .** value but we
6e50: 20 63 61 6e 6e 6f 74 20 63 68 61 6e 67 65 20 74   cannot change t
6e60: 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
6e70: 6e 20 6f 66 20 73 71 6c 69 74 65 33 5f 73 6e 70  n of sqlite3_snp
6e80: 72 69 6e 74 66 28 29 0a 2a 2a 20 6e 6f 77 20 77  rintf().** now w
6e90: 69 74 68 6f 75 74 20 62 72 65 61 6b 69 6e 67 20  ithout breaking 
6ea0: 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a 2a  compatibility..*
6eb0: 2a 0a 2a 2a 20 41 73 20 6c 6f 6e 67 20 61 73 20  *.** As long as 
6ec0: 74 68 65 20 62 75 66 66 65 72 20 73 69 7a 65 20  the buffer size 
6ed0: 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
6ee0: 7a 65 72 6f 2c 20 73 71 6c 69 74 65 33 5f 73 6e  zero, sqlite3_sn
6ef0: 70 72 69 6e 74 66 28 29 0a 2a 2a 20 67 75 61 72  printf().** guar
6f00: 61 6e 74 65 65 73 20 74 68 61 74 20 74 68 65 20  antees that the 
6f10: 62 75 66 66 65 72 20 69 73 20 61 6c 77 61 79 73  buffer is always
6f20: 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64   zero-terminated
6f30: 2e 20 20 54 68 65 20 66 69 72 73 74 0a 2a 2a 20  .  The first.** 
6f40: 70 61 72 61 6d 65 74 65 72 20 22 6e 22 20 69 73  parameter "n" is
6f50: 20 74 68 65 20 74 6f 74 61 6c 20 73 69 7a 65 20   the total size 
6f60: 6f 66 20 74 68 65 20 62 75 66 66 65 72 2c 20 69  of the buffer, i
6f70: 6e 63 6c 75 64 69 6e 67 20 73 70 61 63 65 20 66  ncluding space f
6f80: 6f 72 0a 2a 2a 20 74 68 65 20 7a 65 72 6f 20 74  or.** the zero t
6f90: 65 72 6d 69 6e 61 74 6f 72 2e 20 20 53 6f 20 74  erminator.  So t
6fa0: 68 65 20 6c 6f 6e 67 65 73 74 20 73 74 72 69 6e  he longest strin
6fb0: 67 20 74 68 61 74 20 63 61 6e 20 62 65 20 63 6f  g that can be co
6fc0: 6d 70 6c 65 74 65 6c 79 0a 2a 2a 20 77 72 69 74  mpletely.** writ
6fd0: 74 65 6e 20 77 69 6c 6c 20 62 65 20 6e 2d 31 20  ten will be n-1 
6fe0: 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a  characters..**.*
6ff0: 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
7000: 20 61 6c 6c 20 69 6d 70 6c 65 6d 65 6e 74 20 73   all implement s
7010: 6f 6d 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 66  ome additional f
7020: 6f 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 6f 70 74  ormatting.** opt
7030: 69 6f 6e 73 20 74 68 61 74 20 61 72 65 20 75 73  ions that are us
7040: 65 66 75 6c 20 66 6f 72 20 63 6f 6e 73 74 72 75  eful for constru
7050: 63 74 69 6e 67 20 53 51 4c 20 73 74 61 74 65 6d  cting SQL statem
7060: 65 6e 74 73 2e 0a 2a 2a 20 41 6c 6c 20 6f 66 20  ents..** All of 
7070: 74 68 65 20 75 73 75 61 6c 20 70 72 69 6e 74 66  the usual printf
7080: 20 66 6f 72 6d 61 74 74 69 6e 67 20 6f 70 74 69   formatting opti
7090: 6f 6e 73 20 61 70 70 6c 79 2e 20 20 49 6e 20 61  ons apply.  In a
70a0: 64 64 69 74 69 6f 6e 2c 20 74 68 65 72 65 0a 2a  ddition, there.*
70b0: 2a 20 69 73 20 61 72 65 20 22 25 71 22 20 61 6e  * is are "%q" an
70c0: 64 20 22 25 51 22 20 6f 70 74 69 6f 6e 73 2e 0a  d "%Q" options..
70d0: 2a 2a 0a 2a 2a 20 54 68 65 20 25 71 20 6f 70 74  **.** The %q opt
70e0: 69 6f 6e 20 77 6f 72 6b 73 20 6c 69 6b 65 20 25  ion works like %
70f0: 73 20 69 6e 20 74 68 61 74 20 69 74 20 73 75 62  s in that it sub
7100: 73 74 69 74 75 74 65 73 20 61 20 6e 75 6c 6c 2d  stitutes a null-
7110: 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 73 74  terminated.** st
7120: 72 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 61 72  ring from the ar
7130: 67 75 6d 65 6e 74 20 6c 69 73 74 2e 20 20 42 75  gument list.  Bu
7140: 74 20 25 71 20 61 6c 73 6f 20 64 6f 75 62 6c 65  t %q also double
7150: 73 20 65 76 65 72 79 20 27 5c 27 27 20 63 68 61  s every '\'' cha
7160: 72 61 63 74 65 72 2e 0a 2a 2a 20 25 71 20 69 73  racter..** %q is
7170: 20 64 65 73 69 67 6e 65 64 20 66 6f 72 20 75 73   designed for us
7180: 65 20 69 6e 73 69 64 65 20 61 20 73 74 72 69 6e  e inside a strin
7190: 67 20 6c 69 74 65 72 61 6c 2e 20 20 42 79 20 64  g literal.  By d
71a0: 6f 75 62 6c 69 6e 67 20 65 61 63 68 20 27 5c 27  oubling each '\'
71b0: 27 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 20 69  '.** character i
71c0: 74 20 65 73 63 61 70 65 73 20 74 68 61 74 20 63  t escapes that c
71d0: 68 61 72 61 63 74 65 72 20 61 6e 64 20 61 6c 6c  haracter and all
71e0: 6f 77 73 20 69 74 20 74 6f 20 62 65 20 69 6e 73  ows it to be ins
71f0: 65 72 74 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 68  erted into.** th
7200: 65 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  e string..**.** 
7210: 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 73 6f 20  For example, so 
7220: 73 6f 6d 65 20 73 74 72 69 6e 67 20 76 61 72 69  some string vari
7230: 61 62 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 65  able contains te
7240: 78 74 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  xt as follows:.*
7250: 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65  *.** <blockquote
7260: 3e 3c 70 72 65 3e 0a 2a 2a 20 20 63 68 61 72 20  ><pre>.**  char 
7270: 2a 7a 54 65 78 74 20 3d 20 22 49 74 27 73 20 61  *zText = "It's a
7280: 20 68 61 70 70 79 20 64 61 79 21 22 3b 0a 2a 2a   happy day!";.**
7290: 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75   </pre></blockqu
72a0: 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 4f 6e 65 20 63  ote>.**.** One c
72b0: 61 6e 20 75 73 65 20 74 68 69 73 20 74 65 78 74  an use this text
72c0: 20 69 6e 20 61 6e 20 53 51 4c 20 73 74 61 74 65   in an SQL state
72d0: 6d 65 6e 74 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  ment as follows:
72e0: 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f  .**.** <blockquo
72f0: 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 63 68 61  te><pre>.**  cha
7300: 72 20 2a 7a 53 51 4c 20 3d 20 73 71 6c 69 74 65  r *zSQL = sqlite
7310: 33 5f 6d 70 72 69 6e 74 66 28 22 49 4e 53 45 52  3_mprintf("INSER
7320: 54 20 49 4e 54 4f 20 74 61 62 6c 65 20 56 41 4c  T INTO table VAL
7330: 55 45 53 28 27 25 71 27 29 22 2c 20 7a 54 65 78  UES('%q')", zTex
7340: 74 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f  t);.**  sqlite3_
7350: 65 78 65 63 28 64 62 2c 20 7a 53 51 4c 2c 20 30  exec(db, zSQL, 0
7360: 2c 20 30 2c 20 30 29 3b 0a 2a 2a 20 20 73 71 6c  , 0, 0);.**  sql
7370: 69 74 65 33 5f 66 72 65 65 28 7a 53 51 4c 29 3b  ite3_free(zSQL);
7380: 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63  .** </pre></bloc
7390: 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 42 65  kquote>.**.** Be
73a0: 63 61 75 73 65 20 74 68 65 20 25 71 20 66 6f 72  cause the %q for
73b0: 6d 61 74 20 73 74 72 69 6e 67 20 69 73 20 75 73  mat string is us
73c0: 65 64 2c 20 74 68 65 20 27 5c 27 27 20 63 68 61  ed, the '\'' cha
73d0: 72 61 63 74 65 72 20 69 6e 20 7a 54 65 78 74 0a  racter in zText.
73e0: 2a 2a 20 69 73 20 65 73 63 61 70 65 64 20 61 6e  ** is escaped an
73f0: 64 20 74 68 65 20 53 51 4c 20 67 65 6e 65 72 61  d the SQL genera
7400: 74 65 64 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77  ted is as follow
7410: 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71  s:.**.** <blockq
7420: 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 49  uote><pre>.**  I
7430: 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c 65  NSERT INTO table
7440: 31 20 56 41 4c 55 45 53 28 27 49 74 27 27 73 20  1 VALUES('It''s 
7450: 61 20 68 61 70 70 79 20 64 61 79 21 27 29 0a 2a  a happy day!').*
7460: 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71  * </pre></blockq
7470: 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  uote>.**.** This
7480: 20 69 73 20 63 6f 72 72 65 63 74 2e 20 20 48 61   is correct.  Ha
7490: 64 20 77 65 20 75 73 65 64 20 25 73 20 69 6e 73  d we used %s ins
74a0: 74 65 61 64 20 6f 66 20 25 71 2c 20 74 68 65 20  tead of %q, the 
74b0: 67 65 6e 65 72 61 74 65 64 20 53 51 4c 0a 2a 2a  generated SQL.**
74c0: 20 77 6f 75 6c 64 20 68 61 76 65 20 6c 6f 6f 6b   would have look
74d0: 65 64 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a  ed like this:.**
74e0: 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e  .** <blockquote>
74f0: 3c 70 72 65 3e 0a 2a 2a 20 20 49 4e 53 45 52 54  <pre>.**  INSERT
7500: 20 49 4e 54 4f 20 74 61 62 6c 65 31 20 56 41 4c   INTO table1 VAL
7510: 55 45 53 28 27 49 74 27 73 20 61 20 68 61 70 70  UES('It's a happ
7520: 79 20 64 61 79 21 27 29 3b 0a 2a 2a 20 3c 2f 70  y day!');.** </p
7530: 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e  re></blockquote>
7540: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 65 63 6f  .**.** This seco
7550: 6e 64 20 65 78 61 6d 70 6c 65 20 69 73 20 61 6e  nd example is an
7560: 20 53 51 4c 20 73 79 6e 74 61 78 20 65 72 72 6f   SQL syntax erro
7570: 72 2e 20 20 41 73 20 61 20 67 65 6e 65 72 61 6c  r.  As a general
7580: 20 72 75 6c 65 20 79 6f 75 0a 2a 2a 20 73 68 6f   rule you.** sho
7590: 75 6c 64 20 61 6c 77 61 79 73 20 75 73 65 20 25  uld always use %
75a0: 71 20 69 6e 73 74 65 61 64 20 6f 66 20 25 73 20  q instead of %s 
75b0: 77 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 20 74  when inserting t
75c0: 65 78 74 20 69 6e 74 6f 20 61 20 73 74 72 69 6e  ext into a strin
75d0: 67 20 0a 2a 2a 20 6c 69 74 65 72 61 6c 2e 0a 2a  g .** literal..*
75e0: 2a 0a 2a 2a 20 54 68 65 20 25 51 20 6f 70 74 69  *.** The %Q opti
75f0: 6f 6e 20 77 6f 72 6b 73 20 6c 69 6b 65 20 25 71  on works like %q
7600: 20 65 78 63 65 70 74 20 69 74 20 61 6c 73 6f 20   except it also 
7610: 61 64 64 73 20 73 69 6e 67 6c 65 20 71 75 6f 74  adds single quot
7620: 65 73 20 61 72 6f 75 6e 64 0a 2a 2a 20 74 68 65  es around.** the
7630: 20 6f 75 74 73 69 64 65 20 6f 66 20 74 68 65 20   outside of the 
7640: 74 6f 74 61 6c 20 73 74 72 69 6e 67 2e 20 20 4f  total string.  O
7650: 72 20 69 66 20 74 68 65 20 70 61 72 61 6d 65 74  r if the paramet
7660: 65 72 20 69 6e 20 74 68 65 20 61 72 67 75 6d 65  er in the argume
7670: 6e 74 0a 2a 2a 20 6c 69 73 74 20 69 73 20 61 20  nt.** list is a 
7680: 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 25 51  NULL pointer, %Q
7690: 20 73 75 62 73 74 69 74 75 74 65 73 20 74 68 65   substitutes the
76a0: 20 74 65 78 74 20 22 4e 55 4c 4c 22 20 28 77 69   text "NULL" (wi
76b0: 74 68 6f 75 74 20 73 69 6e 67 6c 65 0a 2a 2a 20  thout single.** 
76c0: 71 75 6f 74 65 73 29 20 69 6e 20 70 6c 61 63 65  quotes) in place
76d0: 20 6f 66 20 74 68 65 20 25 51 20 6f 70 74 69 6f   of the %Q optio
76e0: 6e 2e 20 20 53 6f 2c 20 66 6f 72 20 65 78 61 6d  n.  So, for exam
76f0: 70 6c 65 2c 20 6f 6e 65 20 63 6f 75 6c 64 20 73  ple, one could s
7700: 61 79 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b  ay:.**.** <block
7710: 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20  quote><pre>.**  
7720: 63 68 61 72 20 2a 7a 53 51 4c 20 3d 20 73 71 6c  char *zSQL = sql
7730: 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 49 4e  ite3_mprintf("IN
7740: 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c 65 20  SERT INTO table 
7750: 56 41 4c 55 45 53 28 25 51 29 22 2c 20 7a 54 65  VALUES(%Q)", zTe
7760: 78 74 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33  xt);.**  sqlite3
7770: 5f 65 78 65 63 28 64 62 2c 20 7a 53 51 4c 2c 20  _exec(db, zSQL, 
7780: 30 2c 20 30 2c 20 30 29 3b 0a 2a 2a 20 20 73 71  0, 0, 0);.**  sq
7790: 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 51 4c 29  lite3_free(zSQL)
77a0: 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f  ;.** </pre></blo
77b0: 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54  ckquote>.**.** T
77c0: 68 65 20 63 6f 64 65 20 61 62 6f 76 65 20 77 69  he code above wi
77d0: 6c 6c 20 72 65 6e 64 65 72 20 61 20 63 6f 72 72  ll render a corr
77e0: 65 63 74 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  ect SQL statemen
77f0: 74 20 69 6e 20 74 68 65 20 7a 53 51 4c 0a 2a 2a  t in the zSQL.**
7800: 20 76 61 72 69 61 62 6c 65 20 65 76 65 6e 20 69   variable even i
7810: 66 20 74 68 65 20 7a 54 65 78 74 20 76 61 72 69  f the zText vari
7820: 61 62 6c 65 20 69 73 20 61 20 4e 55 4c 4c 20 70  able is a NULL p
7830: 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54  ointer..*/.SQLIT
7840: 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69  E_API char *sqli
7850: 74 65 33 5f 6d 70 72 69 6e 74 66 28 63 6f 6e 73  te3_mprintf(cons
7860: 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 53 51  t char*,...);.SQ
7870: 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73  LITE_API char *s
7880: 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28  qlite3_vmprintf(
7890: 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76 61 5f  const char*, va_
78a0: 6c 69 73 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50  list);.SQLITE_AP
78b0: 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  I char *sqlite3_
78c0: 73 6e 70 72 69 6e 74 66 28 69 6e 74 2c 63 68 61  snprintf(int,cha
78d0: 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  r*,const char*, 
78e0: 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  ...);../*.** CAP
78f0: 49 33 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41 6c  I3REF: Memory Al
7900: 6c 6f 63 61 74 69 6f 6e 20 46 75 6e 63 74 69 6f  location Functio
7910: 6e 73 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  ns.**.** SQLite 
7920: 75 73 65 73 20 69 74 73 20 6f 77 6e 20 6d 65 6d  uses its own mem
7930: 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 2e 20 20  ory allocator.  
7940: 4f 6e 20 73 6f 6d 65 20 69 6e 73 74 61 6c 6c 61  On some installa
7950: 74 69 6f 6e 73 2c 20 74 68 69 73 0a 2a 2a 20 6d  tions, this.** m
7960: 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20  emory allocator 
7970: 69 73 20 69 64 65 6e 74 69 63 61 6c 20 74 6f 20  is identical to 
7980: 74 68 65 20 73 74 61 6e 64 61 72 64 20 6d 61 6c  the standard mal
7990: 6c 6f 63 28 29 2f 72 65 61 6c 6c 6f 63 28 29 2f  loc()/realloc()/
79a0: 66 72 65 65 28 29 0a 2a 2a 20 61 6e 64 20 63 61  free().** and ca
79b0: 6e 20 62 65 20 75 73 65 64 20 69 6e 74 65 72 63  n be used interc
79c0: 68 61 6e 67 61 62 6c 65 2e 20 20 4f 6e 20 6f 74  hangable.  On ot
79d0: 68 65 72 73 2c 20 74 68 65 20 69 6d 70 6c 65 6d  hers, the implem
79e0: 65 6e 74 61 74 69 6f 6e 73 20 61 72 65 0a 2a 2a  entations are.**
79f0: 20 64 69 66 66 65 72 65 6e 74 2e 20 20 46 6f 72   different.  For
7a00: 20 6d 61 78 69 6d 75 6d 20 70 6f 72 74 61 62 69   maximum portabi
7a10: 6c 69 74 79 2c 20 69 74 20 69 73 20 62 65 73 74  lity, it is best
7a20: 20 6e 6f 74 20 74 6f 20 6d 69 78 20 63 61 6c 6c   not to mix call
7a30: 73 0a 2a 2a 20 74 6f 20 74 68 65 20 73 74 61 6e  s.** to the stan
7a40: 64 61 72 64 20 6d 61 6c 6c 6f 63 2f 72 65 61 6c  dard malloc/real
7a50: 6c 6f 63 2f 66 72 65 65 20 77 69 74 68 20 74 68  loc/free with th
7a60: 65 20 73 71 6c 69 74 65 20 76 65 72 73 69 6f 6e  e sqlite version
7a70: 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  s..*/.SQLITE_API
7a80: 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 6d   void *sqlite3_m
7a90: 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49  alloc(int);.SQLI
7aa0: 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
7ab0: 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 76 6f 69  ite3_realloc(voi
7ac0: 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  d*, int);.SQLITE
7ad0: 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
7ae0: 33 5f 66 72 65 65 28 76 6f 69 64 2a 29 3b 0a 0a  3_free(void*);..
7af0: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
7b00: 43 6f 6d 70 69 6c 65 2d 54 69 6d 65 20 41 75 74  Compile-Time Aut
7b10: 68 6f 72 69 7a 61 74 69 6f 6e 20 43 61 6c 6c 62  horization Callb
7b20: 61 63 6b 73 0a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  acks.***.** This
7b30: 20 72 6f 75 74 69 6e 65 20 72 65 67 69 73 74 65   routine registe
7b40: 72 73 20 61 20 61 75 74 68 6f 72 69 7a 65 72 20  rs a authorizer 
7b50: 63 61 6c 6c 62 61 63 6b 20 77 69 74 68 20 74 68  callback with th
7b60: 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79  e SQLite library
7b70: 2e 20 20 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .  .** The autho
7b80: 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69  rizer callback i
7b90: 73 20 69 6e 76 6f 6b 65 64 20 61 73 20 53 51 4c  s invoked as SQL
7ba0: 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20   statements are 
7bb0: 62 65 69 6e 67 20 63 6f 6d 70 69 6c 65 64 0a 2a  being compiled.*
7bc0: 2a 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 70 72  * by [sqlite3_pr
7bd0: 65 70 61 72 65 28 29 5d 20 6f 72 20 69 74 73 20  epare()] or its 
7be0: 76 61 72 69 61 6e 74 73 20 5b 73 71 6c 69 74 65  variants [sqlite
7bf0: 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 2c  3_prepare_v2()],
7c00: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  .** [sqlite3_pre
7c10: 70 61 72 65 31 36 28 29 5d 20 61 6e 64 20 5b 73  pare16()] and [s
7c20: 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
7c30: 5f 76 32 28 29 5d 2e 20 20 41 74 20 76 61 72 69  _v2()].  At vari
7c40: 6f 75 73 0a 2a 2a 20 70 6f 69 6e 74 73 20 64 75  ous.** points du
7c50: 72 69 6e 67 20 74 68 65 20 63 6f 6d 70 69 6c 61  ring the compila
7c60: 74 69 6f 6e 20 70 72 6f 63 65 73 73 2c 20 61 73  tion process, as
7c70: 20 6c 6f 67 69 63 20 69 73 20 62 65 69 6e 67 20   logic is being 
7c80: 63 72 65 61 74 65 64 0a 2a 2a 20 74 6f 20 70 65  created.** to pe
7c90: 72 66 6f 72 6d 20 76 61 72 69 6f 75 73 20 61 63  rform various ac
7ca0: 74 69 6f 6e 73 2c 20 74 68 65 20 61 75 74 68 6f  tions, the autho
7cb0: 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69  rizer callback i
7cc0: 73 20 69 6e 76 6f 6b 65 64 20 74 6f 0a 2a 2a 20  s invoked to.** 
7cd0: 73 65 65 20 69 66 20 74 68 6f 73 65 20 61 63 74  see if those act
7ce0: 69 6f 6e 73 20 61 72 65 20 61 6c 6c 6f 77 65 64  ions are allowed
7cf0: 2e 20 20 54 68 65 20 61 75 74 68 6f 72 69 7a 65  .  The authorize
7d00: 72 20 63 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c  r callback shoul
7d10: 64 0a 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c 49  d.** return SQLI
7d20: 54 45 5f 4f 4b 20 74 6f 20 61 6c 6c 6f 77 20 74  TE_OK to allow t
7d30: 68 65 20 61 63 74 69 6f 6e 2c 20 5b 53 51 4c 49  he action, [SQLI
7d40: 54 45 5f 49 47 4e 4f 52 45 5d 20 74 6f 20 64 69  TE_IGNORE] to di
7d50: 73 61 6c 6c 6f 77 20 74 68 65 0a 2a 2a 20 73 70  sallow the.** sp
7d60: 65 63 69 66 69 63 20 61 63 74 69 6f 6e 20 62 75  ecific action bu
7d70: 74 20 61 6c 6c 6f 77 20 74 68 65 20 53 51 4c 20  t allow the SQL 
7d80: 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 63 6f 6e  statement to con
7d90: 74 69 6e 75 65 20 74 6f 20 62 65 0a 2a 2a 20 63  tinue to be.** c
7da0: 6f 6d 70 69 6c 65 64 2c 20 6f 72 20 5b 53 51 4c  ompiled, or [SQL
7db0: 49 54 45 5f 44 45 4e 59 5d 20 74 6f 20 63 61 75  ITE_DENY] to cau
7dc0: 73 65 20 74 68 65 20 65 6e 74 69 72 65 20 53 51  se the entire SQ
7dd0: 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62  L statement to b
7de0: 65 0a 2a 2a 20 72 65 6a 65 63 74 65 64 20 77 69  e.** rejected wi
7df0: 74 68 20 61 6e 20 65 72 72 6f 72 2e 20 20 0a 2a  th an error.  .*
7e00: 2a 0a 2a 2a 20 44 65 70 65 6e 64 69 6e 67 20 6f  *.** Depending o
7e10: 6e 20 74 68 65 20 61 63 74 69 6f 6e 2c 20 74 68  n the action, th
7e20: 65 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  e [SQLITE_IGNORE
7e30: 5d 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 44 45  ] and [SQLITE_DE
7e40: 4e 59 5d 20 72 65 74 75 72 6e 0a 2a 2a 20 63 6f  NY] return.** co
7e50: 64 65 73 20 6d 69 67 68 74 20 6d 65 61 6e 20 73  des might mean s
7e60: 6f 6d 65 74 68 69 6e 67 20 64 69 66 66 65 72 65  omething differe
7e70: 6e 74 20 6f 72 20 74 68 65 79 20 6d 69 67 68 74  nt or they might
7e80: 20 6d 65 61 6e 20 74 68 65 20 73 61 6d 65 0a 2a   mean the same.*
7e90: 2a 20 74 68 69 6e 67 2e 20 20 49 66 20 74 68 65  * thing.  If the
7ea0: 20 61 63 74 69 6f 6e 20 69 73 2c 20 66 6f 72 20   action is, for 
7eb0: 65 78 61 6d 70 6c 65 2c 20 74 6f 20 70 65 72 66  example, to perf
7ec0: 6f 72 6d 20 61 20 64 65 6c 65 74 65 20 6f 70 65  orm a delete ope
7ed0: 72 74 69 6f 6e 2c 0a 2a 2a 20 74 68 65 6e 20 5b  rtion,.** then [
7ee0: 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 20 61  SQLITE_IGNORE] a
7ef0: 6e 64 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59 5d  nd [SQLITE_DENY]
7f00: 20 62 6f 74 68 20 63 61 75 73 65 20 74 68 65 20   both cause the 
7f10: 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 69 6c  statement compil
7f20: 61 74 69 6f 6e 0a 2a 2a 20 74 6f 20 66 61 69 6c  ation.** to fail
7f30: 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 2e 20   with an error. 
7f40: 20 42 75 74 20 69 66 20 74 68 65 20 61 63 74 69   But if the acti
7f50: 6f 6e 20 69 73 20 74 6f 20 72 65 61 64 20 61 20  on is to read a 
7f60: 73 70 65 63 69 66 69 63 20 63 6f 6c 75 6d 6e 0a  specific column.
7f70: 2a 2a 20 66 72 6f 6d 20 61 20 73 70 65 63 69 66  ** from a specif
7f80: 69 63 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 5b  ic table, then [
7f90: 53 51 4c 49 54 45 5f 44 45 4e 59 5d 20 77 69 6c  SQLITE_DENY] wil
7fa0: 6c 20 63 61 75 73 65 20 74 68 65 20 65 6e 74 69  l cause the enti
7fb0: 72 65 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20  re.** statement 
7fc0: 74 6f 20 66 61 69 6c 20 62 75 74 20 5b 53 51 4c  to fail but [SQL
7fd0: 49 54 45 5f 49 47 4e 4f 52 45 5d 20 77 69 6c 6c  ITE_IGNORE] will
7fe0: 20 63 61 75 73 65 20 61 20 4e 55 4c 4c 20 76 61   cause a NULL va
7ff0: 6c 75 65 20 74 6f 20 62 65 0a 2a 2a 20 72 65 61  lue to be.** rea
8000: 64 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65  d instead of the
8010: 20 61 63 74 75 61 6c 20 63 6f 6c 75 6d 6e 20 76   actual column v
8020: 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  alue..**.** The 
8030: 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20  first parameter 
8040: 74 6f 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65  to the authorize
8050: 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 20  r callback is a 
8060: 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20 74  copy of.** the t
8070: 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 74  hird parameter t
8080: 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 65  o the sqlite3_se
8090: 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 29 20 69  t_authorizer() i
80a0: 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 20 54 68 65  nterface..** The
80b0: 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   second paramete
80c0: 72 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63  r to the callbac
80d0: 6b 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20  k is an integer 
80e0: 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 50 59  .** [SQLITE_COPY
80f0: 20 7c 20 61 63 74 69 6f 6e 20 63 6f 64 65 5d 20   | action code] 
8100: 74 68 61 74 20 73 70 65 63 69 66 69 65 73 20 74  that specifies t
8110: 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 61 63  he particular ac
8120: 74 69 6f 6e 0a 2a 2a 20 74 6f 20 62 65 20 61 75  tion.** to be au
8130: 74 68 6f 72 69 7a 65 64 2e 20 20 54 68 65 20 61  thorized.  The a
8140: 76 61 69 6c 61 62 6c 65 20 61 63 74 69 6f 6e 20  vailable action 
8150: 63 6f 64 65 73 20 61 72 65 0a 2a 2a 20 5b 53 51  codes are.** [SQ
8160: 4c 49 54 45 5f 43 4f 50 59 20 7c 20 64 6f 63 75  LITE_COPY | docu
8170: 6d 65 6e 74 65 64 20 73 65 70 61 72 61 74 65 6c  mented separatel
8180: 79 5d 2e 20 20 54 68 65 20 74 68 69 72 64 20 74  y].  The third t
8190: 68 72 6f 75 67 68 20 73 69 78 74 68 0a 2a 2a 20  hrough sixth.** 
81a0: 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68  parameters to th
81b0: 65 20 63 61 6c 6c 62 61 63 6b 20 61 72 65 20 73  e callback are s
81c0: 74 72 69 6e 67 73 20 74 68 61 74 20 63 6f 6e 74  trings that cont
81d0: 61 69 6e 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a  ain additional.*
81e0: 2a 20 64 65 74 61 69 6c 73 20 61 62 6f 75 74 20  * details about 
81f0: 74 68 65 20 61 63 74 69 6f 6e 20 74 6f 20 62 65  the action to be
8200: 20 61 75 74 68 6f 72 69 7a 65 64 2e 0a 2a 2a 0a   authorized..**.
8210: 2a 2a 20 41 6e 20 61 75 74 68 6f 72 69 7a 65 72  ** An authorizer
8220: 20 69 73 20 75 73 65 64 20 77 68 65 6e 20 70 72   is used when pr
8230: 65 70 61 72 69 6e 67 20 53 51 4c 20 73 74 61 74  eparing SQL stat
8240: 65 6d 65 6e 74 73 20 66 72 6f 6d 20 61 6e 20 75  ements from an u
8250: 6e 74 72 75 73 74 65 64 0a 2a 2a 20 73 6f 75 72  ntrusted.** sour
8260: 63 65 2c 20 74 6f 20 65 6e 73 75 72 65 20 74 68  ce, to ensure th
8270: 61 74 20 74 68 65 20 53 51 4c 20 73 74 61 74 65  at the SQL state
8280: 6d 65 6e 74 73 20 64 6f 20 6e 6f 74 20 74 72 79  ments do not try
8290: 20 74 6f 20 61 63 63 65 73 73 20 64 61 74 61 0a   to access data.
82a0: 2a 2a 20 74 68 61 74 20 74 68 65 79 20 61 72 65  ** that they are
82b0: 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f 20   not allowed to 
82c0: 73 65 65 2c 20 6f 72 20 74 68 61 74 20 74 68 65  see, or that the
82d0: 79 20 64 6f 20 6e 6f 74 20 74 72 79 20 74 6f 0a  y do not try to.
82e0: 2a 2a 20 65 78 65 63 75 74 65 20 6d 61 6c 69 63  ** execute malic
82f0: 69 6f 75 73 20 73 74 61 74 65 6d 65 6e 74 73 20  ious statements 
8300: 74 68 61 74 20 64 61 6d 61 67 65 20 74 68 65 20  that damage the 
8310: 64 61 74 61 62 61 73 65 2e 20 20 46 6f 72 0a 2a  database.  For.*
8320: 2a 20 65 78 61 6d 70 6c 65 2c 20 61 6e 20 61 70  * example, an ap
8330: 70 6c 69 63 61 74 69 6f 6e 20 6d 61 79 20 61 6c  plication may al
8340: 6c 6f 77 20 61 20 75 73 65 72 20 74 6f 20 65 6e  low a user to en
8350: 74 65 72 20 61 72 62 69 74 72 61 72 79 0a 2a 2a  ter arbitrary.**
8360: 20 53 51 4c 20 71 75 65 72 69 65 73 20 66 6f 72   SQL queries for
8370: 20 65 76 61 6c 75 61 74 69 6f 6e 20 62 79 20 61   evaluation by a
8380: 20 64 61 74 61 62 61 73 65 2e 20 20 42 75 74 20   database.  But 
8390: 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  the application 
83a0: 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74  does.** not want
83b0: 20 74 68 65 20 75 73 65 72 20 74 6f 20 62 65 20   the user to be 
83c0: 61 62 6c 65 20 74 6f 20 6d 61 6b 65 20 61 72 62  able to make arb
83d0: 69 74 72 61 72 79 20 63 68 61 6e 67 65 73 20 74  itrary changes t
83e0: 6f 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73  o the.** databas
83f0: 65 2e 20 20 41 6e 20 61 75 74 68 6f 72 69 7a 65  e.  An authorize
8400: 72 20 63 6f 75 6c 64 20 74 68 65 6e 20 62 65 20  r could then be 
8410: 70 75 74 20 69 6e 20 70 6c 61 63 65 20 77 68 69  put in place whi
8420: 6c 65 20 74 68 65 0a 2a 2a 20 75 73 65 72 2d 65  le the.** user-e
8430: 6e 74 65 72 65 64 20 53 51 4c 20 69 73 20 62 65  ntered SQL is be
8440: 69 6e 67 20 70 72 65 70 61 72 65 64 20 74 68 61  ing prepared tha
8450: 74 20 64 69 73 61 6c 6c 6f 77 73 20 65 76 65 72  t disallows ever
8460: 79 74 68 69 6e 67 0a 2a 2a 20 65 78 63 65 70 74  ything.** except
8470: 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
8480: 74 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79  ts.  .**.** Only
8490: 20 61 20 73 69 6e 67 6c 65 20 61 75 74 68 6f 72   a single author
84a0: 69 7a 65 72 20 63 61 6e 20 62 65 20 69 6e 20 70  izer can be in p
84b0: 6c 61 63 65 20 6f 6e 20 61 20 64 61 74 61 62 61  lace on a databa
84c0: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a  se connection.**
84d0: 20 61 74 20 61 20 74 69 6d 65 2e 20 20 45 61 63   at a time.  Eac
84e0: 68 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  h call to sqlite
84f0: 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72  3_set_authorizer
8500: 20 6f 76 65 72 72 69 64 65 73 20 74 68 65 0a 2a   overrides the.*
8510: 2a 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 2e  * previous call.
8520: 20 20 41 20 4e 55 4c 4c 20 61 75 74 68 6f 72 69    A NULL authori
8530: 7a 65 72 20 6d 65 61 6e 73 20 74 68 61 74 20 6e  zer means that n
8540: 6f 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 0a  o authorization.
8550: 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69  ** callback is i
8560: 6e 76 6f 6b 65 64 2e 20 20 54 68 65 20 64 65 66  nvoked.  The def
8570: 61 75 6c 74 20 61 75 74 68 6f 72 69 7a 65 72 20  ault authorizer 
8580: 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 4e  is NULL..**.** N
8590: 6f 74 65 20 74 68 61 74 20 74 68 65 20 61 75 74  ote that the aut
85a0: 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b  horizer callback
85b0: 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 6c 79   is invoked only
85c0: 20 64 75 72 69 6e 67 20 0a 2a 2a 20 5b 73 71 6c   during .** [sql
85d0: 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20  ite3_prepare()] 
85e0: 6f 72 20 69 74 73 20 76 61 72 69 61 6e 74 73 2e  or its variants.
85f0: 20 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20    Authorization 
8600: 69 73 20 6e 6f 74 0a 2a 2a 20 70 65 72 66 6f 72  is not.** perfor
8610: 6d 65 64 20 64 75 72 69 6e 67 20 73 74 61 74 65  med during state
8620: 6d 65 6e 74 20 65 76 61 6c 75 61 74 69 6f 6e 20  ment evaluation 
8630: 69 6e 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  in [sqlite3_step
8640: 28 29 5d 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69  ()]..*/.int sqli
8650: 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a  te3_set_authoriz
8660: 65 72 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 0a  er(.  sqlite3*,.
8670: 20 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76    int (*xAuth)(v
8680: 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63  oid*,int,const c
8690: 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
86a0: 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
86b0: 73 74 20 63 68 61 72 2a 29 2c 0a 20 20 76 6f 69  st char*),.  voi
86c0: 64 20 2a 70 55 73 65 72 44 61 74 61 0a 29 3b 0a  d *pUserData.);.
86d0: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
86e0: 20 41 75 74 68 6f 72 69 7a 65 72 20 52 65 74 75   Authorizer Retu
86f0: 72 6e 20 43 6f 64 65 73 0a 2a 2a 0a 2a 2a 20 54  rn Codes.**.** T
8700: 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f  he [sqlite3_set_
8710: 61 75 74 68 6f 72 69 7a 65 72 20 7c 20 61 75 74  authorizer | aut
8720: 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b  horizer callback
8730: 20 66 75 6e 63 74 69 6f 6e 5d 20 6d 75 73 74 0a   function] must.
8740: 2a 2a 20 72 65 74 75 72 6e 20 65 69 74 68 65 72  ** return either
8750: 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 72 20   [SQLITE_OK] or 
8760: 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 74 77 6f  one of these two
8770: 20 63 6f 6e 73 74 61 6e 74 73 20 69 6e 20 6f 72   constants in or
8780: 64 65 72 0a 2a 2a 20 74 6f 20 73 69 67 6e 61 6c  der.** to signal
8790: 20 53 51 4c 69 74 65 20 77 68 65 74 68 65 72 20   SQLite whether 
87a0: 6f 72 20 6e 6f 74 20 74 68 65 20 61 63 74 69 6f  or not the actio
87b0: 6e 20 69 73 20 70 65 72 6d 69 74 74 65 64 2e 20  n is permitted. 
87c0: 20 53 65 65 20 74 68 65 0a 2a 2a 20 5b 73 71 6c   See the.** [sql
87d0: 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69  ite3_set_authori
87e0: 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 7a 65 72  zer | authorizer
87f0: 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 5d 20   documentation] 
8800: 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a  for additional.*
8810: 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a  * information..*
8820: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
8830: 5f 44 45 4e 59 20 20 20 31 20 20 20 2f 2a 20 41  _DENY   1   /* A
8840: 62 6f 72 74 20 74 68 65 20 53 51 4c 20 73 74 61  bort the SQL sta
8850: 74 65 6d 65 6e 74 20 77 69 74 68 20 61 6e 20 65  tement with an e
8860: 72 72 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20  rror */.#define 
8870: 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 32 20  SQLITE_IGNORE 2 
8880: 20 20 2f 2a 20 44 6f 6e 27 74 20 61 6c 6c 6f 77    /* Don't allow
8890: 20 61 63 63 65 73 73 2c 20 62 75 74 20 64 6f 6e   access, but don
88a0: 27 74 20 67 65 6e 65 72 61 74 65 20 61 6e 20 65  't generate an e
88b0: 72 72 6f 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43  rror */../*.** C
88c0: 41 50 49 33 52 45 46 3a 20 41 75 74 68 6f 72 69  API3REF: Authori
88d0: 7a 65 72 20 41 63 74 69 6f 6e 20 43 6f 64 65 73  zer Action Codes
88e0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69  .**.** The [sqli
88f0: 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a  te3_set_authoriz
8900: 65 72 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20  er()] interface 
8910: 72 65 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c  registers a call
8920: 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a  back function.**
8930: 20 74 68 61 74 20 69 73 20 69 6e 76 6f 6b 65 64   that is invoked
8940: 20 74 6f 20 61 75 74 68 6f 72 69 7a 65 72 20 63   to authorizer c
8950: 65 72 74 61 69 6e 20 53 51 4c 20 73 74 61 74 65  ertain SQL state
8960: 6d 65 6e 74 20 61 63 74 69 6f 6e 73 2e 20 20 54  ment actions.  T
8970: 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 61 72  he.** second par
8980: 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 63 61  ameter to the ca
8990: 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20 69 6e 74  llback is an int
89a0: 65 67 65 72 20 63 6f 64 65 20 74 68 61 74 20 73  eger code that s
89b0: 70 65 63 69 66 69 65 73 0a 2a 2a 20 77 68 61 74  pecifies.** what
89c0: 20 61 63 74 69 6f 6e 20 69 73 20 62 65 69 6e 67   action is being
89d0: 20 61 75 74 68 6f 72 69 7a 65 64 2e 20 20 54 68   authorized.  Th
89e0: 65 73 65 20 61 72 65 20 74 68 65 20 69 6e 74 65  ese are the inte
89f0: 67 65 72 20 61 63 74 69 6f 6e 20 63 6f 64 65 73  ger action codes
8a00: 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 61 75 74   that.** the aut
8a10: 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b  horizer callback
8a20: 20 6d 61 79 20 62 65 20 70 61 73 73 65 64 2e 0a   may be passed..
8a30: 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 61 63 74 69  **.** These acti
8a40: 6f 6e 20 63 6f 64 65 20 76 61 6c 75 65 73 20 73  on code values s
8a50: 69 67 6e 69 66 79 20 77 68 61 74 20 6b 69 6e 64  ignify what kind
8a60: 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 20 69 73   of operation is
8a70: 20 74 6f 20 62 65 20 0a 2a 2a 20 61 75 74 68 6f   to be .** autho
8a80: 72 69 7a 65 64 2e 20 20 54 68 65 20 33 72 64 20  rized.  The 3rd 
8a90: 61 6e 64 20 34 74 68 20 70 61 72 61 6d 65 74 65  and 4th paramete
8aa0: 72 73 20 74 6f 20 74 68 65 20 61 75 74 68 6f 72  rs to the author
8ab0: 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b  ization callback
8ac0: 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c  .** function wil
8ad0: 6c 20 62 65 20 70 61 72 61 6d 65 74 65 72 73 20  l be parameters 
8ae0: 6f 72 20 4e 55 4c 4c 20 64 65 70 65 6e 64 69 6e  or NULL dependin
8af0: 67 20 6f 6e 20 77 68 69 63 68 20 6f 66 20 74 68  g on which of th
8b00: 65 73 65 0a 2a 2a 20 63 6f 64 65 73 20 69 73 20  ese.** codes is 
8b10: 75 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f  used as the seco
8b20: 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20 20 54  nd parameter.  T
8b30: 68 65 20 35 74 68 20 70 61 72 61 6d 65 74 65 72  he 5th parameter
8b40: 20 74 6f 20 74 68 65 0a 2a 2a 20 61 75 74 68 6f   to the.** autho
8b50: 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69  rizer callback i
8b60: 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  s the name of th
8b70: 65 20 64 61 74 61 62 61 73 65 20 28 22 6d 61 69  e database ("mai
8b80: 6e 22 2c 20 22 74 65 6d 70 22 2c 20 0a 2a 2a 20  n", "temp", .** 
8b90: 65 74 63 2e 29 20 69 66 20 61 70 70 6c 69 63 61  etc.) if applica
8ba0: 62 6c 65 2e 20 20 54 68 65 20 36 74 68 20 70 61  ble.  The 6th pa
8bb0: 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 61  rameter to the a
8bc0: 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61  uthorizer callba
8bd0: 63 6b 0a 2a 2a 20 69 73 20 74 68 65 20 6e 61 6d  ck.** is the nam
8be0: 65 20 6f 66 20 74 68 65 20 69 6e 6e 65 72 2d 6d  e of the inner-m
8bf0: 6f 73 74 20 74 72 69 67 67 65 72 20 6f 72 20 76  ost trigger or v
8c00: 69 65 77 20 74 68 61 74 20 69 73 20 72 65 73 70  iew that is resp
8c10: 6f 6e 73 69 62 6c 65 20 66 6f 72 0a 2a 2a 20 74  onsible for.** t
8c20: 68 65 20 61 63 63 65 73 73 20 61 74 74 65 6d 70  he access attemp
8c30: 74 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 69  t or NULL if thi
8c40: 73 20 61 63 63 65 73 73 20 61 74 74 65 6d 70 74  s access attempt
8c50: 20 69 73 20 64 69 72 65 63 74 6c 79 20 66 72 6f   is directly fro
8c60: 6d 20 0a 2a 2a 20 74 6f 70 2d 6c 65 76 65 6c 20  m .** top-level 
8c70: 53 51 4c 20 63 6f 64 65 2e 0a 2a 2f 0a 2f 2a 2a  SQL code..*/./**
8c80: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c90: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8ca0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 33 72 64 20 2a 2a  ********* 3rd **
8cb0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 34 74 68 20 2a  ********** 4th *
8cc0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 64 65 66  **********/.#def
8cd0: 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54  ine SQLITE_CREAT
8ce0: 45 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20 20  E_INDEX         
8cf0: 20 31 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61   1   /* Index Na
8d00: 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e 61  me      Table Na
8d10: 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69  me      */.#defi
8d20: 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45  ne SQLITE_CREATE
8d30: 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20 20 20  _TABLE          
8d40: 32 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d  2   /* Table Nam
8d50: 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20  e      NULL     
8d60: 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e         */.#defin
8d70: 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f  e SQLITE_CREATE_
8d80: 54 45 4d 50 5f 49 4e 44 45 58 20 20 20 20 20 33  TEMP_INDEX     3
8d90: 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 65     /* Index Name
8da0: 20 20 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65        Table Name
8db0: 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
8dc0: 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54   SQLITE_CREATE_T
8dd0: 45 4d 50 5f 54 41 42 4c 45 20 20 20 20 20 34 20  EMP_TABLE     4 
8de0: 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20    /* Table Name 
8df0: 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20       NULL       
8e00: 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
8e10: 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45  SQLITE_CREATE_TE
8e20: 4d 50 5f 54 52 49 47 47 45 52 20 20 20 35 20 20  MP_TRIGGER   5  
8e30: 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d 65   /* Trigger Name
8e40: 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20      Table Name  
8e50: 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53      */.#define S
8e60: 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d  QLITE_CREATE_TEM
8e70: 50 5f 56 49 45 57 20 20 20 20 20 20 36 20 20 20  P_VIEW      6   
8e80: 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20 20  /* View Name    
8e90: 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20     NULL         
8ea0: 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
8eb0: 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 52 49 47  LITE_CREATE_TRIG
8ec0: 47 45 52 20 20 20 20 20 20 20 20 37 20 20 20 2f  GER        7   /
8ed0: 2a 20 54 72 69 67 67 65 72 20 4e 61 6d 65 20 20  * Trigger Name  
8ee0: 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20    Table Name    
8ef0: 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
8f00: 49 54 45 5f 43 52 45 41 54 45 5f 56 49 45 57 20  ITE_CREATE_VIEW 
8f10: 20 20 20 20 20 20 20 20 20 20 38 20 20 20 2f 2a            8   /*
8f20: 20 56 69 65 77 20 4e 61 6d 65 20 20 20 20 20 20   View Name      
8f30: 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20   NULL           
8f40: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
8f50: 54 45 5f 44 45 4c 45 54 45 20 20 20 20 20 20 20  TE_DELETE       
8f60: 20 20 20 20 20 20 20 20 20 39 20 20 20 2f 2a 20           9   /* 
8f70: 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20  Table Name      
8f80: 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20  NULL            
8f90: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
8fa0: 45 5f 44 52 4f 50 5f 49 4e 44 45 58 20 20 20 20  E_DROP_INDEX    
8fb0: 20 20 20 20 20 20 20 31 30 20 20 20 2f 2a 20 49         10   /* I
8fc0: 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20 20 54  ndex Name      T
8fd0: 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a  able Name      *
8fe0: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
8ff0: 5f 44 52 4f 50 5f 54 41 42 4c 45 20 20 20 20 20  _DROP_TABLE     
9000: 20 20 20 20 20 20 31 31 20 20 20 2f 2a 20 54 61        11   /* Ta
9010: 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55  ble Name      NU
9020: 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f  LL            */
9030: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
9040: 44 52 4f 50 5f 54 45 4d 50 5f 49 4e 44 45 58 20  DROP_TEMP_INDEX 
9050: 20 20 20 20 20 31 32 20 20 20 2f 2a 20 49 6e 64       12   /* Ind
9060: 65 78 20 4e 61 6d 65 20 20 20 20 20 20 54 61 62  ex Name      Tab
9070: 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a  le Name      */.
9080: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44  #define SQLITE_D
9090: 52 4f 50 5f 54 45 4d 50 5f 54 41 42 4c 45 20 20  ROP_TEMP_TABLE  
90a0: 20 20 20 20 31 33 20 20 20 2f 2a 20 54 61 62 6c      13   /* Tabl
90b0: 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c  e Name      NULL
90c0: 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23              */.#
90d0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52  define SQLITE_DR
90e0: 4f 50 5f 54 45 4d 50 5f 54 52 49 47 47 45 52 20  OP_TEMP_TRIGGER 
90f0: 20 20 20 31 34 20 20 20 2f 2a 20 54 72 69 67 67     14   /* Trigg
9100: 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62 6c 65  er Name    Table
9110: 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64   Name      */.#d
9120: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f  efine SQLITE_DRO
9130: 50 5f 54 45 4d 50 5f 56 49 45 57 20 20 20 20 20  P_TEMP_VIEW     
9140: 20 20 31 35 20 20 20 2f 2a 20 56 69 65 77 20 4e    15   /* View N
9150: 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c 20 20  ame       NULL  
9160: 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65            */.#de
9170: 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50  fine SQLITE_DROP
9180: 5f 54 52 49 47 47 45 52 20 20 20 20 20 20 20 20  _TRIGGER        
9190: 20 31 36 20 20 20 2f 2a 20 54 72 69 67 67 65 72   16   /* Trigger
91a0: 20 4e 61 6d 65 20 20 20 20 54 61 62 6c 65 20 4e   Name    Table N
91b0: 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66  ame      */.#def
91c0: 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f  ine SQLITE_DROP_
91d0: 56 49 45 57 20 20 20 20 20 20 20 20 20 20 20 20  VIEW            
91e0: 31 37 20 20 20 2f 2a 20 56 69 65 77 20 4e 61 6d  17   /* View Nam
91f0: 65 20 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20  e       NULL    
9200: 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69          */.#defi
9210: 6e 65 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54  ne SQLITE_INSERT
9220: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
9230: 38 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d  8   /* Table Nam
9240: 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20  e      NULL     
9250: 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e         */.#defin
9260: 65 20 53 51 4c 49 54 45 5f 50 52 41 47 4d 41 20  e SQLITE_PRAGMA 
9270: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 39                19
9280: 20 20 20 2f 2a 20 50 72 61 67 6d 61 20 4e 61 6d     /* Pragma Nam
9290: 65 20 20 20 20 20 31 73 74 20 61 72 67 20 6f 72  e     1st arg or
92a0: 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65   NULL */.#define
92b0: 20 53 51 4c 49 54 45 5f 52 45 41 44 20 20 20 20   SQLITE_READ    
92c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 32 30 20               20 
92d0: 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20    /* Table Name 
92e0: 20 20 20 20 20 43 6f 6c 75 6d 6e 20 4e 61 6d 65       Column Name
92f0: 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
9300: 53 51 4c 49 54 45 5f 53 45 4c 45 43 54 20 20 20  SQLITE_SELECT   
9310: 20 20 20 20 20 20 20 20 20 20 20 20 32 31 20 20              21  
9320: 20 2f 2a 20 4e 55 4c 4c 20 20 20 20 20 20 20 20   /* NULL        
9330: 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20      NULL        
9340: 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53      */.#define S
9350: 51 4c 49 54 45 5f 54 52 41 4e 53 41 43 54 49 4f  QLITE_TRANSACTIO
9360: 4e 20 20 20 20 20 20 20 20 20 20 32 32 20 20 20  N          22   
9370: 2f 2a 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20  /* NULL         
9380: 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20     NULL         
9390: 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
93a0: 4c 49 54 45 5f 55 50 44 41 54 45 20 20 20 20 20  LITE_UPDATE     
93b0: 20 20 20 20 20 20 20 20 20 20 32 33 20 20 20 2f            23   /
93c0: 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20  * Table Name    
93d0: 20 20 43 6f 6c 75 6d 6e 20 4e 61 6d 65 20 20 20    Column Name   
93e0: 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
93f0: 49 54 45 5f 41 54 54 41 43 48 20 20 20 20 20 20  ITE_ATTACH      
9400: 20 20 20 20 20 20 20 20 20 32 34 20 20 20 2f 2a           24   /*
9410: 20 46 69 6c 65 6e 61 6d 65 20 20 20 20 20 20 20   Filename       
9420: 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20   NULL           
9430: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
9440: 54 45 5f 44 45 54 41 43 48 20 20 20 20 20 20 20  TE_DETACH       
9450: 20 20 20 20 20 20 20 20 32 35 20 20 20 2f 2a 20          25   /* 
9460: 44 61 74 61 62 61 73 65 20 4e 61 6d 65 20 20 20  Database Name   
9470: 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20  NULL            
9480: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
9490: 45 5f 41 4c 54 45 52 5f 54 41 42 4c 45 20 20 20  E_ALTER_TABLE   
94a0: 20 20 20 20 20 20 20 32 36 20 20 20 2f 2a 20 44         26   /* D
94b0: 61 74 61 62 61 73 65 20 4e 61 6d 65 20 20 20 54  atabase Name   T
94c0: 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a  able Name      *
94d0: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
94e0: 5f 52 45 49 4e 44 45 58 20 20 20 20 20 20 20 20  _REINDEX        
94f0: 20 20 20 20 20 20 32 37 20 20 20 2f 2a 20 49 6e        27   /* In
9500: 64 65 78 20 4e 61 6d 65 20 20 20 20 20 20 4e 55  dex Name      NU
9510: 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f  LL            */
9520: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
9530: 41 4e 41 4c 59 5a 45 20 20 20 20 20 20 20 20 20  ANALYZE         
9540: 20 20 20 20 20 32 38 20 20 20 2f 2a 20 54 61 62       28   /* Tab
9550: 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c  le Name      NUL
9560: 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  L            */.
9570: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
9580: 52 45 41 54 45 5f 56 54 41 42 4c 45 20 20 20 20  REATE_VTABLE    
9590: 20 20 20 20 32 39 20 20 20 2f 2a 20 54 61 62 6c      29   /* Tabl
95a0: 65 20 4e 61 6d 65 20 20 20 20 20 20 4d 6f 64 75  e Name      Modu
95b0: 6c 65 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a 23  le Name     */.#
95c0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52  define SQLITE_DR
95d0: 4f 50 5f 56 54 41 42 4c 45 20 20 20 20 20 20 20  OP_VTABLE       
95e0: 20 20 20 33 30 20 20 20 2f 2a 20 54 61 62 6c 65     30   /* Table
95f0: 20 4e 61 6d 65 20 20 20 20 20 20 4d 6f 64 75 6c   Name      Modul
9600: 65 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a 23 64  e Name     */.#d
9610: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e  efine SQLITE_FUN
9620: 43 54 49 4f 4e 20 20 20 20 20 20 20 20 20 20 20  CTION           
9630: 20 20 33 31 20 20 20 2f 2a 20 46 75 6e 63 74 69    31   /* Functi
9640: 6f 6e 20 4e 61 6d 65 20 20 20 4e 55 4c 4c 20 20  on Name   NULL  
9650: 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65            */.#de
9660: 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 50 59  fine SQLITE_COPY
9670: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9680: 20 20 30 20 20 20 2f 2a 20 4e 6f 20 6c 6f 6e 67    0   /* No long
9690: 65 72 20 75 73 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a  er used */../*.*
96a0: 2a 20 43 41 50 49 33 52 45 46 3a 20 54 72 61 63  * CAPI3REF: Trac
96b0: 69 6e 67 20 41 6e 64 20 50 72 6f 66 69 6c 69 6e  ing And Profilin
96c0: 67 20 46 75 6e 63 74 69 6f 6e 73 0a 2a 2a 0a 2a  g Functions.**.*
96d0: 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
96e0: 20 72 65 67 69 73 74 65 72 20 63 61 6c 6c 62 61   register callba
96f0: 63 6b 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61  ck functions tha
9700: 74 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f  t can be used fo
9710: 72 0a 2a 2a 20 74 72 61 63 69 6e 67 20 61 6e 64  r.** tracing and
9720: 20 70 72 6f 66 69 6c 69 6e 67 20 74 68 65 20 65   profiling the e
9730: 78 65 63 75 74 69 6f 6e 20 6f 66 20 53 51 4c 20  xecution of SQL 
9740: 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20 54  statements..** T
9750: 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63  he callback func
9760: 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20  tion registered 
9770: 62 79 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65  by sqlite3_trace
9780: 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 0a 2a 2a  () is invoked.**
9790: 20 61 74 20 74 68 65 20 66 69 72 73 74 20 5b 73   at the first [s
97a0: 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 66  qlite3_step()] f
97b0: 6f 72 20 74 68 65 20 65 76 61 6c 75 61 74 69 6f  or the evaluatio
97c0: 6e 20 6f 66 20 61 6e 20 53 51 4c 20 73 74 61 74  n of an SQL stat
97d0: 65 6d 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 63 61  ement..** The ca
97e0: 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20  llback function 
97f0: 72 65 67 69 73 74 65 72 65 64 20 62 79 20 73 71  registered by sq
9800: 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65 28 29 20  lite3_profile() 
9810: 69 73 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 61 73  is invoked.** as
9820: 20 65 61 63 68 20 53 51 4c 20 73 74 61 74 65 6d   each SQL statem
9830: 65 6e 74 20 66 69 6e 69 73 68 65 73 20 61 6e 64  ent finishes and
9840: 20 69 6e 63 6c 75 64 65 73 0a 2a 2a 20 69 6e 66   includes.** inf
9850: 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20 68 6f 77 20  ormation on how 
9860: 6c 6f 6e 67 20 74 68 61 74 20 73 74 61 74 65 6d  long that statem
9870: 65 6e 74 20 72 61 6e 2e 0a 2a 2a 0a 2a 2a 20 54  ent ran..**.** T
9880: 68 65 20 73 71 6c 69 74 65 33 5f 70 72 6f 66 69  he sqlite3_profi
9890: 6c 65 28 29 20 41 50 49 20 69 73 20 63 75 72 72  le() API is curr
98a0: 65 6e 74 6c 79 20 63 6f 6e 73 69 64 65 72 65 64  ently considered
98b0: 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 61 6e   experimental an
98c0: 64 0a 2a 2a 20 69 73 20 73 75 62 6a 65 63 74 20  d.** is subject 
98d0: 74 6f 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a 53 51  to change..*/.SQ
98e0: 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73  LITE_API void *s
98f0: 71 6c 69 74 65 33 5f 74 72 61 63 65 28 73 71 6c  qlite3_trace(sql
9900: 69 74 65 33 2a 2c 20 76 6f 69 64 28 2a 78 54 72  ite3*, void(*xTr
9910: 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74  ace)(void*,const
9920: 20 63 68 61 72 2a 29 2c 20 76 6f 69 64 2a 29 3b   char*), void*);
9930: 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
9940: 20 2a 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c   *sqlite3_profil
9950: 65 28 73 71 6c 69 74 65 33 2a 2c 0a 20 20 20 76  e(sqlite3*,.   v
9960: 6f 69 64 28 2a 78 50 72 6f 66 69 6c 65 29 28 76  oid(*xProfile)(v
9970: 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  oid*,const char*
9980: 2c 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 2c  ,sqlite_uint64),
9990: 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20   void*);../*.** 
99a0: 43 41 50 49 33 52 45 46 3a 20 51 75 65 72 79 20  CAPI3REF: Query 
99b0: 50 72 6f 67 72 65 73 73 20 43 61 6c 6c 62 61 63  Progress Callbac
99c0: 6b 73 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  ks.**.** This ro
99d0: 75 74 69 6e 65 20 63 6f 6e 66 69 67 75 72 65 73  utine configures
99e0: 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63   a callback func
99f0: 74 69 6f 6e 20 2d 20 74 68 65 20 70 72 6f 67 72  tion - the progr
9a00: 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 2d 20 74  ess callback - t
9a10: 68 61 74 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65  hat.** is invoke
9a20: 64 20 70 65 72 69 6f 64 69 63 61 6c 6c 79 20 64  d periodically d
9a30: 75 72 69 6e 67 20 6c 6f 6e 67 20 72 75 6e 6e 69  uring long runni
9a40: 6e 67 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c  ng calls to [sql
9a50: 69 74 65 33 5f 65 78 65 63 28 29 5d 2c 0a 2a 2a  ite3_exec()],.**
9a60: 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   [sqlite3_step()
9a70: 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 67  ] and [sqlite3_g
9a80: 65 74 5f 74 61 62 6c 65 28 29 5d 2e 20 20 41 6e  et_table()].  An
9a90: 20 65 78 61 6d 70 6c 65 20 75 73 65 20 66 6f 72   example use for
9aa0: 20 74 68 69 73 20 0a 2a 2a 20 69 6e 74 65 72 66   this .** interf
9ab0: 61 63 65 20 69 73 20 74 6f 20 6b 65 65 70 20 61  ace is to keep a
9ac0: 20 47 55 49 20 75 70 64 61 74 65 64 20 64 75 72   GUI updated dur
9ad0: 69 6e 67 20 61 20 6c 61 72 67 65 20 71 75 65 72  ing a large quer
9ae0: 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 6f  y..**.** The pro
9af0: 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 69  gress callback i
9b00: 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66  s invoked once f
9b10: 6f 72 20 65 76 65 72 79 20 4e 20 76 69 72 74 75  or every N virtu
9b20: 61 6c 20 6d 61 63 68 69 6e 65 20 6f 70 63 6f 64  al machine opcod
9b30: 65 73 2c 0a 2a 2a 20 77 68 65 72 65 20 4e 20 69  es,.** where N i
9b40: 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  s the second arg
9b50: 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75  ument to this fu
9b60: 6e 63 74 69 6f 6e 2e 20 54 68 65 20 70 72 6f 67  nction. The prog
9b70: 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a  ress callback.**
9b80: 20 69 74 73 65 6c 66 20 69 73 20 69 64 65 6e 74   itself is ident
9b90: 69 66 69 65 64 20 62 79 20 74 68 65 20 74 68 69  ified by the thi
9ba0: 72 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  rd argument to t
9bb0: 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68  his function. Th
9bc0: 65 20 66 6f 75 72 74 68 0a 2a 2a 20 61 72 67 75  e fourth.** argu
9bd0: 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e  ment to this fun
9be0: 63 74 69 6f 6e 20 69 73 20 61 20 76 6f 69 64 20  ction is a void 
9bf0: 70 6f 69 6e 74 65 72 20 70 61 73 73 65 64 20 74  pointer passed t
9c00: 6f 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 63  o the progress c
9c10: 61 6c 6c 62 61 63 6b 0a 2a 2a 20 66 75 6e 63 74  allback.** funct
9c20: 69 6f 6e 20 65 61 63 68 20 74 69 6d 65 20 69 74  ion each time it
9c30: 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a   is invoked..**.
9c40: 2a 2a 20 49 66 20 61 20 63 61 6c 6c 20 74 6f 20  ** If a call to 
9c50: 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d  [sqlite3_exec()]
9c60: 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  , [sqlite3_step(
9c70: 29 5d 2c 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  )], or [sqlite3_
9c80: 67 65 74 5f 74 61 62 6c 65 28 29 5d 0a 2a 2a 20  get_table()].** 
9c90: 72 65 73 75 6c 74 73 20 69 6e 20 66 65 77 65 72  results in fewer
9ca0: 20 74 68 61 6e 20 4e 20 6f 70 63 6f 64 65 73 20   than N opcodes 
9cb0: 62 65 69 6e 67 20 65 78 65 63 75 74 65 64 2c 20  being executed, 
9cc0: 74 68 65 6e 20 74 68 65 20 70 72 6f 67 72 65 73  then the progres
9cd0: 73 20 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 69  s .** callback i
9ce0: 73 20 6e 65 76 65 72 20 69 6e 76 6f 6b 65 64 2e  s never invoked.
9cf0: 0a 2a 2a 20 0a 2a 2a 20 4f 6e 6c 79 20 61 20 73  .** .** Only a s
9d00: 69 6e 67 6c 65 20 70 72 6f 67 72 65 73 73 20 63  ingle progress c
9d10: 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
9d20: 20 6d 61 79 20 62 65 20 72 65 67 69 73 74 65 72   may be register
9d30: 65 64 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 6f  ed for each.** o
9d40: 70 65 6e 20 64 61 74 61 62 61 73 65 20 63 6f 6e  pen database con
9d50: 6e 65 63 74 69 6f 6e 2e 20 20 45 76 65 72 79 20  nection.  Every 
9d60: 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f  call to sqlite3_
9d70: 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65 72  progress_handler
9d80: 28 29 0a 2a 2a 20 6f 76 65 72 77 72 69 74 65 73  ().** overwrites
9d90: 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20   the results of 
9da0: 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 61 6c  the previous cal
9db0: 6c 2e 0a 2a 2a 20 54 6f 20 72 65 6d 6f 76 65 20  l..** To remove 
9dc0: 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c  the progress cal
9dd0: 6c 62 61 63 6b 20 61 6c 74 6f 67 65 74 68 65 72  lback altogether
9de0: 2c 20 70 61 73 73 20 4e 55 4c 4c 20 61 73 20 74  , pass NULL as t
9df0: 68 65 20 74 68 69 72 64 0a 2a 2a 20 61 72 67 75  he third.** argu
9e00: 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e  ment to this fun
9e10: 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ction..**.** If 
9e20: 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c  the progress cal
9e30: 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 61 20  lback returns a 
9e40: 72 65 73 75 6c 74 20 6f 74 68 65 72 20 74 68 61  result other tha
9e50: 6e 20 30 2c 20 74 68 65 6e 20 74 68 65 20 63 75  n 0, then the cu
9e60: 72 72 65 6e 74 20 0a 2a 2a 20 71 75 65 72 79 20  rrent .** query 
9e70: 69 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74  is immediately t
9e80: 65 72 6d 69 6e 61 74 65 64 20 61 6e 64 20 61 6e  erminated and an
9e90: 79 20 64 61 74 61 62 61 73 65 20 63 68 61 6e 67  y database chang
9ea0: 65 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a  es rolled back..
9eb0: 2a 2a 20 54 68 65 20 63 6f 6e 74 61 69 6e 69 6e  ** The containin
9ec0: 67 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28  g [sqlite3_exec(
9ed0: 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65  )], [sqlite3_ste
9ee0: 70 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 71 6c  p()], or.** [sql
9ef0: 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29  ite3_get_table()
9f00: 5d 20 63 61 6c 6c 20 72 65 74 75 72 6e 73 20 53  ] call returns S
9f10: 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 2e  QLITE_INTERRUPT.
9f20: 20 20 20 54 68 69 73 20 66 65 61 74 75 72 65 0a     This feature.
9f30: 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 2c 20  ** can be used, 
9f40: 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 6f 20  for example, to 
9f50: 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 22 43  implement the "C
9f60: 61 6e 63 65 6c 22 20 62 75 74 74 6f 6e 20 6f 6e  ancel" button on
9f70: 20 61 0a 2a 2a 20 70 72 6f 67 72 65 73 73 20 64   a.** progress d
9f80: 69 61 6c 6f 67 20 62 6f 78 20 69 6e 20 61 20 47  ialog box in a G
9f90: 55 49 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69  UI..*/.void sqli
9fa0: 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e  te3_progress_han
9fb0: 64 6c 65 72 28 73 71 6c 69 74 65 33 2a 2c 20 69  dler(sqlite3*, i
9fc0: 6e 74 2c 20 69 6e 74 28 2a 29 28 76 6f 69 64 2a  nt, int(*)(void*
9fd0: 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a  ), void*);../*.*
9fe0: 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 70 65 6e  * CAPI3REF: Open
9ff0: 69 6e 67 20 41 20 4e 65 77 20 44 61 74 61 62 61  ing A New Databa
a000: 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a  se Connection.**
a010: 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 73 71 6c  .** Open the sql
a020: 69 74 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  ite database fil
a030: 65 20 22 66 69 6c 65 6e 61 6d 65 22 2e 20 20 54  e "filename".  T
a040: 68 65 20 22 66 69 6c 65 6e 61 6d 65 22 20 69 73  he "filename" is
a050: 20 55 54 46 2d 38 0a 2a 2a 20 65 6e 63 6f 64 65   UTF-8.** encode
a060: 64 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70  d for sqlite3_op
a070: 65 6e 28 29 20 61 6e 64 20 55 54 46 2d 31 36 20  en() and UTF-16 
a080: 65 6e 63 6f 64 65 64 20 69 6e 20 74 68 65 20 6e  encoded in the n
a090: 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72  ative byte order
a0a0: 0a 2a 2a 20 66 6f 72 20 73 71 6c 69 74 65 33 5f  .** for sqlite3_
a0b0: 6f 70 65 6e 31 36 28 29 2e 20 20 41 6e 20 5b 73  open16().  An [s
a0c0: 71 6c 69 74 65 33 2a 5d 20 68 61 6e 64 6c 65 20  qlite3*] handle 
a0d0: 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20 2a  is returned in *
a0e0: 70 70 44 62 2c 20 65 76 65 6e 0a 2a 2a 20 69 66  ppDb, even.** if
a0f0: 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
a100: 2e 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73  . If the databas
a110: 65 20 69 73 20 6f 70 65 6e 65 64 20 28 6f 72 20  e is opened (or 
a120: 63 72 65 61 74 65 64 29 20 73 75 63 63 65 73 73  created) success
a130: 66 75 6c 6c 79 2c 0a 2a 2a 20 74 68 65 6e 20 53  fully,.** then S
a140: 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
a150: 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 20  rned. Otherwise 
a160: 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  an error code is
a170: 20 72 65 74 75 72 6e 65 64 2e 20 54 68 65 0a 2a   returned. The.*
a180: 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67  * sqlite3_errmsg
a190: 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 65 72  () or sqlite3_er
a1a0: 72 6d 73 67 31 36 28 29 20 20 72 6f 75 74 69 6e  rmsg16()  routin
a1b0: 65 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 74  es can be used t
a1c0: 6f 20 6f 62 74 61 69 6e 0a 2a 2a 20 61 6e 20 45  o obtain.** an E
a1d0: 6e 67 6c 69 73 68 20 6c 61 6e 67 75 61 67 65 20  nglish language 
a1e0: 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74  description of t
a1f0: 68 65 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20  he error..**.** 
a200: 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  If the database 
a210: 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 65 78  file does not ex
a220: 69 73 74 2c 20 74 68 65 6e 20 61 20 6e 65 77 20  ist, then a new 
a230: 64 61 74 61 62 61 73 65 20 77 69 6c 6c 20 62 65  database will be
a240: 20 63 72 65 61 74 65 64 0a 2a 2a 20 61 73 20 6e   created.** as n
a250: 65 65 64 65 64 2e 20 20 54 68 65 20 64 65 66 61  eeded.  The defa
a260: 75 6c 74 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72  ult encoding for
a270: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77 69   the database wi
a280: 6c 6c 20 62 65 20 55 54 46 2d 38 20 69 66 0a 2a  ll be UTF-8 if.*
a290: 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29  * sqlite3_open()
a2a0: 20 69 73 20 63 61 6c 6c 65 64 20 61 6e 64 20 55   is called and U
a2b0: 54 46 2d 31 36 20 69 66 20 73 71 6c 69 74 65 33  TF-16 if sqlite3
a2c0: 5f 6f 70 65 6e 31 36 20 69 73 20 75 73 65 64 2e  _open16 is used.
a2d0: 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65 72 20 6f  .**.** Whether o
a2e0: 72 20 6e 6f 74 20 61 6e 20 65 72 72 6f 72 20 6f  r not an error o
a2f0: 63 63 75 72 73 20 77 68 65 6e 20 69 74 20 69 73  ccurs when it is
a300: 20 6f 70 65 6e 65 64 2c 20 72 65 73 6f 75 72 63   opened, resourc
a310: 65 73 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a  es associated.**
a320: 20 77 69 74 68 20 74 68 65 20 5b 73 71 6c 69 74   with the [sqlit
a330: 65 33 2a 5d 20 68 61 6e 64 6c 65 20 73 68 6f 75  e3*] handle shou
a340: 6c 64 20 62 65 20 72 65 6c 65 61 73 65 64 20 62  ld be released b
a350: 79 20 70 61 73 73 69 6e 67 20 69 74 20 74 6f 0a  y passing it to.
a360: 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65  ** sqlite3_close
a370: 28 29 20 77 68 65 6e 20 69 74 20 69 73 20 6e 6f  () when it is no
a380: 20 6c 6f 6e 67 65 72 20 72 65 71 75 69 72 65 64   longer required
a390: 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 6f 20  ..**.** Note to 
a3a0: 77 69 6e 64 6f 77 73 20 75 73 65 72 73 3a 20 20  windows users:  
a3b0: 54 68 65 20 65 6e 63 6f 64 69 6e 67 20 75 73 65  The encoding use
a3c0: 64 20 66 6f 72 20 74 68 65 20 66 69 6c 65 6e 61  d for the filena
a3d0: 6d 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 6f  me argument.** o
a3e0: 66 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29  f sqlite3_open()
a3f0: 20 6d 75 73 74 20 62 65 20 55 54 46 2d 38 2c 20   must be UTF-8, 
a400: 6e 6f 74 20 77 68 61 74 65 76 65 72 20 63 6f 64  not whatever cod
a410: 65 70 61 67 65 20 69 73 20 63 75 72 72 65 6e 74  epage is current
a420: 6c 79 0a 2a 2a 20 64 65 66 69 6e 65 64 2e 20 20  ly.** defined.  
a430: 46 69 6c 65 6e 61 6d 65 73 20 63 6f 6e 74 61 69  Filenames contai
a440: 6e 69 6e 67 20 69 6e 74 65 72 6e 61 74 69 6f 6e  ning internation
a450: 61 6c 20 63 68 61 72 61 63 74 65 72 73 20 6d 75  al characters mu
a460: 73 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 0a  st be converted.
a470: 2a 2a 20 74 6f 20 55 54 46 2d 38 20 70 72 69 6f  ** to UTF-8 prio
a480: 72 20 74 6f 20 70 61 73 73 69 6e 67 20 74 68 65  r to passing the
a490: 6d 20 69 6e 74 6f 20 73 71 6c 69 74 65 33 5f 6f  m into sqlite3_o
a4a0: 70 65 6e 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  pen()..*/.SQLITE
a4b0: 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
a4c0: 5f 6f 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63  _open(.  const c
a4d0: 68 61 72 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20  har *filename,  
a4e0: 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c   /* Database fil
a4f0: 65 6e 61 6d 65 20 28 55 54 46 2d 38 29 20 2a 2f  ename (UTF-8) */
a500: 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44  .  sqlite3 **ppD
a510: 62 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55  b          /* OU
a520: 54 3a 20 53 51 4c 69 74 65 20 64 62 20 68 61 6e  T: SQLite db han
a530: 64 6c 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45  dle */.);.SQLITE
a540: 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
a550: 5f 6f 70 65 6e 31 36 28 0a 20 20 63 6f 6e 73 74  _open16(.  const
a560: 20 76 6f 69 64 20 2a 66 69 6c 65 6e 61 6d 65 2c   void *filename,
a570: 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66     /* Database f
a580: 69 6c 65 6e 61 6d 65 20 28 55 54 46 2d 31 36 29  ilename (UTF-16)
a590: 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a   */.  sqlite3 **
a5a0: 70 70 44 62 20 20 20 20 20 20 20 20 20 20 2f 2a  ppDb          /*
a5b0: 20 4f 55 54 3a 20 53 51 4c 69 74 65 20 64 62 20   OUT: SQLite db 
a5c0: 68 61 6e 64 6c 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a  handle */.);../*
a5d0: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 72  .** CAPI3REF: Er
a5e0: 72 6f 72 20 43 6f 64 65 73 20 41 6e 64 20 4d 65  ror Codes And Me
a5f0: 73 73 61 67 65 73 0a 2a 2a 0a 2a 2a 20 54 68 65  ssages.**.** The
a600: 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65   sqlite3_errcode
a610: 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74  () interface ret
a620: 75 72 6e 73 20 74 68 65 20 6e 75 6d 65 72 69 63  urns the numeric
a630: 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 4b 20 7c  .** [SQLITE_OK |
a640: 20 72 65 73 75 6c 74 20 63 6f 64 65 5d 20 6f 72   result code] or
a650: 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52   [SQLITE_IOERR_R
a660: 45 41 44 20 7c 20 65 78 74 65 6e 64 65 64 20 72  EAD | extended r
a670: 65 73 75 6c 74 20 63 6f 64 65 5d 0a 2a 2a 20 66  esult code].** f
a680: 6f 72 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  or the most rece
a690: 6e 74 20 66 61 69 6c 65 64 20 73 71 6c 69 74 65  nt failed sqlite
a6a0: 33 5f 2a 20 41 50 49 20 63 61 6c 6c 20 61 73 73  3_* API call ass
a6b0: 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20  ociated.** with 
a6c0: 5b 73 71 6c 69 74 65 33 5d 20 68 61 6e 64 6c 65  [sqlite3] handle
a6d0: 20 27 64 62 27 2e 20 20 49 66 20 61 20 70 72 69   'db'.  If a pri
a6e0: 6f 72 20 41 50 49 20 63 61 6c 6c 20 66 61 69 6c  or API call fail
a6f0: 65 64 20 62 75 74 20 74 68 65 0a 2a 2a 20 6d 6f  ed but the.** mo
a700: 73 74 20 72 65 63 65 6e 74 20 41 50 49 20 63 61  st recent API ca
a710: 6c 6c 20 73 75 63 63 65 65 64 65 64 2c 20 74 68  ll succeeded, th
a720: 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66  e return value f
a730: 72 6f 6d 20 73 71 6c 69 74 65 33 5f 65 72 72 63  rom sqlite3_errc
a740: 6f 64 65 28 29 0a 2a 2a 20 69 73 20 75 6e 64 65  ode().** is unde
a750: 66 69 6e 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68  fined. .**.** Th
a760: 65 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67  e sqlite3_errmsg
a770: 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 65  () and sqlite3_e
a780: 72 72 6d 73 67 31 36 28 29 20 72 65 74 75 72 6e  rrmsg16() return
a790: 20 45 6e 67 6c 69 73 68 2d 6c 61 6e 67 61 75 67   English-langaug
a7a0: 65 0a 2a 2a 20 74 65 78 74 20 74 68 61 74 20 64  e.** text that d
a7b0: 65 73 63 72 69 62 65 73 20 74 68 65 20 65 72 72  escribes the err
a7c0: 6f 72 2c 20 61 73 20 65 69 74 68 65 72 20 55 54  or, as either UT
a7d0: 46 38 20 6f 72 20 55 54 46 31 36 20 72 65 73 70  F8 or UTF16 resp
a7e0: 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 4d 65 6d  ectively..** Mem
a7f0: 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ory to hold the 
a800: 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74  error message st
a810: 72 69 6e 67 20 69 73 20 6d 61 6e 61 67 65 64 20  ring is managed 
a820: 69 6e 74 65 72 6e 61 6c 6c 79 2e 20 20 54 68 65  internally.  The
a830: 20 0a 2a 2a 20 73 74 72 69 6e 67 20 6d 61 79 20   .** string may 
a840: 62 65 20 6f 76 65 72 77 72 69 74 74 65 6e 20 6f  be overwritten o
a850: 72 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 62 79  r deallocated by
a860: 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c   subsequent call
a870: 73 20 74 6f 20 53 51 4c 69 74 65 0a 2a 2a 20 69  s to SQLite.** i
a880: 6e 74 65 72 66 61 63 65 20 66 75 6e 63 74 69 6f  nterface functio
a890: 6e 73 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 73 20  ns..**.** Calls 
a8a0: 74 6f 20 6d 61 6e 79 20 73 71 6c 69 74 65 33 5f  to many sqlite3_
a8b0: 2a 20 66 75 6e 63 74 69 6f 6e 73 20 73 65 74 20  * functions set 
a8c0: 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 61  the error code a
a8d0: 6e 64 20 73 74 72 69 6e 67 20 72 65 74 75 72 6e  nd string return
a8e0: 65 64 0a 2a 2a 20 62 79 20 5b 73 71 6c 69 74 65  ed.** by [sqlite
a8f0: 33 5f 65 72 72 63 6f 64 65 28 29 5d 2c 20 5b 73  3_errcode()], [s
a900: 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d  qlite3_errmsg()]
a910: 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 65  , and [sqlite3_e
a920: 72 72 6d 73 67 31 36 28 29 5d 0a 2a 2a 20 28 6f  rrmsg16()].** (o
a930: 76 65 72 77 72 69 74 69 6e 67 20 74 68 65 20 70  verwriting the p
a940: 72 65 76 69 6f 75 73 20 76 61 6c 75 65 73 29 2e  revious values).
a950: 20 4e 6f 74 65 20 74 68 61 74 20 63 61 6c 6c 73   Note that calls
a960: 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72 72   to [sqlite3_err
a970: 63 6f 64 65 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c  code()],.** [sql
a980: 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2c 20  ite3_errmsg()], 
a990: 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 65 72 72  and [sqlite3_err
a9a0: 6d 73 67 31 36 28 29 5d 20 74 68 65 6d 73 65 6c  msg16()] themsel
a9b0: 76 65 73 20 64 6f 20 6e 6f 74 20 61 66 66 65 63  ves do not affec
a9c0: 74 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 73  t the.** results
a9d0: 20 6f 66 20 66 75 74 75 72 65 20 69 6e 76 6f 63   of future invoc
a9e0: 61 74 69 6f 6e 73 2e 20 20 43 61 6c 6c 73 20 74  ations.  Calls t
a9f0: 6f 20 41 50 49 20 72 6f 75 74 69 6e 65 73 20 74  o API routines t
aa00: 68 61 74 20 64 6f 20 6e 6f 74 20 72 65 74 75 72  hat do not retur
aa10: 6e 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 63 6f  n.** an error co
aa20: 64 65 20 28 65 78 61 6d 70 6c 65 73 3a 20 5b 73  de (examples: [s
aa30: 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e  qlite3_data_coun
aa40: 74 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33  t()] or [sqlite3
aa50: 5f 6d 70 72 69 6e 74 66 28 29 5d 29 20 64 6f 0a  _mprintf()]) do.
aa60: 2a 2a 20 6e 6f 74 20 63 68 61 6e 67 65 20 74 68  ** not change th
aa70: 65 20 65 72 72 6f 72 20 63 6f 64 65 20 72 65 74  e error code ret
aa80: 75 72 6e 65 64 20 62 79 20 74 68 69 73 20 72 6f  urned by this ro
aa90: 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 41 73 73  utine..**.** Ass
aaa0: 75 6d 69 6e 67 20 6e 6f 20 6f 74 68 65 72 20 69  uming no other i
aab0: 6e 74 65 72 76 65 6e 69 6e 67 20 73 71 6c 69 74  ntervening sqlit
aac0: 65 33 5f 2a 20 41 50 49 20 63 61 6c 6c 73 20 61  e3_* API calls a
aad0: 72 65 20 6d 61 64 65 2c 20 74 68 65 20 65 72 72  re made, the err
aae0: 6f 72 0a 2a 2a 20 63 6f 64 65 20 72 65 74 75 72  or.** code retur
aaf0: 6e 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63  ned by this func
ab00: 74 69 6f 6e 20 69 73 20 61 73 73 6f 63 69 61 74  tion is associat
ab10: 65 64 20 77 69 74 68 20 74 68 65 20 73 61 6d 65  ed with the same
ab20: 20 65 72 72 6f 72 20 61 73 0a 2a 2a 20 74 68 65   error as.** the
ab30: 20 73 74 72 69 6e 67 73 20 72 65 74 75 72 6e 65   strings returne
ab40: 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 65 72  d by [sqlite3_er
ab50: 72 6d 73 67 28 29 5d 20 61 6e 64 20 5b 73 71 6c  rmsg()] and [sql
ab60: 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29 5d  ite3_errmsg16()]
ab70: 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
ab80: 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 72 72 63  int sqlite3_errc
ab90: 6f 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29  ode(sqlite3 *db)
aba0: 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  ;.SQLITE_API con
abb0: 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
abc0: 5f 65 72 72 6d 73 67 28 73 71 6c 69 74 65 33 2a  _errmsg(sqlite3*
abd0: 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  );.SQLITE_API co
abe0: 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
abf0: 33 5f 65 72 72 6d 73 67 31 36 28 73 71 6c 69 74  3_errmsg16(sqlit
ac00: 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  e3*);../*.** CAP
ac10: 49 33 52 45 46 3a 20 53 51 4c 20 53 74 61 74 65  I3REF: SQL State
ac20: 6d 65 6e 74 20 4f 62 6a 65 63 74 0a 2a 2a 0a 2a  ment Object.**.*
ac30: 2a 20 49 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  * Instance of th
ac40: 69 73 20 6f 62 6a 65 63 74 20 72 65 70 72 65 73  is object repres
ac50: 65 6e 74 20 73 69 6e 67 6c 65 20 53 51 4c 20 73  ent single SQL s
ac60: 74 61 74 65 6d 65 6e 74 73 2e 20 20 54 68 69 73  tatements.  This
ac70: 0a 2a 2a 20 69 73 20 76 61 72 69 6f 75 73 6c 79  .** is variously
ac80: 20 6b 6e 6f 77 6e 20 61 73 20 61 20 22 70 72 65   known as a "pre
ac90: 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 22  pared statement"
aca0: 20 6f 72 20 61 20 0a 2a 2a 20 22 63 6f 6d 70 69   or a .** "compi
acb0: 6c 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  led SQL statemen
acc0: 74 22 20 6f 72 20 73 69 6d 70 6c 79 20 61 73 20  t" or simply as 
acd0: 61 20 22 73 74 61 74 65 6d 65 6e 74 22 2e 0a 2a  a "statement"..*
ace0: 2a 20 0a 2a 2a 20 54 68 65 20 6c 69 66 65 20 6f  * .** The life o
acf0: 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6f 62  f a statement ob
ad00: 6a 65 63 74 20 67 6f 65 73 20 73 6f 6d 65 74 68  ject goes someth
ad10: 69 6e 67 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a  ing like this:.*
ad20: 2a 0a 2a 2a 20 3c 6f 6c 3e 0a 2a 2a 20 3c 6c 69  *.** <ol>.** <li
ad30: 3e 20 43 72 65 61 74 65 20 74 68 65 20 6f 62 6a  > Create the obj
ad40: 65 63 74 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  ect using [sqlit
ad50: 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
ad60: 20 6f 72 20 61 20 72 65 6c 61 74 65 64 0a 2a 2a   or a related.**
ad70: 20 20 20 20 20 20 66 75 6e 63 74 69 6f 6e 2e 0a        function..
ad80: 2a 2a 20 3c 6c 69 3e 20 42 69 6e 64 20 76 61 6c  ** <li> Bind val
ad90: 75 65 73 20 74 6f 20 68 6f 73 74 20 70 61 72 61  ues to host para
ada0: 6d 65 74 65 72 73 20 75 73 69 6e 67 0a 2a 2a 20  meters using.** 
adb0: 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 62 69       [sqlite3_bi
adc0: 6e 64 5f 62 6c 6f 62 20 7c 20 73 71 6c 69 74 65  nd_blob | sqlite
add0: 33 5f 62 69 6e 64 5f 2a 20 69 6e 74 65 72 66 61  3_bind_* interfa
ade0: 63 65 73 5d 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 75  ces]..** <li> Ru
adf0: 6e 20 74 68 65 20 53 51 4c 20 62 79 20 63 61 6c  n the SQL by cal
ae00: 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 73 74  ling [sqlite3_st
ae10: 65 70 28 29 5d 20 6f 6e 65 20 6f 72 20 6d 6f 72  ep()] one or mor
ae20: 65 20 74 69 6d 65 73 2e 0a 2a 2a 20 3c 6c 69 3e  e times..** <li>
ae30: 20 52 65 73 65 74 20 74 68 65 20 73 74 61 74 65   Reset the state
ae40: 6d 65 6e 74 20 75 73 69 6e 67 20 5b 73 71 6c 69  ment using [sqli
ae50: 74 65 33 5f 72 65 73 65 74 28 29 5d 20 74 68 65  te3_reset()] the
ae60: 6e 20 67 6f 20 62 61 63 6b 0a 2a 2a 20 20 20 20  n go back.**    
ae70: 20 20 74 6f 20 73 74 65 70 20 32 2e 20 20 44 6f    to step 2.  Do
ae80: 20 74 68 69 73 20 7a 65 72 6f 20 6f 72 20 6d 6f   this zero or mo
ae90: 72 65 20 74 69 6d 65 73 2e 0a 2a 2a 20 3c 6c 69  re times..** <li
aea0: 3e 20 44 65 73 74 72 6f 79 20 74 68 65 20 6f 62  > Destroy the ob
aeb0: 6a 65 63 74 20 75 73 69 6e 67 20 5b 73 71 6c 69  ject using [sqli
aec0: 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2e  te3_finalize()].
aed0: 0a 2a 2a 20 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a 20  .** </ol>.**.** 
aee0: 52 65 66 65 72 20 74 6f 20 64 6f 63 75 6d 65 6e  Refer to documen
aef0: 74 61 74 69 6f 6e 20 6f 6e 20 69 6e 64 69 76 69  tation on indivi
af00: 64 75 61 6c 20 6d 65 74 68 6f 64 73 20 61 62 6f  dual methods abo
af10: 76 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61  ve for additiona
af20: 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  l.** information
af30: 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
af40: 75 63 74 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  uct sqlite3_stmt
af50: 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 3b 0a 0a   sqlite3_stmt;..
af60: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
af70: 43 6f 6d 70 69 6c 69 6e 67 20 41 6e 20 53 51 4c  Compiling An SQL
af80: 20 53 74 61 74 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a   Statement.**.**
af90: 20 54 6f 20 65 78 65 63 75 74 65 20 61 6e 20 53   To execute an S
afa0: 51 4c 20 71 75 65 72 79 2c 20 69 74 20 6d 75 73  QL query, it mus
afb0: 74 20 66 69 72 73 74 20 62 65 20 63 6f 6d 70 69  t first be compi
afc0: 6c 65 64 20 69 6e 74 6f 20 61 20 62 79 74 65 2d  led into a byte-
afd0: 63 6f 64 65 0a 2a 2a 20 70 72 6f 67 72 61 6d 20  code.** program 
afe0: 75 73 69 6e 67 20 6f 6e 65 20 6f 66 20 74 68 65  using one of the
aff0: 73 65 20 72 6f 75 74 69 6e 65 73 2e 20 0a 2a 2a  se routines. .**
b000: 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 72  .** The first ar
b010: 67 75 6d 65 6e 74 20 22 64 62 22 20 69 73 20 61  gument "db" is a
b020: 6e 20 5b 73 71 6c 69 74 65 33 20 7c 20 53 51 4c  n [sqlite3 | SQL
b030: 69 74 65 20 64 61 74 61 62 61 73 65 20 68 61 6e  ite database han
b040: 64 6c 65 5d 20 0a 2a 2a 20 6f 62 74 61 69 6e 65  dle] .** obtaine
b050: 64 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 20 63  d from a prior c
b060: 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  all to [sqlite3_
b070: 6f 70 65 6e 28 29 5d 20 6f 72 20 5b 73 71 6c 69  open()] or [sqli
b080: 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a 2a  te3_open16()]..*
b090: 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67  * The second arg
b0a0: 75 6d 65 6e 74 20 22 7a 53 71 6c 22 20 69 73 20  ument "zSql" is 
b0b0: 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 6f  the statement to
b0c0: 20 62 65 20 63 6f 6d 70 69 6c 65 64 2c 20 65 6e   be compiled, en
b0d0: 63 6f 64 65 64 0a 2a 2a 20 61 73 20 65 69 74 68  coded.** as eith
b0e0: 65 72 20 55 54 46 2d 38 20 6f 72 20 55 54 46 2d  er UTF-8 or UTF-
b0f0: 31 36 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33  16.  The sqlite3
b100: 5f 70 72 65 70 61 72 65 28 29 20 61 6e 64 20 73  _prepare() and s
b110: 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
b120: 32 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  2().** interface
b130: 73 20 75 73 65 73 20 55 54 46 2d 38 20 61 6e 64  s uses UTF-8 and
b140: 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
b150: 31 36 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  16() and sqlite3
b160: 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 0a  _prepare16_v2().
b170: 2a 2a 20 75 73 65 20 55 54 46 2d 31 36 2e 0a 2a  ** use UTF-16..*
b180: 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 42 79 74  *.** If the nByt
b190: 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6c 65  e argument is le
b1a0: 73 73 0a 2a 2a 20 74 68 61 6e 20 7a 65 72 6f 2c  ss.** than zero,
b1b0: 20 74 68 65 6e 20 7a 53 71 6c 20 69 73 20 72 65   then zSql is re
b1c0: 61 64 20 75 70 20 74 6f 20 74 68 65 20 66 69 72  ad up to the fir
b1d0: 73 74 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74  st zero terminat
b1e0: 6f 72 2e 20 20 49 66 0a 2a 2a 20 6e 42 79 74 65  or.  If.** nByte
b1f0: 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65   is non-negative
b200: 2c 20 74 68 65 6e 20 69 74 20 69 73 20 74 68 65  , then it is the
b210: 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   maximum number 
b220: 6f 66 20 0a 2a 2a 20 62 79 74 65 73 20 72 65 61  of .** bytes rea
b230: 64 20 66 72 6f 6d 20 7a 53 71 6c 2e 20 20 57 68  d from zSql.  Wh
b240: 65 6e 20 6e 42 79 74 65 20 69 73 20 6e 6f 6e 2d  en nByte is non-
b250: 6e 65 67 61 74 69 76 65 2c 20 74 68 65 0a 2a 2a  negative, the.**
b260: 20 7a 53 71 6c 20 73 74 72 69 6e 67 20 65 6e 64   zSql string end
b270: 73 20 61 74 20 65 69 74 68 65 72 20 74 68 65 20  s at either the 
b280: 66 69 72 73 74 20 27 5c 30 30 30 27 20 63 68 61  first '\000' cha
b290: 72 61 63 74 65 72 20 6f 72 20 0a 2a 2a 20 75 6e  racter or .** un
b2a0: 74 69 6c 20 74 68 65 20 6e 42 79 74 65 2d 74 68  til the nByte-th
b2b0: 20 62 79 74 65 2c 20 77 68 69 63 68 65 76 65 72   byte, whichever
b2c0: 20 63 6f 6d 65 73 20 66 69 72 73 74 2e 0a 2a 2a   comes first..**
b2d0: 0a 2a 2a 20 2a 70 7a 54 61 69 6c 20 69 73 20 6d  .** *pzTail is m
b2e0: 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20  ade to point to 
b2f0: 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20 70  the first byte p
b300: 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74  ast the end of t
b310: 68 65 20 66 69 72 73 74 0a 2a 2a 20 53 51 4c 20  he first.** SQL 
b320: 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 7a 53 71  statement in zSq
b330: 6c 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  l.  This routine
b340: 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65 73 20 74   only compiles t
b350: 68 65 20 66 69 72 73 74 20 73 74 61 74 65 6d 65  he first stateme
b360: 6e 74 0a 2a 2a 20 69 6e 20 7a 53 71 6c 2c 20 73  nt.** in zSql, s
b370: 6f 20 2a 70 7a 54 61 69 6c 20 69 73 20 6c 65 66  o *pzTail is lef
b380: 74 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 77 68  t pointing to wh
b390: 61 74 20 72 65 6d 61 69 6e 73 20 75 6e 63 6f 6d  at remains uncom
b3a0: 70 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 2a 70 70  piled..**.** *pp
b3b0: 53 74 6d 74 20 69 73 20 6c 65 66 74 20 70 6f 69  Stmt is left poi
b3c0: 6e 74 69 6e 67 20 74 6f 20 61 20 63 6f 6d 70 69  nting to a compi
b3d0: 6c 65 64 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  led .** [sqlite3
b3e0: 5f 73 74 6d 74 20 7c 20 53 51 4c 20 73 74 61 74  _stmt | SQL stat
b3f0: 65 6d 65 6e 74 20 73 74 72 75 63 74 75 72 65 5d  ement structure]
b400: 20 74 68 61 74 20 63 61 6e 20 62 65 0a 2a 2a 20   that can be.** 
b410: 65 78 65 63 75 74 65 64 20 75 73 69 6e 67 20 5b  executed using [
b420: 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2e  sqlite3_step()].
b430: 20 20 4f 72 20 69 66 20 74 68 65 72 65 20 69 73    Or if there is
b440: 20 61 6e 20 65 72 72 6f 72 2c 20 2a 70 70 53 74   an error, *ppSt
b450: 6d 74 20 6d 61 79 20 62 65 0a 2a 2a 20 73 65 74  mt may be.** set
b460: 20 74 6f 20 4e 55 4c 4c 2e 20 20 49 66 20 74 68   to NULL.  If th
b470: 65 20 69 6e 70 75 74 20 74 65 78 74 20 63 6f 6e  e input text con
b480: 74 61 69 6e 65 64 20 6e 6f 20 53 51 4c 20 28 69  tained no SQL (i
b490: 66 20 74 68 65 20 69 6e 70 75 74 20 69 73 20 61  f the input is a
b4a0: 6e 64 0a 2a 2a 20 65 6d 70 74 79 20 73 74 72 69  nd.** empty stri
b4b0: 6e 67 20 6f 72 20 61 20 63 6f 6d 6d 65 6e 74 29  ng or a comment)
b4c0: 20 74 68 65 6e 20 2a 70 70 53 74 6d 74 20 69 73   then *ppStmt is
b4d0: 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 20 20 54   set to NULL.  T
b4e0: 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 70 72  he calling.** pr
b4f0: 6f 63 65 64 75 72 65 20 69 73 20 72 65 73 70 6f  ocedure is respo
b500: 6e 73 69 62 6c 65 20 66 6f 72 20 64 65 6c 65 74  nsible for delet
b510: 69 6e 67 20 74 68 65 20 63 6f 6d 70 69 6c 65 64  ing the compiled
b520: 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 0a 2a   SQL statement.*
b530: 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33  * using [sqlite3
b540: 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 61 66 74  _finalize()] aft
b550: 65 72 20 69 74 20 68 61 73 20 66 69 6e 69 73 68  er it has finish
b560: 65 64 20 77 69 74 68 20 69 74 2e 0a 2a 2a 0a 2a  ed with it..**.*
b570: 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 5b 53  * On success, [S
b580: 51 4c 49 54 45 5f 4f 4b 5d 20 69 73 20 72 65 74  QLITE_OK] is ret
b590: 75 72 6e 65 64 2e 20 20 4f 74 68 65 72 77 69 73  urned.  Otherwis
b5a0: 65 20 61 6e 20 0a 2a 2a 20 5b 53 51 4c 49 54 45  e an .** [SQLITE
b5b0: 5f 45 52 52 4f 52 20 7c 20 65 72 72 6f 72 20 63  _ERROR | error c
b5c0: 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64  ode] is returned
b5d0: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
b5e0: 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29  te3_prepare_v2()
b5f0: 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 70 72 65   and sqlite3_pre
b600: 70 61 72 65 31 36 5f 76 32 28 29 20 69 6e 74 65  pare16_v2() inte
b610: 72 66 61 63 65 73 20 61 72 65 0a 2a 2a 20 72 65  rfaces are.** re
b620: 63 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20 61 6c  commended for al
b630: 6c 20 6e 65 77 20 70 72 6f 67 72 61 6d 73 2e 20  l new programs. 
b640: 54 68 65 20 74 77 6f 20 6f 6c 64 65 72 20 69 6e  The two older in
b650: 74 65 72 66 61 63 65 73 20 61 72 65 20 72 65 74  terfaces are ret
b660: 61 69 6e 65 64 0a 2a 2a 20 66 6f 72 20 62 61 63  ained.** for bac
b670: 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69  kwards compatibi
b680: 6c 69 74 79 2c 20 62 75 74 20 74 68 65 69 72 20  lity, but their 
b690: 75 73 65 20 69 73 20 64 69 73 63 6f 75 72 61 67  use is discourag
b6a0: 65 64 2e 0a 2a 2a 20 49 6e 20 74 68 65 20 22 76  ed..** In the "v
b6b0: 32 22 20 69 6e 74 65 72 66 61 63 65 73 2c 20 74  2" interfaces, t
b6c0: 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74  he prepared stat
b6d0: 65 6d 65 6e 74 0a 2a 2a 20 74 68 61 74 20 69 73  ement.** that is
b6e0: 20 72 65 74 75 72 6e 65 64 20 28 74 68 65 20 5b   returned (the [
b6f0: 73 71 6c 69 74 65 33 5f 73 74 6d 74 5d 20 6f 62  sqlite3_stmt] ob
b700: 6a 65 63 74 29 20 63 6f 6e 74 61 69 6e 73 20 61  ject) contains a
b710: 20 63 6f 70 79 20 6f 66 20 74 68 65 20 0a 2a 2a   copy of the .**
b720: 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 74 65   original SQL te
b730: 78 74 2e 20 54 68 69 73 20 63 61 75 73 65 73 20  xt. This causes 
b740: 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65  the [sqlite3_ste
b750: 70 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 74  p()] interface t
b760: 6f 0a 2a 2a 20 62 65 68 61 76 65 20 61 20 64 69  o.** behave a di
b770: 66 66 65 72 65 6e 74 6c 79 20 69 6e 20 74 77 6f  fferently in two
b780: 20 77 61 79 73 3a 0a 2a 2a 0a 2a 2a 20 3c 6f 6c   ways:.**.** <ol
b790: 3e 0a 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20 49 66 20  >.** <li>.** If 
b7a0: 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68  the database sch
b7b0: 65 6d 61 20 63 68 61 6e 67 65 73 2c 20 69 6e 73  ema changes, ins
b7c0: 74 65 61 64 20 6f 66 20 72 65 74 75 72 6e 69 6e  tead of returnin
b7d0: 67 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41  g [SQLITE_SCHEMA
b7e0: 5d 20 61 73 20 69 74 0a 2a 2a 20 61 6c 77 61 79  ] as it.** alway
b7f0: 73 20 75 73 65 64 20 74 6f 20 64 6f 2c 20 5b 73  s used to do, [s
b800: 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 77  qlite3_step()] w
b810: 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  ill automaticall
b820: 79 20 72 65 63 6f 6d 70 69 6c 65 20 74 68 65 20  y recompile the 
b830: 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  SQL.** statement
b840: 20 61 6e 64 20 74 72 79 20 74 6f 20 72 75 6e 20   and try to run 
b850: 69 74 20 61 67 61 69 6e 2e 20 20 49 66 20 74 68  it again.  If th
b860: 65 20 73 63 68 65 6d 61 20 68 61 73 20 63 68 61  e schema has cha
b870: 6e 67 65 64 20 69 6e 20 61 20 77 61 79 0a 2a 2a  nged in a way.**
b880: 20 74 68 61 74 20 6d 61 6b 65 73 20 74 68 65 20   that makes the 
b890: 73 74 61 74 65 6d 65 6e 74 20 6e 6f 20 6c 6f 6e  statement no lon
b8a0: 67 65 72 20 76 61 6c 69 64 2c 20 5b 73 71 6c 69  ger valid, [sqli
b8b0: 74 65 33 5f 73 74 65 70 28 29 5d 20 77 69 6c 6c  te3_step()] will
b8c0: 20 73 74 69 6c 6c 0a 2a 2a 20 72 65 74 75 72 6e   still.** return
b8d0: 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d   [SQLITE_SCHEMA]
b8e0: 2e 20 20 42 75 74 20 75 6e 6c 69 6b 65 20 74 68  .  But unlike th
b8f0: 65 20 6c 65 67 61 63 79 20 62 65 68 61 76 69 6f  e legacy behavio
b900: 72 2c 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d  r, [SQLITE_SCHEM
b910: 41 5d 20 69 73 0a 2a 2a 20 6e 6f 77 20 61 20 66  A] is.** now a f
b920: 61 74 61 6c 20 65 72 72 6f 72 2e 20 20 43 61 6c  atal error.  Cal
b930: 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 72  ling [sqlite3_pr
b940: 65 70 61 72 65 5f 76 32 28 29 5d 20 61 67 61 69  epare_v2()] agai
b950: 6e 20 77 69 6c 6c 20 6e 6f 74 20 6d 61 6b 65 20  n will not make 
b960: 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 67 6f 20  the.** error go 
b970: 61 77 61 79 2e 20 20 4e 6f 74 65 3a 20 75 73 65  away.  Note: use
b980: 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67   [sqlite3_errmsg
b990: 28 29 5d 20 74 6f 20 66 69 6e 64 20 74 68 65 20  ()] to find the 
b9a0: 74 65 78 74 20 6f 66 20 74 68 65 20 70 61 72 73  text of the pars
b9b0: 69 6e 67 0a 2a 2a 20 65 72 72 6f 72 20 74 68 61  ing.** error tha
b9c0: 74 20 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 20  t results in an 
b9d0: 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d 20  [SQLITE_SCHEMA] 
b9e0: 72 65 74 75 72 6e 2e 0a 2a 2a 20 3c 2f 6c 69 3e  return..** </li>
b9f0: 0a 2a 2a 0a 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20 57  .**.** <li>.** W
ba00: 68 65 6e 20 61 6e 20 65 72 72 6f 72 20 6f 63 63  hen an error occ
ba10: 75 72 73 2c 20 0a 2a 2a 20 5b 73 71 6c 69 74 65  urs, .** [sqlite
ba20: 33 5f 73 74 65 70 28 29 5d 20 77 69 6c 6c 20 72  3_step()] will r
ba30: 65 74 75 72 6e 20 6f 6e 65 20 6f 66 20 74 68 65  eturn one of the
ba40: 20 64 65 74 61 69 6c 65 64 20 0a 2a 2a 20 5b 53   detailed .** [S
ba50: 51 4c 49 54 45 5f 45 52 52 4f 52 20 7c 20 72 65  QLITE_ERROR | re
ba60: 73 75 6c 74 20 63 6f 64 65 73 5d 20 6f 72 0a 2a  sult codes] or.*
ba70: 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  * [SQLITE_IOERR_
ba80: 52 45 41 44 20 7c 20 65 78 74 65 6e 64 65 64 20  READ | extended 
ba90: 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 20 73 75  result codes] su
baa0: 63 68 20 61 73 20 64 69 72 65 63 74 6c 79 2e 0a  ch as directly..
bab0: 2a 2a 20 54 68 65 20 6c 65 67 61 63 79 20 62 65  ** The legacy be
bac0: 68 61 76 69 6f 72 20 77 61 73 20 74 68 61 74 20  havior was that 
bad0: 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
bae0: 20 77 6f 75 6c 64 20 6f 6e 6c 79 20 72 65 74 75   would only retu
baf0: 72 6e 20 61 20 67 65 6e 65 72 69 63 0a 2a 2a 20  rn a generic.** 
bb00: 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 72  [SQLITE_ERROR] r
bb10: 65 73 75 6c 74 20 63 6f 64 65 20 61 6e 64 20 79  esult code and y
bb20: 6f 75 20 77 6f 75 6c 64 20 68 61 76 65 20 74 6f  ou would have to
bb30: 20 6d 61 6b 65 20 61 20 73 65 63 6f 6e 64 20 63   make a second c
bb40: 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74  all to.** [sqlit
bb50: 65 33 5f 72 65 73 65 74 28 29 5d 20 69 6e 20 6f  e3_reset()] in o
bb60: 72 64 65 72 20 74 6f 20 66 69 6e 64 20 74 68 65  rder to find the
bb70: 20 75 6e 64 65 72 6c 79 69 6e 67 20 63 61 75 73   underlying caus
bb80: 65 20 6f 66 20 74 68 65 20 70 72 6f 62 6c 65 6d  e of the problem
bb90: 2e 0a 2a 2a 20 57 69 74 68 20 74 68 65 20 22 76  ..** With the "v
bba0: 32 22 20 70 72 65 70 61 72 65 20 69 6e 74 65 72  2" prepare inter
bbb0: 66 61 63 65 73 2c 20 74 68 65 20 75 6e 64 65 72  faces, the under
bbc0: 6c 79 69 6e 67 20 72 65 61 73 6f 6e 20 66 6f 72  lying reason for
bbd0: 20 74 68 65 20 65 72 72 6f 72 20 69 73 0a 2a 2a   the error is.**
bbe0: 20 72 65 74 75 72 6e 65 64 20 69 6d 6d 65 64 69   returned immedi
bbf0: 61 74 65 6c 79 2e 0a 2a 2a 20 3c 2f 6c 69 3e 0a  ately..** </li>.
bc00: 2a 2a 20 3c 2f 6f 6c 3e 0a 2a 2f 0a 53 51 4c 49  ** </ol>.*/.SQLI
bc10: 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
bc20: 65 33 5f 70 72 65 70 61 72 65 28 0a 20 20 73 71  e3_prepare(.  sq
bc30: 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
bc40: 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
bc50: 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f  e handle */.  co
bc60: 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20  nst char *zSql, 
bc70: 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 73 74 61        /* SQL sta
bc80: 74 65 6d 65 6e 74 2c 20 55 54 46 2d 38 20 65 6e  tement, UTF-8 en
bc90: 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  coded */.  int n
bca0: 42 79 74 65 2c 20 20 20 20 20 20 20 20 20 20 20  Byte,           
bcb0: 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65     /* Maximum le
bcc0: 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20  ngth of zSql in 
bcd0: 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69  bytes. */.  sqli
bce0: 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d  te3_stmt **ppStm
bcf0: 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74  t,  /* OUT: Stat
bd00: 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0a  ement handle */.
bd10: 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70    const char **p
bd20: 7a 54 61 69 6c 20 20 20 20 20 2f 2a 20 4f 55 54  zTail     /* OUT
bd30: 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20 75 6e 75  : Pointer to unu
bd40: 73 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 7a  sed portion of z
bd50: 53 71 6c 20 2a 2f 0a 29 3b 0a 69 6e 74 20 73 71  Sql */.);.int sq
bd60: 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
bd70: 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
bd80: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
bd90: 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a  atabase handle *
bda0: 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
bdb0: 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53  zSql,       /* S
bdc0: 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 54  QL statement, UT
bdd0: 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20  F-8 encoded */. 
bde0: 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20   int nByte,     
bdf0: 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69           /* Maxi
be00: 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53  mum length of zS
be10: 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a  ql in bytes. */.
be20: 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
be30: 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54  *ppStmt,  /* OUT
be40: 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e 64  : Statement hand
be50: 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  le */.  const ch
be60: 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20  ar **pzTail     
be70: 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20  /* OUT: Pointer 
be80: 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69 6f  to unused portio
be90: 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a  n of zSql */.);.
bea0: 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
beb0: 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
bec0: 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
bed0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
bee0: 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a  atabase handle *
bef0: 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  /.  const void *
bf00: 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53  zSql,       /* S
bf10: 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 54  QL statement, UT
bf20: 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 2a 2f 0a  F-16 encoded */.
bf30: 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20    int nByte,    
bf40: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78            /* Max
bf50: 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a  imum length of z
bf60: 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f  Sql in bytes. */
bf70: 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
bf80: 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55  **ppStmt,  /* OU
bf90: 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e  T: Statement han
bfa0: 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  dle */.  const v
bfb0: 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20  oid **pzTail    
bfc0: 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72   /* OUT: Pointer
bfd0: 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69   to unused porti
bfe0: 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b  on of zSql */.);
bff0: 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65  .int sqlite3_pre
c000: 70 61 72 65 31 36 5f 76 32 28 0a 20 20 73 71 6c  pare16_v2(.  sql
c010: 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
c020: 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
c030: 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e   handle */.  con
c040: 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 20  st void *zSql,  
c050: 20 20 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 74       /* SQL stat
c060: 65 6d 65 6e 74 2c 20 55 54 46 2d 31 36 20 65 6e  ement, UTF-16 en
c070: 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  coded */.  int n
c080: 42 79 74 65 2c 20 20 20 20 20 20 20 20 20 20 20  Byte,           
c090: 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65     /* Maximum le
c0a0: 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20  ngth of zSql in 
c0b0: 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69  bytes. */.  sqli
c0c0: 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d  te3_stmt **ppStm
c0d0: 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74  t,  /* OUT: Stat
c0e0: 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0a  ement handle */.
c0f0: 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2a 70    const void **p
c100: 7a 54 61 69 6c 20 20 20 20 20 2f 2a 20 4f 55 54  zTail     /* OUT
c110: 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20 75 6e 75  : Pointer to unu
c120: 73 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 7a  sed portion of z
c130: 53 71 6c 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a  Sql */.);../*.**
c140: 20 43 41 50 49 33 52 45 46 3a 20 20 44 79 6e 61   CAPI3REF:  Dyna
c150: 6d 69 63 61 6c 6c 79 20 54 79 70 65 64 20 56 61  mically Typed Va
c160: 6c 75 65 20 4f 62 6a 65 63 74 0a 2a 2a 0a 2a 2a  lue Object.**.**
c170: 20 53 51 4c 69 74 65 20 75 73 65 73 20 64 79 6e   SQLite uses dyn
c180: 61 6d 69 63 20 74 79 70 69 6e 67 20 66 6f 72 20  amic typing for 
c190: 74 68 65 20 76 61 6c 75 65 73 20 69 74 20 73 74  the values it st
c1a0: 6f 72 65 73 2e 20 20 56 61 6c 75 65 73 20 63 61  ores.  Values ca
c1b0: 6e 20 0a 2a 2a 20 62 65 20 69 6e 74 65 67 65 72  n .** be integer
c1c0: 73 2c 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e  s, floating poin
c1d0: 74 20 76 61 6c 75 65 73 2c 20 73 74 72 69 6e 67  t values, string
c1e0: 73 2c 20 42 4c 4f 42 73 2c 20 6f 72 20 4e 55 4c  s, BLOBs, or NUL
c1f0: 4c 2e 20 20 57 68 65 6e 0a 2a 2a 20 70 61 73 73  L.  When.** pass
c200: 69 6e 67 20 61 72 6f 75 6e 64 20 76 61 6c 75 65  ing around value
c210: 73 20 69 6e 74 65 72 6e 61 6c 6c 79 2c 20 65 61  s internally, ea
c220: 63 68 20 76 61 6c 75 65 20 69 73 20 72 65 70 72  ch value is repr
c230: 65 73 65 6e 74 65 64 20 61 73 0a 2a 2a 20 61 6e  esented as.** an
c240: 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
c250: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f   sqlite3_value o
c260: 62 6a 65 63 74 2e 0a 2a 2f 0a 74 79 70 65 64 65  bject..*/.typede
c270: 66 20 73 74 72 75 63 74 20 4d 65 6d 20 73 71 6c  f struct Mem sql
c280: 69 74 65 33 5f 76 61 6c 75 65 3b 0a 0a 2f 2a 0a  ite3_value;../*.
c290: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 20 53 51  ** CAPI3REF:  SQ
c2a0: 4c 20 46 75 6e 63 74 69 6f 6e 20 43 6f 6e 74 65  L Function Conte
c2b0: 78 74 20 4f 62 6a 65 63 74 0a 2a 2a 0a 2a 2a 20  xt Object.**.** 
c2c0: 54 68 65 20 63 6f 6e 74 65 78 74 20 69 6e 20 77  The context in w
c2d0: 68 69 63 68 20 61 6e 20 53 51 4c 20 66 75 6e 63  hich an SQL func
c2e0: 74 69 6f 6e 20 65 78 65 63 75 74 65 73 20 69 73  tion executes is
c2f0: 20 73 74 6f 72 65 64 20 69 6e 20 61 6e 0a 2a 2a   stored in an.**
c300: 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
c310: 20 6f 62 6a 65 63 74 2e 20 20 41 20 70 6f 69 6e   object.  A poin
c320: 74 65 72 20 74 6f 20 73 75 63 68 20 61 6e 20 6f  ter to such an o
c330: 62 6a 65 63 74 20 69 73 20 74 68 65 0a 2a 2a 20  bject is the.** 
c340: 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20  first parameter 
c350: 74 6f 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20  to user-defined 
c360: 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a  SQL functions..*
c370: 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
c380: 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
c390: 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
c3a0: 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
c3b0: 46 3a 20 20 42 69 6e 64 69 6e 67 20 56 61 6c 75  F:  Binding Valu
c3c0: 65 73 20 54 6f 20 50 72 65 70 61 72 65 64 20 53  es To Prepared S
c3d0: 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 0a 2a 2a 20  tatements.**.** 
c3e0: 49 6e 20 74 68 65 20 53 51 4c 20 73 74 72 69 6e  In the SQL strin
c3f0: 67 73 20 69 6e 70 75 74 20 74 6f 20 5b 73 71 6c  gs input to [sql
c400: 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
c410: 29 5d 20 61 6e 64 20 69 74 73 20 76 61 72 69 61  )] and its varia
c420: 6e 74 73 2c 0a 2a 2a 20 6f 6e 65 20 6f 72 20 6d  nts,.** one or m
c430: 6f 72 65 20 6c 69 74 65 72 61 6c 73 20 63 61 6e  ore literals can
c440: 20 62 65 20 72 65 70 6c 61 63 65 20 62 79 20 61   be replace by a
c450: 20 70 61 72 61 6d 65 74 65 72 20 69 6e 20 6f 6e   parameter in on
c460: 65 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 66 6f  e of these.** fo
c470: 72 6d 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a  rms:.**.** <ul>.
c480: 2a 2a 20 3c 6c 69 3e 20 20 3f 0a 2a 2a 20 3c 6c  ** <li>  ?.** <l
c490: 69 3e 20 20 3f 4e 4e 4e 0a 2a 2a 20 3c 6c 69 3e  i>  ?NNN.** <li>
c4a0: 20 20 3a 41 41 41 0a 2a 2a 20 3c 6c 69 3e 20 20    :AAA.** <li>  
c4b0: 40 41 41 41 0a 2a 2a 20 3c 6c 69 3e 20 20 24 56  @AAA.** <li>  $V
c4c0: 56 56 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a  VV.** </ul>.**.*
c4d0: 2a 20 49 6e 20 74 68 65 20 70 61 72 61 6d 65 74  * In the paramet
c4e0: 65 72 20 66 6f 72 6d 73 20 73 68 6f 77 6e 20 61  er forms shown a
c4f0: 62 6f 76 65 20 4e 4e 4e 20 69 73 20 61 6e 20 69  bove NNN is an i
c500: 6e 74 65 67 65 72 20 6c 69 74 65 72 61 6c 2c 0a  nteger literal,.
c510: 2a 2a 20 41 41 41 20 69 73 20 61 6e 20 61 6c 70  ** AAA is an alp
c520: 68 61 6e 75 6d 65 72 69 63 20 69 64 65 6e 74 69  hanumeric identi
c530: 66 69 65 72 20 61 6e 64 20 56 56 56 20 69 73 20  fier and VVV is 
c540: 61 20 76 61 72 69 61 62 6c 65 20 6e 61 6d 65 20  a variable name 
c550: 61 63 63 6f 72 64 69 6e 67 0a 2a 2a 20 74 6f 20  according.** to 
c560: 74 68 65 20 73 79 6e 74 61 78 20 72 75 6c 65 73  the syntax rules
c570: 20 6f 66 20 74 68 65 20 54 43 4c 20 70 72 6f 67   of the TCL prog
c580: 72 61 6d 6d 69 6e 67 20 6c 61 6e 67 75 61 67 65  ramming language
c590: 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 73 20  ..** The values 
c5a0: 6f 66 20 74 68 65 73 65 20 70 61 72 61 6d 65 74  of these paramet
c5b0: 65 72 73 20 28 61 6c 73 6f 20 63 61 6c 6c 65 64  ers (also called
c5c0: 20 22 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72   "host parameter
c5d0: 20 6e 61 6d 65 73 22 29 0a 2a 2a 20 63 61 6e 20   names").** can 
c5e0: 62 65 20 73 65 74 20 75 73 69 6e 67 20 74 68 65  be set using the
c5f0: 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28   sqlite3_bind_*(
c600: 29 20 72 6f 75 74 69 6e 65 73 20 64 65 66 69 6e  ) routines defin
c610: 65 64 20 68 65 72 65 2e 0a 2a 2a 0a 2a 2a 20 54  ed here..**.** T
c620: 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
c630: 74 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33  t to the sqlite3
c640: 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e  _bind_*() routin
c650: 65 73 20 61 6c 77 61 79 73 20 69 73 20 61 20 70  es always is a p
c660: 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65  ointer.** to the
c670: 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 5d 20   [sqlite3_stmt] 
c680: 6f 62 6a 65 63 74 20 72 65 74 75 72 6e 65 64 20  object returned 
c690: 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 70 72  from [sqlite3_pr
c6a0: 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 0a 2a  epare_v2()] or.*
c6b0: 2a 20 69 74 73 20 76 61 72 69 61 6e 74 73 2e 20  * its variants. 
c6c0: 20 54 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 61   The second.** a
c6d0: 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 69  rgument is the i
c6e0: 6e 64 65 78 20 6f 66 20 74 68 65 20 70 61 72 61  ndex of the para
c6f0: 6d 65 74 65 72 20 74 6f 20 62 65 20 73 65 74 2e  meter to be set.
c700: 20 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61    The first para
c710: 6d 65 74 65 72 20 68 61 73 0a 2a 2a 20 61 6e 20  meter has.** an 
c720: 69 6e 64 65 78 20 6f 66 20 31 2e 20 57 68 65 6e  index of 1. When
c730: 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 64 20   the same named 
c740: 70 61 72 61 6d 65 74 65 72 20 69 73 20 75 73 65  parameter is use
c750: 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65  d more than once
c760: 2c 20 73 65 63 6f 6e 64 0a 2a 2a 20 61 6e 64 20  , second.** and 
c770: 73 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 6f 63  subsequent.** oc
c780: 63 75 72 72 65 6e 63 65 73 20 68 61 76 65 20 74  currences have t
c790: 68 65 20 73 61 6d 65 20 69 6e 64 65 78 20 61 73  he same index as
c7a0: 20 74 68 65 20 66 69 72 73 74 20 6f 63 63 75 72   the first occur
c7b0: 72 65 6e 63 65 2e 20 20 54 68 65 20 69 6e 64 65  rence.  The inde
c7c0: 78 20 66 6f 72 0a 2a 2a 20 6e 61 6d 65 64 20 70  x for.** named p
c7d0: 61 72 61 6d 65 74 65 72 73 20 63 61 6e 20 62 65  arameters can be
c7e0: 20 6c 6f 6f 6b 65 64 20 75 70 20 75 73 69 6e 67   looked up using
c7f0: 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33   the.** [sqlite3
c800: 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  _bind_parameter_
c810: 6e 61 6d 65 28 29 5d 20 41 50 49 20 69 66 20 64  name()] API if d
c820: 65 73 69 72 65 64 2e 20 20 54 68 65 20 69 6e 64  esired.  The ind
c830: 65 78 20 66 6f 72 20 22 3f 4e 4e 4e 22 0a 2a 2a  ex for "?NNN".**
c840: 20 70 61 72 61 6d 65 74 65 73 20 69 73 20 74 68   parametes is th
c850: 65 20 76 61 6c 75 65 20 6f 66 20 4e 4e 4e 2e 0a  e value of NNN..
c860: 2a 2a 20 54 68 65 20 4e 4e 4e 20 76 61 6c 75 65  ** The NNN value
c870: 20 6d 75 73 74 20 62 65 20 62 65 74 77 65 65 6e   must be between
c880: 20 31 20 61 6e 64 20 74 68 65 20 63 6f 6d 70 69   1 and the compi
c890: 6c 65 2d 74 69 6d 65 0a 2a 2a 20 70 61 72 61 6d  le-time.** param
c8a0: 65 74 65 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f  eter SQLITE_MAX_
c8b0: 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 20  VARIABLE_NUMBER 
c8c0: 28 64 65 66 61 75 6c 74 20 76 61 6c 75 65 3a 20  (default value: 
c8d0: 39 39 39 29 2e 0a 2a 2a 20 53 65 65 20 3c 61 20  999)..** See <a 
c8e0: 68 72 65 66 3d 22 6c 69 6d 69 74 73 2e 68 74 6d  href="limits.htm
c8f0: 6c 22 3e 6c 69 6d 69 74 73 2e 68 74 6d 6c 3c 2f  l">limits.html</
c900: 61 3e 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61  a> for additiona
c910: 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a  l information..*
c920: 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20 61  *.** The third a
c930: 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 76  rgument is the v
c940: 61 6c 75 65 20 74 6f 20 62 69 6e 64 20 74 6f 20  alue to bind to 
c950: 74 68 65 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a  the parameter..*
c960: 2a 0a 2a 2a 20 49 6e 20 74 68 6f 73 65 0a 2a 2a  *.** In those.**
c970: 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 68   routines that h
c980: 61 76 65 20 61 20 66 6f 75 72 74 68 20 61 72 67  ave a fourth arg
c990: 75 6d 65 6e 74 2c 20 69 74 73 20 76 61 6c 75 65  ument, its value
c9a0: 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
c9b0: 66 20 62 79 74 65 73 0a 2a 2a 20 69 6e 20 74 68  f bytes.** in th
c9c0: 65 20 70 61 72 61 6d 65 74 65 72 2e 20 20 54 6f  e parameter.  To
c9d0: 20 62 65 20 63 6c 65 61 72 3a 20 74 68 65 20 76   be clear: the v
c9e0: 61 6c 75 65 20 69 73 20 74 68 65 20 6e 75 6d 62  alue is the numb
c9f0: 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74  er of bytes in t
ca00: 68 65 0a 2a 2a 20 73 74 72 69 6e 67 2c 20 6e 6f  he.** string, no
ca10: 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  t the number of 
ca20: 63 68 61 72 61 63 74 65 72 73 2e 20 20 54 68 65  characters.  The
ca30: 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 62 79   number.** of by
ca40: 74 65 73 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63  tes does not inc
ca50: 6c 75 64 65 20 74 68 65 20 7a 65 72 6f 2d 74 65  lude the zero-te
ca60: 72 6d 69 6e 61 74 6f 72 20 61 74 20 74 68 65 20  rminator at the 
ca70: 65 6e 64 20 6f 66 20 73 74 72 69 6e 67 73 2e 0a  end of strings..
ca80: 2a 2a 20 49 66 20 74 68 65 20 66 6f 75 72 74 68  ** If the fourth
ca90: 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 65   parameter is ne
caa0: 67 61 74 69 76 65 2c 20 74 68 65 20 6c 65 6e 67  gative, the leng
cab0: 74 68 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67  th of the string
cac0: 20 69 73 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66   is.** number of
cad0: 20 62 79 74 65 73 20 75 70 20 74 6f 20 74 68 65   bytes up to the
cae0: 20 66 69 72 73 74 20 7a 65 72 6f 20 74 65 72 6d   first zero term
caf0: 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  inator..**.** Th
cb00: 65 20 66 69 66 74 68 20 61 72 67 75 6d 65 6e 74  e fifth argument
cb10: 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 69 6e 64   to sqlite3_bind
cb20: 5f 62 6c 6f 62 28 29 2c 20 73 71 6c 69 74 65 33  _blob(), sqlite3
cb30: 5f 62 69 6e 64 5f 74 65 78 74 28 29 2c 20 61 6e  _bind_text(), an
cb40: 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 69 6e  d.** sqlite3_bin
cb50: 64 5f 74 65 78 74 31 36 28 29 20 69 73 20 61 20  d_text16() is a 
cb60: 64 65 73 74 72 75 63 74 6f 72 20 75 73 65 64 20  destructor used 
cb70: 74 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 74 68  to dispose of th
cb80: 65 20 42 4c 4f 42 20 6f 72 0a 2a 2a 20 74 65 78  e BLOB or.** tex
cb90: 74 20 61 66 74 65 72 20 53 51 4c 69 74 65 20 68  t after SQLite h
cba0: 61 73 20 66 69 6e 69 73 68 65 64 20 77 69 74 68  as finished with
cbb0: 20 69 74 2e 20 20 49 66 20 74 68 65 20 66 69 66   it.  If the fif
cbc0: 74 68 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74  th argument is t
cbd0: 68 65 0a 2a 2a 20 73 70 65 63 69 61 6c 20 76 61  he.** special va
cbe0: 6c 75 65 20 5b 53 51 4c 49 54 45 5f 53 54 41 54  lue [SQLITE_STAT
cbf0: 49 43 5d 2c 20 74 68 65 6e 20 74 68 65 20 6c 69  IC], then the li
cc00: 62 72 61 72 79 20 61 73 73 75 6d 65 73 20 74 68  brary assumes th
cc10: 61 74 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69  at the informati
cc20: 6f 6e 0a 2a 2a 20 69 73 20 69 6e 20 73 74 61 74  on.** is in stat
cc30: 69 63 2c 20 75 6e 6d 61 6e 61 67 65 64 20 73 70  ic, unmanaged sp
cc40: 61 63 65 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74  ace and does not
cc50: 20 6e 65 65 64 20 74 6f 20 62 65 20 66 72 65 65   need to be free
cc60: 64 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 66 69  d.  If the.** fi
cc70: 66 74 68 20 61 72 67 75 6d 65 6e 74 20 68 61 73  fth argument has
cc80: 20 74 68 65 20 76 61 6c 75 65 20 5b 53 51 4c 49   the value [SQLI
cc90: 54 45 5f 54 52 41 4e 53 49 45 4e 54 5d 2c 20 74  TE_TRANSIENT], t
cca0: 68 65 6e 20 53 51 4c 69 74 65 20 6d 61 6b 65 73  hen SQLite makes
ccb0: 20 69 74 73 0a 2a 2a 20 6f 77 6e 20 70 72 69 76   its.** own priv
ccc0: 61 74 65 20 63 6f 70 79 20 6f 66 20 74 68 65 20  ate copy of the 
ccd0: 64 61 74 61 20 69 6d 6d 65 64 69 61 74 65 6c 79  data immediately
cce0: 2c 20 62 65 66 6f 72 65 20 74 68 65 20 73 71 6c  , before the sql
ccf0: 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 0a 2a 2a  ite3_bind_*().**
cd00: 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
cd10: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
cd20: 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f  te3_bind_zeroblo
cd30: 62 28 29 20 72 6f 75 74 69 6e 65 20 62 69 6e 64  b() routine bind
cd40: 73 20 61 20 42 4c 4f 42 20 6f 66 20 6c 65 6e 67  s a BLOB of leng
cd50: 74 68 20 6e 20 74 68 61 74 0a 2a 2a 20 69 73 20  th n that.** is 
cd60: 66 69 6c 6c 65 64 20 77 69 74 68 20 7a 65 72 6f  filled with zero
cd70: 73 2e 20 20 41 20 7a 65 72 6f 62 6c 6f 62 20 75  s.  A zeroblob u
cd80: 73 65 73 20 61 20 66 69 78 65 64 20 61 6d 6f 75  ses a fixed amou
cd90: 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20  nt of memory.** 
cda0: 28 6a 75 73 74 20 61 6e 20 69 6e 74 65 67 65 72  (just an integer
cdb0: 20 74 6f 20 68 6f 6c 64 20 69 74 20 73 69 7a 65   to hold it size
cdc0: 29 20 77 68 69 6c 65 20 69 74 20 69 73 20 62 65  ) while it is be
cdd0: 69 6e 67 20 70 72 6f 63 65 73 73 65 64 2e 0a 2a  ing processed..*
cde0: 2a 20 5a 65 72 6f 62 6c 6f 62 73 20 61 72 65 20  * Zeroblobs are 
cdf0: 69 6e 74 65 6e 64 65 64 20 74 6f 20 73 65 72 76  intended to serv
ce00: 65 20 61 73 20 70 6c 61 63 65 2d 68 6f 6c 64 65  e as place-holde
ce10: 72 73 20 66 6f 72 20 42 4c 4f 42 73 20 77 68 6f  rs for BLOBs who
ce20: 73 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 69 73  se.** content is
ce30: 20 6c 61 74 65 72 20 77 72 69 74 74 65 6e 20 75   later written u
ce40: 73 69 6e 67 20 0a 2a 2a 20 5b 73 71 6c 69 74 65  sing .** [sqlite
ce50: 33 5f 62 6c 6f 62 5f 6f 70 65 6e 20 7c 20 69 6e  3_blob_open | in
ce60: 63 72 65 6d 65 6e 74 20 42 4c 4f 42 20 49 2f 4f  crement BLOB I/O
ce70: 5d 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a  ] routines..**.*
ce80: 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 69  * The sqlite3_bi
ce90: 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20  nd_*() routines 
cea0: 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 61  must be called a
ceb0: 66 74 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  fter.** [sqlite3
cec0: 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 28  _prepare_v2()] (
ced0: 61 6e 64 20 69 74 73 20 76 61 72 69 61 6e 74 73  and its variants
cee0: 29 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65  ) or [sqlite3_re
cef0: 73 65 74 28 29 5d 20 61 6e 64 0a 2a 2a 20 62 65  set()] and.** be
cf00: 66 6f 72 65 20 5b 73 71 6c 69 74 65 33 5f 73 74  fore [sqlite3_st
cf10: 65 70 28 29 5d 2e 0a 2a 2a 20 42 69 6e 64 69 6e  ep()]..** Bindin
cf20: 67 73 20 61 72 65 20 6e 6f 74 20 63 6c 65 61 72  gs are not clear
cf30: 65 64 20 62 79 20 74 68 65 20 5b 73 71 6c 69 74  ed by the [sqlit
cf40: 65 33 5f 72 65 73 65 74 28 29 5d 20 72 6f 75 74  e3_reset()] rout
cf50: 69 6e 65 2e 0a 2a 2a 20 55 6e 62 6f 75 6e 64 20  ine..** Unbound 
cf60: 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 69  parameters are i
cf70: 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 4e 55  nterpreted as NU
cf80: 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  LL..**.** These 
cf90: 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20  routines return 
cfa0: 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73  [SQLITE_OK] on s
cfb0: 75 63 63 65 73 73 20 6f 72 20 61 6e 20 65 72 72  uccess or an err
cfc0: 6f 72 20 63 6f 64 65 20 69 66 0a 2a 2a 20 61 6e  or code if.** an
cfd0: 79 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e  ything goes wron
cfe0: 67 2e 20 20 5b 53 51 4c 49 54 45 5f 52 41 4e 47  g.  [SQLITE_RANG
cff0: 45 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  E] is returned i
d000: 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 0a  f the parameter.
d010: 2a 2a 20 69 6e 64 65 78 20 69 73 20 6f 75 74 20  ** index is out 
d020: 6f 66 20 72 61 6e 67 65 2e 20 20 5b 53 51 4c 49  of range.  [SQLI
d030: 54 45 5f 4e 4f 4d 45 4d 5d 20 69 73 20 72 65 74  TE_NOMEM] is ret
d040: 75 72 6e 65 64 20 69 66 20 6d 61 6c 6c 6f 63 20  urned if malloc 
d050: 66 61 69 6c 73 2e 0a 2a 2a 20 5b 53 51 4c 49 54  fails..** [SQLIT
d060: 45 5f 4d 49 53 55 53 45 5d 20 69 73 20 72 65 74  E_MISUSE] is ret
d070: 75 72 6e 65 64 20 69 66 20 74 68 65 73 65 20 72  urned if these r
d080: 6f 75 74 69 6e 65 73 20 61 72 65 20 63 61 6c 6c  outines are call
d090: 65 64 20 6f 6e 20 61 20 76 69 72 74 75 61 6c 0a  ed on a virtual.
d0a0: 2a 2a 20 6d 61 63 68 69 6e 65 20 74 68 61 74 20  ** machine that 
d0b0: 69 73 20 74 68 65 20 77 72 6f 6e 67 20 73 74 61  is the wrong sta
d0c0: 74 65 20 6f 72 20 77 68 69 63 68 20 68 61 73 20  te or which has 
d0d0: 61 6c 72 65 61 64 79 20 62 65 65 6e 20 66 69 6e  already been fin
d0e0: 61 6c 69 7a 65 64 2e 0a 2a 2f 0a 69 6e 74 20 73  alized..*/.int s
d0f0: 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62  qlite3_bind_blob
d100: 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20  (sqlite3_stmt*, 
d110: 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a  int, const void*
d120: 2c 20 69 6e 74 20 6e 2c 20 76 6f 69 64 28 2a 29  , int n, void(*)
d130: 28 76 6f 69 64 2a 29 29 3b 0a 69 6e 74 20 73 71  (void*));.int sq
d140: 6c 69 74 65 33 5f 62 69 6e 64 5f 64 6f 75 62 6c  lite3_bind_doubl
d150: 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  e(sqlite3_stmt*,
d160: 20 69 6e 74 2c 20 64 6f 75 62 6c 65 29 3b 0a 69   int, double);.i
d170: 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
d180: 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  int(sqlite3_stmt
d190: 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 69 6e  *, int, int);.in
d1a0: 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69  t sqlite3_bind_i
d1b0: 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 73 74 6d  nt64(sqlite3_stm
d1c0: 74 2a 2c 20 69 6e 74 2c 20 73 71 6c 69 74 65 5f  t*, int, sqlite_
d1d0: 69 6e 74 36 34 29 3b 0a 69 6e 74 20 73 71 6c 69  int64);.int sqli
d1e0: 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 73 71  te3_bind_null(sq
d1f0: 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74  lite3_stmt*, int
d200: 29 3b 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  );.int sqlite3_b
d210: 69 6e 64 5f 74 65 78 74 28 73 71 6c 69 74 65 33  ind_text(sqlite3
d220: 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e  _stmt*, int, con
d230: 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 20 6e 2c  st char*, int n,
d240: 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29   void(*)(void*))
d250: 3b 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69  ;.int sqlite3_bi
d260: 6e 64 5f 74 65 78 74 31 36 28 73 71 6c 69 74 65  nd_text16(sqlite
d270: 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f  3_stmt*, int, co
d280: 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20  nst void*, int, 
d290: 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b  void(*)(void*));
d2a0: 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e  .int sqlite3_bin
d2b0: 64 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f  d_value(sqlite3_
d2c0: 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73  stmt*, int, cons
d2d0: 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  t sqlite3_value*
d2e0: 29 3b 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  );.int sqlite3_b
d2f0: 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c  ind_zeroblob(sql
d300: 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c  ite3_stmt*, int,
d310: 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a 2a 2a 20   int n);../*.** 
d320: 43 41 50 49 33 52 45 46 3a 20 4e 75 6d 62 65 72  CAPI3REF: Number
d330: 20 4f 66 20 48 6f 73 74 20 50 61 72 61 6d 65 74   Of Host Paramet
d340: 65 72 73 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  ers.**.** Return
d350: 20 74 68 65 20 6c 61 72 67 65 73 74 20 68 6f 73   the largest hos
d360: 74 20 70 61 72 61 6d 65 74 65 72 20 69 6e 64 65  t parameter inde
d370: 78 20 69 6e 20 74 68 65 20 70 72 65 63 6f 6d 70  x in the precomp
d380: 69 6c 65 64 20 73 74 61 74 65 6d 65 6e 74 20 67  iled statement g
d390: 69 76 65 6e 0a 2a 2a 20 61 73 20 74 68 65 20 61  iven.** as the a
d3a0: 72 67 75 6d 65 6e 74 2e 20 20 57 68 65 6e 20 74  rgument.  When t
d3b0: 68 65 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65  he host paramete
d3c0: 72 73 20 61 72 65 20 6f 66 20 74 68 65 20 66 6f  rs are of the fo
d3d0: 72 6d 73 20 6c 69 6b 65 20 22 3a 41 41 41 22 0a  rms like ":AAA".
d3e0: 2a 2a 20 6f 72 20 22 3f 22 2c 20 74 68 65 6e 20  ** or "?", then 
d3f0: 74 68 65 79 20 61 72 65 20 61 73 73 69 67 6e 65  they are assigne
d400: 64 20 73 65 71 75 65 6e 74 69 61 6c 20 69 6e 63  d sequential inc
d410: 72 65 61 73 69 6e 67 20 6e 75 6d 62 65 72 73 20  reasing numbers 
d420: 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 77 69 74  beginning.** wit
d430: 68 20 6f 6e 65 2c 20 73 6f 20 74 68 65 20 76 61  h one, so the va
d440: 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20  lue returned is 
d450: 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  the number of pa
d460: 72 61 6d 65 74 65 72 73 2e 20 20 48 6f 77 65 76  rameters.  Howev
d470: 65 72 0a 2a 2a 20 69 66 20 74 68 65 20 73 61 6d  er.** if the sam
d480: 65 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72  e host parameter
d490: 20 6e 61 6d 65 20 69 73 20 75 73 65 64 20 6d 75   name is used mu
d4a0: 6c 74 69 70 6c 65 20 74 69 6d 65 73 2c 20 65 61  ltiple times, ea
d4b0: 63 68 20 6f 63 63 75 72 72 61 6e 63 65 0a 2a 2a  ch occurrance.**
d4c0: 20 69 73 20 67 69 76 65 6e 20 74 68 65 20 73 61   is given the sa
d4d0: 6d 65 20 6e 75 6d 62 65 72 2c 20 73 6f 20 74 68  me number, so th
d4e0: 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  e value returned
d4f0: 20 69 6e 20 74 68 61 74 20 63 61 73 65 20 69 73   in that case is
d500: 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f   the number.** o
d510: 66 20 75 6e 69 71 75 65 20 68 6f 73 74 20 70 61  f unique host pa
d520: 72 61 6d 65 74 65 72 20 6e 61 6d 65 73 2e 20 20  rameter names.  
d530: 49 66 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65  If host paramete
d540: 72 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22  rs of the form "
d550: 3f 4e 4e 4e 22 0a 2a 2a 20 61 72 65 20 75 73 65  ?NNN".** are use
d560: 64 20 28 77 68 65 72 65 20 4e 4e 4e 20 69 73 20  d (where NNN is 
d570: 61 6e 20 69 6e 74 65 67 65 72 29 20 74 68 65 6e  an integer) then
d580: 20 74 68 65 72 65 20 6d 69 67 68 74 20 62 65 20   there might be 
d590: 67 61 70 73 20 69 6e 20 74 68 65 0a 2a 2a 20 6e  gaps in the.** n
d5a0: 75 6d 62 65 72 69 6e 67 20 61 6e 64 20 74 68 65  umbering and the
d5b0: 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
d5c0: 62 79 20 74 68 69 73 20 69 6e 74 65 72 66 61 63  by this interfac
d5d0: 65 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f  e is the index o
d5e0: 66 20 74 68 65 0a 2a 2a 20 68 6f 73 74 20 70 61  f the.** host pa
d5f0: 72 61 6d 65 74 65 72 20 77 69 74 68 20 74 68 65  rameter with the
d600: 20 6c 61 72 67 65 73 74 20 69 6e 64 65 78 20 76   largest index v
d610: 61 6c 75 65 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c  alue..*/.int sql
d620: 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
d630: 74 65 72 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65  ter_count(sqlite
d640: 33 5f 73 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  3_stmt*);../*.**
d650: 20 43 41 50 49 33 52 45 46 3a 20 4e 61 6d 65 20   CAPI3REF: Name 
d660: 4f 66 20 41 20 48 6f 73 74 20 50 61 72 61 6d 65  Of A Host Parame
d670: 74 65 72 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ter.**.** This r
d680: 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61  outine returns a
d690: 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
d6a0: 6e 61 6d 65 20 6f 66 20 74 68 65 20 6e 2d 74 68  name of the n-th
d6b0: 20 70 61 72 61 6d 65 74 65 72 20 69 6e 20 61 20   parameter in a 
d6c0: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d  .** [sqlite3_stm
d6d0: 74 20 7c 20 70 72 65 70 61 72 65 64 20 73 74 61  t | prepared sta
d6e0: 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 48 6f 73 74  tement]..** Host
d6f0: 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66 20 74   parameters of t
d700: 68 65 20 66 6f 72 6d 20 22 3a 41 41 41 22 20 6f  he form ":AAA" o
d710: 72 20 22 40 41 41 41 22 20 6f 72 20 22 24 56 56  r "@AAA" or "$VV
d720: 56 22 20 68 61 76 65 20 61 20 6e 61 6d 65 0a 2a  V" have a name.*
d730: 2a 20 77 68 69 63 68 20 69 73 20 74 68 65 20 73  * which is the s
d740: 74 72 69 6e 67 20 22 3a 41 41 41 22 20 6f 72 20  tring ":AAA" or 
d750: 22 40 41 41 41 22 20 6f 72 20 22 24 56 56 56 22  "@AAA" or "$VVV"
d760: 2e 20 20 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20  .  .** In other 
d770: 77 6f 72 64 73 2c 20 74 68 65 20 69 6e 69 74 69  words, the initi
d780: 61 6c 20 22 3a 22 20 6f 72 20 22 24 22 20 6f 72  al ":" or "$" or
d790: 20 22 40 22 0a 2a 2a 20 69 73 20 69 6e 63 6c 75   "@".** is inclu
d7a0: 64 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74  ded as part of t
d7b0: 68 65 20 6e 61 6d 65 2e 0a 2a 2a 20 50 61 72 61  he name..** Para
d7c0: 6d 65 74 65 72 73 20 6f 66 20 74 68 65 20 66 6f  meters of the fo
d7d0: 72 6d 20 22 3f 22 20 6f 72 20 22 3f 4e 4e 4e 22  rm "?" or "?NNN"
d7e0: 20 68 61 76 65 20 6e 6f 20 6e 61 6d 65 2e 0a 2a   have no name..*
d7f0: 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 62  *.** The first b
d800: 6f 75 6e 64 20 70 61 72 61 6d 65 74 65 72 20 68  ound parameter h
d810: 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 31  as an index of 1
d820: 2c 20 6e 6f 74 20 30 2e 0a 2a 2a 0a 2a 2a 20 49  , not 0..**.** I
d830: 66 20 74 68 65 20 76 61 6c 75 65 20 6e 20 69 73  f the value n is
d840: 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 6f 72   out of range or
d850: 20 69 66 20 74 68 65 20 6e 2d 74 68 20 70 61 72   if the n-th par
d860: 61 6d 65 74 65 72 20 69 73 20 6e 61 6d 65 6c 65  ameter is namele
d870: 73 73 2c 0a 2a 2a 20 74 68 65 6e 20 4e 55 4c 4c  ss,.** then NULL
d880: 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 54   is returned.  T
d890: 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69  he returned stri
d8a0: 6e 67 20 69 73 20 61 6c 77 61 79 73 20 69 6e 20  ng is always in 
d8b0: 74 68 65 0a 2a 2a 20 55 54 46 2d 38 20 65 6e 63  the.** UTF-8 enc
d8c0: 6f 64 69 6e 67 20 65 76 65 6e 20 69 66 20 74 68  oding even if th
d8d0: 65 20 6e 61 6d 65 64 20 70 61 72 61 6d 65 74 65  e named paramete
d8e0: 72 20 77 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79  r was originally
d8f0: 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 61 73   specified.** as
d900: 20 55 54 46 2d 31 36 20 69 6e 20 5b 73 71 6c 69   UTF-16 in [sqli
d910: 74 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d  te3_prepare16()]
d920: 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65   or [sqlite3_pre
d930: 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a 2f  pare16_v2()]..*/
d940: 0a 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c  .const char *sql
d950: 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
d960: 74 65 72 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33  ter_name(sqlite3
d970: 5f 73 74 6d 74 2a 2c 20 69 6e 74 29 3b 0a 0a 2f  _stmt*, int);../
d980: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 49  *.** CAPI3REF: I
d990: 6e 64 65 78 20 4f 66 20 41 20 50 61 72 61 6d 65  ndex Of A Parame
d9a0: 74 65 72 20 57 69 74 68 20 41 20 47 69 76 65 6e  ter With A Given
d9b0: 20 4e 61 6d 65 0a 2a 2a 0a 2a 2a 20 54 68 69 73   Name.**.** This
d9c0: 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
d9d0: 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 61 20   the index of a 
d9e0: 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 20 77  host parameter w
d9f0: 69 74 68 20 74 68 65 20 67 69 76 65 6e 20 6e 61  ith the given na
da00: 6d 65 2e 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20  me..** The name 
da10: 6d 75 73 74 20 6d 61 74 63 68 20 65 78 61 63 74  must match exact
da20: 6c 79 2e 20 20 49 66 20 6e 6f 20 70 61 72 61 6d  ly.  If no param
da30: 65 74 65 72 20 77 69 74 68 20 74 68 65 20 67 69  eter with the gi
da40: 76 65 6e 20 6e 61 6d 65 20 69 73 20 0a 2a 2a 20  ven name is .** 
da50: 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e 20 30 2e  found, return 0.
da60: 20 20 50 61 72 61 6d 65 74 65 72 20 6e 61 6d 65    Parameter name
da70: 73 20 6d 75 73 74 20 62 65 20 55 54 46 38 2e 0a  s must be UTF8..
da80: 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  */.int sqlite3_b
da90: 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e  ind_parameter_in
daa0: 64 65 78 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  dex(sqlite3_stmt
dab0: 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  *, const char *z
dac0: 4e 61 6d 65 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  Name);../*.** CA
dad0: 50 49 33 52 45 46 3a 20 52 65 73 65 74 20 41 6c  PI3REF: Reset Al
dae0: 6c 20 42 69 6e 64 69 6e 67 73 20 4f 6e 20 41 20  l Bindings On A 
daf0: 50 72 65 70 61 72 65 64 20 53 74 61 74 65 6d 65  Prepared Stateme
db00: 6e 74 0a 2a 2a 0a 2a 2a 20 43 6f 6e 74 72 61 72  nt.**.** Contrar
db10: 79 20 74 6f 20 74 68 65 20 69 6e 74 75 69 74 69  y to the intuiti
db20: 6f 6e 20 6f 66 20 6d 61 6e 79 2c 20 5b 73 71 6c  on of many, [sql
db30: 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 64 6f  ite3_reset()] do
db40: 65 73 20 6e 6f 74 0a 2a 2a 20 72 65 73 65 74 20  es not.** reset 
db50: 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e  the [sqlite3_bin
db60: 64 5f 62 6c 6f 62 20 7c 20 62 69 6e 64 69 6e 67  d_blob | binding
db70: 73 5d 20 6f 6e 20 61 20 0a 2a 2a 20 5b 73 71 6c  s] on a .** [sql
db80: 69 74 65 33 5f 73 74 6d 74 20 7c 20 70 72 65 70  ite3_stmt | prep
db90: 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e  ared statement].
dba0: 20 20 55 73 65 20 74 68 69 73 20 72 6f 75 74 69    Use this routi
dbb0: 6e 65 20 74 6f 0a 2a 2a 20 72 65 73 65 74 20 61  ne to.** reset a
dbc0: 6c 6c 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65  ll host paramete
dbd0: 72 73 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 69  rs to NULL..*/.i
dbe0: 6e 74 20 73 71 6c 69 74 65 33 5f 63 6c 65 61 72  nt sqlite3_clear
dbf0: 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 65  _bindings(sqlite
dc00: 33 5f 73 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  3_stmt*);../*.**
dc10: 20 43 41 50 49 33 52 45 46 3a 20 4e 75 6d 62 65   CAPI3REF: Numbe
dc20: 72 20 4f 66 20 43 6f 6c 75 6d 6e 73 20 49 6e 20  r Of Columns In 
dc30: 41 20 52 65 73 75 6c 74 20 53 65 74 0a 2a 2a 0a  A Result Set.**.
dc40: 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
dc50: 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
dc60: 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  in the result se
dc70: 74 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68  t returned by th
dc80: 65 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73  e .** [sqlite3_s
dc90: 74 6d 74 20 7c 20 63 6f 6d 70 69 6c 65 64 20 53  tmt | compiled S
dca0: 51 4c 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 54  QL statement]. T
dcb0: 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
dcc0: 72 6e 73 20 30 0a 2a 2a 20 69 66 20 70 53 74 6d  rns 0.** if pStm
dcd0: 74 20 69 73 20 61 6e 20 53 51 4c 20 73 74 61 74  t is an SQL stat
dce0: 65 6d 65 6e 74 20 74 68 61 74 20 64 6f 65 73 20  ement that does 
dcf0: 6e 6f 74 20 72 65 74 75 72 6e 20 64 61 74 61 20  not return data 
dd00: 28 66 6f 72 20 0a 2a 2a 20 65 78 61 6d 70 6c 65  (for .** example
dd10: 20 61 6e 20 55 50 44 41 54 45 29 2e 0a 2a 2f 0a   an UPDATE)..*/.
dd20: 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  int sqlite3_colu
dd30: 6d 6e 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33  mn_count(sqlite3
dd40: 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a  _stmt *pStmt);..
dd50: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
dd60: 43 6f 6c 75 6d 6e 20 4e 61 6d 65 73 20 49 6e 20  Column Names In 
dd70: 41 20 52 65 73 75 6c 74 20 53 65 74 0a 2a 2a 0a  A Result Set.**.
dd80: 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  ** These routine
dd90: 73 20 72 65 74 75 72 6e 20 74 68 65 20 6e 61 6d  s return the nam
dda0: 65 20 61 73 73 69 67 6e 65 64 20 74 6f 20 61 20  e assigned to a 
ddb0: 70 61 72 74 69 63 75 6c 61 72 20 63 6f 6c 75 6d  particular colum
ddc0: 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 75  n.** in the resu
ddd0: 6c 74 20 73 65 74 20 6f 66 20 61 20 53 45 4c 45  lt set of a SELE
dde0: 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54  CT statement.  T
ddf0: 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  he sqlite3_colum
de00: 6e 5f 6e 61 6d 65 28 29 0a 2a 2a 20 69 6e 74 65  n_name().** inte
de10: 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 20  rface returns a 
de20: 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 55 54 46  pointer to a UTF
de30: 38 20 73 74 72 69 6e 67 20 61 6e 64 20 73 71 6c  8 string and sql
de40: 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65  ite3_column_name
de50: 31 36 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20  16().** returns 
de60: 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 55  a pointer to a U
de70: 54 46 31 36 20 73 74 72 69 6e 67 2e 20 20 54 68  TF16 string.  Th
de80: 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  e first paramete
de90: 72 20 69 73 20 74 68 65 0a 2a 2a 20 5b 73 71 6c  r is the.** [sql
dea0: 69 74 65 5f 73 74 6d 74 20 7c 20 70 72 65 70 61  ite_stmt | prepa
deb0: 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 74  red statement] t
dec0: 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74  hat implements t
ded0: 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
dee0: 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f  ent..** The seco
def0: 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  nd parameter is 
df00: 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65  the column numbe
df10: 72 2e 20 20 54 68 65 20 6c 65 66 74 2d 6d 6f 73  r.  The left-mos
df20: 74 20 63 6f 6c 75 6d 6e 20 69 73 0a 2a 2a 20 6e  t column is.** n
df30: 75 6d 62 65 72 20 30 2e 0a 2a 2a 0a 2a 2a 20 54  umber 0..**.** T
df40: 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69  he returned stri
df50: 6e 67 20 70 6f 69 6e 74 65 72 20 69 73 20 76 61  ng pointer is va
df60: 6c 69 64 20 75 6e 74 69 6c 20 65 69 74 68 65 72  lid until either
df70: 20 74 68 65 20 0a 2a 2a 20 5b 73 71 6c 69 74 65   the .** [sqlite
df80: 5f 73 74 6d 74 20 7c 20 70 72 65 70 61 72 65 64  _stmt | prepared
df90: 20 73 74 61 74 65 6d 65 6e 74 5d 20 69 73 20 64   statement] is d
dfa0: 65 73 74 72 6f 79 65 64 20 62 79 20 5b 73 71 6c  estroyed by [sql
dfb0: 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d  ite3_finalize()]
dfc0: 0a 2a 2a 20 6f 72 20 75 6e 74 69 6c 20 74 68 65  .** or until the
dfd0: 20 6e 65 78 74 20 63 61 6c 6c 20 73 71 6c 69 74   next call sqlit
dfe0: 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 29  e3_column_name()
dff0: 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   or sqlite3_colu
e000: 6d 6e 5f 6e 61 6d 65 31 36 28 29 0a 2a 2a 20 6f  mn_name16().** o
e010: 6e 20 74 68 65 20 73 61 6d 65 20 63 6f 6c 75 6d  n the same colum
e020: 6e 2e 0a 2a 2f 0a 63 6f 6e 73 74 20 63 68 61 72  n..*/.const char
e030: 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
e040: 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74  _name(sqlite3_st
e050: 6d 74 2a 2c 20 69 6e 74 20 4e 29 3b 0a 63 6f 6e  mt*, int N);.con
e060: 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
e070: 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 73  _column_name16(s
e080: 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
e090: 74 20 4e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  t N);../*.** CAP
e0a0: 49 33 52 45 46 3a 20 53 6f 75 72 63 65 20 4f 66  I3REF: Source Of
e0b0: 20 44 61 74 61 20 49 6e 20 41 20 51 75 65 72 79   Data In A Query
e0c0: 20 52 65 73 75 6c 74 0a 2a 2a 0a 2a 2a 20 54 68   Result.**.** Th
e0d0: 65 73 65 20 72 6f 75 74 69 6e 65 73 20 70 72 6f  ese routines pro
e0e0: 76 69 64 65 20 61 20 6d 65 61 6e 73 20 74 6f 20  vide a means to 
e0f0: 64 65 74 65 72 6d 69 6e 65 20 77 68 61 74 20 63  determine what c
e100: 6f 6c 75 6d 6e 20 6f 66 20 77 68 61 74 0a 2a 2a  olumn of what.**
e110: 20 74 61 62 6c 65 20 69 6e 20 77 68 69 63 68 20   table in which 
e120: 64 61 74 61 62 61 73 65 20 61 20 72 65 73 75 6c  database a resul
e130: 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74  t of a SELECT st
e140: 61 74 65 6d 65 6e 74 20 63 6f 6d 65 73 20 66 72  atement comes fr
e150: 6f 6d 2e 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20  om..** The name 
e160: 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
e170: 6f 72 20 74 61 62 6c 65 20 6f 72 20 63 6f 6c 75  or table or colu
e180: 6d 6e 20 63 61 6e 20 62 65 20 72 65 74 75 72 6e  mn can be return
e190: 65 64 20 61 73 0a 2a 2a 20 65 69 74 68 65 72 20  ed as.** either 
e1a0: 61 20 55 54 46 38 20 6f 72 20 55 54 46 31 36 20  a UTF8 or UTF16 
e1b0: 73 74 72 69 6e 67 2e 20 20 54 68 65 20 5f 64 61  string.  The _da
e1c0: 74 61 62 61 73 65 5f 20 72 6f 75 74 69 6e 65 73  tabase_ routines
e1d0: 20 72 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 64   return.** the d
e1e0: 61 74 61 62 61 73 65 20 6e 61 6d 65 2c 20 74 68  atabase name, th
e1f0: 65 20 5f 74 61 62 6c 65 5f 20 72 6f 75 74 69 6e  e _table_ routin
e200: 65 73 20 72 65 74 75 72 6e 20 74 68 65 20 74 61  es return the ta
e210: 62 6c 65 20 6e 61 6d 65 2c 20 61 6e 64 0a 2a 2a  ble name, and.**
e220: 20 74 68 65 20 6f 72 69 67 69 6e 5f 20 72 6f 75   the origin_ rou
e230: 74 69 6e 65 73 20 72 65 74 75 72 6e 20 74 68 65  tines return the
e240: 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2e 0a 2a 2a   column name..**
e250: 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 73 74   The returned st
e260: 72 69 6e 67 20 69 73 20 76 61 6c 69 64 20 75 6e  ring is valid un
e270: 74 69 6c 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69  til.** the [sqli
e280: 74 65 33 5f 73 74 6d 74 20 7c 20 70 72 65 70 61  te3_stmt | prepa
e290: 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 69  red statement] i
e2a0: 73 20 64 65 73 74 72 6f 79 65 64 20 75 73 69 6e  s destroyed usin
e2b0: 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69  g.** [sqlite3_fi
e2c0: 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 20 75 6e 74  nalize()] or unt
e2d0: 69 6c 20 74 68 65 20 73 61 6d 65 20 69 6e 66 6f  il the same info
e2e0: 72 6d 61 74 69 6f 6e 20 69 73 20 72 65 71 75 65  rmation is reque
e2f0: 73 74 65 64 0a 2a 2a 20 61 67 61 69 6e 20 69 6e  sted.** again in
e300: 20 61 20 64 69 66 66 65 72 65 6e 74 20 65 6e 63   a different enc
e310: 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  oding..**.** The
e320: 20 6e 61 6d 65 73 20 72 65 74 75 72 6e 65 64 20   names returned 
e330: 61 72 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  are the original
e340: 20 75 6e 2d 61 6c 69 61 73 65 64 20 6e 61 6d 65   un-aliased name
e350: 73 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61  s of the.** data
e360: 62 61 73 65 2c 20 74 61 62 6c 65 2c 20 61 6e 64  base, table, and
e370: 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 54   column..**.** T
e380: 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
e390: 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69  t to the followi
e3a0: 6e 67 20 63 61 6c 6c 73 20 69 73 20 61 20 0a 2a  ng calls is a .*
e3b0: 2a 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  * [sqlite3_stmt 
e3c0: 7c 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73  | compiled SQL s
e3d0: 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 54 68  tatement]..** Th
e3e0: 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 72 65  ese functions re
e3f0: 74 75 72 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  turn information
e400: 20 61 62 6f 75 74 20 74 68 65 20 4e 74 68 20 63   about the Nth c
e410: 6f 6c 75 6d 6e 20 72 65 74 75 72 6e 65 64 20 62  olumn returned b
e420: 79 20 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 6d  y .** the statem
e430: 65 6e 74 2c 20 77 68 65 72 65 20 4e 20 69 73 20  ent, where N is 
e440: 74 68 65 20 73 65 63 6f 6e 64 20 66 75 6e 63 74  the second funct
e450: 69 6f 6e 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a  ion argument..**
e460: 0a 2a 2a 20 49 66 20 74 68 65 20 4e 74 68 20 63  .** If the Nth c
e470: 6f 6c 75 6d 6e 20 72 65 74 75 72 6e 65 64 20 62  olumn returned b
e480: 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  y the statement 
e490: 69 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  is an expression
e4a0: 0a 2a 2a 20 6f 72 20 73 75 62 71 75 65 72 79 20  .** or subquery 
e4b0: 61 6e 64 20 69 73 20 6e 6f 74 20 61 20 63 6f 6c  and is not a col
e4c0: 75 6d 6e 20 76 61 6c 75 65 2c 20 74 68 65 6e 20  umn value, then 
e4d0: 61 6c 6c 20 6f 66 20 74 68 65 73 65 20 66 75 6e  all of these fun
e4e0: 63 74 69 6f 6e 73 0a 2a 2a 20 72 65 74 75 72 6e  ctions.** return
e4f0: 20 4e 55 4c 4c 2e 20 4f 74 68 65 72 77 69 73 65   NULL. Otherwise
e500: 2c 20 74 68 65 79 20 72 65 74 75 72 6e 20 74 68  , they return th
e510: 65 20 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68  e .** name of th
e520: 65 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62  e attached datab
e530: 61 73 65 2c 20 74 61 62 6c 65 20 61 6e 64 20 63  ase, table and c
e540: 6f 6c 75 6d 6e 20 74 68 61 74 20 71 75 65 72 79  olumn that query
e550: 20 72 65 73 75 6c 74 0a 2a 2a 20 63 6f 6c 75 6d   result.** colum
e560: 6e 20 77 61 73 20 65 78 74 72 61 63 74 65 64 20  n was extracted 
e570: 66 72 6f 6d 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 77  from..**.** As w
e580: 69 74 68 20 61 6c 6c 20 6f 74 68 65 72 20 53 51  ith all other SQ
e590: 4c 69 74 65 20 41 50 49 73 2c 20 74 68 6f 73 65  Lite APIs, those
e5a0: 20 70 6f 73 74 66 69 78 65 64 20 77 69 74 68 20   postfixed with 
e5b0: 22 31 36 22 20 72 65 74 75 72 6e 20 55 54 46 2d  "16" return UTF-
e5c0: 31 36 0a 2a 2a 20 65 6e 63 6f 64 65 64 20 73 74  16.** encoded st
e5d0: 72 69 6e 67 73 2c 20 74 68 65 20 6f 74 68 65 72  rings, the other
e5e0: 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72   functions retur
e5f0: 6e 20 55 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 54  n UTF-8..**.** T
e600: 68 65 73 65 20 41 50 49 73 20 61 72 65 20 6f 6e  hese APIs are on
e610: 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 66 20  ly available if 
e620: 74 68 65 20 6c 69 62 72 61 72 79 20 77 61 73 20  the library was 
e630: 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68  compiled with th
e640: 65 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 45 4e 41  e .** SQLITE_ENA
e650: 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44  BLE_COLUMN_METAD
e660: 41 54 41 20 70 72 65 70 72 6f 63 65 73 73 6f 72  ATA preprocessor
e670: 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64 2e   symbol defined.
e680: 0a 2a 2f 0a 63 6f 6e 73 74 20 63 68 61 72 20 2a  .*/.const char *
e690: 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64  sqlite3_column_d
e6a0: 61 74 61 62 61 73 65 5f 6e 61 6d 65 28 73 71 6c  atabase_name(sql
e6b0: 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b  ite3_stmt*,int);
e6c0: 0a 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c  .const void *sql
e6d0: 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61  ite3_column_data
e6e0: 62 61 73 65 5f 6e 61 6d 65 31 36 28 73 71 6c 69  base_name16(sqli
e6f0: 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a  te3_stmt*,int);.
e700: 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
e710: 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65  te3_column_table
e720: 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74  _name(sqlite3_st
e730: 6d 74 2a 2c 69 6e 74 29 3b 0a 63 6f 6e 73 74 20  mt*,int);.const 
e740: 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f  void *sqlite3_co
e750: 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31  lumn_table_name1
e760: 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  6(sqlite3_stmt*,
e770: 69 6e 74 29 3b 0a 63 6f 6e 73 74 20 63 68 61 72  int);.const char
e780: 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
e790: 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 28 73 71 6c  _origin_name(sql
e7a0: 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b  ite3_stmt*,int);
e7b0: 0a 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c  .const void *sql
e7c0: 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67  ite3_column_orig
e7d0: 69 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65  in_name16(sqlite
e7e0: 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 0a 2f  3_stmt*,int);../
e7f0: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44  *.** CAPI3REF: D
e800: 65 63 6c 61 72 65 64 20 44 61 74 61 74 79 70 65  eclared Datatype
e810: 20 4f 66 20 41 20 51 75 65 72 79 20 52 65 73 75   Of A Query Resu
e820: 6c 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72  lt.**.** The fir
e830: 73 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  st parameter is 
e840: 61 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  a [sqlite3_stmt 
e850: 7c 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73  | compiled SQL s
e860: 74 61 74 65 6d 65 6e 74 5d 2e 20 0a 2a 2a 20 49  tatement]. .** I
e870: 66 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74  f this statement
e880: 20 69 73 20 61 20 53 45 4c 45 43 54 20 73 74 61   is a SELECT sta
e890: 74 65 6d 65 6e 74 20 61 6e 64 20 74 68 65 20 4e  tement and the N
e8a0: 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  th column of the
e8b0: 20 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 72 65   .** returned re
e8c0: 73 75 6c 74 20 73 65 74 20 20 6f 66 20 74 68 61  sult set  of tha
e8d0: 74 20 53 45 4c 45 43 54 20 69 73 20 61 20 74 61  t SELECT is a ta
e8e0: 62 6c 65 20 63 6f 6c 75 6d 6e 20 28 6e 6f 74 20  ble column (not 
e8f0: 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e  an.** expression
e900: 20 6f 72 20 73 75 62 71 75 65 72 79 29 20 74 68   or subquery) th
e910: 65 6e 20 74 68 65 20 64 65 63 6c 61 72 65 64 20  en the declared 
e920: 74 79 70 65 20 6f 66 20 74 68 65 20 74 61 62 6c  type of the tabl
e930: 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 69 73 20 72  e.** column is r
e940: 65 74 75 72 6e 65 64 2e 20 49 66 20 74 68 65 20  eturned. If the 
e950: 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  Nth column of th
e960: 65 20 72 65 73 75 6c 74 20 73 65 74 20 69 73 20  e result set is 
e970: 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e  an.** expression
e980: 20 6f 72 20 73 75 62 71 75 65 72 79 2c 20 74 68   or subquery, th
e990: 65 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  en a NULL pointe
e9a0: 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  r is returned..*
e9b0: 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 73  * The returned s
e9c0: 74 72 69 6e 67 20 69 73 20 61 6c 77 61 79 73 20  tring is always 
e9d0: 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 2e 20 46  UTF-8 encoded. F
e9e0: 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 6e 0a 2a  or example, in.*
e9f0: 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  * the database s
ea00: 63 68 65 6d 61 3a 0a 2a 2a 0a 2a 2a 20 43 52 45  chema:.**.** CRE
ea10: 41 54 45 20 54 41 42 4c 45 20 74 31 28 63 31 20  ATE TABLE t1(c1 
ea20: 56 41 52 49 41 4e 54 29 3b 0a 2a 2a 0a 2a 2a 20  VARIANT);.**.** 
ea30: 41 6e 64 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  And the followin
ea40: 67 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70  g statement comp
ea50: 69 6c 65 64 3a 0a 2a 2a 0a 2a 2a 20 53 45 4c 45  iled:.**.** SELE
ea60: 43 54 20 63 31 20 2b 20 31 2c 20 63 31 20 46 52  CT c1 + 1, c1 FR
ea70: 4f 4d 20 74 31 3b 0a 2a 2a 0a 2a 2a 20 54 68 65  OM t1;.**.** The
ea80: 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  n this routine w
ea90: 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20  ould return the 
eaa0: 73 74 72 69 6e 67 20 22 56 41 52 49 41 4e 54 22  string "VARIANT"
eab0: 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 0a   for the second.
eac0: 2a 2a 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e  ** result column
ead0: 20 28 69 3d 3d 31 29 2c 20 61 6e 64 20 61 20 4e   (i==1), and a N
eae0: 55 4c 4c 20 70 6f 69 6e 74 65 72 20 66 6f 72 20  ULL pointer for 
eaf0: 74 68 65 20 66 69 72 73 74 20 72 65 73 75 6c 74  the first result
eb00: 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 28 69 3d 3d 30   column.** (i==0
eb10: 29 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  )..**.** SQLite 
eb20: 75 73 65 73 20 64 79 6e 61 6d 69 63 20 72 75 6e  uses dynamic run
eb30: 2d 74 69 6d 65 20 74 79 70 69 6e 67 2e 20 20 53  -time typing.  S
eb40: 6f 20 6a 75 73 74 20 62 65 63 61 75 73 65 20 61  o just because a
eb50: 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 73 20 64 65   column.** is de
eb60: 63 6c 61 72 65 64 20 74 6f 20 63 6f 6e 74 61 69  clared to contai
eb70: 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 74  n a particular t
eb80: 79 70 65 20 64 6f 65 73 20 6e 6f 74 20 6d 65 61  ype does not mea
eb90: 6e 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 64 61  n that the.** da
eba0: 74 61 20 73 74 6f 72 65 64 20 69 6e 20 74 68 61  ta stored in tha
ebb0: 74 20 63 6f 6c 75 6d 6e 20 69 73 20 6f 66 20 74  t column is of t
ebc0: 68 65 20 64 65 63 6c 61 72 65 64 20 74 79 70 65  he declared type
ebd0: 2e 20 20 53 51 4c 69 74 65 20 69 73 0a 2a 2a 20  .  SQLite is.** 
ebe0: 73 74 72 6f 6e 67 6c 79 20 74 79 70 65 64 2c 20  strongly typed, 
ebf0: 62 75 74 20 74 68 65 20 74 79 70 69 6e 67 20 69  but the typing i
ec00: 73 20 64 79 6e 61 6d 69 63 20 6e 6f 74 20 73 74  s dynamic not st
ec10: 61 74 69 63 2e 20 20 54 79 70 65 0a 2a 2a 20 69  atic.  Type.** i
ec20: 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
ec30: 68 20 69 6e 64 69 76 69 64 75 61 6c 20 76 61 6c  h individual val
ec40: 75 65 73 2c 20 6e 6f 74 20 77 69 74 68 20 74 68  ues, not with th
ec50: 65 20 63 6f 6e 74 61 69 6e 65 72 73 0a 2a 2a 20  e containers.** 
ec60: 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 6f  used to hold tho
ec70: 73 65 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 63 6f  se values..*/.co
ec80: 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
ec90: 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70  3_column_decltyp
eca0: 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  e(sqlite3_stmt *
ecb0: 2c 20 69 6e 74 20 69 29 3b 0a 63 6f 6e 73 74 20  , int i);.const 
ecc0: 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f  void *sqlite3_co
ecd0: 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 28  lumn_decltype16(
ece0: 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
ecf0: 74 29 3b 0a 0a 2f 2a 20 0a 2a 2a 20 43 41 50 49  t);../* .** CAPI
ed00: 33 52 45 46 3a 20 20 45 76 61 6c 75 61 74 65 20  3REF:  Evaluate 
ed10: 41 6e 20 53 51 4c 20 53 74 61 74 65 6d 65 6e 74  An SQL Statement
ed20: 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 61 6e 20  .**.** After an 
ed30: 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 7c 20  [sqlite3_stmt | 
ed40: 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 5d 20 68  SQL statement] h
ed50: 61 73 20 62 65 65 6e 20 70 72 65 70 61 72 65 64  as been prepared
ed60: 20 77 69 74 68 20 61 20 63 61 6c 6c 0a 2a 2a 20   with a call.** 
ed70: 74 6f 20 65 69 74 68 65 72 20 5b 73 71 6c 69 74  to either [sqlit
ed80: 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
ed90: 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65   or [sqlite3_pre
eda0: 70 61 72 65 31 36 5f 76 32 28 29 5d 20 6f 72 20  pare16_v2()] or 
edb0: 74 6f 20 6f 6e 65 20 6f 66 0a 2a 2a 20 74 68 65  to one of.** the
edc0: 20 6c 65 67 61 63 79 20 69 6e 74 65 72 66 61 63   legacy interfac
edd0: 65 73 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  es [sqlite3_prep
ede0: 61 72 65 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74  are()] or [sqlit
edf0: 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d 2c  e3_prepare16()],
ee00: 0a 2a 2a 20 74 68 65 6e 20 74 68 69 73 20 66 75  .** then this fu
ee10: 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 63  nction must be c
ee20: 61 6c 6c 65 64 20 6f 6e 65 20 6f 72 20 6d 6f 72  alled one or mor
ee30: 65 20 74 69 6d 65 73 20 74 6f 20 65 76 61 6c 75  e times to evalu
ee40: 61 74 65 20 74 68 65 20 0a 2a 2a 20 73 74 61 74  ate the .** stat
ee50: 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ement..**.** The
ee60: 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68 65 20   details of the 
ee70: 62 65 68 61 76 69 6f 72 20 6f 66 20 74 68 69 73  behavior of this
ee80: 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20   sqlite3_step() 
ee90: 69 6e 74 65 72 66 61 63 65 20 64 65 70 65 6e 64  interface depend
eea0: 0a 2a 2a 20 6f 6e 20 77 68 65 74 68 65 72 20 74  .** on whether t
eeb0: 68 65 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73  he statement was
eec0: 20 70 72 65 70 61 72 65 64 20 75 73 69 6e 67 20   prepared using 
eed0: 74 68 65 20 6e 65 77 65 72 20 22 76 32 22 20 69  the newer "v2" i
eee0: 6e 74 65 72 66 61 63 65 0a 2a 2a 20 5b 73 71 6c  nterface.** [sql
eef0: 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
ef00: 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  )] and [sqlite3_
ef10: 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d 20  prepare16_v2()] 
ef20: 6f 72 20 74 68 65 20 6f 6c 64 65 72 20 6c 65 67  or the older leg
ef30: 61 63 79 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  acy.** interface
ef40: 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
ef50: 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  e()] and [sqlite
ef60: 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d 2e 20  3_prepare16()]. 
ef70: 20 54 68 65 20 75 73 65 20 6f 66 20 74 68 65 0a   The use of the.
ef80: 2a 2a 20 6e 65 77 20 22 76 32 22 20 69 6e 74 65  ** new "v2" inte
ef90: 72 66 61 63 65 20 69 73 20 72 65 63 6f 6d 6d 65  rface is recomme
efa0: 6e 64 65 64 20 66 6f 72 20 6e 65 77 20 61 70 70  nded for new app
efb0: 6c 69 63 61 74 69 6f 6e 73 20 62 75 74 20 74 68  lications but th
efc0: 65 20 6c 65 67 61 63 79 0a 2a 2a 20 69 6e 74 65  e legacy.** inte
efd0: 72 66 61 63 65 20 77 69 6c 6c 20 63 6f 6e 74 69  rface will conti
efe0: 6e 75 65 20 74 6f 20 62 65 20 73 75 70 70 6f 72  nue to be suppor
eff0: 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68  ted..**.** In th
f000: 65 20 6c 61 67 61 63 79 20 69 6e 74 65 72 66 61  e lagacy interfa
f010: 63 65 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76  ce, the return v
f020: 61 6c 75 65 20 77 69 6c 6c 20 62 65 20 65 69 74  alue will be eit
f030: 68 65 72 20 5b 53 51 4c 49 54 45 5f 42 55 53 59  her [SQLITE_BUSY
f040: 5d 2c 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44  ], .** [SQLITE_D
f050: 4f 4e 45 5d 2c 20 5b 53 51 4c 49 54 45 5f 52 4f  ONE], [SQLITE_RO
f060: 57 5d 2c 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f  W], [SQLITE_ERRO
f070: 52 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4d  R], or [SQLITE_M
f080: 49 53 55 53 45 5d 2e 0a 2a 2a 20 57 69 74 68 20  ISUSE]..** With 
f090: 74 68 65 20 22 76 32 22 20 69 6e 74 65 72 66 61  the "v2" interfa
f0a0: 63 65 2c 20 61 6e 79 20 6f 66 20 74 68 65 20 6f  ce, any of the o
f0b0: 74 68 65 72 20 5b 53 51 4c 49 54 45 5f 4f 4b 20  ther [SQLITE_OK 
f0c0: 7c 20 72 65 73 75 6c 74 20 63 6f 64 65 5d 0a 2a  | result code].*
f0d0: 2a 20 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45  * or [SQLITE_IOE
f0e0: 52 52 5f 52 45 41 44 20 7c 20 65 78 74 65 6e 64  RR_READ | extend
f0f0: 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 5d 20  ed result code] 
f100: 6d 69 67 68 74 20 62 65 20 72 65 74 75 72 6e 65  might be returne
f110: 64 20 61 73 0a 2a 2a 20 77 65 6c 6c 2e 0a 2a 2a  d as.** well..**
f120: 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 42 55 53 59  .** [SQLITE_BUSY
f130: 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  ] means that the
f140: 20 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65   database engine
f150: 20 77 61 73 20 75 6e 61 62 6c 65 20 74 6f 20 61   was unable to a
f160: 63 71 75 69 72 65 20 74 68 65 0a 2a 2a 20 64 61  cquire the.** da
f170: 74 61 62 61 73 65 20 6c 6f 63 6b 73 20 69 74 20  tabase locks it 
f180: 6e 65 65 64 73 20 74 6f 20 64 6f 20 69 74 73 20  needs to do its 
f190: 6a 6f 62 2e 20 20 49 66 20 74 68 65 20 73 74 61  job.  If the sta
f1a0: 74 65 6d 65 6e 74 20 69 73 20 61 20 43 4f 4d 4d  tement is a COMM
f1b0: 49 54 0a 2a 2a 20 6f 72 20 6f 63 63 75 72 73 20  IT.** or occurs 
f1c0: 6f 75 74 73 69 64 65 20 6f 66 20 61 6e 20 65 78  outside of an ex
f1d0: 70 6c 69 63 69 74 20 74 72 61 6e 73 61 63 74 69  plicit transacti
f1e0: 6f 6e 2c 20 74 68 65 6e 20 79 6f 75 20 63 61 6e  on, then you can
f1f0: 20 72 65 74 72 79 20 74 68 65 0a 2a 2a 20 73 74   retry the.** st
f200: 61 74 65 6d 65 6e 74 2e 20 20 49 66 20 74 68 65  atement.  If the
f210: 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6e 6f   statement is no
f220: 74 20 61 20 43 4f 4d 4d 49 54 20 61 6e 64 20 6f  t a COMMIT and o
f230: 63 63 75 72 73 20 77 69 74 68 69 6e 20 61 0a 2a  ccurs within a.*
f240: 2a 20 65 78 70 6c 69 63 69 74 20 74 72 61 6e 73  * explicit trans
f250: 61 63 74 69 6f 6e 20 74 68 65 6e 20 79 6f 75 20  action then you 
f260: 73 68 6f 75 6c 64 20 72 6f 6c 6c 62 61 63 6b 20  should rollback 
f270: 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
f280: 62 65 66 6f 72 65 0a 2a 2a 20 63 6f 6e 74 69 6e  before.** contin
f290: 75 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c  uing..**.** [SQL
f2a0: 49 54 45 5f 44 4f 4e 45 5d 20 6d 65 61 6e 73 20  ITE_DONE] means 
f2b0: 74 68 61 74 20 74 68 65 20 73 74 61 74 65 6d 65  that the stateme
f2c0: 6e 74 20 68 61 73 20 66 69 6e 69 73 68 65 64 20  nt has finished 
f2d0: 65 78 65 63 75 74 69 6e 67 0a 2a 2a 20 73 75 63  executing.** suc
f2e0: 63 65 73 73 66 75 6c 6c 79 2e 20 20 73 71 6c 69  cessfully.  sqli
f2f0: 74 65 33 5f 73 74 65 70 28 29 20 73 68 6f 75 6c  te3_step() shoul
f300: 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20  d not be called 
f310: 61 67 61 69 6e 20 6f 6e 20 74 68 69 73 20 76 69  again on this vi
f320: 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65  rtual.** machine
f330: 20 77 69 74 68 6f 75 74 20 66 69 72 73 74 20 63   without first c
f340: 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f  alling [sqlite3_
f350: 72 65 73 65 74 28 29 5d 20 74 6f 20 72 65 73 65  reset()] to rese
f360: 74 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a  t the virtual.**
f370: 20 6d 61 63 68 69 6e 65 20 62 61 63 6b 20 74 6f   machine back to
f380: 20 69 74 73 20 69 6e 69 74 69 61 6c 20 73 74 61   its initial sta
f390: 74 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  te..**.** If the
f3a0: 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 62   SQL statement b
f3b0: 65 69 6e 67 20 65 78 65 63 75 74 65 64 20 72 65  eing executed re
f3c0: 74 75 72 6e 73 20 61 6e 79 20 64 61 74 61 2c 20  turns any data, 
f3d0: 74 68 65 6e 20 0a 2a 2a 20 5b 53 51 4c 49 54 45  then .** [SQLITE
f3e0: 5f 52 4f 57 5d 20 69 73 20 72 65 74 75 72 6e 65  _ROW] is returne
f3f0: 64 20 65 61 63 68 20 74 69 6d 65 20 61 20 6e 65  d each time a ne
f400: 77 20 72 6f 77 20 6f 66 20 64 61 74 61 20 69 73  w row of data is
f410: 20 72 65 61 64 79 0a 2a 2a 20 66 6f 72 20 70 72   ready.** for pr
f420: 6f 63 65 73 73 69 6e 67 20 62 79 20 74 68 65 20  ocessing by the 
f430: 63 61 6c 6c 65 72 2e 20 54 68 65 20 76 61 6c 75  caller. The valu
f440: 65 73 20 6d 61 79 20 62 65 20 61 63 63 65 73 73  es may be access
f450: 65 64 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20  ed using.** the 
f460: 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  [sqlite3_column_
f470: 69 6e 74 20 7c 20 63 6f 6c 75 6d 6e 20 61 63 63  int | column acc
f480: 65 73 73 20 66 75 6e 63 74 69 6f 6e 73 5d 2e 0a  ess functions]..
f490: 2a 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  ** sqlite3_step(
f4a0: 29 20 69 73 20 63 61 6c 6c 65 64 20 61 67 61 69  ) is called agai
f4b0: 6e 20 74 6f 20 72 65 74 72 69 65 76 65 20 74 68  n to retrieve th
f4c0: 65 20 6e 65 78 74 20 72 6f 77 20 6f 66 20 64 61  e next row of da
f4d0: 74 61 2e 0a 2a 2a 20 0a 2a 2a 20 5b 53 51 4c 49  ta..** .** [SQLI
f4e0: 54 45 5f 45 52 52 4f 52 5d 20 6d 65 61 6e 73 20  TE_ERROR] means 
f4f0: 74 68 61 74 20 61 20 72 75 6e 2d 74 69 6d 65 20  that a run-time 
f500: 65 72 72 6f 72 20 28 73 75 63 68 20 61 73 20 61  error (such as a
f510: 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 76   constraint.** v
f520: 69 6f 6c 61 74 69 6f 6e 29 20 68 61 73 20 6f 63  iolation) has oc
f530: 63 75 72 72 65 64 2e 20 20 73 71 6c 69 74 65 33  curred.  sqlite3
f540: 5f 73 74 65 70 28 29 20 73 68 6f 75 6c 64 20 6e  _step() should n
f550: 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 61 67 61  ot be called aga
f560: 69 6e 20 6f 6e 0a 2a 2a 20 74 68 65 20 56 4d 2e  in on.** the VM.
f570: 20 4d 6f 72 65 20 69 6e 66 6f 72 6d 61 74 69 6f   More informatio
f580: 6e 20 6d 61 79 20 62 65 20 66 6f 75 6e 64 20 62  n may be found b
f590: 79 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74  y calling [sqlit
f5a0: 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e 0a 2a 2a  e3_errmsg()]..**
f5b0: 20 57 69 74 68 20 74 68 65 20 6c 65 67 61 63 79   With the legacy
f5c0: 20 69 6e 74 65 72 66 61 63 65 2c 20 61 20 6d 6f   interface, a mo
f5d0: 72 65 20 73 70 65 63 69 66 69 63 20 65 72 72 6f  re specific erro
f5e0: 72 20 63 6f 64 65 20 28 65 78 61 6d 70 6c 65 3a  r code (example:
f5f0: 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45  .** [SQLITE_INTE
f600: 52 52 55 50 54 5d 2c 20 5b 53 51 4c 49 54 45 5f  RRUPT], [SQLITE_
f610: 53 43 48 45 4d 41 5d 2c 20 5b 53 51 4c 49 54 45  SCHEMA], [SQLITE
f620: 5f 43 4f 52 52 55 50 54 5d 2c 20 61 6e 64 20 73  _CORRUPT], and s
f630: 6f 20 66 6f 72 74 68 29 0a 2a 2a 20 63 61 6e 20  o forth).** can 
f640: 62 65 20 6f 62 74 61 69 6e 65 64 20 62 79 20 63  be obtained by c
f650: 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f  alling [sqlite3_
f660: 72 65 73 65 74 28 29 5d 20 6f 6e 20 74 68 65 0a  reset()] on the.
f670: 2a 2a 20 5b 73 71 6c 69 74 65 5f 73 74 6d 74 20  ** [sqlite_stmt 
f680: 7c 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  | prepared state
f690: 6d 65 6e 74 5d 2e 20 20 49 6e 20 74 68 65 20 22  ment].  In the "
f6a0: 76 32 22 20 69 6e 74 65 72 66 61 63 65 2c 0a 2a  v2" interface,.*
f6b0: 2a 20 74 68 65 20 6d 6f 72 65 20 73 70 65 63 69  * the more speci
f6c0: 66 69 63 20 65 72 72 6f 72 20 63 6f 64 65 20 69  fic error code i
f6d0: 73 20 72 65 74 75 72 6e 65 64 20 64 69 72 65 63  s returned direc
f6e0: 74 6c 79 20 62 79 20 73 71 6c 69 74 65 33 5f 73  tly by sqlite3_s
f6f0: 74 65 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51  tep()..**.** [SQ
f700: 4c 49 54 45 5f 4d 49 53 55 53 45 5d 20 6d 65 61  LITE_MISUSE] mea
f710: 6e 73 20 74 68 61 74 20 74 68 65 20 74 68 69 73  ns that the this
f720: 20 72 6f 75 74 69 6e 65 20 77 61 73 20 63 61 6c   routine was cal
f730: 6c 65 64 20 69 6e 61 70 70 72 6f 70 72 69 61 74  led inappropriat
f740: 65 6c 79 2e 0a 2a 2a 20 50 65 72 68 61 70 73 20  ely..** Perhaps 
f750: 69 74 20 77 61 73 20 63 61 6c 6c 65 64 20 6f 6e  it was called on
f760: 20 61 20 5b 73 71 6c 69 74 65 5f 73 74 6d 74 20   a [sqlite_stmt 
f770: 7c 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  | prepared state
f780: 6d 65 6e 74 5d 20 74 68 61 74 20 68 61 73 0a 2a  ment] that has.*
f790: 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 5b  * already been [
f7a0: 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
f7b0: 20 7c 20 66 69 6e 61 6c 69 7a 65 64 5d 20 6f 72   | finalized] or
f7c0: 20 6f 6e 20 6f 6e 65 20 74 68 61 74 20 68 61 64   on one that had
f7d0: 20 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20   .** previously 
f7e0: 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45  returned [SQLITE
f7f0: 5f 45 52 52 4f 52 5d 20 6f 72 20 5b 53 51 4c 49  _ERROR] or [SQLI
f800: 54 45 5f 44 4f 4e 45 5d 2e 20 20 4f 72 20 69 74  TE_DONE].  Or it
f810: 20 63 6f 75 6c 64 0a 2a 2a 20 62 65 20 74 68 65   could.** be the
f820: 20 63 61 73 65 20 74 68 61 74 20 74 68 65 20 73   case that the s
f830: 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ame database con
f840: 6e 65 63 74 69 6f 6e 20 69 73 20 62 65 69 6e 67  nection is being
f850: 20 75 73 65 64 20 62 79 20 74 77 6f 20 6f 72 0a   used by two or.
f860: 2a 2a 20 6d 6f 72 65 20 74 68 72 65 61 64 73 20  ** more threads 
f870: 61 74 20 74 68 65 20 73 61 6d 65 20 6d 6f 6d 65  at the same mome
f880: 6e 74 20 69 6e 20 74 69 6d 65 2e 0a 2a 2a 0a 2a  nt in time..**.*
f890: 2a 20 3c 62 3e 47 6f 6f 66 79 20 49 6e 74 65 72  * <b>Goofy Inter
f8a0: 66 61 63 65 20 41 6c 65 72 74 3a 3c 2f 62 3e 0a  face Alert:</b>.
f8b0: 2a 2a 20 49 6e 20 74 68 65 20 6c 65 67 61 63 79  ** In the legacy
f8c0: 20 69 6e 74 65 72 66 61 63 65 2c 20 0a 2a 2a 20   interface, .** 
f8d0: 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70  the sqlite3_step
f8e0: 28 29 20 41 50 49 20 61 6c 77 61 79 73 20 72 65  () API always re
f8f0: 74 75 72 6e 73 20 61 20 67 65 6e 65 72 69 63 20  turns a generic 
f900: 65 72 72 6f 72 20 63 6f 64 65 2c 0a 2a 2a 20 5b  error code,.** [
f910: 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 2c 20 66  SQLITE_ERROR], f
f920: 6f 6c 6c 6f 77 69 6e 67 20 61 6e 79 20 65 72 72  ollowing any err
f930: 6f 72 20 6f 74 68 65 72 20 74 68 61 6e 20 5b 53  or other than [S
f940: 51 4c 49 54 45 5f 42 55 53 59 5d 0a 2a 2a 20 61  QLITE_BUSY].** a
f950: 6e 64 20 5b 53 51 4c 49 54 45 5f 4d 49 53 55 53  nd [SQLITE_MISUS
f960: 45 5d 2e 20 20 59 6f 75 20 6d 75 73 74 20 63 61  E].  You must ca
f970: 6c 6c 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65  ll [sqlite3_rese
f980: 74 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69  t()] or.** [sqli
f990: 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20  te3_finalize()] 
f9a0: 69 6e 20 6f 72 64 65 72 20 74 6f 20 66 69 6e 64  in order to find
f9b0: 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 70 65 63   one of the spec
f9c0: 69 66 69 63 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  ific.** [SQLITE_
f9d0: 45 52 52 4f 52 20 7c 20 72 65 73 75 6c 74 20 63  ERROR | result c
f9e0: 6f 64 65 73 5d 20 74 68 61 74 20 62 65 74 74 65  odes] that bette
f9f0: 72 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20  r describes the 
fa00: 65 72 72 6f 72 2e 0a 2a 2a 20 57 65 20 61 64 6d  error..** We adm
fa10: 69 74 20 74 68 61 74 20 74 68 69 73 20 69 73 20  it that this is 
fa20: 61 20 67 6f 6f 66 79 20 64 65 73 69 67 6e 2e 20  a goofy design. 
fa30: 20 54 68 65 20 70 72 6f 62 6c 65 6d 20 68 61 73   The problem has
fa40: 20 62 65 65 6e 20 66 69 78 65 64 0a 2a 2a 20 77   been fixed.** w
fa50: 69 74 68 20 74 68 65 20 22 76 32 22 20 69 6e 74  ith the "v2" int
fa60: 65 72 66 61 63 65 2e 20 20 49 66 20 79 6f 75 20  erface.  If you 
fa70: 70 72 65 70 61 72 65 20 61 6c 6c 20 6f 66 20 79  prepare all of y
fa80: 6f 75 72 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  our SQL statemen
fa90: 74 73 0a 2a 2a 20 75 73 69 6e 67 20 65 69 74 68  ts.** using eith
faa0: 65 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  er [sqlite3_prep
fab0: 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 5b 73 71  are_v2()] or [sq
fac0: 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f  lite3_prepare16_
fad0: 76 32 28 29 5d 20 69 6e 73 74 65 61 64 0a 2a 2a  v2()] instead.**
fae0: 20 6f 66 20 74 68 65 20 6c 65 67 61 63 79 20 5b   of the legacy [
faf0: 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28  sqlite3_prepare(
fb00: 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  )] and [sqlite3_
fb10: 70 72 65 70 61 72 65 31 36 28 29 5d 2c 20 74 68  prepare16()], th
fb20: 65 6e 20 74 68 65 20 0a 2a 2a 20 6d 6f 72 65 20  en the .** more 
fb30: 73 70 65 63 69 66 69 63 20 5b 53 51 4c 49 54 45  specific [SQLITE
fb40: 5f 45 52 52 4f 52 20 7c 20 72 65 73 75 6c 74 20  _ERROR | result 
fb50: 63 6f 64 65 73 5d 20 61 72 65 20 72 65 74 75 72  codes] are retur
fb60: 6e 65 64 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20  ned directly.** 
fb70: 62 79 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  by sqlite3_step(
fb80: 29 2e 20 20 54 68 65 20 75 73 65 20 6f 66 20 74  ).  The use of t
fb90: 68 65 20 22 76 32 22 20 69 6e 74 65 72 66 61 63  he "v2" interfac
fba0: 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64  e is recommended
fbb0: 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
fbc0: 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 65 70  int sqlite3_step
fbd0: 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b  (sqlite3_stmt*);
fbe0: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
fbf0: 3a 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  :.**.** Return t
fc00: 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 76 61 6c  he number of val
fc10: 75 65 73 20 69 6e 20 74 68 65 20 63 75 72 72 65  ues in the curre
fc20: 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20 72 65  nt row of the re
fc30: 73 75 6c 74 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20  sult set..**.** 
fc40: 41 66 74 65 72 20 61 20 63 61 6c 6c 20 74 6f 20  After a call to 
fc50: 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
fc60: 20 74 68 61 74 20 72 65 74 75 72 6e 73 20 5b 53   that returns [S
fc70: 51 4c 49 54 45 5f 52 4f 57 5d 2c 20 74 68 69 73  QLITE_ROW], this
fc80: 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 77 69 6c 6c   routine.** will
fc90: 20 72 65 74 75 72 6e 20 74 68 65 20 73 61 6d 65   return the same
fca0: 20 76 61 6c 75 65 20 61 73 20 74 68 65 20 5b 73   value as the [s
fcb0: 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f  qlite3_column_co
fcc0: 75 6e 74 28 29 5d 20 66 75 6e 63 74 69 6f 6e 2e  unt()] function.
fcd0: 0a 2a 2a 20 41 66 74 65 72 20 5b 73 71 6c 69 74  .** After [sqlit
fce0: 65 33 5f 73 74 65 70 28 29 5d 20 68 61 73 20 72  e3_step()] has r
fcf0: 65 74 75 72 6e 65 64 20 61 6e 20 5b 53 51 4c 49  eturned an [SQLI
fd00: 54 45 5f 44 4f 4e 45 5d 2c 20 5b 53 51 4c 49 54  TE_DONE], [SQLIT
fd10: 45 5f 42 55 53 59 5d 2c 20 6f 72 0a 2a 2a 20 61  E_BUSY], or.** a
fd20: 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 7c   [SQLITE_ERROR |
fd30: 20 65 72 72 6f 72 20 63 6f 64 65 5d 2c 20 6f 72   error code], or
fd40: 20 62 65 66 6f 72 65 20 5b 73 71 6c 69 74 65 33   before [sqlite3
fd50: 5f 73 74 65 70 28 29 5d 20 68 61 73 20 62 65 65  _step()] has bee
fd60: 6e 20 0a 2a 2a 20 63 61 6c 6c 65 64 20 6f 6e 20  n .** called on 
fd70: 74 68 65 20 5b 73 71 6c 69 74 65 5f 73 74 6d 74  the [sqlite_stmt
fd80: 20 7c 20 70 72 65 70 61 72 65 64 20 73 74 61 74   | prepared stat
fd90: 65 6d 65 6e 74 5d 20 66 6f 72 20 74 68 65 20 66  ement] for the f
fda0: 69 72 73 74 20 74 69 6d 65 2c 0a 2a 2a 20 74 68  irst time,.** th
fdb0: 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  is routine retur
fdc0: 6e 73 20 7a 65 72 6f 2e 0a 2a 2f 0a 69 6e 74 20  ns zero..*/.int 
fdd0: 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75  sqlite3_data_cou
fde0: 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  nt(sqlite3_stmt 
fdf0: 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20  *pStmt);../*.** 
fe00: 43 41 50 49 33 52 45 46 3a 20 46 75 6e 64 61 6d  CAPI3REF: Fundam
fe10: 65 6e 74 61 6c 20 44 61 74 61 74 79 70 65 73 0a  ental Datatypes.
fe20: 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 76 61 6c 75  **.** Every valu
fe30: 65 20 69 6e 20 53 51 4c 69 74 65 20 68 61 73 20  e in SQLite has 
fe40: 6f 6e 65 20 6f 66 20 66 69 76 65 20 66 75 6e 64  one of five fund
fe50: 61 6d 65 6e 74 61 6c 20 64 61 74 61 74 79 70 65  amental datatype
fe60: 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a  s:.**.** <ul>.**
fe70: 20 3c 6c 69 3e 20 36 34 2d 62 69 74 20 73 69 67   <li> 64-bit sig
fe80: 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 3c  ned integer.** <
fe90: 6c 69 3e 20 36 34 2d 62 69 74 20 49 45 45 45 20  li> 64-bit IEEE 
fea0: 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e  floating point n
feb0: 75 6d 62 65 72 0a 2a 2a 20 3c 6c 69 3e 20 73 74  umber.** <li> st
fec0: 72 69 6e 67 0a 2a 2a 20 3c 6c 69 3e 20 42 4c 4f  ring.** <li> BLO
fed0: 42 0a 2a 2a 20 3c 6c 69 3e 20 4e 55 4c 4c 0a 2a  B.** <li> NULL.*
fee0: 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68  * </ul>.**.** Th
fef0: 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 61 72  ese constants ar
ff00: 65 20 63 6f 64 65 73 20 66 6f 72 20 65 61 63 68  e codes for each
ff10: 20 6f 66 20 74 68 6f 73 65 20 74 79 70 65 73 2e   of those types.
ff20: 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74  .**.** Note that
ff30: 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45 58 54   the SQLITE_TEXT
ff40: 20 63 6f 6e 73 74 61 6e 74 20 77 61 73 20 61 6c   constant was al
ff50: 73 6f 20 75 73 65 64 20 69 6e 20 53 51 4c 69 74  so used in SQLit
ff60: 65 20 76 65 72 73 69 6f 6e 20 32 0a 2a 2a 20 66  e version 2.** f
ff70: 6f 72 20 61 20 63 6f 6d 70 6c 65 74 65 6c 79 20  or a completely 
ff80: 64 69 66 66 65 72 65 6e 74 20 6d 65 61 6e 69 6e  different meanin
ff90: 67 2e 20 20 53 6f 66 74 77 61 72 65 20 74 68 61  g.  Software tha
ffa0: 74 20 6c 69 6e 6b 73 20 61 67 61 69 6e 73 74 20  t links against 
ffb0: 62 6f 74 68 0a 2a 2a 20 53 51 4c 69 74 65 20 76  both.** SQLite v
ffc0: 65 72 73 69 6f 6e 20 32 20 61 6e 64 20 53 51 4c  ersion 2 and SQL
ffd0: 69 74 65 20 76 65 72 73 69 6f 6e 20 33 20 73 68  ite version 3 sh
ffe0: 6f 75 6c 64 20 75 73 65 20 53 51 4c 49 54 45 33  ould use SQLITE3
fff0: 5f 54 45 58 54 20 6e 6f 74 0a 2a 2a 20 53 51 4c  _TEXT not.** SQL
10000 49 54 45 5f 54 45 58 54 2e 0a 2a 2f 0a 23 64 65  ITE_TEXT..*/.#de
10010 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 45  fine SQLITE_INTE
10020 47 45 52 20 20 31 0a 23 64 65 66 69 6e 65 20 53  GER  1.#define S
10030 51 4c 49 54 45 5f 46 4c 4f 41 54 20 20 20 20 32  QLITE_FLOAT    2
10040 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
10050 42 4c 4f 42 20 20 20 20 20 34 0a 23 64 65 66 69  BLOB     4.#defi
10060 6e 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 20  ne SQLITE_NULL  
10070 20 20 20 35 0a 23 69 66 64 65 66 20 53 51 4c 49     5.#ifdef SQLI
10080 54 45 5f 54 45 58 54 0a 23 20 75 6e 64 65 66 20  TE_TEXT.# undef 
10090 53 51 4c 49 54 45 5f 54 45 58 54 0a 23 65 6c 73  SQLITE_TEXT.#els
100a0 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  e.# define SQLIT
100b0 45 5f 54 45 58 54 20 20 20 20 20 33 0a 23 65 6e  E_TEXT     3.#en
100c0 64 69 66 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  dif.#define SQLI
100d0 54 45 33 5f 54 45 58 54 20 20 20 20 20 33 0a 0a  TE3_TEXT     3..
100e0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
100f0 52 65 73 75 6c 74 73 20 56 61 6c 75 65 73 20 46  Results Values F
10100 72 6f 6d 20 41 20 51 75 65 72 79 0a 2a 2a 0a 2a  rom A Query.**.*
10110 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
10120 20 72 65 74 75 72 6e 20 69 6e 66 6f 72 6d 61 74   return informat
10130 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 69 6e  ion about the in
10140 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 69 6e 20  formation.** in 
10150 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20  a single column 
10160 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 72  of the current r
10170 65 73 75 6c 74 20 72 6f 77 20 6f 66 20 61 20 71  esult row of a q
10180 75 65 72 79 2e 20 20 49 6e 20 65 76 65 72 79 0a  uery.  In every.
10190 2a 2a 20 63 61 73 65 20 74 68 65 20 66 69 72 73  ** case the firs
101a0 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20  t argument is a 
101b0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 0a  pointer to the .
101c0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74  ** [sqlite3_stmt
101d0 20 7c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74   | SQL statement
101e0 5d 20 74 68 61 74 20 69 73 20 62 65 69 6e 67 0a  ] that is being.
101f0 2a 2a 20 65 76 61 6c 75 61 74 65 20 28 74 68 65  ** evaluate (the
10200 20 5b 73 71 6c 69 74 65 5f 73 74 6d 74 2a 5d 20   [sqlite_stmt*] 
10210 74 68 61 74 20 77 61 73 20 72 65 74 75 72 6e 65  that was returne
10220 64 20 66 72 6f 6d 20 0a 2a 2a 20 5b 73 71 6c 69  d from .** [sqli
10230 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29  te3_prepare_v2()
10240 5d 20 6f 72 20 6f 6e 65 20 6f 66 20 69 74 73 20  ] or one of its 
10250 76 61 72 69 61 6e 74 73 29 20 61 6e 64 0a 2a 2a  variants) and.**
10260 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
10270 6d 65 6e 74 20 69 73 20 74 68 65 20 69 6e 64 65  ment is the inde
10280 78 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20  x of the column 
10290 66 6f 72 20 77 68 69 63 68 20 69 6e 66 6f 72 6d  for which inform
102a0 61 74 69 6f 6e 20 0a 2a 2a 20 73 68 6f 75 6c 64  ation .** should
102b0 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 20 54   be returned.  T
102c0 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c  he left-most col
102d0 75 6d 6e 20 68 61 73 20 61 6e 20 69 6e 64 65 78  umn has an index
102e0 20 6f 66 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   of 0..**.** If 
102f0 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
10300 74 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74  t is not current
10310 6c 79 20 70 6f 69 6e 74 20 74 6f 20 61 20 76 61  ly point to a va
10320 6c 69 64 20 72 6f 77 2c 20 6f 72 20 69 66 20 74  lid row, or if t
10330 68 65 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e  he.** the column
10340 20 69 6e 64 65 78 20 69 73 20 6f 75 74 20 6f 66   index is out of
10350 20 72 61 6e 67 65 2c 20 74 68 65 20 72 65 73 75   range, the resu
10360 6c 74 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e  lt is undefined.
10370 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
10380 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29  e3_column_type()
10390 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
103a0 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4e 54   .** [SQLITE_INT
103b0 45 47 45 52 20 7c 20 64 61 74 61 74 79 70 65 20  EGER | datatype 
103c0 63 6f 64 65 5d 20 66 6f 72 20 74 68 65 20 69 6e  code] for the in
103d0 69 74 69 61 6c 20 64 61 74 61 20 74 79 70 65 0a  itial data type.
103e0 2a 2a 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  ** of the result
103f0 20 63 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 72 65   column.  The re
10400 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20  turned value is 
10410 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 49  one of [SQLITE_I
10420 4e 54 45 47 45 52 5d 2c 0a 2a 2a 20 5b 53 51 4c  NTEGER],.** [SQL
10430 49 54 45 5f 46 4c 4f 41 54 5d 2c 20 5b 53 51 4c  ITE_FLOAT], [SQL
10440 49 54 45 5f 54 45 58 54 5d 2c 20 5b 53 51 4c 49  ITE_TEXT], [SQLI
10450 54 45 5f 42 4c 4f 42 5d 2c 20 6f 72 20 5b 53 51  TE_BLOB], or [SQ
10460 4c 49 54 45 5f 4e 55 4c 4c 5d 2e 20 20 54 68 65  LITE_NULL].  The
10470 20 76 61 6c 75 65 0a 2a 2a 20 72 65 74 75 72 6e   value.** return
10480 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f  ed by sqlite3_co
10490 6c 75 6d 6e 5f 74 79 70 65 28 29 20 69 73 20 6f  lumn_type() is o
104a0 6e 6c 79 20 6d 65 61 6e 69 6e 67 66 75 6c 20 69  nly meaningful i
104b0 66 20 6e 6f 20 74 79 70 65 0a 2a 2a 20 63 6f 6e  f no type.** con
104c0 76 65 72 73 69 6f 6e 73 20 68 61 76 65 20 6f 63  versions have oc
104d0 63 75 72 72 65 64 20 61 73 20 64 65 73 63 72 69  curred as descri
104e0 62 65 64 20 62 65 6c 6f 77 2e 20 20 41 66 74 65  bed below.  Afte
104f0 72 20 61 20 74 79 70 65 20 63 6f 6e 76 65 72 73  r a type convers
10500 69 6f 6e 2c 0a 2a 2a 20 74 68 65 20 76 61 6c 75  ion,.** the valu
10510 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71  e returned by sq
10520 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70  lite3_column_typ
10530 65 28 29 20 69 73 20 75 6e 64 65 66 69 6e 65 64  e() is undefined
10540 2e 20 20 46 75 74 75 72 65 0a 2a 2a 20 76 65 72  .  Future.** ver
10550 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20  sions of SQLite 
10560 6d 61 79 20 63 68 61 6e 67 65 20 74 68 65 20 62  may change the b
10570 65 68 61 76 69 6f 72 20 6f 66 20 73 71 6c 69 74  ehavior of sqlit
10580 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29  e3_column_type()
10590 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20  .** following a 
105a0 74 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e  type conversion.
105b0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65  .**.** If the re
105c0 73 75 6c 74 20 69 73 20 61 20 42 4c 4f 42 20 6f  sult is a BLOB o
105d0 72 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 74  r UTF-8 string t
105e0 68 65 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f  hen the sqlite3_
105f0 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 20 0a  column_bytes() .
10600 2a 2a 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  ** routine retur
10610 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ns the number of
10620 20 62 79 74 65 73 20 69 6e 20 74 68 61 74 20 42   bytes in that B
10630 4c 4f 42 20 6f 72 20 73 74 72 69 6e 67 2e 0a 2a  LOB or string..*
10640 2a 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 20  * If the result 
10650 69 73 20 61 20 55 54 46 2d 31 36 20 73 74 72 69  is a UTF-16 stri
10660 6e 67 2c 20 74 68 65 6e 20 73 71 6c 69 74 65 33  ng, then sqlite3
10670 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 20  _column_bytes() 
10680 63 6f 6e 76 65 72 74 73 0a 2a 2a 20 74 68 65 20  converts.** the 
10690 73 74 72 69 6e 67 20 74 6f 20 55 54 46 2d 38 20  string to UTF-8 
106a0 61 6e 64 20 74 68 65 6e 20 72 65 74 75 72 6e 73  and then returns
106b0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
106c0 79 74 65 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20  ytes..** If the 
106d0 72 65 73 75 6c 74 20 69 73 20 61 20 6e 75 6d 65  result is a nume
106e0 72 69 63 20 76 61 6c 75 65 20 74 68 65 6e 20 73  ric value then s
106f0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
10700 74 65 73 28 29 20 75 73 65 73 0a 2a 2a 20 5b 73  tes() uses.** [s
10710 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
10720 29 5d 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68  )] to convert th
10730 61 74 20 76 61 6c 75 65 20 74 6f 20 61 20 55 54  at value to a UT
10740 46 2d 38 20 73 74 72 69 6e 67 20 61 6e 64 20 72  F-8 string and r
10750 65 74 75 72 6e 73 0a 2a 2a 20 74 68 65 20 6e 75  eturns.** the nu
10760 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
10770 20 74 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a   that string..**
10780 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   The value retur
10790 6e 65 64 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63  ned does not inc
107a0 6c 75 64 65 20 74 68 65 20 7a 65 72 6f 20 74 65  lude the zero te
107b0 72 6d 69 6e 61 74 6f 72 20 61 74 20 74 68 65 20  rminator at the 
107c0 65 6e 64 0a 2a 2a 20 6f 66 20 74 68 65 20 73 74  end.** of the st
107d0 72 69 6e 67 2e 20 20 46 6f 72 20 63 6c 61 72 69  ring.  For clari
107e0 74 79 3a 20 74 68 65 20 76 61 6c 75 65 20 72 65  ty: the value re
107f0 74 75 72 6e 65 64 20 69 73 20 74 68 65 20 6e 75  turned is the nu
10800 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 79 74 65 73  mber of.** bytes
10810 20 69 6e 20 74 68 65 20 73 74 72 69 6e 67 2c 20   in the string, 
10820 6e 6f 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  not the number o
10830 66 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a  f characters..**
10840 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
10850 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29  column_bytes16()
10860 20 72 6f 75 74 69 6e 65 20 69 73 20 73 69 6d 69   routine is simi
10870 6c 61 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 63  lar to sqlite3_c
10880 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 0a 2a 2a  olumn_bytes().**
10890 20 62 75 74 20 6c 65 61 76 65 73 20 74 68 65 20   but leaves the 
108a0 72 65 73 75 6c 74 20 69 6e 20 55 54 46 2d 31 36  result in UTF-16
108b0 20 69 6e 73 74 65 61 64 20 6f 66 20 55 54 46 2d   instead of UTF-
108c0 38 2e 20 20 0a 2a 2a 20 54 68 65 20 7a 65 72 6f  8.  .** The zero
108d0 20 74 65 72 6d 69 6e 61 74 6f 72 20 69 73 20 6e   terminator is n
108e0 6f 74 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74  ot included in t
108f0 68 69 73 20 63 6f 75 6e 74 2e 0a 2a 2a 0a 2a 2a  his count..**.**
10900 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
10910 61 74 74 65 6d 70 74 20 74 6f 20 63 6f 6e 76 65  attempt to conve
10920 72 74 20 74 68 65 20 76 61 6c 75 65 20 77 68 65  rt the value whe
10930 72 65 20 61 70 70 72 6f 70 72 69 61 74 65 2e 20  re appropriate. 
10940 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c   For.** example,
10950 20 69 66 20 74 68 65 20 69 6e 74 65 72 6e 61 6c   if the internal
10960 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
10970 69 73 20 46 4c 4f 41 54 20 61 6e 64 20 61 20 74  is FLOAT and a t
10980 65 78 74 20 72 65 73 75 6c 74 0a 2a 2a 20 69 73  ext result.** is
10990 20 72 65 71 75 65 73 74 65 64 2c 20 5b 73 71 6c   requested, [sql
109a0 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 5d  ite3_snprintf()]
109b0 20 69 73 20 75 73 65 64 20 69 6e 74 65 72 6e 61   is used interna
109c0 6c 6c 79 20 74 6f 20 64 6f 20 74 68 65 20 63 6f  lly to do the co
109d0 6e 76 65 72 73 69 6f 6e 0a 2a 2a 20 61 75 74 6f  nversion.** auto
109e0 6d 61 74 69 63 61 6c 6c 79 2e 20 20 54 68 65 20  matically.  The 
109f0 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20  following table 
10a00 64 65 74 61 69 6c 73 20 74 68 65 20 63 6f 6e 76  details the conv
10a10 65 72 73 69 6f 6e 73 20 74 68 61 74 0a 2a 2a 20  ersions that.** 
10a20 61 72 65 20 61 70 70 6c 69 65 64 3a 0a 2a 2a 0a  are applied:.**.
10a30 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a  ** <blockquote>.
10a40 2a 2a 20 3c 74 61 62 6c 65 20 62 6f 72 64 65 72  ** <table border
10a50 3d 22 31 22 3e 0a 2a 2a 20 3c 74 72 3e 3c 74 68  ="1">.** <tr><th
10a60 3e 20 49 6e 74 65 72 6e 61 6c 20 3c 74 68 3e 20  > Internal <th> 
10a70 52 65 71 75 65 73 74 65 64 20 3c 74 68 3e 20 0a  Requested <th> .
10a80 2a 2a 20 3c 74 72 3e 3c 74 68 3e 20 20 54 79 70  ** <tr><th>  Typ
10a90 65 20 20 20 20 3c 74 68 3e 20 20 20 20 54 79 70  e    <th>    Typ
10aa0 65 20 20 20 3c 74 68 3e 20 43 6f 6e 76 65 72 73  e   <th> Convers
10ab0 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 72 3e 3c 74  ion.**.** <tr><t
10ac0 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e  d>  NULL    <td>
10ad0 20 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e 20   INTEGER   <td> 
10ae0 52 65 73 75 6c 74 20 69 73 20 30 0a 2a 2a 20 3c  Result is 0.** <
10af0 74 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 20  tr><td>  NULL   
10b00 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 20   <td>  FLOAT    
10b10 3c 74 64 3e 20 52 65 73 75 6c 74 20 69 73 20 30  <td> Result is 0
10b20 2e 30 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20  .0.** <tr><td>  
10b30 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20 20 20 54  NULL    <td>   T
10b40 45 58 54 20 20 20 20 3c 74 64 3e 20 52 65 73 75  EXT    <td> Resu
10b50 6c 74 20 69 73 20 4e 55 4c 4c 20 70 6f 69 6e 74  lt is NULL point
10b60 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20  er.** <tr><td>  
10b70 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20 20 20 42  NULL    <td>   B
10b80 4c 4f 42 20 20 20 20 3c 74 64 3e 20 52 65 73 75  LOB    <td> Resu
10b90 6c 74 20 69 73 20 4e 55 4c 4c 20 70 6f 69 6e 74  lt is NULL point
10ba0 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 49  er.** <tr><td> I
10bb0 4e 54 45 47 45 52 20 20 3c 74 64 3e 20 20 46 4c  NTEGER  <td>  FL
10bc0 4f 41 54 20 20 20 20 3c 74 64 3e 20 43 6f 6e 76  OAT    <td> Conv
10bd0 65 72 74 20 66 72 6f 6d 20 69 6e 74 65 67 65 72  ert from integer
10be0 20 74 6f 20 66 6c 6f 61 74 0a 2a 2a 20 3c 74 72   to float.** <tr
10bf0 3e 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 3c  ><td> INTEGER  <
10c00 74 64 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74  td>   TEXT    <t
10c10 64 3e 20 41 53 43 49 49 20 72 65 6e 64 65 72 69  d> ASCII renderi
10c20 6e 67 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65  ng of the intege
10c30 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 49 4e  r.** <tr><td> IN
10c40 54 45 47 45 52 20 20 3c 74 64 3e 20 20 20 42 4c  TEGER  <td>   BL
10c50 4f 42 20 20 20 20 3c 74 64 3e 20 53 61 6d 65 20  OB    <td> Same 
10c60 61 73 20 66 6f 72 20 49 4e 54 45 47 45 52 2d 3e  as for INTEGER->
10c70 54 45 58 54 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e  TEXT.** <tr><td>
10c80 20 20 46 4c 4f 41 54 20 20 20 3c 74 64 3e 20 49    FLOAT   <td> I
10c90 4e 54 45 47 45 52 20 20 20 3c 74 64 3e 20 43 6f  NTEGER   <td> Co
10ca0 6e 76 65 72 74 20 66 72 6f 6d 20 66 6c 6f 61 74  nvert from float
10cb0 20 74 6f 20 69 6e 74 65 67 65 72 0a 2a 2a 20 3c   to integer.** <
10cc0 74 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20  tr><td>  FLOAT  
10cd0 20 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20 20   <td>   TEXT    
10ce0 3c 74 64 3e 20 41 53 43 49 49 20 72 65 6e 64 65  <td> ASCII rende
10cf0 72 69 6e 67 20 6f 66 20 74 68 65 20 66 6c 6f 61  ring of the floa
10d00 74 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 46  t.** <tr><td>  F
10d10 4c 4f 41 54 20 20 20 3c 74 64 3e 20 20 20 42 4c  LOAT   <td>   BL
10d20 4f 42 20 20 20 20 3c 74 64 3e 20 53 61 6d 65 20  OB    <td> Same 
10d30 61 73 20 46 4c 4f 41 54 2d 3e 54 45 58 54 0a 2a  as FLOAT->TEXT.*
10d40 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 54 45 58 54  * <tr><td>  TEXT
10d50 20 20 20 20 3c 74 64 3e 20 49 4e 54 45 47 45 52      <td> INTEGER
10d60 20 20 20 3c 74 64 3e 20 55 73 65 20 61 74 6f 69     <td> Use atoi
10d70 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20  ().** <tr><td>  
10d80 54 45 58 54 20 20 20 20 3c 74 64 3e 20 20 46 4c  TEXT    <td>  FL
10d90 4f 41 54 20 20 20 20 3c 74 64 3e 20 55 73 65 20  OAT    <td> Use 
10da0 61 74 6f 66 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74  atof().** <tr><t
10db0 64 3e 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e  d>  TEXT    <td>
10dc0 20 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20     BLOB    <td> 
10dd0 4e 6f 20 63 68 61 6e 67 65 0a 2a 2a 20 3c 74 72  No change.** <tr
10de0 3e 3c 74 64 3e 20 20 42 4c 4f 42 20 20 20 20 3c  ><td>  BLOB    <
10df0 74 64 3e 20 49 4e 54 45 47 45 52 20 20 20 3c 74  td> INTEGER   <t
10e00 64 3e 20 43 6f 6e 76 65 72 74 20 74 6f 20 54 45  d> Convert to TE
10e10 58 54 20 74 68 65 6e 20 75 73 65 20 61 74 6f 69  XT then use atoi
10e20 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20  ().** <tr><td>  
10e30 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 20 46 4c  BLOB    <td>  FL
10e40 4f 41 54 20 20 20 20 3c 74 64 3e 20 43 6f 6e 76  OAT    <td> Conv
10e50 65 72 74 20 74 6f 20 54 45 58 54 20 74 68 65 6e  ert to TEXT then
10e60 20 75 73 65 20 61 74 6f 66 28 29 0a 2a 2a 20 3c   use atof().** <
10e70 74 72 3e 3c 74 64 3e 20 20 42 4c 4f 42 20 20 20  tr><td>  BLOB   
10e80 20 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20 20   <td>   TEXT    
10e90 3c 74 64 3e 20 41 64 64 20 61 20 7a 65 72 6f 20  <td> Add a zero 
10ea0 74 65 72 6d 69 6e 61 74 6f 72 20 69 66 20 6e 65  terminator if ne
10eb0 65 64 65 64 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e  eded.** </table>
10ec0 0a 2a 2a 20 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65  .** </blockquote
10ed0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c  >.**.** The tabl
10ee0 65 20 61 62 6f 76 65 20 6d 61 6b 65 73 20 72 65  e above makes re
10ef0 66 65 72 65 6e 63 65 20 74 6f 20 73 74 61 6e 64  ference to stand
10f00 61 72 64 20 43 20 6c 69 62 72 61 72 79 20 66 75  ard C library fu
10f10 6e 63 74 69 6f 6e 73 20 61 74 6f 69 28 29 0a 2a  nctions atoi().*
10f20 2a 20 61 6e 64 20 61 74 6f 66 28 29 2e 20 20 53  * and atof().  S
10f30 51 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74 20 72  QLite does not r
10f40 65 61 6c 6c 79 20 75 73 65 20 74 68 65 73 65 20  eally use these 
10f50 66 75 6e 63 74 69 6f 6e 73 2e 20 20 49 74 20 68  functions.  It h
10f60 61 73 20 69 74 73 0a 2a 2a 20 6f 6e 20 65 71 75  as its.** on equ
10f70 61 76 61 6c 65 6e 74 20 69 6e 74 65 72 6e 61 6c  avalent internal
10f80 20 72 6f 75 74 69 6e 65 73 2e 20 20 54 68 65 20   routines.  The 
10f90 61 74 6f 69 28 29 20 61 6e 64 20 61 74 6f 66 28  atoi() and atof(
10fa0 29 20 6e 61 6d 65 73 20 61 72 65 0a 2a 2a 20 75  ) names are.** u
10fb0 73 65 64 20 69 6e 20 74 68 65 20 74 61 62 6c 65  sed in the table
10fc0 20 66 6f 72 20 62 72 65 76 69 74 79 20 61 6e 64   for brevity and
10fd0 20 62 65 63 61 75 73 65 20 74 68 65 79 20 61 72   because they ar
10fe0 65 20 66 61 6d 69 6c 69 61 72 20 74 6f 20 6d 6f  e familiar to mo
10ff0 73 74 0a 2a 2a 20 43 20 70 72 6f 67 72 61 6d 6d  st.** C programm
11000 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20  ers..**.** Note 
11010 74 68 61 74 20 77 68 65 6e 20 74 79 70 65 20 63  that when type c
11020 6f 6e 76 65 72 73 69 6f 6e 73 20 6f 63 63 75 72  onversions occur
11030 2c 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75 72  , pointers retur
11040 6e 65 64 20 62 79 20 70 72 69 6f 72 0a 2a 2a 20  ned by prior.** 
11050 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33  calls to sqlite3
11060 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20  _column_blob(), 
11070 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
11080 65 78 74 28 29 2c 20 61 6e 64 2f 6f 72 0a 2a 2a  ext(), and/or.**
11090 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
110a0 74 65 78 74 31 36 28 29 20 6d 61 79 20 62 65 20  text16() may be 
110b0 69 6e 76 61 6c 69 64 61 74 65 64 2e 20 0a 2a 2a  invalidated. .**
110c0 20 54 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e   Type conversion
110d0 73 20 61 6e 64 20 70 6f 69 6e 74 65 72 20 69 6e  s and pointer in
110e0 76 61 6c 69 64 61 74 69 6f 6e 73 20 6d 69 67 68  validations migh
110f0 74 20 6f 63 63 75 72 0a 2a 2a 20 69 6e 20 74 68  t occur.** in th
11100 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 73 65  e following case
11110 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a  s:.**.** <ul>.**
11120 20 3c 6c 69 3e 3c 70 3e 20 20 54 68 65 20 69 6e   <li><p>  The in
11130 69 74 69 61 6c 20 63 6f 6e 74 65 6e 74 20 69 73  itial content is
11140 20 61 20 42 4c 4f 42 20 61 6e 64 20 73 71 6c 69   a BLOB and sqli
11150 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28  te3_column_text(
11160 29 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f  ) .**          o
11170 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  r sqlite3_column
11180 5f 74 65 78 74 31 36 28 29 20 69 73 20 63 61 6c  _text16() is cal
11190 6c 65 64 2e 20 20 41 20 7a 65 72 6f 2d 74 65 72  led.  A zero-ter
111a0 6d 69 6e 61 74 6f 72 20 6d 69 67 68 74 0a 2a 2a  minator might.**
111b0 20 20 20 20 20 20 20 20 20 20 6e 65 65 64 20 74            need t
111c0 6f 20 62 65 20 61 64 64 65 64 20 74 6f 20 74 68  o be added to th
111d0 65 20 73 74 72 69 6e 67 2e 3c 2f 70 3e 3c 2f 6c  e string.</p></l
111e0 69 3e 0a 2a 2a 0a 2a 2a 20 3c 6c 69 3e 3c 70 3e  i>.**.** <li><p>
111f0 20 20 54 68 65 20 69 6e 69 74 69 61 6c 20 63 6f    The initial co
11200 6e 74 65 6e 74 20 69 73 20 55 54 46 2d 38 20 74  ntent is UTF-8 t
11210 65 78 74 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  ext and sqlite3_
11220 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29  column_bytes16()
11230 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   or.**          
11240 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
11250 65 78 74 31 36 28 29 20 69 73 20 63 61 6c 6c 65  ext16() is calle
11260 64 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 20  d.  The content 
11270 6d 75 73 74 20 62 65 20 63 6f 6e 76 65 72 74 65  must be converte
11280 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f  d.**          to
11290 20 55 54 46 2d 31 36 2e 3c 2f 70 3e 3c 2f 6c 69   UTF-16.</p></li
112a0 3e 0a 2a 2a 0a 2a 2a 20 3c 6c 69 3e 3c 70 3e 20  >.**.** <li><p> 
112b0 20 54 68 65 20 69 6e 69 74 69 61 6c 20 63 6f 6e   The initial con
112c0 74 65 6e 74 20 69 73 20 55 54 46 2d 31 36 20 74  tent is UTF-16 t
112d0 65 78 74 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  ext and sqlite3_
112e0 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 20 6f  column_bytes() o
112f0 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 71  r.**          sq
11300 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
11310 74 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 20 20  t() is called.  
11320 54 68 65 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74  The content must
11330 20 62 65 20 63 6f 6e 76 65 72 74 65 64 0a 2a 2a   be converted.**
11340 20 20 20 20 20 20 20 20 20 20 74 6f 20 55 54 46            to UTF
11350 2d 38 2e 3c 2f 70 3e 3c 2f 6c 69 3e 0a 2a 2a 20  -8.</p></li>.** 
11360 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76  </ul>.**.** Conv
11370 65 72 73 69 6f 6e 73 20 62 65 74 77 65 65 6e 20  ersions between 
11380 55 54 46 2d 31 36 62 65 20 61 6e 64 20 55 54 46  UTF-16be and UTF
11390 2d 31 36 6c 65 20 61 72 65 20 61 6c 77 61 79 73  -16le are always
113a0 20 64 6f 6e 65 20 69 6e 20 70 6c 61 63 65 20 61   done in place a
113b0 6e 64 20 64 6f 0a 2a 2a 20 6e 6f 74 20 69 6e 76  nd do.** not inv
113c0 61 6c 69 64 61 74 65 20 61 20 70 72 69 6f 72 20  alidate a prior 
113d0 70 6f 69 6e 74 65 72 2c 20 74 68 6f 75 67 68 20  pointer, though 
113e0 6f 66 20 63 6f 75 72 73 65 20 74 68 65 20 63 6f  of course the co
113f0 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 62 75 66  ntent of the buf
11400 66 65 72 0a 2a 2a 20 74 68 61 74 20 74 68 65 20  fer.** that the 
11410 70 72 69 6f 72 20 70 6f 69 6e 74 65 72 20 70 6f  prior pointer po
11420 69 6e 74 73 20 74 6f 20 77 69 6c 6c 20 68 61 76  ints to will hav
11430 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 2e  e been modified.
11440 20 20 4f 74 68 65 72 20 6b 69 6e 64 73 0a 2a 2a    Other kinds.**
11450 20 6f 66 20 63 6f 6e 76 65 72 73 69 6f 6e 20 61   of conversion a
11460 72 65 20 64 6f 6e 65 20 69 6e 20 70 6c 61 63 65  re done in place
11470 20 77 68 65 6e 20 69 74 20 69 73 20 70 6f 73 73   when it is poss
11480 69 62 6c 65 2c 20 62 75 74 20 73 6f 6d 65 74 69  ible, but someti
11490 6d 65 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20  me it is.** not 
114a0 70 6f 73 73 69 62 6c 65 20 61 6e 64 20 69 6e 20  possible and in 
114b0 74 68 6f 73 65 20 63 61 73 65 73 20 70 72 69 6f  those cases prio
114c0 72 20 70 6f 69 6e 74 65 72 73 20 61 72 65 20 69  r pointers are i
114d0 6e 76 61 6c 69 64 61 74 65 64 2e 20 20 0a 2a 2a  nvalidated.  .**
114e0 0a 2a 2a 20 54 68 65 20 73 61 66 65 73 74 20 61  .** The safest a
114f0 6e 64 20 65 61 73 69 65 73 74 20 74 6f 20 72 65  nd easiest to re
11500 6d 65 6d 62 65 72 20 70 6f 6c 69 63 79 20 69 73  member policy is
11510 20 74 6f 20 69 6e 76 6f 6b 65 20 74 68 65 73 65   to invoke these
11520 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 69 6e 20   routines.** in 
11530 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  one of the follo
11540 77 69 6e 67 20 77 61 79 73 3a 0a 2a 2a 0a 2a 2a  wing ways:.**.**
11550 20 20 3c 75 6c 3e 0a 2a 2a 20 20 3c 6c 69 3e 73    <ul>.**  <li>s
11560 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
11570 78 74 28 29 20 66 6f 6c 6c 6f 77 65 64 20 62 79  xt() followed by
11580 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
11590 62 79 74 65 73 28 29 3c 2f 6c 69 3e 0a 2a 2a 20  bytes()</li>.** 
115a0 20 3c 6c 69 3e 73 71 6c 69 74 65 33 5f 63 6f 6c   <li>sqlite3_col
115b0 75 6d 6e 5f 62 6c 6f 62 28 29 20 66 6f 6c 6c 6f  umn_blob() follo
115c0 77 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63  wed by sqlite3_c
115d0 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 3c 2f 6c  olumn_bytes()</l
115e0 69 3e 0a 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69 74  i>.**  <li>sqlit
115f0 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36  e3_column_text16
11600 28 29 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 73  () followed by s
11610 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
11620 74 65 73 31 36 28 29 3c 2f 6c 69 3e 0a 2a 2a 20  tes16()</li>.** 
11630 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 49 6e 20   </ul>.**.** In 
11640 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 79 6f 75  other words, you
11650 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 73 71 6c   should call sql
11660 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
11670 28 29 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  (), sqlite3_colu
11680 6d 6e 5f 62 6c 6f 62 28 29 2c 0a 2a 2a 20 6f 72  mn_blob(),.** or
11690 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
116a0 74 65 78 74 31 36 28 29 20 66 69 72 73 74 20 74  text16() first t
116b0 6f 20 66 6f 72 63 65 20 74 68 65 20 72 65 73 75  o force the resu
116c0 6c 74 20 69 6e 74 6f 20 74 68 65 20 64 65 73 69  lt into the desi
116d0 72 65 64 0a 2a 2a 20 66 6f 72 6d 61 74 2c 20 74  red.** format, t
116e0 68 65 6e 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74  hen invoke sqlit
116f0 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
11700 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c  ) or sqlite3_col
11710 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 20 74 6f  umn_bytes16() to
11720 0a 2a 2a 20 66 69 6e 64 20 74 68 65 20 73 69 7a  .** find the siz
11730 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 2e  e of the result.
11740 20 20 44 6f 20 6e 6f 74 20 6d 69 78 20 63 61 6c    Do not mix cal
11750 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c  l to sqlite3_col
11760 75 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a 2a 2a  umn_text() or.**
11770 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
11780 62 6c 6f 62 28 29 20 77 69 74 68 20 63 61 6c 6c  blob() with call
11790 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c  s to sqlite3_col
117a0 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 2e 20 20  umn_bytes16().  
117b0 41 6e 64 20 64 6f 20 6e 6f 74 0a 2a 2a 20 6d 69  And do not.** mi
117c0 78 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74  x calls to sqlit
117d0 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36  e3_column_text16
117e0 28 29 20 77 69 74 68 20 63 61 6c 6c 73 20 74 6f  () with calls to
117f0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
11800 62 79 74 65 73 28 29 2e 0a 2a 2f 0a 63 6f 6e 73  bytes()..*/.cons
11810 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  t void *sqlite3_
11820 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73 71 6c 69  column_blob(sqli
11830 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69  te3_stmt*, int i
11840 43 6f 6c 29 3b 0a 69 6e 74 20 73 71 6c 69 74 65  Col);.int sqlite
11850 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 73  3_column_bytes(s
11860 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
11870 74 20 69 43 6f 6c 29 3b 0a 69 6e 74 20 73 71 6c  t iCol);.int sql
11880 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
11890 73 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  s16(sqlite3_stmt
118a0 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 64 6f  *, int iCol);.do
118b0 75 62 6c 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  uble sqlite3_col
118c0 75 6d 6e 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74  umn_double(sqlit
118d0 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43  e3_stmt*, int iC
118e0 6f 6c 29 3b 0a 69 6e 74 20 73 71 6c 69 74 65 33  ol);.int sqlite3
118f0 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73 71 6c 69  _column_int(sqli
11900 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69  te3_stmt*, int i
11910 43 6f 6c 29 3b 0a 73 71 6c 69 74 65 5f 69 6e 74  Col);.sqlite_int
11920 36 34 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  64 sqlite3_colum
11930 6e 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f  n_int64(sqlite3_
11940 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29  stmt*, int iCol)
11950 3b 0a 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  ;.const unsigned
11960 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63   char *sqlite3_c
11970 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69 74  olumn_text(sqlit
11980 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43  e3_stmt*, int iC
11990 6f 6c 29 3b 0a 63 6f 6e 73 74 20 76 6f 69 64 20  ol);.const void 
119a0 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  *sqlite3_column_
119b0 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 73  text16(sqlite3_s
119c0 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b  tmt*, int iCol);
119d0 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c  .int sqlite3_col
119e0 75 6d 6e 5f 74 79 70 65 28 73 71 6c 69 74 65 33  umn_type(sqlite3
119f0 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c  _stmt*, int iCol
11a00 29 3b 0a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  );.sqlite3_value
11a10 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
11a20 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 73  _value(sqlite3_s
11a30 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b  tmt*, int iCol);
11a40 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
11a50 3a 20 44 65 73 74 72 6f 79 20 41 20 50 72 65 70  : Destroy A Prep
11a60 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 4f  ared Statement O
11a70 62 6a 65 63 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20  bject.**.** The 
11a80 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
11a90 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  () function is c
11aa0 61 6c 6c 65 64 20 74 6f 20 64 65 6c 65 74 65 20  alled to delete 
11ab0 61 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73  a .** [sqlite3_s
11ac0 74 6d 74 20 7c 20 63 6f 6d 70 69 6c 65 64 20 53  tmt | compiled S
11ad0 51 4c 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 49  QL statement]. I
11ae0 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  f the statement 
11af0 77 61 73 0a 2a 2a 20 65 78 65 63 75 74 65 64 20  was.** executed 
11b00 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 6f 72  successfully, or
11b10 20 6e 6f 74 20 65 78 65 63 75 74 65 64 20 61 74   not executed at
11b20 20 61 6c 6c 2c 20 74 68 65 6e 20 53 51 4c 49 54   all, then SQLIT
11b30 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
11b40 2e 0a 2a 2a 20 49 66 20 65 78 65 63 75 74 69 6f  ..** If executio
11b50 6e 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d 65  n of the stateme
11b60 6e 74 20 66 61 69 6c 65 64 20 74 68 65 6e 20 61  nt failed then a
11b70 6e 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 52  n .** [SQLITE_ER
11b80 52 4f 52 20 7c 20 65 72 72 6f 72 20 63 6f 64 65  ROR | error code
11b90 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45  ] or [SQLITE_IOE
11ba0 52 52 5f 52 45 41 44 20 7c 20 65 78 74 65 6e 64  RR_READ | extend
11bb0 65 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 0a 2a  ed error code].*
11bc0 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 0a  * is returned. .
11bd0 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
11be0 6e 65 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64  ne can be called
11bf0 20 61 74 20 61 6e 79 20 70 6f 69 6e 74 20 64 75   at any point du
11c00 72 69 6e 67 20 74 68 65 20 65 78 65 63 75 74 69  ring the executi
11c10 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71  on of the.** [sq
11c20 6c 69 74 65 33 5f 73 74 6d 74 20 7c 20 76 69 72  lite3_stmt | vir
11c30 74 75 61 6c 20 6d 61 63 68 69 6e 65 5d 2e 20 20  tual machine].  
11c40 49 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d  If the virtual m
11c50 61 63 68 69 6e 65 20 68 61 73 20 6e 6f 74 20 0a  achine has not .
11c60 2a 2a 20 63 6f 6d 70 6c 65 74 65 64 20 65 78 65  ** completed exe
11c70 63 75 74 69 6f 6e 20 77 68 65 6e 20 74 68 69 73  cution when this
11c80 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
11c90 65 64 2c 20 74 68 61 74 20 69 73 20 6c 69 6b 65  ed, that is like
11ca0 0a 2a 2a 20 65 6e 63 6f 75 6e 74 65 72 69 6e 67  .** encountering
11cb0 20 61 6e 20 65 72 72 6f 72 20 6f 72 20 61 6e 20   an error or an 
11cc0 69 6e 74 65 72 72 75 70 74 2e 20 20 28 53 65 65  interrupt.  (See
11cd0 20 5b 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72   [sqlite3_interr
11ce0 75 70 74 28 29 5d 2e 29 20 0a 2a 2a 20 49 6e 63  upt()].) .** Inc
11cf0 6f 6d 70 6c 65 74 65 20 75 70 64 61 74 65 73 20  omplete updates 
11d00 6d 61 79 20 62 65 20 72 6f 6c 6c 65 64 20 62 61  may be rolled ba
11d10 63 6b 20 61 6e 64 20 74 72 61 6e 73 61 63 74 69  ck and transacti
11d20 6f 6e 73 20 63 61 6e 63 65 6c 6c 65 64 2c 20 20  ons cancelled,  
11d30 0a 2a 2a 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e  .** depending on
11d40 20 74 68 65 20 63 69 72 63 75 6d 73 74 61 6e 63   the circumstanc
11d50 65 73 2c 20 61 6e 64 20 74 68 65 20 0a 2a 2a 20  es, and the .** 
11d60 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 7c 20  [SQLITE_ERROR | 
11d70 72 65 73 75 6c 74 20 63 6f 64 65 5d 20 72 65 74  result code] ret
11d80 75 72 6e 65 64 20 77 69 6c 6c 20 62 65 20 5b 53  urned will be [S
11d90 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2f  QLITE_ABORT]..*/
11da0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
11db0 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
11dc0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
11dd0 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  Stmt);../*.** CA
11de0 50 49 33 52 45 46 3a 20 52 65 73 65 74 20 41 20  PI3REF: Reset A 
11df0 50 72 65 70 61 72 65 64 20 53 74 61 74 65 6d 65  Prepared Stateme
11e00 6e 74 20 4f 62 6a 65 63 74 0a 2a 2a 0a 2a 2a 20  nt Object.**.** 
11e10 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 65  The sqlite3_rese
11e20 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  t() function is 
11e30 63 61 6c 6c 65 64 20 74 6f 20 72 65 73 65 74 20  called to reset 
11e40 61 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 5f 73 74  a .** [sqlite_st
11e50 6d 74 20 7c 20 63 6f 6d 70 69 6c 65 64 20 53 51  mt | compiled SQ
11e60 4c 20 73 74 61 74 65 6d 65 6e 74 5d 20 6f 62 6a  L statement] obj
11e70 65 63 74 2e 0a 2a 2a 20 62 61 63 6b 20 74 6f 20  ect..** back to 
11e80 69 74 27 73 20 69 6e 69 74 69 61 6c 20 73 74 61  it's initial sta
11e90 74 65 2c 20 72 65 61 64 79 20 74 6f 20 62 65 20  te, ready to be 
11ea0 72 65 2d 65 78 65 63 75 74 65 64 2e 0a 2a 2a 20  re-executed..** 
11eb0 41 6e 79 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  Any SQL statemen
11ec0 74 20 76 61 72 69 61 62 6c 65 73 20 74 68 61 74  t variables that
11ed0 20 68 61 64 20 76 61 6c 75 65 73 20 62 6f 75 6e   had values boun
11ee0 64 20 74 6f 20 74 68 65 6d 20 75 73 69 6e 67 0a  d to them using.
11ef0 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  ** the [sqlite3_
11f00 62 69 6e 64 5f 62 6c 6f 62 20 7c 20 73 71 6c 69  bind_blob | sqli
11f10 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 41 50 49  te3_bind_*() API
11f20 5d 20 72 65 74 61 69 6e 20 74 68 65 69 72 20 76  ] retain their v
11f30 61 6c 75 65 73 2e 0a 2a 2a 20 55 73 65 20 5b 73  alues..** Use [s
11f40 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e  qlite3_clear_bin
11f50 64 69 6e 67 73 28 29 5d 20 74 6f 20 72 65 73 65  dings()] to rese
11f60 74 20 74 68 65 20 62 69 6e 64 69 6e 67 73 2e 0a  t the bindings..
11f70 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
11f80 74 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28  t sqlite3_reset(
11f90 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
11fa0 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  tmt);../*.** CAP
11fb0 49 33 52 45 46 3a 20 43 72 65 61 74 65 20 4f 72  I3REF: Create Or
11fc0 20 52 65 64 65 66 69 6e 65 20 53 51 4c 20 46 75   Redefine SQL Fu
11fd0 6e 63 74 69 6f 6e 73 0a 2a 2a 0a 2a 2a 20 54 68  nctions.**.** Th
11fe0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20  e following two 
11ff0 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 75 73  functions are us
12000 65 64 20 74 6f 20 61 64 64 20 53 51 4c 20 66 75  ed to add SQL fu
12010 6e 63 74 69 6f 6e 73 20 6f 72 20 61 67 67 72 65  nctions or aggre
12020 67 61 74 65 73 0a 2a 2a 20 6f 72 20 74 6f 20 72  gates.** or to r
12030 65 64 65 66 69 6e 65 20 74 68 65 20 62 65 68 61  edefine the beha
12040 76 69 6f 72 20 6f 66 20 65 78 69 73 74 69 6e 67  vior of existing
12050 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6f   SQL functions o
12060 72 20 61 67 67 72 65 67 61 74 65 73 2e 20 20 54  r aggregates.  T
12070 68 65 0a 2a 2a 20 64 69 66 66 65 72 65 6e 63 65  he.** difference
12080 20 6f 6e 6c 79 20 62 65 74 77 65 65 6e 20 74 68   only between th
12090 65 20 74 77 6f 20 69 73 20 74 68 61 74 20 74 68  e two is that th
120a0 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74  e second paramet
120b0 65 72 2c 20 74 68 65 0a 2a 2a 20 6e 61 6d 65 20  er, the.** name 
120c0 6f 66 20 74 68 65 20 28 73 63 61 6c 61 72 29 20  of the (scalar) 
120d0 66 75 6e 63 74 69 6f 6e 20 6f 72 20 61 67 67 72  function or aggr
120e0 65 67 61 74 65 2c 20 69 73 20 65 6e 63 6f 64 65  egate, is encode
120f0 64 20 69 6e 20 55 54 46 2d 38 20 66 6f 72 0a 2a  d in UTF-8 for.*
12100 2a 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  * sqlite3_create
12110 5f 66 75 6e 63 74 69 6f 6e 28 29 20 61 6e 64 20  _function() and 
12120 55 54 46 2d 31 36 20 66 6f 72 20 73 71 6c 69 74  UTF-16 for sqlit
12130 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
12140 6f 6e 31 36 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68  on16()..**.** Th
12150 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
12160 20 69 73 20 74 68 65 20 5b 73 71 6c 69 74 65 33   is the [sqlite3
12170 20 7c 20 64 61 74 61 62 61 73 65 20 68 61 6e 64   | database hand
12180 6c 65 5d 20 74 68 61 74 20 68 6f 6c 64 73 20 74  le] that holds t
12190 68 65 0a 2a 2a 20 53 51 4c 20 66 75 6e 63 74 69  he.** SQL functi
121a0 6f 6e 20 6f 72 20 61 67 67 72 65 67 61 74 65 20  on or aggregate 
121b0 69 73 20 74 6f 20 62 65 20 61 64 64 65 64 20 6f  is to be added o
121c0 72 20 72 65 64 65 66 69 6e 65 64 2e 20 49 66 20  r redefined. If 
121d0 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 70 72 6f 67  a single.** prog
121e0 72 61 6d 20 75 73 65 73 20 6d 6f 72 65 20 74 68  ram uses more th
121f0 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20  an one database 
12200 68 61 6e 64 6c 65 20 69 6e 74 65 72 6e 61 6c 6c  handle internall
12210 79 2c 20 74 68 65 6e 20 53 51 4c 0a 2a 2a 20 66  y, then SQL.** f
12220 75 6e 63 74 69 6f 6e 73 20 6f 72 20 61 67 67 72  unctions or aggr
12230 65 67 61 74 65 73 20 6d 75 73 74 20 62 65 20 61  egates must be a
12240 64 64 65 64 20 69 6e 64 69 76 69 64 75 61 6c 6c  dded individuall
12250 79 20 74 6f 20 65 61 63 68 20 64 61 74 61 62 61  y to each databa
12260 73 65 0a 2a 2a 20 68 61 6e 64 6c 65 20 77 69 74  se.** handle wit
12270 68 20 77 68 69 63 68 20 74 68 65 79 20 77 69 6c  h which they wil
12280 6c 20 62 65 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a  l be used..**.**
12290 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61   The second para
122a0 6d 65 74 65 72 20 69 73 20 74 68 65 20 6e 61 6d  meter is the nam
122b0 65 20 6f 66 20 74 68 65 20 53 51 4c 20 66 75 6e  e of the SQL fun
122c0 63 74 69 6f 6e 20 74 6f 20 62 65 20 63 72 65 61  ction to be crea
122d0 74 65 64 0a 2a 2a 20 6f 72 20 72 65 64 65 66 69  ted.** or redefi
122e0 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 6c 65 6e 67  ned..** The leng
122f0 74 68 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 69  th of the name i
12300 73 20 6c 69 6d 69 74 65 64 20 74 6f 20 32 35 35  s limited to 255
12310 20 62 79 74 65 73 2c 20 65 78 63 6c 75 73 69 76   bytes, exclusiv
12320 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20 7a 65 72  e of the .** zer
12330 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 2e 20 20 4e  o-terminator.  N
12340 6f 74 65 20 74 68 61 74 20 74 68 65 20 6e 61 6d  ote that the nam
12350 65 20 6c 65 6e 67 74 68 20 6c 69 6d 69 74 20 69  e length limit i
12360 73 20 69 6e 20 62 79 74 65 73 2c 20 6e 6f 74 0a  s in bytes, not.
12370 2a 2a 20 63 68 61 72 61 63 74 65 72 73 2e 20 20  ** characters.  
12380 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 63  Any attempt to c
12390 72 65 61 74 65 20 61 20 66 75 6e 63 74 69 6f 6e  reate a function
123a0 20 77 69 74 68 20 61 20 6c 6f 6e 67 65 72 20 6e   with a longer n
123b0 61 6d 65 0a 2a 2a 20 77 69 6c 6c 20 72 65 73 75  ame.** will resu
123c0 6c 74 20 69 6e 20 61 6e 20 53 51 4c 49 54 45 5f  lt in an SQLITE_
123d0 45 52 52 4f 52 20 65 72 72 6f 72 2e 0a 2a 2a 0a  ERROR error..**.
123e0 2a 2a 20 54 68 65 20 74 68 69 72 64 20 70 61 72  ** The third par
123f0 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 6e 75  ameter is the nu
12400 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
12410 73 20 74 68 61 74 20 74 68 65 20 53 51 4c 20 66  s that the SQL f
12420 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67  unction or.** ag
12430 67 72 65 67 61 74 65 20 74 61 6b 65 73 2e 20 49  gregate takes. I
12440 66 20 74 68 69 73 20 70 61 72 61 6d 65 74 65 72  f this parameter
12450 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68   is negative, th
12460 65 6e 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74  en the SQL funct
12470 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67  ion or.** aggreg
12480 61 74 65 20 6d 61 79 20 74 61 6b 65 20 61 6e 79  ate may take any
12490 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d   number of argum
124a0 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ents..**.** The 
124b0 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72  fourth parameter
124c0 2c 20 65 54 65 78 74 52 65 70 2c 20 73 70 65 63  , eTextRep, spec
124d0 69 66 69 65 73 20 77 68 61 74 20 0a 2a 2a 20 5b  ifies what .** [
124e0 53 51 4c 49 54 45 5f 55 54 46 38 20 7c 20 74 65  SQLITE_UTF8 | te
124f0 78 74 20 65 6e 63 6f 64 69 6e 67 5d 20 74 68 69  xt encoding] thi
12500 73 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 70  s SQL function p
12510 72 65 66 65 72 73 20 66 6f 72 0a 2a 2a 20 69 74  refers for.** it
12520 73 20 70 61 72 61 6d 65 74 65 72 73 2e 20 20 41  s parameters.  A
12530 6e 79 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  ny SQL function 
12540 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 73  implementation s
12550 68 6f 75 6c 64 20 62 65 20 61 62 6c 65 20 74 6f  hould be able to
12560 20 77 6f 72 6b 0a 2a 2a 20 77 6f 72 6b 20 77 69   work.** work wi
12570 74 68 20 55 54 46 2d 38 2c 20 55 54 46 2d 31 36  th UTF-8, UTF-16
12580 6c 65 2c 20 6f 72 20 55 54 46 2d 31 36 62 65 2e  le, or UTF-16be.
12590 20 20 42 75 74 20 73 6f 6d 65 20 69 6d 70 6c 65    But some imple
125a0 6d 65 6e 74 61 74 69 6f 6e 73 20 6d 61 79 20 62  mentations may b
125b0 65 0a 2a 2a 20 6d 6f 72 65 20 65 66 66 69 63 69  e.** more effici
125c0 65 6e 74 20 77 69 74 68 20 6f 6e 65 20 65 6e 63  ent with one enc
125d0 6f 64 69 6e 67 20 74 68 61 6e 20 61 6e 6f 74 68  oding than anoth
125e0 65 72 2e 20 20 49 74 20 69 73 20 61 6c 6c 6f 77  er.  It is allow
125f0 65 64 20 74 6f 0a 2a 2a 20 69 6e 76 6f 6b 65 20  ed to.** invoke 
12600 73 71 6c 69 74 65 5f 63 72 65 61 74 65 5f 66 75  sqlite_create_fu
12610 6e 63 74 69 6f 6e 28 29 20 6f 72 20 73 71 6c 69  nction() or sqli
12620 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
12630 69 6f 6e 31 36 28 29 20 6d 75 6c 74 69 70 6c 65  ion16() multiple
12640 0a 2a 2a 20 74 69 6d 65 73 20 77 69 74 68 20 74  .** times with t
12650 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e  he same function
12660 20 62 75 74 20 77 69 74 68 20 64 69 66 66 65 72   but with differ
12670 65 6e 74 20 76 61 6c 75 65 73 20 6f 66 20 65 54  ent values of eT
12680 65 78 74 52 65 70 2e 0a 2a 2a 20 57 68 65 6e 20  extRep..** When 
12690 6d 75 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d 65  multiple impleme
126a0 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20  ntations of the 
126b0 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20 61 72  same function ar
126c0 65 20 61 76 61 69 6c 61 62 6c 65 2c 20 53 51 4c  e available, SQL
126d0 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 70 69 63 6b  ite.** will pick
126e0 20 74 68 65 20 6f 6e 65 20 74 68 61 74 20 69 6e   the one that in
126f0 76 6f 6c 76 65 73 20 74 68 65 20 6c 65 61 73 74  volves the least
12700 20 61 6d 6f 75 6e 74 20 6f 66 20 64 61 74 61 20   amount of data 
12710 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 2a 2a 20 49  conversion..** I
12720 66 20 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20  f there is only 
12730 61 20 73 69 6e 67 6c 65 20 69 6d 70 6c 65 6d 65  a single impleme
12740 6e 74 61 74 69 6f 6e 20 77 68 69 63 68 20 64 6f  ntation which do
12750 65 73 20 6e 6f 74 20 63 61 72 65 20 77 68 61 74  es not care what
12760 0a 2a 2a 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  .** text encodin
12770 67 20 69 73 20 75 73 65 64 2c 20 74 68 65 6e 20  g is used, then 
12780 74 68 65 20 66 6f 75 72 74 68 20 61 72 67 75 6d  the fourth argum
12790 65 6e 74 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a  ent should be.**
127a0 20 5b 53 51 4c 49 54 45 5f 41 4e 59 5d 2e 0a 2a   [SQLITE_ANY]..*
127b0 2a 0a 2a 2a 20 54 68 65 20 66 69 66 74 68 20 70  *.** The fifth p
127c0 61 72 61 6d 65 74 65 72 20 69 73 20 61 6e 20 61  arameter is an a
127d0 72 62 69 74 72 61 72 79 20 70 6f 69 6e 74 65 72  rbitrary pointer
127e0 2e 20 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74  .  The implement
127f0 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20  ation.** of the 
12800 66 75 6e 63 74 69 6f 6e 20 63 61 6e 20 67 61 69  function can gai
12810 6e 20 61 63 63 65 73 73 20 74 6f 20 74 68 69 73  n access to this
12820 20 70 6f 69 6e 74 65 72 20 75 73 69 6e 67 0a 2a   pointer using.*
12830 2a 20 5b 73 71 6c 69 74 65 5f 75 73 65 72 5f 64  * [sqlite_user_d
12840 61 74 61 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68  ata()]..**.** Th
12850 65 20 73 65 76 65 6e 74 68 2c 20 65 69 67 68 74  e seventh, eight
12860 68 20 61 6e 64 20 6e 69 6e 74 68 20 70 61 72 61  h and ninth para
12870 6d 65 74 65 72 73 2c 20 78 46 75 6e 63 2c 20 78  meters, xFunc, x
12880 53 74 65 70 20 61 6e 64 20 78 46 69 6e 61 6c 2c  Step and xFinal,
12890 20 61 72 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 73   are.** pointers
128a0 20 74 6f 20 43 2d 6c 61 6e 67 75 61 67 65 20 66   to C-language f
128b0 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d  unctions that im
128c0 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 4c 0a  plement the SQL.
128d0 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 6f 72 20 61  ** function or a
128e0 67 67 72 65 67 61 74 65 2e 20 41 20 73 63 61 6c  ggregate. A scal
128f0 61 72 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  ar SQL function 
12900 72 65 71 75 69 72 65 73 20 61 6e 20 69 6d 70 6c  requires an impl
12910 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 0a 2a 2a  ementation of.**
12920 20 74 68 65 20 78 46 75 6e 63 20 63 61 6c 6c 62   the xFunc callb
12930 61 63 6b 20 6f 6e 6c 79 2c 20 4e 55 4c 4c 20 70  ack only, NULL p
12940 6f 69 6e 74 65 72 73 20 73 68 6f 75 6c 64 20 62  ointers should b
12950 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  e passed as the 
12960 78 53 74 65 70 0a 2a 2a 20 61 6e 64 20 78 46 69  xStep.** and xFi
12970 6e 61 6c 20 70 61 72 61 6d 65 74 65 72 73 2e 20  nal parameters. 
12980 41 6e 20 61 67 67 72 65 67 61 74 65 20 53 51 4c  An aggregate SQL
12990 20 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 72   function requir
129a0 65 73 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61  es an implementa
129b0 74 69 6f 6e 0a 2a 2a 20 6f 66 20 78 53 74 65 70  tion.** of xStep
129c0 20 61 6e 64 20 78 46 69 6e 61 6c 20 61 6e 64 20   and xFinal and 
129d0 4e 55 4c 4c 20 73 68 6f 75 6c 64 20 62 65 20 70  NULL should be p
129e0 61 73 73 65 64 20 66 6f 72 20 78 46 75 6e 63 2e  assed for xFunc.
129f0 20 54 6f 20 64 65 6c 65 74 65 20 61 6e 0a 2a 2a   To delete an.**
12a00 20 65 78 69 73 74 69 6e 67 20 53 51 4c 20 66 75   existing SQL fu
12a10 6e 63 74 69 6f 6e 20 6f 72 20 61 67 67 72 65 67  nction or aggreg
12a20 61 74 65 2c 20 70 61 73 73 20 4e 55 4c 4c 20 66  ate, pass NULL f
12a30 6f 72 20 61 6c 6c 20 74 68 72 65 65 20 66 75 6e  or all three fun
12a40 63 74 69 6f 6e 0a 2a 2a 20 63 61 6c 6c 62 61 63  ction.** callbac
12a50 6b 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 70  k..**.** It is p
12a60 65 72 6d 69 74 74 65 64 20 74 6f 20 72 65 67 69  ermitted to regi
12a70 73 74 65 72 20 6d 75 6c 74 69 70 6c 65 20 69 6d  ster multiple im
12a80 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66  plementations of
12a90 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 66 75 6e   the same.** fun
12aa0 63 74 69 6f 6e 73 20 77 69 74 68 20 74 68 65 20  ctions with the 
12ab0 73 61 6d 65 20 6e 61 6d 65 20 62 75 74 20 77 69  same name but wi
12ac0 74 68 20 65 69 74 68 65 72 20 64 69 66 66 65 72  th either differ
12ad0 69 6e 67 20 6e 75 6d 62 65 72 73 20 6f 66 0a 2a  ing numbers of.*
12ae0 2a 20 61 72 67 75 6d 65 6e 74 73 20 6f 72 20 64  * arguments or d
12af0 69 66 66 65 72 69 6e 67 20 70 65 72 66 65 72 72  iffering perferr
12b00 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67  ed text encoding
12b10 73 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  s.  SQLite will 
12b20 75 73 65 0a 2a 2a 20 74 68 65 20 69 6d 70 6c 65  use.** the imple
12b30 6d 65 6e 74 61 74 69 6f 6e 20 6d 6f 73 74 20 63  mentation most c
12b40 6c 6f 73 65 6c 79 20 6d 61 74 63 68 65 73 20 74  losely matches t
12b50 68 65 20 77 61 79 20 69 6e 20 77 68 69 63 68 20  he way in which 
12b60 74 68 65 0a 2a 2a 20 53 51 4c 20 66 75 6e 63 74  the.** SQL funct
12b70 69 6f 6e 20 69 73 20 75 73 65 64 2e 0a 2a 2f 0a  ion is used..*/.
12b80 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61  int sqlite3_crea
12b90 74 65 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20 73  te_function(.  s
12ba0 71 6c 69 74 65 33 20 2a 2c 0a 20 20 63 6f 6e 73  qlite3 *,.  cons
12bb0 74 20 63 68 61 72 20 2a 7a 46 75 6e 63 74 69 6f  t char *zFunctio
12bc0 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72  nName,.  int nAr
12bd0 67 2c 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65  g,.  int eTextRe
12be0 70 2c 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 76 6f  p,.  void*,.  vo
12bf0 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69  id (*xFunc)(sqli
12c00 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
12c10 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a  ,sqlite3_value**
12c20 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 53 74 65  ),.  void (*xSte
12c30 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  p)(sqlite3_conte
12c40 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  xt*,int,sqlite3_
12c50 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64  value**),.  void
12c60 20 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69 74   (*xFinal)(sqlit
12c70 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0a 29 3b 0a  e3_context*).);.
12c80 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61  int sqlite3_crea
12c90 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 0a 20  te_function16(. 
12ca0 20 73 71 6c 69 74 65 33 2a 2c 0a 20 20 63 6f 6e   sqlite3*,.  con
12cb0 73 74 20 76 6f 69 64 20 2a 7a 46 75 6e 63 74 69  st void *zFuncti
12cc0 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 41  onName,.  int nA
12cd0 72 67 2c 0a 20 20 69 6e 74 20 65 54 65 78 74 52  rg,.  int eTextR
12ce0 65 70 2c 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 76  ep,.  void*,.  v
12cf0 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c  oid (*xFunc)(sql
12d00 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
12d10 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  t,sqlite3_value*
12d20 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 53 74  *),.  void (*xSt
12d30 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ep)(sqlite3_cont
12d40 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
12d50 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69  _value**),.  voi
12d60 64 20 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69  d (*xFinal)(sqli
12d70 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0a 29 3b  te3_context*).);
12d80 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
12d90 3a 20 54 65 78 74 20 45 6e 63 6f 64 69 6e 67 73  : Text Encodings
12da0 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e  .**.** These con
12db0 73 74 61 6e 74 20 64 65 66 69 6e 65 20 69 6e 74  stant define int
12dc0 65 67 65 72 20 63 6f 64 65 73 20 74 68 61 74 20  eger codes that 
12dd0 72 65 70 72 65 73 65 6e 74 20 74 68 65 20 76 61  represent the va
12de0 72 69 6f 75 73 0a 2a 2a 20 74 65 78 74 20 65 6e  rious.** text en
12df0 63 6f 64 69 6e 67 73 20 73 75 70 70 6f 72 74 65  codings supporte
12e00 64 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a  d by SQLite..*/.
12e10 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55  #define SQLITE_U
12e20 54 46 38 20 20 20 20 20 20 20 20 20 20 20 31 0a  TF8           1.
12e30 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55  #define SQLITE_U
12e40 54 46 31 36 4c 45 20 20 20 20 20 20 20 20 32 0a  TF16LE        2.
12e50 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55  #define SQLITE_U
12e60 54 46 31 36 42 45 20 20 20 20 20 20 20 20 33 0a  TF16BE        3.
12e70 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55  #define SQLITE_U
12e80 54 46 31 36 20 20 20 20 20 20 20 20 20 20 34 20  TF16          4 
12e90 20 20 20 2f 2a 20 55 73 65 20 6e 61 74 69 76 65     /* Use native
12ea0 20 62 79 74 65 20 6f 72 64 65 72 20 2a 2f 0a 23   byte order */.#
12eb0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 4e  define SQLITE_AN
12ec0 59 20 20 20 20 20 20 20 20 20 20 20 20 35 20 20  Y            5  
12ed0 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 63 72 65    /* sqlite3_cre
12ee0 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 6f 6e 6c  ate_function onl
12ef0 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  y */.#define SQL
12f00 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45  ITE_UTF16_ALIGNE
12f10 44 20 20 38 20 20 20 20 2f 2a 20 73 71 6c 69 74  D  8    /* sqlit
12f20 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
12f30 69 6f 6e 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f 2a 0a  ion only */../*.
12f40 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 62 73  ** CAPI3REF: Obs
12f50 6f 6c 65 74 65 20 46 75 6e 63 74 69 6f 6e 73 0a  olete Functions.
12f60 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 66 75 6e 63  **.** These func
12f70 74 69 6f 6e 73 20 61 72 65 20 61 6c 6c 20 6e 6f  tions are all no
12f80 77 20 6f 62 73 6f 6c 65 74 65 2e 20 20 49 6e 20  w obsolete.  In 
12f90 6f 72 64 65 72 20 74 6f 20 6d 61 69 6e 74 61 69  order to maintai
12fa0 6e 0a 2a 2a 20 62 61 63 6b 77 61 72 64 73 20 63  n.** backwards c
12fb0 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 77 69 74  ompatibility wit
12fc0 68 20 6f 6c 64 65 72 20 63 6f 64 65 2c 20 77 65  h older code, we
12fd0 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 73 75 70   continue to sup
12fe0 70 6f 72 74 0a 2a 2a 20 74 68 65 73 65 20 66 75  port.** these fu
12ff0 6e 63 74 69 6f 6e 73 2e 20 20 48 6f 77 65 76 65  nctions.  Howeve
13000 72 2c 20 6e 65 77 20 64 65 76 65 6c 6f 70 6d 65  r, new developme
13010 6e 74 20 70 72 6f 6a 65 63 74 73 20 73 68 6f 75  nt projects shou
13020 6c 64 20 61 76 6f 69 64 0a 2a 2a 20 74 68 65 20  ld avoid.** the 
13030 75 73 65 20 6f 66 20 74 68 65 73 65 20 66 75 6e  use of these fun
13040 63 74 69 6f 6e 73 2e 20 20 54 6f 20 68 65 6c 70  ctions.  To help
13050 20 65 6e 63 6f 75 72 61 67 65 20 70 65 6f 70 6c   encourage peopl
13060 65 20 74 6f 20 61 76 6f 69 64 0a 2a 2a 20 75 73  e to avoid.** us
13070 69 6e 67 20 74 68 65 73 65 20 66 75 6e 63 74 69  ing these functi
13080 6f 6e 73 2c 20 77 65 20 61 72 65 20 6e 6f 74 20  ons, we are not 
13090 67 6f 69 6e 67 20 74 6f 20 74 65 6c 6c 20 79 6f  going to tell yo
130a0 75 20 77 61 6e 74 20 74 68 65 79 20 64 6f 2e 0a  u want they do..
130b0 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 61  */.int sqlite3_a
130c0 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 73  ggregate_count(s
130d0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29  qlite3_context*)
130e0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
130f0 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72 65 64   sqlite3_expired
13100 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b  (sqlite3_stmt*);
13110 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 72 61  .int sqlite3_tra
13120 6e 73 66 65 72 5f 62 69 6e 64 69 6e 67 73 28 73  nsfer_bindings(s
13130 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 73 71  qlite3_stmt*, sq
13140 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 69 6e  lite3_stmt*);.in
13150 74 20 73 71 6c 69 74 65 33 5f 67 6c 6f 62 61 6c  t sqlite3_global
13160 5f 72 65 63 6f 76 65 72 28 76 6f 69 64 29 3b 0a  _recover(void);.
13170 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
13180 3a 20 4f 62 74 61 69 6e 69 6e 67 20 53 51 4c 20  : Obtaining SQL 
13190 46 75 6e 63 74 69 6f 6e 20 50 61 72 61 6d 65 74  Function Paramet
131a0 65 72 20 56 61 6c 75 65 73 0a 2a 2a 0a 2a 2a 20  er Values.**.** 
131b0 54 68 65 20 43 2d 6c 61 6e 67 75 61 67 65 20 69  The C-language i
131c0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
131d0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61   SQL functions a
131e0 6e 64 20 61 67 67 72 65 67 61 74 65 73 20 75 73  nd aggregates us
131f0 65 73 0a 2a 2a 20 74 68 69 73 20 73 65 74 20 6f  es.** this set o
13200 66 20 69 6e 74 65 72 66 61 63 65 20 72 6f 75 74  f interface rout
13210 69 6e 65 73 20 74 6f 20 61 63 63 65 73 73 20 74  ines to access t
13220 68 65 20 70 61 72 61 6d 65 74 65 72 20 76 61 6c  he parameter val
13230 75 65 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 66 75  ues on.** the fu
13240 6e 63 74 69 6f 6e 20 6f 72 20 61 67 67 72 65 67  nction or aggreg
13250 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78  ate..**.** The x
13260 46 75 6e 63 20 28 66 6f 72 20 73 63 61 6c 61 72  Func (for scalar
13270 20 66 75 6e 63 74 69 6f 6e 73 29 20 6f 72 20 78   functions) or x
13280 53 74 65 70 20 28 66 6f 72 20 61 67 67 72 65 67  Step (for aggreg
13290 61 74 65 73 29 20 70 61 72 61 6d 65 74 65 72 73  ates) parameters
132a0 0a 2a 2a 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  .** to [sqlite3_
132b0 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28  create_function(
132c0 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  )] and [sqlite3_
132d0 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31  create_function1
132e0 36 28 29 5d 0a 2a 2a 20 64 65 66 69 6e 65 20 63  6()].** define c
132f0 61 6c 6c 62 61 63 6b 73 20 74 68 61 74 20 69 6d  allbacks that im
13300 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 4c 20  plement the SQL 
13310 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 61 67  functions and ag
13320 67 72 65 67 61 74 65 73 2e 0a 2a 2a 20 54 68 65  gregates..** The
13330 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74   4th parameter t
13340 6f 20 74 68 65 73 65 20 63 61 6c 6c 62 61 63 6b  o these callback
13350 73 20 69 73 20 61 6e 20 61 72 72 61 79 20 6f 66  s is an array of
13360 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20   pointers to.** 
13370 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20  [sqlite3_value] 
13380 6f 62 6a 65 63 74 73 2e 20 20 54 68 65 72 65 20  objects.  There 
13390 69 73 20 6f 6e 65 20 5b 73 71 6c 69 74 65 33 5f  is one [sqlite3_
133a0 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 66 6f  value] object fo
133b0 72 0a 2a 2a 20 65 61 63 68 20 70 61 72 61 6d 65  r.** each parame
133c0 74 65 72 20 74 6f 20 74 68 65 20 53 51 4c 20 66  ter to the SQL f
133d0 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 73 65 20  unction.  These 
133e0 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65  routines are use
133f0 64 20 74 6f 0a 2a 2a 20 65 78 74 72 61 63 74 20  d to.** extract 
13400 76 61 6c 75 65 73 20 66 72 6f 6d 20 74 68 65 20  values from the 
13410 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20  [sqlite3_value] 
13420 6f 62 6a 65 63 74 73 2e 0a 2a 2a 0a 2a 2a 20 54  objects..**.** T
13430 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 77 6f  hese routines wo
13440 72 6b 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65  rk just like the
13450 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 0a   corresponding .
13460 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ** [sqlite3_colu
13470 6d 6e 5f 62 6c 6f 62 20 7c 20 73 71 6c 69 74 65  mn_blob | sqlite
13480 33 5f 63 6f 6c 75 6d 6e 5f 2a 20 72 6f 75 74 69  3_column_* routi
13490 6e 65 73 5d 20 65 78 63 65 70 74 20 74 68 61 74  nes] except that
134a0 20 0a 2a 2a 20 74 68 65 73 65 20 72 6f 75 74 69   .** these routi
134b0 6e 65 73 20 74 61 6b 65 20 61 20 73 69 6e 67 6c  nes take a singl
134c0 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  e [sqlite3_value
134d0 2a 5d 20 70 6f 69 6e 74 65 72 20 69 6e 73 74 65  *] pointer inste
134e0 61 64 0a 2a 2a 20 6f 66 20 61 6e 20 5b 73 71 6c  ad.** of an [sql
134f0 69 74 65 33 5f 73 74 6d 74 2a 5d 20 70 6f 69 6e  ite3_stmt*] poin
13500 74 65 72 20 61 6e 64 20 61 6e 20 69 6e 74 65 67  ter and an integ
13510 65 72 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  er column number
13520 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
13530 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36  te3_value_text16
13540 28 29 20 69 6e 74 65 72 66 61 63 65 20 65 78 74  () interface ext
13550 72 61 63 74 73 20 61 20 55 54 46 31 36 20 73 74  racts a UTF16 st
13560 72 69 6e 67 0a 2a 2a 20 69 6e 20 74 68 65 20 6e  ring.** in the n
13570 61 74 69 76 65 20 62 79 74 65 2d 6f 72 64 65 72  ative byte-order
13580 20 6f 66 20 74 68 65 20 68 6f 73 74 20 6d 61 63   of the host mac
13590 68 69 6e 65 2e 20 20 54 68 65 0a 2a 2a 20 73 71  hine.  The.** sq
135a0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
135b0 31 36 62 65 28 29 20 61 6e 64 20 73 71 6c 69 74  16be() and sqlit
135c0 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c  e3_value_text16l
135d0 65 28 29 20 69 6e 74 65 72 66 61 63 65 73 0a 2a  e() interfaces.*
135e0 2a 20 65 78 74 72 61 63 74 20 55 54 46 31 36 20  * extract UTF16 
135f0 73 74 72 69 6e 67 73 20 61 73 20 62 69 67 2d 65  strings as big-e
13600 6e 64 69 61 6e 20 61 6e 64 20 6c 69 74 74 6c 65  ndian and little
13610 2d 65 6e 64 69 61 6e 20 72 65 73 70 65 63 74 69  -endian respecti
13620 76 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  vely..**.** The 
13630 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75  sqlite3_value_nu
13640 6d 65 72 69 63 5f 74 79 70 65 28 29 20 69 6e 74  meric_type() int
13650 65 72 66 61 63 65 20 61 74 74 65 6d 70 74 73 20  erface attempts 
13660 74 6f 20 61 70 70 6c 79 0a 2a 2a 20 6e 75 6d 65  to apply.** nume
13670 72 69 63 20 61 66 66 69 6e 69 74 79 20 74 6f 20  ric affinity to 
13680 74 68 65 20 76 61 6c 75 65 2e 20 20 54 68 69 73  the value.  This
13690 20 6d 65 61 6e 73 20 74 68 61 74 20 61 6e 20 61   means that an a
136a0 74 74 65 6d 70 74 20 69 73 0a 2a 2a 20 6d 61 64  ttempt is.** mad
136b0 65 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 65  e to convert the
136c0 20 76 61 6c 75 65 20 74 6f 20 61 6e 20 69 6e 74   value to an int
136d0 65 67 65 72 20 6f 72 20 66 6c 6f 61 74 69 6e 67  eger or floating
136e0 20 70 6f 69 6e 74 2e 20 20 49 66 0a 2a 2a 20 73   point.  If.** s
136f0 75 63 68 20 61 20 63 6f 6e 76 65 72 73 69 6f 6e  uch a conversion
13700 20 69 73 20 70 6f 73 73 69 62 6c 65 20 77 69 74   is possible wit
13710 68 6f 75 74 20 6c 6f 73 73 20 6f 66 20 69 6e 66  hout loss of inf
13720 6f 72 6d 61 74 69 6f 6e 20 28 69 6e 20 6f 72 64  ormation (in ord
13730 65 72 0a 2a 2a 20 77 6f 72 64 73 20 69 66 20 74  er.** words if t
13740 68 65 20 76 61 6c 75 65 20 69 73 20 6f 72 69 67  he value is orig
13750 69 6e 61 6c 20 61 20 73 74 72 69 6e 67 20 74 68  inal a string th
13760 61 74 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20  at looks like a 
13770 6e 75 6d 62 65 72 29 0a 2a 2a 20 74 68 65 6e 20  number).** then 
13780 69 74 20 69 73 20 64 6f 6e 65 2e 20 20 4f 74 68  it is done.  Oth
13790 65 72 77 69 73 65 20 6e 6f 20 63 6f 6e 76 65 72  erwise no conver
137a0 73 69 6f 6e 20 6f 63 63 75 72 73 2e 20 20 54 68  sion occurs.  Th
137b0 65 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4e  e .** [SQLITE_IN
137c0 54 45 47 45 52 20 7c 20 64 61 74 61 74 79 70 65  TEGER | datatype
137d0 5d 20 61 66 74 65 72 20 63 6f 6e 76 65 72 73 69  ] after conversi
137e0 6f 6e 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  on is returned..
137f0 2a 2a 0a 2a 2a 20 50 6c 65 61 73 65 20 70 61 79  **.** Please pay
13800 20 70 61 72 74 69 63 75 6c 61 72 20 61 74 74 65   particular atte
13810 6e 74 69 6f 6e 20 74 6f 20 74 68 65 20 66 61 63  ntion to the fac
13820 74 20 74 68 61 74 20 74 68 65 20 70 6f 69 6e 74  t that the point
13830 65 72 20 74 68 61 74 0a 2a 2a 20 69 73 20 72 65  er that.** is re
13840 74 75 72 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c  turned from [sql
13850 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28  ite3_value_blob(
13860 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c  )], [sqlite3_val
13870 75 65 5f 74 65 78 74 28 29 5d 2c 20 6f 72 0a 2a  ue_text()], or.*
13880 2a 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  * [sqlite3_value
13890 5f 74 65 78 74 31 36 28 29 5d 20 63 61 6e 20 62  _text16()] can b
138a0 65 20 69 6e 76 61 6c 69 64 61 74 65 64 20 62 79  e invalidated by
138b0 20 61 20 73 75 62 73 65 71 75 65 6e 74 20 63 61   a subsequent ca
138c0 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65  ll to.** [sqlite
138d0 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 29 5d  3_value_bytes()]
138e0 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  , [sqlite3_value
138f0 5f 62 79 74 65 73 31 36 28 29 5d 2c 20 5b 73 71  _bytes16()], [sq
13900 6c 69 74 65 5f 76 61 6c 75 65 5f 74 65 78 74 28  lite_value_text(
13910 29 5d 2c 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74  )],.** or [sqlit
13920 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28  e3_value_text16(
13930 29 5d 2e 20 20 0a 2a 2f 0a 63 6f 6e 73 74 20 76  )].  .*/.const v
13940 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c  oid *sqlite3_val
13950 75 65 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f  ue_blob(sqlite3_
13960 76 61 6c 75 65 2a 29 3b 0a 69 6e 74 20 73 71 6c  value*);.int sql
13970 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
13980 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
13990 3b 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61  ;.int sqlite3_va
139a0 6c 75 65 5f 62 79 74 65 73 31 36 28 73 71 6c 69  lue_bytes16(sqli
139b0 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 64 6f 75  te3_value*);.dou
139c0 62 6c 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ble sqlite3_valu
139d0 65 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33  e_double(sqlite3
139e0 5f 76 61 6c 75 65 2a 29 3b 0a 69 6e 74 20 73 71  _value*);.int sq
139f0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28  lite3_value_int(
13a00 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
13a10 0a 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 71  .sqlite_int64 sq
13a20 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36  lite3_value_int6
13a30 34 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  4(sqlite3_value*
13a40 29 3b 0a 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  );.const unsigne
13a50 64 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  d char *sqlite3_
13a60 76 61 6c 75 65 5f 74 65 78 74 28 73 71 6c 69 74  value_text(sqlit
13a70 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 63 6f 6e 73  e3_value*);.cons
13a80 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  t void *sqlite3_
13a90 76 61 6c 75 65 5f 74 65 78 74 31 36 28 73 71 6c  value_text16(sql
13aa0 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 63 6f  ite3_value*);.co
13ab0 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
13ac0 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65  3_value_text16le
13ad0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
13ae0 3b 0a 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  ;.const void *sq
13af0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
13b00 31 36 62 65 28 73 71 6c 69 74 65 33 5f 76 61 6c  16be(sqlite3_val
13b10 75 65 2a 29 3b 0a 69 6e 74 20 73 71 6c 69 74 65  ue*);.int sqlite
13b20 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 73 71 6c  3_value_type(sql
13b30 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 69 6e  ite3_value*);.in
13b40 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  t sqlite3_value_
13b50 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 73 71 6c  numeric_type(sql
13b60 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 0a 2f  ite3_value*);../
13b70 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f  *.** CAPI3REF: O
13b80 62 74 61 69 6e 20 41 67 67 72 65 67 61 74 65 20  btain Aggregate 
13b90 46 75 6e 63 74 69 6f 6e 20 43 6f 6e 74 65 78 74  Function Context
13ba0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65  .**.** The imple
13bb0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 67 67  mentation of agg
13bc0 72 65 67 61 74 65 20 53 51 4c 20 66 75 6e 63 74  regate SQL funct
13bd0 69 6f 6e 73 20 75 73 65 20 74 68 69 73 20 72 6f  ions use this ro
13be0 75 74 69 6e 65 20 74 6f 20 61 6c 6c 6f 63 61 74  utine to allocat
13bf0 65 0a 2a 2a 20 61 20 73 74 72 75 63 74 75 72 65  e.** a structure
13c00 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 65   for storing the
13c10 69 72 20 73 74 61 74 65 2e 20 20 54 68 65 20 66  ir state.  The f
13c20 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 72  irst time this r
13c30 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 63 61 6c  outine.** is cal
13c40 6c 65 64 20 66 6f 72 20 61 20 70 61 72 74 69 63  led for a partic
13c50 75 6c 61 72 20 61 67 67 72 65 67 61 74 65 2c 20  ular aggregate, 
13c60 61 20 6e 65 77 20 73 74 72 75 63 74 75 72 65 20  a new structure 
13c70 6f 66 20 73 69 7a 65 20 6e 42 79 74 65 73 0a 2a  of size nBytes.*
13c80 2a 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 2c 20  * is allocated, 
13c90 7a 65 72 6f 65 64 2c 20 61 6e 64 20 72 65 74 75  zeroed, and retu
13ca0 72 6e 65 64 2e 20 20 4f 6e 20 73 75 62 73 65 71  rned.  On subseq
13cb0 75 65 6e 74 20 63 61 6c 6c 73 20 28 66 6f 72 20  uent calls (for 
13cc0 74 68 65 0a 2a 2a 20 73 61 6d 65 20 61 67 67 72  the.** same aggr
13cd0 65 67 61 74 65 20 69 6e 73 74 61 6e 63 65 29 20  egate instance) 
13ce0 74 68 65 20 73 61 6d 65 20 62 75 66 66 65 72 20  the same buffer 
13cf0 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68  is returned.  Th
13d00 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
13d10 0a 2a 2a 20 6f 66 20 74 68 65 20 61 67 67 72 65  .** of the aggre
13d20 67 61 74 65 20 63 61 6e 20 75 73 65 20 74 68 65  gate can use the
13d30 20 72 65 74 75 72 6e 65 64 20 62 75 66 66 65 72   returned buffer
13d40 20 74 6f 20 61 63 63 75 6d 75 6c 61 74 65 20 64   to accumulate d
13d50 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62  ata..**.** The b
13d60 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 20  uffer allocated 
13d70 69 73 20 66 72 65 65 64 20 61 75 74 6f 6d 61 74  is freed automat
13d80 69 63 61 6c 6c 79 20 62 79 20 53 51 4c 69 74 65  ically by SQLite
13d90 20 77 68 61 6e 20 74 68 65 20 61 67 67 72 65 67   whan the aggreg
13da0 61 74 65 0a 2a 2a 20 71 75 65 72 79 20 63 6f 6e  ate.** query con
13db0 63 6c 75 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  cludes..**.** Th
13dc0 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  e first paramete
13dd0 72 20 73 68 6f 75 6c 64 20 62 65 20 61 20 63 6f  r should be a co
13de0 70 79 20 6f 66 20 74 68 65 20 0a 2a 2a 20 5b 73  py of the .** [s
13df0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 7c  qlite3_context |
13e00 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 63 6f   SQL function co
13e10 6e 74 65 78 74 5d 20 74 68 61 74 20 69 73 20 74  ntext] that is t
13e20 68 65 20 66 69 72 73 74 0a 2a 2a 20 70 61 72 61  he first.** para
13e30 6d 65 74 65 72 20 74 6f 20 74 68 65 20 63 61 6c  meter to the cal
13e40 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 20 74 68  lback routine th
13e50 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68  at implements th
13e60 65 20 61 67 67 72 65 67 61 74 65 0a 2a 2a 20 66  e aggregate.** f
13e70 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 76 6f 69 64  unction..*/.void
13e80 20 2a 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67   *sqlite3_aggreg
13e90 61 74 65 5f 63 6f 6e 74 65 78 74 28 73 71 6c 69  ate_context(sqli
13ea0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e  te3_context*, in
13eb0 74 20 6e 42 79 74 65 73 29 3b 0a 0a 2f 2a 0a 2a  t nBytes);../*.*
13ec0 2a 20 43 41 50 49 33 52 45 46 3a 20 55 73 65 72  * CAPI3REF: User
13ed0 20 44 61 74 61 20 46 6f 72 20 46 75 6e 63 74 69   Data For Functi
13ee0 6f 6e 73 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 55  ons.**.** The pU
13ef0 73 65 72 44 61 74 61 20 70 61 72 61 6d 65 74 65  serData paramete
13f00 72 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65  r to the [sqlite
13f10 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
13f20 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c  n()].** and [sql
13f30 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
13f40 74 69 6f 6e 31 36 28 29 5d 20 72 6f 75 74 69 6e  tion16()] routin
13f50 65 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 72 65  es.** used to re
13f60 67 69 73 74 65 72 20 75 73 65 72 20 66 75 6e 63  gister user func
13f70 74 69 6f 6e 73 20 69 73 20 61 76 61 69 6c 61 62  tions is availab
13f80 6c 65 20 74 6f 0a 2a 2a 20 74 68 65 20 69 6d 70  le to.** the imp
13f90 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
13fa0 68 65 20 66 75 6e 63 74 69 6f 6e 20 75 73 69 6e  he function usin
13fb0 67 20 74 68 69 73 20 63 61 6c 6c 2e 0a 2a 2f 0a  g this call..*/.
13fc0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 75 73  void *sqlite3_us
13fd0 65 72 5f 64 61 74 61 28 73 71 6c 69 74 65 33 5f  er_data(sqlite3_
13fe0 63 6f 6e 74 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a  context*);../*.*
13ff0 2a 20 43 41 50 49 33 52 45 46 3a 20 46 75 6e 63  * CAPI3REF: Func
14000 74 69 6f 6e 20 41 75 78 69 6c 69 61 72 79 20 44  tion Auxiliary D
14010 61 74 61 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f  ata.**.** The fo
14020 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 66 75 6e 63  llowing two func
14030 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 75 73 65  tions may be use
14040 64 20 62 79 20 73 63 61 6c 61 72 20 53 51 4c 20  d by scalar SQL 
14050 66 75 6e 63 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20  functions to.** 
14060 61 73 73 6f 63 69 61 74 65 20 6d 65 74 61 2d 64  associate meta-d
14070 61 74 61 20 77 69 74 68 20 61 72 67 75 6d 65 6e  ata with argumen
14080 74 20 76 61 6c 75 65 73 2e 20 49 66 20 74 68 65  t values. If the
14090 20 73 61 6d 65 20 76 61 6c 75 65 20 69 73 20 70   same value is p
140a0 61 73 73 65 64 20 74 6f 0a 2a 2a 20 6d 75 6c 74  assed to.** mult
140b0 69 70 6c 65 20 69 6e 76 6f 63 61 74 69 6f 6e 73  iple invocations
140c0 20 6f 66 20 74 68 65 20 73 61 6d 65 20 53 51 4c   of the same SQL
140d0 20 66 75 6e 63 74 69 6f 6e 20 64 75 72 69 6e 67   function during
140e0 20 71 75 65 72 79 20 65 78 65 63 75 74 69 6f 6e   query execution
140f0 2c 20 75 6e 64 65 72 0a 2a 2a 20 73 6f 6d 65 20  , under.** some 
14100 63 69 72 63 75 6d 73 74 61 6e 63 65 73 20 74 68  circumstances th
14110 65 20 61 73 73 6f 63 69 61 74 65 64 20 6d 65 74  e associated met
14120 61 2d 64 61 74 61 20 6d 61 79 20 62 65 20 70 72  a-data may be pr
14130 65 73 65 72 76 65 64 2e 20 54 68 69 73 20 6d 61  eserved. This ma
14140 79 0a 2a 2a 20 62 65 20 75 73 65 64 2c 20 66 6f  y.** be used, fo
14150 72 20 65 78 61 6d 70 6c 65 2c 20 74 6f 20 61 64  r example, to ad
14160 64 20 61 20 72 65 67 75 6c 61 72 2d 65 78 70 72  d a regular-expr
14170 65 73 73 69 6f 6e 20 6d 61 74 63 68 69 6e 67 20  ession matching 
14180 73 63 61 6c 61 72 0a 2a 2a 20 66 75 6e 63 74 69  scalar.** functi
14190 6f 6e 2e 20 54 68 65 20 63 6f 6d 70 69 6c 65 64  on. The compiled
141a0 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20   version of the 
141b0 72 65 67 75 6c 61 72 20 65 78 70 72 65 73 73 69  regular expressi
141c0 6f 6e 20 69 73 20 73 74 6f 72 65 64 20 61 73 0a  on is stored as.
141d0 2a 2a 20 6d 65 74 61 2d 64 61 74 61 20 61 73 73  ** meta-data ass
141e0 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
141f0 20 53 51 4c 20 76 61 6c 75 65 20 70 61 73 73 65   SQL value passe
14200 64 20 61 73 20 74 68 65 20 72 65 67 75 6c 61 72  d as the regular
14210 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 70   expression.** p
14220 61 74 74 65 72 6e 2e 20 20 54 68 65 20 63 6f 6d  attern.  The com
14230 70 69 6c 65 64 20 72 65 67 75 6c 61 72 20 65 78  piled regular ex
14240 70 72 65 73 73 69 6f 6e 20 63 61 6e 20 62 65 20  pression can be 
14250 72 65 75 73 65 64 20 6f 6e 20 6d 75 6c 74 69 70  reused on multip
14260 6c 65 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e  le.** invocation
14270 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 66 75  s of the same fu
14280 6e 63 74 69 6f 6e 20 73 6f 20 74 68 61 74 20 74  nction so that t
14290 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 74 74  he original patt
142a0 65 72 6e 20 73 74 72 69 6e 67 0a 2a 2a 20 64 6f  ern string.** do
142b0 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62  es not need to b
142c0 65 20 72 65 63 6f 6d 70 69 6c 65 64 20 6f 6e 20  e recompiled on 
142d0 65 61 63 68 20 69 6e 76 6f 63 61 74 69 6f 6e 2e  each invocation.
142e0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
142f0 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 29  e3_get_auxdata()
14300 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72   interface retur
14310 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ns a pointer to 
14320 74 68 65 20 6d 65 74 61 2d 64 61 74 61 0a 2a 2a  the meta-data.**
14330 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
14340 20 74 68 65 20 4e 74 68 20 61 72 67 75 6d 65 6e   the Nth argumen
14350 74 20 76 61 6c 75 65 20 74 6f 20 74 68 65 20 63  t value to the c
14360 75 72 72 65 6e 74 20 53 51 4c 20 66 75 6e 63 74  urrent SQL funct
14370 69 6f 6e 0a 2a 2a 20 63 61 6c 6c 2c 20 77 68 65  ion.** call, whe
14380 72 65 20 4e 20 69 73 20 74 68 65 20 73 65 63 6f  re N is the seco
14390 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20 49 66  nd parameter. If
143a0 20 6e 6f 20 6d 65 74 61 2d 64 61 74 61 20 68 61   no meta-data ha
143b0 73 20 62 65 65 6e 20 73 65 74 20 66 6f 72 0a 2a  s been set for.*
143c0 2a 20 74 68 61 74 20 76 61 6c 75 65 2c 20 74 68  * that value, th
143d0 65 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  en a NULL pointe
143e0 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  r is returned..*
143f0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
14400 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 69  _set_auxdata() i
14410 73 20 75 73 65 64 20 74 6f 20 61 73 73 6f 63 69  s used to associ
14420 61 74 65 20 6d 65 74 61 2d 64 61 74 61 20 77 69  ate meta-data wi
14430 74 68 20 61 6e 20 53 51 4c 0a 2a 2a 20 66 75 6e  th an SQL.** fun
14440 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 2e 20  ction argument. 
14450 54 68 65 20 74 68 69 72 64 20 70 61 72 61 6d 65  The third parame
14460 74 65 72 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ter is a pointer
14470 20 74 6f 20 74 68 65 20 6d 65 74 61 2d 64 61 74   to the meta-dat
14480 61 0a 2a 2a 20 74 6f 20 62 65 20 61 73 73 6f 63  a.** to be assoc
14490 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 4e  iated with the N
144a0 74 68 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e  th user function
144b0 20 61 72 67 75 6d 65 6e 74 20 76 61 6c 75 65 2e   argument value.
144c0 20 54 68 65 20 66 6f 75 72 74 68 0a 2a 2a 20 70   The fourth.** p
144d0 61 72 61 6d 65 74 65 72 20 73 70 65 63 69 66 69  arameter specifi
144e0 65 73 20 61 20 64 65 73 74 72 75 63 74 6f 72 20  es a destructor 
144f0 74 68 61 74 20 77 69 6c 6c 20 62 65 20 63 61 6c  that will be cal
14500 6c 65 64 20 6f 6e 20 74 68 65 20 6d 65 74 61 2d  led on the meta-
14510 0a 2a 2a 20 64 61 74 61 20 70 6f 69 6e 74 65 72  .** data pointer
14520 20 74 6f 20 72 65 6c 65 61 73 65 20 69 74 20 77   to release it w
14530 68 65 6e 20 69 74 20 69 73 20 6e 6f 20 6c 6f 6e  hen it is no lon
14540 67 65 72 20 72 65 71 75 69 72 65 64 2e 20 49 66  ger required. If
14550 20 74 68 65 20 0a 2a 2a 20 64 65 73 74 72 75 63   the .** destruc
14560 74 6f 72 20 69 73 20 4e 55 4c 4c 2c 20 69 74 20  tor is NULL, it 
14570 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 2e 0a  is not invoked..
14580 2a 2a 0a 2a 2a 20 49 6e 20 70 72 61 63 74 69 63  **.** In practic
14590 65 2c 20 6d 65 74 61 2d 64 61 74 61 20 69 73 20  e, meta-data is 
145a0 70 72 65 73 65 72 76 65 64 20 62 65 74 77 65 65  preserved betwee
145b0 6e 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73  n function calls
145c0 20 66 6f 72 0a 2a 2a 20 65 78 70 72 65 73 73 69   for.** expressi
145d0 6f 6e 73 20 74 68 61 74 20 61 72 65 20 63 6f 6e  ons that are con
145e0 73 74 61 6e 74 20 61 74 20 63 6f 6d 70 69 6c 65  stant at compile
145f0 20 74 69 6d 65 2e 20 54 68 69 73 20 69 6e 63 6c   time. This incl
14600 75 64 65 73 20 6c 69 74 65 72 61 6c 0a 2a 2a 20  udes literal.** 
14610 76 61 6c 75 65 73 20 61 6e 64 20 53 51 4c 20 76  values and SQL v
14620 61 72 69 61 62 6c 65 73 2e 0a 2a 2f 0a 76 6f 69  ariables..*/.voi
14630 64 20 2a 73 71 6c 69 74 65 33 5f 67 65 74 5f 61  d *sqlite3_get_a
14640 75 78 64 61 74 61 28 73 71 6c 69 74 65 33 5f 63  uxdata(sqlite3_c
14650 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 76  ontext*, int);.v
14660 6f 69 64 20 73 71 6c 69 74 65 33 5f 73 65 74 5f  oid sqlite3_set_
14670 61 75 78 64 61 74 61 28 73 71 6c 69 74 65 33 5f  auxdata(sqlite3_
14680 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 2c 20 76  context*, int, v
14690 6f 69 64 2a 2c 20 76 6f 69 64 20 28 2a 29 28 76  oid*, void (*)(v
146a0 6f 69 64 2a 29 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20  oid*));.../*.** 
146b0 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 73 74 61  CAPI3REF: Consta
146c0 6e 74 73 20 44 65 66 69 6e 69 6e 67 20 53 70 65  nts Defining Spe
146d0 63 69 61 6c 20 44 65 73 74 72 75 63 74 6f 72 20  cial Destructor 
146e0 42 65 68 61 76 69 6f 72 0a 2a 2a 0a 2a 2a 20 54  Behavior.**.** T
146f0 68 65 73 65 20 61 72 65 20 73 70 65 63 69 61 6c  hese are special
14700 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 64   value for the d
14710 65 73 74 72 75 63 74 6f 72 20 74 68 61 74 20 69  estructor that i
14720 73 20 70 61 73 73 65 64 20 69 6e 20 61 73 20 74  s passed in as t
14730 68 65 0a 2a 2a 20 66 69 6e 61 6c 20 61 72 67 75  he.** final argu
14740 6d 65 6e 74 20 74 6f 20 72 6f 75 74 69 6e 65 73  ment to routines
14750 20 6c 69 6b 65 20 5b 73 71 6c 69 74 65 33 5f 72   like [sqlite3_r
14760 65 73 75 6c 74 5f 62 6c 6f 62 28 29 5d 2e 20 20  esult_blob()].  
14770 49 66 20 74 68 65 20 64 65 73 74 72 75 63 74 6f  If the destructo
14780 72 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 73  r.** argument is
14790 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 2c 20   SQLITE_STATIC, 
147a0 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  it means that th
147b0 65 20 63 6f 6e 74 65 6e 74 20 70 6f 69 6e 74 65  e content pointe
147c0 72 20 69 73 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a  r is constant.**
147d0 20 61 6e 64 20 77 69 6c 6c 20 6e 65 76 65 72 20   and will never 
147e0 63 68 61 6e 67 65 2e 20 20 49 74 20 64 6f 65 73  change.  It does
147f0 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20   not need to be 
14800 64 65 73 74 72 6f 79 65 64 2e 20 20 54 68 65 20  destroyed.  The 
14810 0a 2a 2a 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  .** SQLITE_TRANS
14820 49 45 4e 54 20 76 61 6c 75 65 20 6d 65 61 6e 73  IENT value means
14830 20 74 68 61 74 20 74 68 65 20 63 6f 6e 74 65 6e   that the conten
14840 74 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 63 68  t will likely ch
14850 61 6e 67 65 20 69 6e 0a 2a 2a 20 74 68 65 20 6e  ange in.** the n
14860 65 61 72 20 66 75 74 75 72 65 20 61 6e 64 20 74  ear future and t
14870 68 61 74 20 53 51 4c 69 74 65 20 73 68 6f 75 6c  hat SQLite shoul
14880 64 20 6d 61 6b 65 20 69 74 73 20 6f 77 6e 20 70  d make its own p
14890 72 69 76 61 74 65 20 63 6f 70 79 20 6f 66 0a 2a  rivate copy of.*
148a0 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 62 65  * the content be
148b0 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a  fore returning..
148c0 2a 2a 0a 2a 2a 20 54 68 65 20 74 79 70 65 64 65  **.** The typede
148d0 66 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 74  f is necessary t
148e0 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 70 72  o work around pr
148f0 6f 62 6c 65 6d 73 20 69 6e 20 63 65 72 74 61 69  oblems in certai
14900 6e 0a 2a 2a 20 43 2b 2b 20 63 6f 6d 70 69 6c 65  n.** C++ compile
14910 72 73 2e 20 20 53 65 65 20 74 69 63 6b 65 74 20  rs.  See ticket 
14920 23 32 31 39 31 2e 0a 2a 2f 0a 74 79 70 65 64 65  #2191..*/.typede
14930 66 20 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33  f void (*sqlite3
14940 5f 64 65 73 74 72 75 63 74 6f 72 5f 74 79 70 65  _destructor_type
14950 29 28 76 6f 69 64 2a 29 3b 0a 23 64 65 66 69 6e  )(void*);.#defin
14960 65 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 20  e SQLITE_STATIC 
14970 20 20 20 20 20 28 28 73 71 6c 69 74 65 33 5f 64       ((sqlite3_d
14980 65 73 74 72 75 63 74 6f 72 5f 74 79 70 65 29 30  estructor_type)0
14990 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ).#define SQLITE
149a0 5f 54 52 41 4e 53 49 45 4e 54 20 20 20 28 28 73  _TRANSIENT   ((s
149b0 71 6c 69 74 65 33 5f 64 65 73 74 72 75 63 74 6f  qlite3_destructo
149c0 72 5f 74 79 70 65 29 2d 31 29 0a 0a 2f 2a 0a 2a  r_type)-1)../*.*
149d0 2a 20 43 41 50 49 33 52 45 46 3a 20 53 65 74 74  * CAPI3REF: Sett
149e0 69 6e 67 20 54 68 65 20 52 65 73 75 6c 74 20 4f  ing The Result O
149f0 66 20 41 6e 20 53 51 4c 20 46 75 6e 63 74 69 6f  f An SQL Functio
14a00 6e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f  n.**.** These ro
14a10 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 20  utines are used 
14a20 62 79 20 74 68 65 20 78 46 75 6e 63 20 6f 72 20  by the xFunc or 
14a30 78 46 69 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 73  xFinal callbacks
14a40 20 74 68 61 74 0a 2a 2a 20 69 6d 70 6c 65 6d 65   that.** impleme
14a50 6e 74 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73  nt SQL functions
14a60 20 61 6e 64 20 61 67 67 72 65 67 61 74 65 73 2e   and aggregates.
14a70 20 20 53 65 65 0a 2a 2a 20 5b 73 71 6c 69 74 65    See.** [sqlite
14a80 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
14a90 6e 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  n()] and [sqlite
14aa0 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
14ab0 6e 31 36 28 29 5d 0a 2a 2a 20 66 6f 72 20 61 64  n16()].** for ad
14ac0 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61  ditional informa
14ad0 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73  tion..**.** Thes
14ae0 65 20 66 75 6e 63 74 69 6f 6e 73 20 77 6f 72 6b  e functions work
14af0 20 76 65 72 79 20 6d 75 63 68 20 6c 69 6b 65 20   very much like 
14b00 74 68 65 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  the .** [sqlite3
14b10 5f 62 69 6e 64 5f 62 6c 6f 62 20 7c 20 73 71 6c  _bind_blob | sql
14b20 69 74 65 33 5f 62 69 6e 64 5f 2a 5d 20 66 61 6d  ite3_bind_*] fam
14b30 69 6c 79 20 6f 66 20 66 75 6e 63 74 69 6f 6e 73  ily of functions
14b40 20 75 73 65 64 0a 2a 2a 20 74 6f 20 62 69 6e 64   used.** to bind
14b50 20 76 61 6c 75 65 73 20 74 6f 20 68 6f 73 74 20   values to host 
14b60 70 61 72 61 6d 65 74 65 72 73 20 69 6e 20 70 72  parameters in pr
14b70 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
14b80 73 2e 0a 2a 2a 20 52 65 66 65 72 20 74 6f 20 74  s..** Refer to t
14b90 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62  he.** [sqlite3_b
14ba0 69 6e 64 5f 62 6c 6f 62 20 7c 20 73 71 6c 69 74  ind_blob | sqlit
14bb0 65 33 5f 62 69 6e 64 5f 2a 20 64 6f 63 75 6d 65  e3_bind_* docume
14bc0 6e 74 61 74 69 6f 6e 5d 20 66 6f 72 0a 2a 2a 20  ntation] for.** 
14bd0 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  additional infor
14be0 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  mation..**.** Th
14bf0 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
14c00 5f 65 72 72 6f 72 28 29 20 61 6e 64 20 73 71 6c  _error() and sql
14c10 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
14c20 72 31 36 28 29 20 66 75 6e 63 74 69 6f 6e 73 0a  r16() functions.
14c30 2a 2a 20 63 61 75 73 65 20 74 68 65 20 69 6d 70  ** cause the imp
14c40 6c 65 6d 65 6e 74 65 64 20 53 51 4c 20 66 75 6e  lemented SQL fun
14c50 63 74 69 6f 6e 20 74 6f 20 74 68 72 6f 77 20 61  ction to throw a
14c60 6e 20 65 78 63 65 70 74 69 6f 6e 2e 20 20 54 68  n exception.  Th
14c70 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 74  e.** parameter t
14c80 6f 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  o sqlite3_result
14c90 5f 65 72 72 6f 72 28 29 20 6f 72 20 73 71 6c 69  _error() or sqli
14ca0 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
14cb0 31 36 28 29 0a 2a 2a 20 69 73 20 74 68 65 20 74  16().** is the t
14cc0 65 78 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 20  ext of an error 
14cd0 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54  message..**.** T
14ce0 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  he sqlite3_resul
14cf0 74 5f 74 6f 6f 62 69 67 28 29 20 63 61 75 73 65  t_toobig() cause
14d00 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 6d   the function im
14d10 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20  plementation.** 
14d20 74 6f 20 74 68 72 6f 77 20 61 6e 64 20 65 72 72  to throw and err
14d30 6f 72 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68  or indicating th
14d40 61 74 20 61 20 73 74 72 69 6e 67 20 6f 72 20 42  at a string or B
14d50 4c 4f 42 20 69 73 20 74 6f 20 6c 6f 6e 67 0a 2a  LOB is to long.*
14d60 2a 20 74 6f 20 72 65 70 72 65 73 65 6e 74 2e 0a  * to represent..
14d70 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  */.void sqlite3_
14d80 72 65 73 75 6c 74 5f 62 6c 6f 62 28 73 71 6c 69  result_blob(sqli
14d90 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f  te3_context*, co
14da0 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20  nst void*, int, 
14db0 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b  void(*)(void*));
14dc0 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65  .void sqlite3_re
14dd0 73 75 6c 74 5f 64 6f 75 62 6c 65 28 73 71 6c 69  sult_double(sqli
14de0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 64 6f  te3_context*, do
14df0 75 62 6c 65 29 3b 0a 76 6f 69 64 20 73 71 6c 69  uble);.void sqli
14e00 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
14e10 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
14e20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  *, const char*, 
14e30 69 6e 74 29 3b 0a 76 6f 69 64 20 73 71 6c 69 74  int);.void sqlit
14e40 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31  e3_result_error1
14e50 36 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  6(sqlite3_contex
14e60 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  t*, const void*,
14e70 20 69 6e 74 29 3b 0a 76 6f 69 64 20 73 71 6c 69   int);.void sqli
14e80 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
14e90 5f 74 6f 6f 62 69 67 28 73 71 6c 69 74 65 33 5f  _toobig(sqlite3_
14ea0 63 6f 6e 74 65 78 74 2a 29 3b 0a 76 6f 69 64 20  context*);.void 
14eb0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
14ec0 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  nt(sqlite3_conte
14ed0 78 74 2a 2c 20 69 6e 74 29 3b 0a 76 6f 69 64 20  xt*, int);.void 
14ee0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
14ef0 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 63 6f 6e  nt64(sqlite3_con
14f00 74 65 78 74 2a 2c 20 73 71 6c 69 74 65 5f 69 6e  text*, sqlite_in
14f10 74 36 34 29 3b 0a 76 6f 69 64 20 73 71 6c 69 74  t64);.void sqlit
14f20 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 73  e3_result_null(s
14f30 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29  qlite3_context*)
14f40 3b 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  ;.void sqlite3_r
14f50 65 73 75 6c 74 5f 74 65 78 74 28 73 71 6c 69 74  esult_text(sqlit
14f60 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e  e3_context*, con
14f70 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 76  st char*, int, v
14f80 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
14f90 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
14fa0 75 6c 74 5f 74 65 78 74 31 36 28 73 71 6c 69 74  ult_text16(sqlit
14fb0 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e  e3_context*, con
14fc0 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 76  st void*, int, v
14fd0 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
14fe0 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
14ff0 75 6c 74 5f 74 65 78 74 31 36 6c 65 28 73 71 6c  ult_text16le(sql
15000 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63  ite3_context*, c
15010 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c  onst void*, int,
15020 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b  void(*)(void*));
15030 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65  .void sqlite3_re
15040 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28 73 71  sult_text16be(sq
15050 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20  lite3_context*, 
15060 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74  const void*, int
15070 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29  ,void(*)(void*))
15080 3b 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  ;.void sqlite3_r
15090 65 73 75 6c 74 5f 76 61 6c 75 65 28 73 71 6c 69  esult_value(sqli
150a0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 73 71  te3_context*, sq
150b0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 76  lite3_value*);.v
150c0 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
150d0 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69  lt_zeroblob(sqli
150e0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e  te3_context*, in
150f0 74 20 6e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  t n);../*.** CAP
15100 49 33 52 45 46 3a 20 44 65 66 69 6e 65 20 4e 65  I3REF: Define Ne
15110 77 20 43 6f 6c 6c 61 74 69 6e 67 20 53 65 71 75  w Collating Sequ
15120 65 6e 63 65 73 0a 2a 2a 0a 2a 2a 20 54 68 65 73  ences.**.** Thes
15130 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20  e functions are 
15140 75 73 65 64 20 74 6f 20 61 64 64 20 6e 65 77 20  used to add new 
15150 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
15160 63 65 73 20 74 6f 20 74 68 65 0a 2a 2a 20 5b 73  ces to the.** [s
15170 71 6c 69 74 65 33 2a 5d 20 68 61 6e 64 6c 65 20  qlite3*] handle 
15180 73 70 65 63 69 66 69 65 64 20 61 73 20 74 68 65  specified as the
15190 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e   first argument.
151a0 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65   .**.** The name
151b0 20 6f 66 20 74 68 65 20 6e 65 77 20 63 6f 6c 6c   of the new coll
151c0 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 69  ation sequence i
151d0 73 20 73 70 65 63 69 66 69 65 64 20 61 73 20 61  s specified as a
151e0 20 55 54 46 2d 38 20 73 74 72 69 6e 67 0a 2a 2a   UTF-8 string.**
151f0 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65   for sqlite3_cre
15200 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 20  ate_collation() 
15210 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 72 65 61  and sqlite3_crea
15220 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28  te_collation_v2(
15230 29 0a 2a 2a 20 61 6e 64 20 61 20 55 54 46 2d 31  ).** and a UTF-1
15240 36 20 73 74 72 69 6e 67 20 66 6f 72 20 73 71 6c  6 string for sql
15250 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c  ite3_create_coll
15260 61 74 69 6f 6e 31 36 28 29 2e 20 20 49 6e 20 61  ation16().  In a
15270 6c 6c 20 63 61 73 65 73 0a 2a 2a 20 74 68 65 20  ll cases.** the 
15280 6e 61 6d 65 20 69 73 20 70 61 73 73 65 64 20 61  name is passed a
15290 73 20 74 68 65 20 73 65 63 6f 6e 64 20 66 75 6e  s the second fun
152a0 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 2e 0a  ction argument..
152b0 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20  **.** The third 
152c0 61 72 67 75 6d 65 6e 74 20 6d 75 73 74 20 62 65  argument must be
152d0 20 6f 6e 65 20 6f 66 20 74 68 65 20 63 6f 6e 73   one of the cons
152e0 74 61 6e 74 73 20 5b 53 51 4c 49 54 45 5f 55 54  tants [SQLITE_UT
152f0 46 38 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  F8],.** [SQLITE_
15300 55 54 46 31 36 4c 45 5d 20 6f 72 20 5b 53 51 4c  UTF16LE] or [SQL
15310 49 54 45 5f 55 54 46 31 36 42 45 5d 2c 20 69 6e  ITE_UTF16BE], in
15320 64 69 63 61 74 69 6e 67 20 74 68 61 74 20 74 68  dicating that th
15330 65 20 75 73 65 72 2d 73 75 70 70 6c 69 65 64 0a  e user-supplied.
15340 2a 2a 20 72 6f 75 74 69 6e 65 20 65 78 70 65 63  ** routine expec
15350 74 73 20 74 6f 20 62 65 20 70 61 73 73 65 64 20  ts to be passed 
15360 70 6f 69 6e 74 65 72 73 20 74 6f 20 73 74 72 69  pointers to stri
15370 6e 67 73 20 65 6e 63 6f 64 65 64 20 75 73 69 6e  ngs encoded usin
15380 67 20 55 54 46 2d 38 2c 0a 2a 2a 20 55 54 46 2d  g UTF-8,.** UTF-
15390 31 36 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e  16 little-endian
153a0 20 6f 72 20 55 54 46 2d 31 36 20 62 69 67 2d 65   or UTF-16 big-e
153b0 6e 64 69 61 6e 20 72 65 73 70 65 63 74 69 76 65  ndian respective
153c0 6c 79 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e  ly..**.** A poin
153d0 74 65 72 20 74 6f 20 74 68 65 20 75 73 65 72 20  ter to the user 
153e0 73 75 70 70 6c 69 65 64 20 72 6f 75 74 69 6e 65  supplied routine
153f0 20 6d 75 73 74 20 62 65 20 70 61 73 73 65 64 20   must be passed 
15400 61 73 20 74 68 65 20 66 69 66 74 68 0a 2a 2a 20  as the fifth.** 
15410 61 72 67 75 6d 65 6e 74 2e 20 49 66 20 69 74 20  argument. If it 
15420 69 73 20 4e 55 4c 4c 2c 20 74 68 69 73 20 69 73  is NULL, this is
15430 20 74 68 65 20 73 61 6d 65 20 61 73 20 64 65 6c   the same as del
15440 65 74 69 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74  eting the collat
15450 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20  ion.** sequence 
15460 28 73 6f 20 74 68 61 74 20 53 51 4c 69 74 65 20  (so that SQLite 
15470 63 61 6e 6e 6f 74 20 63 61 6c 6c 20 69 74 20 61  cannot call it a
15480 6e 79 6d 6f 72 65 29 2e 20 45 61 63 68 20 74 69  nymore). Each ti
15490 6d 65 20 74 68 65 20 75 73 65 72 0a 2a 2a 20 73  me the user.** s
154a0 75 70 70 6c 69 65 64 20 66 75 6e 63 74 69 6f 6e  upplied function
154b0 20 69 73 20 69 6e 76 6f 6b 65 64 2c 20 69 74 20   is invoked, it 
154c0 69 73 20 70 61 73 73 65 64 20 61 20 63 6f 70 79  is passed a copy
154d0 20 6f 66 20 74 68 65 20 76 6f 69 64 2a 20 70 61   of the void* pa
154e0 73 73 65 64 20 61 73 0a 2a 2a 20 74 68 65 20 66  ssed as.** the f
154f0 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 20 74  ourth argument t
15500 6f 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  o sqlite3_create
15510 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 20 6f 72 0a  _collation() or.
15520 2a 2a 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  ** sqlite3_creat
15530 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 29 20  e_collation16() 
15540 61 73 20 69 74 73 20 66 69 72 73 74 20 70 61 72  as its first par
15550 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  ameter..**.** Th
15560 65 20 72 65 6d 61 69 6e 69 6e 67 20 61 72 67 75  e remaining argu
15570 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 75 73 65  ments to the use
15580 72 2d 73 75 70 70 6c 69 65 64 20 72 6f 75 74 69  r-supplied routi
15590 6e 65 20 61 72 65 20 74 77 6f 20 73 74 72 69 6e  ne are two strin
155a0 67 73 2c 0a 2a 2a 20 65 61 63 68 20 72 65 70 72  gs,.** each repr
155b0 65 73 65 6e 74 65 64 20 62 79 20 61 20 5b 6c 65  esented by a [le
155c0 6e 67 74 68 2c 20 64 61 74 61 5d 20 70 61 69 72  ngth, data] pair
155d0 20 61 6e 64 20 65 6e 63 6f 64 65 64 20 69 6e 20   and encoded in 
155e0 74 68 65 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20  the encoding.** 
155f0 74 68 61 74 20 77 61 73 20 70 61 73 73 65 64 20  that was passed 
15600 61 73 20 74 68 65 20 74 68 69 72 64 20 61 72 67  as the third arg
15610 75 6d 65 6e 74 20 77 68 65 6e 20 74 68 65 20 63  ument when the c
15620 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
15630 65 20 77 61 73 0a 2a 2a 20 72 65 67 69 73 74 65  e was.** registe
15640 72 65 64 2e 20 54 68 65 20 75 73 65 72 20 72 6f  red. The user ro
15650 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 72 65 74  utine should ret
15660 75 72 6e 20 6e 65 67 61 74 69 76 65 2c 20 7a 65  urn negative, ze
15670 72 6f 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69  ro or positive i
15680 66 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 73  f.** the first s
15690 74 72 69 6e 67 20 69 73 20 6c 65 73 73 20 74 68  tring is less th
156a0 61 6e 2c 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72  an, equal to, or
156b0 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68   greater than th
156c0 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 73 74 72 69  e second.** stri
156d0 6e 67 2e 20 69 2e 65 2e 20 28 53 54 52 49 4e 47  ng. i.e. (STRING
156e0 31 20 2d 20 53 54 52 49 4e 47 32 29 2e 0a 2a 2a  1 - STRING2)..**
156f0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
15700 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
15710 5f 76 32 28 29 20 77 6f 72 6b 73 20 6c 69 6b 65  _v2() works like
15720 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
15730 63 6f 6c 6c 61 74 69 6f 6e 28 29 0a 2a 2a 20 65  collation().** e
15740 78 63 61 70 74 20 74 68 61 74 20 69 74 20 74 61  xcapt that it ta
15750 6b 65 73 20 61 6e 20 65 78 74 72 61 20 61 72 67  kes an extra arg
15760 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20 61  ument which is a
15770 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f 72 0a   destructor for.
15780 2a 2a 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  ** the collation
15790 2e 20 20 54 68 65 20 64 65 73 74 72 75 63 74 6f  .  The destructo
157a0 72 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e  r is called when
157b0 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 69   the collation i
157c0 73 0a 2a 2a 20 64 65 73 74 72 6f 79 65 64 20 61  s.** destroyed a
157d0 6e 64 20 69 73 20 70 61 73 73 65 64 20 61 20 63  nd is passed a c
157e0 6f 70 79 20 6f 66 20 74 68 65 20 66 6f 75 72 74  opy of the fourt
157f0 68 20 70 61 72 61 6d 65 74 65 72 20 76 6f 69 64  h parameter void
15800 2a 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 6f 66 20  * pointer.** of 
15810 74 68 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61  the sqlite3_crea
15820 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28  te_collation_v2(
15830 29 2e 20 20 43 6f 6c 6c 61 74 69 6f 6e 73 20 61  ).  Collations a
15840 72 65 20 64 65 73 74 72 6f 79 65 64 20 77 68 65  re destroyed whe
15850 6e 0a 2a 2a 20 74 68 65 79 20 61 72 65 20 6f 76  n.** they are ov
15860 65 72 72 69 64 64 65 6e 20 62 79 20 6c 61 74 65  erridden by late
15870 72 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 20 63  r calls to the c
15880 6f 6c 6c 61 74 69 6f 6e 20 63 72 65 61 74 69 6f  ollation creatio
15890 6e 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 6f  n functions.** o
158a0 72 20 77 68 65 6e 20 74 68 65 20 5b 73 71 6c 69  r when the [sqli
158b0 74 65 33 2a 5d 20 64 61 74 61 62 61 73 65 20 68  te3*] database h
158c0 61 6e 64 6c 65 20 69 73 20 63 6c 6f 73 65 64 20  andle is closed 
158d0 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 63  using [sqlite3_c
158e0 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54  lose()]..**.** T
158f0 68 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  he sqlite3_creat
15900 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29  e_collation_v2()
15910 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 65 78   interface is ex
15920 70 65 72 69 6d 65 6e 74 61 6c 20 61 6e 64 0a 2a  perimental and.*
15930 2a 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61  * subject to cha
15940 6e 67 65 20 69 6e 20 66 75 74 75 72 65 20 72 65  nge in future re
15950 6c 65 61 73 65 73 2e 20 20 54 68 65 20 6f 74 68  leases.  The oth
15960 65 72 20 63 6f 6c 6c 61 74 69 6f 6e 20 63 72 65  er collation cre
15970 61 74 69 6f 6e 0a 2a 2a 20 66 75 6e 63 74 69 6f  ation.** functio
15980 6e 73 20 61 72 65 20 73 74 61 62 6c 65 2e 0a 2a  ns are stable..*
15990 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72  /.int sqlite3_cr
159a0 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 0a  eate_collation(.
159b0 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63    sqlite3*, .  c
159c0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
159d0 2c 20 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65  , .  int eTextRe
159e0 70 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69  p, .  void*,.  i
159f0 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f  nt(*xCompare)(vo
15a00 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
15a10 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
15a20 69 64 2a 29 0a 29 3b 0a 69 6e 74 20 73 71 6c 69  id*).);.int sqli
15a30 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
15a40 74 69 6f 6e 5f 76 32 28 0a 20 20 73 71 6c 69 74  tion_v2(.  sqlit
15a50 65 33 2a 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68  e3*, .  const ch
15a60 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20 69 6e  ar *zName, .  in
15a70 74 20 65 54 65 78 74 52 65 70 2c 20 0a 20 20 76  t eTextRep, .  v
15a80 6f 69 64 2a 2c 0a 20 20 69 6e 74 28 2a 78 43 6f  oid*,.  int(*xCo
15a90 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74  mpare)(void*,int
15aa0 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74  ,const void*,int
15ab0 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 2c 0a 20  ,const void*),. 
15ac0 20 76 6f 69 64 28 2a 78 44 65 73 74 72 6f 79 29   void(*xDestroy)
15ad0 28 76 6f 69 64 2a 29 0a 29 3b 0a 69 6e 74 20 73  (void*).);.int s
15ae0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
15af0 6c 6c 61 74 69 6f 6e 31 36 28 0a 20 20 73 71 6c  llation16(.  sql
15b00 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e 73 74 20  ite3*, .  const 
15b10 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20  char *zName, .  
15b20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 20 0a 20  int eTextRep, . 
15b30 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28 2a 78   void*,.  int(*x
15b40 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c 69  Compare)(void*,i
15b50 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69  nt,const void*,i
15b60 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 0a  nt,const void*).
15b70 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
15b80 45 46 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20 4e 65  EF: Collation Ne
15b90 65 64 65 64 20 43 61 6c 6c 62 61 63 6b 73 0a 2a  eded Callbacks.*
15ba0 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 68 61  *.** To avoid ha
15bb0 76 69 6e 67 20 74 6f 20 72 65 67 69 73 74 65 72  ving to register
15bc0 20 61 6c 6c 20 63 6f 6c 6c 61 74 69 6f 6e 20 73   all collation s
15bd0 65 71 75 65 6e 63 65 73 20 62 65 66 6f 72 65 20  equences before 
15be0 61 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 61  a database.** ca
15bf0 6e 20 62 65 20 75 73 65 64 2c 20 61 20 73 69 6e  n be used, a sin
15c00 67 6c 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e  gle callback fun
15c10 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 72 65 67  ction may be reg
15c20 69 73 74 65 72 65 64 20 77 69 74 68 20 74 68 65  istered with the
15c30 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 68 61 6e  .** database han
15c40 64 6c 65 20 74 6f 20 62 65 20 63 61 6c 6c 65 64  dle to be called
15c50 20 77 68 65 6e 65 76 65 72 20 61 6e 20 75 6e 64   whenever an und
15c60 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e  efined collation
15c70 20 73 65 71 75 65 6e 63 65 20 69 73 0a 2a 2a 20   sequence is.** 
15c80 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20  required..**.** 
15c90 49 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  If the function 
15ca0 69 73 20 72 65 67 69 73 74 65 72 65 64 20 75 73  is registered us
15cb0 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f  ing the sqlite3_
15cc0 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
15cd0 28 29 20 41 50 49 2c 0a 2a 2a 20 74 68 65 6e 20  () API,.** then 
15ce0 69 74 20 69 73 20 70 61 73 73 65 64 20 74 68 65  it is passed the
15cf0 20 6e 61 6d 65 73 20 6f 66 20 75 6e 64 65 66 69   names of undefi
15d00 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  ned collation se
15d10 71 75 65 6e 63 65 73 20 61 73 20 73 74 72 69 6e  quences as strin
15d20 67 73 0a 2a 2a 20 65 6e 63 6f 64 65 64 20 69 6e  gs.** encoded in
15d30 20 55 54 46 2d 38 2e 20 49 66 20 73 71 6c 69 74   UTF-8. If sqlit
15d40 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65  e3_collation_nee
15d50 64 65 64 31 36 28 29 20 69 73 20 75 73 65 64 2c  ded16() is used,
15d60 20 74 68 65 20 6e 61 6d 65 73 0a 2a 2a 20 61 72   the names.** ar
15d70 65 20 70 61 73 73 65 64 20 61 73 20 55 54 46 2d  e passed as UTF-
15d80 31 36 20 69 6e 20 6d 61 63 68 69 6e 65 20 6e 61  16 in machine na
15d90 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 2e  tive byte order.
15da0 20 41 20 63 61 6c 6c 20 74 6f 20 65 69 74 68 65   A call to eithe
15db0 72 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65  r.** function re
15dc0 70 6c 61 63 65 73 20 61 6e 79 20 65 78 69 73 74  places any exist
15dd0 69 6e 67 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a  ing callback..**
15de0 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63 61 6c  .** When the cal
15df0 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64  lback is invoked
15e00 2c 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  , the first argu
15e10 6d 65 6e 74 20 70 61 73 73 65 64 20 69 73 20 61  ment passed is a
15e20 20 63 6f 70 79 0a 2a 2a 20 6f 66 20 74 68 65 20   copy.** of the 
15e30 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
15e40 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61  to sqlite3_colla
15e50 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 20 6f 72  tion_needed() or
15e60 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c  .** sqlite3_coll
15e70 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28 29  ation_needed16()
15e80 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67  . The second arg
15e90 75 6d 65 6e 74 20 69 73 20 74 68 65 20 64 61 74  ument is the dat
15ea0 61 62 61 73 65 0a 2a 2a 20 68 61 6e 64 6c 65 2e  abase.** handle.
15eb0 20 54 68 65 20 74 68 69 72 64 20 61 72 67 75 6d   The third argum
15ec0 65 6e 74 20 69 73 20 6f 6e 65 20 6f 66 20 5b 53  ent is one of [S
15ed0 51 4c 49 54 45 5f 55 54 46 38 5d 2c 20 5b 53 51  QLITE_UTF8], [SQ
15ee0 4c 49 54 45 5f 55 54 46 31 36 42 45 5d 2c 20 6f  LITE_UTF16BE], o
15ef0 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 55 54 46  r.** [SQLITE_UTF
15f00 31 36 4c 45 5d 2c 20 69 6e 64 69 63 61 74 69 6e  16LE], indicatin
15f10 67 20 74 68 65 20 6d 6f 73 74 20 64 65 73 69 72  g the most desir
15f20 61 62 6c 65 20 66 6f 72 6d 20 6f 66 20 74 68 65  able form of the
15f30 20 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65   collation.** se
15f40 71 75 65 6e 63 65 20 66 75 6e 63 74 69 6f 6e 20  quence function 
15f50 72 65 71 75 69 72 65 64 2e 20 54 68 65 20 66 6f  required. The fo
15f60 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 20 69  urth parameter i
15f70 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  s the name of th
15f80 65 0a 2a 2a 20 72 65 71 75 69 72 65 64 20 63 6f  e.** required co
15f90 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
15fa0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c  ..**.** The call
15fb0 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 73 68  back function sh
15fc0 6f 75 6c 64 20 72 65 67 69 73 74 65 72 20 74 68  ould register th
15fd0 65 20 64 65 73 69 72 65 64 20 63 6f 6c 6c 61 74  e desired collat
15fe0 69 6f 6e 20 75 73 69 6e 67 0a 2a 2a 20 5b 73 71  ion using.** [sq
15ff0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
16000 6c 61 74 69 6f 6e 28 29 5d 2c 20 5b 73 71 6c 69  lation()], [sqli
16010 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
16020 74 69 6f 6e 31 36 28 29 5d 2c 20 6f 72 0a 2a 2a  tion16()], or.**
16030 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65   [sqlite3_create
16040 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 5d  _collation_v2()]
16050 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33  ..*/.int sqlite3
16060 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65  _collation_neede
16070 64 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a  d(.  sqlite3*, .
16080 20 20 76 6f 69 64 2a 2c 20 0a 20 20 76 6f 69 64    void*, .  void
16090 28 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65  (*)(void*,sqlite
160a0 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c  3*,int eTextRep,
160b0 63 6f 6e 73 74 20 63 68 61 72 2a 29 0a 29 3b 0a  const char*).);.
160c0 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c  int sqlite3_coll
160d0 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28 0a  ation_needed16(.
160e0 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 76    sqlite3*, .  v
160f0 6f 69 64 2a 2c 0a 20 20 76 6f 69 64 28 2a 29 28  oid*,.  void(*)(
16100 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69  void*,sqlite3*,i
16110 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73  nt eTextRep,cons
16120 74 20 76 6f 69 64 2a 29 0a 29 3b 0a 0a 2f 2a 0a  t void*).);../*.
16130 2a 2a 20 53 70 65 63 69 66 79 20 74 68 65 20 6b  ** Specify the k
16140 65 79 20 66 6f 72 20 61 6e 20 65 6e 63 72 79 70  ey for an encryp
16150 74 65 64 20 64 61 74 61 62 61 73 65 2e 20 20 54  ted database.  T
16160 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75  his routine shou
16170 6c 64 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64 20  ld be.** called 
16180 72 69 67 68 74 20 61 66 74 65 72 20 73 71 6c 69  right after sqli
16190 74 65 33 5f 6f 70 65 6e 28 29 2e 0a 2a 2a 0a 2a  te3_open()..**.*
161a0 2a 20 54 68 65 20 63 6f 64 65 20 74 6f 20 69 6d  * The code to im
161b0 70 6c 65 6d 65 6e 74 20 74 68 69 73 20 41 50 49  plement this API
161c0 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c   is not availabl
161d0 65 20 69 6e 20 74 68 65 20 70 75 62 6c 69 63 20  e in the public 
161e0 72 65 6c 65 61 73 65 0a 2a 2a 20 6f 66 20 53 51  release.** of SQ
161f0 4c 69 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  Lite..*/.SQLITE_
16200 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
16210 6b 65 79 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  key(.  sqlite3 *
16220 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  db,             
16230 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
16240 65 20 74 6f 20 62 65 20 72 65 6b 65 79 65 64 20  e to be rekeyed 
16250 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  */.  const void 
16260 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 20  *pKey, int nKey 
16270 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 2a      /* The key *
16280 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e  /.);../*.** Chan
16290 67 65 20 74 68 65 20 6b 65 79 20 6f 6e 20 61 6e  ge the key on an
162a0 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 2e 20   open database. 
162b0 20 49 66 20 74 68 65 20 63 75 72 72 65 6e 74 20   If the current 
162c0 64 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 0a  database is not.
162d0 2a 2a 20 65 6e 63 72 79 70 74 65 64 2c 20 74 68  ** encrypted, th
162e0 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  is routine will 
162f0 65 6e 63 72 79 70 74 20 69 74 2e 20 20 49 66 20  encrypt it.  If 
16300 70 4e 65 77 3d 3d 30 20 6f 72 20 6e 4e 65 77 3d  pNew==0 or nNew=
16310 3d 30 2c 20 74 68 65 0a 2a 2a 20 64 61 74 61 62  =0, the.** datab
16320 61 73 65 20 69 73 20 64 65 63 72 79 70 74 65 64  ase is decrypted
16330 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65  ..**.** The code
16340 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68   to implement th
16350 69 73 20 41 50 49 20 69 73 20 6e 6f 74 20 61 76  is API is not av
16360 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 70  ailable in the p
16370 75 62 6c 69 63 20 72 65 6c 65 61 73 65 0a 2a 2a  ublic release.**
16380 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 53   of SQLite..*/.S
16390 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
163a0 6c 69 74 65 33 5f 72 65 6b 65 79 28 0a 20 20 73  lite3_rekey(.  s
163b0 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
163c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
163d0 20 44 61 74 61 62 61 73 65 20 74 6f 20 62 65 20   Database to be 
163e0 72 65 6b 65 79 65 64 20 2a 2f 0a 20 20 63 6f 6e  rekeyed */.  con
163f0 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69  st void *pKey, i
16400 6e 74 20 6e 4b 65 79 20 20 20 20 20 2f 2a 20 54  nt nKey     /* T
16410 68 65 20 6e 65 77 20 6b 65 79 20 2a 2f 0a 29 3b  he new key */.);
16420 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
16430 3a 20 20 53 75 73 70 65 6e 64 20 45 78 65 63 75  :  Suspend Execu
16440 74 69 6f 6e 20 46 6f 72 20 41 20 53 68 6f 72 74  tion For A Short
16450 20 54 69 6d 65 0a 2a 2a 0a 2a 2a 20 54 68 69 73   Time.**.** This
16460 20 66 75 6e 63 74 69 6f 6e 20 63 61 75 73 65 73   function causes
16470 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 68 72   the current thr
16480 65 61 64 20 74 6f 20 73 75 73 70 65 6e 64 20 65  ead to suspend e
16490 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 61 20 6e 75  xecution.** a nu
164a0 6d 62 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65 63  mber of millisec
164b0 6f 6e 64 73 20 73 70 65 63 69 66 69 65 64 20 69  onds specified i
164c0 6e 20 69 74 73 20 70 61 72 61 6d 65 74 65 72 2e  n its parameter.
164d0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70  .**.** If the op
164e0 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 64  erating system d
164f0 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 20  oes not support 
16500 73 6c 65 65 70 20 72 65 71 75 65 73 74 73 20 77  sleep requests w
16510 69 74 68 20 0a 2a 2a 20 6d 69 6c 6c 69 73 65 63  ith .** millisec
16520 6f 6e 64 20 74 69 6d 65 20 72 65 73 6f 6c 75 74  ond time resolut
16530 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 74 69  ion, then the ti
16540 6d 65 20 77 69 6c 6c 20 62 65 20 72 6f 75 6e 64  me will be round
16550 65 64 20 75 70 20 74 6f 20 0a 2a 2a 20 74 68 65  ed up to .** the
16560 20 6e 65 61 72 65 73 74 20 73 65 63 6f 6e 64 2e   nearest second.
16570 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d   The number of m
16580 69 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20 73  illiseconds of s
16590 6c 65 65 70 20 61 63 74 75 61 6c 6c 79 20 0a 2a  leep actually .*
165a0 2a 20 72 65 71 75 65 73 74 65 64 20 66 72 6f 6d  * requested from
165b0 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73   the operating s
165c0 79 73 74 65 6d 20 69 73 20 72 65 74 75 72 6e 65  ystem is returne
165d0 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  d..*/.SQLITE_API
165e0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 6c 65   int sqlite3_sle
165f0 65 70 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20  ep(int);../*.** 
16600 43 41 50 49 33 52 45 46 3a 20 20 4e 61 6d 65 20  CAPI3REF:  Name 
16610 4f 66 20 54 68 65 20 46 6f 6c 64 65 72 20 48 6f  Of The Folder Ho
16620 6c 64 69 6e 67 20 54 65 6d 70 6f 72 61 72 79 20  lding Temporary 
16630 46 69 6c 65 73 0a 2a 2a 0a 2a 2a 20 49 66 20 74  Files.**.** If t
16640 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 72 69 61  his global varia
16650 62 6c 65 20 69 73 20 6d 61 64 65 20 74 6f 20 70  ble is made to p
16660 6f 69 6e 74 20 74 6f 20 61 20 73 74 72 69 6e 67  oint to a string
16670 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 74 68 65   which is.** the
16680 20 6e 61 6d 65 20 6f 66 20 61 20 66 6f 6c 64 65   name of a folde
16690 72 20 28 61 2e 6b 61 2e 20 64 69 72 65 63 74 6f  r (a.ka. directo
166a0 72 79 29 2c 20 74 68 65 6e 20 61 6c 6c 20 74 65  ry), then all te
166b0 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 0a 2a 2a  mporary files.**
166c0 20 63 72 65 61 74 65 64 20 62 79 20 53 51 4c 69   created by SQLi
166d0 74 65 20 77 69 6c 6c 20 62 65 20 70 6c 61 63 65  te will be place
166e0 64 20 69 6e 20 74 68 61 74 20 64 69 72 65 63 74  d in that direct
166f0 6f 72 79 2e 20 20 49 66 20 74 68 69 73 20 76 61  ory.  If this va
16700 72 69 61 62 6c 65 0a 2a 2a 20 69 73 20 4e 55 4c  riable.** is NUL
16710 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 6e 20  L pointer, then 
16720 53 51 4c 69 74 65 20 64 6f 65 73 20 61 20 73 65  SQLite does a se
16730 61 72 63 68 20 66 6f 72 20 61 6e 20 61 70 70 72  arch for an appr
16740 6f 70 72 69 61 74 65 20 74 65 6d 70 6f 72 61 72  opriate temporar
16750 79 0a 2a 2a 20 66 69 6c 65 20 64 69 72 65 63 74  y.** file direct
16760 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20  ory..**.** Once 
16770 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d  [sqlite3_open()]
16780 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64   has been called
16790 2c 20 63 68 61 6e 67 69 6e 67 20 74 68 69 73 20  , changing this 
167a0 76 61 72 69 61 62 6c 65 20 77 69 6c 6c 0a 2a 2a  variable will.**
167b0 20 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20   invalidate the 
167c0 63 75 72 72 65 6e 74 20 74 65 6d 70 6f 72 61 72  current temporar
167d0 79 20 64 61 74 61 62 61 73 65 2c 20 69 66 20 61  y database, if a
167e0 6e 79 2e 20 20 47 65 6e 65 72 61 6c 6c 79 20 73  ny.  Generally s
167f0 70 65 61 6b 69 6e 67 2c 0a 2a 2a 20 69 74 20 69  peaking,.** it i
16800 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20 69 6e  s not safe to in
16810 76 6f 6b 65 20 74 68 69 73 20 72 6f 75 74 69 6e  voke this routin
16820 65 20 61 66 74 65 72 20 5b 73 71 6c 69 74 65 33  e after [sqlite3
16830 5f 6f 70 65 6e 28 29 5d 20 68 61 73 0a 2a 2a 20  _open()] has.** 
16840 62 65 65 6e 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a  been called..*/.
16850 65 78 74 65 72 6e 20 63 68 61 72 20 2a 73 71 6c  extern char *sql
16860 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74  ite3_temp_direct
16870 6f 72 79 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  ory;../*.** CAPI
16880 33 52 45 46 3a 20 20 54 65 73 74 20 54 6f 20 53  3REF:  Test To S
16890 65 65 20 49 66 20 54 68 65 20 44 61 74 61 62 73  ee If The Databs
168a0 65 20 49 73 20 49 6e 20 41 75 74 6f 2d 43 6f 6d  e Is In Auto-Com
168b0 6d 69 74 20 4d 6f 64 65 0a 2a 2a 0a 2a 2a 20 54  mit Mode.**.** T
168c0 65 73 74 20 74 6f 20 73 65 65 20 77 68 65 74 68  est to see wheth
168d0 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 64 61  er or not the da
168e0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
168f0 6e 20 69 73 20 69 6e 20 61 75 74 6f 63 6f 6d 6d  n is in autocomm
16900 69 74 0a 2a 2a 20 6d 6f 64 65 2e 20 20 52 65 74  it.** mode.  Ret
16910 75 72 6e 20 54 52 55 45 20 69 66 20 69 74 20 69  urn TRUE if it i
16920 73 20 61 6e 64 20 46 41 4c 53 45 20 69 66 20 6e  s and FALSE if n
16930 6f 74 2e 20 20 41 75 74 6f 63 6f 6d 6d 69 74 20  ot.  Autocommit 
16940 6d 6f 64 65 20 69 73 20 6f 6e 0a 2a 2a 20 62 79  mode is on.** by
16950 20 64 65 66 61 75 6c 74 2e 20 20 41 75 74 6f 63   default.  Autoc
16960 6f 6d 6d 69 74 20 69 73 20 64 69 73 61 62 6c 65  ommit is disable
16970 64 20 62 79 20 61 20 42 45 47 49 4e 20 73 74 61  d by a BEGIN sta
16980 74 65 6d 65 6e 74 20 61 6e 64 20 72 65 65 6e 61  tement and reena
16990 62 6c 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 6e  bled.** by the n
169a0 65 78 74 20 43 4f 4d 4d 49 54 20 6f 72 20 52 4f  ext COMMIT or RO
169b0 4c 4c 42 41 43 4b 2e 0a 2a 2f 0a 69 6e 74 20 73  LLBACK..*/.int s
169c0 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 74 6f 63  qlite3_get_autoc
169d0 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33 2a 29 3b  ommit(sqlite3*);
169e0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
169f0 3a 20 20 46 69 6e 64 20 54 68 65 20 44 61 74 61  :  Find The Data
16a00 62 61 73 65 20 48 61 6e 64 6c 65 20 41 73 73 6f  base Handle Asso
16a10 63 69 61 74 65 64 20 57 69 74 68 20 41 20 50 72  ciated With A Pr
16a20 65 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74  epared Statement
16a30 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  .**.** Return th
16a40 65 20 5b 73 71 6c 69 74 65 33 2a 5d 20 64 61 74  e [sqlite3*] dat
16a50 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74 6f 20  abase handle to 
16a60 77 68 69 63 68 20 61 0a 2a 2a 20 5b 73 71 6c 69  which a.** [sqli
16a70 74 65 33 5f 73 74 6d 74 20 7c 20 70 72 65 70 61  te3_stmt | prepa
16a80 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 62  red statement] b
16a90 65 6c 6f 6e 67 73 2e 0a 2a 2a 20 54 68 69 73 20  elongs..** This 
16aa0 69 73 20 74 68 65 20 73 61 6d 65 20 64 61 74 61  is the same data
16ab0 62 61 73 65 20 68 61 6e 64 6c 65 20 74 68 61 74  base handle that
16ac0 20 77 61 73 0a 2a 2a 20 74 68 65 20 66 69 72 73   was.** the firs
16ad0 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  t argument to th
16ae0 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  e [sqlite3_prepa
16af0 72 65 5f 76 32 28 29 5d 20 6f 72 20 69 74 73 20  re_v2()] or its 
16b00 76 61 72 69 61 6e 74 73 0a 2a 2a 20 74 68 61 74  variants.** that
16b10 20 77 61 73 20 75 73 65 64 20 74 6f 20 63 72 65   was used to cre
16b20 61 74 65 20 74 68 65 20 73 74 61 74 65 6d 65 6e  ate the statemen
16b30 74 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 70  t in the first p
16b40 6c 61 63 65 2e 0a 2a 2f 0a 73 71 6c 69 74 65 33  lace..*/.sqlite3
16b50 20 2a 73 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e   *sqlite3_db_han
16b60 64 6c 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  dle(sqlite3_stmt
16b70 2a 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  *);.../*.** CAPI
16b80 33 52 45 46 3a 20 43 6f 6d 6d 69 74 20 41 6e 64  3REF: Commit And
16b90 20 52 6f 6c 6c 62 61 63 6b 20 4e 6f 74 69 66 69   Rollback Notifi
16ba0 63 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73  cation Callbacks
16bb0 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75  .**.** These rou
16bc0 74 69 6e 65 73 0a 2a 2a 20 72 65 67 69 73 74 65  tines.** registe
16bd0 72 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  r callback funct
16be0 69 6f 6e 73 20 74 6f 20 62 65 20 69 6e 76 6f 6b  ions to be invok
16bf0 65 64 20 77 68 65 6e 65 76 65 72 20 61 20 74 72  ed whenever a tr
16c00 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 20  ansaction.** is 
16c10 63 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c  committed or rol
16c20 6c 65 64 20 62 61 63 6b 2e 20 20 54 68 65 20 70  led back.  The p
16c30 41 72 67 20 61 72 67 75 6d 65 6e 74 20 69 73 20  Arg argument is 
16c40 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 0a 2a  passed through.*
16c50 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63  * to the callbac
16c60 6b 2e 20 20 49 66 20 74 68 65 20 63 61 6c 6c 62  k.  If the callb
16c70 61 63 6b 20 6f 6e 20 61 20 63 6f 6d 6d 69 74 20  ack on a commit 
16c80 68 6f 6f 6b 20 66 75 6e 63 74 69 6f 6e 20 0a 2a  hook function .*
16c90 2a 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65  * returns non-ze
16ca0 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 6d  ro, then the com
16cb0 6d 69 74 20 69 73 20 63 6f 6e 76 65 72 74 65 64  mit is converted
16cc0 20 69 6e 74 6f 20 61 20 72 6f 6c 6c 62 61 63 6b   into a rollback
16cd0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74 68  ..**.** If anoth
16ce0 65 72 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20  er function was 
16cf0 70 72 65 76 69 6f 75 73 6c 79 20 72 65 67 69 73  previously regis
16d00 74 65 72 65 64 2c 20 69 74 73 20 70 41 72 67 20  tered, its pArg 
16d10 76 61 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65  value is returne
16d20 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 20  d..** Otherwise 
16d30 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64  NULL is returned
16d40 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72  ..**.** Register
16d50 69 6e 67 20 61 20 4e 55 4c 4c 20 66 75 6e 63 74  ing a NULL funct
16d60 69 6f 6e 20 64 69 73 61 62 6c 65 73 20 74 68 65  ion disables the
16d70 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a   callback..**.**
16d80 20 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65   For the purpose
16d90 73 20 6f 66 20 74 68 69 73 20 41 50 49 2c 20 61  s of this API, a
16da0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
16db0 73 61 69 64 20 74 6f 20 68 61 76 65 20 62 65 65  said to have bee
16dc0 6e 20 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63  n .** rolled bac
16dd0 6b 20 69 66 20 61 6e 20 65 78 70 6c 69 63 69 74  k if an explicit
16de0 20 22 52 4f 4c 4c 42 41 43 4b 22 20 73 74 61 74   "ROLLBACK" stat
16df0 65 6d 65 6e 74 20 69 73 20 65 78 65 63 75 74 65  ement is execute
16e00 64 2c 20 6f 72 0a 2a 2a 20 61 6e 20 65 72 72 6f  d, or.** an erro
16e10 72 20 6f 72 20 63 6f 6e 73 74 72 61 69 6e 74 20  r or constraint 
16e20 63 61 75 73 65 73 20 61 6e 20 69 6d 70 6c 69 63  causes an implic
16e30 69 74 20 72 6f 6c 6c 62 61 63 6b 20 74 6f 20 6f  it rollback to o
16e40 63 63 75 72 2e 20 54 68 65 20 0a 2a 2a 20 63 61  ccur. The .** ca
16e50 6c 6c 62 61 63 6b 20 69 73 20 6e 6f 74 20 69 6e  llback is not in
16e60 76 6f 6b 65 64 20 69 66 20 61 20 74 72 61 6e 73  voked if a trans
16e70 61 63 74 69 6f 6e 20 69 73 20 61 75 74 6f 6d 61  action is automa
16e80 74 69 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 0a 2a  tically rolled.*
16e90 2a 20 62 61 63 6b 20 62 65 63 61 75 73 65 20 74  * back because t
16ea0 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
16eb0 65 63 74 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64  ection is closed
16ec0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 61 72  ..**.** These ar
16ed0 65 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 69  e experimental i
16ee0 6e 74 65 72 66 61 63 65 73 20 61 6e 64 20 61 72  nterfaces and ar
16ef0 65 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61  e subject to cha
16f00 6e 67 65 2e 0a 2a 2f 0a 76 6f 69 64 20 2a 73 71  nge..*/.void *sq
16f10 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f  lite3_commit_hoo
16f20 6b 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 28  k(sqlite3*, int(
16f30 2a 29 28 76 6f 69 64 2a 29 2c 20 76 6f 69 64 2a  *)(void*), void*
16f40 29 3b 0a 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  );.void *sqlite3
16f50 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 73  _rollback_hook(s
16f60 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 28 2a 29  qlite3*, void(*)
16f70 28 76 6f 69 64 20 2a 29 2c 20 76 6f 69 64 2a 29  (void *), void*)
16f80 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
16f90 46 3a 20 44 61 74 61 20 43 68 61 6e 67 65 20 4e  F: Data Change N
16fa0 6f 74 69 66 69 63 61 74 69 6f 6e 20 43 61 6c 6c  otification Call
16fb0 62 61 63 6b 73 0a 2a 2a 0a 2a 2a 20 52 65 67 69  backs.**.** Regi
16fc0 73 74 65 72 20 61 20 63 61 6c 6c 62 61 63 6b 20  ster a callback 
16fd0 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68  function with th
16fe0 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
16ff0 63 74 69 6f 6e 20 69 64 65 6e 74 69 66 69 65 64  ction identified
17000 20 62 79 20 74 68 65 20 0a 2a 2a 20 66 69 72 73   by the .** firs
17010 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 62 65  t argument to be
17020 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 65   invoked wheneve
17030 72 20 61 20 72 6f 77 20 69 73 20 75 70 64 61 74  r a row is updat
17040 65 64 2c 20 69 6e 73 65 72 74 65 64 20 6f 72 20  ed, inserted or 
17050 64 65 6c 65 74 65 64 2e 0a 2a 2a 20 41 6e 79 20  deleted..** Any 
17060 63 61 6c 6c 62 61 63 6b 20 73 65 74 20 62 79 20  callback set by 
17070 61 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 20  a previous call 
17080 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
17090 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 0a 2a   for the same .*
170a0 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * database conne
170b0 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64  ction is overrid
170c0 64 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  den..**.** The s
170d0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69  econd argument i
170e0 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
170f0 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69  he function to i
17100 6e 76 6f 6b 65 20 77 68 65 6e 20 61 20 0a 2a 2a  nvoke when a .**
17110 20 72 6f 77 20 69 73 20 75 70 64 61 74 65 64 2c   row is updated,
17120 20 69 6e 73 65 72 74 65 64 20 6f 72 20 64 65 6c   inserted or del
17130 65 74 65 64 2e 20 54 68 65 20 66 69 72 73 74 20  eted. The first 
17140 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  argument to the 
17150 63 61 6c 6c 62 61 63 6b 20 69 73 0a 2a 2a 20 61  callback is.** a
17160 20 63 6f 70 79 20 6f 66 20 74 68 65 20 74 68 69   copy of the thi
17170 72 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73  rd argument to s
17180 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f  qlite3_update_ho
17190 6f 6b 28 29 2e 20 54 68 65 20 73 65 63 6f 6e 64  ok(). The second
171a0 20 63 61 6c 6c 62 61 63 6b 20 0a 2a 2a 20 61 72   callback .** ar
171b0 67 75 6d 65 6e 74 20 69 73 20 6f 6e 65 20 6f 66  gument is one of
171c0 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 2c 20   SQLITE_INSERT, 
171d0 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 20 6f 72  SQLITE_DELETE or
171e0 20 53 51 4c 49 54 45 5f 55 50 44 41 54 45 2c 20   SQLITE_UPDATE, 
171f0 64 65 70 65 6e 64 69 6e 67 0a 2a 2a 20 6f 6e 20  depending.** on 
17200 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 68  the operation th
17210 61 74 20 63 61 75 73 65 64 20 74 68 65 20 63 61  at caused the ca
17220 6c 6c 62 61 63 6b 20 74 6f 20 62 65 20 69 6e 76  llback to be inv
17230 6f 6b 65 64 2e 20 54 68 65 20 74 68 69 72 64 20  oked. The third 
17240 61 6e 64 20 0a 2a 2a 20 66 6f 75 72 74 68 20 61  and .** fourth a
17250 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20  rguments to the 
17260 63 61 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e  callback contain
17270 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65   pointers to the
17280 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 0a 2a   database and .*
17290 2a 20 74 61 62 6c 65 20 6e 61 6d 65 20 63 6f 6e  * table name con
172a0 74 61 69 6e 69 6e 67 20 74 68 65 20 61 66 66 65  taining the affe
172b0 63 74 65 64 20 72 6f 77 2e 20 54 68 65 20 66 69  cted row. The fi
172c0 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 20 70 61 72  nal callback par
172d0 61 6d 65 74 65 72 20 69 73 20 0a 2a 2a 20 74 68  ameter is .** th
172e0 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 72  e rowid of the r
172f0 6f 77 2e 20 49 6e 20 74 68 65 20 63 61 73 65 20  ow. In the case 
17300 6f 66 20 61 6e 20 75 70 64 61 74 65 2c 20 74 68  of an update, th
17310 69 73 20 69 73 20 74 68 65 20 72 6f 77 69 64 20  is is the rowid 
17320 61 66 74 65 72 20 0a 2a 2a 20 74 68 65 20 75 70  after .** the up
17330 64 61 74 65 20 74 61 6b 65 73 20 70 6c 61 63 65  date takes place
17340 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 75 70 64 61  ..**.** The upda
17350 74 65 20 68 6f 6f 6b 20 69 73 20 6e 6f 74 20 69  te hook is not i
17360 6e 76 6f 6b 65 64 20 77 68 65 6e 20 69 6e 74 65  nvoked when inte
17370 72 6e 61 6c 20 73 79 73 74 65 6d 20 74 61 62 6c  rnal system tabl
17380 65 73 20 61 72 65 0a 2a 2a 20 6d 6f 64 69 66 69  es are.** modifi
17390 65 64 20 28 69 2e 65 2e 20 73 71 6c 69 74 65 5f  ed (i.e. sqlite_
173a0 6d 61 73 74 65 72 20 61 6e 64 20 73 71 6c 69 74  master and sqlit
173b0 65 5f 73 65 71 75 65 6e 63 65 29 2e 0a 2a 2a 0a  e_sequence)..**.
173c0 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 66 75  ** If another fu
173d0 6e 63 74 69 6f 6e 20 77 61 73 20 70 72 65 76 69  nction was previ
173e0 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 65 64  ously registered
173f0 2c 20 69 74 73 20 70 41 72 67 20 76 61 6c 75 65  , its pArg value
17400 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
17410 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 20   Otherwise NULL 
17420 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a  is returned..*/.
17430 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 75 70  void *sqlite3_up
17440 64 61 74 65 5f 68 6f 6f 6b 28 0a 20 20 73 71 6c  date_hook(.  sql
17450 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64 28 2a  ite3*, .  void(*
17460 29 28 76 6f 69 64 20 2a 2c 69 6e 74 20 2c 63 68  )(void *,int ,ch
17470 61 72 20 63 6f 6e 73 74 20 2a 2c 63 68 61 72 20  ar const *,char 
17480 63 6f 6e 73 74 20 2a 2c 73 71 6c 69 74 65 5f 69  const *,sqlite_i
17490 6e 74 36 34 29 2c 0a 20 20 76 6f 69 64 2a 0a 29  nt64),.  void*.)
174a0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
174b0 46 3a 20 20 45 6e 61 62 6c 65 20 4f 72 20 44 69  F:  Enable Or Di
174c0 73 61 62 6c 65 20 53 68 61 72 65 64 20 50 61 67  sable Shared Pag
174d0 65 72 20 43 61 63 68 65 0a 2a 2a 0a 2a 2a 20 54  er Cache.**.** T
174e0 68 69 73 20 72 6f 75 74 69 6e 65 20 65 6e 61 62  his routine enab
174f0 6c 65 73 20 6f 72 20 64 69 73 61 62 6c 65 73 20  les or disables 
17500 74 68 65 20 73 68 61 72 69 6e 67 20 6f 66 20 74  the sharing of t
17510 68 65 20 64 61 74 61 62 61 73 65 20 63 61 63 68  he database cach
17520 65 0a 2a 2a 20 61 6e 64 20 73 63 68 65 6d 61 20  e.** and schema 
17530 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20  data structures 
17540 62 65 74 77 65 65 6e 20 63 6f 6e 6e 65 63 74 69  between connecti
17550 6f 6e 73 20 74 6f 20 74 68 65 20 73 61 6d 65 20  ons to the same 
17560 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20 53 68 61  database..** Sha
17570 72 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 64 20  ring is enabled 
17580 69 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20  if the argument 
17590 69 73 20 74 72 75 65 20 61 6e 64 20 64 69 73 61  is true and disa
175a0 62 6c 65 64 20 69 66 20 74 68 65 20 61 72 67 75  bled if the argu
175b0 6d 65 6e 74 0a 2a 2a 20 69 73 20 66 61 6c 73 65  ment.** is false
175c0 2e 0a 2a 2a 0a 2a 2a 20 43 61 63 68 65 20 73 68  ..**.** Cache sh
175d0 61 72 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 64  aring is enabled
175e0 20 61 6e 64 20 64 69 73 61 62 6c 65 64 20 6f 6e   and disabled on
175f0 20 61 20 74 68 72 65 61 64 2d 62 79 2d 74 68 72   a thread-by-thr
17600 65 61 64 20 62 61 73 69 73 2e 0a 2a 2a 20 45 61  ead basis..** Ea
17610 63 68 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 20  ch call to this 
17620 72 6f 75 74 69 6e 65 20 65 6e 61 62 6c 65 73 20  routine enables 
17630 6f 72 20 64 69 73 61 62 6c 65 73 20 63 61 63 68  or disables cach
17640 65 20 73 68 61 72 69 6e 67 20 6f 6e 6c 79 20 66  e sharing only f
17650 6f 72 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e  or.** connection
17660 73 20 63 72 65 61 74 65 64 20 69 6e 20 74 68 65  s created in the
17670 20 73 61 6d 65 20 74 68 72 65 61 64 20 69 6e 20   same thread in 
17680 77 68 69 63 68 20 74 68 69 73 20 72 6f 75 74 69  which this routi
17690 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a  ne is called..**
176a0 20 54 68 65 72 65 20 69 73 20 6e 6f 20 6d 65 63   There is no mec
176b0 68 61 6e 69 73 6d 20 66 6f 72 20 73 68 61 72 69  hanism for shari
176c0 6e 67 20 63 61 63 68 65 20 62 65 74 77 65 65 6e  ng cache between
176d0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
176e0 74 69 6f 6e 73 0a 2a 2a 20 72 75 6e 6e 69 6e 67  tions.** running
176f0 20 69 6e 20 64 69 66 66 65 72 65 6e 74 20 74 68   in different th
17700 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 53 68 61  reads..**.** Sha
17710 72 69 6e 67 20 6d 75 73 74 20 62 65 20 64 69 73  ring must be dis
17720 61 62 6c 65 64 20 70 72 69 6f 72 20 74 6f 20 73  abled prior to s
17730 68 75 74 74 69 6e 67 20 64 6f 77 6e 20 61 20 74  hutting down a t
17740 68 72 65 61 64 20 6f 72 20 65 6c 73 65 0a 2a 2a  hread or else.**
17750 20 74 68 65 20 74 68 72 65 61 64 20 77 69 6c 6c   the thread will
17760 20 6c 65 61 6b 20 6d 65 6d 6f 72 79 2e 20 20 43   leak memory.  C
17770 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  all this routine
17780 20 77 69 74 68 20 61 6e 20 61 72 67 75 6d 65 6e   with an argumen
17790 74 20 6f 66 0a 2a 2a 20 30 20 74 6f 20 74 75 72  t of.** 0 to tur
177a0 6e 20 6f 66 66 20 73 68 61 72 69 6e 67 2e 20 20  n off sharing.  
177b0 4f 72 20 75 73 65 20 74 68 65 20 73 71 6c 69 74  Or use the sqlit
177c0 65 33 5f 74 68 72 65 61 64 5f 63 6c 65 61 6e 75  e3_thread_cleanu
177d0 70 28 29 20 41 50 49 2e 0a 2a 2a 0a 2a 2a 20 54  p() API..**.** T
177e0 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 74  his routine must
177f0 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 77   not be called w
17800 68 65 6e 20 61 6e 79 20 64 61 74 61 62 61 73 65  hen any database
17810 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 0a 2a 2a 20   connections.** 
17820 61 72 65 20 61 63 74 69 76 65 20 69 6e 20 74 68  are active in th
17830 65 20 63 75 72 72 65 6e 74 20 74 68 72 65 61 64  e current thread
17840 2e 20 20 45 6e 61 62 6c 69 6e 67 20 6f 72 20 64  .  Enabling or d
17850 69 73 61 62 6c 69 6e 67 20 73 68 61 72 65 64 0a  isabling shared.
17860 2a 2a 20 63 61 63 68 65 20 77 68 69 6c 65 20 74  ** cache while t
17870 68 65 72 65 20 61 72 65 20 61 63 74 69 76 65 20  here are active 
17880 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
17890 69 6f 6e 73 20 77 69 6c 6c 20 72 65 73 75 6c 74  ions will result
178a0 0a 2a 2a 20 69 6e 20 6d 65 6d 6f 72 79 20 63 6f  .** in memory co
178b0 72 72 75 70 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  rruption..**.** 
178c0 57 68 65 6e 20 74 68 65 20 73 68 61 72 65 64 20  When the shared 
178d0 63 61 63 68 65 20 69 73 20 65 6e 61 62 6c 65 64  cache is enabled
178e0 2c 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69  , the.** followi
178f0 6e 67 20 72 6f 75 74 69 6e 65 73 20 6d 75 73 74  ng routines must
17900 20 61 6c 77 61 79 73 20 62 65 20 63 61 6c 6c 65   always be calle
17910 64 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20  d from the same 
17920 74 68 72 65 61 64 3a 0a 2a 2a 20 5b 73 71 6c 69  thread:.** [sqli
17930 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71  te3_open()], [sq
17940 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
17950 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74  ()], [sqlite3_st
17960 65 70 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74  ep()],.** [sqlit
17970 65 33 5f 72 65 73 65 74 28 29 5d 2c 20 5b 73 71  e3_reset()], [sq
17980 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29  lite3_finalize()
17990 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  ], and [sqlite3_
179a0 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 20 54 68 69  close()]..** Thi
179b0 73 20 69 73 20 64 75 65 20 74 6f 20 74 68 65 20  s is due to the 
179c0 66 61 63 74 20 74 68 61 74 20 74 68 65 20 73 68  fact that the sh
179d0 61 72 65 64 20 63 61 63 68 65 20 6d 61 6b 65 73  ared cache makes
179e0 20 75 73 65 20 6f 66 0a 2a 2a 20 74 68 72 65 61   use of.** threa
179f0 64 2d 73 70 65 63 69 66 69 63 20 73 74 6f 72 61  d-specific stora
17a00 67 65 20 73 6f 20 74 68 61 74 20 69 74 20 77 69  ge so that it wi
17a10 6c 6c 20 62 65 20 61 76 61 69 6c 61 62 6c 65 20  ll be available 
17a20 66 6f 72 20 73 68 61 72 69 6e 67 0a 2a 2a 20 77  for sharing.** w
17a30 69 74 68 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63  ith other connec
17a40 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 56 69 72  tions..**.** Vir
17a50 74 75 61 6c 20 74 61 62 6c 65 73 20 63 61 6e 6e  tual tables cann
17a60 6f 74 20 62 65 20 75 73 65 64 20 77 69 74 68 20  ot be used with 
17a70 61 20 73 68 61 72 65 64 20 63 61 63 68 65 2e 20  a shared cache. 
17a80 20 57 68 65 6e 20 73 68 61 72 65 64 0a 2a 2a 20   When shared.** 
17a90 63 61 63 68 65 20 69 73 20 65 6e 61 62 6c 65 64  cache is enabled
17aa0 2c 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 72  , the sqlite3_cr
17ab0 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20 41 50  eate_module() AP
17ac0 49 20 75 73 65 64 20 74 6f 20 72 65 67 69 73 74  I used to regist
17ad0 65 72 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74 61  er.** virtual ta
17ae0 62 6c 65 73 20 77 69 6c 6c 20 61 6c 77 61 79 73  bles will always
17af0 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72   return an error
17b00 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
17b10 74 69 6e 65 20 72 65 74 75 72 6e 73 20 5b 53 51  tine returns [SQ
17b20 4c 49 54 45 5f 4f 4b 5d 20 69 66 20 73 68 61 72  LITE_OK] if shar
17b30 65 64 20 63 61 63 68 65 20 77 61 73 0a 2a 2a 20  ed cache was.** 
17b40 65 6e 61 62 6c 65 64 20 6f 72 20 64 69 73 61 62  enabled or disab
17b50 6c 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79  led successfully
17b60 2e 20 20 41 6e 20 5b 53 51 4c 49 54 45 5f 45 52  .  An [SQLITE_ER
17b70 52 4f 52 20 7c 20 65 72 72 6f 72 20 63 6f 64 65  ROR | error code
17b80 5d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64  ].** is returned
17b90 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a   otherwise..**.*
17ba0 2a 20 53 68 61 72 65 64 20 63 61 63 68 65 20 69  * Shared cache i
17bb0 73 20 64 69 73 61 62 6c 65 64 20 62 79 20 64 65  s disabled by de
17bc0 66 61 75 6c 74 20 66 6f 72 20 62 61 63 6b 77 61  fault for backwa
17bd0 72 64 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79  rd compatibility
17be0 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33  ..*/.int sqlite3
17bf0 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63  _enable_shared_c
17c00 61 63 68 65 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a  ache(int);../*.*
17c10 2a 20 43 41 50 49 33 52 45 46 3a 20 20 41 74 74  * CAPI3REF:  Att
17c20 65 6d 70 74 20 54 6f 20 46 72 65 65 20 48 65 61  empt To Free Hea
17c30 70 20 4d 65 6d 6f 72 79 0a 2a 2a 0a 2a 2a 20 41  p Memory.**.** A
17c40 74 74 65 6d 70 74 20 74 6f 20 66 72 65 65 20 4e  ttempt to free N
17c50 20 62 79 74 65 73 20 6f 66 20 68 65 61 70 20 6d   bytes of heap m
17c60 65 6d 6f 72 79 20 62 79 20 64 65 61 6c 6c 6f 63  emory by dealloc
17c70 61 74 69 6e 67 20 6e 6f 6e 2d 65 73 73 65 6e 74  ating non-essent
17c80 69 61 6c 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c  ial.** memory al
17c90 6c 6f 63 61 74 69 6f 6e 73 20 68 65 6c 64 20 62  locations held b
17ca0 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6c  y the database l
17cb0 69 62 72 61 72 79 20 28 65 78 61 6d 70 6c 65 3a  ibrary (example:
17cc0 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 75 73 65 64   memory .** used
17cd0 20 74 6f 20 63 61 63 68 65 20 64 61 74 61 62 61   to cache databa
17ce0 73 65 20 70 61 67 65 73 20 74 6f 20 69 6d 70 72  se pages to impr
17cf0 6f 76 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 29  ove performance)
17d00 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ..**.** This fun
17d10 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 61 20 70  ction is not a p
17d20 61 72 74 20 6f 66 20 73 74 61 6e 64 61 72 64 20  art of standard 
17d30 62 75 69 6c 64 73 2e 20 20 49 74 20 69 73 20 6f  builds.  It is o
17d40 6e 6c 79 20 63 72 65 61 74 65 64 0a 2a 2a 20 69  nly created.** i
17d50 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70  f SQLite is comp
17d60 69 6c 65 64 20 77 69 74 68 20 74 68 65 20 53 51  iled with the SQ
17d70 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f  LITE_ENABLE_MEMO
17d80 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 20 6d 61  RY_MANAGEMENT ma
17d90 63 72 6f 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69  cro..*/.int sqli
17da0 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f  te3_release_memo
17db0 72 79 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20  ry(int);../*.** 
17dc0 43 41 50 49 33 52 45 46 3a 20 20 49 6d 70 6f 73  CAPI3REF:  Impos
17dd0 65 20 41 20 4c 69 6d 69 74 20 4f 6e 20 48 65 61  e A Limit On Hea
17de0 70 20 53 69 7a 65 0a 2a 2a 0a 2a 2a 20 50 6c 61  p Size.**.** Pla
17df0 63 65 20 61 20 22 73 6f 66 74 22 20 6c 69 6d 69  ce a "soft" limi
17e00 74 20 6f 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20  t on the amount 
17e10 6f 66 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 74  of heap memory t
17e20 68 61 74 20 6d 61 79 20 62 65 20 61 6c 6c 6f 63  hat may be alloc
17e30 61 74 65 64 20 62 79 0a 2a 2a 20 53 51 4c 69 74  ated by.** SQLit
17e40 65 20 77 69 74 68 69 6e 20 74 68 65 20 63 75 72  e within the cur
17e50 72 65 6e 74 20 74 68 72 65 61 64 2e 20 49 66 20  rent thread. If 
17e60 61 6e 20 69 6e 74 65 72 6e 61 6c 20 61 6c 6c 6f  an internal allo
17e70 63 61 74 69 6f 6e 20 69 73 20 72 65 71 75 65 73  cation is reques
17e80 74 65 64 20 0a 2a 2a 20 74 68 61 74 20 77 6f 75  ted .** that wou
17e90 6c 64 20 65 78 63 65 65 64 20 74 68 65 20 73 70  ld exceed the sp
17ea0 65 63 69 66 69 65 64 20 6c 69 6d 69 74 2c 20 5b  ecified limit, [
17eb0 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f  sqlite3_release_
17ec0 6d 65 6d 6f 72 79 28 29 5d 20 69 73 20 69 6e 76  memory()] is inv
17ed0 6f 6b 65 64 0a 2a 2a 20 6f 6e 65 20 6f 72 20 6d  oked.** one or m
17ee0 6f 72 65 20 74 69 6d 65 73 20 74 6f 20 66 72 65  ore times to fre
17ef0 65 20 75 70 20 73 6f 6d 65 20 73 70 61 63 65 20  e up some space 
17f00 62 65 66 6f 72 65 20 74 68 65 20 61 6c 6c 6f 63  before the alloc
17f10 61 74 69 6f 6e 20 69 73 20 6d 61 64 65 2e 0a 2a  ation is made..*
17f20 2a 0a 2a 2a 20 54 68 65 20 6c 69 6d 69 74 20 69  *.** The limit i
17f30 73 20 63 61 6c 6c 65 64 20 22 73 6f 66 74 22 2c  s called "soft",
17f40 20 62 65 63 61 75 73 65 20 69 66 20 5b 73 71 6c   because if [sql
17f50 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d  ite3_release_mem
17f60 6f 72 79 28 29 5d 20 63 61 6e 6e 6f 74 20 66 72  ory()] cannot fr
17f70 65 65 0a 2a 2a 20 73 75 66 66 69 63 69 65 6e 74  ee.** sufficient
17f80 20 6d 65 6d 6f 72 79 20 74 6f 20 70 72 65 76 65   memory to preve
17f90 6e 74 20 74 68 65 20 6c 69 6d 69 74 20 66 72 6f  nt the limit fro
17fa0 6d 20 62 65 69 6e 67 20 65 78 63 65 65 64 65 64  m being exceeded
17fb0 2c 20 74 68 65 20 6d 65 6d 6f 72 79 20 69 73 0a  , the memory is.
17fc0 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 79  ** allocated any
17fd0 77 61 79 20 61 6e 64 20 74 68 65 20 63 75 72 72  way and the curr
17fe0 65 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20 70 72  ent operation pr
17ff0 6f 63 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 50 72  oceeds..**.** Pr
18000 69 6f 72 20 74 6f 20 73 68 75 74 74 69 6e 67 20  ior to shutting 
18010 64 6f 77 6e 20 61 20 74 68 72 65 61 64 20 73 71  down a thread sq
18020 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f  lite3_soft_heap_
18030 6c 69 6d 69 74 28 29 20 6d 75 73 74 20 62 65 20  limit() must be 
18040 73 65 74 20 74 6f 20 0a 2a 2a 20 7a 65 72 6f 20  set to .** zero 
18050 28 74 68 65 20 64 65 66 61 75 6c 74 29 20 6f 72  (the default) or
18060 20 65 6c 73 65 20 74 68 65 20 74 68 72 65 61 64   else the thread
18070 20 77 69 6c 6c 20 6c 65 61 6b 20 6d 65 6d 6f 72   will leak memor
18080 79 2e 20 41 6c 74 65 72 6e 61 74 69 76 65 6c 79  y. Alternatively
18090 2c 20 75 73 65 0a 2a 2a 20 74 68 65 20 5b 73 71  , use.** the [sq
180a0 6c 69 74 65 33 5f 74 68 72 65 61 64 5f 63 6c 65  lite3_thread_cle
180b0 61 6e 75 70 28 29 5d 20 41 50 49 2e 0a 2a 2a 0a  anup()] API..**.
180c0 2a 2a 20 41 20 6e 65 67 61 74 69 76 65 20 6f 72  ** A negative or
180d0 20 7a 65 72 6f 20 76 61 6c 75 65 20 66 6f 72 20   zero value for 
180e0 4e 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  N means that the
180f0 72 65 20 69 73 20 6e 6f 20 73 6f 66 74 20 68 65  re is no soft he
18100 61 70 20 6c 69 6d 69 74 20 61 6e 64 0a 2a 2a 20  ap limit and.** 
18110 5b 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65  [sqlite3_release
18120 5f 6d 65 6d 6f 72 79 28 29 5d 20 77 69 6c 6c 20  _memory()] will 
18130 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 77  only be called w
18140 68 65 6e 20 6d 65 6d 6f 72 79 20 69 73 20 65 78  hen memory is ex
18150 68 61 75 73 65 64 2e 0a 2a 2a 20 54 68 65 20 64  haused..** The d
18160 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72  efault value for
18170 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c   the soft heap l
18180 69 6d 69 74 20 69 73 20 7a 65 72 6f 2e 0a 2a 2a  imit is zero..**
18190 0a 2a 2a 20 53 51 4c 69 74 65 20 6d 61 6b 65 73  .** SQLite makes
181a0 20 61 20 62 65 73 74 20 65 66 66 6f 72 74 20 74   a best effort t
181b0 6f 20 68 6f 6e 6f 72 20 74 68 65 20 73 6f 66 74  o honor the soft
181c0 20 68 65 61 70 20 6c 69 6d 69 74 2e 20 20 42 75   heap limit.  Bu
181d0 74 20 69 66 20 69 74 0a 2a 2a 20 69 73 20 75 6e  t if it.** is un
181e0 61 62 6c 65 20 74 6f 20 72 65 64 75 63 65 20 6d  able to reduce m
181f0 65 6d 6f 72 79 20 75 73 61 67 65 20 62 65 6c 6f  emory usage belo
18200 77 20 74 68 65 20 73 6f 66 74 20 6c 69 6d 69 74  w the soft limit
18210 2c 20 65 78 65 63 75 74 69 6f 6e 20 77 69 6c 6c  , execution will
18220 0a 2a 2a 20 63 6f 6e 74 69 6e 75 65 20 77 69 74  .** continue wit
18230 68 6f 75 74 20 65 72 72 6f 72 20 6f 72 20 6e 6f  hout error or no
18240 74 69 66 69 63 61 74 69 6f 6e 2e 20 20 54 68 69  tification.  Thi
18250 73 20 69 73 20 77 68 79 20 74 68 65 20 6c 69 6d  s is why the lim
18260 69 74 20 69 73 20 0a 2a 2a 20 63 61 6c 6c 65 64  it is .** called
18270 20 61 20 22 73 6f 66 74 22 20 6c 69 6d 69 74 2e   a "soft" limit.
18280 20 20 49 74 20 69 73 20 61 64 76 69 73 6f 72 79    It is advisory
18290 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69   only..**.** Thi
182a0 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e  s function is on
182b0 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 66 20  ly available if 
182c0 74 68 65 20 6c 69 62 72 61 72 79 20 77 61 73 20  the library was 
182d0 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68  compiled with th
182e0 65 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 45 4e 41  e .** SQLITE_ENA
182f0 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47  BLE_MEMORY_MANAG
18300 45 4d 45 4e 54 20 6f 70 74 69 6f 6e 20 73 65 74  EMENT option set
18310 2e 0a 2a 2a 20 6d 65 6d 6f 72 79 2d 6d 61 6e 61  ..** memory-mana
18320 67 65 6d 65 6e 74 20 68 61 73 20 62 65 65 6e 20  gement has been 
18330 65 6e 61 62 6c 65 64 2e 0a 2a 2f 0a 76 6f 69 64  enabled..*/.void
18340 20 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65   sqlite3_soft_he
18350 61 70 5f 6c 69 6d 69 74 28 69 6e 74 29 3b 0a 0a  ap_limit(int);..
18360 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
18370 20 43 6c 65 61 6e 20 55 70 20 54 68 72 65 61 64   Clean Up Thread
18380 20 4c 6f 63 61 6c 20 53 74 6f 72 61 67 65 0a 2a   Local Storage.*
18390 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
183a0 65 20 6d 61 6b 65 73 20 73 75 72 65 20 74 68 61  e makes sure tha
183b0 74 20 61 6c 6c 20 74 68 72 65 61 64 2d 6c 6f 63  t all thread-loc
183c0 61 6c 20 73 74 6f 72 61 67 65 20 68 61 73 20 62  al storage has b
183d0 65 65 6e 0a 2a 2a 20 64 65 61 6c 6c 6f 63 61 74  een.** deallocat
183e0 65 64 20 66 6f 72 20 74 68 65 20 63 75 72 72 65  ed for the curre
183f0 6e 74 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a  nt thread..**.**
18400 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
18410 20 6e 6f 74 20 74 65 63 68 6e 69 63 61 6c 6c 79   not technically
18420 20 6e 65 63 65 73 73 61 72 79 2e 20 20 41 6c 6c   necessary.  All
18430 20 74 68 72 65 61 64 2d 6c 6f 63 61 6c 20 73 74   thread-local st
18440 6f 72 61 67 65 0a 2a 2a 20 77 69 6c 6c 20 62 65  orage.** will be
18450 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64   automatically d
18460 65 61 6c 6c 6f 63 61 74 65 64 20 6f 6e 63 65 20  eallocated once 
18470 6d 65 6d 6f 72 79 2d 6d 61 6e 61 67 65 6d 65 6e  memory-managemen
18480 74 20 61 6e 64 0a 2a 2a 20 73 68 61 72 65 64 2d  t and.** shared-
18490 63 61 63 68 65 20 61 72 65 20 64 69 73 61 62 6c  cache are disabl
184a0 65 64 20 61 6e 64 20 74 68 65 20 73 6f 66 74 20  ed and the soft 
184b0 68 65 61 70 20 6c 69 6d 69 74 20 68 61 73 20 62  heap limit has b
184c0 65 65 6e 20 73 65 74 0a 2a 2a 20 74 6f 20 7a 65  een set.** to ze
184d0 72 6f 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  ro.  This routin
184e0 65 20 69 73 20 70 72 6f 76 69 64 65 64 20 61 73  e is provided as
184f0 20 61 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 66   a convenience f
18500 6f 72 20 75 73 65 72 73 20 77 68 6f 0a 2a 2a 20  or users who.** 
18510 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20 61 62 73  want to make abs
18520 6f 6c 75 74 65 6c 79 20 73 75 72 65 20 74 68 65  olutely sure the
18530 79 20 68 61 76 65 20 6e 6f 74 20 66 6f 72 67 6f  y have not forgo
18540 74 74 65 6e 20 73 6f 6d 65 74 68 69 6e 67 0a 2a  tten something.*
18550 2a 20 70 72 69 6f 72 20 74 6f 20 6b 69 6c 6c 69  * prior to killi
18560 6e 67 20 6f 66 66 20 61 20 74 68 72 65 61 64 2e  ng off a thread.
18570 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33  .*/.void sqlite3
18580 5f 74 68 72 65 61 64 5f 63 6c 65 61 6e 75 70 28  _thread_cleanup(
18590 76 6f 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  void);../*.** CA
185a0 50 49 33 52 45 46 3a 20 20 45 78 74 72 61 63 74  PI3REF:  Extract
185b0 20 4d 65 74 61 64 61 74 61 20 41 62 6f 75 74 20   Metadata About 
185c0 41 20 43 6f 6c 75 6d 6e 20 4f 66 20 41 20 54 61  A Column Of A Ta
185d0 62 6c 65 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ble.**.** This r
185e0 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e  outine.** return
185f0 73 20 6d 65 74 61 2d 64 61 74 61 20 61 62 6f 75  s meta-data abou
18600 74 20 61 20 73 70 65 63 69 66 69 63 20 63 6f 6c  t a specific col
18610 75 6d 6e 20 6f 66 20 61 20 73 70 65 63 69 66 69  umn of a specifi
18620 63 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 74 61  c database.** ta
18630 62 6c 65 20 61 63 63 65 73 73 69 62 6c 65 20 75  ble accessible u
18640 73 69 6e 67 20 74 68 65 20 63 6f 6e 6e 65 63 74  sing the connect
18650 69 6f 6e 20 68 61 6e 64 6c 65 20 70 61 73 73 65  ion handle passe
18660 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 66  d as the first f
18670 75 6e 63 74 69 6f 6e 20 0a 2a 2a 20 61 72 67 75  unction .** argu
18680 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ment..**.** The 
18690 63 6f 6c 75 6d 6e 20 69 73 20 69 64 65 6e 74 69  column is identi
186a0 66 69 65 64 20 62 79 20 74 68 65 20 73 65 63 6f  fied by the seco
186b0 6e 64 2c 20 74 68 69 72 64 20 61 6e 64 20 66 6f  nd, third and fo
186c0 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 73 20  urth parameters 
186d0 74 6f 20 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63  to .** this func
186e0 74 69 6f 6e 2e 20 54 68 65 20 73 65 63 6f 6e 64  tion. The second
186f0 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 65 69   parameter is ei
18700 74 68 65 72 20 74 68 65 20 6e 61 6d 65 20 6f 66  ther the name of
18710 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a   the database.**
18720 20 28 69 2e 65 2e 20 22 6d 61 69 6e 22 2c 20 22   (i.e. "main", "
18730 74 65 6d 70 22 20 6f 72 20 61 6e 20 61 74 74 61  temp" or an atta
18740 63 68 65 64 20 64 61 74 61 62 61 73 65 29 20 63  ched database) c
18750 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 73 70  ontaining the sp
18760 65 63 69 66 69 65 64 0a 2a 2a 20 74 61 62 6c 65  ecified.** table
18770 20 6f 72 20 4e 55 4c 4c 2e 20 49 66 20 69 74 20   or NULL. If it 
18780 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 6c  is NULL, then al
18790 6c 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62  l attached datab
187a0 61 73 65 73 20 61 72 65 20 73 65 61 72 63 68 65  ases are searche
187b0 64 0a 2a 2a 20 66 6f 72 20 74 68 65 20 74 61 62  d.** for the tab
187c0 6c 65 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d  le using the sam
187d0 65 20 61 6c 67 6f 72 69 74 68 6d 20 61 73 20 74  e algorithm as t
187e0 68 65 20 64 61 74 61 62 61 73 65 20 65 6e 67 69  he database engi
187f0 6e 65 20 75 73 65 73 20 74 6f 20 0a 2a 2a 20 72  ne uses to .** r
18800 65 73 6f 6c 76 65 20 75 6e 71 75 61 6c 69 66 69  esolve unqualifi
18810 65 64 20 74 61 62 6c 65 20 72 65 66 65 72 65 6e  ed table referen
18820 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74  ces..**.** The t
18830 68 69 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20  hird and fourth 
18840 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68  parameters to th
18850 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20  is function are 
18860 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 63 6f  the table and co
18870 6c 75 6d 6e 20 0a 2a 2a 20 6e 61 6d 65 20 6f 66  lumn .** name of
18880 20 74 68 65 20 64 65 73 69 72 65 64 20 63 6f 6c   the desired col
18890 75 6d 6e 2c 20 72 65 73 70 65 63 74 69 76 65 6c  umn, respectivel
188a0 79 2e 20 4e 65 69 74 68 65 72 20 6f 66 20 74 68  y. Neither of th
188b0 65 73 65 20 70 61 72 61 6d 65 74 65 72 73 20 0a  ese parameters .
188c0 2a 2a 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2e 0a  ** may be NULL..
188d0 2a 2a 0a 2a 2a 20 4d 65 74 61 20 69 6e 66 6f 72  **.** Meta infor
188e0 6d 61 74 69 6f 6e 20 69 73 20 72 65 74 75 72 6e  mation is return
188f0 65 64 20 62 79 20 77 72 69 74 69 6e 67 20 74 6f  ed by writing to
18900 20 74 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61   the memory loca
18910 74 69 6f 6e 73 20 70 61 73 73 65 64 20 61 73 0a  tions passed as.
18920 2a 2a 20 74 68 65 20 35 74 68 20 61 6e 64 20 73  ** the 5th and s
18930 75 62 73 65 71 75 65 6e 74 20 70 61 72 61 6d 65  ubsequent parame
18940 74 65 72 73 20 74 6f 20 74 68 69 73 20 66 75 6e  ters to this fun
18950 63 74 69 6f 6e 2e 20 41 6e 79 20 6f 66 20 74 68  ction. Any of th
18960 65 73 65 20 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  ese .** argument
18970 73 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2c 20 69  s may be NULL, i
18980 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65  n which case the
18990 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 65   corresponding e
189a0 6c 65 6d 65 6e 74 20 6f 66 20 6d 65 74 61 20 0a  lement of meta .
189b0 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  ** information i
189c0 73 20 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a  s ommitted..**.*
189d0 2a 20 3c 70 72 65 3e 0a 2a 2a 20 50 61 72 61 6d  * <pre>.** Param
189e0 65 74 65 72 20 20 20 20 20 4f 75 74 70 75 74 20  eter     Output 
189f0 54 79 70 65 20 20 20 20 20 20 44 65 73 63 72 69  Type      Descri
18a00 70 74 69 6f 6e 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d  ption.** -------
18a10 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
18a20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a  ------------.**.
18a30 2a 2a 20 20 20 35 74 68 20 20 20 20 20 20 20 20  **   5th        
18a40 20 63 6f 6e 73 74 20 63 68 61 72 2a 20 20 20 20   const char*    
18a50 20 20 44 61 74 61 20 74 79 70 65 0a 2a 2a 20 20    Data type.**  
18a60 20 36 74 68 20 20 20 20 20 20 20 20 20 63 6f 6e   6th         con
18a70 73 74 20 63 68 61 72 2a 20 20 20 20 20 20 4e 61  st char*      Na
18a80 6d 65 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c  me of the defaul
18a90 74 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  t collation sequ
18aa0 65 6e 63 65 20 0a 2a 2a 20 20 20 37 74 68 20 20  ence .**   7th  
18ab0 20 20 20 20 20 20 20 69 6e 74 20 20 20 20 20 20         int      
18ac0 20 20 20 20 20 20 20 20 54 72 75 65 20 69 66 20          True if 
18ad0 74 68 65 20 63 6f 6c 75 6d 6e 20 68 61 73 20 61  the column has a
18ae0 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72   NOT NULL constr
18af0 61 69 6e 74 0a 2a 2a 20 20 20 38 74 68 20 20 20  aint.**   8th   
18b00 20 20 20 20 20 20 69 6e 74 20 20 20 20 20 20 20        int       
18b10 20 20 20 20 20 20 20 54 72 75 65 20 69 66 20 74         True if t
18b20 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 70 61 72  he column is par
18b30 74 20 6f 66 20 74 68 65 20 50 52 49 4d 41 52 59  t of the PRIMARY
18b40 20 4b 45 59 0a 2a 2a 20 20 20 39 74 68 20 20 20   KEY.**   9th   
18b50 20 20 20 20 20 20 69 6e 74 20 20 20 20 20 20 20        int       
18b60 20 20 20 20 20 20 20 54 72 75 65 20 69 66 20 74         True if t
18b70 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 41 55 54  he column is AUT
18b80 4f 49 4e 43 52 45 4d 45 4e 54 0a 2a 2a 20 3c 2f  OINCREMENT.** </
18b90 70 72 65 3e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  pre>.**.**.** Th
18ba0 65 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 64  e memory pointed
18bb0 20 74 6f 20 62 79 20 74 68 65 20 63 68 61 72 61   to by the chara
18bc0 63 74 65 72 20 70 6f 69 6e 74 65 72 73 20 72 65  cter pointers re
18bd0 74 75 72 6e 65 64 20 66 6f 72 20 74 68 65 20 0a  turned for the .
18be0 2a 2a 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74  ** declaration t
18bf0 79 70 65 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6f  ype and collatio
18c00 6e 20 73 65 71 75 65 6e 63 65 20 69 73 20 76 61  n sequence is va
18c10 6c 69 64 20 6f 6e 6c 79 20 75 6e 74 69 6c 20 74  lid only until t
18c20 68 65 20 6e 65 78 74 20 0a 2a 2a 20 63 61 6c 6c  he next .** call
18c30 20 74 6f 20 61 6e 79 20 73 71 6c 69 74 65 20 41   to any sqlite A
18c40 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a  PI function..**.
18c50 2a 2a 20 49 66 20 74 68 65 20 73 70 65 63 69 66  ** If the specif
18c60 69 65 64 20 74 61 62 6c 65 20 69 73 20 61 63 74  ied table is act
18c70 75 61 6c 6c 79 20 61 20 76 69 65 77 2c 20 74 68  ually a view, th
18c80 65 6e 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72  en an error is r
18c90 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  eturned..**.** I
18ca0 66 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  f the specified 
18cb0 63 6f 6c 75 6d 6e 20 69 73 20 22 72 6f 77 69 64  column is "rowid
18cc0 22 2c 20 22 6f 69 64 22 20 6f 72 20 22 5f 72 6f  ", "oid" or "_ro
18cd0 77 69 64 5f 22 20 61 6e 64 20 61 6e 20 0a 2a 2a  wid_" and an .**
18ce0 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
18cf0 20 4b 45 59 20 63 6f 6c 75 6d 6e 20 68 61 73 20   KEY column has 
18d00 62 65 65 6e 20 65 78 70 6c 69 63 69 74 6c 79 20  been explicitly 
18d10 64 65 63 6c 61 72 65 64 2c 20 74 68 65 6e 20 74  declared, then t
18d20 68 65 20 6f 75 74 70 75 74 20 0a 2a 2a 20 70 61  he output .** pa
18d30 72 61 6d 65 74 65 72 73 20 61 72 65 20 73 65 74  rameters are set
18d40 20 66 6f 72 20 74 68 65 20 65 78 70 6c 69 63 69   for the explici
18d50 74 6c 79 20 64 65 63 6c 61 72 65 64 20 63 6f 6c  tly declared col
18d60 75 6d 6e 2e 20 49 66 20 74 68 65 72 65 20 69 73  umn. If there is
18d70 20 6e 6f 0a 2a 2a 20 65 78 70 6c 69 63 69 74 6c   no.** explicitl
18d80 79 20 64 65 63 6c 61 72 65 64 20 49 50 4b 20 63  y declared IPK c
18d90 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 74 68 65 20  olumn, then the 
18da0 6f 75 74 70 75 74 20 70 61 72 61 6d 65 74 65 72  output parameter
18db0 73 20 61 72 65 20 73 65 74 20 61 73 20 0a 2a 2a  s are set as .**
18dc0 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20   follows:.**.** 
18dd0 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20 64 61 74  <pre>.**     dat
18de0 61 20 74 79 70 65 3a 20 22 49 4e 54 45 47 45 52  a type: "INTEGER
18df0 22 0a 2a 2a 20 20 20 20 20 63 6f 6c 6c 61 74 69  ".**     collati
18e00 6f 6e 20 73 65 71 75 65 6e 63 65 3a 20 22 42 49  on sequence: "BI
18e10 4e 41 52 59 22 0a 2a 2a 20 20 20 20 20 6e 6f 74  NARY".**     not
18e20 20 6e 75 6c 6c 3a 20 30 0a 2a 2a 20 20 20 20 20   null: 0.**     
18e30 70 72 69 6d 61 72 79 20 6b 65 79 3a 20 31 0a 2a  primary key: 1.*
18e40 2a 20 20 20 20 20 61 75 74 6f 20 69 6e 63 72 65  *     auto incre
18e50 6d 65 6e 74 3a 20 30 0a 2a 2a 20 3c 2f 70 72 65  ment: 0.** </pre
18e60 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  >.**.** This fun
18e70 63 74 69 6f 6e 20 6d 61 79 20 6c 6f 61 64 20 6f  ction may load o
18e80 6e 65 20 6f 72 20 6d 6f 72 65 20 73 63 68 65 6d  ne or more schem
18e90 61 73 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65  as from database
18ea0 20 66 69 6c 65 73 2e 20 49 66 20 61 6e 0a 2a 2a   files. If an.**
18eb0 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75   error occurs du
18ec0 72 69 6e 67 20 74 68 69 73 20 70 72 6f 63 65 73  ring this proces
18ed0 73 2c 20 6f 72 20 69 66 20 74 68 65 20 72 65 71  s, or if the req
18ee0 75 65 73 74 65 64 20 74 61 62 6c 65 20 6f 72 20  uested table or 
18ef0 63 6f 6c 75 6d 6e 0a 2a 2a 20 63 61 6e 6e 6f 74  column.** cannot
18f00 20 62 65 20 66 6f 75 6e 64 2c 20 61 6e 20 53 51   be found, an SQ
18f10 4c 49 54 45 20 65 72 72 6f 72 20 63 6f 64 65 20  LITE error code 
18f20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
18f30 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
18f40 0a 2a 2a 20 6c 65 66 74 20 69 6e 20 74 68 65 20  .** left in the 
18f50 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  database handle 
18f60 28 74 6f 20 62 65 20 72 65 74 72 69 65 76 65 64  (to be retrieved
18f70 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 65   using sqlite3_e
18f80 72 72 6d 73 67 28 29 29 2e 0a 2a 2a 0a 2a 2a 20  rrmsg())..**.** 
18f90 54 68 69 73 20 41 50 49 20 69 73 20 6f 6e 6c 79  This API is only
18fa0 20 61 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68   available if th
18fb0 65 20 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f  e library was co
18fc0 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a  mpiled with the.
18fd0 2a 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ** SQLITE_ENABLE
18fe0 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41  _COLUMN_METADATA
18ff0 20 70 72 65 70 72 6f 63 65 73 73 6f 72 20 73 79   preprocessor sy
19000 6d 62 6f 6c 20 64 65 66 69 6e 65 64 2e 0a 2a 2f  mbol defined..*/
19010 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 61 62  .int sqlite3_tab
19020 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61  le_column_metada
19030 74 61 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  ta(.  sqlite3 *d
19040 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b,              
19050 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20    /* Connection 
19060 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73  handle */.  cons
19070 74 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c  t char *zDbName,
19080 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
19090 61 73 65 20 6e 61 6d 65 20 6f 72 20 4e 55 4c 4c  ase name or NULL
190a0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
190b0 20 2a 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 20 20   *zTableName,   
190c0 20 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20    /* Table name 
190d0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
190e0 2a 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 2c 20 20 20  *zColumnName,   
190f0 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 20   /* Column name 
19100 2a 2f 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20  */.  char const 
19110 2a 2a 70 7a 44 61 74 61 54 79 70 65 2c 20 20 20  **pzDataType,   
19120 20 2f 2a 20 4f 55 54 50 55 54 3a 20 44 65 63 6c   /* OUTPUT: Decl
19130 61 72 65 64 20 64 61 74 61 20 74 79 70 65 20 2a  ared data type *
19140 2f 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  /.  char const *
19150 2a 70 7a 43 6f 6c 6c 53 65 71 2c 20 20 20 20 20  *pzCollSeq,     
19160 2f 2a 20 4f 55 54 50 55 54 3a 20 43 6f 6c 6c 61  /* OUTPUT: Colla
19170 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61  tion sequence na
19180 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4e 6f  me */.  int *pNo
19190 74 4e 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 20  tNull,          
191a0 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54      /* OUTPUT: T
191b0 72 75 65 20 69 66 20 4e 4f 54 20 4e 55 4c 4c 20  rue if NOT NULL 
191c0 63 6f 6e 73 74 72 61 69 6e 74 20 65 78 69 73 74  constraint exist
191d0 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 50 72 69  s */.  int *pPri
191e0 6d 61 72 79 4b 65 79 2c 20 20 20 20 20 20 20 20  maryKey,        
191f0 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72     /* OUTPUT: Tr
19200 75 65 20 69 66 20 63 6f 6c 75 6d 6e 20 70 61 72  ue if column par
19210 74 20 6f 66 20 50 4b 20 2a 2f 0a 20 20 69 6e 74  t of PK */.  int
19220 20 2a 70 41 75 74 6f 69 6e 63 20 20 20 20 20 20   *pAutoinc      
19230 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 50           /* OUTP
19240 55 54 3a 20 54 72 75 65 20 69 66 20 63 6f 6c 75  UT: True if colu
19250 6d 73 20 69 73 20 61 75 74 6f 2d 69 6e 63 72 65  ms is auto-incre
19260 6d 65 6e 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a  ment */.);../*.*
19270 2a 20 43 41 50 49 33 52 45 46 3a 20 4c 6f 61 64  * CAPI3REF: Load
19280 20 41 6e 20 45 78 74 65 6e 73 69 6f 6e 0a 2a 2a   An Extension.**
19290 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6c  .** Attempt to l
192a0 6f 61 64 20 61 6e 20 53 51 4c 69 74 65 20 65 78  oad an SQLite ex
192b0 74 65 6e 73 69 6f 6e 20 6c 69 62 72 61 72 79 20  tension library 
192c0 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65  contained in the
192d0 20 66 69 6c 65 0a 2a 2a 20 7a 46 69 6c 65 2e 20   file.** zFile. 
192e0 20 54 68 65 20 65 6e 74 72 79 20 70 6f 69 6e 74   The entry point
192f0 20 69 73 20 7a 50 72 6f 63 2e 20 20 7a 50 72 6f   is zProc.  zPro
19300 63 20 6d 61 79 20 62 65 20 30 20 69 6e 20 77 68  c may be 0 in wh
19310 69 63 68 20 63 61 73 65 20 74 68 65 0a 2a 2a 20  ich case the.** 
19320 6e 61 6d 65 20 6f 66 20 74 68 65 20 65 6e 74 72  name of the entr
19330 79 20 70 6f 69 6e 74 20 64 65 66 61 75 6c 74 73  y point defaults
19340 20 74 6f 20 22 73 71 6c 69 74 65 33 5f 65 78 74   to "sqlite3_ext
19350 65 6e 73 69 6f 6e 5f 69 6e 69 74 22 2e 0a 2a 2a  ension_init"..**
19360 0a 2a 2a 20 52 65 74 75 72 6e 20 5b 53 51 4c 49  .** Return [SQLI
19370 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73  TE_OK] on succes
19380 73 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 45 52  s and [SQLITE_ER
19390 52 4f 52 5d 20 69 66 20 73 6f 6d 65 74 68 69 6e  ROR] if somethin
193a0 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a  g goes wrong..**
193b0 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20  .** If an error 
193c0 6f 63 63 75 72 73 20 61 6e 64 20 70 7a 45 72 72  occurs and pzErr
193d0 4d 73 67 20 69 73 20 6e 6f 74 20 30 2c 20 74 68  Msg is not 0, th
193e0 65 6e 20 66 69 6c 6c 20 2a 70 7a 45 72 72 4d 73  en fill *pzErrMs
193f0 67 20 77 69 74 68 20 0a 2a 2a 20 65 72 72 6f 72  g with .** error
19400 20 6d 65 73 73 61 67 65 20 74 65 78 74 2e 20 20   message text.  
19410 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63  The calling func
19420 74 69 6f 6e 20 73 68 6f 75 6c 64 20 66 72 65 65  tion should free
19430 20 74 68 69 73 20 6d 65 6d 6f 72 79 0a 2a 2a 20   this memory.** 
19440 62 79 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69  by calling [sqli
19450 74 65 33 5f 66 72 65 65 28 29 5d 2e 0a 2a 2a 0a  te3_free()]..**.
19460 2a 2a 20 45 78 74 65 6e 73 69 6f 6e 20 6c 6f 61  ** Extension loa
19470 64 69 6e 67 20 6d 75 73 74 20 62 65 20 65 6e 61  ding must be ena
19480 62 6c 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69  bled using [sqli
19490 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f  te3_enable_load_
194a0 65 78 74 65 6e 73 69 6f 6e 28 29 5d 0a 2a 2a 20  extension()].** 
194b0 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67  prior to calling
194c0 20 74 68 69 73 20 41 50 49 20 6f 72 20 61 6e 20   this API or an 
194d0 65 72 72 6f 72 20 77 69 6c 6c 20 62 65 20 72 65  error will be re
194e0 74 75 72 6e 65 64 2e 0a 2a 2f 0a 69 6e 74 20 73  turned..*/.int s
194f0 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65  qlite3_load_exte
19500 6e 73 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33  nsion(.  sqlite3
19510 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 2f   *db,          /
19520 2a 20 4c 6f 61 64 20 74 68 65 20 65 78 74 65 6e  * Load the exten
19530 73 69 6f 6e 20 69 6e 74 6f 20 74 68 69 73 20 64  sion into this d
19540 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
19550 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  on */.  const ch
19560 61 72 20 2a 7a 46 69 6c 65 2c 20 20 20 20 2f 2a  ar *zFile,    /*
19570 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 73 68 61   Name of the sha
19580 72 65 64 20 6c 69 62 72 61 72 79 20 63 6f 6e 74  red library cont
19590 61 69 6e 69 6e 67 20 65 78 74 65 6e 73 69 6f 6e  aining extension
195a0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
195b0 20 2a 7a 50 72 6f 63 2c 20 20 20 20 2f 2a 20 45   *zProc,    /* E
195c0 6e 74 72 79 20 70 6f 69 6e 74 2e 20 20 44 65 72  ntry point.  Der
195d0 69 76 65 64 20 66 72 6f 6d 20 7a 46 69 6c 65 20  ived from zFile 
195e0 69 66 20 30 20 2a 2f 0a 20 20 63 68 61 72 20 2a  if 0 */.  char *
195f0 2a 70 7a 45 72 72 4d 73 67 20 20 20 20 20 20 20  *pzErrMsg       
19600 2f 2a 20 50 75 74 20 65 72 72 6f 72 20 6d 65 73  /* Put error mes
19610 73 61 67 65 20 68 65 72 65 20 69 66 20 6e 6f 74  sage here if not
19620 20 30 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20   0 */.);../*.** 
19630 43 41 50 49 33 52 45 46 3a 20 20 45 6e 61 62 6c  CAPI3REF:  Enabl
19640 65 20 4f 72 20 44 69 73 61 62 6c 65 20 45 78 74  e Or Disable Ext
19650 65 6e 73 69 6f 6e 20 4c 6f 61 64 69 6e 67 0a 2a  ension Loading.*
19660 2a 0a 2a 2a 20 53 6f 20 61 73 20 6e 6f 74 20 74  *.** So as not t
19670 6f 20 6f 70 65 6e 20 73 65 63 75 72 69 74 79 20  o open security 
19680 68 6f 6c 65 73 20 69 6e 20 6f 6c 64 65 72 20 61  holes in older a
19690 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74  pplications that
196a0 20 61 72 65 0a 2a 2a 20 75 6e 70 72 65 70 61 72   are.** unprepar
196b0 65 64 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20  ed to deal with 
196c0 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e  extension loadin
196d0 67 2c 20 61 6e 64 20 61 73 20 61 20 6d 65 61 6e  g, and as a mean
196e0 73 20 6f 66 20 64 69 73 61 62 6c 69 6e 67 0a 2a  s of disabling.*
196f0 2a 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64  * extension load
19700 69 6e 67 20 77 68 69 6c 65 20 65 76 61 6c 75 61  ing while evalua
19710 74 69 6e 67 20 75 73 65 72 2d 65 6e 74 65 72 65  ting user-entere
19720 64 20 53 51 4c 2c 20 74 68 65 20 66 6f 6c 6c 6f  d SQL, the follo
19730 77 69 6e 67 0a 2a 2a 20 41 50 49 20 69 73 20 70  wing.** API is p
19740 72 6f 76 69 64 65 64 20 74 6f 20 74 75 72 6e 20  rovided to turn 
19750 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c 6f 61  the [sqlite3_loa
19760 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 6d  d_extension()] m
19770 65 63 68 61 6e 69 73 6d 20 6f 6e 20 61 6e 64 0a  echanism on and.
19780 2a 2a 20 6f 66 66 2e 20 20 49 74 20 69 73 20 6f  ** off.  It is o
19790 66 66 20 62 79 20 64 65 66 61 75 6c 74 2e 20 20  ff by default.  
197a0 53 65 65 20 74 69 63 6b 65 74 20 23 31 38 36 33  See ticket #1863
197b0 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 20 74 68 69  ..**.** Call thi
197c0 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20 6f  s routine with o
197d0 6e 6f 66 66 3d 3d 31 20 74 6f 20 74 75 72 6e 20  noff==1 to turn 
197e0 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e  extension loadin
197f0 67 20 6f 6e 0a 2a 2a 20 61 6e 64 20 63 61 6c 6c  g on.** and call
19800 20 69 74 20 77 69 74 68 20 6f 6e 6f 66 66 3d 3d   it with onoff==
19810 30 20 74 6f 20 74 75 72 6e 20 69 74 20 62 61 63  0 to turn it bac
19820 6b 20 6f 66 66 20 61 67 61 69 6e 2e 0a 2a 2f 0a  k off again..*/.
19830 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e 61 62  int sqlite3_enab
19840 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f  le_load_extensio
19850 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69  n(sqlite3 *db, i
19860 6e 74 20 6f 6e 6f 66 66 29 3b 0a 0a 2f 2a 0a 2a  nt onoff);../*.*
19870 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 61 6b 65  * CAPI3REF: Make
19880 20 41 72 72 61 6e 67 65 6d 65 6e 74 73 20 54 6f   Arrangements To
19890 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 4c   Automatically L
198a0 6f 61 64 20 41 6e 20 45 78 74 65 6e 73 69 6f 6e  oad An Extension
198b0 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20  .**.** Register 
198c0 61 6e 20 65 78 74 65 6e 73 69 6f 6e 20 65 6e 74  an extension ent
198d0 72 79 20 70 6f 69 6e 74 20 74 68 61 74 20 69 73  ry point that is
198e0 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 69   automatically i
198f0 6e 76 6f 6b 65 64 0a 2a 2a 20 77 68 65 6e 65 76  nvoked.** whenev
19900 65 72 20 61 20 6e 65 77 20 64 61 74 61 62 61 73  er a new databas
19910 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20  e connection is 
19920 6f 70 65 6e 65 64 20 75 73 69 6e 67 0a 2a 2a 20  opened using.** 
19930 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d  [sqlite3_open()]
19940 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65   or [sqlite3_ope
19950 6e 31 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68  n16()]..**.** Th
19960 69 73 20 41 50 49 20 63 61 6e 20 62 65 20 69 6e  is API can be in
19970 76 6f 6b 65 64 20 61 74 20 70 72 6f 67 72 61 6d  voked at program
19980 20 73 74 61 72 74 75 70 20 69 6e 20 6f 72 64 65   startup in orde
19990 72 20 74 6f 20 72 65 67 69 73 74 65 72 0a 2a 2a  r to register.**
199a0 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 74 61   one or more sta
199b0 74 69 63 61 6c 6c 79 20 6c 69 6e 6b 65 64 20 65  tically linked e
199c0 78 74 65 6e 73 69 6f 6e 73 20 74 68 61 74 20 77  xtensions that w
199d0 69 6c 6c 20 62 65 20 61 76 61 69 6c 61 62 6c 65  ill be available
199e0 0a 2a 2a 20 74 6f 20 61 6c 6c 20 6e 65 77 20 64  .** to all new d
199f0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
19a00 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 44 75 70 6c 69  ons..**.** Dupli
19a10 63 61 74 65 20 65 78 74 65 6e 73 69 6f 6e 73 20  cate extensions 
19a20 61 72 65 20 64 65 74 65 63 74 65 64 20 73 6f 20  are detected so 
19a30 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75  calling this rou
19a40 74 69 6e 65 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a  tine multiple.**
19a50 20 74 69 6d 65 73 20 77 69 74 68 20 74 68 65 20   times with the 
19a60 73 61 6d 65 20 65 78 74 65 6e 73 69 6f 6e 20 69  same extension i
19a70 73 20 68 61 72 6d 6c 65 73 73 2e 0a 2a 2a 0a 2a  s harmless..**.*
19a80 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73  * This routine s
19a90 74 6f 72 65 73 20 61 20 70 6f 69 6e 74 65 72 20  tores a pointer 
19aa0 74 6f 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e  to the extension
19ab0 20 69 6e 20 61 6e 20 61 72 72 61 79 0a 2a 2a 20   in an array.** 
19ac0 74 68 61 74 20 69 73 20 6f 62 74 61 69 6e 65 64  that is obtained
19ad0 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 20   from malloc(). 
19ae0 20 49 66 20 79 6f 75 20 72 75 6e 20 61 20 6d 65   If you run a me
19af0 6d 6f 72 79 20 6c 65 61 6b 0a 2a 2a 20 63 68 65  mory leak.** che
19b00 63 6b 65 72 20 6f 6e 20 79 6f 75 72 20 70 72 6f  cker on your pro
19b10 67 72 61 6d 20 61 6e 64 20 69 74 20 72 65 70 6f  gram and it repo
19b20 72 74 73 20 61 20 6c 65 61 6b 20 62 65 63 61 75  rts a leak becau
19b30 73 65 20 6f 66 20 74 68 69 73 0a 2a 2a 20 61 72  se of this.** ar
19b40 72 61 79 2c 20 74 68 65 6e 20 69 6e 76 6f 6b 65  ray, then invoke
19b50 20 5b 73 71 6c 69 74 65 33 5f 61 75 74 6f 6d 61   [sqlite3_automa
19b60 74 69 63 5f 65 78 74 65 6e 73 69 6f 6e 5f 72 65  tic_extension_re
19b70 73 65 74 28 29 5d 20 70 72 69 6f 72 0a 2a 2a 20  set()] prior.** 
19b80 74 6f 20 73 68 75 74 64 6f 77 6e 20 74 6f 20 66  to shutdown to f
19b90 72 65 65 20 74 68 65 20 6d 65 6d 6f 72 79 2e 0a  ree the memory..
19ba0 2a 2a 0a 2a 2a 20 41 75 74 6f 6d 61 74 69 63 20  **.** Automatic 
19bb0 65 78 74 65 6e 73 69 6f 6e 73 20 61 70 70 6c 79  extensions apply
19bc0 20 61 63 72 6f 73 73 20 61 6c 6c 20 74 68 72 65   across all thre
19bd0 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ads..**.** This 
19be0 69 6e 74 65 72 66 61 63 65 20 69 73 20 65 78 70  interface is exp
19bf0 65 72 69 6d 65 6e 74 61 6c 20 61 6e 64 20 69 73  erimental and is
19c00 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e   subject to chan
19c10 67 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c  ge or.** removal
19c20 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61   in future relea
19c30 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a  ses of SQLite..*
19c40 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 75  /.int sqlite3_au
19c50 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69  to_extension(voi
19c60 64 20 2a 78 45 6e 74 72 79 50 6f 69 6e 74 29 3b  d *xEntryPoint);
19c70 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  .../*.** CAPI3RE
19c80 46 3a 20 52 65 73 65 74 20 41 75 74 6f 6d 61 74  F: Reset Automat
19c90 69 63 20 45 78 74 65 6e 73 69 6f 6e 20 4c 6f 61  ic Extension Loa
19ca0 64 69 6e 67 0a 2a 2a 0a 2a 2a 20 44 69 73 61 62  ding.**.** Disab
19cb0 6c 65 20 61 6c 6c 20 70 72 65 76 69 6f 75 73 6c  le all previousl
19cc0 79 20 72 65 67 69 73 74 65 72 65 64 20 61 75 74  y registered aut
19cd0 6f 6d 61 74 69 63 20 65 78 74 65 6e 73 69 6f 6e  omatic extension
19ce0 73 2e 20 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74  s.  This.** rout
19cf0 69 6e 65 20 75 6e 64 6f 65 73 20 74 68 65 20 65  ine undoes the e
19d00 66 66 65 63 74 20 6f 66 20 61 6c 6c 20 70 72 69  ffect of all pri
19d10 6f 72 20 5b 73 71 6c 69 74 65 33 5f 61 75 74 6f  or [sqlite3_auto
19d20 6d 61 74 69 63 5f 65 78 74 65 6e 73 69 6f 6e 28  matic_extension(
19d30 29 5d 0a 2a 2a 20 63 61 6c 6c 73 2e 0a 2a 2a 0a  )].** calls..**.
19d40 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 64 69 73  ** This call dis
19d50 61 62 6c 65 64 20 61 75 74 6f 6d 61 74 69 63 20  abled automatic 
19d60 65 78 74 65 6e 73 69 6f 6e 73 20 69 6e 20 61 6c  extensions in al
19d70 6c 20 74 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a  l threads..**.**
19d80 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20   This interface 
19d90 69 73 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20  is experimental 
19da0 61 6e 64 20 69 73 20 73 75 62 6a 65 63 74 20 74  and is subject t
19db0 6f 20 63 68 61 6e 67 65 20 6f 72 0a 2a 2a 20 72  o change or.** r
19dc0 65 6d 6f 76 61 6c 20 69 6e 20 66 75 74 75 72 65  emoval in future
19dd0 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c   releases of SQL
19de0 69 74 65 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c  ite..*/.void sql
19df0 69 74 65 33 5f 72 65 73 65 74 5f 61 75 74 6f 5f  ite3_reset_auto_
19e00 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64 29 3b  extension(void);
19e10 0a 0a 0a 2f 2a 0a 2a 2a 2a 2a 2a 2a 20 45 58 50  .../*.****** EXP
19e20 45 52 49 4d 45 4e 54 41 4c 20 2d 20 73 75 62 6a  ERIMENTAL - subj
19e30 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 77 69  ect to change wi
19e40 74 68 6f 75 74 20 6e 6f 74 69 63 65 20 2a 2a 2a  thout notice ***
19e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
19e60 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20  * The interface 
19e70 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74  to the virtual-t
19e80 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d 20 69  able mechanism i
19e90 73 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73  s currently cons
19ea0 69 64 65 72 65 64 0a 2a 2a 20 74 6f 20 62 65 20  idered.** to be 
19eb0 65 78 70 65 72 69 6d 65 6e 74 61 6c 2e 20 20 54  experimental.  T
19ec0 68 65 20 69 6e 74 65 72 66 61 63 65 20 6d 69 67  he interface mig
19ed0 68 74 20 63 68 61 6e 67 65 20 69 6e 20 69 6e 63  ht change in inc
19ee0 6f 6d 70 61 74 69 62 6c 65 20 77 61 79 73 2e 0a  ompatible ways..
19ef0 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20  ** If this is a 
19f00 70 72 6f 62 6c 65 6d 20 66 6f 72 20 79 6f 75 2c  problem for you,
19f10 20 64 6f 20 6e 6f 74 20 75 73 65 20 74 68 65 20   do not use the 
19f20 69 6e 74 65 72 66 61 63 65 20 61 74 20 74 68 69  interface at thi
19f30 73 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 57 68  s time..**.** Wh
19f40 65 6e 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74  en the virtual-t
19f50 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d 20 73  able mechanism s
19f60 74 61 62 6c 69 7a 65 73 2c 20 77 65 20 77 69 6c  tablizes, we wil
19f70 6c 20 64 65 63 6c 61 72 65 20 74 68 65 0a 2a 2a  l declare the.**
19f80 20 69 6e 74 65 72 66 61 63 65 20 66 69 78 65 64   interface fixed
19f90 2c 20 73 75 70 70 6f 72 74 20 69 74 20 69 6e 64  , support it ind
19fa0 65 66 69 6e 69 74 65 6c 79 2c 20 61 6e 64 20 72  efinitely, and r
19fb0 65 6d 6f 76 65 20 74 68 69 73 20 63 6f 6d 6d 65  emove this comme
19fc0 6e 74 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 74  nt..*/../*.** St
19fd0 72 75 63 74 75 72 65 73 20 75 73 65 64 20 62 79  ructures used by
19fe0 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
19ff0 6c 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2f 0a  le interface.*/.
1a000 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73  typedef struct s
1a010 71 6c 69 74 65 33 5f 76 74 61 62 20 73 71 6c 69  qlite3_vtab sqli
1a020 74 65 33 5f 76 74 61 62 3b 0a 74 79 70 65 64 65  te3_vtab;.typede
1a030 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  f struct sqlite3
1a040 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 71 6c 69  _index_info sqli
1a050 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 3b 0a  te3_index_info;.
1a060 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73  typedef struct s
1a070 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
1a080 6f 72 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  or sqlite3_vtab_
1a090 63 75 72 73 6f 72 3b 0a 74 79 70 65 64 65 66 20  cursor;.typedef 
1a0a0 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d  struct sqlite3_m
1a0b0 6f 64 75 6c 65 20 73 71 6c 69 74 65 33 5f 6d 6f  odule sqlite3_mo
1a0c0 64 75 6c 65 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d  dule;../*.** A m
1a0d0 6f 64 75 6c 65 20 69 73 20 61 20 63 6c 61 73 73  odule is a class
1a0e0 20 6f 66 20 76 69 72 74 75 61 6c 20 74 61 62 6c   of virtual tabl
1a0f0 65 73 2e 20 20 45 61 63 68 20 6d 6f 64 75 6c 65  es.  Each module
1a100 20 69 73 20 64 65 66 69 6e 65 64 0a 2a 2a 20 62   is defined.** b
1a110 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  y an instance of
1a120 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
1a130 74 72 75 63 74 75 72 65 2e 20 20 54 68 69 73 20  tructure.  This 
1a140 73 74 72 75 63 74 75 72 65 20 63 6f 6e 73 69 73  structure consis
1a150 74 73 0a 2a 2a 20 6d 6f 73 74 6c 79 20 6f 66 20  ts.** mostly of 
1a160 6d 65 74 68 6f 64 73 20 66 6f 72 20 74 68 65 20  methods for the 
1a170 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63  module..*/.struc
1a180 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  t sqlite3_module
1a190 20 7b 0a 20 20 69 6e 74 20 69 56 65 72 73 69 6f   {.  int iVersio
1a1a0 6e 3b 0a 20 20 69 6e 74 20 28 2a 78 43 72 65 61  n;.  int (*xCrea
1a1b0 74 65 29 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f  te)(sqlite3*, vo
1a1c0 69 64 20 2a 70 41 75 78 2c 0a 20 20 20 20 20 20  id *pAux,.      
1a1d0 20 20 20 20 20 20 20 20 20 69 6e 74 20 61 72 67           int arg
1a1e0 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63  c, const char *c
1a1f0 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20 20 20 20  onst*argv,.     
1a200 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1a210 33 5f 76 74 61 62 20 2a 2a 70 70 56 54 61 62 2c  3_vtab **ppVTab,
1a220 20 63 68 61 72 2a 2a 29 3b 0a 20 20 69 6e 74 20   char**);.  int 
1a230 28 2a 78 43 6f 6e 6e 65 63 74 29 28 73 71 6c 69  (*xConnect)(sqli
1a240 74 65 33 2a 2c 20 76 6f 69 64 20 2a 70 41 75 78  te3*, void *pAux
1a250 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1a260 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74   int argc, const
1a270 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67   char *const*arg
1a280 76 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  v,.             
1a290 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
1a2a0 2a 70 70 56 54 61 62 2c 20 63 68 61 72 2a 2a 29  *ppVTab, char**)
1a2b0 3b 0a 20 20 69 6e 74 20 28 2a 78 42 65 73 74 49  ;.  int (*xBestI
1a2c0 6e 64 65 78 29 28 73 71 6c 69 74 65 33 5f 76 74  ndex)(sqlite3_vt
1a2d0 61 62 20 2a 70 56 54 61 62 2c 20 73 71 6c 69 74  ab *pVTab, sqlit
1a2e0 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 2a 29 3b  e3_index_info*);
1a2f0 0a 20 20 69 6e 74 20 28 2a 78 44 69 73 63 6f 6e  .  int (*xDiscon
1a300 6e 65 63 74 29 28 73 71 6c 69 74 65 33 5f 76 74  nect)(sqlite3_vt
1a310 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e  ab *pVTab);.  in
1a320 74 20 28 2a 78 44 65 73 74 72 6f 79 29 28 73 71  t (*xDestroy)(sq
1a330 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61  lite3_vtab *pVTa
1a340 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4f 70 65  b);.  int (*xOpe
1a350 6e 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  n)(sqlite3_vtab 
1a360 2a 70 56 54 61 62 2c 20 73 71 6c 69 74 65 33 5f  *pVTab, sqlite3_
1a370 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 2a 70 70  vtab_cursor **pp
1a380 43 75 72 73 6f 72 29 3b 0a 20 20 69 6e 74 20 28  Cursor);.  int (
1a390 2a 78 43 6c 6f 73 65 29 28 73 71 6c 69 74 65 33  *xClose)(sqlite3
1a3a0 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a 29 3b 0a  _vtab_cursor*);.
1a3b0 20 20 69 6e 74 20 28 2a 78 46 69 6c 74 65 72 29    int (*xFilter)
1a3c0 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75  (sqlite3_vtab_cu
1a3d0 72 73 6f 72 2a 2c 20 69 6e 74 20 69 64 78 4e 75  rsor*, int idxNu
1a3e0 6d 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69  m, const char *i
1a3f0 64 78 53 74 72 2c 0a 20 20 20 20 20 20 20 20 20  dxStr,.         
1a400 20 20 20 20 20 20 20 69 6e 74 20 61 72 67 63 2c         int argc,
1a410 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
1a420 2a 61 72 67 76 29 3b 0a 20 20 69 6e 74 20 28 2a  *argv);.  int (*
1a430 78 4e 65 78 74 29 28 73 71 6c 69 74 65 33 5f 76  xNext)(sqlite3_v
1a440 74 61 62 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20  tab_cursor*);.  
1a450 69 6e 74 20 28 2a 78 45 6f 66 29 28 73 71 6c 69  int (*xEof)(sqli
1a460 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a  te3_vtab_cursor*
1a470 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6c 75  );.  int (*xColu
1a480 6d 6e 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62  mn)(sqlite3_vtab
1a490 5f 63 75 72 73 6f 72 2a 2c 20 73 71 6c 69 74 65  _cursor*, sqlite
1a4a0 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 29  3_context*, int)
1a4b0 3b 0a 20 20 69 6e 74 20 28 2a 78 52 6f 77 69 64  ;.  int (*xRowid
1a4c0 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  )(sqlite3_vtab_c
1a4d0 75 72 73 6f 72 2a 2c 20 73 71 6c 69 74 65 5f 69  ursor*, sqlite_i
1a4e0 6e 74 36 34 20 2a 70 52 6f 77 69 64 29 3b 0a 20  nt64 *pRowid);. 
1a4f0 20 69 6e 74 20 28 2a 78 55 70 64 61 74 65 29 28   int (*xUpdate)(
1a500 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2c 20  sqlite3_vtab *, 
1a510 69 6e 74 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  int, sqlite3_val
1a520 75 65 20 2a 2a 2c 20 73 71 6c 69 74 65 5f 69 6e  ue **, sqlite_in
1a530 74 36 34 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  t64 *);.  int (*
1a540 78 42 65 67 69 6e 29 28 73 71 6c 69 74 65 33 5f  xBegin)(sqlite3_
1a550 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20  vtab *pVTab);.  
1a560 69 6e 74 20 28 2a 78 53 79 6e 63 29 28 73 71 6c  int (*xSync)(sql
1a570 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62  ite3_vtab *pVTab
1a580 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6d 6d  );.  int (*xComm
1a590 69 74 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62  it)(sqlite3_vtab
1a5a0 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20   *pVTab);.  int 
1a5b0 28 2a 78 52 6f 6c 6c 62 61 63 6b 29 28 73 71 6c  (*xRollback)(sql
1a5c0 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62  ite3_vtab *pVTab
1a5d0 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46 69 6e 64  );.  int (*xFind
1a5e0 46 75 6e 63 74 69 6f 6e 29 28 73 71 6c 69 74 65  Function)(sqlite
1a5f0 33 5f 76 74 61 62 20 2a 70 56 74 61 62 2c 20 69  3_vtab *pVtab, i
1a600 6e 74 20 6e 41 72 67 2c 20 63 6f 6e 73 74 20 63  nt nArg, const c
1a610 68 61 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 20 20  har *zName,.    
1a620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a630 20 20 20 76 6f 69 64 20 28 2a 2a 70 78 46 75 6e     void (**pxFun
1a640 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  c)(sqlite3_conte
1a650 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  xt*,int,sqlite3_
1a660 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 20 20 20 20  value**),.      
1a670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a680 20 76 6f 69 64 20 2a 2a 70 70 41 72 67 29 3b 0a   void **ppArg);.
1a690 0a 20 20 69 6e 74 20 28 2a 78 52 65 6e 61 6d 65  .  int (*xRename
1a6a0 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  )(sqlite3_vtab *
1a6b0 70 56 74 61 62 2c 20 63 6f 6e 73 74 20 63 68 61  pVtab, const cha
1a6c0 72 20 2a 7a 4e 65 77 29 3b 0a 7d 3b 0a 0a 2f 2a  r *zNew);.};../*
1a6d0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
1a6e0 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63  index_info struc
1a6f0 74 75 72 65 20 61 6e 64 20 69 74 73 20 73 75 62  ture and its sub
1a700 73 74 72 75 63 74 75 72 65 73 20 69 73 20 75 73  structures is us
1a710 65 64 20 74 6f 0a 2a 2a 20 70 61 73 73 20 69 6e  ed to.** pass in
1a720 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 74 6f 20 61  formation into a
1a730 6e 64 20 72 65 63 65 69 76 65 20 74 68 65 20 72  nd receive the r
1a740 65 70 6c 79 20 66 72 6f 6d 20 74 68 65 20 78 42  eply from the xB
1a750 65 73 74 49 6e 64 65 78 0a 2a 2a 20 6d 65 74 68  estIndex.** meth
1a760 6f 64 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 33  od of an sqlite3
1a770 5f 6d 6f 64 75 6c 65 2e 20 20 54 68 65 20 66 69  _module.  The fi
1a780 65 6c 64 73 20 75 6e 64 65 72 20 2a 2a 49 6e 70  elds under **Inp
1a790 75 74 73 2a 2a 20 61 72 65 20 74 68 65 0a 2a 2a  uts** are the.**
1a7a0 20 69 6e 70 75 74 73 20 74 6f 20 78 42 65 73 74   inputs to xBest
1a7b0 49 6e 64 65 78 20 61 6e 64 20 61 72 65 20 72 65  Index and are re
1a7c0 61 64 2d 6f 6e 6c 79 2e 20 20 78 42 65 73 74 49  ad-only.  xBestI
1a7d0 6e 64 65 78 20 69 6e 73 65 72 74 73 20 69 74 73  ndex inserts its
1a7e0 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69 6e 74 6f  .** results into
1a7f0 20 74 68 65 20 2a 2a 4f 75 74 70 75 74 73 2a 2a   the **Outputs**
1a800 20 66 69 65 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20 54   fields..**.** T
1a810 68 65 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d  he aConstraint[]
1a820 20 61 72 72 61 79 20 72 65 63 6f 72 64 73 20 57   array records W
1a830 48 45 52 45 20 63 6c 61 75 73 65 20 63 6f 6e 73  HERE clause cons
1a840 74 72 61 69 6e 74 73 20 6f 66 20 74 68 65 0a 2a  traints of the.*
1a850 2a 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20  * form:.**.**   
1a860 20 20 20 20 20 20 63 6f 6c 75 6d 6e 20 4f 50 20        column OP 
1a870 65 78 70 72 0a 2a 2a 0a 2a 2a 20 57 68 65 72 65  expr.**.** Where
1a880 20 4f 50 20 69 73 20 3d 2c 20 3c 2c 20 3c 3d 2c   OP is =, <, <=,
1a890 20 3e 2c 20 6f 72 20 3e 3d 2e 20 20 54 68 65 20   >, or >=.  The 
1a8a0 70 61 72 74 69 63 75 6c 61 72 20 6f 70 65 72 61  particular opera
1a8b0 74 6f 72 20 69 73 20 73 74 6f 72 65 64 0a 2a 2a  tor is stored.**
1a8c0 20 69 6e 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b   in aConstraint[
1a8d0 5d 2e 6f 70 2e 20 20 54 68 65 20 69 6e 64 65 78  ].op.  The index
1a8e0 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69   of the column i
1a8f0 73 20 73 74 6f 72 65 64 20 69 6e 20 0a 2a 2a 20  s stored in .** 
1a900 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e 69 43  aConstraint[].iC
1a910 6f 6c 75 6d 6e 2e 20 20 61 43 6f 6e 73 74 72 61  olumn.  aConstra
1a920 69 6e 74 5b 5d 2e 75 73 61 62 6c 65 20 69 73 20  int[].usable is 
1a930 54 52 55 45 20 69 66 20 74 68 65 0a 2a 2a 20 65  TRUE if the.** e
1a940 78 70 72 20 6f 6e 20 74 68 65 20 72 69 67 68 74  xpr on the right
1a950 2d 68 61 6e 64 20 73 69 64 65 20 63 61 6e 20 62  -hand side can b
1a960 65 20 65 76 61 6c 75 61 74 65 64 20 28 61 6e 64  e evaluated (and
1a970 20 74 68 75 73 20 74 68 65 20 63 6f 6e 73 74 72   thus the constr
1a980 61 69 6e 74 0a 2a 2a 20 69 73 20 75 73 61 62 6c  aint.** is usabl
1a990 65 29 20 61 6e 64 20 66 61 6c 73 65 20 69 66 20  e) and false if 
1a9a0 69 74 20 63 61 6e 6e 6f 74 2e 0a 2a 2a 0a 2a 2a  it cannot..**.**
1a9b0 20 54 68 65 20 6f 70 74 69 6d 69 7a 65 72 20 61   The optimizer a
1a9c0 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 69 6e 76  utomatically inv
1a9d0 65 72 74 73 20 74 65 72 6d 73 20 6f 66 20 74 68  erts terms of th
1a9e0 65 20 66 6f 72 6d 20 22 65 78 70 72 20 4f 50 20  e form "expr OP 
1a9f0 63 6f 6c 75 6d 6e 22 0a 2a 2a 20 61 6e 64 20 6d  column".** and m
1aa00 61 6b 65 73 20 6f 74 68 65 72 20 73 69 6d 70 6c  akes other simpl
1aa10 69 66 69 63 61 74 69 6e 6f 73 20 74 6f 20 74 68  ificatinos to th
1aa20 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 69  e WHERE clause i
1aa30 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a  n an attempt to.
1aa40 2a 2a 20 67 65 74 20 61 73 20 6d 61 6e 79 20 57  ** get as many W
1aa50 48 45 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d  HERE clause term
1aa60 73 20 69 6e 74 6f 20 74 68 65 20 66 6f 72 6d 20  s into the form 
1aa70 73 68 6f 77 6e 20 61 62 6f 76 65 20 61 73 20 70  shown above as p
1aa80 6f 73 73 69 62 6c 65 2e 0a 2a 2a 20 54 68 65 20  ossible..** The 
1aa90 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 61 72  aConstraint[] ar
1aaa0 72 61 79 20 6f 6e 6c 79 20 72 65 70 6f 72 74 73  ray only reports
1aab0 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65   WHERE clause te
1aac0 72 6d 73 20 69 6e 20 74 68 65 20 63 6f 72 72 65  rms in the corre
1aad0 63 74 0a 2a 2a 20 66 6f 72 6d 20 74 68 61 74 20  ct.** form that 
1aae0 72 65 66 65 72 20 74 6f 20 74 68 65 20 70 61 72  refer to the par
1aaf0 74 69 63 75 6c 61 72 20 76 69 72 74 75 61 6c 20  ticular virtual 
1ab00 74 61 62 6c 65 20 62 65 69 6e 67 20 71 75 65 72  table being quer
1ab10 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 66 6f 72  ied..**.** Infor
1ab20 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65  mation about the
1ab30 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
1ab40 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 4f   is stored in aO
1ab50 72 64 65 72 42 79 5b 5d 2e 0a 2a 2a 20 45 61 63  rderBy[]..** Eac
1ab60 68 20 74 65 72 6d 20 6f 66 20 61 4f 72 64 65 72  h term of aOrder
1ab70 42 79 20 72 65 63 6f 72 64 73 20 61 20 63 6f 6c  By records a col
1ab80 75 6d 6e 20 6f 66 20 74 68 65 20 4f 52 44 45 52  umn of the ORDER
1ab90 20 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a   BY clause..**.*
1aba0 2a 20 54 68 65 20 78 42 65 73 74 49 6e 64 65 78  * The xBestIndex
1abb0 20 6d 65 74 68 6f 64 20 6d 75 73 74 20 66 69 6c   method must fil
1abc0 6c 20 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61  l aConstraintUsa
1abd0 67 65 5b 5d 20 77 69 74 68 20 69 6e 66 6f 72 6d  ge[] with inform
1abe0 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 77  ation.** about w
1abf0 68 61 74 20 70 61 72 61 6d 65 74 65 72 73 20 74  hat parameters t
1ac00 6f 20 70 61 73 73 20 74 6f 20 78 46 69 6c 74 65  o pass to xFilte
1ac10 72 2e 20 20 49 66 20 61 72 67 76 49 6e 64 65 78  r.  If argvIndex
1ac20 3e 30 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 72  >0 then.** the r
1ac30 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 6f  ight-hand side o
1ac40 66 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  f the correspond
1ac50 69 6e 67 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b  ing aConstraint[
1ac60 5d 20 69 73 20 65 76 61 6c 75 61 74 65 64 0a 2a  ] is evaluated.*
1ac70 2a 20 61 6e 64 20 62 65 63 6f 6d 65 73 20 74 68  * and becomes th
1ac80 65 20 61 72 67 76 49 6e 64 65 78 2d 74 68 20 65  e argvIndex-th e
1ac90 6e 74 72 79 20 69 6e 20 61 72 67 76 2e 20 20 49  ntry in argv.  I
1aca0 66 20 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61  f aConstraintUsa
1acb0 67 65 5b 5d 2e 6f 6d 69 74 0a 2a 2a 20 69 73 20  ge[].omit.** is 
1acc0 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 63  true, then the c
1acd0 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 61 73 73  onstraint is ass
1ace0 75 6d 65 64 20 74 6f 20 62 65 20 66 75 6c 6c 79  umed to be fully
1acf0 20 68 61 6e 64 6c 65 64 20 62 79 20 74 68 65 0a   handled by the.
1ad00 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ** virtual table
1ad10 20 61 6e 64 20 69 73 20 6e 6f 74 20 63 68 65 63   and is not chec
1ad20 6b 65 64 20 61 67 61 69 6e 20 62 79 20 53 51 4c  ked again by SQL
1ad30 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69  ite..**.** The i
1ad40 64 78 4e 75 6d 20 61 6e 64 20 69 64 78 50 74 72  dxNum and idxPtr
1ad50 20 76 61 6c 75 65 73 20 61 72 65 20 72 65 63 6f   values are reco
1ad60 72 64 65 64 20 61 6e 64 20 70 61 73 73 65 64 20  rded and passed 
1ad70 69 6e 74 6f 20 78 46 69 6c 74 65 72 2e 0a 2a 2a  into xFilter..**
1ad80 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20   sqlite3_free() 
1ad90 69 73 20 75 73 65 64 20 74 6f 20 66 72 65 65 20  is used to free 
1ada0 69 64 78 50 74 72 20 69 66 20 6e 65 65 64 54 6f  idxPtr if needTo
1adb0 46 72 65 65 49 64 78 50 74 72 20 69 73 20 74 72  FreeIdxPtr is tr
1adc0 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 72  ue..**.** The or
1add0 64 65 72 42 79 43 6f 6e 73 75 6d 65 64 20 6d 65  derByConsumed me
1ade0 61 6e 73 20 74 68 61 74 20 6f 75 74 70 75 74 20  ans that output 
1adf0 66 72 6f 6d 20 78 46 69 6c 74 65 72 20 77 69 6c  from xFilter wil
1ae00 6c 20 6f 63 63 75 72 20 69 6e 0a 2a 2a 20 74 68  l occur in.** th
1ae10 65 20 63 6f 72 72 65 63 74 20 6f 72 64 65 72 20  e correct order 
1ae20 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 20 4f  to satisfy the O
1ae30 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 73  RDER BY clause s
1ae40 6f 20 74 68 61 74 20 6e 6f 20 73 65 70 61 72 61  o that no separa
1ae50 74 65 0a 2a 2a 20 73 6f 72 74 69 6e 67 20 73 74  te.** sorting st
1ae60 65 70 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a  ep is required..
1ae70 2a 2a 0a 2a 2a 20 54 68 65 20 65 73 74 69 6d 61  **.** The estima
1ae80 74 65 64 43 6f 73 74 20 76 61 6c 75 65 20 69 73  tedCost value is
1ae90 20 61 6e 20 65 73 74 69 6d 61 74 65 20 6f 66 20   an estimate of 
1aea0 74 68 65 20 63 6f 73 74 20 6f 66 20 64 6f 69 6e  the cost of doin
1aeb0 67 20 74 68 65 0a 2a 2a 20 70 61 72 74 69 63 75  g the.** particu
1aec0 6c 61 72 20 6c 6f 6f 6b 75 70 2e 20 20 41 20 66  lar lookup.  A f
1aed0 75 6c 6c 20 73 63 61 6e 20 6f 66 20 61 20 74 61  ull scan of a ta
1aee0 62 6c 65 20 77 69 74 68 20 4e 20 65 6e 74 72 69  ble with N entri
1aef0 65 73 20 73 68 6f 75 6c 64 20 68 61 76 65 0a 2a  es should have.*
1af00 2a 20 61 20 63 6f 73 74 20 6f 66 20 4e 2e 20 20  * a cost of N.  
1af10 41 20 62 69 6e 61 72 79 20 73 65 61 72 63 68 20  A binary search 
1af20 6f 66 20 61 20 74 61 62 6c 65 20 6f 66 20 4e 20  of a table of N 
1af30 65 6e 74 72 69 65 73 20 73 68 6f 75 6c 64 20 68  entries should h
1af40 61 76 65 20 61 0a 2a 2a 20 63 6f 73 74 20 6f 66  ave a.** cost of
1af50 20 61 70 70 72 6f 78 69 6d 61 74 65 6c 79 20 6c   approximately l
1af60 6f 67 28 4e 29 2e 0a 2a 2f 0a 73 74 72 75 63 74  og(N)..*/.struct
1af70 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
1af80 6e 66 6f 20 7b 0a 20 20 2f 2a 20 49 6e 70 75 74  nfo {.  /* Input
1af90 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 69 6e 74  s */.  const int
1afa0 20 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20   nConstraint;   
1afb0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
1afc0 6e 74 72 69 65 73 20 69 6e 20 61 43 6f 6e 73 74  ntries in aConst
1afd0 72 61 69 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74  raint */.  const
1afe0 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
1aff0 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74  index_constraint
1b000 20 7b 0a 20 20 20 20 20 69 6e 74 20 69 43 6f 6c   {.     int iCol
1b010 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  umn;            
1b020 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6f 6e 20 6c    /* Column on l
1b030 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66  eft-hand side of
1b040 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20   constraint */. 
1b050 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61      unsigned cha
1b060 72 20 6f 70 3b 20 20 20 20 20 20 20 20 20 2f 2a  r op;         /*
1b070 20 43 6f 6e 73 74 72 61 69 6e 74 20 6f 70 65 72   Constraint oper
1b080 61 74 6f 72 20 2a 2f 0a 20 20 20 20 20 75 6e 73  ator */.     uns
1b090 69 67 6e 65 64 20 63 68 61 72 20 75 73 61 62 6c  igned char usabl
1b0a0 65 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69  e;     /* True i
1b0b0 66 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e  f this constrain
1b0c0 74 20 69 73 20 75 73 61 62 6c 65 20 2a 2f 0a 20  t is usable */. 
1b0d0 20 20 20 20 69 6e 74 20 69 54 65 72 6d 4f 66 66      int iTermOff
1b0e0 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  set;          /*
1b0f0 20 55 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79   Used internally
1b100 20 2d 20 78 42 65 73 74 49 6e 64 65 78 20 73 68   - xBestIndex sh
1b110 6f 75 6c 64 20 69 67 6e 6f 72 65 20 2a 2f 0a 20  ould ignore */. 
1b120 20 7d 20 2a 63 6f 6e 73 74 20 61 43 6f 6e 73 74   } *const aConst
1b130 72 61 69 6e 74 3b 20 20 20 20 20 20 2f 2a 20 54  raint;      /* T
1b140 61 62 6c 65 20 6f 66 20 57 48 45 52 45 20 63 6c  able of WHERE cl
1b150 61 75 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 73  ause constraints
1b160 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20   */.  const int 
1b170 6e 4f 72 64 65 72 42 79 3b 20 20 20 20 20 20 20  nOrderBy;       
1b180 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65   /* Number of te
1b190 72 6d 73 20 69 6e 20 74 68 65 20 4f 52 44 45 52  rms in the ORDER
1b1a0 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20   BY clause */.  
1b1b0 63 6f 6e 73 74 20 73 74 72 75 63 74 20 73 71 6c  const struct sql
1b1c0 69 74 65 33 5f 69 6e 64 65 78 5f 6f 72 64 65 72  ite3_index_order
1b1d0 62 79 20 7b 0a 20 20 20 20 20 69 6e 74 20 69 43  by {.     int iC
1b1e0 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20  olumn;          
1b1f0 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75      /* Column nu
1b200 6d 62 65 72 20 2a 2f 0a 20 20 20 20 20 75 6e 73  mber */.     uns
1b210 69 67 6e 65 64 20 63 68 61 72 20 64 65 73 63 3b  igned char desc;
1b220 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66         /* True f
1b230 6f 72 20 44 45 53 43 2e 20 20 46 61 6c 73 65 20  or DESC.  False 
1b240 66 6f 72 20 41 53 43 2e 20 2a 2f 0a 20 20 7d 20  for ASC. */.  } 
1b250 2a 63 6f 6e 73 74 20 61 4f 72 64 65 72 42 79 3b  *const aOrderBy;
1b260 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1b270 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
1b280 2a 2f 0a 0a 20 20 2f 2a 20 4f 75 74 70 75 74 73  */..  /* Outputs
1b290 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 71 6c   */.  struct sql
1b2a0 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74  ite3_index_const
1b2b0 72 61 69 6e 74 5f 75 73 61 67 65 20 7b 0a 20 20  raint_usage {.  
1b2c0 20 20 69 6e 74 20 61 72 67 76 49 6e 64 65 78 3b    int argvIndex;
1b2d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 66             /* if
1b2e0 20 3e 30 2c 20 63 6f 6e 73 74 72 61 69 6e 74 20   >0, constraint 
1b2f0 69 73 20 70 61 72 74 20 6f 66 20 61 72 67 76 20  is part of argv 
1b300 74 6f 20 78 46 69 6c 74 65 72 20 2a 2f 0a 20 20  to xFilter */.  
1b310 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
1b320 6f 6d 69 74 3b 20 20 20 20 20 20 2f 2a 20 44 6f  omit;      /* Do
1b330 20 6e 6f 74 20 63 6f 64 65 20 61 20 74 65 73 74   not code a test
1b340 20 66 6f 72 20 74 68 69 73 20 63 6f 6e 73 74 72   for this constr
1b350 61 69 6e 74 20 2a 2f 0a 20 20 7d 20 2a 63 6f 6e  aint */.  } *con
1b360 73 74 20 61 43 6f 6e 73 74 72 61 69 6e 74 55 73  st aConstraintUs
1b370 61 67 65 3b 0a 20 20 69 6e 74 20 69 64 78 4e 75  age;.  int idxNu
1b380 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m;              
1b390 20 20 2f 2a 20 4e 75 6d 62 65 72 20 75 73 65 64    /* Number used
1b3a0 20 74 6f 20 69 64 65 6e 74 69 66 79 20 74 68 65   to identify the
1b3b0 20 69 6e 64 65 78 20 2a 2f 0a 20 20 63 68 61 72   index */.  char
1b3c0 20 2a 69 64 78 53 74 72 3b 20 20 20 20 20 20 20   *idxStr;       
1b3d0 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67         /* String
1b3e0 2c 20 70 6f 73 73 69 62 6c 79 20 6f 62 74 61 69  , possibly obtai
1b3f0 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
1b400 5f 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 69 6e 74  _malloc */.  int
1b410 20 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 74   needToFreeIdxSt
1b420 72 3b 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20  r;      /* Free 
1b430 69 64 78 53 74 72 20 75 73 69 6e 67 20 73 71 6c  idxStr using sql
1b440 69 74 65 33 5f 66 72 65 65 28 29 20 69 66 20 74  ite3_free() if t
1b450 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 64  rue */.  int ord
1b460 65 72 42 79 43 6f 6e 73 75 6d 65 64 3b 20 20 20  erByConsumed;   
1b470 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6f      /* True if o
1b480 75 74 70 75 74 20 69 73 20 61 6c 72 65 61 64 79  utput is already
1b490 20 6f 72 64 65 72 65 64 20 2a 2f 0a 20 20 64 6f   ordered */.  do
1b4a0 75 62 6c 65 20 65 73 74 69 6d 61 74 65 64 43 6f  uble estimatedCo
1b4b0 73 74 3b 20 20 20 20 20 20 2f 2a 20 45 73 74 69  st;      /* Esti
1b4c0 6d 61 74 65 64 20 63 6f 73 74 20 6f 66 20 75 73  mated cost of us
1b4d0 69 6e 67 20 74 68 69 73 20 69 6e 64 65 78 20 2a  ing this index *
1b4e0 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c  /.};.#define SQL
1b4f0 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52  ITE_INDEX_CONSTR
1b500 41 49 4e 54 5f 45 51 20 20 20 20 32 0a 23 64 65  AINT_EQ    2.#de
1b510 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45  fine SQLITE_INDE
1b520 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 54 20  X_CONSTRAINT_GT 
1b530 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51 4c     4.#define SQL
1b540 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52  ITE_INDEX_CONSTR
1b550 41 49 4e 54 5f 4c 45 20 20 20 20 38 0a 23 64 65  AINT_LE    8.#de
1b560 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45  fine SQLITE_INDE
1b570 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 54 20  X_CONSTRAINT_LT 
1b580 20 20 20 31 36 0a 23 64 65 66 69 6e 65 20 53 51     16.#define SQ
1b590 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54  LITE_INDEX_CONST
1b5a0 52 41 49 4e 54 5f 47 45 20 20 20 20 33 32 0a 23  RAINT_GE    32.#
1b5b0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e  define SQLITE_IN
1b5c0 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4d  DEX_CONSTRAINT_M
1b5d0 41 54 43 48 20 36 34 0a 0a 2f 2a 0a 2a 2a 20 54  ATCH 64../*.** T
1b5e0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75  his routine is u
1b5f0 73 65 64 20 74 6f 20 72 65 67 69 73 74 65 72 20  sed to register 
1b600 61 20 6e 65 77 20 6d 6f 64 75 6c 65 20 6e 61 6d  a new module nam
1b610 65 20 77 69 74 68 20 61 6e 20 53 51 4c 69 74 65  e with an SQLite
1b620 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20  .** connection. 
1b630 20 4d 6f 64 75 6c 65 20 6e 61 6d 65 73 20 6d 75   Module names mu
1b640 73 74 20 62 65 20 72 65 67 69 73 74 65 72 65 64  st be registered
1b650 20 62 65 66 6f 72 65 20 63 72 65 61 74 69 6e 67   before creating
1b660 20 6e 65 77 0a 2a 2a 20 76 69 72 74 75 61 6c 20   new.** virtual 
1b670 74 61 62 6c 65 73 20 6f 6e 20 74 68 65 20 6d 6f  tables on the mo
1b680 64 75 6c 65 2c 20 6f 72 20 62 65 66 6f 72 65 20  dule, or before 
1b690 75 73 69 6e 67 20 70 72 65 65 78 69 73 74 69 6e  using preexistin
1b6a0 67 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62  g virtual.** tab
1b6b0 6c 65 73 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c  les of the modul
1b6c0 65 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65  e..*/.int sqlite
1b6d0 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28  3_create_module(
1b6e0 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
1b6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1b700 20 53 51 4c 69 74 65 20 63 6f 6e 6e 65 63 74 69   SQLite connecti
1b710 6f 6e 20 74 6f 20 72 65 67 69 73 74 65 72 20 6d  on to register m
1b720 6f 64 75 6c 65 20 77 69 74 68 20 2a 2f 0a 20 20  odule with */.  
1b730 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
1b740 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61  e,         /* Na
1b750 6d 65 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65  me of the module
1b760 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69   */.  const sqli
1b770 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 2c 20 20 20  te3_module *,   
1b780 20 2f 2a 20 4d 65 74 68 6f 64 73 20 66 6f 72 20   /* Methods for 
1b790 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20  the module */.  
1b7a0 76 6f 69 64 20 2a 20 20 20 20 20 20 20 20 20 20  void *          
1b7b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6c             /* Cl
1b7c0 69 65 6e 74 20 64 61 74 61 20 66 6f 72 20 78 43  ient data for xC
1b7d0 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63 74 20 2a  reate/xConnect *
1b7e0 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  /.);../*.** This
1b7f0 20 72 6f 75 74 69 6e 65 20 69 73 20 69 64 65 6e   routine is iden
1b800 74 69 63 61 6c 20 74 6f 20 74 68 65 20 73 71 6c  tical to the sql
1b810 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75  ite3_create_modu
1b820 6c 65 28 29 20 6d 65 74 68 6f 64 20 61 62 6f 76  le() method abov
1b830 65 2c 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61  e,.** except tha
1b840 74 20 69 74 20 61 6c 6c 6f 77 73 20 61 20 64 65  t it allows a de
1b850 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f  structor functio
1b860 6e 20 74 6f 20 62 65 20 73 70 65 63 69 66 69 65  n to be specifie
1b870 64 2e 20 49 74 20 69 73 0a 2a 2a 20 65 76 65 6e  d. It is.** even
1b880 20 6d 6f 72 65 20 65 78 70 65 72 69 6d 65 6e 74   more experiment
1b890 61 6c 20 74 68 61 6e 20 74 68 65 20 72 65 73 74  al than the rest
1b8a0 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20   of the virtual 
1b8b0 74 61 62 6c 65 73 20 41 50 49 2e 0a 2a 2f 0a 69  tables API..*/.i
1b8c0 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  nt sqlite3_creat
1b8d0 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 0a 20 20 73  e_module_v2(.  s
1b8e0 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
1b8f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c            /* SQL
1b900 69 74 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74  ite connection t
1b910 6f 20 72 65 67 69 73 74 65 72 20 6d 6f 64 75 6c  o register modul
1b920 65 20 77 69 74 68 20 2a 2f 0a 20 20 63 6f 6e 73  e with */.  cons
1b930 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20  t char *zName,  
1b940 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
1b950 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a  f the module */.
1b960 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
1b970 6d 6f 64 75 6c 65 20 2a 2c 20 20 20 20 2f 2a 20  module *,    /* 
1b980 4d 65 74 68 6f 64 73 20 66 6f 72 20 74 68 65 20  Methods for the 
1b990 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 76 6f 69 64  module */.  void
1b9a0 20 2a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   *,             
1b9b0 20 20 20 20 20 20 20 2f 2a 20 43 6c 69 65 6e 74         /* Client
1b9c0 20 64 61 74 61 20 66 6f 72 20 78 43 72 65 61 74   data for xCreat
1b9d0 65 2f 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 20 20  e/xConnect */.  
1b9e0 76 6f 69 64 28 2a 78 44 65 73 74 72 6f 79 29 28  void(*xDestroy)(
1b9f0 76 6f 69 64 2a 29 20 20 20 20 20 2f 2a 20 4d 6f  void*)     /* Mo
1ba00 64 75 6c 65 20 64 65 73 74 72 75 63 74 6f 72 20  dule destructor 
1ba10 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 3b 0a 0a  function */.);..
1ba20 2f 2a 0a 2a 2a 20 45 76 65 72 79 20 6d 6f 64 75  /*.** Every modu
1ba30 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  le implementatio
1ba40 6e 20 75 73 65 73 20 61 20 73 75 62 63 6c 61 73  n uses a subclas
1ba50 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  s of the followi
1ba60 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  ng structure.** 
1ba70 74 6f 20 64 65 73 63 72 69 62 65 20 61 20 70 61  to describe a pa
1ba80 72 74 69 63 75 6c 61 72 20 69 6e 73 74 61 6e 63  rticular instanc
1ba90 65 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 2e  e of the module.
1baa0 20 20 45 61 63 68 20 73 75 62 63 6c 61 73 73 20    Each subclass 
1bab0 77 69 6c 6c 0a 2a 2a 20 62 65 20 74 61 79 6c 6f  will.** be taylo
1bac0 72 65 64 20 74 6f 20 74 68 65 20 73 70 65 63 69  red to the speci
1bad0 66 69 63 20 6e 65 65 64 73 20 6f 66 20 74 68 65  fic needs of the
1bae0 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e   module implemen
1baf0 74 61 74 69 6f 6e 2e 20 20 20 54 68 65 0a 2a 2a  tation.   The.**
1bb00 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68 69 73   purpose of this
1bb10 20 73 75 70 65 72 63 6c 61 73 73 20 69 73 20 74   superclass is t
1bb20 6f 20 64 65 66 69 6e 65 20 63 65 72 74 61 69 6e  o define certain
1bb30 20 66 69 65 6c 64 73 20 74 68 61 74 20 61 72 65   fields that are
1bb40 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 74 6f 20 61 6c   common.** to al
1bb50 6c 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65  l module impleme
1bb60 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20  ntations..**.** 
1bb70 56 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6d  Virtual tables m
1bb80 65 74 68 6f 64 73 20 63 61 6e 20 73 65 74 20 61  ethods can set a
1bb90 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
1bba0 62 79 20 61 73 73 69 67 6e 69 6e 67 20 61 0a 2a  by assigning a.*
1bbb0 2a 20 73 74 72 69 6e 67 20 6f 62 74 61 69 6e 65  * string obtaine
1bbc0 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d  d from sqlite3_m
1bbd0 70 72 69 6e 74 66 28 29 20 74 6f 20 7a 45 72 72  printf() to zErr
1bbe0 4d 73 67 2e 20 20 54 68 65 20 6d 65 74 68 6f 64  Msg.  The method
1bbf0 20 73 68 6f 75 6c 64 0a 2a 2a 20 74 61 6b 65 20   should.** take 
1bc00 63 61 72 65 20 74 68 61 74 20 61 6e 79 20 70 72  care that any pr
1bc10 69 6f 72 20 73 74 72 69 6e 67 20 69 73 20 66 72  ior string is fr
1bc20 65 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f  eed by a call to
1bc30 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 0a   sqlite3_free().
1bc40 2a 2a 20 70 72 69 6f 72 20 74 6f 20 61 73 73 69  ** prior to assi
1bc50 67 6e 69 6e 67 20 61 20 6e 65 77 20 73 74 72 69  gning a new stri
1bc60 6e 67 20 74 6f 20 7a 45 72 72 4d 73 67 2e 20 20  ng to zErrMsg.  
1bc70 41 66 74 65 72 20 74 68 65 20 65 72 72 6f 72 20  After the error 
1bc80 6d 65 73 73 61 67 65 0a 2a 2a 20 69 73 20 64 65  message.** is de
1bc90 6c 69 76 65 72 65 64 20 75 70 20 74 6f 20 74 68  livered up to th
1bca0 65 20 63 6c 69 65 6e 74 20 61 70 70 6c 69 63 61  e client applica
1bcb0 74 69 6f 6e 2c 20 74 68 65 20 73 74 72 69 6e 67  tion, the string
1bcc0 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74   will be automat
1bcd0 69 63 61 6c 6c 79 0a 2a 2a 20 66 72 65 65 64 20  ically.** freed 
1bce0 62 79 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  by sqlite3_free(
1bcf0 29 20 61 6e 64 20 74 68 65 20 7a 45 72 72 4d 73  ) and the zErrMs
1bd00 67 20 66 69 65 6c 64 20 77 69 6c 6c 20 62 65 20  g field will be 
1bd10 7a 65 72 6f 65 64 2e 20 20 4e 6f 74 65 0a 2a 2a  zeroed.  Note.**
1bd20 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f 6d 70   that sqlite3_mp
1bd30 72 69 6e 74 66 28 29 20 61 6e 64 20 73 71 6c 69  rintf() and sqli
1bd40 74 65 33 5f 66 72 65 65 28 29 20 61 72 65 20 75  te3_free() are u
1bd50 73 65 64 20 6f 6e 20 74 68 65 20 7a 45 72 72 4d  sed on the zErrM
1bd60 73 67 20 66 69 65 6c 64 0a 2a 2a 20 73 69 6e 63  sg field.** sinc
1bd70 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73  e virtual tables
1bd80 20 61 72 65 20 63 6f 6d 6d 6f 6e 6c 79 20 69 6d   are commonly im
1bd90 70 6c 65 6d 65 6e 74 65 64 20 69 6e 20 6c 6f 61  plemented in loa
1bda0 64 61 62 6c 65 20 65 78 74 65 6e 73 69 6f 6e 73  dable extensions
1bdb0 20 77 68 69 63 68 0a 2a 2a 20 64 6f 20 6e 6f 74   which.** do not
1bdc0 20 68 61 76 65 20 61 63 63 65 73 73 20 74 6f 20   have access to 
1bdd0 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 29  sqlite3MPrintf()
1bde0 20 6f 72 20 73 71 6c 69 74 65 33 46 72 65 65 28   or sqlite3Free(
1bdf0 29 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c  )..*/.struct sql
1be00 69 74 65 33 5f 76 74 61 62 20 7b 0a 20 20 63 6f  ite3_vtab {.  co
1be10 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  nst sqlite3_modu
1be20 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 20 20 2f 2a  le *pModule;  /*
1be30 20 54 68 65 20 6d 6f 64 75 6c 65 20 66 6f 72 20   The module for 
1be40 74 68 69 73 20 76 69 72 74 75 61 6c 20 74 61 62  this virtual tab
1be50 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66  le */.  int nRef
1be60 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1be70 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20          /* Used 
1be80 69 6e 74 65 72 6e 61 6c 6c 79 20 2a 2f 0a 20 20  internally */.  
1be90 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20  char *zErrMsg;  
1bea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1beb0 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73 61 67 65  /* Error message
1bec0 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 70   from sqlite3_mp
1bed0 72 69 6e 74 66 28 29 20 2a 2f 0a 20 20 2f 2a 20  rintf() */.  /* 
1bee0 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6d  Virtual table im
1bef0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69  plementations wi
1bf00 6c 6c 20 74 79 70 69 63 61 6c 6c 79 20 61 64 64  ll typically add
1bf10 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c   additional fiel
1bf20 64 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 45 76 65  ds */.};../* Eve
1bf30 72 79 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d  ry module implem
1bf40 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 61 20  entation uses a 
1bf50 73 75 62 63 6c 61 73 73 20 6f 66 20 74 68 65 20  subclass of the 
1bf60 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
1bf70 75 72 65 0a 2a 2a 20 74 6f 20 64 65 73 63 72 69  ure.** to descri
1bf80 62 65 20 63 75 72 73 6f 72 73 20 74 68 61 74 20  be cursors that 
1bf90 70 6f 69 6e 74 20 69 6e 74 6f 20 74 68 65 20 76  point into the v
1bfa0 69 72 74 75 61 6c 20 74 61 62 6c 65 20 61 6e 64  irtual table and
1bfb0 20 61 72 65 20 75 73 65 64 0a 2a 2a 20 74 6f 20   are used.** to 
1bfc0 6c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65  loop through the
1bfd0 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20   virtual table. 
1bfe0 20 43 75 72 73 6f 72 73 20 61 72 65 20 63 72 65   Cursors are cre
1bff0 61 74 65 64 20 75 73 69 6e 67 20 74 68 65 0a 2a  ated using the.*
1c000 2a 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 6f  * xOpen method o
1c010 66 20 74 68 65 20 6d 6f 64 75 6c 65 2e 20 20 45  f the module.  E
1c020 61 63 68 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65  ach module imple
1c030 6d 65 6e 74 61 74 69 6f 6e 20 77 69 6c 6c 20 64  mentation will d
1c040 65 66 69 6e 65 0a 2a 2a 20 74 68 65 20 63 6f 6e  efine.** the con
1c050 74 65 6e 74 20 6f 66 20 61 20 63 75 72 73 6f 72  tent of a cursor
1c060 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 73 75   structure to su
1c070 69 74 20 69 74 73 20 6f 77 6e 20 6e 65 65 64 73  it its own needs
1c080 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 75 70  ..**.** This sup
1c090 65 72 63 6c 61 73 73 20 65 78 69 73 74 73 20 69  erclass exists i
1c0a0 6e 20 6f 72 64 65 72 20 74 6f 20 64 65 66 69 6e  n order to defin
1c0b0 65 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20  e fields of the 
1c0c0 63 75 72 73 6f 72 20 74 68 61 74 0a 2a 2a 20 61  cursor that.** a
1c0d0 72 65 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c  re common to all
1c0e0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
1c0f0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69  ..*/.struct sqli
1c100 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
1c110 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62  {.  sqlite3_vtab
1c120 20 2a 70 56 74 61 62 3b 20 20 20 20 20 20 2f 2a   *pVtab;      /*
1c130 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6f   Virtual table o
1c140 66 20 74 68 69 73 20 63 75 72 73 6f 72 20 2a 2f  f this cursor */
1c150 0a 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61  .  /* Virtual ta
1c160 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ble implementati
1c170 6f 6e 73 20 77 69 6c 6c 20 74 79 70 69 63 61 6c  ons will typical
1c180 6c 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61  ly add additiona
1c190 6c 20 66 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a 0a  l fields */.};..
1c1a0 2f 2a 0a 2a 2a 20 54 68 65 20 78 43 72 65 61 74  /*.** The xCreat
1c1b0 65 20 61 6e 64 20 78 43 6f 6e 6e 65 63 74 20 6d  e and xConnect m
1c1c0 65 74 68 6f 64 73 20 6f 66 20 61 20 6d 6f 64 75  ethods of a modu
1c1d0 6c 65 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f  le use the follo
1c1e0 77 69 6e 67 20 41 50 49 0a 2a 2a 20 74 6f 20 64  wing API.** to d
1c1f0 65 63 6c 61 72 65 20 74 68 65 20 66 6f 72 6d 61  eclare the forma
1c200 74 20 28 74 68 65 20 6e 61 6d 65 73 20 61 6e 64  t (the names and
1c210 20 64 61 74 61 74 79 70 65 73 20 6f 66 20 74 68   datatypes of th
1c220 65 20 63 6f 6c 75 6d 6e 73 29 20 6f 66 0a 2a 2a  e columns) of.**
1c230 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
1c240 6c 65 73 20 74 68 65 79 20 69 6d 70 6c 65 6d 65  les they impleme
1c250 6e 74 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74  nt..*/.int sqlit
1c260 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 28  e3_declare_vtab(
1c270 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20  sqlite3*, const 
1c280 63 68 61 72 20 2a 7a 43 72 65 61 74 65 54 61 62  char *zCreateTab
1c290 6c 65 29 3b 0a 0a 2f 2a 0a 2a 2a 20 56 69 72 74  le);../*.** Virt
1c2a0 75 61 6c 20 74 61 62 6c 65 73 20 63 61 6e 20 70  ual tables can p
1c2b0 72 6f 76 69 64 65 20 61 6c 74 65 72 6e 61 74 69  rovide alternati
1c2c0 76 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ve implementatio
1c2d0 6e 73 20 6f 66 20 66 75 6e 63 74 69 6f 6e 73 0a  ns of functions.
1c2e0 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 78 46 69  ** using the xFi
1c2f0 6e 64 46 75 6e 63 74 69 6f 6e 20 6d 65 74 68 6f  ndFunction metho
1c300 64 2e 20 20 42 75 74 20 67 6c 6f 62 61 6c 20 76  d.  But global v
1c310 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 6f 73 65  ersions of those
1c320 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 6d 75   functions.** mu
1c330 73 74 20 65 78 69 73 74 20 69 6e 20 6f 72 64 65  st exist in orde
1c340 72 20 74 6f 20 62 65 20 6f 76 65 72 6c 6f 61 64  r to be overload
1c350 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41  ed..**.** This A
1c360 50 49 20 6d 61 6b 65 73 20 73 75 72 65 20 61 20  PI makes sure a 
1c370 67 6c 6f 62 61 6c 20 76 65 72 73 69 6f 6e 20 6f  global version o
1c380 66 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74  f a function wit
1c390 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a  h a particular.*
1c3a0 2a 20 6e 61 6d 65 20 61 6e 64 20 6e 75 6d 62 65  * name and numbe
1c3b0 72 20 6f 66 20 70 61 72 61 6d 65 74 65 72 73 20  r of parameters 
1c3c0 65 78 69 73 74 73 2e 20 20 49 66 20 6e 6f 20 73  exists.  If no s
1c3d0 75 63 68 20 66 75 6e 63 74 69 6f 6e 20 65 78 69  uch function exi
1c3e0 73 74 73 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68  sts.** before th
1c3f0 69 73 20 41 50 49 20 69 73 20 63 61 6c 6c 65 64  is API is called
1c400 2c 20 61 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e  , a new function
1c410 20 69 73 20 63 72 65 61 74 65 64 2e 20 20 54 68   is created.  Th
1c420 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
1c430 0a 2a 2a 20 6f 66 20 74 68 65 20 6e 65 77 20 66  .** of the new f
1c440 75 6e 63 74 69 6f 6e 20 61 6c 77 61 79 73 20 63  unction always c
1c450 61 75 73 65 73 20 61 6e 20 65 78 63 65 70 74 69  auses an excepti
1c460 6f 6e 20 74 6f 20 62 65 20 74 68 72 6f 77 6e 2e  on to be thrown.
1c470 20 20 53 6f 0a 2a 2a 20 74 68 65 20 6e 65 77 20    So.** the new 
1c480 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20  function is not 
1c490 67 6f 6f 64 20 66 6f 72 20 61 6e 79 74 68 69 6e  good for anythin
1c4a0 67 20 62 79 20 69 74 73 65 6c 66 2e 20 20 49 74  g by itself.  It
1c4b0 73 20 6f 6e 6c 79 0a 2a 2a 20 70 75 72 70 6f 73  s only.** purpos
1c4c0 65 20 69 73 20 74 6f 20 62 65 20 61 20 70 6c 61  e is to be a pla
1c4d0 63 65 2d 68 6f 6c 64 65 72 20 66 75 6e 63 74 69  ce-holder functi
1c4e0 6f 6e 20 74 68 61 74 20 63 61 6e 20 62 65 20 6f  on that can be o
1c4f0 76 65 72 6c 6f 61 64 65 64 0a 2a 2a 20 62 79 20  verloaded.** by 
1c500 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 2e 0a  virtual tables..
1c510 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 73  **.** This API s
1c520 68 6f 75 6c 64 20 62 65 20 63 6f 6e 73 69 64 65  hould be conside
1c530 72 65 64 20 70 61 72 74 20 6f 66 20 74 68 65 20  red part of the 
1c540 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e  virtual table in
1c550 74 65 72 66 61 63 65 2c 0a 2a 2a 20 77 68 69 63  terface,.** whic
1c560 68 20 69 73 20 65 78 70 65 72 69 6d 65 6e 74 61  h is experimenta
1c570 6c 20 61 6e 64 20 73 75 62 6a 65 63 74 20 74 6f  l and subject to
1c580 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a 69 6e 74 20   change..*/.int 
1c590 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64  sqlite3_overload
1c5a0 5f 66 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65  _function(sqlite
1c5b0 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  3*, const char *
1c5c0 7a 46 75 6e 63 4e 61 6d 65 2c 20 69 6e 74 20 6e  zFuncName, int n
1c5d0 41 72 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  Arg);../*.** The
1c5e0 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68   interface to th
1c5f0 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20  e virtual-table 
1c600 6d 65 63 68 61 6e 69 73 6d 20 64 65 66 69 6e 65  mechanism define
1c610 64 20 61 62 6f 76 65 20 28 62 61 63 6b 20 75 70  d above (back up
1c620 0a 2a 2a 20 74 6f 20 61 20 63 6f 6d 6d 65 6e 74  .** to a comment
1c630 20 72 65 6d 61 72 6b 61 62 6c 79 20 73 69 6d 69   remarkably simi
1c640 6c 61 72 20 74 6f 20 74 68 69 73 20 6f 6e 65 29  lar to this one)
1c650 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 63 6f   is currently co
1c660 6e 73 69 64 65 72 65 64 0a 2a 2a 20 74 6f 20 62  nsidered.** to b
1c670 65 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 2e 20  e experimental. 
1c680 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 6d   The interface m
1c690 69 67 68 74 20 63 68 61 6e 67 65 20 69 6e 20 69  ight change in i
1c6a0 6e 63 6f 6d 70 61 74 69 62 6c 65 20 77 61 79 73  ncompatible ways
1c6b0 2e 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20  ..** If this is 
1c6c0 61 20 70 72 6f 62 6c 65 6d 20 66 6f 72 20 79 6f  a problem for yo
1c6d0 75 2c 20 64 6f 20 6e 6f 74 20 75 73 65 20 74 68  u, do not use th
1c6e0 65 20 69 6e 74 65 72 66 61 63 65 20 61 74 20 74  e interface at t
1c6f0 68 69 73 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20  his time..**.** 
1c700 57 68 65 6e 20 74 68 65 20 76 69 72 74 75 61 6c  When the virtual
1c710 2d 74 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d  -table mechanism
1c720 20 73 74 61 62 6c 69 7a 65 73 2c 20 77 65 20 77   stablizes, we w
1c730 69 6c 6c 20 64 65 63 6c 61 72 65 20 74 68 65 0a  ill declare the.
1c740 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 66 69 78  ** interface fix
1c750 65 64 2c 20 73 75 70 70 6f 72 74 20 69 74 20 69  ed, support it i
1c760 6e 64 65 66 69 6e 69 74 65 6c 79 2c 20 61 6e 64  ndefinitely, and
1c770 20 72 65 6d 6f 76 65 20 74 68 69 73 20 63 6f 6d   remove this com
1c780 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 20  ment..**.****** 
1c790 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 2d 20 73  EXPERIMENTAL - s
1c7a0 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65  ubject to change
1c7b0 20 77 69 74 68 6f 75 74 20 6e 6f 74 69 63 65 20   without notice 
1c7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
1c7d0 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  /../*.** CAPI3RE
1c7e0 46 3a 20 41 20 48 61 6e 64 6c 65 20 54 6f 20 41  F: A Handle To A
1c7f0 6e 20 4f 70 65 6e 20 42 4c 4f 42 0a 2a 2a 0a 2a  n Open BLOB.**.*
1c800 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
1c810 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f   the following o
1c820 70 61 71 75 65 20 73 74 72 75 63 74 75 72 65 20  paque structure 
1c830 69 73 20 75 73 65 64 20 74 6f 20 0a 2a 2a 20 72  is used to .** r
1c840 65 70 72 65 73 65 6e 74 20 61 6e 20 62 6c 6f 62  epresent an blob
1c850 2d 68 61 6e 64 6c 65 2e 20 20 41 20 62 6c 6f 62  -handle.  A blob
1c860 2d 68 61 6e 64 6c 65 20 69 73 20 63 72 65 61 74  -handle is creat
1c870 65 64 20 62 79 0a 2a 2a 20 5b 73 71 6c 69 74 65  ed by.** [sqlite
1c880 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 20 61  3_blob_open()] a
1c890 6e 64 20 64 65 73 74 72 6f 79 65 64 20 62 79 20  nd destroyed by 
1c8a0 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c  [sqlite3_blob_cl
1c8b0 6f 73 65 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 5b  ose()]..** The [
1c8c0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61  sqlite3_blob_rea
1c8d0 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  d()] and [sqlite
1c8e0 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 29 5d 20  3_blob_write()] 
1c8f0 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 63 61  interfaces.** ca
1c900 6e 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 61  n be used to rea
1c910 64 20 6f 72 20 77 72 69 74 65 20 73 6d 61 6c 6c  d or write small
1c920 20 73 75 62 73 65 63 74 69 6f 6e 73 20 6f 66 20   subsections of 
1c930 74 68 65 20 62 6c 6f 62 2e 0a 2a 2a 20 54 68 65  the blob..** The
1c940 20 5b 73 71 6c 74 69 65 33 5f 62 6c 6f 62 5f 73   [sqltie3_blob_s
1c950 69 7a 65 28 29 5d 20 69 6e 74 65 72 66 61 63 65  ize()] interface
1c960 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 69 7a   returns the siz
1c970 65 20 6f 66 20 74 68 65 0a 2a 2a 20 62 6c 6f 62  e of the.** blob
1c980 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a 74 79   in bytes..*/.ty
1c990 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
1c9a0 69 74 65 33 5f 62 6c 6f 62 20 73 71 6c 69 74 65  ite3_blob sqlite
1c9b0 33 5f 62 6c 6f 62 3b 0a 0a 2f 2a 0a 2a 2a 20 43  3_blob;../*.** C
1c9c0 41 50 49 33 52 45 46 3a 20 4f 70 65 6e 20 41 20  API3REF: Open A 
1c9d0 42 4c 4f 42 20 46 6f 72 20 49 6e 63 72 65 6d 65  BLOB For Increme
1c9e0 6e 74 61 6c 20 49 2f 4f 0a 2a 2a 0a 2a 2a 20 4f  ntal I/O.**.** O
1c9f0 70 65 6e 20 61 20 68 61 6e 64 6c 65 20 74 6f 20  pen a handle to 
1ca00 74 68 65 20 62 6c 6f 62 20 6c 6f 63 61 74 65 64  the blob located
1ca10 20 69 6e 20 72 6f 77 20 69 52 6f 77 2c 2c 20 63   in row iRow,, c
1ca20 6f 6c 75 6d 6e 20 7a 43 6f 6c 75 6d 6e 2c 20 0a  olumn zColumn, .
1ca30 2a 2a 20 74 61 62 6c 65 20 7a 54 61 62 6c 65 20  ** table zTable 
1ca40 69 6e 20 64 61 74 61 62 61 73 65 20 7a 44 62 2e  in database zDb.
1ca50 20 69 2e 65 2e 20 74 68 65 20 73 61 6d 65 20 62   i.e. the same b
1ca60 6c 6f 62 20 74 68 61 74 20 77 6f 75 6c 64 0a 2a  lob that would.*
1ca70 2a 20 62 65 20 73 65 6c 65 63 74 65 64 20 62 79  * be selected by
1ca80 3a 0a 2a 2a 0a 2a 2a 20 3c 70 72 65 3e 0a 2a 2a  :.**.** <pre>.**
1ca90 20 20 20 20 20 53 45 4c 45 43 54 20 7a 43 6f 6c       SELECT zCol
1caa0 75 6d 6e 20 46 52 4f 4d 20 7a 44 62 2e 7a 54 61  umn FROM zDb.zTa
1cab0 62 6c 65 20 57 48 45 52 45 20 72 6f 77 69 64 20  ble WHERE rowid 
1cac0 3d 20 69 52 6f 77 3b 0a 2a 2a 20 3c 2f 70 72 65  = iRow;.** </pre
1cad0 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66  >.**.** If the f
1cae0 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 69  lags parameter i
1caf0 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 20  s non-zero, the 
1cb00 62 6c 6f 62 20 69 73 20 6f 70 65 6e 65 64 20 66  blob is opened f
1cb10 6f 72 20 0a 2a 2a 20 72 65 61 64 20 61 6e 64 20  or .** read and 
1cb20 77 72 69 74 65 20 61 63 63 65 73 73 2e 20 49 66  write access. If
1cb30 20 69 74 20 69 73 20 7a 65 72 6f 2c 20 74 68 65   it is zero, the
1cb40 20 62 6c 6f 62 20 69 73 20 6f 70 65 6e 65 64 20   blob is opened 
1cb50 66 6f 72 20 72 65 61 64 20 0a 2a 2a 20 61 63 63  for read .** acc
1cb60 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75  ess..**.** On su
1cb70 63 63 65 73 73 2c 20 5b 53 51 4c 49 54 45 5f 4f  ccess, [SQLITE_O
1cb80 4b 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  K] is returned a
1cb90 6e 64 20 74 68 65 20 6e 65 77 20 0a 2a 2a 20 5b  nd the new .** [
1cba0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 7c 20 62  sqlite3_blob | b
1cbb0 6c 6f 62 20 68 61 6e 64 6c 65 5d 20 69 73 20 77  lob handle] is w
1cbc0 72 69 74 74 65 6e 20 74 6f 20 2a 70 70 42 6c 6f  ritten to *ppBlo
1cbd0 62 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 20  b..** Otherwise 
1cbe0 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  an error code is
1cbf0 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 0a 2a   returned and .*
1cc00 2a 20 61 6e 79 20 76 61 6c 75 65 20 77 72 69 74  * any value writ
1cc10 74 65 6e 20 74 6f 20 2a 70 70 42 6c 6f 62 20 73  ten to *ppBlob s
1cc20 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 75 73 65  hould not be use
1cc30 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e  d by the caller.
1cc40 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
1cc50 6e 20 73 65 74 73 20 74 68 65 20 64 61 74 61 62  n sets the datab
1cc60 61 73 65 2d 68 61 6e 64 6c 65 20 65 72 72 6f 72  ase-handle error
1cc70 20 63 6f 64 65 20 61 6e 64 20 6d 65 73 73 61 67   code and messag
1cc80 65 0a 2a 2a 20 61 63 63 65 73 73 69 62 6c 65 20  e.** accessible 
1cc90 76 69 61 20 5b 73 71 6c 69 74 65 33 5f 65 72 72  via [sqlite3_err
1cca0 63 6f 64 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c  code()] and [sql
1ccb0 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e 0a  ite3_errmsg()]..
1ccc0 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  */.int sqlite3_b
1ccd0 6c 6f 62 5f 6f 70 65 6e 28 0a 20 20 73 71 6c 69  lob_open(.  sqli
1cce0 74 65 33 2a 2c 0a 20 20 63 6f 6e 73 74 20 63 68  te3*,.  const ch
1ccf0 61 72 20 2a 7a 44 62 2c 0a 20 20 63 6f 6e 73 74  ar *zDb,.  const
1cd00 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 2c 0a 20   char *zTable,. 
1cd10 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f   const char *zCo
1cd20 6c 75 6d 6e 2c 0a 20 20 73 71 6c 69 74 65 5f 69  lumn,.  sqlite_i
1cd30 6e 74 36 34 20 69 52 6f 77 2c 0a 20 20 69 6e 74  nt64 iRow,.  int
1cd40 20 66 6c 61 67 73 2c 0a 20 20 73 71 6c 69 74 65   flags,.  sqlite
1cd50 33 5f 62 6c 6f 62 20 2a 2a 70 70 42 6c 6f 62 0a  3_blob **ppBlob.
1cd60 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
1cd70 45 46 3a 20 20 43 6c 6f 73 65 20 41 20 42 4c 4f  EF:  Close A BLO
1cd80 42 20 48 61 6e 64 6c 65 0a 2a 2a 0a 2a 2a 20 43  B Handle.**.** C
1cd90 6c 6f 73 65 20 61 6e 20 6f 70 65 6e 20 5b 73 71  lose an open [sq
1cda0 6c 69 74 65 33 5f 62 6c 6f 62 20 7c 20 62 6c 6f  lite3_blob | blo
1cdb0 62 20 68 61 6e 64 6c 65 5d 2e 0a 2a 2f 0a 69 6e  b handle]..*/.in
1cdc0 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63  t sqlite3_blob_c
1cdd0 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f  lose(sqlite3_blo
1cde0 62 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  b *);../*.** CAP
1cdf0 49 33 52 45 46 3a 20 20 52 65 74 75 72 6e 20 54  I3REF:  Return T
1ce00 68 65 20 53 69 7a 65 20 4f 66 20 41 6e 20 4f 70  he Size Of An Op
1ce10 65 6e 20 42 4c 4f 42 0a 2a 2a 0a 2a 2a 20 52 65  en BLOB.**.** Re
1ce20 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 69 6e  turn the size in
1ce30 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 62 6c   bytes of the bl
1ce40 6f 62 20 61 63 63 65 73 73 69 62 6c 65 20 76 69  ob accessible vi
1ce50 61 20 74 68 65 20 6f 70 65 6e 20 0a 2a 2a 20 5b  a the open .** [
1ce60 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 7c 20 62  sqlite3_blob | b
1ce70 6c 6f 62 2d 68 61 6e 64 6c 65 5d 20 70 61 73 73  lob-handle] pass
1ce80 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e  ed as an argumen
1ce90 74 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65  t..*/.int sqlite
1cea0 33 5f 62 6c 6f 62 5f 62 79 74 65 73 28 73 71 6c  3_blob_bytes(sql
1ceb0 69 74 65 33 5f 62 6c 6f 62 20 2a 29 3b 0a 0a 2f  ite3_blob *);../
1cec0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 20  *.** CAPI3REF:  
1ced0 52 65 61 64 20 44 61 74 61 20 46 72 6f 6d 20 41  Read Data From A
1cee0 20 42 4c 4f 42 20 49 6e 63 72 65 6d 65 6e 74 61   BLOB Incrementa
1cef0 6c 6c 79 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  lly.**.** This f
1cf00 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  unction is used 
1cf10 74 6f 20 72 65 61 64 20 64 61 74 61 20 66 72 6f  to read data fro
1cf20 6d 20 61 6e 20 6f 70 65 6e 20 0a 2a 2a 20 5b 73  m an open .** [s
1cf30 71 6c 69 74 65 33 5f 62 6c 6f 62 20 7c 20 62 6c  qlite3_blob | bl
1cf40 6f 62 2d 68 61 6e 64 6c 65 5d 20 69 6e 74 6f 20  ob-handle] into 
1cf50 61 20 63 61 6c 6c 65 72 20 73 75 70 70 6c 69 65  a caller supplie
1cf60 64 20 62 75 66 66 65 72 2e 0a 2a 2a 20 6e 20 62  d buffer..** n b
1cf70 79 74 65 73 20 6f 66 20 64 61 74 61 20 61 72 65  ytes of data are
1cf80 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 62 75 66   copied into buf
1cf90 66 65 72 0a 2a 2a 20 7a 20 66 72 6f 6d 20 74 68  fer.** z from th
1cfa0 65 20 6f 70 65 6e 20 62 6c 6f 62 2c 20 73 74 61  e open blob, sta
1cfb0 72 74 69 6e 67 20 61 74 20 6f 66 66 73 65 74 20  rting at offset 
1cfc0 69 4f 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 4f  iOffset..**.** O
1cfd0 6e 20 73 75 63 63 65 73 73 2c 20 53 51 4c 49 54  n success, SQLIT
1cfe0 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
1cff0 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 20  . Otherwise, an 
1d000 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f  .** [SQLITE_ERRO
1d010 52 20 7c 20 53 51 4c 69 74 65 20 65 72 72 6f 72  R | SQLite error
1d020 20 63 6f 64 65 5d 20 6f 72 20 61 6e 0a 2a 2a 20   code] or an.** 
1d030 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45  [SQLITE_IOERR_RE
1d040 41 44 20 7c 20 65 78 74 65 6e 64 65 64 20 65 72  AD | extended er
1d050 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74  ror code] is ret
1d060 75 72 6e 65 64 2e 0a 2a 2f 0a 69 6e 74 20 73 71  urned..*/.int sq
1d070 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28  lite3_blob_read(
1d080 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 2c 20  sqlite3_blob *, 
1d090 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20  void *z, int n, 
1d0a0 69 6e 74 20 69 4f 66 66 73 65 74 29 3b 0a 0a 2f  int iOffset);../
1d0b0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 20  *.** CAPI3REF:  
1d0c0 57 72 69 74 65 20 44 61 74 61 20 49 6e 74 6f 20  Write Data Into 
1d0d0 41 20 42 4c 4f 42 20 49 6e 63 72 65 6d 65 6e 74  A BLOB Increment
1d0e0 61 6c 6c 79 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ally.**.** This 
1d0f0 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64  function is used
1d100 20 74 6f 20 77 72 69 74 65 20 64 61 74 61 20 69   to write data i
1d110 6e 74 6f 20 61 6e 20 6f 70 65 6e 20 0a 2a 2a 20  nto an open .** 
1d120 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 7c 20  [sqlite3_blob | 
1d130 62 6c 6f 62 2d 68 61 6e 64 6c 65 5d 20 66 72 6f  blob-handle] fro
1d140 6d 20 61 20 75 73 65 72 20 73 75 70 70 6c 69 65  m a user supplie
1d150 64 20 62 75 66 66 65 72 2e 0a 2a 2a 20 6e 20 62  d buffer..** n b
1d160 79 74 65 73 20 6f 66 20 64 61 74 61 20 61 72 65  ytes of data are
1d170 20 63 6f 70 69 65 64 20 66 72 6f 6d 20 74 68 65   copied from the
1d180 20 62 75 66 66 65 72 0a 2a 2a 20 70 6f 69 6e 74   buffer.** point
1d190 65 64 20 74 6f 20 62 79 20 7a 20 69 6e 74 6f 20  ed to by z into 
1d1a0 74 68 65 20 6f 70 65 6e 20 62 6c 6f 62 2c 20 73  the open blob, s
1d1b0 74 61 72 74 69 6e 67 20 61 74 20 6f 66 66 73 65  tarting at offse
1d1c0 74 20 69 4f 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a  t iOffset..**.**
1d1d0 20 49 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33   If the [sqlite3
1d1e0 5f 62 6c 6f 62 20 7c 20 62 6c 6f 62 2d 68 61 6e  _blob | blob-han
1d1f0 64 6c 65 5d 20 70 61 73 73 65 64 20 61 73 20 74  dle] passed as t
1d200 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
1d210 74 0a 2a 2a 20 77 61 73 20 6e 6f 74 20 6f 70 65  t.** was not ope
1d220 6e 65 64 20 66 6f 72 20 77 72 69 74 69 6e 67 20  ned for writing 
1d230 28 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d  (the flags param
1d240 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33  eter to [sqlite3
1d250 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 0a 2a 2a  _blob_open()].**
1d260 2a 20 77 61 73 20 7a 65 72 6f 29 2c 20 74 68 69  * was zero), thi
1d270 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
1d280 6e 73 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f  ns [SQLITE_READO
1d290 4e 4c 59 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  NLY]..**.** This
1d2a0 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6f 6e   function may on
1d2b0 6c 79 20 6d 6f 64 69 66 79 20 74 68 65 20 63 6f  ly modify the co
1d2c0 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 62 6c  ntents of the bl
1d2d0 6f 62 2c 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74  ob, it is.** not
1d2e0 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 69 6e 63   possible to inc
1d2f0 72 65 61 73 65 20 74 68 65 20 73 69 7a 65 20 6f  rease the size o
1d300 66 20 61 20 62 6c 6f 62 20 75 73 69 6e 67 20 74  f a blob using t
1d310 68 69 73 20 41 50 49 2e 20 49 66 0a 2a 2a 20 6f  his API. If.** o
1d320 66 66 73 65 74 20 69 4f 66 66 73 65 74 20 69 73  ffset iOffset is
1d330 20 6c 65 73 73 20 74 68 61 6e 20 6e 20 62 79 74   less than n byt
1d340 65 73 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 20  es from the end 
1d350 6f 66 20 74 68 65 20 62 6c 6f 62 2c 20 0a 2a 2a  of the blob, .**
1d360 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20   [SQLITE_ERROR] 
1d370 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
1d380 6e 6f 20 64 61 74 61 20 69 73 20 77 72 69 74 74  no data is writt
1d390 65 6e 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63  en..**.** On suc
1d3a0 63 65 73 73 2c 20 53 51 4c 49 54 45 5f 4f 4b 20  cess, SQLITE_OK 
1d3b0 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68  is returned. Oth
1d3c0 65 72 77 69 73 65 2c 20 61 6e 20 0a 2a 2a 20 5b  erwise, an .** [
1d3d0 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 7c 20 53  SQLITE_ERROR | S
1d3e0 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65  QLite error code
1d3f0 5d 20 6f 72 20 61 6e 0a 2a 2a 20 5b 53 51 4c 49  ] or an.** [SQLI
1d400 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 20 7c 20  TE_IOERR_READ | 
1d410 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63  extended error c
1d420 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64  ode] is returned
1d430 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33  ..*/.int sqlite3
1d440 5f 62 6c 6f 62 5f 77 72 69 74 65 28 73 71 6c 69  _blob_write(sqli
1d450 74 65 33 5f 62 6c 6f 62 20 2a 2c 20 63 6f 6e 73  te3_blob *, cons
1d460 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e  t void *z, int n
1d470 2c 20 69 6e 74 20 69 4f 66 66 73 65 74 29 3b 0a  , int iOffset);.
1d480 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 74 68 65 20  ./*.** Undo the 
1d490 68 61 63 6b 20 74 68 61 74 20 63 6f 6e 76 65 72  hack that conver
1d4a0 74 73 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e  ts floating poin
1d4b0 74 20 74 79 70 65 73 20 74 6f 20 69 6e 74 65 67  t types to integ
1d4c0 65 72 20 66 6f 72 0a 2a 2a 20 62 75 69 6c 64 73  er for.** builds
1d4d0 20 6f 6e 20 70 72 6f 63 65 73 73 6f 72 73 20 77   on processors w
1d4e0 69 74 68 6f 75 74 20 66 6c 6f 61 74 69 6e 67 20  ithout floating 
1d4f0 70 6f 69 6e 74 20 73 75 70 70 6f 72 74 2e 0a 2a  point support..*
1d500 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
1d510 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
1d520 49 4e 54 0a 23 20 75 6e 64 65 66 20 64 6f 75 62  INT.# undef doub
1d530 6c 65 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 30  le.#endif..#if 0
1d540 0a 7d 20 20 2f 2a 20 45 6e 64 20 6f 66 20 74 68  .}  /* End of th
1d550 65 20 27 65 78 74 65 72 6e 20 22 43 22 27 20 62  e 'extern "C"' b
1d560 6c 6f 63 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a 23  lock */.#endif.#
1d570 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  endif../********
1d580 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71  ****** End of sq
1d590 6c 69 74 65 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  lite3.h ********
1d5a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d5b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d5c0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
1d5d0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
1d5e0 65 20 64 61 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  e date.c *******
1d5f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d610 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
1d620 33 20 4f 63 74 6f 62 65 72 20 33 31 0a 2a 2a 0a  3 October 31.**.
1d630 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
1d640 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
1d650 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
1d660 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
1d670 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
1d680 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
1d690 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
1d6a0 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
1d6b0 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
1d6c0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
1d6d0 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
1d6e0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
1d6f0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
1d700 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
1d710 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
1d720 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
1d730 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
1d740 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
1d750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
1d790 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
1d7a0 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e  s the C function
1d7b0 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  s that implement
1d7c0 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 0a 2a   date and time.*
1d7d0 2a 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20  * functions for 
1d7e0 53 51 4c 69 74 65 2e 20 20 0a 2a 2a 0a 2a 2a 20  SQLite.  .**.** 
1d7f0 54 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 6f 6e  There is only on
1d800 65 20 65 78 70 6f 72 74 65 64 20 73 79 6d 62 6f  e exported symbo
1d810 6c 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 2d  l in this file -
1d820 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a   the function.**
1d830 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72   sqlite3Register
1d840 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e  DateTimeFunction
1d850 73 28 29 20 66 6f 75 6e 64 20 61 74 20 74 68 65  s() found at the
1d860 20 62 6f 74 74 6f 6d 20 6f 66 20 74 68 65 20 66   bottom of the f
1d870 69 6c 65 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65  ile..** All othe
1d880 72 20 63 6f 64 65 20 68 61 73 20 66 69 6c 65 20  r code has file 
1d890 73 63 6f 70 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  scope..**.** $Id
1d8a0 3a 20 64 61 74 65 2e 63 2c 76 20 31 2e 36 36 20  : date.c,v 1.66 
1d8b0 32 30 30 37 2f 30 35 2f 30 38 20 32 31 3a 35 36  2007/05/08 21:56
1d8c0 3a 30 30 20 64 72 68 20 45 78 70 20 24 0a 2a 2a  :00 drh Exp $.**
1d8d0 0a 2a 2a 20 53 51 4c 69 74 65 20 70 72 6f 63 65  .** SQLite proce
1d8e0 73 73 65 73 20 61 6c 6c 20 74 69 6d 65 73 20 61  sses all times a
1d8f0 6e 64 20 64 61 74 65 73 20 61 73 20 4a 75 6c 69  nd dates as Juli
1d900 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 73 2e 20  an Day numbers. 
1d910 20 54 68 65 0a 2a 2a 20 64 61 74 65 73 20 61 6e   The.** dates an
1d920 64 20 74 69 6d 65 73 20 61 72 65 20 73 74 6f 72  d times are stor
1d930 65 64 20 61 73 20 74 68 65 20 6e 75 6d 62 65 72  ed as the number
1d940 20 6f 66 20 64 61 79 73 20 73 69 6e 63 65 20 6e   of days since n
1d950 6f 6f 6e 0a 2a 2a 20 69 6e 20 47 72 65 65 6e 77  oon.** in Greenw
1d960 69 63 68 20 6f 6e 20 4e 6f 76 65 6d 62 65 72 20  ich on November 
1d970 32 34 2c 20 34 37 31 34 20 42 2e 43 2e 20 61 63  24, 4714 B.C. ac
1d980 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 47  cording to the G
1d990 72 65 67 6f 72 69 61 6e 0a 2a 2a 20 63 61 6c 65  regorian.** cale
1d9a0 6e 64 61 72 20 73 79 73 74 65 6d 2e 20 0a 2a 2a  ndar system. .**
1d9b0 0a 2a 2a 20 31 39 37 30 2d 30 31 2d 30 31 20 30  .** 1970-01-01 0
1d9c0 30 3a 30 30 3a 30 30 20 69 73 20 4a 44 20 32 34  0:00:00 is JD 24
1d9d0 34 30 35 38 37 2e 35 0a 2a 2a 20 32 30 30 30 2d  40587.5.** 2000-
1d9e0 30 31 2d 30 31 20 30 30 3a 30 30 3a 30 30 20 69  01-01 00:00:00 i
1d9f0 73 20 4a 44 20 32 34 35 31 35 34 34 2e 35 0a 2a  s JD 2451544.5.*
1da00 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d  *.** This implem
1da10 65 6e 74 69 6f 6e 20 72 65 71 75 69 72 65 73 20  ention requires 
1da20 79 65 61 72 73 20 74 6f 20 62 65 20 65 78 70 72  years to be expr
1da30 65 73 73 65 64 20 61 73 20 61 20 34 2d 64 69 67  essed as a 4-dig
1da40 69 74 20 6e 75 6d 62 65 72 0a 2a 2a 20 77 68 69  it number.** whi
1da50 63 68 20 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e  ch means that on
1da60 6c 79 20 64 61 74 65 73 20 62 65 74 77 65 65 6e  ly dates between
1da70 20 30 30 30 30 2d 30 31 2d 30 31 20 61 6e 64 20   0000-01-01 and 
1da80 39 39 39 39 2d 31 32 2d 33 31 20 63 61 6e 0a 2a  9999-12-31 can.*
1da90 2a 20 62 65 20 72 65 70 72 65 73 65 6e 74 65 64  * be represented
1daa0 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 20 6a 75  , even though ju
1dab0 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 73  lian day numbers
1dac0 20 61 6c 6c 6f 77 20 61 20 6d 75 63 68 20 77 69   allow a much wi
1dad0 64 65 72 0a 2a 2a 20 72 61 6e 67 65 20 6f 66 20  der.** range of 
1dae0 64 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  dates..**.** The
1daf0 20 47 72 65 67 6f 72 69 61 6e 20 63 61 6c 65 6e   Gregorian calen
1db00 64 61 72 20 73 79 73 74 65 6d 20 69 73 20 75 73  dar system is us
1db10 65 64 20 66 6f 72 20 61 6c 6c 20 64 61 74 65 73  ed for all dates
1db20 20 61 6e 64 20 74 69 6d 65 73 2c 0a 2a 2a 20 65   and times,.** e
1db30 76 65 6e 20 74 68 6f 73 65 20 74 68 61 74 20 70  ven those that p
1db40 72 65 64 61 74 65 20 74 68 65 20 47 72 65 67 6f  redate the Grego
1db50 72 69 61 6e 20 63 61 6c 65 6e 64 61 72 2e 20 20  rian calendar.  
1db60 48 69 73 74 6f 72 69 61 6e 73 20 75 73 75 61 6c  Historians usual
1db70 6c 79 0a 2a 2a 20 75 73 65 20 74 68 65 20 4a 75  ly.** use the Ju
1db80 6c 69 61 6e 20 63 61 6c 65 6e 64 61 72 20 66 6f  lian calendar fo
1db90 72 20 64 61 74 65 73 20 70 72 69 6f 72 20 74 6f  r dates prior to
1dba0 20 31 35 38 32 2d 31 30 2d 31 35 20 61 6e 64 20   1582-10-15 and 
1dbb0 66 6f 72 20 73 6f 6d 65 0a 2a 2a 20 64 61 74 65  for some.** date
1dbc0 73 20 61 66 74 65 72 77 61 72 64 73 2c 20 64 65  s afterwards, de
1dbd0 70 65 6e 64 69 6e 67 20 6f 6e 20 6c 6f 63 61 6c  pending on local
1dbe0 65 2e 20 20 42 65 77 61 72 65 20 6f 66 20 74 68  e.  Beware of th
1dbf0 69 73 20 64 69 66 66 65 72 65 6e 63 65 2e 0a 2a  is difference..*
1dc00 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 76 65 72 73  *.** The convers
1dc10 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 73 20 61  ion algorithms a
1dc20 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62  re implemented b
1dc30 61 73 65 64 20 6f 6e 20 64 65 73 63 72 69 70 74  ased on descript
1dc40 69 6f 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 66  ions.** in the f
1dc50 6f 6c 6c 6f 77 69 6e 67 20 74 65 78 74 3a 0a 2a  ollowing text:.*
1dc60 2a 0a 2a 2a 20 20 20 20 20 20 4a 65 61 6e 20 4d  *.**      Jean M
1dc70 65 65 75 73 0a 2a 2a 20 20 20 20 20 20 41 73 74  eeus.**      Ast
1dc80 72 6f 6e 6f 6d 69 63 61 6c 20 41 6c 67 6f 72 69  ronomical Algori
1dc90 74 68 6d 73 2c 20 32 6e 64 20 45 64 69 74 69 6f  thms, 2nd Editio
1dca0 6e 2c 20 31 39 39 38 0a 2a 2a 20 20 20 20 20 20  n, 1998.**      
1dcb0 49 53 42 4d 20 30 2d 39 34 33 33 39 36 2d 36 31  ISBM 0-943396-61
1dcc0 2d 31 0a 2a 2a 20 20 20 20 20 20 57 69 6c 6c 6d  -1.**      Willm
1dcd0 61 6e 6e 2d 42 65 6c 6c 2c 20 49 6e 63 0a 2a 2a  ann-Bell, Inc.**
1dce0 20 20 20 20 20 20 52 69 63 68 6d 6f 6e 64 2c 20        Richmond, 
1dcf0 56 69 72 67 69 6e 69 61 20 28 55 53 41 29 0a 2a  Virginia (USA).*
1dd00 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
1dd10 2a 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 74 65  * Include sqlite
1dd20 49 6e 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 64  Int.h in the mid
1dd30 64 6c 65 20 6f 66 20 64 61 74 65 2e 63 20 2a 2a  dle of date.c **
1dd40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dd50 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
1dd60 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73 71 6c  * Begin file sql
1dd70 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  iteInt.h *******
1dd80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dd90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dda0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
1ddb0 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20  tember 15.**.** 
1ddc0 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
1ddd0 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
1dde0 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
1ddf0 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
1de00 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
1de10 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
1de20 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
1de30 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
1de40 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
1de50 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
1de60 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
1de70 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
1de80 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
1de90 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
1dea0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
1deb0 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
1dec0 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
1ded0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1def0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1df00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1df10 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 49 6e 74 65  ********.** Inte
1df20 72 6e 61 6c 20 69 6e 74 65 72 66 61 63 65 20 64  rnal interface d
1df30 65 66 69 6e 69 74 69 6f 6e 73 20 66 6f 72 20 53  efinitions for S
1df40 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 23  QLite..**.** @(#
1df50 29 20 24 49 64 3a 20 73 71 6c 69 74 65 49 6e 74  ) $Id: sqliteInt
1df60 2e 68 2c 76 20 31 2e 35 38 30 20 32 30 30 37 2f  .h,v 1.580 2007/
1df70 30 37 2f 32 33 20 31 39 3a 33 31 3a 31 37 20 64  07/23 19:31:17 d
1df80 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e  rh Exp $.*/.#ifn
1df90 64 65 66 20 5f 53 51 4c 49 54 45 49 4e 54 5f 48  def _SQLITEINT_H
1dfa0 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54  _.#define _SQLIT
1dfb0 45 49 4e 54 5f 48 5f 0a 2f 2a 2a 2a 2a 2a 2a 2a  EINT_H_./*******
1dfc0 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20  ******* Include 
1dfd0 73 71 6c 69 74 65 4c 69 6d 69 74 2e 68 20 69 6e  sqliteLimit.h in
1dfe0 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73   the middle of s
1dff0 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a  qliteInt.h *****
1e000 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
1e010 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
1e020 6c 65 20 73 71 6c 69 74 65 4c 69 6d 69 74 2e 68  le sqliteLimit.h
1e030 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1e040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e050 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
1e060 30 37 20 4d 61 79 20 37 0a 2a 2a 0a 2a 2a 20 54  07 May 7.**.** T
1e070 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
1e080 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
1e090 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
1e0a0 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
1e0b0 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
1e0c0 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
1e0d0 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
1e0e0 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
1e0f0 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
1e100 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
1e110 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
1e120 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
1e130 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
1e140 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
1e150 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
1e160 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
1e170 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
1e180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e1a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e1b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e1c0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 0a 2a 2a 20 54  *******.** .** T
1e1d0 68 69 73 20 66 69 6c 65 20 64 65 66 69 6e 65 73  his file defines
1e1e0 20 76 61 72 69 6f 75 73 20 6c 69 6d 69 74 73 20   various limits 
1e1f0 6f 66 20 77 68 61 74 20 53 51 4c 69 74 65 20 63  of what SQLite c
1e200 61 6e 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a  an process..**.*
1e210 2a 20 40 28 23 29 20 24 49 64 3a 20 73 71 6c 69  * @(#) $Id: sqli
1e220 74 65 4c 69 6d 69 74 2e 68 2c 76 20 31 2e 31 20  teLimit.h,v 1.1 
1e230 32 30 30 37 2f 30 36 2f 31 39 20 31 35 3a 32 33  2007/06/19 15:23
1e240 3a 34 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :48 drh Exp $.*/
1e250 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69  ../*.** The maxi
1e260 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 61 20  mum length of a 
1e270 54 45 58 54 20 6f 72 20 42 4c 4f 42 20 69 6e 20  TEXT or BLOB in 
1e280 62 79 74 65 73 2e 20 20 20 54 68 69 73 20 61 6c  bytes.   This al
1e290 73 6f 0a 2a 2a 20 6c 69 6d 69 74 73 20 74 68 65  so.** limits the
1e2a0 20 73 69 7a 65 20 6f 66 20 61 20 72 6f 77 20 69   size of a row i
1e2b0 6e 20 61 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  n a table or ind
1e2c0 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 68 61  ex..**.** The ha
1e2d0 72 64 20 6c 69 6d 69 74 20 69 73 20 74 68 65 20  rd limit is the 
1e2e0 61 62 69 6c 69 74 79 20 6f 66 20 61 20 33 32 2d  ability of a 32-
1e2f0 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67  bit signed integ
1e300 65 72 0a 2a 2a 20 74 6f 20 63 6f 75 6e 74 20 74  er.** to count t
1e310 68 65 20 73 69 7a 65 3a 20 32 5e 33 31 2d 31 20  he size: 2^31-1 
1e320 6f 72 20 32 31 34 37 34 38 33 36 34 37 2e 0a 2a  or 2147483647..*
1e330 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
1e340 5f 4d 41 58 5f 4c 45 4e 47 54 48 0a 23 20 64 65  _MAX_LENGTH.# de
1e350 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f  fine SQLITE_MAX_
1e360 4c 45 4e 47 54 48 20 31 30 30 30 30 30 30 30 30  LENGTH 100000000
1e370 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  0.#endif../*.** 
1e380 54 68 69 73 20 69 73 20 74 68 65 20 6d 61 78 69  This is the maxi
1e390 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a  mum number of.**
1e3a0 0a 2a 2a 20 20 20 20 2a 20 43 6f 6c 75 6d 6e 73  .**    * Columns
1e3b0 20 69 6e 20 61 20 74 61 62 6c 65 0a 2a 2a 20 20   in a table.**  
1e3c0 20 20 2a 20 43 6f 6c 75 6d 6e 73 20 69 6e 20 61    * Columns in a
1e3d0 6e 20 69 6e 64 65 78 0a 2a 2a 20 20 20 20 2a 20  n index.**    * 
1e3e0 43 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 76 69 65  Columns in a vie
1e3f0 77 0a 2a 2a 20 20 20 20 2a 20 54 65 72 6d 73 20  w.**    * Terms 
1e400 69 6e 20 74 68 65 20 53 45 54 20 63 6c 61 75 73  in the SET claus
1e410 65 20 6f 66 20 61 6e 20 55 50 44 41 54 45 20 73  e of an UPDATE s
1e420 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 20 20 20 2a  tatement.**    *
1e430 20 54 65 72 6d 73 20 69 6e 20 74 68 65 20 72 65   Terms in the re
1e440 73 75 6c 74 20 73 65 74 20 6f 66 20 61 20 53 45  sult set of a SE
1e450 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 0a 2a  LECT statement.*
1e460 2a 20 20 20 20 2a 20 54 65 72 6d 73 20 69 6e 20  *    * Terms in 
1e470 74 68 65 20 47 52 4f 55 50 20 42 59 20 6f 72 20  the GROUP BY or 
1e480 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 73  ORDER BY clauses
1e490 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61   of a SELECT sta
1e4a0 74 65 6d 65 6e 74 2e 0a 2a 2a 20 20 20 20 2a 20  tement..**    * 
1e4b0 54 65 72 6d 73 20 69 6e 20 74 68 65 20 56 41 4c  Terms in the VAL
1e4c0 55 45 53 20 63 6c 61 75 73 65 20 6f 66 20 61 6e  UES clause of an
1e4d0 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e   INSERT statemen
1e4e0 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 68 61 72 64  t.**.** The hard
1e4f0 20 75 70 70 65 72 20 6c 69 6d 69 74 20 68 65 72   upper limit her
1e500 65 20 69 73 20 33 32 36 37 36 2e 20 20 4d 6f 73  e is 32676.  Mos
1e510 74 20 64 61 74 61 62 61 73 65 20 70 65 6f 70 6c  t database peopl
1e520 65 20 77 69 6c 6c 0a 2a 2a 20 74 65 6c 6c 20 79  e will.** tell y
1e530 6f 75 20 74 68 61 74 20 69 6e 20 61 20 77 65 6c  ou that in a wel
1e540 6c 2d 6e 6f 72 6d 61 6c 69 7a 65 64 20 64 61 74  l-normalized dat
1e550 61 62 61 73 65 2c 20 79 6f 75 20 75 73 75 61 6c  abase, you usual
1e560 6c 79 20 73 68 6f 75 6c 64 0a 2a 2a 20 6e 6f 74  ly should.** not
1e570 20 68 61 76 65 20 6d 6f 72 65 20 74 68 61 6e 20   have more than 
1e580 61 20 64 6f 7a 65 6e 20 6f 72 20 73 6f 20 63 6f  a dozen or so co
1e590 6c 75 6d 6e 73 20 69 6e 20 61 6e 79 20 74 61 62  lumns in any tab
1e5a0 6c 65 2e 20 20 41 6e 64 20 69 66 0a 2a 2a 20 74  le.  And if.** t
1e5b0 68 61 74 20 69 73 20 74 68 65 20 63 61 73 65 2c  hat is the case,
1e5c0 20 74 68 65 72 65 20 69 73 20 6e 6f 20 70 6f 69   there is no poi
1e5d0 6e 74 20 69 6e 20 68 61 76 69 6e 67 20 6d 6f 72  nt in having mor
1e5e0 65 20 74 68 61 6e 20 61 20 66 65 77 0a 2a 2a 20  e than a few.** 
1e5f0 64 6f 7a 65 6e 20 76 61 6c 75 65 73 20 69 6e 20  dozen values in 
1e600 61 6e 79 20 6f 66 20 74 68 65 20 6f 74 68 65 72  any of the other
1e610 20 73 69 74 75 61 74 69 6f 6e 73 20 64 65 73 63   situations desc
1e620 72 69 62 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a  ribed above..*/.
1e630 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d  #ifndef SQLITE_M
1e640 41 58 5f 43 4f 4c 55 4d 4e 0a 23 20 64 65 66 69  AX_COLUMN.# defi
1e650 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f  ne SQLITE_MAX_CO
1e660 4c 55 4d 4e 20 32 30 30 30 0a 23 65 6e 64 69 66  LUMN 2000.#endif
1e670 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69  ../*.** The maxi
1e680 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 61 20  mum length of a 
1e690 73 69 6e 67 6c 65 20 53 51 4c 20 73 74 61 74 65  single SQL state
1e6a0 6d 65 6e 74 20 69 6e 20 62 79 74 65 73 2e 0a 2a  ment in bytes..*
1e6b0 2a 20 54 68 65 20 68 61 72 64 20 6c 69 6d 69 74  * The hard limit
1e6c0 20 68 65 72 65 20 69 73 20 74 68 65 20 73 61 6d   here is the sam
1e6d0 65 20 61 73 20 53 51 4c 49 54 45 5f 4d 41 58 5f  e as SQLITE_MAX_
1e6e0 4c 45 4e 47 54 48 2e 0a 2a 2f 0a 23 69 66 6e 64  LENGTH..*/.#ifnd
1e6f0 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51  ef SQLITE_MAX_SQ
1e700 4c 5f 4c 45 4e 47 54 48 0a 23 20 64 65 66 69 6e  L_LENGTH.# defin
1e710 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c  e SQLITE_MAX_SQL
1e720 5f 4c 45 4e 47 54 48 20 31 30 30 30 30 30 30 0a  _LENGTH 1000000.
1e730 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
1e740 65 20 6d 61 78 69 6d 75 6d 20 64 65 70 74 68 20  e maximum depth 
1e750 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  of an expression
1e760 20 74 72 65 65 2e 20 54 68 69 73 20 69 73 20 6c   tree. This is l
1e770 69 6d 69 74 65 64 20 74 6f 20 0a 2a 2a 20 73 6f  imited to .** so
1e780 6d 65 20 65 78 74 65 6e 74 20 62 79 20 53 51 4c  me extent by SQL
1e790 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47  ITE_MAX_SQL_LENG
1e7a0 54 48 2e 20 42 75 74 20 73 6f 6d 65 74 69 6d 65  TH. But sometime
1e7b0 20 79 6f 75 20 6d 69 67 68 74 20 0a 2a 2a 20 77   you might .** w
1e7c0 61 6e 74 20 74 6f 20 70 6c 61 63 65 20 6d 6f 72  ant to place mor
1e7d0 65 20 73 65 76 65 72 65 20 6c 69 6d 69 74 73 20  e severe limits 
1e7e0 6f 6e 20 74 68 65 20 63 6f 6d 70 6c 65 78 69 74  on the complexit
1e7f0 79 20 6f 66 20 61 6e 20 0a 2a 2a 20 65 78 70 72  y of an .** expr
1e800 65 73 73 69 6f 6e 2e 20 41 20 76 61 6c 75 65 20  ession. A value 
1e810 6f 66 20 30 20 28 74 68 65 20 64 65 66 61 75 6c  of 0 (the defaul
1e820 74 29 20 6d 65 61 6e 73 20 64 6f 20 6e 6f 74 20  t) means do not 
1e830 65 6e 66 6f 72 63 65 0a 2a 2a 20 61 6e 79 20 6c  enforce.** any l
1e840 69 6d 69 74 61 74 69 6f 6e 20 6f 6e 20 65 78 70  imitation on exp
1e850 72 65 73 73 69 6f 6e 20 74 72 65 65 20 64 65 70  ression tree dep
1e860 74 68 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  th..*/.#ifndef S
1e870 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44  QLITE_MAX_EXPR_D
1e880 45 50 54 48 0a 23 20 64 65 66 69 6e 65 20 53 51  EPTH.# define SQ
1e890 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45  LITE_MAX_EXPR_DE
1e8a0 50 54 48 20 31 30 30 30 0a 23 65 6e 64 69 66 0a  PTH 1000.#endif.
1e8b0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d  ./*.** The maxim
1e8c0 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72  um number of ter
1e8d0 6d 73 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64  ms in a compound
1e8e0 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
1e8f0 74 2e 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 67  t..** The code g
1e900 65 6e 65 72 61 74 6f 72 20 66 6f 72 20 63 6f 6d  enerator for com
1e910 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 73 74 61  pound SELECT sta
1e920 74 65 6d 65 6e 74 73 20 64 6f 65 73 20 6f 6e 65  tements does one
1e930 0a 2a 2a 20 6c 65 76 65 6c 20 6f 66 20 72 65 63  .** level of rec
1e940 75 72 73 69 6f 6e 20 66 6f 72 20 65 61 63 68 20  ursion for each 
1e950 74 65 72 6d 2e 20 20 41 20 73 74 61 63 6b 20 6f  term.  A stack o
1e960 76 65 72 66 6c 6f 77 20 63 61 6e 20 72 65 73 75  verflow can resu
1e970 6c 74 0a 2a 2a 20 69 66 20 74 68 65 20 6e 75 6d  lt.** if the num
1e980 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 73 20  ber of terms is 
1e990 74 6f 6f 20 6c 61 72 67 65 2e 20 20 49 6e 20 70  too large.  In p
1e9a0 72 61 63 74 69 63 65 2c 20 6d 6f 73 74 20 53 51  ractice, most SQ
1e9b0 4c 0a 2a 2a 20 6e 65 76 65 72 20 68 61 73 20 6d  L.** never has m
1e9c0 6f 72 65 20 74 68 61 6e 20 33 20 6f 72 20 34 20  ore than 3 or 4 
1e9d0 74 65 72 6d 73 2e 20 20 55 73 65 20 61 20 76 61  terms.  Use a va
1e9e0 6c 75 65 20 6f 66 20 30 20 74 6f 20 64 69 73 61  lue of 0 to disa
1e9f0 62 6c 65 0a 2a 2a 20 61 6e 79 20 6c 69 6d 69 74  ble.** any limit
1ea00 20 6f 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   on the number o
1ea10 66 20 74 65 72 6d 73 20 69 6e 20 61 20 63 6f 6d  f terms in a com
1ea20 70 6f 75 6e 74 20 53 45 4c 45 43 54 2e 0a 2a 2f  pount SELECT..*/
1ea30 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1ea40 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c  MAX_COMPOUND_SEL
1ea50 45 43 54 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  ECT.# define SQL
1ea60 49 54 45 5f 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44  ITE_MAX_COMPOUND
1ea70 5f 53 45 4c 45 43 54 20 35 30 30 0a 23 65 6e 64  _SELECT 500.#end
1ea80 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61  if../*.** The ma
1ea90 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20  ximum number of 
1eaa0 6f 70 63 6f 64 65 73 20 69 6e 20 61 20 56 44 42  opcodes in a VDB
1eab0 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 20 4e 6f  E program..** No
1eac0 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 66 6f  t currently enfo
1ead0 72 63 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  rced..*/.#ifndef
1eae0 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 44 42 45   SQLITE_MAX_VDBE
1eaf0 5f 4f 50 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  _OP.# define SQL
1eb00 49 54 45 5f 4d 41 58 5f 56 44 42 45 5f 4f 50 20  ITE_MAX_VDBE_OP 
1eb10 32 35 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a  25000.#endif../*
1eb20 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20  .** The maximum 
1eb30 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65  number of argume
1eb40 6e 74 73 20 74 6f 20 61 6e 20 53 51 4c 20 66 75  nts to an SQL fu
1eb50 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64  nction..*/.#ifnd
1eb60 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55  ef SQLITE_MAX_FU
1eb70 4e 43 54 49 4f 4e 5f 41 52 47 0a 23 20 64 65 66  NCTION_ARG.# def
1eb80 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46  ine SQLITE_MAX_F
1eb90 55 4e 43 54 49 4f 4e 5f 41 52 47 20 31 30 30 0a  UNCTION_ARG 100.
1eba0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
1ebb0 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  e maximum number
1ebc0 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61   of in-memory pa
1ebd0 67 65 73 20 74 6f 20 75 73 65 20 66 6f 72 20 74  ges to use for t
1ebe0 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
1ebf0 0a 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 66 6f  .** table and fo
1ec00 72 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  r temporary tabl
1ec10 65 73 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f  es.  The SQLITE_
1ec20 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49  DEFAULT_CACHE_SI
1ec30 5a 45 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  ZE.*/.#ifndef SQ
1ec40 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43  LITE_DEFAULT_CAC
1ec50 48 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65  HE_SIZE.# define
1ec60 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
1ec70 43 41 43 48 45 5f 53 49 5a 45 20 20 32 30 30 30  CACHE_SIZE  2000
1ec80 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20  .#endif.#ifndef 
1ec90 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 54  SQLITE_DEFAULT_T
1eca0 45 4d 50 5f 43 41 43 48 45 5f 53 49 5a 45 0a 23  EMP_CACHE_SIZE.#
1ecb0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44   define SQLITE_D
1ecc0 45 46 41 55 4c 54 5f 54 45 4d 50 5f 43 41 43 48  EFAULT_TEMP_CACH
1ecd0 45 5f 53 49 5a 45 20 20 35 30 30 0a 23 65 6e 64  E_SIZE  500.#end
1ece0 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61  if../*.** The ma
1ecf0 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20  ximum number of 
1ed00 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
1ed10 65 73 2e 20 20 54 68 69 73 20 6d 75 73 74 20 62  es.  This must b
1ed20 65 20 61 74 20 6c 65 61 73 74 20 32 0a 2a 2a 20  e at least 2.** 
1ed30 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 75 70 70  in order to supp
1ed40 6f 72 74 20 74 68 65 20 6d 61 69 6e 20 64 61 74  ort the main dat
1ed50 61 62 61 73 65 20 66 69 6c 65 20 28 30 29 20 61  abase file (0) a
1ed60 6e 64 20 74 68 65 20 66 69 6c 65 20 75 73 65 64  nd the file used
1ed70 20 74 6f 0a 2a 2a 20 68 6f 6c 64 20 74 65 6d 70   to.** hold temp
1ed80 6f 72 61 72 79 20 74 61 62 6c 65 73 20 28 31 29  orary tables (1)
1ed90 2e 20 20 41 6e 64 20 69 74 20 6d 75 73 74 20 62  .  And it must b
1eda0 65 20 6c 65 73 73 20 74 68 61 6e 20 33 32 20 62  e less than 32 b
1edb0 65 63 61 75 73 65 0a 2a 2a 20 77 65 20 75 73 65  ecause.** we use
1edc0 20 61 20 62 69 74 6d 61 73 6b 20 6f 66 20 64 61   a bitmask of da
1edd0 74 61 62 61 73 65 73 20 77 69 74 68 20 61 20 75  tabases with a u
1ede0 33 32 20 69 6e 20 70 6c 61 63 65 73 20 28 66 6f  32 in places (fo
1edf0 72 20 65 78 61 6d 70 6c 65 0a 2a 2a 20 74 68 65  r example.** the
1ee00 20 50 61 72 73 65 2e 63 6f 6f 6b 69 65 4d 61 73   Parse.cookieMas
1ee10 6b 20 66 69 65 6c 64 29 2e 0a 2a 2f 0a 23 69 66  k field)..*/.#if
1ee20 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  ndef SQLITE_MAX_
1ee30 41 54 54 41 43 48 45 44 0a 23 20 64 65 66 69 6e  ATTACHED.# defin
1ee40 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54  e SQLITE_MAX_ATT
1ee50 41 43 48 45 44 20 31 30 0a 23 65 6e 64 69 66 0a  ACHED 10.#endif.
1ee60 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69  ../*.** The maxi
1ee70 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 61 20 3f  mum value of a ?
1ee80 6e 6e 6e 20 77 69 6c 64 63 61 72 64 20 74 68 61  nnn wildcard tha
1ee90 74 20 74 68 65 20 70 61 72 73 65 72 20 77 69 6c  t the parser wil
1eea0 6c 20 61 63 63 65 70 74 2e 0a 2a 2f 0a 23 69 66  l accept..*/.#if
1eeb0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  ndef SQLITE_MAX_
1eec0 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 0a  VARIABLE_NUMBER.
1eed0 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
1eee0 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d  MAX_VARIABLE_NUM
1eef0 42 45 52 20 39 39 39 0a 23 65 6e 64 69 66 0a 0a  BER 999.#endif..
1ef00 2f 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c  /*.** The defaul
1ef10 74 20 73 69 7a 65 20 6f 66 20 61 20 64 61 74 61  t size of a data
1ef20 62 61 73 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 69  base page..*/.#i
1ef30 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46  fndef SQLITE_DEF
1ef40 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 0a 23  AULT_PAGE_SIZE.#
1ef50 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44   define SQLITE_D
1ef60 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45  EFAULT_PAGE_SIZE
1ef70 20 31 30 32 34 0a 23 65 6e 64 69 66 0a 0a 2f 2a   1024.#endif../*
1ef80 20 4d 61 78 69 6d 75 6d 20 70 61 67 65 20 73 69   Maximum page si
1ef90 7a 65 2e 20 20 54 68 65 20 75 70 70 65 72 20 62  ze.  The upper b
1efa0 6f 75 6e 64 20 6f 6e 20 74 68 69 73 20 76 61 6c  ound on this val
1efb0 75 65 20 69 73 20 33 32 37 36 38 2e 20 20 54 68  ue is 32768.  Th
1efc0 69 73 20 61 20 6c 69 6d 69 74 0a 2a 2a 20 69 6d  is a limit.** im
1efd0 70 6f 73 65 64 20 62 79 20 74 68 65 20 6e 65 63  posed by the nec
1efe0 65 73 73 69 74 79 20 6f 66 20 73 74 6f 72 69 6e  essity of storin
1eff0 67 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 61  g the value in a
1f000 20 32 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64   2-byte unsigned
1f010 20 69 6e 74 65 67 65 72 0a 2a 2a 20 61 6e 64 20   integer.** and 
1f020 74 68 65 20 66 61 63 74 20 74 68 61 74 20 74 68  the fact that th
1f030 65 20 70 61 67 65 20 73 69 7a 65 20 6d 75 73 74  e page size must
1f040 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 32   be a power of 2
1f050 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
1f060 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a  ITE_MAX_PAGE_SIZ
1f070 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  E.# define SQLIT
1f080 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 20  E_MAX_PAGE_SIZE 
1f090 33 32 37 36 38 0a 23 65 6e 64 69 66 0a 0a 2f 2a  32768.#endif../*
1f0a0 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62  .** Maximum numb
1f0b0 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 6f  er of pages in o
1f0c0 6e 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ne database file
1f0d0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  ..**.** This is 
1f0e0 72 65 61 6c 6c 79 20 6a 75 73 74 20 74 68 65 20  really just the 
1f0f0 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f  default value fo
1f100 72 20 74 68 65 20 6d 61 78 5f 70 61 67 65 5f 63  r the max_page_c
1f110 6f 75 6e 74 20 70 72 61 67 6d 61 2e 0a 2a 2a 20  ount pragma..** 
1f120 54 68 69 73 20 76 61 6c 75 65 20 63 61 6e 20 62  This value can b
1f130 65 20 6c 6f 77 65 72 65 64 20 28 6f 72 20 72 61  e lowered (or ra
1f140 69 73 65 64 29 20 61 74 20 72 75 6e 2d 74 69 6d  ised) at run-tim
1f150 65 20 75 73 69 6e 67 20 74 68 61 74 20 74 68 65  e using that the
1f160 0a 2a 2a 20 6d 61 78 5f 70 61 67 65 5f 63 6f 75  .** max_page_cou
1f170 6e 74 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66  nt macro..*/.#if
1f180 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  ndef SQLITE_MAX_
1f190 50 41 47 45 5f 43 4f 55 4e 54 0a 23 20 64 65 66  PAGE_COUNT.# def
1f1a0 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50  ine SQLITE_MAX_P
1f1b0 41 47 45 5f 43 4f 55 4e 54 20 31 30 37 33 37 34  AGE_COUNT 107374
1f1c0 31 38 32 33 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  1823.#endif../*.
1f1d0 2a 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74  ** Maximum lengt
1f1e0 68 20 28 69 6e 20 62 79 74 65 73 29 20 6f 66 20  h (in bytes) of 
1f1f0 74 68 65 20 70 61 74 74 65 72 6e 20 69 6e 20 61  the pattern in a
1f200 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 0a 2a 2a   LIKE or GLOB.**
1f210 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 23 69   operator..*/.#i
1f220 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58  fndef SQLITE_MAX
1f230 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45  _LIKE_PATTERN_LE
1f240 4e 47 54 48 0a 23 20 64 65 66 69 6e 65 20 53 51  NGTH.# define SQ
1f250 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41  LITE_MAX_LIKE_PA
1f260 54 54 45 52 4e 5f 4c 45 4e 47 54 48 20 35 30 30  TTERN_LENGTH 500
1f270 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a  00.#endif../****
1f280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
1f290 66 20 73 71 6c 69 74 65 4c 69 6d 69 74 2e 68 20  f sqliteLimit.h 
1f2a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f2b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f2c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
1f2d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
1f2e0 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
1f2f0 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74  eft off in sqlit
1f300 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eInt.h *********
1f310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 23 69 66  *********/...#if
1f320 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
1f330 54 43 4c 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  TCL) || defined(
1f340 54 43 4c 53 48 29 0a 23 20 69 6e 63 6c 75 64 65  TCLSH).# include
1f350 20 3c 74 63 6c 2e 68 3e 0a 23 65 6e 64 69 66 0a   <tcl.h>.#endif.
1f360 0a 2f 2a 0a 2a 2a 20 4d 61 6e 79 20 70 65 6f 70  ./*.** Many peop
1f370 6c 65 20 61 72 65 20 66 61 69 6c 69 6e 67 20 74  le are failing t
1f380 6f 20 73 65 74 20 2d 44 4e 44 45 42 55 47 3d 31  o set -DNDEBUG=1
1f390 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20   when compiling 
1f3a0 53 51 4c 69 74 65 2e 0a 2a 2a 20 53 65 74 74 69  SQLite..** Setti
1f3b0 6e 67 20 4e 44 45 42 55 47 20 6d 61 6b 65 73 20  ng NDEBUG makes 
1f3c0 74 68 65 20 63 6f 64 65 20 73 6d 61 6c 6c 65 72  the code smaller
1f3d0 20 61 6e 64 20 72 75 6e 20 66 61 73 74 65 72 2e   and run faster.
1f3e0 20 20 53 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69    So the followi
1f3f0 6e 67 0a 2a 2a 20 6c 69 6e 65 73 20 61 72 65 20  ng.** lines are 
1f400 61 64 64 65 64 20 74 6f 20 61 75 74 6f 6d 61 74  added to automat
1f410 69 63 61 6c 6c 79 20 73 65 74 20 4e 44 45 42 55  ically set NDEBU
1f420 47 20 75 6e 6c 65 73 73 20 74 68 65 20 2d 44 53  G unless the -DS
1f430 51 4c 49 54 45 5f 44 45 42 55 47 3d 31 0a 2a 2a  QLITE_DEBUG=1.**
1f440 20 6f 70 74 69 6f 6e 20 69 73 20 73 65 74 2e 20   option is set. 
1f450 20 54 68 75 73 20 4e 44 45 42 55 47 20 62 65 63   Thus NDEBUG bec
1f460 6f 6d 65 73 20 61 6e 20 6f 70 74 2d 69 6e 20 72  omes an opt-in r
1f470 61 74 68 65 72 20 74 68 61 6e 20 61 6e 20 6f 70  ather than an op
1f480 74 2d 6f 75 74 0a 2a 2a 20 66 65 61 74 75 72 65  t-out.** feature
1f490 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65  ..*/.#if !define
1f4a0 64 28 4e 44 45 42 55 47 29 20 26 26 20 21 64 65  d(NDEBUG) && !de
1f4b0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42  fined(SQLITE_DEB
1f4c0 55 47 29 20 0a 23 20 64 65 66 69 6e 65 20 4e 44  UG) .# define ND
1f4d0 45 42 55 47 20 31 0a 23 65 6e 64 69 66 0a 0a 2f  EBUG 1.#endif../
1f4e0 2a 0a 2a 2a 20 54 68 65 73 65 20 23 64 65 66 69  *.** These #defi
1f4f0 6e 65 73 20 73 68 6f 75 6c 64 20 65 6e 61 62 6c  nes should enabl
1f500 65 20 3e 32 47 42 20 66 69 6c 65 20 73 75 70 70  e >2GB file supp
1f510 6f 72 74 20 6f 6e 20 50 6f 73 69 78 20 69 66 20  ort on Posix if 
1f520 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c 79 69 6e  the.** underlyin
1f530 67 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74  g operating syst
1f540 65 6d 20 73 75 70 70 6f 72 74 73 20 69 74 2e 20  em supports it. 
1f550 20 49 66 20 74 68 65 20 4f 53 20 6c 61 63 6b 73   If the OS lacks
1f560 0a 2a 2a 20 6c 61 72 67 65 20 66 69 6c 65 20 73  .** large file s
1f570 75 70 70 6f 72 74 2c 20 6f 72 20 69 66 20 74 68  upport, or if th
1f580 65 20 4f 53 20 69 73 20 77 69 6e 64 6f 77 73 2c  e OS is windows,
1f590 20 74 68 65 73 65 20 73 68 6f 75 6c 64 20 62 65   these should be
1f5a0 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 4c   no-ops..**.** L
1f5b0 61 72 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72  arge file suppor
1f5c0 74 20 63 61 6e 20 62 65 20 64 69 73 61 62 6c 65  t can be disable
1f5d0 64 20 75 73 69 6e 67 20 74 68 65 20 2d 44 53 51  d using the -DSQ
1f5e0 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53  LITE_DISABLE_LFS
1f5f0 20 73 77 69 74 63 68 0a 2a 2a 20 6f 6e 20 74 68   switch.** on th
1f600 65 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61  e compiler comma
1f610 6e 64 20 6c 69 6e 65 2e 20 20 54 68 69 73 20 69  nd line.  This i
1f620 73 20 6e 65 63 65 73 73 61 72 79 20 69 66 20 79  s necessary if y
1f630 6f 75 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67  ou are compiling
1f640 0a 2a 2a 20 6f 6e 20 61 20 72 65 63 65 6e 74 20  .** on a recent 
1f650 6d 61 63 68 69 6e 65 20 28 65 78 3a 20 52 65 64  machine (ex: Red
1f660 48 61 74 20 37 2e 32 29 20 62 75 74 20 79 6f 75  Hat 7.2) but you
1f670 20 77 61 6e 74 20 79 6f 75 72 20 63 6f 64 65 20   want your code 
1f680 74 6f 20 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61 6e  to work.** on an
1f690 20 6f 6c 64 65 72 20 6d 61 63 68 69 6e 65 20 28   older machine (
1f6a0 65 78 3a 20 52 65 64 48 61 74 20 36 2e 30 29 2e  ex: RedHat 6.0).
1f6b0 20 20 49 66 20 79 6f 75 20 63 6f 6d 70 69 6c 65    If you compile
1f6c0 20 6f 6e 20 52 65 64 48 61 74 20 37 2e 32 0a 2a   on RedHat 7.2.*
1f6d0 2a 20 77 69 74 68 6f 75 74 20 74 68 69 73 20 6f  * without this o
1f6e0 70 74 69 6f 6e 2c 20 4c 46 53 20 69 73 20 65 6e  ption, LFS is en
1f6f0 61 62 6c 65 2e 20 20 42 75 74 20 4c 46 53 20 64  able.  But LFS d
1f700 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 69 6e  oes not exist in
1f710 20 74 68 65 20 6b 65 72 6e 65 6c 0a 2a 2a 20 69   the kernel.** i
1f720 6e 20 52 65 64 48 61 74 20 36 2e 30 2c 20 73 6f  n RedHat 6.0, so
1f730 20 74 68 65 20 63 6f 64 65 20 77 6f 6e 27 74 20   the code won't 
1f740 77 6f 72 6b 2e 20 20 48 65 6e 63 65 2c 20 66 6f  work.  Hence, fo
1f750 72 20 6d 61 78 69 6d 75 6d 20 62 69 6e 61 72 79  r maximum binary
1f760 0a 2a 2a 20 70 6f 72 74 61 62 69 6c 69 74 79 20  .** portability 
1f770 79 6f 75 20 73 68 6f 75 6c 64 20 6f 6d 69 74 20  you should omit 
1f780 4c 46 53 2e 0a 2a 2a 0a 2a 2a 20 53 69 6d 69 6c  LFS..**.** Simil
1f790 61 72 20 69 73 20 74 72 75 65 20 66 6f 72 20 4d  ar is true for M
1f7a0 61 63 4f 53 2e 20 20 4c 46 53 20 69 73 20 6f 6e  acOS.  LFS is on
1f7b0 6c 79 20 73 75 70 70 6f 72 74 65 64 20 6f 6e 20  ly supported on 
1f7c0 4d 61 63 4f 53 20 39 20 61 6e 64 20 6c 61 74 65  MacOS 9 and late
1f7d0 72 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  r..*/.#ifndef SQ
1f7e0 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53  LITE_DISABLE_LFS
1f7f0 0a 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 47 45  .# define _LARGE
1f800 5f 46 49 4c 45 20 20 20 20 20 20 20 31 0a 23 20  _FILE       1.# 
1f810 69 66 6e 64 65 66 20 5f 46 49 4c 45 5f 4f 46 46  ifndef _FILE_OFF
1f820 53 45 54 5f 42 49 54 53 0a 23 20 20 20 64 65 66  SET_BITS.#   def
1f830 69 6e 65 20 5f 46 49 4c 45 5f 4f 46 46 53 45 54  ine _FILE_OFFSET
1f840 5f 42 49 54 53 20 36 34 0a 23 20 65 6e 64 69 66  _BITS 64.# endif
1f850 0a 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 47 45  .# define _LARGE
1f860 46 49 4c 45 5f 53 4f 55 52 43 45 20 31 0a 23 65  FILE_SOURCE 1.#e
1f870 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ndif../*********
1f880 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 61  ***** Include ha
1f890 73 68 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  sh.h in the midd
1f8a0 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e  le of sqliteInt.
1f8b0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
1f8c0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
1f8d0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
1f8e0 20 68 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a   hash.h ********
1f8f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f910 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31  ****/./*.** 2001
1f920 20 53 65 70 74 65 6d 62 65 72 20 32 32 0a 2a 2a   September 22.**
1f930 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
1f940 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
1f950 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
1f960 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
1f970 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
1f980 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
1f990 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
1f9a0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
1f9b0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
1f9c0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
1f9d0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
1f9e0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
1f9f0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
1fa00 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
1fa10 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
1fa20 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
1fa30 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
1fa40 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
1fa50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fa60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fa70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fa80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
1fa90 54 68 69 73 20 69 73 20 74 68 65 20 68 65 61 64  This is the head
1faa0 65 72 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20  er file for the 
1fab0 67 65 6e 65 72 69 63 20 68 61 73 68 2d 74 61 62  generic hash-tab
1fac0 6c 65 20 69 6d 70 6c 65 6d 65 6e 61 74 69 6f 6e  le implemenation
1fad0 0a 2a 2a 20 75 73 65 64 20 69 6e 20 53 51 4c 69  .** used in SQLi
1fae0 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68  te..**.** $Id: h
1faf0 61 73 68 2e 68 2c 76 20 31 2e 39 20 32 30 30 36  ash.h,v 1.9 2006
1fb00 2f 30 32 2f 31 34 20 31 30 3a 34 38 3a 33 39 20  /02/14 10:48:39 
1fb10 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20  danielk1977 Exp 
1fb20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51  $.*/.#ifndef _SQ
1fb30 4c 49 54 45 5f 48 41 53 48 5f 48 5f 0a 23 64 65  LITE_HASH_H_.#de
1fb40 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f 48 41 53  fine _SQLITE_HAS
1fb50 48 5f 48 5f 0a 0a 2f 2a 20 46 6f 72 77 61 72 64  H_H_../* Forward
1fb60 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66   declarations of
1fb70 20 73 74 72 75 63 74 75 72 65 73 2e 20 2a 2f 0a   structures. */.
1fb80 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 48  typedef struct H
1fb90 61 73 68 20 48 61 73 68 3b 0a 74 79 70 65 64 65  ash Hash;.typede
1fba0 66 20 73 74 72 75 63 74 20 48 61 73 68 45 6c 65  f struct HashEle
1fbb0 6d 20 48 61 73 68 45 6c 65 6d 3b 0a 0a 2f 2a 20  m HashElem;../* 
1fbc0 41 20 63 6f 6d 70 6c 65 74 65 20 68 61 73 68 20  A complete hash 
1fbd0 74 61 62 6c 65 20 69 73 20 61 6e 20 69 6e 73 74  table is an inst
1fbe0 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
1fbf0 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e  owing structure.
1fc00 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 6e 61 6c  .** The internal
1fc10 73 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  s of this struct
1fc20 75 72 65 20 61 72 65 20 69 6e 74 65 6e 64 65 64  ure are intended
1fc30 20 74 6f 20 62 65 20 6f 70 61 71 75 65 20 2d 2d   to be opaque --
1fc40 20 63 6c 69 65 6e 74 0a 2a 2a 20 63 6f 64 65 20   client.** code 
1fc50 73 68 6f 75 6c 64 20 6e 6f 74 20 61 74 74 65 6d  should not attem
1fc60 70 74 20 74 6f 20 61 63 63 65 73 73 20 6f 72 20  pt to access or 
1fc70 6d 6f 64 69 66 79 20 74 68 65 20 66 69 65 6c 64  modify the field
1fc80 73 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  s of this struct
1fc90 75 72 65 0a 2a 2a 20 64 69 72 65 63 74 6c 79 2e  ure.** directly.
1fca0 20 20 43 68 61 6e 67 65 20 74 68 69 73 20 73 74    Change this st
1fcb0 72 75 63 74 75 72 65 20 6f 6e 6c 79 20 62 79 20  ructure only by 
1fcc0 75 73 69 6e 67 20 74 68 65 20 72 6f 75 74 69 6e  using the routin
1fcd0 65 73 20 62 65 6c 6f 77 2e 0a 2a 2a 20 48 6f 77  es below..** How
1fce0 65 76 65 72 2c 20 6d 61 6e 79 20 6f 66 20 74 68  ever, many of th
1fcf0 65 20 22 70 72 6f 63 65 64 75 72 65 73 22 20 61  e "procedures" a
1fd00 6e 64 20 22 66 75 6e 63 74 69 6f 6e 73 22 20 66  nd "functions" f
1fd10 6f 72 20 6d 6f 64 69 66 79 69 6e 67 20 61 6e 64  or modifying and
1fd20 0a 2a 2a 20 61 63 63 65 73 73 69 6e 67 20 74 68  .** accessing th
1fd30 69 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65  is structure are
1fd40 20 72 65 61 6c 6c 79 20 6d 61 63 72 6f 73 2c 20   really macros, 
1fd50 73 6f 20 77 65 20 63 61 6e 27 74 20 72 65 61 6c  so we can't real
1fd60 6c 79 20 6d 61 6b 65 0a 2a 2a 20 74 68 69 73 20  ly make.** this 
1fd70 73 74 72 75 63 74 75 72 65 20 6f 70 61 71 75 65  structure opaque
1fd80 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 48 61 73 68  ..*/.struct Hash
1fd90 20 7b 0a 20 20 63 68 61 72 20 6b 65 79 43 6c 61   {.  char keyCla
1fda0 73 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ss;          /* 
1fdb0 53 51 4c 49 54 45 5f 48 41 53 48 5f 49 4e 54 2c  SQLITE_HASH_INT,
1fdc0 20 5f 50 4f 49 4e 54 45 52 2c 20 5f 53 54 52 49   _POINTER, _STRI
1fdd0 4e 47 2c 20 5f 42 49 4e 41 52 59 20 2a 2f 0a 20  NG, _BINARY */. 
1fde0 20 63 68 61 72 20 63 6f 70 79 4b 65 79 3b 20 20   char copyKey;  
1fdf0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
1fe00 20 69 66 20 63 6f 70 79 20 6f 66 20 6b 65 79 20   if copy of key 
1fe10 6d 61 64 65 20 6f 6e 20 69 6e 73 65 72 74 20 2a  made on insert *
1fe20 2f 0a 20 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20  /.  int count;  
1fe30 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
1fe40 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
1fe50 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65 20 2a   in this table *
1fe60 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 66 69  /.  HashElem *fi
1fe70 72 73 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  rst;        /* T
1fe80 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74  he first element
1fe90 20 6f 66 20 74 68 65 20 61 72 72 61 79 20 2a 2f   of the array */
1fea0 0a 20 20 76 6f 69 64 20 2a 28 2a 78 4d 61 6c 6c  .  void *(*xMall
1feb0 6f 63 29 28 69 6e 74 29 3b 20 20 2f 2a 20 6d 61  oc)(int);  /* ma
1fec0 6c 6c 6f 63 28 29 20 66 75 6e 63 74 69 6f 6e 20  lloc() function 
1fed0 74 6f 20 75 73 65 20 2a 2f 0a 20 20 76 6f 69 64  to use */.  void
1fee0 20 28 2a 78 46 72 65 65 29 28 76 6f 69 64 20 2a   (*xFree)(void *
1fef0 29 3b 20 20 2f 2a 20 66 72 65 65 28 29 20 66 75  );  /* free() fu
1ff00 6e 63 74 69 6f 6e 20 74 6f 20 75 73 65 20 2a 2f  nction to use */
1ff10 0a 20 20 69 6e 74 20 68 74 73 69 7a 65 3b 20 20  .  int htsize;  
1ff20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
1ff30 6d 62 65 72 20 6f 66 20 62 75 63 6b 65 74 73 20  mber of buckets 
1ff40 69 6e 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  in the hash tabl
1ff50 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 5f 68  e */.  struct _h
1ff60 74 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f  t {            /
1ff70 2a 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  * the hash table
1ff80 20 2a 2f 0a 20 20 20 20 69 6e 74 20 63 6f 75 6e   */.    int coun
1ff90 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
1ffa0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
1ffb0 74 72 69 65 73 20 77 69 74 68 20 74 68 69 73 20  tries with this 
1ffc0 68 61 73 68 20 2a 2f 0a 20 20 20 20 48 61 73 68  hash */.    Hash
1ffd0 45 6c 65 6d 20 2a 63 68 61 69 6e 3b 20 20 20 20  Elem *chain;    
1ffe0 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
1fff0 74 6f 20 66 69 72 73 74 20 65 6e 74 72 79 20 77  to first entry w
20000 69 74 68 20 74 68 69 73 20 68 61 73 68 20 2a 2f  ith this hash */
20010 0a 20 20 7d 20 2a 68 74 3b 0a 7d 3b 0a 0a 2f 2a  .  } *ht;.};../*
20020 20 45 61 63 68 20 65 6c 65 6d 65 6e 74 20 69 6e   Each element in
20030 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
20040 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  is an instance o
20050 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
20060 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20  .** structure.  
20070 41 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 61 72 65  All elements are
20080 20 73 74 6f 72 65 64 20 6f 6e 20 61 20 73 69 6e   stored on a sin
20090 67 6c 65 20 64 6f 75 62 6c 79 2d 6c 69 6e 6b 65  gle doubly-linke
200a0 64 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 41 67  d list..**.** Ag
200b0 61 69 6e 2c 20 74 68 69 73 20 73 74 72 75 63 74  ain, this struct
200c0 75 72 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20  ure is intended 
200d0 74 6f 20 62 65 20 6f 70 61 71 75 65 2c 20 62 75  to be opaque, bu
200e0 74 20 69 74 20 63 61 6e 27 74 20 72 65 61 6c 6c  t it can't reall
200f0 79 0a 2a 2a 20 62 65 20 6f 70 61 71 75 65 20 62  y.** be opaque b
20100 65 63 61 75 73 65 20 69 74 20 69 73 20 75 73 65  ecause it is use
20110 64 20 62 79 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a  d by macros..*/.
20120 73 74 72 75 63 74 20 48 61 73 68 45 6c 65 6d 20  struct HashElem 
20130 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 6e 65  {.  HashElem *ne
20140 78 74 2c 20 2a 70 72 65 76 3b 20 20 20 2f 2a 20  xt, *prev;   /* 
20150 4e 65 78 74 20 61 6e 64 20 70 72 65 76 69 6f 75  Next and previou
20160 73 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68  s elements in th
20170 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 76 6f 69  e table */.  voi
20180 64 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20 20  d *data;        
20190 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 61 73        /* Data as
201a0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
201b0 69 73 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20  is element */.  
201c0 76 6f 69 64 20 2a 70 4b 65 79 3b 20 69 6e 74 20  void *pKey; int 
201d0 6e 4b 65 79 3b 20 20 20 20 2f 2a 20 4b 65 79 20  nKey;    /* Key 
201e0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
201f0 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a  this element */.
20200 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 72 65 20  };../*.** There 
20210 61 72 65 20 34 20 64 69 66 66 65 72 65 6e 74 20  are 4 different 
20220 6d 6f 64 65 73 20 6f 66 20 6f 70 65 72 61 74 69  modes of operati
20230 6f 6e 20 66 6f 72 20 61 20 68 61 73 68 20 74 61  on for a hash ta
20240 62 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c  ble:.**.**   SQL
20250 49 54 45 5f 48 41 53 48 5f 49 4e 54 20 20 20 20  ITE_HASH_INT    
20260 20 20 20 20 20 6e 4b 65 79 20 69 73 20 75 73 65       nKey is use
20270 64 20 61 73 20 74 68 65 20 6b 65 79 20 61 6e 64  d as the key and
20280 20 70 4b 65 79 20 69 73 20 69 67 6e 6f 72 65 64   pKey is ignored
20290 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45  ..**.**   SQLITE
202a0 5f 48 41 53 48 5f 50 4f 49 4e 54 45 52 20 20 20  _HASH_POINTER   
202b0 20 20 70 4b 65 79 20 69 73 20 75 73 65 64 20 61    pKey is used a
202c0 73 20 74 68 65 20 6b 65 79 20 61 6e 64 20 6e 4b  s the key and nK
202d0 65 79 20 69 73 20 69 67 6e 6f 72 65 64 2e 0a 2a  ey is ignored..*
202e0 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 48 41  *.**   SQLITE_HA
202f0 53 48 5f 53 54 52 49 4e 47 20 20 20 20 20 20 70  SH_STRING      p
20300 4b 65 79 20 70 6f 69 6e 74 73 20 74 6f 20 61 20  Key points to a 
20310 73 74 72 69 6e 67 20 74 68 61 74 20 69 73 20 6e  string that is n
20320 4b 65 79 20 62 79 74 65 73 20 6c 6f 6e 67 0a 2a  Key bytes long.*
20330 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
20340 20 20 20 20 20 20 20 20 20 20 20 20 28 69 6e 63              (inc
20350 6c 75 64 69 6e 67 20 74 68 65 20 6e 75 6c 6c 2d  luding the null-
20360 74 65 72 6d 69 6e 61 74 6f 72 2c 20 69 66 20 61  terminator, if a
20370 6e 79 29 2e 20 20 43 61 73 65 0a 2a 2a 20 20 20  ny).  Case.**   
20380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20390 20 20 20 20 20 20 20 20 69 73 20 69 67 6e 6f 72          is ignor
203a0 65 64 20 69 6e 20 63 6f 6d 70 61 72 69 73 6f 6e  ed in comparison
203b0 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54  s..**.**   SQLIT
203c0 45 5f 48 41 53 48 5f 42 49 4e 41 52 59 20 20 20  E_HASH_BINARY   
203d0 20 20 20 70 4b 65 79 20 70 6f 69 6e 74 73 20 74     pKey points t
203e0 6f 20 62 69 6e 61 72 79 20 64 61 74 61 20 6e 4b  o binary data nK
203f0 65 79 20 62 79 74 65 73 20 6c 6f 6e 67 2e 20 0a  ey bytes long. .
20400 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
20410 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d               mem
20420 63 6d 70 28 29 20 69 73 20 75 73 65 64 20 74 6f  cmp() is used to
20430 20 63 6f 6d 70 61 72 65 20 6b 65 79 73 2e 0a 2a   compare keys..*
20440 2a 0a 2a 2a 20 41 20 63 6f 70 79 20 6f 66 20 74  *.** A copy of t
20450 68 65 20 6b 65 79 20 69 73 20 6d 61 64 65 20 66  he key is made f
20460 6f 72 20 53 51 4c 49 54 45 5f 48 41 53 48 5f 53  or SQLITE_HASH_S
20470 54 52 49 4e 47 20 61 6e 64 20 53 51 4c 49 54 45  TRING and SQLITE
20480 5f 48 41 53 48 5f 42 49 4e 41 52 59 0a 2a 2a 20  _HASH_BINARY.** 
20490 69 66 20 74 68 65 20 63 6f 70 79 4b 65 79 20 70  if the copyKey p
204a0 61 72 61 6d 65 74 65 72 20 74 6f 20 48 61 73 68  arameter to Hash
204b0 49 6e 69 74 20 69 73 20 31 2e 20 20 0a 2a 2f 0a  Init is 1.  .*/.
204c0 2f 2a 20 23 64 65 66 69 6e 65 20 53 51 4c 49 54  /* #define SQLIT
204d0 45 5f 48 41 53 48 5f 49 4e 54 20 20 20 20 20 20  E_HASH_INT      
204e0 20 31 20 2f 2f 20 4e 4f 54 20 55 53 45 44 20 2a   1 // NOT USED *
204f0 2f 0a 2f 2a 20 23 64 65 66 69 6e 65 20 53 51 4c  /./* #define SQL
20500 49 54 45 5f 48 41 53 48 5f 50 4f 49 4e 54 45 52  ITE_HASH_POINTER
20510 20 20 20 32 20 2f 2f 20 4e 4f 54 20 55 53 45 44     2 // NOT USED
20520 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
20530 54 45 5f 48 41 53 48 5f 53 54 52 49 4e 47 20 20  TE_HASH_STRING  
20540 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    3.#define SQLI
20550 54 45 5f 48 41 53 48 5f 42 49 4e 41 52 59 20 20  TE_HASH_BINARY  
20560 20 20 34 0a 0a 2f 2a 0a 2a 2a 20 41 63 63 65 73    4../*.** Acces
20570 73 20 72 6f 75 74 69 6e 65 73 2e 20 20 54 6f 20  s routines.  To 
20580 64 65 6c 65 74 65 2c 20 69 6e 73 65 72 74 20 61  delete, insert a
20590 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a   NULL pointer..*
205a0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
205b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61 73   void sqlite3Has
205c0 68 49 6e 69 74 28 48 61 73 68 2a 2c 20 69 6e 74  hInit(Hash*, int
205d0 20 6b 65 79 74 79 70 65 2c 20 69 6e 74 20 63 6f   keytype, int co
205e0 70 79 4b 65 79 29 3b 0a 53 51 4c 49 54 45 5f 50  pyKey);.SQLITE_P
205f0 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
20600 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 48  ite3HashInsert(H
20610 61 73 68 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64  ash*, const void
20620 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79   *pKey, int nKey
20630 2c 20 76 6f 69 64 20 2a 70 44 61 74 61 29 3b 0a  , void *pData);.
20640 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
20650 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 61 73 68  oid *sqlite3Hash
20660 46 69 6e 64 28 63 6f 6e 73 74 20 48 61 73 68 2a  Find(const Hash*
20670 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b  , const void *pK
20680 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 3b 0a 53  ey, int nKey);.S
20690 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
206a0 69 64 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c  id sqlite3HashCl
206b0 65 61 72 28 48 61 73 68 2a 29 3b 0a 0a 2f 2a 0a  ear(Hash*);../*.
206c0 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 6c 6f  ** Macros for lo
206d0 6f 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20 65  oping over all e
206e0 6c 65 6d 65 6e 74 73 20 6f 66 20 61 20 68 61 73  lements of a has
206f0 68 20 74 61 62 6c 65 2e 20 20 54 68 65 20 69 64  h table.  The id
20700 69 6f 6d 20 69 73 0a 2a 2a 20 6c 69 6b 65 20 74  iom is.** like t
20710 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 48 61 73  his:.**.**   Has
20720 68 20 68 3b 0a 2a 2a 20 20 20 48 61 73 68 45 6c  h h;.**   HashEl
20730 65 6d 20 2a 70 3b 0a 2a 2a 20 20 20 2e 2e 2e 0a  em *p;.**   ....
20740 2a 2a 20 20 20 66 6f 72 28 70 3d 73 71 6c 69 74  **   for(p=sqlit
20750 65 48 61 73 68 46 69 72 73 74 28 26 68 29 3b 20  eHashFirst(&h); 
20760 70 3b 20 70 3d 73 71 6c 69 74 65 48 61 73 68 4e  p; p=sqliteHashN
20770 65 78 74 28 70 29 29 7b 0a 2a 2a 20 20 20 20 20  ext(p)){.**     
20780 53 6f 6d 65 53 74 72 75 63 74 75 72 65 20 2a 70  SomeStructure *p
20790 44 61 74 61 20 3d 20 73 71 6c 69 74 65 48 61 73  Data = sqliteHas
207a0 68 44 61 74 61 28 70 29 3b 0a 2a 2a 20 20 20 20  hData(p);.**    
207b0 20 2f 2f 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67   // do something
207c0 20 77 69 74 68 20 70 44 61 74 61 0a 2a 2a 20 20   with pData.**  
207d0 20 7d 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71   }.*/.#define sq
207e0 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 48 29  liteHashFirst(H)
207f0 20 20 28 28 48 29 2d 3e 66 69 72 73 74 29 0a 23    ((H)->first).#
20800 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73  define sqliteHas
20810 68 4e 65 78 74 28 45 29 20 20 20 28 28 45 29 2d  hNext(E)   ((E)-
20820 3e 6e 65 78 74 29 0a 23 64 65 66 69 6e 65 20 73  >next).#define s
20830 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 45 29  qliteHashData(E)
20840 20 20 20 28 28 45 29 2d 3e 64 61 74 61 29 0a 23     ((E)->data).#
20850 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73  define sqliteHas
20860 68 4b 65 79 28 45 29 20 20 20 20 28 28 45 29 2d  hKey(E)    ((E)-
20870 3e 70 4b 65 79 29 0a 23 64 65 66 69 6e 65 20 73  >pKey).#define s
20880 71 6c 69 74 65 48 61 73 68 4b 65 79 73 69 7a 65  qliteHashKeysize
20890 28 45 29 20 28 28 45 29 2d 3e 6e 4b 65 79 29 0a  (E) ((E)->nKey).
208a0 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66  ./*.** Number of
208b0 20 65 6e 74 72 69 65 73 20 69 6e 20 61 20 68 61   entries in a ha
208c0 73 68 20 74 61 62 6c 65 0a 2a 2f 0a 23 64 65 66  sh table.*/.#def
208d0 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68 43 6f  ine sqliteHashCo
208e0 75 6e 74 28 48 29 20 20 28 28 48 29 2d 3e 63 6f  unt(H)  ((H)->co
208f0 75 6e 74 29 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  unt)..#endif /* 
20900 5f 53 51 4c 49 54 45 5f 48 41 53 48 5f 48 5f 20  _SQLITE_HASH_H_ 
20910 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
20920 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 61 73 68 2e  *** End of hash.
20930 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
20940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20960 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
20970 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77  *** Continuing w
20980 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66  here we left off
20990 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20   in sqliteInt.h 
209a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
209b0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
209c0 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 70 61 72 73  *** Include pars
209d0 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  e.h in the middl
209e0 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68  e of sqliteInt.h
209f0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
20a00 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
20a10 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70  *** Begin file p
20a20 61 72 73 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  arse.h *********
20a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20a50 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53  **/.#define TK_S
20a60 45 4d 49 20 20 20 20 20 20 20 20 20 20 20 20 20  EMI             
20a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
20a80 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 58 50 4c  .#define TK_EXPL
20a90 41 49 4e 20 20 20 20 20 20 20 20 20 20 20 20 20  AIN             
20aa0 20 20 20 20 20 20 20 20 20 20 20 20 32 0a 23 64              2.#d
20ab0 65 66 69 6e 65 20 54 4b 5f 51 55 45 52 59 20 20  efine TK_QUERY  
20ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20ad0 20 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 69           3.#defi
20ae0 6e 65 20 54 4b 5f 50 4c 41 4e 20 20 20 20 20 20  ne TK_PLAN      
20af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20b00 20 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20        4.#define 
20b10 54 4b 5f 42 45 47 49 4e 20 20 20 20 20 20 20 20  TK_BEGIN        
20b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20b30 20 20 20 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f     5.#define TK_
20b40 54 52 41 4e 53 41 43 54 49 4f 4e 20 20 20 20 20  TRANSACTION     
20b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20b60 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 46  6.#define TK_DEF
20b70 45 52 52 45 44 20 20 20 20 20 20 20 20 20 20 20  ERRED           
20b80 20 20 20 20 20 20 20 20 20 20 20 20 20 37 0a 23               7.#
20b90 64 65 66 69 6e 65 20 54 4b 5f 49 4d 4d 45 44 49  define TK_IMMEDI
20ba0 41 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ATE             
20bb0 20 20 20 20 20 20 20 20 20 20 38 0a 23 64 65 66            8.#def
20bc0 69 6e 65 20 54 4b 5f 45 58 43 4c 55 53 49 56 45  ine TK_EXCLUSIVE
20bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20be0 20 20 20 20 20 20 20 39 0a 23 64 65 66 69 6e 65         9.#define
20bf0 20 54 4b 5f 43 4f 4d 4d 49 54 20 20 20 20 20 20   TK_COMMIT      
20c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20c10 20 20 20 31 30 0a 23 64 65 66 69 6e 65 20 54 4b     10.#define TK
20c20 5f 45 4e 44 20 20 20 20 20 20 20 20 20 20 20 20  _END            
20c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20c40 31 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 4f  11.#define TK_RO
20c50 4c 4c 42 41 43 4b 20 20 20 20 20 20 20 20 20 20  LLBACK          
20c60 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 0a               12.
20c70 23 64 65 66 69 6e 65 20 54 4b 5f 43 52 45 41 54  #define TK_CREAT
20c80 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
20c90 20 20 20 20 20 20 20 20 20 20 31 33 0a 23 64 65            13.#de
20ca0 66 69 6e 65 20 54 4b 5f 54 41 42 4c 45 20 20 20  fine TK_TABLE   
20cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20cc0 20 20 20 20 20 20 20 31 34 0a 23 64 65 66 69 6e         14.#defin
20cd0 65 20 54 4b 5f 49 46 20 20 20 20 20 20 20 20 20  e TK_IF         
20ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20cf0 20 20 20 20 31 35 0a 23 64 65 66 69 6e 65 20 54      15.#define T
20d00 4b 5f 4e 4f 54 20 20 20 20 20 20 20 20 20 20 20  K_NOT           
20d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20d20 20 31 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45   16.#define TK_E
20d30 58 49 53 54 53 20 20 20 20 20 20 20 20 20 20 20  XISTS           
20d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 37                17
20d50 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 45 4d 50  .#define TK_TEMP
20d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20d70 20 20 20 20 20 20 20 20 20 20 20 31 38 0a 23 64             18.#d
20d80 65 66 69 6e 65 20 54 4b 5f 4c 50 20 20 20 20 20  efine TK_LP     
20d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20da0 20 20 20 20 20 20 20 20 31 39 0a 23 64 65 66 69          19.#defi
20db0 6e 65 20 54 4b 5f 52 50 20 20 20 20 20 20 20 20  ne TK_RP        
20dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20dd0 20 20 20 20 20 32 30 0a 23 64 65 66 69 6e 65 20       20.#define 
20de0 54 4b 5f 41 53 20 20 20 20 20 20 20 20 20 20 20  TK_AS           
20df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20e00 20 20 32 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f    21.#define TK_
20e10 43 4f 4d 4d 41 20 20 20 20 20 20 20 20 20 20 20  COMMA           
20e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32                 2
20e30 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 44 20  2.#define TK_ID 
20e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20e50 20 20 20 20 20 20 20 20 20 20 20 20 32 33 0a 23              23.#
20e60 64 65 66 69 6e 65 20 54 4b 5f 41 42 4f 52 54 20  define TK_ABORT 
20e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20e80 20 20 20 20 20 20 20 20 20 32 34 0a 23 64 65 66           24.#def
20e90 69 6e 65 20 54 4b 5f 41 46 54 45 52 20 20 20 20  ine TK_AFTER    
20ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20eb0 20 20 20 20 20 20 32 35 0a 23 64 65 66 69 6e 65        25.#define
20ec0 20 54 4b 5f 41 4e 41 4c 59 5a 45 20 20 20 20 20   TK_ANALYZE     
20ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20ee0 20 20 20 32 36 0a 23 64 65 66 69 6e 65 20 54 4b     26.#define TK
20ef0 5f 41 53 43 20 20 20 20 20 20 20 20 20 20 20 20  _ASC            
20f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20f10 32 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 54  27.#define TK_AT
20f20 54 41 43 48 20 20 20 20 20 20 20 20 20 20 20 20  TACH            
20f30 20 20 20 20 20 20 20 20 20 20 20 20 20 32 38 0a               28.
20f40 23 64 65 66 69 6e 65 20 54 4b 5f 42 45 46 4f 52  #define TK_BEFOR
20f50 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
20f60 20 20 20 20 20 20 20 20 20 20 32 39 0a 23 64 65            29.#de
20f70 66 69 6e 65 20 54 4b 5f 43 41 53 43 41 44 45 20  fine TK_CASCADE 
20f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20f90 20 20 20 20 20 20 20 33 30 0a 23 64 65 66 69 6e         30.#defin
20fa0 65 20 54 4b 5f 43 41 53 54 20 20 20 20 20 20 20  e TK_CAST       
20fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20fc0 20 20 20 20 33 31 0a 23 64 65 66 69 6e 65 20 54      31.#define T
20fd0 4b 5f 43 4f 4e 46 4c 49 43 54 20 20 20 20 20 20  K_CONFLICT      
20fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20ff0 20 33 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44   32.#define TK_D
21000 41 54 41 42 41 53 45 20 20 20 20 20 20 20 20 20  ATABASE         
21010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 33                33
21020 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 53 43  .#define TK_DESC
21030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21040 20 20 20 20 20 20 20 20 20 20 20 33 34 0a 23 64             34.#d
21050 65 66 69 6e 65 20 54 4b 5f 44 45 54 41 43 48 20  efine TK_DETACH 
21060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21070 20 20 20 20 20 20 20 20 33 35 0a 23 64 65 66 69          35.#defi
21080 6e 65 20 54 4b 5f 45 41 43 48 20 20 20 20 20 20  ne TK_EACH      
21090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
210a0 20 20 20 20 20 33 36 0a 23 64 65 66 69 6e 65 20       36.#define 
210b0 54 4b 5f 46 41 49 4c 20 20 20 20 20 20 20 20 20  TK_FAIL         
210c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
210d0 20 20 33 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f    37.#define TK_
210e0 46 4f 52 20 20 20 20 20 20 20 20 20 20 20 20 20  FOR             
210f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33                 3
21100 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 47 4e  8.#define TK_IGN
21110 4f 52 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ORE             
21120 20 20 20 20 20 20 20 20 20 20 20 20 33 39 0a 23              39.#
21130 64 65 66 69 6e 65 20 54 4b 5f 49 4e 49 54 49 41  define TK_INITIA
21140 4c 4c 59 20 20 20 20 20 20 20 20 20 20 20 20 20  LLY             
21150 20 20 20 20 20 20 20 20 20 34 30 0a 23 64 65 66           40.#def
21160 69 6e 65 20 54 4b 5f 49 4e 53 54 45 41 44 20 20  ine TK_INSTEAD  
21170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21180 20 20 20 20 20 20 34 31 0a 23 64 65 66 69 6e 65        41.#define
21190 20 54 4b 5f 4c 49 4b 45 5f 4b 57 20 20 20 20 20   TK_LIKE_KW     
211a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
211b0 20 20 20 34 32 0a 23 64 65 66 69 6e 65 20 54 4b     42.#define TK
211c0 5f 4d 41 54 43 48 20 20 20 20 20 20 20 20 20 20  _MATCH          
211d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
211e0 34 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4b 45  43.#define TK_KE
211f0 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  Y               
21200 20 20 20 20 20 20 20 20 20 20 20 20 20 34 34 0a               44.
21210 23 64 65 66 69 6e 65 20 54 4b 5f 4f 46 20 20 20  #define TK_OF   
21220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21230 20 20 20 20 20 20 20 20 20 20 34 35 0a 23 64 65            45.#de
21240 66 69 6e 65 20 54 4b 5f 4f 46 46 53 45 54 20 20  fine TK_OFFSET  
21250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21260 20 20 20 20 20 20 20 34 36 0a 23 64 65 66 69 6e         46.#defin
21270 65 20 54 4b 5f 50 52 41 47 4d 41 20 20 20 20 20  e TK_PRAGMA     
21280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21290 20 20 20 20 34 37 0a 23 64 65 66 69 6e 65 20 54      47.#define T
212a0 4b 5f 52 41 49 53 45 20 20 20 20 20 20 20 20 20  K_RAISE         
212b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
212c0 20 34 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52   48.#define TK_R
212d0 45 50 4c 41 43 45 20 20 20 20 20 20 20 20 20 20  EPLACE          
212e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 39                49
212f0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 53 54  .#define TK_REST
21300 52 49 43 54 20 20 20 20 20 20 20 20 20 20 20 20  RICT            
21310 20 20 20 20 20 20 20 20 20 20 20 35 30 0a 23 64             50.#d
21320 65 66 69 6e 65 20 54 4b 5f 52 4f 57 20 20 20 20  efine TK_ROW    
21330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21340 20 20 20 20 20 20 20 20 35 31 0a 23 64 65 66 69          51.#defi
21350 6e 65 20 54 4b 5f 54 52 49 47 47 45 52 20 20 20  ne TK_TRIGGER   
21360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21370 20 20 20 20 20 35 32 0a 23 64 65 66 69 6e 65 20       52.#define 
21380 54 4b 5f 56 41 43 55 55 4d 20 20 20 20 20 20 20  TK_VACUUM       
21390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
213a0 20 20 35 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f    53.#define TK_
213b0 56 49 45 57 20 20 20 20 20 20 20 20 20 20 20 20  VIEW            
213c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35                 5
213d0 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 49 52  4.#define TK_VIR
213e0 54 55 41 4c 20 20 20 20 20 20 20 20 20 20 20 20  TUAL            
213f0 20 20 20 20 20 20 20 20 20 20 20 20 35 35 0a 23              55.#
21400 64 65 66 69 6e 65 20 54 4b 5f 52 45 49 4e 44 45  define TK_REINDE
21410 58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  X               
21420 20 20 20 20 20 20 20 20 20 35 36 0a 23 64 65 66           56.#def
21430 69 6e 65 20 54 4b 5f 52 45 4e 41 4d 45 20 20 20  ine TK_RENAME   
21440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21450 20 20 20 20 20 20 35 37 0a 23 64 65 66 69 6e 65        57.#define
21460 20 54 4b 5f 43 54 49 4d 45 5f 4b 57 20 20 20 20   TK_CTIME_KW    
21470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21480 20 20 20 35 38 0a 23 64 65 66 69 6e 65 20 54 4b     58.#define TK
21490 5f 41 4e 59 20 20 20 20 20 20 20 20 20 20 20 20  _ANY            
214a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
214b0 35 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 52  59.#define TK_OR
214c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
214d0 20 20 20 20 20 20 20 20 20 20 20 20 20 36 30 0a               60.
214e0 23 64 65 66 69 6e 65 20 54 4b 5f 41 4e 44 20 20  #define TK_AND  
214f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21500 20 20 20 20 20 20 20 20 20 20 36 31 0a 23 64 65            61.#de
21510 66 69 6e 65 20 54 4b 5f 49 53 20 20 20 20 20 20  fine TK_IS      
21520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21530 20 20 20 20 20 20 20 36 32 0a 23 64 65 66 69 6e         62.#defin
21540 65 20 54 4b 5f 42 45 54 57 45 45 4e 20 20 20 20  e TK_BETWEEN    
21550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21560 20 20 20 20 36 33 0a 23 64 65 66 69 6e 65 20 54      63.#define T
21570 4b 5f 49 4e 20 20 20 20 20 20 20 20 20 20 20 20  K_IN            
21580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21590 20 36 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49   64.#define TK_I
215a0 53 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20  SNULL           
215b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 35                65
215c0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e 4f 54 4e  .#define TK_NOTN
215d0 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 20  ULL             
215e0 20 20 20 20 20 20 20 20 20 20 20 36 36 0a 23 64             66.#d
215f0 65 66 69 6e 65 20 54 4b 5f 4e 45 20 20 20 20 20  efine TK_NE     
21600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21610 20 20 20 20 20 20 20 20 36 37 0a 23 64 65 66 69          67.#defi
21620 6e 65 20 54 4b 5f 45 51 20 20 20 20 20 20 20 20  ne TK_EQ        
21630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21640 20 20 20 20 20 36 38 0a 23 64 65 66 69 6e 65 20       68.#define 
21650 54 4b 5f 47 54 20 20 20 20 20 20 20 20 20 20 20  TK_GT           
21660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21670 20 20 36 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f    69.#define TK_
21680 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  LE              
21690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37                 7
216a0 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 54 20  0.#define TK_LT 
216b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
216c0 20 20 20 20 20 20 20 20 20 20 20 20 37 31 0a 23              71.#
216d0 64 65 66 69 6e 65 20 54 4b 5f 47 45 20 20 20 20  define TK_GE    
216e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
216f0 20 20 20 20 20 20 20 20 20 37 32 0a 23 64 65 66           72.#def
21700 69 6e 65 20 54 4b 5f 45 53 43 41 50 45 20 20 20  ine TK_ESCAPE   
21710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21720 20 20 20 20 20 20 37 33 0a 23 64 65 66 69 6e 65        73.#define
21730 20 54 4b 5f 42 49 54 41 4e 44 20 20 20 20 20 20   TK_BITAND      
21740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21750 20 20 20 37 34 0a 23 64 65 66 69 6e 65 20 54 4b     74.#define TK
21760 5f 42 49 54 4f 52 20 20 20 20 20 20 20 20 20 20  _BITOR          
21770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21780 37 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 53  75.#define TK_LS
21790 48 49 46 54 20 20 20 20 20 20 20 20 20 20 20 20  HIFT            
217a0 20 20 20 20 20 20 20 20 20 20 20 20 20 37 36 0a               76.
217b0 23 64 65 66 69 6e 65 20 54 4b 5f 52 53 48 49 46  #define TK_RSHIF
217c0 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
217d0 20 20 20 20 20 20 20 20 20 20 37 37 0a 23 64 65            77.#de
217e0 66 69 6e 65 20 54 4b 5f 50 4c 55 53 20 20 20 20  fine TK_PLUS    
217f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21800 20 20 20 20 20 20 20 37 38 0a 23 64 65 66 69 6e         78.#defin
21810 65 20 54 4b 5f 4d 49 4e 55 53 20 20 20 20 20 20  e TK_MINUS      
21820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21830 20 20 20 20 37 39 0a 23 64 65 66 69 6e 65 20 54      79.#define T
21840 4b 5f 53 54 41 52 20 20 20 20 20 20 20 20 20 20  K_STAR          
21850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21860 20 38 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53   80.#define TK_S
21870 4c 41 53 48 20 20 20 20 20 20 20 20 20 20 20 20  LASH            
21880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 31                81
21890 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 4d 20  .#define TK_REM 
218a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
218b0 20 20 20 20 20 20 20 20 20 20 20 38 32 0a 23 64             82.#d
218c0 65 66 69 6e 65 20 54 4b 5f 43 4f 4e 43 41 54 20  efine TK_CONCAT 
218d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
218e0 20 20 20 20 20 20 20 20 38 33 0a 23 64 65 66 69          83.#defi
218f0 6e 65 20 54 4b 5f 43 4f 4c 4c 41 54 45 20 20 20  ne TK_COLLATE   
21900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21910 20 20 20 20 20 38 34 0a 23 64 65 66 69 6e 65 20       84.#define 
21920 54 4b 5f 55 4d 49 4e 55 53 20 20 20 20 20 20 20  TK_UMINUS       
21930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21940 20 20 38 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f    85.#define TK_
21950 55 50 4c 55 53 20 20 20 20 20 20 20 20 20 20 20  UPLUS           
21960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38                 8
21970 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 49 54  6.#define TK_BIT
21980 4e 4f 54 20 20 20 20 20 20 20 20 20 20 20 20 20  NOT             
21990 20 20 20 20 20 20 20 20 20 20 20 20 38 37 0a 23              87.#
219a0 64 65 66 69 6e 65 20 54 4b 5f 53 54 52 49 4e 47  define TK_STRING
219b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
219c0 20 20 20 20 20 20 20 20 20 38 38 0a 23 64 65 66           88.#def
219d0 69 6e 65 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 20 20  ine TK_JOIN_KW  
219e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
219f0 20 20 20 20 20 20 38 39 0a 23 64 65 66 69 6e 65        89.#define
21a00 20 54 4b 5f 43 4f 4e 53 54 52 41 49 4e 54 20 20   TK_CONSTRAINT  
21a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21a20 20 20 20 39 30 0a 23 64 65 66 69 6e 65 20 54 4b     90.#define TK
21a30 5f 44 45 46 41 55 4c 54 20 20 20 20 20 20 20 20  _DEFAULT        
21a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21a50 39 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e 55  91.#define TK_NU
21a60 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  LL              
21a70 20 20 20 20 20 20 20 20 20 20 20 20 20 39 32 0a               92.
21a80 23 64 65 66 69 6e 65 20 54 4b 5f 50 52 49 4d 41  #define TK_PRIMA
21a90 52 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20  RY              
21aa0 20 20 20 20 20 20 20 20 20 20 39 33 0a 23 64 65            93.#de
21ab0 66 69 6e 65 20 54 4b 5f 55 4e 49 51 55 45 20 20  fine TK_UNIQUE  
21ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21ad0 20 20 20 20 20 20 20 39 34 0a 23 64 65 66 69 6e         94.#defin
21ae0 65 20 54 4b 5f 43 48 45 43 4b 20 20 20 20 20 20  e TK_CHECK      
21af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21b00 20 20 20 20 39 35 0a 23 64 65 66 69 6e 65 20 54      95.#define T
21b10 4b 5f 52 45 46 45 52 45 4e 43 45 53 20 20 20 20  K_REFERENCES    
21b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21b30 20 39 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41   96.#define TK_A
21b40 55 54 4f 49 4e 43 52 20 20 20 20 20 20 20 20 20  UTOINCR         
21b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 37                97
21b60 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 4e 20 20  .#define TK_ON  
21b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21b80 20 20 20 20 20 20 20 20 20 20 20 39 38 0a 23 64             98.#d
21b90 65 66 69 6e 65 20 54 4b 5f 44 45 4c 45 54 45 20  efine TK_DELETE 
21ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21bb0 20 20 20 20 20 20 20 20 39 39 0a 23 64 65 66 69          99.#defi
21bc0 6e 65 20 54 4b 5f 55 50 44 41 54 45 20 20 20 20  ne TK_UPDATE    
21bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21be0 20 20 20 20 20 31 30 30 0a 23 64 65 66 69 6e 65       100.#define
21bf0 20 54 4b 5f 49 4e 53 45 52 54 20 20 20 20 20 20   TK_INSERT      
21c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21c10 20 20 20 31 30 31 0a 23 64 65 66 69 6e 65 20 54     101.#define T
21c20 4b 5f 53 45 54 20 20 20 20 20 20 20 20 20 20 20  K_SET           
21c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21c40 20 31 30 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f   102.#define TK_
21c50 44 45 46 45 52 52 41 42 4c 45 20 20 20 20 20 20  DEFERRABLE      
21c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
21c70 30 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 4f  03.#define TK_FO
21c80 52 45 49 47 4e 20 20 20 20 20 20 20 20 20 20 20  REIGN           
21c90 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 34               104
21ca0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 52 4f 50  .#define TK_DROP
21cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21cc0 20 20 20 20 20 20 20 20 20 20 20 31 30 35 0a 23             105.#
21cd0 64 65 66 69 6e 65 20 54 4b 5f 55 4e 49 4f 4e 20  define TK_UNION 
21ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21cf0 20 20 20 20 20 20 20 20 20 31 30 36 0a 23 64 65           106.#de
21d00 66 69 6e 65 20 54 4b 5f 41 4c 4c 20 20 20 20 20  fine TK_ALL     
21d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21d20 20 20 20 20 20 20 20 31 30 37 0a 23 64 65 66 69         107.#defi
21d30 6e 65 20 54 4b 5f 45 58 43 45 50 54 20 20 20 20  ne TK_EXCEPT    
21d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21d50 20 20 20 20 20 31 30 38 0a 23 64 65 66 69 6e 65       108.#define
21d60 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 20 20   TK_INTERSECT   
21d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21d80 20 20 20 31 30 39 0a 23 64 65 66 69 6e 65 20 54     109.#define T
21d90 4b 5f 53 45 4c 45 43 54 20 20 20 20 20 20 20 20  K_SELECT        
21da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21db0 20 31 31 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f   110.#define TK_
21dc0 44 49 53 54 49 4e 43 54 20 20 20 20 20 20 20 20  DISTINCT        
21dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
21de0 31 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 4f  11.#define TK_DO
21df0 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
21e00 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 32               112
21e10 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 52 4f 4d  .#define TK_FROM
21e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21e30 20 20 20 20 20 20 20 20 20 20 20 31 31 33 0a 23             113.#
21e40 64 65 66 69 6e 65 20 54 4b 5f 4a 4f 49 4e 20 20  define TK_JOIN  
21e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21e60 20 20 20 20 20 20 20 20 20 31 31 34 0a 23 64 65           114.#de
21e70 66 69 6e 65 20 54 4b 5f 55 53 49 4e 47 20 20 20  fine TK_USING   
21e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21e90 20 20 20 20 20 20 20 31 31 35 0a 23 64 65 66 69         115.#defi
21ea0 6e 65 20 54 4b 5f 4f 52 44 45 52 20 20 20 20 20  ne TK_ORDER     
21eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21ec0 20 20 20 20 20 31 31 36 0a 23 64 65 66 69 6e 65       116.#define
21ed0 20 54 4b 5f 42 59 20 20 20 20 20 20 20 20 20 20   TK_BY          
21ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21ef0 20 20 20 31 31 37 0a 23 64 65 66 69 6e 65 20 54     117.#define T
21f00 4b 5f 47 52 4f 55 50 20 20 20 20 20 20 20 20 20  K_GROUP         
21f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21f20 20 31 31 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f   118.#define TK_
21f30 48 41 56 49 4e 47 20 20 20 20 20 20 20 20 20 20  HAVING          
21f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
21f50 31 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 49  19.#define TK_LI
21f60 4d 49 54 20 20 20 20 20 20 20 20 20 20 20 20 20  MIT             
21f70 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 30               120
21f80 0a 23 64 65 66 69 6e 65 20 54 4b 5f 57 48 45 52  .#define TK_WHER
21f90 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
21fa0 20 20 20 20 20 20 20 20 20 20 20 31 32 31 0a 23             121.#
21fb0 64 65 66 69 6e 65 20 54 4b 5f 49 4e 54 4f 20 20  define TK_INTO  
21fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21fd0 20 20 20 20 20 20 20 20 20 31 32 32 0a 23 64 65           122.#de
21fe0 66 69 6e 65 20 54 4b 5f 56 41 4c 55 45 53 20 20  fine TK_VALUES  
21ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22000 20 20 20 20 20 20 20 31 32 33 0a 23 64 65 66 69         123.#defi
22010 6e 65 20 54 4b 5f 49 4e 54 45 47 45 52 20 20 20  ne TK_INTEGER   
22020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22030 20 20 20 20 20 31 32 34 0a 23 64 65 66 69 6e 65       124.#define
22040 20 54 4b 5f 46 4c 4f 41 54 20 20 20 20 20 20 20   TK_FLOAT       
22050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22060 20 20 20 31 32 35 0a 23 64 65 66 69 6e 65 20 54     125.#define T
22070 4b 5f 42 4c 4f 42 20 20 20 20 20 20 20 20 20 20  K_BLOB          
22080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22090 20 31 32 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f   126.#define TK_
220a0 52 45 47 49 53 54 45 52 20 20 20 20 20 20 20 20  REGISTER        
220b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
220c0 32 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 41  27.#define TK_VA
220d0 52 49 41 42 4c 45 20 20 20 20 20 20 20 20 20 20  RIABLE          
220e0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 38               128
220f0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 41 53 45  .#define TK_CASE
22100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22110 20 20 20 20 20 20 20 20 20 20 20 31 32 39 0a 23             129.#
22120 64 65 66 69 6e 65 20 54 4b 5f 57 48 45 4e 20 20  define TK_WHEN  
22130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22140 20 20 20 20 20 20 20 20 20 31 33 30 0a 23 64 65           130.#de
22150 66 69 6e 65 20 54 4b 5f 54 48 45 4e 20 20 20 20  fine TK_THEN    
22160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22170 20 20 20 20 20 20 20 31 33 31 0a 23 64 65 66 69         131.#defi
22180 6e 65 20 54 4b 5f 45 4c 53 45 20 20 20 20 20 20  ne TK_ELSE      
22190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
221a0 20 20 20 20 20 31 33 32 0a 23 64 65 66 69 6e 65       132.#define
221b0 20 54 4b 5f 49 4e 44 45 58 20 20 20 20 20 20 20   TK_INDEX       
221c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
221d0 20 20 20 31 33 33 0a 23 64 65 66 69 6e 65 20 54     133.#define T
221e0 4b 5f 41 4c 54 45 52 20 20 20 20 20 20 20 20 20  K_ALTER         
221f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22200 20 31 33 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f   134.#define TK_
22210 54 4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  TO              
22220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
22230 33 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 44  35.#define TK_AD
22240 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  D               
22250 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 36               136
22260 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4c 55  .#define TK_COLU
22270 4d 4e 4b 57 20 20 20 20 20 20 20 20 20 20 20 20  MNKW            
22280 20 20 20 20 20 20 20 20 20 20 20 31 33 37 0a 23             137.#
22290 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 54 45 58  define TK_TO_TEX
222a0 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
222b0 20 20 20 20 20 20 20 20 20 31 33 38 0a 23 64 65           138.#de
222c0 66 69 6e 65 20 54 4b 5f 54 4f 5f 42 4c 4f 42 20  fine TK_TO_BLOB 
222d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
222e0 20 20 20 20 20 20 20 31 33 39 0a 23 64 65 66 69         139.#defi
222f0 6e 65 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43  ne TK_TO_NUMERIC
22300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22310 20 20 20 20 20 31 34 30 0a 23 64 65 66 69 6e 65       140.#define
22320 20 54 4b 5f 54 4f 5f 49 4e 54 20 20 20 20 20 20   TK_TO_INT      
22330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22340 20 20 20 31 34 31 0a 23 64 65 66 69 6e 65 20 54     141.#define T
22350 4b 5f 54 4f 5f 52 45 41 4c 20 20 20 20 20 20 20  K_TO_REAL       
22360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22370 20 31 34 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f   142.#define TK_
22380 45 4e 44 5f 4f 46 5f 46 49 4c 45 20 20 20 20 20  END_OF_FILE     
22390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
223a0 34 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4c  43.#define TK_IL
223b0 4c 45 47 41 4c 20 20 20 20 20 20 20 20 20 20 20  LEGAL           
223c0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 34               144
223d0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 50 41 43  .#define TK_SPAC
223e0 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
223f0 20 20 20 20 20 20 20 20 20 20 20 31 34 35 0a 23             145.#
22400 64 65 66 69 6e 65 20 54 4b 5f 55 4e 43 4c 4f 53  define TK_UNCLOS
22410 45 44 5f 53 54 52 49 4e 47 20 20 20 20 20 20 20  ED_STRING       
22420 20 20 20 20 20 20 20 20 20 31 34 36 0a 23 64 65           146.#de
22430 66 69 6e 65 20 54 4b 5f 43 4f 4d 4d 45 4e 54 20  fine TK_COMMENT 
22440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22450 20 20 20 20 20 20 20 31 34 37 0a 23 64 65 66 69         147.#defi
22460 6e 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 20  ne TK_FUNCTION  
22470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22480 20 20 20 20 20 31 34 38 0a 23 64 65 66 69 6e 65       148.#define
22490 20 54 4b 5f 43 4f 4c 55 4d 4e 20 20 20 20 20 20   TK_COLUMN      
224a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
224b0 20 20 20 31 34 39 0a 23 64 65 66 69 6e 65 20 54     149.#define T
224c0 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 20  K_AGG_FUNCTION  
224d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
224e0 20 31 35 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f   150.#define TK_
224f0 41 47 47 5f 43 4f 4c 55 4d 4e 20 20 20 20 20 20  AGG_COLUMN      
22500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
22510 35 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f  51.#define TK_CO
22520 4e 53 54 5f 46 55 4e 43 20 20 20 20 20 20 20 20  NST_FUNC        
22530 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35 32               152
22540 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
22550 2a 20 45 6e 64 20 6f 66 20 70 61 72 73 65 2e 68  * End of parse.h
22560 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
22570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22590 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
225a0 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
225b0 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
225c0 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a  n sqliteInt.h **
225d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
225e0 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 69  /.#include <stdi
225f0 6f 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73  o.h>.#include <s
22600 74 64 6c 69 62 2e 68 3e 0a 23 69 6e 63 6c 75 64  tdlib.h>.#includ
22610 65 20 3c 73 74 72 69 6e 67 2e 68 3e 0a 23 69 6e  e <string.h>.#in
22620 63 6c 75 64 65 20 3c 61 73 73 65 72 74 2e 68 3e  clude <assert.h>
22630 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 64 65  .#include <stdde
22640 66 2e 68 3e 0a 0a 23 64 65 66 69 6e 65 20 73 71  f.h>..#define sq
22650 6c 69 74 65 33 5f 69 73 6e 61 6e 28 58 29 20 20  lite3_isnan(X)  
22660 28 28 58 29 21 3d 28 58 29 29 0a 0a 2f 2a 0a 2a  ((X)!=(X))../*.*
22670 2a 20 49 66 20 63 6f 6d 70 69 6c 69 6e 67 20 66  * If compiling f
22680 6f 72 20 61 20 70 72 6f 63 65 73 73 6f 72 20 74  or a processor t
22690 68 61 74 20 6c 61 63 6b 73 20 66 6c 6f 61 74 69  hat lacks floati
226a0 6e 67 20 70 6f 69 6e 74 20 73 75 70 70 6f 72 74  ng point support
226b0 2c 0a 2a 2a 20 73 75 62 73 74 69 74 75 74 65 20  ,.** substitute 
226c0 69 6e 74 65 67 65 72 20 66 6f 72 20 66 6c 6f 61  integer for floa
226d0 74 69 6e 67 2d 70 6f 69 6e 74 0a 2a 2f 0a 23 69  ting-point.*/.#i
226e0 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
226f0 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a  _FLOATING_POINT.
22700 23 20 64 65 66 69 6e 65 20 64 6f 75 62 6c 65 20  # define double 
22710 73 71 6c 69 74 65 5f 69 6e 74 36 34 0a 23 20 64  sqlite_int64.# d
22720 65 66 69 6e 65 20 4c 4f 4e 47 44 4f 55 42 4c 45  efine LONGDOUBLE
22730 5f 54 59 50 45 20 73 71 6c 69 74 65 5f 69 6e 74  _TYPE sqlite_int
22740 36 34 0a 23 20 69 66 6e 64 65 66 20 53 51 4c 49  64.# ifndef SQLI
22750 54 45 5f 42 49 47 5f 44 42 4c 0a 23 20 20 20 64  TE_BIG_DBL.#   d
22760 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 42 49 47  efine SQLITE_BIG
22770 5f 44 42 4c 20 28 30 78 37 66 66 66 66 66 66 66  _DBL (0x7fffffff
22780 66 66 66 66 66 66 66 66 29 0a 23 20 65 6e 64 69  ffffffff).# endi
22790 66 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  f.# define SQLIT
227a0 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f  E_OMIT_DATETIME_
227b0 46 55 4e 43 53 20 31 0a 23 20 64 65 66 69 6e 65  FUNCS 1.# define
227c0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41   SQLITE_OMIT_TRA
227d0 43 45 20 31 0a 23 20 75 6e 64 65 66 20 53 51 4c  CE 1.# undef SQL
227e0 49 54 45 5f 4d 49 58 45 44 5f 45 4e 44 49 41 4e  ITE_MIXED_ENDIAN
227f0 5f 36 34 42 49 54 5f 46 4c 4f 41 54 0a 23 65 6e  _64BIT_FLOAT.#en
22800 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  dif.#ifndef SQLI
22810 54 45 5f 42 49 47 5f 44 42 4c 0a 23 20 64 65 66  TE_BIG_DBL.# def
22820 69 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 5f 44  ine SQLITE_BIG_D
22830 42 4c 20 28 31 65 39 39 29 0a 23 65 6e 64 69 66  BL (1e99).#endif
22840 0a 0a 2f 2a 0a 2a 2a 20 4f 4d 49 54 5f 54 45 4d  ../*.** OMIT_TEM
22850 50 44 42 20 69 73 20 73 65 74 20 74 6f 20 31 20  PDB is set to 1 
22860 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  if SQLITE_OMIT_T
22870 45 4d 50 44 42 20 69 73 20 64 65 66 69 6e 65 64  EMPDB is defined
22880 2c 20 6f 72 20 30 0a 2a 2a 20 61 66 74 65 72 77  , or 0.** afterw
22890 61 72 64 2e 20 48 61 76 69 6e 67 20 74 68 69 73  ard. Having this
228a0 20 6d 61 63 72 6f 20 61 6c 6c 6f 77 73 20 75 73   macro allows us
228b0 20 74 6f 20 63 61 75 73 65 20 74 68 65 20 43 20   to cause the C 
228c0 63 6f 6d 70 69 6c 65 72 20 0a 2a 2a 20 74 6f 20  compiler .** to 
228d0 6f 6d 69 74 20 63 6f 64 65 20 75 73 65 64 20 62  omit code used b
228e0 79 20 54 45 4d 50 20 74 61 62 6c 65 73 20 77 69  y TEMP tables wi
228f0 74 68 6f 75 74 20 6d 65 73 73 79 20 23 69 66 6e  thout messy #ifn
22900 64 65 66 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  def statements..
22910 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
22920 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 23 64 65  _OMIT_TEMPDB.#de
22930 66 69 6e 65 20 4f 4d 49 54 5f 54 45 4d 50 44 42  fine OMIT_TEMPDB
22940 20 31 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65   1.#else.#define
22950 20 4f 4d 49 54 5f 54 45 4d 50 44 42 20 30 0a 23   OMIT_TEMPDB 0.#
22960 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  endif../*.** If 
22970 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61  the following ma
22980 63 72 6f 20 69 73 20 73 65 74 20 74 6f 20 31 2c  cro is set to 1,
22990 20 74 68 65 6e 20 4e 55 4c 4c 20 76 61 6c 75 65   then NULL value
229a0 73 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64  s are considered
229b0 0a 2a 2a 20 64 69 73 74 69 6e 63 74 20 77 68 65  .** distinct whe
229c0 6e 20 64 65 74 65 72 6d 69 6e 69 6e 67 20 77 68  n determining wh
229d0 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 77 6f  ether or not two
229e0 20 65 6e 74 72 69 65 73 20 61 72 65 20 74 68 65   entries are the
229f0 20 73 61 6d 65 0a 2a 2a 20 69 6e 20 61 20 55 4e   same.** in a UN
22a00 49 51 55 45 20 69 6e 64 65 78 2e 20 20 54 68 69  IQUE index.  Thi
22a10 73 20 69 73 20 74 68 65 20 77 61 79 20 50 6f 73  s is the way Pos
22a20 74 67 72 65 53 51 4c 2c 20 4f 72 61 63 6c 65 2c  tgreSQL, Oracle,
22a30 20 44 42 32 2c 20 4d 79 53 51 4c 2c 0a 2a 2a 20   DB2, MySQL,.** 
22a40 4f 43 45 4c 4f 54 2c 20 61 6e 64 20 46 69 72 65  OCELOT, and Fire
22a50 62 69 72 64 20 61 6c 6c 20 77 6f 72 6b 2e 20 20  bird all work.  
22a60 54 68 65 20 53 51 4c 39 32 20 73 70 65 63 20 65  The SQL92 spec e
22a70 78 70 6c 69 63 69 74 6c 79 20 73 61 79 73 20 74  xplicitly says t
22a80 68 69 73 0a 2a 2a 20 69 73 20 74 68 65 20 77 61  his.** is the wa
22a90 79 20 74 68 69 6e 67 73 20 61 72 65 20 73 75 70  y things are sup
22aa0 70 6f 73 65 20 74 6f 20 77 6f 72 6b 2e 0a 2a 2a  pose to work..**
22ab0 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f 6c 6c 6f  .** If the follo
22ac0 77 69 6e 67 20 6d 61 63 72 6f 20 69 73 20 73 65  wing macro is se
22ad0 74 20 74 6f 20 30 2c 20 74 68 65 20 4e 55 4c 4c  t to 0, the NULL
22ae0 73 20 61 72 65 20 69 6e 64 69 73 74 69 6e 63 74  s are indistinct
22af0 20 66 6f 72 0a 2a 2a 20 61 20 55 4e 49 51 55 45   for.** a UNIQUE
22b00 20 69 6e 64 65 78 2e 20 20 49 6e 20 74 68 69 73   index.  In this
22b10 20 6d 6f 64 65 2c 20 79 6f 75 20 63 61 6e 20 6f   mode, you can o
22b20 6e 6c 79 20 68 61 76 65 20 61 20 73 69 6e 67 6c  nly have a singl
22b30 65 20 4e 55 4c 4c 20 65 6e 74 72 79 0a 2a 2a 20  e NULL entry.** 
22b40 66 6f 72 20 61 20 63 6f 6c 75 6d 6e 20 64 65 63  for a column dec
22b50 6c 61 72 65 64 20 55 4e 49 51 55 45 2e 20 20 54  lared UNIQUE.  T
22b60 68 69 73 20 69 73 20 74 68 65 20 77 61 79 20 49  his is the way I
22b70 6e 66 6f 72 6d 69 78 20 61 6e 64 20 53 51 4c 20  nformix and SQL 
22b80 53 65 72 76 65 72 0a 2a 2a 20 77 6f 72 6b 2e 0a  Server.** work..
22b90 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 55 4c 4c 5f  */.#define NULL_
22ba0 44 49 53 54 49 4e 43 54 5f 46 4f 52 5f 55 4e 49  DISTINCT_FOR_UNI
22bb0 51 55 45 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  QUE 1../*.** The
22bc0 20 22 66 69 6c 65 20 66 6f 72 6d 61 74 22 20 6e   "file format" n
22bd0 75 6d 62 65 72 20 69 73 20 61 6e 20 69 6e 74 65  umber is an inte
22be0 67 65 72 20 74 68 61 74 20 69 73 20 69 6e 63 72  ger that is incr
22bf0 65 6d 65 6e 74 65 64 20 77 68 65 6e 65 76 65 72  emented whenever
22c00 0a 2a 2a 20 74 68 65 20 56 44 42 45 2d 6c 65 76  .** the VDBE-lev
22c10 65 6c 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 63  el file format c
22c20 68 61 6e 67 65 73 2e 20 20 54 68 65 20 66 6f 6c  hanges.  The fol
22c30 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 64 65  lowing macros de
22c40 66 69 6e 65 20 74 68 65 0a 2a 2a 20 74 68 65 20  fine the.** the 
22c50 64 65 66 61 75 6c 74 20 66 69 6c 65 20 66 6f 72  default file for
22c60 6d 61 74 20 66 6f 72 20 6e 65 77 20 64 61 74 61  mat for new data
22c70 62 61 73 65 73 20 61 6e 64 20 74 68 65 20 6d 61  bases and the ma
22c80 78 69 6d 75 6d 20 66 69 6c 65 20 66 6f 72 6d 61  ximum file forma
22c90 74 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 6c 69  t.** that the li
22ca0 62 72 61 72 79 20 63 61 6e 20 72 65 61 64 2e 0a  brary can read..
22cb0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
22cc0 45 5f 4d 41 58 5f 46 49 4c 45 5f 46 4f 52 4d 41  E_MAX_FILE_FORMA
22cd0 54 20 34 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  T 4.#ifndef SQLI
22ce0 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f  TE_DEFAULT_FILE_
22cf0 46 4f 52 4d 41 54 0a 23 20 64 65 66 69 6e 65 20  FORMAT.# define 
22d00 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46  SQLITE_DEFAULT_F
22d10 49 4c 45 5f 46 4f 52 4d 41 54 20 31 0a 23 65 6e  ILE_FORMAT 1.#en
22d20 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 76 69  dif../*.** Provi
22d30 64 65 20 61 20 64 65 66 61 75 6c 74 20 76 61 6c  de a default val
22d40 75 65 20 66 6f 72 20 54 45 4d 50 5f 53 54 4f 52  ue for TEMP_STOR
22d50 45 20 69 6e 20 63 61 73 65 20 69 74 20 69 73 20  E in case it is 
22d60 6e 6f 74 20 73 70 65 63 69 66 69 65 64 0a 2a 2a  not specified.**
22d70 20 6f 6e 20 74 68 65 20 63 6f 6d 6d 61 6e 64 2d   on the command-
22d80 6c 69 6e 65 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  line.*/.#ifndef 
22d90 54 45 4d 50 5f 53 54 4f 52 45 0a 23 20 64 65 66  TEMP_STORE.# def
22da0 69 6e 65 20 54 45 4d 50 5f 53 54 4f 52 45 20 31  ine TEMP_STORE 1
22db0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47  .#endif../*.** G
22dc0 43 43 20 64 6f 65 73 20 6e 6f 74 20 64 65 66 69  CC does not defi
22dd0 6e 65 20 74 68 65 20 6f 66 66 73 65 74 6f 66 28  ne the offsetof(
22de0 29 20 6d 61 63 72 6f 20 73 6f 20 77 65 27 6c 6c  ) macro so we'll
22df0 20 68 61 76 65 20 74 6f 20 64 6f 20 69 74 0a 2a   have to do it.*
22e00 2a 20 6f 75 72 73 65 6c 76 65 73 2e 0a 2a 2f 0a  * ourselves..*/.
22e10 23 69 66 6e 64 65 66 20 6f 66 66 73 65 74 6f 66  #ifndef offsetof
22e20 0a 23 64 65 66 69 6e 65 20 6f 66 66 73 65 74 6f  .#define offseto
22e30 66 28 53 54 52 55 43 54 55 52 45 2c 46 49 45 4c  f(STRUCTURE,FIEL
22e40 44 29 20 28 28 69 6e 74 29 28 28 63 68 61 72 2a  D) ((int)((char*
22e50 29 26 28 28 53 54 52 55 43 54 55 52 45 2a 29 30  )&((STRUCTURE*)0
22e60 29 2d 3e 46 49 45 4c 44 29 29 0a 23 65 6e 64 69  )->FIELD)).#endi
22e70 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74  f../*.** Check t
22e80 6f 20 73 65 65 20 69 66 20 74 68 69 73 20 6d 61  o see if this ma
22e90 63 68 69 6e 65 20 75 73 65 73 20 45 42 43 44 49  chine uses EBCDI
22ea0 43 2e 20 20 28 59 65 73 2c 20 62 65 6c 69 65 76  C.  (Yes, believ
22eb0 65 20 69 74 20 6f 72 0a 2a 2a 20 6e 6f 74 2c 20  e it or.** not, 
22ec0 74 68 65 72 65 20 61 72 65 20 73 74 69 6c 6c 20  there are still 
22ed0 6d 61 63 68 69 6e 65 73 20 6f 75 74 20 74 68 65  machines out the
22ee0 72 65 20 74 68 61 74 20 75 73 65 20 45 42 43 44  re that use EBCD
22ef0 49 43 2e 29 0a 2a 2f 0a 23 69 66 20 27 41 27 20  IC.).*/.#if 'A' 
22f00 3d 3d 20 27 5c 33 30 31 27 0a 23 20 64 65 66 69  == '\301'.# defi
22f10 6e 65 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43  ne SQLITE_EBCDIC
22f20 20 31 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e   1.#else.# defin
22f30 65 20 53 51 4c 49 54 45 5f 41 53 43 49 49 20 31  e SQLITE_ASCII 1
22f40 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49  .#endif../*.** I
22f50 6e 74 65 67 65 72 73 20 6f 66 20 6b 6e 6f 77 6e  ntegers of known
22f60 20 73 69 7a 65 73 2e 20 20 54 68 65 73 65 20 74   sizes.  These t
22f70 79 70 65 64 65 66 73 20 6d 69 67 68 74 20 63 68  ypedefs might ch
22f80 61 6e 67 65 20 66 6f 72 20 61 72 63 68 69 74 65  ange for archite
22f90 63 74 75 72 65 73 0a 2a 2a 20 77 68 65 72 65 20  ctures.** where 
22fa0 74 68 65 20 73 69 7a 65 73 20 76 65 72 79 2e 20  the sizes very. 
22fb0 20 50 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61   Preprocessor ma
22fc0 63 72 6f 73 20 61 72 65 20 61 76 61 69 6c 61 62  cros are availab
22fd0 6c 65 20 73 6f 20 74 68 61 74 20 74 68 65 0a 2a  le so that the.*
22fe0 2a 20 74 79 70 65 73 20 63 61 6e 20 62 65 20 63  * types can be c
22ff0 6f 6e 76 65 6e 69 65 6e 74 6c 79 20 72 65 64 65  onveniently rede
23000 66 69 6e 65 64 20 61 74 20 63 6f 6d 70 69 6c 65  fined at compile
23010 2d 74 79 70 65 2e 20 20 4c 69 6b 65 20 74 68 69  -type.  Like thi
23020 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  s:.**.**        
23030 20 63 63 20 27 2d 44 55 49 4e 54 50 54 52 5f 54   cc '-DUINTPTR_T
23040 59 50 45 3d 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e  YPE=long long in
23050 74 27 20 2e 2e 2e 0a 2a 2f 0a 23 69 66 6e 64 65  t' ....*/.#ifnde
23060 66 20 55 49 4e 54 33 32 5f 54 59 50 45 0a 23 20  f UINT32_TYPE.# 
23070 64 65 66 69 6e 65 20 55 49 4e 54 33 32 5f 54 59  define UINT32_TY
23080 50 45 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 0a  PE unsigned int.
23090 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 55  #endif.#ifndef U
230a0 49 4e 54 31 36 5f 54 59 50 45 0a 23 20 64 65 66  INT16_TYPE.# def
230b0 69 6e 65 20 55 49 4e 54 31 36 5f 54 59 50 45 20  ine UINT16_TYPE 
230c0 75 6e 73 69 67 6e 65 64 20 73 68 6f 72 74 20 69  unsigned short i
230d0 6e 74 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65  nt.#endif.#ifnde
230e0 66 20 49 4e 54 31 36 5f 54 59 50 45 0a 23 20 64  f INT16_TYPE.# d
230f0 65 66 69 6e 65 20 49 4e 54 31 36 5f 54 59 50 45  efine INT16_TYPE
23100 20 73 68 6f 72 74 20 69 6e 74 0a 23 65 6e 64 69   short int.#endi
23110 66 0a 23 69 66 6e 64 65 66 20 55 49 4e 54 38 5f  f.#ifndef UINT8_
23120 54 59 50 45 0a 23 20 64 65 66 69 6e 65 20 55 49  TYPE.# define UI
23130 4e 54 38 5f 54 59 50 45 20 75 6e 73 69 67 6e 65  NT8_TYPE unsigne
23140 64 20 63 68 61 72 0a 23 65 6e 64 69 66 0a 23 69  d char.#endif.#i
23150 66 6e 64 65 66 20 49 4e 54 38 5f 54 59 50 45 0a  fndef INT8_TYPE.
23160 23 20 64 65 66 69 6e 65 20 49 4e 54 38 5f 54 59  # define INT8_TY
23170 50 45 20 73 69 67 6e 65 64 20 63 68 61 72 0a 23  PE signed char.#
23180 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4c 4f  endif.#ifndef LO
23190 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 0a 23 20  NGDOUBLE_TYPE.# 
231a0 64 65 66 69 6e 65 20 4c 4f 4e 47 44 4f 55 42 4c  define LONGDOUBL
231b0 45 5f 54 59 50 45 20 6c 6f 6e 67 20 64 6f 75 62  E_TYPE long doub
231c0 6c 65 0a 23 65 6e 64 69 66 0a 74 79 70 65 64 65  le.#endif.typede
231d0 66 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69  f sqlite_int64 i
231e0 36 34 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  64;          /* 
231f0 38 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e  8-byte signed in
23200 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 65 66  teger */.typedef
23210 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 75   sqlite_uint64 u
23220 36 34 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 38  64;         /* 8
23230 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 20 69  -byte unsigned i
23240 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 65  nteger */.typede
23250 66 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75 33  f UINT32_TYPE u3
23260 32 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  2;           /* 
23270 34 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 20  4-byte unsigned 
23280 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64  integer */.typed
23290 65 66 20 55 49 4e 54 31 36 5f 54 59 50 45 20 75  ef UINT16_TYPE u
232a0 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  16;           /*
232b0 20 32 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64   2-byte unsigned
232c0 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65   integer */.type
232d0 64 65 66 20 49 4e 54 31 36 5f 54 59 50 45 20 69  def INT16_TYPE i
232e0 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  16;            /
232f0 2a 20 32 2d 62 79 74 65 20 73 69 67 6e 65 64 20  * 2-byte signed 
23300 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64  integer */.typed
23310 65 66 20 55 49 4e 54 38 5f 54 59 50 45 20 75 38  ef UINT8_TYPE u8
23320 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
23330 20 31 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64   1-byte unsigned
23340 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65   integer */.type
23350 64 65 66 20 55 49 4e 54 38 5f 54 59 50 45 20 69  def UINT8_TYPE i
23360 38 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  8;             /
23370 2a 20 31 2d 62 79 74 65 20 73 69 67 6e 65 64 20  * 1-byte signed 
23380 69 6e 74 65 67 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a  integer */../*.*
23390 2a 20 4d 61 63 72 6f 73 20 74 6f 20 64 65 74 65  * Macros to dete
233a0 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 74 68  rmine whether th
233b0 65 20 6d 61 63 68 69 6e 65 20 69 73 20 62 69 67  e machine is big
233c0 20 6f 72 20 6c 69 74 74 6c 65 20 65 6e 64 69 61   or little endia
233d0 6e 2c 0a 2a 2a 20 65 76 61 6c 75 61 74 65 64 20  n,.** evaluated 
233e0 61 74 20 72 75 6e 74 69 6d 65 2e 0a 2a 2f 0a 65  at runtime..*/.e
233f0 78 74 65 72 6e 20 63 6f 6e 73 74 20 69 6e 74 20  xtern const int 
23400 73 71 6c 69 74 65 33 6f 6e 65 3b 0a 23 69 66 20  sqlite3one;.#if 
23410 64 65 66 69 6e 65 64 28 69 33 38 36 29 20 7c 7c  defined(i386) ||
23420 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f   defined(__i386_
23430 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d  _) || defined(_M
23440 5f 49 58 38 36 29 0a 23 20 64 65 66 69 6e 65 20  _IX86).# define 
23450 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e  SQLITE_BIGENDIAN
23460 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 53      0.# define S
23470 51 4c 49 54 45 5f 4c 49 54 54 4c 45 45 4e 44 49  QLITE_LITTLEENDI
23480 41 4e 20 31 0a 23 20 64 65 66 69 6e 65 20 53 51  AN 1.# define SQ
23490 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45  LITE_UTF16NATIVE
234a0 20 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45    SQLITE_UTF16LE
234b0 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
234c0 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e  SQLITE_BIGENDIAN
234d0 20 20 20 20 28 2a 28 63 68 61 72 20 2a 29 28 26      (*(char *)(&
234e0 73 71 6c 69 74 65 33 6f 6e 65 29 3d 3d 30 29 0a  sqlite3one)==0).
234f0 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
23500 4c 49 54 54 4c 45 45 4e 44 49 41 4e 20 28 2a 28  LITTLEENDIAN (*(
23510 63 68 61 72 20 2a 29 28 26 73 71 6c 69 74 65 33  char *)(&sqlite3
23520 6f 6e 65 29 3d 3d 31 29 0a 23 20 64 65 66 69 6e  one)==1).# defin
23530 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41  e SQLITE_UTF16NA
23540 54 49 56 45 20 28 53 51 4c 49 54 45 5f 42 49 47  TIVE (SQLITE_BIG
23550 45 4e 44 49 41 4e 3f 53 51 4c 49 54 45 5f 55 54  ENDIAN?SQLITE_UT
23560 46 31 36 42 45 3a 53 51 4c 49 54 45 5f 55 54 46  F16BE:SQLITE_UTF
23570 31 36 4c 45 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  16LE).#endif../*
23580 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
23590 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
235a0 20 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73   structure is us
235b0 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20  ed to store the 
235c0 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a 2a 2a 20  busy-handler.** 
235d0 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61 20 67  callback for a g
235e0 69 76 65 6e 20 73 71 6c 69 74 65 20 68 61 6e 64  iven sqlite hand
235f0 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  le. .**.** The s
23600 71 6c 69 74 65 2e 62 75 73 79 48 61 6e 64 6c 65  qlite.busyHandle
23610 72 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20  r member of the 
23620 73 71 6c 69 74 65 20 73 74 72 75 63 74 20 63 6f  sqlite struct co
23630 6e 74 61 69 6e 73 20 74 68 65 20 62 75 73 79 0a  ntains the busy.
23640 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20  ** callback for 
23650 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e  the database han
23660 64 6c 65 2e 20 45 61 63 68 20 70 61 67 65 72 20  dle. Each pager 
23670 6f 70 65 6e 65 64 20 76 69 61 20 74 68 65 20 73  opened via the s
23680 71 6c 69 74 65 0a 2a 2a 20 68 61 6e 64 6c 65 20  qlite.** handle 
23690 69 73 20 70 61 73 73 65 64 20 61 20 70 6f 69 6e  is passed a poin
236a0 74 65 72 20 74 6f 20 73 71 6c 69 74 65 2e 62 75  ter to sqlite.bu
236b0 73 79 48 61 6e 64 6c 65 72 2e 20 54 68 65 20 62  syHandler. The b
236c0 75 73 79 2d 68 61 6e 64 6c 65 72 0a 2a 2a 20 63  usy-handler.** c
236d0 61 6c 6c 62 61 63 6b 20 69 73 20 63 75 72 72 65  allback is curre
236e0 6e 74 6c 79 20 69 6e 76 6f 6b 65 64 20 6f 6e 6c  ntly invoked onl
236f0 79 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 70 61  y from within pa
23700 67 65 72 2e 63 2e 0a 2a 2f 0a 74 79 70 65 64 65  ger.c..*/.typede
23710 66 20 73 74 72 75 63 74 20 42 75 73 79 48 61 6e  f struct BusyHan
23720 64 6c 65 72 20 42 75 73 79 48 61 6e 64 6c 65 72  dler BusyHandler
23730 3b 0a 73 74 72 75 63 74 20 42 75 73 79 48 61 6e  ;.struct BusyHan
23740 64 6c 65 72 20 7b 0a 20 20 69 6e 74 20 28 2a 78  dler {.  int (*x
23750 46 75 6e 63 29 28 76 6f 69 64 20 2a 2c 69 6e 74  Func)(void *,int
23760 29 3b 20 20 2f 2a 20 54 68 65 20 62 75 73 79 20  );  /* The busy 
23770 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 76 6f  callback */.  vo
23780 69 64 20 2a 70 41 72 67 3b 20 20 20 20 20 20 20  id *pArg;       
23790 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73           /* Firs
237a0 74 20 61 72 67 20 74 6f 20 62 75 73 79 20 63 61  t arg to busy ca
237b0 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20  llback */.  int 
237c0 6e 42 75 73 79 3b 20 20 20 20 20 20 20 20 20 20  nBusy;          
237d0 20 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d         /* Increm
237e0 65 6e 74 65 64 20 77 69 74 68 20 65 61 63 68 20  ented with each 
237f0 62 75 73 79 20 63 61 6c 6c 20 2a 2f 0a 7d 3b 0a  busy call */.};.
23800 0a 2f 2a 0a 2a 2a 20 44 65 66 65 72 20 73 6f 75  ./*.** Defer sou
23810 72 63 69 6e 67 20 76 64 62 65 2e 68 20 61 6e 64  rcing vdbe.h and
23820 20 62 74 72 65 65 2e 68 20 75 6e 74 69 6c 20 61   btree.h until a
23830 66 74 65 72 20 74 68 65 20 22 75 38 22 20 61 6e  fter the "u8" an
23840 64 20 0a 2a 2a 20 22 42 75 73 79 48 61 6e 64 6c  d .** "BusyHandl
23850 65 72 20 74 79 70 65 64 65 66 73 2e 0a 2a 2f 0a  er typedefs..*/.
23860 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
23870 49 6e 63 6c 75 64 65 20 76 64 62 65 2e 68 20 69  Include vdbe.h i
23880 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
23890 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
238a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
238b0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
238c0 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 2e  Begin file vdbe.
238d0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
238e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
238f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
23900 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
23910 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68  mber 15.**.** Th
23920 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
23930 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
23940 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
23950 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
23960 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
23970 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
23980 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
23990 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
239a0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
239b0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
239c0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
239d0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
239e0 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
239f0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
23a00 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
23a10 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
23a20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
23a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23a70 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 61 64 65 72  ******.** Header
23a80 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 56 69   file for the Vi
23a90 72 74 75 61 6c 20 44 61 74 61 42 61 73 65 20 45  rtual DataBase E
23aa0 6e 67 69 6e 65 20 28 56 44 42 45 29 0a 2a 2a 0a  ngine (VDBE).**.
23ab0 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 64  ** This header d
23ac0 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72  efines the inter
23ad0 66 61 63 65 20 74 6f 20 74 68 65 20 76 69 72 74  face to the virt
23ae0 75 61 6c 20 64 61 74 61 62 61 73 65 20 65 6e 67  ual database eng
23af0 69 6e 65 0a 2a 2a 20 6f 72 20 56 44 42 45 2e 20  ine.** or VDBE. 
23b00 20 54 68 65 20 56 44 42 45 20 69 6d 70 6c 65 6d   The VDBE implem
23b10 65 6e 74 73 20 61 6e 20 61 62 73 74 72 61 63 74  ents an abstract
23b20 20 6d 61 63 68 69 6e 65 20 74 68 61 74 20 72 75   machine that ru
23b30 6e 73 20 61 0a 2a 2a 20 73 69 6d 70 6c 65 20 70  ns a.** simple p
23b40 72 6f 67 72 61 6d 20 74 6f 20 61 63 63 65 73 73  rogram to access
23b50 20 61 6e 64 20 6d 6f 64 69 66 79 20 74 68 65 20   and modify the 
23b60 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 74 61 62  underlying datab
23b70 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  ase..**.** $Id: 
23b80 76 64 62 65 2e 68 2c 76 20 31 2e 31 31 30 20 32  vdbe.h,v 1.110 2
23b90 30 30 37 2f 30 35 2f 30 38 20 32 31 3a 34 35 3a  007/05/08 21:45:
23ba0 32 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a  28 drh Exp $.*/.
23bb0 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54 45 5f  #ifndef _SQLITE_
23bc0 56 44 42 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20  VDBE_H_.#define 
23bd0 5f 53 51 4c 49 54 45 5f 56 44 42 45 5f 48 5f 0a  _SQLITE_VDBE_H_.
23be0 0a 2f 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20  ./*.** A single 
23bf0 56 44 42 45 20 69 73 20 61 6e 20 6f 70 61 71 75  VDBE is an opaqu
23c00 65 20 73 74 72 75 63 74 75 72 65 20 6e 61 6d 65  e structure name
23c10 64 20 22 56 64 62 65 22 2e 20 20 4f 6e 6c 79 20  d "Vdbe".  Only 
23c20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 69 6e 20 74  routines.** in t
23c30 68 65 20 73 6f 75 72 63 65 20 66 69 6c 65 20 73  he source file s
23c40 71 6c 69 74 65 56 64 62 65 2e 63 20 61 72 65 20  qliteVdbe.c are 
23c50 61 6c 6c 6f 77 65 64 20 74 6f 20 73 65 65 20 74  allowed to see t
23c60 68 65 20 69 6e 73 69 64 65 73 0a 2a 2a 20 6f 66  he insides.** of
23c70 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e   this structure.
23c80 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
23c90 63 74 20 56 64 62 65 20 56 64 62 65 3b 0a 0a 2f  ct Vdbe Vdbe;../
23ca0 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 69 6e  *.** A single in
23cb0 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 74 68 65  struction of the
23cc0 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
23cd0 20 68 61 73 20 61 6e 20 6f 70 63 6f 64 65 0a 2a   has an opcode.*
23ce0 2a 20 61 6e 64 20 61 73 20 6d 61 6e 79 20 61 73  * and as many as
23cf0 20 74 68 72 65 65 20 6f 70 65 72 61 6e 64 73 2e   three operands.
23d00 20 20 54 68 65 20 69 6e 73 74 72 75 63 74 69 6f    The instructio
23d10 6e 20 69 73 20 72 65 63 6f 72 64 65 64 0a 2a 2a  n is recorded.**
23d20 20 61 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   as an instance 
23d30 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
23d40 20 73 74 72 75 63 74 75 72 65 3a 0a 2a 2f 0a 73   structure:.*/.s
23d50 74 72 75 63 74 20 56 64 62 65 4f 70 20 7b 0a 20  truct VdbeOp {. 
23d60 20 75 38 20 6f 70 63 6f 64 65 3b 20 20 20 20 20   u8 opcode;     
23d70 20 20 20 20 20 2f 2a 20 57 68 61 74 20 6f 70 65       /* What ope
23d80 72 61 74 69 6f 6e 20 74 6f 20 70 65 72 66 6f 72  ration to perfor
23d90 6d 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b 20 20  m */.  int p1;  
23da0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
23db0 72 73 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20  rst operand */. 
23dc0 20 69 6e 74 20 70 32 3b 20 20 20 20 20 20 20 20   int p2;        
23dd0 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70       /* Second p
23de0 61 72 61 6d 65 74 65 72 20 28 6f 66 74 65 6e 20  arameter (often 
23df0 74 68 65 20 6a 75 6d 70 20 64 65 73 74 69 6e 61  the jump destina
23e00 74 69 6f 6e 29 20 2a 2f 0a 20 20 63 68 61 72 20  tion) */.  char 
23e10 2a 70 33 3b 20 20 20 20 20 20 20 20 20 20 20 2f  *p3;           /
23e20 2a 20 54 68 69 72 64 20 70 61 72 61 6d 65 74 65  * Third paramete
23e30 72 20 2a 2f 0a 20 20 69 6e 74 20 70 33 74 79 70  r */.  int p3typ
23e40 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e  e;         /* On
23e50 65 20 6f 66 20 74 68 65 20 50 33 5f 78 78 78 20  e of the P3_xxx 
23e60 63 6f 6e 73 74 61 6e 74 73 20 64 65 66 69 6e 65  constants define
23e70 64 20 62 65 6c 6f 77 20 2a 2f 0a 23 69 66 64 65  d below */.#ifde
23e80 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20  f VDBE_PROFILE. 
23e90 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 20 20 20   int cnt;       
23ea0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
23eb0 66 20 74 69 6d 65 73 20 74 68 69 73 20 69 6e 73  f times this ins
23ec0 74 72 75 63 74 69 6f 6e 20 77 61 73 20 65 78 65  truction was exe
23ed0 63 75 74 65 64 20 2a 2f 0a 20 20 6c 6f 6e 67 20  cuted */.  long 
23ee0 6c 6f 6e 67 20 63 79 63 6c 65 73 3b 20 20 20 2f  long cycles;   /
23ef0 2a 20 54 6f 74 61 6c 20 74 69 6d 65 20 73 70 65  * Total time spe
23f00 6e 64 20 65 78 65 63 75 74 69 6e 67 20 74 68 69  nd executing thi
23f10 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f  s instruction */
23f20 0a 23 65 6e 64 69 66 0a 7d 3b 0a 74 79 70 65 64  .#endif.};.typed
23f30 65 66 20 73 74 72 75 63 74 20 56 64 62 65 4f 70  ef struct VdbeOp
23f40 20 56 64 62 65 4f 70 3b 0a 0a 2f 2a 0a 2a 2a 20   VdbeOp;../*.** 
23f50 41 20 73 6d 61 6c 6c 65 72 20 76 65 72 73 69 6f  A smaller versio
23f60 6e 20 6f 66 20 56 64 62 65 4f 70 20 75 73 65 64  n of VdbeOp used
23f70 20 66 6f 72 20 74 68 65 20 56 64 62 65 41 64 64   for the VdbeAdd
23f80 4f 70 4c 69 73 74 28 29 20 66 75 6e 63 74 69 6f  OpList() functio
23f90 6e 20 62 65 63 61 75 73 65 0a 2a 2a 20 69 74 20  n because.** it 
23fa0 74 61 6b 65 73 20 75 70 20 6c 65 73 73 20 73 70  takes up less sp
23fb0 61 63 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56  ace..*/.struct V
23fc0 64 62 65 4f 70 4c 69 73 74 20 7b 0a 20 20 75 38  dbeOpList {.  u8
23fd0 20 6f 70 63 6f 64 65 3b 20 20 20 20 20 20 20 20   opcode;        
23fe0 20 20 2f 2a 20 57 68 61 74 20 6f 70 65 72 61 74    /* What operat
23ff0 69 6f 6e 20 74 6f 20 70 65 72 66 6f 72 6d 20 2a  ion to perform *
24000 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 61 72 20  /.  signed char 
24010 70 31 3b 20 20 20 20 20 2f 2a 20 46 69 72 73 74  p1;     /* First
24020 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 73 68   operand */.  sh
24030 6f 72 74 20 69 6e 74 20 70 32 3b 20 20 20 20 20  ort int p2;     
24040 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 61    /* Second para
24050 6d 65 74 65 72 20 28 6f 66 74 65 6e 20 74 68 65  meter (often the
24060 20 6a 75 6d 70 20 64 65 73 74 69 6e 61 74 69 6f   jump destinatio
24070 6e 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 70 33  n) */.  char *p3
24080 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
24090 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 2a  hird parameter *
240a0 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72  /.};.typedef str
240b0 75 63 74 20 56 64 62 65 4f 70 4c 69 73 74 20 56  uct VdbeOpList V
240c0 64 62 65 4f 70 4c 69 73 74 3b 0a 0a 2f 2a 0a 2a  dbeOpList;../*.*
240d0 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73  * Allowed values
240e0 20 6f 66 20 56 64 62 65 4f 70 2e 70 33 74 79 70   of VdbeOp.p3typ
240f0 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 33 5f  e.*/.#define P3_
24100 4e 4f 54 55 53 45 44 20 20 20 20 30 20 20 20 2f  NOTUSED    0   /
24110 2a 20 54 68 65 20 50 33 20 70 61 72 61 6d 65 74  * The P3 paramet
24120 65 72 20 69 73 20 6e 6f 74 20 75 73 65 64 20 2a  er is not used *
24130 2f 0a 23 64 65 66 69 6e 65 20 50 33 5f 44 59 4e  /.#define P3_DYN
24140 41 4d 49 43 20 20 28 2d 31 29 20 20 2f 2a 20 50  AMIC  (-1)  /* P
24150 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 72 69  ointer to a stri
24160 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ng obtained from
24170 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20   sqliteMalloc() 
24180 2a 2f 0a 23 64 65 66 69 6e 65 20 50 33 5f 53 54  */.#define P3_ST
24190 41 54 49 43 20 20 20 28 2d 32 29 20 20 2f 2a 20  ATIC   (-2)  /* 
241a0 50 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61  Pointer to a sta
241b0 74 69 63 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64  tic string */.#d
241c0 65 66 69 6e 65 20 50 33 5f 43 4f 4c 4c 53 45 51  efine P3_COLLSEQ
241d0 20 20 28 2d 34 29 20 20 2f 2a 20 50 33 20 69 73    (-4)  /* P3 is
241e0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
241f0 43 6f 6c 6c 53 65 71 20 73 74 72 75 63 74 75 72  CollSeq structur
24200 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 33 5f  e */.#define P3_
24210 46 55 4e 43 44 45 46 20 20 28 2d 35 29 20 20 2f  FUNCDEF  (-5)  /
24220 2a 20 50 33 20 69 73 20 61 20 70 6f 69 6e 74 65  * P3 is a pointe
24230 72 20 74 6f 20 61 20 46 75 6e 63 44 65 66 20 73  r to a FuncDef s
24240 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64 65 66  tructure */.#def
24250 69 6e 65 20 50 33 5f 4b 45 59 49 4e 46 4f 20 20  ine P3_KEYINFO  
24260 28 2d 36 29 20 20 2f 2a 20 50 33 20 69 73 20 61  (-6)  /* P3 is a
24270 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4b 65   pointer to a Ke
24280 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  yInfo structure 
24290 2a 2f 0a 23 64 65 66 69 6e 65 20 50 33 5f 56 44  */.#define P3_VD
242a0 42 45 46 55 4e 43 20 28 2d 37 29 20 20 2f 2a 20  BEFUNC (-7)  /* 
242b0 50 33 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  P3 is a pointer 
242c0 74 6f 20 61 20 56 64 62 65 46 75 6e 63 20 73 74  to a VdbeFunc st
242d0 72 75 63 74 75 72 65 20 2a 2f 0a 23 64 65 66 69  ructure */.#defi
242e0 6e 65 20 50 33 5f 4d 45 4d 20 20 20 20 20 20 28  ne P3_MEM      (
242f0 2d 38 29 20 20 2f 2a 20 50 33 20 69 73 20 61 20  -8)  /* P3 is a 
24300 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d 65 6d  pointer to a Mem
24310 2a 20 20 20 20 73 74 72 75 63 74 75 72 65 20 2a  *    structure *
24320 2f 0a 23 64 65 66 69 6e 65 20 50 33 5f 54 52 41  /.#define P3_TRA
24330 4e 53 49 45 4e 54 20 28 2d 39 29 20 2f 2a 20 50  NSIENT (-9) /* P
24340 33 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  3 is a pointer t
24350 6f 20 61 20 74 72 61 6e 73 69 65 6e 74 20 73 74  o a transient st
24360 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ring */.#define 
24370 50 33 5f 56 54 41 42 20 20 20 20 20 28 2d 31 30  P3_VTAB     (-10
24380 29 20 2f 2a 20 50 33 20 69 73 20 61 20 70 6f 69  ) /* P3 is a poi
24390 6e 74 65 72 20 74 6f 20 61 6e 20 73 71 6c 69 74  nter to an sqlit
243a0 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 72  e3_vtab structur
243b0 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 33 5f  e */.#define P3_
243c0 4d 50 52 49 4e 54 46 20 20 28 2d 31 31 29 20 2f  MPRINTF  (-11) /
243d0 2a 20 50 33 20 69 73 20 61 20 73 74 72 69 6e 67  * P3 is a string
243e0 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
243f0 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29  qlite3_mprintf()
24400 20 2a 2f 0a 0a 2f 2a 20 57 68 65 6e 20 61 64 64   */../* When add
24410 69 6e 67 20 61 20 50 33 20 61 72 67 75 6d 65 6e  ing a P3 argumen
24420 74 20 75 73 69 6e 67 20 50 33 5f 4b 45 59 49 4e  t using P3_KEYIN
24430 46 4f 2c 20 61 20 63 6f 70 79 20 6f 66 20 74 68  FO, a copy of th
24440 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74  e KeyInfo struct
24450 75 72 65 0a 2a 2a 20 69 73 20 6d 61 64 65 2e 20  ure.** is made. 
24460 20 54 68 61 74 20 63 6f 70 79 20 69 73 20 66 72   That copy is fr
24470 65 65 64 20 77 68 65 6e 20 74 68 65 20 56 64 62  eed when the Vdb
24480 65 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 2e 20  e is finalized. 
24490 20 42 75 74 20 69 66 20 74 68 65 0a 2a 2a 20 61   But if the.** a
244a0 72 67 75 6d 65 6e 74 20 69 73 20 50 33 5f 4b 45  rgument is P3_KE
244b0 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 2c 20 74  YINFO_HANDOFF, t
244c0 68 65 20 70 61 73 73 65 64 20 69 6e 20 70 6f 69  he passed in poi
244d0 6e 74 65 72 20 69 73 20 75 73 65 64 2e 20 20 49  nter is used.  I
244e0 74 20 73 74 69 6c 6c 0a 2a 2a 20 67 65 74 73 20  t still.** gets 
244f0 66 72 65 65 64 20 77 68 65 6e 20 74 68 65 20 56  freed when the V
24500 64 62 65 20 69 73 20 66 69 6e 61 6c 69 7a 65 64  dbe is finalized
24510 20 73 6f 20 69 74 20 73 74 69 6c 6c 20 73 68 6f   so it still sho
24520 75 6c 64 20 62 65 20 6f 62 74 61 69 6e 65 64 0a  uld be obtained.
24530 2a 2a 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65  ** from a single
24540 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e   sqliteMalloc().
24550 20 20 42 75 74 20 6e 6f 20 63 6f 70 79 20 69 73    But no copy is
24560 20 6d 61 64 65 20 61 6e 64 20 74 68 65 20 63 61   made and the ca
24570 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f  lling.** functio
24580 6e 20 73 68 6f 75 6c 64 20 2a 6e 6f 74 2a 20 74  n should *not* t
24590 72 79 20 74 6f 20 66 72 65 65 20 74 68 65 20 4b  ry to free the K
245a0 65 79 49 6e 66 6f 2e 0a 2a 2f 0a 23 64 65 66 69  eyInfo..*/.#defi
245b0 6e 65 20 50 33 5f 4b 45 59 49 4e 46 4f 5f 48 41  ne P3_KEYINFO_HA
245c0 4e 44 4f 46 46 20 28 2d 39 29 0a 0a 2f 2a 0a 2a  NDOFF (-9)../*.*
245d0 2a 20 54 68 65 20 56 64 62 65 2e 61 43 6f 6c 4e  * The Vdbe.aColN
245e0 61 6d 65 20 61 72 72 61 79 20 63 6f 6e 74 61 69  ame array contai
245f0 6e 73 20 35 6e 20 4d 65 6d 20 73 74 72 75 63 74  ns 5n Mem struct
24600 75 72 65 73 2c 20 77 68 65 72 65 20 6e 20 69 73  ures, where n is
24610 20 74 68 65 20 0a 2a 2a 20 6e 75 6d 62 65 72 20   the .** number 
24620 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 64 61  of columns of da
24630 74 61 20 72 65 74 75 72 6e 65 64 20 62 79 20 74  ta returned by t
24640 68 65 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f  he statement..*/
24650 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45  .#define COLNAME
24660 5f 4e 41 4d 45 20 20 20 20 20 30 0a 23 64 65 66  _NAME     0.#def
24670 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c  ine COLNAME_DECL
24680 54 59 50 45 20 31 0a 23 64 65 66 69 6e 65 20 43  TYPE 1.#define C
24690 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 20  OLNAME_DATABASE 
246a0 32 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d  2.#define COLNAM
246b0 45 5f 54 41 42 4c 45 20 20 20 20 33 0a 23 64 65  E_TABLE    3.#de
246c0 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c  fine COLNAME_COL
246d0 55 4d 4e 20 20 20 34 0a 23 64 65 66 69 6e 65 20  UMN   4.#define 
246e0 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20 20 20 20 20  COLNAME_N       
246f0 20 35 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65   5      /* Numbe
24700 72 20 6f 66 20 43 4f 4c 4e 41 4d 45 5f 78 78 78  r of COLNAME_xxx
24710 20 73 79 6d 62 6f 6c 73 20 2a 2f 0a 0a 2f 2a 0a   symbols */../*.
24720 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
24730 20 6d 61 63 72 6f 20 63 6f 6e 76 65 72 74 73 20   macro converts 
24740 61 20 72 65 6c 61 74 69 76 65 20 61 64 64 72 65  a relative addre
24750 73 73 20 69 6e 20 74 68 65 20 70 32 20 66 69 65  ss in the p2 fie
24760 6c 64 0a 2a 2a 20 6f 66 20 61 20 56 64 62 65 4f  ld.** of a VdbeO
24770 70 20 73 74 72 75 63 74 75 72 65 20 69 6e 74 6f  p structure into
24780 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62   a negative numb
24790 65 72 20 73 6f 20 74 68 61 74 20 0a 2a 2a 20 73  er so that .** s
247a0 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c  qlite3VdbeAddOpL
247b0 69 73 74 28 29 20 6b 6e 6f 77 73 20 74 68 61 74  ist() knows that
247c0 20 74 68 65 20 61 64 64 72 65 73 73 20 69 73 20   the address is 
247d0 72 65 6c 61 74 69 76 65 2e 20 20 43 61 6c 6c 69  relative.  Calli
247e0 6e 67 0a 2a 2a 20 74 68 65 20 6d 61 63 72 6f 20  ng.** the macro 
247f0 61 67 61 69 6e 20 72 65 73 74 6f 72 65 73 20 74  again restores t
24800 68 65 20 61 64 64 72 65 73 73 2e 0a 2a 2f 0a 23  he address..*/.#
24810 64 65 66 69 6e 65 20 41 44 44 52 28 58 29 20 20  define ADDR(X)  
24820 28 2d 31 2d 28 58 29 29 0a 0a 2f 2a 0a 2a 2a 20  (-1-(X))../*.** 
24830 54 68 65 20 6d 61 6b 65 66 69 6c 65 20 73 63 61  The makefile sca
24840 6e 73 20 74 68 65 20 76 64 62 65 2e 63 20 73 6f  ns the vdbe.c so
24850 75 72 63 65 20 66 69 6c 65 20 61 6e 64 20 63 72  urce file and cr
24860 65 61 74 65 73 20 74 68 65 20 22 6f 70 63 6f 64  eates the "opcod
24870 65 73 2e 68 22 0a 2a 2a 20 68 65 61 64 65 72 20  es.h".** header 
24880 66 69 6c 65 20 74 68 61 74 20 64 65 66 69 6e 65  file that define
24890 73 20 61 20 6e 75 6d 62 65 72 20 66 6f 72 20 65  s a number for e
248a0 61 63 68 20 6f 70 63 6f 64 65 20 75 73 65 64 20  ach opcode used 
248b0 62 79 20 74 68 65 20 56 44 42 45 2e 0a 2a 2f 0a  by the VDBE..*/.
248c0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
248d0 49 6e 63 6c 75 64 65 20 6f 70 63 6f 64 65 73 2e  Include opcodes.
248e0 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
248f0 6f 66 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a  of vdbe.h ******
24900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
24910 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
24920 42 65 67 69 6e 20 66 69 6c 65 20 6f 70 63 6f 64  Begin file opcod
24930 65 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  es.h ***********
24940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
24960 2f 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79  /* Automatically
24970 20 67 65 6e 65 72 61 74 65 64 2e 20 20 44 6f 20   generated.  Do 
24980 6e 6f 74 20 65 64 69 74 20 2a 2f 0a 2f 2a 20 53  not edit */./* S
24990 65 65 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 68  ee the mkopcodeh
249a0 2e 61 77 6b 20 73 63 72 69 70 74 20 66 6f 72 20  .awk script for 
249b0 64 65 74 61 69 6c 73 20 2a 2f 0a 23 64 65 66 69  details */.#defi
249c0 6e 65 20 4f 50 5f 4d 65 6d 4c 6f 61 64 20 20 20  ne OP_MemLoad   
249d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
249e0 20 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 65             1.#de
249f0 66 69 6e 65 20 4f 50 5f 56 4e 65 78 74 20 20 20  fine OP_VNext   
24a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24a10 20 20 20 20 20 20 20 20 20 20 20 20 20 32 0a 23               2.#
24a20 64 65 66 69 6e 65 20 4f 50 5f 48 65 78 42 6c 6f  define OP_HexBlo
24a30 62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b               
24a40 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 36               126
24a50 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
24a60 5f 42 4c 4f 42 20 20 20 20 20 2a 2f 0a 23 64 65  _BLOB     */.#de
24a70 66 69 6e 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 20  fine OP_Column  
24a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24a90 20 20 20 20 20 20 20 20 20 20 20 20 20 33 0a 23               3.#
24aa0 64 65 66 69 6e 65 20 4f 50 5f 53 65 74 43 6f 6f  define OP_SetCoo
24ab0 6b 69 65 20 20 20 20 20 20 20 20 20 20 20 20 20  kie             
24ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34                 4
24ad0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 4d 65  .#define OP_IfMe
24ae0 6d 50 6f 73 20 20 20 20 20 20 20 20 20 20 20 20  mPos            
24af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24b00 20 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65   5.#define OP_Re
24b10 61 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  al              
24b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24b30 20 31 32 35 20 20 20 2f 2a 20 73 61 6d 65 20 61   125   /* same a
24b40 73 20 54 4b 5f 46 4c 4f 41 54 20 20 20 20 2a 2f  s TK_FLOAT    */
24b50 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 71 75  .#define OP_Sequ
24b60 65 6e 63 65 20 20 20 20 20 20 20 20 20 20 20 20  ence            
24b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24b80 20 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 6f   6.#define OP_Mo
24b90 76 65 47 74 20 20 20 20 20 20 20 20 20 20 20 20  veGt            
24ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24bb0 20 20 20 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f     7.#define OP_
24bc0 47 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  Ge              
24bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24be0 20 20 20 20 37 32 20 20 20 2f 2a 20 73 61 6d 65      72   /* same
24bf0 20 61 73 20 54 4b 5f 47 45 20 20 20 20 20 20 20   as TK_GE       
24c00 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f  */.#define OP_Ro
24c10 77 4b 65 79 20 20 20 20 20 20 20 20 20 20 20 20  wKey            
24c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24c30 20 20 20 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f     8.#define OP_
24c40 45 71 20 20 20 20 20 20 20 20 20 20 20 20 20 20  Eq              
24c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24c60 20 20 20 20 36 38 20 20 20 2f 2a 20 73 61 6d 65      68   /* same
24c70 20 61 73 20 54 4b 5f 45 51 20 20 20 20 20 20 20   as TK_EQ       
24c80 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70  */.#define OP_Op
24c90 65 6e 57 72 69 74 65 20 20 20 20 20 20 20 20 20  enWrite         
24ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24cb0 20 20 20 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f     9.#define OP_
24cc0 4e 6f 74 4e 75 6c 6c 20 20 20 20 20 20 20 20 20  NotNull         
24cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24ce0 20 20 20 20 36 36 20 20 20 2f 2a 20 73 61 6d 65      66   /* same
24cf0 20 61 73 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 20   as TK_NOTNULL  
24d00 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66  */.#define OP_If
24d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24d30 20 20 31 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f    10.#define OP_
24d40 54 6f 49 6e 74 20 20 20 20 20 20 20 20 20 20 20  ToInt           
24d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24d60 20 20 20 31 34 31 20 20 20 2f 2a 20 73 61 6d 65     141   /* same
24d70 20 61 73 20 54 4b 5f 54 4f 5f 49 4e 54 20 20 20   as TK_TO_INT   
24d80 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 74  */.#define OP_St
24d90 72 69 6e 67 38 20 20 20 20 20 20 20 20 20 20 20  ring8           
24da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24db0 20 20 38 38 20 20 20 2f 2a 20 73 61 6d 65 20 61    88   /* same a
24dc0 73 20 54 4b 5f 53 54 52 49 4e 47 20 20 20 2a 2f  s TK_STRING   */
24dd0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 6f 70 20  .#define OP_Pop 
24de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24e00 31 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 52  11.#define OP_VR
24e10 6f 77 69 64 20 20 20 20 20 20 20 20 20 20 20 20  owid            
24e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24e30 20 20 31 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f    12.#define OP_
24e40 43 6f 6c 6c 53 65 71 20 20 20 20 20 20 20 20 20  CollSeq         
24e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24e60 20 20 20 20 31 33 0a 23 64 65 66 69 6e 65 20 4f      13.#define O
24e70 50 5f 4f 70 65 6e 52 65 61 64 20 20 20 20 20 20  P_OpenRead      
24e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24e90 20 20 20 20 20 20 31 34 0a 23 64 65 66 69 6e 65        14.#define
24ea0 20 4f 50 5f 45 78 70 69 72 65 20 20 20 20 20 20   OP_Expire      
24eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24ec0 20 20 20 20 20 20 20 20 31 35 0a 23 64 65 66 69          15.#defi
24ed0 6e 65 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74  ne OP_AutoCommit
24ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24ef0 20 20 20 20 20 20 20 20 20 20 31 37 0a 23 64 65            17.#de
24f00 66 69 6e 65 20 4f 50 5f 47 74 20 20 20 20 20 20  fine OP_Gt      
24f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24f20 20 20 20 20 20 20 20 20 20 20 20 20 36 39 20 20              69  
24f30 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 47   /* same as TK_G
24f40 54 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69  T       */.#defi
24f50 6e 65 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43  ne OP_IntegrityC
24f60 6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k               
24f70 20 20 20 20 20 20 20 20 20 20 31 38 0a 23 64 65            18.#de
24f80 66 69 6e 65 20 4f 50 5f 53 6f 72 74 20 20 20 20  fine OP_Sort    
24f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24fa0 20 20 20 20 20 20 20 20 20 20 20 20 31 39 0a 23              19.#
24fb0 64 65 66 69 6e 65 20 4f 50 5f 46 75 6e 63 74 69  define OP_Functi
24fc0 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  on              
24fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 30                20
24fe0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 6e 64 20  .#define OP_And 
24ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25010 36 31 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20  61   /* same as 
25020 54 4b 5f 41 4e 44 20 20 20 20 20 20 2a 2f 0a 23  TK_AND      */.#
25030 64 65 66 69 6e 65 20 4f 50 5f 53 75 62 74 72 61  define OP_Subtra
25040 63 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ct              
25050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 39                79
25060 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
25070 5f 4d 49 4e 55 53 20 20 20 20 2a 2f 0a 23 64 65  _MINUS    */.#de
25080 66 69 6e 65 20 4f 50 5f 4e 6f 6f 70 20 20 20 20  fine OP_Noop    
25090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
250a0 20 20 20 20 20 20 20 20 20 20 20 20 32 31 0a 23              21.#
250b0 64 65 66 69 6e 65 20 4f 50 5f 52 65 74 75 72 6e  define OP_Return
250c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
250d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 32                22
250e0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 6d 61  .#define OP_Rema
250f0 69 6e 64 65 72 20 20 20 20 20 20 20 20 20 20 20  inder           
25100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25110 38 32 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20  82   /* same as 
25120 54 4b 5f 52 45 4d 20 20 20 20 20 20 2a 2f 0a 23  TK_REM      */.#
25130 64 65 66 69 6e 65 20 4f 50 5f 4e 65 77 52 6f 77  define OP_NewRow
25140 69 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20  id              
25150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 33                23
25160 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 75 6c 74  .#define OP_Mult
25170 69 70 6c 79 20 20 20 20 20 20 20 20 20 20 20 20  iply            
25180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25190 38 30 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20  80   /* same as 
251a0 54 4b 5f 53 54 41 52 20 20 20 20 20 2a 2f 0a 23  TK_STAR     */.#
251b0 64 65 66 69 6e 65 20 4f 50 5f 49 66 4d 65 6d 4e  define OP_IfMemN
251c0 65 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20  eg              
251d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 34                24
251e0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 61 72 69  .#define OP_Vari
251f0 61 62 6c 65 20 20 20 20 20 20 20 20 20 20 20 20  able            
25200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25210 32 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 74  25.#define OP_St
25220 72 69 6e 67 20 20 20 20 20 20 20 20 20 20 20 20  ring            
25230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25240 20 20 32 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f    26.#define OP_
25250 52 65 61 6c 41 66 66 69 6e 69 74 79 20 20 20 20  RealAffinity    
25260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25270 20 20 20 20 32 37 0a 23 64 65 66 69 6e 65 20 4f      27.#define O
25280 50 5f 56 52 65 6e 61 6d 65 20 20 20 20 20 20 20  P_VRename       
25290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
252a0 20 20 20 20 20 20 32 38 0a 23 64 65 66 69 6e 65        28.#define
252b0 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61 20   OP_ParseSchema 
252c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
252d0 20 20 20 20 20 20 20 20 32 39 0a 23 64 65 66 69          29.#defi
252e0 6e 65 20 4f 50 5f 56 4f 70 65 6e 20 20 20 20 20  ne OP_VOpen     
252f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25300 20 20 20 20 20 20 20 20 20 20 33 30 0a 23 64 65            30.#de
25310 66 69 6e 65 20 4f 50 5f 43 6c 6f 73 65 20 20 20  fine OP_Close   
25320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25330 20 20 20 20 20 20 20 20 20 20 20 20 33 31 0a 23              31.#
25340 64 65 66 69 6e 65 20 4f 50 5f 43 72 65 61 74 65  define OP_Create
25350 49 6e 64 65 78 20 20 20 20 20 20 20 20 20 20 20  Index           
25360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 32                32
25370 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 73 55 6e  .#define OP_IsUn
25380 69 71 75 65 20 20 20 20 20 20 20 20 20 20 20 20  ique            
25390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
253a0 33 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f  33.#define OP_No
253b0 74 46 6f 75 6e 64 20 20 20 20 20 20 20 20 20 20  tFound          
253c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
253d0 20 20 33 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f    34.#define OP_
253e0 49 6e 74 36 34 20 20 20 20 20 20 20 20 20 20 20  Int64           
253f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25400 20 20 20 20 33 35 0a 23 64 65 66 69 6e 65 20 4f      35.#define O
25410 50 5f 4d 75 73 74 42 65 49 6e 74 20 20 20 20 20  P_MustBeInt     
25420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25430 20 20 20 20 20 20 33 36 0a 23 64 65 66 69 6e 65        36.#define
25440 20 4f 50 5f 48 61 6c 74 20 20 20 20 20 20 20 20   OP_Halt        
25450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25460 20 20 20 20 20 20 20 20 33 37 0a 23 64 65 66 69          37.#defi
25470 6e 65 20 4f 50 5f 52 6f 77 69 64 20 20 20 20 20  ne OP_Rowid     
25480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25490 20 20 20 20 20 20 20 20 20 20 33 38 0a 23 64 65            38.#de
254a0 66 69 6e 65 20 4f 50 5f 49 64 78 4c 54 20 20 20  fine OP_IdxLT   
254b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
254c0 20 20 20 20 20 20 20 20 20 20 20 20 33 39 0a 23              39.#
254d0 64 65 66 69 6e 65 20 4f 50 5f 41 64 64 49 6d 6d  define OP_AddImm
254e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
254f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 30                40
25500 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 74 61 74  .#define OP_Stat
25510 65 6d 65 6e 74 20 20 20 20 20 20 20 20 20 20 20  ement           
25520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25530 34 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f  41.#define OP_Ro
25540 77 44 61 74 61 20 20 20 20 20 20 20 20 20 20 20  wData           
25550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25560 20 20 34 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f    42.#define OP_
25570 4d 65 6d 4d 61 78 20 20 20 20 20 20 20 20 20 20  MemMax          
25580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25590 20 20 20 20 34 33 0a 23 64 65 66 69 6e 65 20 4f      43.#define O
255a0 50 5f 50 75 73 68 20 20 20 20 20 20 20 20 20 20  P_Push          
255b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
255c0 20 20 20 20 20 20 34 34 0a 23 64 65 66 69 6e 65        44.#define
255d0 20 4f 50 5f 4f 72 20 20 20 20 20 20 20 20 20 20   OP_Or          
255e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
255f0 20 20 20 20 20 20 20 20 36 30 20 20 20 2f 2a 20          60   /* 
25600 73 61 6d 65 20 61 73 20 54 4b 5f 4f 52 20 20 20  same as TK_OR   
25610 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f      */.#define O
25620 50 5f 4e 6f 74 45 78 69 73 74 73 20 20 20 20 20  P_NotExists     
25630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25640 20 20 20 20 20 20 34 35 0a 23 64 65 66 69 6e 65        45.#define
25650 20 4f 50 5f 4d 65 6d 49 6e 63 72 20 20 20 20 20   OP_MemIncr     
25660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25670 20 20 20 20 20 20 20 20 34 36 0a 23 64 65 66 69          46.#defi
25680 6e 65 20 4f 50 5f 47 6f 73 75 62 20 20 20 20 20  ne OP_Gosub     
25690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
256a0 20 20 20 20 20 20 20 20 20 20 34 37 0a 23 64 65            47.#de
256b0 66 69 6e 65 20 4f 50 5f 44 69 76 69 64 65 20 20  fine OP_Divide  
256c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
256d0 20 20 20 20 20 20 20 20 20 20 20 20 38 31 20 20              81  
256e0 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53   /* same as TK_S
256f0 4c 41 53 48 20 20 20 20 2a 2f 0a 23 64 65 66 69  LASH    */.#defi
25700 6e 65 20 4f 50 5f 49 6e 74 65 67 65 72 20 20 20  ne OP_Integer   
25710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25720 20 20 20 20 20 20 20 20 20 20 34 38 0a 23 64 65            48.#de
25730 66 69 6e 65 20 4f 50 5f 54 6f 4e 75 6d 65 72 69  fine OP_ToNumeri
25740 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c               
25750 20 20 20 20 20 20 20 20 20 20 20 31 34 30 20 20             140  
25760 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54   /* same as TK_T
25770 4f 5f 4e 55 4d 45 52 49 43 2a 2f 0a 23 64 65 66  O_NUMERIC*/.#def
25780 69 6e 65 20 4f 50 5f 4d 65 6d 49 6e 74 20 20 20  ine OP_MemInt   
25790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
257a0 20 20 20 20 20 20 20 20 20 20 20 34 39 0a 23 64             49.#d
257b0 65 66 69 6e 65 20 4f 50 5f 50 72 65 76 20 20 20  efine OP_Prev   
257c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
257d0 20 20 20 20 20 20 20 20 20 20 20 20 20 35 30 0a               50.
257e0 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6e 63 61  #define OP_Conca
257f0 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
25800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38                 8
25810 33 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  3   /* same as T
25820 4b 5f 43 4f 4e 43 41 54 20 20 20 2a 2f 0a 23 64  K_CONCAT   */.#d
25830 65 66 69 6e 65 20 4f 50 5f 42 69 74 41 6e 64 20  efine OP_BitAnd 
25840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25850 20 20 20 20 20 20 20 20 20 20 20 20 20 37 34 20               74 
25860 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
25870 42 49 54 41 4e 44 20 20 20 2a 2f 0a 23 64 65 66  BITAND   */.#def
25880 69 6e 65 20 4f 50 5f 56 43 6f 6c 75 6d 6e 20 20  ine OP_VColumn  
25890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
258a0 20 20 20 20 20 20 20 20 20 20 20 35 31 0a 23 64             51.#d
258b0 65 66 69 6e 65 20 4f 50 5f 43 72 65 61 74 65 54  efine OP_CreateT
258c0 61 62 6c 65 20 20 20 20 20 20 20 20 20 20 20 20  able            
258d0 20 20 20 20 20 20 20 20 20 20 20 20 20 35 32 0a               52.
258e0 23 64 65 66 69 6e 65 20 4f 50 5f 4c 61 73 74 20  #define OP_Last 
258f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35                 5
25910 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 73 4e  3.#define OP_IsN
25920 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20  ull             
25930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25940 20 36 35 20 20 20 2f 2a 20 73 61 6d 65 20 61 73   65   /* same as
25950 20 54 4b 5f 49 53 4e 55 4c 4c 20 20 20 2a 2f 0a   TK_ISNULL   */.
25960 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 63 72 56  #define OP_IncrV
25970 61 63 75 75 6d 20 20 20 20 20 20 20 20 20 20 20  acuum           
25980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35                 5
25990 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 64 78  4.#define OP_Idx
259a0 52 6f 77 69 64 20 20 20 20 20 20 20 20 20 20 20  Rowid           
259b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
259c0 20 35 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d   55.#define OP_M
259d0 61 6b 65 49 64 78 52 65 63 20 20 20 20 20 20 20  akeIdxRec       
259e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
259f0 20 20 20 35 36 0a 23 64 65 66 69 6e 65 20 4f 50     56.#define OP
25a00 5f 53 68 69 66 74 52 69 67 68 74 20 20 20 20 20  _ShiftRight     
25a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25a20 20 20 20 20 20 37 37 20 20 20 2f 2a 20 73 61 6d       77   /* sam
25a30 65 20 61 73 20 54 4b 5f 52 53 48 49 46 54 20 20  e as TK_RSHIFT  
25a40 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52   */.#define OP_R
25a50 65 73 65 74 43 6f 75 6e 74 20 20 20 20 20 20 20  esetCount       
25a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25a70 20 20 20 35 37 0a 23 64 65 66 69 6e 65 20 4f 50     57.#define OP
25a80 5f 46 69 66 6f 57 72 69 74 65 20 20 20 20 20 20  _FifoWrite      
25a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25aa0 20 20 20 20 20 35 38 0a 23 64 65 66 69 6e 65 20       58.#define 
25ab0 4f 50 5f 43 61 6c 6c 62 61 63 6b 20 20 20 20 20  OP_Callback     
25ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25ad0 20 20 20 20 20 20 20 35 39 0a 23 64 65 66 69 6e         59.#defin
25ae0 65 20 4f 50 5f 43 6f 6e 74 65 78 74 50 75 73 68  e OP_ContextPush
25af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25b00 20 20 20 20 20 20 20 20 20 36 32 0a 23 64 65 66           62.#def
25b10 69 6e 65 20 4f 50 5f 44 72 6f 70 54 72 69 67 67  ine OP_DropTrigg
25b20 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20  er              
25b30 20 20 20 20 20 20 20 20 20 20 20 36 33 0a 23 64             63.#d
25b40 65 66 69 6e 65 20 4f 50 5f 44 72 6f 70 49 6e 64  efine OP_DropInd
25b50 65 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ex              
25b60 20 20 20 20 20 20 20 20 20 20 20 20 20 36 34 0a               64.
25b70 23 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 47 45  #define OP_IdxGE
25b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37                 7
25ba0 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 64 78  3.#define OP_Idx
25bb0 44 65 6c 65 74 65 20 20 20 20 20 20 20 20 20 20  Delete          
25bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25bd0 20 38 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56   84.#define OP_V
25be0 61 63 75 75 6d 20 20 20 20 20 20 20 20 20 20 20  acuum           
25bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25c00 20 20 20 38 36 0a 23 64 65 66 69 6e 65 20 4f 50     86.#define OP
25c10 5f 4d 6f 76 65 4c 65 20 20 20 20 20 20 20 20 20  _MoveLe         
25c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25c30 20 20 20 20 20 38 39 0a 23 64 65 66 69 6e 65 20       89.#define 
25c40 4f 50 5f 49 66 4e 6f 74 20 20 20 20 20 20 20 20  OP_IfNot        
25c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25c60 20 20 20 20 20 20 20 39 30 0a 23 64 65 66 69 6e         90.#defin
25c70 65 20 4f 50 5f 44 72 6f 70 54 61 62 6c 65 20 20  e OP_DropTable  
25c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25c90 20 20 20 20 20 20 20 20 20 39 31 0a 23 64 65 66           91.#def
25ca0 69 6e 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72  ine OP_MakeRecor
25cb0 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d               
25cc0 20 20 20 20 20 20 20 20 20 20 20 39 32 0a 23 64             92.#d
25cd0 65 66 69 6e 65 20 4f 50 5f 54 6f 42 6c 6f 62 20  efine OP_ToBlob 
25ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25cf0 20 20 20 20 20 20 20 20 20 20 20 20 31 33 39 20              139 
25d00 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
25d10 54 4f 5f 42 4c 4f 42 20 20 2a 2f 0a 23 64 65 66  TO_BLOB  */.#def
25d20 69 6e 65 20 4f 50 5f 44 65 6c 65 74 65 20 20 20  ine OP_Delete   
25d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25d40 20 20 20 20 20 20 20 20 20 20 20 39 33 0a 23 64             93.#d
25d50 65 66 69 6e 65 20 4f 50 5f 41 67 67 46 69 6e 61  efine OP_AggFina
25d60 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l               
25d70 20 20 20 20 20 20 20 20 20 20 20 20 20 39 34 0a               94.
25d80 23 64 65 66 69 6e 65 20 4f 50 5f 53 68 69 66 74  #define OP_Shift
25d90 4c 65 66 74 20 20 20 20 20 20 20 20 20 20 20 20  Left            
25da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37                 7
25db0 36 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  6   /* same as T
25dc0 4b 5f 4c 53 48 49 46 54 20 20 20 2a 2f 0a 23 64  K_LSHIFT   */.#d
25dd0 65 66 69 6e 65 20 4f 50 5f 44 75 70 20 20 20 20  efine OP_Dup    
25de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25df0 20 20 20 20 20 20 20 20 20 20 20 20 20 39 35 0a               95.
25e00 23 64 65 66 69 6e 65 20 4f 50 5f 47 6f 74 6f 20  #define OP_Goto 
25e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39                 9
25e30 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 61 62  6.#define OP_Tab
25e40 6c 65 4c 6f 63 6b 20 20 20 20 20 20 20 20 20 20  leLock          
25e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25e60 20 39 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46   97.#define OP_F
25e70 69 66 6f 52 65 61 64 20 20 20 20 20 20 20 20 20  ifoRead         
25e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25e90 20 20 20 39 38 0a 23 64 65 66 69 6e 65 20 4f 50     98.#define OP
25ea0 5f 43 6c 65 61 72 20 20 20 20 20 20 20 20 20 20  _Clear          
25eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25ec0 20 20 20 20 20 39 39 0a 23 64 65 66 69 6e 65 20       99.#define 
25ed0 4f 50 5f 49 64 78 47 54 20 20 20 20 20 20 20 20  OP_IdxGT        
25ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25ef0 20 20 20 20 20 20 31 30 30 0a 23 64 65 66 69 6e        100.#defin
25f00 65 20 4f 50 5f 4d 6f 76 65 4c 74 20 20 20 20 20  e OP_MoveLt     
25f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25f20 20 20 20 20 20 20 20 20 31 30 31 0a 23 64 65 66          101.#def
25f30 69 6e 65 20 4f 50 5f 4c 65 20 20 20 20 20 20 20  ine OP_Le       
25f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25f50 20 20 20 20 20 20 20 20 20 20 20 37 30 20 20 20             70   
25f60 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 45  /* same as TK_LE
25f70 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e         */.#defin
25f80 65 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69  e OP_VerifyCooki
25f90 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
25fa0 20 20 20 20 20 20 20 20 31 30 32 0a 23 64 65 66          102.#def
25fb0 69 6e 65 20 4f 50 5f 41 67 67 53 74 65 70 20 20  ine OP_AggStep  
25fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25fd0 20 20 20 20 20 20 20 20 20 20 31 30 33 0a 23 64            103.#d
25fe0 65 66 69 6e 65 20 4f 50 5f 50 75 6c 6c 20 20 20  efine OP_Pull   
25ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26000 20 20 20 20 20 20 20 20 20 20 20 20 31 30 34 0a              104.
26010 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f 54 65 78  #define OP_ToTex
26020 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
26030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33                13
26040 38 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  8   /* same as T
26050 4b 5f 54 4f 5f 54 45 58 54 20 20 2a 2f 0a 23 64  K_TO_TEXT  */.#d
26060 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 20 20 20 20  efine OP_Not    
26070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26080 20 20 20 20 20 20 20 20 20 20 20 20 20 31 36 20               16 
26090 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
260a0 4e 4f 54 20 20 20 20 20 20 2a 2f 0a 23 64 65 66  NOT      */.#def
260b0 69 6e 65 20 4f 50 5f 54 6f 52 65 61 6c 20 20 20  ine OP_ToReal   
260c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
260d0 20 20 20 20 20 20 20 20 20 20 31 34 32 20 20 20            142   
260e0 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f  /* same as TK_TO
260f0 5f 52 45 41 4c 20 20 2a 2f 0a 23 64 65 66 69 6e  _REAL  */.#defin
26100 65 20 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d  e OP_SetNumColum
26110 6e 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ns              
26120 20 20 20 20 20 20 20 20 31 30 35 0a 23 64 65 66          105.#def
26130 69 6e 65 20 4f 50 5f 41 62 73 56 61 6c 75 65 20  ine OP_AbsValue 
26140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26150 20 20 20 20 20 20 20 20 20 20 31 30 36 0a 23 64            106.#d
26160 65 66 69 6e 65 20 4f 50 5f 54 72 61 6e 73 61 63  efine OP_Transac
26170 74 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20  tion            
26180 20 20 20 20 20 20 20 20 20 20 20 20 31 30 37 0a              107.
26190 23 64 65 66 69 6e 65 20 4f 50 5f 56 46 69 6c 74  #define OP_VFilt
261a0 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20  er              
261b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30                10
261c0 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 65 67  8.#define OP_Neg
261d0 61 74 69 76 65 20 20 20 20 20 20 20 20 20 20 20  ative           
261e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
261f0 20 38 35 20 20 20 2f 2a 20 73 61 6d 65 20 61 73   85   /* same as
26200 20 54 4b 5f 55 4d 49 4e 55 53 20 20 20 2a 2f 0a   TK_UMINUS   */.
26210 23 64 65 66 69 6e 65 20 4f 50 5f 4e 65 20 20 20  #define OP_Ne   
26220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36                 6
26240 37 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  7   /* same as T
26250 4b 5f 4e 45 20 20 20 20 20 20 20 2a 2f 0a 23 64  K_NE       */.#d
26260 65 66 69 6e 65 20 4f 50 5f 56 44 65 73 74 72 6f  efine OP_VDestro
26270 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y               
26280 20 20 20 20 20 20 20 20 20 20 20 20 31 30 39 0a              109.
26290 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6e 74 65  #define OP_Conte
262a0 78 74 50 6f 70 20 20 20 20 20 20 20 20 20 20 20  xtPop           
262b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31                11
262c0 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 42 69 74  0.#define OP_Bit
262d0 4f 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20  Or              
262e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
262f0 20 37 35 20 20 20 2f 2a 20 73 61 6d 65 20 61 73   75   /* same as
26300 20 54 4b 5f 42 49 54 4f 52 20 20 20 20 2a 2f 0a   TK_BITOR    */.
26310 23 64 65 66 69 6e 65 20 4f 50 5f 4e 65 78 74 20  #define OP_Next 
26320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31                11
26340 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 64 78  1.#define OP_Idx
26350 49 6e 73 65 72 74 20 20 20 20 20 20 20 20 20 20  Insert          
26360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26370 31 31 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44  112.#define OP_D
26380 69 73 74 69 6e 63 74 20 20 20 20 20 20 20 20 20  istinct         
26390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
263a0 20 20 31 31 33 0a 23 64 65 66 69 6e 65 20 4f 50    113.#define OP
263b0 5f 4c 74 20 20 20 20 20 20 20 20 20 20 20 20 20  _Lt             
263c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
263d0 20 20 20 20 20 37 31 20 20 20 2f 2a 20 73 61 6d       71   /* sam
263e0 65 20 61 73 20 54 4b 5f 4c 54 20 20 20 20 20 20  e as TK_LT      
263f0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49   */.#define OP_I
26400 6e 73 65 72 74 20 20 20 20 20 20 20 20 20 20 20  nsert           
26410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26420 20 20 31 31 34 0a 23 64 65 66 69 6e 65 20 4f 50    114.#define OP
26430 5f 44 65 73 74 72 6f 79 20 20 20 20 20 20 20 20  _Destroy        
26440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26450 20 20 20 20 31 31 35 0a 23 64 65 66 69 6e 65 20      115.#define 
26460 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 20 20 20  OP_ReadCookie   
26470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26480 20 20 20 20 20 20 31 31 36 0a 23 64 65 66 69 6e        116.#defin
26490 65 20 4f 50 5f 46 6f 72 63 65 49 6e 74 20 20 20  e OP_ForceInt   
264a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
264b0 20 20 20 20 20 20 20 20 31 31 37 0a 23 64 65 66          117.#def
264c0 69 6e 65 20 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79  ine OP_LoadAnaly
264d0 73 69 73 20 20 20 20 20 20 20 20 20 20 20 20 20  sis             
264e0 20 20 20 20 20 20 20 20 20 20 31 31 38 0a 23 64            118.#d
264f0 65 66 69 6e 65 20 4f 50 5f 45 78 70 6c 61 69 6e  efine OP_Explain
26500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26510 20 20 20 20 20 20 20 20 20 20 20 20 31 31 39 0a              119.
26520 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 4d 65 6d  #define OP_IfMem
26530 5a 65 72 6f 20 20 20 20 20 20 20 20 20 20 20 20  Zero            
26540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32                12
26550 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65  0.#define OP_Ope
26560 6e 50 73 65 75 64 6f 20 20 20 20 20 20 20 20 20  nPseudo         
26570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26580 31 32 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f  121.#define OP_O
26590 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 20 20 20  penEphemeral    
265a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
265b0 20 20 31 32 32 0a 23 64 65 66 69 6e 65 20 4f 50    122.#define OP
265c0 5f 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20  _Null           
265d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
265e0 20 20 20 20 31 32 33 0a 23 64 65 66 69 6e 65 20      123.#define 
265f0 4f 50 5f 42 6c 6f 62 20 20 20 20 20 20 20 20 20  OP_Blob         
26600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26610 20 20 20 20 20 20 31 32 34 0a 23 64 65 66 69 6e        124.#defin
26620 65 20 4f 50 5f 41 64 64 20 20 20 20 20 20 20 20  e OP_Add        
26630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26640 20 20 20 20 20 20 20 20 20 37 38 20 20 20 2f 2a           78   /*
26650 20 73 61 6d 65 20 61 73 20 54 4b 5f 50 4c 55 53   same as TK_PLUS
26660 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
26670 4f 50 5f 4d 65 6d 53 74 6f 72 65 20 20 20 20 20  OP_MemStore     
26680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26690 20 20 20 20 20 20 31 32 37 0a 23 64 65 66 69 6e        127.#defin
266a0 65 20 4f 50 5f 52 65 77 69 6e 64 20 20 20 20 20  e OP_Rewind     
266b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
266c0 20 20 20 20 20 20 20 20 31 32 38 0a 23 64 65 66          128.#def
266d0 69 6e 65 20 4f 50 5f 4d 6f 76 65 47 65 20 20 20  ine OP_MoveGe   
266e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
266f0 20 20 20 20 20 20 20 20 20 20 31 32 39 0a 23 64            129.#d
26700 65 66 69 6e 65 20 4f 50 5f 56 42 65 67 69 6e 20  efine OP_VBegin 
26710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26720 20 20 20 20 20 20 20 20 20 20 20 20 31 33 30 0a              130.
26730 23 64 65 66 69 6e 65 20 4f 50 5f 56 55 70 64 61  #define OP_VUpda
26740 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  te              
26750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33                13
26760 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 42 69 74  1.#define OP_Bit
26770 4e 6f 74 20 20 20 20 20 20 20 20 20 20 20 20 20  Not             
26780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26790 20 38 37 20 20 20 2f 2a 20 73 61 6d 65 20 61 73   87   /* same as
267a0 20 54 4b 5f 42 49 54 4e 4f 54 20 20 20 2a 2f 0a   TK_BITNOT   */.
267b0 23 64 65 66 69 6e 65 20 4f 50 5f 56 43 72 65 61  #define OP_VCrea
267c0 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  te              
267d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33                13
267e0 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 65 6d  2.#define OP_Mem
267f0 4d 6f 76 65 20 20 20 20 20 20 20 20 20 20 20 20  Move            
26800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26810 31 33 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d  133.#define OP_M
26820 65 6d 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20  emNull          
26830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26840 20 20 31 33 34 0a 23 64 65 66 69 6e 65 20 4f 50    134.#define OP
26850 5f 46 6f 75 6e 64 20 20 20 20 20 20 20 20 20 20  _Found          
26860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26870 20 20 20 20 31 33 35 0a 23 64 65 66 69 6e 65 20      135.#define 
26880 4f 50 5f 4e 75 6c 6c 52 6f 77 20 20 20 20 20 20  OP_NullRow      
26890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
268a0 20 20 20 20 20 20 31 33 36 0a 0a 2f 2a 20 54 68        136../* Th
268b0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 70 63 6f  e following opco
268c0 64 65 20 76 61 6c 75 65 73 20 61 72 65 20 6e 65  de values are ne
268d0 76 65 72 20 75 73 65 64 20 2a 2f 0a 23 64 65 66  ver used */.#def
268e0 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31  ine OP_NotUsed_1
268f0 33 37 20 20 20 20 20 20 20 20 20 20 20 20 20 20  37              
26900 20 20 20 20 20 20 20 20 20 20 31 33 37 0a 0a 2f            137../
26910 2a 20 4f 70 63 6f 64 65 73 20 74 68 61 74 20 61  * Opcodes that a
26920 72 65 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f  re guaranteed to
26930 20 6e 65 76 65 72 20 70 75 73 68 20 61 20 76 61   never push a va
26940 6c 75 65 20 6f 6e 74 6f 20 74 68 65 20 73 74 61  lue onto the sta
26950 63 6b 0a 2a 2a 20 63 6f 6e 74 61 69 6e 20 61 20  ck.** contain a 
26960 31 20 74 68 65 69 72 20 63 6f 72 72 65 73 70 6f  1 their correspo
26970 6e 64 69 6e 67 20 70 6f 73 69 74 69 6f 6e 20 6f  nding position o
26980 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
26990 6d 61 73 6b 0a 2a 2a 20 73 65 74 2e 20 20 53 65  mask.** set.  Se
269a0 65 20 74 68 65 20 6f 70 63 6f 64 65 4e 6f 50 75  e the opcodeNoPu
269b0 73 68 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 6e  sh() function in
269c0 20 76 64 62 65 61 75 78 2e 63 20 20 2a 2f 0a 23   vdbeaux.c  */.#
269d0 64 65 66 69 6e 65 20 4e 4f 50 55 53 48 5f 4d 41  define NOPUSH_MA
269e0 53 4b 5f 30 20 30 78 65 65 62 34 0a 23 64 65 66  SK_0 0xeeb4.#def
269f0 69 6e 65 20 4e 4f 50 55 53 48 5f 4d 41 53 4b 5f  ine NOPUSH_MASK_
26a00 31 20 30 78 66 39 36 62 0a 23 64 65 66 69 6e 65  1 0xf96b.#define
26a10 20 4e 4f 50 55 53 48 5f 4d 41 53 4b 5f 32 20 30   NOPUSH_MASK_2 0
26a20 78 66 62 62 36 0a 23 64 65 66 69 6e 65 20 4e 4f  xfbb6.#define NO
26a30 50 55 53 48 5f 4d 41 53 4b 5f 33 20 30 78 66 65  PUSH_MASK_3 0xfe
26a40 36 34 0a 23 64 65 66 69 6e 65 20 4e 4f 50 55 53  64.#define NOPUS
26a50 48 5f 4d 41 53 4b 5f 34 20 30 78 66 66 66 66 0a  H_MASK_4 0xffff.
26a60 23 64 65 66 69 6e 65 20 4e 4f 50 55 53 48 5f 4d  #define NOPUSH_M
26a70 41 53 4b 5f 35 20 30 78 36 65 66 37 0a 23 64 65  ASK_5 0x6ef7.#de
26a80 66 69 6e 65 20 4e 4f 50 55 53 48 5f 4d 41 53 4b  fine NOPUSH_MASK
26a90 5f 36 20 30 78 66 62 66 62 0a 23 64 65 66 69 6e  _6 0xfbfb.#defin
26aa0 65 20 4e 4f 50 55 53 48 5f 4d 41 53 4b 5f 37 20  e NOPUSH_MASK_7 
26ab0 30 78 38 37 36 37 0a 23 64 65 66 69 6e 65 20 4e  0x8767.#define N
26ac0 4f 50 55 53 48 5f 4d 41 53 4b 5f 38 20 30 78 37  OPUSH_MASK_8 0x7
26ad0 64 39 66 0a 23 64 65 66 69 6e 65 20 4e 4f 50 55  d9f.#define NOPU
26ae0 53 48 5f 4d 41 53 4b 5f 39 20 30 78 30 30 30 30  SH_MASK_9 0x0000
26af0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
26b00 2a 20 45 6e 64 20 6f 66 20 6f 70 63 6f 64 65 73  * End of opcodes
26b10 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
26b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26b40 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
26b50 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
26b60 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
26b70 6e 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a  n vdbe.h *******
26b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26b90 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 74 6f 74 79  /../*.** Prototy
26ba0 70 65 73 20 66 6f 72 20 74 68 65 20 56 44 42 45  pes for the VDBE
26bb0 20 69 6e 74 65 72 66 61 63 65 2e 20 20 53 65 65   interface.  See
26bc0 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 74 68 65   comments on the
26bd0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a   implementation.
26be0 2a 2a 20 66 6f 72 20 61 20 64 65 73 63 72 69 70  ** for a descrip
26bf0 74 69 6f 6e 20 6f 66 20 77 68 61 74 20 65 61 63  tion of what eac
26c00 68 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69  h of these routi
26c10 6e 65 73 20 64 6f 65 73 2e 0a 2a 2f 0a 53 51 4c  nes does..*/.SQL
26c20 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 65  ITE_PRIVATE Vdbe
26c30 20 2a 73 71 6c 69 74 65 33 56 64 62 65 43 72 65   *sqlite3VdbeCre
26c40 61 74 65 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53  ate(sqlite3*);.S
26c50 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
26c60 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  t sqlite3VdbeAdd
26c70 4f 70 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74  Op(Vdbe*,int,int
26c80 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
26c90 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
26ca0 33 56 64 62 65 4f 70 33 28 56 64 62 65 2a 2c 69  3VdbeOp3(Vdbe*,i
26cb0 6e 74 2c 69 6e 74 2c 69 6e 74 2c 63 6f 6e 73 74  nt,int,int,const
26cc0 20 63 68 61 72 20 2a 7a 50 33 2c 69 6e 74 29 3b   char *zP3,int);
26cd0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
26ce0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41  int sqlite3VdbeA
26cf0 64 64 4f 70 4c 69 73 74 28 56 64 62 65 2a 2c 20  ddOpList(Vdbe*, 
26d00 69 6e 74 20 6e 4f 70 2c 20 56 64 62 65 4f 70 4c  int nOp, VdbeOpL
26d10 69 73 74 20 63 6f 6e 73 74 20 2a 61 4f 70 29 3b  ist const *aOp);
26d20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
26d30 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
26d40 43 68 61 6e 67 65 50 31 28 56 64 62 65 2a 2c 20  ChangeP1(Vdbe*, 
26d50 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 50 31  int addr, int P1
26d60 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
26d70 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
26d80 62 65 43 68 61 6e 67 65 50 32 28 56 64 62 65 2a  beChangeP2(Vdbe*
26d90 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20  , int addr, int 
26da0 50 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  P2);.SQLITE_PRIV
26db0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
26dc0 56 64 62 65 4a 75 6d 70 48 65 72 65 28 56 64 62  VdbeJumpHere(Vdb
26dd0 65 2a 2c 20 69 6e 74 20 61 64 64 72 29 3b 0a 53  e*, int addr);.S
26de0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
26df0 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  id sqlite3VdbeCh
26e00 61 6e 67 65 54 6f 4e 6f 6f 70 28 56 64 62 65 2a  angeToNoop(Vdbe*
26e10 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20  , int addr, int 
26e20 4e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  N);.SQLITE_PRIVA
26e30 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
26e40 64 62 65 43 68 61 6e 67 65 50 33 28 56 64 62 65  dbeChangeP3(Vdbe
26e50 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20 63 6f 6e  *, int addr, con
26e60 73 74 20 63 68 61 72 20 2a 7a 50 31 2c 20 69 6e  st char *zP1, in
26e70 74 20 4e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  t N);.SQLITE_PRI
26e80 56 41 54 45 20 56 64 62 65 4f 70 20 2a 73 71 6c  VATE VdbeOp *sql
26e90 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 56 64  ite3VdbeGetOp(Vd
26ea0 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  be*, int);.SQLIT
26eb0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
26ec0 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
26ed0 65 6c 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54  el(Vdbe*);.SQLIT
26ee0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
26ef0 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65  qlite3VdbeDelete
26f00 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  (Vdbe*);.SQLITE_
26f10 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
26f20 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64  ite3VdbeMakeRead
26f30 79 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c  y(Vdbe*,int,int,
26f40 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  int,int);.SQLITE
26f50 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
26f60 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65  ite3VdbeFinalize
26f70 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  (Vdbe*);.SQLITE_
26f80 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
26f90 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
26fa0 61 62 65 6c 28 56 64 62 65 2a 2c 20 69 6e 74 29  abel(Vdbe*, int)
26fb0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
26fc0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
26fd0 43 75 72 72 65 6e 74 41 64 64 72 28 56 64 62 65  CurrentAddr(Vdbe
26fe0 2a 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  *);.#ifdef SQLIT
26ff0 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50  E_DEBUG.SQLITE_P
27000 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
27010 6c 69 74 65 33 56 64 62 65 54 72 61 63 65 28 56  lite3VdbeTrace(V
27020 64 62 65 2a 2c 46 49 4c 45 2a 29 3b 0a 23 65 6e  dbe*,FILE*);.#en
27030 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  dif.SQLITE_PRIVA
27040 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
27050 64 62 65 52 65 73 65 74 53 74 65 70 52 65 73 75  dbeResetStepResu
27060 6c 74 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54  lt(Vdbe*);.SQLIT
27070 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
27080 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 28 56  lite3VdbeReset(V
27090 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  dbe*);.SQLITE_PR
270a0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
270b0 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
270c0 28 56 64 62 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c  (Vdbe*,int);.SQL
270d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
270e0 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
270f0 6c 4e 61 6d 65 28 56 64 62 65 2a 2c 20 69 6e 74  lName(Vdbe*, int
27100 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61  , int, const cha
27110 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  r *, int);.SQLIT
27120 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
27130 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e 74 43  qlite3VdbeCountC
27140 68 61 6e 67 65 73 28 56 64 62 65 2a 29 3b 0a 53  hanges(Vdbe*);.S
27150 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71  QLITE_PRIVATE sq
27160 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 56 64  lite3 *sqlite3Vd
27170 62 65 44 62 28 56 64 62 65 2a 29 3b 0a 53 51 4c  beDb(Vdbe*);.SQL
27180 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
27190 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 53   sqlite3VdbeSetS
271a0 71 6c 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74 20  ql(Vdbe*, const 
271b0 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 3b  char *z, int n);
271c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
271d0 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
271e0 74 65 33 56 64 62 65 47 65 74 53 71 6c 28 56 64  te3VdbeGetSql(Vd
271f0 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  be*);.SQLITE_PRI
27200 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
27210 33 56 64 62 65 53 77 61 70 28 56 64 62 65 2a 2c  3VdbeSwap(Vdbe*,
27220 56 64 62 65 2a 29 3b 0a 0a 23 69 66 6e 64 65 66  Vdbe*);..#ifndef
27230 20 4e 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50   NDEBUG.SQLITE_P
27240 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
27250 6c 69 74 65 33 56 64 62 65 43 6f 6d 6d 65 6e 74  lite3VdbeComment
27260 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74 20 63 68  (Vdbe*, const ch
27270 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 23 20 64 65 66  ar*, ...);.# def
27280 69 6e 65 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28  ine VdbeComment(
27290 58 29 20 20 73 71 6c 69 74 65 33 56 64 62 65 43  X)  sqlite3VdbeC
272a0 6f 6d 6d 65 6e 74 20 58 0a 23 65 6c 73 65 0a 23  omment X.#else.#
272b0 20 64 65 66 69 6e 65 20 56 64 62 65 43 6f 6d 6d   define VdbeComm
272c0 65 6e 74 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23  ent(X).#endif..#
272d0 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  endif../********
272e0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64  ****** End of vd
272f0 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  be.h ***********
27300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27320 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
27330 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e  ****** Continuin
27340 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20  g where we left 
27350 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74  off in sqliteInt
27360 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
27370 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
27380 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 62  ****** Include b
27390 74 72 65 65 2e 68 20 69 6e 20 74 68 65 20 6d 69  tree.h in the mi
273a0 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e  ddle of sqliteIn
273b0 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
273c0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
273d0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
273e0 65 20 62 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a  e btree.h ******
273f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27410 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
27420 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a  1 September 15.*
27430 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
27440 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
27450 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
27460 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
27470 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
27480 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
27490 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
274a0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
274b0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
274c0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
274d0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
274e0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
274f0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
27500 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
27510 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
27520 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
27530 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
27540 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
27550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
27590 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c   This header fil
275a0 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e  e defines the in
275b0 74 65 72 66 61 63 65 20 74 68 61 74 20 74 68 65  terface that the
275c0 20 73 71 6c 69 74 65 20 42 2d 54 72 65 65 20 66   sqlite B-Tree f
275d0 69 6c 65 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d  ile.** subsystem
275e0 2e 20 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20  .  See comments 
275f0 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 63 6f  in the source co
27600 64 65 20 66 6f 72 20 61 20 64 65 74 61 69 6c 65  de for a detaile
27610 64 20 64 65 73 63 72 69 70 74 69 6f 6e 0a 2a 2a  d description.**
27620 20 6f 66 20 77 68 61 74 20 65 61 63 68 20 69 6e   of what each in
27630 74 65 72 66 61 63 65 20 72 6f 75 74 69 6e 65 20  terface routine 
27640 64 6f 65 73 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29  does..**.** @(#)
27650 20 24 49 64 3a 20 62 74 72 65 65 2e 68 2c 76 20   $Id: btree.h,v 
27660 31 2e 38 32 20 32 30 30 37 2f 30 35 2f 30 38 20  1.82 2007/05/08 
27670 32 31 3a 34 35 3a 32 37 20 64 72 68 20 45 78 70  21:45:27 drh Exp
27680 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 42   $.*/.#ifndef _B
27690 54 52 45 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20  TREE_H_.#define 
276a0 5f 42 54 52 45 45 5f 48 5f 0a 0a 2f 2a 20 54 4f  _BTREE_H_../* TO
276b0 44 4f 3a 20 54 68 69 73 20 64 65 66 69 6e 69 74  DO: This definit
276c0 69 6f 6e 20 69 73 20 6a 75 73 74 20 69 6e 63 6c  ion is just incl
276d0 75 64 65 64 20 73 6f 20 6f 74 68 65 72 20 6d 6f  uded so other mo
276e0 64 75 6c 65 73 20 63 6f 6d 70 69 6c 65 2e 20 49  dules compile. I
276f0 74 0a 2a 2a 20 6e 65 65 64 73 20 74 6f 20 62 65  t.** needs to be
27700 20 72 65 76 69 73 69 74 65 64 2e 0a 2a 2f 0a 23   revisited..*/.#
27710 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 5f  define SQLITE_N_
27720 42 54 52 45 45 5f 4d 45 54 41 20 31 30 0a 0a 2f  BTREE_META 10../
27730 2a 0a 2a 2a 20 49 66 20 64 65 66 69 6e 65 64 20  *.** If defined 
27740 61 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 61 75 74  as non-zero, aut
27750 6f 2d 76 61 63 75 75 6d 20 69 73 20 65 6e 61 62  o-vacuum is enab
27760 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e 20  led by default. 
27770 4f 74 68 65 72 77 69 73 65 0a 2a 2a 20 69 74 20  Otherwise.** it 
27780 6d 75 73 74 20 62 65 20 74 75 72 6e 65 64 20 6f  must be turned o
27790 6e 20 66 6f 72 20 65 61 63 68 20 64 61 74 61 62  n for each datab
277a0 61 73 65 20 75 73 69 6e 67 20 22 50 52 41 47 4d  ase using "PRAGM
277b0 41 20 61 75 74 6f 5f 76 61 63 75 75 6d 20 3d 20  A auto_vacuum = 
277c0 31 22 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  1"..*/.#ifndef S
277d0 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55  QLITE_DEFAULT_AU
277e0 54 4f 56 41 43 55 55 4d 0a 20 20 23 64 65 66 69  TOVACUUM.  #defi
277f0 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  ne SQLITE_DEFAUL
27800 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 30 0a 23  T_AUTOVACUUM 0.#
27810 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 42  endif..#define B
27820 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f  TREE_AUTOVACUUM_
27830 4e 4f 4e 45 20 30 20 20 20 20 20 20 20 20 2f 2a  NONE 0        /*
27840 20 44 6f 20 6e 6f 74 20 64 6f 20 61 75 74 6f 2d   Do not do auto-
27850 76 61 63 75 75 6d 20 2a 2f 0a 23 64 65 66 69 6e  vacuum */.#defin
27860 65 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55  e BTREE_AUTOVACU
27870 55 4d 5f 46 55 4c 4c 20 31 20 20 20 20 20 20 20  UM_FULL 1       
27880 20 2f 2a 20 44 6f 20 66 75 6c 6c 20 61 75 74 6f   /* Do full auto
27890 2d 76 61 63 75 75 6d 20 2a 2f 0a 23 64 65 66 69  -vacuum */.#defi
278a0 6e 65 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43  ne BTREE_AUTOVAC
278b0 55 55 4d 5f 49 4e 43 52 20 32 20 20 20 20 20 20  UUM_INCR 2      
278c0 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 61 6c    /* Incremental
278d0 20 76 61 63 75 75 6d 20 2a 2f 0a 0a 2f 2a 0a 2a   vacuum */../*.*
278e0 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72  * Forward declar
278f0 61 74 69 6f 6e 73 20 6f 66 20 73 74 72 75 63 74  ations of struct
27900 75 72 65 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  ure.*/.typedef s
27910 74 72 75 63 74 20 42 74 72 65 65 20 42 74 72 65  truct Btree Btre
27920 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  e;.typedef struc
27930 74 20 42 74 43 75 72 73 6f 72 20 42 74 43 75 72  t BtCursor BtCur
27940 73 6f 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72  sor;.typedef str
27950 75 63 74 20 42 74 53 68 61 72 65 64 20 42 74 53  uct BtShared BtS
27960 68 61 72 65 64 3b 0a 0a 0a 53 51 4c 49 54 45 5f  hared;...SQLITE_
27970 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
27980 74 65 33 42 74 72 65 65 4f 70 65 6e 28 0a 20 20  te3BtreeOpen(.  
27990 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
279a0 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 4e 61 6d 65  ename,   /* Name
279b0 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69 6c   of database fil
279c0 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 73  e to open */.  s
279d0 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
279e0 20 20 20 20 20 20 20 20 2f 2a 20 41 73 73 6f 63          /* Assoc
279f0 69 61 74 65 64 20 64 61 74 61 62 61 73 65 20 63  iated database c
27a00 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 42  onnection */.  B
27a10 74 72 65 65 20 2a 2a 2c 20 20 20 20 20 20 20 20  tree **,        
27a20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
27a30 6e 20 6f 70 65 6e 20 42 74 72 65 65 2a 20 68 65  n open Btree* he
27a40 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67  re */.  int flag
27a50 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s               
27a60 20 2f 2a 20 46 6c 61 67 73 20 2a 2f 0a 29 3b 0a   /* Flags */.);.
27a70 0a 2f 2a 20 54 68 65 20 66 6c 61 67 73 20 70 61  ./* The flags pa
27a80 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74  rameter to sqlit
27a90 65 33 42 74 72 65 65 4f 70 65 6e 20 63 61 6e 20  e3BtreeOpen can 
27aa0 62 65 20 74 68 65 20 62 69 74 77 69 73 65 20 6f  be the bitwise o
27ab0 72 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c  r of the.** foll
27ac0 6f 77 69 6e 67 20 76 61 6c 75 65 73 2e 0a 2a 2a  owing values..**
27ad0 0a 2a 2a 20 4e 4f 54 45 3a 20 20 54 68 65 73 65  .** NOTE:  These
27ae0 20 76 61 6c 75 65 73 20 6d 75 73 74 20 6d 61 74   values must mat
27af0 63 68 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  ch the correspon
27b00 64 69 6e 67 20 50 41 47 45 52 5f 20 76 61 6c 75  ding PAGER_ valu
27b10 65 73 20 69 6e 0a 2a 2a 20 70 61 67 65 72 2e 68  es in.** pager.h
27b20 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52  ..*/.#define BTR
27b30 45 45 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20  EE_OMIT_JOURNAL 
27b40 20 31 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 75 73   1  /* Do not us
27b50 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 4e 6f 20 61  e journal.  No a
27b60 72 67 75 6d 65 6e 74 20 2a 2f 0a 23 64 65 66 69  rgument */.#defi
27b70 6e 65 20 42 54 52 45 45 5f 4e 4f 5f 52 45 41 44  ne BTREE_NO_READ
27b80 4c 4f 43 4b 20 20 20 32 20 20 2f 2a 20 4f 6d 69  LOCK   2  /* Omi
27b90 74 20 72 65 61 64 6c 6f 63 6b 73 20 6f 6e 20 72  t readlocks on r
27ba0 65 61 64 6f 6e 6c 79 20 66 69 6c 65 73 20 2a 2f  eadonly files */
27bb0 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 4d  .#define BTREE_M
27bc0 45 4d 4f 52 59 20 20 20 20 20 20 20 20 34 20 20  EMORY        4  
27bd0 2f 2a 20 49 6e 2d 6d 65 6d 6f 72 79 20 44 42 2e  /* In-memory DB.
27be0 20 20 4e 6f 20 61 72 67 75 6d 65 6e 74 20 2a 2f    No argument */
27bf0 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
27c00 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
27c10 65 43 6c 6f 73 65 28 42 74 72 65 65 2a 29 3b 0a  eClose(Btree*);.
27c20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
27c30 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53  nt sqlite3BtreeS
27c40 65 74 42 75 73 79 48 61 6e 64 6c 65 72 28 42 74  etBusyHandler(Bt
27c50 72 65 65 2a 2c 42 75 73 79 48 61 6e 64 6c 65 72  ree*,BusyHandler
27c60 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
27c70 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
27c80 72 65 65 53 65 74 43 61 63 68 65 53 69 7a 65 28  reeSetCacheSize(
27c90 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c  Btree*,int);.SQL
27ca0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
27cb0 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 53  sqlite3BtreeSetS
27cc0 61 66 65 74 79 4c 65 76 65 6c 28 42 74 72 65 65  afetyLevel(Btree
27cd0 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49  *,int,int);.SQLI
27ce0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
27cf0 71 6c 69 74 65 33 42 74 72 65 65 53 79 6e 63 44  qlite3BtreeSyncD
27d00 69 73 61 62 6c 65 64 28 42 74 72 65 65 2a 29 3b  isabled(Btree*);
27d10 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
27d20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
27d30 53 65 74 50 61 67 65 53 69 7a 65 28 42 74 72 65  SetPageSize(Btre
27d40 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c  e*,int,int);.SQL
27d50 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
27d60 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50  sqlite3BtreeGetP
27d70 61 67 65 53 69 7a 65 28 42 74 72 65 65 2a 29 3b  ageSize(Btree*);
27d80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
27d90 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
27da0 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 42 74 72  MaxPageCount(Btr
27db0 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  ee*,int);.SQLITE
27dc0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
27dd0 69 74 65 33 42 74 72 65 65 47 65 74 52 65 73 65  ite3BtreeGetRese
27de0 72 76 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c  rve(Btree*);.SQL
27df0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
27e00 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 41  sqlite3BtreeSetA
27e10 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65 65 20  utoVacuum(Btree 
27e20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
27e30 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
27e40 74 65 33 42 74 72 65 65 47 65 74 41 75 74 6f 56  te3BtreeGetAutoV
27e50 61 63 75 75 6d 28 42 74 72 65 65 20 2a 29 3b 0a  acuum(Btree *);.
27e60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
27e70 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 42  nt sqlite3BtreeB
27e80 65 67 69 6e 54 72 61 6e 73 28 42 74 72 65 65 2a  eginTrans(Btree*
27e90 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
27ea0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
27eb0 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73  3BtreeCommitPhas
27ec0 65 4f 6e 65 28 42 74 72 65 65 2a 2c 20 63 6f 6e  eOne(Btree*, con
27ed0 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72  st char *zMaster
27ee0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
27ef0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
27f00 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f  eeCommitPhaseTwo
27f10 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45  (Btree*);.SQLITE
27f20 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
27f30 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 28  ite3BtreeCommit(
27f40 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Btree*);.SQLITE_
27f50 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
27f60 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b  te3BtreeRollback
27f70 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45  (Btree*);.SQLITE
27f80 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
27f90 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 53 74  ite3BtreeBeginSt
27fa0 6d 74 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49  mt(Btree*);.SQLI
27fb0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
27fc0 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69  qlite3BtreeCommi
27fd0 74 53 74 6d 74 28 42 74 72 65 65 2a 29 3b 0a 53  tStmt(Btree*);.S
27fe0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
27ff0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f  t sqlite3BtreeRo
28000 6c 6c 62 61 63 6b 53 74 6d 74 28 42 74 72 65 65  llbackStmt(Btree
28010 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
28020 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
28030 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 42  reeCreateTable(B
28040 74 72 65 65 2a 2c 20 69 6e 74 2a 2c 20 69 6e 74  tree*, int*, int
28050 20 66 6c 61 67 73 29 3b 0a 53 51 4c 49 54 45 5f   flags);.SQLITE_
28060 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
28070 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e  te3BtreeIsInTran
28080 73 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54  s(Btree*);.SQLIT
28090 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
280a0 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 53 74  lite3BtreeIsInSt
280b0 6d 74 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49  mt(Btree*);.SQLI
280c0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
280d0 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 52  qlite3BtreeIsInR
280e0 65 61 64 54 72 61 6e 73 28 42 74 72 65 65 2a 29  eadTrans(Btree*)
280f0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
28100 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74   void *sqlite3Bt
28110 72 65 65 53 63 68 65 6d 61 28 42 74 72 65 65 20  reeSchema(Btree 
28120 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28  *, int, void(*)(
28130 76 6f 69 64 20 2a 29 29 3b 0a 53 51 4c 49 54 45  void *));.SQLITE
28140 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
28150 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 4c  ite3BtreeSchemaL
28160 6f 63 6b 65 64 28 42 74 72 65 65 20 2a 29 3b 0a  ocked(Btree *);.
28170 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
28180 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  nt sqlite3BtreeL
28190 6f 63 6b 54 61 62 6c 65 28 42 74 72 65 65 20 2a  ockTable(Btree *
281a0 2c 20 69 6e 74 2c 20 75 38 29 3b 0a 0a 53 51 4c  , int, u8);..SQL
281b0 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
281c0 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42  t char *sqlite3B
281d0 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28  treeGetFilename(
281e0 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 45  Btree *);.SQLITE
281f0 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63  _PRIVATE const c
28200 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65  har *sqlite3Btre
28210 65 47 65 74 44 69 72 6e 61 6d 65 28 42 74 72 65  eGetDirname(Btre
28220 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  e *);.SQLITE_PRI
28230 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20  VATE const char 
28240 2a 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74  *sqlite3BtreeGet
28250 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 42 74 72 65  Journalname(Btre
28260 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  e *);.SQLITE_PRI
28270 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
28280 42 74 72 65 65 43 6f 70 79 46 69 6c 65 28 42 74  BtreeCopyFile(Bt
28290 72 65 65 20 2a 2c 20 42 74 72 65 65 20 2a 29 3b  ree *, Btree *);
282a0 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
282b0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
282c0 65 49 6e 63 72 56 61 63 75 75 6d 28 42 74 72 65  eIncrVacuum(Btre
282d0 65 20 2a 29 3b 0a 0a 2f 2a 20 54 68 65 20 66 6c  e *);../* The fl
282e0 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f  ags parameter to
282f0 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 65   sqlite3BtreeCre
28300 61 74 65 54 61 62 6c 65 20 63 61 6e 20 62 65 20  ateTable can be 
28310 74 68 65 20 62 69 74 77 69 73 65 20 4f 52 0a 2a  the bitwise OR.*
28320 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  * of the followi
28330 6e 67 20 66 6c 61 67 73 3a 0a 2a 2f 0a 23 64 65  ng flags:.*/.#de
28340 66 69 6e 65 20 42 54 52 45 45 5f 49 4e 54 4b 45  fine BTREE_INTKE
28350 59 20 20 20 20 20 31 20 20 20 20 2f 2a 20 54 61  Y     1    /* Ta
28360 62 6c 65 20 68 61 73 20 6f 6e 6c 79 20 36 34 2d  ble has only 64-
28370 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67  bit signed integ
28380 65 72 20 6b 65 79 73 20 2a 2f 0a 23 64 65 66 69  er keys */.#defi
28390 6e 65 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54  ne BTREE_ZERODAT
283a0 41 20 20 20 32 20 20 20 20 2f 2a 20 54 61 62 6c  A   2    /* Tabl
283b0 65 20 68 61 73 20 6b 65 79 73 20 6f 6e 6c 79 20  e has keys only 
283c0 2d 20 6e 6f 20 64 61 74 61 20 2a 2f 0a 23 64 65  - no data */.#de
283d0 66 69 6e 65 20 42 54 52 45 45 5f 4c 45 41 46 44  fine BTREE_LEAFD
283e0 41 54 41 20 20 20 34 20 20 20 20 2f 2a 20 44 61  ATA   4    /* Da
283f0 74 61 20 73 74 6f 72 65 64 20 69 6e 20 6c 65 61  ta stored in lea
28400 76 65 73 20 6f 6e 6c 79 2e 20 20 49 6d 70 6c 69  ves only.  Impli
28410 65 73 20 49 4e 54 4b 45 59 20 2a 2f 0a 0a 53 51  es INTKEY */..SQ
28420 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
28430 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f   sqlite3BtreeDro
28440 70 54 61 62 6c 65 28 42 74 72 65 65 2a 2c 20 69  pTable(Btree*, i
28450 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54  nt, int*);.SQLIT
28460 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
28470 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 54  lite3BtreeClearT
28480 61 62 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e 74  able(Btree*, int
28490 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
284a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
284b0 65 65 47 65 74 4d 65 74 61 28 42 74 72 65 65 2a  eeGetMeta(Btree*
284c0 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32 20 2a  , int idx, u32 *
284d0 70 56 61 6c 75 65 29 3b 0a 53 51 4c 49 54 45 5f  pValue);.SQLITE_
284e0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
284f0 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d 65  te3BtreeUpdateMe
28500 74 61 28 42 74 72 65 65 2a 2c 20 69 6e 74 20 69  ta(Btree*, int i
28510 64 78 2c 20 75 33 32 20 76 61 6c 75 65 29 3b 0a  dx, u32 value);.
28520 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
28530 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
28540 43 75 72 73 6f 72 28 0a 20 20 42 74 72 65 65 2a  Cursor(.  Btree*
28550 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
28560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
28570 2a 20 42 54 72 65 65 20 63 6f 6e 74 61 69 6e 69  * BTree containi
28580 6e 67 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e  ng table to open
28590 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65   */.  int iTable
285a0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
285b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
285c0 64 65 78 20 6f 66 20 72 6f 6f 74 20 70 61 67 65  dex of root page
285d0 20 2a 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61 67   */.  int wrFlag
285e0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
285f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20             /* 1 
28600 66 6f 72 20 77 72 69 74 69 6e 67 2e 20 20 30 20  for writing.  0 
28610 66 6f 72 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f  for read-only */
28620 0a 20 20 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c  .  int(*)(void*,
28630 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  int,const void*,
28640 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29  int,const void*)
28650 2c 20 20 2f 2a 20 4b 65 79 20 63 6f 6d 70 61 72  ,  /* Key compar
28660 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  ison function */
28670 0a 20 20 76 6f 69 64 2a 2c 20 20 20 20 20 20 20  .  void*,       
28680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28690 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
286a0 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 63 6f 6d   argument to com
286b0 70 61 72 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  pare function */
286c0 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 2a 70 70  .  BtCursor **pp
286d0 43 75 72 73 6f 72 20 20 20 20 20 20 20 20 20 20  Cursor          
286e0 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
286f0 6e 65 64 20 63 75 72 73 6f 72 20 2a 2f 0a 29 3b  ned cursor */.);
28700 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
28710 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
28720 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 42 74 43  eCloseCursor(BtC
28730 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  ursor*);.SQLITE_
28740 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
28750 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 42  te3BtreeMoveto(B
28760 74 43 75 72 73 6f 72 2a 2c 63 6f 6e 73 74 20 76  tCursor*,const v
28770 6f 69 64 20 2a 70 4b 65 79 2c 69 36 34 20 6e 4b  oid *pKey,i64 nK
28780 65 79 2c 69 6e 74 20 62 69 61 73 2c 69 6e 74 20  ey,int bias,int 
28790 2a 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50  *pRes);.SQLITE_P
287a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
287b0 65 33 42 74 72 65 65 44 65 6c 65 74 65 28 42 74  e3BtreeDelete(Bt
287c0 43 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45  Cursor*);.SQLITE
287d0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
287e0 69 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 28  ite3BtreeInsert(
287f0 42 74 43 75 72 73 6f 72 2a 2c 20 63 6f 6e 73 74  BtCursor*, const
28800 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 36 34   void *pKey, i64
28810 20 6e 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20   nKey,.         
28820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28830 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 76           const v
28840 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74 20  oid *pData, int 
28850 6e 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20  nData,.         
28860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28870 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 5a 65           int nZe
28880 72 6f 2c 20 69 6e 74 20 62 69 61 73 29 3b 0a 53  ro, int bias);.S
28890 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
288a0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 69  t sqlite3BtreeFi
288b0 72 73 74 28 42 74 43 75 72 73 6f 72 2a 2c 20 69  rst(BtCursor*, i
288c0 6e 74 20 2a 70 52 65 73 29 3b 0a 53 51 4c 49 54  nt *pRes);.SQLIT
288d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
288e0 6c 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 42  lite3BtreeLast(B
288f0 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70  tCursor*, int *p
28900 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  Res);.SQLITE_PRI
28910 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
28920 42 74 72 65 65 4e 65 78 74 28 42 74 43 75 72 73  BtreeNext(BtCurs
28930 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b  or*, int *pRes);
28940 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
28950 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
28960 45 6f 66 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a  Eof(BtCursor*);.
28970 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
28980 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 46  nt sqlite3BtreeF
28990 6c 61 67 73 28 42 74 43 75 72 73 6f 72 2a 29 3b  lags(BtCursor*);
289a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
289b0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
289c0 50 72 65 76 69 6f 75 73 28 42 74 43 75 72 73 6f  Previous(BtCurso
289d0 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a  r*, int *pRes);.
289e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
289f0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b  nt sqlite3BtreeK
28a00 65 79 53 69 7a 65 28 42 74 43 75 72 73 6f 72 2a  eySize(BtCursor*
28a10 2c 20 69 36 34 20 2a 70 53 69 7a 65 29 3b 0a 53  , i64 *pSize);.S
28a20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
28a30 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65  t sqlite3BtreeKe
28a40 79 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32  y(BtCursor*, u32
28a50 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74   offset, u32 amt
28a60 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45  , void*);.SQLITE
28a70 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76  _PRIVATE const v
28a80 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65  oid *sqlite3Btre
28a90 65 4b 65 79 46 65 74 63 68 28 42 74 43 75 72 73  eKeyFetch(BtCurs
28aa0 6f 72 2a 2c 20 69 6e 74 20 2a 70 41 6d 74 29 3b  or*, int *pAmt);
28ab0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
28ac0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
28ad0 74 65 33 42 74 72 65 65 44 61 74 61 46 65 74 63  te3BtreeDataFetc
28ae0 68 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74  h(BtCursor*, int
28af0 20 2a 70 41 6d 74 29 3b 0a 53 51 4c 49 54 45 5f   *pAmt);.SQLITE_
28b00 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
28b10 74 65 33 42 74 72 65 65 44 61 74 61 53 69 7a 65  te3BtreeDataSize
28b20 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 20  (BtCursor*, u32 
28b30 2a 70 53 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f  *pSize);.SQLITE_
28b40 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
28b50 74 65 33 42 74 72 65 65 44 61 74 61 28 42 74 43  te3BtreeData(BtC
28b60 75 72 73 6f 72 2a 2c 20 75 33 32 20 6f 66 66 73  ursor*, u32 offs
28b70 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69  et, u32 amt, voi
28b80 64 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49  d*);..SQLITE_PRI
28b90 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74  VATE char *sqlit
28ba0 65 33 42 74 72 65 65 49 6e 74 65 67 72 69 74 79  e3BtreeIntegrity
28bb0 43 68 65 63 6b 28 42 74 72 65 65 2a 2c 20 69 6e  Check(Btree*, in
28bc0 74 20 2a 61 52 6f 6f 74 2c 20 69 6e 74 20 6e 52  t *aRoot, int nR
28bd0 6f 6f 74 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 3b  oot, int, int*);
28be0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
28bf0 73 74 72 75 63 74 20 50 61 67 65 72 20 2a 73 71  struct Pager *sq
28c00 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28  lite3BtreePager(
28c10 42 74 72 65 65 2a 29 3b 0a 0a 53 51 4c 49 54 45  Btree*);..SQLITE
28c20 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
28c30 69 74 65 33 42 74 72 65 65 50 75 74 44 61 74 61  ite3BtreePutData
28c40 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 20  (BtCursor*, u32 
28c50 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c  offset, u32 amt,
28c60 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f   void*);.SQLITE_
28c70 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
28c80 69 74 65 33 42 74 72 65 65 43 61 63 68 65 4f 76  ite3BtreeCacheOv
28c90 65 72 66 6c 6f 77 28 42 74 43 75 72 73 6f 72 20  erflow(BtCursor 
28ca0 2a 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  *);..#ifdef SQLI
28cb0 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50  TE_TEST.SQLITE_P
28cc0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
28cd0 65 33 42 74 72 65 65 43 75 72 73 6f 72 49 6e 66  e3BtreeCursorInf
28ce0 6f 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74  o(BtCursor*, int
28cf0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
28d00 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
28d10 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 4c  ite3BtreeCursorL
28d20 69 73 74 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c  ist(Btree*);.SQL
28d30 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
28d40 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65  sqlite3BtreePage
28d50 44 75 6d 70 28 42 74 72 65 65 2a 2c 20 69 6e 74  Dump(Btree*, int
28d60 2c 20 69 6e 74 20 72 65 63 75 72 73 69 76 65 29  , int recursive)
28d70 3b 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66  ;.#endif..#endif
28d80 20 2f 2a 20 5f 42 54 52 45 45 5f 48 5f 20 2a 2f   /* _BTREE_H_ */
28d90 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
28da0 2a 20 45 6e 64 20 6f 66 20 62 74 72 65 65 2e 68  * End of btree.h
28db0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
28dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28de0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
28df0 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
28e00 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
28e10 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a  n sqliteInt.h **
28e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28e30 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
28e40 2a 20 49 6e 63 6c 75 64 65 20 70 61 67 65 72 2e  * Include pager.
28e50 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
28e60 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a  of sqliteInt.h *
28e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28e80 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
28e90 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 61 67  * Begin file pag
28ea0 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  er.h ***********
28eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28ed0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
28ee0 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20  tember 15.**.** 
28ef0 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
28f00 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
28f10 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
28f20 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
28f30 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
28f40 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
28f50 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
28f60 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
28f70 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
28f80 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
28f90 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
28fa0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
28fb0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
28fc0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
28fd0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
28fe0 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
28ff0 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
29000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29040 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
29050 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 65 66   header file def
29060 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66 61  ines the interfa
29070 63 65 20 74 68 61 74 20 74 68 65 20 73 71 6c 69  ce that the sqli
29080 74 65 20 70 61 67 65 20 63 61 63 68 65 0a 2a 2a  te page cache.**
29090 20 73 75 62 73 79 73 74 65 6d 2e 20 20 54 68 65   subsystem.  The
290a0 20 70 61 67 65 20 63 61 63 68 65 20 73 75 62 73   page cache subs
290b0 79 73 74 65 6d 20 72 65 61 64 73 20 61 6e 64 20  ystem reads and 
290c0 77 72 69 74 65 73 20 61 20 66 69 6c 65 20 61 20  writes a file a 
290d0 70 61 67 65 0a 2a 2a 20 61 74 20 61 20 74 69 6d  page.** at a tim
290e0 65 20 61 6e 64 20 70 72 6f 76 69 64 65 73 20 61  e and provides a
290f0 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 72 6f 6c   journal for rol
29100 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 40 28 23  lback..**.** @(#
29110 29 20 24 49 64 3a 20 70 61 67 65 72 2e 68 2c 76  ) $Id: pager.h,v
29120 20 31 2e 36 31 20 32 30 30 37 2f 30 35 2f 30 38   1.61 2007/05/08
29130 20 32 31 3a 34 35 3a 32 38 20 64 72 68 20 45 78   21:45:28 drh Ex
29140 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  p $.*/..#ifndef 
29150 5f 50 41 47 45 52 5f 48 5f 0a 23 64 65 66 69 6e  _PAGER_H_.#defin
29160 65 20 5f 50 41 47 45 52 5f 48 5f 0a 0a 2f 2a 0a  e _PAGER_H_../*.
29170 2a 2a 20 54 68 65 20 74 79 70 65 20 75 73 65 64  ** The type used
29180 20 74 6f 20 72 65 70 72 65 73 65 6e 74 20 61 20   to represent a 
29190 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 20 54 68  page number.  Th
291a0 65 20 66 69 72 73 74 20 70 61 67 65 20 69 6e 20  e first page in 
291b0 61 20 66 69 6c 65 0a 2a 2a 20 69 73 20 63 61 6c  a file.** is cal
291c0 6c 65 64 20 70 61 67 65 20 31 2e 20 20 30 20 69  led page 1.  0 i
291d0 73 20 75 73 65 64 20 74 6f 20 72 65 70 72 65 73  s used to repres
291e0 65 6e 74 20 22 6e 6f 74 20 61 20 70 61 67 65 22  ent "not a page"
291f0 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75 6e 73  ..*/.typedef uns
29200 69 67 6e 65 64 20 69 6e 74 20 50 67 6e 6f 3b 0a  igned int Pgno;.
29210 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 6f 70 65 6e  ./*.** Each open
29220 20 66 69 6c 65 20 69 73 20 6d 61 6e 61 67 65 64   file is managed
29230 20 62 79 20 61 20 73 65 70 61 72 61 74 65 20 69   by a separate i
29240 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 22  nstance of the "
29250 50 61 67 65 72 22 20 73 74 72 75 63 74 75 72 65  Pager" structure
29260 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
29270 75 63 74 20 50 61 67 65 72 20 50 61 67 65 72 3b  uct Pager Pager;
29280 0a 0a 2f 2a 0a 2a 2a 20 48 61 6e 64 6c 65 20 74  ../*.** Handle t
29290 79 70 65 20 66 6f 72 20 70 61 67 65 73 2e 0a 2a  ype for pages..*
292a0 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
292b0 20 50 67 48 64 72 20 44 62 50 61 67 65 3b 0a 0a   PgHdr DbPage;..
292c0 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61  /*.** Allowed va
292d0 6c 75 65 73 20 66 6f 72 20 74 68 65 20 66 6c 61  lues for the fla
292e0 67 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  gs parameter to 
292f0 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e  sqlite3PagerOpen
29300 28 29 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20  ()..**.** NOTE: 
29310 54 68 69 73 20 76 61 6c 75 65 73 20 6d 75 73 74  This values must
29320 20 6d 61 74 63 68 20 74 68 65 20 63 6f 72 72 65   match the corre
29330 73 70 6f 6e 64 69 6e 67 20 42 54 52 45 45 5f 20  sponding BTREE_ 
29340 76 61 6c 75 65 73 20 69 6e 20 62 74 72 65 65 2e  values in btree.
29350 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41  h..*/.#define PA
29360 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c  GER_OMIT_JOURNAL
29370 20 20 30 78 30 30 30 31 20 20 20 20 2f 2a 20 44    0x0001    /* D
29380 6f 20 6e 6f 74 20 75 73 65 20 61 20 72 6f 6c 6c  o not use a roll
29390 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a  back journal */.
293a0 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4e 4f  #define PAGER_NO
293b0 5f 52 45 41 44 4c 4f 43 4b 20 20 20 30 78 30 30  _READLOCK   0x00
293c0 30 32 20 20 20 20 2f 2a 20 4f 6d 69 74 20 72 65  02    /* Omit re
293d0 61 64 6c 6f 63 6b 73 20 6f 6e 20 72 65 61 64 6f  adlocks on reado
293e0 6e 6c 79 20 66 69 6c 65 73 20 2a 2f 0a 0a 2f 2a  nly files */../*
293f0 0a 2a 2a 20 56 61 6c 69 64 20 76 61 6c 75 65 73  .** Valid values
29400 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20   for the second 
29410 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69  argument to sqli
29420 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d  te3PagerLockingM
29430 6f 64 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ode()..*/.#defin
29440 65 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d  e PAGER_LOCKINGM
29450 4f 44 45 5f 51 55 45 52 59 20 20 20 20 20 20 2d  ODE_QUERY      -
29460 31 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f  1.#define PAGER_
29470 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d  LOCKINGMODE_NORM
29480 41 4c 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e  AL      0.#defin
29490 65 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d  e PAGER_LOCKINGM
294a0 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20 20 20  ODE_EXCLUSIVE   
294b0 31 0a 0a 2f 2a 0a 2a 2a 20 53 65 65 20 73 6f 75  1../*.** See sou
294c0 72 63 65 20 63 6f 64 65 20 63 6f 6d 6d 65 6e 74  rce code comment
294d0 73 20 66 6f 72 20 61 20 64 65 74 61 69 6c 65 64  s for a detailed
294e0 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20   description of 
294f0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a  the following.**
29500 20 72 6f 75 74 69 6e 65 73 3a 0a 2a 2f 0a 53 51   routines:.*/.SQ
29510 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
29520 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65   sqlite3PagerOpe
29530 6e 28 50 61 67 65 72 20 2a 2a 70 70 50 61 67 65  n(Pager **ppPage
29540 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  r, const char *z
29550 46 69 6c 65 6e 61 6d 65 2c 0a 20 20 20 20 20 20  Filename,.      
29560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
29570 6e 74 20 6e 45 78 74 72 61 2c 20 69 6e 74 20 66  nt nExtra, int f
29580 6c 61 67 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52  lags);.SQLITE_PR
29590 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
295a0 65 33 50 61 67 65 72 53 65 74 42 75 73 79 68 61  e3PagerSetBusyha
295b0 6e 64 6c 65 72 28 50 61 67 65 72 2a 2c 20 42 75  ndler(Pager*, Bu
295c0 73 79 48 61 6e 64 6c 65 72 20 2a 70 42 75 73 79  syHandler *pBusy
295d0 48 61 6e 64 6c 65 72 29 3b 0a 53 51 4c 49 54 45  Handler);.SQLITE
295e0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
295f0 6c 69 74 65 33 50 61 67 65 72 53 65 74 44 65 73  lite3PagerSetDes
29600 74 72 75 63 74 6f 72 28 50 61 67 65 72 2a 2c 20  tructor(Pager*, 
29610 76 6f 69 64 28 2a 29 28 44 62 50 61 67 65 2a 2c  void(*)(DbPage*,
29620 69 6e 74 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52  int));.SQLITE_PR
29630 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
29640 65 33 50 61 67 65 72 53 65 74 52 65 69 6e 69 74  e3PagerSetReinit
29650 65 72 28 50 61 67 65 72 2a 2c 20 76 6f 69 64 28  er(Pager*, void(
29660 2a 29 28 44 62 50 61 67 65 2a 2c 69 6e 74 29 29  *)(DbPage*,int))
29670 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
29680 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
29690 72 53 65 74 50 61 67 65 73 69 7a 65 28 50 61 67  rSetPagesize(Pag
296a0 65 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  er*, int);.SQLIT
296b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
296c0 6c 69 74 65 33 50 61 67 65 72 4d 61 78 50 61 67  lite3PagerMaxPag
296d0 65 43 6f 75 6e 74 28 50 61 67 65 72 2a 2c 20 69  eCount(Pager*, i
296e0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
296f0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
29700 61 67 65 72 52 65 61 64 46 69 6c 65 68 65 61 64  agerReadFilehead
29710 65 72 28 50 61 67 65 72 2a 2c 20 69 6e 74 2c 20  er(Pager*, int, 
29720 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 3b  unsigned char*);
29730 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
29740 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65  void sqlite3Page
29750 72 53 65 74 43 61 63 68 65 73 69 7a 65 28 50 61  rSetCachesize(Pa
29760 67 65 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  ger*, int);.SQLI
29770 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
29780 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65  qlite3PagerClose
29790 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 3b  (Pager *pPager);
297a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
297b0 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
297c0 41 63 71 75 69 72 65 28 50 61 67 65 72 20 2a 70  Acquire(Pager *p
297d0 50 61 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f  Pager, Pgno pgno
297e0 2c 20 44 62 50 61 67 65 20 2a 2a 70 70 50 61 67  , DbPage **ppPag
297f0 65 2c 20 69 6e 74 20 63 6c 72 46 6c 61 67 29 3b  e, int clrFlag);
29800 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
29810 50 61 67 65 72 47 65 74 28 41 2c 42 2c 43 29 20  PagerGet(A,B,C) 
29820 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 75  sqlite3PagerAcqu
29830 69 72 65 28 41 2c 42 2c 43 2c 30 29 0a 53 51 4c  ire(A,B,C,0).SQL
29840 49 54 45 5f 50 52 49 56 41 54 45 20 44 62 50 61  ITE_PRIVATE DbPa
29850 67 65 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72  ge *sqlite3Pager
29860 4c 6f 6f 6b 75 70 28 50 61 67 65 72 20 2a 70 50  Lookup(Pager *pP
29870 61 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29  ager, Pgno pgno)
29880 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
29890 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
298a0 72 52 65 66 28 44 62 50 61 67 65 2a 29 3b 0a 53  rRef(DbPage*);.S
298b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
298c0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e  t sqlite3PagerUn
298d0 72 65 66 28 44 62 50 61 67 65 2a 29 3b 0a 53 51  ref(DbPage*);.SQ
298e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
298f0 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
29900 74 65 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c  te(DbPage*);.SQL
29910 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
29920 73 71 6c 69 74 65 33 50 61 67 65 72 4f 76 65 72  sqlite3PagerOver
29930 77 72 69 74 65 28 50 61 67 65 72 20 2a 70 50 61  write(Pager *pPa
29940 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20  ger, Pgno pgno, 
29950 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  void*);.SQLITE_P
29960 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
29970 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74  e3PagerPagecount
29980 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45  (Pager*);.SQLITE
29990 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
299a0 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74  ite3PagerTruncat
299b0 65 28 50 61 67 65 72 2a 2c 50 67 6e 6f 29 3b 0a  e(Pager*,Pgno);.
299c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
299d0 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 42  nt sqlite3PagerB
299e0 65 67 69 6e 28 44 62 50 61 67 65 2a 2c 20 69 6e  egin(DbPage*, in
299f0 74 20 65 78 46 6c 61 67 29 3b 0a 53 51 4c 49 54  t exFlag);.SQLIT
29a00 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
29a10 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74  lite3PagerCommit
29a20 50 68 61 73 65 4f 6e 65 28 50 61 67 65 72 2a 2c  PhaseOne(Pager*,
29a30 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73  const char *zMas
29a40 74 65 72 2c 20 50 67 6e 6f 29 3b 0a 53 51 4c 49  ter, Pgno);.SQLI
29a50 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
29a60 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69  qlite3PagerCommi
29a70 74 50 68 61 73 65 54 77 6f 28 50 61 67 65 72 2a  tPhaseTwo(Pager*
29a80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
29a90 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
29aa0 65 72 52 6f 6c 6c 62 61 63 6b 28 50 61 67 65 72  erRollback(Pager
29ab0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
29ac0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
29ad0 67 65 72 49 73 72 65 61 64 6f 6e 6c 79 28 50 61  gerIsreadonly(Pa
29ae0 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ger*);.SQLITE_PR
29af0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
29b00 33 50 61 67 65 72 53 74 6d 74 42 65 67 69 6e 28  3PagerStmtBegin(
29b10 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  Pager*);.SQLITE_
29b20 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
29b30 74 65 33 50 61 67 65 72 53 74 6d 74 43 6f 6d 6d  te3PagerStmtComm
29b40 69 74 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49  it(Pager*);.SQLI
29b50 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
29b60 71 6c 69 74 65 33 50 61 67 65 72 53 74 6d 74 52  qlite3PagerStmtR
29b70 6f 6c 6c 62 61 63 6b 28 50 61 67 65 72 2a 29 3b  ollback(Pager*);
29b80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
29b90 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65  void sqlite3Page
29ba0 72 44 6f 6e 74 52 6f 6c 6c 62 61 63 6b 28 44 62  rDontRollback(Db
29bb0 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Page*);.SQLITE_P
29bc0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
29bd0 74 65 33 50 61 67 65 72 44 6f 6e 74 57 72 69 74  te3PagerDontWrit
29be0 65 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49  e(DbPage*);.SQLI
29bf0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
29c00 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f  qlite3PagerRefco
29c10 75 6e 74 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c  unt(Pager*);.SQL
29c20 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
29c30 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74   sqlite3PagerSet
29c40 53 61 66 65 74 79 4c 65 76 65 6c 28 50 61 67 65  SafetyLevel(Page
29c50 72 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c  r*,int,int);.SQL
29c60 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
29c70 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 50  t char *sqlite3P
29c80 61 67 65 72 46 69 6c 65 6e 61 6d 65 28 50 61 67  agerFilename(Pag
29c90 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  er*);.SQLITE_PRI
29ca0 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20  VATE const char 
29cb0 2a 73 71 6c 69 74 65 33 50 61 67 65 72 44 69 72  *sqlite3PagerDir
29cc0 6e 61 6d 65 28 50 61 67 65 72 2a 29 3b 0a 53 51  name(Pager*);.SQ
29cd0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
29ce0 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
29cf0 50 61 67 65 72 4a 6f 75 72 6e 61 6c 6e 61 6d 65  PagerJournalname
29d00 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45  (Pager*);.SQLITE
29d10 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
29d20 69 74 65 33 50 61 67 65 72 4e 6f 73 79 6e 63 28  ite3PagerNosync(
29d30 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  Pager*);.SQLITE_
29d40 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
29d50 74 65 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65  te3PagerMovepage
29d60 28 50 61 67 65 72 2a 2c 44 62 50 61 67 65 2a 2c  (Pager*,DbPage*,
29d70 50 67 6e 6f 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Pgno);.SQLITE_PR
29d80 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
29d90 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28  te3PagerGetData(
29da0 44 62 50 61 67 65 20 2a 29 3b 20 0a 53 51 4c 49  DbPage *); .SQLI
29db0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
29dc0 2a 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74  *sqlite3PagerGet
29dd0 45 78 74 72 61 28 44 62 50 61 67 65 20 2a 29 3b  Extra(DbPage *);
29de0 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45   .SQLITE_PRIVATE
29df0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
29e00 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 50 61 67  rLockingMode(Pag
29e10 65 72 20 2a 2c 20 69 6e 74 29 3b 0a 0a 23 69 66  er *, int);..#if
29e20 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
29e30 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41  ENABLE_MEMORY_MA
29e40 4e 41 47 45 4d 45 4e 54 29 20 26 26 20 21 64 65  NAGEMENT) && !de
29e50 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
29e60 54 5f 44 49 53 4b 49 4f 29 0a 53 51 4c 49 54 45  T_DISKIO).SQLITE
29e70 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73  _PRIVATE   int s
29e80 71 6c 69 74 65 33 50 61 67 65 72 52 65 6c 65 61  qlite3PagerRelea
29e90 73 65 4d 65 6d 6f 72 79 28 69 6e 74 29 3b 0a 23  seMemory(int);.#
29ea0 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
29eb0 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 53  LITE_HAS_CODEC.S
29ec0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
29ed0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65  void sqlite3Page
29ee0 72 53 65 74 43 6f 64 65 63 28 50 61 67 65 72 2a  rSetCodec(Pager*
29ef0 2c 76 6f 69 64 2a 28 2a 29 28 76 6f 69 64 2a 2c  ,void*(*)(void*,
29f00 76 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74 29 2c  void*,Pgno,int),
29f10 76 6f 69 64 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a  void*);.#endif..
29f20 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45  #if !defined(NDE
29f30 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  BUG) || defined(
29f40 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 53 51 4c  SQLITE_TEST).SQL
29f50 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 50 67  ITE_PRIVATE   Pg
29f60 6e 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 50  no sqlite3PagerP
29f70 61 67 65 6e 75 6d 62 65 72 28 44 62 50 61 67 65  agenumber(DbPage
29f80 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
29f90 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33  TE   int sqlite3
29fa0 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65  PagerIswriteable
29fb0 28 44 62 50 61 67 65 2a 29 3b 0a 23 65 6e 64 69  (DbPage*);.#endi
29fc0 66 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  f..#if defined(S
29fd0 51 4c 49 54 45 5f 44 45 42 55 47 29 20 7c 7c 20  QLITE_DEBUG) || 
29fe0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54  defined(SQLITE_T
29ff0 45 53 54 29 0a 53 51 4c 49 54 45 5f 50 52 49 56  EST).SQLITE_PRIV
2a000 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65  ATE   int sqlite
2a010 33 50 61 67 65 72 4c 6f 63 6b 73 74 61 74 65 28  3PagerLockstate(
2a020 50 61 67 65 72 2a 29 3b 0a 23 65 6e 64 69 66 0a  Pager*);.#endif.
2a030 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
2a040 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  EST.SQLITE_PRIVA
2a050 54 45 20 20 20 69 6e 74 20 2a 73 71 6c 69 74 65  TE   int *sqlite
2a060 33 50 61 67 65 72 53 74 61 74 73 28 50 61 67 65  3PagerStats(Page
2a070 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
2a080 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
2a090 65 33 50 61 67 65 72 52 65 66 64 75 6d 70 28 50  e3PagerRefdump(P
2a0a0 61 67 65 72 2a 29 3b 0a 20 20 69 6e 74 20 70 61  ager*);.  int pa
2a0b0 67 65 72 33 5f 72 65 66 69 6e 66 6f 5f 65 6e 61  ger3_refinfo_ena
2a0c0 62 6c 65 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66  ble;.#endif..#if
2a0d0 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
2a0e0 76 6f 69 64 20 64 69 73 61 62 6c 65 5f 73 69 6d  void disable_sim
2a0f0 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73  ulated_io_errors
2a100 28 76 6f 69 64 29 3b 0a 76 6f 69 64 20 65 6e 61  (void);.void ena
2a110 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f  ble_simulated_io
2a120 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 3b 0a 23  _errors(void);.#
2a130 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 64 69  else.# define di
2a140 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f  sable_simulated_
2a150 69 6f 5f 65 72 72 6f 72 73 28 29 0a 23 20 64 65  io_errors().# de
2a160 66 69 6e 65 20 65 6e 61 62 6c 65 5f 73 69 6d 75  fine enable_simu
2a170 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28  lated_io_errors(
2a180 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66  ).#endif..#endif
2a190 20 2f 2a 20 5f 50 41 47 45 52 5f 48 5f 20 2a 2f   /* _PAGER_H_ */
2a1a0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
2a1b0 2a 20 45 6e 64 20 6f 66 20 70 61 67 65 72 2e 68  * End of pager.h
2a1c0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2a1d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a1e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a1f0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
2a200 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
2a210 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
2a220 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a  n sqliteInt.h **
2a230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a240 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  /..#ifdef SQLITE
2a250 5f 4d 45 4d 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20  _MEMDEBUG./*.** 
2a260 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c  The following gl
2a270 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 73 20 61  obal variables a
2a280 72 65 20 75 73 65 64 20 66 6f 72 20 74 65 73 74  re used for test
2a290 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e  ing and debuggin
2a2a0 67 0a 2a 2a 20 6f 6e 6c 79 2e 20 20 54 68 65 79  g.** only.  They
2a2b0 20 6f 6e 6c 79 20 77 6f 72 6b 20 69 66 20 53 51   only work if SQ
2a2c0 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 20 69 73  LITE_MEMDEBUG is
2a2d0 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 65 78 74   defined..*/.ext
2a2e0 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  ern int sqlite3_
2a2f0 6e 4d 61 6c 6c 6f 63 3b 20 20 20 20 20 20 2f 2a  nMalloc;      /*
2a300 20 4e 75 6d 62 65 72 20 6f 66 20 73 71 6c 69 74   Number of sqlit
2a310 65 4d 61 6c 6c 6f 63 28 29 20 63 61 6c 6c 73 20  eMalloc() calls 
2a320 2a 2f 0a 65 78 74 65 72 6e 20 69 6e 74 20 73 71  */.extern int sq
2a330 6c 69 74 65 33 5f 6e 46 72 65 65 3b 20 20 20 20  lite3_nFree;    
2a340 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
2a350 20 73 71 6c 69 74 65 46 72 65 65 28 29 20 63 61   sqliteFree() ca
2a360 6c 6c 73 20 2a 2f 0a 65 78 74 65 72 6e 20 69 6e  lls */.extern in
2a370 74 20 73 71 6c 69 74 65 33 5f 69 4d 61 6c 6c 6f  t sqlite3_iMallo
2a380 63 46 61 69 6c 3b 20 20 2f 2a 20 46 61 69 6c 20  cFail;  /* Fail 
2a390 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 61  sqliteMalloc() a
2a3a0 66 74 65 72 20 74 68 69 73 20 6d 61 6e 79 20 63  fter this many c
2a3b0 61 6c 6c 73 20 2a 2f 0a 65 78 74 65 72 6e 20 69  alls */.extern i
2a3c0 6e 74 20 73 71 6c 69 74 65 33 5f 69 4d 61 6c 6c  nt sqlite3_iMall
2a3d0 6f 63 52 65 73 65 74 3b 20 2f 2a 20 53 65 74 20  ocReset; /* Set 
2a3e0 69 4d 61 6c 6c 6f 63 46 61 69 6c 20 74 6f 20 74  iMallocFail to t
2a3f0 68 69 73 20 77 68 65 6e 20 69 74 20 72 65 61 63  his when it reac
2a400 68 65 73 20 30 20 2a 2f 0a 0a 65 78 74 65 72 6e  hes 0 */..extern
2a410 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 70   void *sqlite3_p
2a420 46 69 72 73 74 3b 20 20 20 20 20 20 20 20 20 2f  First;         /
2a430 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6c 69 6e  * Pointer to lin
2a440 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 6f  ked list of allo
2a450 63 61 74 69 6f 6e 73 20 2a 2f 0a 65 78 74 65 72  cations */.exter
2a460 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6e 4d  n int sqlite3_nM
2a470 61 78 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20  axAlloc;        
2a480 2f 2a 20 48 69 67 68 20 77 61 74 65 72 20 6d 61  /* High water ma
2a490 72 6b 20 6f 66 20 54 68 72 65 61 64 44 61 74 61  rk of ThreadData
2a4a0 2e 6e 41 6c 6c 6f 63 20 2a 2f 0a 65 78 74 65 72  .nAlloc */.exter
2a4b0 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 61  n int sqlite3_ma
2a4c0 6c 6c 6f 63 44 69 73 61 6c 6c 6f 77 65 64 3b 20  llocDisallowed; 
2a4d0 2f 2a 20 61 73 73 65 72 74 28 29 20 69 6e 20 73  /* assert() in s
2a4e0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20 69  qlite3Malloc() i
2a4f0 66 20 73 65 74 20 2a 2f 0a 65 78 74 65 72 6e 20  f set */.extern 
2a500 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 73 46 61  int sqlite3_isFa
2a510 69 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  il;           /*
2a520 20 54 72 75 65 20 69 66 20 61 6c 6c 20 6d 61 6c   True if all mal
2a530 6c 6f 63 20 63 61 6c 6c 73 20 73 68 6f 75 6c 64  loc calls should
2a540 20 66 61 69 6c 20 2a 2f 0a 65 78 74 65 72 6e 20   fail */.extern 
2a550 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
2a560 74 65 33 5f 7a 46 69 6c 65 3b 20 20 20 20 2f 2a  te3_zFile;    /*
2a570 20 46 69 6c 65 6e 61 6d 65 20 74 6f 20 61 73 73   Filename to ass
2a580 6f 63 69 61 74 65 20 64 65 62 75 67 20 69 6e 66  ociate debug inf
2a590 6f 20 77 69 74 68 20 2a 2f 0a 65 78 74 65 72 6e  o with */.extern
2a5a0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 4c 69   int sqlite3_iLi
2a5b0 6e 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ne;            /
2a5c0 2a 20 4c 69 6e 65 20 6e 75 6d 62 65 72 20 66 6f  * Line number fo
2a5d0 72 20 64 65 62 75 67 20 69 6e 66 6f 20 2a 2f 0a  r debug info */.
2a5e0 0a 23 64 65 66 69 6e 65 20 45 4e 54 45 52 5f 4d  .#define ENTER_M
2a5f0 41 4c 4c 4f 43 20 28 73 71 6c 69 74 65 33 5f 7a  ALLOC (sqlite3_z
2a600 46 69 6c 65 20 3d 20 5f 5f 46 49 4c 45 5f 5f 2c  File = __FILE__,
2a610 20 73 71 6c 69 74 65 33 5f 69 4c 69 6e 65 20 3d   sqlite3_iLine =
2a620 20 5f 5f 4c 49 4e 45 5f 5f 29 0a 23 64 65 66 69   __LINE__).#defi
2a630 6e 65 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28  ne sqliteMalloc(
2a640 78 29 20 20 20 20 20 20 20 20 20 20 28 45 4e 54  x)          (ENT
2a650 45 52 5f 4d 41 4c 4c 4f 43 2c 20 73 71 6c 69 74  ER_MALLOC, sqlit
2a660 65 33 4d 61 6c 6c 6f 63 28 78 2c 31 29 29 0a 23  e3Malloc(x,1)).#
2a670 64 65 66 69 6e 65 20 73 71 6c 69 74 65 4d 61 6c  define sqliteMal
2a680 6c 6f 63 52 61 77 28 78 29 20 20 20 20 20 20 20  locRaw(x)       
2a690 28 45 4e 54 45 52 5f 4d 41 4c 4c 4f 43 2c 20 73  (ENTER_MALLOC, s
2a6a0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 52 61 77 28  qlite3MallocRaw(
2a6b0 78 2c 31 29 29 0a 23 64 65 66 69 6e 65 20 73 71  x,1)).#define sq
2a6c0 6c 69 74 65 52 65 61 6c 6c 6f 63 28 78 2c 79 29  liteRealloc(x,y)
2a6d0 20 20 20 20 20 20 20 28 45 4e 54 45 52 5f 4d 41         (ENTER_MA
2a6e0 4c 4c 4f 43 2c 20 73 71 6c 69 74 65 33 52 65 61  LLOC, sqlite3Rea
2a6f0 6c 6c 6f 63 28 78 2c 79 29 29 0a 23 64 65 66 69  lloc(x,y)).#defi
2a700 6e 65 20 73 71 6c 69 74 65 53 74 72 44 75 70 28  ne sqliteStrDup(
2a710 78 29 20 20 20 20 20 20 20 20 20 20 28 45 4e 54  x)          (ENT
2a720 45 52 5f 4d 41 4c 4c 4f 43 2c 20 73 71 6c 69 74  ER_MALLOC, sqlit
2a730 65 33 53 74 72 44 75 70 28 78 29 29 0a 23 64 65  e3StrDup(x)).#de
2a740 66 69 6e 65 20 73 71 6c 69 74 65 53 74 72 4e 44  fine sqliteStrND
2a750 75 70 28 78 2c 79 29 20 20 20 20 20 20 20 28 45  up(x,y)       (E
2a760 4e 54 45 52 5f 4d 41 4c 4c 4f 43 2c 20 73 71 6c  NTER_MALLOC, sql
2a770 69 74 65 33 53 74 72 4e 44 75 70 28 78 2c 79 29  ite3StrNDup(x,y)
2a780 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  ).#define sqlite
2a790 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 78 2c  ReallocOrFree(x,
2a7a0 79 29 20 28 45 4e 54 45 52 5f 4d 41 4c 4c 4f 43  y) (ENTER_MALLOC
2a7b0 2c 20 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63  , sqlite3Realloc
2a7c0 4f 72 46 72 65 65 28 78 2c 79 29 29 0a 0a 23 65  OrFree(x,y))..#e
2a7d0 6c 73 65 0a 0a 23 64 65 66 69 6e 65 20 45 4e 54  lse..#define ENT
2a7e0 45 52 5f 4d 41 4c 4c 4f 43 20 30 0a 23 64 65 66  ER_MALLOC 0.#def
2a7f0 69 6e 65 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63  ine sqliteMalloc
2a800 28 78 29 20 20 20 20 20 20 20 20 20 20 73 71 6c  (x)          sql
2a810 69 74 65 33 4d 61 6c 6c 6f 63 28 78 2c 31 29 0a  ite3Malloc(x,1).
2a820 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 4d 61  #define sqliteMa
2a830 6c 6c 6f 63 52 61 77 28 78 29 20 20 20 20 20 20  llocRaw(x)      
2a840 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 52 61   sqlite3MallocRa
2a850 77 28 78 2c 31 29 0a 23 64 65 66 69 6e 65 20 73  w(x,1).#define s
2a860 71 6c 69 74 65 52 65 61 6c 6c 6f 63 28 78 2c 79  qliteRealloc(x,y
2a870 29 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  )       sqlite3R
2a880 65 61 6c 6c 6f 63 28 78 2c 79 29 0a 23 64 65 66  ealloc(x,y).#def
2a890 69 6e 65 20 73 71 6c 69 74 65 53 74 72 44 75 70  ine sqliteStrDup
2a8a0 28 78 29 20 20 20 20 20 20 20 20 20 20 73 71 6c  (x)          sql
2a8b0 69 74 65 33 53 74 72 44 75 70 28 78 29 0a 23 64  ite3StrDup(x).#d
2a8c0 65 66 69 6e 65 20 73 71 6c 69 74 65 53 74 72 4e  efine sqliteStrN
2a8d0 44 75 70 28 78 2c 79 29 20 20 20 20 20 20 20 73  Dup(x,y)       s
2a8e0 71 6c 69 74 65 33 53 74 72 4e 44 75 70 28 78 2c  qlite3StrNDup(x,
2a8f0 79 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  y).#define sqlit
2a900 65 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 78  eReallocOrFree(x
2a910 2c 79 29 20 73 71 6c 69 74 65 33 52 65 61 6c 6c  ,y) sqlite3Reall
2a920 6f 63 4f 72 46 72 65 65 28 78 2c 79 29 0a 0a 23  ocOrFree(x,y)..#
2a930 65 6e 64 69 66 0a 0a 2f 2a 20 56 61 72 69 61 62  endif../* Variab
2a940 6c 65 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  le sqlite3_mallo
2a950 63 48 61 73 46 61 69 6c 65 64 20 69 73 20 73 65  cHasFailed is se
2a960 74 20 74 6f 20 74 72 75 65 20 61 66 74 65 72 20  t to true after 
2a970 61 20 6d 61 6c 6c 6f 63 28 29 20 0a 2a 2a 20 66  a malloc() .** f
2a980 61 69 6c 75 72 65 20 6f 63 63 75 72 73 2e 20 0a  ailure occurs. .
2a990 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
2a9a0 33 4d 61 6c 6c 6f 63 46 61 69 6c 65 64 28 29 20  3MallocFailed() 
2a9b0 6d 61 63 72 6f 20 72 65 74 75 72 6e 73 20 74 72  macro returns tr
2a9c0 75 65 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 68  ue if a malloc h
2a9d0 61 73 20 66 61 69 6c 65 64 0a 2a 2a 20 69 6e 20  as failed.** in 
2a9e0 74 68 69 73 20 74 68 72 65 61 64 20 73 69 6e 63  this thread sinc
2a9f0 65 20 74 68 65 20 6c 61 73 74 20 63 61 6c 6c 20  e the last call 
2aa00 74 6f 20 73 71 6c 69 74 65 33 41 70 69 45 78 69  to sqlite3ApiExi
2aa10 74 28 29 2c 20 6f 72 20 66 61 6c 73 65 20 0a 2a  t(), or false .*
2aa20 2a 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a  * otherwise..*/.
2aa30 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74  extern int sqlit
2aa40 65 33 5f 6d 61 6c 6c 6f 63 48 61 73 46 61 69 6c  e3_mallocHasFail
2aa50 65 64 3b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ed;.#define sqli
2aa60 74 65 33 4d 61 6c 6c 6f 63 46 61 69 6c 65 64 28  te3MallocFailed(
2aa70 29 20 28 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  ) (sqlite3_mallo
2aa80 63 48 61 73 46 61 69 6c 65 64 20 26 26 20 73 71  cHasFailed && sq
2aa90 6c 69 74 65 33 4f 73 49 6e 4d 75 74 65 78 28 31  lite3OsInMutex(1
2aaa0 29 29 0a 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ))..#define sqli
2aab0 74 65 46 72 65 65 28 78 29 20 20 20 20 20 20 20  teFree(x)       
2aac0 20 20 20 73 71 6c 69 74 65 33 46 72 65 65 58 28     sqlite3FreeX(
2aad0 78 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  x).#define sqlit
2aae0 65 41 6c 6c 6f 63 53 69 7a 65 28 78 29 20 20 20  eAllocSize(x)   
2aaf0 20 20 73 71 6c 69 74 65 33 41 6c 6c 6f 63 53 69    sqlite3AllocSi
2ab00 7a 65 28 78 29 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  ze(x)../*.** An 
2ab10 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73  instance of this
2ab20 20 73 74 72 75 63 74 75 72 65 20 6d 69 67 68 74   structure might
2ab30 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f   be allocated to
2ab40 20 73 74 6f 72 65 20 69 6e 66 6f 72 6d 61 74 69   store informati
2ab50 6f 6e 0a 2a 2a 20 73 70 65 63 69 66 69 63 20 74  on.** specific t
2ab60 6f 20 61 20 73 69 6e 67 6c 65 20 74 68 72 65 61  o a single threa
2ab70 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 54 68 72  d..*/.struct Thr
2ab80 65 61 64 44 61 74 61 20 7b 0a 20 20 69 6e 74 20  eadData {.  int 
2ab90 64 75 6d 6d 79 3b 20 20 20 20 20 20 20 20 20 20  dummy;          
2aba0 20 20 20 20 20 2f 2a 20 53 6f 20 74 68 61 74 20       /* So that 
2abb0 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69  this structure i
2abc0 73 20 6e 65 76 65 72 20 65 6d 70 74 79 20 2a 2f  s never empty */
2abd0 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
2abe0 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41  ENABLE_MEMORY_MA
2abf0 4e 41 47 45 4d 45 4e 54 0a 20 20 69 6e 74 20 6e  NAGEMENT.  int n
2ac00 53 6f 66 74 48 65 61 70 4c 69 6d 69 74 3b 20 20  SoftHeapLimit;  
2ac10 20 20 20 20 2f 2a 20 53 75 67 67 65 73 74 65 64      /* Suggested
2ac20 20 6d 61 78 20 6d 65 6d 20 61 6c 6c 6f 63 61 74   max mem allocat
2ac30 69 6f 6e 2e 20 20 4e 6f 20 6c 69 6d 69 74 20 69  ion.  No limit i
2ac40 66 20 3c 30 20 2a 2f 0a 20 20 69 6e 74 20 6e 41  f <0 */.  int nA
2ac50 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 20 20  lloc;           
2ac60 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
2ac70 62 79 74 65 73 20 63 75 72 72 65 6e 74 6c 79 20  bytes currently 
2ac80 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 50  allocated */.  P
2ac90 61 67 65 72 20 2a 70 50 61 67 65 72 3b 20 20 20  ager *pPager;   
2aca0 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 6e 6b 65          /* Linke
2acb0 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 70 61  d list of all pa
2acc0 67 65 72 73 20 69 6e 20 74 68 69 73 20 74 68 72  gers in this thr
2acd0 65 61 64 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 23  ead */.#endif..#
2ace0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2acf0 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a  IT_SHARED_CACHE.
2ad00 20 20 75 38 20 75 73 65 53 68 61 72 65 64 44 61    u8 useSharedDa
2ad10 74 61 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72  ta;        /* Tr
2ad20 75 65 20 69 66 20 73 68 61 72 65 64 20 70 61 67  ue if shared pag
2ad30 65 72 73 20 61 6e 64 20 73 63 68 65 6d 61 73 20  ers and schemas 
2ad40 61 72 65 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20  are enabled */. 
2ad50 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 72 65   BtShared *pBtre
2ad60 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 6e  e;        /* Lin
2ad70 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20  ked list of all 
2ad80 63 75 72 72 65 6e 74 6c 79 20 6f 70 65 6e 20 42  currently open B
2ad90 54 72 65 65 73 20 2a 2f 0a 23 65 6e 64 69 66 0a  Trees */.#endif.
2ada0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 20 6f  };../*.** Name o
2adb0 66 20 74 68 65 20 6d 61 73 74 65 72 20 64 61 74  f the master dat
2adc0 61 62 61 73 65 20 74 61 62 6c 65 2e 20 20 54 68  abase table.  Th
2add0 65 20 6d 61 73 74 65 72 20 64 61 74 61 62 61 73  e master databas
2ade0 65 20 74 61 62 6c 65 0a 2a 2a 20 69 73 20 61 20  e table.** is a 
2adf0 73 70 65 63 69 61 6c 20 74 61 62 6c 65 20 74 68  special table th
2ae00 61 74 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d  at holds the nam
2ae10 65 73 20 61 6e 64 20 61 74 74 72 69 62 75 74 65  es and attribute
2ae20 73 20 6f 66 20 61 6c 6c 0a 2a 2a 20 75 73 65 72  s of all.** user
2ae30 20 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69   tables and indi
2ae40 63 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ces..*/.#define 
2ae50 4d 41 53 54 45 52 5f 4e 41 4d 45 20 20 20 20 20  MASTER_NAME     
2ae60 20 20 22 73 71 6c 69 74 65 5f 6d 61 73 74 65 72    "sqlite_master
2ae70 22 0a 23 64 65 66 69 6e 65 20 54 45 4d 50 5f 4d  ".#define TEMP_M
2ae80 41 53 54 45 52 5f 4e 41 4d 45 20 20 22 73 71 6c  ASTER_NAME  "sql
2ae90 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 22  ite_temp_master"
2aea0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74  ../*.** The root
2aeb0 2d 70 61 67 65 20 6f 66 20 74 68 65 20 6d 61 73  -page of the mas
2aec0 74 65 72 20 64 61 74 61 62 61 73 65 20 74 61 62  ter database tab
2aed0 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  le..*/.#define M
2aee0 41 53 54 45 52 5f 52 4f 4f 54 20 20 20 20 20 20  ASTER_ROOT      
2aef0 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 61   1../*.** The na
2af00 6d 65 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61  me of the schema
2af10 20 74 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69   table..*/.#defi
2af20 6e 65 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28  ne SCHEMA_TABLE(
2af30 78 29 20 20 28 28 21 4f 4d 49 54 5f 54 45 4d 50  x)  ((!OMIT_TEMP
2af40 44 42 29 26 26 28 78 3d 3d 31 29 3f 54 45 4d 50  DB)&&(x==1)?TEMP
2af50 5f 4d 41 53 54 45 52 5f 4e 41 4d 45 3a 4d 41 53  _MASTER_NAME:MAS
2af60 54 45 52 5f 4e 41 4d 45 29 0a 0a 2f 2a 0a 2a 2a  TER_NAME)../*.**
2af70 20 41 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 6d   A convenience m
2af80 61 63 72 6f 20 74 68 61 74 20 72 65 74 75 72 6e  acro that return
2af90 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
2afa0 65 6c 65 6d 65 6e 74 73 20 69 6e 0a 2a 2a 20 61  elements in.** a
2afb0 6e 20 61 72 72 61 79 2e 0a 2a 2f 0a 23 64 65 66  n array..*/.#def
2afc0 69 6e 65 20 41 72 72 61 79 53 69 7a 65 28 58 29  ine ArraySize(X)
2afd0 20 20 20 20 28 73 69 7a 65 6f 66 28 58 29 2f 73      (sizeof(X)/s
2afe0 69 7a 65 6f 66 28 58 5b 30 5d 29 29 0a 0a 2f 2a  izeof(X[0]))../*
2aff0 0a 2a 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65  .** Forward refe
2b000 72 65 6e 63 65 73 20 74 6f 20 73 74 72 75 63 74  rences to struct
2b010 75 72 65 73 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ures.*/.typedef 
2b020 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 20 41  struct AggInfo A
2b030 67 67 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20  ggInfo;.typedef 
2b040 73 74 72 75 63 74 20 41 75 74 68 43 6f 6e 74 65  struct AuthConte
2b050 78 74 20 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a  xt AuthContext;.
2b060 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 43  typedef struct C
2b070 6f 6c 6c 53 65 71 20 43 6f 6c 6c 53 65 71 3b 0a  ollSeq CollSeq;.
2b080 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 43  typedef struct C
2b090 6f 6c 75 6d 6e 20 43 6f 6c 75 6d 6e 3b 0a 74 79  olumn Column;.ty
2b0a0 70 65 64 65 66 20 73 74 72 75 63 74 20 44 62 20  pedef struct Db 
2b0b0 44 62 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  Db;.typedef stru
2b0c0 63 74 20 53 63 68 65 6d 61 20 53 63 68 65 6d 61  ct Schema Schema
2b0d0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
2b0e0 20 45 78 70 72 20 45 78 70 72 3b 0a 74 79 70 65   Expr Expr;.type
2b0f0 64 65 66 20 73 74 72 75 63 74 20 45 78 70 72 4c  def struct ExprL
2b100 69 73 74 20 45 78 70 72 4c 69 73 74 3b 0a 74 79  ist ExprList;.ty
2b110 70 65 64 65 66 20 73 74 72 75 63 74 20 46 4b 65  pedef struct FKe
2b120 79 20 46 4b 65 79 3b 0a 74 79 70 65 64 65 66 20  y FKey;.typedef 
2b130 73 74 72 75 63 74 20 46 75 6e 63 44 65 66 20 46  struct FuncDef F
2b140 75 6e 63 44 65 66 3b 0a 74 79 70 65 64 65 66 20  uncDef;.typedef 
2b150 73 74 72 75 63 74 20 49 64 4c 69 73 74 20 49 64  struct IdList Id
2b160 4c 69 73 74 3b 0a 74 79 70 65 64 65 66 20 73 74  List;.typedef st
2b170 72 75 63 74 20 49 6e 64 65 78 20 49 6e 64 65 78  ruct Index Index
2b180 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
2b190 20 4b 65 79 43 6c 61 73 73 20 4b 65 79 43 6c 61   KeyClass KeyCla
2b1a0 73 73 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  ss;.typedef stru
2b1b0 63 74 20 4b 65 79 49 6e 66 6f 20 4b 65 79 49 6e  ct KeyInfo KeyIn
2b1c0 66 6f 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  fo;.typedef stru
2b1d0 63 74 20 4d 6f 64 75 6c 65 20 4d 6f 64 75 6c 65  ct Module Module
2b1e0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
2b1f0 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 4e 61 6d   NameContext Nam
2b200 65 43 6f 6e 74 65 78 74 3b 0a 74 79 70 65 64 65  eContext;.typede
2b210 66 20 73 74 72 75 63 74 20 50 61 72 73 65 20 50  f struct Parse P
2b220 61 72 73 65 3b 0a 74 79 70 65 64 65 66 20 73 74  arse;.typedef st
2b230 72 75 63 74 20 53 65 6c 65 63 74 20 53 65 6c 65  ruct Select Sele
2b240 63 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  ct;.typedef stru
2b250 63 74 20 53 72 63 4c 69 73 74 20 53 72 63 4c 69  ct SrcList SrcLi
2b260 73 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  st;.typedef stru
2b270 63 74 20 54 68 72 65 61 64 44 61 74 61 20 54 68  ct ThreadData Th
2b280 72 65 61 64 44 61 74 61 3b 0a 74 79 70 65 64 65  readData;.typede
2b290 66 20 73 74 72 75 63 74 20 54 61 62 6c 65 20 54  f struct Table T
2b2a0 61 62 6c 65 3b 0a 74 79 70 65 64 65 66 20 73 74  able;.typedef st
2b2b0 72 75 63 74 20 54 61 62 6c 65 4c 6f 63 6b 20 54  ruct TableLock T
2b2c0 61 62 6c 65 4c 6f 63 6b 3b 0a 74 79 70 65 64 65  ableLock;.typede
2b2d0 66 20 73 74 72 75 63 74 20 54 6f 6b 65 6e 20 54  f struct Token T
2b2e0 6f 6b 65 6e 3b 0a 74 79 70 65 64 65 66 20 73 74  oken;.typedef st
2b2f0 72 75 63 74 20 54 72 69 67 67 65 72 53 74 61 63  ruct TriggerStac
2b300 6b 20 54 72 69 67 67 65 72 53 74 61 63 6b 3b 0a  k TriggerStack;.
2b310 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54  typedef struct T
2b320 72 69 67 67 65 72 53 74 65 70 20 54 72 69 67 67  riggerStep Trigg
2b330 65 72 53 74 65 70 3b 0a 74 79 70 65 64 65 66 20  erStep;.typedef 
2b340 73 74 72 75 63 74 20 54 72 69 67 67 65 72 20 54  struct Trigger T
2b350 72 69 67 67 65 72 3b 0a 74 79 70 65 64 65 66 20  rigger;.typedef 
2b360 73 74 72 75 63 74 20 57 68 65 72 65 49 6e 66 6f  struct WhereInfo
2b370 20 57 68 65 72 65 49 6e 66 6f 3b 0a 74 79 70 65   WhereInfo;.type
2b380 64 65 66 20 73 74 72 75 63 74 20 57 68 65 72 65  def struct Where
2b390 4c 65 76 65 6c 20 57 68 65 72 65 4c 65 76 65 6c  Level WhereLevel
2b3a0 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;../************
2b3b0 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 73 2e 68 20  ** Include os.h 
2b3c0 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
2b3d0 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a   sqliteInt.h ***
2b3e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b3f0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
2b400 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73  ** Begin file os
2b410 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
2b420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b440 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65  */./*.** 2001 Se
2b450 70 74 65 6d 62 65 72 20 31 36 0a 2a 2a 0a 2a 2a  ptember 16.**.**
2b460 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
2b470 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
2b480 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
2b490 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
2b4a0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
2b4b0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
2b4c0 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
2b4d0 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
2b4e0 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
2b4f0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
2b500 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
2b510 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
2b520 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
2b530 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
2b540 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
2b550 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
2b560 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
2b570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b5a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b5b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
2b5c0 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72  *.** This header
2b5d0 20 66 69 6c 65 20 28 74 6f 67 65 74 68 65 72 20   file (together 
2b5e0 77 69 74 68 20 69 73 20 63 6f 6d 70 61 6e 69 6f  with is companio
2b5f0 6e 20 43 20 73 6f 75 72 63 65 2d 63 6f 64 65 20  n C source-code 
2b600 66 69 6c 65 0a 2a 2a 20 22 6f 73 2e 63 22 29 20  file.** "os.c") 
2b610 61 74 74 65 6d 70 74 20 74 6f 20 61 62 73 74 72  attempt to abstr
2b620 61 63 74 20 74 68 65 20 75 6e 64 65 72 6c 79 69  act the underlyi
2b630 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73  ng operating sys
2b640 74 65 6d 20 73 6f 20 74 68 61 74 0a 2a 2a 20 74  tem so that.** t
2b650 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72  he SQLite librar
2b660 79 20 77 69 6c 6c 20 77 6f 72 6b 20 6f 6e 20 62  y will work on b
2b670 6f 74 68 20 50 4f 53 49 58 20 61 6e 64 20 77 69  oth POSIX and wi
2b680 6e 64 6f 77 73 20 73 79 73 74 65 6d 73 2e 0a 2a  ndows systems..*
2b690 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54  /.#ifndef _SQLIT
2b6a0 45 5f 4f 53 5f 48 5f 0a 23 64 65 66 69 6e 65 20  E_OS_H_.#define 
2b6b0 5f 53 51 4c 49 54 45 5f 4f 53 5f 48 5f 0a 0a 2f  _SQLITE_OS_H_../
2b6c0 2a 0a 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 20  *.** Figure out 
2b6d0 69 66 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e  if we are dealin
2b6e0 67 20 77 69 74 68 20 55 6e 69 78 2c 20 57 69 6e  g with Unix, Win
2b6f0 64 6f 77 73 2c 20 6f 72 20 73 6f 6d 65 20 6f 74  dows, or some ot
2b700 68 65 72 0a 2a 2a 20 6f 70 65 72 61 74 69 6e 67  her.** operating
2b710 20 73 79 73 74 65 6d 2e 0a 2a 2f 0a 23 69 66 20   system..*/.#if 
2b720 64 65 66 69 6e 65 64 28 4f 53 5f 4f 54 48 45 52  defined(OS_OTHER
2b730 29 0a 23 20 69 66 20 4f 53 5f 4f 54 48 45 52 3d  ).# if OS_OTHER=
2b740 3d 31 0a 23 20 20 20 75 6e 64 65 66 20 4f 53 5f  =1.#   undef OS_
2b750 55 4e 49 58 0a 23 20 20 20 64 65 66 69 6e 65 20  UNIX.#   define 
2b760 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 20 75 6e  OS_UNIX 0.#   un
2b770 64 65 66 20 4f 53 5f 57 49 4e 0a 23 20 20 20 64  def OS_WIN.#   d
2b780 65 66 69 6e 65 20 4f 53 5f 57 49 4e 20 30 0a 23  efine OS_WIN 0.#
2b790 20 20 20 75 6e 64 65 66 20 4f 53 5f 4f 53 32 0a     undef OS_OS2.
2b7a0 23 20 20 20 64 65 66 69 6e 65 20 4f 53 5f 4f 53  #   define OS_OS
2b7b0 32 20 30 0a 23 20 65 6c 73 65 0a 23 20 20 20 75  2 0.# else.#   u
2b7c0 6e 64 65 66 20 4f 53 5f 4f 54 48 45 52 0a 23 20  ndef OS_OTHER.# 
2b7d0 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 69 66  endif.#endif.#if
2b7e0 20 21 64 65 66 69 6e 65 64 28 4f 53 5f 55 4e 49   !defined(OS_UNI
2b7f0 58 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 4f  X) && !defined(O
2b800 53 5f 4f 54 48 45 52 29 0a 23 20 64 65 66 69 6e  S_OTHER).# defin
2b810 65 20 4f 53 5f 4f 54 48 45 52 20 30 0a 23 20 69  e OS_OTHER 0.# i
2b820 66 6e 64 65 66 20 4f 53 5f 57 49 4e 0a 23 20 20  fndef OS_WIN.#  
2b830 20 69 66 20 64 65 66 69 6e 65 64 28 5f 57 49 4e   if defined(_WIN
2b840 33 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 57  32) || defined(W
2b850 49 4e 33 32 29 20 7c 7c 20 64 65 66 69 6e 65 64  IN32) || defined
2b860 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 20 7c 7c 20  (__CYGWIN__) || 
2b870 64 65 66 69 6e 65 64 28 5f 5f 4d 49 4e 47 57 33  defined(__MINGW3
2b880 32 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  2__) || defined(
2b890 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a 23 20  __BORLANDC__).# 
2b8a0 20 20 20 20 64 65 66 69 6e 65 20 4f 53 5f 57 49      define OS_WI
2b8b0 4e 20 31 0a 23 20 20 20 20 20 64 65 66 69 6e 65  N 1.#     define
2b8c0 20 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 20 20   OS_UNIX 0.#    
2b8d0 20 64 65 66 69 6e 65 20 4f 53 5f 4f 53 32 20 30   define OS_OS2 0
2b8e0 0a 23 20 20 20 65 6c 69 66 20 64 65 66 69 6e 65  .#   elif define
2b8f0 64 28 5f 5f 45 4d 58 5f 5f 29 20 7c 7c 20 64 65  d(__EMX__) || de
2b900 66 69 6e 65 64 28 5f 4f 53 32 29 20 7c 7c 20 64  fined(_OS2) || d
2b910 65 66 69 6e 65 64 28 4f 53 32 29 20 7c 7c 20 64  efined(OS2) || d
2b920 65 66 69 6e 65 64 28 5f 4f 53 32 5f 29 20 7c 7c  efined(_OS2_) ||
2b930 20 64 65 66 69 6e 65 64 28 5f 5f 4f 53 32 5f 5f   defined(__OS2__
2b940 29 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 4f  ).#     define O
2b950 53 5f 57 49 4e 20 30 0a 23 20 20 20 20 20 64 65  S_WIN 0.#     de
2b960 66 69 6e 65 20 4f 53 5f 55 4e 49 58 20 30 0a 23  fine OS_UNIX 0.#
2b970 20 20 20 20 20 64 65 66 69 6e 65 20 4f 53 5f 4f       define OS_O
2b980 53 32 20 31 0a 23 20 20 20 65 6c 73 65 0a 23 20  S2 1.#   else.# 
2b990 20 20 20 20 64 65 66 69 6e 65 20 4f 53 5f 57 49      define OS_WI
2b9a0 4e 20 30 0a 23 20 20 20 20 20 64 65 66 69 6e 65  N 0.#     define
2b9b0 20 4f 53 5f 55 4e 49 58 20 31 0a 23 20 20 20 20   OS_UNIX 1.#    
2b9c0 20 64 65 66 69 6e 65 20 4f 53 5f 4f 53 32 20 30   define OS_OS2 0
2b9d0 0a 23 20 20 65 6e 64 69 66 0a 23 20 65 6c 73 65  .#  endif.# else
2b9e0 0a 23 20 20 64 65 66 69 6e 65 20 4f 53 5f 55 4e  .#  define OS_UN
2b9f0 49 58 20 30 0a 23 20 20 64 65 66 69 6e 65 20 4f  IX 0.#  define O
2ba00 53 5f 4f 53 32 20 30 0a 23 20 65 6e 64 69 66 0a  S_OS2 0.# endif.
2ba10 23 65 6c 73 65 0a 23 20 69 66 6e 64 65 66 20 4f  #else.# ifndef O
2ba20 53 5f 57 49 4e 0a 23 20 20 64 65 66 69 6e 65 20  S_WIN.#  define 
2ba30 4f 53 5f 57 49 4e 20 30 0a 23 20 65 6e 64 69 66  OS_WIN 0.# endif
2ba40 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
2ba50 44 65 66 69 6e 65 20 74 68 65 20 6d 61 78 69 6d  Define the maxim
2ba60 75 6d 20 73 69 7a 65 20 6f 66 20 61 20 74 65 6d  um size of a tem
2ba70 70 6f 72 61 72 79 20 66 69 6c 65 6e 61 6d 65 0a  porary filename.
2ba80 2a 2f 0a 23 69 66 20 4f 53 5f 57 49 4e 0a 23 20  */.#if OS_WIN.# 
2ba90 69 6e 63 6c 75 64 65 20 3c 77 69 6e 64 6f 77 73  include <windows
2baa0 2e 68 3e 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  .h>.# define SQL
2bab0 49 54 45 5f 54 45 4d 50 4e 41 4d 45 5f 53 49 5a  ITE_TEMPNAME_SIZ
2bac0 45 20 28 4d 41 58 5f 50 41 54 48 2b 35 30 29 0a  E (MAX_PATH+50).
2bad0 23 65 6c 69 66 20 4f 53 5f 4f 53 32 0a 23 20 69  #elif OS_OS2.# i
2bae0 66 20 28 5f 5f 47 4e 55 43 5f 5f 20 3e 20 33 20  f (__GNUC__ > 3 
2baf0 7c 7c 20 5f 5f 47 4e 55 43 5f 5f 20 3d 3d 20 33  || __GNUC__ == 3
2bb00 20 26 26 20 5f 5f 47 4e 55 43 5f 4d 49 4e 4f 52   && __GNUC_MINOR
2bb10 5f 5f 20 3e 3d 20 33 29 20 26 26 20 64 65 66 69  __ >= 3) && defi
2bb20 6e 65 64 28 4f 53 32 5f 48 49 47 48 5f 4d 45 4d  ned(OS2_HIGH_MEM
2bb30 4f 52 59 29 0a 23 20 20 69 6e 63 6c 75 64 65 20  ORY).#  include 
2bb40 3c 6f 73 32 73 61 66 65 2e 68 3e 20 2f 2a 20 68  <os2safe.h> /* h
2bb50 61 73 20 74 6f 20 62 65 20 69 6e 63 6c 75 64 65  as to be include
2bb60 64 20 62 65 66 6f 72 65 20 6f 73 32 2e 68 20 66  d before os2.h f
2bb70 6f 72 20 6c 69 6e 6b 69 6e 67 20 74 6f 20 77 6f  or linking to wo
2bb80 72 6b 20 2a 2f 0a 23 20 65 6e 64 69 66 0a 23 20  rk */.# endif.# 
2bb90 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 44  define INCL_DOSD
2bba0 41 54 45 54 49 4d 45 0a 23 20 64 65 66 69 6e 65  ATETIME.# define
2bbb0 20 49 4e 43 4c 5f 44 4f 53 46 49 4c 45 4d 47 52   INCL_DOSFILEMGR
2bbc0 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44  .# define INCL_D
2bbd0 4f 53 45 52 52 4f 52 53 0a 23 20 64 65 66 69 6e  OSERRORS.# defin
2bbe0 65 20 49 4e 43 4c 5f 44 4f 53 4d 49 53 43 0a 23  e INCL_DOSMISC.#
2bbf0 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53   define INCL_DOS
2bc00 50 52 4f 43 45 53 53 0a 23 20 64 65 66 69 6e 65  PROCESS.# define
2bc10 20 49 4e 43 4c 5f 44 4f 53 4d 4f 44 55 4c 45 4d   INCL_DOSMODULEM
2bc20 47 52 0a 23 20 69 6e 63 6c 75 64 65 20 3c 6f 73  GR.# include <os
2bc30 32 2e 68 3e 0a 23 20 64 65 66 69 6e 65 20 53 51  2.h>.# define SQ
2bc40 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45 5f 53 49  LITE_TEMPNAME_SI
2bc50 5a 45 20 28 43 43 48 4d 41 58 50 41 54 48 43 4f  ZE (CCHMAXPATHCO
2bc60 4d 50 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  MP).#else.# defi
2bc70 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41  ne SQLITE_TEMPNA
2bc80 4d 45 5f 53 49 5a 45 20 32 30 30 0a 23 65 6e 64  ME_SIZE 200.#end
2bc90 69 66 0a 0a 2f 2a 20 49 66 20 74 68 65 20 53 45  if../* If the SE
2bca0 54 5f 46 55 4c 4c 53 59 4e 43 20 6d 61 63 72 6f  T_FULLSYNC macro
2bcb0 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 20   is not defined 
2bcc0 61 62 6f 76 65 2c 20 74 68 65 6e 20 6d 61 6b 65  above, then make
2bcd0 20 69 74 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 0a 2a   it.** a no-op.*
2bce0 2f 0a 23 69 66 6e 64 65 66 20 53 45 54 5f 46 55  /.#ifndef SET_FU
2bcf0 4c 4c 53 59 4e 43 0a 23 20 64 65 66 69 6e 65 20  LLSYNC.# define 
2bd00 53 45 54 5f 46 55 4c 4c 53 59 4e 43 28 78 2c 79  SET_FULLSYNC(x,y
2bd10 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ).#endif../*.** 
2bd20 54 68 65 20 64 65 66 61 75 6c 74 20 73 69 7a 65  The default size
2bd30 20 6f 66 20 61 20 64 69 73 6b 20 73 65 63 74 6f   of a disk secto
2bd40 72 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  r.*/.#ifndef SQL
2bd50 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54  ITE_DEFAULT_SECT
2bd60 4f 52 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65  OR_SIZE.# define
2bd70 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
2bd80 53 45 43 54 4f 52 5f 53 49 5a 45 20 35 31 32 0a  SECTOR_SIZE 512.
2bd90 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 65  #endif../*.** Te
2bda0 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 61 72  mporary files ar
2bdb0 65 20 6e 61 6d 65 64 20 73 74 61 72 74 69 6e 67  e named starting
2bdc0 20 77 69 74 68 20 74 68 69 73 20 70 72 65 66 69   with this prefi
2bdd0 78 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 31 36  x followed by 16
2bde0 20 72 61 6e 64 6f 6d 0a 2a 2a 20 61 6c 70 68 61   random.** alpha
2bdf0 6e 75 6d 65 72 69 63 20 63 68 61 72 61 63 74 65  numeric characte
2be00 72 73 2c 20 61 6e 64 20 6e 6f 20 66 69 6c 65 20  rs, and no file 
2be10 65 78 74 65 6e 73 69 6f 6e 2e 20 54 68 65 79 20  extension. They 
2be20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68  are stored in th
2be30 65 0a 2a 2a 20 4f 53 27 73 20 73 74 61 6e 64 61  e.** OS's standa
2be40 72 64 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c  rd temporary fil
2be50 65 20 64 69 72 65 63 74 6f 72 79 2c 20 61 6e 64  e directory, and
2be60 20 61 72 65 20 64 65 6c 65 74 65 64 20 70 72 69   are deleted pri
2be70 6f 72 20 74 6f 20 65 78 69 74 2e 0a 2a 2a 20 49  or to exit..** I
2be80 66 20 73 71 6c 69 74 65 20 69 73 20 62 65 69 6e  f sqlite is bein
2be90 67 20 65 6d 62 65 64 64 65 64 20 69 6e 20 61 6e  g embedded in an
2bea0 6f 74 68 65 72 20 70 72 6f 67 72 61 6d 2c 20 79  other program, y
2beb0 6f 75 20 6d 61 79 20 77 69 73 68 20 74 6f 20 63  ou may wish to c
2bec0 68 61 6e 67 65 20 74 68 65 0a 2a 2a 20 70 72 65  hange the.** pre
2bed0 66 69 78 20 74 6f 20 72 65 66 6c 65 63 74 20 79  fix to reflect y
2bee0 6f 75 72 20 70 72 6f 67 72 61 6d 27 73 20 6e 61  our program's na
2bef0 6d 65 2c 20 73 6f 20 74 68 61 74 20 69 66 20 79  me, so that if y
2bf00 6f 75 72 20 70 72 6f 67 72 61 6d 20 65 78 69 74  our program exit
2bf10 73 0a 2a 2a 20 70 72 65 6d 61 74 75 72 65 6c 79  s.** prematurely
2bf20 2c 20 6f 6c 64 20 74 65 6d 70 6f 72 61 72 79 20  , old temporary 
2bf30 66 69 6c 65 73 20 63 61 6e 20 62 65 20 65 61 73  files can be eas
2bf40 69 6c 79 20 69 64 65 6e 74 69 66 69 65 64 2e 20  ily identified. 
2bf50 54 68 69 73 20 63 61 6e 20 62 65 20 64 6f 6e 65  This can be done
2bf60 0a 2a 2a 20 75 73 69 6e 67 20 2d 44 54 45 4d 50  .** using -DTEMP
2bf70 5f 46 49 4c 45 5f 50 52 45 46 49 58 3d 6d 79 70  _FILE_PREFIX=myp
2bf80 72 65 66 69 78 5f 20 6f 6e 20 74 68 65 20 63 6f  refix_ on the co
2bf90 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 20 6c  mpiler command l
2bfa0 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 36 2d  ine..**.** 2006-
2bfb0 31 30 2d 33 31 3a 20 20 54 68 65 20 64 65 66 61  10-31:  The defa
2bfc0 75 6c 74 20 70 72 65 66 69 78 20 75 73 65 64 20  ult prefix used 
2bfd0 74 6f 20 62 65 20 22 73 71 6c 69 74 65 5f 22 2e  to be "sqlite_".
2bfe0 20 20 42 75 74 20 74 68 65 6e 0a 2a 2a 20 4d 63    But then.** Mc
2bff0 61 66 65 65 20 73 74 61 72 74 65 64 20 75 73 69  afee started usi
2c000 6e 67 20 53 51 4c 69 74 65 20 69 6e 20 74 68 65  ng SQLite in the
2c010 69 72 20 61 6e 74 69 2d 76 69 72 75 73 20 70 72  ir anti-virus pr
2c020 6f 64 75 63 74 20 61 6e 64 20 69 74 0a 2a 2a 20  oduct and it.** 
2c030 73 74 61 72 74 65 64 20 70 75 74 74 69 6e 67 20  started putting 
2c040 66 69 6c 65 73 20 77 69 74 68 20 74 68 65 20 22  files with the "
2c050 73 71 6c 69 74 65 22 20 6e 61 6d 65 20 69 6e 20  sqlite" name in 
2c060 74 68 65 20 63 3a 2f 74 65 6d 70 20 66 6f 6c 64  the c:/temp fold
2c070 65 72 2e 0a 2a 2a 20 54 68 69 73 20 61 6e 6e 6f  er..** This anno
2c080 79 65 64 20 6d 61 6e 79 20 77 69 6e 64 6f 77 73  yed many windows
2c090 20 75 73 65 72 73 2e 20 20 54 68 6f 73 65 20 75   users.  Those u
2c0a0 73 65 72 73 20 77 6f 75 6c 64 20 74 68 65 6e 20  sers would then 
2c0b0 64 6f 20 61 20 0a 2a 2a 20 47 6f 6f 67 6c 65 20  do a .** Google 
2c0c0 73 65 61 72 63 68 20 66 6f 72 20 22 73 71 6c 69  search for "sqli
2c0d0 74 65 22 2c 20 66 69 6e 64 20 74 68 65 20 74 65  te", find the te
2c0e0 6c 65 70 68 6f 6e 65 20 6e 75 6d 62 65 72 73 20  lephone numbers 
2c0f0 6f 66 20 74 68 65 0a 2a 2a 20 64 65 76 65 6c 6f  of the.** develo
2c100 70 65 72 73 20 61 6e 64 20 63 61 6c 6c 20 74 6f  pers and call to
2c110 20 77 61 6b 65 20 74 68 65 6d 20 75 70 20 61 74   wake them up at
2c120 20 6e 69 67 68 74 20 61 6e 64 20 63 6f 6d 70 6c   night and compl
2c130 61 69 6e 2e 0a 2a 2a 20 46 6f 72 20 74 68 69 73  ain..** For this
2c140 20 72 65 61 73 6f 6e 2c 20 74 68 65 20 64 65 66   reason, the def
2c150 61 75 6c 74 20 6e 61 6d 65 20 70 72 65 66 69 78  ault name prefix
2c160 20 69 73 20 63 68 61 6e 67 65 64 20 74 6f 20 62   is changed to b
2c170 65 20 22 73 71 6c 69 74 65 22 20 0a 2a 2a 20 73  e "sqlite" .** s
2c180 70 65 6c 6c 65 64 20 62 61 63 6b 77 61 72 64 73  pelled backwards
2c190 2e 20 20 53 6f 20 74 68 65 20 74 65 6d 70 20 66  .  So the temp f
2c1a0 69 6c 65 73 20 61 72 65 20 73 74 69 6c 6c 20 69  iles are still i
2c1b0 64 65 6e 74 69 66 69 65 64 2c 20 62 75 74 0a 2a  dentified, but.*
2c1c0 2a 20 61 6e 79 62 6f 64 79 20 73 6d 61 72 74 20  * anybody smart 
2c1d0 65 6e 6f 75 67 68 20 74 6f 20 66 69 67 75 72 65  enough to figure
2c1e0 20 6f 75 74 20 74 68 65 20 63 6f 64 65 20 69 73   out the code is
2c1f0 20 61 6c 73 6f 20 6c 69 6b 65 6c 79 20 73 6d 61   also likely sma
2c200 72 74 0a 2a 2a 20 65 6e 6f 75 67 68 20 74 6f 20  rt.** enough to 
2c210 6b 6e 6f 77 20 74 68 61 74 20 63 61 6c 6c 69 6e  know that callin
2c220 67 20 74 68 65 20 64 65 76 65 6c 6f 70 65 72 20  g the developer 
2c230 77 69 6c 6c 20 6e 6f 74 20 68 65 6c 70 20 67 65  will not help ge
2c240 74 20 72 69 64 0a 2a 2a 20 6f 66 20 74 68 65 20  t rid.** of the 
2c250 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  file..*/.#ifndef
2c260 20 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49   TEMP_FILE_PREFI
2c270 58 0a 23 20 64 65 66 69 6e 65 20 54 45 4d 50 5f  X.# define TEMP_
2c280 46 49 4c 45 5f 50 52 45 46 49 58 20 22 65 74 69  FILE_PREFIX "eti
2c290 6c 71 73 5f 22 0a 23 65 6e 64 69 66 0a 0a 2f 2a  lqs_".#endif../*
2c2a0 0a 2a 2a 20 44 65 66 69 6e 65 20 74 68 65 20 69  .** Define the i
2c2b0 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20 55 6e  nterfaces for Un
2c2c0 69 78 2c 20 57 69 6e 64 6f 77 73 2c 20 61 6e 64  ix, Windows, and
2c2d0 20 4f 53 2f 32 2e 0a 2a 2f 0a 23 69 66 20 4f 53   OS/2..*/.#if OS
2c2e0 5f 55 4e 49 58 0a 23 64 65 66 69 6e 65 20 73 71  _UNIX.#define sq
2c2f0 6c 69 74 65 33 4f 73 4f 70 65 6e 52 65 61 64 57  lite3OsOpenReadW
2c300 72 69 74 65 20 20 20 20 20 20 73 71 6c 69 74 65  rite      sqlite
2c310 33 55 6e 69 78 4f 70 65 6e 52 65 61 64 57 72 69  3UnixOpenReadWri
2c320 74 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  te.#define sqlit
2c330 65 33 4f 73 4f 70 65 6e 45 78 63 6c 75 73 69 76  e3OsOpenExclusiv
2c340 65 20 20 20 20 20 20 73 71 6c 69 74 65 33 55 6e  e      sqlite3Un
2c350 69 78 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 0a  ixOpenExclusive.
2c360 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4f  #define sqlite3O
2c370 73 4f 70 65 6e 52 65 61 64 4f 6e 6c 79 20 20 20  sOpenReadOnly   
2c380 20 20 20 20 73 71 6c 69 74 65 33 55 6e 69 78 4f      sqlite3UnixO
2c390 70 65 6e 52 65 61 64 4f 6e 6c 79 0a 23 64 65 66  penReadOnly.#def
2c3a0 69 6e 65 20 73 71 6c 69 74 65 33 4f 73 44 65 6c  ine sqlite3OsDel
2c3b0 65 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ete             
2c3c0 73 71 6c 69 74 65 33 55 6e 69 78 44 65 6c 65 74  sqlite3UnixDelet
2c3d0 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
2c3e0 33 4f 73 46 69 6c 65 45 78 69 73 74 73 20 20 20  3OsFileExists   
2c3f0 20 20 20 20 20 20 73 71 6c 69 74 65 33 55 6e 69        sqlite3Uni
2c400 78 46 69 6c 65 45 78 69 73 74 73 0a 23 64 65 66  xFileExists.#def
2c410 69 6e 65 20 73 71 6c 69 74 65 33 4f 73 46 75 6c  ine sqlite3OsFul
2c420 6c 50 61 74 68 6e 61 6d 65 20 20 20 20 20 20 20  lPathname       
2c430 73 71 6c 69 74 65 33 55 6e 69 78 46 75 6c 6c 50  sqlite3UnixFullP
2c440 61 74 68 6e 61 6d 65 0a 23 64 65 66 69 6e 65 20  athname.#define 
2c450 73 71 6c 69 74 65 33 4f 73 49 73 44 69 72 57 72  sqlite3OsIsDirWr
2c460 69 74 61 62 6c 65 20 20 20 20 20 20 73 71 6c 69  itable      sqli
2c470 74 65 33 55 6e 69 78 49 73 44 69 72 57 72 69 74  te3UnixIsDirWrit
2c480 61 62 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  able.#define sql
2c490 69 74 65 33 4f 73 53 79 6e 63 44 69 72 65 63 74  ite3OsSyncDirect
2c4a0 6f 72 79 20 20 20 20 20 20 73 71 6c 69 74 65 33  ory      sqlite3
2c4b0 55 6e 69 78 53 79 6e 63 44 69 72 65 63 74 6f 72  UnixSyncDirector
2c4c0 79 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  y.#define sqlite
2c4d0 33 4f 73 54 65 6d 70 46 69 6c 65 4e 61 6d 65 20  3OsTempFileName 
2c4e0 20 20 20 20 20 20 73 71 6c 69 74 65 33 55 6e 69        sqlite3Uni
2c4f0 78 54 65 6d 70 46 69 6c 65 4e 61 6d 65 0a 23 64  xTempFileName.#d
2c500 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4f 73 52  efine sqlite3OsR
2c510 61 6e 64 6f 6d 53 65 65 64 20 20 20 20 20 20 20  andomSeed       
2c520 20 20 73 71 6c 69 74 65 33 55 6e 69 78 52 61 6e    sqlite3UnixRan
2c530 64 6f 6d 53 65 65 64 0a 23 64 65 66 69 6e 65 20  domSeed.#define 
2c540 73 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 20 20  sqlite3OsSleep  
2c550 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
2c560 74 65 33 55 6e 69 78 53 6c 65 65 70 0a 23 64 65  te3UnixSleep.#de
2c570 66 69 6e 65 20 73 71 6c 69 74 65 33 4f 73 43 75  fine sqlite3OsCu
2c580 72 72 65 6e 74 54 69 6d 65 20 20 20 20 20 20 20  rrentTime       
2c590 20 73 71 6c 69 74 65 33 55 6e 69 78 43 75 72 72   sqlite3UnixCurr
2c5a0 65 6e 74 54 69 6d 65 0a 23 64 65 66 69 6e 65 20  entTime.#define 
2c5b0 73 71 6c 69 74 65 33 4f 73 45 6e 74 65 72 4d 75  sqlite3OsEnterMu
2c5c0 74 65 78 20 20 20 20 20 20 20 20 20 73 71 6c 69  tex         sqli
2c5d0 74 65 33 55 6e 69 78 45 6e 74 65 72 4d 75 74 65  te3UnixEnterMute
2c5e0 78 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  x.#define sqlite
2c5f0 33 4f 73 4c 65 61 76 65 4d 75 74 65 78 20 20 20  3OsLeaveMutex   
2c600 20 20 20 20 20 20 73 71 6c 69 74 65 33 55 6e 69        sqlite3Uni
2c610 78 4c 65 61 76 65 4d 75 74 65 78 0a 23 64 65 66  xLeaveMutex.#def
2c620 69 6e 65 20 73 71 6c 69 74 65 33 4f 73 49 6e 4d  ine sqlite3OsInM
2c630 75 74 65 78 20 20 20 20 20 20 20 20 20 20 20 20  utex            
2c640 73 71 6c 69 74 65 33 55 6e 69 78 49 6e 4d 75 74  sqlite3UnixInMut
2c650 65 78 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ex.#define sqlit
2c660 65 33 4f 73 54 68 72 65 61 64 53 70 65 63 69 66  e3OsThreadSpecif
2c670 69 63 44 61 74 61 20 73 71 6c 69 74 65 33 55 6e  icData sqlite3Un
2c680 69 78 54 68 72 65 61 64 53 70 65 63 69 66 69 63  ixThreadSpecific
2c690 44 61 74 61 0a 23 64 65 66 69 6e 65 20 73 71 6c  Data.#define sql
2c6a0 69 74 65 33 4f 73 4d 61 6c 6c 6f 63 20 20 20 20  ite3OsMalloc    
2c6b0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2c6c0 47 65 6e 65 72 69 63 4d 61 6c 6c 6f 63 0a 23 64  GenericMalloc.#d
2c6d0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4f 73 52  efine sqlite3OsR
2c6e0 65 61 6c 6c 6f 63 20 20 20 20 20 20 20 20 20 20  ealloc          
2c6f0 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72 69 63    sqlite3Generic
2c700 52 65 61 6c 6c 6f 63 0a 23 64 65 66 69 6e 65 20  Realloc.#define 
2c710 73 71 6c 69 74 65 33 4f 73 46 72 65 65 20 20 20  sqlite3OsFree   
2c720 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
2c730 74 65 33 47 65 6e 65 72 69 63 46 72 65 65 0a 23  te3GenericFree.#
2c740 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4f 73  define sqlite3Os
2c750 41 6c 6c 6f 63 61 74 69 6f 6e 53 69 7a 65 20 20  AllocationSize  
2c760 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72 69     sqlite3Generi
2c770 63 41 6c 6c 6f 63 61 74 69 6f 6e 53 69 7a 65 0a  cAllocationSize.
2c780 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4f  #define sqlite3O
2c790 73 44 6c 6f 70 65 6e 20 20 20 20 20 20 20 20 20  sDlopen         
2c7a0 20 20 20 20 73 71 6c 69 74 65 33 55 6e 69 78 44      sqlite3UnixD
2c7b0 6c 6f 70 65 6e 0a 23 64 65 66 69 6e 65 20 73 71  lopen.#define sq
2c7c0 6c 69 74 65 33 4f 73 44 6c 73 79 6d 20 20 20 20  lite3OsDlsym    
2c7d0 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2c7e0 33 55 6e 69 78 44 6c 73 79 6d 0a 23 64 65 66 69  3UnixDlsym.#defi
2c7f0 6e 65 20 73 71 6c 69 74 65 33 4f 73 44 6c 63 6c  ne sqlite3OsDlcl
2c800 6f 73 65 20 20 20 20 20 20 20 20 20 20 20 20 73  ose            s
2c810 71 6c 69 74 65 33 55 6e 69 78 44 6c 63 6c 6f 73  qlite3UnixDlclos
2c820 65 0a 23 65 6e 64 69 66 0a 23 69 66 20 4f 53 5f  e.#endif.#if OS_
2c830 57 49 4e 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  WIN.#define sqli
2c840 74 65 33 4f 73 4f 70 65 6e 52 65 61 64 57 72 69  te3OsOpenReadWri
2c850 74 65 20 20 20 20 20 20 73 71 6c 69 74 65 33 57  te      sqlite3W
2c860 69 6e 4f 70 65 6e 52 65 61 64 57 72 69 74 65 0a  inOpenReadWrite.
2c870 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4f  #define sqlite3O
2c880 73 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 20 20  sOpenExclusive  
2c890 20 20 20 20 73 71 6c 69 74 65 33 57 69 6e 4f 70      sqlite3WinOp
2c8a0 65 6e 45 78 63 6c 75 73 69 76 65 0a 23 64 65 66  enExclusive.#def
2c8b0 69 6e 65 20 73 71 6c 69 74 65 33 4f 73 4f 70 65  ine sqlite3OsOpe
2c8c0 6e 52 65 61 64 4f 6e 6c 79 20 20 20 20 20 20 20  nReadOnly       
2c8d0 73 71 6c 69 74 65 33 57 69 6e 4f 70 65 6e 52 65  sqlite3WinOpenRe
2c8e0 61 64 4f 6e 6c 79 0a 23 64 65 66 69 6e 65 20 73  adOnly.#define s
2c8f0 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 20 20  qlite3OsDelete  
2c900 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2c910 65 33 57 69 6e 44 65 6c 65 74 65 0a 23 64 65 66  e3WinDelete.#def
2c920 69 6e 65 20 73 71 6c 69 74 65 33 4f 73 46 69 6c  ine sqlite3OsFil
2c930 65 45 78 69 73 74 73 20 20 20 20 20 20 20 20 20  eExists         
2c940 73 71 6c 69 74 65 33 57 69 6e 46 69 6c 65 45 78  sqlite3WinFileEx
2c950 69 73 74 73 0a 23 64 65 66 69 6e 65 20 73 71 6c  ists.#define sql
2c960 69 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61  ite3OsFullPathna
2c970 6d 65 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  me       sqlite3
2c980 57 69 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 0a  WinFullPathname.
2c990 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4f  #define sqlite3O
2c9a0 73 49 73 44 69 72 57 72 69 74 61 62 6c 65 20 20  sIsDirWritable  
2c9b0 20 20 20 20 73 71 6c 69 74 65 33 57 69 6e 49 73      sqlite3WinIs
2c9c0 44 69 72 57 72 69 74 61 62 6c 65 0a 23 64 65 66  DirWritable.#def
2c9d0 69 6e 65 20 73 71 6c 69 74 65 33 4f 73 53 79 6e  ine sqlite3OsSyn
2c9e0 63 44 69 72 65 63 74 6f 72 79 20 20 20 20 20 20  cDirectory      
2c9f0 73 71 6c 69 74 65 33 57 69 6e 53 79 6e 63 44 69  sqlite3WinSyncDi
2ca00 72 65 63 74 6f 72 79 0a 23 64 65 66 69 6e 65 20  rectory.#define 
2ca10 73 71 6c 69 74 65 33 4f 73 54 65 6d 70 46 69 6c  sqlite3OsTempFil
2ca20 65 4e 61 6d 65 20 20 20 20 20 20 20 73 71 6c 69  eName       sqli
2ca30 74 65 33 57 69 6e 54 65 6d 70 46 69 6c 65 4e 61  te3WinTempFileNa
2ca40 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  me.#define sqlit
2ca50 65 33 4f 73 52 61 6e 64 6f 6d 53 65 65 64 20 20  e3OsRandomSeed  
2ca60 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 69         sqlite3Wi
2ca70 6e 52 61 6e 64 6f 6d 53 65 65 64 0a 23 64 65 66  nRandomSeed.#def
2ca80 69 6e 65 20 73 71 6c 69 74 65 33 4f 73 53 6c 65  ine sqlite3OsSle
2ca90 65 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ep              
2caa0 73 71 6c 69 74 65 33 57 69 6e 53 6c 65 65 70 0a  sqlite3WinSleep.
2cab0 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4f  #define sqlite3O
2cac0 73 43 75 72 72 65 6e 74 54 69 6d 65 20 20 20 20  sCurrentTime    
2cad0 20 20 20 20 73 71 6c 69 74 65 33 57 69 6e 43 75      sqlite3WinCu
2cae0 72 72 65 6e 74 54 69 6d 65 0a 23 64 65 66 69 6e  rrentTime.#defin
2caf0 65 20 73 71 6c 69 74 65 33 4f 73 45 6e 74 65 72  e sqlite3OsEnter
2cb00 4d 75 74 65 78 20 20 20 20 20 20 20 20 20 73 71  Mutex         sq
2cb10 6c 69 74 65 33 57 69 6e 45 6e 74 65 72 4d 75 74  lite3WinEnterMut
2cb20 65 78 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ex.#define sqlit
2cb30 65 33 4f 73 4c 65 61 76 65 4d 75 74 65 78 20 20  e3OsLeaveMutex  
2cb40 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 69         sqlite3Wi
2cb50 6e 4c 65 61 76 65 4d 75 74 65 78 0a 23 64 65 66  nLeaveMutex.#def
2cb60 69 6e 65 20 73 71 6c 69 74 65 33 4f 73 49 6e 4d  ine sqlite3OsInM
2cb70 75 74 65 78 20 20 20 20 20 20 20 20 20 20 20 20  utex            
2cb80 73 71 6c 69 74 65 33 57 69 6e 49 6e 4d 75 74 65  sqlite3WinInMute
2cb90 78 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  x.#define sqlite
2cba0 33 4f 73 54 68 72 65 61 64 53 70 65 63 69 66 69  3OsThreadSpecifi
2cbb0 63 44 61 74 61 20 73 71 6c 69 74 65 33 57 69 6e  cData sqlite3Win
2cbc0 54 68 72 65 61 64 53 70 65 63 69 66 69 63 44 61  ThreadSpecificDa
2cbd0 74 61 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ta.#define sqlit
2cbe0 65 33 4f 73 4d 61 6c 6c 6f 63 20 20 20 20 20 20  e3OsMalloc      
2cbf0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 65         sqlite3Ge
2cc00 6e 65 72 69 63 4d 61 6c 6c 6f 63 0a 23 64 65 66  nericMalloc.#def
2cc10 69 6e 65 20 73 71 6c 69 74 65 33 4f 73 52 65 61  ine sqlite3OsRea
2cc20 6c 6c 6f 63 20 20 20 20 20 20 20 20 20 20 20 20  lloc            
2cc30 73 71 6c 69 74 65 33 47 65 6e 65 72 69 63 52 65  sqlite3GenericRe
2cc40 61 6c 6c 6f 63 0a 23 64 65 66 69 6e 65 20 73 71  alloc.#define sq
2cc50 6c 69 74 65 33 4f 73 46 72 65 65 20 20 20 20 20  lite3OsFree     
2cc60 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2cc70 33 47 65 6e 65 72 69 63 46 72 65 65 0a 23 64 65  3GenericFree.#de
2cc80 66 69 6e 65 20 73 71 6c 69 74 65 33 4f 73 41 6c  fine sqlite3OsAl
2cc90 6c 6f 63 61 74 69 6f 6e 53 69 7a 65 20 20 20 20  locationSize    
2cca0 20 73 71 6c 69 74 65 33 47 65 6e 65 72 69 63 41   sqlite3GenericA
2ccb0 6c 6c 6f 63 61 74 69 6f 6e 53 69 7a 65 0a 23 64  llocationSize.#d
2ccc0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4f 73 44  efine sqlite3OsD
2ccd0 6c 6f 70 65 6e 20 20 20 20 20 20 20 20 20 20 20  lopen           
2cce0 20 20 73 71 6c 69 74 65 33 57 69 6e 44 6c 6f 70    sqlite3WinDlop
2ccf0 65 6e 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  en.#define sqlit
2cd00 65 33 4f 73 44 6c 73 79 6d 20 20 20 20 20 20 20  e3OsDlsym       
2cd10 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 69         sqlite3Wi
2cd20 6e 44 6c 73 79 6d 0a 23 64 65 66 69 6e 65 20 73  nDlsym.#define s
2cd30 71 6c 69 74 65 33 4f 73 44 6c 63 6c 6f 73 65 20  qlite3OsDlclose 
2cd40 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2cd50 65 33 57 69 6e 44 6c 63 6c 6f 73 65 0a 23 65 6e  e3WinDlclose.#en
2cd60 64 69 66 0a 23 69 66 20 4f 53 5f 4f 53 32 0a 23  dif.#if OS_OS2.#
2cd70 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4f 73  define sqlite3Os
2cd80 4f 70 65 6e 52 65 61 64 57 72 69 74 65 20 20 20  OpenReadWrite   
2cd90 20 20 20 73 71 6c 69 74 65 33 4f 73 32 4f 70 65     sqlite3Os2Ope
2cda0 6e 52 65 61 64 57 72 69 74 65 0a 23 64 65 66 69  nReadWrite.#defi
2cdb0 6e 65 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e  ne sqlite3OsOpen
2cdc0 45 78 63 6c 75 73 69 76 65 20 20 20 20 20 20 73  Exclusive      s
2cdd0 71 6c 69 74 65 33 4f 73 32 4f 70 65 6e 45 78 63  qlite3Os2OpenExc
2cde0 6c 75 73 69 76 65 0a 23 64 65 66 69 6e 65 20 73  lusive.#define s
2cdf0 71 6c 69 74 65 33 4f 73 4f 70 65 6e 52 65 61 64  qlite3OsOpenRead
2ce00 4f 6e 6c 79 20 20 20 20 20 20 20 73 71 6c 69 74  Only       sqlit
2ce10 65 33 4f 73 32 4f 70 65 6e 52 65 61 64 4f 6e 6c  e3Os2OpenReadOnl
2ce20 79 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  y.#define sqlite
2ce30 33 4f 73 44 65 6c 65 74 65 20 20 20 20 20 20 20  3OsDelete       
2ce40 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 32        sqlite3Os2
2ce50 44 65 6c 65 74 65 0a 23 64 65 66 69 6e 65 20 73  Delete.#define s
2ce60 71 6c 69 74 65 33 4f 73 46 69 6c 65 45 78 69 73  qlite3OsFileExis
2ce70 74 73 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  ts         sqlit
2ce80 65 33 4f 73 32 46 69 6c 65 45 78 69 73 74 73 0a  e3Os2FileExists.
2ce90 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4f  #define sqlite3O
2cea0 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 20 20  sFullPathname   
2ceb0 20 20 20 20 73 71 6c 69 74 65 33 4f 73 32 46 75      sqlite3Os2Fu
2cec0 6c 6c 50 61 74 68 6e 61 6d 65 0a 23 64 65 66 69  llPathname.#defi
2ced0 6e 65 20 73 71 6c 69 74 65 33 4f 73 49 73 44 69  ne sqlite3OsIsDi
2cee0 72 57 72 69 74 61 62 6c 65 20 20 20 20 20 20 73  rWritable      s
2cef0 71 6c 69 74 65 33 4f 73 32 49 73 44 69 72 57 72  qlite3Os2IsDirWr
2cf00 69 74 61 62 6c 65 0a 23 64 65 66 69 6e 65 20 73  itable.#define s
2cf10 71 6c 69 74 65 33 4f 73 53 79 6e 63 44 69 72 65  qlite3OsSyncDire
2cf20 63 74 6f 72 79 20 20 20 20 20 20 73 71 6c 69 74  ctory      sqlit
2cf30 65 33 4f 73 32 53 79 6e 63 44 69 72 65 63 74 6f  e3Os2SyncDirecto
2cf40 72 79 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ry.#define sqlit
2cf50 65 33 4f 73 54 65 6d 70 46 69 6c 65 4e 61 6d 65  e3OsTempFileName
2cf60 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73         sqlite3Os
2cf70 32 54 65 6d 70 46 69 6c 65 4e 61 6d 65 0a 23 64  2TempFileName.#d
2cf80 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4f 73 52  efine sqlite3OsR
2cf90 61 6e 64 6f 6d 53 65 65 64 20 20 20 20 20 20 20  andomSeed       
2cfa0 20 20 73 71 6c 69 74 65 33 4f 73 32 52 61 6e 64    sqlite3Os2Rand
2cfb0 6f 6d 53 65 65 64 0a 23 64 65 66 69 6e 65 20 73  omSeed.#define s
2cfc0 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 20 20 20  qlite3OsSleep   
2cfd0 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2cfe0 65 33 4f 73 32 53 6c 65 65 70 0a 23 64 65 66 69  e3Os2Sleep.#defi
2cff0 6e 65 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72  ne sqlite3OsCurr
2d000 65 6e 74 54 69 6d 65 20 20 20 20 20 20 20 20 73  entTime        s
2d010 71 6c 69 74 65 33 4f 73 32 43 75 72 72 65 6e 74  qlite3Os2Current
2d020 54 69 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  Time.#define sql
2d030 69 74 65 33 4f 73 45 6e 74 65 72 4d 75 74 65 78  ite3OsEnterMutex
2d040 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2d050 4f 73 32 45 6e 74 65 72 4d 75 74 65 78 0a 23 64  Os2EnterMutex.#d
2d060 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4f 73 4c  efine sqlite3OsL
2d070 65 61 76 65 4d 75 74 65 78 20 20 20 20 20 20 20  eaveMutex       
2d080 20 20 73 71 6c 69 74 65 33 4f 73 32 4c 65 61 76    sqlite3Os2Leav
2d090 65 4d 75 74 65 78 0a 23 64 65 66 69 6e 65 20 73  eMutex.#define s
2d0a0 71 6c 69 74 65 33 4f 73 49 6e 4d 75 74 65 78 20  qlite3OsInMutex 
2d0b0 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2d0c0 65 33 4f 73 32 49 6e 4d 75 74 65 78 0a 23 64 65  e3Os2InMutex.#de
2d0d0 66 69 6e 65 20 73 71 6c 69 74 65 33 4f 73 54 68  fine sqlite3OsTh
2d0e0 72 65 61 64 53 70 65 63 69 66 69 63 44 61 74 61  readSpecificData
2d0f0 20 73 71 6c 69 74 65 33 4f 73 32 54 68 72 65 61   sqlite3Os2Threa
2d100 64 53 70 65 63 69 66 69 63 44 61 74 61 0a 23 64  dSpecificData.#d
2d110 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4f 73 4d  efine sqlite3OsM
2d120 61 6c 6c 6f 63 20 20 20 20 20 20 20 20 20 20 20  alloc           
2d130 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72 69 63    sqlite3Generic
2d140 4d 61 6c 6c 6f 63 0a 23 64 65 66 69 6e 65 20 73  Malloc.#define s
2d150 71 6c 69 74 65 33 4f 73 52 65 61 6c 6c 6f 63 20  qlite3OsRealloc 
2d160 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2d170 65 33 47 65 6e 65 72 69 63 52 65 61 6c 6c 6f 63  e3GenericRealloc
2d180 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
2d190 4f 73 46 72 65 65 20 20 20 20 20 20 20 20 20 20  OsFree          
2d1a0 20 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65       sqlite3Gene
2d1b0 72 69 63 46 72 65 65 0a 23 64 65 66 69 6e 65 20  ricFree.#define 
2d1c0 73 71 6c 69 74 65 33 4f 73 41 6c 6c 6f 63 61 74  sqlite3OsAllocat
2d1d0 69 6f 6e 53 69 7a 65 20 20 20 20 20 73 71 6c 69  ionSize     sqli
2d1e0 74 65 33 47 65 6e 65 72 69 63 41 6c 6c 6f 63 61  te3GenericAlloca
2d1f0 74 69 6f 6e 53 69 7a 65 0a 23 64 65 66 69 6e 65  tionSize.#define
2d200 20 73 71 6c 69 74 65 33 4f 73 44 6c 6f 70 65 6e   sqlite3OsDlopen
2d210 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
2d220 69 74 65 33 4f 73 32 44 6c 6f 70 65 6e 0a 23 64  ite3Os2Dlopen.#d
2d230 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4f 73 44  efine sqlite3OsD
2d240 6c 73 79 6d 20 20 20 20 20 20 20 20 20 20 20 20  lsym            
2d250 20 20 73 71 6c 69 74 65 33 4f 73 32 44 6c 73 79    sqlite3Os2Dlsy
2d260 6d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  m.#define sqlite
2d270 33 4f 73 44 6c 63 6c 6f 73 65 20 20 20 20 20 20  3OsDlclose      
2d280 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 32        sqlite3Os2
2d290 44 6c 63 6c 6f 73 65 0a 23 65 6e 64 69 66 0a 0a  Dlclose.#endif..
2d2a0 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 75 73 69 6e  .../*.** If usin
2d2b0 67 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65  g an alternative
2d2c0 20 4f 53 20 69 6e 74 65 72 66 61 63 65 2c 20 74   OS interface, t
2d2d0 68 65 6e 20 77 65 20 6d 75 73 74 20 68 61 76 65  hen we must have
2d2e0 20 61 6e 20 22 6f 73 5f 6f 74 68 65 72 2e 68 22   an "os_other.h"
2d2f0 0a 2a 2a 20 68 65 61 64 65 72 20 66 69 6c 65 20  .** header file 
2d300 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 74 68  available for th
2d310 61 74 20 69 6e 74 65 72 66 61 63 65 2e 20 20 50  at interface.  P
2d320 72 65 73 75 6d 61 62 6c 79 20 74 68 65 20 22 6f  resumably the "o
2d330 73 5f 6f 74 68 65 72 2e 68 22 0a 2a 2a 20 68 65  s_other.h".** he
2d340 61 64 65 72 20 66 69 6c 65 20 63 6f 6e 74 61 69  ader file contai
2d350 6e 73 20 23 64 65 66 69 6e 65 73 20 73 69 6d 69  ns #defines simi
2d360 6c 61 72 20 74 6f 20 74 68 6f 73 65 20 61 62 6f  lar to those abo
2d370 76 65 2e 0a 2a 2f 0a 23 69 66 20 4f 53 5f 4f 54  ve..*/.#if OS_OT
2d380 48 45 52 0a 23 20 69 6e 63 6c 75 64 65 20 22 6f  HER.# include "o
2d390 73 5f 6f 74 68 65 72 2e 68 22 0a 23 65 6e 64 69  s_other.h".#endi
2d3a0 66 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 61  f..../*.** Forwa
2d3b0 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 0a  rd declarations.
2d3c0 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
2d3d0 74 20 4f 73 46 69 6c 65 20 4f 73 46 69 6c 65 3b  t OsFile OsFile;
2d3e0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
2d3f0 49 6f 4d 65 74 68 6f 64 20 49 6f 4d 65 74 68 6f  IoMethod IoMetho
2d400 64 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  d;../*.** An ins
2d410 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
2d420 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
2d430 20 63 6f 6e 74 61 69 6e 73 20 70 6f 69 6e 74 65   contains pointe
2d440 72 73 20 74 6f 20 61 6c 6c 0a 2a 2a 20 6d 65 74  rs to all.** met
2d450 68 6f 64 73 20 6f 6e 20 61 6e 20 4f 73 46 69 6c  hods on an OsFil
2d460 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 72  e object..*/.str
2d470 75 63 74 20 49 6f 4d 65 74 68 6f 64 20 7b 0a 20  uct IoMethod {. 
2d480 20 69 6e 74 20 28 2a 78 43 6c 6f 73 65 29 28 4f   int (*xClose)(O
2d490 73 46 69 6c 65 2a 2a 29 3b 0a 20 20 69 6e 74 20  sFile**);.  int 
2d4a0 28 2a 78 4f 70 65 6e 44 69 72 65 63 74 6f 72 79  (*xOpenDirectory
2d4b0 29 28 4f 73 46 69 6c 65 2a 2c 20 63 6f 6e 73 74  )(OsFile*, const
2d4c0 20 63 68 61 72 2a 29 3b 0a 20 20 69 6e 74 20 28   char*);.  int (
2d4d0 2a 78 52 65 61 64 29 28 4f 73 46 69 6c 65 2a 2c  *xRead)(OsFile*,
2d4e0 20 76 6f 69 64 2a 2c 20 69 6e 74 20 61 6d 74 29   void*, int amt)
2d4f0 3b 0a 20 20 69 6e 74 20 28 2a 78 57 72 69 74 65  ;.  int (*xWrite
2d500 29 28 4f 73 46 69 6c 65 2a 2c 20 63 6f 6e 73 74  )(OsFile*, const
2d510 20 76 6f 69 64 2a 2c 20 69 6e 74 20 61 6d 74 29   void*, int amt)
2d520 3b 0a 20 20 69 6e 74 20 28 2a 78 53 65 65 6b 29  ;.  int (*xSeek)
2d530 28 4f 73 46 69 6c 65 2a 2c 20 69 36 34 20 6f 66  (OsFile*, i64 of
2d540 66 73 65 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78  fset);.  int (*x
2d550 54 72 75 6e 63 61 74 65 29 28 4f 73 46 69 6c 65  Truncate)(OsFile
2d560 2a 2c 20 69 36 34 20 73 69 7a 65 29 3b 0a 20 20  *, i64 size);.  
2d570 69 6e 74 20 28 2a 78 53 79 6e 63 29 28 4f 73 46  int (*xSync)(OsF
2d580 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 20 20 76 6f  ile*, int);.  vo
2d590 69 64 20 28 2a 78 53 65 74 46 75 6c 6c 53 79 6e  id (*xSetFullSyn
2d5a0 63 29 28 4f 73 46 69 6c 65 20 2a 69 64 2c 20 69  c)(OsFile *id, i
2d5b0 6e 74 20 73 65 74 74 69 6e 67 29 3b 0a 20 20 69  nt setting);.  i
2d5c0 6e 74 20 28 2a 78 46 69 6c 65 48 61 6e 64 6c 65  nt (*xFileHandle
2d5d0 29 28 4f 73 46 69 6c 65 20 2a 69 64 29 3b 0a 20  )(OsFile *id);. 
2d5e0 20 69 6e 74 20 28 2a 78 46 69 6c 65 53 69 7a 65   int (*xFileSize
2d5f0 29 28 4f 73 46 69 6c 65 2a 2c 20 69 36 34 20 2a  )(OsFile*, i64 *
2d600 70 53 69 7a 65 29 3b 0a 20 20 69 6e 74 20 28 2a  pSize);.  int (*
2d610 78 4c 6f 63 6b 29 28 4f 73 46 69 6c 65 2a 2c 20  xLock)(OsFile*, 
2d620 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 55  int);.  int (*xU
2d630 6e 6c 6f 63 6b 29 28 4f 73 46 69 6c 65 2a 2c 20  nlock)(OsFile*, 
2d640 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4c  int);.  int (*xL
2d650 6f 63 6b 53 74 61 74 65 29 28 4f 73 46 69 6c 65  ockState)(OsFile
2d660 20 2a 69 64 29 3b 0a 20 20 69 6e 74 20 28 2a 78   *id);.  int (*x
2d670 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
2d680 6b 29 28 4f 73 46 69 6c 65 20 2a 69 64 29 3b 0a  k)(OsFile *id);.
2d690 20 20 69 6e 74 20 28 2a 78 53 65 63 74 6f 72 53    int (*xSectorS
2d6a0 69 7a 65 29 28 4f 73 46 69 6c 65 20 2a 69 64 29  ize)(OsFile *id)
2d6b0 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ;.};../*.** The 
2d6c0 4f 73 46 69 6c 65 20 6f 62 6a 65 63 74 20 64 65  OsFile object de
2d6d0 73 63 72 69 62 65 73 20 61 6e 20 6f 70 65 6e 20  scribes an open 
2d6e0 64 69 73 6b 20 66 69 6c 65 20 69 6e 20 61 6e 20  disk file in an 
2d6f0 4f 53 2d 64 65 70 65 6e 64 65 6e 74 20 77 61 79  OS-dependent way
2d700 2e 0a 2a 2a 20 54 68 65 20 76 65 72 73 69 6f 6e  ..** The version
2d710 20 6f 66 20 4f 73 46 69 6c 65 20 64 65 66 69 6e   of OsFile defin
2d720 65 64 20 68 65 72 65 20 69 73 20 61 20 67 65 6e  ed here is a gen
2d730 65 72 69 63 20 76 65 72 73 69 6f 6e 2e 20 20 45  eric version.  E
2d740 61 63 68 20 4f 53 0a 2a 2a 20 69 6d 70 6c 65 6d  ach OS.** implem
2d750 65 6e 74 61 74 69 6f 6e 20 64 65 66 69 6e 65 73  entation defines
2d760 20 69 74 73 20 6f 77 6e 20 73 75 62 63 6c 61 73   its own subclas
2d770 73 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  s of this struct
2d780 75 72 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ure that contain
2d790 73 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c 20  s.** additional 
2d7a0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6e 65 65 64  information need
2d7b0 65 64 20 74 6f 20 68 61 6e 64 6c 65 20 66 69 6c  ed to handle fil
2d7c0 65 20 49 2f 4f 2e 20 20 42 75 74 20 74 68 65 20  e I/O.  But the 
2d7d0 70 4d 65 74 68 6f 64 0a 2a 2a 20 65 6e 74 72 79  pMethod.** entry
2d7e0 20 28 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68   (pointing to th
2d7f0 65 20 76 69 72 74 75 61 6c 20 66 75 6e 63 74 69  e virtual functi
2d800 6f 6e 20 74 61 62 6c 65 29 20 61 6c 77 61 79 73  on table) always
2d810 20 6f 63 63 75 72 73 20 66 69 72 73 74 0a 2a 2a   occurs first.**
2d820 20 73 6f 20 74 68 61 74 20 77 65 20 63 61 6e 20   so that we can 
2d830 61 6c 77 61 79 73 20 66 69 6e 64 20 74 68 65 20  always find the 
2d840 61 70 70 72 6f 70 72 69 61 74 65 20 6d 65 74 68  appropriate meth
2d850 6f 64 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4f  ods..*/.struct O
2d860 73 46 69 6c 65 20 7b 0a 20 20 49 6f 4d 65 74 68  sFile {.  IoMeth
2d870 6f 64 20 63 6f 6e 73 74 20 2a 70 4d 65 74 68 6f  od const *pMetho
2d880 64 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  d;.};../*.** The
2d890 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65   following value
2d8a0 73 20 6d 61 79 20 62 65 20 70 61 73 73 65 64 20  s may be passed 
2d8b0 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  as the second ar
2d8c0 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 73 71 6c  gument to.** sql
2d8d0 69 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 20 54 68  ite3OsLock(). Th
2d8e0 65 20 76 61 72 69 6f 75 73 20 6c 6f 63 6b 73 20  e various locks 
2d8f0 65 78 68 69 62 69 74 20 74 68 65 20 66 6f 6c 6c  exhibit the foll
2d900 6f 77 69 6e 67 20 73 65 6d 61 6e 74 69 63 73 3a  owing semantics:
2d910 0a 2a 2a 0a 2a 2a 20 53 48 41 52 45 44 3a 20 20  .**.** SHARED:  
2d920 20 20 41 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20    Any number of 
2d930 70 72 6f 63 65 73 73 65 73 20 6d 61 79 20 68 6f  processes may ho
2d940 6c 64 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b  ld a SHARED lock
2d950 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 2e   simultaneously.
2d960 0a 2a 2a 20 52 45 53 45 52 56 45 44 3a 20 20 41  .** RESERVED:  A
2d970 20 73 69 6e 67 6c 65 20 70 72 6f 63 65 73 73 20   single process 
2d980 6d 61 79 20 68 6f 6c 64 20 61 20 52 45 53 45 52  may hold a RESER
2d990 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 61 20 66 69  VED lock on a fi
2d9a0 6c 65 20 61 74 0a 2a 2a 20 20 20 20 20 20 20 20  le at.**        
2d9b0 20 20 20 20 61 6e 79 20 74 69 6d 65 2e 20 4f 74      any time. Ot
2d9c0 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 6d 61  her processes ma
2d9d0 79 20 68 6f 6c 64 20 61 6e 64 20 6f 62 74 61 69  y hold and obtai
2d9e0 6e 20 6e 65 77 20 53 48 41 52 45 44 20 6c 6f 63  n new SHARED loc
2d9f0 6b 73 2e 0a 2a 2a 20 50 45 4e 44 49 4e 47 3a 20  ks..** PENDING: 
2da00 20 20 41 20 73 69 6e 67 6c 65 20 70 72 6f 63 65    A single proce
2da10 73 73 20 6d 61 79 20 68 6f 6c 64 20 61 20 50 45  ss may hold a PE
2da20 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 6e 20 61 20  NDING lock on a 
2da30 66 69 6c 65 20 61 74 0a 2a 2a 20 20 20 20 20 20  file at.**      
2da40 20 20 20 20 20 20 61 6e 79 20 6f 6e 65 20 74 69        any one ti
2da50 6d 65 2e 20 45 78 69 73 74 69 6e 67 20 53 48 41  me. Existing SHA
2da60 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79 20 70 65  RED locks may pe
2da70 72 73 69 73 74 2c 20 62 75 74 20 6e 6f 20 6e 65  rsist, but no ne
2da80 77 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  w.**            
2da90 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79  SHARED locks may
2daa0 20 62 65 20 6f 62 74 61 69 6e 65 64 20 62 79 20   be obtained by 
2dab0 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 2e  other processes.
2dac0 0a 2a 2a 20 45 58 43 4c 55 53 49 56 45 3a 20 41  .** EXCLUSIVE: A
2dad0 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  n EXCLUSIVE lock
2dae0 20 70 72 65 63 6c 75 64 65 73 20 61 6c 6c 20 6f   precludes all o
2daf0 74 68 65 72 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a  ther locks..**.*
2db00 2a 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 6d  * PENDING_LOCK m
2db10 61 79 20 6e 6f 74 20 62 65 20 70 61 73 73 65 64  ay not be passed
2db20 20 64 69 72 65 63 74 6c 79 20 74 6f 20 73 71 6c   directly to sql
2db30 69 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 20 49 6e  ite3OsLock(). In
2db40 73 74 65 61 64 2c 20 61 0a 2a 2a 20 70 72 6f 63  stead, a.** proc
2db50 65 73 73 20 74 68 61 74 20 72 65 71 75 65 73 74  ess that request
2db60 73 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c  s an EXCLUSIVE l
2db70 6f 63 6b 20 6d 61 79 20 61 63 74 75 61 6c 6c 79  ock may actually
2db80 20 6f 62 74 61 69 6e 20 61 20 50 45 4e 44 49 4e   obtain a PENDIN
2db90 47 0a 2a 2a 20 6c 6f 63 6b 2e 20 54 68 69 73 20  G.** lock. This 
2dba0 63 61 6e 20 62 65 20 75 70 67 72 61 64 65 64 20  can be upgraded 
2dbb0 74 6f 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20  to an EXCLUSIVE 
2dbc0 6c 6f 63 6b 20 62 79 20 61 20 73 75 62 73 65 71  lock by a subseq
2dbd0 75 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20  uent call to.** 
2dbe0 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 2e  sqlite3OsLock().
2dbf0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 4f 5f 4c  .*/.#define NO_L
2dc00 4f 43 4b 20 20 20 20 20 20 20 20 20 30 0a 23 64  OCK         0.#d
2dc10 65 66 69 6e 65 20 53 48 41 52 45 44 5f 4c 4f 43  efine SHARED_LOC
2dc20 4b 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20  K     1.#define 
2dc30 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 20 20  RESERVED_LOCK   
2dc40 32 0a 23 64 65 66 69 6e 65 20 50 45 4e 44 49 4e  2.#define PENDIN
2dc50 47 5f 4c 4f 43 4b 20 20 20 20 33 0a 23 64 65 66  G_LOCK    3.#def
2dc60 69 6e 65 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f  ine EXCLUSIVE_LO
2dc70 43 4b 20 20 34 0a 0a 2f 2a 0a 2a 2a 20 46 69 6c  CK  4../*.** Fil
2dc80 65 20 4c 6f 63 6b 69 6e 67 20 4e 6f 74 65 73 3a  e Locking Notes:
2dc90 20 20 28 4d 6f 73 74 6c 79 20 61 62 6f 75 74 20    (Mostly about 
2dca0 77 69 6e 64 6f 77 73 20 62 75 74 20 61 6c 73 6f  windows but also
2dcb0 20 73 6f 6d 65 20 69 6e 66 6f 20 66 6f 72 20 55   some info for U
2dcc0 6e 69 78 29 0a 2a 2a 0a 2a 2a 20 57 65 20 63 61  nix).**.** We ca
2dcd0 6e 6e 6f 74 20 75 73 65 20 4c 6f 63 6b 46 69 6c  nnot use LockFil
2dce0 65 45 78 28 29 20 6f 72 20 55 6e 6c 6f 63 6b 46  eEx() or UnlockF
2dcf0 69 6c 65 45 78 28 29 20 6f 6e 20 57 69 6e 39 35  ileEx() on Win95
2dd00 2f 39 38 2f 4d 45 20 62 65 63 61 75 73 65 0a 2a  /98/ME because.*
2dd10 2a 20 74 68 6f 73 65 20 66 75 6e 63 74 69 6f 6e  * those function
2dd20 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61  s are not availa
2dd30 62 6c 65 2e 20 20 53 6f 20 77 65 20 75 73 65 20  ble.  So we use 
2dd40 6f 6e 6c 79 20 4c 6f 63 6b 46 69 6c 65 28 29 20  only LockFile() 
2dd50 61 6e 64 0a 2a 2a 20 55 6e 6c 6f 63 6b 46 69 6c  and.** UnlockFil
2dd60 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 63 6b 46  e()..**.** LockF
2dd70 69 6c 65 28 29 20 70 72 65 76 65 6e 74 73 20 6e  ile() prevents n
2dd80 6f 74 20 6a 75 73 74 20 77 72 69 74 69 6e 67 20  ot just writing 
2dd90 62 75 74 20 61 6c 73 6f 20 72 65 61 64 69 6e 67  but also reading
2dda0 20 62 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73   by other proces
2ddb0 73 65 73 2e 0a 2a 2a 20 41 20 53 48 41 52 45 44  ses..** A SHARED
2ddc0 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 61 69 6e 65  _LOCK is obtaine
2ddd0 64 20 62 79 20 6c 6f 63 6b 69 6e 67 20 61 20 73  d by locking a s
2dde0 69 6e 67 6c 65 20 72 61 6e 64 6f 6d 6c 79 2d 63  ingle randomly-c
2ddf0 68 6f 73 65 6e 20 0a 2a 2a 20 62 79 74 65 20 6f  hosen .** byte o
2de00 75 74 20 6f 66 20 61 20 73 70 65 63 69 66 69 63  ut of a specific
2de10 20 72 61 6e 67 65 20 6f 66 20 62 79 74 65 73 2e   range of bytes.
2de20 20 54 68 65 20 6c 6f 63 6b 20 62 79 74 65 20 69   The lock byte i
2de30 73 20 6f 62 74 61 69 6e 65 64 20 61 74 20 0a 2a  s obtained at .*
2de40 2a 20 72 61 6e 64 6f 6d 20 73 6f 20 74 77 6f 20  * random so two 
2de50 73 65 70 61 72 61 74 65 20 72 65 61 64 65 72 73  separate readers
2de60 20 63 61 6e 20 70 72 6f 62 61 62 6c 79 20 61 63   can probably ac
2de70 63 65 73 73 20 74 68 65 20 66 69 6c 65 20 61 74  cess the file at
2de80 20 74 68 65 20 0a 2a 2a 20 73 61 6d 65 20 74 69   the .** same ti
2de90 6d 65 2c 20 75 6e 6c 65 73 73 20 74 68 65 79 20  me, unless they 
2dea0 61 72 65 20 75 6e 6c 75 63 6b 79 20 61 6e 64 20  are unlucky and 
2deb0 63 68 6f 6f 73 65 20 74 68 65 20 73 61 6d 65 20  choose the same 
2dec0 6c 6f 63 6b 20 62 79 74 65 2e 0a 2a 2a 20 41 6e  lock byte..** An
2ded0 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20   EXCLUSIVE_LOCK 
2dee0 69 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c  is obtained by l
2def0 6f 63 6b 69 6e 67 20 61 6c 6c 20 62 79 74 65 73  ocking all bytes
2df00 20 69 6e 20 74 68 65 20 72 61 6e 67 65 2e 0a 2a   in the range..*
2df10 2a 20 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79  * There can only
2df20 20 62 65 20 6f 6e 65 20 77 72 69 74 65 72 2e 20   be one writer. 
2df30 20 41 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b   A RESERVED_LOCK
2df40 20 69 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20   is obtained by 
2df50 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 61 20 73 69 6e  locking.** a sin
2df60 67 6c 65 20 62 79 74 65 20 6f 66 20 74 68 65 20  gle byte of the 
2df70 66 69 6c 65 20 74 68 61 74 20 69 73 20 64 65 73  file that is des
2df80 69 67 6e 61 74 65 64 20 61 73 20 74 68 65 20 72  ignated as the r
2df90 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 62 79 74  eserved lock byt
2dfa0 65 2e 0a 2a 2a 20 41 20 50 45 4e 44 49 4e 47 5f  e..** A PENDING_
2dfb0 4c 4f 43 4b 20 69 73 20 6f 62 74 61 69 6e 65 64  LOCK is obtained
2dfc0 20 62 79 20 6c 6f 63 6b 69 6e 67 20 61 20 64 65   by locking a de
2dfd0 73 69 67 6e 61 74 65 64 20 62 79 74 65 20 64 69  signated byte di
2dfe0 66 66 65 72 65 6e 74 20 66 72 6f 6d 0a 2a 2a 20  fferent from.** 
2dff0 74 68 65 20 52 45 53 45 52 56 45 44 5f 4c 4f 43  the RESERVED_LOC
2e000 4b 20 62 79 74 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6e  K byte..**.** On
2e010 20 57 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 79 73   WinNT/2K/XP sys
2e020 74 65 6d 73 2c 20 4c 6f 63 6b 46 69 6c 65 45 78  tems, LockFileEx
2e030 28 29 20 61 6e 64 20 55 6e 6c 6f 63 6b 46 69 6c  () and UnlockFil
2e040 65 45 78 28 29 20 61 72 65 20 61 76 61 69 6c 61  eEx() are availa
2e050 62 6c 65 2c 0a 2a 2a 20 77 68 69 63 68 20 6d 65  ble,.** which me
2e060 61 6e 73 20 77 65 20 63 61 6e 20 75 73 65 20 72  ans we can use r
2e070 65 61 64 65 72 2f 77 72 69 74 65 72 20 6c 6f 63  eader/writer loc
2e080 6b 73 2e 20 20 57 68 65 6e 20 72 65 61 64 65 72  ks.  When reader
2e090 2f 77 72 69 74 65 72 20 6c 6f 63 6b 73 0a 2a 2a  /writer locks.**
2e0a0 20 61 72 65 20 75 73 65 64 2c 20 74 68 65 20 6c   are used, the l
2e0b0 6f 63 6b 20 69 73 20 70 6c 61 63 65 64 20 6f 6e  ock is placed on
2e0c0 20 74 68 65 20 73 61 6d 65 20 72 61 6e 67 65 20   the same range 
2e0d0 6f 66 20 62 79 74 65 73 20 74 68 61 74 20 69 73  of bytes that is
2e0e0 20 75 73 65 64 0a 2a 2a 20 66 6f 72 20 70 72 6f   used.** for pro
2e0f0 62 61 62 69 6c 69 73 74 69 63 20 6c 6f 63 6b 69  babilistic locki
2e100 6e 67 20 69 6e 20 57 69 6e 39 35 2f 39 38 2f 4d  ng in Win95/98/M
2e110 45 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20 6c  E.  Hence, the l
2e120 6f 63 6b 69 6e 67 20 73 63 68 65 6d 65 0a 2a 2a  ocking scheme.**
2e130 20 77 69 6c 6c 20 73 75 70 70 6f 72 74 20 74 77   will support tw
2e140 6f 20 6f 72 20 6d 6f 72 65 20 57 69 6e 39 35 20  o or more Win95 
2e150 72 65 61 64 65 72 73 20 6f 72 20 74 77 6f 20 6f  readers or two o
2e160 72 20 6d 6f 72 65 20 57 69 6e 4e 54 20 72 65 61  r more WinNT rea
2e170 64 65 72 73 2e 0a 2a 2a 20 42 75 74 20 61 20 73  ders..** But a s
2e180 69 6e 67 6c 65 20 57 69 6e 39 35 20 72 65 61 64  ingle Win95 read
2e190 65 72 20 77 69 6c 6c 20 6c 6f 63 6b 20 6f 75 74  er will lock out
2e1a0 20 61 6c 6c 20 57 69 6e 4e 54 20 72 65 61 64 65   all WinNT reade
2e1b0 72 73 20 61 6e 64 20 61 20 73 69 6e 67 6c 65 0a  rs and a single.
2e1c0 2a 2a 20 57 69 6e 4e 54 20 72 65 61 64 65 72 20  ** WinNT reader 
2e1d0 77 69 6c 6c 20 6c 6f 63 6b 20 6f 75 74 20 61 6c  will lock out al
2e1e0 6c 20 6f 74 68 65 72 20 57 69 6e 39 35 20 72 65  l other Win95 re
2e1f0 61 64 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  aders..**.** The
2e200 20 66 6f 6c 6c 6f 77 69 6e 67 20 23 64 65 66 69   following #defi
2e210 6e 65 73 20 73 70 65 63 69 66 79 20 74 68 65 20  nes specify the 
2e220 72 61 6e 67 65 20 6f 66 20 62 79 74 65 73 20 75  range of bytes u
2e230 73 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 2e  sed for locking.
2e240 0a 2a 2a 20 53 48 41 52 45 44 5f 53 49 5a 45 20  .** SHARED_SIZE 
2e250 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
2e260 20 62 79 74 65 73 20 61 76 61 69 6c 61 62 6c 65   bytes available
2e270 20 69 6e 20 74 68 65 20 70 6f 6f 6c 20 66 72 6f   in the pool fro
2e280 6d 20 77 68 69 63 68 0a 2a 2a 20 61 20 72 61 6e  m which.** a ran
2e290 64 6f 6d 20 62 79 74 65 20 69 73 20 73 65 6c 65  dom byte is sele
2e2a0 63 74 65 64 20 66 6f 72 20 61 20 73 68 61 72 65  cted for a share
2e2b0 64 20 6c 6f 63 6b 2e 20 20 54 68 65 20 70 6f 6f  d lock.  The poo
2e2c0 6c 20 6f 66 20 62 79 74 65 73 20 66 6f 72 0a 2a  l of bytes for.*
2e2d0 2a 20 73 68 61 72 65 64 20 6c 6f 63 6b 73 20 62  * shared locks b
2e2e0 65 67 69 6e 73 20 61 74 20 53 48 41 52 45 44 5f  egins at SHARED_
2e2f0 46 49 52 53 54 2e 20 0a 2a 2a 0a 2a 2a 20 54 68  FIRST. .**.** Th
2e300 65 73 65 20 23 64 65 66 69 6e 65 73 20 61 72 65  ese #defines are
2e310 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 73 71   available in sq
2e320 6c 69 74 65 5f 61 75 78 2e 68 20 73 6f 20 74 68  lite_aux.h so th
2e330 61 74 20 61 64 61 70 74 6f 72 73 20 66 6f 72 0a  at adaptors for.
2e340 2a 2a 20 63 6f 6e 6e 65 63 74 69 6e 67 20 53 51  ** connecting SQ
2e350 4c 69 74 65 20 74 6f 20 6f 74 68 65 72 20 6f 70  Lite to other op
2e360 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 73 20  erating systems 
2e370 63 61 6e 20 75 73 65 20 74 68 65 20 73 61 6d 65  can use the same
2e380 20 62 79 74 65 0a 2a 2a 20 72 61 6e 67 65 73 20   byte.** ranges 
2e390 66 6f 72 20 6c 6f 63 6b 69 6e 67 2e 20 20 49 6e  for locking.  In
2e3a0 20 70 61 72 74 69 63 75 6c 61 72 2c 20 74 68 65   particular, the
2e3b0 20 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 20 73 74   same locking st
2e3c0 72 61 74 65 67 79 20 61 6e 64 0a 2a 2a 20 62 79  rategy and.** by
2e3d0 74 65 20 72 61 6e 67 65 73 20 61 72 65 20 75 73  te ranges are us
2e3e0 65 64 20 66 6f 72 20 55 6e 69 78 2e 20 20 54 68  ed for Unix.  Th
2e3f0 69 73 20 6c 65 61 76 65 73 20 6f 70 65 6e 20 74  is leaves open t
2e400 68 65 20 70 6f 73 73 69 62 6c 69 74 79 20 6f 66  he possiblity of
2e410 20 68 61 76 69 6e 67 0a 2a 2a 20 63 6c 69 65 6e   having.** clien
2e420 74 73 20 6f 6e 20 77 69 6e 39 35 2c 20 77 69 6e  ts on win95, win
2e430 4e 54 2c 20 61 6e 64 20 75 6e 69 78 20 61 6c 6c  NT, and unix all
2e440 20 74 61 6c 6b 69 6e 67 20 74 6f 20 74 68 65 20   talking to the 
2e450 73 61 6d 65 20 73 68 61 72 65 64 20 66 69 6c 65  same shared file
2e460 0a 2a 2a 20 61 6e 64 20 61 6c 6c 20 6c 6f 63 6b  .** and all lock
2e470 69 6e 67 20 63 6f 72 72 65 63 74 6c 79 2e 20 20  ing correctly.  
2e480 54 6f 20 64 6f 20 73 6f 20 77 6f 75 6c 64 20 72  To do so would r
2e490 65 71 75 69 72 65 20 74 68 61 74 20 73 61 6d 62  equire that samb
2e4a0 61 20 28 6f 72 20 77 68 61 74 65 76 65 72 0a 2a  a (or whatever.*
2e4b0 2a 20 74 6f 6f 6c 20 69 73 20 62 65 69 6e 67 20  * tool is being 
2e4c0 75 73 65 64 20 66 6f 72 20 66 69 6c 65 20 73 68  used for file sh
2e4d0 61 72 69 6e 67 29 20 69 6d 70 6c 65 6d 65 6e 74  aring) implement
2e4e0 73 20 6c 6f 63 6b 73 20 63 6f 72 72 65 63 74 6c  s locks correctl
2e4f0 79 20 62 65 74 77 65 65 6e 0a 2a 2a 20 77 69 6e  y between.** win
2e500 64 6f 77 73 20 61 6e 64 20 75 6e 69 78 2e 20 20  dows and unix.  
2e510 49 27 6d 20 67 75 65 73 73 69 6e 67 20 74 68 61  I'm guessing tha
2e520 74 20 69 73 6e 27 74 20 6c 69 6b 65 6c 79 20 74  t isn't likely t
2e530 6f 20 68 61 70 70 65 6e 2c 20 62 75 74 20 62 79  o happen, but by
2e540 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 73 61  .** using the sa
2e550 6d 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 65  me locking range
2e560 20 77 65 20 61 72 65 20 61 74 20 6c 65 61 73 74   we are at least
2e570 20 6f 70 65 6e 20 74 6f 20 74 68 65 20 70 6f 73   open to the pos
2e580 73 69 62 69 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20  sibility..**.** 
2e590 4c 6f 63 6b 69 6e 67 20 69 6e 20 77 69 6e 64 6f  Locking in windo
2e5a0 77 73 20 69 73 20 6d 61 6e 64 69 74 6f 72 79 2e  ws is manditory.
2e5b0 20 20 46 6f 72 20 74 68 69 73 20 72 65 61 73 6f    For this reaso
2e5c0 6e 2c 20 77 65 20 63 61 6e 6e 6f 74 20 73 74 6f  n, we cannot sto
2e5d0 72 65 0a 2a 2a 20 61 63 74 75 61 6c 20 64 61 74  re.** actual dat
2e5e0 61 20 69 6e 20 74 68 65 20 62 79 74 65 73 20 75  a in the bytes u
2e5f0 73 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 2e  sed for locking.
2e600 20 20 54 68 65 20 70 61 67 65 72 20 6e 65 76 65    The pager neve
2e610 72 20 61 6c 6c 6f 63 61 74 65 73 0a 2a 2a 20 74  r allocates.** t
2e620 68 65 20 70 61 67 65 73 20 69 6e 76 6f 6c 76 65  he pages involve
2e630 64 20 69 6e 20 6c 6f 63 6b 69 6e 67 20 74 68 65  d in locking the
2e640 72 65 66 6f 72 65 2e 20 20 53 48 41 52 45 44 5f  refore.  SHARED_
2e650 53 49 5a 45 20 69 73 20 73 65 6c 65 63 74 65 64  SIZE is selected
2e660 20 73 6f 0a 2a 2a 20 74 68 61 74 20 61 6c 6c 20   so.** that all 
2e670 6c 6f 63 6b 73 20 77 69 6c 6c 20 66 69 74 20 6f  locks will fit o
2e680 6e 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20  n a single page 
2e690 65 76 65 6e 20 61 74 20 74 68 65 20 6d 69 6e 69  even at the mini
2e6a0 6d 75 6d 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a  mum page size..*
2e6b0 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 64  * PENDING_BYTE d
2e6c0 65 66 69 6e 65 73 20 74 68 65 20 62 65 67 69 6e  efines the begin
2e6d0 6e 69 6e 67 20 6f 66 20 74 68 65 20 6c 6f 63 6b  ning of the lock
2e6e0 73 2e 20 20 42 79 20 64 65 66 61 75 6c 74 20 50  s.  By default P
2e6f0 45 4e 44 49 4e 47 5f 42 59 54 45 0a 2a 2a 20 69  ENDING_BYTE.** i
2e700 73 20 73 65 74 20 68 69 67 68 20 73 6f 20 74 68  s set high so th
2e710 61 74 20 77 65 20 64 6f 6e 27 74 20 68 61 76 65  at we don't have
2e720 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 6e 20   to allocate an 
2e730 75 6e 75 73 65 64 20 70 61 67 65 20 65 78 63 65  unused page exce
2e740 70 74 0a 2a 2a 20 66 6f 72 20 76 65 72 79 20 6c  pt.** for very l
2e750 61 72 67 65 20 64 61 74 61 62 61 73 65 73 2e 20  arge databases. 
2e760 20 42 75 74 20 6f 6e 65 20 73 68 6f 75 6c 64 20   But one should 
2e770 74 65 73 74 20 74 68 65 20 70 61 67 65 20 73 6b  test the page sk
2e780 69 70 70 69 6e 67 20 6c 6f 67 69 63 20 0a 2a 2a  ipping logic .**
2e790 20 62 79 20 73 65 74 74 69 6e 67 20 50 45 4e 44   by setting PEND
2e7a0 49 4e 47 5f 42 59 54 45 20 6c 6f 77 20 61 6e 64  ING_BYTE low and
2e7b0 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 65 6e 74   running the ent
2e7c0 69 72 65 20 72 65 67 72 65 73 73 69 6f 6e 20 73  ire regression s
2e7d0 75 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 6e  uite..**.** Chan
2e7e0 67 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 6f  ging the value o
2e7f0 66 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 72  f PENDING_BYTE r
2e800 65 73 75 6c 74 73 20 69 6e 20 61 20 73 75 62 74  esults in a subt
2e810 6c 79 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 0a  ly incompatible.
2e820 2a 2a 20 66 69 6c 65 20 66 6f 72 6d 61 74 2e 20  ** file format. 
2e830 20 44 65 70 65 6e 64 69 6e 67 20 6f 6e 20 68 6f   Depending on ho
2e840 77 20 69 74 20 69 73 20 63 68 61 6e 67 65 64 2c  w it is changed,
2e850 20 79 6f 75 20 6d 69 67 68 74 20 6e 6f 74 20 6e   you might not n
2e860 6f 74 69 63 65 0a 2a 2a 20 74 68 65 20 69 6e 63  otice.** the inc
2e870 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 72 69 67  ompatibility rig
2e880 68 74 20 61 77 61 79 2c 20 65 76 65 6e 20 72 75  ht away, even ru
2e890 6e 6e 69 6e 67 20 61 20 66 75 6c 6c 20 72 65 67  nning a full reg
2e8a0 72 65 73 73 69 6f 6e 20 74 65 73 74 2e 0a 2a 2a  ression test..**
2e8b0 20 54 68 65 20 64 65 66 61 75 6c 74 20 6c 6f 63   The default loc
2e8c0 61 74 69 6f 6e 20 6f 66 20 50 45 4e 44 49 4e 47  ation of PENDING
2e8d0 5f 42 59 54 45 20 69 73 20 74 68 65 20 66 69 72  _BYTE is the fir
2e8e0 73 74 20 62 79 74 65 20 70 61 73 74 20 74 68 65  st byte past the
2e8f0 0a 2a 2a 20 31 47 42 20 62 6f 75 6e 64 61 72 79  .** 1GB boundary
2e900 2e 0a 2a 2a 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ..**.*/.#ifndef 
2e910 53 51 4c 49 54 45 5f 54 45 53 54 0a 23 64 65 66  SQLITE_TEST.#def
2e920 69 6e 65 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  ine PENDING_BYTE
2e930 20 20 20 20 20 20 30 78 34 30 30 30 30 30 30 30        0x40000000
2e940 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20    /* First byte 
2e950 70 61 73 74 20 74 68 65 20 31 47 42 20 62 6f 75  past the 1GB bou
2e960 6e 64 61 72 79 20 2a 2f 0a 23 65 6c 73 65 0a 65  ndary */.#else.e
2e970 78 74 65 72 6e 20 75 6e 73 69 67 6e 65 64 20 69  xtern unsigned i
2e980 6e 74 20 73 71 6c 69 74 65 33 5f 70 65 6e 64 69  nt sqlite3_pendi
2e990 6e 67 5f 62 79 74 65 3b 0a 23 64 65 66 69 6e 65  ng_byte;.#define
2e9a0 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 73 71   PENDING_BYTE sq
2e9b0 6c 69 74 65 33 5f 70 65 6e 64 69 6e 67 5f 62 79  lite3_pending_by
2e9c0 74 65 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69  te.#endif..#defi
2e9d0 6e 65 20 52 45 53 45 52 56 45 44 5f 42 59 54 45  ne RESERVED_BYTE
2e9e0 20 20 20 20 20 28 50 45 4e 44 49 4e 47 5f 42 59       (PENDING_BY
2e9f0 54 45 2b 31 29 0a 23 64 65 66 69 6e 65 20 53 48  TE+1).#define SH
2ea00 41 52 45 44 5f 46 49 52 53 54 20 20 20 20 20 20  ARED_FIRST      
2ea10 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 32 29  (PENDING_BYTE+2)
2ea20 0a 23 64 65 66 69 6e 65 20 53 48 41 52 45 44 5f  .#define SHARED_
2ea30 53 49 5a 45 20 20 20 20 20 20 20 35 31 30 0a 0a  SIZE       510..
2ea40 2f 2a 0a 2a 2a 20 50 72 6f 74 6f 74 79 70 65 73  /*.** Prototypes
2ea50 20 66 6f 72 20 6f 70 65 72 61 74 69 6e 67 20 73   for operating s
2ea60 79 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65 20  ystem interface 
2ea70 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 53 51 4c  routines..*/.SQL
2ea80 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
2ea90 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 4f  sqlite3OsClose(O
2eaa0 73 46 69 6c 65 2a 2a 29 3b 0a 53 51 4c 49 54 45  sFile**);.SQLITE
2eab0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
2eac0 69 74 65 33 4f 73 4f 70 65 6e 44 69 72 65 63 74  ite3OsOpenDirect
2ead0 6f 72 79 28 4f 73 46 69 6c 65 2a 2c 20 63 6f 6e  ory(OsFile*, con
2eae0 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54  st char*);.SQLIT
2eaf0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
2eb00 6c 69 74 65 33 4f 73 52 65 61 64 28 4f 73 46 69  lite3OsRead(OsFi
2eb10 6c 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20  le*, void*, int 
2eb20 61 6d 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  amt);.SQLITE_PRI
2eb30 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
2eb40 4f 73 57 72 69 74 65 28 4f 73 46 69 6c 65 2a 2c  OsWrite(OsFile*,
2eb50 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e   const void*, in
2eb60 74 20 61 6d 74 29 3b 0a 53 51 4c 49 54 45 5f 50  t amt);.SQLITE_P
2eb70 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2eb80 65 33 4f 73 53 65 65 6b 28 4f 73 46 69 6c 65 2a  e3OsSeek(OsFile*
2eb90 2c 20 69 36 34 20 6f 66 66 73 65 74 29 3b 0a 53  , i64 offset);.S
2eba0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
2ebb0 74 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63  t sqlite3OsTrunc
2ebc0 61 74 65 28 4f 73 46 69 6c 65 2a 2c 20 69 36 34  ate(OsFile*, i64
2ebd0 20 73 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50   size);.SQLITE_P
2ebe0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2ebf0 65 33 4f 73 53 79 6e 63 28 4f 73 46 69 6c 65 2a  e3OsSync(OsFile*
2ec00 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
2ec10 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2ec20 74 65 33 4f 73 53 65 74 46 75 6c 6c 53 79 6e 63  te3OsSetFullSync
2ec30 28 4f 73 46 69 6c 65 20 2a 69 64 2c 20 69 6e 74  (OsFile *id, int
2ec40 20 73 65 74 74 69 6e 67 29 3b 0a 53 51 4c 49 54   setting);.SQLIT
2ec50 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
2ec60 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28  lite3OsFileSize(
2ec70 4f 73 46 69 6c 65 2a 2c 20 69 36 34 20 2a 70 53  OsFile*, i64 *pS
2ec80 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ize);.SQLITE_PRI
2ec90 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
2eca0 4f 73 4c 6f 63 6b 28 4f 73 46 69 6c 65 2a 2c 20  OsLock(OsFile*, 
2ecb0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
2ecc0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
2ecd0 4f 73 55 6e 6c 6f 63 6b 28 4f 73 46 69 6c 65 2a  OsUnlock(OsFile*
2ece0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
2ecf0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2ed00 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76 65  e3OsCheckReserve
2ed10 64 4c 6f 63 6b 28 4f 73 46 69 6c 65 20 2a 69 64  dLock(OsFile *id
2ed20 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
2ed30 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f  E int sqlite3OsO
2ed40 70 65 6e 52 65 61 64 57 72 69 74 65 28 63 6f 6e  penReadWrite(con
2ed50 73 74 20 63 68 61 72 2a 2c 20 4f 73 46 69 6c 65  st char*, OsFile
2ed60 2a 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54  **, int*);.SQLIT
2ed70 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
2ed80 6c 69 74 65 33 4f 73 4f 70 65 6e 45 78 63 6c 75  lite3OsOpenExclu
2ed90 73 69 76 65 28 63 6f 6e 73 74 20 63 68 61 72 2a  sive(const char*
2eda0 2c 20 4f 73 46 69 6c 65 2a 2a 2c 20 69 6e 74 29  , OsFile**, int)
2edb0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
2edc0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f 70   int sqlite3OsOp
2edd0 65 6e 52 65 61 64 4f 6e 6c 79 28 63 6f 6e 73 74  enReadOnly(const
2ede0 20 63 68 61 72 2a 2c 20 4f 73 46 69 6c 65 2a 2a   char*, OsFile**
2edf0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
2ee00 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 44  E int sqlite3OsD
2ee10 65 6c 65 74 65 28 63 6f 6e 73 74 20 63 68 61 72  elete(const char
2ee20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
2ee30 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
2ee40 46 69 6c 65 45 78 69 73 74 73 28 63 6f 6e 73 74  FileExists(const
2ee50 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   char*);.SQLITE_
2ee60 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71  PRIVATE char *sq
2ee70 6c 69 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e  lite3OsFullPathn
2ee80 61 6d 65 28 63 6f 6e 73 74 20 63 68 61 72 2a 29  ame(const char*)
2ee90 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
2eea0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 49 73   int sqlite3OsIs
2eeb0 44 69 72 57 72 69 74 61 62 6c 65 28 63 68 61 72  DirWritable(char
2eec0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
2eed0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
2eee0 53 79 6e 63 44 69 72 65 63 74 6f 72 79 28 63 6f  SyncDirectory(co
2eef0 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49  nst char*);.SQLI
2ef00 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
2ef10 71 6c 69 74 65 33 4f 73 53 65 63 74 6f 72 53 69  qlite3OsSectorSi
2ef20 7a 65 28 4f 73 46 69 6c 65 20 2a 69 64 29 3b 0a  ze(OsFile *id);.
2ef30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2ef40 6e 74 20 73 71 6c 69 74 65 33 4f 73 54 65 6d 70  nt sqlite3OsTemp
2ef50 46 69 6c 65 4e 61 6d 65 28 63 68 61 72 2a 29 3b  FileName(char*);
2ef60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2ef70 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 52 61 6e  int sqlite3OsRan
2ef80 64 6f 6d 53 65 65 64 28 63 68 61 72 2a 29 3b 0a  domSeed(char*);.
2ef90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2efa0 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 6c 65 65  nt sqlite3OsSlee
2efb0 70 28 69 6e 74 20 6d 73 29 3b 0a 53 51 4c 49 54  p(int ms);.SQLIT
2efc0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
2efd0 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69  lite3OsCurrentTi
2efe0 6d 65 28 64 6f 75 62 6c 65 2a 29 3b 0a 53 51 4c  me(double*);.SQL
2eff0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2f000 20 73 71 6c 69 74 65 33 4f 73 45 6e 74 65 72 4d   sqlite3OsEnterM
2f010 75 74 65 78 28 76 6f 69 64 29 3b 0a 53 51 4c 49  utex(void);.SQLI
2f020 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2f030 73 71 6c 69 74 65 33 4f 73 4c 65 61 76 65 4d 75  sqlite3OsLeaveMu
2f040 74 65 78 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54  tex(void);.SQLIT
2f050 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
2f060 6c 69 74 65 33 4f 73 49 6e 4d 75 74 65 78 28 69  lite3OsInMutex(i
2f070 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
2f080 41 54 45 20 54 68 72 65 61 64 44 61 74 61 20 2a  ATE ThreadData *
2f090 73 71 6c 69 74 65 33 4f 73 54 68 72 65 61 64 53  sqlite3OsThreadS
2f0a0 70 65 63 69 66 69 63 44 61 74 61 28 69 6e 74 29  pecificData(int)
2f0b0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
2f0c0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4f 73   void *sqlite3Os
2f0d0 4d 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c  Malloc(int);.SQL
2f0e0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2f0f0 20 2a 73 71 6c 69 74 65 33 4f 73 52 65 61 6c 6c   *sqlite3OsReall
2f100 6f 63 28 76 6f 69 64 20 2a 2c 20 69 6e 74 29 3b  oc(void *, int);
2f110 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2f120 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 73 46 72  void sqlite3OsFr
2f130 65 65 28 76 6f 69 64 20 2a 29 3b 0a 53 51 4c 49  ee(void *);.SQLI
2f140 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
2f150 71 6c 69 74 65 33 4f 73 41 6c 6c 6f 63 61 74 69  qlite3OsAllocati
2f160 6f 6e 53 69 7a 65 28 76 6f 69 64 20 2a 29 3b 0a  onSize(void *);.
2f170 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2f180 6f 69 64 20 2a 73 71 6c 69 74 65 33 4f 73 44 6c  oid *sqlite3OsDl
2f190 6f 70 65 6e 28 63 6f 6e 73 74 20 63 68 61 72 2a  open(const char*
2f1a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
2f1b0 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4f  E void *sqlite3O
2f1c0 73 44 6c 73 79 6d 28 76 6f 69 64 2a 2c 20 63 6f  sDlsym(void*, co
2f1d0 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49  nst char*);.SQLI
2f1e0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
2f1f0 71 6c 69 74 65 33 4f 73 44 6c 63 6c 6f 73 65 28  qlite3OsDlclose(
2f200 76 6f 69 64 2a 29 3b 0a 0a 23 69 66 20 64 65 66  void*);..#if def
2f210 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54  ined(SQLITE_TEST
2f220 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
2f230 49 54 45 5f 44 45 42 55 47 29 0a 53 51 4c 49 54  ITE_DEBUG).SQLIT
2f240 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20  E_PRIVATE   int 
2f250 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 48 61 6e  sqlite3OsFileHan
2f260 64 6c 65 28 4f 73 46 69 6c 65 20 2a 69 64 29 3b  dle(OsFile *id);
2f270 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2f280 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4c    int sqlite3OsL
2f290 6f 63 6b 53 74 61 74 65 28 4f 73 46 69 6c 65 20  ockState(OsFile 
2f2a0 2a 69 64 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a  *id);.#endif../*
2f2b0 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54  .** If the SQLIT
2f2c0 45 5f 45 4e 41 42 4c 45 5f 52 45 44 45 46 5f 49  E_ENABLE_REDEF_I
2f2d0 4f 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e  O macro is defin
2f2e0 65 64 2c 20 74 68 65 6e 20 74 68 65 20 4f 53 2d  ed, then the OS-
2f2f0 6c 61 79 65 72 0a 2a 2a 20 69 6e 74 65 72 66 61  layer.** interfa
2f300 63 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20  ce routines are 
2f310 6e 6f 74 20 63 61 6c 6c 65 64 20 64 69 72 65 63  not called direc
2f320 74 6c 79 20 62 75 74 20 61 72 65 20 69 6e 76 6f  tly but are invo
2f330 6b 65 64 20 75 73 69 6e 67 0a 2a 2a 20 70 6f 69  ked using.** poi
2f340 6e 74 65 72 73 20 74 6f 20 66 75 6e 63 74 69 6f  nters to functio
2f350 6e 73 2e 20 20 54 68 69 73 20 61 6c 6c 6f 77 73  ns.  This allows
2f360 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
2f370 69 6f 6e 20 6f 66 20 76 61 72 69 6f 75 73 0a 2a  ion of various.*
2f380 2a 20 4f 53 2d 6c 61 79 65 72 20 69 6e 74 65 72  * OS-layer inter
2f390 66 61 63 65 20 72 6f 75 74 69 6e 65 73 20 74 6f  face routines to
2f3a0 20 62 65 20 6d 6f 64 69 66 69 65 64 20 61 74 20   be modified at 
2f3b0 72 75 6e 2d 74 69 6d 65 2e 20 20 54 68 65 72 65  run-time.  There
2f3c0 20 61 72 65 0a 2a 2a 20 6f 62 73 63 75 72 65 20   are.** obscure 
2f3d0 62 75 74 20 6c 65 67 69 74 69 6d 61 74 65 20 72  but legitimate r
2f3e0 65 61 73 6f 6e 73 20 66 6f 72 20 77 61 6e 74 69  easons for wanti
2f3f0 6e 67 20 74 6f 20 64 6f 20 74 68 69 73 2e 20 20  ng to do this.  
2f400 42 75 74 20 66 6f 72 0a 2a 2a 20 6d 6f 73 74 20  But for.** most 
2f410 75 73 65 72 73 2c 20 61 20 64 69 72 65 63 74 20  users, a direct 
2f420 63 61 6c 6c 20 74 6f 20 74 68 65 20 75 6e 64 65  call to the unde
2f430 72 6c 79 69 6e 67 20 69 6e 74 65 72 66 61 63 65  rlying interface
2f440 20 69 73 20 70 72 65 66 65 72 61 62 6c 65 0a 2a   is preferable.*
2f450 2a 20 73 6f 20 74 68 65 20 74 68 65 20 72 65 64  * so the the red
2f460 65 66 69 6e 61 62 6c 65 20 49 2f 4f 20 69 6e 74  efinable I/O int
2f470 65 72 66 61 63 65 20 69 73 20 74 75 72 6e 65 64  erface is turned
2f480 20 6f 66 66 20 62 79 20 64 65 66 61 75 6c 74 2e   off by default.
2f490 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
2f4a0 45 5f 45 4e 41 42 4c 45 5f 52 45 44 45 46 5f 49  E_ENABLE_REDEF_I
2f4b0 4f 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 72 65  O../*.** When re
2f4c0 64 65 66 69 6e 61 62 6c 65 20 49 2f 4f 20 69 73  definable I/O is
2f4d0 20 65 6e 61 62 6c 65 64 2c 20 61 20 73 69 6e 67   enabled, a sing
2f4e0 6c 65 20 67 6c 6f 62 61 6c 20 69 6e 73 74 61 6e  le global instan
2f4f0 63 65 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 6c  ce of the.** fol
2f500 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
2f510 20 68 6f 6c 64 73 20 70 6f 69 6e 74 65 72 73 20   holds pointers 
2f520 74 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20  to the routines 
2f530 74 68 61 74 20 53 51 4c 69 74 65 20 0a 2a 2a 20  that SQLite .** 
2f540 75 73 65 73 20 74 6f 20 74 61 6c 6b 20 77 69 74  uses to talk wit
2f550 68 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  h the underlying
2f560 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   operating syste
2f570 6d 2e 20 20 4d 6f 64 69 66 79 20 74 68 69 73 0a  m.  Modify this.
2f580 2a 2a 20 73 74 72 75 63 74 75 72 65 20 28 62 65  ** structure (be
2f590 66 6f 72 65 20 75 73 69 6e 67 20 61 6e 79 20 53  fore using any S
2f5a0 51 4c 69 74 65 20 41 50 49 21 29 20 74 6f 20 61  QLite API!) to a
2f5b0 63 63 6f 6d 6f 64 61 74 65 20 70 65 72 63 75 6c  ccomodate percul
2f5c0 69 61 72 0a 2a 2a 20 6f 70 65 72 61 74 69 6e 67  iar.** operating
2f5d0 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66 61 63   system interfac
2f5e0 65 73 20 6f 72 20 62 65 68 61 76 69 6f 72 73 2e  es or behaviors.
2f5f0 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74  .*/.struct sqlit
2f600 65 33 4f 73 56 74 62 6c 20 7b 0a 20 20 69 6e 74  e3OsVtbl {.  int
2f610 20 28 2a 78 4f 70 65 6e 52 65 61 64 57 72 69 74   (*xOpenReadWrit
2f620 65 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  e)(const char*, 
2f630 4f 73 46 69 6c 65 2a 2a 2c 20 69 6e 74 2a 29 3b  OsFile**, int*);
2f640 0a 20 20 69 6e 74 20 28 2a 78 4f 70 65 6e 45 78  .  int (*xOpenEx
2f650 63 6c 75 73 69 76 65 29 28 63 6f 6e 73 74 20 63  clusive)(const c
2f660 68 61 72 2a 2c 20 4f 73 46 69 6c 65 2a 2a 2c 20  har*, OsFile**, 
2f670 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4f  int);.  int (*xO
2f680 70 65 6e 52 65 61 64 4f 6e 6c 79 29 28 63 6f 6e  penReadOnly)(con
2f690 73 74 20 63 68 61 72 2a 2c 20 4f 73 46 69 6c 65  st char*, OsFile
2f6a0 2a 2a 29 3b 0a 0a 20 20 69 6e 74 20 28 2a 78 44  **);..  int (*xD
2f6b0 65 6c 65 74 65 29 28 63 6f 6e 73 74 20 63 68 61  elete)(const cha
2f6c0 72 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46 69  r*);.  int (*xFi
2f6d0 6c 65 45 78 69 73 74 73 29 28 63 6f 6e 73 74 20  leExists)(const 
2f6e0 63 68 61 72 2a 29 3b 0a 20 20 63 68 61 72 20 2a  char*);.  char *
2f6f0 28 2a 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29  (*xFullPathname)
2f700 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20  (const char*);. 
2f710 20 69 6e 74 20 28 2a 78 49 73 44 69 72 57 72 69   int (*xIsDirWri
2f720 74 61 62 6c 65 29 28 63 68 61 72 2a 29 3b 0a 20  table)(char*);. 
2f730 20 69 6e 74 20 28 2a 78 53 79 6e 63 44 69 72 65   int (*xSyncDire
2f740 63 74 6f 72 79 29 28 63 6f 6e 73 74 20 63 68 61  ctory)(const cha
2f750 72 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 54 65  r*);.  int (*xTe
2f760 6d 70 46 69 6c 65 4e 61 6d 65 29 28 63 68 61 72  mpFileName)(char
2f770 2a 29 3b 0a 0a 20 20 69 6e 74 20 28 2a 78 52 61  *);..  int (*xRa
2f780 6e 64 6f 6d 53 65 65 64 29 28 63 68 61 72 2a 29  ndomSeed)(char*)
2f790 3b 0a 20 20 69 6e 74 20 28 2a 78 53 6c 65 65 70  ;.  int (*xSleep
2f7a0 29 28 69 6e 74 20 6d 73 29 3b 0a 20 20 69 6e 74  )(int ms);.  int
2f7b0 20 28 2a 78 43 75 72 72 65 6e 74 54 69 6d 65 29   (*xCurrentTime)
2f7c0 28 64 6f 75 62 6c 65 2a 29 3b 0a 0a 20 20 76 6f  (double*);..  vo
2f7d0 69 64 20 28 2a 78 45 6e 74 65 72 4d 75 74 65 78  id (*xEnterMutex
2f7e0 29 28 76 6f 69 64 29 3b 0a 20 20 76 6f 69 64 20  )(void);.  void 
2f7f0 28 2a 78 4c 65 61 76 65 4d 75 74 65 78 29 28 76  (*xLeaveMutex)(v
2f800 6f 69 64 29 3b 0a 20 20 69 6e 74 20 28 2a 78 49  oid);.  int (*xI
2f810 6e 4d 75 74 65 78 29 28 69 6e 74 29 3b 0a 20 20  nMutex)(int);.  
2f820 54 68 72 65 61 64 44 61 74 61 20 2a 28 2a 78 54  ThreadData *(*xT
2f830 68 72 65 61 64 53 70 65 63 69 66 69 63 44 61 74  hreadSpecificDat
2f840 61 29 28 69 6e 74 29 3b 0a 0a 20 20 76 6f 69 64  a)(int);..  void
2f850 20 2a 28 2a 78 4d 61 6c 6c 6f 63 29 28 69 6e 74   *(*xMalloc)(int
2f860 29 3b 0a 20 20 76 6f 69 64 20 2a 28 2a 78 52 65  );.  void *(*xRe
2f870 61 6c 6c 6f 63 29 28 76 6f 69 64 20 2a 2c 20 69  alloc)(void *, i
2f880 6e 74 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 46  nt);.  void (*xF
2f890 72 65 65 29 28 76 6f 69 64 20 2a 29 3b 0a 20 20  ree)(void *);.  
2f8a0 69 6e 74 20 28 2a 78 41 6c 6c 6f 63 61 74 69 6f  int (*xAllocatio
2f8b0 6e 53 69 7a 65 29 28 76 6f 69 64 20 2a 29 3b 0a  nSize)(void *);.
2f8c0 0a 20 20 76 6f 69 64 20 2a 28 2a 78 44 6c 6f 70  .  void *(*xDlop
2f8d0 65 6e 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 29  en)(const char*)
2f8e0 3b 0a 20 20 76 6f 69 64 20 2a 28 2a 78 44 6c 73  ;.  void *(*xDls
2f8f0 79 6d 29 28 76 6f 69 64 2a 2c 20 63 6f 6e 73 74  ym)(void*, const
2f900 20 63 68 61 72 2a 29 3b 0a 20 20 69 6e 74 20 28   char*);.  int (
2f910 2a 78 44 6c 63 6c 6f 73 65 29 28 76 6f 69 64 2a  *xDlclose)(void*
2f920 29 3b 0a 7d 3b 0a 0a 2f 2a 20 4d 61 63 72 6f 20  );.};../* Macro 
2f930 75 73 65 64 20 74 6f 20 63 6f 6d 6d 65 6e 74 20  used to comment 
2f940 6f 75 74 20 72 6f 75 74 69 6e 65 73 20 74 68 61  out routines tha
2f950 74 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 73 20  t do not exists 
2f960 77 68 65 6e 20 74 68 65 72 65 20 69 73 0a 2a 2a  when there is.**
2f970 20 6e 6f 20 64 69 73 6b 20 49 2f 4f 20 6f 72 20   no disk I/O or 
2f980 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e  extension loadin
2f990 67 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  g.*/.#ifdef SQLI
2f9a0 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a 23  TE_OMIT_DISKIO.#
2f9b0 20 64 65 66 69 6e 65 20 49 46 5f 44 49 53 4b 49   define IF_DISKI
2f9c0 4f 28 58 29 20 20 30 0a 23 65 6c 73 65 0a 23 20  O(X)  0.#else.# 
2f9d0 64 65 66 69 6e 65 20 49 46 5f 44 49 53 4b 49 4f  define IF_DISKIO
2f9e0 28 58 29 20 20 58 0a 23 65 6e 64 69 66 0a 23 69  (X)  X.#endif.#i
2f9f0 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
2fa00 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a  _LOAD_EXTENSION.
2fa10 23 20 64 65 66 69 6e 65 20 49 46 5f 44 4c 4f 50  # define IF_DLOP
2fa20 45 4e 28 58 29 20 20 30 0a 23 65 6c 73 65 0a 23  EN(X)  0.#else.#
2fa30 20 64 65 66 69 6e 65 20 49 46 5f 44 4c 4f 50 45   define IF_DLOPE
2fa40 4e 28 58 29 20 20 58 0a 23 65 6e 64 69 66 0a 0a  N(X)  X.#endif..
2fa50 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 53 51  .#if defined(_SQ
2fa60 4c 49 54 45 5f 4f 53 5f 43 5f 29 20 7c 7c 20 64  LITE_OS_C_) || d
2fa70 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 41 4d  efined(SQLITE_AM
2fa80 41 4c 47 41 4d 41 54 49 4f 4e 29 0a 20 20 2f 2a  ALGAMATION).  /*
2fa90 0a 20 20 2a 2a 20 54 68 65 20 6f 73 2e 63 20 66  .  ** The os.c f
2faa0 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74  ile implements t
2fab0 68 65 20 67 6c 6f 62 61 6c 20 76 69 72 74 75 61  he global virtua
2fac0 6c 20 66 75 6e 63 74 69 6f 6e 20 74 61 62 6c 65  l function table
2fad0 2e 0a 20 20 2a 2a 20 57 65 20 68 61 76 65 20 74  ..  ** We have t
2fae0 6f 20 70 75 74 20 74 68 69 73 20 66 69 6c 65 20  o put this file 
2faf0 68 65 72 65 20 62 65 63 61 75 73 65 20 74 68 65  here because the
2fb00 20 69 6e 69 74 69 61 6c 69 7a 65 72 73 0a 20 20   initializers.  
2fb10 2a 2a 20 28 65 78 3a 20 73 71 6c 69 74 65 33 4f  ** (ex: sqlite3O
2fb20 73 52 61 6e 64 6f 6d 53 65 65 64 29 20 61 72 65  sRandomSeed) are
2fb30 20 6d 61 63 72 6f 73 20 74 68 61 74 20 61 72 65   macros that are
2fb40 20 61 62 6f 75 74 20 74 6f 20 62 65 0a 20 20 2a   about to be.  *
2fb50 2a 20 72 65 64 65 66 69 6e 65 64 2e 0a 20 20 2a  * redefined..  *
2fb60 2f 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69 74  /.  struct sqlit
2fb70 65 33 4f 73 56 74 62 6c 20 73 71 6c 69 74 65 33  e3OsVtbl sqlite3
2fb80 4f 73 20 3d 20 7b 0a 20 20 20 20 49 46 5f 44 49  Os = {.    IF_DI
2fb90 53 4b 49 4f 28 20 73 71 6c 69 74 65 33 4f 73 4f  SKIO( sqlite3OsO
2fba0 70 65 6e 52 65 61 64 57 72 69 74 65 20 29 2c 0a  penReadWrite ),.
2fbb0 20 20 20 20 49 46 5f 44 49 53 4b 49 4f 28 20 73      IF_DISKIO( s
2fbc0 71 6c 69 74 65 33 4f 73 4f 70 65 6e 45 78 63 6c  qlite3OsOpenExcl
2fbd0 75 73 69 76 65 20 29 2c 0a 20 20 20 20 49 46 5f  usive ),.    IF_
2fbe0 44 49 53 4b 49 4f 28 20 73 71 6c 69 74 65 33 4f  DISKIO( sqlite3O
2fbf0 73 4f 70 65 6e 52 65 61 64 4f 6e 6c 79 20 29 2c  sOpenReadOnly ),
2fc00 0a 20 20 20 20 49 46 5f 44 49 53 4b 49 4f 28 20  .    IF_DISKIO( 
2fc10 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 20  sqlite3OsDelete 
2fc20 29 2c 0a 20 20 20 20 49 46 5f 44 49 53 4b 49 4f  ),.    IF_DISKIO
2fc30 28 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 45  ( sqlite3OsFileE
2fc40 78 69 73 74 73 20 29 2c 0a 20 20 20 20 49 46 5f  xists ),.    IF_
2fc50 44 49 53 4b 49 4f 28 20 73 71 6c 69 74 65 33 4f  DISKIO( sqlite3O
2fc60 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 29 2c  sFullPathname ),
2fc70 0a 20 20 20 20 49 46 5f 44 49 53 4b 49 4f 28 20  .    IF_DISKIO( 
2fc80 73 71 6c 69 74 65 33 4f 73 49 73 44 69 72 57 72  sqlite3OsIsDirWr
2fc90 69 74 61 62 6c 65 20 29 2c 0a 20 20 20 20 49 46  itable ),.    IF
2fca0 5f 44 49 53 4b 49 4f 28 20 73 71 6c 69 74 65 33  _DISKIO( sqlite3
2fcb0 4f 73 53 79 6e 63 44 69 72 65 63 74 6f 72 79 20  OsSyncDirectory 
2fcc0 29 2c 0a 20 20 20 20 49 46 5f 44 49 53 4b 49 4f  ),.    IF_DISKIO
2fcd0 28 20 73 71 6c 69 74 65 33 4f 73 54 65 6d 70 46  ( sqlite3OsTempF
2fce0 69 6c 65 4e 61 6d 65 20 29 2c 0a 20 20 20 20 73  ileName ),.    s
2fcf0 71 6c 69 74 65 33 4f 73 52 61 6e 64 6f 6d 53 65  qlite3OsRandomSe
2fd00 65 64 2c 0a 20 20 20 20 73 71 6c 69 74 65 33 4f  ed,.    sqlite3O
2fd10 73 53 6c 65 65 70 2c 0a 20 20 20 20 73 71 6c 69  sSleep,.    sqli
2fd20 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65  te3OsCurrentTime
2fd30 2c 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 45  ,.    sqlite3OsE
2fd40 6e 74 65 72 4d 75 74 65 78 2c 0a 20 20 20 20 73  nterMutex,.    s
2fd50 71 6c 69 74 65 33 4f 73 4c 65 61 76 65 4d 75 74  qlite3OsLeaveMut
2fd60 65 78 2c 0a 20 20 20 20 73 71 6c 69 74 65 33 4f  ex,.    sqlite3O
2fd70 73 49 6e 4d 75 74 65 78 2c 0a 20 20 20 20 73 71  sInMutex,.    sq
2fd80 6c 69 74 65 33 4f 73 54 68 72 65 61 64 53 70 65  lite3OsThreadSpe
2fd90 63 69 66 69 63 44 61 74 61 2c 0a 20 20 20 20 73  cificData,.    s
2fda0 71 6c 69 74 65 33 4f 73 4d 61 6c 6c 6f 63 2c 0a  qlite3OsMalloc,.
2fdb0 20 20 20 20 73 71 6c 69 74 65 33 4f 73 52 65 61      sqlite3OsRea
2fdc0 6c 6c 6f 63 2c 0a 20 20 20 20 73 71 6c 69 74 65  lloc,.    sqlite
2fdd0 33 4f 73 46 72 65 65 2c 0a 20 20 20 20 73 71 6c  3OsFree,.    sql
2fde0 69 74 65 33 4f 73 41 6c 6c 6f 63 61 74 69 6f 6e  ite3OsAllocation
2fdf0 53 69 7a 65 2c 0a 20 20 20 20 49 46 5f 44 4c 4f  Size,.    IF_DLO
2fe00 50 45 4e 28 20 73 71 6c 69 74 65 33 4f 73 44 6c  PEN( sqlite3OsDl
2fe10 6f 70 65 6e 20 29 2c 0a 20 20 20 20 49 46 5f 44  open ),.    IF_D
2fe20 4c 4f 50 45 4e 28 20 73 71 6c 69 74 65 33 4f 73  LOPEN( sqlite3Os
2fe30 44 6c 73 79 6d 20 29 2c 0a 20 20 20 20 49 46 5f  Dlsym ),.    IF_
2fe40 44 4c 4f 50 45 4e 28 20 73 71 6c 69 74 65 33 4f  DLOPEN( sqlite3O
2fe50 73 44 6c 63 6c 6f 73 65 20 29 2c 0a 20 20 7d 3b  sDlclose ),.  };
2fe60 0a 23 65 6c 73 65 0a 20 20 2f 2a 0a 20 20 2a 2a  .#else.  /*.  **
2fe70 20 46 69 6c 65 73 20 6f 74 68 65 72 20 74 68 61   Files other tha
2fe80 6e 20 6f 73 2e 63 20 6a 75 73 74 20 72 65 66 65  n os.c just refe
2fe90 72 65 6e 63 65 20 74 68 65 20 67 6c 6f 62 61 6c  rence the global
2fea0 20 76 69 72 74 75 61 6c 20 66 75 6e 63 74 69 6f   virtual functio
2feb0 6e 20 74 61 62 6c 65 2e 20 0a 20 20 2a 2f 0a 20  n table. .  */. 
2fec0 20 65 78 74 65 72 6e 20 73 74 72 75 63 74 20 73   extern struct s
2fed0 71 6c 69 74 65 33 4f 73 56 74 62 6c 20 73 71 6c  qlite3OsVtbl sql
2fee0 69 74 65 33 4f 73 3b 0a 23 65 6e 64 69 66 20 2f  ite3Os;.#endif /
2fef0 2a 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 43 5f 20  * _SQLITE_OS_C_ 
2ff00 2a 2f 0a 0a 0a 2f 2a 20 54 68 69 73 20 61 64 64  */.../* This add
2ff10 69 74 69 6f 6e 61 6c 20 41 50 49 20 72 6f 75 74  itional API rout
2ff20 69 6e 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65  ine is available
2ff30 20 77 69 74 68 20 72 65 64 65 66 69 6e 61 62 6c   with redefinabl
2ff40 65 20 49 2f 4f 20 2a 2f 0a 73 74 72 75 63 74 20  e I/O */.struct 
2ff50 73 71 6c 69 74 65 33 4f 73 56 74 62 6c 20 2a 73  sqlite3OsVtbl *s
2ff60 71 6c 69 74 65 33 5f 6f 73 5f 73 77 69 74 63 68  qlite3_os_switch
2ff70 28 76 6f 69 64 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20  (void);.../*.** 
2ff80 52 65 64 65 66 69 6e 65 20 74 68 65 20 4f 53 20  Redefine the OS 
2ff90 69 6e 74 65 72 66 61 63 65 20 74 6f 20 67 6f 20  interface to go 
2ffa0 74 68 72 6f 75 67 68 20 74 68 65 20 76 69 72 74  through the virt
2ffb0 75 61 6c 20 66 75 6e 63 74 69 6f 6e 20 74 61 62  ual function tab
2ffc0 6c 65 0a 2a 2a 20 72 61 74 68 65 72 20 74 68 61  le.** rather tha
2ffd0 6e 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e  n calling routin
2ffe0 65 73 20 64 69 72 65 63 74 6c 79 2e 0a 2a 2f 0a  es directly..*/.
2fff0 23 75 6e 64 65 66 20 73 71 6c 69 74 65 33 4f 73  #undef sqlite3Os
30000 4f 70 65 6e 52 65 61 64 57 72 69 74 65 0a 23 75  OpenReadWrite.#u
30010 6e 64 65 66 20 73 71 6c 69 74 65 33 4f 73 4f 70  ndef sqlite3OsOp
30020 65 6e 45 78 63 6c 75 73 69 76 65 0a 23 75 6e 64  enExclusive.#und
30030 65 66 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e  ef sqlite3OsOpen
30040 52 65 61 64 4f 6e 6c 79 0a 23 75 6e 64 65 66 20  ReadOnly.#undef 
30050 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 0a  sqlite3OsDelete.
30060 23 75 6e 64 65 66 20 73 71 6c 69 74 65 33 4f 73  #undef sqlite3Os
30070 46 69 6c 65 45 78 69 73 74 73 0a 23 75 6e 64 65  FileExists.#unde
30080 66 20 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50  f sqlite3OsFullP
30090 61 74 68 6e 61 6d 65 0a 23 75 6e 64 65 66 20 73  athname.#undef s
300a0 71 6c 69 74 65 33 4f 73 49 73 44 69 72 57 72 69  qlite3OsIsDirWri
300b0 74 61 62 6c 65 0a 23 75 6e 64 65 66 20 73 71 6c  table.#undef sql
300c0 69 74 65 33 4f 73 53 79 6e 63 44 69 72 65 63 74  ite3OsSyncDirect
300d0 6f 72 79 0a 23 75 6e 64 65 66 20 73 71 6c 69 74  ory.#undef sqlit
300e0 65 33 4f 73 54 65 6d 70 46 69 6c 65 4e 61 6d 65  e3OsTempFileName
300f0 0a 23 75 6e 64 65 66 20 73 71 6c 69 74 65 33 4f  .#undef sqlite3O
30100 73 52 61 6e 64 6f 6d 53 65 65 64 0a 23 75 6e 64  sRandomSeed.#und
30110 65 66 20 73 71 6c 69 74 65 33 4f 73 53 6c 65 65  ef sqlite3OsSlee
30120 70 0a 23 75 6e 64 65 66 20 73 71 6c 69 74 65 33  p.#undef sqlite3
30130 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 0a 23 75  OsCurrentTime.#u
30140 6e 64 65 66 20 73 71 6c 69 74 65 33 4f 73 45 6e  ndef sqlite3OsEn
30150 74 65 72 4d 75 74 65 78 0a 23 75 6e 64 65 66 20  terMutex.#undef 
30160 73 71 6c 69 74 65 33 4f 73 4c 65 61 76 65 4d 75  sqlite3OsLeaveMu
30170 74 65 78 0a 23 75 6e 64 65 66 20 73 71 6c 69 74  tex.#undef sqlit
30180 65 33 4f 73 49 6e 4d 75 74 65 78 0a 23 75 6e 64  e3OsInMutex.#und
30190 65 66 20 73 71 6c 69 74 65 33 4f 73 54 68 72 65  ef sqlite3OsThre
301a0 61 64 53 70 65 63 69 66 69 63 44 61 74 61 0a 23  adSpecificData.#
301b0 75 6e 64 65 66 20 73 71 6c 69 74 65 33 4f 73 4d  undef sqlite3OsM
301c0 61 6c 6c 6f 63 0a 23 75 6e 64 65 66 20 73 71 6c  alloc.#undef sql
301d0 69 74 65 33 4f 73 52 65 61 6c 6c 6f 63 0a 23 75  ite3OsRealloc.#u
301e0 6e 64 65 66 20 73 71 6c 69 74 65 33 4f 73 46 72  ndef sqlite3OsFr
301f0 65 65 0a 23 75 6e 64 65 66 20 73 71 6c 69 74 65  ee.#undef sqlite
30200 33 4f 73 41 6c 6c 6f 63 61 74 69 6f 6e 53 69 7a  3OsAllocationSiz
30210 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
30220 33 4f 73 4f 70 65 6e 52 65 61 64 57 72 69 74 65  3OsOpenReadWrite
30230 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 2e        sqlite3Os.
30240 78 4f 70 65 6e 52 65 61 64 57 72 69 74 65 0a 23  xOpenReadWrite.#
30250 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4f 73  define sqlite3Os
30260 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 20 20 20  OpenExclusive   
30270 20 20 20 73 71 6c 69 74 65 33 4f 73 2e 78 4f 70     sqlite3Os.xOp
30280 65 6e 45 78 63 6c 75 73 69 76 65 0a 23 64 65 66  enExclusive.#def
30290 69 6e 65 20 73 71 6c 69 74 65 33 4f 73 4f 70 65  ine sqlite3OsOpe
302a0 6e 52 65 61 64 4f 6e 6c 79 20 20 20 20 20 20 20  nReadOnly       
302b0 73 71 6c 69 74 65 33 4f 73 2e 78 4f 70 65 6e 52  sqlite3Os.xOpenR
302c0 65 61 64 4f 6e 6c 79 0a 23 64 65 66 69 6e 65 20  eadOnly.#define 
302d0 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 20  sqlite3OsDelete 
302e0 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
302f0 74 65 33 4f 73 2e 78 44 65 6c 65 74 65 0a 23 64  te3Os.xDelete.#d
30300 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4f 73 46  efine sqlite3OsF
30310 69 6c 65 45 78 69 73 74 73 20 20 20 20 20 20 20  ileExists       
30320 20 20 73 71 6c 69 74 65 33 4f 73 2e 78 46 69 6c    sqlite3Os.xFil
30330 65 45 78 69 73 74 73 0a 23 64 65 66 69 6e 65 20  eExists.#define 
30340 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 61 74  sqlite3OsFullPat
30350 68 6e 61 6d 65 20 20 20 20 20 20 20 73 71 6c 69  hname       sqli
30360 74 65 33 4f 73 2e 78 46 75 6c 6c 50 61 74 68 6e  te3Os.xFullPathn
30370 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ame.#define sqli
30380 74 65 33 4f 73 49 73 44 69 72 57 72 69 74 61 62  te3OsIsDirWritab
30390 6c 65 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f  le      sqlite3O
303a0 73 2e 78 49 73 44 69 72 57 72 69 74 61 62 6c 65  s.xIsDirWritable
303b0 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
303c0 4f 73 53 79 6e 63 44 69 72 65 63 74 6f 72 79 20  OsSyncDirectory 
303d0 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 2e 78       sqlite3Os.x
303e0 53 79 6e 63 44 69 72 65 63 74 6f 72 79 0a 23 64  SyncDirectory.#d
303f0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4f 73 54  efine sqlite3OsT
30400 65 6d 70 46 69 6c 65 4e 61 6d 65 20 20 20 20 20  empFileName     
30410 20 20 73 71 6c 69 74 65 33 4f 73 2e 78 54 65 6d    sqlite3Os.xTem
30420 70 46 69 6c 65 4e 61 6d 65 0a 23 64 65 66 69 6e  pFileName.#defin
30430 65 20 73 71 6c 69 74 65 33 4f 73 52 61 6e 64 6f  e sqlite3OsRando
30440 6d 53 65 65 64 20 20 20 20 20 20 20 20 20 73 71  mSeed         sq
30450 6c 69 74 65 33 4f 73 2e 78 52 61 6e 64 6f 6d 53  lite3Os.xRandomS
30460 65 65 64 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  eed.#define sqli
30470 74 65 33 4f 73 53 6c 65 65 70 20 20 20 20 20 20  te3OsSleep      
30480 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f          sqlite3O
30490 73 2e 78 53 6c 65 65 70 0a 23 64 65 66 69 6e 65  s.xSleep.#define
304a0 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e   sqlite3OsCurren
304b0 74 54 69 6d 65 20 20 20 20 20 20 20 20 73 71 6c  tTime        sql
304c0 69 74 65 33 4f 73 2e 78 43 75 72 72 65 6e 74 54  ite3Os.xCurrentT
304d0 69 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ime.#define sqli
304e0 74 65 33 4f 73 45 6e 74 65 72 4d 75 74 65 78 20  te3OsEnterMutex 
304f0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f          sqlite3O
30500 73 2e 78 45 6e 74 65 72 4d 75 74 65 78 0a 23 64  s.xEnterMutex.#d
30510 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4f 73 4c  efine sqlite3OsL
30520 65 61 76 65 4d 75 74 65 78 20 20 20 20 20 20 20  eaveMutex       
30530 20 20 73 71 6c 69 74 65 33 4f 73 2e 78 4c 65 61    sqlite3Os.xLea
30540 76 65 4d 75 74 65 78 0a 23 64 65 66 69 6e 65 20  veMutex.#define 
30550 73 71 6c 69 74 65 33 4f 73 49 6e 4d 75 74 65 78  sqlite3OsInMutex
30560 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
30570 74 65 33 4f 73 2e 78 49 6e 4d 75 74 65 78 0a 23  te3Os.xInMutex.#
30580 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4f 73  define sqlite3Os
30590 54 68 72 65 61 64 53 70 65 63 69 66 69 63 44 61  ThreadSpecificDa
305a0 74 61 20 73 71 6c 69 74 65 33 4f 73 2e 78 54 68  ta sqlite3Os.xTh
305b0 72 65 61 64 53 70 65 63 69 66 69 63 44 61 74 61  readSpecificData
305c0 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
305d0 4f 73 4d 61 6c 6c 6f 63 20 20 20 20 20 20 20 20  OsMalloc        
305e0 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 2e 78       sqlite3Os.x
305f0 4d 61 6c 6c 6f 63 0a 23 64 65 66 69 6e 65 20 73  Malloc.#define s
30600 71 6c 69 74 65 33 4f 73 52 65 61 6c 6c 6f 63 20  qlite3OsRealloc 
30610 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
30620 65 33 4f 73 2e 78 52 65 61 6c 6c 6f 63 0a 23 64  e3Os.xRealloc.#d
30630 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4f 73 46  efine sqlite3OsF
30640 72 65 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ree             
30650 20 20 73 71 6c 69 74 65 33 4f 73 2e 78 46 72 65    sqlite3Os.xFre
30660 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
30670 33 4f 73 41 6c 6c 6f 63 61 74 69 6f 6e 53 69 7a  3OsAllocationSiz
30680 65 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 2e  e     sqlite3Os.
30690 78 41 6c 6c 6f 63 61 74 69 6f 6e 53 69 7a 65 0a  xAllocationSize.
306a0 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
306b0 45 5f 45 4e 41 42 4c 45 5f 52 45 44 45 46 5f 49  E_ENABLE_REDEF_I
306c0 4f 20 2a 2f 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  O */..#endif /* 
306d0 5f 53 51 4c 49 54 45 5f 4f 53 5f 48 5f 20 2a 2f  _SQLITE_OS_H_ */
306e0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
306f0 2a 20 45 6e 64 20 6f 66 20 6f 73 2e 68 20 2a 2a  * End of os.h **
30700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30730 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
30740 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
30750 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
30760 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a  n sqliteInt.h **
30770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30780 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 64 61  /../*.** Each da
30790 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 62  tabase file to b
307a0 65 20 61 63 63 65 73 73 65 64 20 62 79 20 74 68  e accessed by th
307b0 65 20 73 79 73 74 65 6d 20 69 73 20 61 6e 20 69  e system is an i
307c0 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68  nstance.** of th
307d0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
307e0 63 74 75 72 65 2e 20 20 54 68 65 72 65 20 61 72  cture.  There ar
307f0 65 20 6e 6f 72 6d 61 6c 6c 79 20 74 77 6f 20 6f  e normally two o
30800 66 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72  f these structur
30810 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73 71 6c  es.** in the sql
30820 69 74 65 2e 61 44 62 5b 5d 20 61 72 72 61 79 2e  ite.aDb[] array.
30830 20 20 61 44 62 5b 30 5d 20 69 73 20 74 68 65 20    aDb[0] is the 
30840 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69  main database fi
30850 6c 65 20 61 6e 64 0a 2a 2a 20 61 44 62 5b 31 5d  le and.** aDb[1]
30860 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65   is the database
30870 20 66 69 6c 65 20 75 73 65 64 20 74 6f 20 68 6f   file used to ho
30880 6c 64 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  ld temporary tab
30890 6c 65 73 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c  les.  Additional
308a0 0a 2a 2a 20 64 61 74 61 62 61 73 65 73 20 6d 61  .** databases ma
308b0 79 20 62 65 20 61 74 74 61 63 68 65 64 2e 0a 2a  y be attached..*
308c0 2f 0a 73 74 72 75 63 74 20 44 62 20 7b 0a 20 20  /.struct Db {.  
308d0 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20  char *zName;    
308e0 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
308f0 74 68 69 73 20 64 61 74 61 62 61 73 65 20 2a 2f  this database */
30900 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b 20 20  .  Btree *pBt;  
30910 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42          /* The B
30920 2a 54 72 65 65 20 73 74 72 75 63 74 75 72 65 20  *Tree structure 
30930 66 6f 72 20 74 68 69 73 20 64 61 74 61 62 61 73  for this databas
30940 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 69  e file */.  u8 i
30950 6e 54 72 61 6e 73 3b 20 20 20 20 20 20 20 20 20  nTrans;         
30960 20 2f 2a 20 30 3a 20 6e 6f 74 20 77 72 69 74 61   /* 0: not writa
30970 62 6c 65 2e 20 20 31 3a 20 54 72 61 6e 73 61 63  ble.  1: Transac
30980 74 69 6f 6e 2e 20 20 32 3a 20 43 68 65 63 6b 70  tion.  2: Checkp
30990 6f 69 6e 74 20 2a 2f 0a 20 20 75 38 20 73 61 66  oint */.  u8 saf
309a0 65 74 79 5f 6c 65 76 65 6c 3b 20 20 20 20 20 2f  ety_level;     /
309b0 2a 20 48 6f 77 20 61 67 67 72 65 73 73 69 76 65  * How aggressive
309c0 20 61 74 20 73 79 6e 63 68 69 6e 67 20 64 61 74   at synching dat
309d0 61 20 74 6f 20 64 69 73 6b 20 2a 2f 0a 20 20 76  a to disk */.  v
309e0 6f 69 64 20 2a 70 41 75 78 3b 20 20 20 20 20 20  oid *pAux;      
309f0 20 20 20 20 20 20 20 20 20 2f 2a 20 41 75 78 69           /* Auxi
30a00 6c 69 61 72 79 20 64 61 74 61 2e 20 20 55 73 75  liary data.  Usu
30a10 61 6c 6c 79 20 4e 55 4c 4c 20 2a 2f 0a 20 20 76  ally NULL */.  v
30a20 6f 69 64 20 28 2a 78 46 72 65 65 41 75 78 29 28  oid (*xFreeAux)(
30a30 76 6f 69 64 2a 29 3b 20 20 2f 2a 20 52 6f 75 74  void*);  /* Rout
30a40 69 6e 65 20 74 6f 20 66 72 65 65 20 70 41 75 78  ine to free pAux
30a50 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53   */.  Schema *pS
30a60 63 68 65 6d 61 3b 20 20 20 20 20 2f 2a 20 50 6f  chema;     /* Po
30a70 69 6e 74 65 72 20 74 6f 20 64 61 74 61 62 61 73  inter to databas
30a80 65 20 73 63 68 65 6d 61 20 28 70 6f 73 73 69 62  e schema (possib
30a90 6c 79 20 73 68 61 72 65 64 29 20 2a 2f 0a 7d 3b  ly shared) */.};
30aa0 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  ../*.** An insta
30ab0 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
30ac0 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 73  wing structure s
30ad0 74 6f 72 65 73 20 61 20 64 61 74 61 62 61 73 65  tores a database
30ae0 20 73 63 68 65 6d 61 2e 0a 2a 2a 0a 2a 2a 20 49   schema..**.** I
30af0 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 76  f there are no v
30b00 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 63 6f  irtual tables co
30b10 6e 66 69 67 75 72 65 64 20 69 6e 20 74 68 69 73  nfigured in this
30b20 20 73 63 68 65 6d 61 2c 20 74 68 65 0a 2a 2a 20   schema, the.** 
30b30 53 63 68 65 6d 61 2e 64 62 20 76 61 72 69 61 62  Schema.db variab
30b40 6c 65 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c  le is set to NUL
30b50 4c 2e 20 41 66 74 65 72 20 74 68 65 20 66 69 72  L. After the fir
30b60 73 74 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  st virtual table
30b70 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 61 64 64  .** has been add
30b80 65 64 2c 20 69 74 20 69 73 20 73 65 74 20 74 6f  ed, it is set to
30b90 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 64 61   point to the da
30ba0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
30bb0 6e 20 0a 2a 2a 20 75 73 65 64 20 74 6f 20 63 72  n .** used to cr
30bc0 65 61 74 65 20 74 68 65 20 63 6f 6e 6e 65 63 74  eate the connect
30bd0 69 6f 6e 2e 20 4f 6e 63 65 20 61 20 76 69 72 74  ion. Once a virt
30be0 75 61 6c 20 74 61 62 6c 65 20 68 61 73 20 62 65  ual table has be
30bf0 65 6e 0a 2a 2a 20 61 64 64 65 64 20 74 6f 20 74  en.** added to t
30c00 68 65 20 53 63 68 65 6d 61 20 73 74 72 75 63 74  he Schema struct
30c10 75 72 65 20 61 6e 64 20 74 68 65 20 53 63 68 65  ure and the Sche
30c20 6d 61 2e 64 62 20 76 61 72 69 61 62 6c 65 20 70  ma.db variable p
30c30 6f 70 75 6c 61 74 65 64 2c 20 0a 2a 2a 20 6f 6e  opulated, .** on
30c40 6c 79 20 74 68 61 74 20 64 61 74 61 62 61 73 65  ly that database
30c50 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 61 79 20   connection may 
30c60 75 73 65 20 74 68 65 20 53 63 68 65 6d 61 20 74  use the Schema t
30c70 6f 20 70 72 65 70 61 72 65 20 0a 2a 2a 20 73 74  o prepare .** st
30c80 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 72  atements..*/.str
30c90 75 63 74 20 53 63 68 65 6d 61 20 7b 0a 20 20 69  uct Schema {.  i
30ca0 6e 74 20 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65  nt schema_cookie
30cb0 3b 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20  ;   /* Database 
30cc0 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 20 6e  schema version n
30cd0 75 6d 62 65 72 20 66 6f 72 20 74 68 69 73 20 66  umber for this f
30ce0 69 6c 65 20 2a 2f 0a 20 20 48 61 73 68 20 74 62  ile */.  Hash tb
30cf0 6c 48 61 73 68 3b 20 20 20 20 20 20 20 20 2f 2a  lHash;        /*
30d00 20 41 6c 6c 20 74 61 62 6c 65 73 20 69 6e 64 65   All tables inde
30d10 78 65 64 20 62 79 20 6e 61 6d 65 20 2a 2f 0a 20  xed by name */. 
30d20 20 48 61 73 68 20 69 64 78 48 61 73 68 3b 20 20   Hash idxHash;  
30d30 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 28 6e 61        /* All (na
30d40 6d 65 64 29 20 69 6e 64 69 63 65 73 20 69 6e 64  med) indices ind
30d50 65 78 65 64 20 62 79 20 6e 61 6d 65 20 2a 2f 0a  exed by name */.
30d60 20 20 48 61 73 68 20 74 72 69 67 48 61 73 68 3b    Hash trigHash;
30d70 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 74 72         /* All tr
30d80 69 67 67 65 72 73 20 69 6e 64 65 78 65 64 20 62  iggers indexed b
30d90 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68  y name */.  Hash
30da0 20 61 46 4b 65 79 3b 20 20 20 20 20 20 20 20 20   aFKey;         
30db0 20 2f 2a 20 46 6f 72 65 69 67 6e 20 6b 65 79 73   /* Foreign keys
30dc0 20 69 6e 64 65 78 65 64 20 62 79 20 74 6f 2d 74   indexed by to-t
30dd0 61 62 6c 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20  able */.  Table 
30de0 2a 70 53 65 71 54 61 62 3b 20 20 20 20 20 20 2f  *pSeqTab;      /
30df0 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 73 65 71  * The sqlite_seq
30e00 75 65 6e 63 65 20 74 61 62 6c 65 20 75 73 65 64  uence table used
30e10 20 62 79 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e   by AUTOINCREMEN
30e20 54 20 2a 2f 0a 20 20 75 38 20 66 69 6c 65 5f 66  T */.  u8 file_f
30e30 6f 72 6d 61 74 3b 20 20 20 20 20 20 2f 2a 20 53  ormat;      /* S
30e40 63 68 65 6d 61 20 66 6f 72 6d 61 74 20 76 65 72  chema format ver
30e50 73 69 6f 6e 20 66 6f 72 20 74 68 69 73 20 66 69  sion for this fi
30e60 6c 65 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20  le */.  u8 enc; 
30e70 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
30e80 54 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 75 73  Text encoding us
30e90 65 64 20 62 79 20 74 68 69 73 20 64 61 74 61 62  ed by this datab
30ea0 61 73 65 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61  ase */.  u16 fla
30eb0 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  gs;           /*
30ec0 20 46 6c 61 67 73 20 61 73 73 6f 63 69 61 74 65   Flags associate
30ed0 64 20 77 69 74 68 20 74 68 69 73 20 73 63 68 65  d with this sche
30ee0 6d 61 20 2a 2f 0a 20 20 69 6e 74 20 63 61 63 68  ma */.  int cach
30ef0 65 5f 73 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20  e_size;      /* 
30f00 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  Number of pages 
30f10 74 6f 20 75 73 65 20 69 6e 20 74 68 65 20 63 61  to use in the ca
30f20 63 68 65 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  che */.#ifndef S
30f30 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
30f40 41 4c 54 41 42 4c 45 0a 20 20 73 71 6c 69 74 65  ALTABLE.  sqlite
30f50 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 2f  3 *db;         /
30f60 2a 20 22 4f 77 6e 65 72 22 20 63 6f 6e 6e 65 63  * "Owner" connec
30f70 74 69 6f 6e 2e 20 53 65 65 20 63 6f 6d 6d 65 6e  tion. See commen
30f80 74 20 61 62 6f 76 65 20 2a 2f 0a 23 65 6e 64 69  t above */.#endi
30f90 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73  f.};../*.** Thes
30fa0 65 20 6d 61 63 72 6f 73 20 63 61 6e 20 62 65 20  e macros can be 
30fb0 75 73 65 64 20 74 6f 20 74 65 73 74 2c 20 73 65  used to test, se
30fc0 74 2c 20 6f 72 20 63 6c 65 61 72 20 62 69 74 73  t, or clear bits
30fd0 20 69 6e 20 74 68 65 20 0a 2a 2a 20 44 62 2e 66   in the .** Db.f
30fe0 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23  lags field..*/.#
30ff0 64 65 66 69 6e 65 20 44 62 48 61 73 50 72 6f 70  define DbHasProp
31000 65 72 74 79 28 44 2c 49 2c 50 29 20 20 20 20 20  erty(D,I,P)     
31010 28 28 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53  (((D)->aDb[I].pS
31020 63 68 65 6d 61 2d 3e 66 6c 61 67 73 26 28 50 29  chema->flags&(P)
31030 29 3d 3d 28 50 29 29 0a 23 64 65 66 69 6e 65 20  )==(P)).#define 
31040 44 62 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79  DbHasAnyProperty
31050 28 44 2c 49 2c 50 29 20 20 28 28 28 44 29 2d 3e  (D,I,P)  (((D)->
31060 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d 61 2d 3e  aDb[I].pSchema->
31070 66 6c 61 67 73 26 28 50 29 29 21 3d 30 29 0a 23  flags&(P))!=0).#
31080 64 65 66 69 6e 65 20 44 62 53 65 74 50 72 6f 70  define DbSetProp
31090 65 72 74 79 28 44 2c 49 2c 50 29 20 20 20 20 20  erty(D,I,P)     
310a0 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68  (D)->aDb[I].pSch
310b0 65 6d 61 2d 3e 66 6c 61 67 73 7c 3d 28 50 29 0a  ema->flags|=(P).
310c0 23 64 65 66 69 6e 65 20 44 62 43 6c 65 61 72 50  #define DbClearP
310d0 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29 20 20  roperty(D,I,P)  
310e0 20 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63   (D)->aDb[I].pSc
310f0 68 65 6d 61 2d 3e 66 6c 61 67 73 26 3d 7e 28 50  hema->flags&=~(P
31100 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64  )../*.** Allowed
31110 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20   values for the 
31120 44 42 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a  DB.flags field..
31130 2a 2a 0a 2a 2a 20 54 68 65 20 44 42 5f 53 63 68  **.** The DB_Sch
31140 65 6d 61 4c 6f 61 64 65 64 20 66 6c 61 67 20 69  emaLoaded flag i
31150 73 20 73 65 74 20 61 66 74 65 72 20 74 68 65 20  s set after the 
31160 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20  database schema 
31170 68 61 73 20 62 65 65 6e 0a 2a 2a 20 72 65 61 64  has been.** read
31180 20 69 6e 74 6f 20 69 6e 74 65 72 6e 61 6c 20 68   into internal h
31190 61 73 68 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a  ash tables..**.*
311a0 2a 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77  * DB_UnresetView
311b0 73 20 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e 65  s means that one
311c0 20 6f 72 20 6d 6f 72 65 20 76 69 65 77 73 20 68   or more views h
311d0 61 76 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  ave column names
311e0 20 74 68 61 74 0a 2a 2a 20 68 61 76 65 20 62 65   that.** have be
311f0 65 6e 20 66 69 6c 6c 65 64 20 6f 75 74 2e 20 20  en filled out.  
31200 49 66 20 74 68 65 20 73 63 68 65 6d 61 20 63 68  If the schema ch
31210 61 6e 67 65 73 2c 20 74 68 65 73 65 20 63 6f 6c  anges, these col
31220 75 6d 6e 20 6e 61 6d 65 73 20 6d 69 67 68 74 0a  umn names might.
31230 2a 2a 20 63 68 61 6e 67 65 73 20 61 6e 64 20 73  ** changes and s
31240 6f 20 74 68 65 20 76 69 65 77 20 77 69 6c 6c 20  o the view will 
31250 6e 65 65 64 20 74 6f 20 62 65 20 72 65 73 65 74  need to be reset
31260 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f  ..*/.#define DB_
31270 53 63 68 65 6d 61 4c 6f 61 64 65 64 20 20 20 20  SchemaLoaded    
31280 30 78 30 30 30 31 20 20 2f 2a 20 54 68 65 20 73  0x0001  /* The s
31290 63 68 65 6d 61 20 68 61 73 20 62 65 65 6e 20 6c  chema has been l
312a0 6f 61 64 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65  oaded */.#define
312b0 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73   DB_UnresetViews
312c0 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a 20 53      0x0002  /* S
312d0 6f 6d 65 20 76 69 65 77 73 20 68 61 76 65 20 64  ome views have d
312e0 65 66 69 6e 65 64 20 63 6f 6c 75 6d 6e 20 6e 61  efined column na
312f0 6d 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 44  mes */.#define D
31300 42 5f 45 6d 70 74 79 20 20 20 20 20 20 20 20 20  B_Empty         
31310 20 20 30 78 30 30 30 34 20 20 2f 2a 20 54 68 65    0x0004  /* The
31320 20 66 69 6c 65 20 69 73 20 65 6d 70 74 79 20 28   file is empty (
31330 6c 65 6e 67 74 68 20 30 20 62 79 74 65 73 29 20  length 0 bytes) 
31340 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20  */.../*.** Each 
31350 64 61 74 61 62 61 73 65 20 69 73 20 61 6e 20 69  database is an i
31360 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
31370 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
31380 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  re..**.** The sq
31390 6c 69 74 65 2e 6c 61 73 74 52 6f 77 69 64 20 72  lite.lastRowid r
313a0 65 63 6f 72 64 73 20 74 68 65 20 6c 61 73 74 20  ecords the last 
313b0 69 6e 73 65 72 74 20 72 6f 77 69 64 20 67 65 6e  insert rowid gen
313c0 65 72 61 74 65 64 20 62 79 20 61 6e 0a 2a 2a 20  erated by an.** 
313d0 69 6e 73 65 72 74 20 73 74 61 74 65 6d 65 6e 74  insert statement
313e0 2e 20 20 49 6e 73 65 72 74 73 20 6f 6e 20 76 69  .  Inserts on vi
313f0 65 77 73 20 64 6f 20 6e 6f 74 20 61 66 66 65 63  ews do not affec
31400 74 20 69 74 73 20 76 61 6c 75 65 2e 20 20 45 61  t its value.  Ea
31410 63 68 0a 2a 2a 20 74 72 69 67 67 65 72 20 68 61  ch.** trigger ha
31420 73 20 69 74 73 20 6f 77 6e 20 63 6f 6e 74 65 78  s its own contex
31430 74 2c 20 73 6f 20 74 68 61 74 20 6c 61 73 74 52  t, so that lastR
31440 6f 77 69 64 20 63 61 6e 20 62 65 20 75 70 64 61  owid can be upda
31450 74 65 64 20 69 6e 73 69 64 65 0a 2a 2a 20 74 72  ted inside.** tr
31460 69 67 67 65 72 73 20 61 73 20 75 73 75 61 6c 2e  iggers as usual.
31470 20 20 54 68 65 20 70 72 65 76 69 6f 75 73 20 76    The previous v
31480 61 6c 75 65 20 77 69 6c 6c 20 62 65 20 72 65 73  alue will be res
31490 74 6f 72 65 64 20 6f 6e 63 65 20 74 68 65 20 74  tored once the t
314a0 72 69 67 67 65 72 0a 2a 2a 20 65 78 69 74 73 2e  rigger.** exits.
314b0 20 20 55 70 6f 6e 20 65 6e 74 65 72 69 6e 67 20    Upon entering 
314c0 61 20 62 65 66 6f 72 65 20 6f 72 20 69 6e 73 74  a before or inst
314d0 65 61 64 20 6f 66 20 74 72 69 67 67 65 72 2c 20  ead of trigger, 
314e0 6c 61 73 74 52 6f 77 69 64 20 69 73 20 6e 6f 0a  lastRowid is no.
314f0 2a 2a 20 6c 6f 6e 67 65 72 20 28 73 69 6e 63 65  ** longer (since
31500 20 61 66 74 65 72 20 76 65 72 73 69 6f 6e 20 32   after version 2
31510 2e 38 2e 31 32 29 20 72 65 73 65 74 20 74 6f 20  .8.12) reset to 
31520 2d 31 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  -1..**.** The sq
31530 6c 69 74 65 2e 6e 43 68 61 6e 67 65 20 64 6f 65  lite.nChange doe
31540 73 20 6e 6f 74 20 63 6f 75 6e 74 20 63 68 61 6e  s not count chan
31550 67 65 73 20 77 69 74 68 69 6e 20 74 72 69 67 67  ges within trigg
31560 65 72 73 20 61 6e 64 20 6b 65 65 70 73 20 6e 6f  ers and keeps no
31570 0a 2a 2a 20 63 6f 6e 74 65 78 74 2e 20 20 49 74  .** context.  It
31580 20 69 73 20 72 65 73 65 74 20 61 74 20 73 74 61   is reset at sta
31590 72 74 20 6f 66 20 73 71 6c 69 74 65 33 5f 65 78  rt of sqlite3_ex
315a0 65 63 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ec..** The sqlit
315b0 65 2e 6c 73 43 68 61 6e 67 65 20 72 65 70 72 65  e.lsChange repre
315c0 73 65 6e 74 73 20 74 68 65 20 6e 75 6d 62 65 72  sents the number
315d0 20 6f 66 20 63 68 61 6e 67 65 73 20 6d 61 64 65   of changes made
315e0 20 62 79 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20   by the last.** 
315f0 69 6e 73 65 72 74 2c 20 75 70 64 61 74 65 2c 20  insert, update, 
31600 6f 72 20 64 65 6c 65 74 65 20 73 74 61 74 65 6d  or delete statem
31610 65 6e 74 2e 20 20 49 74 20 72 65 6d 61 69 6e 73  ent.  It remains
31620 20 63 6f 6e 73 74 61 6e 74 20 74 68 72 6f 75 67   constant throug
31630 68 6f 75 74 20 74 68 65 0a 2a 2a 20 6c 65 6e 67  hout the.** leng
31640 74 68 20 6f 66 20 61 20 73 74 61 74 65 6d 65 6e  th of a statemen
31650 74 20 61 6e 64 20 69 73 20 74 68 65 6e 20 75 70  t and is then up
31660 64 61 74 65 64 20 62 79 20 4f 50 5f 53 65 74 43  dated by OP_SetC
31670 6f 75 6e 74 73 2e 20 20 49 74 20 6b 65 65 70 73  ounts.  It keeps
31680 20 61 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 73 74   a.** context st
31690 61 63 6b 20 6a 75 73 74 20 6c 69 6b 65 20 6c 61  ack just like la
316a0 73 74 52 6f 77 69 64 20 73 6f 20 74 68 61 74 20  stRowid so that 
316b0 74 68 65 20 63 6f 75 6e 74 20 6f 66 20 63 68 61  the count of cha
316c0 6e 67 65 73 0a 2a 2a 20 77 69 74 68 69 6e 20 61  nges.** within a
316d0 20 74 72 69 67 67 65 72 20 69 73 20 6e 6f 74 20   trigger is not 
316e0 73 65 65 6e 20 6f 75 74 73 69 64 65 20 74 68 65  seen outside the
316f0 20 74 72 69 67 67 65 72 2e 20 20 43 68 61 6e 67   trigger.  Chang
31700 65 73 20 74 6f 20 76 69 65 77 73 20 64 6f 20 6e  es to views do n
31710 6f 74 0a 2a 2a 20 61 66 66 65 63 74 20 74 68 65  ot.** affect the
31720 20 76 61 6c 75 65 20 6f 66 20 6c 73 43 68 61 6e   value of lsChan
31730 67 65 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ge..** The sqlit
31740 65 2e 63 73 43 68 61 6e 67 65 20 6b 65 65 70 73  e.csChange keeps
31750 20 74 72 61 63 6b 20 6f 66 20 74 68 65 20 6e 75   track of the nu
31760 6d 62 65 72 20 6f 66 20 63 75 72 72 65 6e 74 20  mber of current 
31770 63 68 61 6e 67 65 73 20 28 73 69 6e 63 65 0a 2a  changes (since.*
31780 2a 20 74 68 65 20 6c 61 73 74 20 73 74 61 74 65  * the last state
31790 6d 65 6e 74 29 20 61 6e 64 20 69 73 20 75 73 65  ment) and is use
317a0 64 20 74 6f 20 75 70 64 61 74 65 20 73 71 6c 69  d to update sqli
317b0 74 65 5f 6c 73 43 68 61 6e 67 65 2e 0a 2a 2a 0a  te_lsChange..**.
317c0 2a 2a 20 54 68 65 20 6d 65 6d 62 65 72 20 76 61  ** The member va
317d0 72 69 61 62 6c 65 73 20 73 71 6c 69 74 65 2e 65  riables sqlite.e
317e0 72 72 43 6f 64 65 2c 20 73 71 6c 69 74 65 2e 7a  rrCode, sqlite.z
317f0 45 72 72 4d 73 67 20 61 6e 64 20 73 71 6c 69 74  ErrMsg and sqlit
31800 65 2e 7a 45 72 72 4d 73 67 31 36 0a 2a 2a 20 73  e.zErrMsg16.** s
31810 74 6f 72 65 20 74 68 65 20 6d 6f 73 74 20 72 65  tore the most re
31820 63 65 6e 74 20 65 72 72 6f 72 20 63 6f 64 65 20  cent error code 
31830 61 6e 64 2c 20 69 66 20 61 70 70 6c 69 63 61 62  and, if applicab
31840 6c 65 2c 20 73 74 72 69 6e 67 2e 20 54 68 65 0a  le, string. The.
31850 2a 2a 20 69 6e 74 65 72 6e 61 6c 20 66 75 6e 63  ** internal func
31860 74 69 6f 6e 20 73 71 6c 69 74 65 33 45 72 72 6f  tion sqlite3Erro
31870 72 28 29 20 69 73 20 75 73 65 64 20 74 6f 20 73  r() is used to s
31880 65 74 20 74 68 65 73 65 20 76 61 72 69 61 62 6c  et these variabl
31890 65 73 0a 2a 2a 20 63 6f 6e 73 69 73 74 65 6e 74  es.** consistent
318a0 6c 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71  ly..*/.struct sq
318b0 6c 69 74 65 33 20 7b 0a 20 20 69 6e 74 20 6e 44  lite3 {.  int nD
318c0 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
318d0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
318e0 72 20 6f 66 20 62 61 63 6b 65 6e 64 73 20 63 75  r of backends cu
318f0 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 20 2a  rrently in use *
31900 2f 0a 20 20 44 62 20 2a 61 44 62 3b 20 20 20 20  /.  Db *aDb;    
31910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31920 20 20 2f 2a 20 41 6c 6c 20 62 61 63 6b 65 6e 64    /* All backend
31930 73 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73  s */.  int flags
31940 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
31950 20 20 20 20 20 2f 2a 20 4d 69 73 63 65 6c 6c 61       /* Miscella
31960 6e 6f 75 73 20 66 6c 61 67 73 2e 20 53 65 65 20  nous flags. See 
31970 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 65  below */.  int e
31980 72 72 43 6f 64 65 3b 20 20 20 20 20 20 20 20 20  rrCode;         
31990 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 73 74           /* Most
319a0 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20 63 6f   recent error co
319b0 64 65 20 28 53 51 4c 49 54 45 5f 2a 29 20 2a 2f  de (SQLITE_*) */
319c0 0a 20 20 69 6e 74 20 65 72 72 4d 61 73 6b 3b 20  .  int errMask; 
319d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
319e0 20 2f 2a 20 26 20 72 65 73 75 6c 74 20 63 6f 64   /* & result cod
319f0 65 73 20 77 69 74 68 20 74 68 69 73 20 62 65 66  es with this bef
31a00 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 20 2a 2f  ore returning */
31a10 0a 20 20 75 38 20 61 75 74 6f 43 6f 6d 6d 69 74  .  u8 autoCommit
31a20 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
31a30 20 2f 2a 20 54 68 65 20 61 75 74 6f 2d 63 6f 6d   /* The auto-com
31a40 6d 69 74 20 66 6c 61 67 2e 20 2a 2f 0a 20 20 75  mit flag. */.  u
31a50 38 20 74 65 6d 70 5f 73 74 6f 72 65 3b 20 20 20  8 temp_store;   
31a60 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
31a70 31 3a 20 66 69 6c 65 20 32 3a 20 6d 65 6d 6f 72  1: file 2: memor
31a80 79 20 30 3a 20 64 65 66 61 75 6c 74 20 2a 2f 0a  y 0: default */.
31a90 20 20 69 6e 74 20 6e 54 61 62 6c 65 3b 20 20 20    int nTable;   
31aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31ab0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 61 62  /* Number of tab
31ac0 6c 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62  les in the datab
31ad0 61 73 65 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71  ase */.  CollSeq
31ae0 20 2a 70 44 66 6c 74 43 6f 6c 6c 3b 20 20 20 20   *pDfltColl;    
31af0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 65         /* The de
31b00 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6e 67 20  fault collating 
31b10 73 65 71 75 65 6e 63 65 20 28 42 49 4e 41 52 59  sequence (BINARY
31b20 29 20 2a 2f 0a 20 20 69 36 34 20 6c 61 73 74 52  ) */.  i64 lastR
31b30 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20 20 20  owid;           
31b40 20 20 20 20 20 2f 2a 20 52 4f 57 49 44 20 6f 66       /* ROWID of
31b50 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 69 6e 73   most recent ins
31b60 65 72 74 20 28 73 65 65 20 61 62 6f 76 65 29 20  ert (see above) 
31b70 2a 2f 0a 20 20 69 36 34 20 70 72 69 6f 72 4e 65  */.  i64 priorNe
31b80 77 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20  wRowid;         
31b90 20 20 20 2f 2a 20 4c 61 73 74 20 72 61 6e 64 6f     /* Last rando
31ba0 6d 6c 79 20 67 65 6e 65 72 61 74 65 64 20 52 4f  mly generated RO
31bb0 57 49 44 20 2a 2f 0a 20 20 69 6e 74 20 6d 61 67  WID */.  int mag
31bc0 69 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ic;             
31bd0 20 20 20 20 20 20 20 2f 2a 20 4d 61 67 69 63 20         /* Magic 
31be0 6e 75 6d 62 65 72 20 66 6f 72 20 64 65 74 65 63  number for detec
31bf0 74 20 6c 69 62 72 61 72 79 20 6d 69 73 75 73 65  t library misuse
31c00 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67   */.  int nChang
31c10 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
31c20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 74      /* Value ret
31c30 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33  urned by sqlite3
31c40 5f 63 68 61 6e 67 65 73 28 29 20 2a 2f 0a 20 20  _changes() */.  
31c50 69 6e 74 20 6e 54 6f 74 61 6c 43 68 61 6e 67 65  int nTotalChange
31c60 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
31c70 20 56 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   Value returned 
31c80 62 79 20 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c  by sqlite3_total
31c90 5f 63 68 61 6e 67 65 73 28 29 20 2a 2f 0a 20 20  _changes() */.  
31ca0 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 49 6e  struct sqlite3In
31cb0 69 74 49 6e 66 6f 20 7b 20 20 20 20 20 20 2f 2a  itInfo {      /*
31cc0 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65   Information use
31cd0 64 20 64 75 72 69 6e 67 20 69 6e 69 74 69 61 6c  d during initial
31ce0 69 7a 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 69  ization */.    i
31cf0 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20  nt iDb;         
31d00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68             /* Wh
31d10 65 6e 20 62 61 63 6b 20 69 73 20 62 65 69 6e 67  en back is being
31d20 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a   initialized */.
31d30 20 20 20 20 69 6e 74 20 6e 65 77 54 6e 75 6d 3b      int newTnum;
31d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31d50 2f 2a 20 52 6f 6f 74 70 61 67 65 20 6f 66 20 74  /* Rootpage of t
31d60 61 62 6c 65 20 62 65 69 6e 67 20 69 6e 69 74 69  able being initi
31d70 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 20 20 75 38  alized */.    u8
31d80 20 62 75 73 79 3b 20 20 20 20 20 20 20 20 20 20   busy;          
31d90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 52 55            /* TRU
31da0 45 20 69 66 20 63 75 72 72 65 6e 74 6c 79 20 69  E if currently i
31db0 6e 69 74 69 61 6c 69 7a 69 6e 67 20 2a 2f 0a 20  nitializing */. 
31dc0 20 7d 20 69 6e 69 74 3b 0a 20 20 69 6e 74 20 6e   } init;.  int n
31dd0 45 78 74 65 6e 73 69 6f 6e 3b 20 20 20 20 20 20  Extension;      
31de0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
31df0 65 72 20 6f 66 20 6c 6f 61 64 65 64 20 65 78 74  er of loaded ext
31e00 65 6e 73 69 6f 6e 73 20 2a 2f 0a 20 20 76 6f 69  ensions */.  voi
31e10 64 20 2a 2a 61 45 78 74 65 6e 73 69 6f 6e 3b 20  d **aExtension; 
31e20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72             /* Ar
31e30 72 61 79 20 6f 66 20 73 68 61 72 65 64 20 6c 69  ray of shared li
31e40 62 72 61 72 61 79 20 68 61 6e 64 6c 65 73 20 2a  braray handles *
31e50 2f 0a 20 20 73 74 72 75 63 74 20 56 64 62 65 20  /.  struct Vdbe 
31e60 2a 70 56 64 62 65 3b 20 20 20 20 20 20 20 20 20  *pVdbe;         
31e70 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 63 74    /* List of act
31e80 69 76 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68  ive virtual mach
31e90 69 6e 65 73 20 2a 2f 0a 20 20 69 6e 74 20 61 63  ines */.  int ac
31ea0 74 69 76 65 56 64 62 65 43 6e 74 3b 20 20 20 20  tiveVdbeCnt;    
31eb0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
31ec0 72 20 6f 66 20 76 64 62 65 73 20 63 75 72 72 65  r of vdbes curre
31ed0 6e 74 6c 79 20 65 78 65 63 75 74 69 6e 67 20 2a  ntly executing *
31ee0 2f 0a 20 20 76 6f 69 64 20 28 2a 78 54 72 61 63  /.  void (*xTrac
31ef0 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63  e)(void*,const c
31f00 68 61 72 2a 29 3b 20 20 20 20 20 20 20 20 2f 2a  har*);        /*
31f10 20 54 72 61 63 65 20 66 75 6e 63 74 69 6f 6e 20   Trace function 
31f20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 54 72 61 63  */.  void *pTrac
31f30 65 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20  eArg;           
31f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
31f50 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  * Argument to th
31f60 65 20 74 72 61 63 65 20 66 75 6e 63 74 69 6f 6e  e trace function
31f70 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 50 72   */.  void (*xPr
31f80 6f 66 69 6c 65 29 28 76 6f 69 64 2a 2c 63 6f 6e  ofile)(void*,con
31f90 73 74 20 63 68 61 72 2a 2c 75 36 34 29 3b 20 20  st char*,u64);  
31fa0 2f 2a 20 50 72 6f 66 69 6c 69 6e 67 20 66 75 6e  /* Profiling fun
31fb0 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20  ction */.  void 
31fc0 2a 70 50 72 6f 66 69 6c 65 41 72 67 3b 20 20 20  *pProfileArg;   
31fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31fe0 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74       /* Argument
31ff0 20 74 6f 20 70 72 6f 66 69 6c 65 20 66 75 6e 63   to profile func
32000 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  tion */.  void *
32010 70 43 6f 6d 6d 69 74 41 72 67 3b 20 20 20 20 20  pCommitArg;     
32020 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
32030 72 67 75 6d 65 6e 74 20 74 6f 20 78 43 6f 6d 6d  rgument to xComm
32040 69 74 43 61 6c 6c 62 61 63 6b 28 29 20 2a 2f 20  itCallback() */ 
32050 20 20 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6d 6d    .  int (*xComm
32060 69 74 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64  itCallback)(void
32070 2a 29 3b 20 20 20 20 2f 2a 20 49 6e 76 6f 6b 65  *);    /* Invoke
32080 64 20 61 74 20 65 76 65 72 79 20 63 6f 6d 6d 69  d at every commi
32090 74 2e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 52  t. */.  void *pR
320a0 6f 6c 6c 62 61 63 6b 41 72 67 3b 20 20 20 20 20  ollbackArg;     
320b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67            /* Arg
320c0 75 6d 65 6e 74 20 74 6f 20 78 52 6f 6c 6c 62 61  ument to xRollba
320d0 63 6b 43 61 6c 6c 62 61 63 6b 28 29 20 2a 2f 20  ckCallback() */ 
320e0 20 20 0a 20 20 76 6f 69 64 20 28 2a 78 52 6f 6c    .  void (*xRol
320f0 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b 29 28 76  lbackCallback)(v
32100 6f 69 64 2a 29 3b 20 2f 2a 20 49 6e 76 6f 6b 65  oid*); /* Invoke
32110 64 20 61 74 20 65 76 65 72 79 20 63 6f 6d 6d 69  d at every commi
32120 74 2e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 55  t. */.  void *pU
32130 70 64 61 74 65 41 72 67 3b 0a 20 20 76 6f 69 64  pdateArg;.  void
32140 20 28 2a 78 55 70 64 61 74 65 43 61 6c 6c 62 61   (*xUpdateCallba
32150 63 6b 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 20 63  ck)(void*,int, c
32160 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74  onst char*,const
32170 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 5f 69 6e   char*,sqlite_in
32180 74 36 34 29 3b 0a 20 20 76 6f 69 64 28 2a 78 43  t64);.  void(*xC
32190 6f 6c 6c 4e 65 65 64 65 64 29 28 76 6f 69 64 2a  ollNeeded)(void*
321a0 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54  ,sqlite3*,int eT
321b0 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 63 68 61  extRep,const cha
321c0 72 2a 29 3b 0a 20 20 76 6f 69 64 28 2a 78 43 6f  r*);.  void(*xCo
321d0 6c 6c 4e 65 65 64 65 64 31 36 29 28 76 6f 69 64  llNeeded16)(void
321e0 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65  *,sqlite3*,int e
321f0 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 76 6f  TextRep,const vo
32200 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 70 43  id*);.  void *pC
32210 6f 6c 6c 4e 65 65 64 65 64 41 72 67 3b 0a 20 20  ollNeededArg;.  
32220 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
32230 45 72 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  Err;          /*
32240 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72   Most recent err
32250 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f 0a 20 20  or message */.  
32260 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20  char *zErrMsg;  
32270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
32280 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72   Most recent err
32290 6f 72 20 6d 65 73 73 61 67 65 20 28 55 54 46 2d  or message (UTF-
322a0 38 20 65 6e 63 6f 64 65 64 29 20 2a 2f 0a 20 20  8 encoded) */.  
322b0 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 31 36 3b  char *zErrMsg16;
322c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
322d0 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72   Most recent err
322e0 6f 72 20 6d 65 73 73 61 67 65 20 28 55 54 46 2d  or message (UTF-
322f0 31 36 20 65 6e 63 6f 64 65 64 29 20 2a 2f 0a 20  16 encoded) */. 
32300 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 69 6e 74   union {.    int
32310 20 69 73 49 6e 74 65 72 72 75 70 74 65 64 3b 20   isInterrupted; 
32320 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
32330 20 69 66 20 73 71 6c 69 74 65 33 5f 69 6e 74 65   if sqlite3_inte
32340 72 72 75 70 74 20 68 61 73 20 62 65 65 6e 20 63  rrupt has been c
32350 61 6c 6c 65 64 20 2a 2f 0a 20 20 20 20 64 6f 75  alled */.    dou
32360 62 6c 65 20 6e 6f 74 55 73 65 64 31 3b 20 20 20  ble notUsed1;   
32370 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63           /* Spac
32380 65 72 20 2a 2f 0a 20 20 7d 20 75 31 3b 0a 23 69  er */.  } u1;.#i
32390 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
323a0 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a  T_AUTHORIZATION.
323b0 20 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76    int (*xAuth)(v
323c0 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63  oid*,int,const c
323d0 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
323e0 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
323f0 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 20 20 20  st char*);.     
32400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32410 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 63             /* Ac
32420 63 65 73 73 20 61 75 74 68 6f 72 69 7a 61 74 69  cess authorizati
32430 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  on function */. 
32440 20 76 6f 69 64 20 2a 70 41 75 74 68 41 72 67 3b   void *pAuthArg;
32450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
32460 2a 20 31 73 74 20 61 72 67 75 6d 65 6e 74 20 74  * 1st argument t
32470 6f 20 74 68 65 20 61 63 63 65 73 73 20 61 75 74  o the access aut
32480 68 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 23 65  h function */.#e
32490 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c  ndif.#ifndef SQL
324a0 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53  ITE_OMIT_PROGRES
324b0 53 5f 43 41 4c 4c 42 41 43 4b 0a 20 20 69 6e 74  S_CALLBACK.  int
324c0 20 28 2a 78 50 72 6f 67 72 65 73 73 29 28 76 6f   (*xProgress)(vo
324d0 69 64 20 2a 29 3b 20 20 20 20 20 2f 2a 20 54 68  id *);     /* Th
324e0 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62  e progress callb
324f0 61 63 6b 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  ack */.  void *p
32500 50 72 6f 67 72 65 73 73 41 72 67 3b 20 20 20 20  ProgressArg;    
32510 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65         /* Argume
32520 6e 74 20 74 6f 20 74 68 65 20 70 72 6f 67 72 65  nt to the progre
32530 73 73 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20  ss callback */. 
32540 20 69 6e 74 20 6e 50 72 6f 67 72 65 73 73 4f 70   int nProgressOp
32550 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  s;             /
32560 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 70 63 6f  * Number of opco
32570 64 65 73 20 66 6f 72 20 70 72 6f 67 72 65 73 73  des for progress
32580 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 23 65 6e   callback */.#en
32590 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  dif.#ifndef SQLI
325a0 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
325b0 41 42 4c 45 0a 20 20 48 61 73 68 20 61 4d 6f 64  ABLE.  Hash aMod
325c0 75 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ule;            
325d0 20 20 20 20 20 2f 2a 20 70 6f 70 75 6c 61 74 65       /* populate
325e0 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 72 65  d by sqlite3_cre
325f0 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a  ate_module() */.
32600 20 20 54 61 62 6c 65 20 2a 70 56 54 61 62 3b 20    Table *pVTab; 
32610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32620 2f 2a 20 76 74 61 62 20 77 69 74 68 20 61 63 74  /* vtab with act
32630 69 76 65 20 43 6f 6e 6e 65 63 74 2f 43 72 65 61  ive Connect/Crea
32640 74 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73  te method */.  s
32650 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 61 56  qlite3_vtab **aV
32660 54 72 61 6e 73 3b 20 20 20 20 20 20 20 2f 2a 20  Trans;       /* 
32670 56 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 77  Virtual tables w
32680 69 74 68 20 6f 70 65 6e 20 74 72 61 6e 73 61 63  ith open transac
32690 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  tions */.  int n
326a0 56 54 72 61 6e 73 3b 20 20 20 20 20 20 20 20 20  VTrans;         
326b0 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f           /* Allo
326c0 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20 61 56  cated size of aV
326d0 54 72 61 6e 73 20 2a 2f 0a 23 65 6e 64 69 66 0a  Trans */.#endif.
326e0 20 20 48 61 73 68 20 61 46 75 6e 63 3b 20 20 20    Hash aFunc;   
326f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32700 2f 2a 20 41 6c 6c 20 66 75 6e 63 74 69 6f 6e 73  /* All functions
32710 20 74 68 61 74 20 63 61 6e 20 62 65 20 69 6e 20   that can be in 
32720 53 51 4c 20 65 78 70 72 73 20 2a 2f 0a 20 20 48  SQL exprs */.  H
32730 61 73 68 20 61 43 6f 6c 6c 53 65 71 3b 20 20 20  ash aCollSeq;   
32740 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
32750 41 6c 6c 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  All collating se
32760 71 75 65 6e 63 65 73 20 2a 2f 0a 20 20 42 75 73  quences */.  Bus
32770 79 48 61 6e 64 6c 65 72 20 62 75 73 79 48 61 6e  yHandler busyHan
32780 64 6c 65 72 3b 20 20 20 20 20 20 2f 2a 20 42 75  dler;      /* Bu
32790 73 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20  sy callback */. 
327a0 20 69 6e 74 20 62 75 73 79 54 69 6d 65 6f 75 74   int busyTimeout
327b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
327c0 2a 20 42 75 73 79 20 68 61 6e 64 6c 65 72 20 74  * Busy handler t
327d0 69 6d 65 6f 75 74 2c 20 69 6e 20 6d 73 65 63 20  imeout, in msec 
327e0 2a 2f 0a 20 20 44 62 20 61 44 62 53 74 61 74 69  */.  Db aDbStati
327f0 63 5b 32 5d 3b 20 20 20 20 20 20 20 20 20 20 20  c[2];           
32800 20 20 20 2f 2a 20 53 74 61 74 69 63 20 73 70 61     /* Static spa
32810 63 65 20 66 6f 72 20 74 68 65 20 32 20 64 65 66  ce for the 2 def
32820 61 75 6c 74 20 62 61 63 6b 65 6e 64 73 20 2a 2f  ault backends */
32830 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53  .#ifdef SQLITE_S
32840 53 45 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  SE.  sqlite3_stm
32850 74 20 2a 70 46 65 74 63 68 3b 20 20 20 20 20 20  t *pFetch;      
32860 20 20 20 2f 2a 20 55 73 65 64 20 62 79 20 53 53     /* Used by SS
32870 45 20 74 6f 20 66 65 74 63 68 20 73 74 6f 72 65  E to fetch store
32880 64 20 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a  d statements */.
32890 23 65 6e 64 69 66 0a 20 20 75 38 20 64 66 6c 74  #endif.  u8 dflt
328a0 4c 6f 63 6b 4d 6f 64 65 3b 20 20 20 20 20 20 20  LockMode;       
328b0 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c         /* Defaul
328c0 74 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 66  t locking-mode f
328d0 6f 72 20 61 74 74 61 63 68 65 64 20 64 62 73 20  or attached dbs 
328e0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d  */.};../*.** A m
328f0 61 63 72 6f 20 74 6f 20 64 69 73 63 6f 76 65 72  acro to discover
32900 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66   the encoding of
32910 20 61 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a   a database..*/.
32920 23 64 65 66 69 6e 65 20 45 4e 43 28 64 62 29 20  #define ENC(db) 
32930 28 28 64 62 29 2d 3e 61 44 62 5b 30 5d 2e 70 53  ((db)->aDb[0].pS
32940 63 68 65 6d 61 2d 3e 65 6e 63 29 0a 0a 2f 2a 0a  chema->enc)../*.
32950 2a 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c 75  ** Possible valu
32960 65 73 20 66 6f 72 20 74 68 65 20 73 71 6c 69 74  es for the sqlit
32970 65 2e 66 6c 61 67 73 20 61 6e 64 20 6f 72 20 44  e.flags and or D
32980 62 2e 66 6c 61 67 73 20 66 69 65 6c 64 73 2e 0a  b.flags fields..
32990 2a 2a 0a 2a 2a 20 4f 6e 20 73 71 6c 69 74 65 2e  **.** On sqlite.
329a0 66 6c 61 67 73 2c 20 74 68 65 20 53 51 4c 49 54  flags, the SQLIT
329b0 45 5f 49 6e 54 72 61 6e 73 20 76 61 6c 75 65 20  E_InTrans value 
329c0 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 68 61  means that we ha
329d0 76 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 20 61  ve.** executed a
329e0 20 42 45 47 49 4e 2e 20 20 4f 6e 20 44 62 2e 66   BEGIN.  On Db.f
329f0 6c 61 67 73 2c 20 53 51 4c 49 54 45 5f 49 6e 54  lags, SQLITE_InT
32a00 72 61 6e 73 20 6d 65 61 6e 73 20 61 20 73 74 61  rans means a sta
32a10 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61  tement.** transa
32a20 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20  ction is active 
32a30 6f 6e 20 74 68 61 74 20 70 61 72 74 69 63 75 6c  on that particul
32a40 61 72 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ar database file
32a50 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ..*/.#define SQL
32a60 49 54 45 5f 56 64 62 65 54 72 61 63 65 20 20 20  ITE_VdbeTrace   
32a70 20 20 20 30 78 30 30 30 30 30 30 30 31 20 20 2f     0x00000001  /
32a80 2a 20 54 72 75 65 20 74 6f 20 74 72 61 63 65 20  * True to trace 
32a90 56 44 42 45 20 65 78 65 63 75 74 69 6f 6e 20 2a  VDBE execution *
32aa0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
32ab0 5f 49 6e 54 72 61 6e 73 20 20 20 20 20 20 20 20  _InTrans        
32ac0 30 78 30 30 30 30 30 30 30 38 20 20 2f 2a 20 54  0x00000008  /* T
32ad0 72 75 65 20 69 66 20 69 6e 20 61 20 74 72 61 6e  rue if in a tran
32ae0 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69  saction */.#defi
32af0 6e 65 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e  ne SQLITE_Intern
32b00 43 68 61 6e 67 65 73 20 20 30 78 30 30 30 30 30  Changes  0x00000
32b10 30 31 30 20 20 2f 2a 20 55 6e 63 6f 6d 6d 69 74  010  /* Uncommit
32b20 74 65 64 20 48 61 73 68 20 74 61 62 6c 65 20 63  ted Hash table c
32b30 68 61 6e 67 65 73 20 2a 2f 0a 23 64 65 66 69 6e  hanges */.#defin
32b40 65 20 53 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c  e SQLITE_FullCol
32b50 4e 61 6d 65 73 20 20 20 30 78 30 30 30 30 30 30  Names   0x000000
32b60 32 30 20 20 2f 2a 20 53 68 6f 77 20 66 75 6c 6c  20  /* Show full
32b70 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f 6e   column names on
32b80 20 53 45 4c 45 43 54 20 2a 2f 0a 23 64 65 66 69   SELECT */.#defi
32b90 6e 65 20 53 51 4c 49 54 45 5f 53 68 6f 72 74 43  ne SQLITE_ShortC
32ba0 6f 6c 4e 61 6d 65 73 20 20 30 78 30 30 30 30 30  olNames  0x00000
32bb0 30 34 30 20 20 2f 2a 20 53 68 6f 77 20 73 68 6f  040  /* Show sho
32bc0 72 74 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 73  rt columns names
32bd0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
32be0 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 20 20 20  TE_CountRows    
32bf0 20 20 30 78 30 30 30 30 30 30 38 30 20 20 2f 2a    0x00000080  /*
32c00 20 43 6f 75 6e 74 20 72 6f 77 73 20 63 68 61 6e   Count rows chan
32c10 67 65 64 20 62 79 20 49 4e 53 45 52 54 2c 20 2a  ged by INSERT, *
32c20 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /.              
32c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32c40 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20              /*  
32c50 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 50 44 41   DELETE, or UPDA
32c60 54 45 20 61 6e 64 20 72 65 74 75 72 6e 20 2a 2f  TE and return */
32c70 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
32c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32c90 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20             /*   
32ca0 74 68 65 20 63 6f 75 6e 74 20 75 73 69 6e 67 20  the count using 
32cb0 61 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 23  a callback. */.#
32cc0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 75  define SQLITE_Nu
32cd0 6c 6c 43 61 6c 6c 62 61 63 6b 20 20 20 30 78 30  llCallback   0x0
32ce0 30 30 30 30 31 30 30 20 20 2f 2a 20 49 6e 76 6f  0000100  /* Invo
32cf0 6b 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  ke the callback 
32d00 6f 6e 63 65 20 69 66 20 74 68 65 20 2a 2f 0a 20  once if the */. 
32d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32d30 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 72 65           /*   re
32d40 73 75 6c 74 20 73 65 74 20 69 73 20 65 6d 70 74  sult set is empt
32d50 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  y */.#define SQL
32d60 49 54 45 5f 53 71 6c 54 72 61 63 65 20 20 20 20  ITE_SqlTrace    
32d70 20 20 20 30 78 30 30 30 30 30 32 30 30 20 20 2f     0x00000200  /
32d80 2a 20 44 65 62 75 67 20 70 72 69 6e 74 20 53 51  * Debug print SQ
32d90 4c 20 61 73 20 69 74 20 65 78 65 63 75 74 65 73  L as it executes
32da0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
32db0 54 45 5f 56 64 62 65 4c 69 73 74 69 6e 67 20 20  TE_VdbeListing  
32dc0 20 20 30 78 30 30 30 30 30 34 30 30 20 20 2f 2a    0x00000400  /*
32dd0 20 44 65 62 75 67 20 6c 69 73 74 69 6e 67 73 20   Debug listings 
32de0 6f 66 20 56 44 42 45 20 70 72 6f 67 72 61 6d 73  of VDBE programs
32df0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
32e00 54 45 5f 57 72 69 74 65 53 63 68 65 6d 61 20 20  TE_WriteSchema  
32e10 20 20 30 78 30 30 30 30 30 38 30 30 20 20 2f 2a    0x00000800  /*
32e20 20 4f 4b 20 74 6f 20 75 70 64 61 74 65 20 53 51   OK to update SQ
32e30 4c 49 54 45 5f 4d 41 53 54 45 52 20 2a 2f 0a 23  LITE_MASTER */.#
32e40 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 6f  define SQLITE_No
32e50 52 65 61 64 6c 6f 63 6b 20 20 20 20 20 30 78 30  Readlock     0x0
32e60 30 30 30 31 30 30 30 20 20 2f 2a 20 52 65 61 64  0001000  /* Read
32e70 6c 6f 63 6b 73 20 61 72 65 20 6f 6d 69 74 74 65  locks are omitte
32e80 64 20 77 68 65 6e 20 0a 20 20 20 20 20 20 20 20  d when .        
32e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32eb0 20 20 2a 2a 20 61 63 63 65 73 73 69 6e 67 20 72    ** accessing r
32ec0 65 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73  ead-only databas
32ed0 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  es */.#define SQ
32ee0 4c 49 54 45 5f 49 67 6e 6f 72 65 43 68 65 63 6b  LITE_IgnoreCheck
32ef0 73 20 20 20 30 78 30 30 30 30 32 30 30 30 20 20  s   0x00002000  
32f00 2f 2a 20 44 6f 20 6e 6f 74 20 65 6e 66 6f 72 63  /* Do not enforc
32f10 65 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 61 69  e check constrai
32f20 6e 74 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  nts */.#define S
32f30 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d  QLITE_ReadUncomm
32f40 69 74 74 65 64 20 30 78 30 30 30 30 34 30 30 30  itted 0x00004000
32f50 20 2f 2a 20 46 6f 72 20 73 68 61 72 65 64 2d 63   /* For shared-c
32f60 61 63 68 65 20 6d 6f 64 65 20 2a 2f 0a 23 64 65  ache mode */.#de
32f70 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 65 67 61  fine SQLITE_Lega
32f80 63 79 46 69 6c 65 46 6d 74 20 20 30 78 30 30 30  cyFileFmt  0x000
32f90 30 38 30 30 30 20 20 2f 2a 20 43 72 65 61 74 65  08000  /* Create
32fa0 20 6e 65 77 20 64 61 74 61 62 61 73 65 73 20 69   new databases i
32fb0 6e 20 66 6f 72 6d 61 74 20 31 20 2a 2f 0a 23 64  n format 1 */.#d
32fc0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 75 6c  efine SQLITE_Ful
32fd0 6c 46 53 79 6e 63 20 20 20 20 20 20 30 78 30 30  lFSync      0x00
32fe0 30 31 30 30 30 30 20 20 2f 2a 20 55 73 65 20 66  010000  /* Use f
32ff0 75 6c 6c 20 66 73 79 6e 63 20 6f 6e 20 74 68 65  ull fsync on the
33000 20 62 61 63 6b 65 6e 64 20 2a 2f 0a 23 64 65 66   backend */.#def
33010 69 6e 65 20 53 51 4c 49 54 45 5f 4c 6f 61 64 45  ine SQLITE_LoadE
33020 78 74 65 6e 73 69 6f 6e 20 20 30 78 30 30 30 32  xtension  0x0002
33030 30 30 30 30 20 20 2f 2a 20 45 6e 61 62 6c 65 20  0000  /* Enable 
33040 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 20 2a  load_extension *
33050 2f 0a 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  /..#define SQLIT
33060 45 5f 52 65 63 6f 76 65 72 79 4d 6f 64 65 20 20  E_RecoveryMode  
33070 20 30 78 30 30 30 34 30 30 30 30 20 20 2f 2a 20   0x00040000  /* 
33080 49 67 6e 6f 72 65 20 73 63 68 65 6d 61 20 65 72  Ignore schema er
33090 72 6f 72 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50  rors */../*.** P
330a0 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20 66  ossible values f
330b0 6f 72 20 74 68 65 20 73 71 6c 69 74 65 2e 6d 61  or the sqlite.ma
330c0 67 69 63 20 66 69 65 6c 64 2e 0a 2a 2a 20 54 68  gic field..** Th
330d0 65 20 6e 75 6d 62 65 72 73 20 61 72 65 20 6f 62  e numbers are ob
330e0 74 61 69 6e 65 64 20 61 74 20 72 61 6e 64 6f 6d  tained at random
330f0 20 61 6e 64 20 68 61 76 65 20 6e 6f 20 73 70 65   and have no spe
33100 63 69 61 6c 20 6d 65 61 6e 69 6e 67 2c 20 6f 74  cial meaning, ot
33110 68 65 72 0a 2a 2a 20 74 68 61 6e 20 62 65 69 6e  her.** than bein
33120 67 20 64 69 73 74 69 6e 63 74 20 66 72 6f 6d 20  g distinct from 
33130 6f 6e 65 20 61 6e 6f 74 68 65 72 2e 0a 2a 2f 0a  one another..*/.
33140 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d  #define SQLITE_M
33150 41 47 49 43 5f 4f 50 45 4e 20 20 20 20 20 30 78  AGIC_OPEN     0x
33160 61 30 32 39 61 36 39 37 20 20 2f 2a 20 44 61 74  a029a697  /* Dat
33170 61 62 61 73 65 20 69 73 20 6f 70 65 6e 20 2a 2f  abase is open */
33180 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
33190 4d 41 47 49 43 5f 43 4c 4f 53 45 44 20 20 20 30  MAGIC_CLOSED   0
331a0 78 39 66 33 63 32 64 33 33 20 20 2f 2a 20 44 61  x9f3c2d33  /* Da
331b0 74 61 62 61 73 65 20 69 73 20 63 6c 6f 73 65 64  tabase is closed
331c0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
331d0 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 20 20  TE_MAGIC_BUSY   
331e0 20 20 30 78 66 30 33 62 37 39 30 36 20 20 2f 2a    0xf03b7906  /*
331f0 20 44 61 74 61 62 61 73 65 20 63 75 72 72 65 6e   Database curren
33200 74 6c 79 20 69 6e 20 75 73 65 20 2a 2f 0a 23 64  tly in use */.#d
33210 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47  efine SQLITE_MAG
33220 49 43 5f 45 52 52 4f 52 20 20 20 20 30 78 62 35  IC_ERROR    0xb5
33230 33 35 37 39 33 30 20 20 2f 2a 20 41 6e 20 53 51  357930  /* An SQ
33240 4c 49 54 45 5f 4d 49 53 55 53 45 20 65 72 72 6f  LITE_MISUSE erro
33250 72 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a 0a 2f  r occurred */../
33260 2a 0a 2a 2a 20 45 61 63 68 20 53 51 4c 20 66 75  *.** Each SQL fu
33270 6e 63 74 69 6f 6e 20 69 73 20 64 65 66 69 6e 65  nction is define
33280 64 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65  d by an instance
33290 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
332a0 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20  g.** structure. 
332b0 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   A pointer to th
332c0 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20  is structure is 
332d0 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 73 71  stored in the sq
332e0 6c 69 74 65 2e 61 46 75 6e 63 0a 2a 2a 20 68 61  lite.aFunc.** ha
332f0 73 68 20 74 61 62 6c 65 2e 20 20 57 68 65 6e 20  sh table.  When 
33300 6d 75 6c 74 69 70 6c 65 20 66 75 6e 63 74 69 6f  multiple functio
33310 6e 73 20 68 61 76 65 20 74 68 65 20 73 61 6d 65  ns have the same
33320 20 6e 61 6d 65 2c 20 74 68 65 20 68 61 73 68 20   name, the hash 
33330 74 61 62 6c 65 0a 2a 2a 20 70 6f 69 6e 74 73 20  table.** points 
33340 74 6f 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74  to a linked list
33350 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 63 74   of these struct
33360 75 72 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ures..*/.struct 
33370 46 75 6e 63 44 65 66 20 7b 0a 20 20 69 31 36 20  FuncDef {.  i16 
33380 6e 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20  nArg;           
33390 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72   /* Number of ar
333a0 67 75 6d 65 6e 74 73 2e 20 20 2d 31 20 6d 65 61  guments.  -1 mea
333b0 6e 73 20 75 6e 6c 69 6d 69 74 65 64 20 2a 2f 0a  ns unlimited */.
333c0 20 20 75 38 20 69 50 72 65 66 45 6e 63 3b 20 20    u8 iPrefEnc;  
333d0 20 20 20 20 20 20 20 2f 2a 20 50 72 65 66 65 72         /* Prefer
333e0 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  red text encodin
333f0 67 20 28 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  g (SQLITE_UTF8, 
33400 31 36 4c 45 2c 20 31 36 42 45 29 20 2a 2f 0a 20  16LE, 16BE) */. 
33410 20 75 38 20 6e 65 65 64 43 6f 6c 6c 53 65 71 3b   u8 needCollSeq;
33420 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
33430 20 73 71 6c 69 74 65 33 47 65 74 46 75 6e 63 43   sqlite3GetFuncC
33440 6f 6c 6c 53 65 71 28 29 20 6d 69 67 68 74 20 62  ollSeq() might b
33450 65 20 63 61 6c 6c 65 64 20 2a 2f 0a 20 20 75 38  e called */.  u8
33460 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20   flags;         
33470 20 20 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d 62 69     /* Some combi
33480 6e 61 74 69 6f 6e 20 6f 66 20 53 51 4c 49 54 45  nation of SQLITE
33490 5f 46 55 4e 43 5f 2a 20 2a 2f 0a 20 20 76 6f 69  _FUNC_* */.  voi
334a0 64 20 2a 70 55 73 65 72 44 61 74 61 3b 20 20 20  d *pUserData;   
334b0 20 20 2f 2a 20 55 73 65 72 20 64 61 74 61 20 70    /* User data p
334c0 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20 46 75  arameter */.  Fu
334d0 6e 63 44 65 66 20 2a 70 4e 65 78 74 3b 20 20 20  ncDef *pNext;   
334e0 20 20 20 2f 2a 20 4e 65 78 74 20 66 75 6e 63 74     /* Next funct
334f0 69 6f 6e 20 77 69 74 68 20 73 61 6d 65 20 6e 61  ion with same na
33500 6d 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78  me */.  void (*x
33510 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f  Func)(sqlite3_co
33520 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74  ntext*,int,sqlit
33530 65 33 5f 76 61 6c 75 65 2a 2a 29 3b 20 2f 2a 20  e3_value**); /* 
33540 52 65 67 75 6c 61 72 20 66 75 6e 63 74 69 6f 6e  Regular function
33550 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 53 74   */.  void (*xSt
33560 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ep)(sqlite3_cont
33570 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
33580 5f 76 61 6c 75 65 2a 2a 29 3b 20 2f 2a 20 41 67  _value**); /* Ag
33590 67 72 65 67 61 74 65 20 73 74 65 70 20 2a 2f 0a  gregate step */.
335a0 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 69    void (*xFinali
335b0 7a 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ze)(sqlite3_cont
335c0 65 78 74 2a 29 3b 20 20 20 20 20 20 20 20 20 20  ext*);          
335d0 20 20 20 20 20 20 2f 2a 20 41 67 67 72 65 67 61        /* Aggrega
335e0 74 65 20 66 69 6e 69 61 6c 69 7a 65 72 20 2a 2f  te finializer */
335f0 0a 20 20 63 68 61 72 20 7a 4e 61 6d 65 5b 31 5d  .  char zName[1]
33600 3b 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 6e  ;       /* SQL n
33610 61 6d 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74  ame of the funct
33620 69 6f 6e 2e 20 20 4d 55 53 54 20 42 45 20 4c 41  ion.  MUST BE LA
33630 53 54 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  ST */.};../*.** 
33640 45 61 63 68 20 53 51 4c 69 74 65 20 6d 6f 64 75  Each SQLite modu
33650 6c 65 20 28 76 69 72 74 75 61 6c 20 74 61 62 6c  le (virtual tabl
33660 65 20 64 65 66 69 6e 69 74 69 6f 6e 29 20 69 73  e definition) is
33670 20 64 65 66 69 6e 65 64 20 62 79 20 61 6e 0a 2a   defined by an.*
33680 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  * instance of th
33690 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
336a0 63 74 75 72 65 2c 20 73 74 6f 72 65 64 20 69 6e  cture, stored in
336b0 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61 4d 6f   the sqlite3.aMo
336c0 64 75 6c 65 0a 2a 2a 20 68 61 73 68 20 74 61 62  dule.** hash tab
336d0 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 6f  le..*/.struct Mo
336e0 64 75 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73  dule {.  const s
336f0 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70  qlite3_module *p
33700 4d 6f 64 75 6c 65 3b 20 20 20 20 20 20 20 2f 2a  Module;       /*
33710 20 43 61 6c 6c 62 61 63 6b 20 70 6f 69 6e 74 65   Callback pointe
33720 72 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  rs */.  const ch
33730 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20  ar *zName;      
33740 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
33750 4e 61 6d 65 20 70 61 73 73 65 64 20 74 6f 20 63  Name passed to c
33760 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20 2a  reate_module() *
33770 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 3b 20  /.  void *pAux; 
33780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33790 20 20 20 20 20 20 20 20 20 2f 2a 20 70 41 75 78           /* pAux
337a0 20 70 61 73 73 65 64 20 74 6f 20 63 72 65 61 74   passed to creat
337b0 65 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a 20 20  e_module() */.  
337c0 76 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79 29  void (*xDestroy)
337d0 28 76 6f 69 64 20 2a 29 3b 20 20 20 20 20 20 20  (void *);       
337e0 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 64       /* Module d
337f0 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69  estructor functi
33800 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  on */.};../*.** 
33810 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20  Possible values 
33820 66 6f 72 20 46 75 6e 63 44 65 66 2e 66 6c 61 67  for FuncDef.flag
33830 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  s.*/.#define SQL
33840 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 20 20 20  ITE_FUNC_LIKE   
33850 30 78 30 31 20 20 2f 2a 20 43 61 6e 64 69 64 61  0x01  /* Candida
33860 74 65 20 66 6f 72 20 74 68 65 20 4c 49 4b 45 20  te for the LIKE 
33870 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 2a 2f 0a  optimization */.
33880 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46  #define SQLITE_F
33890 55 4e 43 5f 43 41 53 45 20 20 20 30 78 30 32 20  UNC_CASE   0x02 
338a0 20 2f 2a 20 43 61 73 65 2d 73 65 6e 73 69 74 69   /* Case-sensiti
338b0 76 65 20 4c 49 4b 45 2d 74 79 70 65 20 66 75 6e  ve LIKE-type fun
338c0 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65  ction */.#define
338d0 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 45 50 48   SQLITE_FUNC_EPH
338e0 45 4d 20 20 30 78 30 34 20 20 2f 2a 20 45 70 68  EM  0x04  /* Eph
338f0 65 72 6d 65 72 61 6c 2e 20 20 44 65 6c 65 74 65  ermeral.  Delete
33900 20 77 69 74 68 20 56 44 42 45 20 2a 2f 0a 0a 2f   with VDBE */../
33910 2a 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  *.** information
33920 20 61 62 6f 75 74 20 65 61 63 68 20 63 6f 6c 75   about each colu
33930 6d 6e 20 6f 66 20 61 6e 20 53 51 4c 20 74 61 62  mn of an SQL tab
33940 6c 65 20 69 73 20 68 65 6c 64 20 69 6e 20 61 6e  le is held in an
33950 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20   instance.** of 
33960 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a  this structure..
33970 2a 2f 0a 73 74 72 75 63 74 20 43 6f 6c 75 6d 6e  */.struct Column
33980 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65   {.  char *zName
33990 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  ;     /* Name of
339a0 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a   this column */.
339b0 20 20 45 78 70 72 20 2a 70 44 66 6c 74 3b 20 20    Expr *pDflt;  
339c0 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 76 61     /* Default va
339d0 6c 75 65 20 6f 66 20 74 68 69 73 20 63 6f 6c 75  lue of this colu
339e0 6d 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54  mn */.  char *zT
339f0 79 70 65 3b 20 20 20 20 20 2f 2a 20 44 61 74 61  ype;     /* Data
33a00 20 74 79 70 65 20 66 6f 72 20 74 68 69 73 20 63   type for this c
33a10 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 63 68 61 72 20  olumn */.  char 
33a20 2a 7a 43 6f 6c 6c 3b 20 20 20 20 20 2f 2a 20 43  *zColl;     /* C
33a30 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
33a40 65 2e 20 20 49 66 20 4e 55 4c 4c 2c 20 75 73 65  e.  If NULL, use
33a50 20 74 68 65 20 64 65 66 61 75 6c 74 20 2a 2f 0a   the default */.
33a60 20 20 75 38 20 6e 6f 74 4e 75 6c 6c 3b 20 20 20    u8 notNull;   
33a70 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
33a80 65 72 65 20 69 73 20 61 20 4e 4f 54 20 4e 55 4c  ere is a NOT NUL
33a90 4c 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a  L constraint */.
33aa0 20 20 75 38 20 69 73 50 72 69 6d 4b 65 79 3b 20    u8 isPrimKey; 
33ab0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
33ac0 69 73 20 63 6f 6c 75 6d 6e 20 69 73 20 70 61 72  is column is par
33ad0 74 20 6f 66 20 74 68 65 20 50 52 49 4d 41 52 59  t of the PRIMARY
33ae0 20 4b 45 59 20 2a 2f 0a 20 20 63 68 61 72 20 61   KEY */.  char a
33af0 66 66 69 6e 69 74 79 3b 20 20 20 2f 2a 20 4f 6e  ffinity;   /* On
33b00 65 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f  e of the SQLITE_
33b10 41 46 46 5f 2e 2e 2e 20 76 61 6c 75 65 73 20 2a  AFF_... values *
33b20 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
33b30 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
33b40 4c 45 0a 20 20 75 38 20 69 73 48 69 64 64 65 6e  LE.  u8 isHidden
33b50 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66  ;     /* True if
33b60 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 69 73 20   this column is 
33b70 27 68 69 64 64 65 6e 27 20 2a 2f 0a 23 65 6e 64  'hidden' */.#end
33b80 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 22  if.};../*.** A "
33b90 43 6f 6c 6c 61 74 69 6e 67 20 53 65 71 75 65 6e  Collating Sequen
33ba0 63 65 22 20 69 73 20 64 65 66 69 6e 65 64 20 62  ce" is defined b
33bb0 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  y an instance of
33bc0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a   the following.*
33bd0 2a 20 73 74 72 75 63 74 75 72 65 2e 20 43 6f 6e  * structure. Con
33be0 63 65 70 74 75 61 6c 6c 79 2c 20 61 20 63 6f 6c  ceptually, a col
33bf0 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
33c00 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 6e 61  consists of a na
33c10 6d 65 20 61 6e 64 0a 2a 2a 20 61 20 63 6f 6d 70  me and.** a comp
33c20 61 72 69 73 6f 6e 20 72 6f 75 74 69 6e 65 20 74  arison routine t
33c30 68 61 74 20 64 65 66 69 6e 65 73 20 74 68 65 20  hat defines the 
33c40 6f 72 64 65 72 20 6f 66 20 74 68 61 74 20 73 65  order of that se
33c50 71 75 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  quence..**.** Th
33c60 65 72 65 20 6d 61 79 20 74 77 6f 20 73 65 70 65  ere may two sepe
33c70 72 61 74 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  rate implementat
33c80 69 6f 6e 73 20 6f 66 20 74 68 65 20 63 6f 6c 6c  ions of the coll
33c90 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 2c 20  ation function, 
33ca0 6f 6e 65 0a 2a 2a 20 74 68 61 74 20 70 72 6f 63  one.** that proc
33cb0 65 73 73 65 73 20 74 65 78 74 20 69 6e 20 55 54  esses text in UT
33cc0 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 28 43 6f  F-8 encoding (Co
33cd0 6c 6c 53 65 71 2e 78 43 6d 70 29 20 61 6e 64 20  llSeq.xCmp) and 
33ce0 61 6e 6f 74 68 65 72 20 74 68 61 74 0a 2a 2a 20  another that.** 
33cf0 70 72 6f 63 65 73 73 65 73 20 74 65 78 74 20 65  processes text e
33d00 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 31 36  ncoded in UTF-16
33d10 20 28 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 31 36   (CollSeq.xCmp16
33d20 29 2c 20 75 73 69 6e 67 20 74 68 65 20 6d 61 63  ), using the mac
33d30 68 69 6e 65 0a 2a 2a 20 6e 61 74 69 76 65 20 62  hine.** native b
33d40 79 74 65 20 6f 72 64 65 72 2e 20 57 68 65 6e 20  yte order. When 
33d50 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  a collation sequ
33d60 65 6e 63 65 20 69 73 20 69 6e 76 6f 6b 65 64 2c  ence is invoked,
33d70 20 53 51 4c 69 74 65 20 73 65 6c 65 63 74 73 0a   SQLite selects.
33d80 2a 2a 20 74 68 65 20 76 65 72 73 69 6f 6e 20 74  ** the version t
33d90 68 61 74 20 77 69 6c 6c 20 72 65 71 75 69 72 65  hat will require
33da0 20 74 68 65 20 6c 65 61 73 74 20 65 78 70 65 6e   the least expen
33db0 73 69 76 65 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a  sive encoding.**
33dc0 20 74 72 61 6e 73 6c 61 74 69 6f 6e 73 2c 20 69   translations, i
33dd0 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  f any..**.** The
33de0 20 43 6f 6c 6c 53 65 71 2e 70 55 73 65 72 20 6d   CollSeq.pUser m
33df0 65 6d 62 65 72 20 76 61 72 69 61 62 6c 65 20 69  ember variable i
33e00 73 20 61 6e 20 65 78 74 72 61 20 70 61 72 61 6d  s an extra param
33e10 65 74 65 72 20 74 68 61 74 20 70 61 73 73 65 64  eter that passed
33e20 20 69 6e 0a 2a 2a 20 61 73 20 74 68 65 20 66 69   in.** as the fi
33e30 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
33e40 74 68 65 20 55 54 46 2d 38 20 63 6f 6d 70 61 72  the UTF-8 compar
33e50 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 2c 20 78  ison function, x
33e60 43 6d 70 2e 0a 2a 2a 20 43 6f 6c 6c 53 65 71 2e  Cmp..** CollSeq.
33e70 70 55 73 65 72 31 36 20 69 73 20 74 68 65 20 65  pUser16 is the e
33e80 71 75 69 76 61 6c 65 6e 74 20 66 6f 72 20 74 68  quivalent for th
33e90 65 20 55 54 46 2d 31 36 20 63 6f 6d 70 61 72 69  e UTF-16 compari
33ea0 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 2c 0a 2a 2a  son function,.**
33eb0 20 78 43 6d 70 31 36 2e 0a 2a 2a 0a 2a 2a 20 49   xCmp16..**.** I
33ec0 66 20 62 6f 74 68 20 43 6f 6c 6c 53 65 71 2e 78  f both CollSeq.x
33ed0 43 6d 70 20 61 6e 64 20 43 6f 6c 6c 53 65 71 2e  Cmp and CollSeq.
33ee0 78 43 6d 70 31 36 20 61 72 65 20 4e 55 4c 4c 2c  xCmp16 are NULL,
33ef0 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74   it means that t
33f00 68 65 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20  he.** collating 
33f10 73 65 71 75 65 6e 63 65 20 69 73 20 75 6e 64 65  sequence is unde
33f20 66 69 6e 65 64 2e 20 20 49 6e 64 69 63 65 73 20  fined.  Indices 
33f30 62 75 69 6c 74 20 6f 6e 20 61 6e 20 75 6e 64 65  built on an unde
33f40 66 69 6e 65 64 0a 2a 2a 20 63 6f 6c 6c 61 74 69  fined.** collati
33f50 6e 67 20 73 65 71 75 65 6e 63 65 20 6d 61 79 20  ng sequence may 
33f60 6e 6f 74 20 62 65 20 72 65 61 64 20 6f 72 20 77  not be read or w
33f70 72 69 74 74 65 6e 2e 0a 2a 2f 0a 73 74 72 75 63  ritten..*/.struc
33f80 74 20 43 6f 6c 6c 53 65 71 20 7b 0a 20 20 63 68  t CollSeq {.  ch
33f90 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20  ar *zName;      
33fa0 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
33fb0 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  he collating seq
33fc0 75 65 6e 63 65 2c 20 55 54 46 2d 38 20 65 6e 63  uence, UTF-8 enc
33fd0 6f 64 65 64 20 2a 2f 0a 20 20 75 38 20 65 6e 63  oded */.  u8 enc
33fe0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
33ff0 2f 2a 20 54 65 78 74 20 65 6e 63 6f 64 69 6e 67  /* Text encoding
34000 20 68 61 6e 64 6c 65 64 20 62 79 20 78 43 6d 70   handled by xCmp
34010 28 29 20 2a 2f 0a 20 20 75 38 20 74 79 70 65 3b  () */.  u8 type;
34020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
34030 20 4f 6e 65 20 6f 66 20 74 68 65 20 53 51 4c 49   One of the SQLI
34040 54 45 5f 43 4f 4c 4c 5f 2e 2e 2e 20 76 61 6c 75  TE_COLL_... valu
34050 65 73 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 76 6f  es below */.  vo
34060 69 64 20 2a 70 55 73 65 72 3b 20 20 20 20 20 20  id *pUser;      
34070 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67      /* First arg
34080 75 6d 65 6e 74 20 74 6f 20 78 43 6d 70 28 29 20  ument to xCmp() 
34090 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43 6d 70 29  */.  int (*xCmp)
340a0 28 76 6f 69 64 2a 2c 69 6e 74 2c 20 63 6f 6e 73  (void*,int, cons
340b0 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 63 6f  t void*, int, co
340c0 6e 73 74 20 76 6f 69 64 2a 29 3b 0a 20 20 76 6f  nst void*);.  vo
340d0 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a  id (*xDel)(void*
340e0 29 3b 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f  );  /* Destructo
340f0 72 20 66 6f 72 20 70 55 73 65 72 20 2a 2f 0a 7d  r for pUser */.}
34100 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64  ;../*.** Allowed
34110 20 76 61 6c 75 65 73 20 6f 66 20 43 6f 6c 6c 53   values of CollS
34120 65 71 20 66 6c 61 67 73 3a 0a 2a 2f 0a 23 64 65  eq flags:.*/.#de
34130 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c  fine SQLITE_COLL
34140 5f 42 49 4e 41 52 59 20 20 31 20 20 2f 2a 20 54  _BINARY  1  /* T
34150 68 65 20 64 65 66 61 75 6c 74 20 6d 65 6d 63 6d  he default memcm
34160 70 28 29 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  p() collating se
34170 71 75 65 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e  quence */.#defin
34180 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 4e 4f  e SQLITE_COLL_NO
34190 43 41 53 45 20 20 32 20 20 2f 2a 20 54 68 65 20  CASE  2  /* The 
341a0 62 75 69 6c 74 2d 69 6e 20 4e 4f 43 41 53 45 20  built-in NOCASE 
341b0 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
341c0 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  ce */.#define SQ
341d0 4c 49 54 45 5f 43 4f 4c 4c 5f 52 45 56 45 52 53  LITE_COLL_REVERS
341e0 45 20 33 20 20 2f 2a 20 54 68 65 20 62 75 69 6c  E 3  /* The buil
341f0 74 2d 69 6e 20 52 45 56 45 52 53 45 20 63 6f 6c  t-in REVERSE col
34200 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
34210 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
34220 45 5f 43 4f 4c 4c 5f 55 53 45 52 20 20 20 20 30  E_COLL_USER    0
34230 20 20 2f 2a 20 41 6e 79 20 6f 74 68 65 72 20 75    /* Any other u
34240 73 65 72 2d 64 65 66 69 6e 65 64 20 63 6f 6c 6c  ser-defined coll
34250 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a  ating sequence *
34260 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 6f 72 74 20  /../*.** A sort 
34270 6f 72 64 65 72 20 63 61 6e 20 62 65 20 65 69 74  order can be eit
34280 68 65 72 20 41 53 43 20 6f 72 20 44 45 53 43 2e  her ASC or DESC.
34290 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
342a0 54 45 5f 53 4f 5f 41 53 43 20 20 20 20 20 20 20  TE_SO_ASC       
342b0 30 20 20 2f 2a 20 53 6f 72 74 20 69 6e 20 61 73  0  /* Sort in as
342c0 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 2a 2f  cending order */
342d0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
342e0 53 4f 5f 44 45 53 43 20 20 20 20 20 20 31 20 20  SO_DESC      1  
342f0 2f 2a 20 53 6f 72 74 20 69 6e 20 61 73 63 65 6e  /* Sort in ascen
34300 64 69 6e 67 20 6f 72 64 65 72 20 2a 2f 0a 0a 2f  ding order */../
34310 2a 0a 2a 2a 20 43 6f 6c 75 6d 6e 20 61 66 66 69  *.** Column affi
34320 6e 69 74 79 20 74 79 70 65 73 2e 0a 2a 2a 0a 2a  nity types..**.*
34330 2a 20 54 68 65 73 65 20 75 73 65 64 20 74 6f 20  * These used to 
34340 68 61 76 65 20 6d 6e 65 6d 6f 6e 69 63 20 6e 61  have mnemonic na
34350 6d 65 20 6c 69 6b 65 20 27 69 27 20 66 6f 72 20  me like 'i' for 
34360 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47  SQLITE_AFF_INTEG
34370 45 52 20 61 6e 64 0a 2a 2a 20 27 74 27 20 66 6f  ER and.** 't' fo
34380 72 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58  r SQLITE_AFF_TEX
34390 54 2e 20 20 42 75 74 20 77 65 20 63 61 6e 20 73  T.  But we can s
343a0 61 76 65 20 61 20 6c 69 74 74 6c 65 20 73 70 61  ave a little spa
343b0 63 65 20 61 6e 64 20 69 6d 70 72 6f 76 65 0a 2a  ce and improve.*
343c0 2a 20 74 68 65 20 73 70 65 65 64 20 61 20 6c 69  * the speed a li
343d0 74 74 6c 65 20 62 79 20 6e 75 6d 62 65 72 20 74  ttle by number t
343e0 68 65 20 76 61 6c 75 65 73 20 63 6f 6e 73 65 63  he values consec
343f0 75 74 69 76 65 6c 79 2e 20 20 0a 2a 2a 0a 2a 2a  utively.  .**.**
34400 20 42 75 74 20 72 61 74 68 65 72 20 74 68 61 6e   But rather than
34410 20 73 74 61 72 74 20 77 69 74 68 20 30 20 6f 72   start with 0 or
34420 20 31 2c 20 77 65 20 62 65 67 69 6e 20 77 69 74   1, we begin wit
34430 68 20 27 61 27 2e 20 20 54 68 61 74 20 77 61 79  h 'a'.  That way
34440 2c 0a 2a 2a 20 77 68 65 6e 20 6d 75 6c 74 69 70  ,.** when multip
34450 6c 65 20 61 66 66 69 6e 69 74 79 20 74 79 70 65  le affinity type
34460 73 20 61 72 65 20 63 6f 6e 63 61 74 65 6e 61 74  s are concatenat
34470 65 64 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67  ed into a string
34480 20 61 6e 64 0a 2a 2a 20 75 73 65 64 20 61 73 20   and.** used as 
34490 74 68 65 20 50 33 20 6f 70 65 72 61 6e 64 2c 20  the P3 operand, 
344a0 74 68 65 79 20 77 69 6c 6c 20 62 65 20 6d 6f 72  they will be mor
344b0 65 20 72 65 61 64 61 62 6c 65 2e 0a 2a 2a 0a 2a  e readable..**.*
344c0 2a 20 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61 74  * Note also that
344d0 20 74 68 65 20 6e 75 6d 65 72 69 63 20 74 79 70   the numeric typ
344e0 65 73 20 61 72 65 20 67 72 6f 75 70 65 64 20 74  es are grouped t
344f0 6f 67 65 74 68 65 72 20 73 6f 20 74 68 61 74 20  ogether so that 
34500 74 65 73 74 69 6e 67 0a 2a 2a 20 66 6f 72 20 61  testing.** for a
34510 20 6e 75 6d 65 72 69 63 20 74 79 70 65 20 69 73   numeric type is
34520 20 61 20 73 69 6e 67 6c 65 20 63 6f 6d 70 61 72   a single compar
34530 69 73 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  ison..*/.#define
34540 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54   SQLITE_AFF_TEXT
34550 20 20 20 20 20 27 61 27 0a 23 64 65 66 69 6e 65       'a'.#define
34560 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45   SQLITE_AFF_NONE
34570 20 20 20 20 20 27 62 27 0a 23 64 65 66 69 6e 65       'b'.#define
34580 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45   SQLITE_AFF_NUME
34590 52 49 43 20 20 27 63 27 0a 23 64 65 66 69 6e 65  RIC  'c'.#define
345a0 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45   SQLITE_AFF_INTE
345b0 47 45 52 20 20 27 64 27 0a 23 64 65 66 69 6e 65  GER  'd'.#define
345c0 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c   SQLITE_AFF_REAL
345d0 20 20 20 20 20 27 65 27 0a 0a 23 64 65 66 69 6e       'e'..#defin
345e0 65 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 65 72  e sqlite3IsNumer
345f0 69 63 41 66 66 69 6e 69 74 79 28 58 29 20 20 28  icAffinity(X)  (
34600 28 58 29 3e 3d 53 51 4c 49 54 45 5f 41 46 46 5f  (X)>=SQLITE_AFF_
34610 4e 55 4d 45 52 49 43 29 0a 0a 2f 2a 0a 2a 2a 20  NUMERIC)../*.** 
34620 45 61 63 68 20 53 51 4c 20 74 61 62 6c 65 20 69  Each SQL table i
34630 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 69 6e  s represented in
34640 20 6d 65 6d 6f 72 79 20 62 79 20 61 6e 20 69 6e   memory by an in
34650 73 74 61 6e 63 65 20 6f 66 20 74 68 65 0a 2a 2a  stance of the.**
34660 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
34670 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 61 62 6c  ture..**.** Tabl
34680 65 2e 7a 4e 61 6d 65 20 69 73 20 74 68 65 20 6e  e.zName is the n
34690 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
346a0 2e 20 20 54 68 65 20 63 61 73 65 20 6f 66 20 74  .  The case of t
346b0 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 43  he original.** C
346c0 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74  REATE TABLE stat
346d0 65 6d 65 6e 74 20 69 73 20 73 74 6f 72 65 64 2c  ement is stored,
346e0 20 62 75 74 20 63 61 73 65 20 69 73 20 6e 6f 74   but case is not
346f0 20 73 69 67 6e 69 66 69 63 61 6e 74 20 66 6f 72   significant for
34700 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 73 2e  .** comparisons.
34710 0a 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e 6e 43 6f  .**.** Table.nCo
34720 6c 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  l is the number 
34730 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  of columns in th
34740 69 73 20 74 61 62 6c 65 2e 20 20 54 61 62 6c 65  is table.  Table
34750 2e 61 43 6f 6c 20 69 73 20 61 0a 2a 2a 20 70 6f  .aCol is a.** po
34760 69 6e 74 65 72 20 74 6f 20 61 6e 20 61 72 72 61  inter to an arra
34770 79 20 6f 66 20 43 6f 6c 75 6d 6e 20 73 74 72 75  y of Column stru
34780 63 74 75 72 65 73 2c 20 6f 6e 65 20 66 6f 72 20  ctures, one for 
34790 65 61 63 68 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a  each column..**.
347a0 2a 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 20  ** If the table 
347b0 68 61 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50  has an INTEGER P
347c0 52 49 4d 41 52 59 20 4b 45 59 2c 20 74 68 65 6e  RIMARY KEY, then
347d0 20 54 61 62 6c 65 2e 69 50 4b 65 79 20 69 73 20   Table.iPKey is 
347e0 74 68 65 20 69 6e 64 65 78 20 6f 66 0a 2a 2a 20  the index of.** 
347f0 74 68 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20  the column that 
34800 69 73 20 74 68 61 74 20 6b 65 79 2e 20 20 20 4f  is that key.   O
34810 74 68 65 72 77 69 73 65 20 54 61 62 6c 65 2e 69  therwise Table.i
34820 50 4b 65 79 20 69 73 20 6e 65 67 61 74 69 76 65  PKey is negative
34830 2e 20 20 4e 6f 74 65 0a 2a 2a 20 74 68 61 74 20  .  Note.** that 
34840 74 68 65 20 64 61 74 61 74 79 70 65 20 6f 66 20  the datatype of 
34850 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20  the PRIMARY KEY 
34860 6d 75 73 74 20 62 65 20 49 4e 54 45 47 45 52 20  must be INTEGER 
34870 66 6f 72 20 74 68 69 73 20 66 69 65 6c 64 20 74  for this field t
34880 6f 0a 2a 2a 20 62 65 20 73 65 74 2e 20 20 41 6e  o.** be set.  An
34890 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
348a0 20 4b 45 59 20 69 73 20 75 73 65 64 20 61 73 20   KEY is used as 
348b0 74 68 65 20 72 6f 77 69 64 20 66 6f 72 20 65 61  the rowid for ea
348c0 63 68 20 72 6f 77 20 6f 66 0a 2a 2a 20 74 68 65  ch row of.** the
348d0 20 74 61 62 6c 65 2e 20 20 49 66 20 61 20 74 61   table.  If a ta
348e0 62 6c 65 20 68 61 73 20 6e 6f 20 49 4e 54 45 47  ble has no INTEG
348f0 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20  ER PRIMARY KEY, 
34900 74 68 65 6e 20 61 20 72 61 6e 64 6f 6d 20 72 6f  then a random ro
34910 77 69 64 0a 2a 2a 20 69 73 20 67 65 6e 65 72 61  wid.** is genera
34920 74 65 64 20 66 6f 72 20 65 61 63 68 20 72 6f 77  ted for each row
34930 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 20   of the table.  
34940 54 61 62 6c 65 2e 68 61 73 50 72 69 6d 4b 65 79  Table.hasPrimKey
34950 20 69 73 20 74 72 75 65 20 69 66 0a 2a 2a 20 74   is true if.** t
34960 68 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e 79  he table has any
34970 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 49 4e   PRIMARY KEY, IN
34980 54 45 47 45 52 20 6f 72 20 6f 74 68 65 72 77 69  TEGER or otherwi
34990 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e  se..**.** Table.
349a0 74 6e 75 6d 20 69 73 20 74 68 65 20 70 61 67 65  tnum is the page
349b0 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20   number for the 
349c0 72 6f 6f 74 20 42 54 72 65 65 20 70 61 67 65 20  root BTree page 
349d0 6f 66 20 74 68 65 20 74 61 62 6c 65 20 69 6e 20  of the table in 
349e0 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  the.** database 
349f0 66 69 6c 65 2e 20 20 49 66 20 54 61 62 6c 65 2e  file.  If Table.
34a00 69 44 62 20 69 73 20 74 68 65 20 69 6e 64 65 78  iDb is the index
34a10 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
34a20 20 74 61 62 6c 65 20 62 61 63 6b 65 6e 64 0a 2a   table backend.*
34a30 2a 20 69 6e 20 73 71 6c 69 74 65 2e 61 44 62 5b  * in sqlite.aDb[
34a40 5d 2e 20 20 30 20 69 73 20 66 6f 72 20 74 68 65  ].  0 is for the
34a50 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 61   main database a
34a60 6e 64 20 31 20 69 73 20 66 6f 72 20 74 68 65 20  nd 1 is for the 
34a70 66 69 6c 65 20 74 68 61 74 0a 2a 2a 20 68 6f 6c  file that.** hol
34a80 64 73 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  ds temporary tab
34a90 6c 65 73 20 61 6e 64 20 69 6e 64 69 63 65 73 2e  les and indices.
34aa0 20 20 49 66 20 54 61 62 6c 65 2e 69 73 45 70 68    If Table.isEph
34ab0 65 6d 0a 2a 2a 20 69 73 20 74 72 75 65 2c 20 74  em.** is true, t
34ac0 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 69 73  hen the table is
34ad0 20 73 74 6f 72 65 64 20 69 6e 20 61 20 66 69 6c   stored in a fil
34ae0 65 20 74 68 61 74 20 69 73 20 61 75 74 6f 6d 61  e that is automa
34af0 74 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 64 0a  tically deleted.
34b00 2a 2a 20 77 68 65 6e 20 74 68 65 20 56 44 42 45  ** when the VDBE
34b10 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 74   cursor to the t
34b20 61 62 6c 65 20 69 73 20 63 6c 6f 73 65 64 2e 20  able is closed. 
34b30 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 54 61   In this case Ta
34b40 62 6c 65 2e 74 6e 75 6d 20 0a 2a 2a 20 72 65 66  ble.tnum .** ref
34b50 65 72 73 20 56 44 42 45 20 63 75 72 73 6f 72 20  ers VDBE cursor 
34b60 6e 75 6d 62 65 72 20 74 68 61 74 20 68 6f 6c 64  number that hold
34b70 73 20 74 68 65 20 74 61 62 6c 65 20 6f 70 65 6e  s the table open
34b80 2c 20 6e 6f 74 20 74 6f 20 74 68 65 20 72 6f 6f  , not to the roo
34b90 74 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65 72  t.** page number
34ba0 2e 20 20 54 72 61 6e 73 69 65 6e 74 20 74 61 62  .  Transient tab
34bb0 6c 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20  les are used to 
34bc0 68 6f 6c 64 20 74 68 65 20 72 65 73 75 6c 74 73  hold the results
34bd0 20 6f 66 20 61 0a 2a 2a 20 73 75 62 2d 71 75 65   of a.** sub-que
34be0 72 79 20 74 68 61 74 20 61 70 70 65 61 72 73 20  ry that appears 
34bf0 69 6e 73 74 65 61 64 20 6f 66 20 61 20 72 65 61  instead of a rea
34c00 6c 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20  l table name in 
34c10 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
34c20 0a 2a 2a 20 6f 66 20 61 20 53 45 4c 45 43 54 20  .** of a SELECT 
34c30 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74  statement..*/.st
34c40 72 75 63 74 20 54 61 62 6c 65 20 7b 0a 20 20 63  ruct Table {.  c
34c50 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20  har *zName;     
34c60 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74  /* Name of the t
34c70 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  able */.  int nC
34c80 6f 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  ol;        /* Nu
34c90 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
34ca0 69 6e 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f  in this table */
34cb0 0a 20 20 43 6f 6c 75 6d 6e 20 2a 61 43 6f 6c 3b  .  Column *aCol;
34cc0 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69      /* Informati
34cd0 6f 6e 20 61 62 6f 75 74 20 65 61 63 68 20 63 6f  on about each co
34ce0 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 50  lumn */.  int iP
34cf0 4b 65 79 3b 20 20 20 20 20 20 20 2f 2a 20 49 66  Key;       /* If
34d00 20 6e 6f 74 20 6c 65 73 73 20 74 68 65 6e 20 30   not less then 0
34d10 2c 20 75 73 65 20 61 43 6f 6c 5b 69 50 4b 65 79  , use aCol[iPKey
34d20 5d 20 61 73 20 74 68 65 20 70 72 69 6d 61 72 79  ] as the primary
34d30 20 6b 65 79 20 2a 2f 0a 20 20 49 6e 64 65 78 20   key */.  Index 
34d40 2a 70 49 6e 64 65 78 3b 20 20 20 2f 2a 20 4c 69  *pIndex;   /* Li
34d50 73 74 20 6f 66 20 53 51 4c 20 69 6e 64 65 78 65  st of SQL indexe
34d60 73 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2e  s on this table.
34d70 20 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d 3b 20   */.  int tnum; 
34d80 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 42         /* Root B
34d90 54 72 65 65 20 6e 6f 64 65 20 66 6f 72 20 74 68  Tree node for th
34da0 69 73 20 74 61 62 6c 65 20 28 73 65 65 20 6e 6f  is table (see no
34db0 74 65 20 61 62 6f 76 65 29 20 2a 2f 0a 20 20 53  te above) */.  S
34dc0 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20  elect *pSelect; 
34dd0 2f 2a 20 4e 55 4c 4c 20 66 6f 72 20 74 61 62 6c  /* NULL for tabl
34de0 65 73 2e 20 20 50 6f 69 6e 74 73 20 74 6f 20 64  es.  Points to d
34df0 65 66 69 6e 69 74 69 6f 6e 20 69 66 20 61 20 76  efinition if a v
34e00 69 65 77 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 52  iew. */.  int nR
34e10 65 66 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ef;          /* 
34e20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74 65  Number of pointe
34e30 72 73 20 74 6f 20 74 68 69 73 20 54 61 62 6c 65  rs to this Table
34e40 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70   */.  Trigger *p
34e50 54 72 69 67 67 65 72 3b 20 2f 2a 20 4c 69 73 74  Trigger; /* List
34e60 20 6f 66 20 53 51 4c 20 74 72 69 67 67 65 72 73   of SQL triggers
34e70 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 20 2a   on this table *
34e80 2f 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 3b  /.  FKey *pFKey;
34e90 20 20 20 20 20 20 20 2f 2a 20 4c 69 6e 6b 65 64         /* Linked
34ea0 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 66 6f 72   list of all for
34eb0 65 69 67 6e 20 6b 65 79 73 20 69 6e 20 74 68 69  eign keys in thi
34ec0 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61  s table */.  cha
34ed0 72 20 2a 7a 43 6f 6c 41 66 66 3b 20 20 20 20 20  r *zColAff;     
34ee0 2f 2a 20 53 74 72 69 6e 67 20 64 65 66 69 6e 69  /* String defini
34ef0 6e 67 20 74 68 65 20 61 66 66 69 6e 69 74 79 20  ng the affinity 
34f00 6f 66 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a  of each column *
34f10 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
34f20 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 45 78  _OMIT_CHECK.  Ex
34f30 70 72 20 2a 70 43 68 65 63 6b 3b 20 20 20 20 20  pr *pCheck;     
34f40 20 2f 2a 20 54 68 65 20 41 4e 44 20 6f 66 20 61   /* The AND of a
34f50 6c 6c 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61  ll CHECK constra
34f60 69 6e 74 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 23  ints */.#endif.#
34f70 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
34f80 49 54 5f 41 4c 54 45 52 54 41 42 4c 45 0a 20 20  IT_ALTERTABLE.  
34f90 69 6e 74 20 61 64 64 43 6f 6c 4f 66 66 73 65 74  int addColOffset
34fa0 3b 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 20  ;  /* Offset in 
34fb0 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61  CREATE TABLE sta
34fc0 74 65 6d 65 6e 74 20 74 6f 20 61 64 64 20 61 20  tement to add a 
34fd0 6e 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 23 65  new column */.#e
34fe0 6e 64 69 66 0a 20 20 75 38 20 72 65 61 64 4f 6e  ndif.  u8 readOn
34ff0 6c 79 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20  ly;     /* True 
35000 69 66 20 74 68 69 73 20 74 61 62 6c 65 20 73 68  if this table sh
35010 6f 75 6c 64 20 6e 6f 74 20 62 65 20 77 72 69 74  ould not be writ
35020 74 65 6e 20 62 79 20 74 68 65 20 75 73 65 72 20  ten by the user 
35030 2a 2f 0a 20 20 75 38 20 69 73 45 70 68 65 6d 3b  */.  u8 isEphem;
35040 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
35050 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 4f   created using O
35060 50 5f 4f 70 65 6e 45 70 68 65 72 6d 65 72 61 6c  P_OpenEphermeral
35070 20 2a 2f 0a 20 20 75 38 20 68 61 73 50 72 69 6d   */.  u8 hasPrim
35080 4b 65 79 3b 20 20 20 2f 2a 20 54 72 75 65 20 69  Key;   /* True i
35090 66 20 74 68 65 72 65 20 65 78 69 73 74 73 20 61  f there exists a
350a0 20 70 72 69 6d 61 72 79 20 6b 65 79 20 2a 2f 0a   primary key */.
350b0 20 20 75 38 20 6b 65 79 43 6f 6e 66 3b 20 20 20    u8 keyConf;   
350c0 20 20 20 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f     /* What to do
350d0 20 69 6e 20 63 61 73 65 20 6f 66 20 75 6e 69 71   in case of uniq
350e0 75 65 6e 65 73 73 20 63 6f 6e 66 6c 69 63 74 20  ueness conflict 
350f0 6f 6e 20 69 50 4b 65 79 20 2a 2f 0a 20 20 75 38  on iPKey */.  u8
35100 20 61 75 74 6f 49 6e 63 3b 20 20 20 20 20 20 2f   autoInc;      /
35110 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 69 6e  * True if the in
35120 74 65 67 65 72 20 70 72 69 6d 61 72 79 20 6b 65  teger primary ke
35130 79 20 69 73 20 61 75 74 6f 69 6e 63 72 65 6d 65  y is autoincreme
35140 6e 74 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  nt */.#ifndef SQ
35150 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
35160 4c 54 41 42 4c 45 0a 20 20 75 38 20 69 73 56 69  LTABLE.  u8 isVi
35170 72 74 75 61 6c 3b 20 20 20 20 20 20 20 20 20 20  rtual;          
35180 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
35190 69 73 20 69 73 20 61 20 76 69 72 74 75 61 6c 20  is is a virtual 
351a0 74 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20 69 73  table */.  u8 is
351b0 43 6f 6d 6d 69 74 3b 20 20 20 20 20 20 20 20 20  Commit;         
351c0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 6f 6e 63       /* True onc
351d0 65 20 74 68 65 20 43 52 45 41 54 45 20 54 41 42  e the CREATE TAB
351e0 4c 45 20 68 61 73 20 62 65 65 6e 20 63 6f 6d 6d  LE has been comm
351f0 69 74 74 65 64 20 2a 2f 0a 20 20 4d 6f 64 75 6c  itted */.  Modul
35200 65 20 2a 70 4d 6f 64 3b 20 20 20 20 20 20 20 20  e *pMod;        
35210 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
35220 74 6f 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  to the implement
35230 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 64  ation of the mod
35240 75 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ule */.  sqlite3
35250 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 20 20 20  _vtab *pVtab;   
35260 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
35270 20 74 68 65 20 6d 6f 64 75 6c 65 20 69 6e 73 74   the module inst
35280 61 6e 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d  ance */.  int nM
35290 6f 64 75 6c 65 41 72 67 3b 20 20 20 20 20 20 20  oduleArg;       
352a0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
352b0 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68   arguments to th
352c0 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 68  e module */.  ch
352d0 61 72 20 2a 2a 61 7a 4d 6f 64 75 6c 65 41 72 67  ar **azModuleArg
352e0 3b 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20  ;       /* Text 
352f0 6f 66 20 61 6c 6c 20 6d 6f 64 75 6c 65 20 61 72  of all module ar
35300 67 73 2e 20 5b 30 5d 20 69 73 20 6d 6f 64 75 6c  gs. [0] is modul
35310 65 20 6e 61 6d 65 20 2a 2f 0a 23 65 6e 64 69 66  e name */.#endif
35320 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65  .  Schema *pSche
35330 6d 61 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 65  ma;.};../*.** Te
35340 73 74 20 74 6f 20 73 65 65 20 77 68 65 74 68 65  st to see whethe
35350 72 20 6f 72 20 6e 6f 74 20 61 20 74 61 62 6c 65  r or not a table
35360 20 69 73 20 61 20 76 69 72 74 75 61 6c 20 74 61   is a virtual ta
35370 62 6c 65 2e 20 20 54 68 69 73 20 69 73 0a 2a 2a  ble.  This is.**
35380 20 64 6f 6e 65 20 61 73 20 61 20 6d 61 63 72 6f   done as a macro
35390 20 73 6f 20 74 68 61 74 20 69 74 20 77 69 6c 6c   so that it will
353a0 20 62 65 20 6f 70 74 69 6d 69 7a 65 64 20 6f 75   be optimized ou
353b0 74 20 77 68 65 6e 20 76 69 72 74 75 61 6c 0a 2a  t when virtual.*
353c0 2a 20 74 61 62 6c 65 20 73 75 70 70 6f 72 74 20  * table support 
353d0 69 73 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20  is omitted from 
353e0 74 68 65 20 62 75 69 6c 64 2e 0a 2a 2f 0a 23 69  the build..*/.#i
353f0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
35400 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 23  T_VIRTUALTABLE.#
35410 20 20 64 65 66 69 6e 65 20 49 73 56 69 72 74 75    define IsVirtu
35420 61 6c 28 58 29 20 20 20 20 20 20 28 28 58 29 2d  al(X)      ((X)-
35430 3e 69 73 56 69 72 74 75 61 6c 29 0a 23 20 20 64  >isVirtual).#  d
35440 65 66 69 6e 65 20 49 73 48 69 64 64 65 6e 43 6f  efine IsHiddenCo
35450 6c 75 6d 6e 28 58 29 20 28 28 58 29 2d 3e 69 73  lumn(X) ((X)->is
35460 48 69 64 64 65 6e 29 0a 23 65 6c 73 65 0a 23 20  Hidden).#else.# 
35470 20 64 65 66 69 6e 65 20 49 73 56 69 72 74 75 61   define IsVirtua
35480 6c 28 58 29 20 20 20 20 20 20 30 0a 23 20 20 64  l(X)      0.#  d
35490 65 66 69 6e 65 20 49 73 48 69 64 64 65 6e 43 6f  efine IsHiddenCo
354a0 6c 75 6d 6e 28 58 29 20 30 0a 23 65 6e 64 69 66  lumn(X) 0.#endif
354b0 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 66 6f 72  ../*.** Each for
354c0 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
354d0 69 6e 74 20 69 73 20 61 6e 20 69 6e 73 74 61 6e  int is an instan
354e0 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
354f0 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  ing structure..*
35500 2a 0a 2a 2a 20 41 20 66 6f 72 65 69 67 6e 20 6b  *.** A foreign k
35510 65 79 20 69 73 20 61 73 73 6f 63 69 61 74 65 64  ey is associated
35520 20 77 69 74 68 20 74 77 6f 20 74 61 62 6c 65 73   with two tables
35530 2e 20 20 54 68 65 20 22 66 72 6f 6d 22 20 74 61  .  The "from" ta
35540 62 6c 65 20 69 73 0a 2a 2a 20 74 68 65 20 74 61  ble is.** the ta
35550 62 6c 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ble that contain
35560 73 20 74 68 65 20 52 45 46 45 52 45 4e 43 45 53  s the REFERENCES
35570 20 63 6c 61 75 73 65 20 74 68 61 74 20 63 72 65   clause that cre
35580 61 74 65 73 20 74 68 65 20 66 6f 72 65 69 67 6e  ates the foreign
35590 0a 2a 2a 20 6b 65 79 2e 20 20 54 68 65 20 22 74  .** key.  The "t
355a0 6f 22 20 74 61 62 6c 65 20 69 73 20 74 68 65 20  o" table is the 
355b0 74 61 62 6c 65 20 74 68 61 74 20 69 73 20 6e 61  table that is na
355c0 6d 65 64 20 69 6e 20 74 68 65 20 52 45 46 45 52  med in the REFER
355d0 45 4e 43 45 53 20 63 6c 61 75 73 65 2e 0a 2a 2a  ENCES clause..**
355e0 20 43 6f 6e 73 69 64 65 72 20 74 68 69 73 20 65   Consider this e
355f0 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20  xample:.**.**   
35600 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 65    CREATE TABLE e
35610 78 31 28 0a 2a 2a 20 20 20 20 20 20 20 61 20 49  x1(.**       a I
35620 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
35630 45 59 2c 0a 2a 2a 20 20 20 20 20 20 20 62 20 49  EY,.**       b I
35640 4e 54 45 47 45 52 20 43 4f 4e 53 54 52 41 49 4e  NTEGER CONSTRAIN
35650 54 20 66 6b 31 20 52 45 46 45 52 45 4e 43 45 53  T fk1 REFERENCES
35660 20 65 78 32 28 78 29 0a 2a 2a 20 20 20 20 20 29   ex2(x).**     )
35670 3b 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 66 6f 72 65  ;.**.** For fore
35680 69 67 6e 20 6b 65 79 20 22 66 6b 31 22 2c 20 74  ign key "fk1", t
35690 68 65 20 66 72 6f 6d 2d 74 61 62 6c 65 20 69 73  he from-table is
356a0 20 22 65 78 31 22 20 61 6e 64 20 74 68 65 20 74   "ex1" and the t
356b0 6f 2d 74 61 62 6c 65 20 69 73 20 22 65 78 32 22  o-table is "ex2"
356c0 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 52 45 46  ..**.** Each REF
356d0 45 52 45 4e 43 45 53 20 63 6c 61 75 73 65 20 67  ERENCES clause g
356e0 65 6e 65 72 61 74 65 73 20 61 6e 20 69 6e 73 74  enerates an inst
356f0 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
35700 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 0a  owing structure.
35710 2a 2a 20 77 68 69 63 68 20 69 73 20 61 74 74 61  ** which is atta
35720 63 68 65 64 20 74 6f 20 74 68 65 20 66 72 6f 6d  ched to the from
35730 2d 74 61 62 6c 65 2e 20 20 54 68 65 20 74 6f 2d  -table.  The to-
35740 74 61 62 6c 65 20 6e 65 65 64 20 6e 6f 74 20 65  table need not e
35750 78 69 73 74 20 77 68 65 6e 0a 2a 2a 20 74 68 65  xist when.** the
35760 20 66 72 6f 6d 2d 74 61 62 6c 65 20 69 73 20 63   from-table is c
35770 72 65 61 74 65 64 2e 20 20 54 68 65 20 65 78 69  reated.  The exi
35780 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 74 6f  stance of the to
35790 2d 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 63 68  -table is not ch
357a0 65 63 6b 65 64 0a 2a 2a 20 75 6e 74 69 6c 20 61  ecked.** until a
357b0 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64  n attempt is mad
357c0 65 20 74 6f 20 69 6e 73 65 72 74 20 64 61 74 61  e to insert data
357d0 20 69 6e 74 6f 20 74 68 65 20 66 72 6f 6d 2d 74   into the from-t
357e0 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  able..**.** The 
357f0 73 71 6c 69 74 65 2e 61 46 4b 65 79 20 68 61 73  sqlite.aFKey has
35800 68 20 74 61 62 6c 65 20 73 74 6f 72 65 73 20 70  h table stores p
35810 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 69 73 20  ointers to this 
35820 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 67 69 76  structure.** giv
35830 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61  en the name of a
35840 20 74 6f 2d 74 61 62 6c 65 2e 20 20 46 6f 72 20   to-table.  For 
35850 65 61 63 68 20 74 6f 2d 74 61 62 6c 65 2c 20 61  each to-table, a
35860 6c 6c 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 0a  ll foreign keys.
35870 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ** associated wi
35880 74 68 20 74 68 61 74 20 74 61 62 6c 65 20 61 72  th that table ar
35890 65 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69  e on a linked li
358a0 73 74 20 75 73 69 6e 67 20 74 68 65 20 46 4b 65  st using the FKe
358b0 79 2e 70 4e 65 78 74 54 6f 0a 2a 2a 20 66 69 65  y.pNextTo.** fie
358c0 6c 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 4b  ld..*/.struct FK
358d0 65 79 20 7b 0a 20 20 54 61 62 6c 65 20 2a 70 46  ey {.  Table *pF
358e0 72 6f 6d 3b 20 20 20 20 20 2f 2a 20 54 68 65 20  rom;     /* The 
358f0 74 61 62 6c 65 20 74 68 61 74 20 63 6f 6e 73 74  table that const
35900 61 69 6e 73 20 74 68 65 20 52 45 46 45 52 45 4e  ains the REFEREN
35910 43 45 53 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  CES clause */.  
35920 46 4b 65 79 20 2a 70 4e 65 78 74 46 72 6f 6d 3b  FKey *pNextFrom;
35930 20 20 2f 2a 20 4e 65 78 74 20 66 6f 72 65 69 67    /* Next foreig
35940 6e 20 6b 65 79 20 69 6e 20 70 46 72 6f 6d 20 2a  n key in pFrom *
35950 2f 0a 20 20 63 68 61 72 20 2a 7a 54 6f 3b 20 20  /.  char *zTo;  
35960 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
35970 20 74 61 62 6c 65 20 74 68 61 74 20 74 68 65 20   table that the 
35980 6b 65 79 20 70 6f 69 6e 74 73 20 74 6f 20 2a 2f  key points to */
35990 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78 74 54 6f  .  FKey *pNextTo
359a0 3b 20 20 20 20 2f 2a 20 4e 65 78 74 20 66 6f 72  ;    /* Next for
359b0 65 69 67 6e 20 6b 65 79 20 74 68 61 74 20 70 6f  eign key that po
359c0 69 6e 74 73 20 74 6f 20 7a 54 6f 20 2a 2f 0a 20  ints to zTo */. 
359d0 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20 20 20 20   int nCol;      
359e0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
359f0 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 69 73 20  columns in this 
35a00 6b 65 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  key */.  struct 
35a10 73 43 6f 6c 4d 61 70 20 7b 20 20 2f 2a 20 4d 61  sColMap {  /* Ma
35a20 70 70 69 6e 67 20 6f 66 20 63 6f 6c 75 6d 6e 73  pping of columns
35a30 20 69 6e 20 70 46 72 6f 6d 20 74 6f 20 63 6f 6c   in pFrom to col
35a40 75 6d 6e 73 20 69 6e 20 7a 54 6f 20 2a 2f 0a 20  umns in zTo */. 
35a50 20 20 20 69 6e 74 20 69 46 72 6f 6d 3b 20 20 20     int iFrom;   
35a60 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
35a70 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 70 46 72 6f  f column in pFro
35a80 6d 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a  m */.    char *z
35a90 43 6f 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e  Col;        /* N
35aa0 61 6d 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e  ame of column in
35ab0 20 7a 54 6f 2e 20 20 49 66 20 30 20 75 73 65 20   zTo.  If 0 use 
35ac0 50 52 49 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 20  PRIMARY KEY */. 
35ad0 20 7d 20 2a 61 43 6f 6c 3b 20 20 20 20 20 20 20   } *aCol;       
35ae0 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20     /* One entry 
35af0 66 6f 72 20 65 61 63 68 20 6f 66 20 6e 43 6f 6c  for each of nCol
35b00 20 63 6f 6c 75 6d 6e 20 73 20 2a 2f 0a 20 20 75   column s */.  u
35b10 38 20 69 73 44 65 66 65 72 72 65 64 3b 20 20 20  8 isDeferred;   
35b20 20 2f 2a 20 54 72 75 65 20 69 66 20 63 6f 6e 73   /* True if cons
35b30 74 72 61 69 6e 74 20 63 68 65 63 6b 69 6e 67 20  traint checking 
35b40 69 73 20 64 65 66 65 72 72 65 64 20 74 69 6c 6c  is deferred till
35b50 20 43 4f 4d 4d 49 54 20 2a 2f 0a 20 20 75 38 20   COMMIT */.  u8 
35b60 75 70 64 61 74 65 43 6f 6e 66 3b 20 20 20 20 2f  updateConf;    /
35b70 2a 20 48 6f 77 20 74 6f 20 72 65 73 6f 6c 76 65  * How to resolve
35b80 20 63 6f 6e 66 6c 69 63 74 73 20 74 68 61 74 20   conflicts that 
35b90 6f 63 63 75 72 20 6f 6e 20 55 50 44 41 54 45 20  occur on UPDATE 
35ba0 2a 2f 0a 20 20 75 38 20 64 65 6c 65 74 65 43 6f  */.  u8 deleteCo
35bb0 6e 66 3b 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f  nf;    /* How to
35bc0 20 72 65 73 6f 6c 76 65 20 63 6f 6e 66 6c 69 63   resolve conflic
35bd0 74 73 20 74 68 61 74 20 6f 63 63 75 72 20 6f 6e  ts that occur on
35be0 20 44 45 4c 45 54 45 20 2a 2f 0a 20 20 75 38 20   DELETE */.  u8 
35bf0 69 6e 73 65 72 74 43 6f 6e 66 3b 20 20 20 20 2f  insertConf;    /
35c00 2a 20 48 6f 77 20 74 6f 20 72 65 73 6f 6c 76 65  * How to resolve
35c10 20 63 6f 6e 66 6c 69 63 74 73 20 74 68 61 74 20   conflicts that 
35c20 6f 63 63 75 72 20 6f 6e 20 49 4e 53 45 52 54 20  occur on INSERT 
35c30 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c  */.};../*.** SQL
35c40 69 74 65 20 73 75 70 70 6f 72 74 73 20 6d 61 6e  ite supports man
35c50 79 20 64 69 66 66 65 72 65 6e 74 20 77 61 79 73  y different ways
35c60 20 74 6f 20 72 65 73 6f 6c 76 65 20 61 20 63 6f   to resolve a co
35c70 6e 74 72 61 69 6e 74 0a 2a 2a 20 65 72 72 6f 72  ntraint.** error
35c80 2e 20 20 52 4f 4c 4c 42 41 43 4b 20 70 72 6f 63  .  ROLLBACK proc
35c90 65 73 73 69 6e 67 20 6d 65 61 6e 73 20 74 68 61  essing means tha
35ca0 74 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 20 76  t a constraint v
35cb0 69 6f 6c 61 74 69 6f 6e 0a 2a 2a 20 63 61 75 73  iolation.** caus
35cc0 65 73 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e  es the operation
35cd0 20 69 6e 20 70 72 6f 63 65 73 73 20 74 6f 20 66   in process to f
35ce0 61 69 6c 20 61 6e 64 20 66 6f 72 20 74 68 65 20  ail and for the 
35cf0 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74  current transact
35d00 69 6f 6e 0a 2a 2a 20 74 6f 20 62 65 20 72 6f 6c  ion.** to be rol
35d10 6c 65 64 20 62 61 63 6b 2e 20 20 41 42 4f 52 54  led back.  ABORT
35d20 20 70 72 6f 63 65 73 73 69 6e 67 20 6d 65 61 6e   processing mean
35d30 73 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20  s the operation 
35d40 69 6e 20 70 72 6f 63 65 73 73 0a 2a 2a 20 66 61  in process.** fa
35d50 69 6c 73 20 61 6e 64 20 61 6e 79 20 70 72 69 6f  ils and any prio
35d60 72 20 63 68 61 6e 67 65 73 20 66 72 6f 6d 20 74  r changes from t
35d70 68 61 74 20 6f 6e 65 20 6f 70 65 72 61 74 69 6f  hat one operatio
35d80 6e 20 61 72 65 20 62 61 63 6b 65 64 20 6f 75 74  n are backed out
35d90 2c 0a 2a 2a 20 62 75 74 20 74 68 65 20 74 72 61  ,.** but the tra
35da0 6e 73 61 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20  nsaction is not 
35db0 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 20 46 41  rolled back.  FA
35dc0 49 4c 20 70 72 6f 63 65 73 73 69 6e 67 20 6d 65  IL processing me
35dd0 61 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 20  ans that.** the 
35de0 6f 70 65 72 61 74 69 6f 6e 20 69 6e 20 70 72 6f  operation in pro
35df0 67 72 65 73 73 20 73 74 6f 70 73 20 61 6e 64 20  gress stops and 
35e00 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72  returns an error
35e10 20 63 6f 64 65 2e 20 20 42 75 74 20 70 72 69 6f   code.  But prio
35e20 72 0a 2a 2a 20 63 68 61 6e 67 65 73 20 64 75 65  r.** changes due
35e30 20 74 6f 20 74 68 65 20 73 61 6d 65 20 6f 70 65   to the same ope
35e40 72 61 74 69 6f 6e 20 61 72 65 20 6e 6f 74 20 62  ration are not b
35e50 61 63 6b 65 64 20 6f 75 74 20 61 6e 64 20 6e 6f  acked out and no
35e60 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6f 63 63   rollback.** occ
35e70 75 72 73 2e 20 20 49 47 4e 4f 52 45 20 6d 65 61  urs.  IGNORE mea
35e80 6e 73 20 74 68 61 74 20 74 68 65 20 70 61 72 74  ns that the part
35e90 69 63 75 6c 61 72 20 72 6f 77 20 74 68 61 74 20  icular row that 
35ea0 63 61 75 73 65 64 20 74 68 65 20 63 6f 6e 73 74  caused the const
35eb0 72 61 69 6e 74 0a 2a 2a 20 65 72 72 6f 72 20 69  raint.** error i
35ec0 73 20 6e 6f 74 20 69 6e 73 65 72 74 65 64 20 6f  s not inserted o
35ed0 72 20 75 70 64 61 74 65 64 2e 20 20 50 72 6f 63  r updated.  Proc
35ee0 65 73 73 69 6e 67 20 63 6f 6e 74 69 6e 75 65 73  essing continues
35ef0 20 61 6e 64 20 6e 6f 20 65 72 72 6f 72 0a 2a 2a   and no error.**
35f00 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 52   is returned.  R
35f10 45 50 4c 41 43 45 20 6d 65 61 6e 73 20 74 68 61  EPLACE means tha
35f20 74 20 70 72 65 65 78 69 73 74 69 6e 67 20 64 61  t preexisting da
35f30 74 61 62 61 73 65 20 72 6f 77 73 20 74 68 61 74  tabase rows that
35f40 20 63 61 75 73 65 64 0a 2a 2a 20 61 20 55 4e 49   caused.** a UNI
35f50 51 55 45 20 63 6f 6e 73 74 72 61 69 6e 74 20 76  QUE constraint v
35f60 69 6f 6c 61 74 69 6f 6e 20 61 72 65 20 72 65 6d  iolation are rem
35f70 6f 76 65 64 20 73 6f 20 74 68 61 74 20 74 68 65  oved so that the
35f80 20 6e 65 77 20 69 6e 73 65 72 74 20 6f 72 0a 2a   new insert or.*
35f90 2a 20 75 70 64 61 74 65 20 63 61 6e 20 70 72 6f  * update can pro
35fa0 63 65 65 64 2e 20 20 50 72 6f 63 65 73 73 69 6e  ceed.  Processin
35fb0 67 20 63 6f 6e 74 69 6e 75 65 73 20 61 6e 64 20  g continues and 
35fc0 6e 6f 20 65 72 72 6f 72 20 69 73 20 72 65 70 6f  no error is repo
35fd0 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 45 53 54  rted..**.** REST
35fe0 52 49 43 54 2c 20 53 45 54 4e 55 4c 4c 2c 20 61  RICT, SETNULL, a
35ff0 6e 64 20 43 41 53 43 41 44 45 20 61 63 74 69 6f  nd CASCADE actio
36000 6e 73 20 61 70 70 6c 79 20 6f 6e 6c 79 20 74 6f  ns apply only to
36010 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 2e 0a 2a   foreign keys..*
36020 2a 20 52 45 53 54 52 49 43 54 20 69 73 20 74 68  * RESTRICT is th
36030 65 20 73 61 6d 65 20 61 73 20 41 42 4f 52 54 20  e same as ABORT 
36040 66 6f 72 20 49 4d 4d 45 44 49 41 54 45 20 66 6f  for IMMEDIATE fo
36050 72 65 69 67 6e 20 6b 65 79 73 20 61 6e 64 20 74  reign keys and t
36060 68 65 0a 2a 2a 20 73 61 6d 65 20 61 73 20 52 4f  he.** same as RO
36070 4c 4c 42 41 43 4b 20 66 6f 72 20 44 45 46 45 52  LLBACK for DEFER
36080 52 45 44 20 6b 65 79 73 2e 20 20 53 45 54 4e 55  RED keys.  SETNU
36090 4c 4c 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  LL means that th
360a0 65 20 66 6f 72 65 69 67 6e 0a 2a 2a 20 6b 65 79  e foreign.** key
360b0 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e   is set to NULL.
360c0 20 20 43 41 53 43 41 44 45 20 6d 65 61 6e 73 20    CASCADE means 
360d0 74 68 61 74 20 61 20 44 45 4c 45 54 45 20 6f 72  that a DELETE or
360e0 20 55 50 44 41 54 45 20 6f 66 20 74 68 65 0a 2a   UPDATE of the.*
360f0 2a 20 72 65 66 65 72 65 6e 63 65 64 20 74 61 62  * referenced tab
36100 6c 65 20 72 6f 77 20 69 73 20 70 72 6f 70 61 67  le row is propag
36110 61 74 65 64 20 69 6e 74 6f 20 74 68 65 20 72 6f  ated into the ro
36120 77 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65  w that holds the
36130 0a 2a 2a 20 66 6f 72 65 69 67 6e 20 6b 65 79 2e  .** foreign key.
36140 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  .** .** The foll
36150 6f 77 69 6e 67 20 73 79 6d 62 6f 6c 69 63 20 76  owing symbolic v
36160 61 6c 75 65 73 20 61 72 65 20 75 73 65 64 20 74  alues are used t
36170 6f 20 72 65 63 6f 72 64 20 77 68 69 63 68 20 74  o record which t
36180 79 70 65 0a 2a 2a 20 6f 66 20 61 63 74 69 6f 6e  ype.** of action
36190 20 74 6f 20 74 61 6b 65 2e 0a 2a 2f 0a 23 64 65   to take..*/.#de
361a0 66 69 6e 65 20 4f 45 5f 4e 6f 6e 65 20 20 20 20  fine OE_None    
361b0 20 30 20 20 20 2f 2a 20 54 68 65 72 65 20 69 73   0   /* There is
361c0 20 6e 6f 20 63 6f 6e 73 74 72 61 69 6e 74 20 74   no constraint t
361d0 6f 20 63 68 65 63 6b 20 2a 2f 0a 23 64 65 66 69  o check */.#defi
361e0 6e 65 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 31  ne OE_Rollback 1
361f0 20 20 20 2f 2a 20 46 61 69 6c 20 74 68 65 20 6f     /* Fail the o
36200 70 65 72 61 74 69 6f 6e 20 61 6e 64 20 72 6f 6c  peration and rol
36210 6c 62 61 63 6b 20 74 68 65 20 74 72 61 6e 73 61  lback the transa
36220 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65  ction */.#define
36230 20 4f 45 5f 41 62 6f 72 74 20 20 20 20 32 20 20   OE_Abort    2  
36240 20 2f 2a 20 42 61 63 6b 20 6f 75 74 20 63 68 61   /* Back out cha
36250 6e 67 65 73 20 62 75 74 20 64 6f 20 6e 6f 20 72  nges but do no r
36260 6f 6c 6c 62 61 63 6b 20 74 72 61 6e 73 61 63 74  ollback transact
36270 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  ion */.#define O
36280 45 5f 46 61 69 6c 20 20 20 20 20 33 20 20 20 2f  E_Fail     3   /
36290 2a 20 53 74 6f 70 20 74 68 65 20 6f 70 65 72 61  * Stop the opera
362a0 74 69 6f 6e 20 62 75 74 20 6c 65 61 76 65 20 61  tion but leave a
362b0 6c 6c 20 70 72 69 6f 72 20 63 68 61 6e 67 65 73  ll prior changes
362c0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 49   */.#define OE_I
362d0 67 6e 6f 72 65 20 20 20 34 20 20 20 2f 2a 20 49  gnore   4   /* I
362e0 67 6e 6f 72 65 20 74 68 65 20 65 72 72 6f 72 2e  gnore the error.
362f0 20 44 6f 20 6e 6f 74 20 64 6f 20 74 68 65 20 49   Do not do the I
36300 4e 53 45 52 54 20 6f 72 20 55 50 44 41 54 45 20  NSERT or UPDATE 
36310 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 52 65  */.#define OE_Re
36320 70 6c 61 63 65 20 20 35 20 20 20 2f 2a 20 44 65  place  5   /* De
36330 6c 65 74 65 20 65 78 69 73 74 69 6e 67 20 72 65  lete existing re
36340 63 6f 72 64 2c 20 74 68 65 6e 20 64 6f 20 49 4e  cord, then do IN
36350 53 45 52 54 20 6f 72 20 55 50 44 41 54 45 20 2a  SERT or UPDATE *
36360 2f 0a 0a 23 64 65 66 69 6e 65 20 4f 45 5f 52 65  /..#define OE_Re
36370 73 74 72 69 63 74 20 36 20 20 20 2f 2a 20 4f 45  strict 6   /* OE
36380 5f 41 62 6f 72 74 20 66 6f 72 20 49 4d 4d 45 44  _Abort for IMMED
36390 49 41 54 45 2c 20 4f 45 5f 52 6f 6c 6c 62 61 63  IATE, OE_Rollbac
363a0 6b 20 66 6f 72 20 44 45 46 45 52 52 45 44 20 2a  k for DEFERRED *
363b0 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 53 65 74  /.#define OE_Set
363c0 4e 75 6c 6c 20 20 37 20 20 20 2f 2a 20 53 65 74  Null  7   /* Set
363d0 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79   the foreign key
363e0 20 76 61 6c 75 65 20 74 6f 20 4e 55 4c 4c 20 2a   value to NULL *
363f0 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 53 65 74  /.#define OE_Set
36400 44 66 6c 74 20 20 38 20 20 20 2f 2a 20 53 65 74  Dflt  8   /* Set
36410 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79   the foreign key
36420 20 76 61 6c 75 65 20 74 6f 20 69 74 73 20 64 65   value to its de
36430 66 61 75 6c 74 20 2a 2f 0a 23 64 65 66 69 6e 65  fault */.#define
36440 20 4f 45 5f 43 61 73 63 61 64 65 20 20 39 20 20   OE_Cascade  9  
36450 20 2f 2a 20 43 61 73 63 61 64 65 20 74 68 65 20   /* Cascade the 
36460 63 68 61 6e 67 65 73 20 2a 2f 0a 0a 23 64 65 66  changes */..#def
36470 69 6e 65 20 4f 45 5f 44 65 66 61 75 6c 74 20 20  ine OE_Default  
36480 39 39 20 20 2f 2a 20 44 6f 20 77 68 61 74 65 76  99  /* Do whatev
36490 65 72 20 74 68 65 20 64 65 66 61 75 6c 74 20 61  er the default a
364a0 63 74 69 6f 6e 20 69 73 20 2a 2f 0a 0a 0a 2f 2a  ction is */.../*
364b0 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
364c0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
364d0 20 73 74 72 75 63 74 75 72 65 20 69 73 20 70 61   structure is pa
364e0 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73  ssed as the firs
364f0 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 6f  t.** argument to
36500 20 73 71 6c 69 74 65 33 56 64 62 65 4b 65 79 43   sqlite3VdbeKeyC
36510 6f 6d 70 61 72 65 20 61 6e 64 20 69 73 20 75 73  ompare and is us
36520 65 64 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 74 68  ed to control th
36530 65 20 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e  e .** comparison
36540 20 6f 66 20 74 68 65 20 74 77 6f 20 69 6e 64 65   of the two inde
36550 78 20 6b 65 79 73 2e 0a 2a 2a 0a 2a 2a 20 49 66  x keys..**.** If
36560 20 74 68 65 20 4b 65 79 49 6e 66 6f 2e 69 6e 63   the KeyInfo.inc
36570 72 4b 65 79 20 76 61 6c 75 65 20 69 73 20 74 72  rKey value is tr
36580 75 65 20 61 6e 64 20 74 68 65 20 63 6f 6d 70 61  ue and the compa
36590 72 69 73 6f 6e 20 77 6f 75 6c 64 0a 2a 2a 20 6f  rison would.** o
365a0 74 68 65 72 77 69 73 65 20 62 65 20 65 71 75 61  therwise be equa
365b0 6c 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20 61  l, then return a
365c0 20 72 65 73 75 6c 74 20 61 73 20 69 66 20 74 68   result as if th
365d0 65 20 73 65 63 6f 6e 64 20 6b 65 79 0a 2a 2a 20  e second key.** 
365e0 77 65 72 65 20 6c 61 72 67 65 72 2e 0a 2a 2f 0a  were larger..*/.
365f0 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 7b  struct KeyInfo {
36600 0a 20 20 75 38 20 65 6e 63 3b 20 20 20 20 20 20  .  u8 enc;      
36610 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 65         /* Text e
36620 6e 63 6f 64 69 6e 67 20 2d 20 6f 6e 65 20 6f 66  ncoding - one of
36630 20 74 68 65 20 54 45 58 54 5f 55 74 66 2a 20 76   the TEXT_Utf* v
36640 61 6c 75 65 73 20 2a 2f 0a 20 20 75 38 20 69 6e  alues */.  u8 in
36650 63 72 4b 65 79 3b 20 20 20 20 20 20 20 20 20 2f  crKey;         /
36660 2a 20 49 6e 63 72 65 61 73 65 20 32 6e 64 20 6b  * Increase 2nd k
36670 65 79 20 62 79 20 65 70 73 69 6c 6f 6e 20 62 65  ey by epsilon be
36680 66 6f 72 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20  fore comparison 
36690 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b  */.  int nField;
366a0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
366b0 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e  er of entries in
366c0 20 61 43 6f 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38   aColl[] */.  u8
366d0 20 2a 61 53 6f 72 74 4f 72 64 65 72 3b 20 20 20   *aSortOrder;   
366e0 20 20 2f 2a 20 49 66 20 64 65 66 69 6e 65 64 20    /* If defined 
366f0 61 6e 20 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d  an aSortOrder[i]
36700 20 69 73 20 74 72 75 65 2c 20 73 6f 72 74 20 44   is true, sort D
36710 45 53 43 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71  ESC */.  CollSeq
36720 20 2a 61 43 6f 6c 6c 5b 31 5d 3b 20 20 2f 2a 20   *aColl[1];  /* 
36730 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  Collating sequen
36740 63 65 20 66 6f 72 20 65 61 63 68 20 74 65 72 6d  ce for each term
36750 20 6f 66 20 74 68 65 20 6b 65 79 20 2a 2f 0a 7d   of the key */.}
36760 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51  ;../*.** Each SQ
36770 4c 20 69 6e 64 65 78 20 69 73 20 72 65 70 72 65  L index is repre
36780 73 65 6e 74 65 64 20 69 6e 20 6d 65 6d 6f 72 79  sented in memory
36790 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e   by an.** instan
367a0 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
367b0 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  ing structure..*
367c0 2a 0a 2a 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 73  *.** The columns
367d0 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 68   of the table th
367e0 61 74 20 61 72 65 20 74 6f 20 62 65 20 69 6e 64  at are to be ind
367f0 65 78 65 64 20 61 72 65 20 64 65 73 63 72 69 62  exed are describ
36800 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 61 69 43  ed.** by the aiC
36810 6f 6c 75 6d 6e 5b 5d 20 66 69 65 6c 64 20 6f 66  olumn[] field of
36820 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e   this structure.
36830 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 73    For example, s
36840 75 70 70 6f 73 65 0a 2a 2a 20 77 65 20 68 61 76  uppose.** we hav
36850 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  e the following 
36860 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 3a  table and index:
36870 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41 54  .**.**     CREAT
36880 45 20 54 41 42 4c 45 20 45 78 31 28 63 31 20 69  E TABLE Ex1(c1 i
36890 6e 74 2c 20 63 32 20 69 6e 74 2c 20 63 33 20 74  nt, c2 int, c3 t
368a0 65 78 74 29 3b 0a 2a 2a 20 20 20 20 20 43 52 45  ext);.**     CRE
368b0 41 54 45 20 49 4e 44 45 58 20 45 78 32 20 4f 4e  ATE INDEX Ex2 ON
368c0 20 45 78 31 28 63 33 2c 63 31 29 3b 0a 2a 2a 0a   Ex1(c3,c1);.**.
368d0 2a 2a 20 49 6e 20 74 68 65 20 54 61 62 6c 65 20  ** In the Table 
368e0 73 74 72 75 63 74 75 72 65 20 64 65 73 63 72 69  structure descri
368f0 62 69 6e 67 20 45 78 31 2c 20 6e 43 6f 6c 3d 3d  bing Ex1, nCol==
36900 33 20 62 65 63 61 75 73 65 20 74 68 65 72 65 20  3 because there 
36910 61 72 65 0a 2a 2a 20 74 68 72 65 65 20 63 6f 6c  are.** three col
36920 75 6d 6e 73 20 69 6e 20 74 68 65 20 74 61 62 6c  umns in the tabl
36930 65 2e 20 20 49 6e 20 74 68 65 20 49 6e 64 65 78  e.  In the Index
36940 20 73 74 72 75 63 74 75 72 65 20 64 65 73 63 72   structure descr
36950 69 62 69 6e 67 0a 2a 2a 20 45 78 32 2c 20 6e 43  ibing.** Ex2, nC
36960 6f 6c 75 6d 6e 3d 3d 32 20 73 69 6e 63 65 20 32  olumn==2 since 2
36970 20 6f 66 20 74 68 65 20 33 20 63 6f 6c 75 6d 6e   of the 3 column
36980 73 20 6f 66 20 45 78 31 20 61 72 65 20 69 6e 64  s of Ex1 are ind
36990 65 78 65 64 2e 0a 2a 2a 20 54 68 65 20 76 61 6c  exed..** The val
369a0 75 65 20 6f 66 20 61 69 43 6f 6c 75 6d 6e 20 69  ue of aiColumn i
369b0 73 20 7b 32 2c 20 30 7d 2e 20 20 61 69 43 6f 6c  s {2, 0}.  aiCol
369c0 75 6d 6e 5b 30 5d 3d 3d 32 20 62 65 63 61 75 73  umn[0]==2 becaus
369d0 65 20 74 68 65 20 0a 2a 2a 20 66 69 72 73 74 20  e the .** first 
369e0 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 69 6e 64  column to be ind
369f0 65 78 65 64 20 28 63 33 29 20 68 61 73 20 61 6e  exed (c3) has an
36a00 20 69 6e 64 65 78 20 6f 66 20 32 20 69 6e 20 45   index of 2 in E
36a10 78 31 2e 61 43 6f 6c 5b 5d 2e 0a 2a 2a 20 54 68  x1.aCol[]..** Th
36a20 65 20 73 65 63 6f 6e 64 20 63 6f 6c 75 6d 6e 20  e second column 
36a30 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 28 63  to be indexed (c
36a40 31 29 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20  1) has an index 
36a50 6f 66 20 30 20 69 6e 0a 2a 2a 20 45 78 31 2e 61  of 0 in.** Ex1.a
36a60 43 6f 6c 5b 5d 2c 20 68 65 6e 63 65 20 45 78 32  Col[], hence Ex2
36a70 2e 61 69 43 6f 6c 75 6d 6e 5b 31 5d 3d 3d 30 2e  .aiColumn[1]==0.
36a80 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 6e 64 65 78  .**.** The Index
36a90 2e 6f 6e 45 72 72 6f 72 20 66 69 65 6c 64 20 64  .onError field d
36aa0 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74 68 65  etermines whethe
36ab0 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 69 6e 64  r or not the ind
36ac0 65 78 65 64 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20  exed columns.** 
36ad0 6d 75 73 74 20 62 65 20 75 6e 69 71 75 65 20 61  must be unique a
36ae0 6e 64 20 77 68 61 74 20 74 6f 20 64 6f 20 69 66  nd what to do if
36af0 20 74 68 65 79 20 61 72 65 20 6e 6f 74 2e 20 20   they are not.  
36b00 57 68 65 6e 20 49 6e 64 65 78 2e 6f 6e 45 72 72  When Index.onErr
36b10 6f 72 3d 4f 45 5f 4e 6f 6e 65 2c 0a 2a 2a 20 69  or=OE_None,.** i
36b20 74 20 6d 65 61 6e 73 20 74 68 69 73 20 69 73 20  t means this is 
36b30 6e 6f 74 20 61 20 75 6e 69 71 75 65 20 69 6e 64  not a unique ind
36b40 65 78 2e 20 20 4f 74 68 65 72 77 69 73 65 20 69  ex.  Otherwise i
36b50 74 20 69 73 20 61 20 75 6e 69 71 75 65 20 69 6e  t is a unique in
36b60 64 65 78 0a 2a 2a 20 61 6e 64 20 74 68 65 20 76  dex.** and the v
36b70 61 6c 75 65 20 6f 66 20 49 6e 64 65 78 2e 6f 6e  alue of Index.on
36b80 45 72 72 6f 72 20 69 6e 64 69 63 61 74 65 20 74  Error indicate t
36b90 68 65 20 77 68 69 63 68 20 63 6f 6e 66 6c 69 63  he which conflic
36ba0 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 0a 2a 2a  t resolution .**
36bb0 20 61 6c 67 6f 72 69 74 68 6d 20 74 6f 20 65 6d   algorithm to em
36bc0 70 6c 6f 79 20 77 68 65 6e 65 76 65 72 20 61 6e  ploy whenever an
36bd0 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65   attempt is made
36be0 20 74 6f 20 69 6e 73 65 72 74 20 61 20 6e 6f 6e   to insert a non
36bf0 2d 75 6e 69 71 75 65 0a 2a 2a 20 65 6c 65 6d 65  -unique.** eleme
36c00 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 49 6e  nt..*/.struct In
36c10 64 65 78 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e  dex {.  char *zN
36c20 61 6d 65 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65  ame;     /* Name
36c30 20 6f 66 20 74 68 69 73 20 69 6e 64 65 78 20 2a   of this index *
36c40 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b  /.  int nColumn;
36c50 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
36c60 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  f columns in the
36c70 20 74 61 62 6c 65 20 75 73 65 64 20 62 79 20 74   table used by t
36c80 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69  his index */.  i
36c90 6e 74 20 2a 61 69 43 6f 6c 75 6d 6e 3b 20 20 20  nt *aiColumn;   
36ca0 2f 2a 20 57 68 69 63 68 20 63 6f 6c 75 6d 6e 73  /* Which columns
36cb0 20 61 72 65 20 75 73 65 64 20 62 79 20 74 68 69   are used by thi
36cc0 73 20 69 6e 64 65 78 2e 20 20 31 73 74 20 69 73  s index.  1st is
36cd0 20 30 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64   0 */.  unsigned
36ce0 20 2a 61 69 52 6f 77 45 73 74 3b 20 2f 2a 20 52   *aiRowEst; /* R
36cf0 65 73 75 6c 74 20 6f 66 20 41 4e 41 4c 59 5a 45  esult of ANALYZE
36d00 3a 20 45 73 74 2e 20 72 6f 77 73 20 73 65 6c 65  : Est. rows sele
36d10 63 74 65 64 20 62 79 20 65 61 63 68 20 63 6f 6c  cted by each col
36d20 75 6d 6e 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  umn */.  Table *
36d30 70 54 61 62 6c 65 3b 20 20 20 2f 2a 20 54 68 65  pTable;   /* The
36d40 20 53 51 4c 20 74 61 62 6c 65 20 62 65 69 6e 67   SQL table being
36d50 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20 20 69 6e   indexed */.  in
36d60 74 20 74 6e 75 6d 3b 20 20 20 20 20 20 20 20 2f  t tnum;        /
36d70 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e  * Page containin
36d80 67 20 72 6f 6f 74 20 6f 66 20 74 68 69 73 20 69  g root of this i
36d90 6e 64 65 78 20 69 6e 20 64 61 74 61 62 61 73 65  ndex in database
36da0 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 6f 6e   file */.  u8 on
36db0 45 72 72 6f 72 3b 20 20 20 20 20 20 2f 2a 20 4f  Error;      /* O
36dc0 45 5f 41 62 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f  E_Abort, OE_Igno
36dd0 72 65 2c 20 4f 45 5f 52 65 70 6c 61 63 65 2c 20  re, OE_Replace, 
36de0 6f 72 20 4f 45 5f 4e 6f 6e 65 20 2a 2f 0a 20 20  or OE_None */.  
36df0 75 38 20 61 75 74 6f 49 6e 64 65 78 3b 20 20 20  u8 autoIndex;   
36e00 20 2f 2a 20 54 72 75 65 20 69 66 20 69 73 20 61   /* True if is a
36e10 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 72 65  utomatically cre
36e20 61 74 65 64 20 28 65 78 3a 20 62 79 20 55 4e 49  ated (ex: by UNI
36e30 51 55 45 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a  QUE) */.  char *
36e40 7a 43 6f 6c 41 66 66 3b 20 20 20 2f 2a 20 53 74  zColAff;   /* St
36e50 72 69 6e 67 20 64 65 66 69 6e 69 6e 67 20 74 68  ring defining th
36e60 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 65 61  e affinity of ea
36e70 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 49  ch column */.  I
36e80 6e 64 65 78 20 2a 70 4e 65 78 74 3b 20 20 20 20  ndex *pNext;    
36e90 2f 2a 20 54 68 65 20 6e 65 78 74 20 69 6e 64 65  /* The next inde
36ea0 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  x associated wit
36eb0 68 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65  h the same table
36ec0 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53   */.  Schema *pS
36ed0 63 68 65 6d 61 3b 20 2f 2a 20 53 63 68 65 6d 61  chema; /* Schema
36ee0 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 69 73   containing this
36ef0 20 69 6e 64 65 78 20 2a 2f 0a 20 20 75 38 20 2a   index */.  u8 *
36f00 61 53 6f 72 74 4f 72 64 65 72 3b 20 20 2f 2a 20  aSortOrder;  /* 
36f10 41 72 72 61 79 20 6f 66 20 73 69 7a 65 20 49 6e  Array of size In
36f20 64 65 78 2e 6e 43 6f 6c 75 6d 6e 2e 20 54 72 75  dex.nColumn. Tru
36f30 65 3d 3d 44 45 53 43 2c 20 46 61 6c 73 65 3d 3d  e==DESC, False==
36f40 41 53 43 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a  ASC */.  char **
36f50 61 7a 43 6f 6c 6c 3b 20 20 20 2f 2a 20 41 72 72  azColl;   /* Arr
36f60 61 79 20 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e 20  ay of collation 
36f70 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 73 20 66  sequence names f
36f80 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 7d 3b 0a 0a  or index */.};..
36f90 2f 2a 0a 2a 2a 20 45 61 63 68 20 74 6f 6b 65 6e  /*.** Each token
36fa0 20 63 6f 6d 69 6e 67 20 6f 75 74 20 6f 66 20 74   coming out of t
36fb0 68 65 20 6c 65 78 65 72 20 69 73 20 61 6e 20 69  he lexer is an i
36fc0 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68  nstance of.** th
36fd0 69 73 20 73 74 72 75 63 74 75 72 65 2e 20 20 54  is structure.  T
36fe0 6f 6b 65 6e 73 20 61 72 65 20 61 6c 73 6f 20 75  okens are also u
36ff0 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61  sed as part of a
37000 6e 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a  n expression..**
37010 0a 2a 2a 20 4e 6f 74 65 20 69 66 20 54 6f 6b 65  .** Note if Toke
37020 6e 2e 7a 3d 3d 30 20 74 68 65 6e 20 54 6f 6b 65  n.z==0 then Toke
37030 6e 2e 64 79 6e 20 61 6e 64 20 54 6f 6b 65 6e 2e  n.dyn and Token.
37040 6e 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 20  n are undefined 
37050 61 6e 64 0a 2a 2a 20 6d 61 79 20 63 6f 6e 74 61  and.** may conta
37060 69 6e 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 73  in random values
37070 2e 20 20 44 6f 20 6e 6f 74 20 6d 61 6b 65 20 61  .  Do not make a
37080 6e 79 20 61 73 73 75 70 74 69 6f 6e 73 20 61 62  ny assuptions ab
37090 6f 75 74 20 54 6f 6b 65 6e 2e 64 79 6e 0a 2a 2a  out Token.dyn.**
370a0 20 61 6e 64 20 54 6f 6b 65 6e 2e 6e 20 77 68 65   and Token.n whe
370b0 6e 20 54 6f 6b 65 6e 2e 7a 3d 3d 30 2e 0a 2a 2f  n Token.z==0..*/
370c0 0a 73 74 72 75 63 74 20 54 6f 6b 65 6e 20 7b 0a  .struct Token {.
370d0 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
370e0 20 63 68 61 72 20 2a 7a 3b 20 2f 2a 20 54 65 78   char *z; /* Tex
370f0 74 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 2e 20  t of the token. 
37100 20 4e 6f 74 20 4e 55 4c 4c 2d 74 65 72 6d 69 6e   Not NULL-termin
37110 61 74 65 64 21 20 2a 2f 0a 20 20 75 6e 73 69 67  ated! */.  unsig
37120 6e 65 64 20 64 79 6e 20 20 3a 20 31 3b 20 20 20  ned dyn  : 1;   
37130 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 6d     /* True for m
37140 61 6c 6c 6f 63 65 64 20 6d 65 6d 6f 72 79 2c 20  alloced memory, 
37150 66 61 6c 73 65 20 66 6f 72 20 73 74 61 74 69 63  false for static
37160 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 6e   */.  unsigned n
37170 20 20 20 20 3a 20 33 31 3b 20 20 20 20 20 2f 2a      : 31;     /*
37180 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61   Number of chara
37190 63 74 65 72 73 20 69 6e 20 74 68 69 73 20 74 6f  cters in this to
371a0 6b 65 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ken */.};../*.**
371b0 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
371c0 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 63  this structure c
371d0 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61 74  ontains informat
371e0 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 67 65  ion needed to ge
371f0 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66  nerate.** code f
37200 6f 72 20 61 20 53 45 4c 45 43 54 20 74 68 61 74  or a SELECT that
37210 20 63 6f 6e 74 61 69 6e 73 20 61 67 67 72 65 67   contains aggreg
37220 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a  ate functions..*
37230 2a 0a 2a 2a 20 49 66 20 45 78 70 72 2e 6f 70 3d  *.** If Expr.op=
37240 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 6f  =TK_AGG_COLUMN o
37250 72 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f  r TK_AGG_FUNCTIO
37260 4e 20 74 68 65 6e 20 45 78 70 72 2e 70 41 67 67  N then Expr.pAgg
37270 49 6e 66 6f 20 69 73 20 61 0a 2a 2a 20 70 6f 69  Info is a.** poi
37280 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74 72  nter to this str
37290 75 63 74 75 72 65 2e 20 20 54 68 65 20 45 78 70  ucture.  The Exp
372a0 72 2e 69 43 6f 6c 75 6d 6e 20 66 69 65 6c 64 20  r.iColumn field 
372b0 69 73 20 74 68 65 20 69 6e 64 65 78 20 69 6e 0a  is the index in.
372c0 2a 2a 20 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 5b  ** AggInfo.aCol[
372d0 5d 20 6f 72 20 41 67 67 49 6e 66 6f 2e 61 46 75  ] or AggInfo.aFu
372e0 6e 63 5b 5d 20 6f 66 20 69 6e 66 6f 72 6d 61 74  nc[] of informat
372f0 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 67 65  ion needed to ge
37300 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66  nerate.** code f
37310 6f 72 20 74 68 61 74 20 6e 6f 64 65 2e 0a 2a 2a  or that node..**
37320 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 70 47 72 6f  .** AggInfo.pGro
37330 75 70 42 79 20 61 6e 64 20 41 67 67 49 6e 66 6f  upBy and AggInfo
37340 2e 61 46 75 6e 63 2e 70 45 78 70 72 20 70 6f 69  .aFunc.pExpr poi
37350 6e 74 20 74 6f 20 66 69 65 6c 64 73 20 77 69 74  nt to fields wit
37360 68 69 6e 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69  hin the.** origi
37370 6e 61 6c 20 53 65 6c 65 63 74 20 73 74 72 75 63  nal Select struc
37380 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72 69  ture that descri
37390 62 65 73 20 74 68 65 20 53 45 4c 45 43 54 20 73  bes the SELECT s
373a0 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 73 65  tatement.  These
373b0 0a 2a 2a 20 66 69 65 6c 64 73 20 64 6f 20 6e 6f  .** fields do no
373c0 74 20 6e 65 65 64 20 74 6f 20 62 65 20 66 72 65  t need to be fre
373d0 65 64 20 77 68 65 6e 20 64 65 61 6c 6c 6f 63 61  ed when dealloca
373e0 74 69 6e 67 20 74 68 65 20 41 67 67 49 6e 66 6f  ting the AggInfo
373f0 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73   structure..*/.s
37400 74 72 75 63 74 20 41 67 67 49 6e 66 6f 20 7b 0a  truct AggInfo {.
37410 20 20 75 38 20 64 69 72 65 63 74 4d 6f 64 65 3b    u8 directMode;
37420 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69 72            /* Dir
37430 65 63 74 20 72 65 6e 64 65 72 69 6e 67 20 6d 6f  ect rendering mo
37440 64 65 20 6d 65 61 6e 73 20 74 61 6b 65 20 64 61  de means take da
37450 74 61 20 64 69 72 65 63 74 6c 79 0a 20 20 20 20  ta directly.    
37460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37470 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 73 6f        ** from so
37480 75 72 63 65 20 74 61 62 6c 65 73 20 72 61 74 68  urce tables rath
37490 65 72 20 74 68 61 6e 20 66 72 6f 6d 20 61 63 63  er than from acc
374a0 75 6d 75 6c 61 74 6f 72 73 20 2a 2f 0a 20 20 75  umulators */.  u
374b0 38 20 75 73 65 53 6f 72 74 69 6e 67 49 64 78 3b  8 useSortingIdx;
374c0 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 64 69 72         /* In dir
374d0 65 63 74 20 6d 6f 64 65 2c 20 72 65 66 65 72 65  ect mode, refere
374e0 6e 63 65 20 74 68 65 20 73 6f 72 74 69 6e 67 20  nce the sorting 
374f0 69 6e 64 65 78 20 72 61 74 68 65 72 0a 20 20 20  index rather.   
37500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37510 20 20 20 20 20 20 20 2a 2a 20 74 68 61 6e 20 74         ** than t
37520 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20  he source table 
37530 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 69 6e 67  */.  int sorting
37540 49 64 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Idx;         /* 
37550 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66  Cursor number of
37560 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64   the sorting ind
37570 65 78 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  ex */.  ExprList
37580 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 20 20 20   *pGroupBy;     
37590 2f 2a 20 54 68 65 20 67 72 6f 75 70 20 62 79 20  /* The group by 
375a0 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20  clause */.  int 
375b0 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 3b 20  nSortingColumn; 
375c0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
375d0 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
375e0 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 2a 2f  sorting index */
375f0 0a 20 20 73 74 72 75 63 74 20 41 67 67 49 6e 66  .  struct AggInf
37600 6f 5f 63 6f 6c 20 7b 20 20 20 20 2f 2a 20 46 6f  o_col {    /* Fo
37610 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 75 73  r each column us
37620 65 64 20 69 6e 20 73 6f 75 72 63 65 20 74 61 62  ed in source tab
37630 6c 65 73 20 2a 2f 0a 20 20 20 20 54 61 62 6c 65  les */.    Table
37640 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20   *pTab;         
37650 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 74 61      /* Source ta
37660 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ble */.    int i
37670 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20  Table;          
37680 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75      /* Cursor nu
37690 6d 62 65 72 20 6f 66 20 74 68 65 20 73 6f 75 72  mber of the sour
376a0 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  ce table */.    
376b0 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20  int iColumn;    
376c0 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75           /* Colu
376d0 6d 6e 20 6e 75 6d 62 65 72 20 77 69 74 68 69 6e  mn number within
376e0 20 74 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c   the source tabl
376f0 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 53 6f  e */.    int iSo
37700 72 74 65 72 43 6f 6c 75 6d 6e 3b 20 20 20 20 20  rterColumn;     
37710 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62    /* Column numb
37720 65 72 20 69 6e 20 74 68 65 20 73 6f 72 74 69 6e  er in the sortin
37730 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 69  g index */.    i
37740 6e 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 20 20  nt iMem;        
37750 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72          /* Memor
37760 79 20 6c 6f 63 61 74 69 6f 6e 20 74 68 61 74 20  y location that 
37770 61 63 74 73 20 61 73 20 61 63 63 75 6d 75 6c 61  acts as accumula
37780 74 6f 72 20 2a 2f 0a 20 20 20 20 45 78 70 72 20  tor */.    Expr 
37790 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 20  *pExpr;         
377a0 20 20 20 20 2f 2a 20 54 68 65 20 6f 72 69 67 69      /* The origi
377b0 6e 61 6c 20 65 78 70 72 65 73 73 69 6f 6e 20 2a  nal expression *
377c0 2f 0a 20 20 7d 20 2a 61 43 6f 6c 3b 0a 20 20 69  /.  } *aCol;.  i
377d0 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20  nt nColumn;     
377e0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
377f0 20 6f 66 20 75 73 65 64 20 65 6e 74 72 69 65 73   of used entries
37800 20 69 6e 20 61 43 6f 6c 5b 5d 20 2a 2f 0a 20 20   in aCol[] */.  
37810 69 6e 74 20 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f 63  int nColumnAlloc
37820 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  ;       /* Numbe
37830 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63  r of slots alloc
37840 61 74 65 64 20 66 6f 72 20 61 43 6f 6c 5b 5d 20  ated for aCol[] 
37850 2a 2f 0a 20 20 69 6e 74 20 6e 41 63 63 75 6d 75  */.  int nAccumu
37860 6c 61 74 6f 72 3b 20 20 20 20 20 20 20 2f 2a 20  lator;       /* 
37870 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  Number of column
37880 73 20 74 68 61 74 20 73 68 6f 77 20 74 68 72 6f  s that show thro
37890 75 67 68 20 74 6f 20 74 68 65 20 6f 75 74 70 75  ugh to the outpu
378a0 74 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t..             
378b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20               ** 
378c0 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6c 75 6d  Additional colum
378d0 6e 73 20 61 72 65 20 75 73 65 64 20 6f 6e 6c 79  ns are used only
378e0 20 61 73 20 70 61 72 61 6d 65 74 65 72 73 20 74   as parameters t
378f0 6f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  o.              
37900 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61              ** a
37910 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
37920 6e 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 41  ns */.  struct A
37930 67 67 49 6e 66 6f 5f 66 75 6e 63 20 7b 20 20 20  ggInfo_func {   
37940 2f 2a 20 46 6f 72 20 65 61 63 68 20 61 67 67 72  /* For each aggr
37950 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 2a  egate function *
37960 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78 70  /.    Expr *pExp
37970 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  r;             /
37980 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 65 6e 63  * Expression enc
37990 6f 64 69 6e 67 20 74 68 65 20 66 75 6e 63 74 69  oding the functi
379a0 6f 6e 20 2a 2f 0a 20 20 20 20 46 75 6e 63 44 65  on */.    FuncDe
379b0 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20  f *pFunc;       
379c0 20 20 20 2f 2a 20 54 68 65 20 61 67 67 72 65 67     /* The aggreg
379d0 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70  ate function imp
379e0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20  lementation */. 
379f0 20 20 20 69 6e 74 20 69 4d 65 6d 3b 20 20 20 20     int iMem;    
37a00 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
37a10 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74  emory location t
37a20 68 61 74 20 61 63 74 73 20 61 73 20 61 63 63 75  hat acts as accu
37a30 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 20 69  mulator */.    i
37a40 6e 74 20 69 44 69 73 74 69 6e 63 74 3b 20 20 20  nt iDistinct;   
37a50 20 20 20 20 20 20 20 20 2f 2a 20 45 70 68 65 72          /* Epher
37a60 6d 65 72 61 6c 20 74 61 62 6c 65 20 75 73 65 64  meral table used
37a70 20 74 6f 20 65 6e 66 6f 72 63 65 20 44 49 53 54   to enforce DIST
37a80 49 4e 43 54 20 2a 2f 0a 20 20 7d 20 2a 61 46 75  INCT */.  } *aFu
37a90 6e 63 3b 0a 20 20 69 6e 74 20 6e 46 75 6e 63 3b  nc;.  int nFunc;
37aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
37ab0 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
37ac0 65 73 20 69 6e 20 61 46 75 6e 63 5b 5d 20 2a 2f  es in aFunc[] */
37ad0 0a 20 20 69 6e 74 20 6e 46 75 6e 63 41 6c 6c 6f  .  int nFuncAllo
37ae0 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  c;         /* Nu
37af0 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c  mber of slots al
37b00 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 46 75 6e  located for aFun
37b10 63 5b 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  c[] */.};../*.**
37b20 20 45 61 63 68 20 6e 6f 64 65 20 6f 66 20 61 6e   Each node of an
37b30 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74   expression in t
37b40 68 65 20 70 61 72 73 65 20 74 72 65 65 20 69 73  he parse tree is
37b50 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20   an instance.** 
37b60 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
37b70 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 6f 70  e..**.** Expr.op
37b80 20 69 73 20 74 68 65 20 6f 70 63 6f 64 65 2e 20   is the opcode. 
37b90 20 54 68 65 20 69 6e 74 65 67 65 72 20 70 61 72   The integer par
37ba0 73 65 72 20 74 6f 6b 65 6e 20 63 6f 64 65 73 20  ser token codes 
37bb0 61 72 65 20 72 65 75 73 65 64 0a 2a 2a 20 61 73  are reused.** as
37bc0 20 6f 70 63 6f 64 65 73 20 68 65 72 65 2e 20 20   opcodes here.  
37bd0 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65  For example, the
37be0 20 70 61 72 73 65 72 20 64 65 66 69 6e 65 73 20   parser defines 
37bf0 54 4b 5f 47 45 20 74 6f 20 62 65 20 61 6e 20 69  TK_GE to be an i
37c00 6e 74 65 67 65 72 0a 2a 2a 20 63 6f 64 65 20 72  nteger.** code r
37c10 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20  epresenting the 
37c20 22 3e 3d 22 20 6f 70 65 72 61 74 6f 72 2e 20 20  ">=" operator.  
37c30 54 68 69 73 20 73 61 6d 65 20 69 6e 74 65 67 65  This same intege
37c40 72 20 63 6f 64 65 20 69 73 20 72 65 75 73 65 64  r code is reused
37c50 0a 2a 2a 20 74 6f 20 72 65 70 72 65 73 65 6e 74  .** to represent
37c60 20 74 68 65 20 67 72 65 61 74 65 72 2d 74 68 61   the greater-tha
37c70 6e 2d 6f 72 2d 65 71 75 61 6c 2d 74 6f 20 6f 70  n-or-equal-to op
37c80 65 72 61 74 6f 72 20 69 6e 20 74 68 65 20 65 78  erator in the ex
37c90 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 65  pression.** tree
37ca0 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 70 52 69  ..**.** Expr.pRi
37cb0 67 68 74 20 61 6e 64 20 45 78 70 72 2e 70 4c 65  ght and Expr.pLe
37cc0 66 74 20 61 72 65 20 73 75 62 65 78 70 72 65 73  ft are subexpres
37cd0 73 69 6f 6e 73 2e 20 20 45 78 70 72 2e 70 4c 69  sions.  Expr.pLi
37ce0 73 74 20 69 73 20 61 20 6c 69 73 74 0a 2a 2a 20  st is a list.** 
37cf0 6f 66 20 61 72 67 75 6d 65 6e 74 20 69 66 20 74  of argument if t
37d00 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
37d10 20 61 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a   a function..**.
37d20 2a 2a 20 45 78 70 72 2e 74 6f 6b 65 6e 20 69 73  ** Expr.token is
37d30 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20 74 6f   the operator to
37d40 6b 65 6e 20 66 6f 72 20 74 68 69 73 20 6e 6f 64  ken for this nod
37d50 65 2e 20 20 46 6f 72 20 73 6f 6d 65 20 65 78 70  e.  For some exp
37d60 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 74 68 61 74  ressions.** that
37d70 20 68 61 76 65 20 73 75 62 65 78 70 72 65 73 73   have subexpress
37d80 69 6f 6e 73 2c 20 45 78 70 72 2e 74 6f 6b 65 6e  ions, Expr.token
37d90 20 63 61 6e 20 62 65 20 74 68 65 20 63 6f 6d 70   can be the comp
37da0 6c 65 74 65 20 74 65 78 74 20 74 68 61 74 20 67  lete text that g
37db0 61 76 65 0a 2a 2a 20 72 69 73 65 20 74 6f 20 74  ave.** rise to t
37dc0 68 65 20 45 78 70 72 2e 20 20 49 6e 20 74 68 65  he Expr.  In the
37dd0 20 6c 61 74 74 65 72 20 63 61 73 65 2c 20 74 68   latter case, th
37de0 65 20 74 6f 6b 65 6e 20 69 73 20 6d 61 72 6b 65  e token is marke
37df0 64 20 61 73 20 62 65 69 6e 67 0a 2a 2a 20 61 20  d as being.** a 
37e00 63 6f 6d 70 6f 75 6e 64 20 74 6f 6b 65 6e 2e 0a  compound token..
37e10 2a 2a 0a 2a 2a 20 41 6e 20 65 78 70 72 65 73 73  **.** An express
37e20 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  ion of the form 
37e30 49 44 20 6f 72 20 49 44 2e 49 44 20 72 65 66 65  ID or ID.ID refe
37e40 72 73 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 69  rs to a column i
37e50 6e 20 61 20 74 61 62 6c 65 2e 0a 2a 2a 20 46 6f  n a table..** Fo
37e60 72 20 73 75 63 68 20 65 78 70 72 65 73 73 69 6f  r such expressio
37e70 6e 73 2c 20 45 78 70 72 2e 6f 70 20 69 73 20 73  ns, Expr.op is s
37e80 65 74 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 20  et to TK_COLUMN 
37e90 61 6e 64 20 45 78 70 72 2e 69 54 61 62 6c 65 20  and Expr.iTable 
37ea0 69 73 0a 2a 2a 20 74 68 65 20 69 6e 74 65 67 65  is.** the intege
37eb0 72 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  r cursor number 
37ec0 6f 66 20 61 20 56 44 42 45 20 63 75 72 73 6f 72  of a VDBE cursor
37ed0 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 61   pointing to tha
37ee0 74 20 74 61 62 6c 65 20 61 6e 64 0a 2a 2a 20 45  t table and.** E
37ef0 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 69 73 20 74  xpr.iColumn is t
37f00 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  he column number
37f10 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69   for the specifi
37f20 63 20 63 6f 6c 75 6d 6e 2e 20 20 49 66 20 74 68  c column.  If th
37f30 65 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20  e.** expression 
37f40 69 73 20 75 73 65 64 20 61 73 20 61 20 72 65 73  is used as a res
37f50 75 6c 74 20 69 6e 20 61 6e 20 61 67 67 72 65 67  ult in an aggreg
37f60 61 74 65 20 53 45 4c 45 43 54 2c 20 74 68 65 6e  ate SELECT, then
37f70 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 69 73   the.** value is
37f80 20 61 6c 73 6f 20 73 74 6f 72 65 64 20 69 6e 20   also stored in 
37f90 74 68 65 20 45 78 70 72 2e 69 41 67 67 20 63 6f  the Expr.iAgg co
37fa0 6c 75 6d 6e 20 69 6e 20 74 68 65 20 61 67 67 72  lumn in the aggr
37fb0 65 67 61 74 65 20 73 6f 20 74 68 61 74 0a 2a 2a  egate so that.**
37fc0 20 69 74 20 63 61 6e 20 62 65 20 61 63 63 65 73   it can be acces
37fd0 73 65 64 20 61 66 74 65 72 20 61 6c 6c 20 61 67  sed after all ag
37fe0 67 72 65 67 61 74 65 73 20 61 72 65 20 63 6f 6d  gregates are com
37ff0 70 75 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  puted..**.** If 
38000 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
38010 73 20 61 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68  s a function, th
38020 65 20 45 78 70 72 2e 69 54 61 62 6c 65 20 69 73  e Expr.iTable is
38030 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 64 65   an integer code
38040 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 69 6e 67  .** representing
38050 20 77 68 69 63 68 20 66 75 6e 63 74 69 6f 6e 2e   which function.
38060 20 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73    If the express
38070 69 6f 6e 20 69 73 20 61 6e 20 75 6e 62 6f 75 6e  ion is an unboun
38080 64 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d 61  d variable.** ma
38090 72 6b 65 72 20 28 61 20 71 75 65 73 74 69 6f 6e  rker (a question
380a0 20 6d 61 72 6b 20 63 68 61 72 61 63 74 65 72 20   mark character 
380b0 27 3f 27 20 69 6e 20 74 68 65 20 6f 72 69 67 69  '?' in the origi
380c0 6e 61 6c 20 53 51 4c 29 20 74 68 65 6e 20 74 68  nal SQL) then th
380d0 65 0a 2a 2a 20 45 78 70 72 2e 69 54 61 62 6c 65  e.** Expr.iTable
380e0 20 68 6f 6c 64 73 20 74 68 65 20 69 6e 64 65 78   holds the index
380f0 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 61 74   number for that
38100 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a   variable..**.**
38110 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69   If the expressi
38120 6f 6e 20 69 73 20 61 20 73 75 62 71 75 65 72 79  on is a subquery
38130 20 74 68 65 6e 20 45 78 70 72 2e 69 43 6f 6c 75   then Expr.iColu
38140 6d 6e 20 68 6f 6c 64 73 20 61 6e 20 69 6e 74 65  mn holds an inte
38150 67 65 72 0a 2a 2a 20 72 65 67 69 73 74 65 72 20  ger.** register 
38160 6e 75 6d 62 65 72 20 63 6f 6e 74 61 69 6e 69 6e  number containin
38170 67 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20  g the result of 
38180 74 68 65 20 73 75 62 71 75 65 72 79 2e 20 20 49  the subquery.  I
38190 66 20 74 68 65 0a 2a 2a 20 73 75 62 71 75 65 72  f the.** subquer
381a0 79 20 67 69 76 65 73 20 61 20 63 6f 6e 73 74 61  y gives a consta
381b0 6e 74 20 72 65 73 75 6c 74 2c 20 74 68 65 6e 20  nt result, then 
381c0 69 54 61 62 6c 65 20 69 73 20 2d 31 2e 20 20 49  iTable is -1.  I
381d0 66 20 74 68 65 20 73 75 62 71 75 65 72 79 0a 2a  f the subquery.*
381e0 2a 20 67 69 76 65 73 20 61 20 64 69 66 66 65 72  * gives a differ
381f0 65 6e 74 20 61 6e 73 77 65 72 20 61 74 20 64 69  ent answer at di
38200 66 66 65 72 65 6e 74 20 74 69 6d 65 73 20 64 75  fferent times du
38210 72 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 70  ring statement p
38220 72 6f 63 65 73 73 69 6e 67 0a 2a 2a 20 74 68 65  rocessing.** the
38230 6e 20 69 54 61 62 6c 65 20 69 73 20 74 68 65 20  n iTable is the 
38240 61 64 64 72 65 73 73 20 6f 66 20 61 20 73 75 62  address of a sub
38250 72 6f 75 74 69 6e 65 20 74 68 61 74 20 63 6f 6d  routine that com
38260 70 75 74 65 73 20 74 68 65 20 73 75 62 71 75 65  putes the subque
38270 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 45 78  ry..**.** The Ex
38280 70 72 2e 70 53 65 6c 65 63 74 20 66 69 65 6c 64  pr.pSelect field
38290 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 53 45 4c   points to a SEL
382a0 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  ECT statement.  
382b0 54 68 65 20 53 45 4c 45 43 54 20 6d 69 67 68 74  The SELECT might
382c0 0a 2a 2a 20 62 65 20 74 68 65 20 72 69 67 68 74  .** be the right
382d0 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 6e 20 49   operand of an I
382e0 4e 20 6f 70 65 72 61 74 6f 72 2e 20 20 4f 72 2c  N operator.  Or,
382f0 20 69 66 20 61 20 73 63 61 6c 61 72 20 53 45 4c   if a scalar SEL
38300 45 43 54 20 61 70 70 65 61 72 73 0a 2a 2a 20 69  ECT appears.** i
38310 6e 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  n an expression 
38320 74 68 65 20 6f 70 63 6f 64 65 20 69 73 20 54 4b  the opcode is TK
38330 5f 53 45 4c 45 43 54 20 61 6e 64 20 45 78 70 72  _SELECT and Expr
38340 2e 70 53 65 6c 65 63 74 20 69 73 20 74 68 65 20  .pSelect is the 
38350 6f 6e 6c 79 0a 2a 2a 20 6f 70 65 72 61 6e 64 2e  only.** operand.
38360 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 45 78  .**.** If the Ex
38370 70 72 20 69 73 20 6f 66 20 74 79 70 65 20 4f 50  pr is of type OP
38380 5f 43 6f 6c 75 6d 6e 2c 20 61 6e 64 20 74 68 65  _Column, and the
38390 20 74 61 62 6c 65 20 69 74 20 69 73 20 73 65 6c   table it is sel
383a0 65 63 74 69 6e 67 20 66 72 6f 6d 0a 2a 2a 20 69  ecting from.** i
383b0 73 20 61 20 64 69 73 6b 20 74 61 62 6c 65 20 6f  s a disk table o
383c0 72 20 74 68 65 20 22 6f 6c 64 2e 2a 22 20 70 73  r the "old.*" ps
383d0 65 75 64 6f 2d 74 61 62 6c 65 2c 20 74 68 65 6e  eudo-table, then
383e0 20 70 54 61 62 20 70 6f 69 6e 74 73 20 74 6f 20   pTab points to 
383f0 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e  the.** correspon
38400 64 69 6e 67 20 74 61 62 6c 65 20 64 65 66 69 6e  ding table defin
38410 69 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74  ition..*/.struct
38420 20 45 78 70 72 20 7b 0a 20 20 75 38 20 6f 70 3b   Expr {.  u8 op;
38430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38440 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 70 65   /* Operation pe
38450 72 66 6f 72 6d 65 64 20 62 79 20 74 68 69 73 20  rformed by this 
38460 6e 6f 64 65 20 2a 2f 0a 20 20 63 68 61 72 20 61  node */.  char a
38470 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 20 20  ffinity;        
38480 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79   /* The affinity
38490 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f   of the column o
384a0 72 20 30 20 69 66 20 6e 6f 74 20 61 20 63 6f 6c  r 0 if not a col
384b0 75 6d 6e 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61  umn */.  u16 fla
384c0 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  gs;             
384d0 2f 2a 20 56 61 72 69 6f 75 73 20 66 6c 61 67 73  /* Various flags
384e0 2e 20 20 53 65 65 20 62 65 6c 6f 77 20 2a 2f 0a  .  See below */.
384f0 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
38500 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ;        /* The 
38510 63 6f 6c 6c 61 74 69 6f 6e 20 74 79 70 65 20 6f  collation type o
38520 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 72 20  f the column or 
38530 30 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c 65  0 */.  Expr *pLe
38540 66 74 2c 20 2a 70 52 69 67 68 74 3b 20 20 2f 2a  ft, *pRight;  /*
38550 20 4c 65 66 74 20 61 6e 64 20 72 69 67 68 74 20   Left and right 
38560 73 75 62 6e 6f 64 65 73 20 2a 2f 0a 20 20 45 78  subnodes */.  Ex
38570 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 3b 20 20  prList *pList;  
38580 20 20 20 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f       /* A list o
38590 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 75 73  f expressions us
385a0 65 64 20 61 73 20 66 75 6e 63 74 69 6f 6e 20 61  ed as function a
385b0 72 67 75 6d 65 6e 74 73 0a 20 20 20 20 20 20 20  rguments.       
385c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
385d0 20 20 2a 2a 20 6f 72 20 69 6e 20 22 3c 65 78 70    ** or in "<exp
385e0 72 3e 20 49 4e 20 28 3c 65 78 70 72 2d 6c 69 73  r> IN (<expr-lis
385f0 74 29 22 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 74  t)" */.  Token t
38600 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20  oken;           
38610 2f 2a 20 41 6e 20 6f 70 65 72 61 6e 64 20 74 6f  /* An operand to
38620 6b 65 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 73  ken */.  Token s
38630 70 61 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  pan;            
38640 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 74 65 78 74  /* Complete text
38650 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69   of the expressi
38660 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62  on */.  int iTab
38670 6c 65 2c 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 2f  le, iColumn;   /
38680 2a 20 57 68 65 6e 20 6f 70 3d 3d 54 4b 5f 43 4f  * When op==TK_CO
38690 4c 55 4d 4e 2c 20 74 68 65 6e 20 74 68 69 73 20  LUMN, then this 
386a0 65 78 70 72 20 6e 6f 64 65 20 6d 65 61 6e 73 20  expr node means 
386b0 74 68 65 0a 20 20 20 20 20 20 20 20 20 20 20 20  the.            
386c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20               ** 
386d0 69 43 6f 6c 75 6d 6e 2d 74 68 20 66 69 65 6c 64  iColumn-th field
386e0 20 6f 66 20 74 68 65 20 69 54 61 62 6c 65 2d 74   of the iTable-t
386f0 68 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 41 67  h table. */.  Ag
38700 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 3b  gInfo *pAggInfo;
38710 20 20 20 20 20 2f 2a 20 55 73 65 64 20 62 79 20       /* Used by 
38720 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 61 6e  TK_AGG_COLUMN an
38730 64 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f  d TK_AGG_FUNCTIO
38740 4e 20 2a 2f 0a 20 20 69 6e 74 20 69 41 67 67 3b  N */.  int iAgg;
38750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
38760 20 57 68 69 63 68 20 65 6e 74 72 79 20 69 6e 20   Which entry in 
38770 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d  pAggInfo->aCol[]
38780 20 6f 72 20 2d 3e 61 46 75 6e 63 5b 5d 20 2a 2f   or ->aFunc[] */
38790 0a 20 20 69 6e 74 20 69 52 69 67 68 74 4a 6f 69  .  int iRightJoi
387a0 6e 54 61 62 6c 65 3b 20 20 20 2f 2a 20 49 66 20  nTable;   /* If 
387b0 45 50 5f 46 72 6f 6d 4a 6f 69 6e 2c 20 74 68 65  EP_FromJoin, the
387c0 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20   right table of 
387d0 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a 20 20 53 65  the join */.  Se
387e0 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 20  lect *pSelect;  
387f0 20 20 20 20 20 2f 2a 20 57 68 65 6e 20 74 68 65       /* When the
38800 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61   expression is a
38810 20 73 75 62 2d 73 65 6c 65 63 74 2e 20 20 41 6c   sub-select.  Al
38820 73 6f 20 74 68 65 0a 20 20 20 20 20 20 20 20 20  so the.         
38830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38840 2a 2a 20 72 69 67 68 74 20 73 69 64 65 20 6f 66  ** right side of
38850 20 22 3c 65 78 70 72 3e 20 49 4e 20 28 3c 73 65   "<expr> IN (<se
38860 6c 65 63 74 3e 29 22 20 2a 2f 0a 20 20 54 61 62  lect>)" */.  Tab
38870 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20  le *pTab;       
38880 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 66 6f 72      /* Table for
38890 20 4f 50 5f 43 6f 6c 75 6d 6e 20 65 78 70 72 65   OP_Column expre
388a0 73 73 69 6f 6e 73 2e 20 2a 2f 0a 20 20 53 63 68  ssions. */.  Sch
388b0 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b 0a 23 69  ema *pSchema;.#i
388c0 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50  f SQLITE_MAX_EXP
388d0 52 5f 44 45 50 54 48 3e 30 0a 20 20 69 6e 74 20  R_DEPTH>0.  int 
388e0 6e 48 65 69 67 68 74 3b 20 20 20 20 20 20 20 20  nHeight;        
388f0 20 20 20 2f 2a 20 48 65 69 67 68 74 20 6f 66 20     /* Height of 
38900 74 68 65 20 74 72 65 65 20 68 65 61 64 65 64 20  the tree headed 
38910 62 79 20 74 68 69 73 20 6e 6f 64 65 20 2a 2f 0a  by this node */.
38920 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  #endif.};../*.**
38930 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61   The following a
38940 72 65 20 74 68 65 20 6d 65 61 6e 69 6e 67 73 20  re the meanings 
38950 6f 66 20 62 69 74 73 20 69 6e 20 74 68 65 20 45  of bits in the E
38960 78 70 72 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e  xpr.flags field.
38970 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 46  .*/.#define EP_F
38980 72 6f 6d 4a 6f 69 6e 20 20 20 20 20 30 78 30 31  romJoin     0x01
38990 20 20 2f 2a 20 4f 72 69 67 69 6e 61 74 65 64 20    /* Originated 
389a0 69 6e 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63  in ON or USING c
389b0 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20  lause of a join 
389c0 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 41 67  */.#define EP_Ag
389d0 67 20 20 20 20 20 20 20 20 20 20 30 78 30 32 20  g          0x02 
389e0 20 2f 2a 20 43 6f 6e 74 61 69 6e 73 20 6f 6e 65   /* Contains one
389f0 20 6f 72 20 6d 6f 72 65 20 61 67 67 72 65 67 61   or more aggrega
38a00 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a  te functions */.
38a10 23 64 65 66 69 6e 65 20 45 50 5f 52 65 73 6f 6c  #define EP_Resol
38a20 76 65 64 20 20 20 20 20 30 78 30 34 20 20 2f 2a  ved     0x04  /*
38a30 20 49 44 73 20 68 61 76 65 20 62 65 65 6e 20 72   IDs have been r
38a40 65 73 6f 6c 76 65 64 20 74 6f 20 43 4f 4c 55 4d  esolved to COLUM
38a50 4e 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50  Ns */.#define EP
38a60 5f 45 72 72 6f 72 20 20 20 20 20 20 20 20 30 78  _Error        0x
38a70 30 38 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f  08  /* Expressio
38a80 6e 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f  n contains one o
38a90 72 20 6d 6f 72 65 20 65 72 72 6f 72 73 20 2a 2f  r more errors */
38aa0 0a 23 64 65 66 69 6e 65 20 45 50 5f 44 69 73 74  .#define EP_Dist
38ab0 69 6e 63 74 20 20 20 20 20 30 78 31 30 20 20 2f  inct     0x10  /
38ac0 2a 20 41 67 67 72 65 67 61 74 65 20 66 75 6e 63  * Aggregate func
38ad0 74 69 6f 6e 20 77 69 74 68 20 44 49 53 54 49 4e  tion with DISTIN
38ae0 43 54 20 6b 65 79 77 6f 72 64 20 2a 2f 0a 23 64  CT keyword */.#d
38af0 65 66 69 6e 65 20 45 50 5f 56 61 72 53 65 6c 65  efine EP_VarSele
38b00 63 74 20 20 20 20 30 78 32 30 20 20 2f 2a 20 70  ct    0x20  /* p
38b10 53 65 6c 65 63 74 20 69 73 20 63 6f 72 72 65 6c  Select is correl
38b20 61 74 65 64 2c 20 6e 6f 74 20 63 6f 6e 73 74 61  ated, not consta
38b30 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50  nt */.#define EP
38b40 5f 44 65 71 75 6f 74 65 64 20 20 20 20 20 30 78  _Dequoted     0x
38b50 34 30 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74  40  /* True if t
38b60 68 65 20 73 74 72 69 6e 67 20 68 61 73 20 62 65  he string has be
38b70 65 6e 20 64 65 71 75 6f 74 65 64 20 2a 2f 0a 23  en dequoted */.#
38b80 64 65 66 69 6e 65 20 45 50 5f 49 6e 66 69 78 46  define EP_InfixF
38b90 75 6e 63 20 20 20 20 30 78 38 30 20 20 2f 2a 20  unc    0x80  /* 
38ba0 54 72 75 65 20 66 6f 72 20 61 6e 20 69 6e 66 69  True for an infi
38bb0 78 20 66 75 6e 63 74 69 6f 6e 3a 20 4c 49 4b 45  x function: LIKE
38bc0 2c 20 47 4c 4f 42 2c 20 65 74 63 20 2a 2f 0a 23  , GLOB, etc */.#
38bd0 64 65 66 69 6e 65 20 45 50 5f 45 78 70 43 6f 6c  define EP_ExpCol
38be0 6c 61 74 65 20 20 30 78 31 30 30 20 20 2f 2a 20  late  0x100  /* 
38bf0 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  Collating sequen
38c00 63 65 20 73 70 65 63 69 66 69 65 64 20 65 78 70  ce specified exp
38c10 6c 69 63 69 74 6c 79 20 2a 2f 0a 0a 2f 2a 0a 2a  licitly */../*.*
38c20 2a 20 54 68 65 73 65 20 6d 61 63 72 6f 73 20 63  * These macros c
38c30 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 74 65  an be used to te
38c40 73 74 2c 20 73 65 74 2c 20 6f 72 20 63 6c 65 61  st, set, or clea
38c50 72 20 62 69 74 73 20 69 6e 20 74 68 65 20 0a 2a  r bits in the .*
38c60 2a 20 45 78 70 72 2e 66 6c 61 67 73 20 66 69 65  * Expr.flags fie
38c70 6c 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45  ld..*/.#define E
38c80 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 45  xprHasProperty(E
38c90 2c 50 29 20 20 20 20 20 28 28 28 45 29 2d 3e 66  ,P)     (((E)->f
38ca0 6c 61 67 73 26 28 50 29 29 3d 3d 28 50 29 29 0a  lags&(P))==(P)).
38cb0 23 64 65 66 69 6e 65 20 45 78 70 72 48 61 73 41  #define ExprHasA
38cc0 6e 79 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20  nyProperty(E,P) 
38cd0 20 28 28 28 45 29 2d 3e 66 6c 61 67 73 26 28 50   (((E)->flags&(P
38ce0 29 29 21 3d 30 29 0a 23 64 65 66 69 6e 65 20 45  ))!=0).#define E
38cf0 78 70 72 53 65 74 50 72 6f 70 65 72 74 79 28 45  xprSetProperty(E
38d00 2c 50 29 20 20 20 20 20 28 45 29 2d 3e 66 6c 61  ,P)     (E)->fla
38d10 67 73 7c 3d 28 50 29 0a 23 64 65 66 69 6e 65 20  gs|=(P).#define 
38d20 45 78 70 72 43 6c 65 61 72 50 72 6f 70 65 72 74  ExprClearPropert
38d30 79 28 45 2c 50 29 20 20 20 28 45 29 2d 3e 66 6c  y(E,P)   (E)->fl
38d40 61 67 73 26 3d 7e 28 50 29 0a 0a 2f 2a 0a 2a 2a  ags&=~(P)../*.**
38d50 20 41 20 6c 69 73 74 20 6f 66 20 65 78 70 72 65   A list of expre
38d60 73 73 69 6f 6e 73 2e 20 20 45 61 63 68 20 65 78  ssions.  Each ex
38d70 70 72 65 73 73 69 6f 6e 20 6d 61 79 20 6f 70 74  pression may opt
38d80 69 6f 6e 61 6c 6c 79 20 68 61 76 65 20 61 0a 2a  ionally have a.*
38d90 2a 20 6e 61 6d 65 2e 20 20 41 6e 20 65 78 70 72  * name.  An expr
38da0 2f 6e 61 6d 65 20 63 6f 6d 62 69 6e 61 74 69 6f  /name combinatio
38db0 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 6e  n can be used in
38dc0 20 73 65 76 65 72 61 6c 20 77 61 79 73 2c 20 73   several ways, s
38dd0 75 63 68 0a 2a 2a 20 61 73 20 74 68 65 20 6c 69  uch.** as the li
38de0 73 74 20 6f 66 20 22 65 78 70 72 20 41 53 20 49  st of "expr AS I
38df0 44 22 20 66 69 65 6c 64 73 20 66 6f 6c 6c 6f 77  D" fields follow
38e00 69 6e 67 20 61 20 22 53 45 4c 45 43 54 22 20 6f  ing a "SELECT" o
38e10 72 20 69 6e 20 74 68 65 0a 2a 2a 20 6c 69 73 74  r in the.** list
38e20 20 6f 66 20 22 49 44 20 3d 20 65 78 70 72 22 20   of "ID = expr" 
38e30 69 74 65 6d 73 20 69 6e 20 61 6e 20 55 50 44 41  items in an UPDA
38e40 54 45 2e 20 20 41 20 6c 69 73 74 20 6f 66 20 65  TE.  A list of e
38e50 78 70 72 65 73 73 69 6f 6e 73 20 63 61 6e 0a 2a  xpressions can.*
38e60 2a 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20 61  * also be used a
38e70 73 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74  s the argument t
38e80 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2c 20 69 6e  o a function, in
38e90 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65 20   which case the 
38ea0 61 2e 7a 4e 61 6d 65 0a 2a 2a 20 66 69 65 6c 64  a.zName.** field
38eb0 20 69 73 20 6e 6f 74 20 75 73 65 64 2e 0a 2a 2f   is not used..*/
38ec0 0a 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74  .struct ExprList
38ed0 20 7b 0a 20 20 69 6e 74 20 6e 45 78 70 72 3b 20   {.  int nExpr; 
38ee0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
38ef0 75 6d 62 65 72 20 6f 66 20 65 78 70 72 65 73 73  umber of express
38f00 69 6f 6e 73 20 6f 6e 20 74 68 65 20 6c 69 73 74  ions on the list
38f10 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63   */.  int nAlloc
38f20 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
38f30 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
38f40 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 65 6c 6f  s allocated belo
38f50 77 20 2a 2f 0a 20 20 69 6e 74 20 69 45 43 75 72  w */.  int iECur
38f60 73 6f 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  sor;          /*
38f70 20 56 44 42 45 20 43 75 72 73 6f 72 20 61 73 73   VDBE Cursor ass
38f80 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69  ociated with thi
38f90 73 20 45 78 70 72 4c 69 73 74 20 2a 2f 0a 20 20  s ExprList */.  
38fa0 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
38fb0 69 74 65 6d 20 7b 0a 20 20 20 20 45 78 70 72 20  item {.    Expr 
38fc0 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 20  *pExpr;         
38fd0 20 20 2f 2a 20 54 68 65 20 6c 69 73 74 20 6f 66    /* The list of
38fe0 20 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a   expressions */.
38ff0 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b      char *zName;
39000 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f             /* To
39010 6b 65 6e 20 61 73 73 6f 63 69 61 74 65 64 20 77  ken associated w
39020 69 74 68 20 74 68 69 73 20 65 78 70 72 65 73 73  ith this express
39030 69 6f 6e 20 2a 2f 0a 20 20 20 20 75 38 20 73 6f  ion */.    u8 so
39040 72 74 4f 72 64 65 72 3b 20 20 20 20 20 20 20 20  rtOrder;        
39050 20 20 2f 2a 20 31 20 66 6f 72 20 44 45 53 43 20    /* 1 for DESC 
39060 6f 72 20 30 20 66 6f 72 20 41 53 43 20 2a 2f 0a  or 0 for ASC */.
39070 20 20 20 20 75 38 20 69 73 41 67 67 3b 20 20 20      u8 isAgg;   
39080 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
39090 75 65 20 69 66 20 74 68 69 73 20 69 73 20 61 6e  ue if this is an
390a0 20 61 67 67 72 65 67 61 74 65 20 6c 69 6b 65 20   aggregate like 
390b0 63 6f 75 6e 74 28 2a 29 20 2a 2f 0a 20 20 20 20  count(*) */.    
390c0 75 38 20 64 6f 6e 65 3b 20 20 20 20 20 20 20 20  u8 done;        
390d0 20 20 20 20 20 20 20 2f 2a 20 41 20 66 6c 61 67         /* A flag
390e0 20 74 6f 20 69 6e 64 69 63 61 74 65 20 77 68 65   to indicate whe
390f0 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 69 73 20  n processing is 
39100 66 69 6e 69 73 68 65 64 20 2a 2f 0a 20 20 7d 20  finished */.  } 
39110 2a 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  *a;             
39120 20 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72       /* One entr
39130 79 20 66 6f 72 20 65 61 63 68 20 65 78 70 72 65  y for each expre
39140 73 73 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  ssion */.};../*.
39150 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
39160 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
39170 20 63 61 6e 20 68 6f 6c 64 20 61 20 73 69 6d 70   can hold a simp
39180 6c 65 20 6c 69 73 74 20 6f 66 20 69 64 65 6e 74  le list of ident
39190 69 66 69 65 72 73 2c 0a 2a 2a 20 73 75 63 68 20  ifiers,.** such 
391a0 61 73 20 74 68 65 20 6c 69 73 74 20 22 61 2c 62  as the list "a,b
391b0 2c 63 22 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f  ,c" in the follo
391c0 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 73 3a  wing statements:
391d0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 49 4e 53 45  .**.**      INSE
391e0 52 54 20 49 4e 54 4f 20 74 28 61 2c 62 2c 63 29  RT INTO t(a,b,c)
391f0 20 56 41 4c 55 45 53 20 2e 2e 2e 3b 0a 2a 2a 20   VALUES ...;.** 
39200 20 20 20 20 20 43 52 45 41 54 45 20 49 4e 44 45       CREATE INDE
39210 58 20 69 64 78 20 4f 4e 20 74 28 61 2c 62 2c 63  X idx ON t(a,b,c
39220 29 3b 0a 2a 2a 20 20 20 20 20 20 43 52 45 41 54  );.**      CREAT
39230 45 20 54 52 49 47 47 45 52 20 74 72 69 67 20 42  E TRIGGER trig B
39240 45 46 4f 52 45 20 55 50 44 41 54 45 20 4f 4e 20  EFORE UPDATE ON 
39250 74 28 61 2c 62 2c 63 29 20 2e 2e 2e 3b 0a 2a 2a  t(a,b,c) ...;.**
39260 0a 2a 2a 20 54 68 65 20 49 64 4c 69 73 74 2e 61  .** The IdList.a
39270 2e 69 64 78 20 66 69 65 6c 64 20 69 73 20 75 73  .idx field is us
39280 65 64 20 77 68 65 6e 20 74 68 65 20 49 64 4c 69  ed when the IdLi
39290 73 74 20 72 65 70 72 65 73 65 6e 74 73 20 74 68  st represents th
392a0 65 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 63 6f 6c  e list of.** col
392b0 75 6d 6e 20 6e 61 6d 65 73 20 61 66 74 65 72 20  umn names after 
392c0 61 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20  a table name in 
392d0 61 6e 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d  an INSERT statem
392e0 65 6e 74 2e 20 20 49 6e 20 74 68 65 20 73 74 61  ent.  In the sta
392f0 74 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a 20 20 20 20  tement.**.**    
39300 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 28 61   INSERT INTO t(a
39310 2c 62 2c 63 29 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20  ,b,c) ....**.** 
39320 49 66 20 22 61 22 20 69 73 20 74 68 65 20 6b 2d  If "a" is the k-
39330 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 61 62  th column of tab
39340 6c 65 20 22 74 22 2c 20 74 68 65 6e 20 49 64 4c  le "t", then IdL
39350 69 73 74 2e 61 5b 30 5d 2e 69 64 78 3d 3d 6b 2e  ist.a[0].idx==k.
39360 0a 2a 2f 0a 73 74 72 75 63 74 20 49 64 4c 69 73  .*/.struct IdLis
39370 74 20 7b 0a 20 20 73 74 72 75 63 74 20 49 64 4c  t {.  struct IdL
39380 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20 20 63  ist_item {.    c
39390 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20  har *zName;     
393a0 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
393b0 69 64 65 6e 74 69 66 69 65 72 20 2a 2f 0a 20 20  identifier */.  
393c0 20 20 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20    int idx;      
393d0 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20      /* Index in 
393e0 73 6f 6d 65 20 54 61 62 6c 65 2e 61 43 6f 6c 5b  some Table.aCol[
393f0 5d 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 6e 61  ] of a column na
39400 6d 65 64 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7d  med zName */.  }
39410 20 2a 61 3b 0a 20 20 69 6e 74 20 6e 49 64 3b 20   *a;.  int nId; 
39420 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
39430 72 20 6f 66 20 69 64 65 6e 74 69 66 69 65 72 73  r of identifiers
39440 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a   on the list */.
39450 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20 20    int nAlloc;   
39460 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
39470 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61 74 65  entries allocate
39480 64 20 66 6f 72 20 61 5b 5d 20 62 65 6c 6f 77 20  d for a[] below 
39490 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  */.};../*.** The
394a0 20 62 69 74 6d 61 73 6b 20 64 61 74 61 74 79 70   bitmask datatyp
394b0 65 20 64 65 66 69 6e 65 64 20 62 65 6c 6f 77 20  e defined below 
394c0 69 73 20 75 73 65 64 20 66 6f 72 20 76 61 72 69  is used for vari
394d0 6f 75 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  ous optimization
394e0 73 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e  s..**.** Changin
394f0 67 20 74 68 69 73 20 66 72 6f 6d 20 61 20 36 34  g this from a 64
39500 2d 62 69 74 20 74 6f 20 61 20 33 32 2d 62 69 74  -bit to a 32-bit
39510 20 74 79 70 65 20 6c 69 6d 69 74 73 20 74 68 65   type limits the
39520 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 74 61   number of.** ta
39530 62 6c 65 73 20 69 6e 20 61 20 6a 6f 69 6e 20 74  bles in a join t
39540 6f 20 33 32 20 69 6e 73 74 65 61 64 20 6f 66 20  o 32 instead of 
39550 36 34 2e 20 20 42 75 74 20 69 74 20 61 6c 73 6f  64.  But it also
39560 20 72 65 64 75 63 65 73 20 74 68 65 20 73 69 7a   reduces the siz
39570 65 0a 2a 2a 20 6f 66 20 74 68 65 20 6c 69 62 72  e.** of the libr
39580 61 72 79 20 62 79 20 37 33 38 20 62 79 74 65 73  ary by 738 bytes
39590 20 6f 6e 20 69 78 38 36 2e 0a 2a 2f 0a 74 79 70   on ix86..*/.typ
395a0 65 64 65 66 20 75 36 34 20 42 69 74 6d 61 73 6b  edef u64 Bitmask
395b0 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  ;../*.** The fol
395c0 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
395d0 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 46   describes the F
395e0 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 61 20  ROM clause of a 
395f0 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
39600 2e 0a 2a 2a 20 45 61 63 68 20 74 61 62 6c 65 20  ..** Each table 
39610 6f 72 20 73 75 62 71 75 65 72 79 20 69 6e 20 74  or subquery in t
39620 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69  he FROM clause i
39630 73 20 61 20 73 65 70 61 72 61 74 65 20 65 6c 65  s a separate ele
39640 6d 65 6e 74 20 6f 66 0a 2a 2a 20 74 68 65 20 53  ment of.** the S
39650 72 63 4c 69 73 74 2e 61 5b 5d 20 61 72 72 61 79  rcList.a[] array
39660 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20 74 68 65  ..**.** With the
39670 20 61 64 64 69 74 69 6f 6e 20 6f 66 20 6d 75 6c   addition of mul
39680 74 69 70 6c 65 20 64 61 74 61 62 61 73 65 20 73  tiple database s
39690 75 70 70 6f 72 74 2c 20 74 68 65 20 66 6f 6c 6c  upport, the foll
396a0 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 0a  owing structure.
396b0 2a 2a 20 63 61 6e 20 61 6c 73 6f 20 62 65 20 75  ** can also be u
396c0 73 65 64 20 74 6f 20 64 65 73 63 72 69 62 65 20  sed to describe 
396d0 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 61 62  a particular tab
396e0 6c 65 20 73 75 63 68 20 61 73 20 74 68 65 20 74  le such as the t
396f0 61 62 6c 65 20 74 68 61 74 0a 2a 2a 20 69 73 20  able that.** is 
39700 6d 6f 64 69 66 69 65 64 20 62 79 20 61 6e 20 49  modified by an I
39710 4e 53 45 52 54 2c 20 44 45 4c 45 54 45 2c 20 6f  NSERT, DELETE, o
39720 72 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65  r UPDATE stateme
39730 6e 74 2e 20 20 49 6e 20 73 74 61 6e 64 61 72 64  nt.  In standard
39740 20 53 51 4c 2c 0a 2a 2a 20 73 75 63 68 20 61 20   SQL,.** such a 
39750 74 61 62 6c 65 20 6d 75 73 74 20 62 65 20 61 20  table must be a 
39760 73 69 6d 70 6c 65 20 6e 61 6d 65 3a 20 49 44 2e  simple name: ID.
39770 20 20 42 75 74 20 69 6e 20 53 51 4c 69 74 65 2c    But in SQLite,
39780 20 74 68 65 20 74 61 62 6c 65 20 63 61 6e 0a 2a   the table can.*
39790 2a 20 6e 6f 77 20 62 65 20 69 64 65 6e 74 69 66  * now be identif
397a0 69 65 64 20 62 79 20 61 20 64 61 74 61 62 61 73  ied by a databas
397b0 65 20 6e 61 6d 65 2c 20 61 20 64 6f 74 2c 20 74  e name, a dot, t
397c0 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 6e 61  hen the table na
397d0 6d 65 3a 20 49 44 2e 49 44 2e 0a 2a 2a 0a 2a 2a  me: ID.ID..**.**
397e0 20 54 68 65 20 6a 6f 69 6e 74 79 70 65 20 73 74   The jointype st
397f0 61 72 74 73 20 6f 75 74 20 73 68 6f 77 69 6e 67  arts out showing
39800 20 74 68 65 20 6a 6f 69 6e 20 74 79 70 65 20 62   the join type b
39810 65 74 77 65 65 6e 20 74 68 65 20 63 75 72 72 65  etween the curre
39820 6e 74 20 74 61 62 6c 65 0a 2a 2a 20 61 6e 64 20  nt table.** and 
39830 74 68 65 20 6e 65 78 74 20 74 61 62 6c 65 20 6f  the next table o
39840 6e 20 74 68 65 20 6c 69 73 74 2e 20 20 54 68 65  n the list.  The
39850 20 70 61 72 73 65 72 20 62 75 69 6c 64 73 20 74   parser builds t
39860 68 65 20 6c 69 73 74 20 74 68 69 73 20 77 61 79  he list this way
39870 2e 0a 2a 2a 20 42 75 74 20 73 71 6c 69 74 65 33  ..** But sqlite3
39880 53 72 63 4c 69 73 74 53 68 69 66 74 4a 6f 69 6e  SrcListShiftJoin
39890 54 79 70 65 28 29 20 6c 61 74 65 72 20 73 68 69  Type() later shi
398a0 66 74 73 20 74 68 65 20 6a 6f 69 6e 74 79 70 65  fts the jointype
398b0 73 20 73 6f 20 74 68 61 74 20 65 61 63 68 0a 2a  s so that each.*
398c0 2a 20 6a 6f 69 6e 74 79 70 65 20 65 78 70 72 65  * jointype expre
398d0 73 73 65 73 20 74 68 65 20 6a 6f 69 6e 20 62 65  sses the join be
398e0 74 77 65 65 6e 20 74 68 65 20 74 61 62 6c 65 20  tween the table 
398f0 61 6e 64 20 74 68 65 20 70 72 65 76 69 6f 75 73  and the previous
39900 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63   table..*/.struc
39910 74 20 53 72 63 4c 69 73 74 20 7b 0a 20 20 69 31  t SrcList {.  i1
39920 36 20 6e 53 72 63 3b 20 20 20 20 20 20 20 20 2f  6 nSrc;        /
39930 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 61 62 6c  * Number of tabl
39940 65 73 20 6f 72 20 73 75 62 71 75 65 72 69 65 73  es or subqueries
39950 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   in the FROM cla
39960 75 73 65 20 2a 2f 0a 20 20 69 31 36 20 6e 41 6c  use */.  i16 nAl
39970 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d  loc;      /* Num
39980 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 61  ber of entries a
39990 6c 6c 6f 63 61 74 65 64 20 69 6e 20 61 5b 5d 20  llocated in a[] 
399a0 62 65 6c 6f 77 20 2a 2f 0a 20 20 73 74 72 75 63  below */.  struc
399b0 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 7b  t SrcList_item {
399c0 0a 20 20 20 20 63 68 61 72 20 2a 7a 44 61 74 61  .    char *zData
399d0 62 61 73 65 3b 20 20 2f 2a 20 4e 61 6d 65 20 6f  base;  /* Name o
399e0 66 20 64 61 74 61 62 61 73 65 20 68 6f 6c 64 69  f database holdi
399f0 6e 67 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f  ng this table */
39a00 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65  .    char *zName
39a10 3b 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f  ;      /* Name o
39a20 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20  f the table */. 
39a30 20 20 20 63 68 61 72 20 2a 7a 41 6c 69 61 73 3b     char *zAlias;
39a40 20 20 20 20 20 2f 2a 20 54 68 65 20 22 42 22 20       /* The "B" 
39a50 70 61 72 74 20 6f 66 20 61 20 22 41 20 41 53 20  part of a "A AS 
39a60 42 22 20 70 68 72 61 73 65 2e 20 20 7a 4e 61 6d  B" phrase.  zNam
39a70 65 20 69 73 20 74 68 65 20 22 41 22 20 2a 2f 0a  e is the "A" */.
39a80 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b      Table *pTab;
39a90 20 20 20 20 20 20 2f 2a 20 41 6e 20 53 51 4c 20        /* An SQL 
39aa0 74 61 62 6c 65 20 63 6f 72 72 65 73 70 6f 6e 64  table correspond
39ab0 69 6e 67 20 74 6f 20 7a 4e 61 6d 65 20 2a 2f 0a  ing to zName */.
39ac0 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c      Select *pSel
39ad0 65 63 74 3b 20 20 2f 2a 20 41 20 53 45 4c 45 43  ect;  /* A SELEC
39ae0 54 20 73 74 61 74 65 6d 65 6e 74 20 75 73 65 64  T statement used
39af0 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 61 20 74   in place of a t
39b00 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20  able name */.   
39b10 20 75 38 20 69 73 50 6f 70 75 6c 61 74 65 64 3b   u8 isPopulated;
39b20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20     /* Temporary 
39b30 74 61 62 6c 65 20 61 73 73 6f 63 69 61 74 65 64  table associated
39b40 20 77 69 74 68 20 53 45 4c 45 43 54 20 69 73 20   with SELECT is 
39b50 70 6f 70 75 6c 61 74 65 64 20 2a 2f 0a 20 20 20  populated */.   
39b60 20 75 38 20 6a 6f 69 6e 74 79 70 65 3b 20 20 20   u8 jointype;   
39b70 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 6a 6f     /* Type of jo
39b80 69 6e 20 62 65 74 77 65 65 6e 20 74 68 69 73 20  in between this 
39b90 61 62 6c 65 20 61 6e 64 20 74 68 65 20 70 72 65  able and the pre
39ba0 76 69 6f 75 73 20 2a 2f 0a 20 20 20 20 69 6e 74  vious */.    int
39bb0 20 69 43 75 72 73 6f 72 3b 20 20 20 20 20 20 2f   iCursor;      /
39bc0 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f  * The VDBE curso
39bd0 72 20 6e 75 6d 62 65 72 20 75 73 65 64 20 74 6f  r number used to
39be0 20 61 63 63 65 73 73 20 74 68 69 73 20 74 61 62   access this tab
39bf0 6c 65 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a  le */.    Expr *
39c00 70 4f 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  pOn;        /* T
39c10 68 65 20 4f 4e 20 63 6c 61 75 73 65 20 6f 66 20  he ON clause of 
39c20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20 20 20 49 64  a join */.    Id
39c30 4c 69 73 74 20 2a 70 55 73 69 6e 67 3b 20 20 20  List *pUsing;   
39c40 2f 2a 20 54 68 65 20 55 53 49 4e 47 20 63 6c 61  /* The USING cla
39c50 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f  use of a join */
39c60 0a 20 20 20 20 42 69 74 6d 61 73 6b 20 63 6f 6c  .    Bitmask col
39c70 55 73 65 64 3b 20 20 2f 2a 20 42 69 74 20 4e 20  Used;  /* Bit N 
39c80 28 31 3c 3c 4e 29 20 73 65 74 20 69 66 20 63 6f  (1<<N) set if co
39c90 6c 75 6d 6e 20 4e 20 6f 72 20 70 54 61 62 20 69  lumn N or pTab i
39ca0 73 20 75 73 65 64 20 2a 2f 0a 20 20 7d 20 61 5b  s used */.  } a[
39cb0 31 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  1];             
39cc0 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72  /* One entry for
39cd0 20 65 61 63 68 20 69 64 65 6e 74 69 66 69 65 72   each identifier
39ce0 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a   on the list */.
39cf0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 6d 69 74  };../*.** Permit
39d00 74 65 64 20 76 61 6c 75 65 73 20 6f 66 20 74 68  ted values of th
39d10 65 20 53 72 63 4c 69 73 74 2e 61 2e 6a 6f 69 6e  e SrcList.a.join
39d20 74 79 70 65 20 66 69 65 6c 64 0a 2a 2f 0a 23 64  type field.*/.#d
39d30 65 66 69 6e 65 20 4a 54 5f 49 4e 4e 45 52 20 20  efine JT_INNER  
39d40 20 20 20 30 78 30 30 30 31 20 20 20 20 2f 2a 20     0x0001    /* 
39d50 41 6e 79 20 6b 69 6e 64 20 6f 66 20 69 6e 6e 65  Any kind of inne
39d60 72 20 6f 72 20 63 72 6f 73 73 20 6a 6f 69 6e 20  r or cross join 
39d70 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 43 52  */.#define JT_CR
39d80 4f 53 53 20 20 20 20 20 30 78 30 30 30 32 20 20  OSS     0x0002  
39d90 20 20 2f 2a 20 45 78 70 6c 69 63 69 74 20 75 73    /* Explicit us
39da0 65 20 6f 66 20 74 68 65 20 43 52 4f 53 53 20 6b  e of the CROSS k
39db0 65 79 77 6f 72 64 20 2a 2f 0a 23 64 65 66 69 6e  eyword */.#defin
39dc0 65 20 4a 54 5f 4e 41 54 55 52 41 4c 20 20 20 30  e JT_NATURAL   0
39dd0 78 30 30 30 34 20 20 20 20 2f 2a 20 54 72 75 65  x0004    /* True
39de0 20 66 6f 72 20 61 20 22 6e 61 74 75 72 61 6c 22   for a "natural"
39df0 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65   join */.#define
39e00 20 4a 54 5f 4c 45 46 54 20 20 20 20 20 20 30 78   JT_LEFT      0x
39e10 30 30 30 38 20 20 20 20 2f 2a 20 4c 65 66 74 20  0008    /* Left 
39e20 6f 75 74 65 72 20 6a 6f 69 6e 20 2a 2f 0a 23 64  outer join */.#d
39e30 65 66 69 6e 65 20 4a 54 5f 52 49 47 48 54 20 20  efine JT_RIGHT  
39e40 20 20 20 30 78 30 30 31 30 20 20 20 20 2f 2a 20     0x0010    /* 
39e50 52 69 67 68 74 20 6f 75 74 65 72 20 6a 6f 69 6e  Right outer join
39e60 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 4f   */.#define JT_O
39e70 55 54 45 52 20 20 20 20 20 30 78 30 30 32 30 20  UTER     0x0020 
39e80 20 20 20 2f 2a 20 54 68 65 20 22 4f 55 54 45 52     /* The "OUTER
39e90 22 20 6b 65 79 77 6f 72 64 20 69 73 20 70 72 65  " keyword is pre
39ea0 73 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20  sent */.#define 
39eb0 4a 54 5f 45 52 52 4f 52 20 20 20 20 20 30 78 30  JT_ERROR     0x0
39ec0 30 34 30 20 20 20 20 2f 2a 20 75 6e 6b 6e 6f 77  040    /* unknow
39ed0 6e 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 65 64  n or unsupported
39ee0 20 6a 6f 69 6e 20 74 79 70 65 20 2a 2f 0a 0a 2f   join type */../
39ef0 2a 0a 2a 2a 20 46 6f 72 20 65 61 63 68 20 6e 65  *.** For each ne
39f00 73 74 65 64 20 6c 6f 6f 70 20 69 6e 20 61 20 57  sted loop in a W
39f10 48 45 52 45 20 63 6c 61 75 73 65 20 69 6d 70 6c  HERE clause impl
39f20 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 74 68 65 20  ementation, the 
39f30 57 68 65 72 65 49 6e 66 6f 0a 2a 2a 20 73 74 72  WhereInfo.** str
39f40 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20  ucture contains 
39f50 61 20 73 69 6e 67 6c 65 20 69 6e 73 74 61 6e 63  a single instanc
39f60 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  e of this struct
39f70 75 72 65 2e 20 20 54 68 69 73 20 73 74 72 75 63  ure.  This struc
39f80 74 75 72 65 0a 2a 2a 20 69 73 20 69 6e 74 65 6e  ture.** is inten
39f90 64 65 64 20 74 6f 20 62 65 20 70 72 69 76 61 74  ded to be privat
39fa0 65 20 74 68 65 20 74 68 65 20 77 68 65 72 65 2e  e the the where.
39fb0 63 20 6d 6f 64 75 6c 65 20 61 6e 64 20 73 68 6f  c module and sho
39fc0 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a 20 61 63  uld not be.** ac
39fd0 63 65 73 73 20 6f 72 20 6d 6f 64 69 66 69 65 64  cess or modified
39fe0 20 62 79 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65   by other module
39ff0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 49 64  s..**.** The pId
3a000 78 49 6e 66 6f 20 61 6e 64 20 70 42 65 73 74 49  xInfo and pBestI
3a010 64 78 20 66 69 65 6c 64 73 20 61 72 65 20 75 73  dx fields are us
3a020 65 64 20 74 6f 20 68 65 6c 70 20 70 69 63 6b 20  ed to help pick 
3a030 74 68 65 20 62 65 73 74 0a 2a 2a 20 69 6e 64 65  the best.** inde
3a040 78 20 6f 6e 20 61 20 76 69 72 74 75 61 6c 20 74  x on a virtual t
3a050 61 62 6c 65 2e 20 20 54 68 65 20 70 49 64 78 49  able.  The pIdxI
3a060 6e 66 6f 20 70 6f 69 6e 74 65 72 20 63 6f 6e 74  nfo pointer cont
3a070 61 69 6e 73 20 69 6e 64 65 78 69 6e 67 0a 2a 2a  ains indexing.**
3a080 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72   information for
3a090 20 74 68 65 20 69 2d 74 68 20 74 61 62 6c 65 20   the i-th table 
3a0a0 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  in the FROM clau
3a0b0 73 65 20 62 65 66 6f 72 65 20 72 65 6f 72 64 65  se before reorde
3a0c0 72 69 6e 67 2e 0a 2a 2a 20 41 6c 6c 20 74 68 65  ring..** All the
3a0d0 20 70 49 64 78 49 6e 66 6f 20 70 6f 69 6e 74 65   pIdxInfo pointe
3a0e0 72 73 20 61 72 65 20 66 72 65 65 64 20 62 79 20  rs are freed by 
3a0f0 77 68 65 72 65 49 6e 66 6f 46 72 65 65 28 29 20  whereInfoFree() 
3a100 69 6e 20 77 68 65 72 65 2e 63 2e 0a 2a 2a 20 54  in where.c..** T
3a110 68 65 20 70 42 65 73 74 49 64 78 20 70 6f 69 6e  he pBestIdx poin
3a120 74 65 72 20 69 73 20 61 20 63 6f 70 79 20 6f 66  ter is a copy of
3a130 20 70 49 64 78 49 6e 66 6f 20 66 6f 72 20 74 68   pIdxInfo for th
3a140 65 20 69 2d 74 68 20 74 61 62 6c 65 20 61 66 74  e i-th table aft
3a150 65 72 0a 2a 2a 20 46 52 4f 4d 20 63 6c 61 75 73  er.** FROM claus
3a160 65 20 6f 72 64 65 72 69 6e 67 2e 20 20 54 68 69  e ordering.  Thi
3a170 73 20 69 73 20 61 20 6c 69 74 74 6c 65 20 63 6f  s is a little co
3a180 6e 66 75 73 69 6e 67 20 73 6f 20 49 20 77 69 6c  nfusing so I wil
3a190 6c 20 72 65 70 65 61 74 0a 2a 2a 20 69 74 20 69  l repeat.** it i
3a1a0 6e 20 64 69 66 66 65 72 65 6e 74 20 77 6f 72 64  n different word
3a1b0 73 2e 20 20 57 68 65 72 65 49 6e 66 6f 2e 61 5b  s.  WhereInfo.a[
3a1c0 69 5d 2e 70 49 64 78 49 6e 66 6f 20 69 73 20 69  i].pIdxInfo is i
3a1d0 6e 64 65 78 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ndex information
3a1e0 20 0a 2a 2a 20 66 6f 72 20 57 68 65 72 65 49 6e   .** for WhereIn
3a1f0 66 6f 2e 70 54 61 62 4c 69 73 74 2e 61 5b 69 5d  fo.pTabList.a[i]
3a200 2e 20 20 57 68 65 72 65 49 6e 66 6f 2e 61 5b 69  .  WhereInfo.a[i
3a210 5d 2e 70 42 65 73 74 49 6e 66 6f 20 69 73 20 74  ].pBestInfo is t
3a220 68 65 0a 2a 2a 20 69 6e 64 65 78 20 69 6e 66 6f  he.** index info
3a230 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20  rmation for the 
3a240 69 2d 74 68 20 6c 6f 6f 70 20 6f 66 20 74 68 65  i-th loop of the
3a250 20 6a 6f 69 6e 2e 20 20 70 42 65 73 74 49 6e 66   join.  pBestInf
3a260 6f 20 69 73 20 61 6c 77 61 79 73 0a 2a 2a 20 65  o is always.** e
3a270 69 74 68 65 72 20 4e 55 4c 4c 20 6f 72 20 61 20  ither NULL or a 
3a280 63 6f 70 79 20 6f 66 20 73 6f 6d 65 20 70 49 64  copy of some pId
3a290 78 49 6e 66 6f 2e 20 20 53 6f 20 66 6f 72 20 63  xInfo.  So for c
3a2a0 6c 65 61 6e 75 70 20 69 74 20 69 73 20 0a 2a 2a  leanup it is .**
3a2b0 20 73 75 66 66 69 63 69 65 6e 74 20 74 6f 20 66   sufficient to f
3a2c0 72 65 65 20 61 6c 6c 20 6f 66 20 74 68 65 20 70  ree all of the p
3a2d0 49 64 78 49 6e 66 6f 20 70 6f 69 6e 74 65 72 73  IdxInfo pointers
3a2e0 2e 0a 2a 2a 20 0a 2a 2f 0a 73 74 72 75 63 74 20  ..** .*/.struct 
3a2f0 57 68 65 72 65 4c 65 76 65 6c 20 7b 0a 20 20 69  WhereLevel {.  i
3a300 6e 74 20 69 46 72 6f 6d 3b 20 20 20 20 20 20 20  nt iFrom;       
3a310 20 20 20 20 20 2f 2a 20 57 68 69 63 68 20 65 6e       /* Which en
3a320 74 72 79 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  try in the FROM 
3a330 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20  clause */.  int 
3a340 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20  flags;          
3a350 20 20 2f 2a 20 46 6c 61 67 73 20 61 73 73 6f 63    /* Flags assoc
3a360 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20  iated with this 
3a370 6c 65 76 65 6c 20 2a 2f 0a 20 20 69 6e 74 20 69  level */.  int i
3a380 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  Mem;            
3a390 20 2f 2a 20 46 69 72 73 74 20 6d 65 6d 6f 72 79   /* First memory
3a3a0 20 63 65 6c 6c 20 75 73 65 64 20 62 79 20 74 68   cell used by th
3a3b0 69 73 20 6c 65 76 65 6c 20 2a 2f 0a 20 20 69 6e  is level */.  in
3a3c0 74 20 69 4c 65 66 74 4a 6f 69 6e 3b 20 20 20 20  t iLeftJoin;    
3a3d0 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65      /* Memory ce
3a3e0 6c 6c 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  ll used to imple
3a3f0 6d 65 6e 74 20 4c 45 46 54 20 4f 55 54 45 52 20  ment LEFT OUTER 
3a400 4a 4f 49 4e 20 2a 2f 0a 20 20 49 6e 64 65 78 20  JOIN */.  Index 
3a410 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20 20 20  *pIdx;          
3a420 2f 2a 20 49 6e 64 65 78 20 75 73 65 64 2e 20 20  /* Index used.  
3a430 4e 55 4c 4c 20 69 66 20 6e 6f 20 69 6e 64 65 78  NULL if no index
3a440 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 43 75   */.  int iTabCu
3a450 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  r;          /* T
3a460 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 75  he VDBE cursor u
3a470 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68  sed to access th
3a480 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  e table */.  int
3a490 20 69 49 64 78 43 75 72 3b 20 20 20 20 20 20 20   iIdxCur;       
3a4a0 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63     /* The VDBE c
3a4b0 75 72 73 6f 72 20 75 73 65 64 20 74 6f 20 61 63  ursor used to ac
3a4c0 65 73 73 73 20 70 49 64 78 20 2a 2f 0a 20 20 69  esss pIdx */.  i
3a4d0 6e 74 20 62 72 6b 3b 20 20 20 20 20 20 20 20 20  nt brk;         
3a4e0 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72       /* Jump her
3a4f0 65 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f  e to break out o
3a500 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20  f the loop */.  
3a510 69 6e 74 20 6e 78 74 3b 20 20 20 20 20 20 20 20  int nxt;        
3a520 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65        /* Jump he
3a530 72 65 20 74 6f 20 73 74 61 72 74 20 74 68 65 20  re to start the 
3a540 6e 65 78 74 20 49 4e 20 63 6f 6d 62 69 6e 61 74  next IN combinat
3a550 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6e  ion */.  int con
3a560 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  t;             /
3a570 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 63  * Jump here to c
3a580 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 74 68 65  ontinue with the
3a590 20 6e 65 78 74 20 6c 6f 6f 70 20 63 79 63 6c 65   next loop cycle
3a5a0 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 70 3b 20 20   */.  int top;  
3a5b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
3a5c0 69 72 73 74 20 69 6e 73 74 72 75 63 74 69 6f 6e  irst instruction
3a5d0 20 6f 66 20 69 6e 74 65 72 69 6f 72 20 6f 66 20   of interior of 
3a5e0 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e  the loop */.  in
3a5f0 74 20 6f 70 2c 20 70 31 2c 20 70 32 3b 20 20 20  t op, p1, p2;   
3a600 20 20 20 20 2f 2a 20 4f 70 63 6f 64 65 20 75 73      /* Opcode us
3a610 65 64 20 74 6f 20 74 65 72 6d 69 6e 61 74 65 20  ed to terminate 
3a620 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e  the loop */.  in
3a630 74 20 6e 45 71 3b 20 20 20 20 20 20 20 20 20 20  t nEq;          
3a640 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
3a650 20 3d 3d 20 6f 72 20 49 4e 20 63 6f 6e 73 74 72   == or IN constr
3a660 61 69 6e 74 73 20 6f 6e 20 74 68 69 73 20 6c 6f  aints on this lo
3a670 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e 3b  op */.  int nIn;
3a680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3a690 20 4e 75 6d 62 65 72 20 6f 66 20 49 4e 20 6f 70   Number of IN op
3a6a0 65 72 61 74 6f 72 73 20 63 6f 6e 73 74 72 61 69  erators constrai
3a6b0 6e 69 6e 67 20 74 68 69 73 20 6c 6f 6f 70 20 2a  ning this loop *
3a6c0 2f 0a 20 20 73 74 72 75 63 74 20 49 6e 4c 6f 6f  /.  struct InLoo
3a6d0 70 20 7b 0a 20 20 20 20 69 6e 74 20 69 43 75 72  p {.    int iCur
3a6e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
3a6f0 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f  * The VDBE curso
3a700 72 20 75 73 65 64 20 62 79 20 74 68 69 73 20 49  r used by this I
3a710 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20  N operator */.  
3a720 20 20 69 6e 74 20 74 6f 70 41 64 64 72 3b 20 20    int topAddr;  
3a730 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 70 20           /* Top 
3a740 6f 66 20 74 68 65 20 49 4e 20 6c 6f 6f 70 20 2a  of the IN loop *
3a750 2f 0a 20 20 7d 20 2a 61 49 6e 4c 6f 6f 70 3b 20  /.  } *aInLoop; 
3a760 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66            /* Inf
3a770 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65  ormation about e
3a780 61 63 68 20 6e 65 73 74 65 64 20 49 4e 20 6f 70  ach nested IN op
3a790 65 72 61 74 6f 72 20 2a 2f 0a 20 20 73 71 6c 69  erator */.  sqli
3a7a0 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a  te3_index_info *
3a7b0 70 42 65 73 74 49 64 78 3b 20 20 2f 2a 20 49 6e  pBestIdx;  /* In
3a7c0 64 65 78 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  dex information 
3a7d0 66 6f 72 20 74 68 69 73 20 6c 65 76 65 6c 20 2a  for this level *
3a7e0 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c  /..  /* The foll
3a7f0 6f 77 69 6e 67 20 66 69 65 6c 64 20 69 73 20 72  owing field is r
3a800 65 61 6c 6c 79 20 6e 6f 74 20 70 61 72 74 20 6f  eally not part o
3a810 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 65  f the current le
3a820 76 65 6c 2e 20 20 42 75 74 0a 20 20 2a 2a 20 77  vel.  But.  ** w
3a830 65 20 6e 65 65 64 20 61 20 70 6c 61 63 65 20 74  e need a place t
3a840 6f 20 63 61 63 68 65 20 69 6e 64 65 78 20 69 6e  o cache index in
3a850 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 65 61  formation for ea
3a860 63 68 20 74 61 62 6c 65 20 69 6e 20 74 68 65 0a  ch table in the.
3a870 20 20 2a 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65    ** FROM clause
3a880 20 61 6e 64 20 74 68 65 20 57 68 65 72 65 4c 65   and the WhereLe
3a890 76 65 6c 20 73 74 72 75 63 74 75 72 65 20 69 73  vel structure is
3a8a0 20 61 20 63 6f 6e 76 65 6e 69 65 6e 74 20 70 6c   a convenient pl
3a8b0 61 63 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  ace..  */.  sqli
3a8c0 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a  te3_index_info *
3a8d0 70 49 64 78 49 6e 66 6f 3b 20 20 2f 2a 20 49 6e  pIdxInfo;  /* In
3a8e0 64 65 78 20 69 6e 66 6f 20 66 6f 72 20 6e 2d 74  dex info for n-t
3a8f0 68 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20 2a  h source table *
3a900 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  /.};../*.** The 
3a910 57 48 45 52 45 20 63 6c 61 75 73 65 20 70 72 6f  WHERE clause pro
3a920 63 65 73 73 69 6e 67 20 72 6f 75 74 69 6e 65 20  cessing routine 
3a930 68 61 73 20 74 77 6f 20 68 61 6c 76 65 73 2e 20  has two halves. 
3a940 20 54 68 65 0a 2a 2a 20 66 69 72 73 74 20 70 61   The.** first pa
3a950 72 74 20 64 6f 65 73 20 74 68 65 20 73 74 61 72  rt does the star
3a960 74 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 6c  t of the WHERE l
3a970 6f 6f 70 20 61 6e 64 20 74 68 65 20 73 65 63 6f  oop and the seco
3a980 6e 64 0a 2a 2a 20 68 61 6c 66 20 64 6f 65 73 20  nd.** half does 
3a990 74 68 65 20 74 61 69 6c 20 6f 66 20 74 68 65 20  the tail of the 
3a9a0 57 48 45 52 45 20 6c 6f 6f 70 2e 20 20 41 6e 20  WHERE loop.  An 
3a9b0 69 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74  instance of.** t
3a9c0 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73  his structure is
3a9d0 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65   returned by the
3a9e0 20 66 69 72 73 74 20 68 61 6c 66 20 61 6e 64 20   first half and 
3a9f0 70 61 73 73 65 64 0a 2a 2a 20 69 6e 74 6f 20 74  passed.** into t
3aa00 68 65 20 73 65 63 6f 6e 64 20 68 61 6c 66 20 74  he second half t
3aa10 6f 20 67 69 76 65 20 73 6f 6d 65 20 63 6f 6e 74  o give some cont
3aa20 69 6e 75 69 74 79 2e 0a 2a 2f 0a 73 74 72 75 63  inuity..*/.struc
3aa30 74 20 57 68 65 72 65 49 6e 66 6f 20 7b 0a 20 20  t WhereInfo {.  
3aa40 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 0a 20  Parse *pParse;. 
3aa50 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69   SrcList *pTabLi
3aa60 73 74 3b 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66  st;   /* List of
3aa70 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 6a   tables in the j
3aa80 6f 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 6f  oin */.  int iTo
3aa90 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  p;            /*
3aaa0 20 54 68 65 20 76 65 72 79 20 62 65 67 69 6e 6e   The very beginn
3aab0 69 6e 67 20 6f 66 20 74 68 65 20 57 48 45 52 45  ing of the WHERE
3aac0 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 69   loop */.  int i
3aad0 43 6f 6e 74 69 6e 75 65 3b 20 20 20 20 20 20 20  Continue;       
3aae0 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20  /* Jump here to 
3aaf0 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 6e 65  continue with ne
3ab00 78 74 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69  xt record */.  i
3ab10 6e 74 20 69 42 72 65 61 6b 3b 20 20 20 20 20 20  nt iBreak;      
3ab20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65      /* Jump here
3ab30 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66   to break out of
3ab40 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69   the loop */.  i
3ab50 6e 74 20 6e 4c 65 76 65 6c 3b 20 20 20 20 20 20  nt nLevel;      
3ab60 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
3ab70 20 6e 65 73 74 65 64 20 6c 6f 6f 70 20 2a 2f 0a   nested loop */.
3ab80 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f    sqlite3_index_
3ab90 69 6e 66 6f 20 2a 2a 61 70 49 6e 66 6f 3b 20 20  info **apInfo;  
3aba0 2f 2a 20 41 72 72 61 79 20 6f 66 20 70 6f 69 6e  /* Array of poin
3abb0 74 65 72 73 20 74 6f 20 69 6e 64 65 78 20 69 6e  ters to index in
3abc0 66 6f 20 73 74 72 75 63 74 75 72 65 73 20 2a 2f  fo structures */
3abd0 0a 20 20 57 68 65 72 65 4c 65 76 65 6c 20 61 5b  .  WhereLevel a[
3abe0 31 5d 3b 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72  1];     /* Infor
3abf0 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 63  mation about eac
3ac00 68 20 6e 65 73 74 20 6c 6f 6f 70 20 69 6e 20 74  h nest loop in t
3ac10 68 65 20 57 48 45 52 45 20 2a 2f 0a 7d 3b 0a 0a  he WHERE */.};..
3ac20 2f 2a 0a 2a 2a 20 41 20 4e 61 6d 65 43 6f 6e 74  /*.** A NameCont
3ac30 65 78 74 20 64 65 66 69 6e 65 73 20 61 20 63 6f  ext defines a co
3ac40 6e 74 65 78 74 20 69 6e 20 77 68 69 63 68 20 74  ntext in which t
3ac50 6f 20 72 65 73 6f 6c 76 65 20 74 61 62 6c 65 20  o resolve table 
3ac60 61 6e 64 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6e 61  and column.** na
3ac70 6d 65 73 2e 20 20 54 68 65 20 63 6f 6e 74 65 78  mes.  The contex
3ac80 74 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20  t consists of a 
3ac90 6c 69 73 74 20 6f 66 20 74 61 62 6c 65 73 20 28  list of tables (
3aca0 74 68 65 20 70 53 72 63 4c 69 73 74 29 20 66 69  the pSrcList) fi
3acb0 65 6c 64 20 61 6e 64 0a 2a 2a 20 61 20 6c 69 73  eld and.** a lis
3acc0 74 20 6f 66 20 6e 61 6d 65 64 20 65 78 70 72 65  t of named expre
3acd0 73 73 69 6f 6e 20 28 70 45 4c 69 73 74 29 2e 20  ssion (pEList). 
3ace0 20 54 68 65 20 6e 61 6d 65 64 20 65 78 70 72 65   The named expre
3acf0 73 73 69 6f 6e 20 6c 69 73 74 20 6d 61 79 0a 2a  ssion list may.*
3ad00 2a 20 62 65 20 4e 55 4c 4c 2e 20 20 54 68 65 20  * be NULL.  The 
3ad10 70 53 72 63 20 63 6f 72 72 65 73 70 6f 6e 64 73  pSrc corresponds
3ad20 20 74 6f 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   to the FROM cla
3ad30 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43 54 20  use of a SELECT 
3ad40 6f 72 0a 2a 2a 20 74 6f 20 74 68 65 20 74 61 62  or.** to the tab
3ad50 6c 65 20 62 65 69 6e 67 20 6f 70 65 72 61 74 65  le being operate
3ad60 64 20 6f 6e 20 62 79 20 49 4e 53 45 52 54 2c 20  d on by INSERT, 
3ad70 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c 45 54  UPDATE, or DELET
3ad80 45 2e 20 20 54 68 65 0a 2a 2a 20 70 45 4c 69 73  E.  The.** pELis
3ad90 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f  t corresponds to
3ada0 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
3adb0 6f 66 20 61 20 53 45 4c 45 43 54 20 61 6e 64 20  of a SELECT and 
3adc0 69 73 20 4e 55 4c 4c 20 66 6f 72 0a 2a 2a 20 6f  is NULL for.** o
3add0 74 68 65 72 20 73 74 61 74 65 6d 65 6e 74 73 2e  ther statements.
3ade0 0a 2a 2a 0a 2a 2a 20 4e 61 6d 65 43 6f 6e 74 65  .**.** NameConte
3adf0 78 74 73 20 63 61 6e 20 62 65 20 6e 65 73 74 65  xts can be neste
3ae00 64 2e 20 20 57 68 65 6e 20 72 65 73 6f 6c 76 69  d.  When resolvi
3ae10 6e 67 20 6e 61 6d 65 73 2c 20 74 68 65 20 69 6e  ng names, the in
3ae20 6e 65 72 2d 6d 6f 73 74 20 0a 2a 2a 20 63 6f 6e  ner-most .** con
3ae30 74 65 78 74 20 69 73 20 73 65 61 72 63 68 65 64  text is searched
3ae40 20 66 69 72 73 74 2e 20 20 49 66 20 6e 6f 20 6d   first.  If no m
3ae50 61 74 63 68 20 69 73 20 66 6f 75 6e 64 2c 20 74  atch is found, t
3ae60 68 65 20 6e 65 78 74 20 6f 75 74 65 72 0a 2a 2a  he next outer.**
3ae70 20 63 6f 6e 74 65 78 74 20 69 73 20 63 68 65 63   context is chec
3ae80 6b 65 64 2e 20 20 49 66 20 74 68 65 72 65 20 69  ked.  If there i
3ae90 73 20 73 74 69 6c 6c 20 6e 6f 20 6d 61 74 63 68  s still no match
3aea0 2c 20 74 68 65 20 6e 65 78 74 20 63 6f 6e 74 65  , the next conte
3aeb0 78 74 0a 2a 2a 20 69 73 20 63 68 65 63 6b 65 64  xt.** is checked
3aec0 2e 20 20 54 68 69 73 20 70 72 6f 63 65 73 73 20  .  This process 
3aed0 63 6f 6e 74 69 6e 75 65 73 20 75 6e 74 69 6c 20  continues until 
3aee0 65 69 74 68 65 72 20 61 20 6d 61 74 63 68 20 69  either a match i
3aef0 73 20 66 6f 75 6e 64 0a 2a 2a 20 6f 72 20 61 6c  s found.** or al
3af00 6c 20 63 6f 6e 74 65 78 74 73 20 61 72 65 20 63  l contexts are c
3af10 68 65 63 6b 2e 20 20 57 68 65 6e 20 61 20 6d 61  heck.  When a ma
3af20 74 63 68 20 69 73 20 66 6f 75 6e 64 2c 20 74 68  tch is found, th
3af30 65 20 6e 52 65 66 20 6d 65 6d 62 65 72 20 6f 66  e nRef member of
3af40 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 78 74 20  .** the context 
3af50 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6d  containing the m
3af60 61 74 63 68 20 69 73 20 69 6e 63 72 65 6d 65 6e  atch is incremen
3af70 74 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 45 61 63 68  ted. .**.** Each
3af80 20 73 75 62 71 75 65 72 79 20 67 65 74 73 20 61   subquery gets a
3af90 20 6e 65 77 20 4e 61 6d 65 43 6f 6e 74 65 78 74   new NameContext
3afa0 2e 20 20 54 68 65 20 70 4e 65 78 74 20 66 69 65  .  The pNext fie
3afb0 6c 64 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65  ld points to the
3afc0 0a 2a 2a 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  .** NameContext 
3afd0 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20 71 75  in the parent qu
3afe0 65 72 79 2e 20 20 54 68 75 73 20 74 68 65 20 70  ery.  Thus the p
3aff0 72 6f 63 65 73 73 20 6f 66 20 73 63 61 6e 6e 69  rocess of scanni
3b000 6e 67 20 74 68 65 0a 2a 2a 20 4e 61 6d 65 43 6f  ng the.** NameCo
3b010 6e 74 65 78 74 20 6c 69 73 74 20 63 6f 72 72 65  ntext list corre
3b020 73 70 6f 6e 64 73 20 74 6f 20 73 65 61 72 63 68  sponds to search
3b030 69 6e 67 20 74 68 72 6f 75 67 68 20 73 75 63 63  ing through succ
3b040 65 73 73 69 76 65 6c 79 20 6f 75 74 65 72 0a 2a  essively outer.*
3b050 2a 20 73 75 62 71 75 65 72 69 65 73 20 6c 6f 6f  * subqueries loo
3b060 6b 69 6e 67 20 66 6f 72 20 61 20 6d 61 74 63 68  king for a match
3b070 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4e 61 6d 65  ..*/.struct Name
3b080 43 6f 6e 74 65 78 74 20 7b 0a 20 20 50 61 72 73  Context {.  Pars
3b090 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20  e *pParse;      
3b0a0 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a   /* The parser *
3b0b0 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  /.  SrcList *pSr
3b0c0 63 4c 69 73 74 3b 20 20 20 2f 2a 20 4f 6e 65 20  cList;   /* One 
3b0d0 6f 72 20 6d 6f 72 65 20 74 61 62 6c 65 73 20 75  or more tables u
3b0e0 73 65 64 20 74 6f 20 72 65 73 6f 6c 76 65 20 6e  sed to resolve n
3b0f0 61 6d 65 73 20 2a 2f 0a 20 20 45 78 70 72 4c 69  ames */.  ExprLi
3b100 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 20 2f  st *pEList;    /
3b110 2a 20 4f 70 74 69 6f 6e 61 6c 20 6c 69 73 74 20  * Optional list 
3b120 6f 66 20 6e 61 6d 65 64 20 65 78 70 72 65 73 73  of named express
3b130 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 52  ions */.  int nR
3b140 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ef;            /
3b150 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 61 6d 65  * Number of name
3b160 73 20 72 65 73 6f 6c 76 65 64 20 62 79 20 74 68  s resolved by th
3b170 69 73 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  is context */.  
3b180 69 6e 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20  int nErr;       
3b190 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
3b1a0 66 20 65 72 72 6f 72 73 20 65 6e 63 6f 75 6e 74  f errors encount
3b1b0 65 72 65 64 20 77 68 69 6c 65 20 72 65 73 6f 6c  ered while resol
3b1c0 76 69 6e 67 20 6e 61 6d 65 73 20 2a 2f 0a 20 20  ving names */.  
3b1d0 75 38 20 61 6c 6c 6f 77 41 67 67 3b 20 20 20 20  u8 allowAgg;    
3b1e0 20 20 20 20 20 2f 2a 20 41 67 67 72 65 67 61 74       /* Aggregat
3b1f0 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 6c 6c 6f  e functions allo
3b200 77 65 64 20 68 65 72 65 20 2a 2f 0a 20 20 75 38  wed here */.  u8
3b210 20 68 61 73 41 67 67 3b 20 20 20 20 20 20 20 20   hasAgg;        
3b220 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 67     /* True if ag
3b230 67 72 65 67 61 74 65 73 20 61 72 65 20 73 65 65  gregates are see
3b240 6e 20 2a 2f 0a 20 20 75 38 20 69 73 43 68 65 63  n */.  u8 isChec
3b250 6b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  k;          /* T
3b260 72 75 65 20 69 66 20 72 65 73 6f 6c 76 69 6e 67  rue if resolving
3b270 20 6e 61 6d 65 73 20 69 6e 20 61 20 43 48 45 43   names in a CHEC
3b280 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a  K constraint */.
3b290 20 20 69 6e 74 20 6e 44 65 70 74 68 3b 20 20 20    int nDepth;   
3b2a0 20 20 20 20 20 20 20 2f 2a 20 44 65 70 74 68 20         /* Depth 
3b2b0 6f 66 20 73 75 62 71 75 65 72 79 20 72 65 63 75  of subquery recu
3b2c0 72 73 69 6f 6e 2e 20 31 20 66 6f 72 20 6e 6f 20  rsion. 1 for no 
3b2d0 72 65 63 75 72 73 69 6f 6e 20 2a 2f 0a 20 20 41  recursion */.  A
3b2e0 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f  ggInfo *pAggInfo
3b2f0 3b 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69  ;   /* Informati
3b300 6f 6e 20 61 62 6f 75 74 20 61 67 67 72 65 67 61  on about aggrega
3b310 74 65 73 20 61 74 20 74 68 69 73 20 6c 65 76 65  tes at this leve
3b320 6c 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65  l */.  NameConte
3b330 78 74 20 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 4e  xt *pNext;  /* N
3b340 65 78 74 20 6f 75 74 65 72 20 6e 61 6d 65 20 63  ext outer name c
3b350 6f 6e 74 65 78 74 2e 20 20 4e 55 4c 4c 20 66 6f  ontext.  NULL fo
3b360 72 20 6f 75 74 65 72 6d 6f 73 74 20 2a 2f 0a 7d  r outermost */.}
3b370 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74  ;../*.** An inst
3b380 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
3b390 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
3b3a0 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 69 6e 66  contains all inf
3b3b0 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 6e 65 65 64  ormation.** need
3b3c0 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20 63  ed to generate c
3b3d0 6f 64 65 20 66 6f 72 20 61 20 73 69 6e 67 6c 65  ode for a single
3b3e0 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
3b3f0 74 2e 0a 2a 2a 0a 2a 2a 20 6e 4c 69 6d 69 74 20  t..**.** nLimit 
3b400 69 73 20 73 65 74 20 74 6f 20 2d 31 20 69 66 20  is set to -1 if 
3b410 74 68 65 72 65 20 69 73 20 6e 6f 20 4c 49 4d 49  there is no LIMI
3b420 54 20 63 6c 61 75 73 65 2e 20 20 6e 4f 66 66 73  T clause.  nOffs
3b430 65 74 20 69 73 20 73 65 74 20 74 6f 20 30 2e 0a  et is set to 0..
3b440 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61  ** If there is a
3b450 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 2c 20 74   LIMIT clause, t
3b460 68 65 20 70 61 72 73 65 72 20 73 65 74 73 20 6e  he parser sets n
3b470 4c 69 6d 69 74 20 74 6f 20 74 68 65 20 76 61 6c  Limit to the val
3b480 75 65 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 69 6d  ue of the.** lim
3b490 69 74 20 61 6e 64 20 6e 4f 66 66 73 65 74 20 74  it and nOffset t
3b4a0 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  o the value of t
3b4b0 68 65 20 6f 66 66 73 65 74 20 28 6f 72 20 30 20  he offset (or 0 
3b4c0 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74 0a  if there is not.
3b4d0 2a 2a 20 6f 66 66 73 65 74 29 2e 20 20 42 75 74  ** offset).  But
3b4e0 20 6c 61 74 65 72 20 6f 6e 2c 20 6e 4c 69 6d 69   later on, nLimi
3b4f0 74 20 61 6e 64 20 6e 4f 66 66 73 65 74 20 62 65  t and nOffset be
3b500 63 6f 6d 65 20 74 68 65 20 6d 65 6d 6f 72 79 20  come the memory 
3b510 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20 69 6e 20  locations.** in 
3b520 74 68 65 20 56 44 42 45 20 74 68 61 74 20 72 65  the VDBE that re
3b530 63 6f 72 64 20 74 68 65 20 6c 69 6d 69 74 20 61  cord the limit a
3b540 6e 64 20 6f 66 66 73 65 74 20 63 6f 75 6e 74 65  nd offset counte
3b550 72 73 2e 0a 2a 2a 0a 2a 2a 20 61 64 64 72 4f 70  rs..**.** addrOp
3b560 65 6e 45 70 68 6d 5b 5d 20 65 6e 74 72 69 65 73  enEphm[] entries
3b570 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 61 64 64   contain the add
3b580 72 65 73 73 20 6f 66 20 4f 50 5f 4f 70 65 6e 45  ress of OP_OpenE
3b590 70 68 65 6d 65 72 61 6c 20 6f 70 63 6f 64 65 73  phemeral opcodes
3b5a0 2e 0a 2a 2a 20 54 68 65 73 65 20 61 64 64 72 65  ..** These addre
3b5b0 73 73 65 73 20 6d 75 73 74 20 62 65 20 73 74 6f  sses must be sto
3b5c0 72 65 64 20 73 6f 20 74 68 61 74 20 77 65 20 63  red so that we c
3b5d0 61 6e 20 67 6f 20 62 61 63 6b 20 61 6e 64 20 66  an go back and f
3b5e0 69 6c 6c 20 69 6e 0a 2a 2a 20 74 68 65 20 50 33  ill in.** the P3
3b5f0 5f 4b 45 59 49 4e 46 4f 20 61 6e 64 20 50 32 20  _KEYINFO and P2 
3b600 70 61 72 61 6d 65 74 65 72 73 20 6c 61 74 65 72  parameters later
3b610 2e 20 20 4e 65 69 74 68 65 72 20 74 68 65 20 4b  .  Neither the K
3b620 65 79 49 6e 66 6f 20 6e 6f 72 0a 2a 2a 20 74 68  eyInfo nor.** th
3b630 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  e number of colu
3b640 6d 6e 73 20 69 6e 20 50 32 20 63 61 6e 20 62 65  mns in P2 can be
3b650 20 63 6f 6d 70 75 74 65 64 20 61 74 20 74 68 65   computed at the
3b660 20 73 61 6d 65 20 74 69 6d 65 0a 2a 2a 20 61 73   same time.** as
3b670 20 74 68 65 20 4f 50 5f 4f 70 65 6e 45 70 68 6d   the OP_OpenEphm
3b680 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20   instruction is 
3b690 63 6f 64 65 64 20 62 65 63 61 75 73 65 20 6e 6f  coded because no
3b6a0 74 0a 2a 2a 20 65 6e 6f 75 67 68 20 69 6e 66 6f  t.** enough info
3b6b0 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68  rmation about th
3b6c0 65 20 63 6f 6d 70 6f 75 6e 64 20 71 75 65 72 79  e compound query
3b6d0 20 69 73 20 6b 6e 6f 77 6e 20 61 74 20 74 68 61   is known at tha
3b6e0 74 20 70 6f 69 6e 74 2e 0a 2a 2a 20 54 68 65 20  t point..** The 
3b6f0 4b 65 79 49 6e 66 6f 20 66 6f 72 20 61 64 64 72  KeyInfo for addr
3b700 4f 70 65 6e 54 72 61 6e 5b 30 5d 20 61 6e 64 20  OpenTran[0] and 
3b710 5b 31 5d 20 63 6f 6e 74 61 69 6e 73 20 63 6f 6c  [1] contains col
3b720 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73  lating sequences
3b730 0a 2a 2a 20 66 6f 72 20 74 68 65 20 72 65 73 75  .** for the resu
3b740 6c 74 20 73 65 74 2e 20 20 54 68 65 20 4b 65 79  lt set.  The Key
3b750 49 6e 66 6f 20 66 6f 72 20 61 64 64 72 4f 70 65  Info for addrOpe
3b760 6e 54 72 61 6e 5b 32 5d 20 63 6f 6e 74 61 69 6e  nTran[2] contain
3b770 73 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73  s collating.** s
3b780 65 71 75 65 6e 63 65 73 20 66 6f 72 20 74 68 65  equences for the
3b790 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
3b7a0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 65 6c 65  ..*/.struct Sele
3b7b0 63 74 20 7b 0a 20 20 45 78 70 72 4c 69 73 74 20  ct {.  ExprList 
3b7c0 2a 70 45 4c 69 73 74 3b 20 20 20 20 20 20 2f 2a  *pEList;      /*
3b7d0 20 54 68 65 20 66 69 65 6c 64 73 20 6f 66 20 74   The fields of t
3b7e0 68 65 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20 75  he result */.  u
3b7f0 38 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20  8 op;           
3b800 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 3a        /* One of:
3b810 20 54 4b 5f 55 4e 49 4f 4e 20 54 4b 5f 41 4c 4c   TK_UNION TK_ALL
3b820 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 54 4b   TK_INTERSECT TK
3b830 5f 45 58 43 45 50 54 20 2a 2f 0a 20 20 75 38 20  _EXCEPT */.  u8 
3b840 69 73 44 69 73 74 69 6e 63 74 3b 20 20 20 20 20  isDistinct;     
3b850 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
3b860 68 65 20 44 49 53 54 49 4e 43 54 20 6b 65 79 77  he DISTINCT keyw
3b870 6f 72 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a  ord is present *
3b880 2f 0a 20 20 75 38 20 69 73 52 65 73 6f 6c 76 65  /.  u8 isResolve
3b890 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  d;         /* Tr
3b8a0 75 65 20 6f 6e 63 65 20 73 71 6c 69 74 65 33 53  ue once sqlite3S
3b8b0 65 6c 65 63 74 52 65 73 6f 6c 76 65 28 29 20 68  electResolve() h
3b8c0 61 73 20 72 75 6e 2e 20 2a 2f 0a 20 20 75 38 20  as run. */.  u8 
3b8d0 69 73 41 67 67 3b 20 20 20 20 20 20 20 20 20 20  isAgg;          
3b8e0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
3b8f0 68 69 73 20 69 73 20 61 6e 20 61 67 67 72 65 67  his is an aggreg
3b900 61 74 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 75  ate query */.  u
3b910 38 20 75 73 65 73 45 70 68 6d 3b 20 20 20 20 20  8 usesEphm;     
3b920 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
3b930 20 75 73 65 73 20 61 6e 20 4f 70 65 6e 45 70 68   uses an OpenEph
3b940 65 6d 65 72 61 6c 20 6f 70 63 6f 64 65 20 2a 2f  emeral opcode */
3b950 0a 20 20 75 38 20 64 69 73 61 6c 6c 6f 77 4f 72  .  u8 disallowOr
3b960 64 65 72 42 79 3b 20 20 20 20 2f 2a 20 44 6f 20  derBy;    /* Do 
3b970 6e 6f 74 20 61 6c 6c 6f 77 20 61 6e 20 4f 52 44  not allow an ORD
3b980 45 52 20 42 59 20 74 6f 20 62 65 20 61 74 74 61  ER BY to be atta
3b990 63 68 65 64 20 69 66 20 54 52 55 45 20 2a 2f 0a  ched if TRUE */.
3b9a0 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b    char affinity;
3b9b0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65           /* Make
3b9c0 52 65 63 6f 72 64 20 77 69 74 68 20 74 68 69 73  Record with this
3b9d0 20 61 66 66 69 6e 69 74 79 20 66 6f 72 20 53 52   affinity for SR
3b9e0 54 5f 53 65 74 20 2a 2f 0a 20 20 53 72 63 4c 69  T_Set */.  SrcLi
3b9f0 73 74 20 2a 70 53 72 63 3b 20 20 20 20 20 20 20  st *pSrc;       
3ba00 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c    /* The FROM cl
3ba10 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a  ause */.  Expr *
3ba20 70 57 68 65 72 65 3b 20 20 20 20 20 20 20 20 20  pWhere;         
3ba30 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c   /* The WHERE cl
3ba40 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69  ause */.  ExprLi
3ba50 73 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 20  st *pGroupBy;   
3ba60 20 2f 2a 20 54 68 65 20 47 52 4f 55 50 20 42 59   /* The GROUP BY
3ba70 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70   clause */.  Exp
3ba80 72 20 2a 70 48 61 76 69 6e 67 3b 20 20 20 20 20  r *pHaving;     
3ba90 20 20 20 20 2f 2a 20 54 68 65 20 48 41 56 49 4e      /* The HAVIN
3baa0 47 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78  G clause */.  Ex
3bab0 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79  prList *pOrderBy
3bac0 3b 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45  ;    /* The ORDE
3bad0 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20  R BY clause */. 
3bae0 20 53 65 6c 65 63 74 20 2a 70 50 72 69 6f 72 3b   Select *pPrior;
3baf0 20 20 20 20 20 20 20 20 2f 2a 20 50 72 69 6f 72          /* Prior
3bb00 20 73 65 6c 65 63 74 20 69 6e 20 61 20 63 6f 6d   select in a com
3bb10 70 6f 75 6e 64 20 73 65 6c 65 63 74 20 73 74 61  pound select sta
3bb20 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 53 65 6c 65  tement */.  Sele
3bb30 63 74 20 2a 70 52 69 67 68 74 6d 6f 73 74 3b 20  ct *pRightmost; 
3bb40 20 20 20 2f 2a 20 52 69 67 68 74 2d 6d 6f 73 74     /* Right-most
3bb50 20 73 65 6c 65 63 74 20 69 6e 20 61 20 63 6f 6d   select in a com
3bb60 70 6f 75 6e 64 20 73 65 6c 65 63 74 20 73 74 61  pound select sta
3bb70 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72  tement */.  Expr
3bb80 20 2a 70 4c 69 6d 69 74 3b 20 20 20 20 20 20 20   *pLimit;       
3bb90 20 20 20 2f 2a 20 4c 49 4d 49 54 20 65 78 70 72     /* LIMIT expr
3bba0 65 73 73 69 6f 6e 2e 20 4e 55 4c 4c 20 6d 65 61  ession. NULL mea
3bbb0 6e 73 20 6e 6f 74 20 75 73 65 64 2e 20 2a 2f 0a  ns not used. */.
3bbc0 20 20 45 78 70 72 20 2a 70 4f 66 66 73 65 74 3b    Expr *pOffset;
3bbd0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 46 46 53           /* OFFS
3bbe0 45 54 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 4e  ET expression. N
3bbf0 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f 74 20 75 73  ULL means not us
3bc00 65 64 2e 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 69  ed. */.  int iLi
3bc10 6d 69 74 2c 20 69 4f 66 66 73 65 74 3b 20 20 20  mit, iOffset;   
3bc20 2f 2a 20 4d 65 6d 6f 72 79 20 72 65 67 69 73 74  /* Memory regist
3bc30 65 72 73 20 68 6f 6c 64 69 6e 67 20 4c 49 4d 49  ers holding LIMI
3bc40 54 20 26 20 4f 46 46 53 45 54 20 63 6f 75 6e 74  T & OFFSET count
3bc50 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  ers */.  int add
3bc60 72 4f 70 65 6e 45 70 68 6d 5b 33 5d 3b 20 20 20  rOpenEphm[3];   
3bc70 2f 2a 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 20  /* OP_OpenEphem 
3bc80 6f 70 63 6f 64 65 73 20 72 65 6c 61 74 65 64 20  opcodes related 
3bc90 74 6f 20 74 68 69 73 20 73 65 6c 65 63 74 20 2a  to this select *
3bca0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  /.};../*.** The 
3bcb0 72 65 73 75 6c 74 73 20 6f 66 20 61 20 73 65 6c  results of a sel
3bcc0 65 63 74 20 63 61 6e 20 62 65 20 64 69 73 74 72  ect can be distr
3bcd0 69 62 75 74 65 64 20 69 6e 20 73 65 76 65 72 61  ibuted in severa
3bce0 6c 20 77 61 79 73 2e 0a 2a 2f 0a 23 64 65 66 69  l ways..*/.#defi
3bcf0 6e 65 20 53 52 54 5f 55 6e 69 6f 6e 20 20 20 20  ne SRT_Union    
3bd00 20 20 20 20 31 20 20 2f 2a 20 53 74 6f 72 65 20      1  /* Store 
3bd10 72 65 73 75 6c 74 20 61 73 20 6b 65 79 73 20 69  result as keys i
3bd20 6e 20 61 6e 20 69 6e 64 65 78 20 2a 2f 0a 23 64  n an index */.#d
3bd30 65 66 69 6e 65 20 53 52 54 5f 45 78 63 65 70 74  efine SRT_Except
3bd40 20 20 20 20 20 20 20 32 20 20 2f 2a 20 52 65 6d         2  /* Rem
3bd50 6f 76 65 20 72 65 73 75 6c 74 20 66 72 6f 6d 20  ove result from 
3bd60 61 20 55 4e 49 4f 4e 20 69 6e 64 65 78 20 2a 2f  a UNION index */
3bd70 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 44 69 73  .#define SRT_Dis
3bd80 63 61 72 64 20 20 20 20 20 20 33 20 20 2f 2a 20  card      3  /* 
3bd90 44 6f 20 6e 6f 74 20 73 61 76 65 20 74 68 65 20  Do not save the 
3bda0 72 65 73 75 6c 74 73 20 61 6e 79 77 68 65 72 65  results anywhere
3bdb0 20 2a 2f 0a 0a 2f 2a 20 54 68 65 20 4f 52 44 45   */../* The ORDE
3bdc0 52 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 69  R BY clause is i
3bdd0 67 6e 6f 72 65 64 20 66 6f 72 20 61 6c 6c 20 6f  gnored for all o
3bde0 66 20 74 68 65 20 61 62 6f 76 65 20 2a 2f 0a 23  f the above */.#
3bdf0 64 65 66 69 6e 65 20 49 67 6e 6f 72 61 62 6c 65  define Ignorable
3be00 4f 72 64 65 72 62 79 28 58 29 20 28 58 3c 3d 53  Orderby(X) (X<=S
3be10 52 54 5f 44 69 73 63 61 72 64 29 0a 0a 23 64 65  RT_Discard)..#de
3be20 66 69 6e 65 20 53 52 54 5f 43 61 6c 6c 62 61 63  fine SRT_Callbac
3be30 6b 20 20 20 20 20 34 20 20 2f 2a 20 49 6e 76 6f  k     4  /* Invo
3be40 6b 65 20 61 20 63 61 6c 6c 62 61 63 6b 20 77 69  ke a callback wi
3be50 74 68 20 65 61 63 68 20 72 6f 77 20 6f 66 20 72  th each row of r
3be60 65 73 75 6c 74 20 2a 2f 0a 23 64 65 66 69 6e 65  esult */.#define
3be70 20 53 52 54 5f 4d 65 6d 20 20 20 20 20 20 20 20   SRT_Mem        
3be80 20 20 35 20 20 2f 2a 20 53 74 6f 72 65 20 72 65    5  /* Store re
3be90 73 75 6c 74 20 69 6e 20 61 20 6d 65 6d 6f 72 79  sult in a memory
3bea0 20 63 65 6c 6c 20 2a 2f 0a 23 64 65 66 69 6e 65   cell */.#define
3beb0 20 53 52 54 5f 53 65 74 20 20 20 20 20 20 20 20   SRT_Set        
3bec0 20 20 36 20 20 2f 2a 20 53 74 6f 72 65 20 6e 6f    6  /* Store no
3bed0 6e 2d 6e 75 6c 6c 20 72 65 73 75 6c 74 73 20 61  n-null results a
3bee0 73 20 6b 65 79 73 20 69 6e 20 61 6e 20 69 6e 64  s keys in an ind
3bef0 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52  ex */.#define SR
3bf00 54 5f 54 61 62 6c 65 20 20 20 20 20 20 20 20 37  T_Table        7
3bf10 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c    /* Store resul
3bf20 74 20 61 73 20 64 61 74 61 20 77 69 74 68 20 61  t as data with a
3bf30 6e 20 61 75 74 6f 6d 61 74 69 63 20 72 6f 77 69  n automatic rowi
3bf40 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54  d */.#define SRT
3bf50 5f 45 70 68 65 6d 54 61 62 20 20 20 20 20 38 20  _EphemTab     8 
3bf60 20 2f 2a 20 43 72 65 61 74 65 20 74 72 61 6e 73   /* Create trans
3bf70 69 65 6e 74 20 74 61 62 20 61 6e 64 20 73 74 6f  ient tab and sto
3bf80 72 65 20 6c 69 6b 65 20 53 52 54 5f 54 61 62 6c  re like SRT_Tabl
3bf90 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54  e */.#define SRT
3bfa0 5f 53 75 62 72 6f 75 74 69 6e 65 20 20 20 39 20  _Subroutine   9 
3bfb0 20 2f 2a 20 43 61 6c 6c 20 61 20 73 75 62 72 6f   /* Call a subro
3bfc0 75 74 69 6e 65 20 74 6f 20 68 61 6e 64 6c 65 20  utine to handle 
3bfd0 72 65 73 75 6c 74 73 20 2a 2f 0a 23 64 65 66 69  results */.#defi
3bfe0 6e 65 20 53 52 54 5f 45 78 69 73 74 73 20 20 20  ne SRT_Exists   
3bff0 20 20 20 31 30 20 20 2f 2a 20 53 74 6f 72 65 20     10  /* Store 
3c000 31 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 20  1 if the result 
3c010 69 73 20 6e 6f 74 20 65 6d 70 74 79 20 2a 2f 0a  is not empty */.
3c020 0a 2f 2a 0a 2a 2a 20 41 6e 20 53 51 4c 20 70 61  ./*.** An SQL pa
3c030 72 73 65 72 20 63 6f 6e 74 65 78 74 2e 20 20 41  rser context.  A
3c040 20 63 6f 70 79 20 6f 66 20 74 68 69 73 20 73 74   copy of this st
3c050 72 75 63 74 75 72 65 20 69 73 20 70 61 73 73 65  ructure is passe
3c060 64 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 68 65  d through.** the
3c070 20 70 61 72 73 65 72 20 61 6e 64 20 64 6f 77 6e   parser and down
3c080 20 69 6e 74 6f 20 61 6c 6c 20 74 68 65 20 70 61   into all the pa
3c090 72 73 65 72 20 61 63 74 69 6f 6e 20 72 6f 75 74  rser action rout
3c0a0 69 6e 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a  ine in order to.
3c0b0 2a 2a 20 63 61 72 72 79 20 61 72 6f 75 6e 64 20  ** carry around 
3c0c0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74  information that
3c0d0 20 69 73 20 67 6c 6f 62 61 6c 20 74 6f 20 74 68   is global to th
3c0e0 65 20 65 6e 74 69 72 65 20 70 61 72 73 65 2e 0a  e entire parse..
3c0f0 2a 2a 0a 2a 2a 20 54 68 65 20 73 74 72 75 63 74  **.** The struct
3c100 75 72 65 20 69 73 20 64 69 76 69 64 65 64 20 69  ure is divided i
3c110 6e 74 6f 20 74 77 6f 20 70 61 72 74 73 2e 20 20  nto two parts.  
3c120 57 68 65 6e 20 74 68 65 20 70 61 72 73 65 72 20  When the parser 
3c130 61 6e 64 20 63 6f 64 65 0a 2a 2a 20 67 65 6e 65  and code.** gene
3c140 72 61 74 65 20 63 61 6c 6c 20 74 68 65 6d 73 65  rate call themse
3c150 6c 76 65 73 20 72 65 63 75 72 73 69 76 65 6c 79  lves recursively
3c160 2c 20 74 68 65 20 66 69 72 73 74 20 70 61 72 74  , the first part
3c170 20 6f 66 20 74 68 65 20 73 74 72 75 63 74 75 72   of the structur
3c180 65 0a 2a 2a 20 69 73 20 63 6f 6e 73 74 61 6e 74  e.** is constant
3c190 20 62 75 74 20 74 68 65 20 73 65 63 6f 6e 64 20   but the second 
3c1a0 70 61 72 74 20 69 73 20 72 65 73 65 74 20 61 74  part is reset at
3c1b0 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 61   the beginning a
3c1c0 6e 64 20 65 6e 64 20 6f 66 0a 2a 2a 20 65 61 63  nd end of.** eac
3c1d0 68 20 72 65 63 75 72 73 69 6f 6e 2e 0a 2a 2a 0a  h recursion..**.
3c1e0 2a 2a 20 54 68 65 20 6e 54 61 62 6c 65 4c 6f 63  ** The nTableLoc
3c1f0 6b 20 61 6e 64 20 61 54 61 62 6c 65 4c 6f 63 6b  k and aTableLock
3c200 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 6f   variables are o
3c210 6e 6c 79 20 75 73 65 64 20 69 66 20 74 68 65 20  nly used if the 
3c220 73 68 61 72 65 64 2d 63 61 63 68 65 20 0a 2a 2a  shared-cache .**
3c230 20 66 65 61 74 75 72 65 20 69 73 20 65 6e 61 62   feature is enab
3c240 6c 65 64 20 28 69 66 20 73 71 6c 69 74 65 33 54  led (if sqlite3T
3c250 73 64 28 29 2d 3e 75 73 65 53 68 61 72 65 64 44  sd()->useSharedD
3c260 61 74 61 20 69 73 20 74 72 75 65 29 2e 20 54 68  ata is true). Th
3c270 65 79 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 74  ey are.** used t
3c280 6f 20 73 74 6f 72 65 20 74 68 65 20 73 65 74 20  o store the set 
3c290 6f 66 20 74 61 62 6c 65 2d 6c 6f 63 6b 73 20 72  of table-locks r
3c2a0 65 71 75 69 72 65 64 20 62 79 20 74 68 65 20 73  equired by the s
3c2b0 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 0a 2a  tatement being.*
3c2c0 2a 20 63 6f 6d 70 69 6c 65 64 2e 20 46 75 6e 63  * compiled. Func
3c2d0 74 69 6f 6e 20 73 71 6c 69 74 65 33 54 61 62 6c  tion sqlite3Tabl
3c2e0 65 4c 6f 63 6b 28 29 20 69 73 20 75 73 65 64 20  eLock() is used 
3c2f0 74 6f 20 61 64 64 20 65 6e 74 72 69 65 73 20 74  to add entries t
3c300 6f 20 74 68 65 0a 2a 2a 20 6c 69 73 74 2e 0a 2a  o the.** list..*
3c310 2f 0a 73 74 72 75 63 74 20 50 61 72 73 65 20 7b  /.struct Parse {
3c320 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20  .  sqlite3 *db; 
3c330 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d          /* The m
3c340 61 69 6e 20 64 61 74 61 62 61 73 65 20 73 74 72  ain database str
3c350 75 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e 74 20  ucture */.  int 
3c360 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
3c370 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
3c380 66 72 6f 6d 20 65 78 65 63 75 74 69 6f 6e 20 2a  from execution *
3c390 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73  /.  char *zErrMs
3c3a0 67 3b 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 65  g;       /* An e
3c3b0 72 72 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f 0a  rror message */.
3c3c0 20 20 56 64 62 65 20 2a 70 56 64 62 65 3b 20 20    Vdbe *pVdbe;  
3c3d0 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 6e 67         /* An eng
3c3e0 69 6e 65 20 66 6f 72 20 65 78 65 63 75 74 69 6e  ine for executin
3c3f0 67 20 64 61 74 61 62 61 73 65 20 62 79 74 65 63  g database bytec
3c400 6f 64 65 20 2a 2f 0a 20 20 75 38 20 63 6f 6c 4e  ode */.  u8 colN
3c410 61 6d 65 73 53 65 74 3b 20 20 20 20 20 20 2f 2a  amesSet;      /*
3c420 20 54 52 55 45 20 61 66 74 65 72 20 4f 50 5f 43   TRUE after OP_C
3c430 6f 6c 75 6d 6e 4e 61 6d 65 20 68 61 73 20 62 65  olumnName has be
3c440 65 6e 20 69 73 73 75 65 64 20 74 6f 20 70 56 64  en issued to pVd
3c450 62 65 20 2a 2f 0a 20 20 75 38 20 6e 61 6d 65 43  be */.  u8 nameC
3c460 6c 61 73 68 3b 20 20 20 20 20 20 20 20 2f 2a 20  lash;        /* 
3c470 41 20 70 65 72 6d 61 6e 65 6e 74 20 74 61 62 6c  A permanent tabl
3c480 65 20 6e 61 6d 65 20 63 6c 61 73 68 65 73 20 77  e name clashes w
3c490 69 74 68 20 74 65 6d 70 20 74 61 62 6c 65 20 6e  ith temp table n
3c4a0 61 6d 65 20 2a 2f 0a 20 20 75 38 20 63 68 65 63  ame */.  u8 chec
3c4b0 6b 53 63 68 65 6d 61 3b 20 20 20 20 20 20 2f 2a  kSchema;      /*
3c4c0 20 43 61 75 73 65 73 20 73 63 68 65 6d 61 20 63   Causes schema c
3c4d0 6f 6f 6b 69 65 20 63 68 65 63 6b 20 61 66 74 65  ookie check afte
3c4e0 72 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20  r an error */.  
3c4f0 75 38 20 6e 65 73 74 65 64 3b 20 20 20 20 20 20  u8 nested;      
3c500 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
3c510 66 20 6e 65 73 74 65 64 20 63 61 6c 6c 73 20 74  f nested calls t
3c520 6f 20 74 68 65 20 70 61 72 73 65 72 2f 63 6f 64  o the parser/cod
3c530 65 20 67 65 6e 65 72 61 74 6f 72 20 2a 2f 0a 20  e generator */. 
3c540 20 75 38 20 70 61 72 73 65 45 72 72 6f 72 3b 20   u8 parseError; 
3c550 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 61 66        /* True af
3c560 74 65 72 20 61 20 70 61 72 73 69 6e 67 20 65 72  ter a parsing er
3c570 72 6f 72 2e 20 20 54 69 63 6b 65 74 20 23 31 37  ror.  Ticket #17
3c580 39 34 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72  94 */.  int nErr
3c590 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
3c5a0 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73  Number of errors
3c5b0 20 73 65 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e   seen */.  int n
3c5c0 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  Tab;            
3c5d0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 72 65  /* Number of pre
3c5e0 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65  viously allocate
3c5f0 64 20 56 44 42 45 20 63 75 72 73 6f 72 73 20 2a  d VDBE cursors *
3c600 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20  /.  int nMem;   
3c610 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
3c620 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c  er of memory cel
3c630 6c 73 20 75 73 65 64 20 73 6f 20 66 61 72 20 2a  ls used so far *
3c640 2f 0a 20 20 69 6e 74 20 6e 53 65 74 3b 20 20 20  /.  int nSet;   
3c650 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
3c660 65 72 20 6f 66 20 73 65 74 73 20 75 73 65 64 20  er of sets used 
3c670 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20  so far */.  int 
3c680 63 6b 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20  ckOffset;       
3c690 20 2f 2a 20 53 74 61 63 6b 20 6f 66 66 73 65 74   /* Stack offset
3c6a0 20 74 6f 20 64 61 74 61 20 75 73 65 64 20 62 79   to data used by
3c6b0 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e   CHECK constrain
3c6c0 74 73 20 2a 2f 0a 20 20 75 33 32 20 77 72 69 74  ts */.  u32 writ
3c6d0 65 4d 61 73 6b 3b 20 20 20 20 20 20 20 2f 2a 20  eMask;       /* 
3c6e0 53 74 61 72 74 20 61 20 77 72 69 74 65 20 74 72  Start a write tr
3c6f0 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65  ansaction on the
3c700 73 65 20 64 61 74 61 62 61 73 65 73 20 2a 2f 0a  se databases */.
3c710 20 20 75 33 32 20 63 6f 6f 6b 69 65 4d 61 73 6b    u32 cookieMask
3c720 3b 20 20 20 20 20 20 2f 2a 20 42 69 74 6d 61 73  ;      /* Bitmas
3c730 6b 20 6f 66 20 73 63 68 65 6d 61 20 76 65 72 69  k of schema veri
3c740 66 69 65 64 20 64 61 74 61 62 61 73 65 73 20 2a  fied databases *
3c750 2f 0a 20 20 69 6e 74 20 63 6f 6f 6b 69 65 47 6f  /.  int cookieGo
3c760 74 6f 3b 20 20 20 20 20 20 2f 2a 20 41 64 64 72  to;      /* Addr
3c770 65 73 73 20 6f 66 20 4f 50 5f 47 6f 74 6f 20 74  ess of OP_Goto t
3c780 6f 20 63 6f 6f 6b 69 65 20 76 65 72 69 66 69 65  o cookie verifie
3c790 72 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a  r subroutine */.
3c7a0 20 20 69 6e 74 20 63 6f 6f 6b 69 65 56 61 6c 75    int cookieValu
3c7b0 65 5b 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54  e[SQLITE_MAX_ATT
3c7c0 41 43 48 45 44 2b 32 5d 3b 20 20 2f 2a 20 56 61  ACHED+2];  /* Va
3c7d0 6c 75 65 73 20 6f 66 20 63 6f 6f 6b 69 65 73 20  lues of cookies 
3c7e0 74 6f 20 76 65 72 69 66 79 20 2a 2f 0a 23 69 66  to verify */.#if
3c7f0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
3c800 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20  _SHARED_CACHE.  
3c810 69 6e 74 20 6e 54 61 62 6c 65 4c 6f 63 6b 3b 20  int nTableLock; 
3c820 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
3c830 20 6f 66 20 6c 6f 63 6b 73 20 69 6e 20 61 54 61   of locks in aTa
3c840 62 6c 65 4c 6f 63 6b 20 2a 2f 0a 20 20 54 61 62  bleLock */.  Tab
3c850 6c 65 4c 6f 63 6b 20 2a 61 54 61 62 6c 65 4c 6f  leLock *aTableLo
3c860 63 6b 3b 20 2f 2a 20 52 65 71 75 69 72 65 64 20  ck; /* Required 
3c870 74 61 62 6c 65 20 6c 6f 63 6b 73 20 66 6f 72 20  table locks for 
3c880 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f 64  shared-cache mod
3c890 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  e */.#endif..  /
3c8a0 2a 20 41 62 6f 76 65 20 69 73 20 63 6f 6e 73 74  * Above is const
3c8b0 61 6e 74 20 62 65 74 77 65 65 6e 20 72 65 63 75  ant between recu
3c8c0 72 73 69 6f 6e 73 2e 20 20 42 65 6c 6f 77 20 69  rsions.  Below i
3c8d0 73 20 72 65 73 65 74 20 62 65 66 6f 72 65 20 61  s reset before a
3c8e0 6e 64 20 61 66 74 65 72 0a 20 20 2a 2a 20 65 61  nd after.  ** ea
3c8f0 63 68 20 72 65 63 75 72 73 69 6f 6e 20 2a 2f 0a  ch recursion */.
3c900 0a 20 20 69 6e 74 20 6e 56 61 72 3b 20 20 20 20  .  int nVar;    
3c910 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
3c920 72 20 6f 66 20 27 3f 27 20 76 61 72 69 61 62 6c  r of '?' variabl
3c930 65 73 20 73 65 65 6e 20 69 6e 20 74 68 65 20 53  es seen in the S
3c940 51 4c 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69  QL so far */.  i
3c950 6e 74 20 6e 56 61 72 45 78 70 72 3b 20 20 20 20  nt nVarExpr;    
3c960 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
3c970 20 75 73 65 64 20 73 6c 6f 74 73 20 69 6e 20 61   used slots in a
3c980 70 56 61 72 45 78 70 72 5b 5d 20 2a 2f 0a 20 20  pVarExpr[] */.  
3c990 69 6e 74 20 6e 56 61 72 45 78 70 72 41 6c 6c 6f  int nVarExprAllo
3c9a0 63 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f  c;   /* Number o
3c9b0 66 20 61 6c 6c 6f 63 61 74 65 64 20 73 6c 6f 74  f allocated slot
3c9c0 73 20 69 6e 20 61 70 56 61 72 45 78 70 72 5b 5d  s in apVarExpr[]
3c9d0 20 2a 2f 0a 20 20 45 78 70 72 20 2a 2a 61 70 56   */.  Expr **apV
3c9e0 61 72 45 78 70 72 3b 20 20 20 20 2f 2a 20 50 6f  arExpr;    /* Po
3c9f0 69 6e 74 65 72 73 20 74 6f 20 3a 61 61 61 20 61  inters to :aaa a
3ca00 6e 64 20 24 61 61 61 61 20 77 69 6c 64 63 61 72  nd $aaaa wildcar
3ca10 64 20 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f  d expressions */
3ca20 0a 20 20 75 38 20 65 78 70 6c 61 69 6e 3b 20 20  .  u8 explain;  
3ca30 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
3ca40 69 66 20 74 68 65 20 45 58 50 4c 41 49 4e 20 66  if the EXPLAIN f
3ca50 6c 61 67 20 69 73 20 66 6f 75 6e 64 20 6f 6e 20  lag is found on 
3ca60 74 68 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 54  the query */.  T
3ca70 6f 6b 65 6e 20 73 45 72 72 54 6f 6b 65 6e 3b 20  oken sErrToken; 
3ca80 20 20 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e      /* The token
3ca90 20 61 74 20 77 68 69 63 68 20 74 68 65 20 65 72   at which the er
3caa0 72 6f 72 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a  ror occurred */.
3cab0 20 20 54 6f 6b 65 6e 20 73 4e 61 6d 65 54 6f 6b    Token sNameTok
3cac0 65 6e 3b 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20  en;    /* Token 
3cad0 77 69 74 68 20 75 6e 71 75 61 6c 69 66 69 65 64  with unqualified
3cae0 20 73 63 68 65 6d 61 20 6f 62 6a 65 63 74 20 6e   schema object n
3caf0 61 6d 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 73  ame */.  Token s
3cb00 4c 61 73 74 54 6f 6b 65 6e 3b 20 20 20 20 2f 2a  LastToken;    /*
3cb10 20 54 68 65 20 6c 61 73 74 20 74 6f 6b 65 6e 20   The last token 
3cb20 70 61 72 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73  parsed */.  cons
3cb30 74 20 63 68 61 72 20 2a 7a 53 71 6c 3b 20 20 20  t char *zSql;   
3cb40 20 2f 2a 20 41 6c 6c 20 53 51 4c 20 74 65 78 74   /* All SQL text
3cb50 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
3cb60 20 2a 7a 54 61 69 6c 3b 20 20 20 2f 2a 20 41 6c   *zTail;   /* Al
3cb70 6c 20 53 51 4c 20 74 65 78 74 20 70 61 73 74 20  l SQL text past 
3cb80 74 68 65 20 6c 61 73 74 20 73 65 6d 69 63 6f 6c  the last semicol
3cb90 6f 6e 20 70 61 72 73 65 64 20 2a 2f 0a 20 20 54  on parsed */.  T
3cba0 61 62 6c 65 20 2a 70 4e 65 77 54 61 62 6c 65 3b  able *pNewTable;
3cbb0 20 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 62      /* A table b
3cbc0 65 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 65 64  eing constructed
3cbd0 20 62 79 20 43 52 45 41 54 45 20 54 41 42 4c 45   by CREATE TABLE
3cbe0 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70   */.  Trigger *p
3cbf0 4e 65 77 54 72 69 67 67 65 72 3b 20 20 20 20 20  NewTrigger;     
3cc00 2f 2a 20 54 72 69 67 67 65 72 20 75 6e 64 65 72  /* Trigger under
3cc10 20 63 6f 6e 73 74 72 75 63 74 20 62 79 20 61 20   construct by a 
3cc20 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20 2a  CREATE TRIGGER *
3cc30 2f 0a 20 20 54 72 69 67 67 65 72 53 74 61 63 6b  /.  TriggerStack
3cc40 20 2a 74 72 69 67 53 74 61 63 6b 3b 20 20 2f 2a   *trigStack;  /*
3cc50 20 54 72 69 67 67 65 72 20 61 63 74 69 6f 6e 73   Trigger actions
3cc60 20 62 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a   being coded */.
3cc70 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41    const char *zA
3cc80 75 74 68 43 6f 6e 74 65 78 74 3b 20 2f 2a 20 54  uthContext; /* T
3cc90 68 65 20 36 74 68 20 70 61 72 61 6d 65 74 65 72  he 6th parameter
3cca0 20 74 6f 20 64 62 2d 3e 78 41 75 74 68 20 63 61   to db->xAuth ca
3ccb0 6c 6c 62 61 63 6b 73 20 2a 2f 0a 23 69 66 6e 64  llbacks */.#ifnd
3ccc0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
3ccd0 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 54 6f  IRTUALTABLE.  To
3cce0 6b 65 6e 20 73 41 72 67 3b 20 20 20 20 20 20 20  ken sArg;       
3ccf0 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 70           /* Comp
3cd00 6c 65 74 65 20 74 65 78 74 20 6f 66 20 61 20 6d  lete text of a m
3cd10 6f 64 75 6c 65 20 61 72 67 75 6d 65 6e 74 20 2a  odule argument *
3cd20 2f 0a 20 20 75 38 20 64 65 63 6c 61 72 65 56 74  /.  u8 declareVt
3cd30 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ab;            /
3cd40 2a 20 54 72 75 65 20 69 66 20 69 6e 73 69 64 65  * True if inside
3cd50 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65   sqlite3_declare
3cd60 5f 76 74 61 62 28 29 20 2a 2f 0a 20 20 54 61 62  _vtab() */.  Tab
3cd70 6c 65 20 2a 70 56 69 72 74 75 61 6c 4c 6f 63 6b  le *pVirtualLock
3cd80 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 71 75 69  ;       /* Requi
3cd90 72 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  re virtual table
3cda0 20 6c 6f 63 6b 20 6f 6e 20 74 68 69 73 20 74 61   lock on this ta
3cdb0 62 6c 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69  ble */.#endif.#i
3cdc0 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50  f SQLITE_MAX_EXP
3cdd0 52 5f 44 45 50 54 48 3e 30 0a 20 20 69 6e 74 20  R_DEPTH>0.  int 
3cde0 6e 48 65 69 67 68 74 3b 20 20 20 20 20 20 20 20  nHeight;        
3cdf0 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f      /* Expressio
3ce00 6e 20 74 72 65 65 20 68 65 69 67 68 74 20 6f 66  n tree height of
3ce10 20 63 75 72 72 65 6e 74 20 73 75 62 2d 73 65 6c   current sub-sel
3ce20 65 63 74 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b  ect */.#endif.};
3ce30 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
3ce40 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
3ce50 45 0a 20 20 23 64 65 66 69 6e 65 20 49 4e 5f 44  E.  #define IN_D
3ce60 45 43 4c 41 52 45 5f 56 54 41 42 20 30 0a 23 65  ECLARE_VTAB 0.#e
3ce70 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 49 4e  lse.  #define IN
3ce80 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 28 70  _DECLARE_VTAB (p
3ce90 50 61 72 73 65 2d 3e 64 65 63 6c 61 72 65 56 74  Parse->declareVt
3cea0 61 62 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ab).#endif../*.*
3ceb0 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
3cec0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
3ced0 74 72 75 63 74 75 72 65 20 63 61 6e 20 62 65 20  tructure can be 
3cee0 64 65 63 6c 61 72 65 64 20 6f 6e 20 61 20 73 74  declared on a st
3cef0 61 63 6b 20 61 6e 64 20 75 73 65 64 0a 2a 2a 20  ack and used.** 
3cf00 74 6f 20 73 61 76 65 20 74 68 65 20 50 61 72 73  to save the Pars
3cf10 65 2e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 76  e.zAuthContext v
3cf20 61 6c 75 65 20 73 6f 20 74 68 61 74 20 69 74 20  alue so that it 
3cf30 63 61 6e 20 62 65 20 72 65 73 74 6f 72 65 64 20  can be restored 
3cf40 6c 61 74 65 72 2e 0a 2a 2f 0a 73 74 72 75 63 74  later..*/.struct
3cf50 20 41 75 74 68 43 6f 6e 74 65 78 74 20 7b 0a 20   AuthContext {. 
3cf60 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 75   const char *zAu
3cf70 74 68 43 6f 6e 74 65 78 74 3b 20 20 20 2f 2a 20  thContext;   /* 
3cf80 50 75 74 20 73 61 76 65 64 20 50 61 72 73 65 2e  Put saved Parse.
3cf90 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 68 65 72  zAuthContext her
3cfa0 65 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50  e */.  Parse *pP
3cfb0 61 72 73 65 3b 20 20 20 20 20 20 20 20 20 20 20  arse;           
3cfc0 20 20 20 2f 2a 20 54 68 65 20 50 61 72 73 65 20     /* The Parse 
3cfd0 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 7d 3b 0a  structure */.};.
3cfe0 0a 2f 2a 0a 2a 2a 20 42 69 74 66 69 65 6c 64 20  ./*.** Bitfield 
3cff0 66 6c 61 67 73 20 66 6f 72 20 50 32 20 76 61 6c  flags for P2 val
3d000 75 65 20 69 6e 20 4f 50 5f 49 6e 73 65 72 74 20  ue in OP_Insert 
3d010 61 6e 64 20 4f 50 5f 44 65 6c 65 74 65 0a 2a 2f  and OP_Delete.*/
3d020 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f  .#define OPFLAG_
3d030 4e 43 48 41 4e 47 45 20 20 20 31 20 20 20 20 2f  NCHANGE   1    /
3d040 2a 20 53 65 74 20 74 6f 20 75 70 64 61 74 65 20  * Set to update 
3d050 64 62 2d 3e 6e 43 68 61 6e 67 65 20 2a 2f 0a 23  db->nChange */.#
3d060 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f 4c 41  define OPFLAG_LA
3d070 53 54 52 4f 57 49 44 20 32 20 20 20 20 2f 2a 20  STROWID 2    /* 
3d080 53 65 74 20 74 6f 20 75 70 64 61 74 65 20 64 62  Set to update db
3d090 2d 3e 6c 61 73 74 52 6f 77 69 64 20 2a 2f 0a 23  ->lastRowid */.#
3d0a0 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f 49 53  define OPFLAG_IS
3d0b0 55 50 44 41 54 45 20 20 34 20 20 20 20 2f 2a 20  UPDATE  4    /* 
3d0c0 54 68 69 73 20 4f 50 5f 49 6e 73 65 72 74 20 69  This OP_Insert i
3d0d0 73 20 61 6e 20 73 71 6c 20 55 50 44 41 54 45 20  s an sql UPDATE 
3d0e0 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41  */.#define OPFLA
3d0f0 47 5f 41 50 50 45 4e 44 20 20 20 20 38 20 20 20  G_APPEND    8   
3d100 20 2f 2a 20 54 68 69 73 20 69 73 20 6c 69 6b 65   /* This is like
3d110 6c 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70 65  ly to be an appe
3d120 6e 64 20 2a 2f 0a 0a 2f 2a 0a 20 2a 20 45 61 63  nd */../*. * Eac
3d130 68 20 74 72 69 67 67 65 72 20 70 72 65 73 65 6e  h trigger presen
3d140 74 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  t in the databas
3d150 65 20 73 63 68 65 6d 61 20 69 73 20 73 74 6f 72  e schema is stor
3d160 65 64 20 61 73 20 61 6e 20 69 6e 73 74 61 6e 63  ed as an instanc
3d170 65 20 6f 66 0a 20 2a 20 73 74 72 75 63 74 20 54  e of. * struct T
3d180 72 69 67 67 65 72 2e 20 0a 20 2a 0a 20 2a 20 50  rigger. . *. * P
3d190 6f 69 6e 74 65 72 73 20 74 6f 20 69 6e 73 74 61  ointers to insta
3d1a0 6e 63 65 73 20 6f 66 20 73 74 72 75 63 74 20 54  nces of struct T
3d1b0 72 69 67 67 65 72 20 61 72 65 20 73 74 6f 72 65  rigger are store
3d1c0 64 20 69 6e 20 74 77 6f 20 77 61 79 73 2e 0a 20  d in two ways.. 
3d1d0 2a 20 31 2e 20 49 6e 20 74 68 65 20 22 74 72 69  * 1. In the "tri
3d1e0 67 48 61 73 68 22 20 68 61 73 68 20 74 61 62 6c  gHash" hash tabl
3d1f0 65 20 28 70 61 72 74 20 6f 66 20 74 68 65 20 73  e (part of the s
3d200 71 6c 69 74 65 33 2a 20 74 68 61 74 20 72 65 70  qlite3* that rep
3d210 72 65 73 65 6e 74 73 20 74 68 65 20 0a 20 2a 20  resents the . * 
3d220 20 20 20 64 61 74 61 62 61 73 65 29 2e 20 54 68     database). Th
3d230 69 73 20 61 6c 6c 6f 77 73 20 54 72 69 67 67 65  is allows Trigge
3d240 72 20 73 74 72 75 63 74 75 72 65 73 20 74 6f 20  r structures to 
3d250 62 65 20 72 65 74 72 69 65 76 65 64 20 62 79 20  be retrieved by 
3d260 6e 61 6d 65 2e 0a 20 2a 20 32 2e 20 41 6c 6c 20  name.. * 2. All 
3d270 74 72 69 67 67 65 72 73 20 61 73 73 6f 63 69 61  triggers associa
3d280 74 65 64 20 77 69 74 68 20 61 20 73 69 6e 67 6c  ted with a singl
3d290 65 20 74 61 62 6c 65 20 66 6f 72 6d 20 61 20 6c  e table form a l
3d2a0 69 6e 6b 65 64 20 6c 69 73 74 2c 20 75 73 69 6e  inked list, usin
3d2b0 67 20 74 68 65 0a 20 2a 20 20 20 20 70 4e 65 78  g the. *    pNex
3d2c0 74 20 6d 65 6d 62 65 72 20 6f 66 20 73 74 72 75  t member of stru
3d2d0 63 74 20 54 72 69 67 67 65 72 2e 20 41 20 70 6f  ct Trigger. A po
3d2e0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66 69 72  inter to the fir
3d2f0 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68  st element of th
3d300 65 0a 20 2a 20 20 20 20 6c 69 6e 6b 65 64 20 6c  e. *    linked l
3d310 69 73 74 20 69 73 20 73 74 6f 72 65 64 20 61 73  ist is stored as
3d320 20 74 68 65 20 22 70 54 72 69 67 67 65 72 22 20   the "pTrigger" 
3d330 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20 61 73  member of the as
3d340 73 6f 63 69 61 74 65 64 0a 20 2a 20 20 20 20 73  sociated. *    s
3d350 74 72 75 63 74 20 54 61 62 6c 65 2e 0a 20 2a 0a  truct Table.. *.
3d360 20 2a 20 54 68 65 20 22 73 74 65 70 5f 6c 69 73   * The "step_lis
3d370 74 22 20 6d 65 6d 62 65 72 20 70 6f 69 6e 74 73  t" member points
3d380 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 65 6c   to the first el
3d390 65 6d 65 6e 74 20 6f 66 20 61 20 6c 69 6e 6b 65  ement of a linke
3d3a0 64 20 6c 69 73 74 0a 20 2a 20 63 6f 6e 74 61 69  d list. * contai
3d3b0 6e 69 6e 67 20 74 68 65 20 53 51 4c 20 73 74 61  ning the SQL sta
3d3c0 74 65 6d 65 6e 74 73 20 73 70 65 63 69 66 69 65  tements specifie
3d3d0 64 20 61 73 20 74 68 65 20 74 72 69 67 67 65 72  d as the trigger
3d3e0 20 70 72 6f 67 72 61 6d 2e 0a 20 2a 2f 0a 73 74   program.. */.st
3d3f0 72 75 63 74 20 54 72 69 67 67 65 72 20 7b 0a 20  ruct Trigger {. 
3d400 20 63 68 61 72 20 2a 6e 61 6d 65 3b 20 20 20 20   char *name;    
3d410 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
3d420 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 72 69 67  name of the trig
3d430 67 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20  ger             
3d440 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20             */.  
3d450 63 68 61 72 20 2a 74 61 62 6c 65 3b 20 20 20 20  char *table;    
3d460 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
3d470 61 62 6c 65 20 6f 72 20 76 69 65 77 20 74 6f 20  able or view to 
3d480 77 68 69 63 68 20 74 68 65 20 74 72 69 67 67 65  which the trigge
3d490 72 20 61 70 70 6c 69 65 73 20 2a 2f 0a 20 20 75  r applies */.  u
3d4a0 38 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20  8 op;           
3d4b0 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66         /* One of
3d4c0 20 54 4b 5f 44 45 4c 45 54 45 2c 20 54 4b 5f 55   TK_DELETE, TK_U
3d4d0 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54  PDATE, TK_INSERT
3d4e0 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 75 38           */.  u8
3d4f0 20 74 72 5f 74 6d 3b 20 20 20 20 20 20 20 20 20   tr_tm;         
3d500 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20        /* One of 
3d510 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 2c 20  TRIGGER_BEFORE, 
3d520 54 52 49 47 47 45 52 5f 41 46 54 45 52 20 2a 2f  TRIGGER_AFTER */
3d530 0a 20 20 45 78 70 72 20 2a 70 57 68 65 6e 3b 20  .  Expr *pWhen; 
3d540 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
3d550 65 20 57 48 45 4e 20 63 6c 61 75 73 65 20 6f 66  e WHEN clause of
3d560 20 74 68 65 20 65 78 70 72 65 73 69 6f 6e 20 28   the expresion (
3d570 6d 61 79 20 62 65 20 4e 55 4c 4c 29 20 2a 2f 0a  may be NULL) */.
3d580 20 20 49 64 4c 69 73 74 20 2a 70 43 6f 6c 75 6d    IdList *pColum
3d590 6e 73 3b 20 20 20 20 20 20 20 2f 2a 20 49 66 20  ns;       /* If 
3d5a0 74 68 69 73 20 69 73 20 61 6e 20 55 50 44 41 54  this is an UPDAT
3d5b0 45 20 4f 46 20 3c 63 6f 6c 75 6d 6e 2d 6c 69 73  E OF <column-lis
3d5c0 74 3e 20 74 72 69 67 67 65 72 2c 0a 20 20 20 20  t> trigger,.    
3d5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3d5e0 20 20 20 20 20 20 20 20 20 74 68 65 20 3c 63 6f           the <co
3d5f0 6c 75 6d 6e 2d 6c 69 73 74 3e 20 69 73 20 73 74  lumn-list> is st
3d600 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20 20 54  ored here */.  T
3d610 6f 6b 65 6e 20 6e 61 6d 65 54 6f 6b 65 6e 3b 20  oken nameToken; 
3d620 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20         /* Token 
3d630 63 6f 6e 74 61 69 6e 69 6e 67 20 7a 4e 61 6d 65  containing zName
3d640 2e 20 55 73 65 20 64 75 72 69 6e 67 20 70 61 72  . Use during par
3d650 73 69 6e 67 20 6f 6e 6c 79 20 2a 2f 0a 20 20 53  sing only */.  S
3d660 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20  chema *pSchema; 
3d670 20 20 20 20 20 20 20 2f 2a 20 53 63 68 65 6d 61         /* Schema
3d680 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
3d690 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 53 63 68  trigger */.  Sch
3d6a0 65 6d 61 20 2a 70 54 61 62 53 63 68 65 6d 61 3b  ema *pTabSchema;
3d6b0 20 20 20 20 20 2f 2a 20 53 63 68 65 6d 61 20 63       /* Schema c
3d6c0 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61  ontaining the ta
3d6d0 62 6c 65 20 2a 2f 0a 20 20 54 72 69 67 67 65 72  ble */.  Trigger
3d6e0 53 74 65 70 20 2a 73 74 65 70 5f 6c 69 73 74 3b  Step *step_list;
3d6f0 20 2f 2a 20 4c 69 6e 6b 20 6c 69 73 74 20 6f 66   /* Link list of
3d700 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d   trigger program
3d710 20 73 74 65 70 73 20 20 20 20 20 20 20 20 20 20   steps          
3d720 20 20 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20     */.  Trigger 
3d730 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20  *pNext;         
3d740 2f 2a 20 4e 65 78 74 20 74 72 69 67 67 65 72 20  /* Next trigger 
3d750 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
3d760 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 7d 3b 0a  the table */.};.
3d770 0a 2f 2a 0a 2a 2a 20 41 20 74 72 69 67 67 65 72  ./*.** A trigger
3d780 20 69 73 20 65 69 74 68 65 72 20 61 20 42 45 46   is either a BEF
3d790 4f 52 45 20 6f 72 20 61 6e 20 41 46 54 45 52 20  ORE or an AFTER 
3d7a0 74 72 69 67 67 65 72 2e 20 20 54 68 65 20 66 6f  trigger.  The fo
3d7b0 6c 6c 6f 77 69 6e 67 20 63 6f 6e 73 74 61 6e 74  llowing constant
3d7c0 73 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65 20 77  s.** determine w
3d7d0 68 69 63 68 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20  hich. .**.** If 
3d7e0 74 68 65 72 65 20 61 72 65 20 6d 75 6c 74 69 70  there are multip
3d7f0 6c 65 20 74 72 69 67 67 65 72 73 2c 20 79 6f 75  le triggers, you
3d800 20 6d 69 67 68 74 20 6f 66 20 73 6f 6d 65 20 42   might of some B
3d810 45 46 4f 52 45 20 61 6e 64 20 73 6f 6d 65 20 41  EFORE and some A
3d820 46 54 45 52 2e 0a 2a 2a 20 49 6e 20 74 68 61 74  FTER..** In that
3d830 20 63 61 73 65 73 2c 20 74 68 65 20 63 6f 6e 73   cases, the cons
3d840 74 61 6e 74 73 20 62 65 6c 6f 77 20 63 61 6e 20  tants below can 
3d850 62 65 20 4f 52 65 64 20 74 6f 67 65 74 68 65 72  be ORed together
3d860 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 52 49  ..*/.#define TRI
3d870 47 47 45 52 5f 42 45 46 4f 52 45 20 20 31 0a 23  GGER_BEFORE  1.#
3d880 64 65 66 69 6e 65 20 54 52 49 47 47 45 52 5f 41  define TRIGGER_A
3d890 46 54 45 52 20 20 20 32 0a 0a 2f 2a 0a 20 2a 20  FTER   2../*. * 
3d8a0 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 73  An instance of s
3d8b0 74 72 75 63 74 20 54 72 69 67 67 65 72 53 74 65  truct TriggerSte
3d8c0 70 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f  p is used to sto
3d8d0 72 65 20 61 20 73 69 6e 67 6c 65 20 53 51 4c 20  re a single SQL 
3d8e0 73 74 61 74 65 6d 65 6e 74 0a 20 2a 20 74 68 61  statement. * tha
3d8f0 74 20 69 73 20 61 20 70 61 72 74 20 6f 66 20 61  t is a part of a
3d900 20 74 72 69 67 67 65 72 2d 70 72 6f 67 72 61 6d   trigger-program
3d910 2e 20 0a 20 2a 0a 20 2a 20 49 6e 73 74 61 6e 63  . . *. * Instanc
3d920 65 73 20 6f 66 20 73 74 72 75 63 74 20 54 72 69  es of struct Tri
3d930 67 67 65 72 53 74 65 70 20 61 72 65 20 73 74 6f  ggerStep are sto
3d940 72 65 64 20 69 6e 20 61 20 73 69 6e 67 6c 79 20  red in a singly 
3d950 6c 69 6e 6b 65 64 20 6c 69 73 74 20 28 6c 69 6e  linked list (lin
3d960 6b 65 64 0a 20 2a 20 75 73 69 6e 67 20 74 68 65  ked. * using the
3d970 20 22 70 4e 65 78 74 22 20 6d 65 6d 62 65 72 29   "pNext" member)
3d980 20 72 65 66 65 72 65 6e 63 65 64 20 62 79 20 74   referenced by t
3d990 68 65 20 22 73 74 65 70 5f 6c 69 73 74 22 20 6d  he "step_list" m
3d9a0 65 6d 62 65 72 20 6f 66 20 74 68 65 20 0a 20 2a  ember of the . *
3d9b0 20 61 73 73 6f 63 69 61 74 65 64 20 73 74 72 75   associated stru
3d9c0 63 74 20 54 72 69 67 67 65 72 20 69 6e 73 74 61  ct Trigger insta
3d9d0 6e 63 65 2e 20 54 68 65 20 66 69 72 73 74 20 65  nce. The first e
3d9e0 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 6c 69  lement of the li
3d9f0 6e 6b 65 64 20 6c 69 73 74 20 69 73 0a 20 2a 20  nked list is. * 
3da00 74 68 65 20 66 69 72 73 74 20 73 74 65 70 20 6f  the first step o
3da10 66 20 74 68 65 20 74 72 69 67 67 65 72 2d 70 72  f the trigger-pr
3da20 6f 67 72 61 6d 2e 0a 20 2a 20 0a 20 2a 20 54 68  ogram.. * . * Th
3da30 65 20 22 6f 70 22 20 6d 65 6d 62 65 72 20 69 6e  e "op" member in
3da40 64 69 63 61 74 65 73 20 77 68 65 74 68 65 72 20  dicates whether 
3da50 74 68 69 73 20 69 73 20 61 20 22 44 45 4c 45 54  this is a "DELET
3da60 45 22 2c 20 22 49 4e 53 45 52 54 22 2c 20 22 55  E", "INSERT", "U
3da70 50 44 41 54 45 22 20 6f 72 0a 20 2a 20 22 53 45  PDATE" or. * "SE
3da80 4c 45 43 54 22 20 73 74 61 74 65 6d 65 6e 74 2e  LECT" statement.
3da90 20 54 68 65 20 6d 65 61 6e 69 6e 67 73 20 6f 66   The meanings of
3daa0 20 74 68 65 20 6f 74 68 65 72 20 6d 65 6d 62 65   the other membe
3dab0 72 73 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64  rs is determined
3dac0 20 62 79 20 74 68 65 20 0a 20 2a 20 76 61 6c 75   by the . * valu
3dad0 65 20 6f 66 20 22 6f 70 22 20 61 73 20 66 6f 6c  e of "op" as fol
3dae0 6c 6f 77 73 3a 0a 20 2a 0a 20 2a 20 28 6f 70 20  lows:. *. * (op 
3daf0 3d 3d 20 54 4b 5f 49 4e 53 45 52 54 29 0a 20 2a  == TK_INSERT). *
3db00 20 6f 72 63 6f 6e 66 20 20 20 20 2d 3e 20 73 74   orconf    -> st
3db10 6f 72 65 73 20 74 68 65 20 4f 4e 20 43 4f 4e 46  ores the ON CONF
3db20 4c 49 43 54 20 61 6c 67 6f 72 69 74 68 6d 0a 20  LICT algorithm. 
3db30 2a 20 70 53 65 6c 65 63 74 20 20 20 2d 3e 20 49  * pSelect   -> I
3db40 66 20 74 68 69 73 20 69 73 20 61 6e 20 49 4e 53  f this is an INS
3db50 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 53 45 4c  ERT INTO ... SEL
3db60 45 43 54 20 2e 2e 2e 20 73 74 61 74 65 6d 65 6e  ECT ... statemen
3db70 74 2c 20 74 68 65 6e 0a 20 2a 20 20 20 20 20 20  t, then. *      
3db80 20 20 20 20 20 20 20 20 74 68 69 73 20 73 74 6f          this sto
3db90 72 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  res a pointer to
3dba0 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   the SELECT stat
3dbb0 65 6d 65 6e 74 2e 20 4f 74 68 65 72 77 69 73 65  ement. Otherwise
3dbc0 20 4e 55 4c 4c 2e 0a 20 2a 20 74 61 72 67 65 74   NULL.. * target
3dbd0 20 20 20 20 2d 3e 20 41 20 74 6f 6b 65 6e 20 68      -> A token h
3dbe0 6f 6c 64 69 6e 67 20 74 68 65 20 6e 61 6d 65 20  olding the name 
3dbf0 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20  of the table to 
3dc00 69 6e 73 65 72 74 20 69 6e 74 6f 2e 0a 20 2a 20  insert into.. * 
3dc10 70 45 78 70 72 4c 69 73 74 20 2d 3e 20 49 66 20  pExprList -> If 
3dc20 74 68 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52  this is an INSER
3dc30 54 20 49 4e 54 4f 20 2e 2e 2e 20 56 41 4c 55 45  T INTO ... VALUE
3dc40 53 20 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74 2c  S ... statement,
3dc50 20 74 68 65 6e 0a 20 2a 20 20 20 20 20 20 20 20   then. *        
3dc60 20 20 20 20 20 20 74 68 69 73 20 73 74 6f 72 65        this store
3dc70 73 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 69  s values to be i
3dc80 6e 73 65 72 74 65 64 2e 20 4f 74 68 65 72 77 69  nserted. Otherwi
3dc90 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 70 49 64 4c  se NULL.. * pIdL
3dca0 69 73 74 20 20 20 2d 3e 20 49 66 20 74 68 69 73  ist   -> If this
3dcb0 20 69 73 20 61 6e 20 49 4e 53 45 52 54 20 49 4e   is an INSERT IN
3dcc0 54 4f 20 2e 2e 2e 20 28 3c 63 6f 6c 75 6d 6e 2d  TO ... (<column-
3dcd0 6e 61 6d 65 73 3e 29 20 56 41 4c 55 45 53 20 2e  names>) VALUES .
3dce0 2e 2e 20 0a 20 2a 20 20 20 20 20 20 20 20 20 20  .. . *          
3dcf0 20 20 20 20 73 74 61 74 65 6d 65 6e 74 2c 20 74      statement, t
3dd00 68 65 6e 20 74 68 69 73 20 73 74 6f 72 65 73 20  hen this stores 
3dd10 74 68 65 20 63 6f 6c 75 6d 6e 2d 6e 61 6d 65 73  the column-names
3dd20 20 74 6f 20 62 65 0a 20 2a 20 20 20 20 20 20 20   to be. *       
3dd30 20 20 20 20 20 20 20 69 6e 73 65 72 74 65 64 20         inserted 
3dd40 69 6e 74 6f 2e 0a 20 2a 0a 20 2a 20 28 6f 70 20  into.. *. * (op 
3dd50 3d 3d 20 54 4b 5f 44 45 4c 45 54 45 29 0a 20 2a  == TK_DELETE). *
3dd60 20 74 61 72 67 65 74 20 20 20 20 2d 3e 20 41 20   target    -> A 
3dd70 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68  token holding th
3dd80 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  e name of the ta
3dd90 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 66 72  ble to delete fr
3dda0 6f 6d 2e 0a 20 2a 20 70 57 68 65 72 65 20 20 20  om.. * pWhere   
3ddb0 20 2d 3e 20 54 68 65 20 57 48 45 52 45 20 63 6c   -> The WHERE cl
3ddc0 61 75 73 65 20 6f 66 20 74 68 65 20 44 45 4c 45  ause of the DELE
3ddd0 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69 66 20  TE statement if 
3dde0 6f 6e 65 20 69 73 20 73 70 65 63 69 66 69 65 64  one is specified
3ddf0 2e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20  .. *            
3de00 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c    Otherwise NULL
3de10 2e 0a 20 2a 20 0a 20 2a 20 28 6f 70 20 3d 3d 20  .. * . * (op == 
3de20 54 4b 5f 55 50 44 41 54 45 29 0a 20 2a 20 74 61  TK_UPDATE). * ta
3de30 72 67 65 74 20 20 20 20 2d 3e 20 41 20 74 6f 6b  rget    -> A tok
3de40 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 6e  en holding the n
3de50 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
3de60 20 74 6f 20 75 70 64 61 74 65 20 72 6f 77 73 20   to update rows 
3de70 6f 66 2e 0a 20 2a 20 70 57 68 65 72 65 20 20 20  of.. * pWhere   
3de80 20 2d 3e 20 54 68 65 20 57 48 45 52 45 20 63 6c   -> The WHERE cl
3de90 61 75 73 65 20 6f 66 20 74 68 65 20 55 50 44 41  ause of the UPDA
3dea0 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69 66 20  TE statement if 
3deb0 6f 6e 65 20 69 73 20 73 70 65 63 69 66 69 65 64  one is specified
3dec0 2e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20  .. *            
3ded0 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c    Otherwise NULL
3dee0 2e 0a 20 2a 20 70 45 78 70 72 4c 69 73 74 20 2d  .. * pExprList -
3def0 3e 20 41 20 6c 69 73 74 20 6f 66 20 74 68 65 20  > A list of the 
3df00 63 6f 6c 75 6d 6e 73 20 74 6f 20 75 70 64 61 74  columns to updat
3df10 65 20 61 6e 64 20 74 68 65 20 65 78 70 72 65 73  e and the expres
3df20 73 69 6f 6e 73 20 74 6f 20 75 70 64 61 74 65 0a  sions to update.
3df30 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   *              
3df40 74 68 65 6d 20 74 6f 2e 20 53 65 65 20 73 71 6c  them to. See sql
3df50 69 74 65 33 55 70 64 61 74 65 28 29 20 64 6f 63  ite3Update() doc
3df60 75 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 22 70  umentation of "p
3df70 43 68 61 6e 67 65 73 22 0a 20 2a 20 20 20 20 20  Changes". *     
3df80 20 20 20 20 20 20 20 20 20 61 72 67 75 6d 65 6e           argumen
3df90 74 2e 0a 20 2a 20 0a 20 2a 2f 0a 73 74 72 75 63  t.. * . */.struc
3dfa0 74 20 54 72 69 67 67 65 72 53 74 65 70 20 7b 0a  t TriggerStep {.
3dfb0 20 20 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20    int op;       
3dfc0 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66         /* One of
3dfd0 20 54 4b 5f 44 45 4c 45 54 45 2c 20 54 4b 5f 55   TK_DELETE, TK_U
3dfe0 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54  PDATE, TK_INSERT
3dff0 2c 20 54 4b 5f 53 45 4c 45 43 54 20 2a 2f 0a 20  , TK_SELECT */. 
3e000 20 69 6e 74 20 6f 72 63 6f 6e 66 3b 20 20 20 20   int orconf;    
3e010 20 20 20 20 20 20 2f 2a 20 4f 45 5f 52 6f 6c 6c        /* OE_Roll
3e020 62 61 63 6b 20 65 74 63 2e 20 2a 2f 0a 20 20 54  back etc. */.  T
3e030 72 69 67 67 65 72 20 2a 70 54 72 69 67 3b 20 20  rigger *pTrig;  
3e040 20 20 20 20 2f 2a 20 54 68 65 20 74 72 69 67 67      /* The trigg
3e050 65 72 20 74 68 61 74 20 74 68 69 73 20 73 74 65  er that this ste
3e060 70 20 69 73 20 61 20 70 61 72 74 20 6f 66 20 2a  p is a part of *
3e070 2f 0a 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65  /..  Select *pSe
3e080 6c 65 63 74 3b 20 20 20 20 20 2f 2a 20 56 61 6c  lect;     /* Val
3e090 69 64 20 66 6f 72 20 53 45 4c 45 43 54 20 61 6e  id for SELECT an
3e0a0 64 20 73 6f 6d 65 74 69 6d 65 73 20 0a 09 09 09  d sometimes ....
3e0b0 20 20 49 4e 53 45 52 54 20 73 74 65 70 73 20 28    INSERT steps (
3e0c0 77 68 65 6e 20 70 45 78 70 72 4c 69 73 74 20 3d  when pExprList =
3e0d0 3d 20 30 29 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  = 0) */.  Token 
3e0e0 74 61 72 67 65 74 3b 20 20 20 20 20 20 20 20 2f  target;        /
3e0f0 2a 20 56 61 6c 69 64 20 66 6f 72 20 44 45 4c 45  * Valid for DELE
3e100 54 45 2c 20 55 50 44 41 54 45 2c 20 49 4e 53 45  TE, UPDATE, INSE
3e110 52 54 20 73 74 65 70 73 20 2a 2f 0a 20 20 45 78  RT steps */.  Ex
3e120 70 72 20 2a 70 57 68 65 72 65 3b 20 20 20 20 20  pr *pWhere;     
3e130 20 20 20 2f 2a 20 56 61 6c 69 64 20 66 6f 72 20     /* Valid for 
3e140 44 45 4c 45 54 45 2c 20 55 50 44 41 54 45 20 73  DELETE, UPDATE s
3e150 74 65 70 73 20 2a 2f 0a 20 20 45 78 70 72 4c 69  teps */.  ExprLi
3e160 73 74 20 2a 70 45 78 70 72 4c 69 73 74 3b 20 2f  st *pExprList; /
3e170 2a 20 56 61 6c 69 64 20 66 6f 72 20 55 50 44 41  * Valid for UPDA
3e180 54 45 20 73 74 61 74 65 6d 65 6e 74 73 20 61 6e  TE statements an
3e190 64 20 73 6f 6d 65 74 69 6d 65 73 20 0a 09 09 09  d sometimes ....
3e1a0 20 20 20 49 4e 53 45 52 54 20 73 74 65 70 73 20     INSERT steps 
3e1b0 28 77 68 65 6e 20 70 53 65 6c 65 63 74 20 3d 3d  (when pSelect ==
3e1c0 20 30 29 20 20 20 20 20 20 20 20 20 2a 2f 0a 20   0)         */. 
3e1d0 20 49 64 4c 69 73 74 20 2a 70 49 64 4c 69 73 74   IdList *pIdList
3e1e0 3b 20 20 20 20 20 2f 2a 20 56 61 6c 69 64 20 66  ;     /* Valid f
3e1f0 6f 72 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d  or INSERT statem
3e200 65 6e 74 73 20 6f 6e 6c 79 20 2a 2f 0a 20 20 54  ents only */.  T
3e210 72 69 67 67 65 72 53 74 65 70 20 2a 70 4e 65 78  riggerStep *pNex
3e220 74 3b 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 74  t;  /* Next in t
3e230 68 65 20 6c 69 6e 6b 2d 6c 69 73 74 20 2a 2f 0a  he link-list */.
3e240 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 70    TriggerStep *p
3e250 4c 61 73 74 3b 20 20 2f 2a 20 4c 61 73 74 20 65  Last;  /* Last e
3e260 6c 65 6d 65 6e 74 20 69 6e 20 6c 69 6e 6b 2d 6c  lement in link-l
3e270 69 73 74 2e 20 56 61 6c 69 64 20 66 6f 72 20 31  ist. Valid for 1
3e280 73 74 20 65 6c 65 6d 20 6f 6e 6c 79 20 2a 2f 0a  st elem only */.
3e290 7d 3b 0a 0a 2f 2a 0a 20 2a 20 41 6e 20 69 6e 73  };../*. * An ins
3e2a0 74 61 6e 63 65 20 6f 66 20 73 74 72 75 63 74 20  tance of struct 
3e2b0 54 72 69 67 67 65 72 53 74 61 63 6b 20 73 74 6f  TriggerStack sto
3e2c0 72 65 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  res information 
3e2d0 72 65 71 75 69 72 65 64 20 64 75 72 69 6e 67 20  required during 
3e2e0 63 6f 64 65 0a 20 2a 20 67 65 6e 65 72 61 74 69  code. * generati
3e2f0 6f 6e 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 74  on of a single t
3e300 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e 20  rigger program. 
3e310 57 68 69 6c 65 20 74 68 65 20 74 72 69 67 67 65  While the trigge
3e320 72 20 70 72 6f 67 72 61 6d 20 69 73 20 62 65 69  r program is bei
3e330 6e 67 0a 20 2a 20 63 6f 64 65 64 2c 20 69 74 73  ng. * coded, its
3e340 20 61 73 73 6f 63 69 61 74 65 64 20 54 72 69 67   associated Trig
3e350 67 65 72 53 74 61 63 6b 20 69 6e 73 74 61 6e 63  gerStack instanc
3e360 65 20 69 73 20 70 6f 69 6e 74 65 64 20 74 6f 20  e is pointed to 
3e370 62 79 20 74 68 65 0a 20 2a 20 22 70 54 72 69 67  by the. * "pTrig
3e380 67 65 72 53 74 61 63 6b 22 20 6d 65 6d 62 65 72  gerStack" member
3e390 20 6f 66 20 74 68 65 20 50 61 72 73 65 20 73 74   of the Parse st
3e3a0 72 75 63 74 75 72 65 2e 0a 20 2a 0a 20 2a 20 54  ructure.. *. * T
3e3b0 68 65 20 70 54 61 62 20 6d 65 6d 62 65 72 20 70  he pTab member p
3e3c0 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 74 61 62  oints to the tab
3e3d0 6c 65 20 74 68 61 74 20 74 72 69 67 67 65 72 73  le that triggers
3e3e0 20 61 72 65 20 62 65 69 6e 67 20 63 6f 64 65 64   are being coded
3e3f0 20 6f 6e 2e 20 54 68 65 20 0a 20 2a 20 6e 65 77   on. The . * new
3e400 49 64 78 20 6d 65 6d 62 65 72 20 63 6f 6e 74 61  Idx member conta
3e410 69 6e 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66  ins the index of
3e420 20 74 68 65 20 76 64 62 65 20 63 75 72 73 6f 72   the vdbe cursor
3e430 20 74 68 61 74 20 70 6f 69 6e 74 73 20 61 74 20   that points at 
3e440 74 68 65 20 74 65 6d 70 0a 20 2a 20 74 61 62 6c  the temp. * tabl
3e450 65 20 74 68 61 74 20 73 74 6f 72 65 73 20 74 68  e that stores th
3e460 65 20 6e 65 77 2e 2a 20 72 65 66 65 72 65 6e 63  e new.* referenc
3e470 65 73 2e 20 49 66 20 6e 65 77 2e 2a 20 72 65 66  es. If new.* ref
3e480 65 72 65 6e 63 65 73 20 61 72 65 20 6e 6f 74 20  erences are not 
3e490 76 61 6c 69 64 0a 20 2a 20 66 6f 72 20 74 68 65  valid. * for the
3e4a0 20 74 72 69 67 67 65 72 20 62 65 69 6e 67 20 63   trigger being c
3e4b0 6f 64 65 64 20 28 66 6f 72 20 65 78 61 6d 70 6c  oded (for exampl
3e4c0 65 20 61 6e 20 4f 4e 20 44 45 4c 45 54 45 20 74  e an ON DELETE t
3e4d0 72 69 67 67 65 72 29 2c 20 74 68 65 6e 20 6e 65  rigger), then ne
3e4e0 77 49 64 78 0a 20 2a 20 69 73 20 73 65 74 20 74  wIdx. * is set t
3e4f0 6f 20 2d 31 2e 20 54 68 65 20 6f 6c 64 49 64 78  o -1. The oldIdx
3e500 20 6d 65 6d 62 65 72 20 69 73 20 61 6e 61 6c 6f   member is analo
3e510 67 6f 75 73 20 74 6f 20 6e 65 77 49 64 78 2c 20  gous to newIdx, 
3e520 66 6f 72 20 6f 6c 64 2e 2a 20 72 65 66 65 72 65  for old.* refere
3e530 6e 63 65 73 2e 0a 20 2a 0a 20 2a 20 54 68 65 20  nces.. *. * The 
3e540 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c 69  ON CONFLICT poli
3e550 63 79 20 74 6f 20 62 65 20 75 73 65 64 20 66 6f  cy to be used fo
3e560 72 20 74 68 65 20 74 72 69 67 67 65 72 20 70 72  r the trigger pr
3e570 6f 67 72 61 6d 20 73 74 65 70 73 20 69 73 20 73  ogram steps is s
3e580 74 6f 72 65 64 20 0a 20 2a 20 61 73 20 74 68 65  tored . * as the
3e590 20 6f 72 63 6f 6e 66 20 6d 65 6d 62 65 72 2e 20   orconf member. 
3e5a0 49 66 20 74 68 69 73 20 69 73 20 4f 45 5f 44 65  If this is OE_De
3e5b0 66 61 75 6c 74 2c 20 74 68 65 6e 20 74 68 65 20  fault, then the 
3e5c0 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c 61 75  ON CONFLICT clau
3e5d0 73 65 20 0a 20 2a 20 73 70 65 63 69 66 69 65 64  se . * specified
3e5e0 20 66 6f 72 20 69 6e 64 69 76 69 64 75 61 6c 20   for individual 
3e5f0 74 72 69 67 67 65 72 73 20 73 74 65 70 73 20 69  triggers steps i
3e600 73 20 75 73 65 64 2e 0a 20 2a 0a 20 2a 20 73 74  s used.. *. * st
3e610 72 75 63 74 20 54 72 69 67 67 65 72 53 74 61 63  ruct TriggerStac
3e620 6b 20 68 61 73 20 61 20 22 70 4e 65 78 74 22 20  k has a "pNext" 
3e630 6d 65 6d 62 65 72 2c 20 74 6f 20 61 6c 6c 6f 77  member, to allow
3e640 20 6c 69 6e 6b 65 64 20 6c 69 73 74 73 20 74 6f   linked lists to
3e650 20 62 65 0a 20 2a 20 63 6f 6e 73 74 72 75 63 74   be. * construct
3e660 65 64 2e 20 57 68 65 6e 20 63 6f 64 69 6e 67 20  ed. When coding 
3e670 6e 65 73 74 65 64 20 74 72 69 67 67 65 72 73 20  nested triggers 
3e680 28 74 72 69 67 67 65 72 73 20 66 69 72 65 64 20  (triggers fired 
3e690 62 79 20 6f 74 68 65 72 20 74 72 69 67 67 65 72  by other trigger
3e6a0 73 29 0a 20 2a 20 65 61 63 68 20 6e 65 73 74 65  s). * each neste
3e6b0 64 20 74 72 69 67 67 65 72 20 73 74 6f 72 65 73  d trigger stores
3e6c0 20 69 74 73 20 70 61 72 65 6e 74 20 74 72 69 67   its parent trig
3e6d0 67 65 72 27 73 20 54 72 69 67 67 65 72 53 74 61  ger's TriggerSta
3e6e0 63 6b 20 61 73 20 74 68 65 20 22 70 4e 65 78 74  ck as the "pNext
3e6f0 22 20 0a 20 2a 20 70 6f 69 6e 74 65 72 2e 20 4f  " . * pointer. O
3e700 6e 63 65 20 74 68 65 20 6e 65 73 74 65 64 20 74  nce the nested t
3e710 72 69 67 67 65 72 20 68 61 73 20 62 65 65 6e 20  rigger has been 
3e720 63 6f 64 65 64 2c 20 74 68 65 20 70 4e 65 78 74  coded, the pNext
3e730 20 76 61 6c 75 65 20 69 73 20 72 65 73 74 6f 72   value is restor
3e740 65 64 0a 20 2a 20 74 6f 20 74 68 65 20 70 54 72  ed. * to the pTr
3e750 69 67 67 65 72 53 74 61 63 6b 20 6d 65 6d 62 65  iggerStack membe
3e760 72 20 6f 66 20 74 68 65 20 50 61 72 73 65 20 73  r of the Parse s
3e770 74 75 63 74 75 72 65 20 61 6e 64 20 63 6f 64 69  tucture and codi
3e780 6e 67 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74  ng of the parent
3e790 0a 20 2a 20 74 72 69 67 67 65 72 20 63 6f 6e 74  . * trigger cont
3e7a0 69 6e 75 65 73 2e 0a 20 2a 0a 20 2a 20 42 65 66  inues.. *. * Bef
3e7b0 6f 72 65 20 61 20 6e 65 73 74 65 64 20 74 72 69  ore a nested tri
3e7c0 67 67 65 72 20 69 73 20 63 6f 64 65 64 2c 20 74  gger is coded, t
3e7d0 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 70  he linked list p
3e7e0 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65  ointed to by the
3e7f0 20 0a 20 2a 20 70 54 72 69 67 67 65 72 53 74 61   . * pTriggerSta
3e800 63 6b 20 69 73 20 73 63 61 6e 6e 65 64 20 74 6f  ck is scanned to
3e810 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 65   ensure that the
3e820 20 74 72 69 67 67 65 72 20 69 73 20 6e 6f 74 20   trigger is not 
3e830 61 62 6f 75 74 20 74 6f 20 62 65 20 63 6f 64 65  about to be code
3e840 64 0a 20 2a 20 72 65 63 75 72 73 69 76 65 6c 79  d. * recursively
3e850 2e 20 49 66 20 74 68 69 73 20 63 6f 6e 64 69 74  . If this condit
3e860 69 6f 6e 20 69 73 20 64 65 74 65 63 74 65 64 2c  ion is detected,
3e870 20 74 68 65 20 6e 65 73 74 65 64 20 74 72 69 67   the nested trig
3e880 67 65 72 20 69 73 20 6e 6f 74 20 63 6f 64 65 64  ger is not coded
3e890 2e 0a 20 2a 2f 0a 73 74 72 75 63 74 20 54 72 69  .. */.struct Tri
3e8a0 67 67 65 72 53 74 61 63 6b 20 7b 0a 20 20 54 61  ggerStack {.  Ta
3e8b0 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20  ble *pTab;      
3e8c0 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 68 61 74     /* Table that
3e8d0 20 74 72 69 67 67 65 72 73 20 61 72 65 20 63 75   triggers are cu
3e8e0 72 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 63 6f  rrently being co
3e8f0 64 65 64 20 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  ded on */.  int 
3e900 6e 65 77 49 64 78 3b 20 20 20 20 20 20 20 20 20  newIdx;         
3e910 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 76 64 62   /* Index of vdb
3e920 65 20 63 75 72 73 6f 72 20 74 6f 20 22 6e 65 77  e cursor to "new
3e930 22 20 74 65 6d 70 20 74 61 62 6c 65 20 2a 2f 0a  " temp table */.
3e940 20 20 69 6e 74 20 6f 6c 64 49 64 78 3b 20 20 20    int oldIdx;   
3e950 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
3e960 6f 66 20 76 64 62 65 20 63 75 72 73 6f 72 20 74  of vdbe cursor t
3e970 6f 20 22 6f 6c 64 22 20 74 65 6d 70 20 74 61 62  o "old" temp tab
3e980 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 63 6f  le */.  int orco
3e990 6e 66 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  nf;          /* 
3e9a0 43 75 72 72 65 6e 74 20 6f 72 63 6f 6e 66 20 70  Current orconf p
3e9b0 6f 6c 69 63 79 20 2a 2f 0a 20 20 69 6e 74 20 69  olicy */.  int i
3e9c0 67 6e 6f 72 65 4a 75 6d 70 3b 20 20 20 20 20 20  gnoreJump;      
3e9d0 2f 2a 20 77 68 65 72 65 20 74 6f 20 6a 75 6d 70  /* where to jump
3e9e0 20 74 6f 20 66 6f 72 20 61 20 52 41 49 53 45 28   to for a RAISE(
3e9f0 49 47 4e 4f 52 45 29 20 2a 2f 0a 20 20 54 72 69  IGNORE) */.  Tri
3ea00 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 20  gger *pTrigger; 
3ea10 20 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72    /* The trigger
3ea20 20 63 75 72 72 65 6e 74 6c 79 20 62 65 69 6e 67   currently being
3ea30 20 63 6f 64 65 64 20 2a 2f 0a 20 20 54 72 69 67   coded */.  Trig
3ea40 67 65 72 53 74 61 63 6b 20 2a 70 4e 65 78 74 3b  gerStack *pNext;
3ea50 20 2f 2a 20 4e 65 78 74 20 74 72 69 67 67 65 72   /* Next trigger
3ea60 20 64 6f 77 6e 20 6f 6e 20 74 68 65 20 74 72 69   down on the tri
3ea70 67 67 65 72 20 73 74 61 63 6b 20 2a 2f 0a 7d 3b  gger stack */.};
3ea80 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
3ea90 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
3eaa0 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61  contains informa
3eab0 74 69 6f 6e 20 75 73 65 64 20 62 79 20 74 68 65  tion used by the
3eac0 20 73 71 6c 69 74 65 46 69 78 2e 2e 2e 0a 2a 2a   sqliteFix....**
3ead0 20 72 6f 75 74 69 6e 65 73 20 61 73 20 74 68 65   routines as the
3eae0 79 20 77 61 6c 6b 20 74 68 65 20 70 61 72 73 65  y walk the parse
3eaf0 20 74 72 65 65 20 74 6f 20 6d 61 6b 65 20 64 61   tree to make da
3eb00 74 61 62 61 73 65 20 72 65 66 65 72 65 6e 63 65  tabase reference
3eb10 73 0a 2a 2a 20 65 78 70 6c 69 63 69 74 2e 20 20  s.** explicit.  
3eb20 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
3eb30 63 74 20 44 62 46 69 78 65 72 20 44 62 46 69 78  ct DbFixer DbFix
3eb40 65 72 3b 0a 73 74 72 75 63 74 20 44 62 46 69 78  er;.struct DbFix
3eb50 65 72 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50  er {.  Parse *pP
3eb60 61 72 73 65 3b 20 20 20 20 20 20 2f 2a 20 54 68  arse;      /* Th
3eb70 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  e parsing contex
3eb80 74 2e 20 20 45 72 72 6f 72 20 6d 65 73 73 61 67  t.  Error messag
3eb90 65 73 20 77 72 69 74 74 65 6e 20 68 65 72 65 20  es written here 
3eba0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
3ebb0 2a 7a 44 62 3b 20 20 20 20 2f 2a 20 4d 61 6b 65  *zDb;    /* Make
3ebc0 20 73 75 72 65 20 61 6c 6c 20 6f 62 6a 65 63 74   sure all object
3ebd0 73 20 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20  s are contained 
3ebe0 69 6e 20 74 68 69 73 20 64 61 74 61 62 61 73 65  in this database
3ebf0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
3ec00 20 2a 7a 54 79 70 65 3b 20 20 2f 2a 20 54 79 70   *zType;  /* Typ
3ec10 65 20 6f 66 20 74 68 65 20 63 6f 6e 74 61 69 6e  e of the contain
3ec20 65 72 20 2d 20 75 73 65 64 20 66 6f 72 20 65 72  er - used for er
3ec30 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a  ror messages */.
3ec40 20 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70    const Token *p
3ec50 4e 61 6d 65 3b 20 2f 2a 20 4e 61 6d 65 20 6f 66  Name; /* Name of
3ec60 20 74 68 65 20 63 6f 6e 74 61 69 6e 65 72 20 2d   the container -
3ec70 20 75 73 65 64 20 66 6f 72 20 65 72 72 6f 72 20   used for error 
3ec80 6d 65 73 73 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a  messages */.};..
3ec90 2f 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20  /*.** A pointer 
3eca0 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72  to this structur
3ecb0 65 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6d  e is used to com
3ecc0 6d 75 6e 69 63 61 74 65 20 69 6e 66 6f 72 6d 61  municate informa
3ecd0 74 69 6f 6e 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c  tion.** from sql
3ece0 69 74 65 33 49 6e 69 74 20 61 6e 64 20 4f 50 5f  ite3Init and OP_
3ecf0 50 61 72 73 65 53 63 68 65 6d 61 20 69 6e 74 6f  ParseSchema into
3ed00 20 74 68 65 20 73 71 6c 69 74 65 33 49 6e 69 74   the sqlite3Init
3ed10 43 61 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 74 79 70  Callback..*/.typ
3ed20 65 64 65 66 20 73 74 72 75 63 74 20 7b 0a 20 20  edef struct {.  
3ed30 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20  sqlite3 *db;    
3ed40 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
3ed50 61 73 65 20 62 65 69 6e 67 20 69 6e 69 74 69 61  ase being initia
3ed60 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69  lized */.  int i
3ed70 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  Db;            /
3ed80 2a 20 30 20 66 6f 72 20 6d 61 69 6e 20 64 61 74  * 0 for main dat
3ed90 61 62 61 73 65 2e 20 20 31 20 66 6f 72 20 54 45  abase.  1 for TE
3eda0 4d 50 2c 20 32 2e 2e 20 66 6f 72 20 41 54 54 41  MP, 2.. for ATTA
3edb0 43 48 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a  CHed */.  char *
3edc0 2a 70 7a 45 72 72 4d 73 67 3b 20 20 20 20 2f 2a  *pzErrMsg;    /*
3edd0 20 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73   Error message s
3ede0 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20 20  tored here */.  
3edf0 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
3ee00 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f      /* Result co
3ee10 64 65 20 73 74 6f 72 65 64 20 68 65 72 65 20 2a  de stored here *
3ee20 2f 0a 7d 20 49 6e 69 74 44 61 74 61 3b 0a 0a 2f  /.} InitData;../
3ee30 2a 0a 20 2a 20 54 68 69 73 20 67 6c 6f 62 61 6c  *. * This global
3ee40 20 66 6c 61 67 20 69 73 20 73 65 74 20 66 6f 72   flag is set for
3ee50 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 65 73   performance tes
3ee60 74 69 6e 67 20 6f 66 20 74 72 69 67 67 65 72 73  ting of triggers
3ee70 2e 20 57 68 65 6e 20 69 74 20 69 73 20 73 65 74  . When it is set
3ee80 0a 20 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  . * SQLite will 
3ee90 70 65 72 66 6f 72 6d 20 74 68 65 20 6f 76 65 72  perform the over
3eea0 68 65 61 64 20 6f 66 20 62 75 69 6c 64 69 6e 67  head of building
3eeb0 20 6e 65 77 20 61 6e 64 20 6f 6c 64 20 74 72 69   new and old tri
3eec0 67 67 65 72 20 72 65 66 65 72 65 6e 63 65 73 20  gger references 
3eed0 0a 20 2a 20 65 76 65 6e 20 77 68 65 6e 20 6e 6f  . * even when no
3eee0 20 74 72 69 67 67 65 72 73 20 65 78 69 73 74 0a   triggers exist.
3eef0 20 2a 2f 0a 65 78 74 65 72 6e 20 69 6e 74 20 73   */.extern int s
3ef00 71 6c 69 74 65 33 5f 61 6c 77 61 79 73 5f 63 6f  qlite3_always_co
3ef10 64 65 5f 74 72 69 67 67 65 72 5f 73 65 74 75 70  de_trigger_setup
3ef20 3b 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e  ;../*.** Assumin
3ef30 67 20 7a 49 6e 20 70 6f 69 6e 74 73 20 74 6f 20  g zIn points to 
3ef40 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20 6f  the first byte o
3ef50 66 20 61 20 55 54 46 2d 38 20 63 68 61 72 61 63  f a UTF-8 charac
3ef60 74 65 72 2c 0a 2a 2a 20 61 64 76 61 6e 63 65 20  ter,.** advance 
3ef70 7a 49 6e 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20  zIn to point to 
3ef80 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20 6f  the first byte o
3ef90 66 20 74 68 65 20 6e 65 78 74 20 55 54 46 2d 38  f the next UTF-8
3efa0 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2f 0a 23   character..*/.#
3efb0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 4b  define SQLITE_SK
3efc0 49 50 5f 55 54 46 38 28 7a 49 6e 29 20 7b 20 20  IP_UTF8(zIn) {  
3efd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3efe0 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 28 2a        \.  if( (*
3eff0 28 7a 49 6e 2b 2b 29 29 3e 3d 30 78 63 30 20 29  (zIn++))>=0xc0 )
3f000 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
3f010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
3f020 0a 20 20 20 20 77 68 69 6c 65 28 20 28 2a 7a 49  .    while( (*zI
3f030 6e 20 26 20 30 78 63 30 29 3d 3d 30 78 38 30 20  n & 0xc0)==0x80 
3f040 29 7b 20 7a 49 6e 2b 2b 3b 20 7d 20 20 20 20 20  ){ zIn++; }     
3f050 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20          \.  }   
3f060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3f070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3f080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3f090 20 5c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20   \.}../*.** The 
3f0a0 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
3f0b0 4b 50 54 20 6d 61 63 72 6f 20 63 61 6e 20 62 65  KPT macro can be
3f0c0 20 65 69 74 68 65 72 20 61 20 63 6f 6e 73 74 61   either a consta
3f0d0 6e 74 20 28 66 6f 72 20 70 72 6f 64 75 63 74 69  nt (for producti
3f0e0 6f 6e 0a 2a 2a 20 62 75 69 6c 64 73 29 20 6f 72  on.** builds) or
3f0f0 20 61 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c   a function call
3f100 20 28 66 6f 72 20 64 65 62 75 67 67 69 6e 67 29   (for debugging)
3f110 2e 20 20 49 66 20 69 74 20 69 73 20 61 20 66 75  .  If it is a fu
3f120 6e 63 74 69 6f 6e 20 63 61 6c 6c 2c 0a 2a 2a 20  nction call,.** 
3f130 69 74 20 61 6c 6c 6f 77 73 20 74 68 65 20 6f 70  it allows the op
3f140 65 72 61 74 6f 72 20 74 6f 20 73 65 74 20 61 20  erator to set a 
3f150 62 72 65 61 6b 70 6f 69 6e 74 20 61 74 20 74 68  breakpoint at th
3f160 65 20 73 70 6f 74 20 77 68 65 72 65 20 64 61 74  e spot where dat
3f170 61 62 61 73 65 0a 2a 2a 20 63 6f 72 72 75 70 74  abase.** corrupt
3f180 69 6f 6e 20 69 73 20 66 69 72 73 74 20 64 65 74  ion is first det
3f190 65 63 74 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66  ected..*/.#ifdef
3f1a0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51   SQLITE_DEBUG.SQ
3f1b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69  LITE_PRIVATE   i
3f1c0 6e 74 20 73 71 6c 69 74 65 33 43 6f 72 72 75 70  nt sqlite3Corrup
3f1d0 74 28 76 6f 69 64 29 3b 0a 23 20 64 65 66 69 6e  t(void);.# defin
3f1e0 65 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  e SQLITE_CORRUPT
3f1f0 5f 42 4b 50 54 20 73 71 6c 69 74 65 33 43 6f 72  _BKPT sqlite3Cor
3f200 72 75 70 74 28 29 0a 23 65 6c 73 65 0a 23 20 64  rupt().#else.# d
3f210 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52  efine SQLITE_COR
3f220 52 55 50 54 5f 42 4b 50 54 20 53 51 4c 49 54 45  RUPT_BKPT SQLITE
3f230 5f 43 4f 52 52 55 50 54 0a 23 65 6e 64 69 66 0a  _CORRUPT.#endif.
3f240 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20  ./*.** Internal 
3f250 66 75 6e 63 74 69 6f 6e 20 70 72 6f 74 6f 74 79  function prototy
3f260 70 65 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  pes.*/.SQLITE_PR
3f270 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
3f280 33 53 74 72 49 43 6d 70 28 63 6f 6e 73 74 20 63  3StrICmp(const c
3f290 68 61 72 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61  har *, const cha
3f2a0 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  r *);.SQLITE_PRI
3f2b0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
3f2c0 53 74 72 4e 49 43 6d 70 28 63 6f 6e 73 74 20 63  StrNICmp(const c
3f2d0 68 61 72 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61  har *, const cha
3f2e0 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  r *, int);.SQLIT
3f2f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
3f300 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28 63 6f  lite3IsNumber(co
3f310 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 2a 2c  nst char*, int*,
3f320 20 75 38 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52   u8);..SQLITE_PR
3f330 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
3f340 74 65 33 4d 61 6c 6c 6f 63 28 69 6e 74 2c 69 6e  te3Malloc(int,in
3f350 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
3f360 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
3f370 4d 61 6c 6c 6f 63 52 61 77 28 69 6e 74 2c 69 6e  MallocRaw(int,in
3f380 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
3f390 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
3f3a0 52 65 61 6c 6c 6f 63 28 76 6f 69 64 2a 2c 69 6e  Realloc(void*,in
3f3b0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
3f3c0 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  TE char *sqlite3
3f3d0 53 74 72 44 75 70 28 63 6f 6e 73 74 20 63 68 61  StrDup(const cha
3f3e0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
3f3f0 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65  ATE char *sqlite
3f400 33 53 74 72 4e 44 75 70 28 63 6f 6e 73 74 20 63  3StrNDup(const c
3f410 68 61 72 2a 2c 20 69 6e 74 29 3b 0a 23 20 64 65  har*, int);.# de
3f420 66 69 6e 65 20 73 71 6c 69 74 65 33 43 68 65 63  fine sqlite3Chec
3f430 6b 4d 65 6d 6f 72 79 28 61 2c 62 29 0a 53 51 4c  kMemory(a,b).SQL
3f440 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
3f450 20 2a 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63   *sqlite3Realloc
3f460 4f 72 46 72 65 65 28 76 6f 69 64 2a 2c 69 6e 74  OrFree(void*,int
3f470 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
3f480 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 72  E void sqlite3Fr
3f490 65 65 58 28 76 6f 69 64 2a 29 3b 0a 53 51 4c 49  eeX(void*);.SQLI
3f4a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
3f4b0 2a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 58 28  *sqlite3MallocX(
3f4c0 69 6e 74 29 3b 0a 23 69 66 64 65 66 20 53 51 4c  int);.#ifdef SQL
3f4d0 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52  ITE_ENABLE_MEMOR
3f4e0 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 53 51 4c  Y_MANAGEMENT.SQL
3f4f0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e  ITE_PRIVATE   in
3f500 74 20 73 71 6c 69 74 65 33 41 6c 6c 6f 63 53 69  t sqlite3AllocSi
3f510 7a 65 28 76 6f 69 64 20 2a 29 3b 0a 23 65 6e 64  ze(void *);.#end
3f520 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  if..SQLITE_PRIVA
3f530 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  TE char *sqlite3
3f540 4d 50 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68  MPrintf(const ch
3f550 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54  ar*, ...);.SQLIT
3f560 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a  E_PRIVATE char *
3f570 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28  sqlite3VMPrintf(
3f580 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76 61 5f  const char*, va_
3f590 6c 69 73 74 29 3b 0a 23 69 66 20 64 65 66 69 6e  list);.#if defin
3f5a0 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20  ed(SQLITE_TEST) 
3f5b0 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  || defined(SQLIT
3f5c0 45 5f 44 45 42 55 47 29 0a 53 51 4c 49 54 45 5f  E_DEBUG).SQLITE_
3f5d0 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
3f5e0 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
3f5f0 66 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e  f(const char*, .
3f600 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..);.SQLITE_PRIV
3f610 41 54 45 20 20 20 76 6f 69 64 20 2a 73 71 6c 69  ATE   void *sqli
3f620 74 65 33 54 65 78 74 54 6f 50 74 72 28 63 6f 6e  te3TextToPtr(con
3f630 73 74 20 63 68 61 72 2a 29 3b 0a 23 65 6e 64 69  st char*);.#endi
3f640 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  f.SQLITE_PRIVATE
3f650 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 74   void sqlite3Set
3f660 53 74 72 69 6e 67 28 63 68 61 72 20 2a 2a 2c 20  String(char **, 
3f670 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ...);.SQLITE_PRI
3f680 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
3f690 33 45 72 72 6f 72 4d 73 67 28 50 61 72 73 65 2a  3ErrorMsg(Parse*
3f6a0 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e  , const char*, .
3f6b0 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..);.SQLITE_PRIV
3f6c0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
3f6d0 45 72 72 6f 72 43 6c 65 61 72 28 50 61 72 73 65  ErrorClear(Parse
3f6e0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
3f6f0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
3f700 65 71 75 6f 74 65 28 63 68 61 72 2a 29 3b 0a 53  equote(char*);.S
3f710 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
3f720 69 64 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74  id sqlite3Dequot
3f730 65 45 78 70 72 28 45 78 70 72 2a 29 3b 0a 53 51  eExpr(Expr*);.SQ
3f740 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
3f750 20 73 71 6c 69 74 65 33 4b 65 79 77 6f 72 64 43   sqlite3KeywordC
3f760 6f 64 65 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e  ode(const unsign
3f770 65 64 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b 0a  ed char*, int);.
3f780 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
3f790 6e 74 20 73 71 6c 69 74 65 33 52 75 6e 50 61 72  nt sqlite3RunPar
3f7a0 73 65 72 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73  ser(Parse*, cons
3f7b0 74 20 63 68 61 72 2a 2c 20 63 68 61 72 20 2a 2a  t char*, char **
3f7c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
3f7d0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 69  E void sqlite3Fi
3f7e0 6e 69 73 68 43 6f 64 69 6e 67 28 50 61 72 73 65  nishCoding(Parse
3f7f0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
3f800 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33  TE Expr *sqlite3
3f810 45 78 70 72 28 69 6e 74 2c 20 45 78 70 72 2a 2c  Expr(int, Expr*,
3f820 20 45 78 70 72 2a 2c 20 63 6f 6e 73 74 20 54 6f   Expr*, const To
3f830 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ken*);.SQLITE_PR
3f840 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69  IVATE Expr *sqli
3f850 74 65 33 45 78 70 72 4f 72 46 72 65 65 28 69 6e  te3ExprOrFree(in
3f860 74 2c 20 45 78 70 72 2a 2c 20 45 78 70 72 2a 2c  t, Expr*, Expr*,
3f870 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 2a 29 3b 0a   const Token*);.
3f880 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45  SQLITE_PRIVATE E
3f890 78 70 72 20 2a 73 71 6c 69 74 65 33 52 65 67 69  xpr *sqlite3Regi
3f8a0 73 74 65 72 45 78 70 72 28 50 61 72 73 65 2a 2c  sterExpr(Parse*,
3f8b0 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  Token*);.SQLITE_
3f8c0 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71  PRIVATE Expr *sq
3f8d0 6c 69 74 65 33 45 78 70 72 41 6e 64 28 45 78 70  lite3ExprAnd(Exp
3f8e0 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49  r*, Expr*);.SQLI
3f8f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
3f900 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28  sqlite3ExprSpan(
3f910 45 78 70 72 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b  Expr*,Token*,Tok
3f920 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  en*);.SQLITE_PRI
3f930 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74  VATE Expr *sqlit
3f940 65 33 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 45  e3ExprFunction(E
3f950 78 70 72 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a  xprList*, Token*
3f960 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
3f970 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
3f980 70 72 41 73 73 69 67 6e 56 61 72 4e 75 6d 62 65  prAssignVarNumbe
3f990 72 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 29  r(Parse*, Expr*)
3f9a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
3f9b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
3f9c0 72 44 65 6c 65 74 65 28 45 78 70 72 2a 29 3b 0a  rDelete(Expr*);.
3f9d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45  SQLITE_PRIVATE E
3f9e0 78 70 72 4c 69 73 74 20 2a 73 71 6c 69 74 65 33  xprList *sqlite3
3f9f0 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 45  ExprListAppend(E
3fa00 78 70 72 4c 69 73 74 2a 2c 45 78 70 72 2a 2c 54  xprList*,Expr*,T
3fa10 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  oken*);.SQLITE_P
3fa20 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
3fa30 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
3fa40 65 28 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53 51  e(ExprList*);.SQ
3fa50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
3fa60 20 73 71 6c 69 74 65 33 49 6e 69 74 28 73 71 6c   sqlite3Init(sql
3fa70 69 74 65 33 2a 2c 20 63 68 61 72 2a 2a 29 3b 0a  ite3*, char**);.
3fa80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
3fa90 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74 43 61  nt sqlite3InitCa
3faa0 6c 6c 62 61 63 6b 28 76 6f 69 64 2a 2c 20 69 6e  llback(void*, in
3fab0 74 2c 20 63 68 61 72 2a 2a 2c 20 63 68 61 72 2a  t, char**, char*
3fac0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
3fad0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
3fae0 72 61 67 6d 61 28 50 61 72 73 65 2a 2c 54 6f 6b  ragma(Parse*,Tok
3faf0 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e  en*,Token*,Token
3fb00 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  *,int);.SQLITE_P
3fb10 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
3fb20 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c  te3ResetInternal
3fb30 53 63 68 65 6d 61 28 73 71 6c 69 74 65 33 2a 2c  Schema(sqlite3*,
3fb40 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
3fb50 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
3fb60 65 33 42 65 67 69 6e 50 61 72 73 65 28 50 61 72  e3BeginParse(Par
3fb70 73 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  se*,int);.SQLITE
3fb80 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
3fb90 6c 69 74 65 33 43 6f 6d 6d 69 74 49 6e 74 65 72  lite3CommitInter
3fba0 6e 61 6c 43 68 61 6e 67 65 73 28 73 71 6c 69 74  nalChanges(sqlit
3fbb0 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  e3*);.SQLITE_PRI
3fbc0 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69  VATE Table *sqli
3fbd0 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65  te3ResultSetOfSe
3fbe0 6c 65 63 74 28 50 61 72 73 65 2a 2c 63 68 61 72  lect(Parse*,char
3fbf0 2a 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49  *,Select*);.SQLI
3fc00 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
3fc10 73 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 65  sqlite3OpenMaste
3fc20 72 54 61 62 6c 65 28 50 61 72 73 65 20 2a 2c 20  rTable(Parse *, 
3fc30 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
3fc40 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
3fc50 33 53 74 61 72 74 54 61 62 6c 65 28 50 61 72 73  3StartTable(Pars
3fc60 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a  e*,Token*,Token*
3fc70 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74  ,int,int,int,int
3fc80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
3fc90 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64  E void sqlite3Ad
3fca0 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 2a 2c 54  dColumn(Parse*,T
3fcb0 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  oken*);.SQLITE_P
3fcc0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
3fcd0 74 65 33 41 64 64 4e 6f 74 4e 75 6c 6c 28 50 61  te3AddNotNull(Pa
3fce0 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  rse*, int);.SQLI
3fcf0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
3fd00 73 71 6c 69 74 65 33 41 64 64 50 72 69 6d 61 72  sqlite3AddPrimar
3fd10 79 4b 65 79 28 50 61 72 73 65 2a 2c 20 45 78 70  yKey(Parse*, Exp
3fd20 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74  rList*, int, int
3fd30 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
3fd40 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
3fd50 74 65 33 41 64 64 43 68 65 63 6b 43 6f 6e 73 74  te3AddCheckConst
3fd60 72 61 69 6e 74 28 50 61 72 73 65 2a 2c 20 45 78  raint(Parse*, Ex
3fd70 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  pr*);.SQLITE_PRI
3fd80 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
3fd90 33 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28 50  3AddColumnType(P
3fda0 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53  arse*,Token*);.S
3fdb0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
3fdc0 69 64 20 73 71 6c 69 74 65 33 41 64 64 44 65 66  id sqlite3AddDef
3fdd0 61 75 6c 74 56 61 6c 75 65 28 50 61 72 73 65 2a  aultValue(Parse*
3fde0 2c 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  ,Expr*);.SQLITE_
3fdf0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
3fe00 69 74 65 33 41 64 64 43 6f 6c 6c 61 74 65 54 79  ite3AddCollateTy
3fe10 70 65 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74  pe(Parse*, const
3fe20 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b 0a 53 51   char*, int);.SQ
3fe30 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
3fe40 64 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c  d sqlite3EndTabl
3fe50 65 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 2c  e(Parse*,Token*,
3fe60 54 6f 6b 65 6e 2a 2c 53 65 6c 65 63 74 2a 29 3b  Token*,Select*);
3fe70 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
3fe80 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65   void sqlite3Cre
3fe90 61 74 65 56 69 65 77 28 50 61 72 73 65 2a 2c 54  ateView(Parse*,T
3fea0 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b  oken*,Token*,Tok
3feb0 65 6e 2a 2c 53 65 6c 65 63 74 2a 2c 69 6e 74 2c  en*,Select*,int,
3fec0 69 6e 74 29 3b 0a 0a 23 69 66 20 21 64 65 66 69  int);..#if !defi
3fed0 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
3fee0 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65  VIEW) || !define
3fef0 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  d(SQLITE_OMIT_VI
3ff00 52 54 55 41 4c 54 41 42 4c 45 29 0a 53 51 4c 49  RTUALTABLE).SQLI
3ff10 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
3ff20 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43   sqlite3ViewGetC
3ff30 6f 6c 75 6d 6e 4e 61 6d 65 73 28 50 61 72 73 65  olumnNames(Parse
3ff40 2a 2c 54 61 62 6c 65 2a 29 3b 0a 23 65 6c 73 65  *,Table*);.#else
3ff50 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
3ff60 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61  3ViewGetColumnNa
3ff70 6d 65 73 28 41 2c 42 29 20 30 0a 23 65 6e 64 69  mes(A,B) 0.#endi
3ff80 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  f..SQLITE_PRIVAT
3ff90 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72  E void sqlite3Dr
3ffa0 6f 70 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 20  opTable(Parse*, 
3ffb0 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69  SrcList*, int, i
3ffc0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
3ffd0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
3ffe0 44 65 6c 65 74 65 54 61 62 6c 65 28 54 61 62 6c  DeleteTable(Tabl
3fff0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
40000 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
40010 49 6e 73 65 72 74 28 50 61 72 73 65 2a 2c 20 53  Insert(Parse*, S
40020 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 4c 69 73  rcList*, ExprLis
40030 74 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 49 64 4c  t*, Select*, IdL
40040 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  ist*, int);.SQLI
40050 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
40060 2a 73 71 6c 69 74 65 33 41 72 72 61 79 41 6c 6c  *sqlite3ArrayAll
40070 6f 63 61 74 65 28 76 6f 69 64 2a 2c 69 6e 74 2c  ocate(void*,int,
40080 69 6e 74 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e  int,int*,int*,in
40090 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
400a0 41 54 45 20 49 64 4c 69 73 74 20 2a 73 71 6c 69  ATE IdList *sqli
400b0 74 65 33 49 64 4c 69 73 74 41 70 70 65 6e 64 28  te3IdListAppend(
400c0 49 64 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 29  IdList*, Token*)
400d0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
400e0 20 69 6e 74 20 73 71 6c 69 74 65 33 49 64 4c 69   int sqlite3IdLi
400f0 73 74 49 6e 64 65 78 28 49 64 4c 69 73 74 2a 2c  stIndex(IdList*,
40100 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51  const char*);.SQ
40110 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 72 63  LITE_PRIVATE Src
40120 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63  List *sqlite3Src
40130 4c 69 73 74 41 70 70 65 6e 64 28 53 72 63 4c 69  ListAppend(SrcLi
40140 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b  st*, Token*, Tok
40150 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  en*);.SQLITE_PRI
40160 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71  VATE SrcList *sq
40170 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65  lite3SrcListAppe
40180 6e 64 46 72 6f 6d 54 65 72 6d 28 53 72 63 4c 69  ndFromTerm(SrcLi
40190 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b  st*, Token*, Tok
401a0 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 0a 20 20 20  en*, Token*,.   
401b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
401c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
401d0 20 20 20 53 65 6c 65 63 74 2a 2c 20 45 78 70 72     Select*, Expr
401e0 2a 2c 20 49 64 4c 69 73 74 2a 29 3b 0a 53 51 4c  *, IdList*);.SQL
401f0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
40200 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 53   sqlite3SrcListS
40210 68 69 66 74 4a 6f 69 6e 54 79 70 65 28 53 72 63  hiftJoinType(Src
40220 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  List*);.SQLITE_P
40230 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
40240 74 65 33 53 72 63 4c 69 73 74 41 73 73 69 67 6e  te3SrcListAssign
40250 43 75 72 73 6f 72 73 28 50 61 72 73 65 2a 2c 20  Cursors(Parse*, 
40260 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54  SrcList*);.SQLIT
40270 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
40280 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65  qlite3IdListDele
40290 74 65 28 49 64 4c 69 73 74 2a 29 3b 0a 53 51 4c  te(IdList*);.SQL
402a0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
402b0 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44   sqlite3SrcListD
402c0 65 6c 65 74 65 28 53 72 63 4c 69 73 74 2a 29 3b  elete(SrcList*);
402d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
402e0 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65 61  void sqlite3Crea
402f0 74 65 49 6e 64 65 78 28 50 61 72 73 65 2a 2c 54  teIndex(Parse*,T
40300 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 72 63  oken*,Token*,Src
40310 4c 69 73 74 2a 2c 45 78 70 72 4c 69 73 74 2a 2c  List*,ExprList*,
40320 69 6e 74 2c 54 6f 6b 65 6e 2a 2c 0a 20 20 20 20  int,Token*,.    
40330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40340 20 20 20 20 54 6f 6b 65 6e 2a 2c 20 69 6e 74 2c      Token*, int,
40350 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
40360 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
40370 65 33 44 72 6f 70 49 6e 64 65 78 28 50 61 72 73  e3DropIndex(Pars
40380 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e  e*, SrcList*, in
40390 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
403a0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65  TE int sqlite3Se
403b0 6c 65 63 74 28 50 61 72 73 65 2a 2c 20 53 65 6c  lect(Parse*, Sel
403c0 65 63 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20  ect*, int, int, 
403d0 53 65 6c 65 63 74 2a 2c 20 69 6e 74 2c 20 69 6e  Select*, int, in
403e0 74 2a 2c 20 63 68 61 72 20 2a 61 66 66 29 3b 0a  t*, char *aff);.
403f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53  SQLITE_PRIVATE S
40400 65 6c 65 63 74 20 2a 73 71 6c 69 74 65 33 53 65  elect *sqlite3Se
40410 6c 65 63 74 4e 65 77 28 45 78 70 72 4c 69 73 74  lectNew(ExprList
40420 2a 2c 53 72 63 4c 69 73 74 2a 2c 45 78 70 72 2a  *,SrcList*,Expr*
40430 2c 45 78 70 72 4c 69 73 74 2a 2c 45 78 70 72 2a  ,ExprList*,Expr*
40440 2c 45 78 70 72 4c 69 73 74 2a 2c 0a 20 20 20 20  ,ExprList*,.    
40450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40460 20 20 20 20 69 6e 74 2c 45 78 70 72 2a 2c 45 78      int,Expr*,Ex
40470 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  pr*);.SQLITE_PRI
40480 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
40490 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 53 65  3SelectDelete(Se
404a0 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  lect*);.SQLITE_P
404b0 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71  RIVATE Table *sq
404c0 6c 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b  lite3SrcListLook
404d0 75 70 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69  up(Parse*, SrcLi
404e0 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  st*);.SQLITE_PRI
404f0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
40500 49 73 52 65 61 64 4f 6e 6c 79 28 50 61 72 73 65  IsReadOnly(Parse
40510 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 29 3b  *, Table*, int);
40520 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
40530 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 70 65 6e  void sqlite3Open
40540 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 20 69 6e  Table(Parse*, in
40550 74 20 69 43 75 72 2c 20 69 6e 74 20 69 44 62 2c  t iCur, int iDb,
40560 20 54 61 62 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53   Table*, int);.S
40570 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
40580 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65  id sqlite3Delete
40590 46 72 6f 6d 28 50 61 72 73 65 2a 2c 20 53 72 63  From(Parse*, Src
405a0 4c 69 73 74 2a 2c 20 45 78 70 72 2a 29 3b 0a 53  List*, Expr*);.S
405b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
405c0 69 64 20 73 71 6c 69 74 65 33 55 70 64 61 74 65  id sqlite3Update
405d0 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74  (Parse*, SrcList
405e0 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 45 78  *, ExprList*, Ex
405f0 70 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  pr*, int);.SQLIT
40600 45 5f 50 52 49 56 41 54 45 20 57 68 65 72 65 49  E_PRIVATE WhereI
40610 6e 66 6f 20 2a 73 71 6c 69 74 65 33 57 68 65 72  nfo *sqlite3Wher
40620 65 42 65 67 69 6e 28 50 61 72 73 65 2a 2c 20 53  eBegin(Parse*, S
40630 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 2a 2c 20  rcList*, Expr*, 
40640 45 78 70 72 4c 69 73 74 2a 2a 29 3b 0a 53 51 4c  ExprList**);.SQL
40650 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
40660 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64   sqlite3WhereEnd
40670 28 57 68 65 72 65 49 6e 66 6f 2a 29 3b 0a 53 51  (WhereInfo*);.SQ
40680 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
40690 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  d sqlite3ExprCod
406a0 65 47 65 74 43 6f 6c 75 6d 6e 28 56 64 62 65 2a  eGetColumn(Vdbe*
406b0 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69  , Table*, int, i
406c0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
406d0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
406e0 45 78 70 72 43 6f 64 65 28 50 61 72 73 65 2a 2c  ExprCode(Parse*,
406f0 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   Expr*);.SQLITE_
40700 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
40710 69 74 65 33 45 78 70 72 43 6f 64 65 41 6e 64 43  ite3ExprCodeAndC
40720 61 63 68 65 28 50 61 72 73 65 2a 2c 20 45 78 70  ache(Parse*, Exp
40730 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
40740 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45  ATE int sqlite3E
40750 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28  xprCodeExprList(
40760 50 61 72 73 65 2a 2c 20 45 78 70 72 4c 69 73 74  Parse*, ExprList
40770 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
40780 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
40790 78 70 72 49 66 54 72 75 65 28 50 61 72 73 65 2a  xprIfTrue(Parse*
407a0 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2c 20 69 6e  , Expr*, int, in
407b0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
407c0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
407d0 78 70 72 49 66 46 61 6c 73 65 28 50 61 72 73 65  xprIfFalse(Parse
407e0 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2c 20 69  *, Expr*, int, i
407f0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
40800 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74  ATE Table *sqlit
40810 65 33 46 69 6e 64 54 61 62 6c 65 28 73 71 6c 69  e3FindTable(sqli
40820 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  te3*,const char*
40830 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a  , const char*);.
40840 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54  SQLITE_PRIVATE T
40850 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 4c 6f 63  able *sqlite3Loc
40860 61 74 65 54 61 62 6c 65 28 50 61 72 73 65 2a 2c  ateTable(Parse*,
40870 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e  const char*, con
40880 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54  st char*);.SQLIT
40890 45 5f 50 52 49 56 41 54 45 20 49 6e 64 65 78 20  E_PRIVATE Index 
408a0 2a 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65  *sqlite3FindInde
408b0 78 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74  x(sqlite3*,const
408c0 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68   char*, const ch
408d0 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ar*);.SQLITE_PRI
408e0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
408f0 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65  3UnlinkAndDelete
40900 54 61 62 6c 65 28 73 71 6c 69 74 65 33 2a 2c 69  Table(sqlite3*,i
40910 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  nt,const char*);
40920 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
40930 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69  void sqlite3Unli
40940 6e 6b 41 6e 64 44 65 6c 65 74 65 49 6e 64 65 78  nkAndDeleteIndex
40950 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f  (sqlite3*,int,co
40960 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49  nst char*);.SQLI
40970 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
40980 73 71 6c 69 74 65 33 56 61 63 75 75 6d 28 50 61  sqlite3Vacuum(Pa
40990 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  rse*);.SQLITE_PR
409a0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
409b0 33 52 75 6e 56 61 63 75 75 6d 28 63 68 61 72 2a  3RunVacuum(char*
409c0 2a 2c 20 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51  *, sqlite3*);.SQ
409d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
409e0 72 20 2a 73 71 6c 69 74 65 33 4e 61 6d 65 46 72  r *sqlite3NameFr
409f0 6f 6d 54 6f 6b 65 6e 28 54 6f 6b 65 6e 2a 29 3b  omToken(Token*);
40a00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
40a10 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43  int sqlite3ExprC
40a20 6f 6d 70 61 72 65 28 45 78 70 72 2a 2c 20 45 78  ompare(Expr*, Ex
40a30 70 72 2a 29 3b 0a 69 6e 74 20 73 71 6c 69 74 65  pr*);.int sqlite
40a40 46 75 6e 63 49 64 28 54 6f 6b 65 6e 2a 29 3b 0a  FuncId(Token*);.
40a50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
40a60 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 52 65  nt sqlite3ExprRe
40a70 73 6f 6c 76 65 4e 61 6d 65 73 28 4e 61 6d 65 43  solveNames(NameC
40a80 6f 6e 74 65 78 74 20 2a 2c 20 45 78 70 72 20 2a  ontext *, Expr *
40a90 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
40aa0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
40ab0 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74  rAnalyzeAggregat
40ac0 65 73 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 2c  es(NameContext*,
40ad0 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   Expr*);.SQLITE_
40ae0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
40af0 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67  te3ExprAnalyzeAg
40b00 67 4c 69 73 74 28 4e 61 6d 65 43 6f 6e 74 65 78  gList(NameContex
40b10 74 2a 2c 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53  t*,ExprList*);.S
40b20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64  QLITE_PRIVATE Vd
40b30 62 65 20 2a 73 71 6c 69 74 65 33 47 65 74 56 64  be *sqlite3GetVd
40b40 62 65 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49  be(Parse*);.SQLI
40b50 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20  TE_PRIVATE Expr 
40b60 2a 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 64  *sqlite3CreateId
40b70 45 78 70 72 28 63 6f 6e 73 74 20 63 68 61 72 2a  Expr(const char*
40b80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
40b90 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 61  E void sqlite3Ra
40ba0 6e 64 6f 6d 6e 65 73 73 28 69 6e 74 2c 20 76 6f  ndomness(int, vo
40bb0 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  id*);.SQLITE_PRI
40bc0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
40bd0 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 73 71 6c  3RollbackAll(sql
40be0 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ite3*);.SQLITE_P
40bf0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
40c00 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 68  te3CodeVerifySch
40c10 65 6d 61 28 50 61 72 73 65 2a 2c 20 69 6e 74 29  ema(Parse*, int)
40c20 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
40c30 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67   void sqlite3Beg
40c40 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 28 50 61  inTransaction(Pa
40c50 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  rse*, int);.SQLI
40c60 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
40c70 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 54 72 61  sqlite3CommitTra
40c80 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65 2a 29  nsaction(Parse*)
40c90 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
40ca0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 6c   void sqlite3Rol
40cb0 6c 62 61 63 6b 54 72 61 6e 73 61 63 74 69 6f 6e  lbackTransaction
40cc0 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45  (Parse*);.SQLITE
40cd0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
40ce0 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61  ite3ExprIsConsta
40cf0 6e 74 28 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54  nt(Expr*);.SQLIT
40d00 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
40d10 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74  lite3ExprIsConst
40d20 61 6e 74 4e 6f 74 4a 6f 69 6e 28 45 78 70 72 2a  antNotJoin(Expr*
40d30 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
40d40 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
40d50 72 49 73 43 6f 6e 73 74 61 6e 74 4f 72 46 75 6e  rIsConstantOrFun
40d60 63 74 69 6f 6e 28 45 78 70 72 2a 29 3b 0a 53 51  ction(Expr*);.SQ
40d70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
40d80 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e   sqlite3ExprIsIn
40d90 74 65 67 65 72 28 45 78 70 72 2a 2c 20 69 6e 74  teger(Expr*, int
40da0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
40db0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73  TE int sqlite3Is
40dc0 52 6f 77 69 64 28 63 6f 6e 73 74 20 63 68 61 72  Rowid(const char
40dd0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
40de0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47  TE void sqlite3G
40df0 65 6e 65 72 61 74 65 52 6f 77 44 65 6c 65 74 65  enerateRowDelete
40e00 28 73 71 6c 69 74 65 33 2a 2c 20 56 64 62 65 2a  (sqlite3*, Vdbe*
40e10 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69  , Table*, int, i
40e20 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
40e30 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
40e40 47 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65 78  GenerateRowIndex
40e50 44 65 6c 65 74 65 28 56 64 62 65 2a 2c 20 54 61  Delete(Vdbe*, Ta
40e60 62 6c 65 2a 2c 20 69 6e 74 2c 20 63 68 61 72 2a  ble*, int, char*
40e70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
40e80 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47 65  E void sqlite3Ge
40e90 6e 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28 56  nerateIndexKey(V
40ea0 64 62 65 2a 2c 20 49 6e 64 65 78 2a 2c 20 69 6e  dbe*, Index*, in
40eb0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
40ec0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47  TE void sqlite3G
40ed0 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e  enerateConstrain
40ee0 74 43 68 65 63 6b 73 28 50 61 72 73 65 2a 2c 54  tChecks(Parse*,T
40ef0 61 62 6c 65 2a 2c 69 6e 74 2c 63 68 61 72 2a 2c  able*,int,char*,
40f00 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29  int,int,int,int)
40f10 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
40f20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d   void sqlite3Com
40f30 70 6c 65 74 65 49 6e 73 65 72 74 69 6f 6e 28 50  pleteInsertion(P
40f40 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69  arse*, Table*, i
40f50 6e 74 2c 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20  nt, char*, int, 
40f60 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  int, int, int);.
40f70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
40f80 6f 69 64 20 73 71 6c 69 74 65 33 4f 70 65 6e 54  oid sqlite3OpenT
40f90 61 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 28 50  ableAndIndices(P
40fa0 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69  arse*, Table*, i
40fb0 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
40fc0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
40fd0 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f  lite3BeginWriteO
40fe0 70 65 72 61 74 69 6f 6e 28 50 61 72 73 65 2a 2c  peration(Parse*,
40ff0 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49   int, int);.SQLI
41000 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20  TE_PRIVATE Expr 
41010 2a 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28  *sqlite3ExprDup(
41020 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Expr*);.SQLITE_P
41030 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
41040 74 65 33 54 6f 6b 65 6e 43 6f 70 79 28 54 6f 6b  te3TokenCopy(Tok
41050 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51  en*, Token*);.SQ
41060 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70  LITE_PRIVATE Exp
41070 72 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 45 78  rList *sqlite3Ex
41080 70 72 4c 69 73 74 44 75 70 28 45 78 70 72 4c 69  prListDup(ExprLi
41090 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  st*);.SQLITE_PRI
410a0 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71  VATE SrcList *sq
410b0 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75 70 28  lite3SrcListDup(
410c0 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54  SrcList*);.SQLIT
410d0 45 5f 50 52 49 56 41 54 45 20 49 64 4c 69 73 74  E_PRIVATE IdList
410e0 20 2a 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44   *sqlite3IdListD
410f0 75 70 28 49 64 4c 69 73 74 2a 29 3b 0a 53 51 4c  up(IdList*);.SQL
41100 49 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65  ITE_PRIVATE Sele
41110 63 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63  ct *sqlite3Selec
41120 74 44 75 70 28 53 65 6c 65 63 74 2a 29 3b 0a 53  tDup(Select*);.S
41130 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 46 75  QLITE_PRIVATE Fu
41140 6e 63 44 65 66 20 2a 73 71 6c 69 74 65 33 46 69  ncDef *sqlite3Fi
41150 6e 64 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74  ndFunction(sqlit
41160 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  e3*,const char*,
41170 69 6e 74 2c 69 6e 74 2c 75 38 2c 69 6e 74 29 3b  int,int,u8,int);
41180 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
41190 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 69  void sqlite3Regi
411a0 73 74 65 72 42 75 69 6c 74 69 6e 46 75 6e 63 74  sterBuiltinFunct
411b0 69 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a  ions(sqlite3*);.
411c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
411d0 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73  oid sqlite3Regis
411e0 74 65 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74  terDateTimeFunct
411f0 69 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a  ions(sqlite3*);.
41200 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
41210 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  nt sqlite3Safety
41220 4f 6e 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51  On(sqlite3*);.SQ
41230 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
41240 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
41250 66 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c  f(sqlite3*);.SQL
41260 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
41270 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65  sqlite3SafetyChe
41280 63 6b 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51  ck(sqlite3*);.SQ
41290 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
412a0 64 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43  d sqlite3ChangeC
412b0 6f 6f 6b 69 65 28 73 71 6c 69 74 65 33 2a 2c 20  ookie(sqlite3*, 
412c0 56 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 0a 23 69  Vdbe*, int);..#i
412d0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
412e0 54 5f 54 52 49 47 47 45 52 0a 53 51 4c 49 54 45  T_TRIGGER.SQLITE
412f0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
41300 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72 69 67  sqlite3BeginTrig
41310 67 65 72 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65  ger(Parse*, Toke
41320 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 2c 69 6e  n*,Token*,int,in
41330 74 2c 49 64 4c 69 73 74 2a 2c 53 72 63 4c 69 73  t,IdList*,SrcLis
41340 74 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  t*,.            
41350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45                 E
41360 78 70 72 2a 2c 69 6e 74 2c 20 69 6e 74 29 3b 0a  xpr*,int, int);.
41370 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
41380 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 69 6e   void sqlite3Fin
41390 69 73 68 54 72 69 67 67 65 72 28 50 61 72 73 65  ishTrigger(Parse
413a0 2a 2c 20 54 72 69 67 67 65 72 53 74 65 70 2a 2c  *, TriggerStep*,
413b0 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45   Token*);.SQLITE
413c0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
413d0 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67 67  sqlite3DropTrigg
413e0 65 72 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69  er(Parse*, SrcLi
413f0 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  st*, int);.SQLIT
41400 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
41410 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67   sqlite3DropTrig
41420 67 65 72 50 74 72 28 50 61 72 73 65 2a 2c 20 54  gerPtr(Parse*, T
41430 72 69 67 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45  rigger*);.SQLITE
41440 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73  _PRIVATE   int s
41450 71 6c 69 74 65 33 54 72 69 67 67 65 72 73 45 78  qlite3TriggersEx
41460 69 73 74 28 50 61 72 73 65 2a 2c 20 54 61 62 6c  ist(Parse*, Tabl
41470 65 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69 73  e*, int, ExprLis
41480 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
41490 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65  ATE   int sqlite
414a0 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28  3CodeRowTrigger(
414b0 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 45 78 70  Parse*, int, Exp
414c0 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 54 61 62  rList*, int, Tab
414d0 6c 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20  le *, int, int, 
414e0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
414f0 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 2c              int,
41500 20 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 73 71   int);.  void sq
41510 6c 69 74 65 56 69 65 77 54 72 69 67 67 65 72 73  liteViewTriggers
41520 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c  (Parse*, Table*,
41530 20 45 78 70 72 2a 2c 20 69 6e 74 2c 20 45 78 70   Expr*, int, Exp
41540 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  rList*);.SQLITE_
41550 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
41560 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67  qlite3DeleteTrig
41570 67 65 72 53 74 65 70 28 54 72 69 67 67 65 72 53  gerStep(TriggerS
41580 74 65 70 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  tep*);.SQLITE_PR
41590 49 56 41 54 45 20 20 20 54 72 69 67 67 65 72 53  IVATE   TriggerS
415a0 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72 69 67  tep *sqlite3Trig
415b0 67 65 72 53 65 6c 65 63 74 53 74 65 70 28 53 65  gerSelectStep(Se
415c0 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  lect*);.SQLITE_P
415d0 52 49 56 41 54 45 20 20 20 54 72 69 67 67 65 72  RIVATE   Trigger
415e0 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72 69  Step *sqlite3Tri
415f0 67 67 65 72 49 6e 73 65 72 74 53 74 65 70 28 54  ggerInsertStep(T
41600 6f 6b 65 6e 2a 2c 20 49 64 4c 69 73 74 2a 2c 20  oken*, IdList*, 
41610 45 78 70 72 4c 69 73 74 2a 2c 53 65 6c 65 63 74  ExprList*,Select
41620 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  *,int);.SQLITE_P
41630 52 49 56 41 54 45 20 20 20 54 72 69 67 67 65 72  RIVATE   Trigger
41640 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72 69  Step *sqlite3Tri
41650 67 67 65 72 55 70 64 61 74 65 53 74 65 70 28 54  ggerUpdateStep(T
41660 6f 6b 65 6e 2a 2c 20 45 78 70 72 4c 69 73 74 2a  oken*, ExprList*
41670 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a 53  , Expr*, int);.S
41680 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
41690 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c  TriggerStep *sql
416a0 69 74 65 33 54 72 69 67 67 65 72 44 65 6c 65 74  ite3TriggerDelet
416b0 65 53 74 65 70 28 54 6f 6b 65 6e 2a 2c 20 45 78  eStep(Token*, Ex
416c0 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  pr*);.SQLITE_PRI
416d0 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
416e0 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 72  te3DeleteTrigger
416f0 28 54 72 69 67 67 65 72 2a 29 3b 0a 53 51 4c 49  (Trigger*);.SQLI
41700 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
41710 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41  d sqlite3UnlinkA
41720 6e 64 44 65 6c 65 74 65 54 72 69 67 67 65 72 28  ndDeleteTrigger(
41730 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e  sqlite3*,int,con
41740 73 74 20 63 68 61 72 2a 29 3b 0a 23 65 6c 73 65  st char*);.#else
41750 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
41760 33 54 72 69 67 67 65 72 73 45 78 69 73 74 28 41  3TriggersExist(A
41770 2c 42 2c 43 2c 44 2c 45 2c 46 29 20 30 0a 23 20  ,B,C,D,E,F) 0.# 
41780 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 44 65  define sqlite3De
41790 6c 65 74 65 54 72 69 67 67 65 72 28 41 29 0a 23  leteTrigger(A).#
417a0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 44   define sqlite3D
417b0 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 41 2c  ropTriggerPtr(A,
417c0 42 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  B).# define sqli
417d0 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65  te3UnlinkAndDele
417e0 74 65 54 72 69 67 67 65 72 28 41 2c 42 2c 43 29  teTrigger(A,B,C)
417f0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
41800 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28  3CodeRowTrigger(
41810 41 2c 42 2c 43 2c 44 2c 45 2c 46 2c 47 2c 48 2c  A,B,C,D,E,F,G,H,
41820 49 29 20 30 0a 23 65 6e 64 69 66 0a 0a 53 51 4c  I) 0.#endif..SQL
41830 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
41840 73 71 6c 69 74 65 33 4a 6f 69 6e 54 79 70 65 28  sqlite3JoinType(
41850 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 20  Parse*, Token*, 
41860 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b  Token*, Token*);
41870 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
41880 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65 61  void sqlite3Crea
41890 74 65 46 6f 72 65 69 67 6e 4b 65 79 28 50 61 72  teForeignKey(Par
418a0 73 65 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20  se*, ExprList*, 
418b0 54 6f 6b 65 6e 2a 2c 20 45 78 70 72 4c 69 73 74  Token*, ExprList
418c0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
418d0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
418e0 69 74 65 33 44 65 66 65 72 46 6f 72 65 69 67 6e  ite3DeferForeign
418f0 4b 65 79 28 50 61 72 73 65 2a 2c 20 69 6e 74 29  Key(Parse*, int)
41900 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
41910 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
41920 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ION.SQLITE_PRIVA
41930 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
41940 33 41 75 74 68 52 65 61 64 28 50 61 72 73 65 2a  3AuthRead(Parse*
41950 2c 45 78 70 72 2a 2c 53 72 63 4c 69 73 74 2a 29  ,Expr*,SrcList*)
41960 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
41970 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 41 75     int sqlite3Au
41980 74 68 43 68 65 63 6b 28 50 61 72 73 65 2a 2c 69  thCheck(Parse*,i
41990 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c  nt, const char*,
419a0 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f   const char*, co
419b0 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49  nst char*);.SQLI
419c0 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
419d0 64 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e  d sqlite3AuthCon
419e0 74 65 78 74 50 75 73 68 28 50 61 72 73 65 2a 2c  textPush(Parse*,
419f0 20 41 75 74 68 43 6f 6e 74 65 78 74 2a 2c 20 63   AuthContext*, c
41a00 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c  onst char*);.SQL
41a10 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
41a20 69 64 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f  id sqlite3AuthCo
41a30 6e 74 65 78 74 50 6f 70 28 41 75 74 68 43 6f 6e  ntextPop(AuthCon
41a40 74 65 78 74 2a 29 3b 0a 23 65 6c 73 65 0a 23 20  text*);.#else.# 
41a50 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75  define sqlite3Au
41a60 74 68 52 65 61 64 28 61 2c 62 2c 63 29 0a 23 20  thRead(a,b,c).# 
41a70 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75  define sqlite3Au
41a80 74 68 43 68 65 63 6b 28 61 2c 62 2c 63 2c 64 2c  thCheck(a,b,c,d,
41a90 65 29 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 0a  e)    SQLITE_OK.
41aa0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
41ab0 41 75 74 68 43 6f 6e 74 65 78 74 50 75 73 68 28  AuthContextPush(
41ac0 61 2c 62 2c 63 29 0a 23 20 64 65 66 69 6e 65 20  a,b,c).# define 
41ad0 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65  sqlite3AuthConte
41ae0 78 74 50 6f 70 28 61 29 20 20 28 28 76 6f 69 64  xtPop(a)  ((void
41af0 29 28 61 29 29 0a 23 65 6e 64 69 66 0a 53 51 4c  )(a)).#endif.SQL
41b00 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
41b10 20 73 71 6c 69 74 65 33 41 74 74 61 63 68 28 50   sqlite3Attach(P
41b20 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 45 78  arse*, Expr*, Ex
41b30 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c  pr*, Expr*);.SQL
41b40 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
41b50 20 73 71 6c 69 74 65 33 44 65 74 61 63 68 28 50   sqlite3Detach(P
41b60 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53  arse*, Expr*);.S
41b70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
41b80 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 61  t sqlite3BtreeFa
41b90 63 74 6f 72 79 28 63 6f 6e 73 74 20 73 71 6c 69  ctory(const sqli
41ba0 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63  te3 *db, const c
41bb0 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 0a  har *zFilename,.
41bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41bd0 20 20 20 20 20 20 20 69 6e 74 20 6f 6d 69 74 4a         int omitJ
41be0 6f 75 72 6e 61 6c 2c 20 69 6e 74 20 6e 43 61 63  ournal, int nCac
41bf0 68 65 2c 20 42 74 72 65 65 20 2a 2a 70 70 42 74  he, Btree **ppBt
41c00 72 65 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ree);.SQLITE_PRI
41c10 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
41c20 46 69 78 49 6e 69 74 28 44 62 46 69 78 65 72 2a  FixInit(DbFixer*
41c30 2c 20 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 63  , Parse*, int, c
41c40 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73  onst char*, cons
41c50 74 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54  t Token*);.SQLIT
41c60 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
41c70 6c 69 74 65 33 46 69 78 53 72 63 4c 69 73 74 28  lite3FixSrcList(
41c80 44 62 46 69 78 65 72 2a 2c 20 53 72 63 4c 69 73  DbFixer*, SrcLis
41c90 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
41ca0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
41cb0 69 78 53 65 6c 65 63 74 28 44 62 46 69 78 65 72  ixSelect(DbFixer
41cc0 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c  *, Select*);.SQL
41cd0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
41ce0 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 44  sqlite3FixExpr(D
41cf0 62 46 69 78 65 72 2a 2c 20 45 78 70 72 2a 29 3b  bFixer*, Expr*);
41d00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
41d10 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 45 78  int sqlite3FixEx
41d20 70 72 4c 69 73 74 28 44 62 46 69 78 65 72 2a 2c  prList(DbFixer*,
41d30 20 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c   ExprList*);.SQL
41d40 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
41d50 73 71 6c 69 74 65 33 46 69 78 54 72 69 67 67 65  sqlite3FixTrigge
41d60 72 53 74 65 70 28 44 62 46 69 78 65 72 2a 2c 20  rStep(DbFixer*, 
41d70 54 72 69 67 67 65 72 53 74 65 70 2a 29 3b 0a 53  TriggerStep*);.S
41d80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
41d90 74 20 73 71 6c 69 74 65 33 41 74 6f 46 28 63 6f  t sqlite3AtoF(co
41da0 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 64 6f 75  nst char *z, dou
41db0 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  ble*);.SQLITE_AP
41dc0 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  I char *sqlite3_
41dd0 73 6e 70 72 69 6e 74 66 28 69 6e 74 2c 63 68 61  snprintf(int,cha
41de0 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e  r*,const char*,.
41df0 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..);.SQLITE_PRIV
41e00 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47  ATE int sqlite3G
41e10 65 74 49 6e 74 33 32 28 63 6f 6e 73 74 20 63 68  etInt32(const ch
41e20 61 72 20 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c  ar *, int*);.SQL
41e30 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
41e40 73 71 6c 69 74 65 33 46 69 74 73 49 6e 36 34 42  sqlite3FitsIn64B
41e50 69 74 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  its(const char *
41e60 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
41e70 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66  E int sqlite3Utf
41e80 31 36 42 79 74 65 4c 65 6e 28 63 6f 6e 73 74 20  16ByteLen(const 
41e90 76 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74  void *pData, int
41ea0 20 6e 43 68 61 72 29 3b 0a 53 51 4c 49 54 45 5f   nChar);.SQLITE_
41eb0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
41ec0 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 63  te3Utf8CharLen(c
41ed0 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61  onst char *pData
41ee0 2c 20 69 6e 74 20 6e 42 79 74 65 29 3b 0a 53 51  , int nByte);.SQ
41ef0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
41f00 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64   sqlite3Utf8Read
41f10 28 63 6f 6e 73 74 20 75 38 2a 2c 20 63 6f 6e 73  (const u8*, cons
41f20 74 20 75 38 2a 2c 20 63 6f 6e 73 74 20 75 38 2a  t u8*, const u8*
41f30 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
41f40 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 75  TE int sqlite3Pu
41f50 74 56 61 72 69 6e 74 28 75 6e 73 69 67 6e 65 64  tVarint(unsigned
41f60 20 63 68 61 72 20 2a 2c 20 75 36 34 29 3b 0a 53   char *, u64);.S
41f70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
41f80 74 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69  t sqlite3GetVari
41f90 6e 74 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  nt(const unsigne
41fa0 64 20 63 68 61 72 20 2a 2c 20 75 36 34 20 2a 29  d char *, u64 *)
41fb0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
41fc0 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 56   int sqlite3GetV
41fd0 61 72 69 6e 74 33 32 28 63 6f 6e 73 74 20 75 6e  arint32(const un
41fe0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2c 20 75  signed char *, u
41ff0 33 32 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  32 *);.SQLITE_PR
42000 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
42010 33 56 61 72 69 6e 74 4c 65 6e 28 75 36 34 20 76  3VarintLen(u64 v
42020 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
42030 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 6e  E void sqlite3In
42040 64 65 78 41 66 66 69 6e 69 74 79 53 74 72 28 56  dexAffinityStr(V
42050 64 62 65 20 2a 2c 20 49 6e 64 65 78 20 2a 29 3b  dbe *, Index *);
42060 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
42070 76 6f 69 64 20 73 71 6c 69 74 65 33 54 61 62 6c  void sqlite3Tabl
42080 65 41 66 66 69 6e 69 74 79 53 74 72 28 56 64 62  eAffinityStr(Vdb
42090 65 20 2a 2c 20 54 61 62 6c 65 20 2a 29 3b 0a 53  e *, Table *);.S
420a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
420b0 61 72 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72  ar sqlite3Compar
420c0 65 41 66 66 69 6e 69 74 79 28 45 78 70 72 20 2a  eAffinity(Expr *
420d0 70 45 78 70 72 2c 20 63 68 61 72 20 61 66 66 32  pExpr, char aff2
420e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
420f0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 64  E int sqlite3Ind
42100 65 78 41 66 66 69 6e 69 74 79 4f 6b 28 45 78 70  exAffinityOk(Exp
42110 72 20 2a 70 45 78 70 72 2c 20 63 68 61 72 20 69  r *pExpr, char i
42120 64 78 5f 61 66 66 69 6e 69 74 79 29 3b 0a 53 51  dx_affinity);.SQ
42130 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
42140 72 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66  r sqlite3ExprAff
42150 69 6e 69 74 79 28 45 78 70 72 20 2a 70 45 78 70  inity(Expr *pExp
42160 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  r);.SQLITE_PRIVA
42170 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74  TE int sqlite3At
42180 6f 69 36 34 28 63 6f 6e 73 74 20 63 68 61 72 2a  oi64(const char*
42190 2c 20 69 36 34 2a 29 3b 0a 53 51 4c 49 54 45 5f  , i64*);.SQLITE_
421a0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
421b0 69 74 65 33 45 72 72 6f 72 28 73 71 6c 69 74 65  ite3Error(sqlite
421c0 33 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63  3*, int, const c
421d0 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 53 51 4c 49 54  har*,...);.SQLIT
421e0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
421f0 73 71 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f 62  sqlite3HexToBlob
42200 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 3b  (const char *z);
42210 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
42220 69 6e 74 20 73 71 6c 69 74 65 33 54 77 6f 50 61  int sqlite3TwoPa
42230 72 74 4e 61 6d 65 28 50 61 72 73 65 20 2a 2c 20  rtName(Parse *, 
42240 54 6f 6b 65 6e 20 2a 2c 20 54 6f 6b 65 6e 20 2a  Token *, Token *
42250 2c 20 54 6f 6b 65 6e 20 2a 2a 29 3b 0a 53 51 4c  , Token **);.SQL
42260 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
42270 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 45  t char *sqlite3E
42280 72 72 53 74 72 28 69 6e 74 29 3b 0a 53 51 4c 49  rrStr(int);.SQLI
42290 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
422a0 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61  qlite3ReadSchema
422b0 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 3b  (Parse *pParse);
422c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
422d0 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33  CollSeq *sqlite3
422e0 46 69 6e 64 43 6f 6c 6c 53 65 71 28 73 71 6c 69  FindCollSeq(sqli
422f0 74 65 33 2a 2c 75 38 20 65 6e 63 2c 20 63 6f 6e  te3*,u8 enc, con
42300 73 74 20 63 68 61 72 20 2a 2c 69 6e 74 2c 69 6e  st char *,int,in
42310 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
42320 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69  TE CollSeq *sqli
42330 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71  te3LocateCollSeq
42340 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
42350 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
42360 65 2c 20 69 6e 74 20 6e 4e 61 6d 65 29 3b 0a 53  e, int nName);.S
42370 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f  QLITE_PRIVATE Co
42380 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 45 78  llSeq *sqlite3Ex
42390 70 72 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20  prCollSeq(Parse 
423a0 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70  *pParse, Expr *p
423b0 45 78 70 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Expr);.SQLITE_PR
423c0 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69  IVATE Expr *sqli
423d0 74 65 33 45 78 70 72 53 65 74 43 6f 6c 6c 28 50  te3ExprSetColl(P
423e0 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78  arse *pParse, Ex
423f0 70 72 20 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a  pr *, Token *);.
42400 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
42410 6e 74 20 73 71 6c 69 74 65 33 43 68 65 63 6b 43  nt sqlite3CheckC
42420 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 2c 20  ollSeq(Parse *, 
42430 43 6f 6c 6c 53 65 71 20 2a 29 3b 0a 53 51 4c 49  CollSeq *);.SQLI
42440 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
42450 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63  qlite3CheckObjec
42460 74 4e 61 6d 65 28 50 61 72 73 65 20 2a 2c 20 63  tName(Parse *, c
42470 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51  onst char *);.SQ
42480 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
42490 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74  d sqlite3VdbeSet
424a0 43 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 20  Changes(sqlite3 
424b0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
424c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
424d0 69 74 65 33 55 74 66 31 36 53 75 62 73 74 72 28  ite3Utf16Substr(
424e0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
424f0 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61  *,int,sqlite3_va
42500 6c 75 65 20 2a 2a 29 3b 0a 0a 53 51 4c 49 54 45  lue **);..SQLITE
42510 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76  _PRIVATE const v
42520 6f 69 64 20 2a 73 71 6c 69 74 65 33 56 61 6c 75  oid *sqlite3Valu
42530 65 54 65 78 74 28 73 71 6c 69 74 65 33 5f 76 61  eText(sqlite3_va
42540 6c 75 65 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54  lue*, u8);.SQLIT
42550 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
42560 6c 69 74 65 33 56 61 6c 75 65 42 79 74 65 73 28  lite3ValueBytes(
42570 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c 20  sqlite3_value*, 
42580 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  u8);.SQLITE_PRIV
42590 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
425a0 56 61 6c 75 65 53 65 74 53 74 72 28 73 71 6c 69  ValueSetStr(sqli
425b0 74 65 33 5f 76 61 6c 75 65 2a 2c 20 69 6e 74 2c  te3_value*, int,
425c0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2c 75 38   const void *,u8
425d0 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29  , void(*)(void*)
425e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
425f0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61  E void sqlite3Va
42600 6c 75 65 46 72 65 65 28 73 71 6c 69 74 65 33 5f  lueFree(sqlite3_
42610 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  value*);.SQLITE_
42620 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f  PRIVATE sqlite3_
42630 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 56 61  value *sqlite3Va
42640 6c 75 65 4e 65 77 28 76 6f 69 64 29 3b 0a 53 51  lueNew(void);.SQ
42650 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
42660 72 20 2a 73 71 6c 69 74 65 33 55 74 66 31 36 74  r *sqlite3Utf16t
42670 6f 38 28 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20  o8(const void*, 
42680 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
42690 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
426a0 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 45 78  ValueFromExpr(Ex
426b0 70 72 20 2a 2c 20 75 38 2c 20 75 38 2c 20 73 71  pr *, u8, u8, sq
426c0 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 3b  lite3_value **);
426d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
426e0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75  void sqlite3Valu
426f0 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 73  eApplyAffinity(s
42700 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2c 20  qlite3_value *, 
42710 75 38 2c 20 75 38 29 3b 0a 65 78 74 65 72 6e 20  u8, u8);.extern 
42720 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
42730 68 61 72 20 73 71 6c 69 74 65 33 55 70 70 65 72  har sqlite3Upper
42740 54 6f 4c 6f 77 65 72 5b 5d 3b 0a 53 51 4c 49 54  ToLower[];.SQLIT
42750 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
42760 71 6c 69 74 65 33 52 6f 6f 74 50 61 67 65 4d 6f  qlite3RootPageMo
42770 76 65 64 28 44 62 2a 2c 20 69 6e 74 2c 20 69 6e  ved(Db*, int, in
42780 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
42790 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52  TE void sqlite3R
427a0 65 69 6e 64 65 78 28 50 61 72 73 65 2a 2c 20 54  eindex(Parse*, T
427b0 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a  oken*, Token*);.
427c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
427d0 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72  oid sqlite3Alter
427e0 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65  Functions(sqlite
427f0 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  3*);.SQLITE_PRIV
42800 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
42810 41 6c 74 65 72 52 65 6e 61 6d 65 54 61 62 6c 65  AlterRenameTable
42820 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74  (Parse*, SrcList
42830 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  *, Token*);.SQLI
42840 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
42850 71 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e 28 63  qlite3GetToken(c
42860 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
42870 61 72 20 2a 2c 20 69 6e 74 20 2a 29 3b 0a 53 51  ar *, int *);.SQ
42880 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
42890 64 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50  d sqlite3NestedP
428a0 61 72 73 65 28 50 61 72 73 65 2a 2c 20 63 6f 6e  arse(Parse*, con
428b0 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a  st char*, ...);.
428c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
428d0 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 69 72  oid sqlite3Expir
428e0 65 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65  ePreparedStateme
428f0 6e 74 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53  nts(sqlite3*);.S
42900 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
42910 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 53 75  id sqlite3CodeSu
42920 62 73 65 6c 65 63 74 28 50 61 72 73 65 20 2a 2c  bselect(Parse *,
42930 20 45 78 70 72 20 2a 29 3b 0a 53 51 4c 49 54 45   Expr *);.SQLITE
42940 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
42950 69 74 65 33 53 65 6c 65 63 74 52 65 73 6f 6c 76  ite3SelectResolv
42960 65 28 50 61 72 73 65 20 2a 2c 20 53 65 6c 65 63  e(Parse *, Selec
42970 74 20 2a 2c 20 4e 61 6d 65 43 6f 6e 74 65 78 74  t *, NameContext
42980 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
42990 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
429a0 43 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 56 64  ColumnDefault(Vd
429b0 62 65 20 2a 2c 20 54 61 62 6c 65 20 2a 2c 20 69  be *, Table *, i
429c0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
429d0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
429e0 41 6c 74 65 72 46 69 6e 69 73 68 41 64 64 43 6f  AlterFinishAddCo
429f0 6c 75 6d 6e 28 50 61 72 73 65 20 2a 2c 20 54 6f  lumn(Parse *, To
42a00 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ken *);.SQLITE_P
42a10 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
42a20 74 65 33 41 6c 74 65 72 42 65 67 69 6e 41 64 64  te3AlterBeginAdd
42a30 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a 2c 20  Column(Parse *, 
42a40 53 72 63 4c 69 73 74 20 2a 29 3b 0a 53 51 4c 49  SrcList *);.SQLI
42a50 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53  TE_PRIVATE CollS
42a60 65 71 20 2a 73 71 6c 69 74 65 33 47 65 74 43 6f  eq *sqlite3GetCo
42a70 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 2a 2c 20  llSeq(sqlite3*, 
42a80 43 6f 6c 6c 53 65 71 20 2a 2c 20 63 6f 6e 73 74  CollSeq *, const
42a90 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 53   char *, int);.S
42aa0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
42ab0 61 72 20 73 71 6c 69 74 65 33 41 66 66 69 6e 69  ar sqlite3Affini
42ac0 74 79 54 79 70 65 28 63 6f 6e 73 74 20 54 6f 6b  tyType(const Tok
42ad0 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  en*);.SQLITE_PRI
42ae0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
42af0 33 41 6e 61 6c 79 7a 65 28 50 61 72 73 65 2a 2c  3Analyze(Parse*,
42b00 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29   Token*, Token*)
42b10 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
42b20 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 76 6f   int sqlite3Invo
42b30 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 28 42 75  keBusyHandler(Bu
42b40 73 79 48 61 6e 64 6c 65 72 2a 29 3b 0a 53 51 4c  syHandler*);.SQL
42b50 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
42b60 73 71 6c 69 74 65 33 46 69 6e 64 44 62 28 73 71  sqlite3FindDb(sq
42b70 6c 69 74 65 33 2a 2c 20 54 6f 6b 65 6e 2a 29 3b  lite3*, Token*);
42b80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
42b90 69 6e 74 20 73 71 6c 69 74 65 33 41 6e 61 6c 79  int sqlite3Analy
42ba0 73 69 73 4c 6f 61 64 28 73 71 6c 69 74 65 33 2a  sisLoad(sqlite3*
42bb0 2c 69 6e 74 20 69 44 42 29 3b 0a 53 51 4c 49 54  ,int iDB);.SQLIT
42bc0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
42bd0 71 6c 69 74 65 33 44 65 66 61 75 6c 74 52 6f 77  qlite3DefaultRow
42be0 45 73 74 28 49 6e 64 65 78 2a 29 3b 0a 53 51 4c  Est(Index*);.SQL
42bf0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
42c00 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72   sqlite3Register
42c10 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 73 28 73 71  LikeFunctions(sq
42c20 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51  lite3*, int);.SQ
42c30 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
42c40 20 73 71 6c 69 74 65 33 49 73 4c 69 6b 65 46 75   sqlite3IsLikeFu
42c50 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 2a 2c  nction(sqlite3*,
42c60 45 78 70 72 2a 2c 69 6e 74 2a 2c 63 68 61 72 2a  Expr*,int*,char*
42c70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
42c80 45 20 54 68 72 65 61 64 44 61 74 61 20 2a 73 71  E ThreadData *sq
42c90 6c 69 74 65 33 54 68 72 65 61 64 44 61 74 61 28  lite3ThreadData(
42ca0 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52  void);.SQLITE_PR
42cb0 49 56 41 54 45 20 63 6f 6e 73 74 20 54 68 72 65  IVATE const Thre
42cc0 61 64 44 61 74 61 20 2a 73 71 6c 69 74 65 33 54  adData *sqlite3T
42cd0 68 72 65 61 64 44 61 74 61 52 65 61 64 4f 6e 6c  hreadDataReadOnl
42ce0 79 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f  y(void);.SQLITE_
42cf0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
42d00 69 74 65 33 52 65 6c 65 61 73 65 54 68 72 65 61  ite3ReleaseThrea
42d10 64 44 61 74 61 28 76 6f 69 64 29 3b 0a 53 51 4c  dData(void);.SQL
42d20 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
42d30 20 73 71 6c 69 74 65 33 41 74 74 61 63 68 46 75   sqlite3AttachFu
42d40 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 20  nctions(sqlite3 
42d50 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
42d60 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d  TE void sqlite3M
42d70 69 6e 69 6d 75 6d 46 69 6c 65 46 6f 72 6d 61 74  inimumFileFormat
42d80 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e  (Parse*, int, in
42d90 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
42da0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
42db0 63 68 65 6d 61 46 72 65 65 28 76 6f 69 64 20 2a  chemaFree(void *
42dc0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
42dd0 45 20 53 63 68 65 6d 61 20 2a 73 71 6c 69 74 65  E Schema *sqlite
42de0 33 53 63 68 65 6d 61 47 65 74 28 42 74 72 65 65  3SchemaGet(Btree
42df0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
42e00 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53  ATE int sqlite3S
42e10 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 73 71 6c  chemaToIndex(sql
42e20 69 74 65 33 20 2a 64 62 2c 20 53 63 68 65 6d 61  ite3 *db, Schema
42e30 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
42e40 41 54 45 20 4b 65 79 49 6e 66 6f 20 2a 73 71 6c  ATE KeyInfo *sql
42e50 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f  ite3IndexKeyinfo
42e60 28 50 61 72 73 65 20 2a 2c 20 49 6e 64 65 78 20  (Parse *, Index 
42e70 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
42e80 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 72  TE int sqlite3Cr
42e90 65 61 74 65 46 75 6e 63 28 73 71 6c 69 74 65 33  eateFunc(sqlite3
42ea0 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   *, const char *
42eb0 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 76 6f 69 64  , int, int, void
42ec0 20 2a 2c 20 0a 20 20 76 6f 69 64 20 28 2a 29 28   *, .  void (*)(
42ed0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
42ee0 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
42ef0 75 65 20 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28  ue **),.  void (
42f00 2a 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  *)(sqlite3_conte
42f10 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  xt*,int,sqlite3_
42f20 76 61 6c 75 65 20 2a 2a 29 2c 20 76 6f 69 64 20  value **), void 
42f30 28 2a 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (*)(sqlite3_cont
42f40 65 78 74 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50  ext*));.SQLITE_P
42f50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
42f60 65 33 41 70 69 45 78 69 74 28 73 71 6c 69 74 65  e3ApiExit(sqlite
42f70 33 20 2a 64 62 2c 20 69 6e 74 29 3b 0a 53 51 4c  3 *db, int);.SQL
42f80 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
42f90 20 73 71 6c 69 74 65 33 46 61 69 6c 65 64 4d 61   sqlite3FailedMa
42fa0 6c 6c 6f 63 28 76 6f 69 64 29 3b 0a 53 51 4c 49  lloc(void);.SQLI
42fb0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
42fc0 73 71 6c 69 74 65 33 41 62 6f 72 74 4f 74 68 65  sqlite3AbortOthe
42fd0 72 41 63 74 69 76 65 56 64 62 65 73 28 73 71 6c  rActiveVdbes(sql
42fe0 69 74 65 33 20 2a 2c 20 56 64 62 65 20 2a 29 3b  ite3 *, Vdbe *);
42ff0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
43000 69 6e 74 20 73 71 6c 69 74 65 33 4f 70 65 6e 54  int sqlite3OpenT
43010 65 6d 70 44 61 74 61 62 61 73 65 28 50 61 72 73  empDatabase(Pars
43020 65 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  e *);../*.** The
43030 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68   interface to th
43040 65 20 4c 45 4d 4f 4e 2d 67 65 6e 65 72 61 74 65  e LEMON-generate
43050 64 20 70 61 72 73 65 72 0a 2a 2f 0a 53 51 4c 49  d parser.*/.SQLI
43060 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
43070 2a 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 6c  *sqlite3ParserAl
43080 6c 6f 63 28 76 6f 69 64 2a 28 2a 29 28 73 69 7a  loc(void*(*)(siz
43090 65 5f 74 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52  e_t));.SQLITE_PR
430a0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
430b0 65 33 50 61 72 73 65 72 46 72 65 65 28 76 6f 69  e3ParserFree(voi
430c0 64 2a 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64  d*, void(*)(void
430d0 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  *));.SQLITE_PRIV
430e0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
430f0 50 61 72 73 65 72 28 76 6f 69 64 2a 2c 20 69 6e  Parser(void*, in
43100 74 2c 20 54 6f 6b 65 6e 2c 20 50 61 72 73 65 2a  t, Token, Parse*
43110 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  );..#ifndef SQLI
43120 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54  TE_OMIT_LOAD_EXT
43130 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52  ENSION.SQLITE_PR
43140 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
43150 69 74 65 33 43 6c 6f 73 65 45 78 74 65 6e 73 69  ite3CloseExtensi
43160 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53  ons(sqlite3*);.S
43170 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
43180 69 6e 74 20 73 71 6c 69 74 65 33 41 75 74 6f 4c  int sqlite3AutoL
43190 6f 61 64 45 78 74 65 6e 73 69 6f 6e 73 28 73 71  oadExtensions(sq
431a0 6c 69 74 65 33 2a 29 3b 0a 23 65 6c 73 65 0a 23  lite3*);.#else.#
431b0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43   define sqlite3C
431c0 6c 6f 73 65 45 78 74 65 6e 73 69 6f 6e 73 28 58  loseExtensions(X
431d0 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  ).# define sqlit
431e0 65 33 41 75 74 6f 4c 6f 61 64 45 78 74 65 6e 73  e3AutoLoadExtens
431f0 69 6f 6e 73 28 58 29 20 20 53 51 4c 49 54 45 5f  ions(X)  SQLITE_
43200 4f 4b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64  OK.#endif..#ifnd
43210 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
43220 48 41 52 45 44 5f 43 41 43 48 45 0a 53 51 4c 49  HARED_CACHE.SQLI
43230 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
43240 64 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f  d sqlite3TableLo
43250 63 6b 28 50 61 72 73 65 20 2a 2c 20 69 6e 74 2c  ck(Parse *, int,
43260 20 69 6e 74 2c 20 75 38 2c 20 63 6f 6e 73 74 20   int, u8, const 
43270 63 68 61 72 20 2a 29 3b 0a 23 65 6c 73 65 0a 20  char *);.#else. 
43280 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33   #define sqlite3
43290 54 61 62 6c 65 4c 6f 63 6b 28 76 2c 77 2c 78 2c  TableLock(v,w,x,
432a0 79 2c 7a 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66  y,z).#endif..#if
432b0 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
432c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
432d0 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38   int sqlite3Utf8
432e0 54 6f 38 28 75 6e 73 69 67 6e 65 64 20 63 68 61  To8(unsigned cha
432f0 72 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66  r*);.#endif..#if
43300 64 65 66 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45  def SQLITE_MEMDE
43310 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  BUG.SQLITE_PRIVA
43320 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
43330 33 4d 61 6c 6c 6f 63 44 69 73 61 6c 6c 6f 77 28  3MallocDisallow(
43340 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52  void);.SQLITE_PR
43350 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
43360 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 6c 6f 77 28  ite3MallocAllow(
43370 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52  void);.SQLITE_PR
43380 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69  IVATE   int sqli
43390 74 65 33 54 65 73 74 4d 61 6c 6c 6f 63 46 61 69  te3TestMallocFai
433a0 6c 28 76 6f 69 64 29 3b 0a 23 65 6c 73 65 0a 20  l(void);.#else. 
433b0 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33   #define sqlite3
433c0 54 65 73 74 4d 61 6c 6c 6f 63 46 61 69 6c 28 29  TestMallocFail()
433d0 20 30 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c   0.  #define sql
433e0 69 74 65 33 4d 61 6c 6c 6f 63 44 69 73 61 6c 6c  ite3MallocDisall
433f0 6f 77 28 29 0a 20 20 23 64 65 66 69 6e 65 20 73  ow().  #define s
43400 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 6c 6f  qlite3MallocAllo
43410 77 28 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64  w().#endif..#ifd
43420 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
43430 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45  _MEMORY_MANAGEME
43440 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  NT.SQLITE_PRIVAT
43450 45 20 20 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  E   void *sqlite
43460 33 54 68 72 65 61 64 53 61 66 65 4d 61 6c 6c 6f  3ThreadSafeMallo
43470 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  c(int);.SQLITE_P
43480 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
43490 6c 69 74 65 33 54 68 72 65 61 64 53 61 66 65 46  lite3ThreadSafeF
434a0 72 65 65 28 76 6f 69 64 20 2a 29 3b 0a 23 65 6c  ree(void *);.#el
434b0 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c  se.  #define sql
434c0 69 74 65 33 54 68 72 65 61 64 53 61 66 65 4d 61  ite3ThreadSafeMa
434d0 6c 6c 6f 63 20 73 71 6c 69 74 65 33 4d 61 6c 6c  lloc sqlite3Mall
434e0 6f 63 58 0a 20 20 23 64 65 66 69 6e 65 20 73 71  ocX.  #define sq
434f0 6c 69 74 65 33 54 68 72 65 61 64 53 61 66 65 46  lite3ThreadSafeF
43500 72 65 65 20 73 71 6c 69 74 65 33 46 72 65 65 58  ree sqlite3FreeX
43510 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
43520 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
43530 55 41 4c 54 41 42 4c 45 0a 23 20 20 64 65 66 69  UALTABLE.#  defi
43540 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62 43 6c  ne sqlite3VtabCl
43550 65 61 72 28 58 29 0a 23 20 20 64 65 66 69 6e 65  ear(X).#  define
43560 20 73 71 6c 69 74 65 33 56 74 61 62 53 79 6e 63   sqlite3VtabSync
43570 28 58 2c 59 29 20 28 59 29 0a 23 20 20 64 65 66  (X,Y) (Y).#  def
43580 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62 52  ine sqlite3VtabR
43590 6f 6c 6c 62 61 63 6b 28 58 29 0a 23 20 20 64 65  ollback(X).#  de
435a0 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62  fine sqlite3Vtab
435b0 43 6f 6d 6d 69 74 28 58 29 0a 23 65 6c 73 65 0a  Commit(X).#else.
435c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
435d0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74    void sqlite3Vt
435e0 61 62 43 6c 65 61 72 28 54 61 62 6c 65 2a 29 3b  abClear(Table*);
435f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
43600 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74     int sqlite3Vt
43610 61 62 53 79 6e 63 28 73 71 6c 69 74 65 33 20 2a  abSync(sqlite3 *
43620 64 62 2c 20 69 6e 74 20 72 63 29 3b 0a 53 51 4c  db, int rc);.SQL
43630 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 69  ITE_PRIVATE    i
43640 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 52 6f  nt sqlite3VtabRo
43650 6c 6c 62 61 63 6b 28 73 71 6c 69 74 65 33 20 2a  llback(sqlite3 *
43660 64 62 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  db);.SQLITE_PRIV
43670 41 54 45 20 20 20 20 69 6e 74 20 73 71 6c 69 74  ATE    int sqlit
43680 65 33 56 74 61 62 43 6f 6d 6d 69 74 28 73 71 6c  e3VtabCommit(sql
43690 69 74 65 33 20 2a 64 62 29 3b 0a 23 65 6e 64 69  ite3 *db);.#endi
436a0 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  f.SQLITE_PRIVATE
436b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61   void sqlite3Vta
436c0 62 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 76 74  bLock(sqlite3_vt
436d0 61 62 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ab*);.SQLITE_PRI
436e0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
436f0 33 56 74 61 62 55 6e 6c 6f 63 6b 28 73 71 6c 69  3VtabUnlock(sqli
43700 74 65 33 2a 2c 20 73 71 6c 69 74 65 33 5f 76 74  te3*, sqlite3_vt
43710 61 62 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ab*);.SQLITE_PRI
43720 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
43730 33 56 74 61 62 42 65 67 69 6e 50 61 72 73 65 28  3VtabBeginParse(
43740 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 20  Parse*, Token*, 
43750 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b  Token*, Token*);
43760 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
43770 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62  void sqlite3Vtab
43780 46 69 6e 69 73 68 50 61 72 73 65 28 50 61 72 73  FinishParse(Pars
43790 65 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c  e*, Token*);.SQL
437a0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
437b0 20 73 71 6c 69 74 65 33 56 74 61 62 41 72 67 49   sqlite3VtabArgI
437c0 6e 69 74 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c  nit(Parse*);.SQL
437d0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
437e0 20 73 71 6c 69 74 65 33 56 74 61 62 41 72 67 45   sqlite3VtabArgE
437f0 78 74 65 6e 64 28 50 61 72 73 65 2a 2c 20 54 6f  xtend(Parse*, To
43800 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ken*);.SQLITE_PR
43810 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
43820 33 56 74 61 62 43 61 6c 6c 43 72 65 61 74 65 28  3VtabCallCreate(
43830 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 63  sqlite3*, int, c
43840 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 63 68 61  onst char *, cha
43850 72 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  r **);.SQLITE_PR
43860 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
43870 33 56 74 61 62 43 61 6c 6c 43 6f 6e 6e 65 63 74  3VtabCallConnect
43880 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 29  (Parse*, Table*)
43890 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
438a0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62   int sqlite3Vtab
438b0 43 61 6c 6c 44 65 73 74 72 6f 79 28 73 71 6c 69  CallDestroy(sqli
438c0 74 65 33 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74  te3*, int, const
438d0 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45   char *);.SQLITE
438e0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
438f0 69 74 65 33 56 74 61 62 42 65 67 69 6e 28 73 71  ite3VtabBegin(sq
43900 6c 69 74 65 33 20 2a 2c 20 73 71 6c 69 74 65 33  lite3 *, sqlite3
43910 5f 76 74 61 62 20 2a 29 3b 0a 53 51 4c 49 54 45  _vtab *);.SQLITE
43920 5f 50 52 49 56 41 54 45 20 46 75 6e 63 44 65 66  _PRIVATE FuncDef
43930 20 2a 73 71 6c 69 74 65 33 56 74 61 62 4f 76 65   *sqlite3VtabOve
43940 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e 28 46 75  rloadFunction(Fu
43950 6e 63 44 65 66 2a 2c 20 69 6e 74 20 6e 41 72 67  ncDef*, int nArg
43960 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45  , Expr*);.SQLITE
43970 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
43980 6c 69 74 65 33 49 6e 76 61 6c 69 64 46 75 6e 63  lite3InvalidFunc
43990 74 69 6f 6e 28 73 71 6c 69 74 65 33 5f 63 6f 6e  tion(sqlite3_con
439a0 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  text*,int,sqlite
439b0 33 5f 76 61 6c 75 65 2a 2a 29 3b 0a 53 51 4c 49  3_value**);.SQLI
439c0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
439d0 71 6c 69 74 65 33 52 65 70 72 65 70 61 72 65 28  qlite3Reprepare(
439e0 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Vdbe*);.SQLITE_P
439f0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
43a00 74 65 33 45 78 70 72 4c 69 73 74 43 68 65 63 6b  te3ExprListCheck
43a10 4c 65 6e 67 74 68 28 50 61 72 73 65 2a 2c 20 45  Length(Parse*, E
43a20 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 63  xprList*, int, c
43a30 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 43 6f 6c  onst char*);.Col
43a40 6c 53 65 71 2a 20 73 71 6c 69 74 65 33 42 69 6e  lSeq* sqlite3Bin
43a50 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65  aryCompareCollSe
43a60 71 28 50 61 72 73 65 20 2a 2c 20 45 78 70 72 20  q(Parse *, Expr 
43a70 2a 2c 20 45 78 70 72 20 2a 29 3b 0a 0a 23 69 66  *, Expr *);..#if
43a80 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52   SQLITE_MAX_EXPR
43a90 5f 44 45 50 54 48 3e 30 0a 53 51 4c 49 54 45 5f  _DEPTH>0.SQLITE_
43aa0 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
43ab0 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69  qlite3ExprSetHei
43ac0 67 68 74 28 45 78 70 72 20 2a 29 3b 0a 53 51 4c  ght(Expr *);.SQL
43ad0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e  ITE_PRIVATE   in
43ae0 74 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45  t sqlite3SelectE
43af0 78 70 72 48 65 69 67 68 74 28 53 65 6c 65 63 74  xprHeight(Select
43b00 20 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65   *);.#else.  #de
43b10 66 69 6e 65 20 73 71 6c 69 74 65 33 45 78 70 72  fine sqlite3Expr
43b20 53 65 74 48 65 69 67 68 74 28 78 29 0a 23 65 6e  SetHeight(x).#en
43b30 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  dif..SQLITE_PRIV
43b40 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 47  ATE u32 sqlite3G
43b50 65 74 32 62 79 74 65 28 63 6f 6e 73 74 20 75 38  et2byte(const u8
43b60 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
43b70 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 47 65  TE u32 sqlite3Ge
43b80 74 34 62 79 74 65 28 63 6f 6e 73 74 20 75 38 2a  t4byte(const u8*
43b90 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
43ba0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 75  E void sqlite3Pu
43bb0 74 32 62 79 74 65 28 75 38 2a 2c 20 75 33 32 29  t2byte(u8*, u32)
43bc0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
43bd0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 75 74   void sqlite3Put
43be0 34 62 79 74 65 28 75 38 2a 2c 20 75 33 32 29 3b  4byte(u8*, u32);
43bf0 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
43c00 53 53 45 0a 23 69 6e 63 6c 75 64 65 20 22 73 73  SSE.#include "ss
43c10 65 49 6e 74 2e 68 22 0a 23 65 6e 64 69 66 0a 0a  eInt.h".#endif..
43c20 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
43c30 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  BUG.SQLITE_PRIVA
43c40 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
43c50 33 50 61 72 73 65 72 54 72 61 63 65 28 46 49 4c  3ParserTrace(FIL
43c60 45 2a 2c 20 63 68 61 72 20 2a 29 3b 0a 23 65 6e  E*, char *);.#en
43c70 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68  dif../*.** If th
43c80 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 20  e SQLITE_ENABLE 
43c90 49 4f 54 52 41 43 45 20 65 78 69 73 74 73 20 74  IOTRACE exists t
43ca0 68 65 6e 20 74 68 65 20 67 6c 6f 62 61 6c 20 76  hen the global v
43cb0 61 72 69 61 62 6c 65 0a 2a 2a 20 73 71 6c 69 74  ariable.** sqlit
43cc0 65 33 5f 69 6f 5f 74 72 61 63 65 20 69 73 20 61  e3_io_trace is a
43cd0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 72   pointer to a pr
43ce0 69 6e 74 66 2d 6c 69 6b 65 20 72 6f 75 74 69 6e  intf-like routin
43cf0 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 70 72 69  e used to.** pri
43d00 6e 74 20 49 2f 4f 20 74 72 61 63 69 6e 67 20 6d  nt I/O tracing m
43d10 65 73 73 61 67 65 73 2e 20 0a 2a 2f 0a 23 69 66  essages. .*/.#if
43d20 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
43d30 45 5f 49 4f 54 52 41 43 45 0a 23 20 64 65 66 69  E_IOTRACE.# defi
43d40 6e 65 20 49 4f 54 52 41 43 45 28 41 29 20 20 69  ne IOTRACE(A)  i
43d50 66 28 20 73 71 6c 69 74 65 33 5f 69 6f 5f 74 72  f( sqlite3_io_tr
43d60 61 63 65 20 29 7b 20 73 71 6c 69 74 65 33 5f 69  ace ){ sqlite3_i
43d70 6f 5f 74 72 61 63 65 20 41 3b 20 7d 0a 53 51 4c  o_trace A; }.SQL
43d80 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
43d90 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 49 4f  id sqlite3VdbeIO
43da0 54 72 61 63 65 53 71 6c 28 56 64 62 65 2a 29 3b  TraceSql(Vdbe*);
43db0 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
43dc0 49 4f 54 52 41 43 45 28 41 29 0a 23 20 64 65 66  IOTRACE(A).# def
43dd0 69 6e 65 20 73 71 6c 69 74 65 33 56 64 62 65 49  ine sqlite3VdbeI
43de0 4f 54 72 61 63 65 53 71 6c 28 58 29 0a 23 65 6e  OTraceSql(X).#en
43df0 64 69 66 0a 65 78 74 65 72 6e 20 76 6f 69 64 20  dif.extern void 
43e00 28 2a 73 71 6c 69 74 65 33 5f 69 6f 5f 74 72 61  (*sqlite3_io_tra
43e10 63 65 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c  ce)(const char*,
43e20 2e 2e 2e 29 3b 0a 0a 23 65 6e 64 69 66 0a 0a 2f  ...);..#endif../
43e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
43e40 6e 64 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e  nd of sqliteInt.
43e50 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
43e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
43e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43  ************** C
43e90 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20  ontinuing where 
43ea0 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 64  we left off in d
43eb0 61 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ate.c **********
43ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23  *************/.#
43ed0 69 6e 63 6c 75 64 65 20 3c 63 74 79 70 65 2e 68  include <ctype.h
43ee0 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 74 69 6d 65  >.#include <time
43ef0 2e 68 3e 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  .h>..#ifndef SQL
43f00 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d  ITE_OMIT_DATETIM
43f10 45 5f 46 55 4e 43 53 0a 0a 2f 2a 0a 2a 2a 20 41  E_FUNCS../*.** A
43f20 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 68   structure for h
43f30 6f 6c 64 69 6e 67 20 61 20 73 69 6e 67 6c 65 20  olding a single 
43f40 64 61 74 65 20 61 6e 64 20 74 69 6d 65 2e 0a 2a  date and time..*
43f50 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
43f60 20 44 61 74 65 54 69 6d 65 20 44 61 74 65 54 69   DateTime DateTi
43f70 6d 65 3b 0a 73 74 72 75 63 74 20 44 61 74 65 54  me;.struct DateT
43f80 69 6d 65 20 7b 0a 20 20 64 6f 75 62 6c 65 20 72  ime {.  double r
43f90 4a 44 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20  JD;      /* The 
43fa0 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 65  julian day numbe
43fb0 72 20 2a 2f 0a 20 20 69 6e 74 20 59 2c 20 4d 2c  r */.  int Y, M,
43fc0 20 44 3b 20 20 20 20 20 2f 2a 20 59 65 61 72 2c   D;     /* Year,
43fd0 20 6d 6f 6e 74 68 2c 20 61 6e 64 20 64 61 79 20   month, and day 
43fe0 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 6d 3b 20 20  */.  int h, m;  
43ff0 20 20 20 20 20 20 2f 2a 20 48 6f 75 72 20 61 6e        /* Hour an
44000 64 20 6d 69 6e 75 74 65 73 20 2a 2f 0a 20 20 69  d minutes */.  i
44010 6e 74 20 74 7a 3b 20 20 20 20 20 20 20 20 20 20  nt tz;          
44020 2f 2a 20 54 69 6d 65 7a 6f 6e 65 20 6f 66 66 73  /* Timezone offs
44030 65 74 20 69 6e 20 6d 69 6e 75 74 65 73 20 2a 2f  et in minutes */
44040 0a 20 20 64 6f 75 62 6c 65 20 73 3b 20 20 20 20  .  double s;    
44050 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 73 20 2a      /* Seconds *
44060 2f 0a 20 20 63 68 61 72 20 76 61 6c 69 64 59 4d  /.  char validYM
44070 44 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20  D;   /* True if 
44080 59 2c 4d 2c 44 20 61 72 65 20 76 61 6c 69 64 20  Y,M,D are valid 
44090 2a 2f 0a 20 20 63 68 61 72 20 76 61 6c 69 64 48  */.  char validH
440a0 4d 53 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 66  MS;   /* True if
440b0 20 68 2c 6d 2c 73 20 61 72 65 20 76 61 6c 69 64   h,m,s are valid
440c0 20 2a 2f 0a 20 20 63 68 61 72 20 76 61 6c 69 64   */.  char valid
440d0 4a 44 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69  JD;    /* True i
440e0 66 20 72 4a 44 20 69 73 20 76 61 6c 69 64 20 2a  f rJD is valid *
440f0 2f 0a 20 20 63 68 61 72 20 76 61 6c 69 64 54 5a  /.  char validTZ
44100 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20  ;    /* True if 
44110 74 7a 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 7d  tz is valid */.}
44120 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  ;.../*.** Conver
44130 74 20 7a 44 61 74 65 20 69 6e 74 6f 20 6f 6e 65  t zDate into one
44140 20 6f 72 20 6d 6f 72 65 20 69 6e 74 65 67 65 72   or more integer
44150 73 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 61  s.  Additional a
44160 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 63 6f 6d 65  rguments.** come
44170 20 69 6e 20 67 72 6f 75 70 73 20 6f 66 20 35 20   in groups of 5 
44180 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
44190 2a 20 20 20 20 20 20 20 4e 20 20 20 20 20 20 20  *       N       
441a0 6e 75 6d 62 65 72 20 6f 66 20 64 69 67 69 74 73  number of digits
441b0 20 69 6e 20 74 68 65 20 69 6e 74 65 67 65 72 0a   in the integer.
441c0 2a 2a 20 20 20 20 20 20 20 6d 69 6e 20 20 20 20  **       min    
441d0 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 77 65 64   minimum allowed
441e0 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e   value of the in
441f0 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 6d  teger.**       m
44200 61 78 20 20 20 20 20 6d 61 78 69 6d 75 6d 20 61  ax     maximum a
44210 6c 6c 6f 77 65 64 20 76 61 6c 75 65 20 6f 66 20  llowed value of 
44220 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20  the integer.**  
44230 20 20 20 20 20 6e 65 78 74 43 20 20 20 66 69 72       nextC   fir
44240 73 74 20 63 68 61 72 61 63 74 65 72 20 61 66 74  st character aft
44250 65 72 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a  er the integer.*
44260 2a 20 20 20 20 20 20 20 70 56 61 6c 20 20 20 20  *       pVal    
44270 77 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 74  where to write t
44280 68 65 20 69 6e 74 65 67 65 72 73 20 76 61 6c 75  he integers valu
44290 65 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 73  e..**.** Convers
442a0 69 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20 75 6e  ions continue un
442b0 74 69 6c 20 6f 6e 65 20 77 69 74 68 20 6e 65 78  til one with nex
442c0 74 43 3d 3d 30 20 69 73 20 65 6e 63 6f 75 6e 74  tC==0 is encount
442d0 65 72 65 64 2e 0a 2a 2a 20 54 68 65 20 66 75 6e  ered..** The fun
442e0 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68  ction returns th
442f0 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 75 63 63  e number of succ
44300 65 73 73 66 75 6c 20 63 6f 6e 76 65 72 73 69 6f  essful conversio
44310 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ns..*/.static in
44320 74 20 67 65 74 44 69 67 69 74 73 28 63 6f 6e 73  t getDigits(cons
44330 74 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 2e  t char *zDate, .
44340 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61  ..){.  va_list a
44350 70 3b 0a 20 20 69 6e 74 20 76 61 6c 3b 0a 20 20  p;.  int val;.  
44360 69 6e 74 20 4e 3b 0a 20 20 69 6e 74 20 6d 69 6e  int N;.  int min
44370 3b 0a 20 20 69 6e 74 20 6d 61 78 3b 0a 20 20 69  ;.  int max;.  i
44380 6e 74 20 6e 65 78 74 43 3b 0a 20 20 69 6e 74 20  nt nextC;.  int 
44390 2a 70 56 61 6c 3b 0a 20 20 69 6e 74 20 63 6e 74  *pVal;.  int cnt
443a0 20 3d 20 30 3b 0a 20 20 76 61 5f 73 74 61 72 74   = 0;.  va_start
443b0 28 61 70 2c 20 7a 44 61 74 65 29 3b 0a 20 20 64  (ap, zDate);.  d
443c0 6f 7b 0a 20 20 20 20 4e 20 3d 20 76 61 5f 61 72  o{.    N = va_ar
443d0 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20  g(ap, int);.    
443e0 6d 69 6e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  min = va_arg(ap,
443f0 20 69 6e 74 29 3b 0a 20 20 20 20 6d 61 78 20 3d   int);.    max =
44400 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29   va_arg(ap, int)
44410 3b 0a 20 20 20 20 6e 65 78 74 43 20 3d 20 76 61  ;.    nextC = va
44420 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20  _arg(ap, int);. 
44430 20 20 20 70 56 61 6c 20 3d 20 76 61 5f 61 72 67     pVal = va_arg
44440 28 61 70 2c 20 69 6e 74 2a 29 3b 0a 20 20 20 20  (ap, int*);.    
44450 76 61 6c 20 3d 20 30 3b 0a 20 20 20 20 77 68 69  val = 0;.    whi
44460 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 20 20  le( N-- ){.     
44470 20 69 66 28 20 21 69 73 64 69 67 69 74 28 2a 28   if( !isdigit(*(
44480 75 38 2a 29 7a 44 61 74 65 29 20 29 7b 0a 20 20  u8*)zDate) ){.  
44490 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 67        goto end_g
444a0 65 74 44 69 67 69 74 73 3b 0a 20 20 20 20 20 20  etDigits;.      
444b0 7d 0a 20 20 20 20 20 20 76 61 6c 20 3d 20 76 61  }.      val = va
444c0 6c 2a 31 30 20 2b 20 2a 7a 44 61 74 65 20 2d 20  l*10 + *zDate - 
444d0 27 30 27 3b 0a 20 20 20 20 20 20 7a 44 61 74 65  '0';.      zDate
444e0 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ++;.    }.    if
444f0 28 20 76 61 6c 3c 6d 69 6e 20 7c 7c 20 76 61 6c  ( val<min || val
44500 3e 6d 61 78 20 7c 7c 20 28 6e 65 78 74 43 21 3d  >max || (nextC!=
44510 30 20 26 26 20 6e 65 78 74 43 21 3d 2a 7a 44 61  0 && nextC!=*zDa
44520 74 65 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  te) ){.      got
44530 6f 20 65 6e 64 5f 67 65 74 44 69 67 69 74 73 3b  o end_getDigits;
44540 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 56 61 6c  .    }.    *pVal
44550 20 3d 20 76 61 6c 3b 0a 20 20 20 20 7a 44 61 74   = val;.    zDat
44560 65 2b 2b 3b 0a 20 20 20 20 63 6e 74 2b 2b 3b 0a  e++;.    cnt++;.
44570 20 20 7d 77 68 69 6c 65 28 20 6e 65 78 74 43 20    }while( nextC 
44580 29 3b 0a 65 6e 64 5f 67 65 74 44 69 67 69 74 73  );.end_getDigits
44590 3a 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a  :.  va_end(ap);.
445a0 20 20 72 65 74 75 72 6e 20 63 6e 74 3b 0a 7d 0a    return cnt;.}.
445b0 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 65 78 74  ./*.** Read text
445c0 20 66 72 6f 6d 20 7a 5b 5d 20 61 6e 64 20 63 6f   from z[] and co
445d0 6e 76 65 72 74 20 69 6e 74 6f 20 61 20 66 6c 6f  nvert into a flo
445e0 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62  ating point numb
445f0 65 72 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74  er.  Return.** t
44600 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 67  he number of dig
44610 69 74 73 20 63 6f 6e 76 65 72 74 65 64 2e 0a 2a  its converted..*
44620 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 56 61 6c  /.#define getVal
44630 75 65 20 73 71 6c 69 74 65 33 41 74 6f 46 0a 0a  ue sqlite3AtoF..
44640 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 61 20 74 69  /*.** Parse a ti
44650 6d 65 7a 6f 6e 65 20 65 78 74 65 6e 73 69 6f 6e  mezone extension
44660 20 6f 6e 20 74 68 65 20 65 6e 64 20 6f 66 20 61   on the end of a
44670 20 64 61 74 65 2d 74 69 6d 65 2e 0a 2a 2a 20 54   date-time..** T
44680 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 73 20  he extension is 
44690 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a  of the form:.**.
446a0 2a 2a 20 20 20 20 20 20 20 20 28 2b 2f 2d 29 48  **        (+/-)H
446b0 48 3a 4d 4d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  H:MM.**.** If th
446c0 65 20 70 61 72 73 65 20 69 73 20 73 75 63 63 65  e parse is succe
446d0 73 73 66 75 6c 2c 20 77 72 69 74 65 20 74 68 65  ssful, write the
446e0 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6e 75 74   number of minut
446f0 65 73 0a 2a 2a 20 6f 66 20 63 68 61 6e 67 65 20  es.** of change 
44700 69 6e 20 2a 70 6e 4d 69 6e 20 61 6e 64 20 72 65  in *pnMin and re
44710 74 75 72 6e 20 30 2e 20 20 49 66 20 61 20 70 61  turn 0.  If a pa
44720 72 73 65 72 20 65 72 72 6f 72 20 6f 63 63 75 72  rser error occur
44730 73 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 0a  s,.** return 0..
44740 2a 2a 0a 2a 2a 20 41 20 6d 69 73 73 69 6e 67 20  **.** A missing 
44750 73 70 65 63 69 66 69 65 72 20 69 73 20 6e 6f 74  specifier is not
44760 20 63 6f 6e 73 69 64 65 72 65 64 20 61 6e 20 65   considered an e
44770 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rror..*/.static 
44780 69 6e 74 20 70 61 72 73 65 54 69 6d 65 7a 6f 6e  int parseTimezon
44790 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44  e(const char *zD
447a0 61 74 65 2c 20 44 61 74 65 54 69 6d 65 20 2a 70  ate, DateTime *p
447b0 29 7b 0a 20 20 69 6e 74 20 73 67 6e 20 3d 20 30  ){.  int sgn = 0
447c0 3b 0a 20 20 69 6e 74 20 6e 48 72 2c 20 6e 4d 6e  ;.  int nHr, nMn
447d0 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 73 70 61  ;.  while( isspa
447e0 63 65 28 2a 28 75 38 2a 29 7a 44 61 74 65 29 20  ce(*(u8*)zDate) 
447f0 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20 20  ){ zDate++; }.  
44800 70 2d 3e 74 7a 20 3d 20 30 3b 0a 20 20 69 66 28  p->tz = 0;.  if(
44810 20 2a 7a 44 61 74 65 3d 3d 27 2d 27 20 29 7b 0a   *zDate=='-' ){.
44820 20 20 20 20 73 67 6e 20 3d 20 2d 31 3b 0a 20 20      sgn = -1;.  
44830 7d 65 6c 73 65 20 69 66 28 20 2a 7a 44 61 74 65  }else if( *zDate
44840 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 73 67 6e  =='+' ){.    sgn
44850 20 3d 20 2b 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a   = +1;.  }else{.
44860 20 20 20 20 72 65 74 75 72 6e 20 2a 7a 44 61 74      return *zDat
44870 65 21 3d 30 3b 0a 20 20 7d 0a 20 20 7a 44 61 74  e!=0;.  }.  zDat
44880 65 2b 2b 3b 0a 20 20 69 66 28 20 67 65 74 44 69  e++;.  if( getDi
44890 67 69 74 73 28 7a 44 61 74 65 2c 20 32 2c 20 30  gits(zDate, 2, 0
448a0 2c 20 31 34 2c 20 27 3a 27 2c 20 26 6e 48 72 2c  , 14, ':', &nHr,
448b0 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c 20 26 6e   2, 0, 59, 0, &n
448c0 4d 6e 29 21 3d 32 20 29 7b 0a 20 20 20 20 72 65  Mn)!=2 ){.    re
448d0 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a 44  turn 1;.  }.  zD
448e0 61 74 65 20 2b 3d 20 35 3b 0a 20 20 70 2d 3e 74  ate += 5;.  p->t
448f0 7a 20 3d 20 73 67 6e 2a 28 6e 4d 6e 20 2b 20 6e  z = sgn*(nMn + n
44900 48 72 2a 36 30 29 3b 0a 20 20 77 68 69 6c 65 28  Hr*60);.  while(
44910 20 69 73 73 70 61 63 65 28 2a 28 75 38 2a 29 7a   isspace(*(u8*)z
44920 44 61 74 65 29 20 29 7b 20 7a 44 61 74 65 2b 2b  Date) ){ zDate++
44930 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a 44  ; }.  return *zD
44940 61 74 65 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ate!=0;.}../*.**
44950 20 50 61 72 73 65 20 74 69 6d 65 73 20 6f 66 20   Parse times of 
44960 74 68 65 20 66 6f 72 6d 20 48 48 3a 4d 4d 20 6f  the form HH:MM o
44970 72 20 48 48 3a 4d 4d 3a 53 53 20 6f 72 20 48 48  r HH:MM:SS or HH
44980 3a 4d 4d 3a 53 53 2e 46 46 46 46 2e 0a 2a 2a 20  :MM:SS.FFFF..** 
44990 54 68 65 20 48 48 2c 20 4d 4d 2c 20 61 6e 64 20  The HH, MM, and 
449a0 53 53 20 6d 75 73 74 20 65 61 63 68 20 62 65 20  SS must each be 
449b0 65 78 61 63 74 6c 79 20 32 20 64 69 67 69 74 73  exactly 2 digits
449c0 2e 20 20 54 68 65 0a 2a 2a 20 66 72 61 63 74 69  .  The.** fracti
449d0 6f 6e 61 6c 20 73 65 63 6f 6e 64 73 20 46 46 46  onal seconds FFF
449e0 46 20 63 61 6e 20 62 65 20 6f 6e 65 20 6f 72 20  F can be one or 
449f0 6d 6f 72 65 20 64 69 67 69 74 73 2e 0a 2a 2a 0a  more digits..**.
44a00 2a 2a 20 52 65 74 75 72 6e 20 31 20 69 66 20 74  ** Return 1 if t
44a10 68 65 72 65 20 69 73 20 61 20 70 61 72 73 69 6e  here is a parsin
44a20 67 20 65 72 72 6f 72 20 61 6e 64 20 30 20 6f 6e  g error and 0 on
44a30 20 73 75 63 63 65 73 73 2e 0a 2a 2f 0a 73 74 61   success..*/.sta
44a40 74 69 63 20 69 6e 74 20 70 61 72 73 65 48 68 4d  tic int parseHhM
44a50 6d 53 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  mSs(const char *
44a60 7a 44 61 74 65 2c 20 44 61 74 65 54 69 6d 65 20  zDate, DateTime 
44a70 2a 70 29 7b 0a 20 20 69 6e 74 20 68 2c 20 6d 2c  *p){.  int h, m,
44a80 20 73 3b 0a 20 20 64 6f 75 62 6c 65 20 6d 73 20   s;.  double ms 
44a90 3d 20 30 2e 30 3b 0a 20 20 69 66 28 20 67 65 74  = 0.0;.  if( get
44aa0 44 69 67 69 74 73 28 7a 44 61 74 65 2c 20 32 2c  Digits(zDate, 2,
44ab0 20 30 2c 20 32 34 2c 20 27 3a 27 2c 20 26 68 2c   0, 24, ':', &h,
44ac0 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c 20 26 6d   2, 0, 59, 0, &m
44ad0 29 21 3d 32 20 29 7b 0a 20 20 20 20 72 65 74 75  )!=2 ){.    retu
44ae0 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a 44 61 74  rn 1;.  }.  zDat
44af0 65 20 2b 3d 20 35 3b 0a 20 20 69 66 28 20 2a 7a  e += 5;.  if( *z
44b00 44 61 74 65 3d 3d 27 3a 27 20 29 7b 0a 20 20 20  Date==':' ){.   
44b10 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 69 66   zDate++;.    if
44b20 28 20 67 65 74 44 69 67 69 74 73 28 7a 44 61 74  ( getDigits(zDat
44b30 65 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c 20  e, 2, 0, 59, 0, 
44b40 26 73 29 21 3d 31 20 29 7b 0a 20 20 20 20 20 20  &s)!=1 ){.      
44b50 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
44b60 20 20 20 20 7a 44 61 74 65 20 2b 3d 20 32 3b 0a      zDate += 2;.
44b70 20 20 20 20 69 66 28 20 2a 7a 44 61 74 65 3d 3d      if( *zDate==
44b80 27 2e 27 20 26 26 20 69 73 64 69 67 69 74 28 28  '.' && isdigit((
44b90 75 38 29 7a 44 61 74 65 5b 31 5d 29 20 29 7b 0a  u8)zDate[1]) ){.
44ba0 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 53 63        double rSc
44bb0 61 6c 65 20 3d 20 31 2e 30 3b 0a 20 20 20 20 20  ale = 1.0;.     
44bc0 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 20 20   zDate++;.      
44bd0 77 68 69 6c 65 28 20 69 73 64 69 67 69 74 28 2a  while( isdigit(*
44be0 28 75 38 2a 29 7a 44 61 74 65 29 20 29 7b 0a 20  (u8*)zDate) ){. 
44bf0 20 20 20 20 20 20 20 6d 73 20 3d 20 6d 73 2a 31         ms = ms*1
44c00 30 2e 30 20 2b 20 2a 7a 44 61 74 65 20 2d 20 27  0.0 + *zDate - '
44c10 30 27 3b 0a 20 20 20 20 20 20 20 20 72 53 63 61  0';.        rSca
44c20 6c 65 20 2a 3d 20 31 30 2e 30 3b 0a 20 20 20 20  le *= 10.0;.    
44c30 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20      zDate++;.   
44c40 20 20 20 7d 0a 20 20 20 20 20 20 6d 73 20 2f 3d     }.      ms /=
44c50 20 72 53 63 61 6c 65 3b 0a 20 20 20 20 7d 0a 20   rScale;.    }. 
44c60 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 20 3d 20   }else{.    s = 
44c70 30 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76 61 6c 69  0;.  }.  p->vali
44c80 64 4a 44 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61  dJD = 0;.  p->va
44c90 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 20 20 70 2d  lidHMS = 1;.  p-
44ca0 3e 68 20 3d 20 68 3b 0a 20 20 70 2d 3e 6d 20 3d  >h = h;.  p->m =
44cb0 20 6d 3b 0a 20 20 70 2d 3e 73 20 3d 20 73 20 2b   m;.  p->s = s +
44cc0 20 6d 73 3b 0a 20 20 69 66 28 20 70 61 72 73 65   ms;.  if( parse
44cd0 54 69 6d 65 7a 6f 6e 65 28 7a 44 61 74 65 2c 20  Timezone(zDate, 
44ce0 70 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20  p) ) return 1;. 
44cf0 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 70 2d   p->validTZ = p-
44d00 3e 74 7a 21 3d 30 3b 0a 20 20 72 65 74 75 72 6e  >tz!=0;.  return
44d10 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e   0;.}../*.** Con
44d20 76 65 72 74 20 66 72 6f 6d 20 59 59 59 59 2d 4d  vert from YYYY-M
44d30 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 20 74 6f  M-DD HH:MM:SS to
44d40 20 6a 75 6c 69 61 6e 20 64 61 79 2e 20 20 57 65   julian day.  We
44d50 20 61 6c 77 61 79 73 20 61 73 73 75 6d 65 0a 2a   always assume.*
44d60 2a 20 74 68 61 74 20 74 68 65 20 59 59 59 59 2d  * that the YYYY-
44d70 4d 4d 2d 44 44 20 69 73 20 61 63 63 6f 72 64 69  MM-DD is accordi
44d80 6e 67 20 74 6f 20 74 68 65 20 47 72 65 67 6f 72  ng to the Gregor
44d90 69 61 6e 20 63 61 6c 65 6e 64 61 72 2e 0a 2a 2a  ian calendar..**
44da0 0a 2a 2a 20 52 65 66 65 72 65 6e 63 65 3a 20 20  .** Reference:  
44db0 4d 65 65 75 73 20 70 61 67 65 20 36 31 0a 2a 2f  Meeus page 61.*/
44dc0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6d  .static void com
44dd0 70 75 74 65 4a 44 28 44 61 74 65 54 69 6d 65 20  puteJD(DateTime 
44de0 2a 70 29 7b 0a 20 20 69 6e 74 20 59 2c 20 4d 2c  *p){.  int Y, M,
44df0 20 44 2c 20 41 2c 20 42 2c 20 58 31 2c 20 58 32   D, A, B, X1, X2
44e00 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c 69  ;..  if( p->vali
44e10 64 4a 44 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  dJD ) return;.  
44e20 69 66 28 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20  if( p->validYMD 
44e30 29 7b 0a 20 20 20 20 59 20 3d 20 70 2d 3e 59 3b  ){.    Y = p->Y;
44e40 0a 20 20 20 20 4d 20 3d 20 70 2d 3e 4d 3b 0a 20  .    M = p->M;. 
44e50 20 20 20 44 20 3d 20 70 2d 3e 44 3b 0a 20 20 7d     D = p->D;.  }
44e60 65 6c 73 65 7b 0a 20 20 20 20 59 20 3d 20 32 30  else{.    Y = 20
44e70 30 30 3b 20 20 2f 2a 20 49 66 20 6e 6f 20 59 4d  00;  /* If no YM
44e80 44 20 73 70 65 63 69 66 69 65 64 2c 20 61 73 73  D specified, ass
44e90 75 6d 65 20 32 30 30 30 2d 4a 61 6e 2d 30 31 20  ume 2000-Jan-01 
44ea0 2a 2f 0a 20 20 20 20 4d 20 3d 20 31 3b 0a 20 20  */.    M = 1;.  
44eb0 20 20 44 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 69    D = 1;.  }.  i
44ec0 66 28 20 4d 3c 3d 32 20 29 7b 0a 20 20 20 20 59  f( M<=2 ){.    Y
44ed0 2d 2d 3b 0a 20 20 20 20 4d 20 2b 3d 20 31 32 3b  --;.    M += 12;
44ee0 0a 20 20 7d 0a 20 20 41 20 3d 20 59 2f 31 30 30  .  }.  A = Y/100
44ef0 3b 0a 20 20 42 20 3d 20 32 20 2d 20 41 20 2b 20  ;.  B = 2 - A + 
44f00 28 41 2f 34 29 3b 0a 20 20 58 31 20 3d 20 33 36  (A/4);.  X1 = 36
44f10 35 2e 32 35 2a 28 59 2b 34 37 31 36 29 3b 0a 20  5.25*(Y+4716);. 
44f20 20 58 32 20 3d 20 33 30 2e 36 30 30 31 2a 28 4d   X2 = 30.6001*(M
44f30 2b 31 29 3b 0a 20 20 70 2d 3e 72 4a 44 20 3d 20  +1);.  p->rJD = 
44f40 58 31 20 2b 20 58 32 20 2b 20 44 20 2b 20 42 20  X1 + X2 + D + B 
44f50 2d 20 31 35 32 34 2e 35 3b 0a 20 20 70 2d 3e 76  - 1524.5;.  p->v
44f60 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 20 69 66  alidJD = 1;.  if
44f70 28 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 29 7b  ( p->validHMS ){
44f80 0a 20 20 20 20 70 2d 3e 72 4a 44 20 2b 3d 20 28  .    p->rJD += (
44f90 70 2d 3e 68 2a 33 36 30 30 2e 30 20 2b 20 70 2d  p->h*3600.0 + p-
44fa0 3e 6d 2a 36 30 2e 30 20 2b 20 70 2d 3e 73 29 2f  >m*60.0 + p->s)/
44fb0 38 36 34 30 30 2e 30 3b 0a 20 20 20 20 69 66 28  86400.0;.    if(
44fc0 20 70 2d 3e 76 61 6c 69 64 54 5a 20 29 7b 0a 20   p->validTZ ){. 
44fd0 20 20 20 20 20 70 2d 3e 72 4a 44 20 2d 3d 20 70       p->rJD -= p
44fe0 2d 3e 74 7a 2a 36 30 2f 38 36 34 30 30 2e 30 3b  ->tz*60/86400.0;
44ff0 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 59  .      p->validY
45000 4d 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d  MD = 0;.      p-
45010 3e 76 61 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20  >validHMS = 0;. 
45020 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20       p->validTZ 
45030 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  = 0;.    }.  }.}
45040 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 64 61  ../*.** Parse da
45050 74 65 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a  tes of the form.
45060 2a 2a 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d  **.**     YYYY-M
45070 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 2e 46 46  M-DD HH:MM:SS.FF
45080 46 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d  F.**     YYYY-MM
45090 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2a 20  -DD HH:MM:SS.** 
450a0 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48      YYYY-MM-DD H
450b0 48 3a 4d 4d 0a 2a 2a 20 20 20 20 20 59 59 59 59  H:MM.**     YYYY
450c0 2d 4d 4d 2d 44 44 0a 2a 2a 0a 2a 2a 20 57 72 69  -MM-DD.**.** Wri
450d0 74 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e  te the result in
450e0 74 6f 20 74 68 65 20 44 61 74 65 54 69 6d 65 20  to the DateTime 
450f0 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 72 65  structure and re
45100 74 75 72 6e 20 30 0a 2a 2a 20 6f 6e 20 73 75 63  turn 0.** on suc
45110 63 65 73 73 20 61 6e 64 20 31 20 69 66 20 74 68  cess and 1 if th
45120 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 69  e input string i
45130 73 20 6e 6f 74 20 61 20 77 65 6c 6c 2d 66 6f 72  s not a well-for
45140 6d 65 64 0a 2a 2a 20 64 61 74 65 2e 0a 2a 2f 0a  med.** date..*/.
45150 73 74 61 74 69 63 20 69 6e 74 20 70 61 72 73 65  static int parse
45160 59 79 79 79 4d 6d 44 64 28 63 6f 6e 73 74 20 63  YyyyMmDd(const c
45170 68 61 72 20 2a 7a 44 61 74 65 2c 20 44 61 74 65  har *zDate, Date
45180 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  Time *p){.  int 
45190 59 2c 20 4d 2c 20 44 2c 20 6e 65 67 3b 0a 0a 20  Y, M, D, neg;.. 
451a0 20 69 66 28 20 7a 44 61 74 65 5b 30 5d 3d 3d 27   if( zDate[0]=='
451b0 2d 27 20 29 7b 0a 20 20 20 20 7a 44 61 74 65 2b  -' ){.    zDate+
451c0 2b 3b 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a  +;.    neg = 1;.
451d0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 65 67    }else{.    neg
451e0 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20   = 0;.  }.  if( 
451f0 67 65 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c  getDigits(zDate,
45200 34 2c 30 2c 39 39 39 39 2c 27 2d 27 2c 26 59 2c  4,0,9999,'-',&Y,
45210 32 2c 31 2c 31 32 2c 27 2d 27 2c 26 4d 2c 32 2c  2,1,12,'-',&M,2,
45220 31 2c 33 31 2c 30 2c 26 44 29 21 3d 33 20 29 7b  1,31,0,&D)!=3 ){
45230 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
45240 20 7d 0a 20 20 7a 44 61 74 65 20 2b 3d 20 31 30   }.  zDate += 10
45250 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 73 70 61  ;.  while( isspa
45260 63 65 28 2a 28 75 38 2a 29 7a 44 61 74 65 29 20  ce(*(u8*)zDate) 
45270 7c 7c 20 27 54 27 3d 3d 2a 28 75 38 2a 29 7a 44  || 'T'==*(u8*)zD
45280 61 74 65 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20  ate ){ zDate++; 
45290 7d 0a 20 20 69 66 28 20 70 61 72 73 65 48 68 4d  }.  if( parseHhM
452a0 6d 53 73 28 7a 44 61 74 65 2c 20 70 29 3d 3d 30  mSs(zDate, p)==0
452b0 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 67 6f   ){.    /* We go
452c0 74 20 74 68 65 20 74 69 6d 65 20 2a 2f 0a 20 20  t the time */.  
452d0 7d 65 6c 73 65 20 69 66 28 20 2a 7a 44 61 74 65  }else if( *zDate
452e0 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 76 61  ==0 ){.    p->va
452f0 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 7d 65  lidHMS = 0;.  }e
45300 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  lse{.    return 
45310 31 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76 61 6c 69  1;.  }.  p->vali
45320 64 4a 44 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61  dJD = 0;.  p->va
45330 6c 69 64 59 4d 44 20 3d 20 31 3b 0a 20 20 70 2d  lidYMD = 1;.  p-
45340 3e 59 20 3d 20 6e 65 67 20 3f 20 2d 59 20 3a 20  >Y = neg ? -Y : 
45350 59 3b 0a 20 20 70 2d 3e 4d 20 3d 20 4d 3b 0a 20  Y;.  p->M = M;. 
45360 20 70 2d 3e 44 20 3d 20 44 3b 0a 20 20 69 66 28   p->D = D;.  if(
45370 20 70 2d 3e 76 61 6c 69 64 54 5a 20 29 7b 0a 20   p->validTZ ){. 
45380 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b     computeJD(p);
45390 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
453a0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70  .}../*.** Attemp
453b0 74 20 74 6f 20 70 61 72 73 65 20 74 68 65 20 67  t to parse the g
453c0 69 76 65 6e 20 73 74 72 69 6e 67 20 69 6e 74 6f  iven string into
453d0 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 4e 75   a Julian Day Nu
453e0 6d 62 65 72 2e 20 20 52 65 74 75 72 6e 0a 2a 2a  mber.  Return.**
453f0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
45400 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  rrors..**.** The
45410 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 61   following are a
45420 63 63 65 70 74 61 62 6c 65 20 66 6f 72 6d 73 20  cceptable forms 
45430 66 6f 72 20 74 68 65 20 69 6e 70 75 74 20 73 74  for the input st
45440 72 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ring:.**.**     
45450 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d   YYYY-MM-DD HH:M
45460 4d 3a 53 53 2e 46 46 46 20 20 2b 2f 2d 48 48 3a  M:SS.FFF  +/-HH:
45470 4d 4d 0a 2a 2a 20 20 20 20 20 20 44 44 44 44 2e  MM.**      DDDD.
45480 44 44 20 0a 2a 2a 20 20 20 20 20 20 6e 6f 77 0a  DD .**      now.
45490 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 66 69 72  **.** In the fir
454a0 73 74 20 66 6f 72 6d 2c 20 74 68 65 20 2b 2f 2d  st form, the +/-
454b0 48 48 3a 4d 4d 20 69 73 20 61 6c 77 61 79 73 20  HH:MM is always 
454c0 6f 70 74 69 6f 6e 61 6c 2e 20 20 54 68 65 20 66  optional.  The f
454d0 72 61 63 74 69 6f 6e 61 6c 0a 2a 2a 20 73 65 63  ractional.** sec
454e0 6f 6e 64 73 20 65 78 74 65 6e 73 69 6f 6e 20 28  onds extension (
454f0 74 68 65 20 22 2e 46 46 46 22 29 20 69 73 20 6f  the ".FFF") is o
45500 70 74 69 6f 6e 61 6c 2e 20 20 54 68 65 20 73 65  ptional.  The se
45510 63 6f 6e 64 73 20 70 6f 72 74 69 6f 6e 0a 2a 2a  conds portion.**
45520 20 28 22 3a 53 53 2e 46 46 46 22 29 20 69 73 20   (":SS.FFF") is 
45530 6f 70 74 69 6f 6e 2e 20 20 54 68 65 20 79 65 61  option.  The yea
45540 72 20 61 6e 64 20 64 61 74 65 20 63 61 6e 20 62  r and date can b
45550 65 20 6f 6d 69 74 74 65 64 20 61 73 20 6c 6f 6e  e omitted as lon
45560 67 0a 2a 2a 20 61 73 20 74 68 65 72 65 20 69 73  g.** as there is
45570 20 61 20 74 69 6d 65 20 73 74 72 69 6e 67 2e 20   a time string. 
45580 20 54 68 65 20 74 69 6d 65 20 73 74 72 69 6e 67   The time string
45590 20 63 61 6e 20 62 65 20 6f 6d 69 74 74 65 64 20   can be omitted 
455a0 61 73 20 6c 6f 6e 67 0a 2a 2a 20 61 73 20 74 68  as long.** as th
455b0 65 72 65 20 69 73 20 61 20 79 65 61 72 20 61 6e  ere is a year an
455c0 64 20 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69  d date..*/.stati
455d0 63 20 69 6e 74 20 70 61 72 73 65 44 61 74 65 4f  c int parseDateO
455e0 72 54 69 6d 65 28 63 6f 6e 73 74 20 63 68 61 72  rTime(const char
455f0 20 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 69 6d   *zDate, DateTim
45600 65 20 2a 70 29 7b 0a 20 20 6d 65 6d 73 65 74 28  e *p){.  memset(
45610 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 29  p, 0, sizeof(*p)
45620 29 3b 0a 20 20 69 66 28 20 70 61 72 73 65 59 79  );.  if( parseYy
45630 79 79 4d 6d 44 64 28 7a 44 61 74 65 2c 70 29 3d  yyMmDd(zDate,p)=
45640 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
45650 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   0;.  }else if( 
45660 70 61 72 73 65 48 68 4d 6d 53 73 28 7a 44 61 74  parseHhMmSs(zDat
45670 65 2c 20 70 29 3d 3d 30 20 29 7b 0a 20 20 20 20  e, p)==0 ){.    
45680 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73  return 0;.  }els
45690 65 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72  e if( sqlite3Str
456a0 49 43 6d 70 28 7a 44 61 74 65 2c 22 6e 6f 77 22  ICmp(zDate,"now"
456b0 29 3d 3d 30 29 7b 0a 20 20 20 20 64 6f 75 62 6c  )==0){.    doubl
456c0 65 20 72 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  e r;.    sqlite3
456d0 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 26 72  OsCurrentTime(&r
456e0 29 3b 0a 20 20 20 20 70 2d 3e 72 4a 44 20 3d 20  );.    p->rJD = 
456f0 72 3b 0a 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a  r;.    p->validJ
45700 44 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72  D = 1;.    retur
45710 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  n 0;.  }else if(
45720 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 72   sqlite3IsNumber
45730 28 7a 44 61 74 65 2c 20 30 2c 20 53 51 4c 49 54  (zDate, 0, SQLIT
45740 45 5f 55 54 46 38 29 20 29 7b 0a 20 20 20 20 67  E_UTF8) ){.    g
45750 65 74 56 61 6c 75 65 28 7a 44 61 74 65 2c 20 26  etValue(zDate, &
45760 70 2d 3e 72 4a 44 29 3b 0a 20 20 20 20 70 2d 3e  p->rJD);.    p->
45770 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 20 20  validJD = 1;.   
45780 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
45790 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a   return 1;.}../*
457a0 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20  .** Compute the 
457b0 59 65 61 72 2c 20 4d 6f 6e 74 68 2c 20 61 6e 64  Year, Month, and
457c0 20 44 61 79 20 66 72 6f 6d 20 74 68 65 20 6a 75   Day from the ju
457d0 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 2e  lian day number.
457e0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
457f0 63 6f 6d 70 75 74 65 59 4d 44 28 44 61 74 65 54  computeYMD(DateT
45800 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 5a  ime *p){.  int Z
45810 2c 20 41 2c 20 42 2c 20 43 2c 20 44 2c 20 45 2c  , A, B, C, D, E,
45820 20 58 31 3b 0a 20 20 69 66 28 20 70 2d 3e 76 61   X1;.  if( p->va
45830 6c 69 64 59 4d 44 20 29 20 72 65 74 75 72 6e 3b  lidYMD ) return;
45840 0a 20 20 69 66 28 20 21 70 2d 3e 76 61 6c 69 64  .  if( !p->valid
45850 4a 44 20 29 7b 0a 20 20 20 20 70 2d 3e 59 20 3d  JD ){.    p->Y =
45860 20 32 30 30 30 3b 0a 20 20 20 20 70 2d 3e 4d 20   2000;.    p->M 
45870 3d 20 31 3b 0a 20 20 20 20 70 2d 3e 44 20 3d 20  = 1;.    p->D = 
45880 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  1;.  }else{.    
45890 5a 20 3d 20 70 2d 3e 72 4a 44 20 2b 20 30 2e 35  Z = p->rJD + 0.5
458a0 3b 0a 20 20 20 20 41 20 3d 20 28 5a 20 2d 20 31  ;.    A = (Z - 1
458b0 38 36 37 32 31 36 2e 32 35 29 2f 33 36 35 32 34  867216.25)/36524
458c0 2e 32 35 3b 0a 20 20 20 20 41 20 3d 20 5a 20 2b  .25;.    A = Z +
458d0 20 31 20 2b 20 41 20 2d 20 28 41 2f 34 29 3b 0a   1 + A - (A/4);.
458e0 20 20 20 20 42 20 3d 20 41 20 2b 20 31 35 32 34      B = A + 1524
458f0 3b 0a 20 20 20 20 43 20 3d 20 28 42 20 2d 20 31  ;.    C = (B - 1
45900 32 32 2e 31 29 2f 33 36 35 2e 32 35 3b 0a 20 20  22.1)/365.25;.  
45910 20 20 44 20 3d 20 33 36 35 2e 32 35 2a 43 3b 0a    D = 365.25*C;.
45920 20 20 20 20 45 20 3d 20 28 42 2d 44 29 2f 33 30      E = (B-D)/30
45930 2e 36 30 30 31 3b 0a 20 20 20 20 58 31 20 3d 20  .6001;.    X1 = 
45940 33 30 2e 36 30 30 31 2a 45 3b 0a 20 20 20 20 70  30.6001*E;.    p
45950 2d 3e 44 20 3d 20 42 20 2d 20 44 20 2d 20 58 31  ->D = B - D - X1
45960 3b 0a 20 20 20 20 70 2d 3e 4d 20 3d 20 45 3c 31  ;.    p->M = E<1
45970 34 20 3f 20 45 2d 31 20 3a 20 45 2d 31 33 3b 0a  4 ? E-1 : E-13;.
45980 20 20 20 20 70 2d 3e 59 20 3d 20 70 2d 3e 4d 3e      p->Y = p->M>
45990 32 20 3f 20 43 20 2d 20 34 37 31 36 20 3a 20 43  2 ? C - 4716 : C
459a0 20 2d 20 34 37 31 35 3b 0a 20 20 7d 0a 20 20 70   - 4715;.  }.  p
459b0 2d 3e 76 61 6c 69 64 59 4d 44 20 3d 20 31 3b 0a  ->validYMD = 1;.
459c0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65  }../*.** Compute
459d0 20 74 68 65 20 48 6f 75 72 2c 20 4d 69 6e 75 74   the Hour, Minut
459e0 65 2c 20 61 6e 64 20 53 65 63 6f 6e 64 73 20 66  e, and Seconds f
459f0 72 6f 6d 20 74 68 65 20 6a 75 6c 69 61 6e 20 64  rom the julian d
45a00 61 79 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74  ay number..*/.st
45a10 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74  atic void comput
45a20 65 48 4d 53 28 44 61 74 65 54 69 6d 65 20 2a 70  eHMS(DateTime *p
45a30 29 7b 0a 20 20 69 6e 74 20 5a 2c 20 73 3b 0a 20  ){.  int Z, s;. 
45a40 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 48 4d 53   if( p->validHMS
45a50 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 63 6f 6d   ) return;.  com
45a60 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 5a 20 3d  puteJD(p);.  Z =
45a70 20 70 2d 3e 72 4a 44 20 2b 20 30 2e 35 3b 0a 20   p->rJD + 0.5;. 
45a80 20 73 20 3d 20 28 70 2d 3e 72 4a 44 20 2b 20 30   s = (p->rJD + 0
45a90 2e 35 20 2d 20 5a 29 2a 38 36 34 30 30 30 30 30  .5 - Z)*86400000
45aa0 2e 30 20 2b 20 30 2e 35 3b 0a 20 20 70 2d 3e 73  .0 + 0.5;.  p->s
45ab0 20 3d 20 30 2e 30 30 31 2a 73 3b 0a 20 20 73 20   = 0.001*s;.  s 
45ac0 3d 20 70 2d 3e 73 3b 0a 20 20 70 2d 3e 73 20 2d  = p->s;.  p->s -
45ad0 3d 20 73 3b 0a 20 20 70 2d 3e 68 20 3d 20 73 2f  = s;.  p->h = s/
45ae0 33 36 30 30 3b 0a 20 20 73 20 2d 3d 20 70 2d 3e  3600;.  s -= p->
45af0 68 2a 33 36 30 30 3b 0a 20 20 70 2d 3e 6d 20 3d  h*3600;.  p->m =
45b00 20 73 2f 36 30 3b 0a 20 20 70 2d 3e 73 20 2b 3d   s/60;.  p->s +=
45b10 20 73 20 2d 20 70 2d 3e 6d 2a 36 30 3b 0a 20 20   s - p->m*60;.  
45b20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b  p->validHMS = 1;
45b30 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74  .}../*.** Comput
45b40 65 20 62 6f 74 68 20 59 4d 44 20 61 6e 64 20 48  e both YMD and H
45b50 4d 53 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  MS.*/.static voi
45b60 64 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53  d computeYMD_HMS
45b70 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20  (DateTime *p){. 
45b80 20 63 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a   computeYMD(p);.
45b90 20 20 63 6f 6d 70 75 74 65 48 4d 53 28 70 29 3b    computeHMS(p);
45ba0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20  .}../*.** Clear 
45bb0 74 68 65 20 59 4d 44 20 61 6e 64 20 48 4d 53 20  the YMD and HMS 
45bc0 61 6e 64 20 74 68 65 20 54 5a 0a 2a 2f 0a 73 74  and the TZ.*/.st
45bd0 61 74 69 63 20 76 6f 69 64 20 63 6c 65 61 72 59  atic void clearY
45be0 4d 44 5f 48 4d 53 5f 54 5a 28 44 61 74 65 54 69  MD_HMS_TZ(DateTi
45bf0 6d 65 20 2a 70 29 7b 0a 20 20 70 2d 3e 76 61 6c  me *p){.  p->val
45c00 69 64 59 4d 44 20 3d 20 30 3b 0a 20 20 70 2d 3e  idYMD = 0;.  p->
45c10 76 61 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20  validHMS = 0;.  
45c20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a  p->validTZ = 0;.
45c30 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65  }../*.** Compute
45c40 20 74 68 65 20 64 69 66 66 65 72 65 6e 63 65 20   the difference 
45c50 28 69 6e 20 64 61 79 73 29 20 62 65 74 77 65 65  (in days) betwee
45c60 6e 20 6c 6f 63 61 6c 74 69 6d 65 20 61 6e 64 20  n localtime and 
45c70 55 54 43 20 28 61 2e 6b 2e 61 2e 20 47 4d 54 29  UTC (a.k.a. GMT)
45c80 0a 2a 2a 20 66 6f 72 20 74 68 65 20 74 69 6d 65  .** for the time
45c90 20 76 61 6c 75 65 20 70 20 77 68 65 72 65 20 70   value p where p
45ca0 20 69 73 20 69 6e 20 55 54 43 2e 0a 2a 2f 0a 73   is in UTC..*/.s
45cb0 74 61 74 69 63 20 64 6f 75 62 6c 65 20 6c 6f 63  tatic double loc
45cc0 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 44 61 74  altimeOffset(Dat
45cd0 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 44 61 74  eTime *p){.  Dat
45ce0 65 54 69 6d 65 20 78 2c 20 79 3b 0a 20 20 74 69  eTime x, y;.  ti
45cf0 6d 65 5f 74 20 74 3b 0a 20 20 78 20 3d 20 2a 70  me_t t;.  x = *p
45d00 3b 0a 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48  ;.  computeYMD_H
45d10 4d 53 28 26 78 29 3b 0a 20 20 69 66 28 20 78 2e  MS(&x);.  if( x.
45d20 59 3c 31 39 37 31 20 7c 7c 20 78 2e 59 3e 3d 32  Y<1971 || x.Y>=2
45d30 30 33 38 20 29 7b 0a 20 20 20 20 78 2e 59 20 3d  038 ){.    x.Y =
45d40 20 32 30 30 30 3b 0a 20 20 20 20 78 2e 4d 20 3d   2000;.    x.M =
45d50 20 31 3b 0a 20 20 20 20 78 2e 44 20 3d 20 31 3b   1;.    x.D = 1;
45d60 0a 20 20 20 20 78 2e 68 20 3d 20 30 3b 0a 20 20  .    x.h = 0;.  
45d70 20 20 78 2e 6d 20 3d 20 30 3b 0a 20 20 20 20 78    x.m = 0;.    x
45d80 2e 73 20 3d 20 30 2e 30 3b 0a 20 20 7d 20 65 6c  .s = 0.0;.  } el
45d90 73 65 20 7b 0a 20 20 20 20 69 6e 74 20 73 20 3d  se {.    int s =
45da0 20 78 2e 73 20 2b 20 30 2e 35 3b 0a 20 20 20 20   x.s + 0.5;.    
45db0 78 2e 73 20 3d 20 73 3b 0a 20 20 7d 0a 20 20 78  x.s = s;.  }.  x
45dc0 2e 74 7a 20 3d 20 30 3b 0a 20 20 78 2e 76 61 6c  .tz = 0;.  x.val
45dd0 69 64 4a 44 20 3d 20 30 3b 0a 20 20 63 6f 6d 70  idJD = 0;.  comp
45de0 75 74 65 4a 44 28 26 78 29 3b 0a 20 20 74 20 3d  uteJD(&x);.  t =
45df0 20 28 78 2e 72 4a 44 2d 32 34 34 30 35 38 37 2e   (x.rJD-2440587.
45e00 35 29 2a 38 36 34 30 30 2e 30 20 2b 20 30 2e 35  5)*86400.0 + 0.5
45e10 3b 0a 23 69 66 64 65 66 20 48 41 56 45 5f 4c 4f  ;.#ifdef HAVE_LO
45e20 43 41 4c 54 49 4d 45 5f 52 0a 20 20 7b 0a 20 20  CALTIME_R.  {.  
45e30 20 20 73 74 72 75 63 74 20 74 6d 20 73 4c 6f 63    struct tm sLoc
45e40 61 6c 3b 0a 20 20 20 20 6c 6f 63 61 6c 74 69 6d  al;.    localtim
45e50 65 5f 72 28 26 74 2c 20 26 73 4c 6f 63 61 6c 29  e_r(&t, &sLocal)
45e60 3b 0a 20 20 20 20 79 2e 59 20 3d 20 73 4c 6f 63  ;.    y.Y = sLoc
45e70 61 6c 2e 74 6d 5f 79 65 61 72 20 2b 20 31 39 30  al.tm_year + 190
45e80 30 3b 0a 20 20 20 20 79 2e 4d 20 3d 20 73 4c 6f  0;.    y.M = sLo
45e90 63 61 6c 2e 74 6d 5f 6d 6f 6e 20 2b 20 31 3b 0a  cal.tm_mon + 1;.
45ea0 20 20 20 20 79 2e 44 20 3d 20 73 4c 6f 63 61 6c      y.D = sLocal
45eb0 2e 74 6d 5f 6d 64 61 79 3b 0a 20 20 20 20 79 2e  .tm_mday;.    y.
45ec0 68 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 68 6f  h = sLocal.tm_ho
45ed0 75 72 3b 0a 20 20 20 20 79 2e 6d 20 3d 20 73 4c  ur;.    y.m = sL
45ee0 6f 63 61 6c 2e 74 6d 5f 6d 69 6e 3b 0a 20 20 20  ocal.tm_min;.   
45ef0 20 79 2e 73 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d   y.s = sLocal.tm
45f00 5f 73 65 63 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a  _sec;.  }.#else.
45f10 20 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74    {.    struct t
45f20 6d 20 2a 70 54 6d 3b 0a 20 20 20 20 73 71 6c 69  m *pTm;.    sqli
45f30 74 65 33 4f 73 45 6e 74 65 72 4d 75 74 65 78 28  te3OsEnterMutex(
45f40 29 3b 0a 20 20 20 20 70 54 6d 20 3d 20 6c 6f 63  );.    pTm = loc
45f50 61 6c 74 69 6d 65 28 26 74 29 3b 0a 20 20 20 20  altime(&t);.    
45f60 79 2e 59 20 3d 20 70 54 6d 2d 3e 74 6d 5f 79 65  y.Y = pTm->tm_ye
45f70 61 72 20 2b 20 31 39 30 30 3b 0a 20 20 20 20 79  ar + 1900;.    y
45f80 2e 4d 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d 6f 6e  .M = pTm->tm_mon
45f90 20 2b 20 31 3b 0a 20 20 20 20 79 2e 44 20 3d 20   + 1;.    y.D = 
45fa0 70 54 6d 2d 3e 74 6d 5f 6d 64 61 79 3b 0a 20 20  pTm->tm_mday;.  
45fb0 20 20 79 2e 68 20 3d 20 70 54 6d 2d 3e 74 6d 5f    y.h = pTm->tm_
45fc0 68 6f 75 72 3b 0a 20 20 20 20 79 2e 6d 20 3d 20  hour;.    y.m = 
45fd0 70 54 6d 2d 3e 74 6d 5f 6d 69 6e 3b 0a 20 20 20  pTm->tm_min;.   
45fe0 20 79 2e 73 20 3d 20 70 54 6d 2d 3e 74 6d 5f 73   y.s = pTm->tm_s
45ff0 65 63 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f  ec;.    sqlite3O
46000 73 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20  sLeaveMutex();. 
46010 20 7d 0a 23 65 6e 64 69 66 0a 20 20 79 2e 76 61   }.#endif.  y.va
46020 6c 69 64 59 4d 44 20 3d 20 31 3b 0a 20 20 79 2e  lidYMD = 1;.  y.
46030 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 20 20  validHMS = 1;.  
46040 79 2e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20  y.validJD = 0;. 
46050 20 79 2e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a   y.validTZ = 0;.
46060 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 79 29 3b    computeJD(&y);
46070 0a 20 20 72 65 74 75 72 6e 20 79 2e 72 4a 44 20  .  return y.rJD 
46080 2d 20 78 2e 72 4a 44 3b 0a 7d 0a 0a 2f 2a 0a 2a  - x.rJD;.}../*.*
46090 2a 20 50 72 6f 63 65 73 73 20 61 20 6d 6f 64 69  * Process a modi
460a0 66 69 65 72 20 74 6f 20 61 20 64 61 74 65 2d 74  fier to a date-t
460b0 69 6d 65 20 73 74 61 6d 70 2e 20 20 54 68 65 20  ime stamp.  The 
460c0 6d 6f 64 69 66 69 65 72 73 20 61 72 65 0a 2a 2a  modifiers are.**
460d0 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a   as follows:.**.
460e0 2a 2a 20 20 20 20 20 4e 4e 4e 20 64 61 79 73 0a  **     NNN days.
460f0 2a 2a 20 20 20 20 20 4e 4e 4e 20 68 6f 75 72 73  **     NNN hours
46100 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 6d 69 6e 75  .**     NNN minu
46110 74 65 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 2e 4e  tes.**     NNN.N
46120 4e 4e 4e 20 73 65 63 6f 6e 64 73 0a 2a 2a 20 20  NNN seconds.**  
46130 20 20 20 4e 4e 4e 20 6d 6f 6e 74 68 73 0a 2a 2a     NNN months.**
46140 20 20 20 20 20 4e 4e 4e 20 79 65 61 72 73 0a 2a       NNN years.*
46150 2a 20 20 20 20 20 73 74 61 72 74 20 6f 66 20 6d  *     start of m
46160 6f 6e 74 68 0a 2a 2a 20 20 20 20 20 73 74 61 72  onth.**     star
46170 74 20 6f 66 20 79 65 61 72 0a 2a 2a 20 20 20 20  t of year.**    
46180 20 73 74 61 72 74 20 6f 66 20 77 65 65 6b 0a 2a   start of week.*
46190 2a 20 20 20 20 20 73 74 61 72 74 20 6f 66 20 64  *     start of d
461a0 61 79 0a 2a 2a 20 20 20 20 20 77 65 65 6b 64 61  ay.**     weekda
461b0 79 20 4e 0a 2a 2a 20 20 20 20 20 75 6e 69 78 65  y N.**     unixe
461c0 70 6f 63 68 0a 2a 2a 20 20 20 20 20 6c 6f 63 61  poch.**     loca
461d0 6c 74 69 6d 65 0a 2a 2a 20 20 20 20 20 75 74 63  ltime.**     utc
461e0 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 30 20  .**.** Return 0 
461f0 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 31  on success and 1
46200 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 79   if there is any
46210 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72 2e 0a   kind of error..
46220 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61  */.static int pa
46230 72 73 65 4d 6f 64 69 66 69 65 72 28 63 6f 6e 73  rseModifier(cons
46240 74 20 63 68 61 72 20 2a 7a 4d 6f 64 2c 20 44 61  t char *zMod, Da
46250 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e  teTime *p){.  in
46260 74 20 72 63 20 3d 20 31 3b 0a 20 20 69 6e 74 20  t rc = 1;.  int 
46270 6e 3b 0a 20 20 64 6f 75 62 6c 65 20 72 3b 0a 20  n;.  double r;. 
46280 20 63 68 61 72 20 2a 7a 2c 20 7a 42 75 66 5b 33   char *z, zBuf[3
46290 30 5d 3b 0a 20 20 7a 20 3d 20 7a 42 75 66 3b 0a  0];.  z = zBuf;.
462a0 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c 73 69 7a    for(n=0; n<siz
462b0 65 6f 66 28 7a 42 75 66 29 2d 31 20 26 26 20 7a  eof(zBuf)-1 && z
462c0 4d 6f 64 5b 6e 5d 3b 20 6e 2b 2b 29 7b 0a 20 20  Mod[n]; n++){.  
462d0 20 20 7a 5b 6e 5d 20 3d 20 74 6f 6c 6f 77 65 72    z[n] = tolower
462e0 28 7a 4d 6f 64 5b 6e 5d 29 3b 0a 20 20 7d 0a 20  (zMod[n]);.  }. 
462f0 20 7a 5b 6e 5d 20 3d 20 30 3b 0a 20 20 73 77 69   z[n] = 0;.  swi
46300 74 63 68 28 20 7a 5b 30 5d 20 29 7b 0a 20 20 20  tch( z[0] ){.   
46310 20 63 61 73 65 20 27 6c 27 3a 20 7b 0a 20 20 20   case 'l': {.   
46320 20 20 20 2f 2a 20 20 20 20 6c 6f 63 61 6c 74 69     /*    localti
46330 6d 65 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  me.      **.    
46340 20 20 2a 2a 20 41 73 73 75 6d 69 6e 67 20 74 68    ** Assuming th
46350 65 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 76  e current time v
46360 61 6c 75 65 20 69 73 20 55 54 43 20 28 61 2e 6b  alue is UTC (a.k
46370 2e 61 2e 20 47 4d 54 29 2c 20 73 68 69 66 74 20  .a. GMT), shift 
46380 69 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 73  it to.      ** s
46390 68 6f 77 20 6c 6f 63 61 6c 20 74 69 6d 65 2e 0a  how local time..
463a0 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
463b0 66 28 20 73 74 72 63 6d 70 28 7a 2c 20 22 6c 6f  f( strcmp(z, "lo
463c0 63 61 6c 74 69 6d 65 22 29 3d 3d 30 20 29 7b 0a  caltime")==0 ){.
463d0 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a          computeJ
463e0 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d  D(p);.        p-
463f0 3e 72 4a 44 20 2b 3d 20 6c 6f 63 61 6c 74 69 6d  >rJD += localtim
46400 65 4f 66 66 73 65 74 28 70 29 3b 0a 20 20 20 20  eOffset(p);.    
46410 20 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53      clearYMD_HMS
46420 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 20  _TZ(p);.        
46430 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  rc = 0;.      }.
46440 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
46450 20 7d 0a 20 20 20 20 63 61 73 65 20 27 75 27 3a   }.    case 'u':
46460 20 7b 0a 20 20 20 20 20 20 2f 2a 0a 20 20 20 20   {.      /*.    
46470 20 20 2a 2a 20 20 20 20 75 6e 69 78 65 70 6f 63    **    unixepoc
46480 68 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  h.      **.     
46490 20 2a 2a 20 54 72 65 61 74 20 74 68 65 20 63 75   ** Treat the cu
464a0 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 70  rrent value of p
464b0 2d 3e 72 4a 44 20 61 73 20 74 68 65 20 6e 75 6d  ->rJD as the num
464c0 62 65 72 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20  ber of.      ** 
464d0 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 31 39  seconds since 19
464e0 37 30 2e 20 20 43 6f 6e 76 65 72 74 20 74 6f 20  70.  Convert to 
464f0 61 20 72 65 61 6c 20 6a 75 6c 69 61 6e 20 64 61  a real julian da
46500 79 20 6e 75 6d 62 65 72 2e 0a 20 20 20 20 20 20  y number..      
46510 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 74 72  */.      if( str
46520 63 6d 70 28 7a 2c 20 22 75 6e 69 78 65 70 6f 63  cmp(z, "unixepoc
46530 68 22 29 3d 3d 30 20 26 26 20 70 2d 3e 76 61 6c  h")==0 && p->val
46540 69 64 4a 44 20 29 7b 0a 20 20 20 20 20 20 20 20  idJD ){.        
46550 70 2d 3e 72 4a 44 20 3d 20 70 2d 3e 72 4a 44 2f  p->rJD = p->rJD/
46560 38 36 34 30 30 2e 30 20 2b 20 32 34 34 30 35 38  86400.0 + 244058
46570 37 2e 35 3b 0a 20 20 20 20 20 20 20 20 63 6c 65  7.5;.        cle
46580 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b  arYMD_HMS_TZ(p);
46590 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b  .        rc = 0;
465a0 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
465b0 20 73 74 72 63 6d 70 28 7a 2c 20 22 75 74 63 22   strcmp(z, "utc"
465c0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
465d0 64 6f 75 62 6c 65 20 63 31 3b 0a 20 20 20 20 20  double c1;.     
465e0 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b     computeJD(p);
465f0 0a 20 20 20 20 20 20 20 20 63 31 20 3d 20 6c 6f  .        c1 = lo
46600 63 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 70 29  caltimeOffset(p)
46610 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 72 4a 44  ;.        p->rJD
46620 20 2d 3d 20 63 31 3b 0a 20 20 20 20 20 20 20 20   -= c1;.        
46630 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28  clearYMD_HMS_TZ(
46640 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 72  p);.        p->r
46650 4a 44 20 2b 3d 20 63 31 20 2d 20 6c 6f 63 61 6c  JD += c1 - local
46660 74 69 6d 65 4f 66 66 73 65 74 28 70 29 3b 0a 20  timeOffset(p);. 
46670 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20         rc = 0;. 
46680 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
46690 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
466a0 73 65 20 27 77 27 3a 20 7b 0a 20 20 20 20 20 20  se 'w': {.      
466b0 2f 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 77  /*.      **    w
466c0 65 65 6b 64 61 79 20 4e 0a 20 20 20 20 20 20 2a  eekday N.      *
466d0 2a 0a 20 20 20 20 20 20 2a 2a 20 4d 6f 76 65 20  *.      ** Move 
466e0 74 68 65 20 64 61 74 65 20 74 6f 20 74 68 65 20  the date to the 
466f0 73 61 6d 65 20 74 69 6d 65 20 6f 6e 20 74 68 65  same time on the
46700 20 6e 65 78 74 20 6f 63 63 75 72 72 65 6e 63 65   next occurrence
46710 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20 77 65 65   of.      ** wee
46720 6b 64 61 79 20 4e 20 77 68 65 72 65 20 30 3d 3d  kday N where 0==
46730 53 75 6e 64 61 79 2c 20 31 3d 3d 4d 6f 6e 64 61  Sunday, 1==Monda
46740 79 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e  y, and so forth.
46750 20 20 49 66 20 74 68 65 0a 20 20 20 20 20 20 2a    If the.      *
46760 2a 20 64 61 74 65 20 69 73 20 61 6c 72 65 61 64  * date is alread
46770 79 20 6f 6e 20 74 68 65 20 61 70 70 72 6f 70 72  y on the appropr
46780 69 61 74 65 20 77 65 65 6b 64 61 79 2c 20 74 68  iate weekday, th
46790 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20  is is a no-op.. 
467a0 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66       */.      if
467b0 28 20 73 74 72 6e 63 6d 70 28 7a 2c 20 22 77 65  ( strncmp(z, "we
467c0 65 6b 64 61 79 20 22 2c 20 38 29 3d 3d 30 20 26  ekday ", 8)==0 &
467d0 26 20 67 65 74 56 61 6c 75 65 28 26 7a 5b 38 5d  & getValue(&z[8]
467e0 2c 26 72 29 3e 30 0a 20 20 20 20 20 20 20 20 20  ,&r)>0.         
467f0 20 20 20 20 20 20 20 20 26 26 20 28 6e 3d 72 29          && (n=r)
46800 3d 3d 72 20 26 26 20 6e 3e 3d 30 20 26 26 20 72  ==r && n>=0 && r
46810 3c 37 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  <7 ){.        in
46820 74 20 5a 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d  t Z;.        com
46830 70 75 74 65 59 4d 44 5f 48 4d 53 28 70 29 3b 0a  puteYMD_HMS(p);.
46840 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64          p->valid
46850 54 5a 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  TZ = 0;.        
46860 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a  p->validJD = 0;.
46870 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a          computeJ
46880 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20 5a 20  D(p);.        Z 
46890 3d 20 70 2d 3e 72 4a 44 20 2b 20 31 2e 35 3b 0a  = p->rJD + 1.5;.
468a0 20 20 20 20 20 20 20 20 5a 20 25 3d 20 37 3b 0a          Z %= 7;.
468b0 20 20 20 20 20 20 20 20 69 66 28 20 5a 3e 6e 20          if( Z>n 
468c0 29 20 5a 20 2d 3d 20 37 3b 0a 20 20 20 20 20 20  ) Z -= 7;.      
468d0 20 20 70 2d 3e 72 4a 44 20 2b 3d 20 6e 20 2d 20    p->rJD += n - 
468e0 5a 3b 0a 20 20 20 20 20 20 20 20 63 6c 65 61 72  Z;.        clear
468f0 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20  YMD_HMS_TZ(p);. 
46900 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20         rc = 0;. 
46910 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
46920 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
46930 73 65 20 27 73 27 3a 20 7b 0a 20 20 20 20 20 20  se 's': {.      
46940 2f 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 73  /*.      **    s
46950 74 61 72 74 20 6f 66 20 54 54 54 54 54 0a 20 20  tart of TTTTT.  
46960 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
46970 4d 6f 76 65 20 74 68 65 20 64 61 74 65 20 62 61  Move the date ba
46980 63 6b 77 61 72 64 73 20 74 6f 20 74 68 65 20 62  ckwards to the b
46990 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20  eginning of the 
469a0 63 75 72 72 65 6e 74 20 64 61 79 2c 0a 20 20 20  current day,.   
469b0 20 20 20 2a 2a 20 6f 72 20 6d 6f 6e 74 68 20 6f     ** or month o
469c0 72 20 79 65 61 72 2e 0a 20 20 20 20 20 20 2a 2f  r year..      */
469d0 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 6e 63  .      if( strnc
469e0 6d 70 28 7a 2c 20 22 73 74 61 72 74 20 6f 66 20  mp(z, "start of 
469f0 22 2c 20 39 29 21 3d 30 20 29 20 62 72 65 61 6b  ", 9)!=0 ) break
46a00 3b 0a 20 20 20 20 20 20 7a 20 2b 3d 20 39 3b 0a  ;.      z += 9;.
46a10 20 20 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44        computeYMD
46a20 28 70 29 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61  (p);.      p->va
46a30 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 20 20 20 20  lidHMS = 1;.    
46a40 20 20 70 2d 3e 68 20 3d 20 70 2d 3e 6d 20 3d 20    p->h = p->m = 
46a50 30 3b 0a 20 20 20 20 20 20 70 2d 3e 73 20 3d 20  0;.      p->s = 
46a60 30 2e 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61  0.0;.      p->va
46a70 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20 20 20 20  lidTZ = 0;.     
46a80 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b   p->validJD = 0;
46a90 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 63 6d  .      if( strcm
46aa0 70 28 7a 2c 22 6d 6f 6e 74 68 22 29 3d 3d 30 20  p(z,"month")==0 
46ab0 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 44 20  ){.        p->D 
46ac0 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 72 63 20  = 1;.        rc 
46ad0 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  = 0;.      }else
46ae0 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c 22 79   if( strcmp(z,"y
46af0 65 61 72 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ear")==0 ){.    
46b00 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 28 70      computeYMD(p
46b10 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 4d 20  );.        p->M 
46b20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  = 1;.        p->
46b30 44 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 72  D = 1;.        r
46b40 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c  c = 0;.      }el
46b50 73 65 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c  se if( strcmp(z,
46b60 22 64 61 79 22 29 3d 3d 30 20 29 7b 0a 20 20 20  "day")==0 ){.   
46b70 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20       rc = 0;.   
46b80 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
46b90 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
46ba0 20 27 2b 27 3a 0a 20 20 20 20 63 61 73 65 20 27   '+':.    case '
46bb0 2d 27 3a 0a 20 20 20 20 63 61 73 65 20 27 30 27  -':.    case '0'
46bc0 3a 0a 20 20 20 20 63 61 73 65 20 27 31 27 3a 0a  :.    case '1':.
46bd0 20 20 20 20 63 61 73 65 20 27 32 27 3a 0a 20 20      case '2':.  
46be0 20 20 63 61 73 65 20 27 33 27 3a 0a 20 20 20 20    case '3':.    
46bf0 63 61 73 65 20 27 34 27 3a 0a 20 20 20 20 63 61  case '4':.    ca
46c00 73 65 20 27 35 27 3a 0a 20 20 20 20 63 61 73 65  se '5':.    case
46c10 20 27 36 27 3a 0a 20 20 20 20 63 61 73 65 20 27   '6':.    case '
46c20 37 27 3a 0a 20 20 20 20 63 61 73 65 20 27 38 27  7':.    case '8'
46c30 3a 0a 20 20 20 20 63 61 73 65 20 27 39 27 3a 20  :.    case '9': 
46c40 7b 0a 20 20 20 20 20 20 6e 20 3d 20 67 65 74 56  {.      n = getV
46c50 61 6c 75 65 28 7a 2c 20 26 72 29 3b 0a 20 20 20  alue(z, &r);.   
46c60 20 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 31 20     assert( n>=1 
46c70 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 6e  );.      if( z[n
46c80 5d 3d 3d 27 3a 27 20 29 7b 0a 20 20 20 20 20 20  ]==':' ){.      
46c90 20 20 2f 2a 20 41 20 6d 6f 64 69 66 69 65 72 20    /* A modifier 
46ca0 6f 66 20 74 68 65 20 66 6f 72 6d 20 28 2b 7c 2d  of the form (+|-
46cb0 29 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 20 61 64  )HH:MM:SS.FFF ad
46cc0 64 73 20 28 6f 72 20 73 75 62 74 72 61 63 74 73  ds (or subtracts
46cd0 29 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a  ) the.        **
46ce0 20 73 70 65 63 69 66 69 65 64 20 6e 75 6d 62 65   specified numbe
46cf0 72 20 6f 66 20 68 6f 75 72 73 2c 20 6d 69 6e 75  r of hours, minu
46d00 74 65 73 2c 20 73 65 63 6f 6e 64 73 2c 20 61 6e  tes, seconds, an
46d10 64 20 66 72 61 63 74 69 6f 6e 61 6c 20 73 65 63  d fractional sec
46d20 6f 6e 64 73 0a 20 20 20 20 20 20 20 20 2a 2a 20  onds.        ** 
46d30 74 6f 20 74 68 65 20 74 69 6d 65 2e 20 20 54 68  to the time.  Th
46d40 65 20 22 2e 46 46 46 22 20 6d 61 79 20 62 65 20  e ".FFF" may be 
46d50 6f 6d 69 74 74 65 64 2e 20 20 54 68 65 20 22 3a  omitted.  The ":
46d60 53 53 2e 46 46 46 22 20 6d 61 79 20 62 65 0a 20  SS.FFF" may be. 
46d70 20 20 20 20 20 20 20 2a 2a 20 6f 6d 69 74 74 65         ** omitte
46d80 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  d..        */.  
46d90 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
46da0 20 2a 7a 32 20 3d 20 7a 3b 0a 20 20 20 20 20 20   *z2 = z;.      
46db0 20 20 44 61 74 65 54 69 6d 65 20 74 78 3b 0a 20    DateTime tx;. 
46dc0 20 20 20 20 20 20 20 69 6e 74 20 64 61 79 3b 0a         int day;.
46dd0 20 20 20 20 20 20 20 20 69 66 28 20 21 69 73 64          if( !isd
46de0 69 67 69 74 28 2a 28 75 38 2a 29 7a 32 29 20 29  igit(*(u8*)z2) )
46df0 20 7a 32 2b 2b 3b 0a 20 20 20 20 20 20 20 20 6d   z2++;.        m
46e00 65 6d 73 65 74 28 26 74 78 2c 20 30 2c 20 73 69  emset(&tx, 0, si
46e10 7a 65 6f 66 28 74 78 29 29 3b 0a 20 20 20 20 20  zeof(tx));.     
46e20 20 20 20 69 66 28 20 70 61 72 73 65 48 68 4d 6d     if( parseHhMm
46e30 53 73 28 7a 32 2c 20 26 74 78 29 20 29 20 62 72  Ss(z2, &tx) ) br
46e40 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d  eak;.        com
46e50 70 75 74 65 4a 44 28 26 74 78 29 3b 0a 20 20 20  puteJD(&tx);.   
46e60 20 20 20 20 20 74 78 2e 72 4a 44 20 2d 3d 20 30       tx.rJD -= 0
46e70 2e 35 3b 0a 20 20 20 20 20 20 20 20 64 61 79 20  .5;.        day 
46e80 3d 20 28 69 6e 74 29 74 78 2e 72 4a 44 3b 0a 20  = (int)tx.rJD;. 
46e90 20 20 20 20 20 20 20 74 78 2e 72 4a 44 20 2d 3d         tx.rJD -=
46ea0 20 64 61 79 3b 0a 20 20 20 20 20 20 20 20 69 66   day;.        if
46eb0 28 20 7a 5b 30 5d 3d 3d 27 2d 27 20 29 20 74 78  ( z[0]=='-' ) tx
46ec0 2e 72 4a 44 20 3d 20 2d 74 78 2e 72 4a 44 3b 0a  .rJD = -tx.rJD;.
46ed0 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a          computeJ
46ee0 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20 63 6c  D(p);.        cl
46ef0 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29  earYMD_HMS_TZ(p)
46f00 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 72 4a 44  ;.        p->rJD
46f10 20 2b 3d 20 74 78 2e 72 4a 44 3b 0a 20 20 20 20   += tx.rJD;.    
46f20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20      rc = 0;.    
46f30 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
46f40 20 7d 0a 20 20 20 20 20 20 7a 20 2b 3d 20 6e 3b   }.      z += n;
46f50 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 69 73  .      while( is
46f60 73 70 61 63 65 28 2a 28 75 38 2a 29 7a 29 20 29  space(*(u8*)z) )
46f70 20 7a 2b 2b 3b 0a 20 20 20 20 20 20 6e 20 3d 20   z++;.      n = 
46f80 73 74 72 6c 65 6e 28 7a 29 3b 0a 20 20 20 20 20  strlen(z);.     
46f90 20 69 66 28 20 6e 3e 31 30 20 7c 7c 20 6e 3c 33   if( n>10 || n<3
46fa0 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
46fb0 69 66 28 20 7a 5b 6e 2d 31 5d 3d 3d 27 73 27 20  if( z[n-1]=='s' 
46fc0 29 7b 20 7a 5b 6e 2d 31 5d 20 3d 20 30 3b 20 6e  ){ z[n-1] = 0; n
46fd0 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 63 6f 6d 70  --; }.      comp
46fe0 75 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20  uteJD(p);.      
46ff0 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66  rc = 0;.      if
47000 28 20 6e 3d 3d 33 20 26 26 20 73 74 72 63 6d 70  ( n==3 && strcmp
47010 28 7a 2c 22 64 61 79 22 29 3d 3d 30 20 29 7b 0a  (z,"day")==0 ){.
47020 20 20 20 20 20 20 20 20 70 2d 3e 72 4a 44 20 2b          p->rJD +
47030 3d 20 72 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  = r;.      }else
47040 20 69 66 28 20 6e 3d 3d 34 20 26 26 20 73 74 72   if( n==4 && str
47050 63 6d 70 28 7a 2c 22 68 6f 75 72 22 29 3d 3d 30  cmp(z,"hour")==0
47060 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 72   ){.        p->r
47070 4a 44 20 2b 3d 20 72 2f 32 34 2e 30 3b 0a 20 20  JD += r/24.0;.  
47080 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d      }else if( n=
47090 3d 36 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22  =6 && strcmp(z,"
470a0 6d 69 6e 75 74 65 22 29 3d 3d 30 20 29 7b 0a 20  minute")==0 ){. 
470b0 20 20 20 20 20 20 20 70 2d 3e 72 4a 44 20 2b 3d         p->rJD +=
470c0 20 72 2f 28 32 34 2e 30 2a 36 30 2e 30 29 3b 0a   r/(24.0*60.0);.
470d0 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
470e0 6e 3d 3d 36 20 26 26 20 73 74 72 63 6d 70 28 7a  n==6 && strcmp(z
470f0 2c 22 73 65 63 6f 6e 64 22 29 3d 3d 30 20 29 7b  ,"second")==0 ){
47100 0a 20 20 20 20 20 20 20 20 70 2d 3e 72 4a 44 20  .        p->rJD 
47110 2b 3d 20 72 2f 28 32 34 2e 30 2a 36 30 2e 30 2a  += r/(24.0*60.0*
47120 36 30 2e 30 29 3b 0a 20 20 20 20 20 20 7d 65 6c  60.0);.      }el
47130 73 65 20 69 66 28 20 6e 3d 3d 35 20 26 26 20 73  se if( n==5 && s
47140 74 72 63 6d 70 28 7a 2c 22 6d 6f 6e 74 68 22 29  trcmp(z,"month")
47150 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ==0 ){.        i
47160 6e 74 20 78 2c 20 79 3b 0a 20 20 20 20 20 20 20  nt x, y;.       
47170 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28   computeYMD_HMS(
47180 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 4d  p);.        p->M
47190 20 2b 3d 20 72 3b 0a 20 20 20 20 20 20 20 20 78   += r;.        x
471a0 20 3d 20 70 2d 3e 4d 3e 30 20 3f 20 28 70 2d 3e   = p->M>0 ? (p->
471b0 4d 2d 31 29 2f 31 32 20 3a 20 28 70 2d 3e 4d 2d  M-1)/12 : (p->M-
471c0 31 32 29 2f 31 32 3b 0a 20 20 20 20 20 20 20 20  12)/12;.        
471d0 70 2d 3e 59 20 2b 3d 20 78 3b 0a 20 20 20 20 20  p->Y += x;.     
471e0 20 20 20 70 2d 3e 4d 20 2d 3d 20 78 2a 31 32 3b     p->M -= x*12;
471f0 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c 69  .        p->vali
47200 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  dJD = 0;.       
47210 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20   computeJD(p);. 
47220 20 20 20 20 20 20 20 79 20 3d 20 72 3b 0a 20 20         y = r;.  
47230 20 20 20 20 20 20 69 66 28 20 79 21 3d 72 20 29        if( y!=r )
47240 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 72  {.          p->r
47250 4a 44 20 2b 3d 20 28 72 20 2d 20 79 29 2a 33 30  JD += (r - y)*30
47260 2e 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  .0;.        }.  
47270 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d      }else if( n=
47280 3d 34 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22  =4 && strcmp(z,"
47290 79 65 61 72 22 29 3d 3d 30 20 29 7b 0a 20 20 20  year")==0 ){.   
472a0 20 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f       computeYMD_
472b0 48 4d 53 28 70 29 3b 0a 20 20 20 20 20 20 20 20  HMS(p);.        
472c0 70 2d 3e 59 20 2b 3d 20 72 3b 0a 20 20 20 20 20  p->Y += r;.     
472d0 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20     p->validJD = 
472e0 30 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75  0;.        compu
472f0 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 7d  teJD(p);.      }
47300 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63  else{.        rc
47310 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 1;.      }.  
47320 20 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53      clearYMD_HMS
47330 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 62 72  _TZ(p);.      br
47340 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  eak;.    }.    d
47350 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20  efault: {.      
47360 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
47370 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
47380 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 20 74  ./*.** Process t
47390 69 6d 65 20 66 75 6e 63 74 69 6f 6e 20 61 72 67  ime function arg
473a0 75 6d 65 6e 74 73 2e 20 20 61 72 67 76 5b 30 5d  uments.  argv[0]
473b0 20 69 73 20 61 20 64 61 74 65 2d 74 69 6d 65 20   is a date-time 
473c0 73 74 61 6d 70 2e 0a 2a 2a 20 61 72 67 76 5b 31  stamp..** argv[1
473d0 5d 20 61 6e 64 20 66 6f 6c 6c 6f 77 69 6e 67 20  ] and following 
473e0 61 72 65 20 6d 6f 64 69 66 69 65 72 73 2e 20 20  are modifiers.  
473f0 50 61 72 73 65 20 74 68 65 6d 20 61 6c 6c 20 61  Parse them all a
47400 6e 64 20 77 72 69 74 65 0a 2a 2a 20 74 68 65 20  nd write.** the 
47410 72 65 73 75 6c 74 69 6e 67 20 74 69 6d 65 20 69  resulting time i
47420 6e 74 6f 20 74 68 65 20 44 61 74 65 54 69 6d 65  nto the DateTime
47430 20 73 74 72 75 63 74 75 72 65 20 70 2e 20 20 52   structure p.  R
47440 65 74 75 72 6e 20 30 0a 2a 2a 20 6f 6e 20 73 75  eturn 0.** on su
47450 63 63 65 73 73 20 61 6e 64 20 31 20 69 66 20 74  ccess and 1 if t
47460 68 65 72 65 20 61 72 65 20 61 6e 79 20 65 72 72  here are any err
47470 6f 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ors..*/.static i
47480 6e 74 20 69 73 44 61 74 65 28 69 6e 74 20 61 72  nt isDate(int ar
47490 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
474a0 65 20 2a 2a 61 72 67 76 2c 20 44 61 74 65 54 69  e **argv, DateTi
474b0 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b  me *p){.  int i;
474c0 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  .  const unsigne
474d0 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69 66 28  d char *z;.  if(
474e0 20 61 72 67 63 3d 3d 30 20 29 20 72 65 74 75 72   argc==0 ) retur
474f0 6e 20 31 3b 0a 20 20 69 66 28 20 28 7a 20 3d 20  n 1;.  if( (z = 
47500 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
47510 78 74 28 61 72 67 76 5b 30 5d 29 29 3d 3d 30 20  xt(argv[0]))==0 
47520 7c 7c 20 70 61 72 73 65 44 61 74 65 4f 72 54 69  || parseDateOrTi
47530 6d 65 28 28 63 68 61 72 2a 29 7a 2c 20 70 29 20  me((char*)z, p) 
47540 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  ){.    return 1;
47550 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 31 3b 20  .  }.  for(i=1; 
47560 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20  i<argc; i++){.  
47570 20 20 69 66 28 20 28 7a 20 3d 20 73 71 6c 69 74    if( (z = sqlit
47580 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
47590 67 76 5b 69 5d 29 29 3d 3d 30 20 7c 7c 20 70 61  gv[i]))==0 || pa
475a0 72 73 65 4d 6f 64 69 66 69 65 72 28 28 63 68 61  rseModifier((cha
475b0 72 2a 29 7a 2c 20 70 29 20 29 7b 0a 20 20 20 20  r*)z, p) ){.    
475c0 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
475d0 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  }.  }.  return 0
475e0 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ;.}.../*.** The 
475f0 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e  following routin
47600 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  es implement the
47610 20 76 61 72 69 6f 75 73 20 64 61 74 65 20 61 6e   various date an
47620 64 20 74 69 6d 65 20 66 75 6e 63 74 69 6f 6e 73  d time functions
47630 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a  .** of SQLite..*
47640 2f 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 6a 75 6c 69  /../*.**    juli
47650 61 6e 64 61 79 28 20 54 49 4d 45 53 54 52 49 4e  anday( TIMESTRIN
47660 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e  G, MOD, MOD, ...
47670 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ).**.** Return t
47680 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75  he julian day nu
47690 6d 62 65 72 20 6f 66 20 74 68 65 20 64 61 74 65  mber of the date
476a0 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68   specified in th
476b0 65 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2f 0a 73  e arguments.*/.s
476c0 74 61 74 69 63 20 76 6f 69 64 20 6a 75 6c 69 61  tatic void julia
476d0 6e 64 61 79 46 75 6e 63 28 0a 20 20 73 71 6c 69  ndayFunc(.  sqli
476e0 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
476f0 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63  text,.  int argc
47700 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
47710 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61  e **argv.){.  Da
47720 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 66 28 20  teTime x;.  if( 
47730 69 73 44 61 74 65 28 61 72 67 63 2c 20 61 72 67  isDate(argc, arg
47740 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20 20  v, &x)==0 ){.   
47750 20 63 6f 6d 70 75 74 65 4a 44 28 26 78 29 3b 0a   computeJD(&x);.
47760 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
47770 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78  lt_double(contex
47780 74 2c 20 78 2e 72 4a 44 29 3b 0a 20 20 7d 0a 7d  t, x.rJD);.  }.}
47790 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 64 61 74 65 74  ../*.**    datet
477a0 69 6d 65 28 20 54 49 4d 45 53 54 52 49 4e 47 2c  ime( TIMESTRING,
477b0 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a   MOD, MOD, ...).
477c0 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 59 59 59  **.** Return YYY
477d0 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53  Y-MM-DD HH:MM:SS
477e0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
477f0 64 61 74 65 74 69 6d 65 46 75 6e 63 28 0a 20 20  datetimeFunc(.  
47800 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
47810 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
47820 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f  argc,.  sqlite3_
47830 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
47840 20 20 44 61 74 65 54 69 6d 65 20 78 3b 0a 20 20    DateTime x;.  
47850 69 66 28 20 69 73 44 61 74 65 28 61 72 67 63 2c  if( isDate(argc,
47860 20 61 72 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b   argv, &x)==0 ){
47870 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b 31  .    char zBuf[1
47880 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75 74 65  00];.    compute
47890 59 4d 44 5f 48 4d 53 28 26 78 29 3b 0a 20 20 20  YMD_HMS(&x);.   
478a0 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
478b0 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20  f(sizeof(zBuf), 
478c0 7a 42 75 66 2c 20 22 25 30 34 64 2d 25 30 32 64  zBuf, "%04d-%02d
478d0 2d 25 30 32 64 20 25 30 32 64 3a 25 30 32 64 3a  -%02d %02d:%02d:
478e0 25 30 32 64 22 2c 0a 20 20 20 20 20 20 20 20 20  %02d",.         
478f0 20 20 20 20 20 20 20 20 20 20 20 20 78 2e 59 2c              x.Y,
47900 20 78 2e 4d 2c 20 78 2e 44 2c 20 78 2e 68 2c 20   x.M, x.D, x.h, 
47910 78 2e 6d 2c 20 28 69 6e 74 29 28 78 2e 73 29 29  x.m, (int)(x.s))
47920 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ;.    sqlite3_re
47930 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78  sult_text(contex
47940 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c  t, zBuf, -1, SQL
47950 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a  ITE_TRANSIENT);.
47960 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20    }.}../*.**    
47970 74 69 6d 65 28 20 54 49 4d 45 53 54 52 49 4e 47  time( TIMESTRING
47980 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29  , MOD, MOD, ...)
47990 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 48 48  .**.** Return HH
479a0 3a 4d 4d 3a 53 53 0a 2a 2f 0a 73 74 61 74 69 63  :MM:SS.*/.static
479b0 20 76 6f 69 64 20 74 69 6d 65 46 75 6e 63 28 0a   void timeFunc(.
479c0 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
479d0 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
479e0 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65  t argc,.  sqlite
479f0 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
47a00 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78 3b 0a  {.  DateTime x;.
47a10 20 20 69 66 28 20 69 73 44 61 74 65 28 61 72 67    if( isDate(arg
47a20 63 2c 20 61 72 67 76 2c 20 26 78 29 3d 3d 30 20  c, argv, &x)==0 
47a30 29 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66  ){.    char zBuf
47a40 5b 31 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75  [100];.    compu
47a50 74 65 48 4d 53 28 26 78 29 3b 0a 20 20 20 20 73  teHMS(&x);.    s
47a60 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
47a70 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42  sizeof(zBuf), zB
47a80 75 66 2c 20 22 25 30 32 64 3a 25 30 32 64 3a 25  uf, "%02d:%02d:%
47a90 30 32 64 22 2c 20 78 2e 68 2c 20 78 2e 6d 2c 20  02d", x.h, x.m, 
47aa0 28 69 6e 74 29 78 2e 73 29 3b 0a 20 20 20 20 73  (int)x.s);.    s
47ab0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
47ac0 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66  xt(context, zBuf
47ad0 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41  , -1, SQLITE_TRA
47ae0 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a  NSIENT);.  }.}..
47af0 2f 2a 0a 2a 2a 20 20 20 20 64 61 74 65 28 20 54  /*.**    date( T
47b00 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20  IMESTRING, MOD, 
47b10 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20  MOD, ...).**.** 
47b20 52 65 74 75 72 6e 20 59 59 59 59 2d 4d 4d 2d 44  Return YYYY-MM-D
47b30 44 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  D.*/.static void
47b40 20 64 61 74 65 46 75 6e 63 28 0a 20 20 73 71 6c   dateFunc(.  sql
47b50 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
47b60 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67  ntext,.  int arg
47b70 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  c,.  sqlite3_val
47b80 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44  ue **argv.){.  D
47b90 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 66 28  ateTime x;.  if(
47ba0 20 69 73 44 61 74 65 28 61 72 67 63 2c 20 61 72   isDate(argc, ar
47bb0 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20  gv, &x)==0 ){.  
47bc0 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d    char zBuf[100]
47bd0 3b 0a 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44  ;.    computeYMD
47be0 28 26 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  (&x);.    sqlite
47bf0 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
47c00 66 28 7a 42 75 66 29 2c 20 7a 42 75 66 2c 20 22  f(zBuf), zBuf, "
47c10 25 30 34 64 2d 25 30 32 64 2d 25 30 32 64 22 2c  %04d-%02d-%02d",
47c20 20 78 2e 59 2c 20 78 2e 4d 2c 20 78 2e 44 29 3b   x.Y, x.M, x.D);
47c30 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
47c40 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
47c50 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49  , zBuf, -1, SQLI
47c60 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20  TE_TRANSIENT);. 
47c70 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 73   }.}../*.**    s
47c80 74 72 66 74 69 6d 65 28 20 46 4f 52 4d 41 54 2c  trftime( FORMAT,
47c90 20 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44   TIMESTRING, MOD
47ca0 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a  , MOD, ...).**.*
47cb0 2a 20 52 65 74 75 72 6e 20 61 20 73 74 72 69 6e  * Return a strin
47cc0 67 20 64 65 73 63 72 69 62 65 64 20 62 79 20 46  g described by F
47cd0 4f 52 4d 41 54 2e 20 20 43 6f 6e 76 65 72 73 69  ORMAT.  Conversi
47ce0 6f 6e 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  ons as follows:.
47cf0 2a 2a 0a 2a 2a 20 20 20 25 64 20 20 64 61 79 20  **.**   %d  day 
47d00 6f 66 20 6d 6f 6e 74 68 0a 2a 2a 20 20 20 25 66  of month.**   %f
47d10 20 20 2a 2a 20 66 72 61 63 74 69 6f 6e 61 6c 20    ** fractional 
47d20 73 65 63 6f 6e 64 73 20 20 53 53 2e 53 53 53 0a  seconds  SS.SSS.
47d30 2a 2a 20 20 20 25 48 20 20 68 6f 75 72 20 30 30  **   %H  hour 00
47d40 2d 32 34 0a 2a 2a 20 20 20 25 6a 20 20 64 61 79  -24.**   %j  day
47d50 20 6f 66 20 79 65 61 72 20 30 30 30 2d 33 36 36   of year 000-366
47d60 0a 2a 2a 20 20 20 25 4a 20 20 2a 2a 20 4a 75 6c  .**   %J  ** Jul
47d70 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 0a 2a  ian day number.*
47d80 2a 20 20 20 25 6d 20 20 6d 6f 6e 74 68 20 30 31  *   %m  month 01
47d90 2d 31 32 0a 2a 2a 20 20 20 25 4d 20 20 6d 69 6e  -12.**   %M  min
47da0 75 74 65 20 30 30 2d 35 39 0a 2a 2a 20 20 20 25  ute 00-59.**   %
47db0 73 20 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 65  s  seconds since
47dc0 20 31 39 37 30 2d 30 31 2d 30 31 0a 2a 2a 20 20   1970-01-01.**  
47dd0 20 25 53 20 20 73 65 63 6f 6e 64 73 20 30 30 2d   %S  seconds 00-
47de0 35 39 0a 2a 2a 20 20 20 25 77 20 20 64 61 79 20  59.**   %w  day 
47df0 6f 66 20 77 65 65 6b 20 30 2d 36 20 20 73 75 6e  of week 0-6  sun
47e00 64 61 79 3d 3d 30 0a 2a 2a 20 20 20 25 57 20 20  day==0.**   %W  
47e10 77 65 65 6b 20 6f 66 20 79 65 61 72 20 30 30 2d  week of year 00-
47e20 35 33 0a 2a 2a 20 20 20 25 59 20 20 79 65 61 72  53.**   %Y  year
47e30 20 30 30 30 30 2d 39 39 39 39 0a 2a 2a 20 20 20   0000-9999.**   
47e40 25 25 20 20 25 0a 2a 2f 0a 73 74 61 74 69 63 20  %%  %.*/.static 
47e50 76 6f 69 64 20 73 74 72 66 74 69 6d 65 46 75 6e  void strftimeFun
47e60 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
47e70 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
47e80 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c   int argc,.  sql
47e90 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
47ea0 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20  v.){.  DateTime 
47eb0 78 3b 0a 20 20 75 36 34 20 6e 3b 0a 20 20 69 6e  x;.  u64 n;.  in
47ec0 74 20 69 2c 20 6a 3b 0a 20 20 63 68 61 72 20 2a  t i, j;.  char *
47ed0 7a 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  z;.  const char 
47ee0 2a 7a 46 6d 74 20 3d 20 28 63 6f 6e 73 74 20 63  *zFmt = (const c
47ef0 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  har*)sqlite3_val
47f00 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
47f10 3b 0a 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30  ;.  char zBuf[10
47f20 30 5d 3b 0a 20 20 69 66 28 20 7a 46 6d 74 3d 3d  0];.  if( zFmt==
47f30 30 20 7c 7c 20 69 73 44 61 74 65 28 61 72 67 63  0 || isDate(argc
47f40 2d 31 2c 20 61 72 67 76 2b 31 2c 20 26 78 29 20  -1, argv+1, &x) 
47f50 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28  ) return;.  for(
47f60 69 3d 30 2c 20 6e 3d 31 3b 20 7a 46 6d 74 5b 69  i=0, n=1; zFmt[i
47f70 5d 3b 20 69 2b 2b 2c 20 6e 2b 2b 29 7b 0a 20 20  ]; i++, n++){.  
47f80 20 20 69 66 28 20 7a 46 6d 74 5b 69 5d 3d 3d 27    if( zFmt[i]=='
47f90 25 27 20 29 7b 0a 20 20 20 20 20 20 73 77 69 74  %' ){.      swit
47fa0 63 68 28 20 7a 46 6d 74 5b 69 2b 31 5d 20 29 7b  ch( zFmt[i+1] ){
47fb0 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 64  .        case 'd
47fc0 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  ':.        case 
47fd0 27 48 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73  'H':.        cas
47fe0 65 20 27 6d 27 3a 0a 20 20 20 20 20 20 20 20 63  e 'm':.        c
47ff0 61 73 65 20 27 4d 27 3a 0a 20 20 20 20 20 20 20  ase 'M':.       
48000 20 63 61 73 65 20 27 53 27 3a 0a 20 20 20 20 20   case 'S':.     
48010 20 20 20 63 61 73 65 20 27 57 27 3a 0a 20 20 20     case 'W':.   
48020 20 20 20 20 20 20 20 6e 2b 2b 3b 0a 20 20 20 20         n++;.    
48030 20 20 20 20 20 20 2f 2a 20 66 61 6c 6c 20 74 68        /* fall th
48040 72 75 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 61  ru */.        ca
48050 73 65 20 27 77 27 3a 0a 20 20 20 20 20 20 20 20  se 'w':.        
48060 63 61 73 65 20 27 25 27 3a 0a 20 20 20 20 20 20  case '%':.      
48070 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
48080 20 20 20 63 61 73 65 20 27 66 27 3a 0a 20 20 20     case 'f':.   
48090 20 20 20 20 20 20 20 6e 20 2b 3d 20 38 3b 0a 20         n += 8;. 
480a0 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
480b0 20 20 20 20 20 20 20 20 63 61 73 65 20 27 6a 27          case 'j'
480c0 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d  :.          n +=
480d0 20 33 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72   3;.          br
480e0 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73  eak;.        cas
480f0 65 20 27 59 27 3a 0a 20 20 20 20 20 20 20 20 20  e 'Y':.         
48100 20 6e 20 2b 3d 20 38 3b 0a 20 20 20 20 20 20 20   n += 8;.       
48110 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
48120 20 20 63 61 73 65 20 27 73 27 3a 0a 20 20 20 20    case 's':.    
48130 20 20 20 20 63 61 73 65 20 27 4a 27 3a 0a 20 20      case 'J':.  
48140 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 35 30 3b          n += 50;
48150 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
48160 3b 0a 20 20 20 20 20 20 20 20 64 65 66 61 75 6c  ;.        defaul
48170 74 3a 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  t:.          ret
48180 75 72 6e 3b 20 20 2f 2a 20 45 52 52 4f 52 2e 20  urn;  /* ERROR. 
48190 20 72 65 74 75 72 6e 20 61 20 4e 55 4c 4c 20 2a   return a NULL *
481a0 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  /.      }.      
481b0 69 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  i++;.    }.  }. 
481c0 20 69 66 28 20 6e 3c 73 69 7a 65 6f 66 28 7a 42   if( n<sizeof(zB
481d0 75 66 29 20 29 7b 0a 20 20 20 20 7a 20 3d 20 7a  uf) ){.    z = z
481e0 42 75 66 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  Buf;.  }else if(
481f0 20 6e 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45   n>SQLITE_MAX_LE
48200 4e 47 54 48 20 29 7b 0a 20 20 20 20 73 71 6c 69  NGTH ){.    sqli
48210 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
48220 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78 74 29  _toobig(context)
48230 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
48240 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20 73  }else{.    z = s
48250 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 20 6e 20 29  qliteMalloc( n )
48260 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29  ;.    if( z==0 )
48270 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 63   return;.  }.  c
48280 6f 6d 70 75 74 65 4a 44 28 26 78 29 3b 0a 20 20  omputeJD(&x);.  
48290 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 26  computeYMD_HMS(&
482a0 78 29 3b 0a 20 20 66 6f 72 28 69 3d 6a 3d 30 3b  x);.  for(i=j=0;
482b0 20 7a 46 6d 74 5b 69 5d 3b 20 69 2b 2b 29 7b 0a   zFmt[i]; i++){.
482c0 20 20 20 20 69 66 28 20 7a 46 6d 74 5b 69 5d 21      if( zFmt[i]!
482d0 3d 27 25 27 20 29 7b 0a 20 20 20 20 20 20 7a 5b  ='%' ){.      z[
482e0 6a 2b 2b 5d 20 3d 20 7a 46 6d 74 5b 69 5d 3b 0a  j++] = zFmt[i];.
482f0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
48300 20 69 2b 2b 3b 0a 20 20 20 20 20 20 73 77 69 74   i++;.      swit
48310 63 68 28 20 7a 46 6d 74 5b 69 5d 20 29 7b 0a 20  ch( zFmt[i] ){. 
48320 20 20 20 20 20 20 20 63 61 73 65 20 27 64 27 3a         case 'd':
48330 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
48340 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32  tf(3, &z[j],"%02
48350 64 22 2c 78 2e 44 29 3b 20 6a 2b 3d 32 3b 20 62  d",x.D); j+=2; b
48360 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61  reak;.        ca
48370 73 65 20 27 66 27 3a 20 7b 0a 20 20 20 20 20 20  se 'f': {.      
48380 20 20 20 20 64 6f 75 62 6c 65 20 73 20 3d 20 78      double s = x
48390 2e 73 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  .s;.          if
483a0 28 20 73 3e 35 39 2e 39 39 39 20 29 20 73 20 3d  ( s>59.999 ) s =
483b0 20 35 39 2e 39 39 39 3b 0a 20 20 20 20 20 20 20   59.999;.       
483c0 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
483d0 6e 74 66 28 37 2c 20 26 7a 5b 6a 5d 2c 22 25 30  ntf(7, &z[j],"%0
483e0 36 2e 33 66 22 2c 20 73 29 3b 0a 20 20 20 20 20  6.3f", s);.     
483f0 20 20 20 20 20 6a 20 2b 3d 20 73 74 72 6c 65 6e       j += strlen
48400 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 20  (&z[j]);.       
48410 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
48420 20 20 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65    }.        case
48430 20 27 48 27 3a 20 20 73 71 6c 69 74 65 33 5f 73   'H':  sqlite3_s
48440 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d  nprintf(3, &z[j]
48450 2c 22 25 30 32 64 22 2c 78 2e 68 29 3b 20 6a 2b  ,"%02d",x.h); j+
48460 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  =2; break;.     
48470 20 20 20 63 61 73 65 20 27 57 27 3a 20 2f 2a 20     case 'W': /* 
48480 46 61 6c 6c 20 74 68 72 75 20 2a 2f 0a 20 20 20  Fall thru */.   
48490 20 20 20 20 20 63 61 73 65 20 27 6a 27 3a 20 7b       case 'j': {
484a0 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e  .          int n
484b0 44 61 79 3b 20 20 20 20 20 20 20 20 20 20 20 20  Day;            
484c0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 61   /* Number of da
484d0 79 73 20 73 69 6e 63 65 20 31 73 74 20 64 61 79  ys since 1st day
484e0 20 6f 66 20 79 65 61 72 20 2a 2f 0a 20 20 20 20   of year */.    
484f0 20 20 20 20 20 20 44 61 74 65 54 69 6d 65 20 79        DateTime y
48500 20 3d 20 78 3b 0a 20 20 20 20 20 20 20 20 20 20   = x;.          
48510 79 2e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20  y.validJD = 0;. 
48520 20 20 20 20 20 20 20 20 20 79 2e 4d 20 3d 20 31           y.M = 1
48530 3b 0a 20 20 20 20 20 20 20 20 20 20 79 2e 44 20  ;.          y.D 
48540 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 63  = 1;.          c
48550 6f 6d 70 75 74 65 4a 44 28 26 79 29 3b 0a 20 20  omputeJD(&y);.  
48560 20 20 20 20 20 20 20 20 6e 44 61 79 20 3d 20 78          nDay = x
48570 2e 72 4a 44 20 2d 20 79 2e 72 4a 44 20 2b 20 30  .rJD - y.rJD + 0
48580 2e 35 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  .5;.          if
48590 28 20 7a 46 6d 74 5b 69 5d 3d 3d 27 57 27 20 29  ( zFmt[i]=='W' )
485a0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e  {.            in
485b0 74 20 77 64 3b 20 20 20 2f 2a 20 30 3d 4d 6f 6e  t wd;   /* 0=Mon
485c0 64 61 79 2c 20 31 3d 54 75 65 73 64 61 79 2c 20  day, 1=Tuesday, 
485d0 2e 2e 2e 20 36 3d 53 75 6e 64 61 79 20 2a 2f 0a  ... 6=Sunday */.
485e0 20 20 20 20 20 20 20 20 20 20 20 20 77 64 20 3d              wd =
485f0 20 28 28 69 6e 74 29 28 78 2e 72 4a 44 2b 30 2e   ((int)(x.rJD+0.
48600 35 29 29 20 25 20 37 3b 0a 20 20 20 20 20 20 20  5)) % 7;.       
48610 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
48620 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22  rintf(3, &z[j],"
48630 25 30 32 64 22 2c 28 6e 44 61 79 2b 37 2d 77 64  %02d",(nDay+7-wd
48640 29 2f 37 29 3b 0a 20 20 20 20 20 20 20 20 20 20  )/7);.          
48650 20 20 6a 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20    j += 2;.      
48660 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
48670 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73         sqlite3_s
48680 6e 70 72 69 6e 74 66 28 34 2c 20 26 7a 5b 6a 5d  nprintf(4, &z[j]
48690 2c 22 25 30 33 64 22 2c 6e 44 61 79 2b 31 29 3b  ,"%03d",nDay+1);
486a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 6a 20 2b  .            j +
486b0 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  = 3;.          }
486c0 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
486d0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
486e0 20 20 20 20 63 61 73 65 20 27 4a 27 3a 20 7b 0a      case 'J': {.
486f0 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
48700 33 5f 73 6e 70 72 69 6e 74 66 28 32 30 2c 20 26  3_snprintf(20, &
48710 7a 5b 6a 5d 2c 22 25 2e 31 36 67 22 2c 78 2e 72  z[j],"%.16g",x.r
48720 4a 44 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6a  JD);.          j
48730 2b 3d 73 74 72 6c 65 6e 28 26 7a 5b 6a 5d 29 3b  +=strlen(&z[j]);
48740 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
48750 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
48760 20 20 20 20 63 61 73 65 20 27 6d 27 3a 20 20 73      case 'm':  s
48770 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
48780 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c  3, &z[j],"%02d",
48790 78 2e 4d 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61  x.M); j+=2; brea
487a0 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  k;.        case 
487b0 27 4d 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e  'M':  sqlite3_sn
487c0 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c  printf(3, &z[j],
487d0 22 25 30 32 64 22 2c 78 2e 6d 29 3b 20 6a 2b 3d  "%02d",x.m); j+=
487e0 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20  2; break;.      
487f0 20 20 63 61 73 65 20 27 73 27 3a 20 7b 0a 20 20    case 's': {.  
48800 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
48810 73 6e 70 72 69 6e 74 66 28 33 30 2c 26 7a 5b 6a  snprintf(30,&z[j
48820 5d 2c 22 25 64 22 2c 0a 20 20 20 20 20 20 20 20  ],"%d",.        
48830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48840 20 20 20 28 69 6e 74 29 28 28 78 2e 72 4a 44 2d     (int)((x.rJD-
48850 32 34 34 30 35 38 37 2e 35 29 2a 38 36 34 30 30  2440587.5)*86400
48860 2e 30 20 2b 20 30 2e 35 29 29 3b 0a 20 20 20 20  .0 + 0.5));.    
48870 20 20 20 20 20 20 6a 20 2b 3d 20 73 74 72 6c 65        j += strle
48880 6e 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 20  n(&z[j]);.      
48890 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
488a0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 61 73     }.        cas
488b0 65 20 27 53 27 3a 20 20 73 71 6c 69 74 65 33 5f  e 'S':  sqlite3_
488c0 73 6e 70 72 69 6e 74 66 28 33 2c 26 7a 5b 6a 5d  snprintf(3,&z[j]
488d0 2c 22 25 30 32 64 22 2c 28 69 6e 74 29 78 2e 73  ,"%02d",(int)x.s
488e0 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a  ); j+=2; break;.
488f0 20 20 20 20 20 20 20 20 63 61 73 65 20 27 77 27          case 'w'
48900 3a 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 28 28 28 69  :  z[j++] = (((i
48910 6e 74 29 28 78 2e 72 4a 44 2b 31 2e 35 29 29 20  nt)(x.rJD+1.5)) 
48920 25 20 37 29 20 2b 20 27 30 27 3b 20 62 72 65 61  % 7) + '0'; brea
48930 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  k;.        case 
48940 27 59 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e  'Y':  sqlite3_sn
48950 70 72 69 6e 74 66 28 35 2c 26 7a 5b 6a 5d 2c 22  printf(5,&z[j],"
48960 25 30 34 64 22 2c 78 2e 59 29 3b 20 6a 2b 3d 73  %04d",x.Y); j+=s
48970 74 72 6c 65 6e 28 26 7a 5b 6a 5d 29 3b 62 72 65  trlen(&z[j]);bre
48980 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65  ak;.        case
48990 20 27 25 27 3a 20 20 7a 5b 6a 2b 2b 5d 20 3d 20   '%':  z[j++] = 
489a0 27 25 27 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20  '%'; break;.    
489b0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
489c0 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69  z[j] = 0;.  sqli
489d0 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
489e0 63 6f 6e 74 65 78 74 2c 20 7a 2c 20 2d 31 2c 20  context, z, -1, 
489f0 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
48a00 29 3b 0a 20 20 69 66 28 20 7a 21 3d 7a 42 75 66  );.  if( z!=zBuf
48a10 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 46 72   ){.    sqliteFr
48a20 65 65 28 7a 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ee(z);.  }.}../*
48a30 0a 2a 2a 20 63 75 72 72 65 6e 74 5f 74 69 6d 65  .** current_time
48a40 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  ().**.** This fu
48a50 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74  nction returns t
48a60 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 61 73  he same value as
48a70 20 74 69 6d 65 28 27 6e 6f 77 27 29 2e 0a 2a 2f   time('now')..*/
48a80 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 74 69  .static void cti
48a90 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  meFunc(.  sqlite
48aa0 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
48ab0 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
48ac0 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
48ad0 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 73 71 6c 69  **argv.){.  sqli
48ae0 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 20  te3_value *pVal 
48af0 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65  = sqlite3ValueNe
48b00 77 28 29 3b 0a 20 20 69 66 28 20 70 56 61 6c 20  w();.  if( pVal 
48b10 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61  ){.    sqlite3Va
48b20 6c 75 65 53 65 74 53 74 72 28 70 56 61 6c 2c 20  lueSetStr(pVal, 
48b30 2d 31 2c 20 22 6e 6f 77 22 2c 20 53 51 4c 49 54  -1, "now", SQLIT
48b40 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 53  E_UTF8, SQLITE_S
48b50 54 41 54 49 43 29 3b 0a 20 20 20 20 74 69 6d 65  TATIC);.    time
48b60 46 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20 31 2c  Func(context, 1,
48b70 20 26 70 56 61 6c 29 3b 0a 20 20 20 20 73 71 6c   &pVal);.    sql
48b80 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 70 56  ite3ValueFree(pV
48b90 61 6c 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  al);.  }.}../*.*
48ba0 2a 20 63 75 72 72 65 6e 74 5f 64 61 74 65 28 29  * current_date()
48bb0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
48bc0 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65  tion returns the
48bd0 20 73 61 6d 65 20 76 61 6c 75 65 20 61 73 20 64   same value as d
48be0 61 74 65 28 27 6e 6f 77 27 29 2e 0a 2a 2f 0a 73  ate('now')..*/.s
48bf0 74 61 74 69 63 20 76 6f 69 64 20 63 64 61 74 65  tatic void cdate
48c00 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
48c10 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
48c20 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
48c30 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
48c40 61 72 67 76 0a 29 7b 0a 20 20 73 71 6c 69 74 65  argv.){.  sqlite
48c50 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 20 3d 20  3_value *pVal = 
48c60 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28  sqlite3ValueNew(
48c70 29 3b 0a 20 20 69 66 28 20 70 56 61 6c 20 29 7b  );.  if( pVal ){
48c80 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75  .    sqlite3Valu
48c90 65 53 65 74 53 74 72 28 70 56 61 6c 2c 20 2d 31  eSetStr(pVal, -1
48ca0 2c 20 22 6e 6f 77 22 2c 20 53 51 4c 49 54 45 5f  , "now", SQLITE_
48cb0 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41  UTF8, SQLITE_STA
48cc0 54 49 43 29 3b 0a 20 20 20 20 64 61 74 65 46 75  TIC);.    dateFu
48cd0 6e 63 28 63 6f 6e 74 65 78 74 2c 20 31 2c 20 26  nc(context, 1, &
48ce0 70 56 61 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74  pVal);.    sqlit
48cf0 65 33 56 61 6c 75 65 46 72 65 65 28 70 56 61 6c  e3ValueFree(pVal
48d00 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
48d10 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74 61 6d  current_timestam
48d20 70 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  p().**.** This f
48d30 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
48d40 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 61  the same value a
48d50 73 20 64 61 74 65 74 69 6d 65 28 27 6e 6f 77 27  s datetime('now'
48d60 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  )..*/.static voi
48d70 64 20 63 74 69 6d 65 73 74 61 6d 70 46 75 6e 63  d ctimestampFunc
48d80 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
48d90 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
48da0 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69  int argc,.  sqli
48db0 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
48dc0 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  .){.  sqlite3_va
48dd0 6c 75 65 20 2a 70 56 61 6c 20 3d 20 73 71 6c 69  lue *pVal = sqli
48de0 74 65 33 56 61 6c 75 65 4e 65 77 28 29 3b 0a 20  te3ValueNew();. 
48df0 20 69 66 28 20 70 56 61 6c 20 29 7b 0a 20 20 20   if( pVal ){.   
48e00 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74   sqlite3ValueSet
48e10 53 74 72 28 70 56 61 6c 2c 20 2d 31 2c 20 22 6e  Str(pVal, -1, "n
48e20 6f 77 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  ow", SQLITE_UTF8
48e30 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
48e40 3b 0a 20 20 20 20 64 61 74 65 74 69 6d 65 46 75  ;.    datetimeFu
48e50 6e 63 28 63 6f 6e 74 65 78 74 2c 20 31 2c 20 26  nc(context, 1, &
48e60 70 56 61 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74  pVal);.    sqlit
48e70 65 33 56 61 6c 75 65 46 72 65 65 28 70 56 61 6c  e3ValueFree(pVal
48e80 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20  );.  }.}.#endif 
48e90 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  /* !defined(SQLI
48ea0 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45  TE_OMIT_DATETIME
48eb0 5f 46 55 4e 43 53 29 20 2a 2f 0a 0a 23 69 66 64  _FUNCS) */..#ifd
48ec0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
48ed0 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 0a 2f 2a  ATETIME_FUNCS./*
48ee0 0a 2a 2a 20 49 66 20 74 68 65 20 6c 69 62 72 61  .** If the libra
48ef0 72 79 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 74  ry is compiled t
48f00 6f 20 6f 6d 69 74 20 74 68 65 20 66 75 6c 6c 2d  o omit the full-
48f10 73 63 61 6c 65 20 64 61 74 65 20 61 6e 64 20 74  scale date and t
48f20 69 6d 65 0a 2a 2a 20 68 61 6e 64 6c 69 6e 67 20  ime.** handling 
48f30 28 74 6f 20 67 65 74 20 61 20 73 6d 61 6c 6c 65  (to get a smalle
48f40 72 20 62 69 6e 61 72 79 29 2c 20 74 68 65 20 66  r binary), the f
48f50 6f 6c 6c 6f 77 69 6e 67 20 6d 69 6e 69 6d 61 6c  ollowing minimal
48f60 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 6f 66 20 74   version.** of t
48f70 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 63 75 72  he functions cur
48f80 72 65 6e 74 5f 74 69 6d 65 28 29 2c 20 63 75 72  rent_time(), cur
48f90 72 65 6e 74 5f 64 61 74 65 28 29 20 61 6e 64 20  rent_date() and 
48fa0 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74 61 6d  current_timestam
48fb0 70 28 29 0a 2a 2a 20 61 72 65 20 69 6e 63 6c 75  p().** are inclu
48fc0 64 65 64 20 69 6e 73 74 65 61 64 2e 20 54 68 69  ded instead. Thi
48fd0 73 20 69 73 20 74 6f 20 73 75 70 70 6f 72 74 20  s is to support 
48fe0 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74 69  column declarati
48ff0 6f 6e 73 20 74 68 61 74 0a 2a 2a 20 69 6e 63 6c  ons that.** incl
49000 75 64 65 20 22 44 45 46 41 55 4c 54 20 43 55 52  ude "DEFAULT CUR
49010 52 45 4e 54 5f 54 49 4d 45 22 20 65 74 63 2e 0a  RENT_TIME" etc..
49020 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  **.** This funct
49030 69 6f 6e 20 75 73 65 73 20 74 68 65 20 43 2d 6c  ion uses the C-l
49040 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 73  ibrary functions
49050 20 74 69 6d 65 28 29 2c 20 67 6d 74 69 6d 65 28   time(), gmtime(
49060 29 0a 2a 2a 20 61 6e 64 20 73 74 72 66 74 69 6d  ).** and strftim
49070 65 28 29 2e 20 54 68 65 20 66 6f 72 6d 61 74 20  e(). The format 
49080 73 74 72 69 6e 67 20 74 6f 20 70 61 73 73 20 74  string to pass t
49090 6f 20 73 74 72 66 74 69 6d 65 28 29 20 69 73 20  o strftime() is 
490a0 73 75 70 70 6c 69 65 64 0a 2a 2a 20 61 73 20 74  supplied.** as t
490b0 68 65 20 75 73 65 72 2d 64 61 74 61 20 66 6f 72  he user-data for
490c0 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a   the function..*
490d0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 75  /.static void cu
490e0 72 72 65 6e 74 54 69 6d 65 46 75 6e 63 28 0a 20  rrentTimeFunc(. 
490f0 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
49100 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
49110 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33   argc,.  sqlite3
49120 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
49130 0a 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 63  .  time_t t;.  c
49140 68 61 72 20 2a 7a 46 6f 72 6d 61 74 20 3d 20 28  har *zFormat = (
49150 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 75  char *)sqlite3_u
49160 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74  ser_data(context
49170 29 3b 0a 20 20 63 68 61 72 20 7a 42 75 66 5b 32  );.  char zBuf[2
49180 30 5d 3b 0a 0a 20 20 74 69 6d 65 28 26 74 29 3b  0];..  time(&t);
49190 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
491a0 45 53 54 0a 20 20 7b 0a 20 20 20 20 65 78 74 65  EST.  {.    exte
491b0 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  rn int sqlite3_c
491c0 75 72 72 65 6e 74 5f 74 69 6d 65 3b 20 20 2f 2a  urrent_time;  /*
491d0 20 53 65 65 20 6f 73 5f 58 58 58 2e 63 20 2a 2f   See os_XXX.c */
491e0 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
491f0 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b  _current_time ){
49200 0a 20 20 20 20 20 20 74 20 3d 20 73 71 6c 69 74  .      t = sqlit
49210 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 3b  e3_current_time;
49220 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
49230 66 0a 0a 23 69 66 64 65 66 20 48 41 56 45 5f 47  f..#ifdef HAVE_G
49240 4d 54 49 4d 45 5f 52 0a 20 20 7b 0a 20 20 20 20  MTIME_R.  {.    
49250 73 74 72 75 63 74 20 74 6d 20 73 4e 6f 77 3b 0a  struct tm sNow;.
49260 20 20 20 20 67 6d 74 69 6d 65 5f 72 28 26 74 2c      gmtime_r(&t,
49270 20 26 73 4e 6f 77 29 3b 0a 20 20 20 20 73 74 72   &sNow);.    str
49280 66 74 69 6d 65 28 7a 42 75 66 2c 20 32 30 2c 20  ftime(zBuf, 20, 
49290 7a 46 6f 72 6d 61 74 2c 20 26 73 4e 6f 77 29 3b  zFormat, &sNow);
492a0 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 7b 0a 20  .  }.#else.  {. 
492b0 20 20 20 73 74 72 75 63 74 20 74 6d 20 2a 70 54     struct tm *pT
492c0 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73  m;.    sqlite3Os
492d0 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20  EnterMutex();.  
492e0 20 20 70 54 6d 20 3d 20 67 6d 74 69 6d 65 28 26    pTm = gmtime(&
492f0 74 29 3b 0a 20 20 20 20 73 74 72 66 74 69 6d 65  t);.    strftime
49300 28 7a 42 75 66 2c 20 32 30 2c 20 7a 46 6f 72 6d  (zBuf, 20, zForm
49310 61 74 2c 20 70 54 6d 29 3b 0a 20 20 20 20 73 71  at, pTm);.    sq
49320 6c 69 74 65 33 4f 73 4c 65 61 76 65 4d 75 74 65  lite3OsLeaveMute
49330 78 28 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  x();.  }.#endif.
49340 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
49350 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  t_text(context, 
49360 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54 45  zBuf, -1, SQLITE
49370 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23  _TRANSIENT);.}.#
49380 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  endif../*.** Thi
49390 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73  s function regis
493a0 74 65 72 65 64 20 61 6c 6c 20 6f 66 20 74 68 65  tered all of the
493b0 20 61 62 6f 76 65 20 43 20 66 75 6e 63 74 69 6f   above C functio
493c0 6e 73 20 61 73 20 53 51 4c 0a 2a 2a 20 66 75 6e  ns as SQL.** fun
493d0 63 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 73 68  ctions.  This sh
493e0 6f 75 6c 64 20 62 65 20 74 68 65 20 6f 6e 6c 79  ould be the only
493f0 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73   routine in this
49400 20 66 69 6c 65 20 77 69 74 68 0a 2a 2a 20 65 78   file with.** ex
49410 74 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a  ternal linkage..
49420 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
49430 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65  E void sqlite3Re
49440 67 69 73 74 65 72 44 61 74 65 54 69 6d 65 46 75  gisterDateTimeFu
49450 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 20  nctions(sqlite3 
49460 2a 64 62 29 7b 0a 23 69 66 6e 64 65 66 20 53 51  *db){.#ifndef SQ
49470 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49  LITE_OMIT_DATETI
49480 4d 45 5f 46 55 4e 43 53 0a 20 20 73 74 61 74 69  ME_FUNCS.  stati
49490 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 7b  c const struct {
494a0 0a 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d  .     char *zNam
494b0 65 3b 0a 20 20 20 20 20 69 6e 74 20 6e 41 72 67  e;.     int nArg
494c0 3b 0a 20 20 20 20 20 76 6f 69 64 20 28 2a 78 46  ;.     void (*xF
494d0 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  unc)(sqlite3_con
494e0 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  text*,int,sqlite
494f0 33 5f 76 61 6c 75 65 2a 2a 29 3b 0a 20 20 7d 20  3_value**);.  } 
49500 61 46 75 6e 63 73 5b 5d 20 3d 20 7b 0a 20 20 20  aFuncs[] = {.   
49510 20 7b 20 22 6a 75 6c 69 61 6e 64 61 79 22 2c 20   { "julianday", 
49520 2d 31 2c 20 6a 75 6c 69 61 6e 64 61 79 46 75 6e  -1, juliandayFun
49530 63 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 64 61  c   },.    { "da
49540 74 65 22 2c 20 20 20 20 20 20 2d 31 2c 20 64 61  te",      -1, da
49550 74 65 46 75 6e 63 20 20 20 20 20 20 20 20 7d 2c  teFunc        },
49560 0a 20 20 20 20 7b 20 22 74 69 6d 65 22 2c 20 20  .    { "time",  
49570 20 20 20 20 2d 31 2c 20 74 69 6d 65 46 75 6e 63      -1, timeFunc
49580 20 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 7b          },.    {
49590 20 22 64 61 74 65 74 69 6d 65 22 2c 20 20 2d 31   "datetime",  -1
495a0 2c 20 64 61 74 65 74 69 6d 65 46 75 6e 63 20 20  , datetimeFunc  
495b0 20 20 7d 2c 0a 20 20 20 20 7b 20 22 73 74 72 66    },.    { "strf
495c0 74 69 6d 65 22 2c 20 20 2d 31 2c 20 73 74 72 66  time",  -1, strf
495d0 74 69 6d 65 46 75 6e 63 20 20 20 20 7d 2c 0a 20  timeFunc    },. 
495e0 20 20 20 7b 20 22 63 75 72 72 65 6e 74 5f 74 69     { "current_ti
495f0 6d 65 22 2c 20 20 20 20 20 20 20 30 2c 20 63 74  me",       0, ct
49600 69 6d 65 46 75 6e 63 20 20 20 20 20 20 7d 2c 0a  imeFunc      },.
49610 20 20 20 20 7b 20 22 63 75 72 72 65 6e 74 5f 74      { "current_t
49620 69 6d 65 73 74 61 6d 70 22 2c 20 20 30 2c 20 63  imestamp",  0, c
49630 74 69 6d 65 73 74 61 6d 70 46 75 6e 63 20 7d 2c  timestampFunc },
49640 0a 20 20 20 20 7b 20 22 63 75 72 72 65 6e 74 5f  .    { "current_
49650 64 61 74 65 22 2c 20 20 20 20 20 20 20 30 2c 20  date",       0, 
49660 63 64 61 74 65 46 75 6e 63 20 20 20 20 20 20 7d  cdateFunc      }
49670 2c 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a  ,.  };.  int i;.
49680 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 69  .  for(i=0; i<si
49690 7a 65 6f 66 28 61 46 75 6e 63 73 29 2f 73 69 7a  zeof(aFuncs)/siz
496a0 65 6f 66 28 61 46 75 6e 63 73 5b 30 5d 29 3b 20  eof(aFuncs[0]); 
496b0 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  i++){.    sqlite
496c0 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20  3CreateFunc(db, 
496d0 61 46 75 6e 63 73 5b 69 5d 2e 7a 4e 61 6d 65 2c  aFuncs[i].zName,
496e0 20 61 46 75 6e 63 73 5b 69 5d 2e 6e 41 72 67 2c   aFuncs[i].nArg,
496f0 0a 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  .        SQLITE_
49700 55 54 46 38 2c 20 30 2c 20 61 46 75 6e 63 73 5b  UTF8, 0, aFuncs[
49710 69 5d 2e 78 46 75 6e 63 2c 20 30 2c 20 30 29 3b  i].xFunc, 0, 0);
49720 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 73 74 61  .  }.#else.  sta
49730 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74  tic const struct
49740 20 7b 0a 20 20 20 20 20 63 68 61 72 20 2a 7a 4e   {.     char *zN
49750 61 6d 65 3b 0a 20 20 20 20 20 63 68 61 72 20 2a  ame;.     char *
49760 7a 46 6f 72 6d 61 74 3b 0a 20 20 7d 20 61 46 75  zFormat;.  } aFu
49770 6e 63 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b 20  ncs[] = {.    { 
49780 22 63 75 72 72 65 6e 74 5f 74 69 6d 65 22 2c 20  "current_time", 
49790 22 25 48 3a 25 4d 3a 25 53 22 20 7d 2c 0a 20 20  "%H:%M:%S" },.  
497a0 20 20 7b 20 22 63 75 72 72 65 6e 74 5f 64 61 74    { "current_dat
497b0 65 22 2c 20 22 25 59 2d 25 6d 2d 25 64 22 20 7d  e", "%Y-%m-%d" }
497c0 2c 0a 20 20 20 20 7b 20 22 63 75 72 72 65 6e 74  ,.    { "current
497d0 5f 74 69 6d 65 73 74 61 6d 70 22 2c 20 22 25 59  _timestamp", "%Y
497e0 2d 25 6d 2d 25 64 20 25 48 3a 25 4d 3a 25 53 22  -%m-%d %H:%M:%S"
497f0 20 7d 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b   }.  };.  int i;
49800 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73  ..  for(i=0; i<s
49810 69 7a 65 6f 66 28 61 46 75 6e 63 73 29 2f 73 69  izeof(aFuncs)/si
49820 7a 65 6f 66 28 61 46 75 6e 63 73 5b 30 5d 29 3b  zeof(aFuncs[0]);
49830 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74   i++){.    sqlit
49840 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c  e3CreateFunc(db,
49850 20 61 46 75 6e 63 73 5b 69 5d 2e 7a 4e 61 6d 65   aFuncs[i].zName
49860 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  , 0, SQLITE_UTF8
49870 2c 20 0a 20 20 20 20 20 20 20 20 61 46 75 6e 63  , .        aFunc
49880 73 5b 69 5d 2e 7a 46 6f 72 6d 61 74 2c 20 63 75  s[i].zFormat, cu
49890 72 72 65 6e 74 54 69 6d 65 46 75 6e 63 2c 20 30  rrentTimeFunc, 0
498a0 2c 20 30 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  , 0);.  }.#endif
498b0 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
498c0 2a 2a 2a 20 45 6e 64 20 6f 66 20 64 61 74 65 2e  *** End of date.
498d0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
498e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
498f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49900 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
49910 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f  *** Begin file o
49920 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s.c ************
49930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49950 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 4e  **/./*.** 2005 N
49960 6f 76 65 6d 62 65 72 20 32 39 0a 2a 2a 0a 2a 2a  ovember 29.**.**
49970 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
49980 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
49990 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
499a0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
499b0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
499c0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
499d0 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
499e0 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
499f0 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
49a00 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
49a10 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
49a20 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
49a30 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
49a40 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
49a50 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
49a60 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
49a70 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
49a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
49ad0 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
49ae0 6f 6e 74 61 69 6e 73 20 4f 53 20 69 6e 74 65 72  ontains OS inter
49af0 66 61 63 65 20 63 6f 64 65 20 74 68 61 74 20 69  face code that i
49b00 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 0a  s common to all.
49b10 2a 2a 20 61 72 63 68 69 74 65 63 74 75 72 65 73  ** architectures
49b20 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 5f 53 51  ..*/.#define _SQ
49b30 4c 49 54 45 5f 4f 53 5f 43 5f 20 31 0a 23 75 6e  LITE_OS_C_ 1.#un
49b40 64 65 66 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 43  def _SQLITE_OS_C
49b50 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  _../*.** The fol
49b60 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 20  lowing routines 
49b70 61 72 65 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20  are convenience 
49b80 77 72 61 70 70 65 72 73 20 61 72 6f 75 6e 64 20  wrappers around 
49b90 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 20 74 68  methods.** of th
49ba0 65 20 4f 73 46 69 6c 65 20 6f 62 6a 65 63 74 2e  e OsFile object.
49bb0 20 20 54 68 69 73 20 69 73 20 6d 6f 73 74 6c 79    This is mostly
49bc0 20 6a 75 73 74 20 73 79 6e 74 61 63 74 69 63 20   just syntactic 
49bd0 73 75 67 61 72 2e 20 20 41 6c 6c 0a 2a 2a 20 6f  sugar.  All.** o
49be0 66 20 74 68 69 73 20 77 6f 75 6c 64 20 62 65 20  f this would be 
49bf0 63 6f 6d 70 6c 65 74 65 6c 79 20 61 75 74 6f 6d  completely autom
49c00 61 74 69 63 20 69 66 20 53 51 4c 69 74 65 20 77  atic if SQLite w
49c10 65 72 65 20 63 6f 64 65 64 20 75 73 69 6e 67 0a  ere coded using.
49c20 2a 2a 20 43 2b 2b 20 69 6e 73 74 65 61 64 20 6f  ** C++ instead o
49c30 66 20 70 6c 61 69 6e 20 6f 6c 64 20 43 2e 0a 2a  f plain old C..*
49c40 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
49c50 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 6c   int sqlite3OsCl
49c60 6f 73 65 28 4f 73 46 69 6c 65 20 2a 2a 70 49 64  ose(OsFile **pId
49c70 29 7b 0a 20 20 4f 73 46 69 6c 65 20 2a 69 64 3b  ){.  OsFile *id;
49c80 0a 20 20 69 66 28 20 70 49 64 21 3d 30 20 26 26  .  if( pId!=0 &&
49c90 20 28 69 64 20 3d 20 2a 70 49 64 29 21 3d 30 20   (id = *pId)!=0 
49ca0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 69 64  ){.    return id
49cb0 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73  ->pMethod->xClos
49cc0 65 28 70 49 64 29 3b 0a 20 20 7d 65 6c 73 65 7b  e(pId);.  }else{
49cd0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
49ce0 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 53 51 4c  TE_OK;.  }.}.SQL
49cf0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
49d00 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 44 69 72  sqlite3OsOpenDir
49d10 65 63 74 6f 72 79 28 4f 73 46 69 6c 65 20 2a 69  ectory(OsFile *i
49d20 64 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  d, const char *z
49d30 4e 61 6d 65 29 7b 0a 20 20 72 65 74 75 72 6e 20  Name){.  return 
49d40 69 64 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 4f 70  id->pMethod->xOp
49d50 65 6e 44 69 72 65 63 74 6f 72 79 28 69 64 2c 20  enDirectory(id, 
49d60 7a 4e 61 6d 65 29 3b 0a 7d 0a 53 51 4c 49 54 45  zName);.}.SQLITE
49d70 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
49d80 69 74 65 33 4f 73 52 65 61 64 28 4f 73 46 69 6c  ite3OsRead(OsFil
49d90 65 20 2a 69 64 2c 20 76 6f 69 64 20 2a 70 42 75  e *id, void *pBu
49da0 66 2c 20 69 6e 74 20 61 6d 74 29 7b 0a 20 20 72  f, int amt){.  r
49db0 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f  eturn id->pMetho
49dc0 64 2d 3e 78 52 65 61 64 28 69 64 2c 20 70 42 75  d->xRead(id, pBu
49dd0 66 2c 20 61 6d 74 29 3b 0a 7d 0a 53 51 4c 49 54  f, amt);.}.SQLIT
49de0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
49df0 6c 69 74 65 33 4f 73 57 72 69 74 65 28 4f 73 46  lite3OsWrite(OsF
49e00 69 6c 65 20 2a 69 64 2c 20 63 6f 6e 73 74 20 76  ile *id, const v
49e10 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 61  oid *pBuf, int a
49e20 6d 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 64  mt){.  return id
49e30 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 57 72 69 74  ->pMethod->xWrit
49e40 65 28 69 64 2c 20 70 42 75 66 2c 20 61 6d 74 29  e(id, pBuf, amt)
49e50 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
49e60 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
49e70 53 65 65 6b 28 4f 73 46 69 6c 65 20 2a 69 64 2c  Seek(OsFile *id,
49e80 20 69 36 34 20 6f 66 66 73 65 74 29 7b 0a 20 20   i64 offset){.  
49e90 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68  return id->pMeth
49ea0 6f 64 2d 3e 78 53 65 65 6b 28 69 64 2c 20 6f 66  od->xSeek(id, of
49eb0 66 73 65 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  fset);.}.SQLITE_
49ec0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
49ed0 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 4f 73  te3OsTruncate(Os
49ee0 46 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 73 69  File *id, i64 si
49ef0 7a 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 64  ze){.  return id
49f00 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 54 72 75 6e  ->pMethod->xTrun
49f10 63 61 74 65 28 69 64 2c 20 73 69 7a 65 29 3b 0a  cate(id, size);.
49f20 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
49f30 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 79   int sqlite3OsSy
49f40 6e 63 28 4f 73 46 69 6c 65 20 2a 69 64 2c 20 69  nc(OsFile *id, i
49f50 6e 74 20 66 75 6c 6c 73 79 6e 63 29 7b 0a 20 20  nt fullsync){.  
49f60 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68  return id->pMeth
49f70 6f 64 2d 3e 78 53 79 6e 63 28 69 64 2c 20 66 75  od->xSync(id, fu
49f80 6c 6c 73 79 6e 63 29 3b 0a 7d 0a 53 51 4c 49 54  llsync);.}.SQLIT
49f90 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
49fa0 71 6c 69 74 65 33 4f 73 53 65 74 46 75 6c 6c 53  qlite3OsSetFullS
49fb0 79 6e 63 28 4f 73 46 69 6c 65 20 2a 69 64 2c 20  ync(OsFile *id, 
49fc0 69 6e 74 20 76 61 6c 75 65 29 7b 0a 20 20 69 64  int value){.  id
49fd0 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 53 65 74 46  ->pMethod->xSetF
49fe0 75 6c 6c 53 79 6e 63 28 69 64 2c 20 76 61 6c 75  ullSync(id, valu
49ff0 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  e);.}.SQLITE_PRI
4a000 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4a010 4f 73 46 69 6c 65 53 69 7a 65 28 4f 73 46 69 6c  OsFileSize(OsFil
4a020 65 20 2a 69 64 2c 20 69 36 34 20 2a 70 53 69 7a  e *id, i64 *pSiz
4a030 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d  e){.  return id-
4a040 3e 70 4d 65 74 68 6f 64 2d 3e 78 46 69 6c 65 53  >pMethod->xFileS
4a050 69 7a 65 28 69 64 2c 20 70 53 69 7a 65 29 3b 0a  ize(id, pSize);.
4a060 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
4a070 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4c 6f   int sqlite3OsLo
4a080 63 6b 28 4f 73 46 69 6c 65 20 2a 69 64 2c 20 69  ck(OsFile *id, i
4a090 6e 74 20 6c 6f 63 6b 54 79 70 65 29 7b 0a 20 20  nt lockType){.  
4a0a0 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68  return id->pMeth
4a0b0 6f 64 2d 3e 78 4c 6f 63 6b 28 69 64 2c 20 6c 6f  od->xLock(id, lo
4a0c0 63 6b 54 79 70 65 29 3b 0a 7d 0a 53 51 4c 49 54  ckType);.}.SQLIT
4a0d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4a0e0 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 4f 73  lite3OsUnlock(Os
4a0f0 46 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f  File *id, int lo
4a100 63 6b 54 79 70 65 29 7b 0a 20 20 72 65 74 75 72  ckType){.  retur
4a110 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78  n id->pMethod->x
4a120 55 6e 6c 6f 63 6b 28 69 64 2c 20 6c 6f 63 6b 54  Unlock(id, lockT
4a130 79 70 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  ype);.}.SQLITE_P
4a140 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
4a150 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76 65  e3OsCheckReserve
4a160 64 4c 6f 63 6b 28 4f 73 46 69 6c 65 20 2a 69 64  dLock(OsFile *id
4a170 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e  ){.  return id->
4a180 70 4d 65 74 68 6f 64 2d 3e 78 43 68 65 63 6b 52  pMethod->xCheckR
4a190 65 73 65 72 76 65 64 4c 6f 63 6b 28 69 64 29 3b  eservedLock(id);
4a1a0 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
4a1b0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 53  E int sqlite3OsS
4a1c0 65 63 74 6f 72 53 69 7a 65 28 4f 73 46 69 6c 65  ectorSize(OsFile
4a1d0 20 2a 69 64 29 7b 0a 20 20 69 6e 74 20 28 2a 78   *id){.  int (*x
4a1e0 53 65 63 74 6f 72 53 69 7a 65 29 28 4f 73 46 69  SectorSize)(OsFi
4a1f0 6c 65 2a 29 20 3d 20 69 64 2d 3e 70 4d 65 74 68  le*) = id->pMeth
4a200 6f 64 2d 3e 78 53 65 63 74 6f 72 53 69 7a 65 3b  od->xSectorSize;
4a210 0a 20 20 72 65 74 75 72 6e 20 78 53 65 63 74 6f  .  return xSecto
4a220 72 53 69 7a 65 20 3f 20 78 53 65 63 74 6f 72 53  rSize ? xSectorS
4a230 69 7a 65 28 69 64 29 20 3a 20 53 51 4c 49 54 45  ize(id) : SQLITE
4a240 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f  _DEFAULT_SECTOR_
4a250 53 49 5a 45 3b 0a 7d 0a 0a 23 69 66 20 64 65 66  SIZE;.}..#if def
4a260 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54  ined(SQLITE_TEST
4a270 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
4a280 49 54 45 5f 44 45 42 55 47 29 0a 20 20 2f 2a 20  ITE_DEBUG).  /* 
4a290 54 68 65 73 65 20 6d 65 74 68 6f 64 73 20 61 72  These methods ar
4a2a0 65 20 63 75 72 72 65 6e 74 6c 79 20 6f 6e 6c 79  e currently only
4a2b0 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e   used for testin
4a2c0 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 2e  g and debugging.
4a2d0 20 2a 2f 0a 20 20 69 6e 74 20 73 71 6c 69 74 65   */.  int sqlite
4a2e0 33 4f 73 46 69 6c 65 48 61 6e 64 6c 65 28 4f 73  3OsFileHandle(Os
4a2f0 46 69 6c 65 20 2a 69 64 29 7b 0a 20 20 20 20 72  File *id){.    r
4a300 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f  eturn id->pMetho
4a310 64 2d 3e 78 46 69 6c 65 48 61 6e 64 6c 65 28 69  d->xFileHandle(i
4a320 64 29 3b 0a 20 20 7d 0a 20 20 69 6e 74 20 73 71  d);.  }.  int sq
4a330 6c 69 74 65 33 4f 73 4c 6f 63 6b 53 74 61 74 65  lite3OsLockState
4a340 28 4f 73 46 69 6c 65 20 2a 69 64 29 7b 0a 20 20  (OsFile *id){.  
4a350 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65    return id->pMe
4a360 74 68 6f 64 2d 3e 78 4c 6f 63 6b 53 74 61 74 65  thod->xLockState
4a370 28 69 64 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  (id);.  }.#endif
4a380 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
4a390 45 4e 41 42 4c 45 5f 52 45 44 45 46 5f 49 4f 0a  ENABLE_REDEF_IO.
4a3a0 2f 2a 0a 2a 2a 20 41 20 66 75 6e 63 74 69 6f 6e  /*.** A function
4a3b0 20 74 6f 20 72 65 74 75 72 6e 20 61 20 70 6f 69   to return a poi
4a3c0 6e 74 65 72 20 74 6f 20 74 68 65 20 76 69 72 74  nter to the virt
4a3d0 75 61 6c 20 66 75 6e 63 74 69 6f 6e 20 74 61 62  ual function tab
4a3e0 6c 65 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  le..** This rout
4a3f0 69 6e 65 20 72 65 61 6c 6c 79 20 64 6f 65 73 20  ine really does 
4a400 6e 6f 74 20 61 63 63 6f 6d 70 6c 69 73 68 20 76  not accomplish v
4a410 65 72 79 20 6d 75 63 68 20 73 69 6e 63 65 20 74  ery much since t
4a420 68 65 0a 2a 2a 20 76 69 72 74 75 61 6c 20 66 75  he.** virtual fu
4a430 6e 63 74 69 6f 6e 20 74 61 62 6c 65 20 69 73 20  nction table is 
4a440 61 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c  a global variabl
4a450 65 20 61 6e 64 20 61 6e 79 62 6f 64 79 20 77 68  e and anybody wh
4a460 6f 0a 2a 2a 20 63 61 6e 20 63 61 6c 6c 20 74 68  o.** can call th
4a470 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 20  is function can 
4a480 6a 75 73 74 20 61 73 20 65 61 73 69 6c 79 20 61  just as easily a
4a490 63 63 65 73 73 20 74 68 65 20 76 61 72 69 61 62  ccess the variab
4a4a0 6c 65 0a 2a 2a 20 66 6f 72 20 74 68 65 6d 73 65  le.** for themse
4a4b0 6c 76 65 73 2e 20 20 4e 65 76 65 72 74 68 65 6c  lves.  Neverthel
4a4c0 65 73 73 2c 20 77 65 20 69 6e 63 6c 75 64 65 20  ess, we include 
4a4d0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66 6f 72  this routine for
4a4e0 0a 2a 2a 20 62 61 63 6b 77 61 72 64 73 20 63 6f  .** backwards co
4a4f0 6d 70 61 74 69 62 69 6c 69 74 79 20 77 69 74 68  mpatibility with
4a500 20 61 6e 20 65 61 72 6c 69 65 72 20 72 65 64 65   an earlier rede
4a510 66 69 6e 61 62 6c 65 20 49 2f 4f 0a 2a 2a 20 69  finable I/O.** i
4a520 6e 74 65 72 66 61 63 65 20 64 65 73 69 67 6e 2e  nterface design.
4a530 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74  .*/.struct sqlit
4a540 65 33 4f 73 56 74 62 6c 20 2a 73 71 6c 69 74 65  e3OsVtbl *sqlite
4a550 33 5f 6f 73 5f 73 77 69 74 63 68 28 76 6f 69 64  3_os_switch(void
4a560 29 7b 0a 20 20 72 65 74 75 72 6e 20 26 73 71 6c  ){.  return &sql
4a570 69 74 65 33 4f 73 3b 0a 7d 0a 23 65 6e 64 69 66  ite3Os;.}.#endif
4a580 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
4a590 2a 20 45 6e 64 20 6f 66 20 6f 73 2e 63 20 2a 2a  * End of os.c **
4a5a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4a5b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4a5c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4a5d0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
4a5e0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 61 6c  * Begin file mal
4a5f0 6c 6f 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  loc.c **********
4a600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4a610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4a620 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
4a630 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20  tember 15.**.** 
4a640 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
4a650 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
4a660 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
4a670 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
4a680 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
4a690 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
4a6a0 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
4a6b0 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
4a6c0 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
4a6d0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
4a6e0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
4a6f0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
4a700 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
4a710 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
4a720 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
4a730 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
4a740 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
4a750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4a760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4a770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4a780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4a790 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 4d 65 6d 6f  ********.** Memo
4a7a0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75  ry allocation fu
4a7b0 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 68 72  nctions used thr
4a7c0 6f 75 67 68 6f 75 74 20 73 71 6c 69 74 65 2e 0a  oughout sqlite..
4a7d0 2a 2a 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 61  **.**.** $Id: ma
4a7e0 6c 6c 6f 63 2e 63 2c 76 20 31 2e 33 20 32 30 30  lloc.c,v 1.3 200
4a7f0 37 2f 30 36 2f 31 35 20 32 30 3a 32 39 3a 32 30  7/06/15 20:29:20
4a800 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f   drh Exp $.*/../
4a810 2a 0a 2a 2a 20 4d 41 4c 4c 4f 43 20 57 52 41 50  *.** MALLOC WRAP
4a820 50 45 52 20 41 52 43 48 49 54 45 43 54 55 52 45  PER ARCHITECTURE
4a830 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
4a840 65 20 63 6f 64 65 20 61 63 63 65 73 73 65 73 20  e code accesses 
4a850 64 79 6e 61 6d 69 63 20 6d 65 6d 6f 72 79 20 61  dynamic memory a
4a860 6c 6c 6f 63 61 74 69 6f 6e 2f 64 65 61 6c 6c 6f  llocation/deallo
4a870 63 61 74 69 6f 6e 20 62 79 20 69 6e 76 6f 6b 69  cation by invoki
4a880 6e 67 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77  ng.** the follow
4a890 69 6e 67 20 73 69 78 20 41 50 49 73 20 28 77 68  ing six APIs (wh
4a8a0 69 63 68 20 6d 61 79 20 62 65 20 69 6d 70 6c 65  ich may be imple
4a8b0 6d 65 6e 74 65 64 20 61 73 20 6d 61 63 72 6f 73  mented as macros
4a8c0 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c  )..**.**     sql
4a8d0 69 74 65 33 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20  ite3Malloc().** 
4a8e0 20 20 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f      sqlite3Mallo
4a8f0 63 52 61 77 28 29 0a 2a 2a 20 20 20 20 20 73 71  cRaw().**     sq
4a900 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 29 0a 2a  lite3Realloc().*
4a910 2a 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 61  *     sqlite3Rea
4a920 6c 6c 6f 63 4f 72 46 72 65 65 28 29 0a 2a 2a 20  llocOrFree().** 
4a930 20 20 20 20 73 71 6c 69 74 65 33 46 72 65 65 28      sqlite3Free(
4a940 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33  ).**     sqlite3
4a950 41 6c 6c 6f 63 53 69 7a 65 28 29 0a 2a 2a 0a 2a  AllocSize().**.*
4a960 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 73  * The function s
4a970 71 6c 69 74 65 33 46 72 65 65 58 20 70 65 72 66  qlite3FreeX perf
4a980 6f 72 6d 73 20 74 68 65 20 73 61 6d 65 20 74 61  orms the same ta
4a990 73 6b 20 61 73 20 73 71 6c 69 74 65 33 46 72 65  sk as sqlite3Fre
4a9a0 65 20 61 6e 64 20 69 73 0a 2a 2a 20 67 75 61 72  e and is.** guar
4a9b0 61 6e 74 65 65 64 20 74 6f 20 62 65 20 61 20 72  anteed to be a r
4a9c0 65 61 6c 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68  eal function. Th
4a9d0 65 20 73 61 6d 65 20 68 6f 6c 64 73 20 66 6f 72  e same holds for
4a9e0 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 58 0a   sqlite3MallocX.
4a9f0 2a 2a 0a 2a 2a 20 54 68 65 20 61 62 6f 76 65 20  **.** The above 
4aa00 41 50 49 73 20 61 72 65 20 69 6d 70 6c 65 6d 65  APIs are impleme
4aa10 6e 74 65 64 20 69 6e 20 74 65 72 6d 73 20 6f 66  nted in terms of
4aa20 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 70   the functions p
4aa30 72 6f 76 69 64 65 64 20 69 6e 20 74 68 65 0a 2a  rovided in the.*
4aa40 2a 20 6f 70 65 72 61 74 69 6e 67 2d 73 79 73 74  * operating-syst
4aa50 65 6d 20 69 6e 74 65 72 66 61 63 65 2e 20 54 68  em interface. Th
4aa60 65 20 4f 53 20 69 6e 74 65 72 66 61 63 65 20 69  e OS interface i
4aa70 73 20 6e 65 76 65 72 20 61 63 63 65 73 73 65 64  s never accessed
4aa80 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20   directly.** by 
4aa90 63 6f 64 65 20 6f 75 74 73 69 64 65 20 6f 66 20  code outside of 
4aaa0 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a  this file..**.**
4aab0 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4d 61       sqlite3OsMa
4aac0 6c 6c 6f 63 28 29 0a 2a 2a 20 20 20 20 20 73 71  lloc().**     sq
4aad0 6c 69 74 65 33 4f 73 52 65 61 6c 6c 6f 63 28 29  lite3OsRealloc()
4aae0 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f  .**     sqlite3O
4aaf0 73 46 72 65 65 28 29 0a 2a 2a 20 20 20 20 20 73  sFree().**     s
4ab00 71 6c 69 74 65 33 4f 73 41 6c 6c 6f 63 61 74 69  qlite3OsAllocati
4ab10 6f 6e 53 69 7a 65 28 29 0a 2a 2a 0a 2a 2a 20 46  onSize().**.** F
4ab20 75 6e 63 74 69 6f 6e 73 20 73 71 6c 69 74 65 33  unctions sqlite3
4ab30 4d 61 6c 6c 6f 63 52 61 77 28 29 20 61 6e 64 20  MallocRaw() and 
4ab40 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 29  sqlite3Realloc()
4ab50 20 6d 61 79 20 69 6e 76 6f 6b 65 20 0a 2a 2a 20   may invoke .** 
4ab60 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f  sqlite3_release_
4ab70 6d 65 6d 6f 72 79 28 29 20 69 66 20 61 20 63 61  memory() if a ca
4ab80 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 4d  ll to sqlite3OsM
4ab90 61 6c 6c 6f 63 28 29 20 6f 72 0a 2a 2a 20 73 71  alloc() or.** sq
4aba0 6c 69 74 65 33 4f 73 52 65 61 6c 6c 6f 63 28 29  lite3OsRealloc()
4abb0 20 66 61 69 6c 73 20 28 6f 72 20 69 66 20 74 68   fails (or if th
4abc0 65 20 73 6f 66 74 2d 68 65 61 70 2d 6c 69 6d 69  e soft-heap-limi
4abd0 74 20 66 6f 72 20 74 68 65 20 74 68 72 65 61 64  t for the thread
4abe0 20 69 73 0a 2a 2a 20 65 78 63 65 65 64 65 64 29   is.** exceeded)
4abf0 2e 20 46 75 6e 63 74 69 6f 6e 20 73 71 6c 69 74  . Function sqlit
4ac00 65 33 4d 61 6c 6c 6f 63 28 29 20 75 73 75 61 6c  e3Malloc() usual
4ac10 6c 79 20 69 6e 76 6f 6b 65 73 0a 2a 2a 20 73 71  ly invokes.** sq
4ac20 6c 69 74 65 33 4d 61 6c 6c 6f 63 52 61 77 28 29  lite3MallocRaw()
4ac30 2e 0a 2a 2a 0a 2a 2a 20 4d 41 4c 4c 4f 43 20 54  ..**.** MALLOC T
4ac40 45 53 54 20 57 52 41 50 50 45 52 20 41 52 43 48  EST WRAPPER ARCH
4ac50 49 54 45 43 54 55 52 45 0a 2a 2a 0a 2a 2a 20 54  ITECTURE.**.** T
4ac60 68 65 20 74 65 73 74 20 77 72 61 70 70 65 72 20  he test wrapper 
4ac70 70 72 6f 76 69 64 65 73 20 65 78 74 72 61 20 74  provides extra t
4ac80 65 73 74 20 66 61 63 69 6c 69 74 69 65 73 20 74  est facilities t
4ac90 6f 20 65 6e 73 75 72 65 20 74 68 65 20 6c 69 62  o ensure the lib
4aca0 72 61 72 79 20 0a 2a 2a 20 64 6f 65 73 20 6e 6f  rary .** does no
4acb0 74 20 6c 65 61 6b 20 6d 65 6d 6f 72 79 20 61 6e  t leak memory an
4acc0 64 20 68 61 6e 64 6c 65 73 20 74 68 65 20 66 61  d handles the fa
4acd0 69 6c 75 72 65 20 6f 66 20 74 68 65 20 75 6e 64  ilure of the und
4ace0 65 72 6c 79 69 6e 67 20 4f 53 20 6c 65 76 65 6c  erlying OS level
4acf0 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73  .** allocation s
4ad00 79 73 74 65 6d 20 63 6f 72 72 65 63 74 6c 79 2e  ystem correctly.
4ad10 20 49 74 20 69 73 20 6f 6e 6c 79 20 70 72 65 73   It is only pres
4ad20 65 6e 74 20 69 66 20 74 68 65 20 6c 69 62 72 61  ent if the libra
4ad30 72 79 20 69 73 20 0a 2a 2a 20 63 6f 6d 70 69 6c  ry is .** compil
4ad40 65 64 20 77 69 74 68 20 74 68 65 20 53 51 4c 49  ed with the SQLI
4ad50 54 45 5f 4d 45 4d 44 45 42 55 47 20 6d 61 63 72  TE_MEMDEBUG macr
4ad60 6f 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  o set..**.**    
4ad70 20 2a 20 47 75 61 72 64 70 6f 73 74 73 20 74 6f   * Guardposts to
4ad80 20 64 65 74 65 63 74 20 6f 76 65 72 77 72 69 74   detect overwrit
4ad90 65 73 2e 0a 2a 2a 20 20 20 20 20 2a 20 41 62 69  es..**     * Abi
4ada0 6c 69 74 79 20 74 6f 20 63 61 75 73 65 20 61 20  lity to cause a 
4adb0 73 70 65 63 69 66 69 63 20 4d 61 6c 6c 6f 63 28  specific Malloc(
4adc0 29 20 6f 72 20 52 65 61 6c 6c 6f 63 28 29 20 74  ) or Realloc() t
4add0 6f 20 66 61 69 6c 2e 0a 2a 2a 20 20 20 20 20 2a  o fail..**     *
4ade0 20 41 75 64 69 74 20 6f 75 74 73 74 61 6e 64 69   Audit outstandi
4adf0 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ng memory alloca
4ae00 74 69 6f 6e 73 20 28 69 2e 65 20 63 68 65 63 6b  tions (i.e check
4ae10 20 66 6f 72 20 6c 65 61 6b 73 29 2e 0a 2a 2f 0a   for leaks)..*/.
4ae20 0a 23 64 65 66 69 6e 65 20 4d 41 58 28 78 2c 79  .#define MAX(x,y
4ae30 29 20 28 28 78 29 3e 28 79 29 3f 28 78 29 3a 28  ) ((x)>(y)?(x):(
4ae40 79 29 29 0a 0a 23 69 66 20 64 65 66 69 6e 65 64  y))..#if defined
4ae50 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d  (SQLITE_ENABLE_M
4ae60 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54  EMORY_MANAGEMENT
4ae70 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
4ae80 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f  LITE_OMIT_DISKIO
4ae90 29 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  )./*.** Set the 
4aea0 73 6f 66 74 20 68 65 61 70 2d 73 69 7a 65 20 6c  soft heap-size l
4aeb0 69 6d 69 74 20 66 6f 72 20 74 68 65 20 63 75 72  imit for the cur
4aec0 72 65 6e 74 20 74 68 72 65 61 64 2e 20 50 61 73  rent thread. Pas
4aed0 73 69 6e 67 20 61 20 6e 65 67 61 74 69 76 65 0a  sing a negative.
4aee0 2a 2a 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74  ** value indicat
4aef0 65 73 20 6e 6f 20 6c 69 6d 69 74 2e 0a 2a 2f 0a  es no limit..*/.
4af00 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 73 6f 66  void sqlite3_sof
4af10 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 69 6e 74  t_heap_limit(int
4af20 20 6e 29 7b 0a 20 20 54 68 72 65 61 64 44 61 74   n){.  ThreadDat
4af30 61 20 2a 70 54 64 20 3d 20 73 71 6c 69 74 65 33  a *pTd = sqlite3
4af40 54 68 72 65 61 64 44 61 74 61 28 29 3b 0a 20 20  ThreadData();.  
4af50 69 66 28 20 70 54 64 20 29 7b 0a 20 20 20 20 70  if( pTd ){.    p
4af60 54 64 2d 3e 6e 53 6f 66 74 48 65 61 70 4c 69 6d  Td->nSoftHeapLim
4af70 69 74 20 3d 20 6e 3b 0a 20 20 7d 0a 20 20 73 71  it = n;.  }.  sq
4af80 6c 69 74 65 33 52 65 6c 65 61 73 65 54 68 72 65  lite3ReleaseThre
4af90 61 64 44 61 74 61 28 29 3b 0a 7d 0a 0a 2f 2a 0a  adData();.}../*.
4afa0 2a 2a 20 52 65 6c 65 61 73 65 20 6d 65 6d 6f 72  ** Release memor
4afb0 79 20 68 65 6c 64 20 62 79 20 53 51 4c 69 74 65  y held by SQLite
4afc0 20 69 6e 73 74 61 6e 63 65 73 20 63 72 65 61 74   instances creat
4afd0 65 64 20 62 79 20 74 68 65 20 63 75 72 72 65 6e  ed by the curren
4afe0 74 20 74 68 72 65 61 64 2e 0a 2a 2f 0a 69 6e 74  t thread..*/.int
4aff0 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65   sqlite3_release
4b000 5f 6d 65 6d 6f 72 79 28 69 6e 74 20 6e 29 7b 0a  _memory(int n){.
4b010 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
4b020 50 61 67 65 72 52 65 6c 65 61 73 65 4d 65 6d 6f  PagerReleaseMemo
4b030 72 79 28 6e 29 3b 0a 7d 0a 23 65 6c 73 65 0a 2f  ry(n);.}.#else./
4b040 2a 20 49 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  * If SQLITE_ENAB
4b050 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45  LE_MEMORY_MANAGE
4b060 4d 45 4e 54 20 69 73 20 6e 6f 74 20 64 65 66 69  MENT is not defi
4b070 6e 65 64 2c 20 74 68 65 6e 20 64 65 66 69 6e 65  ned, then define
4b080 20 61 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 6f 66   a version.** of
4b090 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65   sqlite3_release
4b0a0 5f 6d 65 6d 6f 72 79 28 29 20 74 6f 20 62 65 20  _memory() to be 
4b0b0 75 73 65 64 20 62 79 20 6f 74 68 65 72 20 63 6f  used by other co
4b0c0 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 2e  de in this file.
4b0d0 0a 2a 2a 20 54 68 69 73 20 69 73 20 64 6f 6e 65  .** This is done
4b0e0 20 66 6f 72 20 6e 6f 20 62 65 74 74 65 72 20 72   for no better r
4b0f0 65 61 73 6f 6e 20 74 68 61 6e 20 74 6f 20 72 65  eason than to re
4b100 64 75 63 65 20 74 68 65 20 6e 75 6d 62 65 72 20  duce the number 
4b110 6f 66 20 0a 2a 2a 20 70 72 65 2d 70 72 6f 63 65  of .** pre-proce
4b120 73 73 6f 72 20 23 69 66 6e 64 65 66 20 73 74 61  ssor #ifndef sta
4b130 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 23 64 65 66  tements..*/.#def
4b140 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 6c 65  ine sqlite3_rele
4b150 61 73 65 5f 6d 65 6d 6f 72 79 28 78 29 20 30 20  ase_memory(x) 0 
4b160 20 20 20 2f 2a 20 30 20 3d 3d 20 6e 6f 20 6d 65     /* 0 == no me
4b170 6d 6f 72 79 20 66 72 65 65 64 20 2a 2f 0a 23 65  mory freed */.#e
4b180 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
4b190 49 54 45 5f 4d 45 4d 44 45 42 55 47 0a 2f 2a 2d  ITE_MEMDEBUG./*-
4b1a0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4b1b0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4b1c0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4b1d0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4b1e0 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 42 65 67  ---------.** Beg
4b1f0 69 6e 20 63 6f 64 65 20 66 6f 72 20 6d 65 6d 6f  in code for memo
4b200 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 79  ry allocation sy
4b210 73 74 65 6d 20 74 65 73 74 20 6c 61 79 65 72 2e  stem test layer.
4b220 0a 2a 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 20 64 65  .**.** Memory de
4b230 62 75 67 67 69 6e 67 20 69 73 20 74 75 72 6e 65  bugging is turne
4b240 64 20 6f 6e 20 62 79 20 64 65 66 69 6e 69 6e 67  d on by defining
4b250 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 45 4d 44   the SQLITE_MEMD
4b260 45 42 55 47 20 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a  EBUG macro..**.*
4b270 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55  * SQLITE_MEMDEBU
4b280 47 3d 3d 31 20 20 20 20 2d 3e 20 46 65 6e 63 65  G==1    -> Fence
4b290 2d 70 6f 73 74 69 6e 67 20 6f 6e 6c 79 20 28 74  -posting only (t
4b2a0 68 72 65 61 64 20 73 61 66 65 29 20 0a 2a 2a 20  hread safe) .** 
4b2b0 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 3d  SQLITE_MEMDEBUG=
4b2c0 3d 32 20 20 20 20 2d 3e 20 46 65 6e 63 65 2d 70  =2    -> Fence-p
4b2d0 6f 73 74 69 6e 67 20 2b 20 6c 69 6e 6b 65 64 20  osting + linked 
4b2e0 6c 69 73 74 20 6f 66 20 61 6c 6c 6f 63 61 74 69  list of allocati
4b2f0 6f 6e 73 20 28 6e 6f 74 20 74 73 29 0a 2a 2a 20  ons (not ts).** 
4b300 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 3d  SQLITE_MEMDEBUG=
4b310 3d 33 20 20 20 20 2d 3e 20 41 62 6f 76 65 20 2b  =3    -> Above +
4b320 20 62 61 63 6b 74 72 61 63 65 73 20 28 6e 6f 74   backtraces (not
4b330 20 74 68 72 65 61 64 20 73 61 66 65 2c 20 72 65   thread safe, re
4b340 71 2e 20 67 6c 69 62 63 29 0a 2a 2f 0a 0a 2f 2a  q. glibc).*/../*
4b350 20 46 69 67 75 72 65 20 6f 75 74 20 77 68 65 74   Figure out whet
4b360 68 65 72 20 6f 72 20 6e 6f 74 20 74 6f 20 73 74  her or not to st
4b370 6f 72 65 20 62 61 63 6b 74 72 61 63 65 28 29 20  ore backtrace() 
4b380 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20  information for 
4b390 65 61 63 68 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a 20  each malloc..** 
4b3a0 54 68 65 20 62 61 63 6b 74 72 61 63 65 28 29 20  The backtrace() 
4b3b0 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79  function is only
4b3c0 20 75 73 65 64 20 69 66 20 53 51 4c 49 54 45 5f   used if SQLITE_
4b3d0 4d 45 4d 44 45 42 55 47 20 69 73 20 73 65 74 20  MEMDEBUG is set 
4b3e0 74 6f 20 32 20 6f 72 20 0a 2a 2a 20 67 72 65 61  to 2 or .** grea
4b3f0 74 65 72 20 61 6e 64 20 67 6c 69 62 63 20 69 73  ter and glibc is
4b400 20 69 6e 20 75 73 65 2e 20 49 66 20 77 65 20 64   in use. If we d
4b410 6f 6e 27 74 20 77 61 6e 74 20 74 6f 20 75 73 65  on't want to use
4b420 20 62 61 63 6b 74 72 61 63 65 28 29 2c 20 74 68   backtrace(), th
4b430 65 6e 20 6a 75 73 74 0a 2a 2a 20 64 65 66 69 6e  en just.** defin
4b440 65 20 69 74 20 61 73 20 61 6e 20 65 6d 70 74 79  e it as an empty
4b450 20 6d 61 63 72 6f 20 61 6e 64 20 73 65 74 20 74   macro and set t
4b460 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 73 70 61  he amount of spa
4b470 63 65 20 72 65 73 65 72 76 65 64 20 74 6f 20 30  ce reserved to 0
4b480 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64  ..*/.#if defined
4b490 28 5f 5f 47 4c 49 42 43 5f 5f 29 20 26 26 20 53  (__GLIBC__) && S
4b4a0 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 3e 32  QLITE_MEMDEBUG>2
4b4b0 0a 20 20 65 78 74 65 72 6e 20 69 6e 74 20 62 61  .  extern int ba
4b4c0 63 6b 74 72 61 63 65 28 76 6f 69 64 20 2a 2a 2c  cktrace(void **,
4b4d0 20 69 6e 74 29 3b 0a 20 20 23 64 65 66 69 6e 65   int);.  #define
4b4e0 20 54 45 53 54 41 4c 4c 4f 43 5f 53 54 41 43 4b   TESTALLOC_STACK
4b4f0 53 49 5a 45 20 31 32 38 0a 20 20 23 64 65 66 69  SIZE 128.  #defi
4b500 6e 65 20 54 45 53 54 41 4c 4c 4f 43 5f 53 54 41  ne TESTALLOC_STA
4b510 43 4b 46 52 41 4d 45 53 20 28 28 54 45 53 54 41  CKFRAMES ((TESTA
4b520 4c 4c 4f 43 5f 53 54 41 43 4b 53 49 5a 45 2d 38  LLOC_STACKSIZE-8
4b530 29 2f 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 29  )/sizeof(void*))
4b540 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65  .#else.  #define
4b550 20 62 61 63 6b 74 72 61 63 65 28 78 2c 20 79 29   backtrace(x, y)
4b560 0a 20 20 23 64 65 66 69 6e 65 20 54 45 53 54 41  .  #define TESTA
4b570 4c 4c 4f 43 5f 53 54 41 43 4b 53 49 5a 45 20 30  LLOC_STACKSIZE 0
4b580 0a 20 20 23 64 65 66 69 6e 65 20 54 45 53 54 41  .  #define TESTA
4b590 4c 4c 4f 43 5f 53 54 41 43 4b 46 52 41 4d 45 53  LLOC_STACKFRAMES
4b5a0 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a   0.#endif../*.**
4b5b0 20 4e 75 6d 62 65 72 20 6f 66 20 33 32 2d 62 69   Number of 32-bi
4b5c0 74 20 67 75 61 72 64 20 77 6f 72 64 73 2e 20 20  t guard words.  
4b5d0 54 68 69 73 20 73 68 6f 75 6c 64 20 70 72 6f 62  This should prob
4b5e0 61 62 6c 79 20 62 65 20 61 20 6d 75 6c 74 69 70  ably be a multip
4b5f0 6c 65 20 6f 66 0a 2a 2a 20 32 20 73 69 6e 63 65  le of.** 2 since
4b600 20 6f 6e 20 36 34 2d 62 69 74 20 6d 61 63 68 69   on 64-bit machi
4b610 6e 65 73 20 77 65 20 77 61 6e 74 20 74 68 65 20  nes we want the 
4b620 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62  value returned b
4b630 79 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29  y sqliteMalloc()
4b640 0a 2a 2a 20 74 6f 20 62 65 20 38 2d 62 79 74 65  .** to be 8-byte
4b650 20 61 6c 69 67 6e 65 64 2e 0a 2a 2f 0a 23 69 66   aligned..*/.#if
4b660 6e 64 65 66 20 54 45 53 54 41 4c 4c 4f 43 5f 4e  ndef TESTALLOC_N
4b670 47 55 41 52 44 0a 23 20 64 65 66 69 6e 65 20 54  GUARD.# define T
4b680 45 53 54 41 4c 4c 4f 43 5f 4e 47 55 41 52 44 20  ESTALLOC_NGUARD 
4b690 32 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  2.#endif../*.** 
4b6a0 53 69 7a 65 20 72 65 73 65 72 76 65 64 20 66 6f  Size reserved fo
4b6b0 72 20 73 74 6f 72 69 6e 67 20 66 69 6c 65 2d 6e  r storing file-n
4b6c0 61 6d 65 20 61 6c 6f 6e 67 20 77 69 74 68 20 65  ame along with e
4b6d0 61 63 68 20 6d 61 6c 6c 6f 63 28 29 65 64 20 62  ach malloc()ed b
4b6e0 6c 6f 62 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  lob..*/.#define 
4b6f0 54 45 53 54 41 4c 4c 4f 43 5f 46 49 4c 45 53 49  TESTALLOC_FILESI
4b700 5a 45 20 36 34 0a 0a 2f 2a 0a 2a 2a 20 53 69 7a  ZE 64../*.** Siz
4b710 65 20 72 65 73 65 72 76 65 64 20 66 6f 72 20 73  e reserved for s
4b720 74 6f 72 69 6e 67 20 74 68 65 20 75 73 65 72 20  toring the user 
4b730 73 74 72 69 6e 67 2e 20 45 61 63 68 20 74 69 6d  string. Each tim
4b740 65 20 61 20 4d 61 6c 6c 6f 63 28 29 20 6f 72 20  e a Malloc() or 
4b750 52 65 61 6c 6c 6f 63 28 29 0a 2a 2a 20 63 61 6c  Realloc().** cal
4b760 6c 20 73 75 63 63 65 65 64 73 2c 20 75 70 20 74  l succeeds, up t
4b770 6f 20 54 45 53 54 41 4c 4c 4f 43 5f 55 53 45 52  o TESTALLOC_USER
4b780 53 49 5a 45 20 62 79 74 65 73 20 6f 66 20 74 68  SIZE bytes of th
4b790 65 20 73 74 72 69 6e 67 20 70 6f 69 6e 74 65 64  e string pointed
4b7a0 20 74 6f 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65   to by.** sqlite
4b7b0 33 5f 6d 61 6c 6c 6f 63 5f 69 64 20 61 72 65 20  3_malloc_id are 
4b7c0 73 74 6f 72 65 64 20 61 6c 6f 6e 67 20 77 69 74  stored along wit
4b7d0 68 20 74 68 65 20 6f 74 68 65 72 20 74 65 73 74  h the other test
4b7e0 20 73 79 73 74 65 6d 20 6d 65 74 61 64 61 74 61   system metadata
4b7f0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45 53  ..*/.#define TES
4b800 54 41 4c 4c 4f 43 5f 55 53 45 52 53 49 5a 45 20  TALLOC_USERSIZE 
4b810 36 34 0a 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  64.const char *s
4b820 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 5f 69 64  qlite3_malloc_id
4b830 20 3d 20 30 3b 0a 0a 2f 2a 0a 2a 2a 20 42 6c 6f   = 0;../*.** Blo
4b840 63 6b 73 20 75 73 65 64 20 62 79 20 74 68 65 20  cks used by the 
4b850 74 65 73 74 20 6c 61 79 65 72 20 68 61 76 65 20  test layer have 
4b860 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f  the following fo
4b870 72 6d 61 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  rmat:.**.**     
4b880 20 20 20 3c 73 69 7a 65 6f 66 28 76 6f 69 64 20     <sizeof(void 
4b890 2a 29 20 70 4e 65 78 74 20 70 6f 69 6e 74 65 72  *) pNext pointer
4b8a0 3e 0a 2a 2a 20 20 20 20 20 20 20 20 3c 73 69 7a  >.**        <siz
4b8b0 65 6f 66 28 76 6f 69 64 20 2a 29 20 70 50 72 65  eof(void *) pPre
4b8c0 76 20 70 6f 69 6e 74 65 72 3e 0a 2a 2a 20 20 20  v pointer>.**   
4b8d0 20 20 20 20 20 3c 54 45 53 54 41 4c 4c 4f 43 5f       <TESTALLOC_
4b8e0 4e 47 55 41 52 44 20 33 32 2d 62 69 74 20 67 75  NGUARD 32-bit gu
4b8f0 61 72 64 20 77 6f 72 64 73 3e 0a 2a 2a 20 20 20  ard words>.**   
4b900 20 20 20 20 20 20 20 20 20 3c 54 68 65 20 61 70           <The ap
4b910 70 6c 69 63 61 74 69 6f 6e 20 6c 65 76 65 6c 20  plication level 
4b920 61 6c 6c 6f 63 61 74 69 6f 6e 3e 0a 2a 2a 20 20  allocation>.**  
4b930 20 20 20 20 20 20 3c 54 45 53 54 41 4c 4c 4f 43        <TESTALLOC
4b940 5f 4e 47 55 41 52 44 20 33 32 2d 62 69 74 20 67  _NGUARD 32-bit g
4b950 75 61 72 64 20 77 6f 72 64 73 3e 0a 2a 2a 20 20  uard words>.**  
4b960 20 20 20 20 20 20 3c 33 32 2d 62 69 74 20 6c 69        <32-bit li
4b970 6e 65 20 6e 75 6d 62 65 72 3e 0a 2a 2a 20 20 20  ne number>.**   
4b980 20 20 20 20 20 3c 54 45 53 54 41 4c 4c 4f 43 5f       <TESTALLOC_
4b990 46 49 4c 45 53 49 5a 45 20 62 79 74 65 73 20 63  FILESIZE bytes c
4b9a0 6f 6e 74 61 69 6e 69 6e 67 20 6e 75 6c 6c 2d 74  ontaining null-t
4b9b0 65 72 6d 69 6e 61 74 65 64 20 66 69 6c 65 20 6e  erminated file n
4b9c0 61 6d 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20 3c  ame>.**        <
4b9d0 54 45 53 54 41 4c 4c 4f 43 5f 53 54 41 43 4b 53  TESTALLOC_STACKS
4b9e0 49 5a 45 20 62 79 74 65 73 20 6f 66 20 62 61 63  IZE bytes of bac
4b9f0 6b 74 72 61 63 65 28 29 20 6f 75 74 70 75 74 3e  ktrace() output>
4ba00 0a 2a 2f 20 0a 0a 23 64 65 66 69 6e 65 20 54 45  .*/ ..#define TE
4ba10 53 54 41 4c 4c 4f 43 5f 4f 46 46 53 45 54 5f 47  STALLOC_OFFSET_G
4ba20 55 41 52 44 31 28 70 29 20 20 20 20 28 73 69 7a  UARD1(p)    (siz
4ba30 65 6f 66 28 76 6f 69 64 20 2a 29 20 2a 20 32 29  eof(void *) * 2)
4ba40 0a 23 64 65 66 69 6e 65 20 54 45 53 54 41 4c 4c  .#define TESTALL
4ba50 4f 43 5f 4f 46 46 53 45 54 5f 44 41 54 41 28 70  OC_OFFSET_DATA(p
4ba60 29 20 28 20 5c 0a 20 20 54 45 53 54 41 4c 4c 4f  ) ( \.  TESTALLO
4ba70 43 5f 4f 46 46 53 45 54 5f 47 55 41 52 44 31 28  C_OFFSET_GUARD1(
4ba80 70 29 20 2b 20 73 69 7a 65 6f 66 28 75 33 32 29  p) + sizeof(u32)
4ba90 20 2a 20 54 45 53 54 41 4c 4c 4f 43 5f 4e 47 55   * TESTALLOC_NGU
4baa0 41 52 44 20 5c 0a 29 0a 23 64 65 66 69 6e 65 20  ARD \.).#define 
4bab0 54 45 53 54 41 4c 4c 4f 43 5f 4f 46 46 53 45 54  TESTALLOC_OFFSET
4bac0 5f 47 55 41 52 44 32 28 70 29 20 28 20 5c 0a 20  _GUARD2(p) ( \. 
4bad0 20 54 45 53 54 41 4c 4c 4f 43 5f 4f 46 46 53 45   TESTALLOC_OFFSE
4bae0 54 5f 44 41 54 41 28 70 29 20 2b 20 73 71 6c 69  T_DATA(p) + sqli
4baf0 74 65 33 4f 73 41 6c 6c 6f 63 61 74 69 6f 6e 53  te3OsAllocationS
4bb00 69 7a 65 28 70 29 20 2d 20 54 45 53 54 41 4c 4c  ize(p) - TESTALL
4bb10 4f 43 5f 4f 56 45 52 48 45 41 44 20 5c 0a 29 0a  OC_OVERHEAD \.).
4bb20 23 64 65 66 69 6e 65 20 54 45 53 54 41 4c 4c 4f  #define TESTALLO
4bb30 43 5f 4f 46 46 53 45 54 5f 4c 49 4e 45 4e 55 4d  C_OFFSET_LINENUM
4bb40 42 45 52 28 70 29 20 28 20 5c 0a 20 20 54 45 53  BER(p) ( \.  TES
4bb50 54 41 4c 4c 4f 43 5f 4f 46 46 53 45 54 5f 47 55  TALLOC_OFFSET_GU
4bb60 41 52 44 32 28 70 29 20 2b 20 73 69 7a 65 6f 66  ARD2(p) + sizeof
4bb70 28 75 33 32 29 20 2a 20 54 45 53 54 41 4c 4c 4f  (u32) * TESTALLO
4bb80 43 5f 4e 47 55 41 52 44 20 5c 0a 29 0a 23 64 65  C_NGUARD \.).#de
4bb90 66 69 6e 65 20 54 45 53 54 41 4c 4c 4f 43 5f 4f  fine TESTALLOC_O
4bba0 46 46 53 45 54 5f 46 49 4c 45 4e 41 4d 45 28 70  FFSET_FILENAME(p
4bbb0 29 20 28 20 5c 0a 20 20 54 45 53 54 41 4c 4c 4f  ) ( \.  TESTALLO
4bbc0 43 5f 4f 46 46 53 45 54 5f 4c 49 4e 45 4e 55 4d  C_OFFSET_LINENUM
4bbd0 42 45 52 28 70 29 20 2b 20 73 69 7a 65 6f 66 28  BER(p) + sizeof(
4bbe0 75 33 32 29 20 5c 0a 29 0a 23 64 65 66 69 6e 65  u32) \.).#define
4bbf0 20 54 45 53 54 41 4c 4c 4f 43 5f 4f 46 46 53 45   TESTALLOC_OFFSE
4bc00 54 5f 55 53 45 52 28 70 29 20 28 20 5c 0a 20 20  T_USER(p) ( \.  
4bc10 54 45 53 54 41 4c 4c 4f 43 5f 4f 46 46 53 45 54  TESTALLOC_OFFSET
4bc20 5f 46 49 4c 45 4e 41 4d 45 28 70 29 20 2b 20 54  _FILENAME(p) + T
4bc30 45 53 54 41 4c 4c 4f 43 5f 46 49 4c 45 53 49 5a  ESTALLOC_FILESIZ
4bc40 45 20 5c 0a 29 0a 23 64 65 66 69 6e 65 20 54 45  E \.).#define TE
4bc50 53 54 41 4c 4c 4f 43 5f 4f 46 46 53 45 54 5f 53  STALLOC_OFFSET_S
4bc60 54 41 43 4b 28 70 29 20 28 20 5c 0a 20 20 54 45  TACK(p) ( \.  TE
4bc70 53 54 41 4c 4c 4f 43 5f 4f 46 46 53 45 54 5f 55  STALLOC_OFFSET_U
4bc80 53 45 52 28 70 29 20 2b 20 54 45 53 54 41 4c 4c  SER(p) + TESTALL
4bc90 4f 43 5f 55 53 45 52 53 49 5a 45 20 2b 20 38 20  OC_USERSIZE + 8 
4bca0 2d 20 5c 0a 20 20 28 54 45 53 54 41 4c 4c 4f 43  - \.  (TESTALLOC
4bcb0 5f 4f 46 46 53 45 54 5f 55 53 45 52 28 70 29 20  _OFFSET_USER(p) 
4bcc0 25 20 38 29 20 5c 0a 29 0a 0a 23 64 65 66 69 6e  % 8) \.)..#defin
4bcd0 65 20 54 45 53 54 41 4c 4c 4f 43 5f 4f 56 45 52  e TESTALLOC_OVER
4bce0 48 45 41 44 20 28 20 5c 0a 20 20 73 69 7a 65 6f  HEAD ( \.  sizeo
4bcf0 66 28 76 6f 69 64 20 2a 29 2a 32 20 2b 20 20 20  f(void *)*2 +   
4bd00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4bd10 2f 2a 20 70 50 72 65 76 20 61 6e 64 20 70 4e 65  /* pPrev and pNe
4bd20 78 74 20 70 6f 69 6e 74 65 72 73 20 2a 2f 20 20  xt pointers */  
4bd30 20 5c 0a 20 20 54 45 53 54 41 4c 4c 4f 43 5f 4e   \.  TESTALLOC_N
4bd40 47 55 41 52 44 2a 73 69 7a 65 6f 66 28 75 33 32  GUARD*sizeof(u32
4bd50 29 2a 32 20 2b 20 20 20 20 20 20 20 20 20 20 20  )*2 +           
4bd60 20 20 20 2f 2a 20 47 75 61 72 64 20 77 6f 72 64     /* Guard word
4bd70 73 20 2a 2f 20 20 20 20 20 20 20 5c 0a 20 20 73  s */       \.  s
4bd80 69 7a 65 6f 66 28 75 33 32 29 20 2b 20 54 45 53  izeof(u32) + TES
4bd90 54 41 4c 4c 4f 43 5f 46 49 4c 45 53 49 5a 45 20  TALLOC_FILESIZE 
4bda0 2b 20 20 20 2f 2a 20 46 69 6c 65 20 61 6e 64 20  +   /* File and 
4bdb0 6c 69 6e 65 20 6e 75 6d 62 65 72 20 2a 2f 20 20  line number */  
4bdc0 20 20 20 20 20 5c 0a 20 20 54 45 53 54 41 4c 4c       \.  TESTALL
4bdd0 4f 43 5f 55 53 45 52 53 49 5a 45 20 2b 20 20 20  OC_USERSIZE +   
4bde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4bdf0 20 55 73 65 72 20 73 74 72 69 6e 67 20 2a 2f 20   User string */ 
4be00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
4be10 0a 20 20 54 45 53 54 41 4c 4c 4f 43 5f 53 54 41  .  TESTALLOC_STA
4be20 43 4b 53 49 5a 45 20 20 20 20 20 20 20 20 20 20  CKSIZE          
4be30 20 20 20 20 20 20 20 20 2f 2a 20 62 61 63 6b 74          /* backt
4be40 72 61 63 65 28 29 20 73 74 61 63 6b 20 2a 2f 20  race() stack */ 
4be50 20 20 20 20 20 20 20 20 20 5c 0a 29 0a 0a 0a 2f           \.).../
4be60 2a 0a 2a 2a 20 46 6f 72 20 6b 65 65 70 69 6e 67  *.** For keeping
4be70 20 74 72 61 63 6b 20 6f 66 20 74 68 65 20 6e 75   track of the nu
4be80 6d 62 65 72 20 6f 66 20 6d 61 6c 6c 6f 63 73 20  mber of mallocs 
4be90 61 6e 64 20 66 72 65 65 73 2e 20 20 20 54 68 69  and frees.   Thi
4bea0 73 0a 2a 2a 20 69 73 20 75 73 65 64 20 74 6f 20  s.** is used to 
4beb0 63 68 65 63 6b 20 66 6f 72 20 6d 65 6d 6f 72 79  check for memory
4bec0 20 6c 65 61 6b 73 2e 20 20 54 68 65 20 69 4d 61   leaks.  The iMa
4bed0 6c 6c 6f 63 46 61 69 6c 20 61 6e 64 20 69 4d 61  llocFail and iMa
4bee0 6c 6c 6f 63 52 65 73 65 74 0a 2a 2a 20 76 61 6c  llocReset.** val
4bef0 75 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20  ues are used to 
4bf00 73 69 6d 75 6c 61 74 65 20 6d 61 6c 6c 6f 63 28  simulate malloc(
4bf10 29 20 66 61 69 6c 75 72 65 73 20 64 75 72 69 6e  ) failures durin
4bf20 67 20 74 65 73 74 69 6e 67 20 69 6e 20 0a 2a 2a  g testing in .**
4bf30 20 6f 72 64 65 72 20 74 6f 20 76 65 72 69 66 79   order to verify
4bf40 20 74 68 61 74 20 74 68 65 20 6c 69 62 72 61 72   that the librar
4bf50 79 20 63 6f 72 72 65 63 74 6c 79 20 68 61 6e 64  y correctly hand
4bf60 6c 65 73 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d 65  les an out-of-me
4bf70 6d 6f 72 79 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f  mory.** conditio
4bf80 6e 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65  n..*/.int sqlite
4bf90 33 5f 6e 4d 61 6c 6c 6f 63 3b 20 20 20 20 20 20  3_nMalloc;      
4bfa0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
4bfb0 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 63  sqliteMalloc() c
4bfc0 61 6c 6c 73 20 2a 2f 0a 69 6e 74 20 73 71 6c 69  alls */.int sqli
4bfd0 74 65 33 5f 6e 46 72 65 65 3b 20 20 20 20 20 20  te3_nFree;      
4bfe0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
4bff0 66 20 73 71 6c 69 74 65 46 72 65 65 28 29 20 63  f sqliteFree() c
4c000 61 6c 6c 73 20 2a 2f 0a 69 6e 74 20 73 71 6c 69  alls */.int sqli
4c010 74 65 33 5f 6d 65 6d 55 73 65 64 3b 20 20 20 20  te3_memUsed;    
4c020 20 20 20 20 20 2f 2a 20 54 4f 44 4f 20 54 6f 74       /* TODO Tot
4c030 61 6c 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e  al memory obtain
4c040 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 20 2a  ed from malloc *
4c050 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 65  /.int sqlite3_me
4c060 6d 4d 61 78 3b 20 20 20 20 20 20 20 20 20 20 2f  mMax;          /
4c070 2a 20 54 4f 44 4f 20 4d 65 6d 20 75 73 61 67 65  * TODO Mem usage
4c080 20 68 69 67 68 2d 77 61 74 65 72 20 6d 61 72 6b   high-water mark
4c090 20 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f   */.int sqlite3_
4c0a0 69 4d 61 6c 6c 6f 63 46 61 69 6c 3b 20 20 20 20  iMallocFail;    
4c0b0 20 2f 2a 20 46 61 69 6c 20 73 71 6c 69 74 65 4d   /* Fail sqliteM
4c0c0 61 6c 6c 6f 63 28 29 20 61 66 74 65 72 20 74 68  alloc() after th
4c0d0 69 73 20 6d 61 6e 79 20 63 61 6c 6c 73 20 2a 2f  is many calls */
4c0e0 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 4d 61  .int sqlite3_iMa
4c0f0 6c 6c 6f 63 52 65 73 65 74 20 3d 20 2d 31 3b 20  llocReset = -1; 
4c100 2f 2a 20 57 68 65 6e 20 69 4d 61 6c 6c 6f 63 46  /* When iMallocF
4c110 61 69 6c 20 72 65 61 63 68 65 73 20 30 2c 20 73  ail reaches 0, s
4c120 65 74 20 74 6f 20 74 68 69 73 20 2a 2f 0a 0a 76  et to this */..v
4c130 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 70 46 69  oid *sqlite3_pFi
4c140 72 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20  rst = 0;        
4c150 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6c   /* Pointer to l
4c160 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 6c  inked list of al
4c170 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 69 6e 74  locations */.int
4c180 20 73 71 6c 69 74 65 33 5f 6e 4d 61 78 41 6c 6c   sqlite3_nMaxAll
4c190 6f 63 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f  oc = 0;        /
4c1a0 2a 20 48 69 67 68 20 77 61 74 65 72 20 6d 61 72  * High water mar
4c1b0 6b 20 6f 66 20 54 68 72 65 61 64 44 61 74 61 2e  k of ThreadData.
4c1c0 6e 41 6c 6c 6f 63 20 2a 2f 0a 69 6e 74 20 73 71  nAlloc */.int sq
4c1d0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 44 69 73 61  lite3_mallocDisa
4c1e0 6c 6c 6f 77 65 64 20 3d 20 30 3b 20 2f 2a 20 61  llowed = 0; /* a
4c1f0 73 73 65 72 74 28 29 20 69 6e 20 73 71 6c 69 74  ssert() in sqlit
4c200 65 33 4d 61 6c 6c 6f 63 28 29 20 69 66 20 73 65  e3Malloc() if se
4c210 74 20 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33  t */.int sqlite3
4c220 5f 69 73 46 61 69 6c 20 3d 20 30 3b 20 20 20 20  _isFail = 0;    
4c230 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
4c240 66 20 61 6c 6c 20 6d 61 6c 6c 6f 63 20 63 61 6c  f all malloc cal
4c250 6c 73 20 73 68 6f 75 6c 64 20 66 61 69 6c 20 2a  ls should fail *
4c260 2f 0a 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  /.const char *sq
4c270 6c 69 74 65 33 5f 7a 46 69 6c 65 20 3d 20 30 3b  lite3_zFile = 0;
4c280 20 20 20 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 20      /* Filename 
4c290 74 6f 20 61 73 73 6f 63 69 61 74 65 20 64 65 62  to associate deb
4c2a0 75 67 20 69 6e 66 6f 20 77 69 74 68 20 2a 2f 0a  ug info with */.
4c2b0 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 4c 69 6e  int sqlite3_iLin
4c2c0 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  e = 0;          
4c2d0 20 20 2f 2a 20 4c 69 6e 65 20 6e 75 6d 62 65 72    /* Line number
4c2e0 20 66 6f 72 20 64 65 62 75 67 20 69 6e 66 6f 20   for debug info 
4c2f0 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d  */.int sqlite3_m
4c300 61 6c 6c 6f 63 66 61 69 6c 5f 74 72 61 63 65 20  allocfail_trace 
4c310 3d 20 30 3b 20 2f 2a 20 50 72 69 6e 74 20 61 20  = 0; /* Print a 
4c320 6d 73 67 20 6f 6e 20 6d 61 6c 6c 6f 63 20 66 61  msg on malloc fa
4c330 69 6c 20 69 66 20 74 72 75 65 20 2a 2f 0a 0a 2f  il if true */../
4c340 2a 0a 2a 2a 20 43 68 65 63 6b 20 66 6f 72 20 61  *.** Check for a
4c350 20 73 69 6d 75 6c 61 74 65 64 20 6d 65 6d 6f 72   simulated memor
4c360 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69  y allocation fai
4c370 6c 75 72 65 2e 20 20 52 65 74 75 72 6e 20 74 72  lure.  Return tr
4c380 75 65 20 69 66 0a 2a 2a 20 74 68 65 20 66 61 69  ue if.** the fai
4c390 6c 75 72 65 20 73 68 6f 75 6c 64 20 62 65 20 73  lure should be s
4c3a0 69 6d 75 6c 61 74 65 64 2e 20 20 52 65 74 75 72  imulated.  Retur
4c3b0 6e 20 66 61 6c 73 65 20 74 6f 20 70 72 6f 63 65  n false to proce
4c3c0 65 64 20 61 73 20 6e 6f 72 6d 61 6c 2e 0a 2a 2f  ed as normal..*/
4c3d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4c3e0 69 6e 74 20 73 71 6c 69 74 65 33 54 65 73 74 4d  int sqlite3TestM
4c3f0 61 6c 6c 6f 63 46 61 69 6c 28 29 7b 0a 20 20 69  allocFail(){.  i
4c400 66 28 20 73 71 6c 69 74 65 33 5f 69 73 46 61 69  f( sqlite3_isFai
4c410 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  l ){.    return 
4c420 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c  1;.  }.  if( sql
4c430 69 74 65 33 5f 69 4d 61 6c 6c 6f 63 46 61 69 6c  ite3_iMallocFail
4c440 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  >=0 ){.    sqlit
4c450 65 33 5f 69 4d 61 6c 6c 6f 63 46 61 69 6c 2d 2d  e3_iMallocFail--
4c460 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
4c470 33 5f 69 4d 61 6c 6c 6f 63 46 61 69 6c 3d 3d 30  3_iMallocFail==0
4c480 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
4c490 33 5f 69 4d 61 6c 6c 6f 63 46 61 69 6c 20 3d 20  3_iMallocFail = 
4c4a0 73 71 6c 69 74 65 33 5f 69 4d 61 6c 6c 6f 63 52  sqlite3_iMallocR
4c4b0 65 73 65 74 3b 0a 20 20 20 20 20 20 73 71 6c 69  eset;.      sqli
4c4c0 74 65 33 5f 69 73 46 61 69 6c 20 3d 20 31 3b 0a  te3_isFail = 1;.
4c4d0 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
4c4e0 33 5f 6d 61 6c 6c 6f 63 66 61 69 6c 5f 74 72 61  3_mallocfail_tra
4c4f0 63 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 73  ce ){.         s
4c500 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
4c510 66 28 22 23 23 23 5f 6d 61 6c 6c 6f 63 5f 66 61  f("###_malloc_fa
4c520 69 6c 73 5f 23 23 23 5c 6e 22 29 3b 0a 20 20 20  ils_###\n");.   
4c530 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72     }.      retur
4c540 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  n 1;.    }.  }. 
4c550 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
4c560 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74  .** The argument
4c570 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 72 65   is a pointer re
4c580 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65  turned by sqlite
4c590 33 4f 73 4d 61 6c 6c 6f 63 28 29 20 6f 72 20 78  3OsMalloc() or x
4c5a0 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 61 73  Realloc()..** as
4c5b0 73 65 72 74 28 29 20 74 68 61 74 20 74 68 65 20  sert() that the 
4c5c0 66 69 72 73 74 20 61 6e 64 20 6c 61 73 74 20 28  first and last (
4c5d0 54 45 53 54 41 4c 4c 4f 43 5f 4e 47 55 41 52 44  TESTALLOC_NGUARD
4c5e0 2a 34 29 20 62 79 74 65 73 20 61 72 65 20 73 65  *4) bytes are se
4c5f0 74 20 74 6f 20 74 68 65 0a 2a 2a 20 76 61 6c 75  t to the.** valu
4c600 65 73 20 73 65 74 20 62 79 20 74 68 65 20 61 70  es set by the ap
4c610 70 6c 79 47 75 61 72 64 73 28 29 20 66 75 6e 63  plyGuards() func
4c620 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
4c630 76 6f 69 64 20 63 68 65 63 6b 47 75 61 72 64 73  void checkGuards
4c640 28 75 33 32 20 2a 70 29 0a 7b 0a 20 20 69 6e 74  (u32 *p).{.  int
4c650 20 69 3b 0a 20 20 63 68 61 72 20 2a 7a 41 6c 6c   i;.  char *zAll
4c660 6f 63 20 3d 20 28 63 68 61 72 20 2a 29 70 3b 0a  oc = (char *)p;.
4c670 20 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 2f 2a    char *z;..  /*
4c680 20 46 69 72 73 74 20 73 65 74 20 6f 66 20 67 75   First set of gu
4c690 61 72 64 20 77 6f 72 64 73 20 2a 2f 0a 20 20 7a  ard words */.  z
4c6a0 20 3d 20 26 7a 41 6c 6c 6f 63 5b 54 45 53 54 41   = &zAlloc[TESTA
4c6b0 4c 4c 4f 43 5f 4f 46 46 53 45 54 5f 47 55 41 52  LLOC_OFFSET_GUAR
4c6c0 44 31 28 70 29 5d 3b 0a 20 20 66 6f 72 28 69 3d  D1(p)];.  for(i=
4c6d0 30 3b 20 69 3c 54 45 53 54 41 4c 4c 4f 43 5f 4e  0; i<TESTALLOC_N
4c6e0 47 55 41 52 44 3b 20 69 2b 2b 29 7b 0a 20 20 20  GUARD; i++){.   
4c6f0 20 61 73 73 65 72 74 28 28 28 75 33 32 20 2a 29   assert(((u32 *)
4c700 7a 29 5b 69 5d 3d 3d 30 78 64 65 61 64 31 31 32  z)[i]==0xdead112
4c710 32 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65  2);.  }..  /* Se
4c720 63 6f 6e 64 20 73 65 74 20 6f 66 20 67 75 61 72  cond set of guar
4c730 64 20 77 6f 72 64 73 20 2a 2f 0a 20 20 7a 20 3d  d words */.  z =
4c740 20 26 7a 41 6c 6c 6f 63 5b 54 45 53 54 41 4c 4c   &zAlloc[TESTALL
4c750 4f 43 5f 4f 46 46 53 45 54 5f 47 55 41 52 44 32  OC_OFFSET_GUARD2
4c760 28 70 29 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  (p)];.  for(i=0;
4c770 20 69 3c 54 45 53 54 41 4c 4c 4f 43 5f 4e 47 55   i<TESTALLOC_NGU
4c780 41 52 44 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 75  ARD; i++){.    u
4c790 33 32 20 67 75 61 72 64 20 3d 20 30 3b 0a 20 20  32 guard = 0;.  
4c7a0 20 20 6d 65 6d 63 70 79 28 26 67 75 61 72 64 2c    memcpy(&guard,
4c7b0 20 26 7a 5b 69 2a 73 69 7a 65 6f 66 28 75 33 32   &z[i*sizeof(u32
4c7c0 29 5d 2c 20 73 69 7a 65 6f 66 28 75 33 32 29 29  )], sizeof(u32))
4c7d0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 67 75 61  ;.    assert(gua
4c7e0 72 64 3d 3d 30 78 64 65 61 64 33 33 34 34 29 3b  rd==0xdead3344);
4c7f0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
4c800 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20  e argument is a 
4c810 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e 65 64  pointer returned
4c820 20 62 79 20 73 71 6c 69 74 65 33 4f 73 4d 61 6c   by sqlite3OsMal
4c830 6c 6f 63 28 29 20 6f 72 20 52 65 61 6c 6c 6f 63  loc() or Realloc
4c840 28 29 2e 20 54 68 65 0a 2a 2a 20 66 69 72 73 74  (). The.** first
4c850 20 61 6e 64 20 6c 61 73 74 20 28 54 45 53 54 41   and last (TESTA
4c860 4c 4c 4f 43 5f 4e 47 55 41 52 44 2a 34 29 20 62  LLOC_NGUARD*4) b
4c870 79 74 65 73 20 61 72 65 20 73 65 74 20 74 6f 20  ytes are set to 
4c880 6b 6e 6f 77 6e 20 76 61 6c 75 65 73 20 66 6f 72  known values for
4c890 20 75 73 65 20 61 73 20 0a 2a 2a 20 67 75 61 72   use as .** guar
4c8a0 64 2d 70 6f 73 74 73 2e 0a 2a 2f 0a 73 74 61 74  d-posts..*/.stat
4c8b0 69 63 20 76 6f 69 64 20 61 70 70 6c 79 47 75 61  ic void applyGua
4c8c0 72 64 73 28 75 33 32 20 2a 70 29 0a 7b 0a 20 20  rds(u32 *p).{.  
4c8d0 69 6e 74 20 69 3b 0a 20 20 63 68 61 72 20 2a 7a  int i;.  char *z
4c8e0 3b 0a 20 20 63 68 61 72 20 2a 7a 41 6c 6c 6f 63  ;.  char *zAlloc
4c8f0 20 3d 20 28 63 68 61 72 20 2a 29 70 3b 0a 0a 20   = (char *)p;.. 
4c900 20 2f 2a 20 46 69 72 73 74 20 73 65 74 20 6f 66   /* First set of
4c910 20 67 75 61 72 64 20 77 6f 72 64 73 20 2a 2f 0a   guard words */.
4c920 20 20 7a 20 3d 20 26 7a 41 6c 6c 6f 63 5b 54 45    z = &zAlloc[TE
4c930 53 54 41 4c 4c 4f 43 5f 4f 46 46 53 45 54 5f 47  STALLOC_OFFSET_G
4c940 55 41 52 44 31 28 70 29 5d 3b 0a 20 20 66 6f 72  UARD1(p)];.  for
4c950 28 69 3d 30 3b 20 69 3c 54 45 53 54 41 4c 4c 4f  (i=0; i<TESTALLO
4c960 43 5f 4e 47 55 41 52 44 3b 20 69 2b 2b 29 7b 0a  C_NGUARD; i++){.
4c970 20 20 20 20 28 28 75 33 32 20 2a 29 7a 29 5b 69      ((u32 *)z)[i
4c980 5d 20 3d 20 30 78 64 65 61 64 31 31 32 32 3b 0a  ] = 0xdead1122;.
4c990 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 63 6f 6e 64    }..  /* Second
4c9a0 20 73 65 74 20 6f 66 20 67 75 61 72 64 20 77 6f   set of guard wo
4c9b0 72 64 73 20 2a 2f 0a 20 20 7a 20 3d 20 26 7a 41  rds */.  z = &zA
4c9c0 6c 6c 6f 63 5b 54 45 53 54 41 4c 4c 4f 43 5f 4f  lloc[TESTALLOC_O
4c9d0 46 46 53 45 54 5f 47 55 41 52 44 32 28 70 29 5d  FFSET_GUARD2(p)]
4c9e0 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 54  ;.  for(i=0; i<T
4c9f0 45 53 54 41 4c 4c 4f 43 5f 4e 47 55 41 52 44 3b  ESTALLOC_NGUARD;
4ca00 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 61 74 69   i++){.    stati
4ca10 63 20 63 6f 6e 73 74 20 69 6e 74 20 67 75 61 72  c const int guar
4ca20 64 20 3d 20 30 78 64 65 61 64 33 33 34 34 3b 0a  d = 0xdead3344;.
4ca30 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 5b 69 2a      memcpy(&z[i*
4ca40 73 69 7a 65 6f 66 28 75 33 32 29 5d 2c 20 26 67  sizeof(u32)], &g
4ca50 75 61 72 64 2c 20 73 69 7a 65 6f 66 28 75 33 32  uard, sizeof(u32
4ca60 29 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 69  ));.  }..  /* Li
4ca70 6e 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 7a  ne number */.  z
4ca80 20 3d 20 26 28 28 63 68 61 72 20 2a 29 7a 29 5b   = &((char *)z)[
4ca90 54 45 53 54 41 4c 4c 4f 43 5f 4e 47 55 41 52 44  TESTALLOC_NGUARD
4caa0 2a 73 69 7a 65 6f 66 28 75 33 32 29 5d 3b 20 20  *sizeof(u32)];  
4cab0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47 75             /* Gu
4cac0 61 72 64 20 77 6f 72 64 73 20 2a 2f 0a 20 20 7a  ard words */.  z
4cad0 20 3d 20 26 7a 41 6c 6c 6f 63 5b 54 45 53 54 41   = &zAlloc[TESTA
4cae0 4c 4c 4f 43 5f 4f 46 46 53 45 54 5f 4c 49 4e 45  LLOC_OFFSET_LINE
4caf0 4e 55 4d 42 45 52 28 70 29 5d 3b 0a 20 20 6d 65  NUMBER(p)];.  me
4cb00 6d 63 70 79 28 7a 2c 20 26 73 71 6c 69 74 65 33  mcpy(z, &sqlite3
4cb10 5f 69 4c 69 6e 65 2c 20 73 69 7a 65 6f 66 28 75  _iLine, sizeof(u
4cb20 33 32 29 29 3b 0a 0a 20 20 2f 2a 20 46 69 6c 65  32));..  /* File
4cb30 20 6e 61 6d 65 20 2a 2f 0a 20 20 7a 20 3d 20 26   name */.  z = &
4cb40 7a 41 6c 6c 6f 63 5b 54 45 53 54 41 4c 4c 4f 43  zAlloc[TESTALLOC
4cb50 5f 4f 46 46 53 45 54 5f 46 49 4c 45 4e 41 4d 45  _OFFSET_FILENAME
4cb60 28 70 29 5d 3b 0a 20 20 73 74 72 6e 63 70 79 28  (p)];.  strncpy(
4cb70 7a 2c 20 73 71 6c 69 74 65 33 5f 7a 46 69 6c 65  z, sqlite3_zFile
4cb80 2c 20 54 45 53 54 41 4c 4c 4f 43 5f 46 49 4c 45  , TESTALLOC_FILE
4cb90 53 49 5a 45 29 3b 0a 20 20 7a 5b 54 45 53 54 41  SIZE);.  z[TESTA
4cba0 4c 4c 4f 43 5f 46 49 4c 45 53 49 5a 45 20 2d 20  LLOC_FILESIZE - 
4cbb0 31 5d 20 3d 20 27 5c 30 27 3b 0a 0a 20 20 2f 2a  1] = '\0';..  /*
4cbc0 20 55 73 65 72 20 73 74 72 69 6e 67 20 2a 2f 0a   User string */.
4cbd0 20 20 7a 20 3d 20 26 7a 41 6c 6c 6f 63 5b 54 45    z = &zAlloc[TE
4cbe0 53 54 41 4c 4c 4f 43 5f 4f 46 46 53 45 54 5f 55  STALLOC_OFFSET_U
4cbf0 53 45 52 28 70 29 5d 3b 0a 20 20 7a 5b 30 5d 20  SER(p)];.  z[0] 
4cc00 3d 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  = 0;.  if( sqlit
4cc10 65 33 5f 6d 61 6c 6c 6f 63 5f 69 64 20 29 7b 0a  e3_malloc_id ){.
4cc20 20 20 20 20 73 74 72 6e 63 70 79 28 7a 2c 20 73      strncpy(z, s
4cc30 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 5f 69 64  qlite3_malloc_id
4cc40 2c 20 54 45 53 54 41 4c 4c 4f 43 5f 55 53 45 52  , TESTALLOC_USER
4cc50 53 49 5a 45 29 3b 0a 20 20 20 20 7a 5b 54 45 53  SIZE);.    z[TES
4cc60 54 41 4c 4c 4f 43 5f 55 53 45 52 53 49 5a 45 2d  TALLOC_USERSIZE-
4cc70 31 5d 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f  1] = 0;.  }..  /
4cc80 2a 20 62 61 63 6b 74 72 61 63 65 28 29 20 73 74  * backtrace() st
4cc90 61 63 6b 20 2a 2f 0a 20 20 7a 20 3d 20 26 7a 41  ack */.  z = &zA
4cca0 6c 6c 6f 63 5b 54 45 53 54 41 4c 4c 4f 43 5f 4f  lloc[TESTALLOC_O
4ccb0 46 46 53 45 54 5f 53 54 41 43 4b 28 70 29 5d 3b  FFSET_STACK(p)];
4ccc0 0a 20 20 62 61 63 6b 74 72 61 63 65 28 28 76 6f  .  backtrace((vo
4ccd0 69 64 20 2a 2a 29 7a 2c 20 54 45 53 54 41 4c 4c  id **)z, TESTALL
4cce0 4f 43 5f 53 54 41 43 4b 46 52 41 4d 45 53 29 3b  OC_STACKFRAMES);
4ccf0 0a 0a 20 20 2f 2a 20 53 61 6e 69 74 79 20 63 68  ..  /* Sanity ch
4cd00 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  eck to make sure
4cd10 20 63 68 65 63 6b 47 75 61 72 64 73 28 29 20 69   checkGuards() i
4cd20 73 20 77 6f 72 6b 69 6e 67 20 2a 2f 0a 20 20 63  s working */.  c
4cd30 68 65 63 6b 47 75 61 72 64 73 28 70 29 3b 0a 7d  heckGuards(p);.}
4cd40 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75  ../*.** The argu
4cd50 6d 65 6e 74 20 69 73 20 61 20 6d 61 6c 6c 6f 63  ment is a malloc
4cd60 28 29 65 64 20 70 6f 69 6e 74 65 72 20 61 73 20  ()ed pointer as 
4cd70 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20  returned by the 
4cd80 74 65 73 74 2d 77 72 61 70 70 65 72 2e 0a 2a 2a  test-wrapper..**
4cd90 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
4cda0 72 20 74 6f 20 74 68 65 20 4f 73 20 6c 65 76 65  r to the Os leve
4cdb0 6c 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f  l allocation..*/
4cdc0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 67 65  .static void *ge
4cdd0 74 4f 73 50 6f 69 6e 74 65 72 28 76 6f 69 64 20  tOsPointer(void 
4cde0 2a 70 29 0a 7b 0a 20 20 63 68 61 72 20 2a 7a 20  *p).{.  char *z 
4cdf0 3d 20 28 63 68 61 72 20 2a 29 70 3b 0a 20 20 72  = (char *)p;.  r
4ce00 65 74 75 72 6e 20 28 76 6f 69 64 20 2a 29 28 26  eturn (void *)(&
4ce10 7a 5b 2d 31 20 2a 20 54 45 53 54 41 4c 4c 4f 43  z[-1 * TESTALLOC
4ce20 5f 4f 46 46 53 45 54 5f 44 41 54 41 28 70 29 5d  _OFFSET_DATA(p)]
4ce30 29 3b 0a 7d 0a 0a 0a 23 69 66 20 53 51 4c 49 54  );.}...#if SQLIT
4ce40 45 5f 4d 45 4d 44 45 42 55 47 3e 31 0a 2f 2a 0a  E_MEMDEBUG>1./*.
4ce50 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20  ** The argument 
4ce60 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 4f 73 20  points to an Os 
4ce70 6c 65 76 65 6c 20 61 6c 6c 6f 63 61 74 69 6f 6e  level allocation
4ce80 2e 20 4c 69 6e 6b 20 69 74 20 69 6e 74 6f 20 74  . Link it into t
4ce90 68 65 20 74 68 72 65 61 64 73 20 6c 69 73 74 0a  he threads list.
4cea0 2a 2a 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e  ** of allocation
4ceb0 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  s..*/.static voi
4cec0 64 20 6c 69 6e 6b 41 6c 6c 6f 63 28 76 6f 69 64  d linkAlloc(void
4ced0 20 2a 70 29 7b 0a 20 20 76 6f 69 64 20 2a 2a 70   *p){.  void **p
4cee0 70 20 3d 20 28 76 6f 69 64 20 2a 2a 29 70 3b 0a  p = (void **)p;.
4cef0 20 20 70 70 5b 30 5d 20 3d 20 30 3b 0a 20 20 70    pp[0] = 0;.  p
4cf00 70 5b 31 5d 20 3d 20 73 71 6c 69 74 65 33 5f 70  p[1] = sqlite3_p
4cf10 46 69 72 73 74 3b 0a 20 20 69 66 28 20 73 71 6c  First;.  if( sql
4cf20 69 74 65 33 5f 70 46 69 72 73 74 20 29 7b 0a 20  ite3_pFirst ){. 
4cf30 20 20 20 28 28 76 6f 69 64 20 2a 2a 29 73 71 6c     ((void **)sql
4cf40 69 74 65 33 5f 70 46 69 72 73 74 29 5b 30 5d 20  ite3_pFirst)[0] 
4cf50 3d 20 70 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  = p;.  }.  sqlit
4cf60 65 33 5f 70 46 69 72 73 74 20 3d 20 70 3b 0a 7d  e3_pFirst = p;.}
4cf70 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75  ../*.** The argu
4cf80 6d 65 6e 74 20 70 6f 69 6e 74 73 20 74 6f 20 61  ment points to a
4cf90 6e 20 4f 73 20 6c 65 76 65 6c 20 61 6c 6c 6f 63  n Os level alloc
4cfa0 61 74 69 6f 6e 2e 20 55 6e 6c 69 6e 6b 65 20 69  ation. Unlinke i
4cfb0 74 20 66 72 6f 6d 20 74 68 65 20 74 68 72 65 61  t from the threa
4cfc0 64 73 0a 2a 2a 20 6c 69 73 74 20 6f 66 20 61 6c  ds.** list of al
4cfd0 6c 6f 63 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74  locations..*/.st
4cfe0 61 74 69 63 20 76 6f 69 64 20 75 6e 6c 69 6e 6b  atic void unlink
4cff0 41 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 29 0a 7b  Alloc(void *p).{
4d000 0a 20 20 76 6f 69 64 20 2a 2a 70 70 20 3d 20 28  .  void **pp = (
4d010 76 6f 69 64 20 2a 2a 29 70 3b 0a 20 20 69 66 28  void **)p;.  if(
4d020 20 70 3d 3d 73 71 6c 69 74 65 33 5f 70 46 69 72   p==sqlite3_pFir
4d030 73 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  st ){.    assert
4d040 28 21 70 70 5b 30 5d 29 3b 0a 20 20 20 20 61 73  (!pp[0]);.    as
4d050 73 65 72 74 28 21 70 70 5b 31 5d 20 7c 7c 20 28  sert(!pp[1] || (
4d060 28 76 6f 69 64 20 2a 2a 29 28 70 70 5b 31 5d 29  (void **)(pp[1])
4d070 29 5b 30 5d 3d 3d 70 29 3b 0a 20 20 20 20 73 71  )[0]==p);.    sq
4d080 6c 69 74 65 33 5f 70 46 69 72 73 74 20 3d 20 70  lite3_pFirst = p
4d090 70 5b 31 5d 3b 0a 20 20 20 20 69 66 28 20 73 71  p[1];.    if( sq
4d0a0 6c 69 74 65 33 5f 70 46 69 72 73 74 20 29 7b 0a  lite3_pFirst ){.
4d0b0 20 20 20 20 20 20 28 28 76 6f 69 64 20 2a 2a 29        ((void **)
4d0c0 73 71 6c 69 74 65 33 5f 70 46 69 72 73 74 29 5b  sqlite3_pFirst)[
4d0d0 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  0] = 0;.    }.  
4d0e0 7d 65 6c 73 65 7b 0a 20 20 20 20 76 6f 69 64 20  }else{.    void 
4d0f0 2a 2a 70 70 72 65 76 20 3d 20 70 70 5b 30 5d 3b  **pprev = pp[0];
4d100 0a 20 20 20 20 76 6f 69 64 20 2a 2a 70 6e 65 78  .    void **pnex
4d110 74 20 3d 20 70 70 5b 31 5d 3b 0a 20 20 20 20 61  t = pp[1];.    a
4d120 73 73 65 72 74 28 70 70 72 65 76 29 3b 0a 20 20  ssert(pprev);.  
4d130 20 20 61 73 73 65 72 74 28 70 70 72 65 76 5b 31    assert(pprev[1
4d140 5d 3d 3d 70 29 3b 0a 20 20 20 20 70 70 72 65 76  ]==p);.    pprev
4d150 5b 31 5d 20 3d 20 28 76 6f 69 64 20 2a 29 70 6e  [1] = (void *)pn
4d160 65 78 74 3b 0a 20 20 20 20 69 66 28 20 70 6e 65  ext;.    if( pne
4d170 78 74 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  xt ){.      asse
4d180 72 74 28 70 6e 65 78 74 5b 30 5d 3d 3d 70 29 3b  rt(pnext[0]==p);
4d190 0a 20 20 20 20 20 20 70 6e 65 78 74 5b 30 5d 20  .      pnext[0] 
4d1a0 3d 20 28 76 6f 69 64 20 2a 29 70 70 72 65 76 3b  = (void *)pprev;
4d1b0 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
4d1c0 0a 2a 2a 20 50 6f 69 6e 74 65 72 20 70 20 69 73  .** Pointer p is
4d1d0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
4d1e0 20 4f 53 20 6c 65 76 65 6c 20 61 6c 6c 6f 63 61   OS level alloca
4d1f0 74 69 6f 6e 20 74 68 61 74 20 68 61 73 20 6a 75  tion that has ju
4d200 73 74 20 62 65 65 6e 0a 2a 2a 20 72 65 61 6c 6c  st been.** reall
4d210 6f 63 28 29 65 64 2e 20 53 65 74 20 74 68 65 20  oc()ed. Set the 
4d220 6c 69 73 74 20 70 6f 69 6e 74 65 72 73 20 74 68  list pointers th
4d230 61 74 20 70 6f 69 6e 74 20 74 6f 20 74 68 69 73  at point to this
4d240 20 65 6e 74 72 79 20 74 6f 20 69 74 27 73 20 6e   entry to it's n
4d250 65 77 0a 2a 2a 20 6c 6f 63 61 74 69 6f 6e 2e 0a  ew.** location..
4d260 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72  */.static void r
4d270 65 6c 69 6e 6b 41 6c 6c 6f 63 28 76 6f 69 64 20  elinkAlloc(void 
4d280 2a 70 29 0a 7b 0a 20 20 76 6f 69 64 20 2a 2a 70  *p).{.  void **p
4d290 70 20 3d 20 28 76 6f 69 64 20 2a 2a 29 70 3b 0a  p = (void **)p;.
4d2a0 20 20 69 66 28 20 70 70 5b 30 5d 20 29 7b 0a 20    if( pp[0] ){. 
4d2b0 20 20 20 28 28 76 6f 69 64 20 2a 2a 29 28 70 70     ((void **)(pp
4d2c0 5b 30 5d 29 29 5b 31 5d 20 3d 20 70 3b 0a 20 20  [0]))[1] = p;.  
4d2d0 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
4d2e0 65 33 5f 70 46 69 72 73 74 20 3d 20 70 3b 0a 20  e3_pFirst = p;. 
4d2f0 20 7d 0a 20 20 69 66 28 20 70 70 5b 31 5d 20 29   }.  if( pp[1] )
4d300 7b 0a 20 20 20 20 28 28 76 6f 69 64 20 2a 2a 29  {.    ((void **)
4d310 28 70 70 5b 31 5d 29 29 5b 30 5d 20 3d 20 70 3b  (pp[1]))[0] = p;
4d320 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 23 64 65  .  }.}.#else.#de
4d330 66 69 6e 65 20 6c 69 6e 6b 41 6c 6c 6f 63 28 78  fine linkAlloc(x
4d340 29 0a 23 64 65 66 69 6e 65 20 72 65 6c 69 6e 6b  ).#define relink
4d350 41 6c 6c 6f 63 28 78 29 0a 23 64 65 66 69 6e 65  Alloc(x).#define
4d360 20 75 6e 6c 69 6e 6b 41 6c 6c 6f 63 28 78 29 0a   unlinkAlloc(x).
4d370 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
4d380 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 73  is function sets
4d390 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74   the result of t
4d3a0 68 65 20 54 63 6c 20 69 6e 74 65 72 70 72 65 74  he Tcl interpret
4d3b0 65 72 20 70 61 73 73 65 64 20 61 73 20 61 6e 20  er passed as an 
4d3c0 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 61  argument.** to a
4d3d0 20 6c 69 73 74 20 63 6f 6e 74 61 69 6e 69 6e 67   list containing
4d3e0 20 61 6e 20 65 6e 74 72 79 20 66 6f 72 20 65 61   an entry for ea
4d3f0 63 68 20 63 75 72 72 65 6e 74 6c 79 20 6f 75 74  ch currently out
4d400 73 74 61 6e 64 69 6e 67 20 63 61 6c 6c 20 6d 61  standing call ma
4d410 64 65 20 74 6f 20 0a 2a 2a 20 73 71 6c 69 74 65  de to .** sqlite
4d420 4d 61 6c 6c 6f 63 20 61 6e 64 20 66 72 69 65 6e  Malloc and frien
4d430 64 73 20 62 79 20 74 68 65 20 63 75 72 72 65 6e  ds by the curren
4d440 74 20 74 68 72 65 61 64 2e 20 45 61 63 68 20 6c  t thread. Each l
4d450 69 73 74 20 65 6e 74 72 79 20 69 73 20 69 74 73  ist entry is its
4d460 65 6c 66 20 61 0a 2a 2a 20 6c 69 73 74 2c 20 63  elf a.** list, c
4d470 6f 6e 73 69 73 74 69 6e 67 20 6f 66 20 74 68 65  onsisting of the
4d480 20 66 6f 6c 6c 6f 77 69 6e 67 20 28 69 6e 20 6f   following (in o
4d490 72 64 65 72 29 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  rder):.**.**    
4d4a0 20 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66   * The number of
4d4b0 20 62 79 74 65 73 20 61 6c 6c 6f 63 61 74 65 64   bytes allocated
4d4c0 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 5f 5f  .**     * The __
4d4d0 46 49 4c 45 5f 5f 20 6d 61 63 72 6f 20 61 74 20  FILE__ macro at 
4d4e0 74 68 65 20 74 69 6d 65 20 6f 66 20 74 68 65 20  the time of the 
4d4f0 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 63  sqliteMalloc() c
4d500 61 6c 6c 2e 0a 2a 2a 20 20 20 20 20 2a 20 54 68  all..**     * Th
4d510 65 20 5f 5f 4c 49 4e 45 5f 5f 20 6d 61 63 72 6f  e __LINE__ macro
4d520 20 2e 2e 2e 0a 2a 2a 20 20 20 20 20 2a 20 54 68   ....**     * Th
4d530 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 73  e value of the s
4d540 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 5f 69 64  qlite3_malloc_id
4d550 20 76 61 72 69 61 62 6c 65 20 2e 2e 2e 0a 2a 2a   variable ....**
4d560 20 20 20 20 20 2a 20 54 68 65 20 6f 75 74 70 75       * The outpu
4d570 74 20 6f 66 20 62 61 63 6b 74 72 61 63 65 28 29  t of backtrace()
4d580 20 28 69 66 20 61 76 61 69 6c 61 62 6c 65 29 20   (if available) 
4d590 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 54 6f 64 6f 3a 20  ....**.** Todo: 
4d5a0 57 65 20 63 6f 75 6c 64 20 68 61 76 65 20 61 20  We could have a 
4d5b0 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 69 73 20  version of this 
4d5c0 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 6f 75  function that ou
4d5d0 74 70 75 74 73 20 74 6f 20 73 74 64 6f 75 74 2c  tputs to stdout,
4d5e0 20 0a 2a 2a 20 74 6f 20 64 65 62 75 67 20 6d 65   .** to debug me
4d5f0 6d 6f 72 79 20 6c 65 61 6b 73 20 77 68 65 6e 20  mory leaks when 
4d600 54 63 6c 20 69 73 20 6e 6f 74 20 61 76 61 69 6c  Tcl is not avail
4d610 61 62 6c 65 2e 0a 2a 2f 0a 23 69 66 20 64 65 66  able..*/.#if def
4d620 69 6e 65 64 28 54 43 4c 53 48 29 20 26 26 20 64  ined(TCLSH) && d
4d630 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
4d640 42 55 47 29 20 26 26 20 53 51 4c 49 54 45 5f 4d  BUG) && SQLITE_M
4d650 45 4d 44 45 42 55 47 3e 31 0a 53 51 4c 49 54 45  EMDEBUG>1.SQLITE
4d660 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4d670 69 74 65 33 4f 75 74 73 74 61 6e 64 69 6e 67 4d  ite3OutstandingM
4d680 61 6c 6c 6f 63 73 28 54 63 6c 5f 49 6e 74 65 72  allocs(Tcl_Inter
4d690 70 20 2a 69 6e 74 65 72 70 29 7b 0a 20 20 76 6f  p *interp){.  vo
4d6a0 69 64 20 2a 70 3b 0a 20 20 54 63 6c 5f 4f 62 6a  id *p;.  Tcl_Obj
4d6b0 20 2a 70 52 65 73 20 3d 20 54 63 6c 5f 4e 65 77   *pRes = Tcl_New
4d6c0 4f 62 6a 28 29 3b 0a 20 20 54 63 6c 5f 49 6e 63  Obj();.  Tcl_Inc
4d6d0 72 52 65 66 43 6f 75 6e 74 28 70 52 65 73 29 3b  rRefCount(pRes);
4d6e0 0a 0a 0a 20 20 66 6f 72 28 70 3d 73 71 6c 69 74  ...  for(p=sqlit
4d6f0 65 33 5f 70 46 69 72 73 74 3b 20 70 3b 20 70 3d  e3_pFirst; p; p=
4d700 28 28 76 6f 69 64 20 2a 2a 29 70 29 5b 31 5d 29  ((void **)p)[1])
4d710 7b 0a 20 20 20 20 54 63 6c 5f 4f 62 6a 20 2a 70  {.    Tcl_Obj *p
4d720 45 6e 74 72 79 20 3d 20 54 63 6c 5f 4e 65 77 4f  Entry = Tcl_NewO
4d730 62 6a 28 29 3b 0a 20 20 20 20 54 63 6c 5f 4f 62  bj();.    Tcl_Ob
4d740 6a 20 2a 70 53 74 61 63 6b 20 3d 20 54 63 6c 5f  j *pStack = Tcl_
4d750 4e 65 77 4f 62 6a 28 29 3b 0a 20 20 20 20 63 68  NewObj();.    ch
4d760 61 72 20 2a 7a 3b 0a 20 20 20 20 75 33 32 20 69  ar *z;.    u32 i
4d770 4c 69 6e 65 3b 0a 20 20 20 20 69 6e 74 20 6e 42  Line;.    int nB
4d780 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33 4f 73  ytes = sqlite3Os
4d790 41 6c 6c 6f 63 61 74 69 6f 6e 53 69 7a 65 28 70  AllocationSize(p
4d7a0 29 20 2d 20 54 45 53 54 41 4c 4c 4f 43 5f 4f 56  ) - TESTALLOC_OV
4d7b0 45 52 48 45 41 44 3b 0a 20 20 20 20 63 68 61 72  ERHEAD;.    char
4d7c0 20 2a 7a 41 6c 6c 6f 63 20 3d 20 28 63 68 61 72   *zAlloc = (char
4d7d0 20 2a 29 70 3b 0a 20 20 20 20 69 6e 74 20 69 3b   *)p;.    int i;
4d7e0 0a 0a 20 20 20 20 54 63 6c 5f 4c 69 73 74 4f 62  ..    Tcl_ListOb
4d7f0 6a 41 70 70 65 6e 64 45 6c 65 6d 65 6e 74 28 30  jAppendElement(0
4d800 2c 20 70 45 6e 74 72 79 2c 20 54 63 6c 5f 4e 65  , pEntry, Tcl_Ne
4d810 77 49 6e 74 4f 62 6a 28 6e 42 79 74 65 73 29 29  wIntObj(nBytes))
4d820 3b 0a 0a 20 20 20 20 7a 20 3d 20 26 7a 41 6c 6c  ;..    z = &zAll
4d830 6f 63 5b 54 45 53 54 41 4c 4c 4f 43 5f 4f 46 46  oc[TESTALLOC_OFF
4d840 53 45 54 5f 46 49 4c 45 4e 41 4d 45 28 70 29 5d  SET_FILENAME(p)]
4d850 3b 0a 20 20 20 20 54 63 6c 5f 4c 69 73 74 4f 62  ;.    Tcl_ListOb
4d860 6a 41 70 70 65 6e 64 45 6c 65 6d 65 6e 74 28 30  jAppendElement(0
4d870 2c 20 70 45 6e 74 72 79 2c 20 54 63 6c 5f 4e 65  , pEntry, Tcl_Ne
4d880 77 53 74 72 69 6e 67 4f 62 6a 28 7a 2c 20 2d 31  wStringObj(z, -1
4d890 29 29 3b 0a 0a 20 20 20 20 7a 20 3d 20 26 7a 41  ));..    z = &zA
4d8a0 6c 6c 6f 63 5b 54 45 53 54 41 4c 4c 4f 43 5f 4f  lloc[TESTALLOC_O
4d8b0 46 46 53 45 54 5f 4c 49 4e 45 4e 55 4d 42 45 52  FFSET_LINENUMBER
4d8c0 28 70 29 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79  (p)];.    memcpy
4d8d0 28 26 69 4c 69 6e 65 2c 20 7a 2c 20 73 69 7a 65  (&iLine, z, size
4d8e0 6f 66 28 75 33 32 29 29 3b 0a 20 20 20 20 54 63  of(u32));.    Tc
4d8f0 6c 5f 4c 69 73 74 4f 62 6a 41 70 70 65 6e 64 45  l_ListObjAppendE
4d900 6c 65 6d 65 6e 74 28 30 2c 20 70 45 6e 74 72 79  lement(0, pEntry
4d910 2c 20 54 63 6c 5f 4e 65 77 49 6e 74 4f 62 6a 28  , Tcl_NewIntObj(
4d920 69 4c 69 6e 65 29 29 3b 0a 0a 20 20 20 20 7a 20  iLine));..    z 
4d930 3d 20 26 7a 41 6c 6c 6f 63 5b 54 45 53 54 41 4c  = &zAlloc[TESTAL
4d940 4c 4f 43 5f 4f 46 46 53 45 54 5f 55 53 45 52 28  LOC_OFFSET_USER(
4d950 70 29 5d 3b 0a 20 20 20 20 54 63 6c 5f 4c 69 73  p)];.    Tcl_Lis
4d960 74 4f 62 6a 41 70 70 65 6e 64 45 6c 65 6d 65 6e  tObjAppendElemen
4d970 74 28 30 2c 20 70 45 6e 74 72 79 2c 20 54 63 6c  t(0, pEntry, Tcl
4d980 5f 4e 65 77 53 74 72 69 6e 67 4f 62 6a 28 7a 2c  _NewStringObj(z,
4d990 20 2d 31 29 29 3b 0a 0a 20 20 20 20 7a 20 3d 20   -1));..    z = 
4d9a0 26 7a 41 6c 6c 6f 63 5b 54 45 53 54 41 4c 4c 4f  &zAlloc[TESTALLO
4d9b0 43 5f 4f 46 46 53 45 54 5f 53 54 41 43 4b 28 70  C_OFFSET_STACK(p
4d9c0 29 5d 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  )];.    for(i=0;
4d9d0 20 69 3c 54 45 53 54 41 4c 4c 4f 43 5f 53 54 41   i<TESTALLOC_STA
4d9e0 43 4b 46 52 41 4d 45 53 3b 20 69 2b 2b 29 7b 0a  CKFRAMES; i++){.
4d9f0 20 20 20 20 20 20 63 68 61 72 20 7a 48 65 78 5b        char zHex[
4da00 31 32 38 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69  128];.      sqli
4da10 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
4da20 65 6f 66 28 7a 48 65 78 29 2c 20 7a 48 65 78 2c  eof(zHex), zHex,
4da30 20 22 25 70 22 2c 20 28 28 76 6f 69 64 20 2a 2a   "%p", ((void **
4da40 29 7a 29 5b 69 5d 29 3b 0a 20 20 20 20 20 20 54  )z)[i]);.      T
4da50 63 6c 5f 4c 69 73 74 4f 62 6a 41 70 70 65 6e 64  cl_ListObjAppend
4da60 45 6c 65 6d 65 6e 74 28 30 2c 20 70 53 74 61 63  Element(0, pStac
4da70 6b 2c 20 54 63 6c 5f 4e 65 77 53 74 72 69 6e 67  k, Tcl_NewString
4da80 4f 62 6a 28 7a 48 65 78 2c 20 2d 31 29 29 3b 0a  Obj(zHex, -1));.
4da90 20 20 20 20 7d 0a 0a 20 20 20 20 54 63 6c 5f 4c      }..    Tcl_L
4daa0 69 73 74 4f 62 6a 41 70 70 65 6e 64 45 6c 65 6d  istObjAppendElem
4dab0 65 6e 74 28 30 2c 20 70 45 6e 74 72 79 2c 20 70  ent(0, pEntry, p
4dac0 53 74 61 63 6b 29 3b 0a 20 20 20 20 54 63 6c 5f  Stack);.    Tcl_
4dad0 4c 69 73 74 4f 62 6a 41 70 70 65 6e 64 45 6c 65  ListObjAppendEle
4dae0 6d 65 6e 74 28 30 2c 20 70 52 65 73 2c 20 70 45  ment(0, pRes, pE
4daf0 6e 74 72 79 29 3b 0a 20 20 7d 0a 0a 20 20 54 63  ntry);.  }..  Tc
4db00 6c 5f 52 65 73 65 74 52 65 73 75 6c 74 28 69 6e  l_ResetResult(in
4db10 74 65 72 70 29 3b 0a 20 20 54 63 6c 5f 53 65 74  terp);.  Tcl_Set
4db20 4f 62 6a 52 65 73 75 6c 74 28 69 6e 74 65 72 70  ObjResult(interp
4db30 2c 20 70 52 65 73 29 3b 0a 20 20 54 63 6c 5f 44  , pRes);.  Tcl_D
4db40 65 63 72 52 65 66 43 6f 75 6e 74 28 70 52 65 73  ecrRefCount(pRes
4db50 29 3b 0a 20 20 72 65 74 75 72 6e 20 54 43 4c 5f  );.  return TCL_
4db60 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  OK;.}.#endif../*
4db70 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20  .** This is the 
4db80 74 65 73 74 20 6c 61 79 65 72 27 73 20 77 72 61  test layer's wra
4db90 70 70 65 72 20 61 72 6f 75 6e 64 20 73 71 6c 69  pper around sqli
4dba0 74 65 33 4f 73 4d 61 6c 6c 6f 63 28 29 2e 0a 2a  te3OsMalloc()..*
4dbb0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 20  /.static void * 
4dbc0 4f 53 4d 41 4c 4c 4f 43 28 69 6e 74 20 6e 29 7b  OSMALLOC(int n){
4dbd0 0a 20 20 73 71 6c 69 74 65 33 4f 73 45 6e 74 65  .  sqlite3OsEnte
4dbe0 72 4d 75 74 65 78 28 29 3b 0a 23 69 66 64 65 66  rMutex();.#ifdef
4dbf0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d   SQLITE_ENABLE_M
4dc00 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54  EMORY_MANAGEMENT
4dc10 0a 20 20 73 71 6c 69 74 65 33 5f 6e 4d 61 78 41  .  sqlite3_nMaxA
4dc20 6c 6c 6f 63 20 3d 20 0a 20 20 20 20 20 20 4d 41  lloc = .      MA
4dc30 58 28 73 71 6c 69 74 65 33 5f 6e 4d 61 78 41 6c  X(sqlite3_nMaxAl
4dc40 6c 6f 63 2c 20 73 71 6c 69 74 65 33 54 68 72 65  loc, sqlite3Thre
4dc50 61 64 44 61 74 61 52 65 61 64 4f 6e 6c 79 28 29  adDataReadOnly()
4dc60 2d 3e 6e 41 6c 6c 6f 63 29 3b 0a 23 65 6e 64 69  ->nAlloc);.#endi
4dc70 66 0a 20 20 61 73 73 65 72 74 28 20 21 73 71 6c  f.  assert( !sql
4dc80 69 74 65 33 5f 6d 61 6c 6c 6f 63 44 69 73 61 6c  ite3_mallocDisal
4dc90 6c 6f 77 65 64 20 29 3b 0a 20 20 69 66 28 20 21  lowed );.  if( !
4dca0 73 71 6c 69 74 65 33 54 65 73 74 4d 61 6c 6c 6f  sqlite3TestMallo
4dcb0 63 46 61 69 6c 28 29 20 29 7b 0a 20 20 20 20 75  cFail() ){.    u
4dcc0 33 32 20 2a 70 3b 0a 20 20 20 20 70 20 3d 20 28  32 *p;.    p = (
4dcd0 75 33 32 20 2a 29 73 71 6c 69 74 65 33 4f 73 4d  u32 *)sqlite3OsM
4dce0 61 6c 6c 6f 63 28 6e 20 2b 20 54 45 53 54 41 4c  alloc(n + TESTAL
4dcf0 4c 4f 43 5f 4f 56 45 52 48 45 41 44 29 3b 0a 20  LOC_OVERHEAD);. 
4dd00 20 20 20 61 73 73 65 72 74 28 70 29 3b 0a 20 20     assert(p);.  
4dd10 20 20 73 71 6c 69 74 65 33 5f 6e 4d 61 6c 6c 6f    sqlite3_nMallo
4dd20 63 2b 2b 3b 0a 20 20 20 20 61 70 70 6c 79 47 75  c++;.    applyGu
4dd30 61 72 64 73 28 70 29 3b 0a 20 20 20 20 6c 69 6e  ards(p);.    lin
4dd40 6b 41 6c 6c 6f 63 28 70 29 3b 0a 20 20 20 20 73  kAlloc(p);.    s
4dd50 71 6c 69 74 65 33 4f 73 4c 65 61 76 65 4d 75 74  qlite3OsLeaveMut
4dd60 65 78 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ex();.    return
4dd70 20 28 76 6f 69 64 20 2a 29 28 26 70 5b 54 45 53   (void *)(&p[TES
4dd80 54 41 4c 4c 4f 43 5f 4e 47 55 41 52 44 20 2b 20  TALLOC_NGUARD + 
4dd90 32 2a 73 69 7a 65 6f 66 28 76 6f 69 64 20 2a 29  2*sizeof(void *)
4dda0 2f 73 69 7a 65 6f 66 28 75 33 32 29 5d 29 3b 0a  /sizeof(u32)]);.
4ddb0 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 4f 73 4c    }.  sqlite3OsL
4ddc0 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 72  eaveMutex();.  r
4ddd0 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 73 74 61 74  eturn 0;.}..stat
4dde0 69 63 20 69 6e 74 20 4f 53 53 49 5a 45 4f 46 28  ic int OSSIZEOF(
4ddf0 76 6f 69 64 20 2a 70 29 7b 0a 20 20 69 66 28 20  void *p){.  if( 
4de00 70 20 29 7b 0a 20 20 20 20 75 33 32 20 2a 70 4f  p ){.    u32 *pO
4de10 73 20 3d 20 28 75 33 32 20 2a 29 67 65 74 4f 73  s = (u32 *)getOs
4de20 50 6f 69 6e 74 65 72 28 70 29 3b 0a 20 20 20 20  Pointer(p);.    
4de30 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4f 73  return sqlite3Os
4de40 41 6c 6c 6f 63 61 74 69 6f 6e 53 69 7a 65 28 70  AllocationSize(p
4de50 4f 73 29 20 2d 20 54 45 53 54 41 4c 4c 4f 43 5f  Os) - TESTALLOC_
4de60 4f 56 45 52 48 45 41 44 3b 0a 20 20 7d 0a 20 20  OVERHEAD;.  }.  
4de70 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
4de80 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 74  ** This is the t
4de90 65 73 74 20 6c 61 79 65 72 27 73 20 77 72 61 70  est layer's wrap
4dea0 70 65 72 20 61 72 6f 75 6e 64 20 73 71 6c 69 74  per around sqlit
4deb0 65 33 4f 73 46 72 65 65 28 29 2e 20 54 68 65 20  e3OsFree(). The 
4dec0 61 72 67 75 6d 65 6e 74 20 69 73 20 61 0a 2a 2a  argument is a.**
4ded0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
4dee0 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 20  space allocated 
4def0 66 6f 72 20 74 68 65 20 61 70 70 6c 69 63 61 74  for the applicat
4df00 69 6f 6e 20 74 6f 20 75 73 65 2e 0a 2a 2f 0a 73  ion to use..*/.s
4df10 74 61 74 69 63 20 76 6f 69 64 20 4f 53 46 52 45  tatic void OSFRE
4df20 45 28 76 6f 69 64 20 2a 70 46 72 65 65 29 7b 0a  E(void *pFree){.
4df30 20 20 75 33 32 20 2a 70 3b 20 20 20 20 20 20 20    u32 *p;       
4df40 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
4df50 74 68 65 20 4f 53 2d 6c 61 79 65 72 20 61 6c 6c  the OS-layer all
4df60 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c  ocation */.  sql
4df70 69 74 65 33 4f 73 45 6e 74 65 72 4d 75 74 65 78  ite3OsEnterMutex
4df80 28 29 3b 0a 20 20 70 20 3d 20 28 75 33 32 20 2a  ();.  p = (u32 *
4df90 29 67 65 74 4f 73 50 6f 69 6e 74 65 72 28 70 46  )getOsPointer(pF
4dfa0 72 65 65 29 3b 0a 20 20 63 68 65 63 6b 47 75 61  ree);.  checkGua
4dfb0 72 64 73 28 70 29 3b 0a 20 20 75 6e 6c 69 6e 6b  rds(p);.  unlink
4dfc0 41 6c 6c 6f 63 28 70 29 3b 0a 20 20 6d 65 6d 73  Alloc(p);.  mems
4dfd0 65 74 28 70 46 72 65 65 2c 20 30 78 35 35 2c 20  et(pFree, 0x55, 
4dfe0 4f 53 53 49 5a 45 4f 46 28 70 46 72 65 65 29 29  OSSIZEOF(pFree))
4dff0 3b 0a 20 20 73 71 6c 69 74 65 33 4f 73 46 72 65  ;.  sqlite3OsFre
4e000 65 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  e(p);.  sqlite3_
4e010 6e 46 72 65 65 2b 2b 3b 0a 20 20 73 71 6c 69 74  nFree++;.  sqlit
4e020 65 33 4f 73 4c 65 61 76 65 4d 75 74 65 78 28 29  e3OsLeaveMutex()
4e030 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
4e040 69 73 20 74 68 65 20 74 65 73 74 20 6c 61 79 65  is the test laye
4e050 72 27 73 20 77 72 61 70 70 65 72 20 61 72 6f 75  r's wrapper arou
4e060 6e 64 20 73 71 6c 69 74 65 33 4f 73 52 65 61 6c  nd sqlite3OsReal
4e070 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  loc()..*/.static
4e080 20 76 6f 69 64 20 2a 20 4f 53 52 45 41 4c 4c 4f   void * OSREALLO
4e090 43 28 76 6f 69 64 20 2a 70 52 65 61 6c 6c 6f 63  C(void *pRealloc
4e0a0 2c 20 69 6e 74 20 6e 29 7b 0a 23 69 66 64 65 66  , int n){.#ifdef
4e0b0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d   SQLITE_ENABLE_M
4e0c0 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54  EMORY_MANAGEMENT
4e0d0 0a 20 20 73 71 6c 69 74 65 33 5f 6e 4d 61 78 41  .  sqlite3_nMaxA
4e0e0 6c 6c 6f 63 20 3d 20 0a 20 20 20 20 20 20 4d 41  lloc = .      MA
4e0f0 58 28 73 71 6c 69 74 65 33 5f 6e 4d 61 78 41 6c  X(sqlite3_nMaxAl
4e100 6c 6f 63 2c 20 73 71 6c 69 74 65 33 54 68 72 65  loc, sqlite3Thre
4e110 61 64 44 61 74 61 52 65 61 64 4f 6e 6c 79 28 29  adDataReadOnly()
4e120 2d 3e 6e 41 6c 6c 6f 63 29 3b 0a 23 65 6e 64 69  ->nAlloc);.#endi
4e130 66 0a 20 20 61 73 73 65 72 74 28 20 21 73 71 6c  f.  assert( !sql
4e140 69 74 65 33 5f 6d 61 6c 6c 6f 63 44 69 73 61 6c  ite3_mallocDisal
4e150 6c 6f 77 65 64 20 29 3b 0a 20 20 69 66 28 20 21  lowed );.  if( !
4e160 73 71 6c 69 74 65 33 54 65 73 74 4d 61 6c 6c 6f  sqlite3TestMallo
4e170 63 46 61 69 6c 28 29 20 29 7b 0a 20 20 20 20 75  cFail() ){.    u
4e180 33 32 20 2a 70 20 3d 20 28 75 33 32 20 2a 29 67  32 *p = (u32 *)g
4e190 65 74 4f 73 50 6f 69 6e 74 65 72 28 70 52 65 61  etOsPointer(pRea
4e1a0 6c 6c 6f 63 29 3b 0a 20 20 20 20 63 68 65 63 6b  lloc);.    check
4e1b0 47 75 61 72 64 73 28 70 29 3b 0a 20 20 20 20 70  Guards(p);.    p
4e1c0 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 6c   = sqlite3OsReal
4e1d0 6c 6f 63 28 70 2c 20 6e 20 2b 20 54 45 53 54 41  loc(p, n + TESTA
4e1e0 4c 4c 4f 43 5f 4f 56 45 52 48 45 41 44 29 3b 0a  LLOC_OVERHEAD);.
4e1f0 20 20 20 20 61 70 70 6c 79 47 75 61 72 64 73 28      applyGuards(
4e200 70 29 3b 0a 20 20 20 20 72 65 6c 69 6e 6b 41 6c  p);.    relinkAl
4e210 6c 6f 63 28 70 29 3b 0a 20 20 20 20 72 65 74 75  loc(p);.    retu
4e220 72 6e 20 28 76 6f 69 64 20 2a 29 28 26 70 5b 54  rn (void *)(&p[T
4e230 45 53 54 41 4c 4c 4f 43 5f 4e 47 55 41 52 44 20  ESTALLOC_NGUARD 
4e240 2b 20 32 2a 73 69 7a 65 6f 66 28 76 6f 69 64 20  + 2*sizeof(void 
4e250 2a 29 2f 73 69 7a 65 6f 66 28 75 33 32 29 5d 29  *)/sizeof(u32)])
4e260 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  ;.  }.  return 0
4e270 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  ;.}..static void
4e280 20 4f 53 4d 41 4c 4c 4f 43 5f 46 41 49 4c 45 44   OSMALLOC_FAILED
4e290 28 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 73  (){.  sqlite3_is
4e2a0 46 61 69 6c 20 3d 20 30 3b 0a 7d 0a 0a 23 65 6c  Fail = 0;.}..#el
4e2b0 73 65 0a 2f 2a 20 44 65 66 69 6e 65 20 6d 61 63  se./* Define mac
4e2c0 72 6f 73 20 74 6f 20 63 61 6c 6c 20 74 68 65 20  ros to call the 
4e2d0 73 71 6c 69 74 65 33 4f 73 58 58 58 20 69 6e 74  sqlite3OsXXX int
4e2e0 65 72 66 61 63 65 20 64 69 72 65 63 74 6c 79 20  erface directly 
4e2f0 69 66 20 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54  if .** the SQLIT
4e300 45 5f 4d 45 4d 44 45 42 55 47 20 6d 61 63 72 6f  E_MEMDEBUG macro
4e310 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 2e   is not defined.
4e320 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 53 4d 41  .*/.#define OSMA
4e330 4c 4c 4f 43 28 78 29 20 20 20 20 20 20 20 20 73  LLOC(x)        s
4e340 71 6c 69 74 65 33 4f 73 4d 61 6c 6c 6f 63 28 78  qlite3OsMalloc(x
4e350 29 0a 23 64 65 66 69 6e 65 20 4f 53 52 45 41 4c  ).#define OSREAL
4e360 4c 4f 43 28 78 2c 79 29 20 20 20 20 20 73 71 6c  LOC(x,y)     sql
4e370 69 74 65 33 4f 73 52 65 61 6c 6c 6f 63 28 78 2c  ite3OsRealloc(x,
4e380 79 29 0a 23 64 65 66 69 6e 65 20 4f 53 46 52 45  y).#define OSFRE
4e390 45 28 78 29 20 20 20 20 20 20 20 20 20 20 73 71  E(x)          sq
4e3a0 6c 69 74 65 33 4f 73 46 72 65 65 28 78 29 0a 23  lite3OsFree(x).#
4e3b0 64 65 66 69 6e 65 20 4f 53 53 49 5a 45 4f 46 28  define OSSIZEOF(
4e3c0 78 29 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  x)        sqlite
4e3d0 33 4f 73 41 6c 6c 6f 63 61 74 69 6f 6e 53 69 7a  3OsAllocationSiz
4e3e0 65 28 78 29 0a 23 64 65 66 69 6e 65 20 4f 53 4d  e(x).#define OSM
4e3f0 41 4c 4c 4f 43 5f 46 41 49 4c 45 44 28 29 0a 0a  ALLOC_FAILED()..
4e400 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 54  #endif  /* SQLIT
4e410 45 5f 4d 45 4d 44 45 42 55 47 20 2a 2f 0a 2f 2a  E_MEMDEBUG */./*
4e420 0a 2a 2a 20 45 6e 64 20 63 6f 64 65 20 66 6f 72  .** End code for
4e430 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
4e440 6f 6e 20 73 79 73 74 65 6d 20 74 65 73 74 20 6c  on system test l
4e450 61 79 65 72 2e 0a 2a 2a 2d 2d 2d 2d 2d 2d 2d 2d  ayer..**--------
4e460 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4e470 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4e480 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4e490 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4e4a0 2d 2d 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  --*/../*.** This
4e4b0 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
4e4c0 65 64 20 77 68 65 6e 20 77 65 20 61 72 65 20 61  ed when we are a
4e4d0 62 6f 75 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65  bout to allocate
4e4e0 20 6e 20 61 64 64 69 74 69 6f 6e 61 6c 20 62 79   n additional by
4e4f0 74 65 73 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72 79  tes.** of memory
4e500 2e 20 20 49 66 20 74 68 65 20 6e 65 77 20 61 6c  .  If the new al
4e510 6c 6f 63 61 74 69 6f 6e 20 77 69 6c 6c 20 70 75  location will pu
4e520 74 20 69 73 20 6f 76 65 72 20 74 68 65 20 73 6f  t is over the so
4e530 66 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a  ft allocation.**
4e540 20 6c 69 6d 69 74 2c 20 74 68 65 6e 20 69 6e 76   limit, then inv
4e550 6f 6b 65 20 73 71 6c 69 74 65 33 5f 72 65 6c 65  oke sqlite3_rele
4e560 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 74 6f 20  ase_memory() to 
4e570 74 72 79 20 74 6f 20 72 65 6c 65 61 73 65 20 73  try to release s
4e580 6f 6d 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 62 65  ome.** memory be
4e590 66 6f 72 65 20 63 6f 6e 74 69 6e 75 69 6e 67 20  fore continuing 
4e5a0 77 69 74 68 20 74 68 65 20 61 6c 6c 6f 63 61 74  with the allocat
4e5b0 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ion..**.** This 
4e5c0 72 6f 75 74 69 6e 65 20 61 6c 73 6f 20 6d 61 6b  routine also mak
4e5d0 65 73 20 73 75 72 65 20 74 68 61 74 20 74 68 65  es sure that the
4e5e0 20 74 68 72 65 61 64 2d 73 70 65 63 69 66 69 63   thread-specific
4e5f0 2d 64 61 74 61 20 28 54 53 44 29 20 68 61 73 0a  -data (TSD) has.
4e600 2a 2a 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e  ** be allocated.
4e610 20 20 49 66 20 69 74 20 68 61 73 20 6e 6f 74 20    If it has not 
4e620 61 6e 64 20 63 61 6e 20 6e 6f 74 20 62 65 20 61  and can not be a
4e630 6c 6c 6f 63 61 74 65 64 2c 20 74 68 65 6e 20 72  llocated, then r
4e640 65 74 75 72 6e 0a 2a 2a 20 66 61 6c 73 65 2e 20  eturn.** false. 
4e650 20 54 68 65 20 75 70 64 61 74 65 4d 65 6d 6f 72   The updateMemor
4e660 79 55 73 65 64 43 6f 75 6e 74 28 29 20 72 6f 75  yUsedCount() rou
4e670 74 69 6e 65 20 62 65 6c 6f 77 20 77 69 6c 6c 20  tine below will 
4e680 64 65 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 74 68  deallocate.** th
4e690 65 20 54 53 44 20 69 66 20 69 74 20 6f 75 67 68  e TSD if it ough
4e6a0 74 20 74 6f 20 62 65 2e 0a 2a 2a 0a 2a 2a 20 49  t to be..**.** I
4e6b0 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
4e6c0 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e  MEMORY_MANAGEMEN
4e6d0 54 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64  T is not defined
4e6e0 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  , this routine i
4e6f0 73 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 0a 2a 2f 20  s.** a no-op.*/ 
4e700 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
4e710 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e  NABLE_MEMORY_MAN
4e720 41 47 45 4d 45 4e 54 0a 73 74 61 74 69 63 20 69  AGEMENT.static i
4e730 6e 74 20 65 6e 66 6f 72 63 65 53 6f 66 74 4c 69  nt enforceSoftLi
4e740 6d 69 74 28 69 6e 74 20 6e 29 7b 0a 20 20 54 68  mit(int n){.  Th
4e750 72 65 61 64 44 61 74 61 20 2a 70 54 73 64 20 3d  readData *pTsd =
4e760 20 73 71 6c 69 74 65 33 54 68 72 65 61 64 44 61   sqlite3ThreadDa
4e770 74 61 28 29 3b 0a 20 20 69 66 28 20 70 54 73 64  ta();.  if( pTsd
4e780 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
4e790 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  n 0;.  }.  asser
4e7a0 74 28 20 70 54 73 64 2d 3e 6e 41 6c 6c 6f 63 3e  t( pTsd->nAlloc>
4e7b0 3d 30 20 29 3b 0a 20 20 69 66 28 20 6e 3e 30 20  =0 );.  if( n>0 
4e7c0 26 26 20 70 54 73 64 2d 3e 6e 53 6f 66 74 48 65  && pTsd->nSoftHe
4e7d0 61 70 4c 69 6d 69 74 3e 30 20 29 7b 0a 20 20 20  apLimit>0 ){.   
4e7e0 20 77 68 69 6c 65 28 20 70 54 73 64 2d 3e 6e 41   while( pTsd->nA
4e7f0 6c 6c 6f 63 2b 6e 3e 70 54 73 64 2d 3e 6e 53 6f  lloc+n>pTsd->nSo
4e800 66 74 48 65 61 70 4c 69 6d 69 74 20 26 26 20 73  ftHeapLimit && s
4e810 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d  qlite3_release_m
4e820 65 6d 6f 72 79 28 6e 29 20 29 7b 7d 0a 20 20 7d  emory(n) ){}.  }
4e830 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23  .  return 1;.}.#
4e840 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 65 6e  else.# define en
4e850 66 6f 72 63 65 53 6f 66 74 4c 69 6d 69 74 28 58  forceSoftLimit(X
4e860 29 20 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  )  1.#endif../*.
4e870 2a 2a 20 55 70 64 61 74 65 20 74 68 65 20 63 6f  ** Update the co
4e880 75 6e 74 20 6f 66 20 74 6f 74 61 6c 20 6f 75 74  unt of total out
4e890 73 74 61 6e 64 69 6e 67 20 6d 65 6d 6f 72 79 20  standing memory 
4e8a0 74 68 61 74 20 69 73 20 68 65 6c 64 20 69 6e 0a  that is held in.
4e8b0 2a 2a 20 74 68 72 65 61 64 2d 73 70 65 63 69 66  ** thread-specif
4e8c0 69 63 2d 64 61 74 61 20 28 54 53 44 29 2e 20 20  ic-data (TSD).  
4e8d0 49 66 20 61 66 74 65 72 20 74 68 69 73 20 75 70  If after this up
4e8e0 64 61 74 65 20 74 68 65 20 54 53 44 20 69 73 0a  date the TSD is.
4e8f0 2a 2a 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65 69  ** no longer bei
4e900 6e 67 20 75 73 65 64 2c 20 74 68 65 6e 20 64 65  ng used, then de
4e910 61 6c 6c 6f 63 61 74 65 20 69 74 2e 0a 2a 2a 0a  allocate it..**.
4e920 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 45 4e 41  ** If SQLITE_ENA
4e930 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47  BLE_MEMORY_MANAG
4e940 45 4d 45 4e 54 20 69 73 20 6e 6f 74 20 64 65 66  EMENT is not def
4e950 69 6e 65 64 2c 20 74 68 69 73 20 72 6f 75 74 69  ined, this routi
4e960 6e 65 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f 70  ne is.** a no-op
4e970 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
4e980 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f  E_ENABLE_MEMORY_
4e990 4d 41 4e 41 47 45 4d 45 4e 54 0a 73 74 61 74 69  MANAGEMENT.stati
4e9a0 63 20 76 6f 69 64 20 75 70 64 61 74 65 4d 65 6d  c void updateMem
4e9b0 6f 72 79 55 73 65 64 43 6f 75 6e 74 28 69 6e 74  oryUsedCount(int
4e9c0 20 6e 29 7b 0a 20 20 54 68 72 65 61 64 44 61 74   n){.  ThreadDat
4e9d0 61 20 2a 70 54 73 64 20 3d 20 73 71 6c 69 74 65  a *pTsd = sqlite
4e9e0 33 54 68 72 65 61 64 44 61 74 61 28 29 3b 0a 20  3ThreadData();. 
4e9f0 20 69 66 28 20 70 54 73 64 20 29 7b 0a 20 20 20   if( pTsd ){.   
4ea00 20 70 54 73 64 2d 3e 6e 41 6c 6c 6f 63 20 2b 3d   pTsd->nAlloc +=
4ea10 20 6e 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   n;.    assert( 
4ea20 70 54 73 64 2d 3e 6e 41 6c 6c 6f 63 3e 3d 30 20  pTsd->nAlloc>=0 
4ea30 29 3b 0a 20 20 20 20 69 66 28 20 70 54 73 64 2d  );.    if( pTsd-
4ea40 3e 6e 41 6c 6c 6f 63 3d 3d 30 20 26 26 20 70 54  >nAlloc==0 && pT
4ea50 73 64 2d 3e 6e 53 6f 66 74 48 65 61 70 4c 69 6d  sd->nSoftHeapLim
4ea60 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73  it==0 ){.      s
4ea70 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 68 72  qlite3ReleaseThr
4ea80 65 61 64 44 61 74 61 28 29 3b 0a 20 20 20 20 7d  eadData();.    }
4ea90 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 23 64 65  .  }.}.#else.#de
4eaa0 66 69 6e 65 20 75 70 64 61 74 65 4d 65 6d 6f 72  fine updateMemor
4eab0 79 55 73 65 64 43 6f 75 6e 74 28 78 29 20 20 2f  yUsedCount(x)  /
4eac0 2a 20 6e 6f 2d 6f 70 20 2a 2f 0a 23 65 6e 64 69  * no-op */.#endi
4ead0 66 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74  f../*.** Allocat
4eae0 65 20 61 6e 64 20 72 65 74 75 72 6e 20 4e 20 62  e and return N b
4eaf0 79 74 65 73 20 6f 66 20 75 6e 69 6e 69 74 69 61  ytes of uninitia
4eb00 6c 69 73 65 64 20 6d 65 6d 6f 72 79 20 62 79 20  lised memory by 
4eb10 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74  calling.** sqlit
4eb20 65 33 4f 73 4d 61 6c 6c 6f 63 28 29 2e 20 49 66  e3OsMalloc(). If
4eb30 20 74 68 65 20 4d 61 6c 6c 6f 63 28 29 20 63 61   the Malloc() ca
4eb40 6c 6c 20 66 61 69 6c 73 2c 20 61 74 74 65 6d 70  ll fails, attemp
4eb50 74 20 74 6f 20 66 72 65 65 20 6d 65 6d 6f 72 79  t to free memory
4eb60 20 0a 2a 2a 20 62 79 20 63 61 6c 6c 69 6e 67 20   .** by calling 
4eb70 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f  sqlite3_release_
4eb80 6d 65 6d 6f 72 79 28 29 2e 0a 2a 2f 0a 53 51 4c  memory()..*/.SQL
4eb90 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
4eba0 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 52   *sqlite3MallocR
4ebb0 61 77 28 69 6e 74 20 6e 2c 20 69 6e 74 20 64 6f  aw(int n, int do
4ebc0 4d 65 6d 4d 61 6e 61 67 65 29 7b 0a 20 20 76 6f  MemManage){.  vo
4ebd0 69 64 20 2a 70 20 3d 20 30 3b 0a 20 20 69 66 28  id *p = 0;.  if(
4ebe0 20 6e 3e 30 20 26 26 20 21 73 71 6c 69 74 65 33   n>0 && !sqlite3
4ebf0 4d 61 6c 6c 6f 63 46 61 69 6c 65 64 28 29 20 26  MallocFailed() &
4ec00 26 20 28 21 64 6f 4d 65 6d 4d 61 6e 61 67 65 20  & (!doMemManage 
4ec10 7c 7c 20 65 6e 66 6f 72 63 65 53 6f 66 74 4c 69  || enforceSoftLi
4ec20 6d 69 74 28 6e 29 29 20 29 7b 0a 20 20 20 20 77  mit(n)) ){.    w
4ec30 68 69 6c 65 28 20 28 70 20 3d 20 4f 53 4d 41 4c  hile( (p = OSMAL
4ec40 4c 4f 43 28 6e 29 29 3d 3d 30 20 26 26 20 73 71  LOC(n))==0 && sq
4ec50 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65  lite3_release_me
4ec60 6d 6f 72 79 28 6e 29 20 29 7b 7d 0a 20 20 20 20  mory(n) ){}.    
4ec70 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 20 20  if( !p ){.      
4ec80 73 71 6c 69 74 65 33 46 61 69 6c 65 64 4d 61 6c  sqlite3FailedMal
4ec90 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 4f 53 4d  loc();.      OSM
4eca0 41 4c 4c 4f 43 5f 46 41 49 4c 45 44 28 29 3b 0a  ALLOC_FAILED();.
4ecb0 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 64 6f      }else if( do
4ecc0 4d 65 6d 4d 61 6e 61 67 65 20 29 7b 0a 20 20 20  MemManage ){.   
4ecd0 20 20 20 75 70 64 61 74 65 4d 65 6d 6f 72 79 55     updateMemoryU
4ece0 73 65 64 43 6f 75 6e 74 28 4f 53 53 49 5a 45 4f  sedCount(OSSIZEO
4ecf0 46 28 70 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  F(p));.    }.  }
4ed00 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
4ed10 2f 2a 0a 2a 2a 20 52 65 73 69 7a 65 20 74 68 65  /*.** Resize the
4ed20 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 20 70   allocation at p
4ed30 20 74 6f 20 6e 20 62 79 74 65 73 20 62 79 20 63   to n bytes by c
4ed40 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 4f 73  alling sqlite3Os
4ed50 52 65 61 6c 6c 6f 63 28 29 2e 20 54 68 65 0a 2a  Realloc(). The.*
4ed60 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  * pointer to the
4ed70 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 20   new allocation 
4ed80 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66  is returned.  If
4ed90 20 74 68 65 20 52 65 61 6c 6c 6f 63 28 29 20 63   the Realloc() c
4eda0 61 6c 6c 20 66 61 69 6c 73 2c 0a 2a 2a 20 61 74  all fails,.** at
4edb0 74 65 6d 70 74 20 74 6f 20 66 72 65 65 20 6d 65  tempt to free me
4edc0 6d 6f 72 79 20 62 79 20 63 61 6c 6c 69 6e 67 20  mory by calling 
4edd0 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f  sqlite3_release_
4ede0 6d 65 6d 6f 72 79 28 29 2e 0a 2a 2f 0a 53 51 4c  memory()..*/.SQL
4edf0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
4ee00 20 2a 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63   *sqlite3Realloc
4ee10 28 76 6f 69 64 20 2a 70 2c 20 69 6e 74 20 6e 29  (void *p, int n)
4ee20 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 4d  {.  if( sqlite3M
4ee30 61 6c 6c 6f 63 46 61 69 6c 65 64 28 29 20 29 7b  allocFailed() ){
4ee40 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
4ee50 20 7d 0a 0a 20 20 69 66 28 20 21 70 20 29 7b 0a   }..  if( !p ){.
4ee60 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74      return sqlit
4ee70 65 33 4d 61 6c 6c 6f 63 28 6e 2c 20 31 29 3b 0a  e3Malloc(n, 1);.
4ee80 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 76 6f 69    }else{.    voi
4ee90 64 20 2a 6e 70 20 3d 20 30 3b 0a 23 69 66 64 65  d *np = 0;.#ifde
4eea0 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
4eeb0 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e  MEMORY_MANAGEMEN
4eec0 54 0a 20 20 20 20 69 6e 74 20 6f 72 69 67 53 69  T.    int origSi
4eed0 7a 65 20 3d 20 4f 53 53 49 5a 45 4f 46 28 70 29  ze = OSSIZEOF(p)
4eee0 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28  ;.#endif.    if(
4eef0 20 65 6e 66 6f 72 63 65 53 6f 66 74 4c 69 6d 69   enforceSoftLimi
4ef00 74 28 6e 20 2d 20 6f 72 69 67 53 69 7a 65 29 20  t(n - origSize) 
4ef10 29 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  ){.      while( 
4ef20 28 6e 70 20 3d 20 4f 53 52 45 41 4c 4c 4f 43 28  (np = OSREALLOC(
4ef30 70 2c 20 6e 29 29 3d 3d 30 20 26 26 20 73 71 6c  p, n))==0 && sql
4ef40 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d  ite3_release_mem
4ef50 6f 72 79 28 6e 29 20 29 7b 7d 0a 20 20 20 20 20  ory(n) ){}.     
4ef60 20 69 66 28 20 21 6e 70 20 29 7b 0a 20 20 20 20   if( !np ){.    
4ef70 20 20 20 20 73 71 6c 69 74 65 33 46 61 69 6c 65      sqlite3Faile
4ef80 64 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20  dMalloc();.     
4ef90 20 20 20 4f 53 4d 41 4c 4c 4f 43 5f 46 41 49 4c     OSMALLOC_FAIL
4efa0 45 44 28 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  ED();.      }els
4efb0 65 7b 0a 20 20 20 20 20 20 20 20 75 70 64 61 74  e{.        updat
4efc0 65 4d 65 6d 6f 72 79 55 73 65 64 43 6f 75 6e 74  eMemoryUsedCount
4efd0 28 4f 53 53 49 5a 45 4f 46 28 6e 70 29 20 2d 20  (OSSIZEOF(np) - 
4efe0 6f 72 69 67 53 69 7a 65 29 3b 0a 20 20 20 20 20  origSize);.     
4eff0 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74   }.    }.    ret
4f000 75 72 6e 20 6e 70 3b 0a 20 20 7d 0a 7d 0a 0a 2f  urn np;.  }.}../
4f010 2a 0a 2a 2a 20 46 72 65 65 20 74 68 65 20 6d 65  *.** Free the me
4f020 6d 6f 72 79 20 70 6f 69 6e 74 65 64 20 74 6f 20  mory pointed to 
4f030 62 79 20 70 2e 20 70 20 6d 75 73 74 20 62 65 20  by p. p must be 
4f040 65 69 74 68 65 72 20 61 20 4e 55 4c 4c 20 70 6f  either a NULL po
4f050 69 6e 74 65 72 20 6f 72 20 61 20 0a 2a 2a 20 76  inter or a .** v
4f060 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79  alue returned by
4f070 20 61 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c   a previous call
4f080 20 74 6f 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f   to sqlite3Mallo
4f090 63 28 29 20 6f 72 20 73 71 6c 69 74 65 33 52 65  c() or sqlite3Re
4f0a0 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49  alloc()..*/.SQLI
4f0b0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
4f0c0 73 71 6c 69 74 65 33 46 72 65 65 58 28 76 6f 69  sqlite3FreeX(voi
4f0d0 64 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29  d *p){.  if( p )
4f0e0 7b 0a 20 20 20 20 75 70 64 61 74 65 4d 65 6d 6f  {.    updateMemo
4f0f0 72 79 55 73 65 64 43 6f 75 6e 74 28 30 20 2d 20  ryUsedCount(0 - 
4f100 4f 53 53 49 5a 45 4f 46 28 70 29 29 3b 0a 20 20  OSSIZEOF(p));.  
4f110 20 20 4f 53 46 52 45 45 28 70 29 3b 0a 20 20 7d    OSFREE(p);.  }
4f120 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 76 65 72 73  .}../*.** A vers
4f130 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 4d 61 6c  ion of sqliteMal
4f140 6c 6f 63 28 29 20 74 68 61 74 20 69 73 20 61 6c  loc() that is al
4f150 77 61 79 73 20 61 20 66 75 6e 63 74 69 6f 6e 2c  ways a function,
4f160 20 6e 6f 74 20 61 20 6d 61 63 72 6f 2e 0a 2a 2a   not a macro..**
4f170 20 43 75 72 72 65 6e 74 6c 79 2c 20 74 68 69 73   Currently, this
4f180 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 74 6f   is used only to
4f190 20 61 6c 6c 6f 63 20 74 6f 20 61 6c 6c 6f 63 61   alloc to alloca
4f1a0 74 65 20 74 68 65 20 70 61 72 73 65 72 20 65 6e  te the parser en
4f1b0 67 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  gine..*/.SQLITE_
4f1c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
4f1d0 6c 69 74 65 33 4d 61 6c 6c 6f 63 58 28 69 6e 74  lite3MallocX(int
4f1e0 20 6e 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71   n){.  return sq
4f1f0 6c 69 74 65 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 7d  liteMalloc(n);.}
4f200 0a 0a 2f 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 4d  ../*.** sqlite3M
4f210 61 6c 6c 6f 63 0a 2a 2a 20 73 71 6c 69 74 65 33  alloc.** sqlite3
4f220 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 0a 2a 2a  ReallocOrFree.**
4f230 0a 2a 2a 20 54 68 65 73 65 20 74 77 6f 20 61 72  .** These two ar
4f240 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73  e implemented as
4f250 20 77 72 61 70 70 65 72 73 20 61 72 6f 75 6e 64   wrappers around
4f260 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 52 61   sqlite3MallocRa
4f270 77 28 29 2c 20 0a 2a 2a 20 73 71 6c 69 74 65 33  w(), .** sqlite3
4f280 52 65 61 6c 6c 6f 63 28 29 20 61 6e 64 20 73 71  Realloc() and sq
4f290 6c 69 74 65 33 46 72 65 65 28 29 2e 0a 2a 2f 20  lite3Free()..*/ 
4f2a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4f2b0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c  void *sqlite3Mal
4f2c0 6c 6f 63 28 69 6e 74 20 6e 2c 20 69 6e 74 20 64  loc(int n, int d
4f2d0 6f 4d 65 6d 4d 61 6e 61 67 65 29 7b 0a 20 20 76  oMemManage){.  v
4f2e0 6f 69 64 20 2a 70 20 3d 20 73 71 6c 69 74 65 33  oid *p = sqlite3
4f2f0 4d 61 6c 6c 6f 63 52 61 77 28 6e 2c 20 64 6f 4d  MallocRaw(n, doM
4f300 65 6d 4d 61 6e 61 67 65 29 3b 0a 20 20 69 66 28  emManage);.  if(
4f310 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74   p ){.    memset
4f320 28 70 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a 20  (p, 0, n);.  }. 
4f330 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 53 51 4c   return p;.}.SQL
4f340 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
4f350 20 2a 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63   *sqlite3Realloc
4f360 4f 72 46 72 65 65 28 76 6f 69 64 20 2a 70 2c 20  OrFree(void *p, 
4f370 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a  int n){.  void *
4f380 70 4e 65 77 3b 0a 20 20 70 4e 65 77 20 3d 20 73  pNew;.  pNew = s
4f390 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 70 2c  qlite3Realloc(p,
4f3a0 20 6e 29 3b 0a 20 20 69 66 28 20 21 70 4e 65 77   n);.  if( !pNew
4f3b0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46   ){.    sqlite3F
4f3c0 72 65 65 58 28 70 29 3b 0a 20 20 7d 0a 20 20 72  reeX(p);.  }.  r
4f3d0 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f  eturn pNew;.}../
4f3e0 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 54 68 72 65  *.** sqlite3Thre
4f3f0 61 64 53 61 66 65 4d 61 6c 6c 6f 63 28 29 20 61  adSafeMalloc() a
4f400 6e 64 20 73 71 6c 69 74 65 33 54 68 72 65 61 64  nd sqlite3Thread
4f410 53 61 66 65 46 72 65 65 28 29 20 61 72 65 20 75  SafeFree() are u
4f420 73 65 64 20 69 6e 20 74 68 6f 73 65 0a 2a 2a 20  sed in those.** 
4f430 72 61 72 65 20 73 63 65 6e 61 72 69 6f 73 20 77  rare scenarios w
4f440 68 65 72 65 20 73 71 6c 69 74 65 20 6d 61 79 20  here sqlite may 
4f450 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20  allocate memory 
4f460 69 6e 20 6f 6e 65 20 74 68 72 65 61 64 20 61 6e  in one thread an
4f470 64 20 66 72 65 65 0a 2a 2a 20 69 74 20 69 6e 20  d free.** it in 
4f480 61 6e 6f 74 68 65 72 2e 20 54 68 65 79 20 61 72  another. They ar
4f490 65 20 65 78 61 63 74 6c 79 20 74 68 65 20 73 61  e exactly the sa
4f4a0 6d 65 20 61 73 20 73 71 6c 69 74 65 33 4d 61 6c  me as sqlite3Mal
4f4b0 6c 6f 63 28 29 20 61 6e 64 20 0a 2a 2a 20 73 71  loc() and .** sq
4f4c0 6c 69 74 65 33 46 72 65 65 28 29 20 65 78 63 65  lite3Free() exce
4f4d0 70 74 20 74 68 61 74 3a 0a 2a 2a 0a 2a 2a 20 20  pt that:.**.**  
4f4e0 20 2a 20 54 68 65 20 61 6c 6c 6f 63 61 74 65 64   * The allocated
4f4f0 20 6d 65 6d 6f 72 79 20 69 73 20 6e 6f 74 20 69   memory is not i
4f500 6e 63 6c 75 64 65 64 20 69 6e 20 61 6e 79 20 63  ncluded in any c
4f510 61 6c 63 75 6c 61 74 69 6f 6e 73 20 77 69 74 68  alculations with
4f520 20 0a 2a 2a 20 20 20 20 20 72 65 73 70 65 63 74   .**     respect
4f530 20 74 6f 20 74 68 65 20 73 6f 66 74 2d 68 65 61   to the soft-hea
4f540 70 2d 6c 69 6d 69 74 2c 20 61 6e 64 0a 2a 2a 0a  p-limit, and.**.
4f550 2a 2a 20 20 20 2a 20 73 71 6c 69 74 65 33 54 68  **   * sqlite3Th
4f560 72 65 61 64 53 61 66 65 4d 61 6c 6c 6f 63 28 29  readSafeMalloc()
4f570 20 6d 75 73 74 20 62 65 20 6d 61 74 63 68 65 64   must be matched
4f580 20 77 69 74 68 20 54 68 72 65 61 64 53 61 66 65   with ThreadSafe
4f590 46 72 65 65 28 29 2c 0a 2a 2a 20 20 20 20 20 6e  Free(),.**     n
4f5a0 6f 74 20 73 71 6c 69 74 65 33 46 72 65 65 28 29  ot sqlite3Free()
4f5b0 2e 20 43 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65  . Calling sqlite
4f5c0 33 46 72 65 65 28 29 20 6f 6e 20 6d 65 6d 6f 72  3Free() on memor
4f5d0 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a  y obtained from.
4f5e0 2a 2a 20 20 20 20 20 54 68 72 65 61 64 53 61 66  **     ThreadSaf
4f5f0 65 4d 61 6c 6c 6f 63 28 29 20 77 69 6c 6c 20 63  eMalloc() will c
4f600 61 75 73 65 20 61 6e 20 65 72 72 6f 72 20 73 6f  ause an error so
4f610 6d 65 77 68 65 72 65 20 64 6f 77 6e 20 74 68 65  mewhere down the
4f620 20 6c 69 6e 65 2e 0a 2a 2f 0a 23 69 66 64 65 66   line..*/.#ifdef
4f630 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d   SQLITE_ENABLE_M
4f640 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54  EMORY_MANAGEMENT
4f650 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4f660 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 54 68 72  void *sqlite3Thr
4f670 65 61 64 53 61 66 65 4d 61 6c 6c 6f 63 28 69 6e  eadSafeMalloc(in
4f680 74 20 6e 29 7b 0a 20 20 28 76 6f 69 64 29 45 4e  t n){.  (void)EN
4f690 54 45 52 5f 4d 41 4c 4c 4f 43 3b 0a 20 20 72 65  TER_MALLOC;.  re
4f6a0 74 75 72 6e 20 73 71 6c 69 74 65 33 4d 61 6c 6c  turn sqlite3Mall
4f6b0 6f 63 28 6e 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49  oc(n, 0);.}.SQLI
4f6c0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
4f6d0 73 71 6c 69 74 65 33 54 68 72 65 61 64 53 61 66  sqlite3ThreadSaf
4f6e0 65 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a  eFree(void *p){.
4f6f0 20 20 28 76 6f 69 64 29 45 4e 54 45 52 5f 4d 41    (void)ENTER_MA
4f700 4c 4c 4f 43 3b 0a 20 20 69 66 28 20 70 20 29 7b  LLOC;.  if( p ){
4f710 0a 20 20 20 20 4f 53 46 52 45 45 28 70 29 3b 0a  .    OSFREE(p);.
4f720 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f    }.}.#endif.../
4f730 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
4f740 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
4f750 61 6c 6c 6f 63 61 74 65 64 20 61 74 20 6c 6f 63  allocated at loc
4f760 61 74 69 6f 6e 20 70 2e 20 70 20 6d 75 73 74 20  ation p. p must 
4f770 62 65 20 65 69 74 68 65 72 20 0a 2a 2a 20 61 20  be either .** a 
4f780 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 28 69 6e  NULL pointer (in
4f790 20 77 68 69 63 68 20 63 61 73 65 20 30 20 69 73   which case 0 is
4f7a0 20 72 65 74 75 72 6e 65 64 29 20 6f 72 20 61 20   returned) or a 
4f7b0 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e 65 64  pointer returned
4f7c0 20 62 79 20 0a 2a 2a 20 73 71 6c 69 74 65 33 4d   by .** sqlite3M
4f7d0 61 6c 6c 6f 63 28 29 2c 20 73 71 6c 69 74 65 33  alloc(), sqlite3
4f7e0 52 65 61 6c 6c 6f 63 28 29 20 6f 72 20 73 71 6c  Realloc() or sql
4f7f0 69 74 65 33 52 65 61 6c 6c 6f 63 4f 72 46 72 65  ite3ReallocOrFre
4f800 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e  e()..**.** The n
4f810 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 61  umber of bytes a
4f820 6c 6c 6f 63 61 74 65 64 20 64 6f 65 73 20 6e 6f  llocated does no
4f830 74 20 69 6e 63 6c 75 64 65 20 61 6e 79 20 6f 76  t include any ov
4f840 65 72 68 65 61 64 20 69 6e 73 65 72 74 65 64 20  erhead inserted 
4f850 62 79 20 0a 2a 2a 20 61 6e 79 20 6d 61 6c 6c 6f  by .** any mallo
4f860 63 28 29 20 77 72 61 70 70 65 72 20 66 75 6e 63  c() wrapper func
4f870 74 69 6f 6e 73 20 74 68 61 74 20 6d 61 79 20 62  tions that may b
4f880 65 20 63 61 6c 6c 65 64 2e 20 53 6f 20 74 68 65  e called. So the
4f890 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 0a   value returned.
4f8a0 2a 2a 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  ** is the number
4f8b0 20 6f 66 20 62 79 74 65 73 20 74 68 61 74 20 77   of bytes that w
4f8c0 65 72 65 20 61 76 61 69 6c 61 62 6c 65 20 74 6f  ere available to
4f8d0 20 53 51 4c 69 74 65 20 75 73 69 6e 67 20 70 6f   SQLite using po
4f8e0 69 6e 74 65 72 20 70 2c 20 0a 2a 2a 20 72 65 67  inter p, .** reg
4f8f0 61 72 64 6c 65 73 73 20 6f 66 20 68 6f 77 20 6d  ardless of how m
4f900 75 63 68 20 6d 65 6d 6f 72 79 20 77 61 73 20 61  uch memory was a
4f910 63 74 75 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65  ctually allocate
4f920 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  d..*/.#ifdef SQL
4f930 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52  ITE_ENABLE_MEMOR
4f940 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 53 51 4c  Y_MANAGEMENT.SQL
4f950 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4f960 73 71 6c 69 74 65 33 41 6c 6c 6f 63 53 69 7a 65  sqlite3AllocSize
4f970 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 72 65 74  (void *p){.  ret
4f980 75 72 6e 20 4f 53 53 49 5a 45 4f 46 28 70 29 3b  urn OSSIZEOF(p);
4f990 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
4f9a0 20 4d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20   Make a copy of 
4f9b0 61 20 73 74 72 69 6e 67 20 69 6e 20 6d 65 6d 6f  a string in memo
4f9c0 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ry obtained from
4f9d0 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e   sqliteMalloc().
4f9e0 20 54 68 65 73 65 20 0a 2a 2a 20 66 75 6e 63 74   These .** funct
4f9f0 69 6f 6e 73 20 63 61 6c 6c 20 73 71 6c 69 74 65  ions call sqlite
4fa00 33 4d 61 6c 6c 6f 63 52 61 77 28 29 20 64 69 72  3MallocRaw() dir
4fa10 65 63 74 6c 79 20 69 6e 73 74 65 61 64 20 6f 66  ectly instead of
4fa20 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e   sqliteMalloc().
4fa30 20 54 68 69 73 0a 2a 2a 20 69 73 20 62 65 63 61   This.** is beca
4fa40 75 73 65 20 77 68 65 6e 20 6d 65 6d 6f 72 79 20  use when memory 
4fa50 64 65 62 75 67 67 69 6e 67 20 69 73 20 74 75 72  debugging is tur
4fa60 6e 65 64 20 6f 6e 2c 20 74 68 65 73 65 20 74 77  ned on, these tw
4fa70 6f 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20  o functions are 
4fa80 0a 2a 2a 20 63 61 6c 6c 65 64 20 76 69 61 20 6d  .** called via m
4fa90 61 63 72 6f 73 20 74 68 61 74 20 72 65 63 6f 72  acros that recor
4faa0 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 66 69  d the current fi
4fab0 6c 65 20 61 6e 64 20 6c 69 6e 65 20 6e 75 6d 62  le and line numb
4fac0 65 72 20 69 6e 20 74 68 65 0a 2a 2a 20 54 68 72  er in the.** Thr
4fad0 65 61 64 44 61 74 61 20 73 74 72 75 63 74 75 72  eadData structur
4fae0 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
4faf0 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74  VATE char *sqlit
4fb00 65 33 53 74 72 44 75 70 28 63 6f 6e 73 74 20 63  e3StrDup(const c
4fb10 68 61 72 20 2a 7a 29 7b 0a 20 20 63 68 61 72 20  har *z){.  char 
4fb20 2a 7a 4e 65 77 3b 0a 20 20 69 6e 74 20 6e 3b 0a  *zNew;.  int n;.
4fb30 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74    if( z==0 ) ret
4fb40 75 72 6e 20 30 3b 0a 20 20 6e 20 3d 20 73 74 72  urn 0;.  n = str
4fb50 6c 65 6e 28 7a 29 2b 31 3b 0a 20 20 7a 4e 65 77  len(z)+1;.  zNew
4fb60 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
4fb70 52 61 77 28 6e 2c 20 31 29 3b 0a 20 20 69 66 28  Raw(n, 1);.  if(
4fb80 20 7a 4e 65 77 20 29 20 6d 65 6d 63 70 79 28 7a   zNew ) memcpy(z
4fb90 4e 65 77 2c 20 7a 2c 20 6e 29 3b 0a 20 20 72 65  New, z, n);.  re
4fba0 74 75 72 6e 20 7a 4e 65 77 3b 0a 7d 0a 53 51 4c  turn zNew;.}.SQL
4fbb0 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
4fbc0 20 2a 73 71 6c 69 74 65 33 53 74 72 4e 44 75 70   *sqlite3StrNDup
4fbd0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20  (const char *z, 
4fbe0 69 6e 74 20 6e 29 7b 0a 20 20 63 68 61 72 20 2a  int n){.  char *
4fbf0 7a 4e 65 77 3b 0a 20 20 69 66 28 20 7a 3d 3d 30  zNew;.  if( z==0
4fc00 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7a   ) return 0;.  z
4fc10 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  New = sqlite3Mal
4fc20 6c 6f 63 52 61 77 28 6e 2b 31 2c 20 31 29 3b 0a  locRaw(n+1, 1);.
4fc30 20 20 69 66 28 20 7a 4e 65 77 20 29 7b 0a 20 20    if( zNew ){.  
4fc40 20 20 6d 65 6d 63 70 79 28 7a 4e 65 77 2c 20 7a    memcpy(zNew, z
4fc50 2c 20 6e 29 3b 0a 20 20 20 20 7a 4e 65 77 5b 6e  , n);.    zNew[n
4fc60 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74  ] = 0;.  }.  ret
4fc70 75 72 6e 20 7a 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a  urn zNew;.}../*.
4fc80 2a 2a 20 43 72 65 61 74 65 20 61 20 73 74 72 69  ** Create a stri
4fc90 6e 67 20 66 72 6f 6d 20 74 68 65 20 32 6e 64 20  ng from the 2nd 
4fca0 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74 20 61  and subsequent a
4fcb0 72 67 75 6d 65 6e 74 73 20 28 75 70 20 74 6f 20  rguments (up to 
4fcc0 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 4e 55 4c  the.** first NUL
4fcd0 4c 20 61 72 67 75 6d 65 6e 74 29 2c 20 73 74 6f  L argument), sto
4fce0 72 65 20 74 68 65 20 73 74 72 69 6e 67 20 69 6e  re the string in
4fcf0 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64   memory obtained
4fd00 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 4d   from.** sqliteM
4fd10 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 61 6b 65  alloc() and make
4fd20 20 74 68 65 20 70 6f 69 6e 74 65 72 20 69 6e 64   the pointer ind
4fd30 69 63 61 74 65 64 20 62 79 20 74 68 65 20 31 73  icated by the 1s
4fd40 74 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 70 6f  t argument.** po
4fd50 69 6e 74 20 74 6f 20 74 68 61 74 20 73 74 72 69  int to that stri
4fd60 6e 67 2e 20 20 54 68 65 20 31 73 74 20 61 72 67  ng.  The 1st arg
4fd70 75 6d 65 6e 74 20 6d 75 73 74 20 65 69 74 68 65  ument must eithe
4fd80 72 20 62 65 20 4e 55 4c 4c 20 6f 72 20 0a 2a 2a  r be NULL or .**
4fd90 20 70 6f 69 6e 74 20 74 6f 20 6d 65 6d 6f 72 79   point to memory
4fda0 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
4fdb0 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 0a 2a  qliteMalloc()..*
4fdc0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
4fdd0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 74   void sqlite3Set
4fde0 53 74 72 69 6e 67 28 63 68 61 72 20 2a 2a 70 7a  String(char **pz
4fdf0 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73  , ...){.  va_lis
4fe00 74 20 61 70 3b 0a 20 20 69 6e 74 20 6e 42 79 74  t ap;.  int nByt
4fe10 65 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  e;.  const char 
4fe20 2a 7a 3b 0a 20 20 63 68 61 72 20 2a 7a 52 65 73  *z;.  char *zRes
4fe30 75 6c 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  ult;..  assert( 
4fe40 70 7a 21 3d 30 20 29 3b 0a 20 20 6e 42 79 74 65  pz!=0 );.  nByte
4fe50 20 3d 20 31 3b 0a 20 20 76 61 5f 73 74 61 72 74   = 1;.  va_start
4fe60 28 61 70 2c 20 70 7a 29 3b 0a 20 20 77 68 69 6c  (ap, pz);.  whil
4fe70 65 28 20 28 7a 20 3d 20 76 61 5f 61 72 67 28 61  e( (z = va_arg(a
4fe80 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 29  p, const char*))
4fe90 21 3d 30 20 29 7b 0a 20 20 20 20 6e 42 79 74 65  !=0 ){.    nByte
4fea0 20 2b 3d 20 73 74 72 6c 65 6e 28 7a 29 3b 0a 20   += strlen(z);. 
4feb0 20 7d 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b   }.  va_end(ap);
4fec0 0a 20 20 73 71 6c 69 74 65 46 72 65 65 28 2a 70  .  sqliteFree(*p
4fed0 7a 29 3b 0a 20 20 2a 70 7a 20 3d 20 7a 52 65 73  z);.  *pz = zRes
4fee0 75 6c 74 20 3d 20 73 71 6c 69 74 65 4d 61 6c 6c  ult = sqliteMall
4fef0 6f 63 52 61 77 28 20 6e 42 79 74 65 20 29 3b 0a  ocRaw( nByte );.
4ff00 20 20 69 66 28 20 7a 52 65 73 75 6c 74 3d 3d 30    if( zResult==0
4ff10 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
4ff20 20 20 7d 0a 20 20 2a 7a 52 65 73 75 6c 74 20 3d    }.  *zResult =
4ff30 20 30 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61   0;.  va_start(a
4ff40 70 2c 20 70 7a 29 3b 0a 20 20 77 68 69 6c 65 28  p, pz);.  while(
4ff50 20 28 7a 20 3d 20 76 61 5f 61 72 67 28 61 70 2c   (z = va_arg(ap,
4ff60 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 29 21 3d   const char*))!=
4ff70 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 20 3d  0 ){.    int n =
4ff80 20 73 74 72 6c 65 6e 28 7a 29 3b 0a 20 20 20 20   strlen(z);.    
4ff90 6d 65 6d 63 70 79 28 7a 52 65 73 75 6c 74 2c 20  memcpy(zResult, 
4ffa0 7a 2c 20 6e 29 3b 0a 20 20 20 20 7a 52 65 73 75  z, n);.    zResu
4ffb0 6c 74 20 2b 3d 20 6e 3b 0a 20 20 7d 0a 20 20 7a  lt += n;.  }.  z
4ffc0 52 65 73 75 6c 74 5b 30 5d 20 3d 20 30 3b 0a 20  Result[0] = 0;. 
4ffd0 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 7d 0a 0a   va_end(ap);.}..
4ffe0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
4fff0 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 63 61 6c  tion must be cal
50000 6c 65 64 20 62 65 66 6f 72 65 20 65 78 69 74 69  led before exiti
50010 6e 67 20 61 6e 79 20 41 50 49 20 66 75 6e 63 74  ng any API funct
50020 69 6f 6e 20 28 69 2e 65 2e 20 0a 2a 2a 20 72 65  ion (i.e. .** re
50030 74 75 72 6e 69 6e 67 20 63 6f 6e 74 72 6f 6c 20  turning control 
50040 74 6f 20 74 68 65 20 75 73 65 72 29 20 74 68 61  to the user) tha
50050 74 20 68 61 73 20 63 61 6c 6c 65 64 20 73 71 6c  t has called sql
50060 69 74 65 33 4d 61 6c 6c 6f 63 20 6f 72 0a 2a 2a  ite3Malloc or.**
50070 20 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 2e   sqlite3Realloc.
50080 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72  .**.** The retur
50090 6e 65 64 20 76 61 6c 75 65 20 69 73 20 6e 6f 72  ned value is nor
500a0 6d 61 6c 6c 79 20 61 20 63 6f 70 79 20 6f 66 20  mally a copy of 
500b0 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
500c0 65 6e 74 20 74 6f 20 74 68 69 73 0a 2a 2a 20 66  ent to this.** f
500d0 75 6e 63 74 69 6f 6e 2e 20 48 6f 77 65 76 65 72  unction. However
500e0 2c 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20  , if a malloc() 
500f0 66 61 69 6c 75 72 65 20 68 61 73 20 6f 63 63 75  failure has occu
50100 72 65 64 20 73 69 6e 63 65 20 74 68 65 20 70 72  red since the pr
50110 65 76 69 6f 75 73 0a 2a 2a 20 69 6e 76 6f 63 61  evious.** invoca
50120 74 69 6f 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  tion SQLITE_NOME
50130 4d 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e  M is returned in
50140 73 74 65 61 64 2e 20 0a 2a 2a 0a 2a 2a 20 49 66  stead. .**.** If
50150 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
50160 65 6e 74 2c 20 64 62 2c 20 69 73 20 6e 6f 74 20  ent, db, is not 
50170 4e 55 4c 4c 20 61 6e 64 20 61 20 6d 61 6c 6c 6f  NULL and a mallo
50180 63 28 29 20 65 72 72 6f 72 20 68 61 73 20 6f 63  c() error has oc
50190 63 75 72 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 74  cured,.** then t
501a0 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 65 72  he connection er
501b0 72 6f 72 2d 63 6f 64 65 20 28 74 68 65 20 76 61  ror-code (the va
501c0 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20  lue returned by 
501d0 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28  sqlite3_errcode(
501e0 29 29 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20  )).** is set to 
501f0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2f  SQLITE_NOMEM..*/
50200 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 61 6c  .int sqlite3_mal
50210 6c 6f 63 48 61 73 46 61 69 6c 65 64 20 3d 20 30  locHasFailed = 0
50220 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
50230 20 69 6e 74 20 73 71 6c 69 74 65 33 41 70 69 45   int sqlite3ApiE
50240 78 69 74 28 73 71 6c 69 74 65 33 2a 20 64 62 2c  xit(sqlite3* db,
50250 20 69 6e 74 20 72 63 29 7b 0a 20 20 69 66 28 20   int rc){.  if( 
50260 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69  sqlite3MallocFai
50270 6c 65 64 28 29 20 29 7b 0a 20 20 20 20 73 71 6c  led() ){.    sql
50280 69 74 65 33 5f 6d 61 6c 6c 6f 63 48 61 73 46 61  ite3_mallocHasFa
50290 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 73 71  iled = 0;.    sq
502a0 6c 69 74 65 33 4f 73 4c 65 61 76 65 4d 75 74 65  lite3OsLeaveMute
502b0 78 28 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  x();.    sqlite3
502c0 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45  Error(db, SQLITE
502d0 5f 4e 4f 4d 45 4d 2c 20 30 29 3b 0a 20 20 20 20  _NOMEM, 0);.    
502e0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
502f0 4d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  M;.  }.  return 
50300 72 63 20 26 20 28 64 62 20 3f 20 64 62 2d 3e 65  rc & (db ? db->e
50310 72 72 4d 61 73 6b 20 3a 20 30 78 66 66 29 3b 0a  rrMask : 0xff);.
50320 7d 0a 0a 2f 2a 20 0a 2a 2a 20 53 65 74 20 74 68  }../* .** Set th
50330 65 20 22 6d 61 6c 6c 6f 63 20 68 61 73 20 66 61  e "malloc has fa
50340 69 6c 65 64 22 20 63 6f 6e 64 69 74 69 6f 6e 20  iled" condition 
50350 74 6f 20 74 72 75 65 20 66 6f 72 20 74 68 69 73  to true for this
50360 20 74 68 72 65 61 64 2e 0a 2a 2f 0a 53 51 4c 49   thread..*/.SQLI
50370 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
50380 73 71 6c 69 74 65 33 46 61 69 6c 65 64 4d 61 6c  sqlite3FailedMal
50390 6c 6f 63 28 29 7b 0a 20 20 69 66 28 20 21 73 71  loc(){.  if( !sq
503a0 6c 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69 6c 65  lite3MallocFaile
503b0 64 28 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  d() ){.    sqlit
503c0 65 33 4f 73 45 6e 74 65 72 4d 75 74 65 78 28 29  e3OsEnterMutex()
503d0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71  ;.    assert( sq
503e0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 48 61 73 46  lite3_mallocHasF
503f0 61 69 6c 65 64 3d 3d 30 20 29 3b 0a 20 20 20 20  ailed==0 );.    
50400 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 48 61  sqlite3_mallocHa
50410 73 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d  sFailed = 1;.  }
50420 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .}..#ifdef SQLIT
50430 45 5f 4d 45 4d 44 45 42 55 47 0a 2f 2a 0a 2a 2a  E_MEMDEBUG./*.**
50440 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73   This function s
50450 65 74 73 20 61 20 66 6c 61 67 20 69 6e 20 74 68  ets a flag in th
50460 65 20 74 68 72 65 61 64 2d 73 70 65 63 69 66 69  e thread-specifi
50470 63 2d 64 61 74 61 20 73 74 72 75 63 74 75 72 65  c-data structure
50480 20 74 68 61 74 20 77 69 6c 6c 0a 2a 2a 20 63 61   that will.** ca
50490 75 73 65 20 61 6e 20 61 73 73 65 72 74 20 74 6f  use an assert to
504a0 20 66 61 69 6c 20 69 66 20 73 71 6c 69 74 65 4d   fail if sqliteM
504b0 61 6c 6c 6f 63 28 29 20 6f 72 20 73 71 6c 69 74  alloc() or sqlit
504c0 65 52 65 61 6c 6c 6f 63 28 29 20 69 73 20 63 61  eRealloc() is ca
504d0 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  lled..*/.SQLITE_
504e0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
504f0 69 74 65 33 4d 61 6c 6c 6f 63 44 69 73 61 6c 6c  ite3MallocDisall
50500 6f 77 28 29 7b 0a 20 20 61 73 73 65 72 74 28 20  ow(){.  assert( 
50510 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 44 69  sqlite3_mallocDi
50520 73 61 6c 6c 6f 77 65 64 3e 3d 30 20 29 3b 0a 20  sallowed>=0 );. 
50530 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 44   sqlite3_mallocD
50540 69 73 61 6c 6c 6f 77 65 64 2b 2b 3b 0a 7d 0a 0a  isallowed++;.}..
50550 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
50560 69 6f 6e 20 63 6c 65 61 72 73 20 74 68 65 20 66  ion clears the f
50570 6c 61 67 20 73 65 74 20 69 6e 20 74 68 65 20 74  lag set in the t
50580 68 72 65 61 64 2d 73 70 65 63 69 66 69 63 2d 64  hread-specific-d
50590 61 74 61 20 73 74 72 75 63 74 75 72 65 20 73 65  ata structure se
505a0 74 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65 33 4d  t.** by sqlite3M
505b0 61 6c 6c 6f 63 44 69 73 61 6c 6c 6f 77 28 29 2e  allocDisallow().
505c0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
505d0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d  TE void sqlite3M
505e0 61 6c 6c 6f 63 41 6c 6c 6f 77 28 29 7b 0a 20 20  allocAllow(){.  
505f0 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
50600 6d 61 6c 6c 6f 63 44 69 73 61 6c 6c 6f 77 65 64  mallocDisallowed
50610 3e 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  >0 );.  sqlite3_
50620 6d 61 6c 6c 6f 63 44 69 73 61 6c 6c 6f 77 65 64  mallocDisallowed
50630 2d 2d 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  --;.}.#endif../*
50640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
50650 64 20 6f 66 20 6d 61 6c 6c 6f 63 2e 63 20 2a 2a  d of malloc.c **
50660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
50690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
506a0 67 69 6e 20 66 69 6c 65 20 70 72 69 6e 74 66 2e  gin file printf.
506b0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
506c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
506d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
506e0 0a 2a 2a 20 54 68 65 20 22 70 72 69 6e 74 66 22  .** The "printf"
506f0 20 63 6f 64 65 20 74 68 61 74 20 66 6f 6c 6c 6f   code that follo
50700 77 73 20 64 61 74 65 73 20 66 72 6f 6d 20 74 68  ws dates from th
50710 65 20 31 39 38 30 27 73 2e 20 20 49 74 20 69 73  e 1980's.  It is
50720 20 69 6e 0a 2a 2a 20 74 68 65 20 70 75 62 6c 69   in.** the publi
50730 63 20 64 6f 6d 61 69 6e 2e 20 20 54 68 65 20 6f  c domain.  The o
50740 72 69 67 69 6e 61 6c 20 63 6f 6d 6d 65 6e 74 73  riginal comments
50750 20 61 72 65 20 69 6e 63 6c 75 64 65 64 20 68 65   are included he
50760 72 65 20 66 6f 72 0a 2a 2a 20 63 6f 6d 70 6c 65  re for.** comple
50770 74 65 6e 65 73 73 2e 20 20 54 68 65 79 20 61 72  teness.  They ar
50780 65 20 76 65 72 79 20 6f 75 74 2d 6f 66 2d 64 61  e very out-of-da
50790 74 65 20 62 75 74 20 6d 69 67 68 74 20 62 65 20  te but might be 
507a0 75 73 65 66 75 6c 20 61 73 0a 2a 2a 20 61 6e 20  useful as.** an 
507b0 68 69 73 74 6f 72 69 63 61 6c 20 72 65 66 65 72  historical refer
507c0 65 6e 63 65 2e 20 20 4d 6f 73 74 20 6f 66 20 74  ence.  Most of t
507d0 68 65 20 22 65 6e 68 61 6e 63 65 6d 65 6e 74 73  he "enhancements
507e0 22 20 68 61 76 65 20 62 65 65 6e 20 62 61 63 6b  " have been back
507f0 65 64 0a 2a 2a 20 6f 75 74 20 73 6f 20 74 68 61  ed.** out so tha
50800 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 61 6c  t the functional
50810 69 74 79 20 69 73 20 6e 6f 77 20 74 68 65 20 73  ity is now the s
50820 61 6d 65 20 61 73 20 73 74 61 6e 64 61 72 64 20  ame as standard 
50830 70 72 69 6e 74 66 28 29 2e 0a 2a 2a 0a 2a 2a 2a  printf()..**.***
50840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50880 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
50890 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 6f 64 75  e following modu
508a0 6c 65 73 20 69 73 20 61 6e 20 65 6e 68 61 6e 63  les is an enhanc
508b0 65 64 20 72 65 70 6c 61 63 65 6d 65 6e 74 20 66  ed replacement f
508c0 6f 72 20 74 68 65 20 22 70 72 69 6e 74 66 22 20  or the "printf" 
508d0 73 75 62 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 66  subroutines.** f
508e0 6f 75 6e 64 20 69 6e 20 74 68 65 20 73 74 61 6e  ound in the stan
508f0 64 61 72 64 20 43 20 6c 69 62 72 61 72 79 2e 20  dard C library. 
50900 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65   The following e
50910 6e 68 61 6e 63 65 6d 65 6e 74 73 20 61 72 65 0a  nhancements are.
50920 2a 2a 20 73 75 70 70 6f 72 74 65 64 3a 0a 2a 2a  ** supported:.**
50930 0a 2a 2a 20 20 20 20 20 20 2b 20 20 41 64 64 69  .**      +  Addi
50940 74 69 6f 6e 61 6c 20 66 75 6e 63 74 69 6f 6e 73  tional functions
50950 2e 20 20 54 68 65 20 73 74 61 6e 64 61 72 64 20  .  The standard 
50960 73 65 74 20 6f 66 20 22 70 72 69 6e 74 66 22 20  set of "printf" 
50970 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 20 20 20  functions.**    
50980 20 20 20 20 20 69 6e 63 6c 75 64 65 73 20 70 72       includes pr
50990 69 6e 74 66 2c 20 66 70 72 69 6e 74 66 2c 20 73  intf, fprintf, s
509a0 70 72 69 6e 74 66 2c 20 76 70 72 69 6e 74 66 2c  printf, vprintf,
509b0 20 76 66 70 72 69 6e 74 66 2c 20 61 6e 64 0a 2a   vfprintf, and.*
509c0 2a 20 20 20 20 20 20 20 20 20 76 73 70 72 69 6e  *         vsprin
509d0 74 66 2e 20 20 54 68 69 73 20 6d 6f 64 75 6c 65  tf.  This module
509e0 20 61 64 64 73 20 74 68 65 20 66 6f 6c 6c 6f 77   adds the follow
509f0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  ing:.**.**      
50a00 20 20 20 20 20 2a 20 20 73 6e 70 72 69 6e 74 66       *  snprintf
50a10 20 2d 2d 20 57 6f 72 6b 73 20 6c 69 6b 65 20 73   -- Works like s
50a20 70 72 69 6e 74 66 2c 20 62 75 74 20 68 61 73 20  printf, but has 
50a30 61 6e 20 65 78 74 72 61 20 61 72 67 75 6d 65 6e  an extra argumen
50a40 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
50a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77 68                wh
50a60 69 63 68 20 69 73 20 74 68 65 20 73 69 7a 65 20  ich is the size 
50a70 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 77 72  of the buffer wr
50a80 69 74 74 65 6e 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20  itten to..**.** 
50a90 20 20 20 20 20 20 20 20 20 20 2a 20 20 6d 70 72            *  mpr
50aa0 69 6e 74 66 20 2d 2d 20 20 53 69 6d 69 6c 61 72  intf --  Similar
50ab0 20 74 6f 20 73 70 72 69 6e 74 66 2e 20 20 57 72   to sprintf.  Wr
50ac0 69 74 65 73 20 6f 75 74 70 75 74 20 74 6f 20 6d  ites output to m
50ad0 65 6d 6f 72 79 0a 2a 2a 20 20 20 20 20 20 20 20  emory.**        
50ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50af0 20 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20    obtained from 
50b00 6d 61 6c 6c 6f 63 2e 0a 2a 2a 0a 2a 2a 20 20 20  malloc..**.**   
50b10 20 20 20 20 20 20 20 20 2a 20 20 78 70 72 69 6e          *  xprin
50b20 74 66 20 2d 2d 20 20 43 61 6c 6c 73 20 61 20 66  tf --  Calls a f
50b30 75 6e 63 74 69 6f 6e 20 74 6f 20 64 69 73 70 6f  unction to dispo
50b40 73 65 20 6f 66 20 6f 75 74 70 75 74 2e 0a 2a 2a  se of output..**
50b50 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20  .**           * 
50b60 20 6e 70 72 69 6e 74 66 20 2d 2d 20 20 4e 6f 20   nprintf --  No 
50b70 6f 75 74 70 75 74 2c 20 62 75 74 20 72 65 74 75  output, but retu
50b80 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  rns the number o
50b90 66 20 63 68 61 72 61 63 74 65 72 73 0a 2a 2a 20  f characters.** 
50ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50bb0 20 20 20 20 20 20 20 20 20 74 68 61 74 20 77 6f           that wo
50bc0 75 6c 64 20 68 61 76 65 20 62 65 65 6e 20 6f 75  uld have been ou
50bd0 74 70 75 74 20 62 79 20 70 72 69 6e 74 66 2e 0a  tput by printf..
50be0 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  **.**           
50bf0 2a 20 20 41 20 76 2d 20 76 65 72 73 69 6f 6e 20  *  A v- version 
50c00 28 65 78 3a 20 76 73 6e 70 72 69 6e 74 66 29 20  (ex: vsnprintf) 
50c10 6f 66 20 65 76 65 72 79 20 66 75 6e 63 74 69 6f  of every functio
50c20 6e 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 20 20 20  n is also.**    
50c30 20 20 20 20 20 20 20 20 20 20 73 75 70 70 6c 69            suppli
50c40 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b  ed..**.**      +
50c50 20 20 41 20 66 65 77 20 65 78 74 65 6e 73 69 6f    A few extensio
50c60 6e 73 20 74 6f 20 74 68 65 20 66 6f 72 6d 61 74  ns to the format
50c70 74 69 6e 67 20 6e 6f 74 61 74 69 6f 6e 20 61 72  ting notation ar
50c80 65 20 73 75 70 70 6f 72 74 65 64 3a 0a 2a 2a 0a  e supported:.**.
50c90 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20  **           *  
50ca0 54 68 65 20 22 3d 22 20 66 6c 61 67 20 28 73 69  The "=" flag (si
50cb0 6d 69 6c 61 72 20 74 6f 20 22 2d 22 29 20 63 61  milar to "-") ca
50cc0 75 73 65 73 20 74 68 65 20 6f 75 74 70 75 74 20  uses the output 
50cd0 74 6f 20 62 65 0a 2a 2a 20 20 20 20 20 20 20 20  to be.**        
50ce0 20 20 20 20 20 20 62 65 20 63 65 6e 74 65 72 65        be centere
50cf0 64 20 69 6e 20 74 68 65 20 61 70 70 72 6f 70 72  d in the appropr
50d00 69 61 74 65 6c 79 20 73 69 7a 65 64 20 66 69 65  iately sized fie
50d10 6c 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  ld..**.**       
50d20 20 20 20 20 2a 20 20 54 68 65 20 25 62 20 66 69      *  The %b fi
50d30 65 6c 64 20 6f 75 74 70 75 74 73 20 61 6e 20 69  eld outputs an i
50d40 6e 74 65 67 65 72 20 69 6e 20 62 69 6e 61 72 79  nteger in binary
50d50 20 6e 6f 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   notation..**.**
50d60 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 54 68             *  Th
50d70 65 20 25 63 20 66 69 65 6c 64 20 6e 6f 77 20 61  e %c field now a
50d80 63 63 65 70 74 73 20 61 20 70 72 65 63 69 73 69  ccepts a precisi
50d90 6f 6e 2e 20 20 54 68 65 20 63 68 61 72 61 63 74  on.  The charact
50da0 65 72 20 6f 75 74 70 75 74 0a 2a 2a 20 20 20 20  er output.**    
50db0 20 20 20 20 20 20 20 20 20 20 69 73 20 72 65 70            is rep
50dc0 65 61 74 65 64 20 62 79 20 74 68 65 20 6e 75 6d  eated by the num
50dd0 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 65  ber of times the
50de0 20 70 72 65 63 69 73 69 6f 6e 20 73 70 65 63 69   precision speci
50df0 66 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  fies..**.**     
50e00 20 20 20 20 20 20 2a 20 20 54 68 65 20 25 27 20        *  The %' 
50e10 66 69 65 6c 64 20 77 6f 72 6b 73 20 6c 69 6b 65  field works like
50e20 20 25 63 2c 20 62 75 74 20 74 61 6b 65 73 20 61   %c, but takes a
50e30 73 20 69 74 73 20 63 68 61 72 61 63 74 65 72 20  s its character 
50e40 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
50e50 20 20 20 20 6e 65 78 74 20 63 68 61 72 61 63 74      next charact
50e60 65 72 20 6f 66 20 74 68 65 20 66 6f 72 6d 61 74  er of the format
50e70 20 73 74 72 69 6e 67 2c 20 69 6e 73 74 65 61 64   string, instead
50e80 20 6f 66 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20   of the next.** 
50e90 20 20 20 20 20 20 20 20 20 20 20 20 20 61 72 67               arg
50ea0 75 6d 65 6e 74 2e 20 20 46 6f 72 20 65 78 61 6d  ument.  For exam
50eb0 70 6c 65 2c 20 20 70 72 69 6e 74 66 28 22 25 2e  ple,  printf("%.
50ec0 37 38 27 2d 22 29 20 20 70 72 69 6e 74 73 20 37  78'-")  prints 7
50ed0 38 20 6d 69 6e 75 73 0a 2a 2a 20 20 20 20 20 20  8 minus.**      
50ee0 20 20 20 20 20 20 20 20 73 69 67 6e 73 2c 20 74          signs, t
50ef0 68 65 20 73 61 6d 65 20 61 73 20 20 70 72 69 6e  he same as  prin
50f00 74 66 28 22 25 2e 37 38 63 22 2c 27 2d 27 29 2e  tf("%.78c",'-').
50f10 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20 57  .**.**      +  W
50f20 68 65 6e 20 63 6f 6d 70 69 6c 65 64 20 75 73 69  hen compiled usi
50f30 6e 67 20 47 43 43 20 6f 6e 20 61 20 53 50 41 52  ng GCC on a SPAR
50f40 43 2c 20 74 68 69 73 20 76 65 72 73 69 6f 6e 20  C, this version 
50f50 6f 66 20 70 72 69 6e 74 66 20 69 73 0a 2a 2a 20  of printf is.** 
50f60 20 20 20 20 20 20 20 20 66 61 73 74 65 72 20 74          faster t
50f70 68 61 6e 20 74 68 65 20 6c 69 62 72 61 72 79 20  han the library 
50f80 70 72 69 6e 74 66 20 66 6f 72 20 53 55 4e 20 4f  printf for SUN O
50f90 53 20 34 2e 31 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  S 4.1..**.**    
50fa0 20 20 2b 20 20 41 6c 6c 20 66 75 6e 63 74 69 6f    +  All functio
50fb0 6e 73 20 61 72 65 20 66 75 6c 6c 79 20 72 65 65  ns are fully ree
50fc0 6e 74 72 61 6e 74 2e 0a 2a 2a 0a 2a 2f 0a 23 69  ntrant..**.*/.#i
50fd0 6e 63 6c 75 64 65 20 3c 6d 61 74 68 2e 68 3e 0a  nclude <math.h>.
50fe0 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 73 69 6f  ./*.** Conversio
50ff0 6e 20 74 79 70 65 73 20 66 61 6c 6c 20 69 6e 74  n types fall int
51000 6f 20 76 61 72 69 6f 75 73 20 63 61 74 65 67 6f  o various catego
51010 72 69 65 73 20 61 73 20 64 65 66 69 6e 65 64 20  ries as defined 
51020 62 79 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77  by the.** follow
51030 69 6e 67 20 65 6e 75 6d 65 72 61 74 69 6f 6e 2e  ing enumeration.
51040 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 52 41  .*/.#define etRA
51050 44 49 58 20 20 20 20 20 20 20 31 20 2f 2a 20 49  DIX       1 /* I
51060 6e 74 65 67 65 72 20 74 79 70 65 73 2e 20 20 25  nteger types.  %
51070 64 2c 20 25 78 2c 20 25 6f 2c 20 61 6e 64 20 73  d, %x, %o, and s
51080 6f 20 66 6f 72 74 68 20 2a 2f 0a 23 64 65 66 69  o forth */.#defi
51090 6e 65 20 65 74 46 4c 4f 41 54 20 20 20 20 20 20  ne etFLOAT      
510a0 20 32 20 2f 2a 20 46 6c 6f 61 74 69 6e 67 20 70   2 /* Floating p
510b0 6f 69 6e 74 2e 20 20 25 66 20 2a 2f 0a 23 64 65  oint.  %f */.#de
510c0 66 69 6e 65 20 65 74 45 58 50 20 20 20 20 20 20  fine etEXP      
510d0 20 20 20 33 20 2f 2a 20 45 78 70 6f 6e 65 6e 74     3 /* Exponent
510e0 69 6f 6e 61 6c 20 6e 6f 74 61 74 69 6f 6e 2e 20  ional notation. 
510f0 25 65 20 61 6e 64 20 25 45 20 2a 2f 0a 23 64 65  %e and %E */.#de
51100 66 69 6e 65 20 65 74 47 45 4e 45 52 49 43 20 20  fine etGENERIC  
51110 20 20 20 34 20 2f 2a 20 46 6c 6f 61 74 69 6e 67     4 /* Floating
51120 20 6f 72 20 65 78 70 6f 6e 65 6e 74 69 61 6c 2c   or exponential,
51130 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 65 78   depending on ex
51140 70 6f 6e 65 6e 74 2e 20 25 67 20 2a 2f 0a 23 64  ponent. %g */.#d
51150 65 66 69 6e 65 20 65 74 53 49 5a 45 20 20 20 20  efine etSIZE    
51160 20 20 20 20 35 20 2f 2a 20 52 65 74 75 72 6e 20      5 /* Return 
51170 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63  number of charac
51180 74 65 72 73 20 70 72 6f 63 65 73 73 65 64 20 73  ters processed s
51190 6f 20 66 61 72 2e 20 25 6e 20 2a 2f 0a 23 64 65  o far. %n */.#de
511a0 66 69 6e 65 20 65 74 53 54 52 49 4e 47 20 20 20  fine etSTRING   
511b0 20 20 20 36 20 2f 2a 20 53 74 72 69 6e 67 73 2e     6 /* Strings.
511c0 20 25 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65   %s */.#define e
511d0 74 44 59 4e 53 54 52 49 4e 47 20 20 20 37 20 2f  tDYNSTRING   7 /
511e0 2a 20 44 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c  * Dynamically al
511f0 6c 6f 63 61 74 65 64 20 73 74 72 69 6e 67 73 2e  located strings.
51200 20 25 7a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65   %z */.#define e
51210 74 50 45 52 43 45 4e 54 20 20 20 20 20 38 20 2f  tPERCENT     8 /
51220 2a 20 50 65 72 63 65 6e 74 20 73 79 6d 62 6f 6c  * Percent symbol
51230 2e 20 25 25 20 2a 2f 0a 23 64 65 66 69 6e 65 20  . %% */.#define 
51240 65 74 43 48 41 52 58 20 20 20 20 20 20 20 39 20  etCHARX       9 
51250 2f 2a 20 43 68 61 72 61 63 74 65 72 73 2e 20 25  /* Characters. %
51260 63 20 2a 2f 0a 2f 2a 20 54 68 65 20 72 65 73 74  c */./* The rest
51270 20 61 72 65 20 65 78 74 65 6e 73 69 6f 6e 73 2c   are extensions,
51280 20 6e 6f 74 20 6e 6f 72 6d 61 6c 6c 79 20 66 6f   not normally fo
51290 75 6e 64 20 69 6e 20 70 72 69 6e 74 66 28 29 20  und in printf() 
512a0 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 43 48 41  */.#define etCHA
512b0 52 4c 49 54 20 20 20 20 31 30 20 2f 2a 20 4c 69  RLIT    10 /* Li
512c0 74 65 72 61 6c 20 63 68 61 72 61 63 74 65 72 73  teral characters
512d0 2e 20 20 25 27 20 2a 2f 0a 23 64 65 66 69 6e 65  .  %' */.#define
512e0 20 65 74 53 51 4c 45 53 43 41 50 45 20 20 31 31   etSQLESCAPE  11
512f0 20 2f 2a 20 53 74 72 69 6e 67 73 20 77 69 74 68   /* Strings with
51300 20 27 5c 27 27 20 64 6f 75 62 6c 65 64 2e 20 20   '\'' doubled.  
51310 25 71 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74  %q */.#define et
51320 53 51 4c 45 53 43 41 50 45 32 20 31 32 20 2f 2a  SQLESCAPE2 12 /*
51330 20 53 74 72 69 6e 67 73 20 77 69 74 68 20 27 5c   Strings with '\
51340 27 27 20 64 6f 75 62 6c 65 64 20 61 6e 64 20 65  '' doubled and e
51350 6e 63 6c 6f 73 65 64 20 69 6e 20 27 27 2c 0a 20  nclosed in '',. 
51360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51370 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 20 70 6f           NULL po
51380 69 6e 74 65 72 73 20 72 65 70 6c 61 63 65 64 20  inters replaced 
51390 62 79 20 53 51 4c 20 4e 55 4c 4c 2e 20 20 25 51  by SQL NULL.  %Q
513a0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 54 4f   */.#define etTO
513b0 4b 45 4e 20 20 20 20 20 20 31 33 20 2f 2a 20 61  KEN      13 /* a
513c0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 54 6f   pointer to a To
513d0 6b 65 6e 20 73 74 72 75 63 74 75 72 65 20 2a 2f  ken structure */
513e0 0a 23 64 65 66 69 6e 65 20 65 74 53 52 43 4c 49  .#define etSRCLI
513f0 53 54 20 20 20 20 31 34 20 2f 2a 20 61 20 70 6f  ST    14 /* a po
51400 69 6e 74 65 72 20 74 6f 20 61 20 53 72 63 4c 69  inter to a SrcLi
51410 73 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74  st */.#define et
51420 50 4f 49 4e 54 45 52 20 20 20 20 31 35 20 2f 2a  POINTER    15 /*
51430 20 54 68 65 20 25 70 20 63 6f 6e 76 65 72 73 69   The %p conversi
51440 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74  on */.#define et
51450 53 51 4c 45 53 43 41 50 45 33 20 31 36 20 2f 2a  SQLESCAPE3 16 /*
51460 20 25 77 20 2d 3e 20 53 74 72 69 6e 67 73 20 77   %w -> Strings w
51470 69 74 68 20 27 5c 22 27 20 64 6f 75 62 6c 65 64  ith '\"' doubled
51480 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 22   */.../*.** An "
51490 65 74 42 79 74 65 22 20 69 73 20 61 6e 20 38 2d  etByte" is an 8-
514a0 62 69 74 20 75 6e 73 69 67 6e 65 64 20 76 61 6c  bit unsigned val
514b0 75 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75  ue..*/.typedef u
514c0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 65 74 42  nsigned char etB
514d0 79 74 65 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68  yte;../*.** Each
514e0 20 62 75 69 6c 74 69 6e 20 63 6f 6e 76 65 72 73   builtin convers
514f0 69 6f 6e 20 63 68 61 72 61 63 74 65 72 20 28 65  ion character (e
51500 78 3a 20 74 68 65 20 27 64 27 20 69 6e 20 22 25  x: the 'd' in "%
51510 64 22 29 20 69 73 20 64 65 73 63 72 69 62 65 64  d") is described
51520 0a 2a 2a 20 62 79 20 61 6e 20 69 6e 73 74 61 6e  .** by an instan
51530 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
51540 69 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2f  ing structure.*/
51550 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
51560 65 74 5f 69 6e 66 6f 20 7b 20 20 20 2f 2a 20 49  et_info {   /* I
51570 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
51580 20 65 61 63 68 20 66 6f 72 6d 61 74 20 66 69 65   each format fie
51590 6c 64 20 2a 2f 0a 20 20 63 68 61 72 20 66 6d 74  ld */.  char fmt
515a0 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20  type;           
515b0 20 2f 2a 20 54 68 65 20 66 6f 72 6d 61 74 20 66   /* The format f
515c0 69 65 6c 64 20 63 6f 64 65 20 6c 65 74 74 65 72  ield code letter
515d0 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 62 61 73   */.  etByte bas
515e0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  e;             /
515f0 2a 20 54 68 65 20 62 61 73 65 20 66 6f 72 20 72  * The base for r
51600 61 64 69 78 20 63 6f 6e 76 65 72 73 69 6f 6e 20  adix conversion 
51610 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67  */.  etByte flag
51620 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  s;            /*
51630 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20   One or more of 
51640 46 4c 41 47 5f 20 63 6f 6e 73 74 61 6e 74 73 20  FLAG_ constants 
51650 62 65 6c 6f 77 20 2a 2f 0a 20 20 65 74 42 79 74  below */.  etByt
51660 65 20 74 79 70 65 3b 20 20 20 20 20 20 20 20 20  e type;         
51670 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69 6f      /* Conversio
51680 6e 20 70 61 72 61 64 69 67 6d 20 2a 2f 0a 20 20  n paradigm */.  
51690 65 74 42 79 74 65 20 63 68 61 72 73 65 74 3b 20  etByte charset; 
516a0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73           /* Offs
516b0 65 74 20 69 6e 74 6f 20 61 44 69 67 69 74 73 5b  et into aDigits[
516c0 5d 20 6f 66 20 74 68 65 20 64 69 67 69 74 73 20  ] of the digits 
516d0 73 74 72 69 6e 67 20 2a 2f 0a 20 20 65 74 42 79  string */.  etBy
516e0 74 65 20 70 72 65 66 69 78 3b 20 20 20 20 20 20  te prefix;      
516f0 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69       /* Offset i
51700 6e 74 6f 20 61 50 72 65 66 69 78 5b 5d 20 6f 66  nto aPrefix[] of
51710 20 74 68 65 20 70 72 65 66 69 78 20 73 74 72 69   the prefix stri
51720 6e 67 20 2a 2f 0a 7d 20 65 74 5f 69 6e 66 6f 3b  ng */.} et_info;
51730 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20  ../*.** Allowed 
51740 76 61 6c 75 65 73 20 66 6f 72 20 65 74 5f 69 6e  values for et_in
51750 66 6f 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 65 66  fo.flags.*/.#def
51760 69 6e 65 20 46 4c 41 47 5f 53 49 47 4e 45 44 20  ine FLAG_SIGNED 
51770 20 31 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69   1     /* True i
51780 66 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20 63  f the value to c
51790 6f 6e 76 65 72 74 20 69 73 20 73 69 67 6e 65 64  onvert is signed
517a0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4c 41 47   */.#define FLAG
517b0 5f 49 4e 54 45 52 4e 20 20 32 20 20 20 20 20 2f  _INTERN  2     /
517c0 2a 20 54 72 75 65 20 69 66 20 66 6f 72 20 69 6e  * True if for in
517d0 74 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79 20  ternal use only 
517e0 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4c 41 47 5f  */.#define FLAG_
517f0 53 54 52 49 4e 47 20 20 34 20 20 20 20 20 2f 2a  STRING  4     /*
51800 20 41 6c 6c 6f 77 20 69 6e 66 69 6e 69 74 79 20   Allow infinity 
51810 70 72 65 63 69 73 69 6f 6e 20 2a 2f 0a 0a 0a 2f  precision */.../
51820 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
51830 6e 67 20 74 61 62 6c 65 20 69 73 20 73 65 61 72  ng table is sear
51840 63 68 65 64 20 6c 69 6e 65 61 72 6c 79 2c 20 73  ched linearly, s
51850 6f 20 69 74 20 69 73 20 67 6f 6f 64 20 74 6f 20  o it is good to 
51860 70 75 74 20 74 68 65 0a 2a 2a 20 6d 6f 73 74 20  put the.** most 
51870 66 72 65 71 75 65 6e 74 6c 79 20 75 73 65 64 20  frequently used 
51880 63 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 73  conversion types
51890 20 66 69 72 73 74 2e 0a 2a 2f 0a 73 74 61 74 69   first..*/.stati
518a0 63 20 63 6f 6e 73 74 20 63 68 61 72 20 61 44 69  c const char aDi
518b0 67 69 74 73 5b 5d 20 3d 20 22 30 31 32 33 34 35  gits[] = "012345
518c0 36 37 38 39 41 42 43 44 45 46 30 31 32 33 34 35  6789ABCDEF012345
518d0 36 37 38 39 61 62 63 64 65 66 22 3b 0a 73 74 61  6789abcdef";.sta
518e0 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 61  tic const char a
518f0 50 72 65 66 69 78 5b 5d 20 3d 20 22 2d 78 30 5c  Prefix[] = "-x0\
51900 30 30 30 58 30 22 3b 0a 73 74 61 74 69 63 20 63  000X0";.static c
51910 6f 6e 73 74 20 65 74 5f 69 6e 66 6f 20 66 6d 74  onst et_info fmt
51920 69 6e 66 6f 5b 5d 20 3d 20 7b 0a 20 20 7b 20 20  info[] = {.  {  
51930 27 64 27 2c 20 31 30 2c 20 31 2c 20 65 74 52 41  'd', 10, 1, etRA
51940 44 49 58 2c 20 20 20 20 20 20 30 2c 20 20 30 20  DIX,      0,  0 
51950 7d 2c 0a 20 20 7b 20 20 27 73 27 2c 20 20 30 2c  },.  {  's',  0,
51960 20 34 2c 20 65 74 53 54 52 49 4e 47 2c 20 20 20   4, etSTRING,   
51970 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20    0,  0 },.  {  
51980 27 67 27 2c 20 20 30 2c 20 31 2c 20 65 74 47 45  'g',  0, 1, etGE
51990 4e 45 52 49 43 2c 20 20 20 20 33 30 2c 20 30 20  NERIC,    30, 0 
519a0 7d 2c 0a 20 20 7b 20 20 27 7a 27 2c 20 20 30 2c  },.  {  'z',  0,
519b0 20 36 2c 20 65 74 44 59 4e 53 54 52 49 4e 47 2c   6, etDYNSTRING,
519c0 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20    0,  0 },.  {  
519d0 27 71 27 2c 20 20 30 2c 20 34 2c 20 65 74 53 51  'q',  0, 4, etSQ
519e0 4c 45 53 43 41 50 45 2c 20 20 30 2c 20 20 30 20  LESCAPE,  0,  0 
519f0 7d 2c 0a 20 20 7b 20 20 27 51 27 2c 20 20 30 2c  },.  {  'Q',  0,
51a00 20 34 2c 20 65 74 53 51 4c 45 53 43 41 50 45 32   4, etSQLESCAPE2
51a10 2c 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20  , 0,  0 },.  {  
51a20 27 77 27 2c 20 20 30 2c 20 34 2c 20 65 74 53 51  'w',  0, 4, etSQ
51a30 4c 45 53 43 41 50 45 33 2c 20 30 2c 20 20 30 20  LESCAPE3, 0,  0 
51a40 7d 2c 0a 20 20 7b 20 20 27 63 27 2c 20 20 30 2c  },.  {  'c',  0,
51a50 20 30 2c 20 65 74 43 48 41 52 58 2c 20 20 20 20   0, etCHARX,    
51a60 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20    0,  0 },.  {  
51a70 27 6f 27 2c 20 20 38 2c 20 30 2c 20 65 74 52 41  'o',  8, 0, etRA
51a80 44 49 58 2c 20 20 20 20 20 20 30 2c 20 20 32 20  DIX,      0,  2 
51a90 7d 2c 0a 20 20 7b 20 20 27 75 27 2c 20 31 30 2c  },.  {  'u', 10,
51aa0 20 30 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20   0, etRADIX,    
51ab0 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20    0,  0 },.  {  
51ac0 27 78 27 2c 20 31 36 2c 20 30 2c 20 65 74 52 41  'x', 16, 0, etRA
51ad0 44 49 58 2c 20 20 20 20 20 20 31 36 2c 20 31 20  DIX,      16, 1 
51ae0 7d 2c 0a 20 20 7b 20 20 27 58 27 2c 20 31 36 2c  },.  {  'X', 16,
51af0 20 30 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20   0, etRADIX,    
51b00 20 20 30 2c 20 20 34 20 7d 2c 0a 23 69 66 6e 64    0,  4 },.#ifnd
51b10 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
51b20 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20  LOATING_POINT.  
51b30 7b 20 20 27 66 27 2c 20 20 30 2c 20 31 2c 20 65  {  'f',  0, 1, e
51b40 74 46 4c 4f 41 54 2c 20 20 20 20 20 20 30 2c 20  tFLOAT,      0, 
51b50 20 30 20 7d 2c 0a 20 20 7b 20 20 27 65 27 2c 20   0 },.  {  'e', 
51b60 20 30 2c 20 31 2c 20 65 74 45 58 50 2c 20 20 20   0, 1, etEXP,   
51b70 20 20 20 20 20 33 30 2c 20 30 20 7d 2c 0a 20 20       30, 0 },.  
51b80 7b 20 20 27 45 27 2c 20 20 30 2c 20 31 2c 20 65  {  'E',  0, 1, e
51b90 74 45 58 50 2c 20 20 20 20 20 20 20 20 31 34 2c  tEXP,        14,
51ba0 20 30 20 7d 2c 0a 20 20 7b 20 20 27 47 27 2c 20   0 },.  {  'G', 
51bb0 20 30 2c 20 31 2c 20 65 74 47 45 4e 45 52 49 43   0, 1, etGENERIC
51bc0 2c 20 20 20 20 31 34 2c 20 30 20 7d 2c 0a 23 65  ,    14, 0 },.#e
51bd0 6e 64 69 66 0a 20 20 7b 20 20 27 69 27 2c 20 31  ndif.  {  'i', 1
51be0 30 2c 20 31 2c 20 65 74 52 41 44 49 58 2c 20 20  0, 1, etRADIX,  
51bf0 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b      0,  0 },.  {
51c00 20 20 27 6e 27 2c 20 20 30 2c 20 30 2c 20 65 74    'n',  0, 0, et
51c10 53 49 5a 45 2c 20 20 20 20 20 20 20 30 2c 20 20  SIZE,       0,  
51c20 30 20 7d 2c 0a 20 20 7b 20 20 27 25 27 2c 20 20  0 },.  {  '%',  
51c30 30 2c 20 30 2c 20 65 74 50 45 52 43 45 4e 54 2c  0, 0, etPERCENT,
51c40 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b      0,  0 },.  {
51c50 20 20 27 70 27 2c 20 31 36 2c 20 30 2c 20 65 74    'p', 16, 0, et
51c60 50 4f 49 4e 54 45 52 2c 20 20 20 20 30 2c 20 20  POINTER,    0,  
51c70 31 20 7d 2c 0a 20 20 7b 20 20 27 54 27 2c 20 20  1 },.  {  'T',  
51c80 30 2c 20 32 2c 20 65 74 54 4f 4b 45 4e 2c 20 20  0, 2, etTOKEN,  
51c90 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b      0,  0 },.  {
51ca0 20 20 27 53 27 2c 20 20 30 2c 20 32 2c 20 65 74    'S',  0, 2, et
51cb0 53 52 43 4c 49 53 54 2c 20 20 20 20 30 2c 20 20  SRCLIST,    0,  
51cc0 30 20 7d 2c 0a 7d 3b 0a 23 64 65 66 69 6e 65 20  0 },.};.#define 
51cd0 65 74 4e 49 4e 46 4f 20 20 28 73 69 7a 65 6f 66  etNINFO  (sizeof
51ce0 28 66 6d 74 69 6e 66 6f 29 2f 73 69 7a 65 6f 66  (fmtinfo)/sizeof
51cf0 28 66 6d 74 69 6e 66 6f 5b 30 5d 29 29 0a 0a 2f  (fmtinfo[0]))../
51d00 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f  *.** If SQLITE_O
51d10 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49  MIT_FLOATING_POI
51d20 4e 54 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74  NT is defined, t
51d30 68 65 6e 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20  hen none of the 
51d40 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a  floating point.*
51d50 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 77 69  * conversions wi
51d60 6c 6c 20 77 6f 72 6b 2e 0a 2a 2f 0a 23 69 66 6e  ll work..*/.#ifn
51d70 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
51d80 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 2f  FLOATING_POINT./
51d90 2a 0a 2a 2a 20 22 2a 76 61 6c 22 20 69 73 20 61  *.** "*val" is a
51da0 20 64 6f 75 62 6c 65 20 73 75 63 68 20 74 68 61   double such tha
51db0 74 20 30 2e 31 20 3c 3d 20 2a 76 61 6c 20 3c 20  t 0.1 <= *val < 
51dc0 31 30 2e 30 0a 2a 2a 20 52 65 74 75 72 6e 20 74  10.0.** Return t
51dd0 68 65 20 61 73 63 69 69 20 63 6f 64 65 20 66 6f  he ascii code fo
51de0 72 20 74 68 65 20 6c 65 61 64 69 6e 67 20 64 69  r the leading di
51df0 67 69 74 20 6f 66 20 2a 76 61 6c 2c 20 74 68 65  git of *val, the
51e00 6e 0a 2a 2a 20 6d 75 6c 74 69 70 6c 79 20 22 2a  n.** multiply "*
51e10 76 61 6c 22 20 62 79 20 31 30 2e 30 20 74 6f 20  val" by 10.0 to 
51e20 72 65 6e 6f 72 6d 61 6c 69 7a 65 2e 0a 2a 2a 0a  renormalize..**.
51e30 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a 2a 2a 20 20  ** Example:.**  
51e40 20 20 20 69 6e 70 75 74 3a 20 20 20 20 20 2a 76     input:     *v
51e50 61 6c 20 3d 20 33 2e 31 34 31 35 39 0a 2a 2a 20  al = 3.14159.** 
51e60 20 20 20 20 6f 75 74 70 75 74 3a 20 20 20 20 2a      output:    *
51e70 76 61 6c 20 3d 20 31 2e 34 31 35 39 20 20 20 20  val = 1.4159    
51e80 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 20  function return 
51e90 3d 20 27 33 27 0a 2a 2a 0a 2a 2a 20 54 68 65 20  = '3'.**.** The 
51ea0 63 6f 75 6e 74 65 72 20 2a 63 6e 74 20 69 73 20  counter *cnt is 
51eb0 69 6e 63 72 65 6d 65 6e 74 65 64 20 65 61 63 68  incremented each
51ec0 20 74 69 6d 65 2e 20 20 41 66 74 65 72 20 63 6f   time.  After co
51ed0 75 6e 74 65 72 20 65 78 63 65 65 64 73 0a 2a 2a  unter exceeds.**
51ee0 20 31 36 20 28 74 68 65 20 6e 75 6d 62 65 72 20   16 (the number 
51ef0 6f 66 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64  of significant d
51f00 69 67 69 74 73 20 69 6e 20 61 20 36 34 2d 62 69  igits in a 64-bi
51f10 74 20 66 6c 6f 61 74 29 20 27 30 27 20 69 73 0a  t float) '0' is.
51f20 2a 2a 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e  ** always return
51f30 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ed..*/.static in
51f40 74 20 65 74 5f 67 65 74 64 69 67 69 74 28 4c 4f  t et_getdigit(LO
51f50 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 2a 76  NGDOUBLE_TYPE *v
51f60 61 6c 2c 20 69 6e 74 20 2a 63 6e 74 29 7b 0a 20  al, int *cnt){. 
51f70 20 69 6e 74 20 64 69 67 69 74 3b 0a 20 20 4c 4f   int digit;.  LO
51f80 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 64 3b  NGDOUBLE_TYPE d;
51f90 0a 20 20 69 66 28 20 28 2a 63 6e 74 29 2b 2b 20  .  if( (*cnt)++ 
51fa0 3e 3d 20 31 36 20 29 20 72 65 74 75 72 6e 20 27  >= 16 ) return '
51fb0 30 27 3b 0a 20 20 64 69 67 69 74 20 3d 20 28 69  0';.  digit = (i
51fc0 6e 74 29 2a 76 61 6c 3b 0a 20 20 64 20 3d 20 64  nt)*val;.  d = d
51fd0 69 67 69 74 3b 0a 20 20 64 69 67 69 74 20 2b 3d  igit;.  digit +=
51fe0 20 27 30 27 3b 0a 20 20 2a 76 61 6c 20 3d 20 28   '0';.  *val = (
51ff0 2a 76 61 6c 20 2d 20 64 29 2a 31 30 2e 30 3b 0a  *val - d)*10.0;.
52000 20 20 72 65 74 75 72 6e 20 64 69 67 69 74 3b 0a    return digit;.
52010 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
52020 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
52030 5f 50 4f 49 4e 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  _POINT */../*.**
52040 20 4f 6e 20 6d 61 63 68 69 6e 65 73 20 77 69 74   On machines wit
52050 68 20 61 20 73 6d 61 6c 6c 20 73 74 61 63 6b 20  h a small stack 
52060 73 69 7a 65 2c 20 79 6f 75 20 63 61 6e 20 72 65  size, you can re
52070 64 65 66 69 6e 65 20 74 68 65 0a 2a 2a 20 53 51  define the.** SQ
52080 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53  LITE_PRINT_BUF_S
52090 49 5a 45 20 74 6f 20 62 65 20 6c 65 73 73 20 74  IZE to be less t
520a0 68 61 6e 20 33 35 30 2e 20 20 42 75 74 20 62 65  han 350.  But be
520b0 77 61 72 65 20 2d 20 66 6f 72 0a 2a 2a 20 73 6d  ware - for.** sm
520c0 61 6c 6c 65 72 20 76 61 6c 75 65 73 20 73 6f 6d  aller values som
520d0 65 20 25 66 20 63 6f 6e 76 65 72 73 69 6f 6e 73  e %f conversions
520e0 20 6d 61 79 20 67 6f 20 69 6e 74 6f 20 61 6e 20   may go into an 
520f0 69 6e 66 69 6e 69 74 65 20 6c 6f 6f 70 2e 0a 2a  infinite loop..*
52100 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
52110 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 0a  _PRINT_BUF_SIZE.
52120 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
52130 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20 33  PRINT_BUF_SIZE 3
52140 35 30 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e  50.#endif.#defin
52150 65 20 65 74 42 55 46 53 49 5a 45 20 53 51 4c 49  e etBUFSIZE SQLI
52160 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a  TE_PRINT_BUF_SIZ
52170 45 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68  E  /* Size of th
52180 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20  e output buffer 
52190 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f  */../*.** The ro
521a0 6f 74 20 70 72 6f 67 72 61 6d 2e 20 20 41 6c 6c  ot program.  All
521b0 20 76 61 72 69 61 74 69 6f 6e 73 20 63 61 6c 6c   variations call
521c0 20 74 68 69 73 20 63 6f 72 65 2e 0a 2a 2a 0a 2a   this core..**.*
521d0 2a 20 49 4e 50 55 54 53 3a 0a 2a 2a 20 20 20 66  * INPUTS:.**   f
521e0 75 6e 63 20 20 20 54 68 69 73 20 69 73 20 61 20  unc   This is a 
521f0 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e  pointer to a fun
52200 63 74 69 6f 6e 20 74 61 6b 69 6e 67 20 74 68 72  ction taking thr
52210 65 65 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20  ee arguments.** 
52220 20 20 20 20 20 20 20 20 20 20 20 31 2e 20 41 20             1. A 
52230 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 79 74 68  pointer to anyth
52240 69 6e 67 2e 20 20 53 61 6d 65 20 61 73 20 74 68  ing.  Same as th
52250 65 20 22 61 72 67 22 20 70 61 72 61 6d 65 74 65  e "arg" paramete
52260 72 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  r..**           
52270 20 32 2e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f   2. A pointer to
52280 20 74 68 65 20 6c 69 73 74 20 6f 66 20 63 68 61   the list of cha
52290 72 61 63 74 65 72 73 20 74 6f 20 62 65 20 6f 75  racters to be ou
522a0 74 70 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  tput.**         
522b0 20 20 20 20 20 20 28 4e 6f 74 65 2c 20 74 68 69        (Note, thi
522c0 73 20 6c 69 73 74 20 69 73 20 4e 4f 54 20 6e 75  s list is NOT nu
522d0 6c 6c 20 74 65 72 6d 69 6e 61 74 65 64 2e 29 0a  ll terminated.).
522e0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 33 2e  **            3.
522f0 20 41 6e 20 69 6e 74 65 67 65 72 20 6e 75 6d 62   An integer numb
52300 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73  er of characters
52310 20 74 6f 20 62 65 20 6f 75 74 70 75 74 2e 0a 2a   to be output..*
52320 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
52330 28 4e 6f 74 65 3a 20 54 68 69 73 20 6e 75 6d 62  (Note: This numb
52340 65 72 20 6d 69 67 68 74 20 62 65 20 7a 65 72 6f  er might be zero
52350 2e 29 0a 2a 2a 0a 2a 2a 20 20 20 61 72 67 20 20  .).**.**   arg  
52360 20 20 54 68 69 73 20 69 73 20 74 68 65 20 70 6f    This is the po
52370 69 6e 74 65 72 20 74 6f 20 61 6e 79 74 68 69 6e  inter to anythin
52380 67 20 77 68 69 63 68 20 77 69 6c 6c 20 62 65 20  g which will be 
52390 70 61 73 73 65 64 20 61 73 20 74 68 65 0a 2a 2a  passed as the.**
523a0 20 20 20 20 20 20 20 20 20 20 66 69 72 73 74 20            first 
523b0 61 72 67 75 6d 65 6e 74 20 74 6f 20 22 66 75 6e  argument to "fun
523c0 63 22 2e 20 20 55 73 65 20 69 74 20 66 6f 72 20  c".  Use it for 
523d0 77 68 61 74 65 76 65 72 20 79 6f 75 20 6c 69 6b  whatever you lik
523e0 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 66 6d 74 20 20  e..**.**   fmt  
523f0 20 20 54 68 69 73 20 69 73 20 74 68 65 20 66 6f    This is the fo
52400 72 6d 61 74 20 73 74 72 69 6e 67 2c 20 61 73 20  rmat string, as 
52410 69 6e 20 74 68 65 20 75 73 75 61 6c 20 70 72 69  in the usual pri
52420 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 61 70 20 20  nt..**.**   ap  
52430 20 20 20 54 68 69 73 20 69 73 20 61 20 70 6f 69     This is a poi
52440 6e 74 65 72 20 74 6f 20 61 20 6c 69 73 74 20 6f  nter to a list o
52450 66 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 53 61  f arguments.  Sa
52460 6d 65 20 61 73 20 69 6e 0a 2a 2a 20 20 20 20 20  me as in.**     
52470 20 20 20 20 20 76 66 70 72 69 6e 74 2e 0a 2a 2a       vfprint..**
52480 0a 2a 2a 20 4f 55 54 50 55 54 53 3a 0a 2a 2a 20  .** OUTPUTS:.** 
52490 20 20 20 20 20 20 20 20 20 54 68 65 20 72 65 74           The ret
524a0 75 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65  urn value is the
524b0 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   total number of
524c0 20 63 68 61 72 61 63 74 65 72 73 20 73 65 6e 74   characters sent
524d0 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   to.**          
524e0 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 22 66 75  the function "fu
524f0 6e 63 22 2e 20 20 52 65 74 75 72 6e 73 20 2d 31  nc".  Returns -1
52500 20 6f 6e 20 61 20 65 72 72 6f 72 2e 0a 2a 2a 0a   on a error..**.
52510 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65  ** Note that the
52520 20 6f 72 64 65 72 20 69 6e 20 77 68 69 63 68 20   order in which 
52530 61 75 74 6f 6d 61 74 69 63 20 76 61 72 69 61 62  automatic variab
52540 6c 65 73 20 61 72 65 20 64 65 63 6c 61 72 65 64  les are declared
52550 20 62 65 6c 6f 77 0a 2a 2a 20 73 65 65 6d 73 20   below.** seems 
52560 74 6f 20 6d 61 6b 65 20 61 20 62 69 67 20 64 69  to make a big di
52570 66 66 65 72 65 6e 63 65 20 69 6e 20 64 65 74 65  fference in dete
52580 72 6d 69 6e 69 6e 67 20 68 6f 77 20 66 61 73 74  rmining how fast
52590 20 74 68 69 73 20 62 65 61 73 74 0a 2a 2a 20 77   this beast.** w
525a0 69 6c 6c 20 72 75 6e 2e 0a 2a 2f 0a 73 74 61 74  ill run..*/.stat
525b0 69 63 20 69 6e 74 20 76 78 70 72 69 6e 74 66 28  ic int vxprintf(
525c0 0a 20 20 76 6f 69 64 20 28 2a 66 75 6e 63 29 28  .  void (*func)(
525d0 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72  void*,const char
525e0 2a 2c 69 6e 74 29 2c 20 20 20 20 20 2f 2a 20 43  *,int),     /* C
525f0 6f 6e 73 75 6d 65 72 20 6f 66 20 74 65 78 74 20  onsumer of text 
52600 2a 2f 0a 20 20 76 6f 69 64 20 2a 61 72 67 2c 20  */.  void *arg, 
52610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52620 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
52630 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   argument to the
52640 20 63 6f 6e 73 75 6d 65 72 20 2a 2f 0a 20 20 69   consumer */.  i
52650 6e 74 20 75 73 65 45 78 74 65 6e 64 65 64 2c 20  nt useExtended, 
52660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52670 20 20 2f 2a 20 41 6c 6c 6f 77 20 65 78 74 65 6e    /* Allow exten
52680 64 65 64 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e  ded %-conversion
52690 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  s */.  const cha
526a0 72 20 2a 66 6d 74 2c 20 20 20 20 20 20 20 20 20  r *fmt,         
526b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72            /* For
526c0 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20  mat string */.  
526d0 76 61 5f 6c 69 73 74 20 61 70 20 20 20 20 20 20  va_list ap      
526e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
526f0 20 20 20 2f 2a 20 61 72 67 75 6d 65 6e 74 73 20     /* arguments 
52700 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 3b 20 20  */.){.  int c;  
52710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52720 20 20 20 2f 2a 20 4e 65 78 74 20 63 68 61 72 61     /* Next chara
52730 63 74 65 72 20 69 6e 20 74 68 65 20 66 6f 72 6d  cter in the form
52740 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 63  at string */.  c
52750 68 61 72 20 2a 62 75 66 70 74 3b 20 20 20 20 20  har *bufpt;     
52760 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
52770 6e 74 65 72 20 74 6f 20 74 68 65 20 63 6f 6e 76  nter to the conv
52780 65 72 73 69 6f 6e 20 62 75 66 66 65 72 20 2a 2f  ersion buffer */
52790 0a 20 20 69 6e 74 20 70 72 65 63 69 73 69 6f 6e  .  int precision
527a0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
527b0 20 50 72 65 63 69 73 69 6f 6e 20 6f 66 20 74 68   Precision of th
527c0 65 20 63 75 72 72 65 6e 74 20 66 69 65 6c 64 20  e current field 
527d0 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 67 74 68 3b  */.  int length;
527e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
527f0 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65  /* Length of the
52800 20 66 69 65 6c 64 20 2a 2f 0a 20 20 69 6e 74 20   field */.  int 
52810 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  idx;            
52820 20 20 20 20 20 20 20 2f 2a 20 41 20 67 65 6e 65         /* A gene
52830 72 61 6c 20 70 75 72 70 6f 73 65 20 6c 6f 6f 70  ral purpose loop
52840 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e   counter */.  in
52850 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20 20  t count;        
52860 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61           /* Tota
52870 6c 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72  l number of char
52880 61 63 74 65 72 73 20 6f 75 74 70 75 74 20 2a 2f  acters output */
52890 0a 20 20 69 6e 74 20 77 69 64 74 68 3b 20 20 20  .  int width;   
528a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
528b0 20 57 69 64 74 68 20 6f 66 20 74 68 65 20 63 75   Width of the cu
528c0 72 72 65 6e 74 20 66 69 65 6c 64 20 2a 2f 0a 20  rrent field */. 
528d0 20 65 74 42 79 74 65 20 66 6c 61 67 5f 6c 65 66   etByte flag_lef
528e0 74 6a 75 73 74 69 66 79 3b 20 20 20 2f 2a 20 54  tjustify;   /* T
528f0 72 75 65 20 69 66 20 22 2d 22 20 66 6c 61 67 20  rue if "-" flag 
52900 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20  is present */.  
52910 65 74 42 79 74 65 20 66 6c 61 67 5f 70 6c 75 73  etByte flag_plus
52920 73 69 67 6e 3b 20 20 20 20 20 20 2f 2a 20 54 72  sign;      /* Tr
52930 75 65 20 69 66 20 22 2b 22 20 66 6c 61 67 20 69  ue if "+" flag i
52940 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65  s present */.  e
52950 74 42 79 74 65 20 66 6c 61 67 5f 62 6c 61 6e 6b  tByte flag_blank
52960 73 69 67 6e 3b 20 20 20 20 20 2f 2a 20 54 72 75  sign;     /* Tru
52970 65 20 69 66 20 22 20 22 20 66 6c 61 67 20 69 73  e if " " flag is
52980 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74   present */.  et
52990 42 79 74 65 20 66 6c 61 67 5f 61 6c 74 65 72 6e  Byte flag_altern
529a0 61 74 65 66 6f 72 6d 3b 20 2f 2a 20 54 72 75 65  ateform; /* True
529b0 20 69 66 20 22 23 22 20 66 6c 61 67 20 69 73 20   if "#" flag is 
529c0 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42  present */.  etB
529d0 79 74 65 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d  yte flag_altform
529e0 32 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  2;      /* True 
529f0 69 66 20 22 21 22 20 66 6c 61 67 20 69 73 20 70  if "!" flag is p
52a00 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79  resent */.  etBy
52a10 74 65 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 3b  te flag_zeropad;
52a20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
52a30 66 20 66 69 65 6c 64 20 77 69 64 74 68 20 63 6f  f field width co
52a40 6e 73 74 61 6e 74 20 73 74 61 72 74 73 20 77 69  nstant starts wi
52a50 74 68 20 7a 65 72 6f 20 2a 2f 0a 20 20 65 74 42  th zero */.  etB
52a60 79 74 65 20 66 6c 61 67 5f 6c 6f 6e 67 3b 20 20  yte flag_long;  
52a70 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
52a80 69 66 20 22 6c 22 20 66 6c 61 67 20 69 73 20 70  if "l" flag is p
52a90 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79  resent */.  etBy
52aa0 74 65 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67  te flag_longlong
52ab0 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69  ;      /* True i
52ac0 66 20 74 68 65 20 22 6c 6c 22 20 66 6c 61 67 20  f the "ll" flag 
52ad0 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20  is present */.  
52ae0 65 74 42 79 74 65 20 64 6f 6e 65 3b 20 20 20 20  etByte done;    
52af0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
52b00 6f 70 20 74 65 72 6d 69 6e 61 74 69 6f 6e 20 66  op termination f
52b10 6c 61 67 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f  lag */.  sqlite_
52b20 75 69 6e 74 36 34 20 6c 6f 6e 67 76 61 6c 75 65  uint64 longvalue
52b30 3b 20 20 20 2f 2a 20 56 61 6c 75 65 20 66 6f 72  ;   /* Value for
52b40 20 69 6e 74 65 67 65 72 20 74 79 70 65 73 20 2a   integer types *
52b50 2f 0a 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54  /.  LONGDOUBLE_T
52b60 59 50 45 20 72 65 61 6c 76 61 6c 75 65 3b 20 2f  YPE realvalue; /
52b70 2a 20 56 61 6c 75 65 20 66 6f 72 20 72 65 61 6c  * Value for real
52b80 20 74 79 70 65 73 20 2a 2f 0a 20 20 63 6f 6e 73   types */.  cons
52b90 74 20 65 74 5f 69 6e 66 6f 20 2a 69 6e 66 6f 70  t et_info *infop
52ba0 3b 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65  ;      /* Pointe
52bb0 72 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70 72  r to the appropr
52bc0 69 61 74 65 20 69 6e 66 6f 20 73 74 72 75 63 74  iate info struct
52bd0 75 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 62 75  ure */.  char bu
52be0 66 5b 65 74 42 55 46 53 49 5a 45 5d 3b 20 20 20  f[etBUFSIZE];   
52bf0 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69 6f      /* Conversio
52c00 6e 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 63 68  n buffer */.  ch
52c10 61 72 20 70 72 65 66 69 78 3b 20 20 20 20 20 20  ar prefix;      
52c20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 66           /* Pref
52c30 69 78 20 63 68 61 72 61 63 74 65 72 2e 20 20 22  ix character.  "
52c40 2b 22 20 6f 72 20 22 2d 22 20 6f 72 20 22 20 22  +" or "-" or " "
52c50 20 6f 72 20 27 5c 30 27 2e 20 2a 2f 0a 20 20 65   or '\0'. */.  e
52c60 74 42 79 74 65 20 65 72 72 6f 72 66 6c 61 67 20  tByte errorflag 
52c70 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 54 72 75  = 0;      /* Tru
52c80 65 20 69 66 20 61 6e 20 65 72 72 6f 72 20 69 73  e if an error is
52c90 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 2a 2f 0a   encountered */.
52ca0 20 20 65 74 42 79 74 65 20 78 74 79 70 65 3b 20    etByte xtype; 
52cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
52cc0 43 6f 6e 76 65 72 73 69 6f 6e 20 70 61 72 61 64  Conversion parad
52cd0 69 67 6d 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  igm */.  char *z
52ce0 45 78 74 72 61 3b 20 20 20 20 20 20 20 20 20 20  Extra;          
52cf0 20 20 20 20 2f 2a 20 45 78 74 72 61 20 6d 65 6d      /* Extra mem
52d00 6f 72 79 20 75 73 65 64 20 66 6f 72 20 65 74 54  ory used for etT
52d10 43 4c 45 53 43 41 50 45 20 63 6f 6e 76 65 72 73  CLESCAPE convers
52d20 69 6f 6e 73 20 2a 2f 0a 20 20 73 74 61 74 69 63  ions */.  static
52d30 20 63 6f 6e 73 74 20 63 68 61 72 20 73 70 61 63   const char spac
52d40 65 73 5b 5d 20 3d 0a 20 20 20 22 20 20 20 20 20  es[] =.   "     
52d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52d90 20 20 20 20 22 3b 0a 23 64 65 66 69 6e 65 20 65      ";.#define e
52da0 74 53 50 41 43 45 53 49 5a 45 20 28 73 69 7a 65  tSPACESIZE (size
52db0 6f 66 28 73 70 61 63 65 73 29 2d 31 29 0a 23 69  of(spaces)-1).#i
52dc0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
52dd0 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
52de0 0a 20 20 69 6e 74 20 20 65 78 70 2c 20 65 32 3b  .  int  exp, e2;
52df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
52e00 20 65 78 70 6f 6e 65 6e 74 20 6f 66 20 72 65 61   exponent of rea
52e10 6c 20 6e 75 6d 62 65 72 73 20 2a 2f 0a 20 20 64  l numbers */.  d
52e20 6f 75 62 6c 65 20 72 6f 75 6e 64 65 72 3b 20 20  ouble rounder;  
52e30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65            /* Use
52e40 64 20 66 6f 72 20 72 6f 75 6e 64 69 6e 67 20 66  d for rounding f
52e50 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61  loating point va
52e60 6c 75 65 73 20 2a 2f 0a 20 20 65 74 42 79 74 65  lues */.  etByte
52e70 20 66 6c 61 67 5f 64 70 3b 20 20 20 20 20 20 20   flag_dp;       
52e80 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
52e90 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 73 68  decimal point sh
52ea0 6f 75 6c 64 20 62 65 20 73 68 6f 77 6e 20 2a 2f  ould be shown */
52eb0 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 72  .  etByte flag_r
52ec0 74 7a 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  tz;           /*
52ed0 20 54 72 75 65 20 69 66 20 74 72 61 69 6c 69 6e   True if trailin
52ee0 67 20 7a 65 72 6f 73 20 73 68 6f 75 6c 64 20 62  g zeros should b
52ef0 65 20 72 65 6d 6f 76 65 64 20 2a 2f 0a 20 20 65  e removed */.  e
52f00 74 42 79 74 65 20 66 6c 61 67 5f 65 78 70 3b 20  tByte flag_exp; 
52f10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
52f20 65 20 74 6f 20 66 6f 72 63 65 20 64 69 73 70 6c  e to force displ
52f30 61 79 20 6f 66 20 74 68 65 20 65 78 70 6f 6e 65  ay of the expone
52f40 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 73 64 3b  nt */.  int nsd;
52f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52f60 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
52f70 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69  significant digi
52f80 74 73 20 72 65 74 75 72 6e 65 64 20 2a 2f 0a 23  ts returned */.#
52f90 65 6e 64 69 66 0a 0a 20 20 66 75 6e 63 28 61 72  endif..  func(ar
52fa0 67 2c 22 22 2c 30 29 3b 0a 20 20 63 6f 75 6e 74  g,"",0);.  count
52fb0 20 3d 20 6c 65 6e 67 74 68 20 3d 20 30 3b 0a 20   = length = 0;. 
52fc0 20 62 75 66 70 74 20 3d 20 30 3b 0a 20 20 66 6f   bufpt = 0;.  fo
52fd0 72 28 3b 20 28 63 3d 28 2a 66 6d 74 29 29 21 3d  r(; (c=(*fmt))!=
52fe0 30 3b 20 2b 2b 66 6d 74 29 7b 0a 20 20 20 20 69  0; ++fmt){.    i
52ff0 66 28 20 63 21 3d 27 25 27 20 29 7b 0a 20 20 20  f( c!='%' ){.   
53000 20 20 20 69 6e 74 20 61 6d 74 3b 0a 20 20 20 20     int amt;.    
53010 20 20 62 75 66 70 74 20 3d 20 28 63 68 61 72 20    bufpt = (char 
53020 2a 29 66 6d 74 3b 0a 20 20 20 20 20 20 61 6d 74  *)fmt;.      amt
53030 20 3d 20 31 3b 0a 20 20 20 20 20 20 77 68 69 6c   = 1;.      whil
53040 65 28 20 28 63 3d 28 2a 2b 2b 66 6d 74 29 29 21  e( (c=(*++fmt))!
53050 3d 27 25 27 20 26 26 20 63 21 3d 30 20 29 20 61  ='%' && c!=0 ) a
53060 6d 74 2b 2b 3b 0a 20 20 20 20 20 20 28 2a 66 75  mt++;.      (*fu
53070 6e 63 29 28 61 72 67 2c 62 75 66 70 74 2c 61 6d  nc)(arg,bufpt,am
53080 74 29 3b 0a 20 20 20 20 20 20 63 6f 75 6e 74 20  t);.      count 
53090 2b 3d 20 61 6d 74 3b 0a 20 20 20 20 20 20 69 66  += amt;.      if
530a0 28 20 63 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a  ( c==0 ) break;.
530b0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 63      }.    if( (c
530c0 3d 28 2a 2b 2b 66 6d 74 29 29 3d 3d 30 20 29 7b  =(*++fmt))==0 ){
530d0 0a 20 20 20 20 20 20 65 72 72 6f 72 66 6c 61 67  .      errorflag
530e0 20 3d 20 31 3b 0a 20 20 20 20 20 20 28 2a 66 75   = 1;.      (*fu
530f0 6e 63 29 28 61 72 67 2c 22 25 22 2c 31 29 3b 0a  nc)(arg,"%",1);.
53100 20 20 20 20 20 20 63 6f 75 6e 74 2b 2b 3b 0a 20        count++;. 
53110 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
53120 7d 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 6f 75  }.    /* Find ou
53130 74 20 77 68 61 74 20 66 6c 61 67 73 20 61 72 65  t what flags are
53140 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 20 20   present */.    
53150 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79  flag_leftjustify
53160 20 3d 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e   = flag_plussign
53170 20 3d 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67   = flag_blanksig
53180 6e 20 3d 20 0a 20 20 20 20 20 66 6c 61 67 5f 61  n = .     flag_a
53190 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 3d 20 66  lternateform = f
531a0 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 20 3d 20 66  lag_altform2 = f
531b0 6c 61 67 5f 7a 65 72 6f 70 61 64 20 3d 20 30 3b  lag_zeropad = 0;
531c0 0a 20 20 20 20 64 6f 6e 65 20 3d 20 30 3b 0a 20  .    done = 0;. 
531d0 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 73 77 69     do{.      swi
531e0 74 63 68 28 20 63 20 29 7b 0a 20 20 20 20 20 20  tch( c ){.      
531f0 20 20 63 61 73 65 20 27 2d 27 3a 20 20 20 66 6c    case '-':   fl
53200 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 3d  ag_leftjustify =
53210 20 31 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20   1;     break;. 
53220 20 20 20 20 20 20 20 63 61 73 65 20 27 2b 27 3a         case '+':
53230 20 20 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e     flag_plussign
53240 20 3d 20 31 3b 20 20 20 20 20 20 20 20 62 72 65   = 1;        bre
53250 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65  ak;.        case
53260 20 27 20 27 3a 20 20 20 66 6c 61 67 5f 62 6c 61   ' ':   flag_bla
53270 6e 6b 73 69 67 6e 20 3d 20 31 3b 20 20 20 20 20  nksign = 1;     
53280 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
53290 20 63 61 73 65 20 27 23 27 3a 20 20 20 66 6c 61   case '#':   fla
532a0 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20  g_alternateform 
532b0 3d 20 31 3b 20 20 20 62 72 65 61 6b 3b 0a 20 20  = 1;   break;.  
532c0 20 20 20 20 20 20 63 61 73 65 20 27 21 27 3a 20        case '!': 
532d0 20 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 20    flag_altform2 
532e0 3d 20 31 3b 20 20 20 20 20 20 20 20 62 72 65 61  = 1;        brea
532f0 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  k;.        case 
53300 27 30 27 3a 20 20 20 66 6c 61 67 5f 7a 65 72 6f  '0':   flag_zero
53310 70 61 64 20 3d 20 31 3b 20 20 20 20 20 20 20 20  pad = 1;        
53320 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
53330 64 65 66 61 75 6c 74 3a 20 20 20 20 64 6f 6e 65  default:    done
53340 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 20   = 1;           
53350 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
53360 20 20 20 7d 0a 20 20 20 20 7d 77 68 69 6c 65 28     }.    }while(
53370 20 21 64 6f 6e 65 20 26 26 20 28 63 3d 28 2a 2b   !done && (c=(*+
53380 2b 66 6d 74 29 29 21 3d 30 20 29 3b 0a 20 20 20  +fmt))!=0 );.   
53390 20 2f 2a 20 47 65 74 20 74 68 65 20 66 69 65 6c   /* Get the fiel
533a0 64 20 77 69 64 74 68 20 2a 2f 0a 20 20 20 20 77  d width */.    w
533b0 69 64 74 68 20 3d 20 30 3b 0a 20 20 20 20 69 66  idth = 0;.    if
533c0 28 20 63 3d 3d 27 2a 27 20 29 7b 0a 20 20 20 20  ( c=='*' ){.    
533d0 20 20 77 69 64 74 68 20 3d 20 76 61 5f 61 72 67    width = va_arg
533e0 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20 20  (ap,int);.      
533f0 69 66 28 20 77 69 64 74 68 3c 30 20 29 7b 0a 20  if( width<0 ){. 
53400 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 65 66 74         flag_left
53410 6a 75 73 74 69 66 79 20 3d 20 31 3b 0a 20 20 20  justify = 1;.   
53420 20 20 20 20 20 77 69 64 74 68 20 3d 20 2d 77 69       width = -wi
53430 64 74 68 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  dth;.      }.   
53440 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20     c = *++fmt;. 
53450 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
53460 77 68 69 6c 65 28 20 63 3e 3d 27 30 27 20 26 26  while( c>='0' &&
53470 20 63 3c 3d 27 39 27 20 29 7b 0a 20 20 20 20 20   c<='9' ){.     
53480 20 20 20 77 69 64 74 68 20 3d 20 77 69 64 74 68     width = width
53490 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 3b 0a 20  *10 + c - '0';. 
534a0 20 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d         c = *++fm
534b0 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  t;.      }.    }
534c0 0a 20 20 20 20 69 66 28 20 77 69 64 74 68 20 3e  .    if( width >
534d0 20 65 74 42 55 46 53 49 5a 45 2d 31 30 20 29 7b   etBUFSIZE-10 ){
534e0 0a 20 20 20 20 20 20 77 69 64 74 68 20 3d 20 65  .      width = e
534f0 74 42 55 46 53 49 5a 45 2d 31 30 3b 0a 20 20 20  tBUFSIZE-10;.   
53500 20 7d 0a 20 20 20 20 2f 2a 20 47 65 74 20 74 68   }.    /* Get th
53510 65 20 70 72 65 63 69 73 69 6f 6e 20 2a 2f 0a 20  e precision */. 
53520 20 20 20 69 66 28 20 63 3d 3d 27 2e 27 20 29 7b     if( c=='.' ){
53530 0a 20 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e  .      precision
53540 20 3d 20 30 3b 0a 20 20 20 20 20 20 63 20 3d 20   = 0;.      c = 
53550 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20 69 66  *++fmt;.      if
53560 28 20 63 3d 3d 27 2a 27 20 29 7b 0a 20 20 20 20  ( c=='*' ){.    
53570 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20      precision = 
53580 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a  va_arg(ap,int);.
53590 20 20 20 20 20 20 20 20 69 66 28 20 70 72 65 63          if( prec
535a0 69 73 69 6f 6e 3c 30 20 29 20 70 72 65 63 69 73  ision<0 ) precis
535b0 69 6f 6e 20 3d 20 2d 70 72 65 63 69 73 69 6f 6e  ion = -precision
535c0 3b 0a 20 20 20 20 20 20 20 20 63 20 3d 20 2a 2b  ;.        c = *+
535d0 2b 66 6d 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73  +fmt;.      }els
535e0 65 7b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65  e{.        while
535f0 28 20 63 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27  ( c>='0' && c<='
53600 39 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  9' ){.          
53610 70 72 65 63 69 73 69 6f 6e 20 3d 20 70 72 65 63  precision = prec
53620 69 73 69 6f 6e 2a 31 30 20 2b 20 63 20 2d 20 27  ision*10 + c - '
53630 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20 63 20  0';.          c 
53640 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20  = *++fmt;.      
53650 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
53660 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 72 65  }else{.      pre
53670 63 69 73 69 6f 6e 20 3d 20 2d 31 3b 0a 20 20 20  cision = -1;.   
53680 20 7d 0a 20 20 20 20 2f 2a 20 47 65 74 20 74 68   }.    /* Get th
53690 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70  e conversion typ
536a0 65 20 6d 6f 64 69 66 69 65 72 20 2a 2f 0a 20 20  e modifier */.  
536b0 20 20 69 66 28 20 63 3d 3d 27 6c 27 20 29 7b 0a    if( c=='l' ){.
536c0 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 20        flag_long 
536d0 3d 20 31 3b 0a 20 20 20 20 20 20 63 20 3d 20 2a  = 1;.      c = *
536e0 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20 69 66 28  ++fmt;.      if(
536f0 20 63 3d 3d 27 6c 27 20 29 7b 0a 20 20 20 20 20   c=='l' ){.     
53700 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67     flag_longlong
53710 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 63 20   = 1;.        c 
53720 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20  = *++fmt;.      
53730 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 66  }else{.        f
53740 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 30  lag_longlong = 0
53750 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
53760 6c 73 65 7b 0a 20 20 20 20 20 20 66 6c 61 67 5f  lse{.      flag_
53770 6c 6f 6e 67 20 3d 20 66 6c 61 67 5f 6c 6f 6e 67  long = flag_long
53780 6c 6f 6e 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  long = 0;.    }.
53790 20 20 20 20 2f 2a 20 46 65 74 63 68 20 74 68 65      /* Fetch the
537a0 20 69 6e 66 6f 20 65 6e 74 72 79 20 66 6f 72 20   info entry for 
537b0 74 68 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20 20  the field */.   
537c0 20 69 6e 66 6f 70 20 3d 20 30 3b 0a 20 20 20 20   infop = 0;.    
537d0 66 6f 72 28 69 64 78 3d 30 3b 20 69 64 78 3c 65  for(idx=0; idx<e
537e0 74 4e 49 4e 46 4f 3b 20 69 64 78 2b 2b 29 7b 0a  tNINFO; idx++){.
537f0 20 20 20 20 20 20 69 66 28 20 63 3d 3d 66 6d 74        if( c==fmt
53800 69 6e 66 6f 5b 69 64 78 5d 2e 66 6d 74 74 79 70  info[idx].fmttyp
53810 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 66  e ){.        inf
53820 6f 70 20 3d 20 26 66 6d 74 69 6e 66 6f 5b 69 64  op = &fmtinfo[id
53830 78 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  x];.        if( 
53840 75 73 65 45 78 74 65 6e 64 65 64 20 7c 7c 20 28  useExtended || (
53850 69 6e 66 6f 70 2d 3e 66 6c 61 67 73 20 26 20 46  infop->flags & F
53860 4c 41 47 5f 49 4e 54 45 52 4e 29 3d 3d 30 20 29  LAG_INTERN)==0 )
53870 7b 0a 20 20 20 20 20 20 20 20 20 20 78 74 79 70  {.          xtyp
53880 65 20 3d 20 69 6e 66 6f 70 2d 3e 74 79 70 65 3b  e = infop->type;
53890 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
538a0 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
538b0 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20   -1;.        }. 
538c0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
538d0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
538e0 7a 45 78 74 72 61 20 3d 20 30 3b 0a 20 20 20 20  zExtra = 0;.    
538f0 69 66 28 20 69 6e 66 6f 70 3d 3d 30 20 29 7b 0a  if( infop==0 ){.
53900 20 20 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b        return -1;
53910 0a 20 20 20 20 7d 0a 0a 0a 20 20 20 20 2f 2a 20  .    }...    /* 
53920 4c 69 6d 69 74 20 74 68 65 20 70 72 65 63 69 73  Limit the precis
53930 69 6f 6e 20 74 6f 20 70 72 65 76 65 6e 74 20 6f  ion to prevent o
53940 76 65 72 66 6c 6f 77 69 6e 67 20 62 75 66 5b 5d  verflowing buf[]
53950 20 64 75 72 69 6e 67 20 63 6f 6e 76 65 72 73 69   during conversi
53960 6f 6e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 72  on */.    if( pr
53970 65 63 69 73 69 6f 6e 3e 65 74 42 55 46 53 49 5a  ecision>etBUFSIZ
53980 45 2d 34 30 20 26 26 20 28 69 6e 66 6f 70 2d 3e  E-40 && (infop->
53990 66 6c 61 67 73 20 26 20 46 4c 41 47 5f 53 54 52  flags & FLAG_STR
539a0 49 4e 47 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ING)==0 ){.     
539b0 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 65 74 42   precision = etB
539c0 55 46 53 49 5a 45 2d 34 30 3b 0a 20 20 20 20 7d  UFSIZE-40;.    }
539d0 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20  ..    /*.    ** 
539e0 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 76  At this point, v
539f0 61 72 69 61 62 6c 65 73 20 61 72 65 20 69 6e 69  ariables are ini
53a00 74 69 61 6c 69 7a 65 64 20 61 73 20 66 6f 6c 6c  tialized as foll
53a10 6f 77 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ows:.    **.    
53a20 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74 65 72 6e  **   flag_altern
53a30 61 74 65 66 6f 72 6d 20 20 20 20 20 20 20 20 20  ateform         
53a40 20 54 52 55 45 20 69 66 20 61 20 27 23 27 20 69   TRUE if a '#' i
53a50 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a  s present..    *
53a60 2a 20 20 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d  *   flag_altform
53a70 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  2               
53a80 54 52 55 45 20 69 66 20 61 20 27 21 27 20 69 73  TRUE if a '!' is
53a90 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a   present..    **
53aa0 20 20 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e     flag_plussign
53ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54                 T
53ac0 52 55 45 20 69 66 20 61 20 27 2b 27 20 69 73 20  RUE if a '+' is 
53ad0 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a 20  present..    ** 
53ae0 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69    flag_leftjusti
53af0 66 79 20 20 20 20 20 20 20 20 20 20 20 20 54 52  fy            TR
53b00 55 45 20 69 66 20 61 20 27 2d 27 20 69 73 20 70  UE if a '-' is p
53b10 72 65 73 65 6e 74 20 6f 72 20 69 66 20 74 68 65  resent or if the
53b20 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
53b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53b40 20 20 20 20 20 20 66 69 65 6c 64 20 77 69 64 74        field widt
53b50 68 20 77 61 73 20 6e 65 67 61 74 69 76 65 2e 0a  h was negative..
53b60 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 7a 65      **   flag_ze
53b70 72 6f 70 61 64 20 20 20 20 20 20 20 20 20 20 20  ropad           
53b80 20 20 20 20 20 54 52 55 45 20 69 66 20 74 68 65       TRUE if the
53b90 20 77 69 64 74 68 20 62 65 67 61 6e 20 77 69 74   width began wit
53ba0 68 20 30 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c  h 0..    **   fl
53bb0 61 67 5f 6c 6f 6e 67 20 20 20 20 20 20 20 20 20  ag_long         
53bc0 20 20 20 20 20 20 20 20 20 20 54 52 55 45 20 69            TRUE i
53bd0 66 20 74 68 65 20 6c 65 74 74 65 72 20 27 6c 27  f the letter 'l'
53be0 20 28 65 6c 6c 29 20 70 72 65 66 69 78 65 64 0a   (ell) prefixed.
53bf0 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
53c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53c10 20 20 20 20 20 74 68 65 20 63 6f 6e 76 65 72 73       the convers
53c20 69 6f 6e 20 63 68 61 72 61 63 74 65 72 2e 0a 20  ion character.. 
53c30 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 6c 6f 6e     **   flag_lon
53c40 67 6c 6f 6e 67 20 20 20 20 20 20 20 20 20 20 20  glong           
53c50 20 20 20 20 54 52 55 45 20 69 66 20 74 68 65 20      TRUE if the 
53c60 6c 65 74 74 65 72 20 27 6c 6c 27 20 28 65 6c 6c  letter 'll' (ell
53c70 20 65 6c 6c 29 20 70 72 65 66 69 78 65 64 0a 20   ell) prefixed. 
53c80 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20     **           
53c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53ca0 20 20 20 20 74 68 65 20 63 6f 6e 76 65 72 73 69      the conversi
53cb0 6f 6e 20 63 68 61 72 61 63 74 65 72 2e 0a 20 20  on character..  
53cc0 20 20 2a 2a 20 20 20 66 6c 61 67 5f 62 6c 61 6e    **   flag_blan
53cd0 6b 73 69 67 6e 20 20 20 20 20 20 20 20 20 20 20  ksign           
53ce0 20 20 20 54 52 55 45 20 69 66 20 61 20 27 20 27     TRUE if a ' '
53cf0 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20   is present..   
53d00 20 2a 2a 20 20 20 77 69 64 74 68 20 20 20 20 20   **   width     
53d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53d20 20 20 54 68 65 20 73 70 65 63 69 66 69 65 64 20    The specified 
53d30 66 69 65 6c 64 20 77 69 64 74 68 2e 20 20 54 68  field width.  Th
53d40 69 73 20 69 73 0a 20 20 20 20 2a 2a 20 20 20 20  is is.    **    
53d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53d60 20 20 20 20 20 20 20 20 20 20 20 61 6c 77 61 79             alway
53d70 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 20  s non-negative. 
53d80 20 5a 65 72 6f 20 69 73 20 74 68 65 20 64 65 66   Zero is the def
53d90 61 75 6c 74 2e 0a 20 20 20 20 2a 2a 20 20 20 70  ault..    **   p
53da0 72 65 63 69 73 69 6f 6e 20 20 20 20 20 20 20 20  recision        
53db0 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20 73             The s
53dc0 70 65 63 69 66 69 65 64 20 70 72 65 63 69 73 69  pecified precisi
53dd0 6f 6e 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74  on.  The default
53de0 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
53df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53e00 20 20 20 20 20 20 69 73 20 2d 31 2e 0a 20 20 20        is -1..   
53e10 20 2a 2a 20 20 20 78 74 79 70 65 20 20 20 20 20   **   xtype     
53e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53e30 20 20 54 68 65 20 63 6c 61 73 73 20 6f 66 20 74    The class of t
53e40 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 20  he conversion.. 
53e50 20 20 20 2a 2a 20 20 20 69 6e 66 6f 70 20 20 20     **   infop   
53e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53e70 20 20 20 20 50 6f 69 6e 74 65 72 20 74 6f 20 74      Pointer to t
53e80 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 69  he appropriate i
53e90 6e 66 6f 20 73 74 72 75 63 74 2e 0a 20 20 20 20  nfo struct..    
53ea0 2a 2f 0a 20 20 20 20 73 77 69 74 63 68 28 20 78  */.    switch( x
53eb0 74 79 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61  type ){.      ca
53ec0 73 65 20 65 74 50 4f 49 4e 54 45 52 3a 0a 20 20  se etPOINTER:.  
53ed0 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c        flag_longl
53ee0 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 63 68 61  ong = sizeof(cha
53ef0 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 69 36 34 29  r*)==sizeof(i64)
53f00 3b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c  ;.        flag_l
53f10 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 63 68 61  ong = sizeof(cha
53f20 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 6c 6f 6e 67  r*)==sizeof(long
53f30 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 2f   int);.        /
53f40 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 69  * Fall through i
53f50 6e 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73  nto the next cas
53f60 65 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  e */.      case 
53f70 65 74 52 41 44 49 58 3a 0a 20 20 20 20 20 20 20  etRADIX:.       
53f80 20 69 66 28 20 69 6e 66 6f 70 2d 3e 66 6c 61 67   if( infop->flag
53f90 73 20 26 20 46 4c 41 47 5f 53 49 47 4e 45 44 20  s & FLAG_SIGNED 
53fa0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 36 34  ){.          i64
53fb0 20 76 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66   v;.          if
53fc0 28 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20  ( flag_longlong 
53fd0 29 20 20 20 76 20 3d 20 76 61 5f 61 72 67 28 61  )   v = va_arg(a
53fe0 70 2c 69 36 34 29 3b 0a 20 20 20 20 20 20 20 20  p,i64);.        
53ff0 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67 5f    else if( flag_
54000 6c 6f 6e 67 20 29 20 20 76 20 3d 20 76 61 5f 61  long )  v = va_a
54010 72 67 28 61 70 2c 6c 6f 6e 67 20 69 6e 74 29 3b  rg(ap,long int);
54020 0a 20 20 20 20 20 20 20 20 20 20 65 6c 73 65 20  .          else 
54030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54040 20 76 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69   v = va_arg(ap,i
54050 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  nt);.          i
54060 66 28 20 76 3c 30 20 29 7b 0a 20 20 20 20 20 20  f( v<0 ){.      
54070 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20        longvalue 
54080 3d 20 2d 76 3b 0a 20 20 20 20 20 20 20 20 20 20  = -v;.          
54090 20 20 70 72 65 66 69 78 20 3d 20 27 2d 27 3b 0a    prefix = '-';.
540a0 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
540b0 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f 6e  .            lon
540c0 67 76 61 6c 75 65 20 3d 20 76 3b 0a 20 20 20 20  gvalue = v;.    
540d0 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67          if( flag
540e0 5f 70 6c 75 73 73 69 67 6e 20 29 20 20 20 20 20  _plussign )     
540f0 20 20 20 70 72 65 66 69 78 20 3d 20 27 2b 27 3b     prefix = '+';
54100 0a 20 20 20 20 20 20 20 20 20 20 20 20 65 6c 73  .            els
54110 65 20 69 66 28 20 66 6c 61 67 5f 62 6c 61 6e 6b  e if( flag_blank
54120 73 69 67 6e 20 29 20 20 70 72 65 66 69 78 20 3d  sign )  prefix =
54130 20 27 20 27 3b 0a 20 20 20 20 20 20 20 20 20 20   ' ';.          
54140 20 20 65 6c 73 65 20 20 20 20 20 20 20 20 20 20    else          
54150 20 20 20 20 20 20 20 20 20 20 20 20 20 70 72 65               pre
54160 66 69 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  fix = 0;.       
54170 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c     }.        }el
54180 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  se{.          if
54190 28 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20  ( flag_longlong 
541a0 29 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20  )   longvalue = 
541b0 76 61 5f 61 72 67 28 61 70 2c 75 36 34 29 3b 0a  va_arg(ap,u64);.
541c0 20 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 69            else i
541d0 66 28 20 66 6c 61 67 5f 6c 6f 6e 67 20 29 20 20  f( flag_long )  
541e0 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 76 61 5f 61  longvalue = va_a
541f0 72 67 28 61 70 2c 75 6e 73 69 67 6e 65 64 20 6c  rg(ap,unsigned l
54200 6f 6e 67 20 69 6e 74 29 3b 0a 20 20 20 20 20 20  ong int);.      
54210 20 20 20 20 65 6c 73 65 20 20 20 20 20 20 20 20      else        
54220 20 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61            longva
54230 6c 75 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  lue = va_arg(ap,
54240 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 3b 0a 20  unsigned int);. 
54250 20 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20           prefix 
54260 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = 0;.        }. 
54270 20 20 20 20 20 20 20 69 66 28 20 6c 6f 6e 67 76         if( longv
54280 61 6c 75 65 3d 3d 30 20 29 20 66 6c 61 67 5f 61  alue==0 ) flag_a
54290 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 3d 20 30  lternateform = 0
542a0 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 6c  ;.        if( fl
542b0 61 67 5f 7a 65 72 6f 70 61 64 20 26 26 20 70 72  ag_zeropad && pr
542c0 65 63 69 73 69 6f 6e 3c 77 69 64 74 68 2d 28 70  ecision<width-(p
542d0 72 65 66 69 78 21 3d 30 29 20 29 7b 0a 20 20 20  refix!=0) ){.   
542e0 20 20 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e         precision
542f0 20 3d 20 77 69 64 74 68 2d 28 70 72 65 66 69 78   = width-(prefix
54300 21 3d 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  !=0);.        }.
54310 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20          bufpt = 
54320 26 62 75 66 5b 65 74 42 55 46 53 49 5a 45 2d 31  &buf[etBUFSIZE-1
54330 5d 3b 0a 20 20 20 20 20 20 20 20 7b 0a 20 20 20  ];.        {.   
54340 20 20 20 20 20 20 20 72 65 67 69 73 74 65 72 20         register 
54350 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 73 65 74  const char *cset
54360 3b 20 20 20 20 20 20 2f 2a 20 55 73 65 20 72 65  ;      /* Use re
54370 67 69 73 74 65 72 73 20 66 6f 72 20 73 70 65 65  gisters for spee
54380 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72  d */.          r
54390 65 67 69 73 74 65 72 20 69 6e 74 20 62 61 73 65  egister int base
543a0 3b 0a 20 20 20 20 20 20 20 20 20 20 63 73 65 74  ;.          cset
543b0 20 3d 20 26 61 44 69 67 69 74 73 5b 69 6e 66 6f   = &aDigits[info
543c0 70 2d 3e 63 68 61 72 73 65 74 5d 3b 0a 20 20 20  p->charset];.   
543d0 20 20 20 20 20 20 20 62 61 73 65 20 3d 20 69 6e         base = in
543e0 66 6f 70 2d 3e 62 61 73 65 3b 0a 20 20 20 20 20  fop->base;.     
543f0 20 20 20 20 20 64 6f 7b 20 20 20 20 20 20 20 20       do{        
54400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54420 20 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20 74 6f     /* Convert to
54430 20 61 73 63 69 69 20 2a 2f 0a 20 20 20 20 20 20   ascii */.      
54440 20 20 20 20 20 20 2a 28 2d 2d 62 75 66 70 74 29        *(--bufpt)
54450 20 3d 20 63 73 65 74 5b 6c 6f 6e 67 76 61 6c 75   = cset[longvalu
54460 65 25 62 61 73 65 5d 3b 0a 20 20 20 20 20 20 20  e%base];.       
54470 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d       longvalue =
54480 20 6c 6f 6e 67 76 61 6c 75 65 2f 62 61 73 65 3b   longvalue/base;
54490 0a 20 20 20 20 20 20 20 20 20 20 7d 77 68 69 6c  .          }whil
544a0 65 28 20 6c 6f 6e 67 76 61 6c 75 65 3e 30 20 29  e( longvalue>0 )
544b0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
544c0 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 26 62 75      length = &bu
544d0 66 5b 65 74 42 55 46 53 49 5a 45 2d 31 5d 2d 62  f[etBUFSIZE-1]-b
544e0 75 66 70 74 3b 0a 20 20 20 20 20 20 20 20 66 6f  ufpt;.        fo
544f0 72 28 69 64 78 3d 70 72 65 63 69 73 69 6f 6e 2d  r(idx=precision-
54500 6c 65 6e 67 74 68 3b 20 69 64 78 3e 30 3b 20 69  length; idx>0; i
54510 64 78 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20  dx--){.         
54520 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 27 30   *(--bufpt) = '0
54530 27 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ';              
54540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
54550 2a 20 5a 65 72 6f 20 70 61 64 20 2a 2f 0a 20 20  * Zero pad */.  
54560 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
54570 69 66 28 20 70 72 65 66 69 78 20 29 20 2a 28 2d  if( prefix ) *(-
54580 2d 62 75 66 70 74 29 20 3d 20 70 72 65 66 69 78  -bufpt) = prefix
54590 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
545a0 2f 2a 20 41 64 64 20 73 69 67 6e 20 2a 2f 0a 20  /* Add sign */. 
545b0 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f         if( flag_
545c0 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 26 26  alternateform &&
545d0 20 69 6e 66 6f 70 2d 3e 70 72 65 66 69 78 20 29   infop->prefix )
545e0 7b 20 20 20 20 20 20 2f 2a 20 41 64 64 20 22 30  {      /* Add "0
545f0 22 20 6f 72 20 22 30 78 22 20 2a 2f 0a 20 20 20  " or "0x" */.   
54600 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
54610 72 20 2a 70 72 65 3b 0a 20 20 20 20 20 20 20 20  r *pre;.        
54620 20 20 63 68 61 72 20 78 3b 0a 20 20 20 20 20 20    char x;.      
54630 20 20 20 20 70 72 65 20 3d 20 26 61 50 72 65 66      pre = &aPref
54640 69 78 5b 69 6e 66 6f 70 2d 3e 70 72 65 66 69 78  ix[infop->prefix
54650 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ];.          if(
54660 20 2a 62 75 66 70 74 21 3d 70 72 65 5b 30 5d 20   *bufpt!=pre[0] 
54670 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 66  ){.            f
54680 6f 72 28 3b 20 28 78 3d 28 2a 70 72 65 29 29 21  or(; (x=(*pre))!
54690 3d 30 3b 20 70 72 65 2b 2b 29 20 2a 28 2d 2d 62  =0; pre++) *(--b
546a0 75 66 70 74 29 20 3d 20 78 3b 0a 20 20 20 20 20  ufpt) = x;.     
546b0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
546c0 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20  .        length 
546d0 3d 20 26 62 75 66 5b 65 74 42 55 46 53 49 5a 45  = &buf[etBUFSIZE
546e0 2d 31 5d 2d 62 75 66 70 74 3b 0a 20 20 20 20 20  -1]-bufpt;.     
546f0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
54700 63 61 73 65 20 65 74 46 4c 4f 41 54 3a 0a 20 20  case etFLOAT:.  
54710 20 20 20 20 63 61 73 65 20 65 74 45 58 50 3a 0a      case etEXP:.
54720 20 20 20 20 20 20 63 61 73 65 20 65 74 47 45 4e        case etGEN
54730 45 52 49 43 3a 0a 20 20 20 20 20 20 20 20 72 65  ERIC:.        re
54740 61 6c 76 61 6c 75 65 20 3d 20 76 61 5f 61 72 67  alvalue = va_arg
54750 28 61 70 2c 64 6f 75 62 6c 65 29 3b 0a 23 69 66  (ap,double);.#if
54760 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
54770 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a  _FLOATING_POINT.
54780 20 20 20 20 20 20 20 20 69 66 28 20 70 72 65 63          if( prec
54790 69 73 69 6f 6e 3c 30 20 29 20 70 72 65 63 69 73  ision<0 ) precis
547a0 69 6f 6e 20 3d 20 36 3b 20 20 20 20 20 20 20 20  ion = 6;        
547b0 20 2f 2a 20 53 65 74 20 64 65 66 61 75 6c 74 20   /* Set default 
547c0 70 72 65 63 69 73 69 6f 6e 20 2a 2f 0a 20 20 20  precision */.   
547d0 20 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69       if( precisi
547e0 6f 6e 3e 65 74 42 55 46 53 49 5a 45 2f 32 2d 31  on>etBUFSIZE/2-1
547f0 30 20 29 20 70 72 65 63 69 73 69 6f 6e 20 3d 20  0 ) precision = 
54800 65 74 42 55 46 53 49 5a 45 2f 32 2d 31 30 3b 0a  etBUFSIZE/2-10;.
54810 20 20 20 20 20 20 20 20 69 66 28 20 72 65 61 6c          if( real
54820 76 61 6c 75 65 3c 30 2e 30 20 29 7b 0a 20 20 20  value<0.0 ){.   
54830 20 20 20 20 20 20 20 72 65 61 6c 76 61 6c 75 65         realvalue
54840 20 3d 20 2d 72 65 61 6c 76 61 6c 75 65 3b 0a 20   = -realvalue;. 
54850 20 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20           prefix 
54860 3d 20 27 2d 27 3b 0a 20 20 20 20 20 20 20 20 7d  = '-';.        }
54870 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
54880 69 66 28 20 66 6c 61 67 5f 70 6c 75 73 73 69 67  if( flag_plussig
54890 6e 20 29 20 20 20 20 20 20 20 20 20 20 70 72 65  n )          pre
548a0 66 69 78 20 3d 20 27 2b 27 3b 0a 20 20 20 20 20  fix = '+';.     
548b0 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 66 6c       else if( fl
548c0 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 29 20 20  ag_blanksign )  
548d0 20 20 70 72 65 66 69 78 20 3d 20 27 20 27 3b 0a    prefix = ' ';.
548e0 20 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 20            else  
548f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54900 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d 20         prefix = 
54910 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  0;.        }.   
54920 20 20 20 20 20 69 66 28 20 78 74 79 70 65 3d 3d       if( xtype==
54930 65 74 47 45 4e 45 52 49 43 20 26 26 20 70 72 65  etGENERIC && pre
54940 63 69 73 69 6f 6e 3e 30 20 29 20 70 72 65 63 69  cision>0 ) preci
54950 73 69 6f 6e 2d 2d 3b 0a 23 69 66 20 30 0a 20 20  sion--;.#if 0.  
54960 20 20 20 20 20 20 2f 2a 20 52 6f 75 6e 64 69 6e        /* Roundin
54970 67 20 77 6f 72 6b 73 20 6c 69 6b 65 20 42 53 44  g works like BSD
54980 20 77 68 65 6e 20 74 68 65 20 63 6f 6e 73 74 61   when the consta
54990 6e 74 20 30 2e 34 39 39 39 20 69 73 20 75 73 65  nt 0.4999 is use
549a0 64 2e 20 20 57 69 65 72 64 21 20 2a 2f 0a 20 20  d.  Wierd! */.  
549b0 20 20 20 20 20 20 66 6f 72 28 69 64 78 3d 70 72        for(idx=pr
549c0 65 63 69 73 69 6f 6e 2c 20 72 6f 75 6e 64 65 72  ecision, rounder
549d0 3d 30 2e 34 39 39 39 3b 20 69 64 78 3e 30 3b 20  =0.4999; idx>0; 
549e0 69 64 78 2d 2d 2c 20 72 6f 75 6e 64 65 72 2a 3d  idx--, rounder*=
549f0 30 2e 31 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20  0.1);.#else.    
54a00 20 20 20 20 2f 2a 20 49 74 20 6d 61 6b 65 73 20      /* It makes 
54a10 6d 6f 72 65 20 73 65 6e 73 65 20 74 6f 20 75 73  more sense to us
54a20 65 20 30 2e 35 20 2a 2f 0a 20 20 20 20 20 20 20  e 0.5 */.       
54a30 20 66 6f 72 28 69 64 78 3d 70 72 65 63 69 73 69   for(idx=precisi
54a40 6f 6e 2c 20 72 6f 75 6e 64 65 72 3d 30 2e 35 3b  on, rounder=0.5;
54a50 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d 2c 20 72   idx>0; idx--, r
54a60 6f 75 6e 64 65 72 2a 3d 30 2e 31 29 7b 7d 0a 23  ounder*=0.1){}.#
54a70 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 69 66  endif.        if
54a80 28 20 78 74 79 70 65 3d 3d 65 74 46 4c 4f 41 54  ( xtype==etFLOAT
54a90 20 29 20 72 65 61 6c 76 61 6c 75 65 20 2b 3d 20   ) realvalue += 
54aa0 72 6f 75 6e 64 65 72 3b 0a 20 20 20 20 20 20 20  rounder;.       
54ab0 20 2f 2a 20 4e 6f 72 6d 61 6c 69 7a 65 20 72 65   /* Normalize re
54ac0 61 6c 76 61 6c 75 65 20 74 6f 20 77 69 74 68 69  alvalue to withi
54ad0 6e 20 31 30 2e 30 20 3e 20 72 65 61 6c 76 61 6c  n 10.0 > realval
54ae0 75 65 20 3e 3d 20 31 2e 30 20 2a 2f 0a 20 20 20  ue >= 1.0 */.   
54af0 20 20 20 20 20 65 78 70 20 3d 20 30 3b 0a 20 20       exp = 0;.  
54b00 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
54b10 33 5f 69 73 6e 61 6e 28 72 65 61 6c 76 61 6c 75  3_isnan(realvalu
54b20 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  e) ){.          
54b30 62 75 66 70 74 20 3d 20 22 4e 61 4e 22 3b 0a 20  bufpt = "NaN";. 
54b40 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20           length 
54b50 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 20 20 62  = 3;.          b
54b60 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
54b70 20 20 20 20 20 20 20 20 69 66 28 20 72 65 61 6c          if( real
54b80 76 61 6c 75 65 3e 30 2e 30 20 29 7b 0a 20 20 20  value>0.0 ){.   
54b90 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 72 65         while( re
54ba0 61 6c 76 61 6c 75 65 3e 3d 31 65 33 32 20 26 26  alvalue>=1e32 &&
54bb0 20 65 78 70 3c 3d 33 35 30 20 29 7b 20 72 65 61   exp<=350 ){ rea
54bc0 6c 76 61 6c 75 65 20 2a 3d 20 31 65 2d 33 32 3b  lvalue *= 1e-32;
54bd0 20 65 78 70 2b 3d 33 32 3b 20 7d 0a 20 20 20 20   exp+=32; }.    
54be0 20 20 20 20 20 20 77 68 69 6c 65 28 20 72 65 61        while( rea
54bf0 6c 76 61 6c 75 65 3e 3d 31 65 38 20 26 26 20 65  lvalue>=1e8 && e
54c00 78 70 3c 3d 33 35 30 20 29 7b 20 72 65 61 6c 76  xp<=350 ){ realv
54c10 61 6c 75 65 20 2a 3d 20 31 65 2d 38 3b 20 65 78  alue *= 1e-8; ex
54c20 70 2b 3d 38 3b 20 7d 0a 20 20 20 20 20 20 20 20  p+=8; }.        
54c30 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 6c    while( realval
54c40 75 65 3e 3d 31 30 2e 30 20 26 26 20 65 78 70 3c  ue>=10.0 && exp<
54c50 3d 33 35 30 20 29 7b 20 72 65 61 6c 76 61 6c 75  =350 ){ realvalu
54c60 65 20 2a 3d 20 30 2e 31 3b 20 65 78 70 2b 2b 3b  e *= 0.1; exp++;
54c70 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68 69   }.          whi
54c80 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3c 31 65  le( realvalue<1e
54c90 2d 38 20 26 26 20 65 78 70 3e 3d 2d 33 35 30 20  -8 && exp>=-350 
54ca0 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20  ){ realvalue *= 
54cb0 31 65 38 3b 20 65 78 70 2d 3d 38 3b 20 7d 0a 20  1e8; exp-=8; }. 
54cc0 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20           while( 
54cd0 72 65 61 6c 76 61 6c 75 65 3c 31 2e 30 20 26 26  realvalue<1.0 &&
54ce0 20 65 78 70 3e 3d 2d 33 35 30 20 29 7b 20 72 65   exp>=-350 ){ re
54cf0 61 6c 76 61 6c 75 65 20 2a 3d 20 31 30 2e 30 3b  alvalue *= 10.0;
54d00 20 65 78 70 2d 2d 3b 20 7d 0a 20 20 20 20 20 20   exp--; }.      
54d10 20 20 20 20 69 66 28 20 65 78 70 3e 33 35 30 20      if( exp>350 
54d20 7c 7c 20 65 78 70 3c 2d 33 35 30 20 29 7b 0a 20  || exp<-350 ){. 
54d30 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70             if( p
54d40 72 65 66 69 78 3d 3d 27 2d 27 20 29 7b 0a 20 20  refix=='-' ){.  
54d50 20 20 20 20 20 20 20 20 20 20 20 20 62 75 66 70              bufp
54d60 74 20 3d 20 22 2d 49 6e 66 22 3b 0a 20 20 20 20  t = "-Inf";.    
54d70 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66          }else if
54d80 28 20 70 72 65 66 69 78 3d 3d 27 2b 27 20 29 7b  ( prefix=='+' ){
54d90 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62  .              b
54da0 75 66 70 74 20 3d 20 22 2b 49 6e 66 22 3b 0a 20  ufpt = "+Inf";. 
54db0 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65             }else
54dc0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
54dd0 62 75 66 70 74 20 3d 20 22 49 6e 66 22 3b 0a 20  bufpt = "Inf";. 
54de0 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
54df0 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20           length 
54e00 3d 20 73 74 72 6c 65 6e 28 62 75 66 70 74 29 3b  = strlen(bufpt);
54e10 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65  .            bre
54e20 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ak;.          }.
54e30 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
54e40 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 20    bufpt = buf;. 
54e50 20 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20         /*.      
54e60 20 20 2a 2a 20 49 66 20 74 68 65 20 66 69 65 6c    ** If the fiel
54e70 64 20 74 79 70 65 20 69 73 20 65 74 47 45 4e 45  d type is etGENE
54e80 52 49 43 2c 20 74 68 65 6e 20 63 6f 6e 76 65 72  RIC, then conver
54e90 74 20 74 6f 20 65 69 74 68 65 72 20 65 74 45 58  t to either etEX
54ea0 50 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20  P.        ** or 
54eb0 65 74 46 4c 4f 41 54 2c 20 61 73 20 61 70 70 72  etFLOAT, as appr
54ec0 6f 70 72 69 61 74 65 2e 0a 20 20 20 20 20 20 20  opriate..       
54ed0 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6c 61 67   */.        flag
54ee0 5f 65 78 70 20 3d 20 78 74 79 70 65 3d 3d 65 74  _exp = xtype==et
54ef0 45 58 50 3b 0a 20 20 20 20 20 20 20 20 69 66 28  EXP;.        if(
54f00 20 78 74 79 70 65 21 3d 65 74 46 4c 4f 41 54 20   xtype!=etFLOAT 
54f10 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 61  ){.          rea
54f20 6c 76 61 6c 75 65 20 2b 3d 20 72 6f 75 6e 64 65  lvalue += rounde
54f30 72 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  r;.          if(
54f40 20 72 65 61 6c 76 61 6c 75 65 3e 3d 31 30 2e 30   realvalue>=10.0
54f50 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d   ){ realvalue *=
54f60 20 30 2e 31 3b 20 65 78 70 2b 2b 3b 20 7d 0a 20   0.1; exp++; }. 
54f70 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
54f80 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 47 45   if( xtype==etGE
54f90 4e 45 52 49 43 20 29 7b 0a 20 20 20 20 20 20 20  NERIC ){.       
54fa0 20 20 20 66 6c 61 67 5f 72 74 7a 20 3d 20 21 66     flag_rtz = !f
54fb0 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72  lag_alternatefor
54fc0 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  m;.          if(
54fd0 20 65 78 70 3c 2d 34 20 7c 7c 20 65 78 70 3e 70   exp<-4 || exp>p
54fe0 72 65 63 69 73 69 6f 6e 20 29 7b 0a 20 20 20 20  recision ){.    
54ff0 20 20 20 20 20 20 20 20 78 74 79 70 65 20 3d 20          xtype = 
55000 65 74 45 58 50 3b 0a 20 20 20 20 20 20 20 20 20  etEXP;.         
55010 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
55020 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20      precision = 
55030 70 72 65 63 69 73 69 6f 6e 20 2d 20 65 78 70 3b  precision - exp;
55040 0a 20 20 20 20 20 20 20 20 20 20 20 20 78 74 79  .            xty
55050 70 65 20 3d 20 65 74 46 4c 4f 41 54 3b 0a 20 20  pe = etFLOAT;.  
55060 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
55070 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
55080 20 20 20 66 6c 61 67 5f 72 74 7a 20 3d 20 30 3b     flag_rtz = 0;
55090 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
550a0 20 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74     if( xtype==et
550b0 45 58 50 20 29 7b 0a 20 20 20 20 20 20 20 20 20  EXP ){.         
550c0 20 65 32 20 3d 20 30 3b 0a 20 20 20 20 20 20 20   e2 = 0;.       
550d0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
550e0 20 20 65 32 20 3d 20 65 78 70 3b 0a 20 20 20 20    e2 = exp;.    
550f0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 73      }.        ns
55100 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 66  d = 0;.        f
55110 6c 61 67 5f 64 70 20 3d 20 28 70 72 65 63 69 73  lag_dp = (precis
55120 69 6f 6e 3e 30 29 20 7c 20 66 6c 61 67 5f 61 6c  ion>0) | flag_al
55130 74 65 72 6e 61 74 65 66 6f 72 6d 20 7c 20 66 6c  ternateform | fl
55140 61 67 5f 61 6c 74 66 6f 72 6d 32 3b 0a 20 20 20  ag_altform2;.   
55150 20 20 20 20 20 2f 2a 20 54 68 65 20 73 69 67 6e       /* The sign
55160 20 69 6e 20 66 72 6f 6e 74 20 6f 66 20 74 68 65   in front of the
55170 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 20   number */.     
55180 20 20 20 69 66 28 20 70 72 65 66 69 78 20 29 7b     if( prefix ){
55190 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66  .          *(buf
551a0 70 74 2b 2b 29 20 3d 20 70 72 65 66 69 78 3b 0a  pt++) = prefix;.
551b0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
551c0 20 20 2f 2a 20 44 69 67 69 74 73 20 70 72 69 6f    /* Digits prio
551d0 72 20 74 6f 20 74 68 65 20 64 65 63 69 6d 61 6c  r to the decimal
551e0 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20   point */.      
551f0 20 20 69 66 28 20 65 32 3c 30 20 29 7b 0a 20 20    if( e2<0 ){.  
55200 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b          *(bufpt+
55210 2b 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 20  +) = '0';.      
55220 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
55230 20 20 20 66 6f 72 28 3b 20 65 32 3e 3d 30 3b 20     for(; e2>=0; 
55240 65 32 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20  e2--){.         
55250 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20     *(bufpt++) = 
55260 65 74 5f 67 65 74 64 69 67 69 74 28 26 72 65 61  et_getdigit(&rea
55270 6c 76 61 6c 75 65 2c 26 6e 73 64 29 3b 0a 20 20  lvalue,&nsd);.  
55280 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
55290 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 54    }.        /* T
552a0 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74  he decimal point
552b0 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
552c0 66 6c 61 67 5f 64 70 20 29 7b 0a 20 20 20 20 20  flag_dp ){.     
552d0 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20       *(bufpt++) 
552e0 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 20 20 7d  = '.';.        }
552f0 0a 20 20 20 20 20 20 20 20 2f 2a 20 22 30 22 20  .        /* "0" 
55300 64 69 67 69 74 73 20 61 66 74 65 72 20 74 68 65  digits after the
55310 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 62   decimal point b
55320 75 74 20 62 65 66 6f 72 65 20 74 68 65 20 66 69  ut before the fi
55330 72 73 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 73  rst.        ** s
55340 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74  ignificant digit
55350 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 2a   of the number *
55360 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28 65 32  /.        for(e2
55370 2b 2b 3b 20 65 32 3c 30 20 26 26 20 70 72 65 63  ++; e2<0 && prec
55380 69 73 69 6f 6e 3e 30 3b 20 70 72 65 63 69 73 69  ision>0; precisi
55390 6f 6e 2d 2d 2c 20 65 32 2b 2b 29 7b 0a 20 20 20  on--, e2++){.   
553a0 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b         *(bufpt++
553b0 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 20 20  ) = '0';.       
553c0 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 53 69   }.        /* Si
553d0 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74 73  gnificant digits
553e0 20 61 66 74 65 72 20 74 68 65 20 64 65 63 69 6d   after the decim
553f0 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 20  al point */.    
55400 20 20 20 20 77 68 69 6c 65 28 20 28 70 72 65 63      while( (prec
55410 69 73 69 6f 6e 2d 2d 29 3e 30 20 29 7b 0a 20 20  ision--)>0 ){.  
55420 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b          *(bufpt+
55430 2b 29 20 3d 20 65 74 5f 67 65 74 64 69 67 69 74  +) = et_getdigit
55440 28 26 72 65 61 6c 76 61 6c 75 65 2c 26 6e 73 64  (&realvalue,&nsd
55450 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
55460 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74       /* Remove t
55470 72 61 69 6c 69 6e 67 20 7a 65 72 6f 73 20 61 6e  railing zeros an
55480 64 20 74 68 65 20 22 2e 22 20 69 66 20 6e 6f 20  d the "." if no 
55490 64 69 67 69 74 73 20 66 6f 6c 6c 6f 77 20 74 68  digits follow th
554a0 65 20 22 2e 22 20 2a 2f 0a 20 20 20 20 20 20 20  e "." */.       
554b0 20 69 66 28 20 66 6c 61 67 5f 72 74 7a 20 26 26   if( flag_rtz &&
554c0 20 66 6c 61 67 5f 64 70 20 29 7b 0a 20 20 20 20   flag_dp ){.    
554d0 20 20 20 20 20 20 77 68 69 6c 65 28 20 62 75 66        while( buf
554e0 70 74 5b 2d 31 5d 3d 3d 27 30 27 20 29 20 2a 28  pt[-1]=='0' ) *(
554f0 2d 2d 62 75 66 70 74 29 20 3d 20 30 3b 0a 20 20  --bufpt) = 0;.  
55500 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
55510 62 75 66 70 74 3e 62 75 66 20 29 3b 0a 20 20 20  bufpt>buf );.   
55520 20 20 20 20 20 20 20 69 66 28 20 62 75 66 70 74         if( bufpt
55530 5b 2d 31 5d 3d 3d 27 2e 27 20 29 7b 0a 20 20 20  [-1]=='.' ){.   
55540 20 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61           if( fla
55550 67 5f 61 6c 74 66 6f 72 6d 32 20 29 7b 0a 20 20  g_altform2 ){.  
55560 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 62 75              *(bu
55570 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 20  fpt++) = '0';.  
55580 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
55590 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a  .              *
555a0 28 2d 2d 62 75 66 70 74 29 20 3d 20 30 3b 0a 20  (--bufpt) = 0;. 
555b0 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
555c0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
555d0 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 64   }.        /* Ad
555e0 64 20 74 68 65 20 22 65 4e 4e 4e 22 20 73 75 66  d the "eNNN" suf
555f0 66 69 78 20 2a 2f 0a 20 20 20 20 20 20 20 20 69  fix */.        i
55600 66 28 20 66 6c 61 67 5f 65 78 70 20 7c 7c 20 28  f( flag_exp || (
55610 78 74 79 70 65 3d 3d 65 74 45 58 50 20 26 26 20  xtype==etEXP && 
55620 65 78 70 29 20 29 7b 0a 20 20 20 20 20 20 20 20  exp) ){.        
55630 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 61    *(bufpt++) = a
55640 44 69 67 69 74 73 5b 69 6e 66 6f 70 2d 3e 63 68  Digits[infop->ch
55650 61 72 73 65 74 5d 3b 0a 20 20 20 20 20 20 20 20  arset];.        
55660 20 20 69 66 28 20 65 78 70 3c 30 20 29 7b 0a 20    if( exp<0 ){. 
55670 20 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66             *(buf
55680 70 74 2b 2b 29 20 3d 20 27 2d 27 3b 20 65 78 70  pt++) = '-'; exp
55690 20 3d 20 2d 65 78 70 3b 0a 20 20 20 20 20 20 20   = -exp;.       
556a0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
556b0 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29        *(bufpt++)
556c0 20 3d 20 27 2b 27 3b 0a 20 20 20 20 20 20 20 20   = '+';.        
556d0 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66    }.          if
556e0 28 20 65 78 70 3e 3d 31 30 30 20 29 7b 0a 20 20  ( exp>=100 ){.  
556f0 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70            *(bufp
55700 74 2b 2b 29 20 3d 20 28 65 78 70 2f 31 30 30 29  t++) = (exp/100)
55710 2b 27 30 27 3b 20 20 20 20 20 20 20 20 20 20 20  +'0';           
55720 20 20 20 20 20 2f 2a 20 31 30 30 27 73 20 64 69       /* 100's di
55730 67 69 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  git */.         
55740 20 20 20 65 78 70 20 25 3d 20 31 30 30 3b 0a 20     exp %= 100;. 
55750 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
55760 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20       *(bufpt++) 
55770 3d 20 65 78 70 2f 31 30 2b 27 30 27 3b 20 20 20  = exp/10+'0';   
55780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
55790 20 20 2f 2a 20 31 30 27 73 20 64 69 67 69 74 20    /* 10's digit 
557a0 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62  */.          *(b
557b0 75 66 70 74 2b 2b 29 20 3d 20 65 78 70 25 31 30  ufpt++) = exp%10
557c0 2b 27 30 27 3b 20 20 20 20 20 20 20 20 20 20 20  +'0';           
557d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 27 73            /* 1's
557e0 20 64 69 67 69 74 20 2a 2f 0a 20 20 20 20 20 20   digit */.      
557f0 20 20 7d 0a 20 20 20 20 20 20 20 20 2a 62 75 66    }.        *buf
55800 70 74 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20 20  pt = 0;..       
55810 20 2f 2a 20 54 68 65 20 63 6f 6e 76 65 72 74 65   /* The converte
55820 64 20 6e 75 6d 62 65 72 20 69 73 20 69 6e 20 62  d number is in b
55830 75 66 5b 5d 20 61 6e 64 20 7a 65 72 6f 20 74 65  uf[] and zero te
55840 72 6d 69 6e 61 74 65 64 2e 20 4f 75 74 70 75 74  rminated. Output
55850 20 69 74 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20   it..        ** 
55860 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 6e 75  Note that the nu
55870 6d 62 65 72 20 69 73 20 69 6e 20 74 68 65 20 75  mber is in the u
55880 73 75 61 6c 20 6f 72 64 65 72 2c 20 6e 6f 74 20  sual order, not 
55890 72 65 76 65 72 73 65 64 20 61 73 20 77 69 74 68  reversed as with
558a0 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 74 65  .        ** inte
558b0 67 65 72 20 63 6f 6e 76 65 72 73 69 6f 6e 73 2e  ger conversions.
558c0 20 2a 2f 0a 20 20 20 20 20 20 20 20 6c 65 6e 67   */.        leng
558d0 74 68 20 3d 20 62 75 66 70 74 2d 62 75 66 3b 0a  th = bufpt-buf;.
558e0 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20          bufpt = 
558f0 62 75 66 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a  buf;..        /*
55900 20 53 70 65 63 69 61 6c 20 63 61 73 65 3a 20 20   Special case:  
55910 41 64 64 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f  Add leading zero
55920 73 20 69 66 20 74 68 65 20 66 6c 61 67 5f 7a 65  s if the flag_ze
55930 72 6f 70 61 64 20 66 6c 61 67 20 69 73 0a 20 20  ropad flag is.  
55940 20 20 20 20 20 20 2a 2a 20 73 65 74 20 61 6e 64        ** set and
55950 20 77 65 20 61 72 65 20 6e 6f 74 20 6c 65 66 74   we are not left
55960 20 6a 75 73 74 69 66 69 65 64 20 2a 2f 0a 20 20   justified */.  
55970 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 7a        if( flag_z
55980 65 72 6f 70 61 64 20 26 26 20 21 66 6c 61 67 5f  eropad && !flag_
55990 6c 65 66 74 6a 75 73 74 69 66 79 20 26 26 20 6c  leftjustify && l
559a0 65 6e 67 74 68 20 3c 20 77 69 64 74 68 29 7b 0a  ength < width){.
559b0 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 3b            int i;
559c0 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e  .          int n
559d0 50 61 64 20 3d 20 77 69 64 74 68 20 2d 20 6c 65  Pad = width - le
559e0 6e 67 74 68 3b 0a 20 20 20 20 20 20 20 20 20 20  ngth;.          
559f0 66 6f 72 28 69 3d 77 69 64 74 68 3b 20 69 3e 3d  for(i=width; i>=
55a00 6e 50 61 64 3b 20 69 2d 2d 29 7b 0a 20 20 20 20  nPad; i--){.    
55a10 20 20 20 20 20 20 20 20 62 75 66 70 74 5b 69 5d          bufpt[i]
55a20 20 3d 20 62 75 66 70 74 5b 69 2d 6e 50 61 64 5d   = bufpt[i-nPad]
55a30 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
55a40 20 20 20 20 20 20 20 20 69 20 3d 20 70 72 65 66          i = pref
55a50 69 78 21 3d 30 3b 0a 20 20 20 20 20 20 20 20 20  ix!=0;.         
55a60 20 77 68 69 6c 65 28 20 6e 50 61 64 2d 2d 20 29   while( nPad-- )
55a70 20 62 75 66 70 74 5b 69 2b 2b 5d 20 3d 20 27 30   bufpt[i++] = '0
55a80 27 3b 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e  ';.          len
55a90 67 74 68 20 3d 20 77 69 64 74 68 3b 0a 20 20 20  gth = width;.   
55aa0 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20       }.#endif.  
55ab0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
55ac0 20 20 20 63 61 73 65 20 65 74 53 49 5a 45 3a 0a     case etSIZE:.
55ad0 20 20 20 20 20 20 20 20 2a 28 76 61 5f 61 72 67          *(va_arg
55ae0 28 61 70 2c 69 6e 74 2a 29 29 20 3d 20 63 6f 75  (ap,int*)) = cou
55af0 6e 74 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e 67  nt;.        leng
55b00 74 68 20 3d 20 77 69 64 74 68 20 3d 20 30 3b 0a  th = width = 0;.
55b10 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
55b20 20 20 20 20 20 63 61 73 65 20 65 74 50 45 52 43       case etPERC
55b30 45 4e 54 3a 0a 20 20 20 20 20 20 20 20 62 75 66  ENT:.        buf
55b40 5b 30 5d 20 3d 20 27 25 27 3b 0a 20 20 20 20 20  [0] = '%';.     
55b50 20 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a     bufpt = buf;.
55b60 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d          length =
55b70 20 31 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61   1;.        brea
55b80 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 74  k;.      case et
55b90 43 48 41 52 4c 49 54 3a 0a 20 20 20 20 20 20 63  CHARLIT:.      c
55ba0 61 73 65 20 65 74 43 48 41 52 58 3a 0a 20 20 20  ase etCHARX:.   
55bb0 20 20 20 20 20 63 20 3d 20 62 75 66 5b 30 5d 20       c = buf[0] 
55bc0 3d 20 28 78 74 79 70 65 3d 3d 65 74 43 48 41 52  = (xtype==etCHAR
55bd0 58 20 3f 20 76 61 5f 61 72 67 28 61 70 2c 69 6e  X ? va_arg(ap,in
55be0 74 29 20 3a 20 2a 2b 2b 66 6d 74 29 3b 0a 20 20  t) : *++fmt);.  
55bf0 20 20 20 20 20 20 69 66 28 20 70 72 65 63 69 73        if( precis
55c00 69 6f 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20  ion>=0 ){.      
55c10 20 20 20 20 66 6f 72 28 69 64 78 3d 31 3b 20 69      for(idx=1; i
55c20 64 78 3c 70 72 65 63 69 73 69 6f 6e 3b 20 69 64  dx<precision; id
55c30 78 2b 2b 29 20 62 75 66 5b 69 64 78 5d 20 3d 20  x++) buf[idx] = 
55c40 63 3b 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e  c;.          len
55c50 67 74 68 20 3d 20 70 72 65 63 69 73 69 6f 6e 3b  gth = precision;
55c60 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
55c70 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68            length
55c80 20 3d 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20   =1;.        }. 
55c90 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 62         bufpt = b
55ca0 75 66 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  uf;.        brea
55cb0 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 74  k;.      case et
55cc0 53 54 52 49 4e 47 3a 0a 20 20 20 20 20 20 63 61  STRING:.      ca
55cd0 73 65 20 65 74 44 59 4e 53 54 52 49 4e 47 3a 0a  se etDYNSTRING:.
55ce0 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20          bufpt = 
55cf0 76 61 5f 61 72 67 28 61 70 2c 63 68 61 72 2a 29  va_arg(ap,char*)
55d00 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 62 75  ;.        if( bu
55d10 66 70 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  fpt==0 ){.      
55d20 20 20 20 20 62 75 66 70 74 20 3d 20 22 22 3b 0a      bufpt = "";.
55d30 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66          }else if
55d40 28 20 78 74 79 70 65 3d 3d 65 74 44 59 4e 53 54  ( xtype==etDYNST
55d50 52 49 4e 47 20 29 7b 0a 20 20 20 20 20 20 20 20  RING ){.        
55d60 20 20 7a 45 78 74 72 61 20 3d 20 62 75 66 70 74    zExtra = bufpt
55d70 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
55d80 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 73 74 72      length = str
55d90 6c 65 6e 28 62 75 66 70 74 29 3b 0a 20 20 20 20  len(bufpt);.    
55da0 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69 6f      if( precisio
55db0 6e 3e 3d 30 20 26 26 20 70 72 65 63 69 73 69 6f  n>=0 && precisio
55dc0 6e 3c 6c 65 6e 67 74 68 20 29 20 6c 65 6e 67 74  n<length ) lengt
55dd0 68 20 3d 20 70 72 65 63 69 73 69 6f 6e 3b 0a 20  h = precision;. 
55de0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
55df0 20 20 20 20 63 61 73 65 20 65 74 53 51 4c 45 53      case etSQLES
55e00 43 41 50 45 3a 0a 20 20 20 20 20 20 63 61 73 65  CAPE:.      case
55e10 20 65 74 53 51 4c 45 53 43 41 50 45 32 3a 0a 20   etSQLESCAPE2:. 
55e20 20 20 20 20 20 63 61 73 65 20 65 74 53 51 4c 45       case etSQLE
55e30 53 43 41 50 45 33 3a 20 7b 0a 20 20 20 20 20 20  SCAPE3: {.      
55e40 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6e 2c 20 63    int i, j, n, c
55e50 68 2c 20 69 73 6e 75 6c 6c 3b 0a 20 20 20 20 20  h, isnull;.     
55e60 20 20 20 69 6e 74 20 6e 65 65 64 51 75 6f 74 65     int needQuote
55e70 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 71  ;.        char q
55e80 20 3d 20 28 28 78 74 79 70 65 3d 3d 65 74 53 51   = ((xtype==etSQ
55e90 4c 45 53 43 41 50 45 33 29 3f 27 22 27 3a 27 5c  LESCAPE3)?'"':'\
55ea0 27 27 29 3b 20 20 20 2f 2a 20 51 75 6f 74 65 20  '');   /* Quote 
55eb0 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20 20  character */.   
55ec0 20 20 20 20 20 63 68 61 72 20 2a 65 73 63 61 72       char *escar
55ed0 67 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 63 68  g = va_arg(ap,ch
55ee0 61 72 2a 29 3b 0a 20 20 20 20 20 20 20 20 69 73  ar*);.        is
55ef0 6e 75 6c 6c 20 3d 20 65 73 63 61 72 67 3d 3d 30  null = escarg==0
55f00 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 73  ;.        if( is
55f10 6e 75 6c 6c 20 29 20 65 73 63 61 72 67 20 3d 20  null ) escarg = 
55f20 28 78 74 79 70 65 3d 3d 65 74 53 51 4c 45 53 43  (xtype==etSQLESC
55f30 41 50 45 32 20 3f 20 22 4e 55 4c 4c 22 20 3a 20  APE2 ? "NULL" : 
55f40 22 28 4e 55 4c 4c 29 22 29 3b 0a 20 20 20 20 20  "(NULL)");.     
55f50 20 20 20 66 6f 72 28 69 3d 6e 3d 30 3b 20 28 63     for(i=n=0; (c
55f60 68 3d 65 73 63 61 72 67 5b 69 5d 29 21 3d 30 3b  h=escarg[i])!=0;
55f70 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   i++){.         
55f80 20 69 66 28 20 63 68 3d 3d 71 20 29 20 20 6e 2b   if( ch==q )  n+
55f90 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  +;.        }.   
55fa0 20 20 20 20 20 6e 65 65 64 51 75 6f 74 65 20 3d       needQuote =
55fb0 20 21 69 73 6e 75 6c 6c 20 26 26 20 78 74 79 70   !isnull && xtyp
55fc0 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 45 32 3b  e==etSQLESCAPE2;
55fd0 0a 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 69 20  .        n += i 
55fe0 2b 20 31 20 2b 20 6e 65 65 64 51 75 6f 74 65 2a  + 1 + needQuote*
55ff0 32 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e  2;.        if( n
56000 3e 65 74 42 55 46 53 49 5a 45 20 29 7b 0a 20 20  >etBUFSIZE ){.  
56010 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20          bufpt = 
56020 7a 45 78 74 72 61 20 3d 20 73 71 6c 69 74 65 4d  zExtra = sqliteM
56030 61 6c 6c 6f 63 28 20 6e 20 29 3b 0a 20 20 20 20  alloc( n );.    
56040 20 20 20 20 20 20 69 66 28 20 62 75 66 70 74 3d        if( bufpt=
56050 3d 30 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a  =0 ) return -1;.
56060 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
56070 20 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d           bufpt =
56080 20 62 75 66 3b 0a 20 20 20 20 20 20 20 20 7d 0a   buf;.        }.
56090 20 20 20 20 20 20 20 20 6a 20 3d 20 30 3b 0a 20          j = 0;. 
560a0 20 20 20 20 20 20 20 69 66 28 20 6e 65 65 64 51         if( needQ
560b0 75 6f 74 65 20 29 20 62 75 66 70 74 5b 6a 2b 2b  uote ) bufpt[j++
560c0 5d 20 3d 20 71 3b 0a 20 20 20 20 20 20 20 20 66  ] = q;.        f
560d0 6f 72 28 69 3d 30 3b 20 28 63 68 3d 65 73 63 61  or(i=0; (ch=esca
560e0 72 67 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b  rg[i])!=0; i++){
560f0 0a 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74  .          bufpt
56100 5b 6a 2b 2b 5d 20 3d 20 63 68 3b 0a 20 20 20 20  [j++] = ch;.    
56110 20 20 20 20 20 20 69 66 28 20 63 68 3d 3d 71 20        if( ch==q 
56120 29 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 63  ) bufpt[j++] = c
56130 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  h;.        }.   
56140 20 20 20 20 20 69 66 28 20 6e 65 65 64 51 75 6f       if( needQuo
56150 74 65 20 29 20 62 75 66 70 74 5b 6a 2b 2b 5d 20  te ) bufpt[j++] 
56160 3d 20 71 3b 0a 20 20 20 20 20 20 20 20 62 75 66  = q;.        buf
56170 70 74 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 20  pt[j] = 0;.     
56180 20 20 20 6c 65 6e 67 74 68 20 3d 20 6a 3b 0a 20     length = j;. 
56190 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72         /* The pr
561a0 65 63 69 73 69 6f 6e 20 69 73 20 69 67 6e 6f 72  ecision is ignor
561b0 65 64 20 6f 6e 20 25 71 20 61 6e 64 20 25 51 20  ed on %q and %Q 
561c0 2a 2f 0a 20 20 20 20 20 20 20 20 2f 2a 20 69 66  */.        /* if
561d0 28 20 70 72 65 63 69 73 69 6f 6e 3e 3d 30 20 26  ( precision>=0 &
561e0 26 20 70 72 65 63 69 73 69 6f 6e 3c 6c 65 6e 67  & precision<leng
561f0 74 68 20 29 20 6c 65 6e 67 74 68 20 3d 20 70 72  th ) length = pr
56200 65 63 69 73 69 6f 6e 3b 20 2a 2f 0a 20 20 20 20  ecision; */.    
56210 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
56220 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 65 74   }.      case et
56230 54 4f 4b 45 4e 3a 20 7b 0a 20 20 20 20 20 20 20  TOKEN: {.       
56240 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20 3d   Token *pToken =
56250 20 76 61 5f 61 72 67 28 61 70 2c 20 54 6f 6b 65   va_arg(ap, Toke
56260 6e 2a 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  n*);.        if(
56270 20 70 54 6f 6b 65 6e 20 26 26 20 70 54 6f 6b 65   pToken && pToke
56280 6e 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 20 20 20  n->z ){.        
56290 20 20 28 2a 66 75 6e 63 29 28 61 72 67 2c 20 28    (*func)(arg, (
562a0 63 68 61 72 2a 29 70 54 6f 6b 65 6e 2d 3e 7a 2c  char*)pToken->z,
562b0 20 70 54 6f 6b 65 6e 2d 3e 6e 29 3b 0a 20 20 20   pToken->n);.   
562c0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6c       }.        l
562d0 65 6e 67 74 68 20 3d 20 77 69 64 74 68 20 3d 20  ength = width = 
562e0 30 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  0;.        break
562f0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
56300 63 61 73 65 20 65 74 53 52 43 4c 49 53 54 3a 20  case etSRCLIST: 
56310 7b 0a 20 20 20 20 20 20 20 20 53 72 63 4c 69 73  {.        SrcLis
56320 74 20 2a 70 53 72 63 20 3d 20 76 61 5f 61 72 67  t *pSrc = va_arg
56330 28 61 70 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a  (ap, SrcList*);.
56340 20 20 20 20 20 20 20 20 69 6e 74 20 6b 20 3d 20          int k = 
56350 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b  va_arg(ap, int);
56360 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20  .        struct 
56370 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  SrcList_item *pI
56380 74 65 6d 20 3d 20 26 70 53 72 63 2d 3e 61 5b 6b  tem = &pSrc->a[k
56390 5d 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ];.        asser
563a0 74 28 20 6b 3e 3d 30 20 26 26 20 6b 3c 70 53 72  t( k>=0 && k<pSr
563b0 63 2d 3e 6e 53 72 63 20 29 3b 0a 20 20 20 20 20  c->nSrc );.     
563c0 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 44     if( pItem->zD
563d0 61 74 61 62 61 73 65 20 26 26 20 70 49 74 65 6d  atabase && pItem
563e0 2d 3e 7a 44 61 74 61 62 61 73 65 5b 30 5d 20 29  ->zDatabase[0] )
563f0 7b 0a 20 20 20 20 20 20 20 20 20 20 28 2a 66 75  {.          (*fu
56400 6e 63 29 28 61 72 67 2c 20 70 49 74 65 6d 2d 3e  nc)(arg, pItem->
56410 7a 44 61 74 61 62 61 73 65 2c 20 73 74 72 6c 65  zDatabase, strle
56420 6e 28 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61  n(pItem->zDataba
56430 73 65 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20  se));.          
56440 28 2a 66 75 6e 63 29 28 61 72 67 2c 20 22 2e 22  (*func)(arg, "."
56450 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  , 1);.        }.
56460 20 20 20 20 20 20 20 20 28 2a 66 75 6e 63 29 28          (*func)(
56470 61 72 67 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d  arg, pItem->zNam
56480 65 2c 20 73 74 72 6c 65 6e 28 70 49 74 65 6d 2d  e, strlen(pItem-
56490 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 20 20  >zName));.      
564a0 20 20 6c 65 6e 67 74 68 20 3d 20 77 69 64 74 68    length = width
564b0 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62 72   = 0;.        br
564c0 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
564d0 20 7d 2f 2a 20 45 6e 64 20 73 77 69 74 63 68 20   }/* End switch 
564e0 6f 76 65 72 20 74 68 65 20 66 6f 72 6d 61 74 20  over the format 
564f0 74 79 70 65 20 2a 2f 0a 20 20 20 20 2f 2a 0a 20  type */.    /*. 
56500 20 20 20 2a 2a 20 54 68 65 20 74 65 78 74 20 6f     ** The text o
56510 66 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e  f the conversion
56520 20 69 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62   is pointed to b
56530 79 20 22 62 75 66 70 74 22 20 61 6e 64 20 69 73  y "bufpt" and is
56540 0a 20 20 20 20 2a 2a 20 22 6c 65 6e 67 74 68 22  .    ** "length"
56550 20 63 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 67   characters long
56560 2e 20 20 54 68 65 20 66 69 65 6c 64 20 77 69 64  .  The field wid
56570 74 68 20 69 73 20 22 77 69 64 74 68 22 2e 20 20  th is "width".  
56580 44 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 6f 75  Do.    ** the ou
56590 74 70 75 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  tput..    */.   
565a0 20 69 66 28 20 21 66 6c 61 67 5f 6c 65 66 74 6a   if( !flag_leftj
565b0 75 73 74 69 66 79 20 29 7b 0a 20 20 20 20 20 20  ustify ){.      
565c0 72 65 67 69 73 74 65 72 20 69 6e 74 20 6e 73 70  register int nsp
565d0 61 63 65 3b 0a 20 20 20 20 20 20 6e 73 70 61 63  ace;.      nspac
565e0 65 20 3d 20 77 69 64 74 68 2d 6c 65 6e 67 74 68  e = width-length
565f0 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 73 70 61  ;.      if( nspa
56600 63 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20  ce>0 ){.        
56610 63 6f 75 6e 74 20 2b 3d 20 6e 73 70 61 63 65 3b  count += nspace;
56620 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
56630 6e 73 70 61 63 65 3e 3d 65 74 53 50 41 43 45 53  nspace>=etSPACES
56640 49 5a 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20  IZE ){.         
56650 20 28 2a 66 75 6e 63 29 28 61 72 67 2c 73 70 61   (*func)(arg,spa
56660 63 65 73 2c 65 74 53 50 41 43 45 53 49 5a 45 29  ces,etSPACESIZE)
56670 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 73 70 61  ;.          nspa
56680 63 65 20 2d 3d 20 65 74 53 50 41 43 45 53 49 5a  ce -= etSPACESIZ
56690 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  E;.        }.   
566a0 20 20 20 20 20 69 66 28 20 6e 73 70 61 63 65 3e       if( nspace>
566b0 30 20 29 20 28 2a 66 75 6e 63 29 28 61 72 67 2c  0 ) (*func)(arg,
566c0 73 70 61 63 65 73 2c 6e 73 70 61 63 65 29 3b 0a  spaces,nspace);.
566d0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
566e0 20 20 69 66 28 20 6c 65 6e 67 74 68 3e 30 20 29    if( length>0 )
566f0 7b 0a 20 20 20 20 20 20 28 2a 66 75 6e 63 29 28  {.      (*func)(
56700 61 72 67 2c 62 75 66 70 74 2c 6c 65 6e 67 74 68  arg,bufpt,length
56710 29 3b 0a 20 20 20 20 20 20 63 6f 75 6e 74 20 2b  );.      count +
56720 3d 20 6c 65 6e 67 74 68 3b 0a 20 20 20 20 7d 0a  = length;.    }.
56730 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 65 66      if( flag_lef
56740 74 6a 75 73 74 69 66 79 20 29 7b 0a 20 20 20 20  tjustify ){.    
56750 20 20 72 65 67 69 73 74 65 72 20 69 6e 74 20 6e    register int n
56760 73 70 61 63 65 3b 0a 20 20 20 20 20 20 6e 73 70  space;.      nsp
56770 61 63 65 20 3d 20 77 69 64 74 68 2d 6c 65 6e 67  ace = width-leng
56780 74 68 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 73  th;.      if( ns
56790 70 61 63 65 3e 30 20 29 7b 0a 20 20 20 20 20 20  pace>0 ){.      
567a0 20 20 63 6f 75 6e 74 20 2b 3d 20 6e 73 70 61 63    count += nspac
567b0 65 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65  e;.        while
567c0 28 20 6e 73 70 61 63 65 3e 3d 65 74 53 50 41 43  ( nspace>=etSPAC
567d0 45 53 49 5a 45 20 29 7b 0a 20 20 20 20 20 20 20  ESIZE ){.       
567e0 20 20 20 28 2a 66 75 6e 63 29 28 61 72 67 2c 73     (*func)(arg,s
567f0 70 61 63 65 73 2c 65 74 53 50 41 43 45 53 49 5a  paces,etSPACESIZ
56800 45 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 73  E);.          ns
56810 70 61 63 65 20 2d 3d 20 65 74 53 50 41 43 45 53  pace -= etSPACES
56820 49 5a 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  IZE;.        }. 
56830 20 20 20 20 20 20 20 69 66 28 20 6e 73 70 61 63         if( nspac
56840 65 3e 30 20 29 20 28 2a 66 75 6e 63 29 28 61 72  e>0 ) (*func)(ar
56850 67 2c 73 70 61 63 65 73 2c 6e 73 70 61 63 65 29  g,spaces,nspace)
56860 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
56870 20 20 20 20 69 66 28 20 7a 45 78 74 72 61 20 29      if( zExtra )
56880 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 46 72  {.      sqliteFr
56890 65 65 28 7a 45 78 74 72 61 29 3b 0a 20 20 20 20  ee(zExtra);.    
568a0 7d 0a 20 20 7d 2f 2a 20 45 6e 64 20 66 6f 72 20  }.  }/* End for 
568b0 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 66 6f  loop over the fo
568c0 72 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20  rmat string */. 
568d0 20 72 65 74 75 72 6e 20 65 72 72 6f 72 66 6c 61   return errorfla
568e0 67 20 3f 20 2d 31 20 3a 20 63 6f 75 6e 74 3b 0a  g ? -1 : count;.
568f0 7d 20 2f 2a 20 45 6e 64 20 6f 66 20 66 75 6e 63  } /* End of func
56900 74 69 6f 6e 20 2a 2f 0a 0a 0a 2f 2a 20 54 68 69  tion */.../* Thi
56910 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 75  s structure is u
56920 73 65 64 20 74 6f 20 73 74 6f 72 65 20 73 74 61  sed to store sta
56930 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  te information a
56940 62 6f 75 74 20 74 68 65 0a 2a 2a 20 77 72 69 74  bout the.** writ
56950 65 20 74 6f 20 6d 65 6d 6f 72 79 20 74 68 61 74  e to memory that
56960 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e   is currently in
56970 20 70 72 6f 67 72 65 73 73 2e 0a 2a 2f 0a 73 74   progress..*/.st
56980 72 75 63 74 20 73 67 4d 70 72 69 6e 74 66 20 7b  ruct sgMprintf {
56990 0a 20 20 63 68 61 72 20 2a 7a 42 61 73 65 3b 20  .  char *zBase; 
569a0 20 20 20 20 2f 2a 20 41 20 62 61 73 65 20 61 6c      /* A base al
569b0 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 63 68  location */.  ch
569c0 61 72 20 2a 7a 54 65 78 74 3b 20 20 20 20 20 2f  ar *zText;     /
569d0 2a 20 54 68 65 20 73 74 72 69 6e 67 20 63 6f 6c  * The string col
569e0 6c 65 63 74 65 64 20 73 6f 20 66 61 72 20 2a 2f  lected so far */
569f0 0a 20 20 69 6e 74 20 20 6e 43 68 61 72 3b 20 20  .  int  nChar;  
56a00 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66      /* Length of
56a10 20 74 68 65 20 73 74 72 69 6e 67 20 73 6f 20 66   the string so f
56a20 61 72 20 2a 2f 0a 20 20 69 6e 74 20 20 6e 54 6f  ar */.  int  nTo
56a30 74 61 6c 3b 20 20 20 20 20 2f 2a 20 4f 75 74 70  tal;     /* Outp
56a40 75 74 20 73 69 7a 65 20 69 66 20 75 6e 63 6f 6e  ut size if uncon
56a50 73 74 72 61 69 6e 65 64 20 2a 2f 0a 20 20 69 6e  strained */.  in
56a60 74 20 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 2f  t  nAlloc;     /
56a70 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 73 70 61 63  * Amount of spac
56a80 65 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 7a  e allocated in z
56a90 54 65 78 74 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  Text */.  void *
56aa0 28 2a 78 52 65 61 6c 6c 6f 63 29 28 76 6f 69 64  (*xRealloc)(void
56ab0 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 46 75 6e 63  *,int);  /* Func
56ac0 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 72 65 61  tion used to rea
56ad0 6c 6c 6f 63 20 6d 65 6d 6f 72 79 20 2a 2f 0a 7d  lloc memory */.}
56ae0 3b 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 66  ;../* .** This f
56af0 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e  unction implemen
56b00 74 73 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  ts the callback 
56b10 66 72 6f 6d 20 76 78 70 72 69 6e 74 66 2e 20 0a  from vxprintf. .
56b20 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
56b30 6e 65 20 61 64 64 20 6e 4e 65 77 43 68 61 72 20  ne add nNewChar 
56b40 63 68 61 72 61 63 74 65 72 73 20 6f 66 20 74 65  characters of te
56b50 78 74 20 69 6e 20 7a 4e 65 77 54 65 78 74 20 74  xt in zNewText t
56b60 6f 0a 2a 2a 20 74 68 65 20 73 67 4d 70 72 69 6e  o.** the sgMprin
56b70 74 66 20 73 74 72 75 63 74 75 72 65 20 70 6f 69  tf structure poi
56b80 6e 74 65 64 20 74 6f 20 62 79 20 22 61 72 67 22  nted to by "arg"
56b90 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
56ba0 20 6d 6f 75 74 28 76 6f 69 64 20 2a 61 72 67 2c   mout(void *arg,
56bb0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 65   const char *zNe
56bc0 77 54 65 78 74 2c 20 69 6e 74 20 6e 4e 65 77 43  wText, int nNewC
56bd0 68 61 72 29 7b 0a 20 20 73 74 72 75 63 74 20 73  har){.  struct s
56be0 67 4d 70 72 69 6e 74 66 20 2a 70 4d 20 3d 20 28  gMprintf *pM = (
56bf0 73 74 72 75 63 74 20 73 67 4d 70 72 69 6e 74 66  struct sgMprintf
56c00 2a 29 61 72 67 3b 0a 20 20 70 4d 2d 3e 6e 54 6f  *)arg;.  pM->nTo
56c10 74 61 6c 20 2b 3d 20 6e 4e 65 77 43 68 61 72 3b  tal += nNewChar;
56c20 0a 20 20 69 66 28 20 70 4d 2d 3e 6e 43 68 61 72  .  if( pM->nChar
56c30 20 2b 20 6e 4e 65 77 43 68 61 72 20 2b 20 31 20   + nNewChar + 1 
56c40 3e 20 70 4d 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a  > pM->nAlloc ){.
56c50 20 20 20 20 69 66 28 20 70 4d 2d 3e 78 52 65 61      if( pM->xRea
56c60 6c 6c 6f 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20  lloc==0 ){.     
56c70 20 6e 4e 65 77 43 68 61 72 20 3d 20 20 70 4d 2d   nNewChar =  pM-
56c80 3e 6e 41 6c 6c 6f 63 20 2d 20 70 4d 2d 3e 6e 43  >nAlloc - pM->nC
56c90 68 61 72 20 2d 20 31 3b 0a 20 20 20 20 7d 65 6c  har - 1;.    }el
56ca0 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 41  se{.      int nA
56cb0 6c 6c 6f 63 20 3d 20 70 4d 2d 3e 6e 43 68 61 72  lloc = pM->nChar
56cc0 20 2b 20 6e 4e 65 77 43 68 61 72 2a 32 20 2b 20   + nNewChar*2 + 
56cd0 31 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4d 2d  1;.      if( pM-
56ce0 3e 7a 54 65 78 74 3d 3d 70 4d 2d 3e 7a 42 61 73  >zText==pM->zBas
56cf0 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4d 2d  e ){.        pM-
56d00 3e 7a 54 65 78 74 20 3d 20 70 4d 2d 3e 78 52 65  >zText = pM->xRe
56d10 61 6c 6c 6f 63 28 30 2c 20 6e 41 6c 6c 6f 63 29  alloc(0, nAlloc)
56d20 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4d  ;.        if( pM
56d30 2d 3e 7a 54 65 78 74 20 26 26 20 70 4d 2d 3e 6e  ->zText && pM->n
56d40 43 68 61 72 20 29 7b 0a 20 20 20 20 20 20 20 20  Char ){.        
56d50 20 20 6d 65 6d 63 70 79 28 70 4d 2d 3e 7a 54 65    memcpy(pM->zTe
56d60 78 74 2c 20 70 4d 2d 3e 7a 42 61 73 65 2c 20 70  xt, pM->zBase, p
56d70 4d 2d 3e 6e 43 68 61 72 29 3b 0a 20 20 20 20 20  M->nChar);.     
56d80 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
56d90 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a  {.        char *
56da0 7a 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 7a 4e  zNew;.        zN
56db0 65 77 20 3d 20 70 4d 2d 3e 78 52 65 61 6c 6c 6f  ew = pM->xReallo
56dc0 63 28 70 4d 2d 3e 7a 54 65 78 74 2c 20 6e 41 6c  c(pM->zText, nAl
56dd0 6c 6f 63 29 3b 0a 20 20 20 20 20 20 20 20 69 66  loc);.        if
56de0 28 20 7a 4e 65 77 20 29 7b 0a 20 20 20 20 20 20  ( zNew ){.      
56df0 20 20 20 20 70 4d 2d 3e 7a 54 65 78 74 20 3d 20      pM->zText = 
56e00 7a 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 7d 65  zNew;.        }e
56e10 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 72  lse{.          r
56e20 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d  eturn;.        }
56e30 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
56e40 4d 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 6e 41 6c 6c  M->nAlloc = nAll
56e50 6f 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  oc;.    }.  }.  
56e60 69 66 28 20 70 4d 2d 3e 7a 54 65 78 74 20 29 7b  if( pM->zText ){
56e70 0a 20 20 20 20 69 66 28 20 6e 4e 65 77 43 68 61  .    if( nNewCha
56e80 72 3e 30 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d  r>0 ){.      mem
56e90 63 70 79 28 26 70 4d 2d 3e 7a 54 65 78 74 5b 70  cpy(&pM->zText[p
56ea0 4d 2d 3e 6e 43 68 61 72 5d 2c 20 7a 4e 65 77 54  M->nChar], zNewT
56eb0 65 78 74 2c 20 6e 4e 65 77 43 68 61 72 29 3b 0a  ext, nNewChar);.
56ec0 20 20 20 20 20 20 70 4d 2d 3e 6e 43 68 61 72 20        pM->nChar 
56ed0 2b 3d 20 6e 4e 65 77 43 68 61 72 3b 0a 20 20 20  += nNewChar;.   
56ee0 20 7d 0a 20 20 20 20 70 4d 2d 3e 7a 54 65 78 74   }.    pM->zText
56ef0 5b 70 4d 2d 3e 6e 43 68 61 72 5d 20 3d 20 30 3b  [pM->nChar] = 0;
56f00 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
56f10 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
56f20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 78  wrapper around x
56f30 70 72 69 6e 74 66 28 29 20 74 68 61 74 20 69 6e  printf() that in
56f40 76 6f 6b 65 73 20 6d 6f 75 74 28 29 20 61 73 0a  vokes mout() as.
56f50 2a 2a 20 74 68 65 20 63 6f 6e 73 75 6d 65 72 2e  ** the consumer.
56f60 20 20 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61    .*/.static cha
56f70 72 20 2a 62 61 73 65 5f 76 70 72 69 6e 74 66 28  r *base_vprintf(
56f80 0a 20 20 76 6f 69 64 20 2a 28 2a 78 52 65 61 6c  .  void *(*xReal
56f90 6c 6f 63 29 28 76 6f 69 64 2a 2c 69 6e 74 29 2c  loc)(void*,int),
56fa0 20 20 20 2f 2a 20 52 6f 75 74 69 6e 65 20 74 6f     /* Routine to
56fb0 20 72 65 61 6c 6c 6f 63 20 6d 65 6d 6f 72 79 2e   realloc memory.
56fc0 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a   May be NULL */.
56fd0 20 20 69 6e 74 20 75 73 65 49 6e 74 65 72 6e 61    int useInterna
56fe0 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l,              
56ff0 20 20 2f 2a 20 55 73 65 20 69 6e 74 65 72 6e 61    /* Use interna
57000 6c 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 73 20  l %-conversions 
57010 69 66 20 74 72 75 65 20 2a 2f 0a 20 20 63 68 61  if true */.  cha
57020 72 20 2a 7a 49 6e 69 74 42 75 66 2c 20 20 20 20  r *zInitBuf,    
57030 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
57040 49 6e 69 74 69 61 6c 6c 79 20 77 72 69 74 65 20  Initially write 
57050 68 65 72 65 2c 20 62 65 66 6f 72 65 20 6d 61 6c  here, before mal
57060 6c 6f 63 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20  locing */.  int 
57070 6e 49 6e 69 74 42 75 66 2c 20 20 20 20 20 20 20  nInitBuf,       
57080 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
57090 69 7a 65 20 6f 66 20 7a 49 6e 69 74 42 75 66 5b  ize of zInitBuf[
570a0 5d 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  ] */.  const cha
570b0 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 20 20 20 20  r *zFormat,     
570c0 20 20 20 20 20 20 20 2f 2a 20 66 6f 72 6d 61 74         /* format
570d0 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 76 61 5f   string */.  va_
570e0 6c 69 73 74 20 61 70 20 20 20 20 20 20 20 20 20  list ap         
570f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
57100 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 29 7b 0a  arguments */.){.
57110 20 20 73 74 72 75 63 74 20 73 67 4d 70 72 69 6e    struct sgMprin
57120 74 66 20 73 4d 3b 0a 20 20 73 4d 2e 7a 42 61 73  tf sM;.  sM.zBas
57130 65 20 3d 20 73 4d 2e 7a 54 65 78 74 20 3d 20 7a  e = sM.zText = z
57140 49 6e 69 74 42 75 66 3b 0a 20 20 73 4d 2e 6e 43  InitBuf;.  sM.nC
57150 68 61 72 20 3d 20 73 4d 2e 6e 54 6f 74 61 6c 20  har = sM.nTotal 
57160 3d 20 30 3b 0a 20 20 73 4d 2e 6e 41 6c 6c 6f 63  = 0;.  sM.nAlloc
57170 20 3d 20 6e 49 6e 69 74 42 75 66 3b 0a 20 20 73   = nInitBuf;.  s
57180 4d 2e 78 52 65 61 6c 6c 6f 63 20 3d 20 78 52 65  M.xRealloc = xRe
57190 61 6c 6c 6f 63 3b 0a 20 20 76 78 70 72 69 6e 74  alloc;.  vxprint
571a0 66 28 6d 6f 75 74 2c 20 26 73 4d 2c 20 75 73 65  f(mout, &sM, use
571b0 49 6e 74 65 72 6e 61 6c 2c 20 7a 46 6f 72 6d 61  Internal, zForma
571c0 74 2c 20 61 70 29 3b 0a 20 20 69 66 28 20 78 52  t, ap);.  if( xR
571d0 65 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 69 66  ealloc ){.    if
571e0 28 20 73 4d 2e 7a 54 65 78 74 3d 3d 73 4d 2e 7a  ( sM.zText==sM.z
571f0 42 61 73 65 20 29 7b 0a 20 20 20 20 20 20 73 4d  Base ){.      sM
57200 2e 7a 54 65 78 74 20 3d 20 78 52 65 61 6c 6c 6f  .zText = xReallo
57210 63 28 30 2c 20 73 4d 2e 6e 43 68 61 72 2b 31 29  c(0, sM.nChar+1)
57220 3b 0a 20 20 20 20 20 20 69 66 28 20 73 4d 2e 7a  ;.      if( sM.z
57230 54 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Text ){.        
57240 6d 65 6d 63 70 79 28 73 4d 2e 7a 54 65 78 74 2c  memcpy(sM.zText,
57250 20 73 4d 2e 7a 42 61 73 65 2c 20 73 4d 2e 6e 43   sM.zBase, sM.nC
57260 68 61 72 2b 31 29 3b 0a 20 20 20 20 20 20 7d 0a  har+1);.      }.
57270 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 4d      }else if( sM
57280 2e 6e 41 6c 6c 6f 63 3e 73 4d 2e 6e 43 68 61 72  .nAlloc>sM.nChar
57290 2b 31 30 20 29 7b 0a 20 20 20 20 20 20 63 68 61  +10 ){.      cha
572a0 72 20 2a 7a 4e 65 77 20 3d 20 78 52 65 61 6c 6c  r *zNew = xReall
572b0 6f 63 28 73 4d 2e 7a 54 65 78 74 2c 20 73 4d 2e  oc(sM.zText, sM.
572c0 6e 43 68 61 72 2b 31 29 3b 0a 20 20 20 20 20 20  nChar+1);.      
572d0 69 66 28 20 7a 4e 65 77 20 29 7b 0a 20 20 20 20  if( zNew ){.    
572e0 20 20 20 20 73 4d 2e 7a 54 65 78 74 20 3d 20 7a      sM.zText = z
572f0 4e 65 77 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  New;.      }.   
57300 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
57310 73 4d 2e 7a 54 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a  sM.zText;.}../*.
57320 2a 2a 20 52 65 61 6c 6c 6f 63 20 74 68 61 74 20  ** Realloc that 
57330 69 73 20 61 20 72 65 61 6c 20 66 75 6e 63 74 69  is a real functi
57340 6f 6e 2c 20 6e 6f 74 20 61 20 6d 61 63 72 6f 2e  on, not a macro.
57350 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
57360 2a 70 72 69 6e 74 66 5f 72 65 61 6c 6c 6f 63 28  *printf_realloc(
57370 76 6f 69 64 20 2a 6f 6c 64 2c 20 69 6e 74 20 73  void *old, int s
57380 69 7a 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 73  ize){.  return s
57390 71 6c 69 74 65 52 65 61 6c 6c 6f 63 28 6f 6c 64  qliteRealloc(old
573a0 2c 73 69 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ,size);.}../*.**
573b0 20 50 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f   Print into memo
573c0 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ry obtained from
573d0 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e   sqliteMalloc().
573e0 20 20 55 73 65 20 74 68 65 20 69 6e 74 65 72 6e    Use the intern
573f0 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69  al.** %-conversi
57400 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a  on extensions..*
57410 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
57420 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 56 4d   char *sqlite3VM
57430 50 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61  Printf(const cha
57440 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c  r *zFormat, va_l
57450 69 73 74 20 61 70 29 7b 0a 20 20 63 68 61 72 20  ist ap){.  char 
57460 7a 42 61 73 65 5b 53 51 4c 49 54 45 5f 50 52 49  zBase[SQLITE_PRI
57470 4e 54 5f 42 55 46 5f 53 49 5a 45 5d 3b 0a 20 20  NT_BUF_SIZE];.  
57480 72 65 74 75 72 6e 20 62 61 73 65 5f 76 70 72 69  return base_vpri
57490 6e 74 66 28 70 72 69 6e 74 66 5f 72 65 61 6c 6c  ntf(printf_reall
574a0 6f 63 2c 20 31 2c 20 7a 42 61 73 65 2c 20 73 69  oc, 1, zBase, si
574b0 7a 65 6f 66 28 7a 42 61 73 65 29 2c 20 7a 46 6f  zeof(zBase), zFo
574c0 72 6d 61 74 2c 20 61 70 29 3b 0a 7d 0a 0a 2f 2a  rmat, ap);.}../*
574d0 0a 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20 6d  .** Print into m
574e0 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66  emory obtained f
574f0 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63  rom sqliteMalloc
57500 28 29 2e 20 20 55 73 65 20 74 68 65 20 69 6e 74  ().  Use the int
57510 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65  ernal.** %-conve
57520 72 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73  rsion extensions
57530 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
57540 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65  ATE char *sqlite
57550 33 4d 50 72 69 6e 74 66 28 63 6f 6e 73 74 20 63  3MPrintf(const c
57560 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e  har *zFormat, ..
57570 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70  .){.  va_list ap
57580 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 63  ;.  char *z;.  c
57590 68 61 72 20 7a 42 61 73 65 5b 53 51 4c 49 54 45  har zBase[SQLITE
575a0 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 5d  _PRINT_BUF_SIZE]
575b0 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c  ;.  va_start(ap,
575c0 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d   zFormat);.  z =
575d0 20 62 61 73 65 5f 76 70 72 69 6e 74 66 28 70 72   base_vprintf(pr
575e0 69 6e 74 66 5f 72 65 61 6c 6c 6f 63 2c 20 31 2c  intf_realloc, 1,
575f0 20 7a 42 61 73 65 2c 20 73 69 7a 65 6f 66 28 7a   zBase, sizeof(z
57600 42 61 73 65 29 2c 20 7a 46 6f 72 6d 61 74 2c 20  Base), zFormat, 
57610 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70  ap);.  va_end(ap
57620 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d  );.  return z;.}
57630 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69 6e  ../*.** Print in
57640 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e  to memory obtain
57650 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f  ed from sqlite3_
57660 6d 61 6c 6c 6f 63 28 29 2e 20 20 4f 6d 69 74 20  malloc().  Omit 
57670 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20  the internal.** 
57680 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 65 78 74  %-conversion ext
57690 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49  ensions..*/.SQLI
576a0 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c  TE_API char *sql
576b0 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 63 6f  ite3_vmprintf(co
576c0 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61  nst char *zForma
576d0 74 2c 20 76 61 5f 6c 69 73 74 20 61 70 29 7b 0a  t, va_list ap){.
576e0 20 20 63 68 61 72 20 7a 42 61 73 65 5b 53 51 4c    char zBase[SQL
576f0 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49  ITE_PRINT_BUF_SI
57700 5a 45 5d 3b 0a 20 20 72 65 74 75 72 6e 20 62 61  ZE];.  return ba
57710 73 65 5f 76 70 72 69 6e 74 66 28 73 71 6c 69 74  se_vprintf(sqlit
57720 65 33 5f 72 65 61 6c 6c 6f 63 2c 20 30 2c 20 7a  e3_realloc, 0, z
57730 42 61 73 65 2c 20 73 69 7a 65 6f 66 28 7a 42 61  Base, sizeof(zBa
57740 73 65 29 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70  se), zFormat, ap
57750 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e  );.}../*.** Prin
57760 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62  t into memory ob
57770 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69  tained from sqli
57780 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 28 29 2e 20  te3_malloc()(). 
57790 20 4f 6d 69 74 20 74 68 65 20 69 6e 74 65 72 6e   Omit the intern
577a0 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69  al.** %-conversi
577b0 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a  on extensions..*
577c0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61  /.SQLITE_API cha
577d0 72 20 2a 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e  r *sqlite3_mprin
577e0 74 66 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  tf(const char *z
577f0 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20  Format, ...){.  
57800 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68  va_list ap;.  ch
57810 61 72 20 2a 7a 3b 0a 20 20 76 61 5f 73 74 61 72  ar *z;.  va_star
57820 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a  t(ap, zFormat);.
57830 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 6d    z = sqlite3_vm
57840 70 72 69 6e 74 66 28 7a 46 6f 72 6d 61 74 2c 20  printf(zFormat, 
57850 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70  ap);.  va_end(ap
57860 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d  );.  return z;.}
57870 0a 0a 2f 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  ../*.** sqlite3_
57880 73 6e 70 72 69 6e 74 66 28 29 20 77 6f 72 6b 73  snprintf() works
57890 20 6c 69 6b 65 20 73 6e 70 72 69 6e 74 66 28 29   like snprintf()
578a0 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74 20   except that it 
578b0 69 67 6e 6f 72 65 73 20 74 68 65 0a 2a 2a 20 63  ignores the.** c
578c0 75 72 72 65 6e 74 20 6c 6f 63 61 6c 65 20 73 65  urrent locale se
578d0 74 74 69 6e 67 73 2e 20 20 54 68 69 73 20 69 73  ttings.  This is
578e0 20 69 6d 70 6f 72 74 61 6e 74 20 66 6f 72 20 53   important for S
578f0 51 4c 69 74 65 20 62 65 63 61 75 73 65 20 77 65  QLite because we
57900 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 61 62 6c 65  .** are not able
57910 20 74 6f 20 75 73 65 20 61 20 22 2c 22 20 61 73   to use a "," as
57920 20 74 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69   the decimal poi
57930 6e 74 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 22  nt in place of "
57940 2e 22 20 61 73 0a 2a 2a 20 73 70 65 63 69 66 69  ." as.** specifi
57950 65 64 20 62 79 20 73 6f 6d 65 20 6c 6f 63 61 6c  ed by some local
57960 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  es..*/.SQLITE_AP
57970 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  I char *sqlite3_
57980 73 6e 70 72 69 6e 74 66 28 69 6e 74 20 6e 2c 20  snprintf(int n, 
57990 63 68 61 72 20 2a 7a 42 75 66 2c 20 63 6f 6e 73  char *zBuf, cons
579a0 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c  t char *zFormat,
579b0 20 2e 2e 2e 29 7b 0a 20 20 63 68 61 72 20 2a 7a   ...){.  char *z
579c0 3b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a  ;.  va_list ap;.
579d0 0a 20 20 69 66 28 20 6e 3c 3d 30 20 29 7b 0a 20  .  if( n<=0 ){. 
579e0 20 20 20 72 65 74 75 72 6e 20 7a 42 75 66 3b 0a     return zBuf;.
579f0 20 20 7d 0a 20 20 7a 42 75 66 5b 30 5d 20 3d 20    }.  zBuf[0] = 
57a00 30 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70  0;.  va_start(ap
57a10 2c 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d  ,zFormat);.  z =
57a20 20 62 61 73 65 5f 76 70 72 69 6e 74 66 28 30 2c   base_vprintf(0,
57a30 20 30 2c 20 7a 42 75 66 2c 20 6e 2c 20 7a 46 6f   0, zBuf, n, zFo
57a40 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f  rmat, ap);.  va_
57a50 65 6e 64 28 61 70 29 3b 0a 20 20 72 65 74 75 72  end(ap);.  retur
57a60 6e 20 7a 3b 0a 7d 0a 0a 23 69 66 20 64 65 66 69  n z;.}..#if defi
57a70 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29  ned(SQLITE_TEST)
57a80 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
57a90 54 45 5f 44 45 42 55 47 29 20 7c 7c 20 64 65 66  TE_DEBUG) || def
57aa0 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 45 4d 44  ined(SQLITE_MEMD
57ab0 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 41 20 76 65  EBUG)./*.** A ve
57ac0 72 73 69 6f 6e 20 6f 66 20 70 72 69 6e 74 66 28  rsion of printf(
57ad0 29 20 74 68 61 74 20 75 6e 64 65 72 73 74 61 6e  ) that understan
57ae0 64 73 20 25 6c 6c 64 2e 20 20 55 73 65 64 20 66  ds %lld.  Used f
57af0 6f 72 20 64 65 62 75 67 67 69 6e 67 2e 0a 2a 2a  or debugging..**
57b00 20 54 68 65 20 70 72 69 6e 74 66 28 29 20 62 75   The printf() bu
57b10 69 6c 74 20 69 6e 74 6f 20 73 6f 6d 65 20 76 65  ilt into some ve
57b20 72 73 69 6f 6e 73 20 6f 66 20 77 69 6e 64 6f 77  rsions of window
57b30 73 20 64 6f 65 73 20 6e 6f 74 20 75 6e 64 65 72  s does not under
57b40 73 74 61 6e 64 20 25 6c 6c 64 0a 2a 2a 20 61 6e  stand %lld.** an
57b50 64 20 73 65 67 66 61 75 6c 74 73 20 69 66 20 79  d segfaults if y
57b60 6f 75 20 67 69 76 65 20 69 74 20 61 20 6c 6f 6e  ou give it a lon
57b70 67 20 6c 6f 6e 67 20 69 6e 74 2e 0a 2a 2f 0a 53  g long int..*/.S
57b80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
57b90 69 64 20 73 71 6c 69 74 65 33 44 65 62 75 67 50  id sqlite3DebugP
57ba0 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72  rintf(const char
57bb0 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b   *zFormat, ...){
57bc0 0a 20 20 65 78 74 65 72 6e 20 69 6e 74 20 67 65  .  extern int ge
57bd0 74 70 69 64 28 76 6f 69 64 29 3b 0a 20 20 76 61  tpid(void);.  va
57be0 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72  _list ap;.  char
57bf0 20 7a 42 75 66 5b 35 30 30 5d 3b 0a 20 20 76 61   zBuf[500];.  va
57c00 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d  _start(ap, zForm
57c10 61 74 29 3b 0a 20 20 62 61 73 65 5f 76 70 72 69  at);.  base_vpri
57c20 6e 74 66 28 30 2c 20 30 2c 20 7a 42 75 66 2c 20  ntf(0, 0, zBuf, 
57c30 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a 46  sizeof(zBuf), zF
57c40 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61  ormat, ap);.  va
57c50 5f 65 6e 64 28 61 70 29 3b 0a 20 20 66 70 72 69  _end(ap);.  fpri
57c60 6e 74 66 28 73 74 64 6f 75 74 2c 22 25 73 22 2c  ntf(stdout,"%s",
57c70 20 7a 42 75 66 29 3b 0a 20 20 66 66 6c 75 73 68   zBuf);.  fflush
57c80 28 73 74 64 6f 75 74 29 3b 0a 7d 0a 23 65 6e 64  (stdout);.}.#end
57c90 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  if../***********
57ca0 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 72 69 6e 74  *** End of print
57cb0 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f.c ************
57cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
57cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
57ce0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
57cf0 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 72  *** Begin file r
57d00 61 6e 64 6f 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  andom.c ********
57d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
57d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
57d30 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53  **/./*.** 2001 S
57d40 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a  eptember 15.**.*
57d50 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
57d60 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
57d70 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
57d80 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
57d90 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
57da0 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
57db0 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
57dc0 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
57dd0 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
57de0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
57df0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
57e00 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
57e10 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
57e20 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
57e30 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
57e40 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
57e50 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
57e60 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
57e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
57e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
57e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
57ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
57eb0 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
57ec0 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65   code to impleme
57ed0 6e 74 20 61 20 70 73 65 75 64 6f 2d 72 61 6e 64  nt a pseudo-rand
57ee0 6f 6d 20 6e 75 6d 62 65 72 0a 2a 2a 20 67 65 6e  om number.** gen
57ef0 65 72 61 74 6f 72 20 28 50 52 4e 47 29 20 66 6f  erator (PRNG) fo
57f00 72 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20  r SQLite..**.** 
57f10 52 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 73 20 61  Random numbers a
57f20 72 65 20 75 73 65 64 20 62 79 20 73 6f 6d 65 20  re used by some 
57f30 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
57f40 62 61 63 6b 65 6e 64 73 20 69 6e 20 6f 72 64 65  backends in orde
57f50 72 0a 2a 2a 20 74 6f 20 67 65 6e 65 72 61 74 65  r.** to generate
57f60 20 72 61 6e 64 6f 6d 20 69 6e 74 65 67 65 72 20   random integer 
57f70 6b 65 79 73 20 66 6f 72 20 74 61 62 6c 65 73 20  keys for tables 
57f80 6f 72 20 72 61 6e 64 6f 6d 20 66 69 6c 65 6e 61  or random filena
57f90 6d 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  mes..**.** $Id: 
57fa0 72 61 6e 64 6f 6d 2e 63 2c 76 20 31 2e 31 36 20  random.c,v 1.16 
57fb0 32 30 30 37 2f 30 31 2f 30 35 20 31 34 3a 33 38  2007/01/05 14:38
57fc0 3a 35 36 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :56 drh Exp $.*/
57fd0 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 73  .../*.** Get a s
57fe0 69 6e 67 6c 65 20 38 2d 62 69 74 20 72 61 6e 64  ingle 8-bit rand
57ff0 6f 6d 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68  om value from th
58000 65 20 52 43 34 20 50 52 4e 47 2e 20 20 54 68 65  e RC4 PRNG.  The
58010 20 4d 75 74 65 78 0a 2a 2a 20 6d 75 73 74 20 62   Mutex.** must b
58020 65 20 68 65 6c 64 20 77 68 69 6c 65 20 65 78 65  e held while exe
58030 63 75 74 69 6e 67 20 74 68 69 73 20 72 6f 75 74  cuting this rout
58040 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 79 20 6e  ine..**.** Why n
58050 6f 74 20 6a 75 73 74 20 75 73 65 20 61 20 6c 69  ot just use a li
58060 62 72 61 72 79 20 72 61 6e 64 6f 6d 20 67 65 6e  brary random gen
58070 65 72 61 74 6f 72 20 6c 69 6b 65 20 6c 72 61 6e  erator like lran
58080 64 34 38 28 29 20 66 6f 72 20 74 68 69 73 3f 0a  d48() for this?.
58090 2a 2a 20 42 65 63 61 75 73 65 20 74 68 65 20 4f  ** Because the O
580a0 50 5f 4e 65 77 52 6f 77 69 64 20 6f 70 63 6f 64  P_NewRowid opcod
580b0 65 20 69 6e 20 74 68 65 20 56 44 42 45 20 64 65  e in the VDBE de
580c0 70 65 6e 64 73 20 6f 6e 20 68 61 76 69 6e 67 20  pends on having 
580d0 61 20 76 65 72 79 0a 2a 2a 20 67 6f 6f 64 20 73  a very.** good s
580e0 6f 75 72 63 65 20 6f 66 20 72 61 6e 64 6f 6d 20  ource of random 
580f0 6e 75 6d 62 65 72 73 2e 20 20 54 68 65 20 6c 72  numbers.  The lr
58100 61 6e 64 34 38 28 29 20 6c 69 62 72 61 72 79 20  and48() library 
58110 66 75 6e 63 74 69 6f 6e 20 6d 61 79 0a 2a 2a 20  function may.** 
58120 77 65 6c 6c 20 62 65 20 67 6f 6f 64 20 65 6e 6f  well be good eno
58130 75 67 68 2e 20 20 42 75 74 20 6d 61 79 62 65 20  ugh.  But maybe 
58140 6e 6f 74 2e 20 20 4f 72 20 6d 61 79 62 65 20 6c  not.  Or maybe l
58150 72 61 6e 64 34 38 28 29 20 68 61 73 20 73 6f 6d  rand48() has som
58160 65 0a 2a 2a 20 73 75 62 74 6c 65 20 70 72 6f 62  e.** subtle prob
58170 6c 65 6d 73 20 6f 6e 20 73 6f 6d 65 20 73 79 73  lems on some sys
58180 74 65 6d 73 20 74 68 61 74 20 63 6f 75 6c 64 20  tems that could 
58190 63 61 75 73 65 20 70 72 6f 62 6c 65 6d 73 2e 20  cause problems. 
581a0 20 49 74 20 69 73 20 68 61 72 64 0a 2a 2a 20 74   It is hard.** t
581b0 6f 20 6b 6e 6f 77 2e 20 20 54 6f 20 6d 69 6e 69  o know.  To mini
581c0 6d 69 7a 65 20 74 68 65 20 72 69 73 6b 20 6f 66  mize the risk of
581d0 20 70 72 6f 62 6c 65 6d 73 20 64 75 65 20 74 6f   problems due to
581e0 20 62 61 64 20 6c 72 61 6e 64 34 38 28 29 0a 2a   bad lrand48().*
581f0 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * implementation
58200 73 2c 20 53 51 4c 69 74 65 20 75 73 65 73 20 74  s, SQLite uses t
58210 68 69 73 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65  his random numbe
58220 72 20 67 65 6e 65 72 61 74 6f 72 20 62 61 73 65  r generator base
58230 64 0a 2a 2a 20 6f 6e 20 52 43 34 2c 20 77 68 69  d.** on RC4, whi
58240 63 68 20 77 65 20 6b 6e 6f 77 20 77 6f 72 6b 73  ch we know works
58250 20 76 65 72 79 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a   very well..**.*
58260 2a 20 28 4c 61 74 65 72 29 3a 20 20 41 63 74 75  * (Later):  Actu
58270 61 6c 6c 79 2c 20 4f 50 5f 4e 65 77 52 6f 77 69  ally, OP_NewRowi
58280 64 20 64 6f 65 73 20 6e 6f 74 20 64 65 70 65 6e  d does not depen
58290 64 20 6f 6e 20 61 20 67 6f 6f 64 20 73 6f 75 72  d on a good sour
582a0 63 65 20 6f 66 0a 2a 2a 20 72 61 6e 64 6f 6d 6e  ce of.** randomn
582b0 65 73 73 20 61 6e 79 20 6d 6f 72 65 2e 20 20 42  ess any more.  B
582c0 75 74 20 77 65 20 77 69 6c 6c 20 6c 65 61 76 65  ut we will leave
582d0 20 74 68 69 73 20 63 6f 64 65 20 69 6e 20 61 6c   this code in al
582e0 6c 20 74 68 65 20 73 61 6d 65 2e 0a 2a 2f 0a 73  l the same..*/.s
582f0 74 61 74 69 63 20 69 6e 74 20 72 61 6e 64 6f 6d  tatic int random
58300 42 79 74 65 28 76 6f 69 64 29 7b 0a 20 20 75 6e  Byte(void){.  un
58310 73 69 67 6e 65 64 20 63 68 61 72 20 74 3b 0a 0a  signed char t;..
58320 20 20 2f 2a 20 41 6c 6c 20 74 68 72 65 61 64 73    /* All threads
58330 20 73 68 61 72 65 20 61 20 73 69 6e 67 6c 65 20   share a single 
58340 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65  random number ge
58350 6e 65 72 61 74 6f 72 2e 0a 20 20 2a 2a 20 54 68  nerator..  ** Th
58360 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20  is structure is 
58370 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 74  the current stat
58380 65 20 6f 66 20 74 68 65 20 67 65 6e 65 72 61 74  e of the generat
58390 6f 72 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69  or..  */.  stati
583a0 63 20 73 74 72 75 63 74 20 7b 0a 20 20 20 20 75  c struct {.    u
583b0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69 73 49  nsigned char isI
583c0 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  nit;          /*
583d0 20 54 72 75 65 20 69 66 20 69 6e 69 74 69 61 6c   True if initial
583e0 69 7a 65 64 20 2a 2f 0a 20 20 20 20 75 6e 73 69  ized */.    unsi
583f0 67 6e 65 64 20 63 68 61 72 20 69 2c 20 6a 3b 20  gned char i, j; 
58400 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74             /* St
58410 61 74 65 20 76 61 72 69 61 62 6c 65 73 20 2a 2f  ate variables */
58420 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68  .    unsigned ch
58430 61 72 20 73 5b 32 35 36 5d 3b 20 20 20 20 20 20  ar s[256];      
58440 20 20 20 20 2f 2a 20 53 74 61 74 65 20 76 61 72      /* State var
58450 69 61 62 6c 65 73 20 2a 2f 0a 20 20 7d 20 70 72  iables */.  } pr
58460 6e 67 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61  ng;..  /* Initia
58470 6c 69 7a 65 20 74 68 65 20 73 74 61 74 65 20 6f  lize the state o
58480 66 20 74 68 65 20 72 61 6e 64 6f 6d 20 6e 75 6d  f the random num
58490 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20 6f 6e  ber generator on
584a0 63 65 2c 0a 20 20 2a 2a 20 74 68 65 20 66 69 72  ce,.  ** the fir
584b0 73 74 20 74 69 6d 65 20 74 68 69 73 20 72 6f 75  st time this rou
584c0 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 20  tine is called. 
584d0 20 54 68 65 20 73 65 65 64 20 76 61 6c 75 65 20   The seed value 
584e0 64 6f 65 73 0a 20 20 2a 2a 20 6e 6f 74 20 6e 65  does.  ** not ne
584f0 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20  ed to contain a 
58500 6c 6f 74 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 73  lot of randomnes
58510 73 20 73 69 6e 63 65 20 77 65 20 61 72 65 20 6e  s since we are n
58520 6f 74 0a 20 20 2a 2a 20 74 72 79 69 6e 67 20 74  ot.  ** trying t
58530 6f 20 64 6f 20 73 65 63 75 72 65 20 65 6e 63 72  o do secure encr
58540 79 70 74 69 6f 6e 20 6f 72 20 61 6e 79 74 68 69  yption or anythi
58550 6e 67 20 6c 69 6b 65 20 74 68 61 74 2e 2e 2e 0a  ng like that....
58560 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 68 69 6e    **.  ** Nothin
58570 67 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 6f  g in this file o
58580 72 20 61 6e 79 77 68 65 72 65 20 65 6c 73 65 20  r anywhere else 
58590 69 6e 20 53 51 4c 69 74 65 20 64 6f 65 73 20 61  in SQLite does a
585a0 6e 79 20 6b 69 6e 64 20 6f 66 0a 20 20 2a 2a 20  ny kind of.  ** 
585b0 65 6e 63 72 79 70 74 69 6f 6e 2e 20 20 54 68 65  encryption.  The
585c0 20 52 43 34 20 61 6c 67 6f 72 69 74 68 6d 20 69   RC4 algorithm i
585d0 73 20 62 65 69 6e 67 20 75 73 65 64 20 61 73 20  s being used as 
585e0 61 20 50 52 4e 47 20 28 70 73 65 75 64 6f 2d 72  a PRNG (pseudo-r
585f0 61 6e 64 6f 6d 0a 20 20 2a 2a 20 6e 75 6d 62 65  andom.  ** numbe
58600 72 20 67 65 6e 65 72 61 74 6f 72 29 20 6e 6f 74  r generator) not
58610 20 61 73 20 61 6e 20 65 6e 63 72 79 70 74 69 6f   as an encryptio
58620 6e 20 64 65 76 69 63 65 2e 0a 20 20 2a 2f 0a 20  n device..  */. 
58630 20 69 66 28 20 21 70 72 6e 67 2e 69 73 49 6e 69   if( !prng.isIni
58640 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  t ){.    int i;.
58650 20 20 20 20 63 68 61 72 20 6b 5b 32 35 36 5d 3b      char k[256];
58660 0a 20 20 20 20 70 72 6e 67 2e 6a 20 3d 20 30 3b  .    prng.j = 0;
58670 0a 20 20 20 20 70 72 6e 67 2e 69 20 3d 20 30 3b  .    prng.i = 0;
58680 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 52 61  .    sqlite3OsRa
58690 6e 64 6f 6d 53 65 65 64 28 6b 29 3b 0a 20 20 20  ndomSeed(k);.   
586a0 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 35 36 3b   for(i=0; i<256;
586b0 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 72 6e   i++){.      prn
586c0 67 2e 73 5b 69 5d 20 3d 20 69 3b 0a 20 20 20 20  g.s[i] = i;.    
586d0 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  }.    for(i=0; i
586e0 3c 32 35 36 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  <256; i++){.    
586f0 20 20 70 72 6e 67 2e 6a 20 2b 3d 20 70 72 6e 67    prng.j += prng
58700 2e 73 5b 69 5d 20 2b 20 6b 5b 69 5d 3b 0a 20 20  .s[i] + k[i];.  
58710 20 20 20 20 74 20 3d 20 70 72 6e 67 2e 73 5b 70      t = prng.s[p
58720 72 6e 67 2e 6a 5d 3b 0a 20 20 20 20 20 20 70 72  rng.j];.      pr
58730 6e 67 2e 73 5b 70 72 6e 67 2e 6a 5d 20 3d 20 70  ng.s[prng.j] = p
58740 72 6e 67 2e 73 5b 69 5d 3b 0a 20 20 20 20 20 20  rng.s[i];.      
58750 70 72 6e 67 2e 73 5b 69 5d 20 3d 20 74 3b 0a 20  prng.s[i] = t;. 
58760 20 20 20 7d 0a 20 20 20 20 70 72 6e 67 2e 69 73     }.    prng.is
58770 49 6e 69 74 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20  Init = 1;.  }.. 
58780 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 6e 64   /* Generate and
58790 20 72 65 74 75 72 6e 20 73 69 6e 67 6c 65 20 72   return single r
587a0 61 6e 64 6f 6d 20 62 79 74 65 0a 20 20 2a 2f 0a  andom byte.  */.
587b0 20 20 70 72 6e 67 2e 69 2b 2b 3b 0a 20 20 74 20    prng.i++;.  t 
587c0 3d 20 70 72 6e 67 2e 73 5b 70 72 6e 67 2e 69 5d  = prng.s[prng.i]
587d0 3b 0a 20 20 70 72 6e 67 2e 6a 20 2b 3d 20 74 3b  ;.  prng.j += t;
587e0 0a 20 20 70 72 6e 67 2e 73 5b 70 72 6e 67 2e 69  .  prng.s[prng.i
587f0 5d 20 3d 20 70 72 6e 67 2e 73 5b 70 72 6e 67 2e  ] = prng.s[prng.
58800 6a 5d 3b 0a 20 20 70 72 6e 67 2e 73 5b 70 72 6e  j];.  prng.s[prn
58810 67 2e 6a 5d 20 3d 20 74 3b 0a 20 20 74 20 2b 3d  g.j] = t;.  t +=
58820 20 70 72 6e 67 2e 73 5b 70 72 6e 67 2e 69 5d 3b   prng.s[prng.i];
58830 0a 20 20 72 65 74 75 72 6e 20 70 72 6e 67 2e 73  .  return prng.s
58840 5b 74 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  [t];.}../*.** Re
58850 74 75 72 6e 20 4e 20 72 61 6e 64 6f 6d 20 62 79  turn N random by
58860 74 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  tes..*/.SQLITE_P
58870 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
58880 74 65 33 52 61 6e 64 6f 6d 6e 65 73 73 28 69 6e  te3Randomness(in
58890 74 20 4e 2c 20 76 6f 69 64 20 2a 70 42 75 66 29  t N, void *pBuf)
588a0 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  {.  unsigned cha
588b0 72 20 2a 7a 42 75 66 20 3d 20 70 42 75 66 3b 0a  r *zBuf = pBuf;.
588c0 20 20 73 71 6c 69 74 65 33 4f 73 45 6e 74 65 72    sqlite3OsEnter
588d0 4d 75 74 65 78 28 29 3b 0a 20 20 77 68 69 6c 65  Mutex();.  while
588e0 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 20 2a 28 7a  ( N-- ){.    *(z
588f0 42 75 66 2b 2b 29 20 3d 20 72 61 6e 64 6f 6d 42  Buf++) = randomB
58900 79 74 65 28 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  yte();.  }.  sql
58910 69 74 65 33 4f 73 4c 65 61 76 65 4d 75 74 65 78  ite3OsLeaveMutex
58920 28 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ();.}../********
58930 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 72 61  ****** End of ra
58940 6e 64 6f 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  ndom.c *********
58950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58970 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
58980 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
58990 65 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  e utf.c ********
589a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
589b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
589c0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
589d0 34 20 41 70 72 69 6c 20 31 33 0a 2a 2a 0a 2a 2a  4 April 13.**.**
589e0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
589f0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
58a00 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
58a10 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
58a20 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
58a30 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
58a40 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
58a50 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
58a60 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
58a70 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
58a80 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
58a90 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
58aa0 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
58ab0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
58ac0 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
58ad0 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
58ae0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
58af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
58b40 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
58b50 72 6f 75 74 69 6e 65 73 20 75 73 65 64 20 74 6f  routines used to
58b60 20 74 72 61 6e 73 6c 61 74 65 20 62 65 74 77 65   translate betwe
58b70 65 6e 20 55 54 46 2d 38 2c 20 0a 2a 2a 20 55 54  en UTF-8, .** UT
58b80 46 2d 31 36 2c 20 55 54 46 2d 31 36 42 45 2c 20  F-16, UTF-16BE, 
58b90 61 6e 64 20 55 54 46 2d 31 36 4c 45 2e 0a 2a 2a  and UTF-16LE..**
58ba0 0a 2a 2a 20 24 49 64 3a 20 75 74 66 2e 63 2c 76  .** $Id: utf.c,v
58bb0 20 31 2e 35 32 20 32 30 30 37 2f 30 37 2f 32 33   1.52 2007/07/23
58bc0 20 31 39 3a 31 32 3a 34 32 20 64 72 68 20 45 78   19:12:42 drh Ex
58bd0 70 20 24 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 20  p $.**.** Notes 
58be0 6f 6e 20 55 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a 20  on UTF-8:.**.** 
58bf0 20 20 42 79 74 65 2d 30 20 20 20 20 42 79 74 65    Byte-0    Byte
58c00 2d 31 20 20 20 20 42 79 74 65 2d 32 20 20 20 20  -1    Byte-2    
58c10 42 79 74 65 2d 33 20 20 20 20 56 61 6c 75 65 0a  Byte-3    Value.
58c20 2a 2a 20 20 30 78 78 78 78 78 78 78 20 20 20 20  **  0xxxxxxx    
58c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58c40 20 20 20 20 20 20 20 20 20 20 20 20 20 30 30 30               000
58c50 30 30 30 30 30 20 30 30 30 30 30 30 30 30 20 30  00000 00000000 0
58c60 78 78 78 78 78 78 78 0a 2a 2a 20 20 31 31 30 79  xxxxxxx.**  110y
58c70 79 79 79 79 20 20 31 30 78 78 78 78 78 78 20 20  yyyy  10xxxxxx  
58c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58c90 20 20 20 20 20 30 30 30 30 30 30 30 30 20 30 30       00000000 00
58ca0 30 30 30 79 79 79 20 79 79 78 78 78 78 78 78 0a  000yyy yyxxxxxx.
58cb0 2a 2a 20 20 31 31 31 30 7a 7a 7a 7a 20 20 31 30  **  1110zzzz  10
58cc0 79 79 79 79 79 79 20 20 31 30 78 78 78 78 78 78  yyyyyy  10xxxxxx
58cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 30 30 30               000
58ce0 30 30 30 30 30 20 7a 7a 7a 7a 79 79 79 79 20 79  00000 zzzzyyyy y
58cf0 79 78 78 78 78 78 78 0a 2a 2a 20 20 31 31 31 31  yxxxxxx.**  1111
58d00 30 75 75 75 20 20 31 30 75 75 7a 7a 7a 7a 20 20  0uuu  10uuzzzz  
58d10 31 30 79 79 79 79 79 79 20 20 31 30 78 78 78 78  10yyyyyy  10xxxx
58d20 78 78 20 20 20 30 30 30 75 75 75 75 75 20 7a 7a  xx   000uuuuu zz
58d30 7a 7a 79 79 79 79 20 79 79 78 78 78 78 78 78 0a  zzyyyy yyxxxxxx.
58d40 2a 2a 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 20 6f  **.**.** Notes o
58d50 6e 20 55 54 46 2d 31 36 3a 20 20 28 77 69 74 68  n UTF-16:  (with
58d60 20 77 77 77 77 2b 31 3d 3d 75 75 75 75 75 29 0a   wwww+1==uuuuu).
58d70 2a 2a 0a 2a 2a 20 20 20 20 20 20 57 6f 72 64 2d  **.**      Word-
58d80 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0               
58d90 57 6f 72 64 2d 31 20 20 20 20 20 20 20 20 20 20  Word-1          
58da0 56 61 6c 75 65 0a 2a 2a 20 20 31 31 30 31 31 30  Value.**  110110
58db0 77 77 20 77 77 7a 7a 7a 7a 79 79 20 20 20 31 31  ww wwzzzzyy   11
58dc0 30 31 31 31 79 79 20 79 79 78 78 78 78 78 78 20  0111yy yyxxxxxx 
58dd0 20 20 20 30 30 30 75 75 75 75 75 20 7a 7a 7a 7a     000uuuuu zzzz
58de0 79 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a  yyyy yyxxxxxx.**
58df0 20 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 78    zzzzyyyy yyxxx
58e00 78 78 78 20 20 20 20 20 20 20 20 20 20 20 20 20  xxx             
58e10 20 20 20 20 20 20 20 20 20 20 20 30 30 30 30 30             00000
58e20 30 30 30 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78  000 zzzzyyyy yyx
58e30 78 78 78 78 78 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 42  xxxxx.**.**.** B
58e40 4f 4d 20 6f 72 20 42 79 74 65 20 4f 72 64 65 72  OM or Byte Order
58e50 20 4d 61 72 6b 3a 0a 2a 2a 20 20 20 20 20 30 78   Mark:.**     0x
58e60 66 66 20 30 78 66 65 20 20 20 6c 69 74 74 6c 65  ff 0xfe   little
58e70 2d 65 6e 64 69 61 6e 20 75 74 66 2d 31 36 20 66  -endian utf-16 f
58e80 6f 6c 6c 6f 77 73 0a 2a 2a 20 20 20 20 20 30 78  ollows.**     0x
58e90 66 65 20 30 78 66 66 20 20 20 62 69 67 2d 65 6e  fe 0xff   big-en
58ea0 64 69 61 6e 20 75 74 66 2d 31 36 20 66 6f 6c 6c  dian utf-16 foll
58eb0 6f 77 73 0a 2a 2a 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ows.**.*/./*****
58ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64  ********* Includ
58ed0 65 20 76 64 62 65 49 6e 74 2e 68 20 69 6e 20 74  e vdbeInt.h in t
58ee0 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 75 74 66  he middle of utf
58ef0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
58f00 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
58f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
58f20 66 69 6c 65 20 76 64 62 65 49 6e 74 2e 68 20 2a  file vdbeInt.h *
58f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58f50 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
58f60 32 30 30 33 20 53 65 70 74 65 6d 62 65 72 20 36  2003 September 6
58f70 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
58f80 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
58f90 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
58fa0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
58fb0 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
58fc0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
58fd0 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
58fe0 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
58ff0 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
59000 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
59010 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
59020 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
59030 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
59040 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
59050 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
59060 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
59070 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
59080 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
59090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
590a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
590b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
590c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
590d0 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 68  ** This is the h
590e0 65 61 64 65 72 20 66 69 6c 65 20 66 6f 72 20 69  eader file for i
590f0 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74 20  nformation that 
59100 69 73 20 70 72 69 76 61 74 65 20 74 6f 20 74 68  is private to th
59110 65 0a 2a 2a 20 56 44 42 45 2e 20 20 54 68 69 73  e.** VDBE.  This
59120 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65   information use
59130 64 20 74 6f 20 61 6c 6c 20 62 65 20 61 74 20 74  d to all be at t
59140 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 69  he top of the si
59150 6e 67 6c 65 0a 2a 2a 20 73 6f 75 72 63 65 20 63  ngle.** source c
59160 6f 64 65 20 66 69 6c 65 20 22 76 64 62 65 2e 63  ode file "vdbe.c
59170 22 2e 20 20 57 68 65 6e 20 74 68 61 74 20 66 69  ".  When that fi
59180 6c 65 20 62 65 63 61 6d 65 20 74 6f 6f 20 62 69  le became too bi
59190 67 20 28 6f 76 65 72 0a 2a 2a 20 36 30 30 30 20  g (over.** 6000 
591a0 6c 69 6e 65 73 20 6c 6f 6e 67 29 20 69 74 20 77  lines long) it w
591b0 61 73 20 73 70 6c 69 74 20 75 70 20 69 6e 74 6f  as split up into
591c0 20 73 65 76 65 72 61 6c 20 73 6d 61 6c 6c 65 72   several smaller
591d0 20 66 69 6c 65 73 20 61 6e 64 0a 2a 2a 20 74 68   files and.** th
591e0 69 73 20 68 65 61 64 65 72 20 69 6e 66 6f 72 6d  is header inform
591f0 61 74 69 6f 6e 20 77 61 73 20 66 61 63 74 6f 72  ation was factor
59200 65 64 20 6f 75 74 2e 0a 2a 2f 0a 23 69 66 6e 64  ed out..*/.#ifnd
59210 65 66 20 5f 56 44 42 45 49 4e 54 5f 48 5f 0a 23  ef _VDBEINT_H_.#
59220 64 65 66 69 6e 65 20 5f 56 44 42 45 49 4e 54 5f  define _VDBEINT_
59230 48 5f 0a 0a 2f 2a 0a 2a 2a 20 69 6e 74 54 6f 4b  H_../*.** intToK
59240 65 79 28 29 20 61 6e 64 20 6b 65 79 54 6f 49 6e  ey() and keyToIn
59250 74 28 29 20 75 73 65 64 20 74 6f 20 74 72 61 6e  t() used to tran
59260 73 66 6f 72 6d 20 74 68 65 20 72 6f 77 69 64 2e  sform the rowid.
59270 20 20 42 75 74 20 77 69 74 68 0a 2a 2a 20 74 68    But with.** th
59280 65 20 6c 61 74 65 73 74 20 76 65 72 73 69 6f 6e  e latest version
59290 73 20 6f 66 20 74 68 65 20 64 65 73 69 67 6e 20  s of the design 
592a0 74 68 65 79 20 61 72 65 20 6e 6f 2d 6f 70 73 2e  they are no-ops.
592b0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 6b 65 79 54  .*/.#define keyT
592c0 6f 49 6e 74 28 58 29 20 20 20 28 58 29 0a 23 64  oInt(X)   (X).#d
592d0 65 66 69 6e 65 20 69 6e 74 54 6f 4b 65 79 28 58  efine intToKey(X
592e0 29 20 20 20 28 58 29 0a 0a 2f 2a 0a 2a 2a 20 54  )   (X)../*.** T
592f0 68 65 20 6d 61 6b 65 66 69 6c 65 20 73 63 61 6e  he makefile scan
59300 73 20 74 68 65 20 76 64 62 65 2e 63 20 73 6f 75  s the vdbe.c sou
59310 72 63 65 20 66 69 6c 65 20 61 6e 64 20 63 72 65  rce file and cre
59320 61 74 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69  ates the followi
59330 6e 67 0a 2a 2a 20 61 72 72 61 79 20 6f 66 20 73  ng.** array of s
59340 74 72 69 6e 67 20 63 6f 6e 73 74 61 6e 74 73 20  tring constants 
59350 77 68 69 63 68 20 61 72 65 20 74 68 65 20 6e 61  which are the na
59360 6d 65 73 20 6f 66 20 61 6c 6c 20 56 44 42 45 20  mes of all VDBE 
59370 6f 70 63 6f 64 65 73 2e 20 20 54 68 69 73 0a 2a  opcodes.  This.*
59380 2a 20 61 72 72 61 79 20 69 73 20 64 65 66 69 6e  * array is defin
59390 65 64 20 69 6e 20 61 20 73 65 70 61 72 61 74 65  ed in a separate
593a0 20 73 6f 75 72 63 65 20 63 6f 64 65 20 66 69 6c   source code fil
593b0 65 20 6e 61 6d 65 64 20 6f 70 63 6f 64 65 2e 63  e named opcode.c
593c0 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 61 75 74   which is.** aut
593d0 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e 65 72  omatically gener
593e0 61 74 65 64 20 62 79 20 74 68 65 20 6d 61 6b 65  ated by the make
593f0 66 69 6c 65 2e 0a 2a 2f 0a 65 78 74 65 72 6e 20  file..*/.extern 
59400 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73  const char *cons
59410 74 20 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e  t sqlite3OpcodeN
59420 61 6d 65 73 5b 5d 3b 0a 0a 2f 2a 0a 2a 2a 20 53  ames[];../*.** S
59430 51 4c 20 69 73 20 74 72 61 6e 73 6c 61 74 65 64  QL is translated
59440 20 69 6e 74 6f 20 61 20 73 65 71 75 65 6e 63 65   into a sequence
59450 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e 73   of instructions
59460 20 74 6f 20 62 65 0a 2a 2a 20 65 78 65 63 75 74   to be.** execut
59470 65 64 20 62 79 20 61 20 76 69 72 74 75 61 6c 20  ed by a virtual 
59480 6d 61 63 68 69 6e 65 2e 20 20 45 61 63 68 20 69  machine.  Each i
59490 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 61 6e  nstruction is an
594a0 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20   instance.** of 
594b0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
594c0 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 70 65  ructure..*/.type
594d0 64 65 66 20 73 74 72 75 63 74 20 56 64 62 65 4f  def struct VdbeO
594e0 70 20 4f 70 3b 0a 0a 2f 2a 0a 2a 2a 20 42 6f 6f  p Op;../*.** Boo
594f0 6c 65 61 6e 20 76 61 6c 75 65 73 0a 2a 2f 0a 74  lean values.*/.t
59500 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64 20  ypedef unsigned 
59510 63 68 61 72 20 42 6f 6f 6c 3b 0a 0a 2f 2a 0a 2a  char Bool;../*.*
59520 2a 20 41 20 63 75 72 73 6f 72 20 69 73 20 61 20  * A cursor is a 
59530 70 6f 69 6e 74 65 72 20 69 6e 74 6f 20 61 20 73  pointer into a s
59540 69 6e 67 6c 65 20 42 54 72 65 65 20 77 69 74 68  ingle BTree with
59550 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 69  in a database fi
59560 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f  le..** The curso
59570 72 20 63 61 6e 20 73 65 65 6b 20 74 6f 20 61 20  r can seek to a 
59580 42 54 72 65 65 20 65 6e 74 72 79 20 77 69 74 68  BTree entry with
59590 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 6b 65   a particular ke
595a0 79 2c 20 6f 72 0a 2a 2a 20 6c 6f 6f 70 20 6f 76  y, or.** loop ov
595b0 65 72 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 6f  er all entries o
595c0 66 20 74 68 65 20 42 74 72 65 65 2e 20 20 59 6f  f the Btree.  Yo
595d0 75 20 63 61 6e 20 61 6c 73 6f 20 69 6e 73 65 72  u can also inser
595e0 74 20 6e 65 77 20 42 54 72 65 65 0a 2a 2a 20 65  t new BTree.** e
595f0 6e 74 72 69 65 73 20 6f 72 20 72 65 74 72 69 65  ntries or retrie
59600 76 65 20 74 68 65 20 6b 65 79 20 6f 72 20 64 61  ve the key or da
59610 74 61 20 66 72 6f 6d 20 74 68 65 20 65 6e 74 72  ta from the entr
59620 79 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f  y that the curso
59630 72 0a 2a 2a 20 69 73 20 63 75 72 72 65 6e 74 6c  r.** is currentl
59640 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 0a 2a  y pointing to..*
59650 2a 20 0a 2a 2a 20 45 76 65 72 79 20 63 75 72 73  * .** Every curs
59660 6f 72 20 74 68 61 74 20 74 68 65 20 76 69 72 74  or that the virt
59670 75 61 6c 20 6d 61 63 68 69 6e 65 20 68 61 73 20  ual machine has 
59680 6f 70 65 6e 20 69 73 20 72 65 70 72 65 73 65 6e  open is represen
59690 74 65 64 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73  ted by an.** ins
596a0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
596b0 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
596c0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 43  ..**.** If the C
596d0 75 72 73 6f 72 2e 69 73 54 72 69 67 67 65 72 52  ursor.isTriggerR
596e0 6f 77 20 66 6c 61 67 20 69 73 20 73 65 74 20 69  ow flag is set i
596f0 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 69  t means that thi
59700 73 20 63 75 72 73 6f 72 20 69 73 0a 2a 2a 20 72  s cursor is.** r
59710 65 61 6c 6c 79 20 61 20 73 69 6e 67 6c 65 20 72  eally a single r
59720 6f 77 20 74 68 61 74 20 72 65 70 72 65 73 65 6e  ow that represen
59730 74 73 20 74 68 65 20 4e 45 57 20 6f 72 20 4f 4c  ts the NEW or OL
59740 44 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 6f  D pseudo-table o
59750 66 0a 2a 2a 20 61 20 72 6f 77 20 74 72 69 67 67  f.** a row trigg
59760 65 72 2e 20 20 54 68 65 20 64 61 74 61 20 66 6f  er.  The data fo
59770 72 20 74 68 65 20 72 6f 77 20 69 73 20 73 74 6f  r the row is sto
59780 72 65 64 20 69 6e 20 43 75 72 73 6f 72 2e 70 44  red in Cursor.pD
59790 61 74 61 20 61 6e 64 0a 2a 2a 20 74 68 65 20 72  ata and.** the r
597a0 6f 77 69 64 20 69 73 20 69 6e 20 43 75 72 73 6f  owid is in Curso
597b0 72 2e 69 4b 65 79 2e 0a 2a 2f 0a 73 74 72 75 63  r.iKey..*/.struc
597c0 74 20 43 75 72 73 6f 72 20 7b 0a 20 20 42 74 43  t Cursor {.  BtC
597d0 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b 20  ursor *pCursor; 
597e0 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72     /* The cursor
597f0 20 73 74 72 75 63 74 75 72 65 20 6f 66 20 74 68   structure of th
59800 65 20 62 61 63 6b 65 6e 64 20 2a 2f 0a 20 20 69  e backend */.  i
59810 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20  nt iDb;         
59820 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
59830 20 63 75 72 73 6f 72 20 64 61 74 61 62 61 73 65   cursor database
59840 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 20 28 6f   in db->aDb[] (o
59850 72 20 2d 31 29 20 2a 2f 0a 20 20 69 36 34 20 6c  r -1) */.  i64 l
59860 61 73 74 52 6f 77 69 64 3b 20 20 20 20 20 20 20  astRowid;       
59870 20 2f 2a 20 4c 61 73 74 20 72 6f 77 69 64 20 66   /* Last rowid f
59880 72 6f 6d 20 61 20 4e 65 78 74 20 6f 72 20 4e 65  rom a Next or Ne
59890 78 74 49 64 78 20 6f 70 65 72 61 74 69 6f 6e 20  xtIdx operation 
598a0 2a 2f 0a 20 20 69 36 34 20 6e 65 78 74 52 6f 77  */.  i64 nextRow
598b0 69 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65  id;        /* Ne
598c0 78 74 20 72 6f 77 69 64 20 72 65 74 75 72 6e 65  xt rowid returne
598d0 64 20 62 79 20 4f 50 5f 4e 65 77 52 6f 77 69 64  d by OP_NewRowid
598e0 20 2a 2f 0a 20 20 42 6f 6f 6c 20 7a 65 72 6f 65   */.  Bool zeroe
598f0 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  d;          /* T
59900 72 75 65 20 69 66 20 7a 65 72 6f 65 64 20 6f 75  rue if zeroed ou
59910 74 20 61 6e 64 20 72 65 61 64 79 20 66 6f 72 20  t and ready for 
59920 72 65 75 73 65 20 2a 2f 0a 20 20 42 6f 6f 6c 20  reuse */.  Bool 
59930 72 6f 77 69 64 49 73 56 61 6c 69 64 3b 20 20 20  rowidIsValid;   
59940 20 2f 2a 20 54 72 75 65 20 69 66 20 6c 61 73 74   /* True if last
59950 52 6f 77 69 64 20 69 73 20 76 61 6c 69 64 20 2a  Rowid is valid *
59960 2f 0a 20 20 42 6f 6f 6c 20 61 74 46 69 72 73 74  /.  Bool atFirst
59970 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  ;         /* Tru
59980 65 20 69 66 20 70 6f 69 6e 74 69 6e 67 20 74 6f  e if pointing to
59990 20 66 69 72 73 74 20 65 6e 74 72 79 20 2a 2f 0a   first entry */.
599a0 20 20 42 6f 6f 6c 20 75 73 65 52 61 6e 64 6f 6d    Bool useRandom
599b0 52 6f 77 69 64 3b 20 20 2f 2a 20 47 65 6e 65 72  Rowid;  /* Gener
599c0 61 74 65 20 6e 65 77 20 72 65 63 6f 72 64 20 6e  ate new record n
599d0 75 6d 62 65 72 73 20 73 65 6d 69 2d 72 61 6e 64  umbers semi-rand
599e0 6f 6d 6c 79 20 2a 2f 0a 20 20 42 6f 6f 6c 20 6e  omly */.  Bool n
599f0 75 6c 6c 52 6f 77 3b 20 20 20 20 20 20 20 20 20  ullRow;         
59a00 2f 2a 20 54 72 75 65 20 69 66 20 70 6f 69 6e 74  /* True if point
59a10 69 6e 67 20 74 6f 20 61 20 72 6f 77 20 77 69 74  ing to a row wit
59a20 68 20 6e 6f 20 64 61 74 61 20 2a 2f 0a 20 20 42  h no data */.  B
59a30 6f 6f 6c 20 6e 65 78 74 52 6f 77 69 64 56 61 6c  ool nextRowidVal
59a40 69 64 3b 20 20 2f 2a 20 54 72 75 65 20 69 66 20  id;  /* True if 
59a50 74 68 65 20 6e 65 78 74 52 6f 77 69 64 20 66 69  the nextRowid fi
59a60 65 6c 64 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a  eld is valid */.
59a70 20 20 42 6f 6f 6c 20 70 73 65 75 64 6f 54 61 62    Bool pseudoTab
59a80 6c 65 3b 20 20 20 20 20 2f 2a 20 54 68 69 73 20  le;     /* This 
59a90 69 73 20 61 20 4e 45 57 20 6f 72 20 4f 4c 44 20  is a NEW or OLD 
59aa0 70 73 65 75 64 6f 2d 74 61 62 6c 65 73 20 6f 66  pseudo-tables of
59ab0 20 61 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20   a trigger */.  
59ac0 42 6f 6f 6c 20 64 65 66 65 72 72 65 64 4d 6f 76  Bool deferredMov
59ad0 65 74 6f 3b 20 20 2f 2a 20 41 20 63 61 6c 6c 20  eto;  /* A call 
59ae0 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d  to sqlite3BtreeM
59af0 6f 76 65 74 6f 28 29 20 69 73 20 6e 65 65 64 65  oveto() is neede
59b00 64 20 2a 2f 0a 20 20 42 6f 6f 6c 20 69 73 54 61  d */.  Bool isTa
59b10 62 6c 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ble;         /* 
59b20 54 72 75 65 20 69 66 20 61 20 74 61 62 6c 65 20  True if a table 
59b30 72 65 71 75 69 72 69 6e 67 20 69 6e 74 65 67 65  requiring intege
59b40 72 20 6b 65 79 73 20 2a 2f 0a 20 20 42 6f 6f 6c  r keys */.  Bool
59b50 20 69 73 49 6e 64 65 78 3b 20 20 20 20 20 20 20   isIndex;       
59b60 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 20    /* True if an 
59b70 69 6e 64 65 78 20 63 6f 6e 74 61 69 6e 69 6e 67  index containing
59b80 20 6b 65 79 73 20 6f 6e 6c 79 20 2d 20 6e 6f 20   keys only - no 
59b90 64 61 74 61 20 2a 2f 0a 20 20 75 38 20 62 6f 67  data */.  u8 bog
59ba0 75 73 49 6e 63 72 4b 65 79 3b 20 20 20 20 20 20  usIncrKey;      
59bb0 2f 2a 20 53 6f 6d 65 74 68 69 6e 67 20 66 6f 72  /* Something for
59bc0 20 70 49 6e 63 72 4b 65 79 20 74 6f 20 70 6f 69   pIncrKey to poi
59bd0 6e 74 20 74 6f 20 69 66 20 70 4b 65 79 49 6e 66  nt to if pKeyInf
59be0 6f 3d 3d 30 20 2a 2f 0a 20 20 69 36 34 20 6d 6f  o==0 */.  i64 mo
59bf0 76 65 74 6f 54 61 72 67 65 74 3b 20 20 20 20 20  vetoTarget;     
59c00 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74  /* Argument to t
59c10 68 65 20 64 65 66 65 72 72 65 64 20 73 71 6c 69  he deferred sqli
59c20 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 29  te3BtreeMoveto()
59c30 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 74   */.  Btree *pBt
59c40 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53  ;           /* S
59c50 65 70 61 72 61 74 65 20 66 69 6c 65 20 68 6f 6c  eparate file hol
59c60 64 69 6e 67 20 74 65 6d 70 6f 72 61 72 79 20 74  ding temporary t
59c70 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 44  able */.  int nD
59c80 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20  ata;            
59c90 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
59ca0 65 73 20 69 6e 20 70 44 61 74 61 20 2a 2f 0a 20  es in pData */. 
59cb0 20 63 68 61 72 20 2a 70 44 61 74 61 3b 20 20 20   char *pData;   
59cc0 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 66         /* Data f
59cd0 6f 72 20 61 20 4e 45 57 20 6f 72 20 4f 4c 44 20  or a NEW or OLD 
59ce0 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 2a 2f 0a  pseudo-table */.
59cf0 20 20 69 36 34 20 69 4b 65 79 3b 20 20 20 20 20    i64 iKey;     
59d00 20 20 20 20 20 20 20 20 2f 2a 20 4b 65 79 20 66          /* Key f
59d10 6f 72 20 74 68 65 20 4e 45 57 20 6f 72 20 4f 4c  or the NEW or OL
59d20 44 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 72  D pseudo-table r
59d30 6f 77 20 2a 2f 0a 20 20 75 38 20 2a 70 49 6e 63  ow */.  u8 *pInc
59d40 72 4b 65 79 3b 20 20 20 20 20 20 20 20 20 2f 2a  rKey;         /*
59d50 20 50 6f 69 6e 74 65 72 20 74 6f 20 70 4b 65 79   Pointer to pKey
59d60 49 6e 66 6f 2d 3e 69 6e 63 72 4b 65 79 20 2a 2f  Info->incrKey */
59d70 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79  .  KeyInfo *pKey
59d80 49 6e 66 6f 3b 20 20 20 20 2f 2a 20 49 6e 66 6f  Info;    /* Info
59d90 20 61 62 6f 75 74 20 69 6e 64 65 78 20 6b 65 79   about index key
59da0 73 20 6e 65 65 64 65 64 20 62 79 20 69 6e 64 65  s needed by inde
59db0 78 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 69  x cursors */.  i
59dc0 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20  nt nField;      
59dd0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
59de0 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20  f fields in the 
59df0 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 36 34 20  header */.  i64 
59e00 73 65 71 43 6f 75 6e 74 3b 20 20 20 20 20 20 20  seqCount;       
59e10 20 20 2f 2a 20 53 65 71 75 65 6e 63 65 20 63 6f    /* Sequence co
59e20 75 6e 74 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74  unter */.  sqlit
59e30 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a  e3_vtab_cursor *
59e40 70 56 74 61 62 43 75 72 73 6f 72 3b 20 20 2f 2a  pVtabCursor;  /*
59e50 20 54 68 65 20 63 75 72 73 6f 72 20 66 6f 72 20   The cursor for 
59e60 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  a virtual table 
59e70 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  */.  const sqlit
59e80 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75  e3_module *pModu
59e90 6c 65 3b 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c  le;     /* Modul
59ea0 65 20 66 6f 72 20 63 75 72 73 6f 72 20 70 56 74  e for cursor pVt
59eb0 61 62 43 75 72 73 6f 72 20 2a 2f 0a 0a 20 20 2f  abCursor */..  /
59ec0 2a 20 43 61 63 68 65 64 20 69 6e 66 6f 72 6d 61  * Cached informa
59ed0 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 68  tion about the h
59ee0 65 61 64 65 72 20 66 6f 72 20 74 68 65 20 64 61  eader for the da
59ef0 74 61 20 72 65 63 6f 72 64 20 74 68 61 74 20 74  ta record that t
59f00 68 65 0a 20 20 2a 2a 20 63 75 72 73 6f 72 20 69  he.  ** cursor i
59f10 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e  s currently poin
59f20 74 69 6e 67 20 74 6f 2e 20 20 4f 6e 6c 79 20 76  ting to.  Only v
59f30 61 6c 69 64 20 69 66 20 63 61 63 68 65 56 61 6c  alid if cacheVal
59f40 69 64 20 69 73 20 74 72 75 65 2e 0a 20 20 2a 2a  id is true..  **
59f50 20 61 52 6f 77 20 6d 69 67 68 74 20 70 6f 69 6e   aRow might poin
59f60 74 20 74 6f 20 28 65 70 68 65 6d 65 72 61 6c 29  t to (ephemeral)
59f70 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 63 75   data for the cu
59f80 72 72 65 6e 74 20 72 6f 77 2c 20 6f 72 20 69 74  rrent row, or it
59f90 20 6d 69 67 68 74 0a 20 20 2a 2a 20 62 65 20 4e   might.  ** be N
59fa0 55 4c 4c 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20  ULL..  */.  int 
59fb0 63 61 63 68 65 53 74 61 74 75 73 3b 20 20 20 20  cacheStatus;    
59fc0 20 20 2f 2a 20 43 61 63 68 65 20 69 73 20 76 61    /* Cache is va
59fd0 6c 69 64 20 69 66 20 74 68 69 73 20 6d 61 74 63  lid if this matc
59fe0 68 65 73 20 56 64 62 65 2e 63 61 63 68 65 43 74  hes Vdbe.cacheCt
59ff0 72 20 2a 2f 0a 20 20 69 6e 74 20 70 61 79 6c 6f  r */.  int paylo
5a000 61 64 53 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20  adSize;      /* 
5a010 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  Total number of 
5a020 62 79 74 65 73 20 69 6e 20 74 68 65 20 72 65 63  bytes in the rec
5a030 6f 72 64 20 2a 2f 0a 20 20 75 33 32 20 2a 61 54  ord */.  u32 *aT
5a040 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f  ype;           /
5a050 2a 20 54 79 70 65 20 76 61 6c 75 65 73 20 66 6f  * Type values fo
5a060 72 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 69 6e  r all entries in
5a070 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20   the record */. 
5a080 20 75 33 32 20 2a 61 4f 66 66 73 65 74 3b 20 20   u32 *aOffset;  
5a090 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 64         /* Cached
5a0a0 20 6f 66 66 73 65 74 73 20 74 6f 20 74 68 65 20   offsets to the 
5a0b0 73 74 61 72 74 20 6f 66 20 65 61 63 68 20 63 6f  start of each co
5a0c0 6c 75 6d 6e 73 20 64 61 74 61 20 2a 2f 0a 20 20  lumns data */.  
5a0d0 75 38 20 2a 61 52 6f 77 3b 20 20 20 20 20 20 20  u8 *aRow;       
5a0e0 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 66 6f        /* Data fo
5a0f0 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f  r the current ro
5a100 77 2c 20 69 66 20 61 6c 6c 20 6f 6e 20 6f 6e 65  w, if all on one
5a110 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 74 79 70 65   page */.};.type
5a120 64 65 66 20 73 74 72 75 63 74 20 43 75 72 73 6f  def struct Curso
5a130 72 20 43 75 72 73 6f 72 3b 0a 0a 2f 2a 0a 2a 2a  r Cursor;../*.**
5a140 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
5a150 20 6f 66 20 73 74 72 69 6e 67 20 73 74 6f 72 61   of string stora
5a160 67 65 20 73 70 61 63 65 20 61 76 61 69 6c 61 62  ge space availab
5a170 6c 65 20 74 6f 20 65 61 63 68 20 73 74 61 63 6b  le to each stack
5a180 0a 2a 2a 20 6c 61 79 65 72 20 77 69 74 68 6f 75  .** layer withou
5a190 74 20 68 61 76 69 6e 67 20 74 6f 20 6d 61 6c 6c  t having to mall
5a1a0 6f 63 2e 20 20 4e 42 46 53 20 69 73 20 73 68 6f  oc.  NBFS is sho
5a1b0 72 74 20 66 6f 72 20 4e 75 6d 62 65 72 20 6f 66  rt for Number of
5a1c0 20 42 79 74 65 73 0a 2a 2a 20 46 6f 72 20 53 74   Bytes.** For St
5a1d0 72 69 6e 67 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e  rings..*/.#defin
5a1e0 65 20 4e 42 46 53 20 33 32 0a 0a 2f 2a 0a 2a 2a  e NBFS 32../*.**
5a1f0 20 41 20 76 61 6c 75 65 20 66 6f 72 20 43 75 72   A value for Cur
5a200 73 6f 72 2e 63 61 63 68 65 56 61 6c 69 64 20 74  sor.cacheValid t
5a210 68 61 74 20 6d 65 61 6e 73 20 74 68 65 20 63 61  hat means the ca
5a220 63 68 65 20 69 73 20 61 6c 77 61 79 73 20 69 6e  che is always in
5a230 76 61 6c 69 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e  valid..*/.#defin
5a240 65 20 43 41 43 48 45 5f 53 54 41 4c 45 20 30 0a  e CACHE_STALE 0.
5a250 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 6c  ./*.** Internall
5a260 79 2c 20 74 68 65 20 76 64 62 65 20 6d 61 6e 69  y, the vdbe mani
5a270 70 75 6c 61 74 65 73 20 6e 65 61 72 6c 79 20 61  pulates nearly a
5a280 6c 6c 20 53 51 4c 20 76 61 6c 75 65 73 20 61 73  ll SQL values as
5a290 20 4d 65 6d 0a 2a 2a 20 73 74 72 75 63 74 75 72   Mem.** structur
5a2a0 65 73 2e 20 45 61 63 68 20 4d 65 6d 20 73 74 72  es. Each Mem str
5a2b0 75 63 74 20 6d 61 79 20 63 61 63 68 65 20 6d 75  uct may cache mu
5a2c0 6c 74 69 70 6c 65 20 72 65 70 72 65 73 65 6e 74  ltiple represent
5a2d0 61 74 69 6f 6e 73 20 28 73 74 72 69 6e 67 2c 0a  ations (string,.
5a2e0 2a 2a 20 69 6e 74 65 67 65 72 20 65 74 63 2e 29  ** integer etc.)
5a2f0 20 6f 66 20 74 68 65 20 73 61 6d 65 20 76 61 6c   of the same val
5a300 75 65 2e 20 20 41 20 76 61 6c 75 65 20 28 61 6e  ue.  A value (an
5a310 64 20 74 68 65 72 65 66 6f 72 65 20 4d 65 6d 20  d therefore Mem 
5a320 73 74 72 75 63 74 75 72 65 29 0a 2a 2a 20 68 61  structure).** ha
5a330 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  s the following 
5a340 70 72 6f 70 65 72 74 69 65 73 3a 0a 2a 2a 0a 2a  properties:.**.*
5a350 2a 20 45 61 63 68 20 76 61 6c 75 65 20 68 61 73  * Each value has
5a360 20 61 20 6d 61 6e 69 66 65 73 74 20 74 79 70 65   a manifest type
5a370 2e 20 54 68 65 20 6d 61 6e 69 66 65 73 74 20 74  . The manifest t
5a380 79 70 65 20 6f 66 20 74 68 65 20 76 61 6c 75 65  ype of the value
5a390 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 61 20   stored.** in a 
5a3a0 4d 65 6d 20 73 74 72 75 63 74 20 69 73 20 72 65  Mem struct is re
5a3b0 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 4d 65  turned by the Me
5a3c0 6d 54 79 70 65 28 4d 65 6d 2a 29 20 6d 61 63 72  mType(Mem*) macr
5a3d0 6f 2e 20 54 68 65 20 74 79 70 65 20 69 73 0a 2a  o. The type is.*
5a3e0 2a 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f  * one of SQLITE_
5a3f0 4e 55 4c 4c 2c 20 53 51 4c 49 54 45 5f 49 4e 54  NULL, SQLITE_INT
5a400 45 47 45 52 2c 20 53 51 4c 49 54 45 5f 52 45 41  EGER, SQLITE_REA
5a410 4c 2c 20 53 51 4c 49 54 45 5f 54 45 58 54 20 6f  L, SQLITE_TEXT o
5a420 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 4c 4f 42  r.** SQLITE_BLOB
5a430 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 20  ..*/.struct Mem 
5a440 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20  {.  union {.    
5a450 69 36 34 20 69 3b 20 20 20 20 20 20 20 20 20 20  i64 i;          
5a460 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76      /* Integer v
5a470 61 6c 75 65 2e 20 4f 72 20 46 75 6e 63 44 65 66  alue. Or FuncDef
5a480 2a 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d 4d 45  * when flags==ME
5a490 4d 5f 41 67 67 20 2a 2f 0a 20 20 20 20 46 75 6e  M_Agg */.    Fun
5a4a0 63 44 65 66 20 2a 70 44 65 66 3b 20 20 20 20 20  cDef *pDef;     
5a4b0 20 2f 2a 20 55 73 65 64 20 6f 6e 6c 79 20 77 68   /* Used only wh
5a4c0 65 6e 20 66 6c 61 67 73 3d 3d 4d 45 4d 5f 41 67  en flags==MEM_Ag
5a4d0 67 20 2a 2f 0a 20 20 7d 20 75 3b 0a 20 20 64 6f  g */.  } u;.  do
5a4e0 75 62 6c 65 20 72 3b 20 20 20 20 20 20 20 20 20  uble r;         
5a4f0 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c 75 65 20    /* Real value 
5a500 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 3b 20 20 20  */.  char *z;   
5a510 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69           /* Stri
5a520 6e 67 20 6f 72 20 42 4c 4f 42 20 76 61 6c 75 65  ng or BLOB value
5a530 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20   */.  int n;    
5a540 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
5a550 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72  ber of character
5a560 73 20 69 6e 20 73 74 72 69 6e 67 20 76 61 6c 75  s in string valu
5a570 65 2c 20 69 6e 63 6c 75 64 69 6e 67 20 27 5c 30  e, including '\0
5a580 27 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67 73  ' */.  u16 flags
5a590 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f  ;          /* So
5a5a0 6d 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f  me combination o
5a5b0 66 20 4d 45 4d 5f 4e 75 6c 6c 2c 20 4d 45 4d 5f  f MEM_Null, MEM_
5a5c0 53 74 72 2c 20 4d 45 4d 5f 44 79 6e 2c 20 65 74  Str, MEM_Dyn, et
5a5d0 63 2e 20 2a 2f 0a 20 20 75 38 20 20 74 79 70 65  c. */.  u8  type
5a5e0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  ;           /* O
5a5f0 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 4e 55 4c  ne of SQLITE_NUL
5a600 4c 2c 20 53 51 4c 49 54 45 5f 54 45 58 54 2c 20  L, SQLITE_TEXT, 
5a610 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 2c 20  SQLITE_INTEGER, 
5a620 65 74 63 20 2a 2f 0a 20 20 75 38 20 20 65 6e 63  etc */.  u8  enc
5a630 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
5a640 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c  SQLITE_UTF8, SQL
5a650 49 54 45 5f 55 54 46 31 36 42 45 2c 20 53 51 4c  ITE_UTF16BE, SQL
5a660 49 54 45 5f 55 54 46 31 36 4c 45 20 2a 2f 0a 20  ITE_UTF16LE */. 
5a670 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f   void (*xDel)(vo
5a680 69 64 20 2a 29 3b 20 20 2f 2a 20 49 66 20 6e 6f  id *);  /* If no
5a690 74 20 6e 75 6c 6c 2c 20 63 61 6c 6c 20 74 68 69  t null, call thi
5a6a0 73 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 64 65  s function to de
5a6b0 6c 65 74 65 20 4d 65 6d 2e 7a 20 2a 2f 0a 20 20  lete Mem.z */.  
5a6c0 63 68 61 72 20 7a 53 68 6f 72 74 5b 4e 42 46 53  char zShort[NBFS
5a6d0 5d 3b 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72  ];  /* Space for
5a6e0 20 73 68 6f 72 74 20 73 74 72 69 6e 67 73 20 2a   short strings *
5a6f0 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72  /.};.typedef str
5a700 75 63 74 20 4d 65 6d 20 4d 65 6d 3b 0a 0a 2f 2a  uct Mem Mem;../*
5a710 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20   One or more of 
5a720 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c  the following fl
5a730 61 67 73 20 61 72 65 20 73 65 74 20 74 6f 20 69  ags are set to i
5a740 6e 64 69 63 61 74 65 20 74 68 65 20 76 61 6c 69  ndicate the vali
5a750 64 4f 4b 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74  dOK.** represent
5a760 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 76 61  ations of the va
5a770 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68  lue stored in th
5a780 65 20 4d 65 6d 20 73 74 72 75 63 74 2e 0a 2a 2a  e Mem struct..**
5a790 0a 2a 2a 20 49 66 20 74 68 65 20 4d 45 4d 5f 4e  .** If the MEM_N
5a7a0 75 6c 6c 20 66 6c 61 67 20 69 73 20 73 65 74 2c  ull flag is set,
5a7b0 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20   then the value 
5a7c0 69 73 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 76  is an SQL NULL v
5a7d0 61 6c 75 65 2e 0a 2a 2a 20 4e 6f 20 6f 74 68 65  alue..** No othe
5a7e0 72 20 66 6c 61 67 73 20 6d 61 79 20 62 65 20 73  r flags may be s
5a7f0 65 74 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e  et in this case.
5a800 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4d 45  .**.** If the ME
5a810 4d 5f 53 74 72 20 66 6c 61 67 20 69 73 20 73 65  M_Str flag is se
5a820 74 20 74 68 65 6e 20 4d 65 6d 2e 7a 20 70 6f 69  t then Mem.z poi
5a830 6e 74 73 20 61 74 20 61 20 73 74 72 69 6e 67 20  nts at a string 
5a840 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a  representation..
5a850 2a 2a 20 55 73 75 61 6c 6c 79 20 74 68 69 73 20  ** Usually this 
5a860 69 73 20 65 6e 63 6f 64 65 64 20 69 6e 20 74 68  is encoded in th
5a870 65 20 73 61 6d 65 20 75 6e 69 63 6f 64 65 20 65  e same unicode e
5a880 6e 63 6f 64 69 6e 67 20 61 73 20 74 68 65 20 6d  ncoding as the m
5a890 61 69 6e 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  ain.** database 
5a8a0 28 73 65 65 20 62 65 6c 6f 77 20 66 6f 72 20 65  (see below for e
5a8b0 78 63 65 70 74 69 6f 6e 73 29 2e 20 49 66 20 74  xceptions). If t
5a8c0 68 65 20 4d 45 4d 5f 54 65 72 6d 20 66 6c 61 67  he MEM_Term flag
5a8d0 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 73 65 74 2c   is also.** set,
5a8e0 20 74 68 65 6e 20 74 68 65 20 73 74 72 69 6e 67   then the string
5a8f0 20 69 73 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74   is nul terminat
5a900 65 64 2e 20 54 68 65 20 4d 45 4d 5f 49 6e 74 20  ed. The MEM_Int 
5a910 61 6e 64 20 4d 45 4d 5f 52 65 61 6c 20 0a 2a 2a  and MEM_Real .**
5a920 20 66 6c 61 67 73 20 6d 61 79 20 63 6f 65 78 69   flags may coexi
5a930 73 74 20 77 69 74 68 20 74 68 65 20 4d 45 4d 5f  st with the MEM_
5a940 53 74 72 20 66 6c 61 67 2e 0a 2a 2a 0a 2a 2a 20  Str flag..**.** 
5a950 4d 75 6c 74 69 70 6c 65 20 6f 66 20 74 68 65 73  Multiple of thes
5a960 65 20 76 61 6c 75 65 73 20 63 61 6e 20 61 70 70  e values can app
5a970 65 61 72 20 69 6e 20 4d 65 6d 2e 66 6c 61 67 73  ear in Mem.flags
5a980 2e 20 20 42 75 74 20 6f 6e 6c 79 20 6f 6e 65 0a  .  But only one.
5a990 2a 2a 20 61 74 20 61 20 74 69 6d 65 20 63 61 6e  ** at a time can
5a9a0 20 61 70 70 65 61 72 20 69 6e 20 4d 65 6d 2e 74   appear in Mem.t
5a9b0 79 70 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ype..*/.#define 
5a9c0 4d 45 4d 5f 4e 75 6c 6c 20 20 20 20 20 20 30 78  MEM_Null      0x
5a9d0 30 30 30 31 20 20 20 2f 2a 20 56 61 6c 75 65 20  0001   /* Value 
5a9e0 69 73 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69  is NULL */.#defi
5a9f0 6e 65 20 4d 45 4d 5f 53 74 72 20 20 20 20 20 20  ne MEM_Str      
5aa00 20 30 78 30 30 30 32 20 20 20 2f 2a 20 56 61 6c   0x0002   /* Val
5aa10 75 65 20 69 73 20 61 20 73 74 72 69 6e 67 20 2a  ue is a string *
5aa20 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 49 6e  /.#define MEM_In
5aa30 74 20 20 20 20 20 20 20 30 78 30 30 30 34 20 20  t       0x0004  
5aa40 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 61 6e 20   /* Value is an 
5aa50 69 6e 74 65 67 65 72 20 2a 2f 0a 23 64 65 66 69  integer */.#defi
5aa60 6e 65 20 4d 45 4d 5f 52 65 61 6c 20 20 20 20 20  ne MEM_Real     
5aa70 20 30 78 30 30 30 38 20 20 20 2f 2a 20 56 61 6c   0x0008   /* Val
5aa80 75 65 20 69 73 20 61 20 72 65 61 6c 20 6e 75 6d  ue is a real num
5aa90 62 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  ber */.#define M
5aaa0 45 4d 5f 42 6c 6f 62 20 20 20 20 20 20 30 78 30  EM_Blob      0x0
5aab0 30 31 30 20 20 20 2f 2a 20 56 61 6c 75 65 20 69  010   /* Value i
5aac0 73 20 61 20 42 4c 4f 42 20 2a 2f 0a 0a 2f 2a 20  s a BLOB */../* 
5aad0 57 68 65 6e 65 76 65 72 20 4d 65 6d 20 63 6f 6e  Whenever Mem con
5aae0 74 61 69 6e 73 20 61 20 76 61 6c 69 64 20 73 74  tains a valid st
5aaf0 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 72 65 70  ring or blob rep
5ab00 72 65 73 65 6e 74 61 74 69 6f 6e 2c 20 6f 6e 65  resentation, one
5ab10 20 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f   of.** the follo
5ab20 77 69 6e 67 20 66 6c 61 67 73 20 6d 75 73 74 20  wing flags must 
5ab30 62 65 20 73 65 74 20 74 6f 20 64 65 74 65 72 6d  be set to determ
5ab40 69 6e 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 6d  ine the memory m
5ab50 61 6e 61 67 65 6d 65 6e 74 0a 2a 2a 20 70 6f 6c  anagement.** pol
5ab60 69 63 79 20 66 6f 72 20 4d 65 6d 2e 7a 2e 20 20  icy for Mem.z.  
5ab70 54 68 65 20 4d 45 4d 5f 54 65 72 6d 20 66 6c 61  The MEM_Term fla
5ab80 67 20 74 65 6c 6c 73 20 75 73 20 77 68 65 74 68  g tells us wheth
5ab90 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 0a 2a 2a  er or not the.**
5aba0 20 73 74 72 69 6e 67 20 69 73 20 5c 30 30 30 20   string is \000 
5abb0 6f 72 20 5c 75 30 30 30 30 20 74 65 72 6d 69 6e  or \u0000 termin
5abc0 61 74 65 64 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ated.*/.#define 
5abd0 4d 45 4d 5f 54 65 72 6d 20 20 20 20 20 20 30 78  MEM_Term      0x
5abe0 30 30 32 30 20 20 20 2f 2a 20 53 74 72 69 6e 67  0020   /* String
5abf0 20 72 65 70 20 69 73 20 6e 75 6c 20 74 65 72 6d   rep is nul term
5ac00 69 6e 61 74 65 64 20 2a 2f 0a 23 64 65 66 69 6e  inated */.#defin
5ac10 65 20 4d 45 4d 5f 44 79 6e 20 20 20 20 20 20 20  e MEM_Dyn       
5ac20 30 78 30 30 34 30 20 20 20 2f 2a 20 4e 65 65 64  0x0040   /* Need
5ac30 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 46   to call sqliteF
5ac40 72 65 65 28 29 20 6f 6e 20 4d 65 6d 2e 7a 20 2a  ree() on Mem.z *
5ac50 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 53 74  /.#define MEM_St
5ac60 61 74 69 63 20 20 20 20 30 78 30 30 38 30 20 20  atic    0x0080  
5ac70 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73   /* Mem.z points
5ac80 20 74 6f 20 61 20 73 74 61 74 69 63 20 73 74 72   to a static str
5ac90 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  ing */.#define M
5aca0 45 4d 5f 45 70 68 65 6d 20 20 20 20 20 30 78 30  EM_Ephem     0x0
5acb0 31 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70  100   /* Mem.z p
5acc0 6f 69 6e 74 73 20 74 6f 20 61 6e 20 65 70 68 65  oints to an ephe
5acd0 6d 65 72 61 6c 20 73 74 72 69 6e 67 20 2a 2f 0a  meral string */.
5ace0 23 64 65 66 69 6e 65 20 4d 45 4d 5f 53 68 6f 72  #define MEM_Shor
5acf0 74 20 20 20 20 20 30 78 30 32 30 30 20 20 20 2f  t     0x0200   /
5ad00 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20 74  * Mem.z points t
5ad10 6f 20 4d 65 6d 2e 7a 53 68 6f 72 74 20 2a 2f 0a  o Mem.zShort */.
5ad20 23 64 65 66 69 6e 65 20 4d 45 4d 5f 41 67 67 20  #define MEM_Agg 
5ad30 20 20 20 20 20 20 30 78 30 34 30 30 20 20 20 2f        0x0400   /
5ad40 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20 74  * Mem.z points t
5ad50 6f 20 61 6e 20 61 67 67 20 66 75 6e 63 74 69 6f  o an agg functio
5ad60 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 23 64 65  n context */.#de
5ad70 66 69 6e 65 20 4d 45 4d 5f 5a 65 72 6f 20 20 20  fine MEM_Zero   
5ad80 20 20 20 30 78 30 38 30 30 20 20 20 2f 2a 20 4d     0x0800   /* M
5ad90 65 6d 2e 69 20 63 6f 6e 74 61 69 6e 73 20 63 6f  em.i contains co
5ada0 75 6e 74 20 6f 66 20 30 73 20 61 70 70 65 6e 64  unt of 0s append
5adb0 65 64 20 74 6f 20 62 6c 6f 62 20 2a 2f 0a 0a 23  ed to blob */..#
5adc0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
5add0 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 23 75 6e  T_INCRBLOB.  #un
5ade0 64 65 66 20 4d 45 4d 5f 5a 65 72 6f 0a 20 20 23  def MEM_Zero.  #
5adf0 64 65 66 69 6e 65 20 4d 45 4d 5f 5a 65 72 6f 20  define MEM_Zero 
5ae00 30 78 30 30 30 30 0a 23 65 6e 64 69 66 0a 0a 0a  0x0000.#endif...
5ae10 2f 2a 20 41 20 56 64 62 65 46 75 6e 63 20 69 73  /* A VdbeFunc is
5ae20 20 6a 75 73 74 20 61 20 46 75 6e 63 44 65 66 20   just a FuncDef 
5ae30 28 64 65 66 69 6e 65 64 20 69 6e 20 73 71 6c 69  (defined in sqli
5ae40 74 65 49 6e 74 2e 68 29 20 74 68 61 74 20 63 6f  teInt.h) that co
5ae50 6e 74 61 69 6e 73 0a 2a 2a 20 61 64 64 69 74 69  ntains.** additi
5ae60 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
5ae70 20 61 62 6f 75 74 20 61 75 78 69 6c 69 61 72 79   about auxiliary
5ae80 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 62 6f 75   information bou
5ae90 6e 64 20 74 6f 20 61 72 67 75 6d 65 6e 74 73 0a  nd to arguments.
5aea0 2a 2a 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69  ** of the functi
5aeb0 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 75 73 65  on.  This is use
5aec0 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74  d to implement t
5aed0 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61  he sqlite3_get_a
5aee0 75 78 64 61 74 61 28 29 0a 2a 2a 20 61 6e 64 20  uxdata().** and 
5aef0 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64  sqlite3_set_auxd
5af00 61 74 61 28 29 20 41 50 49 73 2e 20 20 54 68 65  ata() APIs.  The
5af10 20 22 61 75 78 64 61 74 61 22 20 69 73 20 73 6f   "auxdata" is so
5af20 6d 65 20 61 75 78 69 6c 69 61 72 79 20 64 61 74  me auxiliary dat
5af30 61 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65  a.** that can be
5af40 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
5af50 20 61 20 63 6f 6e 73 74 61 6e 74 20 61 72 67 75   a constant argu
5af60 6d 65 6e 74 20 74 6f 20 61 20 66 75 6e 63 74 69  ment to a functi
5af70 6f 6e 2e 20 20 54 68 69 73 0a 2a 2a 20 61 6c 6c  on.  This.** all
5af80 6f 77 73 20 66 75 6e 63 74 69 6f 6e 73 20 73 75  ows functions su
5af90 63 68 20 61 73 20 22 72 65 67 65 78 70 22 20 74  ch as "regexp" t
5afa0 6f 20 63 6f 6d 70 69 6c 65 20 74 68 65 69 72 20  o compile their 
5afb0 63 6f 6e 73 74 61 6e 74 20 72 65 67 75 6c 61 72  constant regular
5afc0 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 61  .** expression a
5afd0 72 67 75 6d 65 6e 74 20 6f 6e 63 65 20 61 6e 64  rgument once and
5afe0 20 72 65 75 73 65 64 20 74 68 65 20 63 6f 6d 70   reused the comp
5aff0 69 6c 65 64 20 63 6f 64 65 20 66 6f 72 20 6d 75  iled code for mu
5b000 6c 74 69 70 6c 65 0a 2a 2a 20 69 6e 76 6f 63 61  ltiple.** invoca
5b010 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63 74  tions..*/.struct
5b020 20 56 64 62 65 46 75 6e 63 20 7b 0a 20 20 46 75   VdbeFunc {.  Fu
5b030 6e 63 44 65 66 20 2a 70 46 75 6e 63 3b 20 20 20  ncDef *pFunc;   
5b040 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
5b050 68 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66  he definition of
5b060 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f   the function */
5b070 0a 20 20 69 6e 74 20 6e 41 75 78 3b 20 20 20 20  .  int nAux;    
5b080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b090 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
5b0a0 74 72 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 20  tries allocated 
5b0b0 66 6f 72 20 61 70 41 75 78 5b 5d 20 2a 2f 0a 20  for apAux[] */. 
5b0c0 20 73 74 72 75 63 74 20 41 75 78 44 61 74 61 20   struct AuxData 
5b0d0 7b 0a 20 20 20 20 76 6f 69 64 20 2a 70 41 75 78  {.    void *pAux
5b0e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
5b0f0 20 20 20 20 2f 2a 20 41 75 78 20 64 61 74 61 20      /* Aux data 
5b100 66 6f 72 20 74 68 65 20 69 2d 74 68 20 61 72 67  for the i-th arg
5b110 75 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 76 6f 69  ument */.    voi
5b120 64 20 28 2a 78 44 65 6c 65 74 65 29 28 76 6f 69  d (*xDelete)(voi
5b130 64 20 2a 29 3b 20 20 20 20 20 20 2f 2a 20 44 65  d *);      /* De
5b140 73 74 72 75 63 74 6f 72 20 66 6f 72 20 74 68 65  structor for the
5b150 20 61 75 78 20 64 61 74 61 20 2a 2f 0a 20 20 7d   aux data */.  }
5b160 20 61 70 41 75 78 5b 31 5d 3b 20 20 20 20 20 20   apAux[1];      
5b170 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5b180 4f 6e 65 20 73 6c 6f 74 20 66 6f 72 20 65 61 63  One slot for eac
5b190 68 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d  h function argum
5b1a0 65 6e 74 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65  ent */.};.typede
5b1b0 66 20 73 74 72 75 63 74 20 56 64 62 65 46 75 6e  f struct VdbeFun
5b1c0 63 20 56 64 62 65 46 75 6e 63 3b 0a 0a 2f 2a 0a  c VdbeFunc;../*.
5b1d0 2a 2a 20 54 68 65 20 22 63 6f 6e 74 65 78 74 22  ** The "context"
5b1e0 20 61 72 67 75 6d 65 6e 74 20 66 6f 72 20 61 20   argument for a 
5b1f0 69 6e 73 74 61 6c 6c 61 62 6c 65 20 66 75 6e 63  installable func
5b200 74 69 6f 6e 2e 20 20 41 20 70 6f 69 6e 74 65 72  tion.  A pointer
5b210 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e   to an.** instan
5b220 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63  ce of this struc
5b230 74 75 72 65 20 69 73 20 74 68 65 20 66 69 72 73  ture is the firs
5b240 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  t argument to th
5b250 65 20 72 6f 75 74 69 6e 65 73 20 75 73 65 64 0a  e routines used.
5b260 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  ** implement the
5b270 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 2e 0a   SQL functions..
5b280 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 61  **.** There is a
5b290 20 74 79 70 65 64 65 66 20 66 6f 72 20 74 68 69   typedef for thi
5b2a0 73 20 73 74 72 75 63 74 75 72 65 20 69 6e 20 73  s structure in s
5b2b0 71 6c 69 74 65 2e 68 2e 20 20 53 6f 20 61 6c 6c  qlite.h.  So all
5b2c0 20 72 6f 75 74 69 6e 65 73 2c 0a 2a 2a 20 65 76   routines,.** ev
5b2d0 65 6e 20 74 68 65 20 70 75 62 6c 69 63 20 69 6e  en the public in
5b2e0 74 65 72 66 61 63 65 20 74 6f 20 53 51 4c 69 74  terface to SQLit
5b2f0 65 2c 20 63 61 6e 20 75 73 65 20 61 20 70 6f 69  e, can use a poi
5b300 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74 72  nter to this str
5b310 75 63 74 75 72 65 2e 0a 2a 2a 20 42 75 74 20 74  ucture..** But t
5b320 68 69 73 20 66 69 6c 65 20 69 73 20 74 68 65 20  his file is the 
5b330 6f 6e 6c 79 20 70 6c 61 63 65 20 77 68 65 72 65  only place where
5b340 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 64 65   the internal de
5b350 74 61 69 6c 73 20 6f 66 20 74 68 69 73 0a 2a 2a  tails of this.**
5b360 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 6b   structure are k
5b370 6e 6f 77 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  nown..**.** This
5b380 20 73 74 72 75 63 74 75 72 65 20 69 73 20 64 65   structure is de
5b390 66 69 6e 65 64 20 69 6e 73 69 64 65 20 6f 66 20  fined inside of 
5b3a0 76 64 62 65 49 6e 74 2e 68 20 62 65 63 61 75 73  vdbeInt.h becaus
5b3b0 65 20 69 74 20 75 73 65 73 20 73 75 62 73 74 72  e it uses substr
5b3c0 75 63 74 75 72 65 73 0a 2a 2a 20 28 4d 65 6d 29  uctures.** (Mem)
5b3d0 20 77 68 69 63 68 20 61 72 65 20 6f 6e 6c 79 20   which are only 
5b3e0 64 65 66 69 6e 65 64 20 74 68 65 72 65 2e 0a 2a  defined there..*
5b3f0 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  /.struct sqlite3
5b400 5f 63 6f 6e 74 65 78 74 20 7b 0a 20 20 46 75 6e  _context {.  Fun
5b410 63 44 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20  cDef *pFunc;    
5b420 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
5b430 20 66 75 6e 63 74 69 6f 6e 20 69 6e 66 6f 72 6d   function inform
5b440 61 74 69 6f 6e 2e 20 20 4d 55 53 54 20 42 45 20  ation.  MUST BE 
5b450 46 49 52 53 54 20 2a 2f 0a 20 20 56 64 62 65 46  FIRST */.  VdbeF
5b460 75 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 3b 20  unc *pVdbeFunc; 
5b470 20 2f 2a 20 41 75 78 69 6c 61 72 79 20 64 61 74   /* Auxilary dat
5b480 61 2c 20 69 66 20 63 72 65 61 74 65 64 2e 20 2a  a, if created. *
5b490 2f 0a 20 20 4d 65 6d 20 73 3b 20 20 20 20 20 20  /.  Mem s;      
5b4a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
5b4b0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73   return value is
5b4c0 20 73 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a   stored here */.
5b4d0 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 20    Mem *pMem;    
5b4e0 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72          /* Memor
5b4f0 79 20 63 65 6c 6c 20 75 73 65 64 20 74 6f 20 73  y cell used to s
5b500 74 6f 72 65 20 61 67 67 72 65 67 61 74 65 20 63  tore aggregate c
5b510 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 75 38 20 69  ontext */.  u8 i
5b520 73 45 72 72 6f 72 3b 20 20 20 20 20 20 20 20 20  sError;         
5b530 20 20 2f 2a 20 53 65 74 20 74 6f 20 74 72 75 65    /* Set to true
5b540 20 66 6f 72 20 61 6e 20 65 72 72 6f 72 20 2a 2f   for an error */
5b550 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c  .  CollSeq *pCol
5b560 6c 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c  l;       /* Coll
5b570 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a  ating sequence *
5b580 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 53 65  /.};../*.** A Se
5b590 74 20 73 74 72 75 63 74 75 72 65 20 69 73 20 75  t structure is u
5b5a0 73 65 64 20 66 6f 72 20 71 75 69 63 6b 20 74 65  sed for quick te
5b5b0 73 74 69 6e 67 20 74 6f 20 73 65 65 20 69 66 20  sting to see if 
5b5c0 61 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 70 61  a value.** is pa
5b5d0 72 74 20 6f 66 20 61 20 73 6d 61 6c 6c 20 73 65  rt of a small se
5b5e0 74 2e 20 20 53 65 74 73 20 61 72 65 20 75 73 65  t.  Sets are use
5b5f0 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 63  d to implement c
5b600 6f 64 65 20 6c 69 6b 65 0a 2a 2a 20 74 68 69 73  ode like.** this
5b610 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  :.**            
5b620 78 2e 79 20 49 4e 20 28 27 68 69 27 2c 27 68 6f  x.y IN ('hi','ho
5b630 6f 27 2c 27 68 75 6d 27 29 0a 2a 2f 0a 74 79 70  o','hum').*/.typ
5b640 65 64 65 66 20 73 74 72 75 63 74 20 53 65 74 20  edef struct Set 
5b650 53 65 74 3b 0a 73 74 72 75 63 74 20 53 65 74 20  Set;.struct Set 
5b660 7b 0a 20 20 48 61 73 68 20 68 61 73 68 3b 20 20  {.  Hash hash;  
5b670 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20             /* A 
5b680 73 65 74 20 69 73 20 6a 75 73 74 20 61 20 68 61  set is just a ha
5b690 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 48 61  sh table */.  Ha
5b6a0 73 68 45 6c 65 6d 20 2a 70 72 65 76 3b 20 20 20  shElem *prev;   
5b6b0 20 20 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73       /* Previous
5b6c0 6c 79 20 61 63 63 65 73 73 65 64 20 68 61 73 68  ly accessed hash
5b6d0 20 65 6c 65 6d 65 6e 20 2a 2f 0a 7d 3b 0a 0a 2f   elemen */.};../
5b6e0 2a 0a 2a 2a 20 41 20 46 69 66 6f 50 61 67 65 20  *.** A FifoPage 
5b6f0 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20  structure holds 
5b700 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 6f 66  a single page of
5b710 20 76 61 6c 76 65 73 2e 20 20 50 61 67 65 73 20   valves.  Pages 
5b720 61 72 65 20 61 72 72 61 6e 67 65 64 0a 2a 2a 20  are arranged.** 
5b730 69 6e 20 61 20 6c 69 73 74 2e 0a 2a 2f 0a 74 79  in a list..*/.ty
5b740 70 65 64 65 66 20 73 74 72 75 63 74 20 46 69 66  pedef struct Fif
5b750 6f 50 61 67 65 20 46 69 66 6f 50 61 67 65 3b 0a  oPage FifoPage;.
5b760 73 74 72 75 63 74 20 46 69 66 6f 50 61 67 65 20  struct FifoPage 
5b770 7b 0a 20 20 69 6e 74 20 6e 53 6c 6f 74 3b 20 20  {.  int nSlot;  
5b780 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
5b790 20 6f 66 20 65 6e 74 72 69 65 73 20 61 53 6c 6f   of entries aSlo
5b7a0 74 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 57 72  t[] */.  int iWr
5b7b0 69 74 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 50  ite;        /* P
5b7c0 75 73 68 20 74 68 65 20 6e 65 78 74 20 76 61 6c  ush the next val
5b7d0 75 65 20 69 6e 74 6f 20 74 68 69 73 20 65 6e 74  ue into this ent
5b7e0 72 79 20 69 6e 20 61 53 6c 6f 74 5b 5d 20 2a 2f  ry in aSlot[] */
5b7f0 0a 20 20 69 6e 74 20 69 52 65 61 64 3b 20 20 20  .  int iRead;   
5b800 20 20 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68        /* Read th
5b810 65 20 6e 65 78 74 20 76 61 6c 75 65 20 66 72 6f  e next value fro
5b820 6d 20 74 68 69 73 20 65 6e 74 72 79 20 69 6e 20  m this entry in 
5b830 61 53 6c 6f 74 5b 5d 20 2a 2f 0a 20 20 46 69 66  aSlot[] */.  Fif
5b840 6f 50 61 67 65 20 2a 70 4e 65 78 74 3b 20 20 20  oPage *pNext;   
5b850 2f 2a 20 4e 65 78 74 20 70 61 67 65 20 69 6e 20  /* Next page in 
5b860 74 68 65 20 66 69 66 6f 20 2a 2f 0a 20 20 69 36  the fifo */.  i6
5b870 34 20 61 53 6c 6f 74 5b 31 5d 3b 20 20 20 20 20  4 aSlot[1];     
5b880 20 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 20   /* One or more 
5b890 73 6c 6f 74 73 20 66 6f 72 20 72 6f 77 69 64 20  slots for rowid 
5b8a0 76 61 6c 75 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  values */.};../*
5b8b0 0a 2a 2a 20 54 68 65 20 46 69 66 6f 20 73 74 72  .** The Fifo str
5b8c0 75 63 74 75 72 65 20 69 73 20 74 79 70 65 64 65  ucture is typede
5b8d0 66 2d 65 64 20 69 6e 20 76 64 62 65 49 6e 74 2e  f-ed in vdbeInt.
5b8e0 68 2e 20 20 42 75 74 20 74 68 65 20 69 6d 70 6c  h.  But the impl
5b8f0 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f 66  ementation.** of
5b900 20 74 68 61 74 20 73 74 72 75 63 74 75 72 65 20   that structure 
5b910 69 73 20 70 72 69 76 61 74 65 20 74 6f 20 74 68  is private to th
5b920 69 73 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54  is file..**.** T
5b930 68 65 20 46 69 66 6f 20 73 74 72 75 63 74 75 72  he Fifo structur
5b940 65 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20  e describes the 
5b950 65 6e 74 69 72 65 20 66 69 66 6f 2e 20 20 0a 2a  entire fifo.  .*
5b960 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
5b970 20 46 69 66 6f 20 46 69 66 6f 3b 0a 73 74 72 75   Fifo Fifo;.stru
5b980 63 74 20 46 69 66 6f 20 7b 0a 20 20 69 6e 74 20  ct Fifo {.  int 
5b990 6e 45 6e 74 72 79 3b 20 20 20 20 20 20 20 20 20  nEntry;         
5b9a0 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20  /* Total number 
5b9b0 6f 66 20 65 6e 74 72 69 65 73 20 2a 2f 0a 20 20  of entries */.  
5b9c0 46 69 66 6f 50 61 67 65 20 2a 70 46 69 72 73 74  FifoPage *pFirst
5b9d0 3b 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61 67  ;   /* First pag
5b9e0 65 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f  e on the list */
5b9f0 0a 20 20 46 69 66 6f 50 61 67 65 20 2a 70 4c 61  .  FifoPage *pLa
5ba00 73 74 3b 20 20 20 20 2f 2a 20 4c 61 73 74 20 70  st;    /* Last p
5ba10 61 67 65 20 6f 6e 20 74 68 65 20 6c 69 73 74 20  age on the list 
5ba20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 43  */.};../*.** A C
5ba30 6f 6e 74 65 78 74 20 73 74 6f 72 65 73 20 74 68  ontext stores th
5ba40 65 20 6c 61 73 74 20 69 6e 73 65 72 74 20 72 6f  e last insert ro
5ba50 77 69 64 2c 20 74 68 65 20 6c 61 73 74 20 73 74  wid, the last st
5ba60 61 74 65 6d 65 6e 74 20 63 68 61 6e 67 65 20 63  atement change c
5ba70 6f 75 6e 74 2c 0a 2a 2a 20 61 6e 64 20 74 68 65  ount,.** and the
5ba80 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 6d 65   current stateme
5ba90 6e 74 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 20  nt change count 
5baa0 28 69 2e 65 2e 20 63 68 61 6e 67 65 73 20 73 69  (i.e. changes si
5bab0 6e 63 65 20 6c 61 73 74 20 73 74 61 74 65 6d 65  nce last stateme
5bac0 6e 74 29 2e 0a 2a 2a 20 54 68 65 20 63 75 72 72  nt)..** The curr
5bad0 65 6e 74 20 6b 65 79 6c 69 73 74 20 69 73 20 61  ent keylist is a
5bae0 6c 73 6f 20 73 74 6f 72 65 64 20 69 6e 20 74 68  lso stored in th
5baf0 65 20 63 6f 6e 74 65 78 74 2e 0a 2a 2a 20 45 6c  e context..** El
5bb00 65 6d 65 6e 74 73 20 6f 66 20 43 6f 6e 74 65 78  ements of Contex
5bb10 74 20 73 74 72 75 63 74 75 72 65 20 74 79 70 65  t structure type
5bb20 20 6d 61 6b 65 20 75 70 20 74 68 65 20 43 6f 6e   make up the Con
5bb30 74 65 78 74 53 74 61 63 6b 2c 20 77 68 69 63 68  textStack, which
5bb40 20 69 73 0a 2a 2a 20 75 70 64 61 74 65 64 20 62   is.** updated b
5bb50 79 20 74 68 65 20 43 6f 6e 74 65 78 74 50 75 73  y the ContextPus
5bb60 68 20 61 6e 64 20 43 6f 6e 74 65 78 74 50 6f 70  h and ContextPop
5bb70 20 6f 70 63 6f 64 65 73 20 28 75 73 65 64 20 62   opcodes (used b
5bb80 79 20 74 72 69 67 67 65 72 73 29 2e 0a 2a 2a 20  y triggers)..** 
5bb90 54 68 65 20 63 6f 6e 74 65 78 74 20 69 73 20 70  The context is p
5bba0 75 73 68 65 64 20 62 65 66 6f 72 65 20 65 78 65  ushed before exe
5bbb0 63 75 74 69 6e 67 20 61 20 74 72 69 67 67 65 72  cuting a trigger
5bbc0 20 61 20 70 6f 70 70 65 64 20 77 68 65 6e 20 74   a popped when t
5bbd0 68 65 0a 2a 2a 20 74 72 69 67 67 65 72 20 66 69  he.** trigger fi
5bbe0 6e 69 73 68 65 73 2e 0a 2a 2f 0a 74 79 70 65 64  nishes..*/.typed
5bbf0 65 66 20 73 74 72 75 63 74 20 43 6f 6e 74 65 78  ef struct Contex
5bc00 74 20 43 6f 6e 74 65 78 74 3b 0a 73 74 72 75 63  t Context;.struc
5bc10 74 20 43 6f 6e 74 65 78 74 20 7b 0a 20 20 69 36  t Context {.  i6
5bc20 34 20 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 20  4 lastRowid;    
5bc30 2f 2a 20 4c 61 73 74 20 69 6e 73 65 72 74 20 72  /* Last insert r
5bc40 6f 77 69 64 20 28 73 71 6c 69 74 65 33 2e 6c 61  owid (sqlite3.la
5bc50 73 74 52 6f 77 69 64 29 20 2a 2f 0a 20 20 69 6e  stRowid) */.  in
5bc60 74 20 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20  t nChange;      
5bc70 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 63 68 61  /* Statement cha
5bc80 6e 67 65 73 20 28 56 64 62 65 2e 6e 43 68 61 6e  nges (Vdbe.nChan
5bc90 67 65 73 29 20 20 20 20 20 2a 2f 0a 20 20 46 69  ges)     */.  Fi
5bca0 66 6f 20 73 46 69 66 6f 3b 20 20 20 20 20 20 20  fo sFifo;       
5bcb0 2f 2a 20 52 65 63 6f 72 64 73 20 74 68 61 74 20  /* Records that 
5bcc0 77 69 6c 6c 20 70 61 72 74 69 63 69 70 61 74 65  will participate
5bcd0 20 69 6e 20 61 20 44 45 4c 45 54 45 20 6f 72 20   in a DELETE or 
5bce0 55 50 44 41 54 45 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  UPDATE */.};../*
5bcf0 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
5bd00 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d  of the virtual m
5bd10 61 63 68 69 6e 65 2e 20 20 54 68 69 73 20 73 74  achine.  This st
5bd20 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73  ructure contains
5bd30 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a   the complete.**
5bd40 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 76 69   state of the vi
5bd50 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a  rtual machine..*
5bd60 2a 0a 2a 2a 20 54 68 65 20 22 73 71 6c 69 74 65  *.** The "sqlite
5bd70 33 5f 73 74 6d 74 22 20 73 74 72 75 63 74 75 72  3_stmt" structur
5bd80 65 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 69  e pointer that i
5bd90 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71  s returned by sq
5bda0 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65 28 29 0a  lite3_compile().
5bdb0 2a 2a 20 69 73 20 72 65 61 6c 6c 79 20 61 20 70  ** is really a p
5bdc0 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73  ointer to an ins
5bdd0 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74  tance of this st
5bde0 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54  ructure..**.** T
5bdf0 68 65 20 56 64 62 65 2e 69 6e 56 74 61 62 4d 65  he Vdbe.inVtabMe
5be00 74 68 6f 64 20 76 61 72 69 61 62 6c 65 20 69 73  thod variable is
5be10 20 73 65 74 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f   set to non-zero
5be20 20 66 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f   for the duratio
5be30 6e 20 6f 66 0a 2a 2a 20 61 6e 79 20 76 69 72 74  n of.** any virt
5be40 75 61 6c 20 74 61 62 6c 65 20 6d 65 74 68 6f 64  ual table method
5be50 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6d 61 64   invocations mad
5be60 65 20 62 79 20 74 68 65 20 76 64 62 65 20 70 72  e by the vdbe pr
5be70 6f 67 72 61 6d 2e 20 49 74 20 69 73 0a 2a 2a 20  ogram. It is.** 
5be80 73 65 74 20 74 6f 20 32 20 66 6f 72 20 78 44 65  set to 2 for xDe
5be90 73 74 72 6f 79 20 6d 65 74 68 6f 64 20 63 61 6c  stroy method cal
5bea0 6c 73 20 61 6e 64 20 31 20 66 6f 72 20 61 6c 6c  ls and 1 for all
5beb0 20 6f 74 68 65 72 20 6d 65 74 68 6f 64 73 2e 20   other methods. 
5bec0 54 68 69 73 0a 2a 2a 20 76 61 72 69 61 62 6c 65  This.** variable
5bed0 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 77 6f   is used for two
5bee0 20 70 75 72 70 6f 73 65 73 3a 20 74 6f 20 61 6c   purposes: to al
5bef0 6c 6f 77 20 78 44 65 73 74 72 6f 79 20 6d 65 74  low xDestroy met
5bf00 68 6f 64 73 20 74 6f 20 65 78 65 63 75 74 65 0a  hods to execute.
5bf10 2a 2a 20 22 44 52 4f 50 20 54 41 42 4c 45 22 20  ** "DROP TABLE" 
5bf20 73 74 61 74 65 6d 65 6e 74 73 20 61 6e 64 20 74  statements and t
5bf30 6f 20 70 72 65 76 65 6e 74 20 73 6f 6d 65 20 6e  o prevent some n
5bf40 61 73 74 79 20 73 69 64 65 20 65 66 66 65 63 74  asty side effect
5bf50 73 20 6f 66 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66  s of.** malloc f
5bf60 61 69 6c 75 72 65 20 77 68 65 6e 20 53 51 4c 69  ailure when SQLi
5bf70 74 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 72 65  te is invoked re
5bf80 63 75 72 73 69 76 65 6c 79 20 62 79 20 61 20 76  cursively by a v
5bf90 69 72 74 75 61 6c 20 74 61 62 6c 65 20 0a 2a 2a  irtual table .**
5bfa0 20 6d 65 74 68 6f 64 20 66 75 6e 63 74 69 6f 6e   method function
5bfb0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65  ..*/.struct Vdbe
5bfc0 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62   {.  sqlite3 *db
5bfd0 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ;        /* The 
5bfe0 77 68 6f 6c 65 20 64 61 74 61 62 61 73 65 20 2a  whole database *
5bff0 2f 0a 20 20 56 64 62 65 20 2a 70 50 72 65 76 2c  /.  Vdbe *pPrev,
5c000 2a 70 4e 65 78 74 3b 20 2f 2a 20 4c 69 6e 6b 65  *pNext; /* Linke
5c010 64 20 6c 69 73 74 20 6f 66 20 56 44 42 45 73 20  d list of VDBEs 
5c020 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 56 64  with the same Vd
5c030 62 65 2e 64 62 20 2a 2f 0a 20 20 69 6e 74 20 6e  be.db */.  int n
5c040 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  Op;            /
5c050 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69 6e 73 74  * Number of inst
5c060 72 75 63 74 69 6f 6e 73 20 69 6e 20 74 68 65 20  ructions in the 
5c070 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74  program */.  int
5c080 20 6e 4f 70 41 6c 6c 6f 63 3b 20 20 20 20 20 20   nOpAlloc;      
5c090 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c   /* Number of sl
5c0a0 6f 74 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f  ots allocated fo
5c0b0 72 20 61 4f 70 5b 5d 20 2a 2f 0a 20 20 4f 70 20  r aOp[] */.  Op 
5c0c0 2a 61 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20  *aOp;           
5c0d0 20 2f 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c   /* Space to hol
5c0e0 64 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61  d the virtual ma
5c0f0 63 68 69 6e 65 27 73 20 70 72 6f 67 72 61 6d 20  chine's program 
5c100 2a 2f 0a 20 20 69 6e 74 20 6e 4c 61 62 65 6c 3b  */.  int nLabel;
5c110 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
5c120 65 72 20 6f 66 20 6c 61 62 65 6c 73 20 75 73 65  er of labels use
5c130 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 61 62 65  d */.  int nLabe
5c140 6c 41 6c 6c 6f 63 3b 20 20 20 20 2f 2a 20 4e 75  lAlloc;    /* Nu
5c150 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c  mber of slots al
5c160 6c 6f 63 61 74 65 64 20 69 6e 20 61 4c 61 62 65  located in aLabe
5c170 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 4c  l[] */.  int *aL
5c180 61 62 65 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20  abel;        /* 
5c190 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68  Space to hold th
5c1a0 65 20 6c 61 62 65 6c 73 20 2a 2f 0a 20 20 4d 65  e labels */.  Me
5c1b0 6d 20 2a 61 53 74 61 63 6b 3b 20 20 20 20 20 20  m *aStack;      
5c1c0 20 20 2f 2a 20 54 68 65 20 6f 70 65 72 61 6e 64    /* The operand
5c1d0 20 73 74 61 63 6b 2c 20 65 78 63 65 70 74 20 73   stack, except s
5c1e0 74 72 69 6e 67 20 76 61 6c 75 65 73 20 2a 2f 0a  tring values */.
5c1f0 20 20 4d 65 6d 20 2a 70 54 6f 73 3b 20 20 20 20    Mem *pTos;    
5c200 20 20 20 20 20 20 2f 2a 20 54 6f 70 20 65 6e 74        /* Top ent
5c210 72 79 20 69 6e 20 74 68 65 20 6f 70 65 72 61 6e  ry in the operan
5c220 64 20 73 74 61 63 6b 20 2a 2f 0a 20 20 4d 65 6d  d stack */.  Mem
5c230 20 2a 2a 61 70 41 72 67 3b 20 20 20 20 20 20 20   **apArg;       
5c240 20 2f 2a 20 41 72 67 75 6d 65 6e 74 73 20 74 6f   /* Arguments to
5c250 20 63 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75   currently execu
5c260 74 69 6e 67 20 75 73 65 72 20 66 75 6e 63 74 69  ting user functi
5c270 6f 6e 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 43 6f  on */.  Mem *aCo
5c280 6c 4e 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20 43  lName;      /* C
5c290 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 74 6f 20 72  olumn names to r
5c2a0 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e  eturn */.  int n
5c2b0 43 75 72 73 6f 72 3b 20 20 20 20 20 20 20 20 2f  Cursor;        /
5c2c0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74  * Number of slot
5c2d0 73 20 69 6e 20 61 70 43 73 72 5b 5d 20 2a 2f 0a  s in apCsr[] */.
5c2e0 20 20 43 75 72 73 6f 72 20 2a 2a 61 70 43 73 72    Cursor **apCsr
5c2f0 3b 20 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6c 65  ;     /* One ele
5c300 6d 65 6e 74 20 6f 66 20 74 68 69 73 20 61 72 72  ment of this arr
5c310 61 79 20 66 6f 72 20 65 61 63 68 20 6f 70 65 6e  ay for each open
5c320 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 69 6e 74   cursor */.  int
5c330 20 6e 56 61 72 3b 20 20 20 20 20 20 20 20 20 20   nVar;          
5c340 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
5c350 74 72 69 65 73 20 69 6e 20 61 56 61 72 5b 5d 20  tries in aVar[] 
5c360 2a 2f 0a 20 20 4d 65 6d 20 2a 61 56 61 72 3b 20  */.  Mem *aVar; 
5c370 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75           /* Valu
5c380 65 73 20 66 6f 72 20 74 68 65 20 4f 50 5f 56 61  es for the OP_Va
5c390 72 69 61 62 6c 65 20 6f 70 63 6f 64 65 2e 20 2a  riable opcode. *
5c3a0 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 56 61 72  /.  char **azVar
5c3b0 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20  ;       /* Name 
5c3c0 6f 66 20 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a  of variables */.
5c3d0 20 20 69 6e 74 20 6f 6b 56 61 72 3b 20 20 20 20    int okVar;    
5c3e0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
5c3f0 20 61 7a 56 61 72 5b 5d 20 68 61 73 20 62 65 65   azVar[] has bee
5c400 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f  n initialized */
5c410 0a 20 20 69 6e 74 20 6d 61 67 69 63 3b 20 20 20  .  int magic;   
5c420 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
5c430 67 69 63 20 6e 75 6d 62 65 72 20 66 6f 72 20 73  gic number for s
5c440 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a  anity checking *
5c450 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20  /.  int nMem;   
5c460 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
5c470 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20  umber of memory 
5c480 6c 6f 63 61 74 69 6f 6e 73 20 63 75 72 72 65 6e  locations curren
5c490 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f  tly allocated */
5c4a0 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20 20 20  .  Mem *aMem;   
5c4b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
5c4c0 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f  e memory locatio
5c4d0 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 61 6c  ns */.  int nCal
5c4e0 6c 62 61 63 6b 3b 20 20 20 20 20 20 20 20 20 20  lback;          
5c4f0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 61 6c  /* Number of cal
5c500 6c 62 61 63 6b 73 20 69 6e 76 6f 6b 65 64 20 73  lbacks invoked s
5c510 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 63  o far */.  int c
5c520 61 63 68 65 43 74 72 3b 20 20 20 20 20 20 20 20  acheCtr;        
5c530 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 72 6f 77     /* Cursor row
5c540 20 63 61 63 68 65 20 67 65 6e 65 72 61 74 69 6f   cache generatio
5c550 6e 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 46  n counter */.  F
5c560 69 66 6f 20 73 46 69 66 6f 3b 20 20 20 20 20 20  ifo sFifo;      
5c570 20 20 20 20 20 20 20 2f 2a 20 41 20 6c 69 73 74         /* A list
5c580 20 6f 66 20 52 4f 57 49 44 73 20 2a 2f 0a 20 20   of ROWIDs */.  
5c590 69 6e 74 20 63 6f 6e 74 65 78 74 53 74 61 63 6b  int contextStack
5c5a0 54 6f 70 3b 20 20 20 20 2f 2a 20 49 6e 64 65 78  Top;    /* Index
5c5b0 20 6f 66 20 74 6f 70 20 65 6c 65 6d 65 6e 74 20   of top element 
5c5c0 69 6e 20 74 68 65 20 63 6f 6e 74 65 78 74 20 73  in the context s
5c5d0 74 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 63 6f  tack */.  int co
5c5e0 6e 74 65 78 74 53 74 61 63 6b 44 65 70 74 68 3b  ntextStackDepth;
5c5f0 20 20 2f 2a 20 54 68 65 20 73 69 7a 65 20 6f 66    /* The size of
5c600 20 74 68 65 20 22 63 6f 6e 74 65 78 74 22 20 73   the "context" s
5c610 74 61 63 6b 20 2a 2f 0a 20 20 43 6f 6e 74 65 78  tack */.  Contex
5c620 74 20 2a 63 6f 6e 74 65 78 74 53 74 61 63 6b 3b  t *contextStack;
5c630 20 20 2f 2a 20 53 74 61 63 6b 20 75 73 65 64 20    /* Stack used 
5c640 62 79 20 6f 70 63 6f 64 65 73 20 43 6f 6e 74 65  by opcodes Conte
5c650 78 74 50 75 73 68 20 26 20 43 6f 6e 74 65 78 74  xtPush & Context
5c660 50 6f 70 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20  Pop*/.  int pc; 
5c670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c680 2f 2a 20 54 68 65 20 70 72 6f 67 72 61 6d 20 63  /* The program c
5c690 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  ounter */.  int 
5c6a0 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
5c6b0 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20      /* Value to 
5c6c0 72 65 74 75 72 6e 20 2a 2f 0a 20 20 75 6e 73 69  return */.  unsi
5c6d0 67 6e 65 64 20 75 6e 69 71 75 65 43 6e 74 3b 20  gned uniqueCnt; 
5c6e0 20 20 20 20 2f 2a 20 55 73 65 64 20 62 79 20 4f      /* Used by O
5c6f0 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 77 68 65  P_MakeRecord whe
5c700 6e 20 50 32 21 3d 30 20 2a 2f 0a 20 20 69 6e 74  n P2!=0 */.  int
5c710 20 65 72 72 6f 72 41 63 74 69 6f 6e 3b 20 20 20   errorAction;   
5c720 20 20 20 20 20 2f 2a 20 52 65 63 6f 76 65 72 79       /* Recovery
5c730 20 61 63 74 69 6f 6e 20 74 6f 20 64 6f 20 69 6e   action to do in
5c740 20 63 61 73 65 20 6f 66 20 61 6e 20 65 72 72 6f   case of an erro
5c750 72 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 54 65 6d  r */.  int inTem
5c760 70 54 72 61 6e 73 3b 20 20 20 20 20 20 20 20 2f  pTrans;        /
5c770 2a 20 54 72 75 65 20 69 66 20 74 65 6d 70 20 64  * True if temp d
5c780 61 74 61 62 61 73 65 20 69 73 20 74 72 61 6e 73  atabase is trans
5c790 61 63 74 69 6f 6e 65 64 20 2a 2f 0a 20 20 69 6e  actioned */.  in
5c7a0 74 20 72 65 74 75 72 6e 53 74 61 63 6b 5b 31 30  t returnStack[10
5c7b0 30 5d 3b 20 20 20 2f 2a 20 52 65 74 75 72 6e 20  0];   /* Return 
5c7c0 61 64 64 72 65 73 73 20 73 74 61 63 6b 20 66 6f  address stack fo
5c7d0 72 20 4f 50 5f 47 6f 73 75 62 20 26 20 4f 50 5f  r OP_Gosub & OP_
5c7e0 52 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20  Return */.  int 
5c7f0 72 65 74 75 72 6e 44 65 70 74 68 3b 20 20 20 20  returnDepth;    
5c800 20 20 20 20 2f 2a 20 4e 65 78 74 20 75 6e 75 73      /* Next unus
5c810 65 64 20 65 6c 65 6d 65 6e 74 20 69 6e 20 72 65  ed element in re
5c820 74 75 72 6e 53 74 61 63 6b 5b 5d 20 2a 2f 0a 20  turnStack[] */. 
5c830 20 69 6e 74 20 6e 52 65 73 43 6f 6c 75 6d 6e 3b   int nResColumn;
5c840 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
5c850 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
5c860 20 6f 6e 65 20 72 6f 77 20 6f 66 20 74 68 65 20   one row of the 
5c870 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20  result set */.  
5c880 63 68 61 72 20 2a 2a 61 7a 52 65 73 43 6f 6c 75  char **azResColu
5c890 6d 6e 3b 20 20 20 20 20 2f 2a 20 56 61 6c 75 65  mn;     /* Value
5c8a0 73 20 66 6f 72 20 6f 6e 65 20 72 6f 77 20 6f 66  s for one row of
5c8b0 20 72 65 73 75 6c 74 20 2a 2f 20 0a 20 20 69 6e   result */ .  in
5c8c0 74 20 70 6f 70 53 74 61 63 6b 3b 20 20 20 20 20  t popStack;     
5c8d0 20 20 20 20 20 20 2f 2a 20 50 6f 70 20 74 68 65        /* Pop the
5c8e0 20 73 74 61 63 6b 20 74 68 69 73 20 6d 75 63 68   stack this much
5c8f0 20 6f 6e 20 65 6e 74 72 79 20 74 6f 20 56 64 62   on entry to Vdb
5c900 65 45 78 65 63 28 29 20 2a 2f 0a 20 20 63 68 61  eExec() */.  cha
5c910 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20  r *zErrMsg;     
5c920 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65       /* Error me
5c930 73 73 61 67 65 20 77 72 69 74 74 65 6e 20 68 65  ssage written he
5c940 72 65 20 2a 2f 0a 20 20 75 38 20 72 65 73 4f 6e  re */.  u8 resOn
5c950 53 74 61 63 6b 3b 20 20 20 20 20 20 20 20 20 20  Stack;          
5c960 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 72 65  /* True if there
5c970 20 61 72 65 20 72 65 73 75 6c 74 20 76 61 6c 75   are result valu
5c980 65 73 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 20  es on the stack 
5c990 2a 2f 0a 20 20 75 38 20 65 78 70 6c 61 69 6e 3b  */.  u8 explain;
5c9a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5c9b0 54 72 75 65 20 69 66 20 45 58 50 4c 41 49 4e 20  True if EXPLAIN 
5c9c0 70 72 65 73 65 6e 74 20 6f 6e 20 53 51 4c 20 63  present on SQL c
5c9d0 6f 6d 6d 61 6e 64 20 2a 2f 0a 20 20 75 38 20 63  ommand */.  u8 c
5c9e0 68 61 6e 67 65 43 6e 74 4f 6e 3b 20 20 20 20 20  hangeCntOn;     
5c9f0 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 75      /* True to u
5ca00 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 65  pdate the change
5ca10 2d 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 38  -counter */.  u8
5ca20 20 61 62 6f 72 74 65 64 3b 20 20 20 20 20 20 20   aborted;       
5ca30 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
5ca40 20 52 4f 4c 4c 42 41 43 4b 20 69 6e 20 61 6e 6f   ROLLBACK in ano
5ca50 74 68 65 72 20 56 4d 20 63 61 75 73 65 73 20 61  ther VM causes a
5ca60 6e 20 61 62 6f 72 74 20 2a 2f 0a 20 20 75 38 20  n abort */.  u8 
5ca70 65 78 70 69 72 65 64 3b 20 20 20 20 20 20 20 20  expired;        
5ca80 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
5ca90 74 68 65 20 56 4d 20 6e 65 65 64 73 20 74 6f 20  the VM needs to 
5caa0 62 65 20 72 65 63 6f 6d 70 69 6c 65 64 20 2a 2f  be recompiled */
5cab0 0a 20 20 75 38 20 6d 69 6e 57 72 69 74 65 46 69  .  u8 minWriteFi
5cac0 6c 65 46 6f 72 6d 61 74 3b 20 20 2f 2a 20 4d 69  leFormat;  /* Mi
5cad0 6e 69 6d 75 6d 20 66 69 6c 65 20 66 6f 72 6d 61  nimum file forma
5cae0 74 20 66 6f 72 20 77 72 69 74 61 62 6c 65 20 64  t for writable d
5caf0 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 2a 2f  atabase files */
5cb00 0a 20 20 75 38 20 69 6e 56 74 61 62 4d 65 74 68  .  u8 inVtabMeth
5cb10 6f 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 65  od;        /* Se
5cb20 65 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65  e comments above
5cb30 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67   */.  int nChang
5cb40 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e;            /*
5cb50 20 4e 75 6d 62 65 72 20 6f 66 20 64 62 20 63 68   Number of db ch
5cb60 61 6e 67 65 73 20 6d 61 64 65 20 73 69 6e 63 65  anges made since
5cb70 20 6c 61 73 74 20 72 65 73 65 74 20 2a 2f 0a 20   last reset */. 
5cb80 20 69 36 34 20 73 74 61 72 74 54 69 6d 65 3b 20   i64 startTime; 
5cb90 20 20 20 20 20 20 20 20 20 2f 2a 20 54 69 6d 65           /* Time
5cba0 20 77 68 65 6e 20 71 75 65 72 79 20 73 74 61 72   when query star
5cbb0 74 65 64 20 2d 20 75 73 65 64 20 66 6f 72 20 70  ted - used for p
5cbc0 72 6f 66 69 6c 69 6e 67 20 2a 2f 0a 20 20 69 6e  rofiling */.  in
5cbd0 74 20 6e 53 71 6c 3b 20 20 20 20 20 20 20 20 20  t nSql;         
5cbe0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
5cbf0 20 62 79 74 65 73 20 69 6e 20 7a 53 71 6c 20 2a   bytes in zSql *
5cc00 2f 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 20  /.  char *zSql; 
5cc10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78            /* Tex
5cc20 74 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74 61  t of the SQL sta
5cc30 74 65 6d 65 6e 74 20 74 68 61 74 20 67 65 6e 65  tement that gene
5cc40 72 61 74 65 64 20 74 68 69 73 20 2a 2f 0a 23 69  rated this */.#i
5cc50 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
5cc60 47 0a 20 20 46 49 4c 45 20 2a 74 72 61 63 65 3b  G.  FILE *trace;
5cc70 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65          /* Write
5cc80 20 61 6e 20 65 78 65 63 75 74 69 6f 6e 20 74 72   an execution tr
5cc90 61 63 65 20 68 65 72 65 2c 20 69 66 20 6e 6f 74  ace here, if not
5cca0 20 4e 55 4c 4c 20 2a 2f 0a 23 65 6e 64 69 66 0a   NULL */.#endif.
5ccb0 20 20 69 6e 74 20 6f 70 65 6e 65 64 53 74 61 74    int openedStat
5ccc0 65 6d 65 6e 74 3b 20 20 2f 2a 20 54 72 75 65 20  ement;  /* True 
5ccd0 69 66 20 74 68 69 73 20 56 4d 20 68 61 73 20 6f  if this VM has o
5cce0 70 65 6e 65 64 20 61 20 73 74 61 74 65 6d 65 6e  pened a statemen
5ccf0 74 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 69 66  t journal */.#if
5cd00 64 65 66 20 53 51 4c 49 54 45 5f 53 53 45 0a 20  def SQLITE_SSE. 
5cd10 20 69 6e 74 20 66 65 74 63 68 49 64 3b 20 20 20   int fetchId;   
5cd20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 65 6d         /* Statem
5cd30 65 6e 74 20 6e 75 6d 62 65 72 20 75 73 65 64 20  ent number used 
5cd40 62 79 20 73 71 6c 69 74 65 33 5f 66 65 74 63 68  by sqlite3_fetch
5cd50 5f 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20  _statement */.  
5cd60 69 6e 74 20 6c 72 75 3b 20 20 20 20 20 20 20 20  int lru;        
5cd70 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72        /* Counter
5cd80 20 75 73 65 64 20 66 6f 72 20 4c 52 55 20 63 61   used for LRU ca
5cd90 63 68 65 20 72 65 70 6c 61 63 65 6d 65 6e 74 20  che replacement 
5cda0 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a  */.#endif.};../*
5cdb0 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
5cdc0 67 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 76 61  g are allowed va
5cdd0 6c 75 65 73 20 66 6f 72 20 56 64 62 65 2e 6d 61  lues for Vdbe.ma
5cde0 67 69 63 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 56  gic.*/.#define V
5cdf0 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 20  DBE_MAGIC_INIT  
5ce00 20 20 20 30 78 32 36 62 63 65 61 61 35 20 20 20     0x26bceaa5   
5ce10 20 2f 2a 20 42 75 69 6c 64 69 6e 67 20 61 20 56   /* Building a V
5ce20 44 42 45 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 23  DBE program */.#
5ce30 64 65 66 69 6e 65 20 56 44 42 45 5f 4d 41 47 49  define VDBE_MAGI
5ce40 43 5f 52 55 4e 20 20 20 20 20 20 30 78 62 64 66  C_RUN      0xbdf
5ce50 32 30 64 61 33 20 20 20 20 2f 2a 20 56 44 42 45  20da3    /* VDBE
5ce60 20 69 73 20 72 65 61 64 79 20 74 6f 20 65 78 65   is ready to exe
5ce70 63 75 74 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  cute */.#define 
5ce80 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20  VDBE_MAGIC_HALT 
5ce90 20 20 20 20 30 78 35 31 39 63 32 39 37 33 20 20      0x519c2973  
5cea0 20 20 2f 2a 20 56 44 42 45 20 68 61 73 20 63 6f    /* VDBE has co
5ceb0 6d 70 6c 65 74 65 64 20 65 78 65 63 75 74 69 6f  mpleted executio
5cec0 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42  n */.#define VDB
5ced0 45 5f 4d 41 47 49 43 5f 44 45 41 44 20 20 20 20  E_MAGIC_DEAD    
5cee0 20 30 78 62 36 30 36 63 33 63 38 20 20 20 20 2f   0xb606c3c8    /
5cef0 2a 20 54 68 65 20 56 44 42 45 20 68 61 73 20 62  * The VDBE has b
5cf00 65 65 6e 20 64 65 61 6c 6c 6f 63 61 74 65 64 20  een deallocated 
5cf10 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69  */../*.** Functi
5cf20 6f 6e 20 70 72 6f 74 6f 74 79 70 65 73 0a 2a 2f  on prototypes.*/
5cf30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5cf40 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
5cf50 46 72 65 65 43 75 72 73 6f 72 28 56 64 62 65 20  FreeCursor(Vdbe 
5cf60 2a 2c 20 43 75 72 73 6f 72 2a 29 3b 0a 76 6f 69  *, Cursor*);.voi
5cf70 64 20 73 71 6c 69 74 65 56 64 62 65 50 6f 70 53  d sqliteVdbePopS
5cf80 74 61 63 6b 28 56 64 62 65 2a 2c 69 6e 74 29 3b  tack(Vdbe*,int);
5cf90 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5cfa0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43  int sqlite3VdbeC
5cfb0 75 72 73 6f 72 4d 6f 76 65 74 6f 28 43 75 72 73  ursorMoveto(Curs
5cfc0 6f 72 2a 29 3b 0a 23 69 66 20 64 65 66 69 6e 65  or*);.#if define
5cfd0 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20  d(SQLITE_DEBUG) 
5cfe0 7c 7c 20 64 65 66 69 6e 65 64 28 56 44 42 45 5f  || defined(VDBE_
5cff0 50 52 4f 46 49 4c 45 29 0a 53 51 4c 49 54 45 5f  PROFILE).SQLITE_
5d000 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
5d010 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28  ite3VdbePrintOp(
5d020 46 49 4c 45 2a 2c 20 69 6e 74 2c 20 4f 70 2a 29  FILE*, int, Op*)
5d030 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f  ;.#endif.SQLITE_
5d040 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
5d050 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70  te3VdbeSerialTyp
5d060 65 4c 65 6e 28 75 33 32 29 3b 0a 53 51 4c 49 54  eLen(u32);.SQLIT
5d070 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71  E_PRIVATE u32 sq
5d080 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54  lite3VdbeSerialT
5d090 79 70 65 28 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a  ype(Mem*, int);.
5d0a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
5d0b0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  nt sqlite3VdbeSe
5d0c0 72 69 61 6c 50 75 74 28 75 6e 73 69 67 6e 65 64  rialPut(unsigned
5d0d0 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 4d 65 6d   char*, int, Mem
5d0e0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
5d0f0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
5d100 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 65 74  te3VdbeSerialGet
5d110 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20  (const unsigned 
5d120 63 68 61 72 2a 2c 20 75 33 32 2c 20 4d 65 6d 2a  char*, u32, Mem*
5d130 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
5d140 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
5d150 62 65 44 65 6c 65 74 65 41 75 78 44 61 74 61 28  beDeleteAuxData(
5d160 56 64 62 65 46 75 6e 63 2a 2c 20 69 6e 74 29 3b  VdbeFunc*, int);
5d170 0a 0a 69 6e 74 20 73 71 6c 69 74 65 32 42 74 72  ..int sqlite2Btr
5d180 65 65 4b 65 79 43 6f 6d 70 61 72 65 28 42 74 43  eeKeyCompare(BtC
5d190 75 72 73 6f 72 20 2a 2c 20 63 6f 6e 73 74 20 76  ursor *, const v
5d1a0 6f 69 64 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c  oid *, int, int,
5d1b0 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f   int *);.SQLITE_
5d1c0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
5d1d0 74 65 33 56 64 62 65 49 64 78 4b 65 79 43 6f 6d  te3VdbeIdxKeyCom
5d1e0 70 61 72 65 28 43 75 72 73 6f 72 2a 2c 20 69 6e  pare(Cursor*, in
5d1f0 74 20 2c 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  t , const unsign
5d200 65 64 20 63 68 61 72 2a 2c 20 69 6e 74 2a 29 3b  ed char*, int*);
5d210 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5d220 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 49  int sqlite3VdbeI
5d230 64 78 52 6f 77 69 64 28 42 74 43 75 72 73 6f 72  dxRowid(BtCursor
5d240 20 2a 2c 20 69 36 34 20 2a 29 3b 0a 53 51 4c 49   *, i64 *);.SQLI
5d250 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
5d260 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65  qlite3MemCompare
5d270 28 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20 63 6f 6e  (const Mem*, con
5d280 73 74 20 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 43  st Mem*, const C
5d290 6f 6c 6c 53 65 71 2a 29 3b 0a 53 51 4c 49 54 45  ollSeq*);.SQLITE
5d2a0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
5d2b0 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 43 6f  ite3VdbeRecordCo
5d2c0 6d 70 61 72 65 28 76 6f 69 64 2a 2c 69 6e 74 2c  mpare(void*,int,
5d2d0 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c  const void*,int,
5d2e0 20 63 6f 6e 73 74 20 76 6f 69 64 2a 29 3b 0a 53   const void*);.S
5d2f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
5d300 74 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 78  t sqlite3VdbeIdx
5d310 52 6f 77 69 64 4c 65 6e 28 63 6f 6e 73 74 20 75  RowidLen(const u
5d320 38 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  8*);.SQLITE_PRIV
5d330 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
5d340 64 62 65 45 78 65 63 28 56 64 62 65 2a 29 3b 0a  dbeExec(Vdbe*);.
5d350 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
5d360 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4c 69  nt sqlite3VdbeLi
5d370 73 74 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54  st(Vdbe*);.SQLIT
5d380 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
5d390 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28 56 64  lite3VdbeHalt(Vd
5d3a0 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  be*);.SQLITE_PRI
5d3b0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
5d3c0 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69  VdbeChangeEncodi
5d3d0 6e 67 28 4d 65 6d 20 2a 2c 20 69 6e 74 29 3b 0a  ng(Mem *, int);.
5d3e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
5d3f0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
5d400 6d 54 6f 6f 42 69 67 28 4d 65 6d 2a 29 3b 0a 53  mTooBig(Mem*);.S
5d410 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
5d420 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
5d430 43 6f 70 79 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74  Copy(Mem*, const
5d440 20 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50   Mem*);.SQLITE_P
5d450 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
5d460 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f  te3VdbeMemShallo
5d470 77 43 6f 70 79 28 4d 65 6d 2a 2c 20 63 6f 6e 73  wCopy(Mem*, cons
5d480 74 20 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51  t Mem*, int);.SQ
5d490 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
5d4a0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d   sqlite3VdbeMemM
5d4b0 6f 76 65 28 4d 65 6d 2a 2c 20 4d 65 6d 2a 29 3b  ove(Mem*, Mem*);
5d4c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5d4d0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
5d4e0 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 4d  emNulTerminate(M
5d4f0 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  em*);.SQLITE_PRI
5d500 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
5d510 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 4d 65  VdbeMemSetStr(Me
5d520 6d 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c  m*, const char*,
5d530 20 69 6e 74 2c 20 75 38 2c 20 76 6f 69 64 28 2a   int, u8, void(*
5d540 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54  )(void*));.SQLIT
5d550 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
5d560 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
5d570 49 6e 74 36 34 28 4d 65 6d 2a 2c 20 69 36 34 29  Int64(Mem*, i64)
5d580 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
5d590 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
5d5a0 65 4d 65 6d 53 65 74 44 6f 75 62 6c 65 28 4d 65  eMemSetDouble(Me
5d5b0 6d 2a 2c 20 64 6f 75 62 6c 65 29 3b 0a 53 51 4c  m*, double);.SQL
5d5c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
5d5d0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
5d5e0 65 74 4e 75 6c 6c 28 4d 65 6d 2a 29 3b 0a 53 51  etNull(Mem*);.SQ
5d5f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
5d600 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  d sqlite3VdbeMem
5d610 53 65 74 5a 65 72 6f 42 6c 6f 62 28 4d 65 6d 2a  SetZeroBlob(Mem*
5d620 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
5d630 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
5d640 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74  3VdbeMemMakeWrit
5d650 65 61 62 6c 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c  eable(Mem*);.SQL
5d660 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
5d670 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 44 79  sqlite3VdbeMemDy
5d680 6e 61 6d 69 63 69 66 79 28 4d 65 6d 2a 29 3b 0a  namicify(Mem*);.
5d690 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
5d6a0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
5d6b0 6d 53 74 72 69 6e 67 69 66 79 28 4d 65 6d 2a 2c  mStringify(Mem*,
5d6c0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
5d6d0 49 56 41 54 45 20 69 36 34 20 73 71 6c 69 74 65  IVATE i64 sqlite
5d6e0 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 4d 65  3VdbeIntValue(Me
5d6f0 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  m*);.SQLITE_PRIV
5d700 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
5d710 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79  dbeMemIntegerify
5d720 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  (Mem*);.SQLITE_P
5d730 52 49 56 41 54 45 20 64 6f 75 62 6c 65 20 73 71  RIVATE double sq
5d740 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c  lite3VdbeRealVal
5d750 75 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45  ue(Mem*);.SQLITE
5d760 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
5d770 6c 69 74 65 33 56 64 62 65 49 6e 74 65 67 65 72  lite3VdbeInteger
5d780 41 66 66 69 6e 69 74 79 28 4d 65 6d 2a 29 3b 0a  Affinity(Mem*);.
5d790 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
5d7a0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
5d7b0 6d 52 65 61 6c 69 66 79 28 4d 65 6d 2a 29 3b 0a  mRealify(Mem*);.
5d7c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
5d7d0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
5d7e0 6d 4e 75 6d 65 72 69 66 79 28 4d 65 6d 2a 29 3b  mNumerify(Mem*);
5d7f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5d800 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
5d810 65 6d 46 72 6f 6d 42 74 72 65 65 28 42 74 43 75  emFromBtree(BtCu
5d820 72 73 6f 72 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e  rsor*,int,int,in
5d830 74 2c 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f  t,Mem*);.SQLITE_
5d840 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
5d850 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
5d860 73 65 28 4d 65 6d 20 2a 70 29 3b 0a 53 51 4c 49  se(Mem *p);.SQLI
5d870 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
5d880 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e  qlite3VdbeMemFin
5d890 61 6c 69 7a 65 28 4d 65 6d 2a 2c 20 46 75 6e 63  alize(Mem*, Func
5d8a0 44 65 66 2a 29 3b 0a 23 69 66 6e 64 65 66 20 4e  Def*);.#ifndef N
5d8b0 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49  DEBUG.SQLITE_PRI
5d8c0 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
5d8d0 74 65 33 56 64 62 65 4d 65 6d 53 61 6e 69 74 79  te3VdbeMemSanity
5d8e0 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  (Mem*);.SQLITE_P
5d8f0 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c  RIVATE   int sql
5d900 69 74 65 33 56 64 62 65 4f 70 63 6f 64 65 4e 6f  ite3VdbeOpcodeNo
5d910 50 75 73 68 28 75 38 29 3b 0a 23 65 6e 64 69 66  Push(u8);.#endif
5d920 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5d930 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
5d940 65 6d 54 72 61 6e 73 6c 61 74 65 28 4d 65 6d 2a  emTranslate(Mem*
5d950 2c 20 75 38 29 3b 0a 23 69 66 64 65 66 20 53 51  , u8);.#ifdef SQ
5d960 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54  LITE_DEBUG.SQLIT
5d970 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
5d980 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e   sqlite3VdbePrin
5d990 74 53 71 6c 28 56 64 62 65 2a 29 3b 0a 53 51 4c  tSql(Vdbe*);.SQL
5d9a0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
5d9b0 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  id sqlite3VdbeMe
5d9c0 6d 50 72 65 74 74 79 50 72 69 6e 74 28 4d 65 6d  mPrettyPrint(Mem
5d9d0 20 2a 70 4d 65 6d 2c 20 63 68 61 72 20 2a 7a 42   *pMem, char *zB
5d9e0 75 66 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49  uf);.#endif.SQLI
5d9f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
5da00 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 48 61 6e  qlite3VdbeMemHan
5da10 64 6c 65 42 6f 6d 28 4d 65 6d 20 2a 70 4d 65 6d  dleBom(Mem *pMem
5da20 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
5da30 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
5da40 62 65 46 69 66 6f 49 6e 69 74 28 46 69 66 6f 2a  beFifoInit(Fifo*
5da50 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
5da60 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
5da70 65 46 69 66 6f 50 75 73 68 28 46 69 66 6f 2a 2c  eFifoPush(Fifo*,
5da80 20 69 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52   i64);.SQLITE_PR
5da90 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
5daa0 33 56 64 62 65 46 69 66 6f 50 6f 70 28 46 69 66  3VdbeFifoPop(Fif
5dab0 6f 2a 2c 20 69 36 34 2a 29 3b 0a 53 51 4c 49 54  o*, i64*);.SQLIT
5dac0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
5dad0 71 6c 69 74 65 33 56 64 62 65 46 69 66 6f 43 6c  qlite3VdbeFifoCl
5dae0 65 61 72 28 46 69 66 6f 2a 29 3b 0a 0a 23 69 66  ear(Fifo*);..#if
5daf0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
5db00 5f 49 4e 43 52 42 4c 4f 42 0a 53 51 4c 49 54 45  _INCRBLOB.SQLITE
5db10 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73  _PRIVATE   int s
5db20 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70  qlite3VdbeMemExp
5db30 61 6e 64 42 6c 6f 62 28 4d 65 6d 20 2a 29 3b 0a  andBlob(Mem *);.
5db40 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20  #else.  #define 
5db50 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78  sqlite3VdbeMemEx
5db60 70 61 6e 64 42 6c 6f 62 28 78 29 20 53 51 4c 49  pandBlob(x) SQLI
5db70 54 45 5f 4f 4b 0a 23 65 6e 64 69 66 0a 0a 23 65  TE_OK.#endif..#e
5db80 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
5db90 28 5f 56 44 42 45 49 4e 54 5f 48 5f 29 20 2a 2f  (_VDBEINT_H_) */
5dba0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
5dbb0 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 49 6e 74  * End of vdbeInt
5dbc0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
5dbd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5dbe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5dbf0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
5dc00 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
5dc10 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
5dc20 6e 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  n utf.c ********
5dc30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5dc40 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  /../*.** The fol
5dc50 6c 6f 77 69 6e 67 20 63 6f 6e 73 74 61 6e 74 20  lowing constant 
5dc60 76 61 6c 75 65 20 69 73 20 75 73 65 64 20 62 79  value is used by
5dc70 20 74 68 65 20 53 51 4c 49 54 45 5f 42 49 47 45   the SQLITE_BIGE
5dc80 4e 44 49 41 4e 20 61 6e 64 0a 2a 2a 20 53 51 4c  NDIAN and.** SQL
5dc90 49 54 45 5f 4c 49 54 54 4c 45 45 4e 44 49 41 4e  ITE_LITTLEENDIAN
5dca0 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 63 6f 6e 73   macros..*/.cons
5dcb0 74 20 69 6e 74 20 73 71 6c 69 74 65 33 6f 6e 65  t int sqlite3one
5dcc0 20 3d 20 31 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69   = 1;../*.** Thi
5dcd0 73 20 6c 6f 6f 6b 75 70 20 74 61 62 6c 65 20 69  s lookup table i
5dce0 73 20 75 73 65 64 20 74 6f 20 68 65 6c 70 20 64  s used to help d
5dcf0 65 63 6f 64 65 20 74 68 65 20 66 69 72 73 74 20  ecode the first 
5dd00 62 79 74 65 20 6f 66 0a 2a 2a 20 61 20 6d 75 6c  byte of.** a mul
5dd10 74 69 2d 62 79 74 65 20 55 54 46 38 20 63 68 61  ti-byte UTF8 cha
5dd20 72 61 63 74 65 72 2e 0a 2a 2f 0a 63 6f 6e 73 74  racter..*/.const
5dd30 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73   unsigned char s
5dd40 71 6c 69 74 65 33 55 74 66 54 72 61 6e 73 31 5b  qlite3UtfTrans1[
5dd50 5d 20 3d 20 7b 0a 20 20 30 78 30 30 2c 20 30 78  ] = {.  0x00, 0x
5dd60 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33 2c 20  01, 0x02, 0x03, 
5dd70 30 78 30 34 2c 20 30 78 30 35 2c 20 30 78 30 36  0x04, 0x05, 0x06
5dd80 2c 20 30 78 30 37 2c 0a 20 20 30 78 30 38 2c 20  , 0x07,.  0x08, 
5dd90 30 78 30 39 2c 20 30 78 30 61 2c 20 30 78 30 62  0x09, 0x0a, 0x0b
5dda0 2c 20 30 78 30 63 2c 20 30 78 30 64 2c 20 30 78  , 0x0c, 0x0d, 0x
5ddb0 30 65 2c 20 30 78 30 66 2c 0a 20 20 30 78 31 30  0e, 0x0f,.  0x10
5ddc0 2c 20 30 78 31 31 2c 20 30 78 31 32 2c 20 30 78  , 0x11, 0x12, 0x
5ddd0 31 33 2c 20 30 78 31 34 2c 20 30 78 31 35 2c 20  13, 0x14, 0x15, 
5dde0 30 78 31 36 2c 20 30 78 31 37 2c 0a 20 20 30 78  0x16, 0x17,.  0x
5ddf0 31 38 2c 20 30 78 31 39 2c 20 30 78 31 61 2c 20  18, 0x19, 0x1a, 
5de00 30 78 31 62 2c 20 30 78 31 63 2c 20 30 78 31 64  0x1b, 0x1c, 0x1d
5de10 2c 20 30 78 31 65 2c 20 30 78 31 66 2c 0a 20 20  , 0x1e, 0x1f,.  
5de20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 32  0x00, 0x01, 0x02
5de30 2c 20 30 78 30 33 2c 20 30 78 30 34 2c 20 30 78  , 0x03, 0x04, 0x
5de40 30 35 2c 20 30 78 30 36 2c 20 30 78 30 37 2c 0a  05, 0x06, 0x07,.
5de50 20 20 30 78 30 38 2c 20 30 78 30 39 2c 20 30 78    0x08, 0x09, 0x
5de60 30 61 2c 20 30 78 30 62 2c 20 30 78 30 63 2c 20  0a, 0x0b, 0x0c, 
5de70 30 78 30 64 2c 20 30 78 30 65 2c 20 30 78 30 66  0x0d, 0x0e, 0x0f
5de80 2c 0a 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20  ,.  0x00, 0x01, 
5de90 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78 30 34  0x02, 0x03, 0x04
5dea0 2c 20 30 78 30 35 2c 20 30 78 30 36 2c 20 30 78  , 0x05, 0x06, 0x
5deb0 30 37 2c 0a 20 20 30 78 30 30 2c 20 30 78 30 31  07,.  0x00, 0x01
5dec0 2c 20 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78  , 0x02, 0x03, 0x
5ded0 30 30 2c 20 30 78 30 31 2c 20 30 78 30 30 2c 20  00, 0x01, 0x00, 
5dee0 30 78 30 30 2c 0a 7d 3b 0a 0a 0a 23 64 65 66 69  0x00,.};...#defi
5def0 6e 65 20 57 52 49 54 45 5f 55 54 46 38 28 7a 4f  ne WRITE_UTF8(zO
5df00 75 74 2c 20 63 29 20 7b 20 20 20 20 20 20 20 20  ut, c) {        
5df10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5df20 20 20 5c 0a 20 20 69 66 28 20 63 3c 30 78 30 30    \.  if( c<0x00
5df30 30 38 30 20 29 7b 20 20 20 20 20 20 20 20 20 20  080 ){          
5df40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5df50 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
5df60 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 63 26 30 78   *zOut++ = (c&0x
5df70 46 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  FF);            
5df80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5df90 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20      \.  }       
5dfa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5dfb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5dfc0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
5dfd0 20 65 6c 73 65 20 69 66 28 20 63 3c 30 78 30 30   else if( c<0x00
5dfe0 38 30 30 20 29 7b 20 20 20 20 20 20 20 20 20 20  800 ){          
5dff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e000 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75        \.    *zOu
5e010 74 2b 2b 20 3d 20 30 78 43 30 20 2b 20 28 28 63  t++ = 0xC0 + ((c
5e020 3e 3e 36 29 26 30 78 31 46 29 3b 20 20 20 20 20  >>6)&0x1F);     
5e030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
5e040 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30  .    *zOut++ = 0
5e050 78 38 30 20 2b 20 28 63 20 26 20 30 78 33 46 29  x80 + (c & 0x3F)
5e060 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
5e070 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20          \.  }   
5e080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e0b0 20 5c 0a 20 20 65 6c 73 65 20 69 66 28 20 63 3c   \.  else if( c<
5e0c0 30 78 31 30 30 30 30 20 29 7b 20 20 20 20 20 20  0x10000 ){      
5e0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e0e0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
5e0f0 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 45 30 20 2b  *zOut++ = 0xE0 +
5e100 20 28 28 63 3e 3e 31 32 29 26 30 78 30 46 29 3b   ((c>>12)&0x0F);
5e110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e120 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b     \.    *zOut++
5e130 20 3d 20 30 78 38 30 20 2b 20 28 28 63 3e 3e 36   = 0x80 + ((c>>6
5e140 29 20 26 20 30 78 33 46 29 3b 20 20 20 20 20 20  ) & 0x3F);      
5e150 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
5e160 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30    *zOut++ = 0x80
5e170 20 2b 20 28 63 20 26 20 30 78 33 46 29 3b 20 20   + (c & 0x3F);  
5e180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e190 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20       \.  }else{ 
5e1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
5e1d0 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78      *zOut++ = 0x
5e1e0 46 30 20 2b 20 28 28 63 3e 3e 31 38 29 20 26 20  F0 + ((c>>18) & 
5e1f0 30 78 30 37 29 3b 20 20 20 20 20 20 20 20 20 20  0x07);          
5e200 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f         \.    *zO
5e210 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 28  ut++ = 0x80 + ((
5e220 63 3e 3e 31 32 29 20 26 20 30 78 33 46 29 3b 20  c>>12) & 0x3F); 
5e230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e240 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20  \.    *zOut++ = 
5e250 30 78 38 30 20 2b 20 28 28 63 3e 3e 36 29 20 26  0x80 + ((c>>6) &
5e260 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 20 20   0x3F);         
5e270 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a           \.    *
5e280 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20  zOut++ = 0x80 + 
5e290 28 63 20 26 20 30 78 33 46 29 3b 20 20 20 20 20  (c & 0x3F);     
5e2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e2b0 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20    \.  }         
5e2c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e2e0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a             \.}..
5e2f0 23 64 65 66 69 6e 65 20 57 52 49 54 45 5f 55 54  #define WRITE_UT
5e300 46 31 36 4c 45 28 7a 4f 75 74 2c 20 63 29 20 7b  F16LE(zOut, c) {
5e310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e330 5c 0a 20 20 69 66 28 20 63 3c 3d 30 78 46 46 46  \.  if( c<=0xFFF
5e340 46 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20  F ){            
5e350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e370 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20    \.    *zOut++ 
5e380 3d 20 28 63 26 30 78 30 30 46 46 29 3b 20 20 20  = (c&0x00FF);   
5e390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e3b0 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b      \.    *zOut+
5e3c0 2b 20 3d 20 28 28 63 3e 3e 38 29 26 30 78 30 30  + = ((c>>8)&0x00
5e3d0 46 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  FF);            
5e3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e3f0 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b        \.  }else{
5e400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e430 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a          \.    *z
5e440 4f 75 74 2b 2b 20 3d 20 28 28 28 63 3e 3e 31 30  Out++ = (((c>>10
5e450 29 26 30 78 30 30 33 46 29 20 2b 20 28 28 28 63  )&0x003F) + (((c
5e460 2d 30 78 31 30 30 30 30 29 3e 3e 31 30 29 26 30  -0x10000)>>10)&0
5e470 78 30 30 43 30 29 29 3b 20 20 5c 0a 20 20 20 20  x00C0));  \.    
5e480 2a 7a 4f 75 74 2b 2b 20 3d 20 28 30 78 30 30 44  *zOut++ = (0x00D
5e490 38 20 2b 20 28 28 28 63 2d 30 78 31 30 30 30 30  8 + (((c-0x10000
5e4a0 29 3e 3e 31 38 29 26 30 78 30 33 29 29 3b 20 20  )>>18)&0x03));  
5e4b0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
5e4c0 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 63 26 30    *zOut++ = (c&0
5e4d0 78 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 20  x00FF);         
5e4e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e4f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
5e500 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 30      *zOut++ = (0
5e510 78 30 30 44 43 20 2b 20 28 28 63 3e 3e 38 29 26  x00DC + ((c>>8)&
5e520 30 78 30 33 29 29 3b 20 20 20 20 20 20 20 20 20  0x03));         
5e530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e540 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 20  \.  }           
5e550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e580 20 20 5c 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 57    \.}..#define W
5e590 52 49 54 45 5f 55 54 46 31 36 42 45 28 7a 4f 75  RITE_UTF16BE(zOu
5e5a0 74 2c 20 63 29 20 7b 20 20 20 20 20 20 20 20 20  t, c) {         
5e5b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e5c0 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63         \.  if( c
5e5d0 3c 3d 30 78 46 46 46 46 20 29 7b 20 20 20 20 20  <=0xFFFF ){     
5e5e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e5f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e600 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a           \.    *
5e610 7a 4f 75 74 2b 2b 20 3d 20 28 28 63 3e 3e 38 29  zOut++ = ((c>>8)
5e620 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 20 20  &0x00FF);       
5e630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e640 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
5e650 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 63 26 30 78   *zOut++ = (c&0x
5e660 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 20 20  00FF);          
5e670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e680 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
5e690 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20 20 20   }else{         
5e6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
5e6d0 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28  .    *zOut++ = (
5e6e0 30 78 30 30 44 38 20 2b 20 28 28 28 63 2d 30 78  0x00D8 + (((c-0x
5e6f0 31 30 30 30 30 29 3e 3e 31 38 29 26 30 78 30 33  10000)>>18)&0x03
5e700 29 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ));             
5e710 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d   \.    *zOut++ =
5e720 20 28 28 28 63 3e 3e 31 30 29 26 30 78 30 30 33   (((c>>10)&0x003
5e730 46 29 20 2b 20 28 28 28 63 2d 30 78 31 30 30 30  F) + (((c-0x1000
5e740 30 29 3e 3e 31 30 29 26 30 78 30 30 43 30 29 29  0)>>10)&0x00C0))
5e750 3b 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b  ;  \.    *zOut++
5e760 20 3d 20 28 30 78 30 30 44 43 20 2b 20 28 28 63   = (0x00DC + ((c
5e770 3e 3e 38 29 26 30 78 30 33 29 29 3b 20 20 20 20  >>8)&0x03));    
5e780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e790 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74       \.    *zOut
5e7a0 2b 2b 20 3d 20 28 63 26 30 78 30 30 46 46 29 3b  ++ = (c&0x00FF);
5e7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e7c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e7d0 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20         \.  }    
5e7e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e810 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 64           \.}..#d
5e820 65 66 69 6e 65 20 52 45 41 44 5f 55 54 46 31 36  efine READ_UTF16
5e830 4c 45 28 7a 49 6e 2c 20 63 29 7b 20 20 20 20 20  LE(zIn, c){     
5e840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e860 20 20 20 20 5c 0a 20 20 63 20 3d 20 28 2a 7a 49      \.  c = (*zI
5e870 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20  n++);           
5e880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e8a0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
5e8b0 63 20 2b 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c  c += ((*zIn++)<<
5e8c0 38 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  8);             
5e8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e8f0 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3e 3d 30      \.  if( c>=0
5e900 78 44 38 30 30 20 26 26 20 63 3c 30 78 45 30 30  xD800 && c<0xE00
5e910 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20  0 ){            
5e920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e930 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
5e940 20 69 6e 74 20 63 32 20 3d 20 28 2a 7a 49 6e 2b   int c2 = (*zIn+
5e950 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  +);             
5e960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e980 20 20 20 5c 0a 20 20 20 20 63 32 20 2b 3d 20 28     \.    c2 += (
5e990 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20  (*zIn++)<<8);   
5e9a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e9b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e9c0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
5e9d0 20 63 20 3d 20 28 63 32 26 30 78 30 33 46 46 29   c = (c2&0x03FF)
5e9e0 20 2b 20 28 28 63 26 30 78 30 30 33 46 29 3c 3c   + ((c&0x003F)<<
5e9f0 31 30 29 20 2b 20 28 28 28 63 26 30 78 30 33 43  10) + (((c&0x03C
5ea00 30 29 2b 30 78 30 30 34 30 29 3c 3c 31 30 29 3b  0)+0x0040)<<10);
5ea10 20 20 20 5c 0a 20 20 20 20 69 66 28 20 28 63 20     \.    if( (c 
5ea20 26 20 30 78 46 46 46 46 30 30 30 30 29 3d 3d 30  & 0xFFFF0000)==0
5ea30 20 29 20 63 20 3d 20 30 78 46 46 46 44 3b 20 20   ) c = 0xFFFD;  
5ea40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ea50 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d             \.  }
5ea60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ea70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ea80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ea90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5eaa0 20 20 20 5c 0a 7d 0a 0a 23 64 65 66 69 6e 65 20     \.}..#define 
5eab0 52 45 41 44 5f 55 54 46 31 36 42 45 28 7a 49 6e  READ_UTF16BE(zIn
5eac0 2c 20 63 29 7b 20 20 20 20 20 20 20 20 20 20 20  , c){           
5ead0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5eae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
5eaf0 20 20 63 20 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c    c = ((*zIn++)<
5eb00 3c 38 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  <8);            
5eb10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5eb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5eb30 20 20 20 20 20 20 5c 0a 20 20 63 20 2b 3d 20 28        \.  c += (
5eb40 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20  *zIn++);        
5eb50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5eb60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5eb70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
5eb80 20 20 69 66 28 20 63 3e 3d 30 78 44 38 30 30 20    if( c>=0xD800 
5eb90 26 26 20 63 3c 30 78 45 30 30 30 20 29 7b 20 20  && c<0xE000 ){  
5eba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ebb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ebc0 20 20 20 20 20 5c 0a 20 20 20 20 69 6e 74 20 63       \.    int c
5ebd0 32 20 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38  2 = ((*zIn++)<<8
5ebe0 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
5ebf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ec00 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
5ec10 20 20 20 63 32 20 2b 3d 20 28 2a 7a 49 6e 2b 2b     c2 += (*zIn++
5ec20 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
5ec30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ec40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ec50 20 20 20 20 20 5c 0a 20 20 20 20 63 20 3d 20 28       \.    c = (
5ec60 63 32 26 30 78 30 33 46 46 29 20 2b 20 28 28 63  c2&0x03FF) + ((c
5ec70 26 30 78 30 30 33 46 29 3c 3c 31 30 29 20 2b 20  &0x003F)<<10) + 
5ec80 28 28 28 63 26 30 78 30 33 43 30 29 2b 30 78 30  (((c&0x03C0)+0x0
5ec90 30 34 30 29 3c 3c 31 30 29 3b 20 20 20 5c 0a 20  040)<<10);   \. 
5eca0 20 20 20 69 66 28 20 28 63 20 26 20 30 78 46 46     if( (c & 0xFF
5ecb0 46 46 30 30 30 30 29 3d 3d 30 20 29 20 63 20 3d  FF0000)==0 ) c =
5ecc0 20 30 78 46 46 46 44 3b 20 20 20 20 20 20 20 20   0xFFFD;        
5ecd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ece0 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20       \.  }      
5ecf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ed00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ed10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ed20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d               \.}
5ed30 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61 74  ../*.** Translat
5ed40 65 20 61 20 73 69 6e 67 6c 65 20 55 54 46 2d 38  e a single UTF-8
5ed50 20 63 68 61 72 61 63 74 65 72 2e 20 20 52 65 74   character.  Ret
5ed60 75 72 6e 20 74 68 65 20 75 6e 69 63 6f 64 65 20  urn the unicode 
5ed70 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 44 75 72  value..**.** Dur
5ed80 69 6e 67 20 74 72 61 6e 73 6c 61 74 69 6f 6e 2c  ing translation,
5ed90 20 61 73 73 75 6d 65 20 74 68 61 74 20 74 68 65   assume that the
5eda0 20 62 79 74 65 20 74 68 61 74 20 7a 54 65 72 6d   byte that zTerm
5edb0 20 70 6f 69 6e 74 73 0a 2a 2a 20 69 73 20 61 20   points.** is a 
5edc0 30 78 30 30 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 74  0x00..**.** Writ
5edd0 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  e a pointer to t
5ede0 68 65 20 6e 65 78 74 20 75 6e 72 65 61 64 20 62  he next unread b
5edf0 79 74 65 20 62 61 63 6b 20 69 6e 74 6f 20 2a 70  yte back into *p
5ee00 7a 4e 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74  zNext..**.** Not
5ee10 65 73 20 4f 6e 20 49 6e 76 61 6c 69 64 20 55 54  es On Invalid UT
5ee20 46 2d 38 3a 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54  F-8:.**.**  *  T
5ee30 68 69 73 20 72 6f 75 74 69 6e 65 20 6e 65 76 65  his routine neve
5ee40 72 20 61 6c 6c 6f 77 73 20 61 20 37 2d 62 69 74  r allows a 7-bit
5ee50 20 63 68 61 72 61 63 74 65 72 20 28 30 78 30 30   character (0x00
5ee60 20 74 68 72 6f 75 67 68 20 30 78 37 66 29 20 74   through 0x7f) t
5ee70 6f 0a 2a 2a 20 20 20 20 20 62 65 20 65 6e 63 6f  o.**     be enco
5ee80 64 65 64 20 61 73 20 61 20 6d 75 6c 74 69 2d 62  ded as a multi-b
5ee90 79 74 65 20 63 68 61 72 61 63 74 65 72 2e 20 20  yte character.  
5eea0 41 6e 79 20 6d 75 6c 74 69 2d 62 79 74 65 20 63  Any multi-byte c
5eeb0 68 61 72 61 63 74 65 72 20 74 68 61 74 0a 2a 2a  haracter that.**
5eec0 20 20 20 20 20 61 74 74 65 6d 70 74 73 20 74 6f       attempts to
5eed0 20 65 6e 63 6f 64 65 20 61 20 76 61 6c 75 65 20   encode a value 
5eee0 62 65 74 77 65 65 6e 20 30 78 30 30 20 61 6e 64  between 0x00 and
5eef0 20 30 78 37 66 20 69 73 20 72 65 6e 64 65 72 65   0x7f is rendere
5ef00 64 20 61 73 20 30 78 66 66 66 64 2e 0a 2a 2a 0a  d as 0xfffd..**.
5ef10 2a 2a 20 20 2a 20 20 54 68 69 73 20 72 6f 75 74  **  *  This rout
5ef20 69 6e 65 20 6e 65 76 65 72 20 61 6c 6c 6f 77 73  ine never allows
5ef30 20 61 20 55 54 46 31 36 20 73 75 72 72 6f 67 61   a UTF16 surroga
5ef40 74 65 20 76 61 6c 75 65 20 74 6f 20 62 65 20 65  te value to be e
5ef50 6e 63 6f 64 65 64 2e 0a 2a 2a 20 20 20 20 20 49  ncoded..**     I
5ef60 66 20 61 20 6d 75 6c 74 69 2d 62 79 74 65 20 63  f a multi-byte c
5ef70 68 61 72 61 63 74 65 72 20 61 74 74 65 6d 70 74  haracter attempt
5ef80 73 20 74 6f 20 65 6e 63 6f 64 65 20 61 20 76 61  s to encode a va
5ef90 6c 75 65 20 62 65 74 77 65 65 6e 0a 2a 2a 20 20  lue between.**  
5efa0 20 20 20 30 78 64 38 30 30 20 61 6e 64 20 30 78     0xd800 and 0x
5efb0 65 30 30 30 20 74 68 65 6e 20 69 74 20 69 73 20  e000 then it is 
5efc0 72 65 6e 64 65 72 65 64 20 61 73 20 30 78 66 66  rendered as 0xff
5efd0 66 64 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 42 79  fd..**.**  *  By
5efe0 74 65 73 20 69 6e 20 74 68 65 20 72 61 6e 67 65  tes in the range
5eff0 20 6f 66 20 30 78 38 30 20 74 68 72 6f 75 67 68   of 0x80 through
5f000 20 30 78 62 66 20 77 68 69 63 68 20 6f 63 63 75   0xbf which occu
5f010 72 20 61 73 20 74 68 65 20 66 69 72 73 74 0a 2a  r as the first.*
5f020 2a 20 20 20 20 20 62 79 74 65 20 6f 66 20 61 20  *     byte of a 
5f030 63 68 61 72 61 63 74 65 72 20 61 72 65 20 69 6e  character are in
5f040 74 65 72 70 72 65 74 65 64 20 61 73 20 73 69 6e  terpreted as sin
5f050 67 6c 65 2d 62 79 74 65 20 63 68 61 72 61 63 74  gle-byte charact
5f060 65 72 73 0a 2a 2a 20 20 20 20 20 61 6e 64 20 72  ers.**     and r
5f070 65 6e 64 65 72 65 64 20 61 73 20 74 68 65 6d 73  endered as thems
5f080 65 6c 76 65 73 20 65 76 65 6e 20 74 68 6f 75 67  elves even thoug
5f090 68 20 74 68 65 79 20 61 72 65 20 74 65 63 68 6e  h they are techn
5f0a0 69 63 61 6c 6c 79 0a 2a 2a 20 20 20 20 20 69 6e  ically.**     in
5f0b0 76 61 6c 69 64 20 63 68 61 72 61 63 74 65 72 73  valid characters
5f0c0 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54 68 69 73  ..**.**  *  This
5f0d0 20 72 6f 75 74 69 6e 65 20 61 63 63 65 70 74 73   routine accepts
5f0e0 20 61 6e 20 69 6e 66 69 6e 69 74 65 20 6e 75 6d   an infinite num
5f0f0 62 65 72 20 6f 66 20 64 69 66 66 65 72 65 6e 74  ber of different
5f100 20 55 54 46 38 20 65 6e 63 6f 64 69 6e 67 73 0a   UTF8 encodings.
5f110 2a 2a 20 20 20 20 20 66 6f 72 20 75 6e 69 63 6f  **     for unico
5f120 64 65 20 76 61 6c 75 65 73 20 30 78 38 30 20 61  de values 0x80 a
5f130 6e 64 20 67 72 65 61 74 65 72 2e 20 20 49 74 20  nd greater.  It 
5f140 64 6f 20 6e 6f 74 20 63 68 61 6e 67 65 20 6f 76  do not change ov
5f150 65 72 2d 6c 65 6e 67 74 68 0a 2a 2a 20 20 20 20  er-length.**    
5f160 20 65 6e 63 6f 64 69 6e 67 73 20 74 6f 20 30 78   encodings to 0x
5f170 66 66 66 64 20 61 73 20 73 6f 6d 65 20 73 79 73  fffd as some sys
5f180 74 65 6d 73 20 72 65 63 6f 6d 6d 65 6e 64 2e 0a  tems recommend..
5f190 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
5f1a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66  E int sqlite3Utf
5f1b0 38 52 65 61 64 28 0a 20 20 63 6f 6e 73 74 20 75  8Read(.  const u
5f1c0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 2c  nsigned char *z,
5f1d0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73           /* Firs
5f1e0 74 20 62 79 74 65 20 6f 66 20 55 54 46 2d 38 20  t byte of UTF-8 
5f1f0 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20 63  character */.  c
5f200 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
5f210 61 72 20 2a 7a 54 65 72 6d 2c 20 20 20 20 20 2f  ar *zTerm,     /
5f220 2a 20 50 72 65 74 65 6e 64 20 74 68 69 73 20 62  * Pretend this b
5f230 79 74 65 20 69 73 20 30 78 30 30 20 2a 2f 0a 20  yte is 0x00 */. 
5f240 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
5f250 63 68 61 72 20 2a 2a 70 7a 4e 65 78 74 20 20 20  char **pzNext   
5f260 20 2f 2a 20 57 72 69 74 65 20 66 69 72 73 74 20   /* Write first 
5f270 62 79 74 65 20 70 61 73 74 20 55 54 46 2d 38 20  byte past UTF-8 
5f280 63 68 61 72 20 68 65 72 65 20 2a 2f 0a 29 7b 0a  char here */.){.
5f290 20 20 69 6e 74 20 63 20 3d 20 2a 28 7a 2b 2b 29    int c = *(z++)
5f2a0 3b 0a 20 20 69 66 28 20 63 3e 3d 30 78 63 30 20  ;.  if( c>=0xc0 
5f2b0 29 7b 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 74  ){.    c = sqlit
5f2c0 65 33 55 74 66 54 72 61 6e 73 31 5b 63 2d 30 78  e3UtfTrans1[c-0x
5f2d0 63 30 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  c0];.    while( 
5f2e0 7a 21 3d 7a 54 65 72 6d 20 26 26 20 28 2a 7a 20  z!=zTerm && (*z 
5f2f0 26 20 30 78 63 30 29 3d 3d 30 78 38 30 20 29 7b  & 0xc0)==0x80 ){
5f300 0a 20 20 20 20 20 20 63 20 3d 20 28 63 3c 3c 36  .      c = (c<<6
5f310 29 20 2b 20 28 30 78 33 66 20 26 20 2a 28 7a 2b  ) + (0x3f & *(z+
5f320 2b 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  +));.    }.    i
5f330 66 28 20 63 3c 30 78 38 30 0a 20 20 20 20 20 20  f( c<0x80.      
5f340 20 20 7c 7c 20 28 63 26 30 78 46 46 46 46 46 38    || (c&0xFFFFF8
5f350 30 30 29 3d 3d 30 78 44 38 30 30 0a 20 20 20 20  00)==0xD800.    
5f360 20 20 20 20 7c 7c 20 28 63 26 30 78 46 46 46 46      || (c&0xFFFF
5f370 46 46 46 45 29 3d 3d 30 78 46 46 46 45 20 29 7b  FFFE)==0xFFFE ){
5f380 20 20 63 20 3d 20 30 78 46 46 46 44 3b 20 7d 0a    c = 0xFFFD; }.
5f390 20 20 7d 0a 20 20 2a 70 7a 4e 65 78 74 20 3d 20    }.  *pzNext = 
5f3a0 7a 3b 0a 20 20 72 65 74 75 72 6e 20 63 3b 0a 7d  z;.  return c;.}
5f3b0 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  ..../*.** If the
5f3c0 20 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45   TRANSLATE_TRACE
5f3d0 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65   macro is define
5f3e0 64 2c 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  d, the value of 
5f3f0 65 61 63 68 20 4d 65 6d 20 69 73 0a 2a 2a 20 70  each Mem is.** p
5f400 72 69 6e 74 65 64 20 6f 6e 20 73 74 64 65 72 72  rinted on stderr
5f410 20 6f 6e 20 74 68 65 20 77 61 79 20 69 6e 74 6f   on the way into
5f420 20 61 6e 64 20 6f 75 74 20 6f 66 20 73 71 6c 69   and out of sqli
5f430 74 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c  te3VdbeMemTransl
5f440 61 74 65 28 29 2e 0a 2a 2f 20 0a 2f 2a 20 23 64  ate()..*/ ./* #d
5f450 65 66 69 6e 65 20 54 52 41 4e 53 4c 41 54 45 5f  efine TRANSLATE_
5f460 54 52 41 43 45 20 31 20 2a 2f 0a 0a 23 69 66 6e  TRACE 1 */..#ifn
5f470 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
5f480 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 54 68 69 73  UTF16./*.** This
5f490 20 72 6f 75 74 69 6e 65 20 74 72 61 6e 73 66 6f   routine transfo
5f4a0 72 6d 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  rms the internal
5f4b0 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 75   text encoding u
5f4c0 73 65 64 20 62 79 20 70 4d 65 6d 20 74 6f 0a 2a  sed by pMem to.*
5f4d0 2a 20 64 65 73 69 72 65 64 45 6e 63 2e 20 49 74  * desiredEnc. It
5f4e0 20 69 73 20 61 6e 20 65 72 72 6f 72 20 69 66 20   is an error if 
5f4f0 74 68 65 20 73 74 72 69 6e 67 20 69 73 20 61 6c  the string is al
5f500 72 65 61 64 79 20 6f 66 20 74 68 65 20 64 65 73  ready of the des
5f510 69 72 65 64 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67  ired.** encoding
5f520 2c 20 6f 72 20 69 66 20 2a 70 4d 65 6d 20 64 6f  , or if *pMem do
5f530 65 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61  es not contain a
5f540 20 73 74 72 69 6e 67 20 76 61 6c 75 65 2e 0a 2a   string value..*
5f550 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
5f560 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
5f570 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 4d 65 6d  MemTranslate(Mem
5f580 20 2a 70 4d 65 6d 2c 20 75 38 20 64 65 73 69 72   *pMem, u8 desir
5f590 65 64 45 6e 63 29 7b 0a 20 20 75 6e 73 69 67 6e  edEnc){.  unsign
5f5a0 65 64 20 63 68 61 72 20 7a 53 68 6f 72 74 5b 4e  ed char zShort[N
5f5b0 42 46 53 5d 3b 20 2f 2a 20 54 65 6d 70 6f 72 61  BFS]; /* Tempora
5f5c0 72 79 20 73 68 6f 72 74 20 6f 75 74 70 75 74 20  ry short output 
5f5d0 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20  buffer */.  int 
5f5e0 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  len;            
5f5f0 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d          /* Maxim
5f600 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 6f 75 74  um length of out
5f610 70 75 74 20 73 74 72 69 6e 67 20 69 6e 20 62 79  put string in by
5f620 74 65 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  tes */.  unsigne
5f630 64 20 63 68 61 72 20 2a 7a 4f 75 74 3b 20 20 20  d char *zOut;   
5f640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5f650 2a 20 4f 75 74 70 75 74 20 62 75 66 66 65 72 20  * Output buffer 
5f660 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  */.  unsigned ch
5f670 61 72 20 2a 7a 49 6e 3b 20 20 20 20 20 20 20 20  ar *zIn;        
5f680 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
5f690 70 75 74 20 69 74 65 72 61 74 6f 72 20 2a 2f 0a  put iterator */.
5f6a0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
5f6b0 2a 7a 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20  *zTerm;         
5f6c0 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 64 20 6f          /* End o
5f6d0 66 20 69 6e 70 75 74 20 2a 2f 0a 20 20 75 6e 73  f input */.  uns
5f6e0 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b 20 20  igned char *z;  
5f6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5f700 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 69 74 65     /* Output ite
5f710 72 61 74 6f 72 20 2a 2f 0a 20 20 75 6e 73 69 67  rator */.  unsig
5f720 6e 65 64 20 69 6e 74 20 63 3b 0a 0a 20 20 61 73  ned int c;..  as
5f730 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67  sert( pMem->flag
5f740 73 26 4d 45 4d 5f 53 74 72 20 29 3b 0a 20 20 61  s&MEM_Str );.  a
5f750 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 65 6e 63  ssert( pMem->enc
5f760 21 3d 64 65 73 69 72 65 64 45 6e 63 20 29 3b 0a  !=desiredEnc );.
5f770 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e    assert( pMem->
5f780 65 6e 63 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  enc!=0 );.  asse
5f790 72 74 28 20 70 4d 65 6d 2d 3e 6e 3e 3d 30 20 29  rt( pMem->n>=0 )
5f7a0 3b 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54  ;..#if defined(T
5f7b0 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 29 20  RANSLATE_TRACE) 
5f7c0 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  && defined(SQLIT
5f7d0 45 5f 44 45 42 55 47 29 0a 20 20 7b 0a 20 20 20  E_DEBUG).  {.   
5f7e0 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b   char zBuf[100];
5f7f0 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
5f800 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74 28 70  MemPrettyPrint(p
5f810 4d 65 6d 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20  Mem, zBuf);.    
5f820 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20  fprintf(stderr, 
5f830 22 49 4e 50 55 54 3a 20 20 25 73 5c 6e 22 2c 20  "INPUT:  %s\n", 
5f840 7a 42 75 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  zBuf);.  }.#endi
5f850 66 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 74  f..  /* If the t
5f860 72 61 6e 73 6c 61 74 69 6f 6e 20 69 73 20 62 65  ranslation is be
5f870 74 77 65 65 6e 20 55 54 46 2d 31 36 20 6c 69 74  tween UTF-16 lit
5f880 74 6c 65 20 61 6e 64 20 62 69 67 20 65 6e 64 69  tle and big endi
5f890 61 6e 2c 20 74 68 65 6e 20 0a 20 20 2a 2a 20 61  an, then .  ** a
5f8a0 6c 6c 20 74 68 61 74 20 69 73 20 72 65 71 75 69  ll that is requi
5f8b0 72 65 64 20 69 73 20 74 6f 20 73 77 61 70 20 74  red is to swap t
5f8c0 68 65 20 62 79 74 65 20 6f 72 64 65 72 2e 20 54  he byte order. T
5f8d0 68 69 73 20 63 61 73 65 20 69 73 20 68 61 6e 64  his case is hand
5f8e0 6c 65 64 0a 20 20 2a 2a 20 64 69 66 66 65 72 65  led.  ** differe
5f8f0 6e 74 6c 79 20 66 72 6f 6d 20 74 68 65 20 6f 74  ntly from the ot
5f900 68 65 72 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  hers..  */.  if(
5f910 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 53 51 4c 49   pMem->enc!=SQLI
5f920 54 45 5f 55 54 46 38 20 26 26 20 64 65 73 69 72  TE_UTF8 && desir
5f930 65 64 45 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54  edEnc!=SQLITE_UT
5f940 46 38 20 29 7b 0a 20 20 20 20 75 38 20 74 65 6d  F8 ){.    u8 tem
5f950 70 3b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20  p;.    int rc;. 
5f960 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
5f970 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61  dbeMemMakeWritea
5f980 62 6c 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 69  ble(pMem);.    i
5f990 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
5f9a0 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
5f9b0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  ( rc==SQLITE_NOM
5f9c0 45 4d 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75  EM );.      retu
5f9d0 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
5f9e0 0a 20 20 20 20 7d 0a 20 20 20 20 7a 49 6e 20 3d  .    }.    zIn =
5f9f0 20 28 75 38 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20   (u8*)pMem->z;. 
5fa00 20 20 20 7a 54 65 72 6d 20 3d 20 26 7a 49 6e 5b     zTerm = &zIn[
5fa10 70 4d 65 6d 2d 3e 6e 5d 3b 0a 20 20 20 20 77 68  pMem->n];.    wh
5fa20 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29  ile( zIn<zTerm )
5fa30 7b 0a 20 20 20 20 20 20 74 65 6d 70 20 3d 20 2a  {.      temp = *
5fa40 7a 49 6e 3b 0a 20 20 20 20 20 20 2a 7a 49 6e 20  zIn;.      *zIn 
5fa50 3d 20 2a 28 7a 49 6e 2b 31 29 3b 0a 20 20 20 20  = *(zIn+1);.    
5fa60 20 20 7a 49 6e 2b 2b 3b 0a 20 20 20 20 20 20 2a    zIn++;.      *
5fa70 7a 49 6e 2b 2b 20 3d 20 74 65 6d 70 3b 0a 20 20  zIn++ = temp;.  
5fa80 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e    }.    pMem->en
5fa90 63 20 3d 20 64 65 73 69 72 65 64 45 6e 63 3b 0a  c = desiredEnc;.
5faa0 20 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 6c 61      goto transla
5fab0 74 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f  te_out;.  }..  /
5fac0 2a 20 53 65 74 20 6c 65 6e 20 74 6f 20 74 68 65  * Set len to the
5fad0 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   maximum number 
5fae0 6f 66 20 62 79 74 65 73 20 72 65 71 75 69 72 65  of bytes require
5faf0 64 20 69 6e 20 74 68 65 20 6f 75 74 70 75 74 20  d in the output 
5fb00 62 75 66 66 65 72 2e 20 2a 2f 0a 20 20 69 66 28  buffer. */.  if(
5fb10 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c   desiredEnc==SQL
5fb20 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20  ITE_UTF8 ){.    
5fb30 2f 2a 20 57 68 65 6e 20 63 6f 6e 76 65 72 74 69  /* When converti
5fb40 6e 67 20 66 72 6f 6d 20 55 54 46 2d 31 36 2c 20  ng from UTF-16, 
5fb50 74 68 65 20 6d 61 78 69 6d 75 6d 20 67 72 6f 77  the maximum grow
5fb60 74 68 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 0a  th results from.
5fb70 20 20 20 20 2a 2a 20 74 72 61 6e 73 6c 61 74 69      ** translati
5fb80 6e 67 20 61 20 32 2d 62 79 74 65 20 63 68 61 72  ng a 2-byte char
5fb90 61 63 74 65 72 20 74 6f 20 61 20 34 2d 62 79 74  acter to a 4-byt
5fba0 65 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65  e UTF-8 characte
5fbb0 72 2e 0a 20 20 20 20 2a 2a 20 41 20 73 69 6e 67  r..    ** A sing
5fbc0 6c 65 20 62 79 74 65 20 69 73 20 72 65 71 75 69  le byte is requi
5fbd0 72 65 64 20 66 6f 72 20 74 68 65 20 6f 75 74 70  red for the outp
5fbe0 75 74 20 73 74 72 69 6e 67 0a 20 20 20 20 2a 2a  ut string.    **
5fbf0 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 2e   nul-terminator.
5fc00 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6c 65 6e 20  .    */.    len 
5fc10 3d 20 70 4d 65 6d 2d 3e 6e 20 2a 20 32 20 2b 20  = pMem->n * 2 + 
5fc20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  1;.  }else{.    
5fc30 2f 2a 20 57 68 65 6e 20 63 6f 6e 76 65 72 74 69  /* When converti
5fc40 6e 67 20 66 72 6f 6d 20 55 54 46 2d 38 20 74 6f  ng from UTF-8 to
5fc50 20 55 54 46 2d 31 36 20 74 68 65 20 6d 61 78 69   UTF-16 the maxi
5fc60 6d 75 6d 20 67 72 6f 77 74 68 20 69 73 20 63 61  mum growth is ca
5fc70 75 73 65 64 0a 20 20 20 20 2a 2a 20 77 68 65 6e  used.    ** when
5fc80 20 61 20 31 2d 62 79 74 65 20 55 54 46 2d 38 20   a 1-byte UTF-8 
5fc90 63 68 61 72 61 63 74 65 72 20 69 73 20 74 72 61  character is tra
5fca0 6e 73 6c 61 74 65 64 20 69 6e 74 6f 20 61 20 32  nslated into a 2
5fcb0 2d 62 79 74 65 20 55 54 46 2d 31 36 0a 20 20 20  -byte UTF-16.   
5fcc0 20 2a 2a 20 63 68 61 72 61 63 74 65 72 2e 20 54   ** character. T
5fcd0 77 6f 20 62 79 74 65 73 20 61 72 65 20 72 65 71  wo bytes are req
5fce0 75 69 72 65 64 20 69 6e 20 74 68 65 20 6f 75 74  uired in the out
5fcf0 70 75 74 20 62 75 66 66 65 72 20 66 6f 72 20 74  put buffer for t
5fd00 68 65 0a 20 20 20 20 2a 2a 20 6e 75 6c 2d 74 65  he.    ** nul-te
5fd10 72 6d 69 6e 61 74 6f 72 2e 0a 20 20 20 20 2a 2f  rminator..    */
5fd20 0a 20 20 20 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d  .    len = pMem-
5fd30 3e 6e 20 2a 20 32 20 2b 20 32 3b 0a 20 20 7d 0a  >n * 2 + 2;.  }.
5fd40 0a 20 20 2f 2a 20 53 65 74 20 7a 49 6e 20 74 6f  .  /* Set zIn to
5fd50 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 73 74   point at the st
5fd60 61 72 74 20 6f 66 20 74 68 65 20 69 6e 70 75 74  art of the input
5fd70 20 62 75 66 66 65 72 20 61 6e 64 20 7a 54 65 72   buffer and zTer
5fd80 6d 20 74 6f 20 70 6f 69 6e 74 20 31 0a 20 20 2a  m to point 1.  *
5fd90 2a 20 62 79 74 65 20 70 61 73 74 20 74 68 65 20  * byte past the 
5fda0 65 6e 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56  end..  **.  ** V
5fdb0 61 72 69 61 62 6c 65 20 7a 4f 75 74 20 69 73 20  ariable zOut is 
5fdc0 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 61 74 20  set to point at 
5fdd0 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65  the output buffe
5fde0 72 2e 20 54 68 69 73 20 6d 61 79 20 62 65 20 73  r. This may be s
5fdf0 70 61 63 65 0a 20 20 2a 2a 20 6f 62 74 61 69 6e  pace.  ** obtain
5fe00 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29  ed from malloc()
5fe10 2c 20 6f 72 20 4d 65 6d 2e 7a 53 68 6f 72 74 2c  , or Mem.zShort,
5fe20 20 69 66 20 69 74 20 6c 61 72 67 65 20 65 6e 6f   if it large eno
5fe30 75 67 68 20 61 6e 64 20 6e 6f 74 20 69 6e 0a 20  ugh and not in. 
5fe40 20 2a 2a 20 75 73 65 2c 20 6f 72 20 74 68 65 20   ** use, or the 
5fe50 7a 53 68 6f 72 74 20 61 72 72 61 79 20 6f 6e 20  zShort array on 
5fe60 74 68 65 20 73 74 61 63 6b 20 28 73 65 65 20 61  the stack (see a
5fe70 62 6f 76 65 29 2e 0a 20 20 2a 2f 0a 20 20 7a 49  bove)..  */.  zI
5fe80 6e 20 3d 20 28 75 38 2a 29 70 4d 65 6d 2d 3e 7a  n = (u8*)pMem->z
5fe90 3b 0a 20 20 7a 54 65 72 6d 20 3d 20 26 7a 49 6e  ;.  zTerm = &zIn
5fea0 5b 70 4d 65 6d 2d 3e 6e 5d 3b 0a 20 20 69 66 28  [pMem->n];.  if(
5feb0 20 6c 65 6e 3e 4e 42 46 53 20 29 7b 0a 20 20 20   len>NBFS ){.   
5fec0 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 4d 61   zOut = sqliteMa
5fed0 6c 6c 6f 63 52 61 77 28 6c 65 6e 29 3b 0a 20 20  llocRaw(len);.  
5fee0 20 20 69 66 28 20 21 7a 4f 75 74 20 29 20 72 65    if( !zOut ) re
5fef0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
5ff00 4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  M;.  }else{.    
5ff10 7a 4f 75 74 20 3d 20 7a 53 68 6f 72 74 3b 0a 20  zOut = zShort;. 
5ff20 20 7d 0a 20 20 7a 20 3d 20 7a 4f 75 74 3b 0a 0a   }.  z = zOut;..
5ff30 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 6e 63 3d    if( pMem->enc=
5ff40 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a  =SQLITE_UTF8 ){.
5ff50 20 20 20 20 69 66 28 20 64 65 73 69 72 65 64 45      if( desiredE
5ff60 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36  nc==SQLITE_UTF16
5ff70 4c 45 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 55  LE ){.      /* U
5ff80 54 46 2d 38 20 2d 3e 20 55 54 46 2d 31 36 20 4c  TF-8 -> UTF-16 L
5ff90 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 2a 2f 0a  ittle-endian */.
5ffa0 20 20 20 20 20 20 77 68 69 6c 65 28 20 7a 49 6e        while( zIn
5ffb0 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20  <zTerm ){.      
5ffc0 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66    c = sqlite3Utf
5ffd0 38 52 65 61 64 28 7a 49 6e 2c 20 7a 54 65 72 6d  8Read(zIn, zTerm
5ffe0 2c 20 28 63 6f 6e 73 74 20 75 38 2a 2a 29 26 7a  , (const u8**)&z
5fff0 49 6e 29 3b 0a 20 20 20 20 20 20 20 20 57 52 49  In);.        WRI
60000 54 45 5f 55 54 46 31 36 4c 45 28 7a 2c 20 63 29  TE_UTF16LE(z, c)
60010 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
60020 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72  lse{.      asser
60030 74 28 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53  t( desiredEnc==S
60040 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b  QLITE_UTF16BE );
60050 0a 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20  .      /* UTF-8 
60060 2d 3e 20 55 54 46 2d 31 36 20 42 69 67 2d 65 6e  -> UTF-16 Big-en
60070 64 69 61 6e 20 2a 2f 0a 20 20 20 20 20 20 77 68  dian */.      wh
60080 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29  ile( zIn<zTerm )
60090 7b 0a 20 20 20 20 20 20 20 20 63 20 3d 20 73 71  {.        c = sq
600a0 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 49  lite3Utf8Read(zI
600b0 6e 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74  n, zTerm, (const
600c0 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 0a 20 20 20   u8**)&zIn);.   
600d0 20 20 20 20 20 57 52 49 54 45 5f 55 54 46 31 36       WRITE_UTF16
600e0 42 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20  BE(z, c);.      
600f0 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d  }.    }.    pMem
60100 2d 3e 6e 20 3d 20 7a 20 2d 20 7a 4f 75 74 3b 0a  ->n = z - zOut;.
60110 20 20 20 20 2a 7a 2b 2b 20 3d 20 30 3b 0a 20 20      *z++ = 0;.  
60120 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72  }else{.    asser
60130 74 28 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53  t( desiredEnc==S
60140 51 4c 49 54 45 5f 55 54 46 38 20 29 3b 0a 20 20  QLITE_UTF8 );.  
60150 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 6e 63 3d    if( pMem->enc=
60160 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20  =SQLITE_UTF16LE 
60170 29 7b 0a 20 20 20 20 20 20 2f 2a 20 55 54 46 2d  ){.      /* UTF-
60180 31 36 20 4c 69 74 74 6c 65 2d 65 6e 64 69 61 6e  16 Little-endian
60190 20 2d 3e 20 55 54 46 2d 38 20 2a 2f 0a 20 20 20   -> UTF-8 */.   
601a0 20 20 20 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54     while( zIn<zT
601b0 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 52  erm ){.        R
601c0 45 41 44 5f 55 54 46 31 36 4c 45 28 7a 49 6e 2c  EAD_UTF16LE(zIn,
601d0 20 63 29 3b 20 0a 20 20 20 20 20 20 20 20 57 52   c); .        WR
601e0 49 54 45 5f 55 54 46 38 28 7a 2c 20 63 29 3b 0a  ITE_UTF8(z, c);.
601f0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
60200 65 7b 0a 20 20 20 20 20 20 2f 2a 20 55 54 46 2d  e{.      /* UTF-
60210 31 36 20 4c 69 74 74 6c 65 2d 65 6e 64 69 61 6e  16 Little-endian
60220 20 2d 3e 20 55 54 46 2d 38 20 2a 2f 0a 20 20 20   -> UTF-8 */.   
60230 20 20 20 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54     while( zIn<zT
60240 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 52  erm ){.        R
60250 45 41 44 5f 55 54 46 31 36 42 45 28 7a 49 6e 2c  EAD_UTF16BE(zIn,
60260 20 63 29 3b 20 0a 20 20 20 20 20 20 20 20 57 52   c); .        WR
60270 49 54 45 5f 55 54 46 38 28 7a 2c 20 63 29 3b 0a  ITE_UTF8(z, c);.
60280 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
60290 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 7a 20 2d 20    pMem->n = z - 
602a0 7a 4f 75 74 3b 0a 20 20 7d 0a 20 20 2a 7a 20 3d  zOut;.  }.  *z =
602b0 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70   0;.  assert( (p
602c0 4d 65 6d 2d 3e 6e 2b 28 64 65 73 69 72 65 64 45  Mem->n+(desiredE
602d0 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f  nc==SQLITE_UTF8?
602e0 31 3a 32 29 29 3c 3d 6c 65 6e 20 29 3b 0a 0a 20  1:2))<=len );.. 
602f0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
60300 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20  elease(pMem);.  
60310 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 7e  pMem->flags &= ~
60320 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f  (MEM_Static|MEM_
60330 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45  Dyn|MEM_Ephem|ME
60340 4d 5f 53 68 6f 72 74 29 3b 0a 20 20 70 4d 65 6d  M_Short);.  pMem
60350 2d 3e 65 6e 63 20 3d 20 64 65 73 69 72 65 64 45  ->enc = desiredE
60360 6e 63 3b 0a 20 20 69 66 28 20 7a 4f 75 74 3d 3d  nc;.  if( zOut==
60370 7a 53 68 6f 72 74 20 29 7b 0a 20 20 20 20 6d 65  zShort ){.    me
60380 6d 63 70 79 28 70 4d 65 6d 2d 3e 7a 53 68 6f 72  mcpy(pMem->zShor
60390 74 2c 20 7a 4f 75 74 2c 20 6c 65 6e 29 3b 0a 20  t, zOut, len);. 
603a0 20 20 20 7a 4f 75 74 20 3d 20 28 75 38 2a 29 70     zOut = (u8*)p
603b0 4d 65 6d 2d 3e 7a 53 68 6f 72 74 3b 0a 20 20 20  Mem->zShort;.   
603c0 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20   pMem->flags |= 
603d0 28 4d 45 4d 5f 54 65 72 6d 7c 4d 45 4d 5f 53 68  (MEM_Term|MEM_Sh
603e0 6f 72 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ort);.  }else{. 
603f0 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c     pMem->flags |
60400 3d 20 28 4d 45 4d 5f 54 65 72 6d 7c 4d 45 4d 5f  = (MEM_Term|MEM_
60410 44 79 6e 29 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d  Dyn);.  }.  pMem
60420 2d 3e 7a 20 3d 20 28 63 68 61 72 2a 29 7a 4f 75  ->z = (char*)zOu
60430 74 3b 0a 0a 74 72 61 6e 73 6c 61 74 65 5f 6f 75  t;..translate_ou
60440 74 3a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54  t:.#if defined(T
60450 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 29 20  RANSLATE_TRACE) 
60460 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  && defined(SQLIT
60470 45 5f 44 45 42 55 47 29 0a 20 20 7b 0a 20 20 20  E_DEBUG).  {.   
60480 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b   char zBuf[100];
60490 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
604a0 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74 28 70  MemPrettyPrint(p
604b0 4d 65 6d 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20  Mem, zBuf);.    
604c0 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20  fprintf(stderr, 
604d0 22 4f 55 54 50 55 54 3a 20 25 73 5c 6e 22 2c 20  "OUTPUT: %s\n", 
604e0 7a 42 75 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  zBuf);.  }.#endi
604f0 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  f.  return SQLIT
60500 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  E_OK;.}../*.** T
60510 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63  his routine chec
60520 6b 73 20 66 6f 72 20 61 20 62 79 74 65 2d 6f 72  ks for a byte-or
60530 64 65 72 20 6d 61 72 6b 20 61 74 20 74 68 65 20  der mark at the 
60540 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65  beginning of the
60550 20 0a 2a 2a 20 55 54 46 2d 31 36 20 73 74 72 69   .** UTF-16 stri
60560 6e 67 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d  ng stored in *pM
60570 65 6d 2e 20 49 66 20 6f 6e 65 20 69 73 20 70 72  em. If one is pr
60580 65 73 65 6e 74 2c 20 69 74 20 69 73 20 72 65 6d  esent, it is rem
60590 6f 76 65 64 20 61 6e 64 0a 2a 2a 20 74 68 65 20  oved and.** the 
605a0 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20  encoding of the 
605b0 4d 65 6d 20 61 64 6a 75 73 74 65 64 2e 20 54 68  Mem adjusted. Th
605c0 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20  is routine does 
605d0 6e 6f 74 20 64 6f 20 61 6e 79 0a 2a 2a 20 62 79  not do any.** by
605e0 74 65 2d 73 77 61 70 70 69 6e 67 2c 20 69 74 20  te-swapping, it 
605f0 6a 75 73 74 20 73 65 74 73 20 4d 65 6d 2e 65 6e  just sets Mem.en
60600 63 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e  c appropriately.
60610 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 6c 6c 6f 63  .**.** The alloc
60620 61 74 69 6f 6e 20 28 73 74 61 74 69 63 2c 20 64  ation (static, d
60630 79 6e 61 6d 69 63 20 65 74 63 2e 29 20 61 6e 64  ynamic etc.) and
60640 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65   encoding of the
60650 20 4d 65 6d 20 6d 61 79 20 62 65 0a 2a 2a 20 63   Mem may be.** c
60660 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 66  hanged by this f
60670 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49  unction..*/.SQLI
60680 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
60690 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 48 61 6e  qlite3VdbeMemHan
606a0 64 6c 65 42 6f 6d 28 4d 65 6d 20 2a 70 4d 65 6d  dleBom(Mem *pMem
606b0 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
606c0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 62 6f  LITE_OK;.  u8 bo
606d0 6d 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 70 4d  m = 0;..  if( pM
606e0 65 6d 2d 3e 6e 3c 30 20 7c 7c 20 70 4d 65 6d 2d  em->n<0 || pMem-
606f0 3e 6e 3e 31 20 29 7b 0a 20 20 20 20 75 38 20 62  >n>1 ){.    u8 b
60700 31 20 3d 20 2a 28 75 38 20 2a 29 70 4d 65 6d 2d  1 = *(u8 *)pMem-
60710 3e 7a 3b 0a 20 20 20 20 75 38 20 62 32 20 3d 20  >z;.    u8 b2 = 
60720 2a 28 28 28 75 38 20 2a 29 70 4d 65 6d 2d 3e 7a  *(((u8 *)pMem->z
60730 29 20 2b 20 31 29 3b 0a 20 20 20 20 69 66 28 20  ) + 1);.    if( 
60740 62 31 3d 3d 30 78 46 45 20 26 26 20 62 32 3d 3d  b1==0xFE && b2==
60750 30 78 46 46 20 29 7b 0a 20 20 20 20 20 20 62 6f  0xFF ){.      bo
60760 6d 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36  m = SQLITE_UTF16
60770 42 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  BE;.    }.    if
60780 28 20 62 31 3d 3d 30 78 46 46 20 26 26 20 62 32  ( b1==0xFF && b2
60790 3d 3d 30 78 46 45 20 29 7b 0a 20 20 20 20 20 20  ==0xFE ){.      
607a0 62 6f 6d 20 3d 20 53 51 4c 49 54 45 5f 55 54 46  bom = SQLITE_UTF
607b0 31 36 4c 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  16LE;.    }.  }.
607c0 20 20 0a 20 20 69 66 28 20 62 6f 6d 20 29 7b 0a    .  if( bom ){.
607d0 20 20 20 20 2f 2a 20 54 68 69 73 20 66 75 6e 63      /* This func
607e0 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61  tion is called a
607f0 73 20 73 6f 6f 6e 20 61 73 20 61 20 73 74 72 69  s soon as a stri
60800 6e 67 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  ng is stored in 
60810 61 20 4d 65 6d 2a 2c 0a 20 20 20 20 2a 2a 20 66  a Mem*,.    ** f
60820 72 6f 6d 20 77 69 74 68 69 6e 20 73 71 6c 69 74  rom within sqlit
60830 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28  e3VdbeMemSetStr(
60840 29 2e 20 41 74 20 74 68 61 74 20 70 6f 69 6e 74  ). At that point
60850 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69   it is not possi
60860 62 6c 65 0a 20 20 20 20 2a 2a 20 66 6f 72 20 74  ble.    ** for t
60870 68 65 20 73 74 72 69 6e 67 20 74 6f 20 62 65 20  he string to be 
60880 73 74 6f 72 65 64 20 69 6e 20 4d 65 6d 2e 7a 53  stored in Mem.zS
60890 68 6f 72 74 2c 20 6f 72 20 66 6f 72 20 69 74 20  hort, or for it 
608a0 74 6f 20 62 65 20 73 74 6f 72 65 64 0a 20 20 20  to be stored.   
608b0 20 2a 2a 20 69 6e 20 64 79 6e 61 6d 69 63 20 6d   ** in dynamic m
608c0 65 6d 6f 72 79 20 77 69 74 68 20 6e 6f 20 64 65  emory with no de
608d0 73 74 72 75 63 74 6f 72 2e 0a 20 20 20 20 2a 2f  structor..    */
608e0 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 28 70  .    assert( !(p
608f0 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53  Mem->flags&MEM_S
60900 68 6f 72 74 29 20 29 3b 0a 20 20 20 20 61 73 73  hort) );.    ass
60910 65 72 74 28 20 21 28 70 4d 65 6d 2d 3e 66 6c 61  ert( !(pMem->fla
60920 67 73 26 4d 45 4d 5f 44 79 6e 29 20 7c 7c 20 70  gs&MEM_Dyn) || p
60930 4d 65 6d 2d 3e 78 44 65 6c 20 29 3b 0a 20 20 20  Mem->xDel );.   
60940 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73   if( pMem->flags
60950 20 26 20 4d 45 4d 5f 44 79 6e 20 29 7b 0a 20 20   & MEM_Dyn ){.  
60960 20 20 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29      void (*xDel)
60970 28 76 6f 69 64 2a 29 20 3d 20 70 4d 65 6d 2d 3e  (void*) = pMem->
60980 78 44 65 6c 3b 0a 20 20 20 20 20 20 63 68 61 72  xDel;.      char
60990 20 2a 7a 20 3d 20 70 4d 65 6d 2d 3e 7a 3b 0a 20   *z = pMem->z;. 
609a0 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 30       pMem->z = 0
609b0 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 78 44  ;.      pMem->xD
609c0 65 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63  el = 0;.      rc
609d0 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65   = sqlite3VdbeMe
609e0 6d 53 65 74 53 74 72 28 70 4d 65 6d 2c 20 26 7a  mSetStr(pMem, &z
609f0 5b 32 5d 2c 20 70 4d 65 6d 2d 3e 6e 2d 32 2c 20  [2], pMem->n-2, 
60a00 62 6f 6d 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e  bom, SQLITE_TRAN
60a10 53 49 45 4e 54 29 3b 0a 20 20 20 20 20 20 78 44  SIENT);.      xD
60a20 65 6c 28 7a 29 3b 0a 20 20 20 20 7d 65 6c 73 65  el(z);.    }else
60a30 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
60a40 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74  ite3VdbeMemSetSt
60a50 72 28 70 4d 65 6d 2c 20 26 70 4d 65 6d 2d 3e 7a  r(pMem, &pMem->z
60a60 5b 32 5d 2c 20 70 4d 65 6d 2d 3e 6e 2d 32 2c 20  [2], pMem->n-2, 
60a70 62 6f 6d 2c 20 0a 20 20 20 20 20 20 20 20 20 20  bom, .          
60a80 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
60a90 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  );.    }.  }.  r
60aa0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64  eturn rc;.}.#end
60ab0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
60ac0 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a  T_UTF16 */../*.*
60ad0 2a 20 70 5a 20 69 73 20 61 20 55 54 46 2d 38 20  * pZ is a UTF-8 
60ae0 65 6e 63 6f 64 65 64 20 75 6e 69 63 6f 64 65 20  encoded unicode 
60af0 73 74 72 69 6e 67 2e 20 49 66 20 6e 42 79 74 65  string. If nByte
60b00 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65   is less than ze
60b10 72 6f 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 74 68  ro,.** return th
60b20 65 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 63  e number of unic
60b30 6f 64 65 20 63 68 61 72 61 63 74 65 72 73 20 69  ode characters i
60b40 6e 20 70 5a 20 75 70 20 74 6f 20 28 62 75 74 20  n pZ up to (but 
60b50 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67 29 0a 2a  not including).*
60b60 2a 20 74 68 65 20 66 69 72 73 74 20 30 78 30 30  * the first 0x00
60b70 20 62 79 74 65 2e 20 49 66 20 6e 42 79 74 65 20   byte. If nByte 
60b80 69 73 20 6e 6f 74 20 6c 65 73 73 20 74 68 61 6e  is not less than
60b90 20 7a 65 72 6f 2c 20 72 65 74 75 72 6e 20 74 68   zero, return th
60ba0 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 75  e.** number of u
60bb0 6e 69 63 6f 64 65 20 63 68 61 72 61 63 74 65 72  nicode character
60bc0 73 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 6e  s in the first n
60bd0 42 79 74 65 20 6f 66 20 70 5a 20 28 6f 72 20 75  Byte of pZ (or u
60be0 70 20 74 6f 20 0a 2a 2a 20 74 68 65 20 66 69 72  p to .** the fir
60bf0 73 74 20 30 78 30 30 2c 20 77 68 69 63 68 65 76  st 0x00, whichev
60c00 65 72 20 63 6f 6d 65 73 20 66 69 72 73 74 29 2e  er comes first).
60c10 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
60c20 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74  TE int sqlite3Ut
60c30 66 38 43 68 61 72 4c 65 6e 28 63 6f 6e 73 74 20  f8CharLen(const 
60c40 63 68 61 72 20 2a 7a 49 6e 2c 20 69 6e 74 20 6e  char *zIn, int n
60c50 42 79 74 65 29 7b 0a 20 20 69 6e 74 20 72 20 3d  Byte){.  int r =
60c60 20 30 3b 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a   0;.  const u8 *
60c70 7a 20 3d 20 28 63 6f 6e 73 74 20 75 38 2a 29 7a  z = (const u8*)z
60c80 49 6e 3b 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a  In;.  const u8 *
60c90 7a 54 65 72 6d 3b 0a 20 20 69 66 28 20 6e 42 79  zTerm;.  if( nBy
60ca0 74 65 3e 3d 30 20 29 7b 0a 20 20 20 20 7a 54 65  te>=0 ){.    zTe
60cb0 72 6d 20 3d 20 26 7a 5b 6e 42 79 74 65 5d 3b 0a  rm = &z[nByte];.
60cc0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 54 65    }else{.    zTe
60cd0 72 6d 20 3d 20 28 63 6f 6e 73 74 20 75 38 2a 29  rm = (const u8*)
60ce0 28 2d 31 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65  (-1);.  }.  asse
60cf0 72 74 28 20 7a 3c 3d 7a 54 65 72 6d 20 29 3b 0a  rt( z<=zTerm );.
60d00 20 20 77 68 69 6c 65 28 20 2a 7a 21 3d 30 20 26    while( *z!=0 &
60d10 26 20 7a 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20  & z<zTerm ){.   
60d20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46   SQLITE_SKIP_UTF
60d30 38 28 7a 29 3b 0a 20 20 20 20 72 2b 2b 3b 0a 20  8(z);.    r++;. 
60d40 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 3b 0a 7d   }.  return r;.}
60d50 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
60d60 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a  _OMIT_UTF16./*.*
60d70 2a 20 43 6f 6e 76 65 72 74 20 61 20 55 54 46 2d  * Convert a UTF-
60d80 31 36 20 73 74 72 69 6e 67 20 69 6e 20 74 68 65  16 string in the
60d90 20 6e 61 74 69 76 65 20 65 6e 63 6f 64 69 6e 67   native encoding
60da0 20 69 6e 74 6f 20 61 20 55 54 46 2d 38 20 73 74   into a UTF-8 st
60db0 72 69 6e 67 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20  ring..** Memory 
60dc0 74 6f 20 68 6f 6c 64 20 74 68 65 20 55 54 46 2d  to hold the UTF-
60dd0 38 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61  8 string is obta
60de0 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63  ined from malloc
60df0 20 61 6e 64 20 6d 75 73 74 20 62 65 0a 2a 2a 20   and must be.** 
60e00 66 72 65 65 64 20 62 79 20 74 68 65 20 63 61 6c  freed by the cal
60e10 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  ling function..*
60e20 2a 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74  *.** NULL is ret
60e30 75 72 6e 65 64 20 69 66 20 74 68 65 72 65 20 69  urned if there i
60e40 73 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  s an allocation 
60e50 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  error..*/.SQLITE
60e60 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73  _PRIVATE char *s
60e70 71 6c 69 74 65 33 55 74 66 31 36 74 6f 38 28 63  qlite3Utf16to8(c
60e80 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e  onst void *z, in
60e90 74 20 6e 42 79 74 65 29 7b 0a 20 20 4d 65 6d 20  t nByte){.  Mem 
60ea0 6d 3b 0a 20 20 6d 65 6d 73 65 74 28 26 6d 2c 20  m;.  memset(&m, 
60eb0 30 2c 20 73 69 7a 65 6f 66 28 6d 29 29 3b 0a 20  0, sizeof(m));. 
60ec0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
60ed0 65 74 53 74 72 28 26 6d 2c 20 7a 2c 20 6e 42 79  etStr(&m, z, nBy
60ee0 74 65 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36  te, SQLITE_UTF16
60ef0 4e 41 54 49 56 45 2c 20 53 51 4c 49 54 45 5f 53  NATIVE, SQLITE_S
60f00 54 41 54 49 43 29 3b 0a 20 20 73 71 6c 69 74 65  TATIC);.  sqlite
60f10 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64  3VdbeChangeEncod
60f20 69 6e 67 28 26 6d 2c 20 53 51 4c 49 54 45 5f 55  ing(&m, SQLITE_U
60f30 54 46 38 29 3b 0a 20 20 61 73 73 65 72 74 28 20  TF8);.  assert( 
60f40 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 54  (m.flags & MEM_T
60f50 65 72 6d 29 21 3d 30 20 7c 7c 20 73 71 6c 69 74  erm)!=0 || sqlit
60f60 65 33 4d 61 6c 6c 6f 63 46 61 69 6c 65 64 28 29  e3MallocFailed()
60f70 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d   );.  assert( (m
60f80 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72  .flags & MEM_Str
60f90 29 21 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 4d  )!=0 || sqlite3M
60fa0 61 6c 6c 6f 63 46 61 69 6c 65 64 28 29 20 29 3b  allocFailed() );
60fb0 0a 20 20 72 65 74 75 72 6e 20 28 6d 2e 66 6c 61  .  return (m.fla
60fc0 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 21 3d 30  gs & MEM_Dyn)!=0
60fd0 20 3f 20 6d 2e 7a 20 3a 20 73 71 6c 69 74 65 53   ? m.z : sqliteS
60fe0 74 72 44 75 70 28 6d 2e 7a 29 3b 0a 7d 0a 0a 2f  trDup(m.z);.}../
60ff0 2a 0a 2a 2a 20 70 5a 20 69 73 20 61 20 55 54 46  *.** pZ is a UTF
61000 2d 31 36 20 65 6e 63 6f 64 65 64 20 75 6e 69 63  -16 encoded unic
61010 6f 64 65 20 73 74 72 69 6e 67 2e 20 49 66 20 6e  ode string. If n
61020 43 68 61 72 20 69 73 20 6c 65 73 73 20 74 68 61  Char is less tha
61030 6e 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65 74 75 72  n zero,.** retur
61040 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
61050 62 79 74 65 73 20 75 70 20 74 6f 20 28 62 75 74  bytes up to (but
61060 20 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67 29 2c   not including),
61070 20 74 68 65 20 66 69 72 73 74 20 70 61 69 72 0a   the first pair.
61080 2a 2a 20 6f 66 20 63 6f 6e 73 65 63 75 74 69 76  ** of consecutiv
61090 65 20 30 78 30 30 20 62 79 74 65 73 20 69 6e 20  e 0x00 bytes in 
610a0 70 5a 2e 20 49 66 20 6e 43 68 61 72 20 69 73 20  pZ. If nChar is 
610b0 6e 6f 74 20 6c 65 73 73 20 74 68 61 6e 20 7a 65  not less than ze
610c0 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 72 65 74 75  ro,.** then retu
610d0 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
610e0 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 66 69   bytes in the fi
610f0 72 73 74 20 6e 43 68 61 72 20 75 6e 69 63 6f 64  rst nChar unicod
61100 65 20 63 68 61 72 61 63 74 65 72 73 0a 2a 2a 20  e characters.** 
61110 69 6e 20 70 5a 20 28 6f 72 20 75 70 20 75 6e 74  in pZ (or up unt
61120 69 6c 20 74 68 65 20 66 69 72 73 74 20 70 61 69  il the first pai
61130 72 20 6f 66 20 30 78 30 30 20 62 79 74 65 73 2c  r of 0x00 bytes,
61140 20 77 68 69 63 68 65 76 65 72 20 63 6f 6d 65 73   whichever comes
61150 20 66 69 72 73 74 29 2e 0a 2a 2f 0a 53 51 4c 49   first)..*/.SQLI
61160 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
61170 71 6c 69 74 65 33 55 74 66 31 36 42 79 74 65 4c  qlite3Utf16ByteL
61180 65 6e 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  en(const void *z
61190 49 6e 2c 20 69 6e 74 20 6e 43 68 61 72 29 7b 0a  In, int nChar){.
611a0 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 63    unsigned int c
611b0 20 3d 20 31 3b 0a 20 20 63 68 61 72 20 63 6f 6e   = 1;.  char con
611c0 73 74 20 2a 7a 20 3d 20 7a 49 6e 3b 0a 20 20 69  st *z = zIn;.  i
611d0 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20  nt n = 0;.  if( 
611e0 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49  SQLITE_UTF16NATI
611f0 56 45 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36  VE==SQLITE_UTF16
61200 42 45 20 29 7b 0a 20 20 20 20 2f 2a 20 55 73 69  BE ){.    /* Usi
61210 6e 67 20 61 6e 20 22 69 66 20 28 53 51 4c 49 54  ng an "if (SQLIT
61220 45 5f 55 54 46 31 36 4e 41 54 49 56 45 3d 3d 53  E_UTF16NATIVE==S
61230 51 4c 49 54 45 5f 55 54 46 31 36 42 45 29 22 20  QLITE_UTF16BE)" 
61240 63 6f 6e 73 74 72 75 63 74 20 68 65 72 65 0a 20  construct here. 
61250 20 20 20 2a 2a 20 61 6e 64 20 69 6e 20 6f 74 68     ** and in oth
61260 65 72 20 70 61 72 74 73 20 6f 66 20 74 68 69 73  er parts of this
61270 20 66 69 6c 65 20 6d 65 61 6e 73 20 74 68 61 74   file means that
61280 20 61 74 20 6f 6e 65 20 62 72 61 6e 63 68 20 77   at one branch w
61290 69 6c 6c 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 62  ill.    ** not b
612a0 65 20 63 6f 76 65 72 65 64 20 62 79 20 63 6f 76  e covered by cov
612b0 65 72 61 67 65 20 74 65 73 74 69 6e 67 20 6f 6e  erage testing on
612c0 20 61 6e 79 20 73 69 6e 67 6c 65 20 68 6f 73 74   any single host
612d0 2e 20 42 75 74 20 63 6f 76 65 72 61 67 65 0a 20  . But coverage. 
612e0 20 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 63 6f     ** will be co
612f0 6d 70 6c 65 74 65 20 69 66 20 74 68 65 20 74 65  mplete if the te
61300 73 74 73 20 61 72 65 20 72 75 6e 20 6f 6e 20 62  sts are run on b
61310 6f 74 68 20 61 20 6c 69 74 74 6c 65 2d 65 6e 64  oth a little-end
61320 69 61 6e 20 61 6e 64 20 0a 20 20 20 20 2a 2a 20  ian and .    ** 
61330 62 69 67 2d 65 6e 64 69 61 6e 20 68 6f 73 74 2e  big-endian host.
61340 20 42 65 63 61 75 73 65 20 62 6f 74 68 20 74 68   Because both th
61350 65 20 55 54 46 31 36 4e 41 54 49 56 45 20 61 6e  e UTF16NATIVE an
61360 64 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45  d SQLITE_UTF16BE
61370 0a 20 20 20 20 2a 2a 20 6d 61 63 72 6f 73 20 61  .    ** macros a
61380 72 65 20 63 6f 6e 73 74 61 6e 74 20 61 74 20 63  re constant at c
61390 6f 6d 70 69 6c 65 20 74 69 6d 65 20 74 68 65 20  ompile time the 
613a0 63 6f 6d 70 69 6c 65 72 20 63 61 6e 20 64 65 74  compiler can det
613b0 65 72 6d 69 6e 65 0a 20 20 20 20 2a 2a 20 77 68  ermine.    ** wh
613c0 69 63 68 20 62 72 61 6e 63 68 20 77 69 6c 6c 20  ich branch will 
613d0 62 65 20 66 6f 6c 6c 6f 77 65 64 2e 20 49 74 20  be followed. It 
613e0 69 73 20 74 68 65 72 65 66 6f 72 65 20 61 73 73  is therefore ass
613f0 75 6d 65 64 20 74 68 61 74 20 6e 6f 20 72 75 6e  umed that no run
61400 74 69 6d 65 0a 20 20 20 20 2a 2a 20 70 65 6e 61  time.    ** pena
61410 6c 74 79 20 69 73 20 70 61 69 64 20 66 6f 72 20  lty is paid for 
61420 74 68 69 73 20 22 69 66 22 20 73 74 61 74 65 6d  this "if" statem
61430 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ent..    */.    
61440 77 68 69 6c 65 28 20 63 20 26 26 20 28 28 6e 43  while( c && ((nC
61450 68 61 72 3c 30 29 20 7c 7c 20 6e 3c 6e 43 68 61  har<0) || n<nCha
61460 72 29 20 29 7b 0a 20 20 20 20 20 20 52 45 41 44  r) ){.      READ
61470 5f 55 54 46 31 36 42 45 28 7a 2c 20 63 29 3b 0a  _UTF16BE(z, c);.
61480 20 20 20 20 20 20 6e 2b 2b 3b 0a 20 20 20 20 7d        n++;.    }
61490 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 77 68  .  }else{.    wh
614a0 69 6c 65 28 20 63 20 26 26 20 28 28 6e 43 68 61  ile( c && ((nCha
614b0 72 3c 30 29 20 7c 7c 20 6e 3c 6e 43 68 61 72 29  r<0) || n<nChar)
614c0 20 29 7b 0a 20 20 20 20 20 20 52 45 41 44 5f 55   ){.      READ_U
614d0 54 46 31 36 4c 45 28 7a 2c 20 63 29 3b 0a 20 20  TF16LE(z, c);.  
614e0 20 20 20 20 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20      n++;.    }. 
614f0 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 7a 2d 28   }.  return (z-(
61500 63 68 61 72 20 63 6f 6e 73 74 20 2a 29 7a 49 6e  char const *)zIn
61510 29 2d 28 28 63 3d 3d 30 29 3f 32 3a 30 29 3b 0a  )-((c==0)?2:0);.
61520 7d 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  }..#if defined(S
61530 51 4c 49 54 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a  QLITE_TEST)./*.*
61540 2a 20 54 72 61 6e 73 6c 61 74 65 20 55 54 46 2d  * Translate UTF-
61550 38 20 74 6f 20 55 54 46 2d 38 2e 0a 2a 2a 0a 2a  8 to UTF-8..**.*
61560 2a 20 54 68 69 73 20 68 61 73 20 74 68 65 20 65  * This has the e
61570 66 66 65 63 74 20 6f 66 20 6d 61 6b 69 6e 67 20  ffect of making 
61580 73 75 72 65 20 74 68 61 74 20 74 68 65 20 73 74  sure that the st
61590 72 69 6e 67 20 69 73 20 77 65 6c 6c 2d 66 6f 72  ring is well-for
615a0 6d 65 64 0a 2a 2a 20 55 54 46 2d 38 2e 20 20 4d  med.** UTF-8.  M
615b0 69 73 63 6f 64 65 64 20 63 68 61 72 61 63 74 65  iscoded characte
615c0 72 73 20 61 72 65 20 72 65 6d 6f 76 65 64 2e 0a  rs are removed..
615d0 2a 2a 0a 2a 2a 20 54 68 65 20 74 72 61 6e 73 6c  **.** The transl
615e0 61 74 69 6f 6e 20 69 73 20 64 6f 6e 65 20 69 6e  ation is done in
615f0 2d 70 6c 61 63 65 20 28 73 69 6e 63 65 20 69 74  -place (since it
61600 20 69 73 20 69 6d 70 6f 73 73 69 62 6c 65 20 66   is impossible f
61610 6f 72 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 63  or the.** correc
61620 74 20 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67  t UTF-8 encoding
61630 20 74 6f 20 62 65 20 6c 6f 6e 67 65 72 20 74 68   to be longer th
61640 61 6e 20 61 20 6d 61 6c 66 6f 72 6d 65 64 20 65  an a malformed e
61650 6e 63 6f 64 69 6e 67 29 2e 0a 2a 2f 0a 53 51 4c  ncoding)..*/.SQL
61660 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
61670 73 71 6c 69 74 65 33 55 74 66 38 54 6f 38 28 75  sqlite3Utf8To8(u
61680 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 49  nsigned char *zI
61690 6e 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  n){.  unsigned c
616a0 68 61 72 20 2a 7a 4f 75 74 20 3d 20 7a 49 6e 3b  har *zOut = zIn;
616b0 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
616c0 20 2a 7a 53 74 61 72 74 20 3d 20 7a 49 6e 3b 0a   *zStart = zIn;.
616d0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
616e0 2a 7a 54 65 72 6d 3b 0a 20 20 75 33 32 20 63 3b  *zTerm;.  u32 c;
616f0 0a 0a 20 20 77 68 69 6c 65 28 20 7a 49 6e 5b 30  ..  while( zIn[0
61700 5d 20 29 7b 0a 20 20 20 20 63 20 3d 20 73 71 6c  ] ){.    c = sql
61710 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 49 6e  ite3Utf8Read(zIn
61720 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74 20  , zTerm, (const 
61730 75 38 2a 2a 29 26 7a 49 6e 29 3b 0a 20 20 20 20  u8**)&zIn);.    
61740 69 66 28 20 63 21 3d 30 78 66 66 66 64 20 29 7b  if( c!=0xfffd ){
61750 0a 20 20 20 20 20 20 57 52 49 54 45 5f 55 54 46  .      WRITE_UTF
61760 38 28 7a 4f 75 74 2c 20 63 29 3b 0a 20 20 20 20  8(zOut, c);.    
61770 7d 0a 20 20 7d 0a 20 20 2a 7a 4f 75 74 20 3d 20  }.  }.  *zOut = 
61780 30 3b 0a 20 20 72 65 74 75 72 6e 20 7a 4f 75 74  0;.  return zOut
61790 20 2d 20 7a 53 74 61 72 74 3b 0a 7d 0a 23 65 6e   - zStart;.}.#en
617a0 64 69 66 0a 0a 23 69 66 20 64 65 66 69 6e 65 64  dif..#if defined
617b0 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 2f 2a  (SQLITE_TEST)./*
617c0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
617d0 20 69 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20   is called from 
617e0 74 68 65 20 54 43 4c 20 74 65 73 74 20 66 75 6e  the TCL test fun
617f0 63 74 69 6f 6e 20 22 74 72 61 6e 73 6c 61 74 65  ction "translate
61800 5f 73 65 6c 66 74 65 73 74 22 2e 0a 2a 2a 20 49  _selftest"..** I
61810 74 20 63 68 65 63 6b 73 20 74 68 61 74 20 74 68  t checks that th
61820 65 20 70 72 69 6d 69 74 69 76 65 73 20 66 6f 72  e primitives for
61830 20 73 65 72 69 61 6c 69 7a 69 6e 67 20 61 6e 64   serializing and
61840 20 64 65 73 65 72 69 61 6c 69 7a 69 6e 67 0a 2a   deserializing.*
61850 2a 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20  * characters in 
61860 65 61 63 68 20 65 6e 63 6f 64 69 6e 67 20 61 72  each encoding ar
61870 65 20 69 6e 76 65 72 73 65 73 20 6f 66 20 65 61  e inverses of ea
61880 63 68 20 6f 74 68 65 72 2e 0a 2a 2f 0a 53 51 4c  ch other..*/.SQL
61890 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
618a0 20 73 71 6c 69 74 65 33 55 74 66 53 65 6c 66 54   sqlite3UtfSelfT
618b0 65 73 74 28 29 7b 0a 20 20 75 6e 73 69 67 6e 65  est(){.  unsigne
618c0 64 20 69 6e 74 20 69 2c 20 74 3b 0a 20 20 75 6e  d int i, t;.  un
618d0 73 69 67 6e 65 64 20 63 68 61 72 20 7a 42 75 66  signed char zBuf
618e0 5b 32 30 5d 3b 0a 20 20 75 6e 73 69 67 6e 65 64  [20];.  unsigned
618f0 20 63 68 61 72 20 2a 7a 3b 0a 20 20 75 6e 73 69   char *z;.  unsi
61900 67 6e 65 64 20 63 68 61 72 20 2a 7a 54 65 72 6d  gned char *zTerm
61910 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 75 6e 73  ;.  int n;.  uns
61920 69 67 6e 65 64 20 69 6e 74 20 63 3b 0a 0a 20 20  igned int c;..  
61930 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 31  for(i=0; i<0x001
61940 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 20  10000; i++){.   
61950 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57   z = zBuf;.    W
61960 52 49 54 45 5f 55 54 46 38 28 7a 2c 20 69 29 3b  RITE_UTF8(z, i);
61970 0a 20 20 20 20 6e 20 3d 20 7a 2d 7a 42 75 66 3b  .    n = z-zBuf;
61980 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a 20  .    z[0] = 0;. 
61990 20 20 20 7a 54 65 72 6d 20 3d 20 7a 3b 0a 20 20     zTerm = z;.  
619a0 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20    z = zBuf;.    
619b0 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52  c = sqlite3Utf8R
619c0 65 61 64 28 7a 2c 20 7a 54 65 72 6d 2c 20 28 63  ead(z, zTerm, (c
619d0 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 29 3b 0a 20  onst u8**)&z);. 
619e0 20 20 20 74 20 3d 20 69 3b 0a 20 20 20 20 69 66     t = i;.    if
619f0 28 20 69 3e 3d 30 78 44 38 30 30 20 26 26 20 69  ( i>=0xD800 && i
61a00 3c 3d 30 78 44 46 46 46 20 29 20 74 20 3d 20 30  <=0xDFFF ) t = 0
61a10 78 46 46 46 44 3b 0a 20 20 20 20 69 66 28 20 28  xFFFD;.    if( (
61a20 69 26 30 78 46 46 46 46 46 46 46 45 29 3d 3d 30  i&0xFFFFFFFE)==0
61a30 78 46 46 46 45 20 29 20 74 20 3d 20 30 78 46 46  xFFFE ) t = 0xFF
61a40 46 44 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  FD;.    assert( 
61a50 63 3d 3d 74 20 29 3b 0a 20 20 20 20 61 73 73 65  c==t );.    asse
61a60 72 74 28 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20  rt( (z-zBuf)==n 
61a70 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30  );.  }.  for(i=0
61a80 3b 20 69 3c 30 78 30 30 31 31 30 30 30 30 3b 20  ; i<0x00110000; 
61a90 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 69 3e  i++){.    if( i>
61aa0 3d 30 78 44 38 30 30 20 26 26 20 69 3c 30 78 45  =0xD800 && i<0xE
61ab0 30 30 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  000 ) continue;.
61ac0 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20      z = zBuf;.  
61ad0 20 20 57 52 49 54 45 5f 55 54 46 31 36 4c 45 28    WRITE_UTF16LE(
61ae0 7a 2c 20 69 29 3b 0a 20 20 20 20 6e 20 3d 20 7a  z, i);.    n = z
61af0 2d 7a 42 75 66 3b 0a 20 20 20 20 7a 5b 30 5d 20  -zBuf;.    z[0] 
61b00 3d 20 30 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75  = 0;.    z = zBu
61b10 66 3b 0a 20 20 20 20 52 45 41 44 5f 55 54 46 31  f;.    READ_UTF1
61b20 36 4c 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 61  6LE(z, c);.    a
61b30 73 73 65 72 74 28 20 63 3d 3d 69 20 29 3b 0a 20  ssert( c==i );. 
61b40 20 20 20 61 73 73 65 72 74 28 20 28 7a 2d 7a 42     assert( (z-zB
61b50 75 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a 20 20  uf)==n );.  }.  
61b60 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 31  for(i=0; i<0x001
61b70 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 20  10000; i++){.   
61b80 20 69 66 28 20 69 3e 3d 30 78 44 38 30 30 20 26   if( i>=0xD800 &
61b90 26 20 69 3c 30 78 45 30 30 30 20 29 20 63 6f 6e  & i<0xE000 ) con
61ba0 74 69 6e 75 65 3b 0a 20 20 20 20 7a 20 3d 20 7a  tinue;.    z = z
61bb0 42 75 66 3b 0a 20 20 20 20 57 52 49 54 45 5f 55  Buf;.    WRITE_U
61bc0 54 46 31 36 42 45 28 7a 2c 20 69 29 3b 0a 20 20  TF16BE(z, i);.  
61bd0 20 20 6e 20 3d 20 7a 2d 7a 42 75 66 3b 0a 20 20    n = z-zBuf;.  
61be0 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20    z[0] = 0;.    
61bf0 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 52 45  z = zBuf;.    RE
61c00 41 44 5f 55 54 46 31 36 42 45 28 7a 2c 20 63 29  AD_UTF16BE(z, c)
61c10 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 3d  ;.    assert( c=
61c20 3d 69 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =i );.    assert
61c30 28 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b  ( (z-zBuf)==n );
61c40 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  .  }.}.#endif /*
61c50 20 53 51 4c 49 54 45 5f 54 45 53 54 20 2a 2f 0a   SQLITE_TEST */.
61c60 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
61c70 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a  _OMIT_UTF16 */..
61c80 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
61c90 45 6e 64 20 6f 66 20 75 74 66 2e 63 20 2a 2a 2a  End of utf.c ***
61ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
61cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
61cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
61cd0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
61ce0 42 65 67 69 6e 20 66 69 6c 65 20 75 74 69 6c 2e  Begin file util.
61cf0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
61d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
61d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
61d20 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
61d30 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68  mber 15.**.** Th
61d40 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
61d50 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
61d60 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
61d70 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
61d80 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
61d90 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
61da0 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
61db0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
61dc0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
61dd0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
61de0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
61df0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
61e00 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
61e10 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
61e20 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
61e30 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
61e40 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
61e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
61e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
61e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
61e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
61e90 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 55 74 69 6c 69 74  ******.** Utilit
61ea0 79 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64  y functions used
61eb0 20 74 68 72 6f 75 67 68 6f 75 74 20 73 71 6c 69   throughout sqli
61ec0 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  te..**.** This f
61ed0 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 66 75 6e  ile contains fun
61ee0 63 74 69 6f 6e 73 20 66 6f 72 20 61 6c 6c 6f 63  ctions for alloc
61ef0 61 74 69 6e 67 20 6d 65 6d 6f 72 79 2c 20 63 6f  ating memory, co
61f00 6d 70 61 72 69 6e 67 0a 2a 2a 20 73 74 72 69 6e  mparing.** strin
61f10 67 73 2c 20 61 6e 64 20 73 74 75 66 66 20 6c 69  gs, and stuff li
61f20 6b 65 20 74 68 61 74 2e 0a 2a 2a 0a 2a 2a 20 24  ke that..**.** $
61f30 49 64 3a 20 75 74 69 6c 2e 63 2c 76 20 31 2e 32  Id: util.c,v 1.2
61f40 30 37 20 32 30 30 37 2f 30 36 2f 32 36 20 30 30  07 2007/06/26 00
61f50 3a 33 37 3a 32 38 20 64 72 68 20 45 78 70 20 24  :37:28 drh Exp $
61f60 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20  .*/.../*.** Set 
61f70 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
61f80 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 65  error code and e
61f90 72 72 6f 72 20 73 74 72 69 6e 67 20 66 6f 72 20  rror string for 
61fa0 74 68 65 20 73 71 6c 69 74 65 0a 2a 2a 20 68 61  the sqlite.** ha
61fb0 6e 64 6c 65 20 22 64 62 22 2e 20 54 68 65 20 65  ndle "db". The e
61fc0 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 65 74  rror code is set
61fd0 20 74 6f 20 22 65 72 72 5f 63 6f 64 65 22 2e 0a   to "err_code"..
61fe0 2a 2a 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 6e  **.** If it is n
61ff0 6f 74 20 4e 55 4c 4c 2c 20 73 74 72 69 6e 67 20  ot NULL, string 
62000 7a 46 6f 72 6d 61 74 20 73 70 65 63 69 66 69 65  zFormat specifie
62010 73 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 20  s the format of 
62020 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 73 74 72  the.** error str
62030 69 6e 67 20 69 6e 20 74 68 65 20 73 74 79 6c 65  ing in the style
62040 20 6f 66 20 74 68 65 20 70 72 69 6e 74 66 20 66   of the printf f
62050 75 6e 63 74 69 6f 6e 73 3a 20 54 68 65 20 66 6f  unctions: The fo
62060 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 66 6f 72 6d 61  llowing.** forma
62070 74 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65  t characters are
62080 20 61 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20   allowed:.**.** 
62090 20 20 20 20 20 25 73 20 20 20 20 20 20 49 6e 73       %s      Ins
620a0 65 72 74 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20  ert a string.** 
620b0 20 20 20 20 20 25 7a 20 20 20 20 20 20 41 20 73       %z      A s
620c0 74 72 69 6e 67 20 74 68 61 74 20 73 68 6f 75 6c  tring that shoul
620d0 64 20 62 65 20 66 72 65 65 64 20 61 66 74 65 72  d be freed after
620e0 20 75 73 65 0a 2a 2a 20 20 20 20 20 20 25 64 20   use.**      %d 
620f0 20 20 20 20 20 49 6e 73 65 72 74 20 61 6e 20 69       Insert an i
62100 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 25  nteger.**      %
62110 54 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 20  T      Insert a 
62120 74 6f 6b 65 6e 0a 2a 2a 20 20 20 20 20 20 25 53  token.**      %S
62130 20 20 20 20 20 20 49 6e 73 65 72 74 20 74 68 65        Insert the
62140 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f   first element o
62150 66 20 61 20 53 72 63 4c 69 73 74 0a 2a 2a 0a 2a  f a SrcList.**.*
62160 2a 20 7a 46 6f 72 6d 61 74 20 61 6e 64 20 61 6e  * zFormat and an
62170 79 20 73 74 72 69 6e 67 20 74 6f 6b 65 6e 73 20  y string tokens 
62180 74 68 61 74 20 66 6f 6c 6c 6f 77 20 69 74 20 61  that follow it a
62190 72 65 20 61 73 73 75 6d 65 64 20 74 6f 20 62 65  re assumed to be
621a0 0a 2a 2a 20 65 6e 63 6f 64 65 64 20 69 6e 20 55  .** encoded in U
621b0 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 63  TF-8..**.** To c
621c0 6c 65 61 72 20 74 68 65 20 6d 6f 73 74 20 72 65  lear the most re
621d0 63 65 6e 74 20 65 72 72 6f 72 20 66 6f 72 20 73  cent error for s
621e0 71 6c 69 74 65 20 68 61 6e 64 6c 65 20 22 64 62  qlite handle "db
621f0 22 2c 20 73 71 6c 69 74 65 33 45 72 72 6f 72 0a  ", sqlite3Error.
62200 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 63 61 6c  ** should be cal
62210 6c 65 64 20 77 69 74 68 20 65 72 72 5f 63 6f 64  led with err_cod
62220 65 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f  e set to SQLITE_
62230 4f 4b 20 61 6e 64 20 7a 46 6f 72 6d 61 74 20 73  OK and zFormat s
62240 65 74 0a 2a 2a 20 74 6f 20 4e 55 4c 4c 2e 0a 2a  et.** to NULL..*
62250 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
62260 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72   void sqlite3Err
62270 6f 72 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  or(sqlite3 *db, 
62280 69 6e 74 20 65 72 72 5f 63 6f 64 65 2c 20 63 6f  int err_code, co
62290 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61  nst char *zForma
622a0 74 2c 20 2e 2e 2e 29 7b 0a 20 20 69 66 28 20 64  t, ...){.  if( d
622b0 62 20 26 26 20 28 64 62 2d 3e 70 45 72 72 20 7c  b && (db->pErr |
622c0 7c 20 28 64 62 2d 3e 70 45 72 72 20 3d 20 73 71  | (db->pErr = sq
622d0 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 29 29  lite3ValueNew())
622e0 21 3d 30 29 20 29 7b 0a 20 20 20 20 64 62 2d 3e  !=0) ){.    db->
622f0 65 72 72 43 6f 64 65 20 3d 20 65 72 72 5f 63 6f  errCode = err_co
62300 64 65 3b 0a 20 20 20 20 69 66 28 20 7a 46 6f 72  de;.    if( zFor
62310 6d 61 74 20 29 7b 0a 20 20 20 20 20 20 63 68 61  mat ){.      cha
62320 72 20 2a 7a 3b 0a 20 20 20 20 20 20 76 61 5f 6c  r *z;.      va_l
62330 69 73 74 20 61 70 3b 0a 20 20 20 20 20 20 76 61  ist ap;.      va
62340 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d  _start(ap, zForm
62350 61 74 29 3b 0a 20 20 20 20 20 20 7a 20 3d 20 73  at);.      z = s
62360 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 7a  qlite3VMPrintf(z
62370 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 20  Format, ap);.   
62380 20 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20     va_end(ap);. 
62390 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75       sqlite3Valu
623a0 65 53 65 74 53 74 72 28 64 62 2d 3e 70 45 72 72  eSetStr(db->pErr
623b0 2c 20 2d 31 2c 20 7a 2c 20 53 51 4c 49 54 45 5f  , -1, z, SQLITE_
623c0 55 54 46 38 2c 20 73 71 6c 69 74 65 33 46 72 65  UTF8, sqlite3Fre
623d0 65 58 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  eX);.    }else{.
623e0 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c        sqlite3Val
623f0 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70 45 72  ueSetStr(db->pEr
62400 72 2c 20 30 2c 20 30 2c 20 53 51 4c 49 54 45 5f  r, 0, 0, SQLITE_
62410 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41  UTF8, SQLITE_STA
62420 54 49 43 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  TIC);.    }.  }.
62430 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 6e 20  }../*.** Add an 
62440 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f  error message to
62450 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67   pParse->zErrMsg
62460 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e 74 20 70   and increment p
62470 50 61 72 73 65 2d 3e 6e 45 72 72 2e 0a 2a 2a 20  Parse->nErr..** 
62480 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f  The following fo
62490 72 6d 61 74 74 69 6e 67 20 63 68 61 72 61 63 74  rmatting charact
624a0 65 72 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 3a  ers are allowed:
624b0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 25 73 20 20  .**.**      %s  
624c0 20 20 20 20 49 6e 73 65 72 74 20 61 20 73 74 72      Insert a str
624d0 69 6e 67 0a 2a 2a 20 20 20 20 20 20 25 7a 20 20  ing.**      %z  
624e0 20 20 20 20 41 20 73 74 72 69 6e 67 20 74 68 61      A string tha
624f0 74 20 73 68 6f 75 6c 64 20 62 65 20 66 72 65 65  t should be free
62500 64 20 61 66 74 65 72 20 75 73 65 0a 2a 2a 20 20  d after use.**  
62510 20 20 20 20 25 64 20 20 20 20 20 20 49 6e 73 65      %d      Inse
62520 72 74 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a  rt an integer.**
62530 20 20 20 20 20 20 25 54 20 20 20 20 20 20 49 6e        %T      In
62540 73 65 72 74 20 61 20 74 6f 6b 65 6e 0a 2a 2a 20  sert a token.** 
62550 20 20 20 20 20 25 53 20 20 20 20 20 20 49 6e 73       %S      Ins
62560 65 72 74 20 74 68 65 20 66 69 72 73 74 20 65 6c  ert the first el
62570 65 6d 65 6e 74 20 6f 66 20 61 20 53 72 63 4c 69  ement of a SrcLi
62580 73 74 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  st.**.** This fu
62590 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 62 65  nction should be
625a0 20 75 73 65 64 20 74 6f 20 72 65 70 6f 72 74 20   used to report 
625b0 61 6e 79 20 65 72 72 6f 72 20 74 68 61 74 20 6f  any error that o
625c0 63 63 75 72 73 20 77 68 69 6c 73 74 0a 2a 2a 20  ccurs whilst.** 
625d0 63 6f 6d 70 69 6c 69 6e 67 20 61 6e 20 53 51 4c  compiling an SQL
625e0 20 73 74 61 74 65 6d 65 6e 74 20 28 69 2e 65 2e   statement (i.e.
625f0 20 77 69 74 68 69 6e 20 73 71 6c 69 74 65 33 5f   within sqlite3_
62600 70 72 65 70 61 72 65 28 29 29 2e 20 54 68 65 0a  prepare()). The.
62610 2a 2a 20 6c 61 73 74 20 74 68 69 6e 67 20 74 68  ** last thing th
62620 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  e sqlite3_prepar
62630 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65  e() function doe
62640 73 20 69 73 20 63 6f 70 79 20 74 68 65 20 65 72  s is copy the er
62650 72 6f 72 0a 2a 2a 20 73 74 6f 72 65 64 20 62 79  ror.** stored by
62660 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
62670 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65  nto the database
62680 20 68 61 6e 64 6c 65 20 75 73 69 6e 67 20 73 71   handle using sq
62690 6c 69 74 65 33 45 72 72 6f 72 28 29 2e 0a 2a 2a  lite3Error()..**
626a0 20 46 75 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65   Function sqlite
626b0 33 45 72 72 6f 72 28 29 20 73 68 6f 75 6c 64 20  3Error() should 
626c0 62 65 20 75 73 65 64 20 64 75 72 69 6e 67 20 73  be used during s
626d0 74 61 74 65 6d 65 6e 74 20 65 78 65 63 75 74 69  tatement executi
626e0 6f 6e 0a 2a 2a 20 28 73 71 6c 69 74 65 33 5f 73  on.** (sqlite3_s
626f0 74 65 70 28 29 20 65 74 63 2e 29 2e 0a 2a 2f 0a  tep() etc.)..*/.
62700 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
62710 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72  oid sqlite3Error
62720 4d 73 67 28 50 61 72 73 65 20 2a 70 50 61 72 73  Msg(Parse *pPars
62730 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  e, const char *z
62740 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20  Format, ...){.  
62750 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 70 50  va_list ap;.  pP
62760 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20  arse->nErr++;.  
62770 73 71 6c 69 74 65 46 72 65 65 28 70 50 61 72 73  sqliteFree(pPars
62780 65 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 76  e->zErrMsg);.  v
62790 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72  a_start(ap, zFor
627a0 6d 61 74 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e  mat);.  pParse->
627b0 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65  zErrMsg = sqlite
627c0 33 56 4d 50 72 69 6e 74 66 28 7a 46 6f 72 6d 61  3VMPrintf(zForma
627d0 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64  t, ap);.  va_end
627e0 28 61 70 29 3b 0a 20 20 69 66 28 20 70 50 61 72  (ap);.  if( pPar
627f0 73 65 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  se->rc==SQLITE_O
62800 4b 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d  K ){.    pParse-
62810 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52  >rc = SQLITE_ERR
62820 4f 52 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  OR;.  }.}../*.**
62830 20 43 6c 65 61 72 20 74 68 65 20 65 72 72 6f 72   Clear the error
62840 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50 61 72   message in pPar
62850 73 65 2c 20 69 66 20 61 6e 79 0a 2a 2f 0a 53 51  se, if any.*/.SQ
62860 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
62870 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 43 6c  d sqlite3ErrorCl
62880 65 61 72 28 50 61 72 73 65 20 2a 70 50 61 72 73  ear(Parse *pPars
62890 65 29 7b 0a 20 20 73 71 6c 69 74 65 46 72 65 65  e){.  sqliteFree
628a0 28 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67  (pParse->zErrMsg
628b0 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 7a 45 72  );.  pParse->zEr
628c0 72 4d 73 67 20 3d 20 30 3b 0a 20 20 70 50 61 72  rMsg = 0;.  pPar
628d0 73 65 2d 3e 6e 45 72 72 20 3d 20 30 3b 0a 7d 0a  se->nErr = 0;.}.
628e0 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61  ./*.** Convert a
628f0 6e 20 53 51 4c 2d 73 74 79 6c 65 20 71 75 6f 74  n SQL-style quot
62900 65 64 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61  ed string into a
62910 20 6e 6f 72 6d 61 6c 20 73 74 72 69 6e 67 20 62   normal string b
62920 79 20 72 65 6d 6f 76 69 6e 67 0a 2a 2a 20 74 68  y removing.** th
62930 65 20 71 75 6f 74 65 20 63 68 61 72 61 63 74 65  e quote characte
62940 72 73 2e 20 20 54 68 65 20 63 6f 6e 76 65 72 73  rs.  The convers
62950 69 6f 6e 20 69 73 20 64 6f 6e 65 20 69 6e 2d 70  ion is done in-p
62960 6c 61 63 65 2e 20 20 49 66 20 74 68 65 0a 2a 2a  lace.  If the.**
62970 20 69 6e 70 75 74 20 64 6f 65 73 20 6e 6f 74 20   input does not 
62980 62 65 67 69 6e 20 77 69 74 68 20 61 20 71 75 6f  begin with a quo
62990 74 65 20 63 68 61 72 61 63 74 65 72 2c 20 74 68  te character, th
629a0 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a  en this routine.
629b0 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  ** is a no-op..*
629c0 2a 0a 2a 2a 20 32 30 30 32 2d 46 65 62 2d 31 34  *.** 2002-Feb-14
629d0 3a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  : This routine i
629e0 73 20 65 78 74 65 6e 64 65 64 20 74 6f 20 72 65  s extended to re
629f0 6d 6f 76 65 20 4d 53 2d 41 63 63 65 73 73 20 73  move MS-Access s
62a00 74 79 6c 65 0a 2a 2a 20 62 72 61 63 6b 65 74 73  tyle.** brackets
62a10 20 66 72 6f 6d 20 61 72 6f 75 6e 64 20 69 64 65   from around ide
62a20 6e 74 69 66 65 72 73 2e 20 20 46 6f 72 20 65 78  ntifers.  For ex
62a30 61 6d 70 6c 65 3a 20 20 22 5b 61 2d 62 2d 63 5d  ample:  "[a-b-c]
62a40 22 20 62 65 63 6f 6d 65 73 0a 2a 2a 20 22 61 2d  " becomes.** "a-
62a50 62 2d 63 22 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  b-c"..*/.SQLITE_
62a60 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
62a70 69 74 65 33 44 65 71 75 6f 74 65 28 63 68 61 72  ite3Dequote(char
62a80 20 2a 7a 29 7b 0a 20 20 69 6e 74 20 71 75 6f 74   *z){.  int quot
62a90 65 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20  e;.  int i, j;. 
62aa0 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75   if( z==0 ) retu
62ab0 72 6e 3b 0a 20 20 71 75 6f 74 65 20 3d 20 7a 5b  rn;.  quote = z[
62ac0 30 5d 3b 0a 20 20 73 77 69 74 63 68 28 20 71 75  0];.  switch( qu
62ad0 6f 74 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20  ote ){.    case 
62ae0 27 5c 27 27 3a 20 20 62 72 65 61 6b 3b 0a 20 20  '\'':  break;.  
62af0 20 20 63 61 73 65 20 27 22 27 3a 20 20 20 62 72    case '"':   br
62b00 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 27 60  eak;.    case '`
62b10 27 3a 20 20 20 62 72 65 61 6b 3b 20 20 20 20 20  ':   break;     
62b20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f             /* Fo
62b30 72 20 4d 79 53 51 4c 20 63 6f 6d 70 61 74 69 62  r MySQL compatib
62b40 69 6c 69 74 79 20 2a 2f 0a 20 20 20 20 63 61 73  ility */.    cas
62b50 65 20 27 5b 27 3a 20 20 20 71 75 6f 74 65 20 3d  e '[':   quote =
62b60 20 27 5d 27 3b 20 20 62 72 65 61 6b 3b 20 20 2f   ']';  break;  /
62b70 2a 20 46 6f 72 20 4d 53 20 53 71 6c 53 65 72 76  * For MS SqlServ
62b80 65 72 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79  er compatibility
62b90 20 2a 2f 0a 20 20 20 20 64 65 66 61 75 6c 74 3a   */.    default:
62ba0 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
62bb0 20 20 66 6f 72 28 69 3d 31 2c 20 6a 3d 30 3b 20    for(i=1, j=0; 
62bc0 7a 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  z[i]; i++){.    
62bd0 69 66 28 20 7a 5b 69 5d 3d 3d 71 75 6f 74 65 20  if( z[i]==quote 
62be0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 69  ){.      if( z[i
62bf0 2b 31 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a 20 20  +1]==quote ){.  
62c00 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 71        z[j++] = q
62c10 75 6f 74 65 3b 0a 20 20 20 20 20 20 20 20 69 2b  uote;.        i+
62c20 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  +;.      }else{.
62c30 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d          z[j++] =
62c40 20 30 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61   0;.        brea
62c50 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
62c60 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 5b 6a 2b  else{.      z[j+
62c70 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20 20 20 20 7d  +] = z[i];.    }
62c80 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 41 6e 20 61 72  .  }.}../* An ar
62c90 72 61 79 20 74 6f 20 6d 61 70 20 61 6c 6c 20 75  ray to map all u
62ca0 70 70 65 72 2d 63 61 73 65 20 63 68 61 72 61 63  pper-case charac
62cb0 74 65 72 73 20 69 6e 74 6f 20 74 68 65 69 72 20  ters into their 
62cc0 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a  corresponding.**
62cd0 20 6c 6f 77 65 72 2d 63 61 73 65 20 63 68 61 72   lower-case char
62ce0 61 63 74 65 72 2e 20 0a 2a 2f 0a 63 6f 6e 73 74  acter. .*/.const
62cf0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73   unsigned char s
62d00 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77  qlite3UpperToLow
62d10 65 72 5b 5d 20 3d 20 7b 0a 23 69 66 64 65 66 20  er[] = {.#ifdef 
62d20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 20 20 20  SQLITE_ASCII.   
62d30 20 20 20 30 2c 20 20 31 2c 20 20 32 2c 20 20 33     0,  1,  2,  3
62d40 2c 20 20 34 2c 20 20 35 2c 20 20 36 2c 20 20 37  ,  4,  5,  6,  7
62d50 2c 20 20 38 2c 20 20 39 2c 20 31 30 2c 20 31 31  ,  8,  9, 10, 11
62d60 2c 20 31 32 2c 20 31 33 2c 20 31 34 2c 20 31 35  , 12, 13, 14, 15
62d70 2c 20 31 36 2c 20 31 37 2c 0a 20 20 20 20 20 31  , 16, 17,.     1
62d80 38 2c 20 31 39 2c 20 32 30 2c 20 32 31 2c 20 32  8, 19, 20, 21, 2
62d90 32 2c 20 32 33 2c 20 32 34 2c 20 32 35 2c 20 32  2, 23, 24, 25, 2
62da0 36 2c 20 32 37 2c 20 32 38 2c 20 32 39 2c 20 33  6, 27, 28, 29, 3
62db0 30 2c 20 33 31 2c 20 33 32 2c 20 33 33 2c 20 33  0, 31, 32, 33, 3
62dc0 34 2c 20 33 35 2c 0a 20 20 20 20 20 33 36 2c 20  4, 35,.     36, 
62dd0 33 37 2c 20 33 38 2c 20 33 39 2c 20 34 30 2c 20  37, 38, 39, 40, 
62de0 34 31 2c 20 34 32 2c 20 34 33 2c 20 34 34 2c 20  41, 42, 43, 44, 
62df0 34 35 2c 20 34 36 2c 20 34 37 2c 20 34 38 2c 20  45, 46, 47, 48, 
62e00 34 39 2c 20 35 30 2c 20 35 31 2c 20 35 32 2c 20  49, 50, 51, 52, 
62e10 35 33 2c 0a 20 20 20 20 20 35 34 2c 20 35 35 2c  53,.     54, 55,
62e20 20 35 36 2c 20 35 37 2c 20 35 38 2c 20 35 39 2c   56, 57, 58, 59,
62e30 20 36 30 2c 20 36 31 2c 20 36 32 2c 20 36 33 2c   60, 61, 62, 63,
62e40 20 36 34 2c 20 39 37 2c 20 39 38 2c 20 39 39 2c   64, 97, 98, 99,
62e50 31 30 30 2c 31 30 31 2c 31 30 32 2c 31 30 33 2c  100,101,102,103,
62e60 0a 20 20 20 20 31 30 34 2c 31 30 35 2c 31 30 36  .    104,105,106
62e70 2c 31 30 37 2c 31 30 38 2c 31 30 39 2c 31 31 30  ,107,108,109,110
62e80 2c 31 31 31 2c 31 31 32 2c 31 31 33 2c 31 31 34  ,111,112,113,114
62e90 2c 31 31 35 2c 31 31 36 2c 31 31 37 2c 31 31 38  ,115,116,117,118
62ea0 2c 31 31 39 2c 31 32 30 2c 31 32 31 2c 0a 20 20  ,119,120,121,.  
62eb0 20 20 31 32 32 2c 20 39 31 2c 20 39 32 2c 20 39    122, 91, 92, 9
62ec0 33 2c 20 39 34 2c 20 39 35 2c 20 39 36 2c 20 39  3, 94, 95, 96, 9
62ed0 37 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 30  7, 98, 99,100,10
62ee0 31 2c 31 30 32 2c 31 30 33 2c 31 30 34 2c 31 30  1,102,103,104,10
62ef0 35 2c 31 30 36 2c 31 30 37 2c 0a 20 20 20 20 31  5,106,107,.    1
62f00 30 38 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 31  08,109,110,111,1
62f10 31 32 2c 31 31 33 2c 31 31 34 2c 31 31 35 2c 31  12,113,114,115,1
62f20 31 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31  16,117,118,119,1
62f30 32 30 2c 31 32 31 2c 31 32 32 2c 31 32 33 2c 31  20,121,122,123,1
62f40 32 34 2c 31 32 35 2c 0a 20 20 20 20 31 32 36 2c  24,125,.    126,
62f50 31 32 37 2c 31 32 38 2c 31 32 39 2c 31 33 30 2c  127,128,129,130,
62f60 31 33 31 2c 31 33 32 2c 31 33 33 2c 31 33 34 2c  131,132,133,134,
62f70 31 33 35 2c 31 33 36 2c 31 33 37 2c 31 33 38 2c  135,136,137,138,
62f80 31 33 39 2c 31 34 30 2c 31 34 31 2c 31 34 32 2c  139,140,141,142,
62f90 31 34 33 2c 0a 20 20 20 20 31 34 34 2c 31 34 35  143,.    144,145
62fa0 2c 31 34 36 2c 31 34 37 2c 31 34 38 2c 31 34 39  ,146,147,148,149
62fb0 2c 31 35 30 2c 31 35 31 2c 31 35 32 2c 31 35 33  ,150,151,152,153
62fc0 2c 31 35 34 2c 31 35 35 2c 31 35 36 2c 31 35 37  ,154,155,156,157
62fd0 2c 31 35 38 2c 31 35 39 2c 31 36 30 2c 31 36 31  ,158,159,160,161
62fe0 2c 0a 20 20 20 20 31 36 32 2c 31 36 33 2c 31 36  ,.    162,163,16
62ff0 34 2c 31 36 35 2c 31 36 36 2c 31 36 37 2c 31 36  4,165,166,167,16
63000 38 2c 31 36 39 2c 31 37 30 2c 31 37 31 2c 31 37  8,169,170,171,17
63010 32 2c 31 37 33 2c 31 37 34 2c 31 37 35 2c 31 37  2,173,174,175,17
63020 36 2c 31 37 37 2c 31 37 38 2c 31 37 39 2c 0a 20  6,177,178,179,. 
63030 20 20 20 31 38 30 2c 31 38 31 2c 31 38 32 2c 31     180,181,182,1
63040 38 33 2c 31 38 34 2c 31 38 35 2c 31 38 36 2c 31  83,184,185,186,1
63050 38 37 2c 31 38 38 2c 31 38 39 2c 31 39 30 2c 31  87,188,189,190,1
63060 39 31 2c 31 39 32 2c 31 39 33 2c 31 39 34 2c 31  91,192,193,194,1
63070 39 35 2c 31 39 36 2c 31 39 37 2c 0a 20 20 20 20  95,196,197,.    
63080 31 39 38 2c 31 39 39 2c 32 30 30 2c 32 30 31 2c  198,199,200,201,
63090 32 30 32 2c 32 30 33 2c 32 30 34 2c 32 30 35 2c  202,203,204,205,
630a0 32 30 36 2c 32 30 37 2c 32 30 38 2c 32 30 39 2c  206,207,208,209,
630b0 32 31 30 2c 32 31 31 2c 32 31 32 2c 32 31 33 2c  210,211,212,213,
630c0 32 31 34 2c 32 31 35 2c 0a 20 20 20 20 32 31 36  214,215,.    216
630d0 2c 32 31 37 2c 32 31 38 2c 32 31 39 2c 32 32 30  ,217,218,219,220
630e0 2c 32 32 31 2c 32 32 32 2c 32 32 33 2c 32 32 34  ,221,222,223,224
630f0 2c 32 32 35 2c 32 32 36 2c 32 32 37 2c 32 32 38  ,225,226,227,228
63100 2c 32 32 39 2c 32 33 30 2c 32 33 31 2c 32 33 32  ,229,230,231,232
63110 2c 32 33 33 2c 0a 20 20 20 20 32 33 34 2c 32 33  ,233,.    234,23
63120 35 2c 32 33 36 2c 32 33 37 2c 32 33 38 2c 32 33  5,236,237,238,23
63130 39 2c 32 34 30 2c 32 34 31 2c 32 34 32 2c 32 34  9,240,241,242,24
63140 33 2c 32 34 34 2c 32 34 35 2c 32 34 36 2c 32 34  3,244,245,246,24
63150 37 2c 32 34 38 2c 32 34 39 2c 32 35 30 2c 32 35  7,248,249,250,25
63160 31 2c 0a 20 20 20 20 32 35 32 2c 32 35 33 2c 32  1,.    252,253,2
63170 35 34 2c 32 35 35 0a 23 65 6e 64 69 66 0a 23 69  54,255.#endif.#i
63180 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44  fdef SQLITE_EBCD
63190 49 43 0a 20 20 20 20 20 20 30 2c 20 20 31 2c 20  IC.      0,  1, 
631a0 20 32 2c 20 20 33 2c 20 20 34 2c 20 20 35 2c 20   2,  3,  4,  5, 
631b0 20 36 2c 20 20 37 2c 20 20 38 2c 20 20 39 2c 20   6,  7,  8,  9, 
631c0 31 30 2c 20 31 31 2c 20 31 32 2c 20 31 33 2c 20  10, 11, 12, 13, 
631d0 31 34 2c 20 31 35 2c 20 2f 2a 20 30 78 20 2a 2f  14, 15, /* 0x */
631e0 0a 20 20 20 20 20 31 36 2c 20 31 37 2c 20 31 38  .     16, 17, 18
631f0 2c 20 31 39 2c 20 32 30 2c 20 32 31 2c 20 32 32  , 19, 20, 21, 22
63200 2c 20 32 33 2c 20 32 34 2c 20 32 35 2c 20 32 36  , 23, 24, 25, 26
63210 2c 20 32 37 2c 20 32 38 2c 20 32 39 2c 20 33 30  , 27, 28, 29, 30
63220 2c 20 33 31 2c 20 2f 2a 20 31 78 20 2a 2f 0a 20  , 31, /* 1x */. 
63230 20 20 20 20 33 32 2c 20 33 33 2c 20 33 34 2c 20      32, 33, 34, 
63240 33 35 2c 20 33 36 2c 20 33 37 2c 20 33 38 2c 20  35, 36, 37, 38, 
63250 33 39 2c 20 34 30 2c 20 34 31 2c 20 34 32 2c 20  39, 40, 41, 42, 
63260 34 33 2c 20 34 34 2c 20 34 35 2c 20 34 36 2c 20  43, 44, 45, 46, 
63270 34 37 2c 20 2f 2a 20 32 78 20 2a 2f 0a 20 20 20  47, /* 2x */.   
63280 20 20 34 38 2c 20 34 39 2c 20 35 30 2c 20 35 31    48, 49, 50, 51
63290 2c 20 35 32 2c 20 35 33 2c 20 35 34 2c 20 35 35  , 52, 53, 54, 55
632a0 2c 20 35 36 2c 20 35 37 2c 20 35 38 2c 20 35 39  , 56, 57, 58, 59
632b0 2c 20 36 30 2c 20 36 31 2c 20 36 32 2c 20 36 33  , 60, 61, 62, 63
632c0 2c 20 2f 2a 20 33 78 20 2a 2f 0a 20 20 20 20 20  , /* 3x */.     
632d0 36 34 2c 20 36 35 2c 20 36 36 2c 20 36 37 2c 20  64, 65, 66, 67, 
632e0 36 38 2c 20 36 39 2c 20 37 30 2c 20 37 31 2c 20  68, 69, 70, 71, 
632f0 37 32 2c 20 37 33 2c 20 37 34 2c 20 37 35 2c 20  72, 73, 74, 75, 
63300 37 36 2c 20 37 37 2c 20 37 38 2c 20 37 39 2c 20  76, 77, 78, 79, 
63310 2f 2a 20 34 78 20 2a 2f 0a 20 20 20 20 20 38 30  /* 4x */.     80
63320 2c 20 38 31 2c 20 38 32 2c 20 38 33 2c 20 38 34  , 81, 82, 83, 84
63330 2c 20 38 35 2c 20 38 36 2c 20 38 37 2c 20 38 38  , 85, 86, 87, 88
63340 2c 20 38 39 2c 20 39 30 2c 20 39 31 2c 20 39 32  , 89, 90, 91, 92
63350 2c 20 39 33 2c 20 39 34 2c 20 39 35 2c 20 2f 2a  , 93, 94, 95, /*
63360 20 35 78 20 2a 2f 0a 20 20 20 20 20 39 36 2c 20   5x */.     96, 
63370 39 37 2c 20 36 36 2c 20 36 37 2c 20 36 38 2c 20  97, 66, 67, 68, 
63380 36 39 2c 20 37 30 2c 20 37 31 2c 20 37 32 2c 20  69, 70, 71, 72, 
63390 37 33 2c 31 30 36 2c 31 30 37 2c 31 30 38 2c 31  73,106,107,108,1
633a0 30 39 2c 31 31 30 2c 31 31 31 2c 20 2f 2a 20 36  09,110,111, /* 6
633b0 78 20 2a 2f 0a 20 20 20 20 31 31 32 2c 20 38 31  x */.    112, 81
633c0 2c 20 38 32 2c 20 38 33 2c 20 38 34 2c 20 38 35  , 82, 83, 84, 85
633d0 2c 20 38 36 2c 20 38 37 2c 20 38 38 2c 20 38 39  , 86, 87, 88, 89
633e0 2c 31 32 32 2c 31 32 33 2c 31 32 34 2c 31 32 35  ,122,123,124,125
633f0 2c 31 32 36 2c 31 32 37 2c 20 2f 2a 20 37 78 20  ,126,127, /* 7x 
63400 2a 2f 0a 20 20 20 20 31 32 38 2c 31 32 39 2c 31  */.    128,129,1
63410 33 30 2c 31 33 31 2c 31 33 32 2c 31 33 33 2c 31  30,131,132,133,1
63420 33 34 2c 31 33 35 2c 31 33 36 2c 31 33 37 2c 31  34,135,136,137,1
63430 33 38 2c 31 33 39 2c 31 34 30 2c 31 34 31 2c 31  38,139,140,141,1
63440 34 32 2c 31 34 33 2c 20 2f 2a 20 38 78 20 2a 2f  42,143, /* 8x */
63450 0a 20 20 20 20 31 34 34 2c 31 34 35 2c 31 34 36  .    144,145,146
63460 2c 31 34 37 2c 31 34 38 2c 31 34 39 2c 31 35 30  ,147,148,149,150
63470 2c 31 35 31 2c 31 35 32 2c 31 35 33 2c 31 35 34  ,151,152,153,154
63480 2c 31 35 35 2c 31 35 36 2c 31 35 37 2c 31 35 36  ,155,156,157,156
63490 2c 31 35 39 2c 20 2f 2a 20 39 78 20 2a 2f 0a 20  ,159, /* 9x */. 
634a0 20 20 20 31 36 30 2c 31 36 31 2c 31 36 32 2c 31     160,161,162,1
634b0 36 33 2c 31 36 34 2c 31 36 35 2c 31 36 36 2c 31  63,164,165,166,1
634c0 36 37 2c 31 36 38 2c 31 36 39 2c 31 37 30 2c 31  67,168,169,170,1
634d0 37 31 2c 31 34 30 2c 31 34 31 2c 31 34 32 2c 31  71,140,141,142,1
634e0 37 35 2c 20 2f 2a 20 41 78 20 2a 2f 0a 20 20 20  75, /* Ax */.   
634f0 20 31 37 36 2c 31 37 37 2c 31 37 38 2c 31 37 39   176,177,178,179
63500 2c 31 38 30 2c 31 38 31 2c 31 38 32 2c 31 38 33  ,180,181,182,183
63510 2c 31 38 34 2c 31 38 35 2c 31 38 36 2c 31 38 37  ,184,185,186,187
63520 2c 31 38 38 2c 31 38 39 2c 31 39 30 2c 31 39 31  ,188,189,190,191
63530 2c 20 2f 2a 20 42 78 20 2a 2f 0a 20 20 20 20 31  , /* Bx */.    1
63540 39 32 2c 31 32 39 2c 31 33 30 2c 31 33 31 2c 31  92,129,130,131,1
63550 33 32 2c 31 33 33 2c 31 33 34 2c 31 33 35 2c 31  32,133,134,135,1
63560 33 36 2c 31 33 37 2c 32 30 32 2c 32 30 33 2c 32  36,137,202,203,2
63570 30 34 2c 32 30 35 2c 32 30 36 2c 32 30 37 2c 20  04,205,206,207, 
63580 2f 2a 20 43 78 20 2a 2f 0a 20 20 20 20 32 30 38  /* Cx */.    208
63590 2c 31 34 35 2c 31 34 36 2c 31 34 37 2c 31 34 38  ,145,146,147,148
635a0 2c 31 34 39 2c 31 35 30 2c 31 35 31 2c 31 35 32  ,149,150,151,152
635b0 2c 31 35 33 2c 32 31 38 2c 32 31 39 2c 32 32 30  ,153,218,219,220
635c0 2c 32 32 31 2c 32 32 32 2c 32 32 33 2c 20 2f 2a  ,221,222,223, /*
635d0 20 44 78 20 2a 2f 0a 20 20 20 20 32 32 34 2c 32   Dx */.    224,2
635e0 32 35 2c 31 36 32 2c 31 36 33 2c 31 36 34 2c 31  25,162,163,164,1
635f0 36 35 2c 31 36 36 2c 31 36 37 2c 31 36 38 2c 31  65,166,167,168,1
63600 36 39 2c 32 33 32 2c 32 30 33 2c 32 30 34 2c 32  69,232,203,204,2
63610 30 35 2c 32 30 36 2c 32 30 37 2c 20 2f 2a 20 45  05,206,207, /* E
63620 78 20 2a 2f 0a 20 20 20 20 32 33 39 2c 32 34 30  x */.    239,240
63630 2c 32 34 31 2c 32 34 32 2c 32 34 33 2c 32 34 34  ,241,242,243,244
63640 2c 32 34 35 2c 32 34 36 2c 32 34 37 2c 32 34 38  ,245,246,247,248
63650 2c 32 34 39 2c 32 31 39 2c 32 32 30 2c 32 32 31  ,249,219,220,221
63660 2c 32 32 32 2c 32 35 35 2c 20 2f 2a 20 46 78 20  ,222,255, /* Fx 
63670 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 23 64 65  */.#endif.};.#de
63680 66 69 6e 65 20 55 70 70 65 72 54 6f 4c 6f 77 65  fine UpperToLowe
63690 72 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f  r sqlite3UpperTo
636a0 4c 6f 77 65 72 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d  Lower../*.** Som
636b0 65 20 73 79 73 74 65 6d 73 20 68 61 76 65 20 73  e systems have s
636c0 74 72 69 63 6d 70 28 29 2e 20 20 4f 74 68 65 72  tricmp().  Other
636d0 73 20 68 61 76 65 20 73 74 72 63 61 73 65 63 6d  s have strcasecm
636e0 70 28 29 2e 20 20 42 65 63 61 75 73 65 0a 2a 2a  p().  Because.**
636f0 20 74 68 65 72 65 20 69 73 20 6e 6f 20 63 6f 6e   there is no con
63700 73 69 73 74 65 6e 63 79 2c 20 77 65 20 77 69 6c  sistency, we wil
63710 6c 20 64 65 66 69 6e 65 20 6f 75 72 20 6f 77 6e  l define our own
63720 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
63730 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53  ATE int sqlite3S
63740 74 72 49 43 6d 70 28 63 6f 6e 73 74 20 63 68 61  trICmp(const cha
63750 72 20 2a 7a 4c 65 66 74 2c 20 63 6f 6e 73 74 20  r *zLeft, const 
63760 63 68 61 72 20 2a 7a 52 69 67 68 74 29 7b 0a 20  char *zRight){. 
63770 20 72 65 67 69 73 74 65 72 20 75 6e 73 69 67 6e   register unsign
63780 65 64 20 63 68 61 72 20 2a 61 2c 20 2a 62 3b 0a  ed char *a, *b;.
63790 20 20 61 20 3d 20 28 75 6e 73 69 67 6e 65 64 20    a = (unsigned 
637a0 63 68 61 72 20 2a 29 7a 4c 65 66 74 3b 0a 20 20  char *)zLeft;.  
637b0 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68  b = (unsigned ch
637c0 61 72 20 2a 29 7a 52 69 67 68 74 3b 0a 20 20 77  ar *)zRight;.  w
637d0 68 69 6c 65 28 20 2a 61 21 3d 30 20 26 26 20 55  hile( *a!=0 && U
637e0 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d 3d  pperToLower[*a]=
637f0 3d 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62  =UpperToLower[*b
63800 5d 29 7b 20 61 2b 2b 3b 20 62 2b 2b 3b 20 7d 0a  ]){ a++; b++; }.
63810 20 20 72 65 74 75 72 6e 20 55 70 70 65 72 54 6f    return UpperTo
63820 4c 6f 77 65 72 5b 2a 61 5d 20 2d 20 55 70 70 65  Lower[*a] - Uppe
63830 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 3b 0a 7d 0a  rToLower[*b];.}.
63840 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
63850 6e 74 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43  nt sqlite3StrNIC
63860 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  mp(const char *z
63870 4c 65 66 74 2c 20 63 6f 6e 73 74 20 63 68 61 72  Left, const char
63880 20 2a 7a 52 69 67 68 74 2c 20 69 6e 74 20 4e 29   *zRight, int N)
63890 7b 0a 20 20 72 65 67 69 73 74 65 72 20 75 6e 73  {.  register uns
638a0 69 67 6e 65 64 20 63 68 61 72 20 2a 61 2c 20 2a  igned char *a, *
638b0 62 3b 0a 20 20 61 20 3d 20 28 75 6e 73 69 67 6e  b;.  a = (unsign
638c0 65 64 20 63 68 61 72 20 2a 29 7a 4c 65 66 74 3b  ed char *)zLeft;
638d0 0a 20 20 62 20 3d 20 28 75 6e 73 69 67 6e 65 64  .  b = (unsigned
638e0 20 63 68 61 72 20 2a 29 7a 52 69 67 68 74 3b 0a   char *)zRight;.
638f0 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 3e 20 30    while( N-- > 0
63900 20 26 26 20 2a 61 21 3d 30 20 26 26 20 55 70 70   && *a!=0 && Upp
63910 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d 3d 3d 55  erToLower[*a]==U
63920 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 29  pperToLower[*b])
63930 7b 20 61 2b 2b 3b 20 62 2b 2b 3b 20 7d 0a 20 20  { a++; b++; }.  
63940 72 65 74 75 72 6e 20 4e 3c 30 20 3f 20 30 20 3a  return N<0 ? 0 :
63950 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61   UpperToLower[*a
63960 5d 20 2d 20 55 70 70 65 72 54 6f 4c 6f 77 65 72  ] - UpperToLower
63970 5b 2a 62 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  [*b];.}../*.** R
63980 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 7a 20  eturn TRUE if z 
63990 69 73 20 61 20 70 75 72 65 20 6e 75 6d 65 72 69  is a pure numeri
639a0 63 20 73 74 72 69 6e 67 2e 20 20 52 65 74 75 72  c string.  Retur
639b0 6e 20 46 41 4c 53 45 20 69 66 20 74 68 65 0a 2a  n FALSE if the.*
639c0 2a 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e  * string contain
639d0 73 20 61 6e 79 20 63 68 61 72 61 63 74 65 72 20  s any character 
639e0 77 68 69 63 68 20 69 73 20 6e 6f 74 20 70 61 72  which is not par
639f0 74 20 6f 66 20 61 20 6e 75 6d 62 65 72 2e 20 49  t of a number. I
63a00 66 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20  f.** the string 
63a10 69 73 20 6e 75 6d 65 72 69 63 20 61 6e 64 20 63  is numeric and c
63a20 6f 6e 74 61 69 6e 73 20 74 68 65 20 27 2e 27 20  ontains the '.' 
63a30 63 68 61 72 61 63 74 65 72 2c 20 73 65 74 20 2a  character, set *
63a40 72 65 61 6c 6e 75 6d 0a 2a 2a 20 74 6f 20 54 52  realnum.** to TR
63a50 55 45 20 28 6f 74 68 65 72 77 69 73 65 20 46 41  UE (otherwise FA
63a60 4c 53 45 29 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65  LSE)..**.** An e
63a70 6d 70 74 79 20 73 74 72 69 6e 67 20 69 73 20 63  mpty string is c
63a80 6f 6e 73 69 64 65 72 65 64 20 6e 6f 6e 2d 6e 75  onsidered non-nu
63a90 6d 65 72 69 63 2e 0a 2a 2f 0a 53 51 4c 49 54 45  meric..*/.SQLITE
63aa0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
63ab0 69 74 65 33 49 73 4e 75 6d 62 65 72 28 63 6f 6e  ite3IsNumber(con
63ac0 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20  st char *z, int 
63ad0 2a 72 65 61 6c 6e 75 6d 2c 20 75 38 20 65 6e 63  *realnum, u8 enc
63ae0 29 7b 0a 20 20 69 6e 74 20 69 6e 63 72 20 3d 20  ){.  int incr = 
63af0 28 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46  (enc==SQLITE_UTF
63b00 38 3f 31 3a 32 29 3b 0a 20 20 69 66 28 20 65 6e  8?1:2);.  if( en
63b10 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42  c==SQLITE_UTF16B
63b20 45 20 29 20 7a 2b 2b 3b 0a 20 20 69 66 28 20 2a  E ) z++;.  if( *
63b30 7a 3d 3d 27 2d 27 20 7c 7c 20 2a 7a 3d 3d 27 2b  z=='-' || *z=='+
63b40 27 20 29 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20  ' ) z += incr;. 
63b50 20 69 66 28 20 21 69 73 64 69 67 69 74 28 2a 28   if( !isdigit(*(
63b60 75 38 2a 29 7a 29 20 29 7b 0a 20 20 20 20 72 65  u8*)z) ){.    re
63b70 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 20  turn 0;.  }.  z 
63b80 2b 3d 20 69 6e 63 72 3b 0a 20 20 69 66 28 20 72  += incr;.  if( r
63b90 65 61 6c 6e 75 6d 20 29 20 2a 72 65 61 6c 6e 75  ealnum ) *realnu
63ba0 6d 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20  m = 0;.  while( 
63bb0 69 73 64 69 67 69 74 28 2a 28 75 38 2a 29 7a 29  isdigit(*(u8*)z)
63bc0 20 29 7b 20 7a 20 2b 3d 20 69 6e 63 72 3b 20 7d   ){ z += incr; }
63bd0 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2e 27 20 29  .  if( *z=='.' )
63be0 7b 0a 20 20 20 20 7a 20 2b 3d 20 69 6e 63 72 3b  {.    z += incr;
63bf0 0a 20 20 20 20 69 66 28 20 21 69 73 64 69 67 69  .    if( !isdigi
63c00 74 28 2a 28 75 38 2a 29 7a 29 20 29 20 72 65 74  t(*(u8*)z) ) ret
63c10 75 72 6e 20 30 3b 0a 20 20 20 20 77 68 69 6c 65  urn 0;.    while
63c20 28 20 69 73 64 69 67 69 74 28 2a 28 75 38 2a 29  ( isdigit(*(u8*)
63c30 7a 29 20 29 7b 20 7a 20 2b 3d 20 69 6e 63 72 3b  z) ){ z += incr;
63c40 20 7d 0a 20 20 20 20 69 66 28 20 72 65 61 6c 6e   }.    if( realn
63c50 75 6d 20 29 20 2a 72 65 61 6c 6e 75 6d 20 3d 20  um ) *realnum = 
63c60 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 2a 7a 3d  1;.  }.  if( *z=
63c70 3d 27 65 27 20 7c 7c 20 2a 7a 3d 3d 27 45 27 20  ='e' || *z=='E' 
63c80 29 7b 0a 20 20 20 20 7a 20 2b 3d 20 69 6e 63 72  ){.    z += incr
63c90 3b 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d 27 2b  ;.    if( *z=='+
63ca0 27 20 7c 7c 20 2a 7a 3d 3d 27 2d 27 20 29 20 7a  ' || *z=='-' ) z
63cb0 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 20 20 69 66   += incr;.    if
63cc0 28 20 21 69 73 64 69 67 69 74 28 2a 28 75 38 2a  ( !isdigit(*(u8*
63cd0 29 7a 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  )z) ) return 0;.
63ce0 20 20 20 20 77 68 69 6c 65 28 20 69 73 64 69 67      while( isdig
63cf0 69 74 28 2a 28 75 38 2a 29 7a 29 20 29 7b 20 7a  it(*(u8*)z) ){ z
63d00 20 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20 20 20 20   += incr; }.    
63d10 69 66 28 20 72 65 61 6c 6e 75 6d 20 29 20 2a 72  if( realnum ) *r
63d20 65 61 6c 6e 75 6d 20 3d 20 31 3b 0a 20 20 7d 0a  ealnum = 1;.  }.
63d30 20 20 72 65 74 75 72 6e 20 2a 7a 3d 3d 30 3b 0a    return *z==0;.
63d40 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 74 72  }../*.** The str
63d50 69 6e 67 20 7a 5b 5d 20 69 73 20 61 6e 20 61 73  ing z[] is an as
63d60 63 69 69 20 72 65 70 72 65 73 65 6e 74 61 74 69  cii representati
63d70 6f 6e 20 6f 66 20 61 20 72 65 61 6c 20 6e 75 6d  on of a real num
63d80 62 65 72 2e 0a 2a 2a 20 43 6f 6e 76 65 72 74 20  ber..** Convert 
63d90 74 68 69 73 20 73 74 72 69 6e 67 20 74 6f 20 61  this string to a
63da0 20 64 6f 75 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54   double..**.** T
63db0 68 69 73 20 72 6f 75 74 69 6e 65 20 61 73 73 75  his routine assu
63dc0 6d 65 73 20 74 68 61 74 20 7a 5b 5d 20 72 65 61  mes that z[] rea
63dd0 6c 6c 79 20 69 73 20 61 20 76 61 6c 69 64 20 6e  lly is a valid n
63de0 75 6d 62 65 72 2e 20 20 49 66 20 69 74 0a 2a 2a  umber.  If it.**
63df0 20 69 73 20 6e 6f 74 2c 20 74 68 65 20 72 65 73   is not, the res
63e00 75 6c 74 20 69 73 20 75 6e 64 65 66 69 6e 65 64  ult is undefined
63e10 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
63e20 74 69 6e 65 20 69 73 20 75 73 65 64 20 69 6e 73  tine is used ins
63e30 74 65 61 64 20 6f 66 20 74 68 65 20 6c 69 62 72  tead of the libr
63e40 61 72 79 20 61 74 6f 66 28 29 20 66 75 6e 63 74  ary atof() funct
63e50 69 6f 6e 20 62 65 63 61 75 73 65 0a 2a 2a 20 74  ion because.** t
63e60 68 65 20 6c 69 62 72 61 72 79 20 61 74 6f 66 28  he library atof(
63e70 29 20 6d 69 67 68 74 20 77 61 6e 74 20 74 6f 20  ) might want to 
63e80 75 73 65 20 22 2c 22 20 61 73 20 74 68 65 20 64  use "," as the d
63e90 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 69 6e 73  ecimal point ins
63ea0 74 65 61 64 0a 2a 2a 20 6f 66 20 22 2e 22 20 64  tead.** of "." d
63eb0 65 70 65 6e 64 69 6e 67 20 6f 6e 20 68 6f 77 20  epending on how 
63ec0 6c 6f 63 61 6c 65 20 69 73 20 73 65 74 2e 20 20  locale is set.  
63ed0 42 75 74 20 74 68 61 74 20 77 6f 75 6c 64 20 63  But that would c
63ee0 61 75 73 65 20 70 72 6f 62 6c 65 6d 73 0a 2a 2a  ause problems.**
63ef0 20 66 6f 72 20 53 51 4c 2e 20 20 53 6f 20 74 68   for SQL.  So th
63f00 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 77 61 79  is routine alway
63f10 73 20 75 73 65 73 20 22 2e 22 20 72 65 67 61 72  s uses "." regar
63f20 64 6c 65 73 73 20 6f 66 20 6c 6f 63 61 6c 65 2e  dless of locale.
63f30 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
63f40 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74  TE int sqlite3At
63f50 6f 46 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  oF(const char *z
63f60 2c 20 64 6f 75 62 6c 65 20 2a 70 52 65 73 75 6c  , double *pResul
63f70 74 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  t){.#ifndef SQLI
63f80 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
63f90 5f 50 4f 49 4e 54 0a 20 20 69 6e 74 20 73 69 67  _POINT.  int sig
63fa0 6e 20 3d 20 31 3b 0a 20 20 63 6f 6e 73 74 20 63  n = 1;.  const c
63fb0 68 61 72 20 2a 7a 42 65 67 69 6e 20 3d 20 7a 3b  har *zBegin = z;
63fc0 0a 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59  .  LONGDOUBLE_TY
63fd0 50 45 20 76 31 20 3d 20 30 2e 30 3b 0a 20 20 77  PE v1 = 0.0;.  w
63fe0 68 69 6c 65 28 20 69 73 73 70 61 63 65 28 2a 28  hile( isspace(*(
63ff0 75 38 2a 29 7a 29 20 29 20 7a 2b 2b 3b 0a 20 20  u8*)z) ) z++;.  
64000 69 66 28 20 2a 7a 3d 3d 27 2d 27 20 29 7b 0a 20  if( *z=='-' ){. 
64010 20 20 20 73 69 67 6e 20 3d 20 2d 31 3b 0a 20 20     sign = -1;.  
64020 20 20 7a 2b 2b 3b 0a 20 20 7d 65 6c 73 65 20 69    z++;.  }else i
64030 66 28 20 2a 7a 3d 3d 27 2b 27 20 29 7b 0a 20 20  f( *z=='+' ){.  
64040 20 20 7a 2b 2b 3b 0a 20 20 7d 0a 20 20 77 68 69    z++;.  }.  whi
64050 6c 65 28 20 69 73 64 69 67 69 74 28 2a 28 75 38  le( isdigit(*(u8
64060 2a 29 7a 29 20 29 7b 0a 20 20 20 20 76 31 20 3d  *)z) ){.    v1 =
64070 20 76 31 2a 31 30 2e 30 20 2b 20 28 2a 7a 20 2d   v1*10.0 + (*z -
64080 20 27 30 27 29 3b 0a 20 20 20 20 7a 2b 2b 3b 0a   '0');.    z++;.
64090 20 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2e    }.  if( *z=='.
640a0 27 20 29 7b 0a 20 20 20 20 4c 4f 4e 47 44 4f 55  ' ){.    LONGDOU
640b0 42 4c 45 5f 54 59 50 45 20 64 69 76 69 73 6f 72  BLE_TYPE divisor
640c0 20 3d 20 31 2e 30 3b 0a 20 20 20 20 7a 2b 2b 3b   = 1.0;.    z++;
640d0 0a 20 20 20 20 77 68 69 6c 65 28 20 69 73 64 69  .    while( isdi
640e0 67 69 74 28 2a 28 75 38 2a 29 7a 29 20 29 7b 0a  git(*(u8*)z) ){.
640f0 20 20 20 20 20 20 76 31 20 3d 20 76 31 2a 31 30        v1 = v1*10
64100 2e 30 20 2b 20 28 2a 7a 20 2d 20 27 30 27 29 3b  .0 + (*z - '0');
64110 0a 20 20 20 20 20 20 64 69 76 69 73 6f 72 20 2a  .      divisor *
64120 3d 20 31 30 2e 30 3b 0a 20 20 20 20 20 20 7a 2b  = 10.0;.      z+
64130 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 76 31 20  +;.    }.    v1 
64140 2f 3d 20 64 69 76 69 73 6f 72 3b 0a 20 20 7d 0a  /= divisor;.  }.
64150 20 20 69 66 28 20 2a 7a 3d 3d 27 65 27 20 7c 7c    if( *z=='e' ||
64160 20 2a 7a 3d 3d 27 45 27 20 29 7b 0a 20 20 20 20   *z=='E' ){.    
64170 69 6e 74 20 65 73 69 67 6e 20 3d 20 31 3b 0a 20  int esign = 1;. 
64180 20 20 20 69 6e 74 20 65 76 61 6c 20 3d 20 30 3b     int eval = 0;
64190 0a 20 20 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f  .    LONGDOUBLE_
641a0 54 59 50 45 20 73 63 61 6c 65 20 3d 20 31 2e 30  TYPE scale = 1.0
641b0 3b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 69  ;.    z++;.    i
641c0 66 28 20 2a 7a 3d 3d 27 2d 27 20 29 7b 0a 20 20  f( *z=='-' ){.  
641d0 20 20 20 20 65 73 69 67 6e 20 3d 20 2d 31 3b 0a      esign = -1;.
641e0 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d        z++;.    }
641f0 65 6c 73 65 20 69 66 28 20 2a 7a 3d 3d 27 2b 27  else if( *z=='+'
64200 20 29 7b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20   ){.      z++;. 
64210 20 20 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 20     }.    while( 
64220 69 73 64 69 67 69 74 28 2a 28 75 38 2a 29 7a 29  isdigit(*(u8*)z)
64230 20 29 7b 0a 20 20 20 20 20 20 65 76 61 6c 20 3d   ){.      eval =
64240 20 65 76 61 6c 2a 31 30 20 2b 20 2a 7a 20 2d 20   eval*10 + *z - 
64250 27 30 27 3b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a  '0';.      z++;.
64260 20 20 20 20 7d 0a 20 20 20 20 77 68 69 6c 65 28      }.    while(
64270 20 65 76 61 6c 3e 3d 36 34 20 29 7b 20 73 63 61   eval>=64 ){ sca
64280 6c 65 20 2a 3d 20 31 2e 30 65 2b 36 34 3b 20 65  le *= 1.0e+64; e
64290 76 61 6c 20 2d 3d 20 36 34 3b 20 7d 0a 20 20 20  val -= 64; }.   
642a0 20 77 68 69 6c 65 28 20 65 76 61 6c 3e 3d 31 36   while( eval>=16
642b0 20 29 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e 30   ){ scale *= 1.0
642c0 65 2b 31 36 3b 20 65 76 61 6c 20 2d 3d 20 31 36  e+16; eval -= 16
642d0 3b 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 65  ; }.    while( e
642e0 76 61 6c 3e 3d 34 20 29 7b 20 73 63 61 6c 65 20  val>=4 ){ scale 
642f0 2a 3d 20 31 2e 30 65 2b 34 3b 20 65 76 61 6c 20  *= 1.0e+4; eval 
64300 2d 3d 20 34 3b 20 7d 0a 20 20 20 20 77 68 69 6c  -= 4; }.    whil
64310 65 28 20 65 76 61 6c 3e 3d 31 20 29 7b 20 73 63  e( eval>=1 ){ sc
64320 61 6c 65 20 2a 3d 20 31 2e 30 65 2b 31 3b 20 65  ale *= 1.0e+1; e
64330 76 61 6c 20 2d 3d 20 31 3b 20 7d 0a 20 20 20 20  val -= 1; }.    
64340 69 66 28 20 65 73 69 67 6e 3c 30 20 29 7b 0a 20  if( esign<0 ){. 
64350 20 20 20 20 20 76 31 20 2f 3d 20 73 63 61 6c 65       v1 /= scale
64360 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
64370 20 20 20 76 31 20 2a 3d 20 73 63 61 6c 65 3b 0a     v1 *= scale;.
64380 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 52 65      }.  }.  *pRe
64390 73 75 6c 74 20 3d 20 73 69 67 6e 3c 30 20 3f 20  sult = sign<0 ? 
643a0 2d 76 31 20 3a 20 76 31 3b 0a 20 20 72 65 74 75  -v1 : v1;.  retu
643b0 72 6e 20 7a 20 2d 20 7a 42 65 67 69 6e 3b 0a 23  rn z - zBegin;.#
643c0 65 6c 73 65 0a 20 20 72 65 74 75 72 6e 20 73 71  else.  return sq
643d0 6c 69 74 65 33 41 74 6f 69 36 34 28 7a 2c 20 70  lite3Atoi64(z, p
643e0 52 65 73 75 6c 74 29 3b 0a 23 65 6e 64 69 66 20  Result);.#endif 
643f0 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  /* SQLITE_OMIT_F
64400 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 20 2a 2f  LOATING_POINT */
64410 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72  .}../*.** Compar
64420 65 20 74 68 65 20 31 39 2d 63 68 61 72 61 63 74  e the 19-charact
64430 65 72 20 73 74 72 69 6e 67 20 7a 4e 75 6d 20 61  er string zNum a
64440 67 61 69 6e 73 74 20 74 68 65 20 74 65 78 74 20  gainst the text 
64450 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 0a 2a  representation.*
64460 2a 20 76 61 6c 75 65 20 32 5e 36 33 3a 20 20 39  * value 2^63:  9
64470 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38  2233720368547758
64480 30 38 2e 20 20 52 65 74 75 72 6e 20 6e 65 67 61  08.  Return nega
64490 74 69 76 65 2c 20 7a 65 72 6f 2c 20 6f 72 20 70  tive, zero, or p
644a0 6f 73 69 74 69 76 65 0a 2a 2a 20 69 66 20 7a 4e  ositive.** if zN
644b0 75 6d 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c  um is less than,
644c0 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72   equal to, or gr
644d0 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 73  eater than the s
644e0 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c  tring..**.** Unl
644f0 69 6b 65 20 6d 65 6d 63 6d 70 28 29 20 74 68 69  ike memcmp() thi
64500 73 20 72 6f 75 74 69 6e 65 20 69 73 20 67 75 61  s routine is gua
64510 72 61 6e 74 65 65 64 20 74 6f 20 72 65 74 75 72  ranteed to retur
64520 6e 20 74 68 65 20 64 69 66 66 65 72 65 6e 63 65  n the difference
64530 0a 2a 2a 20 69 6e 20 74 68 65 20 76 61 6c 75 65  .** in the value
64540 73 20 6f 66 20 74 68 65 20 6c 61 73 74 20 64 69  s of the last di
64550 67 69 74 20 69 66 20 74 68 65 20 6f 6e 6c 79 20  git if the only 
64560 64 69 66 66 65 72 65 6e 63 65 20 69 73 20 69 6e  difference is in
64570 20 74 68 65 0a 2a 2a 20 6c 61 73 74 20 64 69 67   the.** last dig
64580 69 74 2e 20 20 53 6f 2c 20 66 6f 72 20 65 78 61  it.  So, for exa
64590 6d 70 6c 65 2c 0a 2a 2a 0a 2a 2a 20 20 20 20 20  mple,.**.**     
645a0 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 22   compare2pow63("
645b0 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35  9223372036854775
645c0 38 30 30 22 29 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c  800").**.** will
645d0 20 72 65 74 75 72 6e 20 2d 38 2e 0a 2a 2f 0a 73   return -8..*/.s
645e0 74 61 74 69 63 20 69 6e 74 20 63 6f 6d 70 61 72  tatic int compar
645f0 65 32 70 6f 77 36 33 28 63 6f 6e 73 74 20 63 68  e2pow63(const ch
64600 61 72 20 2a 7a 4e 75 6d 29 7b 0a 20 20 69 6e 74  ar *zNum){.  int
64610 20 63 3b 0a 20 20 63 20 3d 20 6d 65 6d 63 6d 70   c;.  c = memcmp
64620 28 7a 4e 75 6d 2c 22 39 32 32 33 33 37 32 30 33  (zNum,"922337203
64630 36 38 35 34 37 37 35 38 30 22 2c 31 38 29 3b 0a  685477580",18);.
64640 20 20 69 66 28 20 63 3d 3d 30 20 29 7b 0a 20 20    if( c==0 ){.  
64650 20 20 63 20 3d 20 7a 4e 75 6d 5b 31 38 5d 20 2d    c = zNum[18] -
64660 20 27 38 27 3b 0a 20 20 7d 0a 20 20 72 65 74 75   '8';.  }.  retu
64670 72 6e 20 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  rn c;.}.../*.** 
64680 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 7a  Return TRUE if z
64690 4e 75 6d 20 69 73 20 61 20 36 34 2d 62 69 74 20  Num is a 64-bit 
646a0 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 61  signed integer a
646b0 6e 64 20 77 72 69 74 65 0a 2a 2a 20 74 68 65 20  nd write.** the 
646c0 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e 74  value of the int
646d0 65 67 65 72 20 69 6e 74 6f 20 2a 70 4e 75 6d 2e  eger into *pNum.
646e0 20 20 49 66 20 7a 4e 75 6d 20 69 73 20 6e 6f 74    If zNum is not
646f0 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 6f   an integer.** o
64700 72 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20  r is an integer 
64710 74 68 61 74 20 69 73 20 74 6f 6f 20 6c 61 72 67  that is too larg
64720 65 20 74 6f 20 62 65 20 65 78 70 72 65 73 73 65  e to be expresse
64730 64 20 77 69 74 68 20 36 34 20 62 69 74 73 2c 0a  d with 64 bits,.
64740 2a 2a 20 74 68 65 6e 20 72 65 74 75 72 6e 20 66  ** then return f
64750 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  alse..**.** When
64760 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61   this routine wa
64770 73 20 6f 72 69 67 69 6e 61 6c 6c 79 20 77 72 69  s originally wri
64780 74 74 65 6e 20 69 74 20 64 65 61 6c 74 20 77 69  tten it dealt wi
64790 74 68 20 6f 6e 6c 79 0a 2a 2a 20 33 32 2d 62 69  th only.** 32-bi
647a0 74 20 6e 75 6d 62 65 72 73 2e 20 20 41 74 20 74  t numbers.  At t
647b0 68 61 74 20 74 69 6d 65 2c 20 69 74 20 77 61 73  hat time, it was
647c0 20 6d 75 63 68 20 66 61 73 74 65 72 20 74 68 61   much faster tha
647d0 6e 20 74 68 65 0a 2a 2a 20 61 74 6f 69 28 29 20  n the.** atoi() 
647e0 6c 69 62 72 61 72 79 20 72 6f 75 74 69 6e 65 20  library routine 
647f0 69 6e 20 52 65 64 48 61 74 20 37 2e 32 2e 0a 2a  in RedHat 7.2..*
64800 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
64810 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f 69   int sqlite3Atoi
64820 36 34 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  64(const char *z
64830 4e 75 6d 2c 20 69 36 34 20 2a 70 4e 75 6d 29 7b  Num, i64 *pNum){
64840 0a 20 20 69 36 34 20 76 20 3d 20 30 3b 0a 20 20  .  i64 v = 0;.  
64850 69 6e 74 20 6e 65 67 3b 0a 20 20 69 6e 74 20 69  int neg;.  int i
64860 2c 20 63 3b 0a 20 20 77 68 69 6c 65 28 20 69 73  , c;.  while( is
64870 73 70 61 63 65 28 2a 28 75 38 2a 29 7a 4e 75 6d  space(*(u8*)zNum
64880 29 20 29 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 69 66  ) ) zNum++;.  if
64890 28 20 2a 7a 4e 75 6d 3d 3d 27 2d 27 20 29 7b 0a  ( *zNum=='-' ){.
648a0 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20 20      neg = 1;.   
648b0 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 65   zNum++;.  }else
648c0 20 69 66 28 20 2a 7a 4e 75 6d 3d 3d 27 2b 27 20   if( *zNum=='+' 
648d0 29 7b 0a 20 20 20 20 6e 65 67 20 3d 20 30 3b 0a  ){.    neg = 0;.
648e0 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65      zNum++;.  }e
648f0 6c 73 65 7b 0a 20 20 20 20 6e 65 67 20 3d 20 30  lse{.    neg = 0
64900 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 7a  ;.  }.  while( z
64910 4e 75 6d 5b 30 5d 3d 3d 27 30 27 20 29 7b 20 7a  Num[0]=='0' ){ z
64920 4e 75 6d 2b 2b 3b 20 7d 20 2f 2a 20 53 6b 69 70  Num++; } /* Skip
64930 20 6f 76 65 72 20 6c 65 61 64 69 6e 67 20 7a 65   over leading ze
64940 72 6f 73 2e 20 54 69 63 6b 65 74 20 23 32 34 35  ros. Ticket #245
64950 34 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20  4 */.  for(i=0; 
64960 28 63 3d 7a 4e 75 6d 5b 69 5d 29 3e 3d 27 30 27  (c=zNum[i])>='0'
64970 20 26 26 20 63 3c 3d 27 39 27 3b 20 69 2b 2b 29   && c<='9'; i++)
64980 7b 0a 20 20 20 20 76 20 3d 20 76 2a 31 30 20 2b  {.    v = v*10 +
64990 20 63 20 2d 20 27 30 27 3b 0a 20 20 7d 0a 20 20   c - '0';.  }.  
649a0 2a 70 4e 75 6d 20 3d 20 6e 65 67 20 3f 20 2d 76  *pNum = neg ? -v
649b0 20 3a 20 76 3b 0a 20 20 69 66 28 20 63 21 3d 30   : v;.  if( c!=0
649c0 20 7c 7c 20 69 3d 3d 30 20 7c 7c 20 69 3e 31 39   || i==0 || i>19
649d0 20 29 7b 0a 20 20 20 20 2f 2a 20 7a 4e 75 6d 20   ){.    /* zNum 
649e0 69 73 20 65 6d 70 74 79 20 6f 72 20 63 6f 6e 74  is empty or cont
649f0 61 69 6e 73 20 6e 6f 6e 2d 6e 75 6d 65 72 69 63  ains non-numeric
64a00 20 74 65 78 74 20 6f 72 20 69 73 20 6c 6f 6e 67   text or is long
64a10 65 72 0a 20 20 20 20 2a 2a 20 74 68 61 6e 20 31  er.    ** than 1
64a20 39 20 64 69 67 69 74 73 20 28 74 68 75 73 20 67  9 digits (thus g
64a30 75 61 72 61 6e 74 69 6e 67 20 74 68 61 74 20 69  uaranting that i
64a40 74 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 29 20  t is too large) 
64a50 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  */.    return 0;
64a60 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 3c 31  .  }else if( i<1
64a70 39 20 29 7b 0a 20 20 20 20 2f 2a 20 4c 65 73 73  9 ){.    /* Less
64a80 20 74 68 61 6e 20 31 39 20 64 69 67 69 74 73 2c   than 19 digits,
64a90 20 73 6f 20 77 65 20 6b 6e 6f 77 20 74 68 61 74   so we know that
64aa0 20 69 74 20 66 69 74 73 20 69 6e 20 36 34 20 62   it fits in 64 b
64ab0 69 74 73 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  its */.    retur
64ac0 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  n 1;.  }else{.  
64ad0 20 20 2f 2a 20 31 39 2d 64 69 67 69 74 20 6e 75    /* 19-digit nu
64ae0 6d 62 65 72 73 20 6d 75 73 74 20 62 65 20 6e 6f  mbers must be no
64af0 20 6c 61 72 67 65 72 20 74 68 61 6e 20 39 32 32   larger than 922
64b00 33 33 37 32 30 33 36 38 35 34 37 37 35 38 30 37  3372036854775807
64b10 20 69 66 20 70 6f 73 69 74 69 76 65 0a 20 20 20   if positive.   
64b20 20 2a 2a 20 6f 72 20 39 32 32 33 33 37 32 30 33   ** or 922337203
64b30 36 38 35 34 37 37 35 38 30 38 20 69 66 20 6e 65  6854775808 if ne
64b40 67 61 74 69 76 65 2e 20 20 4e 6f 74 65 20 74 68  gative.  Note th
64b50 61 74 20 39 32 32 33 33 37 32 30 33 36 38 35 34  at 9223372036854
64b60 36 36 35 38 30 38 0a 20 20 20 20 2a 2a 20 69 73  665808.    ** is
64b70 20 32 5e 36 33 2e 20 2a 2f 0a 20 20 20 20 72 65   2^63. */.    re
64b80 74 75 72 6e 20 63 6f 6d 70 61 72 65 32 70 6f 77  turn compare2pow
64b90 36 33 28 7a 4e 75 6d 29 3c 6e 65 67 3b 0a 20 20  63(zNum)<neg;.  
64ba0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  }.}../*.** The s
64bb0 74 72 69 6e 67 20 7a 4e 75 6d 20 72 65 70 72 65  tring zNum repre
64bc0 73 65 6e 74 73 20 61 6e 20 69 6e 74 65 67 65 72  sents an integer
64bd0 2e 20 20 54 68 65 72 65 20 6d 69 67 68 74 20 62  .  There might b
64be0 65 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20  e some other.** 
64bf0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 6c 6c  information foll
64c00 6f 77 69 6e 67 20 74 68 65 20 69 6e 74 65 67 65  owing the intege
64c10 72 20 74 6f 6f 2c 20 62 75 74 20 74 68 61 74 20  r too, but that 
64c20 70 61 72 74 20 69 73 20 69 67 6e 6f 72 65 64 2e  part is ignored.
64c30 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 74 65 67  .** If the integ
64c40 65 72 20 74 68 61 74 20 74 68 65 20 70 72 65 66  er that the pref
64c50 69 78 20 6f 66 20 7a 4e 75 6d 20 72 65 70 72 65  ix of zNum repre
64c60 73 65 6e 74 73 20 77 69 6c 6c 20 66 69 74 20 69  sents will fit i
64c70 6e 20 61 0a 2a 2a 20 36 34 2d 62 69 74 20 73 69  n a.** 64-bit si
64c80 67 6e 65 64 20 69 6e 74 65 67 65 72 2c 20 72 65  gned integer, re
64c90 74 75 72 6e 20 54 52 55 45 2e 20 20 4f 74 68 65  turn TRUE.  Othe
64ca0 72 77 69 73 65 20 72 65 74 75 72 6e 20 46 41 4c  rwise return FAL
64cb0 53 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  SE..**.** This r
64cc0 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 46  outine returns F
64cd0 41 4c 53 45 20 66 6f 72 20 74 68 65 20 73 74 72  ALSE for the str
64ce0 69 6e 67 20 2d 39 32 32 33 33 37 32 30 33 36 38  ing -92233720368
64cf0 35 34 37 37 35 38 30 38 20 65 76 65 6e 20 74 68  54775808 even th
64d00 61 74 0a 2a 2a 20 74 68 61 74 20 6e 75 6d 62 65  at.** that numbe
64d10 72 20 77 69 6c 6c 2c 20 69 6e 20 74 68 65 6f 72  r will, in theor
64d20 79 20 66 69 74 20 69 6e 20 61 20 36 34 2d 62 69  y fit in a 64-bi
64d30 74 20 69 6e 74 65 67 65 72 2e 20 20 50 6f 73 69  t integer.  Posi
64d40 74 69 76 65 0a 2a 2a 20 39 32 32 33 33 37 33 30  tive.** 92233730
64d50 33 36 38 35 34 37 37 35 38 30 38 20 77 69 6c 6c  36854775808 will
64d60 20 6e 6f 74 20 66 69 74 20 69 6e 20 36 34 20 62   not fit in 64 b
64d70 69 74 73 2e 20 20 53 6f 20 69 74 20 73 65 65 6d  its.  So it seem
64d80 73 20 73 61 66 65 72 20 74 6f 20 72 65 74 75 72  s safer to retur
64d90 6e 0a 2a 2a 20 66 61 6c 73 65 2e 0a 2a 2f 0a 53  n.** false..*/.S
64da0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
64db0 74 20 73 71 6c 69 74 65 33 46 69 74 73 49 6e 36  t sqlite3FitsIn6
64dc0 34 42 69 74 73 28 63 6f 6e 73 74 20 63 68 61 72  4Bits(const char
64dd0 20 2a 7a 4e 75 6d 29 7b 0a 20 20 69 6e 74 20 69   *zNum){.  int i
64de0 2c 20 63 3b 0a 20 20 69 6e 74 20 6e 65 67 20 3d  , c;.  int neg =
64df0 20 30 3b 0a 20 20 69 66 28 20 2a 7a 4e 75 6d 3d   0;.  if( *zNum=
64e00 3d 27 2d 27 20 29 7b 0a 20 20 20 20 6e 65 67 20  ='-' ){.    neg 
64e10 3d 20 31 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b  = 1;.    zNum++;
64e20 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a 4e  .  }else if( *zN
64e30 75 6d 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 7a  um=='+' ){.    z
64e40 4e 75 6d 2b 2b 3b 0a 20 20 7d 0a 20 20 77 68 69  Num++;.  }.  whi
64e50 6c 65 28 20 2a 7a 4e 75 6d 3d 3d 27 30 27 20 29  le( *zNum=='0' )
64e60 7b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 20 20 20  {.    zNum++;   
64e70 2f 2a 20 53 6b 69 70 20 6c 65 61 64 69 6e 67 20  /* Skip leading 
64e80 7a 65 72 6f 73 2e 20 20 54 69 63 6b 65 74 20 23  zeros.  Ticket #
64e90 32 34 35 34 20 2a 2f 0a 20 20 7d 0a 20 20 66 6f  2454 */.  }.  fo
64ea0 72 28 69 3d 30 3b 20 28 63 3d 7a 4e 75 6d 5b 69  r(i=0; (c=zNum[i
64eb0 5d 29 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27 39  ])>='0' && c<='9
64ec0 27 3b 20 69 2b 2b 29 7b 7d 0a 20 20 69 66 28 20  '; i++){}.  if( 
64ed0 69 3c 31 39 20 29 7b 0a 20 20 20 20 2f 2a 20 47  i<19 ){.    /* G
64ee0 75 61 72 61 6e 74 65 65 64 20 74 6f 20 66 69 74  uaranteed to fit
64ef0 20 69 66 20 6c 65 73 73 20 74 68 61 6e 20 31 39   if less than 19
64f00 20 64 69 67 69 74 73 20 2a 2f 0a 20 20 20 20 72   digits */.    r
64f10 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65  eturn 1;.  }else
64f20 20 69 66 28 20 69 3e 31 39 20 29 7b 0a 20 20 20   if( i>19 ){.   
64f30 20 2f 2a 20 47 75 61 72 61 6e 74 65 65 64 20 74   /* Guaranteed t
64f40 6f 20 62 65 20 74 6f 6f 20 62 69 67 20 69 66 20  o be too big if 
64f50 67 72 65 61 74 65 72 20 74 68 61 6e 20 31 39 20  greater than 19 
64f60 64 69 67 69 74 73 20 2a 2f 0a 20 20 20 20 72 65  digits */.    re
64f70 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 7b  turn 0;.  }else{
64f80 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 61 72 65 20  .    /* Compare 
64f90 61 67 61 69 6e 73 74 20 32 5e 36 33 2e 20 2a 2f  against 2^63. */
64fa0 0a 20 20 20 20 72 65 74 75 72 6e 20 63 6f 6d 70  .    return comp
64fb0 61 72 65 32 70 6f 77 36 33 28 7a 4e 75 6d 29 3c  are2pow63(zNum)<
64fc0 6e 65 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  neg;.  }.}../*.*
64fd0 2a 20 49 66 20 7a 4e 75 6d 20 72 65 70 72 65 73  * If zNum repres
64fe0 65 6e 74 73 20 61 6e 20 69 6e 74 65 67 65 72 20  ents an integer 
64ff0 74 68 61 74 20 77 69 6c 6c 20 66 69 74 20 69 6e  that will fit in
65000 20 33 32 2d 62 69 74 73 2c 20 74 68 65 6e 20 73   32-bits, then s
65010 65 74 0a 2a 2a 20 2a 70 56 61 6c 75 65 20 74 6f  et.** *pValue to
65020 20 74 68 61 74 20 69 6e 74 65 67 65 72 20 61 6e   that integer an
65030 64 20 72 65 74 75 72 6e 20 74 72 75 65 2e 20 20  d return true.  
65040 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 72 6e  Otherwise return
65050 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e   false..**.** An
65060 79 20 6e 6f 6e 2d 6e 75 6d 65 72 69 63 20 63 68  y non-numeric ch
65070 61 72 61 63 74 65 72 73 20 74 68 61 74 20 66 6f  aracters that fo
65080 6c 6c 6f 77 69 6e 67 20 7a 4e 75 6d 20 61 72 65  llowing zNum are
65090 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 20 54 68 69   ignored..** Thi
650a0 73 20 69 73 20 64 69 66 66 65 72 65 6e 74 20 66  s is different f
650b0 72 6f 6d 20 73 71 6c 69 74 65 33 41 74 6f 69 36  rom sqlite3Atoi6
650c0 34 28 29 20 77 68 69 63 68 20 72 65 71 75 69 72  4() which requir
650d0 65 73 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20  es the.** input 
650e0 6e 75 6d 62 65 72 20 74 6f 20 62 65 20 7a 65 72  number to be zer
650f0 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 0a 2a 2f  o-terminated..*/
65100 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
65110 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 49 6e  int sqlite3GetIn
65120 74 33 32 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  t32(const char *
65130 7a 4e 75 6d 2c 20 69 6e 74 20 2a 70 56 61 6c 75  zNum, int *pValu
65140 65 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74  e){.  sqlite_int
65150 36 34 20 76 20 3d 20 30 3b 0a 20 20 69 6e 74 20  64 v = 0;.  int 
65160 69 2c 20 63 3b 0a 20 20 69 6e 74 20 6e 65 67 20  i, c;.  int neg 
65170 3d 20 30 3b 0a 20 20 69 66 28 20 7a 4e 75 6d 5b  = 0;.  if( zNum[
65180 30 5d 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 6e  0]=='-' ){.    n
65190 65 67 20 3d 20 31 3b 0a 20 20 20 20 7a 4e 75 6d  eg = 1;.    zNum
651a0 2b 2b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  ++;.  }else if( 
651b0 7a 4e 75 6d 5b 30 5d 3d 3d 27 2b 27 20 29 7b 0a  zNum[0]=='+' ){.
651c0 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 0a      zNum++;.  }.
651d0 20 20 77 68 69 6c 65 28 20 7a 4e 75 6d 5b 30 5d    while( zNum[0]
651e0 3d 3d 27 30 27 20 29 20 7a 4e 75 6d 2b 2b 3b 0a  =='0' ) zNum++;.
651f0 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31 30 20    for(i=0; i<10 
65200 26 26 20 28 63 20 3d 20 7a 4e 75 6d 5b 69 5d 20  && (c = zNum[i] 
65210 2d 20 27 30 27 29 3e 3d 30 20 26 26 20 63 3c 3d  - '0')>=0 && c<=
65220 39 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 76 20 3d  9; i++){.    v =
65230 20 76 2a 31 30 20 2b 20 63 3b 0a 20 20 7d 0a 20   v*10 + c;.  }. 
65240 20 69 66 28 20 69 3e 39 20 29 7b 0a 20 20 20 20   if( i>9 ){.    
65250 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
65260 69 66 28 20 76 2d 6e 65 67 3e 32 31 34 37 34 38  if( v-neg>214748
65270 33 36 34 37 20 29 7b 0a 20 20 20 20 72 65 74 75  3647 ){.    retu
65280 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20  rn 0;.  }.  if( 
65290 6e 65 67 20 29 7b 0a 20 20 20 20 76 20 3d 20 2d  neg ){.    v = -
652a0 76 3b 0a 20 20 7d 0a 20 20 2a 70 56 61 6c 75 65  v;.  }.  *pValue
652b0 20 3d 20 28 69 6e 74 29 76 3b 0a 20 20 72 65 74   = (int)v;.  ret
652c0 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 1;.}../*.** 
652d0 43 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75  Check to make su
652e0 72 65 20 77 65 20 68 61 76 65 20 61 20 76 61 6c  re we have a val
652f0 69 64 20 64 62 20 70 6f 69 6e 74 65 72 2e 20 20  id db pointer.  
65300 54 68 69 73 20 74 65 73 74 20 69 73 20 6e 6f 74  This test is not
65310 0a 2a 2a 20 66 6f 6f 6c 70 72 6f 6f 66 20 62 75  .** foolproof bu
65320 74 20 69 74 20 64 6f 65 73 20 70 72 6f 76 69 64  t it does provid
65330 65 20 73 6f 6d 65 20 6d 65 61 73 75 72 65 20 6f  e some measure o
65340 66 20 70 72 6f 74 65 63 74 69 6f 6e 20 61 67 61  f protection aga
65350 69 6e 73 74 0a 2a 2a 20 6d 69 73 75 73 65 20 6f  inst.** misuse o
65360 66 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20  f the interface 
65370 73 75 63 68 20 61 73 20 70 61 73 73 69 6e 67 20  such as passing 
65380 69 6e 20 64 62 20 70 6f 69 6e 74 65 72 73 20 74  in db pointers t
65390 68 61 74 20 61 72 65 0a 2a 2a 20 4e 55 4c 4c 20  hat are.** NULL 
653a0 6f 72 20 77 68 69 63 68 20 68 61 76 65 20 62 65  or which have be
653b0 65 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 63 6c  en previously cl
653c0 6f 73 65 64 2e 20 20 49 66 20 74 68 69 73 20 72  osed.  If this r
653d0 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 0a 2a  outine returns.*
653e0 2a 20 54 52 55 45 20 69 74 20 6d 65 61 6e 73 20  * TRUE it means 
653f0 74 68 61 74 20 74 68 65 20 64 62 20 70 6f 69 6e  that the db poin
65400 74 65 72 20 69 73 20 69 6e 76 61 6c 69 64 20 61  ter is invalid a
65410 6e 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65  nd should not be
65420 0a 2a 2a 20 64 65 72 65 66 65 72 65 6e 63 65 64  .** dereferenced
65430 20 66 6f 72 20 61 6e 79 20 72 65 61 73 6f 6e 2e   for any reason.
65440 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75    The calling fu
65450 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 69 6e  nction should in
65460 76 6f 6b 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d  voke.** SQLITE_M
65470 49 53 55 53 45 20 69 6d 6d 65 64 69 61 74 65 6c  ISUSE immediatel
65480 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
65490 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
654a0 53 61 66 65 74 79 43 68 65 63 6b 28 73 71 6c 69  SafetyCheck(sqli
654b0 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20  te3 *db){.  int 
654c0 6d 61 67 69 63 3b 0a 20 20 69 66 28 20 64 62 3d  magic;.  if( db=
654d0 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20  =0 ) return 1;. 
654e0 20 6d 61 67 69 63 20 3d 20 64 62 2d 3e 6d 61 67   magic = db->mag
654f0 69 63 3b 0a 20 20 69 66 28 20 6d 61 67 69 63 21  ic;.  if( magic!
65500 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 43 4c  =SQLITE_MAGIC_CL
65510 4f 53 45 44 20 26 26 0a 20 20 20 20 20 20 20 20  OSED &&.        
65520 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d   magic!=SQLITE_M
65530 41 47 49 43 5f 4f 50 45 4e 20 26 26 0a 20 20 20  AGIC_OPEN &&.   
65540 20 20 20 20 20 20 6d 61 67 69 63 21 3d 53 51 4c        magic!=SQL
65550 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29  ITE_MAGIC_BUSY )
65560 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 72 65 74   return 1;.  ret
65570 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
65580 54 68 65 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e  The variable-len
65590 67 74 68 20 69 6e 74 65 67 65 72 20 65 6e 63 6f  gth integer enco
655a0 64 69 6e 67 20 69 73 20 61 73 20 66 6f 6c 6c 6f  ding is as follo
655b0 77 73 3a 0a 2a 2a 0a 2a 2a 20 4b 45 59 3a 0a 2a  ws:.**.** KEY:.*
655c0 2a 20 20 20 20 20 20 20 20 20 41 20 3d 20 30 78  *         A = 0x
655d0 78 78 78 78 78 78 20 20 20 20 37 20 62 69 74 73  xxxxxx    7 bits
655e0 20 6f 66 20 64 61 74 61 20 61 6e 64 20 6f 6e 65   of data and one
655f0 20 66 6c 61 67 20 62 69 74 0a 2a 2a 20 20 20 20   flag bit.**    
65600 20 20 20 20 20 42 20 3d 20 31 78 78 78 78 78 78       B = 1xxxxxx
65610 78 20 20 20 20 37 20 62 69 74 73 20 6f 66 20 64  x    7 bits of d
65620 61 74 61 20 61 6e 64 20 6f 6e 65 20 66 6c 61 67  ata and one flag
65630 20 62 69 74 0a 2a 2a 20 20 20 20 20 20 20 20 20   bit.**         
65640 43 20 3d 20 78 78 78 78 78 78 78 78 20 20 20 20  C = xxxxxxxx    
65650 38 20 62 69 74 73 20 6f 66 20 64 61 74 61 0a 2a  8 bits of data.*
65660 2a 0a 2a 2a 20 20 37 20 62 69 74 73 20 2d 20 41  *.**  7 bits - A
65670 0a 2a 2a 20 31 34 20 62 69 74 73 20 2d 20 42 41  .** 14 bits - BA
65680 0a 2a 2a 20 32 31 20 62 69 74 73 20 2d 20 42 42  .** 21 bits - BB
65690 41 0a 2a 2a 20 32 38 20 62 69 74 73 20 2d 20 42  A.** 28 bits - B
656a0 42 42 41 0a 2a 2a 20 33 35 20 62 69 74 73 20 2d  BBA.** 35 bits -
656b0 20 42 42 42 42 41 0a 2a 2a 20 34 32 20 62 69 74   BBBBA.** 42 bit
656c0 73 20 2d 20 42 42 42 42 42 41 0a 2a 2a 20 34 39  s - BBBBBA.** 49
656d0 20 62 69 74 73 20 2d 20 42 42 42 42 42 42 41 0a   bits - BBBBBBA.
656e0 2a 2a 20 35 36 20 62 69 74 73 20 2d 20 42 42 42  ** 56 bits - BBB
656f0 42 42 42 42 41 0a 2a 2a 20 36 34 20 62 69 74 73  BBBBA.** 64 bits
65700 20 2d 20 42 42 42 42 42 42 42 42 43 0a 2a 2f 0a   - BBBBBBBBC.*/.
65710 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 36  ./*.** Write a 6
65720 34 2d 62 69 74 20 76 61 72 69 61 62 6c 65 2d 6c  4-bit variable-l
65730 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 74 6f  ength integer to
65740 20 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67   memory starting
65750 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20 54 68 65   at p[0]..** The
65760 20 6c 65 6e 67 74 68 20 6f 66 20 64 61 74 61 20   length of data 
65770 77 72 69 74 65 20 77 69 6c 6c 20 62 65 20 62 65  write will be be
65780 74 77 65 65 6e 20 31 20 61 6e 64 20 39 20 62 79  tween 1 and 9 by
65790 74 65 73 2e 20 20 54 68 65 20 6e 75 6d 62 65 72  tes.  The number
657a0 0a 2a 2a 20 6f 66 20 62 79 74 65 73 20 77 72 69  .** of bytes wri
657b0 74 74 65 6e 20 69 73 20 72 65 74 75 72 6e 65 64  tten is returned
657c0 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 61 72 69 61 62  ..**.** A variab
657d0 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65  le-length intege
657e0 72 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 68  r consists of th
657f0 65 20 6c 6f 77 65 72 20 37 20 62 69 74 73 20 6f  e lower 7 bits o
65800 66 20 65 61 63 68 20 62 79 74 65 0a 2a 2a 20 66  f each byte.** f
65810 6f 72 20 61 6c 6c 20 62 79 74 65 73 20 74 68 61  or all bytes tha
65820 74 20 68 61 76 65 20 74 68 65 20 38 74 68 20 62  t have the 8th b
65830 69 74 20 73 65 74 20 61 6e 64 20 6f 6e 65 20 62  it set and one b
65840 79 74 65 20 77 69 74 68 20 74 68 65 20 38 74 68  yte with the 8th
65850 0a 2a 2a 20 62 69 74 20 63 6c 65 61 72 2e 20 20  .** bit clear.  
65860 45 78 63 65 70 74 2c 20 69 66 20 77 65 20 67 65  Except, if we ge
65870 74 20 74 6f 20 74 68 65 20 39 74 68 20 62 79 74  t to the 9th byt
65880 65 2c 20 69 74 20 73 74 6f 72 65 73 20 74 68 65  e, it stores the
65890 20 66 75 6c 6c 0a 2a 2a 20 38 20 62 69 74 73 20   full.** 8 bits 
658a0 61 6e 64 20 69 73 20 74 68 65 20 6c 61 73 74 20  and is the last 
658b0 62 79 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  byte..*/.SQLITE_
658c0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
658d0 74 65 33 50 75 74 56 61 72 69 6e 74 28 75 6e 73  te3PutVarint(uns
658e0 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75  igned char *p, u
658f0 36 34 20 76 29 7b 0a 20 20 69 6e 74 20 69 2c 20  64 v){.  int i, 
65900 6a 2c 20 6e 3b 0a 20 20 75 38 20 62 75 66 5b 31  j, n;.  u8 buf[1
65910 30 5d 3b 0a 20 20 69 66 28 20 76 20 26 20 28 28  0];.  if( v & ((
65920 28 75 36 34 29 30 78 66 66 30 30 30 30 30 30 29  (u64)0xff000000)
65930 3c 3c 33 32 29 20 29 7b 0a 20 20 20 20 70 5b 38  <<32) ){.    p[8
65940 5d 20 3d 20 76 3b 0a 20 20 20 20 76 20 3e 3e 3d  ] = v;.    v >>=
65950 20 38 3b 0a 20 20 20 20 66 6f 72 28 69 3d 37 3b   8;.    for(i=7;
65960 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20   i>=0; i--){.   
65970 20 20 20 70 5b 69 5d 20 3d 20 28 76 20 26 20 30     p[i] = (v & 0
65980 78 37 66 29 20 7c 20 30 78 38 30 3b 0a 20 20 20  x7f) | 0x80;.   
65990 20 20 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 20 20     v >>= 7;.    
659a0 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 39 3b 0a  }.    return 9;.
659b0 20 20 7d 20 20 20 20 0a 20 20 6e 20 3d 20 30 3b    }    .  n = 0;
659c0 0a 20 20 64 6f 7b 0a 20 20 20 20 62 75 66 5b 6e  .  do{.    buf[n
659d0 2b 2b 5d 20 3d 20 28 76 20 26 20 30 78 37 66 29  ++] = (v & 0x7f)
659e0 20 7c 20 30 78 38 30 3b 0a 20 20 20 20 76 20 3e   | 0x80;.    v >
659f0 3e 3d 20 37 3b 0a 20 20 7d 77 68 69 6c 65 28 20  >= 7;.  }while( 
65a00 76 21 3d 30 20 29 3b 0a 20 20 62 75 66 5b 30 5d  v!=0 );.  buf[0]
65a10 20 26 3d 20 30 78 37 66 3b 0a 20 20 61 73 73 65   &= 0x7f;.  asse
65a20 72 74 28 20 6e 3c 3d 39 20 29 3b 0a 20 20 66 6f  rt( n<=9 );.  fo
65a30 72 28 69 3d 30 2c 20 6a 3d 6e 2d 31 3b 20 6a 3e  r(i=0, j=n-1; j>
65a40 3d 30 3b 20 6a 2d 2d 2c 20 69 2b 2b 29 7b 0a 20  =0; j--, i++){. 
65a50 20 20 20 70 5b 69 5d 20 3d 20 62 75 66 5b 6a 5d     p[i] = buf[j]
65a60 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e  ;.  }.  return n
65a70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20  ;.}../*.** Read 
65a80 61 20 36 34 2d 62 69 74 20 76 61 72 69 61 62 6c  a 64-bit variabl
65a90 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72  e-length integer
65aa0 20 66 72 6f 6d 20 6d 65 6d 6f 72 79 20 73 74 61   from memory sta
65ab0 72 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 2a  rting at p[0]..*
65ac0 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
65ad0 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 61  ber of bytes rea
65ae0 64 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73  d.  The value is
65af0 20 73 74 6f 72 65 64 20 69 6e 20 2a 76 2e 0a 2a   stored in *v..*
65b00 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
65b10 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 56   int sqlite3GetV
65b20 61 72 69 6e 74 28 63 6f 6e 73 74 20 75 6e 73 69  arint(const unsi
65b30 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75 36  gned char *p, u6
65b40 34 20 2a 76 29 7b 0a 20 20 75 33 32 20 78 3b 0a  4 *v){.  u32 x;.
65b50 20 20 75 36 34 20 78 36 34 3b 0a 20 20 69 6e 74    u64 x64;.  int
65b60 20 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   n;.  unsigned c
65b70 68 61 72 20 63 3b 0a 20 20 69 66 28 20 28 28 63  har c;.  if( ((c
65b80 20 3d 20 70 5b 30 5d 29 20 26 20 30 78 38 30 29   = p[0]) & 0x80)
65b90 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 76 20 3d 20  ==0 ){.    *v = 
65ba0 63 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  c;.    return 1;
65bb0 0a 20 20 7d 0a 20 20 78 20 3d 20 63 20 26 20 30  .  }.  x = c & 0
65bc0 78 37 66 3b 0a 20 20 69 66 28 20 28 28 63 20 3d  x7f;.  if( ((c =
65bd0 20 70 5b 31 5d 29 20 26 20 30 78 38 30 29 3d 3d   p[1]) & 0x80)==
65be0 30 20 29 7b 0a 20 20 20 20 2a 76 20 3d 20 28 78  0 ){.    *v = (x
65bf0 3c 3c 37 29 20 7c 20 63 3b 0a 20 20 20 20 72 65  <<7) | c;.    re
65c00 74 75 72 6e 20 32 3b 0a 20 20 7d 0a 20 20 78 20  turn 2;.  }.  x 
65c10 3d 20 28 78 3c 3c 37 29 20 7c 20 28 63 26 30 78  = (x<<7) | (c&0x
65c20 37 66 29 3b 0a 20 20 69 66 28 20 28 28 63 20 3d  7f);.  if( ((c =
65c30 20 70 5b 32 5d 29 20 26 20 30 78 38 30 29 3d 3d   p[2]) & 0x80)==
65c40 30 20 29 7b 0a 20 20 20 20 2a 76 20 3d 20 28 78  0 ){.    *v = (x
65c50 3c 3c 37 29 20 7c 20 63 3b 0a 20 20 20 20 72 65  <<7) | c;.    re
65c60 74 75 72 6e 20 33 3b 0a 20 20 7d 0a 20 20 78 20  turn 3;.  }.  x 
65c70 3d 20 28 78 3c 3c 37 29 20 7c 20 28 63 26 30 78  = (x<<7) | (c&0x
65c80 37 66 29 3b 0a 20 20 69 66 28 20 28 28 63 20 3d  7f);.  if( ((c =
65c90 20 70 5b 33 5d 29 20 26 20 30 78 38 30 29 3d 3d   p[3]) & 0x80)==
65ca0 30 20 29 7b 0a 20 20 20 20 2a 76 20 3d 20 28 78  0 ){.    *v = (x
65cb0 3c 3c 37 29 20 7c 20 63 3b 0a 20 20 20 20 72 65  <<7) | c;.    re
65cc0 74 75 72 6e 20 34 3b 0a 20 20 7d 0a 20 20 78 36  turn 4;.  }.  x6
65cd0 34 20 3d 20 28 78 3c 3c 37 29 20 7c 20 28 63 26  4 = (x<<7) | (c&
65ce0 30 78 37 66 29 3b 0a 20 20 6e 20 3d 20 34 3b 0a  0x7f);.  n = 4;.
65cf0 20 20 64 6f 7b 0a 20 20 20 20 63 20 3d 20 70 5b    do{.    c = p[
65d00 6e 2b 2b 5d 3b 0a 20 20 20 20 69 66 28 20 6e 3d  n++];.    if( n=
65d10 3d 39 20 29 7b 0a 20 20 20 20 20 20 78 36 34 20  =9 ){.      x64 
65d20 3d 20 28 78 36 34 3c 3c 38 29 20 7c 20 63 3b 0a  = (x64<<8) | c;.
65d30 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
65d40 20 7d 0a 20 20 20 20 78 36 34 20 3d 20 28 78 36   }.    x64 = (x6
65d50 34 3c 3c 37 29 20 7c 20 28 63 26 30 78 37 66 29  4<<7) | (c&0x7f)
65d60 3b 0a 20 20 7d 77 68 69 6c 65 28 20 28 63 20 26  ;.  }while( (c &
65d70 20 30 78 38 30 29 21 3d 30 20 29 3b 0a 20 20 2a   0x80)!=0 );.  *
65d80 76 20 3d 20 78 36 34 3b 0a 20 20 72 65 74 75 72  v = x64;.  retur
65d90 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  n n;.}../*.** Re
65da0 61 64 20 61 20 33 32 2d 62 69 74 20 76 61 72 69  ad a 32-bit vari
65db0 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65  able-length inte
65dc0 67 65 72 20 66 72 6f 6d 20 6d 65 6d 6f 72 79 20  ger from memory 
65dd0 73 74 61 72 74 69 6e 67 20 61 74 20 70 5b 30 5d  starting at p[0]
65de0 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  ..** Return the 
65df0 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
65e00 72 65 61 64 2e 20 20 54 68 65 20 76 61 6c 75 65  read.  The value
65e10 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 2a 76   is stored in *v
65e20 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
65e30 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47  ATE int sqlite3G
65e40 65 74 56 61 72 69 6e 74 33 32 28 63 6f 6e 73 74  etVarint32(const
65e50 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
65e60 70 2c 20 75 33 32 20 2a 76 29 7b 0a 20 20 75 33  p, u32 *v){.  u3
65e70 32 20 78 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20  2 x;.  int n;.  
65e80 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 3b  unsigned char c;
65e90 0a 20 20 69 66 28 20 28 28 73 69 67 6e 65 64 20  .  if( ((signed 
65ea0 63 68 61 72 2a 29 70 29 5b 30 5d 3e 3d 30 20 29  char*)p)[0]>=0 )
65eb0 7b 0a 20 20 20 20 2a 76 20 3d 20 70 5b 30 5d 3b  {.    *v = p[0];
65ec0 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
65ed0 20 7d 0a 20 20 78 20 3d 20 70 5b 30 5d 20 26 20   }.  x = p[0] & 
65ee0 30 78 37 66 3b 0a 20 20 69 66 28 20 28 28 73 69  0x7f;.  if( ((si
65ef0 67 6e 65 64 20 63 68 61 72 2a 29 70 29 5b 31 5d  gned char*)p)[1]
65f00 3e 3d 30 20 29 7b 0a 20 20 20 20 2a 76 20 3d 20  >=0 ){.    *v = 
65f10 28 78 3c 3c 37 29 20 7c 20 70 5b 31 5d 3b 0a 20  (x<<7) | p[1];. 
65f20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 7d     return 2;.  }
65f30 0a 20 20 78 20 3d 20 28 78 3c 3c 37 29 20 7c 20  .  x = (x<<7) | 
65f40 28 70 5b 31 5d 20 26 20 30 78 37 66 29 3b 0a 20  (p[1] & 0x7f);. 
65f50 20 6e 20 3d 20 32 3b 0a 20 20 64 6f 7b 0a 20 20   n = 2;.  do{.  
65f60 20 20 78 20 3d 20 28 78 3c 3c 37 29 20 7c 20 28    x = (x<<7) | (
65f70 28 63 20 3d 20 70 5b 6e 2b 2b 5d 29 26 30 78 37  (c = p[n++])&0x7
65f80 66 29 3b 0a 20 20 7d 77 68 69 6c 65 28 20 28 63  f);.  }while( (c
65f90 20 26 20 30 78 38 30 29 21 3d 30 20 26 26 20 6e   & 0x80)!=0 && n
65fa0 3c 39 20 29 3b 0a 20 20 2a 76 20 3d 20 78 3b 0a  <9 );.  *v = x;.
65fb0 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f    return n;.}../
65fc0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
65fd0 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
65fe0 74 68 61 74 20 77 69 6c 6c 20 62 65 20 6e 65 65  that will be nee
65ff0 64 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65  ded to store the
66000 20 67 69 76 65 6e 0a 2a 2a 20 36 34 2d 62 69 74   given.** 64-bit
66010 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 53 51 4c   integer..*/.SQL
66020 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
66030 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e  sqlite3VarintLen
66040 28 75 36 34 20 76 29 7b 0a 20 20 69 6e 74 20 69  (u64 v){.  int i
66050 20 3d 20 30 3b 0a 20 20 64 6f 7b 0a 20 20 20 20   = 0;.  do{.    
66060 69 2b 2b 3b 0a 20 20 20 20 76 20 3e 3e 3d 20 37  i++;.    v >>= 7
66070 3b 0a 20 20 7d 77 68 69 6c 65 28 20 76 21 3d 30  ;.  }while( v!=0
66080 20 26 26 20 69 3c 39 20 29 3b 0a 20 20 72 65 74   && i<9 );.  ret
66090 75 72 6e 20 69 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  urn i;.}.../*.**
660a0 20 52 65 61 64 20 6f 72 20 77 72 69 74 65 20 61   Read or write a
660b0 20 66 6f 75 72 2d 62 79 74 65 20 62 69 67 2d 65   four-byte big-e
660c0 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 76 61  ndian integer va
660d0 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  lue..*/.SQLITE_P
660e0 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74  RIVATE u32 sqlit
660f0 65 33 47 65 74 34 62 79 74 65 28 63 6f 6e 73 74  e3Get4byte(const
66100 20 75 38 20 2a 70 29 7b 0a 20 20 72 65 74 75 72   u8 *p){.  retur
66110 6e 20 28 70 5b 30 5d 3c 3c 32 34 29 20 7c 20 28  n (p[0]<<24) | (
66120 70 5b 31 5d 3c 3c 31 36 29 20 7c 20 28 70 5b 32  p[1]<<16) | (p[2
66130 5d 3c 3c 38 29 20 7c 20 70 5b 33 5d 3b 0a 7d 0a  ]<<8) | p[3];.}.
66140 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
66150 6f 69 64 20 73 71 6c 69 74 65 33 50 75 74 34 62  oid sqlite3Put4b
66160 79 74 65 28 75 6e 73 69 67 6e 65 64 20 63 68 61  yte(unsigned cha
66170 72 20 2a 70 2c 20 75 33 32 20 76 29 7b 0a 20 20  r *p, u32 v){.  
66180 70 5b 30 5d 20 3d 20 76 3e 3e 32 34 3b 0a 20 20  p[0] = v>>24;.  
66190 70 5b 31 5d 20 3d 20 76 3e 3e 31 36 3b 0a 20 20  p[1] = v>>16;.  
661a0 70 5b 32 5d 20 3d 20 76 3e 3e 38 3b 0a 20 20 70  p[2] = v>>8;.  p
661b0 5b 33 5d 20 3d 20 76 3b 0a 7d 0a 0a 0a 0a 23 69  [3] = v;.}....#i
661c0 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
661d0 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45  E_OMIT_BLOB_LITE
661e0 52 41 4c 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  RAL) || defined(
661f0 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43  SQLITE_HAS_CODEC
66200 29 20 5c 0a 20 20 20 20 7c 7c 20 64 65 66 69 6e  ) \.    || defin
66210 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a  ed(SQLITE_TEST).
66220 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61 74 65 20  /*.** Translate 
66230 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 6f 66  a single byte of
66240 20 48 65 78 20 69 6e 74 6f 20 61 6e 20 69 6e 74   Hex into an int
66250 65 67 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  eger..*/.static 
66260 69 6e 74 20 68 65 78 54 6f 49 6e 74 28 69 6e 74  int hexToInt(int
66270 20 68 29 7b 0a 20 20 69 66 28 20 68 3e 3d 27 30   h){.  if( h>='0
66280 27 20 26 26 20 68 3c 3d 27 39 27 20 29 7b 0a 20  ' && h<='9' ){. 
66290 20 20 20 72 65 74 75 72 6e 20 68 20 2d 20 27 30     return h - '0
662a0 27 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 68  ';.  }else if( h
662b0 3e 3d 27 61 27 20 26 26 20 68 3c 3d 27 66 27 20  >='a' && h<='f' 
662c0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 68 20  ){.    return h 
662d0 2d 20 27 61 27 20 2b 20 31 30 3b 0a 20 20 7d 65  - 'a' + 10;.  }e
662e0 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
662f0 20 68 3e 3d 27 41 27 20 26 26 20 68 3c 3d 27 46   h>='A' && h<='F
66300 27 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ' );.    return 
66310 68 20 2d 20 27 41 27 20 2b 20 31 30 3b 0a 20 20  h - 'A' + 10;.  
66320 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53  }.}.#endif /* !S
66330 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f  QLITE_OMIT_BLOB_
66340 4c 49 54 45 52 41 4c 20 7c 7c 20 53 51 4c 49 54  LITERAL || SQLIT
66350 45 5f 48 41 53 5f 43 4f 44 45 43 20 7c 7c 20 53  E_HAS_CODEC || S
66360 51 4c 49 54 45 5f 54 45 53 54 20 2a 2f 0a 0a 23  QLITE_TEST */..#
66370 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
66380 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54  TE_OMIT_BLOB_LIT
66390 45 52 41 4c 29 20 7c 7c 20 64 65 66 69 6e 65 64  ERAL) || defined
663a0 28 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45  (SQLITE_HAS_CODE
663b0 43 29 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74  C)./*.** Convert
663c0 20 61 20 42 4c 4f 42 20 6c 69 74 65 72 61 6c 20   a BLOB literal 
663d0 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 78 27 68  of the form "x'h
663e0 68 68 68 68 68 27 22 20 69 6e 74 6f 20 69 74 73  hhhhh'" into its
663f0 20 62 69 6e 61 72 79 0a 2a 2a 20 76 61 6c 75 65   binary.** value
66400 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  .  Return a poin
66410 74 65 72 20 74 6f 20 69 74 73 20 62 69 6e 61 72  ter to its binar
66420 79 20 76 61 6c 75 65 2e 20 20 53 70 61 63 65 20  y value.  Space 
66430 74 6f 20 68 6f 6c 64 20 74 68 65 0a 2a 2a 20 62  to hold the.** b
66440 69 6e 61 72 79 20 76 61 6c 75 65 20 68 61 73 20  inary value has 
66450 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 66 72  been obtained fr
66460 6f 6d 20 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d 75  om malloc and mu
66470 73 74 20 62 65 20 66 72 65 65 64 20 62 79 0a 2a  st be freed by.*
66480 2a 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f  * the calling ro
66490 75 74 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  utine..*/.SQLITE
664a0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
664b0 71 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f 62 28  qlite3HexToBlob(
664c0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a  const char *z){.
664d0 20 20 63 68 61 72 20 2a 7a 42 6c 6f 62 3b 0a 20    char *zBlob;. 
664e0 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 20   int i;.  int n 
664f0 3d 20 73 74 72 6c 65 6e 28 7a 29 3b 0a 20 20 69  = strlen(z);.  i
66500 66 28 20 6e 25 32 20 29 20 72 65 74 75 72 6e 20  f( n%2 ) return 
66510 30 3b 0a 0a 20 20 7a 42 6c 6f 62 20 3d 20 28 63  0;..  zBlob = (c
66520 68 61 72 20 2a 29 73 71 6c 69 74 65 4d 61 6c 6c  har *)sqliteMall
66530 6f 63 28 6e 2f 32 29 3b 0a 20 20 69 66 28 20 7a  oc(n/2);.  if( z
66540 42 6c 6f 62 20 29 7b 0a 20 20 20 20 66 6f 72 28  Blob ){.    for(
66550 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 3d 32 29 7b  i=0; i<n; i+=2){
66560 0a 20 20 20 20 20 20 7a 42 6c 6f 62 5b 69 2f 32  .      zBlob[i/2
66570 5d 20 3d 20 28 68 65 78 54 6f 49 6e 74 28 7a 5b  ] = (hexToInt(z[
66580 69 5d 29 3c 3c 34 29 20 7c 20 68 65 78 54 6f 49  i])<<4) | hexToI
66590 6e 74 28 7a 5b 69 2b 31 5d 29 3b 0a 20 20 20 20  nt(z[i+1]);.    
665a0 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a  }.  }.  return z
665b0 42 6c 6f 62 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  Blob;.}.#endif /
665c0 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42  * !SQLITE_OMIT_B
665d0 4c 4f 42 5f 4c 49 54 45 52 41 4c 20 7c 7c 20 53  LOB_LITERAL || S
665e0 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 20  QLITE_HAS_CODEC 
665f0 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67  */.../*.** Chang
66600 65 20 74 68 65 20 73 71 6c 69 74 65 2e 6d 61 67  e the sqlite.mag
66610 69 63 20 66 72 6f 6d 20 53 51 4c 49 54 45 5f 4d  ic from SQLITE_M
66620 41 47 49 43 5f 4f 50 45 4e 20 74 6f 20 53 51 4c  AGIC_OPEN to SQL
66630 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 2e 0a  ITE_MAGIC_BUSY..
66640 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72  ** Return an err
66650 6f 72 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66  or (non-zero) if
66660 20 74 68 65 20 6d 61 67 69 63 20 77 61 73 20 6e   the magic was n
66670 6f 74 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f  ot SQLITE_MAGIC_
66680 4f 50 45 4e 0a 2a 2a 20 77 68 65 6e 20 74 68 69  OPEN.** when thi
66690 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
666a0 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  led..**.** This 
666b0 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
666c0 64 20 77 68 65 6e 20 65 6e 74 65 72 69 6e 67 20  d when entering 
666d0 61 6e 20 53 51 4c 69 74 65 20 41 50 49 2e 20 20  an SQLite API.  
666e0 54 68 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43  The SQLITE_MAGIC
666f0 5f 4f 50 45 4e 0a 2a 2a 20 76 61 6c 75 65 20 69  _OPEN.** value i
66700 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 74 68  ndicates that th
66710 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
66720 63 74 69 6f 6e 20 70 61 73 73 65 64 20 69 6e 74  ction passed int
66730 6f 20 74 68 65 20 41 50 49 20 69 73 0a 2a 2a 20  o the API is.** 
66740 6f 70 65 6e 20 61 6e 64 20 69 73 20 6e 6f 74 20  open and is not 
66750 62 65 69 6e 67 20 75 73 65 64 20 62 79 20 61 6e  being used by an
66760 6f 74 68 65 72 20 74 68 72 65 61 64 2e 20 20 42  other thread.  B
66770 79 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20 76  y changing the v
66780 61 6c 75 65 0a 2a 2a 20 74 6f 20 53 51 4c 49 54  alue.** to SQLIT
66790 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 77 65 20  E_MAGIC_BUSY we 
667a0 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 74 68  indicate that th
667b0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20  e connection is 
667c0 69 6e 20 75 73 65 2e 0a 2a 2a 20 73 71 6c 69 74  in use..** sqlit
667d0 65 33 53 61 66 65 74 79 4f 66 66 28 29 20 62 65  e3SafetyOff() be
667e0 6c 6f 77 20 77 69 6c 6c 20 63 68 61 6e 67 65 20  low will change 
667f0 74 68 65 20 76 61 6c 75 65 20 62 61 63 6b 20 74  the value back t
66800 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f  o SQLITE_MAGIC_O
66810 50 45 4e 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20  PEN.** when the 
66820 41 50 49 20 65 78 69 74 73 2e 20 0a 2a 2a 0a 2a  API exits. .**.*
66830 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
66840 73 20 61 20 61 74 74 65 6d 70 74 20 74 6f 20 64  s a attempt to d
66850 65 74 65 63 74 20 69 66 20 74 77 6f 20 74 68 72  etect if two thr
66860 65 61 64 73 20 75 73 65 20 74 68 65 0a 2a 2a 20  eads use the.** 
66870 73 61 6d 65 20 73 71 6c 69 74 65 2a 20 70 6f 69  same sqlite* poi
66880 6e 74 65 72 20 61 74 20 74 68 65 20 73 61 6d 65  nter at the same
66890 20 74 69 6d 65 2e 20 20 54 68 65 72 65 20 69 73   time.  There is
668a0 20 61 20 72 61 63 65 20 0a 2a 2a 20 63 6f 6e 64   a race .** cond
668b0 69 74 69 6f 6e 20 73 6f 20 69 74 20 69 73 20 70  ition so it is p
668c0 6f 73 73 69 62 6c 65 20 74 68 61 74 20 74 68 65  ossible that the
668d0 20 65 72 72 6f 72 20 69 73 20 6e 6f 74 20 64 65   error is not de
668e0 74 65 63 74 65 64 2e 0a 2a 2a 20 42 75 74 20 75  tected..** But u
668f0 73 75 61 6c 6c 79 20 74 68 65 20 70 72 6f 62 6c  sually the probl
66900 65 6d 20 77 69 6c 6c 20 62 65 20 73 65 65 6e 2e  em will be seen.
66910 20 20 54 68 65 20 72 65 73 75 6c 74 20 77 69 6c    The result wil
66920 6c 20 62 65 20 61 6e 0a 2a 2a 20 65 72 72 6f 72  l be an.** error
66930 20 77 68 69 63 68 20 63 61 6e 20 62 65 20 75 73   which can be us
66940 65 64 20 74 6f 20 64 65 62 75 67 20 74 68 65 20  ed to debug the 
66950 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 68 61 74  application that
66960 20 69 73 0a 2a 2a 20 75 73 69 6e 67 20 53 51 4c   is.** using SQL
66970 69 74 65 20 69 6e 63 6f 72 72 65 63 74 6c 79 2e  ite incorrectly.
66980 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74 20 23 32  .**.** Ticket #2
66990 30 32 3a 20 20 49 66 20 64 62 2d 3e 6d 61 67 69  02:  If db->magi
669a0 63 20 69 73 20 6e 6f 74 20 61 20 76 61 6c 69 64  c is not a valid
669b0 20 6f 70 65 6e 20 76 61 6c 75 65 2c 20 74 61 6b   open value, tak
669c0 65 20 63 61 72 65 20 6e 6f 74 0a 2a 2a 20 74 6f  e care not.** to
669d0 20 6d 6f 64 69 66 79 20 74 68 65 20 64 62 20 73   modify the db s
669e0 74 72 75 63 74 75 72 65 20 61 74 20 61 6c 6c 2e  tructure at all.
669f0 20 20 49 74 20 63 6f 75 6c 64 20 62 65 20 74 68    It could be th
66a00 61 74 20 64 62 20 69 73 20 61 20 73 74 61 6c 65  at db is a stale
66a10 0a 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 20 49 6e  .** pointer.  In
66a20 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 74   other words, it
66a30 20 63 6f 75 6c 64 20 62 65 20 74 68 61 74 20 74   could be that t
66a40 68 65 72 65 20 68 61 73 20 62 65 65 6e 20 61 20  here has been a 
66a50 70 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c 20 74 6f  prior.** call to
66a60 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 64   sqlite3_close(d
66a70 62 29 20 61 6e 64 20 64 62 20 68 61 73 20 62 65  b) and db has be
66a80 65 6e 20 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20  en deallocated. 
66a90 20 41 6e 64 20 77 65 20 64 6f 0a 2a 2a 20 6e 6f   And we do.** no
66aa0 74 20 77 61 6e 74 20 74 6f 20 77 72 69 74 65 20  t want to write 
66ab0 69 6e 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 64  into deallocated
66ac0 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 53 51 4c 49   memory..*/.SQLI
66ad0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
66ae0 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 73  qlite3SafetyOn(s
66af0 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69  qlite3 *db){.  i
66b00 66 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51  f( db->magic==SQ
66b10 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20  LITE_MAGIC_OPEN 
66b20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 63  ){.    db->magic
66b30 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f   = SQLITE_MAGIC_
66b40 42 55 53 59 3b 0a 20 20 20 20 72 65 74 75 72 6e  BUSY;.    return
66b50 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   0;.  }else if( 
66b60 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54  db->magic==SQLIT
66b70 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 7b 0a  E_MAGIC_BUSY ){.
66b80 20 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20      db->magic = 
66b90 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 45 52 52  SQLITE_MAGIC_ERR
66ba0 4f 52 3b 0a 20 20 20 20 64 62 2d 3e 75 31 2e 69  OR;.    db->u1.i
66bb0 73 49 6e 74 65 72 72 75 70 74 65 64 20 3d 20 31  sInterrupted = 1
66bc0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31  ;.  }.  return 1
66bd0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67  ;.}../*.** Chang
66be0 65 20 74 68 65 20 6d 61 67 69 63 20 66 72 6f 6d  e the magic from
66bf0 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55   SQLITE_MAGIC_BU
66c00 53 59 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47  SY to SQLITE_MAG
66c10 49 43 5f 4f 50 45 4e 2e 0a 2a 2a 20 52 65 74 75  IC_OPEN..** Retu
66c20 72 6e 20 61 6e 20 65 72 72 6f 72 20 28 6e 6f 6e  rn an error (non
66c30 2d 7a 65 72 6f 29 20 69 66 20 74 68 65 20 6d 61  -zero) if the ma
66c40 67 69 63 20 77 61 73 20 6e 6f 74 20 53 51 4c 49  gic was not SQLI
66c50 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 0a 2a 2a  TE_MAGIC_BUSY.**
66c60 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   when this routi
66c70 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f  ne is called..*/
66c80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
66c90 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74  int sqlite3Safet
66ca0 79 4f 66 66 28 73 71 6c 69 74 65 33 20 2a 64 62  yOff(sqlite3 *db
66cb0 29 7b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 67  ){.  if( db->mag
66cc0 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43  ic==SQLITE_MAGIC
66cd0 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 64 62 2d  _BUSY ){.    db-
66ce0 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f  >magic = SQLITE_
66cf0 4d 41 47 49 43 5f 4f 50 45 4e 3b 0a 20 20 20 20  MAGIC_OPEN;.    
66d00 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73  return 0;.  }els
66d10 65 20 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69  e {.    db->magi
66d20 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43  c = SQLITE_MAGIC
66d30 5f 45 52 52 4f 52 3b 0a 20 20 20 20 64 62 2d 3e  _ERROR;.    db->
66d40 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64  u1.isInterrupted
66d50 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e   = 1;.    return
66d60 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a   1;.  }.}../*.**
66d70 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
66d80 72 20 74 6f 20 74 68 65 20 54 68 72 65 61 64 44  r to the ThreadD
66d90 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 77  ata associated w
66da0 69 74 68 20 74 68 65 20 63 61 6c 6c 69 6e 67 20  ith the calling 
66db0 74 68 72 65 61 64 2e 0a 2a 2f 0a 53 51 4c 49 54  thread..*/.SQLIT
66dc0 45 5f 50 52 49 56 41 54 45 20 54 68 72 65 61 64  E_PRIVATE Thread
66dd0 44 61 74 61 20 2a 73 71 6c 69 74 65 33 54 68 72  Data *sqlite3Thr
66de0 65 61 64 44 61 74 61 28 29 7b 0a 20 20 54 68 72  eadData(){.  Thr
66df0 65 61 64 44 61 74 61 20 2a 70 20 3d 20 28 54 68  eadData *p = (Th
66e00 72 65 61 64 44 61 74 61 2a 29 73 71 6c 69 74 65  readData*)sqlite
66e10 33 4f 73 54 68 72 65 61 64 53 70 65 63 69 66 69  3OsThreadSpecifi
66e20 63 44 61 74 61 28 31 29 3b 0a 20 20 69 66 28 20  cData(1);.  if( 
66e30 21 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  !p ){.    sqlite
66e40 33 46 61 69 6c 65 64 4d 61 6c 6c 6f 63 28 29 3b  3FailedMalloc();
66e50 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b  .  }.  return p;
66e60 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
66e70 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
66e80 65 20 54 68 72 65 61 64 44 61 74 61 20 61 73 73  e ThreadData ass
66e90 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
66ea0 20 63 61 6c 6c 69 6e 67 20 74 68 72 65 61 64 2e   calling thread.
66eb0 0a 2a 2a 20 49 66 20 6e 6f 20 54 68 72 65 61 64  .** If no Thread
66ec0 44 61 74 61 20 68 61 73 20 62 65 65 6e 20 61 6c  Data has been al
66ed0 6c 6f 63 61 74 65 64 20 74 6f 20 74 68 69 73 20  located to this 
66ee0 74 68 72 65 61 64 20 79 65 74 2c 20 72 65 74 75  thread yet, retu
66ef0 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20  rn a pointer.** 
66f00 74 6f 20 61 20 73 75 62 73 74 69 74 75 74 65 20  to a substitute 
66f10 54 68 72 65 61 64 44 61 74 61 20 73 74 72 75 63  ThreadData struc
66f20 74 75 72 65 20 74 68 61 74 20 69 73 20 61 6c 6c  ture that is all
66f30 20 7a 65 72 6f 73 2e 20 0a 2a 2f 0a 53 51 4c 49   zeros. .*/.SQLI
66f40 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
66f50 20 54 68 72 65 61 64 44 61 74 61 20 2a 73 71 6c   ThreadData *sql
66f60 69 74 65 33 54 68 72 65 61 64 44 61 74 61 52 65  ite3ThreadDataRe
66f70 61 64 4f 6e 6c 79 28 29 7b 0a 20 20 73 74 61 74  adOnly(){.  stat
66f80 69 63 20 63 6f 6e 73 74 20 54 68 72 65 61 64 44  ic const ThreadD
66f90 61 74 61 20 7a 65 72 6f 44 61 74 61 20 3d 20 7b  ata zeroData = {
66fa0 30 7d 3b 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69  0};  /* Initiali
66fb0 7a 65 72 20 74 6f 20 73 69 6c 65 6e 63 65 20 77  zer to silence w
66fc0 61 72 6e 69 6e 67 73 0a 20 20 20 20 20 20 20 20  arnings.        
66fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
66fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
66ff0 20 20 20 2a 2a 20 66 72 6f 6d 20 62 72 6f 6b 65     ** from broke
67000 6e 20 63 6f 6d 70 69 6c 65 72 73 20 2a 2f 0a 20  n compilers */. 
67010 20 63 6f 6e 73 74 20 54 68 72 65 61 64 44 61 74   const ThreadDat
67020 61 20 2a 70 54 64 20 3d 20 73 71 6c 69 74 65 33  a *pTd = sqlite3
67030 4f 73 54 68 72 65 61 64 53 70 65 63 69 66 69 63  OsThreadSpecific
67040 44 61 74 61 28 30 29 3b 0a 20 20 72 65 74 75 72  Data(0);.  retur
67050 6e 20 70 54 64 20 3f 20 70 54 64 20 3a 20 26 7a  n pTd ? pTd : &z
67060 65 72 6f 44 61 74 61 3b 0a 7d 0a 0a 2f 2a 0a 2a  eroData;.}../*.*
67070 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69  * Check to see i
67080 66 20 74 68 65 20 54 68 72 65 61 64 44 61 74 61  f the ThreadData
67090 20 66 6f 72 20 74 68 69 73 20 74 68 72 65 61 64   for this thread
670a0 20 69 73 20 61 6c 6c 20 7a 65 72 6f 2e 20 20 49   is all zero.  I
670b0 66 20 69 74 0a 2a 2a 20 69 73 2c 20 74 68 65 6e  f it.** is, then
670c0 20 64 65 61 6c 6c 6f 63 61 74 65 20 69 74 2e 20   deallocate it. 
670d0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
670e0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52  TE void sqlite3R
670f0 65 6c 65 61 73 65 54 68 72 65 61 64 44 61 74 61  eleaseThreadData
67100 28 29 7b 0a 20 20 73 71 6c 69 74 65 33 4f 73 54  (){.  sqlite3OsT
67110 68 72 65 61 64 53 70 65 63 69 66 69 63 44 61 74  hreadSpecificDat
67120 61 28 2d 31 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  a(-1);.}../*****
67130 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
67140 20 75 74 69 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   util.c ********
67150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
67160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
67170 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
67180 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
67190 66 69 6c 65 20 68 61 73 68 2e 63 20 2a 2a 2a 2a  file hash.c ****
671a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
671b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
671c0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
671d0 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 32  2001 September 2
671e0 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  2.**.** The auth
671f0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
67200 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
67210 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
67220 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
67230 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
67240 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
67250 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
67260 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
67270 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
67280 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
67290 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
672a0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
672b0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
672c0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
672d0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
672e0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
672f0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
67300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
67310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
67320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
67330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
67340 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20  .** This is the 
67350 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
67360 66 20 67 65 6e 65 72 69 63 20 68 61 73 68 2d 74  f generic hash-t
67370 61 62 6c 65 73 0a 2a 2a 20 75 73 65 64 20 69 6e  ables.** used in
67380 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24   SQLite..**.** $
67390 49 64 3a 20 68 61 73 68 2e 63 2c 76 20 31 2e 31  Id: hash.c,v 1.1
673a0 39 20 32 30 30 37 2f 30 33 2f 33 31 20 30 33 3a  9 2007/03/31 03:
673b0 35 39 3a 32 34 20 64 72 68 20 45 78 70 20 24 0a  59:24 drh Exp $.
673c0 2a 2f 0a 0a 2f 2a 20 54 75 72 6e 20 62 75 6c 6b  */../* Turn bulk
673d0 20 6d 65 6d 6f 72 79 20 69 6e 74 6f 20 61 20 68   memory into a h
673e0 61 73 68 20 74 61 62 6c 65 20 6f 62 6a 65 63 74  ash table object
673f0 20 62 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67   by initializing
67400 20 74 68 65 0a 2a 2a 20 66 69 65 6c 64 73 20 6f   the.** fields o
67410 66 20 74 68 65 20 48 61 73 68 20 73 74 72 75 63  f the Hash struc
67420 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 22 70 4e 65  ture..**.** "pNe
67430 77 22 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  w" is a pointer 
67440 74 6f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  to the hash tabl
67450 65 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 20  e that is to be 
67460 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20  initialized..** 
67470 6b 65 79 43 6c 61 73 73 20 69 73 20 6f 6e 65 20  keyClass is one 
67480 6f 66 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 73  of the constants
67490 20 53 51 4c 49 54 45 5f 48 41 53 48 5f 49 4e 54   SQLITE_HASH_INT
674a0 2c 20 53 51 4c 49 54 45 5f 48 41 53 48 5f 50 4f  , SQLITE_HASH_PO
674b0 49 4e 54 45 52 2c 0a 2a 2a 20 53 51 4c 49 54 45  INTER,.** SQLITE
674c0 5f 48 41 53 48 5f 42 49 4e 41 52 59 2c 20 6f 72  _HASH_BINARY, or
674d0 20 53 51 4c 49 54 45 5f 48 41 53 48 5f 53 54 52   SQLITE_HASH_STR
674e0 49 4e 47 2e 20 20 54 68 65 20 76 61 6c 75 65 20  ING.  The value 
674f0 6f 66 20 6b 65 79 43 6c 61 73 73 20 0a 2a 2a 20  of keyClass .** 
67500 64 65 74 65 72 6d 69 6e 65 73 20 77 68 61 74 20  determines what 
67510 6b 69 6e 64 20 6f 66 20 6b 65 79 20 74 68 65 20  kind of key the 
67520 68 61 73 68 20 74 61 62 6c 65 20 77 69 6c 6c 20  hash table will 
67530 75 73 65 2e 20 20 22 63 6f 70 79 4b 65 79 22 20  use.  "copyKey" 
67540 69 73 0a 2a 2a 20 74 72 75 65 20 69 66 20 74 68  is.** true if th
67550 65 20 68 61 73 68 20 74 61 62 6c 65 20 73 68 6f  e hash table sho
67560 75 6c 64 20 6d 61 6b 65 20 69 74 73 20 6f 77 6e  uld make its own
67570 20 70 72 69 76 61 74 65 20 63 6f 70 79 20 6f 66   private copy of
67580 20 6b 65 79 73 20 61 6e 64 0a 2a 2a 20 66 61 6c   keys and.** fal
67590 73 65 20 69 66 20 69 74 20 73 68 6f 75 6c 64 20  se if it should 
675a0 6a 75 73 74 20 75 73 65 20 74 68 65 20 73 75 70  just use the sup
675b0 70 6c 69 65 64 20 70 6f 69 6e 74 65 72 2e 20 20  plied pointer.  
675c0 43 6f 70 79 4b 65 79 20 6f 6e 6c 79 20 6d 61 6b  CopyKey only mak
675d0 65 73 0a 2a 2a 20 73 65 6e 73 65 20 66 6f 72 20  es.** sense for 
675e0 53 51 4c 49 54 45 5f 48 41 53 48 5f 53 54 52 49  SQLITE_HASH_STRI
675f0 4e 47 20 61 6e 64 20 53 51 4c 49 54 45 5f 48 41  NG and SQLITE_HA
67600 53 48 5f 42 49 4e 41 52 59 20 61 6e 64 20 69 73  SH_BINARY and is
67610 20 69 67 6e 6f 72 65 64 0a 2a 2a 20 66 6f 72 20   ignored.** for 
67620 6f 74 68 65 72 20 6b 65 79 20 63 6c 61 73 73 65  other key classe
67630 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
67640 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
67650 33 48 61 73 68 49 6e 69 74 28 48 61 73 68 20 2a  3HashInit(Hash *
67660 70 4e 65 77 2c 20 69 6e 74 20 6b 65 79 43 6c 61  pNew, int keyCla
67670 73 73 2c 20 69 6e 74 20 63 6f 70 79 4b 65 79 29  ss, int copyKey)
67680 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65 77  {.  assert( pNew
67690 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
676a0 20 6b 65 79 43 6c 61 73 73 3e 3d 53 51 4c 49 54   keyClass>=SQLIT
676b0 45 5f 48 41 53 48 5f 53 54 52 49 4e 47 20 26 26  E_HASH_STRING &&
676c0 20 6b 65 79 43 6c 61 73 73 3c 3d 53 51 4c 49 54   keyClass<=SQLIT
676d0 45 5f 48 41 53 48 5f 42 49 4e 41 52 59 20 29 3b  E_HASH_BINARY );
676e0 0a 20 20 70 4e 65 77 2d 3e 6b 65 79 43 6c 61 73  .  pNew->keyClas
676f0 73 20 3d 20 6b 65 79 43 6c 61 73 73 3b 0a 23 69  s = keyClass;.#i
67700 66 20 30 0a 20 20 69 66 28 20 6b 65 79 43 6c 61  f 0.  if( keyCla
67710 73 73 3d 3d 53 51 4c 49 54 45 5f 48 41 53 48 5f  ss==SQLITE_HASH_
67720 50 4f 49 4e 54 45 52 20 7c 7c 20 6b 65 79 43 6c  POINTER || keyCl
67730 61 73 73 3d 3d 53 51 4c 49 54 45 5f 48 41 53 48  ass==SQLITE_HASH
67740 5f 49 4e 54 20 29 20 63 6f 70 79 4b 65 79 20 3d  _INT ) copyKey =
67750 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 70 4e 65   0;.#endif.  pNe
67760 77 2d 3e 63 6f 70 79 4b 65 79 20 3d 20 63 6f 70  w->copyKey = cop
67770 79 4b 65 79 3b 0a 20 20 70 4e 65 77 2d 3e 66 69  yKey;.  pNew->fi
67780 72 73 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d  rst = 0;.  pNew-
67790 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 20 20 70 4e  >count = 0;.  pN
677a0 65 77 2d 3e 68 74 73 69 7a 65 20 3d 20 30 3b 0a  ew->htsize = 0;.
677b0 20 20 70 4e 65 77 2d 3e 68 74 20 3d 20 30 3b 0a    pNew->ht = 0;.
677c0 20 20 70 4e 65 77 2d 3e 78 4d 61 6c 6c 6f 63 20    pNew->xMalloc 
677d0 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 58  = sqlite3MallocX
677e0 3b 0a 20 20 70 4e 65 77 2d 3e 78 46 72 65 65 20  ;.  pNew->xFree 
677f0 3d 20 73 71 6c 69 74 65 33 46 72 65 65 58 3b 0a  = sqlite3FreeX;.
67800 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 6c 6c  }../* Remove all
67810 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 61 20   entries from a 
67820 68 61 73 68 20 74 61 62 6c 65 2e 20 20 52 65 63  hash table.  Rec
67830 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 2e  laim all memory.
67840 0a 2a 2a 20 43 61 6c 6c 20 74 68 69 73 20 72 6f  .** Call this ro
67850 75 74 69 6e 65 20 74 6f 20 64 65 6c 65 74 65 20  utine to delete 
67860 61 20 68 61 73 68 20 74 61 62 6c 65 20 6f 72 20  a hash table or 
67870 74 6f 20 72 65 73 65 74 20 61 20 68 61 73 68 20  to reset a hash 
67880 74 61 62 6c 65 0a 2a 2a 20 74 6f 20 74 68 65 20  table.** to the 
67890 65 6d 70 74 79 20 73 74 61 74 65 2e 0a 2a 2f 0a  empty state..*/.
678a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
678b0 6f 69 64 20 73 71 6c 69 74 65 33 48 61 73 68 43  oid sqlite3HashC
678c0 6c 65 61 72 28 48 61 73 68 20 2a 70 48 29 7b 0a  lear(Hash *pH){.
678d0 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d    HashElem *elem
678e0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72  ;         /* For
678f0 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 61 6c   looping over al
67900 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68  l elements of th
67910 65 20 74 61 62 6c 65 20 2a 2f 0a 0a 20 20 61 73  e table */..  as
67920 73 65 72 74 28 20 70 48 21 3d 30 20 29 3b 0a 20  sert( pH!=0 );. 
67930 20 65 6c 65 6d 20 3d 20 70 48 2d 3e 66 69 72 73   elem = pH->firs
67940 74 3b 0a 20 20 70 48 2d 3e 66 69 72 73 74 20 3d  t;.  pH->first =
67950 20 30 3b 0a 20 20 69 66 28 20 70 48 2d 3e 68 74   0;.  if( pH->ht
67960 20 29 20 70 48 2d 3e 78 46 72 65 65 28 70 48 2d   ) pH->xFree(pH-
67970 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68 74 20 3d  >ht);.  pH->ht =
67980 20 30 3b 0a 20 20 70 48 2d 3e 68 74 73 69 7a 65   0;.  pH->htsize
67990 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 65   = 0;.  while( e
679a0 6c 65 6d 20 29 7b 0a 20 20 20 20 48 61 73 68 45  lem ){.    HashE
679b0 6c 65 6d 20 2a 6e 65 78 74 5f 65 6c 65 6d 20 3d  lem *next_elem =
679c0 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20   elem->next;.   
679d0 20 69 66 28 20 70 48 2d 3e 63 6f 70 79 4b 65 79   if( pH->copyKey
679e0 20 26 26 20 65 6c 65 6d 2d 3e 70 4b 65 79 20 29   && elem->pKey )
679f0 7b 0a 20 20 20 20 20 20 70 48 2d 3e 78 46 72 65  {.      pH->xFre
67a00 65 28 65 6c 65 6d 2d 3e 70 4b 65 79 29 3b 0a 20  e(elem->pKey);. 
67a10 20 20 20 7d 0a 20 20 20 20 70 48 2d 3e 78 46 72     }.    pH->xFr
67a20 65 65 28 65 6c 65 6d 29 3b 0a 20 20 20 20 65 6c  ee(elem);.    el
67a30 65 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 3b 0a  em = next_elem;.
67a40 20 20 7d 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 20    }.  pH->count 
67a50 3d 20 30 3b 0a 7d 0a 0a 23 69 66 20 30 20 2f 2a  = 0;.}..#if 0 /*
67a60 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0a 2f 2a 0a   NOT USED */./*.
67a70 2a 2a 20 48 61 73 68 20 61 6e 64 20 63 6f 6d 70  ** Hash and comp
67a80 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 73  arison functions
67a90 20 77 68 65 6e 20 74 68 65 20 6d 6f 64 65 20 69   when the mode i
67aa0 73 20 53 51 4c 49 54 45 5f 48 41 53 48 5f 49 4e  s SQLITE_HASH_IN
67ab0 54 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  T.*/.static int 
67ac0 69 6e 74 48 61 73 68 28 63 6f 6e 73 74 20 76 6f  intHash(const vo
67ad0 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b  id *pKey, int nK
67ae0 65 79 29 7b 0a 20 20 72 65 74 75 72 6e 20 6e 4b  ey){.  return nK
67af0 65 79 20 5e 20 28 6e 4b 65 79 3c 3c 38 29 20 5e  ey ^ (nKey<<8) ^
67b00 20 28 6e 4b 65 79 3e 3e 38 29 3b 0a 7d 0a 73 74   (nKey>>8);.}.st
67b10 61 74 69 63 20 69 6e 74 20 69 6e 74 43 6f 6d 70  atic int intComp
67b20 61 72 65 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a  are(const void *
67b30 70 4b 65 79 31 2c 20 69 6e 74 20 6e 31 2c 20 63  pKey1, int n1, c
67b40 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 32  onst void *pKey2
67b50 2c 20 69 6e 74 20 6e 32 29 7b 0a 20 20 72 65 74  , int n2){.  ret
67b60 75 72 6e 20 6e 32 20 2d 20 6e 31 3b 0a 7d 0a 23  urn n2 - n1;.}.#
67b70 65 6e 64 69 66 0a 0a 23 69 66 20 30 20 2f 2a 20  endif..#if 0 /* 
67b80 4e 4f 54 20 55 53 45 44 20 2a 2f 0a 2f 2a 0a 2a  NOT USED */./*.*
67b90 2a 20 48 61 73 68 20 61 6e 64 20 63 6f 6d 70 61  * Hash and compa
67ba0 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20  rison functions 
67bb0 77 68 65 6e 20 74 68 65 20 6d 6f 64 65 20 69 73  when the mode is
67bc0 20 53 51 4c 49 54 45 5f 48 41 53 48 5f 50 4f 49   SQLITE_HASH_POI
67bd0 4e 54 45 52 0a 2a 2f 0a 73 74 61 74 69 63 20 69  NTER.*/.static i
67be0 6e 74 20 70 74 72 48 61 73 68 28 63 6f 6e 73 74  nt ptrHash(const
67bf0 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74   void *pKey, int
67c00 20 6e 4b 65 79 29 7b 0a 20 20 75 70 74 72 20 78   nKey){.  uptr x
67c10 20 3d 20 41 64 64 72 28 70 4b 65 79 29 3b 0a 20   = Addr(pKey);. 
67c20 20 72 65 74 75 72 6e 20 78 20 5e 20 28 78 3c 3c   return x ^ (x<<
67c30 38 29 20 5e 20 28 78 3e 3e 38 29 3b 0a 7d 0a 73  8) ^ (x>>8);.}.s
67c40 74 61 74 69 63 20 69 6e 74 20 70 74 72 43 6f 6d  tatic int ptrCom
67c50 70 61 72 65 28 63 6f 6e 73 74 20 76 6f 69 64 20  pare(const void 
67c60 2a 70 4b 65 79 31 2c 20 69 6e 74 20 6e 31 2c 20  *pKey1, int n1, 
67c70 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
67c80 32 2c 20 69 6e 74 20 6e 32 29 7b 0a 20 20 69 66  2, int n2){.  if
67c90 28 20 70 4b 65 79 31 3d 3d 70 4b 65 79 32 20 29  ( pKey1==pKey2 )
67ca0 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28   return 0;.  if(
67cb0 20 70 4b 65 79 31 3c 70 4b 65 79 32 20 29 20 72   pKey1<pKey2 ) r
67cc0 65 74 75 72 6e 20 2d 31 3b 0a 20 20 72 65 74 75  eturn -1;.  retu
67cd0 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  rn 1;.}.#endif..
67ce0 2f 2a 0a 2a 2a 20 48 61 73 68 20 61 6e 64 20 63  /*.** Hash and c
67cf0 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69  omparison functi
67d00 6f 6e 73 20 77 68 65 6e 20 74 68 65 20 6d 6f 64  ons when the mod
67d10 65 20 69 73 20 53 51 4c 49 54 45 5f 48 41 53 48  e is SQLITE_HASH
67d20 5f 53 54 52 49 4e 47 0a 2a 2f 0a 73 74 61 74 69  _STRING.*/.stati
67d30 63 20 69 6e 74 20 73 74 72 48 61 73 68 28 63 6f  c int strHash(co
67d40 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20  nst void *pKey, 
67d50 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 63 6f 6e  int nKey){.  con
67d60 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 28 63 6f  st char *z = (co
67d70 6e 73 74 20 63 68 61 72 20 2a 29 70 4b 65 79 3b  nst char *)pKey;
67d80 0a 20 20 69 6e 74 20 68 20 3d 20 30 3b 0a 20 20  .  int h = 0;.  
67d90 69 66 28 20 6e 4b 65 79 3c 3d 30 20 29 20 6e 4b  if( nKey<=0 ) nK
67da0 65 79 20 3d 20 73 74 72 6c 65 6e 28 7a 29 3b 0a  ey = strlen(z);.
67db0 20 20 77 68 69 6c 65 28 20 6e 4b 65 79 20 3e 20    while( nKey > 
67dc0 30 20 20 29 7b 0a 20 20 20 20 68 20 3d 20 28 68  0  ){.    h = (h
67dd0 3c 3c 33 29 20 5e 20 68 20 5e 20 73 71 6c 69 74  <<3) ^ h ^ sqlit
67de0 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28  e3UpperToLower[(
67df0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 2a 7a  unsigned char)*z
67e00 2b 2b 5d 3b 0a 20 20 20 20 6e 4b 65 79 2d 2d 3b  ++];.    nKey--;
67e10 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 20  .  }.  return h 
67e20 26 20 30 78 37 66 66 66 66 66 66 66 3b 0a 7d 0a  & 0x7fffffff;.}.
67e30 73 74 61 74 69 63 20 69 6e 74 20 73 74 72 43 6f  static int strCo
67e40 6d 70 61 72 65 28 63 6f 6e 73 74 20 76 6f 69 64  mpare(const void
67e50 20 2a 70 4b 65 79 31 2c 20 69 6e 74 20 6e 31 2c   *pKey1, int n1,
67e60 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65   const void *pKe
67e70 79 32 2c 20 69 6e 74 20 6e 32 29 7b 0a 20 20 69  y2, int n2){.  i
67e80 66 28 20 6e 31 21 3d 6e 32 20 29 20 72 65 74 75  f( n1!=n2 ) retu
67e90 72 6e 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 73  rn 1;.  return s
67ea0 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 28  qlite3StrNICmp((
67eb0 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 4b 65 79  const char*)pKey
67ec0 31 2c 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70  1,(const char*)p
67ed0 4b 65 79 32 2c 6e 31 29 3b 0a 7d 0a 0a 2f 2a 0a  Key2,n1);.}../*.
67ee0 2a 2a 20 48 61 73 68 20 61 6e 64 20 63 6f 6d 70  ** Hash and comp
67ef0 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 73  arison functions
67f00 20 77 68 65 6e 20 74 68 65 20 6d 6f 64 65 20 69   when the mode i
67f10 73 20 53 51 4c 49 54 45 5f 48 41 53 48 5f 42 49  s SQLITE_HASH_BI
67f20 4e 41 52 59 0a 2a 2f 0a 73 74 61 74 69 63 20 69  NARY.*/.static i
67f30 6e 74 20 62 69 6e 48 61 73 68 28 63 6f 6e 73 74  nt binHash(const
67f40 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74   void *pKey, int
67f50 20 6e 4b 65 79 29 7b 0a 20 20 69 6e 74 20 68 20   nKey){.  int h 
67f60 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  = 0;.  const cha
67f70 72 20 2a 7a 20 3d 20 28 63 6f 6e 73 74 20 63 68  r *z = (const ch
67f80 61 72 20 2a 29 70 4b 65 79 3b 0a 20 20 77 68 69  ar *)pKey;.  whi
67f90 6c 65 28 20 6e 4b 65 79 2d 2d 20 3e 20 30 20 29  le( nKey-- > 0 )
67fa0 7b 0a 20 20 20 20 68 20 3d 20 28 68 3c 3c 33 29  {.    h = (h<<3)
67fb0 20 5e 20 68 20 5e 20 2a 28 7a 2b 2b 29 3b 0a 20   ^ h ^ *(z++);. 
67fc0 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 20 26 20   }.  return h & 
67fd0 30 78 37 66 66 66 66 66 66 66 3b 0a 7d 0a 73 74  0x7fffffff;.}.st
67fe0 61 74 69 63 20 69 6e 74 20 62 69 6e 43 6f 6d 70  atic int binComp
67ff0 61 72 65 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a  are(const void *
68000 70 4b 65 79 31 2c 20 69 6e 74 20 6e 31 2c 20 63  pKey1, int n1, c
68010 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 32  onst void *pKey2
68020 2c 20 69 6e 74 20 6e 32 29 7b 0a 20 20 69 66 28  , int n2){.  if(
68030 20 6e 31 21 3d 6e 32 20 29 20 72 65 74 75 72 6e   n1!=n2 ) return
68040 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 6d 65 6d   1;.  return mem
68050 63 6d 70 28 70 4b 65 79 31 2c 70 4b 65 79 32 2c  cmp(pKey1,pKey2,
68060 6e 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  n1);.}../*.** Re
68070 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
68080 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  o the appropriat
68090 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f 6e 20  e hash function 
680a0 67 69 76 65 6e 20 74 68 65 20 6b 65 79 20 63 6c  given the key cl
680b0 61 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43  ass..**.** The C
680c0 20 73 79 6e 74 61 78 20 69 6e 20 74 68 69 73 20   syntax in this 
680d0 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74  function definit
680e0 69 6f 6e 20 6d 61 79 20 62 65 20 75 6e 66 61 6d  ion may be unfam
680f0 69 6c 61 72 20 74 6f 20 73 6f 6d 65 20 0a 2a 2a  ilar to some .**
68100 20 70 72 6f 67 72 61 6d 6d 65 72 73 2c 20 73 6f   programmers, so
68110 20 77 65 20 70 72 6f 76 69 64 65 20 74 68 65 20   we provide the 
68120 66 6f 6c 6c 6f 77 69 6e 67 20 61 64 64 69 74 69  following additi
68130 6f 6e 61 6c 20 65 78 70 6c 61 6e 61 74 69 6f 6e  onal explanation
68140 3a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65  :.**.** The name
68150 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   of the function
68160 20 69 73 20 22 68 61 73 68 46 75 6e 63 74 69 6f   is "hashFunctio
68170 6e 22 2e 20 20 54 68 65 20 66 75 6e 63 74 69 6f  n".  The functio
68180 6e 20 74 61 6b 65 73 20 61 0a 2a 2a 20 73 69 6e  n takes a.** sin
68190 67 6c 65 20 70 61 72 61 6d 65 74 65 72 20 22 6b  gle parameter "k
681a0 65 79 43 6c 61 73 73 22 2e 20 20 54 68 65 20 72  eyClass".  The r
681b0 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 68  eturn value of h
681c0 61 73 68 46 75 6e 63 74 69 6f 6e 28 29 0a 2a 2a  ashFunction().**
681d0 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
681e0 20 61 6e 6f 74 68 65 72 20 66 75 6e 63 74 69 6f   another functio
681f0 6e 2e 20 20 53 70 65 63 69 66 69 63 61 6c 6c 79  n.  Specifically
68200 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  , the return val
68210 75 65 0a 2a 2a 20 6f 66 20 68 61 73 68 46 75 6e  ue.** of hashFun
68220 63 74 69 6f 6e 28 29 20 69 73 20 61 20 70 6f 69  ction() is a poi
68230 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69  nter to a functi
68240 6f 6e 20 74 68 61 74 20 74 61 6b 65 73 20 74 77  on that takes tw
68250 6f 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20  o parameters.** 
68260 77 69 74 68 20 74 79 70 65 73 20 22 63 6f 6e 73  with types "cons
68270 74 20 76 6f 69 64 2a 22 20 61 6e 64 20 22 69 6e  t void*" and "in
68280 74 22 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61  t" and returns a
68290 6e 20 22 69 6e 74 22 2e 0a 2a 2f 0a 73 74 61 74  n "int"..*/.stat
682a0 69 63 20 69 6e 74 20 28 2a 68 61 73 68 46 75 6e  ic int (*hashFun
682b0 63 74 69 6f 6e 28 69 6e 74 20 6b 65 79 43 6c 61  ction(int keyCla
682c0 73 73 29 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a  ss))(const void*
682d0 2c 69 6e 74 29 7b 0a 23 69 66 20 30 20 20 2f 2a  ,int){.#if 0  /*
682e0 20 48 41 53 48 5f 49 4e 54 20 61 6e 64 20 48 41   HASH_INT and HA
682f0 53 48 5f 50 4f 49 4e 54 45 52 20 61 72 65 20 6e  SH_POINTER are n
68300 65 76 65 72 20 75 73 65 64 20 2a 2f 0a 20 20 73  ever used */.  s
68310 77 69 74 63 68 28 20 6b 65 79 43 6c 61 73 73 20  witch( keyClass 
68320 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  ){.    case SQLI
68330 54 45 5f 48 41 53 48 5f 49 4e 54 3a 20 20 20 20  TE_HASH_INT:    
68340 20 72 65 74 75 72 6e 20 26 69 6e 74 48 61 73 68   return &intHash
68350 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
68360 45 5f 48 41 53 48 5f 50 4f 49 4e 54 45 52 3a 20  E_HASH_POINTER: 
68370 72 65 74 75 72 6e 20 26 70 74 72 48 61 73 68 3b  return &ptrHash;
68380 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
68390 5f 48 41 53 48 5f 53 54 52 49 4e 47 3a 20 20 72  _HASH_STRING:  r
683a0 65 74 75 72 6e 20 26 73 74 72 48 61 73 68 3b 0a  eturn &strHash;.
683b0 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
683c0 48 41 53 48 5f 42 49 4e 41 52 59 3a 20 20 72 65  HASH_BINARY:  re
683d0 74 75 72 6e 20 26 62 69 6e 48 61 73 68 3b 3b 0a  turn &binHash;;.
683e0 20 20 20 20 64 65 66 61 75 6c 74 3a 20 62 72 65      default: bre
683f0 61 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ak;.  }.  return
68400 20 30 3b 0a 23 65 6c 73 65 0a 20 20 69 66 28 20   0;.#else.  if( 
68410 6b 65 79 43 6c 61 73 73 3d 3d 53 51 4c 49 54 45  keyClass==SQLITE
68420 5f 48 41 53 48 5f 53 54 52 49 4e 47 20 29 7b 0a  _HASH_STRING ){.
68430 20 20 20 20 72 65 74 75 72 6e 20 26 73 74 72 48      return &strH
68440 61 73 68 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ash;.  }else{.  
68450 20 20 61 73 73 65 72 74 28 20 6b 65 79 43 6c 61    assert( keyCla
68460 73 73 3d 3d 53 51 4c 49 54 45 5f 48 41 53 48 5f  ss==SQLITE_HASH_
68470 42 49 4e 41 52 59 20 29 3b 0a 20 20 20 20 72 65  BINARY );.    re
68480 74 75 72 6e 20 26 62 69 6e 48 61 73 68 3b 0a 20  turn &binHash;. 
68490 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a   }.#endif.}../*.
684a0 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  ** Return a poin
684b0 74 65 72 20 74 6f 20 74 68 65 20 61 70 70 72 6f  ter to the appro
684c0 70 72 69 61 74 65 20 68 61 73 68 20 66 75 6e 63  priate hash func
684d0 74 69 6f 6e 20 67 69 76 65 6e 20 74 68 65 20 6b  tion given the k
684e0 65 79 20 63 6c 61 73 73 2e 0a 2a 2a 0a 2a 2a 20  ey class..**.** 
684f0 46 6f 72 20 68 65 6c 70 20 69 6e 20 69 6e 74 65  For help in inte
68500 72 70 72 65 74 65 64 20 74 68 65 20 6f 62 73 63  rpreted the obsc
68510 75 72 65 20 43 20 63 6f 64 65 20 69 6e 20 74 68  ure C code in th
68520 65 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e  e function defin
68530 69 74 69 6f 6e 2c 0a 2a 2a 20 73 65 65 20 74 68  ition,.** see th
68540 65 20 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74  e header comment
68550 20 6f 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73   on the previous
68560 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74   function..*/.st
68570 61 74 69 63 20 69 6e 74 20 28 2a 63 6f 6d 70 61  atic int (*compa
68580 72 65 46 75 6e 63 74 69 6f 6e 28 69 6e 74 20 6b  reFunction(int k
68590 65 79 43 6c 61 73 73 29 29 28 63 6f 6e 73 74 20  eyClass))(const 
685a0 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
685b0 76 6f 69 64 2a 2c 69 6e 74 29 7b 0a 23 69 66 20  void*,int){.#if 
685c0 30 20 2f 2a 20 48 41 53 48 5f 49 4e 54 20 61 6e  0 /* HASH_INT an
685d0 64 20 48 41 53 48 5f 50 4f 49 4e 54 45 52 20 61  d HASH_POINTER a
685e0 72 65 20 6e 65 76 65 72 20 75 73 65 64 20 2a 2f  re never used */
685f0 0a 20 20 73 77 69 74 63 68 28 20 6b 65 79 43 6c  .  switch( keyCl
68600 61 73 73 20 29 7b 0a 20 20 20 20 63 61 73 65 20  ass ){.    case 
68610 53 51 4c 49 54 45 5f 48 41 53 48 5f 49 4e 54 3a  SQLITE_HASH_INT:
68620 20 20 20 20 20 72 65 74 75 72 6e 20 26 69 6e 74       return &int
68630 43 6f 6d 70 61 72 65 3b 0a 20 20 20 20 63 61 73  Compare;.    cas
68640 65 20 53 51 4c 49 54 45 5f 48 41 53 48 5f 50 4f  e SQLITE_HASH_PO
68650 49 4e 54 45 52 3a 20 72 65 74 75 72 6e 20 26 70  INTER: return &p
68660 74 72 43 6f 6d 70 61 72 65 3b 0a 20 20 20 20 63  trCompare;.    c
68670 61 73 65 20 53 51 4c 49 54 45 5f 48 41 53 48 5f  ase SQLITE_HASH_
68680 53 54 52 49 4e 47 3a 20 20 72 65 74 75 72 6e 20  STRING:  return 
68690 26 73 74 72 43 6f 6d 70 61 72 65 3b 0a 20 20 20  &strCompare;.   
686a0 20 63 61 73 65 20 53 51 4c 49 54 45 5f 48 41 53   case SQLITE_HAS
686b0 48 5f 42 49 4e 41 52 59 3a 20 20 72 65 74 75 72  H_BINARY:  retur
686c0 6e 20 26 62 69 6e 43 6f 6d 70 61 72 65 3b 0a 20  n &binCompare;. 
686d0 20 20 20 64 65 66 61 75 6c 74 3a 20 62 72 65 61     default: brea
686e0 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  k;.  }.  return 
686f0 30 3b 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 6b  0;.#else.  if( k
68700 65 79 43 6c 61 73 73 3d 3d 53 51 4c 49 54 45 5f  eyClass==SQLITE_
68710 48 41 53 48 5f 53 54 52 49 4e 47 20 29 7b 0a 20  HASH_STRING ){. 
68720 20 20 20 72 65 74 75 72 6e 20 26 73 74 72 43 6f     return &strCo
68730 6d 70 61 72 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a  mpare;.  }else{.
68740 20 20 20 20 61 73 73 65 72 74 28 20 6b 65 79 43      assert( keyC
68750 6c 61 73 73 3d 3d 53 51 4c 49 54 45 5f 48 41 53  lass==SQLITE_HAS
68760 48 5f 42 49 4e 41 52 59 20 29 3b 0a 20 20 20 20  H_BINARY );.    
68770 72 65 74 75 72 6e 20 26 62 69 6e 43 6f 6d 70 61  return &binCompa
68780 72 65 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d  re;.  }.#endif.}
68790 0a 0a 2f 2a 20 4c 69 6e 6b 20 61 6e 20 65 6c 65  ../* Link an ele
687a0 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 68 61  ment into the ha
687b0 73 68 20 74 61 62 6c 65 0a 2a 2f 0a 73 74 61 74  sh table.*/.stat
687c0 69 63 20 76 6f 69 64 20 69 6e 73 65 72 74 45 6c  ic void insertEl
687d0 65 6d 65 6e 74 28 0a 20 20 48 61 73 68 20 2a 70  ement(.  Hash *p
687e0 48 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  H,              
687f0 2f 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20  /* The complete 
68800 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20  hash table */.  
68810 73 74 72 75 63 74 20 5f 68 74 20 2a 70 45 6e 74  struct _ht *pEnt
68820 72 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 65 6e  ry,    /* The en
68830 74 72 79 20 69 6e 74 6f 20 77 68 69 63 68 20 70  try into which p
68840 4e 65 77 20 69 73 20 69 6e 73 65 72 74 65 64 20  New is inserted 
68850 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 70  */.  HashElem *p
68860 4e 65 77 20 20 20 20 20 20 20 20 20 2f 2a 20 54  New         /* T
68870 68 65 20 65 6c 65 6d 65 6e 74 20 74 6f 20 62 65  he element to be
68880 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 29 7b 0a   inserted */.){.
68890 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 48 65 61    HashElem *pHea
688a0 64 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73  d;       /* Firs
688b0 74 20 65 6c 65 6d 65 6e 74 20 61 6c 72 65 61 64  t element alread
688c0 79 20 69 6e 20 70 45 6e 74 72 79 20 2a 2f 0a 20  y in pEntry */. 
688d0 20 70 48 65 61 64 20 3d 20 70 45 6e 74 72 79 2d   pHead = pEntry-
688e0 3e 63 68 61 69 6e 3b 0a 20 20 69 66 28 20 70 48  >chain;.  if( pH
688f0 65 61 64 20 29 7b 0a 20 20 20 20 70 4e 65 77 2d  ead ){.    pNew-
68900 3e 6e 65 78 74 20 3d 20 70 48 65 61 64 3b 0a 20  >next = pHead;. 
68910 20 20 20 70 4e 65 77 2d 3e 70 72 65 76 20 3d 20     pNew->prev = 
68920 70 48 65 61 64 2d 3e 70 72 65 76 3b 0a 20 20 20  pHead->prev;.   
68930 20 69 66 28 20 70 48 65 61 64 2d 3e 70 72 65 76   if( pHead->prev
68940 20 29 7b 20 70 48 65 61 64 2d 3e 70 72 65 76 2d   ){ pHead->prev-
68950 3e 6e 65 78 74 20 3d 20 70 4e 65 77 3b 20 7d 0a  >next = pNew; }.
68960 20 20 20 20 65 6c 73 65 20 20 20 20 20 20 20 20      else        
68970 20 20 20 20 20 7b 20 70 48 2d 3e 66 69 72 73 74       { pH->first
68980 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20 70   = pNew; }.    p
68990 48 65 61 64 2d 3e 70 72 65 76 20 3d 20 70 4e 65  Head->prev = pNe
689a0 77 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  w;.  }else{.    
689b0 70 4e 65 77 2d 3e 6e 65 78 74 20 3d 20 70 48 2d  pNew->next = pH-
689c0 3e 66 69 72 73 74 3b 0a 20 20 20 20 69 66 28 20  >first;.    if( 
689d0 70 48 2d 3e 66 69 72 73 74 20 29 7b 20 70 48 2d  pH->first ){ pH-
689e0 3e 66 69 72 73 74 2d 3e 70 72 65 76 20 3d 20 70  >first->prev = p
689f0 4e 65 77 3b 20 7d 0a 20 20 20 20 70 4e 65 77 2d  New; }.    pNew-
68a00 3e 70 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 70  >prev = 0;.    p
68a10 48 2d 3e 66 69 72 73 74 20 3d 20 70 4e 65 77 3b  H->first = pNew;
68a20 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 2d 3e 63  .  }.  pEntry->c
68a30 6f 75 6e 74 2b 2b 3b 0a 20 20 70 45 6e 74 72 79  ount++;.  pEntry
68a40 2d 3e 63 68 61 69 6e 20 3d 20 70 4e 65 77 3b 0a  ->chain = pNew;.
68a50 7d 0a 0a 0a 2f 2a 20 52 65 73 69 7a 65 20 74 68  }.../* Resize th
68a60 65 20 68 61 73 68 20 74 61 62 6c 65 20 73 6f 20  e hash table so 
68a70 74 68 61 74 20 69 74 20 63 61 6e 74 61 69 6e 73  that it cantains
68a80 20 22 6e 65 77 5f 73 69 7a 65 22 20 62 75 63 6b   "new_size" buck
68a90 65 74 73 2e 0a 2a 2a 20 22 6e 65 77 5f 73 69 7a  ets..** "new_siz
68aa0 65 22 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77  e" must be a pow
68ab0 65 72 20 6f 66 20 32 2e 20 20 54 68 65 20 68 61  er of 2.  The ha
68ac0 73 68 20 74 61 62 6c 65 20 6d 69 67 68 74 20 66  sh table might f
68ad0 61 69 6c 20 0a 2a 2a 20 74 6f 20 72 65 73 69 7a  ail .** to resiz
68ae0 65 20 69 66 20 73 71 6c 69 74 65 4d 61 6c 6c 6f  e if sqliteMallo
68af0 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74  c() fails..*/.st
68b00 61 74 69 63 20 76 6f 69 64 20 72 65 68 61 73 68  atic void rehash
68b10 28 48 61 73 68 20 2a 70 48 2c 20 69 6e 74 20 6e  (Hash *pH, int n
68b20 65 77 5f 73 69 7a 65 29 7b 0a 20 20 73 74 72 75  ew_size){.  stru
68b30 63 74 20 5f 68 74 20 2a 6e 65 77 5f 68 74 3b 20  ct _ht *new_ht; 
68b40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
68b50 65 20 6e 65 77 20 68 61 73 68 20 74 61 62 6c 65  e new hash table
68b60 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a   */.  HashElem *
68b70 65 6c 65 6d 2c 20 2a 6e 65 78 74 5f 65 6c 65 6d  elem, *next_elem
68b80 3b 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70  ;    /* For loop
68b90 69 6e 67 20 6f 76 65 72 20 65 78 69 73 74 69 6e  ing over existin
68ba0 67 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20  g elements */.  
68bb0 69 6e 74 20 28 2a 78 48 61 73 68 29 28 63 6f 6e  int (*xHash)(con
68bc0 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 20 2f  st void*,int); /
68bd0 2a 20 54 68 65 20 68 61 73 68 20 66 75 6e 63 74  * The hash funct
68be0 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  ion */..  assert
68bf0 28 20 28 6e 65 77 5f 73 69 7a 65 20 26 20 28 6e  ( (new_size & (n
68c00 65 77 5f 73 69 7a 65 2d 31 29 29 3d 3d 30 20 29  ew_size-1))==0 )
68c10 3b 0a 20 20 6e 65 77 5f 68 74 20 3d 20 28 73 74  ;.  new_ht = (st
68c20 72 75 63 74 20 5f 68 74 20 2a 29 70 48 2d 3e 78  ruct _ht *)pH->x
68c30 4d 61 6c 6c 6f 63 28 20 6e 65 77 5f 73 69 7a 65  Malloc( new_size
68c40 2a 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f  *sizeof(struct _
68c50 68 74 29 20 29 3b 0a 20 20 69 66 28 20 6e 65 77  ht) );.  if( new
68c60 5f 68 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  _ht==0 ) return;
68c70 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 20 29 20  .  if( pH->ht ) 
68c80 70 48 2d 3e 78 46 72 65 65 28 70 48 2d 3e 68 74  pH->xFree(pH->ht
68c90 29 3b 0a 20 20 70 48 2d 3e 68 74 20 3d 20 6e 65  );.  pH->ht = ne
68ca0 77 5f 68 74 3b 0a 20 20 70 48 2d 3e 68 74 73 69  w_ht;.  pH->htsi
68cb0 7a 65 20 3d 20 6e 65 77 5f 73 69 7a 65 3b 0a 20  ze = new_size;. 
68cc0 20 78 48 61 73 68 20 3d 20 68 61 73 68 46 75 6e   xHash = hashFun
68cd0 63 74 69 6f 6e 28 70 48 2d 3e 6b 65 79 43 6c 61  ction(pH->keyCla
68ce0 73 73 29 3b 0a 20 20 66 6f 72 28 65 6c 65 6d 3d  ss);.  for(elem=
68cf0 70 48 2d 3e 66 69 72 73 74 2c 20 70 48 2d 3e 66  pH->first, pH->f
68d00 69 72 73 74 3d 30 3b 20 65 6c 65 6d 3b 20 65 6c  irst=0; elem; el
68d10 65 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 29 7b  em = next_elem){
68d20 0a 20 20 20 20 69 6e 74 20 68 20 3d 20 28 2a 78  .    int h = (*x
68d30 48 61 73 68 29 28 65 6c 65 6d 2d 3e 70 4b 65 79  Hash)(elem->pKey
68d40 2c 20 65 6c 65 6d 2d 3e 6e 4b 65 79 29 20 26 20  , elem->nKey) & 
68d50 28 6e 65 77 5f 73 69 7a 65 2d 31 29 3b 0a 20 20  (new_size-1);.  
68d60 20 20 6e 65 78 74 5f 65 6c 65 6d 20 3d 20 65 6c    next_elem = el
68d70 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 69 6e  em->next;.    in
68d80 73 65 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20  sertElement(pH, 
68d90 26 6e 65 77 5f 68 74 5b 68 5d 2c 20 65 6c 65 6d  &new_ht[h], elem
68da0 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 54 68 69  );.  }.}../* Thi
68db0 73 20 66 75 6e 63 74 69 6f 6e 20 28 66 6f 72 20  s function (for 
68dc0 69 6e 74 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c  internal use onl
68dd0 79 29 20 6c 6f 63 61 74 65 73 20 61 6e 20 65 6c  y) locates an el
68de0 65 6d 65 6e 74 20 69 6e 20 61 6e 0a 2a 2a 20 68  ement in an.** h
68df0 61 73 68 20 74 61 62 6c 65 20 74 68 61 74 20 6d  ash table that m
68e00 61 74 63 68 65 73 20 74 68 65 20 67 69 76 65 6e  atches the given
68e10 20 6b 65 79 2e 20 20 54 68 65 20 68 61 73 68 20   key.  The hash 
68e20 66 6f 72 20 74 68 69 73 20 6b 65 79 20 68 61 73  for this key has
68e30 0a 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e  .** already been
68e40 20 63 6f 6d 70 75 74 65 64 20 61 6e 64 20 69 73   computed and is
68e50 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 34   passed as the 4
68e60 74 68 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f  th parameter..*/
68e70 0a 73 74 61 74 69 63 20 48 61 73 68 45 6c 65 6d  .static HashElem
68e80 20 2a 66 69 6e 64 45 6c 65 6d 65 6e 74 47 69 76   *findElementGiv
68e90 65 6e 48 61 73 68 28 0a 20 20 63 6f 6e 73 74 20  enHash(.  const 
68ea0 48 61 73 68 20 2a 70 48 2c 20 20 20 20 20 2f 2a  Hash *pH,     /*
68eb0 20 54 68 65 20 70 48 20 74 6f 20 62 65 20 73 65   The pH to be se
68ec0 61 72 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73  arched */.  cons
68ed0 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20  t void *pKey,   
68ee0 2f 2a 20 54 68 65 20 6b 65 79 20 77 65 20 61 72  /* The key we ar
68ef0 65 20 73 65 61 72 63 68 69 6e 67 20 66 6f 72 20  e searching for 
68f00 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 0a 20  */.  int nKey,. 
68f10 20 69 6e 74 20 68 20 20 20 20 20 20 20 20 20 20   int h          
68f20 20 20 20 20 20 2f 2a 20 54 68 65 20 68 61 73 68       /* The hash
68f30 20 66 6f 72 20 74 68 69 73 20 6b 65 79 2e 20 2a   for this key. *
68f40 2f 0a 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20  /.){.  HashElem 
68f50 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20 20 20 20  *elem;          
68f60 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f        /* Used to
68f70 20 6c 6f 6f 70 20 74 68 72 75 20 74 68 65 20 65   loop thru the e
68f80 6c 65 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20  lement list */. 
68f90 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 20   int count;     
68fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68fb0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65  /* Number of ele
68fc0 6d 65 6e 74 73 20 6c 65 66 74 20 74 6f 20 74 65  ments left to te
68fd0 73 74 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43  st */.  int (*xC
68fe0 6f 6d 70 61 72 65 29 28 63 6f 6e 73 74 20 76 6f  ompare)(const vo
68ff0 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
69000 69 64 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 63 6f  id*,int);  /* co
69010 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f  mparison functio
69020 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 70 48 2d 3e  n */..  if( pH->
69030 68 74 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74  ht ){.    struct
69040 20 5f 68 74 20 2a 70 45 6e 74 72 79 20 3d 20 26   _ht *pEntry = &
69050 70 48 2d 3e 68 74 5b 68 5d 3b 0a 20 20 20 20 65  pH->ht[h];.    e
69060 6c 65 6d 20 3d 20 70 45 6e 74 72 79 2d 3e 63 68  lem = pEntry->ch
69070 61 69 6e 3b 0a 20 20 20 20 63 6f 75 6e 74 20 3d  ain;.    count =
69080 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 3b 0a   pEntry->count;.
69090 20 20 20 20 78 43 6f 6d 70 61 72 65 20 3d 20 63      xCompare = c
690a0 6f 6d 70 61 72 65 46 75 6e 63 74 69 6f 6e 28 70  ompareFunction(p
690b0 48 2d 3e 6b 65 79 43 6c 61 73 73 29 3b 0a 20 20  H->keyClass);.  
690c0 20 20 77 68 69 6c 65 28 20 63 6f 75 6e 74 2d 2d    while( count--
690d0 20 26 26 20 65 6c 65 6d 20 29 7b 0a 20 20 20 20   && elem ){.    
690e0 20 20 69 66 28 20 28 2a 78 43 6f 6d 70 61 72 65    if( (*xCompare
690f0 29 28 65 6c 65 6d 2d 3e 70 4b 65 79 2c 65 6c 65  )(elem->pKey,ele
69100 6d 2d 3e 6e 4b 65 79 2c 70 4b 65 79 2c 6e 4b 65  m->nKey,pKey,nKe
69110 79 29 3d 3d 30 20 29 7b 20 0a 20 20 20 20 20 20  y)==0 ){ .      
69120 20 20 72 65 74 75 72 6e 20 65 6c 65 6d 3b 0a 20    return elem;. 
69130 20 20 20 20 20 7d 0a 20 20 20 20 20 20 65 6c 65       }.      ele
69140 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a  m = elem->next;.
69150 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
69160 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f  rn 0;.}../* Remo
69170 76 65 20 61 20 73 69 6e 67 6c 65 20 65 6e 74 72  ve a single entr
69180 79 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20  y from the hash 
69190 74 61 62 6c 65 20 67 69 76 65 6e 20 61 20 70 6f  table given a po
691a0 69 6e 74 65 72 20 74 6f 20 74 68 61 74 0a 2a 2a  inter to that.**
691b0 20 65 6c 65 6d 65 6e 74 20 61 6e 64 20 61 20 68   element and a h
691c0 61 73 68 20 6f 6e 20 74 68 65 20 65 6c 65 6d 65  ash on the eleme
691d0 6e 74 27 73 20 6b 65 79 2e 0a 2a 2f 0a 73 74 61  nt's key..*/.sta
691e0 74 69 63 20 76 6f 69 64 20 72 65 6d 6f 76 65 45  tic void removeE
691f0 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 28  lementGivenHash(
69200 0a 20 20 48 61 73 68 20 2a 70 48 2c 20 20 20 20  .  Hash *pH,    
69210 20 20 20 20 20 2f 2a 20 54 68 65 20 70 48 20 63       /* The pH c
69220 6f 6e 74 61 69 6e 69 6e 67 20 22 65 6c 65 6d 22  ontaining "elem"
69230 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 2a 20   */.  HashElem* 
69240 65 6c 65 6d 2c 20 20 20 2f 2a 20 54 68 65 20 65  elem,   /* The e
69250 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20 72 65 6d  lement to be rem
69260 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 70 48  oved from the pH
69270 20 2a 2f 0a 20 20 69 6e 74 20 68 20 20 20 20 20   */.  int h     
69280 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20          /* Hash 
69290 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 65 6c  value for the el
692a0 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 73 74  ement */.){.  st
692b0 72 75 63 74 20 5f 68 74 20 2a 70 45 6e 74 72 79  ruct _ht *pEntry
692c0 3b 0a 20 20 69 66 28 20 65 6c 65 6d 2d 3e 70 72  ;.  if( elem->pr
692d0 65 76 20 29 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e  ev ){.    elem->
692e0 70 72 65 76 2d 3e 6e 65 78 74 20 3d 20 65 6c 65  prev->next = ele
692f0 6d 2d 3e 6e 65 78 74 3b 20 0a 20 20 7d 65 6c 73  m->next; .  }els
69300 65 7b 0a 20 20 20 20 70 48 2d 3e 66 69 72 73 74  e{.    pH->first
69310 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20   = elem->next;. 
69320 20 7d 0a 20 20 69 66 28 20 65 6c 65 6d 2d 3e 6e   }.  if( elem->n
69330 65 78 74 20 29 7b 0a 20 20 20 20 65 6c 65 6d 2d  ext ){.    elem-
69340 3e 6e 65 78 74 2d 3e 70 72 65 76 20 3d 20 65 6c  >next->prev = el
69350 65 6d 2d 3e 70 72 65 76 3b 0a 20 20 7d 0a 20 20  em->prev;.  }.  
69360 70 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68 74  pEntry = &pH->ht
69370 5b 68 5d 3b 0a 20 20 69 66 28 20 70 45 6e 74 72  [h];.  if( pEntr
69380 79 2d 3e 63 68 61 69 6e 3d 3d 65 6c 65 6d 20 29  y->chain==elem )
69390 7b 0a 20 20 20 20 70 45 6e 74 72 79 2d 3e 63 68  {.    pEntry->ch
693a0 61 69 6e 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74  ain = elem->next
693b0 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 2d 3e  ;.  }.  pEntry->
693c0 63 6f 75 6e 74 2d 2d 3b 0a 20 20 69 66 28 20 70  count--;.  if( p
693d0 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 3c 3d 30 20  Entry->count<=0 
693e0 29 7b 0a 20 20 20 20 70 45 6e 74 72 79 2d 3e 63  ){.    pEntry->c
693f0 68 61 69 6e 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  hain = 0;.  }.  
69400 69 66 28 20 70 48 2d 3e 63 6f 70 79 4b 65 79 20  if( pH->copyKey 
69410 29 7b 0a 20 20 20 20 70 48 2d 3e 78 46 72 65 65  ){.    pH->xFree
69420 28 65 6c 65 6d 2d 3e 70 4b 65 79 29 3b 0a 20 20  (elem->pKey);.  
69430 7d 0a 20 20 70 48 2d 3e 78 46 72 65 65 28 20 65  }.  pH->xFree( e
69440 6c 65 6d 20 29 3b 0a 20 20 70 48 2d 3e 63 6f 75  lem );.  pH->cou
69450 6e 74 2d 2d 3b 0a 20 20 69 66 28 20 70 48 2d 3e  nt--;.  if( pH->
69460 63 6f 75 6e 74 3c 3d 30 20 29 7b 0a 20 20 20 20  count<=0 ){.    
69470 61 73 73 65 72 74 28 20 70 48 2d 3e 66 69 72 73  assert( pH->firs
69480 74 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65  t==0 );.    asse
69490 72 74 28 20 70 48 2d 3e 63 6f 75 6e 74 3d 3d 30  rt( pH->count==0
694a0 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 48   );.    sqlite3H
694b0 61 73 68 43 6c 65 61 72 28 70 48 29 3b 0a 20 20  ashClear(pH);.  
694c0 7d 0a 7d 0a 0a 2f 2a 20 41 74 74 65 6d 70 74 20  }.}../* Attempt 
694d0 74 6f 20 6c 6f 63 61 74 65 20 61 6e 20 65 6c 65  to locate an ele
694e0 6d 65 6e 74 20 6f 66 20 74 68 65 20 68 61 73 68  ment of the hash
694f0 20 74 61 62 6c 65 20 70 48 20 77 69 74 68 20 61   table pH with a
69500 20 6b 65 79 0a 2a 2a 20 74 68 61 74 20 6d 61 74   key.** that mat
69510 63 68 65 73 20 70 4b 65 79 2c 6e 4b 65 79 2e 20  ches pKey,nKey. 
69520 20 52 65 74 75 72 6e 20 74 68 65 20 64 61 74 61   Return the data
69530 20 66 6f 72 20 74 68 69 73 20 65 6c 65 6d 65 6e   for this elemen
69540 74 20 69 66 20 69 74 20 69 73 0a 2a 2a 20 66 6f  t if it is.** fo
69550 75 6e 64 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20  und, or NULL if 
69560 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63  there is no matc
69570 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  h..*/.SQLITE_PRI
69580 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
69590 65 33 48 61 73 68 46 69 6e 64 28 63 6f 6e 73 74  e3HashFind(const
695a0 20 48 61 73 68 20 2a 70 48 2c 20 63 6f 6e 73 74   Hash *pH, const
695b0 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74   void *pKey, int
695c0 20 6e 4b 65 79 29 7b 0a 20 20 69 6e 74 20 68 3b   nKey){.  int h;
695d0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
695e0 41 20 68 61 73 68 20 6f 6e 20 6b 65 79 20 2a 2f  A hash on key */
695f0 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65  .  HashElem *ele
69600 6d 3b 20 20 20 20 2f 2a 20 54 68 65 20 65 6c 65  m;    /* The ele
69610 6d 65 6e 74 20 74 68 61 74 20 6d 61 74 63 68 65  ment that matche
69620 73 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 28  s key */.  int (
69630 2a 78 48 61 73 68 29 28 63 6f 6e 73 74 20 76 6f  *xHash)(const vo
69640 69 64 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 54 68  id*,int);  /* Th
69650 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f 6e 20  e hash function 
69660 2a 2f 0a 0a 20 20 69 66 28 20 70 48 3d 3d 30 20  */..  if( pH==0 
69670 7c 7c 20 70 48 2d 3e 68 74 3d 3d 30 20 29 20 72  || pH->ht==0 ) r
69680 65 74 75 72 6e 20 30 3b 0a 20 20 78 48 61 73 68  eturn 0;.  xHash
69690 20 3d 20 68 61 73 68 46 75 6e 63 74 69 6f 6e 28   = hashFunction(
696a0 70 48 2d 3e 6b 65 79 43 6c 61 73 73 29 3b 0a 20  pH->keyClass);. 
696b0 20 61 73 73 65 72 74 28 20 78 48 61 73 68 21 3d   assert( xHash!=
696c0 30 20 29 3b 0a 20 20 68 20 3d 20 28 2a 78 48 61  0 );.  h = (*xHa
696d0 73 68 29 28 70 4b 65 79 2c 6e 4b 65 79 29 3b 0a  sh)(pKey,nKey);.
696e0 20 20 61 73 73 65 72 74 28 20 28 70 48 2d 3e 68    assert( (pH->h
696f0 74 73 69 7a 65 20 26 20 28 70 48 2d 3e 68 74 73  tsize & (pH->hts
69700 69 7a 65 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20  ize-1))==0 );.  
69710 65 6c 65 6d 20 3d 20 66 69 6e 64 45 6c 65 6d 65  elem = findEleme
69720 6e 74 47 69 76 65 6e 48 61 73 68 28 70 48 2c 70  ntGivenHash(pH,p
69730 4b 65 79 2c 6e 4b 65 79 2c 20 68 20 26 20 28 70  Key,nKey, h & (p
69740 48 2d 3e 68 74 73 69 7a 65 2d 31 29 29 3b 0a 20  H->htsize-1));. 
69750 20 72 65 74 75 72 6e 20 65 6c 65 6d 20 3f 20 65   return elem ? e
69760 6c 65 6d 2d 3e 64 61 74 61 20 3a 20 30 3b 0a 7d  lem->data : 0;.}
69770 0a 0a 2f 2a 20 49 6e 73 65 72 74 20 61 6e 20 65  ../* Insert an e
69780 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65 20  lement into the 
69790 68 61 73 68 20 74 61 62 6c 65 20 70 48 2e 20 20  hash table pH.  
697a0 54 68 65 20 6b 65 79 20 69 73 20 70 4b 65 79 2c  The key is pKey,
697b0 6e 4b 65 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20  nKey.** and the 
697c0 64 61 74 61 20 69 73 20 22 64 61 74 61 22 2e 0a  data is "data"..
697d0 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 65 6c 65 6d  **.** If no elem
697e0 65 6e 74 20 65 78 69 73 74 73 20 77 69 74 68 20  ent exists with 
697f0 61 20 6d 61 74 63 68 69 6e 67 20 6b 65 79 2c 20  a matching key, 
69800 74 68 65 6e 20 61 20 6e 65 77 0a 2a 2a 20 65 6c  then a new.** el
69810 65 6d 65 6e 74 20 69 73 20 63 72 65 61 74 65 64  ement is created
69820 2e 20 20 41 20 63 6f 70 79 20 6f 66 20 74 68 65  .  A copy of the
69830 20 6b 65 79 20 69 73 20 6d 61 64 65 20 69 66 20   key is made if 
69840 74 68 65 20 63 6f 70 79 4b 65 79 0a 2a 2a 20 66  the copyKey.** f
69850 6c 61 67 20 69 73 20 73 65 74 2e 20 20 4e 55 4c  lag is set.  NUL
69860 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  L is returned..*
69870 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20  *.** If another 
69880 65 6c 65 6d 65 6e 74 20 61 6c 72 65 61 64 79 20  element already 
69890 65 78 69 73 74 73 20 77 69 74 68 20 74 68 65 20  exists with the 
698a0 73 61 6d 65 20 6b 65 79 2c 20 74 68 65 6e 20 74  same key, then t
698b0 68 65 0a 2a 2a 20 6e 65 77 20 64 61 74 61 20 72  he.** new data r
698c0 65 70 6c 61 63 65 73 20 74 68 65 20 6f 6c 64 20  eplaces the old 
698d0 64 61 74 61 20 61 6e 64 20 74 68 65 20 6f 6c 64  data and the old
698e0 20 64 61 74 61 20 69 73 20 72 65 74 75 72 6e 65   data is returne
698f0 64 2e 0a 2a 2a 20 54 68 65 20 6b 65 79 20 69 73  d..** The key is
69900 20 6e 6f 74 20 63 6f 70 69 65 64 20 69 6e 20 74   not copied in t
69910 68 69 73 20 69 6e 73 74 61 6e 63 65 2e 20 20 49  his instance.  I
69920 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73  f a malloc fails
69930 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6e 65  , then.** the ne
69940 77 20 64 61 74 61 20 69 73 20 72 65 74 75 72 6e  w data is return
69950 65 64 20 61 6e 64 20 74 68 65 20 68 61 73 68 20  ed and the hash 
69960 74 61 62 6c 65 20 69 73 20 75 6e 63 68 61 6e 67  table is unchang
69970 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ed..**.** If the
69980 20 22 64 61 74 61 22 20 70 61 72 61 6d 65 74 65   "data" paramete
69990 72 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69  r to this functi
699a0 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e  on is NULL, then
699b0 20 74 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20   the.** element 
699c0 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f  corresponding to
699d0 20 22 6b 65 79 22 20 69 73 20 72 65 6d 6f 76 65   "key" is remove
699e0 64 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20  d from the hash 
699f0 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  table..*/.SQLITE
69a00 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
69a10 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74  qlite3HashInsert
69a20 28 48 61 73 68 20 2a 70 48 2c 20 63 6f 6e 73 74  (Hash *pH, const
69a30 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74   void *pKey, int
69a40 20 6e 4b 65 79 2c 20 76 6f 69 64 20 2a 64 61 74   nKey, void *dat
69a50 61 29 7b 0a 20 20 69 6e 74 20 68 72 61 77 3b 20  a){.  int hraw; 
69a60 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
69a70 61 77 20 68 61 73 68 20 76 61 6c 75 65 20 6f 66  aw hash value of
69a80 20 74 68 65 20 6b 65 79 20 2a 2f 0a 20 20 69 6e   the key */.  in
69a90 74 20 68 3b 20 20 20 20 20 20 20 20 20 20 20 20  t h;            
69aa0 20 20 20 20 2f 2a 20 74 68 65 20 68 61 73 68 20      /* the hash 
69ab0 6f 66 20 74 68 65 20 6b 65 79 20 6d 6f 64 75 6c  of the key modul
69ac0 6f 20 68 61 73 68 20 74 61 62 6c 65 20 73 69 7a  o hash table siz
69ad0 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20  e */.  HashElem 
69ae0 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20 2f 2a 20  *elem;       /* 
69af0 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20 74 68 72  Used to loop thr
69b00 75 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 6c 69  u the element li
69b10 73 74 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d  st */.  HashElem
69b20 20 2a 6e 65 77 5f 65 6c 65 6d 3b 20 20 20 2f 2a   *new_elem;   /*
69b30 20 4e 65 77 20 65 6c 65 6d 65 6e 74 20 61 64 64   New element add
69b40 65 64 20 74 6f 20 74 68 65 20 70 48 20 2a 2f 0a  ed to the pH */.
69b50 20 20 69 6e 74 20 28 2a 78 48 61 73 68 29 28 63    int (*xHash)(c
69b60 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 3b  onst void*,int);
69b70 20 20 2f 2a 20 54 68 65 20 68 61 73 68 20 66 75    /* The hash fu
69b80 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73  nction */..  ass
69b90 65 72 74 28 20 70 48 21 3d 30 20 29 3b 0a 20 20  ert( pH!=0 );.  
69ba0 78 48 61 73 68 20 3d 20 68 61 73 68 46 75 6e 63  xHash = hashFunc
69bb0 74 69 6f 6e 28 70 48 2d 3e 6b 65 79 43 6c 61 73  tion(pH->keyClas
69bc0 73 29 3b 0a 20 20 61 73 73 65 72 74 28 20 78 48  s);.  assert( xH
69bd0 61 73 68 21 3d 30 20 29 3b 0a 20 20 68 72 61 77  ash!=0 );.  hraw
69be0 20 3d 20 28 2a 78 48 61 73 68 29 28 70 4b 65 79   = (*xHash)(pKey
69bf0 2c 20 6e 4b 65 79 29 3b 0a 20 20 61 73 73 65 72  , nKey);.  asser
69c00 74 28 20 28 70 48 2d 3e 68 74 73 69 7a 65 20 26  t( (pH->htsize &
69c10 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d 31 29 29   (pH->htsize-1))
69c20 3d 3d 30 20 29 3b 0a 20 20 68 20 3d 20 68 72 61  ==0 );.  h = hra
69c30 77 20 26 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d  w & (pH->htsize-
69c40 31 29 3b 0a 20 20 65 6c 65 6d 20 3d 20 66 69 6e  1);.  elem = fin
69c50 64 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73  dElementGivenHas
69c60 68 28 70 48 2c 70 4b 65 79 2c 6e 4b 65 79 2c 68  h(pH,pKey,nKey,h
69c70 29 3b 0a 20 20 69 66 28 20 65 6c 65 6d 20 29 7b  );.  if( elem ){
69c80 0a 20 20 20 20 76 6f 69 64 20 2a 6f 6c 64 5f 64  .    void *old_d
69c90 61 74 61 20 3d 20 65 6c 65 6d 2d 3e 64 61 74 61  ata = elem->data
69ca0 3b 0a 20 20 20 20 69 66 28 20 64 61 74 61 3d 3d  ;.    if( data==
69cb0 30 20 29 7b 0a 20 20 20 20 20 20 72 65 6d 6f 76  0 ){.      remov
69cc0 65 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73  eElementGivenHas
69cd0 68 28 70 48 2c 65 6c 65 6d 2c 68 29 3b 0a 20 20  h(pH,elem,h);.  
69ce0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 65    }else{.      e
69cf0 6c 65 6d 2d 3e 64 61 74 61 20 3d 20 64 61 74 61  lem->data = data
69d00 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ;.    }.    retu
69d10 72 6e 20 6f 6c 64 5f 64 61 74 61 3b 0a 20 20 7d  rn old_data;.  }
69d20 0a 20 20 69 66 28 20 64 61 74 61 3d 3d 30 20 29  .  if( data==0 )
69d30 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 6e 65 77   return 0;.  new
69d40 5f 65 6c 65 6d 20 3d 20 28 48 61 73 68 45 6c 65  _elem = (HashEle
69d50 6d 2a 29 70 48 2d 3e 78 4d 61 6c 6c 6f 63 28 20  m*)pH->xMalloc( 
69d60 73 69 7a 65 6f 66 28 48 61 73 68 45 6c 65 6d 29  sizeof(HashElem)
69d70 20 29 3b 0a 20 20 69 66 28 20 6e 65 77 5f 65 6c   );.  if( new_el
69d80 65 6d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 64  em==0 ) return d
69d90 61 74 61 3b 0a 20 20 69 66 28 20 70 48 2d 3e 63  ata;.  if( pH->c
69da0 6f 70 79 4b 65 79 20 26 26 20 70 4b 65 79 21 3d  opyKey && pKey!=
69db0 30 20 29 7b 0a 20 20 20 20 6e 65 77 5f 65 6c 65  0 ){.    new_ele
69dc0 6d 2d 3e 70 4b 65 79 20 3d 20 70 48 2d 3e 78 4d  m->pKey = pH->xM
69dd0 61 6c 6c 6f 63 28 20 6e 4b 65 79 20 29 3b 0a 20  alloc( nKey );. 
69de0 20 20 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d 2d     if( new_elem-
69df0 3e 70 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20  >pKey==0 ){.    
69e00 20 20 70 48 2d 3e 78 46 72 65 65 28 6e 65 77 5f    pH->xFree(new_
69e10 65 6c 65 6d 29 3b 0a 20 20 20 20 20 20 72 65 74  elem);.      ret
69e20 75 72 6e 20 64 61 74 61 3b 0a 20 20 20 20 7d 0a  urn data;.    }.
69e30 20 20 20 20 6d 65 6d 63 70 79 28 28 76 6f 69 64      memcpy((void
69e40 2a 29 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79  *)new_elem->pKey
69e50 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a 20  , pKey, nKey);. 
69e60 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 65 77 5f   }else{.    new_
69e70 65 6c 65 6d 2d 3e 70 4b 65 79 20 3d 20 28 76 6f  elem->pKey = (vo
69e80 69 64 2a 29 70 4b 65 79 3b 0a 20 20 7d 0a 20 20  id*)pKey;.  }.  
69e90 6e 65 77 5f 65 6c 65 6d 2d 3e 6e 4b 65 79 20 3d  new_elem->nKey =
69ea0 20 6e 4b 65 79 3b 0a 20 20 70 48 2d 3e 63 6f 75   nKey;.  pH->cou
69eb0 6e 74 2b 2b 3b 0a 20 20 69 66 28 20 70 48 2d 3e  nt++;.  if( pH->
69ec0 68 74 73 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20  htsize==0 ){.   
69ed0 20 72 65 68 61 73 68 28 70 48 2c 38 29 3b 0a 20   rehash(pH,8);. 
69ee0 20 20 20 69 66 28 20 70 48 2d 3e 68 74 73 69 7a     if( pH->htsiz
69ef0 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 48  e==0 ){.      pH
69f00 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 20 20 20  ->count = 0;.   
69f10 20 20 20 69 66 28 20 70 48 2d 3e 63 6f 70 79 4b     if( pH->copyK
69f20 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 70 48  ey ){.        pH
69f30 2d 3e 78 46 72 65 65 28 6e 65 77 5f 65 6c 65 6d  ->xFree(new_elem
69f40 2d 3e 70 4b 65 79 29 3b 0a 20 20 20 20 20 20 7d  ->pKey);.      }
69f50 0a 20 20 20 20 20 20 70 48 2d 3e 78 46 72 65 65  .      pH->xFree
69f60 28 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 20 20  (new_elem);.    
69f70 20 20 72 65 74 75 72 6e 20 64 61 74 61 3b 0a 20    return data;. 
69f80 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70     }.  }.  if( p
69f90 48 2d 3e 63 6f 75 6e 74 20 3e 20 70 48 2d 3e 68  H->count > pH->h
69fa0 74 73 69 7a 65 20 29 7b 0a 20 20 20 20 72 65 68  tsize ){.    reh
69fb0 61 73 68 28 70 48 2c 70 48 2d 3e 68 74 73 69 7a  ash(pH,pH->htsiz
69fc0 65 2a 32 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65  e*2);.  }.  asse
69fd0 72 74 28 20 70 48 2d 3e 68 74 73 69 7a 65 3e 30  rt( pH->htsize>0
69fe0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70   );.  assert( (p
69ff0 48 2d 3e 68 74 73 69 7a 65 20 26 20 28 70 48 2d  H->htsize & (pH-
6a000 3e 68 74 73 69 7a 65 2d 31 29 29 3d 3d 30 20 29  >htsize-1))==0 )
6a010 3b 0a 20 20 68 20 3d 20 68 72 61 77 20 26 20 28  ;.  h = hraw & (
6a020 70 48 2d 3e 68 74 73 69 7a 65 2d 31 29 3b 0a 20  pH->htsize-1);. 
6a030 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 70   insertElement(p
6a040 48 2c 20 26 70 48 2d 3e 68 74 5b 68 5d 2c 20 6e  H, &pH->ht[h], n
6a050 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 6e 65 77 5f  ew_elem);.  new_
6a060 65 6c 65 6d 2d 3e 64 61 74 61 20 3d 20 64 61 74  elem->data = dat
6a070 61 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d  a;.  return 0;.}
6a080 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
6a090 2a 20 45 6e 64 20 6f 66 20 68 61 73 68 2e 63 20  * End of hash.c 
6a0a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6a0b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6a0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6a0d0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
6a0e0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 70 63  * Begin file opc
6a0f0 6f 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  odes.c *********
6a100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6a110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6a120 2f 0a 2f 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c  /./* Automatical
6a130 6c 79 20 67 65 6e 65 72 61 74 65 64 2e 20 20 44  ly generated.  D
6a140 6f 20 6e 6f 74 20 65 64 69 74 20 2a 2f 0a 2f 2a  o not edit */./*
6a150 20 53 65 65 20 74 68 65 20 6d 6b 6f 70 63 6f 64   See the mkopcod
6a160 65 63 2e 61 77 6b 20 73 63 72 69 70 74 20 66 6f  ec.awk script fo
6a170 72 20 64 65 74 61 69 6c 73 2e 20 2a 2f 0a 23 69  r details. */.#i
6a180 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
6a190 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 29 20  E_OMIT_EXPLAIN) 
6a1a0 7c 7c 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42  || !defined(NDEB
6a1b0 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 56  UG) || defined(V
6a1c0 44 42 45 5f 50 52 4f 46 49 4c 45 29 20 7c 7c 20  DBE_PROFILE) || 
6a1d0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
6a1e0 45 42 55 47 29 0a 63 6f 6e 73 74 20 63 68 61 72  EBUG).const char
6a1f0 20 2a 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 4f   *const sqlite3O
6a200 70 63 6f 64 65 4e 61 6d 65 73 5b 5d 20 3d 20 7b  pcodeNames[] = {
6a210 20 22 3f 22 2c 0a 20 2f 2a 20 20 20 31 20 2a 2f   "?",. /*   1 */
6a220 20 22 4d 65 6d 4c 6f 61 64 22 2c 0a 20 2f 2a 20   "MemLoad",. /* 
6a230 20 20 32 20 2a 2f 20 22 56 4e 65 78 74 22 2c 0a    2 */ "VNext",.
6a240 20 2f 2a 20 20 20 33 20 2a 2f 20 22 43 6f 6c 75   /*   3 */ "Colu
6a250 6d 6e 22 2c 0a 20 2f 2a 20 20 20 34 20 2a 2f 20  mn",. /*   4 */ 
6a260 22 53 65 74 43 6f 6f 6b 69 65 22 2c 0a 20 2f 2a  "SetCookie",. /*
6a270 20 20 20 35 20 2a 2f 20 22 49 66 4d 65 6d 50 6f     5 */ "IfMemPo
6a280 73 22 2c 0a 20 2f 2a 20 20 20 36 20 2a 2f 20 22  s",. /*   6 */ "
6a290 53 65 71 75 65 6e 63 65 22 2c 0a 20 2f 2a 20 20  Sequence",. /*  
6a2a0 20 37 20 2a 2f 20 22 4d 6f 76 65 47 74 22 2c 0a   7 */ "MoveGt",.
6a2b0 20 2f 2a 20 20 20 38 20 2a 2f 20 22 52 6f 77 4b   /*   8 */ "RowK
6a2c0 65 79 22 2c 0a 20 2f 2a 20 20 20 39 20 2a 2f 20  ey",. /*   9 */ 
6a2d0 22 4f 70 65 6e 57 72 69 74 65 22 2c 0a 20 2f 2a  "OpenWrite",. /*
6a2e0 20 20 31 30 20 2a 2f 20 22 49 66 22 2c 0a 20 2f    10 */ "If",. /
6a2f0 2a 20 20 31 31 20 2a 2f 20 22 50 6f 70 22 2c 0a  *  11 */ "Pop",.
6a300 20 2f 2a 20 20 31 32 20 2a 2f 20 22 56 52 6f 77   /*  12 */ "VRow
6a310 69 64 22 2c 0a 20 2f 2a 20 20 31 33 20 2a 2f 20  id",. /*  13 */ 
6a320 22 43 6f 6c 6c 53 65 71 22 2c 0a 20 2f 2a 20 20  "CollSeq",. /*  
6a330 31 34 20 2a 2f 20 22 4f 70 65 6e 52 65 61 64 22  14 */ "OpenRead"
6a340 2c 0a 20 2f 2a 20 20 31 35 20 2a 2f 20 22 45 78  ,. /*  15 */ "Ex
6a350 70 69 72 65 22 2c 0a 20 2f 2a 20 20 31 36 20 2a  pire",. /*  16 *
6a360 2f 20 22 4e 6f 74 22 2c 0a 20 2f 2a 20 20 31 37  / "Not",. /*  17
6a370 20 2a 2f 20 22 41 75 74 6f 43 6f 6d 6d 69 74 22   */ "AutoCommit"
6a380 2c 0a 20 2f 2a 20 20 31 38 20 2a 2f 20 22 49 6e  ,. /*  18 */ "In
6a390 74 65 67 72 69 74 79 43 6b 22 2c 0a 20 2f 2a 20  tegrityCk",. /* 
6a3a0 20 31 39 20 2a 2f 20 22 53 6f 72 74 22 2c 0a 20   19 */ "Sort",. 
6a3b0 2f 2a 20 20 32 30 20 2a 2f 20 22 46 75 6e 63 74  /*  20 */ "Funct
6a3c0 69 6f 6e 22 2c 0a 20 2f 2a 20 20 32 31 20 2a 2f  ion",. /*  21 */
6a3d0 20 22 4e 6f 6f 70 22 2c 0a 20 2f 2a 20 20 32 32   "Noop",. /*  22
6a3e0 20 2a 2f 20 22 52 65 74 75 72 6e 22 2c 0a 20 2f   */ "Return",. /
6a3f0 2a 20 20 32 33 20 2a 2f 20 22 4e 65 77 52 6f 77  *  23 */ "NewRow
6a400 69 64 22 2c 0a 20 2f 2a 20 20 32 34 20 2a 2f 20  id",. /*  24 */ 
6a410 22 49 66 4d 65 6d 4e 65 67 22 2c 0a 20 2f 2a 20  "IfMemNeg",. /* 
6a420 20 32 35 20 2a 2f 20 22 56 61 72 69 61 62 6c 65   25 */ "Variable
6a430 22 2c 0a 20 2f 2a 20 20 32 36 20 2a 2f 20 22 53  ",. /*  26 */ "S
6a440 74 72 69 6e 67 22 2c 0a 20 2f 2a 20 20 32 37 20  tring",. /*  27 
6a450 2a 2f 20 22 52 65 61 6c 41 66 66 69 6e 69 74 79  */ "RealAffinity
6a460 22 2c 0a 20 2f 2a 20 20 32 38 20 2a 2f 20 22 56  ",. /*  28 */ "V
6a470 52 65 6e 61 6d 65 22 2c 0a 20 2f 2a 20 20 32 39  Rename",. /*  29
6a480 20 2a 2f 20 22 50 61 72 73 65 53 63 68 65 6d 61   */ "ParseSchema
6a490 22 2c 0a 20 2f 2a 20 20 33 30 20 2a 2f 20 22 56  ",. /*  30 */ "V
6a4a0 4f 70 65 6e 22 2c 0a 20 2f 2a 20 20 33 31 20 2a  Open",. /*  31 *
6a4b0 2f 20 22 43 6c 6f 73 65 22 2c 0a 20 2f 2a 20 20  / "Close",. /*  
6a4c0 33 32 20 2a 2f 20 22 43 72 65 61 74 65 49 6e 64  32 */ "CreateInd
6a4d0 65 78 22 2c 0a 20 2f 2a 20 20 33 33 20 2a 2f 20  ex",. /*  33 */ 
6a4e0 22 49 73 55 6e 69 71 75 65 22 2c 0a 20 2f 2a 20  "IsUnique",. /* 
6a4f0 20 33 34 20 2a 2f 20 22 4e 6f 74 46 6f 75 6e 64   34 */ "NotFound
6a500 22 2c 0a 20 2f 2a 20 20 33 35 20 2a 2f 20 22 49  ",. /*  35 */ "I
6a510 6e 74 36 34 22 2c 0a 20 2f 2a 20 20 33 36 20 2a  nt64",. /*  36 *
6a520 2f 20 22 4d 75 73 74 42 65 49 6e 74 22 2c 0a 20  / "MustBeInt",. 
6a530 2f 2a 20 20 33 37 20 2a 2f 20 22 48 61 6c 74 22  /*  37 */ "Halt"
6a540 2c 0a 20 2f 2a 20 20 33 38 20 2a 2f 20 22 52 6f  ,. /*  38 */ "Ro
6a550 77 69 64 22 2c 0a 20 2f 2a 20 20 33 39 20 2a 2f  wid",. /*  39 */
6a560 20 22 49 64 78 4c 54 22 2c 0a 20 2f 2a 20 20 34   "IdxLT",. /*  4
6a570 30 20 2a 2f 20 22 41 64 64 49 6d 6d 22 2c 0a 20  0 */ "AddImm",. 
6a580 2f 2a 20 20 34 31 20 2a 2f 20 22 53 74 61 74 65  /*  41 */ "State
6a590 6d 65 6e 74 22 2c 0a 20 2f 2a 20 20 34 32 20 2a  ment",. /*  42 *
6a5a0 2f 20 22 52 6f 77 44 61 74 61 22 2c 0a 20 2f 2a  / "RowData",. /*
6a5b0 20 20 34 33 20 2a 2f 20 22 4d 65 6d 4d 61 78 22    43 */ "MemMax"
6a5c0 2c 0a 20 2f 2a 20 20 34 34 20 2a 2f 20 22 50 75  ,. /*  44 */ "Pu
6a5d0 73 68 22 2c 0a 20 2f 2a 20 20 34 35 20 2a 2f 20  sh",. /*  45 */ 
6a5e0 22 4e 6f 74 45 78 69 73 74 73 22 2c 0a 20 2f 2a  "NotExists",. /*
6a5f0 20 20 34 36 20 2a 2f 20 22 4d 65 6d 49 6e 63 72    46 */ "MemIncr
6a600 22 2c 0a 20 2f 2a 20 20 34 37 20 2a 2f 20 22 47  ",. /*  47 */ "G
6a610 6f 73 75 62 22 2c 0a 20 2f 2a 20 20 34 38 20 2a  osub",. /*  48 *
6a620 2f 20 22 49 6e 74 65 67 65 72 22 2c 0a 20 2f 2a  / "Integer",. /*
6a630 20 20 34 39 20 2a 2f 20 22 4d 65 6d 49 6e 74 22    49 */ "MemInt"
6a640 2c 0a 20 2f 2a 20 20 35 30 20 2a 2f 20 22 50 72  ,. /*  50 */ "Pr
6a650 65 76 22 2c 0a 20 2f 2a 20 20 35 31 20 2a 2f 20  ev",. /*  51 */ 
6a660 22 56 43 6f 6c 75 6d 6e 22 2c 0a 20 2f 2a 20 20  "VColumn",. /*  
6a670 35 32 20 2a 2f 20 22 43 72 65 61 74 65 54 61 62  52 */ "CreateTab
6a680 6c 65 22 2c 0a 20 2f 2a 20 20 35 33 20 2a 2f 20  le",. /*  53 */ 
6a690 22 4c 61 73 74 22 2c 0a 20 2f 2a 20 20 35 34 20  "Last",. /*  54 
6a6a0 2a 2f 20 22 49 6e 63 72 56 61 63 75 75 6d 22 2c  */ "IncrVacuum",
6a6b0 0a 20 2f 2a 20 20 35 35 20 2a 2f 20 22 49 64 78  . /*  55 */ "Idx
6a6c0 52 6f 77 69 64 22 2c 0a 20 2f 2a 20 20 35 36 20  Rowid",. /*  56 
6a6d0 2a 2f 20 22 4d 61 6b 65 49 64 78 52 65 63 22 2c  */ "MakeIdxRec",
6a6e0 0a 20 2f 2a 20 20 35 37 20 2a 2f 20 22 52 65 73  . /*  57 */ "Res
6a6f0 65 74 43 6f 75 6e 74 22 2c 0a 20 2f 2a 20 20 35  etCount",. /*  5
6a700 38 20 2a 2f 20 22 46 69 66 6f 57 72 69 74 65 22  8 */ "FifoWrite"
6a710 2c 0a 20 2f 2a 20 20 35 39 20 2a 2f 20 22 43 61  ,. /*  59 */ "Ca
6a720 6c 6c 62 61 63 6b 22 2c 0a 20 2f 2a 20 20 36 30  llback",. /*  60
6a730 20 2a 2f 20 22 4f 72 22 2c 0a 20 2f 2a 20 20 36   */ "Or",. /*  6
6a740 31 20 2a 2f 20 22 41 6e 64 22 2c 0a 20 2f 2a 20  1 */ "And",. /* 
6a750 20 36 32 20 2a 2f 20 22 43 6f 6e 74 65 78 74 50   62 */ "ContextP
6a760 75 73 68 22 2c 0a 20 2f 2a 20 20 36 33 20 2a 2f  ush",. /*  63 */
6a770 20 22 44 72 6f 70 54 72 69 67 67 65 72 22 2c 0a   "DropTrigger",.
6a780 20 2f 2a 20 20 36 34 20 2a 2f 20 22 44 72 6f 70   /*  64 */ "Drop
6a790 49 6e 64 65 78 22 2c 0a 20 2f 2a 20 20 36 35 20  Index",. /*  65 
6a7a0 2a 2f 20 22 49 73 4e 75 6c 6c 22 2c 0a 20 2f 2a  */ "IsNull",. /*
6a7b0 20 20 36 36 20 2a 2f 20 22 4e 6f 74 4e 75 6c 6c    66 */ "NotNull
6a7c0 22 2c 0a 20 2f 2a 20 20 36 37 20 2a 2f 20 22 4e  ",. /*  67 */ "N
6a7d0 65 22 2c 0a 20 2f 2a 20 20 36 38 20 2a 2f 20 22  e",. /*  68 */ "
6a7e0 45 71 22 2c 0a 20 2f 2a 20 20 36 39 20 2a 2f 20  Eq",. /*  69 */ 
6a7f0 22 47 74 22 2c 0a 20 2f 2a 20 20 37 30 20 2a 2f  "Gt",. /*  70 */
6a800 20 22 4c 65 22 2c 0a 20 2f 2a 20 20 37 31 20 2a   "Le",. /*  71 *
6a810 2f 20 22 4c 74 22 2c 0a 20 2f 2a 20 20 37 32 20  / "Lt",. /*  72 
6a820 2a 2f 20 22 47 65 22 2c 0a 20 2f 2a 20 20 37 33  */ "Ge",. /*  73
6a830 20 2a 2f 20 22 49 64 78 47 45 22 2c 0a 20 2f 2a   */ "IdxGE",. /*
6a840 20 20 37 34 20 2a 2f 20 22 42 69 74 41 6e 64 22    74 */ "BitAnd"
6a850 2c 0a 20 2f 2a 20 20 37 35 20 2a 2f 20 22 42 69  ,. /*  75 */ "Bi
6a860 74 4f 72 22 2c 0a 20 2f 2a 20 20 37 36 20 2a 2f  tOr",. /*  76 */
6a870 20 22 53 68 69 66 74 4c 65 66 74 22 2c 0a 20 2f   "ShiftLeft",. /
6a880 2a 20 20 37 37 20 2a 2f 20 22 53 68 69 66 74 52  *  77 */ "ShiftR
6a890 69 67 68 74 22 2c 0a 20 2f 2a 20 20 37 38 20 2a  ight",. /*  78 *
6a8a0 2f 20 22 41 64 64 22 2c 0a 20 2f 2a 20 20 37 39  / "Add",. /*  79
6a8b0 20 2a 2f 20 22 53 75 62 74 72 61 63 74 22 2c 0a   */ "Subtract",.
6a8c0 20 2f 2a 20 20 38 30 20 2a 2f 20 22 4d 75 6c 74   /*  80 */ "Mult
6a8d0 69 70 6c 79 22 2c 0a 20 2f 2a 20 20 38 31 20 2a  iply",. /*  81 *
6a8e0 2f 20 22 44 69 76 69 64 65 22 2c 0a 20 2f 2a 20  / "Divide",. /* 
6a8f0 20 38 32 20 2a 2f 20 22 52 65 6d 61 69 6e 64 65   82 */ "Remainde
6a900 72 22 2c 0a 20 2f 2a 20 20 38 33 20 2a 2f 20 22  r",. /*  83 */ "
6a910 43 6f 6e 63 61 74 22 2c 0a 20 2f 2a 20 20 38 34  Concat",. /*  84
6a920 20 2a 2f 20 22 49 64 78 44 65 6c 65 74 65 22 2c   */ "IdxDelete",
6a930 0a 20 2f 2a 20 20 38 35 20 2a 2f 20 22 4e 65 67  . /*  85 */ "Neg
6a940 61 74 69 76 65 22 2c 0a 20 2f 2a 20 20 38 36 20  ative",. /*  86 
6a950 2a 2f 20 22 56 61 63 75 75 6d 22 2c 0a 20 2f 2a  */ "Vacuum",. /*
6a960 20 20 38 37 20 2a 2f 20 22 42 69 74 4e 6f 74 22    87 */ "BitNot"
6a970 2c 0a 20 2f 2a 20 20 38 38 20 2a 2f 20 22 53 74  ,. /*  88 */ "St
6a980 72 69 6e 67 38 22 2c 0a 20 2f 2a 20 20 38 39 20  ring8",. /*  89 
6a990 2a 2f 20 22 4d 6f 76 65 4c 65 22 2c 0a 20 2f 2a  */ "MoveLe",. /*
6a9a0 20 20 39 30 20 2a 2f 20 22 49 66 4e 6f 74 22 2c    90 */ "IfNot",
6a9b0 0a 20 2f 2a 20 20 39 31 20 2a 2f 20 22 44 72 6f  . /*  91 */ "Dro
6a9c0 70 54 61 62 6c 65 22 2c 0a 20 2f 2a 20 20 39 32  pTable",. /*  92
6a9d0 20 2a 2f 20 22 4d 61 6b 65 52 65 63 6f 72 64 22   */ "MakeRecord"
6a9e0 2c 0a 20 2f 2a 20 20 39 33 20 2a 2f 20 22 44 65  ,. /*  93 */ "De
6a9f0 6c 65 74 65 22 2c 0a 20 2f 2a 20 20 39 34 20 2a  lete",. /*  94 *
6aa00 2f 20 22 41 67 67 46 69 6e 61 6c 22 2c 0a 20 2f  / "AggFinal",. /
6aa10 2a 20 20 39 35 20 2a 2f 20 22 44 75 70 22 2c 0a  *  95 */ "Dup",.
6aa20 20 2f 2a 20 20 39 36 20 2a 2f 20 22 47 6f 74 6f   /*  96 */ "Goto
6aa30 22 2c 0a 20 2f 2a 20 20 39 37 20 2a 2f 20 22 54  ",. /*  97 */ "T
6aa40 61 62 6c 65 4c 6f 63 6b 22 2c 0a 20 2f 2a 20 20  ableLock",. /*  
6aa50 39 38 20 2a 2f 20 22 46 69 66 6f 52 65 61 64 22  98 */ "FifoRead"
6aa60 2c 0a 20 2f 2a 20 20 39 39 20 2a 2f 20 22 43 6c  ,. /*  99 */ "Cl
6aa70 65 61 72 22 2c 0a 20 2f 2a 20 31 30 30 20 2a 2f  ear",. /* 100 */
6aa80 20 22 49 64 78 47 54 22 2c 0a 20 2f 2a 20 31 30   "IdxGT",. /* 10
6aa90 31 20 2a 2f 20 22 4d 6f 76 65 4c 74 22 2c 0a 20  1 */ "MoveLt",. 
6aaa0 2f 2a 20 31 30 32 20 2a 2f 20 22 56 65 72 69 66  /* 102 */ "Verif
6aab0 79 43 6f 6f 6b 69 65 22 2c 0a 20 2f 2a 20 31 30  yCookie",. /* 10
6aac0 33 20 2a 2f 20 22 41 67 67 53 74 65 70 22 2c 0a  3 */ "AggStep",.
6aad0 20 2f 2a 20 31 30 34 20 2a 2f 20 22 50 75 6c 6c   /* 104 */ "Pull
6aae0 22 2c 0a 20 2f 2a 20 31 30 35 20 2a 2f 20 22 53  ",. /* 105 */ "S
6aaf0 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 22 2c 0a 20  etNumColumns",. 
6ab00 2f 2a 20 31 30 36 20 2a 2f 20 22 41 62 73 56 61  /* 106 */ "AbsVa
6ab10 6c 75 65 22 2c 0a 20 2f 2a 20 31 30 37 20 2a 2f  lue",. /* 107 */
6ab20 20 22 54 72 61 6e 73 61 63 74 69 6f 6e 22 2c 0a   "Transaction",.
6ab30 20 2f 2a 20 31 30 38 20 2a 2f 20 22 56 46 69 6c   /* 108 */ "VFil
6ab40 74 65 72 22 2c 0a 20 2f 2a 20 31 30 39 20 2a 2f  ter",. /* 109 */
6ab50 20 22 56 44 65 73 74 72 6f 79 22 2c 0a 20 2f 2a   "VDestroy",. /*
6ab60 20 31 31 30 20 2a 2f 20 22 43 6f 6e 74 65 78 74   110 */ "Context
6ab70 50 6f 70 22 2c 0a 20 2f 2a 20 31 31 31 20 2a 2f  Pop",. /* 111 */
6ab80 20 22 4e 65 78 74 22 2c 0a 20 2f 2a 20 31 31 32   "Next",. /* 112
6ab90 20 2a 2f 20 22 49 64 78 49 6e 73 65 72 74 22 2c   */ "IdxInsert",
6aba0 0a 20 2f 2a 20 31 31 33 20 2a 2f 20 22 44 69 73  . /* 113 */ "Dis
6abb0 74 69 6e 63 74 22 2c 0a 20 2f 2a 20 31 31 34 20  tinct",. /* 114 
6abc0 2a 2f 20 22 49 6e 73 65 72 74 22 2c 0a 20 2f 2a  */ "Insert",. /*
6abd0 20 31 31 35 20 2a 2f 20 22 44 65 73 74 72 6f 79   115 */ "Destroy
6abe0 22 2c 0a 20 2f 2a 20 31 31 36 20 2a 2f 20 22 52  ",. /* 116 */ "R
6abf0 65 61 64 43 6f 6f 6b 69 65 22 2c 0a 20 2f 2a 20  eadCookie",. /* 
6ac00 31 31 37 20 2a 2f 20 22 46 6f 72 63 65 49 6e 74  117 */ "ForceInt
6ac10 22 2c 0a 20 2f 2a 20 31 31 38 20 2a 2f 20 22 4c  ",. /* 118 */ "L
6ac20 6f 61 64 41 6e 61 6c 79 73 69 73 22 2c 0a 20 2f  oadAnalysis",. /
6ac30 2a 20 31 31 39 20 2a 2f 20 22 45 78 70 6c 61 69  * 119 */ "Explai
6ac40 6e 22 2c 0a 20 2f 2a 20 31 32 30 20 2a 2f 20 22  n",. /* 120 */ "
6ac50 49 66 4d 65 6d 5a 65 72 6f 22 2c 0a 20 2f 2a 20  IfMemZero",. /* 
6ac60 31 32 31 20 2a 2f 20 22 4f 70 65 6e 50 73 65 75  121 */ "OpenPseu
6ac70 64 6f 22 2c 0a 20 2f 2a 20 31 32 32 20 2a 2f 20  do",. /* 122 */ 
6ac80 22 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 22 2c  "OpenEphemeral",
6ac90 0a 20 2f 2a 20 31 32 33 20 2a 2f 20 22 4e 75 6c  . /* 123 */ "Nul
6aca0 6c 22 2c 0a 20 2f 2a 20 31 32 34 20 2a 2f 20 22  l",. /* 124 */ "
6acb0 42 6c 6f 62 22 2c 0a 20 2f 2a 20 31 32 35 20 2a  Blob",. /* 125 *
6acc0 2f 20 22 52 65 61 6c 22 2c 0a 20 2f 2a 20 31 32  / "Real",. /* 12
6acd0 36 20 2a 2f 20 22 48 65 78 42 6c 6f 62 22 2c 0a  6 */ "HexBlob",.
6ace0 20 2f 2a 20 31 32 37 20 2a 2f 20 22 4d 65 6d 53   /* 127 */ "MemS
6acf0 74 6f 72 65 22 2c 0a 20 2f 2a 20 31 32 38 20 2a  tore",. /* 128 *
6ad00 2f 20 22 52 65 77 69 6e 64 22 2c 0a 20 2f 2a 20  / "Rewind",. /* 
6ad10 31 32 39 20 2a 2f 20 22 4d 6f 76 65 47 65 22 2c  129 */ "MoveGe",
6ad20 0a 20 2f 2a 20 31 33 30 20 2a 2f 20 22 56 42 65  . /* 130 */ "VBe
6ad30 67 69 6e 22 2c 0a 20 2f 2a 20 31 33 31 20 2a 2f  gin",. /* 131 */
6ad40 20 22 56 55 70 64 61 74 65 22 2c 0a 20 2f 2a 20   "VUpdate",. /* 
6ad50 31 33 32 20 2a 2f 20 22 56 43 72 65 61 74 65 22  132 */ "VCreate"
6ad60 2c 0a 20 2f 2a 20 31 33 33 20 2a 2f 20 22 4d 65  ,. /* 133 */ "Me
6ad70 6d 4d 6f 76 65 22 2c 0a 20 2f 2a 20 31 33 34 20  mMove",. /* 134 
6ad80 2a 2f 20 22 4d 65 6d 4e 75 6c 6c 22 2c 0a 20 2f  */ "MemNull",. /
6ad90 2a 20 31 33 35 20 2a 2f 20 22 46 6f 75 6e 64 22  * 135 */ "Found"
6ada0 2c 0a 20 2f 2a 20 31 33 36 20 2a 2f 20 22 4e 75  ,. /* 136 */ "Nu
6adb0 6c 6c 52 6f 77 22 2c 0a 20 2f 2a 20 31 33 37 20  llRow",. /* 137 
6adc0 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 37 22  */ "NotUsed_137"
6add0 2c 0a 20 2f 2a 20 31 33 38 20 2a 2f 20 22 54 6f  ,. /* 138 */ "To
6ade0 54 65 78 74 22 2c 0a 20 2f 2a 20 31 33 39 20 2a  Text",. /* 139 *
6adf0 2f 20 22 54 6f 42 6c 6f 62 22 2c 0a 20 2f 2a 20  / "ToBlob",. /* 
6ae00 31 34 30 20 2a 2f 20 22 54 6f 4e 75 6d 65 72 69  140 */ "ToNumeri
6ae10 63 22 2c 0a 20 2f 2a 20 31 34 31 20 2a 2f 20 22  c",. /* 141 */ "
6ae20 54 6f 49 6e 74 22 2c 0a 20 2f 2a 20 31 34 32 20  ToInt",. /* 142 
6ae30 2a 2f 20 22 54 6f 52 65 61 6c 22 2c 0a 7d 3b 0a  */ "ToReal",.};.
6ae40 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  #endif../*******
6ae50 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f  ******* End of o
6ae60 70 63 6f 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a  pcodes.c *******
6ae70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6ae80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6ae90 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
6aea0 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
6aeb0 6c 65 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a  le os_os2.c ****
6aec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6aed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6aee0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
6aef0 30 36 20 46 65 62 20 31 34 0a 2a 2a 0a 2a 2a 20  06 Feb 14.**.** 
6af00 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
6af10 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
6af20 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
6af30 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
6af40 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
6af50 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
6af60 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
6af70 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
6af80 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
6af90 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
6afa0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
6afb0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
6afc0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
6afd0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
6afe0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
6aff0 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
6b000 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
6b010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6b020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6b030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6b040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6b050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
6b060 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
6b070 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 68 61 74  ntains code that
6b080 20 69 73 20 73 70 65 63 69 66 69 63 20 74 6f 20   is specific to 
6b090 4f 53 2f 32 2e 0a 2a 2f 0a 0a 0a 23 69 66 20 4f  OS/2..*/...#if O
6b0a0 53 5f 4f 53 32 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63  S_OS2../*.** Mac
6b0b0 72 6f 73 20 75 73 65 64 20 74 6f 20 64 65 74 65  ros used to dete
6b0c0 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72  rmine whether or
6b0d0 20 6e 6f 74 20 74 6f 20 75 73 65 20 74 68 72 65   not to use thre
6b0e0 61 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69  ads..*/.#if defi
6b0f0 6e 65 64 28 54 48 52 45 41 44 53 41 46 45 29 20  ned(THREADSAFE) 
6b100 26 26 20 54 48 52 45 41 44 53 41 46 45 0a 23 20  && THREADSAFE.# 
6b110 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53  define SQLITE_OS
6b120 32 5f 54 48 52 45 41 44 53 20 31 0a 23 65 6e 64  2_THREADS 1.#end
6b130 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64  if../*.** Includ
6b140 65 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63  e code that is c
6b150 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73 5f  ommon to all os_
6b160 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a 2a  *.c files.*/./**
6b170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63  ************ Inc
6b180 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68  lude os_common.h
6b190 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
6b1a0 66 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a  f os_os2.c *****
6b1b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
6b1c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
6b1d0 69 6e 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d 6f  in file os_commo
6b1e0 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.h ************
6b1f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6b200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
6b210 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a 2a  ** 2004 May 22.*
6b220 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
6b230 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
6b240 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
6b250 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
6b260 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
6b270 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
6b280 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
6b290 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
6b2a0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
6b2b0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
6b2c0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
6b2d0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
6b2e0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
6b2f0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
6b300 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
6b310 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
6b320 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
6b330 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
6b340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6b350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6b360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6b370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6b380 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  **.**.** This fi
6b390 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6d 61 63 72  le contains macr
6b3a0 6f 73 20 61 6e 64 20 61 20 6c 69 74 74 6c 65 20  os and a little 
6b3b0 62 69 74 20 6f 66 20 63 6f 64 65 20 74 68 61 74  bit of code that
6b3c0 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a 2a   is common to.**
6b3d0 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 6c 61 74   all of the plat
6b3e0 66 6f 72 6d 2d 73 70 65 63 69 66 69 63 20 66 69  form-specific fi
6b3f0 6c 65 73 20 28 6f 73 5f 2a 2e 63 29 20 61 6e 64  les (os_*.c) and
6b400 20 69 73 20 23 69 6e 63 6c 75 64 65 64 20 69 6e   is #included in
6b410 74 6f 20 74 68 6f 73 65 0a 2a 2a 20 66 69 6c 65  to those.** file
6b420 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  s..**.** This fi
6b430 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 23 69 6e  le should be #in
6b440 63 6c 75 64 65 64 20 62 79 20 74 68 65 20 6f 73  cluded by the os
6b450 5f 2a 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79 2e  _*.c files only.
6b460 20 20 49 74 20 69 73 20 6e 6f 74 20 61 0a 2a 2a    It is not a.**
6b470 20 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 65   general purpose
6b480 20 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2f   header file..*/
6b490 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 74  ../*.** At least
6b4a0 20 74 77 6f 20 62 75 67 73 20 68 61 76 65 20 73   two bugs have s
6b4b0 6c 69 70 70 65 64 20 69 6e 20 62 65 63 61 75 73  lipped in becaus
6b4c0 65 20 77 65 20 63 68 61 6e 67 65 64 20 74 68 65  e we changed the
6b4d0 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a 2a   MEMORY_DEBUG.**
6b4e0 20 6d 61 63 72 6f 20 74 6f 20 53 51 4c 49 54 45   macro to SQLITE
6b4f0 5f 44 45 42 55 47 20 61 6e 64 20 73 6f 6d 65 20  _DEBUG and some 
6b500 6f 6c 64 65 72 20 6d 61 6b 65 66 69 6c 65 73 20  older makefiles 
6b510 68 61 76 65 20 6e 6f 74 20 79 65 74 20 6d 61 64  have not yet mad
6b520 65 20 74 68 65 0a 2a 2a 20 73 77 69 74 63 68 2e  e the.** switch.
6b530 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
6b540 63 6f 64 65 20 73 68 6f 75 6c 64 20 63 61 74 63  code should catc
6b550 68 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 61  h this problem a
6b560 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a  t compile-time..
6b570 2a 2f 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52 59  */.#ifdef MEMORY
6b580 5f 44 45 42 55 47 0a 23 20 65 72 72 6f 72 20 22  _DEBUG.# error "
6b590 54 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47  The MEMORY_DEBUG
6b5a0 20 6d 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c 65   macro is obsole
6b5b0 74 65 2e 20 20 55 73 65 20 53 51 4c 49 54 45 5f  te.  Use SQLITE_
6b5c0 44 45 42 55 47 20 69 6e 73 74 65 61 64 2e 22 0a  DEBUG instead.".
6b5d0 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 20 2a 20 57  #endif.../*. * W
6b5e0 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 74 68 69  hen testing, thi
6b5f0 73 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c  s global variabl
6b600 65 20 73 74 6f 72 65 73 20 74 68 65 20 6c 6f 63  e stores the loc
6b610 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 20 2a 20  ation of the. * 
6b620 70 65 6e 64 69 6e 67 2d 62 79 74 65 20 69 6e 20  pending-byte in 
6b630 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
6b640 65 2e 0a 20 2a 2f 0a 23 69 66 64 65 66 20 53 51  e.. */.#ifdef SQ
6b650 4c 49 54 45 5f 54 45 53 54 0a 75 6e 73 69 67 6e  LITE_TEST.unsign
6b660 65 64 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70  ed int sqlite3_p
6b670 65 6e 64 69 6e 67 5f 62 79 74 65 20 3d 20 30 78  ending_byte = 0x
6b680 34 30 30 30 30 30 30 30 3b 0a 23 65 6e 64 69 66  40000000;.#endif
6b690 0a 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73  ..int sqlite3_os
6b6a0 5f 74 72 61 63 65 20 3d 20 30 3b 0a 23 69 66 64  _trace = 0;.#ifd
6b6b0 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
6b6c0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 31  #define OSTRACE1
6b6d0 28 58 29 20 20 20 20 20 20 20 20 20 69 66 28 20  (X)         if( 
6b6e0 73 71 6c 69 74 65 33 5f 6f 73 5f 74 72 61 63 65  sqlite3_os_trace
6b6f0 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50   ) sqlite3DebugP
6b700 72 69 6e 74 66 28 58 29 0a 23 64 65 66 69 6e 65  rintf(X).#define
6b710 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 20 20   OSTRACE2(X,Y)  
6b720 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
6b730 5f 6f 73 5f 74 72 61 63 65 20 29 20 73 71 6c 69  _os_trace ) sqli
6b740 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58  te3DebugPrintf(X
6b750 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,Y).#define OSTR
6b760 41 43 45 33 28 58 2c 59 2c 5a 29 20 20 20 20 20  ACE3(X,Y,Z)     
6b770 69 66 28 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74  if( sqlite3_os_t
6b780 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65  race ) sqlite3De
6b790 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 29  bugPrintf(X,Y,Z)
6b7a0 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
6b7b0 34 28 58 2c 59 2c 5a 2c 41 29 20 20 20 69 66 28  4(X,Y,Z,A)   if(
6b7c0 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 72 61 63   sqlite3_os_trac
6b7d0 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67  e ) sqlite3Debug
6b7e0 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 29 0a  Printf(X,Y,Z,A).
6b7f0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35  #define OSTRACE5
6b800 28 58 2c 59 2c 5a 2c 41 2c 42 29 20 69 66 28 20  (X,Y,Z,A,B) if( 
6b810 73 71 6c 69 74 65 33 5f 6f 73 5f 74 72 61 63 65  sqlite3_os_trace
6b820 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50   ) sqlite3DebugP
6b830 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 29  rintf(X,Y,Z,A,B)
6b840 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
6b850 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20 5c  6(X,Y,Z,A,B,C) \
6b860 0a 20 20 20 20 69 66 28 73 71 6c 69 74 65 33 5f  .    if(sqlite3_
6b870 6f 73 5f 74 72 61 63 65 29 20 73 71 6c 69 74 65  os_trace) sqlite
6b880 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59  3DebugPrintf(X,Y
6b890 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e  ,Z,A,B,C).#defin
6b8a0 65 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a  e OSTRACE7(X,Y,Z
6b8b0 2c 41 2c 42 2c 43 2c 44 29 20 5c 0a 20 20 20 20  ,A,B,C,D) \.    
6b8c0 69 66 28 73 71 6c 69 74 65 33 5f 6f 73 5f 74 72  if(sqlite3_os_tr
6b8d0 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 75  ace) sqlite3Debu
6b8e0 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c  gPrintf(X,Y,Z,A,
6b8f0 42 2c 43 2c 44 29 0a 23 65 6c 73 65 0a 23 64 65  B,C,D).#else.#de
6b900 66 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29  fine OSTRACE1(X)
6b910 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
6b920 32 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f  2(X,Y).#define O
6b930 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 0a 23  STRACE3(X,Y,Z).#
6b940 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28  define OSTRACE4(
6b950 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65  X,Y,Z,A).#define
6b960 20 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c   OSTRACE5(X,Y,Z,
6b970 41 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54  A,B).#define OST
6b980 52 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c  RACE6(X,Y,Z,A,B,
6b990 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  C).#define OSTRA
6b9a0 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c  CE7(X,Y,Z,A,B,C,
6b9b0 44 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  D).#endif../*.**
6b9c0 20 4d 61 63 72 6f 73 20 66 6f 72 20 70 65 72 66   Macros for perf
6b9d0 6f 72 6d 61 6e 63 65 20 74 72 61 63 69 6e 67 2e  ormance tracing.
6b9e0 20 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65    Normally turne
6b9f0 64 20 6f 66 66 2e 20 20 4f 6e 6c 79 20 77 6f 72  d off.  Only wor
6ba00 6b 73 0a 2a 2a 20 6f 6e 20 69 34 38 36 20 68 61  ks.** on i486 ha
6ba10 72 64 77 61 72 65 2e 0a 2a 2f 0a 23 69 66 64 65  rdware..*/.#ifde
6ba20 66 20 53 51 4c 49 54 45 5f 50 45 52 46 4f 52 4d  f SQLITE_PERFORM
6ba30 41 4e 43 45 5f 54 52 41 43 45 0a 5f 5f 69 6e 6c  ANCE_TRACE.__inl
6ba40 69 6e 65 5f 5f 20 75 6e 73 69 67 6e 65 64 20 6c  ine__ unsigned l
6ba50 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 20 68 77 74  ong long int hwt
6ba60 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 75 6e 73  ime(void){.  uns
6ba70 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20  igned long long 
6ba80 69 6e 74 20 78 3b 0a 20 20 5f 5f 61 73 6d 5f 5f  int x;.  __asm__
6ba90 28 22 72 64 74 73 63 5c 6e 5c 74 22 0a 20 20 20  ("rdtsc\n\t".   
6baa0 20 20 20 20 20 20 20 22 6d 6f 76 20 25 25 65 64         "mov %%ed
6bab0 78 2c 20 25 25 65 63 78 5c 6e 5c 74 22 0a 20 20  x, %%ecx\n\t".  
6bac0 20 20 20 20 20 20 20 20 3a 22 3d 41 22 20 28 78          :"=A" (x
6bad0 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 78 3b 0a  ));.  return x;.
6bae0 7d 0a 73 74 61 74 69 63 20 75 6e 73 69 67 6e 65  }.static unsigne
6baf0 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 20  d long long int 
6bb00 67 5f 73 74 61 72 74 3b 0a 73 74 61 74 69 63 20  g_start;.static 
6bb10 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 65 6c 61  unsigned int ela
6bb20 70 73 65 3b 0a 23 64 65 66 69 6e 65 20 54 49 4d  pse;.#define TIM
6bb30 45 52 5f 53 54 41 52 54 20 20 20 20 20 20 20 67  ER_START       g
6bb40 5f 73 74 61 72 74 3d 68 77 74 69 6d 65 28 29 0a  _start=hwtime().
6bb50 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e  #define TIMER_EN
6bb60 44 20 20 20 20 20 20 20 20 20 65 6c 61 70 73 65  D         elapse
6bb70 3d 68 77 74 69 6d 65 28 29 2d 67 5f 73 74 61 72  =hwtime()-g_star
6bb80 74 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f  t.#define TIMER_
6bb90 45 4c 41 50 53 45 44 20 20 20 20 20 65 6c 61 70  ELAPSED     elap
6bba0 73 65 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65  se.#else.#define
6bbb0 20 54 49 4d 45 52 5f 53 54 41 52 54 0a 23 64 65   TIMER_START.#de
6bbc0 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 0a 23  fine TIMER_END.#
6bbd0 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41  define TIMER_ELA
6bbe0 50 53 45 44 20 20 20 20 20 30 0a 23 65 6e 64 69  PSED     0.#endi
6bbf0 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 63  f../*.** If we c
6bc00 6f 6d 70 69 6c 65 20 77 69 74 68 20 74 68 65 20  ompile with the 
6bc10 53 51 4c 49 54 45 5f 54 45 53 54 20 6d 61 63 72  SQLITE_TEST macr
6bc20 6f 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20  o set, then the 
6bc30 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a  following block.
6bc40 2a 2a 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c 20  ** of code will 
6bc50 67 69 76 65 20 75 73 20 74 68 65 20 61 62 69 6c  give us the abil
6bc60 69 74 79 20 74 6f 20 73 69 6d 75 6c 61 74 65 20  ity to simulate 
6bc70 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72  a disk I/O error
6bc80 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73 20 75 73  .  This.** is us
6bc90 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 74  ed for testing t
6bca0 68 65 20 49 2f 4f 20 72 65 63 6f 76 65 72 79 20  he I/O recovery 
6bcb0 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69 66 64 65 66  logic..*/.#ifdef
6bcc0 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 69 6e 74   SQLITE_TEST.int
6bcd0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
6bce0 72 5f 68 69 74 20 3d 20 30 3b 0a 69 6e 74 20 73  r_hit = 0;.int s
6bcf0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
6bd00 70 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 69 6e 74  pending = 0;.int
6bd10 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
6bd20 72 5f 70 65 72 73 69 73 74 20 3d 20 30 3b 0a 69  r_persist = 0;.i
6bd30 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66  nt sqlite3_diskf
6bd40 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b  ull_pending = 0;
6bd50 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73  .int sqlite3_dis
6bd60 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23 64 65 66 69  kfull = 0;.#defi
6bd70 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  ne SimulateIOErr
6bd80 6f 72 28 43 4f 44 45 29 20 20 5c 0a 20 20 69 66  or(CODE)  \.  if
6bd90 28 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  ( sqlite3_io_err
6bda0 6f 72 5f 70 65 6e 64 69 6e 67 20 7c 7c 20 73 71  or_pending || sq
6bdb0 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68  lite3_io_error_h
6bdc0 69 74 20 29 20 5c 0a 20 20 20 20 20 69 66 28 20  it ) \.     if( 
6bdd0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
6bde0 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31 20  _pending-- == 1 
6bdf0 5c 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 28 73  \.         || (s
6be00 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
6be10 70 65 72 73 69 73 74 20 26 26 20 73 71 6c 69 74  persist && sqlit
6be20 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 29  e3_io_error_hit)
6be30 20 29 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20   ) \.           
6be40 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65       { local_ioe
6be50 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0a 73 74  rr(); CODE; }.st
6be60 61 74 69 63 20 76 6f 69 64 20 6c 6f 63 61 6c 5f  atic void local_
6be70 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f 54 52 41  ioerr(){.  IOTRA
6be80 43 45 28 28 22 49 4f 45 52 52 5c 6e 22 29 29 3b  CE(("IOERR\n"));
6be90 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  .  sqlite3_io_er
6bea0 72 6f 72 5f 68 69 74 20 3d 20 31 3b 0a 7d 0a 23  ror_hit = 1;.}.#
6beb0 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44  define SimulateD
6bec0 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 43 4f 44  iskfullError(COD
6bed0 45 29 20 5c 0a 20 20 20 69 66 28 20 73 71 6c 69  E) \.   if( sqli
6bee0 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e  te3_diskfull_pen
6bef0 64 69 6e 67 20 29 7b 20 5c 0a 20 20 20 20 20 69  ding ){ \.     i
6bf00 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66  f( sqlite3_diskf
6bf10 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d 20 31  ull_pending == 1
6bf20 20 29 7b 20 5c 0a 20 20 20 20 20 20 20 6c 6f 63   ){ \.       loc
6bf30 61 6c 5f 69 6f 65 72 72 28 29 3b 20 5c 0a 20 20  al_ioerr(); \.  
6bf40 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73       sqlite3_dis
6bf50 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20 20  kfull = 1; \.   
6bf60 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65      sqlite3_io_e
6bf70 72 72 6f 72 5f 68 69 74 20 3d 20 31 3b 20 5c 0a  rror_hit = 1; \.
6bf80 20 20 20 20 20 20 20 43 4f 44 45 3b 20 5c 0a 20         CODE; \. 
6bf90 20 20 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 20 20      }else{ \.   
6bfa0 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b      sqlite3_disk
6bfb0 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20  full_pending--; 
6bfc0 5c 0a 20 20 20 20 20 7d 20 5c 0a 20 20 20 7d 0a  \.     } \.   }.
6bfd0 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 53 69  #else.#define Si
6bfe0 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 41 29  mulateIOError(A)
6bff0 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74  .#define Simulat
6c000 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 41  eDiskfullError(A
6c010 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ).#endif../*.** 
6c020 57 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 6b 65  When testing, ke
6c030 65 70 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68  ep a count of th
6c040 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e  e number of open
6c050 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64 65   files..*/.#ifde
6c060 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 69 6e  f SQLITE_TEST.in
6c070 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66  t sqlite3_open_f
6c080 69 6c 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23  ile_count = 0;.#
6c090 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74  define OpenCount
6c0a0 65 72 28 58 29 20 20 73 71 6c 69 74 65 33 5f 6f  er(X)  sqlite3_o
6c0b0 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 2b 3d  pen_file_count+=
6c0c0 28 58 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e  (X).#else.#defin
6c0d0 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29  e OpenCounter(X)
6c0e0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 73  .#endif../*.** s
6c0f0 71 6c 69 74 65 33 47 65 6e 65 72 69 63 4d 61 6c  qlite3GenericMal
6c100 6c 6f 63 0a 2a 2a 20 73 71 6c 69 74 65 33 47 65  loc.** sqlite3Ge
6c110 6e 65 72 69 63 52 65 61 6c 6c 6f 63 0a 2a 2a 20  nericRealloc.** 
6c120 73 71 6c 69 74 65 33 47 65 6e 65 72 69 63 4f 73  sqlite3GenericOs
6c130 46 72 65 65 0a 2a 2a 20 73 71 6c 69 74 65 33 47  Free.** sqlite3G
6c140 65 6e 65 72 69 63 41 6c 6c 6f 63 61 74 69 6f 6e  enericAllocation
6c150 53 69 7a 65 0a 2a 2a 0a 2a 2a 20 49 6d 70 6c 65  Size.**.** Imple
6c160 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
6c170 20 6f 73 20 6c 65 76 65 6c 20 64 79 6e 61 6d 69   os level dynami
6c180 63 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  c memory allocat
6c190 69 6f 6e 20 69 6e 74 65 72 66 61 63 65 20 69 6e  ion interface in
6c1a0 20 74 65 72 6d 73 0a 2a 2a 20 6f 66 20 74 68 65   terms.** of the
6c1b0 20 73 74 61 6e 64 61 72 64 20 6d 61 6c 6c 6f 63   standard malloc
6c1c0 28 29 2c 20 72 65 61 6c 6c 6f 63 28 29 20 61 6e  (), realloc() an
6c1d0 64 20 66 72 65 65 28 29 20 66 6f 75 6e 64 20 69  d free() found i
6c1e0 6e 20 6d 61 6e 79 20 6f 70 65 72 61 74 69 6e 67  n many operating
6c1f0 0a 2a 2a 20 73 79 73 74 65 6d 73 2e 20 4e 6f 20  .** systems. No 
6c200 72 6f 63 6b 65 74 20 73 63 69 65 6e 63 65 20 68  rocket science h
6c210 65 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65  ere..**.** There
6c220 20 61 72 65 20 74 77 6f 20 76 65 72 73 69 6f 6e   are two version
6c230 73 20 6f 66 20 74 68 65 73 65 20 66 6f 75 72 20  s of these four 
6c240 66 75 6e 63 74 69 6f 6e 73 20 68 65 72 65 2e 20  functions here. 
6c250 54 68 65 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 69  The version.** i
6c260 6d 70 6c 65 6d 65 6e 74 65 64 20 68 65 72 65 20  mplemented here 
6c270 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20  is only used if 
6c280 6d 65 6d 6f 72 79 2d 6d 61 6e 61 67 65 6d 65 6e  memory-managemen
6c290 74 20 6f 72 20 6d 65 6d 6f 72 79 2d 64 65 62 75  t or memory-debu
6c2a0 67 67 69 6e 67 20 69 73 0a 2a 2a 20 65 6e 61 62  gging is.** enab
6c2b0 6c 65 64 2e 20 54 68 69 73 20 76 65 72 73 69 6f  led. This versio
6c2c0 6e 20 61 6c 6c 6f 63 61 74 65 73 20 61 6e 20 65  n allocates an e
6c2d0 78 74 72 61 20 38 2d 62 79 74 65 73 20 61 74 20  xtra 8-bytes at 
6c2e0 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
6c2f0 20 65 61 63 68 0a 2a 2a 20 62 6c 6f 63 6b 20 61   each.** block a
6c300 6e 64 20 73 74 6f 72 65 73 20 74 68 65 20 73 69  nd stores the si
6c310 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61  ze of the alloca
6c320 74 69 6f 6e 20 74 68 65 72 65 2e 0a 2a 2a 0a 2a  tion there..**.*
6c330 2a 20 49 66 20 6e 65 69 74 68 65 72 20 6d 65 6d  * If neither mem
6c340 6f 72 79 2d 6d 61 6e 61 67 65 6d 65 6e 74 20 6f  ory-management o
6c350 72 20 64 65 62 75 67 67 69 6e 67 20 69 73 20 65  r debugging is e
6c360 6e 61 62 6c 65 64 2c 20 74 68 65 20 73 65 63 6f  nabled, the seco
6c370 6e 64 0a 2a 2a 20 73 65 74 20 6f 66 20 69 6d 70  nd.** set of imp
6c380 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 69 73 20  lementations is 
6c390 75 73 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2f  used instead..*/
6c3a0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
6c3b0 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52  ITE_ENABLE_MEMOR
6c3c0 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 29 20 7c 7c  Y_MANAGEMENT) ||
6c3d0 20 64 65 66 69 6e 65 64 20 28 53 51 4c 49 54 45   defined (SQLITE
6c3e0 5f 4d 45 4d 44 45 42 55 47 29 0a 53 51 4c 49 54  _MEMDEBUG).SQLIT
6c3f0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
6c400 73 71 6c 69 74 65 33 47 65 6e 65 72 69 63 4d 61  sqlite3GenericMa
6c410 6c 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 20 63  lloc(int n){.  c
6c420 68 61 72 20 2a 70 20 3d 20 28 63 68 61 72 20 2a  har *p = (char *
6c430 29 6d 61 6c 6c 6f 63 28 6e 2b 38 29 3b 0a 20 20  )malloc(n+8);.  
6c440 61 73 73 65 72 74 28 6e 3e 30 29 3b 0a 20 20 61  assert(n>0);.  a
6c450 73 73 65 72 74 28 73 69 7a 65 6f 66 28 69 6e 74  ssert(sizeof(int
6c460 29 3c 3d 38 29 3b 0a 20 20 69 66 28 20 70 20 29  )<=8);.  if( p )
6c470 7b 0a 20 20 20 20 2a 28 69 6e 74 20 2a 29 70 20  {.    *(int *)p 
6c480 3d 20 6e 3b 0a 20 20 20 20 70 20 2b 3d 20 38 3b  = n;.    p += 8;
6c490 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 76  .  }.  return (v
6c4a0 6f 69 64 20 2a 29 70 3b 0a 7d 0a 53 51 4c 49 54  oid *)p;.}.SQLIT
6c4b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
6c4c0 73 71 6c 69 74 65 33 47 65 6e 65 72 69 63 52 65  sqlite3GenericRe
6c4d0 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 2c 20 69  alloc(void *p, i
6c4e0 6e 74 20 6e 29 7b 0a 20 20 63 68 61 72 20 2a 70  nt n){.  char *p
6c4f0 32 20 3d 20 28 28 63 68 61 72 20 2a 29 70 20 2d  2 = ((char *)p -
6c500 20 38 29 3b 0a 20 20 61 73 73 65 72 74 28 6e 3e   8);.  assert(n>
6c510 30 29 3b 0a 20 20 70 32 20 3d 20 28 63 68 61 72  0);.  p2 = (char
6c520 2a 29 72 65 61 6c 6c 6f 63 28 70 32 2c 20 6e 2b  *)realloc(p2, n+
6c530 38 29 3b 0a 20 20 69 66 28 20 70 32 20 29 7b 0a  8);.  if( p2 ){.
6c540 20 20 20 20 2a 28 69 6e 74 20 2a 29 70 32 20 3d      *(int *)p2 =
6c550 20 6e 3b 0a 20 20 20 20 70 32 20 2b 3d 20 38 3b   n;.    p2 += 8;
6c560 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 76  .  }.  return (v
6c570 6f 69 64 20 2a 29 70 32 3b 0a 7d 0a 53 51 4c 49  oid *)p2;.}.SQLI
6c580 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6c590 73 71 6c 69 74 65 33 47 65 6e 65 72 69 63 46 72  sqlite3GenericFr
6c5a0 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 61  ee(void *p){.  a
6c5b0 73 73 65 72 74 28 70 29 3b 0a 20 20 66 72 65 65  ssert(p);.  free
6c5c0 28 28 76 6f 69 64 20 2a 29 28 28 63 68 61 72 20  ((void *)((char 
6c5d0 2a 29 70 20 2d 20 38 29 29 3b 0a 7d 0a 53 51 4c  *)p - 8));.}.SQL
6c5e0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6c5f0 73 71 6c 69 74 65 33 47 65 6e 65 72 69 63 41 6c  sqlite3GenericAl
6c600 6c 6f 63 61 74 69 6f 6e 53 69 7a 65 28 76 6f 69  locationSize(voi
6c610 64 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20  d *p){.  return 
6c620 70 20 3f 20 2a 28 69 6e 74 20 2a 29 28 28 63 68  p ? *(int *)((ch
6c630 61 72 20 2a 29 70 20 2d 20 38 29 20 3a 20 30 3b  ar *)p - 8) : 0;
6c640 0a 7d 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f  .}.#else.SQLITE_
6c650 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
6c660 6c 69 74 65 33 47 65 6e 65 72 69 63 4d 61 6c 6c  lite3GenericMall
6c670 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 20 63 68 61  oc(int n){.  cha
6c680 72 20 2a 70 20 3d 20 28 63 68 61 72 20 2a 29 6d  r *p = (char *)m
6c690 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 72 65 74 75  alloc(n);.  retu
6c6a0 72 6e 20 28 76 6f 69 64 20 2a 29 70 3b 0a 7d 0a  rn (void *)p;.}.
6c6b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6c6c0 6f 69 64 20 2a 73 71 6c 69 74 65 33 47 65 6e 65  oid *sqlite3Gene
6c6d0 72 69 63 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20  ricRealloc(void 
6c6e0 2a 70 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 61 73  *p, int n){.  as
6c6f0 73 65 72 74 28 6e 3e 30 29 3b 0a 20 20 70 20 3d  sert(n>0);.  p =
6c700 20 72 65 61 6c 6c 6f 63 28 70 2c 20 6e 29 3b 0a   realloc(p, n);.
6c710 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 53 51    return p;.}.SQ
6c720 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6c730 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72 69 63  d sqlite3Generic
6c740 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20  Free(void *p){. 
6c750 20 61 73 73 65 72 74 28 70 29 3b 0a 20 20 66 72   assert(p);.  fr
6c760 65 65 28 70 29 3b 0a 7d 0a 2f 2a 20 4e 65 76 65  ee(p);.}./* Neve
6c770 72 20 61 63 74 75 61 6c 6c 79 20 75 73 65 64 2c  r actually used,
6c780 20 62 75 74 20 6e 65 65 64 65 64 20 66 6f 72 20   but needed for 
6c790 74 68 65 20 6c 69 6e 6b 65 72 20 2a 2f 0a 53 51  the linker */.SQ
6c7a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
6c7b0 20 73 71 6c 69 74 65 33 47 65 6e 65 72 69 63 41   sqlite3GenericA
6c7c0 6c 6c 6f 63 61 74 69 6f 6e 53 69 7a 65 28 76 6f  llocationSize(vo
6c7d0 69 64 20 2a 70 29 7b 20 72 65 74 75 72 6e 20 30  id *p){ return 0
6c7e0 3b 20 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ; }.#endif../*.*
6c7f0 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 73 69  * The default si
6c800 7a 65 20 6f 66 20 61 20 64 69 73 6b 20 73 65 63  ze of a disk sec
6c810 74 6f 72 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 50  tor.*/.#ifndef P
6c820 41 47 45 52 5f 53 45 43 54 4f 52 5f 53 49 5a 45  AGER_SECTOR_SIZE
6c830 0a 23 20 64 65 66 69 6e 65 20 50 41 47 45 52 5f  .# define PAGER_
6c840 53 45 43 54 4f 52 5f 53 49 5a 45 20 35 31 32 0a  SECTOR_SIZE 512.
6c850 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  #endif../*******
6c860 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f  ******* End of o
6c870 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a  s_common.h *****
6c880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6c890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6c8a0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
6c8b0 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69  ******* Continui
6c8c0 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74  ng where we left
6c8d0 20 6f 66 66 20 69 6e 20 6f 73 5f 6f 73 32 2e 63   off in os_os2.c
6c8e0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
6c8f0 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  ******/../*.** T
6c900 68 65 20 6f 73 32 46 69 6c 65 20 73 74 72 75 63  he os2File struc
6c910 74 75 72 65 20 69 73 20 73 75 62 63 6c 61 73 73  ture is subclass
6c920 20 6f 66 20 4f 73 46 69 6c 65 20 73 70 65 63 69   of OsFile speci
6c930 66 69 63 20 66 6f 72 20 74 68 65 20 4f 53 2f 32  fic for the OS/2
6c940 0a 2a 2a 20 70 72 6f 74 61 62 69 6c 69 74 79 20  .** protability 
6c950 6c 61 79 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65  layer..*/.typede
6c960 66 20 73 74 72 75 63 74 20 6f 73 32 46 69 6c 65  f struct os2File
6c970 20 6f 73 32 46 69 6c 65 3b 0a 73 74 72 75 63 74   os2File;.struct
6c980 20 6f 73 32 46 69 6c 65 20 7b 0a 20 20 49 6f 4d   os2File {.  IoM
6c990 65 74 68 6f 64 20 63 6f 6e 73 74 20 2a 70 4d 65  ethod const *pMe
6c9a0 74 68 6f 64 3b 20 20 2f 2a 20 41 6c 77 61 79 73  thod;  /* Always
6c9b0 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79   the first entry
6c9c0 20 2a 2f 0a 20 20 48 46 49 4c 45 20 68 3b 20 20   */.  HFILE h;  
6c9d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6c9e0 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72 20 61 63  /* Handle for ac
6c9f0 63 65 73 73 69 6e 67 20 74 68 65 20 66 69 6c 65  cessing the file
6ca00 20 2a 2f 0a 20 20 69 6e 74 20 64 65 6c 4f 6e 43   */.  int delOnC
6ca10 6c 6f 73 65 3b 20 20 20 20 20 20 20 20 20 20 20  lose;           
6ca20 2f 2a 20 54 72 75 65 20 69 66 20 66 69 6c 65 20  /* True if file 
6ca30 69 73 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64  is to be deleted
6ca40 20 6f 6e 20 63 6c 6f 73 65 20 2a 2f 0a 20 20 63   on close */.  c
6ca50 68 61 72 2a 20 70 61 74 68 54 6f 44 65 6c 3b 20  har* pathToDel; 
6ca60 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
6ca70 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c 65   of file to dele
6ca80 74 65 20 6f 6e 20 63 6c 6f 73 65 20 2a 2f 0a 20  te on close */. 
6ca90 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6c   unsigned char l
6caa0 6f 63 6b 74 79 70 65 3b 20 20 20 2f 2a 20 54 79  ocktype;   /* Ty
6cab0 70 65 20 6f 66 20 6c 6f 63 6b 20 63 75 72 72 65  pe of lock curre
6cac0 6e 74 6c 79 20 68 65 6c 64 20 6f 6e 20 74 68 69  ntly held on thi
6cad0 73 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  s file */.};../*
6cae0 0a 2a 2a 20 44 6f 20 6e 6f 74 20 69 6e 63 6c 75  .** Do not inclu
6caf0 64 65 20 61 6e 79 20 6f 66 20 74 68 65 20 46 69  de any of the Fi
6cb00 6c 65 20 49 2f 4f 20 69 6e 74 65 72 66 61 63 65  le I/O interface
6cb10 20 70 72 6f 63 65 64 75 72 65 73 20 69 66 20 74   procedures if t
6cb20 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  he.** SQLITE_OMI
6cb30 54 5f 44 49 53 4b 49 4f 20 6d 61 63 72 6f 20 69  T_DISKIO macro i
6cb40 73 20 64 65 66 69 6e 65 64 20 28 69 6e 64 69 63  s defined (indic
6cb50 61 74 69 6e 67 20 74 68 61 74 20 74 68 65 72 65  ating that there
6cb60 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 77 69 6c   database.** wil
6cb70 6c 20 62 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 6f  l be in-memory o
6cb80 6e 6c 79 29 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  nly).*/.#ifndef 
6cb90 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b  SQLITE_OMIT_DISK
6cba0 49 4f 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65  IO../*.** Delete
6cbb0 20 74 68 65 20 6e 61 6d 65 64 20 66 69 6c 65 0a   the named file.
6cbc0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
6cbd0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 32  E int sqlite3Os2
6cbe0 44 65 6c 65 74 65 28 20 63 6f 6e 73 74 20 63 68  Delete( const ch
6cbf0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 20 29 7b  ar *zFilename ){
6cc00 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e  .  APIRET rc = N
6cc10 4f 5f 45 52 52 4f 52 3b 0a 0a 20 20 72 63 20 3d  O_ERROR;..  rc =
6cc20 20 44 6f 73 44 65 6c 65 74 65 28 20 28 50 53 5a   DosDelete( (PSZ
6cc30 29 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 20 20  )zFilename );.  
6cc40 4f 53 54 52 41 43 45 32 28 20 22 44 45 4c 45 54  OSTRACE2( "DELET
6cc50 45 20 5c 22 25 73 5c 22 5c 6e 22 2c 20 7a 46 69  E \"%s\"\n", zFi
6cc60 6c 65 6e 61 6d 65 20 29 3b 0a 20 20 72 65 74 75  lename );.  retu
6cc70 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f  rn rc == NO_ERRO
6cc80 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20  R ? SQLITE_OK : 
6cc90 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 7d 0a  SQLITE_IOERR;.}.
6cca0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52  ./*.** Return TR
6ccb0 55 45 20 69 66 20 74 68 65 20 6e 61 6d 65 64 20  UE if the named 
6ccc0 66 69 6c 65 20 65 78 69 73 74 73 2e 0a 2a 2f 0a  file exists..*/.
6ccd0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6cce0 6e 74 20 73 71 6c 69 74 65 33 4f 73 32 46 69 6c  nt sqlite3Os2Fil
6ccf0 65 45 78 69 73 74 73 28 20 63 6f 6e 73 74 20 63  eExists( const c
6cd00 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 20 29  har *zFilename )
6cd10 7b 0a 20 20 46 49 4c 45 53 54 41 54 55 53 33 20  {.  FILESTATUS3 
6cd20 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 3b  fsts3ConfigInfo;
6cd30 0a 20 20 6d 65 6d 73 65 74 28 26 66 73 74 73 33  .  memset(&fsts3
6cd40 43 6f 6e 66 69 67 49 6e 66 6f 2c 20 30 2c 20 73  ConfigInfo, 0, s
6cd50 69 7a 65 6f 66 28 66 73 74 73 33 43 6f 6e 66 69  izeof(fsts3Confi
6cd60 67 49 6e 66 6f 29 29 3b 0a 20 20 72 65 74 75 72  gInfo));.  retur
6cd70 6e 20 44 6f 73 51 75 65 72 79 50 61 74 68 49 6e  n DosQueryPathIn
6cd80 66 6f 28 20 28 50 53 5a 29 7a 46 69 6c 65 6e 61  fo( (PSZ)zFilena
6cd90 6d 65 2c 20 46 49 4c 5f 53 54 41 4e 44 41 52 44  me, FIL_STANDARD
6cda0 2c 0a 20 20 20 20 20 20 20 20 26 66 73 74 73 33  ,.        &fsts3
6cdb0 43 6f 6e 66 69 67 49 6e 66 6f 2c 20 73 69 7a 65  ConfigInfo, size
6cdc0 6f 66 28 46 49 4c 45 53 54 41 54 55 53 33 29 20  of(FILESTATUS3) 
6cdd0 29 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 7d  ) == NO_ERROR;.}
6cde0 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63  ../* Forward dec
6cdf0 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a 69 6e 74 20  laration */.int 
6ce00 61 6c 6c 6f 63 61 74 65 4f 73 32 46 69 6c 65 28  allocateOs2File(
6ce10 20 6f 73 32 46 69 6c 65 20 2a 70 49 6e 69 74 2c   os2File *pInit,
6ce20 20 4f 73 46 69 6c 65 20 2a 2a 70 6c 64 20 29 3b   OsFile **pld );
6ce30 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20  ../*.** Attempt 
6ce40 74 6f 20 6f 70 65 6e 20 61 20 66 69 6c 65 20 66  to open a file f
6ce50 6f 72 20 62 6f 74 68 20 72 65 61 64 69 6e 67 20  or both reading 
6ce60 61 6e 64 20 77 72 69 74 69 6e 67 2e 20 20 49 66  and writing.  If
6ce70 20 74 68 61 74 0a 2a 2a 20 66 61 69 6c 73 2c 20   that.** fails, 
6ce80 74 72 79 20 6f 70 65 6e 69 6e 67 20 69 74 20 72  try opening it r
6ce90 65 61 64 2d 6f 6e 6c 79 2e 20 20 49 66 20 74 68  ead-only.  If th
6cea0 65 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20  e file does not 
6ceb0 65 78 69 73 74 2c 0a 2a 2a 20 74 72 79 20 74 6f  exist,.** try to
6cec0 20 63 72 65 61 74 65 20 69 74 2e 0a 2a 2a 0a 2a   create it..**.*
6ced0 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 61 20  * On success, a 
6cee0 68 61 6e 64 6c 65 20 66 6f 72 20 74 68 65 20 6f  handle for the o
6cef0 70 65 6e 20 66 69 6c 65 20 69 73 20 77 72 69 74  pen file is writ
6cf00 74 65 6e 20 74 6f 20 2a 69 64 0a 2a 2a 20 61 6e  ten to *id.** an
6cf10 64 20 2a 70 52 65 61 64 6f 6e 6c 79 20 69 73 20  d *pReadonly is 
6cf20 73 65 74 20 74 6f 20 30 20 69 66 20 74 68 65 20  set to 0 if the 
6cf30 66 69 6c 65 20 77 61 73 20 6f 70 65 6e 65 64 20  file was opened 
6cf40 66 6f 72 20 72 65 61 64 69 6e 67 20 61 6e 64 0a  for reading and.
6cf50 2a 2a 20 77 72 69 74 69 6e 67 20 6f 72 20 31 20  ** writing or 1 
6cf60 69 66 20 74 68 65 20 66 69 6c 65 20 77 61 73 20  if the file was 
6cf70 6f 70 65 6e 65 64 20 72 65 61 64 2d 6f 6e 6c 79  opened read-only
6cf80 2e 20 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20  .  The function 
6cf90 72 65 74 75 72 6e 73 0a 2a 2a 20 53 51 4c 49 54  returns.** SQLIT
6cfa0 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 66  E_OK..**.** On f
6cfb0 61 69 6c 75 72 65 2c 20 74 68 65 20 66 75 6e 63  ailure, the func
6cfc0 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 53 51 4c  tion returns SQL
6cfd0 49 54 45 5f 43 41 4e 54 4f 50 45 4e 20 61 6e 64  ITE_CANTOPEN and
6cfe0 20 6c 65 61 76 65 73 0a 2a 2a 20 2a 69 64 20 61   leaves.** *id a
6cff0 6e 64 20 2a 70 52 65 61 64 6f 6e 6c 79 20 75 6e  nd *pReadonly un
6d000 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49  changed..*/.SQLI
6d010 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6d020 71 6c 69 74 65 33 4f 73 32 4f 70 65 6e 52 65 61  qlite3Os2OpenRea
6d030 64 57 72 69 74 65 28 0a 20 20 63 6f 6e 73 74 20  dWrite(.  const 
6d040 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c  char *zFilename,
6d050 0a 20 20 4f 73 46 69 6c 65 20 2a 2a 70 6c 64 2c  .  OsFile **pld,
6d060 0a 20 20 69 6e 74 20 2a 70 52 65 61 64 6f 6e 6c  .  int *pReadonl
6d070 79 0a 29 7b 0a 20 20 6f 73 32 46 69 6c 65 20 20  y.){.  os2File  
6d080 66 3b 0a 20 20 48 46 49 4c 45 20 20 20 20 68 66  f;.  HFILE    hf
6d090 3b 0a 20 20 55 4c 4f 4e 47 20 20 20 20 75 6c 41  ;.  ULONG    ulA
6d0a0 63 74 69 6f 6e 3b 0a 20 20 41 50 49 52 45 54 20  ction;.  APIRET 
6d0b0 20 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b    rc = NO_ERROR;
6d0c0 0a 0a 20 20 61 73 73 65 72 74 28 20 2a 70 6c 64  ..  assert( *pld
6d0d0 20 3d 3d 20 30 20 29 3b 0a 20 20 72 63 20 3d 20   == 0 );.  rc = 
6d0e0 44 6f 73 4f 70 65 6e 28 20 28 50 53 5a 29 7a 46  DosOpen( (PSZ)zF
6d0f0 69 6c 65 6e 61 6d 65 2c 20 26 68 66 2c 20 26 75  ilename, &hf, &u
6d100 6c 41 63 74 69 6f 6e 2c 20 30 4c 2c 0a 20 20 20  lAction, 0L,.   
6d110 20 20 20 20 20 20 20 20 20 46 49 4c 45 5f 41 52           FILE_AR
6d120 43 48 49 56 45 44 20 7c 20 46 49 4c 45 5f 4e 4f  CHIVED | FILE_NO
6d130 52 4d 41 4c 2c 0a 20 20 20 20 20 20 20 20 20 20  RMAL,.          
6d140 20 20 20 20 20 20 4f 50 45 4e 5f 41 43 54 49 4f        OPEN_ACTIO
6d150 4e 5f 43 52 45 41 54 45 5f 49 46 5f 4e 45 57 20  N_CREATE_IF_NEW 
6d160 7c 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 4f 50  | OPEN_ACTION_OP
6d170 45 4e 5f 49 46 5f 45 58 49 53 54 53 2c 0a 20 20  EN_IF_EXISTS,.  
6d180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f 50                OP
6d190 45 4e 5f 46 4c 41 47 53 5f 46 41 49 4c 5f 4f 4e  EN_FLAGS_FAIL_ON
6d1a0 5f 45 52 52 4f 52 20 7c 20 4f 50 45 4e 5f 46 4c  _ERROR | OPEN_FL
6d1b0 41 47 53 5f 52 41 4e 44 4f 4d 20 7c 0a 20 20 20  AGS_RANDOM |.   
6d1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d1d0 20 4f 50 45 4e 5f 53 48 41 52 45 5f 44 45 4e 59   OPEN_SHARE_DENY
6d1e0 4e 4f 4e 45 20 7c 20 4f 50 45 4e 5f 41 43 43 45  NONE | OPEN_ACCE
6d1f0 53 53 5f 52 45 41 44 57 52 49 54 45 2c 20 28 50  SS_READWRITE, (P
6d200 45 41 4f 50 32 29 4e 55 4c 4c 20 29 3b 0a 20 20  EAOP2)NULL );.  
6d210 69 66 28 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52  if( rc != NO_ERR
6d220 4f 52 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 44  OR ){.    rc = D
6d230 6f 73 4f 70 65 6e 28 20 28 50 53 5a 29 7a 46 69  osOpen( (PSZ)zFi
6d240 6c 65 6e 61 6d 65 2c 20 26 68 66 2c 20 26 75 6c  lename, &hf, &ul
6d250 41 63 74 69 6f 6e 2c 20 30 4c 2c 0a 20 20 20 20  Action, 0L,.    
6d260 20 20 20 20 20 20 20 20 46 49 4c 45 5f 41 52 43          FILE_ARC
6d270 48 49 56 45 44 20 7c 20 46 49 4c 45 5f 4e 4f 52  HIVED | FILE_NOR
6d280 4d 41 4c 2c 0a 20 20 20 20 20 20 20 20 20 20 20  MAL,.           
6d290 20 20 20 20 20 4f 50 45 4e 5f 41 43 54 49 4f 4e       OPEN_ACTION
6d2a0 5f 43 52 45 41 54 45 5f 49 46 5f 4e 45 57 20 7c  _CREATE_IF_NEW |
6d2b0 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 4f 50 45   OPEN_ACTION_OPE
6d2c0 4e 5f 49 46 5f 45 58 49 53 54 53 2c 0a 20 20 20  N_IF_EXISTS,.   
6d2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 4f 50 45               OPE
6d2e0 4e 5f 46 4c 41 47 53 5f 46 41 49 4c 5f 4f 4e 5f  N_FLAGS_FAIL_ON_
6d2f0 45 52 52 4f 52 20 7c 20 4f 50 45 4e 5f 46 4c 41  ERROR | OPEN_FLA
6d300 47 53 5f 52 41 4e 44 4f 4d 20 7c 0a 20 20 20 20  GS_RANDOM |.    
6d310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d320 20 20 20 20 4f 50 45 4e 5f 53 48 41 52 45 5f 44      OPEN_SHARE_D
6d330 45 4e 59 57 52 49 54 45 20 7c 20 4f 50 45 4e 5f  ENYWRITE | OPEN_
6d340 41 43 43 45 53 53 5f 52 45 41 44 4f 4e 4c 59 2c  ACCESS_READONLY,
6d350 20 28 50 45 41 4f 50 32 29 4e 55 4c 4c 20 29 3b   (PEAOP2)NULL );
6d360 0a 20 20 20 20 69 66 28 20 72 63 20 21 3d 20 4e  .    if( rc != N
6d370 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20  O_ERROR ){.     
6d380 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
6d390 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d  _CANTOPEN;.    }
6d3a0 0a 20 20 20 20 2a 70 52 65 61 64 6f 6e 6c 79 20  .    *pReadonly 
6d3b0 3d 20 31 3b 0a 20 20 7d 0a 20 20 65 6c 73 65 7b  = 1;.  }.  else{
6d3c0 0a 20 20 20 20 2a 70 52 65 61 64 6f 6e 6c 79 20  .    *pReadonly 
6d3d0 3d 20 30 3b 0a 20 20 7d 0a 20 20 66 2e 68 20 3d  = 0;.  }.  f.h =
6d3e0 20 68 66 3b 0a 20 20 66 2e 6c 6f 63 6b 74 79 70   hf;.  f.locktyp
6d3f0 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 66  e = NO_LOCK;.  f
6d400 2e 64 65 6c 4f 6e 43 6c 6f 73 65 20 3d 20 30 3b  .delOnClose = 0;
6d410 0a 20 20 66 2e 70 61 74 68 54 6f 44 65 6c 20 3d  .  f.pathToDel =
6d420 20 4e 55 4c 4c 3b 0a 20 20 4f 70 65 6e 43 6f 75   NULL;.  OpenCou
6d430 6e 74 65 72 28 2b 31 29 3b 0a 20 20 4f 53 54 52  nter(+1);.  OSTR
6d440 41 43 45 33 28 20 22 4f 50 45 4e 20 52 2f 57 20  ACE3( "OPEN R/W 
6d450 25 64 20 5c 22 25 73 5c 22 5c 6e 22 2c 20 68 66  %d \"%s\"\n", hf
6d460 2c 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 20  , zFilename );. 
6d470 20 72 65 74 75 72 6e 20 61 6c 6c 6f 63 61 74 65   return allocate
6d480 4f 73 32 46 69 6c 65 28 20 26 66 2c 20 70 6c 64  Os2File( &f, pld
6d490 20 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 74   );.}.../*.** At
6d4a0 74 65 6d 70 74 20 74 6f 20 6f 70 65 6e 20 61 20  tempt to open a 
6d4b0 6e 65 77 20 66 69 6c 65 20 66 6f 72 20 65 78 63  new file for exc
6d4c0 6c 75 73 69 76 65 20 61 63 63 65 73 73 20 62 79  lusive access by
6d4d0 20 74 68 69 73 20 70 72 6f 63 65 73 73 2e 0a 2a   this process..*
6d4e0 2a 20 54 68 65 20 66 69 6c 65 20 77 69 6c 6c 20  * The file will 
6d4f0 62 65 20 6f 70 65 6e 65 64 20 66 6f 72 20 62 6f  be opened for bo
6d500 74 68 20 72 65 61 64 69 6e 67 20 61 6e 64 20 77  th reading and w
6d510 72 69 74 69 6e 67 2e 20 20 54 6f 20 61 76 6f 69  riting.  To avoi
6d520 64 0a 2a 2a 20 61 20 70 6f 74 65 6e 74 69 61 6c  d.** a potential
6d530 20 73 65 63 75 72 69 74 79 20 70 72 6f 62 6c 65   security proble
6d540 6d 2c 20 77 65 20 64 6f 20 6e 6f 74 20 61 6c 6c  m, we do not all
6d550 6f 77 20 74 68 65 20 66 69 6c 65 20 74 6f 20 68  ow the file to h
6d560 61 76 65 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c  ave.** previousl
6d570 79 20 65 78 69 73 74 65 64 2e 20 20 4e 6f 72 20  y existed.  Nor 
6d580 64 6f 20 77 65 20 61 6c 6c 6f 77 20 74 68 65 20  do we allow the 
6d590 66 69 6c 65 20 74 6f 20 62 65 20 61 20 73 79 6d  file to be a sym
6d5a0 62 6f 6c 69 63 0a 2a 2a 20 6c 69 6e 6b 2e 0a 2a  bolic.** link..*
6d5b0 2a 0a 2a 2a 20 49 66 20 64 65 6c 46 6c 61 67 20  *.** If delFlag 
6d5c0 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 6d 61  is true, then ma
6d5d0 6b 65 20 61 72 72 61 6e 67 65 6d 65 6e 74 73 20  ke arrangements 
6d5e0 74 6f 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  to automatically
6d5f0 20 64 65 6c 65 74 65 0a 2a 2a 20 74 68 65 20 66   delete.** the f
6d600 69 6c 65 20 77 68 65 6e 20 69 74 20 69 73 20 63  ile when it is c
6d610 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20  losed..**.** On 
6d620 73 75 63 63 65 73 73 2c 20 77 72 69 74 65 20 74  success, write t
6d630 68 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 69  he file handle i
6d640 6e 74 6f 20 2a 69 64 20 61 6e 64 20 72 65 74 75  nto *id and retu
6d650 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a  rn SQLITE_OK..**
6d660 0a 2a 2a 20 4f 6e 20 66 61 69 6c 75 72 65 2c 20  .** On failure, 
6d670 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41  return SQLITE_CA
6d680 4e 54 4f 50 45 4e 2e 0a 2a 2f 0a 53 51 4c 49 54  NTOPEN..*/.SQLIT
6d690 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6d6a0 6c 69 74 65 33 4f 73 32 4f 70 65 6e 45 78 63 6c  lite3Os2OpenExcl
6d6b0 75 73 69 76 65 28 20 63 6f 6e 73 74 20 63 68 61  usive( const cha
6d6c0 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 4f 73  r *zFilename, Os
6d6d0 46 69 6c 65 20 2a 2a 70 6c 64 2c 20 69 6e 74 20  File **pld, int 
6d6e0 64 65 6c 46 6c 61 67 20 29 7b 0a 20 20 6f 73 32  delFlag ){.  os2
6d6f0 46 69 6c 65 20 20 66 3b 0a 20 20 48 46 49 4c 45  File  f;.  HFILE
6d700 20 20 20 20 68 66 3b 0a 20 20 55 4c 4f 4e 47 20      hf;.  ULONG 
6d710 20 20 20 75 6c 41 63 74 69 6f 6e 3b 0a 20 20 41     ulAction;.  A
6d720 50 49 52 45 54 20 20 20 72 63 20 3d 20 4e 4f 5f  PIRET   rc = NO_
6d730 45 52 52 4f 52 3b 0a 0a 20 20 61 73 73 65 72 74  ERROR;..  assert
6d740 28 20 2a 70 6c 64 20 3d 3d 20 30 20 29 3b 0a 20  ( *pld == 0 );. 
6d750 20 72 63 20 3d 20 44 6f 73 4f 70 65 6e 28 20 28   rc = DosOpen( (
6d760 50 53 5a 29 7a 46 69 6c 65 6e 61 6d 65 2c 20 26  PSZ)zFilename, &
6d770 68 66 2c 20 26 75 6c 41 63 74 69 6f 6e 2c 20 30  hf, &ulAction, 0
6d780 4c 2c 20 46 49 4c 45 5f 4e 4f 52 4d 41 4c 2c 0a  L, FILE_NORMAL,.
6d790 20 20 20 20 20 20 20 20 20 20 20 20 4f 50 45 4e              OPEN
6d7a0 5f 41 43 54 49 4f 4e 5f 43 52 45 41 54 45 5f 49  _ACTION_CREATE_I
6d7b0 46 5f 4e 45 57 20 7c 20 4f 50 45 4e 5f 41 43 54  F_NEW | OPEN_ACT
6d7c0 49 4f 4e 5f 52 45 50 4c 41 43 45 5f 49 46 5f 45  ION_REPLACE_IF_E
6d7d0 58 49 53 54 53 2c 0a 20 20 20 20 20 20 20 20 20  XISTS,.         
6d7e0 20 20 20 4f 50 45 4e 5f 46 4c 41 47 53 5f 46 41     OPEN_FLAGS_FA
6d7f0 49 4c 5f 4f 4e 5f 45 52 52 4f 52 20 7c 20 4f 50  IL_ON_ERROR | OP
6d800 45 4e 5f 46 4c 41 47 53 5f 52 41 4e 44 4f 4d 20  EN_FLAGS_RANDOM 
6d810 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  |.              
6d820 20 20 4f 50 45 4e 5f 53 48 41 52 45 5f 44 45 4e    OPEN_SHARE_DEN
6d830 59 52 45 41 44 57 52 49 54 45 20 7c 20 4f 50 45  YREADWRITE | OPE
6d840 4e 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49  N_ACCESS_READWRI
6d850 54 45 2c 20 28 50 45 41 4f 50 32 29 4e 55 4c 4c  TE, (PEAOP2)NULL
6d860 20 29 3b 0a 20 20 69 66 28 20 72 63 20 21 3d 20   );.  if( rc != 
6d870 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20  NO_ERROR ){.    
6d880 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41  return SQLITE_CA
6d890 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a 0a 20 20 66  NTOPEN;.  }..  f
6d8a0 2e 68 20 3d 20 68 66 3b 0a 20 20 66 2e 6c 6f 63  .h = hf;.  f.loc
6d8b0 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b  ktype = NO_LOCK;
6d8c0 0a 20 20 66 2e 64 65 6c 4f 6e 43 6c 6f 73 65 20  .  f.delOnClose 
6d8d0 3d 20 64 65 6c 46 6c 61 67 20 3f 20 31 20 3a 20  = delFlag ? 1 : 
6d8e0 30 3b 0a 20 20 66 2e 70 61 74 68 54 6f 44 65 6c  0;.  f.pathToDel
6d8f0 20 3d 20 64 65 6c 46 6c 61 67 20 3f 20 73 71 6c   = delFlag ? sql
6d900 69 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61  ite3OsFullPathna
6d910 6d 65 28 20 7a 46 69 6c 65 6e 61 6d 65 20 29 20  me( zFilename ) 
6d920 3a 20 4e 55 4c 4c 3b 0a 20 20 4f 70 65 6e 43 6f  : NULL;.  OpenCo
6d930 75 6e 74 65 72 28 20 2b 31 20 29 3b 0a 20 20 69  unter( +1 );.  i
6d940 66 28 20 64 65 6c 46 6c 61 67 20 29 20 44 6f 73  f( delFlag ) Dos
6d950 46 6f 72 63 65 44 65 6c 65 74 65 28 20 28 50 53  ForceDelete( (PS
6d960 5a 29 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50  Z)sqlite3OsFullP
6d970 61 74 68 6e 61 6d 65 28 20 7a 46 69 6c 65 6e 61  athname( zFilena
6d980 6d 65 20 29 20 29 3b 0a 20 20 4f 53 54 52 41 43  me ) );.  OSTRAC
6d990 45 33 28 20 22 4f 50 45 4e 20 45 58 20 25 64 20  E3( "OPEN EX %d 
6d9a0 5c 22 25 73 5c 22 5c 6e 22 2c 20 68 66 2c 20 73  \"%s\"\n", hf, s
6d9b0 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 61 74 68  qlite3OsFullPath
6d9c0 6e 61 6d 65 20 28 20 7a 46 69 6c 65 6e 61 6d 65  name ( zFilename
6d9d0 20 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 61   ) );.  return a
6d9e0 6c 6c 6f 63 61 74 65 4f 73 32 46 69 6c 65 28 20  llocateOs2File( 
6d9f0 26 66 2c 20 70 6c 64 20 29 3b 0a 7d 0a 0a 2f 2a  &f, pld );.}../*
6da00 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6f  .** Attempt to o
6da10 70 65 6e 20 61 20 6e 65 77 20 66 69 6c 65 20 66  pen a new file f
6da20 6f 72 20 72 65 61 64 2d 6f 6e 6c 79 20 61 63 63  or read-only acc
6da30 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75  ess..**.** On su
6da40 63 63 65 73 73 2c 20 77 72 69 74 65 20 74 68 65  ccess, write the
6da50 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 69 6e 74   file handle int
6da60 6f 20 2a 69 64 20 61 6e 64 20 72 65 74 75 72 6e  o *id and return
6da70 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a   SQLITE_OK..**.*
6da80 2a 20 4f 6e 20 66 61 69 6c 75 72 65 2c 20 72 65  * On failure, re
6da90 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54  turn SQLITE_CANT
6daa0 4f 50 45 4e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  OPEN..*/.SQLITE_
6dab0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6dac0 74 65 33 4f 73 32 4f 70 65 6e 52 65 61 64 4f 6e  te3Os2OpenReadOn
6dad0 6c 79 28 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ly( const char *
6dae0 7a 46 69 6c 65 6e 61 6d 65 2c 20 4f 73 46 69 6c  zFilename, OsFil
6daf0 65 20 2a 2a 70 6c 64 20 29 7b 0a 20 20 6f 73 32  e **pld ){.  os2
6db00 46 69 6c 65 20 20 66 3b 0a 20 20 48 46 49 4c 45  File  f;.  HFILE
6db10 20 20 20 20 68 66 3b 0a 20 20 55 4c 4f 4e 47 20      hf;.  ULONG 
6db20 20 20 20 75 6c 41 63 74 69 6f 6e 3b 0a 20 20 41     ulAction;.  A
6db30 50 49 52 45 54 20 20 20 72 63 20 3d 20 4e 4f 5f  PIRET   rc = NO_
6db40 45 52 52 4f 52 3b 0a 0a 20 20 61 73 73 65 72 74  ERROR;..  assert
6db50 28 20 2a 70 6c 64 20 3d 3d 20 30 20 29 3b 0a 20  ( *pld == 0 );. 
6db60 20 72 63 20 3d 20 44 6f 73 4f 70 65 6e 28 20 28   rc = DosOpen( (
6db70 50 53 5a 29 7a 46 69 6c 65 6e 61 6d 65 2c 20 26  PSZ)zFilename, &
6db80 68 66 2c 20 26 75 6c 41 63 74 69 6f 6e 2c 20 30  hf, &ulAction, 0
6db90 4c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 46  L,.            F
6dba0 49 4c 45 5f 4e 4f 52 4d 41 4c 2c 20 4f 50 45 4e  ILE_NORMAL, OPEN
6dbb0 5f 41 43 54 49 4f 4e 5f 4f 50 45 4e 5f 49 46 5f  _ACTION_OPEN_IF_
6dbc0 45 58 49 53 54 53 2c 0a 20 20 20 20 20 20 20 20  EXISTS,.        
6dbd0 20 20 20 20 4f 50 45 4e 5f 46 4c 41 47 53 5f 46      OPEN_FLAGS_F
6dbe0 41 49 4c 5f 4f 4e 5f 45 52 52 4f 52 20 7c 20 4f  AIL_ON_ERROR | O
6dbf0 50 45 4e 5f 46 4c 41 47 53 5f 52 41 4e 44 4f 4d  PEN_FLAGS_RANDOM
6dc00 20 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   |.             
6dc10 20 20 20 4f 50 45 4e 5f 53 48 41 52 45 5f 44 45     OPEN_SHARE_DE
6dc20 4e 59 57 52 49 54 45 20 7c 20 4f 50 45 4e 5f 41  NYWRITE | OPEN_A
6dc30 43 43 45 53 53 5f 52 45 41 44 4f 4e 4c 59 2c 20  CCESS_READONLY, 
6dc40 28 50 45 41 4f 50 32 29 4e 55 4c 4c 20 29 3b 0a  (PEAOP2)NULL );.
6dc50 20 20 69 66 28 20 72 63 20 21 3d 20 4e 4f 5f 45    if( rc != NO_E
6dc60 52 52 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75  RROR ){.    retu
6dc70 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50  rn SQLITE_CANTOP
6dc80 45 4e 3b 0a 20 20 7d 0a 20 20 66 2e 68 20 3d 20  EN;.  }.  f.h = 
6dc90 68 66 3b 0a 20 20 66 2e 6c 6f 63 6b 74 79 70 65  hf;.  f.locktype
6dca0 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 66 2e   = NO_LOCK;.  f.
6dcb0 64 65 6c 4f 6e 43 6c 6f 73 65 20 3d 20 30 3b 0a  delOnClose = 0;.
6dcc0 20 20 66 2e 70 61 74 68 54 6f 44 65 6c 20 3d 20    f.pathToDel = 
6dcd0 4e 55 4c 4c 3b 0a 20 20 4f 70 65 6e 43 6f 75 6e  NULL;.  OpenCoun
6dce0 74 65 72 28 20 2b 31 20 29 3b 0a 20 20 4f 53 54  ter( +1 );.  OST
6dcf0 52 41 43 45 33 28 20 22 4f 50 45 4e 20 52 4f 20  RACE3( "OPEN RO 
6dd00 25 64 20 5c 22 25 73 5c 22 5c 6e 22 2c 20 68 66  %d \"%s\"\n", hf
6dd10 2c 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 20  , zFilename );. 
6dd20 20 72 65 74 75 72 6e 20 61 6c 6c 6f 63 61 74 65   return allocate
6dd30 4f 73 32 46 69 6c 65 28 20 26 66 2c 20 70 6c 64  Os2File( &f, pld
6dd40 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74   );.}../*.** Att
6dd50 65 6d 70 74 20 74 6f 20 6f 70 65 6e 20 61 20 66  empt to open a f
6dd60 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 66  ile descriptor f
6dd70 6f 72 20 74 68 65 20 64 69 72 65 63 74 6f 72 79  or the directory
6dd80 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 61   that contains a
6dd90 0a 2a 2a 20 66 69 6c 65 2e 20 20 54 68 69 73 20  .** file.  This 
6dda0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
6ddb0 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 66  can be used to f
6ddc0 73 79 6e 63 28 29 20 74 68 65 20 64 69 72 65 63  sync() the direc
6ddd0 74 6f 72 79 0a 2a 2a 20 69 6e 20 6f 72 64 65 72  tory.** in order
6dde0 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
6ddf0 65 20 63 72 65 61 74 69 6f 6e 20 6f 66 20 61 20  e creation of a 
6de00 6e 65 77 20 66 69 6c 65 20 69 73 20 61 63 74 75  new file is actu
6de10 61 6c 6c 79 20 77 72 69 74 74 65 6e 0a 2a 2a 20  ally written.** 
6de20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 54  to disk..**.** T
6de30 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6f  his routine is o
6de40 6e 6c 79 20 6d 65 61 6e 69 6e 67 66 75 6c 20 66  nly meaningful f
6de50 6f 72 20 55 6e 69 78 2e 20 20 49 74 20 69 73 20  or Unix.  It is 
6de60 61 20 6e 6f 2d 6f 70 20 75 6e 64 65 72 0a 2a 2a  a no-op under.**
6de70 20 4f 53 2f 32 20 73 69 6e 63 65 20 4f 53 2f 32   OS/2 since OS/2
6de80 20 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72   does not suppor
6de90 74 20 68 61 72 64 20 6c 69 6e 6b 73 2e 0a 2a 2a  t hard links..**
6dea0 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c 20  .** On success, 
6deb0 61 20 68 61 6e 64 6c 65 20 66 6f 72 20 61 20 70  a handle for a p
6dec0 72 65 76 69 6f 75 73 6c 79 20 6f 70 65 6e 20 66  reviously open f
6ded0 69 6c 65 20 69 73 20 61 74 20 2a 69 64 20 69 73  ile is at *id is
6dee0 0a 2a 2a 20 75 70 64 61 74 65 64 20 77 69 74 68  .** updated with
6def0 20 74 68 65 20 6e 65 77 20 64 69 72 65 63 74 6f   the new directo
6df00 72 79 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  ry file descript
6df10 6f 72 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b  or and SQLITE_OK
6df20 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e   is.** returned.
6df30 0a 2a 2a 0a 2a 2a 20 4f 6e 20 66 61 69 6c 75 72  .**.** On failur
6df40 65 2c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  e, the function 
6df50 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 43  returns SQLITE_C
6df60 41 4e 54 4f 50 45 4e 20 61 6e 64 20 6c 65 61 76  ANTOPEN and leav
6df70 65 73 0a 2a 2a 20 2a 69 64 20 75 6e 63 68 61 6e  es.** *id unchan
6df80 67 65 64 2e 0a 2a 2f 0a 69 6e 74 20 6f 73 32 4f  ged..*/.int os2O
6df90 70 65 6e 44 69 72 65 63 74 6f 72 79 28 0a 20 20  penDirectory(.  
6dfa0 4f 73 46 69 6c 65 20 2a 69 64 2c 0a 20 20 63 6f  OsFile *id,.  co
6dfb0 6e 73 74 20 63 68 61 72 20 2a 7a 44 69 72 6e 61  nst char *zDirna
6dfc0 6d 65 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 53  me.){.  return S
6dfd0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
6dfe0 2a 2a 20 43 72 65 61 74 65 20 61 20 74 65 6d 70  ** Create a temp
6dff0 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65 20  orary file name 
6e000 69 6e 20 7a 42 75 66 2e 20 20 7a 42 75 66 20 6d  in zBuf.  zBuf m
6e010 75 73 74 20 62 65 20 62 69 67 20 65 6e 6f 75 67  ust be big enoug
6e020 68 20 74 6f 0a 2a 2a 20 68 6f 6c 64 20 61 74 20  h to.** hold at 
6e030 6c 65 61 73 74 20 53 51 4c 49 54 45 5f 54 45 4d  least SQLITE_TEM
6e040 50 4e 41 4d 45 5f 53 49 5a 45 20 63 68 61 72 61  PNAME_SIZE chara
6e050 63 74 65 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  cters..*/.SQLITE
6e060 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
6e070 69 74 65 33 4f 73 32 54 65 6d 70 46 69 6c 65 4e  ite3Os2TempFileN
6e080 61 6d 65 28 20 63 68 61 72 20 2a 7a 42 75 66 20  ame( char *zBuf 
6e090 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  ){.  static cons
6e0a0 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
6e0b0 7a 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22  zChars[] =.    "
6e0c0 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70  abcdefghijklmnop
6e0d0 71 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20 20  qrstuvwxyz".    
6e0e0 22 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  "ABCDEFGHIJKLMNO
6e0f0 50 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20 20  PQRSTUVWXYZ".   
6e100 20 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a 20   "0123456789";. 
6e110 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 50 53 5a   int i, j;.  PSZ
6e120 20 7a 54 65 6d 70 50 61 74 68 20 3d 20 30 3b 0a   zTempPath = 0;.
6e130 20 20 69 66 28 20 44 6f 73 53 63 61 6e 45 6e 76    if( DosScanEnv
6e140 28 20 28 50 53 5a 29 22 54 45 4d 50 22 2c 20 26  ( (PSZ)"TEMP", &
6e150 7a 54 65 6d 70 50 61 74 68 20 29 20 29 7b 0a 20  zTempPath ) ){. 
6e160 20 20 20 69 66 28 20 44 6f 73 53 63 61 6e 45 6e     if( DosScanEn
6e170 76 28 20 28 50 53 5a 29 22 54 4d 50 22 2c 20 26  v( (PSZ)"TMP", &
6e180 7a 54 65 6d 70 50 61 74 68 20 29 20 29 7b 0a 20  zTempPath ) ){. 
6e190 20 20 20 20 20 69 66 28 20 44 6f 73 53 63 61 6e       if( DosScan
6e1a0 45 6e 76 28 20 28 50 53 5a 29 22 54 4d 50 44 49  Env( (PSZ)"TMPDI
6e1b0 52 22 2c 20 26 7a 54 65 6d 70 50 61 74 68 20 29  R", &zTempPath )
6e1c0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 55   ){.           U
6e1d0 4c 4f 4e 47 20 75 6c 44 72 69 76 65 4e 75 6d 20  LONG ulDriveNum 
6e1e0 3d 20 30 2c 20 75 6c 44 72 69 76 65 4d 61 70 20  = 0, ulDriveMap 
6e1f0 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20  = 0;.           
6e200 44 6f 73 51 75 65 72 79 43 75 72 72 65 6e 74 44  DosQueryCurrentD
6e210 69 73 6b 28 20 26 75 6c 44 72 69 76 65 4e 75 6d  isk( &ulDriveNum
6e220 2c 20 26 75 6c 44 72 69 76 65 4d 61 70 20 29 3b  , &ulDriveMap );
6e230 0a 20 20 20 20 20 20 20 20 20 20 20 73 70 72 69  .           spri
6e240 6e 74 66 28 20 28 63 68 61 72 2a 29 7a 54 65 6d  ntf( (char*)zTem
6e250 70 50 61 74 68 2c 20 22 25 63 3a 22 2c 20 28 63  pPath, "%c:", (c
6e260 68 61 72 29 28 20 27 41 27 20 2b 20 75 6c 44 72  har)( 'A' + ulDr
6e270 69 76 65 4e 75 6d 20 2d 20 31 20 29 20 29 3b 0a  iveNum - 1 ) );.
6e280 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
6e290 7d 0a 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20  }.  for(;;){.   
6e2a0 20 20 20 73 70 72 69 6e 74 66 28 20 7a 42 75 66     sprintf( zBuf
6e2b0 2c 20 22 25 73 5c 5c 22 54 45 4d 50 5f 46 49 4c  , "%s\\"TEMP_FIL
6e2c0 45 5f 50 52 45 46 49 58 2c 20 7a 54 65 6d 70 50  E_PREFIX, zTempP
6e2d0 61 74 68 20 29 3b 0a 20 20 20 20 20 20 6a 20 3d  ath );.      j =
6e2e0 20 73 74 72 6c 65 6e 28 20 7a 42 75 66 20 29 3b   strlen( zBuf );
6e2f0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 61  .      sqlite3Ra
6e300 6e 64 6f 6d 6e 65 73 73 28 20 31 35 2c 20 26 7a  ndomness( 15, &z
6e310 42 75 66 5b 6a 5d 20 29 3b 0a 20 20 20 20 20 20  Buf[j] );.      
6e320 66 6f 72 28 20 69 20 3d 20 30 3b 20 69 20 3c 20  for( i = 0; i < 
6e330 31 35 3b 20 69 2b 2b 2c 20 6a 2b 2b 20 29 7b 0a  15; i++, j++ ){.
6e340 20 20 20 20 20 20 20 20 7a 42 75 66 5b 6a 5d 20          zBuf[j] 
6e350 3d 20 28 63 68 61 72 29 7a 43 68 61 72 73 5b 20  = (char)zChars[ 
6e360 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29  ((unsigned char)
6e370 7a 42 75 66 5b 6a 5d 29 25 28 73 69 7a 65 6f 66  zBuf[j])%(sizeof
6e380 28 7a 43 68 61 72 73 29 2d 31 29 20 5d 3b 0a 20  (zChars)-1) ];. 
6e390 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 42 75       }.      zBu
6e3a0 66 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20  f[j] = 0;.      
6e3b0 69 66 28 20 21 73 71 6c 69 74 65 33 4f 73 46 69  if( !sqlite3OsFi
6e3c0 6c 65 45 78 69 73 74 73 28 20 7a 42 75 66 20 29  leExists( zBuf )
6e3d0 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20   ) break;.  }.  
6e3e0 4f 53 54 52 41 43 45 32 28 20 22 54 45 4d 50 20  OSTRACE2( "TEMP 
6e3f0 46 49 4c 45 4e 41 4d 45 3a 20 25 73 5c 6e 22 2c  FILENAME: %s\n",
6e400 20 7a 42 75 66 20 29 3b 0a 20 20 72 65 74 75 72   zBuf );.  retur
6e410 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
6e420 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69  /*.** Close a fi
6e430 6c 65 2e 0a 2a 2f 0a 69 6e 74 20 6f 73 32 43 6c  le..*/.int os2Cl
6e440 6f 73 65 28 20 4f 73 46 69 6c 65 20 2a 2a 70 6c  ose( OsFile **pl
6e450 64 20 29 7b 0a 20 20 6f 73 32 46 69 6c 65 20 2a  d ){.  os2File *
6e460 70 46 69 6c 65 3b 0a 20 20 41 50 49 52 45 54 20  pFile;.  APIRET 
6e470 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20  rc = NO_ERROR;. 
6e480 20 69 66 28 20 70 6c 64 20 26 26 20 28 70 46 69   if( pld && (pFi
6e490 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 2a  le = (os2File*)*
6e4a0 70 6c 64 29 20 21 3d 20 30 20 29 7b 0a 20 20 20  pld) != 0 ){.   
6e4b0 20 4f 53 54 52 41 43 45 32 28 20 22 43 4c 4f 53   OSTRACE2( "CLOS
6e4c0 45 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  E %d\n", pFile->
6e4d0 68 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 44 6f  h );.    rc = Do
6e4e0 73 43 6c 6f 73 65 28 20 70 46 69 6c 65 2d 3e 68  sClose( pFile->h
6e4f0 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c   );.    pFile->l
6e500 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43  ocktype = NO_LOC
6e510 4b 3b 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65  K;.    if( pFile
6e520 2d 3e 64 65 6c 4f 6e 43 6c 6f 73 65 20 21 3d 20  ->delOnClose != 
6e530 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  0 ){.        rc 
6e540 3d 20 44 6f 73 46 6f 72 63 65 44 65 6c 65 74 65  = DosForceDelete
6e550 28 20 28 50 53 5a 29 70 46 69 6c 65 2d 3e 70 61  ( (PSZ)pFile->pa
6e560 74 68 54 6f 44 65 6c 20 29 3b 0a 20 20 20 20 7d  thToDel );.    }
6e570 0a 20 20 20 20 2a 70 6c 64 20 3d 20 30 3b 0a 20  .    *pld = 0;. 
6e580 20 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 20     OpenCounter( 
6e590 2d 31 20 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  -1 );.  }..  ret
6e5a0 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52  urn rc == NO_ERR
6e5b0 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a  OR ? SQLITE_OK :
6e5c0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 7d   SQLITE_IOERR;.}
6e5d0 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74  ../*.** Read dat
6e5e0 61 20 66 72 6f 6d 20 61 20 66 69 6c 65 20 69 6e  a from a file in
6e5f0 74 6f 20 61 20 62 75 66 66 65 72 2e 20 20 52 65  to a buffer.  Re
6e600 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69  turn SQLITE_OK i
6e610 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 20 77  f all.** bytes w
6e620 65 72 65 20 72 65 61 64 20 73 75 63 63 65 73 73  ere read success
6e630 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c 49 54 45  fully and SQLITE
6e640 5f 49 4f 45 52 52 20 69 66 20 61 6e 79 74 68 69  _IOERR if anythi
6e650 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67  ng goes.** wrong
6e660 2e 0a 2a 2f 0a 69 6e 74 20 6f 73 32 52 65 61 64  ..*/.int os2Read
6e670 28 20 4f 73 46 69 6c 65 20 2a 69 64 2c 20 76 6f  ( OsFile *id, vo
6e680 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 61 6d  id *pBuf, int am
6e690 74 20 29 7b 0a 20 20 55 4c 4f 4e 47 20 67 6f 74  t ){.  ULONG got
6e6a0 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d  ;.  assert( id!=
6e6b0 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49  0 );.  SimulateI
6e6c0 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53  OError( return S
6e6d0 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 3b 0a 20  QLITE_IOERR );. 
6e6e0 20 4f 53 54 52 41 43 45 33 28 20 22 52 45 41 44   OSTRACE3( "READ
6e6f0 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20   %d lock=%d\n", 
6e700 28 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e  ((os2File*)id)->
6e710 68 2c 20 28 28 6f 73 32 46 69 6c 65 2a 29 69 64  h, ((os2File*)id
6e720 29 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20  )->locktype );. 
6e730 20 44 6f 73 52 65 61 64 28 20 28 28 6f 73 32 46   DosRead( ((os2F
6e740 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 70 42 75  ile*)id)->h, pBu
6e750 66 2c 20 61 6d 74 2c 20 26 67 6f 74 20 29 3b 0a  f, amt, &got );.
6e760 20 20 69 66 20 28 67 6f 74 20 3d 3d 20 28 55 4c    if (got == (UL
6e770 4f 4e 47 29 61 6d 74 29 0a 20 20 20 20 72 65 74  ONG)amt).    ret
6e780 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
6e790 20 65 6c 73 65 20 69 66 20 28 67 6f 74 20 3d 3d   else if (got ==
6e7a0 20 30 29 0a 20 20 20 20 72 65 74 75 72 6e 20 53   0).    return S
6e7b0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44  QLITE_IOERR_READ
6e7c0 3b 0a 20 20 65 6c 73 65 20 7b 0a 20 20 20 20 6d  ;.  else {.    m
6e7d0 65 6d 73 65 74 28 26 28 28 63 68 61 72 2a 29 70  emset(&((char*)p
6e7e0 42 75 66 29 5b 67 6f 74 5d 2c 20 30 2c 20 61 6d  Buf)[got], 0, am
6e7f0 74 2d 67 6f 74 29 3b 0a 20 20 20 20 72 65 74 75  t-got);.    retu
6e800 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
6e810 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d 0a  SHORT_READ;.  }.
6e820 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64  }../*.** Write d
6e830 61 74 61 20 66 72 6f 6d 20 61 20 62 75 66 66 65  ata from a buffe
6e840 72 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e 20 20  r into a file.  
6e850 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
6e860 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f   on success.** o
6e870 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72  r some other err
6e880 6f 72 20 63 6f 64 65 20 6f 6e 20 66 61 69 6c 75  or code on failu
6e890 72 65 2e 0a 2a 2f 0a 69 6e 74 20 6f 73 32 57 72  re..*/.int os2Wr
6e8a0 69 74 65 28 20 4f 73 46 69 6c 65 20 2a 69 64 2c  ite( OsFile *id,
6e8b0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75   const void *pBu
6e8c0 66 2c 20 69 6e 74 20 61 6d 74 20 29 7b 0a 20 20  f, int amt ){.  
6e8d0 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45  APIRET rc = NO_E
6e8e0 52 52 4f 52 3b 0a 20 20 55 4c 4f 4e 47 20 77 72  RROR;.  ULONG wr
6e8f0 6f 74 65 3b 0a 20 20 61 73 73 65 72 74 28 20 69  ote;.  assert( i
6e900 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61  d!=0 );.  Simula
6e910 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72  teIOError( retur
6e920 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29  n SQLITE_IOERR )
6e930 3b 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b  ;.  SimulateDisk
6e940 66 75 6c 6c 45 72 72 6f 72 28 20 72 65 74 75 72  fullError( retur
6e950 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 3b  n SQLITE_FULL );
6e960 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 57 52  .  OSTRACE3( "WR
6e970 49 54 45 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e  ITE %d lock=%d\n
6e980 22 2c 20 28 28 6f 73 32 46 69 6c 65 2a 29 69 64  ", ((os2File*)id
6e990 29 2d 3e 68 2c 20 28 28 6f 73 32 46 69 6c 65 2a  )->h, ((os2File*
6e9a0 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65 20 29  )id)->locktype )
6e9b0 3b 0a 20 20 77 68 69 6c 65 28 20 61 6d 74 20 3e  ;.  while( amt >
6e9c0 20 30 20 26 26 0a 20 20 20 20 20 20 28 72 63 20   0 &&.      (rc 
6e9d0 3d 20 44 6f 73 57 72 69 74 65 28 20 28 28 6f 73  = DosWrite( ((os
6e9e0 32 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 28  2File*)id)->h, (
6e9f0 50 56 4f 49 44 29 70 42 75 66 2c 20 61 6d 74 2c  PVOID)pBuf, amt,
6ea00 20 26 77 72 6f 74 65 20 29 29 20 26 26 20 77 72   &wrote )) && wr
6ea10 6f 74 65 20 3e 20 30 20 29 7b 0a 20 20 20 20 20  ote > 0 ){.     
6ea20 20 61 6d 74 20 2d 3d 20 77 72 6f 74 65 3b 0a 20   amt -= wrote;. 
6ea30 20 20 20 20 20 70 42 75 66 20 3d 20 26 28 28 63       pBuf = &((c
6ea40 68 61 72 2a 29 70 42 75 66 29 5b 77 72 6f 74 65  har*)pBuf)[wrote
6ea50 5d 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  ];.  }..  return
6ea60 20 28 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f   ( rc != NO_ERRO
6ea70 52 20 7c 7c 20 61 6d 74 20 3e 20 28 69 6e 74 29  R || amt > (int)
6ea80 77 72 6f 74 65 20 29 20 3f 20 53 51 4c 49 54 45  wrote ) ? SQLITE
6ea90 5f 46 55 4c 4c 20 3a 20 53 51 4c 49 54 45 5f 4f  _FULL : SQLITE_O
6eaa0 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65  K;.}../*.** Move
6eab0 20 74 68 65 20 72 65 61 64 2f 77 72 69 74 65 20   the read/write 
6eac0 70 6f 69 6e 74 65 72 20 69 6e 20 61 20 66 69 6c  pointer in a fil
6ead0 65 2e 0a 2a 2f 0a 69 6e 74 20 6f 73 32 53 65 65  e..*/.int os2See
6eae0 6b 28 20 4f 73 46 69 6c 65 20 2a 69 64 2c 20 69  k( OsFile *id, i
6eaf0 36 34 20 6f 66 66 73 65 74 20 29 7b 0a 20 20 41  64 offset ){.  A
6eb00 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52  PIRET rc = NO_ER
6eb10 52 4f 52 3b 0a 20 20 55 4c 4f 4e 47 20 66 69 6c  ROR;.  ULONG fil
6eb20 65 50 6f 69 6e 74 65 72 20 3d 20 30 4c 3b 0a 20  ePointer = 0L;. 
6eb30 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29   assert( id!=0 )
6eb40 3b 0a 20 20 72 63 20 3d 20 44 6f 73 53 65 74 46  ;.  rc = DosSetF
6eb50 69 6c 65 50 74 72 28 20 28 28 6f 73 32 46 69 6c  ilePtr( ((os2Fil
6eb60 65 2a 29 69 64 29 2d 3e 68 2c 20 6f 66 66 73 65  e*)id)->h, offse
6eb70 74 2c 20 46 49 4c 45 5f 42 45 47 49 4e 2c 20 26  t, FILE_BEGIN, &
6eb80 66 69 6c 65 50 6f 69 6e 74 65 72 20 29 3b 0a 20  filePointer );. 
6eb90 20 4f 53 54 52 41 43 45 33 28 20 22 53 45 45 4b   OSTRACE3( "SEEK
6eba0 20 25 64 20 25 6c 6c 64 5c 6e 22 2c 20 28 28 6f   %d %lld\n", ((o
6ebb0 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20  s2File*)id)->h, 
6ebc0 6f 66 66 73 65 74 20 29 3b 0a 20 20 72 65 74 75  offset );.  retu
6ebd0 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f  rn rc == NO_ERRO
6ebe0 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20  R ? SQLITE_OK : 
6ebf0 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 7d 0a  SQLITE_IOERR;.}.
6ec00 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65  ./*.** Make sure
6ec10 20 61 6c 6c 20 77 72 69 74 65 73 20 74 6f 20 61   all writes to a
6ec20 20 70 61 72 74 69 63 75 6c 61 72 20 66 69 6c 65   particular file
6ec30 20 61 72 65 20 63 6f 6d 6d 69 74 74 65 64 20 74   are committed t
6ec40 6f 20 64 69 73 6b 2e 0a 2a 2f 0a 69 6e 74 20 6f  o disk..*/.int o
6ec50 73 32 53 79 6e 63 28 20 4f 73 46 69 6c 65 20 2a  s2Sync( OsFile *
6ec60 69 64 2c 20 69 6e 74 20 64 61 74 61 4f 6e 6c 79  id, int dataOnly
6ec70 20 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69 64   ){.  assert( id
6ec80 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41 43 45  !=0 );.  OSTRACE
6ec90 33 28 20 22 53 59 4e 43 20 25 64 20 6c 6f 63 6b  3( "SYNC %d lock
6eca0 3d 25 64 5c 6e 22 2c 20 28 28 6f 73 32 46 69 6c  =%d\n", ((os2Fil
6ecb0 65 2a 29 69 64 29 2d 3e 68 2c 20 28 28 6f 73 32  e*)id)->h, ((os2
6ecc0 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74  File*)id)->lockt
6ecd0 79 70 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  ype );.  return 
6ece0 44 6f 73 52 65 73 65 74 42 75 66 66 65 72 28 20  DosResetBuffer( 
6ecf0 28 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e  ((os2File*)id)->
6ed00 68 20 29 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20  h ) == NO_ERROR 
6ed10 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51  ? SQLITE_OK : SQ
6ed20 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 7d 0a 0a 2f  LITE_IOERR;.}../
6ed30 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 65 20 64 69  *.** Sync the di
6ed40 72 65 63 74 6f 72 79 20 7a 44 69 72 6e 61 6d 65  rectory zDirname
6ed50 2e 20 54 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f  . This is a no-o
6ed60 70 20 6f 6e 20 6f 70 65 72 61 74 69 6e 67 20 73  p on operating s
6ed70 79 73 74 65 6d 73 20 6f 74 68 65 72 0a 2a 2a 20  ystems other.** 
6ed80 74 68 61 6e 20 55 4e 49 58 2e 0a 2a 2f 0a 53 51  than UNIX..*/.SQ
6ed90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
6eda0 20 73 71 6c 69 74 65 33 4f 73 32 53 79 6e 63 44   sqlite3Os2SyncD
6edb0 69 72 65 63 74 6f 72 79 28 20 63 6f 6e 73 74 20  irectory( const 
6edc0 63 68 61 72 20 2a 7a 44 69 72 6e 61 6d 65 20 29  char *zDirname )
6edd0 7b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  {.  SimulateIOEr
6ede0 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49  ror( return SQLI
6edf0 54 45 5f 49 4f 45 52 52 20 29 3b 0a 20 20 72 65  TE_IOERR );.  re
6ee00 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
6ee10 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74  }../*.** Truncat
6ee20 65 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 74  e an open file t
6ee30 6f 20 61 20 73 70 65 63 69 66 69 65 64 20 73 69  o a specified si
6ee40 7a 65 0a 2a 2f 0a 69 6e 74 20 6f 73 32 54 72 75  ze.*/.int os2Tru
6ee50 6e 63 61 74 65 28 20 4f 73 46 69 6c 65 20 2a 69  ncate( OsFile *i
6ee60 64 2c 20 69 36 34 20 6e 42 79 74 65 20 29 7b 0a  d, i64 nByte ){.
6ee70 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f    APIRET rc = NO
6ee80 5f 45 52 52 4f 52 3b 0a 20 20 55 4c 4f 4e 47 20  _ERROR;.  ULONG 
6ee90 75 70 70 65 72 42 69 74 73 20 3d 20 6e 42 79 74  upperBits = nByt
6eea0 65 3e 3e 33 32 3b 0a 20 20 61 73 73 65 72 74 28  e>>32;.  assert(
6eeb0 20 69 64 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52   id!=0 );.  OSTR
6eec0 41 43 45 33 28 20 22 54 52 55 4e 43 41 54 45 20  ACE3( "TRUNCATE 
6eed0 25 64 20 25 6c 6c 64 5c 6e 22 2c 20 28 28 6f 73  %d %lld\n", ((os
6eee0 32 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 6e  2File*)id)->h, n
6eef0 42 79 74 65 20 29 3b 0a 20 20 53 69 6d 75 6c 61  Byte );.  Simula
6ef00 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72  teIOError( retur
6ef10 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29  n SQLITE_IOERR )
6ef20 3b 0a 20 20 72 63 20 3d 20 44 6f 73 53 65 74 46  ;.  rc = DosSetF
6ef30 69 6c 65 50 74 72 28 20 28 28 6f 73 32 46 69 6c  ilePtr( ((os2Fil
6ef40 65 2a 29 69 64 29 2d 3e 68 2c 20 6e 42 79 74 65  e*)id)->h, nByte
6ef50 2c 20 46 49 4c 45 5f 42 45 47 49 4e 2c 20 26 75  , FILE_BEGIN, &u
6ef60 70 70 65 72 42 69 74 73 20 29 3b 0a 20 20 69 66  pperBits );.  if
6ef70 28 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52  ( rc != NO_ERROR
6ef80 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
6ef90 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 7d  QLITE_IOERR;.  }
6efa0 0a 20 20 72 63 20 3d 20 44 6f 73 53 65 74 46 69  .  rc = DosSetFi
6efb0 6c 65 50 74 72 28 20 28 28 6f 73 32 46 69 6c 65  lePtr( ((os2File
6efc0 2a 29 69 64 29 2d 3e 68 2c 20 30 4c 2c 20 46 49  *)id)->h, 0L, FI
6efd0 4c 45 5f 45 4e 44 2c 20 26 75 70 70 65 72 42 69  LE_END, &upperBi
6efe0 74 73 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  ts );.  return r
6eff0 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20  c == NO_ERROR ? 
6f000 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49  SQLITE_OK : SQLI
6f010 54 45 5f 49 4f 45 52 52 3b 0a 7d 0a 0a 2f 2a 0a  TE_IOERR;.}../*.
6f020 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65  ** Determine the
6f030 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66   current size of
6f040 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73   a file in bytes
6f050 0a 2a 2f 0a 69 6e 74 20 6f 73 32 46 69 6c 65 53  .*/.int os2FileS
6f060 69 7a 65 28 20 4f 73 46 69 6c 65 20 2a 69 64 2c  ize( OsFile *id,
6f070 20 69 36 34 20 2a 70 53 69 7a 65 20 29 7b 0a 20   i64 *pSize ){. 
6f080 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f   APIRET rc = NO_
6f090 45 52 52 4f 52 3b 0a 20 20 46 49 4c 45 53 54 41  ERROR;.  FILESTA
6f0a0 54 55 53 33 20 66 73 74 73 33 46 69 6c 65 49 6e  TUS3 fsts3FileIn
6f0b0 66 6f 3b 0a 20 20 6d 65 6d 73 65 74 28 26 66 73  fo;.  memset(&fs
6f0c0 74 73 33 46 69 6c 65 49 6e 66 6f 2c 20 30 2c 20  ts3FileInfo, 0, 
6f0d0 73 69 7a 65 6f 66 28 66 73 74 73 33 46 69 6c 65  sizeof(fsts3File
6f0e0 49 6e 66 6f 29 29 3b 0a 20 20 61 73 73 65 72 74  Info));.  assert
6f0f0 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d  ( id!=0 );.  Sim
6f100 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65  ulateIOError( re
6f110 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
6f120 52 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 51  R );.  rc = DosQ
6f130 75 65 72 79 46 69 6c 65 49 6e 66 6f 28 20 28 28  ueryFileInfo( ((
6f140 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c  os2File*)id)->h,
6f150 20 46 49 4c 5f 53 54 41 4e 44 41 52 44 2c 20 26   FIL_STANDARD, &
6f160 66 73 74 73 33 46 69 6c 65 49 6e 66 6f 2c 20 73  fsts3FileInfo, s
6f170 69 7a 65 6f 66 28 46 49 4c 45 53 54 41 54 55 53  izeof(FILESTATUS
6f180 33 29 20 29 3b 0a 20 20 69 66 28 20 72 63 20 3d  3) );.  if( rc =
6f190 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20  = NO_ERROR ){.  
6f1a0 20 20 2a 70 53 69 7a 65 20 3d 20 66 73 74 73 33    *pSize = fsts3
6f1b0 46 69 6c 65 49 6e 66 6f 2e 63 62 46 69 6c 65 3b  FileInfo.cbFile;
6f1c0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
6f1d0 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 65 6c 73  TE_OK;.  }.  els
6f1e0 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  e{.    return SQ
6f1f0 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a  LITE_IOERR;.  }.
6f200 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65  }../*.** Acquire
6f210 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b 2e 0a   a reader lock..
6f220 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65  */.static int ge
6f230 74 52 65 61 64 4c 6f 63 6b 28 20 6f 73 32 46 69  tReadLock( os2Fi
6f240 6c 65 20 2a 69 64 20 29 7b 0a 20 20 46 49 4c 45  le *id ){.  FILE
6f250 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0a  LOCK  LockArea,.
6f260 20 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c 6f              Unlo
6f270 63 6b 41 72 65 61 3b 0a 20 20 6d 65 6d 73 65 74  ckArea;.  memset
6f280 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73  (&LockArea, 0, s
6f290 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61 29 29  izeof(LockArea))
6f2a0 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f  ;.  memset(&Unlo
6f2b0 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f  ckArea, 0, sizeo
6f2c0 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0a  f(UnlockArea));.
6f2d0 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73    LockArea.lOffs
6f2e0 65 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53  et = SHARED_FIRS
6f2f0 54 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52  T;.  LockArea.lR
6f300 61 6e 67 65 20 3d 20 53 48 41 52 45 44 5f 53 49  ange = SHARED_SI
6f310 5a 45 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61  ZE;.  UnlockArea
6f320 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20  .lOffset = 0L;. 
6f330 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e   UnlockArea.lRan
6f340 67 65 20 3d 20 30 4c 3b 0a 20 20 72 65 74 75 72  ge = 0L;.  retur
6f350 6e 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b  n DosSetFileLock
6f360 73 28 20 69 64 2d 3e 68 2c 20 26 55 6e 6c 6f 63  s( id->h, &Unloc
6f370 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61  kArea, &LockArea
6f380 2c 20 32 30 30 30 4c 2c 20 31 4c 20 29 3b 0a 7d  , 2000L, 1L );.}
6f390 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 61 20 72  ../*.** Undo a r
6f3a0 65 61 64 6c 6f 63 6b 0a 2a 2f 0a 73 74 61 74 69  eadlock.*/.stati
6f3b0 63 20 69 6e 74 20 75 6e 6c 6f 63 6b 52 65 61 64  c int unlockRead
6f3c0 4c 6f 63 6b 28 20 6f 73 32 46 69 6c 65 20 2a 69  Lock( os2File *i
6f3d0 64 20 29 7b 0a 20 20 46 49 4c 45 4c 4f 43 4b 20  d ){.  FILELOCK 
6f3e0 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20   LockArea,.     
6f3f0 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65         UnlockAre
6f400 61 3b 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63  a;.  memset(&Loc
6f410 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66  kArea, 0, sizeof
6f420 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d  (LockArea));.  m
6f430 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65  emset(&UnlockAre
6f440 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c  a, 0, sizeof(Unl
6f450 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 4c 6f 63  ockArea));.  Loc
6f460 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20  kArea.lOffset = 
6f470 30 4c 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c  0L;.  LockArea.l
6f480 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 55 6e  Range = 0L;.  Un
6f490 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74  lockArea.lOffset
6f4a0 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b   = SHARED_FIRST;
6f4b0 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52  .  UnlockArea.lR
6f4c0 61 6e 67 65 20 3d 20 53 48 41 52 45 44 5f 53 49  ange = SHARED_SI
6f4d0 5a 45 3b 0a 20 20 72 65 74 75 72 6e 20 44 6f 73  ZE;.  return Dos
6f4e0 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 69 64  SetFileLocks( id
6f4f0 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61  ->h, &UnlockArea
6f500 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 32 30 30  , &LockArea, 200
6f510 30 4c 2c 20 31 4c 20 29 3b 0a 7d 0a 0a 23 69 66  0L, 1L );.}..#if
6f520 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
6f530 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 2f  _PAGER_PRAGMAS./
6f540 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 20  *.** Check that 
6f550 61 20 67 69 76 65 6e 20 70 61 74 68 6e 61 6d 65  a given pathname
6f560 20 69 73 20 61 20 64 69 72 65 63 74 6f 72 79 20   is a directory 
6f570 61 6e 64 20 69 73 20 77 72 69 74 61 62 6c 65 0a  and is writable.
6f580 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  **.*/.SQLITE_PRI
6f590 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6f5a0 4f 73 32 49 73 44 69 72 57 72 69 74 61 62 6c 65  Os2IsDirWritable
6f5b0 28 20 63 68 61 72 20 2a 7a 44 69 72 6e 61 6d 65  ( char *zDirname
6f5c0 20 29 7b 0a 20 20 46 49 4c 45 53 54 41 54 55 53   ){.  FILESTATUS
6f5d0 33 20 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66  3 fsts3ConfigInf
6f5e0 6f 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d  o;.  APIRET rc =
6f5f0 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 6d 65 6d   NO_ERROR;.  mem
6f600 73 65 74 28 26 66 73 74 73 33 43 6f 6e 66 69 67  set(&fsts3Config
6f610 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Info, 0, sizeof(
6f620 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 29  fsts3ConfigInfo)
6f630 29 3b 0a 20 20 69 66 28 20 7a 44 69 72 6e 61 6d  );.  if( zDirnam
6f640 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  e==0 ) return 0;
6f650 0a 20 20 69 66 28 20 73 74 72 6c 65 6e 28 7a 44  .  if( strlen(zD
6f660 69 72 6e 61 6d 65 29 3e 43 43 48 4d 41 58 50 41  irname)>CCHMAXPA
6f670 54 48 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  TH ) return 0;. 
6f680 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 50 61   rc = DosQueryPa
6f690 74 68 49 6e 66 6f 28 20 28 50 53 5a 29 7a 44 69  thInfo( (PSZ)zDi
6f6a0 72 6e 61 6d 65 2c 20 46 49 4c 5f 53 54 41 4e 44  rname, FIL_STAND
6f6b0 41 52 44 2c 20 26 66 73 74 73 33 43 6f 6e 66 69  ARD, &fsts3Confi
6f6c0 67 49 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 46 49  gInfo, sizeof(FI
6f6d0 4c 45 53 54 41 54 55 53 33 29 20 29 3b 0a 20 20  LESTATUS3) );.  
6f6e0 69 66 28 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52  if( rc != NO_ERR
6f6f0 4f 52 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  OR ) return 0;. 
6f700 20 69 66 28 20 28 66 73 74 73 33 43 6f 6e 66 69   if( (fsts3Confi
6f710 67 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 20 26  gInfo.attrFile &
6f720 20 46 49 4c 45 5f 44 49 52 45 43 54 4f 52 59 29   FILE_DIRECTORY)
6f730 20 21 3d 20 46 49 4c 45 5f 44 49 52 45 43 54 4f   != FILE_DIRECTO
6f740 52 59 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a  RY ) return 0;..
6f750 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65    return 1;.}.#e
6f760 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
6f770 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41  MIT_PAGER_PRAGMA
6f780 53 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b  S */../*.** Lock
6f790 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 74   the file with t
6f7a0 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65  he lock specifie
6f7b0 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c  d by parameter l
6f7c0 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a  ocktype - one.**
6f7d0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
6f7e0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29  g:.**.**     (1)
6f7f0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20   SHARED_LOCK.** 
6f800 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45 44      (2) RESERVED
6f810 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29  _LOCK.**     (3)
6f820 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a   PENDING_LOCK.**
6f830 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 49       (4) EXCLUSI
6f840 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f  VE_LOCK.**.** So
6f850 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71  metimes when req
6f860 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b  uesting one lock
6f870 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e   state, addition
6f880 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a  al lock states.*
6f890 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 69  * are inserted i
6f8a0 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20  n between.  The 
6f8b0 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61  locking might fa
6f8c0 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65  il on one of the
6f8d0 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69   later.** transi
6f8e0 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68  tions leaving th
6f8f0 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66  e lock state dif
6f900 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74  ferent from what
6f910 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74 0a   it started but.
6f920 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f  ** still short o
6f930 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65  f its goal.  The
6f940 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74   following chart
6f950 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77   shows the allow
6f960 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e  ed.** transition
6f970 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74  s and the insert
6f980 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20  ed intermediate 
6f990 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  states:.**.**   
6f9a0 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41   UNLOCKED -> SHA
6f9b0 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44  RED.**    SHARED
6f9c0 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20   -> RESERVED.** 
6f9d0 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45     SHARED -> (PE
6f9e0 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53  NDING) -> EXCLUS
6f9f0 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56  IVE.**    RESERV
6fa00 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20  ED -> (PENDING) 
6fa10 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20  -> EXCLUSIVE.** 
6fa20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58     PENDING -> EX
6fa30 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68  CLUSIVE.**.** Th
6fa40 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  is routine will 
6fa50 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20  only increase a 
6fa60 6c 6f 63 6b 2e 20 20 54 68 65 20 6f 73 32 55 6e  lock.  The os2Un
6fa70 6c 6f 63 6b 28 29 20 72 6f 75 74 69 6e 65 0a 2a  lock() routine.*
6fa80 2a 20 65 72 61 73 65 73 20 61 6c 6c 20 6c 6f 63  * erases all loc
6fa90 6b 73 20 61 74 20 6f 6e 63 65 20 61 6e 64 20 72  ks at once and r
6faa0 65 74 75 72 6e 73 20 75 73 20 69 6d 6d 65 64 69  eturns us immedi
6fab0 61 74 65 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e 67  ately to locking
6fac0 20 6c 65 76 65 6c 20 30 2e 0a 2a 2a 20 49 74 20   level 0..** It 
6fad0 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  is not possible 
6fae0 74 6f 20 6c 6f 77 65 72 20 74 68 65 20 6c 6f 63  to lower the loc
6faf0 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 65 20 73  king level one s
6fb00 74 65 70 20 61 74 20 61 20 74 69 6d 65 2e 20 20  tep at a time.  
6fb10 59 6f 75 0a 2a 2a 20 6d 75 73 74 20 67 6f 20 73  You.** must go s
6fb20 74 72 61 69 67 68 74 20 74 6f 20 6c 6f 63 6b 69  traight to locki
6fb30 6e 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a 69  ng level 0..*/.i
6fb40 6e 74 20 6f 73 32 4c 6f 63 6b 28 20 4f 73 46 69  nt os2Lock( OsFi
6fb50 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b  le *id, int lock
6fb60 74 79 70 65 20 29 7b 0a 20 20 41 50 49 52 45 54  type ){.  APIRET
6fb70 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
6fb80 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
6fb90 64 65 20 66 72 6f 6d 20 73 75 62 72 6f 75 74 69  de from subrouti
6fba0 6e 65 73 20 2a 2f 0a 20 20 41 50 49 52 45 54 20  nes */.  APIRET 
6fbb0 72 65 73 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 20  res = NO_ERROR; 
6fbc0 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20     /* Result of 
6fbd0 61 6e 20 4f 53 2f 32 20 6c 6f 63 6b 20 63 61 6c  an OS/2 lock cal
6fbe0 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 4c 6f  l */.  int newLo
6fbf0 63 6b 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a  cktype;       /*
6fc00 20 53 65 74 20 69 64 2d 3e 6c 6f 63 6b 74 79 70   Set id->locktyp
6fc10 65 20 74 6f 20 74 68 69 73 20 76 61 6c 75 65 20  e to this value 
6fc20 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20 2a  before exiting *
6fc30 2f 0a 20 20 69 6e 74 20 67 6f 74 50 65 6e 64 69  /.  int gotPendi
6fc40 6e 67 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20 54 72  ngLock = 0;/* Tr
6fc50 75 65 20 69 66 20 77 65 20 61 63 71 75 69 72 65  ue if we acquire
6fc60 64 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b  d a PENDING lock
6fc70 20 74 68 69 73 20 74 69 6d 65 20 2a 2f 0a 20 20   this time */.  
6fc80 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72  FILELOCK  LockAr
6fc90 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ea,.            
6fca0 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 6f 73  UnlockArea;.  os
6fcb0 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  2File *pFile = (
6fcc0 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 6d  os2File*)id;.  m
6fcd0 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c  emset(&LockArea,
6fce0 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41   0, sizeof(LockA
6fcf0 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28  rea));.  memset(
6fd00 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20  &UnlockArea, 0, 
6fd10 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65  sizeof(UnlockAre
6fd20 61 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  a));.  assert( p
6fd30 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 4f 53 54  File!=0 );.  OST
6fd40 52 41 43 45 34 28 20 22 4c 4f 43 4b 20 25 64 20  RACE4( "LOCK %d 
6fd50 25 64 20 77 61 73 20 25 64 5c 6e 22 2c 20 70 46  %d was %d\n", pF
6fd60 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65  ile->h, locktype
6fd70 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  , pFile->locktyp
6fd80 65 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  e );..  /* If th
6fd90 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61  ere is already a
6fda0 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79   lock of this ty
6fdb0 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72  pe or more restr
6fdc0 69 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 20  ictive on the.  
6fdd0 2a 2a 20 4f 73 46 69 6c 65 2c 20 64 6f 20 6e 6f  ** OsFile, do no
6fde0 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65  thing. Don't use
6fdf0 20 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65   the end_lock: e
6fe00 78 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a  xit path, as.  *
6fe10 2a 20 73 71 6c 69 74 65 33 4f 73 45 6e 74 65 72  * sqlite3OsEnter
6fe20 4d 75 74 65 78 28 29 20 68 61 73 6e 27 74 20 62  Mutex() hasn't b
6fe30 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a  een called yet..
6fe40 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65    */.  if( pFile
6fe50 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b  ->locktype>=lock
6fe60 74 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75  type ){.    retu
6fe70 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
6fe80 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72  }..  /* Make sur
6fe90 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 65  e the locking se
6fea0 71 75 65 6e 63 65 20 69 73 20 63 6f 72 72 65 63  quence is correc
6feb0 74 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  t.  */.  assert(
6fec0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
6fed0 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63  !=NO_LOCK || loc
6fee0 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
6fef0 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  CK );.  assert( 
6ff00 6c 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44 49 4e  locktype!=PENDIN
6ff10 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65  G_LOCK );.  asse
6ff20 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 52 45  rt( locktype!=RE
6ff30 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70  SERVED_LOCK || p
6ff40 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d  File->locktype==
6ff50 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 0a  SHARED_LOCK );..
6ff60 20 20 2f 2a 20 4c 6f 63 6b 20 74 68 65 20 50 45    /* Lock the PE
6ff70 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 20  NDING_LOCK byte 
6ff80 69 66 20 77 65 20 6e 65 65 64 20 74 6f 20 61 63  if we need to ac
6ff90 71 75 69 72 65 20 61 20 50 45 4e 44 49 4e 47 20  quire a PENDING 
6ffa0 6c 6f 63 6b 20 6f 72 0a 20 20 2a 2a 20 61 20 53  lock or.  ** a S
6ffb0 48 41 52 45 44 20 6c 6f 63 6b 2e 20 20 49 66 20  HARED lock.  If 
6ffc0 77 65 20 61 72 65 20 61 63 71 75 69 72 69 6e 67  we are acquiring
6ffd0 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20   a SHARED lock, 
6ffe0 74 68 65 20 61 63 71 75 69 73 69 74 69 6f 6e 20  the acquisition 
6fff0 6f 66 0a 20 20 2a 2a 20 74 68 65 20 50 45 4e 44  of.  ** the PEND
70000 49 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 20 69 73  ING_LOCK byte is
70010 20 74 65 6d 70 6f 72 61 72 79 2e 0a 20 20 2a 2f   temporary..  */
70020 0a 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d  .  newLocktype =
70030 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
70040 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c  ;.  if( pFile->l
70050 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b  ocktype==NO_LOCK
70060 0a 20 20 20 7c 7c 20 28 6c 6f 63 6b 74 79 70 65  .   || (locktype
70070 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  ==EXCLUSIVE_LOCK
70080 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74   && pFile->lockt
70090 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f  ype==RESERVED_LO
700a0 43 4b 29 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74  CK).  ){.    int
700b0 20 63 6e 74 20 3d 20 33 3b 0a 0a 20 20 20 20 4c   cnt = 3;..    L
700c0 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20  ockArea.lOffset 
700d0 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a  = PENDING_BYTE;.
700e0 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61      LockArea.lRa
700f0 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 55 6e  nge = 1L;.    Un
70100 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74  lockArea.lOffset
70110 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63   = 0L;.    Unloc
70120 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30  kArea.lRange = 0
70130 4c 3b 0a 0a 20 20 20 20 77 68 69 6c 65 28 20 63  L;..    while( c
70140 6e 74 2d 2d 3e 30 20 26 26 20 28 72 65 73 20 3d  nt-->0 && (res =
70150 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73   DosSetFileLocks
70160 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c  ( pFile->h, &Unl
70170 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72  ockArea, &LockAr
70180 65 61 2c 20 32 30 30 30 4c 2c 20 31 4c 29 20 29  ea, 2000L, 1L) )
70190 21 3d 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20  !=NO_ERROR ){.  
701a0 20 20 20 20 2f 2a 20 54 72 79 20 33 20 74 69 6d      /* Try 3 tim
701b0 65 73 20 74 6f 20 67 65 74 20 74 68 65 20 70 65  es to get the pe
701c0 6e 64 69 6e 67 20 6c 6f 63 6b 2e 20 20 54 68 65  nding lock.  The
701d0 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 6d 69   pending lock mi
701e0 67 68 74 20 62 65 0a 20 20 20 20 20 20 2a 2a 20  ght be.      ** 
701f0 68 65 6c 64 20 62 79 20 61 6e 6f 74 68 65 72 20  held by another 
70200 72 65 61 64 65 72 20 70 72 6f 63 65 73 73 20 77  reader process w
70210 68 6f 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 20  ho will release 
70220 69 74 20 6d 6f 6d 65 6e 74 61 72 69 6c 79 2e 0a  it momentarily..
70230 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 4f        */.      O
70240 53 54 52 41 43 45 32 28 20 22 63 6f 75 6c 64 20  STRACE2( "could 
70250 6e 6f 74 20 67 65 74 20 61 20 50 45 4e 44 49 4e  not get a PENDIN
70260 47 20 6c 6f 63 6b 2e 20 63 6e 74 3d 25 64 5c 6e  G lock. cnt=%d\n
70270 22 2c 20 63 6e 74 20 29 3b 0a 20 20 20 20 20 20  ", cnt );.      
70280 44 6f 73 53 6c 65 65 70 28 31 29 3b 0a 20 20 20  DosSleep(1);.   
70290 20 7d 0a 20 20 20 20 67 6f 74 50 65 6e 64 69 6e   }.    gotPendin
702a0 67 4c 6f 63 6b 20 3d 20 72 65 73 3b 0a 20 20 7d  gLock = res;.  }
702b0 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61  ..  /* Acquire a
702c0 20 73 68 61 72 65 64 20 6c 6f 63 6b 0a 20 20 2a   shared lock.  *
702d0 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65  /.  if( locktype
702e0 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26  ==SHARED_LOCK &&
702f0 20 72 65 73 20 29 7b 0a 20 20 20 20 61 73 73 65   res ){.    asse
70300 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  rt( pFile->lockt
70310 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a  ype==NO_LOCK );.
70320 20 20 20 20 72 65 73 20 3d 20 67 65 74 52 65 61      res = getRea
70330 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20  dLock(pFile);.  
70340 20 20 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f    if( res == NO_
70350 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 6e  ERROR ){.      n
70360 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41  ewLocktype = SHA
70370 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a  RED_LOCK;.    }.
70380 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72    }..  /* Acquir
70390 65 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63  e a RESERVED loc
703a0 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63  k.  */.  if( loc
703b0 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f  ktype==RESERVED_
703c0 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a 20  LOCK && res ){. 
703d0 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65     assert( pFile
703e0 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52  ->locktype==SHAR
703f0 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 4c  ED_LOCK );.    L
70400 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20  ockArea.lOffset 
70410 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b  = RESERVED_BYTE;
70420 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52  .    LockArea.lR
70430 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 55  ange = 1L;.    U
70440 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65  nlockArea.lOffse
70450 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f  t = 0L;.    Unlo
70460 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20  ckArea.lRange = 
70470 30 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f  0L;.    res = Do
70480 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70  sSetFileLocks( p
70490 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b  File->h, &Unlock
704a0 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c  Area, &LockArea,
704b0 20 32 30 30 30 4c 2c 20 31 4c 20 29 3b 0a 20 20   2000L, 1L );.  
704c0 20 20 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f    if( res == NO_
704d0 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 6e  ERROR ){.      n
704e0 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 52 45 53  ewLocktype = RES
704f0 45 52 56 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20  ERVED_LOCK;.    
70500 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75  }.  }..  /* Acqu
70510 69 72 65 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f  ire a PENDING lo
70520 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f  ck.  */.  if( lo
70530 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56  cktype==EXCLUSIV
70540 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 7b  E_LOCK && res ){
70550 0a 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65  .    newLocktype
70560 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b   = PENDING_LOCK;
70570 0a 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c  .    gotPendingL
70580 6f 63 6b 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20  ock = 0;.  }..  
70590 2f 2a 20 41 63 71 75 69 72 65 20 61 6e 20 45 58  /* Acquire an EX
705a0 43 4c 55 53 49 56 45 20 6c 6f 63 6b 0a 20 20 2a  CLUSIVE lock.  *
705b0 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65  /.  if( locktype
705c0 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  ==EXCLUSIVE_LOCK
705d0 20 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20 61   && res ){.    a
705e0 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f  ssert( pFile->lo
705f0 63 6b 74 79 70 65 3e 3d 53 48 41 52 45 44 5f 4c  cktype>=SHARED_L
70600 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20 3d  OCK );.    res =
70610 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28   unlockReadLock(
70620 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f 53 54 52  pFile);.    OSTR
70630 41 43 45 32 28 20 22 75 6e 72 65 61 64 6c 6f 63  ACE2( "unreadloc
70640 6b 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73 20 29  k = %d\n", res )
70650 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c  ;.    LockArea.l
70660 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f  Offset = SHARED_
70670 46 49 52 53 54 3b 0a 20 20 20 20 4c 6f 63 6b 41  FIRST;.    LockA
70680 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41  rea.lRange = SHA
70690 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 55 6e  RED_SIZE;.    Un
706a0 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74  lockArea.lOffset
706b0 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63   = 0L;.    Unloc
706c0 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30  kArea.lRange = 0
706d0 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73  L;.    res = Dos
706e0 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46  SetFileLocks( pF
706f0 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41  ile->h, &UnlockA
70700 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20  rea, &LockArea, 
70710 32 30 30 30 4c 2c 20 31 4c 20 29 3b 0a 20 20 20  2000L, 1L );.   
70720 20 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45   if( res == NO_E
70730 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 6e 65  RROR ){.      ne
70740 77 4c 6f 63 6b 74 79 70 65 20 3d 20 45 58 43 4c  wLocktype = EXCL
70750 55 53 49 56 45 5f 4c 4f 43 4b 3b 0a 20 20 20 20  USIVE_LOCK;.    
70760 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 4f 53 54  }else{.      OST
70770 52 41 43 45 32 28 20 22 65 72 72 6f 72 2d 63 6f  RACE2( "error-co
70780 64 65 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73 20  de = %d\n", res 
70790 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
707a0 2f 2a 20 49 66 20 77 65 20 61 72 65 20 68 6f 6c  /* If we are hol
707b0 64 69 6e 67 20 61 20 50 45 4e 44 49 4e 47 20 6c  ding a PENDING l
707c0 6f 63 6b 20 74 68 61 74 20 6f 75 67 68 74 20 74  ock that ought t
707d0 6f 20 62 65 20 72 65 6c 65 61 73 65 64 2c 20 74  o be released, t
707e0 68 65 6e 0a 20 20 2a 2a 20 72 65 6c 65 61 73 65  hen.  ** release
707f0 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20   it now..  */.  
70800 69 66 28 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f  if( gotPendingLo
70810 63 6b 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d  ck && locktype==
70820 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20  SHARED_LOCK ){. 
70830 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66     LockArea.lOff
70840 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f  set = 0L;.    Lo
70850 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20  ckArea.lRange = 
70860 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72  0L;.    UnlockAr
70870 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e  ea.lOffset = PEN
70880 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 55  DING_BYTE;.    U
70890 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65  nlockArea.lRange
708a0 20 3d 20 31 4c 3b 0a 20 20 20 20 44 6f 73 53 65   = 1L;.    DosSe
708b0 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c  tFileLocks( pFil
708c0 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65  e->h, &UnlockAre
708d0 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 32 30  a, &LockArea, 20
708e0 30 30 4c 2c 20 31 4c 20 29 3b 0a 20 20 7d 0a 0a  00L, 1L );.  }..
708f0 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20    /* Update the 
70900 73 74 61 74 65 20 6f 66 20 74 68 65 20 6c 6f 63  state of the loc
70910 6b 20 68 61 73 20 68 65 6c 64 20 69 6e 20 74 68  k has held in th
70920 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  e file descripto
70930 72 20 74 68 65 6e 0a 20 20 2a 2a 20 72 65 74 75  r then.  ** retu
70940 72 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  rn the appropria
70950 74 65 20 72 65 73 75 6c 74 20 63 6f 64 65 2e 0a  te result code..
70960 20 20 2a 2f 0a 20 20 69 66 28 20 72 65 73 20 3d    */.  if( res =
70970 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20  = NO_ERROR ){.  
70980 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
70990 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4f  ;.  }else{.    O
709a0 53 54 52 41 43 45 34 28 20 22 4c 4f 43 4b 20 46  STRACE4( "LOCK F
709b0 41 49 4c 45 44 20 25 64 20 74 72 79 69 6e 67 20  AILED %d trying 
709c0 66 6f 72 20 25 64 20 62 75 74 20 67 6f 74 20 25  for %d but got %
709d0 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a  d\n", pFile->h,.
709e0 20 20 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74             lockt
709f0 79 70 65 2c 20 6e 65 77 4c 6f 63 6b 74 79 70 65  ype, newLocktype
70a00 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   );.    rc = SQL
70a10 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20  ITE_BUSY;.  }.  
70a20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
70a30 3d 20 6e 65 77 4c 6f 63 6b 74 79 70 65 3b 0a 20  = newLocktype;. 
70a40 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
70a50 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
70a60 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72  e checks if ther
70a70 65 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20  e is a RESERVED 
70a80 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65  lock held on the
70a90 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69   specified.** fi
70aa0 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e  le by this or an
70ab0 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e  y other process.
70ac0 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20   If such a lock 
70ad0 69 73 20 68 65 6c 64 2c 20 72 65 74 75 72 6e 0a  is held, return.
70ae0 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 6f 74 68  ** non-zero, oth
70af0 65 72 77 69 73 65 20 7a 65 72 6f 2e 0a 2a 2f 0a  erwise zero..*/.
70b00 69 6e 74 20 6f 73 32 43 68 65 63 6b 52 65 73 65  int os2CheckRese
70b10 72 76 65 64 4c 6f 63 6b 28 20 4f 73 46 69 6c 65  rvedLock( OsFile
70b20 20 2a 69 64 20 29 7b 0a 20 20 41 50 49 52 45 54   *id ){.  APIRET
70b30 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a   rc = NO_ERROR;.
70b40 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65    os2File *pFile
70b50 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b   = (os2File*)id;
70b60 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65  .  assert( pFile
70b70 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 46 69  !=0 );.  if( pFi
70b80 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 52 45  le->locktype>=RE
70b90 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20  SERVED_LOCK ){. 
70ba0 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 4f     rc = 1;.    O
70bb0 53 54 52 41 43 45 33 28 20 22 54 45 53 54 20 57  STRACE3( "TEST W
70bc0 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 6c 6f  R-LOCK %d %d (lo
70bd0 63 61 6c 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  cal)\n", pFile->
70be0 68 2c 20 72 63 20 29 3b 0a 20 20 7d 65 6c 73 65  h, rc );.  }else
70bf0 7b 0a 20 20 20 20 46 49 4c 45 4c 4f 43 4b 20 20  {.    FILELOCK  
70c00 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 20  LockArea,.      
70c10 20 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72          UnlockAr
70c20 65 61 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26  ea;.    memset(&
70c30 4c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a  LockArea, 0, siz
70c40 65 6f 66 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a  eof(LockArea));.
70c50 20 20 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f      memset(&Unlo
70c60 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f  ckArea, 0, sizeo
70c70 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0a  f(UnlockArea));.
70c80 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66      LockArea.lOf
70c90 66 73 65 74 20 3d 20 52 45 53 45 52 56 45 44 5f  fset = RESERVED_
70ca0 42 59 54 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72  BYTE;.    LockAr
70cb0 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a  ea.lRange = 1L;.
70cc0 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c      UnlockArea.l
70cd0 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20  Offset = 0L;.   
70ce0 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e   UnlockArea.lRan
70cf0 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 72 63 20  ge = 0L;.    rc 
70d00 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b  = DosSetFileLock
70d10 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e  s( pFile->h, &Un
70d20 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41  lockArea, &LockA
70d30 72 65 61 2c 20 32 30 30 30 4c 2c 20 31 4c 20 29  rea, 2000L, 1L )
70d40 3b 0a 20 20 20 20 69 66 28 20 72 63 20 3d 3d 20  ;.    if( rc == 
70d50 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20  NO_ERROR ){.    
70d60 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73    LockArea.lOffs
70d70 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 20 20 4c  et = 0L;.      L
70d80 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d  ockArea.lRange =
70d90 20 30 4c 3b 0a 20 20 20 20 20 20 55 6e 6c 6f 63   0L;.      Unloc
70da0 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20  kArea.lOffset = 
70db0 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a 20  RESERVED_BYTE;. 
70dc0 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e       UnlockArea.
70dd0 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20  lRange = 1L;.   
70de0 20 20 20 72 63 20 3d 20 44 6f 73 53 65 74 46 69     rc = DosSetFi
70df0 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e  leLocks( pFile->
70e00 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20  h, &UnlockArea, 
70e10 26 4c 6f 63 6b 41 72 65 61 2c 20 32 30 30 30 4c  &LockArea, 2000L
70e20 2c 20 31 4c 20 29 3b 0a 20 20 20 20 7d 0a 20 20  , 1L );.    }.  
70e30 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 45 53    OSTRACE3( "TES
70e40 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20  T WR-LOCK %d %d 
70e50 28 72 65 6d 6f 74 65 29 5c 6e 22 2c 20 70 46 69  (remote)\n", pFi
70e60 6c 65 2d 3e 68 2c 20 72 63 20 29 3b 0a 20 20 7d  le->h, rc );.  }
70e70 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
70e80 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65  ./*.** Lower the
70e90 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f   locking level o
70ea0 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  n file descripto
70eb0 72 20 69 64 20 74 6f 20 6c 6f 63 6b 74 79 70 65  r id to locktype
70ec0 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d  .  locktype.** m
70ed0 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f  ust be either NO
70ee0 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f  _LOCK or SHARED_
70ef0 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  LOCK..**.** If t
70f00 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  he locking level
70f10 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73   of the file des
70f20 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61  criptor is alrea
70f30 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a  dy at or below.*
70f40 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  * the requested 
70f50 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74  locking level, t
70f60 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
70f70 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 74   no-op..**.** It
70f80 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
70f90 20 66 6f 72 20 74 68 69 73 20 72 6f 75 74 69 6e   for this routin
70fa0 65 20 74 6f 20 66 61 69 6c 20 69 66 20 74 68 65  e to fail if the
70fb0 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
70fc0 0a 2a 2a 20 69 73 20 4e 4f 5f 4c 4f 43 4b 2e 20  .** is NO_LOCK. 
70fd0 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61   If the second a
70fe0 72 67 75 6d 65 6e 74 20 69 73 20 53 48 41 52 45  rgument is SHARE
70ff0 44 5f 4c 4f 43 4b 20 74 68 65 6e 20 74 68 69 73  D_LOCK then this
71000 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d 69 67 68   routine.** migh
71010 74 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  t return SQLITE_
71020 49 4f 45 52 52 3b 0a 2a 2f 0a 69 6e 74 20 6f 73  IOERR;.*/.int os
71030 32 55 6e 6c 6f 63 6b 28 20 4f 73 46 69 6c 65 20  2Unlock( OsFile 
71040 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70  *id, int locktyp
71050 65 20 29 7b 0a 20 20 69 6e 74 20 74 79 70 65 3b  e ){.  int type;
71060 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 53  .  APIRET rc = S
71070 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 6f 73 32 46  QLITE_OK;.  os2F
71080 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73  ile *pFile = (os
71090 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 46 49 4c  2File*)id;.  FIL
710a0 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c  ELOCK  LockArea,
710b0 0a 20 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c  .            Unl
710c0 6f 63 6b 41 72 65 61 3b 0a 20 20 6d 65 6d 73 65  ockArea;.  memse
710d0 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c 20  t(&LockArea, 0, 
710e0 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61 29  sizeof(LockArea)
710f0 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c  );.  memset(&Unl
71100 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65  ockArea, 0, size
71110 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b  of(UnlockArea));
71120 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65  .  assert( pFile
71130 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
71140 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45   locktype<=SHARE
71150 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 4f 53 54 52  D_LOCK );.  OSTR
71160 41 43 45 34 28 20 22 55 4e 4c 4f 43 4b 20 25 64  ACE4( "UNLOCK %d
71170 20 74 6f 20 25 64 20 77 61 73 20 25 64 5c 6e 22   to %d was %d\n"
71180 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b  , pFile->h, lock
71190 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63  type, pFile->loc
711a0 6b 74 79 70 65 20 29 3b 0a 20 20 74 79 70 65 20  ktype );.  type 
711b0 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  = pFile->locktyp
711c0 65 3b 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 45  e;.  if( type>=E
711d0 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b  XCLUSIVE_LOCK ){
711e0 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f  .    LockArea.lO
711f0 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20  ffset = 0L;.    
71200 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  LockArea.lRange 
71210 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b  = 0L;.    Unlock
71220 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53  Area.lOffset = S
71230 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 20  HARED_FIRST;.   
71240 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e   UnlockArea.lRan
71250 67 65 20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45  ge = SHARED_SIZE
71260 3b 0a 20 20 20 20 44 6f 73 53 65 74 46 69 6c 65  ;.    DosSetFile
71270 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c  Locks( pFile->h,
71280 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c   &UnlockArea, &L
71290 6f 63 6b 41 72 65 61 2c 20 32 30 30 30 4c 2c 20  ockArea, 2000L, 
712a0 31 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 6c 6f  1L );.    if( lo
712b0 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c  cktype==SHARED_L
712c0 4f 43 4b 20 26 26 20 67 65 74 52 65 61 64 4c 6f  OCK && getReadLo
712d0 63 6b 28 70 46 69 6c 65 29 20 21 3d 20 4e 4f 5f  ck(pFile) != NO_
712e0 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 2f  ERROR ){.      /
712f0 2a 20 54 68 69 73 20 73 68 6f 75 6c 64 20 6e 65  * This should ne
71300 76 65 72 20 68 61 70 70 65 6e 2e 20 20 57 65 20  ver happen.  We 
71310 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65  should always be
71320 20 61 62 6c 65 20 74 6f 0a 20 20 20 20 20 20 2a   able to.      *
71330 2a 20 72 65 61 63 71 75 69 72 65 20 74 68 65 20  * reacquire the 
71340 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20  read lock */.   
71350 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49     rc = SQLITE_I
71360 4f 45 52 52 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  OERR;.    }.  }.
71370 20 20 69 66 28 20 74 79 70 65 3e 3d 52 45 53 45    if( type>=RESE
71380 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  RVED_LOCK ){.   
71390 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65   LockArea.lOffse
713a0 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b  t = 0L;.    Lock
713b0 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c  Area.lRange = 0L
713c0 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61  ;.    UnlockArea
713d0 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 45 52  .lOffset = RESER
713e0 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e  VED_BYTE;.    Un
713f0 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  lockArea.lRange 
71400 3d 20 31 4c 3b 0a 20 20 20 20 44 6f 73 53 65 74  = 1L;.    DosSet
71410 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65  FileLocks( pFile
71420 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61  ->h, &UnlockArea
71430 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 32 30 30  , &LockArea, 200
71440 30 4c 2c 20 31 4c 20 29 3b 0a 20 20 7d 0a 20 20  0L, 1L );.  }.  
71450 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f  if( locktype==NO
71460 5f 4c 4f 43 4b 20 26 26 20 74 79 70 65 3e 3d 53  _LOCK && type>=S
71470 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20  HARED_LOCK ){.  
71480 20 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b    unlockReadLock
71490 28 70 46 69 6c 65 29 3b 0a 20 20 7d 0a 20 20 69  (pFile);.  }.  i
714a0 66 28 20 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47  f( type>=PENDING
714b0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c 6f 63  _LOCK ){.    Loc
714c0 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20  kArea.lOffset = 
714d0 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61  0L;.    LockArea
714e0 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20  .lRange = 0L;.  
714f0 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66    UnlockArea.lOf
71500 66 73 65 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42  fset = PENDING_B
71510 59 54 45 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41  YTE;.    UnlockA
71520 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b  rea.lRange = 1L;
71530 0a 20 20 20 20 44 6f 73 53 65 74 46 69 6c 65 4c  .    DosSetFileL
71540 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20  ocks( pFile->h, 
71550 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f  &UnlockArea, &Lo
71560 63 6b 41 72 65 61 2c 20 32 30 30 30 4c 2c 20 31  ckArea, 2000L, 1
71570 4c 20 29 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65  L );.  }.  pFile
71580 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63  ->locktype = loc
71590 6b 74 79 70 65 3b 0a 20 20 72 65 74 75 72 6e 20  ktype;.  return 
715a0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 75 72  rc;.}../*.** Tur
715b0 6e 20 61 20 72 65 6c 61 74 69 76 65 20 70 61 74  n a relative pat
715c0 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20 66 75 6c  hname into a ful
715d0 6c 20 70 61 74 68 6e 61 6d 65 2e 20 20 52 65 74  l pathname.  Ret
715e0 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a  urn a pointer.**
715f0 20 74 6f 20 74 68 65 20 66 75 6c 6c 20 70 61 74   to the full pat
71600 68 6e 61 6d 65 20 73 74 6f 72 65 64 20 69 6e 20  hname stored in 
71610 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64 20 66  space obtained f
71620 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63  rom sqliteMalloc
71630 28 29 2e 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69  ()..** The calli
71640 6e 67 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72  ng function is r
71650 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 66  esponsible for f
71660 72 65 65 69 6e 67 20 74 68 69 73 20 73 70 61 63  reeing this spac
71670 65 20 6f 6e 63 65 20 69 74 0a 2a 2a 20 69 73 20  e once it.** is 
71680 6e 6f 20 6c 6f 6e 67 65 72 20 6e 65 65 64 65 64  no longer needed
71690 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
716a0 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65  ATE char *sqlite
716b0 33 4f 73 32 46 75 6c 6c 50 61 74 68 6e 61 6d 65  3Os2FullPathname
716c0 28 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52  ( const char *zR
716d0 65 6c 61 74 69 76 65 20 29 7b 0a 20 20 63 68 61  elative ){.  cha
716e0 72 20 2a 7a 46 75 6c 6c 20 3d 20 30 3b 0a 20 20  r *zFull = 0;.  
716f0 69 66 28 20 73 74 72 63 68 72 28 7a 52 65 6c 61  if( strchr(zRela
71700 74 69 76 65 2c 20 27 3a 27 29 20 29 7b 0a 20 20  tive, ':') ){.  
71710 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
71720 6e 67 28 20 26 7a 46 75 6c 6c 2c 20 7a 52 65 6c  ng( &zFull, zRel
71730 61 74 69 76 65 2c 20 28 63 68 61 72 2a 29 30 20  ative, (char*)0 
71740 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
71750 55 4c 4f 4e 47 20 75 6c 44 72 69 76 65 4e 75 6d  ULONG ulDriveNum
71760 20 3d 20 30 3b 0a 20 20 20 20 55 4c 4f 4e 47 20   = 0;.    ULONG 
71770 75 6c 44 72 69 76 65 4d 61 70 20 3d 20 30 3b 0a  ulDriveMap = 0;.
71780 20 20 20 20 55 4c 4f 4e 47 20 63 62 7a 42 75 66      ULONG cbzBuf
71790 4c 65 6e 20 3d 20 53 51 4c 49 54 45 5f 54 45 4d  Len = SQLITE_TEM
717a0 50 4e 41 4d 45 5f 53 49 5a 45 3b 0a 20 20 20 20  PNAME_SIZE;.    
717b0 63 68 61 72 20 7a 44 72 69 76 65 5b 32 5d 3b 0a  char zDrive[2];.
717c0 20 20 20 20 63 68 61 72 20 2a 7a 42 75 66 66 3b      char *zBuff;
717d0 0a 0a 20 20 20 20 7a 42 75 66 66 20 3d 20 73 71  ..    zBuff = sq
717e0 6c 69 74 65 4d 61 6c 6c 6f 63 28 20 63 62 7a 42  liteMalloc( cbzB
717f0 75 66 4c 65 6e 20 29 3b 0a 20 20 20 20 69 66 28  ufLen );.    if(
71800 20 7a 42 75 66 66 20 21 3d 20 30 20 29 7b 0a 20   zBuff != 0 ){. 
71810 20 20 20 20 20 44 6f 73 51 75 65 72 79 43 75 72       DosQueryCur
71820 72 65 6e 74 44 69 73 6b 28 20 26 75 6c 44 72 69  rentDisk( &ulDri
71830 76 65 4e 75 6d 2c 20 26 75 6c 44 72 69 76 65 4d  veNum, &ulDriveM
71840 61 70 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ap );.      if( 
71850 44 6f 73 51 75 65 72 79 43 75 72 72 65 6e 74 44  DosQueryCurrentD
71860 69 72 28 20 75 6c 44 72 69 76 65 4e 75 6d 2c 20  ir( ulDriveNum, 
71870 28 50 42 59 54 45 29 7a 42 75 66 66 2c 20 26 63  (PBYTE)zBuff, &c
71880 62 7a 42 75 66 4c 65 6e 20 29 20 3d 3d 20 4e 4f  bzBufLen ) == NO
71890 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20  _ERROR ){.      
718a0 20 20 73 70 72 69 6e 74 66 28 20 7a 44 72 69 76    sprintf( zDriv
718b0 65 2c 20 22 25 63 22 2c 20 28 63 68 61 72 29 28  e, "%c", (char)(
718c0 27 41 27 20 2b 20 75 6c 44 72 69 76 65 4e 75 6d  'A' + ulDriveNum
718d0 20 2d 20 31 29 20 29 3b 0a 20 20 20 20 20 20 20   - 1) );.       
718e0 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e   sqlite3SetStrin
718f0 67 28 20 26 7a 46 75 6c 6c 2c 20 7a 44 72 69 76  g( &zFull, zDriv
71900 65 2c 20 22 3a 5c 5c 22 2c 20 7a 42 75 66 66 2c  e, ":\\", zBuff,
71910 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
71920 20 20 20 20 20 20 20 20 20 20 20 22 5c 5c 22 2c             "\\",
71930 20 7a 52 65 6c 61 74 69 76 65 2c 20 28 63 68 61   zRelative, (cha
71940 72 2a 29 30 20 29 3b 0a 20 20 20 20 20 20 7d 0a  r*)0 );.      }.
71950 20 20 20 20 20 20 73 71 6c 69 74 65 46 72 65 65        sqliteFree
71960 28 20 7a 42 75 66 66 20 29 3b 0a 20 20 20 20 7d  ( zBuff );.    }
71970 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 46  .  }.  return zF
71980 75 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  ull;.}../*.** Th
71990 65 20 66 75 6c 6c 53 79 6e 63 20 6f 70 74 69 6f  e fullSync optio
719a0 6e 20 69 73 20 6d 65 61 6e 69 6e 67 6c 65 73 73  n is meaningless
719b0 20 6f 6e 20 6f 73 32 2c 20 6f 72 20 63 6f 72 72   on os2, or corr
719c0 65 63 74 20 6d 65 20 69 66 20 49 27 6d 20 77 72  ect me if I'm wr
719d0 6f 6e 67 2e 20 20 54 68 69 73 20 69 73 20 61 20  ong.  This is a 
719e0 6e 6f 2d 6f 70 2e 0a 2a 2a 20 46 72 6f 6d 20 6f  no-op..** From o
719f0 73 5f 75 6e 69 78 2e 63 3a 20 43 68 61 6e 67 65  s_unix.c: Change
71a00 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
71a10 65 20 66 75 6c 6c 73 79 6e 63 20 66 6c 61 67 20  e fullsync flag 
71a20 69 6e 20 74 68 65 20 67 69 76 65 6e 20 66 69 6c  in the given fil
71a30 65 20 64 65 73 63 72 69 70 74 6f 72 2e 0a 2a 2a  e descriptor..**
71a40 20 46 72 6f 6d 20 6f 73 5f 75 6e 69 78 2e 63 3a   From os_unix.c:
71a50 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29   ((unixFile*)id)
71a60 2d 3e 66 75 6c 6c 53 79 6e 63 20 3d 20 76 3b 0a  ->fullSync = v;.
71a70 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f  */.static void o
71a80 73 32 53 65 74 46 75 6c 6c 53 79 6e 63 28 20 4f  s2SetFullSync( O
71a90 73 46 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 76  sFile *id, int v
71aa0 20 29 7b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a   ){.  return;.}.
71ab0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
71ac0 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 66 69 6c  e underlying fil
71ad0 65 20 68 61 6e 64 6c 65 20 66 6f 72 20 61 6e 20  e handle for an 
71ae0 4f 73 46 69 6c 65 0a 2a 2f 0a 73 74 61 74 69 63  OsFile.*/.static
71af0 20 69 6e 74 20 6f 73 32 46 69 6c 65 48 61 6e 64   int os2FileHand
71b00 6c 65 28 20 4f 73 46 69 6c 65 20 2a 69 64 20 29  le( OsFile *id )
71b10 7b 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29  {.  return (int)
71b20 28 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e  ((os2File*)id)->
71b30 68 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  h;.}../*.** Retu
71b40 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68  rn an integer th
71b50 61 74 20 69 6e 64 69 63 65 73 20 74 68 65 20 74  at indices the t
71b60 79 70 65 20 6f 66 20 6c 6f 63 6b 20 63 75 72 72  ype of lock curr
71b70 65 6e 74 6c 79 20 68 65 6c 64 0a 2a 2a 20 62 79  ently held.** by
71b80 20 74 68 69 73 20 68 61 6e 64 6c 65 2e 20 20 28   this handle.  (
71b90 55 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67  Used for testing
71ba0 20 61 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f 6e   and analysis on
71bb0 6c 79 2e 29 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ly.).*/.static i
71bc0 6e 74 20 6f 73 32 4c 6f 63 6b 53 74 61 74 65 28  nt os2LockState(
71bd0 20 4f 73 46 69 6c 65 20 2a 69 64 20 29 7b 0a 20   OsFile *id ){. 
71be0 20 72 65 74 75 72 6e 20 28 28 6f 73 32 46 69 6c   return ((os2Fil
71bf0 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65  e*)id)->locktype
71c00 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
71c10 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a  n the sector siz
71c20 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68  e in bytes of th
71c30 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f  e underlying blo
71c40 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a  ck device for.**
71c50 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 66   the specified f
71c60 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c 6d  ile. This is alm
71c70 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32 20 62  ost always 512 b
71c80 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20 62 65  ytes, but may be
71c90 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 73  .** larger for s
71ca0 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a  ome devices..**.
71cb0 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 20 61  ** SQLite code a
71cc0 73 73 75 6d 65 73 20 74 68 69 73 20 66 75 6e 63  ssumes this func
71cd0 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c  tion cannot fail
71ce0 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75 6d 65  . It also assume
71cf0 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f  s that.** if two
71d00 20 66 69 6c 65 73 20 61 72 65 20 63 72 65 61 74   files are creat
71d10 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 66  ed in the same f
71d20 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 65 63  ile-system direc
71d30 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20  tory (i.e..** a 
71d40 64 61 74 61 62 61 73 65 20 61 6e 64 20 69 74 27  database and it'
71d50 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20  s journal file) 
71d60 74 68 61 74 20 74 68 65 20 73 65 63 74 6f 72 20  that the sector 
71d70 73 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 65  size will be the
71d80 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f 74  .** same for bot
71d90 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  h..*/.static int
71da0 20 6f 73 32 53 65 63 74 6f 72 53 69 7a 65 28 4f   os2SectorSize(O
71db0 73 46 69 6c 65 20 2a 69 64 29 7b 0a 20 20 72 65  sFile *id){.  re
71dc0 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 45 46 41  turn SQLITE_DEFA
71dd0 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3b  ULT_SECTOR_SIZE;
71de0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76  .}../*.** This v
71df0 65 63 74 6f 72 20 64 65 66 69 6e 65 73 20 61 6c  ector defines al
71e00 6c 20 74 68 65 20 6d 65 74 68 6f 64 73 20 74 68  l the methods th
71e10 61 74 20 63 61 6e 20 6f 70 65 72 61 74 65 20 6f  at can operate o
71e20 6e 20 61 6e 20 4f 73 46 69 6c 65 0a 2a 2a 20 66  n an OsFile.** f
71e30 6f 72 20 6f 73 32 2e 0a 2a 2f 0a 73 74 61 74 69  or os2..*/.stati
71e40 63 20 63 6f 6e 73 74 20 49 6f 4d 65 74 68 6f 64  c const IoMethod
71e50 20 73 71 6c 69 74 65 33 4f 73 32 49 6f 4d 65 74   sqlite3Os2IoMet
71e60 68 6f 64 20 3d 20 7b 0a 20 20 6f 73 32 43 6c 6f  hod = {.  os2Clo
71e70 73 65 2c 0a 20 20 6f 73 32 4f 70 65 6e 44 69 72  se,.  os2OpenDir
71e80 65 63 74 6f 72 79 2c 0a 20 20 6f 73 32 52 65 61  ectory,.  os2Rea
71e90 64 2c 0a 20 20 6f 73 32 57 72 69 74 65 2c 0a 20  d,.  os2Write,. 
71ea0 20 6f 73 32 53 65 65 6b 2c 0a 20 20 6f 73 32 54   os2Seek,.  os2T
71eb0 72 75 6e 63 61 74 65 2c 0a 20 20 6f 73 32 53 79  runcate,.  os2Sy
71ec0 6e 63 2c 0a 20 20 6f 73 32 53 65 74 46 75 6c 6c  nc,.  os2SetFull
71ed0 53 79 6e 63 2c 0a 20 20 6f 73 32 46 69 6c 65 48  Sync,.  os2FileH
71ee0 61 6e 64 6c 65 2c 0a 20 20 6f 73 32 46 69 6c 65  andle,.  os2File
71ef0 53 69 7a 65 2c 0a 20 20 6f 73 32 4c 6f 63 6b 2c  Size,.  os2Lock,
71f00 0a 20 20 6f 73 32 55 6e 6c 6f 63 6b 2c 0a 20 20  .  os2Unlock,.  
71f10 6f 73 32 4c 6f 63 6b 53 74 61 74 65 2c 0a 20 20  os2LockState,.  
71f20 6f 73 32 43 68 65 63 6b 52 65 73 65 72 76 65 64  os2CheckReserved
71f30 4c 6f 63 6b 2c 0a 20 20 6f 73 32 53 65 63 74 6f  Lock,.  os2Secto
71f40 72 53 69 7a 65 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  rSize,.};../*.**
71f50 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79   Allocate memory
71f60 20 66 6f 72 20 61 6e 20 4f 73 46 69 6c 65 2e 20   for an OsFile. 
71f70 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20   Initialize the 
71f80 6e 65 77 20 4f 73 46 69 6c 65 0a 2a 2a 20 74 6f  new OsFile.** to
71f90 20 74 68 65 20 76 61 6c 75 65 20 67 69 76 65 6e   the value given
71fa0 20 69 6e 20 70 49 6e 69 74 20 61 6e 64 20 72 65   in pInit and re
71fb0 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
71fc0 6f 20 74 68 65 20 6e 65 77 0a 2a 2a 20 4f 73 46  o the new.** OsF
71fd0 69 6c 65 2e 20 20 49 66 20 77 65 20 72 75 6e 20  ile.  If we run 
71fe0 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 2c 20 63  out of memory, c
71ff0 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20 61 6e  lose the file an
72000 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a  d return NULL..*
72010 2f 0a 69 6e 74 20 61 6c 6c 6f 63 61 74 65 4f 73  /.int allocateOs
72020 32 46 69 6c 65 28 20 6f 73 32 46 69 6c 65 20 2a  2File( os2File *
72030 70 49 6e 69 74 2c 20 4f 73 46 69 6c 65 20 2a 2a  pInit, OsFile **
72040 70 6c 64 20 29 7b 0a 20 20 6f 73 32 46 69 6c 65  pld ){.  os2File
72050 20 2a 70 4e 65 77 3b 0a 20 20 70 4e 65 77 20 3d   *pNew;.  pNew =
72060 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 20 73   sqliteMalloc( s
72070 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20 29 3b 0a  izeof(*pNew) );.
72080 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b    if( pNew==0 ){
72090 0a 20 20 20 20 44 6f 73 43 6c 6f 73 65 28 20 70  .    DosClose( p
720a0 49 6e 69 74 2d 3e 68 20 29 3b 0a 20 20 20 20 2a  Init->h );.    *
720b0 70 6c 64 20 3d 20 30 3b 0a 20 20 20 20 72 65 74  pld = 0;.    ret
720c0 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
720d0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a  ;.  }else{.    *
720e0 70 4e 65 77 20 3d 20 2a 70 49 6e 69 74 3b 0a 20  pNew = *pInit;. 
720f0 20 20 20 70 4e 65 77 2d 3e 70 4d 65 74 68 6f 64     pNew->pMethod
72100 20 3d 20 26 73 71 6c 69 74 65 33 4f 73 32 49 6f   = &sqlite3Os2Io
72110 4d 65 74 68 6f 64 3b 0a 20 20 20 20 70 4e 65 77  Method;.    pNew
72120 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f  ->locktype = NO_
72130 4c 4f 43 4b 3b 0a 20 20 20 20 2a 70 6c 64 20 3d  LOCK;.    *pld =
72140 20 28 4f 73 46 69 6c 65 2a 29 70 4e 65 77 3b 0a   (OsFile*)pNew;.
72150 20 20 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28      OpenCounter(
72160 2b 31 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  +1);.    return 
72170 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d  SQLITE_OK;.  }.}
72180 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ..#endif /* SQLI
72190 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 20 2a  TE_OMIT_DISKIO *
721a0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
721b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
721c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
721d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
721e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
721f0 2a 20 45 76 65 72 79 74 68 69 6e 67 20 61 62 6f  * Everything abo
72200 76 65 20 64 65 61 6c 73 20 77 69 74 68 20 66 69  ve deals with fi
72210 6c 65 20 49 2f 4f 2e 20 20 45 76 65 72 79 74 68  le I/O.  Everyth
72220 69 6e 67 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73  ing that follows
72230 20 64 65 61 6c 73 0a 2a 2a 20 77 69 74 68 20 6f   deals.** with o
72240 74 68 65 72 20 6d 69 73 63 65 6c 6c 61 6e 6f 75  ther miscellanou
72250 73 20 61 73 70 65 63 74 73 20 6f 66 20 74 68 65  s aspects of the
72260 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   operating syste
72270 6d 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 2a 2a  m interface.****
72280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
72290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
722a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
722b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
722c0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 69 66 6e 64  ********/..#ifnd
722d0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c  ef SQLITE_OMIT_L
722e0 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a  OAD_EXTENSION./*
722f0 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 20 66  .** Interfaces f
72300 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 61  or opening a sha
72310 72 65 64 20 6c 69 62 72 61 72 79 2c 20 66 69 6e  red library, fin
72320 64 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e 74  ding entry point
72330 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20  s.** within the 
72340 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20  shared library, 
72350 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 20  and closing the 
72360 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2e 0a  shared library..
72370 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
72380 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4f  E void *sqlite3O
72390 73 32 44 6c 6f 70 65 6e 28 63 6f 6e 73 74 20 63  s2Dlopen(const c
723a0 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b  har *zFilename){
723b0 0a 20 20 55 43 48 41 52 20 6c 6f 61 64 45 72 72  .  UCHAR loadErr
723c0 5b 32 35 36 5d 3b 0a 20 20 48 4d 4f 44 55 4c 45  [256];.  HMODULE
723d0 20 68 6d 6f 64 3b 0a 20 20 41 50 49 52 45 54 20   hmod;.  APIRET 
723e0 72 63 3b 0a 20 20 72 63 20 3d 20 44 6f 73 4c 6f  rc;.  rc = DosLo
723f0 61 64 4d 6f 64 75 6c 65 28 28 50 53 5a 29 6c 6f  adModule((PSZ)lo
72400 61 64 45 72 72 2c 20 73 69 7a 65 6f 66 28 6c 6f  adErr, sizeof(lo
72410 61 64 45 72 72 29 2c 20 7a 46 69 6c 65 6e 61 6d  adErr), zFilenam
72420 65 2c 20 26 68 6d 6f 64 29 3b 0a 20 20 69 66 20  e, &hmod);.  if 
72430 28 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 29  (rc != NO_ERROR)
72440 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 72 65 74   return 0;.  ret
72450 75 72 6e 20 28 76 6f 69 64 2a 29 68 6d 6f 64 3b  urn (void*)hmod;
72460 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
72470 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4f  E void *sqlite3O
72480 73 32 44 6c 73 79 6d 28 76 6f 69 64 20 2a 70 48  s2Dlsym(void *pH
72490 61 6e 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61  andle, const cha
724a0 72 20 2a 7a 53 79 6d 62 6f 6c 29 7b 0a 20 20 50  r *zSymbol){.  P
724b0 46 4e 20 70 66 6e 3b 0a 20 20 41 50 49 52 45 54  FN pfn;.  APIRET
724c0 20 72 63 3b 0a 20 20 72 63 20 3d 20 44 6f 73 51   rc;.  rc = DosQ
724d0 75 65 72 79 50 72 6f 63 41 64 64 72 28 28 48 4d  ueryProcAddr((HM
724e0 4f 44 55 4c 45 29 70 48 61 6e 64 6c 65 2c 20 30  ODULE)pHandle, 0
724f0 4c 2c 20 7a 53 79 6d 62 6f 6c 2c 20 26 70 66 6e  L, zSymbol, &pfn
72500 29 3b 0a 20 20 69 66 20 28 72 63 20 21 3d 20 4e  );.  if (rc != N
72510 4f 5f 45 52 52 4f 52 29 20 7b 0a 20 20 20 20 2f  O_ERROR) {.    /
72520 2a 20 69 66 20 74 68 65 20 73 79 6d 62 6f 6c 20  * if the symbol 
72530 69 74 73 65 6c 66 20 77 61 73 20 6e 6f 74 20 66  itself was not f
72540 6f 75 6e 64 2c 20 73 65 61 72 63 68 20 61 67 61  ound, search aga
72550 69 6e 20 66 6f 72 20 74 68 65 20 73 61 6d 65 0a  in for the same.
72560 20 20 20 20 20 2a 20 73 79 6d 62 6f 6c 20 77 69       * symbol wi
72570 74 68 20 61 6e 20 65 78 74 72 61 20 75 6e 64 65  th an extra unde
72580 72 73 63 6f 72 65 2c 20 74 68 61 74 20 6d 69 67  rscore, that mig
72590 68 74 20 62 65 20 6e 65 65 64 65 64 20 64 65 70  ht be needed dep
725a0 65 6e 64 69 6e 67 0a 20 20 20 20 20 2a 20 6f 6e  ending.     * on
725b0 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 63 6f 6e   the calling con
725c0 76 65 6e 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 63  vention */.    c
725d0 68 61 72 20 5f 7a 53 79 6d 62 6f 6c 5b 32 35 36  har _zSymbol[256
725e0 5d 20 3d 20 22 5f 22 3b 0a 20 20 20 20 73 74 72  ] = "_";.    str
725f0 6e 63 61 74 28 5f 7a 53 79 6d 62 6f 6c 2c 20 7a  ncat(_zSymbol, z
72600 53 79 6d 62 6f 6c 2c 20 32 35 35 29 3b 0a 20 20  Symbol, 255);.  
72610 20 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 50    rc = DosQueryP
72620 72 6f 63 41 64 64 72 28 28 48 4d 4f 44 55 4c 45  rocAddr((HMODULE
72630 29 70 48 61 6e 64 6c 65 2c 20 30 4c 2c 20 5f 7a  )pHandle, 0L, _z
72640 53 79 6d 62 6f 6c 2c 20 26 70 66 6e 29 3b 0a 20  Symbol, &pfn);. 
72650 20 7d 0a 20 20 69 66 20 28 72 63 20 21 3d 20 4e   }.  if (rc != N
72660 4f 5f 45 52 52 4f 52 29 20 72 65 74 75 72 6e 20  O_ERROR) return 
72670 30 3b 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69  0;.  return (voi
72680 64 20 2a 29 70 66 6e 3b 0a 7d 0a 53 51 4c 49 54  d *)pfn;.}.SQLIT
72690 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
726a0 6c 69 74 65 33 4f 73 32 44 6c 63 6c 6f 73 65 28  lite3Os2Dlclose(
726b0 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a  void *pHandle){.
726c0 20 20 72 65 74 75 72 6e 20 44 6f 73 46 72 65 65    return DosFree
726d0 4d 6f 64 75 6c 65 28 28 48 4d 4f 44 55 4c 45 29  Module((HMODULE)
726e0 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6e 64  pHandle);.}.#end
726f0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
72700 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e  T_LOAD_EXTENSION
72710 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20   */.../*.** Get 
72720 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 73  information to s
72730 65 65 64 20 74 68 65 20 72 61 6e 64 6f 6d 20 6e  eed the random n
72740 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 2e  umber generator.
72750 20 20 54 68 65 20 73 65 65 64 0a 2a 2a 20 69 73    The seed.** is
72760 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68   written into th
72770 65 20 62 75 66 66 65 72 20 7a 42 75 66 5b 32 35  e buffer zBuf[25
72780 36 5d 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67  6].  The calling
72790 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 0a 2a   function must.*
727a0 2a 20 73 75 70 70 6c 79 20 61 20 73 75 66 66 69  * supply a suffi
727b0 63 69 65 6e 74 6c 79 20 6c 61 72 67 65 20 62 75  ciently large bu
727c0 66 66 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ffer..*/.SQLITE_
727d0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
727e0 74 65 33 4f 73 32 52 61 6e 64 6f 6d 53 65 65 64  te3Os2RandomSeed
727f0 28 20 63 68 61 72 20 2a 7a 42 75 66 20 29 7b 0a  ( char *zBuf ){.
72800 20 20 2f 2a 20 57 65 20 68 61 76 65 20 74 6f 20    /* We have to 
72810 69 6e 69 74 69 61 6c 69 7a 65 20 7a 42 75 66 20  initialize zBuf 
72820 74 6f 20 70 72 65 76 65 6e 74 20 76 61 6c 67 72  to prevent valgr
72830 69 6e 64 20 66 72 6f 6d 20 72 65 70 6f 72 74 69  ind from reporti
72840 6e 67 0a 20 20 2a 2a 20 65 72 72 6f 72 73 2e 20  ng.  ** errors. 
72850 20 54 68 65 20 72 65 70 6f 72 74 73 20 69 73 73   The reports iss
72860 75 65 64 20 62 79 20 76 61 6c 67 72 69 6e 64 20  ued by valgrind 
72870 61 72 65 20 69 6e 63 6f 72 72 65 63 74 20 2d 20  are incorrect - 
72880 77 65 20 77 6f 75 6c 64 0a 20 20 2a 2a 20 70 72  we would.  ** pr
72890 65 66 65 72 20 74 68 61 74 20 74 68 65 20 72 61  efer that the ra
728a0 6e 64 6f 6d 6e 65 73 73 20 62 65 20 69 6e 63 72  ndomness be incr
728b0 65 61 73 65 64 20 62 79 20 6d 61 6b 69 6e 67 20  eased by making 
728c0 75 73 65 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20  use of the.  ** 
728d0 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 73 70  uninitialized sp
728e0 61 63 65 20 69 6e 20 7a 42 75 66 20 2d 20 62 75  ace in zBuf - bu
728f0 74 20 76 61 6c 67 72 69 6e 64 20 65 72 72 6f 72  t valgrind error
72900 73 20 74 65 6e 64 20 74 6f 20 77 6f 72 72 79 0a  s tend to worry.
72910 20 20 2a 2a 20 73 6f 6d 65 20 75 73 65 72 73 2e    ** some users.
72920 20 20 52 61 74 68 65 72 20 74 68 61 6e 20 61 72    Rather than ar
72930 67 75 65 2c 20 69 74 20 73 65 65 6d 73 20 65 61  gue, it seems ea
72940 73 69 65 72 20 6a 75 73 74 20 74 6f 20 69 6e 69  sier just to ini
72950 74 69 61 6c 69 7a 65 0a 20 20 2a 2a 20 74 68 65  tialize.  ** the
72960 20 77 68 6f 6c 65 20 61 72 72 61 79 20 61 6e 64   whole array and
72970 20 73 69 6c 65 6e 63 65 20 76 61 6c 67 72 69 6e   silence valgrin
72980 64 2c 20 65 76 65 6e 20 69 66 20 74 68 61 74 20  d, even if that 
72990 6d 65 61 6e 73 20 6c 65 73 73 20 72 61 6e 64 6f  means less rando
729a0 6d 6e 65 73 73 0a 20 20 2a 2a 20 69 6e 20 74 68  mness.  ** in th
729b0 65 20 72 61 6e 64 6f 6d 20 73 65 65 64 2e 0a 20  e random seed.. 
729c0 20 2a 2a 0a 20 20 2a 2a 20 57 68 65 6e 20 74 65   **.  ** When te
729d0 73 74 69 6e 67 2c 20 69 6e 69 74 69 61 6c 69 7a  sting, initializ
729e0 69 6e 67 20 7a 42 75 66 5b 5d 20 74 6f 20 7a 65  ing zBuf[] to ze
729f0 72 6f 20 69 73 20 61 6c 6c 20 77 65 20 64 6f 2e  ro is all we do.
72a00 20 20 54 68 61 74 20 6d 65 61 6e 73 0a 20 20 2a    That means.  *
72a10 2a 20 74 68 61 74 20 77 65 20 61 6c 77 61 79 73  * that we always
72a20 20 75 73 65 20 74 68 65 20 73 61 6d 65 20 72 61   use the same ra
72a30 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 73 65 71 75  ndom number sequ
72a40 65 6e 63 65 2e 20 54 68 69 73 20 6d 61 6b 65 73  ence. This makes
72a50 20 74 68 65 0a 20 20 2a 2a 20 74 65 73 74 73 20   the.  ** tests 
72a60 72 65 70 65 61 74 61 62 6c 65 2e 0a 20 20 2a 2f  repeatable..  */
72a70 0a 20 20 6d 65 6d 73 65 74 28 20 7a 42 75 66 2c  .  memset( zBuf,
72a80 20 30 2c 20 32 35 36 20 29 3b 0a 20 20 44 6f 73   0, 256 );.  Dos
72a90 47 65 74 44 61 74 65 54 69 6d 65 28 20 28 50 44  GetDateTime( (PD
72aa0 41 54 45 54 49 4d 45 29 7a 42 75 66 20 29 3b 0a  ATETIME)zBuf );.
72ab0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
72ac0 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65  OK;.}../*.** Sle
72ad0 65 70 20 66 6f 72 20 61 20 6c 69 74 74 6c 65 20  ep for a little 
72ae0 77 68 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 74  while.  Return t
72af0 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d  he amount of tim
72b00 65 20 73 6c 65 70 74 2e 0a 2a 2f 0a 53 51 4c 49  e slept..*/.SQLI
72b10 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
72b20 71 6c 69 74 65 33 4f 73 32 53 6c 65 65 70 28 20  qlite3Os2Sleep( 
72b30 69 6e 74 20 6d 73 20 29 7b 0a 20 20 44 6f 73 53  int ms ){.  DosS
72b40 6c 65 65 70 28 20 6d 73 20 29 3b 0a 20 20 72 65  leep( ms );.  re
72b50 74 75 72 6e 20 6d 73 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn ms;.}../*.*
72b60 2a 20 53 74 61 74 69 63 20 76 61 72 69 61 62 6c  * Static variabl
72b70 65 73 20 75 73 65 64 20 66 6f 72 20 74 68 72 65  es used for thre
72b80 61 64 20 73 79 6e 63 68 72 6f 6e 69 7a 61 74 69  ad synchronizati
72b90 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  on.*/.static int
72ba0 20 69 6e 4d 75 74 65 78 20 3d 20 30 3b 0a 23 69   inMutex = 0;.#i
72bb0 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 32 5f  fdef SQLITE_OS2_
72bc0 54 48 52 45 41 44 53 0a 73 74 61 74 69 63 20 55  THREADS.static U
72bd0 4c 4f 4e 47 20 6d 75 74 65 78 4f 77 6e 65 72 3b  LONG mutexOwner;
72be0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
72bf0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 61 69  he following pai
72c00 72 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 69 6d  r of routines im
72c10 70 6c 65 6d 65 6e 74 20 6d 75 74 75 61 6c 20 65  plement mutual e
72c20 78 63 6c 75 73 69 6f 6e 20 66 6f 72 0a 2a 2a 20  xclusion for.** 
72c30 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 70  multi-threaded p
72c40 72 6f 63 65 73 73 65 73 2e 20 20 4f 6e 6c 79 20  rocesses.  Only 
72c50 61 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64 20  a single thread 
72c60 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f 0a 2a 2a  is allowed to.**
72c70 20 65 78 65 63 75 74 65 64 20 63 6f 64 65 20 74   executed code t
72c80 68 61 74 20 69 73 20 73 75 72 72 6f 75 6e 64 65  hat is surrounde
72c90 64 20 62 79 20 45 6e 74 65 72 4d 75 74 65 78 28  d by EnterMutex(
72ca0 29 20 61 6e 64 20 4c 65 61 76 65 4d 75 74 65 78  ) and LeaveMutex
72cb0 28 29 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65  ()..**.** SQLite
72cc0 20 75 73 65 73 20 6f 6e 6c 79 20 61 20 73 69 6e   uses only a sin
72cd0 67 6c 65 20 4d 75 74 65 78 2e 20 20 54 68 65 72  gle Mutex.  Ther
72ce0 65 20 69 73 20 6e 6f 74 20 6d 75 63 68 20 63 72  e is not much cr
72cf0 69 74 69 63 61 6c 0a 2a 2a 20 63 6f 64 65 20 61  itical.** code a
72d00 6e 64 20 77 68 61 74 20 6c 69 74 74 6c 65 20 74  nd what little t
72d10 68 65 72 65 20 69 73 20 65 78 65 63 75 74 65 73  here is executes
72d20 20 71 75 69 63 6b 6c 79 20 61 6e 64 20 77 69 74   quickly and wit
72d30 68 6f 75 74 20 62 6c 6f 63 6b 69 6e 67 2e 0a 2a  hout blocking..*
72d40 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
72d50 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 73 32   void sqlite3Os2
72d60 45 6e 74 65 72 4d 75 74 65 78 28 29 7b 0a 23 69  EnterMutex(){.#i
72d70 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 32 5f  fdef SQLITE_OS2_
72d80 54 48 52 45 41 44 53 0a 20 20 50 54 49 42 20 70  THREADS.  PTIB p
72d90 74 69 62 3b 0a 20 20 44 6f 73 45 6e 74 65 72 43  tib;.  DosEnterC
72da0 72 69 74 53 65 63 28 29 3b 0a 20 20 44 6f 73 47  ritSec();.  DosG
72db0 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 20 26 70  etInfoBlocks( &p
72dc0 74 69 62 2c 20 4e 55 4c 4c 20 29 3b 0a 20 20 6d  tib, NULL );.  m
72dd0 75 74 65 78 4f 77 6e 65 72 20 3d 20 70 74 69 62  utexOwner = ptib
72de0 2d 3e 74 69 62 5f 70 74 69 62 32 2d 3e 74 69 62  ->tib_ptib2->tib
72df0 32 5f 75 6c 74 69 64 3b 0a 23 65 6e 64 69 66 0a  2_ultid;.#endif.
72e00 20 20 61 73 73 65 72 74 28 20 21 69 6e 4d 75 74    assert( !inMut
72e10 65 78 20 29 3b 0a 20 20 69 6e 4d 75 74 65 78 20  ex );.  inMutex 
72e20 3d 20 31 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  = 1;.}.SQLITE_PR
72e30 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
72e40 65 33 4f 73 32 4c 65 61 76 65 4d 75 74 65 78 28  e3Os2LeaveMutex(
72e50 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ){.#ifdef SQLITE
72e60 5f 4f 53 32 5f 54 48 52 45 41 44 53 0a 20 20 50  _OS2_THREADS.  P
72e70 54 49 42 20 70 74 69 62 3b 0a 23 65 6e 64 69 66  TIB ptib;.#endif
72e80 0a 20 20 61 73 73 65 72 74 28 20 69 6e 4d 75 74  .  assert( inMut
72e90 65 78 20 29 3b 0a 20 20 69 6e 4d 75 74 65 78 20  ex );.  inMutex 
72ea0 3d 20 30 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  = 0;.#ifdef SQLI
72eb0 54 45 5f 4f 53 32 5f 54 48 52 45 41 44 53 0a 20  TE_OS2_THREADS. 
72ec0 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b   DosGetInfoBlock
72ed0 73 28 20 26 70 74 69 62 2c 20 4e 55 4c 4c 20 29  s( &ptib, NULL )
72ee0 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 75 74 65  ;.  assert( mute
72ef0 78 4f 77 6e 65 72 20 3d 3d 20 70 74 69 62 2d 3e  xOwner == ptib->
72f00 74 69 62 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f  tib_ptib2->tib2_
72f10 75 6c 74 69 64 20 29 3b 0a 20 20 44 6f 73 45 78  ultid );.  DosEx
72f20 69 74 43 72 69 74 53 65 63 28 29 3b 0a 23 65 6e  itCritSec();.#en
72f30 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  dif.}../*.** Ret
72f40 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20  urn TRUE if the 
72f50 6d 75 74 65 78 20 69 73 20 63 75 72 72 65 6e 74  mutex is current
72f60 6c 79 20 68 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 49  ly held..**.** I
72f70 66 20 74 68 65 20 74 68 69 73 54 68 72 65 61 64  f the thisThread
72f80 4f 6e 6c 79 20 70 61 72 61 6d 65 74 65 72 20 69  Only parameter i
72f90 73 20 74 72 75 65 2c 20 72 65 74 75 72 6e 20 74  s true, return t
72fa0 72 75 65 20 69 66 20 61 6e 64 20 6f 6e 6c 79 20  rue if and only 
72fb0 69 66 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 69 6e  if the.** callin
72fc0 67 20 74 68 72 65 61 64 20 68 6f 6c 64 73 20 74  g thread holds t
72fd0 68 65 20 6d 75 74 65 78 2e 20 20 49 66 20 74 68  he mutex.  If th
72fe0 65 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 66  e parameter is f
72ff0 61 6c 73 65 2c 20 72 65 74 75 72 6e 0a 2a 2a 20  alse, return.** 
73000 74 72 75 65 20 69 66 20 61 6e 79 20 74 68 72 65  true if any thre
73010 61 64 20 68 6f 6c 64 73 20 74 68 65 20 6d 75 74  ad holds the mut
73020 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ex..*/.SQLITE_PR
73030 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
73040 33 4f 73 32 49 6e 4d 75 74 65 78 28 20 69 6e 74  3Os2InMutex( int
73050 20 74 68 69 73 54 68 72 65 61 64 4f 6e 6c 79 20   thisThreadOnly 
73060 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ){.#ifdef SQLITE
73070 5f 4f 53 32 5f 54 48 52 45 41 44 53 0a 20 20 50  _OS2_THREADS.  P
73080 54 49 42 20 70 74 69 62 3b 0a 20 20 44 6f 73 47  TIB ptib;.  DosG
73090 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 20 26 70  etInfoBlocks( &p
730a0 74 69 62 2c 20 4e 55 4c 4c 20 29 3b 0a 20 20 72  tib, NULL );.  r
730b0 65 74 75 72 6e 20 69 6e 4d 75 74 65 78 3e 30 20  eturn inMutex>0 
730c0 26 26 20 28 74 68 69 73 54 68 72 65 61 64 4f 6e  && (thisThreadOn
730d0 6c 79 3d 3d 30 20 7c 7c 20 6d 75 74 65 78 4f 77  ly==0 || mutexOw
730e0 6e 65 72 3d 3d 70 74 69 62 2d 3e 74 69 62 5f 70  ner==ptib->tib_p
730f0 74 69 62 32 2d 3e 74 69 62 32 5f 75 6c 74 69 64  tib2->tib2_ultid
73100 29 3b 0a 23 65 6c 73 65 0a 20 20 72 65 74 75 72  );.#else.  retur
73110 6e 20 69 6e 4d 75 74 65 78 3e 30 3b 0a 23 65 6e  n inMutex>0;.#en
73120 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  dif.}../*.** The
73130 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61   following varia
73140 62 6c 65 2c 20 69 66 20 73 65 74 20 74 6f 20 61  ble, if set to a
73150 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2c   non-zero value,
73160 20 62 65 63 6f 6d 65 73 20 74 68 65 20 72 65 73   becomes the res
73170 75 6c 74 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20  ult.** returned 
73180 66 72 6f 6d 20 73 71 6c 69 74 65 33 4f 73 43 75  from sqlite3OsCu
73190 72 72 65 6e 74 54 69 6d 65 28 29 2e 20 20 54 68  rrentTime().  Th
731a0 69 73 20 69 73 20 75 73 65 64 20 66 6f 72 20 74  is is used for t
731b0 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 64 65  esting..*/.#ifde
731c0 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 69 6e  f SQLITE_TEST.in
731d0 74 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e  t sqlite3_curren
731e0 74 5f 74 69 6d 65 20 3d 20 30 3b 0a 23 65 6e 64  t_time = 0;.#end
731f0 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74  if../*.** Find t
73200 68 65 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20  he current time 
73210 28 69 6e 20 55 6e 69 76 65 72 73 61 6c 20 43 6f  (in Universal Co
73220 6f 72 64 69 6e 61 74 65 64 20 54 69 6d 65 29 2e  ordinated Time).
73230 20 20 57 72 69 74 65 20 74 68 65 0a 2a 2a 20 63    Write the.** c
73240 75 72 72 65 6e 74 20 74 69 6d 65 20 61 6e 64 20  urrent time and 
73250 64 61 74 65 20 61 73 20 61 20 4a 75 6c 69 61 6e  date as a Julian
73260 20 44 61 79 20 6e 75 6d 62 65 72 20 69 6e 74 6f   Day number into
73270 20 2a 70 72 4e 6f 77 20 61 6e 64 0a 2a 2a 20 72   *prNow and.** r
73280 65 74 75 72 6e 20 30 2e 20 20 52 65 74 75 72 6e  eturn 0.  Return
73290 20 31 20 69 66 20 74 68 65 20 74 69 6d 65 20 61   1 if the time a
732a0 6e 64 20 64 61 74 65 20 63 61 6e 6e 6f 74 20 62  nd date cannot b
732b0 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 53 51 4c 49  e found..*/.SQLI
732c0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
732d0 71 6c 69 74 65 33 4f 73 32 43 75 72 72 65 6e 74  qlite3Os2Current
732e0 54 69 6d 65 28 20 64 6f 75 62 6c 65 20 2a 70 72  Time( double *pr
732f0 4e 6f 77 20 29 7b 0a 20 20 64 6f 75 62 6c 65 20  Now ){.  double 
73300 6e 6f 77 3b 0a 20 20 55 53 48 4f 52 54 20 73 65  now;.  USHORT se
73310 63 6f 6e 64 2c 20 6d 69 6e 75 74 65 2c 20 68 6f  cond, minute, ho
73320 75 72 2c 0a 20 20 20 20 20 20 20 20 20 64 61 79  ur,.         day
73330 2c 20 6d 6f 6e 74 68 2c 20 79 65 61 72 3b 0a 20  , month, year;. 
73340 20 44 41 54 45 54 49 4d 45 20 64 74 3b 0a 20 20   DATETIME dt;.  
73350 44 6f 73 47 65 74 44 61 74 65 54 69 6d 65 28 20  DosGetDateTime( 
73360 26 64 74 20 29 3b 0a 20 20 73 65 63 6f 6e 64 20  &dt );.  second 
73370 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 73 65 63  = (USHORT)dt.sec
73380 6f 6e 64 73 3b 0a 20 20 6d 69 6e 75 74 65 20 3d  onds;.  minute =
73390 20 28 55 53 48 4f 52 54 29 64 74 2e 6d 69 6e 75   (USHORT)dt.minu
733a0 74 65 73 20 2b 20 64 74 2e 74 69 6d 65 7a 6f 6e  tes + dt.timezon
733b0 65 3b 0a 20 20 68 6f 75 72 20 3d 20 28 55 53 48  e;.  hour = (USH
733c0 4f 52 54 29 64 74 2e 68 6f 75 72 73 3b 0a 20 20  ORT)dt.hours;.  
733d0 64 61 79 20 3d 20 28 55 53 48 4f 52 54 29 64 74  day = (USHORT)dt
733e0 2e 64 61 79 3b 0a 20 20 6d 6f 6e 74 68 20 3d 20  .day;.  month = 
733f0 28 55 53 48 4f 52 54 29 64 74 2e 6d 6f 6e 74 68  (USHORT)dt.month
73400 3b 0a 20 20 79 65 61 72 20 3d 20 28 55 53 48 4f  ;.  year = (USHO
73410 52 54 29 64 74 2e 79 65 61 72 3b 0a 0a 20 20 2f  RT)dt.year;..  /
73420 2a 20 43 61 6c 63 75 6c 61 74 69 6f 6e 73 20 66  * Calculations f
73430 72 6f 6d 20 68 74 74 70 3a 2f 2f 77 77 77 2e 61  rom http://www.a
73440 73 74 72 6f 2e 6b 65 65 6c 65 2e 61 63 2e 75 6b  stro.keele.ac.uk
73450 2f 7e 72 6e 6f 2f 41 73 74 72 6f 6e 6f 6d 79 2f  /~rno/Astronomy/
73460 68 6a 64 2e 68 74 6d 6c 0a 20 20 20 20 20 68 74  hjd.html.     ht
73470 74 70 3a 2f 2f 77 77 77 2e 61 73 74 72 6f 2e 6b  tp://www.astro.k
73480 65 65 6c 65 2e 61 63 2e 75 6b 2f 7e 72 6e 6f 2f  eele.ac.uk/~rno/
73490 41 73 74 72 6f 6e 6f 6d 79 2f 68 6a 64 2d 30 2e  Astronomy/hjd-0.
734a0 31 2e 63 20 2a 2f 0a 20 20 2f 2a 20 43 61 6c 63  1.c */.  /* Calc
734b0 75 6c 61 74 65 20 74 68 65 20 4a 75 6c 69 61 6e  ulate the Julian
734c0 20 64 61 79 73 20 2a 2f 0a 20 20 6e 6f 77 20 3d   days */.  now =
734d0 20 64 61 79 20 2d 20 33 32 30 37 36 20 2b 0a 20   day - 32076 +. 
734e0 20 20 20 31 34 36 31 2a 28 79 65 61 72 20 2b 20     1461*(year + 
734f0 34 38 30 30 20 2b 20 28 6d 6f 6e 74 68 20 2d 20  4800 + (month - 
73500 31 34 29 2f 31 32 29 2f 34 20 2b 0a 20 20 20 20  14)/12)/4 +.    
73510 33 36 37 2a 28 6d 6f 6e 74 68 20 2d 20 32 20 2d  367*(month - 2 -
73520 20 28 6d 6f 6e 74 68 20 2d 20 31 34 29 2f 31 32   (month - 14)/12
73530 2a 31 32 29 2f 31 32 20 2d 0a 20 20 20 20 33 2a  *12)/12 -.    3*
73540 28 28 79 65 61 72 20 2b 20 34 39 30 30 20 2b 20  ((year + 4900 + 
73550 28 6d 6f 6e 74 68 20 2d 20 31 34 29 2f 31 32 29  (month - 14)/12)
73560 2f 31 30 30 29 2f 34 3b 0a 0a 20 20 2f 2a 20 41  /100)/4;..  /* A
73570 64 64 20 74 68 65 20 66 72 61 63 74 69 6f 6e 61  dd the fractiona
73580 6c 20 68 6f 75 72 73 2c 20 6d 69 6e 73 20 61 6e  l hours, mins an
73590 64 20 73 65 63 6f 6e 64 73 20 2a 2f 0a 20 20 6e  d seconds */.  n
735a0 6f 77 20 2b 3d 20 28 68 6f 75 72 20 2b 20 31 32  ow += (hour + 12
735b0 2e 30 29 2f 32 34 2e 30 3b 0a 20 20 6e 6f 77 20  .0)/24.0;.  now 
735c0 2b 3d 20 6d 69 6e 75 74 65 2f 31 34 34 30 2e 30  += minute/1440.0
735d0 3b 0a 20 20 6e 6f 77 20 2b 3d 20 73 65 63 6f 6e  ;.  now += secon
735e0 64 2f 38 36 34 30 30 2e 30 3b 0a 20 20 2a 70 72  d/86400.0;.  *pr
735f0 4e 6f 77 20 3d 20 6e 6f 77 3b 0a 23 69 66 64 65  Now = now;.#ifde
73600 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20  f SQLITE_TEST.  
73610 69 66 28 20 73 71 6c 69 74 65 33 5f 63 75 72 72  if( sqlite3_curr
73620 65 6e 74 5f 74 69 6d 65 20 29 7b 0a 20 20 20 20  ent_time ){.    
73630 2a 70 72 4e 6f 77 20 3d 20 73 71 6c 69 74 65 33  *prNow = sqlite3
73640 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 2f 38 36  _current_time/86
73650 34 30 30 2e 30 20 2b 20 32 34 34 30 35 38 37 2e  400.0 + 2440587.
73660 35 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  5;.  }.#endif.  
73670 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
73680 2a 2a 20 52 65 6d 65 6d 62 65 72 20 74 68 65 20  ** Remember the 
73690 6e 75 6d 62 65 72 20 6f 66 20 74 68 72 65 61 64  number of thread
736a0 2d 73 70 65 63 69 66 69 63 2d 64 61 74 61 20 62  -specific-data b
736b0 6c 6f 63 6b 73 20 61 6c 6c 6f 63 61 74 65 64 2e  locks allocated.
736c0 0a 2a 2a 20 55 73 65 20 74 68 69 73 20 74 6f 20  .** Use this to 
736d0 76 65 72 69 66 79 20 74 68 61 74 20 77 65 20 61  verify that we a
736e0 72 65 20 6e 6f 74 20 6c 65 61 6b 69 6e 67 20 74  re not leaking t
736f0 68 72 65 61 64 2d 73 70 65 63 69 66 69 63 2d 64  hread-specific-d
73700 61 74 61 2e 0a 2a 2a 20 54 69 63 6b 65 74 20 23  ata..** Ticket #
73710 31 36 30 31 0a 2a 2f 0a 23 69 66 64 65 66 20 53  1601.*/.#ifdef S
73720 51 4c 49 54 45 5f 54 45 53 54 0a 69 6e 74 20 73  QLITE_TEST.int s
73730 71 6c 69 74 65 33 5f 74 73 64 5f 63 6f 75 6e 74  qlite3_tsd_count
73740 20 3d 20 30 3b 0a 23 20 64 65 66 69 6e 65 20 54   = 0;.# define T
73750 53 44 5f 43 4f 55 4e 54 45 52 5f 49 4e 43 52 20  SD_COUNTER_INCR 
73760 49 6e 74 65 72 6c 6f 63 6b 65 64 49 6e 63 72 65  InterlockedIncre
73770 6d 65 6e 74 28 20 26 73 71 6c 69 74 65 33 5f 74  ment( &sqlite3_t
73780 73 64 5f 63 6f 75 6e 74 20 29 0a 23 20 64 65 66  sd_count ).# def
73790 69 6e 65 20 54 53 44 5f 43 4f 55 4e 54 45 52 5f  ine TSD_COUNTER_
737a0 44 45 43 52 20 49 6e 74 65 72 6c 6f 63 6b 65 64  DECR Interlocked
737b0 44 65 63 72 65 6d 65 6e 74 28 20 26 73 71 6c 69  Decrement( &sqli
737c0 74 65 33 5f 74 73 64 5f 63 6f 75 6e 74 20 29 0a  te3_tsd_count ).
737d0 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 54  #else.# define T
737e0 53 44 5f 43 4f 55 4e 54 45 52 5f 49 4e 43 52 20  SD_COUNTER_INCR 
737f0 20 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f 0a 23 20 64   /* no-op */.# d
73800 65 66 69 6e 65 20 54 53 44 5f 43 4f 55 4e 54 45  efine TSD_COUNTE
73810 52 5f 44 45 43 52 20 20 2f 2a 20 6e 6f 2d 6f 70  R_DECR  /* no-op
73820 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a   */.#endif../*.*
73830 2a 20 49 66 20 63 61 6c 6c 65 64 20 77 69 74 68  * If called with
73840 20 61 6c 6c 6f 63 61 74 65 46 6c 61 67 3e 31 2c   allocateFlag>1,
73850 20 74 68 65 6e 20 72 65 74 75 72 6e 20 61 20 70   then return a p
73860 6f 69 6e 74 65 72 20 74 6f 20 74 68 72 65 61 64  ointer to thread
73870 0a 2a 2a 20 73 70 65 63 69 66 69 63 20 64 61 74  .** specific dat
73880 61 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e  a for the curren
73890 74 20 74 68 72 65 61 64 2e 20 20 41 6c 6c 6f 63  t thread.  Alloc
738a0 61 74 65 20 61 6e 64 20 7a 65 72 6f 20 74 68 65  ate and zero the
738b0 0a 2a 2a 20 74 68 72 65 61 64 2d 73 70 65 63 69  .** thread-speci
738c0 66 69 63 20 64 61 74 61 20 69 66 20 69 74 20 64  fic data if it d
738d0 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20  oes not already 
738e0 65 78 69 73 74 20 6e 65 63 65 73 73 61 72 79 2e  exist necessary.
738f0 0a 2a 2a 0a 2a 2a 20 49 66 20 63 61 6c 6c 65 64  .**.** If called
73900 20 77 69 74 68 20 61 6c 6c 6f 63 61 74 65 46 6c   with allocateFl
73910 61 67 3d 3d 30 2c 20 74 68 65 6e 20 63 68 65 63  ag==0, then chec
73920 6b 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 68  k the current th
73930 72 65 61 64 0a 2a 2a 20 73 70 65 63 69 66 69 63  read.** specific
73940 20 64 61 74 61 2e 20 20 52 65 74 75 72 6e 20 69   data.  Return i
73950 74 20 69 66 20 69 74 20 65 78 69 73 74 73 2e 20  t if it exists. 
73960 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20   If it does not 
73970 65 78 69 73 74 2c 0a 2a 2a 20 74 68 65 6e 20 72  exist,.** then r
73980 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a  eturn NULL..**.*
73990 2a 20 49 66 20 63 61 6c 6c 65 64 20 77 69 74 68  * If called with
739a0 20 61 6c 6c 6f 63 61 74 65 46 6c 61 67 3c 30 2c   allocateFlag<0,
739b0 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   check to see if
739c0 20 74 68 65 20 74 68 72 65 61 64 20 73 70 65 63   the thread spec
739d0 69 66 69 63 0a 2a 2a 20 64 61 74 61 20 69 73 20  ific.** data is 
739e0 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 69 73  allocated and is
739f0 20 61 6c 6c 20 7a 65 72 6f 2e 20 20 49 66 20 69   all zero.  If i
73a00 74 20 69 73 20 74 68 65 6e 20 64 65 61 6c 6c 6f  t is then deallo
73a10 63 61 74 65 20 69 74 2e 0a 2a 2a 20 52 65 74 75  cate it..** Retu
73a20 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
73a30 74 68 65 20 74 68 72 65 61 64 20 73 70 65 63 69  the thread speci
73a40 66 69 63 20 64 61 74 61 20 6f 72 20 4e 55 4c 4c  fic data or NULL
73a50 20 69 66 20 69 74 20 69 73 0a 2a 2a 20 75 6e 61   if it is.** una
73a60 6c 6c 6f 63 61 74 65 64 20 6f 72 20 67 65 74 73  llocated or gets
73a70 20 64 65 61 6c 6c 6f 63 61 74 65 64 2e 0a 2a 2f   deallocated..*/
73a80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
73a90 54 68 72 65 61 64 44 61 74 61 20 2a 73 71 6c 69  ThreadData *sqli
73aa0 74 65 33 4f 73 32 54 68 72 65 61 64 53 70 65 63  te3Os2ThreadSpec
73ab0 69 66 69 63 44 61 74 61 28 20 69 6e 74 20 61 6c  ificData( int al
73ac0 6c 6f 63 61 74 65 46 6c 61 67 20 29 7b 0a 20 20  locateFlag ){.  
73ad0 73 74 61 74 69 63 20 54 68 72 65 61 64 44 61 74  static ThreadDat
73ae0 61 20 2a 2a 73 5f 70 70 54 73 64 20 3d 20 4e 55  a **s_ppTsd = NU
73af0 4c 4c 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e  LL;.  static con
73b00 73 74 20 54 68 72 65 61 64 44 61 74 61 20 7a 65  st ThreadData ze
73b10 72 6f 44 61 74 61 20 3d 20 7b 30 2c 20 30 2c 20  roData = {0, 0, 
73b20 30 7d 3b 0a 20 20 54 68 72 65 61 64 44 61 74 61  0};.  ThreadData
73b30 20 2a 70 54 73 64 3b 0a 0a 20 20 69 66 28 20 21   *pTsd;..  if( !
73b40 73 5f 70 70 54 73 64 20 29 7b 0a 20 20 20 20 73  s_ppTsd ){.    s
73b50 71 6c 69 74 65 33 4f 73 45 6e 74 65 72 4d 75 74  qlite3OsEnterMut
73b60 65 78 28 29 3b 0a 20 20 20 20 69 66 28 20 21 73  ex();.    if( !s
73b70 5f 70 70 54 73 64 20 29 7b 0a 20 20 20 20 20 20  _ppTsd ){.      
73b80 50 55 4c 4f 4e 47 20 70 75 6c 3b 0a 20 20 20 20  PULONG pul;.    
73b90 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 44 6f    APIRET rc = Do
73ba0 73 41 6c 6c 6f 63 54 68 72 65 61 64 4c 6f 63 61  sAllocThreadLoca
73bb0 6c 4d 65 6d 6f 72 79 28 31 2c 20 26 70 75 6c 29  lMemory(1, &pul)
73bc0 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 21  ;.      if( rc !
73bd0 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20  = NO_ERROR ){.  
73be0 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4c        sqlite3OsL
73bf0 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20  eaveMutex();.   
73c00 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
73c10 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 5f 70       }.      s_p
73c20 70 54 73 64 20 3d 20 28 54 68 72 65 61 64 44 61  pTsd = (ThreadDa
73c30 74 61 20 2a 2a 29 70 75 6c 3b 0a 20 20 20 20 7d  ta **)pul;.    }
73c40 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4c 65  .    sqlite3OsLe
73c50 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a  aveMutex();.  }.
73c60 20 20 70 54 73 64 20 3d 20 2a 73 5f 70 70 54 73    pTsd = *s_ppTs
73c70 64 3b 0a 20 20 69 66 28 20 61 6c 6c 6f 63 61 74  d;.  if( allocat
73c80 65 46 6c 61 67 3e 30 20 29 7b 0a 20 20 20 20 69  eFlag>0 ){.    i
73c90 66 28 20 21 70 54 73 64 20 29 7b 0a 20 20 20 20  f( !pTsd ){.    
73ca0 20 20 70 54 73 64 20 3d 20 73 71 6c 69 74 65 33    pTsd = sqlite3
73cb0 4f 73 4d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66  OsMalloc( sizeof
73cc0 28 7a 65 72 6f 44 61 74 61 29 20 29 3b 0a 20 20  (zeroData) );.  
73cd0 20 20 20 20 69 66 28 20 70 54 73 64 20 29 7b 0a      if( pTsd ){.
73ce0 20 20 20 20 20 20 20 20 2a 70 54 73 64 20 3d 20          *pTsd = 
73cf0 7a 65 72 6f 44 61 74 61 3b 0a 20 20 20 20 20 20  zeroData;.      
73d00 20 20 2a 73 5f 70 70 54 73 64 20 3d 20 70 54 73    *s_ppTsd = pTs
73d10 64 3b 0a 20 20 20 20 20 20 20 20 54 53 44 5f 43  d;.        TSD_C
73d20 4f 55 4e 54 45 52 5f 49 4e 43 52 3b 0a 20 20 20  OUNTER_INCR;.   
73d30 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c     }.    }.  }el
73d40 73 65 20 69 66 28 20 70 54 73 64 21 3d 30 20 26  se if( pTsd!=0 &
73d50 26 20 61 6c 6c 6f 63 61 74 65 46 6c 61 67 3c 30  & allocateFlag<0
73d60 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26  .              &
73d70 26 20 6d 65 6d 63 6d 70 28 20 70 54 73 64 2c 20  & memcmp( pTsd, 
73d80 26 7a 65 72 6f 44 61 74 61 2c 20 73 69 7a 65 6f  &zeroData, sizeo
73d90 66 28 54 68 72 65 61 64 44 61 74 61 29 20 29 3d  f(ThreadData) )=
73da0 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
73db0 33 4f 73 46 72 65 65 28 70 54 73 64 29 3b 0a 20  3OsFree(pTsd);. 
73dc0 20 20 20 2a 73 5f 70 70 54 73 64 20 3d 20 4e 55     *s_ppTsd = NU
73dd0 4c 4c 3b 0a 20 20 20 20 54 53 44 5f 43 4f 55 4e  LL;.    TSD_COUN
73de0 54 45 52 5f 44 45 43 52 3b 0a 20 20 20 20 70 54  TER_DECR;.    pT
73df0 73 64 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65  sd = 0;.  }.  re
73e00 74 75 72 6e 20 70 54 73 64 3b 0a 7d 0a 23 65 6e  turn pTsd;.}.#en
73e10 64 69 66 20 2f 2a 20 4f 53 5f 4f 53 32 20 2a 2f  dif /* OS_OS2 */
73e20 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
73e30 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 6f 73 32 2e  * End of os_os2.
73e40 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
73e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
73e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
73e70 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
73e80 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f  * Begin file os_
73e90 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  unix.c *********
73ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
73eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
73ec0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79  /./*.** 2004 May
73ed0 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   22.**.** The au
73ee0 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
73ef0 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
73f00 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
73f10 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
73f20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
73f30 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
73f40 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
73f50 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
73f60 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
73f70 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
73f80 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
73f90 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
73fa0 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
73fb0 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
73fc0 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
73fd0 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
73fe0 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
73ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
74000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
74010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
74020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
74030 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
74040 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
74050 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 73 70   code that is sp
74060 65 63 69 66 69 63 20 74 6f 20 55 6e 69 78 20 73  ecific to Unix s
74070 79 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66 20 4f  ystems..*/.#if O
74080 53 5f 55 4e 49 58 20 20 20 20 20 20 20 20 20 20  S_UNIX          
74090 20 20 20 20 2f 2a 20 54 68 69 73 20 66 69 6c 65      /* This file
740a0 20 69 73 20 75 73 65 64 20 6f 6e 20 75 6e 69 78   is used on unix
740b0 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f 2a 20 23 64 65   only */../* #de
740c0 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e 41 42  fine SQLITE_ENAB
740d0 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
740e0 20 30 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65   0 */../*.** The
740f0 73 65 20 23 64 65 66 69 6e 65 73 20 73 68 6f 75  se #defines shou
74100 6c 64 20 65 6e 61 62 6c 65 20 3e 32 47 42 20 66  ld enable >2GB f
74110 69 6c 65 20 73 75 70 70 6f 72 74 20 6f 6e 20 50  ile support on P
74120 6f 73 69 78 20 69 66 20 74 68 65 0a 2a 2a 20 75  osix if the.** u
74130 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74  nderlying operat
74140 69 6e 67 20 73 79 73 74 65 6d 20 73 75 70 70 6f  ing system suppo
74150 72 74 73 20 69 74 2e 20 20 49 66 20 74 68 65 20  rts it.  If the 
74160 4f 53 20 6c 61 63 6b 73 0a 2a 2a 20 6c 61 72 67  OS lacks.** larg
74170 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 2c 20  e file support, 
74180 74 68 65 73 65 20 73 68 6f 75 6c 64 20 62 65 20  these should be 
74190 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61  no-ops..**.** La
741a0 72 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74  rge file support
741b0 20 63 61 6e 20 62 65 20 64 69 73 61 62 6c 65 64   can be disabled
741c0 20 75 73 69 6e 67 20 74 68 65 20 2d 44 53 51 4c   using the -DSQL
741d0 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20  ITE_DISABLE_LFS 
741e0 73 77 69 74 63 68 0a 2a 2a 20 6f 6e 20 74 68 65  switch.** on the
741f0 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e   compiler comman
74200 64 20 6c 69 6e 65 2e 20 20 54 68 69 73 20 69 73  d line.  This is
74210 20 6e 65 63 65 73 73 61 72 79 20 69 66 20 79 6f   necessary if yo
74220 75 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 0a  u are compiling.
74230 2a 2a 20 6f 6e 20 61 20 72 65 63 65 6e 74 20 6d  ** on a recent m
74240 61 63 68 69 6e 65 20 28 65 78 3a 20 52 65 64 48  achine (ex: RedH
74250 61 74 20 37 2e 32 29 20 62 75 74 20 79 6f 75 20  at 7.2) but you 
74260 77 61 6e 74 20 79 6f 75 72 20 63 6f 64 65 20 74  want your code t
74270 6f 20 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61 6e 20  o work.** on an 
74280 6f 6c 64 65 72 20 6d 61 63 68 69 6e 65 20 28 65  older machine (e
74290 78 3a 20 52 65 64 48 61 74 20 36 2e 30 29 2e 20  x: RedHat 6.0). 
742a0 20 49 66 20 79 6f 75 20 63 6f 6d 70 69 6c 65 20   If you compile 
742b0 6f 6e 20 52 65 64 48 61 74 20 37 2e 32 0a 2a 2a  on RedHat 7.2.**
742c0 20 77 69 74 68 6f 75 74 20 74 68 69 73 20 6f 70   without this op
742d0 74 69 6f 6e 2c 20 4c 46 53 20 69 73 20 65 6e 61  tion, LFS is ena
742e0 62 6c 65 2e 20 20 42 75 74 20 4c 46 53 20 64 6f  ble.  But LFS do
742f0 65 73 20 6e 6f 74 20 65 78 69 73 74 20 69 6e 20  es not exist in 
74300 74 68 65 20 6b 65 72 6e 65 6c 0a 2a 2a 20 69 6e  the kernel.** in
74310 20 52 65 64 48 61 74 20 36 2e 30 2c 20 73 6f 20   RedHat 6.0, so 
74320 74 68 65 20 63 6f 64 65 20 77 6f 6e 27 74 20 77  the code won't w
74330 6f 72 6b 2e 20 20 48 65 6e 63 65 2c 20 66 6f 72  ork.  Hence, for
74340 20 6d 61 78 69 6d 75 6d 20 62 69 6e 61 72 79 0a   maximum binary.
74350 2a 2a 20 70 6f 72 74 61 62 69 6c 69 74 79 20 79  ** portability y
74360 6f 75 20 73 68 6f 75 6c 64 20 6f 6d 69 74 20 4c  ou should omit L
74370 46 53 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  FS..*/.#ifndef S
74380 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46  QLITE_DISABLE_LF
74390 53 0a 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 47  S.# define _LARG
743a0 45 5f 46 49 4c 45 20 20 20 20 20 20 20 31 0a 23  E_FILE       1.#
743b0 20 69 66 6e 64 65 66 20 5f 46 49 4c 45 5f 4f 46   ifndef _FILE_OF
743c0 46 53 45 54 5f 42 49 54 53 0a 23 20 20 20 64 65  FSET_BITS.#   de
743d0 66 69 6e 65 20 5f 46 49 4c 45 5f 4f 46 46 53 45  fine _FILE_OFFSE
743e0 54 5f 42 49 54 53 20 36 34 0a 23 20 65 6e 64 69  T_BITS 64.# endi
743f0 66 0a 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 47  f.# define _LARG
74400 45 46 49 4c 45 5f 53 4f 55 52 43 45 20 31 0a 23  EFILE_SOURCE 1.#
74410 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 73 74 61  endif../*.** sta
74420 6e 64 61 72 64 20 69 6e 63 6c 75 64 65 20 66 69  ndard include fi
74430 6c 65 73 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65  les..*/.#include
74440 20 3c 73 79 73 2f 74 79 70 65 73 2e 68 3e 0a 23   <sys/types.h>.#
74450 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 73 74 61  include <sys/sta
74460 74 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 66  t.h>.#include <f
74470 63 6e 74 6c 2e 68 3e 0a 23 69 6e 63 6c 75 64 65  cntl.h>.#include
74480 20 3c 75 6e 69 73 74 64 2e 68 3e 0a 23 69 6e 63   <unistd.h>.#inc
74490 6c 75 64 65 20 3c 73 79 73 2f 74 69 6d 65 2e 68  lude <sys/time.h
744a0 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 65 72 72 6e  >.#include <errn
744b0 6f 2e 68 3e 0a 23 69 66 64 65 66 20 53 51 4c 49  o.h>.#ifdef SQLI
744c0 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
744d0 47 5f 53 54 59 4c 45 0a 23 69 6e 63 6c 75 64 65  G_STYLE.#include
744e0 20 3c 73 79 73 2f 69 6f 63 74 6c 2e 68 3e 0a 23   <sys/ioctl.h>.#
744f0 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 70 61 72  include <sys/par
74500 61 6d 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c  am.h>.#include <
74510 73 79 73 2f 6d 6f 75 6e 74 2e 68 3e 0a 23 65 6e  sys/mount.h>.#en
74520 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e  dif /* SQLITE_EN
74530 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
74540 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  LE */../*.** If 
74550 77 65 20 61 72 65 20 74 6f 20 62 65 20 74 68 72  we are to be thr
74560 65 61 64 2d 73 61 66 65 2c 20 69 6e 63 6c 75 64  ead-safe, includ
74570 65 20 74 68 65 20 70 74 68 72 65 61 64 73 20 68  e the pthreads h
74580 65 61 64 65 72 20 61 6e 64 20 64 65 66 69 6e 65  eader and define
74590 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f 55  .** the SQLITE_U
745a0 4e 49 58 5f 54 48 52 45 41 44 53 20 6d 61 63 72  NIX_THREADS macr
745b0 6f 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 54 48  o..*/.#ifndef TH
745c0 52 45 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e  READSAFE.# defin
745d0 65 20 54 48 52 45 41 44 53 41 46 45 20 31 0a 23  e THREADSAFE 1.#
745e0 65 6e 64 69 66 0a 23 69 66 20 54 48 52 45 41 44  endif.#if THREAD
745f0 53 41 46 45 0a 23 20 69 6e 63 6c 75 64 65 20 3c  SAFE.# include <
74600 70 74 68 72 65 61 64 2e 68 3e 0a 23 20 64 65 66  pthread.h>.# def
74610 69 6e 65 20 53 51 4c 49 54 45 5f 55 4e 49 58 5f  ine SQLITE_UNIX_
74620 54 48 52 45 41 44 53 20 31 0a 23 65 6e 64 69 66  THREADS 1.#endif
74630 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 61 75 6c 74 20  ../*.** Default 
74640 70 65 72 6d 69 73 73 69 6f 6e 73 20 77 68 65 6e  permissions when
74650 20 63 72 65 61 74 69 6e 67 20 61 20 6e 65 77 20   creating a new 
74660 66 69 6c 65 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  file.*/.#ifndef 
74670 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46  SQLITE_DEFAULT_F
74680 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 0a  ILE_PERMISSIONS.
74690 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
746a0 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52  DEFAULT_FILE_PER
746b0 4d 49 53 53 49 4f 4e 53 20 30 36 34 34 0a 23 65  MISSIONS 0644.#e
746c0 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68  ndif..../*.** Th
746d0 65 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63  e unixFile struc
746e0 74 75 72 65 20 69 73 20 73 75 62 63 6c 61 73 73  ture is subclass
746f0 20 6f 66 20 4f 73 46 69 6c 65 20 73 70 65 63 69   of OsFile speci
74700 66 69 63 20 66 6f 72 20 74 68 65 20 75 6e 69 78  fic for the unix
74710 0a 2a 2a 20 70 72 6f 74 61 62 69 6c 69 74 79 20  .** protability 
74720 6c 61 79 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65  layer..*/.typede
74730 66 20 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c  f struct unixFil
74740 65 20 75 6e 69 78 46 69 6c 65 3b 0a 73 74 72 75  e unixFile;.stru
74750 63 74 20 75 6e 69 78 46 69 6c 65 20 7b 0a 20 20  ct unixFile {.  
74760 49 6f 4d 65 74 68 6f 64 20 63 6f 6e 73 74 20 2a  IoMethod const *
74770 70 4d 65 74 68 6f 64 3b 20 20 2f 2a 20 41 6c 77  pMethod;  /* Alw
74780 61 79 73 20 74 68 65 20 66 69 72 73 74 20 65 6e  ays the first en
74790 74 72 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  try */.  struct 
747a0 6f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 3b 20  openCnt *pOpen; 
747b0 20 20 20 2f 2a 20 49 6e 66 6f 20 61 62 6f 75 74     /* Info about
747c0 20 61 6c 6c 20 6f 70 65 6e 20 66 64 27 73 20 6f   all open fd's o
747d0 6e 20 74 68 69 73 20 69 6e 6f 64 65 20 2a 2f 0a  n this inode */.
747e0 20 20 73 74 72 75 63 74 20 6c 6f 63 6b 49 6e 66    struct lockInf
747f0 6f 20 2a 70 4c 6f 63 6b 3b 20 20 20 2f 2a 20 49  o *pLock;   /* I
74800 6e 66 6f 20 61 62 6f 75 74 20 6c 6f 63 6b 73 20  nfo about locks 
74810 6f 6e 20 74 68 69 73 20 69 6e 6f 64 65 20 2a 2f  on this inode */
74820 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
74830 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
74840 59 4c 45 0a 20 20 76 6f 69 64 20 2a 6c 6f 63 6b  YLE.  void *lock
74850 69 6e 67 43 6f 6e 74 65 78 74 3b 20 20 20 20 20  ingContext;     
74860 2f 2a 20 4c 6f 63 6b 69 6e 67 20 73 74 79 6c 65  /* Locking style
74870 20 73 70 65 63 69 66 69 63 20 73 74 61 74 65 20   specific state 
74880 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  */.#endif /* SQL
74890 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
748a0 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 20 20 69 6e  NG_STYLE */.  in
748b0 74 20 68 3b 20 20 20 20 20 20 20 20 20 20 20 20  t h;            
748c0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66          /* The f
748d0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a  ile descriptor *
748e0 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  /.  unsigned cha
748f0 72 20 6c 6f 63 6b 74 79 70 65 3b 20 20 20 2f 2a  r locktype;   /*
74900 20 54 68 65 20 74 79 70 65 20 6f 66 20 6c 6f 63   The type of loc
74910 6b 20 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 66  k held on this f
74920 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  d */.  unsigned 
74930 63 68 61 72 20 69 73 4f 70 65 6e 3b 20 20 20 20  char isOpen;    
74940 20 2f 2a 20 54 72 75 65 20 69 66 20 6e 65 65 64   /* True if need
74950 73 20 74 6f 20 62 65 20 63 6c 6f 73 65 64 20 2a  s to be closed *
74960 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  /.  unsigned cha
74970 72 20 66 75 6c 6c 53 79 6e 63 3b 20 20 20 2f 2a  r fullSync;   /*
74980 20 55 73 65 20 46 5f 46 55 4c 4c 53 59 4e 43 20   Use F_FULLSYNC 
74990 69 66 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f 0a  if available */.
749a0 20 20 69 6e 74 20 64 69 72 66 64 3b 20 20 20 20    int dirfd;    
749b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
749c0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 66  ile descriptor f
749d0 6f 72 20 74 68 65 20 64 69 72 65 63 74 6f 72 79  or the directory
749e0 20 2a 2f 0a 20 20 69 36 34 20 6f 66 66 73 65 74   */.  i64 offset
749f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
74a00 2f 2a 20 53 65 65 6b 20 6f 66 66 73 65 74 20 2a  /* Seek offset *
74a10 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
74a20 55 4e 49 58 5f 54 48 52 45 41 44 53 0a 20 20 70  UNIX_THREADS.  p
74a30 74 68 72 65 61 64 5f 74 20 74 69 64 3b 20 20 20  thread_t tid;   
74a40 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
74a50 74 68 72 65 61 64 20 74 68 61 74 20 22 6f 77 6e  thread that "own
74a60 73 22 20 74 68 69 73 20 4f 73 46 69 6c 65 20 2a  s" this OsFile *
74a70 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a  /.#endif.};../*.
74a80 2a 2a 20 50 72 6f 76 69 64 65 20 74 68 65 20 61  ** Provide the a
74a90 62 69 6c 69 74 79 20 74 6f 20 6f 76 65 72 72 69  bility to overri
74aa0 64 65 20 73 6f 6d 65 20 4f 53 2d 6c 61 79 65 72  de some OS-layer
74ab0 20 66 75 6e 63 74 69 6f 6e 73 20 64 75 72 69 6e   functions durin
74ac0 67 0a 2a 2a 20 74 65 73 74 69 6e 67 2e 20 20 54  g.** testing.  T
74ad0 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 73  his is used to s
74ae0 69 6d 75 6c 61 74 65 20 4f 53 20 63 72 61 73 68  imulate OS crash
74af0 65 73 20 74 6f 20 76 65 72 69 66 79 20 74 68 61  es to verify tha
74b00 74 20 0a 2a 2a 20 63 6f 6d 6d 69 74 73 20 61 72  t .** commits ar
74b10 65 20 61 74 6f 6d 69 63 20 65 76 65 6e 20 69 6e  e atomic even in
74b20 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 6e   the event of an
74b30 20 4f 53 20 63 72 61 73 68 2e 0a 2a 2f 0a 23 69   OS crash..*/.#i
74b40 66 64 65 66 20 53 51 4c 49 54 45 5f 43 52 41 53  fdef SQLITE_CRAS
74b50 48 5f 54 45 53 54 0a 20 20 65 78 74 65 72 6e 20  H_TEST.  extern 
74b60 69 6e 74 20 73 71 6c 69 74 65 33 43 72 61 73 68  int sqlite3Crash
74b70 54 65 73 74 45 6e 61 62 6c 65 3b 0a 20 20 65 78  TestEnable;.  ex
74b80 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33  tern int sqlite3
74b90 43 72 61 73 68 4f 70 65 6e 52 65 61 64 57 72 69  CrashOpenReadWri
74ba0 74 65 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  te(const char*, 
74bb0 4f 73 46 69 6c 65 2a 2a 2c 20 69 6e 74 2a 29 3b  OsFile**, int*);
74bc0 0a 20 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71  .  extern int sq
74bd0 6c 69 74 65 33 43 72 61 73 68 4f 70 65 6e 45 78  lite3CrashOpenEx
74be0 63 6c 75 73 69 76 65 28 63 6f 6e 73 74 20 63 68  clusive(const ch
74bf0 61 72 2a 2c 20 4f 73 46 69 6c 65 2a 2a 2c 20 69  ar*, OsFile**, i
74c00 6e 74 29 3b 0a 20 20 65 78 74 65 72 6e 20 69 6e  nt);.  extern in
74c10 74 20 73 71 6c 69 74 65 33 43 72 61 73 68 4f 70  t sqlite3CrashOp
74c20 65 6e 52 65 61 64 4f 6e 6c 79 28 63 6f 6e 73 74  enReadOnly(const
74c30 20 63 68 61 72 2a 2c 20 4f 73 46 69 6c 65 2a 2a   char*, OsFile**
74c40 2c 20 69 6e 74 29 3b 0a 23 20 64 65 66 69 6e 65  , int);.# define
74c50 20 43 52 41 53 48 5f 54 45 53 54 5f 4f 56 45 52   CRASH_TEST_OVER
74c60 52 49 44 45 28 58 2c 41 2c 42 2c 43 29 20 5c 0a  RIDE(X,A,B,C) \.
74c70 20 20 20 20 69 66 28 73 71 6c 69 74 65 33 43 72      if(sqlite3Cr
74c80 61 73 68 54 65 73 74 45 6e 61 62 6c 65 29 7b 20  ashTestEnable){ 
74c90 72 65 74 75 72 6e 20 58 28 41 2c 42 2c 43 29 3b  return X(A,B,C);
74ca0 20 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e   }.#else.# defin
74cb0 65 20 43 52 41 53 48 5f 54 45 53 54 5f 4f 56 45  e CRASH_TEST_OVE
74cc0 52 52 49 44 45 28 58 2c 41 2c 42 2c 43 29 20 20  RRIDE(X,A,B,C)  
74cd0 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f 0a 23 65 6e 64  /* no-op */.#end
74ce0 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75  if.../*.** Inclu
74cf0 64 65 20 63 6f 64 65 20 74 68 61 74 20 69 73 20  de code that is 
74d00 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73  common to all os
74d10 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a  _*.c files.*/./*
74d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
74d30 63 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e  clude os_common.
74d40 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
74d50 6f 66 20 6f 73 5f 75 6e 69 78 2e 63 20 2a 2a 2a  of os_unix.c ***
74d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
74d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
74d80 67 69 6e 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d  gin file os_comm
74d90 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  on.h ***********
74da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
74db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
74dc0 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a  .** 2004 May 22.
74dd0 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
74de0 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
74df0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
74e00 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
74e10 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
74e20 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
74e30 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
74e40 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
74e50 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
74e60 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
74e70 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
74e80 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
74e90 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
74ea0 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
74eb0 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
74ec0 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
74ed0 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
74ee0 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
74ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
74f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
74f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
74f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
74f30 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ***.**.** This f
74f40 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6d 61 63  ile contains mac
74f50 72 6f 73 20 61 6e 64 20 61 20 6c 69 74 74 6c 65  ros and a little
74f60 20 62 69 74 20 6f 66 20 63 6f 64 65 20 74 68 61   bit of code tha
74f70 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a  t is common to.*
74f80 2a 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 6c 61  * all of the pla
74f90 74 66 6f 72 6d 2d 73 70 65 63 69 66 69 63 20 66  tform-specific f
74fa0 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 29 20 61 6e  iles (os_*.c) an
74fb0 64 20 69 73 20 23 69 6e 63 6c 75 64 65 64 20 69  d is #included i
74fc0 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a 20 66 69 6c  nto those.** fil
74fd0 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  es..**.** This f
74fe0 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 23 69  ile should be #i
74ff0 6e 63 6c 75 64 65 64 20 62 79 20 74 68 65 20 6f  ncluded by the o
75000 73 5f 2a 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79  s_*.c files only
75010 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 61 0a 2a  .  It is not a.*
75020 2a 20 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73  * general purpos
75030 65 20 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a  e header file..*
75040 2f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73  /../*.** At leas
75050 74 20 74 77 6f 20 62 75 67 73 20 68 61 76 65 20  t two bugs have 
75060 73 6c 69 70 70 65 64 20 69 6e 20 62 65 63 61 75  slipped in becau
75070 73 65 20 77 65 20 63 68 61 6e 67 65 64 20 74 68  se we changed th
75080 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a  e MEMORY_DEBUG.*
75090 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51 4c 49 54  * macro to SQLIT
750a0 45 5f 44 45 42 55 47 20 61 6e 64 20 73 6f 6d 65  E_DEBUG and some
750b0 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69 6c 65 73   older makefiles
750c0 20 68 61 76 65 20 6e 6f 74 20 79 65 74 20 6d 61   have not yet ma
750d0 64 65 20 74 68 65 0a 2a 2a 20 73 77 69 74 63 68  de the.** switch
750e0 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  .  The following
750f0 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 63 61 74   code should cat
75100 63 68 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20  ch this problem 
75110 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e  at compile-time.
75120 0a 2a 2f 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52  .*/.#ifdef MEMOR
75130 59 5f 44 45 42 55 47 0a 23 20 65 72 72 6f 72 20  Y_DEBUG.# error 
75140 22 54 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55  "The MEMORY_DEBU
75150 47 20 6d 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c  G macro is obsol
75160 65 74 65 2e 20 20 55 73 65 20 53 51 4c 49 54 45  ete.  Use SQLITE
75170 5f 44 45 42 55 47 20 69 6e 73 74 65 61 64 2e 22  _DEBUG instead."
75180 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 20 2a 20  .#endif.../*. * 
75190 57 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 74 68  When testing, th
751a0 69 73 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62  is global variab
751b0 6c 65 20 73 74 6f 72 65 73 20 74 68 65 20 6c 6f  le stores the lo
751c0 63 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 20 2a  cation of the. *
751d0 20 70 65 6e 64 69 6e 67 2d 62 79 74 65 20 69 6e   pending-byte in
751e0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
751f0 6c 65 2e 0a 20 2a 2f 0a 23 69 66 64 65 66 20 53  le.. */.#ifdef S
75200 51 4c 49 54 45 5f 54 45 53 54 0a 75 6e 73 69 67  QLITE_TEST.unsig
75210 6e 65 64 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  ned int sqlite3_
75220 70 65 6e 64 69 6e 67 5f 62 79 74 65 20 3d 20 30  pending_byte = 0
75230 78 34 30 30 30 30 30 30 30 3b 0a 23 65 6e 64 69  x40000000;.#endi
75240 66 0a 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  f..int sqlite3_o
75250 73 5f 74 72 61 63 65 20 3d 20 30 3b 0a 23 69 66  s_trace = 0;.#if
75260 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
75270 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
75280 31 28 58 29 20 20 20 20 20 20 20 20 20 69 66 28  1(X)         if(
75290 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 72 61 63   sqlite3_os_trac
752a0 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67  e ) sqlite3Debug
752b0 50 72 69 6e 74 66 28 58 29 0a 23 64 65 66 69 6e  Printf(X).#defin
752c0 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 20  e OSTRACE2(X,Y) 
752d0 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
752e0 33 5f 6f 73 5f 74 72 61 63 65 20 29 20 73 71 6c  3_os_trace ) sql
752f0 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
75300 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54  X,Y).#define OST
75310 52 41 43 45 33 28 58 2c 59 2c 5a 29 20 20 20 20  RACE3(X,Y,Z)    
75320 20 69 66 28 20 73 71 6c 69 74 65 33 5f 6f 73 5f   if( sqlite3_os_
75330 74 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44  trace ) sqlite3D
75340 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a  ebugPrintf(X,Y,Z
75350 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
75360 45 34 28 58 2c 59 2c 5a 2c 41 29 20 20 20 69 66  E4(X,Y,Z,A)   if
75370 28 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 72 61  ( sqlite3_os_tra
75380 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75  ce ) sqlite3Debu
75390 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 29  gPrintf(X,Y,Z,A)
753a0 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
753b0 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 20 69 66 28  5(X,Y,Z,A,B) if(
753c0 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 72 61 63   sqlite3_os_trac
753d0 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67  e ) sqlite3Debug
753e0 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42  Printf(X,Y,Z,A,B
753f0 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
75400 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20  E6(X,Y,Z,A,B,C) 
75410 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 65 33  \.    if(sqlite3
75420 5f 6f 73 5f 74 72 61 63 65 29 20 73 71 6c 69 74  _os_trace) sqlit
75430 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c  e3DebugPrintf(X,
75440 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69  Y,Z,A,B,C).#defi
75450 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c  ne OSTRACE7(X,Y,
75460 5a 2c 41 2c 42 2c 43 2c 44 29 20 5c 0a 20 20 20  Z,A,B,C,D) \.   
75470 20 69 66 28 73 71 6c 69 74 65 33 5f 6f 73 5f 74   if(sqlite3_os_t
75480 72 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 62  race) sqlite3Deb
75490 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41  ugPrintf(X,Y,Z,A
754a0 2c 42 2c 43 2c 44 29 0a 23 65 6c 73 65 0a 23 64  ,B,C,D).#else.#d
754b0 65 66 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58  efine OSTRACE1(X
754c0 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
754d0 45 32 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20  E2(X,Y).#define 
754e0 4f 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 0a  OSTRACE3(X,Y,Z).
754f0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34  #define OSTRACE4
75500 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e  (X,Y,Z,A).#defin
75510 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a  e OSTRACE5(X,Y,Z
75520 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53  ,A,B).#define OS
75530 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42  TRACE6(X,Y,Z,A,B
75540 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,C).#define OSTR
75550 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43  ACE7(X,Y,Z,A,B,C
75560 2c 44 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ,D).#endif../*.*
75570 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 70 65 72  * Macros for per
75580 66 6f 72 6d 61 6e 63 65 20 74 72 61 63 69 6e 67  formance tracing
75590 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e  .  Normally turn
755a0 65 64 20 6f 66 66 2e 20 20 4f 6e 6c 79 20 77 6f  ed off.  Only wo
755b0 72 6b 73 0a 2a 2a 20 6f 6e 20 69 34 38 36 20 68  rks.** on i486 h
755c0 61 72 64 77 61 72 65 2e 0a 2a 2f 0a 23 69 66 64  ardware..*/.#ifd
755d0 65 66 20 53 51 4c 49 54 45 5f 50 45 52 46 4f 52  ef SQLITE_PERFOR
755e0 4d 41 4e 43 45 5f 54 52 41 43 45 0a 5f 5f 69 6e  MANCE_TRACE.__in
755f0 6c 69 6e 65 5f 5f 20 75 6e 73 69 67 6e 65 64 20  line__ unsigned 
75600 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 20 68 77  long long int hw
75610 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 75 6e  time(void){.  un
75620 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67  signed long long
75630 20 69 6e 74 20 78 3b 0a 20 20 5f 5f 61 73 6d 5f   int x;.  __asm_
75640 5f 28 22 72 64 74 73 63 5c 6e 5c 74 22 0a 20 20  _("rdtsc\n\t".  
75650 20 20 20 20 20 20 20 20 22 6d 6f 76 20 25 25 65          "mov %%e
75660 64 78 2c 20 25 25 65 63 78 5c 6e 5c 74 22 0a 20  dx, %%ecx\n\t". 
75670 20 20 20 20 20 20 20 20 20 3a 22 3d 41 22 20 28           :"=A" (
75680 78 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 78 3b  x));.  return x;
75690 0a 7d 0a 73 74 61 74 69 63 20 75 6e 73 69 67 6e  .}.static unsign
756a0 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74  ed long long int
756b0 20 67 5f 73 74 61 72 74 3b 0a 73 74 61 74 69 63   g_start;.static
756c0 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 65 6c   unsigned int el
756d0 61 70 73 65 3b 0a 23 64 65 66 69 6e 65 20 54 49  apse;.#define TI
756e0 4d 45 52 5f 53 54 41 52 54 20 20 20 20 20 20 20  MER_START       
756f0 67 5f 73 74 61 72 74 3d 68 77 74 69 6d 65 28 29  g_start=hwtime()
75700 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45  .#define TIMER_E
75710 4e 44 20 20 20 20 20 20 20 20 20 65 6c 61 70 73  ND         elaps
75720 65 3d 68 77 74 69 6d 65 28 29 2d 67 5f 73 74 61  e=hwtime()-g_sta
75730 72 74 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52  rt.#define TIMER
75740 5f 45 4c 41 50 53 45 44 20 20 20 20 20 65 6c 61  _ELAPSED     ela
75750 70 73 65 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e  pse.#else.#defin
75760 65 20 54 49 4d 45 52 5f 53 54 41 52 54 0a 23 64  e TIMER_START.#d
75770 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 0a  efine TIMER_END.
75780 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c  #define TIMER_EL
75790 41 50 53 45 44 20 20 20 20 20 30 0a 23 65 6e 64  APSED     0.#end
757a0 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20  if../*.** If we 
757b0 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 74 68 65  compile with the
757c0 20 53 51 4c 49 54 45 5f 54 45 53 54 20 6d 61 63   SQLITE_TEST mac
757d0 72 6f 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65  ro set, then the
757e0 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b   following block
757f0 0a 2a 2a 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c  .** of code will
75800 20 67 69 76 65 20 75 73 20 74 68 65 20 61 62 69   give us the abi
75810 6c 69 74 79 20 74 6f 20 73 69 6d 75 6c 61 74 65  lity to simulate
75820 20 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f   a disk I/O erro
75830 72 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73 20 75  r.  This.** is u
75840 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20  sed for testing 
75850 74 68 65 20 49 2f 4f 20 72 65 63 6f 76 65 72 79  the I/O recovery
75860 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69 66 64 65   logic..*/.#ifde
75870 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 69 6e  f SQLITE_TEST.in
75880 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  t sqlite3_io_err
75890 6f 72 5f 68 69 74 20 3d 20 30 3b 0a 69 6e 74 20  or_hit = 0;.int 
758a0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
758b0 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 69 6e  _pending = 0;.in
758c0 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  t sqlite3_io_err
758d0 6f 72 5f 70 65 72 73 69 73 74 20 3d 20 30 3b 0a  or_persist = 0;.
758e0 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b  int sqlite3_disk
758f0 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 20 30  full_pending = 0
75900 3b 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69  ;.int sqlite3_di
75910 73 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23 64 65 66  skfull = 0;.#def
75920 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ine SimulateIOEr
75930 72 6f 72 28 43 4f 44 45 29 20 20 5c 0a 20 20 69  ror(CODE)  \.  i
75940 66 28 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  f( sqlite3_io_er
75950 72 6f 72 5f 70 65 6e 64 69 6e 67 20 7c 7c 20 73  ror_pending || s
75960 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
75970 68 69 74 20 29 20 5c 0a 20 20 20 20 20 69 66 28  hit ) \.     if(
75980 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
75990 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31  r_pending-- == 1
759a0 20 5c 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 28   \.         || (
759b0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
759c0 5f 70 65 72 73 69 73 74 20 26 26 20 73 71 6c 69  _persist && sqli
759d0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74  te3_io_error_hit
759e0 29 20 29 20 5c 0a 20 20 20 20 20 20 20 20 20 20  ) ) \.          
759f0 20 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f        { local_io
75a00 65 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0a 73  err(); CODE; }.s
75a10 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 63 61 6c  tatic void local
75a20 5f 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f 54 52  _ioerr(){.  IOTR
75a30 41 43 45 28 28 22 49 4f 45 52 52 5c 6e 22 29 29  ACE(("IOERR\n"))
75a40 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  ;.  sqlite3_io_e
75a50 72 72 6f 72 5f 68 69 74 20 3d 20 31 3b 0a 7d 0a  rror_hit = 1;.}.
75a60 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65  #define Simulate
75a70 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 43 4f  DiskfullError(CO
75a80 44 45 29 20 5c 0a 20 20 20 69 66 28 20 73 71 6c  DE) \.   if( sql
75a90 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65  ite3_diskfull_pe
75aa0 6e 64 69 6e 67 20 29 7b 20 5c 0a 20 20 20 20 20  nding ){ \.     
75ab0 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b  if( sqlite3_disk
75ac0 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d 20  full_pending == 
75ad0 31 20 29 7b 20 5c 0a 20 20 20 20 20 20 20 6c 6f  1 ){ \.       lo
75ae0 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 5c 0a 20  cal_ioerr(); \. 
75af0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69        sqlite3_di
75b00 73 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20  skfull = 1; \.  
75b10 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f       sqlite3_io_
75b20 65 72 72 6f 72 5f 68 69 74 20 3d 20 31 3b 20 5c  error_hit = 1; \
75b30 0a 20 20 20 20 20 20 20 43 4f 44 45 3b 20 5c 0a  .       CODE; \.
75b40 20 20 20 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 20       }else{ \.  
75b50 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73       sqlite3_dis
75b60 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b  kfull_pending--;
75b70 20 5c 0a 20 20 20 20 20 7d 20 5c 0a 20 20 20 7d   \.     } \.   }
75b80 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 53  .#else.#define S
75b90 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 41  imulateIOError(A
75ba0 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61  ).#define Simula
75bb0 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28  teDiskfullError(
75bc0 41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  A).#endif../*.**
75bd0 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 6b   When testing, k
75be0 65 65 70 20 61 20 63 6f 75 6e 74 20 6f 66 20 74  eep a count of t
75bf0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 65  he number of ope
75c00 6e 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64  n files..*/.#ifd
75c10 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 69  ef SQLITE_TEST.i
75c20 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  nt sqlite3_open_
75c30 66 69 6c 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a  file_count = 0;.
75c40 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e  #define OpenCoun
75c50 74 65 72 28 58 29 20 20 73 71 6c 69 74 65 33 5f  ter(X)  sqlite3_
75c60 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 2b  open_file_count+
75c70 3d 28 58 29 0a 23 65 6c 73 65 0a 23 64 65 66 69  =(X).#else.#defi
75c80 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58  ne OpenCounter(X
75c90 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ).#endif../*.** 
75ca0 73 71 6c 69 74 65 33 47 65 6e 65 72 69 63 4d 61  sqlite3GenericMa
75cb0 6c 6c 6f 63 0a 2a 2a 20 73 71 6c 69 74 65 33 47  lloc.** sqlite3G
75cc0 65 6e 65 72 69 63 52 65 61 6c 6c 6f 63 0a 2a 2a  enericRealloc.**
75cd0 20 73 71 6c 69 74 65 33 47 65 6e 65 72 69 63 4f   sqlite3GenericO
75ce0 73 46 72 65 65 0a 2a 2a 20 73 71 6c 69 74 65 33  sFree.** sqlite3
75cf0 47 65 6e 65 72 69 63 41 6c 6c 6f 63 61 74 69 6f  GenericAllocatio
75d00 6e 53 69 7a 65 0a 2a 2a 0a 2a 2a 20 49 6d 70 6c  nSize.**.** Impl
75d10 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
75d20 65 20 6f 73 20 6c 65 76 65 6c 20 64 79 6e 61 6d  e os level dynam
75d30 69 63 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ic memory alloca
75d40 74 69 6f 6e 20 69 6e 74 65 72 66 61 63 65 20 69  tion interface i
75d50 6e 20 74 65 72 6d 73 0a 2a 2a 20 6f 66 20 74 68  n terms.** of th
75d60 65 20 73 74 61 6e 64 61 72 64 20 6d 61 6c 6c 6f  e standard mallo
75d70 63 28 29 2c 20 72 65 61 6c 6c 6f 63 28 29 20 61  c(), realloc() a
75d80 6e 64 20 66 72 65 65 28 29 20 66 6f 75 6e 64 20  nd free() found 
75d90 69 6e 20 6d 61 6e 79 20 6f 70 65 72 61 74 69 6e  in many operatin
75da0 67 0a 2a 2a 20 73 79 73 74 65 6d 73 2e 20 4e 6f  g.** systems. No
75db0 20 72 6f 63 6b 65 74 20 73 63 69 65 6e 63 65 20   rocket science 
75dc0 68 65 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72  here..**.** Ther
75dd0 65 20 61 72 65 20 74 77 6f 20 76 65 72 73 69 6f  e are two versio
75de0 6e 73 20 6f 66 20 74 68 65 73 65 20 66 6f 75 72  ns of these four
75df0 20 66 75 6e 63 74 69 6f 6e 73 20 68 65 72 65 2e   functions here.
75e00 20 54 68 65 20 76 65 72 73 69 6f 6e 0a 2a 2a 20   The version.** 
75e10 69 6d 70 6c 65 6d 65 6e 74 65 64 20 68 65 72 65  implemented here
75e20 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66   is only used if
75e30 20 6d 65 6d 6f 72 79 2d 6d 61 6e 61 67 65 6d 65   memory-manageme
75e40 6e 74 20 6f 72 20 6d 65 6d 6f 72 79 2d 64 65 62  nt or memory-deb
75e50 75 67 67 69 6e 67 20 69 73 0a 2a 2a 20 65 6e 61  ugging is.** ena
75e60 62 6c 65 64 2e 20 54 68 69 73 20 76 65 72 73 69  bled. This versi
75e70 6f 6e 20 61 6c 6c 6f 63 61 74 65 73 20 61 6e 20  on allocates an 
75e80 65 78 74 72 61 20 38 2d 62 79 74 65 73 20 61 74  extra 8-bytes at
75e90 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f   the beginning o
75ea0 66 20 65 61 63 68 0a 2a 2a 20 62 6c 6f 63 6b 20  f each.** block 
75eb0 61 6e 64 20 73 74 6f 72 65 73 20 74 68 65 20 73  and stores the s
75ec0 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63  ize of the alloc
75ed0 61 74 69 6f 6e 20 74 68 65 72 65 2e 0a 2a 2a 0a  ation there..**.
75ee0 2a 2a 20 49 66 20 6e 65 69 74 68 65 72 20 6d 65  ** If neither me
75ef0 6d 6f 72 79 2d 6d 61 6e 61 67 65 6d 65 6e 74 20  mory-management 
75f00 6f 72 20 64 65 62 75 67 67 69 6e 67 20 69 73 20  or debugging is 
75f10 65 6e 61 62 6c 65 64 2c 20 74 68 65 20 73 65 63  enabled, the sec
75f20 6f 6e 64 0a 2a 2a 20 73 65 74 20 6f 66 20 69 6d  ond.** set of im
75f30 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 69 73  plementations is
75f40 20 75 73 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a   used instead..*
75f50 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  /.#if defined(SQ
75f60 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f  LITE_ENABLE_MEMO
75f70 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 29 20 7c  RY_MANAGEMENT) |
75f80 7c 20 64 65 66 69 6e 65 64 20 28 53 51 4c 49 54  | defined (SQLIT
75f90 45 5f 4d 45 4d 44 45 42 55 47 29 0a 53 51 4c 49  E_MEMDEBUG).SQLI
75fa0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
75fb0 2a 73 71 6c 69 74 65 33 47 65 6e 65 72 69 63 4d  *sqlite3GenericM
75fc0 61 6c 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 20  alloc(int n){.  
75fd0 63 68 61 72 20 2a 70 20 3d 20 28 63 68 61 72 20  char *p = (char 
75fe0 2a 29 6d 61 6c 6c 6f 63 28 6e 2b 38 29 3b 0a 20  *)malloc(n+8);. 
75ff0 20 61 73 73 65 72 74 28 6e 3e 30 29 3b 0a 20 20   assert(n>0);.  
76000 61 73 73 65 72 74 28 73 69 7a 65 6f 66 28 69 6e  assert(sizeof(in
76010 74 29 3c 3d 38 29 3b 0a 20 20 69 66 28 20 70 20  t)<=8);.  if( p 
76020 29 7b 0a 20 20 20 20 2a 28 69 6e 74 20 2a 29 70  ){.    *(int *)p
76030 20 3d 20 6e 3b 0a 20 20 20 20 70 20 2b 3d 20 38   = n;.    p += 8
76040 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28  ;.  }.  return (
76050 76 6f 69 64 20 2a 29 70 3b 0a 7d 0a 53 51 4c 49  void *)p;.}.SQLI
76060 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
76070 2a 73 71 6c 69 74 65 33 47 65 6e 65 72 69 63 52  *sqlite3GenericR
76080 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 2c 20  ealloc(void *p, 
76090 69 6e 74 20 6e 29 7b 0a 20 20 63 68 61 72 20 2a  int n){.  char *
760a0 70 32 20 3d 20 28 28 63 68 61 72 20 2a 29 70 20  p2 = ((char *)p 
760b0 2d 20 38 29 3b 0a 20 20 61 73 73 65 72 74 28 6e  - 8);.  assert(n
760c0 3e 30 29 3b 0a 20 20 70 32 20 3d 20 28 63 68 61  >0);.  p2 = (cha
760d0 72 2a 29 72 65 61 6c 6c 6f 63 28 70 32 2c 20 6e  r*)realloc(p2, n
760e0 2b 38 29 3b 0a 20 20 69 66 28 20 70 32 20 29 7b  +8);.  if( p2 ){
760f0 0a 20 20 20 20 2a 28 69 6e 74 20 2a 29 70 32 20  .    *(int *)p2 
76100 3d 20 6e 3b 0a 20 20 20 20 70 32 20 2b 3d 20 38  = n;.    p2 += 8
76110 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28  ;.  }.  return (
76120 76 6f 69 64 20 2a 29 70 32 3b 0a 7d 0a 53 51 4c  void *)p2;.}.SQL
76130 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
76140 20 73 71 6c 69 74 65 33 47 65 6e 65 72 69 63 46   sqlite3GenericF
76150 72 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20  ree(void *p){.  
76160 61 73 73 65 72 74 28 70 29 3b 0a 20 20 66 72 65  assert(p);.  fre
76170 65 28 28 76 6f 69 64 20 2a 29 28 28 63 68 61 72  e((void *)((char
76180 20 2a 29 70 20 2d 20 38 29 29 3b 0a 7d 0a 53 51   *)p - 8));.}.SQ
76190 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
761a0 20 73 71 6c 69 74 65 33 47 65 6e 65 72 69 63 41   sqlite3GenericA
761b0 6c 6c 6f 63 61 74 69 6f 6e 53 69 7a 65 28 76 6f  llocationSize(vo
761c0 69 64 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e  id *p){.  return
761d0 20 70 20 3f 20 2a 28 69 6e 74 20 2a 29 28 28 63   p ? *(int *)((c
761e0 68 61 72 20 2a 29 70 20 2d 20 38 29 20 3a 20 30  har *)p - 8) : 0
761f0 3b 0a 7d 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45  ;.}.#else.SQLITE
76200 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
76210 71 6c 69 74 65 33 47 65 6e 65 72 69 63 4d 61 6c  qlite3GenericMal
76220 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 20 63 68  loc(int n){.  ch
76230 61 72 20 2a 70 20 3d 20 28 63 68 61 72 20 2a 29  ar *p = (char *)
76240 6d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 72 65 74  malloc(n);.  ret
76250 75 72 6e 20 28 76 6f 69 64 20 2a 29 70 3b 0a 7d  urn (void *)p;.}
76260 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
76270 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 47 65 6e  void *sqlite3Gen
76280 65 72 69 63 52 65 61 6c 6c 6f 63 28 76 6f 69 64  ericRealloc(void
76290 20 2a 70 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 61   *p, int n){.  a
762a0 73 73 65 72 74 28 6e 3e 30 29 3b 0a 20 20 70 20  ssert(n>0);.  p 
762b0 3d 20 72 65 61 6c 6c 6f 63 28 70 2c 20 6e 29 3b  = realloc(p, n);
762c0 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 53  .  return p;.}.S
762d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
762e0 69 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72 69  id sqlite3Generi
762f0 63 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a  cFree(void *p){.
76300 20 20 61 73 73 65 72 74 28 70 29 3b 0a 20 20 66    assert(p);.  f
76310 72 65 65 28 70 29 3b 0a 7d 0a 2f 2a 20 4e 65 76  ree(p);.}./* Nev
76320 65 72 20 61 63 74 75 61 6c 6c 79 20 75 73 65 64  er actually used
76330 2c 20 62 75 74 20 6e 65 65 64 65 64 20 66 6f 72  , but needed for
76340 20 74 68 65 20 6c 69 6e 6b 65 72 20 2a 2f 0a 53   the linker */.S
76350 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
76360 74 20 73 71 6c 69 74 65 33 47 65 6e 65 72 69 63  t sqlite3Generic
76370 41 6c 6c 6f 63 61 74 69 6f 6e 53 69 7a 65 28 76  AllocationSize(v
76380 6f 69 64 20 2a 70 29 7b 20 72 65 74 75 72 6e 20  oid *p){ return 
76390 30 3b 20 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  0; }.#endif../*.
763a0 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 73  ** The default s
763b0 69 7a 65 20 6f 66 20 61 20 64 69 73 6b 20 73 65  ize of a disk se
763c0 63 74 6f 72 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ctor.*/.#ifndef 
763d0 50 41 47 45 52 5f 53 45 43 54 4f 52 5f 53 49 5a  PAGER_SECTOR_SIZ
763e0 45 0a 23 20 64 65 66 69 6e 65 20 50 41 47 45 52  E.# define PAGER
763f0 5f 53 45 43 54 4f 52 5f 53 49 5a 45 20 35 31 32  _SECTOR_SIZE 512
76400 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a  .#endif../******
76410 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
76420 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a  os_common.h ****
76430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
76440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
76450 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
76460 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
76470 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
76480 74 20 6f 66 66 20 69 6e 20 6f 73 5f 75 6e 69 78  t off in os_unix
76490 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
764a0 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  *******/../*.** 
764b0 44 6f 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 61  Do not include a
764c0 6e 79 20 6f 66 20 74 68 65 20 46 69 6c 65 20 49  ny of the File I
764d0 2f 4f 20 69 6e 74 65 72 66 61 63 65 20 70 72 6f  /O interface pro
764e0 63 65 64 75 72 65 73 20 69 66 20 74 68 65 0a 2a  cedures if the.*
764f0 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49  * SQLITE_OMIT_DI
76500 53 4b 49 4f 20 6d 61 63 72 6f 20 69 73 20 64 65  SKIO macro is de
76510 66 69 6e 65 64 20 28 69 6e 64 69 63 61 74 69 6e  fined (indicatin
76520 67 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62  g that the datab
76530 61 73 65 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 69  ase.** will be i
76540 6e 2d 6d 65 6d 6f 72 79 20 6f 6e 6c 79 29 0a 2a  n-memory only).*
76550 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
76560 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a 0a 0a 2f  _OMIT_DISKIO.../
76570 2a 0a 2a 2a 20 44 65 66 69 6e 65 20 76 61 72 69  *.** Define vari
76580 6f 75 73 20 6d 61 63 72 6f 73 20 74 68 61 74 20  ous macros that 
76590 61 72 65 20 6d 69 73 73 69 6e 67 20 66 72 6f 6d  are missing from
765a0 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2e 0a 2a   some systems..*
765b0 2f 0a 23 69 66 6e 64 65 66 20 4f 5f 4c 41 52 47  /.#ifndef O_LARG
765c0 45 46 49 4c 45 0a 23 20 64 65 66 69 6e 65 20 4f  EFILE.# define O
765d0 5f 4c 41 52 47 45 46 49 4c 45 20 30 0a 23 65 6e  _LARGEFILE 0.#en
765e0 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54  dif.#ifdef SQLIT
765f0 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 0a 23 20  E_DISABLE_LFS.# 
76600 75 6e 64 65 66 20 4f 5f 4c 41 52 47 45 46 49 4c  undef O_LARGEFIL
76610 45 0a 23 20 64 65 66 69 6e 65 20 4f 5f 4c 41 52  E.# define O_LAR
76620 47 45 46 49 4c 45 20 30 0a 23 65 6e 64 69 66 0a  GEFILE 0.#endif.
76630 23 69 66 6e 64 65 66 20 4f 5f 4e 4f 46 4f 4c 4c  #ifndef O_NOFOLL
76640 4f 57 0a 23 20 64 65 66 69 6e 65 20 4f 5f 4e 4f  OW.# define O_NO
76650 46 4f 4c 4c 4f 57 20 30 0a 23 65 6e 64 69 66 0a  FOLLOW 0.#endif.
76660 23 69 66 6e 64 65 66 20 4f 5f 42 49 4e 41 52 59  #ifndef O_BINARY
76670 0a 23 20 64 65 66 69 6e 65 20 4f 5f 42 49 4e 41  .# define O_BINA
76680 52 59 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  RY 0.#endif../*.
76690 2a 2a 20 54 68 65 20 44 4a 47 50 50 20 63 6f 6d  ** The DJGPP com
766a0 70 69 6c 65 72 20 65 6e 76 69 72 6f 6e 6d 65 6e  piler environmen
766b0 74 20 6c 6f 6f 6b 73 20 6d 6f 73 74 6c 79 20 6c  t looks mostly l
766c0 69 6b 65 20 55 6e 69 78 2c 20 62 75 74 20 69 74  ike Unix, but it
766d0 0a 2a 2a 20 6c 61 63 6b 73 20 74 68 65 20 66 63  .** lacks the fc
766e0 6e 74 6c 28 29 20 73 79 73 74 65 6d 20 63 61 6c  ntl() system cal
766f0 6c 2e 20 20 53 6f 20 72 65 64 65 66 69 6e 65 20  l.  So redefine 
76700 66 63 6e 74 6c 28 29 20 74 6f 20 62 65 20 73 6f  fcntl() to be so
76710 6d 65 74 68 69 6e 67 0a 2a 2a 20 74 68 61 74 20  mething.** that 
76720 61 6c 77 61 79 73 20 73 75 63 63 65 65 64 73 2e  always succeeds.
76730 20 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61    This means tha
76740 74 20 6c 6f 63 6b 69 6e 67 20 64 6f 65 73 20 6e  t locking does n
76750 6f 74 20 6f 63 63 75 72 20 75 6e 64 65 72 0a 2a  ot occur under.*
76760 2a 20 44 4a 47 50 50 2e 20 20 42 75 74 20 69 74  * DJGPP.  But it
76770 27 73 20 44 4f 53 20 2d 20 77 68 61 74 20 64 69  's DOS - what di
76780 64 20 79 6f 75 20 65 78 70 65 63 74 3f 0a 2a 2f  d you expect?.*/
76790 0a 23 69 66 64 65 66 20 5f 5f 44 4a 47 50 50 5f  .#ifdef __DJGPP_
767a0 5f 0a 23 20 64 65 66 69 6e 65 20 66 63 6e 74 6c  _.# define fcntl
767b0 28 41 2c 42 2c 43 29 20 30 0a 23 65 6e 64 69 66  (A,B,C) 0.#endif
767c0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 68 72 65  ../*.** The thre
767d0 61 64 69 64 20 6d 61 63 72 6f 20 72 65 73 6f 6c  adid macro resol
767e0 76 65 73 20 74 6f 20 74 68 65 20 74 68 72 65 61  ves to the threa
767f0 64 2d 69 64 20 6f 72 20 74 6f 20 30 2e 20 20 55  d-id or to 0.  U
76800 73 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74 69  sed for.** testi
76810 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67  ng and debugging
76820 20 6f 6e 6c 79 2e 0a 2a 2f 0a 23 69 66 64 65 66   only..*/.#ifdef
76830 20 53 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48 52   SQLITE_UNIX_THR
76840 45 41 44 53 0a 23 64 65 66 69 6e 65 20 74 68 72  EADS.#define thr
76850 65 61 64 69 64 20 70 74 68 72 65 61 64 5f 73 65  eadid pthread_se
76860 6c 66 28 29 0a 23 65 6c 73 65 0a 23 64 65 66 69  lf().#else.#defi
76870 6e 65 20 74 68 72 65 61 64 69 64 20 30 0a 23 65  ne threadid 0.#e
76880 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20  ndif../*.** Set 
76890 6f 72 20 63 68 65 63 6b 20 74 68 65 20 4f 73 46  or check the OsF
768a0 69 6c 65 2e 74 69 64 20 66 69 65 6c 64 2e 20 20  ile.tid field.  
768b0 54 68 69 73 20 66 69 65 6c 64 20 69 73 20 73 65  This field is se
768c0 74 20 77 68 65 6e 20 61 6e 20 4f 73 46 69 6c 65  t when an OsFile
768d0 0a 2a 2a 20 69 73 20 66 69 72 73 74 20 6f 70 65  .** is first ope
768e0 6e 65 64 2e 20 20 41 6c 6c 20 73 75 62 73 65 71  ned.  All subseq
768f0 75 65 6e 74 20 75 73 65 73 20 6f 66 20 74 68 65  uent uses of the
76900 20 4f 73 46 69 6c 65 20 76 65 72 69 66 79 20 74   OsFile verify t
76910 68 61 74 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20  hat the.** same 
76920 74 68 72 65 61 64 20 69 73 20 6f 70 65 72 61 74  thread is operat
76930 69 6e 67 20 6f 6e 20 74 68 65 20 4f 73 46 69 6c  ing on the OsFil
76940 65 2e 20 20 53 6f 6d 65 20 6f 70 65 72 61 74 69  e.  Some operati
76950 6e 67 20 73 79 73 74 65 6d 73 20 64 6f 0a 2a 2a  ng systems do.**
76960 20 6e 6f 74 20 61 6c 6c 6f 77 20 6c 6f 63 6b 73   not allow locks
76970 20 74 6f 20 62 65 20 6f 76 65 72 72 69 64 64 65   to be overridde
76980 6e 20 62 79 20 6f 74 68 65 72 20 74 68 72 65 61  n by other threa
76990 64 73 20 61 6e 64 20 74 68 61 74 20 72 65 73 74  ds and that rest
769a0 72 69 63 74 69 6f 6e 0a 2a 2a 20 6d 65 61 6e 73  riction.** means
769b0 20 74 68 61 74 20 73 71 6c 69 74 65 33 2a 20 64   that sqlite3* d
769c0 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 73 20  atabase handles 
769d0 63 61 6e 6e 6f 74 20 62 65 20 6d 6f 76 65 64 20  cannot be moved 
769e0 66 72 6f 6d 20 6f 6e 65 20 74 68 72 65 61 64 0a  from one thread.
769f0 2a 2a 20 74 6f 20 61 6e 6f 74 68 65 72 2e 20 20  ** to another.  
76a00 54 68 69 73 20 6c 6f 67 69 63 20 6d 61 6b 65 73  This logic makes
76a10 20 73 75 72 65 20 61 20 75 73 65 72 20 64 6f 65   sure a user doe
76a20 73 20 6e 6f 74 20 74 72 79 20 74 6f 20 64 6f 20  s not try to do 
76a30 74 68 61 74 0a 2a 2a 20 62 79 20 6d 69 73 74 61  that.** by mista
76a40 6b 65 2e 0a 2a 2a 0a 2a 2a 20 56 65 72 73 69 6f  ke..**.** Versio
76a50 6e 20 33 2e 33 2e 31 20 28 32 30 30 36 2d 30 31  n 3.3.1 (2006-01
76a60 2d 31 35 29 3a 20 20 4f 73 46 69 6c 65 73 20 63  -15):  OsFiles c
76a70 61 6e 20 62 65 20 6d 6f 76 65 64 20 66 72 6f 6d  an be moved from
76a80 20 6f 6e 65 20 74 68 72 65 61 64 20 74 6f 0a 2a   one thread to.*
76a90 2a 20 61 6e 6f 74 68 65 72 20 61 73 20 6c 6f 6e  * another as lon
76aa0 67 20 61 73 20 77 65 20 61 72 65 20 72 75 6e 6e  g as we are runn
76ab0 69 6e 67 20 6f 6e 20 61 20 73 79 73 74 65 6d 20  ing on a system 
76ac0 74 68 61 74 20 73 75 70 70 6f 72 74 73 20 74 68  that supports th
76ad0 72 65 61 64 73 0a 2a 2a 20 6f 76 65 72 72 69 64  reads.** overrid
76ae0 69 6e 67 20 65 61 63 68 20 6f 74 68 65 72 73 20  ing each others 
76af0 6c 6f 63 6b 73 20 28 77 68 69 63 68 20 6e 6f 77  locks (which now
76b00 20 74 68 65 20 6d 6f 73 74 20 63 6f 6d 6d 6f 6e   the most common
76b10 20 62 65 68 61 76 69 6f 72 29 0a 2a 2a 20 6f 72   behavior).** or
76b20 20 69 66 20 6e 6f 20 6c 6f 63 6b 73 20 61 72 65   if no locks are
76b30 20 68 65 6c 64 2e 20 20 42 75 74 20 74 68 65 20   held.  But the 
76b40 4f 73 46 69 6c 65 2e 70 4c 6f 63 6b 20 66 69 65  OsFile.pLock fie
76b50 6c 64 20 6e 65 65 64 73 20 74 6f 20 62 65 0a 2a  ld needs to be.*
76b60 2a 20 72 65 63 6f 6d 70 75 74 65 64 20 62 65 63  * recomputed bec
76b70 61 75 73 65 20 69 74 73 20 6b 65 79 20 69 6e 63  ause its key inc
76b80 6c 75 64 65 73 20 74 68 65 20 74 68 72 65 61 64  ludes the thread
76b90 2d 69 64 2e 20 20 53 65 65 20 74 68 65 20 0a 2a  -id.  See the .*
76ba0 2a 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73  * transferOwners
76bb0 68 69 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 62  hip() function b
76bc0 65 6c 6f 77 20 66 6f 72 20 61 64 64 69 74 69 6f  elow for additio
76bd0 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a  nal information.
76be0 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  */.#if defined(S
76bf0 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48 52 45 41  QLITE_UNIX_THREA
76c00 44 53 29 0a 23 20 64 65 66 69 6e 65 20 53 45 54  DS).# define SET
76c10 5f 54 48 52 45 41 44 49 44 28 58 29 20 20 20 28  _THREADID(X)   (
76c20 58 29 2d 3e 74 69 64 20 3d 20 70 74 68 72 65 61  X)->tid = pthrea
76c30 64 5f 73 65 6c 66 28 29 0a 23 20 64 65 66 69 6e  d_self().# defin
76c40 65 20 43 48 45 43 4b 5f 54 48 52 45 41 44 49 44  e CHECK_THREADID
76c50 28 58 29 20 28 74 68 72 65 61 64 73 4f 76 65 72  (X) (threadsOver
76c60 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f  rideEachOthersLo
76c70 63 6b 73 3d 3d 30 20 26 26 20 5c 0a 20 20 20 20  cks==0 && \.    
76c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
76c90 20 20 20 20 20 20 20 20 21 70 74 68 72 65 61 64          !pthread
76ca0 5f 65 71 75 61 6c 28 28 58 29 2d 3e 74 69 64 2c  _equal((X)->tid,
76cb0 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 29   pthread_self())
76cc0 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  ).#else.# define
76cd0 20 53 45 54 5f 54 48 52 45 41 44 49 44 28 58 29   SET_THREADID(X)
76ce0 0a 23 20 64 65 66 69 6e 65 20 43 48 45 43 4b 5f  .# define CHECK_
76cf0 54 48 52 45 41 44 49 44 28 58 29 20 30 0a 23 65  THREADID(X) 0.#e
76d00 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 48 65 72 65  ndif../*.** Here
76d10 20 69 73 20 74 68 65 20 64 69 72 74 20 6f 6e 20   is the dirt on 
76d20 50 4f 53 49 58 20 61 64 76 69 73 6f 72 79 20 6c  POSIX advisory l
76d30 6f 63 6b 73 3a 20 20 41 4e 53 49 20 53 54 44 20  ocks:  ANSI STD 
76d40 31 30 30 33 2e 31 20 28 31 39 39 36 29 0a 2a 2a  1003.1 (1996).**
76d50 20 73 65 63 74 69 6f 6e 20 36 2e 35 2e 32 2e 32   section 6.5.2.2
76d60 20 6c 69 6e 65 73 20 34 38 33 20 74 68 72 6f 75   lines 483 throu
76d70 67 68 20 34 39 30 20 73 70 65 63 69 66 79 20 74  gh 490 specify t
76d80 68 61 74 20 77 68 65 6e 20 61 20 70 72 6f 63 65  hat when a proce
76d90 73 73 0a 2a 2a 20 73 65 74 73 20 6f 72 20 63 6c  ss.** sets or cl
76da0 65 61 72 73 20 61 20 6c 6f 63 6b 2c 20 74 68 61  ears a lock, tha
76db0 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 76 65 72  t operation over
76dc0 72 69 64 65 73 20 61 6e 79 20 70 72 69 6f 72 20  rides any prior 
76dd0 6c 6f 63 6b 73 20 73 65 74 0a 2a 2a 20 62 79 20  locks set.** by 
76de0 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73  the same process
76df0 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 65  .  It does not e
76e00 78 70 6c 69 63 69 74 6c 79 20 73 61 79 20 73 6f  xplicitly say so
76e10 2c 20 62 75 74 20 74 68 69 73 20 69 6d 70 6c 69  , but this impli
76e20 65 73 0a 2a 2a 20 74 68 61 74 20 69 74 20 6f 76  es.** that it ov
76e30 65 72 72 69 64 65 73 20 6c 6f 63 6b 73 20 73 65  errides locks se
76e40 74 20 62 79 20 74 68 65 20 73 61 6d 65 20 70 72  t by the same pr
76e50 6f 63 65 73 73 20 75 73 69 6e 67 20 61 20 64 69  ocess using a di
76e60 66 66 65 72 65 6e 74 0a 2a 2a 20 66 69 6c 65 20  fferent.** file 
76e70 64 65 73 63 72 69 70 74 6f 72 2e 20 20 43 6f 6e  descriptor.  Con
76e80 73 69 64 65 72 20 74 68 69 73 20 74 65 73 74 20  sider this test 
76e90 63 61 73 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  case:.**.**     
76ea0 20 20 69 6e 74 20 66 64 31 20 3d 20 6f 70 65 6e    int fd1 = open
76eb0 28 22 2e 2f 66 69 6c 65 31 22 2c 20 4f 5f 52 44  ("./file1", O_RD
76ec0 57 52 7c 4f 5f 43 52 45 41 54 2c 20 30 36 34 34  WR|O_CREAT, 0644
76ed0 29 3b 0a 2a 2a 20 20 20 20 20 20 20 69 6e 74 20  );.**       int 
76ee0 66 64 32 20 3d 20 6f 70 65 6e 28 22 2e 2f 66 69  fd2 = open("./fi
76ef0 6c 65 32 22 2c 20 4f 5f 52 44 57 52 7c 4f 5f 43  le2", O_RDWR|O_C
76f00 52 45 41 54 2c 20 30 36 34 34 29 3b 0a 2a 2a 0a  REAT, 0644);.**.
76f10 2a 2a 20 53 75 70 70 6f 73 65 20 2e 2f 66 69 6c  ** Suppose ./fil
76f20 65 31 20 61 6e 64 20 2e 2f 66 69 6c 65 32 20 61  e1 and ./file2 a
76f30 72 65 20 72 65 61 6c 6c 79 20 74 68 65 20 73 61  re really the sa
76f40 6d 65 20 66 69 6c 65 20 28 62 65 63 61 75 73 65  me file (because
76f50 0a 2a 2a 20 6f 6e 65 20 69 73 20 61 20 68 61 72  .** one is a har
76f60 64 20 6f 72 20 73 79 6d 62 6f 6c 69 63 20 6c 69  d or symbolic li
76f70 6e 6b 20 74 6f 20 74 68 65 20 6f 74 68 65 72 29  nk to the other)
76f80 20 74 68 65 6e 20 69 66 20 79 6f 75 20 73 65 74   then if you set
76f90 0a 2a 2a 20 61 6e 20 65 78 63 6c 75 73 69 76 65  .** an exclusive
76fa0 20 6c 6f 63 6b 20 6f 6e 20 66 64 31 2c 20 74 68   lock on fd1, th
76fb0 65 6e 20 74 72 79 20 74 6f 20 67 65 74 20 61 6e  en try to get an
76fc0 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 0a   exclusive lock.
76fd0 2a 2a 20 6f 6e 20 66 64 32 2c 20 69 74 20 77 6f  ** on fd2, it wo
76fe0 72 6b 73 2e 20 20 49 20 77 6f 75 6c 64 20 68 61  rks.  I would ha
76ff0 76 65 20 65 78 70 65 63 74 65 64 20 74 68 65 20  ve expected the 
77000 73 65 63 6f 6e 64 20 6c 6f 63 6b 20 74 6f 0a 2a  second lock to.*
77010 2a 20 66 61 69 6c 20 73 69 6e 63 65 20 74 68 65  * fail since the
77020 72 65 20 77 61 73 20 61 6c 72 65 61 64 79 20 61  re was already a
77030 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c   lock on the fil
77040 65 20 64 75 65 20 74 6f 20 66 64 31 2e 0a 2a 2a  e due to fd1..**
77050 20 42 75 74 20 6e 6f 74 20 73 6f 2e 20 20 53 69   But not so.  Si
77060 6e 63 65 20 62 6f 74 68 20 6c 6f 63 6b 73 20 63  nce both locks c
77070 61 6d 65 20 66 72 6f 6d 20 74 68 65 20 73 61 6d  ame from the sam
77080 65 20 70 72 6f 63 65 73 73 2c 20 74 68 65 0a 2a  e process, the.*
77090 2a 20 73 65 63 6f 6e 64 20 6f 76 65 72 72 69 64  * second overrid
770a0 65 73 20 74 68 65 20 66 69 72 73 74 2c 20 65 76  es the first, ev
770b0 65 6e 20 74 68 6f 75 67 68 20 74 68 65 79 20 77  en though they w
770c0 65 72 65 20 6f 6e 20 64 69 66 66 65 72 65 6e 74  ere on different
770d0 0a 2a 2a 20 66 69 6c 65 20 64 65 73 63 72 69 70  .** file descrip
770e0 74 6f 72 73 20 6f 70 65 6e 65 64 20 6f 6e 20 64  tors opened on d
770f0 69 66 66 65 72 65 6e 74 20 66 69 6c 65 20 6e 61  ifferent file na
77100 6d 65 73 2e 0a 2a 2a 0a 2a 2a 20 42 75 6d 6d 65  mes..**.** Bumme
77110 72 2e 20 20 49 66 20 79 6f 75 20 61 73 6b 20 6d  r.  If you ask m
77120 65 2c 20 74 68 69 73 20 69 73 20 62 72 6f 6b 65  e, this is broke
77130 6e 2e 20 20 42 61 64 6c 79 20 62 72 6f 6b 65 6e  n.  Badly broken
77140 2e 20 20 49 74 20 6d 65 61 6e 73 0a 2a 2a 20 74  .  It means.** t
77150 68 61 74 20 77 65 20 63 61 6e 6e 6f 74 20 75 73  hat we cannot us
77160 65 20 50 4f 53 49 58 20 6c 6f 63 6b 73 20 74 6f  e POSIX locks to
77170 20 73 79 6e 63 68 72 6f 6e 69 7a 65 20 66 69 6c   synchronize fil
77180 65 20 61 63 63 65 73 73 20 61 6d 6f 6e 67 0a 2a  e access among.*
77190 2a 20 63 6f 6d 70 65 74 69 6e 67 20 74 68 72 65  * competing thre
771a0 61 64 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20  ads of the same 
771b0 70 72 6f 63 65 73 73 2e 20 20 50 4f 53 49 58 20  process.  POSIX 
771c0 6c 6f 63 6b 73 20 77 69 6c 6c 20 77 6f 72 6b 20  locks will work 
771d0 66 69 6e 65 0a 2a 2a 20 74 6f 20 73 79 6e 63 68  fine.** to synch
771e0 72 6f 6e 69 7a 65 20 61 63 63 65 73 73 20 66 6f  ronize access fo
771f0 72 20 74 68 72 65 61 64 73 20 69 6e 20 73 65 70  r threads in sep
77200 61 72 61 74 65 20 70 72 6f 63 65 73 73 65 73 2c  arate processes,
77210 20 62 75 74 20 6e 6f 74 0a 2a 2a 20 74 68 72 65   but not.** thre
77220 61 64 73 20 77 69 74 68 69 6e 20 74 68 65 20 73  ads within the s
77230 61 6d 65 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a  ame process..**.
77240 2a 2a 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e  ** To work aroun
77250 64 20 74 68 65 20 70 72 6f 62 6c 65 6d 2c 20 53  d the problem, S
77260 51 4c 69 74 65 20 68 61 73 20 74 6f 20 6d 61 6e  QLite has to man
77270 61 67 65 20 66 69 6c 65 20 6c 6f 63 6b 73 20 69  age file locks i
77280 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 6f 6e 20  nternally.** on 
77290 69 74 73 20 6f 77 6e 2e 20 20 57 68 65 6e 65 76  its own.  Whenev
772a0 65 72 20 61 20 6e 65 77 20 64 61 74 61 62 61 73  er a new databas
772b0 65 20 69 73 20 6f 70 65 6e 65 64 2c 20 77 65 20  e is opened, we 
772c0 68 61 76 65 20 74 6f 20 66 69 6e 64 20 74 68 65  have to find the
772d0 0a 2a 2a 20 73 70 65 63 69 66 69 63 20 69 6e 6f  .** specific ino
772e0 64 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  de of the databa
772f0 73 65 20 66 69 6c 65 20 28 74 68 65 20 69 6e 6f  se file (the ino
77300 64 65 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64  de is determined
77310 20 62 79 20 74 68 65 0a 2a 2a 20 73 74 5f 64 65   by the.** st_de
77320 76 20 61 6e 64 20 73 74 5f 69 6e 6f 20 66 69 65  v and st_ino fie
77330 6c 64 73 20 6f 66 20 74 68 65 20 73 74 61 74 20  lds of the stat 
77340 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 66  structure that f
77350 73 74 61 74 28 29 20 66 69 6c 6c 73 20 69 6e 29  stat() fills in)
77360 0a 2a 2a 20 61 6e 64 20 63 68 65 63 6b 20 66 6f  .** and check fo
77370 72 20 6c 6f 63 6b 73 20 61 6c 72 65 61 64 79 20  r locks already 
77380 65 78 69 73 74 69 6e 67 20 6f 6e 20 74 68 61 74  existing on that
77390 20 69 6e 6f 64 65 2e 20 20 57 68 65 6e 20 6c 6f   inode.  When lo
773a0 63 6b 73 20 61 72 65 0a 2a 2a 20 63 72 65 61 74  cks are.** creat
773b0 65 64 20 6f 72 20 72 65 6d 6f 76 65 64 2c 20 77  ed or removed, w
773c0 65 20 68 61 76 65 20 74 6f 20 6c 6f 6f 6b 20 61  e have to look a
773d0 74 20 6f 75 72 20 6f 77 6e 20 69 6e 74 65 72 6e  t our own intern
773e0 61 6c 20 72 65 63 6f 72 64 20 6f 66 20 74 68 65  al record of the
773f0 0a 2a 2a 20 6c 6f 63 6b 73 20 74 6f 20 73 65 65  .** locks to see
77400 20 69 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65   if another thre
77410 61 64 20 68 61 73 20 70 72 65 76 69 6f 75 73 6c  ad has previousl
77420 79 20 73 65 74 20 61 20 6c 6f 63 6b 20 6f 6e 20  y set a lock on 
77430 74 68 61 74 20 73 61 6d 65 0a 2a 2a 20 69 6e 6f  that same.** ino
77440 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 4f 73  de..**.** The Os
77450 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 66  File structure f
77460 6f 72 20 50 4f 53 49 58 20 69 73 20 6e 6f 20 6c  or POSIX is no l
77470 6f 6e 67 65 72 20 6a 75 73 74 20 61 6e 20 69 6e  onger just an in
77480 74 65 67 65 72 20 66 69 6c 65 0a 2a 2a 20 64 65  teger file.** de
77490 73 63 72 69 70 74 6f 72 2e 20 20 49 74 20 69 73  scriptor.  It is
774a0 20 6e 6f 77 20 61 20 73 74 72 75 63 74 75 72 65   now a structure
774b0 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20   that holds the 
774c0 69 6e 74 65 67 65 72 20 66 69 6c 65 0a 2a 2a 20  integer file.** 
774d0 64 65 73 63 72 69 70 74 6f 72 20 61 6e 64 20 61  descriptor and a
774e0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74   pointer to a st
774f0 72 75 63 74 75 72 65 20 74 68 61 74 20 64 65 73  ructure that des
77500 63 72 69 62 65 73 20 74 68 65 20 69 6e 74 65 72  cribes the inter
77510 6e 61 6c 0a 2a 2a 20 6c 6f 63 6b 73 20 6f 6e 20  nal.** locks on 
77520 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
77530 67 20 69 6e 6f 64 65 2e 20 20 54 68 65 72 65 20  g inode.  There 
77540 69 73 20 6f 6e 65 20 6c 6f 63 6b 69 6e 67 20 73  is one locking s
77550 74 72 75 63 74 75 72 65 0a 2a 2a 20 70 65 72 20  tructure.** per 
77560 69 6e 6f 64 65 2c 20 73 6f 20 69 66 20 74 68 65  inode, so if the
77570 20 73 61 6d 65 20 69 6e 6f 64 65 20 69 73 20 6f   same inode is o
77580 70 65 6e 65 64 20 74 77 69 63 65 2c 20 62 6f 74  pened twice, bot
77590 68 20 4f 73 46 69 6c 65 20 73 74 72 75 63 74 75  h OsFile structu
775a0 72 65 73 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20  res.** point to 
775b0 74 68 65 20 73 61 6d 65 20 6c 6f 63 6b 69 6e 67  the same locking
775c0 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65   structure.  The
775d0 20 6c 6f 63 6b 69 6e 67 20 73 74 72 75 63 74 75   locking structu
775e0 72 65 20 6b 65 65 70 73 0a 2a 2a 20 61 20 72 65  re keeps.** a re
775f0 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 28 73  ference count (s
77600 6f 20 77 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 77  o we will know w
77610 68 65 6e 20 74 6f 20 64 65 6c 65 74 65 20 69 74  hen to delete it
77620 29 20 61 6e 64 20 61 20 22 63 6e 74 22 0a 2a 2a  ) and a "cnt".**
77630 20 66 69 65 6c 64 20 74 68 61 74 20 74 65 6c 6c   field that tell
77640 73 20 75 73 20 69 74 73 20 69 6e 74 65 72 6e 61  s us its interna
77650 6c 20 6c 6f 63 6b 20 73 74 61 74 75 73 2e 20 20  l lock status.  
77660 63 6e 74 3d 3d 30 20 6d 65 61 6e 73 20 74 68 65  cnt==0 means the
77670 0a 2a 2a 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f  .** file is unlo
77680 63 6b 65 64 2e 20 20 63 6e 74 3d 3d 2d 31 20 6d  cked.  cnt==-1 m
77690 65 61 6e 73 20 74 68 65 20 66 69 6c 65 20 68 61  eans the file ha
776a0 73 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c  s an exclusive l
776b0 6f 63 6b 2e 0a 2a 2a 20 63 6e 74 3e 30 20 6d 65  ock..** cnt>0 me
776c0 61 6e 73 20 74 68 65 72 65 20 61 72 65 20 63 6e  ans there are cn
776d0 74 20 73 68 61 72 65 64 20 6c 6f 63 6b 73 20 6f  t shared locks o
776e0 6e 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  n the file..**.*
776f0 2a 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f  * Any attempt to
77700 20 6c 6f 63 6b 20 6f 72 20 75 6e 6c 6f 63 6b 20   lock or unlock 
77710 61 20 66 69 6c 65 20 66 69 72 73 74 20 63 68 65  a file first che
77720 63 6b 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 0a  cks the locking.
77730 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20 54  ** structure.  T
77740 68 65 20 66 63 6e 74 6c 28 29 20 73 79 73 74 65  he fcntl() syste
77750 6d 20 63 61 6c 6c 20 69 73 20 6f 6e 6c 79 20 69  m call is only i
77760 6e 76 6f 6b 65 64 20 74 6f 20 73 65 74 20 61 20  nvoked to set a 
77770 0a 2a 2a 20 50 4f 53 49 58 20 6c 6f 63 6b 20 69  .** POSIX lock i
77780 66 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6c  f the internal l
77790 6f 63 6b 20 73 74 72 75 63 74 75 72 65 20 74 72  ock structure tr
777a0 61 6e 73 69 74 69 6f 6e 73 20 62 65 74 77 65 65  ansitions betwee
777b0 6e 0a 2a 2a 20 61 20 6c 6f 63 6b 65 64 20 61 6e  n.** a locked an
777c0 64 20 61 6e 20 75 6e 6c 6f 63 6b 65 64 20 73 74  d an unlocked st
777d0 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 34 2d  ate..**.** 2004-
777e0 4a 61 6e 2d 31 31 3a 0a 2a 2a 20 4d 6f 72 65 20  Jan-11:.** More 
777f0 72 65 63 65 6e 74 20 64 69 73 63 6f 76 65 72 69  recent discoveri
77800 65 73 20 61 62 6f 75 74 20 50 4f 53 49 58 20 61  es about POSIX a
77810 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 2e 20 20  dvisory locks.  
77820 28 54 68 65 20 6d 6f 72 65 0a 2a 2a 20 49 20 64  (The more.** I d
77830 69 73 63 6f 76 65 72 2c 20 74 68 65 20 6d 6f 72  iscover, the mor
77840 65 20 49 20 72 65 61 6c 69 7a 65 20 74 68 65 20  e I realize the 
77850 61 20 50 4f 53 49 58 20 61 64 76 69 73 6f 72 79  a POSIX advisory
77860 20 6c 6f 63 6b 73 20 61 72 65 0a 2a 2a 20 61 6e   locks are.** an
77870 20 61 62 6f 6d 69 6e 61 74 69 6f 6e 2e 29 0a 2a   abomination.).*
77880 2a 0a 2a 2a 20 49 66 20 79 6f 75 20 63 6c 6f 73  *.** If you clos
77890 65 20 61 20 66 69 6c 65 20 64 65 73 63 72 69 70  e a file descrip
778a0 74 6f 72 20 74 68 61 74 20 70 6f 69 6e 74 73 20  tor that points 
778b0 74 6f 20 61 20 66 69 6c 65 20 74 68 61 74 20 68  to a file that h
778c0 61 73 20 6c 6f 63 6b 73 2c 0a 2a 2a 20 61 6c 6c  as locks,.** all
778d0 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 61 74 20 66   locks on that f
778e0 69 6c 65 20 74 68 61 74 20 61 72 65 20 6f 77 6e  ile that are own
778f0 65 64 20 62 79 20 74 68 65 20 63 75 72 72 65 6e  ed by the curren
77900 74 20 70 72 6f 63 65 73 73 20 61 72 65 0a 2a 2a  t process are.**
77910 20 72 65 6c 65 61 73 65 64 2e 20 20 54 6f 20 77   released.  To w
77920 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 69 73 20  ork around this 
77930 70 72 6f 62 6c 65 6d 2c 20 65 61 63 68 20 4f 73  problem, each Os
77940 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 63  File structure c
77950 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 70 6f 69  ontains.** a poi
77960 6e 74 65 72 20 74 6f 20 61 6e 20 6f 70 65 6e 43  nter to an openC
77970 6e 74 20 73 74 72 75 63 74 75 72 65 2e 20 20 54  nt structure.  T
77980 68 65 72 65 20 69 73 20 6f 6e 65 20 6f 70 65 6e  here is one open
77990 43 6e 74 20 73 74 72 75 63 74 75 72 65 0a 2a 2a  Cnt structure.**
779a0 20 70 65 72 20 6f 70 65 6e 20 69 6e 6f 64 65 2c   per open inode,
779b0 20 77 68 69 63 68 20 6d 65 61 6e 73 20 74 68 61   which means tha
779c0 74 20 6d 75 6c 74 69 70 6c 65 20 4f 73 46 69 6c  t multiple OsFil
779d0 65 73 20 63 61 6e 20 70 6f 69 6e 74 20 74 6f 20  es can point to 
779e0 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 6f 70 65 6e  a single.** open
779f0 43 6e 74 2e 20 20 57 68 65 6e 20 61 6e 20 61 74  Cnt.  When an at
77a00 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f  tempt is made to
77a10 20 63 6c 6f 73 65 20 61 6e 20 4f 73 46 69 6c 65   close an OsFile
77a20 2c 20 69 66 20 74 68 65 72 65 20 61 72 65 0a 2a  , if there are.*
77a30 2a 20 6f 74 68 65 72 20 4f 73 46 69 6c 65 73 20  * other OsFiles 
77a40 6f 70 65 6e 20 6f 6e 20 74 68 65 20 73 61 6d 65  open on the same
77a50 20 69 6e 6f 64 65 20 74 68 61 74 20 61 72 65 20   inode that are 
77a60 68 6f 6c 64 69 6e 67 20 6c 6f 63 6b 73 2c 20 74  holding locks, t
77a70 68 65 20 63 61 6c 6c 0a 2a 2a 20 74 6f 20 63 6c  he call.** to cl
77a80 6f 73 65 28 29 20 74 68 65 20 66 69 6c 65 20 64  ose() the file d
77a90 65 73 63 72 69 70 74 6f 72 20 69 73 20 64 65 66  escriptor is def
77aa0 65 72 72 65 64 20 75 6e 74 69 6c 20 61 6c 6c 20  erred until all 
77ab0 6f 66 20 74 68 65 20 6c 6f 63 6b 73 20 63 6c 65  of the locks cle
77ac0 61 72 2e 0a 2a 2a 20 54 68 65 20 6f 70 65 6e 43  ar..** The openC
77ad0 6e 74 20 73 74 72 75 63 74 75 72 65 20 6b 65 65  nt structure kee
77ae0 70 73 20 61 20 6c 69 73 74 20 6f 66 20 66 69 6c  ps a list of fil
77af0 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 74 68  e descriptors th
77b00 61 74 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 62 65  at need to.** be
77b10 20 63 6c 6f 73 65 64 20 61 6e 64 20 74 68 61 74   closed and that
77b20 20 6c 69 73 74 20 69 73 20 77 61 6c 6b 65 64 20   list is walked 
77b30 28 61 6e 64 20 63 6c 65 61 72 65 64 29 20 77 68  (and cleared) wh
77b40 65 6e 20 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b  en the last lock
77b50 0a 2a 2a 20 63 6c 65 61 72 73 2e 0a 2a 2a 0a 2a  .** clears..**.*
77b60 2a 20 46 69 72 73 74 2c 20 75 6e 64 65 72 20 4c  * First, under L
77b70 69 6e 75 78 20 74 68 72 65 61 64 73 2c 20 62 65  inux threads, be
77b80 63 61 75 73 65 20 65 61 63 68 20 74 68 72 65 61  cause each threa
77b90 64 20 68 61 73 20 61 20 73 65 70 61 72 61 74 65  d has a separate
77ba0 0a 2a 2a 20 70 72 6f 63 65 73 73 20 49 44 2c 20  .** process ID, 
77bb0 6c 6f 63 6b 20 6f 70 65 72 61 74 69 6f 6e 73 20  lock operations 
77bc0 69 6e 20 6f 6e 65 20 74 68 72 65 61 64 20 64 6f  in one thread do
77bd0 20 6e 6f 74 20 6f 76 65 72 72 69 64 65 20 6c 6f   not override lo
77be0 63 6b 73 0a 2a 2a 20 74 6f 20 74 68 65 20 73 61  cks.** to the sa
77bf0 6d 65 20 66 69 6c 65 20 69 6e 20 6f 74 68 65 72  me file in other
77c00 20 74 68 72 65 61 64 73 2e 20 20 4c 69 6e 75 78   threads.  Linux
77c10 20 74 68 72 65 61 64 73 20 62 65 68 61 76 65 20   threads behave 
77c20 6c 69 6b 65 0a 2a 2a 20 73 65 70 61 72 61 74 65  like.** separate
77c30 20 70 72 6f 63 65 73 73 65 73 20 69 6e 20 74 68   processes in th
77c40 69 73 20 72 65 73 70 65 63 74 2e 20 20 42 75 74  is respect.  But
77c50 2c 20 69 66 20 79 6f 75 20 63 6c 6f 73 65 20 61  , if you close a
77c60 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70   file.** descrip
77c70 74 6f 72 20 69 6e 20 6c 69 6e 75 78 20 74 68 72  tor in linux thr
77c80 65 61 64 73 2c 20 61 6c 6c 20 6c 6f 63 6b 73 20  eads, all locks 
77c90 61 72 65 20 63 6c 65 61 72 65 64 2c 20 65 76 65  are cleared, eve
77ca0 6e 20 6c 6f 63 6b 73 0a 2a 2a 20 6f 6e 20 6f 74  n locks.** on ot
77cb0 68 65 72 20 74 68 72 65 61 64 73 20 61 6e 64 20  her threads and 
77cc0 65 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65 20  even though the 
77cd0 6f 74 68 65 72 20 74 68 72 65 61 64 73 20 68 61  other threads ha
77ce0 76 65 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20  ve different.** 
77cf0 70 72 6f 63 65 73 73 20 49 44 73 2e 20 20 4c 69  process IDs.  Li
77d00 6e 75 78 20 74 68 72 65 61 64 73 20 69 73 20 69  nux threads is i
77d10 6e 63 6f 6e 73 69 73 74 65 6e 74 20 69 6e 20 74  nconsistent in t
77d20 68 69 73 20 72 65 73 70 65 63 74 2e 0a 2a 2a 20  his respect..** 
77d30 28 49 27 6d 20 62 65 67 69 6e 6e 69 6e 67 20 74  (I'm beginning t
77d40 6f 20 74 68 69 6e 6b 20 74 68 61 74 20 6c 69 6e  o think that lin
77d50 75 78 20 74 68 72 65 61 64 73 20 69 73 20 61 6e  ux threads is an
77d60 20 61 62 6f 6d 69 6e 61 74 69 6f 6e 20 74 6f 6f   abomination too
77d70 2e 29 0a 2a 2a 20 54 68 65 20 63 6f 6e 73 65 71  .).** The conseq
77d80 75 65 6e 63 65 20 6f 66 20 74 68 69 73 20 61 6c  uence of this al
77d90 6c 20 69 73 20 74 68 61 74 20 74 68 65 20 68 61  l is that the ha
77da0 73 68 20 74 61 62 6c 65 20 66 6f 72 20 74 68 65  sh table for the
77db0 20 6c 6f 63 6b 49 6e 66 6f 0a 2a 2a 20 73 74 72   lockInfo.** str
77dc0 75 63 74 75 72 65 20 68 61 73 20 74 6f 20 69 6e  ucture has to in
77dd0 63 6c 75 64 65 20 74 68 65 20 70 72 6f 63 65 73  clude the proces
77de0 73 20 69 64 20 61 73 20 70 61 72 74 20 6f 66 20  s id as part of 
77df0 69 74 73 20 6b 65 79 20 62 65 63 61 75 73 65 0a  its key because.
77e00 2a 2a 20 6c 6f 63 6b 73 20 69 6e 20 64 69 66 66  ** locks in diff
77e10 65 72 65 6e 74 20 74 68 72 65 61 64 73 20 61 72  erent threads ar
77e20 65 20 74 72 65 61 74 65 64 20 61 73 20 64 69 73  e treated as dis
77e30 74 69 6e 63 74 2e 20 20 42 75 74 20 74 68 65 20  tinct.  But the 
77e40 0a 2a 2a 20 6f 70 65 6e 43 6e 74 20 73 74 72 75  .** openCnt stru
77e50 63 74 75 72 65 20 73 68 6f 75 6c 64 20 6e 6f 74  cture should not
77e60 20 69 6e 63 6c 75 64 65 20 74 68 65 20 70 72 6f   include the pro
77e70 63 65 73 73 20 69 64 20 69 6e 20 69 74 73 0a 2a  cess id in its.*
77e80 2a 20 6b 65 79 20 62 65 63 61 75 73 65 20 63 6c  * key because cl
77e90 6f 73 65 28 29 20 63 6c 65 61 72 73 20 6c 6f 63  ose() clears loc
77ea0 6b 20 6f 6e 20 61 6c 6c 20 74 68 72 65 61 64 73  k on all threads
77eb0 2c 20 6e 6f 74 20 6a 75 73 74 20 74 68 65 20 63  , not just the c
77ec0 75 72 72 65 6e 74 0a 2a 2a 20 74 68 72 65 61 64  urrent.** thread
77ed0 2e 20 20 57 65 72 65 20 69 74 20 6e 6f 74 20 66  .  Were it not f
77ee0 6f 72 20 74 68 69 73 20 67 6f 6f 66 69 6e 65 73  or this goofines
77ef0 73 20 69 6e 20 6c 69 6e 75 78 20 74 68 72 65 61  s in linux threa
77f00 64 73 2c 20 77 65 20 63 6f 75 6c 64 0a 2a 2a 20  ds, we could.** 
77f10 63 6f 6d 62 69 6e 65 20 74 68 65 20 6c 6f 63 6b  combine the lock
77f20 49 6e 66 6f 20 61 6e 64 20 6f 70 65 6e 43 6e 74  Info and openCnt
77f30 20 73 74 72 75 63 74 75 72 65 73 20 69 6e 74 6f   structures into
77f40 20 61 20 73 69 6e 67 6c 65 20 73 74 72 75 63 74   a single struct
77f50 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 34 2d  ure..**.** 2004-
77f60 4a 75 6e 2d 32 38 3a 0a 2a 2a 20 4f 6e 20 73 6f  Jun-28:.** On so
77f70 6d 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 6c  me versions of l
77f80 69 6e 75 78 2c 20 74 68 72 65 61 64 73 20 63 61  inux, threads ca
77f90 6e 20 6f 76 65 72 72 69 64 65 20 65 61 63 68 20  n override each 
77fa0 6f 74 68 65 72 73 20 6c 6f 63 6b 73 2e 0a 2a 2a  others locks..**
77fb0 20 4f 6e 20 6f 74 68 65 72 73 20 6e 6f 74 2e 20   On others not. 
77fc0 20 53 6f 6d 65 74 69 6d 65 73 20 79 6f 75 20 63   Sometimes you c
77fd0 61 6e 20 63 68 61 6e 67 65 20 74 68 65 20 62 65  an change the be
77fe0 68 61 76 69 6f 72 20 6f 6e 20 74 68 65 20 73 61  havior on the sa
77ff0 6d 65 0a 2a 2a 20 73 79 73 74 65 6d 20 62 79 20  me.** system by 
78000 73 65 74 74 69 6e 67 20 74 68 65 20 4c 44 5f 41  setting the LD_A
78010 53 53 55 4d 45 5f 4b 45 52 4e 45 4c 20 65 6e 76  SSUME_KERNEL env
78020 69 72 6f 6e 6d 65 6e 74 20 76 61 72 69 61 62 6c  ironment variabl
78030 65 2e 20 20 54 68 65 0a 2a 2a 20 50 4f 53 49 58  e.  The.** POSIX
78040 20 73 74 61 6e 64 61 72 64 20 69 73 20 73 69 6c   standard is sil
78050 65 6e 74 20 61 73 20 74 6f 20 77 68 69 63 68 20  ent as to which 
78060 62 65 68 61 76 69 6f 72 20 69 73 20 63 6f 72 72  behavior is corr
78070 65 63 74 2c 20 61 73 20 66 61 72 0a 2a 2a 20 61  ect, as far.** a
78080 73 20 49 20 63 61 6e 20 74 65 6c 6c 2c 20 73 6f  s I can tell, so
78090 20 6f 74 68 65 72 20 76 65 72 73 69 6f 6e 73 20   other versions 
780a0 6f 66 20 75 6e 69 78 20 6d 69 67 68 74 20 73 68  of unix might sh
780b0 6f 77 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 69  ow the same.** i
780c0 6e 63 6f 6e 73 69 73 74 65 6e 63 79 2e 20 20 54  nconsistency.  T
780d0 68 65 72 65 20 69 73 20 6e 6f 20 6c 69 74 74 6c  here is no littl
780e0 65 20 64 6f 75 62 74 20 69 6e 20 6d 79 20 6d 69  e doubt in my mi
780f0 6e 64 20 74 68 61 74 20 70 6f 73 69 78 0a 2a 2a  nd that posix.**
78100 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 20   advisory locks 
78110 61 6e 64 20 6c 69 6e 75 78 20 74 68 72 65 61 64  and linux thread
78120 73 20 61 72 65 20 70 72 6f 66 6f 75 6e 64 6c 79  s are profoundly
78130 20 62 72 6f 6b 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54   broken..**.** T
78140 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68  o work around th
78150 65 20 69 6e 63 6f 6e 73 69 73 74 65 6e 63 69 65  e inconsistencie
78160 73 2c 20 77 65 20 68 61 76 65 20 74 6f 20 74 65  s, we have to te
78170 73 74 20 61 74 20 72 75 6e 74 69 6d 65 20 0a 2a  st at runtime .*
78180 2a 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  * whether or not
78190 20 74 68 72 65 61 64 73 20 63 61 6e 20 6f 76 65   threads can ove
781a0 72 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 72  rride each other
781b0 73 20 6c 6f 63 6b 73 2e 20 20 54 68 69 73 20 74  s locks.  This t
781c0 65 73 74 0a 2a 2a 20 69 73 20 72 75 6e 20 6f 6e  est.** is run on
781d0 63 65 2c 20 74 68 65 20 66 69 72 73 74 20 74 69  ce, the first ti
781e0 6d 65 20 61 6e 79 20 6c 6f 63 6b 20 69 73 20 61  me any lock is a
781f0 74 74 65 6d 70 74 65 64 2e 20 20 41 20 73 74 61  ttempted.  A sta
78200 74 69 63 20 0a 2a 2a 20 76 61 72 69 61 62 6c 65  tic .** variable
78210 20 69 73 20 73 65 74 20 74 6f 20 72 65 63 6f 72   is set to recor
78220 64 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66  d the results of
78230 20 74 68 69 73 20 74 65 73 74 20 66 6f 72 20 66   this test for f
78240 75 74 75 72 65 0a 2a 2a 20 75 73 65 2e 0a 2a 2f  uture.** use..*/
78250 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  ../*.** An insta
78260 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
78270 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 73  wing structure s
78280 65 72 76 65 73 20 61 73 20 74 68 65 20 6b 65 79  erves as the key
78290 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f 63 61   used.** to loca
782a0 74 65 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  te a particular 
782b0 6c 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63 74 75  lockInfo structu
782c0 72 65 20 67 69 76 65 6e 20 69 74 73 20 69 6e 6f  re given its ino
782d0 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 72  de..**.** If thr
782e0 65 61 64 73 20 63 61 6e 6e 6f 74 20 6f 76 65 72  eads cannot over
782f0 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 73  ride each others
78300 20 6c 6f 63 6b 73 2c 20 74 68 65 6e 20 77 65 20   locks, then we 
78310 73 65 74 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 4b  set the.** lockK
78320 65 79 2e 74 69 64 20 66 69 65 6c 64 20 74 6f 20  ey.tid field to 
78330 74 68 65 20 74 68 72 65 61 64 20 49 44 2e 20 20  the thread ID.  
78340 49 66 20 74 68 72 65 61 64 73 20 63 61 6e 20 6f  If threads can o
78350 76 65 72 72 69 64 65 0a 2a 2a 20 65 61 63 68 20  verride.** each 
78360 6f 74 68 65 72 73 20 6c 6f 63 6b 73 20 74 68 65  others locks the
78370 6e 20 74 69 64 20 69 73 20 61 6c 77 61 79 73 20  n tid is always 
78380 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 74 69  set to zero.  ti
78390 64 20 69 73 20 6f 6d 69 74 74 65 64 0a 2a 2a 20  d is omitted.** 
783a0 69 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69  if we compile wi
783b0 74 68 6f 75 74 20 74 68 72 65 61 64 69 6e 67 20  thout threading 
783c0 73 75 70 70 6f 72 74 2e 0a 2a 2f 0a 73 74 72 75  support..*/.stru
783d0 63 74 20 6c 6f 63 6b 4b 65 79 20 7b 0a 20 20 64  ct lockKey {.  d
783e0 65 76 5f 74 20 64 65 76 3b 20 20 20 20 20 20 20  ev_t dev;       
783f0 2f 2a 20 44 65 76 69 63 65 20 6e 75 6d 62 65 72  /* Device number
78400 20 2a 2f 0a 20 20 69 6e 6f 5f 74 20 69 6e 6f 3b   */.  ino_t ino;
78410 20 20 20 20 20 20 20 2f 2a 20 49 6e 6f 64 65 20         /* Inode 
78420 6e 75 6d 62 65 72 20 2a 2f 0a 23 69 66 64 65 66  number */.#ifdef
78430 20 53 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48 52   SQLITE_UNIX_THR
78440 45 41 44 53 0a 20 20 70 74 68 72 65 61 64 5f 74  EADS.  pthread_t
78450 20 74 69 64 3b 20 20 20 2f 2a 20 54 68 72 65 61   tid;   /* Threa
78460 64 20 49 44 20 6f 72 20 7a 65 72 6f 20 69 66 20  d ID or zero if 
78470 74 68 72 65 61 64 73 20 63 61 6e 20 6f 76 65 72  threads can over
78480 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 20  ride each other 
78490 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a  */.#endif.};../*
784a0 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
784b0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
784c0 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c   structure is al
784d0 6c 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63 68  located for each
784e0 20 6f 70 65 6e 0a 2a 2a 20 69 6e 6f 64 65 20 6f   open.** inode o
784f0 6e 20 65 61 63 68 20 74 68 72 65 61 64 20 77 69  n each thread wi
78500 74 68 20 61 20 64 69 66 66 65 72 65 6e 74 20 70  th a different p
78510 72 6f 63 65 73 73 20 49 44 2e 20 20 28 54 68 72  rocess ID.  (Thr
78520 65 61 64 73 20 68 61 76 65 0a 2a 2a 20 64 69 66  eads have.** dif
78530 66 65 72 65 6e 74 20 70 72 6f 63 65 73 73 20 49  ferent process I
78540 44 73 20 6f 6e 20 6c 69 6e 75 78 2c 20 62 75 74  Ds on linux, but
78550 20 6e 6f 74 20 6f 6e 20 6d 6f 73 74 20 6f 74 68   not on most oth
78560 65 72 20 75 6e 69 78 65 73 2e 29 0a 2a 2a 0a 2a  er unixes.).**.*
78570 2a 20 41 20 73 69 6e 67 6c 65 20 69 6e 6f 64 65  * A single inode
78580 20 63 61 6e 20 68 61 76 65 20 6d 75 6c 74 69 70   can have multip
78590 6c 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  le file descript
785a0 6f 72 73 2c 20 73 6f 20 65 61 63 68 20 4f 73 46  ors, so each OsF
785b0 69 6c 65 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  ile.** structure
785c0 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e   contains a poin
785d0 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e  ter to an instan
785e0 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63  ce of this objec
785f0 74 20 61 6e 64 20 74 68 69 73 0a 2a 2a 20 6f 62  t and this.** ob
78600 6a 65 63 74 20 6b 65 65 70 73 20 61 20 63 6f 75  ject keeps a cou
78610 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72  nt of the number
78620 20 6f 66 20 4f 73 46 69 6c 65 73 20 70 6f 69 6e   of OsFiles poin
78630 74 69 6e 67 20 74 6f 20 69 74 2e 0a 2a 2f 0a 73  ting to it..*/.s
78640 74 72 75 63 74 20 6c 6f 63 6b 49 6e 66 6f 20 7b  truct lockInfo {
78650 0a 20 20 73 74 72 75 63 74 20 6c 6f 63 6b 4b 65  .  struct lockKe
78660 79 20 6b 65 79 3b 20 20 2f 2a 20 54 68 65 20 6c  y key;  /* The l
78670 6f 6f 6b 75 70 20 6b 65 79 20 2a 2f 0a 20 20 69  ookup key */.  i
78680 6e 74 20 63 6e 74 3b 20 20 20 20 20 20 20 20 20  nt cnt;         
78690 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
786a0 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 68 65   SHARED locks he
786b0 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 6c 6f 63 6b  ld */.  int lock
786c0 74 79 70 65 3b 20 20 20 20 20 20 20 20 2f 2a 20  type;        /* 
786d0 4f 6e 65 20 6f 66 20 53 48 41 52 45 44 5f 4c 4f  One of SHARED_LO
786e0 43 4b 2c 20 52 45 53 45 52 56 45 44 5f 4c 4f 43  CK, RESERVED_LOC
786f0 4b 20 65 74 63 2e 20 2a 2f 0a 20 20 69 6e 74 20  K etc. */.  int 
78700 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20  nRef;           
78710 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f   /* Number of po
78720 69 6e 74 65 72 73 20 74 6f 20 74 68 69 73 20 73  inters to this s
78730 74 72 75 63 74 75 72 65 20 2a 2f 0a 7d 3b 0a 0a  tructure */.};..
78740 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
78750 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
78760 6e 67 20 73 74 72 75 63 74 75 72 65 20 73 65 72  ng structure ser
78770 76 65 73 20 61 73 20 74 68 65 20 6b 65 79 20 75  ves as the key u
78780 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f 63 61 74 65  sed.** to locate
78790 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 6f 70   a particular op
787a0 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 20  enCnt structure 
787b0 67 69 76 65 6e 20 69 74 73 20 69 6e 6f 64 65 2e  given its inode.
787c0 20 20 54 68 69 73 0a 2a 2a 20 69 73 20 74 68 65    This.** is the
787d0 20 73 61 6d 65 20 61 73 20 74 68 65 20 6c 6f 63   same as the loc
787e0 6b 4b 65 79 20 65 78 63 65 70 74 20 74 68 61 74  kKey except that
787f0 20 74 68 65 20 74 68 72 65 61 64 20 49 44 20 69   the thread ID i
78800 73 20 6f 6d 69 74 74 65 64 2e 0a 2a 2f 0a 73 74  s omitted..*/.st
78810 72 75 63 74 20 6f 70 65 6e 4b 65 79 20 7b 0a 20  ruct openKey {. 
78820 20 64 65 76 5f 74 20 64 65 76 3b 20 20 20 2f 2a   dev_t dev;   /*
78830 20 44 65 76 69 63 65 20 6e 75 6d 62 65 72 20 2a   Device number *
78840 2f 0a 20 20 69 6e 6f 5f 74 20 69 6e 6f 3b 20 20  /.  ino_t ino;  
78850 20 2f 2a 20 49 6e 6f 64 65 20 6e 75 6d 62 65 72   /* Inode number
78860 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e   */.};../*.** An
78870 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
78880 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
78890 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 65  ture is allocate
788a0 64 20 66 6f 72 20 65 61 63 68 20 6f 70 65 6e 0a  d for each open.
788b0 2a 2a 20 69 6e 6f 64 65 2e 20 20 54 68 69 73 20  ** inode.  This 
788c0 73 74 72 75 63 74 75 72 65 20 6b 65 65 70 73 20  structure keeps 
788d0 74 72 61 63 6b 20 6f 66 20 74 68 65 20 6e 75 6d  track of the num
788e0 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 6f 6e 20  ber of locks on 
788f0 74 68 61 74 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20  that.** inode.  
78900 49 66 20 61 20 63 6c 6f 73 65 20 69 73 20 61 74  If a close is at
78910 74 65 6d 70 74 65 64 20 61 67 61 69 6e 73 74 20  tempted against 
78920 61 6e 20 69 6e 6f 64 65 20 74 68 61 74 20 69 73  an inode that is
78930 20 68 6f 6c 64 69 6e 67 0a 2a 2a 20 6c 6f 63 6b   holding.** lock
78940 73 2c 20 74 68 65 20 63 6c 6f 73 65 20 69 73 20  s, the close is 
78950 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c 20 61  deferred until a
78960 6c 6c 20 6c 6f 63 6b 73 20 63 6c 65 61 72 20 62  ll locks clear b
78970 79 20 61 64 64 69 6e 67 20 74 68 65 0a 2a 2a 20  y adding the.** 
78980 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
78990 74 6f 20 62 65 20 63 6c 6f 73 65 64 20 74 6f 20  to be closed to 
789a0 74 68 65 20 70 65 6e 64 69 6e 67 20 6c 69 73 74  the pending list
789b0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 6f 70 65 6e  ..*/.struct open
789c0 43 6e 74 20 7b 0a 20 20 73 74 72 75 63 74 20 6f  Cnt {.  struct o
789d0 70 65 6e 4b 65 79 20 6b 65 79 3b 20 20 20 2f 2a  penKey key;   /*
789e0 20 54 68 65 20 6c 6f 6f 6b 75 70 20 6b 65 79 20   The lookup key 
789f0 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20  */.  int nRef;  
78a00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
78a10 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74 65 72 73  mber of pointers
78a20 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75   to this structu
78a30 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 6f 63  re */.  int nLoc
78a40 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  k;            /*
78a50 20 4e 75 6d 62 65 72 20 6f 66 20 6f 75 74 73 74   Number of outst
78a60 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 20 2a 2f 0a  anding locks */.
78a70 20 20 69 6e 74 20 6e 50 65 6e 64 69 6e 67 3b 20    int nPending; 
78a80 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
78a90 72 20 6f 66 20 70 65 6e 64 69 6e 67 20 63 6c 6f  r of pending clo
78aa0 73 65 28 29 20 6f 70 65 72 61 74 69 6f 6e 73 20  se() operations 
78ab0 2a 2f 0a 20 20 69 6e 74 20 2a 61 50 65 6e 64 69  */.  int *aPendi
78ac0 6e 67 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 61  ng;        /* Ma
78ad0 6c 6c 6f 63 65 64 20 73 70 61 63 65 20 68 6f 6c  lloced space hol
78ae0 64 69 6e 67 20 66 64 27 73 20 61 77 61 69 74 69  ding fd's awaiti
78af0 6e 67 20 61 20 63 6c 6f 73 65 28 29 20 2a 2f 0a  ng a close() */.
78b00 7d 3b 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 65 73 65  };../* .** These
78b10 20 68 61 73 68 20 74 61 62 6c 65 73 20 6d 61 70   hash tables map
78b20 20 69 6e 6f 64 65 73 20 61 6e 64 20 66 69 6c 65   inodes and file
78b30 20 64 65 73 63 72 69 70 74 6f 72 73 20 28 72 65   descriptors (re
78b40 61 6c 6c 79 2c 20 6c 6f 63 6b 4b 65 79 20 61 6e  ally, lockKey an
78b50 64 0a 2a 2a 20 6f 70 65 6e 4b 65 79 20 73 74 72  d.** openKey str
78b60 75 63 74 75 72 65 73 29 20 69 6e 74 6f 20 6c 6f  uctures) into lo
78b70 63 6b 49 6e 66 6f 20 61 6e 64 20 6f 70 65 6e 43  ckInfo and openC
78b80 6e 74 20 73 74 72 75 63 74 75 72 65 73 2e 20 20  nt structures.  
78b90 41 63 63 65 73 73 20 74 6f 20 0a 2a 2a 20 74 68  Access to .** th
78ba0 65 73 65 20 68 61 73 68 20 74 61 62 6c 65 73 20  ese hash tables 
78bb0 6d 75 73 74 20 62 65 20 70 72 6f 74 65 63 74 65  must be protecte
78bc0 64 20 62 79 20 61 20 6d 75 74 65 78 2e 0a 2a 2f  d by a mutex..*/
78bd0 0a 73 74 61 74 69 63 20 48 61 73 68 20 6c 6f 63  .static Hash loc
78be0 6b 48 61 73 68 20 3d 20 7b 53 51 4c 49 54 45 5f  kHash = {SQLITE_
78bf0 48 41 53 48 5f 42 49 4e 41 52 59 2c 20 30 2c 20  HASH_BINARY, 0, 
78c00 30 2c 20 30 2c 20 0a 20 20 20 20 73 71 6c 69 74  0, 0, .    sqlit
78c10 65 33 54 68 72 65 61 64 53 61 66 65 4d 61 6c 6c  e3ThreadSafeMall
78c20 6f 63 2c 20 73 71 6c 69 74 65 33 54 68 72 65 61  oc, sqlite3Threa
78c30 64 53 61 66 65 46 72 65 65 2c 20 30 2c 20 30 7d  dSafeFree, 0, 0}
78c40 3b 0a 73 74 61 74 69 63 20 48 61 73 68 20 6f 70  ;.static Hash op
78c50 65 6e 48 61 73 68 20 3d 20 7b 53 51 4c 49 54 45  enHash = {SQLITE
78c60 5f 48 41 53 48 5f 42 49 4e 41 52 59 2c 20 30 2c  _HASH_BINARY, 0,
78c70 20 30 2c 20 30 2c 20 0a 20 20 20 20 73 71 6c 69   0, 0, .    sqli
78c80 74 65 33 54 68 72 65 61 64 53 61 66 65 4d 61 6c  te3ThreadSafeMal
78c90 6c 6f 63 2c 20 73 71 6c 69 74 65 33 54 68 72 65  loc, sqlite3Thre
78ca0 61 64 53 61 66 65 46 72 65 65 2c 20 30 2c 20 30  adSafeFree, 0, 0
78cb0 7d 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  };..#ifdef SQLIT
78cc0 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
78cd0 5f 53 54 59 4c 45 0a 2f 2a 0a 2a 2a 20 54 68 65  _STYLE./*.** The
78ce0 20 6c 6f 63 6b 69 6e 67 20 73 74 79 6c 65 73 20   locking styles 
78cf0 61 72 65 20 61 73 73 6f 63 69 61 74 65 64 20 77  are associated w
78d00 69 74 68 20 74 68 65 20 64 69 66 66 65 72 65 6e  ith the differen
78d10 74 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a 2a  t file locking.*
78d20 2a 20 63 61 70 61 62 69 6c 69 74 69 65 73 20 73  * capabilities s
78d30 75 70 70 6f 72 74 65 64 20 62 79 20 64 69 66 66  upported by diff
78d40 65 72 65 6e 74 20 66 69 6c 65 20 73 79 73 74 65  erent file syste
78d50 6d 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 50 4f 53 49  ms.  .**.** POSI
78d60 58 20 6c 6f 63 6b 69 6e 67 20 73 74 79 6c 65 20  X locking style 
78d70 66 75 6c 6c 79 20 73 75 70 70 6f 72 74 73 20 73  fully supports s
78d80 68 61 72 65 64 20 61 6e 64 20 65 78 63 6c 75 73  hared and exclus
78d90 69 76 65 20 62 79 74 65 2d 72 61 6e 67 65 20 6c  ive byte-range l
78da0 6f 63 6b 73 20 0a 2a 2a 20 41 44 50 20 6c 6f 63  ocks .** ADP loc
78db0 6b 69 6e 67 20 6f 6e 6c 79 20 73 75 70 70 6f 72  king only suppor
78dc0 74 73 20 65 78 63 6c 75 73 69 76 65 20 62 79 74  ts exclusive byt
78dd0 65 2d 72 61 6e 67 65 20 6c 6f 63 6b 73 0a 2a 2a  e-range locks.**
78de0 20 46 4c 4f 43 4b 20 6f 6e 6c 79 20 73 75 70 70   FLOCK only supp
78df0 6f 72 74 73 20 61 20 73 69 6e 67 6c 65 20 66 69  orts a single fi
78e00 6c 65 2d 67 6c 6f 62 61 6c 20 65 78 63 6c 75 73  le-global exclus
78e10 69 76 65 20 6c 6f 63 6b 0a 2a 2a 20 44 4f 54 4c  ive lock.** DOTL
78e20 4f 43 4b 20 69 73 6e 27 74 20 61 20 74 72 75 65  OCK isn't a true
78e30 20 6c 6f 63 6b 69 6e 67 20 73 74 79 6c 65 2c 20   locking style, 
78e40 69 74 20 72 65 66 65 72 73 20 74 6f 20 74 68 65  it refers to the
78e50 20 75 73 65 20 6f 66 20 61 20 73 70 65 63 69 61   use of a specia
78e60 6c 0a 2a 2a 20 20 20 66 69 6c 65 20 6e 61 6d 65  l.**   file name
78e70 64 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68  d the same as th
78e80 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
78e90 77 69 74 68 20 61 20 27 2e 6c 6f 63 6b 27 20 65  with a '.lock' e
78ea0 78 74 65 6e 73 69 6f 6e 2c 20 74 68 69 73 0a 2a  xtension, this.*
78eb0 2a 20 20 20 63 61 6e 20 62 65 20 75 73 65 64 20  *   can be used 
78ec0 6f 6e 20 66 69 6c 65 20 73 79 73 74 65 6d 73 20  on file systems 
78ed0 74 68 61 74 20 64 6f 20 6e 6f 74 20 6f 66 66 65  that do not offe
78ee0 72 20 61 6e 79 20 72 65 6c 69 61 62 6c 65 20 66  r any reliable f
78ef0 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 4e  ile locking.** N
78f00 4f 20 6c 6f 63 6b 69 6e 67 20 6d 65 61 6e 73 20  O locking means 
78f10 74 68 61 74 20 6e 6f 20 6c 6f 63 6b 69 6e 67 20  that no locking 
78f20 77 69 6c 6c 20 62 65 20 61 74 74 65 6d 70 74 65  will be attempte
78f30 64 2c 20 74 68 69 73 20 69 73 20 6f 6e 6c 79 20  d, this is only 
78f40 75 73 65 64 20 66 6f 72 0a 2a 2a 20 20 20 72 65  used for.**   re
78f50 61 64 2d 6f 6e 6c 79 20 66 69 6c 65 20 73 79 73  ad-only file sys
78f60 74 65 6d 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a  tems currently.*
78f70 2a 20 55 4e 53 55 50 50 4f 52 54 45 44 20 6d 65  * UNSUPPORTED me
78f80 61 6e 73 20 74 68 61 74 20 6e 6f 20 6c 6f 63 6b  ans that no lock
78f90 69 6e 67 20 77 69 6c 6c 20 62 65 20 61 74 74 65  ing will be atte
78fa0 6d 70 74 65 64 2c 20 74 68 69 73 20 69 73 20 6f  mpted, this is o
78fb0 6e 6c 79 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20  nly used for.** 
78fc0 20 20 66 69 6c 65 20 73 79 73 74 65 6d 73 20 74    file systems t
78fd0 68 61 74 20 61 72 65 20 6b 6e 6f 77 6e 20 74 6f  hat are known to
78fe0 20 62 65 20 75 6e 73 75 70 70 6f 72 74 65 64 0a   be unsupported.
78ff0 2a 2f 0a 74 79 70 65 64 65 66 20 65 6e 75 6d 20  */.typedef enum 
79000 7b 0a 09 70 6f 73 69 78 4c 6f 63 6b 69 6e 67 53  {..posixLockingS
79010 74 79 6c 65 20 3d 20 30 2c 20 20 20 20 20 20 20  tyle = 0,       
79020 2f 2a 20 73 74 61 6e 64 61 72 64 20 70 6f 73 69  /* standard posi
79030 78 2d 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73  x-advisory locks
79040 20 2a 2f 0a 09 61 66 70 4c 6f 63 6b 69 6e 67 53   */..afpLockingS
79050 74 79 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20  tyle,           
79060 20 20 2f 2a 20 75 73 65 20 61 66 70 20 6c 6f 63    /* use afp loc
79070 6b 73 20 2a 2f 0a 09 66 6c 6f 63 6b 4c 6f 63 6b  ks */..flockLock
79080 69 6e 67 53 74 79 6c 65 2c 20 20 20 20 20 20 20  ingStyle,       
79090 20 20 20 20 2f 2a 20 75 73 65 20 66 6c 6f 63 6b      /* use flock
790a0 28 29 20 2a 2f 0a 09 64 6f 74 6c 6f 63 6b 4c 6f  () */..dotlockLo
790b0 63 6b 69 6e 67 53 74 79 6c 65 2c 20 20 20 20 20  ckingStyle,     
790c0 20 20 20 20 2f 2a 20 75 73 65 20 3c 66 69 6c 65      /* use <file
790d0 3e 2e 6c 6f 63 6b 20 66 69 6c 65 73 20 2a 2f 0a  >.lock files */.
790e0 09 6e 6f 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 2c  .noLockingStyle,
790f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
79100 20 75 73 65 66 75 6c 20 66 6f 72 20 72 65 61 64   useful for read
79110 2d 6f 6e 6c 79 20 66 69 6c 65 20 73 79 73 74 65  -only file syste
79120 6d 20 2a 2f 0a 09 75 6e 73 75 70 70 6f 72 74 65  m */..unsupporte
79130 64 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 20 20  dLockingStyle   
79140 20 20 20 2f 2a 20 69 6e 64 69 63 61 74 65 73 20     /* indicates 
79150 75 6e 73 75 70 70 6f 72 74 65 64 20 66 69 6c 65  unsupported file
79160 20 73 79 73 74 65 6d 20 2a 2f 0a 7d 20 73 71 6c   system */.} sql
79170 69 74 65 33 4c 6f 63 6b 69 6e 67 53 74 79 6c 65  ite3LockingStyle
79180 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ;.#endif /* SQLI
79190 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
791a0 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a 23 69 66 64  G_STYLE */..#ifd
791b0 65 66 20 53 51 4c 49 54 45 5f 55 4e 49 58 5f 54  ef SQLITE_UNIX_T
791c0 48 52 45 41 44 53 0a 2f 2a 0a 2a 2a 20 54 68 69  HREADS./*.** Thi
791d0 73 20 76 61 72 69 61 62 6c 65 20 72 65 63 6f 72  s variable recor
791e0 64 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  ds whether or no
791f0 74 20 74 68 72 65 61 64 73 20 63 61 6e 20 6f 76  t threads can ov
79200 65 72 72 69 64 65 20 65 61 63 68 20 6f 74 68 65  erride each othe
79210 72 73 0a 2a 2a 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a  rs.** locks..**.
79220 2a 2a 20 20 20 20 30 3a 20 20 4e 6f 2e 20 20 54  **    0:  No.  T
79230 68 72 65 61 64 73 20 63 61 6e 6e 6f 74 20 6f 76  hreads cannot ov
79240 65 72 72 69 64 65 20 65 61 63 68 20 6f 74 68 65  erride each othe
79250 72 73 20 6c 6f 63 6b 73 2e 0a 2a 2a 20 20 20 20  rs locks..**    
79260 31 3a 20 20 59 65 73 2e 20 20 54 68 72 65 61 64  1:  Yes.  Thread
79270 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 65  s can override e
79280 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73  ach others locks
79290 2e 0a 2a 2a 20 20 20 2d 31 3a 20 20 57 65 20 64  ..**   -1:  We d
792a0 6f 6e 27 74 20 6b 6e 6f 77 20 79 65 74 2e 0a 2a  on't know yet..*
792b0 2a 0a 2a 2a 20 4f 6e 20 73 6f 6d 65 20 73 79 73  *.** On some sys
792c0 74 65 6d 73 2c 20 77 65 20 6b 6e 6f 77 20 61 74  tems, we know at
792d0 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 69 66   compile-time if
792e0 20 74 68 72 65 61 64 73 20 63 61 6e 20 6f 76 65   threads can ove
792f0 72 72 69 64 65 20 65 61 63 68 0a 2a 2a 20 6f 74  rride each.** ot
79300 68 65 72 73 20 6c 6f 63 6b 73 2e 20 20 4f 6e 20  hers locks.  On 
79310 74 68 6f 73 65 20 73 79 73 74 65 6d 73 2c 20 74  those systems, t
79320 68 65 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  he SQLITE_THREAD
79330 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43 4b 20 6d  _OVERRIDE_LOCK m
79340 61 63 72 6f 0a 2a 2a 20 77 69 6c 6c 20 62 65 20  acro.** will be 
79350 73 65 74 20 61 70 70 72 6f 70 72 69 61 74 65 6c  set appropriatel
79360 79 2e 20 20 4f 6e 20 6f 74 68 65 72 20 73 79 73  y.  On other sys
79370 74 65 6d 73 2c 20 77 65 20 68 61 76 65 20 74 6f  tems, we have to
79380 20 63 68 65 63 6b 20 61 74 0a 2a 2a 20 72 75 6e   check at.** run
79390 74 69 6d 65 2e 20 20 4f 6e 20 74 68 65 73 65 20  time.  On these 
793a0 6c 61 74 74 65 72 20 73 79 73 74 65 6d 73 2c 20  latter systems, 
793b0 53 51 4c 54 49 45 5f 54 48 52 45 41 44 5f 4f 56  SQLTIE_THREAD_OV
793c0 45 52 52 49 44 45 5f 4c 4f 43 4b 20 69 73 0a 2a  ERRIDE_LOCK is.*
793d0 2a 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a  * undefined..**.
793e0 2a 2a 20 54 68 69 73 20 76 61 72 69 61 62 6c 65  ** This variable
793f0 20 6e 6f 72 6d 61 6c 6c 79 20 68 61 73 20 66 69   normally has fi
79400 6c 65 20 73 63 6f 70 65 20 6f 6e 6c 79 2e 20 20  le scope only.  
79410 42 75 74 20 64 75 72 69 6e 67 20 74 65 73 74 69  But during testi
79420 6e 67 2c 20 77 65 20 6d 61 6b 65 0a 2a 2a 20 69  ng, we make.** i
79430 74 20 61 20 67 6c 6f 62 61 6c 20 73 6f 20 74 68  t a global so th
79440 61 74 20 74 68 65 20 74 65 73 74 20 63 6f 64 65  at the test code
79450 20 63 61 6e 20 63 68 61 6e 67 65 20 69 74 73 20   can change its 
79460 76 61 6c 75 65 20 69 6e 20 6f 72 64 65 72 20 74  value in order t
79470 6f 20 76 65 72 69 66 79 0a 2a 2a 20 74 68 61 74  o verify.** that
79480 20 74 68 65 20 72 69 67 68 74 20 73 74 75 66 66   the right stuff
79490 20 68 61 70 70 65 6e 73 20 69 6e 20 65 69 74 68   happens in eith
794a0 65 72 20 63 61 73 65 2e 0a 2a 2f 0a 23 69 66 6e  er case..*/.#ifn
794b0 64 65 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  def SQLITE_THREA
794c0 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43 4b 0a  D_OVERRIDE_LOCK.
794d0 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
794e0 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f  THREAD_OVERRIDE_
794f0 4c 4f 43 4b 20 2d 31 0a 23 65 6e 64 69 66 0a 23  LOCK -1.#endif.#
79500 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
79510 54 0a 69 6e 74 20 74 68 72 65 61 64 73 4f 76 65  T.int threadsOve
79520 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c  rrideEachOthersL
79530 6f 63 6b 73 20 3d 20 53 51 4c 49 54 45 5f 54 48  ocks = SQLITE_TH
79540 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f  READ_OVERRIDE_LO
79550 43 4b 3b 0a 23 65 6c 73 65 0a 73 74 61 74 69 63  CK;.#else.static
79560 20 69 6e 74 20 74 68 72 65 61 64 73 4f 76 65 72   int threadsOver
79570 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f  rideEachOthersLo
79580 63 6b 73 20 3d 20 53 51 4c 49 54 45 5f 54 48 52  cks = SQLITE_THR
79590 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43  EAD_OVERRIDE_LOC
795a0 4b 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  K;.#endif../*.**
795b0 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20   This structure 
795c0 68 6f 6c 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f  holds informatio
795d0 6e 20 70 61 73 73 65 64 20 69 6e 74 6f 20 69 6e  n passed into in
795e0 64 69 76 69 64 75 61 6c 20 74 65 73 74 0a 2a 2a  dividual test.**
795f0 20 74 68 72 65 61 64 73 20 62 79 20 74 68 65 20   threads by the 
79600 74 65 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e  testThreadLockin
79610 67 42 65 68 61 76 69 6f 72 28 29 20 72 6f 75 74  gBehavior() rout
79620 69 6e 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 74  ine..*/.struct t
79630 68 72 65 61 64 54 65 73 74 44 61 74 61 20 7b 0a  hreadTestData {.
79640 20 20 69 6e 74 20 66 64 3b 20 20 20 20 20 20 20    int fd;       
79650 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65           /* File
79660 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f   to be locked */
79670 0a 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20  .  struct flock 
79680 6c 6f 63 6b 3b 20 20 20 20 20 2f 2a 20 54 68 65  lock;     /* The
79690 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69   locking operati
796a0 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 75  on */.  int resu
796b0 6c 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  lt;            /
796c0 2a 20 52 65 73 75 6c 74 20 6f 66 20 74 68 65 20  * Result of the 
796d0 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f  locking operatio
796e0 6e 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 64 65 66 20  n */.};..#ifdef 
796f0 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 41 43  SQLITE_LOCK_TRAC
79700 45 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 6f 75  E./*.** Print ou
79710 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  t information ab
79720 6f 75 74 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20  out all locking 
79730 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a  operations..**.*
79740 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
79750 73 20 75 73 65 64 20 66 6f 72 20 74 72 6f 75 62  s used for troub
79760 6c 65 73 68 6f 6f 74 69 6e 67 20 6c 6f 63 6b 73  leshooting locks
79770 20 6f 6e 20 6d 75 6c 74 69 74 68 72 65 61 64 65   on multithreade
79780 64 0a 2a 2a 20 70 6c 61 74 66 6f 72 6d 73 2e 20  d.** platforms. 
79790 20 45 6e 61 62 6c 65 20 62 79 20 63 6f 6d 70 69   Enable by compi
797a0 6c 69 6e 67 20 77 69 74 68 20 74 68 65 20 2d 44  ling with the -D
797b0 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 41 43  SQLITE_LOCK_TRAC
797c0 45 0a 2a 2a 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e  E.** command-lin
797d0 65 20 6f 70 74 69 6f 6e 20 6f 6e 20 74 68 65 20  e option on the 
797e0 63 6f 6d 70 69 6c 65 72 2e 20 20 54 68 69 73 20  compiler.  This 
797f0 63 6f 64 65 20 69 73 20 6e 6f 72 6d 61 6c 6c 79  code is normally
79800 0a 2a 2a 20 74 75 72 6e 65 64 20 6f 66 66 2e 0a  .** turned off..
79810 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f  */.static int lo
79820 63 6b 54 72 61 63 65 28 69 6e 74 20 66 64 2c 20  ckTrace(int fd, 
79830 69 6e 74 20 6f 70 2c 20 73 74 72 75 63 74 20 66  int op, struct f
79840 6c 6f 63 6b 20 2a 70 29 7b 0a 20 20 63 68 61 72  lock *p){.  char
79850 20 2a 7a 4f 70 4e 61 6d 65 2c 20 2a 7a 54 79 70   *zOpName, *zTyp
79860 65 3b 0a 20 20 69 6e 74 20 73 3b 0a 20 20 69 6e  e;.  int s;.  in
79870 74 20 73 61 76 65 64 45 72 72 6e 6f 3b 0a 20 20  t savedErrno;.  
79880 69 66 28 20 6f 70 3d 3d 46 5f 47 45 54 4c 4b 20  if( op==F_GETLK 
79890 29 7b 0a 20 20 20 20 7a 4f 70 4e 61 6d 65 20 3d  ){.    zOpName =
798a0 20 22 47 45 54 4c 4b 22 3b 0a 20 20 7d 65 6c 73   "GETLK";.  }els
798b0 65 20 69 66 28 20 6f 70 3d 3d 46 5f 53 45 54 4c  e if( op==F_SETL
798c0 4b 20 29 7b 0a 20 20 20 20 7a 4f 70 4e 61 6d 65  K ){.    zOpName
798d0 20 3d 20 22 53 45 54 4c 4b 22 3b 0a 20 20 7d 65   = "SETLK";.  }e
798e0 6c 73 65 7b 0a 20 20 20 20 73 20 3d 20 66 63 6e  lse{.    s = fcn
798f0 74 6c 28 66 64 2c 20 6f 70 2c 20 70 29 3b 0a 20  tl(fd, op, p);. 
79900 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
79910 72 69 6e 74 66 28 22 66 63 6e 74 6c 20 75 6e 6b  rintf("fcntl unk
79920 6e 6f 77 6e 20 25 64 20 25 64 20 25 64 5c 6e 22  nown %d %d %d\n"
79930 2c 20 66 64 2c 20 6f 70 2c 20 73 29 3b 0a 20 20  , fd, op, s);.  
79940 20 20 72 65 74 75 72 6e 20 73 3b 0a 20 20 7d 0a    return s;.  }.
79950 20 20 69 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d    if( p->l_type=
79960 3d 46 5f 52 44 4c 43 4b 20 29 7b 0a 20 20 20 20  =F_RDLCK ){.    
79970 7a 54 79 70 65 20 3d 20 22 52 44 4c 43 4b 22 3b  zType = "RDLCK";
79980 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e  .  }else if( p->
79990 6c 5f 74 79 70 65 3d 3d 46 5f 57 52 4c 43 4b 20  l_type==F_WRLCK 
799a0 29 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 22  ){.    zType = "
799b0 57 52 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 20  WRLCK";.  }else 
799c0 69 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46  if( p->l_type==F
799d0 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 7a 54  _UNLCK ){.    zT
799e0 79 70 65 20 3d 20 22 55 4e 4c 43 4b 22 3b 0a 20  ype = "UNLCK";. 
799f0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
79a00 72 74 28 20 30 20 29 3b 0a 20 20 7d 0a 20 20 61  rt( 0 );.  }.  a
79a10 73 73 65 72 74 28 20 70 2d 3e 6c 5f 77 68 65 6e  ssert( p->l_when
79a20 63 65 3d 3d 53 45 45 4b 5f 53 45 54 20 29 3b 0a  ce==SEEK_SET );.
79a30 20 20 73 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20    s = fcntl(fd, 
79a40 6f 70 2c 20 70 29 3b 0a 20 20 73 61 76 65 64 45  op, p);.  savedE
79a50 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  rrno = errno;.  
79a60 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
79a70 74 66 28 22 66 63 6e 74 6c 20 25 64 20 25 64 20  tf("fcntl %d %d 
79a80 25 73 20 25 73 20 25 64 20 25 64 20 25 64 20 25  %s %s %d %d %d %
79a90 64 5c 6e 22 2c 0a 20 20 20 20 20 74 68 72 65 61  d\n",.     threa
79aa0 64 69 64 2c 20 66 64 2c 20 7a 4f 70 4e 61 6d 65  did, fd, zOpName
79ab0 2c 20 7a 54 79 70 65 2c 20 28 69 6e 74 29 70 2d  , zType, (int)p-
79ac0 3e 6c 5f 73 74 61 72 74 2c 20 28 69 6e 74 29 70  >l_start, (int)p
79ad0 2d 3e 6c 5f 6c 65 6e 2c 0a 20 20 20 20 20 28 69  ->l_len,.     (i
79ae0 6e 74 29 70 2d 3e 6c 5f 70 69 64 2c 20 73 29 3b  nt)p->l_pid, s);
79af0 0a 20 20 69 66 28 20 73 3d 3d 28 2d 31 29 20 26  .  if( s==(-1) &
79b00 26 20 6f 70 3d 3d 46 5f 53 45 54 4c 4b 20 26 26  & op==F_SETLK &&
79b10 20 28 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 52   (p->l_type==F_R
79b20 44 4c 43 4b 20 7c 7c 20 70 2d 3e 6c 5f 74 79 70  DLCK || p->l_typ
79b30 65 3d 3d 46 5f 57 52 4c 43 4b 29 20 29 7b 0a 20  e==F_WRLCK) ){. 
79b40 20 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20     struct flock 
79b50 6c 32 3b 0a 20 20 20 20 6c 32 20 3d 20 2a 70 3b  l2;.    l2 = *p;
79b60 0a 20 20 20 20 66 63 6e 74 6c 28 66 64 2c 20 46  .    fcntl(fd, F
79b70 5f 47 45 54 4c 4b 2c 20 26 6c 32 29 3b 0a 20 20  _GETLK, &l2);.  
79b80 20 20 69 66 28 20 6c 32 2e 6c 5f 74 79 70 65 3d    if( l2.l_type=
79b90 3d 46 5f 52 44 4c 43 4b 20 29 7b 0a 20 20 20 20  =F_RDLCK ){.    
79ba0 20 20 7a 54 79 70 65 20 3d 20 22 52 44 4c 43 4b    zType = "RDLCK
79bb0 22 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  ";.    }else if(
79bc0 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f 57 52   l2.l_type==F_WR
79bd0 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 7a 54 79  LCK ){.      zTy
79be0 70 65 20 3d 20 22 57 52 4c 43 4b 22 3b 0a 20 20  pe = "WRLCK";.  
79bf0 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 32 2e 6c    }else if( l2.l
79c00 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 20 29  _type==F_UNLCK )
79c10 7b 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20  {.      zType = 
79c20 22 55 4e 4c 43 4b 22 3b 0a 20 20 20 20 7d 65 6c  "UNLCK";.    }el
79c30 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  se{.      assert
79c40 28 20 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20  ( 0 );.    }.   
79c50 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
79c60 6e 74 66 28 22 66 63 6e 74 6c 2d 66 61 69 6c 75  ntf("fcntl-failu
79c70 72 65 2d 72 65 61 73 6f 6e 3a 20 25 73 20 25 64  re-reason: %s %d
79c80 20 25 64 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20   %d %d\n",.     
79c90 20 20 7a 54 79 70 65 2c 20 28 69 6e 74 29 6c 32    zType, (int)l2
79ca0 2e 6c 5f 73 74 61 72 74 2c 20 28 69 6e 74 29 6c  .l_start, (int)l
79cb0 32 2e 6c 5f 6c 65 6e 2c 20 28 69 6e 74 29 6c 32  2.l_len, (int)l2
79cc0 2e 6c 5f 70 69 64 29 3b 0a 20 20 7d 0a 20 20 65  .l_pid);.  }.  e
79cd0 72 72 6e 6f 20 3d 20 73 61 76 65 64 45 72 72 6e  rrno = savedErrn
79ce0 6f 3b 0a 20 20 72 65 74 75 72 6e 20 73 3b 0a 7d  o;.  return s;.}
79cf0 0a 23 64 65 66 69 6e 65 20 66 63 6e 74 6c 20 6c  .#define fcntl l
79d00 6f 63 6b 54 72 61 63 65 0a 23 65 6e 64 69 66 20  ockTrace.#endif 
79d10 2f 2a 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54  /* SQLITE_LOCK_T
79d20 52 41 43 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  RACE */../*.** T
79d30 68 65 20 74 65 73 74 54 68 72 65 61 64 4c 6f 63  he testThreadLoc
79d40 6b 69 6e 67 42 65 68 61 76 69 6f 72 28 29 20 72  kingBehavior() r
79d50 6f 75 74 69 6e 65 20 6c 61 75 6e 63 68 65 73 20  outine launches 
79d60 74 77 6f 20 73 65 70 61 72 61 74 65 0a 2a 2a 20  two separate.** 
79d70 74 68 72 65 61 64 73 20 6f 6e 20 74 68 69 73 20  threads on this 
79d80 72 6f 75 74 69 6e 65 2e 20 20 54 68 69 73 20 72  routine.  This r
79d90 6f 75 74 69 6e 65 20 61 74 74 65 6d 70 74 73 20  outine attempts 
79da0 74 6f 20 6c 6f 63 6b 20 61 20 66 69 6c 65 0a 2a  to lock a file.*
79db0 2a 20 64 65 73 63 72 69 70 74 6f 72 20 74 68 65  * descriptor the
79dc0 6e 20 72 65 74 75 72 6e 73 2e 20 20 54 68 65 20  n returns.  The 
79dd0 73 75 63 63 65 73 73 20 6f 72 20 66 61 69 6c 75  success or failu
79de0 72 65 20 6f 66 20 74 68 61 74 20 61 74 74 65 6d  re of that attem
79df0 70 74 0a 2a 2a 20 61 6c 6c 6f 77 73 20 74 68 65  pt.** allows the
79e00 20 74 65 73 74 54 68 72 65 61 64 4c 6f 63 6b 69   testThreadLocki
79e10 6e 67 42 65 68 61 76 69 6f 72 28 29 20 70 72 6f  ngBehavior() pro
79e20 63 65 64 75 72 65 20 74 6f 20 64 65 74 65 72 6d  cedure to determ
79e30 69 6e 65 0a 2a 2a 20 77 68 65 74 68 65 72 20 6f  ine.** whether o
79e40 72 20 6e 6f 74 20 74 68 72 65 61 64 73 20 63 61  r not threads ca
79e50 6e 20 6f 76 65 72 72 69 64 65 20 65 61 63 68 20  n override each 
79e60 6f 74 68 65 72 73 20 6c 6f 63 6b 73 2e 0a 2a 2f  others locks..*/
79e70 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 74 68  .static void *th
79e80 72 65 61 64 4c 6f 63 6b 69 6e 67 54 65 73 74 28  readLockingTest(
79e90 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 73  void *pArg){.  s
79ea0 74 72 75 63 74 20 74 68 72 65 61 64 54 65 73 74  truct threadTest
79eb0 44 61 74 61 20 2a 70 44 61 74 61 20 3d 20 28 73  Data *pData = (s
79ec0 74 72 75 63 74 20 74 68 72 65 61 64 54 65 73 74  truct threadTest
79ed0 44 61 74 61 2a 29 70 41 72 67 3b 0a 20 20 70 44  Data*)pArg;.  pD
79ee0 61 74 61 2d 3e 72 65 73 75 6c 74 20 3d 20 66 63  ata->result = fc
79ef0 6e 74 6c 28 70 44 61 74 61 2d 3e 66 64 2c 20 46  ntl(pData->fd, F
79f00 5f 53 45 54 4c 4b 2c 20 26 70 44 61 74 61 2d 3e  _SETLK, &pData->
79f10 6c 6f 63 6b 29 3b 0a 20 20 72 65 74 75 72 6e 20  lock);.  return 
79f20 70 41 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  pArg;.}../*.** T
79f30 68 69 73 20 70 72 6f 63 65 64 75 72 65 20 61 74  his procedure at
79f40 74 65 6d 70 74 73 20 74 6f 20 64 65 74 65 72 6d  tempts to determ
79f50 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e  ine whether or n
79f60 6f 74 20 74 68 72 65 61 64 73 0a 2a 2a 20 63 61  ot threads.** ca
79f70 6e 20 6f 76 65 72 72 69 64 65 20 65 61 63 68 20  n override each 
79f80 6f 74 68 65 72 73 20 6c 6f 63 6b 73 20 74 68 65  others locks the
79f90 6e 20 73 65 74 73 20 74 68 65 20 0a 2a 2a 20 74  n sets the .** t
79fa0 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45 61  hreadsOverrideEa
79fb0 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 20 76 61  chOthersLocks va
79fc0 72 69 61 62 6c 65 20 61 70 70 72 6f 70 72 69 61  riable appropria
79fd0 74 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tely..*/.static 
79fe0 76 6f 69 64 20 74 65 73 74 54 68 72 65 61 64 4c  void testThreadL
79ff0 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 28 69  ockingBehavior(i
7a000 6e 74 20 66 64 5f 6f 72 69 67 29 7b 0a 20 20 69  nt fd_orig){.  i
7a010 6e 74 20 66 64 3b 0a 20 20 73 74 72 75 63 74 20  nt fd;.  struct 
7a020 74 68 72 65 61 64 54 65 73 74 44 61 74 61 20 64  threadTestData d
7a030 5b 32 5d 3b 0a 20 20 70 74 68 72 65 61 64 5f 74  [2];.  pthread_t
7a040 20 74 5b 32 5d 3b 0a 0a 20 20 66 64 20 3d 20 64   t[2];..  fd = d
7a050 75 70 28 66 64 5f 6f 72 69 67 29 3b 0a 20 20 69  up(fd_orig);.  i
7a060 66 28 20 66 64 3c 30 20 29 20 72 65 74 75 72 6e  f( fd<0 ) return
7a070 3b 0a 20 20 6d 65 6d 73 65 74 28 64 2c 20 30 2c  ;.  memset(d, 0,
7a080 20 73 69 7a 65 6f 66 28 64 29 29 3b 0a 20 20 64   sizeof(d));.  d
7a090 5b 30 5d 2e 66 64 20 3d 20 66 64 3b 0a 20 20 64  [0].fd = fd;.  d
7a0a0 5b 30 5d 2e 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20  [0].lock.l_type 
7a0b0 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 64 5b 30  = F_RDLCK;.  d[0
7a0c0 5d 2e 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31  ].lock.l_len = 1
7a0d0 3b 0a 20 20 64 5b 30 5d 2e 6c 6f 63 6b 2e 6c 5f  ;.  d[0].lock.l_
7a0e0 73 74 61 72 74 20 3d 20 30 3b 0a 20 20 64 5b 30  start = 0;.  d[0
7a0f0 5d 2e 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20  ].lock.l_whence 
7a100 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 64 5b  = SEEK_SET;.  d[
7a110 31 5d 20 3d 20 64 5b 30 5d 3b 0a 20 20 64 5b 31  1] = d[0];.  d[1
7a120 5d 2e 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20  ].lock.l_type = 
7a130 46 5f 57 52 4c 43 4b 3b 0a 20 20 70 74 68 72 65  F_WRLCK;.  pthre
7a140 61 64 5f 63 72 65 61 74 65 28 26 74 5b 30 5d 2c  ad_create(&t[0],
7a150 20 30 2c 20 74 68 72 65 61 64 4c 6f 63 6b 69 6e   0, threadLockin
7a160 67 54 65 73 74 2c 20 26 64 5b 30 5d 29 3b 0a 20  gTest, &d[0]);. 
7a170 20 70 74 68 72 65 61 64 5f 63 72 65 61 74 65 28   pthread_create(
7a180 26 74 5b 31 5d 2c 20 30 2c 20 74 68 72 65 61 64  &t[1], 0, thread
7a190 4c 6f 63 6b 69 6e 67 54 65 73 74 2c 20 26 64 5b  LockingTest, &d[
7a1a0 31 5d 29 3b 0a 20 20 70 74 68 72 65 61 64 5f 6a  1]);.  pthread_j
7a1b0 6f 69 6e 28 74 5b 30 5d 2c 20 30 29 3b 0a 20 20  oin(t[0], 0);.  
7a1c0 70 74 68 72 65 61 64 5f 6a 6f 69 6e 28 74 5b 31  pthread_join(t[1
7a1d0 5d 2c 20 30 29 3b 0a 20 20 63 6c 6f 73 65 28 66  ], 0);.  close(f
7a1e0 64 29 3b 0a 20 20 74 68 72 65 61 64 73 4f 76 65  d);.  threadsOve
7a1f0 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c  rrideEachOthersL
7a200 6f 63 6b 73 20 3d 20 20 64 5b 30 5d 2e 72 65 73  ocks =  d[0].res
7a210 75 6c 74 3d 3d 30 20 26 26 20 64 5b 31 5d 2e 72  ult==0 && d[1].r
7a220 65 73 75 6c 74 3d 3d 30 3b 0a 7d 0a 23 65 6e 64  esult==0;.}.#end
7a230 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 55 4e 49  if /* SQLITE_UNI
7a240 58 5f 54 48 52 45 41 44 53 20 2a 2f 0a 0a 2f 2a  X_THREADS */../*
7a250 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 6c 6f  .** Release a lo
7a260 63 6b 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  ckInfo structure
7a270 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f   previously allo
7a280 63 61 74 65 64 20 62 79 20 66 69 6e 64 4c 6f 63  cated by findLoc
7a290 6b 49 6e 66 6f 28 29 2e 0a 2a 2f 0a 73 74 61 74  kInfo()..*/.stat
7a2a0 69 63 20 76 6f 69 64 20 72 65 6c 65 61 73 65 4c  ic void releaseL
7a2b0 6f 63 6b 49 6e 66 6f 28 73 74 72 75 63 74 20 6c  ockInfo(struct l
7a2c0 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 29 7b  ockInfo *pLock){
7a2d0 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
7a2e0 65 33 4f 73 49 6e 4d 75 74 65 78 28 31 29 20 29  e3OsInMutex(1) )
7a2f0 3b 0a 20 20 69 66 20 28 70 4c 6f 63 6b 20 3d 3d  ;.  if (pLock ==
7a300 20 4e 55 4c 4c 29 0a 20 20 20 20 72 65 74 75 72   NULL).    retur
7a310 6e 3b 0a 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65 66  n;.  pLock->nRef
7a320 2d 2d 3b 0a 20 20 69 66 28 20 70 4c 6f 63 6b 2d  --;.  if( pLock-
7a330 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20  >nRef==0 ){.    
7a340 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72  sqlite3HashInser
7a350 74 28 26 6c 6f 63 6b 48 61 73 68 2c 20 26 70 4c  t(&lockHash, &pL
7a360 6f 63 6b 2d 3e 6b 65 79 2c 20 73 69 7a 65 6f 66  ock->key, sizeof
7a370 28 70 4c 6f 63 6b 2d 3e 6b 65 79 29 2c 20 30 29  (pLock->key), 0)
7a380 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 54 68 72  ;.    sqlite3Thr
7a390 65 61 64 53 61 66 65 46 72 65 65 28 70 4c 6f 63  eadSafeFree(pLoc
7a3a0 6b 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  k);.  }.}../*.**
7a3b0 20 52 65 6c 65 61 73 65 20 61 20 6f 70 65 6e 43   Release a openC
7a3c0 6e 74 20 73 74 72 75 63 74 75 72 65 20 70 72 65  nt structure pre
7a3d0 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65  viously allocate
7a3e0 64 20 62 79 20 66 69 6e 64 4c 6f 63 6b 49 6e 66  d by findLockInf
7a3f0 6f 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  o()..*/.static v
7a400 6f 69 64 20 72 65 6c 65 61 73 65 4f 70 65 6e 43  oid releaseOpenC
7a410 6e 74 28 73 74 72 75 63 74 20 6f 70 65 6e 43 6e  nt(struct openCn
7a420 74 20 2a 70 4f 70 65 6e 29 7b 0a 20 20 61 73 73  t *pOpen){.  ass
7a430 65 72 74 28 20 73 71 6c 69 74 65 33 4f 73 49 6e  ert( sqlite3OsIn
7a440 4d 75 74 65 78 28 31 29 20 29 3b 0a 20 20 69 66  Mutex(1) );.  if
7a450 20 28 70 4f 70 65 6e 20 3d 3d 20 4e 55 4c 4c 29   (pOpen == NULL)
7a460 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 70  .    return;.  p
7a470 4f 70 65 6e 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20  Open->nRef--;.  
7a480 69 66 28 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 3d  if( pOpen->nRef=
7a490 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
7a4a0 33 48 61 73 68 49 6e 73 65 72 74 28 26 6f 70 65  3HashInsert(&ope
7a4b0 6e 48 61 73 68 2c 20 26 70 4f 70 65 6e 2d 3e 6b  nHash, &pOpen->k
7a4c0 65 79 2c 20 73 69 7a 65 6f 66 28 70 4f 70 65 6e  ey, sizeof(pOpen
7a4d0 2d 3e 6b 65 79 29 2c 20 30 29 3b 0a 20 20 20 20  ->key), 0);.    
7a4e0 66 72 65 65 28 70 4f 70 65 6e 2d 3e 61 50 65 6e  free(pOpen->aPen
7a4f0 64 69 6e 67 29 3b 0a 20 20 20 20 73 71 6c 69 74  ding);.    sqlit
7a500 65 33 54 68 72 65 61 64 53 61 66 65 46 72 65 65  e3ThreadSafeFree
7a510 28 70 4f 70 65 6e 29 3b 0a 20 20 7d 0a 7d 0a 0a  (pOpen);.  }.}..
7a520 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
7a530 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
7a540 4c 45 0a 2f 2a 0a 2a 2a 20 54 65 73 74 73 20 61  LE./*.** Tests a
7a550 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b   byte-range lock
7a560 69 6e 67 20 71 75 65 72 79 20 74 6f 20 73 65 65  ing query to see
7a570 20 69 66 20 62 79 74 65 20 72 61 6e 67 65 20 6c   if byte range l
7a580 6f 63 6b 73 20 61 72 65 20 0a 2a 2a 20 73 75 70  ocks are .** sup
7a590 70 6f 72 74 65 64 2c 20 69 66 20 6e 6f 74 20 77  ported, if not w
7a5a0 65 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 64  e fall back to d
7a5b0 6f 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 53 74 79  otlockLockingSty
7a5c0 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71  le..*/.static sq
7a5d0 6c 69 74 65 33 4c 6f 63 6b 69 6e 67 53 74 79 6c  lite3LockingStyl
7a5e0 65 20 73 71 6c 69 74 65 33 54 65 73 74 4c 6f 63  e sqlite3TestLoc
7a5f0 6b 69 6e 67 53 74 79 6c 65 28 63 6f 6e 73 74 20  kingStyle(const 
7a600 63 68 61 72 20 2a 66 69 6c 65 50 61 74 68 2c 20  char *filePath, 
7a610 0a 20 20 69 6e 74 20 66 64 29 20 7b 0a 20 20 2f  .  int fd) {.  /
7a620 2a 20 74 65 73 74 20 62 79 74 65 2d 72 61 6e 67  * test byte-rang
7a630 65 20 6c 6f 63 6b 20 75 73 69 6e 67 20 66 63 6e  e lock using fcn
7a640 74 6c 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 66  tl */.  struct f
7a650 6c 6f 63 6b 20 6c 6f 63 6b 49 6e 66 6f 3b 0a 20  lock lockInfo;. 
7a660 20 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 6c   .  lockInfo.l_l
7a670 65 6e 20 3d 20 31 3b 0a 20 20 6c 6f 63 6b 49 6e  en = 1;.  lockIn
7a680 66 6f 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b 0a  fo.l_start = 0;.
7a690 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 77 68 65    lockInfo.l_whe
7a6a0 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a  nce = SEEK_SET;.
7a6b0 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 74 79 70    lockInfo.l_typ
7a6c0 65 20 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 0a  e = F_RDLCK;.  .
7a6d0 20 20 69 66 20 28 66 63 6e 74 6c 28 66 64 2c 20    if (fcntl(fd, 
7a6e0 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b 49 6e  F_GETLK, &lockIn
7a6f0 66 6f 29 20 21 3d 20 2d 31 29 20 7b 0a 20 20 20  fo) != -1) {.   
7a700 20 72 65 74 75 72 6e 20 70 6f 73 69 78 4c 6f 63   return posixLoc
7a710 6b 69 6e 67 53 74 79 6c 65 3b 0a 20 20 7d 20 0a  kingStyle;.  } .
7a720 20 20 0a 20 20 2f 2a 20 74 65 73 74 69 6e 67 20    .  /* testing 
7a730 66 6f 72 20 66 6c 6f 63 6b 20 63 61 6e 20 67 69  for flock can gi
7a740 76 65 20 66 61 6c 73 65 20 70 6f 73 69 74 69 76  ve false positiv
7a750 65 73 2e 20 20 53 6f 20 69 66 20 69 66 20 74 68  es.  So if if th
7a760 65 20 61 62 6f 76 65 20 74 65 73 74 0a 20 20 2a  e above test.  *
7a770 2a 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 77 65  * fails, then we
7a780 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 75 73   fall back to us
7a790 69 6e 67 20 64 6f 74 2d 6c 6f 63 6b 20 73 74 79  ing dot-lock sty
7a7a0 6c 65 20 6c 6f 63 6b 69 6e 67 2e 0a 20 20 2a 2f  le locking..  */
7a7b0 20 20 0a 20 20 72 65 74 75 72 6e 20 64 6f 74 6c    .  return dotl
7a7c0 6f 63 6b 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 3b  ockLockingStyle;
7a7d0 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 45 78 61 6d 69  .}../* .** Exami
7a7e0 6e 65 73 20 74 68 65 20 66 5f 66 73 74 79 70 65  nes the f_fstype
7a7f0 6e 61 6d 65 20 65 6e 74 72 79 20 69 6e 20 74 68  name entry in th
7a800 65 20 73 74 61 74 66 73 20 73 74 72 75 63 74 75  e statfs structu
7a810 72 65 20 61 73 20 72 65 74 75 72 6e 65 64 20 62  re as returned b
7a820 79 20 0a 2a 2a 20 73 74 61 74 28 29 20 66 6f 72  y .** stat() for
7a830 20 74 68 65 20 66 69 6c 65 20 73 79 73 74 65 6d   the file system
7a840 20 68 6f 73 74 69 6e 67 20 74 68 65 20 64 61 74   hosting the dat
7a850 61 62 61 73 65 20 66 69 6c 65 2c 20 61 73 73 69  abase file, assi
7a860 67 6e 73 20 74 68 65 20 0a 2a 2a 20 61 70 70 72  gns the .** appr
7a870 6f 70 72 69 61 74 65 20 6c 6f 63 6b 69 6e 67 20  opriate locking 
7a880 73 74 79 6c 65 20 62 61 73 65 64 20 6f 6e 20 69  style based on i
7a890 74 27 73 20 76 61 6c 75 65 2e 20 20 54 68 65 73  t's value.  Thes
7a8a0 65 20 76 61 6c 75 65 73 20 61 6e 64 20 0a 2a 2a  e values and .**
7a8b0 20 61 73 73 69 67 6e 6d 65 6e 74 73 20 61 72 65   assignments are
7a8c0 20 62 61 73 65 64 20 6f 6e 20 44 61 72 77 69 6e   based on Darwin
7a8d0 2f 4f 53 58 20 62 65 68 61 76 69 6f 72 20 61 6e  /OSX behavior an
7a8e0 64 20 68 61 76 65 20 6e 6f 74 20 62 65 65 6e 20  d have not been 
7a8f0 74 65 73 74 65 64 20 6f 6e 20 0a 2a 2a 20 6f 74  tested on .** ot
7a900 68 65 72 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a  her systems..*/.
7a910 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 4c 6f  static sqlite3Lo
7a920 63 6b 69 6e 67 53 74 79 6c 65 20 73 71 6c 69 74  ckingStyle sqlit
7a930 65 33 44 65 74 65 63 74 4c 6f 63 6b 69 6e 67 53  e3DetectLockingS
7a940 74 79 6c 65 28 63 6f 6e 73 74 20 63 68 61 72 20  tyle(const char 
7a950 2a 66 69 6c 65 50 61 74 68 2c 20 0a 20 20 69 6e  *filePath, .  in
7a960 74 20 66 64 29 20 7b 0a 0a 23 69 66 64 65 66 20  t fd) {..#ifdef 
7a970 53 51 4c 49 54 45 5f 46 49 58 45 44 5f 4c 4f 43  SQLITE_FIXED_LOC
7a980 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 72 65 74  KING_STYLE.  ret
7a990 75 72 6e 20 28 73 71 6c 69 74 65 33 4c 6f 63 6b  urn (sqlite3Lock
7a9a0 69 6e 67 53 74 79 6c 65 29 53 51 4c 49 54 45 5f  ingStyle)SQLITE_
7a9b0 46 49 58 45 44 5f 4c 4f 43 4b 49 4e 47 5f 53 54  FIXED_LOCKING_ST
7a9c0 59 4c 45 3b 0a 23 65 6c 73 65 0a 20 20 73 74 72  YLE;.#else.  str
7a9d0 75 63 74 20 73 74 61 74 66 73 20 66 73 49 6e 66  uct statfs fsInf
7a9e0 6f 3b 0a 0a 20 20 69 66 20 28 73 74 61 74 66 73  o;..  if (statfs
7a9f0 28 66 69 6c 65 50 61 74 68 2c 20 26 66 73 49 6e  (filePath, &fsIn
7aa00 66 6f 29 20 3d 3d 20 2d 31 29 0a 20 20 20 20 72  fo) == -1).    r
7aa10 65 74 75 72 6e 20 73 71 6c 69 74 65 33 54 65 73  eturn sqlite3Tes
7aa20 74 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 28 66 69  tLockingStyle(fi
7aa30 6c 65 50 61 74 68 2c 20 66 64 29 3b 0a 20 20 0a  lePath, fd);.  .
7aa40 20 20 69 66 20 28 66 73 49 6e 66 6f 2e 66 5f 66    if (fsInfo.f_f
7aa50 6c 61 67 73 20 26 20 4d 4e 54 5f 52 44 4f 4e 4c  lags & MNT_RDONL
7aa60 59 29 0a 20 20 20 20 72 65 74 75 72 6e 20 6e 6f  Y).    return no
7aa70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 3b 0a 20 20  LockingStyle;.  
7aa80 0a 20 20 69 66 28 20 28 21 73 74 72 63 6d 70 28  .  if( (!strcmp(
7aa90 66 73 49 6e 66 6f 2e 66 5f 66 73 74 79 70 65 6e  fsInfo.f_fstypen
7aaa0 61 6d 65 2c 20 22 68 66 73 22 29 29 20 7c 7c 0a  ame, "hfs")) ||.
7aab0 20 20 20 20 28 21 73 74 72 63 6d 70 28 66 73 49      (!strcmp(fsI
7aac0 6e 66 6f 2e 66 5f 66 73 74 79 70 65 6e 61 6d 65  nfo.f_fstypename
7aad0 2c 20 22 75 66 73 22 29 29 20 29 0a 09 09 72 65  , "ufs")) )...re
7aae0 74 75 72 6e 20 70 6f 73 69 78 4c 6f 63 6b 69 6e  turn posixLockin
7aaf0 67 53 74 79 6c 65 3b 0a 20 20 0a 20 20 69 66 28  gStyle;.  .  if(
7ab00 21 73 74 72 63 6d 70 28 66 73 49 6e 66 6f 2e 66  !strcmp(fsInfo.f
7ab10 5f 66 73 74 79 70 65 6e 61 6d 65 2c 20 22 61 66  _fstypename, "af
7ab20 70 66 73 22 29 29 0a 20 20 20 20 72 65 74 75 72  pfs")).    retur
7ab30 6e 20 61 66 70 4c 6f 63 6b 69 6e 67 53 74 79 6c  n afpLockingStyl
7ab40 65 3b 0a 20 20 0a 20 20 69 66 28 21 73 74 72 63  e;.  .  if(!strc
7ab50 6d 70 28 66 73 49 6e 66 6f 2e 66 5f 66 73 74 79  mp(fsInfo.f_fsty
7ab60 70 65 6e 61 6d 65 2c 20 22 6e 66 73 22 29 29 20  pename, "nfs")) 
7ab70 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69  .    return sqli
7ab80 74 65 33 54 65 73 74 4c 6f 63 6b 69 6e 67 53 74  te3TestLockingSt
7ab90 79 6c 65 28 66 69 6c 65 50 61 74 68 2c 20 66 64  yle(filePath, fd
7aba0 29 3b 0a 20 20 0a 20 20 69 66 28 21 73 74 72 63  );.  .  if(!strc
7abb0 6d 70 28 66 73 49 6e 66 6f 2e 66 5f 66 73 74 79  mp(fsInfo.f_fsty
7abc0 70 65 6e 61 6d 65 2c 20 22 73 6d 62 66 73 22 29  pename, "smbfs")
7abd0 29 0a 20 20 20 20 72 65 74 75 72 6e 20 66 6c 6f  ).    return flo
7abe0 63 6b 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 3b 0a  ckLockingStyle;.
7abf0 20 20 0a 20 20 69 66 28 21 73 74 72 63 6d 70 28    .  if(!strcmp(
7ac00 66 73 49 6e 66 6f 2e 66 5f 66 73 74 79 70 65 6e  fsInfo.f_fstypen
7ac10 61 6d 65 2c 20 22 6d 73 64 6f 73 22 29 29 0a 20  ame, "msdos")). 
7ac20 20 20 20 72 65 74 75 72 6e 20 64 6f 74 6c 6f 63     return dotloc
7ac30 6b 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 3b 0a 20  kLockingStyle;. 
7ac40 20 0a 20 20 69 66 28 21 73 74 72 63 6d 70 28 66   .  if(!strcmp(f
7ac50 73 49 6e 66 6f 2e 66 5f 66 73 74 79 70 65 6e 61  sInfo.f_fstypena
7ac60 6d 65 2c 20 22 77 65 62 64 61 76 22 29 29 0a 20  me, "webdav")). 
7ac70 20 20 20 72 65 74 75 72 6e 20 75 6e 73 75 70 70     return unsupp
7ac80 6f 72 74 65 64 4c 6f 63 6b 69 6e 67 53 74 79 6c  ortedLockingStyl
7ac90 65 3b 0a 20 20 0a 20 20 72 65 74 75 72 6e 20 73  e;.  .  return s
7aca0 71 6c 69 74 65 33 54 65 73 74 4c 6f 63 6b 69 6e  qlite3TestLockin
7acb0 67 53 74 79 6c 65 28 66 69 6c 65 50 61 74 68 2c  gStyle(filePath,
7acc0 20 66 64 29 3b 20 20 0a 23 65 6e 64 69 66 20 2f   fd);  .#endif /
7acd0 2a 20 53 51 4c 49 54 45 5f 46 49 58 45 44 5f 4c  * SQLITE_FIXED_L
7ace0 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a  OCKING_STYLE */.
7acf0 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  }..#endif /* SQL
7ad00 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
7ad10 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a  NG_STYLE */../*.
7ad20 2a 2a 20 47 69 76 65 6e 20 61 20 66 69 6c 65 20  ** Given a file 
7ad30 64 65 73 63 72 69 70 74 6f 72 2c 20 6c 6f 63 61  descriptor, loca
7ad40 74 65 20 6c 6f 63 6b 49 6e 66 6f 20 61 6e 64 20  te lockInfo and 
7ad50 6f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72  openCnt structur
7ad60 65 73 20 74 68 61 74 0a 2a 2a 20 64 65 73 63 72  es that.** descr
7ad70 69 62 65 73 20 74 68 61 74 20 66 69 6c 65 20 64  ibes that file d
7ad80 65 73 63 72 69 70 74 6f 72 2e 20 20 43 72 65 61  escriptor.  Crea
7ad90 74 65 20 6e 65 77 20 6f 6e 65 73 20 69 66 20 6e  te new ones if n
7ada0 65 63 65 73 73 61 72 79 2e 20 20 54 68 65 0a 2a  ecessary.  The.*
7adb0 2a 20 72 65 74 75 72 6e 20 76 61 6c 75 65 73 20  * return values 
7adc0 6d 69 67 68 74 20 62 65 20 75 6e 69 6e 69 74 69  might be uniniti
7add0 61 6c 69 7a 65 64 20 69 66 20 61 6e 20 65 72 72  alized if an err
7ade0 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a  or occurs..**.**
7adf0 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
7ae00 65 72 20 6f 66 20 65 72 72 6f 72 73 2e 0a 2a 2f  er of errors..*/
7ae10 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6e 64  .static int find
7ae20 4c 6f 63 6b 49 6e 66 6f 28 0a 20 20 69 6e 74 20  LockInfo(.  int 
7ae30 66 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  fd,             
7ae40 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
7ae50 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
7ae60 75 73 65 64 20 69 6e 20 74 68 65 20 6b 65 79 20  used in the key 
7ae70 2a 2f 0a 20 20 73 74 72 75 63 74 20 6c 6f 63 6b  */.  struct lock
7ae80 49 6e 66 6f 20 2a 2a 70 70 4c 6f 63 6b 2c 20 20  Info **ppLock,  
7ae90 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20    /* Return the 
7aea0 6c 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63 74 75  lockInfo structu
7aeb0 72 65 20 68 65 72 65 20 2a 2f 0a 20 20 73 74 72  re here */.  str
7aec0 75 63 74 20 6f 70 65 6e 43 6e 74 20 2a 2a 70 70  uct openCnt **pp
7aed0 4f 70 65 6e 20 20 20 20 20 20 2f 2a 20 52 65 74  Open      /* Ret
7aee0 75 72 6e 20 74 68 65 20 6f 70 65 6e 43 6e 74 20  urn the openCnt 
7aef0 73 74 72 75 63 74 75 72 65 20 68 65 72 65 20 2a  structure here *
7af00 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  /.){.  int rc;. 
7af10 20 73 74 72 75 63 74 20 6c 6f 63 6b 4b 65 79 20   struct lockKey 
7af20 6b 65 79 31 3b 0a 20 20 73 74 72 75 63 74 20 6f  key1;.  struct o
7af30 70 65 6e 4b 65 79 20 6b 65 79 32 3b 0a 20 20 73  penKey key2;.  s
7af40 74 72 75 63 74 20 73 74 61 74 20 73 74 61 74 62  truct stat statb
7af50 75 66 3b 0a 20 20 73 74 72 75 63 74 20 6c 6f 63  uf;.  struct loc
7af60 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 3b 0a 20 20  kInfo *pLock;.  
7af70 73 74 72 75 63 74 20 6f 70 65 6e 43 6e 74 20 2a  struct openCnt *
7af80 70 4f 70 65 6e 3b 0a 20 20 72 63 20 3d 20 66 73  pOpen;.  rc = fs
7af90 74 61 74 28 66 64 2c 20 26 73 74 61 74 62 75 66  tat(fd, &statbuf
7afa0 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 30 20 29  );.  if( rc!=0 )
7afb0 20 72 65 74 75 72 6e 20 31 3b 0a 0a 20 20 61 73   return 1;..  as
7afc0 73 65 72 74 28 20 73 71 6c 69 74 65 33 4f 73 49  sert( sqlite3OsI
7afd0 6e 4d 75 74 65 78 28 31 29 20 29 3b 0a 20 20 6d  nMutex(1) );.  m
7afe0 65 6d 73 65 74 28 26 6b 65 79 31 2c 20 30 2c 20  emset(&key1, 0, 
7aff0 73 69 7a 65 6f 66 28 6b 65 79 31 29 29 3b 0a 20  sizeof(key1));. 
7b000 20 6b 65 79 31 2e 64 65 76 20 3d 20 73 74 61 74   key1.dev = stat
7b010 62 75 66 2e 73 74 5f 64 65 76 3b 0a 20 20 6b 65  buf.st_dev;.  ke
7b020 79 31 2e 69 6e 6f 20 3d 20 73 74 61 74 62 75 66  y1.ino = statbuf
7b030 2e 73 74 5f 69 6e 6f 3b 0a 23 69 66 64 65 66 20  .st_ino;.#ifdef 
7b040 53 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48 52 45  SQLITE_UNIX_THRE
7b050 41 44 53 0a 20 20 69 66 28 20 74 68 72 65 61 64  ADS.  if( thread
7b060 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68  sOverrideEachOth
7b070 65 72 73 4c 6f 63 6b 73 3c 30 20 29 7b 0a 20 20  ersLocks<0 ){.  
7b080 20 20 74 65 73 74 54 68 72 65 61 64 4c 6f 63 6b    testThreadLock
7b090 69 6e 67 42 65 68 61 76 69 6f 72 28 66 64 29 3b  ingBehavior(fd);
7b0a0 0a 20 20 7d 0a 20 20 6b 65 79 31 2e 74 69 64 20  .  }.  key1.tid 
7b0b0 3d 20 74 68 72 65 61 64 73 4f 76 65 72 72 69 64  = threadsOverrid
7b0c0 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73  eEachOthersLocks
7b0d0 20 3f 20 30 20 3a 20 70 74 68 72 65 61 64 5f 73   ? 0 : pthread_s
7b0e0 65 6c 66 28 29 3b 0a 23 65 6e 64 69 66 0a 20 20  elf();.#endif.  
7b0f0 6d 65 6d 73 65 74 28 26 6b 65 79 32 2c 20 30 2c  memset(&key2, 0,
7b100 20 73 69 7a 65 6f 66 28 6b 65 79 32 29 29 3b 0a   sizeof(key2));.
7b110 20 20 6b 65 79 32 2e 64 65 76 20 3d 20 73 74 61    key2.dev = sta
7b120 74 62 75 66 2e 73 74 5f 64 65 76 3b 0a 20 20 6b  tbuf.st_dev;.  k
7b130 65 79 32 2e 69 6e 6f 20 3d 20 73 74 61 74 62 75  ey2.ino = statbu
7b140 66 2e 73 74 5f 69 6e 6f 3b 0a 20 20 70 4c 6f 63  f.st_ino;.  pLoc
7b150 6b 20 3d 20 28 73 74 72 75 63 74 20 6c 6f 63 6b  k = (struct lock
7b160 49 6e 66 6f 2a 29 73 71 6c 69 74 65 33 48 61 73  Info*)sqlite3Has
7b170 68 46 69 6e 64 28 26 6c 6f 63 6b 48 61 73 68 2c  hFind(&lockHash,
7b180 20 26 6b 65 79 31 2c 20 73 69 7a 65 6f 66 28 6b   &key1, sizeof(k
7b190 65 79 31 29 29 3b 0a 20 20 69 66 28 20 70 4c 6f  ey1));.  if( pLo
7b1a0 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74 72  ck==0 ){.    str
7b1b0 75 63 74 20 6c 6f 63 6b 49 6e 66 6f 20 2a 70 4f  uct lockInfo *pO
7b1c0 6c 64 3b 0a 20 20 20 20 70 4c 6f 63 6b 20 3d 20  ld;.    pLock = 
7b1d0 73 71 6c 69 74 65 33 54 68 72 65 61 64 53 61 66  sqlite3ThreadSaf
7b1e0 65 4d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28  eMalloc( sizeof(
7b1f0 2a 70 4c 6f 63 6b 29 20 29 3b 0a 20 20 20 20 69  *pLock) );.    i
7b200 66 28 20 70 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20  f( pLock==0 ){. 
7b210 20 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20       rc = 1;.   
7b220 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 66 69 6e     goto exit_fin
7b230 64 6c 6f 63 6b 69 6e 66 6f 3b 0a 20 20 20 20 7d  dlockinfo;.    }
7b240 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 6b 65 79 20  .    pLock->key 
7b250 3d 20 6b 65 79 31 3b 0a 20 20 20 20 70 4c 6f 63  = key1;.    pLoc
7b260 6b 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20  k->nRef = 1;.   
7b270 20 70 4c 6f 63 6b 2d 3e 63 6e 74 20 3d 20 30 3b   pLock->cnt = 0;
7b280 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b  .    pLock->lock
7b290 74 79 70 65 20 3d 20 30 3b 0a 20 20 20 20 70 4f  type = 0;.    pO
7b2a0 6c 64 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68  ld = sqlite3Hash
7b2b0 49 6e 73 65 72 74 28 26 6c 6f 63 6b 48 61 73 68  Insert(&lockHash
7b2c0 2c 20 26 70 4c 6f 63 6b 2d 3e 6b 65 79 2c 20 73  , &pLock->key, s
7b2d0 69 7a 65 6f 66 28 6b 65 79 31 29 2c 20 70 4c 6f  izeof(key1), pLo
7b2e0 63 6b 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 6c  ck);.    if( pOl
7b2f0 64 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73  d!=0 ){.      as
7b300 73 65 72 74 28 20 70 4f 6c 64 3d 3d 70 4c 6f 63  sert( pOld==pLoc
7b310 6b 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  k );.      sqlit
7b320 65 33 54 68 72 65 61 64 53 61 66 65 46 72 65 65  e3ThreadSafeFree
7b330 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 72  (pLock);.      r
7b340 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 67 6f 74  c = 1;.      got
7b350 6f 20 65 78 69 74 5f 66 69 6e 64 6c 6f 63 6b 69  o exit_findlocki
7b360 6e 66 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  nfo;.    }.  }el
7b370 73 65 7b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e  se{.    pLock->n
7b380 52 65 66 2b 2b 3b 0a 20 20 7d 0a 20 20 2a 70 70  Ref++;.  }.  *pp
7b390 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20  Lock = pLock;.  
7b3a0 69 66 28 20 70 70 4f 70 65 6e 21 3d 30 20 29 7b  if( ppOpen!=0 ){
7b3b0 0a 20 20 20 20 70 4f 70 65 6e 20 3d 20 28 73 74  .    pOpen = (st
7b3c0 72 75 63 74 20 6f 70 65 6e 43 6e 74 2a 29 73 71  ruct openCnt*)sq
7b3d0 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 26 6f  lite3HashFind(&o
7b3e0 70 65 6e 48 61 73 68 2c 20 26 6b 65 79 32 2c 20  penHash, &key2, 
7b3f0 73 69 7a 65 6f 66 28 6b 65 79 32 29 29 3b 0a 20  sizeof(key2));. 
7b400 20 20 20 69 66 28 20 70 4f 70 65 6e 3d 3d 30 20     if( pOpen==0 
7b410 29 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20  ){.      struct 
7b420 6f 70 65 6e 43 6e 74 20 2a 70 4f 6c 64 3b 0a 20  openCnt *pOld;. 
7b430 20 20 20 20 20 70 4f 70 65 6e 20 3d 20 73 71 6c       pOpen = sql
7b440 69 74 65 33 54 68 72 65 61 64 53 61 66 65 4d 61  ite3ThreadSafeMa
7b450 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 4f  lloc( sizeof(*pO
7b460 70 65 6e 29 20 29 3b 0a 20 20 20 20 20 20 69 66  pen) );.      if
7b470 28 20 70 4f 70 65 6e 3d 3d 30 20 29 7b 0a 20 20  ( pOpen==0 ){.  
7b480 20 20 20 20 20 20 72 65 6c 65 61 73 65 4c 6f 63        releaseLoc
7b490 6b 49 6e 66 6f 28 70 4c 6f 63 6b 29 3b 0a 20 20  kInfo(pLock);.  
7b4a0 20 20 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20        rc = 1;.  
7b4b0 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f        goto exit_
7b4c0 66 69 6e 64 6c 6f 63 6b 69 6e 66 6f 3b 0a 20 20  findlockinfo;.  
7b4d0 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4f 70 65      }.      pOpe
7b4e0 6e 2d 3e 6b 65 79 20 3d 20 6b 65 79 32 3b 0a 20  n->key = key2;. 
7b4f0 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65 66       pOpen->nRef
7b500 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 4f 70 65   = 1;.      pOpe
7b510 6e 2d 3e 6e 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20  n->nLock = 0;.  
7b520 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64      pOpen->nPend
7b530 69 6e 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  ing = 0;.      p
7b540 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 20 3d  Open->aPending =
7b550 20 30 3b 0a 20 20 20 20 20 20 70 4f 6c 64 20 3d   0;.      pOld =
7b560 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65   sqlite3HashInse
7b570 72 74 28 26 6f 70 65 6e 48 61 73 68 2c 20 26 70  rt(&openHash, &p
7b580 4f 70 65 6e 2d 3e 6b 65 79 2c 20 73 69 7a 65 6f  Open->key, sizeo
7b590 66 28 6b 65 79 32 29 2c 20 70 4f 70 65 6e 29 3b  f(key2), pOpen);
7b5a0 0a 20 20 20 20 20 20 69 66 28 20 70 4f 6c 64 21  .      if( pOld!
7b5b0 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73  =0 ){.        as
7b5c0 73 65 72 74 28 20 70 4f 6c 64 3d 3d 70 4f 70 65  sert( pOld==pOpe
7b5d0 6e 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  n );.        sql
7b5e0 69 74 65 33 54 68 72 65 61 64 53 61 66 65 46 72  ite3ThreadSafeFr
7b5f0 65 65 28 70 4f 70 65 6e 29 3b 0a 20 20 20 20 20  ee(pOpen);.     
7b600 20 20 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e     releaseLockIn
7b610 66 6f 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 20  fo(pLock);.     
7b620 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 20     rc = 1;.     
7b630 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 66 69 6e     goto exit_fin
7b640 64 6c 6f 63 6b 69 6e 66 6f 3b 0a 20 20 20 20 20  dlockinfo;.     
7b650 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
7b660 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 2b      pOpen->nRef+
7b670 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 70  +;.    }.    *pp
7b680 4f 70 65 6e 20 3d 20 70 4f 70 65 6e 3b 0a 20 20  Open = pOpen;.  
7b690 7d 0a 0a 65 78 69 74 5f 66 69 6e 64 6c 6f 63 6b  }..exit_findlock
7b6a0 69 6e 66 6f 3a 0a 20 20 72 65 74 75 72 6e 20 72  info:.  return r
7b6b0 63 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c  c;.}..#ifdef SQL
7b6c0 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20  ITE_DEBUG./*.** 
7b6d0 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20  Helper function 
7b6e0 66 6f 72 20 70 72 69 6e 74 69 6e 67 20 6f 75 74  for printing out
7b6f0 20 74 72 61 63 65 20 69 6e 66 6f 72 6d 61 74 69   trace informati
7b700 6f 6e 20 66 72 6f 6d 20 64 65 62 75 67 67 69 6e  on from debuggin
7b710 67 0a 2a 2a 20 62 69 6e 61 72 69 65 73 2e 20 54  g.** binaries. T
7b720 68 69 73 20 72 65 74 75 72 6e 73 20 74 68 65 20  his returns the 
7b730 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 74 61  string represeta
7b740 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 75 70 70  tion of the supp
7b750 6c 69 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72 20  lied.** integer 
7b760 6c 6f 63 6b 2d 74 79 70 65 2e 0a 2a 2f 0a 73 74  lock-type..*/.st
7b770 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
7b780 2a 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 69 6e  *locktypeName(in
7b790 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 73  t locktype){.  s
7b7a0 77 69 74 63 68 28 20 6c 6f 63 6b 74 79 70 65 20  witch( locktype 
7b7b0 29 7b 0a 20 20 63 61 73 65 20 4e 4f 5f 4c 4f 43  ){.  case NO_LOC
7b7c0 4b 3a 20 72 65 74 75 72 6e 20 22 4e 4f 4e 45 22  K: return "NONE"
7b7d0 3b 0a 20 20 63 61 73 65 20 53 48 41 52 45 44 5f  ;.  case SHARED_
7b7e0 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 53 48  LOCK: return "SH
7b7f0 41 52 45 44 22 3b 0a 20 20 63 61 73 65 20 52 45  ARED";.  case RE
7b800 53 45 52 56 45 44 5f 4c 4f 43 4b 3a 20 72 65 74  SERVED_LOCK: ret
7b810 75 72 6e 20 22 52 45 53 45 52 56 45 44 22 3b 0a  urn "RESERVED";.
7b820 20 20 63 61 73 65 20 50 45 4e 44 49 4e 47 5f 4c    case PENDING_L
7b830 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 50 45 4e  OCK: return "PEN
7b840 44 49 4e 47 22 3b 0a 20 20 63 61 73 65 20 45 58  DING";.  case EX
7b850 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3a 20 72 65  CLUSIVE_LOCK: re
7b860 74 75 72 6e 20 22 45 58 43 4c 55 53 49 56 45 22  turn "EXCLUSIVE"
7b870 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 22  ;.  }.  return "
7b880 45 52 52 4f 52 22 3b 0a 7d 0a 23 65 6e 64 69 66  ERROR";.}.#endif
7b890 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72  ../*.** If we ar
7b8a0 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 61  e currently in a
7b8b0 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61   different threa
7b8c0 64 20 74 68 61 6e 20 74 68 65 20 74 68 72 65 61  d than the threa
7b8d0 64 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 75 6e  d that the.** un
7b8e0 69 78 46 69 6c 65 20 61 72 67 75 6d 65 6e 74 20  ixFile argument 
7b8f0 62 65 6c 6f 6e 67 73 20 74 6f 2c 20 74 68 65 6e  belongs to, then
7b900 20 74 72 61 6e 73 66 65 72 20 6f 77 6e 65 72 73   transfer owners
7b910 68 69 70 20 6f 66 20 74 68 65 20 75 6e 69 78 46  hip of the unixF
7b920 69 6c 65 0a 2a 2a 20 6f 76 65 72 20 74 6f 20 74  ile.** over to t
7b930 68 65 20 63 75 72 72 65 6e 74 20 74 68 72 65 61  he current threa
7b940 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 75 6e 69 78 46  d..**.** A unixF
7b950 69 6c 65 20 69 73 20 6f 6e 6c 79 20 6f 77 6e 65  ile is only owne
7b960 64 20 62 79 20 61 20 74 68 72 65 61 64 20 6f 6e  d by a thread on
7b970 20 73 79 73 74 65 6d 73 20 77 68 65 72 65 20 6f   systems where o
7b980 6e 65 20 74 68 72 65 61 64 20 69 73 0a 2a 2a 20  ne thread is.** 
7b990 75 6e 61 62 6c 65 20 74 6f 20 6f 76 65 72 72 69  unable to overri
7b9a0 64 65 20 6c 6f 63 6b 73 20 63 72 65 61 74 65 64  de locks created
7b9b0 20 62 79 20 61 20 64 69 66 66 65 72 65 6e 74 20   by a different 
7b9c0 74 68 72 65 61 64 2e 20 20 52 65 64 48 61 74 39  thread.  RedHat9
7b9d0 20 69 73 0a 2a 2a 20 61 6e 20 65 78 61 6d 70 6c   is.** an exampl
7b9e0 65 20 6f 66 20 73 75 63 68 20 61 20 73 79 73 74  e of such a syst
7b9f0 65 6d 2e 0a 2a 2a 0a 2a 2a 20 4f 77 6e 65 72 73  em..**.** Owners
7ba00 68 69 70 20 74 72 61 6e 73 66 65 72 20 69 73 20  hip transfer is 
7ba10 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 69 66 20  only allowed if 
7ba20 74 68 65 20 75 6e 69 78 46 69 6c 65 20 69 73 20  the unixFile is 
7ba30 63 75 72 72 65 6e 74 6c 79 20 75 6e 6c 6f 63 6b  currently unlock
7ba40 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 75 6e  ed..** If the un
7ba50 69 78 46 69 6c 65 20 69 73 20 6c 6f 63 6b 65 64  ixFile is locked
7ba60 20 61 6e 64 20 61 6e 20 6f 77 6e 65 72 73 68 69   and an ownershi
7ba70 70 20 69 73 20 77 72 6f 6e 67 2c 20 74 68 65 6e  p is wrong, then
7ba80 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54   return.** SQLIT
7ba90 45 5f 4d 49 53 55 53 45 2e 20 20 53 51 4c 49 54  E_MISUSE.  SQLIT
7baa0 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
7bab0 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 77   if everything w
7bac0 6f 72 6b 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  orks..*/.#ifdef 
7bad0 53 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48 52 45  SQLITE_UNIX_THRE
7bae0 41 44 53 0a 73 74 61 74 69 63 20 69 6e 74 20 74  ADS.static int t
7baf0 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 68 69 70  ransferOwnership
7bb00 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65  (unixFile *pFile
7bb10 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 70  ){.  int rc;.  p
7bb20 74 68 72 65 61 64 5f 74 20 68 53 65 6c 66 3b 0a  thread_t hSelf;.
7bb30 20 20 69 66 28 20 74 68 72 65 61 64 73 4f 76 65    if( threadsOve
7bb40 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c  rrideEachOthersL
7bb50 6f 63 6b 73 20 29 7b 0a 20 20 20 20 2f 2a 20 4f  ocks ){.    /* O
7bb60 77 6e 65 72 73 68 69 70 20 74 72 61 6e 73 66 65  wnership transfe
7bb70 72 73 20 6e 6f 74 20 6e 65 65 64 65 64 20 6f 6e  rs not needed on
7bb80 20 74 68 69 73 20 73 79 73 74 65 6d 20 2a 2f 0a   this system */.
7bb90 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
7bba0 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 68 53 65 6c  E_OK;.  }.  hSel
7bbb0 66 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66  f = pthread_self
7bbc0 28 29 3b 0a 20 20 69 66 28 20 70 74 68 72 65 61  ();.  if( pthrea
7bbd0 64 5f 65 71 75 61 6c 28 70 46 69 6c 65 2d 3e 74  d_equal(pFile->t
7bbe0 69 64 2c 20 68 53 65 6c 66 29 20 29 7b 0a 20 20  id, hSelf) ){.  
7bbf0 20 20 2f 2a 20 57 65 20 61 72 65 20 73 74 69 6c    /* We are stil
7bc00 6c 20 69 6e 20 74 68 65 20 73 61 6d 65 20 74 68  l in the same th
7bc10 72 65 61 64 20 2a 2f 0a 20 20 20 20 4f 53 54 52  read */.    OSTR
7bc20 41 43 45 31 28 22 4e 6f 2d 74 72 61 6e 73 66 65  ACE1("No-transfe
7bc30 72 2c 20 73 61 6d 65 20 74 68 72 65 61 64 5c 6e  r, same thread\n
7bc40 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  ");.    return S
7bc50 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
7bc60 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  if( pFile->lockt
7bc70 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a  ype!=NO_LOCK ){.
7bc80 20 20 20 20 2f 2a 20 57 65 20 63 61 6e 6e 6f 74      /* We cannot
7bc90 20 63 68 61 6e 67 65 20 6f 77 6e 65 72 73 68 69   change ownershi
7bca0 70 20 77 68 69 6c 65 20 77 65 20 61 72 65 20 68  p while we are h
7bcb0 6f 6c 64 69 6e 67 20 61 20 6c 6f 63 6b 21 20 2a  olding a lock! *
7bcc0 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  /.    return SQL
7bcd0 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a  ITE_MISUSE;.  }.
7bce0 20 20 4f 53 54 52 41 43 45 34 28 22 54 72 61 6e    OSTRACE4("Tran
7bcf0 73 66 65 72 20 6f 77 6e 65 72 73 68 69 70 20 6f  sfer ownership o
7bd00 66 20 25 64 20 66 72 6f 6d 20 25 64 20 74 6f 20  f %d from %d to 
7bd10 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20  %d\n",.         
7bd20 20 20 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69     pFile->h, pFi
7bd30 6c 65 2d 3e 74 69 64 2c 20 68 53 65 6c 66 29 3b  le->tid, hSelf);
7bd40 0a 20 20 70 46 69 6c 65 2d 3e 74 69 64 20 3d 20  .  pFile->tid = 
7bd50 68 53 65 6c 66 3b 0a 20 20 69 66 20 28 70 46 69  hSelf;.  if (pFi
7bd60 6c 65 2d 3e 70 4c 6f 63 6b 20 21 3d 20 4e 55 4c  le->pLock != NUL
7bd70 4c 29 20 7b 0a 20 20 20 20 72 65 6c 65 61 73 65  L) {.    release
7bd80 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 6c 65 2d 3e  LockInfo(pFile->
7bd90 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 72 63 20 3d  pLock);.    rc =
7bda0 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 46   findLockInfo(pF
7bdb0 69 6c 65 2d 3e 68 2c 20 26 70 46 69 6c 65 2d 3e  ile->h, &pFile->
7bdc0 70 4c 6f 63 6b 2c 20 30 29 3b 0a 20 20 20 20 4f  pLock, 0);.    O
7bdd0 53 54 52 41 43 45 35 28 22 4c 4f 43 4b 20 20 20  STRACE5("LOCK   
7bde0 20 25 64 20 69 73 20 6e 6f 77 20 25 73 28 25 73   %d is now %s(%s
7bdf0 2c 25 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  ,%d)\n", pFile->
7be00 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 6c 6f  h,.           lo
7be10 63 6b 74 79 70 65 4e 61 6d 65 28 70 46 69 6c 65  cktypeName(pFile
7be20 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 0a 20 20 20  ->locktype),.   
7be30 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65          locktype
7be40 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 70 4c 6f 63  Name(pFile->pLoc
7be50 6b 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 20 70 46  k->locktype), pF
7be60 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 63 6e 74 29  ile->pLock->cnt)
7be70 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  ;.    return rc;
7be80 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20  .  } else {.    
7be90 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
7bea0 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 20 20  ;.  }.}.#else.  
7beb0 2f 2a 20 4f 6e 20 73 69 6e 67 6c 65 2d 74 68 72  /* On single-thr
7bec0 65 61 64 65 64 20 62 75 69 6c 64 73 2c 20 6f 77  eaded builds, ow
7bed0 6e 65 72 73 68 69 70 20 74 72 61 6e 73 66 65 72  nership transfer
7bee0 20 69 73 20 61 20 6e 6f 2d 6f 70 20 2a 2f 0a 23   is a no-op */.#
7bef0 20 64 65 66 69 6e 65 20 74 72 61 6e 73 66 65 72   define transfer
7bf00 4f 77 6e 65 72 73 68 69 70 28 58 29 20 53 51 4c  Ownership(X) SQL
7bf10 49 54 45 5f 4f 4b 0a 23 65 6e 64 69 66 0a 0a 2f  ITE_OK.#endif../
7bf20 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20  *.** Delete the 
7bf30 6e 61 6d 65 64 20 66 69 6c 65 0a 2a 2f 0a 53 51  named file.*/.SQ
7bf40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
7bf50 20 73 71 6c 69 74 65 33 55 6e 69 78 44 65 6c 65   sqlite3UnixDele
7bf60 74 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  te(const char *z
7bf70 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 53 69 6d  Filename){.  Sim
7bf80 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74  ulateIOError(ret
7bf90 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
7bfa0 5f 44 45 4c 45 54 45 29 3b 0a 20 20 75 6e 6c 69  _DELETE);.  unli
7bfb0 6e 6b 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  nk(zFilename);. 
7bfc0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
7bfd0 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  K;.}../*.** Retu
7bfe0 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 6e  rn TRUE if the n
7bff0 61 6d 65 64 20 66 69 6c 65 20 65 78 69 73 74 73  amed file exists
7c000 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
7c010 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55  ATE int sqlite3U
7c020 6e 69 78 46 69 6c 65 45 78 69 73 74 73 28 63 6f  nixFileExists(co
7c030 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  nst char *zFilen
7c040 61 6d 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 61  ame){.  return a
7c050 63 63 65 73 73 28 7a 46 69 6c 65 6e 61 6d 65 2c  ccess(zFilename,
7c060 20 30 29 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 20 46 6f   0)==0;.}../* Fo
7c070 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f  rward declaratio
7c080 6e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  n */.static int 
7c090 61 6c 6c 6f 63 61 74 65 55 6e 69 78 46 69 6c 65  allocateUnixFile
7c0a0 28 0a 20 20 69 6e 74 20 68 2c 20 20 20 20 20 20  (.  int h,      
7c0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
7c0c0 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   File descriptor
7c0d0 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 66 69 6c   of the open fil
7c0e0 65 20 2a 2f 0a 20 20 4f 73 46 69 6c 65 20 2a 2a  e */.  OsFile **
7c0f0 70 49 64 2c 20 20 20 20 20 20 20 20 20 20 20 20  pId,            
7c100 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 72 65   /* Write the re
7c110 61 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  al file descript
7c120 6f 72 20 68 65 72 65 20 2a 2f 0a 20 20 63 6f 6e  or here */.  con
7c130 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  st char *zFilena
7c140 6d 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f  me,    /* Name o
7c150 66 20 74 68 65 20 66 69 6c 65 20 62 65 69 6e 67  f the file being
7c160 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 69 6e 74   opened */.  int
7c170 20 64 65 6c 46 6c 61 67 20 20 20 20 20 20 20 20   delFlag        
7c180 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75         /* If tru
7c190 65 2c 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65  e, make sure the
7c1a0 20 66 69 6c 65 20 64 65 6c 65 74 65 73 20 6f 6e   file deletes on
7c1b0 20 63 6c 6f 73 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a   close */.);../*
7c1c0 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6f  .** Attempt to o
7c1d0 70 65 6e 20 61 20 66 69 6c 65 20 66 6f 72 20 62  pen a file for b
7c1e0 6f 74 68 20 72 65 61 64 69 6e 67 20 61 6e 64 20  oth reading and 
7c1f0 77 72 69 74 69 6e 67 2e 20 20 49 66 20 74 68 61  writing.  If tha
7c200 74 0a 2a 2a 20 66 61 69 6c 73 2c 20 74 72 79 20  t.** fails, try 
7c210 6f 70 65 6e 69 6e 67 20 69 74 20 72 65 61 64 2d  opening it read-
7c220 6f 6e 6c 79 2e 20 20 49 66 20 74 68 65 20 66 69  only.  If the fi
7c230 6c 65 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73  le does not exis
7c240 74 2c 0a 2a 2a 20 74 72 79 20 74 6f 20 63 72 65  t,.** try to cre
7c250 61 74 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e  ate it..**.** On
7c260 20 73 75 63 63 65 73 73 2c 20 61 20 68 61 6e 64   success, a hand
7c270 6c 65 20 66 6f 72 20 74 68 65 20 6f 70 65 6e 20  le for the open 
7c280 66 69 6c 65 20 69 73 20 77 72 69 74 74 65 6e 20  file is written 
7c290 74 6f 20 2a 69 64 0a 2a 2a 20 61 6e 64 20 2a 70  to *id.** and *p
7c2a0 52 65 61 64 6f 6e 6c 79 20 69 73 20 73 65 74 20  Readonly is set 
7c2b0 74 6f 20 30 20 69 66 20 74 68 65 20 66 69 6c 65  to 0 if the file
7c2c0 20 77 61 73 20 6f 70 65 6e 65 64 20 66 6f 72 20   was opened for 
7c2d0 72 65 61 64 69 6e 67 20 61 6e 64 0a 2a 2a 20 77  reading and.** w
7c2e0 72 69 74 69 6e 67 20 6f 72 20 31 20 69 66 20 74  riting or 1 if t
7c2f0 68 65 20 66 69 6c 65 20 77 61 73 20 6f 70 65 6e  he file was open
7c300 65 64 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 20 54  ed read-only.  T
7c310 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  he function retu
7c320 72 6e 73 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b  rns.** SQLITE_OK
7c330 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 66 61 69 6c 75  ..**.** On failu
7c340 72 65 2c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e  re, the function
7c350 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   returns SQLITE_
7c360 43 41 4e 54 4f 50 45 4e 20 61 6e 64 20 6c 65 61  CANTOPEN and lea
7c370 76 65 73 0a 2a 2a 20 2a 69 64 20 61 6e 64 20 2a  ves.** *id and *
7c380 70 52 65 61 64 6f 6e 6c 79 20 75 6e 63 68 61 6e  pReadonly unchan
7c390 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ged..*/.SQLITE_P
7c3a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
7c3b0 65 33 55 6e 69 78 4f 70 65 6e 52 65 61 64 57 72  e3UnixOpenReadWr
7c3c0 69 74 65 28 0a 20 20 63 6f 6e 73 74 20 63 68 61  ite(.  const cha
7c3d0 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20 20  r *zFilename,.  
7c3e0 4f 73 46 69 6c 65 20 2a 2a 70 49 64 2c 0a 20 20  OsFile **pId,.  
7c3f0 69 6e 74 20 2a 70 52 65 61 64 6f 6e 6c 79 0a 29  int *pReadonly.)
7c400 7b 0a 20 20 69 6e 74 20 68 3b 0a 20 20 0a 20 20  {.  int h;.  .  
7c410 43 52 41 53 48 5f 54 45 53 54 5f 4f 56 45 52 52  CRASH_TEST_OVERR
7c420 49 44 45 28 73 71 6c 69 74 65 33 43 72 61 73 68  IDE(sqlite3Crash
7c430 4f 70 65 6e 52 65 61 64 57 72 69 74 65 2c 20 7a  OpenReadWrite, z
7c440 46 69 6c 65 6e 61 6d 65 2c 20 70 49 64 2c 20 70  Filename, pId, p
7c450 52 65 61 64 6f 6e 6c 79 29 3b 0a 20 20 61 73 73  Readonly);.  ass
7c460 65 72 74 28 20 30 3d 3d 2a 70 49 64 20 29 3b 0a  ert( 0==*pId );.
7c470 20 20 68 20 3d 20 6f 70 65 6e 28 7a 46 69 6c 65    h = open(zFile
7c480 6e 61 6d 65 2c 20 4f 5f 52 44 57 52 7c 4f 5f 43  name, O_RDWR|O_C
7c490 52 45 41 54 7c 4f 5f 4c 41 52 47 45 46 49 4c 45  REAT|O_LARGEFILE
7c4a0 7c 4f 5f 42 49 4e 41 52 59 2c 0a 20 20 20 20 20  |O_BINARY,.     
7c4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7c4c0 20 20 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c     SQLITE_DEFAUL
7c4d0 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f  T_FILE_PERMISSIO
7c4e0 4e 53 29 3b 0a 20 20 69 66 28 20 68 3c 30 20 29  NS);.  if( h<0 )
7c4f0 7b 0a 23 69 66 64 65 66 20 45 49 53 44 49 52 0a  {.#ifdef EISDIR.
7c500 20 20 20 20 69 66 28 20 65 72 72 6e 6f 3d 3d 45      if( errno==E
7c510 49 53 44 49 52 20 29 7b 0a 20 20 20 20 20 20 72  ISDIR ){.      r
7c520 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e  eturn SQLITE_CAN
7c530 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 23 65 6e  TOPEN;.    }.#en
7c540 64 69 66 0a 20 20 20 20 68 20 3d 20 6f 70 65 6e  dif.    h = open
7c550 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 4f 5f 52 44  (zFilename, O_RD
7c560 4f 4e 4c 59 7c 4f 5f 4c 41 52 47 45 46 49 4c 45  ONLY|O_LARGEFILE
7c570 7c 4f 5f 42 49 4e 41 52 59 29 3b 0a 20 20 20 20  |O_BINARY);.    
7c580 69 66 28 20 68 3c 30 20 29 7b 0a 20 20 20 20 20  if( h<0 ){.     
7c590 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
7c5a0 41 4e 54 4f 50 45 4e 3b 20 0a 20 20 20 20 7d 0a  ANTOPEN; .    }.
7c5b0 20 20 20 20 2a 70 52 65 61 64 6f 6e 6c 79 20 3d      *pReadonly =
7c5c0 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   1;.  }else{.   
7c5d0 20 2a 70 52 65 61 64 6f 6e 6c 79 20 3d 20 30 3b   *pReadonly = 0;
7c5e0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 61 6c  .  }.  return al
7c5f0 6c 6f 63 61 74 65 55 6e 69 78 46 69 6c 65 28 68  locateUnixFile(h
7c600 2c 20 70 49 64 2c 20 7a 46 69 6c 65 6e 61 6d 65  , pId, zFilename
7c610 2c 20 30 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  , 0);.}.../*.** 
7c620 41 74 74 65 6d 70 74 20 74 6f 20 6f 70 65 6e 20  Attempt to open 
7c630 61 20 6e 65 77 20 66 69 6c 65 20 66 6f 72 20 65  a new file for e
7c640 78 63 6c 75 73 69 76 65 20 61 63 63 65 73 73 20  xclusive access 
7c650 62 79 20 74 68 69 73 20 70 72 6f 63 65 73 73 2e  by this process.
7c660 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 77 69 6c  .** The file wil
7c670 6c 20 62 65 20 6f 70 65 6e 65 64 20 66 6f 72 20  l be opened for 
7c680 62 6f 74 68 20 72 65 61 64 69 6e 67 20 61 6e 64  both reading and
7c690 20 77 72 69 74 69 6e 67 2e 20 20 54 6f 20 61 76   writing.  To av
7c6a0 6f 69 64 0a 2a 2a 20 61 20 70 6f 74 65 6e 74 69  oid.** a potenti
7c6b0 61 6c 20 73 65 63 75 72 69 74 79 20 70 72 6f 62  al security prob
7c6c0 6c 65 6d 2c 20 77 65 20 64 6f 20 6e 6f 74 20 61  lem, we do not a
7c6d0 6c 6c 6f 77 20 74 68 65 20 66 69 6c 65 20 74 6f  llow the file to
7c6e0 20 68 61 76 65 0a 2a 2a 20 70 72 65 76 69 6f 75   have.** previou
7c6f0 73 6c 79 20 65 78 69 73 74 65 64 2e 20 20 4e 6f  sly existed.  No
7c700 72 20 64 6f 20 77 65 20 61 6c 6c 6f 77 20 74 68  r do we allow th
7c710 65 20 66 69 6c 65 20 74 6f 20 62 65 20 61 20 73  e file to be a s
7c720 79 6d 62 6f 6c 69 63 0a 2a 2a 20 6c 69 6e 6b 2e  ymbolic.** link.
7c730 0a 2a 2a 0a 2a 2a 20 49 66 20 64 65 6c 46 6c 61  .**.** If delFla
7c740 67 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20  g is true, then 
7c750 6d 61 6b 65 20 61 72 72 61 6e 67 65 6d 65 6e 74  make arrangement
7c760 73 20 74 6f 20 61 75 74 6f 6d 61 74 69 63 61 6c  s to automatical
7c770 6c 79 20 64 65 6c 65 74 65 0a 2a 2a 20 74 68 65  ly delete.** the
7c780 20 66 69 6c 65 20 77 68 65 6e 20 69 74 20 69 73   file when it is
7c790 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f   closed..**.** O
7c7a0 6e 20 73 75 63 63 65 73 73 2c 20 77 72 69 74 65  n success, write
7c7b0 20 74 68 65 20 66 69 6c 65 20 68 61 6e 64 6c 65   the file handle
7c7c0 20 69 6e 74 6f 20 2a 69 64 20 61 6e 64 20 72 65   into *id and re
7c7d0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a  turn SQLITE_OK..
7c7e0 2a 2a 0a 2a 2a 20 4f 6e 20 66 61 69 6c 75 72 65  **.** On failure
7c7f0 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  , return SQLITE_
7c800 43 41 4e 54 4f 50 45 4e 2e 0a 2a 2f 0a 53 51 4c  CANTOPEN..*/.SQL
7c810 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
7c820 73 71 6c 69 74 65 33 55 6e 69 78 4f 70 65 6e 45  sqlite3UnixOpenE
7c830 78 63 6c 75 73 69 76 65 28 63 6f 6e 73 74 20 63  xclusive(const c
7c840 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20  har *zFilename, 
7c850 4f 73 46 69 6c 65 20 2a 2a 70 49 64 2c 20 69 6e  OsFile **pId, in
7c860 74 20 64 65 6c 46 6c 61 67 29 7b 0a 20 20 69 6e  t delFlag){.  in
7c870 74 20 68 3b 0a 0a 20 20 43 52 41 53 48 5f 54 45  t h;..  CRASH_TE
7c880 53 54 5f 4f 56 45 52 52 49 44 45 28 73 71 6c 69  ST_OVERRIDE(sqli
7c890 74 65 33 43 72 61 73 68 4f 70 65 6e 45 78 63 6c  te3CrashOpenExcl
7c8a0 75 73 69 76 65 2c 20 7a 46 69 6c 65 6e 61 6d 65  usive, zFilename
7c8b0 2c 20 70 49 64 2c 20 64 65 6c 46 6c 61 67 29 3b  , pId, delFlag);
7c8c0 0a 20 20 61 73 73 65 72 74 28 20 30 3d 3d 2a 70  .  assert( 0==*p
7c8d0 49 64 20 29 3b 0a 20 20 68 20 3d 20 6f 70 65 6e  Id );.  h = open
7c8e0 28 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20 20 20 20  (zFilename,.    
7c8f0 20 20 20 20 20 20 20 20 20 20 20 20 4f 5f 52 44              O_RD
7c900 57 52 7c 4f 5f 43 52 45 41 54 7c 4f 5f 45 58 43  WR|O_CREAT|O_EXC
7c910 4c 7c 4f 5f 4e 4f 46 4f 4c 4c 4f 57 7c 4f 5f 4c  L|O_NOFOLLOW|O_L
7c920 41 52 47 45 46 49 4c 45 7c 4f 5f 42 49 4e 41 52  ARGEFILE|O_BINAR
7c930 59 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  Y,.             
7c940 20 20 20 64 65 6c 46 6c 61 67 20 3f 20 30 36 30     delFlag ? 060
7c950 30 20 3a 20 53 51 4c 49 54 45 5f 44 45 46 41 55  0 : SQLITE_DEFAU
7c960 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53 49  LT_FILE_PERMISSI
7c970 4f 4e 53 29 3b 0a 20 20 69 66 28 20 68 3c 30 20  ONS);.  if( h<0 
7c980 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
7c990 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20  LITE_CANTOPEN;. 
7c9a0 20 7d 0a 20 20 72 65 74 75 72 6e 20 61 6c 6c 6f   }.  return allo
7c9b0 63 61 74 65 55 6e 69 78 46 69 6c 65 28 68 2c 20  cateUnixFile(h, 
7c9c0 70 49 64 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20  pId, zFilename, 
7c9d0 64 65 6c 46 6c 61 67 29 3b 0a 7d 0a 0a 2f 2a 0a  delFlag);.}../*.
7c9e0 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6f 70  ** Attempt to op
7c9f0 65 6e 20 61 20 6e 65 77 20 66 69 6c 65 20 66 6f  en a new file fo
7ca00 72 20 72 65 61 64 2d 6f 6e 6c 79 20 61 63 63 65  r read-only acce
7ca10 73 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63  ss..**.** On suc
7ca20 63 65 73 73 2c 20 77 72 69 74 65 20 74 68 65 20  cess, write the 
7ca30 66 69 6c 65 20 68 61 6e 64 6c 65 20 69 6e 74 6f  file handle into
7ca40 20 2a 69 64 20 61 6e 64 20 72 65 74 75 72 6e 20   *id and return 
7ca50 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a  SQLITE_OK..**.**
7ca60 20 4f 6e 20 66 61 69 6c 75 72 65 2c 20 72 65 74   On failure, ret
7ca70 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f  urn SQLITE_CANTO
7ca80 50 45 4e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  PEN..*/.SQLITE_P
7ca90 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
7caa0 65 33 55 6e 69 78 4f 70 65 6e 52 65 61 64 4f 6e  e3UnixOpenReadOn
7cab0 6c 79 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  ly(const char *z
7cac0 46 69 6c 65 6e 61 6d 65 2c 20 4f 73 46 69 6c 65  Filename, OsFile
7cad0 20 2a 2a 70 49 64 29 7b 0a 20 20 69 6e 74 20 68   **pId){.  int h
7cae0 3b 0a 20 20 0a 20 20 43 52 41 53 48 5f 54 45 53  ;.  .  CRASH_TES
7caf0 54 5f 4f 56 45 52 52 49 44 45 28 73 71 6c 69 74  T_OVERRIDE(sqlit
7cb00 65 33 43 72 61 73 68 4f 70 65 6e 52 65 61 64 4f  e3CrashOpenReadO
7cb10 6e 6c 79 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20  nly, zFilename, 
7cb20 70 49 64 2c 20 30 29 3b 0a 20 20 61 73 73 65 72  pId, 0);.  asser
7cb30 74 28 20 30 3d 3d 2a 70 49 64 20 29 3b 0a 20 20  t( 0==*pId );.  
7cb40 68 20 3d 20 6f 70 65 6e 28 7a 46 69 6c 65 6e 61  h = open(zFilena
7cb50 6d 65 2c 20 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 4c  me, O_RDONLY|O_L
7cb60 41 52 47 45 46 49 4c 45 7c 4f 5f 42 49 4e 41 52  ARGEFILE|O_BINAR
7cb70 59 29 3b 0a 20 20 69 66 28 20 68 3c 30 20 29 7b  Y);.  if( h<0 ){
7cb80 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
7cb90 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d  TE_CANTOPEN;.  }
7cba0 0a 20 20 72 65 74 75 72 6e 20 61 6c 6c 6f 63 61  .  return alloca
7cbb0 74 65 55 6e 69 78 46 69 6c 65 28 68 2c 20 70 49  teUnixFile(h, pI
7cbc0 64 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 30 29  d, zFilename, 0)
7cbd0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d  ;.}../*.** Attem
7cbe0 70 74 20 74 6f 20 6f 70 65 6e 20 61 20 66 69 6c  pt to open a fil
7cbf0 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72  e descriptor for
7cc00 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 74   the directory t
7cc10 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 0a 2a  hat contains a.*
7cc20 2a 20 66 69 6c 65 2e 20 20 54 68 69 73 20 66 69  * file.  This fi
7cc30 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 63 61  le descriptor ca
7cc40 6e 20 62 65 20 75 73 65 64 20 74 6f 20 66 73 79  n be used to fsy
7cc50 6e 63 28 29 20 74 68 65 20 64 69 72 65 63 74 6f  nc() the directo
7cc60 72 79 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74  ry.** in order t
7cc70 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20  o make sure the 
7cc80 63 72 65 61 74 69 6f 6e 20 6f 66 20 61 20 6e 65  creation of a ne
7cc90 77 20 66 69 6c 65 20 69 73 20 61 63 74 75 61 6c  w file is actual
7cca0 6c 79 20 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f  ly written.** to
7ccb0 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69   disk..**.** Thi
7ccc0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6f 6e 6c  s routine is onl
7ccd0 79 20 6d 65 61 6e 69 6e 67 66 75 6c 20 66 6f 72  y meaningful for
7cce0 20 55 6e 69 78 2e 20 20 49 74 20 69 73 20 61 20   Unix.  It is a 
7ccf0 6e 6f 2d 6f 70 20 75 6e 64 65 72 0a 2a 2a 20 77  no-op under.** w
7cd00 69 6e 64 6f 77 73 20 73 69 6e 63 65 20 77 69 6e  indows since win
7cd10 64 6f 77 73 20 64 6f 65 73 20 6e 6f 74 20 73 75  dows does not su
7cd20 70 70 6f 72 74 20 68 61 72 64 20 6c 69 6e 6b 73  pport hard links
7cd30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 46 55 4c 4c 5f  ..**.** If FULL_
7cd40 46 53 59 4e 43 20 69 73 20 65 6e 61 62 6c 65 64  FSYNC is enabled
7cd50 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  , this function 
7cd60 69 73 20 6e 6f 74 20 6c 6f 6e 67 65 72 20 75 73  is not longer us
7cd70 65 66 75 6c 2c 20 0a 2a 2a 20 61 20 46 55 4c 4c  eful, .** a FULL
7cd80 5f 46 53 59 4e 43 20 73 79 6e 63 20 61 70 70 6c  _FSYNC sync appl
7cd90 69 65 73 20 74 6f 20 61 6c 6c 20 70 65 6e 64 69  ies to all pendi
7cda0 6e 67 20 64 69 73 6b 20 6f 70 65 72 61 74 69 6f  ng disk operatio
7cdb0 6e 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63  ns..**.** On suc
7cdc0 63 65 73 73 2c 20 61 20 68 61 6e 64 6c 65 20 66  cess, a handle f
7cdd0 6f 72 20 61 20 70 72 65 76 69 6f 75 73 6c 79 20  or a previously 
7cde0 6f 70 65 6e 20 66 69 6c 65 20 61 74 20 2a 69 64  open file at *id
7cdf0 20 69 73 0a 2a 2a 20 75 70 64 61 74 65 64 20 77   is.** updated w
7ce00 69 74 68 20 74 68 65 20 6e 65 77 20 64 69 72 65  ith the new dire
7ce10 63 74 6f 72 79 20 66 69 6c 65 20 64 65 73 63 72  ctory file descr
7ce20 69 70 74 6f 72 20 61 6e 64 20 53 51 4c 49 54 45  iptor and SQLITE
7ce30 5f 4f 4b 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e  _OK is.** return
7ce40 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 66 61 69  ed..**.** On fai
7ce50 6c 75 72 65 2c 20 74 68 65 20 66 75 6e 63 74 69  lure, the functi
7ce60 6f 6e 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  on returns SQLIT
7ce70 45 5f 43 41 4e 54 4f 50 45 4e 20 61 6e 64 20 6c  E_CANTOPEN and l
7ce80 65 61 76 65 73 0a 2a 2a 20 2a 69 64 20 75 6e 63  eaves.** *id unc
7ce90 68 61 6e 67 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  hanged..*/.stati
7cea0 63 20 69 6e 74 20 75 6e 69 78 4f 70 65 6e 44 69  c int unixOpenDi
7ceb0 72 65 63 74 6f 72 79 28 0a 20 20 4f 73 46 69 6c  rectory(.  OsFil
7cec0 65 20 2a 69 64 2c 0a 20 20 63 6f 6e 73 74 20 63  e *id,.  const c
7ced0 68 61 72 20 2a 7a 44 69 72 6e 61 6d 65 0a 29 7b  har *zDirname.){
7cee0 0a 20 20 69 6e 74 20 68 3b 0a 20 20 75 6e 69 78  .  int h;.  unix
7cef0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
7cf00 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61  nixFile*)id;.  a
7cf10 73 73 65 72 74 28 20 70 46 69 6c 65 21 3d 30 20  ssert( pFile!=0 
7cf20 29 3b 0a 20 20 53 45 54 5f 54 48 52 45 41 44 49  );.  SET_THREADI
7cf30 44 28 70 46 69 6c 65 29 3b 0a 20 20 61 73 73 65  D(pFile);.  asse
7cf40 72 74 28 20 70 46 69 6c 65 2d 3e 64 69 72 66 64  rt( pFile->dirfd
7cf50 3c 30 20 29 3b 0a 20 20 70 46 69 6c 65 2d 3e 64  <0 );.  pFile->d
7cf60 69 72 66 64 20 3d 20 68 20 3d 20 6f 70 65 6e 28  irfd = h = open(
7cf70 7a 44 69 72 6e 61 6d 65 2c 20 4f 5f 52 44 4f 4e  zDirname, O_RDON
7cf80 4c 59 7c 4f 5f 42 49 4e 41 52 59 2c 20 30 29 3b  LY|O_BINARY, 0);
7cf90 0a 20 20 69 66 28 20 68 3c 30 20 29 7b 0a 20 20  .  if( h<0 ){.  
7cfa0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
7cfb0 43 41 4e 54 4f 50 45 4e 3b 20 0a 20 20 7d 0a 23  CANTOPEN; .  }.#
7cfc0 69 66 64 65 66 20 46 44 5f 43 4c 4f 45 58 45 43  ifdef FD_CLOEXEC
7cfd0 0a 20 20 66 63 6e 74 6c 28 68 2c 20 46 5f 53 45  .  fcntl(h, F_SE
7cfe0 54 46 44 2c 20 66 63 6e 74 6c 28 68 2c 20 46 5f  TFD, fcntl(h, F_
7cff0 47 45 54 46 44 2c 20 30 29 20 7c 20 46 44 5f 43  GETFD, 0) | FD_C
7d000 4c 4f 45 58 45 43 29 3b 0a 23 65 6e 64 69 66 0a  LOEXEC);.#endif.
7d010 20 20 4f 53 54 52 41 43 45 33 28 22 4f 50 45 4e    OSTRACE3("OPEN
7d020 44 49 52 20 25 2d 33 64 20 25 73 5c 6e 22 2c 20  DIR %-3d %s\n", 
7d030 68 2c 20 7a 44 69 72 6e 61 6d 65 29 3b 0a 20 20  h, zDirname);.  
7d040 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
7d050 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74  ;.}../*.** Creat
7d060 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69  e a temporary fi
7d070 6c 65 20 6e 61 6d 65 20 69 6e 20 7a 42 75 66 2e  le name in zBuf.
7d080 20 20 7a 42 75 66 20 6d 75 73 74 20 62 65 20 62    zBuf must be b
7d090 69 67 20 65 6e 6f 75 67 68 20 74 6f 0a 2a 2a 20  ig enough to.** 
7d0a0 68 6f 6c 64 20 61 74 20 6c 65 61 73 74 20 53 51  hold at least SQ
7d0b0 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45 5f 53 49  LITE_TEMPNAME_SI
7d0c0 5a 45 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a  ZE characters..*
7d0d0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
7d0e0 20 69 6e 74 20 73 71 6c 69 74 65 33 55 6e 69 78   int sqlite3Unix
7d0f0 54 65 6d 70 46 69 6c 65 4e 61 6d 65 28 63 68 61  TempFileName(cha
7d100 72 20 2a 7a 42 75 66 29 7b 0a 20 20 73 74 61 74  r *zBuf){.  stat
7d110 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 61  ic const char *a
7d120 7a 44 69 72 73 5b 5d 20 3d 20 7b 0a 20 20 20 20  zDirs[] = {.    
7d130 20 30 2c 0a 20 20 20 20 20 22 2f 76 61 72 2f 74   0,.     "/var/t
7d140 6d 70 22 2c 0a 20 20 20 20 20 22 2f 75 73 72 2f  mp",.     "/usr/
7d150 74 6d 70 22 2c 0a 20 20 20 20 20 22 2f 74 6d 70  tmp",.     "/tmp
7d160 22 2c 0a 20 20 20 20 20 22 2e 22 2c 0a 20 20 7d  ",.     ".",.  }
7d170 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  ;.  static const
7d180 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 7a   unsigned char z
7d190 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22 61  Chars[] =.    "a
7d1a0 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71  bcdefghijklmnopq
7d1b0 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20 20 22  rstuvwxyz".    "
7d1c0 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50  ABCDEFGHIJKLMNOP
7d1d0 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20 20 20  QRSTUVWXYZ".    
7d1e0 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a 20 20  "0123456789";.  
7d1f0 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 73 74 72 75  int i, j;.  stru
7d200 63 74 20 73 74 61 74 20 62 75 66 3b 0a 20 20 63  ct stat buf;.  c
7d210 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 69 72 20  onst char *zDir 
7d220 3d 20 22 2e 22 3b 0a 20 20 61 7a 44 69 72 73 5b  = ".";.  azDirs[
7d230 30 5d 20 3d 20 73 71 6c 69 74 65 33 5f 74 65 6d  0] = sqlite3_tem
7d240 70 5f 64 69 72 65 63 74 6f 72 79 3b 0a 20 20 66  p_directory;.  f
7d250 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66  or(i=0; i<sizeof
7d260 28 61 7a 44 69 72 73 29 2f 73 69 7a 65 6f 66 28  (azDirs)/sizeof(
7d270 61 7a 44 69 72 73 5b 30 5d 29 3b 20 69 2b 2b 29  azDirs[0]); i++)
7d280 7b 0a 20 20 20 20 69 66 28 20 61 7a 44 69 72 73  {.    if( azDirs
7d290 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75  [i]==0 ) continu
7d2a0 65 3b 0a 20 20 20 20 69 66 28 20 73 74 61 74 28  e;.    if( stat(
7d2b0 61 7a 44 69 72 73 5b 69 5d 2c 20 26 62 75 66 29  azDirs[i], &buf)
7d2c0 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
7d2d0 20 69 66 28 20 21 53 5f 49 53 44 49 52 28 62 75   if( !S_ISDIR(bu
7d2e0 66 2e 73 74 5f 6d 6f 64 65 29 20 29 20 63 6f 6e  f.st_mode) ) con
7d2f0 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 61  tinue;.    if( a
7d300 63 63 65 73 73 28 61 7a 44 69 72 73 5b 69 5d 2c  ccess(azDirs[i],
7d310 20 30 37 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b   07) ) continue;
7d320 0a 20 20 20 20 7a 44 69 72 20 3d 20 61 7a 44 69  .    zDir = azDi
7d330 72 73 5b 69 5d 3b 0a 20 20 20 20 62 72 65 61 6b  rs[i];.    break
7d340 3b 0a 20 20 7d 0a 20 20 64 6f 7b 0a 20 20 20 20  ;.  }.  do{.    
7d350 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
7d360 28 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45  (SQLITE_TEMPNAME
7d370 5f 53 49 5a 45 2c 20 7a 42 75 66 2c 20 22 25 73  _SIZE, zBuf, "%s
7d380 2f 22 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46  /"TEMP_FILE_PREF
7d390 49 58 2c 20 7a 44 69 72 29 3b 0a 20 20 20 20 6a  IX, zDir);.    j
7d3a0 20 3d 20 73 74 72 6c 65 6e 28 7a 42 75 66 29 3b   = strlen(zBuf);
7d3b0 0a 20 20 20 20 73 71 6c 69 74 65 33 52 61 6e 64  .    sqlite3Rand
7d3c0 6f 6d 6e 65 73 73 28 31 35 2c 20 26 7a 42 75 66  omness(15, &zBuf
7d3d0 5b 6a 5d 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d  [j]);.    for(i=
7d3e0 30 3b 20 69 3c 31 35 3b 20 69 2b 2b 2c 20 6a 2b  0; i<15; i++, j+
7d3f0 2b 29 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b 6a  +){.      zBuf[j
7d400 5d 20 3d 20 28 63 68 61 72 29 7a 43 68 61 72 73  ] = (char)zChars
7d410 5b 20 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61  [ ((unsigned cha
7d420 72 29 7a 42 75 66 5b 6a 5d 29 25 28 73 69 7a 65  r)zBuf[j])%(size
7d430 6f 66 28 7a 43 68 61 72 73 29 2d 31 29 20 5d 3b  of(zChars)-1) ];
7d440 0a 20 20 20 20 7d 0a 20 20 20 20 7a 42 75 66 5b  .    }.    zBuf[
7d450 6a 5d 20 3d 20 30 3b 0a 20 20 7d 77 68 69 6c 65  j] = 0;.  }while
7d460 28 20 61 63 63 65 73 73 28 7a 42 75 66 2c 30 29  ( access(zBuf,0)
7d470 3d 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  ==0 );.  return 
7d480 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 0a 2f  SQLITE_OK; .}../
7d490 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 20  *.** Check that 
7d4a0 61 20 67 69 76 65 6e 20 70 61 74 68 6e 61 6d 65  a given pathname
7d4b0 20 69 73 20 61 20 64 69 72 65 63 74 6f 72 79 20   is a directory 
7d4c0 61 6e 64 20 69 73 20 77 72 69 74 61 62 6c 65 20  and is writable 
7d4d0 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  .**.*/.SQLITE_PR
7d4e0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
7d4f0 33 55 6e 69 78 49 73 44 69 72 57 72 69 74 61 62  3UnixIsDirWritab
7d500 6c 65 28 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a  le(char *zBuf){.
7d510 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
7d520 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41  MIT_PAGER_PRAGMA
7d530 53 0a 20 20 73 74 72 75 63 74 20 73 74 61 74 20  S.  struct stat 
7d540 62 75 66 3b 0a 20 20 69 66 28 20 7a 42 75 66 3d  buf;.  if( zBuf=
7d550 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
7d560 20 69 66 28 20 7a 42 75 66 5b 30 5d 3d 3d 30 20   if( zBuf[0]==0 
7d570 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66  ) return 0;.  if
7d580 28 20 73 74 61 74 28 7a 42 75 66 2c 20 26 62 75  ( stat(zBuf, &bu
7d590 66 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  f) ) return 0;. 
7d5a0 20 69 66 28 20 21 53 5f 49 53 44 49 52 28 62 75   if( !S_ISDIR(bu
7d5b0 66 2e 73 74 5f 6d 6f 64 65 29 20 29 20 72 65 74  f.st_mode) ) ret
7d5c0 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 61 63 63  urn 0;.  if( acc
7d5d0 65 73 73 28 7a 42 75 66 2c 20 30 37 29 20 29 20  ess(zBuf, 07) ) 
7d5e0 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66  return 0;.#endif
7d5f0 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
7d600 50 41 47 45 52 5f 50 52 41 47 4d 41 53 20 2a 2f  PAGER_PRAGMAS */
7d610 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a  .  return 1;.}..
7d620 2f 2a 0a 2a 2a 20 53 65 65 6b 20 74 6f 20 74 68  /*.** Seek to th
7d630 65 20 6f 66 66 73 65 74 20 69 6e 20 69 64 2d 3e  e offset in id->
7d640 6f 66 66 73 65 74 20 74 68 65 6e 20 72 65 61 64  offset then read
7d650 20 63 6e 74 20 62 79 74 65 73 20 69 6e 74 6f 20   cnt bytes into 
7d660 70 42 75 66 2e 0a 2a 2a 20 52 65 74 75 72 6e 20  pBuf..** Return 
7d670 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
7d680 74 65 73 20 61 63 74 75 61 6c 6c 79 20 72 65 61  tes actually rea
7d690 64 2e 20 20 55 70 64 61 74 65 20 74 68 65 20 6f  d.  Update the o
7d6a0 66 66 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  ffset..*/.static
7d6b0 20 69 6e 74 20 73 65 65 6b 41 6e 64 52 65 61 64   int seekAndRead
7d6c0 28 75 6e 69 78 46 69 6c 65 20 2a 69 64 2c 20 76  (unixFile *id, v
7d6d0 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 63  oid *pBuf, int c
7d6e0 6e 74 29 7b 0a 20 20 69 6e 74 20 67 6f 74 3b 0a  nt){.  int got;.
7d6f0 20 20 69 36 34 20 6e 65 77 4f 66 66 73 65 74 3b    i64 newOffset;
7d700 0a 20 20 54 49 4d 45 52 5f 53 54 41 52 54 3b 0a  .  TIMER_START;.
7d710 23 69 66 20 64 65 66 69 6e 65 64 28 55 53 45 5f  #if defined(USE_
7d720 50 52 45 41 44 29 0a 20 20 67 6f 74 20 3d 20 70  PREAD).  got = p
7d730 72 65 61 64 28 69 64 2d 3e 68 2c 20 70 42 75 66  read(id->h, pBuf
7d740 2c 20 63 6e 74 2c 20 69 64 2d 3e 6f 66 66 73 65  , cnt, id->offse
7d750 74 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f  t);.  SimulateIO
7d760 45 72 72 6f 72 28 20 67 6f 74 20 3d 20 2d 31 20  Error( got = -1 
7d770 29 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64  );.#elif defined
7d780 28 55 53 45 5f 50 52 45 41 44 36 34 29 0a 20 20  (USE_PREAD64).  
7d790 67 6f 74 20 3d 20 70 72 65 61 64 36 34 28 69 64  got = pread64(id
7d7a0 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20  ->h, pBuf, cnt, 
7d7b0 69 64 2d 3e 6f 66 66 73 65 74 29 3b 0a 20 20 53  id->offset);.  S
7d7c0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20  imulateIOError( 
7d7d0 67 6f 74 20 3d 20 2d 31 20 29 3b 0a 23 65 6c 73  got = -1 );.#els
7d7e0 65 0a 20 20 6e 65 77 4f 66 66 73 65 74 20 3d 20  e.  newOffset = 
7d7f0 6c 73 65 65 6b 28 69 64 2d 3e 68 2c 20 69 64 2d  lseek(id->h, id-
7d800 3e 6f 66 66 73 65 74 2c 20 53 45 45 4b 5f 53 45  >offset, SEEK_SE
7d810 54 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f  T);.  SimulateIO
7d820 45 72 72 6f 72 28 20 6e 65 77 4f 66 66 73 65 74  Error( newOffset
7d830 2d 2d 20 29 3b 0a 20 20 69 66 28 20 6e 65 77 4f  -- );.  if( newO
7d840 66 66 73 65 74 21 3d 69 64 2d 3e 6f 66 66 73 65  ffset!=id->offse
7d850 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  t ){.    return 
7d860 2d 31 3b 0a 20 20 7d 0a 20 20 67 6f 74 20 3d 20  -1;.  }.  got = 
7d870 72 65 61 64 28 69 64 2d 3e 68 2c 20 70 42 75 66  read(id->h, pBuf
7d880 2c 20 63 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 20  , cnt);.#endif. 
7d890 20 54 49 4d 45 52 5f 45 4e 44 3b 0a 20 20 4f 53   TIMER_END;.  OS
7d8a0 54 52 41 43 45 35 28 22 52 45 41 44 20 20 20 20  TRACE5("READ    
7d8b0 25 2d 33 64 20 25 35 64 20 25 37 6c 6c 64 20 25  %-3d %5d %7lld %
7d8c0 64 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 67 6f 74  d\n", id->h, got
7d8d0 2c 20 69 64 2d 3e 6f 66 66 73 65 74 2c 20 54 49  , id->offset, TI
7d8e0 4d 45 52 5f 45 4c 41 50 53 45 44 29 3b 0a 20 20  MER_ELAPSED);.  
7d8f0 69 66 28 20 67 6f 74 3e 30 20 29 7b 0a 20 20 20  if( got>0 ){.   
7d900 20 69 64 2d 3e 6f 66 66 73 65 74 20 2b 3d 20 67   id->offset += g
7d910 6f 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ot;.  }.  return
7d920 20 67 6f 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52   got;.}../*.** R
7d930 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61 20  ead data from a 
7d940 66 69 6c 65 20 69 6e 74 6f 20 61 20 62 75 66 66  file into a buff
7d950 65 72 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49  er.  Return SQLI
7d960 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20  TE_OK if all.** 
7d970 62 79 74 65 73 20 77 65 72 65 20 72 65 61 64 20  bytes were read 
7d980 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61 6e 64  successfully and
7d990 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 69 66   SQLITE_IOERR if
7d9a0 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a   anything goes.*
7d9b0 2a 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74  * wrong..*/.stat
7d9c0 69 63 20 69 6e 74 20 75 6e 69 78 52 65 61 64 28  ic int unixRead(
7d9d0 4f 73 46 69 6c 65 20 2a 69 64 2c 20 76 6f 69 64  OsFile *id, void
7d9e0 20 2a 70 42 75 66 2c 20 69 6e 74 20 61 6d 74 29   *pBuf, int amt)
7d9f0 7b 0a 20 20 69 6e 74 20 67 6f 74 3b 0a 20 20 61  {.  int got;.  a
7da00 73 73 65 72 74 28 20 69 64 20 29 3b 0a 20 20 67  ssert( id );.  g
7da10 6f 74 20 3d 20 73 65 65 6b 41 6e 64 52 65 61 64  ot = seekAndRead
7da20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 2c 20  ((unixFile*)id, 
7da30 70 42 75 66 2c 20 61 6d 74 29 3b 0a 20 20 69 66  pBuf, amt);.  if
7da40 28 20 67 6f 74 3d 3d 61 6d 74 20 29 7b 0a 20 20  ( got==amt ){.  
7da50 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
7da60 4f 4b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  OK;.  }else if( 
7da70 67 6f 74 3c 30 20 29 7b 0a 20 20 20 20 72 65 74  got<0 ){.    ret
7da80 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
7da90 5f 52 45 41 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a  _READ;.  }else{.
7daa0 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63 68      memset(&((ch
7dab0 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c 20  ar*)pBuf)[got], 
7dac0 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 20  0, amt-got);.   
7dad0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
7dae0 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b  OERR_SHORT_READ;
7daf0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65  .  }.}../*.** Se
7db00 65 6b 20 74 6f 20 74 68 65 20 6f 66 66 73 65 74  ek to the offset
7db10 20 69 6e 20 69 64 2d 3e 6f 66 66 73 65 74 20 74   in id->offset t
7db20 68 65 6e 20 72 65 61 64 20 63 6e 74 20 62 79 74  hen read cnt byt
7db30 65 73 20 69 6e 74 6f 20 70 42 75 66 2e 0a 2a 2a  es into pBuf..**
7db40 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
7db50 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 74 75  er of bytes actu
7db60 61 6c 6c 79 20 72 65 61 64 2e 20 20 55 70 64 61  ally read.  Upda
7db70 74 65 20 74 68 65 20 6f 66 66 73 65 74 2e 0a 2a  te the offset..*
7db80 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 65  /.static int see
7db90 6b 41 6e 64 57 72 69 74 65 28 75 6e 69 78 46 69  kAndWrite(unixFi
7dba0 6c 65 20 2a 69 64 2c 20 63 6f 6e 73 74 20 76 6f  le *id, const vo
7dbb0 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 63 6e  id *pBuf, int cn
7dbc0 74 29 7b 0a 20 20 69 6e 74 20 67 6f 74 3b 0a 20  t){.  int got;. 
7dbd0 20 69 36 34 20 6e 65 77 4f 66 66 73 65 74 3b 0a   i64 newOffset;.
7dbe0 20 20 54 49 4d 45 52 5f 53 54 41 52 54 3b 0a 23    TIMER_START;.#
7dbf0 69 66 20 64 65 66 69 6e 65 64 28 55 53 45 5f 50  if defined(USE_P
7dc00 52 45 41 44 29 0a 20 20 67 6f 74 20 3d 20 70 77  READ).  got = pw
7dc10 72 69 74 65 28 69 64 2d 3e 68 2c 20 70 42 75 66  rite(id->h, pBuf
7dc20 2c 20 63 6e 74 2c 20 69 64 2d 3e 6f 66 66 73 65  , cnt, id->offse
7dc30 74 29 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65  t);.#elif define
7dc40 64 28 55 53 45 5f 50 52 45 41 44 36 34 29 0a 20  d(USE_PREAD64). 
7dc50 20 67 6f 74 20 3d 20 70 77 72 69 74 65 36 34 28   got = pwrite64(
7dc60 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74  id->h, pBuf, cnt
7dc70 2c 20 69 64 2d 3e 6f 66 66 73 65 74 29 3b 0a 23  , id->offset);.#
7dc80 65 6c 73 65 0a 20 20 6e 65 77 4f 66 66 73 65 74  else.  newOffset
7dc90 20 3d 20 6c 73 65 65 6b 28 69 64 2d 3e 68 2c 20   = lseek(id->h, 
7dca0 69 64 2d 3e 6f 66 66 73 65 74 2c 20 53 45 45 4b  id->offset, SEEK
7dcb0 5f 53 45 54 29 3b 0a 20 20 69 66 28 20 6e 65 77  _SET);.  if( new
7dcc0 4f 66 66 73 65 74 21 3d 69 64 2d 3e 6f 66 66 73  Offset!=id->offs
7dcd0 65 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  et ){.    return
7dce0 20 2d 31 3b 0a 20 20 7d 0a 20 20 67 6f 74 20 3d   -1;.  }.  got =
7dcf0 20 77 72 69 74 65 28 69 64 2d 3e 68 2c 20 70 42   write(id->h, pB
7dd00 75 66 2c 20 63 6e 74 29 3b 0a 23 65 6e 64 69 66  uf, cnt);.#endif
7dd10 0a 20 20 54 49 4d 45 52 5f 45 4e 44 3b 0a 20 20  .  TIMER_END;.  
7dd20 4f 53 54 52 41 43 45 35 28 22 57 52 49 54 45 20  OSTRACE5("WRITE 
7dd30 20 20 25 2d 33 64 20 25 35 64 20 25 37 6c 6c 64    %-3d %5d %7lld
7dd40 20 25 64 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 67   %d\n", id->h, g
7dd50 6f 74 2c 20 69 64 2d 3e 6f 66 66 73 65 74 2c 20  ot, id->offset, 
7dd60 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 29 3b 0a  TIMER_ELAPSED);.
7dd70 20 20 69 66 28 20 67 6f 74 3e 30 20 29 7b 0a 20    if( got>0 ){. 
7dd80 20 20 20 69 64 2d 3e 6f 66 66 73 65 74 20 2b 3d     id->offset +=
7dd90 20 67 6f 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75   got;.  }.  retu
7dda0 72 6e 20 67 6f 74 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  rn got;.}.../*.*
7ddb0 2a 20 57 72 69 74 65 20 64 61 74 61 20 66 72 6f  * Write data fro
7ddc0 6d 20 61 20 62 75 66 66 65 72 20 69 6e 74 6f 20  m a buffer into 
7ddd0 61 20 66 69 6c 65 2e 20 20 52 65 74 75 72 6e 20  a file.  Return 
7dde0 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63  SQLITE_OK on suc
7ddf0 63 65 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20  cess.** or some 
7de00 6f 74 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65  other error code
7de10 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a   on failure..*/.
7de20 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 57  static int unixW
7de30 72 69 74 65 28 4f 73 46 69 6c 65 20 2a 69 64 2c  rite(OsFile *id,
7de40 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75   const void *pBu
7de50 66 2c 20 69 6e 74 20 61 6d 74 29 7b 0a 20 20 69  f, int amt){.  i
7de60 6e 74 20 77 72 6f 74 65 20 3d 20 30 3b 0a 20 20  nt wrote = 0;.  
7de70 61 73 73 65 72 74 28 20 69 64 20 29 3b 0a 20 20  assert( id );.  
7de80 61 73 73 65 72 74 28 20 61 6d 74 3e 30 20 29 3b  assert( amt>0 );
7de90 0a 20 20 77 68 69 6c 65 28 20 61 6d 74 3e 30 20  .  while( amt>0 
7dea0 26 26 20 28 77 72 6f 74 65 20 3d 20 73 65 65 6b  && (wrote = seek
7deb0 41 6e 64 57 72 69 74 65 28 28 75 6e 69 78 46 69  AndWrite((unixFi
7dec0 6c 65 2a 29 69 64 2c 20 70 42 75 66 2c 20 61 6d  le*)id, pBuf, am
7ded0 74 29 29 3e 30 20 29 7b 0a 20 20 20 20 61 6d 74  t))>0 ){.    amt
7dee0 20 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20 70   -= wrote;.    p
7def0 42 75 66 20 3d 20 26 28 28 63 68 61 72 2a 29 70  Buf = &((char*)p
7df00 42 75 66 29 5b 77 72 6f 74 65 5d 3b 0a 20 20 7d  Buf)[wrote];.  }
7df10 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  .  SimulateIOErr
7df20 6f 72 28 28 20 77 72 6f 74 65 3d 28 2d 31 29 2c  or(( wrote=(-1),
7df30 20 61 6d 74 3d 31 20 29 29 3b 0a 20 20 53 69 6d   amt=1 ));.  Sim
7df40 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72  ulateDiskfullErr
7df50 6f 72 28 28 20 77 72 6f 74 65 3d 30 2c 20 61 6d  or(( wrote=0, am
7df60 74 3d 31 20 29 29 3b 0a 20 20 69 66 28 20 61 6d  t=1 ));.  if( am
7df70 74 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 77  t>0 ){.    if( w
7df80 72 6f 74 65 3c 30 20 29 7b 0a 20 20 20 20 20 20  rote<0 ){.      
7df90 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
7dfa0 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 20 7d  ERR_WRITE;.    }
7dfb0 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75  else{.      retu
7dfc0 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a  rn SQLITE_FULL;.
7dfd0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
7dfe0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
7dff0 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20  ./*.** Move the 
7e000 72 65 61 64 2f 77 72 69 74 65 20 70 6f 69 6e 74  read/write point
7e010 65 72 20 69 6e 20 61 20 66 69 6c 65 2e 0a 2a 2f  er in a file..*/
7e020 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78  .static int unix
7e030 53 65 65 6b 28 4f 73 46 69 6c 65 20 2a 69 64 2c  Seek(OsFile *id,
7e040 20 69 36 34 20 6f 66 66 73 65 74 29 7b 0a 20 20   i64 offset){.  
7e050 61 73 73 65 72 74 28 20 69 64 20 29 3b 0a 23 69  assert( id );.#i
7e060 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
7e070 0a 20 20 69 66 28 20 6f 66 66 73 65 74 20 29 20  .  if( offset ) 
7e080 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c  SimulateDiskfull
7e090 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c  Error(return SQL
7e0a0 49 54 45 5f 46 55 4c 4c 29 3b 0a 23 65 6e 64 69  ITE_FULL);.#endi
7e0b0 66 0a 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29  f.  ((unixFile*)
7e0c0 69 64 29 2d 3e 6f 66 66 73 65 74 20 3d 20 6f 66  id)->offset = of
7e0d0 66 73 65 74 3b 0a 20 20 72 65 74 75 72 6e 20 53  fset;.  return S
7e0e0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66  QLITE_OK;.}..#if
7e0f0 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
7e100 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 68 65 20  /*.** Count the 
7e110 6e 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c 73 79  number of fullsy
7e120 6e 63 73 20 61 6e 64 20 6e 6f 72 6d 61 6c 20 73  ncs and normal s
7e130 79 6e 63 73 2e 20 20 54 68 69 73 20 69 73 20 75  yncs.  This is u
7e140 73 65 64 20 74 6f 20 74 65 73 74 0a 2a 2a 20 74  sed to test.** t
7e150 68 61 74 20 73 79 6e 63 73 20 61 6e 64 20 66 75  hat syncs and fu
7e160 6c 6c 73 79 6e 63 73 20 61 72 65 20 6f 63 63 75  llsyncs are occu
7e170 72 69 6e 67 20 61 74 20 74 68 65 20 72 69 67 68  ring at the righ
7e180 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a 69 6e 74 20  t times..*/.int 
7e190 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75  sqlite3_sync_cou
7e1a0 6e 74 20 3d 20 30 3b 0a 69 6e 74 20 73 71 6c 69  nt = 0;.int sqli
7e1b0 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75  te3_fullsync_cou
7e1c0 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a  nt = 0;.#endif..
7e1d0 2f 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 66 64  /*.** Use the fd
7e1e0 61 74 61 73 79 6e 63 28 29 20 41 50 49 20 6f 6e  atasync() API on
7e1f0 6c 79 20 69 66 20 74 68 65 20 48 41 56 45 5f 46  ly if the HAVE_F
7e200 44 41 54 41 53 59 4e 43 20 6d 61 63 72 6f 20 69  DATASYNC macro i
7e210 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 4f 74  s defined..** Ot
7e220 68 65 72 77 69 73 65 20 75 73 65 20 66 73 79 6e  herwise use fsyn
7e230 63 28 29 20 69 6e 20 69 74 73 20 70 6c 61 63 65  c() in its place
7e240 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 48 41 56  ..*/.#ifndef HAV
7e250 45 5f 46 44 41 54 41 53 59 4e 43 0a 23 20 64 65  E_FDATASYNC.# de
7e260 66 69 6e 65 20 66 64 61 74 61 73 79 6e 63 20 66  fine fdatasync f
7e270 73 79 6e 63 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  sync.#endif../*.
7e280 2a 2a 20 44 65 66 69 6e 65 20 48 41 56 45 5f 46  ** Define HAVE_F
7e290 55 4c 4c 46 53 59 4e 43 20 74 6f 20 30 20 6f 72  ULLFSYNC to 0 or
7e2a0 20 31 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20   1 depending on 
7e2b0 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 0a 2a  whether or not.*
7e2c0 2a 20 74 68 65 20 46 5f 46 55 4c 4c 46 53 59 4e  * the F_FULLFSYN
7e2d0 43 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e  C macro is defin
7e2e0 65 64 2e 20 20 46 5f 46 55 4c 4c 46 53 59 4e 43  ed.  F_FULLFSYNC
7e2f0 20 69 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a   is currently.**
7e300 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20   only available 
7e310 6f 6e 20 4d 61 63 20 4f 53 20 58 2e 20 20 42 75  on Mac OS X.  Bu
7e320 74 20 74 68 61 74 20 63 6f 75 6c 64 20 63 68 61  t that could cha
7e330 6e 67 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 46  nge..*/.#ifdef F
7e340 5f 46 55 4c 4c 46 53 59 4e 43 0a 23 20 64 65 66  _FULLFSYNC.# def
7e350 69 6e 65 20 48 41 56 45 5f 46 55 4c 4c 46 53 59  ine HAVE_FULLFSY
7e360 4e 43 20 31 0a 23 65 6c 73 65 0a 23 20 64 65 66  NC 1.#else.# def
7e370 69 6e 65 20 48 41 56 45 5f 46 55 4c 4c 46 53 59  ine HAVE_FULLFSY
7e380 4e 43 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  NC 0.#endif.../*
7e390 0a 2a 2a 20 54 68 65 20 66 73 79 6e 63 28 29 20  .** The fsync() 
7e3a0 73 79 73 74 65 6d 20 63 61 6c 6c 20 64 6f 65 73  system call does
7e3b0 20 6e 6f 74 20 77 6f 72 6b 20 61 73 20 61 64 76   not work as adv
7e3c0 65 72 74 69 73 65 64 20 6f 6e 20 6d 61 6e 79 0a  ertised on many.
7e3d0 2a 2a 20 75 6e 69 78 20 73 79 73 74 65 6d 73 2e  ** unix systems.
7e3e0 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
7e3f0 70 72 6f 63 65 64 75 72 65 20 69 73 20 61 6e 20  procedure is an 
7e400 61 74 74 65 6d 70 74 20 74 6f 20 6d 61 6b 65 0a  attempt to make.
7e410 2a 2a 20 69 74 20 77 6f 72 6b 20 62 65 74 74 65  ** it work bette
7e420 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c  r..**.** The SQL
7e430 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 6d 61 63 72  ITE_NO_SYNC macr
7e440 6f 20 64 69 73 61 62 6c 65 73 20 61 6c 6c 20 66  o disables all f
7e450 73 79 6e 63 28 29 73 2e 20 20 54 68 69 73 20 69  sync()s.  This i
7e460 73 20 75 73 65 66 75 6c 0a 2a 2a 20 66 6f 72 20  s useful.** for 
7e470 74 65 73 74 69 6e 67 20 77 68 65 6e 20 77 65 20  testing when we 
7e480 77 61 6e 74 20 74 6f 20 72 75 6e 20 74 68 72 6f  want to run thro
7e490 75 67 68 20 74 68 65 20 74 65 73 74 20 73 75 69  ugh the test sui
7e4a0 74 65 20 71 75 69 63 6b 6c 79 2e 0a 2a 2a 20 59  te quickly..** Y
7e4b0 6f 75 20 61 72 65 20 73 74 72 6f 6e 67 6c 79 20  ou are strongly 
7e4c0 61 64 76 69 73 65 64 20 2a 6e 6f 74 2a 20 74 6f  advised *not* to
7e4d0 20 64 65 70 6c 6f 79 20 77 69 74 68 20 53 51 4c   deploy with SQL
7e4e0 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 2a 2a 20 65  ITE_NO_SYNC.** e
7e4f0 6e 61 62 6c 65 64 2c 20 68 6f 77 65 76 65 72 2c  nabled, however,
7e500 20 73 69 6e 63 65 20 77 69 74 68 20 53 51 4c 49   since with SQLI
7e510 54 45 5f 4e 4f 5f 53 59 4e 43 20 65 6e 61 62 6c  TE_NO_SYNC enabl
7e520 65 64 2c 20 61 6e 20 4f 53 20 63 72 61 73 68 0a  ed, an OS crash.
7e530 2a 2a 20 6f 72 20 70 6f 77 65 72 20 66 61 69 6c  ** or power fail
7e540 75 72 65 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20  ure will likely 
7e550 63 6f 72 72 75 70 74 20 74 68 65 20 64 61 74 61  corrupt the data
7e560 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74  base file..*/.st
7e570 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 5f 66 73  atic int full_fs
7e580 79 6e 63 28 69 6e 74 20 66 64 2c 20 69 6e 74 20  ync(int fd, int 
7e590 66 75 6c 6c 53 79 6e 63 2c 20 69 6e 74 20 64 61  fullSync, int da
7e5a0 74 61 4f 6e 6c 79 29 7b 0a 20 20 69 6e 74 20 72  taOnly){.  int r
7e5b0 63 3b 0a 0a 20 20 2f 2a 20 52 65 63 6f 72 64 20  c;..  /* Record 
7e5c0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 69  the number of ti
7e5d0 6d 65 73 20 74 68 61 74 20 77 65 20 64 6f 20 61  mes that we do a
7e5e0 20 6e 6f 72 6d 61 6c 20 66 73 79 6e 63 28 29 20   normal fsync() 
7e5f0 61 6e 64 20 0a 20 20 2a 2a 20 46 55 4c 4c 53 59  and .  ** FULLSY
7e600 4e 43 2e 20 20 54 68 69 73 20 69 73 20 75 73 65  NC.  This is use
7e610 64 20 64 75 72 69 6e 67 20 74 65 73 74 69 6e 67  d during testing
7e620 20 74 6f 20 76 65 72 69 66 79 20 74 68 61 74 20   to verify that 
7e630 74 68 69 73 20 70 72 6f 63 65 64 75 72 65 0a 20  this procedure. 
7e640 20 2a 2a 20 67 65 74 73 20 63 61 6c 6c 65 64 20   ** gets called 
7e650 77 69 74 68 20 74 68 65 20 63 6f 72 72 65 63 74  with the correct
7e660 20 61 72 67 75 6d 65 6e 74 73 2e 0a 20 20 2a 2f   arguments..  */
7e670 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
7e680 45 53 54 0a 20 20 69 66 28 20 66 75 6c 6c 53 79  EST.  if( fullSy
7e690 6e 63 20 29 20 73 71 6c 69 74 65 33 5f 66 75 6c  nc ) sqlite3_ful
7e6a0 6c 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 20  lsync_count++;. 
7e6b0 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f   sqlite3_sync_co
7e6c0 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 0a 20  unt++;.#endif.. 
7e6d0 20 2f 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c   /* If we compil
7e6e0 65 64 20 77 69 74 68 20 74 68 65 20 53 51 4c 49  ed with the SQLI
7e6f0 54 45 5f 4e 4f 5f 53 59 4e 43 20 66 6c 61 67 2c  TE_NO_SYNC flag,
7e700 20 74 68 65 6e 20 73 79 6e 63 69 6e 67 20 69 73   then syncing is
7e710 20 61 0a 20 20 2a 2a 20 6e 6f 2d 6f 70 0a 20 20   a.  ** no-op.  
7e720 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
7e730 5f 4e 4f 5f 53 59 4e 43 0a 20 20 72 63 20 3d 20  _NO_SYNC.  rc = 
7e740 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6c 73 65  SQLITE_OK;.#else
7e750 0a 0a 23 69 66 20 48 41 56 45 5f 46 55 4c 4c 46  ..#if HAVE_FULLF
7e760 53 59 4e 43 0a 20 20 69 66 28 20 66 75 6c 6c 53  SYNC.  if( fullS
7e770 79 6e 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ync ){.    rc = 
7e780 66 63 6e 74 6c 28 66 64 2c 20 46 5f 46 55 4c 4c  fcntl(fd, F_FULL
7e790 46 53 59 4e 43 2c 20 30 29 3b 0a 20 20 7d 65 6c  FSYNC, 0);.  }el
7e7a0 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a  se{.    rc = 1;.
7e7b0 20 20 7d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20    }.  /* If the 
7e7c0 46 55 4c 4c 46 53 59 4e 43 20 66 61 69 6c 65 64  FULLFSYNC failed
7e7d0 2c 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 61  , fall back to a
7e7e0 74 74 65 6d 70 74 69 6e 67 20 61 6e 20 66 73 79  ttempting an fsy
7e7f0 6e 63 28 29 2e 0a 20 20 20 2a 20 49 74 20 73 68  nc()..   * It sh
7e800 6f 75 6c 64 6e 27 74 20 62 65 20 70 6f 73 73 69  ouldn't be possi
7e810 62 6c 65 20 66 6f 72 20 66 75 6c 6c 66 73 79 6e  ble for fullfsyn
7e820 63 20 74 6f 20 66 61 69 6c 20 6f 6e 20 74 68 65  c to fail on the
7e830 20 6c 6f 63 61 6c 20 0a 20 20 20 2a 20 66 69 6c   local .   * fil
7e840 65 20 73 79 73 74 65 6d 20 28 6f 6e 20 4f 53 58  e system (on OSX
7e850 29 2c 20 73 6f 20 66 61 69 6c 75 72 65 20 69 6e  ), so failure in
7e860 64 69 63 61 74 65 73 20 74 68 61 74 20 46 55 4c  dicates that FUL
7e870 4c 46 53 59 4e 43 0a 20 20 20 2a 20 69 73 6e 27  LFSYNC.   * isn'
7e880 74 20 73 75 70 70 6f 72 74 65 64 20 66 6f 72 20  t supported for 
7e890 74 68 69 73 20 66 69 6c 65 20 73 79 73 74 65 6d  this file system
7e8a0 2e 20 53 6f 2c 20 61 74 74 65 6d 70 74 20 61 6e  . So, attempt an
7e8b0 20 66 73 79 6e 63 20 0a 20 20 20 2a 20 61 6e 64   fsync .   * and
7e8c0 20 28 66 6f 72 20 6e 6f 77 29 20 69 67 6e 6f 72   (for now) ignor
7e8d0 65 20 74 68 65 20 6f 76 65 72 68 65 61 64 20 6f  e the overhead o
7e8e0 66 20 61 20 73 75 70 65 72 66 6c 75 6f 75 73 20  f a superfluous 
7e8f0 66 63 6e 74 6c 20 63 61 6c 6c 2e 20 20 0a 20 20  fcntl call.  .  
7e900 20 2a 20 49 74 27 64 20 62 65 20 62 65 74 74 65   * It'd be bette
7e910 72 20 74 6f 20 64 65 74 65 63 74 20 66 75 6c 6c  r to detect full
7e920 66 73 79 6e 63 20 73 75 70 70 6f 72 74 20 6f 6e  fsync support on
7e930 63 65 20 61 6e 64 20 61 76 6f 69 64 20 0a 20 20  ce and avoid .  
7e940 20 2a 20 74 68 65 20 66 63 6e 74 6c 20 63 61 6c   * the fcntl cal
7e950 6c 20 65 76 65 72 79 20 74 69 6d 65 20 73 79 6e  l every time syn
7e960 63 20 69 73 20 63 61 6c 6c 65 64 2e 0a 20 20 20  c is called..   
7e970 2a 2f 0a 20 20 69 66 28 20 72 63 20 29 20 72 63  */.  if( rc ) rc
7e980 20 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 0a 23   = fsync(fd);..#
7e990 65 6c 73 65 20 0a 20 20 69 66 28 20 64 61 74 61  else .  if( data
7e9a0 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 72 63 20 3d  Only ){.    rc =
7e9b0 20 66 64 61 74 61 73 79 6e 63 28 66 64 29 3b 0a   fdatasync(fd);.
7e9c0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
7e9d0 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 20 20 7d  = fsync(fd);.  }
7e9e0 0a 23 65 6e 64 69 66 20 2f 2a 20 48 41 56 45 5f  .#endif /* HAVE_
7e9f0 46 55 4c 4c 46 53 59 4e 43 20 2a 2f 0a 23 65 6e  FULLFSYNC */.#en
7ea00 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53  dif /* defined(S
7ea10 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 29 20 2a  QLITE_NO_SYNC) *
7ea20 2f 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  /..  return rc;.
7ea30 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75  }../*.** Make su
7ea40 72 65 20 61 6c 6c 20 77 72 69 74 65 73 20 74 6f  re all writes to
7ea50 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 66 69   a particular fi
7ea60 6c 65 20 61 72 65 20 63 6f 6d 6d 69 74 74 65 64  le are committed
7ea70 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20   to disk..**.** 
7ea80 49 66 20 64 61 74 61 4f 6e 6c 79 3d 3d 30 20 74  If dataOnly==0 t
7ea90 68 65 6e 20 62 6f 74 68 20 74 68 65 20 66 69 6c  hen both the fil
7eaa0 65 20 69 74 73 65 6c 66 20 61 6e 64 20 69 74 73  e itself and its
7eab0 20 6d 65 74 61 64 61 74 61 20 28 66 69 6c 65 0a   metadata (file.
7eac0 2a 2a 20 73 69 7a 65 2c 20 61 63 63 65 73 73 20  ** size, access 
7ead0 74 69 6d 65 2c 20 65 74 63 29 20 61 72 65 20 73  time, etc) are s
7eae0 79 6e 63 65 64 2e 20 20 49 66 20 64 61 74 61 4f  ynced.  If dataO
7eaf0 6e 6c 79 21 3d 30 20 74 68 65 6e 20 6f 6e 6c 79  nly!=0 then only
7eb00 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 64 61 74   the.** file dat
7eb10 61 20 69 73 20 73 79 6e 63 65 64 2e 0a 2a 2a 0a  a is synced..**.
7eb20 2a 2a 20 55 6e 64 65 72 20 55 6e 69 78 2c 20 61  ** Under Unix, a
7eb30 6c 73 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68  lso make sure th
7eb40 61 74 20 74 68 65 20 64 69 72 65 63 74 6f 72 79  at the directory
7eb50 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 66   entry for the f
7eb60 69 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20  ile.** has been 
7eb70 63 72 65 61 74 65 64 20 62 79 20 66 73 79 6e 63  created by fsync
7eb80 2d 69 6e 67 20 74 68 65 20 64 69 72 65 63 74 6f  -ing the directo
7eb90 72 79 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  ry that contains
7eba0 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 49 66   the file..** If
7ebb0 20 77 65 20 64 6f 20 6e 6f 74 20 64 6f 20 74 68   we do not do th
7ebc0 69 73 20 61 6e 64 20 77 65 20 65 6e 63 6f 75 6e  is and we encoun
7ebd0 74 65 72 20 61 20 70 6f 77 65 72 20 66 61 69 6c  ter a power fail
7ebe0 75 72 65 2c 20 74 68 65 20 64 69 72 65 63 74 6f  ure, the directo
7ebf0 72 79 0a 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20  ry.** entry for 
7ec00 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68  the journal migh
7ec10 74 20 6e 6f 74 20 65 78 69 73 74 20 61 66 74 65  t not exist afte
7ec20 72 20 77 65 20 72 65 62 6f 6f 74 2e 20 20 54 68  r we reboot.  Th
7ec30 65 20 6e 65 78 74 0a 2a 2a 20 53 51 4c 69 74 65  e next.** SQLite
7ec40 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 66   to access the f
7ec50 69 6c 65 20 77 69 6c 6c 20 6e 6f 74 20 6b 6e 6f  ile will not kno
7ec60 77 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e  w that the journ
7ec70 61 6c 20 65 78 69 73 74 73 20 28 62 65 63 61 75  al exists (becau
7ec80 73 65 0a 2a 2a 20 74 68 65 20 64 69 72 65 63 74  se.** the direct
7ec90 6f 72 79 20 65 6e 74 72 79 20 66 6f 72 20 74 68  ory entry for th
7eca0 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 6e 65  e journal was ne
7ecb0 76 65 72 20 63 72 65 61 74 65 64 29 20 61 6e 64  ver created) and
7ecc0 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
7ecd0 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 72 6f 6c  .** will not rol
7ece0 6c 20 62 61 63 6b 20 2d 20 70 6f 73 73 69 62 6c  l back - possibl
7ecf0 79 20 6c 65 61 64 69 6e 67 20 74 6f 20 64 61 74  y leading to dat
7ed00 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e  abase corruption
7ed10 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
7ed20 75 6e 69 78 53 79 6e 63 28 4f 73 46 69 6c 65 20  unixSync(OsFile 
7ed30 2a 69 64 2c 20 69 6e 74 20 64 61 74 61 4f 6e 6c  *id, int dataOnl
7ed40 79 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  y){.  int rc;.  
7ed50 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
7ed60 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b  = (unixFile*)id;
7ed70 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65  .  assert( pFile
7ed80 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 22   );.  OSTRACE2("
7ed90 53 59 4e 43 20 20 20 20 25 2d 33 64 5c 6e 22 2c  SYNC    %-3d\n",
7eda0 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 72 63   pFile->h);.  rc
7edb0 20 3d 20 66 75 6c 6c 5f 66 73 79 6e 63 28 70 46   = full_fsync(pF
7edc0 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 66  ile->h, pFile->f
7edd0 75 6c 6c 53 79 6e 63 2c 20 64 61 74 61 4f 6e 6c  ullSync, dataOnl
7ede0 79 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f  y);.  SimulateIO
7edf0 45 72 72 6f 72 28 20 72 63 3d 31 20 29 3b 0a 20  Error( rc=1 );. 
7ee00 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72   if( rc ){.    r
7ee10 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
7ee20 52 52 5f 46 53 59 4e 43 3b 0a 20 20 7d 0a 20 20  RR_FSYNC;.  }.  
7ee30 69 66 28 20 70 46 69 6c 65 2d 3e 64 69 72 66 64  if( pFile->dirfd
7ee40 3e 3d 30 20 29 7b 0a 20 20 20 20 4f 53 54 52 41  >=0 ){.    OSTRA
7ee50 43 45 34 28 22 44 49 52 53 59 4e 43 20 25 2d 33  CE4("DIRSYNC %-3
7ee60 64 20 28 68 61 76 65 5f 66 75 6c 6c 66 73 79 6e  d (have_fullfsyn
7ee70 63 3d 25 64 20 66 75 6c 6c 73 79 6e 63 3d 25 64  c=%d fullsync=%d
7ee80 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 64 69 72  )\n", pFile->dir
7ee90 66 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  fd,.            
7eea0 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 2c 20  HAVE_FULLFSYNC, 
7eeb0 70 46 69 6c 65 2d 3e 66 75 6c 6c 53 79 6e 63 29  pFile->fullSync)
7eec0 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
7eed0 5f 44 49 53 41 42 4c 45 5f 44 49 52 53 59 4e 43  _DISABLE_DIRSYNC
7eee0 0a 20 20 20 20 2f 2a 20 54 68 65 20 64 69 72 65  .    /* The dire
7eef0 63 74 6f 72 79 20 73 79 6e 63 20 69 73 20 6f 6e  ctory sync is on
7ef00 6c 79 20 61 74 74 65 6d 70 74 65 64 20 69 66 20  ly attempted if 
7ef10 66 75 6c 6c 5f 66 73 79 6e 63 20 69 73 0a 20 20  full_fsync is.  
7ef20 20 20 2a 2a 20 74 75 72 6e 65 64 20 6f 66 66 20    ** turned off 
7ef30 6f 72 20 75 6e 61 76 61 69 6c 61 62 6c 65 2e 20  or unavailable. 
7ef40 20 49 66 20 61 20 66 75 6c 6c 5f 66 73 79 6e 63   If a full_fsync
7ef50 20 6f 63 63 75 72 72 65 64 20 61 62 6f 76 65 2c   occurred above,
7ef60 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65  .    ** then the
7ef70 20 64 69 72 65 63 74 6f 72 79 20 73 79 6e 63 20   directory sync 
7ef80 69 73 20 73 75 70 65 72 66 6c 75 6f 75 73 2e 0a  is superfluous..
7ef90 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 28      */.    if( (
7efa0 21 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20  !HAVE_FULLFSYNC 
7efb0 7c 7c 20 21 70 46 69 6c 65 2d 3e 66 75 6c 6c 53  || !pFile->fullS
7efc0 79 6e 63 29 20 26 26 20 66 75 6c 6c 5f 66 73 79  ync) && full_fsy
7efd0 6e 63 28 70 46 69 6c 65 2d 3e 64 69 72 66 64 2c  nc(pFile->dirfd,
7efe0 30 2c 30 29 20 29 7b 0a 20 20 20 20 20 20 20 2f  0,0) ){.       /
7eff0 2a 0a 20 20 20 20 20 20 20 2a 2a 20 57 65 20 68  *.       ** We h
7f000 61 76 65 20 72 65 63 65 69 76 65 64 20 6d 75 6c  ave received mul
7f010 74 69 70 6c 65 20 72 65 70 6f 72 74 73 20 6f 66  tiple reports of
7f020 20 66 73 79 6e 63 28 29 20 72 65 74 75 72 6e 69   fsync() returni
7f030 6e 67 0a 20 20 20 20 20 20 20 2a 2a 20 65 72 72  ng.       ** err
7f040 6f 72 73 20 77 68 65 6e 20 61 70 70 6c 69 65 64  ors when applied
7f050 20 74 6f 20 64 69 72 65 63 74 6f 72 69 65 73 20   to directories 
7f060 6f 6e 20 63 65 72 74 61 69 6e 20 66 69 6c 65 20  on certain file 
7f070 73 79 73 74 65 6d 73 2e 0a 20 20 20 20 20 20 20  systems..       
7f080 2a 2a 20 41 20 66 61 69 6c 65 64 20 64 69 72 65  ** A failed dire
7f090 63 74 6f 72 79 20 73 79 6e 63 20 69 73 20 6e 6f  ctory sync is no
7f0a0 74 20 61 20 62 69 67 20 64 65 61 6c 2e 20 20 53  t a big deal.  S
7f0b0 6f 20 69 74 20 73 65 65 6d 73 0a 20 20 20 20 20  o it seems.     
7f0c0 20 20 2a 2a 20 62 65 74 74 65 72 20 74 6f 20 69    ** better to i
7f0d0 67 6e 6f 72 65 20 74 68 65 20 65 72 72 6f 72 2e  gnore the error.
7f0e0 20 20 54 69 63 6b 65 74 20 23 31 36 35 37 0a 20    Ticket #1657. 
7f0f0 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
7f100 2f 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  /* return SQLITE
7f110 5f 49 4f 45 52 52 3b 20 2a 2f 0a 20 20 20 20 7d  _IOERR; */.    }
7f120 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 6c 6f 73  .#endif.    clos
7f130 65 28 70 46 69 6c 65 2d 3e 64 69 72 66 64 29 3b  e(pFile->dirfd);
7f140 20 20 2f 2a 20 4f 6e 6c 79 20 6e 65 65 64 20 74    /* Only need t
7f150 6f 20 73 79 6e 63 20 6f 6e 63 65 2c 20 73 6f 20  o sync once, so 
7f160 63 6c 6f 73 65 20 74 68 65 20 64 69 72 65 63 74  close the direct
7f170 6f 72 79 20 2a 2f 0a 20 20 20 20 70 46 69 6c 65  ory */.    pFile
7f180 2d 3e 64 69 72 66 64 20 3d 20 2d 31 3b 20 20 20  ->dirfd = -1;   
7f190 20 2f 2a 20 77 68 65 6e 20 77 65 20 61 72 65 20   /* when we are 
7f1a0 64 6f 6e 65 2e 20 2a 2f 0a 20 20 7d 0a 20 20 72  done. */.  }.  r
7f1b0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
7f1c0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74  .}../*.** Sync t
7f1d0 68 65 20 64 69 72 65 63 74 6f 72 79 20 7a 44 69  he directory zDi
7f1e0 72 6e 61 6d 65 2e 20 54 68 69 73 20 69 73 20 61  rname. This is a
7f1f0 20 6e 6f 2d 6f 70 20 6f 6e 20 6f 70 65 72 61 74   no-op on operat
7f200 69 6e 67 20 73 79 73 74 65 6d 73 20 6f 74 68 65  ing systems othe
7f210 72 0a 2a 2a 20 74 68 61 6e 20 55 4e 49 58 2e 0a  r.** than UNIX..
7f220 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 75 73  **.** This is us
7f230 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  ed to make sure 
7f240 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
7f250 61 6c 20 66 69 6c 65 20 68 61 73 20 74 72 75 65  al file has true
7f260 6c 79 20 62 65 65 6e 20 64 65 6c 65 74 65 64 0a  ly been deleted.
7f270 2a 2a 20 62 65 66 6f 72 65 20 6d 61 6b 69 6e 67  ** before making
7f280 20 63 68 61 6e 67 65 73 20 74 6f 20 69 6e 64 69   changes to indi
7f290 76 69 64 75 61 6c 20 6a 6f 75 72 6e 61 6c 73 20  vidual journals 
7f2a0 6f 6e 20 61 20 6d 75 6c 74 69 2d 64 61 74 61 62  on a multi-datab
7f2b0 61 73 65 20 63 6f 6d 6d 69 74 2e 0a 2a 2a 20 54  ase commit..** T
7f2c0 68 65 20 46 5f 46 55 4c 4c 46 53 59 4e 43 20 6f  he F_FULLFSYNC o
7f2d0 70 74 69 6f 6e 20 69 73 20 6e 6f 74 20 6e 65 65  ption is not nee
7f2e0 64 65 64 20 68 65 72 65 2e 0a 2a 2f 0a 53 51 4c  ded here..*/.SQL
7f2f0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
7f300 73 71 6c 69 74 65 33 55 6e 69 78 53 79 6e 63 44  sqlite3UnixSyncD
7f310 69 72 65 63 74 6f 72 79 28 63 6f 6e 73 74 20 63  irectory(const c
7f320 68 61 72 20 2a 7a 44 69 72 6e 61 6d 65 29 7b 0a  har *zDirname){.
7f330 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 49  #ifdef SQLITE_DI
7f340 53 41 42 4c 45 5f 44 49 52 53 59 4e 43 0a 20 20  SABLE_DIRSYNC.  
7f350 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
7f360 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20 66 64  ;.#else.  int fd
7f370 3b 0a 20 20 69 6e 74 20 72 3b 0a 20 20 66 64 20  ;.  int r;.  fd 
7f380 3d 20 6f 70 65 6e 28 7a 44 69 72 6e 61 6d 65 2c  = open(zDirname,
7f390 20 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 42 49 4e 41   O_RDONLY|O_BINA
7f3a0 52 59 2c 20 30 29 3b 0a 20 20 4f 53 54 52 41 43  RY, 0);.  OSTRAC
7f3b0 45 33 28 22 44 49 52 53 59 4e 43 20 25 2d 33 64  E3("DIRSYNC %-3d
7f3c0 20 28 25 73 29 5c 6e 22 2c 20 66 64 2c 20 7a 44   (%s)\n", fd, zD
7f3d0 69 72 6e 61 6d 65 29 3b 0a 20 20 69 66 28 20 66  irname);.  if( f
7f3e0 64 3c 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  d<0 ){.    retur
7f3f0 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45  n SQLITE_CANTOPE
7f400 4e 3b 20 0a 20 20 7d 0a 20 20 72 20 3d 20 66 73  N; .  }.  r = fs
7f410 79 6e 63 28 66 64 29 3b 0a 20 20 63 6c 6f 73 65  ync(fd);.  close
7f420 28 66 64 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65  (fd);.  Simulate
7f430 49 4f 45 72 72 6f 72 28 20 72 3d 31 20 29 3b 0a  IOError( r=1 );.
7f440 20 20 69 66 28 20 72 20 29 7b 0a 20 20 20 20 72    if( r ){.    r
7f450 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
7f460 52 52 5f 44 49 52 5f 46 53 59 4e 43 3b 0a 20 20  RR_DIR_FSYNC;.  
7f470 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72  }else{.    retur
7f480 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
7f490 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a  .#endif.}../*.**
7f4a0 20 54 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65   Truncate an ope
7f4b0 6e 20 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63  n file to a spec
7f4c0 69 66 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74  ified size.*/.st
7f4d0 61 74 69 63 20 69 6e 74 20 75 6e 69 78 54 72 75  atic int unixTru
7f4e0 6e 63 61 74 65 28 4f 73 46 69 6c 65 20 2a 69 64  ncate(OsFile *id
7f4f0 2c 20 69 36 34 20 6e 42 79 74 65 29 7b 0a 20 20  , i64 nByte){.  
7f500 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74  int rc;.  assert
7f510 28 20 69 64 20 29 3b 0a 20 20 72 63 20 3d 20 66  ( id );.  rc = f
7f520 74 72 75 6e 63 61 74 65 28 28 28 75 6e 69 78 46  truncate(((unixF
7f530 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 28 6f 66  ile*)id)->h, (of
7f540 66 5f 74 29 6e 42 79 74 65 29 3b 0a 20 20 53 69  f_t)nByte);.  Si
7f550 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72  mulateIOError( r
7f560 63 3d 31 20 29 3b 0a 20 20 69 66 28 20 72 63 20  c=1 );.  if( rc 
7f570 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
7f580 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43  LITE_IOERR_TRUNC
7f590 41 54 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ATE;.  }else{.  
7f5a0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
7f5b0 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  OK;.  }.}../*.**
7f5c0 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 63   Determine the c
7f5d0 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 61  urrent size of a
7f5e0 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 0a 2a   file in bytes.*
7f5f0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
7f600 78 46 69 6c 65 53 69 7a 65 28 4f 73 46 69 6c 65  xFileSize(OsFile
7f610 20 2a 69 64 2c 20 69 36 34 20 2a 70 53 69 7a 65   *id, i64 *pSize
7f620 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73  ){.  int rc;.  s
7f630 74 72 75 63 74 20 73 74 61 74 20 62 75 66 3b 0a  truct stat buf;.
7f640 20 20 61 73 73 65 72 74 28 20 69 64 20 29 3b 0a    assert( id );.
7f650 20 20 72 63 20 3d 20 66 73 74 61 74 28 28 28 75    rc = fstat(((u
7f660 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c  nixFile*)id)->h,
7f670 20 26 62 75 66 29 3b 0a 20 20 53 69 6d 75 6c 61   &buf);.  Simula
7f680 74 65 49 4f 45 72 72 6f 72 28 20 72 63 3d 31 20  teIOError( rc=1 
7f690 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 30 20 29  );.  if( rc!=0 )
7f6a0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
7f6b0 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b  ITE_IOERR_FSTAT;
7f6c0 0a 20 20 7d 0a 20 20 2a 70 53 69 7a 65 20 3d 20  .  }.  *pSize = 
7f6d0 62 75 66 2e 73 74 5f 73 69 7a 65 3b 0a 20 20 72  buf.st_size;.  r
7f6e0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
7f6f0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
7f700 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66  outine checks if
7f710 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45   there is a RESE
7f720 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f  RVED lock held o
7f730 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a  n the specified.
7f740 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20  ** file by this 
7f750 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f  or any other pro
7f760 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20  cess. If such a 
7f770 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 72 65  lock is held, re
7f780 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f  turn.** non-zero
7f790 2e 20 20 49 66 20 74 68 65 20 66 69 6c 65 20 69  .  If the file i
7f7a0 73 20 75 6e 6c 6f 63 6b 65 64 20 6f 72 20 68 6f  s unlocked or ho
7f7b0 6c 64 73 20 6f 6e 6c 79 20 53 48 41 52 45 44 20  lds only SHARED 
7f7c0 6c 6f 63 6b 73 2c 20 74 68 65 6e 0a 2a 2a 20 72  locks, then.** r
7f7d0 65 74 75 72 6e 20 7a 65 72 6f 2e 0a 2a 2f 0a 73  eturn zero..*/.s
7f7e0 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 43 68  tatic int unixCh
7f7f0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28  eckReservedLock(
7f800 4f 73 46 69 6c 65 20 2a 69 64 29 7b 0a 20 20 69  OsFile *id){.  i
7f810 6e 74 20 72 20 3d 20 30 3b 0a 20 20 75 6e 69 78  nt r = 0;.  unix
7f820 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
7f830 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20  nixFile*)id;..  
7f840 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b  assert( pFile );
7f850 0a 20 20 73 71 6c 69 74 65 33 4f 73 45 6e 74 65  .  sqlite3OsEnte
7f860 72 4d 75 74 65 78 28 29 3b 20 2f 2a 20 42 65 63  rMutex(); /* Bec
7f870 61 75 73 65 20 70 46 69 6c 65 2d 3e 70 4c 6f 63  ause pFile->pLoc
7f880 6b 20 69 73 20 73 68 61 72 65 64 20 61 63 72 6f  k is shared acro
7f890 73 73 20 74 68 72 65 61 64 73 20 2a 2f 0a 0a 20  ss threads */.. 
7f8a0 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 61 20 74   /* Check if a t
7f8b0 68 72 65 61 64 20 69 6e 20 74 68 69 73 20 70 72  hread in this pr
7f8c0 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 75 63 68  ocess holds such
7f8d0 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28   a lock */.  if(
7f8e0 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c   pFile->pLock->l
7f8f0 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c  ocktype>SHARED_L
7f900 4f 43 4b 20 29 7b 0a 20 20 20 20 72 20 3d 20 31  OCK ){.    r = 1
7f910 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 74 68 65  ;.  }..  /* Othe
7f920 72 77 69 73 65 20 73 65 65 20 69 66 20 73 6f 6d  rwise see if som
7f930 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20  e other process 
7f940 68 6f 6c 64 73 20 69 74 2e 0a 20 20 2a 2f 0a 20  holds it..  */. 
7f950 20 69 66 28 20 21 72 20 29 7b 0a 20 20 20 20 73   if( !r ){.    s
7f960 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b  truct flock lock
7f970 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65  ;.    lock.l_whe
7f980 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a  nce = SEEK_SET;.
7f990 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74      lock.l_start
7f9a0 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45   = RESERVED_BYTE
7f9b0 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e  ;.    lock.l_len
7f9c0 20 3d 20 31 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c   = 1;.    lock.l
7f9d0 5f 74 79 70 65 20 3d 20 46 5f 57 52 4c 43 4b 3b  _type = F_WRLCK;
7f9e0 0a 20 20 20 20 66 63 6e 74 6c 28 70 46 69 6c 65  .    fcntl(pFile
7f9f0 2d 3e 68 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 6c  ->h, F_GETLK, &l
7fa00 6f 63 6b 29 3b 0a 20 20 20 20 69 66 28 20 6c 6f  ock);.    if( lo
7fa10 63 6b 2e 6c 5f 74 79 70 65 21 3d 46 5f 55 4e 4c  ck.l_type!=F_UNL
7fa20 43 4b 20 29 7b 0a 20 20 20 20 20 20 72 20 3d 20  CK ){.      r = 
7fa30 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a  1;.    }.  }.  .
7fa40 20 20 73 71 6c 69 74 65 33 4f 73 4c 65 61 76 65    sqlite3OsLeave
7fa50 4d 75 74 65 78 28 29 3b 0a 20 20 4f 53 54 52 41  Mutex();.  OSTRA
7fa60 43 45 33 28 22 54 45 53 54 20 57 52 2d 4c 4f 43  CE3("TEST WR-LOC
7fa70 4b 20 25 64 20 25 64 5c 6e 22 2c 20 70 46 69 6c  K %d %d\n", pFil
7fa80 65 2d 3e 68 2c 20 72 29 3b 0a 0a 20 20 72 65 74  e->h, r);..  ret
7fa90 75 72 6e 20 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn r;.}../*.** 
7faa0 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69  Lock the file wi
7fab0 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63  th the lock spec
7fac0 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74  ified by paramet
7fad0 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e  er locktype - on
7fae0 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c  e.** of the foll
7faf0 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  owing:.**.**    
7fb00 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b   (1) SHARED_LOCK
7fb10 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53 45  .**     (2) RESE
7fb20 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20  RVED_LOCK.**    
7fb30 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43   (3) PENDING_LOC
7fb40 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43  K.**     (4) EXC
7fb50 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a  LUSIVE_LOCK.**.*
7fb60 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e  * Sometimes when
7fb70 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20   requesting one 
7fb80 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69  lock state, addi
7fb90 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74  tional lock stat
7fba0 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74  es.** are insert
7fbb0 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20  ed in between.  
7fbc0 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68  The locking migh
7fbd0 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66  t fail on one of
7fbe0 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72   the later.** tr
7fbf0 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e  ansitions leavin
7fc00 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65  g the lock state
7fc10 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20   different from 
7fc20 77 68 61 74 20 69 74 20 73 74 61 72 74 65 64 20  what it started 
7fc30 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f  but.** still sho
7fc40 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20  rt of its goal. 
7fc50 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   The following c
7fc60 68 61 72 74 20 73 68 6f 77 73 20 74 68 65 20 61  hart shows the a
7fc70 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69  llowed.** transi
7fc80 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e  tions and the in
7fc90 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69  serted intermedi
7fca0 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a  ate states:.**.*
7fcb0 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e  *    UNLOCKED ->
7fcc0 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48   SHARED.**    SH
7fcd0 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44  ARED -> RESERVED
7fce0 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e  .**    SHARED ->
7fcf0 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58   (PENDING) -> EX
7fd00 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45  CLUSIVE.**    RE
7fd10 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49  SERVED -> (PENDI
7fd20 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45  NG) -> EXCLUSIVE
7fd30 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d  .**    PENDING -
7fd40 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a  > EXCLUSIVE.**.*
7fd50 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77  * This routine w
7fd60 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73  ill only increas
7fd70 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74  e a lock.  Use t
7fd80 68 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f  he sqlite3OsUnlo
7fd90 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  ck().** routine 
7fda0 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69  to lower a locki
7fdb0 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61  ng level..*/.sta
7fdc0 74 69 63 20 69 6e 74 20 75 6e 69 78 4c 6f 63 6b  tic int unixLock
7fdd0 28 4f 73 46 69 6c 65 20 2a 69 64 2c 20 69 6e 74  (OsFile *id, int
7fde0 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 2f 2a   locktype){.  /*
7fdf0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 64   The following d
7fe00 65 73 63 72 69 62 65 73 20 74 68 65 20 69 6d 70  escribes the imp
7fe10 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
7fe20 68 65 20 76 61 72 69 6f 75 73 20 6c 6f 63 6b 73  he various locks
7fe30 20 61 6e 64 0a 20 20 2a 2a 20 6c 6f 63 6b 20 74   and.  ** lock t
7fe40 72 61 6e 73 69 74 69 6f 6e 73 20 69 6e 20 74 65  ransitions in te
7fe50 72 6d 73 20 6f 66 20 74 68 65 20 50 4f 53 49 58  rms of the POSIX
7fe60 20 61 64 76 69 73 6f 72 79 20 73 68 61 72 65 64   advisory shared
7fe70 20 61 6e 64 20 65 78 63 6c 75 73 69 76 65 0a 20   and exclusive. 
7fe80 20 2a 2a 20 6c 6f 63 6b 20 70 72 69 6d 69 74 69   ** lock primiti
7fe90 76 65 73 20 28 63 61 6c 6c 65 64 20 72 65 61 64  ves (called read
7fea0 2d 6c 6f 63 6b 73 20 61 6e 64 20 77 72 69 74 65  -locks and write
7feb0 2d 6c 6f 63 6b 73 20 62 65 6c 6f 77 2c 20 74 6f  -locks below, to
7fec0 20 61 76 6f 69 64 0a 20 20 2a 2a 20 63 6f 6e 66   avoid.  ** conf
7fed0 75 73 69 6f 6e 20 77 69 74 68 20 53 51 4c 69 74  usion with SQLit
7fee0 65 20 6c 6f 63 6b 20 6e 61 6d 65 73 29 2e 20 54  e lock names). T
7fef0 68 65 20 61 6c 67 6f 72 69 74 68 6d 73 20 61 72  he algorithms ar
7ff00 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 0a 20 20  e complicated.  
7ff10 2a 2a 20 73 6c 69 67 68 74 6c 79 20 69 6e 20 6f  ** slightly in o
7ff20 72 64 65 72 20 74 6f 20 62 65 20 63 6f 6d 70 61  rder to be compa
7ff30 74 69 62 6c 65 20 77 69 74 68 20 77 69 6e 64 6f  tible with windo
7ff40 77 73 20 73 79 73 74 65 6d 73 20 73 69 6d 75 6c  ws systems simul
7ff50 74 61 6e 65 6f 75 73 6c 79 0a 20 20 2a 2a 20 61  taneously.  ** a
7ff60 63 63 65 73 73 69 6e 67 20 74 68 65 20 73 61 6d  ccessing the sam
7ff70 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  e database file,
7ff80 20 69 6e 20 63 61 73 65 20 74 68 61 74 20 69 73   in case that is
7ff90 20 65 76 65 72 20 72 65 71 75 69 72 65 64 2e 0a   ever required..
7ffa0 20 20 2a 2a 0a 20 20 2a 2a 20 53 79 6d 62 6f 6c    **.  ** Symbol
7ffb0 73 20 64 65 66 69 6e 65 64 20 69 6e 20 6f 73 2e  s defined in os.
7ffc0 68 20 69 6e 64 65 6e 74 69 66 79 20 74 68 65 20  h indentify the 
7ffd0 27 70 65 6e 64 69 6e 67 20 62 79 74 65 27 20 61  'pending byte' a
7ffe0 6e 64 20 74 68 65 20 27 72 65 73 65 72 76 65 64  nd the 'reserved
7fff0 0a 20 20 2a 2a 20 62 79 74 65 27 2c 20 65 61 63  .  ** byte', eac
80000 68 20 73 69 6e 67 6c 65 20 62 79 74 65 73 20 61  h single bytes a
80010 74 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66  t well known off
80020 73 65 74 73 2c 20 61 6e 64 20 74 68 65 20 27 73  sets, and the 's
80030 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a 20  hared byte.  ** 
80040 72 61 6e 67 65 27 2c 20 61 20 72 61 6e 67 65 20  range', a range 
80050 6f 66 20 35 31 30 20 62 79 74 65 73 20 61 74 20  of 510 bytes at 
80060 61 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66  a well known off
80070 73 65 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  set..  **.  ** T
80080 6f 20 6f 62 74 61 69 6e 20 61 20 53 48 41 52 45  o obtain a SHARE
80090 44 20 6c 6f 63 6b 2c 20 61 20 72 65 61 64 2d 6c  D lock, a read-l
800a0 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20  ock is obtained 
800b0 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67 0a  on the 'pending.
800c0 20 20 2a 2a 20 62 79 74 65 27 2e 20 20 49 66 20    ** byte'.  If 
800d0 74 68 69 73 20 69 73 20 73 75 63 63 65 73 73 66  this is successf
800e0 75 6c 2c 20 61 20 72 61 6e 64 6f 6d 20 62 79 74  ul, a random byt
800f0 65 20 66 72 6f 6d 20 74 68 65 20 27 73 68 61 72  e from the 'shar
80100 65 64 20 62 79 74 65 0a 20 20 2a 2a 20 72 61 6e  ed byte.  ** ran
80110 67 65 27 20 69 73 20 72 65 61 64 2d 6c 6f 63 6b  ge' is read-lock
80120 65 64 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20  ed and the lock 
80130 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67 20  on the 'pending 
80140 62 79 74 65 27 20 72 65 6c 65 61 73 65 64 2e 0a  byte' released..
80150 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 70 72 6f 63    **.  ** A proc
80160 65 73 73 20 6d 61 79 20 6f 6e 6c 79 20 6f 62 74  ess may only obt
80170 61 69 6e 20 61 20 52 45 53 45 52 56 45 44 20 6c  ain a RESERVED l
80180 6f 63 6b 20 61 66 74 65 72 20 69 74 20 68 61 73  ock after it has
80190 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e 0a   a SHARED lock..
801a0 20 20 2a 2a 20 41 20 52 45 53 45 52 56 45 44 20    ** A RESERVED 
801b0 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65 6d 65 6e  lock is implemen
801c0 74 65 64 20 62 79 20 67 72 61 62 62 69 6e 67 20  ted by grabbing 
801d0 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20  a write-lock on 
801e0 74 68 65 0a 20 20 2a 2a 20 27 72 65 73 65 72 76  the.  ** 'reserv
801f0 65 64 20 62 79 74 65 27 2e 20 0a 20 20 2a 2a 0a  ed byte'. .  **.
80200 20 20 2a 2a 20 41 20 70 72 6f 63 65 73 73 20 6d    ** A process m
80210 61 79 20 6f 6e 6c 79 20 6f 62 74 61 69 6e 20 61  ay only obtain a
80220 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 61 66   PENDING lock af
80230 74 65 72 20 69 74 20 68 61 73 20 6f 62 74 61 69  ter it has obtai
80240 6e 65 64 20 61 0a 20 20 2a 2a 20 53 48 41 52 45  ned a.  ** SHARE
80250 44 20 6c 6f 63 6b 2e 20 41 20 50 45 4e 44 49 4e  D lock. A PENDIN
80260 47 20 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65 6d  G lock is implem
80270 65 6e 74 65 64 20 62 79 20 6f 62 74 61 69 6e 69  ented by obtaini
80280 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 0a  ng a write-lock.
80290 20 20 2a 2a 20 6f 6e 20 74 68 65 20 27 70 65 6e    ** on the 'pen
802a0 64 69 6e 67 20 62 79 74 65 27 2e 20 54 68 69 73  ding byte'. This
802b0 20 65 6e 73 75 72 65 73 20 74 68 61 74 20 6e 6f   ensures that no
802c0 20 6e 65 77 20 53 48 41 52 45 44 20 6c 6f 63 6b   new SHARED lock
802d0 73 20 63 61 6e 20 62 65 0a 20 20 2a 2a 20 6f 62  s can be.  ** ob
802e0 74 61 69 6e 65 64 2c 20 62 75 74 20 65 78 69 73  tained, but exis
802f0 74 69 6e 67 20 53 48 41 52 45 44 20 6c 6f 63 6b  ting SHARED lock
80300 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f  s are allowed to
80310 20 70 65 72 73 69 73 74 2e 20 41 20 70 72 6f 63   persist. A proc
80320 65 73 73 0a 20 20 2a 2a 20 64 6f 65 73 20 6e 6f  ess.  ** does no
80330 74 20 68 61 76 65 20 74 6f 20 6f 62 74 61 69 6e  t have to obtain
80340 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b   a RESERVED lock
80350 20 6f 6e 20 74 68 65 20 77 61 79 20 74 6f 20 61   on the way to a
80360 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 0a 20   PENDING lock.. 
80370 20 2a 2a 20 54 68 69 73 20 70 72 6f 70 65 72 74   ** This propert
80380 79 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65  y is used by the
80390 20 61 6c 67 6f 72 69 74 68 6d 20 66 6f 72 20 72   algorithm for r
803a0 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 6a 6f  olling back a jo
803b0 75 72 6e 61 6c 20 66 69 6c 65 0a 20 20 2a 2a 20  urnal file.  ** 
803c0 61 66 74 65 72 20 61 20 63 72 61 73 68 2e 0a 20  after a crash.. 
803d0 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 45 58 43 4c   **.  ** An EXCL
803e0 55 53 49 56 45 20 6c 6f 63 6b 2c 20 6f 62 74 61  USIVE lock, obta
803f0 69 6e 65 64 20 61 66 74 65 72 20 61 20 50 45 4e  ined after a PEN
80400 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 68 65 6c  DING lock is hel
80410 64 2c 20 69 73 0a 20 20 2a 2a 20 69 6d 70 6c 65  d, is.  ** imple
80420 6d 65 6e 74 65 64 20 62 79 20 6f 62 74 61 69 6e  mented by obtain
80430 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b  ing a write-lock
80440 20 6f 6e 20 74 68 65 20 65 6e 74 69 72 65 20 27   on the entire '
80450 73 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a  shared byte.  **
80460 20 72 61 6e 67 65 27 2e 20 53 69 6e 63 65 20 61   range'. Since a
80470 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 6b 73 20 72  ll other locks r
80480 65 71 75 69 72 65 20 61 20 72 65 61 64 2d 6c 6f  equire a read-lo
80490 63 6b 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65  ck on one of the
804a0 20 62 79 74 65 73 0a 20 20 2a 2a 20 77 69 74 68   bytes.  ** with
804b0 69 6e 20 74 68 69 73 20 72 61 6e 67 65 2c 20 74  in this range, t
804c0 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 61 74  his ensures that
804d0 20 6e 6f 20 6f 74 68 65 72 20 6c 6f 63 6b 73 20   no other locks 
804e0 61 72 65 20 68 65 6c 64 20 6f 6e 20 74 68 65 0a  are held on the.
804f0 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 0a    ** database. .
80500 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 72 65    **.  ** The re
80510 61 73 6f 6e 20 61 20 73 69 6e 67 6c 65 20 62 79  ason a single by
80520 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 65  te cannot be use
80530 64 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65  d instead of the
80540 20 27 73 68 61 72 65 64 20 62 79 74 65 0a 20 20   'shared byte.  
80550 2a 2a 20 72 61 6e 67 65 27 20 69 73 20 74 68 61  ** range' is tha
80560 74 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20  t some versions 
80570 6f 66 20 77 69 6e 64 6f 77 73 20 64 6f 20 6e 6f  of windows do no
80580 74 20 73 75 70 70 6f 72 74 20 72 65 61 64 2d 6c  t support read-l
80590 6f 63 6b 73 2e 20 42 79 0a 20 20 2a 2a 20 6c 6f  ocks. By.  ** lo
805a0 63 6b 69 6e 67 20 61 20 72 61 6e 64 6f 6d 20 62  cking a random b
805b0 79 74 65 20 66 72 6f 6d 20 61 20 72 61 6e 67 65  yte from a range
805c0 2c 20 63 6f 6e 63 75 72 72 65 6e 74 20 53 48 41  , concurrent SHA
805d0 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79 20 65 78  RED locks may ex
805e0 69 73 74 0a 20 20 2a 2a 20 65 76 65 6e 20 69 66  ist.  ** even if
805f0 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 70 72 69   the locking pri
80600 6d 69 74 69 76 65 20 75 73 65 64 20 69 73 20 61  mitive used is a
80610 6c 77 61 79 73 20 61 20 77 72 69 74 65 2d 6c 6f  lways a write-lo
80620 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 72  ck..  */.  int r
80630 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
80640 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
80650 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
80660 3b 0a 20 20 73 74 72 75 63 74 20 6c 6f 63 6b 49  ;.  struct lockI
80670 6e 66 6f 20 2a 70 4c 6f 63 6b 20 3d 20 70 46 69  nfo *pLock = pFi
80680 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 73 74 72  le->pLock;.  str
80690 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a  uct flock lock;.
806a0 20 20 69 6e 74 20 73 3b 0a 0a 20 20 61 73 73 65    int s;..  asse
806b0 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f  rt( pFile );.  O
806c0 53 54 52 41 43 45 37 28 22 4c 4f 43 4b 20 20 20  STRACE7("LOCK   
806d0 20 25 64 20 25 73 20 77 61 73 20 25 73 28 25 73   %d %s was %s(%s
806e0 2c 25 64 29 20 70 69 64 3d 25 64 5c 6e 22 2c 20  ,%d) pid=%d\n", 
806f0 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20  pFile->h,.      
80700 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63  locktypeName(loc
80710 6b 74 79 70 65 29 2c 20 6c 6f 63 6b 74 79 70 65  ktype), locktype
80720 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b  Name(pFile->lock
80730 74 79 70 65 29 2c 0a 20 20 20 20 20 20 6c 6f 63  type),.      loc
80740 6b 74 79 70 65 4e 61 6d 65 28 70 4c 6f 63 6b 2d  ktypeName(pLock-
80750 3e 6c 6f 63 6b 74 79 70 65 29 2c 20 70 4c 6f 63  >locktype), pLoc
80760 6b 2d 3e 63 6e 74 20 2c 20 67 65 74 70 69 64 28  k->cnt , getpid(
80770 29 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65  ));..  /* If the
80780 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20  re is already a 
80790 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70  lock of this typ
807a0 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69  e or more restri
807b0 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a  ctive on the.  *
807c0 2a 20 4f 73 46 69 6c 65 2c 20 64 6f 20 6e 6f 74  * OsFile, do not
807d0 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 20  hing. Don't use 
807e0 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78  the end_lock: ex
807f0 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a  it path, as.  **
80800 20 73 71 6c 69 74 65 33 4f 73 45 6e 74 65 72 4d   sqlite3OsEnterM
80810 75 74 65 78 28 29 20 68 61 73 6e 27 74 20 62 65  utex() hasn't be
80820 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20  en called yet.. 
80830 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d   */.  if( pFile-
80840 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74  >locktype>=lockt
80850 79 70 65 20 29 7b 0a 20 20 20 20 4f 53 54 52 41  ype ){.    OSTRA
80860 43 45 33 28 22 4c 4f 43 4b 20 20 20 20 25 64 20  CE3("LOCK    %d 
80870 25 73 20 6f 6b 20 28 61 6c 72 65 61 64 79 20 68  %s ok (already h
80880 65 6c 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  eld)\n", pFile->
80890 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c  h,.            l
808a0 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b  ocktypeName(lock
808b0 74 79 70 65 29 29 3b 0a 20 20 20 20 72 65 74 75  type));.    retu
808c0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
808d0 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72  }..  /* Make sur
808e0 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 65  e the locking se
808f0 71 75 65 6e 63 65 20 69 73 20 63 6f 72 72 65 63  quence is correc
80900 74 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  t.  */.  assert(
80910 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
80920 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63  !=NO_LOCK || loc
80930 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
80940 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  CK );.  assert( 
80950 6c 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44 49 4e  locktype!=PENDIN
80960 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65  G_LOCK );.  asse
80970 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 52 45  rt( locktype!=RE
80980 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70  SERVED_LOCK || p
80990 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d  File->locktype==
809a0 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 0a  SHARED_LOCK );..
809b0 20 20 2f 2a 20 54 68 69 73 20 6d 75 74 65 78 20    /* This mutex 
809c0 69 73 20 6e 65 65 64 65 64 20 62 65 63 61 75 73  is needed becaus
809d0 65 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 69  e pFile->pLock i
809e0 73 20 73 68 61 72 65 64 20 61 63 72 6f 73 73 20  s shared across 
809f0 74 68 72 65 61 64 73 0a 20 20 2a 2f 0a 20 20 73  threads.  */.  s
80a00 71 6c 69 74 65 33 4f 73 45 6e 74 65 72 4d 75 74  qlite3OsEnterMut
80a10 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65  ex();..  /* Make
80a20 20 73 75 72 65 20 74 68 65 20 63 75 72 72 65 6e   sure the curren
80a30 74 20 74 68 72 65 61 64 20 6f 77 6e 73 20 74 68  t thread owns th
80a40 65 20 70 46 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20  e pFile..  */.  
80a50 72 63 20 3d 20 74 72 61 6e 73 66 65 72 4f 77 6e  rc = transferOwn
80a60 65 72 73 68 69 70 28 70 46 69 6c 65 29 3b 0a 20  ership(pFile);. 
80a70 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
80a80 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  OK ){.    sqlite
80a90 33 4f 73 4c 65 61 76 65 4d 75 74 65 78 28 29 3b  3OsLeaveMutex();
80aa0 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
80ab0 20 20 7d 0a 20 20 70 4c 6f 63 6b 20 3d 20 70 46    }.  pLock = pF
80ac0 69 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a 0a 20 20 2f  ile->pLock;..  /
80ad0 2a 20 49 66 20 73 6f 6d 65 20 74 68 72 65 61 64  * If some thread
80ae0 20 75 73 69 6e 67 20 74 68 69 73 20 50 49 44 20   using this PID 
80af0 68 61 73 20 61 20 6c 6f 63 6b 20 76 69 61 20 61  has a lock via a
80b00 20 64 69 66 66 65 72 65 6e 74 20 4f 73 46 69 6c   different OsFil
80b10 65 2a 0a 20 20 2a 2a 20 68 61 6e 64 6c 65 20 74  e*.  ** handle t
80b20 68 61 74 20 70 72 65 63 6c 75 64 65 73 20 74 68  hat precludes th
80b30 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b  e requested lock
80b40 2c 20 72 65 74 75 72 6e 20 42 55 53 59 2e 0a 20  , return BUSY.. 
80b50 20 2a 2f 0a 20 20 69 66 28 20 28 70 46 69 6c 65   */.  if( (pFile
80b60 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 70 4c 6f 63  ->locktype!=pLoc
80b70 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 26 26 20 0a  k->locktype && .
80b80 20 20 20 20 20 20 20 20 20 20 28 70 4c 6f 63 6b            (pLock
80b90 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 50 45 4e 44  ->locktype>=PEND
80ba0 49 4e 47 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b  ING_LOCK || lock
80bb0 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b  type>SHARED_LOCK
80bc0 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 63 20 3d  )).  ){.    rc =
80bd0 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
80be0 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b    goto end_lock;
80bf0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20  .  }..  /* If a 
80c00 53 48 41 52 45 44 20 6c 6f 63 6b 20 69 73 20 72  SHARED lock is r
80c10 65 71 75 65 73 74 65 64 2c 20 61 6e 64 20 73 6f  equested, and so
80c20 6d 65 20 74 68 72 65 61 64 20 75 73 69 6e 67 20  me thread using 
80c30 74 68 69 73 20 50 49 44 20 61 6c 72 65 61 64 79  this PID already
80c40 0a 20 20 2a 2a 20 68 61 73 20 61 20 53 48 41 52  .  ** has a SHAR
80c50 45 44 20 6f 72 20 52 45 53 45 52 56 45 44 20 6c  ED or RESERVED l
80c60 6f 63 6b 2c 20 74 68 65 6e 20 69 6e 63 72 65 6d  ock, then increm
80c70 65 6e 74 20 72 65 66 65 72 65 6e 63 65 20 63 6f  ent reference co
80c80 75 6e 74 73 20 61 6e 64 0a 20 20 2a 2a 20 72 65  unts and.  ** re
80c90 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a  turn SQLITE_OK..
80ca0 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74    */.  if( lockt
80cb0 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
80cc0 20 26 26 20 0a 20 20 20 20 20 20 28 70 4c 6f 63   && .      (pLoc
80cd0 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  k->locktype==SHA
80ce0 52 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 4c 6f 63  RED_LOCK || pLoc
80cf0 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53  k->locktype==RES
80d00 45 52 56 45 44 5f 4c 4f 43 4b 29 20 29 7b 0a 20  ERVED_LOCK) ){. 
80d10 20 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74     assert( lockt
80d20 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
80d30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
80d40 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pFile->locktype=
80d50 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =0 );.    assert
80d60 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74 3e 30 20 29  ( pLock->cnt>0 )
80d70 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63  ;.    pFile->loc
80d80 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c  ktype = SHARED_L
80d90 4f 43 4b 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e  OCK;.    pLock->
80da0 63 6e 74 2b 2b 3b 0a 20 20 20 20 70 46 69 6c 65  cnt++;.    pFile
80db0 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2b 2b  ->pOpen->nLock++
80dc0 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c  ;.    goto end_l
80dd0 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 6c 6f 63 6b  ock;.  }..  lock
80de0 2e 6c 5f 6c 65 6e 20 3d 20 31 4c 3b 0a 0a 20 20  .l_len = 1L;..  
80df0 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20  lock.l_whence = 
80e00 53 45 45 4b 5f 53 45 54 3b 0a 0a 20 20 2f 2a 20  SEEK_SET;..  /* 
80e10 41 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69  A PENDING lock i
80e20 73 20 6e 65 65 64 65 64 20 62 65 66 6f 72 65 20  s needed before 
80e30 61 63 71 75 69 72 69 6e 67 20 61 20 53 48 41 52  acquiring a SHAR
80e40 45 44 20 6c 6f 63 6b 20 61 6e 64 20 62 65 66 6f  ED lock and befo
80e50 72 65 0a 20 20 2a 2a 20 61 63 71 75 69 72 69 6e  re.  ** acquirin
80e60 67 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c  g an EXCLUSIVE l
80e70 6f 63 6b 2e 20 20 46 6f 72 20 74 68 65 20 53 48  ock.  For the SH
80e80 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20 50  ARED lock, the P
80e90 45 4e 44 49 4e 47 20 77 69 6c 6c 0a 20 20 2a 2a  ENDING will.  **
80ea0 20 62 65 20 72 65 6c 65 61 73 65 64 2e 0a 20 20   be released..  
80eb0 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70  */.  if( locktyp
80ec0 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 0a  e==SHARED_LOCK .
80ed0 20 20 20 20 20 20 7c 7c 20 28 6c 6f 63 6b 74 79        || (lockty
80ee0 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f  pe==EXCLUSIVE_LO
80ef0 43 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63  CK && pFile->loc
80f00 6b 74 79 70 65 3c 50 45 4e 44 49 4e 47 5f 4c 4f  ktype<PENDING_LO
80f10 43 4b 29 0a 20 20 29 7b 0a 20 20 20 20 6c 6f 63  CK).  ){.    loc
80f20 6b 2e 6c 5f 74 79 70 65 20 3d 20 28 6c 6f 63 6b  k.l_type = (lock
80f30 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
80f40 4b 3f 46 5f 52 44 4c 43 4b 3a 46 5f 57 52 4c 43  K?F_RDLCK:F_WRLC
80f50 4b 29 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73  K);.    lock.l_s
80f60 74 61 72 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42  tart = PENDING_B
80f70 59 54 45 3b 0a 20 20 20 20 73 20 3d 20 66 63 6e  YTE;.    s = fcn
80f80 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53  tl(pFile->h, F_S
80f90 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20  ETLK, &lock);.  
80fa0 20 20 69 66 28 20 73 3d 3d 28 2d 31 29 20 29 7b    if( s==(-1) ){
80fb0 0a 20 20 20 20 20 20 72 63 20 3d 20 28 65 72 72  .      rc = (err
80fc0 6e 6f 3d 3d 45 49 4e 56 41 4c 29 20 3f 20 53 51  no==EINVAL) ? SQ
80fd0 4c 49 54 45 5f 4e 4f 4c 46 53 20 3a 20 53 51 4c  LITE_NOLFS : SQL
80fe0 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20  ITE_BUSY;.      
80ff0 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20  goto end_lock;. 
81000 20 20 20 7d 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20     }.  }...  /* 
81010 49 66 20 63 6f 6e 74 72 6f 6c 20 67 65 74 73 20  If control gets 
81020 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 74  to this point, t
81030 68 65 6e 20 61 63 74 75 61 6c 6c 79 20 67 6f 20  hen actually go 
81040 61 68 65 61 64 20 61 6e 64 20 6d 61 6b 65 0a 20  ahead and make. 
81050 20 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20 73 79   ** operating sy
81060 73 74 65 6d 20 63 61 6c 6c 73 20 66 6f 72 20 74  stem calls for t
81070 68 65 20 73 70 65 63 69 66 69 65 64 20 6c 6f 63  he specified loc
81080 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f  k..  */.  if( lo
81090 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c  cktype==SHARED_L
810a0 4f 43 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72  OCK ){.    asser
810b0 74 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74 3d 3d 30  t( pLock->cnt==0
810c0 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
810d0 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pLock->locktype=
810e0 3d 30 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 4e 6f  =0 );..    /* No
810f0 77 20 67 65 74 20 74 68 65 20 72 65 61 64 2d 6c  w get the read-l
81100 6f 63 6b 20 2a 2f 0a 20 20 20 20 6c 6f 63 6b 2e  ock */.    lock.
81110 6c 5f 73 74 61 72 74 20 3d 20 53 48 41 52 45 44  l_start = SHARED
81120 5f 46 49 52 53 54 3b 0a 20 20 20 20 6c 6f 63 6b  _FIRST;.    lock
81130 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 52 45 44 5f  .l_len = SHARED_
81140 53 49 5a 45 3b 0a 20 20 20 20 73 20 3d 20 66 63  SIZE;.    s = fc
81150 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f  ntl(pFile->h, F_
81160 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 0a  SETLK, &lock);..
81170 20 20 20 20 2f 2a 20 44 72 6f 70 20 74 68 65 20      /* Drop the 
81180 74 65 6d 70 6f 72 61 72 79 20 50 45 4e 44 49 4e  temporary PENDIN
81190 47 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 6c 6f  G lock */.    lo
811a0 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 50 45 4e  ck.l_start = PEN
811b0 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 6c  DING_BYTE;.    l
811c0 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 4c 3b 0a  ock.l_len = 1L;.
811d0 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20      lock.l_type 
811e0 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 69  = F_UNLCK;.    i
811f0 66 28 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e  f( fcntl(pFile->
81200 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63  h, F_SETLK, &loc
81210 6b 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 72  k)!=0 ){.      r
81220 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  c = SQLITE_IOERR
81230 5f 55 4e 4c 4f 43 4b 3b 20 20 2f 2a 20 54 68 69  _UNLOCK;  /* Thi
81240 73 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 68  s should never h
81250 61 70 70 65 6e 20 2a 2f 0a 20 20 20 20 20 20 67  appen */.      g
81260 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20  oto end_lock;.  
81270 20 20 7d 0a 20 20 20 20 69 66 28 20 73 3d 3d 28    }.    if( s==(
81280 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20  -1) ){.      rc 
81290 3d 20 28 65 72 72 6e 6f 3d 3d 45 49 4e 56 41 4c  = (errno==EINVAL
812a0 29 20 3f 20 53 51 4c 49 54 45 5f 4e 4f 4c 46 53  ) ? SQLITE_NOLFS
812b0 20 3a 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a   : SQLITE_BUSY;.
812c0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
812d0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
812e0 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a   = SHARED_LOCK;.
812f0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 70 4f 70        pFile->pOp
81300 65 6e 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20  en->nLock++;.   
81310 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 20 3d 20     pLock->cnt = 
81320 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  1;.    }.  }else
81330 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45   if( locktype==E
81340 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26  XCLUSIVE_LOCK &&
81350 20 70 4c 6f 63 6b 2d 3e 63 6e 74 3e 31 20 29 7b   pLock->cnt>1 ){
81360 0a 20 20 20 20 2f 2a 20 57 65 20 61 72 65 20 74  .    /* We are t
81370 72 79 69 6e 67 20 66 6f 72 20 61 6e 20 65 78 63  rying for an exc
81380 6c 75 73 69 76 65 20 6c 6f 63 6b 20 62 75 74 20  lusive lock but 
81390 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 69  another thread i
813a0 6e 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 73 61  n this.    ** sa
813b0 6d 65 20 70 72 6f 63 65 73 73 20 69 73 20 73 74  me process is st
813c0 69 6c 6c 20 68 6f 6c 64 69 6e 67 20 61 20 73 68  ill holding a sh
813d0 61 72 65 64 20 6c 6f 63 6b 2e 20 2a 2f 0a 20 20  ared lock. */.  
813e0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55    rc = SQLITE_BU
813f0 53 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  SY;.  }else{.   
81400 20 2f 2a 20 54 68 65 20 72 65 71 75 65 73 74 20   /* The request 
81410 77 61 73 20 66 6f 72 20 61 20 52 45 53 45 52 56  was for a RESERV
81420 45 44 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20  ED or EXCLUSIVE 
81430 6c 6f 63 6b 2e 20 20 49 74 20 69 73 0a 20 20 20  lock.  It is.   
81440 20 2a 2a 20 61 73 73 75 6d 65 64 20 74 68 61 74   ** assumed that
81450 20 74 68 65 72 65 20 69 73 20 61 20 53 48 41 52   there is a SHAR
81460 45 44 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f  ED or greater lo
81470 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 0a 20  ck on the file. 
81480 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 2e 0a 20     ** already.. 
81490 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74     */.    assert
814a0 28 20 30 21 3d 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ( 0!=pFile->lock
814b0 74 79 70 65 20 29 3b 0a 20 20 20 20 6c 6f 63 6b  type );.    lock
814c0 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 57 52 4c 43  .l_type = F_WRLC
814d0 4b 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20 6c  K;.    switch( l
814e0 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 20  ocktype ){.     
814f0 20 63 61 73 65 20 52 45 53 45 52 56 45 44 5f 4c   case RESERVED_L
81500 4f 43 4b 3a 0a 20 20 20 20 20 20 20 20 6c 6f 63  OCK:.        loc
81510 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 52 45 53 45  k.l_start = RESE
81520 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 20  RVED_BYTE;.     
81530 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
81540 63 61 73 65 20 45 58 43 4c 55 53 49 56 45 5f 4c  case EXCLUSIVE_L
81550 4f 43 4b 3a 0a 20 20 20 20 20 20 20 20 6c 6f 63  OCK:.        loc
81560 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 53 48 41 52  k.l_start = SHAR
81570 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 20 20  ED_FIRST;.      
81580 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 53    lock.l_len = S
81590 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20  HARED_SIZE;.    
815a0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
815b0 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20   default:.      
815c0 20 20 61 73 73 65 72 74 28 30 29 3b 0a 20 20 20    assert(0);.   
815d0 20 7d 0a 20 20 20 20 73 20 3d 20 66 63 6e 74 6c   }.    s = fcntl
815e0 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54  (pFile->h, F_SET
815f0 4c 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 20  LK, &lock);.    
81600 69 66 28 20 73 3d 3d 28 2d 31 29 20 29 7b 0a 20  if( s==(-1) ){. 
81610 20 20 20 20 20 72 63 20 3d 20 28 65 72 72 6e 6f       rc = (errno
81620 3d 3d 45 49 4e 56 41 4c 29 20 3f 20 53 51 4c 49  ==EINVAL) ? SQLI
81630 54 45 5f 4e 4f 4c 46 53 20 3a 20 53 51 4c 49 54  TE_NOLFS : SQLIT
81640 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 0a 20 20  E_BUSY;.    }.  
81650 7d 0a 20 20 0a 20 20 69 66 28 20 72 63 3d 3d 53  }.  .  if( rc==S
81660 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
81670 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
81680 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20  = locktype;.    
81690 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20  pLock->locktype 
816a0 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 65  = locktype;.  }e
816b0 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74 79 70 65  lse if( locktype
816c0 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  ==EXCLUSIVE_LOCK
816d0 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c   ){.    pFile->l
816e0 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 4e  ocktype = PENDIN
816f0 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20 70 4c 6f 63  G_LOCK;.    pLoc
81700 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 50 45  k->locktype = PE
81710 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 7d 0a  NDING_LOCK;.  }.
81720 0a 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 73 71 6c  .end_lock:.  sql
81730 69 74 65 33 4f 73 4c 65 61 76 65 4d 75 74 65 78  ite3OsLeaveMutex
81740 28 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 22  ();.  OSTRACE4("
81750 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 25 73  LOCK    %d %s %s
81760 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c  \n", pFile->h, l
81770 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b  ocktypeName(lock
81780 74 79 70 65 29 2c 20 0a 20 20 20 20 20 20 72 63  type), .      rc
81790 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f  ==SQLITE_OK ? "o
817a0 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 3b 0a  k" : "failed");.
817b0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
817c0 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20  /*.** Lower the 
817d0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e  locking level on
817e0 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
817f0 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79   pFile to lockty
81800 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a  pe.  locktype.**
81810 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20   must be either 
81820 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45  NO_LOCK or SHARE
81830 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66  D_LOCK..**.** If
81840 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76   the locking lev
81850 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64  el of the file d
81860 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72  escriptor is alr
81870 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77  eady at or below
81880 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 65  .** the requeste
81890 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c  d locking level,
818a0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
818b0 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61   a no-op..*/.sta
818c0 74 69 63 20 69 6e 74 20 75 6e 69 78 55 6e 6c 6f  tic int unixUnlo
818d0 63 6b 28 4f 73 46 69 6c 65 20 2a 69 64 2c 20 69  ck(OsFile *id, i
818e0 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20  nt locktype){.  
818f0 73 74 72 75 63 74 20 6c 6f 63 6b 49 6e 66 6f 20  struct lockInfo 
81900 2a 70 4c 6f 63 6b 3b 0a 20 20 73 74 72 75 63 74  *pLock;.  struct
81910 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 69   flock lock;.  i
81920 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
81930 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70  K;.  unixFile *p
81940 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
81950 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 74 28  *)id;..  assert(
81960 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52   pFile );.  OSTR
81970 41 43 45 37 28 22 55 4e 4c 4f 43 4b 20 20 25 64  ACE7("UNLOCK  %d
81980 20 25 64 20 77 61 73 20 25 64 28 25 64 2c 25 64   %d was %d(%d,%d
81990 29 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69  ) pid=%d\n", pFi
819a0 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c  le->h, locktype,
819b0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f  .      pFile->lo
819c0 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 70  cktype, pFile->p
819d0 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20  Lock->locktype, 
819e0 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 63 6e  pFile->pLock->cn
819f0 74 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20  t, getpid());.. 
81a00 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70   assert( locktyp
81a10 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e<=SHARED_LOCK )
81a20 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c  ;.  if( pFile->l
81a30 6f 63 6b 74 79 70 65 3c 3d 6c 6f 63 6b 74 79 70  ocktype<=locktyp
81a40 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  e ){.    return 
81a50 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
81a60 20 69 66 28 20 43 48 45 43 4b 5f 54 48 52 45 41   if( CHECK_THREA
81a70 44 49 44 28 70 46 69 6c 65 29 20 29 7b 0a 20 20  DID(pFile) ){.  
81a80 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
81a90 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 73 71  MISUSE;.  }.  sq
81aa0 6c 69 74 65 33 4f 73 45 6e 74 65 72 4d 75 74 65  lite3OsEnterMute
81ab0 78 28 29 3b 0a 20 20 70 4c 6f 63 6b 20 3d 20 70  x();.  pLock = p
81ac0 46 69 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 61  File->pLock;.  a
81ad0 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 63 6e  ssert( pLock->cn
81ae0 74 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 46  t!=0 );.  if( pF
81af0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48  ile->locktype>SH
81b00 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  ARED_LOCK ){.   
81b10 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e   assert( pLock->
81b20 6c 6f 63 6b 74 79 70 65 3d 3d 70 46 69 6c 65 2d  locktype==pFile-
81b30 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20  >locktype );.   
81b40 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53   if( locktype==S
81b50 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20  HARED_LOCK ){.  
81b60 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20      lock.l_type 
81b70 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 20 20 20  = F_RDLCK;.     
81b80 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d   lock.l_whence =
81b90 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 20   SEEK_SET;.     
81ba0 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20   lock.l_start = 
81bb0 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20  SHARED_FIRST;.  
81bc0 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d      lock.l_len =
81bd0 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20   SHARED_SIZE;.  
81be0 20 20 20 20 69 66 28 20 66 63 6e 74 6c 28 70 46      if( fcntl(pF
81bf0 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c  ile->h, F_SETLK,
81c00 20 26 6c 6f 63 6b 29 3d 3d 28 2d 31 29 20 29 7b   &lock)==(-1) ){
81c10 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73  .        /* This
81c20 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 68 61   should never ha
81c30 70 70 65 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20  ppen */.        
81c40 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52  rc = SQLITE_IOER
81c50 52 5f 52 44 4c 4f 43 4b 3b 0a 20 20 20 20 20 20  R_RDLOCK;.      
81c60 7d 0a 20 20 20 20 7d 0a 20 20 20 20 6c 6f 63 6b  }.    }.    lock
81c70 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43  .l_type = F_UNLC
81c80 4b 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68  K;.    lock.l_wh
81c90 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b  ence = SEEK_SET;
81ca0 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72  .    lock.l_star
81cb0 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  t = PENDING_BYTE
81cc0 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e  ;.    lock.l_len
81cd0 20 3d 20 32 4c 3b 20 20 61 73 73 65 72 74 28 20   = 2L;  assert( 
81ce0 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 31 3d 3d  PENDING_BYTE+1==
81cf0 52 45 53 45 52 56 45 44 5f 42 59 54 45 20 29 3b  RESERVED_BYTE );
81d00 0a 20 20 20 20 69 66 28 20 66 63 6e 74 6c 28 70  .    if( fcntl(p
81d10 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b  File->h, F_SETLK
81d20 2c 20 26 6c 6f 63 6b 29 21 3d 28 2d 31 29 20 29  , &lock)!=(-1) )
81d30 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c  {.      pLock->l
81d40 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44  ocktype = SHARED
81d50 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65  _LOCK;.    }else
81d60 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
81d70 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b  ITE_IOERR_UNLOCK
81d80 3b 20 20 2f 2a 20 54 68 69 73 20 73 68 6f 75 6c  ;  /* This shoul
81d90 64 20 6e 65 76 65 72 20 68 61 70 70 65 6e 20 2a  d never happen *
81da0 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  /.    }.  }.  if
81db0 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c  ( locktype==NO_L
81dc0 4f 43 4b 20 29 7b 0a 20 20 20 20 73 74 72 75 63  OCK ){.    struc
81dd0 74 20 6f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e  t openCnt *pOpen
81de0 3b 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 6d  ;..    /* Decrem
81df0 65 6e 74 20 74 68 65 20 73 68 61 72 65 64 20 6c  ent the shared l
81e00 6f 63 6b 20 63 6f 75 6e 74 65 72 2e 20 20 52 65  ock counter.  Re
81e10 6c 65 61 73 65 20 74 68 65 20 6c 6f 63 6b 20 75  lease the lock u
81e20 73 69 6e 67 20 61 6e 0a 20 20 20 20 2a 2a 20 4f  sing an.    ** O
81e30 53 20 63 61 6c 6c 20 6f 6e 6c 79 20 77 68 65 6e  S call only when
81e40 20 61 6c 6c 20 74 68 72 65 61 64 73 20 69 6e 20   all threads in 
81e50 74 68 69 73 20 73 61 6d 65 20 70 72 6f 63 65 73  this same proces
81e60 73 20 68 61 76 65 20 72 65 6c 65 61 73 65 64 0a  s have released.
81e70 20 20 20 20 2a 2a 20 74 68 65 20 6c 6f 63 6b 2e      ** the lock.
81e80 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 4c 6f 63  .    */.    pLoc
81e90 6b 2d 3e 63 6e 74 2d 2d 3b 0a 20 20 20 20 69 66  k->cnt--;.    if
81ea0 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74 3d 3d 30 20  ( pLock->cnt==0 
81eb0 29 7b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f  ){.      lock.l_
81ec0 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a  type = F_UNLCK;.
81ed0 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65        lock.l_whe
81ee0 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a  nce = SEEK_SET;.
81ef0 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61        lock.l_sta
81f00 72 74 20 3d 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20  rt = lock.l_len 
81f10 3d 20 30 4c 3b 0a 20 20 20 20 20 20 69 66 28 20  = 0L;.      if( 
81f20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20  fcntl(pFile->h, 
81f30 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 21  F_SETLK, &lock)!
81f40 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20  =(-1) ){.       
81f50 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65   pLock->locktype
81f60 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20   = NO_LOCK;.    
81f70 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
81f80 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45   rc = SQLITE_IOE
81f90 52 52 5f 55 4e 4c 4f 43 4b 3b 20 20 2f 2a 20 54  RR_UNLOCK;  /* T
81fa0 68 69 73 20 73 68 6f 75 6c 64 20 6e 65 76 65 72  his should never
81fb0 20 68 61 70 70 65 6e 20 2a 2f 0a 20 20 20 20 20   happen */.     
81fc0 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a   }.    }..    /*
81fd0 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 63   Decrement the c
81fe0 6f 75 6e 74 20 6f 66 20 6c 6f 63 6b 73 20 61 67  ount of locks ag
81ff0 61 69 6e 73 74 20 74 68 69 73 20 73 61 6d 65 20  ainst this same 
82000 66 69 6c 65 2e 20 20 57 68 65 6e 20 74 68 65 0a  file.  When the.
82010 20 20 20 20 2a 2a 20 63 6f 75 6e 74 20 72 65 61      ** count rea
82020 63 68 65 73 20 7a 65 72 6f 2c 20 63 6c 6f 73 65  ches zero, close
82030 20 61 6e 79 20 6f 74 68 65 72 20 66 69 6c 65 20   any other file 
82040 64 65 73 63 72 69 70 74 6f 72 73 20 77 68 6f 73  descriptors whos
82050 65 20 63 6c 6f 73 65 0a 20 20 20 20 2a 2a 20 77  e close.    ** w
82060 61 73 20 64 65 66 65 72 72 65 64 20 62 65 63 61  as deferred beca
82070 75 73 65 20 6f 66 20 6f 75 74 73 74 61 6e 64 69  use of outstandi
82080 6e 67 20 6c 6f 63 6b 73 2e 0a 20 20 20 20 2a 2f  ng locks..    */
82090 0a 20 20 20 20 70 4f 70 65 6e 20 3d 20 70 46 69  .    pOpen = pFi
820a0 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 20 20 70  le->pOpen;.    p
820b0 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2d 2d 3b 0a 20  Open->nLock--;. 
820c0 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 65 6e     assert( pOpen
820d0 2d 3e 6e 4c 6f 63 6b 3e 3d 30 20 29 3b 0a 20 20  ->nLock>=0 );.  
820e0 20 20 69 66 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f    if( pOpen->nLo
820f0 63 6b 3d 3d 30 20 26 26 20 70 4f 70 65 6e 2d 3e  ck==0 && pOpen->
82100 6e 50 65 6e 64 69 6e 67 3e 30 20 29 7b 0a 20 20  nPending>0 ){.  
82110 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20      int i;.     
82120 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 70 65   for(i=0; i<pOpe
82130 6e 2d 3e 6e 50 65 6e 64 69 6e 67 3b 20 69 2b 2b  n->nPending; i++
82140 29 7b 0a 20 20 20 20 20 20 20 20 63 6c 6f 73 65  ){.        close
82150 28 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67  (pOpen->aPending
82160 5b 69 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  [i]);.      }.  
82170 20 20 20 20 66 72 65 65 28 70 4f 70 65 6e 2d 3e      free(pOpen->
82180 61 50 65 6e 64 69 6e 67 29 3b 0a 20 20 20 20 20  aPending);.     
82190 20 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67   pOpen->nPending
821a0 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4f 70 65   = 0;.      pOpe
821b0 6e 2d 3e 61 50 65 6e 64 69 6e 67 20 3d 20 30 3b  n->aPending = 0;
821c0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c  .    }.  }.  sql
821d0 69 74 65 33 4f 73 4c 65 61 76 65 4d 75 74 65 78  ite3OsLeaveMutex
821e0 28 29 3b 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63  ();.  pFile->loc
821f0 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65  ktype = locktype
82200 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
82210 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20  ../*.** Close a 
82220 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  file..*/.static 
82230 69 6e 74 20 75 6e 69 78 43 6c 6f 73 65 28 4f 73  int unixClose(Os
82240 46 69 6c 65 20 2a 2a 70 49 64 29 7b 0a 20 20 75  File **pId){.  u
82250 6e 69 78 46 69 6c 65 20 2a 69 64 20 3d 20 28 75  nixFile *id = (u
82260 6e 69 78 46 69 6c 65 2a 29 2a 70 49 64 3b 0a 0a  nixFile*)*pId;..
82270 20 20 69 66 28 20 21 69 64 20 29 20 72 65 74 75    if( !id ) retu
82280 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
82290 75 6e 69 78 55 6e 6c 6f 63 6b 28 2a 70 49 64 2c  unixUnlock(*pId,
822a0 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 69 66 28   NO_LOCK);.  if(
822b0 20 69 64 2d 3e 64 69 72 66 64 3e 3d 30 20 29 20   id->dirfd>=0 ) 
822c0 63 6c 6f 73 65 28 69 64 2d 3e 64 69 72 66 64 29  close(id->dirfd)
822d0 3b 0a 20 20 69 64 2d 3e 64 69 72 66 64 20 3d 20  ;.  id->dirfd = 
822e0 2d 31 3b 0a 20 20 73 71 6c 69 74 65 33 4f 73 45  -1;.  sqlite3OsE
822f0 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 0a 20 20  nterMutex();..  
82300 69 66 28 20 69 64 2d 3e 70 4f 70 65 6e 2d 3e 6e  if( id->pOpen->n
82310 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 2f 2a 20 49  Lock ){.    /* I
82320 66 20 74 68 65 72 65 20 61 72 65 20 6f 75 74 73  f there are outs
82330 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 2c 20 64  tanding locks, d
82340 6f 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 63  o not actually c
82350 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20 6a 75  lose the file ju
82360 73 74 0a 20 20 20 20 2a 2a 20 79 65 74 20 62 65  st.    ** yet be
82370 63 61 75 73 65 20 74 68 61 74 20 77 6f 75 6c 64  cause that would
82380 20 63 6c 65 61 72 20 74 68 6f 73 65 20 6c 6f 63   clear those loc
82390 6b 73 2e 20 20 49 6e 73 74 65 61 64 2c 20 61 64  ks.  Instead, ad
823a0 64 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a  d the file.    *
823b0 2a 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20  * descriptor to 
823c0 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 2e  pOpen->aPending.
823d0 20 20 49 74 20 77 69 6c 6c 20 62 65 20 61 75 74    It will be aut
823e0 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6c 6f 73 65  omatically close
823f0 64 20 77 68 65 6e 0a 20 20 20 20 2a 2a 20 74 68  d when.    ** th
82400 65 20 6c 61 73 74 20 6c 6f 63 6b 20 69 73 20 63  e last lock is c
82410 6c 65 61 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20  leared..    */. 
82420 20 20 20 69 6e 74 20 2a 61 4e 65 77 3b 0a 20 20     int *aNew;.  
82430 20 20 73 74 72 75 63 74 20 6f 70 65 6e 43 6e 74    struct openCnt
82440 20 2a 70 4f 70 65 6e 20 3d 20 69 64 2d 3e 70 4f   *pOpen = id->pO
82450 70 65 6e 3b 0a 20 20 20 20 61 4e 65 77 20 3d 20  pen;.    aNew = 
82460 72 65 61 6c 6c 6f 63 28 20 70 4f 70 65 6e 2d 3e  realloc( pOpen->
82470 61 50 65 6e 64 69 6e 67 2c 20 28 70 4f 70 65 6e  aPending, (pOpen
82480 2d 3e 6e 50 65 6e 64 69 6e 67 2b 31 29 2a 73 69  ->nPending+1)*si
82490 7a 65 6f 66 28 69 6e 74 29 20 29 3b 0a 20 20 20  zeof(int) );.   
824a0 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 7b 0a   if( aNew==0 ){.
824b0 20 20 20 20 20 20 2f 2a 20 49 66 20 61 20 6d 61        /* If a ma
824c0 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 6a 75 73 74  lloc fails, just
824d0 20 6c 65 61 6b 20 74 68 65 20 66 69 6c 65 20 64   leak the file d
824e0 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 20  escriptor */.   
824f0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 4f   }else{.      pO
82500 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 20 3d 20  pen->aPending = 
82510 61 4e 65 77 3b 0a 20 20 20 20 20 20 70 4f 70 65  aNew;.      pOpe
82520 6e 2d 3e 61 50 65 6e 64 69 6e 67 5b 70 4f 70 65  n->aPending[pOpe
82530 6e 2d 3e 6e 50 65 6e 64 69 6e 67 5d 20 3d 20 69  n->nPending] = i
82540 64 2d 3e 68 3b 0a 20 20 20 20 20 20 70 4f 70 65  d->h;.      pOpe
82550 6e 2d 3e 6e 50 65 6e 64 69 6e 67 2b 2b 3b 0a 20  n->nPending++;. 
82560 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
82570 20 20 2f 2a 20 54 68 65 72 65 20 61 72 65 20 6e    /* There are n
82580 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f  o outstanding lo
82590 63 6b 73 20 73 6f 20 77 65 20 63 61 6e 20 63 6c  cks so we can cl
825a0 6f 73 65 20 74 68 65 20 66 69 6c 65 20 69 6d 6d  ose the file imm
825b0 65 64 69 61 74 65 6c 79 20 2a 2f 0a 20 20 20 20  ediately */.    
825c0 63 6c 6f 73 65 28 69 64 2d 3e 68 29 3b 0a 20 20  close(id->h);.  
825d0 7d 0a 20 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49  }.  releaseLockI
825e0 6e 66 6f 28 69 64 2d 3e 70 4c 6f 63 6b 29 3b 0a  nfo(id->pLock);.
825f0 20 20 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74    releaseOpenCnt
82600 28 69 64 2d 3e 70 4f 70 65 6e 29 3b 0a 0a 20 20  (id->pOpen);..  
82610 73 71 6c 69 74 65 33 4f 73 4c 65 61 76 65 4d 75  sqlite3OsLeaveMu
82620 74 65 78 28 29 3b 0a 20 20 69 64 2d 3e 69 73 4f  tex();.  id->isO
82630 70 65 6e 20 3d 20 30 3b 0a 20 20 4f 53 54 52 41  pen = 0;.  OSTRA
82640 43 45 32 28 22 43 4c 4f 53 45 20 20 20 25 2d 33  CE2("CLOSE   %-3
82650 64 5c 6e 22 2c 20 69 64 2d 3e 68 29 3b 0a 20 20  d\n", id->h);.  
82660 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2d 31 29 3b  OpenCounter(-1);
82670 0a 20 20 73 71 6c 69 74 65 33 54 68 72 65 61 64  .  sqlite3Thread
82680 53 61 66 65 46 72 65 65 28 69 64 29 3b 0a 20 20  SafeFree(id);.  
82690 2a 70 49 64 20 3d 20 30 3b 0a 20 20 72 65 74 75  *pId = 0;.  retu
826a0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
826b0 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
826c0 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
826d0 54 59 4c 45 0a 23 70 72 61 67 6d 61 20 6d 61 72  TYLE.#pragma mar
826e0 6b 20 41 46 50 20 53 75 70 70 6f 72 74 0a 0a 2f  k AFP Support../
826f0 2a 0a 20 2a 2a 20 54 68 65 20 61 66 70 4c 6f 63  *. ** The afpLoc
82700 6b 69 6e 67 43 6f 6e 74 65 78 74 20 73 74 72 75  kingContext stru
82710 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61  cture contains a
82720 6c 6c 20 61 66 70 20 6c 6f 63 6b 20 73 70 65 63  ll afp lock spec
82730 69 66 69 63 20 73 74 61 74 65 0a 20 2a 2f 0a 74  ific state. */.t
82740 79 70 65 64 65 66 20 73 74 72 75 63 74 20 61 66  ypedef struct af
82750 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  pLockingContext 
82760 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  afpLockingContex
82770 74 3b 0a 73 74 72 75 63 74 20 61 66 70 4c 6f 63  t;.struct afpLoc
82780 6b 69 6e 67 43 6f 6e 74 65 78 74 20 7b 0a 20 20  kingContext {.  
82790 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f  unsigned long lo
827a0 6e 67 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74  ng sharedLockByt
827b0 65 3b 0a 20 20 63 68 61 72 20 2a 66 69 6c 65 50  e;.  char *fileP
827c0 61 74 68 3b 0a 7d 3b 0a 0a 73 74 72 75 63 74 20  ath;.};..struct 
827d0 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32  ByteRangeLockPB2
827e0 0a 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f  .{.  unsigned lo
827f0 6e 67 20 6c 6f 6e 67 20 6f 66 66 73 65 74 3b 20  ng long offset; 
82800 20 20 20 20 20 20 20 2f 2a 20 6f 66 66 73 65 74         /* offset
82810 20 74 6f 20 66 69 72 73 74 20 62 79 74 65 20 74   to first byte t
82820 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 69  o lock */.  unsi
82830 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6c  gned long long l
82840 65 6e 67 74 68 3b 20 20 20 20 20 20 20 20 2f 2a  ength;        /*
82850 20 6e 62 72 20 6f 66 20 62 79 74 65 73 20 74 6f   nbr of bytes to
82860 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 69 67   lock */.  unsig
82870 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65  ned long long re
82880 74 52 61 6e 67 65 53 74 61 72 74 3b 20 2f 2a 20  tRangeStart; /* 
82890 6e 62 72 20 6f 66 20 31 73 74 20 62 79 74 65 20  nbr of 1st byte 
828a0 6c 6f 63 6b 65 64 20 69 66 20 73 75 63 63 65 73  locked if succes
828b0 73 66 75 6c 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  sful */.  unsign
828c0 65 64 20 63 68 61 72 20 75 6e 4c 6f 63 6b 46 6c  ed char unLockFl
828d0 61 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 31  ag;         /* 1
828e0 20 3d 20 75 6e 6c 6f 63 6b 2c 20 30 20 3d 20 6c   = unlock, 0 = l
828f0 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  ock */.  unsigne
82900 64 20 63 68 61 72 20 73 74 61 72 74 45 6e 64 46  d char startEndF
82910 6c 61 67 3b 20 20 20 20 20 20 20 2f 2a 20 31 3d  lag;       /* 1=
82920 72 65 6c 20 74 6f 20 65 6e 64 20 6f 66 20 66 6f  rel to end of fo
82930 72 6b 2c 20 30 3d 72 65 6c 20 74 6f 20 73 74 61  rk, 0=rel to sta
82940 72 74 20 2a 2f 0a 20 20 69 6e 74 20 66 64 3b 20  rt */.  int fd; 
82950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
82960 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 69 6c            /* fil
82970 65 20 64 65 73 63 20 74 6f 20 61 73 73 6f 63 20  e desc to assoc 
82980 74 68 69 73 20 6c 6f 63 6b 20 77 69 74 68 20 2a  this lock with *
82990 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e 65 20 61 66  /.};..#define af
829a0 70 66 73 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b  pfsByteRangeLock
829b0 32 46 53 43 54 4c 09 5f 49 4f 57 52 28 27 7a 27  2FSCTL._IOWR('z'
829c0 2c 20 32 33 2c 20 73 74 72 75 63 74 20 42 79 74  , 23, struct Byt
829d0 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32 29 0a 0a  eRangeLockPB2)..
829e0 2f 2a 20 72 65 74 75 72 6e 20 30 20 6f 6e 20 73  /* return 0 on s
829f0 75 63 63 65 73 73 2c 20 31 20 6f 6e 20 66 61 69  uccess, 1 on fai
82a00 6c 75 72 65 2e 20 20 54 6f 20 6d 61 74 63 68 20  lure.  To match 
82a10 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66 20  the behavior of 
82a20 74 68 65 20 0a 20 20 6e 6f 72 6d 61 6c 20 70 6f  the .  normal po
82a30 73 69 78 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67  six file locking
82a40 20 28 75 73 65 64 20 69 6e 20 75 6e 69 78 4c 6f   (used in unixLo
82a50 63 6b 20 66 6f 72 20 65 78 61 6d 70 6c 65 29 2c  ck for example),
82a60 20 77 65 20 73 68 6f 75 6c 64 20 0a 20 20 70 72   we should .  pr
82a70 6f 76 69 64 65 20 27 72 69 63 68 65 72 27 20 72  ovide 'richer' r
82a80 65 74 75 72 6e 20 63 6f 64 65 73 20 2d 20 73 70  eturn codes - sp
82a90 65 63 69 66 69 63 61 6c 6c 79 20 74 6f 20 64 69  ecifically to di
82aa0 66 66 65 72 65 6e 74 69 61 74 65 20 62 65 74 77  fferentiate betw
82ab0 65 65 6e 0a 20 20 27 66 69 6c 65 20 62 75 73 79  een.  'file busy
82ac0 27 20 61 6e 64 20 27 66 69 6c 65 20 73 79 73 74  ' and 'file syst
82ad0 65 6d 20 65 72 72 6f 72 27 20 72 65 73 75 6c 74  em error' result
82ae0 73 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  s */.static int 
82af0 5f 41 46 50 46 53 53 65 74 4c 6f 63 6b 28 63 6f  _AFPFSSetLock(co
82b00 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 2c 20  nst char *path, 
82b10 69 6e 74 20 66 64 2c 20 75 6e 73 69 67 6e 65 64  int fd, unsigned
82b20 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6f 66 66 73 65   long long offse
82b30 74 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  t, .            
82b40 20 20 20 20 20 20 20 20 20 20 20 20 20 75 6e 73               uns
82b50 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20  igned long long 
82b60 6c 65 6e 67 74 68 2c 20 69 6e 74 20 73 65 74 4c  length, int setL
82b70 6f 63 6b 46 6c 61 67 29 0a 7b 0a 20 20 73 74 72  ockFlag).{.  str
82b80 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c 6f 63  uct ByteRangeLoc
82b90 6b 50 42 32 09 70 62 3b 0a 20 20 69 6e 74 20 20  kPB2.pb;.  int  
82ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
82bb0 20 20 20 65 72 72 3b 0a 20 20 0a 20 20 70 62 2e     err;.  .  pb.
82bc0 75 6e 4c 6f 63 6b 46 6c 61 67 20 3d 20 73 65 74  unLockFlag = set
82bd0 4c 6f 63 6b 46 6c 61 67 20 3f 20 30 20 3a 20 31  LockFlag ? 0 : 1
82be0 3b 0a 20 20 70 62 2e 73 74 61 72 74 45 6e 64 46  ;.  pb.startEndF
82bf0 6c 61 67 20 3d 20 30 3b 0a 20 20 70 62 2e 6f 66  lag = 0;.  pb.of
82c00 66 73 65 74 20 3d 20 6f 66 66 73 65 74 3b 0a 20  fset = offset;. 
82c10 20 70 62 2e 6c 65 6e 67 74 68 20 3d 20 6c 65 6e   pb.length = len
82c20 67 74 68 3b 20 0a 20 20 70 62 2e 66 64 20 3d 20  gth; .  pb.fd = 
82c30 66 64 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22  fd;.  OSTRACE5("
82c40 41 46 50 4c 4f 43 4b 20 73 65 74 74 69 6e 67 20  AFPLOCK setting 
82c50 6c 6f 63 6b 20 25 73 20 66 6f 72 20 25 64 20 69  lock %s for %d i
82c60 6e 20 72 61 6e 67 65 20 25 6c 6c 78 3a 25 6c 6c  n range %llx:%ll
82c70 78 5c 6e 22 2c 20 0a 20 20 20 20 28 73 65 74 4c  x\n", .    (setL
82c80 6f 63 6b 46 6c 61 67 3f 22 4f 4e 22 3a 22 4f 46  ockFlag?"ON":"OF
82c90 46 22 29 2c 20 66 64 2c 20 6f 66 66 73 65 74 2c  F"), fd, offset,
82ca0 20 6c 65 6e 67 74 68 29 3b 0a 20 20 65 72 72 20   length);.  err 
82cb0 3d 20 66 73 63 74 6c 28 70 61 74 68 2c 20 61 66  = fsctl(path, af
82cc0 70 66 73 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b  pfsByteRangeLock
82cd0 32 46 53 43 54 4c 2c 20 26 70 62 2c 20 30 29 3b  2FSCTL, &pb, 0);
82ce0 0a 20 20 69 66 20 28 20 65 72 72 3d 3d 2d 31 20  .  if ( err==-1 
82cf0 29 20 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 34  ) {.    OSTRACE4
82d00 28 22 41 46 50 4c 4f 43 4b 20 66 61 69 6c 65 64  ("AFPLOCK failed
82d10 20 74 6f 20 66 73 63 74 6c 28 29 20 27 25 73 27   to fsctl() '%s'
82d20 20 25 64 20 25 73 5c 6e 22 2c 20 70 61 74 68 2c   %d %s\n", path,
82d30 20 65 72 72 6e 6f 2c 20 0a 20 20 20 20 20 20 73   errno, .      s
82d40 74 72 65 72 72 6f 72 28 65 72 72 6e 6f 29 29 3b  trerror(errno));
82d50 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 20 2f  .    return 1; /
82d60 2a 20 65 72 72 6f 72 20 2a 2f 0a 20 20 7d 20 65  * error */.  } e
82d70 6c 73 65 20 7b 0a 20 20 20 20 72 65 74 75 72 6e  lse {.    return
82d80 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 20 2a   0;.  }.}../*. *
82d90 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
82da0 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 69  hecks if there i
82db0 73 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63  s a RESERVED loc
82dc0 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70  k held on the sp
82dd0 65 63 69 66 69 65 64 0a 20 2a 2a 20 66 69 6c 65  ecified. ** file
82de0 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20   by this or any 
82df0 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49  other process. I
82e00 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73  f such a lock is
82e10 20 68 65 6c 64 2c 20 72 65 74 75 72 6e 0a 20 2a   held, return. *
82e20 2a 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 49 66 20  * non-zero.  If 
82e30 74 68 65 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f  the file is unlo
82e40 63 6b 65 64 20 6f 72 20 68 6f 6c 64 73 20 6f 6e  cked or holds on
82e50 6c 79 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 2c  ly SHARED locks,
82e60 20 74 68 65 6e 0a 20 2a 2a 20 72 65 74 75 72 6e   then. ** return
82e70 20 7a 65 72 6f 2e 0a 20 2a 2f 0a 73 74 61 74 69   zero.. */.stati
82e80 63 20 69 6e 74 20 61 66 70 55 6e 69 78 43 68 65  c int afpUnixChe
82e90 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 4f  ckReservedLock(O
82ea0 73 46 69 6c 65 20 2a 69 64 29 7b 0a 20 20 69 6e  sFile *id){.  in
82eb0 74 20 72 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46  t r = 0;.  unixF
82ec0 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e  ile *pFile = (un
82ed0 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 0a 20  ixFile*)id;.  . 
82ee0 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29   assert( pFile )
82ef0 3b 20 0a 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43  ; .  afpLockingC
82f00 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 20  ontext *context 
82f10 3d 20 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e  = (afpLockingCon
82f20 74 65 78 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c  text *) pFile->l
82f30 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20  ockingContext;. 
82f40 20 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20   .  /* Check if 
82f50 61 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73  a thread in this
82f60 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73   process holds s
82f70 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20  uch a lock */.  
82f80 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  if( pFile->lockt
82f90 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20  ype>SHARED_LOCK 
82fa0 29 7b 0a 20 20 20 20 72 20 3d 20 31 3b 0a 20 20  ){.    r = 1;.  
82fb0 7d 0a 20 20 0a 20 20 2f 2a 20 4f 74 68 65 72 77  }.  .  /* Otherw
82fc0 69 73 65 20 73 65 65 20 69 66 20 73 6f 6d 65 20  ise see if some 
82fd0 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 68 6f  other process ho
82fe0 6c 64 73 20 69 74 2e 0a 20 20 20 2a 2f 0a 20 20  lds it..   */.  
82ff0 69 66 20 28 20 21 72 20 29 20 7b 0a 20 20 20 20  if ( !r ) {.    
83000 2f 2a 20 6c 6f 63 6b 20 74 68 65 20 62 79 74 65  /* lock the byte
83010 20 2a 2f 0a 20 20 20 20 69 6e 74 20 66 61 69 6c   */.    int fail
83020 65 64 20 3d 20 5f 41 46 50 46 53 53 65 74 4c 6f  ed = _AFPFSSetLo
83030 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 66 69 6c 65  ck(context->file
83040 50 61 74 68 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  Path, pFile->h, 
83050 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 31  RESERVED_BYTE, 1
83060 2c 31 29 3b 20 20 0a 20 20 20 20 69 66 20 28 66  ,1);  .    if (f
83070 61 69 6c 65 64 29 20 7b 0a 20 20 20 20 20 20 2f  ailed) {.      /
83080 2a 20 69 66 20 77 65 20 66 61 69 6c 65 64 20 74  * if we failed t
83090 6f 20 67 65 74 20 74 68 65 20 6c 6f 63 6b 20 74  o get the lock t
830a0 68 65 6e 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65  hen someone else
830b0 20 6d 75 73 74 20 68 61 76 65 20 69 74 20 2a 2f   must have it */
830c0 0a 20 20 20 20 20 20 72 20 3d 20 31 3b 0a 20 20  .      r = 1;.  
830d0 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20    } else {.     
830e0 20 2f 2a 20 69 66 20 77 65 20 73 75 63 63 65 65   /* if we succee
830f0 64 65 64 20 69 6e 20 74 61 6b 69 6e 67 20 74 68  ded in taking th
83100 65 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 2c  e reserved lock,
83110 20 75 6e 6c 6f 63 6b 20 69 74 20 74 6f 20 72 65   unlock it to re
83120 73 74 6f 72 65 0a 20 20 20 20 20 20 2a 2a 20 74  store.      ** t
83130 68 65 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74  he original stat
83140 65 20 2a 2f 0a 20 20 20 20 20 20 5f 41 46 50 46  e */.      _AFPF
83150 53 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74  SSetLock(context
83160 2d 3e 66 69 6c 65 50 61 74 68 2c 20 70 46 69 6c  ->filePath, pFil
83170 65 2d 3e 68 2c 20 52 45 53 45 52 56 45 44 5f 42  e->h, RESERVED_B
83180 59 54 45 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20  YTE, 1, 0);.    
83190 7d 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 33  }.  }.  OSTRACE3
831a0 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25  ("TEST WR-LOCK %
831b0 64 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  d %d\n", pFile->
831c0 68 2c 20 72 29 3b 0a 20 20 0a 20 20 72 65 74 75  h, r);.  .  retu
831d0 72 6e 20 72 3b 0a 7d 0a 0a 2f 2a 20 41 46 50 2d  rn r;.}../* AFP-
831e0 73 74 79 6c 65 20 6c 6f 63 6b 69 6e 67 20 66 6f  style locking fo
831f0 6c 6c 6f 77 69 6e 67 20 74 68 65 20 62 65 68 61  llowing the beha
83200 76 69 6f 72 20 6f 66 20 75 6e 69 78 4c 6f 63 6b  vior of unixLock
83210 2c 20 73 65 65 20 74 68 65 20 75 6e 69 78 4c 6f  , see the unixLo
83220 63 6b 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  ck .** function 
83230 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 64 65 74  comments for det
83240 61 69 6c 73 20 6f 66 20 6c 6f 63 6b 20 6d 61 6e  ails of lock man
83250 61 67 65 6d 65 6e 74 2e 20 2a 2f 0a 73 74 61 74  agement. */.stat
83260 69 63 20 69 6e 74 20 61 66 70 55 6e 69 78 4c 6f  ic int afpUnixLo
83270 63 6b 28 4f 73 46 69 6c 65 20 2a 69 64 2c 20 69  ck(OsFile *id, i
83280 6e 74 20 6c 6f 63 6b 74 79 70 65 29 0a 7b 0a 20  nt locktype).{. 
83290 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
832a0 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20  _OK;.  unixFile 
832b0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
832c0 6c 65 2a 29 69 64 3b 0a 20 20 61 66 70 4c 6f 63  le*)id;.  afpLoc
832d0 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 63 6f 6e  kingContext *con
832e0 74 65 78 74 20 3d 20 28 61 66 70 4c 6f 63 6b 69  text = (afpLocki
832f0 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 69  ngContext *) pFi
83300 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
83310 78 74 3b 0a 20 20 69 6e 74 20 67 6f 74 50 65 6e  xt;.  int gotPen
83320 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20  dingLock = 0;.  
83330 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65  .  assert( pFile
83340 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22   );.  OSTRACE5("
83350 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 77 61  LOCK    %d %s wa
83360 73 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20  s %s pid=%d\n", 
83370 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20  pFile->h,.      
83380 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28     locktypeName(
83390 6c 6f 63 6b 74 79 70 65 29 2c 20 6c 6f 63 6b 74  locktype), lockt
833a0 79 70 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c  ypeName(pFile->l
833b0 6f 63 6b 74 79 70 65 29 2c 20 67 65 74 70 69 64  ocktype), getpid
833c0 28 29 29 3b 20 20 0a 20 20 2f 2a 20 49 66 20 74  ());  .  /* If t
833d0 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20  here is already 
833e0 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74  a lock of this t
833f0 79 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74  ype or more rest
83400 72 69 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20  rictive on the. 
83410 20 20 20 2a 2a 20 4f 73 46 69 6c 65 2c 20 64 6f     ** OsFile, do
83420 20 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20   nothing. Don't 
83430 75 73 65 20 74 68 65 20 61 66 70 5f 65 6e 64 5f  use the afp_end_
83440 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61 74 68 2c  lock: exit path,
83450 20 61 73 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74   as.    ** sqlit
83460 65 33 4f 73 45 6e 74 65 72 4d 75 74 65 78 28 29  e3OsEnterMutex()
83470 20 68 61 73 6e 27 74 20 62 65 65 6e 20 63 61 6c   hasn't been cal
83480 6c 65 64 20 79 65 74 2e 0a 20 20 20 20 2a 2f 0a  led yet..    */.
83490 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63    if( pFile->loc
834a0 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 20  ktype>=locktype 
834b0 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28  ){.    OSTRACE3(
834c0 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 6f  "LOCK    %d %s o
834d0 6b 20 28 61 6c 72 65 61 64 79 20 68 65 6c 64 29  k (already held)
834e0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20  \n", pFile->h,. 
834f0 20 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79            lockty
83500 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29  peName(locktype)
83510 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
83520 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20  LITE_OK;.  }..  
83530 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65  /* Make sure the
83540 20 6c 6f 63 6b 69 6e 67 20 73 65 71 75 65 6e 63   locking sequenc
83550 65 20 69 73 20 63 6f 72 72 65 63 74 0a 20 20 20  e is correct.   
83560 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 46   */.  assert( pF
83570 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e  ile->locktype!=N
83580 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79  O_LOCK || lockty
83590 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe==SHARED_LOCK 
835a0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63  );.  assert( loc
835b0 6b 74 79 70 65 21 3d 50 45 4e 44 49 4e 47 5f 4c  ktype!=PENDING_L
835c0 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28  OCK );.  assert(
835d0 20 6c 6f 63 6b 74 79 70 65 21 3d 52 45 53 45 52   locktype!=RESER
835e0 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46 69 6c  VED_LOCK || pFil
835f0 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  e->locktype==SHA
83600 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20  RED_LOCK );.  . 
83610 20 2f 2a 20 54 68 69 73 20 6d 75 74 65 78 20 69   /* This mutex i
83620 73 20 6e 65 65 64 65 64 20 62 65 63 61 75 73 65  s needed because
83630 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 69 73   pFile->pLock is
83640 20 73 68 61 72 65 64 20 61 63 72 6f 73 73 20 74   shared across t
83650 68 72 65 61 64 73 0a 20 20 20 20 2a 2f 0a 20 20  hreads.    */.  
83660 73 71 6c 69 74 65 33 4f 73 45 6e 74 65 72 4d 75  sqlite3OsEnterMu
83670 74 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b  tex();..  /* Mak
83680 65 20 73 75 72 65 20 74 68 65 20 63 75 72 72 65  e sure the curre
83690 6e 74 20 74 68 72 65 61 64 20 6f 77 6e 73 20 74  nt thread owns t
836a0 68 65 20 70 46 69 6c 65 2e 0a 20 20 20 20 2a 2f  he pFile..    */
836b0 0a 20 20 72 63 20 3d 20 74 72 61 6e 73 66 65 72  .  rc = transfer
836c0 4f 77 6e 65 72 73 68 69 70 28 70 46 69 6c 65 29  Ownership(pFile)
836d0 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
836e0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c  TE_OK ){.    sql
836f0 69 74 65 33 4f 73 4c 65 61 76 65 4d 75 74 65 78  ite3OsLeaveMutex
83700 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72  ();.    return r
83710 63 3b 0a 20 20 7d 0a 20 20 20 20 0a 20 20 2f 2a  c;.  }.    .  /*
83720 20 41 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20   A PENDING lock 
83730 69 73 20 6e 65 65 64 65 64 20 62 65 66 6f 72 65  is needed before
83740 20 61 63 71 75 69 72 69 6e 67 20 61 20 53 48 41   acquiring a SHA
83750 52 45 44 20 6c 6f 63 6b 20 61 6e 64 20 62 65 66  RED lock and bef
83760 6f 72 65 0a 20 20 20 20 2a 2a 20 61 63 71 75 69  ore.    ** acqui
83770 72 69 6e 67 20 61 6e 20 45 58 43 4c 55 53 49 56  ring an EXCLUSIV
83780 45 20 6c 6f 63 6b 2e 20 20 46 6f 72 20 74 68 65  E lock.  For the
83790 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68   SHARED lock, th
837a0 65 20 50 45 4e 44 49 4e 47 20 77 69 6c 6c 0a 20  e PENDING will. 
837b0 20 20 20 2a 2a 20 62 65 20 72 65 6c 65 61 73 65     ** be release
837c0 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 69 66 28 20  d..    */.  if( 
837d0 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44  locktype==SHARED
837e0 5f 4c 4f 43 4b 20 0a 20 20 20 20 20 20 7c 7c 20  _LOCK .      || 
837f0 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55  (locktype==EXCLU
83800 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 46 69  SIVE_LOCK && pFi
83810 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 50 45 4e  le->locktype<PEN
83820 44 49 4e 47 5f 4c 4f 43 4b 29 0a 20 20 20 20 20  DING_LOCK).     
83830 20 29 7b 0a 20 20 20 20 69 6e 74 20 66 61 69 6c   ){.    int fail
83840 65 64 20 3d 20 5f 41 46 50 46 53 53 65 74 4c 6f  ed = _AFPFSSetLo
83850 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 66 69 6c 65  ck(context->file
83860 50 61 74 68 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  Path, pFile->h, 
83870 0a 20 20 20 20 20 20 50 45 4e 44 49 4e 47 5f 42  .      PENDING_B
83880 59 54 45 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20  YTE, 1, 1);.    
83890 69 66 20 28 66 61 69 6c 65 64 29 20 7b 0a 20 20  if (failed) {.  
838a0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
838b0 42 55 53 59 3b 0a 20 20 20 20 20 20 67 6f 74 6f  BUSY;.      goto
838c0 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20   afp_end_lock;. 
838d0 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a     }.  }.  .  /*
838e0 20 49 66 20 63 6f 6e 74 72 6f 6c 20 67 65 74 73   If control gets
838f0 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 2c 20   to this point, 
83900 74 68 65 6e 20 61 63 74 75 61 6c 6c 79 20 67 6f  then actually go
83910 20 61 68 65 61 64 20 61 6e 64 20 6d 61 6b 65 0a   ahead and make.
83920 20 20 20 20 2a 2a 20 6f 70 65 72 61 74 69 6e 67      ** operating
83930 20 73 79 73 74 65 6d 20 63 61 6c 6c 73 20 66 6f   system calls fo
83940 72 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  r the specified 
83950 6c 6f 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 69  lock..    */.  i
83960 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  f( locktype==SHA
83970 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  RED_LOCK ){.    
83980 69 6e 74 20 6c 6b 2c 20 66 61 69 6c 65 64 3b 0a  int lk, failed;.
83990 20 20 20 20 69 6e 74 20 74 72 69 65 73 20 3d 20      int tries = 
839a0 30 3b 0a 20 20 20 20 0a 20 20 20 20 2f 2a 20 4e  0;.    .    /* N
839b0 6f 77 20 67 65 74 20 74 68 65 20 72 65 61 64 2d  ow get the read-
839c0 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 2f 2a 20 6e  lock */.    /* n
839d0 6f 74 65 20 74 68 61 74 20 74 68 65 20 71 75 61  ote that the qua
839e0 6c 69 74 79 20 6f 66 20 74 68 65 20 72 61 6e 64  lity of the rand
839f0 6f 6d 6e 65 73 73 20 64 6f 65 73 6e 27 74 20 6d  omness doesn't m
83a00 61 74 74 65 72 20 74 68 61 74 20 6d 75 63 68 20  atter that much 
83a10 2a 2f 0a 20 20 20 20 6c 6b 20 3d 20 72 61 6e 64  */.    lk = rand
83a20 6f 6d 28 29 3b 20 0a 20 20 20 20 63 6f 6e 74 65  om(); .    conte
83a30 78 74 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79  xt->sharedLockBy
83a40 74 65 20 3d 20 28 6c 6b 20 26 20 30 78 37 66 66  te = (lk & 0x7ff
83a50 66 66 66 66 66 29 25 28 53 48 41 52 45 44 5f 53  fffff)%(SHARED_S
83a60 49 5a 45 20 2d 20 31 29 3b 0a 20 20 20 20 66 61  IZE - 1);.    fa
83a70 69 6c 65 64 20 3d 20 5f 41 46 50 46 53 53 65 74  iled = _AFPFSSet
83a80 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 66 69  Lock(context->fi
83a90 6c 65 50 61 74 68 2c 20 70 46 69 6c 65 2d 3e 68  lePath, pFile->h
83aa0 2c 20 0a 20 20 20 20 20 20 53 48 41 52 45 44 5f  , .      SHARED_
83ab0 46 49 52 53 54 2b 63 6f 6e 74 65 78 74 2d 3e 73  FIRST+context->s
83ac0 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 31  haredLockByte, 1
83ad0 2c 20 31 29 3b 0a 20 20 20 20 0a 20 20 20 20 2f  , 1);.    .    /
83ae0 2a 20 44 72 6f 70 20 74 68 65 20 74 65 6d 70 6f  * Drop the tempo
83af0 72 61 72 79 20 50 45 4e 44 49 4e 47 20 6c 6f 63  rary PENDING loc
83b00 6b 20 2a 2f 0a 20 20 20 20 69 66 20 28 5f 41 46  k */.    if (_AF
83b10 50 46 53 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65  PFSSetLock(conte
83b20 78 74 2d 3e 66 69 6c 65 50 61 74 68 2c 20 70 46  xt->filePath, pF
83b30 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47 5f  ile->h, PENDING_
83b40 42 59 54 45 2c 20 31 2c 20 30 29 29 20 7b 0a 20  BYTE, 1, 0)) {. 
83b50 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
83b60 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b 20 20  _IOERR_UNLOCK;  
83b70 2f 2a 20 54 68 69 73 20 73 68 6f 75 6c 64 20 6e  /* This should n
83b80 65 76 65 72 20 68 61 70 70 65 6e 20 2a 2f 0a 20  ever happen */. 
83b90 20 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e       goto afp_en
83ba0 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20 20  d_lock;.    }.  
83bb0 20 20 0a 20 20 20 20 69 66 28 20 66 61 69 6c 65    .    if( faile
83bc0 64 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  d ){.      rc = 
83bd0 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20  SQLITE_BUSY;.   
83be0 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20   } else {.      
83bf0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
83c00 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20  = SHARED_LOCK;. 
83c10 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
83c20 20 20 2f 2a 20 54 68 65 20 72 65 71 75 65 73 74    /* The request
83c30 20 77 61 73 20 66 6f 72 20 61 20 52 45 53 45 52   was for a RESER
83c40 56 45 44 20 6f 72 20 45 58 43 4c 55 53 49 56 45  VED or EXCLUSIVE
83c50 20 6c 6f 63 6b 2e 20 20 49 74 20 69 73 0a 20 20   lock.  It is.  
83c60 20 20 2a 2a 20 61 73 73 75 6d 65 64 20 74 68 61    ** assumed tha
83c70 74 20 74 68 65 72 65 20 69 73 20 61 20 53 48 41  t there is a SHA
83c80 52 45 44 20 6f 72 20 67 72 65 61 74 65 72 20 6c  RED or greater l
83c90 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 0a  ock on the file.
83ca0 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 2e 0a      ** already..
83cb0 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 66      */.    int f
83cc0 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 61  ailed = 0;.    a
83cd0 73 73 65 72 74 28 20 30 21 3d 70 46 69 6c 65 2d  ssert( 0!=pFile-
83ce0 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20  >locktype );.   
83cf0 20 69 66 20 28 6c 6f 63 6b 74 79 70 65 20 3e 3d   if (locktype >=
83d00 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 26   RESERVED_LOCK &
83d10 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  & pFile->locktyp
83d20 65 20 3c 20 52 45 53 45 52 56 45 44 5f 4c 4f 43  e < RESERVED_LOC
83d30 4b 29 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  K) {.        /* 
83d40 41 63 71 75 69 72 65 20 61 20 52 45 53 45 52 56  Acquire a RESERV
83d50 45 44 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20  ED lock */.     
83d60 20 20 20 66 61 69 6c 65 64 20 3d 20 5f 41 46 50     failed = _AFP
83d70 46 53 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78  FSSetLock(contex
83d80 74 2d 3e 66 69 6c 65 50 61 74 68 2c 20 70 46 69  t->filePath, pFi
83d90 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56 45 44 5f  le->h, RESERVED_
83da0 42 59 54 45 2c 20 31 2c 31 29 3b 0a 20 20 20 20  BYTE, 1,1);.    
83db0 7d 0a 20 20 20 20 69 66 20 28 21 66 61 69 6c 65  }.    if (!faile
83dc0 64 20 26 26 20 6c 6f 63 6b 74 79 70 65 20 3d 3d  d && locktype ==
83dd0 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29   EXCLUSIVE_LOCK)
83de0 20 7b 0a 20 20 20 20 20 20 2f 2a 20 41 63 71 75   {.      /* Acqu
83df0 69 72 65 20 61 6e 20 45 58 43 4c 55 53 49 56 45  ire an EXCLUSIVE
83e00 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 20   lock */.       
83e10 20 0a 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76   .      /* Remov
83e20 65 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63  e the shared loc
83e30 6b 20 62 65 66 6f 72 65 20 74 72 79 69 6e 67 20  k before trying 
83e40 74 68 65 20 72 61 6e 67 65 2e 20 20 77 65 27 6c  the range.  we'l
83e50 6c 20 6e 65 65 64 20 74 6f 20 0a 20 20 20 20 20  l need to .     
83e60 20 2a 2a 20 72 65 65 73 74 61 62 6c 69 73 68 20   ** reestablish 
83e70 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20  the shared lock 
83e80 69 66 20 77 65 20 63 61 6e 27 74 20 67 65 74 20  if we can't get 
83e90 74 68 65 20 20 61 66 70 55 6e 69 78 55 6e 6c 6f  the  afpUnixUnlo
83ea0 63 6b 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  ck.      */.    
83eb0 20 20 69 66 20 28 21 5f 41 46 50 46 53 53 65 74    if (!_AFPFSSet
83ec0 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 66 69  Lock(context->fi
83ed0 6c 65 50 61 74 68 2c 20 70 46 69 6c 65 2d 3e 68  lePath, pFile->h
83ee0 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 2b  , SHARED_FIRST +
83ef0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
83f00 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 65 78            contex
83f10 74 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74  t->sharedLockByt
83f20 65 2c 20 31 2c 20 30 29 29 20 7b 0a 20 20 20 20  e, 1, 0)) {.    
83f30 20 20 20 20 2f 2a 20 6e 6f 77 20 61 74 74 65 6d      /* now attem
83f40 6d 70 74 20 74 6f 20 67 65 74 20 74 68 65 20 65  mpt to get the e
83f50 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 72 61  xclusive lock ra
83f60 6e 67 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 66  nge */.        f
83f70 61 69 6c 65 64 20 3d 20 5f 41 46 50 46 53 53 65  ailed = _AFPFSSe
83f80 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 66  tLock(context->f
83f90 69 6c 65 50 61 74 68 2c 20 70 46 69 6c 65 2d 3e  ilePath, pFile->
83fa0 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 2c  h, SHARED_FIRST,
83fb0 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
83fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
83fd0 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 31 29   SHARED_SIZE, 1)
83fe0 3b 0a 20 20 20 20 20 20 20 20 69 66 20 28 66 61  ;.        if (fa
83ff0 69 6c 65 64 20 26 26 20 5f 41 46 50 46 53 53 65  iled && _AFPFSSe
84000 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 66  tLock(context->f
84010 69 6c 65 50 61 74 68 2c 20 70 46 69 6c 65 2d 3e  ilePath, pFile->
84020 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 20  h, SHARED_FIRST 
84030 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  +.              
84040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
84050 20 20 20 20 20 20 63 6f 6e 74 65 78 74 2d 3e 73        context->s
84060 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 31  haredLockByte, 1
84070 2c 20 31 29 29 20 7b 0a 20 20 20 20 20 20 20 20  , 1)) {.        
84080 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f    rc = SQLITE_IO
84090 45 52 52 5f 52 44 4c 4f 43 4b 3b 20 2f 2a 20 74  ERR_RDLOCK; /* t
840a0 68 69 73 20 73 68 6f 75 6c 64 20 6e 65 76 65 72  his should never
840b0 20 68 61 70 70 65 6e 20 2a 2f 0a 20 20 20 20 20   happen */.     
840c0 20 20 20 7d 0a 20 20 20 20 20 20 7d 20 65 6c 73     }.      } els
840d0 65 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 2a  e {.        /* *
840e0 2f 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53  /.        rc = S
840f0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f  QLITE_IOERR_UNLO
84100 43 4b 3b 20 2f 2a 20 74 68 69 73 20 73 68 6f 75  CK; /* this shou
84110 6c 64 20 6e 65 76 65 72 20 68 61 70 70 65 6e 20  ld never happen 
84120 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  */.      }.    }
84130 0a 20 20 20 20 69 66 28 20 66 61 69 6c 65 64 20  .    if( failed 
84140 26 26 20 72 63 20 3d 3d 20 53 51 4c 49 54 45 5f  && rc == SQLITE_
84150 4f 4b 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  OK){.      rc = 
84160 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20  SQLITE_BUSY;.   
84170 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 69 66 28 20   }.  }.  .  if( 
84180 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
84190 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  .    pFile->lock
841a0 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b  type = locktype;
841b0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63  .  }else if( loc
841c0 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45  ktype==EXCLUSIVE
841d0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 70 46 69  _LOCK ){.    pFi
841e0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 50  le->locktype = P
841f0 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 7d  ENDING_LOCK;.  }
84200 0a 20 20 0a 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b  .  .afp_end_lock
84210 3a 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4c  :.    sqlite3OsL
84220 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f  eaveMutex();.  O
84230 53 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 20 20  STRACE4("LOCK   
84240 20 25 64 20 25 73 20 25 73 5c 6e 22 2c 20 70 46   %d %s %s\n", pF
84250 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65  ile->h, locktype
84260 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20  Name(locktype), 
84270 0a 20 20 20 20 20 20 20 20 20 72 63 3d 3d 53 51  .         rc==SQ
84280 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a  LITE_OK ? "ok" :
84290 20 22 66 61 69 6c 65 64 22 29 3b 0a 20 20 72 65   "failed");.  re
842a0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 20  turn rc;.}../*. 
842b0 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63  ** Lower the loc
842c0 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69  king level on fi
842d0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70 46  le descriptor pF
842e0 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e  ile to locktype.
842f0 20 20 6c 6f 63 6b 74 79 70 65 0a 20 2a 2a 20 6d    locktype. ** m
84300 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f  ust be either NO
84310 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f  _LOCK or SHARED_
84320 4c 4f 43 4b 2e 0a 20 2a 2a 0a 20 2a 2a 20 49 66  LOCK.. **. ** If
84330 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76   the locking lev
84340 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64  el of the file d
84350 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72  escriptor is alr
84360 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77  eady at or below
84370 0a 20 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74  . ** the request
84380 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  ed locking level
84390 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  , this routine i
843a0 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 2a 2f 0a 73  s a no-op.. */.s
843b0 74 61 74 69 63 20 69 6e 74 20 61 66 70 55 6e 69  tatic int afpUni
843c0 78 55 6e 6c 6f 63 6b 28 4f 73 46 69 6c 65 20 2a  xUnlock(OsFile *
843d0 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65  id, int locktype
843e0 29 20 7b 0a 20 20 73 74 72 75 63 74 20 66 6c 6f  ) {.  struct flo
843f0 63 6b 20 6c 6f 63 6b 3b 0a 20 20 69 6e 74 20 72  ck lock;.  int r
84400 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
84410 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
84420 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
84430 3b 0a 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f  ;.  afpLockingCo
84440 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 20 3d  ntext *context =
84450 20 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74   (afpLockingCont
84460 65 78 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f  ext *) pFile->lo
84470 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 0a 20  ckingContext;.. 
84480 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29   assert( pFile )
84490 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 55 4e  ;.  OSTRACE5("UN
844a0 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73 20  LOCK  %d %d was 
844b0 25 64 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46  %d pid=%d\n", pF
844c0 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65  ile->h, locktype
844d0 2c 0a 20 20 20 20 20 20 20 20 20 70 46 69 6c 65  ,.         pFile
844e0 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74 70  ->locktype, getp
844f0 69 64 28 29 29 3b 0a 20 20 0a 20 20 61 73 73 65  id());.  .  asse
84500 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48  rt( locktype<=SH
84510 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69  ARED_LOCK );.  i
84520 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  f( pFile->lockty
84530 70 65 3c 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a  pe<=locktype ){.
84540 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
84550 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20  E_OK;.  }.  if( 
84560 43 48 45 43 4b 5f 54 48 52 45 41 44 49 44 28 70  CHECK_THREADID(p
84570 46 69 6c 65 29 20 29 7b 0a 20 20 20 20 72 65 74  File) ){.    ret
84580 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  urn SQLITE_MISUS
84590 45 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  E;.  }.  sqlite3
845a0 4f 73 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a  OsEnterMutex();.
845b0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63    if( pFile->loc
845c0 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43  ktype>SHARED_LOC
845d0 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 6c 6f 63  K ){.    if( loc
845e0 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
845f0 43 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  CK ){.      int 
84600 66 61 69 6c 65 64 20 3d 20 30 3b 0a 0a 20 20 20  failed = 0;..   
84610 20 20 20 2f 2a 20 75 6e 6c 6f 63 6b 20 74 68 65     /* unlock the
84620 20 65 78 63 6c 75 73 69 76 65 20 72 61 6e 67 65   exclusive range
84630 20 2d 20 74 68 65 6e 20 72 65 2d 65 73 74 61 62   - then re-estab
84640 6c 69 73 68 20 74 68 65 20 73 68 61 72 65 64 20  lish the shared 
84650 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 69 66  lock */.      if
84660 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70   (pFile->locktyp
84670 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  e==EXCLUSIVE_LOC
84680 4b 29 20 7b 0a 20 20 20 20 20 20 20 20 66 61 69  K) {.        fai
84690 6c 65 64 20 3d 20 5f 41 46 50 46 53 53 65 74 4c  led = _AFPFSSetL
846a0 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 66 69 6c  ock(context->fil
846b0 65 50 61 74 68 2c 20 70 46 69 6c 65 2d 3e 68 2c  ePath, pFile->h,
846c0 20 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 0a   SHARED_FIRST, .
846d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
846e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
846f0 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 29   SHARED_SIZE, 0)
84700 3b 0a 20 20 20 20 20 20 20 20 69 66 20 28 21 66  ;.        if (!f
84710 61 69 6c 65 64 29 20 7b 0a 20 20 20 20 20 20 20  ailed) {.       
84720 20 20 20 2f 2a 20 73 75 63 63 65 73 73 66 75 6c     /* successful
84730 6c 79 20 72 65 6d 6f 76 65 64 20 74 68 65 20 65  ly removed the e
84740 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 2a 2f  xclusive lock */
84750 0a 20 20 20 20 20 20 20 20 20 20 69 66 20 28 5f  .          if (_
84760 41 46 50 46 53 53 65 74 4c 6f 63 6b 28 63 6f 6e  AFPFSSetLock(con
84770 74 65 78 74 2d 3e 66 69 6c 65 50 61 74 68 2c 20  text->filePath, 
84780 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44  pFile->h, SHARED
84790 5f 46 49 52 53 54 2b 0a 20 20 20 20 20 20 20 20  _FIRST+.        
847a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
847b0 20 20 20 20 63 6f 6e 74 65 78 74 2d 3e 73 68 61      context->sha
847c0 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 31 2c 20  redLockByte, 1, 
847d0 31 29 29 20 7b 0a 20 20 20 20 20 20 20 20 20 20  1)) {.          
847e0 20 20 2f 2a 20 66 61 69 6c 65 64 20 74 6f 20 72    /* failed to r
847f0 65 2d 65 73 74 61 62 6c 69 73 68 20 6f 75 72 20  e-establish our 
84800 73 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20  shared lock */. 
84810 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20             rc = 
84820 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 44 4c  SQLITE_IOERR_RDL
84830 4f 43 4b 3b 20 2f 2a 20 54 68 69 73 20 73 68 6f  OCK; /* This sho
84840 75 6c 64 20 6e 65 76 65 72 20 68 61 70 70 65 6e  uld never happen
84850 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 7d 0a   */.          }.
84860 20 20 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b          } else {
84870 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  .          /* Th
84880 69 73 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20  is should never 
84890 68 61 70 70 65 6e 20 2d 20 66 61 69 6c 65 64 20  happen - failed 
848a0 74 6f 20 75 6e 6c 6f 63 6b 20 74 68 65 20 65 78  to unlock the ex
848b0 63 6c 75 73 69 76 65 20 72 61 6e 67 65 20 2a 2f  clusive range */
848c0 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
848d0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c  SQLITE_IOERR_UNL
848e0 4f 43 4b 3b 0a 20 20 20 20 20 20 20 20 7d 20 0a  OCK;.        } .
848f0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
84900 20 20 69 66 20 28 72 63 20 3d 3d 20 53 51 4c 49    if (rc == SQLI
84910 54 45 5f 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e  TE_OK && pFile->
84920 6c 6f 63 6b 74 79 70 65 3e 3d 50 45 4e 44 49 4e  locktype>=PENDIN
84930 47 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 20 20  G_LOCK) {.      
84940 69 66 20 28 5f 41 46 50 46 53 53 65 74 4c 6f 63  if (_AFPFSSetLoc
84950 6b 28 63 6f 6e 74 65 78 74 2d 3e 66 69 6c 65 50  k(context->fileP
84960 61 74 68 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 50  ath, pFile->h, P
84970 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20  ENDING_BYTE, 1, 
84980 30 29 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  0)){.        /* 
84990 66 61 69 6c 65 64 20 74 6f 20 72 65 6c 65 61 73  failed to releas
849a0 65 20 74 68 65 20 70 65 6e 64 69 6e 67 20 6c 6f  e the pending lo
849b0 63 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63  ck */.        rc
849c0 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f   = SQLITE_IOERR_
849d0 55 4e 4c 4f 43 4b 3b 20 2f 2a 20 54 68 69 73 20  UNLOCK; /* This 
849e0 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 68 61 70  should never hap
849f0 70 65 6e 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20  pen */.      }. 
84a00 20 20 20 7d 20 0a 20 20 20 20 69 66 20 28 72 63     } .    if (rc
84a10 20 3d 3d 20 53 51 4c 49 54 45 5f 4f 4b 20 26 26   == SQLITE_OK &&
84a20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
84a30 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29  >=RESERVED_LOCK)
84a40 20 7b 0a 20 20 20 20 20 20 69 66 20 28 5f 41 46   {.      if (_AF
84a50 50 46 53 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65  PFSSetLock(conte
84a60 78 74 2d 3e 66 69 6c 65 50 61 74 68 2c 20 70 46  xt->filePath, pF
84a70 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56 45 44  ile->h, RESERVED
84a80 5f 42 59 54 45 2c 20 31 2c 20 30 29 29 20 7b 0a  _BYTE, 1, 0)) {.
84a90 20 20 20 20 20 20 20 20 2f 2a 20 66 61 69 6c 65          /* faile
84aa0 64 20 74 6f 20 72 65 6c 65 61 73 65 20 74 68 65  d to release the
84ab0 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 2a   reserved lock *
84ac0 2f 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53  /.        rc = S
84ad0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f  QLITE_IOERR_UNLO
84ae0 43 4b 3b 20 20 2f 2a 20 54 68 69 73 20 73 68 6f  CK;  /* This sho
84af0 75 6c 64 20 6e 65 76 65 72 20 68 61 70 70 65 6e  uld never happen
84b00 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   */.      }.    
84b10 7d 20 0a 20 20 7d 0a 20 20 69 66 28 20 6c 6f 63  } .  }.  if( loc
84b20 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29  ktype==NO_LOCK )
84b30 7b 0a 20 20 20 20 69 6e 74 20 66 61 69 6c 65 64  {.    int failed
84b40 20 3d 20 5f 41 46 50 46 53 53 65 74 4c 6f 63 6b   = _AFPFSSetLock
84b50 28 63 6f 6e 74 65 78 74 2d 3e 66 69 6c 65 50 61  (context->filePa
84b60 74 68 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 0a 20  th, pFile->h, . 
84b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
84b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 48                SH
84b90 41 52 45 44 5f 46 49 52 53 54 20 2b 20 63 6f 6e  ARED_FIRST + con
84ba0 74 65 78 74 2d 3e 73 68 61 72 65 64 4c 6f 63 6b  text->sharedLock
84bb0 42 79 74 65 2c 20 31 2c 20 30 29 3b 0a 20 20 20  Byte, 1, 0);.   
84bc0 20 69 66 20 28 66 61 69 6c 65 64 29 20 7b 0a 20   if (failed) {. 
84bd0 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
84be0 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b 20 20  _IOERR_UNLOCK;  
84bf0 2f 2a 20 54 68 69 73 20 73 68 6f 75 6c 64 20 6e  /* This should n
84c00 65 76 65 72 20 68 61 70 70 65 6e 20 2a 2f 0a 20  ever happen */. 
84c10 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 20 28 72     }.  }.  if (r
84c20 63 20 3d 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a  c == SQLITE_OK).
84c30 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74      pFile->lockt
84c40 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a  ype = locktype;.
84c50 20 20 73 71 6c 69 74 65 33 4f 73 4c 65 61 76 65    sqlite3OsLeave
84c60 4d 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75 72  Mutex();.  retur
84c70 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 20 2a 2a 20  n rc;.}../*. ** 
84c80 43 6c 6f 73 65 20 61 20 66 69 6c 65 20 26 20 63  Close a file & c
84c90 6c 65 61 6e 75 70 20 41 46 50 20 73 70 65 63 69  leanup AFP speci
84ca0 66 69 63 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74  fic locking cont
84cb0 65 78 74 20 0a 20 2a 2f 0a 73 74 61 74 69 63 20  ext . */.static 
84cc0 69 6e 74 20 61 66 70 55 6e 69 78 43 6c 6f 73 65  int afpUnixClose
84cd0 28 4f 73 46 69 6c 65 20 2a 2a 70 49 64 29 20 7b  (OsFile **pId) {
84ce0 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 69 64 20  .  unixFile *id 
84cf0 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 2a 70 49  = (unixFile*)*pI
84d00 64 3b 0a 20 20 0a 20 20 69 66 28 20 21 69 64 20  d;.  .  if( !id 
84d10 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
84d20 4f 4b 3b 0a 20 20 61 66 70 55 6e 69 78 55 6e 6c  OK;.  afpUnixUnl
84d30 6f 63 6b 28 2a 70 49 64 2c 20 4e 4f 5f 4c 4f 43  ock(*pId, NO_LOC
84d40 4b 29 3b 0a 20 20 2f 2a 20 66 72 65 65 20 74 68  K);.  /* free th
84d50 65 20 41 46 50 20 6c 6f 63 6b 69 6e 67 20 73 74  e AFP locking st
84d60 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 69 66 20  ructure */.  if 
84d70 28 69 64 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74  (id->lockingCont
84d80 65 78 74 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a 20  ext != NULL) {. 
84d90 20 20 20 69 66 20 28 28 28 61 66 70 4c 6f 63 6b     if (((afpLock
84da0 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 69 64 2d  ingContext *)id-
84db0 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29  >lockingContext)
84dc0 2d 3e 66 69 6c 65 50 61 74 68 20 21 3d 20 4e 55  ->filePath != NU
84dd0 4c 4c 29 0a 20 20 20 20 20 20 73 71 6c 69 74 65  LL).      sqlite
84de0 33 54 68 72 65 61 64 53 61 66 65 46 72 65 65 28  3ThreadSafeFree(
84df0 28 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74  ((afpLockingCont
84e00 65 78 74 2a 29 69 64 2d 3e 6c 6f 63 6b 69 6e 67  ext*)id->locking
84e10 43 6f 6e 74 65 78 74 29 2d 3e 66 69 6c 65 50 61  Context)->filePa
84e20 74 68 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  th);.    sqlite3
84e30 54 68 72 65 61 64 53 61 66 65 46 72 65 65 28 69  ThreadSafeFree(i
84e40 64 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  d->lockingContex
84e50 74 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 69 66 28  t);.  }.  .  if(
84e60 20 69 64 2d 3e 64 69 72 66 64 3e 3d 30 20 29 20   id->dirfd>=0 ) 
84e70 63 6c 6f 73 65 28 69 64 2d 3e 64 69 72 66 64 29  close(id->dirfd)
84e80 3b 0a 20 20 69 64 2d 3e 64 69 72 66 64 20 3d 20  ;.  id->dirfd = 
84e90 2d 31 3b 0a 20 20 63 6c 6f 73 65 28 69 64 2d 3e  -1;.  close(id->
84ea0 68 29 3b 0a 20 20 69 64 2d 3e 69 73 4f 70 65 6e  h);.  id->isOpen
84eb0 20 3d 20 30 3b 0a 20 20 4f 53 54 52 41 43 45 32   = 0;.  OSTRACE2
84ec0 28 22 43 4c 4f 53 45 20 20 20 25 2d 33 64 5c 6e  ("CLOSE   %-3d\n
84ed0 22 2c 20 69 64 2d 3e 68 29 3b 0a 20 20 4f 70 65  ", id->h);.  Ope
84ee0 6e 43 6f 75 6e 74 65 72 28 2d 31 29 3b 0a 20 20  nCounter(-1);.  
84ef0 73 71 6c 69 74 65 33 54 68 72 65 61 64 53 61 66  sqlite3ThreadSaf
84f00 65 46 72 65 65 28 69 64 29 3b 0a 20 20 2a 70 49  eFree(id);.  *pI
84f10 64 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20  d = 0;.  return 
84f20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 23  SQLITE_OK;.}...#
84f30 70 72 61 67 6d 61 20 6d 61 72 6b 20 66 6c 6f 63  pragma mark floc
84f40 6b 28 29 20 73 74 79 6c 65 20 6c 6f 63 6b 69 6e  k() style lockin
84f50 67 0a 0a 2f 2a 0a 20 2a 2a 20 54 68 65 20 66 6c  g../*. ** The fl
84f60 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  ockLockingContex
84f70 74 20 69 73 20 6e 6f 74 20 75 73 65 64 0a 20 2a  t is not used. *
84f80 2f 0a 74 79 70 65 64 65 66 20 76 6f 69 64 20 66  /.typedef void f
84f90 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  lockLockingConte
84fa0 78 74 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  xt;..static int 
84fb0 66 6c 6f 63 6b 55 6e 69 78 43 68 65 63 6b 52 65  flockUnixCheckRe
84fc0 73 65 72 76 65 64 4c 6f 63 6b 28 4f 73 46 69 6c  servedLock(OsFil
84fd0 65 20 2a 69 64 29 20 7b 0a 20 20 75 6e 69 78 46  e *id) {.  unixF
84fe0 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e  ile *pFile = (un
84ff0 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 0a 20  ixFile*)id;.  . 
85000 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b   if (pFile->lock
85010 74 79 70 65 20 3d 3d 20 52 45 53 45 52 56 45 44  type == RESERVED
85020 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 72 65 74  _LOCK) {.    ret
85030 75 72 6e 20 31 3b 20 2f 2a 20 61 6c 72 65 61 64  urn 1; /* alread
85040 79 20 68 61 76 65 20 61 20 72 65 73 65 72 76 65  y have a reserve
85050 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 7d 20 65 6c  d lock */.  } el
85060 73 65 20 7b 0a 20 20 20 20 2f 2a 20 61 74 74 65  se {.    /* atte
85070 6d 70 74 20 74 6f 20 67 65 74 20 74 68 65 20 6c  mpt to get the l
85080 6f 63 6b 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72  ock */.    int r
85090 63 20 3d 20 66 6c 6f 63 6b 28 70 46 69 6c 65 2d  c = flock(pFile-
850a0 3e 68 2c 20 4c 4f 43 4b 5f 45 58 20 7c 20 4c 4f  >h, LOCK_EX | LO
850b0 43 4b 5f 4e 42 29 3b 0a 20 20 20 20 69 66 20 28  CK_NB);.    if (
850c0 21 72 63 29 20 7b 0a 20 20 20 20 20 20 2f 2a 20  !rc) {.      /* 
850d0 67 6f 74 20 74 68 65 20 6c 6f 63 6b 2c 20 75 6e  got the lock, un
850e0 6c 6f 63 6b 20 69 74 20 2a 2f 0a 20 20 20 20 20  lock it */.     
850f0 20 66 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c   flock(pFile->h,
85100 20 4c 4f 43 4b 5f 55 4e 29 3b 0a 20 20 20 20 20   LOCK_UN);.     
85110 20 72 65 74 75 72 6e 20 30 3b 20 20 2f 2a 20 6e   return 0;  /* n
85120 6f 20 6f 6e 65 20 68 61 73 20 69 74 20 72 65 73  o one has it res
85130 65 72 76 65 64 20 2a 2f 0a 20 20 20 20 7d 0a 20  erved */.    }. 
85140 20 20 20 72 65 74 75 72 6e 20 31 3b 20 2f 2a 20     return 1; /* 
85150 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 6d 69 67  someone else mig
85160 68 74 20 68 61 76 65 20 69 74 20 72 65 73 65 72  ht have it reser
85170 76 65 64 20 2a 2f 0a 20 20 7d 0a 7d 0a 0a 73 74  ved */.  }.}..st
85180 61 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b 55 6e  atic int flockUn
85190 69 78 4c 6f 63 6b 28 4f 73 46 69 6c 65 20 2a 69  ixLock(OsFile *i
851a0 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29  d, int locktype)
851b0 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70   {.  unixFile *p
851c0 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
851d0 2a 29 69 64 3b 0a 20 20 0a 20 20 2f 2a 20 69 66  *)id;.  .  /* if
851e0 20 77 65 20 61 6c 72 65 61 64 79 20 68 61 76 65   we already have
851f0 20 61 20 6c 6f 63 6b 2c 20 69 74 20 69 73 20 65   a lock, it is e
85200 78 63 6c 75 73 69 76 65 2e 20 20 0a 20 20 2a 2a  xclusive.  .  **
85210 20 4a 75 73 74 20 61 64 6a 75 73 74 20 6c 65 76   Just adjust lev
85220 65 6c 20 61 6e 64 20 70 75 6e 74 20 6f 6e 20 6f  el and punt on o
85230 75 74 74 61 20 68 65 72 65 2e 20 2a 2f 0a 20 20  utta here. */.  
85240 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  if (pFile->lockt
85250 79 70 65 20 3e 20 4e 4f 5f 4c 4f 43 4b 29 20 7b  ype > NO_LOCK) {
85260 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  .    pFile->lock
85270 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b  type = locktype;
85280 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
85290 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20  TE_OK;.  }.  .  
852a0 2f 2a 20 67 72 61 62 20 61 6e 20 65 78 63 6c 75  /* grab an exclu
852b0 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69  sive lock */.  i
852c0 6e 74 20 72 63 20 3d 20 66 6c 6f 63 6b 28 70 46  nt rc = flock(pF
852d0 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 45 58 20  ile->h, LOCK_EX 
852e0 7c 20 4c 4f 43 4b 5f 4e 42 29 3b 0a 20 20 69 66  | LOCK_NB);.  if
852f0 20 28 72 63 29 20 7b 0a 20 20 20 20 2f 2a 20 64   (rc) {.    /* d
85300 69 64 6e 27 74 20 67 65 74 2c 20 6d 75 73 74 20  idn't get, must 
85310 62 65 20 62 75 73 79 20 2a 2f 0a 20 20 20 20 72  be busy */.    r
85320 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53  eturn SQLITE_BUS
85330 59 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20  Y;.  } else {.  
85340 20 20 2f 2a 20 67 6f 74 20 69 74 2c 20 73 65 74    /* got it, set
85350 20 74 68 65 20 74 79 70 65 20 61 6e 64 20 72 65   the type and re
85360 74 75 72 6e 20 6f 6b 20 2a 2f 0a 20 20 20 20 70  turn ok */.    p
85370 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  File->locktype =
85380 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 72   locktype;.    r
85390 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
853a0 0a 20 20 7d 0a 7d 0a 0a 73 74 61 74 69 63 20 69  .  }.}..static i
853b0 6e 74 20 66 6c 6f 63 6b 55 6e 69 78 55 6e 6c 6f  nt flockUnixUnlo
853c0 63 6b 28 4f 73 46 69 6c 65 20 2a 69 64 2c 20 69  ck(OsFile *id, i
853d0 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20  nt locktype) {. 
853e0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
853f0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
85400 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 6c  ;.  .  assert( l
85410 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f  ocktype<=SHARED_
85420 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a 20  LOCK );.  .  /* 
85430 6e 6f 2d 6f 70 20 69 66 20 70 6f 73 73 69 62 6c  no-op if possibl
85440 65 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65  e */.  if( pFile
85450 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 6c 6f 63 6b  ->locktype==lock
85460 74 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75  type ){.    retu
85470 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
85480 7d 0a 20 20 0a 20 20 2f 2a 20 73 68 61 72 65 64  }.  .  /* shared
85490 20 63 61 6e 20 6a 75 73 74 20 62 65 20 73 65 74   can just be set
854a0 20 62 65 63 61 75 73 65 20 77 65 20 61 6c 77 61   because we alwa
854b0 79 73 20 68 61 76 65 20 61 6e 20 65 78 63 6c 75  ys have an exclu
854c0 73 69 76 65 20 2a 2f 0a 20 20 69 66 20 28 6c 6f  sive */.  if (lo
854d0 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c  cktype==SHARED_L
854e0 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c 65  OCK) {.    pFile
854f0 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63  ->locktype = loc
85500 6b 74 79 70 65 3b 0a 20 20 20 20 72 65 74 75 72  ktype;.    retur
85510 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
85520 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2c 20 72 65 61  .  .  /* no, rea
85530 6c 6c 79 2c 20 75 6e 6c 6f 63 6b 2e 20 2a 2f 0a  lly, unlock. */.
85540 20 20 69 6e 74 20 72 63 20 3d 20 66 6c 6f 63 6b    int rc = flock
85550 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f  (pFile->h, LOCK_
85560 55 4e 29 3b 0a 20 20 69 66 20 28 72 63 29 0a 20  UN);.  if (rc). 
85570 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
85580 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b 0a 20  _IOERR_UNLOCK;. 
85590 20 65 6c 73 65 20 7b 0a 20 20 20 20 70 46 69 6c   else {.    pFil
855a0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f  e->locktype = NO
855b0 5f 4c 4f 43 4b 3b 0a 20 20 20 20 72 65 74 75 72  _LOCK;.    retur
855c0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
855d0 0a 7d 0a 0a 2f 2a 0a 20 2a 2a 20 43 6c 6f 73 65  .}../*. ** Close
855e0 20 61 20 66 69 6c 65 2e 0a 20 2a 2f 0a 73 74 61   a file.. */.sta
855f0 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b 55 6e 69  tic int flockUni
85600 78 43 6c 6f 73 65 28 4f 73 46 69 6c 65 20 2a 2a  xClose(OsFile **
85610 70 49 64 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c  pId) {.  unixFil
85620 65 20 2a 69 64 20 3d 20 28 75 6e 69 78 46 69 6c  e *id = (unixFil
85630 65 2a 29 2a 70 49 64 3b 0a 20 20 0a 20 20 69 66  e*)*pId;.  .  if
85640 28 20 21 69 64 20 29 20 72 65 74 75 72 6e 20 53  ( !id ) return S
85650 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 66 6c 6f 63  QLITE_OK;.  floc
85660 6b 55 6e 69 78 55 6e 6c 6f 63 6b 28 2a 70 49 64  kUnixUnlock(*pId
85670 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 0a 20  , NO_LOCK);.  . 
85680 20 69 66 28 20 69 64 2d 3e 64 69 72 66 64 3e 3d   if( id->dirfd>=
85690 30 20 29 20 63 6c 6f 73 65 28 69 64 2d 3e 64 69  0 ) close(id->di
856a0 72 66 64 29 3b 0a 20 20 69 64 2d 3e 64 69 72 66  rfd);.  id->dirf
856b0 64 20 3d 20 2d 31 3b 0a 20 20 73 71 6c 69 74 65  d = -1;.  sqlite
856c0 33 4f 73 45 6e 74 65 72 4d 75 74 65 78 28 29 3b  3OsEnterMutex();
856d0 0a 20 20 0a 20 20 63 6c 6f 73 65 28 69 64 2d 3e  .  .  close(id->
856e0 68 29 3b 20 20 0a 20 20 73 71 6c 69 74 65 33 4f  h);  .  sqlite3O
856f0 73 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20  sLeaveMutex();. 
85700 20 69 64 2d 3e 69 73 4f 70 65 6e 20 3d 20 30 3b   id->isOpen = 0;
85710 0a 20 20 4f 53 54 52 41 43 45 32 28 22 43 4c 4f  .  OSTRACE2("CLO
85720 53 45 20 20 20 25 2d 33 64 5c 6e 22 2c 20 69 64  SE   %-3d\n", id
85730 2d 3e 68 29 3b 0a 20 20 4f 70 65 6e 43 6f 75 6e  ->h);.  OpenCoun
85740 74 65 72 28 2d 31 29 3b 0a 20 20 73 71 6c 69 74  ter(-1);.  sqlit
85750 65 33 54 68 72 65 61 64 53 61 66 65 46 72 65 65  e3ThreadSafeFree
85760 28 69 64 29 3b 0a 20 20 2a 70 49 64 20 3d 20 30  (id);.  *pId = 0
85770 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
85780 45 5f 4f 4b 3b 0a 7d 0a 0a 23 70 72 61 67 6d 61  E_OK;.}..#pragma
85790 20 6d 61 72 6b 20 4f 6c 64 2d 53 63 68 6f 6f 6c   mark Old-School
857a0 20 2e 6c 6f 63 6b 20 66 69 6c 65 20 62 61 73 65   .lock file base
857b0 64 20 6c 6f 63 6b 69 6e 67 0a 0a 2f 2a 0a 20 2a  d locking../*. *
857c0 2a 20 54 68 65 20 64 6f 74 6c 6f 63 6b 4c 6f 63  * The dotlockLoc
857d0 6b 69 6e 67 43 6f 6e 74 65 78 74 20 73 74 72 75  kingContext stru
857e0 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61  cture contains a
857f0 6c 6c 20 64 6f 74 6c 6f 63 6b 20 28 2e 6c 6f 63  ll dotlock (.loc
85800 6b 29 20 6c 6f 63 6b 0a 20 2a 2a 20 73 70 65 63  k) lock. ** spec
85810 69 66 69 63 20 73 74 61 74 65 0a 20 2a 2f 0a 74  ific state. */.t
85820 79 70 65 64 65 66 20 73 74 72 75 63 74 20 64 6f  ypedef struct do
85830 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74  tlockLockingCont
85840 65 78 74 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 69  ext dotlockLocki
85850 6e 67 43 6f 6e 74 65 78 74 3b 0a 73 74 72 75 63  ngContext;.struc
85860 74 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67  t dotlockLocking
85870 43 6f 6e 74 65 78 74 20 7b 0a 20 20 63 68 61 72  Context {.  char
85880 20 2a 6c 6f 63 6b 50 61 74 68 3b 0a 7d 3b 0a 0a   *lockPath;.};..
85890 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c  .static int dotl
858a0 6f 63 6b 55 6e 69 78 43 68 65 63 6b 52 65 73 65  ockUnixCheckRese
858b0 72 76 65 64 4c 6f 63 6b 28 4f 73 46 69 6c 65 20  rvedLock(OsFile 
858c0 2a 69 64 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c  *id) {.  unixFil
858d0 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78  e *pFile = (unix
858e0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 64 6f 74 6c  File*)id;.  dotl
858f0 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  ockLockingContex
85900 74 20 2a 63 6f 6e 74 65 78 74 20 3d 20 0a 20 20  t *context = .  
85910 20 20 28 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 69 6e    (dotlockLockin
85920 67 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 69 6c  gContext *) pFil
85930 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
85940 74 3b 0a 20 20 0a 20 20 69 66 20 28 70 46 69 6c  t;.  .  if (pFil
85950 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 3d 20 52  e->locktype == R
85960 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 20 7b 0a  ESERVED_LOCK) {.
85970 20 20 20 20 72 65 74 75 72 6e 20 31 3b 20 2f 2a      return 1; /*
85980 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 61 20   already have a 
85990 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 2a 2f  reserved lock */
859a0 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20  .  } else {.    
859b0 73 74 72 75 63 74 20 73 74 61 74 20 73 74 61 74  struct stat stat
859c0 42 75 66 3b 0a 20 20 20 20 69 66 20 28 6c 73 74  Buf;.    if (lst
859d0 61 74 28 63 6f 6e 74 65 78 74 2d 3e 6c 6f 63 6b  at(context->lock
859e0 50 61 74 68 2c 26 73 74 61 74 42 75 66 29 20 3d  Path,&statBuf) =
859f0 3d 20 30 29 0a 20 20 20 20 20 20 2f 2a 20 66 69  = 0).      /* fi
85a00 6c 65 20 65 78 69 73 74 73 2c 20 73 6f 6d 65 6f  le exists, someo
85a10 6e 65 20 65 6c 73 65 20 68 61 73 20 74 68 65 20  ne else has the 
85a20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 72 65  lock */.      re
85a30 74 75 72 6e 20 31 3b 0a 20 20 20 20 65 6c 73 65  turn 1;.    else
85a40 0a 20 20 20 20 20 20 2f 2a 20 66 69 6c 65 20 64  .      /* file d
85a50 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 77  oes not exist, w
85a60 65 20 63 6f 75 6c 64 20 68 61 76 65 20 69 74 20  e could have it 
85a70 69 66 20 77 65 20 77 61 6e 74 20 69 74 20 2a 2f  if we want it */
85a80 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  .      return 0;
85a90 0a 20 20 7d 0a 7d 0a 0a 73 74 61 74 69 63 20 69  .  }.}..static i
85aa0 6e 74 20 64 6f 74 6c 6f 63 6b 55 6e 69 78 4c 6f  nt dotlockUnixLo
85ab0 63 6b 28 4f 73 46 69 6c 65 20 2a 69 64 2c 20 69  ck(OsFile *id, i
85ac0 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20  nt locktype) {. 
85ad0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
85ae0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
85af0 3b 0a 20 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 69  ;.  dotlockLocki
85b00 6e 67 43 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  ngContext *conte
85b10 78 74 20 3d 20 0a 20 20 20 20 28 64 6f 74 6c 6f  xt = .    (dotlo
85b20 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ckLockingContext
85b30 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69   *) pFile->locki
85b40 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 0a 20 20  ngContext;.  .  
85b50 2f 2a 20 69 66 20 77 65 20 61 6c 72 65 61 64 79  /* if we already
85b60 20 68 61 76 65 20 61 20 6c 6f 63 6b 2c 20 69 74   have a lock, it
85b70 20 69 73 20 65 78 63 6c 75 73 69 76 65 2e 20 20   is exclusive.  
85b80 0a 20 20 2a 2a 20 4a 75 73 74 20 61 64 6a 75 73  .  ** Just adjus
85b90 74 20 6c 65 76 65 6c 20 61 6e 64 20 70 75 6e 74  t level and punt
85ba0 20 6f 6e 20 6f 75 74 74 61 20 68 65 72 65 2e 20   on outta here. 
85bb0 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e  */.  if (pFile->
85bc0 6c 6f 63 6b 74 79 70 65 20 3e 20 4e 4f 5f 4c 4f  locktype > NO_LO
85bd0 43 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d  CK) {.    pFile-
85be0 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b  >locktype = lock
85bf0 74 79 70 65 3b 0a 20 20 20 20 0a 20 20 20 20 2f  type;.    .    /
85c00 2a 20 41 6c 77 61 79 73 20 75 70 64 61 74 65 20  * Always update 
85c10 74 68 65 20 74 69 6d 65 73 74 61 6d 70 20 6f 6e  the timestamp on
85c20 20 74 68 65 20 6f 6c 64 20 66 69 6c 65 20 2a 2f   the old file */
85c30 0a 20 20 20 20 75 74 69 6d 65 73 28 63 6f 6e 74  .    utimes(cont
85c40 65 78 74 2d 3e 6c 6f 63 6b 50 61 74 68 2c 4e 55  ext->lockPath,NU
85c50 4c 4c 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  LL);.    return 
85c60 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
85c70 20 0a 20 20 2f 2a 20 63 68 65 63 6b 20 74 6f 20   .  /* check to 
85c80 73 65 65 20 69 66 20 6c 6f 63 6b 20 66 69 6c 65  see if lock file
85c90 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20   already exists 
85ca0 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 74 61 74  */.  struct stat
85cb0 20 73 74 61 74 42 75 66 3b 0a 20 20 69 66 20 28   statBuf;.  if (
85cc0 6c 73 74 61 74 28 63 6f 6e 74 65 78 74 2d 3e 6c  lstat(context->l
85cd0 6f 63 6b 50 61 74 68 2c 26 73 74 61 74 42 75 66  ockPath,&statBuf
85ce0 29 20 3d 3d 20 30 29 7b 0a 20 20 20 20 72 65 74  ) == 0){.    ret
85cf0 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  urn SQLITE_BUSY;
85d00 20 2f 2a 20 69 74 20 64 6f 65 73 2c 20 62 75 73   /* it does, bus
85d10 79 20 2a 2f 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a  y */.  }.  .  /*
85d20 20 67 72 61 62 20 61 6e 20 65 78 63 6c 75 73 69   grab an exclusi
85d30 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74  ve lock */.  int
85d40 20 66 64 20 3d 20 6f 70 65 6e 28 63 6f 6e 74 65   fd = open(conte
85d50 78 74 2d 3e 6c 6f 63 6b 50 61 74 68 2c 4f 5f 52  xt->lockPath,O_R
85d60 44 4f 4e 4c 59 7c 4f 5f 43 52 45 41 54 7c 4f 5f  DONLY|O_CREAT|O_
85d70 45 58 43 4c 2c 30 36 30 30 29 3b 0a 20 20 69 66  EXCL,0600);.  if
85d80 20 28 66 64 20 3c 20 30 29 20 7b 0a 20 20 20 20   (fd < 0) {.    
85d90 2f 2a 20 66 61 69 6c 65 64 20 74 6f 20 6f 70 65  /* failed to ope
85da0 6e 2f 63 72 65 61 74 65 20 74 68 65 20 66 69 6c  n/create the fil
85db0 65 2c 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20  e, someone else 
85dc0 6d 61 79 20 68 61 76 65 20 73 74 6f 6c 65 6e 20  may have stolen 
85dd0 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20  the lock */.    
85de0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55  return SQLITE_BU
85df0 53 59 3b 20 0a 20 20 7d 0a 20 20 63 6c 6f 73 65  SY; .  }.  close
85e00 28 66 64 29 3b 0a 20 20 0a 20 20 2f 2a 20 67 6f  (fd);.  .  /* go
85e10 74 20 69 74 2c 20 73 65 74 20 74 68 65 20 74 79  t it, set the ty
85e20 70 65 20 61 6e 64 20 72 65 74 75 72 6e 20 6f 6b  pe and return ok
85e30 20 2a 2f 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63   */.  pFile->loc
85e40 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65  ktype = locktype
85e50 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
85e60 45 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  E_OK;.}..static 
85e70 69 6e 74 20 64 6f 74 6c 6f 63 6b 55 6e 69 78 55  int dotlockUnixU
85e80 6e 6c 6f 63 6b 28 4f 73 46 69 6c 65 20 2a 69 64  nlock(OsFile *id
85e90 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20  , int locktype) 
85ea0 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46  {.  unixFile *pF
85eb0 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a  ile = (unixFile*
85ec0 29 69 64 3b 0a 20 20 64 6f 74 6c 6f 63 6b 4c 6f  )id;.  dotlockLo
85ed0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 63 6f  ckingContext *co
85ee0 6e 74 65 78 74 20 3d 20 0a 20 20 20 20 28 64 6f  ntext = .    (do
85ef0 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74  tlockLockingCont
85f00 65 78 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f  ext *) pFile->lo
85f10 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20  ckingContext;.  
85f20 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74  .  assert( lockt
85f30 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype<=SHARED_LOCK
85f40 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f   );.  .  /* no-o
85f50 70 20 69 66 20 70 6f 73 73 69 62 6c 65 20 2a 2f  p if possible */
85f60 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f  .  if( pFile->lo
85f70 63 6b 74 79 70 65 3d 3d 6c 6f 63 6b 74 79 70 65  cktype==locktype
85f80 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
85f90 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
85fa0 0a 20 20 2f 2a 20 73 68 61 72 65 64 20 63 61 6e  .  /* shared can
85fb0 20 6a 75 73 74 20 62 65 20 73 65 74 20 62 65 63   just be set bec
85fc0 61 75 73 65 20 77 65 20 61 6c 77 61 79 73 20 68  ause we always h
85fd0 61 76 65 20 61 6e 20 65 78 63 6c 75 73 69 76 65  ave an exclusive
85fe0 20 2a 2f 0a 20 20 69 66 20 28 6c 6f 63 6b 74 79   */.  if (lockty
85ff0 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 29  pe==SHARED_LOCK)
86000 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f   {.    pFile->lo
86010 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70  cktype = locktyp
86020 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  e;.    return SQ
86030 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a  LITE_OK;.  }.  .
86040 20 20 2f 2a 20 6e 6f 2c 20 72 65 61 6c 6c 79 2c    /* no, really,
86050 20 75 6e 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 75 6e   unlock. */.  un
86060 6c 69 6e 6b 28 63 6f 6e 74 65 78 74 2d 3e 6c 6f  link(context->lo
86070 63 6b 50 61 74 68 29 3b 0a 20 20 70 46 69 6c 65  ckPath);.  pFile
86080 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f  ->locktype = NO_
86090 4c 4f 43 4b 3b 0a 20 20 72 65 74 75 72 6e 20 53  LOCK;.  return S
860a0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
860b0 20 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65   ** Close a file
860c0 2e 0a 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  .. */.static int
860d0 20 64 6f 74 6c 6f 63 6b 55 6e 69 78 43 6c 6f 73   dotlockUnixClos
860e0 65 28 4f 73 46 69 6c 65 20 2a 2a 70 49 64 29 20  e(OsFile **pId) 
860f0 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 69 64  {.  unixFile *id
86100 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 2a 70   = (unixFile*)*p
86110 49 64 3b 0a 20 20 0a 20 20 69 66 28 20 21 69 64  Id;.  .  if( !id
86120 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
86130 5f 4f 4b 3b 0a 20 20 64 6f 74 6c 6f 63 6b 55 6e  _OK;.  dotlockUn
86140 69 78 55 6e 6c 6f 63 6b 28 2a 70 49 64 2c 20 4e  ixUnlock(*pId, N
86150 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 2f 2a 20 66 72  O_LOCK);.  /* fr
86160 65 65 20 74 68 65 20 64 6f 74 6c 6f 63 6b 20 6c  ee the dotlock l
86170 6f 63 6b 69 6e 67 20 73 74 72 75 63 74 75 72 65  ocking structure
86180 20 2a 2f 0a 20 20 69 66 20 28 69 64 2d 3e 6c 6f   */.  if (id->lo
86190 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 21 3d 20  ckingContext != 
861a0 4e 55 4c 4c 29 20 7b 0a 20 20 20 20 69 66 20 28  NULL) {.    if (
861b0 28 28 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67  ((dotlockLocking
861c0 43 6f 6e 74 65 78 74 20 2a 29 69 64 2d 3e 6c 6f  Context *)id->lo
861d0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 2d 3e 6c  ckingContext)->l
861e0 6f 63 6b 50 61 74 68 20 21 3d 20 4e 55 4c 4c 29  ockPath != NULL)
861f0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 54 68  .      sqlite3Th
86200 72 65 61 64 53 61 66 65 46 72 65 65 28 20 28 20  readSafeFree( ( 
86210 28 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 43  (dotlockLockingC
86220 6f 6e 74 65 78 74 20 2a 29 0a 20 20 20 20 20 20  ontext *).      
86230 20 20 69 64 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e    id->lockingCon
86240 74 65 78 74 29 2d 3e 6c 6f 63 6b 50 61 74 68 29  text)->lockPath)
86250 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 54 68 72  ;.    sqlite3Thr
86260 65 61 64 53 61 66 65 46 72 65 65 28 69 64 2d 3e  eadSafeFree(id->
86270 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3b  lockingContext);
86280 0a 20 20 7d 0a 20 20 0a 20 20 69 66 28 20 69 64  .  }.  .  if( id
86290 2d 3e 64 69 72 66 64 3e 3d 30 20 29 20 63 6c 6f  ->dirfd>=0 ) clo
862a0 73 65 28 69 64 2d 3e 64 69 72 66 64 29 3b 0a 20  se(id->dirfd);. 
862b0 20 69 64 2d 3e 64 69 72 66 64 20 3d 20 2d 31 3b   id->dirfd = -1;
862c0 0a 20 20 73 71 6c 69 74 65 33 4f 73 45 6e 74 65  .  sqlite3OsEnte
862d0 72 4d 75 74 65 78 28 29 3b 0a 20 20 0a 20 20 63  rMutex();.  .  c
862e0 6c 6f 73 65 28 69 64 2d 3e 68 29 3b 0a 20 20 0a  lose(id->h);.  .
862f0 20 20 73 71 6c 69 74 65 33 4f 73 4c 65 61 76 65    sqlite3OsLeave
86300 4d 75 74 65 78 28 29 3b 0a 20 20 69 64 2d 3e 69  Mutex();.  id->i
86310 73 4f 70 65 6e 20 3d 20 30 3b 0a 20 20 4f 53 54  sOpen = 0;.  OST
86320 52 41 43 45 32 28 22 43 4c 4f 53 45 20 20 20 25  RACE2("CLOSE   %
86330 2d 33 64 5c 6e 22 2c 20 69 64 2d 3e 68 29 3b 0a  -3d\n", id->h);.
86340 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2d 31    OpenCounter(-1
86350 29 3b 0a 20 20 73 71 6c 69 74 65 33 54 68 72 65  );.  sqlite3Thre
86360 61 64 53 61 66 65 46 72 65 65 28 69 64 29 3b 0a  adSafeFree(id);.
86370 20 20 2a 70 49 64 20 3d 20 30 3b 0a 20 20 72 65    *pId = 0;.  re
86380 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
86390 7d 0a 0a 0a 23 70 72 61 67 6d 61 20 6d 61 72 6b  }...#pragma mark
863a0 20 4e 6f 20 6c 6f 63 6b 69 6e 67 0a 0a 2f 2a 0a   No locking../*.
863b0 20 2a 2a 20 54 68 65 20 6e 6f 6c 6f 63 6b 4c 6f   ** The nolockLo
863c0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 69 73 20  ckingContext is 
863d0 76 6f 69 64 0a 20 2a 2f 0a 74 79 70 65 64 65 66  void. */.typedef
863e0 20 76 6f 69 64 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b   void nolockLock
863f0 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 0a 73 74 61  ingContext;..sta
86400 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 55 6e  tic int nolockUn
86410 69 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  ixCheckReservedL
86420 6f 63 6b 28 4f 73 46 69 6c 65 20 2a 69 64 29 20  ock(OsFile *id) 
86430 7b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  {.  return 0;.}.
86440 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c 6f  .static int nolo
86450 63 6b 55 6e 69 78 4c 6f 63 6b 28 4f 73 46 69 6c  ckUnixLock(OsFil
86460 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74  e *id, int lockt
86470 79 70 65 29 20 7b 0a 20 20 72 65 74 75 72 6e 20  ype) {.  return 
86480 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 73 74  SQLITE_OK;.}..st
86490 61 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 55  atic int nolockU
864a0 6e 69 78 55 6e 6c 6f 63 6b 28 4f 73 46 69 6c 65  nixUnlock(OsFile
864b0 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79   *id, int lockty
864c0 70 65 29 20 7b 0a 20 20 72 65 74 75 72 6e 20 53  pe) {.  return S
864d0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
864e0 20 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65   ** Close a file
864f0 2e 0a 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  .. */.static int
86500 20 6e 6f 6c 6f 63 6b 55 6e 69 78 43 6c 6f 73 65   nolockUnixClose
86510 28 4f 73 46 69 6c 65 20 2a 2a 70 49 64 29 20 7b  (OsFile **pId) {
86520 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 69 64 20  .  unixFile *id 
86530 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 2a 70 49  = (unixFile*)*pI
86540 64 3b 0a 20 20 0a 20 20 69 66 28 20 21 69 64 20  d;.  .  if( !id 
86550 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
86560 4f 4b 3b 0a 20 20 69 66 28 20 69 64 2d 3e 64 69  OK;.  if( id->di
86570 72 66 64 3e 3d 30 20 29 20 63 6c 6f 73 65 28 69  rfd>=0 ) close(i
86580 64 2d 3e 64 69 72 66 64 29 3b 0a 20 20 69 64 2d  d->dirfd);.  id-
86590 3e 64 69 72 66 64 20 3d 20 2d 31 3b 0a 20 20 73  >dirfd = -1;.  s
865a0 71 6c 69 74 65 33 4f 73 45 6e 74 65 72 4d 75 74  qlite3OsEnterMut
865b0 65 78 28 29 3b 0a 20 20 0a 20 20 63 6c 6f 73 65  ex();.  .  close
865c0 28 69 64 2d 3e 68 29 3b 0a 20 20 0a 20 20 73 71  (id->h);.  .  sq
865d0 6c 69 74 65 33 4f 73 4c 65 61 76 65 4d 75 74 65  lite3OsLeaveMute
865e0 78 28 29 3b 0a 20 20 69 64 2d 3e 69 73 4f 70 65  x();.  id->isOpe
865f0 6e 20 3d 20 30 3b 0a 20 20 4f 53 54 52 41 43 45  n = 0;.  OSTRACE
86600 32 28 22 43 4c 4f 53 45 20 20 20 25 2d 33 64 5c  2("CLOSE   %-3d\
86610 6e 22 2c 20 69 64 2d 3e 68 29 3b 0a 20 20 4f 70  n", id->h);.  Op
86620 65 6e 43 6f 75 6e 74 65 72 28 2d 31 29 3b 0a 20  enCounter(-1);. 
86630 20 73 71 6c 69 74 65 33 54 68 72 65 61 64 53 61   sqlite3ThreadSa
86640 66 65 46 72 65 65 28 69 64 29 3b 0a 20 20 2a 70  feFree(id);.  *p
86650 49 64 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e  Id = 0;.  return
86660 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23   SQLITE_OK;.}..#
86670 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
86680 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
86690 54 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  TYLE */../*.** T
866a0 75 72 6e 20 61 20 72 65 6c 61 74 69 76 65 20 70  urn a relative p
866b0 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20 66  athname into a f
866c0 75 6c 6c 20 70 61 74 68 6e 61 6d 65 2e 20 20 52  ull pathname.  R
866d0 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a  eturn a pointer.
866e0 2a 2a 20 74 6f 20 74 68 65 20 66 75 6c 6c 20 70  ** to the full p
866f0 61 74 68 6e 61 6d 65 20 73 74 6f 72 65 64 20 69  athname stored i
86700 6e 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64  n space obtained
86710 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c   from sqliteMall
86720 6f 63 28 29 2e 0a 2a 2a 20 54 68 65 20 63 61 6c  oc()..** The cal
86730 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 69 73  ling function is
86740 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72   responsible for
86750 20 66 72 65 65 69 6e 67 20 74 68 69 73 20 73 70   freeing this sp
86760 61 63 65 20 6f 6e 63 65 20 69 74 0a 2a 2a 20 69  ace once it.** i
86770 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 6e 65 65 64  s no longer need
86780 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
86790 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69  IVATE char *sqli
867a0 74 65 33 55 6e 69 78 46 75 6c 6c 50 61 74 68 6e  te3UnixFullPathn
867b0 61 6d 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ame(const char *
867c0 7a 52 65 6c 61 74 69 76 65 29 7b 0a 20 20 63 68  zRelative){.  ch
867d0 61 72 20 2a 7a 46 75 6c 6c 20 3d 20 30 3b 0a 20  ar *zFull = 0;. 
867e0 20 69 66 28 20 7a 52 65 6c 61 74 69 76 65 5b 30   if( zRelative[0
867f0 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 73 71  ]=='/' ){.    sq
86800 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26  lite3SetString(&
86810 7a 46 75 6c 6c 2c 20 7a 52 65 6c 61 74 69 76 65  zFull, zRelative
86820 2c 20 28 63 68 61 72 2a 29 30 29 3b 0a 20 20 7d  , (char*)0);.  }
86830 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a  else{.    char *
86840 7a 42 75 66 20 3d 20 73 71 6c 69 74 65 4d 61 6c  zBuf = sqliteMal
86850 6c 6f 63 28 35 30 30 30 29 3b 0a 20 20 20 20 69  loc(5000);.    i
86860 66 28 20 7a 42 75 66 3d 3d 30 20 29 7b 0a 20 20  f( zBuf==0 ){.  
86870 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
86880 20 20 7d 0a 20 20 20 20 7a 42 75 66 5b 30 5d 20    }.    zBuf[0] 
86890 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  = 0;.    sqlite3
868a0 53 65 74 53 74 72 69 6e 67 28 26 7a 46 75 6c 6c  SetString(&zFull
868b0 2c 20 67 65 74 63 77 64 28 7a 42 75 66 2c 20 35  , getcwd(zBuf, 5
868c0 30 30 30 29 2c 20 22 2f 22 2c 20 7a 52 65 6c 61  000), "/", zRela
868d0 74 69 76 65 2c 0a 20 20 20 20 20 20 20 20 20 20  tive,.          
868e0 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a            (char*
868f0 29 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 46  )0);.    sqliteF
86900 72 65 65 28 7a 42 75 66 29 3b 0a 20 20 7d 0a 0a  ree(zBuf);.  }..
86910 23 69 66 20 30 0a 20 20 2f 2a 0a 20 20 2a 2a 20  #if 0.  /*.  ** 
86920 52 65 6d 6f 76 65 20 22 2f 2e 2f 22 20 70 61 74  Remove "/./" pat
86930 68 20 65 6c 65 6d 65 6e 74 73 20 61 6e 64 20 63  h elements and c
86940 6f 6e 76 65 72 74 20 22 2f 41 2f 2e 2f 22 20 70  onvert "/A/./" p
86950 61 74 68 20 65 6c 65 6d 65 6e 74 73 0a 20 20 2a  ath elements.  *
86960 2a 20 74 6f 20 6a 75 73 74 20 22 2f 22 2e 0a 20  * to just "/".. 
86970 20 2a 2f 0a 20 20 69 66 28 20 7a 46 75 6c 6c 20   */.  if( zFull 
86980 29 7b 0a 20 20 20 20 69 6e 74 20 69 2c 20 6a 3b  ){.    int i, j;
86990 0a 20 20 20 20 66 6f 72 28 69 3d 6a 3d 30 3b 20  .    for(i=j=0; 
869a0 7a 46 75 6c 6c 5b 69 5d 3b 20 69 2b 2b 29 7b 0a  zFull[i]; i++){.
869b0 20 20 20 20 20 20 69 66 28 20 7a 46 75 6c 6c 5b        if( zFull[
869c0 69 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20  i]=='/' ){.     
869d0 20 20 20 69 66 28 20 7a 46 75 6c 6c 5b 69 2b 31     if( zFull[i+1
869e0 5d 3d 3d 27 2f 27 20 29 20 63 6f 6e 74 69 6e 75  ]=='/' ) continu
869f0 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a  e;.        if( z
86a00 46 75 6c 6c 5b 69 2b 31 5d 3d 3d 27 2e 27 20 26  Full[i+1]=='.' &
86a10 26 20 7a 46 75 6c 6c 5b 69 2b 32 5d 3d 3d 27 2f  & zFull[i+2]=='/
86a20 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  ' ){.          i
86a30 20 2b 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20   += 1;.         
86a40 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
86a50 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
86a60 20 7a 46 75 6c 6c 5b 69 2b 31 5d 3d 3d 27 2e 27   zFull[i+1]=='.'
86a70 20 26 26 20 7a 46 75 6c 6c 5b 69 2b 32 5d 3d 3d   && zFull[i+2]==
86a80 27 2e 27 20 26 26 20 7a 46 75 6c 6c 5b 69 2b 33  '.' && zFull[i+3
86a90 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20  ]=='/' ){.      
86aa0 20 20 20 20 77 68 69 6c 65 28 20 6a 3e 30 20 26      while( j>0 &
86ab0 26 20 7a 46 75 6c 6c 5b 6a 2d 31 5d 21 3d 27 2f  & zFull[j-1]!='/
86ac0 27 20 29 7b 20 6a 2d 2d 3b 20 7d 0a 20 20 20 20  ' ){ j--; }.    
86ad0 20 20 20 20 20 20 69 20 2b 3d 20 33 3b 0a 20 20        i += 3;.  
86ae0 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65          continue
86af0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
86b00 20 20 7d 0a 20 20 20 20 20 20 7a 46 75 6c 6c 5b    }.      zFull[
86b10 6a 2b 2b 5d 20 3d 20 7a 46 75 6c 6c 5b 69 5d 3b  j++] = zFull[i];
86b20 0a 20 20 20 20 7d 0a 20 20 20 20 7a 46 75 6c 6c  .    }.    zFull
86b30 5b 6a 5d 20 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e  [j] = 0;.  }.#en
86b40 64 69 66 0a 0a 20 20 72 65 74 75 72 6e 20 7a 46  dif..  return zF
86b50 75 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68  ull;.}../*.** Ch
86b60 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f  ange the value o
86b70 66 20 74 68 65 20 66 75 6c 6c 73 79 6e 63 20 66  f the fullsync f
86b80 6c 61 67 20 69 6e 20 74 68 65 20 67 69 76 65 6e  lag in the given
86b90 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
86ba0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
86bb0 20 75 6e 69 78 53 65 74 46 75 6c 6c 53 79 6e 63   unixSetFullSync
86bc0 28 4f 73 46 69 6c 65 20 2a 69 64 2c 20 69 6e 74  (OsFile *id, int
86bd0 20 76 29 7b 0a 20 20 28 28 75 6e 69 78 46 69 6c   v){.  ((unixFil
86be0 65 2a 29 69 64 29 2d 3e 66 75 6c 6c 53 79 6e 63  e*)id)->fullSync
86bf0 20 3d 20 76 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52   = v;.}../*.** R
86c00 65 74 75 72 6e 20 74 68 65 20 75 6e 64 65 72 6c  eturn the underl
86c10 79 69 6e 67 20 66 69 6c 65 20 68 61 6e 64 6c 65  ying file handle
86c20 20 66 6f 72 20 61 6e 20 4f 73 46 69 6c 65 0a 2a   for an OsFile.*
86c30 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
86c40 78 46 69 6c 65 48 61 6e 64 6c 65 28 4f 73 46 69  xFileHandle(OsFi
86c50 6c 65 20 2a 69 64 29 7b 0a 20 20 72 65 74 75 72  le *id){.  retur
86c60 6e 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64  n ((unixFile*)id
86c70 29 2d 3e 68 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  )->h;.}../*.** R
86c80 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72  eturn an integer
86c90 20 74 68 61 74 20 69 6e 64 69 63 65 73 20 74 68   that indices th
86ca0 65 20 74 79 70 65 20 6f 66 20 6c 6f 63 6b 20 63  e type of lock c
86cb0 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 0a 2a 2a  urrently held.**
86cc0 20 62 79 20 74 68 69 73 20 68 61 6e 64 6c 65 2e   by this handle.
86cd0 20 20 28 55 73 65 64 20 66 6f 72 20 74 65 73 74    (Used for test
86ce0 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73 69 73  ing and analysis
86cf0 20 6f 6e 6c 79 2e 29 0a 2a 2f 0a 73 74 61 74 69   only.).*/.stati
86d00 63 20 69 6e 74 20 75 6e 69 78 4c 6f 63 6b 53 74  c int unixLockSt
86d10 61 74 65 28 4f 73 46 69 6c 65 20 2a 69 64 29 7b  ate(OsFile *id){
86d20 0a 20 20 72 65 74 75 72 6e 20 28 28 75 6e 69 78  .  return ((unix
86d30 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74  File*)id)->lockt
86d40 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  ype;.}../*.** Re
86d50 74 75 72 6e 20 74 68 65 20 73 65 63 74 6f 72 20  turn the sector 
86d60 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66  size in bytes of
86d70 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20   the underlying 
86d80 62 6c 6f 63 6b 20 64 65 76 69 63 65 20 66 6f 72  block device for
86d90 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 66 69 65  .** the specifie
86da0 64 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20  d file. This is 
86db0 61 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 35 31  almost always 51
86dc0 32 20 62 79 74 65 73 2c 20 62 75 74 20 6d 61 79  2 bytes, but may
86dd0 20 62 65 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f   be.** larger fo
86de0 72 20 73 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a  r some devices..
86df0 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64  **.** SQLite cod
86e00 65 20 61 73 73 75 6d 65 73 20 74 68 69 73 20 66  e assumes this f
86e10 75 6e 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66  unction cannot f
86e20 61 69 6c 2e 20 49 74 20 61 6c 73 6f 20 61 73 73  ail. It also ass
86e30 75 6d 65 73 20 74 68 61 74 0a 2a 2a 20 69 66 20  umes that.** if 
86e40 74 77 6f 20 66 69 6c 65 73 20 61 72 65 20 63 72  two files are cr
86e50 65 61 74 65 64 20 69 6e 20 74 68 65 20 73 61 6d  eated in the sam
86e60 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 64 69  e file-system di
86e70 72 65 63 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a  rectory (i.e..**
86e80 20 61 20 64 61 74 61 62 61 73 65 20 61 6e 64 20   a database and 
86e90 69 74 27 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  it's journal fil
86ea0 65 29 20 74 68 61 74 20 74 68 65 20 73 65 63 74  e) that the sect
86eb0 6f 72 20 73 69 7a 65 20 77 69 6c 6c 20 62 65 20  or size will be 
86ec0 74 68 65 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 20  the.** same for 
86ed0 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  both..*/.static 
86ee0 69 6e 74 20 75 6e 69 78 53 65 63 74 6f 72 53 69  int unixSectorSi
86ef0 7a 65 28 4f 73 46 69 6c 65 20 2a 69 64 29 7b 0a  ze(OsFile *id){.
86f00 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
86f10 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53  DEFAULT_SECTOR_S
86f20 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  IZE;.}../*.** Th
86f30 69 73 20 76 65 63 74 6f 72 20 64 65 66 69 6e 65  is vector define
86f40 73 20 61 6c 6c 20 74 68 65 20 6d 65 74 68 6f 64  s all the method
86f50 73 20 74 68 61 74 20 63 61 6e 20 6f 70 65 72 61  s that can opera
86f60 74 65 20 6f 6e 20 61 6e 20 4f 73 46 69 6c 65 0a  te on an OsFile.
86f70 2a 2a 20 66 6f 72 20 75 6e 69 78 2e 0a 2a 2f 0a  ** for unix..*/.
86f80 73 74 61 74 69 63 20 63 6f 6e 73 74 20 49 6f 4d  static const IoM
86f90 65 74 68 6f 64 20 73 71 6c 69 74 65 33 55 6e 69  ethod sqlite3Uni
86fa0 78 49 6f 4d 65 74 68 6f 64 20 3d 20 7b 0a 20 20  xIoMethod = {.  
86fb0 75 6e 69 78 43 6c 6f 73 65 2c 0a 20 20 75 6e 69  unixClose,.  uni
86fc0 78 4f 70 65 6e 44 69 72 65 63 74 6f 72 79 2c 0a  xOpenDirectory,.
86fd0 20 20 75 6e 69 78 52 65 61 64 2c 0a 20 20 75 6e    unixRead,.  un
86fe0 69 78 57 72 69 74 65 2c 0a 20 20 75 6e 69 78 53  ixWrite,.  unixS
86ff0 65 65 6b 2c 0a 20 20 75 6e 69 78 54 72 75 6e 63  eek,.  unixTrunc
87000 61 74 65 2c 0a 20 20 75 6e 69 78 53 79 6e 63 2c  ate,.  unixSync,
87010 0a 20 20 75 6e 69 78 53 65 74 46 75 6c 6c 53 79  .  unixSetFullSy
87020 6e 63 2c 0a 20 20 75 6e 69 78 46 69 6c 65 48 61  nc,.  unixFileHa
87030 6e 64 6c 65 2c 0a 20 20 75 6e 69 78 46 69 6c 65  ndle,.  unixFile
87040 53 69 7a 65 2c 0a 20 20 75 6e 69 78 4c 6f 63 6b  Size,.  unixLock
87050 2c 0a 20 20 75 6e 69 78 55 6e 6c 6f 63 6b 2c 0a  ,.  unixUnlock,.
87060 20 20 75 6e 69 78 4c 6f 63 6b 53 74 61 74 65 2c    unixLockState,
87070 0a 20 20 75 6e 69 78 43 68 65 63 6b 52 65 73 65  .  unixCheckRese
87080 72 76 65 64 4c 6f 63 6b 2c 0a 20 20 75 6e 69 78  rvedLock,.  unix
87090 53 65 63 74 6f 72 53 69 7a 65 2c 0a 7d 3b 0a 0a  SectorSize,.};..
870a0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
870b0 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
870c0 4c 45 0a 2f 2a 0a 20 2a 2a 20 54 68 69 73 20 76  LE./*. ** This v
870d0 65 63 74 6f 72 20 64 65 66 69 6e 65 73 20 61 6c  ector defines al
870e0 6c 20 74 68 65 20 6d 65 74 68 6f 64 73 20 74 68  l the methods th
870f0 61 74 20 63 61 6e 20 6f 70 65 72 61 74 65 20 6f  at can operate o
87100 6e 20 61 6e 20 4f 73 46 69 6c 65 0a 20 2a 2a 20  n an OsFile. ** 
87110 66 6f 72 20 75 6e 69 78 20 77 69 74 68 20 41 46  for unix with AF
87120 50 20 73 74 79 6c 65 20 66 69 6c 65 20 6c 6f 63  P style file loc
87130 6b 69 6e 67 2e 0a 20 2a 2f 0a 73 74 61 74 69 63  king.. */.static
87140 20 63 6f 6e 73 74 20 49 6f 4d 65 74 68 6f 64 20   const IoMethod 
87150 73 71 6c 69 74 65 33 41 46 50 4c 6f 63 6b 69 6e  sqlite3AFPLockin
87160 67 55 6e 69 78 49 6f 4d 65 74 68 6f 64 20 3d 20  gUnixIoMethod = 
87170 7b 0a 20 20 20 20 61 66 70 55 6e 69 78 43 6c 6f  {.    afpUnixClo
87180 73 65 2c 0a 20 20 20 20 75 6e 69 78 4f 70 65 6e  se,.    unixOpen
87190 44 69 72 65 63 74 6f 72 79 2c 0a 20 20 20 20 75  Directory,.    u
871a0 6e 69 78 52 65 61 64 2c 0a 20 20 20 20 75 6e 69  nixRead,.    uni
871b0 78 57 72 69 74 65 2c 0a 20 20 20 20 75 6e 69 78  xWrite,.    unix
871c0 53 65 65 6b 2c 0a 20 20 20 20 75 6e 69 78 54 72  Seek,.    unixTr
871d0 75 6e 63 61 74 65 2c 0a 20 20 20 20 75 6e 69 78  uncate,.    unix
871e0 53 79 6e 63 2c 0a 20 20 20 20 75 6e 69 78 53 65  Sync,.    unixSe
871f0 74 46 75 6c 6c 53 79 6e 63 2c 0a 20 20 20 20 75  tFullSync,.    u
87200 6e 69 78 46 69 6c 65 48 61 6e 64 6c 65 2c 0a 20  nixFileHandle,. 
87210 20 20 20 75 6e 69 78 46 69 6c 65 53 69 7a 65 2c     unixFileSize,
87220 0a 20 20 20 20 61 66 70 55 6e 69 78 4c 6f 63 6b  .    afpUnixLock
87230 2c 0a 20 20 20 20 61 66 70 55 6e 69 78 55 6e 6c  ,.    afpUnixUnl
87240 6f 63 6b 2c 0a 20 20 20 20 75 6e 69 78 4c 6f 63  ock,.    unixLoc
87250 6b 53 74 61 74 65 2c 0a 20 20 20 20 61 66 70 55  kState,.    afpU
87260 6e 69 78 43 68 65 63 6b 52 65 73 65 72 76 65 64  nixCheckReserved
87270 4c 6f 63 6b 2c 0a 20 20 20 20 75 6e 69 78 53 65  Lock,.    unixSe
87280 63 74 6f 72 53 69 7a 65 2c 0a 7d 3b 0a 0a 2f 2a  ctorSize,.};../*
87290 0a 20 2a 2a 20 54 68 69 73 20 76 65 63 74 6f 72  . ** This vector
872a0 20 64 65 66 69 6e 65 73 20 61 6c 6c 20 74 68 65   defines all the
872b0 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20 63 61   methods that ca
872c0 6e 20 6f 70 65 72 61 74 65 20 6f 6e 20 61 6e 20  n operate on an 
872d0 4f 73 46 69 6c 65 0a 20 2a 2a 20 66 6f 72 20 75  OsFile. ** for u
872e0 6e 69 78 20 77 69 74 68 20 66 6c 6f 63 6b 28 29  nix with flock()
872f0 20 73 74 79 6c 65 20 66 69 6c 65 20 6c 6f 63 6b   style file lock
87300 69 6e 67 2e 0a 20 2a 2f 0a 73 74 61 74 69 63 20  ing.. */.static 
87310 63 6f 6e 73 74 20 49 6f 4d 65 74 68 6f 64 20 73  const IoMethod s
87320 71 6c 69 74 65 33 46 6c 6f 63 6b 4c 6f 63 6b 69  qlite3FlockLocki
87330 6e 67 55 6e 69 78 49 6f 4d 65 74 68 6f 64 20 3d  ngUnixIoMethod =
87340 20 7b 0a 20 20 20 20 66 6c 6f 63 6b 55 6e 69 78   {.    flockUnix
87350 43 6c 6f 73 65 2c 0a 20 20 20 20 75 6e 69 78 4f  Close,.    unixO
87360 70 65 6e 44 69 72 65 63 74 6f 72 79 2c 0a 20 20  penDirectory,.  
87370 20 20 75 6e 69 78 52 65 61 64 2c 0a 20 20 20 20    unixRead,.    
87380 75 6e 69 78 57 72 69 74 65 2c 0a 20 20 20 20 75  unixWrite,.    u
87390 6e 69 78 53 65 65 6b 2c 0a 20 20 20 20 75 6e 69  nixSeek,.    uni
873a0 78 54 72 75 6e 63 61 74 65 2c 0a 20 20 20 20 75  xTruncate,.    u
873b0 6e 69 78 53 79 6e 63 2c 0a 20 20 20 20 75 6e 69  nixSync,.    uni
873c0 78 53 65 74 46 75 6c 6c 53 79 6e 63 2c 0a 20 20  xSetFullSync,.  
873d0 20 20 75 6e 69 78 46 69 6c 65 48 61 6e 64 6c 65    unixFileHandle
873e0 2c 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 53 69  ,.    unixFileSi
873f0 7a 65 2c 0a 20 20 20 20 66 6c 6f 63 6b 55 6e 69  ze,.    flockUni
87400 78 4c 6f 63 6b 2c 0a 20 20 20 20 66 6c 6f 63 6b  xLock,.    flock
87410 55 6e 69 78 55 6e 6c 6f 63 6b 2c 0a 20 20 20 20  UnixUnlock,.    
87420 75 6e 69 78 4c 6f 63 6b 53 74 61 74 65 2c 0a 20  unixLockState,. 
87430 20 20 20 66 6c 6f 63 6b 55 6e 69 78 43 68 65 63     flockUnixChec
87440 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 2c 0a 20  kReservedLock,. 
87450 20 20 20 75 6e 69 78 53 65 63 74 6f 72 53 69 7a     unixSectorSiz
87460 65 2c 0a 7d 3b 0a 0a 2f 2a 0a 20 2a 2a 20 54 68  e,.};../*. ** Th
87470 69 73 20 76 65 63 74 6f 72 20 64 65 66 69 6e 65  is vector define
87480 73 20 61 6c 6c 20 74 68 65 20 6d 65 74 68 6f 64  s all the method
87490 73 20 74 68 61 74 20 63 61 6e 20 6f 70 65 72 61  s that can opera
874a0 74 65 20 6f 6e 20 61 6e 20 4f 73 46 69 6c 65 0a  te on an OsFile.
874b0 20 2a 2a 20 66 6f 72 20 75 6e 69 78 20 77 69 74   ** for unix wit
874c0 68 20 64 6f 74 6c 6f 63 6b 20 73 74 79 6c 65 20  h dotlock style 
874d0 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 2e 0a 20 2a  file locking.. *
874e0 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 49  /.static const I
874f0 6f 4d 65 74 68 6f 64 20 73 71 6c 69 74 65 33 44  oMethod sqlite3D
87500 6f 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 55 6e 69  otlockLockingUni
87510 78 49 6f 4d 65 74 68 6f 64 20 3d 20 7b 0a 20 20  xIoMethod = {.  
87520 20 20 64 6f 74 6c 6f 63 6b 55 6e 69 78 43 6c 6f    dotlockUnixClo
87530 73 65 2c 0a 20 20 20 20 75 6e 69 78 4f 70 65 6e  se,.    unixOpen
87540 44 69 72 65 63 74 6f 72 79 2c 0a 20 20 20 20 75  Directory,.    u
87550 6e 69 78 52 65 61 64 2c 0a 20 20 20 20 75 6e 69  nixRead,.    uni
87560 78 57 72 69 74 65 2c 0a 20 20 20 20 75 6e 69 78  xWrite,.    unix
87570 53 65 65 6b 2c 0a 20 20 20 20 75 6e 69 78 54 72  Seek,.    unixTr
87580 75 6e 63 61 74 65 2c 0a 20 20 20 20 75 6e 69 78  uncate,.    unix
87590 53 79 6e 63 2c 0a 20 20 20 20 75 6e 69 78 53 65  Sync,.    unixSe
875a0 74 46 75 6c 6c 53 79 6e 63 2c 0a 20 20 20 20 75  tFullSync,.    u
875b0 6e 69 78 46 69 6c 65 48 61 6e 64 6c 65 2c 0a 20  nixFileHandle,. 
875c0 20 20 20 75 6e 69 78 46 69 6c 65 53 69 7a 65 2c     unixFileSize,
875d0 0a 20 20 20 20 64 6f 74 6c 6f 63 6b 55 6e 69 78  .    dotlockUnix
875e0 4c 6f 63 6b 2c 0a 20 20 20 20 64 6f 74 6c 6f 63  Lock,.    dotloc
875f0 6b 55 6e 69 78 55 6e 6c 6f 63 6b 2c 0a 20 20 20  kUnixUnlock,.   
87600 20 75 6e 69 78 4c 6f 63 6b 53 74 61 74 65 2c 0a   unixLockState,.
87610 20 20 20 20 64 6f 74 6c 6f 63 6b 55 6e 69 78 43      dotlockUnixC
87620 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
87630 2c 0a 20 20 20 20 75 6e 69 78 53 65 63 74 6f 72  ,.    unixSector
87640 53 69 7a 65 2c 0a 7d 3b 0a 0a 2f 2a 0a 20 2a 2a  Size,.};../*. **
87650 20 54 68 69 73 20 76 65 63 74 6f 72 20 64 65 66   This vector def
87660 69 6e 65 73 20 61 6c 6c 20 74 68 65 20 6d 65 74  ines all the met
87670 68 6f 64 73 20 74 68 61 74 20 63 61 6e 20 6f 70  hods that can op
87680 65 72 61 74 65 20 6f 6e 20 61 6e 20 4f 73 46 69  erate on an OsFi
87690 6c 65 0a 20 2a 2a 20 66 6f 72 20 75 6e 69 78 20  le. ** for unix 
876a0 77 69 74 68 20 64 6f 74 6c 6f 63 6b 20 73 74 79  with dotlock sty
876b0 6c 65 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 2e  le file locking.
876c0 0a 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  . */.static cons
876d0 74 20 49 6f 4d 65 74 68 6f 64 20 73 71 6c 69 74  t IoMethod sqlit
876e0 65 33 4e 6f 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 55  e3NolockLockingU
876f0 6e 69 78 49 6f 4d 65 74 68 6f 64 20 3d 20 7b 0a  nixIoMethod = {.
87700 20 20 6e 6f 6c 6f 63 6b 55 6e 69 78 43 6c 6f 73    nolockUnixClos
87710 65 2c 0a 20 20 75 6e 69 78 4f 70 65 6e 44 69 72  e,.  unixOpenDir
87720 65 63 74 6f 72 79 2c 0a 20 20 75 6e 69 78 52 65  ectory,.  unixRe
87730 61 64 2c 0a 20 20 75 6e 69 78 57 72 69 74 65 2c  ad,.  unixWrite,
87740 0a 20 20 75 6e 69 78 53 65 65 6b 2c 0a 20 20 75  .  unixSeek,.  u
87750 6e 69 78 54 72 75 6e 63 61 74 65 2c 0a 20 20 75  nixTruncate,.  u
87760 6e 69 78 53 79 6e 63 2c 0a 20 20 75 6e 69 78 53  nixSync,.  unixS
87770 65 74 46 75 6c 6c 53 79 6e 63 2c 0a 20 20 75 6e  etFullSync,.  un
87780 69 78 46 69 6c 65 48 61 6e 64 6c 65 2c 0a 20 20  ixFileHandle,.  
87790 75 6e 69 78 46 69 6c 65 53 69 7a 65 2c 0a 20 20  unixFileSize,.  
877a0 6e 6f 6c 6f 63 6b 55 6e 69 78 4c 6f 63 6b 2c 0a  nolockUnixLock,.
877b0 20 20 6e 6f 6c 6f 63 6b 55 6e 69 78 55 6e 6c 6f    nolockUnixUnlo
877c0 63 6b 2c 0a 20 20 75 6e 69 78 4c 6f 63 6b 53 74  ck,.  unixLockSt
877d0 61 74 65 2c 0a 20 20 6e 6f 6c 6f 63 6b 55 6e 69  ate,.  nolockUni
877e0 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  xCheckReservedLo
877f0 63 6b 2c 0a 20 20 75 6e 69 78 53 65 63 74 6f 72  ck,.  unixSector
87800 53 69 7a 65 2c 0a 7d 3b 0a 0a 23 65 6e 64 69 66  Size,.};..#endif
87810 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   /* SQLITE_ENABL
87820 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
87830 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61  */../*.** Alloca
87840 74 65 20 6d 65 6d 6f 72 79 20 66 6f 72 20 61 20  te memory for a 
87850 6e 65 77 20 75 6e 69 78 46 69 6c 65 20 61 6e 64  new unixFile and
87860 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 61 74   initialize that
87870 20 75 6e 69 78 46 69 6c 65 2e 0a 2a 2a 20 57 72   unixFile..** Wr
87880 69 74 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  ite a pointer to
87890 20 74 68 65 20 6e 65 77 20 75 6e 69 78 46 69 6c   the new unixFil
878a0 65 20 69 6e 74 6f 20 2a 70 49 64 2e 0a 2a 2a 20  e into *pId..** 
878b0 49 66 20 77 65 20 72 75 6e 20 6f 75 74 20 6f 66  If we run out of
878c0 20 6d 65 6d 6f 72 79 2c 20 63 6c 6f 73 65 20 74   memory, close t
878d0 68 65 20 66 69 6c 65 20 61 6e 64 20 72 65 74 75  he file and retu
878e0 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a  rn an error..*/.
878f0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
87900 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
87910 4c 45 0a 2f 2a 20 0a 20 2a 2a 20 57 68 65 6e 20  LE./* . ** When 
87920 6c 6f 63 6b 69 6e 67 20 65 78 74 65 6e 73 69 6f  locking extensio
87930 6e 73 20 61 72 65 20 65 6e 61 62 6c 65 64 2c 20  ns are enabled, 
87940 74 68 65 20 66 69 6c 65 70 61 74 68 20 61 6e 64  the filepath and
87950 20 6c 6f 63 6b 69 6e 67 20 73 74 79 6c 65 20 0a   locking style .
87960 20 2a 2a 20 61 72 65 20 6e 65 65 64 65 64 20 74   ** are needed t
87970 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20  o determine the 
87980 75 6e 69 78 46 69 6c 65 20 70 4d 65 74 68 6f 64  unixFile pMethod
87990 20 74 6f 20 75 73 65 20 66 6f 72 20 6c 6f 63 6b   to use for lock
879a0 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 73 2e 0a  ing operations..
879b0 20 2a 2a 20 54 68 65 20 6c 6f 63 6b 69 6e 67 2d   ** The locking-
879c0 73 74 79 6c 65 20 73 70 65 63 69 66 69 63 20 6c  style specific l
879d0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 64 61  ockingContext da
879e0 74 61 20 73 74 72 75 63 74 75 72 65 20 69 73 20  ta structure is 
879f0 63 72 65 61 74 65 64 20 0a 20 2a 2a 20 61 6e 64  created . ** and
87a00 20 61 73 73 69 67 6e 65 64 20 68 65 72 65 20 61   assigned here a
87a10 6c 73 6f 2e 0a 20 2a 2f 0a 73 74 61 74 69 63 20  lso.. */.static 
87a20 69 6e 74 20 61 6c 6c 6f 63 61 74 65 55 6e 69 78  int allocateUnix
87a30 46 69 6c 65 28 0a 20 20 69 6e 74 20 68 2c 20 20  File(.  int h,  
87a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
87a50 2f 2a 20 4f 70 65 6e 20 66 69 6c 65 20 64 65 73  /* Open file des
87a60 63 72 69 70 74 6f 72 20 6f 66 20 66 69 6c 65 20  criptor of file 
87a70 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 2a 2f 0a  being opened */.
87a80 20 20 4f 73 46 69 6c 65 20 2a 2a 70 49 64 2c 20    OsFile **pId, 
87a90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
87aa0 74 65 20 63 6f 6d 70 6c 65 74 65 64 20 69 6e 69  te completed ini
87ab0 74 69 61 6c 69 7a 61 74 69 6f 6e 20 68 65 72 65  tialization here
87ac0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
87ad0 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 2f 2a   *zFilename,  /*
87ae0 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c   Name of the fil
87af0 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 2a  e being opened *
87b00 2f 0a 20 20 69 6e 74 20 64 65 6c 46 6c 61 67 20  /.  int delFlag 
87b10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
87b20 65 6c 65 74 65 2d 6f 6e 2d 6f 72 2d 62 65 66 6f  elete-on-or-befo
87b30 72 65 2d 63 6c 6f 73 65 20 66 6c 61 67 20 2a 2f  re-close flag */
87b40 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 4c 6f 63  .){.  sqlite3Loc
87b50 6b 69 6e 67 53 74 79 6c 65 20 6c 6f 63 6b 69 6e  kingStyle lockin
87b60 67 53 74 79 6c 65 3b 0a 20 20 75 6e 69 78 46 69  gStyle;.  unixFi
87b70 6c 65 20 2a 70 4e 65 77 3b 0a 20 20 75 6e 69 78  le *pNew;.  unix
87b80 46 69 6c 65 20 66 3b 0a 20 20 69 6e 74 20 72 63  File f;.  int rc
87b90 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 26 66 2c 20  ;..  memset(&f, 
87ba0 30 2c 20 73 69 7a 65 6f 66 28 66 29 29 3b 0a 20  0, sizeof(f));. 
87bb0 20 6c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d 20   lockingStyle = 
87bc0 73 71 6c 69 74 65 33 44 65 74 65 63 74 4c 6f 63  sqlite3DetectLoc
87bd0 6b 69 6e 67 53 74 79 6c 65 28 7a 46 69 6c 65 6e  kingStyle(zFilen
87be0 61 6d 65 2c 20 68 29 3b 0a 20 20 69 66 20 28 20  ame, h);.  if ( 
87bf0 6c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d 3d 20  lockingStyle == 
87c00 70 6f 73 69 78 4c 6f 63 6b 69 6e 67 53 74 79 6c  posixLockingStyl
87c10 65 20 29 20 7b 0a 20 20 20 20 73 71 6c 69 74 65  e ) {.    sqlite
87c20 33 4f 73 45 6e 74 65 72 4d 75 74 65 78 28 29 3b  3OsEnterMutex();
87c30 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c 6f  .    rc = findLo
87c40 63 6b 49 6e 66 6f 28 68 2c 20 26 66 2e 70 4c 6f  ckInfo(h, &f.pLo
87c50 63 6b 2c 20 26 66 2e 70 4f 70 65 6e 29 3b 0a 20  ck, &f.pOpen);. 
87c60 20 20 20 73 71 6c 69 74 65 33 4f 73 4c 65 61 76     sqlite3OsLeav
87c70 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 69 66  eMutex();.    if
87c80 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 63 6c  ( rc ){.      cl
87c90 6f 73 65 28 68 29 3b 0a 20 20 20 20 20 20 75 6e  ose(h);.      un
87ca0 6c 69 6e 6b 28 7a 46 69 6c 65 6e 61 6d 65 29 3b  link(zFilename);
87cb0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
87cc0 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
87cd0 7d 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20  }.  } else {.   
87ce0 20 2f 2a 20 20 70 4c 6f 63 6b 20 61 6e 64 20 70   /*  pLock and p
87cf0 4f 70 65 6e 20 61 72 65 20 6f 6e 6c 79 20 75 73  Open are only us
87d00 65 64 20 66 6f 72 20 70 6f 73 69 78 20 61 64 76  ed for posix adv
87d10 69 73 6f 72 79 20 6c 6f 63 6b 69 6e 67 20 2a 2f  isory locking */
87d20 0a 20 20 20 20 66 2e 70 4c 6f 63 6b 20 3d 20 4e  .    f.pLock = N
87d30 55 4c 4c 3b 0a 20 20 20 20 66 2e 70 4f 70 65 6e  ULL;.    f.pOpen
87d40 20 3d 20 4e 55 4c 4c 3b 0a 20 20 7d 0a 20 20 69   = NULL;.  }.  i
87d50 66 28 20 64 65 6c 46 6c 61 67 20 29 7b 0a 20 20  f( delFlag ){.  
87d60 20 20 75 6e 6c 69 6e 6b 28 7a 46 69 6c 65 6e 61    unlink(zFilena
87d70 6d 65 29 3b 0a 20 20 7d 0a 20 20 66 2e 64 69 72  me);.  }.  f.dir
87d80 66 64 20 3d 20 2d 31 3b 0a 20 20 66 2e 68 20 3d  fd = -1;.  f.h =
87d90 20 68 3b 0a 20 20 53 45 54 5f 54 48 52 45 41 44   h;.  SET_THREAD
87da0 49 44 28 26 66 29 3b 0a 20 20 70 4e 65 77 20 3d  ID(&f);.  pNew =
87db0 20 73 71 6c 69 74 65 33 54 68 72 65 61 64 53 61   sqlite3ThreadSa
87dc0 66 65 4d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66  feMalloc( sizeof
87dd0 28 75 6e 69 78 46 69 6c 65 29 20 29 3b 0a 20 20  (unixFile) );.  
87de0 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20  if( pNew==0 ){. 
87df0 20 20 20 63 6c 6f 73 65 28 68 29 3b 0a 20 20 20     close(h);.   
87e00 20 73 71 6c 69 74 65 33 4f 73 45 6e 74 65 72 4d   sqlite3OsEnterM
87e10 75 74 65 78 28 29 3b 0a 20 20 20 20 72 65 6c 65  utex();.    rele
87e20 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 66 2e 70 4c  aseLockInfo(f.pL
87e30 6f 63 6b 29 3b 0a 20 20 20 20 72 65 6c 65 61 73  ock);.    releas
87e40 65 4f 70 65 6e 43 6e 74 28 66 2e 70 4f 70 65 6e  eOpenCnt(f.pOpen
87e50 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73  );.    sqlite3Os
87e60 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20  LeaveMutex();.  
87e70 20 20 2a 70 49 64 20 3d 20 30 3b 0a 20 20 20 20    *pId = 0;.    
87e80 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
87e90 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  MEM;.  }else{.  
87ea0 20 20 2a 70 4e 65 77 20 3d 20 66 3b 0a 20 20 20    *pNew = f;.   
87eb0 20 73 77 69 74 63 68 28 6c 6f 63 6b 69 6e 67 53   switch(lockingS
87ec0 74 79 6c 65 29 20 7b 0a 20 20 20 20 20 20 63 61  tyle) {.      ca
87ed0 73 65 20 61 66 70 4c 6f 63 6b 69 6e 67 53 74 79  se afpLockingSty
87ee0 6c 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a  le: {.        /*
87ef0 20 61 66 70 20 6c 6f 63 6b 69 6e 67 20 75 73 65   afp locking use
87f00 73 20 74 68 65 20 66 69 6c 65 20 70 61 74 68 20  s the file path 
87f10 73 6f 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62  so it needs to b
87f20 65 20 69 6e 63 6c 75 64 65 64 20 69 6e 0a 20 20  e included in.  
87f30 20 20 20 20 20 20 2a 2a 20 74 68 65 20 61 66 70        ** the afp
87f40 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a  LockingContext *
87f50 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 46  /.        int nF
87f60 69 6c 65 6e 61 6d 65 3b 0a 20 20 20 20 20 20 20  ilename;.       
87f70 20 70 4e 65 77 2d 3e 70 4d 65 74 68 6f 64 20 3d   pNew->pMethod =
87f80 20 26 73 71 6c 69 74 65 33 41 46 50 4c 6f 63 6b   &sqlite3AFPLock
87f90 69 6e 67 55 6e 69 78 49 6f 4d 65 74 68 6f 64 3b  ingUnixIoMethod;
87fa0 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 6c  .        pNew->l
87fb0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20  ockingContext = 
87fc0 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
87fd0 65 33 54 68 72 65 61 64 53 61 66 65 4d 61 6c 6c  e3ThreadSafeMall
87fe0 6f 63 28 73 69 7a 65 6f 66 28 61 66 70 4c 6f 63  oc(sizeof(afpLoc
87ff0 6b 69 6e 67 43 6f 6e 74 65 78 74 29 29 3b 0a 20  kingContext));. 
88000 20 20 20 20 20 20 20 6e 46 69 6c 65 6e 61 6d 65         nFilename
88010 20 3d 20 73 74 72 6c 65 6e 28 7a 46 69 6c 65 6e   = strlen(zFilen
88020 61 6d 65 29 2b 31 3b 0a 20 20 20 20 20 20 20 20  ame)+1;.        
88030 28 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74  ((afpLockingCont
88040 65 78 74 20 2a 29 70 4e 65 77 2d 3e 6c 6f 63 6b  ext *)pNew->lock
88050 69 6e 67 43 6f 6e 74 65 78 74 29 2d 3e 66 69 6c  ingContext)->fil
88060 65 50 61 74 68 20 3d 20 0a 20 20 20 20 20 20 20  ePath = .       
88070 20 20 20 73 71 6c 69 74 65 33 54 68 72 65 61 64     sqlite3Thread
88080 53 61 66 65 4d 61 6c 6c 6f 63 28 6e 46 69 6c 65  SafeMalloc(nFile
88090 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 6d  name);.        m
880a0 65 6d 63 70 79 28 28 28 61 66 70 4c 6f 63 6b 69  emcpy(((afpLocki
880b0 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 4e 65 77  ngContext *)pNew
880c0 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ->lockingContext
880d0 29 2d 3e 66 69 6c 65 50 61 74 68 2c 20 0a 20 20  )->filePath, .  
880e0 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 46 69               zFi
880f0 6c 65 6e 61 6d 65 2c 20 6e 46 69 6c 65 6e 61 6d  lename, nFilenam
88100 65 29 3b 0a 20 20 20 20 20 20 20 20 73 72 61 6e  e);.        sran
88110 64 6f 6d 64 65 76 28 29 3b 0a 20 20 20 20 20 20  domdev();.      
88120 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
88130 0a 20 20 20 20 20 20 63 61 73 65 20 66 6c 6f 63  .      case floc
88140 6b 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 3a 0a 20  kLockingStyle:. 
88150 20 20 20 20 20 20 20 2f 2a 20 66 6c 6f 63 6b 20         /* flock 
88160 6c 6f 63 6b 69 6e 67 20 64 6f 65 73 6e 27 74 20  locking doesn't 
88170 6e 65 65 64 20 61 64 64 69 74 69 6f 6e 61 6c 20  need additional 
88180 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 69  lockingContext i
88190 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20  nformation */.  
881a0 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4d 65 74        pNew->pMet
881b0 68 6f 64 20 3d 20 26 73 71 6c 69 74 65 33 46 6c  hod = &sqlite3Fl
881c0 6f 63 6b 4c 6f 63 6b 69 6e 67 55 6e 69 78 49 6f  ockLockingUnixIo
881d0 4d 65 74 68 6f 64 3b 0a 20 20 20 20 20 20 20 20  Method;.        
881e0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
881f0 65 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67  e dotlockLocking
88200 53 74 79 6c 65 3a 20 7b 0a 20 20 20 20 20 20 20  Style: {.       
88210 20 2f 2a 20 64 6f 74 6c 6f 63 6b 20 6c 6f 63 6b   /* dotlock lock
88220 69 6e 67 20 75 73 65 73 20 74 68 65 20 66 69 6c  ing uses the fil
88230 65 20 70 61 74 68 20 73 6f 20 69 74 20 6e 65 65  e path so it nee
88240 64 73 20 74 6f 20 62 65 20 69 6e 63 6c 75 64 65  ds to be include
88250 64 20 69 6e 0a 20 20 20 20 20 20 20 20 20 2a 2a  d in.         **
88260 20 74 68 65 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b   the dotlockLock
88270 69 6e 67 43 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ingContext */.  
88280 20 20 20 20 20 20 69 6e 74 20 6e 46 69 6c 65 6e        int nFilen
88290 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 70 4e 65  ame;.        pNe
882a0 77 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 73 71  w->pMethod = &sq
882b0 6c 69 74 65 33 44 6f 74 6c 6f 63 6b 4c 6f 63 6b  lite3DotlockLock
882c0 69 6e 67 55 6e 69 78 49 6f 4d 65 74 68 6f 64 3b  ingUnixIoMethod;
882d0 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 6c  .        pNew->l
882e0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20  ockingContext = 
882f0 73 71 6c 69 74 65 33 54 68 72 65 61 64 53 61 66  sqlite3ThreadSaf
88300 65 4d 61 6c 6c 6f 63 28 0a 20 20 20 20 20 20 20  eMalloc(.       
88310 20 20 20 73 69 7a 65 6f 66 28 64 6f 74 6c 6f 63     sizeof(dotloc
88320 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29  kLockingContext)
88330 29 3b 0a 20 20 20 20 20 20 20 20 6e 46 69 6c 65  );.        nFile
88340 6e 61 6d 65 20 3d 20 73 74 72 6c 65 6e 28 7a 46  name = strlen(zF
88350 69 6c 65 6e 61 6d 65 29 20 2b 20 36 3b 0a 20 20  ilename) + 6;.  
88360 20 20 20 20 20 20 28 28 64 6f 74 6c 6f 63 6b 4c        ((dotlockL
88370 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29  ockingContext *)
88380 70 4e 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e  pNew->lockingCon
88390 74 65 78 74 29 2d 3e 6c 6f 63 6b 50 61 74 68 20  text)->lockPath 
883a0 3d 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  = .            s
883b0 71 6c 69 74 65 33 54 68 72 65 61 64 53 61 66 65  qlite3ThreadSafe
883c0 4d 61 6c 6c 6f 63 28 20 6e 46 69 6c 65 6e 61 6d  Malloc( nFilenam
883d0 65 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  e );.        sql
883e0 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 46  ite3_snprintf(nF
883f0 69 6c 65 6e 61 6d 65 2c 20 0a 20 20 20 20 20 20  ilename, .      
88400 20 20 20 20 20 20 20 20 20 20 28 28 64 6f 74 6c            ((dotl
88410 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  ockLockingContex
88420 74 20 2a 29 70 4e 65 77 2d 3e 6c 6f 63 6b 69 6e  t *)pNew->lockin
88430 67 43 6f 6e 74 65 78 74 29 2d 3e 6c 6f 63 6b 50  gContext)->lockP
88440 61 74 68 2c 20 0a 20 20 20 20 20 20 20 20 20 20  ath, .          
88450 20 20 20 20 20 20 22 25 73 2e 6c 6f 63 6b 22 2c        "%s.lock",
88460 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20   zFilename);.   
88470 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
88480 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 70    }.      case p
88490 6f 73 69 78 4c 6f 63 6b 69 6e 67 53 74 79 6c 65  osixLockingStyle
884a0 3a 0a 20 20 20 20 20 20 20 20 2f 2a 20 70 6f 73  :.        /* pos
884b0 69 78 20 6c 6f 63 6b 69 6e 67 20 64 6f 65 73 6e  ix locking doesn
884c0 27 74 20 6e 65 65 64 20 61 64 64 69 74 69 6f 6e  't need addition
884d0 61 6c 20 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  al lockingContex
884e0 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f  t information */
884f0 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70  .        pNew->p
88500 4d 65 74 68 6f 64 20 3d 20 26 73 71 6c 69 74 65  Method = &sqlite
88510 33 55 6e 69 78 49 6f 4d 65 74 68 6f 64 3b 0a 20  3UnixIoMethod;. 
88520 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
88530 20 20 20 20 63 61 73 65 20 6e 6f 4c 6f 63 6b 69      case noLocki
88540 6e 67 53 74 79 6c 65 3a 0a 20 20 20 20 20 20 63  ngStyle:.      c
88550 61 73 65 20 75 6e 73 75 70 70 6f 72 74 65 64 4c  ase unsupportedL
88560 6f 63 6b 69 6e 67 53 74 79 6c 65 3a 0a 20 20 20  ockingStyle:.   
88570 20 20 20 64 65 66 61 75 6c 74 3a 20 0a 20 20 20     default: .   
88580 20 20 20 20 20 70 4e 65 77 2d 3e 70 4d 65 74 68       pNew->pMeth
88590 6f 64 20 3d 20 26 73 71 6c 69 74 65 33 4e 6f 6c  od = &sqlite3Nol
885a0 6f 63 6b 4c 6f 63 6b 69 6e 67 55 6e 69 78 49 6f  ockLockingUnixIo
885b0 4d 65 74 68 6f 64 3b 0a 20 20 20 20 7d 0a 20 20  Method;.    }.  
885c0 20 20 2a 70 49 64 20 3d 20 28 4f 73 46 69 6c 65    *pId = (OsFile
885d0 2a 29 70 4e 65 77 3b 0a 20 20 20 20 4f 70 65 6e  *)pNew;.    Open
885e0 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20 20  Counter(+1);.   
885f0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
88600 4b 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 20 2f  K;.  }.}.#else /
88610 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  * SQLITE_ENABLE_
88620 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f  LOCKING_STYLE */
88630 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f  .static int allo
88640 63 61 74 65 55 6e 69 78 46 69 6c 65 28 0a 20 20  cateUnixFile(.  
88650 69 6e 74 20 68 2c 20 20 20 20 20 20 20 20 20 20  int h,          
88660 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 66         /* Open f
88670 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 6f  ile descriptor o
88680 6e 20 66 69 6c 65 20 62 65 69 6e 67 20 6f 70 65  n file being ope
88690 6e 65 64 20 2a 2f 0a 20 20 4f 73 46 69 6c 65 20  ned */.  OsFile 
886a0 2a 2a 70 49 64 2c 20 20 20 20 20 20 20 20 20 20  **pId,          
886b0 2f 2a 20 57 72 69 74 65 20 74 68 65 20 72 65 73  /* Write the res
886c0 75 6c 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75  ul unixFile stru
886d0 63 74 75 72 65 20 68 65 72 65 20 2a 2f 0a 20 20  cture here */.  
886e0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
886f0 65 6e 61 6d 65 2c 20 2f 2a 20 4e 61 6d 65 20 6f  ename, /* Name o
88700 66 20 74 68 65 20 66 69 6c 65 20 62 65 69 6e 67  f the file being
88710 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 69 6e 74   opened */.  int
88720 20 64 65 6c 46 6c 61 67 20 20 20 20 20 20 20 20   delFlag        
88730 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c 20      /* If true, 
88740 64 65 6c 65 74 65 20 74 68 65 20 66 69 6c 65 20  delete the file 
88750 6f 6e 20 6f 72 20 62 65 66 6f 72 65 20 63 6c 6f  on or before clo
88760 73 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 75 6e 69  sing */.){.  uni
88770 78 46 69 6c 65 20 2a 70 4e 65 77 3b 0a 20 20 75  xFile *pNew;.  u
88780 6e 69 78 46 69 6c 65 20 66 3b 0a 20 20 69 6e 74  nixFile f;.  int
88790 20 72 63 3b 0a 0a 23 69 66 64 65 66 20 46 44 5f   rc;..#ifdef FD_
887a0 43 4c 4f 45 58 45 43 0a 20 20 66 63 6e 74 6c 28  CLOEXEC.  fcntl(
887b0 68 2c 20 46 5f 53 45 54 46 44 2c 20 66 63 6e 74  h, F_SETFD, fcnt
887c0 6c 28 68 2c 20 46 5f 47 45 54 46 44 2c 20 30 29  l(h, F_GETFD, 0)
887d0 20 7c 20 46 44 5f 43 4c 4f 45 58 45 43 29 3b 0a   | FD_CLOEXEC);.
887e0 23 65 6e 64 69 66 0a 20 20 6d 65 6d 73 65 74 28  #endif.  memset(
887f0 26 66 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66 29  &f, 0, sizeof(f)
88800 29 3b 0a 20 20 73 71 6c 69 74 65 33 4f 73 45 6e  );.  sqlite3OsEn
88810 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 72 63  terMutex();.  rc
88820 20 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28   = findLockInfo(
88830 68 2c 20 26 66 2e 70 4c 6f 63 6b 2c 20 26 66 2e  h, &f.pLock, &f.
88840 70 4f 70 65 6e 29 3b 0a 20 20 73 71 6c 69 74 65  pOpen);.  sqlite
88850 33 4f 73 4c 65 61 76 65 4d 75 74 65 78 28 29 3b  3OsLeaveMutex();
88860 0a 20 20 69 66 28 20 64 65 6c 46 6c 61 67 20 29  .  if( delFlag )
88870 7b 0a 20 20 20 20 75 6e 6c 69 6e 6b 28 7a 46 69  {.    unlink(zFi
88880 6c 65 6e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 69  lename);.  }.  i
88890 66 28 20 72 63 20 29 7b 0a 20 20 20 20 63 6c 6f  f( rc ){.    clo
888a0 73 65 28 68 29 3b 0a 20 20 20 20 72 65 74 75 72  se(h);.    retur
888b0 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
888c0 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 33 28 22    }.  OSTRACE3("
888d0 4f 50 45 4e 20 20 20 20 25 2d 33 64 20 25 73 5c  OPEN    %-3d %s\
888e0 6e 22 2c 20 68 2c 20 7a 46 69 6c 65 6e 61 6d 65  n", h, zFilename
888f0 29 3b 0a 20 20 66 2e 64 69 72 66 64 20 3d 20 2d  );.  f.dirfd = -
88900 31 3b 0a 20 20 66 2e 68 20 3d 20 68 3b 0a 20 20  1;.  f.h = h;.  
88910 53 45 54 5f 54 48 52 45 41 44 49 44 28 26 66 29  SET_THREADID(&f)
88920 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74  ;.  pNew = sqlit
88930 65 33 54 68 72 65 61 64 53 61 66 65 4d 61 6c 6c  e3ThreadSafeMall
88940 6f 63 28 20 73 69 7a 65 6f 66 28 75 6e 69 78 46  oc( sizeof(unixF
88950 69 6c 65 29 20 29 3b 0a 20 20 69 66 28 20 70 4e  ile) );.  if( pN
88960 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6c 6f  ew==0 ){.    clo
88970 73 65 28 68 29 3b 0a 20 20 20 20 73 71 6c 69 74  se(h);.    sqlit
88980 65 33 4f 73 45 6e 74 65 72 4d 75 74 65 78 28 29  e3OsEnterMutex()
88990 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 4c 6f 63  ;.    releaseLoc
889a0 6b 49 6e 66 6f 28 66 2e 70 4c 6f 63 6b 29 3b 0a  kInfo(f.pLock);.
889b0 20 20 20 20 72 65 6c 65 61 73 65 4f 70 65 6e 43      releaseOpenC
889c0 6e 74 28 66 2e 70 4f 70 65 6e 29 3b 0a 20 20 20  nt(f.pOpen);.   
889d0 20 73 71 6c 69 74 65 33 4f 73 4c 65 61 76 65 4d   sqlite3OsLeaveM
889e0 75 74 65 78 28 29 3b 0a 20 20 20 20 2a 70 49 64  utex();.    *pId
889f0 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e   = 0;.    return
88a00 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
88a10 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 4e 65   }else{.    *pNe
88a20 77 20 3d 20 66 3b 0a 20 20 20 20 70 4e 65 77 2d  w = f;.    pNew-
88a30 3e 70 4d 65 74 68 6f 64 20 3d 20 26 73 71 6c 69  >pMethod = &sqli
88a40 74 65 33 55 6e 69 78 49 6f 4d 65 74 68 6f 64 3b  te3UnixIoMethod;
88a50 0a 20 20 20 20 2a 70 49 64 20 3d 20 28 4f 73 46  .    *pId = (OsF
88a60 69 6c 65 2a 29 70 4e 65 77 3b 0a 20 20 20 20 4f  ile*)pNew;.    O
88a70 70 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a  penCounter(+1);.
88a80 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
88a90 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64  E_OK;.  }.}.#end
88aa0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41  if /* SQLITE_ENA
88ab0 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
88ac0 45 20 2a 2f 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  E */..#endif /* 
88ad0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b  SQLITE_OMIT_DISK
88ae0 49 4f 20 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  IO */./*********
88af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88b30 2a 2a 0a 2a 2a 20 45 76 65 72 79 74 68 69 6e 67  **.** Everything
88b40 20 61 62 6f 76 65 20 64 65 61 6c 73 20 77 69 74   above deals wit
88b50 68 20 66 69 6c 65 20 49 2f 4f 2e 20 20 45 76 65  h file I/O.  Eve
88b60 72 79 74 68 69 6e 67 20 74 68 61 74 20 66 6f 6c  rything that fol
88b70 6c 6f 77 73 20 64 65 61 6c 73 0a 2a 2a 20 77 69  lows deals.** wi
88b80 74 68 20 6f 74 68 65 72 20 6d 69 73 63 65 6c 6c  th other miscell
88b90 61 6e 6f 75 73 20 61 73 70 65 63 74 73 20 6f 66  anous aspects of
88ba0 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73   the operating s
88bb0 79 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65 0a  ystem interface.
88bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a  ************/...
88c10 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
88c20 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49  MIT_LOAD_EXTENSI
88c30 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61  ON./*.** Interfa
88c40 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20  ces for opening 
88c50 61 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79  a shared library
88c60 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20  , finding entry 
88c70 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e  points.** within
88c80 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72   the shared libr
88c90 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67  ary, and closing
88ca0 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72   the shared libr
88cb0 61 72 79 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65  ary..*/.#include
88cc0 20 3c 64 6c 66 63 6e 2e 68 3e 0a 53 51 4c 49 54   <dlfcn.h>.SQLIT
88cd0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
88ce0 73 71 6c 69 74 65 33 55 6e 69 78 44 6c 6f 70 65  sqlite3UnixDlope
88cf0 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  n(const char *zF
88d00 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 72 65 74 75  ilename){.  retu
88d10 72 6e 20 64 6c 6f 70 65 6e 28 7a 46 69 6c 65 6e  rn dlopen(zFilen
88d20 61 6d 65 2c 20 52 54 4c 44 5f 4e 4f 57 20 7c 20  ame, RTLD_NOW | 
88d30 52 54 4c 44 5f 47 4c 4f 42 41 4c 29 3b 0a 7d 0a  RTLD_GLOBAL);.}.
88d40 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
88d50 6f 69 64 20 2a 73 71 6c 69 74 65 33 55 6e 69 78  oid *sqlite3Unix
88d60 44 6c 73 79 6d 28 76 6f 69 64 20 2a 70 48 61 6e  Dlsym(void *pHan
88d70 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  dle, const char 
88d80 2a 7a 53 79 6d 62 6f 6c 29 7b 0a 20 20 72 65 74  *zSymbol){.  ret
88d90 75 72 6e 20 64 6c 73 79 6d 28 70 48 61 6e 64 6c  urn dlsym(pHandl
88da0 65 2c 20 7a 53 79 6d 62 6f 6c 29 3b 0a 7d 0a 53  e, zSymbol);.}.S
88db0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
88dc0 74 20 73 71 6c 69 74 65 33 55 6e 69 78 44 6c 63  t sqlite3UnixDlc
88dd0 6c 6f 73 65 28 76 6f 69 64 20 2a 70 48 61 6e 64  lose(void *pHand
88de0 6c 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 64 6c  le){.  return dl
88df0 63 6c 6f 73 65 28 70 48 61 6e 64 6c 65 29 3b 0a  close(pHandle);.
88e00 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
88e10 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54  TE_OMIT_LOAD_EXT
88e20 45 4e 53 49 4f 4e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  ENSION */../*.**
88e30 20 47 65 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e   Get information
88e40 20 74 6f 20 73 65 65 64 20 74 68 65 20 72 61 6e   to seed the ran
88e50 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65 72  dom number gener
88e60 61 74 6f 72 2e 20 20 54 68 65 20 73 65 65 64 0a  ator.  The seed.
88e70 2a 2a 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e  ** is written in
88e80 74 6f 20 74 68 65 20 62 75 66 66 65 72 20 7a 42  to the buffer zB
88e90 75 66 5b 32 35 36 5d 2e 20 20 54 68 65 20 63 61  uf[256].  The ca
88ea0 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6d  lling function m
88eb0 75 73 74 0a 2a 2a 20 73 75 70 70 6c 79 20 61 20  ust.** supply a 
88ec0 73 75 66 66 69 63 69 65 6e 74 6c 79 20 6c 61 72  sufficiently lar
88ed0 67 65 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 53 51  ge buffer..*/.SQ
88ee0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
88ef0 20 73 71 6c 69 74 65 33 55 6e 69 78 52 61 6e 64   sqlite3UnixRand
88f00 6f 6d 53 65 65 64 28 63 68 61 72 20 2a 7a 42 75  omSeed(char *zBu
88f10 66 29 7b 0a 20 20 2f 2a 20 57 65 20 68 61 76 65  f){.  /* We have
88f20 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 7a   to initialize z
88f30 42 75 66 20 74 6f 20 70 72 65 76 65 6e 74 20 76  Buf to prevent v
88f40 61 6c 67 72 69 6e 64 20 66 72 6f 6d 20 72 65 70  algrind from rep
88f50 6f 72 74 69 6e 67 0a 20 20 2a 2a 20 65 72 72 6f  orting.  ** erro
88f60 72 73 2e 20 20 54 68 65 20 72 65 70 6f 72 74 73  rs.  The reports
88f70 20 69 73 73 75 65 64 20 62 79 20 76 61 6c 67 72   issued by valgr
88f80 69 6e 64 20 61 72 65 20 69 6e 63 6f 72 72 65 63  ind are incorrec
88f90 74 20 2d 20 77 65 20 77 6f 75 6c 64 0a 20 20 2a  t - we would.  *
88fa0 2a 20 70 72 65 66 65 72 20 74 68 61 74 20 74 68  * prefer that th
88fb0 65 20 72 61 6e 64 6f 6d 6e 65 73 73 20 62 65 20  e randomness be 
88fc0 69 6e 63 72 65 61 73 65 64 20 62 79 20 6d 61 6b  increased by mak
88fd0 69 6e 67 20 75 73 65 20 6f 66 20 74 68 65 0a 20  ing use of the. 
88fe0 20 2a 2a 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65   ** uninitialize
88ff0 64 20 73 70 61 63 65 20 69 6e 20 7a 42 75 66 20  d space in zBuf 
89000 2d 20 62 75 74 20 76 61 6c 67 72 69 6e 64 20 65  - but valgrind e
89010 72 72 6f 72 73 20 74 65 6e 64 20 74 6f 20 77 6f  rrors tend to wo
89020 72 72 79 0a 20 20 2a 2a 20 73 6f 6d 65 20 75 73  rry.  ** some us
89030 65 72 73 2e 20 20 52 61 74 68 65 72 20 74 68 61  ers.  Rather tha
89040 6e 20 61 72 67 75 65 2c 20 69 74 20 73 65 65 6d  n argue, it seem
89050 73 20 65 61 73 69 65 72 20 6a 75 73 74 20 74 6f  s easier just to
89060 20 69 6e 69 74 69 61 6c 69 7a 65 0a 20 20 2a 2a   initialize.  **
89070 20 74 68 65 20 77 68 6f 6c 65 20 61 72 72 61 79   the whole array
89080 20 61 6e 64 20 73 69 6c 65 6e 63 65 20 76 61 6c   and silence val
89090 67 72 69 6e 64 2c 20 65 76 65 6e 20 69 66 20 74  grind, even if t
890a0 68 61 74 20 6d 65 61 6e 73 20 6c 65 73 73 20 72  hat means less r
890b0 61 6e 64 6f 6d 6e 65 73 73 0a 20 20 2a 2a 20 69  andomness.  ** i
890c0 6e 20 74 68 65 20 72 61 6e 64 6f 6d 20 73 65 65  n the random see
890d0 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 65  d..  **.  ** Whe
890e0 6e 20 74 65 73 74 69 6e 67 2c 20 69 6e 69 74 69  n testing, initi
890f0 61 6c 69 7a 69 6e 67 20 7a 42 75 66 5b 5d 20 74  alizing zBuf[] t
89100 6f 20 7a 65 72 6f 20 69 73 20 61 6c 6c 20 77 65  o zero is all we
89110 20 64 6f 2e 20 20 54 68 61 74 20 6d 65 61 6e 73   do.  That means
89120 0a 20 20 2a 2a 20 74 68 61 74 20 77 65 20 61 6c  .  ** that we al
89130 77 61 79 73 20 75 73 65 20 74 68 65 20 73 61 6d  ways use the sam
89140 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20  e random number 
89150 73 65 71 75 65 6e 63 65 2e 20 20 54 68 69 73 20  sequence.  This 
89160 6d 61 6b 65 73 20 74 68 65 0a 20 20 2a 2a 20 74  makes the.  ** t
89170 65 73 74 73 20 72 65 70 65 61 74 61 62 6c 65 2e  ests repeatable.
89180 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 7a  .  */.  memset(z
89190 42 75 66 2c 20 30 2c 20 32 35 36 29 3b 0a 23 69  Buf, 0, 256);.#i
891a0 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
891b0 45 5f 54 45 53 54 29 0a 20 20 7b 0a 20 20 20 20  E_TEST).  {.    
891c0 69 6e 74 20 70 69 64 2c 20 66 64 3b 0a 20 20 20  int pid, fd;.   
891d0 20 66 64 20 3d 20 6f 70 65 6e 28 22 2f 64 65 76   fd = open("/dev
891e0 2f 75 72 61 6e 64 6f 6d 22 2c 20 4f 5f 52 44 4f  /urandom", O_RDO
891f0 4e 4c 59 29 3b 0a 20 20 20 20 69 66 28 20 66 64  NLY);.    if( fd
89200 3c 30 20 29 7b 0a 20 20 20 20 20 20 74 69 6d 65  <0 ){.      time
89210 5f 74 20 74 3b 0a 20 20 20 20 20 20 74 69 6d 65  _t t;.      time
89220 28 26 74 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63  (&t);.      memc
89230 70 79 28 7a 42 75 66 2c 20 26 74 2c 20 73 69 7a  py(zBuf, &t, siz
89240 65 6f 66 28 74 29 29 3b 0a 20 20 20 20 20 20 70  eof(t));.      p
89250 69 64 20 3d 20 67 65 74 70 69 64 28 29 3b 0a 20  id = getpid();. 
89260 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75       memcpy(&zBu
89270 66 5b 73 69 7a 65 6f 66 28 74 69 6d 65 5f 74 29  f[sizeof(time_t)
89280 5d 2c 20 26 70 69 64 2c 20 73 69 7a 65 6f 66 28  ], &pid, sizeof(
89290 70 69 64 29 29 3b 0a 20 20 20 20 7d 65 6c 73 65  pid));.    }else
892a0 7b 0a 20 20 20 20 20 20 72 65 61 64 28 66 64 2c  {.      read(fd,
892b0 20 7a 42 75 66 2c 20 32 35 36 29 3b 0a 20 20 20   zBuf, 256);.   
892c0 20 20 20 63 6c 6f 73 65 28 66 64 29 3b 0a 20 20     close(fd);.  
892d0 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20    }.  }.#endif. 
892e0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
892f0 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65  K;.}../*.** Slee
89300 70 20 66 6f 72 20 61 20 6c 69 74 74 6c 65 20 77  p for a little w
89310 68 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 74 68  hile.  Return th
89320 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65  e amount of time
89330 20 73 6c 65 70 74 2e 0a 2a 2a 20 54 68 65 20 61   slept..** The a
89340 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 6e  rgument is the n
89350 75 6d 62 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65  umber of millise
89360 63 6f 6e 64 73 20 77 65 20 77 61 6e 74 20 74 6f  conds we want to
89370 20 73 6c 65 65 70 2e 0a 2a 2f 0a 53 51 4c 49 54   sleep..*/.SQLIT
89380 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
89390 6c 69 74 65 33 55 6e 69 78 53 6c 65 65 70 28 69  lite3UnixSleep(i
893a0 6e 74 20 6d 73 29 7b 0a 23 69 66 20 64 65 66 69  nt ms){.#if defi
893b0 6e 65 64 28 48 41 56 45 5f 55 53 4c 45 45 50 29  ned(HAVE_USLEEP)
893c0 20 26 26 20 48 41 56 45 5f 55 53 4c 45 45 50 0a   && HAVE_USLEEP.
893d0 20 20 75 73 6c 65 65 70 28 6d 73 2a 31 30 30 30    usleep(ms*1000
893e0 29 3b 0a 20 20 72 65 74 75 72 6e 20 6d 73 3b 0a  );.  return ms;.
893f0 23 65 6c 73 65 0a 20 20 73 6c 65 65 70 28 28 6d  #else.  sleep((m
89400 73 2b 39 39 39 29 2f 31 30 30 30 29 3b 0a 20 20  s+999)/1000);.  
89410 72 65 74 75 72 6e 20 31 30 30 30 2a 28 28 6d 73  return 1000*((ms
89420 2b 39 39 39 29 2f 31 30 30 30 29 3b 0a 23 65 6e  +999)/1000);.#en
89430 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 74 61  dif.}../*.** Sta
89440 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 75 73  tic variables us
89450 65 64 20 66 6f 72 20 74 68 72 65 61 64 20 73 79  ed for thread sy
89460 6e 63 68 72 6f 6e 69 7a 61 74 69 6f 6e 2e 0a 2a  nchronization..*
89470 2a 0a 2a 2a 20 69 6e 4d 75 74 65 78 20 20 20 20  *.** inMutex    
89480 20 20 74 68 65 20 6e 65 73 74 69 6e 67 20 64 65    the nesting de
89490 70 74 68 20 6f 66 20 74 68 65 20 72 65 63 75 72  pth of the recur
894a0 73 69 76 65 20 6d 75 74 65 78 2e 20 20 54 68 65  sive mutex.  The
894b0 20 74 68 72 65 61 64 0a 2a 2a 20 20 20 20 20 20   thread.**      
894c0 20 20 20 20 20 20 20 20 68 6f 6c 64 69 6e 67 20          holding 
894d0 6d 75 74 65 78 4d 61 69 6e 20 63 61 6e 20 72 65  mutexMain can re
894e0 61 64 20 74 68 69 73 20 76 61 72 69 61 62 6c 65  ad this variable
894f0 20 61 74 20 61 6e 79 20 74 69 6d 65 2e 0a 2a 2a   at any time..**
89500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 42 75                Bu
89510 74 20 69 73 20 6d 75 73 74 20 68 6f 6c 64 20 6d  t is must hold m
89520 75 74 65 78 41 75 78 20 74 6f 20 63 68 61 6e 67  utexAux to chang
89530 65 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 2e  e this variable.
89540 20 20 4f 74 68 65 72 0a 2a 2a 20 20 20 20 20 20    Other.**      
89550 20 20 20 20 20 20 20 20 74 68 72 65 61 64 73 20          threads 
89560 6d 75 73 74 20 68 6f 6c 64 20 6d 75 74 65 78 41  must hold mutexA
89570 75 78 20 74 6f 20 72 65 61 64 20 74 68 65 20 76  ux to read the v
89580 61 72 69 61 62 6c 65 20 61 6e 64 20 63 61 6e 0a  ariable and can.
89590 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
895a0 6e 65 76 65 72 20 77 72 69 74 65 2e 0a 2a 2a 0a  never write..**.
895b0 2a 2a 20 6d 75 74 65 78 4f 77 6e 65 72 20 20 20  ** mutexOwner   
895c0 54 68 65 20 74 68 72 65 61 64 20 69 64 20 6f 66  The thread id of
895d0 20 74 68 65 20 74 68 72 65 61 64 20 68 6f 6c 64   the thread hold
895e0 69 6e 67 20 6d 75 74 65 78 4d 61 69 6e 2e 20 20  ing mutexMain.  
895f0 53 61 6d 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  Same.**         
89600 20 20 20 20 20 61 63 63 65 73 73 20 72 75 6c 65       access rule
89610 73 20 61 73 20 66 6f 72 20 69 6e 4d 75 74 65 78  s as for inMutex
89620 2e 0a 2a 2a 0a 2a 2a 20 6d 75 74 65 78 4f 77 6e  ..**.** mutexOwn
89630 65 72 56 61 6c 69 64 20 20 20 54 72 75 65 20 69  erValid   True i
89640 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 6d  f the value in m
89650 75 74 65 78 4f 77 6e 65 72 20 69 73 20 76 61 6c  utexOwner is val
89660 69 64 2e 20 20 54 68 65 20 73 61 6d 65 0a 2a 2a  id.  The same.**
89670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
89680 20 20 20 61 63 63 65 73 73 20 72 75 6c 65 73 20     access rules 
89690 61 70 70 6c 79 20 61 73 20 66 6f 72 20 69 6e 4d  apply as for inM
896a0 75 74 65 78 2e 0a 2a 2a 0a 2a 2a 20 6d 75 74 65  utex..**.** mute
896b0 78 4d 61 69 6e 20 20 20 20 54 68 65 20 6d 61 69  xMain    The mai
896c0 6e 20 6d 75 74 65 78 2e 20 20 48 6f 6c 64 20 74  n mutex.  Hold t
896d0 68 69 73 20 6d 75 74 65 78 20 69 6e 20 6f 72 64  his mutex in ord
896e0 65 72 20 74 6f 20 67 65 74 20 65 78 63 6c 75 73  er to get exclus
896f0 69 76 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ive.**          
89700 20 20 20 20 61 63 63 65 73 73 20 74 6f 20 53 51      access to SQ
89710 4c 69 74 65 20 64 61 74 61 20 73 74 72 75 63 74  Lite data struct
89720 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 6d 75 74 65  ures..**.** mute
89730 78 41 75 78 20 20 20 20 20 41 6e 20 61 75 78 69  xAux     An auxi
89740 6c 69 61 72 79 20 6d 75 74 65 78 20 6e 65 65 64  liary mutex need
89750 65 64 20 74 6f 20 61 63 63 65 73 73 20 76 61 72  ed to access var
89760 69 61 62 6c 65 73 20 64 65 66 69 6e 65 64 20 61  iables defined a
89770 62 6f 76 65 2e 0a 2a 2a 0a 2a 2a 20 4d 75 74 65  bove..**.** Mute
89780 78 65 73 20 61 72 65 20 61 6c 77 61 79 73 20 61  xes are always a
89790 63 71 75 69 72 65 64 20 69 6e 20 74 68 69 73 20  cquired in this 
897a0 6f 72 64 65 72 3a 20 6d 75 74 65 78 4d 61 69 6e  order: mutexMain
897b0 20 6d 75 74 65 78 41 75 78 2e 20 20 20 49 74 0a   mutexAux.   It.
897c0 2a 2a 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73  ** is not necess
897d0 61 72 79 20 74 6f 20 61 63 71 75 69 72 65 20 6d  ary to acquire m
897e0 75 74 65 78 4d 61 69 6e 20 69 6e 20 6f 72 64 65  utexMain in orde
897f0 72 20 74 6f 20 67 65 74 20 6d 75 74 65 78 41 75  r to get mutexAu
89800 78 20 2d 20 6a 75 73 74 0a 2a 2a 20 64 6f 20 6e  x - just.** do n
89810 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 61 63  ot attempt to ac
89820 71 75 69 72 65 20 74 68 65 6d 20 69 6e 20 74 68  quire them in th
89830 65 20 72 65 76 65 72 73 65 20 6f 72 64 65 72 3a  e reverse order:
89840 20 6d 75 74 65 78 41 75 78 20 6d 75 74 65 78 4d   mutexAux mutexM
89850 61 69 6e 2e 0a 2a 2a 20 45 69 74 68 65 72 20 67  ain..** Either g
89860 65 74 20 74 68 65 20 6d 75 74 65 78 65 73 20 77  et the mutexes w
89870 69 74 68 20 6d 75 74 65 78 4d 61 69 6e 20 66 69  ith mutexMain fi
89880 72 73 74 20 6f 72 20 67 65 74 20 6d 75 74 65 78  rst or get mutex
89890 41 75 78 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20  Aux only..**.** 
898a0 57 68 65 6e 20 72 75 6e 6e 69 6e 67 20 6f 6e 20  When running on 
898b0 61 20 70 6c 61 74 66 6f 72 6d 20 77 68 65 72 65  a platform where
898c0 20 74 68 65 20 74 68 72 65 65 20 76 61 72 69 61   the three varia
898d0 62 6c 65 73 20 69 6e 4d 75 74 65 78 2c 20 6d 75  bles inMutex, mu
898e0 74 65 78 4f 77 6e 65 72 2c 0a 2a 2a 20 61 6e 64  texOwner,.** and
898f0 20 6d 75 74 65 78 4f 77 6e 65 72 56 61 6c 69 64   mutexOwnerValid
89900 20 63 61 6e 20 62 65 20 73 65 74 20 61 74 6f 6d   can be set atom
89910 69 63 61 6c 6c 79 2c 20 74 68 65 20 6d 75 74 65  ically, the mute
89920 78 41 75 78 20 69 73 20 6e 6f 74 20 72 65 71 75  xAux is not requ
89930 69 72 65 64 2e 0a 2a 2a 20 4f 6e 20 6d 61 6e 79  ired..** On many
89940 20 73 79 73 74 65 6d 73 2c 20 61 6c 6c 20 74 68   systems, all th
89950 72 65 65 20 61 72 65 20 33 32 2d 62 69 74 20 69  ree are 32-bit i
89960 6e 74 65 67 65 72 73 20 61 6e 64 20 77 72 69 74  ntegers and writ
89970 69 6e 67 20 74 6f 20 61 20 33 32 2d 62 69 74 0a  ing to a 32-bit.
89980 2a 2a 20 69 6e 74 65 67 65 72 20 69 73 20 61 74  ** integer is at
89990 6f 6d 69 63 2e 20 20 49 20 74 68 69 6e 6b 2e 20  omic.  I think. 
899a0 20 42 75 74 20 74 68 65 72 65 20 61 72 65 20 6e   But there are n
899b0 6f 20 67 75 61 72 61 6e 74 65 65 73 2e 20 20 53  o guarantees.  S
899c0 6f 20 69 74 20 73 65 65 6d 73 0a 2a 2a 20 73 61  o it seems.** sa
899d0 66 65 72 20 74 6f 20 70 72 6f 74 65 63 74 20 74  fer to protect t
899e0 68 65 6d 20 75 73 69 6e 67 20 6d 75 74 65 78 41  hem using mutexA
899f0 75 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ux..*/.static in
89a00 74 20 69 6e 4d 75 74 65 78 20 3d 20 30 3b 0a 23  t inMutex = 0;.#
89a10 69 66 64 65 66 20 53 51 4c 49 54 45 5f 55 4e 49  ifdef SQLITE_UNI
89a20 58 5f 54 48 52 45 41 44 53 0a 73 74 61 74 69 63  X_THREADS.static
89a30 20 70 74 68 72 65 61 64 5f 74 20 6d 75 74 65 78   pthread_t mutex
89a40 4f 77 6e 65 72 3b 20 20 20 20 20 20 20 20 20 20  Owner;          
89a50 2f 2a 20 54 68 72 65 61 64 20 68 6f 6c 64 69 6e  /* Thread holdin
89a60 67 20 6d 75 74 65 78 4d 61 69 6e 20 2a 2f 0a 73  g mutexMain */.s
89a70 74 61 74 69 63 20 69 6e 74 20 6d 75 74 65 78 4f  tatic int mutexO
89a80 77 6e 65 72 56 61 6c 69 64 20 3d 20 30 3b 20 20  wnerValid = 0;  
89a90 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
89aa0 6d 75 74 65 78 4f 77 6e 65 72 20 69 73 20 76 61  mutexOwner is va
89ab0 6c 69 64 20 2a 2f 0a 73 74 61 74 69 63 20 70 74  lid */.static pt
89ac0 68 72 65 61 64 5f 6d 75 74 65 78 5f 74 20 6d 75  hread_mutex_t mu
89ad0 74 65 78 4d 61 69 6e 20 3d 20 50 54 48 52 45 41  texMain = PTHREA
89ae0 44 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49  D_MUTEX_INITIALI
89af0 5a 45 52 3b 20 2f 2a 20 54 68 65 20 6d 75 74 65  ZER; /* The mute
89b00 78 20 2a 2f 0a 73 74 61 74 69 63 20 70 74 68 72  x */.static pthr
89b10 65 61 64 5f 6d 75 74 65 78 5f 74 20 6d 75 74 65  ead_mutex_t mute
89b20 78 41 75 78 20 3d 20 50 54 48 52 45 41 44 5f 4d  xAux = PTHREAD_M
89b30 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52  UTEX_INITIALIZER
89b40 3b 20 20 2f 2a 20 41 75 78 20 6d 75 74 65 78 20  ;  /* Aux mutex 
89b50 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  */.#endif../*.**
89b60 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 70   The following p
89b70 61 69 72 20 6f 66 20 72 6f 75 74 69 6e 65 20 69  air of routine i
89b80 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 75 61 6c 20  mplement mutual 
89b90 65 78 63 6c 75 73 69 6f 6e 20 66 6f 72 0a 2a 2a  exclusion for.**
89ba0 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20   multi-threaded 
89bb0 70 72 6f 63 65 73 73 65 73 2e 20 20 4f 6e 6c 79  processes.  Only
89bc0 20 61 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64   a single thread
89bd0 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f 0a 2a   is allowed to.*
89be0 2a 20 65 78 65 63 75 74 65 64 20 63 6f 64 65 20  * executed code 
89bf0 74 68 61 74 20 69 73 20 73 75 72 72 6f 75 6e 64  that is surround
89c00 65 64 20 62 79 20 45 6e 74 65 72 4d 75 74 65 78  ed by EnterMutex
89c10 28 29 20 61 6e 64 20 4c 65 61 76 65 4d 75 74 65  () and LeaveMute
89c20 78 28 29 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  x()..**.** SQLit
89c30 65 20 75 73 65 73 20 6f 6e 6c 79 20 61 20 73 69  e uses only a si
89c40 6e 67 6c 65 20 4d 75 74 65 78 2e 20 20 54 68 65  ngle Mutex.  The
89c50 72 65 20 69 73 20 6e 6f 74 20 6d 75 63 68 20 63  re is not much c
89c60 72 69 74 69 63 61 6c 0a 2a 2a 20 63 6f 64 65 20  ritical.** code 
89c70 61 6e 64 20 77 68 61 74 20 6c 69 74 74 6c 65 20  and what little 
89c80 74 68 65 72 65 20 69 73 20 65 78 65 63 75 74 65  there is execute
89c90 73 20 71 75 69 63 6b 6c 79 20 61 6e 64 20 77 69  s quickly and wi
89ca0 74 68 6f 75 74 20 62 6c 6f 63 6b 69 6e 67 2e 0a  thout blocking..
89cb0 2a 2a 0a 2a 2a 20 41 73 20 6f 66 20 76 65 72 73  **.** As of vers
89cc0 69 6f 6e 20 33 2e 33 2e 32 2c 20 74 68 69 73 20  ion 3.3.2, this 
89cd0 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 72 65  mutex must be re
89ce0 63 75 72 73 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49  cursive..*/.SQLI
89cf0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
89d00 73 71 6c 69 74 65 33 55 6e 69 78 45 6e 74 65 72  sqlite3UnixEnter
89d10 4d 75 74 65 78 28 29 7b 0a 23 69 66 64 65 66 20  Mutex(){.#ifdef 
89d20 53 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48 52 45  SQLITE_UNIX_THRE
89d30 41 44 53 0a 20 20 70 74 68 72 65 61 64 5f 6d 75  ADS.  pthread_mu
89d40 74 65 78 5f 6c 6f 63 6b 28 26 6d 75 74 65 78 41  tex_lock(&mutexA
89d50 75 78 29 3b 0a 20 20 69 66 28 20 21 6d 75 74 65  ux);.  if( !mute
89d60 78 4f 77 6e 65 72 56 61 6c 69 64 20 7c 7c 20 21  xOwnerValid || !
89d70 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 6d 75  pthread_equal(mu
89d80 74 65 78 4f 77 6e 65 72 2c 20 70 74 68 72 65 61  texOwner, pthrea
89d90 64 5f 73 65 6c 66 28 29 29 20 29 7b 0a 20 20 20  d_self()) ){.   
89da0 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 75   pthread_mutex_u
89db0 6e 6c 6f 63 6b 28 26 6d 75 74 65 78 41 75 78 29  nlock(&mutexAux)
89dc0 3b 0a 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75  ;.    pthread_mu
89dd0 74 65 78 5f 6c 6f 63 6b 28 26 6d 75 74 65 78 4d  tex_lock(&mutexM
89de0 61 69 6e 29 3b 0a 20 20 20 20 61 73 73 65 72 74  ain);.    assert
89df0 28 20 69 6e 4d 75 74 65 78 3d 3d 30 20 29 3b 0a  ( inMutex==0 );.
89e00 20 20 20 20 61 73 73 65 72 74 28 20 21 6d 75 74      assert( !mut
89e10 65 78 4f 77 6e 65 72 56 61 6c 69 64 20 29 3b 0a  exOwnerValid );.
89e20 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65      pthread_mute
89e30 78 5f 6c 6f 63 6b 28 26 6d 75 74 65 78 41 75 78  x_lock(&mutexAux
89e40 29 3b 0a 20 20 20 20 6d 75 74 65 78 4f 77 6e 65  );.    mutexOwne
89e50 72 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66  r = pthread_self
89e60 28 29 3b 0a 20 20 20 20 6d 75 74 65 78 4f 77 6e  ();.    mutexOwn
89e70 65 72 56 61 6c 69 64 20 3d 20 31 3b 0a 20 20 7d  erValid = 1;.  }
89e80 0a 20 20 69 6e 4d 75 74 65 78 2b 2b 3b 0a 20 20  .  inMutex++;.  
89e90 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e  pthread_mutex_un
89ea0 6c 6f 63 6b 28 26 6d 75 74 65 78 41 75 78 29 3b  lock(&mutexAux);
89eb0 0a 23 65 6c 73 65 0a 20 20 69 6e 4d 75 74 65 78  .#else.  inMutex
89ec0 2b 2b 3b 0a 23 65 6e 64 69 66 0a 7d 0a 53 51 4c  ++;.#endif.}.SQL
89ed0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
89ee0 20 73 71 6c 69 74 65 33 55 6e 69 78 4c 65 61 76   sqlite3UnixLeav
89ef0 65 4d 75 74 65 78 28 29 7b 0a 20 20 61 73 73 65  eMutex(){.  asse
89f00 72 74 28 20 69 6e 4d 75 74 65 78 3e 30 20 29 3b  rt( inMutex>0 );
89f10 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 55  .#ifdef SQLITE_U
89f20 4e 49 58 5f 54 48 52 45 41 44 53 0a 20 20 70 74  NIX_THREADS.  pt
89f30 68 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b  hread_mutex_lock
89f40 28 26 6d 75 74 65 78 41 75 78 29 3b 0a 20 20 69  (&mutexAux);.  i
89f50 6e 4d 75 74 65 78 2d 2d 3b 0a 20 20 61 73 73 65  nMutex--;.  asse
89f60 72 74 28 20 70 74 68 72 65 61 64 5f 65 71 75 61  rt( pthread_equa
89f70 6c 28 6d 75 74 65 78 4f 77 6e 65 72 2c 20 70 74  l(mutexOwner, pt
89f80 68 72 65 61 64 5f 73 65 6c 66 28 29 29 20 29 3b  hread_self()) );
89f90 0a 20 20 69 66 28 20 69 6e 4d 75 74 65 78 3d 3d  .  if( inMutex==
89fa0 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  0 ){.    assert(
89fb0 20 6d 75 74 65 78 4f 77 6e 65 72 56 61 6c 69 64   mutexOwnerValid
89fc0 20 29 3b 0a 20 20 20 20 6d 75 74 65 78 4f 77 6e   );.    mutexOwn
89fd0 65 72 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20  erValid = 0;.   
89fe0 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 75   pthread_mutex_u
89ff0 6e 6c 6f 63 6b 28 26 6d 75 74 65 78 4d 61 69 6e  nlock(&mutexMain
8a000 29 3b 0a 20 20 7d 0a 20 20 70 74 68 72 65 61 64  );.  }.  pthread
8a010 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 6d  _mutex_unlock(&m
8a020 75 74 65 78 41 75 78 29 3b 0a 23 65 6c 73 65 0a  utexAux);.#else.
8a030 20 20 69 6e 4d 75 74 65 78 2d 2d 3b 0a 23 65 6e    inMutex--;.#en
8a040 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  dif.}../*.** Ret
8a050 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20  urn TRUE if the 
8a060 6d 75 74 65 78 20 69 73 20 63 75 72 72 65 6e 74  mutex is current
8a070 6c 79 20 68 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 49  ly held..**.** I
8a080 66 20 74 68 65 20 74 68 69 73 54 68 72 64 20 70  f the thisThrd p
8a090 61 72 61 6d 65 74 65 72 20 69 73 20 74 72 75 65  arameter is true
8a0a0 2c 20 72 65 74 75 72 6e 20 74 72 75 65 20 6f 6e  , return true on
8a0b0 6c 79 20 69 66 20 74 68 65 0a 2a 2a 20 63 61 6c  ly if the.** cal
8a0c0 6c 69 6e 67 20 74 68 72 65 61 64 20 68 6f 6c 64  ling thread hold
8a0d0 73 20 74 68 65 20 6d 75 74 65 78 2e 20 20 49 66  s the mutex.  If
8a0e0 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 69   the parameter i
8a0f0 73 20 66 61 6c 73 65 2c 20 72 65 74 75 72 6e 0a  s false, return.
8a100 2a 2a 20 74 72 75 65 20 69 66 20 61 6e 79 20 74  ** true if any t
8a110 68 72 65 61 64 20 68 6f 6c 64 73 20 74 68 65 20  hread holds the 
8a120 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45  mutex..*/.SQLITE
8a130 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
8a140 69 74 65 33 55 6e 69 78 49 6e 4d 75 74 65 78 28  ite3UnixInMutex(
8a150 69 6e 74 20 74 68 69 73 54 68 72 64 29 7b 0a 23  int thisThrd){.#
8a160 69 66 64 65 66 20 53 51 4c 49 54 45 5f 55 4e 49  ifdef SQLITE_UNI
8a170 58 5f 54 48 52 45 41 44 53 0a 20 20 69 6e 74 20  X_THREADS.  int 
8a180 72 63 3b 0a 20 20 70 74 68 72 65 61 64 5f 6d 75  rc;.  pthread_mu
8a190 74 65 78 5f 6c 6f 63 6b 28 26 6d 75 74 65 78 41  tex_lock(&mutexA
8a1a0 75 78 29 3b 0a 20 20 72 63 20 3d 20 69 6e 4d 75  ux);.  rc = inMu
8a1b0 74 65 78 3e 30 20 26 26 20 28 74 68 69 73 54 68  tex>0 && (thisTh
8a1c0 72 64 3d 3d 30 20 7c 7c 20 70 74 68 72 65 61 64  rd==0 || pthread
8a1d0 5f 65 71 75 61 6c 28 6d 75 74 65 78 4f 77 6e 65  _equal(mutexOwne
8a1e0 72 2c 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29  r,pthread_self()
8a1f0 29 29 3b 0a 20 20 70 74 68 72 65 61 64 5f 6d 75  ));.  pthread_mu
8a200 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 6d 75 74 65  tex_unlock(&mute
8a210 78 41 75 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  xAux);.  return 
8a220 72 63 3b 0a 23 65 6c 73 65 0a 20 20 72 65 74 75  rc;.#else.  retu
8a230 72 6e 20 69 6e 4d 75 74 65 78 3e 30 3b 0a 23 65  rn inMutex>0;.#e
8a240 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  ndif.}../*.** Re
8a250 6d 65 6d 62 65 72 20 74 68 65 20 6e 75 6d 62 65  member the numbe
8a260 72 20 6f 66 20 74 68 72 65 61 64 2d 73 70 65 63  r of thread-spec
8a270 69 66 69 63 2d 64 61 74 61 20 62 6c 6f 63 6b 73  ific-data blocks
8a280 20 61 6c 6c 6f 63 61 74 65 64 2e 0a 2a 2a 20 55   allocated..** U
8a290 73 65 20 74 68 69 73 20 74 6f 20 76 65 72 69 66  se this to verif
8a2a0 79 20 74 68 61 74 20 77 65 20 61 72 65 20 6e 6f  y that we are no
8a2b0 74 20 6c 65 61 6b 69 6e 67 20 74 68 72 65 61 64  t leaking thread
8a2c0 2d 73 70 65 63 69 66 69 63 2d 64 61 74 61 2e 0a  -specific-data..
8a2d0 2a 2a 20 54 69 63 6b 65 74 20 23 31 36 30 31 0a  ** Ticket #1601.
8a2e0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
8a2f0 5f 54 45 53 54 0a 69 6e 74 20 73 71 6c 69 74 65  _TEST.int sqlite
8a300 33 5f 74 73 64 5f 63 6f 75 6e 74 20 3d 20 30 3b  3_tsd_count = 0;
8a310 0a 23 20 69 66 64 65 66 20 53 51 4c 49 54 45 5f  .# ifdef SQLITE_
8a320 55 4e 49 58 5f 54 48 52 45 41 44 53 0a 20 20 20  UNIX_THREADS.   
8a330 20 73 74 61 74 69 63 20 70 74 68 72 65 61 64 5f   static pthread_
8a340 6d 75 74 65 78 5f 74 20 74 73 64 5f 63 6f 75 6e  mutex_t tsd_coun
8a350 74 65 72 5f 6d 75 74 65 78 20 3d 20 50 54 48 52  ter_mutex = PTHR
8a360 45 41 44 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41  EAD_MUTEX_INITIA
8a370 4c 49 5a 45 52 3b 0a 23 20 20 20 64 65 66 69 6e  LIZER;.#   defin
8a380 65 20 54 53 44 5f 43 4f 55 4e 54 45 52 28 4e 29  e TSD_COUNTER(N)
8a390 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   \.             
8a3a0 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f  pthread_mutex_lo
8a3b0 63 6b 28 26 74 73 64 5f 63 6f 75 6e 74 65 72 5f  ck(&tsd_counter_
8a3c0 6d 75 74 65 78 29 3b 20 5c 0a 20 20 20 20 20 20  mutex); \.      
8a3d0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 74         sqlite3_t
8a3e0 73 64 5f 63 6f 75 6e 74 20 2b 3d 20 4e 3b 20 5c  sd_count += N; \
8a3f0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 70 74  .             pt
8a400 68 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f  hread_mutex_unlo
8a410 63 6b 28 26 74 73 64 5f 63 6f 75 6e 74 65 72 5f  ck(&tsd_counter_
8a420 6d 75 74 65 78 29 3b 0a 23 20 65 6c 73 65 0a 23  mutex);.# else.#
8a430 20 20 20 64 65 66 69 6e 65 20 54 53 44 5f 43 4f     define TSD_CO
8a440 55 4e 54 45 52 28 4e 29 20 20 73 71 6c 69 74 65  UNTER(N)  sqlite
8a450 33 5f 74 73 64 5f 63 6f 75 6e 74 20 2b 3d 20 4e  3_tsd_count += N
8a460 0a 23 20 65 6e 64 69 66 0a 23 65 6c 73 65 0a 23  .# endif.#else.#
8a470 20 64 65 66 69 6e 65 20 54 53 44 5f 43 4f 55 4e   define TSD_COUN
8a480 54 45 52 28 4e 29 20 20 2f 2a 20 6e 6f 2d 6f 70  TER(N)  /* no-op
8a490 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a   */.#endif../*.*
8a4a0 2a 20 49 66 20 63 61 6c 6c 65 64 20 77 69 74 68  * If called with
8a4b0 20 61 6c 6c 6f 63 61 74 65 46 6c 61 67 3e 30 2c   allocateFlag>0,
8a4c0 20 74 68 65 6e 20 72 65 74 75 72 6e 20 61 20 70   then return a p
8a4d0 6f 69 6e 74 65 72 20 74 6f 20 74 68 72 65 61 64  ointer to thread
8a4e0 0a 2a 2a 20 73 70 65 63 69 66 69 63 20 64 61 74  .** specific dat
8a4f0 61 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e  a for the curren
8a500 74 20 74 68 72 65 61 64 2e 20 20 41 6c 6c 6f 63  t thread.  Alloc
8a510 61 74 65 20 61 6e 64 20 7a 65 72 6f 20 74 68 65  ate and zero the
8a520 0a 2a 2a 20 74 68 72 65 61 64 2d 73 70 65 63 69  .** thread-speci
8a530 66 69 63 20 64 61 74 61 20 69 66 20 69 74 20 64  fic data if it d
8a540 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20  oes not already 
8a550 65 78 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  exist..**.** If 
8a560 63 61 6c 6c 65 64 20 77 69 74 68 20 61 6c 6c 6f  called with allo
8a570 63 61 74 65 46 6c 61 67 3d 3d 30 2c 20 74 68 65  cateFlag==0, the
8a580 6e 20 63 68 65 63 6b 20 74 68 65 20 63 75 72 72  n check the curr
8a590 65 6e 74 20 74 68 72 65 61 64 0a 2a 2a 20 73 70  ent thread.** sp
8a5a0 65 63 69 66 69 63 20 64 61 74 61 2e 20 20 52 65  ecific data.  Re
8a5b0 74 75 72 6e 20 69 74 20 69 66 20 69 74 20 65 78  turn it if it ex
8a5c0 69 73 74 73 2e 20 20 49 66 20 69 74 20 64 6f 65  ists.  If it doe
8a5d0 73 20 6e 6f 74 20 65 78 69 73 74 2c 0a 2a 2a 20  s not exist,.** 
8a5e0 74 68 65 6e 20 72 65 74 75 72 6e 20 4e 55 4c 4c  then return NULL
8a5f0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 63 61 6c 6c 65  ..**.** If calle
8a600 64 20 77 69 74 68 20 61 6c 6c 6f 63 61 74 65 46  d with allocateF
8a610 6c 61 67 3c 30 2c 20 63 68 65 63 6b 20 74 6f 20  lag<0, check to 
8a620 73 65 65 20 69 66 20 74 68 65 20 74 68 72 65 61  see if the threa
8a630 64 20 73 70 65 63 69 66 69 63 0a 2a 2a 20 64 61  d specific.** da
8a640 74 61 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20  ta is allocated 
8a650 61 6e 64 20 69 73 20 61 6c 6c 20 7a 65 72 6f 2e  and is all zero.
8a660 20 20 49 66 20 69 74 20 69 73 20 74 68 65 6e 20    If it is then 
8a670 64 65 61 6c 6c 6f 63 61 74 65 20 69 74 2e 0a 2a  deallocate it..*
8a680 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  * Return a point
8a690 65 72 20 74 6f 20 74 68 65 20 74 68 72 65 61 64  er to the thread
8a6a0 20 73 70 65 63 69 66 69 63 20 64 61 74 61 20 6f   specific data o
8a6b0 72 20 4e 55 4c 4c 20 69 66 20 69 74 20 69 73 0a  r NULL if it is.
8a6c0 2a 2a 20 75 6e 61 6c 6c 6f 63 61 74 65 64 20 6f  ** unallocated o
8a6d0 72 20 67 65 74 73 20 64 65 61 6c 6c 6f 63 61 74  r gets deallocat
8a6e0 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
8a6f0 49 56 41 54 45 20 54 68 72 65 61 64 44 61 74 61  IVATE ThreadData
8a700 20 2a 73 71 6c 69 74 65 33 55 6e 69 78 54 68 72   *sqlite3UnixThr
8a710 65 61 64 53 70 65 63 69 66 69 63 44 61 74 61 28  eadSpecificData(
8a720 69 6e 74 20 61 6c 6c 6f 63 61 74 65 46 6c 61 67  int allocateFlag
8a730 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  ){.  static cons
8a740 74 20 54 68 72 65 61 64 44 61 74 61 20 7a 65 72  t ThreadData zer
8a750 6f 44 61 74 61 20 3d 20 7b 30 7d 3b 20 20 2f 2a  oData = {0};  /*
8a760 20 49 6e 69 74 69 61 6c 69 7a 65 72 20 74 6f 20   Initializer to 
8a770 73 69 6c 65 6e 63 65 20 77 61 72 6e 69 6e 67 73  silence warnings
8a780 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
8a790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8a7a0 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66              ** f
8a7b0 72 6f 6d 20 62 72 6f 6b 65 6e 20 63 6f 6d 70 69  rom broken compi
8a7c0 6c 65 72 73 20 2a 2f 0a 23 69 66 64 65 66 20 53  lers */.#ifdef S
8a7d0 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48 52 45 41  QLITE_UNIX_THREA
8a7e0 44 53 0a 20 20 73 74 61 74 69 63 20 70 74 68 72  DS.  static pthr
8a7f0 65 61 64 5f 6b 65 79 5f 74 20 6b 65 79 3b 0a 20  ead_key_t key;. 
8a800 20 73 74 61 74 69 63 20 69 6e 74 20 6b 65 79 49   static int keyI
8a810 6e 69 74 20 3d 20 30 3b 0a 20 20 54 68 72 65 61  nit = 0;.  Threa
8a820 64 44 61 74 61 20 2a 70 54 73 64 3b 0a 0a 20 20  dData *pTsd;..  
8a830 69 66 28 20 21 6b 65 79 49 6e 69 74 20 29 7b 0a  if( !keyInit ){.
8a840 20 20 20 20 73 71 6c 69 74 65 33 4f 73 45 6e 74      sqlite3OsEnt
8a850 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 69  erMutex();.    i
8a860 66 28 20 21 6b 65 79 49 6e 69 74 20 29 7b 0a 20  f( !keyInit ){. 
8a870 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20       int rc;.   
8a880 20 20 20 72 63 20 3d 20 70 74 68 72 65 61 64 5f     rc = pthread_
8a890 6b 65 79 5f 63 72 65 61 74 65 28 26 6b 65 79 2c  key_create(&key,
8a8a0 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72   0);.      if( r
8a8b0 63 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  c ){.        sql
8a8c0 69 74 65 33 4f 73 4c 65 61 76 65 4d 75 74 65 78  ite3OsLeaveMutex
8a8d0 28 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  ();.        retu
8a8e0 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 0;.      }.  
8a8f0 20 20 20 20 6b 65 79 49 6e 69 74 20 3d 20 31 3b      keyInit = 1;
8a900 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
8a910 65 33 4f 73 4c 65 61 76 65 4d 75 74 65 78 28 29  e3OsLeaveMutex()
8a920 3b 0a 20 20 7d 0a 0a 20 20 70 54 73 64 20 3d 20  ;.  }..  pTsd = 
8a930 70 74 68 72 65 61 64 5f 67 65 74 73 70 65 63 69  pthread_getspeci
8a940 66 69 63 28 6b 65 79 29 3b 0a 20 20 69 66 28 20  fic(key);.  if( 
8a950 61 6c 6c 6f 63 61 74 65 46 6c 61 67 3e 30 20 29  allocateFlag>0 )
8a960 7b 0a 20 20 20 20 69 66 28 20 70 54 73 64 3d 3d  {.    if( pTsd==
8a970 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21  0 ){.      if( !
8a980 73 71 6c 69 74 65 33 54 65 73 74 4d 61 6c 6c 6f  sqlite3TestMallo
8a990 63 46 61 69 6c 28 29 20 29 7b 0a 20 20 20 20 20  cFail() ){.     
8a9a0 20 20 20 70 54 73 64 20 3d 20 73 71 6c 69 74 65     pTsd = sqlite
8a9b0 33 4f 73 4d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66  3OsMalloc(sizeof
8a9c0 28 7a 65 72 6f 44 61 74 61 29 29 3b 0a 20 20 20  (zeroData));.   
8a9d0 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49     }.#ifdef SQLI
8a9e0 54 45 5f 4d 45 4d 44 45 42 55 47 0a 20 20 20 20  TE_MEMDEBUG.    
8a9f0 20 20 73 71 6c 69 74 65 33 5f 69 73 46 61 69 6c    sqlite3_isFail
8aa00 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 20   = 0;.#endif.   
8aa10 20 20 20 69 66 28 20 70 54 73 64 20 29 7b 0a 20     if( pTsd ){. 
8aa20 20 20 20 20 20 20 20 2a 70 54 73 64 20 3d 20 7a         *pTsd = z
8aa30 65 72 6f 44 61 74 61 3b 0a 20 20 20 20 20 20 20  eroData;.       
8aa40 20 70 74 68 72 65 61 64 5f 73 65 74 73 70 65 63   pthread_setspec
8aa50 69 66 69 63 28 6b 65 79 2c 20 70 54 73 64 29 3b  ific(key, pTsd);
8aa60 0a 20 20 20 20 20 20 20 20 54 53 44 5f 43 4f 55  .        TSD_COU
8aa70 4e 54 45 52 28 2b 31 29 3b 0a 20 20 20 20 20 20  NTER(+1);.      
8aa80 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20  }.    }.  }else 
8aa90 69 66 28 20 70 54 73 64 21 3d 30 20 26 26 20 61  if( pTsd!=0 && a
8aaa0 6c 6c 6f 63 61 74 65 46 6c 61 67 3c 30 20 0a 20  llocateFlag<0 . 
8aab0 20 20 20 20 20 20 20 20 20 20 20 26 26 20 6d 65             && me
8aac0 6d 63 6d 70 28 70 54 73 64 2c 20 26 7a 65 72 6f  mcmp(pTsd, &zero
8aad0 44 61 74 61 2c 20 73 69 7a 65 6f 66 28 54 68 72  Data, sizeof(Thr
8aae0 65 61 64 44 61 74 61 29 29 3d 3d 30 20 29 7b 0a  eadData))==0 ){.
8aaf0 20 20 20 20 73 71 6c 69 74 65 33 4f 73 46 72 65      sqlite3OsFre
8ab00 65 28 70 54 73 64 29 3b 0a 20 20 20 20 70 74 68  e(pTsd);.    pth
8ab10 72 65 61 64 5f 73 65 74 73 70 65 63 69 66 69 63  read_setspecific
8ab20 28 6b 65 79 2c 20 30 29 3b 0a 20 20 20 20 54 53  (key, 0);.    TS
8ab30 44 5f 43 4f 55 4e 54 45 52 28 2d 31 29 3b 0a 20  D_COUNTER(-1);. 
8ab40 20 20 20 70 54 73 64 20 3d 20 30 3b 0a 20 20 7d     pTsd = 0;.  }
8ab50 0a 20 20 72 65 74 75 72 6e 20 70 54 73 64 3b 0a  .  return pTsd;.
8ab60 23 65 6c 73 65 0a 20 20 73 74 61 74 69 63 20 54  #else.  static T
8ab70 68 72 65 61 64 44 61 74 61 20 2a 70 54 73 64 20  hreadData *pTsd 
8ab80 3d 20 30 3b 0a 20 20 69 66 28 20 61 6c 6c 6f 63  = 0;.  if( alloc
8ab90 61 74 65 46 6c 61 67 3e 30 20 29 7b 0a 20 20 20  ateFlag>0 ){.   
8aba0 20 69 66 28 20 70 54 73 64 3d 3d 30 20 29 7b 0a   if( pTsd==0 ){.
8abb0 20 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 74        if( !sqlit
8abc0 65 33 54 65 73 74 4d 61 6c 6c 6f 63 46 61 69 6c  e3TestMallocFail
8abd0 28 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54  () ){.        pT
8abe0 73 64 20 3d 20 73 71 6c 69 74 65 33 4f 73 4d 61  sd = sqlite3OsMa
8abf0 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 7a 65 72  lloc( sizeof(zer
8ac00 6f 44 61 74 61 29 20 29 3b 0a 20 20 20 20 20 20  oData) );.      
8ac10 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  }.#ifdef SQLITE_
8ac20 4d 45 4d 44 45 42 55 47 0a 20 20 20 20 20 20 73  MEMDEBUG.      s
8ac30 71 6c 69 74 65 33 5f 69 73 46 61 69 6c 20 3d 20  qlite3_isFail = 
8ac40 30 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20  0;.#endif.      
8ac50 69 66 28 20 70 54 73 64 20 29 7b 0a 20 20 20 20  if( pTsd ){.    
8ac60 20 20 20 20 2a 70 54 73 64 20 3d 20 7a 65 72 6f      *pTsd = zero
8ac70 44 61 74 61 3b 0a 20 20 20 20 20 20 20 20 54 53  Data;.        TS
8ac80 44 5f 43 4f 55 4e 54 45 52 28 2b 31 29 3b 0a 20  D_COUNTER(+1);. 
8ac90 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
8aca0 65 6c 73 65 20 69 66 28 20 70 54 73 64 21 3d 30  else if( pTsd!=0
8acb0 20 26 26 20 61 6c 6c 6f 63 61 74 65 46 6c 61 67   && allocateFlag
8acc0 3c 30 0a 20 20 20 20 20 20 20 20 20 20 20 20 26  <0.            &
8acd0 26 20 6d 65 6d 63 6d 70 28 70 54 73 64 2c 20 26  & memcmp(pTsd, &
8ace0 7a 65 72 6f 44 61 74 61 2c 20 73 69 7a 65 6f 66  zeroData, sizeof
8acf0 28 54 68 72 65 61 64 44 61 74 61 29 29 3d 3d 30  (ThreadData))==0
8ad00 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f   ){.    sqlite3O
8ad10 73 46 72 65 65 28 70 54 73 64 29 3b 0a 20 20 20  sFree(pTsd);.   
8ad20 20 54 53 44 5f 43 4f 55 4e 54 45 52 28 2d 31 29   TSD_COUNTER(-1)
8ad30 3b 0a 20 20 20 20 70 54 73 64 20 3d 20 30 3b 0a  ;.    pTsd = 0;.
8ad40 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 54 73    }.  return pTs
8ad50 64 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a  d;.#endif.}../*.
8ad60 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
8ad70 20 76 61 72 69 61 62 6c 65 2c 20 69 66 20 73 65   variable, if se
8ad80 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20  t to a non-zero 
8ad90 76 61 6c 75 65 2c 20 62 65 63 6f 6d 65 73 20 74  value, becomes t
8ada0 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 72 65 74  he result.** ret
8adb0 75 72 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74  urned from sqlit
8adc0 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28  e3OsCurrentTime(
8add0 29 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64  ).  This is used
8ade0 20 66 6f 72 20 74 65 73 74 69 6e 67 2e 0a 2a 2f   for testing..*/
8adf0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
8ae00 45 53 54 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f  EST.int sqlite3_
8ae10 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30  current_time = 0
8ae20 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ;.#endif../*.** 
8ae30 46 69 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74  Find the current
8ae40 20 74 69 6d 65 20 28 69 6e 20 55 6e 69 76 65 72   time (in Univer
8ae50 73 61 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64 20  sal Coordinated 
8ae60 54 69 6d 65 29 2e 20 20 57 72 69 74 65 20 74 68  Time).  Write th
8ae70 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 74 69 6d  e.** current tim
8ae80 65 20 61 6e 64 20 64 61 74 65 20 61 73 20 61 20  e and date as a 
8ae90 4a 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65  Julian Day numbe
8aea0 72 20 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61 6e  r into *prNow an
8aeb0 64 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20 20  d.** return 0.  
8aec0 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20  Return 1 if the 
8aed0 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 63 61  time and date ca
8aee0 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a  nnot be found..*
8aef0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
8af00 20 69 6e 74 20 73 71 6c 69 74 65 33 55 6e 69 78   int sqlite3Unix
8af10 43 75 72 72 65 6e 74 54 69 6d 65 28 64 6f 75 62  CurrentTime(doub
8af20 6c 65 20 2a 70 72 4e 6f 77 29 7b 0a 23 69 66 64  le *prNow){.#ifd
8af30 65 66 20 4e 4f 5f 47 45 54 54 4f 44 0a 20 20 74  ef NO_GETTOD.  t
8af40 69 6d 65 5f 74 20 74 3b 0a 20 20 74 69 6d 65 28  ime_t t;.  time(
8af50 26 74 29 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20  &t);.  *prNow = 
8af60 74 2f 38 36 34 30 30 2e 30 20 2b 20 32 34 34 30  t/86400.0 + 2440
8af70 35 38 37 2e 35 3b 0a 23 65 6c 73 65 0a 20 20 73  587.5;.#else.  s
8af80 74 72 75 63 74 20 74 69 6d 65 76 61 6c 20 73 4e  truct timeval sN
8af90 6f 77 3b 0a 20 20 67 65 74 74 69 6d 65 6f 66 64  ow;.  gettimeofd
8afa0 61 79 28 26 73 4e 6f 77 2c 20 30 29 3b 0a 20 20  ay(&sNow, 0);.  
8afb0 2a 70 72 4e 6f 77 20 3d 20 32 34 34 30 35 38 37  *prNow = 2440587
8afc0 2e 35 20 2b 20 73 4e 6f 77 2e 74 76 5f 73 65 63  .5 + sNow.tv_sec
8afd0 2f 38 36 34 30 30 2e 30 20 2b 20 73 4e 6f 77 2e  /86400.0 + sNow.
8afe0 74 76 5f 75 73 65 63 2f 38 36 34 30 30 30 30 30  tv_usec/86400000
8aff0 30 30 30 2e 30 3b 0a 23 65 6e 64 69 66 0a 23 69  000.0;.#endif.#i
8b000 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
8b010 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63  .  if( sqlite3_c
8b020 75 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0a 20  urrent_time ){. 
8b030 20 20 20 2a 70 72 4e 6f 77 20 3d 20 73 71 6c 69     *prNow = sqli
8b040 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65  te3_current_time
8b050 2f 38 36 34 30 30 2e 30 20 2b 20 32 34 34 30 35  /86400.0 + 24405
8b060 38 37 2e 35 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  87.5;.  }.#endif
8b070 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
8b080 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 55 4e 49  #endif /* OS_UNI
8b090 58 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  X */../*********
8b0a0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f  ***** End of os_
8b0b0 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  unix.c *********
8b0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8b0d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8b0e0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
8b0f0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
8b100 20 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a   os_win.c ******
8b110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8b120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8b130 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34  ****/./*.** 2004
8b140 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68   May 22.**.** Th
8b150 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
8b160 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
8b170 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
8b180 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
8b190 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
8b1a0 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
8b1b0 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
8b1c0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
8b1d0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
8b1e0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
8b1f0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
8b200 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
8b210 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
8b220 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
8b230 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
8b240 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
8b250 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
8b260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8b270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8b280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8b290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8b2a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
8b2b0 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
8b2c0 61 69 6e 73 20 63 6f 64 65 20 74 68 61 74 20 69  ains code that i
8b2d0 73 20 73 70 65 63 69 66 69 63 20 74 6f 20 77 69  s specific to wi
8b2e0 6e 64 6f 77 73 2e 0a 2a 2f 0a 23 69 66 20 4f 53  ndows..*/.#if OS
8b2f0 5f 57 49 4e 20 20 20 20 20 20 20 20 20 20 20 20  _WIN            
8b300 20 20 20 2f 2a 20 54 68 69 73 20 66 69 6c 65 20     /* This file 
8b310 69 73 20 75 73 65 64 20 66 6f 72 20 77 69 6e 64  is used for wind
8b320 6f 77 73 20 6f 6e 6c 79 20 2a 2f 0a 0a 23 69 6e  ows only */..#in
8b330 63 6c 75 64 65 20 3c 77 69 6e 62 61 73 65 2e 68  clude <winbase.h
8b340 3e 0a 0a 23 69 66 64 65 66 20 5f 5f 43 59 47 57  >..#ifdef __CYGW
8b350 49 4e 5f 5f 0a 23 20 69 6e 63 6c 75 64 65 20 3c  IN__.# include <
8b360 73 79 73 2f 63 79 67 77 69 6e 2e 68 3e 0a 23 65  sys/cygwin.h>.#e
8b370 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72  ndif../*.** Macr
8b380 6f 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72  os used to deter
8b390 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20  mine whether or 
8b3a0 6e 6f 74 20 74 6f 20 75 73 65 20 74 68 72 65 61  not to use threa
8b3b0 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e  ds..*/.#if defin
8b3c0 65 64 28 54 48 52 45 41 44 53 41 46 45 29 20 26  ed(THREADSAFE) &
8b3d0 26 20 54 48 52 45 41 44 53 41 46 45 0a 23 20 64  & THREADSAFE.# d
8b3e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 33 32  efine SQLITE_W32
8b3f0 5f 54 48 52 45 41 44 53 20 31 0a 23 65 6e 64 69  _THREADS 1.#endi
8b400 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65  f../*.** Include
8b410 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f   code that is co
8b420 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73 5f 2a  mmon to all os_*
8b430 2e 63 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a  .c files.*/./***
8b440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
8b450 75 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20  ude os_common.h 
8b460 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
8b470 20 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a   os_win.c ******
8b480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
8b490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
8b4a0 6e 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d 6f 6e  n file os_common
8b4b0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
8b4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8b4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
8b4e0 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a 2a 2a  * 2004 May 22.**
8b4f0 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
8b500 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
8b510 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
8b520 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
8b530 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
8b540 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
8b550 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
8b560 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
8b570 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
8b580 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
8b590 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
8b5a0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
8b5b0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
8b5c0 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
8b5d0 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
8b5e0 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
8b5f0 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
8b600 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
8b610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8b620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8b630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8b640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8b650 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  *.**.** This fil
8b660 65 20 63 6f 6e 74 61 69 6e 73 20 6d 61 63 72 6f  e contains macro
8b670 73 20 61 6e 64 20 61 20 6c 69 74 74 6c 65 20 62  s and a little b
8b680 69 74 20 6f 66 20 63 6f 64 65 20 74 68 61 74 20  it of code that 
8b690 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20  is common to.** 
8b6a0 61 6c 6c 20 6f 66 20 74 68 65 20 70 6c 61 74 66  all of the platf
8b6b0 6f 72 6d 2d 73 70 65 63 69 66 69 63 20 66 69 6c  orm-specific fil
8b6c0 65 73 20 28 6f 73 5f 2a 2e 63 29 20 61 6e 64 20  es (os_*.c) and 
8b6d0 69 73 20 23 69 6e 63 6c 75 64 65 64 20 69 6e 74  is #included int
8b6e0 6f 20 74 68 6f 73 65 0a 2a 2a 20 66 69 6c 65 73  o those.** files
8b6f0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ..**.** This fil
8b700 65 20 73 68 6f 75 6c 64 20 62 65 20 23 69 6e 63  e should be #inc
8b710 6c 75 64 65 64 20 62 79 20 74 68 65 20 6f 73 5f  luded by the os_
8b720 2a 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79 2e 20  *.c files only. 
8b730 20 49 74 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20   It is not a.** 
8b740 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 65 20  general purpose 
8b750 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2f 0a  header file..*/.
8b760 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 74 20  ./*.** At least 
8b770 74 77 6f 20 62 75 67 73 20 68 61 76 65 20 73 6c  two bugs have sl
8b780 69 70 70 65 64 20 69 6e 20 62 65 63 61 75 73 65  ipped in because
8b790 20 77 65 20 63 68 61 6e 67 65 64 20 74 68 65 20   we changed the 
8b7a0 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a 2a 20  MEMORY_DEBUG.** 
8b7b0 6d 61 63 72 6f 20 74 6f 20 53 51 4c 49 54 45 5f  macro to SQLITE_
8b7c0 44 45 42 55 47 20 61 6e 64 20 73 6f 6d 65 20 6f  DEBUG and some o
8b7d0 6c 64 65 72 20 6d 61 6b 65 66 69 6c 65 73 20 68  lder makefiles h
8b7e0 61 76 65 20 6e 6f 74 20 79 65 74 20 6d 61 64 65  ave not yet made
8b7f0 20 74 68 65 0a 2a 2a 20 73 77 69 74 63 68 2e 20   the.** switch. 
8b800 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   The following c
8b810 6f 64 65 20 73 68 6f 75 6c 64 20 63 61 74 63 68  ode should catch
8b820 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 61 74   this problem at
8b830 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a 2a   compile-time..*
8b840 2f 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52 59 5f  /.#ifdef MEMORY_
8b850 44 45 42 55 47 0a 23 20 65 72 72 6f 72 20 22 54  DEBUG.# error "T
8b860 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 20  he MEMORY_DEBUG 
8b870 6d 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c 65 74  macro is obsolet
8b880 65 2e 20 20 55 73 65 20 53 51 4c 49 54 45 5f 44  e.  Use SQLITE_D
8b890 45 42 55 47 20 69 6e 73 74 65 61 64 2e 22 0a 23  EBUG instead.".#
8b8a0 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 20 2a 20 57 68  endif.../*. * Wh
8b8b0 65 6e 20 74 65 73 74 69 6e 67 2c 20 74 68 69 73  en testing, this
8b8c0 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65   global variable
8b8d0 20 73 74 6f 72 65 73 20 74 68 65 20 6c 6f 63 61   stores the loca
8b8e0 74 69 6f 6e 20 6f 66 20 74 68 65 0a 20 2a 20 70  tion of the. * p
8b8f0 65 6e 64 69 6e 67 2d 62 79 74 65 20 69 6e 20 74  ending-byte in t
8b900 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
8b910 2e 0a 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  .. */.#ifdef SQL
8b920 49 54 45 5f 54 45 53 54 0a 75 6e 73 69 67 6e 65  ITE_TEST.unsigne
8b930 64 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 65  d int sqlite3_pe
8b940 6e 64 69 6e 67 5f 62 79 74 65 20 3d 20 30 78 34  nding_byte = 0x4
8b950 30 30 30 30 30 30 30 3b 0a 23 65 6e 64 69 66 0a  0000000;.#endif.
8b960 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f  .int sqlite3_os_
8b970 74 72 61 63 65 20 3d 20 30 3b 0a 23 69 66 64 65  trace = 0;.#ifde
8b980 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 23  f SQLITE_DEBUG.#
8b990 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 31 28  define OSTRACE1(
8b9a0 58 29 20 20 20 20 20 20 20 20 20 69 66 28 20 73  X)         if( s
8b9b0 71 6c 69 74 65 33 5f 6f 73 5f 74 72 61 63 65 20  qlite3_os_trace 
8b9c0 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72  ) sqlite3DebugPr
8b9d0 69 6e 74 66 28 58 29 0a 23 64 65 66 69 6e 65 20  intf(X).#define 
8b9e0 4f 53 54 52 41 43 45 32 28 58 2c 59 29 20 20 20  OSTRACE2(X,Y)   
8b9f0 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f      if( sqlite3_
8ba00 6f 73 5f 74 72 61 63 65 20 29 20 73 71 6c 69 74  os_trace ) sqlit
8ba10 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c  e3DebugPrintf(X,
8ba20 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  Y).#define OSTRA
8ba30 43 45 33 28 58 2c 59 2c 5a 29 20 20 20 20 20 69  CE3(X,Y,Z)     i
8ba40 66 28 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 72  f( sqlite3_os_tr
8ba50 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62  ace ) sqlite3Deb
8ba60 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 29 0a  ugPrintf(X,Y,Z).
8ba70 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34  #define OSTRACE4
8ba80 28 58 2c 59 2c 5a 2c 41 29 20 20 20 69 66 28 20  (X,Y,Z,A)   if( 
8ba90 73 71 6c 69 74 65 33 5f 6f 73 5f 74 72 61 63 65  sqlite3_os_trace
8baa0 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50   ) sqlite3DebugP
8bab0 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 29 0a 23  rintf(X,Y,Z,A).#
8bac0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28  define OSTRACE5(
8bad0 58 2c 59 2c 5a 2c 41 2c 42 29 20 69 66 28 20 73  X,Y,Z,A,B) if( s
8bae0 71 6c 69 74 65 33 5f 6f 73 5f 74 72 61 63 65 20  qlite3_os_trace 
8baf0 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72  ) sqlite3DebugPr
8bb00 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a  intf(X,Y,Z,A,B).
8bb10 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36  #define OSTRACE6
8bb20 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20 5c 0a  (X,Y,Z,A,B,C) \.
8bb30 20 20 20 20 69 66 28 73 71 6c 69 74 65 33 5f 6f      if(sqlite3_o
8bb40 73 5f 74 72 61 63 65 29 20 73 71 6c 69 74 65 33  s_trace) sqlite3
8bb50 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c  DebugPrintf(X,Y,
8bb60 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65  Z,A,B,C).#define
8bb70 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c   OSTRACE7(X,Y,Z,
8bb80 41 2c 42 2c 43 2c 44 29 20 5c 0a 20 20 20 20 69  A,B,C,D) \.    i
8bb90 66 28 73 71 6c 69 74 65 33 5f 6f 73 5f 74 72 61  f(sqlite3_os_tra
8bba0 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 75 67  ce) sqlite3Debug
8bbb0 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42  Printf(X,Y,Z,A,B
8bbc0 2c 43 2c 44 29 0a 23 65 6c 73 65 0a 23 64 65 66  ,C,D).#else.#def
8bbd0 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 0a  ine OSTRACE1(X).
8bbe0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32  #define OSTRACE2
8bbf0 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53  (X,Y).#define OS
8bc00 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 0a 23 64  TRACE3(X,Y,Z).#d
8bc10 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58  efine OSTRACE4(X
8bc20 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20  ,Y,Z,A).#define 
8bc30 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41  OSTRACE5(X,Y,Z,A
8bc40 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,B).#define OSTR
8bc50 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43  ACE6(X,Y,Z,A,B,C
8bc60 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
8bc70 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44  E7(X,Y,Z,A,B,C,D
8bc80 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ).#endif../*.** 
8bc90 4d 61 63 72 6f 73 20 66 6f 72 20 70 65 72 66 6f  Macros for perfo
8bca0 72 6d 61 6e 63 65 20 74 72 61 63 69 6e 67 2e 20  rmance tracing. 
8bcb0 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64   Normally turned
8bcc0 20 6f 66 66 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b   off.  Only work
8bcd0 73 0a 2a 2a 20 6f 6e 20 69 34 38 36 20 68 61 72  s.** on i486 har
8bce0 64 77 61 72 65 2e 0a 2a 2f 0a 23 69 66 64 65 66  dware..*/.#ifdef
8bcf0 20 53 51 4c 49 54 45 5f 50 45 52 46 4f 52 4d 41   SQLITE_PERFORMA
8bd00 4e 43 45 5f 54 52 41 43 45 0a 5f 5f 69 6e 6c 69  NCE_TRACE.__inli
8bd10 6e 65 5f 5f 20 75 6e 73 69 67 6e 65 64 20 6c 6f  ne__ unsigned lo
8bd20 6e 67 20 6c 6f 6e 67 20 69 6e 74 20 68 77 74 69  ng long int hwti
8bd30 6d 65 28 76 6f 69 64 29 7b 0a 20 20 75 6e 73 69  me(void){.  unsi
8bd40 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 69  gned long long i
8bd50 6e 74 20 78 3b 0a 20 20 5f 5f 61 73 6d 5f 5f 28  nt x;.  __asm__(
8bd60 22 72 64 74 73 63 5c 6e 5c 74 22 0a 20 20 20 20  "rdtsc\n\t".    
8bd70 20 20 20 20 20 20 22 6d 6f 76 20 25 25 65 64 78        "mov %%edx
8bd80 2c 20 25 25 65 63 78 5c 6e 5c 74 22 0a 20 20 20  , %%ecx\n\t".   
8bd90 20 20 20 20 20 20 20 3a 22 3d 41 22 20 28 78 29         :"=A" (x)
8bda0 29 3b 0a 20 20 72 65 74 75 72 6e 20 78 3b 0a 7d  );.  return x;.}
8bdb0 0a 73 74 61 74 69 63 20 75 6e 73 69 67 6e 65 64  .static unsigned
8bdc0 20 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 20 67   long long int g
8bdd0 5f 73 74 61 72 74 3b 0a 73 74 61 74 69 63 20 75  _start;.static u
8bde0 6e 73 69 67 6e 65 64 20 69 6e 74 20 65 6c 61 70  nsigned int elap
8bdf0 73 65 3b 0a 23 64 65 66 69 6e 65 20 54 49 4d 45  se;.#define TIME
8be00 52 5f 53 54 41 52 54 20 20 20 20 20 20 20 67 5f  R_START       g_
8be10 73 74 61 72 74 3d 68 77 74 69 6d 65 28 29 0a 23  start=hwtime().#
8be20 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44  define TIMER_END
8be30 20 20 20 20 20 20 20 20 20 65 6c 61 70 73 65 3d           elapse=
8be40 68 77 74 69 6d 65 28 29 2d 67 5f 73 74 61 72 74  hwtime()-g_start
8be50 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45  .#define TIMER_E
8be60 4c 41 50 53 45 44 20 20 20 20 20 65 6c 61 70 73  LAPSED     elaps
8be70 65 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20  e.#else.#define 
8be80 54 49 4d 45 52 5f 53 54 41 52 54 0a 23 64 65 66  TIMER_START.#def
8be90 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 0a 23 64  ine TIMER_END.#d
8bea0 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50  efine TIMER_ELAP
8beb0 53 45 44 20 20 20 20 20 30 0a 23 65 6e 64 69 66  SED     0.#endif
8bec0 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 63 6f  ../*.** If we co
8bed0 6d 70 69 6c 65 20 77 69 74 68 20 74 68 65 20 53  mpile with the S
8bee0 51 4c 49 54 45 5f 54 45 53 54 20 6d 61 63 72 6f  QLITE_TEST macro
8bef0 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 66   set, then the f
8bf00 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a 2a  ollowing block.*
8bf10 2a 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c 20 67  * of code will g
8bf20 69 76 65 20 75 73 20 74 68 65 20 61 62 69 6c 69  ive us the abili
8bf30 74 79 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61  ty to simulate a
8bf40 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 2e   disk I/O error.
8bf50 20 20 54 68 69 73 0a 2a 2a 20 69 73 20 75 73 65    This.** is use
8bf60 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 74 68  d for testing th
8bf70 65 20 49 2f 4f 20 72 65 63 6f 76 65 72 79 20 6c  e I/O recovery l
8bf80 6f 67 69 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  ogic..*/.#ifdef 
8bf90 53 51 4c 49 54 45 5f 54 45 53 54 0a 69 6e 74 20  SQLITE_TEST.int 
8bfa0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
8bfb0 5f 68 69 74 20 3d 20 30 3b 0a 69 6e 74 20 73 71  _hit = 0;.int sq
8bfc0 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70  lite3_io_error_p
8bfd0 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 69 6e 74 20  ending = 0;.int 
8bfe0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
8bff0 5f 70 65 72 73 69 73 74 20 3d 20 30 3b 0a 69 6e  _persist = 0;.in
8c000 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75  t sqlite3_diskfu
8c010 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 0a  ll_pending = 0;.
8c020 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b  int sqlite3_disk
8c030 66 75 6c 6c 20 3d 20 30 3b 0a 23 64 65 66 69 6e  full = 0;.#defin
8c040 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f  e SimulateIOErro
8c050 72 28 43 4f 44 45 29 20 20 5c 0a 20 20 69 66 28  r(CODE)  \.  if(
8c060 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
8c070 72 5f 70 65 6e 64 69 6e 67 20 7c 7c 20 73 71 6c  r_pending || sql
8c080 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69  ite3_io_error_hi
8c090 74 20 29 20 5c 0a 20 20 20 20 20 69 66 28 20 73  t ) \.     if( s
8c0a0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
8c0b0 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31 20 5c  pending-- == 1 \
8c0c0 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 28 73 71  .         || (sq
8c0d0 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70  lite3_io_error_p
8c0e0 65 72 73 69 73 74 20 26 26 20 73 71 6c 69 74 65  ersist && sqlite
8c0f0 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 29 20  3_io_error_hit) 
8c100 29 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ) \.            
8c110 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65 72      { local_ioer
8c120 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0a 73 74 61  r(); CODE; }.sta
8c130 74 69 63 20 76 6f 69 64 20 6c 6f 63 61 6c 5f 69  tic void local_i
8c140 6f 65 72 72 28 29 7b 0a 20 20 49 4f 54 52 41 43  oerr(){.  IOTRAC
8c150 45 28 28 22 49 4f 45 52 52 5c 6e 22 29 29 3b 0a  E(("IOERR\n"));.
8c160 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72    sqlite3_io_err
8c170 6f 72 5f 68 69 74 20 3d 20 31 3b 0a 7d 0a 23 64  or_hit = 1;.}.#d
8c180 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69  efine SimulateDi
8c190 73 6b 66 75 6c 6c 45 72 72 6f 72 28 43 4f 44 45  skfullError(CODE
8c1a0 29 20 5c 0a 20 20 20 69 66 28 20 73 71 6c 69 74  ) \.   if( sqlit
8c1b0 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64  e3_diskfull_pend
8c1c0 69 6e 67 20 29 7b 20 5c 0a 20 20 20 20 20 69 66  ing ){ \.     if
8c1d0 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75  ( sqlite3_diskfu
8c1e0 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d 20 31 20  ll_pending == 1 
8c1f0 29 7b 20 5c 0a 20 20 20 20 20 20 20 6c 6f 63 61  ){ \.       loca
8c200 6c 5f 69 6f 65 72 72 28 29 3b 20 5c 0a 20 20 20  l_ioerr(); \.   
8c210 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b      sqlite3_disk
8c220 66 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20 20 20  full = 1; \.    
8c230 20 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72     sqlite3_io_er
8c240 72 6f 72 5f 68 69 74 20 3d 20 31 3b 20 5c 0a 20  ror_hit = 1; \. 
8c250 20 20 20 20 20 20 43 4f 44 45 3b 20 5c 0a 20 20        CODE; \.  
8c260 20 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 20 20 20     }else{ \.    
8c270 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66     sqlite3_diskf
8c280 75 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c  ull_pending--; \
8c290 0a 20 20 20 20 20 7d 20 5c 0a 20 20 20 7d 0a 23  .     } \.   }.#
8c2a0 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 53 69 6d  else.#define Sim
8c2b0 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 41 29 0a  ulateIOError(A).
8c2c0 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65  #define Simulate
8c2d0 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 41 29  DiskfullError(A)
8c2e0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57  .#endif../*.** W
8c2f0 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 6b 65 65  hen testing, kee
8c300 70 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65  p a count of the
8c310 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20   number of open 
8c320 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66  files..*/.#ifdef
8c330 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 69 6e 74   SQLITE_TEST.int
8c340 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69   sqlite3_open_fi
8c350 6c 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 64  le_count = 0;.#d
8c360 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65  efine OpenCounte
8c370 72 28 58 29 20 20 73 71 6c 69 74 65 33 5f 6f 70  r(X)  sqlite3_op
8c380 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 2b 3d 28  en_file_count+=(
8c390 58 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65  X).#else.#define
8c3a0 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 0a   OpenCounter(X).
8c3b0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 73 71  #endif../*.** sq
8c3c0 6c 69 74 65 33 47 65 6e 65 72 69 63 4d 61 6c 6c  lite3GenericMall
8c3d0 6f 63 0a 2a 2a 20 73 71 6c 69 74 65 33 47 65 6e  oc.** sqlite3Gen
8c3e0 65 72 69 63 52 65 61 6c 6c 6f 63 0a 2a 2a 20 73  ericRealloc.** s
8c3f0 71 6c 69 74 65 33 47 65 6e 65 72 69 63 4f 73 46  qlite3GenericOsF
8c400 72 65 65 0a 2a 2a 20 73 71 6c 69 74 65 33 47 65  ree.** sqlite3Ge
8c410 6e 65 72 69 63 41 6c 6c 6f 63 61 74 69 6f 6e 53  nericAllocationS
8c420 69 7a 65 0a 2a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  ize.**.** Implem
8c430 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
8c440 6f 73 20 6c 65 76 65 6c 20 64 79 6e 61 6d 69 63  os level dynamic
8c450 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
8c460 6f 6e 20 69 6e 74 65 72 66 61 63 65 20 69 6e 20  on interface in 
8c470 74 65 72 6d 73 0a 2a 2a 20 6f 66 20 74 68 65 20  terms.** of the 
8c480 73 74 61 6e 64 61 72 64 20 6d 61 6c 6c 6f 63 28  standard malloc(
8c490 29 2c 20 72 65 61 6c 6c 6f 63 28 29 20 61 6e 64  ), realloc() and
8c4a0 20 66 72 65 65 28 29 20 66 6f 75 6e 64 20 69 6e   free() found in
8c4b0 20 6d 61 6e 79 20 6f 70 65 72 61 74 69 6e 67 0a   many operating.
8c4c0 2a 2a 20 73 79 73 74 65 6d 73 2e 20 4e 6f 20 72  ** systems. No r
8c4d0 6f 63 6b 65 74 20 73 63 69 65 6e 63 65 20 68 65  ocket science he
8c4e0 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20  re..**.** There 
8c4f0 61 72 65 20 74 77 6f 20 76 65 72 73 69 6f 6e 73  are two versions
8c500 20 6f 66 20 74 68 65 73 65 20 66 6f 75 72 20 66   of these four f
8c510 75 6e 63 74 69 6f 6e 73 20 68 65 72 65 2e 20 54  unctions here. T
8c520 68 65 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 69 6d  he version.** im
8c530 70 6c 65 6d 65 6e 74 65 64 20 68 65 72 65 20 69  plemented here i
8c540 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 6d  s only used if m
8c550 65 6d 6f 72 79 2d 6d 61 6e 61 67 65 6d 65 6e 74  emory-management
8c560 20 6f 72 20 6d 65 6d 6f 72 79 2d 64 65 62 75 67   or memory-debug
8c570 67 69 6e 67 20 69 73 0a 2a 2a 20 65 6e 61 62 6c  ging is.** enabl
8c580 65 64 2e 20 54 68 69 73 20 76 65 72 73 69 6f 6e  ed. This version
8c590 20 61 6c 6c 6f 63 61 74 65 73 20 61 6e 20 65 78   allocates an ex
8c5a0 74 72 61 20 38 2d 62 79 74 65 73 20 61 74 20 74  tra 8-bytes at t
8c5b0 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
8c5c0 65 61 63 68 0a 2a 2a 20 62 6c 6f 63 6b 20 61 6e  each.** block an
8c5d0 64 20 73 74 6f 72 65 73 20 74 68 65 20 73 69 7a  d stores the siz
8c5e0 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74  e of the allocat
8c5f0 69 6f 6e 20 74 68 65 72 65 2e 0a 2a 2a 0a 2a 2a  ion there..**.**
8c600 20 49 66 20 6e 65 69 74 68 65 72 20 6d 65 6d 6f   If neither memo
8c610 72 79 2d 6d 61 6e 61 67 65 6d 65 6e 74 20 6f 72  ry-management or
8c620 20 64 65 62 75 67 67 69 6e 67 20 69 73 20 65 6e   debugging is en
8c630 61 62 6c 65 64 2c 20 74 68 65 20 73 65 63 6f 6e  abled, the secon
8c640 64 0a 2a 2a 20 73 65 74 20 6f 66 20 69 6d 70 6c  d.** set of impl
8c650 65 6d 65 6e 74 61 74 69 6f 6e 73 20 69 73 20 75  ementations is u
8c660 73 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2f 0a  sed instead..*/.
8c670 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
8c680 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59  TE_ENABLE_MEMORY
8c690 5f 4d 41 4e 41 47 45 4d 45 4e 54 29 20 7c 7c 20  _MANAGEMENT) || 
8c6a0 64 65 66 69 6e 65 64 20 28 53 51 4c 49 54 45 5f  defined (SQLITE_
8c6b0 4d 45 4d 44 45 42 55 47 29 0a 53 51 4c 49 54 45  MEMDEBUG).SQLITE
8c6c0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
8c6d0 71 6c 69 74 65 33 47 65 6e 65 72 69 63 4d 61 6c  qlite3GenericMal
8c6e0 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 20 63 68  loc(int n){.  ch
8c6f0 61 72 20 2a 70 20 3d 20 28 63 68 61 72 20 2a 29  ar *p = (char *)
8c700 6d 61 6c 6c 6f 63 28 6e 2b 38 29 3b 0a 20 20 61  malloc(n+8);.  a
8c710 73 73 65 72 74 28 6e 3e 30 29 3b 0a 20 20 61 73  ssert(n>0);.  as
8c720 73 65 72 74 28 73 69 7a 65 6f 66 28 69 6e 74 29  sert(sizeof(int)
8c730 3c 3d 38 29 3b 0a 20 20 69 66 28 20 70 20 29 7b  <=8);.  if( p ){
8c740 0a 20 20 20 20 2a 28 69 6e 74 20 2a 29 70 20 3d  .    *(int *)p =
8c750 20 6e 3b 0a 20 20 20 20 70 20 2b 3d 20 38 3b 0a   n;.    p += 8;.
8c760 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 6f    }.  return (vo
8c770 69 64 20 2a 29 70 3b 0a 7d 0a 53 51 4c 49 54 45  id *)p;.}.SQLITE
8c780 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
8c790 71 6c 69 74 65 33 47 65 6e 65 72 69 63 52 65 61  qlite3GenericRea
8c7a0 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 2c 20 69 6e  lloc(void *p, in
8c7b0 74 20 6e 29 7b 0a 20 20 63 68 61 72 20 2a 70 32  t n){.  char *p2
8c7c0 20 3d 20 28 28 63 68 61 72 20 2a 29 70 20 2d 20   = ((char *)p - 
8c7d0 38 29 3b 0a 20 20 61 73 73 65 72 74 28 6e 3e 30  8);.  assert(n>0
8c7e0 29 3b 0a 20 20 70 32 20 3d 20 28 63 68 61 72 2a  );.  p2 = (char*
8c7f0 29 72 65 61 6c 6c 6f 63 28 70 32 2c 20 6e 2b 38  )realloc(p2, n+8
8c800 29 3b 0a 20 20 69 66 28 20 70 32 20 29 7b 0a 20  );.  if( p2 ){. 
8c810 20 20 20 2a 28 69 6e 74 20 2a 29 70 32 20 3d 20     *(int *)p2 = 
8c820 6e 3b 0a 20 20 20 20 70 32 20 2b 3d 20 38 3b 0a  n;.    p2 += 8;.
8c830 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 6f    }.  return (vo
8c840 69 64 20 2a 29 70 32 3b 0a 7d 0a 53 51 4c 49 54  id *)p2;.}.SQLIT
8c850 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
8c860 71 6c 69 74 65 33 47 65 6e 65 72 69 63 46 72 65  qlite3GenericFre
8c870 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 61 73  e(void *p){.  as
8c880 73 65 72 74 28 70 29 3b 0a 20 20 66 72 65 65 28  sert(p);.  free(
8c890 28 76 6f 69 64 20 2a 29 28 28 63 68 61 72 20 2a  (void *)((char *
8c8a0 29 70 20 2d 20 38 29 29 3b 0a 7d 0a 53 51 4c 49  )p - 8));.}.SQLI
8c8b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
8c8c0 71 6c 69 74 65 33 47 65 6e 65 72 69 63 41 6c 6c  qlite3GenericAll
8c8d0 6f 63 61 74 69 6f 6e 53 69 7a 65 28 76 6f 69 64  ocationSize(void
8c8e0 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70   *p){.  return p
8c8f0 20 3f 20 2a 28 69 6e 74 20 2a 29 28 28 63 68 61   ? *(int *)((cha
8c900 72 20 2a 29 70 20 2d 20 38 29 20 3a 20 30 3b 0a  r *)p - 8) : 0;.
8c910 7d 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50  }.#else.SQLITE_P
8c920 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
8c930 69 74 65 33 47 65 6e 65 72 69 63 4d 61 6c 6c 6f  ite3GenericMallo
8c940 63 28 69 6e 74 20 6e 29 7b 0a 20 20 63 68 61 72  c(int n){.  char
8c950 20 2a 70 20 3d 20 28 63 68 61 72 20 2a 29 6d 61   *p = (char *)ma
8c960 6c 6c 6f 63 28 6e 29 3b 0a 20 20 72 65 74 75 72  lloc(n);.  retur
8c970 6e 20 28 76 6f 69 64 20 2a 29 70 3b 0a 7d 0a 53  n (void *)p;.}.S
8c980 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
8c990 69 64 20 2a 73 71 6c 69 74 65 33 47 65 6e 65 72  id *sqlite3Gener
8c9a0 69 63 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a  icRealloc(void *
8c9b0 70 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 61 73 73  p, int n){.  ass
8c9c0 65 72 74 28 6e 3e 30 29 3b 0a 20 20 70 20 3d 20  ert(n>0);.  p = 
8c9d0 72 65 61 6c 6c 6f 63 28 70 2c 20 6e 29 3b 0a 20  realloc(p, n);. 
8c9e0 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 53 51 4c   return p;.}.SQL
8c9f0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
8ca00 20 73 71 6c 69 74 65 33 47 65 6e 65 72 69 63 46   sqlite3GenericF
8ca10 72 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20  ree(void *p){.  
8ca20 61 73 73 65 72 74 28 70 29 3b 0a 20 20 66 72 65  assert(p);.  fre
8ca30 65 28 70 29 3b 0a 7d 0a 2f 2a 20 4e 65 76 65 72  e(p);.}./* Never
8ca40 20 61 63 74 75 61 6c 6c 79 20 75 73 65 64 2c 20   actually used, 
8ca50 62 75 74 20 6e 65 65 64 65 64 20 66 6f 72 20 74  but needed for t
8ca60 68 65 20 6c 69 6e 6b 65 72 20 2a 2f 0a 53 51 4c  he linker */.SQL
8ca70 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
8ca80 73 71 6c 69 74 65 33 47 65 6e 65 72 69 63 41 6c  sqlite3GenericAl
8ca90 6c 6f 63 61 74 69 6f 6e 53 69 7a 65 28 76 6f 69  locationSize(voi
8caa0 64 20 2a 70 29 7b 20 72 65 74 75 72 6e 20 30 3b  d *p){ return 0;
8cab0 20 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a   }.#endif../*.**
8cac0 20 54 68 65 20 64 65 66 61 75 6c 74 20 73 69 7a   The default siz
8cad0 65 20 6f 66 20 61 20 64 69 73 6b 20 73 65 63 74  e of a disk sect
8cae0 6f 72 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 50 41  or.*/.#ifndef PA
8caf0 47 45 52 5f 53 45 43 54 4f 52 5f 53 49 5a 45 0a  GER_SECTOR_SIZE.
8cb00 23 20 64 65 66 69 6e 65 20 50 41 47 45 52 5f 53  # define PAGER_S
8cb10 45 43 54 4f 52 5f 53 49 5a 45 20 35 31 32 0a 23  ECTOR_SIZE 512.#
8cb20 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  endif../********
8cb30 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73  ****** End of os
8cb40 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a  _common.h ******
8cb50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8cb60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8cb70 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
8cb80 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e  ****** Continuin
8cb90 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20  g where we left 
8cba0 6f 66 66 20 69 6e 20 6f 73 5f 77 69 6e 2e 63 20  off in os_win.c 
8cbb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8cbc0 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 44 65  *****/../*.** De
8cbd0 74 65 72 6d 69 6e 65 20 69 66 20 77 65 20 61 72  termine if we ar
8cbe0 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 57  e dealing with W
8cbf0 69 6e 64 6f 77 73 43 45 20 2d 20 77 68 69 63 68  indowsCE - which
8cc00 20 68 61 73 20 61 20 6d 75 63 68 0a 2a 2a 20 72   has a much.** r
8cc10 65 64 75 63 65 64 20 41 50 49 2e 0a 2a 2f 0a 23  educed API..*/.#
8cc20 69 66 20 64 65 66 69 6e 65 64 28 5f 57 49 4e 33  if defined(_WIN3
8cc30 32 5f 57 43 45 29 0a 23 20 64 65 66 69 6e 65 20  2_WCE).# define 
8cc40 4f 53 5f 57 49 4e 43 45 20 31 0a 23 20 64 65 66  OS_WINCE 1.# def
8cc50 69 6e 65 20 41 72 65 46 69 6c 65 41 70 69 73 41  ine AreFileApisA
8cc60 4e 53 49 28 29 20 31 0a 23 65 6c 73 65 0a 23 20  NSI() 1.#else.# 
8cc70 64 65 66 69 6e 65 20 4f 53 5f 57 49 4e 43 45 20  define OS_WINCE 
8cc80 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  0.#endif../*.** 
8cc90 57 69 6e 43 45 20 6c 61 63 6b 73 20 6e 61 74 69  WinCE lacks nati
8cca0 76 65 20 73 75 70 70 6f 72 74 20 66 6f 72 20 66  ve support for f
8ccb0 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 73 6f 20 77  ile locking so w
8ccc0 65 20 68 61 76 65 20 74 6f 20 66 61 6b 65 20 69  e have to fake i
8ccd0 74 0a 2a 2a 20 77 69 74 68 20 73 6f 6d 65 20 63  t.** with some c
8cce0 6f 64 65 20 6f 66 20 6f 75 72 20 6f 77 6e 2e 0a  ode of our own..
8ccf0 2a 2f 0a 23 69 66 20 4f 53 5f 57 49 4e 43 45 0a  */.#if OS_WINCE.
8cd00 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 77  typedef struct w
8cd10 69 6e 63 65 4c 6f 63 6b 20 7b 0a 20 20 69 6e 74  inceLock {.  int
8cd20 20 6e 52 65 61 64 65 72 73 3b 20 20 20 20 20 20   nReaders;      
8cd30 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65   /* Number of re
8cd40 61 64 65 72 20 6c 6f 63 6b 73 20 6f 62 74 61 69  ader locks obtai
8cd50 6e 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62 50  ned */.  BOOL bP
8cd60 65 6e 64 69 6e 67 3b 20 20 20 20 20 20 2f 2a 20  ending;      /* 
8cd70 49 6e 64 69 63 61 74 65 73 20 61 20 70 65 6e 64  Indicates a pend
8cd80 69 6e 67 20 6c 6f 63 6b 20 68 61 73 20 62 65 65  ing lock has bee
8cd90 6e 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 20 20  n obtained */.  
8cda0 42 4f 4f 4c 20 62 52 65 73 65 72 76 65 64 3b 20  BOOL bReserved; 
8cdb0 20 20 20 20 2f 2a 20 49 6e 64 69 63 61 74 65 73      /* Indicates
8cdc0 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b   a reserved lock
8cdd0 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69 6e   has been obtain
8cde0 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62 45 78  ed */.  BOOL bEx
8cdf0 63 6c 75 73 69 76 65 3b 20 20 20 20 2f 2a 20 49  clusive;    /* I
8ce00 6e 64 69 63 61 74 65 73 20 61 6e 20 65 78 63 6c  ndicates an excl
8ce10 75 73 69 76 65 20 6c 6f 63 6b 20 68 61 73 20 62  usive lock has b
8ce20 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a  een obtained */.
8ce30 7d 20 77 69 6e 63 65 4c 6f 63 6b 3b 0a 23 65 6e  } winceLock;.#en
8ce40 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 77  dif../*.** The w
8ce50 69 6e 46 69 6c 65 20 73 74 72 75 63 74 75 72 65  inFile structure
8ce60 20 69 73 20 61 20 73 75 62 63 6c 61 73 73 20 6f   is a subclass o
8ce70 66 20 4f 73 46 69 6c 65 20 73 70 65 63 69 66 69  f OsFile specifi
8ce80 63 20 74 6f 20 74 68 65 20 77 69 6e 33 32 0a 2a  c to the win32.*
8ce90 2a 20 70 6f 72 74 61 62 69 6c 69 74 79 20 6c 61  * portability la
8cea0 79 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  yer..*/.typedef 
8ceb0 73 74 72 75 63 74 20 77 69 6e 46 69 6c 65 20 77  struct winFile w
8cec0 69 6e 46 69 6c 65 3b 0a 73 74 72 75 63 74 20 77  inFile;.struct w
8ced0 69 6e 46 69 6c 65 20 7b 0a 20 20 49 6f 4d 65 74  inFile {.  IoMet
8cee0 68 6f 64 20 63 6f 6e 73 74 20 2a 70 4d 65 74 68  hod const *pMeth
8cef0 6f 64 3b 2f 2a 20 4d 75 73 74 20 62 65 20 66 69  od;/* Must be fi
8cf00 72 73 74 20 2a 2f 0a 20 20 48 41 4e 44 4c 45 20  rst */.  HANDLE 
8cf10 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h;              
8cf20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72 20 61   /* Handle for a
8cf30 63 63 65 73 73 69 6e 67 20 74 68 65 20 66 69 6c  ccessing the fil
8cf40 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  e */.  unsigned 
8cf50 63 68 61 72 20 6c 6f 63 6b 74 79 70 65 3b 20 2f  char locktype; /
8cf60 2a 20 54 79 70 65 20 6f 66 20 6c 6f 63 6b 20 63  * Type of lock c
8cf70 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 20 6f 6e  urrently held on
8cf80 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20   this file */.  
8cf90 73 68 6f 72 74 20 73 68 61 72 65 64 4c 6f 63 6b  short sharedLock
8cfa0 42 79 74 65 3b 20 20 20 2f 2a 20 52 61 6e 64 6f  Byte;   /* Rando
8cfb0 6d 6c 79 20 63 68 6f 73 65 6e 20 62 79 74 65 20  mly chosen byte 
8cfc0 75 73 65 64 20 61 73 20 61 20 73 68 61 72 65 64  used as a shared
8cfd0 20 6c 6f 63 6b 20 2a 2f 0a 23 69 66 20 4f 53 5f   lock */.#if OS_
8cfe0 57 49 4e 43 45 0a 20 20 57 43 48 41 52 20 2a 7a  WINCE.  WCHAR *z
8cff0 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 3b 20 20  DeleteOnClose;  
8d000 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20  /* Name of file 
8d010 74 6f 20 64 65 6c 65 74 65 20 77 68 65 6e 20 63  to delete when c
8d020 6c 6f 73 69 6e 67 20 2a 2f 0a 20 20 48 41 4e 44  losing */.  HAND
8d030 4c 45 20 68 4d 75 74 65 78 3b 20 20 20 20 20 20  LE hMutex;      
8d040 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 75 73 65      /* Mutex use
8d050 64 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63  d to control acc
8d060 65 73 73 20 74 6f 20 73 68 61 72 65 64 20 6c 6f  ess to shared lo
8d070 63 6b 20 2a 2f 20 20 0a 20 20 48 41 4e 44 4c 45  ck */  .  HANDLE
8d080 20 68 53 68 61 72 65 64 3b 20 20 20 20 20 20 20   hShared;       
8d090 20 20 2f 2a 20 53 68 61 72 65 64 20 6d 65 6d 6f    /* Shared memo
8d0a0 72 79 20 73 65 67 6d 65 6e 74 20 75 73 65 64 20  ry segment used 
8d0b0 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20  for locking */. 
8d0c0 20 77 69 6e 63 65 4c 6f 63 6b 20 6c 6f 63 61 6c   winceLock local
8d0d0 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 63 6b  ;        /* Lock
8d0e0 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 74 68  s obtained by th
8d0f0 69 73 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 77  is instance of w
8d100 69 6e 46 69 6c 65 20 2a 2f 0a 20 20 77 69 6e 63  inFile */.  winc
8d110 65 4c 6f 63 6b 20 2a 73 68 61 72 65 64 3b 20 20  eLock *shared;  
8d120 20 20 20 20 2f 2a 20 47 6c 6f 62 61 6c 20 73 68      /* Global sh
8d130 61 72 65 64 20 6c 6f 63 6b 20 6d 65 6d 6f 72 79  ared lock memory
8d140 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20 20 2a   for the file  *
8d150 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 0a 2f 2a  /.#endif.};.../*
8d160 0a 2a 2a 20 44 6f 20 6e 6f 74 20 69 6e 63 6c 75  .** Do not inclu
8d170 64 65 20 61 6e 79 20 6f 66 20 74 68 65 20 46 69  de any of the Fi
8d180 6c 65 20 49 2f 4f 20 69 6e 74 65 72 66 61 63 65  le I/O interface
8d190 20 70 72 6f 63 65 64 75 72 65 73 20 69 66 20 74   procedures if t
8d1a0 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  he.** SQLITE_OMI
8d1b0 54 5f 44 49 53 4b 49 4f 20 6d 61 63 72 6f 20 69  T_DISKIO macro i
8d1c0 73 20 64 65 66 69 6e 65 64 20 28 69 6e 64 69 63  s defined (indic
8d1d0 61 74 69 6e 67 20 74 68 61 74 20 74 68 65 72 65  ating that there
8d1e0 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 77 69 6c   database.** wil
8d1f0 6c 20 62 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 6f  l be in-memory o
8d200 6e 6c 79 29 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  nly).*/.#ifndef 
8d210 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b  SQLITE_OMIT_DISK
8d220 49 4f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  IO../*.** The fo
8d230 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65  llowing variable
8d240 20 69 73 20 28 6e 6f 72 6d 61 6c 6c 79 29 20 73   is (normally) s
8d250 65 74 20 6f 6e 63 65 20 61 6e 64 20 6e 65 76 65  et once and neve
8d260 72 20 63 68 61 6e 67 65 73 0a 2a 2a 20 74 68 65  r changes.** the
8d270 72 65 61 66 74 65 72 2e 20 20 49 74 20 72 65 63  reafter.  It rec
8d280 6f 72 64 73 20 77 68 65 74 68 65 72 20 74 68 65  ords whether the
8d290 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   operating syste
8d2a0 6d 20 69 73 20 57 69 6e 39 35 0a 2a 2a 20 6f 72  m is Win95.** or
8d2b0 20 57 69 6e 4e 54 2e 0a 2a 2a 0a 2a 2a 20 30 3a   WinNT..**.** 0:
8d2c0 20 20 20 4f 70 65 72 61 74 69 6e 67 20 73 79 73     Operating sys
8d2d0 74 65 6d 20 75 6e 6b 6e 6f 77 6e 2e 0a 2a 2a 20  tem unknown..** 
8d2e0 31 3a 20 20 20 4f 70 65 72 61 74 69 6e 67 20 73  1:   Operating s
8d2f0 79 73 74 65 6d 20 69 73 20 57 69 6e 39 35 2e 0a  ystem is Win95..
8d300 2a 2a 20 32 3a 20 20 20 4f 70 65 72 61 74 69 6e  ** 2:   Operatin
8d310 67 20 73 79 73 74 65 6d 20 69 73 20 57 69 6e 4e  g system is WinN
8d320 54 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 72 64 65  T..**.** In orde
8d330 72 20 74 6f 20 66 61 63 69 6c 69 74 61 74 65 20  r to facilitate 
8d340 74 65 73 74 69 6e 67 20 6f 6e 20 61 20 57 69 6e  testing on a Win
8d350 4e 54 20 73 79 73 74 65 6d 2c 20 74 68 65 20 74  NT system, the t
8d360 65 73 74 20 66 69 78 74 75 72 65 0a 2a 2a 20 63  est fixture.** c
8d370 61 6e 20 6d 61 6e 75 61 6c 6c 79 20 73 65 74 20  an manually set 
8d380 74 68 69 73 20 76 61 6c 75 65 20 74 6f 20 31 20  this value to 1 
8d390 74 6f 20 65 6d 75 6c 61 74 65 20 57 69 6e 39 38  to emulate Win98
8d3a0 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2f 0a 69 6e   behavior..*/.in
8d3b0 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70  t sqlite3_os_typ
8d3c0 65 20 3d 20 30 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65  e = 0;../*.** Re
8d3d0 74 75 72 6e 20 74 72 75 65 20 28 6e 6f 6e 2d 7a  turn true (non-z
8d3e0 65 72 6f 29 20 69 66 20 77 65 20 61 72 65 20 72  ero) if we are r
8d3f0 75 6e 6e 69 6e 67 20 75 6e 64 65 72 20 57 69 6e  unning under Win
8d400 4e 54 2c 20 57 69 6e 32 4b 2c 20 57 69 6e 58 50  NT, Win2K, WinXP
8d410 2c 0a 2a 2a 20 6f 72 20 57 69 6e 43 45 2e 20 20  ,.** or WinCE.  
8d420 52 65 74 75 72 6e 20 66 61 6c 73 65 20 28 7a 65  Return false (ze
8d430 72 6f 29 20 66 6f 72 20 57 69 6e 39 35 2c 20 57  ro) for Win95, W
8d440 69 6e 39 38 2c 20 6f 72 20 57 69 6e 4d 45 2e 0a  in98, or WinME..
8d450 2a 2a 0a 2a 2a 20 48 65 72 65 20 69 73 20 61 6e  **.** Here is an
8d460 20 69 6e 74 65 72 65 73 74 69 6e 67 20 6f 62 73   interesting obs
8d470 65 72 76 61 74 69 6f 6e 3a 20 20 57 69 6e 39 35  ervation:  Win95
8d480 2c 20 57 69 6e 39 38 2c 20 61 6e 64 20 57 69 6e  , Win98, and Win
8d490 4d 45 20 6c 61 63 6b 0a 2a 2a 20 74 68 65 20 4c  ME lack.** the L
8d4a0 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e  ockFileEx() API.
8d4b0 20 20 42 75 74 20 77 65 20 63 61 6e 20 73 74 69    But we can sti
8d4c0 6c 6c 20 73 74 61 74 69 63 61 6c 6c 79 20 6c 69  ll statically li
8d4d0 6e 6b 20 61 67 61 69 6e 73 74 20 74 68 61 74 0a  nk against that.
8d4e0 2a 2a 20 41 50 49 20 61 73 20 6c 6f 6e 67 20 61  ** API as long a
8d4f0 73 20 77 65 20 64 6f 6e 27 74 20 63 61 6c 6c 20  s we don't call 
8d500 69 74 20 77 69 6e 20 72 75 6e 6e 69 6e 67 20 57  it win running W
8d510 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20 41 20 63  in95/98/ME.  A c
8d520 61 6c 6c 20 74 6f 0a 2a 2a 20 74 68 69 73 20 72  all to.** this r
8d530 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 74  outine is used t
8d540 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20 74  o determine if t
8d550 68 65 20 68 6f 73 74 20 69 73 20 57 69 6e 39 35  he host is Win95
8d560 2f 39 38 2f 4d 45 20 6f 72 0a 2a 2a 20 57 69 6e  /98/ME or.** Win
8d570 4e 54 2f 32 4b 2f 58 50 20 73 6f 20 74 68 61 74  NT/2K/XP so that
8d580 20 77 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 77 68   we will know wh
8d590 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 77 65 20  ether or not we 
8d5a0 63 61 6e 20 73 61 66 65 6c 79 20 63 61 6c 6c 0a  can safely call.
8d5b0 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45  ** the LockFileE
8d5c0 78 28 29 20 41 50 49 2e 0a 2a 2f 0a 23 69 66 20  x() API..*/.#if 
8d5d0 4f 53 5f 57 49 4e 43 45 0a 23 20 64 65 66 69 6e  OS_WINCE.# defin
8d5e0 65 20 69 73 4e 54 28 29 20 20 28 31 29 0a 23 65  e isNT()  (1).#e
8d5f0 6c 73 65 0a 20 20 73 74 61 74 69 63 20 69 6e 74  lse.  static int
8d600 20 69 73 4e 54 28 76 6f 69 64 29 7b 0a 20 20 20   isNT(void){.   
8d610 20 69 66 28 20 73 71 6c 69 74 65 33 5f 6f 73 5f   if( sqlite3_os_
8d620 74 79 70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20  type==0 ){.     
8d630 20 4f 53 56 45 52 53 49 4f 4e 49 4e 46 4f 20 73   OSVERSIONINFO s
8d640 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73 49 6e 66  Info;.      sInf
8d650 6f 2e 64 77 4f 53 56 65 72 73 69 6f 6e 49 6e 66  o.dwOSVersionInf
8d660 6f 53 69 7a 65 20 3d 20 73 69 7a 65 6f 66 28 73  oSize = sizeof(s
8d670 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 47 65 74  Info);.      Get
8d680 56 65 72 73 69 6f 6e 45 78 28 26 73 49 6e 66 6f  VersionEx(&sInfo
8d690 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
8d6a0 5f 6f 73 5f 74 79 70 65 20 3d 20 73 49 6e 66 6f  _os_type = sInfo
8d6b0 2e 64 77 50 6c 61 74 66 6f 72 6d 49 64 3d 3d 56  .dwPlatformId==V
8d6c0 45 52 5f 50 4c 41 54 46 4f 52 4d 5f 57 49 4e 33  ER_PLATFORM_WIN3
8d6d0 32 5f 4e 54 20 3f 20 32 20 3a 20 31 3b 0a 20 20  2_NT ? 2 : 1;.  
8d6e0 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 73    }.    return s
8d6f0 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 3d 3d  qlite3_os_type==
8d700 32 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a  2;.  }.#endif /*
8d710 20 4f 53 5f 57 49 4e 43 45 20 2a 2f 0a 0a 2f 2a   OS_WINCE */../*
8d720 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 55 54  .** Convert a UT
8d730 46 2d 38 20 73 74 72 69 6e 67 20 74 6f 20 6d 69  F-8 string to mi
8d740 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 20  crosoft unicode 
8d750 28 55 54 46 2d 31 36 3f 29 2e 20 0a 2a 2a 0a 2a  (UTF-16?). .**.*
8d760 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20  * Space to hold 
8d770 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72  the returned str
8d780 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 20  ing is obtained 
8d790 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f  from sqliteMallo
8d7a0 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 57 43 48  c..*/.static WCH
8d7b0 41 52 20 2a 75 74 66 38 54 6f 55 6e 69 63 6f 64  AR *utf8ToUnicod
8d7c0 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  e(const char *zF
8d7d0 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20  ilename){.  int 
8d7e0 6e 43 68 61 72 3b 0a 20 20 57 43 48 41 52 20 2a  nChar;.  WCHAR *
8d7f0 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 3b 0a 0a  zWideFilename;..
8d800 20 20 6e 43 68 61 72 20 3d 20 4d 75 6c 74 69 42    nChar = MultiB
8d810 79 74 65 54 6f 57 69 64 65 43 68 61 72 28 43 50  yteToWideChar(CP
8d820 5f 55 54 46 38 2c 20 30 2c 20 7a 46 69 6c 65 6e  _UTF8, 0, zFilen
8d830 61 6d 65 2c 20 2d 31 2c 20 4e 55 4c 4c 2c 20 30  ame, -1, NULL, 0
8d840 29 3b 0a 20 20 7a 57 69 64 65 46 69 6c 65 6e 61  );.  zWideFilena
8d850 6d 65 20 3d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f  me = sqliteMallo
8d860 63 28 20 6e 43 68 61 72 2a 73 69 7a 65 6f 66 28  c( nChar*sizeof(
8d870 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 5b 30 5d  zWideFilename[0]
8d880 29 20 29 3b 0a 20 20 69 66 28 20 7a 57 69 64 65  ) );.  if( zWide
8d890 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20  Filename==0 ){. 
8d8a0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
8d8b0 0a 20 20 6e 43 68 61 72 20 3d 20 4d 75 6c 74 69  .  nChar = Multi
8d8c0 42 79 74 65 54 6f 57 69 64 65 43 68 61 72 28 43  ByteToWideChar(C
8d8d0 50 5f 55 54 46 38 2c 20 30 2c 20 7a 46 69 6c 65  P_UTF8, 0, zFile
8d8e0 6e 61 6d 65 2c 20 2d 31 2c 20 7a 57 69 64 65 46  name, -1, zWideF
8d8f0 69 6c 65 6e 61 6d 65 2c 20 6e 43 68 61 72 29 3b  ilename, nChar);
8d900 0a 20 20 69 66 28 20 6e 43 68 61 72 3d 3d 30 20  .  if( nChar==0 
8d910 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 46 72 65  ){.    sqliteFre
8d920 65 28 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29  e(zWideFilename)
8d930 3b 0a 20 20 20 20 7a 57 69 64 65 46 69 6c 65 6e  ;.    zWideFilen
8d940 61 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72  ame = 0;.  }.  r
8d950 65 74 75 72 6e 20 7a 57 69 64 65 46 69 6c 65 6e  eturn zWideFilen
8d960 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  ame;.}../*.** Co
8d970 6e 76 65 72 74 20 6d 69 63 72 6f 73 6f 66 74 20  nvert microsoft 
8d980 75 6e 69 63 6f 64 65 20 74 6f 20 55 54 46 2d 38  unicode to UTF-8
8d990 2e 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64  .  Space to hold
8d9a0 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74   the returned st
8d9b0 72 69 6e 67 20 69 73 0a 2a 2a 20 6f 62 74 61 69  ring is.** obtai
8d9c0 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d  ned from sqliteM
8d9d0 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74  alloc()..*/.stat
8d9e0 69 63 20 63 68 61 72 20 2a 75 6e 69 63 6f 64 65  ic char *unicode
8d9f0 54 6f 55 74 66 38 28 63 6f 6e 73 74 20 57 43 48  ToUtf8(const WCH
8da00 41 52 20 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d  AR *zWideFilenam
8da10 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b  e){.  int nByte;
8da20 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  .  char *zFilena
8da30 6d 65 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 57  me;..  nByte = W
8da40 69 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79  ideCharToMultiBy
8da50 74 65 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a  te(CP_UTF8, 0, z
8da60 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31  WideFilename, -1
8da70 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  , 0, 0, 0, 0);. 
8da80 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 73 71 6c   zFilename = sql
8da90 69 74 65 4d 61 6c 6c 6f 63 28 20 6e 42 79 74 65  iteMalloc( nByte
8daa0 20 29 3b 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e   );.  if( zFilen
8dab0 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  ame==0 ){.    re
8dac0 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 42  turn 0;.  }.  nB
8dad0 79 74 65 20 3d 20 57 69 64 65 43 68 61 72 54 6f  yte = WideCharTo
8dae0 4d 75 6c 74 69 42 79 74 65 28 43 50 5f 55 54 46  MultiByte(CP_UTF
8daf0 38 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c 65 6e  8, 0, zWideFilen
8db00 61 6d 65 2c 20 2d 31 2c 20 7a 46 69 6c 65 6e 61  ame, -1, zFilena
8db10 6d 65 2c 20 6e 42 79 74 65 2c 0a 20 20 20 20 20  me, nByte,.     
8db20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8db30 20 20 20 20 20 20 20 20 20 30 2c 20 30 29 3b 0a           0, 0);.
8db40 20 20 69 66 28 20 6e 42 79 74 65 20 3d 3d 20 30    if( nByte == 0
8db50 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 46 72   ){.    sqliteFr
8db60 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  ee(zFilename);. 
8db70 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 30     zFilename = 0
8db80 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a  ;.  }.  return z
8db90 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a  Filename;.}../*.
8dba0 2a 2a 20 43 6f 6e 76 65 72 74 20 61 6e 20 61 6e  ** Convert an an
8dbb0 73 69 20 73 74 72 69 6e 67 20 74 6f 20 6d 69 63  si string to mic
8dbc0 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 2c 20  rosoft unicode, 
8dbd0 62 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20  based on the.** 
8dbe0 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61 67 65  current codepage
8dbf0 20 73 65 74 74 69 6e 67 73 20 66 6f 72 20 66 69   settings for fi
8dc00 6c 65 20 61 70 69 73 2e 0a 2a 2a 20 0a 2a 2a 20  le apis..** .** 
8dc10 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68  Space to hold th
8dc20 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e  e returned strin
8dc30 67 20 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a  g is obtained.**
8dc40 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c   from sqliteMall
8dc50 6f 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 57 43  oc..*/.static WC
8dc60 48 41 52 20 2a 6d 62 63 73 54 6f 55 6e 69 63 6f  HAR *mbcsToUnico
8dc70 64 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  de(const char *z
8dc80 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74  Filename){.  int
8dc90 20 6e 42 79 74 65 3b 0a 20 20 57 43 48 41 52 20   nByte;.  WCHAR 
8dca0 2a 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 3b 0a  *zMbcsFilename;.
8dcb0 20 20 69 6e 74 20 63 6f 64 65 70 61 67 65 20 3d    int codepage =
8dcc0 20 41 72 65 46 69 6c 65 41 70 69 73 41 4e 53 49   AreFileApisANSI
8dcd0 28 29 20 3f 20 43 50 5f 41 43 50 20 3a 20 43 50  () ? CP_ACP : CP
8dce0 5f 4f 45 4d 43 50 3b 0a 0a 20 20 6e 42 79 74 65  _OEMCP;..  nByte
8dcf0 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f 57 69   = MultiByteToWi
8dd00 64 65 43 68 61 72 28 63 6f 64 65 70 61 67 65 2c  deChar(codepage,
8dd10 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d   0, zFilename, -
8dd20 31 2c 20 4e 55 4c 4c 2c 30 29 2a 73 69 7a 65 6f  1, NULL,0)*sizeo
8dd30 66 28 57 43 48 41 52 29 3b 0a 20 20 7a 4d 62 63  f(WCHAR);.  zMbc
8dd40 73 46 69 6c 65 6e 61 6d 65 20 3d 20 73 71 6c 69  sFilename = sqli
8dd50 74 65 4d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 2a  teMalloc( nByte*
8dd60 73 69 7a 65 6f 66 28 7a 4d 62 63 73 46 69 6c 65  sizeof(zMbcsFile
8dd70 6e 61 6d 65 5b 30 5d 29 20 29 3b 0a 20 20 69 66  name[0]) );.  if
8dd80 28 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 3d  ( zMbcsFilename=
8dd90 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
8dda0 20 30 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65 20   0;.  }.  nByte 
8ddb0 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f 57 69 64  = MultiByteToWid
8ddc0 65 43 68 61 72 28 63 6f 64 65 70 61 67 65 2c 20  eChar(codepage, 
8ddd0 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 31  0, zFilename, -1
8dde0 2c 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 2c  , zMbcsFilename,
8ddf0 20 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20 6e   nByte);.  if( n
8de00 42 79 74 65 3d 3d 30 20 29 7b 0a 20 20 20 20 73  Byte==0 ){.    s
8de10 71 6c 69 74 65 46 72 65 65 28 7a 4d 62 63 73 46  qliteFree(zMbcsF
8de20 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 4d  ilename);.    zM
8de30 62 63 73 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b  bcsFilename = 0;
8de40 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4d  .  }.  return zM
8de50 62 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a  bcsFilename;.}..
8de60 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 69  /*.** Convert mi
8de70 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 20  crosoft unicode 
8de80 74 6f 20 6d 75 6c 74 69 62 79 74 65 20 63 68 61  to multibyte cha
8de90 72 61 63 74 65 72 20 73 74 72 69 6e 67 2c 20 62  racter string, b
8dea0 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 75  ased on the.** u
8deb0 73 65 72 27 73 20 41 6e 73 69 20 63 6f 64 65 70  ser's Ansi codep
8dec0 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65  age..**.** Space
8ded0 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74   to hold the ret
8dee0 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20  urned string is 
8def0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a  obtained from.**
8df00 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e   sqliteMalloc().
8df10 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20  .*/.static char 
8df20 2a 75 6e 69 63 6f 64 65 54 6f 4d 62 63 73 28 63  *unicodeToMbcs(c
8df30 6f 6e 73 74 20 57 43 48 41 52 20 2a 7a 57 69 64  onst WCHAR *zWid
8df40 65 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e  eFilename){.  in
8df50 74 20 6e 42 79 74 65 3b 0a 20 20 63 68 61 72 20  t nByte;.  char 
8df60 2a 7a 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 69 6e  *zFilename;.  in
8df70 74 20 63 6f 64 65 70 61 67 65 20 3d 20 41 72 65  t codepage = Are
8df80 46 69 6c 65 41 70 69 73 41 4e 53 49 28 29 20 3f  FileApisANSI() ?
8df90 20 43 50 5f 41 43 50 20 3a 20 43 50 5f 4f 45 4d   CP_ACP : CP_OEM
8dfa0 43 50 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 57  CP;..  nByte = W
8dfb0 69 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79  ideCharToMultiBy
8dfc0 74 65 28 63 6f 64 65 70 61 67 65 2c 20 30 2c 20  te(codepage, 0, 
8dfd0 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d  zWideFilename, -
8dfe0 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a  1, 0, 0, 0, 0);.
8dff0 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 73 71    zFilename = sq
8e000 6c 69 74 65 4d 61 6c 6c 6f 63 28 20 6e 42 79 74  liteMalloc( nByt
8e010 65 20 29 3b 0a 20 20 69 66 28 20 7a 46 69 6c 65  e );.  if( zFile
8e020 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72  name==0 ){.    r
8e030 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e  eturn 0;.  }.  n
8e040 42 79 74 65 20 3d 20 57 69 64 65 43 68 61 72 54  Byte = WideCharT
8e050 6f 4d 75 6c 74 69 42 79 74 65 28 63 6f 64 65 70  oMultiByte(codep
8e060 61 67 65 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c  age, 0, zWideFil
8e070 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a 46 69 6c 65  ename, -1, zFile
8e080 6e 61 6d 65 2c 20 6e 42 79 74 65 2c 0a 20 20 20  name, nByte,.   
8e090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8e0a0 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 30 29             0, 0)
8e0b0 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 20 3d 3d  ;.  if( nByte ==
8e0c0 20 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65   0 ){.    sqlite
8e0d0 46 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b  Free(zFilename);
8e0e0 0a 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d  .    zFilename =
8e0f0 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   0;.  }.  return
8e100 20 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f   zFilename;.}../
8e110 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 75 6c  *.** Convert mul
8e120 74 69 62 79 74 65 20 63 68 61 72 61 63 74 65 72  tibyte character
8e130 20 73 74 72 69 6e 67 20 74 6f 20 55 54 46 2d 38   string to UTF-8
8e140 2e 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64  .  Space to hold
8e150 20 74 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64   the.** returned
8e160 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69   string is obtai
8e170 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d  ned from sqliteM
8e180 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74  alloc()..*/.stat
8e190 69 63 20 63 68 61 72 20 2a 6d 62 63 73 54 6f 55  ic char *mbcsToU
8e1a0 74 66 38 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  tf8(const char *
8e1b0 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 63 68  zFilename){.  ch
8e1c0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 55 74 66  ar *zFilenameUtf
8e1d0 38 3b 0a 20 20 57 43 48 41 52 20 2a 7a 54 6d 70  8;.  WCHAR *zTmp
8e1e0 57 69 64 65 3b 0a 0a 20 20 7a 54 6d 70 57 69 64  Wide;..  zTmpWid
8e1f0 65 20 3d 20 6d 62 63 73 54 6f 55 6e 69 63 6f 64  e = mbcsToUnicod
8e200 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20  e(zFilename);.  
8e210 69 66 28 20 7a 54 6d 70 57 69 64 65 3d 3d 30 20  if( zTmpWide==0 
8e220 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
8e230 0a 20 20 7d 0a 20 20 7a 46 69 6c 65 6e 61 6d 65  .  }.  zFilename
8e240 55 74 66 38 20 3d 20 75 6e 69 63 6f 64 65 54 6f  Utf8 = unicodeTo
8e250 55 74 66 38 28 7a 54 6d 70 57 69 64 65 29 3b 0a  Utf8(zTmpWide);.
8e260 20 20 73 71 6c 69 74 65 46 72 65 65 28 7a 54 6d    sqliteFree(zTm
8e270 70 57 69 64 65 29 3b 0a 20 20 72 65 74 75 72 6e  pWide);.  return
8e280 20 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38 3b 0a   zFilenameUtf8;.
8e290 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74  }../*.** Convert
8e2a0 20 55 54 46 2d 38 20 74 6f 20 6d 75 6c 74 69 62   UTF-8 to multib
8e2b0 79 74 65 20 63 68 61 72 61 63 74 65 72 20 73 74  yte character st
8e2c0 72 69 6e 67 2e 20 20 53 70 61 63 65 20 74 6f 20  ring.  Space to 
8e2d0 68 6f 6c 64 20 74 68 65 20 0a 2a 2a 20 72 65 74  hold the .** ret
8e2e0 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20  urned string is 
8e2f0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71  obtained from sq
8e300 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f  liteMalloc()..*/
8e310 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 75 74  .static char *ut
8e320 66 38 54 6f 4d 62 63 73 28 63 6f 6e 73 74 20 63  f8ToMbcs(const c
8e330 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b  har *zFilename){
8e340 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  .  char *zFilena
8e350 6d 65 4d 62 63 73 3b 0a 20 20 57 43 48 41 52 20  meMbcs;.  WCHAR 
8e360 2a 7a 54 6d 70 57 69 64 65 3b 0a 0a 20 20 7a 54  *zTmpWide;..  zT
8e370 6d 70 57 69 64 65 20 3d 20 75 74 66 38 54 6f 55  mpWide = utf8ToU
8e380 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65  nicode(zFilename
8e390 29 3b 0a 20 20 69 66 28 20 7a 54 6d 70 57 69 64  );.  if( zTmpWid
8e3a0 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  e==0 ){.    retu
8e3b0 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 46 69 6c  rn 0;.  }.  zFil
8e3c0 65 6e 61 6d 65 4d 62 63 73 20 3d 20 75 6e 69 63  enameMbcs = unic
8e3d0 6f 64 65 54 6f 4d 62 63 73 28 7a 54 6d 70 57 69  odeToMbcs(zTmpWi
8e3e0 64 65 29 3b 0a 20 20 73 71 6c 69 74 65 46 72 65  de);.  sqliteFre
8e3f0 65 28 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20 72  e(zTmpWide);.  r
8e400 65 74 75 72 6e 20 7a 46 69 6c 65 6e 61 6d 65 4d  eturn zFilenameM
8e410 62 63 73 3b 0a 7d 0a 0a 23 69 66 20 4f 53 5f 57  bcs;.}..#if OS_W
8e420 49 4e 43 45 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  INCE./**********
8e430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8e440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8e450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8e460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
8e470 2a 2a 20 54 68 69 73 20 73 65 63 74 69 6f 6e 20  ** This section 
8e480 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 66 6f  contains code fo
8e490 72 20 57 69 6e 43 45 20 6f 6e 6c 79 2e 0a 2a 2f  r WinCE only..*/
8e4a0 0a 2f 2a 0a 2a 2a 20 57 69 6e 64 6f 77 73 43 45  ./*.** WindowsCE
8e4b0 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 61   does not have a
8e4c0 20 6c 6f 63 61 6c 74 69 6d 65 28 29 20 66 75 6e   localtime() fun
8e4d0 63 74 69 6f 6e 2e 20 20 53 6f 20 63 72 65 61 74  ction.  So creat
8e4e0 65 20 61 0a 2a 2a 20 73 75 62 73 74 69 74 75 74  e a.** substitut
8e4f0 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 74 6d 20  e..*/.struct tm 
8e500 2a 5f 5f 63 64 65 63 6c 20 6c 6f 63 61 6c 74 69  *__cdecl localti
8e510 6d 65 28 63 6f 6e 73 74 20 74 69 6d 65 5f 74 20  me(const time_t 
8e520 2a 74 29 0a 7b 0a 20 20 73 74 61 74 69 63 20 73  *t).{.  static s
8e530 74 72 75 63 74 20 74 6d 20 79 3b 0a 20 20 46 49  truct tm y;.  FI
8e540 4c 45 54 49 4d 45 20 75 54 6d 2c 20 6c 54 6d 3b  LETIME uTm, lTm;
8e550 0a 20 20 53 59 53 54 45 4d 54 49 4d 45 20 70 54  .  SYSTEMTIME pT
8e560 6d 3b 0a 20 20 69 36 34 20 74 36 34 3b 0a 20 20  m;.  i64 t64;.  
8e570 74 36 34 20 3d 20 2a 74 3b 0a 20 20 74 36 34 20  t64 = *t;.  t64 
8e580 3d 20 28 74 36 34 20 2b 20 31 31 36 34 34 34 37  = (t64 + 1164447
8e590 33 36 30 30 29 2a 31 30 30 30 30 30 30 30 3b 0a  3600)*10000000;.
8e5a0 20 20 75 54 6d 2e 64 77 4c 6f 77 44 61 74 65 54    uTm.dwLowDateT
8e5b0 69 6d 65 20 3d 20 74 36 34 20 26 20 30 78 46 46  ime = t64 & 0xFF
8e5c0 46 46 46 46 46 46 3b 0a 20 20 75 54 6d 2e 64 77  FFFFFF;.  uTm.dw
8e5d0 48 69 67 68 44 61 74 65 54 69 6d 65 3d 20 74 36  HighDateTime= t6
8e5e0 34 20 3e 3e 20 33 32 3b 0a 20 20 46 69 6c 65 54  4 >> 32;.  FileT
8e5f0 69 6d 65 54 6f 4c 6f 63 61 6c 46 69 6c 65 54 69  imeToLocalFileTi
8e600 6d 65 28 26 75 54 6d 2c 26 6c 54 6d 29 3b 0a 20  me(&uTm,&lTm);. 
8e610 20 46 69 6c 65 54 69 6d 65 54 6f 53 79 73 74 65   FileTimeToSyste
8e620 6d 54 69 6d 65 28 26 6c 54 6d 2c 26 70 54 6d 29  mTime(&lTm,&pTm)
8e630 3b 0a 20 20 79 2e 74 6d 5f 79 65 61 72 20 3d 20  ;.  y.tm_year = 
8e640 70 54 6d 2e 77 59 65 61 72 20 2d 20 31 39 30 30  pTm.wYear - 1900
8e650 3b 0a 20 20 79 2e 74 6d 5f 6d 6f 6e 20 3d 20 70  ;.  y.tm_mon = p
8e660 54 6d 2e 77 4d 6f 6e 74 68 20 2d 20 31 3b 0a 20  Tm.wMonth - 1;. 
8e670 20 79 2e 74 6d 5f 77 64 61 79 20 3d 20 70 54 6d   y.tm_wday = pTm
8e680 2e 77 44 61 79 4f 66 57 65 65 6b 3b 0a 20 20 79  .wDayOfWeek;.  y
8e690 2e 74 6d 5f 6d 64 61 79 20 3d 20 70 54 6d 2e 77  .tm_mday = pTm.w
8e6a0 44 61 79 3b 0a 20 20 79 2e 74 6d 5f 68 6f 75 72  Day;.  y.tm_hour
8e6b0 20 3d 20 70 54 6d 2e 77 48 6f 75 72 3b 0a 20 20   = pTm.wHour;.  
8e6c0 79 2e 74 6d 5f 6d 69 6e 20 3d 20 70 54 6d 2e 77  y.tm_min = pTm.w
8e6d0 4d 69 6e 75 74 65 3b 0a 20 20 79 2e 74 6d 5f 73  Minute;.  y.tm_s
8e6e0 65 63 20 3d 20 70 54 6d 2e 77 53 65 63 6f 6e 64  ec = pTm.wSecond
8e6f0 3b 0a 20 20 72 65 74 75 72 6e 20 26 79 3b 0a 7d  ;.  return &y;.}
8e700 0a 0a 2f 2a 20 54 68 69 73 20 77 69 6c 6c 20 6e  ../* This will n
8e710 65 76 65 72 20 62 65 20 63 61 6c 6c 65 64 2c 20  ever be called, 
8e720 62 75 74 20 64 65 66 69 6e 65 64 20 74 6f 20 6d  but defined to m
8e730 61 6b 65 20 74 68 65 20 63 6f 64 65 20 63 6f 6d  ake the code com
8e740 70 69 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  pile */.#define 
8e750 47 65 74 54 65 6d 70 50 61 74 68 41 28 61 2c 62  GetTempPathA(a,b
8e760 29 0a 0a 23 64 65 66 69 6e 65 20 4c 6f 63 6b 46  )..#define LockF
8e770 69 6c 65 28 61 2c 62 2c 63 2c 64 2c 65 29 20 20  ile(a,b,c,d,e)  
8e780 20 20 20 20 20 77 69 6e 63 65 4c 6f 63 6b 46 69       winceLockFi
8e790 6c 65 28 26 61 2c 20 62 2c 20 63 2c 20 64 2c 20  le(&a, b, c, d, 
8e7a0 65 29 0a 23 64 65 66 69 6e 65 20 55 6e 6c 6f 63  e).#define Unloc
8e7b0 6b 46 69 6c 65 28 61 2c 62 2c 63 2c 64 2c 65 29  kFile(a,b,c,d,e)
8e7c0 20 20 20 20 20 77 69 6e 63 65 55 6e 6c 6f 63 6b       winceUnlock
8e7d0 46 69 6c 65 28 26 61 2c 20 62 2c 20 63 2c 20 64  File(&a, b, c, d
8e7e0 2c 20 65 29 0a 23 64 65 66 69 6e 65 20 4c 6f 63  , e).#define Loc
8e7f0 6b 46 69 6c 65 45 78 28 61 2c 62 2c 63 2c 64 2c  kFileEx(a,b,c,d,
8e800 65 2c 66 29 20 20 20 77 69 6e 63 65 4c 6f 63 6b  e,f)   winceLock
8e810 46 69 6c 65 45 78 28 26 61 2c 20 62 2c 20 63 2c  FileEx(&a, b, c,
8e820 20 64 2c 20 65 2c 20 66 29 0a 0a 23 64 65 66 69   d, e, f)..#defi
8e830 6e 65 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e  ne HANDLE_TO_WIN
8e840 46 49 4c 45 28 61 29 20 28 77 69 6e 46 69 6c 65  FILE(a) (winFile
8e850 2a 29 26 28 28 63 68 61 72 2a 29 61 29 5b 2d 6f  *)&((char*)a)[-o
8e860 66 66 73 65 74 6f 66 28 77 69 6e 46 69 6c 65 2c  ffsetof(winFile,
8e870 68 29 5d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69  h)]../*.** Acqui
8e880 72 65 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  re a lock on the
8e890 20 68 61 6e 64 6c 65 20 68 0a 2a 2f 0a 73 74 61   handle h.*/.sta
8e8a0 74 69 63 20 76 6f 69 64 20 77 69 6e 63 65 4d 75  tic void winceMu
8e8b0 74 65 78 41 63 71 75 69 72 65 28 48 41 4e 44 4c  texAcquire(HANDL
8e8c0 45 20 68 29 7b 0a 20 20 20 44 57 4f 52 44 20 64  E h){.   DWORD d
8e8d0 77 45 72 72 3b 0a 20 20 20 64 6f 20 7b 0a 20 20  wErr;.   do {.  
8e8e0 20 20 20 64 77 45 72 72 20 3d 20 57 61 69 74 46     dwErr = WaitF
8e8f0 6f 72 53 69 6e 67 6c 65 4f 62 6a 65 63 74 28 68  orSingleObject(h
8e900 2c 20 49 4e 46 49 4e 49 54 45 29 3b 0a 20 20 20  , INFINITE);.   
8e910 7d 20 77 68 69 6c 65 20 28 64 77 45 72 72 20 21  } while (dwErr !
8e920 3d 20 57 41 49 54 5f 4f 42 4a 45 43 54 5f 30 20  = WAIT_OBJECT_0 
8e930 26 26 20 64 77 45 72 72 20 21 3d 20 57 41 49 54  && dwErr != WAIT
8e940 5f 41 42 41 4e 44 4f 4e 45 44 29 3b 0a 7d 0a 2f  _ABANDONED);.}./
8e950 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 6c  *.** Release a l
8e960 6f 63 6b 20 61 63 71 75 69 72 65 64 20 62 79 20  ock acquired by 
8e970 77 69 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72  winceMutexAcquir
8e980 65 28 29 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 77  e().*/.#define w
8e990 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 73 65  inceMutexRelease
8e9a0 28 68 29 20 52 65 6c 65 61 73 65 4d 75 74 65 78  (h) ReleaseMutex
8e9b0 28 68 29 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74  (h)../*.** Creat
8e9c0 65 20 74 68 65 20 6d 75 74 65 78 20 61 6e 64 20  e the mutex and 
8e9d0 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 75 73  shared memory us
8e9e0 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 69  ed for locking i
8e9f0 6e 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20 64 65  n the file.** de
8ea00 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 0a 2a  scriptor pFile.*
8ea10 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20 77 69  /.static BOOL wi
8ea20 6e 63 65 43 72 65 61 74 65 4c 6f 63 6b 28 63 6f  nceCreateLock(co
8ea30 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  nst char *zFilen
8ea40 61 6d 65 2c 20 77 69 6e 46 69 6c 65 20 2a 70 46  ame, winFile *pF
8ea50 69 6c 65 29 7b 0a 20 20 57 43 48 41 52 20 2a 7a  ile){.  WCHAR *z
8ea60 54 6f 6b 3b 0a 20 20 57 43 48 41 52 20 2a 7a 4e  Tok;.  WCHAR *zN
8ea70 61 6d 65 20 3d 20 75 74 66 38 54 6f 55 6e 69 63  ame = utf8ToUnic
8ea80 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a  ode(zFilename);.
8ea90 20 20 42 4f 4f 4c 20 62 49 6e 69 74 20 3d 20 54    BOOL bInit = T
8eaa0 52 55 45 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69  RUE;..  /* Initi
8eab0 61 6c 69 7a 65 20 74 68 65 20 6c 6f 63 61 6c 20  alize the local 
8eac0 6c 6f 63 6b 64 61 74 61 20 2a 2f 0a 20 20 5a 65  lockdata */.  Ze
8ead0 72 6f 4d 65 6d 6f 72 79 28 26 70 46 69 6c 65 2d  roMemory(&pFile-
8eae0 3e 6c 6f 63 61 6c 2c 20 73 69 7a 65 6f 66 28 70  >local, sizeof(p
8eaf0 46 69 6c 65 2d 3e 6c 6f 63 61 6c 29 29 3b 0a 0a  File->local));..
8eb00 20 20 2f 2a 20 52 65 70 6c 61 63 65 20 74 68 65    /* Replace the
8eb10 20 62 61 63 6b 73 6c 61 73 68 65 73 20 66 72 6f   backslashes fro
8eb20 6d 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 61  m the filename a
8eb30 6e 64 20 6c 6f 77 65 72 63 61 73 65 20 69 74 0a  nd lowercase it.
8eb40 20 20 2a 2a 20 74 6f 20 64 65 72 69 76 65 20 61    ** to derive a
8eb50 20 6d 75 74 65 78 20 6e 61 6d 65 2e 20 2a 2f 0a   mutex name. */.
8eb60 20 20 7a 54 6f 6b 20 3d 20 43 68 61 72 4c 6f 77    zTok = CharLow
8eb70 65 72 57 28 7a 4e 61 6d 65 29 3b 0a 20 20 66 6f  erW(zName);.  fo
8eb80 72 20 28 3b 2a 7a 54 6f 6b 3b 7a 54 6f 6b 2b 2b  r (;*zTok;zTok++
8eb90 29 7b 0a 20 20 20 20 69 66 20 28 2a 7a 54 6f 6b  ){.    if (*zTok
8eba0 20 3d 3d 20 27 5c 5c 27 29 20 2a 7a 54 6f 6b 20   == '\\') *zTok 
8ebb0 3d 20 27 5f 27 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  = '_';.  }..  /*
8ebc0 20 43 72 65 61 74 65 2f 6f 70 65 6e 20 74 68 65   Create/open the
8ebd0 20 6e 61 6d 65 64 20 6d 75 74 65 78 20 2a 2f 0a   named mutex */.
8ebe0 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 20    pFile->hMutex 
8ebf0 3d 20 43 72 65 61 74 65 4d 75 74 65 78 57 28 4e  = CreateMutexW(N
8ec00 55 4c 4c 2c 20 46 41 4c 53 45 2c 20 7a 4e 61 6d  ULL, FALSE, zNam
8ec10 65 29 3b 0a 20 20 69 66 20 28 21 70 46 69 6c 65  e);.  if (!pFile
8ec20 2d 3e 68 4d 75 74 65 78 29 7b 0a 20 20 20 20 73  ->hMutex){.    s
8ec30 71 6c 69 74 65 46 72 65 65 28 7a 4e 61 6d 65 29  qliteFree(zName)
8ec40 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 46 41 4c  ;.    return FAL
8ec50 53 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63  SE;.  }..  /* Ac
8ec60 71 75 69 72 65 20 74 68 65 20 6d 75 74 65 78 20  quire the mutex 
8ec70 62 65 66 6f 72 65 20 63 6f 6e 74 69 6e 75 69 6e  before continuin
8ec80 67 20 2a 2f 0a 20 20 77 69 6e 63 65 4d 75 74 65  g */.  winceMute
8ec90 78 41 63 71 75 69 72 65 28 70 46 69 6c 65 2d 3e  xAcquire(pFile->
8eca0 68 4d 75 74 65 78 29 3b 0a 20 20 0a 20 20 2f 2a  hMutex);.  .  /*
8ecb0 20 53 69 6e 63 65 20 74 68 65 20 6e 61 6d 65 73   Since the names
8ecc0 20 6f 66 20 6e 61 6d 65 64 20 6d 75 74 65 78 65   of named mutexe
8ecd0 73 2c 20 73 65 6d 61 70 68 6f 72 65 73 2c 20 66  s, semaphores, f
8ece0 69 6c 65 20 6d 61 70 70 69 6e 67 73 20 65 74 63  ile mappings etc
8ecf0 20 61 72 65 20 0a 20 20 2a 2a 20 63 61 73 65 2d   are .  ** case-
8ed00 73 65 6e 73 69 74 69 76 65 2c 20 74 61 6b 65 20  sensitive, take 
8ed10 61 64 76 61 6e 74 61 67 65 20 6f 66 20 74 68 61  advantage of tha
8ed20 74 20 62 79 20 75 70 70 65 72 63 61 73 69 6e 67  t by uppercasing
8ed30 20 74 68 65 20 6d 75 74 65 78 20 6e 61 6d 65 0a   the mutex name.
8ed40 20 20 2a 2a 20 61 6e 64 20 75 73 69 6e 67 20 74    ** and using t
8ed50 68 61 74 20 61 73 20 74 68 65 20 73 68 61 72 65  hat as the share
8ed60 64 20 66 69 6c 65 6d 61 70 70 69 6e 67 20 6e 61  d filemapping na
8ed70 6d 65 2e 0a 20 20 2a 2f 0a 20 20 43 68 61 72 55  me..  */.  CharU
8ed80 70 70 65 72 57 28 7a 4e 61 6d 65 29 3b 0a 20 20  pperW(zName);.  
8ed90 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d  pFile->hShared =
8eda0 20 43 72 65 61 74 65 46 69 6c 65 4d 61 70 70 69   CreateFileMappi
8edb0 6e 67 57 28 49 4e 56 41 4c 49 44 5f 48 41 4e 44  ngW(INVALID_HAND
8edc0 4c 45 5f 56 41 4c 55 45 2c 20 4e 55 4c 4c 2c 0a  LE_VALUE, NULL,.
8edd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8ede0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8edf0 20 20 20 20 20 20 20 50 41 47 45 5f 52 45 41 44         PAGE_READ
8ee00 57 52 49 54 45 2c 20 30 2c 20 73 69 7a 65 6f 66  WRITE, 0, sizeof
8ee10 28 77 69 6e 63 65 4c 6f 63 6b 29 2c 0a 20 20 20  (winceLock),.   
8ee20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8ee30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8ee40 20 20 20 20 7a 4e 61 6d 65 29 3b 20 20 0a 0a 20      zName);  .. 
8ee50 20 2f 2a 20 53 65 74 20 61 20 66 6c 61 67 20 74   /* Set a flag t
8ee60 68 61 74 20 69 6e 64 69 63 61 74 65 73 20 77 65  hat indicates we
8ee70 27 72 65 20 74 68 65 20 66 69 72 73 74 20 74 6f  're the first to
8ee80 20 63 72 65 61 74 65 20 74 68 65 20 6d 65 6d 6f   create the memo
8ee90 72 79 20 73 6f 20 69 74 20 0a 20 20 2a 2a 20 6d  ry so it .  ** m
8eea0 75 73 74 20 62 65 20 7a 65 72 6f 2d 69 6e 69 74  ust be zero-init
8eeb0 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 66 20  ialized */.  if 
8eec0 28 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 20  (GetLastError() 
8eed0 3d 3d 20 45 52 52 4f 52 5f 41 4c 52 45 41 44 59  == ERROR_ALREADY
8eee0 5f 45 58 49 53 54 53 29 7b 0a 20 20 20 20 62 49  _EXISTS){.    bI
8eef0 6e 69 74 20 3d 20 46 41 4c 53 45 3b 0a 20 20 7d  nit = FALSE;.  }
8ef00 0a 0a 20 20 73 71 6c 69 74 65 46 72 65 65 28 7a  ..  sqliteFree(z
8ef10 4e 61 6d 65 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  Name);..  /* If 
8ef20 77 65 20 73 75 63 63 65 65 64 65 64 20 69 6e 20  we succeeded in 
8ef30 6d 61 6b 69 6e 67 20 74 68 65 20 73 68 61 72 65  making the share
8ef40 64 20 6d 65 6d 6f 72 79 20 68 61 6e 64 6c 65 2c  d memory handle,
8ef50 20 6d 61 70 20 69 74 2e 20 2a 2f 0a 20 20 69 66   map it. */.  if
8ef60 20 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64   (pFile->hShared
8ef70 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 73 68  ){.    pFile->sh
8ef80 61 72 65 64 20 3d 20 28 77 69 6e 63 65 4c 6f 63  ared = (winceLoc
8ef90 6b 2a 29 4d 61 70 56 69 65 77 4f 66 46 69 6c 65  k*)MapViewOfFile
8efa0 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 2c  (pFile->hShared,
8efb0 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 46   .             F
8efc0 49 4c 45 5f 4d 41 50 5f 52 45 41 44 7c 46 49 4c  ILE_MAP_READ|FIL
8efd0 45 5f 4d 41 50 5f 57 52 49 54 45 2c 20 30 2c 20  E_MAP_WRITE, 0, 
8efe0 30 2c 20 73 69 7a 65 6f 66 28 77 69 6e 63 65 4c  0, sizeof(winceL
8eff0 6f 63 6b 29 29 3b 0a 20 20 20 20 2f 2a 20 49 66  ock));.    /* If
8f000 20 6d 61 70 70 69 6e 67 20 66 61 69 6c 65 64 2c   mapping failed,
8f010 20 63 6c 6f 73 65 20 74 68 65 20 73 68 61 72 65   close the share
8f020 64 20 6d 65 6d 6f 72 79 20 68 61 6e 64 6c 65 20  d memory handle 
8f030 61 6e 64 20 65 72 61 73 65 20 69 74 20 2a 2f 0a  and erase it */.
8f040 20 20 20 20 69 66 20 28 21 70 46 69 6c 65 2d 3e      if (!pFile->
8f050 73 68 61 72 65 64 29 7b 0a 20 20 20 20 20 20 43  shared){.      C
8f060 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c 65  loseHandle(pFile
8f070 2d 3e 68 53 68 61 72 65 64 29 3b 0a 20 20 20 20  ->hShared);.    
8f080 20 20 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64    pFile->hShared
8f090 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20   = NULL;.    }. 
8f0a0 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 73 68 61 72   }..  /* If shar
8f0b0 65 64 20 6d 65 6d 6f 72 79 20 63 6f 75 6c 64 20  ed memory could 
8f0c0 6e 6f 74 20 62 65 20 63 72 65 61 74 65 64 2c 20  not be created, 
8f0d0 74 68 65 6e 20 63 6c 6f 73 65 20 74 68 65 20 6d  then close the m
8f0e0 75 74 65 78 20 61 6e 64 20 66 61 69 6c 20 2a 2f  utex and fail */
8f0f0 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 68 53  .  if (pFile->hS
8f100 68 61 72 65 64 20 3d 3d 20 4e 55 4c 4c 29 7b 0a  hared == NULL){.
8f110 20 20 20 20 77 69 6e 63 65 4d 75 74 65 78 52 65      winceMutexRe
8f120 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75  lease(pFile->hMu
8f130 74 65 78 29 3b 0a 20 20 20 20 43 6c 6f 73 65 48  tex);.    CloseH
8f140 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 4d 75  andle(pFile->hMu
8f150 74 65 78 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d  tex);.    pFile-
8f160 3e 68 4d 75 74 65 78 20 3d 20 4e 55 4c 4c 3b 0a  >hMutex = NULL;.
8f170 20 20 20 20 72 65 74 75 72 6e 20 46 41 4c 53 45      return FALSE
8f180 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 49 6e  ;.  }.  .  /* In
8f190 69 74 69 61 6c 69 7a 65 20 74 68 65 20 73 68 61  itialize the sha
8f1a0 72 65 64 20 6d 65 6d 6f 72 79 20 69 66 20 77 65  red memory if we
8f1b0 27 72 65 20 73 75 70 70 6f 73 65 64 20 74 6f 20  're supposed to 
8f1c0 2a 2f 0a 20 20 69 66 20 28 62 49 6e 69 74 29 20  */.  if (bInit) 
8f1d0 7b 0a 20 20 20 20 5a 65 72 6f 4d 65 6d 6f 72 79  {.    ZeroMemory
8f1e0 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2c 20  (pFile->shared, 
8f1f0 73 69 7a 65 6f 66 28 77 69 6e 63 65 4c 6f 63 6b  sizeof(winceLock
8f200 29 29 3b 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 65  ));.  }..  wince
8f210 4d 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 69  MutexRelease(pFi
8f220 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 72  le->hMutex);.  r
8f230 65 74 75 72 6e 20 54 52 55 45 3b 0a 7d 0a 0a 2f  eturn TRUE;.}../
8f240 2a 0a 2a 2a 20 44 65 73 74 72 6f 79 20 74 68 65  *.** Destroy the
8f250 20 70 61 72 74 20 6f 66 20 77 69 6e 46 69 6c 65   part of winFile
8f260 20 74 68 61 74 20 64 65 61 6c 73 20 77 69 74 68   that deals with
8f270 20 77 69 6e 63 65 20 6c 6f 63 6b 73 0a 2a 2f 0a   wince locks.*/.
8f280 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 63  static void winc
8f290 65 44 65 73 74 72 6f 79 4c 6f 63 6b 28 77 69 6e  eDestroyLock(win
8f2a0 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20  File *pFile){.  
8f2b0 69 66 20 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65  if (pFile->hMute
8f2c0 78 29 7b 0a 20 20 20 20 2f 2a 20 41 63 71 75 69  x){.    /* Acqui
8f2d0 72 65 20 74 68 65 20 6d 75 74 65 78 20 2a 2f 0a  re the mutex */.
8f2e0 20 20 20 20 77 69 6e 63 65 4d 75 74 65 78 41 63      winceMutexAc
8f2f0 71 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d 75  quire(pFile->hMu
8f300 74 65 78 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68  tex);..    /* Th
8f310 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63  e following bloc
8f320 6b 73 20 73 68 6f 75 6c 64 20 70 72 6f 62 61 62  ks should probab
8f330 6c 79 20 61 73 73 65 72 74 20 69 6e 20 64 65 62  ly assert in deb
8f340 75 67 20 6d 6f 64 65 2c 20 62 75 74 20 74 68 65  ug mode, but the
8f350 79 0a 20 20 20 20 20 20 20 61 72 65 20 74 6f 20  y.       are to 
8f360 63 6c 65 61 6e 75 70 20 69 6e 20 63 61 73 65 20  cleanup in case 
8f370 61 6e 79 20 6c 6f 63 6b 73 20 72 65 6d 61 69 6e  any locks remain
8f380 65 64 20 6f 70 65 6e 20 2a 2f 0a 20 20 20 20 69  ed open */.    i
8f390 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e  f (pFile->local.
8f3a0 6e 52 65 61 64 65 72 73 29 7b 0a 20 20 20 20 20  nReaders){.     
8f3b0 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e   pFile->shared->
8f3c0 6e 52 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20 20  nReaders --;.   
8f3d0 20 7d 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65   }.    if (pFile
8f3e0 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72 76 65  ->local.bReserve
8f3f0 64 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d  d){.      pFile-
8f400 3e 73 68 61 72 65 64 2d 3e 62 52 65 73 65 72 76  >shared->bReserv
8f410 65 64 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20  ed = FALSE;.    
8f420 7d 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d  }.    if (pFile-
8f430 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 29  >local.bPending)
8f440 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73  {.      pFile->s
8f450 68 61 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67 20  hared->bPending 
8f460 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 7d 0a 20  = FALSE;.    }. 
8f470 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f     if (pFile->lo
8f480 63 61 6c 2e 62 45 78 63 6c 75 73 69 76 65 29 7b  cal.bExclusive){
8f490 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68  .      pFile->sh
8f4a0 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 65  ared->bExclusive
8f4b0 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 7d 0a   = FALSE;.    }.
8f4c0 0a 20 20 20 20 2f 2a 20 44 65 2d 72 65 66 65 72  .    /* De-refer
8f4d0 65 6e 63 65 20 61 6e 64 20 63 6c 6f 73 65 20 6f  ence and close o
8f4e0 75 72 20 63 6f 70 79 20 6f 66 20 74 68 65 20 73  ur copy of the s
8f4f0 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 68 61 6e  hared memory han
8f500 64 6c 65 20 2a 2f 0a 20 20 20 20 55 6e 6d 61 70  dle */.    Unmap
8f510 56 69 65 77 4f 66 46 69 6c 65 28 70 46 69 6c 65  ViewOfFile(pFile
8f520 2d 3e 73 68 61 72 65 64 29 3b 0a 20 20 20 20 43  ->shared);.    C
8f530 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c 65  loseHandle(pFile
8f540 2d 3e 68 53 68 61 72 65 64 29 3b 0a 0a 20 20 20  ->hShared);..   
8f550 20 69 66 28 20 70 46 69 6c 65 2d 3e 7a 44 65 6c   if( pFile->zDel
8f560 65 74 65 4f 6e 43 6c 6f 73 65 20 29 7b 0a 20 20  eteOnClose ){.  
8f570 20 20 20 20 44 65 6c 65 74 65 46 69 6c 65 57 28      DeleteFileW(
8f580 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e  pFile->zDeleteOn
8f590 43 6c 6f 73 65 29 3b 0a 20 20 20 20 20 20 73 71  Close);.      sq
8f5a0 6c 69 74 65 46 72 65 65 28 70 46 69 6c 65 2d 3e  liteFree(pFile->
8f5b0 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 29 3b  zDeleteOnClose);
8f5c0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 7a 44  .      pFile->zD
8f5d0 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 20 3d 20 30  eleteOnClose = 0
8f5e0 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
8f5f0 44 6f 6e 65 20 77 69 74 68 20 74 68 65 20 6d 75  Done with the mu
8f600 74 65 78 20 2a 2f 0a 20 20 20 20 77 69 6e 63 65  tex */.    wince
8f610 4d 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 69  MutexRelease(pFi
8f620 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 20 20 20 20  le->hMutex);    
8f630 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65  .    CloseHandle
8f640 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b  (pFile->hMutex);
8f650 0a 20 20 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74  .    pFile->hMut
8f660 65 78 20 3d 20 4e 55 4c 4c 3b 0a 20 20 7d 0a 7d  ex = NULL;.  }.}
8f670 0a 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 69 6d 70 6c  ../* .** An impl
8f680 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
8f690 65 20 4c 6f 63 6b 46 69 6c 65 28 29 20 41 50 49  e LockFile() API
8f6a0 20 6f 66 20 77 69 6e 64 6f 77 73 20 66 6f 72 20   of windows for 
8f6b0 77 69 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20  wince.*/.static 
8f6c0 42 4f 4f 4c 20 77 69 6e 63 65 4c 6f 63 6b 46 69  BOOL winceLockFi
8f6d0 6c 65 28 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68  le(.  HANDLE *ph
8f6e0 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64 77  File,.  DWORD dw
8f6f0 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 2c 0a 20  FileOffsetLow,. 
8f700 20 44 57 4f 52 44 20 64 77 46 69 6c 65 4f 66 66   DWORD dwFileOff
8f710 73 65 74 48 69 67 68 2c 0a 20 20 44 57 4f 52 44  setHigh,.  DWORD
8f720 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54   nNumberOfBytesT
8f730 6f 4c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52  oLockLow,.  DWOR
8f740 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73  D nNumberOfBytes
8f750 54 6f 4c 6f 63 6b 48 69 67 68 0a 29 7b 0a 20 20  ToLockHigh.){.  
8f760 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  winFile *pFile =
8f770 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 49   HANDLE_TO_WINFI
8f780 4c 45 28 70 68 46 69 6c 65 29 3b 0a 20 20 42 4f  LE(phFile);.  BO
8f790 4f 4c 20 62 52 65 74 75 72 6e 20 3d 20 46 41 4c  OL bReturn = FAL
8f7a0 53 45 3b 0a 0a 20 20 69 66 20 28 21 70 46 69 6c  SE;..  if (!pFil
8f7b0 65 2d 3e 68 4d 75 74 65 78 29 20 72 65 74 75 72  e->hMutex) retur
8f7c0 6e 20 54 52 55 45 3b 0a 20 20 77 69 6e 63 65 4d  n TRUE;.  winceM
8f7d0 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69 6c  utexAcquire(pFil
8f7e0 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 20 2f  e->hMutex);..  /
8f7f0 2a 20 57 61 6e 74 69 6e 67 20 61 6e 20 65 78 63  * Wanting an exc
8f800 6c 75 73 69 76 65 20 6c 6f 63 6b 3f 20 2a 2f 0a  lusive lock? */.
8f810 20 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73    if (dwFileOffs
8f820 65 74 4c 6f 77 20 3d 3d 20 53 48 41 52 45 44 5f  etLow == SHARED_
8f830 46 49 52 53 54 0a 20 20 20 20 20 20 20 26 26 20  FIRST.       && 
8f840 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f  nNumberOfBytesTo
8f850 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 53 48 41 52 45  LockLow == SHARE
8f860 44 5f 53 49 5a 45 29 7b 0a 20 20 20 20 69 66 20  D_SIZE){.    if 
8f870 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e  (pFile->shared->
8f880 6e 52 65 61 64 65 72 73 20 3d 3d 20 30 20 26 26  nReaders == 0 &&
8f890 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e   pFile->shared->
8f8a0 62 45 78 63 6c 75 73 69 76 65 20 3d 3d 20 30 29  bExclusive == 0)
8f8b0 7b 0a 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  {.       pFile->
8f8c0 73 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69  shared->bExclusi
8f8d0 76 65 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 20  ve = TRUE;.     
8f8e0 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62    pFile->local.b
8f8f0 45 78 63 6c 75 73 69 76 65 20 3d 20 54 52 55 45  Exclusive = TRUE
8f900 3b 0a 20 20 20 20 20 20 20 62 52 65 74 75 72 6e  ;.       bReturn
8f910 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20   = TRUE;.    }. 
8f920 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20   }..  /* Want a 
8f930 72 65 61 64 2d 6f 6e 6c 79 20 6c 6f 63 6b 3f 20  read-only lock? 
8f940 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 28 64  */.  else if ((d
8f950 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3e  wFileOffsetLow >
8f960 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 26  = SHARED_FIRST &
8f970 26 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 77  &.            dw
8f980 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3c 20  FileOffsetLow < 
8f990 53 48 41 52 45 44 5f 46 49 52 53 54 20 2b 20 53  SHARED_FIRST + S
8f9a0 48 41 52 45 44 5f 53 49 5a 45 29 20 26 26 0a 20  HARED_SIZE) &&. 
8f9b0 20 20 20 20 20 20 20 20 20 20 20 6e 4e 75 6d 62             nNumb
8f9c0 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c  erOfBytesToLockL
8f9d0 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66  ow == 1){.    if
8f9e0 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d   (pFile->shared-
8f9f0 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 3d 20 30  >bExclusive == 0
8fa00 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  ){.      pFile->
8fa10 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 20 2b  local.nReaders +
8fa20 2b 3b 0a 20 20 20 20 20 20 69 66 20 28 70 46 69  +;.      if (pFi
8fa30 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65  le->local.nReade
8fa40 72 73 20 3d 3d 20 31 29 7b 0a 20 20 20 20 20 20  rs == 1){.      
8fa50 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d    pFile->shared-
8fa60 3e 6e 52 65 61 64 65 72 73 20 2b 2b 3b 0a 20 20  >nReaders ++;.  
8fa70 20 20 20 20 7d 0a 20 20 20 20 20 20 62 52 65 74      }.      bRet
8fa80 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20  urn = TRUE;.    
8fa90 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74  }.  }..  /* Want
8faa0 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 3f   a pending lock?
8fab0 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64   */.  else if (d
8fac0 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d  wFileOffsetLow =
8fad0 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 26  = PENDING_BYTE &
8fae0 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73  & nNumberOfBytes
8faf0 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b  ToLockLow == 1){
8fb00 0a 20 20 20 20 2f 2a 20 49 66 20 6e 6f 20 70 65  .    /* If no pe
8fb10 6e 64 69 6e 67 20 6c 6f 63 6b 20 68 61 73 20 62  nding lock has b
8fb20 65 65 6e 20 61 63 71 75 69 72 65 64 2c 20 74 68  een acquired, th
8fb30 65 6e 20 61 63 71 75 69 72 65 20 69 74 20 2a 2f  en acquire it */
8fb40 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e  .    if (pFile->
8fb50 73 68 61 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67  shared->bPending
8fb60 20 3d 3d 20 30 29 20 7b 0a 20 20 20 20 20 20 70   == 0) {.      p
8fb70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50  File->shared->bP
8fb80 65 6e 64 69 6e 67 20 3d 20 54 52 55 45 3b 0a 20  ending = TRUE;. 
8fb90 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61       pFile->loca
8fba0 6c 2e 62 50 65 6e 64 69 6e 67 20 3d 20 54 52 55  l.bPending = TRU
8fbb0 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e  E;.      bReturn
8fbc0 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20   = TRUE;.    }. 
8fbd0 20 7d 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20 72   }.  /* Want a r
8fbe0 65 73 65 72 76 65 64 20 6c 6f 63 6b 3f 20 2a 2f  eserved lock? */
8fbf0 0a 20 20 65 6c 73 65 20 69 66 20 28 64 77 46 69  .  else if (dwFi
8fc00 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 52  leOffsetLow == R
8fc10 45 53 45 52 56 45 44 5f 42 59 54 45 20 26 26 20  ESERVED_BYTE && 
8fc20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f  nNumberOfBytesTo
8fc30 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20  LockLow == 1){. 
8fc40 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 73 68     if (pFile->sh
8fc50 61 72 65 64 2d 3e 62 52 65 73 65 72 76 65 64 20  ared->bReserved 
8fc60 3d 3d 20 30 29 20 7b 0a 20 20 20 20 20 20 70 46  == 0) {.      pF
8fc70 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65  ile->shared->bRe
8fc80 73 65 72 76 65 64 20 3d 20 54 52 55 45 3b 0a 20  served = TRUE;. 
8fc90 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61       pFile->loca
8fca0 6c 2e 62 52 65 73 65 72 76 65 64 20 3d 20 54 52  l.bReserved = TR
8fcb0 55 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72  UE;.      bRetur
8fcc0 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a  n = TRUE;.    }.
8fcd0 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65    }..  winceMute
8fce0 78 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e  xRelease(pFile->
8fcf0 68 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  hMutex);.  retur
8fd00 6e 20 62 52 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a  n bReturn;.}../*
8fd10 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e 74  .** An implement
8fd20 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 55 6e 6c  ation of the Unl
8fd30 6f 63 6b 46 69 6c 65 20 41 50 49 20 6f 66 20 77  ockFile API of w
8fd40 69 6e 64 6f 77 73 20 66 6f 72 20 77 69 6e 63 65  indows for wince
8fd50 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20  .*/.static BOOL 
8fd60 77 69 6e 63 65 55 6e 6c 6f 63 6b 46 69 6c 65 28  winceUnlockFile(
8fd70 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c  .  HANDLE *phFil
8fd80 65 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 69 6c  e,.  DWORD dwFil
8fd90 65 4f 66 66 73 65 74 4c 6f 77 2c 0a 20 20 44 57  eOffsetLow,.  DW
8fda0 4f 52 44 20 64 77 46 69 6c 65 4f 66 66 73 65 74  ORD dwFileOffset
8fdb0 48 69 67 68 2c 0a 20 20 44 57 4f 52 44 20 6e 4e  High,.  DWORD nN
8fdc0 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 6e  umberOfBytesToUn
8fdd0 6c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44  lockLow,.  DWORD
8fde0 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54   nNumberOfBytesT
8fdf0 6f 55 6e 6c 6f 63 6b 48 69 67 68 0a 29 7b 0a 20  oUnlockHigh.){. 
8fe00 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20   winFile *pFile 
8fe10 3d 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46  = HANDLE_TO_WINF
8fe20 49 4c 45 28 70 68 46 69 6c 65 29 3b 0a 20 20 42  ILE(phFile);.  B
8fe30 4f 4f 4c 20 62 52 65 74 75 72 6e 20 3d 20 46 41  OOL bReturn = FA
8fe40 4c 53 45 3b 0a 0a 20 20 69 66 20 28 21 70 46 69  LSE;..  if (!pFi
8fe50 6c 65 2d 3e 68 4d 75 74 65 78 29 20 72 65 74 75  le->hMutex) retu
8fe60 72 6e 20 54 52 55 45 3b 0a 20 20 77 69 6e 63 65  rn TRUE;.  wince
8fe70 4d 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69  MutexAcquire(pFi
8fe80 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 20  le->hMutex);..  
8fe90 2f 2a 20 52 65 6c 65 61 73 69 6e 67 20 61 20 72  /* Releasing a r
8fea0 65 61 64 65 72 20 6c 6f 63 6b 20 6f 72 20 61 6e  eader lock or an
8feb0 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20   exclusive lock 
8fec0 2a 2f 0a 20 20 69 66 20 28 64 77 46 69 6c 65 4f  */.  if (dwFileO
8fed0 66 66 73 65 74 4c 6f 77 20 3e 3d 20 53 48 41 52  ffsetLow >= SHAR
8fee0 45 44 5f 46 49 52 53 54 20 26 26 0a 20 20 20 20  ED_FIRST &&.    
8fef0 20 20 20 64 77 46 69 6c 65 4f 66 66 73 65 74 4c     dwFileOffsetL
8ff00 6f 77 20 3c 20 53 48 41 52 45 44 5f 46 49 52 53  ow < SHARED_FIRS
8ff10 54 20 2b 20 53 48 41 52 45 44 5f 53 49 5a 45 29  T + SHARED_SIZE)
8ff20 7b 0a 20 20 20 20 2f 2a 20 44 69 64 20 77 65 20  {.    /* Did we 
8ff30 68 61 76 65 20 61 6e 20 65 78 63 6c 75 73 69 76  have an exclusiv
8ff40 65 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 20 20 69  e lock? */.    i
8ff50 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e  f (pFile->local.
8ff60 62 45 78 63 6c 75 73 69 76 65 29 7b 0a 20 20 20  bExclusive){.   
8ff70 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e     pFile->local.
8ff80 62 45 78 63 6c 75 73 69 76 65 20 3d 20 46 41 4c  bExclusive = FAL
8ff90 53 45 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d  SE;.      pFile-
8ffa0 3e 73 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73  >shared->bExclus
8ffb0 69 76 65 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20  ive = FALSE;.   
8ffc0 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55     bReturn = TRU
8ffd0 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  E;.    }..    /*
8ffe0 20 44 69 64 20 77 65 20 6a 75 73 74 20 68 61 76   Did we just hav
8fff0 65 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b 3f  e a reader lock?
90000 20 2a 2f 0a 20 20 20 20 65 6c 73 65 20 69 66 20   */.    else if 
90010 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52  (pFile->local.nR
90020 65 61 64 65 72 73 29 7b 0a 20 20 20 20 20 20 70  eaders){.      p
90030 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61  File->local.nRea
90040 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20 20 20 69  ders --;.      i
90050 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e  f (pFile->local.
90060 6e 52 65 61 64 65 72 73 20 3d 3d 20 30 29 0a 20  nReaders == 0). 
90070 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 70       {.        p
90080 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52  File->shared->nR
90090 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20 20  eaders --;.     
900a0 20 7d 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e   }.      bReturn
900b0 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20   = TRUE;.    }. 
900c0 20 7d 0a 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69   }..  /* Releasi
900d0 6e 67 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63  ng a pending loc
900e0 6b 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28  k */.  else if (
900f0 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20  dwFileOffsetLow 
90100 3d 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20  == PENDING_BYTE 
90110 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65  && nNumberOfByte
90120 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20  sToUnlockLow == 
90130 31 29 7b 0a 20 20 20 20 69 66 20 28 70 46 69 6c  1){.    if (pFil
90140 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e  e->local.bPendin
90150 67 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d  g){.      pFile-
90160 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 20  >local.bPending 
90170 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 70  = FALSE;.      p
90180 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50  File->shared->bP
90190 65 6e 64 69 6e 67 20 3d 20 46 41 4c 53 45 3b 0a  ending = FALSE;.
901a0 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20        bReturn = 
901b0 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  TRUE;.    }.  }.
901c0 20 20 2f 2a 20 52 65 6c 65 61 73 69 6e 67 20 61    /* Releasing a
901d0 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 2a   reserved lock *
901e0 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64 77 46  /.  else if (dwF
901f0 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20  ileOffsetLow == 
90200 52 45 53 45 52 56 45 44 5f 42 59 54 45 20 26 26  RESERVED_BYTE &&
90210 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54   nNumberOfBytesT
90220 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29  oUnlockLow == 1)
90230 7b 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d  {.    if (pFile-
90240 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64  >local.bReserved
90250 29 20 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d  ) {.      pFile-
90260 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64  >local.bReserved
90270 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 20 20   = FALSE;.      
90280 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62  pFile->shared->b
90290 52 65 73 65 72 76 65 64 20 3d 20 46 41 4c 53 45  Reserved = FALSE
902a0 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e 20  ;.      bReturn 
902b0 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20  = TRUE;.    }.  
902c0 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 52  }..  winceMutexR
902d0 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d  elease(pFile->hM
902e0 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
902f0 62 52 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a  bReturn;.}../*.*
90300 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74  * An implementat
90310 69 6f 6e 20 6f 66 20 74 68 65 20 4c 6f 63 6b 46  ion of the LockF
90320 69 6c 65 45 78 28 29 20 41 50 49 20 6f 66 20 77  ileEx() API of w
90330 69 6e 64 6f 77 73 20 66 6f 72 20 77 69 6e 63 65  indows for wince
90340 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20  .*/.static BOOL 
90350 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 45 78 28  winceLockFileEx(
90360 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c  .  HANDLE *phFil
90370 65 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 6c 61  e,.  DWORD dwFla
90380 67 73 2c 0a 20 20 44 57 4f 52 44 20 64 77 52 65  gs,.  DWORD dwRe
90390 73 65 72 76 65 64 2c 0a 20 20 44 57 4f 52 44 20  served,.  DWORD 
903a0 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f  nNumberOfBytesTo
903b0 4c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44  LockLow,.  DWORD
903c0 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54   nNumberOfBytesT
903d0 6f 4c 6f 63 6b 48 69 67 68 2c 0a 20 20 4c 50 4f  oLockHigh,.  LPO
903e0 56 45 52 4c 41 50 50 45 44 20 6c 70 4f 76 65 72  VERLAPPED lpOver
903f0 6c 61 70 70 65 64 0a 29 7b 0a 20 20 2f 2a 20 49  lapped.){.  /* I
90400 66 20 74 68 65 20 63 61 6c 6c 65 72 20 77 61 6e  f the caller wan
90410 74 73 20 61 20 73 68 61 72 65 64 20 72 65 61 64  ts a shared read
90420 20 6c 6f 63 6b 2c 20 66 6f 72 77 61 72 64 20 74   lock, forward t
90430 68 69 73 20 63 61 6c 6c 0a 20 20 2a 2a 20 74 6f  his call.  ** to
90440 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 20 2a   winceLockFile *
90450 2f 0a 20 20 69 66 20 28 6c 70 4f 76 65 72 6c 61  /.  if (lpOverla
90460 70 70 65 64 2d 3e 4f 66 66 73 65 74 20 3d 3d 20  pped->Offset == 
90470 53 48 41 52 45 44 5f 46 49 52 53 54 20 26 26 0a  SHARED_FIRST &&.
90480 20 20 20 20 20 20 64 77 46 6c 61 67 73 20 3d 3d        dwFlags ==
90490 20 31 20 26 26 0a 20 20 20 20 20 20 6e 4e 75 6d   1 &&.      nNum
904a0 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b  berOfBytesToLock
904b0 4c 6f 77 20 3d 3d 20 53 48 41 52 45 44 5f 53 49  Low == SHARED_SI
904c0 5a 45 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ZE){.    return 
904d0 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28 70 68  winceLockFile(ph
904e0 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49 52  File, SHARED_FIR
904f0 53 54 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20  ST, 0, 1, 0);.  
90500 7d 0a 20 20 72 65 74 75 72 6e 20 46 41 4c 53 45  }.  return FALSE
90510 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 45 6e 64 20 6f 66  ;.}./*.** End of
90520 20 74 68 65 20 73 70 65 63 69 61 6c 20 63 6f 64   the special cod
90530 65 20 66 6f 72 20 77 69 6e 63 65 0a 2a 2a 2a 2a  e for wince.****
90540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64 69  *********/.#endi
90590 66 20 2f 2a 20 4f 53 5f 57 49 4e 43 45 20 2a 2f  f /* OS_WINCE */
905a0 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20  ../*.** Convert 
905b0 61 20 55 54 46 2d 38 20 66 69 6c 65 6e 61 6d 65  a UTF-8 filename
905c0 20 69 6e 74 6f 20 77 68 61 74 65 76 65 72 20 66   into whatever f
905d0 6f 72 6d 20 74 68 65 20 75 6e 64 65 72 6c 79 69  orm the underlyi
905e0 6e 67 0a 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20  ng.** operating 
905f0 73 79 73 74 65 6d 20 77 61 6e 74 73 20 66 69 6c  system wants fil
90600 65 6e 61 6d 65 73 20 69 6e 2e 20 20 53 70 61 63  enames in.  Spac
90610 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65  e to hold the re
90620 73 75 6c 74 0a 2a 2a 20 69 73 20 6f 62 74 61 69  sult.** is obtai
90630 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d  ned from sqliteM
90640 61 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74 20 62  alloc and must b
90650 65 20 66 72 65 65 64 20 62 79 20 74 68 65 20 63  e freed by the c
90660 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69  alling.** functi
90670 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  on..*/.static vo
90680 69 64 20 2a 63 6f 6e 76 65 72 74 55 74 66 38 46  id *convertUtf8F
90690 69 6c 65 6e 61 6d 65 28 63 6f 6e 73 74 20 63 68  ilename(const ch
906a0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a  ar *zFilename){.
906b0 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74    void *zConvert
906c0 65 64 20 3d 20 30 3b 0a 20 20 69 66 28 20 69 73  ed = 0;.  if( is
906d0 4e 54 28 29 20 29 7b 0a 20 20 20 20 7a 43 6f 6e  NT() ){.    zCon
906e0 76 65 72 74 65 64 20 3d 20 75 74 66 38 54 6f 55  verted = utf8ToU
906f0 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65  nicode(zFilename
90700 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
90710 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 75 74 66  zConverted = utf
90720 38 54 6f 4d 62 63 73 28 7a 46 69 6c 65 6e 61 6d  8ToMbcs(zFilenam
90730 65 29 3b 0a 20 20 7d 0a 20 20 2f 2a 20 63 61 6c  e);.  }.  /* cal
90740 6c 65 72 20 77 69 6c 6c 20 68 61 6e 64 6c 65 20  ler will handle 
90750 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 20 2a 2f  out of memory */
90760 0a 20 20 72 65 74 75 72 6e 20 7a 43 6f 6e 76 65  .  return zConve
90770 72 74 65 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  rted;.}../*.** D
90780 65 6c 65 74 65 20 74 68 65 20 6e 61 6d 65 64 20  elete the named 
90790 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65  file..**.** Note
907a0 20 74 68 61 74 20 77 69 6e 64 6f 77 73 20 64 6f   that windows do
907b0 65 73 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 20 66  es not allow a f
907c0 69 6c 65 20 74 6f 20 62 65 20 64 65 6c 65 74 65  ile to be delete
907d0 64 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 0a  d if some other.
907e0 2a 2a 20 70 72 6f 63 65 73 73 20 68 61 73 20 69  ** process has i
907f0 74 20 6f 70 65 6e 2e 20 20 53 6f 6d 65 74 69 6d  t open.  Sometim
90800 65 73 20 61 20 76 69 72 75 73 20 73 63 61 6e 6e  es a virus scann
90810 65 72 20 6f 72 20 69 6e 64 65 78 69 6e 67 20 70  er or indexing p
90820 72 6f 67 72 61 6d 0a 2a 2a 20 77 69 6c 6c 20 6f  rogram.** will o
90830 70 65 6e 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69  pen a journal fi
90840 6c 65 20 73 68 6f 72 74 6c 79 20 61 66 74 65 72  le shortly after
90850 20 69 74 20 69 73 20 63 72 65 61 74 65 64 20 69   it is created i
90860 6e 20 6f 72 64 65 72 20 74 6f 20 64 6f 0a 2a 2a  n order to do.**
90870 20 77 68 61 74 65 76 65 72 20 69 74 20 69 73 20   whatever it is 
90880 69 74 20 64 6f 65 73 2e 20 20 57 68 69 6c 65 20  it does.  While 
90890 74 68 69 73 20 6f 74 68 65 72 20 70 72 6f 63 65  this other proce
908a0 73 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 74 68  ss is holding th
908b0 65 0a 2a 2a 20 66 69 6c 65 20 6f 70 65 6e 2c 20  e.** file open, 
908c0 77 65 20 77 69 6c 6c 20 62 65 20 75 6e 61 62 6c  we will be unabl
908d0 65 20 74 6f 20 64 65 6c 65 74 65 20 69 74 2e 20  e to delete it. 
908e0 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20   To work around 
908f0 74 68 69 73 0a 2a 2a 20 70 72 6f 62 6c 65 6d 2c  this.** problem,
90900 20 77 65 20 64 65 6c 61 79 20 31 30 30 20 6d 69   we delay 100 mi
90910 6c 6c 69 73 65 63 6f 6e 64 73 20 61 6e 64 20 74  lliseconds and t
90920 72 79 20 74 6f 20 64 65 6c 65 74 65 20 61 67 61  ry to delete aga
90930 69 6e 2e 20 20 55 70 0a 2a 2a 20 74 6f 20 4d 58  in.  Up.** to MX
90940 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50  _DELETION_ATTEMP
90950 54 73 20 64 65 6c 65 74 69 6f 6e 20 61 74 74 65  Ts deletion atte
90960 6d 70 74 73 20 61 72 65 20 72 75 6e 20 62 65 66  mpts are run bef
90970 6f 72 65 20 67 69 76 69 6e 67 0a 2a 2a 20 75 70  ore giving.** up
90980 20 61 6e 64 20 72 65 74 75 72 6e 69 6e 67 20 61   and returning a
90990 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 23 64 65 66  n error..*/.#def
909a0 69 6e 65 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f  ine MX_DELETION_
909b0 41 54 54 45 4d 50 54 53 20 33 0a 53 51 4c 49 54  ATTEMPTS 3.SQLIT
909c0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
909d0 6c 69 74 65 33 57 69 6e 44 65 6c 65 74 65 28 63  lite3WinDelete(c
909e0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
909f0 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 63 6e 74  name){.  int cnt
90a00 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a   = 0;.  int rc;.
90a10 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74    void *zConvert
90a20 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38  ed = convertUtf8
90a30 46 69 6c 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 61  Filename(zFilena
90a40 6d 65 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76  me);.  if( zConv
90a50 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20  erted==0 ){.    
90a60 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
90a70 4d 45 4d 3b 0a 20 20 7d 0a 20 20 53 69 6d 75 6c  MEM;.  }.  Simul
90a80 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75 72  ateIOError(retur
90a90 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44  n SQLITE_IOERR_D
90aa0 45 4c 45 54 45 29 3b 0a 20 20 69 66 28 20 69 73  ELETE);.  if( is
90ab0 4e 54 28 29 20 29 7b 0a 20 20 20 20 64 6f 7b 0a  NT() ){.    do{.
90ac0 20 20 20 20 20 20 72 63 20 3d 20 44 65 6c 65 74        rc = Delet
90ad0 65 46 69 6c 65 57 28 7a 43 6f 6e 76 65 72 74 65  eFileW(zConverte
90ae0 64 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20  d);.    }while( 
90af0 72 63 3d 3d 30 20 26 26 20 47 65 74 46 69 6c 65  rc==0 && GetFile
90b00 41 74 74 72 69 62 75 74 65 73 57 28 7a 43 6f 6e  AttributesW(zCon
90b10 76 65 72 74 65 64 29 21 3d 30 78 66 66 66 66 66  verted)!=0xfffff
90b20 66 66 66 20 0a 20 20 20 20 20 20 20 20 20 20 20  fff .           
90b30 20 26 26 20 63 6e 74 2b 2b 20 3c 20 4d 58 5f 44   && cnt++ < MX_D
90b40 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53  ELETION_ATTEMPTS
90b50 20 26 26 20 28 53 6c 65 65 70 28 31 30 30 29 2c   && (Sleep(100),
90b60 20 31 29 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a   1) );.  }else{.
90b70 23 69 66 20 4f 53 5f 57 49 4e 43 45 0a 20 20 20  #if OS_WINCE.   
90b80 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
90b90 4f 4d 45 4d 3b 0a 23 65 6c 73 65 0a 20 20 20 20  OMEM;.#else.    
90ba0 64 6f 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 44  do{.      rc = D
90bb0 65 6c 65 74 65 46 69 6c 65 41 28 7a 43 6f 6e 76  eleteFileA(zConv
90bc0 65 72 74 65 64 29 3b 0a 20 20 20 20 7d 77 68 69  erted);.    }whi
90bd0 6c 65 28 20 72 63 3d 3d 30 20 26 26 20 47 65 74  le( rc==0 && Get
90be0 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 41 28  FileAttributesA(
90bf0 7a 43 6f 6e 76 65 72 74 65 64 29 21 3d 30 78 66  zConverted)!=0xf
90c00 66 66 66 66 66 66 66 0a 20 20 20 20 20 20 20 20  fffffff.        
90c10 20 20 20 20 26 26 20 63 6e 74 2b 2b 20 3c 20 4d      && cnt++ < M
90c20 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d  X_DELETION_ATTEM
90c30 50 54 53 20 26 26 20 28 53 6c 65 65 70 28 31 30  PTS && (Sleep(10
90c40 30 29 2c 20 31 29 20 29 3b 0a 23 65 6e 64 69 66  0), 1) );.#endif
90c50 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 46 72 65  .  }.  sqliteFre
90c60 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20  e(zConverted);. 
90c70 20 4f 53 54 52 41 43 45 32 28 22 44 45 4c 45 54   OSTRACE2("DELET
90c80 45 20 5c 22 25 73 5c 22 5c 6e 22 2c 20 7a 46 69  E \"%s\"\n", zFi
90c90 6c 65 6e 61 6d 65 29 3b 0a 20 20 72 65 74 75 72  lename);.  retur
90ca0 6e 20 72 63 21 3d 30 20 3f 20 53 51 4c 49 54 45  n rc!=0 ? SQLITE
90cb0 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45  _OK : SQLITE_IOE
90cc0 52 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  RR;.}../*.** Ret
90cd0 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20  urn TRUE if the 
90ce0 6e 61 6d 65 64 20 66 69 6c 65 20 65 78 69 73 74  named file exist
90cf0 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
90d00 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
90d10 57 69 6e 46 69 6c 65 45 78 69 73 74 73 28 63 6f  WinFileExists(co
90d20 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  nst char *zFilen
90d30 61 6d 65 29 7b 0a 20 20 69 6e 74 20 65 78 69 73  ame){.  int exis
90d40 74 73 20 3d 20 30 3b 0a 20 20 76 6f 69 64 20 2a  ts = 0;.  void *
90d50 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e  zConverted = con
90d60 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65  vertUtf8Filename
90d70 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 69  (zFilename);.  i
90d80 66 28 20 7a 43 6f 6e 76 65 72 74 65 64 3d 3d 30  f( zConverted==0
90d90 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
90da0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
90db0 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b  .  if( isNT() ){
90dc0 0a 20 20 20 20 65 78 69 73 74 73 20 3d 20 47 65  .    exists = Ge
90dd0 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 57  tFileAttributesW
90de0 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72  ((WCHAR*)zConver
90df0 74 65 64 29 20 21 3d 20 30 78 66 66 66 66 66 66  ted) != 0xffffff
90e00 66 66 3b 0a 20 20 7d 65 6c 73 65 7b 0a 23 69 66  ff;.  }else{.#if
90e10 20 4f 53 5f 57 49 4e 43 45 0a 20 20 20 20 72 65   OS_WINCE.    re
90e20 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
90e30 4d 3b 0a 23 65 6c 73 65 0a 20 20 20 20 65 78 69  M;.#else.    exi
90e40 73 74 73 20 3d 20 47 65 74 46 69 6c 65 41 74 74  sts = GetFileAtt
90e50 72 69 62 75 74 65 73 41 28 28 63 68 61 72 2a 29  ributesA((char*)
90e60 7a 43 6f 6e 76 65 72 74 65 64 29 20 21 3d 20 30  zConverted) != 0
90e70 78 66 66 66 66 66 66 66 66 3b 0a 23 65 6e 64 69  xffffffff;.#endi
90e80 66 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 46 72  f.  }.  sqliteFr
90e90 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a  ee(zConverted);.
90ea0 20 20 72 65 74 75 72 6e 20 65 78 69 73 74 73 3b    return exists;
90eb0 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64  .}../* Forward d
90ec0 65 63 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a 73 74  eclaration */.st
90ed0 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74  atic int allocat
90ee0 65 57 69 6e 46 69 6c 65 28 77 69 6e 46 69 6c 65  eWinFile(winFile
90ef0 20 2a 70 49 6e 69 74 2c 20 4f 73 46 69 6c 65 20   *pInit, OsFile 
90f00 2a 2a 70 49 64 29 3b 0a 0a 2f 2a 0a 2a 2a 20 41  **pId);../*.** A
90f10 74 74 65 6d 70 74 20 74 6f 20 6f 70 65 6e 20 61  ttempt to open a
90f20 20 66 69 6c 65 20 66 6f 72 20 62 6f 74 68 20 72   file for both r
90f30 65 61 64 69 6e 67 20 61 6e 64 20 77 72 69 74 69  eading and writi
90f40 6e 67 2e 20 20 49 66 20 74 68 61 74 0a 2a 2a 20  ng.  If that.** 
90f50 66 61 69 6c 73 2c 20 74 72 79 20 6f 70 65 6e 69  fails, try openi
90f60 6e 67 20 69 74 20 72 65 61 64 2d 6f 6e 6c 79 2e  ng it read-only.
90f70 20 20 49 66 20 74 68 65 20 66 69 6c 65 20 64 6f    If the file do
90f80 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 0a 2a 2a  es not exist,.**
90f90 20 74 72 79 20 74 6f 20 63 72 65 61 74 65 20 69   try to create i
90fa0 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63  t..**.** On succ
90fb0 65 73 73 2c 20 61 20 68 61 6e 64 6c 65 20 66 6f  ess, a handle fo
90fc0 72 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20  r the open file 
90fd0 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 2a 69  is written to *i
90fe0 64 0a 2a 2a 20 61 6e 64 20 2a 70 52 65 61 64 6f  d.** and *pReado
90ff0 6e 6c 79 20 69 73 20 73 65 74 20 74 6f 20 30 20  nly is set to 0 
91000 69 66 20 74 68 65 20 66 69 6c 65 20 77 61 73 20  if the file was 
91010 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 69  opened for readi
91020 6e 67 20 61 6e 64 0a 2a 2a 20 77 72 69 74 69 6e  ng and.** writin
91030 67 20 6f 72 20 31 20 69 66 20 74 68 65 20 66 69  g or 1 if the fi
91040 6c 65 20 77 61 73 20 6f 70 65 6e 65 64 20 72 65  le was opened re
91050 61 64 2d 6f 6e 6c 79 2e 20 20 54 68 65 20 66 75  ad-only.  The fu
91060 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 0a 2a  nction returns.*
91070 2a 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a  * SQLITE_OK..**.
91080 2a 2a 20 4f 6e 20 66 61 69 6c 75 72 65 2c 20 74  ** On failure, t
91090 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  he function retu
910a0 72 6e 73 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f  rns SQLITE_CANTO
910b0 50 45 4e 20 61 6e 64 20 6c 65 61 76 65 73 0a 2a  PEN and leaves.*
910c0 2a 20 2a 69 64 20 61 6e 64 20 2a 70 52 65 61 64  * *id and *pRead
910d0 6f 6e 6c 79 20 75 6e 63 68 61 6e 67 65 64 2e 0a  only unchanged..
910e0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
910f0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 69 6e  E int sqlite3Win
91100 4f 70 65 6e 52 65 61 64 57 72 69 74 65 28 0a 20  OpenReadWrite(. 
91110 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
91120 6c 65 6e 61 6d 65 2c 0a 20 20 4f 73 46 69 6c 65  lename,.  OsFile
91130 20 2a 2a 70 49 64 2c 0a 20 20 69 6e 74 20 2a 70   **pId,.  int *p
91140 52 65 61 64 6f 6e 6c 79 0a 29 7b 0a 20 20 77 69  Readonly.){.  wi
91150 6e 46 69 6c 65 20 66 3b 0a 20 20 48 41 4e 44 4c  nFile f;.  HANDL
91160 45 20 68 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f  E h;.  void *zCo
91170 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 72  nverted = conver
91180 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a 46  tUtf8Filename(zF
91190 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 69 66 28 20  ilename);.  if( 
911a0 7a 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b  zConverted==0 ){
911b0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
911c0 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
911d0 61 73 73 65 72 74 28 20 2a 70 49 64 3d 3d 30 20  assert( *pId==0 
911e0 29 3b 0a 0a 20 20 69 66 28 20 69 73 4e 54 28 29  );..  if( isNT()
911f0 20 29 7b 0a 20 20 20 20 68 20 3d 20 43 72 65 61   ){.    h = Crea
91200 74 65 46 69 6c 65 57 28 28 57 43 48 41 52 2a 29  teFileW((WCHAR*)
91210 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20 20 20 20  zConverted,.    
91220 20 20 20 47 45 4e 45 52 49 43 5f 52 45 41 44 20     GENERIC_READ 
91230 7c 20 47 45 4e 45 52 49 43 5f 57 52 49 54 45 2c  | GENERIC_WRITE,
91240 0a 20 20 20 20 20 20 20 46 49 4c 45 5f 53 48 41  .       FILE_SHA
91250 52 45 5f 52 45 41 44 20 7c 20 46 49 4c 45 5f 53  RE_READ | FILE_S
91260 48 41 52 45 5f 57 52 49 54 45 2c 0a 20 20 20 20  HARE_WRITE,.    
91270 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20     NULL,.       
91280 4f 50 45 4e 5f 41 4c 57 41 59 53 2c 0a 20 20 20  OPEN_ALWAYS,.   
91290 20 20 20 20 46 49 4c 45 5f 41 54 54 52 49 42 55      FILE_ATTRIBU
912a0 54 45 5f 4e 4f 52 4d 41 4c 20 7c 20 46 49 4c 45  TE_NORMAL | FILE
912b0 5f 46 4c 41 47 5f 52 41 4e 44 4f 4d 5f 41 43 43  _FLAG_RANDOM_ACC
912c0 45 53 53 2c 0a 20 20 20 20 20 20 20 4e 55 4c 4c  ESS,.       NULL
912d0 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20  .    );.    if( 
912e0 68 3d 3d 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c  h==INVALID_HANDL
912f0 45 5f 56 41 4c 55 45 20 29 7b 0a 20 20 20 20 20  E_VALUE ){.     
91300 20 68 20 3d 20 43 72 65 61 74 65 46 69 6c 65 57   h = CreateFileW
91310 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72  ((WCHAR*)zConver
91320 74 65 64 2c 0a 20 20 20 20 20 20 20 20 20 47 45  ted,.         GE
91330 4e 45 52 49 43 5f 52 45 41 44 2c 0a 20 20 20 20  NERIC_READ,.    
91340 20 20 20 20 20 46 49 4c 45 5f 53 48 41 52 45 5f       FILE_SHARE_
91350 52 45 41 44 20 7c 20 46 49 4c 45 5f 53 48 41 52  READ | FILE_SHAR
91360 45 5f 57 52 49 54 45 2c 0a 20 20 20 20 20 20 20  E_WRITE,.       
91370 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20 20    NULL,.        
91380 20 4f 50 45 4e 5f 41 4c 57 41 59 53 2c 0a 20 20   OPEN_ALWAYS,.  
91390 20 20 20 20 20 20 20 46 49 4c 45 5f 41 54 54 52         FILE_ATTR
913a0 49 42 55 54 45 5f 4e 4f 52 4d 41 4c 20 7c 20 46  IBUTE_NORMAL | F
913b0 49 4c 45 5f 46 4c 41 47 5f 52 41 4e 44 4f 4d 5f  ILE_FLAG_RANDOM_
913c0 41 43 43 45 53 53 2c 0a 20 20 20 20 20 20 20 20  ACCESS,.        
913d0 20 4e 55 4c 4c 0a 20 20 20 20 20 20 29 3b 0a 20   NULL.      );. 
913e0 20 20 20 20 20 69 66 28 20 68 3d 3d 49 4e 56 41       if( h==INVA
913f0 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45  LID_HANDLE_VALUE
91400 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
91410 74 65 46 72 65 65 28 7a 43 6f 6e 76 65 72 74 65  teFree(zConverte
91420 64 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  d);.        retu
91430 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50  rn SQLITE_CANTOP
91440 45 4e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  EN;.      }.    
91450 20 20 2a 70 52 65 61 64 6f 6e 6c 79 20 3d 20 31    *pReadonly = 1
91460 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
91470 20 20 20 2a 70 52 65 61 64 6f 6e 6c 79 20 3d 20     *pReadonly = 
91480 30 3b 0a 20 20 20 20 7d 0a 23 69 66 20 4f 53 5f  0;.    }.#if OS_
91490 57 49 4e 43 45 0a 20 20 20 20 69 66 20 28 21 77  WINCE.    if (!w
914a0 69 6e 63 65 43 72 65 61 74 65 4c 6f 63 6b 28 7a  inceCreateLock(z
914b0 46 69 6c 65 6e 61 6d 65 2c 20 26 66 29 29 7b 0a  Filename, &f)){.
914c0 20 20 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c        CloseHandl
914d0 65 28 68 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  e(h);.      sqli
914e0 74 65 46 72 65 65 28 7a 43 6f 6e 76 65 72 74 65  teFree(zConverte
914f0 64 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  d);.      return
91500 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e   SQLITE_CANTOPEN
91510 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
91520 20 7d 65 6c 73 65 7b 0a 23 69 66 20 4f 53 5f 57   }else{.#if OS_W
91530 49 4e 43 45 0a 20 20 20 20 72 65 74 75 72 6e 20  INCE.    return 
91540 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 23 65  SQLITE_NOMEM;.#e
91550 6c 73 65 0a 20 20 20 20 68 20 3d 20 43 72 65 61  lse.    h = Crea
91560 74 65 46 69 6c 65 41 28 28 63 68 61 72 2a 29 7a  teFileA((char*)z
91570 43 6f 6e 76 65 72 74 65 64 2c 0a 20 20 20 20 20  Converted,.     
91580 20 20 47 45 4e 45 52 49 43 5f 52 45 41 44 20 7c    GENERIC_READ |
91590 20 47 45 4e 45 52 49 43 5f 57 52 49 54 45 2c 0a   GENERIC_WRITE,.
915a0 20 20 20 20 20 20 20 46 49 4c 45 5f 53 48 41 52         FILE_SHAR
915b0 45 5f 52 45 41 44 20 7c 20 46 49 4c 45 5f 53 48  E_READ | FILE_SH
915c0 41 52 45 5f 57 52 49 54 45 2c 0a 20 20 20 20 20  ARE_WRITE,.     
915d0 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20 4f    NULL,.       O
915e0 50 45 4e 5f 41 4c 57 41 59 53 2c 0a 20 20 20 20  PEN_ALWAYS,.    
915f0 20 20 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54     FILE_ATTRIBUT
91600 45 5f 4e 4f 52 4d 41 4c 20 7c 20 46 49 4c 45 5f  E_NORMAL | FILE_
91610 46 4c 41 47 5f 52 41 4e 44 4f 4d 5f 41 43 43 45  FLAG_RANDOM_ACCE
91620 53 53 2c 0a 20 20 20 20 20 20 20 4e 55 4c 4c 0a  SS,.       NULL.
91630 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20 68      );.    if( h
91640 3d 3d 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c 45  ==INVALID_HANDLE
91650 5f 56 41 4c 55 45 20 29 7b 0a 20 20 20 20 20 20  _VALUE ){.      
91660 68 20 3d 20 43 72 65 61 74 65 46 69 6c 65 41 28  h = CreateFileA(
91670 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65  (char*)zConverte
91680 64 2c 0a 20 20 20 20 20 20 20 20 20 47 45 4e 45  d,.         GENE
91690 52 49 43 5f 52 45 41 44 2c 0a 20 20 20 20 20 20  RIC_READ,.      
916a0 20 20 20 46 49 4c 45 5f 53 48 41 52 45 5f 52 45     FILE_SHARE_RE
916b0 41 44 20 7c 20 46 49 4c 45 5f 53 48 41 52 45 5f  AD | FILE_SHARE_
916c0 57 52 49 54 45 2c 0a 20 20 20 20 20 20 20 20 20  WRITE,.         
916d0 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20 20 20 4f  NULL,.         O
916e0 50 45 4e 5f 41 4c 57 41 59 53 2c 0a 20 20 20 20  PEN_ALWAYS,.    
916f0 20 20 20 20 20 46 49 4c 45 5f 41 54 54 52 49 42       FILE_ATTRIB
91700 55 54 45 5f 4e 4f 52 4d 41 4c 20 7c 20 46 49 4c  UTE_NORMAL | FIL
91710 45 5f 46 4c 41 47 5f 52 41 4e 44 4f 4d 5f 41 43  E_FLAG_RANDOM_AC
91720 43 45 53 53 2c 0a 20 20 20 20 20 20 20 20 20 4e  CESS,.         N
91730 55 4c 4c 0a 20 20 20 20 20 20 29 3b 0a 20 20 20  ULL.      );.   
91740 20 20 20 69 66 28 20 68 3d 3d 49 4e 56 41 4c 49     if( h==INVALI
91750 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45 20 29  D_HANDLE_VALUE )
91760 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
91770 46 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29  Free(zConverted)
91780 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
91790 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e   SQLITE_CANTOPEN
917a0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
917b0 2a 70 52 65 61 64 6f 6e 6c 79 20 3d 20 31 3b 0a  *pReadonly = 1;.
917c0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
917d0 20 2a 70 52 65 61 64 6f 6e 6c 79 20 3d 20 30 3b   *pReadonly = 0;
917e0 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a  .    }.#endif /*
917f0 20 4f 53 5f 57 49 4e 43 45 20 2a 2f 0a 20 20 7d   OS_WINCE */.  }
91800 0a 0a 20 20 73 71 6c 69 74 65 46 72 65 65 28 7a  ..  sqliteFree(z
91810 43 6f 6e 76 65 72 74 65 64 29 3b 0a 0a 20 20 66  Converted);..  f
91820 2e 68 20 3d 20 68 3b 0a 23 69 66 20 4f 53 5f 57  .h = h;.#if OS_W
91830 49 4e 43 45 0a 20 20 66 2e 7a 44 65 6c 65 74 65  INCE.  f.zDelete
91840 4f 6e 43 6c 6f 73 65 20 3d 20 30 3b 0a 23 65 6e  OnClose = 0;.#en
91850 64 69 66 0a 20 20 4f 53 54 52 41 43 45 33 28 22  dif.  OSTRACE3("
91860 4f 50 45 4e 20 52 2f 57 20 25 64 20 5c 22 25 73  OPEN R/W %d \"%s
91870 5c 22 5c 6e 22 2c 20 68 2c 20 7a 46 69 6c 65 6e  \"\n", h, zFilen
91880 61 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 61  ame);.  return a
91890 6c 6c 6f 63 61 74 65 57 69 6e 46 69 6c 65 28 26  llocateWinFile(&
918a0 66 2c 20 70 49 64 29 3b 0a 7d 0a 0a 0a 2f 2a 0a  f, pId);.}.../*.
918b0 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6f 70  ** Attempt to op
918c0 65 6e 20 61 20 6e 65 77 20 66 69 6c 65 20 66 6f  en a new file fo
918d0 72 20 65 78 63 6c 75 73 69 76 65 20 61 63 63 65  r exclusive acce
918e0 73 73 20 62 79 20 74 68 69 73 20 70 72 6f 63 65  ss by this proce
918f0 73 73 2e 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20  ss..** The file 
91900 77 69 6c 6c 20 62 65 20 6f 70 65 6e 65 64 20 66  will be opened f
91910 6f 72 20 62 6f 74 68 20 72 65 61 64 69 6e 67 20  or both reading 
91920 61 6e 64 20 77 72 69 74 69 6e 67 2e 20 20 54 6f  and writing.  To
91930 20 61 76 6f 69 64 0a 2a 2a 20 61 20 70 6f 74 65   avoid.** a pote
91940 6e 74 69 61 6c 20 73 65 63 75 72 69 74 79 20 70  ntial security p
91950 72 6f 62 6c 65 6d 2c 20 77 65 20 64 6f 20 6e 6f  roblem, we do no
91960 74 20 61 6c 6c 6f 77 20 74 68 65 20 66 69 6c 65  t allow the file
91970 20 74 6f 20 68 61 76 65 0a 2a 2a 20 70 72 65 76   to have.** prev
91980 69 6f 75 73 6c 79 20 65 78 69 73 74 65 64 2e 20  iously existed. 
91990 20 4e 6f 72 20 64 6f 20 77 65 20 61 6c 6c 6f 77   Nor do we allow
919a0 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65 20   the file to be 
919b0 61 20 73 79 6d 62 6f 6c 69 63 0a 2a 2a 20 6c 69  a symbolic.** li
919c0 6e 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 64 65 6c  nk..**.** If del
919d0 46 6c 61 67 20 69 73 20 74 72 75 65 2c 20 74 68  Flag is true, th
919e0 65 6e 20 6d 61 6b 65 20 61 72 72 61 6e 67 65 6d  en make arrangem
919f0 65 6e 74 73 20 74 6f 20 61 75 74 6f 6d 61 74 69  ents to automati
91a00 63 61 6c 6c 79 20 64 65 6c 65 74 65 0a 2a 2a 20  cally delete.** 
91a10 74 68 65 20 66 69 6c 65 20 77 68 65 6e 20 69 74  the file when it
91a20 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a   is closed..**.*
91a30 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 77 72  * On success, wr
91a40 69 74 65 20 74 68 65 20 66 69 6c 65 20 68 61 6e  ite the file han
91a50 64 6c 65 20 69 6e 74 6f 20 2a 69 64 20 61 6e 64  dle into *id and
91a60 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
91a70 4b 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 66 61 69 6c  K..**.** On fail
91a80 75 72 65 2c 20 72 65 74 75 72 6e 20 53 51 4c 49  ure, return SQLI
91a90 54 45 5f 43 41 4e 54 4f 50 45 4e 2e 0a 2a 2a 0a  TE_CANTOPEN..**.
91aa0 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 69 66 20  ** Sometimes if 
91ab0 77 65 20 68 61 76 65 20 6a 75 73 74 20 64 65 6c  we have just del
91ac0 65 74 65 64 20 61 20 70 72 69 6f 72 20 6a 6f 75  eted a prior jou
91ad0 72 6e 61 6c 20 66 69 6c 65 2c 20 77 69 6e 64 6f  rnal file, windo
91ae0 77 73 0a 2a 2a 20 77 69 6c 6c 20 66 61 69 6c 20  ws.** will fail 
91af0 74 6f 20 6f 70 65 6e 20 61 20 6e 65 77 20 6f 6e  to open a new on
91b00 65 20 62 65 63 61 75 73 65 20 74 68 65 72 65 20  e because there 
91b10 69 73 20 61 20 22 70 65 6e 64 69 6e 67 20 64 65  is a "pending de
91b20 6c 65 74 65 22 2e 0a 2a 2a 20 54 6f 20 77 6f 72  lete"..** To wor
91b30 6b 20 61 72 6f 75 6e 64 20 74 68 69 73 20 62 75  k around this bu
91b40 67 2c 20 77 65 20 70 61 75 73 65 20 66 6f 72 20  g, we pause for 
91b50 31 30 30 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73  100 milliseconds
91b60 20 61 6e 64 20 61 74 74 65 6d 70 74 0a 2a 2a 20   and attempt.** 
91b70 61 20 73 65 63 6f 6e 64 20 6f 70 65 6e 20 61 66  a second open af
91b80 74 65 72 20 74 68 65 20 66 69 72 73 74 20 6f 6e  ter the first on
91b90 65 20 66 61 69 6c 73 2e 20 20 54 68 65 20 77 68  e fails.  The wh
91ba0 6f 6c 65 20 6f 70 65 72 61 74 69 6f 6e 20 6f 6e  ole operation on
91bb0 6c 79 0a 2a 2a 20 66 61 69 6c 73 20 69 66 20 62  ly.** fails if b
91bc0 6f 74 68 20 6f 70 65 6e 20 61 74 74 65 6d 70 74  oth open attempt
91bd0 73 20 61 72 65 20 75 6e 73 75 63 63 65 73 73 66  s are unsuccessf
91be0 75 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ul..*/.SQLITE_PR
91bf0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
91c00 33 57 69 6e 4f 70 65 6e 45 78 63 6c 75 73 69 76  3WinOpenExclusiv
91c10 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  e(const char *zF
91c20 69 6c 65 6e 61 6d 65 2c 20 4f 73 46 69 6c 65 20  ilename, OsFile 
91c30 2a 2a 70 49 64 2c 20 69 6e 74 20 64 65 6c 46 6c  **pId, int delFl
91c40 61 67 29 7b 0a 20 20 77 69 6e 46 69 6c 65 20 66  ag){.  winFile f
91c50 3b 0a 20 20 48 41 4e 44 4c 45 20 68 3b 0a 20 20  ;.  HANDLE h;.  
91c60 44 57 4f 52 44 20 66 69 6c 65 66 6c 61 67 73 3b  DWORD fileflags;
91c70 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72  .  void *zConver
91c80 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66  ted = convertUtf
91c90 38 46 69 6c 65 6e 61 6d 65 28 7a 46 69 6c 65 6e  8Filename(zFilen
91ca0 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e  ame);.  if( zCon
91cb0 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20  verted==0 ){.   
91cc0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
91cd0 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 61 73 73 65  OMEM;.  }.  asse
91ce0 72 74 28 20 2a 70 49 64 20 3d 3d 20 30 20 29 3b  rt( *pId == 0 );
91cf0 0a 20 20 66 69 6c 65 66 6c 61 67 73 20 3d 20 46  .  fileflags = F
91d00 49 4c 45 5f 46 4c 41 47 5f 52 41 4e 44 4f 4d 5f  ILE_FLAG_RANDOM_
91d10 41 43 43 45 53 53 3b 0a 23 69 66 20 21 4f 53 5f  ACCESS;.#if !OS_
91d20 57 49 4e 43 45 0a 20 20 69 66 28 20 64 65 6c 46  WINCE.  if( delF
91d30 6c 61 67 20 29 7b 0a 20 20 20 20 66 69 6c 65 66  lag ){.    filef
91d40 6c 61 67 73 20 7c 3d 20 46 49 4c 45 5f 41 54 54  lags |= FILE_ATT
91d50 52 49 42 55 54 45 5f 54 45 4d 50 4f 52 41 52 59  RIBUTE_TEMPORARY
91d60 20 7c 20 46 49 4c 45 5f 46 4c 41 47 5f 44 45 4c   | FILE_FLAG_DEL
91d70 45 54 45 5f 4f 4e 5f 43 4c 4f 53 45 3b 0a 20 20  ETE_ON_CLOSE;.  
91d80 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 69  }.#endif.  if( i
91d90 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 69 6e 74  sNT() ){.    int
91da0 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 64 6f   cnt = 0;.    do
91db0 7b 0a 20 20 20 20 20 20 68 20 3d 20 43 72 65 61  {.      h = Crea
91dc0 74 65 46 69 6c 65 57 28 28 57 43 48 41 52 2a 29  teFileW((WCHAR*)
91dd0 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20 20 20 20  zConverted,.    
91de0 20 20 20 20 20 47 45 4e 45 52 49 43 5f 52 45 41       GENERIC_REA
91df0 44 20 7c 20 47 45 4e 45 52 49 43 5f 57 52 49 54  D | GENERIC_WRIT
91e00 45 2c 0a 20 20 20 20 20 20 20 20 20 30 2c 0a 20  E,.         0,. 
91e10 20 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20          NULL,.  
91e20 20 20 20 20 20 20 20 43 52 45 41 54 45 5f 41 4c         CREATE_AL
91e30 57 41 59 53 2c 0a 20 20 20 20 20 20 20 20 20 66  WAYS,.         f
91e40 69 6c 65 66 6c 61 67 73 2c 0a 20 20 20 20 20 20  ileflags,.      
91e50 20 20 20 4e 55 4c 4c 0a 20 20 20 20 20 20 29 3b     NULL.      );
91e60 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 68 3d 3d  .    }while( h==
91e70 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c 45 5f 56  INVALID_HANDLE_V
91e80 41 4c 55 45 20 26 26 20 63 6e 74 2b 2b 20 3c 20  ALUE && cnt++ < 
91e90 32 20 26 26 20 28 53 6c 65 65 70 28 31 30 30 29  2 && (Sleep(100)
91ea0 2c 20 31 29 20 29 3b 0a 20 20 7d 65 6c 73 65 7b  , 1) );.  }else{
91eb0 0a 23 69 66 20 4f 53 5f 57 49 4e 43 45 0a 20 20  .#if OS_WINCE.  
91ec0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
91ed0 4e 4f 4d 45 4d 3b 0a 23 65 6c 73 65 0a 20 20 20  NOMEM;.#else.   
91ee0 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20   int cnt = 0;.  
91ef0 20 20 64 6f 7b 0a 20 20 20 20 20 20 68 20 3d 20    do{.      h = 
91f00 43 72 65 61 74 65 46 69 6c 65 41 28 28 63 68 61  CreateFileA((cha
91f10 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20  r*)zConverted,. 
91f20 20 20 20 20 20 20 20 47 45 4e 45 52 49 43 5f 52         GENERIC_R
91f30 45 41 44 20 7c 20 47 45 4e 45 52 49 43 5f 57 52  EAD | GENERIC_WR
91f40 49 54 45 2c 0a 20 20 20 20 20 20 20 20 30 2c 0a  ITE,.        0,.
91f50 20 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20          NULL,.  
91f60 20 20 20 20 20 20 43 52 45 41 54 45 5f 41 4c 57        CREATE_ALW
91f70 41 59 53 2c 0a 20 20 20 20 20 20 20 20 66 69 6c  AYS,.        fil
91f80 65 66 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 20  eflags,.        
91f90 4e 55 4c 4c 0a 20 20 20 20 20 20 29 3b 0a 20 20  NULL.      );.  
91fa0 20 20 7d 77 68 69 6c 65 28 20 68 3d 3d 49 4e 56    }while( h==INV
91fb0 41 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55  ALID_HANDLE_VALU
91fc0 45 20 26 26 20 63 6e 74 2b 2b 20 3c 20 32 20 26  E && cnt++ < 2 &
91fd0 26 20 28 53 6c 65 65 70 28 31 30 30 29 2c 20 31  & (Sleep(100), 1
91fe0 29 20 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 4f  ) );.#endif /* O
91ff0 53 5f 57 49 4e 43 45 20 2a 2f 0a 20 20 7d 0a 23  S_WINCE */.  }.#
92000 69 66 20 4f 53 5f 57 49 4e 43 45 0a 20 20 69 66  if OS_WINCE.  if
92010 28 20 64 65 6c 46 6c 61 67 20 26 26 20 68 21 3d  ( delFlag && h!=
92020 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c 45 5f 56  INVALID_HANDLE_V
92030 41 4c 55 45 20 29 7b 0a 20 20 20 20 66 2e 7a 44  ALUE ){.    f.zD
92040 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 20 3d 20 7a  eleteOnClose = z
92050 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 20 20 7a  Converted;.    z
92060 43 6f 6e 76 65 72 74 65 64 20 3d 20 30 3b 0a 20  Converted = 0;. 
92070 20 7d 0a 20 20 66 2e 68 4d 75 74 65 78 20 3d 20   }.  f.hMutex = 
92080 4e 55 4c 4c 3b 0a 23 65 6e 64 69 66 0a 20 20 73  NULL;.#endif.  s
92090 71 6c 69 74 65 46 72 65 65 28 7a 43 6f 6e 76 65  qliteFree(zConve
920a0 72 74 65 64 29 3b 0a 20 20 69 66 28 20 68 3d 3d  rted);.  if( h==
920b0 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c 45 5f 56  INVALID_HANDLE_V
920c0 41 4c 55 45 20 29 7b 0a 20 20 20 20 72 65 74 75  ALUE ){.    retu
920d0 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50  rn SQLITE_CANTOP
920e0 45 4e 3b 0a 20 20 7d 0a 20 20 66 2e 68 20 3d 20  EN;.  }.  f.h = 
920f0 68 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 22 4f  h;.  OSTRACE3("O
92100 50 45 4e 20 45 58 20 25 64 20 5c 22 25 73 5c 22  PEN EX %d \"%s\"
92110 5c 6e 22 2c 20 68 2c 20 7a 46 69 6c 65 6e 61 6d  \n", h, zFilenam
92120 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 61 6c 6c  e);.  return all
92130 6f 63 61 74 65 57 69 6e 46 69 6c 65 28 26 66 2c  ocateWinFile(&f,
92140 20 70 49 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   pId);.}../*.** 
92150 41 74 74 65 6d 70 74 20 74 6f 20 6f 70 65 6e 20  Attempt to open 
92160 61 20 6e 65 77 20 66 69 6c 65 20 66 6f 72 20 72  a new file for r
92170 65 61 64 2d 6f 6e 6c 79 20 61 63 63 65 73 73 2e  ead-only access.
92180 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73  .**.** On succes
92190 73 2c 20 77 72 69 74 65 20 74 68 65 20 66 69 6c  s, write the fil
921a0 65 20 68 61 6e 64 6c 65 20 69 6e 74 6f 20 2a 69  e handle into *i
921b0 64 20 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c  d and return SQL
921c0 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 4f 6e  ITE_OK..**.** On
921d0 20 66 61 69 6c 75 72 65 2c 20 72 65 74 75 72 6e   failure, return
921e0 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e   SQLITE_CANTOPEN
921f0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
92200 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57  ATE int sqlite3W
92210 69 6e 4f 70 65 6e 52 65 61 64 4f 6e 6c 79 28 63  inOpenReadOnly(c
92220 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
92230 6e 61 6d 65 2c 20 4f 73 46 69 6c 65 20 2a 2a 70  name, OsFile **p
92240 49 64 29 7b 0a 20 20 77 69 6e 46 69 6c 65 20 66  Id){.  winFile f
92250 3b 0a 20 20 48 41 4e 44 4c 45 20 68 3b 0a 20 20  ;.  HANDLE h;.  
92260 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64  void *zConverted
92270 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69   = convertUtf8Fi
92280 6c 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65  lename(zFilename
92290 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76 65 72  );.  if( zConver
922a0 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  ted==0 ){.    re
922b0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
922c0 4d 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  M;.  }.  assert(
922d0 20 2a 70 49 64 3d 3d 30 20 29 3b 0a 20 20 69 66   *pId==0 );.  if
922e0 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20  ( isNT() ){.    
922f0 68 20 3d 20 43 72 65 61 74 65 46 69 6c 65 57 28  h = CreateFileW(
92300 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74  (WCHAR*)zConvert
92310 65 64 2c 0a 20 20 20 20 20 20 20 47 45 4e 45 52  ed,.       GENER
92320 49 43 5f 52 45 41 44 2c 0a 20 20 20 20 20 20 20  IC_READ,.       
92330 30 2c 0a 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a  0,.       NULL,.
92340 20 20 20 20 20 20 20 4f 50 45 4e 5f 45 58 49 53         OPEN_EXIS
92350 54 49 4e 47 2c 0a 20 20 20 20 20 20 20 46 49 4c  TING,.       FIL
92360 45 5f 41 54 54 52 49 42 55 54 45 5f 4e 4f 52 4d  E_ATTRIBUTE_NORM
92370 41 4c 20 7c 20 46 49 4c 45 5f 46 4c 41 47 5f 52  AL | FILE_FLAG_R
92380 41 4e 44 4f 4d 5f 41 43 43 45 53 53 2c 0a 20 20  ANDOM_ACCESS,.  
92390 20 20 20 20 20 4e 55 4c 4c 0a 20 20 20 20 29 3b       NULL.    );
923a0 0a 20 20 7d 65 6c 73 65 7b 0a 23 69 66 20 4f 53  .  }else{.#if OS
923b0 5f 57 49 4e 43 45 0a 20 20 20 20 72 65 74 75 72  _WINCE.    retur
923c0 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
923d0 23 65 6c 73 65 0a 20 20 20 20 68 20 3d 20 43 72  #else.    h = Cr
923e0 65 61 74 65 46 69 6c 65 41 28 28 63 68 61 72 2a  eateFileA((char*
923f0 29 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20 20 20  )zConverted,.   
92400 20 20 20 20 47 45 4e 45 52 49 43 5f 52 45 41 44      GENERIC_READ
92410 2c 0a 20 20 20 20 20 20 20 30 2c 0a 20 20 20 20  ,.       0,.    
92420 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20     NULL,.       
92430 4f 50 45 4e 5f 45 58 49 53 54 49 4e 47 2c 0a 20  OPEN_EXISTING,. 
92440 20 20 20 20 20 20 46 49 4c 45 5f 41 54 54 52 49        FILE_ATTRI
92450 42 55 54 45 5f 4e 4f 52 4d 41 4c 20 7c 20 46 49  BUTE_NORMAL | FI
92460 4c 45 5f 46 4c 41 47 5f 52 41 4e 44 4f 4d 5f 41  LE_FLAG_RANDOM_A
92470 43 43 45 53 53 2c 0a 20 20 20 20 20 20 20 4e 55  CCESS,.       NU
92480 4c 4c 0a 20 20 20 20 29 3b 0a 23 65 6e 64 69 66  LL.    );.#endif
92490 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 46 72 65  .  }.  sqliteFre
924a0 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20  e(zConverted);. 
924b0 20 69 66 28 20 68 3d 3d 49 4e 56 41 4c 49 44 5f   if( h==INVALID_
924c0 48 41 4e 44 4c 45 5f 56 41 4c 55 45 20 29 7b 0a  HANDLE_VALUE ){.
924d0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
924e0 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a  E_CANTOPEN;.  }.
924f0 20 20 66 2e 68 20 3d 20 68 3b 0a 23 69 66 20 4f    f.h = h;.#if O
92500 53 5f 57 49 4e 43 45 0a 20 20 66 2e 7a 44 65 6c  S_WINCE.  f.zDel
92510 65 74 65 4f 6e 43 6c 6f 73 65 20 3d 20 30 3b 0a  eteOnClose = 0;.
92520 20 20 66 2e 68 4d 75 74 65 78 20 3d 20 4e 55 4c    f.hMutex = NUL
92530 4c 3b 0a 23 65 6e 64 69 66 0a 20 20 4f 53 54 52  L;.#endif.  OSTR
92540 41 43 45 33 28 22 4f 50 45 4e 20 52 4f 20 25 64  ACE3("OPEN RO %d
92550 20 5c 22 25 73 5c 22 5c 6e 22 2c 20 68 2c 20 7a   \"%s\"\n", h, z
92560 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 72 65 74  Filename);.  ret
92570 75 72 6e 20 61 6c 6c 6f 63 61 74 65 57 69 6e 46  urn allocateWinF
92580 69 6c 65 28 26 66 2c 20 70 49 64 29 3b 0a 7d 0a  ile(&f, pId);.}.
92590 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74  ./*.** Attempt t
925a0 6f 20 6f 70 65 6e 20 61 20 66 69 6c 65 20 64 65  o open a file de
925b0 73 63 72 69 70 74 6f 72 20 66 6f 72 20 74 68 65  scriptor for the
925c0 20 64 69 72 65 63 74 6f 72 79 20 74 68 61 74 20   directory that 
925d0 63 6f 6e 74 61 69 6e 73 20 61 0a 2a 2a 20 66 69  contains a.** fi
925e0 6c 65 2e 20 20 54 68 69 73 20 66 69 6c 65 20 64  le.  This file d
925f0 65 73 63 72 69 70 74 6f 72 20 63 61 6e 20 62 65  escriptor can be
92600 20 75 73 65 64 20 74 6f 20 66 73 79 6e 63 28 29   used to fsync()
92610 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 0a 2a   the directory.*
92620 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6d 61  * in order to ma
92630 6b 65 20 73 75 72 65 20 74 68 65 20 63 72 65 61  ke sure the crea
92640 74 69 6f 6e 20 6f 66 20 61 20 6e 65 77 20 66 69  tion of a new fi
92650 6c 65 20 69 73 20 61 63 74 75 61 6c 6c 79 20 77  le is actually w
92660 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 64 69 73  ritten.** to dis
92670 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  k..**.** This ro
92680 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79 20 6d 65  utine is only me
92690 61 6e 69 6e 67 66 75 6c 20 66 6f 72 20 55 6e 69  aningful for Uni
926a0 78 2e 20 20 49 74 20 69 73 20 61 20 6e 6f 2d 6f  x.  It is a no-o
926b0 70 20 75 6e 64 65 72 0a 2a 2a 20 77 69 6e 64 6f  p under.** windo
926c0 77 73 20 73 69 6e 63 65 20 77 69 6e 64 6f 77 73  ws since windows
926d0 20 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72   does not suppor
926e0 74 20 68 61 72 64 20 6c 69 6e 6b 73 2e 0a 2a 2a  t hard links..**
926f0 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c 20  .** On success, 
92700 61 20 68 61 6e 64 6c 65 20 66 6f 72 20 61 20 70  a handle for a p
92710 72 65 76 69 6f 75 73 6c 79 20 6f 70 65 6e 20 66  reviously open f
92720 69 6c 65 20 69 73 20 61 74 20 2a 69 64 20 69 73  ile is at *id is
92730 0a 2a 2a 20 75 70 64 61 74 65 64 20 77 69 74 68  .** updated with
92740 20 74 68 65 20 6e 65 77 20 64 69 72 65 63 74 6f   the new directo
92750 72 79 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  ry file descript
92760 6f 72 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b  or and SQLITE_OK
92770 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e   is.** returned.
92780 0a 2a 2a 0a 2a 2a 20 4f 6e 20 66 61 69 6c 75 72  .**.** On failur
92790 65 2c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  e, the function 
927a0 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 43  returns SQLITE_C
927b0 41 4e 54 4f 50 45 4e 20 61 6e 64 20 6c 65 61 76  ANTOPEN and leav
927c0 65 73 0a 2a 2a 20 2a 69 64 20 75 6e 63 68 61 6e  es.** *id unchan
927d0 67 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ged..*/.static i
927e0 6e 74 20 77 69 6e 4f 70 65 6e 44 69 72 65 63 74  nt winOpenDirect
927f0 6f 72 79 28 0a 20 20 4f 73 46 69 6c 65 20 2a 69  ory(.  OsFile *i
92800 64 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  d,.  const char 
92810 2a 7a 44 69 72 6e 61 6d 65 0a 29 7b 0a 20 20 72  *zDirname.){.  r
92820 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
92830 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65  .}../*.** Create
92840 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c   a temporary fil
92850 65 20 6e 61 6d 65 20 69 6e 20 7a 42 75 66 2e 20  e name in zBuf. 
92860 20 7a 42 75 66 20 6d 75 73 74 20 62 65 20 62 69   zBuf must be bi
92870 67 20 65 6e 6f 75 67 68 20 74 6f 0a 2a 2a 20 68  g enough to.** h
92880 6f 6c 64 20 61 74 20 6c 65 61 73 74 20 53 51 4c  old at least SQL
92890 49 54 45 5f 54 45 4d 50 4e 41 4d 45 5f 53 49 5a  ITE_TEMPNAME_SIZ
928a0 45 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2f  E characters..*/
928b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
928c0 69 6e 74 20 73 71 6c 69 74 65 33 57 69 6e 54 65  int sqlite3WinTe
928d0 6d 70 46 69 6c 65 4e 61 6d 65 28 63 68 61 72 20  mpFileName(char 
928e0 2a 7a 42 75 66 29 7b 0a 20 20 73 74 61 74 69 63  *zBuf){.  static
928f0 20 63 68 61 72 20 7a 43 68 61 72 73 5b 5d 20 3d   char zChars[] =
92900 0a 20 20 20 20 22 61 62 63 64 65 66 67 68 69 6a  .    "abcdefghij
92910 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a  klmnopqrstuvwxyz
92920 22 0a 20 20 20 20 22 41 42 43 44 45 46 47 48 49  ".    "ABCDEFGHI
92930 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59  JKLMNOPQRSTUVWXY
92940 5a 22 0a 20 20 20 20 22 30 31 32 33 34 35 36 37  Z".    "01234567
92950 38 39 22 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b  89";.  int i, j;
92960 0a 20 20 63 68 61 72 20 7a 54 65 6d 70 50 61 74  .  char zTempPat
92970 68 5b 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d  h[SQLITE_TEMPNAM
92980 45 5f 53 49 5a 45 5d 3b 0a 20 20 69 66 28 20 73  E_SIZE];.  if( s
92990 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65  qlite3_temp_dire
929a0 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 73 74 72  ctory ){.    str
929b0 6e 63 70 79 28 7a 54 65 6d 70 50 61 74 68 2c 20  ncpy(zTempPath, 
929c0 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72  sqlite3_temp_dir
929d0 65 63 74 6f 72 79 2c 20 53 51 4c 49 54 45 5f 54  ectory, SQLITE_T
929e0 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 2d 33 30 29  EMPNAME_SIZE-30)
929f0 3b 0a 20 20 20 20 7a 54 65 6d 70 50 61 74 68 5b  ;.    zTempPath[
92a00 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45 5f  SQLITE_TEMPNAME_
92a10 53 49 5a 45 2d 33 30 5d 20 3d 20 30 3b 0a 20 20  SIZE-30] = 0;.  
92a20 7d 65 6c 73 65 20 69 66 28 20 69 73 4e 54 28 29  }else if( isNT()
92a30 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d   ){.    char *zM
92a40 75 6c 74 69 3b 0a 20 20 20 20 57 43 48 41 52 20  ulti;.    WCHAR 
92a50 7a 57 69 64 65 50 61 74 68 5b 53 51 4c 49 54 45  zWidePath[SQLITE
92a60 5f 54 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 5d 3b  _TEMPNAME_SIZE];
92a70 0a 20 20 20 20 47 65 74 54 65 6d 70 50 61 74 68  .    GetTempPath
92a80 57 28 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d  W(SQLITE_TEMPNAM
92a90 45 5f 53 49 5a 45 2d 33 30 2c 20 7a 57 69 64 65  E_SIZE-30, zWide
92aa0 50 61 74 68 29 3b 0a 20 20 20 20 7a 4d 75 6c 74  Path);.    zMult
92ab0 69 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74 66  i = unicodeToUtf
92ac0 38 28 7a 57 69 64 65 50 61 74 68 29 3b 0a 20 20  8(zWidePath);.  
92ad0 20 20 69 66 28 20 7a 4d 75 6c 74 69 20 29 7b 0a    if( zMulti ){.
92ae0 20 20 20 20 20 20 73 74 72 6e 63 70 79 28 7a 54        strncpy(zT
92af0 65 6d 70 50 61 74 68 2c 20 7a 4d 75 6c 74 69 2c  empPath, zMulti,
92b00 20 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45   SQLITE_TEMPNAME
92b10 5f 53 49 5a 45 2d 33 30 29 3b 0a 20 20 20 20 20  _SIZE-30);.     
92b20 20 7a 54 65 6d 70 50 61 74 68 5b 53 51 4c 49 54   zTempPath[SQLIT
92b30 45 5f 54 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 2d  E_TEMPNAME_SIZE-
92b40 33 30 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 73  30] = 0;.      s
92b50 71 6c 69 74 65 46 72 65 65 28 7a 4d 75 6c 74 69  qliteFree(zMulti
92b60 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
92b70 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
92b80 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20  E_NOMEM;.    }. 
92b90 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72   }else{.    char
92ba0 20 2a 7a 55 74 66 38 3b 0a 20 20 20 20 63 68 61   *zUtf8;.    cha
92bb0 72 20 7a 4d 62 63 73 50 61 74 68 5b 53 51 4c 49  r zMbcsPath[SQLI
92bc0 54 45 5f 54 45 4d 50 4e 41 4d 45 5f 53 49 5a 45  TE_TEMPNAME_SIZE
92bd0 5d 3b 0a 20 20 20 20 47 65 74 54 65 6d 70 50 61  ];.    GetTempPa
92be0 74 68 41 28 53 51 4c 49 54 45 5f 54 45 4d 50 4e  thA(SQLITE_TEMPN
92bf0 41 4d 45 5f 53 49 5a 45 2d 33 30 2c 20 7a 4d 62  AME_SIZE-30, zMb
92c00 63 73 50 61 74 68 29 3b 0a 20 20 20 20 7a 55 74  csPath);.    zUt
92c10 66 38 20 3d 20 6d 62 63 73 54 6f 55 74 66 38 28  f8 = mbcsToUtf8(
92c20 7a 4d 62 63 73 50 61 74 68 29 3b 0a 20 20 20 20  zMbcsPath);.    
92c30 69 66 28 20 7a 55 74 66 38 20 29 7b 0a 20 20 20  if( zUtf8 ){.   
92c40 20 20 20 73 74 72 6e 63 70 79 28 7a 54 65 6d 70     strncpy(zTemp
92c50 50 61 74 68 2c 20 7a 55 74 66 38 2c 20 53 51 4c  Path, zUtf8, SQL
92c60 49 54 45 5f 54 45 4d 50 4e 41 4d 45 5f 53 49 5a  ITE_TEMPNAME_SIZ
92c70 45 2d 33 30 29 3b 0a 20 20 20 20 20 20 7a 54 65  E-30);.      zTe
92c80 6d 70 50 61 74 68 5b 53 51 4c 49 54 45 5f 54 45  mpPath[SQLITE_TE
92c90 4d 50 4e 41 4d 45 5f 53 49 5a 45 2d 33 30 5d 20  MPNAME_SIZE-30] 
92ca0 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  = 0;.      sqlit
92cb0 65 46 72 65 65 28 7a 55 74 66 38 29 3b 0a 20 20  eFree(zUtf8);.  
92cc0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
92cd0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
92ce0 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  EM;.    }.  }.  
92cf0 66 6f 72 28 69 3d 73 74 72 6c 65 6e 28 7a 54 65  for(i=strlen(zTe
92d00 6d 70 50 61 74 68 29 3b 20 69 3e 30 20 26 26 20  mpPath); i>0 && 
92d10 7a 54 65 6d 70 50 61 74 68 5b 69 2d 31 5d 3d 3d  zTempPath[i-1]==
92d20 27 5c 5c 27 3b 20 69 2d 2d 29 7b 7d 0a 20 20 7a  '\\'; i--){}.  z
92d30 54 65 6d 70 50 61 74 68 5b 69 5d 20 3d 20 30 3b  TempPath[i] = 0;
92d40 0a 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 20  .  for(;;){.    
92d50 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
92d60 28 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45  (SQLITE_TEMPNAME
92d70 5f 53 49 5a 45 2c 20 7a 42 75 66 2c 0a 20 20 20  _SIZE, zBuf,.   
92d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
92d90 20 20 22 25 73 5c 5c 22 54 45 4d 50 5f 46 49 4c    "%s\\"TEMP_FIL
92da0 45 5f 50 52 45 46 49 58 2c 20 7a 54 65 6d 70 50  E_PREFIX, zTempP
92db0 61 74 68 29 3b 0a 20 20 20 20 6a 20 3d 20 73 74  ath);.    j = st
92dc0 72 6c 65 6e 28 7a 42 75 66 29 3b 0a 20 20 20 20  rlen(zBuf);.    
92dd0 73 71 6c 69 74 65 33 52 61 6e 64 6f 6d 6e 65 73  sqlite3Randomnes
92de0 73 28 31 35 2c 20 26 7a 42 75 66 5b 6a 5d 29 3b  s(15, &zBuf[j]);
92df0 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
92e00 31 35 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20  15; i++, j++){. 
92e10 20 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 28       zBuf[j] = (
92e20 63 68 61 72 29 7a 43 68 61 72 73 5b 20 28 28 75  char)zChars[ ((u
92e30 6e 73 69 67 6e 65 64 20 63 68 61 72 29 7a 42 75  nsigned char)zBu
92e40 66 5b 6a 5d 29 25 28 73 69 7a 65 6f 66 28 7a 43  f[j])%(sizeof(zC
92e50 68 61 72 73 29 2d 31 29 20 5d 3b 0a 20 20 20 20  hars)-1) ];.    
92e60 7d 0a 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20  }.    zBuf[j] = 
92e70 30 3b 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69  0;.    if( !sqli
92e80 74 65 33 4f 73 46 69 6c 65 45 78 69 73 74 73 28  te3OsFileExists(
92e90 7a 42 75 66 29 20 29 20 62 72 65 61 6b 3b 0a 20  zBuf) ) break;. 
92ea0 20 7d 0a 20 20 4f 53 54 52 41 43 45 32 28 22 54   }.  OSTRACE2("T
92eb0 45 4d 50 20 46 49 4c 45 4e 41 4d 45 3a 20 25 73  EMP FILENAME: %s
92ec0 5c 6e 22 2c 20 7a 42 75 66 29 3b 0a 20 20 72 65  \n", zBuf);.  re
92ed0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  turn SQLITE_OK; 
92ee0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20  .}../*.** Close 
92ef0 61 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 74  a file..**.** It
92f00 20 69 73 20 72 65 70 6f 72 74 65 64 20 74 68 61   is reported tha
92f10 74 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20  t an attempt to 
92f20 63 6c 6f 73 65 20 61 20 68 61 6e 64 6c 65 20 6d  close a handle m
92f30 69 67 68 74 20 73 6f 6d 65 74 69 6d 65 73 0a 2a  ight sometimes.*
92f40 2a 20 66 61 69 6c 2e 20 20 54 68 69 73 20 69 73  * fail.  This is
92f50 20 61 20 76 65 72 79 20 75 6e 72 65 61 73 6f 6e   a very unreason
92f60 61 62 6c 65 20 72 65 73 75 6c 74 2c 20 62 75 74  able result, but
92f70 20 77 69 6e 64 6f 77 73 20 69 73 20 6e 6f 74 6f   windows is noto
92f80 72 69 6f 75 73 0a 2a 2a 20 66 6f 72 20 62 65 69  rious.** for bei
92f90 6e 67 20 75 6e 72 65 61 73 6f 6e 61 62 6c 65 20  ng unreasonable 
92fa0 73 6f 20 49 20 64 6f 20 6e 6f 74 20 64 6f 75 62  so I do not doub
92fb0 74 20 74 68 61 74 20 69 74 20 6d 69 67 68 74 20  t that it might 
92fc0 68 61 70 70 65 6e 2e 20 20 49 66 0a 2a 2a 20 74  happen.  If.** t
92fd0 68 65 20 63 6c 6f 73 65 20 66 61 69 6c 73 2c 20  he close fails, 
92fe0 77 65 20 70 61 75 73 65 20 66 6f 72 20 31 30 30  we pause for 100
92ff0 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 61 6e   milliseconds an
93000 64 20 74 72 79 20 61 67 61 69 6e 2e 20 20 41 73  d try again.  As
93010 0a 2a 2a 20 6d 61 6e 79 20 61 73 20 4d 58 5f 43  .** many as MX_C
93020 4c 4f 53 45 5f 41 54 54 45 4d 50 54 20 61 74 74  LOSE_ATTEMPT att
93030 65 6d 70 74 73 20 74 6f 20 63 6c 6f 73 65 20 74  empts to close t
93040 68 65 20 68 61 6e 64 6c 65 20 61 72 65 20 6d 61  he handle are ma
93050 64 65 20 62 65 66 6f 72 65 0a 2a 2a 20 67 69 76  de before.** giv
93060 69 6e 67 20 75 70 20 61 6e 64 20 72 65 74 75 72  ing up and retur
93070 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 2e 0a 2a  ning an error..*
93080 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f 43 4c 4f  /.#define MX_CLO
93090 53 45 5f 41 54 54 45 4d 50 54 20 33 0a 73 74 61  SE_ATTEMPT 3.sta
930a0 74 69 63 20 69 6e 74 20 77 69 6e 43 6c 6f 73 65  tic int winClose
930b0 28 4f 73 46 69 6c 65 20 2a 2a 70 49 64 29 7b 0a  (OsFile **pId){.
930c0 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65    winFile *pFile
930d0 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 31 3b 0a  ;.  int rc = 1;.
930e0 20 20 69 66 28 20 70 49 64 20 26 26 20 28 70 46    if( pId && (pF
930f0 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29  ile = (winFile*)
93100 2a 70 49 64 29 21 3d 30 20 29 7b 0a 20 20 20 20  *pId)!=0 ){.    
93110 69 6e 74 20 72 63 2c 20 63 6e 74 20 3d 20 30 3b  int rc, cnt = 0;
93120 0a 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 43  .    OSTRACE2("C
93130 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 70 46 69 6c  LOSE %d\n", pFil
93140 65 2d 3e 68 29 3b 0a 20 20 20 20 64 6f 7b 0a 20  e->h);.    do{. 
93150 20 20 20 20 20 72 63 20 3d 20 43 6c 6f 73 65 48       rc = CloseH
93160 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 29 3b  andle(pFile->h);
93170 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 72 63 3d  .    }while( rc=
93180 3d 30 20 26 26 20 63 6e 74 2b 2b 20 3c 20 4d 58  =0 && cnt++ < MX
93190 5f 43 4c 4f 53 45 5f 41 54 54 45 4d 50 54 20 26  _CLOSE_ATTEMPT &
931a0 26 20 28 53 6c 65 65 70 28 31 30 30 29 2c 20 31  & (Sleep(100), 1
931b0 29 20 29 3b 0a 23 69 66 20 4f 53 5f 57 49 4e 43  ) );.#if OS_WINC
931c0 45 0a 20 20 20 20 77 69 6e 63 65 44 65 73 74 72  E.    winceDestr
931d0 6f 79 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 23  oyLock(pFile);.#
931e0 65 6e 64 69 66 0a 20 20 20 20 4f 70 65 6e 43 6f  endif.    OpenCo
931f0 75 6e 74 65 72 28 2d 31 29 3b 0a 20 20 20 20 73  unter(-1);.    s
93200 71 6c 69 74 65 46 72 65 65 28 70 46 69 6c 65 29  qliteFree(pFile)
93210 3b 0a 20 20 20 20 2a 70 49 64 20 3d 20 30 3b 0a  ;.    *pId = 0;.
93220 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20    }.  return rc 
93230 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51  ? SQLITE_OK : SQ
93240 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 7d 0a 0a 2f  LITE_IOERR;.}../
93250 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20 66  *.** Read data f
93260 72 6f 6d 20 61 20 66 69 6c 65 20 69 6e 74 6f 20  rom a file into 
93270 61 20 62 75 66 66 65 72 2e 20 20 52 65 74 75 72  a buffer.  Retur
93280 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 61  n SQLITE_OK if a
93290 6c 6c 0a 2a 2a 20 62 79 74 65 73 20 77 65 72 65  ll.** bytes were
932a0 20 72 65 61 64 20 73 75 63 63 65 73 73 66 75 6c   read successful
932b0 6c 79 20 61 6e 64 20 53 51 4c 49 54 45 5f 49 4f  ly and SQLITE_IO
932c0 45 52 52 20 69 66 20 61 6e 79 74 68 69 6e 67 20  ERR if anything 
932d0 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 0a 2a  goes.** wrong..*
932e0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e  /.static int win
932f0 52 65 61 64 28 4f 73 46 69 6c 65 20 2a 69 64 2c  Read(OsFile *id,
93300 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74   void *pBuf, int
93310 20 61 6d 74 29 7b 0a 20 20 44 57 4f 52 44 20 67   amt){.  DWORD g
93320 6f 74 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64  ot;.  assert( id
93330 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74  !=0 );.  Simulat
93340 65 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20  eIOError(return 
93350 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41  SQLITE_IOERR_REA
93360 44 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 22  D);.  OSTRACE3("
93370 52 45 41 44 20 25 64 20 6c 6f 63 6b 3d 25 64 5c  READ %d lock=%d\
93380 6e 22 2c 20 28 28 77 69 6e 46 69 6c 65 2a 29 69  n", ((winFile*)i
93390 64 29 2d 3e 68 2c 20 28 28 77 69 6e 46 69 6c 65  d)->h, ((winFile
933a0 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65 29  *)id)->locktype)
933b0 3b 0a 20 20 69 66 28 20 21 52 65 61 64 46 69 6c  ;.  if( !ReadFil
933c0 65 28 28 28 77 69 6e 46 69 6c 65 2a 29 69 64 29  e(((winFile*)id)
933d0 2d 3e 68 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20  ->h, pBuf, amt, 
933e0 26 67 6f 74 2c 20 30 29 20 29 7b 0a 20 20 20 20  &got, 0) ){.    
933f0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
93400 45 52 52 5f 52 45 41 44 3b 0a 20 20 7d 0a 20 20  ERR_READ;.  }.  
93410 69 66 28 20 67 6f 74 3d 3d 28 44 57 4f 52 44 29  if( got==(DWORD)
93420 61 6d 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72  amt ){.    retur
93430 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
93440 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 73 65 74  else{.    memset
93450 28 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b  (&((char*)pBuf)[
93460 67 6f 74 5d 2c 20 30 2c 20 61 6d 74 2d 67 6f 74  got], 0, amt-got
93470 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
93480 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54  LITE_IOERR_SHORT
93490 5f 52 45 41 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  _READ;.  }.}../*
934a0 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20 66  .** Write data f
934b0 72 6f 6d 20 61 20 62 75 66 66 65 72 20 69 6e 74  rom a buffer int
934c0 6f 20 61 20 66 69 6c 65 2e 20 20 52 65 74 75 72  o a file.  Retur
934d0 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73  n SQLITE_OK on s
934e0 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d  uccess.** or som
934f0 65 20 6f 74 68 65 72 20 65 72 72 6f 72 20 63 6f  e other error co
93500 64 65 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a  de on failure..*
93510 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e  /.static int win
93520 57 72 69 74 65 28 4f 73 46 69 6c 65 20 2a 69 64  Write(OsFile *id
93530 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42  , const void *pB
93540 75 66 2c 20 69 6e 74 20 61 6d 74 29 7b 0a 20 20  uf, int amt){.  
93550 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 44 57  int rc = 0;.  DW
93560 4f 52 44 20 77 72 6f 74 65 3b 0a 20 20 61 73 73  ORD wrote;.  ass
93570 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20  ert( id!=0 );.  
93580 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
93590 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
935a0 45 52 52 5f 52 45 41 44 29 3b 0a 20 20 53 69 6d  ERR_READ);.  Sim
935b0 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72  ulateDiskfullErr
935c0 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45  or(return SQLITE
935d0 5f 46 55 4c 4c 29 3b 0a 20 20 4f 53 54 52 41 43  _FULL);.  OSTRAC
935e0 45 33 28 22 57 52 49 54 45 20 25 64 20 6c 6f 63  E3("WRITE %d loc
935f0 6b 3d 25 64 5c 6e 22 2c 20 28 28 77 69 6e 46 69  k=%d\n", ((winFi
93600 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 28 28 77 69  le*)id)->h, ((wi
93610 6e 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b  nFile*)id)->lock
93620 74 79 70 65 29 3b 0a 20 20 61 73 73 65 72 74 28  type);.  assert(
93630 20 61 6d 74 3e 30 20 29 3b 0a 20 20 77 68 69 6c   amt>0 );.  whil
93640 65 28 20 61 6d 74 3e 30 20 26 26 20 28 72 63 20  e( amt>0 && (rc 
93650 3d 20 57 72 69 74 65 46 69 6c 65 28 28 28 77 69  = WriteFile(((wi
93660 6e 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 70  nFile*)id)->h, p
93670 42 75 66 2c 20 61 6d 74 2c 20 26 77 72 6f 74 65  Buf, amt, &wrote
93680 2c 20 30 29 29 21 3d 30 0a 20 20 20 20 20 20 20  , 0))!=0.       
93690 20 20 26 26 20 77 72 6f 74 65 3e 30 20 29 7b 0a    && wrote>0 ){.
936a0 20 20 20 20 61 6d 74 20 2d 3d 20 77 72 6f 74 65      amt -= wrote
936b0 3b 0a 20 20 20 20 70 42 75 66 20 3d 20 26 28 28  ;.    pBuf = &((
936c0 63 68 61 72 2a 29 70 42 75 66 29 5b 77 72 6f 74  char*)pBuf)[wrot
936d0 65 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 72  e];.  }.  if( !r
936e0 63 20 7c 7c 20 61 6d 74 3e 28 69 6e 74 29 77 72  c || amt>(int)wr
936f0 6f 74 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ote ){.    retur
93700 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20  n SQLITE_FULL;. 
93710 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
93720 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
93730 53 6f 6d 65 20 6d 69 63 72 6f 73 6f 66 74 20 63  Some microsoft c
93740 6f 6d 70 69 6c 65 72 73 20 6c 61 63 6b 20 74 68  ompilers lack th
93750 69 73 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a  is definition..*
93760 2f 0a 23 69 66 6e 64 65 66 20 49 4e 56 41 4c 49  /.#ifndef INVALI
93770 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54  D_SET_FILE_POINT
93780 45 52 0a 23 20 64 65 66 69 6e 65 20 49 4e 56 41  ER.# define INVA
93790 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49  LID_SET_FILE_POI
937a0 4e 54 45 52 20 28 28 44 57 4f 52 44 29 2d 31 29  NTER ((DWORD)-1)
937b0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d  .#endif../*.** M
937c0 6f 76 65 20 74 68 65 20 72 65 61 64 2f 77 72 69  ove the read/wri
937d0 74 65 20 70 6f 69 6e 74 65 72 20 69 6e 20 61 20  te pointer in a 
937e0 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  file..*/.static 
937f0 69 6e 74 20 77 69 6e 53 65 65 6b 28 4f 73 46 69  int winSeek(OsFi
93800 6c 65 20 2a 69 64 2c 20 69 36 34 20 6f 66 66 73  le *id, i64 offs
93810 65 74 29 7b 0a 20 20 4c 4f 4e 47 20 75 70 70 65  et){.  LONG uppe
93820 72 42 69 74 73 20 3d 20 6f 66 66 73 65 74 3e 3e  rBits = offset>>
93830 33 32 3b 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65 72  32;.  LONG lower
93840 42 69 74 73 20 3d 20 6f 66 66 73 65 74 20 26 20  Bits = offset & 
93850 30 78 66 66 66 66 66 66 66 66 3b 0a 20 20 44 57  0xffffffff;.  DW
93860 4f 52 44 20 72 63 3b 0a 20 20 61 73 73 65 72 74  ORD rc;.  assert
93870 28 20 69 64 21 3d 30 20 29 3b 0a 23 69 66 64 65  ( id!=0 );.#ifde
93880 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20  f SQLITE_TEST.  
93890 69 66 28 20 6f 66 66 73 65 74 20 29 20 53 69 6d  if( offset ) Sim
938a0 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72  ulateDiskfullErr
938b0 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45  or(return SQLITE
938c0 5f 46 55 4c 4c 29 3b 0a 23 65 6e 64 69 66 0a 20  _FULL);.#endif. 
938d0 20 72 63 20 3d 20 53 65 74 46 69 6c 65 50 6f 69   rc = SetFilePoi
938e0 6e 74 65 72 28 28 28 77 69 6e 46 69 6c 65 2a 29  nter(((winFile*)
938f0 69 64 29 2d 3e 68 2c 20 6c 6f 77 65 72 42 69 74  id)->h, lowerBit
93900 73 2c 20 26 75 70 70 65 72 42 69 74 73 2c 20 46  s, &upperBits, F
93910 49 4c 45 5f 42 45 47 49 4e 29 3b 0a 20 20 4f 53  ILE_BEGIN);.  OS
93920 54 52 41 43 45 33 28 22 53 45 45 4b 20 25 64 20  TRACE3("SEEK %d 
93930 25 6c 6c 64 5c 6e 22 2c 20 28 28 77 69 6e 46 69  %lld\n", ((winFi
93940 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 6f 66 66 73  le*)id)->h, offs
93950 65 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 49  et);.  if( rc==I
93960 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f  NVALID_SET_FILE_
93970 50 4f 49 4e 54 45 52 20 26 26 20 47 65 74 4c 61  POINTER && GetLa
93980 73 74 45 72 72 6f 72 28 29 21 3d 4e 4f 5f 45 52  stError()!=NO_ER
93990 52 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ROR ){.    retur
939a0 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20  n SQLITE_FULL;. 
939b0 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
939c0 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
939d0 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 77 72  Make sure all wr
939e0 69 74 65 73 20 74 6f 20 61 20 70 61 72 74 69 63  ites to a partic
939f0 75 6c 61 72 20 66 69 6c 65 20 61 72 65 20 63 6f  ular file are co
93a00 6d 6d 69 74 74 65 64 20 74 6f 20 64 69 73 6b 2e  mmitted to disk.
93a10 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
93a20 69 6e 53 79 6e 63 28 4f 73 46 69 6c 65 20 2a 69  inSync(OsFile *i
93a30 64 2c 20 69 6e 74 20 64 61 74 61 4f 6e 6c 79 29  d, int dataOnly)
93a40 7b 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d  {.  assert( id!=
93a50 30 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28  0 );.  OSTRACE3(
93a60 22 53 59 4e 43 20 25 64 20 6c 6f 63 6b 3d 25 64  "SYNC %d lock=%d
93a70 5c 6e 22 2c 20 28 28 77 69 6e 46 69 6c 65 2a 29  \n", ((winFile*)
93a80 69 64 29 2d 3e 68 2c 20 28 28 77 69 6e 46 69 6c  id)->h, ((winFil
93a90 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65  e*)id)->locktype
93aa0 29 3b 0a 20 20 69 66 28 20 46 6c 75 73 68 46 69  );.  if( FlushFi
93ab0 6c 65 42 75 66 66 65 72 73 28 28 28 77 69 6e 46  leBuffers(((winF
93ac0 69 6c 65 2a 29 69 64 29 2d 3e 68 29 20 29 7b 0a  ile*)id)->h) ){.
93ad0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
93ae0 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  E_OK;.  }else{. 
93af0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
93b00 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 7d 0a 0a 2f  _IOERR;.  }.}../
93b10 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 65 20 64 69  *.** Sync the di
93b20 72 65 63 74 6f 72 79 20 7a 44 69 72 6e 61 6d 65  rectory zDirname
93b30 2e 20 54 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f  . This is a no-o
93b40 70 20 6f 6e 20 6f 70 65 72 61 74 69 6e 67 20 73  p on operating s
93b50 79 73 74 65 6d 73 20 6f 74 68 65 72 0a 2a 2a 20  ystems other.** 
93b60 74 68 61 6e 20 55 4e 49 58 2e 0a 2a 2f 0a 53 51  than UNIX..*/.SQ
93b70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
93b80 20 73 71 6c 69 74 65 33 57 69 6e 53 79 6e 63 44   sqlite3WinSyncD
93b90 69 72 65 63 74 6f 72 79 28 63 6f 6e 73 74 20 63  irectory(const c
93ba0 68 61 72 20 2a 7a 44 69 72 6e 61 6d 65 29 7b 0a  har *zDirname){.
93bb0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
93bc0 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  r(return SQLITE_
93bd0 49 4f 45 52 52 5f 52 45 41 44 29 3b 0a 20 20 72  IOERR_READ);.  r
93be0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
93bf0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61  .}../*.** Trunca
93c00 74 65 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20  te an open file 
93c10 74 6f 20 61 20 73 70 65 63 69 66 69 65 64 20 73  to a specified s
93c20 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ize.*/.static in
93c30 74 20 77 69 6e 54 72 75 6e 63 61 74 65 28 4f 73  t winTruncate(Os
93c40 46 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 6e 42  File *id, i64 nB
93c50 79 74 65 29 7b 0a 20 20 4c 4f 4e 47 20 75 70 70  yte){.  LONG upp
93c60 65 72 42 69 74 73 20 3d 20 6e 42 79 74 65 3e 3e  erBits = nByte>>
93c70 33 32 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64  32;.  assert( id
93c80 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41 43 45  !=0 );.  OSTRACE
93c90 33 28 22 54 52 55 4e 43 41 54 45 20 25 64 20 25  3("TRUNCATE %d %
93ca0 6c 6c 64 5c 6e 22 2c 20 28 28 77 69 6e 46 69 6c  lld\n", ((winFil
93cb0 65 2a 29 69 64 29 2d 3e 68 2c 20 6e 42 79 74 65  e*)id)->h, nByte
93cc0 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  );.  SimulateIOE
93cd0 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49  rror(return SQLI
93ce0 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54  TE_IOERR_TRUNCAT
93cf0 45 29 3b 0a 20 20 53 65 74 46 69 6c 65 50 6f 69  E);.  SetFilePoi
93d00 6e 74 65 72 28 28 28 77 69 6e 46 69 6c 65 2a 29  nter(((winFile*)
93d10 69 64 29 2d 3e 68 2c 20 6e 42 79 74 65 2c 20 26  id)->h, nByte, &
93d20 75 70 70 65 72 42 69 74 73 2c 20 46 49 4c 45 5f  upperBits, FILE_
93d30 42 45 47 49 4e 29 3b 0a 20 20 53 65 74 45 6e 64  BEGIN);.  SetEnd
93d40 4f 66 46 69 6c 65 28 28 28 77 69 6e 46 69 6c 65  OfFile(((winFile
93d50 2a 29 69 64 29 2d 3e 68 29 3b 0a 20 20 72 65 74  *)id)->h);.  ret
93d60 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
93d70 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e  ../*.** Determin
93d80 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 69  e the current si
93d90 7a 65 20 6f 66 20 61 20 66 69 6c 65 20 69 6e 20  ze of a file in 
93da0 62 79 74 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20  bytes.*/.static 
93db0 69 6e 74 20 77 69 6e 46 69 6c 65 53 69 7a 65 28  int winFileSize(
93dc0 4f 73 46 69 6c 65 20 2a 69 64 2c 20 69 36 34 20  OsFile *id, i64 
93dd0 2a 70 53 69 7a 65 29 7b 0a 20 20 44 57 4f 52 44  *pSize){.  DWORD
93de0 20 75 70 70 65 72 42 69 74 73 2c 20 6c 6f 77 65   upperBits, lowe
93df0 72 42 69 74 73 3b 0a 20 20 61 73 73 65 72 74 28  rBits;.  assert(
93e00 20 69 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75   id!=0 );.  Simu
93e10 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75  lateIOError(retu
93e20 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
93e30 46 53 54 41 54 29 3b 0a 20 20 6c 6f 77 65 72 42  FSTAT);.  lowerB
93e40 69 74 73 20 3d 20 47 65 74 46 69 6c 65 53 69 7a  its = GetFileSiz
93e50 65 28 28 28 77 69 6e 46 69 6c 65 2a 29 69 64 29  e(((winFile*)id)
93e60 2d 3e 68 2c 20 26 75 70 70 65 72 42 69 74 73 29  ->h, &upperBits)
93e70 3b 0a 20 20 2a 70 53 69 7a 65 20 3d 20 28 28 28  ;.  *pSize = (((
93e80 69 36 34 29 75 70 70 65 72 42 69 74 73 29 3c 3c  i64)upperBits)<<
93e90 33 32 29 20 2b 20 6c 6f 77 65 72 42 69 74 73 3b  32) + lowerBits;
93ea0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
93eb0 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 4f  _OK;.}../*.** LO
93ec0 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45  CKFILE_FAIL_IMME
93ed0 44 49 41 54 45 4c 59 20 69 73 20 75 6e 64 65 66  DIATELY is undef
93ee0 69 6e 65 64 20 6f 6e 20 73 6f 6d 65 20 57 69 6e  ined on some Win
93ef0 64 6f 77 73 20 73 79 73 74 65 6d 73 2e 0a 2a 2f  dows systems..*/
93f00 0a 23 69 66 6e 64 65 66 20 4c 4f 43 4b 46 49 4c  .#ifndef LOCKFIL
93f10 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 41 54 45  E_FAIL_IMMEDIATE
93f20 4c 59 0a 23 20 64 65 66 69 6e 65 20 4c 4f 43 4b  LY.# define LOCK
93f30 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49  FILE_FAIL_IMMEDI
93f40 41 54 45 4c 59 20 31 0a 23 65 6e 64 69 66 0a 0a  ATELY 1.#endif..
93f50 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20 61 20  /*.** Acquire a 
93f60 72 65 61 64 65 72 20 6c 6f 63 6b 2e 0a 2a 2a 20  reader lock..** 
93f70 44 69 66 66 65 72 65 6e 74 20 41 50 49 20 72 6f  Different API ro
93f80 75 74 69 6e 65 73 20 61 72 65 20 63 61 6c 6c 65  utines are calle
93f90 64 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77  d depending on w
93fa0 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
93fb0 69 73 0a 2a 2a 20 69 73 20 57 69 6e 39 35 20 6f  is.** is Win95 o
93fc0 72 20 57 69 6e 4e 54 2e 0a 2a 2f 0a 73 74 61 74  r WinNT..*/.stat
93fd0 69 63 20 69 6e 74 20 67 65 74 52 65 61 64 4c 6f  ic int getReadLo
93fe0 63 6b 28 77 69 6e 46 69 6c 65 20 2a 69 64 29 7b  ck(winFile *id){
93ff0 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 69 66  .  int res;.  if
94000 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20  ( isNT() ){.    
94010 4f 56 45 52 4c 41 50 50 45 44 20 6f 76 6c 70 3b  OVERLAPPED ovlp;
94020 0a 20 20 20 20 6f 76 6c 70 2e 4f 66 66 73 65 74  .    ovlp.Offset
94030 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b   = SHARED_FIRST;
94040 0a 20 20 20 20 6f 76 6c 70 2e 4f 66 66 73 65 74  .    ovlp.Offset
94050 48 69 67 68 20 3d 20 30 3b 0a 20 20 20 20 6f 76  High = 0;.    ov
94060 6c 70 2e 68 45 76 65 6e 74 20 3d 20 30 3b 0a 20  lp.hEvent = 0;. 
94070 20 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c     res = LockFil
94080 65 45 78 28 69 64 2d 3e 68 2c 20 4c 4f 43 4b 46  eEx(id->h, LOCKF
94090 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 41  ILE_FAIL_IMMEDIA
940a0 54 45 4c 59 2c 20 30 2c 20 53 48 41 52 45 44 5f  TELY, 0, SHARED_
940b0 53 49 5a 45 2c 30 2c 26 6f 76 6c 70 29 3b 0a 20  SIZE,0,&ovlp);. 
940c0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20   }else{.    int 
940d0 6c 6b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52  lk;.    sqlite3R
940e0 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66  andomness(sizeof
940f0 28 6c 6b 29 2c 20 26 6c 6b 29 3b 0a 20 20 20 20  (lk), &lk);.    
94100 69 64 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79  id->sharedLockBy
94110 74 65 20 3d 20 28 6c 6b 20 26 20 30 78 37 66 66  te = (lk & 0x7ff
94120 66 66 66 66 66 29 25 28 53 48 41 52 45 44 5f 53  fffff)%(SHARED_S
94130 49 5a 45 20 2d 20 31 29 3b 0a 20 20 20 20 72 65  IZE - 1);.    re
94140 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 69 64 2d  s = LockFile(id-
94150 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54  >h, SHARED_FIRST
94160 2b 69 64 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42  +id->sharedLockB
94170 79 74 65 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20  yte, 0, 1, 0);. 
94180 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b   }.  return res;
94190 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 61  .}../*.** Undo a
941a0 20 72 65 61 64 6c 6f 63 6b 0a 2a 2f 0a 73 74 61   readlock.*/.sta
941b0 74 69 63 20 69 6e 74 20 75 6e 6c 6f 63 6b 52 65  tic int unlockRe
941c0 61 64 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 20 2a  adLock(winFile *
941d0 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 65  pFile){.  int re
941e0 73 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20  s;.  if( isNT() 
941f0 29 7b 0a 20 20 20 20 72 65 73 20 3d 20 55 6e 6c  ){.    res = Unl
94200 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68  ockFile(pFile->h
94210 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20  , SHARED_FIRST, 
94220 30 2c 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20  0, SHARED_SIZE, 
94230 30 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  0);.  }else{.   
94240 20 72 65 73 20 3d 20 55 6e 6c 6f 63 6b 46 69 6c   res = UnlockFil
94250 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52  e(pFile->h, SHAR
94260 45 44 5f 46 49 52 53 54 20 2b 20 70 46 69 6c 65  ED_FIRST + pFile
94270 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65  ->sharedLockByte
94280 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a  , 0, 1, 0);.  }.
94290 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a    return res;.}.
942a0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
942b0 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d  OMIT_PAGER_PRAGM
942c0 41 53 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74  AS./*.** Check t
942d0 68 61 74 20 61 20 67 69 76 65 6e 20 70 61 74 68  hat a given path
942e0 6e 61 6d 65 20 69 73 20 61 20 64 69 72 65 63 74  name is a direct
942f0 6f 72 79 20 61 6e 64 20 69 73 20 77 72 69 74 61  ory and is writa
94300 62 6c 65 20 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54  ble .**.*/.SQLIT
94310 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
94320 6c 69 74 65 33 57 69 6e 49 73 44 69 72 57 72 69  lite3WinIsDirWri
94330 74 61 62 6c 65 28 63 68 61 72 20 2a 7a 44 69 72  table(char *zDir
94340 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 66 69 6c  name){.  int fil
94350 65 41 74 74 72 3b 0a 20 20 76 6f 69 64 20 2a 7a  eAttr;.  void *z
94360 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 69 66 28  Converted;.  if(
94370 20 7a 44 69 72 6e 61 6d 65 3d 3d 30 20 29 20 72   zDirname==0 ) r
94380 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 21  eturn 0;.  if( !
94390 69 73 4e 54 28 29 20 26 26 20 73 74 72 6c 65 6e  isNT() && strlen
943a0 28 7a 44 69 72 6e 61 6d 65 29 3e 4d 41 58 5f 50  (zDirname)>MAX_P
943b0 41 54 48 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ATH ) return 0;.
943c0 0a 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20  .  zConverted = 
943d0 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e  convertUtf8Filen
943e0 61 6d 65 28 7a 44 69 72 6e 61 6d 65 29 3b 0a 20  ame(zDirname);. 
943f0 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64 3d   if( zConverted=
94400 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
94410 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
94420 20 7d 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20   }.  if( isNT() 
94430 29 7b 0a 20 20 20 20 66 69 6c 65 41 74 74 72 20  ){.    fileAttr 
94440 3d 20 47 65 74 46 69 6c 65 41 74 74 72 69 62 75  = GetFileAttribu
94450 74 65 73 57 28 28 57 43 48 41 52 2a 29 7a 43 6f  tesW((WCHAR*)zCo
94460 6e 76 65 72 74 65 64 29 3b 0a 20 20 7d 65 6c 73  nverted);.  }els
94470 65 7b 0a 23 69 66 20 4f 53 5f 57 49 4e 43 45 0a  e{.#if OS_WINCE.
94480 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 23 65      return 0;.#e
94490 6c 73 65 0a 20 20 20 20 66 69 6c 65 41 74 74 72  lse.    fileAttr
944a0 20 3d 20 47 65 74 46 69 6c 65 41 74 74 72 69 62   = GetFileAttrib
944b0 75 74 65 73 41 28 28 63 68 61 72 2a 29 7a 43 6f  utesA((char*)zCo
944c0 6e 76 65 72 74 65 64 29 3b 0a 23 65 6e 64 69 66  nverted);.#endif
944d0 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 46 72 65  .  }.  sqliteFre
944e0 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20  e(zConverted);. 
944f0 20 69 66 28 20 66 69 6c 65 41 74 74 72 20 3d 3d   if( fileAttr ==
94500 20 30 78 66 66 66 66 66 66 66 66 20 29 20 72 65   0xffffffff ) re
94510 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 28 66  turn 0;.  if( (f
94520 69 6c 65 41 74 74 72 20 26 20 46 49 4c 45 5f 41  ileAttr & FILE_A
94530 54 54 52 49 42 55 54 45 5f 44 49 52 45 43 54 4f  TTRIBUTE_DIRECTO
94540 52 59 29 20 21 3d 20 46 49 4c 45 5f 41 54 54 52  RY) != FILE_ATTR
94550 49 42 55 54 45 5f 44 49 52 45 43 54 4f 52 59 20  IBUTE_DIRECTORY 
94560 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
94570 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b  .  }.  return 1;
94580 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
94590 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50  ITE_OMIT_PAGER_P
945a0 52 41 47 4d 41 53 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  RAGMAS */../*.**
945b0 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77   Lock the file w
945c0 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65  ith the lock spe
945d0 63 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65  cified by parame
945e0 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f  ter locktype - o
945f0 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c  ne.** of the fol
94600 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  lowing:.**.**   
94610 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43    (1) SHARED_LOC
94620 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53  K.**     (2) RES
94630 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  ERVED_LOCK.**   
94640 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f    (3) PENDING_LO
94650 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58  CK.**     (4) EX
94660 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a  CLUSIVE_LOCK.**.
94670 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65  ** Sometimes whe
94680 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65  n requesting one
94690 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64   lock state, add
946a0 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61  itional lock sta
946b0 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72  tes.** are inser
946c0 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20  ted in between. 
946d0 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67   The locking mig
946e0 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f  ht fail on one o
946f0 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74  f the later.** t
94700 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69  ransitions leavi
94710 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74  ng the lock stat
94720 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d  e different from
94730 20 77 68 61 74 20 69 74 20 73 74 61 72 74 65 64   what it started
94740 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68   but.** still sh
94750 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e  ort of its goal.
94760 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
94770 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 65 20  chart shows the 
94780 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73  allowed.** trans
94790 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69  itions and the i
947a0 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64  nserted intermed
947b0 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a  iate states:.**.
947c0 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d  **    UNLOCKED -
947d0 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53  > SHARED.**    S
947e0 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45  HARED -> RESERVE
947f0 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d  D.**    SHARED -
94800 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45  > (PENDING) -> E
94810 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52  XCLUSIVE.**    R
94820 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44  ESERVED -> (PEND
94830 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56  ING) -> EXCLUSIV
94840 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20  E.**    PENDING 
94850 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a  -> EXCLUSIVE.**.
94860 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
94870 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61  will only increa
94880 73 65 20 61 20 6c 6f 63 6b 2e 20 20 54 68 65 20  se a lock.  The 
94890 77 69 6e 55 6e 6c 6f 63 6b 28 29 20 72 6f 75 74  winUnlock() rout
948a0 69 6e 65 0a 2a 2a 20 65 72 61 73 65 73 20 61 6c  ine.** erases al
948b0 6c 20 6c 6f 63 6b 73 20 61 74 20 6f 6e 63 65 20  l locks at once 
948c0 61 6e 64 20 72 65 74 75 72 6e 73 20 75 73 20 69  and returns us i
948d0 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 6c 6f  mmediately to lo
948e0 63 6b 69 6e 67 20 6c 65 76 65 6c 20 30 2e 0a 2a  cking level 0..*
948f0 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73  * It is not poss
94900 69 62 6c 65 20 74 6f 20 6c 6f 77 65 72 20 74 68  ible to lower th
94910 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20  e locking level 
94920 6f 6e 65 20 73 74 65 70 20 61 74 20 61 20 74 69  one step at a ti
94930 6d 65 2e 20 20 59 6f 75 0a 2a 2a 20 6d 75 73 74  me.  You.** must
94940 20 67 6f 20 73 74 72 61 69 67 68 74 20 74 6f 20   go straight to 
94950 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 30 2e  locking level 0.
94960 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
94970 69 6e 4c 6f 63 6b 28 4f 73 46 69 6c 65 20 2a 69  inLock(OsFile *i
94980 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29  d, int locktype)
94990 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
949a0 49 54 45 5f 4f 4b 3b 20 20 20 20 2f 2a 20 52 65  ITE_OK;    /* Re
949b0 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73  turn code from s
949c0 75 62 72 6f 75 74 69 6e 65 73 20 2a 2f 0a 20 20  ubroutines */.  
949d0 69 6e 74 20 72 65 73 20 3d 20 31 3b 20 20 20 20  int res = 1;    
949e0 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74         /* Result
949f0 20 6f 66 20 61 20 77 69 6e 64 6f 77 73 20 6c 6f   of a windows lo
94a00 63 6b 20 63 61 6c 6c 20 2a 2f 0a 20 20 69 6e 74  ck call */.  int
94a10 20 6e 65 77 4c 6f 63 6b 74 79 70 65 3b 20 20 20   newLocktype;   
94a20 20 20 20 20 2f 2a 20 53 65 74 20 69 64 2d 3e 6c      /* Set id->l
94a30 6f 63 6b 74 79 70 65 20 74 6f 20 74 68 69 73 20  ocktype to this 
94a40 76 61 6c 75 65 20 62 65 66 6f 72 65 20 65 78 69  value before exi
94a50 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 67 6f  ting */.  int go
94a60 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 30  tPendingLock = 0
94a70 3b 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20 61  ;/* True if we a
94a80 63 71 75 69 72 65 64 20 61 20 50 45 4e 44 49 4e  cquired a PENDIN
94a90 47 20 6c 6f 63 6b 20 74 68 69 73 20 74 69 6d 65  G lock this time
94aa0 20 2a 2f 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70   */.  winFile *p
94ab0 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a  File = (winFile*
94ac0 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  )id;..  assert( 
94ad0 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 4f 53  pFile!=0 );.  OS
94ae0 54 52 41 43 45 35 28 22 4c 4f 43 4b 20 25 64 20  TRACE5("LOCK %d 
94af0 25 64 20 77 61 73 20 25 64 28 25 64 29 5c 6e 22  %d was %d(%d)\n"
94b00 2c 0a 20 20 20 20 20 20 20 20 20 20 70 46 69 6c  ,.          pFil
94b10 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 20  e->h, locktype, 
94b20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c  pFile->locktype,
94b30 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f   pFile->sharedLo
94b40 63 6b 42 79 74 65 29 3b 0a 0a 20 20 2f 2a 20 49  ckByte);..  /* I
94b50 66 20 74 68 65 72 65 20 69 73 20 61 6c 72 65 61  f there is alrea
94b60 64 79 20 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69  dy a lock of thi
94b70 73 20 74 79 70 65 20 6f 72 20 6d 6f 72 65 20 72  s type or more r
94b80 65 73 74 72 69 63 74 69 76 65 20 6f 6e 20 74 68  estrictive on th
94b90 65 0a 20 20 2a 2a 20 4f 73 46 69 6c 65 2c 20 64  e.  ** OsFile, d
94ba0 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74  o nothing. Don't
94bb0 20 75 73 65 20 74 68 65 20 65 6e 64 5f 6c 6f 63   use the end_loc
94bc0 6b 3a 20 65 78 69 74 20 70 61 74 68 2c 20 61 73  k: exit path, as
94bd0 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 4f 73 45  .  ** sqlite3OsE
94be0 6e 74 65 72 4d 75 74 65 78 28 29 20 68 61 73 6e  nterMutex() hasn
94bf0 27 74 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 79  't been called y
94c00 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  et..  */.  if( p
94c10 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d  File->locktype>=
94c20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20  locktype ){.    
94c30 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
94c40 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65  ;.  }..  /* Make
94c50 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e   sure the lockin
94c60 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f  g sequence is co
94c70 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61 73 73  rrect.  */.  ass
94c80 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ert( pFile->lock
94c90 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c  type!=NO_LOCK ||
94ca0 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45   locktype==SHARE
94cb0 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65  D_LOCK );.  asse
94cc0 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50 45  rt( locktype!=PE
94cd0 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20  NDING_LOCK );.  
94ce0 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65  assert( locktype
94cf0 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20  !=RESERVED_LOCK 
94d00 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  || pFile->lockty
94d10 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe==SHARED_LOCK 
94d20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 6b 20 74 68  );..  /* Lock th
94d30 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62  e PENDING_LOCK b
94d40 79 74 65 20 69 66 20 77 65 20 6e 65 65 64 20 74  yte if we need t
94d50 6f 20 61 63 71 75 69 72 65 20 61 20 50 45 4e 44  o acquire a PEND
94d60 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a 20 20 2a 2a  ING lock or.  **
94d70 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e 20   a SHARED lock. 
94d80 20 49 66 20 77 65 20 61 72 65 20 61 63 71 75 69   If we are acqui
94d90 72 69 6e 67 20 61 20 53 48 41 52 45 44 20 6c 6f  ring a SHARED lo
94da0 63 6b 2c 20 74 68 65 20 61 63 71 75 69 73 69 74  ck, the acquisit
94db0 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20  ion of.  ** the 
94dc0 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 79 74  PENDING_LOCK byt
94dd0 65 20 69 73 20 74 65 6d 70 6f 72 61 72 79 2e 0a  e is temporary..
94de0 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f 63 6b 74 79    */.  newLockty
94df0 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  pe = pFile->lock
94e00 74 79 70 65 3b 0a 20 20 69 66 28 20 70 46 69 6c  type;.  if( pFil
94e10 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f  e->locktype==NO_
94e20 4c 4f 43 4b 0a 20 20 20 7c 7c 20 28 6c 6f 63 6b  LOCK.   || (lock
94e30 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f  type==EXCLUSIVE_
94e40 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c  LOCK && pFile->l
94e50 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 45  ocktype==RESERVE
94e60 44 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a 20 20 20  D_LOCK).  ){.   
94e70 20 69 6e 74 20 63 6e 74 20 3d 20 33 3b 0a 20 20   int cnt = 3;.  
94e80 20 20 77 68 69 6c 65 28 20 63 6e 74 2d 2d 3e 30    while( cnt-->0
94e90 20 26 26 20 28 72 65 73 20 3d 20 4c 6f 63 6b 46   && (res = LockF
94ea0 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 50 45  ile(pFile->h, PE
94eb0 4e 44 49 4e 47 5f 42 59 54 45 2c 20 30 2c 20 31  NDING_BYTE, 0, 1
94ec0 2c 20 30 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20  , 0))==0 ){.    
94ed0 20 20 2f 2a 20 54 72 79 20 33 20 74 69 6d 65 73    /* Try 3 times
94ee0 20 74 6f 20 67 65 74 20 74 68 65 20 70 65 6e 64   to get the pend
94ef0 69 6e 67 20 6c 6f 63 6b 2e 20 20 54 68 65 20 70  ing lock.  The p
94f00 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 6d 69 67 68  ending lock migh
94f10 74 20 62 65 0a 20 20 20 20 20 20 2a 2a 20 68 65  t be.      ** he
94f20 6c 64 20 62 79 20 61 6e 6f 74 68 65 72 20 72 65  ld by another re
94f30 61 64 65 72 20 70 72 6f 63 65 73 73 20 77 68 6f  ader process who
94f40 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 20 69 74   will release it
94f50 20 6d 6f 6d 65 6e 74 61 72 69 6c 79 2e 0a 20 20   momentarily..  
94f60 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 4f 53 54      */.      OST
94f70 52 41 43 45 32 28 22 63 6f 75 6c 64 20 6e 6f 74  RACE2("could not
94f80 20 67 65 74 20 61 20 50 45 4e 44 49 4e 47 20 6c   get a PENDING l
94f90 6f 63 6b 2e 20 63 6e 74 3d 25 64 5c 6e 22 2c 20  ock. cnt=%d\n", 
94fa0 63 6e 74 29 3b 0a 20 20 20 20 20 20 53 6c 65 65  cnt);.      Slee
94fb0 70 28 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  p(1);.    }.    
94fc0 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d  gotPendingLock =
94fd0 20 72 65 73 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20   res;.  }..  /* 
94fe0 41 63 71 75 69 72 65 20 61 20 73 68 61 72 65 64  Acquire a shared
94ff0 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28   lock.  */.  if(
95000 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45   locktype==SHARE
95010 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 7b  D_LOCK && res ){
95020 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69  .    assert( pFi
95030 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f  le->locktype==NO
95040 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 73  _LOCK );.    res
95050 20 3d 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 70   = getReadLock(p
95060 46 69 6c 65 29 3b 0a 20 20 20 20 69 66 28 20 72  File);.    if( r
95070 65 73 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c  es ){.      newL
95080 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44  ocktype = SHARED
95090 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d  _LOCK;.    }.  }
950a0 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61  ..  /* Acquire a
950b0 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 0a 20   RESERVED lock. 
950c0 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79   */.  if( lockty
950d0 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43  pe==RESERVED_LOC
950e0 4b 20 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20  K && res ){.    
950f0 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c  assert( pFile->l
95100 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f  ocktype==SHARED_
95110 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20  LOCK );.    res 
95120 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65  = LockFile(pFile
95130 2d 3e 68 2c 20 52 45 53 45 52 56 45 44 5f 42 59  ->h, RESERVED_BY
95140 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20  TE, 0, 1, 0);.  
95150 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20    if( res ){.   
95160 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d     newLocktype =
95170 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3b 0a   RESERVED_LOCK;.
95180 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
95190 41 63 71 75 69 72 65 20 61 20 50 45 4e 44 49 4e  Acquire a PENDIN
951a0 47 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66  G lock.  */.  if
951b0 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c  ( locktype==EXCL
951c0 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65  USIVE_LOCK && re
951d0 73 20 29 7b 0a 20 20 20 20 6e 65 77 4c 6f 63 6b  s ){.    newLock
951e0 74 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c  type = PENDING_L
951f0 4f 43 4b 3b 0a 20 20 20 20 67 6f 74 50 65 6e 64  OCK;.    gotPend
95200 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 7d  ingLock = 0;.  }
95210 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61  ..  /* Acquire a
95220 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  n EXCLUSIVE lock
95230 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b  .  */.  if( lock
95240 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f  type==EXCLUSIVE_
95250 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a 20  LOCK && res ){. 
95260 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65     assert( pFile
95270 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53 48 41 52  ->locktype>=SHAR
95280 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72  ED_LOCK );.    r
95290 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c  es = unlockReadL
952a0 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20  ock(pFile);.    
952b0 4f 53 54 52 41 43 45 32 28 22 75 6e 72 65 61 64  OSTRACE2("unread
952c0 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c 20 72 65  lock = %d\n", re
952d0 73 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f  s);.    res = Lo
952e0 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c  ckFile(pFile->h,
952f0 20 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 30   SHARED_FIRST, 0
95300 2c 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 30  , SHARED_SIZE, 0
95310 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 29  );.    if( res )
95320 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74  {.      newLockt
95330 79 70 65 20 3d 20 45 58 43 4c 55 53 49 56 45 5f  ype = EXCLUSIVE_
95340 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  LOCK;.    }else{
95350 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 32 28  .      OSTRACE2(
95360 22 65 72 72 6f 72 2d 63 6f 64 65 20 3d 20 25 64  "error-code = %d
95370 5c 6e 22 2c 20 47 65 74 4c 61 73 74 45 72 72 6f  \n", GetLastErro
95380 72 28 29 29 3b 0a 20 20 20 20 20 20 67 65 74 52  r());.      getR
95390 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a  eadLock(pFile);.
953a0 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
953b0 49 66 20 77 65 20 61 72 65 20 68 6f 6c 64 69 6e  If we are holdin
953c0 67 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b  g a PENDING lock
953d0 20 74 68 61 74 20 6f 75 67 68 74 20 74 6f 20 62   that ought to b
953e0 65 20 72 65 6c 65 61 73 65 64 2c 20 74 68 65 6e  e released, then
953f0 0a 20 20 2a 2a 20 72 65 6c 65 61 73 65 20 69 74  .  ** release it
95400 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28   now..  */.  if(
95410 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20   gotPendingLock 
95420 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  && locktype==SHA
95430 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  RED_LOCK ){.    
95440 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65  UnlockFile(pFile
95450 2d 3e 68 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54  ->h, PENDING_BYT
95460 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 7d  E, 0, 1, 0);.  }
95470 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68  ..  /* Update th
95480 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 6c  e state of the l
95490 6f 63 6b 20 68 61 73 20 68 65 6c 64 20 69 6e 20  ock has held in 
954a0 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  the file descrip
954b0 74 6f 72 20 74 68 65 6e 0a 20 20 2a 2a 20 72 65  tor then.  ** re
954c0 74 75 72 6e 20 74 68 65 20 61 70 70 72 6f 70 72  turn the appropr
954d0 69 61 74 65 20 72 65 73 75 6c 74 20 63 6f 64 65  iate result code
954e0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 65 73  ..  */.  if( res
954f0 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
95500 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b  ITE_OK;.  }else{
95510 0a 20 20 20 20 4f 53 54 52 41 43 45 34 28 22 4c  .    OSTRACE4("L
95520 4f 43 4b 20 46 41 49 4c 45 44 20 25 64 20 74 72  OCK FAILED %d tr
95530 79 69 6e 67 20 66 6f 72 20 25 64 20 62 75 74 20  ying for %d but 
95540 67 6f 74 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65  got %d\n", pFile
95550 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ->h,.           
95560 6c 6f 63 6b 74 79 70 65 2c 20 6e 65 77 4c 6f 63  locktype, newLoc
95570 6b 74 79 70 65 29 3b 0a 20 20 20 20 72 63 20 3d  ktype);.    rc =
95580 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
95590 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  }.  pFile->lockt
955a0 79 70 65 20 3d 20 6e 65 77 4c 6f 63 6b 74 79 70  ype = newLocktyp
955b0 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  e;.  return rc;.
955c0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
955d0 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20  utine checks if 
955e0 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52  there is a RESER
955f0 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e  VED lock held on
95600 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a   the specified.*
95610 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f  * file by this o
95620 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63  r any other proc
95630 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c  ess. If such a l
95640 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 72 65 74  ock is held, ret
95650 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2c  urn.** non-zero,
95660 20 6f 74 68 65 72 77 69 73 65 20 7a 65 72 6f 2e   otherwise zero.
95670 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
95680 69 6e 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  inCheckReservedL
95690 6f 63 6b 28 4f 73 46 69 6c 65 20 2a 69 64 29 7b  ock(OsFile *id){
956a0 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 77 69 6e  .  int rc;.  win
956b0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77  File *pFile = (w
956c0 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 73  inFile*)id;.  as
956d0 73 65 72 74 28 20 70 46 69 6c 65 21 3d 30 20 29  sert( pFile!=0 )
956e0 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c  ;.  if( pFile->l
956f0 6f 63 6b 74 79 70 65 3e 3d 52 45 53 45 52 56 45  ocktype>=RESERVE
95700 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 63  D_LOCK ){.    rc
95710 20 3d 20 31 3b 0a 20 20 20 20 4f 53 54 52 41 43   = 1;.    OSTRAC
95720 45 33 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b  E3("TEST WR-LOCK
95730 20 25 64 20 25 64 20 28 6c 6f 63 61 6c 29 5c 6e   %d %d (local)\n
95740 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 29  ", pFile->h, rc)
95750 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
95760 63 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69  c = LockFile(pFi
95770 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56 45 44 5f  le->h, RESERVED_
95780 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a  BYTE, 0, 1, 0);.
95790 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20      if( rc ){.  
957a0 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70      UnlockFile(p
957b0 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56 45  File->h, RESERVE
957c0 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29  D_BYTE, 0, 1, 0)
957d0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d  ;.    }.    rc =
957e0 20 21 72 63 3b 0a 20 20 20 20 4f 53 54 52 41 43   !rc;.    OSTRAC
957f0 45 33 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b  E3("TEST WR-LOCK
95800 20 25 64 20 25 64 20 28 72 65 6d 6f 74 65 29 5c   %d %d (remote)\
95810 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63  n", pFile->h, rc
95820 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
95830 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77  rc;.}../*.** Low
95840 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c  er the locking l
95850 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73  evel on file des
95860 63 72 69 70 74 6f 72 20 69 64 20 74 6f 20 6c 6f  criptor id to lo
95870 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70  cktype.  locktyp
95880 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74  e.** must be eit
95890 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53  her NO_LOCK or S
958a0 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a  HARED_LOCK..**.*
958b0 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67  * If the locking
958c0 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69   level of the fi
958d0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73  le descriptor is
958e0 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62   already at or b
958f0 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75  elow.** the requ
95900 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65  ested locking le
95910 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  vel, this routin
95920 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a  e is a no-op..**
95930 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f  .** It is not po
95940 73 73 69 62 6c 65 20 66 6f 72 20 74 68 69 73 20  ssible for this 
95950 72 6f 75 74 69 6e 65 20 74 6f 20 66 61 69 6c 20  routine to fail 
95960 69 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  if the second ar
95970 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 4e 4f 5f  gument.** is NO_
95980 4c 4f 43 4b 2e 20 20 49 66 20 74 68 65 20 73 65  LOCK.  If the se
95990 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73  cond argument is
959a0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 74 68 65   SHARED_LOCK the
959b0 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a  n this routine.*
959c0 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 53  * might return S
959d0 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 2a 2f 0a  QLITE_IOERR;.*/.
959e0 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 55 6e  static int winUn
959f0 6c 6f 63 6b 28 4f 73 46 69 6c 65 20 2a 69 64 2c  lock(OsFile *id,
95a00 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a   int locktype){.
95a10 20 20 69 6e 74 20 74 79 70 65 3b 0a 20 20 69 6e    int type;.  in
95a20 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
95a30 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69  ;.  winFile *pFi
95a40 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69  le = (winFile*)i
95a50 64 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 69  d;.  assert( pFi
95a60 6c 65 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  le!=0 );.  asser
95a70 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41  t( locktype<=SHA
95a80 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 4f 53  RED_LOCK );.  OS
95a90 54 52 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20 25  TRACE5("UNLOCK %
95aa0 64 20 74 6f 20 25 64 20 77 61 73 20 25 64 28 25  d to %d was %d(%
95ab0 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  d)\n", pFile->h,
95ac0 20 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 20 20 20   locktype,.     
95ad0 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b       pFile->lock
95ae0 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 73 68 61  type, pFile->sha
95af0 72 65 64 4c 6f 63 6b 42 79 74 65 29 3b 0a 20 20  redLockByte);.  
95b00 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f  type = pFile->lo
95b10 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 74 79  cktype;.  if( ty
95b20 70 65 3e 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f  pe>=EXCLUSIVE_LO
95b30 43 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b  CK ){.    Unlock
95b40 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53  File(pFile->h, S
95b50 48 41 52 45 44 5f 46 49 52 53 54 2c 20 30 2c 20  HARED_FIRST, 0, 
95b60 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b  SHARED_SIZE, 0);
95b70 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 70  .    if( locktyp
95b80 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26  e==SHARED_LOCK &
95b90 26 20 21 67 65 74 52 65 61 64 4c 6f 63 6b 28 70  & !getReadLock(p
95ba0 46 69 6c 65 29 20 29 7b 0a 20 20 20 20 20 20 2f  File) ){.      /
95bb0 2a 20 54 68 69 73 20 73 68 6f 75 6c 64 20 6e 65  * This should ne
95bc0 76 65 72 20 68 61 70 70 65 6e 2e 20 20 57 65 20  ver happen.  We 
95bd0 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65  should always be
95be0 20 61 62 6c 65 20 74 6f 0a 20 20 20 20 20 20 2a   able to.      *
95bf0 2a 20 72 65 61 63 71 75 69 72 65 20 74 68 65 20  * reacquire the 
95c00 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20  read lock */.   
95c10 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49     rc = SQLITE_I
95c20 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b 0a 20 20 20  OERR_UNLOCK;.   
95c30 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 74 79 70   }.  }.  if( typ
95c40 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  e>=RESERVED_LOCK
95c50 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 69   ){.    UnlockFi
95c60 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53  le(pFile->h, RES
95c70 45 52 56 45 44 5f 42 59 54 45 2c 20 30 2c 20 31  ERVED_BYTE, 0, 1
95c80 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  , 0);.  }.  if( 
95c90 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43  locktype==NO_LOC
95ca0 4b 20 26 26 20 74 79 70 65 3e 3d 53 48 41 52 45  K && type>=SHARE
95cb0 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 75 6e  D_LOCK ){.    un
95cc0 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 70 46 69  lockReadLock(pFi
95cd0 6c 65 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 74  le);.  }.  if( t
95ce0 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43  ype>=PENDING_LOC
95cf0 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b 46  K ){.    UnlockF
95d00 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 50 45  ile(pFile->h, PE
95d10 4e 44 49 4e 47 5f 42 59 54 45 2c 20 30 2c 20 31  NDING_BYTE, 0, 1
95d20 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 70 46 69 6c  , 0);.  }.  pFil
95d30 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f  e->locktype = lo
95d40 63 6b 74 79 70 65 3b 0a 20 20 72 65 74 75 72 6e  cktype;.  return
95d50 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 75   rc;.}../*.** Tu
95d60 72 6e 20 61 20 72 65 6c 61 74 69 76 65 20 70 61  rn a relative pa
95d70 74 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20 66 75  thname into a fu
95d80 6c 6c 20 70 61 74 68 6e 61 6d 65 2e 20 20 52 65  ll pathname.  Re
95d90 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a 2a  turn a pointer.*
95da0 2a 20 74 6f 20 74 68 65 20 66 75 6c 6c 20 70 61  * to the full pa
95db0 74 68 6e 61 6d 65 20 73 74 6f 72 65 64 20 69 6e  thname stored in
95dc0 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64 20   space obtained 
95dd0 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f  from sqliteMallo
95de0 63 28 29 2e 0a 2a 2a 20 54 68 65 20 63 61 6c 6c  c()..** The call
95df0 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  ing function is 
95e00 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20  responsible for 
95e10 66 72 65 65 69 6e 67 20 74 68 69 73 20 73 70 61  freeing this spa
95e20 63 65 20 6f 6e 63 65 20 69 74 0a 2a 2a 20 69 73  ce once it.** is
95e30 20 6e 6f 20 6c 6f 6e 67 65 72 20 6e 65 65 64 65   no longer neede
95e40 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
95e50 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74  VATE char *sqlit
95e60 65 33 57 69 6e 46 75 6c 6c 50 61 74 68 6e 61 6d  e3WinFullPathnam
95e70 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52  e(const char *zR
95e80 65 6c 61 74 69 76 65 29 7b 0a 20 20 63 68 61 72  elative){.  char
95e90 20 2a 7a 46 75 6c 6c 3b 0a 23 69 66 20 64 65 66   *zFull;.#if def
95ea0 69 6e 65 64 28 5f 5f 43 59 47 57 49 4e 5f 5f 29  ined(__CYGWIN__)
95eb0 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20  .  int nByte;.  
95ec0 6e 42 79 74 65 20 3d 20 73 74 72 6c 65 6e 28 7a  nByte = strlen(z
95ed0 52 65 6c 61 74 69 76 65 29 20 2b 20 4d 41 58 5f  Relative) + MAX_
95ee0 50 41 54 48 20 2b 20 31 30 30 31 3b 0a 20 20 7a  PATH + 1001;.  z
95ef0 46 75 6c 6c 20 3d 20 73 71 6c 69 74 65 4d 61 6c  Full = sqliteMal
95f00 6c 6f 63 28 20 6e 42 79 74 65 20 29 3b 0a 20 20  loc( nByte );.  
95f10 69 66 28 20 7a 46 75 6c 6c 3d 3d 30 20 29 20 72  if( zFull==0 ) r
95f20 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 63  eturn 0;.  if( c
95f30 79 67 77 69 6e 5f 63 6f 6e 76 5f 74 6f 5f 66 75  ygwin_conv_to_fu
95f40 6c 6c 5f 77 69 6e 33 32 5f 70 61 74 68 28 7a 52  ll_win32_path(zR
95f50 65 6c 61 74 69 76 65 2c 20 7a 46 75 6c 6c 29 20  elative, zFull) 
95f60 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6c 69  ) return 0;.#eli
95f70 66 20 4f 53 5f 57 49 4e 43 45 0a 20 20 2f 2a 20  f OS_WINCE.  /* 
95f80 57 69 6e 43 45 20 68 61 73 20 6e 6f 20 63 6f 6e  WinCE has no con
95f90 63 65 70 74 20 6f 66 20 61 20 72 65 6c 61 74 69  cept of a relati
95fa0 76 65 20 70 61 74 68 6e 61 6d 65 2c 20 6f 72 20  ve pathname, or 
95fb0 73 6f 20 49 20 61 6d 20 74 6f 6c 64 2e 20 2a 2f  so I am told. */
95fc0 0a 20 20 7a 46 75 6c 6c 20 3d 20 73 71 6c 69 74  .  zFull = sqlit
95fd0 65 53 74 72 44 75 70 28 7a 52 65 6c 61 74 69 76  eStrDup(zRelativ
95fe0 65 29 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20  e);.#else.  int 
95ff0 6e 42 79 74 65 3b 0a 20 20 76 6f 69 64 20 2a 7a  nByte;.  void *z
96000 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 7a 43 6f  Converted;.  zCo
96010 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 72  nverted = conver
96020 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a 52  tUtf8Filename(zR
96030 65 6c 61 74 69 76 65 29 3b 0a 20 20 69 66 28 20  elative);.  if( 
96040 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 57 43  isNT() ){.    WC
96050 48 41 52 20 2a 7a 54 65 6d 70 3b 0a 20 20 20 20  HAR *zTemp;.    
96060 6e 42 79 74 65 20 3d 20 47 65 74 46 75 6c 6c 50  nByte = GetFullP
96070 61 74 68 4e 61 6d 65 57 28 28 57 43 48 41 52 2a  athNameW((WCHAR*
96080 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20 30 2c 20  )zConverted, 0, 
96090 30 2c 20 30 29 20 2b 20 33 3b 0a 20 20 20 20 7a  0, 0) + 3;.    z
960a0 54 65 6d 70 20 3d 20 73 71 6c 69 74 65 4d 61 6c  Temp = sqliteMal
960b0 6c 6f 63 28 20 6e 42 79 74 65 2a 73 69 7a 65 6f  loc( nByte*sizeo
960c0 66 28 7a 54 65 6d 70 5b 30 5d 29 20 29 3b 0a 20  f(zTemp[0]) );. 
960d0 20 20 20 69 66 28 20 7a 54 65 6d 70 3d 3d 30 20     if( zTemp==0 
960e0 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 46  ){.      sqliteF
960f0 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b  ree(zConverted);
96100 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  .      return 0;
96110 0a 20 20 20 20 7d 0a 20 20 20 20 47 65 74 46 75  .    }.    GetFu
96120 6c 6c 50 61 74 68 4e 61 6d 65 57 28 28 57 43 48  llPathNameW((WCH
96130 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20  AR*)zConverted, 
96140 6e 42 79 74 65 2c 20 7a 54 65 6d 70 2c 20 30 29  nByte, zTemp, 0)
96150 3b 0a 20 20 20 20 73 71 6c 69 74 65 46 72 65 65  ;.    sqliteFree
96160 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20  (zConverted);.  
96170 20 20 7a 46 75 6c 6c 20 3d 20 75 6e 69 63 6f 64    zFull = unicod
96180 65 54 6f 55 74 66 38 28 7a 54 65 6d 70 29 3b 0a  eToUtf8(zTemp);.
96190 20 20 20 20 73 71 6c 69 74 65 46 72 65 65 28 7a      sqliteFree(z
961a0 54 65 6d 70 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Temp);.  }else{.
961b0 20 20 20 20 63 68 61 72 20 2a 7a 54 65 6d 70 3b      char *zTemp;
961c0 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 47 65 74  .    nByte = Get
961d0 46 75 6c 6c 50 61 74 68 4e 61 6d 65 41 28 28 63  FullPathNameA((c
961e0 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c  har*)zConverted,
961f0 20 30 2c 20 30 2c 20 30 29 20 2b 20 33 3b 0a 20   0, 0, 0) + 3;. 
96200 20 20 20 7a 54 65 6d 70 20 3d 20 73 71 6c 69 74     zTemp = sqlit
96210 65 4d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 2a 73  eMalloc( nByte*s
96220 69 7a 65 6f 66 28 7a 54 65 6d 70 5b 30 5d 29 20  izeof(zTemp[0]) 
96230 29 3b 0a 20 20 20 20 69 66 28 20 7a 54 65 6d 70  );.    if( zTemp
96240 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ==0 ){.      sql
96250 69 74 65 46 72 65 65 28 7a 43 6f 6e 76 65 72 74  iteFree(zConvert
96260 65 64 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  ed);.      retur
96270 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 47  n 0;.    }.    G
96280 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 41 28  etFullPathNameA(
96290 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65  (char*)zConverte
962a0 64 2c 20 6e 42 79 74 65 2c 20 7a 54 65 6d 70 2c  d, nByte, zTemp,
962b0 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 46   0);.    sqliteF
962c0 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b  ree(zConverted);
962d0 0a 20 20 20 20 7a 46 75 6c 6c 20 3d 20 6d 62 63  .    zFull = mbc
962e0 73 54 6f 55 74 66 38 28 7a 54 65 6d 70 29 3b 0a  sToUtf8(zTemp);.
962f0 20 20 20 20 73 71 6c 69 74 65 46 72 65 65 28 7a      sqliteFree(z
96300 54 65 6d 70 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  Temp);.  }.#endi
96310 66 0a 20 20 72 65 74 75 72 6e 20 7a 46 75 6c 6c  f.  return zFull
96320 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  ;.}../*.** The f
96330 75 6c 6c 53 79 6e 63 20 6f 70 74 69 6f 6e 20 69  ullSync option i
96340 73 20 6d 65 61 6e 69 6e 67 6c 65 73 73 20 6f 6e  s meaningless on
96350 20 77 69 6e 64 6f 77 73 2e 20 20 20 54 68 69 73   windows.   This
96360 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a   is a no-op..*/.
96370 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 53  static void winS
96380 65 74 46 75 6c 6c 53 79 6e 63 28 4f 73 46 69 6c  etFullSync(OsFil
96390 65 20 2a 69 64 2c 20 69 6e 74 20 76 29 7b 0a 20  e *id, int v){. 
963a0 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a   return;.}../*.*
963b0 2a 20 52 65 74 75 72 6e 20 74 68 65 20 75 6e 64  * Return the und
963c0 65 72 6c 79 69 6e 67 20 66 69 6c 65 20 68 61 6e  erlying file han
963d0 64 6c 65 20 66 6f 72 20 61 6e 20 4f 73 46 69 6c  dle for an OsFil
963e0 65 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  e.*/.static int 
963f0 77 69 6e 46 69 6c 65 48 61 6e 64 6c 65 28 4f 73  winFileHandle(Os
96400 46 69 6c 65 20 2a 69 64 29 7b 0a 20 20 72 65 74  File *id){.  ret
96410 75 72 6e 20 28 69 6e 74 29 28 28 77 69 6e 46 69  urn (int)((winFi
96420 6c 65 2a 29 69 64 29 2d 3e 68 3b 0a 7d 0a 0a 2f  le*)id)->h;.}../
96430 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 69  *.** Return an i
96440 6e 74 65 67 65 72 20 74 68 61 74 20 69 6e 64 69  nteger that indi
96450 63 65 73 20 74 68 65 20 74 79 70 65 20 6f 66 20  ces the type of 
96460 6c 6f 63 6b 20 63 75 72 72 65 6e 74 6c 79 20 68  lock currently h
96470 65 6c 64 0a 2a 2a 20 62 79 20 74 68 69 73 20 68  eld.** by this h
96480 61 6e 64 6c 65 2e 20 20 28 55 73 65 64 20 66 6f  andle.  (Used fo
96490 72 20 74 65 73 74 69 6e 67 20 61 6e 64 20 61 6e  r testing and an
964a0 61 6c 79 73 69 73 20 6f 6e 6c 79 2e 29 0a 2a 2f  alysis only.).*/
964b0 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4c  .static int winL
964c0 6f 63 6b 53 74 61 74 65 28 4f 73 46 69 6c 65 20  ockState(OsFile 
964d0 2a 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 28  *id){.  return (
964e0 28 77 69 6e 46 69 6c 65 2a 29 69 64 29 2d 3e 6c  (winFile*)id)->l
964f0 6f 63 6b 74 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  ocktype;.}../*.*
96500 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 65 63  * Return the sec
96510 74 6f 72 20 73 69 7a 65 20 69 6e 20 62 79 74 65  tor size in byte
96520 73 20 6f 66 20 74 68 65 20 75 6e 64 65 72 6c 79  s of the underly
96530 69 6e 67 20 62 6c 6f 63 6b 20 64 65 76 69 63 65  ing block device
96540 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73 70 65 63   for.** the spec
96550 69 66 69 65 64 20 66 69 6c 65 2e 20 54 68 69 73  ified file. This
96560 20 69 73 20 61 6c 6d 6f 73 74 20 61 6c 77 61 79   is almost alway
96570 73 20 35 31 32 20 62 79 74 65 73 2c 20 62 75 74  s 512 bytes, but
96580 20 6d 61 79 20 62 65 0a 2a 2a 20 6c 61 72 67 65   may be.** large
96590 72 20 66 6f 72 20 73 6f 6d 65 20 64 65 76 69 63  r for some devic
965a0 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65  es..**.** SQLite
965b0 20 63 6f 64 65 20 61 73 73 75 6d 65 73 20 74 68   code assumes th
965c0 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 6e  is function cann
965d0 6f 74 20 66 61 69 6c 2e 20 49 74 20 61 6c 73 6f  ot fail. It also
965e0 20 61 73 73 75 6d 65 73 20 74 68 61 74 0a 2a 2a   assumes that.**
965f0 20 69 66 20 74 77 6f 20 66 69 6c 65 73 20 61 72   if two files ar
96600 65 20 63 72 65 61 74 65 64 20 69 6e 20 74 68 65  e created in the
96610 20 73 61 6d 65 20 66 69 6c 65 2d 73 79 73 74 65   same file-syste
96620 6d 20 64 69 72 65 63 74 6f 72 79 20 28 69 2e 65  m directory (i.e
96630 2e 0a 2a 2a 20 61 20 64 61 74 61 62 61 73 65 20  ..** a database 
96640 61 6e 64 20 69 74 27 73 20 6a 6f 75 72 6e 61 6c  and it's journal
96650 20 66 69 6c 65 29 20 74 68 61 74 20 74 68 65 20   file) that the 
96660 73 65 63 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c  sector size will
96670 20 62 65 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20   be the.** same 
96680 66 6f 72 20 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61  for both..*/.sta
96690 74 69 63 20 69 6e 74 20 77 69 6e 53 65 63 74 6f  tic int winSecto
966a0 72 53 69 7a 65 28 4f 73 46 69 6c 65 20 2a 69 64  rSize(OsFile *id
966b0 29 7b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  ){.  return SQLI
966c0 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f  TE_DEFAULT_SECTO
966d0 52 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  R_SIZE;.}../*.**
966e0 20 54 68 69 73 20 76 65 63 74 6f 72 20 64 65 66   This vector def
966f0 69 6e 65 73 20 61 6c 6c 20 74 68 65 20 6d 65 74  ines all the met
96700 68 6f 64 73 20 74 68 61 74 20 63 61 6e 20 6f 70  hods that can op
96710 65 72 61 74 65 20 6f 6e 20 61 6e 20 4f 73 46 69  erate on an OsFi
96720 6c 65 0a 2a 2a 20 66 6f 72 20 77 69 6e 33 32 2e  le.** for win32.
96730 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .*/.static const
96740 20 49 6f 4d 65 74 68 6f 64 20 73 71 6c 69 74 65   IoMethod sqlite
96750 33 57 69 6e 49 6f 4d 65 74 68 6f 64 20 3d 20 7b  3WinIoMethod = {
96760 0a 20 20 77 69 6e 43 6c 6f 73 65 2c 0a 20 20 77  .  winClose,.  w
96770 69 6e 4f 70 65 6e 44 69 72 65 63 74 6f 72 79 2c  inOpenDirectory,
96780 0a 20 20 77 69 6e 52 65 61 64 2c 0a 20 20 77 69  .  winRead,.  wi
96790 6e 57 72 69 74 65 2c 0a 20 20 77 69 6e 53 65 65  nWrite,.  winSee
967a0 6b 2c 0a 20 20 77 69 6e 54 72 75 6e 63 61 74 65  k,.  winTruncate
967b0 2c 0a 20 20 77 69 6e 53 79 6e 63 2c 0a 20 20 77  ,.  winSync,.  w
967c0 69 6e 53 65 74 46 75 6c 6c 53 79 6e 63 2c 0a 20  inSetFullSync,. 
967d0 20 77 69 6e 46 69 6c 65 48 61 6e 64 6c 65 2c 0a   winFileHandle,.
967e0 20 20 77 69 6e 46 69 6c 65 53 69 7a 65 2c 0a 20    winFileSize,. 
967f0 20 77 69 6e 4c 6f 63 6b 2c 0a 20 20 77 69 6e 55   winLock,.  winU
96800 6e 6c 6f 63 6b 2c 0a 20 20 77 69 6e 4c 6f 63 6b  nlock,.  winLock
96810 53 74 61 74 65 2c 0a 20 20 77 69 6e 43 68 65 63  State,.  winChec
96820 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 2c 0a 20  kReservedLock,. 
96830 20 77 69 6e 53 65 63 74 6f 72 53 69 7a 65 2c 0a   winSectorSize,.
96840 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61  };../*.** Alloca
96850 74 65 20 6d 65 6d 6f 72 79 20 66 6f 72 20 61 6e  te memory for an
96860 20 4f 73 46 69 6c 65 2e 20 20 49 6e 69 74 69 61   OsFile.  Initia
96870 6c 69 7a 65 20 74 68 65 20 6e 65 77 20 4f 73 46  lize the new OsF
96880 69 6c 65 0a 2a 2a 20 74 6f 20 74 68 65 20 76 61  ile.** to the va
96890 6c 75 65 20 67 69 76 65 6e 20 69 6e 20 70 49 6e  lue given in pIn
968a0 69 74 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20  it and return a 
968b0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e  pointer to the n
968c0 65 77 0a 2a 2a 20 4f 73 46 69 6c 65 2e 20 20 49  ew.** OsFile.  I
968d0 66 20 77 65 20 72 75 6e 20 6f 75 74 20 6f 66 20  f we run out of 
968e0 6d 65 6d 6f 72 79 2c 20 63 6c 6f 73 65 20 74 68  memory, close th
968f0 65 20 66 69 6c 65 20 61 6e 64 20 72 65 74 75 72  e file and retur
96900 6e 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69  n NULL..*/.stati
96910 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65 57 69  c int allocateWi
96920 6e 46 69 6c 65 28 77 69 6e 46 69 6c 65 20 2a 70  nFile(winFile *p
96930 49 6e 69 74 2c 20 4f 73 46 69 6c 65 20 2a 2a 70  Init, OsFile **p
96940 49 64 29 7b 0a 20 20 77 69 6e 46 69 6c 65 20 2a  Id){.  winFile *
96950 70 4e 65 77 3b 0a 20 20 70 4e 65 77 20 3d 20 73  pNew;.  pNew = s
96960 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 20 73 69 7a  qliteMalloc( siz
96970 65 6f 66 28 2a 70 4e 65 77 29 20 29 3b 0a 20 20  eof(*pNew) );.  
96980 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20  if( pNew==0 ){. 
96990 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70     CloseHandle(p
969a0 49 6e 69 74 2d 3e 68 29 3b 0a 23 69 66 20 4f 53  Init->h);.#if OS
969b0 5f 57 49 4e 43 45 0a 20 20 20 20 73 71 6c 69 74  _WINCE.    sqlit
969c0 65 46 72 65 65 28 70 49 6e 69 74 2d 3e 7a 44 65  eFree(pInit->zDe
969d0 6c 65 74 65 4f 6e 43 6c 6f 73 65 29 3b 0a 23 65  leteOnClose);.#e
969e0 6e 64 69 66 0a 20 20 20 20 2a 70 49 64 20 3d 20  ndif.    *pId = 
969f0 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  0;.    return SQ
96a00 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65  LITE_NOMEM;.  }e
96a10 6c 73 65 7b 0a 20 20 20 20 2a 70 4e 65 77 20 3d  lse{.    *pNew =
96a20 20 2a 70 49 6e 69 74 3b 0a 20 20 20 20 70 4e 65   *pInit;.    pNe
96a30 77 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 73 71  w->pMethod = &sq
96a40 6c 69 74 65 33 57 69 6e 49 6f 4d 65 74 68 6f 64  lite3WinIoMethod
96a50 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b  ;.    pNew->lock
96a60 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a  type = NO_LOCK;.
96a70 20 20 20 20 70 4e 65 77 2d 3e 73 68 61 72 65 64      pNew->shared
96a80 4c 6f 63 6b 42 79 74 65 20 3d 20 30 3b 0a 20 20  LockByte = 0;.  
96a90 20 20 2a 70 49 64 20 3d 20 28 4f 73 46 69 6c 65    *pId = (OsFile
96aa0 2a 29 70 4e 65 77 3b 0a 20 20 20 20 4f 70 65 6e  *)pNew;.    Open
96ab0 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20 20  Counter(+1);.   
96ac0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
96ad0 4b 3b 0a 20 20 7d 0a 7d 0a 0a 0a 23 65 6e 64 69  K;.  }.}...#endi
96ae0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
96af0 5f 44 49 53 4b 49 4f 20 2a 2f 0a 2f 2a 2a 2a 2a  _DISKIO */./****
96b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
96b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
96b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
96b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
96b40 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 45 76 65 72 79  *******.** Every
96b50 74 68 69 6e 67 20 61 62 6f 76 65 20 64 65 61 6c  thing above deal
96b60 73 20 77 69 74 68 20 66 69 6c 65 20 49 2f 4f 2e  s with file I/O.
96b70 20 20 45 76 65 72 79 74 68 69 6e 67 20 74 68 61    Everything tha
96b80 74 20 66 6f 6c 6c 6f 77 73 20 64 65 61 6c 73 0a  t follows deals.
96b90 2a 2a 20 77 69 74 68 20 6f 74 68 65 72 20 6d 69  ** with other mi
96ba0 73 63 65 6c 6c 61 6e 6f 75 73 20 61 73 70 65 63  scellanous aspec
96bb0 74 73 20 6f 66 20 74 68 65 20 6f 70 65 72 61 74  ts of the operat
96bc0 69 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72  ing system inter
96bd0 66 61 63 65 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  face.***********
96be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
96bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
96c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
96c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
96c20 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64  */..#if !defined
96c30 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41  (SQLITE_OMIT_LOA
96c40 44 5f 45 58 54 45 4e 53 49 4f 4e 29 0a 2f 2a 0a  D_EXTENSION)./*.
96c50 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 20 66 6f  ** Interfaces fo
96c60 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 72  r opening a shar
96c70 65 64 20 6c 69 62 72 61 72 79 2c 20 66 69 6e 64  ed library, find
96c80 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 73  ing entry points
96c90 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 73  .** within the s
96ca0 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 61  hared library, a
96cb0 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 73  nd closing the s
96cc0 68 61 72 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a  hared library..*
96cd0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
96ce0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 57 69   void *sqlite3Wi
96cf0 6e 44 6c 6f 70 65 6e 28 63 6f 6e 73 74 20 63 68  nDlopen(const ch
96d00 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a  ar *zFilename){.
96d10 20 20 48 41 4e 44 4c 45 20 68 3b 0a 20 20 76 6f    HANDLE h;.  vo
96d20 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d  id *zConverted =
96d30 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65   convertUtf8File
96d40 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b  name(zFilename);
96d50 0a 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 65  .  if( zConverte
96d60 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  d==0 ){.    retu
96d70 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20  rn 0;.  }.  if( 
96d80 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 68 20  isNT() ){.    h 
96d90 3d 20 4c 6f 61 64 4c 69 62 72 61 72 79 57 28 28  = LoadLibraryW((
96da0 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65  WCHAR*)zConverte
96db0 64 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 23 69 66  d);.  }else{.#if
96dc0 20 4f 53 5f 57 49 4e 43 45 0a 20 20 20 20 72 65   OS_WINCE.    re
96dd0 74 75 72 6e 20 30 3b 0a 23 65 6c 73 65 0a 20 20  turn 0;.#else.  
96de0 20 20 68 20 3d 20 4c 6f 61 64 4c 69 62 72 61 72    h = LoadLibrar
96df0 79 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65  yA((char*)zConve
96e00 72 74 65 64 29 3b 0a 23 65 6e 64 69 66 0a 20 20  rted);.#endif.  
96e10 7d 0a 20 20 73 71 6c 69 74 65 46 72 65 65 28 7a  }.  sqliteFree(z
96e20 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 72 65  Converted);.  re
96e30 74 75 72 6e 20 28 76 6f 69 64 2a 29 68 3b 0a 20  turn (void*)h;. 
96e40 20 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   .}.SQLITE_PRIVA
96e50 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
96e60 57 69 6e 44 6c 73 79 6d 28 76 6f 69 64 20 2a 70  WinDlsym(void *p
96e70 48 61 6e 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68  Handle, const ch
96e80 61 72 20 2a 7a 53 79 6d 62 6f 6c 29 7b 0a 23 69  ar *zSymbol){.#i
96e90 66 20 4f 53 5f 57 49 4e 43 45 0a 20 20 2f 2a 20  f OS_WINCE.  /* 
96ea0 54 68 65 20 47 65 74 50 72 6f 63 41 64 64 72 65  The GetProcAddre
96eb0 73 73 41 28 29 20 72 6f 75 74 69 6e 65 20 69 73  ssA() routine is
96ec0 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20   only available 
96ed0 6f 6e 20 77 69 6e 63 65 2e 20 2a 2f 0a 20 20 72  on wince. */.  r
96ee0 65 74 75 72 6e 20 47 65 74 50 72 6f 63 41 64 64  eturn GetProcAdd
96ef0 72 65 73 73 41 28 28 48 41 4e 44 4c 45 29 70 48  ressA((HANDLE)pH
96f00 61 6e 64 6c 65 2c 20 7a 53 79 6d 62 6f 6c 29 3b  andle, zSymbol);
96f10 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 41 6c 6c 20  .#else.  /* All 
96f20 6f 74 68 65 72 20 77 69 6e 64 6f 77 73 20 70 6c  other windows pl
96f30 61 74 66 6f 72 6d 73 20 65 78 70 65 63 74 20 47  atforms expect G
96f40 65 74 50 72 6f 63 41 64 64 72 65 73 73 28 29 20  etProcAddress() 
96f50 74 6f 20 74 61 6b 65 0a 20 20 2a 2a 20 61 6e 20  to take.  ** an 
96f60 41 6e 73 69 20 73 74 72 69 6e 67 20 72 65 67 61  Ansi string rega
96f70 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 5f 55  rdless of the _U
96f80 4e 49 43 4f 44 45 20 73 65 74 74 69 6e 67 20 2a  NICODE setting *
96f90 2f 0a 20 20 72 65 74 75 72 6e 20 47 65 74 50 72  /.  return GetPr
96fa0 6f 63 41 64 64 72 65 73 73 28 28 48 41 4e 44 4c  ocAddress((HANDL
96fb0 45 29 70 48 61 6e 64 6c 65 2c 20 7a 53 79 6d 62  E)pHandle, zSymb
96fc0 6f 6c 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 53 51  ol);.#endif.}.SQ
96fd0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
96fe0 20 73 71 6c 69 74 65 33 57 69 6e 44 6c 63 6c 6f   sqlite3WinDlclo
96ff0 73 65 28 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65  se(void *pHandle
97000 29 7b 0a 20 20 72 65 74 75 72 6e 20 46 72 65 65  ){.  return Free
97010 4c 69 62 72 61 72 79 28 28 48 41 4e 44 4c 45 29  Library((HANDLE)
97020 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6e 64  pHandle);.}.#end
97030 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d  if /* !SQLITE_OM
97040 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f  IT_LOAD_EXTENSIO
97050 4e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20  N */../*.** Get 
97060 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 73  information to s
97070 65 65 64 20 74 68 65 20 72 61 6e 64 6f 6d 20 6e  eed the random n
97080 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 2e  umber generator.
97090 20 20 54 68 65 20 73 65 65 64 0a 2a 2a 20 69 73    The seed.** is
970a0 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68   written into th
970b0 65 20 62 75 66 66 65 72 20 7a 42 75 66 5b 32 35  e buffer zBuf[25
970c0 36 5d 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67  6].  The calling
970d0 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 0a 2a   function must.*
970e0 2a 20 73 75 70 70 6c 79 20 61 20 73 75 66 66 69  * supply a suffi
970f0 63 69 65 6e 74 6c 79 20 6c 61 72 67 65 20 62 75  ciently large bu
97100 66 66 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ffer..*/.SQLITE_
97110 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
97120 74 65 33 57 69 6e 52 61 6e 64 6f 6d 53 65 65 64  te3WinRandomSeed
97130 28 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20  (char *zBuf){.  
97140 2f 2a 20 57 65 20 68 61 76 65 20 74 6f 20 69 6e  /* We have to in
97150 69 74 69 61 6c 69 7a 65 20 7a 42 75 66 20 74 6f  itialize zBuf to
97160 20 70 72 65 76 65 6e 74 20 76 61 6c 67 72 69 6e   prevent valgrin
97170 64 20 66 72 6f 6d 20 72 65 70 6f 72 74 69 6e 67  d from reporting
97180 0a 20 20 2a 2a 20 65 72 72 6f 72 73 2e 20 20 54  .  ** errors.  T
97190 68 65 20 72 65 70 6f 72 74 73 20 69 73 73 75 65  he reports issue
971a0 64 20 62 79 20 76 61 6c 67 72 69 6e 64 20 61 72  d by valgrind ar
971b0 65 20 69 6e 63 6f 72 72 65 63 74 20 2d 20 77 65  e incorrect - we
971c0 20 77 6f 75 6c 64 0a 20 20 2a 2a 20 70 72 65 66   would.  ** pref
971d0 65 72 20 74 68 61 74 20 74 68 65 20 72 61 6e 64  er that the rand
971e0 6f 6d 6e 65 73 73 20 62 65 20 69 6e 63 72 65 61  omness be increa
971f0 73 65 64 20 62 79 20 6d 61 6b 69 6e 67 20 75 73  sed by making us
97200 65 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 75 6e  e of the.  ** un
97210 69 6e 69 74 69 61 6c 69 7a 65 64 20 73 70 61 63  initialized spac
97220 65 20 69 6e 20 7a 42 75 66 20 2d 20 62 75 74 20  e in zBuf - but 
97230 76 61 6c 67 72 69 6e 64 20 65 72 72 6f 72 73 20  valgrind errors 
97240 74 65 6e 64 20 74 6f 20 77 6f 72 72 79 0a 20 20  tend to worry.  
97250 2a 2a 20 73 6f 6d 65 20 75 73 65 72 73 2e 20 20  ** some users.  
97260 52 61 74 68 65 72 20 74 68 61 6e 20 61 72 67 75  Rather than argu
97270 65 2c 20 69 74 20 73 65 65 6d 73 20 65 61 73 69  e, it seems easi
97280 65 72 20 6a 75 73 74 20 74 6f 20 69 6e 69 74 69  er just to initi
97290 61 6c 69 7a 65 0a 20 20 2a 2a 20 74 68 65 20 77  alize.  ** the w
972a0 68 6f 6c 65 20 61 72 72 61 79 20 61 6e 64 20 73  hole array and s
972b0 69 6c 65 6e 63 65 20 76 61 6c 67 72 69 6e 64 2c  ilence valgrind,
972c0 20 65 76 65 6e 20 69 66 20 74 68 61 74 20 6d 65   even if that me
972d0 61 6e 73 20 6c 65 73 73 20 72 61 6e 64 6f 6d 6e  ans less randomn
972e0 65 73 73 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20  ess.  ** in the 
972f0 72 61 6e 64 6f 6d 20 73 65 65 64 2e 0a 20 20 2a  random seed..  *
97300 2a 0a 20 20 2a 2a 20 57 68 65 6e 20 74 65 73 74  *.  ** When test
97310 69 6e 67 2c 20 69 6e 69 74 69 61 6c 69 7a 69 6e  ing, initializin
97320 67 20 7a 42 75 66 5b 5d 20 74 6f 20 7a 65 72 6f  g zBuf[] to zero
97330 20 69 73 20 61 6c 6c 20 77 65 20 64 6f 2e 20 20   is all we do.  
97340 54 68 61 74 20 6d 65 61 6e 73 0a 20 20 2a 2a 20  That means.  ** 
97350 74 68 61 74 20 77 65 20 61 6c 77 61 79 73 20 75  that we always u
97360 73 65 20 74 68 65 20 73 61 6d 65 20 72 61 6e 64  se the same rand
97370 6f 6d 20 6e 75 6d 62 65 72 20 73 65 71 75 65 6e  om number sequen
97380 63 65 2e 2a 20 54 68 69 73 20 6d 61 6b 65 73 20  ce.* This makes 
97390 74 68 65 0a 20 20 2a 2a 20 74 65 73 74 73 20 72  the.  ** tests r
973a0 65 70 65 61 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a  epeatable..  */.
973b0 20 20 6d 65 6d 73 65 74 28 7a 42 75 66 2c 20 30    memset(zBuf, 0
973c0 2c 20 32 35 36 29 3b 0a 20 20 47 65 74 53 79 73  , 256);.  GetSys
973d0 74 65 6d 54 69 6d 65 28 28 4c 50 53 59 53 54 45  temTime((LPSYSTE
973e0 4d 54 49 4d 45 29 7a 42 75 66 29 3b 0a 20 20 72  MTIME)zBuf);.  r
973f0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
97400 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20  .}../*.** Sleep 
97410 66 6f 72 20 61 20 6c 69 74 74 6c 65 20 77 68 69  for a little whi
97420 6c 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20  le.  Return the 
97430 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20 73  amount of time s
97440 6c 65 70 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  lept..*/.SQLITE_
97450 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
97460 74 65 33 57 69 6e 53 6c 65 65 70 28 69 6e 74 20  te3WinSleep(int 
97470 6d 73 29 7b 0a 20 20 53 6c 65 65 70 28 6d 73 29  ms){.  Sleep(ms)
97480 3b 0a 20 20 72 65 74 75 72 6e 20 6d 73 3b 0a 7d  ;.  return ms;.}
97490 0a 0a 2f 2a 0a 2a 2a 20 53 74 61 74 69 63 20 76  ../*.** Static v
974a0 61 72 69 61 62 6c 65 73 20 75 73 65 64 20 66 6f  ariables used fo
974b0 72 20 74 68 72 65 61 64 20 73 79 6e 63 68 72 6f  r thread synchro
974c0 6e 69 7a 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74  nization.*/.stat
974d0 69 63 20 69 6e 74 20 69 6e 4d 75 74 65 78 20 3d  ic int inMutex =
974e0 20 30 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54   0;.#ifdef SQLIT
974f0 45 5f 57 33 32 5f 54 48 52 45 41 44 53 0a 20 20  E_W32_THREADS.  
97500 73 74 61 74 69 63 20 44 57 4f 52 44 20 6d 75 74  static DWORD mut
97510 65 78 4f 77 6e 65 72 3b 0a 20 20 73 74 61 74 69  exOwner;.  stati
97520 63 20 43 52 49 54 49 43 41 4c 5f 53 45 43 54 49  c CRITICAL_SECTI
97530 4f 4e 20 63 73 3b 0a 23 65 6e 64 69 66 0a 0a 2f  ON cs;.#endif../
97540 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
97550 6e 67 20 70 61 69 72 20 6f 66 20 72 6f 75 74 69  ng pair of routi
97560 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75  nes implement mu
97570 74 75 61 6c 20 65 78 63 6c 75 73 69 6f 6e 20 66  tual exclusion f
97580 6f 72 0a 2a 2a 20 6d 75 6c 74 69 2d 74 68 72 65  or.** multi-thre
97590 61 64 65 64 20 70 72 6f 63 65 73 73 65 73 2e 20  aded processes. 
975a0 20 4f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 74   Only a single t
975b0 68 72 65 61 64 20 69 73 20 61 6c 6c 6f 77 65 64  hread is allowed
975c0 20 74 6f 0a 2a 2a 20 65 78 65 63 75 74 65 64 20   to.** executed 
975d0 63 6f 64 65 20 74 68 61 74 20 69 73 20 73 75 72  code that is sur
975e0 72 6f 75 6e 64 65 64 20 62 79 20 45 6e 74 65 72  rounded by Enter
975f0 4d 75 74 65 78 28 29 20 61 6e 64 20 4c 65 61 76  Mutex() and Leav
97600 65 4d 75 74 65 78 28 29 2e 0a 2a 2a 0a 2a 2a 20  eMutex()..**.** 
97610 53 51 4c 69 74 65 20 75 73 65 73 20 6f 6e 6c 79  SQLite uses only
97620 20 61 20 73 69 6e 67 6c 65 20 4d 75 74 65 78 2e   a single Mutex.
97630 20 20 54 68 65 72 65 20 69 73 20 6e 6f 74 20 6d    There is not m
97640 75 63 68 20 63 72 69 74 69 63 61 6c 0a 2a 2a 20  uch critical.** 
97650 63 6f 64 65 20 61 6e 64 20 77 68 61 74 20 6c 69  code and what li
97660 74 74 6c 65 20 74 68 65 72 65 20 69 73 20 65 78  ttle there is ex
97670 65 63 75 74 65 73 20 71 75 69 63 6b 6c 79 20 61  ecutes quickly a
97680 6e 64 20 77 69 74 68 6f 75 74 20 62 6c 6f 63 6b  nd without block
97690 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 56 65 72 73 69  ing..**.** Versi
976a0 6f 6e 20 33 2e 33 2e 31 20 61 6e 64 20 65 61 72  on 3.3.1 and ear
976b0 6c 69 65 72 20 75 73 65 64 20 61 20 73 69 6d 70  lier used a simp
976c0 6c 65 20 6d 75 74 65 78 2e 20 20 42 65 67 69 6e  le mutex.  Begin
976d0 6e 69 6e 67 20 77 69 74 68 0a 2a 2a 20 76 65 72  ning with.** ver
976e0 73 69 6f 6e 20 33 2e 33 2e 32 2c 20 61 20 72 65  sion 3.3.2, a re
976f0 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 73  cursive mutex is
97700 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 53 51   required..*/.SQ
97710 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
97720 64 20 73 71 6c 69 74 65 33 57 69 6e 45 6e 74 65  d sqlite3WinEnte
97730 72 4d 75 74 65 78 28 29 7b 0a 23 69 66 64 65 66  rMutex(){.#ifdef
97740 20 53 51 4c 49 54 45 5f 57 33 32 5f 54 48 52 45   SQLITE_W32_THRE
97750 41 44 53 0a 20 20 73 74 61 74 69 63 20 69 6e 74  ADS.  static int
97760 20 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 77   isInit = 0;.  w
97770 68 69 6c 65 28 20 21 69 73 49 6e 69 74 20 29 7b  hile( !isInit ){
97780 0a 20 20 20 20 73 74 61 74 69 63 20 6c 6f 6e 67  .    static long
97790 20 6c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20 69   lock = 0;.    i
977a0 66 28 20 49 6e 74 65 72 6c 6f 63 6b 65 64 49 6e  f( InterlockedIn
977b0 63 72 65 6d 65 6e 74 28 26 6c 6f 63 6b 29 3d 3d  crement(&lock)==
977c0 31 20 29 7b 0a 20 20 20 20 20 20 49 6e 69 74 69  1 ){.      Initi
977d0 61 6c 69 7a 65 43 72 69 74 69 63 61 6c 53 65 63  alizeCriticalSec
977e0 74 69 6f 6e 28 26 63 73 29 3b 0a 20 20 20 20 20  tion(&cs);.     
977f0 20 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 20   isInit = 1;.   
97800 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 53 6c   }else{.      Sl
97810 65 65 70 28 31 29 3b 0a 20 20 20 20 7d 0a 20 20  eep(1);.    }.  
97820 7d 0a 20 20 45 6e 74 65 72 43 72 69 74 69 63 61  }.  EnterCritica
97830 6c 53 65 63 74 69 6f 6e 28 26 63 73 29 3b 0a 20  lSection(&cs);. 
97840 20 6d 75 74 65 78 4f 77 6e 65 72 20 3d 20 47 65   mutexOwner = Ge
97850 74 43 75 72 72 65 6e 74 54 68 72 65 61 64 49 64  tCurrentThreadId
97860 28 29 3b 0a 23 65 6e 64 69 66 0a 20 20 69 6e 4d  ();.#endif.  inM
97870 75 74 65 78 2b 2b 3b 0a 7d 0a 53 51 4c 49 54 45  utex++;.}.SQLITE
97880 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
97890 6c 69 74 65 33 57 69 6e 4c 65 61 76 65 4d 75 74  lite3WinLeaveMut
978a0 65 78 28 29 7b 0a 20 20 61 73 73 65 72 74 28 20  ex(){.  assert( 
978b0 69 6e 4d 75 74 65 78 20 29 3b 0a 20 20 69 6e 4d  inMutex );.  inM
978c0 75 74 65 78 2d 2d 3b 0a 23 69 66 64 65 66 20 53  utex--;.#ifdef S
978d0 51 4c 49 54 45 5f 57 33 32 5f 54 48 52 45 41 44  QLITE_W32_THREAD
978e0 53 0a 20 20 61 73 73 65 72 74 28 20 6d 75 74 65  S.  assert( mute
978f0 78 4f 77 6e 65 72 3d 3d 47 65 74 43 75 72 72 65  xOwner==GetCurre
97900 6e 74 54 68 72 65 61 64 49 64 28 29 20 29 3b 0a  ntThreadId() );.
97910 20 20 4c 65 61 76 65 43 72 69 74 69 63 61 6c 53    LeaveCriticalS
97920 65 63 74 69 6f 6e 28 26 63 73 29 3b 0a 23 65 6e  ection(&cs);.#en
97930 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  dif.}../*.** Ret
97940 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20  urn TRUE if the 
97950 6d 75 74 65 78 20 69 73 20 63 75 72 72 65 6e 74  mutex is current
97960 6c 79 20 68 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 49  ly held..**.** I
97970 66 20 74 68 65 20 74 68 69 73 54 68 72 65 61 64  f the thisThread
97980 4f 6e 6c 79 20 70 61 72 61 6d 65 74 65 72 20 69  Only parameter i
97990 73 20 74 72 75 65 2c 20 72 65 74 75 72 6e 20 74  s true, return t
979a0 72 75 65 20 69 66 20 61 6e 64 20 6f 6e 6c 79 20  rue if and only 
979b0 69 66 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 69 6e  if the.** callin
979c0 67 20 74 68 72 65 61 64 20 68 6f 6c 64 73 20 74  g thread holds t
979d0 68 65 20 6d 75 74 65 78 2e 20 20 49 66 20 74 68  he mutex.  If th
979e0 65 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 66  e parameter is f
979f0 61 6c 73 65 2c 20 72 65 74 75 72 6e 0a 2a 2a 20  alse, return.** 
97a00 74 72 75 65 20 69 66 20 61 6e 79 20 74 68 72 65  true if any thre
97a10 61 64 20 68 6f 6c 64 73 20 74 68 65 20 6d 75 74  ad holds the mut
97a20 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ex..*/.SQLITE_PR
97a30 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
97a40 33 57 69 6e 49 6e 4d 75 74 65 78 28 69 6e 74 20  3WinInMutex(int 
97a50 74 68 69 73 54 68 72 65 61 64 4f 6e 6c 79 29 7b  thisThreadOnly){
97a60 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 57  .#ifdef SQLITE_W
97a70 33 32 5f 54 48 52 45 41 44 53 0a 20 20 72 65 74  32_THREADS.  ret
97a80 75 72 6e 20 69 6e 4d 75 74 65 78 3e 30 20 26 26  urn inMutex>0 &&
97a90 20 28 74 68 69 73 54 68 72 65 61 64 4f 6e 6c 79   (thisThreadOnly
97aa0 3d 3d 30 20 7c 7c 20 6d 75 74 65 78 4f 77 6e 65  ==0 || mutexOwne
97ab0 72 3d 3d 47 65 74 43 75 72 72 65 6e 74 54 68 72  r==GetCurrentThr
97ac0 65 61 64 49 64 28 29 29 3b 0a 23 65 6c 73 65 0a  eadId());.#else.
97ad0 20 20 72 65 74 75 72 6e 20 69 6e 4d 75 74 65 78    return inMutex
97ae0 3e 30 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 0a 2f  >0;.#endif.}.../
97af0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
97b00 6e 67 20 76 61 72 69 61 62 6c 65 2c 20 69 66 20  ng variable, if 
97b10 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72  set to a non-zer
97b20 6f 20 76 61 6c 75 65 2c 20 62 65 63 6f 6d 65 73  o value, becomes
97b30 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 72   the result.** r
97b40 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 73 71 6c  eturned from sql
97b50 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d  ite3OsCurrentTim
97b60 65 28 29 2e 20 20 54 68 69 73 20 69 73 20 75 73  e().  This is us
97b70 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 2e 0a  ed for testing..
97b80 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
97b90 5f 54 45 53 54 0a 69 6e 74 20 73 71 6c 69 74 65  _TEST.int sqlite
97ba0 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 3d  3_current_time =
97bb0 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a   0;.#endif../*.*
97bc0 2a 20 46 69 6e 64 20 74 68 65 20 63 75 72 72 65  * Find the curre
97bd0 6e 74 20 74 69 6d 65 20 28 69 6e 20 55 6e 69 76  nt time (in Univ
97be0 65 72 73 61 6c 20 43 6f 6f 72 64 69 6e 61 74 65  ersal Coordinate
97bf0 64 20 54 69 6d 65 29 2e 20 20 57 72 69 74 65 20  d Time).  Write 
97c00 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 74  the.** current t
97c10 69 6d 65 20 61 6e 64 20 64 61 74 65 20 61 73 20  ime and date as 
97c20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d  a Julian Day num
97c30 62 65 72 20 69 6e 74 6f 20 2a 70 72 4e 6f 77 20  ber into *prNow 
97c40 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e  and.** return 0.
97c50 20 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68    Return 1 if th
97c60 65 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20  e time and date 
97c70 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e  cannot be found.
97c80 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
97c90 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 69  TE int sqlite3Wi
97ca0 6e 43 75 72 72 65 6e 74 54 69 6d 65 28 64 6f 75  nCurrentTime(dou
97cb0 62 6c 65 20 2a 70 72 4e 6f 77 29 7b 0a 20 20 46  ble *prNow){.  F
97cc0 49 4c 45 54 49 4d 45 20 66 74 3b 0a 20 20 2f 2a  ILETIME ft;.  /*
97cd0 20 46 49 4c 45 54 49 4d 45 20 73 74 72 75 63 74   FILETIME struct
97ce0 75 72 65 20 69 73 20 61 20 36 34 2d 62 69 74 20  ure is a 64-bit 
97cf0 76 61 6c 75 65 20 72 65 70 72 65 73 65 6e 74 69  value representi
97d00 6e 67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ng the number of
97d10 20 0a 20 20 20 20 20 31 30 30 2d 6e 61 6e 6f 73   .     100-nanos
97d20 65 63 6f 6e 64 20 69 6e 74 65 72 76 61 6c 73 20  econd intervals 
97d30 73 69 6e 63 65 20 4a 61 6e 75 61 72 79 20 31 2c  since January 1,
97d40 20 31 36 30 31 20 28 3d 20 4a 44 20 32 33 30 35   1601 (= JD 2305
97d50 38 31 33 2e 35 29 2e 20 0a 20 20 2a 2f 0a 20 20  813.5). .  */.  
97d60 64 6f 75 62 6c 65 20 6e 6f 77 3b 0a 23 69 66 20  double now;.#if 
97d70 4f 53 5f 57 49 4e 43 45 0a 20 20 53 59 53 54 45  OS_WINCE.  SYSTE
97d80 4d 54 49 4d 45 20 74 69 6d 65 3b 0a 20 20 47 65  MTIME time;.  Ge
97d90 74 53 79 73 74 65 6d 54 69 6d 65 28 26 74 69 6d  tSystemTime(&tim
97da0 65 29 3b 0a 20 20 53 79 73 74 65 6d 54 69 6d 65  e);.  SystemTime
97db0 54 6f 46 69 6c 65 54 69 6d 65 28 26 74 69 6d 65  ToFileTime(&time
97dc0 2c 26 66 74 29 3b 0a 23 65 6c 73 65 0a 20 20 47  ,&ft);.#else.  G
97dd0 65 74 53 79 73 74 65 6d 54 69 6d 65 41 73 46 69  etSystemTimeAsFi
97de0 6c 65 54 69 6d 65 28 20 26 66 74 20 29 3b 0a 23  leTime( &ft );.#
97df0 65 6e 64 69 66 0a 20 20 6e 6f 77 20 3d 20 28 28  endif.  now = ((
97e00 64 6f 75 62 6c 65 29 66 74 2e 64 77 48 69 67 68  double)ft.dwHigh
97e10 44 61 74 65 54 69 6d 65 29 20 2a 20 34 32 39 34  DateTime) * 4294
97e20 39 36 37 32 39 36 2e 30 3b 20 0a 20 20 2a 70 72  967296.0; .  *pr
97e30 4e 6f 77 20 3d 20 28 6e 6f 77 20 2b 20 66 74 2e  Now = (now + ft.
97e40 64 77 4c 6f 77 44 61 74 65 54 69 6d 65 29 2f 38  dwLowDateTime)/8
97e50 36 34 30 30 30 30 30 30 30 30 30 2e 30 20 2b 20  64000000000.0 + 
97e60 32 33 30 35 38 31 33 2e 35 3b 0a 23 69 66 64 65  2305813.5;.#ifde
97e70 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20  f SQLITE_TEST.  
97e80 69 66 28 20 73 71 6c 69 74 65 33 5f 63 75 72 72  if( sqlite3_curr
97e90 65 6e 74 5f 74 69 6d 65 20 29 7b 0a 20 20 20 20  ent_time ){.    
97ea0 2a 70 72 4e 6f 77 20 3d 20 73 71 6c 69 74 65 33  *prNow = sqlite3
97eb0 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 2f 38 36  _current_time/86
97ec0 34 30 30 2e 30 20 2b 20 32 34 34 30 35 38 37 2e  400.0 + 2440587.
97ed0 35 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  5;.  }.#endif.  
97ee0 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
97ef0 2a 2a 20 52 65 6d 65 6d 62 65 72 20 74 68 65 20  ** Remember the 
97f00 6e 75 6d 62 65 72 20 6f 66 20 74 68 72 65 61 64  number of thread
97f10 2d 73 70 65 63 69 66 69 63 2d 64 61 74 61 20 62  -specific-data b
97f20 6c 6f 63 6b 73 20 61 6c 6c 6f 63 61 74 65 64 2e  locks allocated.
97f30 0a 2a 2a 20 55 73 65 20 74 68 69 73 20 74 6f 20  .** Use this to 
97f40 76 65 72 69 66 79 20 74 68 61 74 20 77 65 20 61  verify that we a
97f50 72 65 20 6e 6f 74 20 6c 65 61 6b 69 6e 67 20 74  re not leaking t
97f60 68 72 65 61 64 2d 73 70 65 63 69 66 69 63 2d 64  hread-specific-d
97f70 61 74 61 2e 0a 2a 2a 20 54 69 63 6b 65 74 20 23  ata..** Ticket #
97f80 31 36 30 31 0a 2a 2f 0a 23 69 66 64 65 66 20 53  1601.*/.#ifdef S
97f90 51 4c 49 54 45 5f 54 45 53 54 0a 69 6e 74 20 73  QLITE_TEST.int s
97fa0 71 6c 69 74 65 33 5f 74 73 64 5f 63 6f 75 6e 74  qlite3_tsd_count
97fb0 20 3d 20 30 3b 0a 23 20 64 65 66 69 6e 65 20 54   = 0;.# define T
97fc0 53 44 5f 43 4f 55 4e 54 45 52 5f 49 4e 43 52 20  SD_COUNTER_INCR 
97fd0 49 6e 74 65 72 6c 6f 63 6b 65 64 49 6e 63 72 65  InterlockedIncre
97fe0 6d 65 6e 74 28 26 73 71 6c 69 74 65 33 5f 74 73  ment(&sqlite3_ts
97ff0 64 5f 63 6f 75 6e 74 29 0a 23 20 64 65 66 69 6e  d_count).# defin
98000 65 20 54 53 44 5f 43 4f 55 4e 54 45 52 5f 44 45  e TSD_COUNTER_DE
98010 43 52 20 49 6e 74 65 72 6c 6f 63 6b 65 64 44 65  CR InterlockedDe
98020 63 72 65 6d 65 6e 74 28 26 73 71 6c 69 74 65 33  crement(&sqlite3
98030 5f 74 73 64 5f 63 6f 75 6e 74 29 0a 23 65 6c 73  _tsd_count).#els
98040 65 0a 23 20 64 65 66 69 6e 65 20 54 53 44 5f 43  e.# define TSD_C
98050 4f 55 4e 54 45 52 5f 49 4e 43 52 20 20 2f 2a 20  OUNTER_INCR  /* 
98060 6e 6f 2d 6f 70 20 2a 2f 0a 23 20 64 65 66 69 6e  no-op */.# defin
98070 65 20 54 53 44 5f 43 4f 55 4e 54 45 52 5f 44 45  e TSD_COUNTER_DE
98080 43 52 20 20 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f 0a  CR  /* no-op */.
98090 23 65 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a 2a 20  #endif..../*.** 
980a0 49 66 20 63 61 6c 6c 65 64 20 77 69 74 68 20 61  If called with a
980b0 6c 6c 6f 63 61 74 65 46 6c 61 67 3e 31 2c 20 74  llocateFlag>1, t
980c0 68 65 6e 20 72 65 74 75 72 6e 20 61 20 70 6f 69  hen return a poi
980d0 6e 74 65 72 20 74 6f 20 74 68 72 65 61 64 0a 2a  nter to thread.*
980e0 2a 20 73 70 65 63 69 66 69 63 20 64 61 74 61 20  * specific data 
980f0 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20  for the current 
98100 74 68 72 65 61 64 2e 20 20 41 6c 6c 6f 63 61 74  thread.  Allocat
98110 65 20 61 6e 64 20 7a 65 72 6f 20 74 68 65 0a 2a  e and zero the.*
98120 2a 20 74 68 72 65 61 64 2d 73 70 65 63 69 66 69  * thread-specifi
98130 63 20 64 61 74 61 20 69 66 20 69 74 20 64 6f 65  c data if it doe
98140 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78  s not already ex
98150 69 73 74 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a  ist necessary..*
98160 2a 0a 2a 2a 20 49 66 20 63 61 6c 6c 65 64 20 77  *.** If called w
98170 69 74 68 20 61 6c 6c 6f 63 61 74 65 46 6c 61 67  ith allocateFlag
98180 3d 3d 30 2c 20 74 68 65 6e 20 63 68 65 63 6b 20  ==0, then check 
98190 74 68 65 20 63 75 72 72 65 6e 74 20 74 68 72 65  the current thre
981a0 61 64 0a 2a 2a 20 73 70 65 63 69 66 69 63 20 64  ad.** specific d
981b0 61 74 61 2e 20 20 52 65 74 75 72 6e 20 69 74 20  ata.  Return it 
981c0 69 66 20 69 74 20 65 78 69 73 74 73 2e 20 20 49  if it exists.  I
981d0 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 65 78  f it does not ex
981e0 69 73 74 2c 0a 2a 2a 20 74 68 65 6e 20 72 65 74  ist,.** then ret
981f0 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20  urn NULL..**.** 
98200 49 66 20 63 61 6c 6c 65 64 20 77 69 74 68 20 61  If called with a
98210 6c 6c 6f 63 61 74 65 46 6c 61 67 3c 30 2c 20 63  llocateFlag<0, c
98220 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74  heck to see if t
98230 68 65 20 74 68 72 65 61 64 20 73 70 65 63 69 66  he thread specif
98240 69 63 0a 2a 2a 20 64 61 74 61 20 69 73 20 61 6c  ic.** data is al
98250 6c 6f 63 61 74 65 64 20 61 6e 64 20 69 73 20 61  located and is a
98260 6c 6c 20 7a 65 72 6f 2e 20 20 49 66 20 69 74 20  ll zero.  If it 
98270 69 73 20 74 68 65 6e 20 64 65 61 6c 6c 6f 63 61  is then dealloca
98280 74 65 20 69 74 2e 0a 2a 2a 20 52 65 74 75 72 6e  te it..** Return
98290 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
982a0 65 20 74 68 72 65 61 64 20 73 70 65 63 69 66 69  e thread specifi
982b0 63 20 64 61 74 61 20 6f 72 20 4e 55 4c 4c 20 69  c data or NULL i
982c0 66 20 69 74 20 69 73 0a 2a 2a 20 75 6e 61 6c 6c  f it is.** unall
982d0 6f 63 61 74 65 64 20 6f 72 20 67 65 74 73 20 64  ocated or gets d
982e0 65 61 6c 6c 6f 63 61 74 65 64 2e 0a 2a 2f 0a 53  eallocated..*/.S
982f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 68  QLITE_PRIVATE Th
98300 72 65 61 64 44 61 74 61 20 2a 73 71 6c 69 74 65  readData *sqlite
98310 33 57 69 6e 54 68 72 65 61 64 53 70 65 63 69 66  3WinThreadSpecif
98320 69 63 44 61 74 61 28 69 6e 74 20 61 6c 6c 6f 63  icData(int alloc
98330 61 74 65 46 6c 61 67 29 7b 0a 20 20 73 74 61 74  ateFlag){.  stat
98340 69 63 20 69 6e 74 20 6b 65 79 3b 0a 20 20 73 74  ic int key;.  st
98350 61 74 69 63 20 69 6e 74 20 6b 65 79 49 6e 69 74  atic int keyInit
98360 20 3d 20 30 3b 0a 20 20 73 74 61 74 69 63 20 63   = 0;.  static c
98370 6f 6e 73 74 20 54 68 72 65 61 64 44 61 74 61 20  onst ThreadData 
98380 7a 65 72 6f 44 61 74 61 20 3d 20 7b 30 7d 3b 0a  zeroData = {0};.
98390 20 20 54 68 72 65 61 64 44 61 74 61 20 2a 70 54    ThreadData *pT
983a0 73 64 3b 0a 0a 20 20 69 66 28 20 21 6b 65 79 49  sd;..  if( !keyI
983b0 6e 69 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  nit ){.    sqlit
983c0 65 33 4f 73 45 6e 74 65 72 4d 75 74 65 78 28 29  e3OsEnterMutex()
983d0 3b 0a 20 20 20 20 69 66 28 20 21 6b 65 79 49 6e  ;.    if( !keyIn
983e0 69 74 20 29 7b 0a 20 20 20 20 20 20 6b 65 79 20  it ){.      key 
983f0 3d 20 54 6c 73 41 6c 6c 6f 63 28 29 3b 0a 20 20  = TlsAlloc();.  
98400 20 20 20 20 69 66 28 20 6b 65 79 3d 3d 30 78 66      if( key==0xf
98410 66 66 66 66 66 66 66 20 29 7b 0a 20 20 20 20 20  fffffff ){.     
98420 20 20 20 73 71 6c 69 74 65 33 4f 73 4c 65 61 76     sqlite3OsLeav
98430 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 20 20  eMutex();.      
98440 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
98450 20 20 7d 0a 20 20 20 20 20 20 6b 65 79 49 6e 69    }.      keyIni
98460 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  t = 1;.    }.   
98470 20 73 71 6c 69 74 65 33 4f 73 4c 65 61 76 65 4d   sqlite3OsLeaveM
98480 75 74 65 78 28 29 3b 0a 20 20 7d 0a 20 20 70 54  utex();.  }.  pT
98490 73 64 20 3d 20 54 6c 73 47 65 74 56 61 6c 75 65  sd = TlsGetValue
984a0 28 6b 65 79 29 3b 0a 20 20 69 66 28 20 61 6c 6c  (key);.  if( all
984b0 6f 63 61 74 65 46 6c 61 67 3e 30 20 29 7b 0a 20  ocateFlag>0 ){. 
984c0 20 20 20 69 66 28 20 21 70 54 73 64 20 29 7b 0a     if( !pTsd ){.
984d0 20 20 20 20 20 20 70 54 73 64 20 3d 20 73 71 6c        pTsd = sql
984e0 69 74 65 33 4f 73 4d 61 6c 6c 6f 63 28 20 73 69  ite3OsMalloc( si
984f0 7a 65 6f 66 28 7a 65 72 6f 44 61 74 61 29 20 29  zeof(zeroData) )
98500 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 73 64  ;.      if( pTsd
98510 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 70 54 73   ){.        *pTs
98520 64 20 3d 20 7a 65 72 6f 44 61 74 61 3b 0a 20 20  d = zeroData;.  
98530 20 20 20 20 20 20 54 6c 73 53 65 74 56 61 6c 75        TlsSetValu
98540 65 28 6b 65 79 2c 20 70 54 73 64 29 3b 0a 20 20  e(key, pTsd);.  
98550 20 20 20 20 20 20 54 53 44 5f 43 4f 55 4e 54 45        TSD_COUNTE
98560 52 5f 49 4e 43 52 3b 0a 20 20 20 20 20 20 7d 0a  R_INCR;.      }.
98570 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66      }.  }else if
98580 28 20 70 54 73 64 21 3d 30 20 26 26 20 61 6c 6c  ( pTsd!=0 && all
98590 6f 63 61 74 65 46 6c 61 67 3c 30 20 0a 20 20 20  ocateFlag<0 .   
985a0 20 20 20 20 20 20 20 20 20 20 20 26 26 20 6d 65             && me
985b0 6d 63 6d 70 28 70 54 73 64 2c 20 26 7a 65 72 6f  mcmp(pTsd, &zero
985c0 44 61 74 61 2c 20 73 69 7a 65 6f 66 28 54 68 72  Data, sizeof(Thr
985d0 65 61 64 44 61 74 61 29 29 3d 3d 30 20 29 7b 0a  eadData))==0 ){.
985e0 20 20 20 20 73 71 6c 69 74 65 33 4f 73 46 72 65      sqlite3OsFre
985f0 65 28 70 54 73 64 29 3b 0a 20 20 20 20 54 6c 73  e(pTsd);.    Tls
98600 53 65 74 56 61 6c 75 65 28 6b 65 79 2c 20 30 29  SetValue(key, 0)
98610 3b 0a 20 20 20 20 54 53 44 5f 43 4f 55 4e 54 45  ;.    TSD_COUNTE
98620 52 5f 44 45 43 52 3b 0a 20 20 20 20 70 54 73 64  R_DECR;.    pTsd
98630 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 0;.  }.  retu
98640 72 6e 20 70 54 73 64 3b 0a 7d 0a 23 65 6e 64 69  rn pTsd;.}.#endi
98650 66 20 2f 2a 20 4f 53 5f 57 49 4e 20 2a 2f 0a 0a  f /* OS_WIN */..
98660 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
98670 45 6e 64 20 6f 66 20 6f 73 5f 77 69 6e 2e 63 20  End of os_win.c 
98680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
986a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
986b0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
986c0 42 65 67 69 6e 20 66 69 6c 65 20 70 61 67 65 72  Begin file pager
986d0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
986e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
986f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
98700 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
98710 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68  mber 15.**.** Th
98720 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
98730 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
98740 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
98750 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
98760 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
98770 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
98780 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
98790 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
987a0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
987b0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
987c0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
987d0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
987e0 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
987f0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
98800 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
98810 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
98820 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
98830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98870 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  ******.** This i
98880 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  s the implementa
98890 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 61 67 65  tion of the page
988a0 20 63 61 63 68 65 20 73 75 62 73 79 73 74 65 6d   cache subsystem
988b0 20 6f 72 20 22 70 61 67 65 72 22 2e 0a 2a 2a 20   or "pager"..** 
988c0 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 69 73  .** The pager is
988d0 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20   used to access 
988e0 61 20 64 61 74 61 62 61 73 65 20 64 69 73 6b 20  a database disk 
988f0 66 69 6c 65 2e 20 20 49 74 20 69 6d 70 6c 65 6d  file.  It implem
98900 65 6e 74 73 0a 2a 2a 20 61 74 6f 6d 69 63 20 63  ents.** atomic c
98910 6f 6d 6d 69 74 20 61 6e 64 20 72 6f 6c 6c 62 61  ommit and rollba
98920 63 6b 20 74 68 72 6f 75 67 68 20 74 68 65 20 75  ck through the u
98930 73 65 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20  se of a journal 
98940 66 69 6c 65 20 74 68 61 74 0a 2a 2a 20 69 73 20  file that.** is 
98950 73 65 70 61 72 61 74 65 20 66 72 6f 6d 20 74 68  separate from th
98960 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
98970 20 20 54 68 65 20 70 61 67 65 72 20 61 6c 73 6f    The pager also
98980 20 69 6d 70 6c 65 6d 65 6e 74 73 20 66 69 6c 65   implements file
98990 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 70  .** locking to p
989a0 72 65 76 65 6e 74 20 74 77 6f 20 70 72 6f 63 65  revent two proce
989b0 73 73 65 73 20 66 72 6f 6d 20 77 72 69 74 69 6e  sses from writin
989c0 67 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62  g the same datab
989d0 61 73 65 0a 2a 2a 20 66 69 6c 65 20 73 69 6d 75  ase.** file simu
989e0 6c 74 61 6e 65 6f 75 73 6c 79 2c 20 6f 72 20 6f  ltaneously, or o
989f0 6e 65 20 70 72 6f 63 65 73 73 20 66 72 6f 6d 20  ne process from 
98a00 72 65 61 64 69 6e 67 20 74 68 65 20 64 61 74 61  reading the data
98a10 62 61 73 65 20 77 68 69 6c 65 0a 2a 2a 20 61 6e  base while.** an
98a20 6f 74 68 65 72 20 69 73 20 77 72 69 74 69 6e 67  other is writing
98a30 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64  ..**.** @(#) $Id
98a40 3a 20 70 61 67 65 72 2e 63 2c 76 20 31 2e 33 35  : pager.c,v 1.35
98a50 31 20 32 30 30 37 2f 30 37 2f 32 30 20 30 30 3a  1 2007/07/20 00:
98a60 33 33 3a 33 36 20 64 72 68 20 45 78 70 20 24 0a  33:36 drh Exp $.
98a70 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
98a80 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a 0a 2f  E_OMIT_DISKIO../
98a90 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20  *.** Macros for 
98aa0 74 72 6f 75 62 6c 65 73 68 6f 6f 74 69 6e 67 2e  troubleshooting.
98ab0 20 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65    Normally turne
98ac0 64 20 6f 66 66 0a 2a 2f 0a 23 69 66 20 30 0a 23  d off.*/.#if 0.#
98ad0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 44 65  define sqlite3De
98ae0 62 75 67 50 72 69 6e 74 66 20 70 72 69 6e 74 66  bugPrintf printf
98af0 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 54 52  .#define PAGERTR
98b00 41 43 45 31 28 58 29 20 20 20 20 20 20 20 73 71  ACE1(X)       sq
98b10 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
98b20 28 58 29 0a 23 64 65 66 69 6e 65 20 50 41 47 45  (X).#define PAGE
98b30 52 54 52 41 43 45 32 28 58 2c 59 29 20 20 20 20  RTRACE2(X,Y)    
98b40 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
98b50 6e 74 66 28 58 2c 59 29 0a 23 64 65 66 69 6e 65  ntf(X,Y).#define
98b60 20 50 41 47 45 52 54 52 41 43 45 33 28 58 2c 59   PAGERTRACE3(X,Y
98b70 2c 5a 29 20 20 20 73 71 6c 69 74 65 33 44 65 62  ,Z)   sqlite3Deb
98b80 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 29 0a  ugPrintf(X,Y,Z).
98b90 23 64 65 66 69 6e 65 20 50 41 47 45 52 54 52 41  #define PAGERTRA
98ba0 43 45 34 28 58 2c 59 2c 5a 2c 57 29 20 73 71 6c  CE4(X,Y,Z,W) sql
98bb0 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
98bc0 58 2c 59 2c 5a 2c 57 29 0a 23 64 65 66 69 6e 65  X,Y,Z,W).#define
98bd0 20 50 41 47 45 52 54 52 41 43 45 35 28 58 2c 59   PAGERTRACE5(X,Y
98be0 2c 5a 2c 57 2c 56 29 20 73 71 6c 69 74 65 33 44  ,Z,W,V) sqlite3D
98bf0 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a  ebugPrintf(X,Y,Z
98c00 2c 57 2c 56 29 0a 23 65 6c 73 65 0a 23 64 65 66  ,W,V).#else.#def
98c10 69 6e 65 20 50 41 47 45 52 54 52 41 43 45 31 28  ine PAGERTRACE1(
98c20 58 29 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52  X).#define PAGER
98c30 54 52 41 43 45 32 28 58 2c 59 29 0a 23 64 65 66  TRACE2(X,Y).#def
98c40 69 6e 65 20 50 41 47 45 52 54 52 41 43 45 33 28  ine PAGERTRACE3(
98c50 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 50  X,Y,Z).#define P
98c60 41 47 45 52 54 52 41 43 45 34 28 58 2c 59 2c 5a  AGERTRACE4(X,Y,Z
98c70 2c 57 29 0a 23 64 65 66 69 6e 65 20 50 41 47 45  ,W).#define PAGE
98c80 52 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 57 2c  RTRACE5(X,Y,Z,W,
98c90 56 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  V).#endif../*.**
98ca0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74   The following t
98cb0 77 6f 20 6d 61 63 72 6f 73 20 61 72 65 20 75 73  wo macros are us
98cc0 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 50 41  ed within the PA
98cd0 47 45 52 54 52 41 43 45 58 28 29 20 6d 61 63 72  GERTRACEX() macr
98ce0 6f 73 20 61 62 6f 76 65 0a 2a 2a 20 74 6f 20 70  os above.** to p
98cf0 72 69 6e 74 20 6f 75 74 20 66 69 6c 65 2d 64 65  rint out file-de
98d00 73 63 72 69 70 74 6f 72 73 2e 20 0a 2a 2a 0a 2a  scriptors. .**.*
98d10 2a 20 50 41 47 45 52 49 44 28 29 20 74 61 6b 65  * PAGERID() take
98d20 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
98d30 20 50 61 67 65 72 20 73 74 72 75 63 74 20 61 73   Pager struct as
98d40 20 69 74 27 73 20 61 72 67 75 6d 65 6e 74 2e 20   it's argument. 
98d50 54 68 65 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65  The.** associate
98d60 64 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f  d file-descripto
98d70 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 46  r is returned. F
98d80 49 4c 45 48 41 4e 44 4c 45 49 44 28 29 20 74 61  ILEHANDLEID() ta
98d90 6b 65 73 20 61 6e 20 4f 73 46 69 6c 65 0a 2a 2a  kes an OsFile.**
98da0 20 73 74 72 75 63 74 20 61 73 20 69 74 27 73 20   struct as it's 
98db0 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 23 64 65  argument..*/.#de
98dc0 66 69 6e 65 20 50 41 47 45 52 49 44 28 70 29 20  fine PAGERID(p) 
98dd0 28 28 69 6e 74 29 28 70 2d 3e 66 64 29 29 0a 23  ((int)(p->fd)).#
98de0 64 65 66 69 6e 65 20 46 49 4c 45 48 41 4e 44 4c  define FILEHANDL
98df0 45 49 44 28 66 64 29 20 28 28 69 6e 74 29 66 64  EID(fd) ((int)fd
98e00 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 67  )../*.** The pag
98e10 65 20 63 61 63 68 65 20 61 73 20 61 20 77 68 6f  e cache as a who
98e20 6c 65 20 69 73 20 61 6c 77 61 79 73 20 69 6e 20  le is always in 
98e30 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  one of the follo
98e40 77 69 6e 67 0a 2a 2a 20 73 74 61 74 65 73 3a 0a  wing.** states:.
98e50 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f 55 4e  **.**   PAGER_UN
98e60 4c 4f 43 4b 20 20 20 20 20 20 20 20 54 68 65 20  LOCK        The 
98e70 70 61 67 65 20 63 61 63 68 65 20 69 73 20 6e 6f  page cache is no
98e80 74 20 63 75 72 72 65 6e 74 6c 79 20 72 65 61 64  t currently read
98e90 69 6e 67 20 6f 72 20 0a 2a 2a 20 20 20 20 20 20  ing or .**      
98ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
98eb0 20 77 72 69 74 69 6e 67 20 74 68 65 20 64 61 74   writing the dat
98ec0 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65  abase file.  The
98ed0 72 65 20 69 73 20 6e 6f 0a 2a 2a 20 20 20 20 20  re is no.**     
98ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
98ef0 20 20 64 61 74 61 20 68 65 6c 64 20 69 6e 20 6d    data held in m
98f00 65 6d 6f 72 79 2e 20 20 54 68 69 73 20 69 73 20  emory.  This is 
98f10 74 68 65 20 69 6e 69 74 69 61 6c 0a 2a 2a 20 20  the initial.**  
98f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
98f30 20 20 20 20 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a       state..**.*
98f40 2a 20 20 20 50 41 47 45 52 5f 53 48 41 52 45 44  *   PAGER_SHARED
98f50 20 20 20 20 20 20 20 20 54 68 65 20 70 61 67 65          The page
98f60 20 63 61 63 68 65 20 69 73 20 72 65 61 64 69 6e   cache is readin
98f70 67 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a  g the database..
98f80 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
98f90 20 20 20 20 20 20 20 20 20 57 72 69 74 69 6e 67           Writing
98fa0 20 69 73 20 6e 6f 74 20 70 65 72 6d 69 74 74 65   is not permitte
98fb0 64 2e 20 20 54 68 65 72 65 20 63 61 6e 20 62 65  d.  There can be
98fc0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
98fd0 20 20 20 20 20 20 20 20 20 20 6d 75 6c 74 69 70            multip
98fe0 6c 65 20 72 65 61 64 65 72 73 20 61 63 63 65 73  le readers acces
98ff0 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61  sing the same da
99000 74 61 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 20  tabase.**       
99010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
99020 66 69 6c 65 20 61 74 20 74 68 65 20 73 61 6d 65  file at the same
99030 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50   time..**.**   P
99040 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 20 20  AGER_RESERVED   
99050 20 20 20 54 68 69 73 20 70 72 6f 63 65 73 73 20     This process 
99060 68 61 73 20 72 65 73 65 72 76 65 64 20 74 68 65  has reserved the
99070 20 64 61 74 61 62 61 73 65 20 66 6f 72 20 77 72   database for wr
99080 69 74 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20  iting.**        
99090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
990a0 75 74 20 68 61 73 20 6e 6f 74 20 79 65 74 20 6d  ut has not yet m
990b0 61 64 65 20 61 6e 79 20 63 68 61 6e 67 65 73 2e  ade any changes.
990c0 20 20 4f 6e 6c 79 20 6f 6e 65 20 70 72 6f 63 65    Only one proce
990d0 73 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ss.**           
990e0 20 20 20 20 20 20 20 20 20 20 20 20 61 74 20 61              at a
990f0 20 74 69 6d 65 20 63 61 6e 20 72 65 73 65 72 76   time can reserv
99100 65 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  e the database. 
99110 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a   The original.**
99120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
99130 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65 20         database 
99140 66 69 6c 65 20 68 61 73 20 6e 6f 74 20 62 65 65  file has not bee
99150 6e 20 6d 6f 64 69 66 69 65 64 20 73 6f 20 6f 74  n modified so ot
99160 68 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  her.**          
99170 20 20 20 20 20 20 20 20 20 20 20 20 20 70 72 6f               pro
99180 63 65 73 73 65 73 20 6d 61 79 20 73 74 69 6c 6c  cesses may still
99190 20 62 65 20 72 65 61 64 69 6e 67 20 74 68 65 20   be reading the 
991a0 6f 6e 2d 64 69 73 6b 0a 2a 2a 20 20 20 20 20 20  on-disk.**      
991b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
991c0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a   database file..
991d0 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f 45 58  **.**   PAGER_EX
991e0 43 4c 55 53 49 56 45 20 20 20 20 20 54 68 65 20  CLUSIVE     The 
991f0 70 61 67 65 20 63 61 63 68 65 20 69 73 20 77 72  page cache is wr
99200 69 74 69 6e 67 20 74 68 65 20 64 61 74 61 62 61  iting the databa
99210 73 65 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  se..**          
99220 20 20 20 20 20 20 20 20 20 20 20 20 20 41 63 63               Acc
99230 65 73 73 20 69 73 20 65 78 63 6c 75 73 69 76 65  ess is exclusive
99240 2e 20 20 4e 6f 20 6f 74 68 65 72 20 70 72 6f 63  .  No other proc
99250 65 73 73 65 73 20 6f 72 0a 2a 2a 20 20 20 20 20  esses or.**     
99260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
99270 20 20 74 68 72 65 61 64 73 20 63 61 6e 20 62 65    threads can be
99280 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74   reading or writ
99290 69 6e 67 20 77 68 69 6c 65 20 6f 6e 65 0a 2a 2a  ing while one.**
992a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
992b0 20 20 20 20 20 20 20 70 72 6f 63 65 73 73 20 69         process i
992c0 73 20 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a  s writing..**.**
992d0 20 20 20 50 41 47 45 52 5f 53 59 4e 43 45 44 20     PAGER_SYNCED 
992e0 20 20 20 20 20 20 20 54 68 65 20 70 61 67 65 72         The pager
992f0 20 6d 6f 76 65 73 20 74 6f 20 74 68 69 73 20 73   moves to this s
99300 74 61 74 65 20 66 72 6f 6d 20 50 41 47 45 52 5f  tate from PAGER_
99310 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20  EXCLUSIVE.**    
99320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
99330 20 20 20 61 66 74 65 72 20 61 6c 6c 20 64 69 72     after all dir
99340 74 79 20 70 61 67 65 73 20 68 61 76 65 20 62 65  ty pages have be
99350 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68  en written to th
99360 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  e.**            
99370 20 20 20 20 20 20 20 20 20 20 20 64 61 74 61 62             datab
99380 61 73 65 20 66 69 6c 65 20 61 6e 64 20 74 68 65  ase file and the
99390 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 73   file has been s
993a0 79 6e 63 65 64 20 74 6f 0a 2a 2a 20 20 20 20 20  ynced to.**     
993b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
993c0 20 20 64 69 73 6b 2e 20 41 6c 6c 20 74 68 61 74    disk. All that
993d0 20 72 65 6d 61 69 6e 73 20 74 6f 20 64 6f 20 69   remains to do i
993e0 73 20 74 6f 20 72 65 6d 6f 76 65 20 6f 72 0a 2a  s to remove or.*
993f0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
99400 20 20 20 20 20 20 20 20 74 72 75 6e 63 61 74 65          truncate
99410 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
99420 65 20 61 6e 64 20 74 68 65 20 74 72 61 6e 73 61  e and the transa
99430 63 74 69 6f 6e 20 0a 2a 2a 20 20 20 20 20 20 20  ction .**       
99440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
99450 77 69 6c 6c 20 62 65 20 63 6f 6d 6d 69 74 74 65  will be committe
99460 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67  d..**.** The pag
99470 65 20 63 61 63 68 65 20 63 6f 6d 65 73 20 75 70  e cache comes up
99480 20 69 6e 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b   in PAGER_UNLOCK
99490 2e 20 20 54 68 65 20 66 69 72 73 74 20 74 69 6d  .  The first tim
994a0 65 20 61 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61  e a.** sqlite3Pa
994b0 67 65 72 47 65 74 28 29 20 6f 63 63 75 72 73 2c  gerGet() occurs,
994c0 20 74 68 65 20 73 74 61 74 65 20 74 72 61 6e 73   the state trans
994d0 69 74 69 6f 6e 73 20 74 6f 20 50 41 47 45 52 5f  itions to PAGER_
994e0 53 48 41 52 45 44 2e 0a 2a 2a 20 41 66 74 65 72  SHARED..** After
994f0 20 61 6c 6c 20 70 61 67 65 73 20 68 61 76 65 20   all pages have 
99500 62 65 65 6e 20 72 65 6c 65 61 73 65 64 20 75 73  been released us
99510 69 6e 67 20 73 71 6c 69 74 65 5f 70 61 67 65 5f  ing sqlite_page_
99520 75 6e 72 65 66 28 29 2c 0a 2a 2a 20 74 68 65 20  unref(),.** the 
99530 73 74 61 74 65 20 74 72 61 6e 73 69 74 69 6f 6e  state transition
99540 73 20 62 61 63 6b 20 74 6f 20 50 41 47 45 52 5f  s back to PAGER_
99550 55 4e 4c 4f 43 4b 2e 20 20 54 68 65 20 66 69 72  UNLOCK.  The fir
99560 73 74 20 74 69 6d 65 0a 2a 2a 20 74 68 61 74 20  st time.** that 
99570 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
99580 65 28 29 20 69 73 20 63 61 6c 6c 65 64 2c 20 74  e() is called, t
99590 68 65 20 73 74 61 74 65 20 74 72 61 6e 73 69 74  he state transit
995a0 69 6f 6e 73 20 74 6f 0a 2a 2a 20 50 41 47 45 52  ions to.** PAGER
995b0 5f 52 45 53 45 52 56 45 44 2e 20 20 28 4e 6f 74  _RESERVED.  (Not
995c0 65 20 74 68 61 74 20 73 71 6c 69 74 65 33 50 61  e that sqlite3Pa
995d0 67 65 72 57 72 69 74 65 28 29 20 63 61 6e 20 6f  gerWrite() can o
995e0 6e 6c 79 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64  nly be.** called
995f0 20 6f 6e 20 61 6e 20 6f 75 74 73 74 61 6e 64 69   on an outstandi
99600 6e 67 20 70 61 67 65 20 77 68 69 63 68 20 6d 65  ng page which me
99610 61 6e 73 20 74 68 61 74 20 74 68 65 20 70 61 67  ans that the pag
99620 65 72 20 6d 75 73 74 0a 2a 2a 20 62 65 20 69 6e  er must.** be in
99630 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 62 65   PAGER_SHARED be
99640 66 6f 72 65 20 69 74 20 74 72 61 6e 73 69 74 69  fore it transiti
99650 6f 6e 73 20 74 6f 20 50 41 47 45 52 5f 52 45 53  ons to PAGER_RES
99660 45 52 56 45 44 2e 29 0a 2a 2a 20 50 41 47 45 52  ERVED.).** PAGER
99670 5f 52 45 53 45 52 56 45 44 20 6d 65 61 6e 73 20  _RESERVED means 
99680 74 68 61 74 20 74 68 65 72 65 20 69 73 20 61 6e  that there is an
99690 20 6f 70 65 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a   open rollback j
996a0 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20 74  ournal..** The t
996b0 72 61 6e 73 69 74 69 6f 6e 20 74 6f 20 50 41 47  ransition to PAG
996c0 45 52 5f 45 58 43 4c 55 53 49 56 45 20 6f 63 63  ER_EXCLUSIVE occ
996d0 75 72 73 20 62 65 66 6f 72 65 20 61 6e 79 20 63  urs before any c
996e0 68 61 6e 67 65 73 0a 2a 2a 20 61 72 65 20 6d 61  hanges.** are ma
996f0 64 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  de to the databa
99700 73 65 20 66 69 6c 65 2c 20 74 68 6f 75 67 68 20  se file, though 
99710 77 72 69 74 65 73 20 74 6f 20 74 68 65 20 72 6f  writes to the ro
99720 6c 6c 62 61 63 6b 0a 2a 2a 20 6a 6f 75 72 6e 61  llback.** journa
99730 6c 20 6f 63 63 75 72 73 20 77 69 74 68 20 6a 75  l occurs with ju
99740 73 74 20 50 41 47 45 52 5f 52 45 53 45 52 56 45  st PAGER_RESERVE
99750 44 2e 20 20 41 66 74 65 72 20 61 6e 20 73 71 6c  D.  After an sql
99760 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63  ite3PagerRollbac
99770 6b 28 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65  k().** or sqlite
99780 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73  3PagerCommitPhas
99790 65 54 77 6f 28 29 2c 20 74 68 65 20 73 74 61 74  eTwo(), the stat
997a0 65 20 63 61 6e 20 67 6f 20 62 61 63 6b 20 74 6f  e can go back to
997b0 20 50 41 47 45 52 5f 53 48 41 52 45 44 2c 0a 2a   PAGER_SHARED,.*
997c0 2a 20 6f 72 20 69 74 20 63 61 6e 20 73 74 61 79  * or it can stay
997d0 20 61 74 20 50 41 47 45 52 5f 45 58 43 4c 55 53   at PAGER_EXCLUS
997e0 49 56 45 20 69 66 20 77 65 20 61 72 65 20 69 6e  IVE if we are in
997f0 20 65 78 63 6c 75 73 69 76 65 20 61 63 63 65 73   exclusive acces
99800 73 20 6d 6f 64 65 2e 0a 2a 2f 0a 23 64 65 66 69  s mode..*/.#defi
99810 6e 65 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20  ne PAGER_UNLOCK 
99820 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 50       0.#define P
99830 41 47 45 52 5f 53 48 41 52 45 44 20 20 20 20 20  AGER_SHARED     
99840 20 31 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20   1   /* same as 
99850 53 48 41 52 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 23  SHARED_LOCK */.#
99860 64 65 66 69 6e 65 20 50 41 47 45 52 5f 52 45 53  define PAGER_RES
99870 45 52 56 45 44 20 20 20 20 32 20 20 20 2f 2a 20  ERVED    2   /* 
99880 73 61 6d 65 20 61 73 20 52 45 53 45 52 56 45 44  same as RESERVED
99890 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 65 66 69 6e 65  _LOCK */.#define
998a0 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45   PAGER_EXCLUSIVE
998b0 20 20 20 34 20 20 20 2f 2a 20 73 61 6d 65 20 61     4   /* same a
998c0 73 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  s EXCLUSIVE_LOCK
998d0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45   */.#define PAGE
998e0 52 5f 53 59 4e 43 45 44 20 20 20 20 20 20 35 0a  R_SYNCED      5.
998f0 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51  ./*.** If the SQ
99900 4c 49 54 45 5f 42 55 53 59 5f 52 45 53 45 52 56  LITE_BUSY_RESERV
99910 45 44 5f 4c 4f 43 4b 20 6d 61 63 72 6f 20 69 73  ED_LOCK macro is
99920 20 73 65 74 20 74 6f 20 74 72 75 65 20 61 74 20   set to true at 
99930 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2c 0a 2a 2a  compile-time,.**
99940 20 74 68 65 6e 20 66 61 69 6c 65 64 20 61 74 74   then failed att
99950 65 6d 70 74 73 20 74 6f 20 67 65 74 20 61 20 72  empts to get a r
99960 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 77 69 6c  eserved lock wil
99970 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73  l invoke the bus
99980 79 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 54  y callback..** T
99990 68 69 73 20 69 73 20 6f 66 66 20 62 79 20 64 65  his is off by de
999a0 66 61 75 6c 74 2e 20 20 54 6f 20 73 65 65 20 77  fault.  To see w
999b0 68 79 2c 20 63 6f 6e 73 69 64 65 72 20 74 68 65  hy, consider the
999c0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 63 65 6e 61   following scena
999d0 72 69 6f 3a 0a 2a 2a 20 0a 2a 2a 20 53 75 70 70  rio:.** .** Supp
999e0 6f 73 65 20 74 68 72 65 61 64 20 41 20 61 6c 72  ose thread A alr
999f0 65 61 64 79 20 68 61 73 20 61 20 73 68 61 72 65  eady has a share
99a00 64 20 6c 6f 63 6b 20 61 6e 64 20 77 61 6e 74 73  d lock and wants
99a10 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b   a reserved lock
99a20 2e 0a 2a 2a 20 54 68 72 65 61 64 20 42 20 61 6c  ..** Thread B al
99a30 72 65 61 64 79 20 68 61 73 20 61 20 72 65 73 65  ready has a rese
99a40 72 76 65 64 20 6c 6f 63 6b 20 61 6e 64 20 77 61  rved lock and wa
99a50 6e 74 73 20 61 6e 20 65 78 63 6c 75 73 69 76 65  nts an exclusive
99a60 20 6c 6f 63 6b 2e 20 20 49 66 0a 2a 2a 20 62 6f   lock.  If.** bo
99a70 74 68 20 74 68 72 65 61 64 73 20 61 72 65 20 75  th threads are u
99a80 73 69 6e 67 20 74 68 65 69 72 20 62 75 73 79 20  sing their busy 
99a90 63 61 6c 6c 62 61 63 6b 73 2c 20 69 74 20 6d 69  callbacks, it mi
99aa0 67 68 74 20 62 65 20 61 20 6c 6f 6e 67 20 74 69  ght be a long ti
99ab0 6d 65 0a 2a 2a 20 62 65 20 66 6f 72 20 6f 6e 65  me.** be for one
99ac0 20 6f 66 20 74 68 65 20 74 68 72 65 61 64 73 20   of the threads 
99ad0 67 69 76 65 20 75 70 20 61 6e 64 20 61 6c 6c 6f  give up and allo
99ae0 77 73 20 74 68 65 20 6f 74 68 65 72 20 74 6f 20  ws the other to 
99af0 70 72 6f 63 65 65 64 2e 0a 2a 2a 20 42 75 74 20  proceed..** But 
99b00 69 66 20 74 68 65 20 74 68 72 65 61 64 20 74 72  if the thread tr
99b10 79 69 6e 67 20 74 6f 20 67 65 74 20 74 68 65 20  ying to get the 
99b20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 67 69  reserved lock gi
99b30 76 65 73 20 75 70 20 71 75 69 63 6b 6c 79 0a 2a  ves up quickly.*
99b40 2a 20 28 69 66 20 69 74 20 6e 65 76 65 72 20 69  * (if it never i
99b50 6e 76 6f 6b 65 73 20 69 74 73 20 62 75 73 79 20  nvokes its busy 
99b60 63 61 6c 6c 62 61 63 6b 29 20 74 68 65 6e 20 74  callback) then t
99b70 68 65 20 63 6f 6e 74 65 6e 74 69 6f 6e 20 77 69  he contention wi
99b80 6c 6c 20 62 65 0a 2a 2a 20 72 65 73 6f 6c 76 65  ll be.** resolve
99b90 64 20 71 75 69 63 6b 6c 79 2e 0a 2a 2f 0a 23 69  d quickly..*/.#i
99ba0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 42 55 53  fndef SQLITE_BUS
99bb0 59 5f 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a  Y_RESERVED_LOCK.
99bc0 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
99bd0 42 55 53 59 5f 52 45 53 45 52 56 45 44 5f 4c 4f  BUSY_RESERVED_LO
99be0 43 4b 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  CK 0.#endif../*.
99bf0 2a 2a 20 54 68 69 73 20 6d 61 63 72 6f 20 72 6f  ** This macro ro
99c00 75 6e 64 73 20 76 61 6c 75 65 73 20 75 70 20 73  unds values up s
99c10 6f 20 74 68 61 74 20 69 66 20 74 68 65 20 76 61  o that if the va
99c20 6c 75 65 20 69 73 20 61 6e 20 61 64 64 72 65 73  lue is an addres
99c30 73 20 69 74 0a 2a 2a 20 69 73 20 67 75 61 72 61  s it.** is guara
99c40 6e 74 65 65 64 20 74 6f 20 62 65 20 61 6e 20 61  nteed to be an a
99c50 64 64 72 65 73 73 20 74 68 61 74 20 69 73 20 61  ddress that is a
99c60 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 38 2d 62  ligned to an 8-b
99c70 79 74 65 20 62 6f 75 6e 64 61 72 79 2e 0a 2a 2f  yte boundary..*/
99c80 0a 23 64 65 66 69 6e 65 20 46 4f 52 43 45 5f 41  .#define FORCE_A
99c90 4c 49 47 4e 4d 45 4e 54 28 58 29 20 20 20 28 28  LIGNMENT(X)   ((
99ca0 28 58 29 2b 37 29 26 7e 37 29 0a 0a 2f 2a 0a 2a  (X)+7)&~7)../*.*
99cb0 2a 20 45 61 63 68 20 69 6e 2d 6d 65 6d 6f 72 79  * Each in-memory
99cc0 20 69 6d 61 67 65 20 6f 66 20 61 20 70 61 67 65   image of a page
99cd0 20 62 65 67 69 6e 73 20 77 69 74 68 20 74 68 65   begins with the
99ce0 20 66 6f 6c 6c 6f 77 69 6e 67 20 68 65 61 64 65   following heade
99cf0 72 2e 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65  r..** This heade
99d00 72 20 69 73 20 6f 6e 6c 79 20 76 69 73 69 62 6c  r is only visibl
99d10 65 20 74 6f 20 74 68 69 73 20 70 61 67 65 72 20  e to this pager 
99d20 6d 6f 64 75 6c 65 2e 20 20 54 68 65 20 63 6c 69  module.  The cli
99d30 65 6e 74 0a 2a 2a 20 63 6f 64 65 20 74 68 61 74  ent.** code that
99d40 20 63 61 6c 6c 73 20 70 61 67 65 72 20 73 65 65   calls pager see
99d50 73 20 6f 6e 6c 79 20 74 68 65 20 64 61 74 61 20  s only the data 
99d60 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20 74 68 65  that follows the
99d70 20 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 43   header..**.** C
99d80 6c 69 65 6e 74 20 63 6f 64 65 20 73 68 6f 75 6c  lient code shoul
99d90 64 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 50 61  d call sqlite3Pa
99da0 67 65 72 57 72 69 74 65 28 29 20 6f 6e 20 61 20  gerWrite() on a 
99db0 70 61 67 65 20 70 72 69 6f 72 20 74 6f 20 6d 61  page prior to ma
99dc0 6b 69 6e 67 0a 2a 2a 20 61 6e 79 20 6d 6f 64 69  king.** any modi
99dd0 66 69 63 61 74 69 6f 6e 73 20 74 6f 20 74 68 61  fications to tha
99de0 74 20 70 61 67 65 2e 20 20 54 68 65 20 66 69 72  t page.  The fir
99df0 73 74 20 74 69 6d 65 20 73 71 6c 69 74 65 33 50  st time sqlite3P
99e00 61 67 65 72 57 72 69 74 65 28 29 0a 2a 2a 20 69  agerWrite().** i
99e10 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 6f 72  s called, the or
99e20 69 67 69 6e 61 6c 20 70 61 67 65 20 63 6f 6e 74  iginal page cont
99e30 65 6e 74 73 20 61 72 65 20 77 72 69 74 74 65 6e  ents are written
99e40 20 69 6e 74 6f 20 74 68 65 20 72 6f 6c 6c 62 61   into the rollba
99e50 63 6b 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 61 6e  ck.** journal an
99e60 64 20 50 67 48 64 72 2e 69 6e 4a 6f 75 72 6e 61  d PgHdr.inJourna
99e70 6c 20 61 6e 64 20 50 67 48 64 72 2e 6e 65 65 64  l and PgHdr.need
99e80 53 79 6e 63 20 61 72 65 20 73 65 74 2e 20 20 4c  Sync are set.  L
99e90 61 74 65 72 2c 20 6f 6e 63 65 0a 2a 2a 20 74 68  ater, once.** th
99ea0 65 20 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20 68  e journal page h
99eb0 61 73 20 6d 61 64 65 20 69 74 20 6f 6e 74 6f 20  as made it onto 
99ec0 74 68 65 20 64 69 73 6b 20 73 75 72 66 61 63 65  the disk surface
99ed0 2c 20 50 67 48 64 72 2e 6e 65 65 64 53 79 6e 63  , PgHdr.needSync
99ee0 0a 2a 2a 20 69 73 20 63 6c 65 61 72 65 64 2e 20  .** is cleared. 
99ef0 20 54 68 65 20 6d 6f 64 69 66 69 65 64 20 70 61   The modified pa
99f00 67 65 20 63 61 6e 6e 6f 74 20 62 65 20 77 72 69  ge cannot be wri
99f10 74 74 65 6e 20 62 61 63 6b 20 69 6e 74 6f 20 74  tten back into t
99f20 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 64  he original.** d
99f30 61 74 61 62 61 73 65 20 66 69 6c 65 20 75 6e 74  atabase file unt
99f40 69 6c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 70  il the journal p
99f50 61 67 65 73 20 68 61 73 20 62 65 65 6e 20 73 79  ages has been sy
99f60 6e 63 65 64 20 74 6f 20 64 69 73 6b 20 61 6e 64  nced to disk and
99f70 20 74 68 65 0a 2a 2a 20 50 67 48 64 72 2e 6e 65   the.** PgHdr.ne
99f80 65 64 53 79 6e 63 20 68 61 73 20 62 65 65 6e 20  edSync has been 
99f90 63 6c 65 61 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  cleared..**.** T
99fa0 68 65 20 50 67 48 64 72 2e 64 69 72 74 79 20 66  he PgHdr.dirty f
99fb0 6c 61 67 20 69 73 20 73 65 74 20 77 68 65 6e 20  lag is set when 
99fc0 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
99fd0 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20 61 6e  e() is called an
99fe0 64 0a 2a 2a 20 69 73 20 63 6c 65 61 72 65 64 20  d.** is cleared 
99ff0 61 67 61 69 6e 20 77 68 65 6e 20 74 68 65 20 70  again when the p
9a000 61 67 65 20 63 6f 6e 74 65 6e 74 20 69 73 20 77  age content is w
9a010 72 69 74 74 65 6e 20 62 61 63 6b 20 74 6f 20 74  ritten back to t
9a020 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 64  he original.** d
9a030 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a  atabase file..**
9a040 0a 2a 2a 20 44 65 74 61 69 6c 73 20 6f 66 20 69  .** Details of i
9a050 6d 70 6f 72 74 61 6e 74 20 73 74 72 75 63 74 75  mportant structu
9a060 72 65 20 65 6c 65 6d 65 6e 74 73 3a 0a 2a 2a 0a  re elements:.**.
9a070 2a 2a 20 6e 65 65 64 53 79 6e 63 0a 2a 2a 0a 2a  ** needSync.**.*
9a080 2a 20 20 20 20 20 49 66 20 74 68 69 73 20 69 73  *     If this is
9a090 20 74 72 75 65 2c 20 74 68 69 73 20 6d 65 61 6e   true, this mean
9a0a0 73 20 74 68 61 74 20 69 74 20 69 73 20 6e 6f 74  s that it is not
9a0b0 20 73 61 66 65 20 74 6f 20 77 72 69 74 65 20 74   safe to write t
9a0c0 68 65 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 63  he page.**     c
9a0d0 6f 6e 74 65 6e 74 20 74 6f 20 74 68 65 20 64 61  ontent to the da
9a0e0 74 61 62 61 73 65 20 62 65 63 61 75 73 65 20 74  tabase because t
9a0f0 68 65 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6e 74  he original cont
9a100 65 6e 74 20 6e 65 65 64 65 64 0a 2a 2a 20 20 20  ent needed.**   
9a110 20 20 66 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 68    for rollback h
9a120 61 73 20 6e 6f 74 20 62 79 20 73 79 6e 63 65 64  as not by synced
9a130 20 74 6f 20 74 68 65 20 6d 61 69 6e 20 72 6f 6c   to the main rol
9a140 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 2a  lback journal..*
9a150 2a 20 20 20 20 20 54 68 65 20 6f 72 69 67 69 6e  *     The origin
9a160 61 6c 20 63 6f 6e 74 65 6e 74 20 6d 61 79 20 68  al content may h
9a170 61 76 65 20 62 65 65 6e 20 77 72 69 74 74 65 6e  ave been written
9a180 20 74 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b   to the rollback
9a190 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20   journal.**     
9a1a0 62 75 74 20 69 74 20 68 61 73 20 6e 6f 74 20 79  but it has not y
9a1b0 65 74 20 62 65 65 6e 20 73 79 6e 63 65 64 2e 20  et been synced. 
9a1c0 20 53 6f 20 77 65 20 63 61 6e 6e 6f 74 20 77 72   So we cannot wr
9a1d0 69 74 65 20 74 6f 20 74 68 65 20 64 61 74 61 62  ite to the datab
9a1e0 61 73 65 0a 2a 2a 20 20 20 20 20 66 69 6c 65 20  ase.**     file 
9a1f0 62 65 63 61 75 73 65 20 70 6f 77 65 72 20 66 61  because power fa
9a200 69 6c 75 72 65 20 6d 69 67 68 74 20 63 61 75 73  ilure might caus
9a210 65 20 74 68 65 20 70 61 67 65 20 69 6e 20 74 68  e the page in th
9a220 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a  e journal file.*
9a230 2a 20 20 20 20 20 74 6f 20 6e 65 76 65 72 20 72  *     to never r
9a240 65 61 63 68 20 74 68 65 20 64 69 73 6b 2e 20 20  each the disk.  
9a250 49 74 20 69 73 20 61 73 20 69 66 20 74 68 65 20  It is as if the 
9a260 77 72 69 74 65 20 74 6f 20 74 68 65 20 6a 6f 75  write to the jou
9a270 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 20 20 20  rnal file.**    
9a280 20 64 6f 65 73 20 6e 6f 74 20 6f 63 63 75 72 20   does not occur 
9a290 75 6e 74 69 6c 20 74 68 65 20 6a 6f 75 72 6e 61  until the journa
9a2a0 6c 20 66 69 6c 65 20 69 73 20 73 79 6e 63 65 64  l file is synced
9a2b0 2e 0a 2a 2a 20 20 20 20 20 0a 2a 2a 20 20 20 20  ..**     .**    
9a2c0 20 54 68 69 73 20 66 6c 61 67 20 69 73 20 66 61   This flag is fa
9a2d0 6c 73 65 20 69 66 20 74 68 65 20 70 61 67 65 20  lse if the page 
9a2e0 63 6f 6e 74 65 6e 74 20 65 78 61 63 74 6c 79 20  content exactly 
9a2f0 6d 61 74 63 68 65 73 20 77 68 61 74 0a 2a 2a 20  matches what.** 
9a300 20 20 20 20 63 75 72 72 65 6e 74 6c 79 20 65 78      currently ex
9a310 69 73 74 73 20 69 6e 20 74 68 65 20 64 61 74 61  ists in the data
9a320 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20  base file.  The 
9a330 6e 65 65 64 53 79 6e 63 20 66 6c 61 67 20 69 73  needSync flag is
9a340 20 61 6c 73 6f 0a 2a 2a 20 20 20 20 20 66 61 6c   also.**     fal
9a350 73 65 20 69 66 20 74 68 65 20 6f 72 69 67 69 6e  se if the origin
9a360 61 6c 20 63 6f 6e 74 65 6e 74 20 68 61 73 20 62  al content has b
9a370 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74  een written to t
9a380 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b  he main rollback
9a390 0a 2a 2a 20 20 20 20 20 6a 6f 75 72 6e 61 6c 20  .**     journal 
9a3a0 61 6e 64 20 73 79 6e 63 65 64 2e 20 20 49 66 20  and synced.  If 
9a3b0 74 68 65 20 70 61 67 65 20 72 65 70 72 65 73 65  the page represe
9a3c0 6e 74 73 20 61 20 6e 65 77 20 70 61 67 65 20 74  nts a new page t
9a3d0 68 61 74 20 68 61 73 0a 2a 2a 20 20 20 20 20 62  hat has.**     b
9a3e0 65 65 6e 20 61 64 64 65 64 20 6f 6e 74 6f 20 74  een added onto t
9a3f0 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 64 61  he end of the da
9a400 74 61 62 61 73 65 20 64 75 72 69 6e 67 20 74 68  tabase during th
9a410 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 20 20 20  e current.**    
9a420 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68   transaction, th
9a430 65 20 6e 65 65 64 53 79 6e 63 20 66 6c 61 67 20  e needSync flag 
9a440 69 73 20 74 72 75 65 20 75 6e 74 69 6c 20 74 68  is true until th
9a450 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 62  e original datab
9a460 61 73 65 0a 2a 2a 20 20 20 20 20 73 69 7a 65 20  ase.**     size 
9a470 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68  in the journal h
9a480 65 61 64 65 72 20 68 61 73 20 62 65 65 6e 20 73  eader has been s
9a490 79 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a  ynced to disk..*
9a4a0 2a 0a 2a 2a 20 69 6e 4a 6f 75 72 6e 61 6c 0a 2a  *.** inJournal.*
9a4b0 2a 0a 2a 2a 20 20 20 20 20 54 68 69 73 20 69 73  *.**     This is
9a4c0 20 74 72 75 65 20 69 66 20 74 68 65 20 6f 72 69   true if the ori
9a4d0 67 69 6e 61 6c 20 70 61 67 65 20 68 61 73 20 62  ginal page has b
9a4e0 65 65 6e 20 77 72 69 74 74 65 6e 20 69 6e 74 6f  een written into
9a4f0 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20 20 20 20   the main.**    
9a500 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61   rollback journa
9a510 6c 2e 20 20 54 68 69 73 20 69 73 20 61 6c 77 61  l.  This is alwa
9a520 79 73 20 66 61 6c 73 65 20 66 6f 72 20 6e 65 77  ys false for new
9a530 20 70 61 67 65 73 20 61 64 64 65 64 20 74 6f 0a   pages added to.
9a540 2a 2a 20 20 20 20 20 74 68 65 20 65 6e 64 20 6f  **     the end o
9a550 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  f the database f
9a560 69 6c 65 20 64 75 72 69 6e 67 20 74 68 65 20 63  ile during the c
9a570 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69  urrent transacti
9a580 6f 6e 2e 0a 2a 2a 20 20 20 20 20 41 6e 64 20 74  on..**     And t
9a590 68 69 73 20 66 6c 61 67 20 73 61 79 73 20 6e 6f  his flag says no
9a5a0 74 68 69 6e 67 20 61 62 6f 75 74 20 77 68 65 74  thing about whet
9a5b0 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 6a  her or not the j
9a5c0 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 68 61  ournal.**     ha
9a5d0 73 20 62 65 65 6e 20 73 79 6e 63 65 64 20 74 6f  s been synced to
9a5e0 20 64 69 73 6b 2e 20 20 46 6f 72 20 70 61 67 65   disk.  For page
9a5f0 73 20 74 68 61 74 20 61 72 65 20 69 6e 20 74 68  s that are in th
9a600 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 20 20  e original.**   
9a610 20 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c    database file,
9a620 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65   the following e
9a630 78 70 72 65 73 73 69 6f 6e 20 73 68 6f 75 6c 64  xpression should
9a640 20 61 6c 77 61 79 73 20 62 65 20 74 72 75 65 3a   always be true:
9a650 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 69 6e 4a  .**.**       inJ
9a660 6f 75 72 6e 61 6c 20 3d 20 28 70 50 61 67 65 72  ournal = (pPager
9a670 2d 3e 61 49 6e 4a 6f 75 72 6e 61 6c 5b 28 70 67  ->aInJournal[(pg
9a680 6e 6f 2d 31 29 2f 38 5d 20 26 20 28 31 3c 3c 28  no-1)/8] & (1<<(
9a690 28 70 67 6e 6f 2d 31 29 25 38 29 29 21 3d 30 0a  (pgno-1)%8))!=0.
9a6a0 2a 2a 0a 2a 2a 20 20 20 20 20 54 68 65 20 70 50  **.**     The pP
9a6b0 61 67 65 72 2d 3e 61 49 6e 4a 6f 75 72 6e 61 6c  ager->aInJournal
9a6c0 5b 5d 20 61 72 72 61 79 20 69 73 20 6f 6e 6c 79  [] array is only
9a6d0 20 76 61 6c 69 64 20 66 6f 72 20 74 68 65 20 6f   valid for the o
9a6e0 72 69 67 69 6e 61 6c 0a 2a 2a 20 20 20 20 20 70  riginal.**     p
9a6f0 61 67 65 73 20 6f 66 20 74 68 65 20 64 61 74 61  ages of the data
9a700 62 61 73 65 2c 20 6e 6f 74 20 6e 65 77 20 70 61  base, not new pa
9a710 67 65 73 20 74 68 61 74 20 61 72 65 20 61 64 64  ges that are add
9a720 65 64 20 74 6f 20 74 68 65 20 65 6e 64 0a 2a 2a  ed to the end.**
9a730 20 20 20 20 20 6f 66 20 74 68 65 20 64 61 74 61       of the data
9a740 62 61 73 65 2c 20 73 6f 20 6f 62 76 69 6f 75 73  base, so obvious
9a750 6c 79 20 74 68 65 20 61 62 6f 76 65 20 65 78 70  ly the above exp
9a760 72 65 73 73 69 6f 6e 20 63 61 6e 6e 6f 74 20 62  ression cannot b
9a770 65 0a 2a 2a 20 20 20 20 20 76 61 6c 69 64 20 66  e.**     valid f
9a780 6f 72 20 6e 65 77 20 70 61 67 65 73 2e 20 20 46  or new pages.  F
9a790 6f 72 20 6e 65 77 20 70 61 67 65 73 20 69 6e 4a  or new pages inJ
9a7a0 6f 75 72 6e 61 6c 20 69 73 20 61 6c 77 61 79 73  ournal is always
9a7b0 20 30 2e 0a 2a 2a 0a 2a 2a 20 64 69 72 74 79 0a   0..**.** dirty.
9a7c0 2a 2a 0a 2a 2a 20 20 20 20 20 57 68 65 6e 20 74  **.**     When t
9a7d0 72 75 65 2c 20 74 68 69 73 20 6d 65 61 6e 73 20  rue, this means 
9a7e0 74 68 61 74 20 74 68 65 20 63 6f 6e 74 65 6e 74  that the content
9a7f0 20 6f 66 20 74 68 65 20 70 61 67 65 20 68 61 73   of the page has
9a800 20 62 65 65 6e 0a 2a 2a 20 20 20 20 20 6d 6f 64   been.**     mod
9a810 69 66 69 65 64 20 61 6e 64 20 6e 65 65 64 73 20  ified and needs 
9a820 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 62 61  to be written ba
9a830 63 6b 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ck to the databa
9a840 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 20 20 20 20  se file..**     
9a850 49 66 20 66 61 6c 73 65 2c 20 69 74 20 6d 65 61  If false, it mea
9a860 6e 73 20 74 68 61 74 20 65 69 74 68 65 72 20 74  ns that either t
9a870 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68  he content of th
9a880 65 20 70 61 67 65 20 69 73 0a 2a 2a 20 20 20 20  e page is.**    
9a890 20 75 6e 63 68 61 6e 67 65 64 20 6f 72 20 65 6c   unchanged or el
9a8a0 73 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 69  se the content i
9a8b0 73 20 75 6e 69 6d 70 6f 72 74 61 6e 74 20 61 6e  s unimportant an
9a8c0 64 20 77 65 20 64 6f 20 6e 6f 74 0a 2a 2a 20 20  d we do not.**  
9a8d0 20 20 20 63 61 72 65 20 77 68 65 74 68 65 72 20     care whether 
9a8e0 6f 72 20 6e 6f 74 20 69 74 20 69 73 20 70 72 65  or not it is pre
9a8f0 73 65 72 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 61 6c  served..**.** al
9a900 77 61 79 73 52 6f 6c 6c 62 61 63 6b 0a 2a 2a 0a  waysRollback.**.
9a910 2a 2a 20 20 20 20 20 54 68 69 73 20 6d 65 61 6e  **     This mean
9a920 73 20 74 68 61 74 20 74 68 65 20 73 71 6c 69 74  s that the sqlit
9a930 65 33 50 61 67 65 72 44 6f 6e 74 52 6f 6c 6c 62  e3PagerDontRollb
9a940 61 63 6b 28 29 20 41 50 49 20 73 68 6f 75 6c 64  ack() API should
9a950 20 62 65 0a 2a 2a 20 20 20 20 20 69 67 6e 6f 72   be.**     ignor
9a960 65 64 20 66 6f 72 20 74 68 69 73 20 70 61 67 65  ed for this page
9a970 2e 20 20 54 68 65 20 44 6f 6e 74 52 6f 6c 6c 62  .  The DontRollb
9a980 61 63 6b 28 29 20 41 50 49 20 61 74 74 65 6d 70  ack() API attemp
9a990 74 73 20 74 6f 20 73 61 79 0a 2a 2a 20 20 20 20  ts to say.**    
9a9a0 20 74 68 61 74 20 74 68 65 20 63 6f 6e 74 65 6e   that the conten
9a9b0 74 20 6f 66 20 74 68 65 20 70 61 67 65 20 6f 6e  t of the page on
9a9c0 20 64 69 73 6b 20 69 73 20 75 6e 69 6d 70 6f 72   disk is unimpor
9a9d0 74 61 6e 74 20 28 69 74 20 69 73 20 61 6e 0a 2a  tant (it is an.*
9a9e0 2a 20 20 20 20 20 75 6e 75 73 65 64 20 70 61 67  *     unused pag
9a9f0 65 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 73  e on the freelis
9aa00 74 29 20 73 6f 20 74 68 61 74 20 69 74 20 69 73  t) so that it is
9aa10 20 75 6e 6e 65 63 65 73 73 61 72 79 20 74 6f 20   unnecessary to 
9aa20 0a 2a 2a 20 20 20 20 20 72 6f 6c 6c 62 61 63 6b  .**     rollback
9aa30 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 69 73   changes to this
9aa40 20 70 61 67 65 20 62 65 63 61 75 73 65 20 74 68   page because th
9aa50 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65  e content of the
9aa60 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 63 61 6e   page.**     can
9aa70 20 63 68 61 6e 67 65 20 77 69 74 68 6f 75 74 20   change without 
9aa80 63 68 61 6e 67 69 6e 67 20 74 68 65 20 6d 65 61  changing the mea
9aa90 6e 69 6e 67 20 6f 66 20 74 68 65 20 64 61 74 61  ning of the data
9aaa0 62 61 73 65 2e 20 20 54 68 69 73 0a 2a 2a 20 20  base.  This.**  
9aab0 20 20 20 66 6c 61 67 20 6f 76 65 72 72 69 64 65     flag override
9aac0 73 20 61 6e 79 20 44 6f 6e 74 52 6f 6c 6c 62 61  s any DontRollba
9aad0 63 6b 28 29 20 61 74 74 65 6d 70 74 2e 20 20 54  ck() attempt.  T
9aae0 68 69 73 20 66 6c 61 67 20 69 73 20 73 65 74 0a  his flag is set.
9aaf0 2a 2a 20 20 20 20 20 77 68 65 6e 20 61 20 70 61  **     when a pa
9ab00 67 65 20 74 68 61 74 20 6f 72 69 67 69 6e 61 6c  ge that original
9ab10 6c 79 20 63 6f 6e 74 61 69 6e 65 64 20 76 61 6c  ly contained val
9ab20 69 64 20 64 61 74 61 20 69 73 20 61 64 64 65 64  id data is added
9ab30 20 74 6f 0a 2a 2a 20 20 20 20 20 74 68 65 20 66   to.**     the f
9ab40 72 65 65 6c 69 73 74 2e 20 20 4c 61 74 65 72 20  reelist.  Later 
9ab50 69 6e 20 74 68 65 20 73 61 6d 65 20 74 72 61 6e  in the same tran
9ab60 73 61 63 74 69 6f 6e 2c 20 74 68 69 73 20 70 61  saction, this pa
9ab70 67 65 20 6d 69 67 68 74 0a 2a 2a 20 20 20 20 20  ge might.**     
9ab80 62 65 20 70 75 6c 6c 65 64 20 66 72 6f 6d 20 74  be pulled from t
9ab90 68 65 20 66 72 65 65 6c 69 73 74 20 61 6e 64 20  he freelist and 
9aba0 72 65 75 73 65 64 20 66 6f 72 20 73 6f 6d 65 74  reused for somet
9abb0 68 69 6e 67 20 64 69 66 66 65 72 65 6e 74 0a 2a  hing different.*
9abc0 2a 20 20 20 20 20 61 6e 64 20 61 74 20 74 68 61  *     and at tha
9abd0 74 20 70 6f 69 6e 74 20 74 68 65 20 44 6f 6e 74  t point the Dont
9abe0 52 6f 6c 6c 62 61 63 6b 28 29 20 41 50 49 20 77  Rollback() API w
9abf0 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 62 65  ill be called be
9ac00 63 61 75 73 65 0a 2a 2a 20 20 20 20 20 70 61 67  cause.**     pag
9ac10 65 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74 68  es taken from th
9ac20 65 20 66 72 65 65 6c 69 73 74 20 64 6f 20 6e 6f  e freelist do no
9ac30 74 20 6e 65 65 64 20 74 6f 20 62 65 20 70 72 6f  t need to be pro
9ac40 74 65 63 74 65 64 20 62 79 0a 2a 2a 20 20 20 20  tected by.**    
9ac50 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f   the rollback jo
9ac60 75 72 6e 61 6c 2e 20 20 42 75 74 20 74 68 69 73  urnal.  But this
9ac70 20 66 6c 61 67 20 73 61 79 73 20 74 68 61 74 20   flag says that 
9ac80 74 68 65 20 70 61 67 65 20 77 61 73 0a 2a 2a 20  the page was.** 
9ac90 20 20 20 20 6e 6f 74 20 6f 72 69 67 69 6e 61 6c      not original
9aca0 6c 79 20 70 61 72 74 20 6f 66 20 74 68 65 20 66  ly part of the f
9acb0 72 65 65 6c 69 73 74 20 73 6f 20 74 68 61 74 20  reelist so that 
9acc0 69 74 20 73 74 69 6c 6c 20 6e 65 65 64 73 20 74  it still needs t
9acd0 6f 0a 2a 2a 20 20 20 20 20 62 65 20 72 6f 6c 6c  o.**     be roll
9ace0 65 64 20 62 61 63 6b 20 69 6e 20 73 70 69 74 65  ed back in spite
9acf0 20 6f 66 20 61 6e 79 20 73 75 62 73 65 71 75 65   of any subseque
9ad00 6e 74 20 44 6f 6e 74 52 6f 6c 6c 62 61 63 6b 28  nt DontRollback(
9ad10 29 20 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 6e  ) calls..**.** n
9ad20 65 65 64 52 65 61 64 20 0a 2a 2a 0a 2a 2a 20 20  eedRead .**.**  
9ad30 20 20 20 54 68 69 73 20 66 6c 61 67 20 6d 65 61     This flag mea
9ad40 6e 73 20 28 77 68 65 6e 20 74 72 75 65 29 20 74  ns (when true) t
9ad50 68 61 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  hat the content 
9ad60 6f 66 20 74 68 65 20 70 61 67 65 20 68 61 73 0a  of the page has.
9ad70 2a 2a 20 20 20 20 20 6e 6f 74 20 79 65 74 20 62  **     not yet b
9ad80 65 65 6e 20 6c 6f 61 64 65 64 20 66 72 6f 6d 20  een loaded from 
9ad90 64 69 73 6b 2e 20 20 54 68 65 20 69 6e 2d 6d 65  disk.  The in-me
9ada0 6d 6f 72 79 20 63 6f 6e 74 65 6e 74 20 69 73 20  mory content is 
9adb0 6a 75 73 74 0a 2a 2a 20 20 20 20 20 67 61 72 62  just.**     garb
9adc0 61 67 65 2e 20 20 28 41 63 74 75 61 6c 6c 79 2c  age.  (Actually,
9add0 20 77 65 20 7a 65 72 6f 20 74 68 65 20 63 6f 6e   we zero the con
9ade0 74 65 6e 74 2c 20 62 75 74 20 79 6f 75 20 73 68  tent, but you sh
9adf0 6f 75 6c 64 20 6e 6f 74 0a 2a 2a 20 20 20 20 20  ould not.**     
9ae00 6d 61 6b 65 20 61 6e 79 20 61 73 73 75 6d 70 74  make any assumpt
9ae10 69 6f 6e 73 20 61 62 6f 75 74 20 74 68 65 20 63  ions about the c
9ae20 6f 6e 74 65 6e 74 20 6e 65 76 65 72 74 68 65 6c  ontent neverthel
9ae30 65 73 73 2e 29 20 20 49 66 20 74 68 65 0a 2a 2a  ess.)  If the.**
9ae40 20 20 20 20 20 63 6f 6e 74 65 6e 74 20 69 73 20       content is 
9ae50 6e 65 65 64 65 64 20 69 6e 20 74 68 65 20 66 75  needed in the fu
9ae60 74 75 72 65 2c 20 69 74 20 73 68 6f 75 6c 64 20  ture, it should 
9ae70 62 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65  be read from the
9ae80 0a 2a 2a 20 20 20 20 20 6f 72 69 67 69 6e 61 6c  .**     original
9ae90 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a   database file..
9aea0 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
9aeb0 74 20 50 67 48 64 72 20 50 67 48 64 72 3b 0a 73  t PgHdr PgHdr;.s
9aec0 74 72 75 63 74 20 50 67 48 64 72 20 7b 0a 20 20  truct PgHdr {.  
9aed0 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b 20 20  Pager *pPager;  
9aee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
9aef0 2a 20 54 68 65 20 70 61 67 65 72 20 74 6f 20 77  * The pager to w
9af00 68 69 63 68 20 74 68 69 73 20 70 61 67 65 20 62  hich this page b
9af10 65 6c 6f 6e 67 73 20 2a 2f 0a 20 20 50 67 6e 6f  elongs */.  Pgno
9af20 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20   pgno;          
9af30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
9af40 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66 6f  e page number fo
9af50 72 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20  r this page */. 
9af60 20 50 67 48 64 72 20 2a 70 4e 65 78 74 48 61 73   PgHdr *pNextHas
9af70 68 2c 20 2a 70 50 72 65 76 48 61 73 68 3b 20 20  h, *pPrevHash;  
9af80 2f 2a 20 48 61 73 68 20 63 6f 6c 6c 69 73 69 6f  /* Hash collisio
9af90 6e 20 63 68 61 69 6e 20 66 6f 72 20 50 67 48 64  n chain for PgHd
9afa0 72 2e 70 67 6e 6f 20 2a 2f 0a 20 20 50 67 48 64  r.pgno */.  PgHd
9afb0 72 20 2a 70 4e 65 78 74 46 72 65 65 2c 20 2a 70  r *pNextFree, *p
9afc0 50 72 65 76 46 72 65 65 3b 20 20 2f 2a 20 46 72  PrevFree;  /* Fr
9afd0 65 65 6c 69 73 74 20 6f 66 20 70 61 67 65 73 20  eelist of pages 
9afe0 77 68 65 72 65 20 6e 52 65 66 3d 3d 30 20 2a 2f  where nRef==0 */
9aff0 0a 20 20 50 67 48 64 72 20 2a 70 4e 65 78 74 41  .  PgHdr *pNextA
9b000 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ll;             
9b010 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f 66 20 61    /* A list of a
9b020 6c 6c 20 70 61 67 65 73 20 2a 2f 0a 20 20 75 38  ll pages */.  u8
9b030 20 69 6e 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20   inJournal;     
9b040 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
9b050 54 52 55 45 20 69 66 20 68 61 73 20 62 65 65 6e  TRUE if has been
9b060 20 77 72 69 74 74 65 6e 20 74 6f 20 6a 6f 75 72   written to jour
9b070 6e 61 6c 20 2a 2f 0a 20 20 75 38 20 64 69 72 74  nal */.  u8 dirt
9b080 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y;              
9b090 20 20 20 20 20 20 20 20 2f 2a 20 54 52 55 45 20          /* TRUE 
9b0a0 69 66 20 77 65 20 6e 65 65 64 20 74 6f 20 77 72  if we need to wr
9b0b0 69 74 65 20 62 61 63 6b 20 63 68 61 6e 67 65 73  ite back changes
9b0c0 20 2a 2f 0a 20 20 75 38 20 6e 65 65 64 53 79 6e   */.  u8 needSyn
9b0d0 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
9b0e0 20 20 20 20 20 2f 2a 20 53 79 6e 63 20 6a 6f 75       /* Sync jou
9b0f0 72 6e 61 6c 20 62 65 66 6f 72 65 20 77 72 69 74  rnal before writ
9b100 69 6e 67 20 74 68 69 73 20 70 61 67 65 20 2a 2f  ing this page */
9b110 0a 20 20 75 38 20 61 6c 77 61 79 73 52 6f 6c 6c  .  u8 alwaysRoll
9b120 62 61 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20  back;           
9b130 20 20 2f 2a 20 44 69 73 61 62 6c 65 20 44 6f 6e    /* Disable Don
9b140 74 52 6f 6c 6c 62 61 63 6b 28 29 20 66 6f 72 20  tRollback() for 
9b150 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 75  this page */.  u
9b160 38 20 6e 65 65 64 52 65 61 64 3b 20 20 20 20 20  8 needRead;     
9b170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
9b180 20 52 65 61 64 20 63 6f 6e 74 65 6e 74 20 69 66   Read content if
9b190 20 50 61 67 65 72 57 72 69 74 65 28 29 20 69 73   PagerWrite() is
9b1a0 20 63 61 6c 6c 65 64 20 2a 2f 0a 20 20 73 68 6f   called */.  sho
9b1b0 72 74 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20  rt int nRef;    
9b1c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
9b1d0 75 6d 62 65 72 20 6f 66 20 75 73 65 72 73 20 6f  umber of users o
9b1e0 66 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20  f this page */. 
9b1f0 20 50 67 48 64 72 20 2a 70 44 69 72 74 79 2c 20   PgHdr *pDirty, 
9b200 2a 70 50 72 65 76 44 69 72 74 79 3b 20 20 20 20  *pPrevDirty;    
9b210 2f 2a 20 44 69 72 74 79 20 70 61 67 65 73 20 2a  /* Dirty pages *
9b220 2f 0a 20 20 75 33 32 20 6e 6f 74 55 73 65 64 3b  /.  u32 notUsed;
9b230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9b240 20 20 20 2f 2a 20 42 75 66 66 65 72 20 73 70 61     /* Buffer spa
9b250 63 65 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  ce */.#ifdef SQL
9b260 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a  ITE_CHECK_PAGES.
9b270 20 20 75 33 32 20 70 61 67 65 48 61 73 68 3b 0a    u32 pageHash;.
9b280 23 65 6e 64 69 66 0a 20 20 2f 2a 20 70 50 61 67  #endif.  /* pPag
9b290 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 74  er->pageSize byt
9b2a0 65 73 20 6f 66 20 70 61 67 65 20 64 61 74 61 20  es of page data 
9b2b0 66 6f 6c 6c 6f 77 20 74 68 69 73 20 68 65 61 64  follow this head
9b2c0 65 72 20 2a 2f 0a 20 20 2f 2a 20 50 61 67 65 72  er */.  /* Pager
9b2d0 2e 6e 45 78 74 72 61 20 62 79 74 65 73 20 6f 66  .nExtra bytes of
9b2e0 20 6c 6f 63 61 6c 20 64 61 74 61 20 66 6f 6c 6c   local data foll
9b2f0 6f 77 20 74 68 65 20 70 61 67 65 20 64 61 74 61  ow the page data
9b300 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6f   */.};../*.** Fo
9b310 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6f  r an in-memory o
9b320 6e 6c 79 20 64 61 74 61 62 61 73 65 2c 20 73 6f  nly database, so
9b330 6d 65 20 65 78 74 72 61 20 69 6e 66 6f 72 6d 61  me extra informa
9b340 74 69 6f 6e 20 69 73 20 72 65 63 6f 72 64 65 64  tion is recorded
9b350 20 61 62 6f 75 74 0a 2a 2a 20 65 61 63 68 20 70   about.** each p
9b360 61 67 65 20 73 6f 20 74 68 61 74 20 63 68 61 6e  age so that chan
9b370 67 65 73 20 63 61 6e 20 62 65 20 72 6f 6c 6c 65  ges can be rolle
9b380 64 20 62 61 63 6b 2e 20 20 28 4a 6f 75 72 6e 61  d back.  (Journa
9b390 6c 20 66 69 6c 65 73 20 61 72 65 20 6e 6f 74 0a  l files are not.
9b3a0 2a 2a 20 75 73 65 64 20 66 6f 72 20 69 6e 2d 6d  ** used for in-m
9b3b0 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 73 2e  emory databases.
9b3c0 29 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  )  The following
9b3d0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20   information is 
9b3e0 61 64 64 65 64 20 74 6f 0a 2a 2a 20 74 68 65 20  added to.** the 
9b3f0 65 6e 64 20 6f 66 20 65 76 65 72 79 20 45 58 54  end of every EXT
9b400 52 41 20 62 6c 6f 63 6b 20 66 6f 72 20 69 6e 2d  RA block for in-
9b410 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 73  memory databases
9b420 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 66  ..**.** This inf
9b430 6f 72 6d 61 74 69 6f 6e 20 63 6f 75 6c 64 20 68  ormation could h
9b440 61 76 65 20 62 65 65 6e 20 61 64 64 65 64 20 64  ave been added d
9b450 69 72 65 63 74 6c 79 20 74 6f 20 74 68 65 20 50  irectly to the P
9b460 67 48 64 72 20 73 74 72 75 63 74 75 72 65 2e 0a  gHdr structure..
9b470 2a 2a 20 42 75 74 20 74 68 65 6e 20 69 74 20 77  ** But then it w
9b480 6f 75 6c 64 20 74 61 6b 65 20 75 70 20 61 6e 20  ould take up an 
9b490 65 78 74 72 61 20 38 20 62 79 74 65 73 20 6f 66  extra 8 bytes of
9b4a0 20 73 74 6f 72 61 67 65 20 6f 6e 20 65 76 65 72   storage on ever
9b4b0 79 20 50 67 48 64 72 0a 2a 2a 20 65 76 65 6e 20  y PgHdr.** even 
9b4c0 66 6f 72 20 64 69 73 6b 2d 62 61 73 65 64 20 64  for disk-based d
9b4d0 61 74 61 62 61 73 65 73 2e 20 20 53 70 6c 69 74  atabases.  Split
9b4e0 74 69 6e 67 20 69 74 20 6f 75 74 20 73 61 76 65  ting it out save
9b4f0 73 20 38 20 62 79 74 65 73 2e 20 20 54 68 69 73  s 8 bytes.  This
9b500 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 61 20 73 61  .** is only a sa
9b510 76 69 6e 67 73 20 6f 66 20 30 2e 38 25 20 62 75  vings of 0.8% bu
9b520 74 20 74 68 6f 73 65 20 70 65 72 63 65 6e 74 61  t those percenta
9b530 67 65 73 20 61 64 64 20 75 70 2e 0a 2a 2f 0a 74  ges add up..*/.t
9b540 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 67  ypedef struct Pg
9b550 48 69 73 74 6f 72 79 20 50 67 48 69 73 74 6f 72  History PgHistor
9b560 79 3b 0a 73 74 72 75 63 74 20 50 67 48 69 73 74  y;.struct PgHist
9b570 6f 72 79 20 7b 0a 20 20 75 38 20 2a 70 4f 72 69  ory {.  u8 *pOri
9b580 67 3b 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e  g;     /* Origin
9b590 61 6c 20 70 61 67 65 20 74 65 78 74 2e 20 20 52  al page text.  R
9b5a0 65 73 74 6f 72 65 20 74 6f 20 74 68 69 73 20 6f  estore to this o
9b5b0 6e 20 61 20 66 75 6c 6c 20 72 6f 6c 6c 62 61 63  n a full rollbac
9b5c0 6b 20 2a 2f 0a 20 20 75 38 20 2a 70 53 74 6d 74  k */.  u8 *pStmt
9b5d0 3b 20 20 20 20 20 2f 2a 20 54 65 78 74 20 61 73  ;     /* Text as
9b5e0 20 69 74 20 77 61 73 20 61 74 20 74 68 65 20 62   it was at the b
9b5f0 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20  eginning of the 
9b600 63 75 72 72 65 6e 74 20 73 74 61 74 65 6d 65 6e  current statemen
9b610 74 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 4e  t */.  PgHdr *pN
9b620 65 78 74 53 74 6d 74 2c 20 2a 70 50 72 65 76 53  extStmt, *pPrevS
9b630 74 6d 74 3b 20 20 2f 2a 20 4c 69 73 74 20 6f 66  tmt;  /* List of
9b640 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 73 74   pages in the st
9b650 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20  atement journal 
9b660 2a 2f 0a 20 20 75 38 20 69 6e 53 74 6d 74 3b 20  */.  u8 inStmt; 
9b670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9b680 20 20 20 20 2f 2a 20 54 52 55 45 20 69 66 20 69      /* TRUE if i
9b690 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  n the statement 
9b6a0 73 75 62 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 7d 3b  subjournal */.};
9b6b0 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 61 63 72 6f 20  ../*.** A macro 
9b6c0 75 73 65 64 20 66 6f 72 20 69 6e 76 6f 6b 69 6e  used for invokin
9b6d0 67 20 74 68 65 20 63 6f 64 65 63 20 69 66 20 74  g the codec if t
9b6e0 68 65 72 65 20 69 73 20 6f 6e 65 0a 2a 2f 0a 23  here is one.*/.#
9b6f0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53  ifdef SQLITE_HAS
9b700 5f 43 4f 44 45 43 0a 23 20 64 65 66 69 6e 65 20  _CODEC.# define 
9b710 43 4f 44 45 43 31 28 50 2c 44 2c 4e 2c 58 29 20  CODEC1(P,D,N,X) 
9b720 69 66 28 20 50 2d 3e 78 43 6f 64 65 63 21 3d 30  if( P->xCodec!=0
9b730 20 29 7b 20 50 2d 3e 78 43 6f 64 65 63 28 50 2d   ){ P->xCodec(P-
9b740 3e 70 43 6f 64 65 63 41 72 67 2c 44 2c 4e 2c 58  >pCodecArg,D,N,X
9b750 29 3b 20 7d 0a 23 20 64 65 66 69 6e 65 20 43 4f  ); }.# define CO
9b760 44 45 43 32 28 50 2c 44 2c 4e 2c 58 29 20 28 28  DEC2(P,D,N,X) ((
9b770 63 68 61 72 2a 29 28 50 2d 3e 78 43 6f 64 65 63  char*)(P->xCodec
9b780 21 3d 30 3f 50 2d 3e 78 43 6f 64 65 63 28 50 2d  !=0?P->xCodec(P-
9b790 3e 70 43 6f 64 65 63 41 72 67 2c 44 2c 4e 2c 58  >pCodecArg,D,N,X
9b7a0 29 3a 44 29 29 0a 23 65 6c 73 65 0a 23 20 64 65  ):D)).#else.# de
9b7b0 66 69 6e 65 20 43 4f 44 45 43 31 28 50 2c 44 2c  fine CODEC1(P,D,
9b7c0 4e 2c 58 29 20 2f 2a 20 4e 4f 2d 4f 50 20 2a 2f  N,X) /* NO-OP */
9b7d0 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 45 43 32  .# define CODEC2
9b7e0 28 50 2c 44 2c 4e 2c 58 29 20 28 28 63 68 61 72  (P,D,N,X) ((char
9b7f0 2a 29 44 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  *)D).#endif../*.
9b800 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 70 6f 69  ** Convert a poi
9b810 6e 74 65 72 20 74 6f 20 61 20 50 67 48 64 72 20  nter to a PgHdr 
9b820 69 6e 74 6f 20 61 20 70 6f 69 6e 74 65 72 20 74  into a pointer t
9b830 6f 20 69 74 73 20 64 61 74 61 0a 2a 2a 20 61 6e  o its data.** an
9b840 64 20 62 61 63 6b 20 61 67 61 69 6e 2e 0a 2a 2f  d back again..*/
9b850 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f 54  .#define PGHDR_T
9b860 4f 5f 44 41 54 41 28 50 29 20 20 28 28 76 6f 69  O_DATA(P)  ((voi
9b870 64 2a 29 28 26 28 50 29 5b 31 5d 29 29 0a 23 64  d*)(&(P)[1])).#d
9b880 65 66 69 6e 65 20 44 41 54 41 5f 54 4f 5f 50 47  efine DATA_TO_PG
9b890 48 44 52 28 44 29 20 20 28 26 28 28 50 67 48 64  HDR(D)  (&((PgHd
9b8a0 72 2a 29 28 44 29 29 5b 2d 31 5d 29 0a 23 64 65  r*)(D))[-1]).#de
9b8b0 66 69 6e 65 20 50 47 48 44 52 5f 54 4f 5f 45 58  fine PGHDR_TO_EX
9b8c0 54 52 41 28 47 2c 50 29 20 28 28 76 6f 69 64 2a  TRA(G,P) ((void*
9b8d0 29 26 28 28 63 68 61 72 2a 29 28 26 28 47 29 5b  )&((char*)(&(G)[
9b8e0 31 5d 29 29 5b 28 50 29 2d 3e 70 61 67 65 53 69  1]))[(P)->pageSi
9b8f0 7a 65 5d 29 0a 23 64 65 66 69 6e 65 20 50 47 48  ze]).#define PGH
9b900 44 52 5f 54 4f 5f 48 49 53 54 28 50 2c 50 47 52  DR_TO_HIST(P,PGR
9b910 29 20 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20  )  \.           
9b920 20 28 28 50 67 48 69 73 74 6f 72 79 2a 29 26 28   ((PgHistory*)&(
9b930 28 63 68 61 72 2a 29 28 26 28 50 29 5b 31 5d 29  (char*)(&(P)[1])
9b940 29 5b 28 50 47 52 29 2d 3e 70 61 67 65 53 69 7a  )[(PGR)->pageSiz
9b950 65 2b 28 50 47 52 29 2d 3e 6e 45 78 74 72 61 5d  e+(PGR)->nExtra]
9b960 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 6f 70 65 6e 20  )../*.** A open 
9b970 70 61 67 65 20 63 61 63 68 65 20 69 73 20 61 6e  page cache is an
9b980 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
9b990 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
9b9a0 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 50 61 67 65  ture..**.** Page
9b9b0 72 2e 65 72 72 43 6f 64 65 20 6d 61 79 20 62 65  r.errCode may be
9b9c0 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 49   set to SQLITE_I
9b9d0 4f 45 52 52 2c 20 53 51 4c 49 54 45 5f 43 4f 52  OERR, SQLITE_COR
9b9e0 52 55 50 54 2c 20 6f 72 0a 2a 2a 20 6f 72 20 53  RUPT, or.** or S
9b9f0 51 4c 49 54 45 5f 46 55 4c 4c 2e 20 4f 6e 63 65  QLITE_FULL. Once
9ba00 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 69 72 73   one of the firs
9ba10 74 20 74 68 72 65 65 20 65 72 72 6f 72 73 20 6f  t three errors o
9ba20 63 63 75 72 73 2c 20 69 74 20 70 65 72 73 69 73  ccurs, it persis
9ba30 74 73 0a 2a 2a 20 61 6e 64 20 69 73 20 72 65 74  ts.** and is ret
9ba40 75 72 6e 65 64 20 61 73 20 74 68 65 20 72 65 73  urned as the res
9ba50 75 6c 74 20 6f 66 20 65 76 65 72 79 20 6d 61 6a  ult of every maj
9ba60 6f 72 20 70 61 67 65 72 20 41 50 49 20 63 61 6c  or pager API cal
9ba70 6c 2e 20 20 54 68 65 0a 2a 2a 20 53 51 4c 49 54  l.  The.** SQLIT
9ba80 45 5f 46 55 4c 4c 20 72 65 74 75 72 6e 20 63 6f  E_FULL return co
9ba90 64 65 20 69 73 20 73 6c 69 67 68 74 6c 79 20 64  de is slightly d
9baa0 69 66 66 65 72 65 6e 74 2e 20 49 74 20 70 65 72  ifferent. It per
9bab0 73 69 73 74 73 20 6f 6e 6c 79 20 75 6e 74 69 6c  sists only until
9bac0 20 74 68 65 0a 2a 2a 20 6e 65 78 74 20 73 75 63   the.** next suc
9bad0 63 65 73 73 66 75 6c 20 72 6f 6c 6c 62 61 63 6b  cessful rollback
9bae0 20 69 73 20 70 65 72 66 6f 72 6d 65 64 20 6f 6e   is performed on
9baf0 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65   the pager cache
9bb00 2e 20 41 6c 73 6f 2c 0a 2a 2a 20 53 51 4c 49 54  . Also,.** SQLIT
9bb10 45 5f 46 55 4c 4c 20 64 6f 65 73 20 6e 6f 74 20  E_FULL does not 
9bb20 61 66 66 65 63 74 20 74 68 65 20 73 71 6c 69 74  affect the sqlit
9bb30 65 33 50 61 67 65 72 47 65 74 28 29 20 61 6e 64  e3PagerGet() and
9bb40 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f   sqlite3PagerLoo
9bb50 6b 75 70 28 29 0a 2a 2a 20 41 50 49 73 2c 20 74  kup().** APIs, t
9bb60 68 65 79 20 6d 61 79 20 73 74 69 6c 6c 20 62 65  hey may still be
9bb70 20 75 73 65 64 20 73 75 63 63 65 73 73 66 75 6c   used successful
9bb80 6c 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 61  ly..*/.struct Pa
9bb90 67 65 72 20 7b 0a 20 20 75 38 20 6a 6f 75 72 6e  ger {.  u8 journ
9bba0 61 6c 4f 70 65 6e 3b 20 20 20 20 20 20 20 20 20  alOpen;         
9bbb0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6a      /* True if j
9bbc0 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 63  ournal file desc
9bbd0 72 69 70 74 6f 72 73 20 69 73 20 76 61 6c 69 64  riptors is valid
9bbe0 20 2a 2f 0a 20 20 75 38 20 6a 6f 75 72 6e 61 6c   */.  u8 journal
9bbf0 53 74 61 72 74 65 64 3b 20 20 20 20 20 20 20 20  Started;        
9bc00 20 20 2f 2a 20 54 72 75 65 20 69 66 20 68 65 61    /* True if hea
9bc10 64 65 72 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 69  der of journal i
9bc20 73 20 73 79 6e 63 65 64 20 2a 2f 0a 20 20 75 38  s synced */.  u8
9bc30 20 75 73 65 4a 6f 75 72 6e 61 6c 3b 20 20 20 20   useJournal;    
9bc40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65            /* Use
9bc50 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72   a rollback jour
9bc60 6e 61 6c 20 6f 6e 20 74 68 69 73 20 66 69 6c 65  nal on this file
9bc70 20 2a 2f 0a 20 20 75 38 20 6e 6f 52 65 61 64 6c   */.  u8 noReadl
9bc80 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20  ock;            
9bc90 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 62 6f 74 68    /* Do not both
9bca0 65 72 20 74 6f 20 6f 62 74 61 69 6e 20 72 65 61  er to obtain rea
9bcb0 64 6c 6f 63 6b 73 20 2a 2f 0a 20 20 75 38 20 73  dlocks */.  u8 s
9bcc0 74 6d 74 4f 70 65 6e 3b 20 20 20 20 20 20 20 20  tmtOpen;        
9bcd0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
9bce0 69 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  if the statement
9bcf0 20 73 75 62 6a 6f 75 72 6e 61 6c 20 69 73 20 6f   subjournal is o
9bd00 70 65 6e 20 2a 2f 0a 20 20 75 38 20 73 74 6d 74  pen */.  u8 stmt
9bd10 49 6e 55 73 65 3b 20 20 20 20 20 20 20 20 20 20  InUse;          
9bd20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 77 65 20       /* True we 
9bd30 61 72 65 20 69 6e 20 61 20 73 74 61 74 65 6d 65  are in a stateme
9bd40 6e 74 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f  nt subtransactio
9bd50 6e 20 2a 2f 0a 20 20 75 38 20 73 74 6d 74 41 75  n */.  u8 stmtAu
9bd60 74 6f 6f 70 65 6e 3b 20 20 20 20 20 20 20 20 20  toopen;         
9bd70 20 20 20 2f 2a 20 4f 70 65 6e 20 73 74 6d 74 20     /* Open stmt 
9bd80 6a 6f 75 72 6e 61 6c 20 77 68 65 6e 20 6d 61 69  journal when mai
9bd90 6e 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 70 65  n journal is ope
9bda0 6e 65 64 2a 2f 0a 20 20 75 38 20 6e 6f 53 79 6e  ned*/.  u8 noSyn
9bdb0 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
9bdc0 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 73 79      /* Do not sy
9bdd0 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69  nc the journal i
9bde0 66 20 74 72 75 65 20 2a 2f 0a 20 20 75 38 20 66  f true */.  u8 f
9bdf0 75 6c 6c 53 79 6e 63 3b 20 20 20 20 20 20 20 20  ullSync;        
9be00 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 65 78          /* Do ex
9be10 74 72 61 20 73 79 6e 63 73 20 6f 66 20 74 68 65  tra syncs of the
9be20 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 72 6f 62   journal for rob
9be30 75 73 74 6e 65 73 73 20 2a 2f 0a 20 20 75 38 20  ustness */.  u8 
9be40 66 75 6c 6c 5f 66 73 79 6e 63 3b 20 20 20 20 20  full_fsync;     
9be50 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20           /* Use 
9be60 46 5f 46 55 4c 4c 46 53 59 4e 43 20 77 68 65 6e  F_FULLFSYNC when
9be70 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f 0a 20 20   available */.  
9be80 75 38 20 73 74 61 74 65 3b 20 20 20 20 20 20 20  u8 state;       
9be90 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
9bea0 41 47 45 52 5f 55 4e 4c 4f 43 4b 2c 20 5f 53 48  AGER_UNLOCK, _SH
9beb0 41 52 45 44 2c 20 5f 52 45 53 45 52 56 45 44 2c  ARED, _RESERVED,
9bec0 20 65 74 63 2e 20 2a 2f 0a 20 20 75 38 20 74 65   etc. */.  u8 te
9bed0 6d 70 46 69 6c 65 3b 20 20 20 20 20 20 20 20 20  mpFile;         
9bee0 20 20 20 20 20 20 20 2f 2a 20 7a 46 69 6c 65 6e         /* zFilen
9bef0 61 6d 65 20 69 73 20 61 20 74 65 6d 70 6f 72 61  ame is a tempora
9bf00 72 79 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20  ry file */.  u8 
9bf10 72 65 61 64 4f 6e 6c 79 3b 20 20 20 20 20 20 20  readOnly;       
9bf20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
9bf30 20 66 6f 72 20 61 20 72 65 61 64 2d 6f 6e 6c 79   for a read-only
9bf40 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 75   database */.  u
9bf50 38 20 6e 65 65 64 53 79 6e 63 3b 20 20 20 20 20  8 needSync;     
9bf60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
9bf70 75 65 20 69 66 20 61 6e 20 66 73 79 6e 63 28 29  ue if an fsync()
9bf80 20 69 73 20 6e 65 65 64 65 64 20 6f 6e 20 74 68   is needed on th
9bf90 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75  e journal */.  u
9bfa0 38 20 64 69 72 74 79 43 61 63 68 65 3b 20 20 20  8 dirtyCache;   
9bfb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
9bfc0 75 65 20 69 66 20 63 61 63 68 65 64 20 70 61 67  ue if cached pag
9bfd0 65 73 20 68 61 76 65 20 63 68 61 6e 67 65 64 20  es have changed 
9bfe0 2a 2f 0a 20 20 75 38 20 61 6c 77 61 79 73 52 6f  */.  u8 alwaysRo
9bff0 6c 6c 62 61 63 6b 3b 20 20 20 20 20 20 20 20 20  llback;         
9c000 20 2f 2a 20 44 69 73 61 62 6c 65 20 44 6f 6e 74   /* Disable Dont
9c010 52 6f 6c 6c 62 61 63 6b 28 29 20 66 6f 72 20 61  Rollback() for a
9c020 6c 6c 20 70 61 67 65 73 20 2a 2f 0a 20 20 75 38  ll pages */.  u8
9c030 20 6d 65 6d 44 62 3b 20 20 20 20 20 20 20 20 20   memDb;         
9c040 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
9c050 65 20 74 6f 20 69 6e 68 69 62 69 74 20 61 6c 6c  e to inhibit all
9c060 20 66 69 6c 65 20 49 2f 4f 20 2a 2f 0a 20 20 75   file I/O */.  u
9c070 38 20 73 65 74 4d 61 73 74 65 72 3b 20 20 20 20  8 setMaster;    
9c080 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
9c090 75 65 20 69 66 20 61 20 6d 2d 6a 20 6e 61 6d 65  ue if a m-j name
9c0a0 20 68 61 73 20 62 65 65 6e 20 77 72 69 74 74 65   has been writte
9c0b0 6e 20 74 6f 20 6a 72 6e 6c 20 2a 2f 0a 20 20 75  n to jrnl */.  u
9c0c0 38 20 64 6f 4e 6f 74 53 79 6e 63 3b 20 20 20 20  8 doNotSync;    
9c0d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 6f             /* Bo
9c0e0 6f 6c 65 61 6e 2e 20 57 68 69 6c 65 20 74 72 75  olean. While tru
9c0f0 65 2c 20 64 6f 20 6e 6f 74 20 73 70 69 6c 6c 20  e, do not spill 
9c100 74 68 65 20 63 61 63 68 65 20 2a 2f 0a 20 20 75  the cache */.  u
9c110 38 20 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 3b  8 exclusiveMode;
9c120 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 6f             /* Bo
9c130 6f 6c 65 61 6e 2e 20 54 72 75 65 20 69 66 20 6c  olean. True if l
9c140 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 3d 45 58 43  ocking_mode==EXC
9c150 4c 55 53 49 56 45 20 2a 2f 0a 20 20 75 38 20 63  LUSIVE */.  u8 c
9c160 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 3b 20  hangeCountDone; 
9c170 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 61          /* Set a
9c180 66 74 65 72 20 69 6e 63 72 65 6d 65 6e 74 69 6e  fter incrementin
9c190 67 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75  g the change-cou
9c1a0 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 65 72  nter */.  int er
9c1b0 72 43 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20  rCode;          
9c1c0 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20        /* One of 
9c1d0 73 65 76 65 72 61 6c 20 6b 69 6e 64 73 20 6f 66  several kinds of
9c1e0 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 69 6e 74   errors */.  int
9c1f0 20 64 62 53 69 7a 65 3b 20 20 20 20 20 20 20 20   dbSize;        
9c200 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
9c210 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74  er of pages in t
9c220 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74  he file */.  int
9c230 20 6f 72 69 67 44 62 53 69 7a 65 3b 20 20 20 20   origDbSize;    
9c240 20 20 20 20 20 20 20 20 20 2f 2a 20 64 62 53 69           /* dbSi
9c250 7a 65 20 62 65 66 6f 72 65 20 74 68 65 20 63 75  ze before the cu
9c260 72 72 65 6e 74 20 63 68 61 6e 67 65 20 2a 2f 0a  rrent change */.
9c270 20 20 69 6e 74 20 73 74 6d 74 53 69 7a 65 3b 20    int stmtSize; 
9c280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
9c290 20 53 69 7a 65 20 6f 66 20 64 61 74 61 62 61 73   Size of databas
9c2a0 65 20 28 69 6e 20 70 61 67 65 73 29 20 61 74 20  e (in pages) at 
9c2b0 73 74 6d 74 5f 62 65 67 69 6e 28 29 20 2a 2f 0a  stmt_begin() */.
9c2c0 20 20 69 6e 74 20 6e 52 65 63 3b 20 20 20 20 20    int nRec;     
9c2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
9c2e0 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   Number of pages
9c2f0 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20   written to the 
9c300 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 33 32  journal */.  u32
9c310 20 63 6b 73 75 6d 49 6e 69 74 3b 20 20 20 20 20   cksumInit;     
9c320 20 20 20 20 20 20 20 20 20 2f 2a 20 51 75 61 73           /* Quas
9c330 69 2d 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 61  i-random value a
9c340 64 64 65 64 20 74 6f 20 65 76 65 72 79 20 63 68  dded to every ch
9c350 65 63 6b 73 75 6d 20 2a 2f 0a 20 20 69 6e 74 20  ecksum */.  int 
9c360 73 74 6d 74 4e 52 65 63 3b 20 20 20 20 20 20 20  stmtNRec;       
9c370 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
9c380 72 20 6f 66 20 72 65 63 6f 72 64 73 20 69 6e 20  r of records in 
9c390 73 74 6d 74 20 73 75 62 6a 6f 75 72 6e 61 6c 20  stmt subjournal 
9c3a0 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 3b  */.  int nExtra;
9c3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9c3c0 20 2f 2a 20 41 64 64 20 74 68 69 73 20 6d 61 6e   /* Add this man
9c3d0 79 20 62 79 74 65 73 20 74 6f 20 65 61 63 68 20  y bytes to each 
9c3e0 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 20 2a  in-memory page *
9c3f0 2f 0a 20 20 69 6e 74 20 70 61 67 65 53 69 7a 65  /.  int pageSize
9c400 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
9c410 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
9c420 65 73 20 69 6e 20 61 20 70 61 67 65 20 2a 2f 0a  es in a page */.
9c430 20 20 69 6e 74 20 6e 50 61 67 65 3b 20 20 20 20    int nPage;    
9c440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
9c450 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   Total number of
9c460 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73   in-memory pages
9c470 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20   */.  int nRef; 
9c480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9c490 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69    /* Number of i
9c4a0 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 20 77  n-memory pages w
9c4b0 69 74 68 20 50 67 48 64 72 2e 6e 52 65 66 3e 30  ith PgHdr.nRef>0
9c4c0 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 50 61 67 65   */.  int mxPage
9c4d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
9c4e0 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d    /* Maximum num
9c4f0 62 65 72 20 6f 66 20 70 61 67 65 73 20 74 6f 20  ber of pages to 
9c500 68 6f 6c 64 20 69 6e 20 63 61 63 68 65 20 2a 2f  hold in cache */
9c510 0a 20 20 50 67 6e 6f 20 6d 78 50 67 6e 6f 3b 20  .  Pgno mxPgno; 
9c520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
9c530 2a 20 4d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65  * Maximum allowe
9c540 64 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61  d size of the da
9c550 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 38 20 2a  tabase */.  u8 *
9c560 61 49 6e 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20  aInJournal;     
9c570 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 62          /* One b
9c580 69 74 20 66 6f 72 20 65 61 63 68 20 70 61 67 65  it for each page
9c590 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
9c5a0 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 2a 61   file */.  u8 *a
9c5b0 49 6e 53 74 6d 74 3b 20 20 20 20 20 20 20 20 20  InStmt;         
9c5c0 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 62 69         /* One bi
9c5d0 74 20 66 6f 72 20 65 61 63 68 20 70 61 67 65 20  t for each page 
9c5e0 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
9c5f0 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65  */.  char *zFile
9c600 6e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20  name;           
9c610 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
9c620 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f  database file */
9c630 0a 20 20 63 68 61 72 20 2a 7a 4a 6f 75 72 6e 61  .  char *zJourna
9c640 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  l;             /
9c650 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f  * Name of the jo
9c660 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20  urnal file */.  
9c670 63 68 61 72 20 2a 7a 44 69 72 65 63 74 6f 72 79  char *zDirectory
9c680 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44  ;           /* D
9c690 69 72 65 63 74 6f 72 79 20 68 6f 6c 64 20 64 61  irectory hold da
9c6a0 74 61 62 61 73 65 20 61 6e 64 20 6a 6f 75 72 6e  tabase and journ
9c6b0 61 6c 20 66 69 6c 65 73 20 2a 2f 0a 20 20 4f 73  al files */.  Os
9c6c0 46 69 6c 65 20 2a 66 64 2c 20 2a 6a 66 64 3b 20  File *fd, *jfd; 
9c6d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c            /* Fil
9c6e0 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 66 6f  e descriptors fo
9c6f0 72 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 6a  r database and j
9c700 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 4f 73 46 69  ournal */.  OsFi
9c710 6c 65 20 2a 73 74 66 64 3b 20 20 20 20 20 20 20  le *stfd;       
9c720 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20          /* File 
9c730 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20 74  descriptor for t
9c740 68 65 20 73 74 61 74 65 6d 65 6e 74 20 73 75 62  he statement sub
9c750 6a 6f 75 72 6e 61 6c 2a 2f 0a 20 20 42 75 73 79  journal*/.  Busy
9c760 48 61 6e 64 6c 65 72 20 2a 70 42 75 73 79 48 61  Handler *pBusyHa
9c770 6e 64 6c 65 72 3b 20 20 2f 2a 20 50 6f 69 6e 74  ndler;  /* Point
9c780 65 72 20 74 6f 20 73 71 6c 69 74 65 2e 62 75 73  er to sqlite.bus
9c790 79 48 61 6e 64 6c 65 72 20 2a 2f 0a 20 20 50 67  yHandler */.  Pg
9c7a0 48 64 72 20 2a 70 46 69 72 73 74 2c 20 2a 70 4c  Hdr *pFirst, *pL
9c7b0 61 73 74 3b 20 20 20 20 20 20 2f 2a 20 4c 69 73  ast;      /* Lis
9c7c0 74 20 6f 66 20 66 72 65 65 20 70 61 67 65 73 20  t of free pages 
9c7d0 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 46 69 72  */.  PgHdr *pFir
9c7e0 73 74 53 79 6e 63 65 64 3b 20 20 20 20 20 20 20  stSynced;       
9c7f0 20 2f 2a 20 46 69 72 73 74 20 66 72 65 65 20 70   /* First free p
9c800 61 67 65 20 77 69 74 68 20 50 67 48 64 72 2e 6e  age with PgHdr.n
9c810 65 65 64 53 79 6e 63 3d 3d 30 20 2a 2f 0a 20 20  eedSync==0 */.  
9c820 50 67 48 64 72 20 2a 70 41 6c 6c 3b 20 20 20 20  PgHdr *pAll;    
9c830 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
9c840 69 73 74 20 6f 66 20 61 6c 6c 20 70 61 67 65 73  ist of all pages
9c850 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 53 74   */.  PgHdr *pSt
9c860 6d 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  mt;             
9c870 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 70 61 67    /* List of pag
9c880 65 73 20 69 6e 20 74 68 65 20 73 74 61 74 65 6d  es in the statem
9c890 65 6e 74 20 73 75 62 6a 6f 75 72 6e 61 6c 20 2a  ent subjournal *
9c8a0 2f 0a 20 20 50 67 48 64 72 20 2a 70 44 69 72 74  /.  PgHdr *pDirt
9c8b0 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y;              
9c8c0 2f 2a 20 4c 69 73 74 20 6f 66 20 61 6c 6c 20 64  /* List of all d
9c8d0 69 72 74 79 20 70 61 67 65 73 20 2a 2f 0a 20 20  irty pages */.  
9c8e0 69 36 34 20 6a 6f 75 72 6e 61 6c 4f 66 66 3b 20  i64 journalOff; 
9c8f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
9c900 75 72 72 65 6e 74 20 62 79 74 65 20 6f 66 66 73  urrent byte offs
9c910 65 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61  et in the journa
9c920 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 36 34 20  l file */.  i64 
9c930 6a 6f 75 72 6e 61 6c 48 64 72 3b 20 20 20 20 20  journalHdr;     
9c940 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 20          /* Byte 
9c950 6f 66 66 73 65 74 20 74 6f 20 70 72 65 76 69 6f  offset to previo
9c960 75 73 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  us journal heade
9c970 72 20 2a 2f 0a 20 20 69 36 34 20 73 74 6d 74 48  r */.  i64 stmtH
9c980 64 72 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20  drOff;          
9c990 20 20 20 2f 2a 20 46 69 72 73 74 20 6a 6f 75 72     /* First jour
9c9a0 6e 61 6c 20 68 65 61 64 65 72 20 77 72 69 74 74  nal header writt
9c9b0 65 6e 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e  en this statemen
9c9c0 74 20 2a 2f 0a 20 20 69 36 34 20 73 74 6d 74 43  t */.  i64 stmtC
9c9d0 6b 73 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20  ksum;           
9c9e0 20 20 20 2f 2a 20 63 6b 73 75 6d 49 6e 69 74 20     /* cksumInit 
9c9f0 77 68 65 6e 20 73 74 61 74 65 6d 65 6e 74 20 77  when statement w
9ca00 61 73 20 73 74 61 72 74 65 64 20 2a 2f 0a 20 20  as started */.  
9ca10 69 36 34 20 73 74 6d 74 4a 53 69 7a 65 3b 20 20  i64 stmtJSize;  
9ca20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
9ca30 69 7a 65 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 61  ize of journal a
9ca40 74 20 73 74 6d 74 5f 62 65 67 69 6e 28 29 20 2a  t stmt_begin() *
9ca50 2f 0a 20 20 69 6e 74 20 73 65 63 74 6f 72 53 69  /.  int sectorSi
9ca60 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ze;             
9ca70 2f 2a 20 41 73 73 75 6d 65 64 20 73 65 63 74 6f  /* Assumed secto
9ca80 72 20 73 69 7a 65 20 64 75 72 69 6e 67 20 72 6f  r size during ro
9ca90 6c 6c 62 61 63 6b 20 2a 2f 0a 23 69 66 64 65 66  llback */.#ifdef
9caa0 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69   SQLITE_TEST.  i
9cab0 6e 74 20 6e 48 69 74 2c 20 6e 4d 69 73 73 3b 20  nt nHit, nMiss; 
9cac0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61             /* Ca
9cad0 63 68 65 20 68 69 74 73 20 61 6e 64 20 6d 69 73  che hits and mis
9cae0 73 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6e 52  sing */.  int nR
9caf0 65 61 64 2c 20 6e 57 72 69 74 65 3b 20 20 20 20  ead, nWrite;    
9cb00 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
9cb10 65 20 70 61 67 65 73 20 72 65 61 64 2f 77 72 69  e pages read/wri
9cb20 74 74 65 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 20  tten */.#endif. 
9cb30 20 76 6f 69 64 20 28 2a 78 44 65 73 74 72 75 63   void (*xDestruc
9cb40 74 6f 72 29 28 44 62 50 61 67 65 2a 2c 69 6e 74  tor)(DbPage*,int
9cb50 29 3b 20 2f 2a 20 43 61 6c 6c 20 74 68 69 73 20  ); /* Call this 
9cb60 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 66 72 65  routine when fre
9cb70 65 69 6e 67 20 70 61 67 65 73 20 2a 2f 0a 20 20  eing pages */.  
9cb80 76 6f 69 64 20 28 2a 78 52 65 69 6e 69 74 65 72  void (*xReiniter
9cb90 29 28 44 62 50 61 67 65 2a 2c 69 6e 74 29 3b 20  )(DbPage*,int); 
9cba0 20 20 2f 2a 20 43 61 6c 6c 20 74 68 69 73 20 72    /* Call this r
9cbb0 6f 75 74 69 6e 65 20 77 68 65 6e 20 72 65 6c 6f  outine when relo
9cbc0 61 64 69 6e 67 20 70 61 67 65 73 20 2a 2f 0a 23  ading pages */.#
9cbd0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53  ifdef SQLITE_HAS
9cbe0 5f 43 4f 44 45 43 0a 20 20 76 6f 69 64 20 2a 28  _CODEC.  void *(
9cbf0 2a 78 43 6f 64 65 63 29 28 76 6f 69 64 2a 2c 76  *xCodec)(void*,v
9cc00 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74 29 3b 20  oid*,Pgno,int); 
9cc10 2f 2a 20 52 6f 75 74 69 6e 65 20 66 6f 72 20 65  /* Routine for e
9cc20 6e 2f 64 65 63 6f 64 69 6e 67 20 64 61 74 61 20  n/decoding data 
9cc30 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6f 64 65  */.  void *pCode
9cc40 63 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20  cArg;           
9cc50 20 2f 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65   /* First argume
9cc60 6e 74 20 74 6f 20 78 43 6f 64 65 63 28 29 20 2a  nt to xCodec() *
9cc70 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 6e  /.#endif.  int n
9cc80 48 61 73 68 3b 20 20 20 20 20 20 20 20 20 20 20  Hash;           
9cc90 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
9cca0 66 20 74 68 65 20 70 61 67 65 72 20 68 61 73 68  f the pager hash
9ccb0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 50 67 48 64   table */.  PgHd
9ccc0 72 20 2a 2a 61 48 61 73 68 3b 20 20 20 20 20 20  r **aHash;      
9ccd0 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20          /* Hash 
9cce0 74 61 62 6c 65 20 74 6f 20 6d 61 70 20 70 61 67  table to map pag
9ccf0 65 20 6e 75 6d 62 65 72 20 74 6f 20 50 67 48 64  e number to PgHd
9cd00 72 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  r */.#ifdef SQLI
9cd10 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59  TE_ENABLE_MEMORY
9cd20 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 20 20 50 61  _MANAGEMENT.  Pa
9cd30 67 65 72 20 2a 70 4e 65 78 74 3b 20 20 20 20 20  ger *pNext;     
9cd40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 6e            /* Lin
9cd50 6b 65 64 20 6c 69 73 74 20 6f 66 20 70 61 67 65  ked list of page
9cd60 72 73 20 69 6e 20 74 68 69 73 20 74 68 72 65 61  rs in this threa
9cd70 64 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 63 68  d */.#endif.  ch
9cd80 61 72 20 2a 70 54 6d 70 53 70 61 63 65 3b 20 20  ar *pTmpSpace;  
9cd90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67            /* Pag
9cda0 65 72 2e 70 61 67 65 53 69 7a 65 20 62 79 74 65  er.pageSize byte
9cdb0 73 20 6f 66 20 73 70 61 63 65 20 66 6f 72 20 74  s of space for t
9cdc0 6d 70 20 75 73 65 20 2a 2f 0a 20 20 63 68 61 72  mp use */.  char
9cdd0 20 64 62 46 69 6c 65 56 65 72 73 5b 31 36 5d 3b   dbFileVers[16];
9cde0 20 20 20 20 20 20 20 20 2f 2a 20 43 68 61 6e 67          /* Chang
9cdf0 65 73 20 77 68 65 6e 65 76 65 72 20 64 61 74 61  es whenever data
9ce00 62 61 73 65 20 66 69 6c 65 20 63 68 61 6e 67 65  base file change
9ce10 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54  s */.};../*.** T
9ce20 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f  he following glo
9ce30 62 61 6c 20 76 61 72 69 61 62 6c 65 73 20 68 6f  bal variables ho
9ce40 6c 64 20 63 6f 75 6e 74 65 72 73 20 75 73 65 64  ld counters used
9ce50 20 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20   for.** testing 
9ce60 70 75 72 70 6f 73 65 73 20 6f 6e 6c 79 2e 20 20  purposes only.  
9ce70 54 68 65 73 65 20 76 61 72 69 61 62 6c 65 73 20  These variables 
9ce80 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 69 6e 0a  do not exist in.
9ce90 2a 2a 20 61 20 6e 6f 6e 2d 74 65 73 74 69 6e 67  ** a non-testing
9cea0 20 62 75 69 6c 64 2e 20 20 54 68 65 73 65 20 76   build.  These v
9ceb0 61 72 69 61 62 6c 65 73 20 61 72 65 20 6e 6f 74  ariables are not
9cec0 20 74 68 72 65 61 64 2d 73 61 66 65 2e 0a 2a 2f   thread-safe..*/
9ced0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
9cee0 45 53 54 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f  EST.int sqlite3_
9cef0 70 61 67 65 72 5f 72 65 61 64 64 62 5f 63 6f 75  pager_readdb_cou
9cf00 6e 74 20 3d 20 30 3b 20 20 20 20 2f 2a 20 4e 75  nt = 0;    /* Nu
9cf10 6d 62 65 72 20 6f 66 20 66 75 6c 6c 20 70 61 67  mber of full pag
9cf20 65 73 20 72 65 61 64 20 66 72 6f 6d 20 44 42 20  es read from DB 
9cf30 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 70  */.int sqlite3_p
9cf40 61 67 65 72 5f 77 72 69 74 65 64 62 5f 63 6f 75  ager_writedb_cou
9cf50 6e 74 20 3d 20 30 3b 20 20 20 2f 2a 20 4e 75 6d  nt = 0;   /* Num
9cf60 62 65 72 20 6f 66 20 66 75 6c 6c 20 70 61 67 65  ber of full page
9cf70 73 20 77 72 69 74 74 65 6e 20 74 6f 20 44 42 20  s written to DB 
9cf80 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 70  */.int sqlite3_p
9cf90 61 67 65 72 5f 77 72 69 74 65 6a 5f 63 6f 75 6e  ager_writej_coun
9cfa0 74 20 3d 20 30 3b 20 20 20 20 2f 2a 20 4e 75 6d  t = 0;    /* Num
9cfb0 62 65 72 20 6f 66 20 70 61 67 65 73 20 77 72 69  ber of pages wri
9cfc0 74 74 65 6e 20 74 6f 20 6a 6f 75 72 6e 61 6c 20  tten to journal 
9cfd0 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 70  */.int sqlite3_p
9cfe0 61 67 65 72 5f 70 67 66 72 65 65 5f 63 6f 75 6e  ager_pgfree_coun
9cff0 74 20 3d 20 30 3b 20 20 20 20 2f 2a 20 4e 75 6d  t = 0;    /* Num
9d000 62 65 72 20 6f 66 20 63 61 63 68 65 20 70 61 67  ber of cache pag
9d010 65 73 20 66 72 65 65 64 20 2a 2f 0a 23 20 64 65  es freed */.# de
9d020 66 69 6e 65 20 50 41 47 45 52 5f 49 4e 43 52 28  fine PAGER_INCR(
9d030 76 29 20 20 76 2b 2b 0a 23 65 6c 73 65 0a 23 20  v)  v++.#else.# 
9d040 64 65 66 69 6e 65 20 50 41 47 45 52 5f 49 4e 43  define PAGER_INC
9d050 52 28 76 29 0a 23 65 6e 64 69 66 0a 0a 0a 0a 2f  R(v).#endif..../
9d060 2a 0a 2a 2a 20 4a 6f 75 72 6e 61 6c 20 66 69 6c  *.** Journal fil
9d070 65 73 20 62 65 67 69 6e 20 77 69 74 68 20 74 68  es begin with th
9d080 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 67 69  e following magi
9d090 63 20 73 74 72 69 6e 67 2e 20 20 54 68 65 20 64  c string.  The d
9d0a0 61 74 61 0a 2a 2a 20 77 61 73 20 6f 62 74 61 69  ata.** was obtai
9d0b0 6e 65 64 20 66 72 6f 6d 20 2f 64 65 76 2f 72 61  ned from /dev/ra
9d0c0 6e 64 6f 6d 2e 20 20 49 74 20 69 73 20 75 73 65  ndom.  It is use
9d0d0 64 20 6f 6e 6c 79 20 61 73 20 61 20 73 61 6e 69  d only as a sani
9d0e0 74 79 20 63 68 65 63 6b 2e 0a 2a 2a 0a 2a 2a 20  ty check..**.** 
9d0f0 53 69 6e 63 65 20 76 65 72 73 69 6f 6e 20 32 2e  Since version 2.
9d100 38 2e 30 2c 20 74 68 65 20 6a 6f 75 72 6e 61 6c  8.0, the journal
9d110 20 66 6f 72 6d 61 74 20 63 6f 6e 74 61 69 6e 73   format contains
9d120 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 61 6e 69   additional sani
9d130 74 79 0a 2a 2a 20 63 68 65 63 6b 69 6e 67 20 69  ty.** checking i
9d140 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 49 66 20  nformation.  If 
9d150 74 68 65 20 70 6f 77 65 72 20 66 61 69 6c 73 20  the power fails 
9d160 77 68 69 6c 65 20 74 68 65 20 6a 6f 75 72 6e 61  while the journa
9d170 6c 20 69 73 20 62 65 67 69 6e 0a 2a 2a 20 77 72  l is begin.** wr
9d180 69 74 74 65 6e 2c 20 73 65 6d 69 2d 72 61 6e 64  itten, semi-rand
9d190 6f 6d 20 67 61 72 62 61 67 65 20 64 61 74 61 20  om garbage data 
9d1a0 6d 69 67 68 74 20 61 70 70 65 61 72 20 69 6e 20  might appear in 
9d1b0 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66  the journal.** f
9d1c0 69 6c 65 20 61 66 74 65 72 20 70 6f 77 65 72 20  ile after power 
9d1d0 69 73 20 72 65 73 74 6f 72 65 64 2e 20 20 49 66  is restored.  If
9d1e0 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 74   an attempt is t
9d1f0 68 65 6e 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 72  hen made.** to r
9d200 6f 6c 6c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  oll the journal 
9d210 62 61 63 6b 2c 20 74 68 65 20 64 61 74 61 62 61  back, the databa
9d220 73 65 20 63 6f 75 6c 64 20 62 65 20 63 6f 72 72  se could be corr
9d230 75 70 74 65 64 2e 20 20 54 68 65 20 61 64 64 69  upted.  The addi
9d240 74 69 6f 6e 61 6c 0a 2a 2a 20 73 61 6e 69 74 79  tional.** sanity
9d250 20 63 68 65 63 6b 69 6e 67 20 64 61 74 61 20 69   checking data i
9d260 73 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20  s an attempt to 
9d270 64 69 73 63 6f 76 65 72 20 74 68 65 20 67 61 72  discover the gar
9d280 62 61 67 65 20 69 6e 20 74 68 65 0a 2a 2a 20 6a  bage in the.** j
9d290 6f 75 72 6e 61 6c 20 61 6e 64 20 69 67 6e 6f 72  ournal and ignor
9d2a0 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  e it..**.** The 
9d2b0 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20  sanity checking 
9d2c0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20  information for 
9d2d0 74 68 65 20 6e 65 77 20 6a 6f 75 72 6e 61 6c 20  the new journal 
9d2e0 66 6f 72 6d 61 74 20 63 6f 6e 73 69 73 74 73 0a  format consists.
9d2f0 2a 2a 20 6f 66 20 61 20 33 32 2d 62 69 74 20 63  ** of a 32-bit c
9d300 68 65 63 6b 73 75 6d 20 6f 6e 20 65 61 63 68 20  hecksum on each 
9d310 70 61 67 65 20 6f 66 20 64 61 74 61 2e 20 20 54  page of data.  T
9d320 68 65 20 63 68 65 63 6b 73 75 6d 20 63 6f 76 65  he checksum cove
9d330 72 73 20 62 6f 74 68 0a 2a 2a 20 74 68 65 20 70  rs both.** the p
9d340 61 67 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 74  age number and t
9d350 68 65 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53  he pPager->pageS
9d360 69 7a 65 20 62 79 74 65 73 20 6f 66 20 64 61 74  ize bytes of dat
9d370 61 20 66 6f 72 20 74 68 65 20 70 61 67 65 2e 0a  a for the page..
9d380 2a 2a 20 54 68 69 73 20 63 6b 73 75 6d 20 69 73  ** This cksum is
9d390 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20   initialized to 
9d3a0 61 20 33 32 2d 62 69 74 20 72 61 6e 64 6f 6d 20  a 32-bit random 
9d3b0 76 61 6c 75 65 20 74 68 61 74 20 61 70 70 65 61  value that appea
9d3c0 72 73 20 69 6e 20 74 68 65 0a 2a 2a 20 6a 6f 75  rs in the.** jou
9d3d0 72 6e 61 6c 20 66 69 6c 65 20 72 69 67 68 74 20  rnal file right 
9d3e0 61 66 74 65 72 20 74 68 65 20 68 65 61 64 65 72  after the header
9d3f0 2e 20 20 54 68 65 20 72 61 6e 64 6f 6d 20 69 6e  .  The random in
9d400 69 74 69 61 6c 69 7a 65 72 20 69 73 20 69 6d 70  itializer is imp
9d410 6f 72 74 61 6e 74 2c 0a 2a 2a 20 62 65 63 61 75  ortant,.** becau
9d420 73 65 20 67 61 72 62 61 67 65 20 64 61 74 61 20  se garbage data 
9d430 74 68 61 74 20 61 70 70 65 61 72 73 20 61 74 20  that appears at 
9d440 74 68 65 20 65 6e 64 20 6f 66 20 61 20 6a 6f 75  the end of a jou
9d450 72 6e 61 6c 20 69 73 20 6c 69 6b 65 6c 79 0a 2a  rnal is likely.*
9d460 2a 20 64 61 74 61 20 74 68 61 74 20 77 61 73 20  * data that was 
9d470 6f 6e 63 65 20 69 6e 20 6f 74 68 65 72 20 66 69  once in other fi
9d480 6c 65 73 20 74 68 61 74 20 68 61 76 65 20 6e 6f  les that have no
9d490 77 20 62 65 65 6e 20 64 65 6c 65 74 65 64 2e 20  w been deleted. 
9d4a0 20 49 66 20 74 68 65 0a 2a 2a 20 67 61 72 62 61   If the.** garba
9d4b0 67 65 20 64 61 74 61 20 63 61 6d 65 20 66 72 6f  ge data came fro
9d4c0 6d 20 61 6e 20 6f 62 73 6f 6c 65 74 65 20 6a 6f  m an obsolete jo
9d4d0 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68 65 20  urnal file, the 
9d4e0 63 68 65 63 6b 73 75 6d 73 20 6d 69 67 68 74 0a  checksums might.
9d4f0 2a 2a 20 62 65 20 63 6f 72 72 65 63 74 2e 20 20  ** be correct.  
9d500 42 75 74 20 62 79 20 69 6e 69 74 69 61 6c 69 7a  But by initializ
9d510 69 6e 67 20 74 68 65 20 63 68 65 63 6b 73 75 6d  ing the checksum
9d520 20 74 6f 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65   to random value
9d530 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 64 69 66   which.** is dif
9d540 66 65 72 65 6e 74 20 66 6f 72 20 65 76 65 72 79  ferent for every
9d550 20 6a 6f 75 72 6e 61 6c 2c 20 77 65 20 6d 69 6e   journal, we min
9d560 69 6d 69 7a 65 20 74 68 61 74 20 72 69 73 6b 2e  imize that risk.
9d570 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .*/.static const
9d580 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61   unsigned char a
9d590 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 5b 5d 20 3d  JournalMagic[] =
9d5a0 20 7b 0a 20 20 30 78 64 39 2c 20 30 78 64 35 2c   {.  0xd9, 0xd5,
9d5b0 20 30 78 30 35 2c 20 30 78 66 39 2c 20 30 78 32   0x05, 0xf9, 0x2
9d5c0 30 2c 20 30 78 61 31 2c 20 30 78 36 33 2c 20 30  0, 0xa1, 0x63, 0
9d5d0 78 64 37 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54  xd7,.};../*.** T
9d5e0 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 68  he size of the h
9d5f0 65 61 64 65 72 20 61 6e 64 20 6f 66 20 65 61 63  eader and of eac
9d600 68 20 70 61 67 65 20 69 6e 20 74 68 65 20 6a 6f  h page in the jo
9d610 75 72 6e 61 6c 20 69 73 20 64 65 74 65 72 6d 69  urnal is determi
9d620 6e 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 66 6f  ned.** by the fo
9d630 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 2e 0a  llowing macros..
9d640 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e  */.#define JOURN
9d650 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29  AL_PG_SZ(pPager)
9d660 20 20 28 28 70 50 61 67 65 72 2d 3e 70 61 67 65    ((pPager->page
9d670 53 69 7a 65 29 20 2b 20 38 29 0a 0a 2f 2a 0a 2a  Size) + 8)../*.*
9d680 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65  * The journal he
9d690 61 64 65 72 20 73 69 7a 65 20 66 6f 72 20 74 68  ader size for th
9d6a0 69 73 20 70 61 67 65 72 2e 20 49 6e 20 74 68 65  is pager. In the
9d6b0 20 66 75 74 75 72 65 2c 20 74 68 69 73 20 63 6f   future, this co
9d6c0 75 6c 64 20 62 65 0a 2a 2a 20 73 65 74 20 74 6f  uld be.** set to
9d6d0 20 73 6f 6d 65 20 76 61 6c 75 65 20 72 65 61 64   some value read
9d6e0 20 66 72 6f 6d 20 74 68 65 20 64 69 73 6b 20 63   from the disk c
9d6f0 6f 6e 74 72 6f 6c 6c 65 72 2e 20 54 68 65 20 69  ontroller. The i
9d700 6d 70 6f 72 74 61 6e 74 0a 2a 2a 20 63 68 61 72  mportant.** char
9d710 61 63 74 65 72 69 73 74 69 63 20 69 73 20 74 68  acteristic is th
9d720 61 74 20 69 74 20 69 73 20 74 68 65 20 73 61 6d  at it is the sam
9d730 65 20 73 69 7a 65 20 61 73 20 61 20 64 69 73 6b  e size as a disk
9d740 20 73 65 63 74 6f 72 2e 0a 2a 2f 0a 23 64 65 66   sector..*/.#def
9d750 69 6e 65 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f  ine JOURNAL_HDR_
9d760 53 5a 28 70 50 61 67 65 72 29 20 28 70 50 61 67  SZ(pPager) (pPag
9d770 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 29 0a  er->sectorSize).
9d780 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 63 72 6f  ./*.** The macro
9d790 20 4d 45 4d 44 42 20 69 73 20 74 72 75 65 20 69   MEMDB is true i
9d7a0 66 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67  f we are dealing
9d7b0 20 77 69 74 68 20 61 6e 20 69 6e 2d 6d 65 6d 6f   with an in-memo
9d7c0 72 79 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20  ry database..** 
9d7d0 57 65 20 64 6f 20 74 68 69 73 20 61 73 20 61 20  We do this as a 
9d7e0 6d 61 63 72 6f 20 73 6f 20 74 68 61 74 20 69 66  macro so that if
9d7f0 20 74 68 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54   the SQLITE_OMIT
9d800 5f 4d 45 4d 4f 52 59 44 42 20 6d 61 63 72 6f 20  _MEMORYDB macro 
9d810 69 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 20 76  is set,.** the v
9d820 61 6c 75 65 20 6f 66 20 4d 45 4d 44 42 20 77 69  alue of MEMDB wi
9d830 6c 6c 20 62 65 20 61 20 63 6f 6e 73 74 61 6e 74  ll be a constant
9d840 20 61 6e 64 20 74 68 65 20 63 6f 6d 70 69 6c 65   and the compile
9d850 72 20 77 69 6c 6c 20 6f 70 74 69 6d 69 7a 65 0a  r will optimize.
9d860 2a 2a 20 6f 75 74 20 63 6f 64 65 20 74 68 61 74  ** out code that
9d870 20 77 6f 75 6c 64 20 6e 65 76 65 72 20 65 78 65   would never exe
9d880 63 75 74 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  cute..*/.#ifdef 
9d890 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f  SQLITE_OMIT_MEMO
9d8a0 52 59 44 42 0a 23 20 64 65 66 69 6e 65 20 4d 45  RYDB.# define ME
9d8b0 4d 44 42 20 30 0a 23 65 6c 73 65 0a 23 20 64 65  MDB 0.#else.# de
9d8c0 66 69 6e 65 20 4d 45 4d 44 42 20 70 50 61 67 65  fine MEMDB pPage
9d8d0 72 2d 3e 6d 65 6d 44 62 0a 23 65 6e 64 69 66 0a  r->memDb.#endif.
9d8e0 0a 2f 2a 0a 2a 2a 20 50 61 67 65 20 6e 75 6d 62  ./*.** Page numb
9d8f0 65 72 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f  er PAGER_MJ_PGNO
9d900 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 20 69   is never used i
9d910 6e 20 61 6e 20 53 51 4c 69 74 65 20 64 61 74 61  n an SQLite data
9d920 62 61 73 65 20 28 69 74 20 69 73 0a 2a 2a 20 72  base (it is.** r
9d930 65 73 65 72 76 65 64 20 66 6f 72 20 77 6f 72 6b  eserved for work
9d940 69 6e 67 20 61 72 6f 75 6e 64 20 61 20 77 69 6e  ing around a win
9d950 64 6f 77 73 2f 70 6f 73 69 78 20 69 6e 63 6f 6d  dows/posix incom
9d960 70 61 74 69 62 69 6c 69 74 79 29 2e 20 49 74 20  patibility). It 
9d970 69 73 0a 2a 2a 20 75 73 65 64 20 69 6e 20 74 68  is.** used in th
9d980 65 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 73 69 67  e journal to sig
9d990 6e 69 66 79 20 74 68 61 74 20 74 68 65 20 72 65  nify that the re
9d9a0 6d 61 69 6e 64 65 72 20 6f 66 20 74 68 65 20 6a  mainder of the j
9d9b0 6f 75 72 6e 61 6c 20 66 69 6c 65 20 0a 2a 2a 20  ournal file .** 
9d9c0 69 73 20 64 65 76 6f 74 65 64 20 74 6f 20 73 74  is devoted to st
9d9d0 6f 72 69 6e 67 20 61 20 6d 61 73 74 65 72 20 6a  oring a master j
9d9e0 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 2d 20 74 68  ournal name - th
9d9f0 65 72 65 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20  ere are no more 
9da00 70 61 67 65 73 20 74 6f 0a 2a 2a 20 72 6f 6c 6c  pages to.** roll
9da10 20 62 61 63 6b 2e 20 53 65 65 20 63 6f 6d 6d 65   back. See comme
9da20 6e 74 73 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e  nts for function
9da30 20 77 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 72   writeMasterJour
9da40 6e 61 6c 28 29 20 66 6f 72 20 64 65 74 61 69 6c  nal() for detail
9da50 73 2e 0a 2a 2f 0a 2f 2a 20 23 64 65 66 69 6e 65  s..*/./* #define
9da60 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 78   PAGER_MJ_PGNO(x
9da70 29 20 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 2f  ) (PENDING_BYTE/
9da80 28 28 78 29 2d 3e 70 61 67 65 53 69 7a 65 29 29  ((x)->pageSize))
9da90 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45   */.#define PAGE
9daa0 52 5f 4d 4a 5f 50 47 4e 4f 28 78 29 20 28 28 50  R_MJ_PGNO(x) ((P
9dab0 45 4e 44 49 4e 47 5f 42 59 54 45 2f 28 28 78 29  ENDING_BYTE/((x)
9dac0 2d 3e 70 61 67 65 53 69 7a 65 29 29 2b 31 29 0a  ->pageSize))+1).
9dad0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d  ./*.** The maxim
9dae0 75 6d 20 6c 65 67 61 6c 20 70 61 67 65 20 6e 75  um legal page nu
9daf0 6d 62 65 72 20 69 73 20 28 32 5e 33 31 20 2d 20  mber is (2^31 - 
9db00 31 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50  1)..*/.#define P
9db10 41 47 45 52 5f 4d 41 58 5f 50 47 4e 4f 20 32 31  AGER_MAX_PGNO 21
9db20 34 37 34 38 33 36 34 37 0a 0a 2f 2a 0a 2a 2a 20  47483647../*.** 
9db30 45 6e 61 62 6c 65 20 72 65 66 65 72 65 6e 63 65  Enable reference
9db40 20 63 6f 75 6e 74 20 74 72 61 63 6b 69 6e 67 20   count tracking 
9db50 28 66 6f 72 20 64 65 62 75 67 67 69 6e 67 29 20  (for debugging) 
9db60 68 65 72 65 3a 0a 2a 2f 0a 23 69 66 64 65 66 20  here:.*/.#ifdef 
9db70 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 69  SQLITE_DEBUG.  i
9db80 6e 74 20 70 61 67 65 72 33 5f 72 65 66 69 6e 66  nt pager3_refinf
9db90 6f 5f 65 6e 61 62 6c 65 20 3d 20 30 3b 0a 20 20  o_enable = 0;.  
9dba0 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65  static void page
9dbb0 72 5f 72 65 66 69 6e 66 6f 28 50 67 48 64 72 20  r_refinfo(PgHdr 
9dbc0 2a 70 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20  *p){.    static 
9dbd0 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20  int cnt = 0;.   
9dbe0 20 69 66 28 20 21 70 61 67 65 72 33 5f 72 65 66   if( !pager3_ref
9dbf0 69 6e 66 6f 5f 65 6e 61 62 6c 65 20 29 20 72 65  info_enable ) re
9dc00 74 75 72 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65  turn;.    sqlite
9dc10 33 44 65 62 75 67 50 72 69 6e 74 66 28 0a 20 20  3DebugPrintf(.  
9dc20 20 20 20 20 20 22 52 45 46 43 4e 54 3a 20 25 34       "REFCNT: %4
9dc30 64 20 61 64 64 72 3d 25 70 20 6e 52 65 66 3d 25  d addr=%p nRef=%
9dc40 2d 33 64 20 74 6f 74 61 6c 3d 25 64 5c 6e 22 2c  -3d total=%d\n",
9dc50 0a 20 20 20 20 20 20 20 70 2d 3e 70 67 6e 6f 2c  .       p->pgno,
9dc60 20 50 47 48 44 52 5f 54 4f 5f 44 41 54 41 28 70   PGHDR_TO_DATA(p
9dc70 29 2c 20 70 2d 3e 6e 52 65 66 2c 20 70 2d 3e 70  ), p->nRef, p->p
9dc80 50 61 67 65 72 2d 3e 6e 52 65 66 0a 20 20 20 20  Pager->nRef.    
9dc90 29 3b 0a 20 20 20 20 63 6e 74 2b 2b 3b 20 20 20  );.    cnt++;   
9dca0 2f 2a 20 53 6f 6d 65 74 68 69 6e 67 20 74 6f 20  /* Something to 
9dcb0 73 65 74 20 61 20 62 72 65 61 6b 70 6f 69 6e 74  set a breakpoint
9dcc0 20 6f 6e 20 2a 2f 0a 20 20 7d 0a 23 20 64 65 66   on */.  }.# def
9dcd0 69 6e 65 20 52 45 46 49 4e 46 4f 28 58 29 20 20  ine REFINFO(X)  
9dce0 70 61 67 65 72 5f 72 65 66 69 6e 66 6f 28 58 29  pager_refinfo(X)
9dcf0 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
9dd00 52 45 46 49 4e 46 4f 28 58 29 0a 23 65 6e 64 69  REFINFO(X).#endi
9dd10 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  f../*.** Return 
9dd20 74 72 75 65 20 69 66 20 70 61 67 65 20 2a 70 50  true if page *pP
9dd30 67 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  g has already be
9dd40 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68  en written to th
9dd50 65 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 6a  e statement.** j
9dd60 6f 75 72 6e 61 6c 20 28 6f 72 20 73 74 61 74 65  ournal (or state
9dd70 6d 65 6e 74 20 73 6e 61 70 73 68 6f 74 20 68 61  ment snapshot ha
9dd80 73 20 62 65 65 6e 20 63 72 65 61 74 65 64 2c 20  s been created, 
9dd90 69 66 20 2a 70 50 67 20 69 73 20 70 61 72 74 0a  if *pPg is part.
9dda0 2a 2a 20 6f 66 20 61 6e 20 69 6e 2d 6d 65 6d 6f  ** of an in-memo
9ddb0 72 79 20 64 61 74 61 62 61 73 65 29 2e 0a 2a 2f  ry database)..*/
9ddc0 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65  .static int page
9ddd0 49 6e 53 74 61 74 65 6d 65 6e 74 28 50 67 48 64  InStatement(PgHd
9dde0 72 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72  r *pPg){.  Pager
9ddf0 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e   *pPager = pPg->
9de00 70 50 61 67 65 72 3b 0a 20 20 69 66 28 20 4d 45  pPager;.  if( ME
9de10 4d 44 42 20 29 7b 0a 20 20 20 20 72 65 74 75 72  MDB ){.    retur
9de20 6e 20 50 47 48 44 52 5f 54 4f 5f 48 49 53 54 28  n PGHDR_TO_HIST(
9de30 70 50 67 2c 20 70 50 61 67 65 72 29 2d 3e 69 6e  pPg, pPager)->in
9de40 53 74 6d 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Stmt;.  }else{. 
9de50 20 20 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70     Pgno pgno = p
9de60 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 75 38  Pg->pgno;.    u8
9de70 20 2a 61 20 3d 20 70 50 61 67 65 72 2d 3e 61 49   *a = pPager->aI
9de80 6e 53 74 6d 74 3b 0a 20 20 20 20 72 65 74 75 72  nStmt;.    retur
9de90 6e 20 28 61 20 26 26 20 28 69 6e 74 29 70 67 6e  n (a && (int)pgn
9dea0 6f 3c 3d 70 50 61 67 65 72 2d 3e 73 74 6d 74 53  o<=pPager->stmtS
9deb0 69 7a 65 20 26 26 20 28 61 5b 70 67 6e 6f 2f 38  ize && (a[pgno/8
9dec0 5d 20 26 20 28 31 3c 3c 28 70 67 6e 6f 26 37 29  ] & (1<<(pgno&7)
9ded0 29 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  )));.  }.}../*.*
9dee0 2a 20 43 68 61 6e 67 65 20 74 68 65 20 73 69 7a  * Change the siz
9def0 65 20 6f 66 20 74 68 65 20 70 61 67 65 72 20 68  e of the pager h
9df00 61 73 68 20 74 61 62 6c 65 20 74 6f 20 4e 2e 20  ash table to N. 
9df10 20 4e 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77   N must be a pow
9df20 65 72 0a 2a 2a 20 6f 66 20 74 77 6f 2e 0a 2a 2f  er.** of two..*/
9df30 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67  .static void pag
9df40 65 72 5f 72 65 73 69 7a 65 5f 68 61 73 68 5f 74  er_resize_hash_t
9df50 61 62 6c 65 28 50 61 67 65 72 20 2a 70 50 61 67  able(Pager *pPag
9df60 65 72 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 50 67  er, int N){.  Pg
9df70 48 64 72 20 2a 2a 61 48 61 73 68 2c 20 2a 70 50  Hdr **aHash, *pP
9df80 67 3b 0a 20 20 61 73 73 65 72 74 28 20 4e 3e 30  g;.  assert( N>0
9df90 20 26 26 20 28 4e 26 28 4e 2d 31 29 29 3d 3d 30   && (N&(N-1))==0
9dfa0 20 29 3b 0a 20 20 61 48 61 73 68 20 3d 20 73 71   );.  aHash = sq
9dfb0 6c 69 74 65 4d 61 6c 6c 6f 63 28 20 73 69 7a 65  liteMalloc( size
9dfc0 6f 66 28 61 48 61 73 68 5b 30 5d 29 2a 4e 20 29  of(aHash[0])*N )
9dfd0 3b 0a 20 20 69 66 28 20 61 48 61 73 68 3d 3d 30  ;.  if( aHash==0
9dfe0 20 29 7b 0a 20 20 20 20 2f 2a 20 46 61 69 6c 75   ){.    /* Failu
9dff0 72 65 20 74 6f 20 72 65 68 61 73 68 20 69 73 20  re to rehash is 
9e000 6e 6f 74 20 61 6e 20 65 72 72 6f 72 2e 20 20 49  not an error.  I
9e010 74 20 69 73 20 6f 6e 6c 79 20 61 20 70 65 72 66  t is only a perf
9e020 6f 72 6d 61 6e 63 65 20 68 69 74 2e 20 2a 2f 0a  ormance hit. */.
9e030 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
9e040 20 20 73 71 6c 69 74 65 46 72 65 65 28 70 50 61    sqliteFree(pPa
9e050 67 65 72 2d 3e 61 48 61 73 68 29 3b 0a 20 20 70  ger->aHash);.  p
9e060 50 61 67 65 72 2d 3e 6e 48 61 73 68 20 3d 20 4e  Pager->nHash = N
9e070 3b 0a 20 20 70 50 61 67 65 72 2d 3e 61 48 61 73  ;.  pPager->aHas
9e080 68 20 3d 20 61 48 61 73 68 3b 0a 20 20 66 6f 72  h = aHash;.  for
9e090 28 70 50 67 3d 70 50 61 67 65 72 2d 3e 70 41 6c  (pPg=pPager->pAl
9e0a0 6c 3b 20 70 50 67 3b 20 70 50 67 3d 70 50 67 2d  l; pPg; pPg=pPg-
9e0b0 3e 70 4e 65 78 74 41 6c 6c 29 7b 0a 20 20 20 20  >pNextAll){.    
9e0c0 69 6e 74 20 68 3b 0a 20 20 20 20 69 66 28 20 70  int h;.    if( p
9e0d0 50 67 2d 3e 70 67 6e 6f 3d 3d 30 20 29 7b 0a 20  Pg->pgno==0 ){. 
9e0e0 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 67       assert( pPg
9e0f0 2d 3e 70 4e 65 78 74 48 61 73 68 3d 3d 30 20 26  ->pNextHash==0 &
9e100 26 20 70 50 67 2d 3e 70 50 72 65 76 48 61 73 68  & pPg->pPrevHash
9e110 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 63 6f 6e  ==0 );.      con
9e120 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20  tinue;.    }.   
9e130 20 68 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 20 26   h = pPg->pgno &
9e140 20 28 4e 2d 31 29 3b 0a 20 20 20 20 70 50 67 2d   (N-1);.    pPg-
9e150 3e 70 4e 65 78 74 48 61 73 68 20 3d 20 61 48 61  >pNextHash = aHa
9e160 73 68 5b 68 5d 3b 0a 20 20 20 20 69 66 28 20 61  sh[h];.    if( a
9e170 48 61 73 68 5b 68 5d 20 29 7b 0a 20 20 20 20 20  Hash[h] ){.     
9e180 20 61 48 61 73 68 5b 68 5d 2d 3e 70 50 72 65 76   aHash[h]->pPrev
9e190 48 61 73 68 20 3d 20 70 50 67 3b 0a 20 20 20 20  Hash = pPg;.    
9e1a0 7d 0a 20 20 20 20 61 48 61 73 68 5b 68 5d 20 3d  }.    aHash[h] =
9e1b0 20 70 50 67 3b 0a 20 20 20 20 70 50 67 2d 3e 70   pPg;.    pPg->p
9e1c0 50 72 65 76 48 61 73 68 20 3d 20 30 3b 0a 20 20  PrevHash = 0;.  
9e1d0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20  }.}../*.** Read 
9e1e0 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72  a 32-bit integer
9e1f0 20 66 72 6f 6d 20 74 68 65 20 67 69 76 65 6e 20   from the given 
9e200 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e  file descriptor.
9e210 20 20 53 74 6f 72 65 20 74 68 65 20 69 6e 74 65    Store the inte
9e220 67 65 72 0a 2a 2a 20 74 68 61 74 20 69 73 20 72  ger.** that is r
9e230 65 61 64 20 69 6e 20 2a 70 52 65 73 2e 20 20 52  ead in *pRes.  R
9e240 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
9e250 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f  if everything wo
9e260 72 6b 65 64 2c 20 6f 72 20 61 6e 0a 2a 2a 20 65  rked, or an.** e
9e270 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 6f 6d  rror code is som
9e280 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e  ething goes wron
9e290 67 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 76 61 6c  g..**.** All val
9e2a0 75 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f  ues are stored o
9e2b0 6e 20 64 69 73 6b 20 61 73 20 62 69 67 2d 65 6e  n disk as big-en
9e2c0 64 69 61 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  dian..*/.static 
9e2d0 69 6e 74 20 72 65 61 64 33 32 62 69 74 73 28 4f  int read32bits(O
9e2e0 73 46 69 6c 65 20 2a 66 64 2c 20 75 33 32 20 2a  sFile *fd, u32 *
9e2f0 70 52 65 73 29 7b 0a 20 20 75 6e 73 69 67 6e 65  pRes){.  unsigne
9e300 64 20 63 68 61 72 20 61 63 5b 34 5d 3b 0a 20 20  d char ac[4];.  
9e310 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33  int rc = sqlite3
9e320 4f 73 52 65 61 64 28 66 64 2c 20 61 63 2c 20 73  OsRead(fd, ac, s
9e330 69 7a 65 6f 66 28 61 63 29 29 3b 0a 20 20 69 66  izeof(ac));.  if
9e340 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
9e350 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 73  ){.    *pRes = s
9e360 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 61  qlite3Get4byte(a
9e370 63 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  c);.  }.  return
9e380 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72   rc;.}../*.** Wr
9e390 69 74 65 20 61 20 33 32 2d 62 69 74 20 69 6e 74  ite a 32-bit int
9e3a0 65 67 65 72 20 69 6e 74 6f 20 61 20 73 74 72 69  eger into a stri
9e3b0 6e 67 20 62 75 66 66 65 72 20 69 6e 20 62 69 67  ng buffer in big
9e3c0 2d 65 6e 64 69 61 6e 20 62 79 74 65 20 6f 72 64  -endian byte ord
9e3d0 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 70  er..*/.#define p
9e3e0 75 74 33 32 62 69 74 73 28 41 2c 42 29 20 20 73  ut32bits(A,B)  s
9e3f0 71 6c 69 74 65 33 50 75 74 34 62 79 74 65 28 28  qlite3Put4byte((
9e400 75 38 2a 29 41 2c 42 29 0a 0a 2f 2a 0a 2a 2a 20  u8*)A,B)../*.** 
9e410 57 72 69 74 65 20 61 20 33 32 2d 62 69 74 20 69  Write a 32-bit i
9e420 6e 74 65 67 65 72 20 69 6e 74 6f 20 74 68 65 20  nteger into the 
9e430 67 69 76 65 6e 20 66 69 6c 65 20 64 65 73 63 72  given file descr
9e440 69 70 74 6f 72 2e 20 20 52 65 74 75 72 6e 20 53  iptor.  Return S
9e450 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73  QLITE_OK.** on s
9e460 75 63 63 65 73 73 20 6f 72 20 61 6e 20 65 72 72  uccess or an err
9e470 6f 72 20 63 6f 64 65 20 69 73 20 73 6f 6d 65 74  or code is somet
9e480 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e  hing goes wrong.
9e490 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
9e4a0 72 69 74 65 33 32 62 69 74 73 28 4f 73 46 69 6c  rite32bits(OsFil
9e4b0 65 20 2a 66 64 2c 20 75 33 32 20 76 61 6c 29 7b  e *fd, u32 val){
9e4c0 0a 20 20 63 68 61 72 20 61 63 5b 34 5d 3b 0a 20  .  char ac[4];. 
9e4d0 20 70 75 74 33 32 62 69 74 73 28 61 63 2c 20 76   put32bits(ac, v
9e4e0 61 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71  al);.  return sq
9e4f0 6c 69 74 65 33 4f 73 57 72 69 74 65 28 66 64 2c  lite3OsWrite(fd,
9e500 20 61 63 2c 20 34 29 3b 0a 7d 0a 0a 2f 2a 0a 2a   ac, 4);.}../*.*
9e510 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
9e520 73 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64  should be called
9e530 20 77 68 65 6e 20 61 6e 20 65 72 72 6f 72 20 6f   when an error o
9e540 63 63 75 72 73 20 77 69 74 68 69 6e 20 74 68 65  ccurs within the
9e550 20 70 61 67 65 72 0a 2a 2a 20 63 6f 64 65 2e 20   pager.** code. 
9e560 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  The first argume
9e570 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  nt is a pointer 
9e580 74 6f 20 74 68 65 20 70 61 67 65 72 20 73 74 72  to the pager str
9e590 75 63 74 75 72 65 2c 20 74 68 65 0a 2a 2a 20 73  ucture, the.** s
9e5a0 65 63 6f 6e 64 20 74 68 65 20 65 72 72 6f 72 2d  econd the error-
9e5b0 63 6f 64 65 20 61 62 6f 75 74 20 74 6f 20 62 65  code about to be
9e5c0 20 72 65 74 75 72 6e 65 64 20 62 79 20 61 20 70   returned by a p
9e5d0 61 67 65 72 20 41 50 49 20 66 75 6e 63 74 69 6f  ager API functio
9e5e0 6e 2e 20 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65  n. .** The value
9e5f0 20 72 65 74 75 72 6e 65 64 20 69 73 20 61 20 63   returned is a c
9e600 6f 70 79 20 6f 66 20 74 68 65 20 73 65 63 6f 6e  opy of the secon
9e610 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  d argument to th
9e620 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 0a 2a 2a  is function. .**
9e630 0a 2a 2a 20 49 66 20 74 68 65 20 73 65 63 6f 6e  .** If the secon
9e640 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53 51  d argument is SQ
9e650 4c 49 54 45 5f 49 4f 45 52 52 2c 20 53 51 4c 49  LITE_IOERR, SQLI
9e660 54 45 5f 43 4f 52 52 55 50 54 2c 20 6f 72 20 53  TE_CORRUPT, or S
9e670 51 4c 49 54 45 5f 46 55 4c 4c 0a 2a 2a 20 74 68  QLITE_FULL.** th
9e680 65 20 65 72 72 6f 72 20 62 65 63 6f 6d 65 73 20  e error becomes 
9e690 70 65 72 73 69 73 74 65 6e 74 2e 20 41 6c 6c 20  persistent. All 
9e6a0 73 75 62 73 65 71 75 65 6e 74 20 41 50 49 20 63  subsequent API c
9e6b0 61 6c 6c 73 20 6f 6e 20 74 68 69 73 20 50 61 67  alls on this Pag
9e6c0 65 72 0a 2a 2a 20 77 69 6c 6c 20 69 6d 6d 65 64  er.** will immed
9e6d0 69 61 74 65 6c 79 20 72 65 74 75 72 6e 20 74 68  iately return th
9e6e0 65 20 73 61 6d 65 20 65 72 72 6f 72 20 63 6f 64  e same error cod
9e6f0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
9e700 20 70 61 67 65 72 5f 65 72 72 6f 72 28 50 61 67   pager_error(Pag
9e710 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20  er *pPager, int 
9e720 72 63 29 7b 0a 20 20 69 6e 74 20 72 63 32 20 3d  rc){.  int rc2 =
9e730 20 72 63 20 26 20 30 78 66 66 3b 0a 20 20 61 73   rc & 0xff;.  as
9e740 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 72  sert( pPager->er
9e750 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 46 55  rCode==SQLITE_FU
9e760 4c 4c 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65 72  LL || pPager->er
9e770 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b  rCode==SQLITE_OK
9e780 20 29 3b 0a 20 20 69 66 28 0a 20 20 20 20 72 63   );.  if(.    rc
9e790 32 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 7c  2==SQLITE_FULL |
9e7a0 7c 0a 20 20 20 20 72 63 32 3d 3d 53 51 4c 49 54  |.    rc2==SQLIT
9e7b0 45 5f 49 4f 45 52 52 20 7c 7c 0a 20 20 20 20 72  E_IOERR ||.    r
9e7c0 63 32 3d 3d 53 51 4c 49 54 45 5f 43 4f 52 52 55  c2==SQLITE_CORRU
9e7d0 50 54 0a 20 20 29 7b 0a 20 20 20 20 70 50 61 67  PT.  ){.    pPag
9e7e0 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 63  er->errCode = rc
9e7f0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
9e800 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53  c;.}../*.** If S
9e810 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45  QLITE_CHECK_PAGE
9e820 53 20 69 73 20 64 65 66 69 6e 65 64 20 74 68 65  S is defined the
9e830 6e 20 77 65 20 64 6f 20 73 6f 6d 65 20 73 61 6e  n we do some san
9e840 69 74 79 20 63 68 65 63 6b 69 6e 67 0a 2a 2a 20  ity checking.** 
9e850 6f 6e 20 74 68 65 20 63 61 63 68 65 20 75 73 69  on the cache usi
9e860 6e 67 20 61 20 68 61 73 68 20 66 75 6e 63 74 69  ng a hash functi
9e870 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 75 73 65  on.  This is use
9e880 64 20 66 6f 72 20 74 65 73 74 69 6e 67 0a 2a 2a  d for testing.**
9e890 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20 6f   and debugging o
9e8a0 6e 6c 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  nly..*/.#ifdef S
9e8b0 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45  QLITE_CHECK_PAGE
9e8c0 53 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  S./*.** Return a
9e8d0 20 33 32 2d 62 69 74 20 68 61 73 68 20 6f 66 20   32-bit hash of 
9e8e0 74 68 65 20 70 61 67 65 20 64 61 74 61 20 66 6f  the page data fo
9e8f0 72 20 70 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 74  r pPage..*/.stat
9e900 69 63 20 75 33 32 20 70 61 67 65 72 5f 64 61 74  ic u32 pager_dat
9e910 61 68 61 73 68 28 69 6e 74 20 6e 42 79 74 65 2c  ahash(int nByte,
9e920 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
9e930 70 44 61 74 61 29 7b 0a 20 20 75 33 32 20 68 61  pData){.  u32 ha
9e940 73 68 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b  sh = 0;.  int i;
9e950 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 42  .  for(i=0; i<nB
9e960 79 74 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 68  yte; i++){.    h
9e970 61 73 68 20 3d 20 28 68 61 73 68 2a 31 30 33 39  ash = (hash*1039
9e980 29 20 2b 20 70 44 61 74 61 5b 69 5d 3b 0a 20 20  ) + pData[i];.  
9e990 7d 0a 20 20 72 65 74 75 72 6e 20 68 61 73 68 3b  }.  return hash;
9e9a0 0a 7d 0a 73 74 61 74 69 63 20 75 33 32 20 70 61  .}.static u32 pa
9e9b0 67 65 72 5f 70 61 67 65 68 61 73 68 28 50 67 48  ger_pagehash(PgH
9e9c0 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 72 65  dr *pPage){.  re
9e9d0 74 75 72 6e 20 70 61 67 65 72 5f 64 61 74 61 68  turn pager_datah
9e9e0 61 73 68 28 70 50 61 67 65 2d 3e 70 50 61 67 65  ash(pPage->pPage
9e9f0 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 0a 20 20  r->pageSize, .  
9ea00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9ea10 20 20 20 20 20 20 28 75 6e 73 69 67 6e 65 64 20        (unsigned 
9ea20 63 68 61 72 20 2a 29 50 47 48 44 52 5f 54 4f 5f  char *)PGHDR_TO_
9ea30 44 41 54 41 28 70 50 61 67 65 29 29 3b 0a 7d 0a  DATA(pPage));.}.
9ea40 0a 2f 2a 0a 2a 2a 20 54 68 65 20 43 48 45 43 4b  ./*.** The CHECK
9ea50 5f 50 41 47 45 20 6d 61 63 72 6f 20 74 61 6b 65  _PAGE macro take
9ea60 73 20 61 20 50 67 48 64 72 2a 20 61 73 20 61 6e  s a PgHdr* as an
9ea70 20 61 72 67 75 6d 65 6e 74 2e 20 49 66 20 53 51   argument. If SQ
9ea80 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53  LITE_CHECK_PAGES
9ea90 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 64 2c 20  .** is defined, 
9eaa0 61 6e 64 20 4e 44 45 42 55 47 20 69 73 20 6e 6f  and NDEBUG is no
9eab0 74 20 64 65 66 69 6e 65 64 2c 20 61 6e 20 61 73  t defined, an as
9eac0 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74  sert() statement
9ead0 20 63 68 65 63 6b 73 0a 2a 2a 20 74 68 61 74 20   checks.** that 
9eae0 74 68 65 20 70 61 67 65 20 69 73 20 65 69 74 68  the page is eith
9eaf0 65 72 20 64 69 72 74 79 20 6f 72 20 73 74 69 6c  er dirty or stil
9eb00 6c 20 6d 61 74 63 68 65 73 20 74 68 65 20 63 61  l matches the ca
9eb10 6c 63 75 6c 61 74 65 64 20 70 61 67 65 2d 68 61  lculated page-ha
9eb20 73 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43  sh..*/.#define C
9eb30 48 45 43 4b 5f 50 41 47 45 28 78 29 20 63 68 65  HECK_PAGE(x) che
9eb40 63 6b 50 61 67 65 28 78 29 0a 73 74 61 74 69 63  ckPage(x).static
9eb50 20 76 6f 69 64 20 63 68 65 63 6b 50 61 67 65 28   void checkPage(
9eb60 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 50  PgHdr *pPg){.  P
9eb70 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70  ager *pPager = p
9eb80 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 61 73  Pg->pPager;.  as
9eb90 73 65 72 74 28 20 21 70 50 67 2d 3e 70 61 67 65  sert( !pPg->page
9eba0 48 61 73 68 20 7c 7c 20 70 50 61 67 65 72 2d 3e  Hash || pPager->
9ebb0 65 72 72 43 6f 64 65 20 7c 7c 20 4d 45 4d 44 42  errCode || MEMDB
9ebc0 20 7c 7c 20 70 50 67 2d 3e 64 69 72 74 79 20 7c   || pPg->dirty |
9ebd0 7c 20 0a 20 20 20 20 20 20 70 50 67 2d 3e 70 61  | .      pPg->pa
9ebe0 67 65 48 61 73 68 3d 3d 70 61 67 65 72 5f 70 61  geHash==pager_pa
9ebf0 67 65 68 61 73 68 28 70 50 67 29 20 29 3b 0a 7d  gehash(pPg) );.}
9ec00 0a 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20  ..#else.#define 
9ec10 70 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 58  pager_datahash(X
9ec20 2c 59 29 20 20 30 0a 23 64 65 66 69 6e 65 20 70  ,Y)  0.#define p
9ec30 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 58 29  ager_pagehash(X)
9ec40 20 20 30 0a 23 64 65 66 69 6e 65 20 43 48 45 43    0.#define CHEC
9ec50 4b 5f 50 41 47 45 28 78 29 0a 23 65 6e 64 69 66  K_PAGE(x).#endif
9ec60 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69  ../*.** When thi
9ec70 73 20 69 73 20 63 61 6c 6c 65 64 20 74 68 65 20  s is called the 
9ec80 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72  journal file for
9ec90 20 70 61 67 65 72 20 70 50 61 67 65 72 20 6d 75   pager pPager mu
9eca0 73 74 20 62 65 20 6f 70 65 6e 2e 0a 2a 2a 20 54  st be open..** T
9ecb0 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
9ecc0 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 72  l file name is r
9ecd0 65 61 64 20 66 72 6f 6d 20 74 68 65 20 65 6e 64  ead from the end
9ece0 20 6f 66 20 74 68 65 20 66 69 6c 65 20 61 6e 64   of the file and
9ecf0 20 0a 2a 2a 20 77 72 69 74 74 65 6e 20 69 6e 74   .** written int
9ed00 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65  o memory obtaine
9ed10 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c  d from sqliteMal
9ed20 6c 6f 63 28 29 2e 20 2a 70 7a 4d 61 73 74 65 72  loc(). *pzMaster
9ed30 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 70 6f   is.** set to po
9ed40 69 6e 74 20 61 74 20 74 68 65 20 6d 65 6d 6f 72  int at the memor
9ed50 79 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20  y and SQLITE_OK 
9ed60 72 65 74 75 72 6e 65 64 2e 20 54 68 65 20 63 61  returned. The ca
9ed70 6c 6c 65 72 20 6d 75 73 74 0a 2a 2a 20 73 71 6c  ller must.** sql
9ed80 69 74 65 46 72 65 65 28 29 20 2a 70 7a 4d 61 73  iteFree() *pzMas
9ed90 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f  ter..**.** If no
9eda0 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
9edb0 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 70 72 65  file name is pre
9edc0 73 65 6e 74 20 2a 70 7a 4d 61 73 74 65 72 20 69  sent *pzMaster i
9edd0 73 20 73 65 74 20 74 6f 20 30 20 61 6e 64 0a 2a  s set to 0 and.*
9ede0 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75  * SQLITE_OK retu
9edf0 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rned..*/.static 
9ee00 69 6e 74 20 72 65 61 64 4d 61 73 74 65 72 4a 6f  int readMasterJo
9ee10 75 72 6e 61 6c 28 4f 73 46 69 6c 65 20 2a 70 4a  urnal(OsFile *pJ
9ee20 72 6e 6c 2c 20 63 68 61 72 20 2a 2a 70 7a 4d 61  rnl, char **pzMa
9ee30 73 74 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b  ster){.  int rc;
9ee40 0a 20 20 75 33 32 20 6c 65 6e 3b 0a 20 20 69 36  .  u32 len;.  i6
9ee50 34 20 73 7a 4a 3b 0a 20 20 75 33 32 20 63 6b 73  4 szJ;.  u32 cks
9ee60 75 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 75  um;.  int i;.  u
9ee70 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61 4d 61  nsigned char aMa
9ee80 67 69 63 5b 38 5d 3b 20 2f 2a 20 41 20 62 75 66  gic[8]; /* A buf
9ee90 66 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  fer to hold the 
9eea0 6d 61 67 69 63 20 68 65 61 64 65 72 20 2a 2f 0a  magic header */.
9eeb0 0a 20 20 2a 70 7a 4d 61 73 74 65 72 20 3d 20 30  .  *pzMaster = 0
9eec0 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  ;..  rc = sqlite
9eed0 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 4a 72 6e  3OsFileSize(pJrn
9eee0 6c 2c 20 26 73 7a 4a 29 3b 0a 20 20 69 66 28 20  l, &szJ);.  if( 
9eef0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc!=SQLITE_OK ||
9ef00 20 73 7a 4a 3c 31 36 20 29 20 72 65 74 75 72 6e   szJ<16 ) return
9ef10 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c   rc;..  rc = sql
9ef20 69 74 65 33 4f 73 53 65 65 6b 28 70 4a 72 6e 6c  ite3OsSeek(pJrnl
9ef30 2c 20 73 7a 4a 2d 31 36 29 3b 0a 20 20 69 66 28  , szJ-16);.  if(
9ef40 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
9ef50 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 0a 20 20   return rc;. .  
9ef60 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28  rc = read32bits(
9ef70 70 4a 72 6e 6c 2c 20 26 6c 65 6e 29 3b 0a 20 20  pJrnl, &len);.  
9ef80 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
9ef90 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
9efa0 20 20 72 63 20 3d 20 72 65 61 64 33 32 62 69 74    rc = read32bit
9efb0 73 28 70 4a 72 6e 6c 2c 20 26 63 6b 73 75 6d 29  s(pJrnl, &cksum)
9efc0 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
9efd0 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
9efe0 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  c;..  rc = sqlit
9eff0 65 33 4f 73 52 65 61 64 28 70 4a 72 6e 6c 2c 20  e3OsRead(pJrnl, 
9f000 61 4d 61 67 69 63 2c 20 38 29 3b 0a 20 20 69 66  aMagic, 8);.  if
9f010 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
9f020 7c 7c 20 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63  || memcmp(aMagic
9f030 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c  , aJournalMagic,
9f040 20 38 29 20 29 20 72 65 74 75 72 6e 20 72 63 3b   8) ) return rc;
9f050 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
9f060 4f 73 53 65 65 6b 28 70 4a 72 6e 6c 2c 20 73 7a  OsSeek(pJrnl, sz
9f070 4a 2d 31 36 2d 6c 65 6e 29 3b 0a 20 20 69 66 28  J-16-len);.  if(
9f080 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
9f090 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2a   return rc;..  *
9f0a0 70 7a 4d 61 73 74 65 72 20 3d 20 28 63 68 61 72  pzMaster = (char
9f0b0 20 2a 29 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28   *)sqliteMalloc(
9f0c0 6c 65 6e 2b 31 29 3b 0a 20 20 69 66 28 20 21 2a  len+1);.  if( !*
9f0d0 70 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20  pzMaster ){.    
9f0e0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
9f0f0 4d 45 4d 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20  MEM;.  }.  rc = 
9f100 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 4a  sqlite3OsRead(pJ
9f110 72 6e 6c 2c 20 2a 70 7a 4d 61 73 74 65 72 2c 20  rnl, *pzMaster, 
9f120 6c 65 6e 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  len);.  if( rc!=
9f130 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
9f140 20 73 71 6c 69 74 65 46 72 65 65 28 2a 70 7a 4d   sqliteFree(*pzM
9f150 61 73 74 65 72 29 3b 0a 20 20 20 20 2a 70 7a 4d  aster);.    *pzM
9f160 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 72  aster = 0;.    r
9f170 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20  eturn rc;.  }.. 
9f180 20 2f 2a 20 53 65 65 20 69 66 20 74 68 65 20 63   /* See if the c
9f190 68 65 63 6b 73 75 6d 20 6d 61 74 63 68 65 73 20  hecksum matches 
9f1a0 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
9f1b0 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20 20 66 6f 72  al name */.  for
9f1c0 28 69 3d 30 3b 20 69 3c 6c 65 6e 3b 20 69 2b 2b  (i=0; i<len; i++
9f1d0 29 7b 0a 20 20 20 20 63 6b 73 75 6d 20 2d 3d 20  ){.    cksum -= 
9f1e0 28 2a 70 7a 4d 61 73 74 65 72 29 5b 69 5d 3b 0a  (*pzMaster)[i];.
9f1f0 20 20 7d 0a 20 20 69 66 28 20 63 6b 73 75 6d 20    }.  if( cksum 
9f200 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  ){.    /* If the
9f210 20 63 68 65 63 6b 73 75 6d 20 64 6f 65 73 6e 27   checksum doesn'
9f220 74 20 61 64 64 20 75 70 2c 20 74 68 65 6e 20 6f  t add up, then o
9f230 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68  ne or more of th
9f240 65 20 64 69 73 6b 20 73 65 63 74 6f 72 73 0a 20  e disk sectors. 
9f250 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 69 6e 67     ** containing
9f260 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
9f270 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69 73 20  nal filename is 
9f280 63 6f 72 72 75 70 74 65 64 2e 20 54 68 69 73 20  corrupted. This 
9f290 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 64 65 66  means.    ** def
9f2a0 69 6e 69 74 65 6c 79 20 72 6f 6c 6c 20 62 61 63  initely roll bac
9f2b0 6b 2c 20 73 6f 20 6a 75 73 74 20 72 65 74 75 72  k, so just retur
9f2c0 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64 20  n SQLITE_OK and 
9f2d0 72 65 70 6f 72 74 20 61 20 28 6e 75 6c 29 0a 20  report a (nul). 
9f2e0 20 20 20 2a 2a 20 6d 61 73 74 65 72 2d 6a 6f 75     ** master-jou
9f2f0 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 2e 0a 20  rnal filename.. 
9f300 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65     */.    sqlite
9f310 46 72 65 65 28 2a 70 7a 4d 61 73 74 65 72 29 3b  Free(*pzMaster);
9f320 0a 20 20 20 20 2a 70 7a 4d 61 73 74 65 72 20 3d  .    *pzMaster =
9f330 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
9f340 20 28 2a 70 7a 4d 61 73 74 65 72 29 5b 6c 65 6e   (*pzMaster)[len
9f350 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 7d 0a 20 20  ] = '\0';.  }.  
9f360 20 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54   .  return SQLIT
9f370 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  E_OK;.}../*.** S
9f380 65 65 6b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  eek the journal 
9f390 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
9f3a0 74 6f 20 74 68 65 20 6e 65 78 74 20 73 65 63 74  to the next sect
9f3b0 6f 72 20 62 6f 75 6e 64 61 72 79 20 77 68 65 72  or boundary wher
9f3c0 65 20 61 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 68  e a.** journal h
9f3d0 65 61 64 65 72 20 6d 61 79 20 62 65 20 72 65 61  eader may be rea
9f3e0 64 20 6f 72 20 77 72 69 74 74 65 6e 2e 20 50 61  d or written. Pa
9f3f0 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20 69  ger.journalOff i
9f400 73 20 75 70 64 61 74 65 64 20 77 69 74 68 0a 2a  s updated with.*
9f410 2a 20 74 68 65 20 6e 65 77 20 73 65 65 6b 20 6f  * the new seek o
9f420 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 69 2e 65  ffset..**.** i.e
9f430 20 66 6f 72 20 61 20 73 65 63 74 6f 72 20 73 69   for a sector si
9f440 7a 65 20 6f 66 20 35 31 32 3a 0a 2a 2a 0a 2a 2a  ze of 512:.**.**
9f450 20 49 6e 70 75 74 20 4f 66 66 73 65 74 20 20 20   Input Offset   
9f460 20 20 20 20 20 20 20 20 20 20 20 4f 75 74 70 75             Outpu
9f470 74 20 4f 66 66 73 65 74 0a 2a 2a 20 2d 2d 2d 2d  t Offset.** ----
9f480 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
9f490 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
9f4a0 2d 2d 2d 0a 2a 2a 20 30 20 20 20 20 20 20 20 20  ---.** 0        
9f4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9f4c0 20 30 0a 2a 2a 20 35 31 32 20 20 20 20 20 20 20   0.** 512       
9f4d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9f4e0 35 31 32 0a 2a 2a 20 31 30 30 20 20 20 20 20 20  512.** 100      
9f4f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9f500 20 35 31 32 0a 2a 2a 20 32 30 30 30 20 20 20 20   512.** 2000    
9f510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9f520 20 20 32 30 34 38 0a 2a 2a 20 0a 2a 2f 0a 73 74    2048.** .*/.st
9f530 61 74 69 63 20 69 6e 74 20 73 65 65 6b 4a 6f 75  atic int seekJou
9f540 72 6e 61 6c 48 64 72 28 50 61 67 65 72 20 2a 70  rnalHdr(Pager *p
9f550 50 61 67 65 72 29 7b 0a 20 20 69 36 34 20 6f 66  Pager){.  i64 of
9f560 66 73 65 74 20 3d 20 30 3b 0a 20 20 69 36 34 20  fset = 0;.  i64 
9f570 63 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  c = pPager->jour
9f580 6e 61 6c 4f 66 66 3b 0a 20 20 69 66 28 20 63 20  nalOff;.  if( c 
9f590 29 7b 0a 20 20 20 20 6f 66 66 73 65 74 20 3d 20  ){.    offset = 
9f5a0 28 28 63 2d 31 29 2f 4a 4f 55 52 4e 41 4c 5f 48  ((c-1)/JOURNAL_H
9f5b0 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 2b 20  DR_SZ(pPager) + 
9f5c0 31 29 20 2a 20 4a 4f 55 52 4e 41 4c 5f 48 44 52  1) * JOURNAL_HDR
9f5d0 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 7d  _SZ(pPager);.  }
9f5e0 0a 20 20 61 73 73 65 72 74 28 20 6f 66 66 73 65  .  assert( offse
9f5f0 74 25 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a  t%JOURNAL_HDR_SZ
9f600 28 70 50 61 67 65 72 29 3d 3d 30 20 29 3b 0a 20  (pPager)==0 );. 
9f610 20 61 73 73 65 72 74 28 20 6f 66 66 73 65 74 3e   assert( offset>
9f620 3d 63 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =c );.  assert( 
9f630 28 6f 66 66 73 65 74 2d 63 29 3c 4a 4f 55 52 4e  (offset-c)<JOURN
9f640 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72  AL_HDR_SZ(pPager
9f650 29 20 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6a  ) );.  pPager->j
9f660 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 6f 66 66 73  ournalOff = offs
9f670 65 74 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  et;.  return sql
9f680 69 74 65 33 4f 73 53 65 65 6b 28 70 50 61 67 65  ite3OsSeek(pPage
9f690 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e  r->jfd, pPager->
9f6a0 6a 6f 75 72 6e 61 6c 4f 66 66 29 3b 0a 7d 0a 0a  journalOff);.}..
9f6b0 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61  /*.** The journa
9f6c0 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f  l file must be o
9f6d0 70 65 6e 20 77 68 65 6e 20 74 68 69 73 20 72 6f  pen when this ro
9f6e0 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e  utine is called.
9f6f0 20 41 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 68 65   A journal.** he
9f700 61 64 65 72 20 28 4a 4f 55 52 4e 41 4c 5f 48 44  ader (JOURNAL_HD
9f710 52 5f 53 5a 20 62 79 74 65 73 29 20 69 73 20 77  R_SZ bytes) is w
9f720 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20  ritten into the 
9f730 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 74 20  journal file at 
9f740 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 6c  the.** current l
9f750 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54  ocation..**.** T
9f760 68 65 20 66 6f 72 6d 61 74 20 66 6f 72 20 74 68  he format for th
9f770 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  e journal header
9f780 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a   is as follows:.
9f790 2a 2a 20 2d 20 38 20 62 79 74 65 73 3a 20 4d 61  ** - 8 bytes: Ma
9f7a0 67 69 63 20 69 64 65 6e 74 69 66 79 69 6e 67 20  gic identifying 
9f7b0 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 2e 0a  journal format..
9f7c0 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 4e 75  ** - 4 bytes: Nu
9f7d0 6d 62 65 72 20 6f 66 20 72 65 63 6f 72 64 73 20  mber of records 
9f7e0 69 6e 20 6a 6f 75 72 6e 61 6c 2c 20 6f 72 20 2d  in journal, or -
9f7f0 31 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 20 69  1 no-sync mode i
9f800 73 20 6f 6e 2e 0a 2a 2a 20 2d 20 34 20 62 79 74  s on..** - 4 byt
9f810 65 73 3a 20 52 61 6e 64 6f 6d 20 6e 75 6d 62 65  es: Random numbe
9f820 72 20 75 73 65 64 20 66 6f 72 20 70 61 67 65 20  r used for page 
9f830 68 61 73 68 2e 0a 2a 2a 20 2d 20 34 20 62 79 74  hash..** - 4 byt
9f840 65 73 3a 20 49 6e 69 74 69 61 6c 20 64 61 74 61  es: Initial data
9f850 62 61 73 65 20 70 61 67 65 20 63 6f 75 6e 74 2e  base page count.
9f860 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 53  .** - 4 bytes: S
9f870 65 63 74 6f 72 20 73 69 7a 65 20 75 73 65 64 20  ector size used 
9f880 62 79 20 74 68 65 20 70 72 6f 63 65 73 73 20 74  by the process t
9f890 68 61 74 20 77 72 6f 74 65 20 74 68 69 73 20 6a  hat wrote this j
9f8a0 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 0a 2a 2a 20 46  ournal..** .** F
9f8b0 6f 6c 6c 6f 77 65 64 20 62 79 20 28 4a 4f 55 52  ollowed by (JOUR
9f8c0 4e 41 4c 5f 48 44 52 5f 53 5a 20 2d 20 32 34 29  NAL_HDR_SZ - 24)
9f8d0 20 62 79 74 65 73 20 6f 66 20 75 6e 75 73 65 64   bytes of unused
9f8e0 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 61 74 69   space..*/.stati
9f8f0 63 20 69 6e 74 20 77 72 69 74 65 4a 6f 75 72 6e  c int writeJourn
9f900 61 6c 48 64 72 28 50 61 67 65 72 20 2a 70 50 61  alHdr(Pager *pPa
9f910 67 65 72 29 7b 0a 20 20 63 68 61 72 20 7a 48 65  ger){.  char zHe
9f920 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75  ader[sizeof(aJou
9f930 72 6e 61 6c 4d 61 67 69 63 29 2b 31 36 5d 3b 0a  rnalMagic)+16];.
9f940 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 69 66 28    int rc;..  if(
9f950 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 48 64 72   pPager->stmtHdr
9f960 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 20 20 70 50  Off==0 ){.    pP
9f970 61 67 65 72 2d 3e 73 74 6d 74 48 64 72 4f 66 66  ager->stmtHdrOff
9f980 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e   = pPager->journ
9f990 61 6c 4f 66 66 3b 0a 20 20 7d 0a 0a 20 20 72 63  alOff;.  }..  rc
9f9a0 20 3d 20 73 65 65 6b 4a 6f 75 72 6e 61 6c 48 64   = seekJournalHd
9f9b0 72 28 70 50 61 67 65 72 29 3b 0a 20 20 69 66 28  r(pPager);.  if(
9f9c0 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
9f9d0 0a 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  ..  pPager->jour
9f9e0 6e 61 6c 48 64 72 20 3d 20 70 50 61 67 65 72 2d  nalHdr = pPager-
9f9f0 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 70  >journalOff;.  p
9fa00 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
9fa10 66 20 2b 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52  f += JOURNAL_HDR
9fa20 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 0a 20 20  _SZ(pPager);..  
9fa30 2f 2a 20 46 49 58 20 4d 45 3a 20 0a 20 20 2a 2a  /* FIX ME: .  **
9fa40 0a 20 20 2a 2a 20 50 6f 73 73 69 62 6c 79 20 66  .  ** Possibly f
9fa50 6f 72 20 61 20 70 61 67 65 72 20 6e 6f 74 20 69  or a pager not i
9fa60 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 2c 20  n no-sync mode, 
9fa70 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 61 67 69  the journal magi
9fa80 63 20 73 68 6f 75 6c 64 20 6e 6f 74 0a 20 20 2a  c should not.  *
9fa90 2a 20 62 65 20 77 72 69 74 74 65 6e 20 75 6e 74  * be written unt
9faa0 69 6c 20 6e 52 65 63 20 69 73 20 66 69 6c 6c 65  il nRec is fille
9fab0 64 20 69 6e 20 61 73 20 70 61 72 74 20 6f 66 20  d in as part of 
9fac0 6e 65 78 74 20 73 79 6e 63 4a 6f 75 72 6e 61 6c  next syncJournal
9fad0 28 29 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41  (). .  **.  ** A
9fae0 63 74 75 61 6c 6c 79 20 6d 61 79 62 65 20 74 68  ctually maybe th
9faf0 65 20 77 68 6f 6c 65 20 6a 6f 75 72 6e 61 6c 20  e whole journal 
9fb00 68 65 61 64 65 72 20 73 68 6f 75 6c 64 20 62 65  header should be
9fb10 20 64 65 6c 61 79 65 64 20 75 6e 74 69 6c 20 74   delayed until t
9fb20 68 61 74 0a 20 20 2a 2a 20 70 6f 69 6e 74 2e 20  hat.  ** point. 
9fb30 54 68 69 6e 6b 20 61 62 6f 75 74 20 74 68 69 73  Think about this
9fb40 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 63 70 79 28  ..  */.  memcpy(
9fb50 7a 48 65 61 64 65 72 2c 20 61 4a 6f 75 72 6e 61  zHeader, aJourna
9fb60 6c 4d 61 67 69 63 2c 20 73 69 7a 65 6f 66 28 61  lMagic, sizeof(a
9fb70 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 29 3b 0a  JournalMagic));.
9fb80 20 20 2f 2a 20 54 68 65 20 6e 52 65 63 20 46 69    /* The nRec Fi
9fb90 65 6c 64 2e 20 30 78 46 46 46 46 46 46 46 46 20  eld. 0xFFFFFFFF 
9fba0 66 6f 72 20 6e 6f 2d 73 79 6e 63 20 6a 6f 75 72  for no-sync jour
9fbb0 6e 61 6c 73 2e 20 2a 2f 0a 20 20 70 75 74 33 32  nals. */.  put32
9fbc0 62 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69  bits(&zHeader[si
9fbd0 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67  zeof(aJournalMag
9fbe0 69 63 29 5d 2c 20 70 50 61 67 65 72 2d 3e 6e 6f  ic)], pPager->no
9fbf0 53 79 6e 63 20 3f 20 30 78 66 66 66 66 66 66 66  Sync ? 0xfffffff
9fc00 66 20 3a 20 30 29 3b 0a 20 20 2f 2a 20 54 68 65  f : 0);.  /* The
9fc10 20 72 61 6e 64 6f 6d 20 63 68 65 63 6b 2d 68 61   random check-ha
9fc20 73 68 20 69 6e 69 74 69 61 6c 69 73 65 72 20 2a  sh initialiser *
9fc30 2f 20 0a 20 20 73 71 6c 69 74 65 33 52 61 6e 64  / .  sqlite3Rand
9fc40 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 70 50  omness(sizeof(pP
9fc50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29  ager->cksumInit)
9fc60 2c 20 26 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d  , &pPager->cksum
9fc70 49 6e 69 74 29 3b 0a 20 20 70 75 74 33 32 62 69  Init);.  put32bi
9fc80 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65  ts(&zHeader[size
9fc90 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63  of(aJournalMagic
9fca0 29 2b 34 5d 2c 20 70 50 61 67 65 72 2d 3e 63 6b  )+4], pPager->ck
9fcb0 73 75 6d 49 6e 69 74 29 3b 0a 20 20 2f 2a 20 54  sumInit);.  /* T
9fcc0 68 65 20 69 6e 69 74 69 61 6c 20 64 61 74 61 62  he initial datab
9fcd0 61 73 65 20 73 69 7a 65 20 2a 2f 0a 20 20 70 75  ase size */.  pu
9fce0 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 65 72  t32bits(&zHeader
9fcf0 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c  [sizeof(aJournal
9fd00 4d 61 67 69 63 29 2b 38 5d 2c 20 70 50 61 67 65  Magic)+8], pPage
9fd10 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 20 2f 2a  r->dbSize);.  /*
9fd20 20 54 68 65 20 61 73 73 75 6d 65 64 20 73 65 63   The assumed sec
9fd30 74 6f 72 20 73 69 7a 65 20 66 6f 72 20 74 68 69  tor size for thi
9fd40 73 20 70 72 6f 63 65 73 73 20 2a 2f 0a 20 20 70  s process */.  p
9fd50 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 65  ut32bits(&zHeade
9fd60 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61  r[sizeof(aJourna
9fd70 6c 4d 61 67 69 63 29 2b 31 32 5d 2c 20 70 50 61  lMagic)+12], pPa
9fd80 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 29  ger->sectorSize)
9fd90 3b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 4a 48  ;.  IOTRACE(("JH
9fda0 44 52 20 25 70 20 25 6c 6c 64 20 25 64 5c 6e 22  DR %p %lld %d\n"
9fdb0 2c 20 70 50 61 67 65 72 2c 20 70 50 61 67 65 72  , pPager, pPager
9fdc0 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2c 20 73 69  ->journalHdr, si
9fdd0 7a 65 6f 66 28 7a 48 65 61 64 65 72 29 29 29 0a  zeof(zHeader))).
9fde0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
9fdf0 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66  Write(pPager->jf
9fe00 64 2c 20 7a 48 65 61 64 65 72 2c 20 73 69 7a 65  d, zHeader, size
9fe10 6f 66 28 7a 48 65 61 64 65 72 29 29 3b 0a 0a 20  of(zHeader));.. 
9fe20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20   /* The journal 
9fe30 68 65 61 64 65 72 20 68 61 73 20 62 65 65 6e 20  header has been 
9fe40 77 72 69 74 74 65 6e 20 73 75 63 63 65 73 73 66  written successf
9fe50 75 6c 6c 79 2e 20 53 65 65 6b 20 74 68 65 20 6a  ully. Seek the j
9fe60 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65  ournal.  ** file
9fe70 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20 74   descriptor to t
9fe80 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6a 6f  he end of the jo
9fe90 75 72 6e 61 6c 20 68 65 61 64 65 72 20 73 65 63  urnal header sec
9fea0 74 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  tor..  */.  if( 
9feb0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
9fec0 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a  .    IOTRACE(("J
9fed0 54 41 49 4c 20 25 70 20 25 6c 6c 64 5c 6e 22 2c  TAIL %p %lld\n",
9fee0 20 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d   pPager, pPager-
9fef0 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2d 31 29 29 0a  >journalOff-1)).
9ff00 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
9ff10 4f 73 53 65 65 6b 28 70 50 61 67 65 72 2d 3e 6a  OsSeek(pPager->j
9ff20 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  fd, pPager->jour
9ff30 6e 61 6c 4f 66 66 2d 31 29 3b 0a 20 20 20 20 69  nalOff-1);.    i
9ff40 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
9ff50 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   ){.      rc = s
9ff60 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50  qlite3OsWrite(pP
9ff70 61 67 65 72 2d 3e 6a 66 64 2c 20 22 5c 30 30 30  ager->jfd, "\000
9ff80 22 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ", 1);.    }.  }
9ff90 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
9ffa0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e  ./*.** The journ
9ffb0 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20  al file must be 
9ffc0 6f 70 65 6e 20 77 68 65 6e 20 74 68 69 73 20 69  open when this i
9ffd0 73 20 63 61 6c 6c 65 64 2e 20 41 20 6a 6f 75 72  s called. A jour
9ffe0 6e 61 6c 20 68 65 61 64 65 72 20 66 69 6c 65 0a  nal header file.
9fff0 2a 2a 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f  ** (JOURNAL_HDR_
a0000 53 5a 20 62 79 74 65 73 29 20 69 73 20 72 65 61  SZ bytes) is rea
a0010 64 20 66 72 6f 6d 20 74 68 65 20 63 75 72 72 65  d from the curre
a0020 6e 74 20 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 74  nt location in t
a0030 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69  he journal.** fi
a0040 6c 65 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73  le. See comments
a0050 20 61 62 6f 76 65 20 66 75 6e 63 74 69 6f 6e 20   above function 
a0060 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28  writeJournalHdr(
a0070 29 20 66 6f 72 20 61 20 64 65 73 63 72 69 70 74  ) for a descript
a0080 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65 20 6a 6f  ion of.** the jo
a0090 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 6f 72  urnal header for
a00a0 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  mat..**.** If th
a00b0 65 20 68 65 61 64 65 72 20 69 73 20 72 65 61 64  e header is read
a00c0 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 2a   successfully, *
a00d0 6e 52 65 63 20 69 73 20 73 65 74 20 74 6f 20 74  nRec is set to t
a00e0 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20  he number of.** 
a00f0 70 61 67 65 20 72 65 63 6f 72 64 73 20 66 6f 6c  page records fol
a0100 6c 6f 77 69 6e 67 20 74 68 69 73 20 68 65 61 64  lowing this head
a0110 65 72 20 61 6e 64 20 2a 64 62 53 69 7a 65 20 69  er and *dbSize i
a0120 73 20 73 65 74 20 74 6f 20 74 68 65 20 73 69 7a  s set to the siz
a0130 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61  e of the.** data
a0140 62 61 73 65 20 62 65 66 6f 72 65 20 74 68 65 20  base before the 
a0150 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 67 61  transaction bega
a0160 6e 2c 20 69 6e 20 70 61 67 65 73 2e 20 41 6c 73  n, in pages. Als
a0170 6f 2c 20 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d  o, pPager->cksum
a0180 49 6e 69 74 0a 2a 2a 20 69 73 20 73 65 74 20 74  Init.** is set t
a0190 6f 20 74 68 65 20 76 61 6c 75 65 20 72 65 61 64  o the value read
a01a0 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61   from the journa
a01b0 6c 20 68 65 61 64 65 72 2e 20 53 51 4c 49 54 45  l header. SQLITE
a01c0 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 0a  _OK is returned.
a01d0 2a 2a 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e  ** in this case.
a01e0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a 6f  .**.** If the jo
a01f0 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 69 6c  urnal header fil
a0200 65 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 20  e appears to be 
a0210 63 6f 72 72 75 70 74 65 64 2c 20 53 51 4c 49 54  corrupted, SQLIT
a0220 45 5f 44 4f 4e 45 20 69 73 0a 2a 2a 20 72 65 74  E_DONE is.** ret
a0230 75 72 6e 65 64 20 61 6e 64 20 2a 6e 52 65 63 20  urned and *nRec 
a0240 61 6e 64 20 2a 64 62 53 69 7a 65 20 61 72 65 20  and *dbSize are 
a0250 6e 6f 74 20 73 65 74 2e 20 20 49 66 20 4a 4f 55  not set.  If JOU
a0260 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 79 74 65  RNAL_HDR_SZ byte
a0270 73 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 72  s.** cannot be r
a0280 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75  ead from the jou
a0290 72 6e 61 6c 20 66 69 6c 65 20 61 6e 20 65 72 72  rnal file an err
a02a0 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
a02b0 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ned..*/.static i
a02c0 6e 74 20 72 65 61 64 4a 6f 75 72 6e 61 6c 48 64  nt readJournalHd
a02d0 72 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  r(.  Pager *pPag
a02e0 65 72 2c 20 0a 20 20 69 36 34 20 6a 6f 75 72 6e  er, .  i64 journ
a02f0 61 6c 53 69 7a 65 2c 0a 20 20 75 33 32 20 2a 70  alSize,.  u32 *p
a0300 4e 52 65 63 2c 20 0a 20 20 75 33 32 20 2a 70 44  NRec, .  u32 *pD
a0310 62 53 69 7a 65 0a 29 7b 0a 20 20 69 6e 74 20 72  bSize.){.  int r
a0320 63 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  c;.  unsigned ch
a0330 61 72 20 61 4d 61 67 69 63 5b 38 5d 3b 20 2f 2a  ar aMagic[8]; /*
a0340 20 41 20 62 75 66 66 65 72 20 74 6f 20 68 6f 6c   A buffer to hol
a0350 64 20 74 68 65 20 6d 61 67 69 63 20 68 65 61 64  d the magic head
a0360 65 72 20 2a 2f 0a 0a 20 20 72 63 20 3d 20 73 65  er */..  rc = se
a0370 65 6b 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61  ekJournalHdr(pPa
a0380 67 65 72 29 3b 0a 20 20 69 66 28 20 72 63 20 29  ger);.  if( rc )
a0390 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 69   return rc;..  i
a03a0 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  f( pPager->journ
a03b0 61 6c 4f 66 66 2b 4a 4f 55 52 4e 41 4c 5f 48 44  alOff+JOURNAL_HD
a03c0 52 5f 53 5a 28 70 50 61 67 65 72 29 20 3e 20 6a  R_SZ(pPager) > j
a03d0 6f 75 72 6e 61 6c 53 69 7a 65 20 29 7b 0a 20 20  ournalSize ){.  
a03e0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
a03f0 44 4f 4e 45 3b 0a 20 20 7d 0a 0a 20 20 72 63 20  DONE;.  }..  rc 
a0400 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28  = sqlite3OsRead(
a0410 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 61 4d 61  pPager->jfd, aMa
a0420 67 69 63 2c 20 73 69 7a 65 6f 66 28 61 4d 61 67  gic, sizeof(aMag
a0430 69 63 29 29 3b 0a 20 20 69 66 28 20 72 63 20 29  ic));.  if( rc )
a0440 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 69   return rc;..  i
a0450 66 28 20 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63  f( memcmp(aMagic
a0460 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c  , aJournalMagic,
a0470 20 73 69 7a 65 6f 66 28 61 4d 61 67 69 63 29 29   sizeof(aMagic))
a0480 21 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  !=0 ){.    retur
a0490 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20  n SQLITE_DONE;. 
a04a0 20 7d 0a 0a 20 20 72 63 20 3d 20 72 65 61 64 33   }..  rc = read3
a04b0 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66  2bits(pPager->jf
a04c0 64 2c 20 70 4e 52 65 63 29 3b 0a 20 20 69 66 28  d, pNRec);.  if(
a04d0 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
a04e0 0a 0a 20 20 72 63 20 3d 20 72 65 61 64 33 32 62  ..  rc = read32b
a04f0 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  its(pPager->jfd,
a0500 20 26 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49   &pPager->cksumI
a0510 6e 69 74 29 3b 0a 20 20 69 66 28 20 72 63 20 29  nit);.  if( rc )
a0520 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72   return rc;..  r
a0530 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 70  c = read32bits(p
a0540 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 44 62 53  Pager->jfd, pDbS
a0550 69 7a 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29  ize);.  if( rc )
a0560 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f   return rc;..  /
a0570 2a 20 55 70 64 61 74 65 20 74 68 65 20 61 73 73  * Update the ass
a0580 75 6d 65 64 20 73 65 63 74 6f 72 2d 73 69 7a 65  umed sector-size
a0590 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20 76 61   to match the va
a05a0 6c 75 65 20 75 73 65 64 20 62 79 20 0a 20 20 2a  lue used by .  *
a05b0 2a 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 68  * the process th
a05c0 61 74 20 63 72 65 61 74 65 64 20 74 68 69 73 20  at created this 
a05d0 6a 6f 75 72 6e 61 6c 2e 20 49 66 20 74 68 69 73  journal. If this
a05e0 20 6a 6f 75 72 6e 61 6c 20 77 61 73 0a 20 20 2a   journal was.  *
a05f0 2a 20 63 72 65 61 74 65 64 20 62 79 20 61 20 70  * created by a p
a0600 72 6f 63 65 73 73 20 6f 74 68 65 72 20 74 68 61  rocess other tha
a0610 6e 20 74 68 69 73 20 6f 6e 65 2c 20 74 68 65 6e  n this one, then
a0620 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 20 20   this routine.  
a0630 2a 2a 20 69 73 20 62 65 69 6e 67 20 63 61 6c 6c  ** is being call
a0640 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 70  ed from within p
a0650 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 2e  ager_playback().
a0660 20 54 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65   The local value
a0670 0a 20 20 2a 2a 20 6f 66 20 50 61 67 65 72 2e 73  .  ** of Pager.s
a0680 65 63 74 6f 72 53 69 7a 65 20 69 73 20 72 65 73  ectorSize is res
a0690 74 6f 72 65 64 20 61 74 20 74 68 65 20 65 6e 64  tored at the end
a06a0 20 6f 66 20 74 68 61 74 20 72 6f 75 74 69 6e 65   of that routine
a06b0 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 72 65  ..  */.  rc = re
a06c0 61 64 33 32 62 69 74 73 28 70 50 61 67 65 72 2d  ad32bits(pPager-
a06d0 3e 6a 66 64 2c 20 28 75 33 32 20 2a 29 26 70 50  >jfd, (u32 *)&pP
a06e0 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65  ager->sectorSize
a06f0 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65  );.  if( rc ) re
a0700 74 75 72 6e 20 72 63 3b 0a 0a 20 20 70 50 61 67  turn rc;..  pPag
a0710 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b  er->journalOff +
a0720 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a  = JOURNAL_HDR_SZ
a0730 28 70 50 61 67 65 72 29 3b 0a 20 20 72 63 20 3d  (pPager);.  rc =
a0740 20 73 71 6c 69 74 65 33 4f 73 53 65 65 6b 28 70   sqlite3OsSeek(p
a0750 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67  Pager->jfd, pPag
a0760 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 3b  er->journalOff);
a0770 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
a0780 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68  ../*.** Write th
a0790 65 20 73 75 70 70 6c 69 65 64 20 6d 61 73 74 65  e supplied maste
a07a0 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 69  r journal name i
a07b0 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  nto the journal 
a07c0 66 69 6c 65 20 66 6f 72 20 70 61 67 65 72 0a 2a  file for pager.*
a07d0 2a 20 70 50 61 67 65 72 20 61 74 20 74 68 65 20  * pPager at the 
a07e0 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e  current location
a07f0 2e 20 54 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  . The master jou
a0800 72 6e 61 6c 20 6e 61 6d 65 20 6d 75 73 74 20 62  rnal name must b
a0810 65 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20 74 68  e the last.** th
a0820 69 6e 67 20 77 72 69 74 74 65 6e 20 74 6f 20 61  ing written to a
a0830 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49   journal file. I
a0840 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69  f the pager is i
a0850 6e 20 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65  n full-sync mode
a0860 2c 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c  , the.** journal
a0870 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
a0880 20 69 73 20 61 64 76 61 6e 63 65 64 20 74 6f 20   is advanced to 
a0890 74 68 65 20 6e 65 78 74 20 73 65 63 74 6f 72 20  the next sector 
a08a0 62 6f 75 6e 64 61 72 79 20 62 65 66 6f 72 65 0a  boundary before.
a08b0 2a 2a 20 61 6e 79 74 68 69 6e 67 20 69 73 20 77  ** anything is w
a08c0 72 69 74 74 65 6e 2e 20 54 68 65 20 66 6f 72 6d  ritten. The form
a08d0 61 74 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 2b 20 34  at is:.**.** + 4
a08e0 20 62 79 74 65 73 3a 20 50 41 47 45 52 5f 4d 4a   bytes: PAGER_MJ
a08f0 5f 50 47 4e 4f 2e 0a 2a 2a 20 2b 20 4e 20 62 79  _PGNO..** + N by
a0900 74 65 73 3a 20 6c 65 6e 67 74 68 20 6f 66 20 6d  tes: length of m
a0910 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61  aster journal na
a0920 6d 65 2e 0a 2a 2a 20 2b 20 34 20 62 79 74 65 73  me..** + 4 bytes
a0930 3a 20 4e 0a 2a 2a 20 2b 20 34 20 62 79 74 65 73  : N.** + 4 bytes
a0940 3a 20 4d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  : Master journal
a0950 20 6e 61 6d 65 20 63 68 65 63 6b 73 75 6d 2e 0a   name checksum..
a0960 2a 2a 20 2b 20 38 20 62 79 74 65 73 3a 20 61 4a  ** + 8 bytes: aJ
a0970 6f 75 72 6e 61 6c 4d 61 67 69 63 5b 5d 2e 0a 2a  ournalMagic[]..*
a0980 2a 0a 2a 2a 20 54 68 65 20 6d 61 73 74 65 72 20  *.** The master 
a0990 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20 63 68 65  journal page che
a09a0 63 6b 73 75 6d 20 69 73 20 74 68 65 20 73 75 6d  cksum is the sum
a09b0 20 6f 66 20 74 68 65 20 62 79 74 65 73 20 69 6e   of the bytes in
a09c0 20 74 68 65 20 6d 61 73 74 65 72 0a 2a 2a 20 6a   the master.** j
a09d0 6f 75 72 6e 61 6c 20 6e 61 6d 65 2e 0a 2a 2a 0a  ournal name..**.
a09e0 2a 2a 20 49 66 20 7a 4d 61 73 74 65 72 20 69 73  ** If zMaster is
a09f0 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20   a NULL pointer 
a0a00 28 6f 63 63 75 72 73 20 66 6f 72 20 61 20 73 69  (occurs for a si
a0a10 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 74 72  ngle database tr
a0a20 61 6e 73 61 63 74 69 6f 6e 29 2c 20 0a 2a 2a 20  ansaction), .** 
a0a30 74 68 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e  this call is a n
a0a40 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  o-op..*/.static 
a0a50 69 6e 74 20 77 72 69 74 65 4d 61 73 74 65 72 4a  int writeMasterJ
a0a60 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50  ournal(Pager *pP
a0a70 61 67 65 72 2c 20 63 6f 6e 73 74 20 63 68 61 72  ager, const char
a0a80 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e   *zMaster){.  in
a0a90 74 20 72 63 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b  t rc;.  int len;
a0aa0 20 0a 20 20 69 6e 74 20 69 3b 20 0a 20 20 75 33   .  int i; .  u3
a0ab0 32 20 63 6b 73 75 6d 20 3d 20 30 3b 0a 20 20 63  2 cksum = 0;.  c
a0ac0 68 61 72 20 7a 42 75 66 5b 73 69 7a 65 6f 66 28  har zBuf[sizeof(
a0ad0 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 32  aJournalMagic)+2
a0ae0 2a 34 5d 3b 0a 0a 20 20 69 66 28 20 21 7a 4d 61  *4];..  if( !zMa
a0af0 73 74 65 72 20 7c 7c 20 70 50 61 67 65 72 2d 3e  ster || pPager->
a0b00 73 65 74 4d 61 73 74 65 72 29 20 72 65 74 75 72  setMaster) retur
a0b10 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 70  n SQLITE_OK;.  p
a0b20 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72  Pager->setMaster
a0b30 20 3d 20 31 3b 0a 0a 20 20 6c 65 6e 20 3d 20 73   = 1;..  len = s
a0b40 74 72 6c 65 6e 28 7a 4d 61 73 74 65 72 29 3b 0a  trlen(zMaster);.
a0b50 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6c 65 6e    for(i=0; i<len
a0b60 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 6b 73 75  ; i++){.    cksu
a0b70 6d 20 2b 3d 20 7a 4d 61 73 74 65 72 5b 69 5d 3b  m += zMaster[i];
a0b80 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 69 6e  .  }..  /* If in
a0b90 20 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 2c   full-sync mode,
a0ba0 20 61 64 76 61 6e 63 65 20 74 6f 20 74 68 65 20   advance to the 
a0bb0 6e 65 78 74 20 64 69 73 6b 20 73 65 63 74 6f 72  next disk sector
a0bc0 20 62 65 66 6f 72 65 20 77 72 69 74 69 6e 67 0a   before writing.
a0bd0 20 20 2a 2a 20 74 68 65 20 6d 61 73 74 65 72 20    ** the master 
a0be0 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 2e 20 54 68  journal name. Th
a0bf0 69 73 20 69 73 20 69 6e 20 63 61 73 65 20 74 68  is is in case th
a0c00 65 20 70 72 65 76 69 6f 75 73 20 70 61 67 65 20  e previous page 
a0c10 77 72 69 74 74 65 6e 20 74 6f 0a 20 20 2a 2a 20  written to.  ** 
a0c20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 61 73 20  the journal has 
a0c30 61 6c 72 65 61 64 79 20 62 65 65 6e 20 73 79 6e  already been syn
a0c40 63 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ced..  */.  if( 
a0c50 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63  pPager->fullSync
a0c60 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 65 65   ){.    rc = see
a0c70 6b 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67  kJournalHdr(pPag
a0c80 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  er);.    if( rc!
a0c90 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
a0ca0 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 50  urn rc;.  }.  pP
a0cb0 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
a0cc0 20 2b 3d 20 28 6c 65 6e 2b 32 30 29 3b 0a 0a 20   += (len+20);.. 
a0cd0 20 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 74   rc = write32bit
a0ce0 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 50  s(pPager->jfd, P
a0cf0 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61  AGER_MJ_PGNO(pPa
a0d00 67 65 72 29 29 3b 0a 20 20 69 66 28 20 72 63 21  ger));.  if( rc!
a0d10 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
a0d20 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20  urn rc;..  rc = 
a0d30 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70  sqlite3OsWrite(p
a0d40 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73  Pager->jfd, zMas
a0d50 74 65 72 2c 20 6c 65 6e 29 3b 0a 20 20 69 66 28  ter, len);.  if(
a0d60 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
a0d70 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 70   return rc;..  p
a0d80 75 74 33 32 62 69 74 73 28 7a 42 75 66 2c 20 6c  ut32bits(zBuf, l
a0d90 65 6e 29 3b 0a 20 20 70 75 74 33 32 62 69 74 73  en);.  put32bits
a0da0 28 26 7a 42 75 66 5b 34 5d 2c 20 63 6b 73 75 6d  (&zBuf[4], cksum
a0db0 29 3b 0a 20 20 6d 65 6d 63 70 79 28 26 7a 42 75  );.  memcpy(&zBu
a0dc0 66 5b 38 5d 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61  f[8], aJournalMa
a0dd0 67 69 63 2c 20 73 69 7a 65 6f 66 28 61 4a 6f 75  gic, sizeof(aJou
a0de0 72 6e 61 6c 4d 61 67 69 63 29 29 3b 0a 20 20 72  rnalMagic));.  r
a0df0 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69  c = sqlite3OsWri
a0e00 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  te(pPager->jfd, 
a0e10 7a 42 75 66 2c 20 38 2b 73 69 7a 65 6f 66 28 61  zBuf, 8+sizeof(a
a0e20 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 29 3b 0a  JournalMagic));.
a0e30 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79    pPager->needSy
a0e40 6e 63 20 3d 20 21 70 50 61 67 65 72 2d 3e 6e 6f  nc = !pPager->no
a0e50 53 79 6e 63 3b 0a 20 20 72 65 74 75 72 6e 20 72  Sync;.  return r
a0e60 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20  c;.}../*.** Add 
a0e70 6f 72 20 72 65 6d 6f 76 65 20 61 20 70 61 67 65  or remove a page
a0e80 20 66 72 6f 6d 20 74 68 65 20 6c 69 73 74 20 6f   from the list o
a0e90 66 20 61 6c 6c 20 70 61 67 65 73 20 74 68 61 74  f all pages that
a0ea0 20 61 72 65 20 69 6e 20 74 68 65 0a 2a 2a 20 73   are in the.** s
a0eb0 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c  tatement journal
a0ec0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 61 67 65  ..**.** The Page
a0ed0 72 20 6b 65 65 70 73 20 61 20 73 65 70 61 72 61  r keeps a separa
a0ee0 74 65 20 6c 69 73 74 20 6f 66 20 70 61 67 65 73  te list of pages
a0ef0 20 74 68 61 74 20 61 72 65 20 63 75 72 72 65 6e   that are curren
a0f00 74 6c 79 20 69 6e 0a 2a 2a 20 74 68 65 20 73 74  tly in.** the st
a0f10 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e  atement journal.
a0f20 20 20 54 68 69 73 20 68 65 6c 70 73 20 74 68 65    This helps the
a0f30 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 74 6d   sqlite3PagerStm
a0f40 74 43 6f 6d 6d 69 74 28 29 0a 2a 2a 20 72 6f 75  tCommit().** rou
a0f50 74 69 6e 65 20 72 75 6e 20 4d 55 43 48 20 66 61  tine run MUCH fa
a0f60 73 74 65 72 20 66 6f 72 20 74 68 65 20 63 6f 6d  ster for the com
a0f70 6d 6f 6e 20 63 61 73 65 20 77 68 65 72 65 20 74  mon case where t
a0f80 68 65 72 65 20 61 72 65 20 6d 61 6e 79 0a 2a 2a  here are many.**
a0f90 20 70 61 67 65 73 20 69 6e 20 6d 65 6d 6f 72 79   pages in memory
a0fa0 20 62 75 74 20 6f 6e 6c 79 20 61 20 66 65 77 20   but only a few 
a0fb0 61 72 65 20 69 6e 20 74 68 65 20 73 74 61 74 65  are in the state
a0fc0 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2f  ment journal..*/
a0fd0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67  .static void pag
a0fe0 65 5f 61 64 64 5f 74 6f 5f 73 74 6d 74 5f 6c 69  e_add_to_stmt_li
a0ff0 73 74 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a  st(PgHdr *pPg){.
a1000 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20    Pager *pPager 
a1010 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20  = pPg->pPager;. 
a1020 20 50 67 48 69 73 74 6f 72 79 20 2a 70 48 69 73   PgHistory *pHis
a1030 74 20 3d 20 50 47 48 44 52 5f 54 4f 5f 48 49 53  t = PGHDR_TO_HIS
a1040 54 28 70 50 67 2c 20 70 50 61 67 65 72 29 3b 0a  T(pPg, pPager);.
a1050 20 20 61 73 73 65 72 74 28 20 4d 45 4d 44 42 20    assert( MEMDB 
a1060 29 3b 0a 20 20 69 66 28 20 21 70 48 69 73 74 2d  );.  if( !pHist-
a1070 3e 69 6e 53 74 6d 74 20 29 7b 0a 20 20 20 20 61  >inStmt ){.    a
a1080 73 73 65 72 74 28 20 70 48 69 73 74 2d 3e 70 50  ssert( pHist->pP
a1090 72 65 76 53 74 6d 74 3d 3d 30 20 26 26 20 70 48  revStmt==0 && pH
a10a0 69 73 74 2d 3e 70 4e 65 78 74 53 74 6d 74 3d 3d  ist->pNextStmt==
a10b0 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61  0 );.    if( pPa
a10c0 67 65 72 2d 3e 70 53 74 6d 74 20 29 7b 0a 20 20  ger->pStmt ){.  
a10d0 20 20 20 20 50 47 48 44 52 5f 54 4f 5f 48 49 53      PGHDR_TO_HIS
a10e0 54 28 70 50 61 67 65 72 2d 3e 70 53 74 6d 74 2c  T(pPager->pStmt,
a10f0 20 70 50 61 67 65 72 29 2d 3e 70 50 72 65 76 53   pPager)->pPrevS
a1100 74 6d 74 20 3d 20 70 50 67 3b 0a 20 20 20 20 7d  tmt = pPg;.    }
a1110 0a 20 20 20 20 70 48 69 73 74 2d 3e 70 4e 65 78  .    pHist->pNex
a1120 74 53 74 6d 74 20 3d 20 70 50 61 67 65 72 2d 3e  tStmt = pPager->
a1130 70 53 74 6d 74 3b 0a 20 20 20 20 70 50 61 67 65  pStmt;.    pPage
a1140 72 2d 3e 70 53 74 6d 74 20 3d 20 70 50 67 3b 0a  r->pStmt = pPg;.
a1150 20 20 20 20 70 48 69 73 74 2d 3e 69 6e 53 74 6d      pHist->inStm
a1160 74 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  t = 1;.  }.}../*
a1170 0a 2a 2a 20 46 69 6e 64 20 61 20 70 61 67 65 20  .** Find a page 
a1180 69 6e 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  in the hash tabl
a1190 65 20 67 69 76 65 6e 20 69 74 73 20 70 61 67 65  e given its page
a11a0 20 6e 75 6d 62 65 72 2e 20 20 52 65 74 75 72 6e   number.  Return
a11b0 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  .** a pointer to
a11c0 20 74 68 65 20 70 61 67 65 20 6f 72 20 4e 55 4c   the page or NUL
a11d0 4c 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a  L if not found..
a11e0 2a 2f 0a 73 74 61 74 69 63 20 50 67 48 64 72 20  */.static PgHdr 
a11f0 2a 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 50 61  *pager_lookup(Pa
a1200 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e  ger *pPager, Pgn
a1210 6f 20 70 67 6e 6f 29 7b 0a 20 20 50 67 48 64 72  o pgno){.  PgHdr
a1220 20 2a 70 3b 0a 20 20 69 66 28 20 70 50 61 67 65   *p;.  if( pPage
a1230 72 2d 3e 61 48 61 73 68 3d 3d 30 20 29 20 72 65  r->aHash==0 ) re
a1240 74 75 72 6e 20 30 3b 0a 20 20 70 20 3d 20 70 50  turn 0;.  p = pP
a1250 61 67 65 72 2d 3e 61 48 61 73 68 5b 70 67 6e 6f  ager->aHash[pgno
a1260 20 26 20 28 70 50 61 67 65 72 2d 3e 6e 48 61 73   & (pPager->nHas
a1270 68 2d 31 29 5d 3b 0a 20 20 77 68 69 6c 65 28 20  h-1)];.  while( 
a1280 70 20 26 26 20 70 2d 3e 70 67 6e 6f 21 3d 70 67  p && p->pgno!=pg
a1290 6e 6f 20 29 7b 0a 20 20 20 20 70 20 3d 20 70 2d  no ){.    p = p-
a12a0 3e 70 4e 65 78 74 48 61 73 68 3b 0a 20 20 7d 0a  >pNextHash;.  }.
a12b0 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f    return p;.}../
a12c0 2a 0a 2a 2a 20 55 6e 6c 6f 63 6b 20 74 68 65 20  *.** Unlock the 
a12d0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a  database file..*
a12e0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61  /.static void pa
a12f0 67 65 72 5f 75 6e 6c 6f 63 6b 28 50 61 67 65 72  ger_unlock(Pager
a1300 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28   *pPager){.  if(
a1310 20 21 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73   !pPager->exclus
a1320 69 76 65 4d 6f 64 65 20 29 7b 0a 20 20 20 20 69  iveMode ){.    i
a1330 66 28 20 21 4d 45 4d 44 42 20 29 7b 0a 20 20 20  f( !MEMDB ){.   
a1340 20 20 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f     sqlite3OsUnlo
a1350 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 4e  ck(pPager->fd, N
a1360 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 70  O_LOCK);.      p
a1370 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20  Pager->dbSize = 
a1380 2d 31 3b 0a 20 20 20 20 20 20 49 4f 54 52 41 43  -1;.      IOTRAC
a1390 45 28 28 22 55 4e 4c 4f 43 4b 20 25 70 5c 6e 22  E(("UNLOCK %p\n"
a13a0 2c 20 70 50 61 67 65 72 29 29 0a 20 20 20 20 7d  , pPager)).    }
a13b0 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61  .    pPager->sta
a13c0 74 65 20 3d 20 50 41 47 45 52 5f 55 4e 4c 4f 43  te = PAGER_UNLOC
a13d0 4b 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 63  K;.    pPager->c
a13e0 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d  hangeCountDone =
a13f0 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a   0;.  }.}../*.**
a1400 20 45 78 65 63 75 74 65 20 61 20 72 6f 6c 6c 62   Execute a rollb
a1410 61 63 6b 20 69 66 20 61 20 74 72 61 6e 73 61 63  ack if a transac
a1420 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20 61  tion is active a
a1430 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 65 20 0a 2a  nd unlock the .*
a1440 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  * database file.
a1450 20 54 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70   This is a no-op
a1460 20 69 66 20 74 68 65 20 70 61 67 65 72 20 68 61   if the pager ha
a1470 73 20 61 6c 72 65 61 64 79 20 65 6e 74 65 72 65  s already entere
a1480 64 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 2d 73  d.** the error-s
a1490 74 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tate..*/.static 
a14a0 76 6f 69 64 20 70 61 67 65 72 55 6e 6c 6f 63 6b  void pagerUnlock
a14b0 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 50 61 67 65  AndRollback(Page
a14c0 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e  r *p){.  if( p->
a14d0 65 72 72 43 6f 64 65 20 29 20 72 65 74 75 72 6e  errCode ) return
a14e0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73  ;.  assert( p->s
a14f0 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45  tate>=PAGER_RESE
a1500 52 56 45 44 20 7c 7c 20 70 2d 3e 6a 6f 75 72 6e  RVED || p->journ
a1510 61 6c 4f 70 65 6e 3d 3d 30 20 29 3b 0a 20 20 69  alOpen==0 );.  i
a1520 66 28 20 70 2d 3e 73 74 61 74 65 3e 3d 50 41 47  f( p->state>=PAG
a1530 45 52 5f 52 45 53 45 52 56 45 44 20 29 7b 0a 20  ER_RESERVED ){. 
a1540 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 52     sqlite3PagerR
a1550 6f 6c 6c 62 61 63 6b 28 70 29 3b 0a 20 20 7d 0a  ollback(p);.  }.
a1560 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70    pager_unlock(p
a1570 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
a1580 65 72 72 43 6f 64 65 20 7c 7c 20 21 70 2d 3e 6a  errCode || !p->j
a1590 6f 75 72 6e 61 6c 4f 70 65 6e 20 7c 7c 20 28 70  ournalOpen || (p
a15a0 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 26  ->exclusiveMode&
a15b0 26 21 70 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29  &!p->journalOff)
a15c0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d   );.  assert( p-
a15d0 3e 65 72 72 43 6f 64 65 20 7c 7c 20 21 70 2d 3e  >errCode || !p->
a15e0 73 74 6d 74 4f 70 65 6e 20 7c 7c 20 70 2d 3e 65  stmtOpen || p->e
a15f0 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 29 3b 0a  xclusiveMode );.
a1600 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20  }.../*.** Clear 
a1610 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61  the in-memory ca
a1620 63 68 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69  che.  This routi
a1630 6e 65 0a 2a 2a 20 73 65 74 73 20 74 68 65 20 73  ne.** sets the s
a1640 74 61 74 65 20 6f 66 20 74 68 65 20 70 61 67 65  tate of the page
a1650 72 20 62 61 63 6b 20 74 6f 20 77 68 61 74 20 69  r back to what i
a1660 74 20 77 61 73 20 77 68 65 6e 20 69 74 20 77 61  t was when it wa
a1670 73 20 66 69 72 73 74 0a 2a 2a 20 6f 70 65 6e 65  s first.** opene
a1680 64 2e 20 20 41 6e 79 20 6f 75 74 73 74 61 6e 64  d.  Any outstand
a1690 69 6e 67 20 70 61 67 65 73 20 61 72 65 20 69 6e  ing pages are in
a16a0 76 61 6c 69 64 61 74 65 64 20 61 6e 64 20 73 75  validated and su
a16b0 62 73 65 71 75 65 6e 74 20 61 74 74 65 6d 70 74  bsequent attempt
a16c0 73 0a 2a 2a 20 74 6f 20 61 63 63 65 73 73 20 74  s.** to access t
a16d0 68 6f 73 65 20 70 61 67 65 73 20 77 69 6c 6c 20  hose pages will 
a16e0 6c 69 6b 65 6c 79 20 72 65 73 75 6c 74 20 69 6e  likely result in
a16f0 20 61 20 63 6f 72 65 64 75 6d 70 2e 0a 2a 2f 0a   a coredump..*/.
a1700 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65  static void page
a1710 72 5f 72 65 73 65 74 28 50 61 67 65 72 20 2a 70  r_reset(Pager *p
a1720 50 61 67 65 72 29 7b 0a 20 20 50 67 48 64 72 20  Pager){.  PgHdr 
a1730 2a 70 50 67 2c 20 2a 70 4e 65 78 74 3b 0a 20 20  *pPg, *pNext;.  
a1740 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43  if( pPager->errC
a1750 6f 64 65 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ode ) return;.  
a1760 66 6f 72 28 70 50 67 3d 70 50 61 67 65 72 2d 3e  for(pPg=pPager->
a1770 70 41 6c 6c 3b 20 70 50 67 3b 20 70 50 67 3d 70  pAll; pPg; pPg=p
a1780 4e 65 78 74 29 7b 0a 20 20 20 20 49 4f 54 52 41  Next){.    IOTRA
a1790 43 45 28 28 22 50 47 46 52 45 45 20 25 70 20 25  CE(("PGFREE %p %
a17a0 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50  d\n", pPager, pP
a17b0 67 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 50  g->pgno));.    P
a17c0 41 47 45 52 5f 49 4e 43 52 28 73 71 6c 69 74 65  AGER_INCR(sqlite
a17d0 33 5f 70 61 67 65 72 5f 70 67 66 72 65 65 5f 63  3_pager_pgfree_c
a17e0 6f 75 6e 74 29 3b 0a 20 20 20 20 70 4e 65 78 74  ount);.    pNext
a17f0 20 3d 20 70 50 67 2d 3e 70 4e 65 78 74 41 6c 6c   = pPg->pNextAll
a1800 3b 0a 20 20 20 20 73 71 6c 69 74 65 46 72 65 65  ;.    sqliteFree
a1810 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20 70 50 61  (pPg);.  }.  pPa
a1820 67 65 72 2d 3e 70 53 74 6d 74 20 3d 20 30 3b 0a  ger->pStmt = 0;.
a1830 20 20 70 50 61 67 65 72 2d 3e 70 46 69 72 73 74    pPager->pFirst
a1840 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e   = 0;.  pPager->
a1850 70 46 69 72 73 74 53 79 6e 63 65 64 20 3d 20 30  pFirstSynced = 0
a1860 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70 4c 61 73  ;.  pPager->pLas
a1870 74 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d  t = 0;.  pPager-
a1880 3e 70 41 6c 6c 20 3d 20 30 3b 0a 20 20 70 50 61  >pAll = 0;.  pPa
a1890 67 65 72 2d 3e 6e 48 61 73 68 20 3d 20 30 3b 0a  ger->nHash = 0;.
a18a0 20 20 73 71 6c 69 74 65 46 72 65 65 28 70 50 61    sqliteFree(pPa
a18b0 67 65 72 2d 3e 61 48 61 73 68 29 3b 0a 20 20 70  ger->aHash);.  p
a18c0 50 61 67 65 72 2d 3e 6e 50 61 67 65 20 3d 20 30  Pager->nPage = 0
a18d0 3b 0a 20 20 70 50 61 67 65 72 2d 3e 61 48 61 73  ;.  pPager->aHas
a18e0 68 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d  h = 0;.  pPager-
a18f0 3e 6e 52 65 66 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a  >nRef = 0;.}../*
a1900 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
a1910 20 65 6e 64 73 20 61 20 74 72 61 6e 73 61 63 74   ends a transact
a1920 69 6f 6e 2e 20 20 41 20 74 72 61 6e 73 61 63 74  ion.  A transact
a1930 69 6f 6e 20 69 73 20 65 6e 64 65 64 20 62 79 20  ion is ended by 
a1940 65 69 74 68 65 72 0a 2a 2a 20 61 20 43 4f 4d 4d  either.** a COMM
a1950 49 54 20 6f 72 20 61 20 52 4f 4c 4c 42 41 43 4b  IT or a ROLLBACK
a1960 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69  ..**.** When thi
a1970 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
a1980 6c 65 64 2c 20 74 68 65 20 70 61 67 65 72 20 68  led, the pager h
a1990 61 73 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  as the journal f
a19a0 69 6c 65 20 6f 70 65 6e 20 61 6e 64 0a 2a 2a 20  ile open and.** 
a19b0 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 45 58  a RESERVED or EX
a19c0 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20  CLUSIVE lock on 
a19d0 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54  the database.  T
a19e0 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c  his routine will
a19f0 20 72 65 6c 65 61 73 65 0a 2a 2a 20 74 68 65 20   release.** the 
a1a00 64 61 74 61 62 61 73 65 20 6c 6f 63 6b 20 61 6e  database lock an
a1a10 64 20 61 63 71 75 69 72 65 73 20 61 20 53 48 41  d acquires a SHA
a1a20 52 45 44 20 6c 6f 63 6b 20 69 6e 20 69 74 73 20  RED lock in its 
a1a30 70 6c 61 63 65 20 69 66 20 74 68 61 74 20 69 73  place if that is
a1a40 0a 2a 2a 20 74 68 65 20 61 70 70 72 6f 70 72 69  .** the appropri
a1a50 61 74 65 20 74 68 69 6e 67 20 74 6f 20 64 6f 2e  ate thing to do.
a1a60 20 20 52 65 6c 65 61 73 65 20 6c 6f 63 6b 73 20    Release locks 
a1a70 75 73 75 61 6c 6c 79 20 69 73 20 61 70 70 72 6f  usually is appro
a1a80 70 72 69 61 74 65 2c 0a 2a 2a 20 75 6e 6c 65 73  priate,.** unles
a1a90 73 20 77 65 20 61 72 65 20 69 6e 20 65 78 63 6c  s we are in excl
a1aa0 75 73 69 76 65 20 61 63 63 65 73 73 20 6d 6f 64  usive access mod
a1ab0 65 20 6f 72 20 75 6e 6c 65 73 73 20 74 68 69 73  e or unless this
a1ac0 20 69 73 20 61 20 0a 2a 2a 20 43 4f 4d 4d 49 54   is a .** COMMIT
a1ad0 20 41 4e 44 20 42 45 47 49 4e 20 6f 72 20 52 4f   AND BEGIN or RO
a1ae0 4c 4c 42 41 43 4b 20 41 4e 44 20 42 45 47 49 4e  LLBACK AND BEGIN
a1af0 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a   operation..**.*
a1b00 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  * The journal fi
a1b10 6c 65 20 69 73 20 65 69 74 68 65 72 20 64 65 6c  le is either del
a1b20 65 74 65 64 20 6f 72 20 74 72 75 6e 63 61 74 65  eted or truncate
a1b30 64 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 43  d..**.** TODO: C
a1b40 6f 6e 73 69 64 65 72 20 6b 65 65 70 69 6e 67 20  onsider keeping 
a1b50 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
a1b60 20 6f 70 65 6e 20 66 6f 72 20 74 65 6d 70 6f 72   open for tempor
a1b70 61 72 79 20 64 61 74 61 62 61 73 65 73 2e 0a 2a  ary databases..*
a1b80 2a 20 54 68 69 73 20 6d 69 67 68 74 20 67 69 76  * This might giv
a1b90 65 20 61 20 70 65 72 66 6f 72 6d 61 6e 63 65 20  e a performance 
a1ba0 69 6d 70 72 6f 76 65 6d 65 6e 74 20 6f 6e 20 77  improvement on w
a1bb0 69 6e 64 6f 77 73 20 77 68 65 72 65 20 6f 70 65  indows where ope
a1bc0 6e 69 6e 67 0a 2a 2a 20 61 20 66 69 6c 65 20 69  ning.** a file i
a1bd0 73 20 61 6e 20 65 78 70 65 6e 73 69 76 65 20 6f  s an expensive o
a1be0 70 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  peration..*/.sta
a1bf0 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 65 6e  tic int pager_en
a1c00 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 50 61  d_transaction(Pa
a1c10 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
a1c20 50 67 48 64 72 20 2a 70 50 67 3b 0a 20 20 69 6e  PgHdr *pPg;.  in
a1c30 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
a1c40 3b 0a 20 20 69 6e 74 20 72 63 32 20 3d 20 53 51  ;.  int rc2 = SQ
a1c50 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72  LITE_OK;.  asser
a1c60 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 69  t( !MEMDB );.  i
a1c70 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  f( pPager->state
a1c80 3c 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20  <PAGER_RESERVED 
a1c90 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
a1ca0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 73  LITE_OK;.  }.  s
a1cb0 71 6c 69 74 65 33 50 61 67 65 72 53 74 6d 74 43  qlite3PagerStmtC
a1cc0 6f 6d 6d 69 74 28 70 50 61 67 65 72 29 3b 0a 20  ommit(pPager);. 
a1cd0 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 6d   if( pPager->stm
a1ce0 74 4f 70 65 6e 20 26 26 20 21 70 50 61 67 65 72  tOpen && !pPager
a1cf0 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20  ->exclusiveMode 
a1d00 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73  ){.    sqlite3Os
a1d10 43 6c 6f 73 65 28 26 70 50 61 67 65 72 2d 3e 73  Close(&pPager->s
a1d20 74 66 64 29 3b 0a 20 20 20 20 70 50 61 67 65 72  tfd);.    pPager
a1d30 2d 3e 73 74 6d 74 4f 70 65 6e 20 3d 20 30 3b 0a  ->stmtOpen = 0;.
a1d40 20 20 7d 0a 20 20 69 66 28 20 70 50 61 67 65 72    }.  if( pPager
a1d50 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 29 7b  ->journalOpen ){
a1d60 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d  .    if( pPager-
a1d70 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 0a  >exclusiveMode .
a1d80 20 20 20 20 20 20 20 20 20 20 26 26 20 28 72 63            && (rc
a1d90 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e   = sqlite3OsTrun
a1da0 63 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64  cate(pPager->jfd
a1db0 2c 20 30 29 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b  , 0))==SQLITE_OK
a1dc0 20 29 7b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   ){;.      sqlit
a1dd0 65 33 4f 73 53 65 65 6b 28 70 50 61 67 65 72 2d  e3OsSeek(pPager-
a1de0 3e 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20 20 20  >jfd, 0);.      
a1df0 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
a1e00 66 66 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50  ff = 0;.      pP
a1e10 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61  ager->journalSta
a1e20 72 74 65 64 20 3d 20 30 3b 0a 20 20 20 20 7d 65  rted = 0;.    }e
a1e30 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
a1e40 65 33 4f 73 43 6c 6f 73 65 28 26 70 50 61 67 65  e3OsClose(&pPage
a1e50 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 70  r->jfd);.      p
a1e60 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70  Pager->journalOp
a1e70 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66  en = 0;.      if
a1e80 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
a1e90 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
a1ea0 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28  sqlite3OsDelete(
a1eb0 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c  pPager->zJournal
a1ec0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
a1ed0 0a 20 20 20 20 73 71 6c 69 74 65 46 72 65 65 28  .    sqliteFree(
a1ee0 20 70 50 61 67 65 72 2d 3e 61 49 6e 4a 6f 75 72   pPager->aInJour
a1ef0 6e 61 6c 20 29 3b 0a 20 20 20 20 70 50 61 67 65  nal );.    pPage
a1f00 72 2d 3e 61 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20  r->aInJournal = 
a1f10 30 3b 0a 20 20 20 20 66 6f 72 28 70 50 67 3d 70  0;.    for(pPg=p
a1f20 50 61 67 65 72 2d 3e 70 41 6c 6c 3b 20 70 50 67  Pager->pAll; pPg
a1f30 3b 20 70 50 67 3d 70 50 67 2d 3e 70 4e 65 78 74  ; pPg=pPg->pNext
a1f40 41 6c 6c 29 7b 0a 20 20 20 20 20 20 70 50 67 2d  All){.      pPg-
a1f50 3e 69 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a  >inJournal = 0;.
a1f60 20 20 20 20 20 20 70 50 67 2d 3e 64 69 72 74 79        pPg->dirty
a1f70 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 67 2d   = 0;.      pPg-
a1f80 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20  >needSync = 0;. 
a1f90 20 20 20 20 20 70 50 67 2d 3e 61 6c 77 61 79 73       pPg->always
a1fa0 52 6f 6c 6c 62 61 63 6b 20 3d 20 30 3b 0a 23 69  Rollback = 0;.#i
a1fb0 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43  fdef SQLITE_CHEC
a1fc0 4b 5f 50 41 47 45 53 0a 20 20 20 20 20 20 70 50  K_PAGES.      pP
a1fd0 67 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70 61  g->pageHash = pa
a1fe0 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67  ger_pagehash(pPg
a1ff0 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a  );.#endif.    }.
a2000 20 20 20 20 70 50 61 67 65 72 2d 3e 70 44 69 72      pPager->pDir
a2010 74 79 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67  ty = 0;.    pPag
a2020 65 72 2d 3e 64 69 72 74 79 43 61 63 68 65 20 3d  er->dirtyCache =
a2030 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   0;.    pPager->
a2040 6e 52 65 63 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  nRec = 0;.  }els
a2050 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  e{.    assert( p
a2060 50 61 67 65 72 2d 3e 61 49 6e 4a 6f 75 72 6e 61  Pager->aInJourna
a2070 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65  l==0 );.    asse
a2080 72 74 28 20 70 50 61 67 65 72 2d 3e 64 69 72 74  rt( pPager->dirt
a2090 79 43 61 63 68 65 3d 3d 30 20 7c 7c 20 70 50 61  yCache==0 || pPa
a20a0 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 3d  ger->useJournal=
a20b0 3d 30 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  =0 );.  }..  if(
a20c0 20 21 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73   !pPager->exclus
a20d0 69 76 65 4d 6f 64 65 20 29 7b 0a 20 20 20 20 72  iveMode ){.    r
a20e0 63 32 20 3d 20 73 71 6c 69 74 65 33 4f 73 55 6e  c2 = sqlite3OsUn
a20f0 6c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c  lock(pPager->fd,
a2100 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20   SHARED_LOCK);. 
a2110 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65     pPager->state
a2120 20 3d 20 50 41 47 45 52 5f 53 48 41 52 45 44 3b   = PAGER_SHARED;
a2130 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61  .  }else if( pPa
a2140 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45  ger->state==PAGE
a2150 52 5f 53 59 4e 43 45 44 20 29 7b 0a 20 20 20 20  R_SYNCED ){.    
a2160 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20  pPager->state = 
a2170 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 3b  PAGER_EXCLUSIVE;
a2180 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 6f  .  }.  pPager->o
a2190 72 69 67 44 62 53 69 7a 65 20 3d 20 30 3b 0a 20  rigDbSize = 0;. 
a21a0 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74   pPager->setMast
a21b0 65 72 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72  er = 0;.  pPager
a21c0 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a  ->needSync = 0;.
a21d0 20 20 70 50 61 67 65 72 2d 3e 70 46 69 72 73 74    pPager->pFirst
a21e0 53 79 6e 63 65 64 20 3d 20 70 50 61 67 65 72 2d  Synced = pPager-
a21f0 3e 70 46 69 72 73 74 3b 0a 20 20 70 50 61 67 65  >pFirst;.  pPage
a2200 72 2d 3e 64 62 53 69 7a 65 20 3d 20 2d 31 3b 0a  r->dbSize = -1;.
a2210 0a 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d 53  .  return (rc==S
a2220 51 4c 49 54 45 5f 4f 4b 3f 72 63 32 3a 72 63 29  QLITE_OK?rc2:rc)
a2230 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75  ;.}../*.** Compu
a2240 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20  te and return a 
a2250 63 68 65 63 6b 73 75 6d 20 66 6f 72 20 74 68 65  checksum for the
a2260 20 70 61 67 65 20 6f 66 20 64 61 74 61 2e 0a 2a   page of data..*
a2270 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 6e 6f 74  *.** This is not
a2280 20 61 20 72 65 61 6c 20 63 68 65 63 6b 73 75 6d   a real checksum
a2290 2e 20 20 49 74 20 69 73 20 72 65 61 6c 6c 79 20  .  It is really 
a22a0 6a 75 73 74 20 74 68 65 20 73 75 6d 20 6f 66 20  just the sum of 
a22b0 74 68 65 20 0a 2a 2a 20 72 61 6e 64 6f 6d 20 69  the .** random i
a22c0 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 61 6e 64  nitial value and
a22d0 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72   the page number
a22e0 2e 20 20 57 65 20 65 78 70 65 72 69 6d 65 6e 74  .  We experiment
a22f0 65 64 20 77 69 74 68 0a 2a 2a 20 61 20 63 68 65  ed with.** a che
a2300 63 6b 73 75 6d 20 6f 66 20 74 68 65 20 65 6e 74  cksum of the ent
a2310 69 72 65 20 64 61 74 61 2c 20 62 75 74 20 74 68  ire data, but th
a2320 61 74 20 77 61 73 20 66 6f 75 6e 64 20 74 6f 20  at was found to 
a2330 62 65 20 74 6f 6f 20 73 6c 6f 77 2e 0a 2a 2a 0a  be too slow..**.
a2340 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65  ** Note that the
a2350 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20   page number is 
a2360 73 74 6f 72 65 64 20 61 74 20 74 68 65 20 62 65  stored at the be
a2370 67 69 6e 6e 69 6e 67 20 6f 66 20 64 61 74 61 20  ginning of data 
a2380 61 6e 64 0a 2a 2a 20 74 68 65 20 63 68 65 63 6b  and.** the check
a2390 73 75 6d 20 69 73 20 73 74 6f 72 65 64 20 61 74  sum is stored at
a23a0 20 74 68 65 20 65 6e 64 2e 20 20 54 68 69 73 20   the end.  This 
a23b0 69 73 20 69 6d 70 6f 72 74 61 6e 74 2e 20 20 49  is important.  I
a23c0 66 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 63 6f 72  f journal.** cor
a23d0 72 75 70 74 69 6f 6e 20 6f 63 63 75 72 73 20 64  ruption occurs d
a23e0 75 65 20 74 6f 20 61 20 70 6f 77 65 72 20 66 61  ue to a power fa
a23f0 69 6c 75 72 65 2c 20 74 68 65 20 6d 6f 73 74 20  ilure, the most 
a2400 6c 69 6b 65 6c 79 20 73 63 65 6e 61 72 69 6f 0a  likely scenario.
a2410 2a 2a 20 69 73 20 74 68 61 74 20 6f 6e 65 20 65  ** is that one e
a2420 6e 64 20 6f 72 20 74 68 65 20 6f 74 68 65 72 20  nd or the other 
a2430 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 77 69  of the record wi
a2440 6c 6c 20 62 65 20 63 68 61 6e 67 65 64 2e 20 20  ll be changed.  
a2450 49 74 20 69 73 0a 2a 2a 20 6d 75 63 68 20 6c 65  It is.** much le
a2460 73 73 20 6c 69 6b 65 6c 79 20 74 68 61 74 20 74  ss likely that t
a2470 68 65 20 74 77 6f 20 65 6e 64 73 20 6f 66 20 74  he two ends of t
a2480 68 65 20 6a 6f 75 72 6e 61 6c 20 72 65 63 6f 72  he journal recor
a2490 64 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 63 6f 72  d will be.** cor
a24a0 72 65 63 74 20 61 6e 64 20 74 68 65 20 6d 69 64  rect and the mid
a24b0 64 6c 65 20 62 65 20 63 6f 72 72 75 70 74 2e 20  dle be corrupt. 
a24c0 20 54 68 75 73 2c 20 74 68 69 73 20 22 63 68 65   Thus, this "che
a24d0 63 6b 73 75 6d 22 20 73 63 68 65 6d 65 2c 0a 2a  cksum" scheme,.*
a24e0 2a 20 74 68 6f 75 67 68 20 66 61 73 74 20 61 6e  * though fast an
a24f0 64 20 73 69 6d 70 6c 65 2c 20 63 61 74 63 68 65  d simple, catche
a2500 73 20 74 68 65 20 6d 6f 73 74 6c 79 20 6c 69 6b  s the mostly lik
a2510 65 6c 79 20 6b 69 6e 64 20 6f 66 20 63 6f 72 72  ely kind of corr
a2520 75 70 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 49  uption..**.** FI
a2530 58 20 4d 45 3a 20 20 43 6f 6e 73 69 64 65 72 20  X ME:  Consider 
a2540 61 64 64 69 6e 67 20 65 76 65 72 79 20 32 30 30  adding every 200
a2550 74 68 20 28 6f 72 20 73 6f 29 20 62 79 74 65 20  th (or so) byte 
a2560 6f 66 20 74 68 65 20 64 61 74 61 20 74 6f 20 74  of the data to t
a2570 68 65 0a 2a 2a 20 63 68 65 63 6b 73 75 6d 2e 20  he.** checksum. 
a2580 20 54 68 61 74 20 77 61 79 20 69 66 20 61 20 73   That way if a s
a2590 69 6e 67 6c 65 20 70 61 67 65 20 73 70 61 6e 73  ingle page spans
a25a0 20 33 20 6f 72 20 6d 6f 72 65 20 64 69 73 6b 20   3 or more disk 
a25b0 73 65 63 74 6f 72 73 20 61 6e 64 0a 2a 2a 20 6f  sectors and.** o
a25c0 6e 6c 79 20 74 68 65 20 6d 69 64 64 6c 65 20 73  nly the middle s
a25d0 65 63 74 6f 72 20 69 73 20 63 6f 72 72 75 70 74  ector is corrupt
a25e0 2c 20 77 65 20 77 69 6c 6c 20 73 74 69 6c 6c 20  , we will still 
a25f0 68 61 76 65 20 61 20 72 65 61 73 6f 6e 61 62 6c  have a reasonabl
a2600 65 0a 2a 2a 20 63 68 61 6e 63 65 20 6f 66 20 66  e.** chance of f
a2610 61 69 6c 69 6e 67 20 74 68 65 20 63 68 65 63 6b  ailing the check
a2620 73 75 6d 20 61 6e 64 20 74 68 75 73 20 64 65 74  sum and thus det
a2630 65 63 74 69 6e 67 20 74 68 65 20 70 72 6f 62 6c  ecting the probl
a2640 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 33  em..*/.static u3
a2650 32 20 70 61 67 65 72 5f 63 6b 73 75 6d 28 50 61  2 pager_cksum(Pa
a2660 67 65 72 20 2a 70 50 61 67 65 72 2c 20 63 6f 6e  ger *pPager, con
a2670 73 74 20 75 38 20 2a 61 44 61 74 61 29 7b 0a 20  st u8 *aData){. 
a2680 20 75 33 32 20 63 6b 73 75 6d 20 3d 20 70 50 61   u32 cksum = pPa
a2690 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 3b 0a  ger->cksumInit;.
a26a0 20 20 69 6e 74 20 69 20 3d 20 70 50 61 67 65 72    int i = pPager
a26b0 2d 3e 70 61 67 65 53 69 7a 65 2d 32 30 30 3b 0a  ->pageSize-200;.
a26c0 20 20 77 68 69 6c 65 28 20 69 3e 30 20 29 7b 0a    while( i>0 ){.
a26d0 20 20 20 20 63 6b 73 75 6d 20 2b 3d 20 61 44 61      cksum += aDa
a26e0 74 61 5b 69 5d 3b 0a 20 20 20 20 69 20 2d 3d 20  ta[i];.    i -= 
a26f0 32 30 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  200;.  }.  retur
a2700 6e 20 63 6b 73 75 6d 3b 0a 7d 0a 0a 2f 2a 20 46  n cksum;.}../* F
a2710 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69  orward declarati
a2720 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  on */.static voi
a2730 64 20 6d 61 6b 65 43 6c 65 61 6e 28 50 67 48 64  d makeClean(PgHd
a2740 72 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64  r*);../*.** Read
a2750 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 66   a single page f
a2760 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  rom the journal 
a2770 66 69 6c 65 20 6f 70 65 6e 65 64 20 6f 6e 20 66  file opened on f
a2780 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 0a 2a  ile descriptor.*
a2790 2a 20 6a 66 64 2e 20 20 50 6c 61 79 62 61 63 6b  * jfd.  Playback
a27a0 20 74 68 69 73 20 6f 6e 65 20 70 61 67 65 2e 0a   this one page..
a27b0 2a 2a 0a 2a 2a 20 49 66 20 75 73 65 43 6b 73 75  **.** If useCksu
a27c0 6d 3d 3d 30 20 69 74 20 6d 65 61 6e 73 20 74 68  m==0 it means th
a27d0 69 73 20 6a 6f 75 72 6e 61 6c 20 64 6f 65 73 20  is journal does 
a27e0 6e 6f 74 20 75 73 65 20 63 68 65 63 6b 73 75 6d  not use checksum
a27f0 73 2e 20 20 43 68 65 63 6b 73 75 6d 73 0a 2a 2a  s.  Checksums.**
a2800 20 61 72 65 20 6e 6f 74 20 75 73 65 64 20 69 6e   are not used in
a2810 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e   statement journ
a2820 61 6c 73 20 62 65 63 61 75 73 65 20 73 74 61 74  als because stat
a2830 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 73 20 64  ement journals d
a2840 6f 20 6e 6f 74 0a 2a 2a 20 6e 65 65 64 20 74 6f  o not.** need to
a2850 20 73 75 72 76 69 76 65 20 70 6f 77 65 72 20 66   survive power f
a2860 61 69 6c 75 72 65 73 2e 0a 2a 2f 0a 73 74 61 74  ailures..*/.stat
a2870 69 63 20 69 6e 74 20 70 61 67 65 72 5f 70 6c 61  ic int pager_pla
a2880 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 50  yback_one_page(P
a2890 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 4f 73  ager *pPager, Os
a28a0 46 69 6c 65 20 2a 6a 66 64 2c 20 69 6e 74 20 75  File *jfd, int u
a28b0 73 65 43 6b 73 75 6d 29 7b 0a 20 20 69 6e 74 20  seCksum){.  int 
a28c0 72 63 3b 0a 20 20 50 67 48 64 72 20 2a 70 50 67  rc;.  PgHdr *pPg
a28d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
a28e0 20 20 20 20 2f 2a 20 41 6e 20 65 78 69 73 74 69      /* An existi
a28f0 6e 67 20 70 61 67 65 20 69 6e 20 74 68 65 20 63  ng page in the c
a2900 61 63 68 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70  ache */.  Pgno p
a2910 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20  gno;            
a2920 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
a2930 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 20  age number of a 
a2940 70 61 67 65 20 69 6e 20 6a 6f 75 72 6e 61 6c 20  page in journal 
a2950 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d 3b 20  */.  u32 cksum; 
a2960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2970 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d 20 75     /* Checksum u
a2980 73 65 64 20 66 6f 72 20 73 61 6e 69 74 79 20 63  sed for sanity c
a2990 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 75 38 20  hecking */.  u8 
a29a0 2a 61 44 61 74 61 20 3d 20 28 75 38 20 2a 29 70  *aData = (u8 *)p
a29b0 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65  Pager->pTmpSpace
a29c0 3b 20 20 20 2f 2a 20 54 65 6d 70 20 73 74 6f 72  ;   /* Temp stor
a29d0 61 67 65 20 66 6f 72 20 61 20 70 61 67 65 20 2a  age for a page *
a29e0 2f 0a 0a 20 20 2f 2a 20 75 73 65 43 6b 73 75 6d  /..  /* useCksum
a29f0 20 73 68 6f 75 6c 64 20 62 65 20 74 72 75 65 20   should be true 
a2a00 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75  for the main jou
a2a10 72 6e 61 6c 20 61 6e 64 20 66 61 6c 73 65 20 66  rnal and false f
a2a20 6f 72 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e  or.  ** statemen
a2a30 74 20 6a 6f 75 72 6e 61 6c 73 2e 20 20 56 65 72  t journals.  Ver
a2a40 69 66 79 20 74 68 61 74 20 74 68 69 73 20 69 73  ify that this is
a2a50 20 61 6c 77 61 79 73 20 74 68 65 20 63 61 73 65   always the case
a2a60 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
a2a70 6a 66 64 20 3d 3d 20 28 75 73 65 43 6b 73 75 6d  jfd == (useCksum
a2a80 20 3f 20 70 50 61 67 65 72 2d 3e 6a 66 64 20 3a   ? pPager->jfd :
a2a90 20 70 50 61 67 65 72 2d 3e 73 74 66 64 29 20 29   pPager->stfd) )
a2aa0 3b 0a 20 20 61 73 73 65 72 74 28 20 61 44 61 74  ;.  assert( aDat
a2ab0 61 20 29 3b 0a 0a 20 20 72 63 20 3d 20 72 65 61  a );..  rc = rea
a2ac0 64 33 32 62 69 74 73 28 6a 66 64 2c 20 26 70 67  d32bits(jfd, &pg
a2ad0 6e 6f 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  no);.  if( rc!=S
a2ae0 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
a2af0 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c  n rc;.  rc = sql
a2b00 69 74 65 33 4f 73 52 65 61 64 28 6a 66 64 2c 20  ite3OsRead(jfd, 
a2b10 61 44 61 74 61 2c 20 70 50 61 67 65 72 2d 3e 70  aData, pPager->p
a2b20 61 67 65 53 69 7a 65 29 3b 0a 20 20 69 66 28 20  ageSize);.  if( 
a2b30 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
a2b40 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 70 50 61  return rc;.  pPa
a2b50 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20  ger->journalOff 
a2b60 2b 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53  += pPager->pageS
a2b70 69 7a 65 20 2b 20 34 3b 0a 0a 20 20 2f 2a 20 53  ize + 4;..  /* S
a2b80 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 6f  anity checking o
a2b90 6e 20 74 68 65 20 70 61 67 65 2e 20 20 54 68 69  n the page.  Thi
a2ba0 73 20 69 73 20 6d 6f 72 65 20 69 6d 70 6f 72 74  s is more import
a2bb0 61 6e 74 20 74 68 61 74 20 49 20 6f 72 69 67 69  ant that I origi
a2bc0 6e 61 6c 6c 79 0a 20 20 2a 2a 20 74 68 6f 75 67  nally.  ** thoug
a2bd0 68 74 2e 20 20 49 66 20 61 20 70 6f 77 65 72 20  ht.  If a power 
a2be0 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 20 77  failure occurs w
a2bf0 68 69 6c 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c  hile the journal
a2c00 20 69 73 20 62 65 69 6e 67 20 77 72 69 74 74 65   is being writte
a2c10 6e 2c 0a 20 20 2a 2a 20 69 74 20 63 6f 75 6c 64  n,.  ** it could
a2c20 20 63 61 75 73 65 20 69 6e 76 61 6c 69 64 20 64   cause invalid d
a2c30 61 74 61 20 74 6f 20 62 65 20 77 72 69 74 74 65  ata to be writte
a2c40 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e  n into the journ
a2c50 61 6c 2e 20 20 57 65 20 6e 65 65 64 20 74 6f 0a  al.  We need to.
a2c60 20 20 2a 2a 20 64 65 74 65 63 74 20 74 68 69 73    ** detect this
a2c70 20 69 6e 76 61 6c 69 64 20 64 61 74 61 20 28 77   invalid data (w
a2c80 69 74 68 20 68 69 67 68 20 70 72 6f 62 61 62 69  ith high probabi
a2c90 6c 69 74 79 29 20 61 6e 64 20 69 67 6e 6f 72 65  lity) and ignore
a2ca0 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20   it..  */.  if( 
a2cb0 70 67 6e 6f 3d 3d 30 20 7c 7c 20 70 67 6e 6f 3d  pgno==0 || pgno=
a2cc0 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70  =PAGER_MJ_PGNO(p
a2cd0 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 72 65  Pager) ){.    re
a2ce0 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45  turn SQLITE_DONE
a2cf0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 67 6e 6f  ;.  }.  if( pgno
a2d00 3e 28 75 6e 73 69 67 6e 65 64 29 70 50 61 67 65  >(unsigned)pPage
a2d10 72 2d 3e 64 62 53 69 7a 65 20 29 7b 0a 20 20 20  r->dbSize ){.   
a2d20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
a2d30 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 75 73 65  K;.  }.  if( use
a2d40 43 6b 73 75 6d 20 29 7b 0a 20 20 20 20 72 63 20  Cksum ){.    rc 
a2d50 3d 20 72 65 61 64 33 32 62 69 74 73 28 6a 66 64  = read32bits(jfd
a2d60 2c 20 26 63 6b 73 75 6d 29 3b 0a 20 20 20 20 69  , &cksum);.    i
a2d70 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
a2d80 63 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a  c;.    pPager->j
a2d90 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 34 3b 0a  ournalOff += 4;.
a2da0 20 20 20 20 69 66 28 20 70 61 67 65 72 5f 63 6b      if( pager_ck
a2db0 73 75 6d 28 70 50 61 67 65 72 2c 20 61 44 61 74  sum(pPager, aDat
a2dc0 61 29 21 3d 63 6b 73 75 6d 20 29 7b 0a 20 20 20  a)!=cksum ){.   
a2dd0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
a2de0 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d  _DONE;.    }.  }
a2df0 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ..  assert( pPag
a2e00 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52  er->state==PAGER
a2e10 5f 52 45 53 45 52 56 45 44 20 7c 7c 20 70 50 61  _RESERVED || pPa
a2e20 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45  ger->state>=PAGE
a2e30 52 5f 45 58 43 4c 55 53 49 56 45 20 29 3b 0a 0a  R_EXCLUSIVE );..
a2e40 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65    /* If the page
a2e50 72 20 69 73 20 69 6e 20 52 45 53 45 52 56 45 44  r is in RESERVED
a2e60 20 73 74 61 74 65 2c 20 74 68 65 6e 20 74 68 65   state, then the
a2e70 72 65 20 6d 75 73 74 20 62 65 20 61 20 63 6f 70  re must be a cop
a2e80 79 20 6f 66 20 74 68 69 73 0a 20 20 2a 2a 20 70  y of this.  ** p
a2e90 61 67 65 20 69 6e 20 74 68 65 20 70 61 67 65 72  age in the pager
a2ea0 20 63 61 63 68 65 2e 20 49 6e 20 74 68 69 73 20   cache. In this 
a2eb0 63 61 73 65 20 6a 75 73 74 20 75 70 64 61 74 65  case just update
a2ec0 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65   the pager cache
a2ed0 2c 0a 20 20 2a 2a 20 6e 6f 74 20 74 68 65 20 64  ,.  ** not the d
a2ee0 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68  atabase file. Th
a2ef0 65 20 70 61 67 65 20 69 73 20 6c 65 66 74 20 6d  e page is left m
a2f00 61 72 6b 65 64 20 64 69 72 74 79 20 69 6e 20 74  arked dirty in t
a2f10 68 69 73 20 63 61 73 65 2e 0a 20 20 2a 2a 0a 20  his case..  **. 
a2f20 20 2a 2a 20 41 6e 20 65 78 63 65 70 74 69 6f 6e   ** An exception
a2f30 20 74 6f 20 74 68 65 20 61 62 6f 76 65 20 72 75   to the above ru
a2f40 6c 65 3a 20 49 66 20 74 68 65 20 64 61 74 61 62  le: If the datab
a2f50 61 73 65 20 69 73 20 69 6e 20 6e 6f 2d 73 79 6e  ase is in no-syn
a2f60 63 20 6d 6f 64 65 0a 20 20 2a 2a 20 61 6e 64 20  c mode.  ** and 
a2f70 61 20 70 61 67 65 20 69 73 20 6d 6f 76 65 64 20  a page is moved 
a2f80 64 75 72 69 6e 67 20 61 6e 20 69 6e 63 72 65 6d  during an increm
a2f90 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 74 68 65  ental vacuum the
a2fa0 6e 20 74 68 65 20 70 61 67 65 20 6d 61 79 0a 20  n the page may. 
a2fb0 20 2a 2a 20 6e 6f 74 20 62 65 20 69 6e 20 74 68   ** not be in th
a2fc0 65 20 70 61 67 65 72 20 63 61 63 68 65 2e 20 4c  e pager cache. L
a2fd0 61 74 65 72 3a 20 69 66 20 61 20 6d 61 6c 6c 6f  ater: if a mallo
a2fe0 63 28 29 20 6f 72 20 49 4f 20 65 72 72 6f 72 20  c() or IO error 
a2ff0 6f 63 63 75 72 73 0a 20 20 2a 2a 20 64 75 72 69  occurs.  ** duri
a3000 6e 67 20 61 20 4d 6f 76 65 70 61 67 65 28 29 20  ng a Movepage() 
a3010 63 61 6c 6c 2c 20 74 68 65 6e 20 74 68 65 20 70  call, then the p
a3020 61 67 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 69  age may not be i
a3030 6e 20 74 68 65 20 63 61 63 68 65 0a 20 20 2a 2a  n the cache.  **
a3040 20 65 69 74 68 65 72 2e 20 53 6f 20 74 68 65 20   either. So the 
a3050 63 6f 6e 64 69 74 69 6f 6e 20 64 65 73 63 72 69  condition descri
a3060 62 65 64 20 69 6e 20 74 68 65 20 61 62 6f 76 65  bed in the above
a3070 20 70 61 72 61 67 72 61 70 68 20 69 73 20 6e 6f   paragraph is no
a3080 74 0a 20 20 2a 2a 20 61 73 73 65 72 74 28 29 61  t.  ** assert()a
a3090 62 6c 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49  ble..  **.  ** I
a30a0 66 20 69 6e 20 45 58 43 4c 55 53 49 56 45 20 73  f in EXCLUSIVE s
a30b0 74 61 74 65 2c 20 74 68 65 6e 20 77 65 20 75 70  tate, then we up
a30c0 64 61 74 65 20 74 68 65 20 70 61 67 65 72 20 63  date the pager c
a30d0 61 63 68 65 20 69 66 20 69 74 20 65 78 69 73 74  ache if it exist
a30e0 73 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 6d  s.  ** and the m
a30f0 61 69 6e 20 66 69 6c 65 2e 20 54 68 65 20 70 61  ain file. The pa
a3100 67 65 20 69 73 20 74 68 65 6e 20 6d 61 72 6b 65  ge is then marke
a3110 64 20 6e 6f 74 20 64 69 72 74 79 2e 0a 20 20 2a  d not dirty..  *
a3120 2a 0a 20 20 2a 2a 20 54 69 63 6b 65 74 20 23 31  *.  ** Ticket #1
a3130 31 37 31 3a 20 20 54 68 65 20 73 74 61 74 65 6d  171:  The statem
a3140 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68  ent journal migh
a3150 74 20 63 6f 6e 74 61 69 6e 20 70 61 67 65 20 63  t contain page c
a3160 6f 6e 74 65 6e 74 20 74 68 61 74 20 69 73 0a 20  ontent that is. 
a3170 20 2a 2a 20 64 69 66 66 65 72 65 6e 74 20 66 72   ** different fr
a3180 6f 6d 20 74 68 65 20 70 61 67 65 20 63 6f 6e 74  om the page cont
a3190 65 6e 74 20 61 74 20 74 68 65 20 73 74 61 72 74  ent at the start
a31a0 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61 63 74   of the transact
a31b0 69 6f 6e 2e 0a 20 20 2a 2a 20 54 68 69 73 20 6f  ion..  ** This o
a31c0 63 63 75 72 73 20 77 68 65 6e 20 61 20 70 61 67  ccurs when a pag
a31d0 65 20 69 73 20 63 68 61 6e 67 65 64 20 70 72 69  e is changed pri
a31e0 6f 72 20 74 6f 20 74 68 65 20 73 74 61 72 74 20  or to the start 
a31f0 6f 66 20 61 20 73 74 61 74 65 6d 65 6e 74 0a 20  of a statement. 
a3200 20 2a 2a 20 74 68 65 6e 20 63 68 61 6e 67 65 64   ** then changed
a3210 20 61 67 61 69 6e 20 77 69 74 68 69 6e 20 74 68   again within th
a3220 65 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 57 68  e statement.  Wh
a3230 65 6e 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20  en rolling back 
a3240 73 75 63 68 20 61 0a 20 20 2a 2a 20 73 74 61 74  such a.  ** stat
a3250 65 6d 65 6e 74 20 77 65 20 6d 75 73 74 20 6e 6f  ement we must no
a3260 74 20 77 72 69 74 65 20 74 6f 20 74 68 65 20 6f  t write to the o
a3270 72 69 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65  riginal database
a3280 20 75 6e 6c 65 73 73 20 77 65 20 6b 6e 6f 77 0a   unless we know.
a3290 20 20 2a 2a 20 66 6f 72 20 63 65 72 74 61 69 6e    ** for certain
a32a0 20 74 68 61 74 20 6f 72 69 67 69 6e 61 6c 20 70   that original p
a32b0 61 67 65 20 63 6f 6e 74 65 6e 74 73 20 61 72 65  age contents are
a32c0 20 73 79 6e 63 65 64 20 69 6e 74 6f 20 74 68 65   synced into the
a32d0 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 0a 20   main rollback. 
a32e0 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2e 20 20 4f 74   ** journal.  Ot
a32f0 68 65 72 77 69 73 65 2c 20 61 20 70 6f 77 65 72  herwise, a power
a3300 20 6c 6f 73 73 20 6d 69 67 68 74 20 6c 65 61 76   loss might leav
a3310 65 20 6d 6f 64 69 66 69 65 64 20 64 61 74 61 20  e modified data 
a3320 69 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61  in the.  ** data
a3330 62 61 73 65 20 66 69 6c 65 20 77 69 74 68 6f 75  base file withou
a3340 74 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 74 68  t an entry in th
a3350 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  e rollback journ
a3360 61 6c 20 74 68 61 74 20 63 61 6e 0a 20 20 2a 2a  al that can.  **
a3370 20 72 65 73 74 6f 72 65 20 74 68 65 20 64 61 74   restore the dat
a3380 61 62 61 73 65 20 74 6f 20 69 74 73 20 6f 72 69  abase to its ori
a3390 67 69 6e 61 6c 20 66 6f 72 6d 2e 20 20 54 77 6f  ginal form.  Two
a33a0 20 63 6f 6e 64 69 74 69 6f 6e 73 20 6d 75 73 74   conditions must
a33b0 20 62 65 0a 20 20 2a 2a 20 6d 65 74 20 62 65 66   be.  ** met bef
a33c0 6f 72 65 20 77 72 69 74 69 6e 67 20 74 6f 20 74  ore writing to t
a33d0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
a33e0 73 2e 20 28 31 29 20 74 68 65 20 64 61 74 61 62  s. (1) the datab
a33f0 61 73 65 20 6d 75 73 74 20 62 65 0a 20 20 2a 2a  ase must be.  **
a3400 20 6c 6f 63 6b 65 64 2e 20 20 28 32 29 20 77 65   locked.  (2) we
a3410 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 20 6f   know that the o
a3420 72 69 67 69 6e 61 6c 20 70 61 67 65 20 63 6f 6e  riginal page con
a3430 74 65 6e 74 20 69 73 20 66 75 6c 6c 79 20 73 79  tent is fully sy
a3440 6e 63 65 64 0a 20 20 2a 2a 20 69 6e 20 74 68 65  nced.  ** in the
a3450 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 65 69   main journal ei
a3460 74 68 65 72 20 62 65 63 61 75 73 65 20 74 68 65  ther because the
a3470 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e 20   page is not in 
a3480 63 61 63 68 65 20 6f 72 20 65 6c 73 65 0a 20 20  cache or else.  
a3490 2a 2a 20 74 68 65 20 70 61 67 65 20 69 73 20 6d  ** the page is m
a34a0 61 72 6b 65 64 20 61 73 20 6e 65 65 64 53 79 6e  arked as needSyn
a34b0 63 3d 3d 30 2e 0a 20 20 2a 2f 0a 20 20 70 50 67  c==0..  */.  pPg
a34c0 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28   = pager_lookup(
a34d0 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 3b 0a 20  pPager, pgno);. 
a34e0 20 50 41 47 45 52 54 52 41 43 45 34 28 22 50 4c   PAGERTRACE4("PL
a34f0 41 59 42 41 43 4b 20 25 64 20 70 61 67 65 20 25  AYBACK %d page %
a3500 64 20 68 61 73 68 28 25 30 38 78 29 5c 6e 22 2c  d hash(%08x)\n",
a3510 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
a3520 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c  PAGERID(pPager),
a3530 20 70 67 6e 6f 2c 20 70 61 67 65 72 5f 64 61 74   pgno, pager_dat
a3540 61 68 61 73 68 28 70 50 61 67 65 72 2d 3e 70 61  ahash(pPager->pa
a3550 67 65 53 69 7a 65 2c 20 61 44 61 74 61 29 29 3b  geSize, aData));
a3560 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73  .  if( pPager->s
a3570 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c  tate>=PAGER_EXCL
a3580 55 53 49 56 45 20 26 26 20 28 70 50 67 3d 3d 30  USIVE && (pPg==0
a3590 20 7c 7c 20 70 50 67 2d 3e 6e 65 65 64 53 79 6e   || pPg->needSyn
a35a0 63 3d 3d 30 29 20 29 7b 0a 20 20 20 20 72 63 20  c==0) ){.    rc 
a35b0 3d 20 73 71 6c 69 74 65 33 4f 73 53 65 65 6b 28  = sqlite3OsSeek(
a35c0 70 50 61 67 65 72 2d 3e 66 64 2c 20 28 70 67 6e  pPager->fd, (pgn
a35d0 6f 2d 31 29 2a 28 69 36 34 29 70 50 61 67 65 72  o-1)*(i64)pPager
a35e0 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20  ->pageSize);.   
a35f0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
a3600 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  OK ){.      rc =
a3610 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28   sqlite3OsWrite(
a3620 70 50 61 67 65 72 2d 3e 66 64 2c 20 61 44 61 74  pPager->fd, aDat
a3630 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53  a, pPager->pageS
a3640 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ize);.    }.    
a3650 69 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 20  if( pPg ){.     
a3660 20 6d 61 6b 65 43 6c 65 61 6e 28 70 50 67 29 3b   makeClean(pPg);
a3670 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
a3680 20 70 50 67 20 29 7b 0a 20 20 20 20 2f 2a 20 4e   pPg ){.    /* N
a3690 6f 20 70 61 67 65 20 73 68 6f 75 6c 64 20 65 76  o page should ev
a36a0 65 72 20 62 65 20 65 78 70 6c 69 63 69 74 6c 79  er be explicitly
a36b0 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 68 61   rolled back tha
a36c0 74 20 69 73 20 69 6e 20 75 73 65 2c 20 65 78 63  t is in use, exc
a36d0 65 70 74 0a 20 20 20 20 2a 2a 20 66 6f 72 20 70  ept.    ** for p
a36e0 61 67 65 20 31 20 77 68 69 63 68 20 69 73 20 68  age 1 which is h
a36f0 65 6c 64 20 69 6e 20 75 73 65 20 69 6e 20 6f 72  eld in use in or
a3700 64 65 72 20 74 6f 20 6b 65 65 70 20 74 68 65 20  der to keep the 
a3710 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20 20 20  lock on the.    
a3720 2a 2a 20 64 61 74 61 62 61 73 65 20 61 63 74 69  ** database acti
a3730 76 65 2e 20 48 6f 77 65 76 65 72 20 73 75 63 68  ve. However such
a3740 20 61 20 70 61 67 65 20 6d 61 79 20 62 65 20 72   a page may be r
a3750 6f 6c 6c 65 64 20 62 61 63 6b 20 61 73 20 61 20  olled back as a 
a3760 72 65 73 75 6c 74 0a 20 20 20 20 2a 2a 20 6f 66  result.    ** of
a3770 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 65 72 72   an internal err
a3780 6f 72 20 72 65 73 75 6c 74 69 6e 67 20 69 6e 20  or resulting in 
a3790 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 63 61 6c  an automatic cal
a37a0 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c 69  l to.    ** sqli
a37b0 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b  te3PagerRollback
a37c0 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 76  ()..    */.    v
a37d0 6f 69 64 20 2a 70 44 61 74 61 3b 0a 20 20 20 20  oid *pData;.    
a37e0 2f 2a 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e  /* assert( pPg->
a37f0 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 50 67 2d 3e  nRef==0 || pPg->
a3800 70 67 6e 6f 3d 3d 31 20 29 3b 20 2a 2f 0a 20 20  pgno==1 ); */.  
a3810 20 20 70 44 61 74 61 20 3d 20 50 47 48 44 52 5f    pData = PGHDR_
a3820 54 4f 5f 44 41 54 41 28 70 50 67 29 3b 0a 20 20  TO_DATA(pPg);.  
a3830 20 20 6d 65 6d 63 70 79 28 70 44 61 74 61 2c 20    memcpy(pData, 
a3840 61 44 61 74 61 2c 20 70 50 61 67 65 72 2d 3e 70  aData, pPager->p
a3850 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 69 66  ageSize);.    if
a3860 28 20 70 50 61 67 65 72 2d 3e 78 52 65 69 6e 69  ( pPager->xReini
a3870 74 65 72 20 29 7b 0a 20 20 20 20 20 20 70 50 61  ter ){.      pPa
a3880 67 65 72 2d 3e 78 52 65 69 6e 69 74 65 72 28 70  ger->xReiniter(p
a3890 50 67 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65  Pg, pPager->page
a38a0 53 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 23 69 66  Size);.    }.#if
a38b0 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b  def SQLITE_CHECK
a38c0 5f 50 41 47 45 53 0a 20 20 20 20 70 50 67 2d 3e  _PAGES.    pPg->
a38d0 70 61 67 65 48 61 73 68 20 3d 20 70 61 67 65 72  pageHash = pager
a38e0 5f 70 61 67 65 68 61 73 68 28 70 50 67 29 3b 0a  _pagehash(pPg);.
a38f0 23 65 6e 64 69 66 0a 20 20 20 20 2f 2a 20 49 66  #endif.    /* If
a3900 20 74 68 69 73 20 77 61 73 20 70 61 67 65 20 31   this was page 1
a3910 2c 20 74 68 65 6e 20 72 65 73 74 6f 72 65 20 74  , then restore t
a3920 68 65 20 76 61 6c 75 65 20 6f 66 20 50 61 67 65  he value of Page
a3930 72 2e 64 62 46 69 6c 65 56 65 72 73 2e 0a 20 20  r.dbFileVers..  
a3940 20 20 2a 2a 20 44 6f 20 74 68 69 73 20 62 65 66    ** Do this bef
a3950 6f 72 65 20 61 6e 79 20 64 65 63 6f 64 69 6e 67  ore any decoding
a3960 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 67 6e  . */.    if( pgn
a3970 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 6d 65  o==1 ){.      me
a3980 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e 64 62  mcpy(&pPager->db
a3990 46 69 6c 65 56 65 72 73 2c 20 26 28 28 75 38 2a  FileVers, &((u8*
a39a0 29 70 44 61 74 61 29 5b 32 34 5d 2c 73 69 7a 65  )pData)[24],size
a39b0 6f 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69 6c  of(pPager->dbFil
a39c0 65 56 65 72 73 29 29 3b 0a 20 20 20 20 7d 0a 0a  eVers));.    }..
a39d0 20 20 20 20 2f 2a 20 44 65 63 6f 64 65 20 74 68      /* Decode th
a39e0 65 20 70 61 67 65 20 6a 75 73 74 20 72 65 61 64  e page just read
a39f0 20 66 72 6f 6d 20 64 69 73 6b 20 2a 2f 0a 20 20   from disk */.  
a3a00 20 20 43 4f 44 45 43 31 28 70 50 61 67 65 72 2c    CODEC1(pPager,
a3a10 20 70 44 61 74 61 2c 20 70 50 67 2d 3e 70 67 6e   pData, pPg->pgn
a3a20 6f 2c 20 33 29 3b 0a 20 20 7d 0a 20 20 72 65 74  o, 3);.  }.  ret
a3a30 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
a3a40 20 50 61 72 61 6d 65 74 65 72 20 7a 4d 61 73 74   Parameter zMast
a3a50 65 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f  er is the name o
a3a60 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  f a master journ
a3a70 61 6c 20 66 69 6c 65 2e 20 41 20 73 69 6e 67 6c  al file. A singl
a3a80 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c  e journal.** fil
a3a90 65 20 74 68 61 74 20 72 65 66 65 72 72 65 64 20  e that referred 
a3aa0 74 6f 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  to the master jo
a3ab0 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 73 20 6a  urnal file has j
a3ac0 75 73 74 20 62 65 65 6e 20 72 6f 6c 6c 65 64 20  ust been rolled 
a3ad0 62 61 63 6b 2e 0a 2a 2a 20 54 68 69 73 20 72 6f  back..** This ro
a3ae0 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20  utine checks if 
a3af0 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74  it is possible t
a3b00 6f 20 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73  o delete the mas
a3b10 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
a3b20 2c 0a 2a 2a 20 61 6e 64 20 64 6f 65 73 20 73 6f  ,.** and does so
a3b30 20 69 66 20 69 74 20 69 73 2e 0a 2a 2a 0a 2a 2a   if it is..**.**
a3b40 20 54 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   The master jour
a3b50 6e 61 6c 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  nal file contain
a3b60 73 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 61  s the names of a
a3b70 6c 6c 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c  ll child journal
a3b80 73 2e 0a 2a 2a 20 54 6f 20 74 65 6c 6c 20 69 66  s..** To tell if
a3b90 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61   a master journa
a3ba0 6c 20 63 61 6e 20 62 65 20 64 65 6c 65 74 65 64  l can be deleted
a3bb0 2c 20 63 68 65 63 6b 20 74 6f 20 65 61 63 68 20  , check to each 
a3bc0 6f 66 20 74 68 65 0a 2a 2a 20 63 68 69 6c 64 72  of the.** childr
a3bd0 65 6e 2e 20 20 49 66 20 61 6c 6c 20 63 68 69 6c  en.  If all chil
a3be0 64 72 65 6e 20 61 72 65 20 65 69 74 68 65 72 20  dren are either 
a3bf0 6d 69 73 73 69 6e 67 20 6f 72 20 64 6f 20 6e 6f  missing or do no
a3c00 74 20 72 65 66 65 72 20 74 6f 0a 2a 2a 20 61 20  t refer to.** a 
a3c10 64 69 66 66 65 72 65 6e 74 20 6d 61 73 74 65 72  different master
a3c20 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 65 6e 20 74   journal, then t
a3c30 68 69 73 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  his master journ
a3c40 61 6c 20 63 61 6e 20 62 65 20 64 65 6c 65 74 65  al can be delete
a3c50 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
a3c60 20 70 61 67 65 72 5f 64 65 6c 6d 61 73 74 65 72   pager_delmaster
a3c70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61  (const char *zMa
a3c80 73 74 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b  ster){.  int rc;
a3c90 0a 20 20 69 6e 74 20 6d 61 73 74 65 72 5f 6f 70  .  int master_op
a3ca0 65 6e 20 3d 20 30 3b 0a 20 20 4f 73 46 69 6c 65  en = 0;.  OsFile
a3cb0 20 2a 6d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20   *master = 0;.  
a3cc0 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 4a 6f 75  char *zMasterJou
a3cd0 72 6e 61 6c 20 3d 20 30 3b 20 2f 2a 20 43 6f 6e  rnal = 0; /* Con
a3ce0 74 65 6e 74 73 20 6f 66 20 6d 61 73 74 65 72 20  tents of master 
a3cf0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a  journal file */.
a3d00 20 20 69 36 34 20 6e 4d 61 73 74 65 72 4a 6f 75    i64 nMasterJou
a3d10 72 6e 61 6c 3b 20 20 20 20 20 20 20 2f 2a 20 53  rnal;       /* S
a3d20 69 7a 65 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f  ize of master jo
a3d30 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 0a 20  urnal file */.. 
a3d40 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 6d 61 73   /* Open the mas
a3d50 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
a3d60 20 65 78 63 6c 75 73 69 76 65 6c 79 20 69 6e 20   exclusively in 
a3d70 63 61 73 65 20 73 6f 6d 65 20 6f 74 68 65 72 20  case some other 
a3d80 70 72 6f 63 65 73 73 0a 20 20 2a 2a 20 69 73 20  process.  ** is 
a3d90 72 75 6e 6e 69 6e 67 20 74 68 69 73 20 72 6f 75  running this rou
a3da0 74 69 6e 65 20 61 6c 73 6f 2e 20 4e 6f 74 20 74  tine also. Not t
a3db0 68 61 74 20 69 74 20 6d 61 6b 65 73 20 74 6f 6f  hat it makes too
a3dc0 20 6d 75 63 68 20 64 69 66 66 65 72 65 6e 63 65   much difference
a3dd0 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71  ..  */.  rc = sq
a3de0 6c 69 74 65 33 4f 73 4f 70 65 6e 52 65 61 64 4f  lite3OsOpenReadO
a3df0 6e 6c 79 28 7a 4d 61 73 74 65 72 2c 20 26 6d 61  nly(zMaster, &ma
a3e00 73 74 65 72 29 3b 0a 20 20 61 73 73 65 72 74 28  ster);.  assert(
a3e10 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc!=SQLITE_OK |
a3e20 7c 20 6d 61 73 74 65 72 20 29 3b 0a 20 20 69 66  | master );.  if
a3e30 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
a3e40 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72  ) goto delmaster
a3e50 5f 6f 75 74 3b 0a 20 20 6d 61 73 74 65 72 5f 6f  _out;.  master_o
a3e60 70 65 6e 20 3d 20 31 3b 0a 20 20 72 63 20 3d 20  pen = 1;.  rc = 
a3e70 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a  sqlite3OsFileSiz
a3e80 65 28 6d 61 73 74 65 72 2c 20 26 6e 4d 61 73 74  e(master, &nMast
a3e90 65 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 69 66  erJournal);.  if
a3ea0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
a3eb0 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72  ) goto delmaster
a3ec0 5f 6f 75 74 3b 0a 0a 20 20 69 66 28 20 6e 4d 61  _out;..  if( nMa
a3ed0 73 74 65 72 4a 6f 75 72 6e 61 6c 3e 30 20 29 7b  sterJournal>0 ){
a3ee0 0a 20 20 20 20 63 68 61 72 20 2a 7a 4a 6f 75 72  .    char *zJour
a3ef0 6e 61 6c 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a  nal;.    char *z
a3f00 4d 61 73 74 65 72 50 74 72 20 3d 20 30 3b 0a 0a  MasterPtr = 0;..
a3f10 20 20 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20      /* Load the 
a3f20 65 6e 74 69 72 65 20 6d 61 73 74 65 72 20 6a 6f  entire master jo
a3f30 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f 20  urnal file into 
a3f40 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64 20 66  space obtained f
a3f50 72 6f 6d 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74  rom.    ** sqlit
a3f60 65 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 70 6f  eMalloc() and po
a3f70 69 6e 74 65 64 20 74 6f 20 62 79 20 7a 4d 61 73  inted to by zMas
a3f80 74 65 72 4a 6f 75 72 6e 61 6c 2e 20 0a 20 20 20  terJournal. .   
a3f90 20 2a 2f 0a 20 20 20 20 7a 4d 61 73 74 65 72 4a   */.    zMasterJ
a3fa0 6f 75 72 6e 61 6c 20 3d 20 28 63 68 61 72 20 2a  ournal = (char *
a3fb0 29 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 6e 4d  )sqliteMalloc(nM
a3fc0 61 73 74 65 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20  asterJournal);. 
a3fd0 20 20 20 69 66 28 20 21 7a 4d 61 73 74 65 72 4a     if( !zMasterJ
a3fe0 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20  ournal ){.      
a3ff0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
a4000 4d 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 64 65  M;.      goto de
a4010 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20  lmaster_out;.   
a4020 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69   }.    rc = sqli
a4030 74 65 33 4f 73 52 65 61 64 28 6d 61 73 74 65 72  te3OsRead(master
a4040 2c 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  , zMasterJournal
a4050 2c 20 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  , nMasterJournal
a4060 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
a4070 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
a4080 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 0a  delmaster_out;..
a4090 20 20 20 20 7a 4a 6f 75 72 6e 61 6c 20 3d 20 7a      zJournal = z
a40a0 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3b 0a 20  MasterJournal;. 
a40b0 20 20 20 77 68 69 6c 65 28 20 28 7a 4a 6f 75 72     while( (zJour
a40c0 6e 61 6c 2d 7a 4d 61 73 74 65 72 4a 6f 75 72 6e  nal-zMasterJourn
a40d0 61 6c 29 3c 6e 4d 61 73 74 65 72 4a 6f 75 72 6e  al)<nMasterJourn
a40e0 61 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  al ){.      if( 
a40f0 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 45 78 69  sqlite3OsFileExi
a4100 73 74 73 28 7a 4a 6f 75 72 6e 61 6c 29 20 29 7b  sts(zJournal) ){
a4110 0a 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20  .        /* One 
a4120 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 73 20  of the journals 
a4130 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 74 68  pointed to by th
a4140 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
a4150 20 65 78 69 73 74 73 2e 0a 20 20 20 20 20 20 20   exists..       
a4160 20 2a 2a 20 4f 70 65 6e 20 69 74 20 61 6e 64 20   ** Open it and 
a4170 63 68 65 63 6b 20 69 66 20 69 74 20 70 6f 69 6e  check if it poin
a4180 74 73 20 61 74 20 74 68 65 20 6d 61 73 74 65 72  ts at the master
a4190 20 6a 6f 75 72 6e 61 6c 2e 20 49 66 0a 20 20 20   journal. If.   
a41a0 20 20 20 20 20 2a 2a 20 73 6f 2c 20 72 65 74 75       ** so, retu
a41b0 72 6e 20 77 69 74 68 6f 75 74 20 64 65 6c 65 74  rn without delet
a41c0 69 6e 67 20 74 68 65 20 6d 61 73 74 65 72 20 6a  ing the master j
a41d0 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20  ournal file..   
a41e0 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
a41f0 4f 73 46 69 6c 65 20 2a 6a 6f 75 72 6e 61 6c 20  OsFile *journal 
a4200 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  = 0;.        int
a4210 20 63 3b 0a 0a 20 20 20 20 20 20 20 20 72 63 20   c;..        rc 
a4220 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 52  = sqlite3OsOpenR
a4230 65 61 64 4f 6e 6c 79 28 7a 4a 6f 75 72 6e 61 6c  eadOnly(zJournal
a4240 2c 20 26 6a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20  , &journal);.   
a4250 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 21       assert( rc!
a4260 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 6a 6f  =SQLITE_OK || jo
a4270 75 72 6e 61 6c 20 29 3b 0a 20 20 20 20 20 20 20  urnal );.       
a4280 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
a4290 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
a42a0 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f  goto delmaster_o
a42b0 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20  ut;.        }.. 
a42c0 20 20 20 20 20 20 20 72 63 20 3d 20 72 65 61 64         rc = read
a42d0 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 6a 6f  MasterJournal(jo
a42e0 75 72 6e 61 6c 2c 20 26 7a 4d 61 73 74 65 72 50  urnal, &zMasterP
a42f0 74 72 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  tr);.        sql
a4300 69 74 65 33 4f 73 43 6c 6f 73 65 28 26 6a 6f 75  ite3OsClose(&jou
a4310 72 6e 61 6c 29 3b 0a 20 20 20 20 20 20 20 20 69  rnal);.        i
a4320 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
a4330 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f   ){.          go
a4340 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74  to delmaster_out
a4350 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20  ;.        }..   
a4360 20 20 20 20 20 63 20 3d 20 7a 4d 61 73 74 65 72       c = zMaster
a4370 50 74 72 21 3d 30 20 26 26 20 73 74 72 63 6d 70  Ptr!=0 && strcmp
a4380 28 7a 4d 61 73 74 65 72 50 74 72 2c 20 7a 4d 61  (zMasterPtr, zMa
a4390 73 74 65 72 29 3d 3d 30 3b 0a 20 20 20 20 20 20  ster)==0;.      
a43a0 20 20 73 71 6c 69 74 65 46 72 65 65 28 7a 4d 61    sqliteFree(zMa
a43b0 73 74 65 72 50 74 72 29 3b 0a 20 20 20 20 20 20  sterPtr);.      
a43c0 20 20 69 66 28 20 63 20 29 7b 0a 20 20 20 20 20    if( c ){.     
a43d0 20 20 20 20 20 2f 2a 20 57 65 20 68 61 76 65 20       /* We have 
a43e0 61 20 6d 61 74 63 68 2e 20 44 6f 20 6e 6f 74 20  a match. Do not 
a43f0 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65  delete the maste
a4400 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20  r journal file. 
a4410 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  */.          got
a4420 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b  o delmaster_out;
a4430 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
a4440 20 7d 0a 20 20 20 20 20 20 7a 4a 6f 75 72 6e 61   }.      zJourna
a4450 6c 20 2b 3d 20 28 73 74 72 6c 65 6e 28 7a 4a 6f  l += (strlen(zJo
a4460 75 72 6e 61 6c 29 2b 31 29 3b 0a 20 20 20 20 7d  urnal)+1);.    }
a4470 0a 20 20 7d 0a 20 20 0a 20 20 72 63 20 3d 20 73  .  }.  .  rc = s
a4480 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 7a  qlite3OsDelete(z
a4490 4d 61 73 74 65 72 29 3b 0a 0a 64 65 6c 6d 61 73  Master);..delmas
a44a0 74 65 72 5f 6f 75 74 3a 0a 20 20 69 66 28 20 7a  ter_out:.  if( z
a44b0 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 29 7b  MasterJournal ){
a44c0 0a 20 20 20 20 73 71 6c 69 74 65 46 72 65 65 28  .    sqliteFree(
a44d0 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 29 3b  zMasterJournal);
a44e0 0a 20 20 7d 20 20 0a 20 20 69 66 28 20 6d 61 73  .  }  .  if( mas
a44f0 74 65 72 5f 6f 70 65 6e 20 29 7b 0a 20 20 20 20  ter_open ){.    
a4500 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 26  sqlite3OsClose(&
a4510 6d 61 73 74 65 72 29 3b 0a 20 20 7d 0a 20 20 72  master);.  }.  r
a4520 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 73 74  eturn rc;.}...st
a4530 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 5f  atic void pager_
a4540 74 72 75 6e 63 61 74 65 5f 63 61 63 68 65 28 50  truncate_cache(P
a4550 61 67 65 72 20 2a 70 50 61 67 65 72 29 3b 0a 0a  ager *pPager);..
a4560 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 74  /*.** Truncate t
a4570 68 65 20 6d 61 69 6e 20 66 69 6c 65 20 6f 66 20  he main file of 
a4580 74 68 65 20 67 69 76 65 6e 20 70 61 67 65 72 20  the given pager 
a4590 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  to the number of
a45a0 20 70 61 67 65 73 0a 2a 2a 20 69 6e 64 69 63 61   pages.** indica
a45b0 74 65 64 2e 20 41 6c 73 6f 20 74 72 75 6e 63 61  ted. Also trunca
a45c0 74 65 20 74 68 65 20 63 61 63 68 65 64 20 72 65  te the cached re
a45d0 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20  presentation of 
a45e0 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61  the file..*/.sta
a45f0 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 74 72  tic int pager_tr
a4600 75 6e 63 61 74 65 28 50 61 67 65 72 20 2a 70 50  uncate(Pager *pP
a4610 61 67 65 72 2c 20 69 6e 74 20 6e 50 61 67 65 29  ager, int nPage)
a4620 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
a4630 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 50  ITE_OK;.  if( pP
a4640 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47  ager->state>=PAG
a4650 45 52 5f 45 58 43 4c 55 53 49 56 45 20 29 7b 0a  ER_EXCLUSIVE ){.
a4660 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
a4670 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 65  OsTruncate(pPage
a4680 72 2d 3e 66 64 2c 20 70 50 61 67 65 72 2d 3e 70  r->fd, pPager->p
a4690 61 67 65 53 69 7a 65 2a 28 69 36 34 29 6e 50 61  ageSize*(i64)nPa
a46a0 67 65 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72  ge);.  }.  if( r
a46b0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
a46c0 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69      pPager->dbSi
a46d0 7a 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20 20 20  ze = nPage;.    
a46e0 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65 5f 63  pager_truncate_c
a46f0 61 63 68 65 28 70 50 61 67 65 72 29 3b 0a 20 20  ache(pPager);.  
a4700 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
a4710 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
a4720 73 65 63 74 6f 72 53 69 7a 65 20 66 6f 72 20 74  sectorSize for t
a4730 68 65 20 67 69 76 65 6e 20 70 61 67 65 72 2e 0a  he given pager..
a4740 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 74 6f 72  **.** The sector
a4750 20 73 69 7a 65 20 69 73 20 74 68 65 20 6c 61 72   size is the lar
a4760 67 65 72 20 6f 66 20 74 68 65 20 73 65 63 74 6f  ger of the secto
a4770 72 20 73 69 7a 65 20 72 65 70 6f 72 74 65 64 0a  r size reported.
a4780 2a 2a 20 62 79 20 73 71 6c 69 74 65 33 4f 73 53  ** by sqlite3OsS
a4790 65 63 74 6f 72 53 69 7a 65 28 29 20 61 6e 64 20  ectorSize() and 
a47a0 74 68 65 20 70 61 67 65 53 69 7a 65 2e 0a 2a 2f  the pageSize..*/
a47b0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 74  .static void set
a47c0 53 65 63 74 6f 72 53 69 7a 65 28 50 61 67 65 72  SectorSize(Pager
a47d0 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 70 50 61   *pPager){.  pPa
a47e0 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20  ger->sectorSize 
a47f0 3d 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f  = sqlite3OsSecto
a4800 72 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 66 64  rSize(pPager->fd
a4810 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  );.  if( pPager-
a4820 3e 73 65 63 74 6f 72 53 69 7a 65 3c 70 50 61 67  >sectorSize<pPag
a4830 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 29 7b 0a  er->pageSize ){.
a4840 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 74      pPager->sect
a4850 6f 72 53 69 7a 65 20 3d 20 70 50 61 67 65 72 2d  orSize = pPager-
a4860 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 7d 0a 7d  >pageSize;.  }.}
a4870 0a 0a 2f 2a 0a 2a 2a 20 50 6c 61 79 62 61 63 6b  ../*.** Playback
a4880 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 6e 64   the journal and
a4890 20 74 68 75 73 20 72 65 73 74 6f 72 65 20 74 68   thus restore th
a48a0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
a48b0 74 6f 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 20  to.** the state 
a48c0 69 74 20 77 61 73 20 69 6e 20 62 65 66 6f 72 65  it was in before
a48d0 20 77 65 20 73 74 61 72 74 65 64 20 6d 61 6b 69   we started maki
a48e0 6e 67 20 63 68 61 6e 67 65 73 2e 20 20 0a 2a 2a  ng changes.  .**
a48f0 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20  .** The journal 
a4900 66 69 6c 65 20 66 6f 72 6d 61 74 20 69 73 20 61  file format is a
a4910 73 20 66 6f 6c 6c 6f 77 73 3a 20 0a 2a 2a 0a 2a  s follows: .**.*
a4920 2a 20 20 28 31 29 20 20 38 20 62 79 74 65 20 70  *  (1)  8 byte p
a4930 72 65 66 69 78 2e 20 20 41 20 63 6f 70 79 20 6f  refix.  A copy o
a4940 66 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 5b  f aJournalMagic[
a4950 5d 2e 0a 2a 2a 20 20 28 32 29 20 20 34 20 62 79  ]..**  (2)  4 by
a4960 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e  te big-endian in
a4970 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74  teger which is t
a4980 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 76 61 6c  he number of val
a4990 69 64 20 70 61 67 65 20 72 65 63 6f 72 64 73 0a  id page records.
a49a0 2a 2a 20 20 20 20 20 20 20 69 6e 20 74 68 65 20  **       in the 
a49b0 6a 6f 75 72 6e 61 6c 2e 20 20 49 66 20 74 68 69  journal.  If thi
a49c0 73 20 76 61 6c 75 65 20 69 73 20 30 78 66 66 66  s value is 0xfff
a49d0 66 66 66 66 66 2c 20 74 68 65 6e 20 63 6f 6d 70  fffff, then comp
a49e0 75 74 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 20  ute the.**      
a49f0 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20   number of page 
a4a00 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 74 68 65  records from the
a4a10 20 6a 6f 75 72 6e 61 6c 20 73 69 7a 65 2e 0a 2a   journal size..*
a4a20 2a 20 20 28 33 29 20 20 34 20 62 79 74 65 20 62  *  (3)  4 byte b
a4a30 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65  ig-endian intege
a4a40 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 69  r which is the i
a4a50 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 66 6f 72  nitial value for
a4a60 20 74 68 65 20 0a 2a 2a 20 20 20 20 20 20 20 73   the .**       s
a4a70 61 6e 69 74 79 20 63 68 65 63 6b 73 75 6d 2e 0a  anity checksum..
a4a80 2a 2a 20 20 28 34 29 20 20 34 20 62 79 74 65 20  **  (4)  4 byte 
a4a90 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73  integer which is
a4aa0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
a4ab0 61 67 65 73 20 74 6f 20 74 72 75 6e 63 61 74 65  ages to truncate
a4ac0 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 64 61   the.**       da
a4ad0 74 61 62 61 73 65 20 74 6f 20 64 75 72 69 6e 67  tabase to during
a4ae0 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 20   a rollback..** 
a4af0 20 28 35 29 20 20 34 20 62 79 74 65 20 69 6e 74   (5)  4 byte int
a4b00 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68  eger which is th
a4b10 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
a4b20 73 20 69 6e 20 74 68 65 20 6d 61 73 74 65 72 20  s in the master 
a4b30 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 20  journal.**      
a4b40 20 6e 61 6d 65 2e 20 20 54 68 65 20 76 61 6c 75   name.  The valu
a4b50 65 20 6d 61 79 20 62 65 20 7a 65 72 6f 20 28 69  e may be zero (i
a4b60 6e 64 69 63 61 74 65 20 74 68 61 74 20 74 68 65  ndicate that the
a4b70 72 65 20 69 73 20 6e 6f 20 6d 61 73 74 65 72 0a  re is no master.
a4b80 2a 2a 20 20 20 20 20 20 20 6a 6f 75 72 6e 61 6c  **       journal
a4b90 2e 29 0a 2a 2a 20 20 28 36 29 20 20 4e 20 62 79  .).**  (6)  N by
a4ba0 74 65 73 20 6f 66 20 74 68 65 20 6d 61 73 74 65  tes of the maste
a4bb0 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 2e 20  r journal name. 
a4bc0 20 54 68 65 20 6e 61 6d 65 20 77 69 6c 6c 20 62   The name will b
a4bd0 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64  e nul-terminated
a4be0 0a 2a 2a 20 20 20 20 20 20 20 61 6e 64 20 6d 69  .**       and mi
a4bf0 67 68 74 20 62 65 20 73 68 6f 72 74 65 72 20 74  ght be shorter t
a4c00 68 61 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65  han the value re
a4c10 61 64 20 66 72 6f 6d 20 28 35 29 2e 20 20 49 66  ad from (5).  If
a4c20 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 0a   the first byte.
a4c30 2a 2a 20 20 20 20 20 20 20 6f 66 20 74 68 65 20  **       of the 
a4c40 6e 61 6d 65 20 69 73 20 5c 30 30 30 20 74 68 65  name is \000 the
a4c50 6e 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61  n there is no ma
a4c60 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 20 20 54  ster journal.  T
a4c70 68 65 20 6d 61 73 74 65 72 0a 2a 2a 20 20 20 20  he master.**    
a4c80 20 20 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20     journal name 
a4c90 69 73 20 73 74 6f 72 65 64 20 69 6e 20 55 54 46  is stored in UTF
a4ca0 2d 38 2e 0a 2a 2a 20 20 28 37 29 20 20 5a 65 72  -8..**  (7)  Zer
a4cb0 6f 20 6f 72 20 6d 6f 72 65 20 70 61 67 65 73 20  o or more pages 
a4cc0 69 6e 73 74 61 6e 63 65 73 2c 20 65 61 63 68 20  instances, each 
a4cd0 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 20 20  as follows:.**  
a4ce0 20 20 20 20 20 20 2b 20 20 34 20 62 79 74 65 20        +  4 byte 
a4cf0 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20  page number..** 
a4d00 20 20 20 20 20 20 20 2b 20 20 70 50 61 67 65 72         +  pPager
a4d10 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73  ->pageSize bytes
a4d20 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 20 20 20 20   of data..**    
a4d30 20 20 20 20 2b 20 20 34 20 62 79 74 65 20 63 68      +  4 byte ch
a4d40 65 63 6b 73 75 6d 0a 2a 2a 0a 2a 2a 20 57 68 65  ecksum.**.** Whe
a4d50 6e 20 77 65 20 73 70 65 61 6b 20 6f 66 20 74 68  n we speak of th
a4d60 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  e journal header
a4d70 2c 20 77 65 20 6d 65 61 6e 20 74 68 65 20 66 69  , we mean the fi
a4d80 72 73 74 20 36 20 69 74 65 6d 73 20 61 62 6f 76  rst 6 items abov
a4d90 65 2e 0a 2a 2a 20 45 61 63 68 20 65 6e 74 72 79  e..** Each entry
a4da0 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   in the journal 
a4db0 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  is an instance o
a4dc0 66 20 74 68 65 20 37 74 68 20 69 74 65 6d 2e 0a  f the 7th item..
a4dd0 2a 2a 0a 2a 2a 20 43 61 6c 6c 20 74 68 65 20 76  **.** Call the v
a4de0 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 73 65  alue from the se
a4df0 63 6f 6e 64 20 62 75 6c 6c 65 74 20 22 6e 52 65  cond bullet "nRe
a4e00 63 22 2e 20 20 6e 52 65 63 20 69 73 20 74 68 65  c".  nRec is the
a4e10 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 76 61   number of.** va
a4e20 6c 69 64 20 70 61 67 65 20 65 6e 74 72 69 65 73  lid page entries
a4e30 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e   in the journal.
a4e40 20 20 49 6e 20 6d 6f 73 74 20 63 61 73 65 73 2c    In most cases,
a4e50 20 79 6f 75 20 63 61 6e 20 63 6f 6d 70 75 74 65   you can compute
a4e60 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 6f 66   the.** value of
a4e70 20 6e 52 65 63 20 66 72 6f 6d 20 74 68 65 20 73   nRec from the s
a4e80 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  ize of the journ
a4e90 61 6c 20 66 69 6c 65 2e 20 20 42 75 74 20 69 66  al file.  But if
a4ea0 20 61 20 70 6f 77 65 72 0a 2a 2a 20 66 61 69 6c   a power.** fail
a4eb0 75 72 65 20 6f 63 63 75 72 72 65 64 20 77 68 69  ure occurred whi
a4ec0 6c 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77  le the journal w
a4ed0 61 73 20 62 65 69 6e 67 20 77 72 69 74 74 65 6e  as being written
a4ee0 2c 20 69 74 20 63 6f 75 6c 64 20 62 65 20 74 68  , it could be th
a4ef0 65 0a 2a 2a 20 63 61 73 65 20 74 68 61 74 20 74  e.** case that t
a4f00 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6a  he size of the j
a4f10 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 64 20  ournal file had 
a4f20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 69 6e 63  already been inc
a4f30 72 65 61 73 65 64 20 62 75 74 0a 2a 2a 20 74 68  reased but.** th
a4f40 65 20 65 78 74 72 61 20 65 6e 74 72 69 65 73 20  e extra entries 
a4f50 68 61 64 20 6e 6f 74 20 79 65 74 20 6d 61 64 65  had not yet made
a4f60 20 69 74 20 73 61 66 65 6c 79 20 74 6f 20 64 69   it safely to di
a4f70 73 6b 2e 20 20 49 6e 20 73 75 63 68 20 61 20 63  sk.  In such a c
a4f80 61 73 65 2c 0a 2a 2a 20 74 68 65 20 76 61 6c 75  ase,.** the valu
a4f90 65 20 6f 66 20 6e 52 65 63 20 63 6f 6d 70 75 74  e of nRec comput
a4fa0 65 64 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65  ed from the file
a4fb0 20 73 69 7a 65 20 77 6f 75 6c 64 20 62 65 20 74   size would be t
a4fc0 6f 6f 20 6c 61 72 67 65 2e 20 20 46 6f 72 0a 2a  oo large.  For.*
a4fd0 2a 20 74 68 61 74 20 72 65 61 73 6f 6e 2c 20 77  * that reason, w
a4fe0 65 20 61 6c 77 61 79 73 20 75 73 65 20 74 68 65  e always use the
a4ff0 20 6e 52 65 63 20 76 61 6c 75 65 20 69 6e 20 74   nRec value in t
a5000 68 65 20 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a  he header..**.**
a5010 20 49 66 20 74 68 65 20 6e 52 65 63 20 76 61 6c   If the nRec val
a5020 75 65 20 69 73 20 30 78 66 66 66 66 66 66 66 66  ue is 0xffffffff
a5030 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 6e   it means that n
a5040 52 65 63 20 73 68 6f 75 6c 64 20 62 65 20 63 6f  Rec should be co
a5050 6d 70 75 74 65 64 0a 2a 2a 20 66 72 6f 6d 20 74  mputed.** from t
a5060 68 65 20 66 69 6c 65 20 73 69 7a 65 2e 20 20 54  he file size.  T
a5070 68 69 73 20 76 61 6c 75 65 20 69 73 20 75 73 65  his value is use
a5080 64 20 77 68 65 6e 20 74 68 65 20 75 73 65 72 20  d when the user 
a5090 73 65 6c 65 63 74 73 20 74 68 65 0a 2a 2a 20 6e  selects the.** n
a50a0 6f 2d 73 79 6e 63 20 6f 70 74 69 6f 6e 20 66 6f  o-sync option fo
a50b0 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20  r the journal.  
a50c0 41 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20  A power failure 
a50d0 63 6f 75 6c 64 20 6c 65 61 64 20 74 6f 20 63 6f  could lead to co
a50e0 72 72 75 70 74 69 6f 6e 0a 2a 2a 20 69 6e 20 74  rruption.** in t
a50f0 68 69 73 20 63 61 73 65 2e 20 20 42 75 74 20 66  his case.  But f
a5100 6f 72 20 74 68 69 6e 67 73 20 6c 69 6b 65 20 74  or things like t
a5110 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 28  emporary table (
a5120 77 68 69 63 68 20 77 69 6c 6c 20 62 65 0a 2a 2a  which will be.**
a5130 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68   deleted when th
a5140 65 20 70 6f 77 65 72 20 69 73 20 72 65 73 74 6f  e power is resto
a5150 72 65 64 29 20 77 65 20 64 6f 6e 27 74 20 63 61  red) we don't ca
a5160 72 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74  re.  .**.** If t
a5170 68 65 20 66 69 6c 65 20 6f 70 65 6e 65 64 20 61  he file opened a
a5180 73 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  s the journal fi
a5190 6c 65 20 69 73 20 6e 6f 74 20 61 20 77 65 6c 6c  le is not a well
a51a0 2d 66 6f 72 6d 65 64 0a 2a 2a 20 6a 6f 75 72 6e  -formed.** journ
a51b0 61 6c 20 66 69 6c 65 20 74 68 65 6e 20 61 6c 6c  al file then all
a51c0 20 70 61 67 65 73 20 75 70 20 74 6f 20 74 68 65   pages up to the
a51d0 20 66 69 72 73 74 20 63 6f 72 72 75 70 74 65 64   first corrupted
a51e0 20 70 61 67 65 20 61 72 65 20 72 6f 6c 6c 65 64   page are rolled
a51f0 0a 2a 2a 20 62 61 63 6b 20 28 6f 72 20 6e 6f 20  .** back (or no 
a5200 70 61 67 65 73 20 69 66 20 74 68 65 20 6a 6f 75  pages if the jou
a5210 72 6e 61 6c 20 68 65 61 64 65 72 20 69 73 20 63  rnal header is c
a5220 6f 72 72 75 70 74 65 64 29 2e 20 54 68 65 20 6a  orrupted). The j
a5230 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 69  ournal file.** i
a5240 73 20 74 68 65 6e 20 64 65 6c 65 74 65 64 20 61  s then deleted a
a5250 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74  nd SQLITE_OK ret
a5260 75 72 6e 65 64 2c 20 6a 75 73 74 20 61 73 20 69  urned, just as i
a5270 66 20 6e 6f 20 63 6f 72 72 75 70 74 69 6f 6e 20  f no corruption 
a5280 68 61 64 0a 2a 2a 20 62 65 65 6e 20 65 6e 63 6f  had.** been enco
a5290 75 6e 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  untered..**.** I
a52a0 66 20 61 6e 20 49 2f 4f 20 6f 72 20 6d 61 6c 6c  f an I/O or mall
a52b0 6f 63 28 29 20 65 72 72 6f 72 20 6f 63 63 75 72  oc() error occur
a52c0 73 2c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 66  s, the journal-f
a52d0 69 6c 65 20 69 73 20 6e 6f 74 20 64 65 6c 65 74  ile is not delet
a52e0 65 64 0a 2a 2a 20 61 6e 64 20 61 6e 20 65 72 72  ed.** and an err
a52f0 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
a5300 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ned..*/.static i
a5310 6e 74 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63  nt pager_playbac
a5320 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c  k(Pager *pPager,
a5330 20 69 6e 74 20 69 73 48 6f 74 29 7b 0a 20 20 69   int isHot){.  i
a5340 36 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20  64 szJ;         
a5350 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
a5360 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  of the journal f
a5370 69 6c 65 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  ile in bytes */.
a5380 20 20 75 33 32 20 6e 52 65 63 3b 20 20 20 20 20    u32 nRec;     
a5390 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
a53a0 6d 62 65 72 20 6f 66 20 52 65 63 6f 72 64 73 20  mber of Records 
a53b0 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 2a  in the journal *
a53c0 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  /.  int i;      
a53d0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a53e0 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  Loop counter */.
a53f0 20 20 50 67 6e 6f 20 6d 78 50 67 20 3d 20 30 3b    Pgno mxPg = 0;
a5400 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
a5410 7a 65 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e  ze of the origin
a5420 61 6c 20 66 69 6c 65 20 69 6e 20 70 61 67 65 73  al file in pages
a5430 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20   */.  int rc;   
a5440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
a5450 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20 6f 66  * Result code of
a5460 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f   a subroutine */
a5470 0a 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72  .  char *zMaster
a5480 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4e   = 0;       /* N
a5490 61 6d 65 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f  ame of master jo
a54a0 75 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 61 6e  urnal file if an
a54b0 79 20 2a 2f 0a 0a 20 20 2f 2a 20 46 69 67 75 72  y */..  /* Figur
a54c0 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 72  e out how many r
a54d0 65 63 6f 72 64 73 20 61 72 65 20 69 6e 20 74 68  ecords are in th
a54e0 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 41 62 6f 72  e journal.  Abor
a54f0 74 20 65 61 72 6c 79 20 69 66 0a 20 20 2a 2a 20  t early if.  ** 
a5500 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 65  the journal is e
a5510 6d 70 74 79 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  mpty..  */.  ass
a5520 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75  ert( pPager->jou
a5530 72 6e 61 6c 4f 70 65 6e 20 29 3b 0a 20 20 72 63  rnalOpen );.  rc
a5540 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65   = sqlite3OsFile
a5550 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a 66 64  Size(pPager->jfd
a5560 2c 20 26 73 7a 4a 29 3b 0a 20 20 69 66 28 20 72  , &szJ);.  if( r
a5570 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c!=SQLITE_OK || 
a5580 73 7a 4a 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f  szJ==0 ){.    go
a5590 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b  to end_playback;
a55a0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 64 20  .  }..  /* Read 
a55b0 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
a55c0 61 6c 20 6e 61 6d 65 20 66 72 6f 6d 20 74 68 65  al name from the
a55d0 20 6a 6f 75 72 6e 61 6c 2c 20 69 66 20 69 74 20   journal, if it 
a55e0 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 2a 2a  is present..  **
a55f0 20 49 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75   If a master jou
a5600 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69  rnal file name i
a5610 73 20 73 70 65 63 69 66 69 65 64 2c 20 62 75 74  s specified, but
a5620 20 74 68 65 20 66 69 6c 65 20 69 73 20 6e 6f 74   the file is not
a5630 0a 20 20 2a 2a 20 70 72 65 73 65 6e 74 20 6f 6e  .  ** present on
a5640 20 64 69 73 6b 2c 20 74 68 65 6e 20 74 68 65 20   disk, then the 
a5650 6a 6f 75 72 6e 61 6c 20 69 73 20 6e 6f 74 20 68  journal is not h
a5660 6f 74 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20  ot and does not 
a5670 6e 65 65 64 20 74 6f 20 62 65 0a 20 20 2a 2a 20  need to be.  ** 
a5680 70 6c 61 79 65 64 20 62 61 63 6b 2e 0a 20 20 2a  played back..  *
a5690 2f 0a 20 20 72 63 20 3d 20 72 65 61 64 4d 61 73  /.  rc = readMas
a56a0 74 65 72 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65  terJournal(pPage
a56b0 72 2d 3e 6a 66 64 2c 20 26 7a 4d 61 73 74 65 72  r->jfd, &zMaster
a56c0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 21  );.  assert( rc!
a56d0 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a  =SQLITE_DONE );.
a56e0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
a56f0 5f 4f 4b 20 7c 7c 20 28 7a 4d 61 73 74 65 72 20  _OK || (zMaster 
a5700 26 26 20 21 73 71 6c 69 74 65 33 4f 73 46 69 6c  && !sqlite3OsFil
a5710 65 45 78 69 73 74 73 28 7a 4d 61 73 74 65 72 29  eExists(zMaster)
a5720 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 46  ) ){.    sqliteF
a5730 72 65 65 28 7a 4d 61 73 74 65 72 29 3b 0a 20 20  ree(zMaster);.  
a5740 20 20 7a 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20    zMaster = 0;. 
a5750 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
a5760 45 5f 44 4f 4e 45 20 29 20 72 63 20 3d 20 53 51  E_DONE ) rc = SQ
a5770 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 67 6f 74  LITE_OK;.    got
a5780 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a  o end_playback;.
a5790 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 4f 73 53    }.  sqlite3OsS
a57a0 65 65 6b 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  eek(pPager->jfd,
a57b0 20 30 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6a   0);.  pPager->j
a57c0 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 0a  ournalOff = 0;..
a57d0 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20 74    /* This loop t
a57e0 65 72 6d 69 6e 61 74 65 73 20 65 69 74 68 65 72  erminates either
a57f0 20 77 68 65 6e 20 74 68 65 20 72 65 61 64 4a 6f   when the readJo
a5800 75 72 6e 61 6c 48 64 72 28 29 20 63 61 6c 6c 20  urnalHdr() call 
a5810 72 65 74 75 72 6e 73 0a 20 20 2a 2a 20 53 51 4c  returns.  ** SQL
a5820 49 54 45 5f 44 4f 4e 45 20 6f 72 20 61 6e 20 49  ITE_DONE or an I
a5830 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 20  O error occurs. 
a5840 2a 2f 0a 20 20 77 68 69 6c 65 28 20 31 20 29 7b  */.  while( 1 ){
a5850 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68  ..    /* Read th
a5860 65 20 6e 65 78 74 20 6a 6f 75 72 6e 61 6c 20 68  e next journal h
a5870 65 61 64 65 72 20 66 72 6f 6d 20 74 68 65 20 6a  eader from the j
a5880 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 49 66  ournal file.  If
a5890 20 74 68 65 72 65 20 61 72 65 0a 20 20 20 20 2a   there are.    *
a58a0 2a 20 6e 6f 74 20 65 6e 6f 75 67 68 20 62 79 74  * not enough byt
a58b0 65 73 20 6c 65 66 74 20 69 6e 20 74 68 65 20 6a  es left in the j
a58c0 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20  ournal file for 
a58d0 61 20 63 6f 6d 70 6c 65 74 65 20 68 65 61 64 65  a complete heade
a58e0 72 2c 20 6f 72 0a 20 20 20 20 2a 2a 20 69 74 20  r, or.    ** it 
a58f0 69 73 20 63 6f 72 72 75 70 74 65 64 2c 20 74 68  is corrupted, th
a5900 65 6e 20 61 20 70 72 6f 63 65 73 73 20 6d 75 73  en a process mus
a5910 74 20 6f 66 20 66 61 69 6c 65 64 20 77 68 69 6c  t of failed whil
a5920 65 20 77 72 69 74 69 6e 67 20 69 74 2e 0a 20 20  e writing it..  
a5930 20 20 2a 2a 20 54 68 69 73 20 69 6e 64 69 63 61    ** This indica
a5940 74 65 73 20 6e 6f 74 68 69 6e 67 20 6d 6f 72 65  tes nothing more
a5950 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 6f 6c   needs to be rol
a5960 6c 65 64 20 62 61 63 6b 2e 0a 20 20 20 20 2a 2f  led back..    */
a5970 0a 20 20 20 20 72 63 20 3d 20 72 65 61 64 4a 6f  .    rc = readJo
a5980 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72 2c  urnalHdr(pPager,
a5990 20 73 7a 4a 2c 20 26 6e 52 65 63 2c 20 26 6d 78   szJ, &nRec, &mx
a59a0 50 67 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  Pg);.    if( rc!
a59b0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 20 0a 20  =SQLITE_OK ){ . 
a59c0 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
a59d0 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20  ITE_DONE ){.    
a59e0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
a59f0 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  OK;.      }.    
a5a00 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62    goto end_playb
a5a10 61 63 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  ack;.    }..    
a5a20 2f 2a 20 49 66 20 6e 52 65 63 20 69 73 20 30 78  /* If nRec is 0x
a5a30 66 66 66 66 66 66 66 66 2c 20 74 68 65 6e 20 74  ffffffff, then t
a5a40 68 69 73 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20  his journal was 
a5a50 63 72 65 61 74 65 64 20 62 79 20 61 20 70 72 6f  created by a pro
a5a60 63 65 73 73 0a 20 20 20 20 2a 2a 20 77 6f 72 6b  cess.    ** work
a5a70 69 6e 67 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d  ing in no-sync m
a5a80 6f 64 65 2e 20 54 68 69 73 20 6d 65 61 6e 73 20  ode. This means 
a5a90 74 68 61 74 20 74 68 65 20 72 65 73 74 20 6f 66  that the rest of
a5aa0 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20   the journal.   
a5ab0 20 2a 2a 20 66 69 6c 65 20 63 6f 6e 73 69 73 74   ** file consist
a5ac0 73 20 6f 66 20 70 61 67 65 73 2c 20 74 68 65 72  s of pages, ther
a5ad0 65 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20 6a 6f  e are no more jo
a5ae0 75 72 6e 61 6c 20 68 65 61 64 65 72 73 2e 20 43  urnal headers. C
a5af0 6f 6d 70 75 74 65 0a 20 20 20 20 2a 2a 20 74 68  ompute.    ** th
a5b00 65 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 63 20  e value of nRec 
a5b10 62 61 73 65 64 20 6f 6e 20 74 68 69 73 20 61 73  based on this as
a5b20 73 75 6d 70 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f  sumption..    */
a5b30 0a 20 20 20 20 69 66 28 20 6e 52 65 63 3d 3d 30  .    if( nRec==0
a5b40 78 66 66 66 66 66 66 66 66 20 29 7b 0a 20 20 20  xffffffff ){.   
a5b50 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
a5b60 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 4a  r->journalOff==J
a5b70 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50  OURNAL_HDR_SZ(pP
a5b80 61 67 65 72 29 20 29 3b 0a 20 20 20 20 20 20 6e  ager) );.      n
a5b90 52 65 63 20 3d 20 28 73 7a 4a 20 2d 20 4a 4f 55  Rec = (szJ - JOU
a5ba0 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67  RNAL_HDR_SZ(pPag
a5bb0 65 72 29 29 2f 4a 4f 55 52 4e 41 4c 5f 50 47 5f  er))/JOURNAL_PG_
a5bc0 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  SZ(pPager);.    
a5bd0 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 6e 52 65  }..    /* If nRe
a5be0 63 20 69 73 20 30 20 61 6e 64 20 74 68 69 73 20  c is 0 and this 
a5bf0 72 6f 6c 6c 62 61 63 6b 20 69 73 20 6f 66 20 61  rollback is of a
a5c00 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 72 65   transaction cre
a5c10 61 74 65 64 20 62 79 20 74 68 69 73 0a 20 20 20  ated by this.   
a5c20 20 2a 2a 20 70 72 6f 63 65 73 73 2e 20 49 6e 20   ** process. In 
a5c30 74 68 69 73 20 63 61 73 65 20 74 68 65 20 72 65  this case the re
a5c40 73 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  st of the journa
a5c50 6c 20 66 69 6c 65 20 63 6f 6e 73 69 73 74 73 20  l file consists 
a5c60 6f 66 0a 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61  of.    ** journa
a5c70 6c 6c 65 64 20 63 6f 70 69 65 73 20 6f 66 20 70  lled copies of p
a5c80 61 67 65 73 20 74 68 61 74 20 6e 65 65 64 20 74  ages that need t
a5c90 6f 20 62 65 20 72 65 61 64 20 62 61 63 6b 20 69  o be read back i
a5ca0 6e 74 6f 20 74 68 65 20 63 61 63 68 65 2e 0a 20  nto the cache.. 
a5cb0 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 52     */.    if( nR
a5cc0 65 63 3d 3d 30 20 26 26 20 21 69 73 48 6f 74 20  ec==0 && !isHot 
a5cd0 29 7b 0a 20 20 20 20 20 20 6e 52 65 63 20 3d 20  ){.      nRec = 
a5ce0 28 73 7a 4a 20 2d 20 70 50 61 67 65 72 2d 3e 6a  (szJ - pPager->j
a5cf0 6f 75 72 6e 61 6c 4f 66 66 29 20 2f 20 4a 4f 55  ournalOff) / JOU
a5d00 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65  RNAL_PG_SZ(pPage
a5d10 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  r);.    }..    /
a5d20 2a 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65  * If this is the
a5d30 20 66 69 72 73 74 20 68 65 61 64 65 72 20 72 65   first header re
a5d40 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72  ad from the jour
a5d50 6e 61 6c 2c 20 74 72 75 6e 63 61 74 65 20 74 68  nal, truncate th
a5d60 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73  e.    ** databas
a5d70 65 20 66 69 6c 65 20 62 61 63 6b 20 74 6f 20 69  e file back to i
a5d80 74 27 73 20 6f 72 69 67 69 6e 61 6c 20 73 69 7a  t's original siz
a5d90 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  e..    */.    if
a5da0 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  ( pPager->journa
a5db0 6c 4f 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44  lOff==JOURNAL_HD
a5dc0 52 5f 53 5a 28 70 50 61 67 65 72 29 20 29 7b 0a  R_SZ(pPager) ){.
a5dd0 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72        rc = pager
a5de0 5f 74 72 75 6e 63 61 74 65 28 70 50 61 67 65 72  _truncate(pPager
a5df0 2c 20 6d 78 50 67 29 3b 0a 20 20 20 20 20 20 69  , mxPg);.      i
a5e00 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
a5e10 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f   ){.        goto
a5e20 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20   end_playback;. 
a5e30 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
a5e40 20 20 2f 2a 20 43 6f 70 79 20 6f 72 69 67 69 6e    /* Copy origin
a5e50 61 6c 20 70 61 67 65 73 20 6f 75 74 20 6f 66 20  al pages out of 
a5e60 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20  the journal and 
a5e70 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 64 61  back into the da
a5e80 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20  tabase file..   
a5e90 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   */.    for(i=0;
a5ea0 20 69 3c 6e 52 65 63 3b 20 69 2b 2b 29 7b 0a 20   i<nRec; i++){. 
a5eb0 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f       rc = pager_
a5ec0 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67  playback_one_pag
a5ed0 65 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 72  e(pPager, pPager
a5ee0 2d 3e 6a 66 64 2c 20 31 29 3b 0a 20 20 20 20 20  ->jfd, 1);.     
a5ef0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
a5f00 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  OK ){.        if
a5f10 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc==SQLITE_DON
a5f20 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  E ){.          r
a5f30 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
a5f40 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d           pPager-
a5f50 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 73 7a  >journalOff = sz
a5f60 4a 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  J;.          bre
a5f70 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  ak;.        }els
a5f80 65 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  e{.          got
a5f90 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a  o end_playback;.
a5fa0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a5fb0 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a  }.    }.  }.  /*
a5fc0 4e 4f 54 52 45 41 43 48 45 44 2a 2f 0a 20 20 61  NOTREACHED*/.  a
a5fd0 73 73 65 72 74 28 20 30 20 29 3b 0a 0a 65 6e 64  ssert( 0 );..end
a5fe0 5f 70 6c 61 79 62 61 63 6b 3a 0a 20 20 69 66 28  _playback:.  if(
a5ff0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
a6000 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72  {.    rc = pager
a6010 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e  _end_transaction
a6020 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20  (pPager);.  }.  
a6030 69 66 28 20 7a 4d 61 73 74 65 72 20 29 7b 0a 20  if( zMaster ){. 
a6040 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 77     /* If there w
a6050 61 73 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72  as a master jour
a6060 6e 61 6c 20 61 6e 64 20 74 68 69 73 20 72 6f 75  nal and this rou
a6070 74 69 6e 65 20 77 69 6c 6c 20 72 65 74 75 72 6e  tine will return
a6080 20 73 75 63 63 65 73 73 2c 0a 20 20 20 20 2a 2a   success,.    **
a6090 20 73 65 65 20 69 66 20 69 74 20 69 73 20 70 6f   see if it is po
a60a0 73 73 69 62 6c 65 20 74 6f 20 64 65 6c 65 74 65  ssible to delete
a60b0 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
a60c0 6e 61 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  nal..    */.    
a60d0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
a60e0 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  K ){.      rc = 
a60f0 70 61 67 65 72 5f 64 65 6c 6d 61 73 74 65 72 28  pager_delmaster(
a6100 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 7d 0a  zMaster);.    }.
a6110 20 20 20 20 73 71 6c 69 74 65 46 72 65 65 28 7a      sqliteFree(z
a6120 4d 61 73 74 65 72 29 3b 0a 20 20 7d 0a 0a 20 20  Master);.  }..  
a6130 2f 2a 20 54 68 65 20 50 61 67 65 72 2e 73 65 63  /* The Pager.sec
a6140 74 6f 72 53 69 7a 65 20 76 61 72 69 61 62 6c 65  torSize variable
a6150 20 6d 61 79 20 68 61 76 65 20 62 65 65 6e 20 75   may have been u
a6160 70 64 61 74 65 64 20 77 68 69 6c 65 20 72 6f 6c  pdated while rol
a6170 6c 69 6e 67 0a 20 20 2a 2a 20 62 61 63 6b 20 61  ling.  ** back a
a6180 20 6a 6f 75 72 6e 61 6c 20 63 72 65 61 74 65 64   journal created
a6190 20 62 79 20 61 20 70 72 6f 63 65 73 73 20 77 69   by a process wi
a61a0 74 68 20 61 20 64 69 66 66 65 72 65 6e 74 20 73  th a different s
a61b0 65 63 74 6f 72 20 73 69 7a 65 0a 20 20 2a 2a 20  ector size.  ** 
a61c0 76 61 6c 75 65 2e 20 52 65 73 65 74 20 69 74 20  value. Reset it 
a61d0 74 6f 20 74 68 65 20 63 6f 72 72 65 63 74 20 76  to the correct v
a61e0 61 6c 75 65 20 66 6f 72 20 74 68 69 73 20 70 72  alue for this pr
a61f0 6f 63 65 73 73 2e 0a 20 20 2a 2f 0a 20 20 73 65  ocess..  */.  se
a6200 74 53 65 63 74 6f 72 53 69 7a 65 28 70 50 61 67  tSectorSize(pPag
a6210 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  er);.  return rc
a6220 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6c 61 79 62  ;.}../*.** Playb
a6230 61 63 6b 20 74 68 65 20 73 74 61 74 65 6d 65 6e  ack the statemen
a6240 74 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a  t journal..**.**
a6250 20 54 68 69 73 20 69 73 20 73 69 6d 69 6c 61 72   This is similar
a6260 20 74 6f 20 70 6c 61 79 69 6e 67 20 62 61 63 6b   to playing back
a6270 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
a6280 20 6a 6f 75 72 6e 61 6c 20 62 75 74 20 77 69 74   journal but wit
a6290 68 0a 2a 2a 20 61 20 66 65 77 20 65 78 74 72 61  h.** a few extra
a62a0 20 74 77 69 73 74 73 2e 0a 2a 2a 0a 2a 2a 20 20   twists..**.**  
a62b0 20 20 28 31 29 20 20 54 68 65 20 6e 75 6d 62 65    (1)  The numbe
a62c0 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68  r of pages in th
a62d0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
a62e0 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 0a  at the start of.
a62f0 2a 2a 20 20 20 20 20 20 20 20 20 74 68 65 20 73  **         the s
a6300 74 61 74 65 6d 65 6e 74 20 69 73 20 73 74 6f 72  tatement is stor
a6310 65 64 20 69 6e 20 70 50 61 67 65 72 2d 3e 73 74  ed in pPager->st
a6320 6d 74 53 69 7a 65 2c 20 6e 6f 74 20 69 6e 20 74  mtSize, not in t
a6330 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 6a 6f  he.**         jo
a6340 75 72 6e 61 6c 20 66 69 6c 65 20 69 74 73 65 6c  urnal file itsel
a6350 66 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20  f..**.**    (2) 
a6360 20 49 6e 20 61 64 64 69 74 69 6f 6e 20 74 6f 20   In addition to 
a6370 70 6c 61 79 69 6e 67 20 62 61 63 6b 20 74 68 65  playing back the
a6380 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e   statement journ
a6390 61 6c 2c 20 61 6c 73 6f 0a 2a 2a 20 20 20 20 20  al, also.**     
a63a0 20 20 20 20 70 6c 61 79 62 61 63 6b 20 61 6c 6c      playback all
a63b0 20 70 61 67 65 73 20 6f 66 20 74 68 65 20 74 72   pages of the tr
a63c0 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61  ansaction journa
a63d0 6c 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 20  l beginning.**  
a63e0 20 20 20 20 20 20 20 61 74 20 6f 66 66 73 65 74         at offset
a63f0 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 4a 53 69   pPager->stmtJSi
a6400 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ze..*/.static in
a6410 74 20 70 61 67 65 72 5f 73 74 6d 74 5f 70 6c 61  t pager_stmt_pla
a6420 79 62 61 63 6b 28 50 61 67 65 72 20 2a 70 50 61  yback(Pager *pPa
a6430 67 65 72 29 7b 0a 20 20 69 36 34 20 73 7a 4a 3b  ger){.  i64 szJ;
a6440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6450 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20   /* Size of the 
a6460 66 75 6c 6c 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a  full journal */.
a6470 20 20 69 36 34 20 68 64 72 4f 66 66 3b 0a 20 20    i64 hdrOff;.  
a6480 69 6e 74 20 6e 52 65 63 3b 20 20 20 20 20 20 20  int nRec;       
a6490 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
a64a0 65 72 20 6f 66 20 52 65 63 6f 72 64 73 20 2a 2f  er of Records */
a64b0 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
a64c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
a64d0 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
a64e0 20 69 6e 74 20 72 63 3b 0a 0a 20 20 73 7a 4a 20   int rc;..  szJ 
a64f0 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  = pPager->journa
a6500 6c 4f 66 66 3b 0a 23 69 66 6e 64 65 66 20 4e 44  lOff;.#ifndef ND
a6510 45 42 55 47 20 0a 20 20 7b 0a 20 20 20 20 69 36  EBUG .  {.    i6
a6520 34 20 6f 73 5f 73 7a 4a 3b 0a 20 20 20 20 72 63  4 os_szJ;.    rc
a6530 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65   = sqlite3OsFile
a6540 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a 66 64  Size(pPager->jfd
a6550 2c 20 26 6f 73 5f 73 7a 4a 29 3b 0a 20 20 20 20  , &os_szJ);.    
a6560 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
a6570 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  K ) return rc;. 
a6580 20 20 20 61 73 73 65 72 74 28 20 73 7a 4a 3d 3d     assert( szJ==
a6590 6f 73 5f 73 7a 4a 20 29 3b 0a 20 20 7d 0a 23 65  os_szJ );.  }.#e
a65a0 6e 64 69 66 0a 0a 20 20 2f 2a 20 53 65 74 20 68  ndif..  /* Set h
a65b0 64 72 4f 66 66 20 74 6f 20 62 65 20 74 68 65 20  drOff to be the 
a65c0 6f 66 66 73 65 74 20 6a 75 73 74 20 61 66 74 65  offset just afte
a65d0 72 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  r the end of the
a65e0 20 6c 61 73 74 20 6a 6f 75 72 6e 61 6c 0a 20 20   last journal.  
a65f0 2a 2a 20 70 61 67 65 20 77 72 69 74 74 65 6e 20  ** page written 
a6600 62 65 66 6f 72 65 20 74 68 65 20 66 69 72 73 74  before the first
a6610 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20   journal-header 
a6620 66 6f 72 20 74 68 69 73 20 73 74 61 74 65 6d 65  for this stateme
a6630 6e 74 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74  nt.  ** transact
a6640 69 6f 6e 20 77 61 73 20 77 72 69 74 74 65 6e 2c  ion was written,
a6650 20 6f 72 20 74 68 65 20 65 6e 64 20 6f 66 20 74   or the end of t
a6660 68 65 20 66 69 6c 65 20 69 66 20 6e 6f 20 6a 6f  he file if no jo
a6670 75 72 6e 61 6c 0a 20 20 2a 2a 20 68 65 61 64 65  urnal.  ** heade
a6680 72 20 77 61 73 20 77 72 69 74 74 65 6e 2e 0a 20  r was written.. 
a6690 20 2a 2f 0a 20 20 68 64 72 4f 66 66 20 3d 20 70   */.  hdrOff = p
a66a0 50 61 67 65 72 2d 3e 73 74 6d 74 48 64 72 4f 66  Pager->stmtHdrOf
a66b0 66 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  f;.  assert( pPa
a66c0 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 7c 7c  ger->fullSync ||
a66d0 20 21 68 64 72 4f 66 66 20 29 3b 0a 20 20 69 66   !hdrOff );.  if
a66e0 28 20 21 68 64 72 4f 66 66 20 29 7b 0a 20 20 20  ( !hdrOff ){.   
a66f0 20 68 64 72 4f 66 66 20 3d 20 73 7a 4a 3b 0a 20   hdrOff = szJ;. 
a6700 20 7d 0a 20 20 0a 20 20 2f 2a 20 54 72 75 6e 63   }.  .  /* Trunc
a6710 61 74 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ate the database
a6720 20 62 61 63 6b 20 74 6f 20 69 74 73 20 6f 72 69   back to its ori
a6730 67 69 6e 61 6c 20 73 69 7a 65 2e 0a 20 20 2a 2f  ginal size..  */
a6740 0a 20 20 72 63 20 3d 20 70 61 67 65 72 5f 74 72  .  rc = pager_tr
a6750 75 6e 63 61 74 65 28 70 50 61 67 65 72 2c 20 70  uncate(pPager, p
a6760 50 61 67 65 72 2d 3e 73 74 6d 74 53 69 7a 65 29  Pager->stmtSize)
a6770 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
a6780 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52  er->state>=PAGER
a6790 5f 53 48 41 52 45 44 20 29 3b 0a 0a 20 20 2f 2a  _SHARED );..  /*
a67a0 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20   Figure out how 
a67b0 6d 61 6e 79 20 72 65 63 6f 72 64 73 20 61 72 65  many records are
a67c0 20 69 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e   in the statemen
a67d0 74 20 6a 6f 75 72 6e 61 6c 2e 0a 20 20 2a 2f 0a  t journal..  */.
a67e0 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
a67f0 2d 3e 73 74 6d 74 49 6e 55 73 65 20 26 26 20 70  ->stmtInUse && p
a6800 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70  Pager->journalOp
a6810 65 6e 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 4f  en );.  sqlite3O
a6820 73 53 65 65 6b 28 70 50 61 67 65 72 2d 3e 73 74  sSeek(pPager->st
a6830 66 64 2c 20 30 29 3b 0a 20 20 6e 52 65 63 20 3d  fd, 0);.  nRec =
a6840 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 4e 52 65   pPager->stmtNRe
a6850 63 3b 0a 20 20 0a 20 20 2f 2a 20 43 6f 70 79 20  c;.  .  /* Copy 
a6860 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 73 20 6f  original pages o
a6870 75 74 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d  ut of the statem
a6880 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20  ent journal and 
a6890 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 0a 20 20  back into the.  
a68a0 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ** database file
a68b0 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65  .  Note that the
a68c0 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e   statement journ
a68d0 61 6c 20 6f 6d 69 74 73 20 63 68 65 63 6b 73 75  al omits checksu
a68e0 6d 73 20 66 72 6f 6d 0a 20 20 2a 2a 20 65 61 63  ms from.  ** eac
a68f0 68 20 72 65 63 6f 72 64 20 73 69 6e 63 65 20 70  h record since p
a6900 6f 77 65 72 2d 66 61 69 6c 75 72 65 20 72 65 63  ower-failure rec
a6910 6f 76 65 72 79 20 69 73 20 6e 6f 74 20 69 6d 70  overy is not imp
a6920 6f 72 74 61 6e 74 20 74 6f 20 73 74 61 74 65 6d  ortant to statem
a6930 65 6e 74 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c  ent.  ** journal
a6940 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d  s..  */.  for(i=
a6950 6e 52 65 63 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d  nRec-1; i>=0; i-
a6960 2d 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67  -){.    rc = pag
a6970 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f  er_playback_one_
a6980 70 61 67 65 28 70 50 61 67 65 72 2c 20 70 50 61  page(pPager, pPa
a6990 67 65 72 2d 3e 73 74 66 64 2c 20 30 29 3b 0a 20  ger->stfd, 0);. 
a69a0 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53     assert( rc!=S
a69b0 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20  QLITE_DONE );.  
a69c0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
a69d0 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 73  _OK ) goto end_s
a69e0 74 6d 74 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20  tmt_playback;.  
a69f0 7d 0a 0a 20 20 2f 2a 20 4e 6f 77 20 72 6f 6c 6c  }..  /* Now roll
a6a00 20 73 6f 6d 65 20 70 61 67 65 73 20 62 61 63 6b   some pages back
a6a10 20 66 72 6f 6d 20 74 68 65 20 74 72 61 6e 73 61   from the transa
a6a20 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 2e 20 50  ction journal. P
a6a30 61 67 65 72 2e 73 74 6d 74 4a 53 69 7a 65 0a 20  ager.stmtJSize. 
a6a40 20 2a 2a 20 77 61 73 20 74 68 65 20 73 69 7a 65   ** was the size
a6a50 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
a6a60 66 69 6c 65 20 77 68 65 6e 20 74 68 69 73 20 73  file when this s
a6a70 74 61 74 65 6d 65 6e 74 20 77 61 73 20 73 74 61  tatement was sta
a6a80 72 74 65 64 2c 20 73 6f 0a 20 20 2a 2a 20 65 76  rted, so.  ** ev
a6a90 65 72 79 74 68 69 6e 67 20 61 66 74 65 72 20 74  erything after t
a6aa0 68 61 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20  hat needs to be 
a6ab0 72 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 65 69 74  rolled back, eit
a6ac0 68 65 72 20 69 6e 74 6f 20 74 68 65 0a 20 20 2a  her into the.  *
a6ad0 2a 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 20  * database, the 
a6ae0 6d 65 6d 6f 72 79 20 63 61 63 68 65 2c 20 6f 72  memory cache, or
a6af0 20 62 6f 74 68 2e 0a 20 20 2a 2a 0a 20 20 2a 2a   both..  **.  **
a6b00 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20 7a 65   If it is not ze
a6b10 72 6f 2c 20 74 68 65 6e 20 50 61 67 65 72 2e 73  ro, then Pager.s
a6b20 74 6d 74 48 64 72 4f 66 66 20 69 73 20 74 68 65  tmtHdrOff is the
a6b30 20 6f 66 66 73 65 74 20 74 6f 20 74 68 65 20 73   offset to the s
a6b40 74 61 72 74 0a 20 20 2a 2a 20 6f 66 20 74 68 65  tart.  ** of the
a6b50 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68   first journal h
a6b60 65 61 64 65 72 20 77 72 69 74 74 65 6e 20 64 75  eader written du
a6b70 72 69 6e 67 20 74 68 69 73 20 73 74 61 74 65 6d  ring this statem
a6b80 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  ent transaction.
a6b90 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c  .  */.  rc = sql
a6ba0 69 74 65 33 4f 73 53 65 65 6b 28 70 50 61 67 65  ite3OsSeek(pPage
a6bb0 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e  r->jfd, pPager->
a6bc0 73 74 6d 74 4a 53 69 7a 65 29 3b 0a 20 20 69 66  stmtJSize);.  if
a6bd0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
a6be0 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f  ){.    goto end_
a6bf0 73 74 6d 74 5f 70 6c 61 79 62 61 63 6b 3b 0a 20  stmt_playback;. 
a6c00 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75   }.  pPager->jou
a6c10 72 6e 61 6c 4f 66 66 20 3d 20 70 50 61 67 65 72  rnalOff = pPager
a6c20 2d 3e 73 74 6d 74 4a 53 69 7a 65 3b 0a 20 20 70  ->stmtJSize;.  p
a6c30 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74  Pager->cksumInit
a6c40 20 3d 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 43   = pPager->stmtC
a6c50 6b 73 75 6d 3b 0a 20 20 77 68 69 6c 65 28 20 70  ksum;.  while( p
a6c60 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
a6c70 66 20 3c 20 68 64 72 4f 66 66 20 29 7b 0a 20 20  f < hdrOff ){.  
a6c80 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61    rc = pager_pla
a6c90 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70  yback_one_page(p
a6ca0 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a  Pager, pPager->j
a6cb0 66 64 2c 20 31 29 3b 0a 20 20 20 20 61 73 73 65  fd, 1);.    asse
a6cc0 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44  rt( rc!=SQLITE_D
a6cd0 4f 4e 45 20 29 3b 0a 20 20 20 20 69 66 28 20 72  ONE );.    if( r
a6ce0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
a6cf0 6f 74 6f 20 65 6e 64 5f 73 74 6d 74 5f 70 6c 61  oto end_stmt_pla
a6d00 79 62 61 63 6b 3b 0a 20 20 7d 0a 0a 20 20 77 68  yback;.  }..  wh
a6d10 69 6c 65 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75  ile( pPager->jou
a6d20 72 6e 61 6c 4f 66 66 20 3c 20 73 7a 4a 20 29 7b  rnalOff < szJ ){
a6d30 0a 20 20 20 20 75 33 32 20 6e 4a 52 65 63 3b 20  .    u32 nJRec; 
a6d40 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
a6d50 72 20 6f 66 20 4a 6f 75 72 6e 61 6c 20 52 65 63  r of Journal Rec
a6d60 6f 72 64 73 20 2a 2f 0a 20 20 20 20 75 33 32 20  ords */.    u32 
a6d70 64 75 6d 6d 79 3b 0a 20 20 20 20 72 63 20 3d 20  dummy;.    rc = 
a6d80 72 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72 28 70  readJournalHdr(p
a6d90 50 61 67 65 72 2c 20 73 7a 4a 2c 20 26 6e 4a 52  Pager, szJ, &nJR
a6da0 65 63 2c 20 26 64 75 6d 6d 79 29 3b 0a 20 20 20  ec, &dummy);.   
a6db0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
a6dc0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  OK ){.      asse
a6dd0 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44  rt( rc!=SQLITE_D
a6de0 4f 4e 45 20 29 3b 0a 20 20 20 20 20 20 67 6f 74  ONE );.      got
a6df0 6f 20 65 6e 64 5f 73 74 6d 74 5f 70 6c 61 79 62  o end_stmt_playb
a6e00 61 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ack;.    }.    i
a6e10 66 28 20 6e 4a 52 65 63 3d 3d 30 20 29 7b 0a 20  f( nJRec==0 ){. 
a6e20 20 20 20 20 20 6e 4a 52 65 63 20 3d 20 28 73 7a       nJRec = (sz
a6e30 4a 20 2d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  J - pPager->jour
a6e40 6e 61 6c 4f 66 66 29 20 2f 20 28 70 50 61 67 65  nalOff) / (pPage
a6e50 72 2d 3e 70 61 67 65 53 69 7a 65 2b 38 29 3b 0a  r->pageSize+8);.
a6e60 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d      }.    for(i=
a6e70 6e 4a 52 65 63 2d 31 3b 20 69 3e 3d 30 20 26 26  nJRec-1; i>=0 &&
a6e80 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
a6e90 4f 66 66 20 3c 20 73 7a 4a 3b 20 69 2d 2d 29 7b  Off < szJ; i--){
a6ea0 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65  .      rc = page
a6eb0 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70  r_playback_one_p
a6ec0 61 67 65 28 70 50 61 67 65 72 2c 20 70 50 61 67  age(pPager, pPag
a6ed0 65 72 2d 3e 6a 66 64 2c 20 31 29 3b 0a 20 20 20  er->jfd, 1);.   
a6ee0 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53     assert( rc!=S
a6ef0 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20  QLITE_DONE );.  
a6f00 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
a6f10 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64  TE_OK ) goto end
a6f20 5f 73 74 6d 74 5f 70 6c 61 79 62 61 63 6b 3b 0a  _stmt_playback;.
a6f30 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 50 61      }.  }..  pPa
a6f40 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20  ger->journalOff 
a6f50 3d 20 73 7a 4a 3b 0a 20 20 0a 65 6e 64 5f 73 74  = szJ;.  .end_st
a6f60 6d 74 5f 70 6c 61 79 62 61 63 6b 3a 0a 20 20 69  mt_playback:.  i
a6f70 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
a6f80 29 20 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e  ) {.    pPager->
a6f90 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 73 7a 4a  journalOff = szJ
a6fa0 3b 0a 20 20 20 20 2f 2a 20 70 61 67 65 72 5f 72  ;.    /* pager_r
a6fb0 65 6c 6f 61 64 5f 63 61 63 68 65 28 70 50 61 67  eload_cache(pPag
a6fc0 65 72 29 3b 20 2a 2f 0a 20 20 7d 0a 20 20 72 65  er); */.  }.  re
a6fd0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
a6fe0 2a 20 43 68 61 6e 67 65 20 74 68 65 20 6d 61 78  * Change the max
a6ff0 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 69  imum number of i
a7000 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 20 74  n-memory pages t
a7010 68 61 74 20 61 72 65 20 61 6c 6c 6f 77 65 64 2e  hat are allowed.
a7020 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
a7030 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
a7040 61 67 65 72 53 65 74 43 61 63 68 65 73 69 7a 65  agerSetCachesize
a7050 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
a7060 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 69  int mxPage){.  i
a7070 66 28 20 6d 78 50 61 67 65 3e 31 30 20 29 7b 0a  f( mxPage>10 ){.
a7080 20 20 20 20 70 50 61 67 65 72 2d 3e 6d 78 50 61      pPager->mxPa
a7090 67 65 20 3d 20 6d 78 50 61 67 65 3b 0a 20 20 7d  ge = mxPage;.  }
a70a0 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 67 65 72  else{.    pPager
a70b0 2d 3e 6d 78 50 61 67 65 20 3d 20 31 30 3b 0a 20  ->mxPage = 10;. 
a70c0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 6a 75   }.}../*.** Adju
a70d0 73 74 20 74 68 65 20 72 6f 62 75 73 74 6e 65 73  st the robustnes
a70e0 73 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  s of the databas
a70f0 65 20 74 6f 20 64 61 6d 61 67 65 20 64 75 65 20  e to damage due 
a7100 74 6f 20 4f 53 20 63 72 61 73 68 65 73 0a 2a 2a  to OS crashes.**
a7110 20 6f 72 20 70 6f 77 65 72 20 66 61 69 6c 75 72   or power failur
a7120 65 73 20 62 79 20 63 68 61 6e 67 69 6e 67 20 74  es by changing t
a7130 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 79 6e  he number of syn
a7140 63 73 28 29 73 20 77 68 65 6e 20 77 72 69 74 69  cs()s when writi
a7150 6e 67 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61  ng.** the rollba
a7160 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65  ck journal.  The
a7170 72 65 20 61 72 65 20 74 68 72 65 65 20 6c 65 76  re are three lev
a7180 65 6c 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4f 46  els:.**.**    OF
a7190 46 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f  F       sqlite3O
a71a0 73 53 79 6e 63 28 29 20 69 73 20 6e 65 76 65 72  sSync() is never
a71b0 20 63 61 6c 6c 65 64 2e 20 20 54 68 69 73 20 69   called.  This i
a71c0 73 20 74 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a  s the default.**
a71d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 6f                fo
a71e0 72 20 74 65 6d 70 6f 72 61 72 79 20 61 6e 64 20  r temporary and 
a71f0 74 72 61 6e 73 69 65 6e 74 20 66 69 6c 65 73 2e  transient files.
a7200 0a 2a 2a 0a 2a 2a 20 20 20 20 4e 4f 52 4d 41 4c  .**.**    NORMAL
a7210 20 20 20 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20      The journal 
a7220 69 73 20 73 79 6e 63 65 64 20 6f 6e 63 65 20 62  is synced once b
a7230 65 66 6f 72 65 20 77 72 69 74 65 73 20 62 65 67  efore writes beg
a7240 69 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 20 20 20  in on the.**    
a7250 20 20 20 20 20 20 20 20 20 20 64 61 74 61 62 61            databa
a7260 73 65 2e 20 20 54 68 69 73 20 69 73 20 6e 6f 72  se.  This is nor
a7270 6d 61 6c 6c 79 20 61 64 65 71 75 61 74 65 20 70  mally adequate p
a7280 72 6f 74 65 63 74 69 6f 6e 2c 20 62 75 74 0a 2a  rotection, but.*
a7290 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69  *              i
a72a0 74 20 69 73 20 74 68 65 6f 72 65 74 69 63 61 6c  t is theoretical
a72b0 6c 79 20 70 6f 73 73 69 62 6c 65 2c 20 74 68 6f  ly possible, tho
a72c0 75 67 68 20 76 65 72 79 20 75 6e 6c 69 6b 65 6c  ugh very unlikel
a72d0 79 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  y,.**           
a72e0 20 20 20 74 68 61 74 20 61 6e 20 69 6e 6f 70 65     that an inope
a72f0 72 74 75 6e 65 20 70 6f 77 65 72 20 66 61 69 6c  rtune power fail
a7300 75 72 65 20 63 6f 75 6c 64 20 6c 65 61 76 65 20  ure could leave 
a7310 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20  the journal.**  
a7320 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 20 61              in a
a7330 20 73 74 61 74 65 20 77 68 69 63 68 20 77 6f 75   state which wou
a7340 6c 64 20 63 61 75 73 65 20 64 61 6d 61 67 65 20  ld cause damage 
a7350 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 0a  to the database.
a7360 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
a7370 77 68 65 6e 20 69 74 20 69 73 20 72 6f 6c 6c 65  when it is rolle
a7380 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20  d back..**.**   
a7390 20 46 55 4c 4c 20 20 20 20 20 20 54 68 65 20 6a   FULL      The j
a73a0 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64  ournal is synced
a73b0 20 74 77 69 63 65 20 62 65 66 6f 72 65 20 77 72   twice before wr
a73c0 69 74 65 73 20 62 65 67 69 6e 20 6f 6e 20 74 68  ites begin on th
a73d0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  e.**            
a73e0 20 20 64 61 74 61 62 61 73 65 20 28 77 69 74 68    database (with
a73f0 20 73 6f 6d 65 20 61 64 64 69 74 69 6f 6e 61 6c   some additional
a7400 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2d 20 74   information - t
a7410 68 65 20 6e 52 65 63 20 66 69 65 6c 64 0a 2a 2a  he nRec field.**
a7420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 66                of
a7430 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61   the journal hea
a7440 64 65 72 20 2d 20 62 65 69 6e 67 20 77 72 69 74  der - being writ
a7450 74 65 6e 20 69 6e 20 62 65 74 77 65 65 6e 20 74  ten in between t
a7460 68 65 20 74 77 6f 0a 2a 2a 20 20 20 20 20 20 20  he two.**       
a7470 20 20 20 20 20 20 20 73 79 6e 63 73 29 2e 20 20         syncs).  
a7480 49 66 20 77 65 20 61 73 73 75 6d 65 20 74 68 61  If we assume tha
a7490 74 20 77 72 69 74 69 6e 67 20 61 0a 2a 2a 20 20  t writing a.**  
a74a0 20 20 20 20 20 20 20 20 20 20 20 20 73 69 6e 67              sing
a74b0 6c 65 20 64 69 73 6b 20 73 65 63 74 6f 72 20 69  le disk sector i
a74c0 73 20 61 74 6f 6d 69 63 2c 20 74 68 65 6e 20 74  s atomic, then t
a74d0 68 69 73 20 6d 6f 64 65 20 70 72 6f 76 69 64 65  his mode provide
a74e0 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  s.**            
a74f0 20 20 61 73 73 75 72 61 6e 63 65 20 74 68 61 74    assurance that
a7500 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77 69 6c   the journal wil
a7510 6c 20 6e 6f 74 20 62 65 20 63 6f 72 72 75 70 74  l not be corrupt
a7520 65 64 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20 20  ed to the.**    
a7530 20 20 20 20 20 20 20 20 20 20 70 6f 69 6e 74 20            point 
a7540 6f 66 20 63 61 75 73 69 6e 67 20 64 61 6d 61 67  of causing damag
a7550 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  e to the databas
a7560 65 20 64 75 72 69 6e 67 20 72 6f 6c 6c 62 61 63  e during rollbac
a7570 6b 2e 0a 2a 2a 0a 2a 2a 20 4e 75 6d 65 72 69 63  k..**.** Numeric
a7580 20 76 61 6c 75 65 73 20 61 73 73 6f 63 69 61 74   values associat
a7590 65 64 20 77 69 74 68 20 74 68 65 73 65 20 73 74  ed with these st
a75a0 61 74 65 73 20 61 72 65 20 4f 46 46 3d 3d 31 2c  ates are OFF==1,
a75b0 20 4e 4f 52 4d 41 4c 3d 32 2c 0a 2a 2a 20 61 6e   NORMAL=2,.** an
a75c0 64 20 46 55 4c 4c 3d 33 2e 0a 2a 2f 0a 23 69 66  d FULL=3..*/.#if
a75d0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
a75e0 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 53  _PAGER_PRAGMAS.S
a75f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
a7600 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 53  id sqlite3PagerS
a7610 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28 50 61  etSafetyLevel(Pa
a7620 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74  ger *pPager, int
a7630 20 6c 65 76 65 6c 2c 20 69 6e 74 20 66 75 6c 6c   level, int full
a7640 5f 66 73 79 6e 63 29 7b 0a 20 20 70 50 61 67 65  _fsync){.  pPage
a7650 72 2d 3e 6e 6f 53 79 6e 63 20 3d 20 20 6c 65 76  r->noSync =  lev
a7660 65 6c 3d 3d 31 20 7c 7c 20 70 50 61 67 65 72 2d  el==1 || pPager-
a7670 3e 74 65 6d 70 46 69 6c 65 3b 0a 20 20 70 50 61  >tempFile;.  pPa
a7680 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 3d 20  ger->fullSync = 
a7690 6c 65 76 65 6c 3d 3d 33 20 26 26 20 21 70 50 61  level==3 && !pPa
a76a0 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a 20  ger->tempFile;. 
a76b0 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 5f 66 73   pPager->full_fs
a76c0 79 6e 63 20 3d 20 66 75 6c 6c 5f 66 73 79 6e 63  ync = full_fsync
a76d0 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  ;.  if( pPager->
a76e0 6e 6f 53 79 6e 63 20 29 20 70 50 61 67 65 72 2d  noSync ) pPager-
a76f0 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 7d  >needSync = 0;.}
a7700 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
a7710 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f  he following glo
a7720 62 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73 20  bal variable is 
a7730 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 68 65 6e  incremented when
a7740 65 76 65 72 20 74 68 65 20 6c 69 62 72 61 72 79  ever the library
a7750 0a 2a 2a 20 61 74 74 65 6d 70 74 73 20 74 6f 20  .** attempts to 
a7760 6f 70 65 6e 20 61 20 74 65 6d 70 6f 72 61 72 79  open a temporary
a7770 20 66 69 6c 65 2e 20 20 54 68 69 73 20 69 6e 66   file.  This inf
a7780 6f 72 6d 61 74 69 6f 6e 20 69 73 20 75 73 65 64  ormation is used
a7790 20 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20   for.** testing 
a77a0 61 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c  and analysis onl
a77b0 79 2e 20 20 0a 2a 2f 0a 23 69 66 64 65 66 20 53  y.  .*/.#ifdef S
a77c0 51 4c 49 54 45 5f 54 45 53 54 0a 69 6e 74 20 73  QLITE_TEST.int s
a77d0 71 6c 69 74 65 33 5f 6f 70 65 6e 74 65 6d 70 5f  qlite3_opentemp_
a77e0 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69  count = 0;.#endi
a77f0 66 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20  f../*.** Open a 
a7800 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e 20  temporary file. 
a7810 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65  .**.** Write the
a7820 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
a7830 20 69 6e 74 6f 20 2a 66 64 2e 20 20 52 65 74 75   into *fd.  Retu
a7840 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20  rn SQLITE_OK on 
a7850 73 75 63 63 65 73 73 20 6f 72 20 73 6f 6d 65 0a  success or some.
a7860 2a 2a 20 6f 74 68 65 72 20 65 72 72 6f 72 20 63  ** other error c
a7870 6f 64 65 20 69 66 20 77 65 20 66 61 69 6c 2e 0a  ode if we fail..
a7880 2a 2a 0a 2a 2a 20 54 68 65 20 4f 53 20 77 69 6c  **.** The OS wil
a7890 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  l automatically 
a78a0 64 65 6c 65 74 65 20 74 68 65 20 74 65 6d 70 6f  delete the tempo
a78b0 72 61 72 79 20 66 69 6c 65 20 77 68 65 6e 20 69  rary file when i
a78c0 74 20 69 73 0a 2a 2a 20 63 6c 6f 73 65 64 2e 0a  t is.** closed..
a78d0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71  */.static int sq
a78e0 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 74 65  lite3PagerOpente
a78f0 6d 70 28 4f 73 46 69 6c 65 20 2a 2a 70 46 64 29  mp(OsFile **pFd)
a7900 7b 0a 20 20 69 6e 74 20 63 6e 74 20 3d 20 38 3b  {.  int cnt = 8;
a7910 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 63 68 61  .  int rc;.  cha
a7920 72 20 7a 46 69 6c 65 5b 53 51 4c 49 54 45 5f 54  r zFile[SQLITE_T
a7930 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 5d 3b 0a 0a  EMPNAME_SIZE];..
a7940 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
a7950 53 54 0a 20 20 73 71 6c 69 74 65 33 5f 6f 70 65  ST.  sqlite3_ope
a7960 6e 74 65 6d 70 5f 63 6f 75 6e 74 2b 2b 3b 20 20  ntemp_count++;  
a7970 2f 2a 20 55 73 65 64 20 66 6f 72 20 74 65 73 74  /* Used for test
a7980 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73 69 73  ing and analysis
a7990 20 6f 6e 6c 79 20 2a 2f 0a 23 65 6e 64 69 66 0a   only */.#endif.
a79a0 20 20 64 6f 7b 0a 20 20 20 20 63 6e 74 2d 2d 3b    do{.    cnt--;
a79b0 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 54 65  .    sqlite3OsTe
a79c0 6d 70 46 69 6c 65 4e 61 6d 65 28 7a 46 69 6c 65  mpFileName(zFile
a79d0 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  );.    rc = sqli
a79e0 74 65 33 4f 73 4f 70 65 6e 45 78 63 6c 75 73 69  te3OsOpenExclusi
a79f0 76 65 28 7a 46 69 6c 65 2c 20 70 46 64 2c 20 31  ve(zFile, pFd, 1
a7a00 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72  );.    assert( r
a7a10 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c!=SQLITE_OK || 
a7a20 2a 70 46 64 20 29 3b 0a 20 20 7d 77 68 69 6c 65  *pFd );.  }while
a7a30 28 20 63 6e 74 3e 30 20 26 26 20 72 63 21 3d 53  ( cnt>0 && rc!=S
a7a40 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d  QLITE_OK && rc!=
a7a50 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a  SQLITE_NOMEM );.
a7a60 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
a7a70 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e  /*.** Create a n
a7a80 65 77 20 70 61 67 65 20 63 61 63 68 65 20 61 6e  ew page cache an
a7a90 64 20 70 75 74 20 61 20 70 6f 69 6e 74 65 72 20  d put a pointer 
a7aa0 74 6f 20 74 68 65 20 70 61 67 65 20 63 61 63 68  to the page cach
a7ab0 65 20 69 6e 20 2a 70 70 50 61 67 65 72 2e 0a 2a  e in *ppPager..*
a7ac0 2a 20 54 68 65 20 66 69 6c 65 20 74 6f 20 62 65  * The file to be
a7ad0 20 63 61 63 68 65 64 20 6e 65 65 64 20 6e 6f 74   cached need not
a7ae0 20 65 78 69 73 74 2e 20 20 54 68 65 20 66 69 6c   exist.  The fil
a7af0 65 20 69 73 20 6e 6f 74 20 6c 6f 63 6b 65 64 20  e is not locked 
a7b00 75 6e 74 69 6c 0a 2a 2a 20 74 68 65 20 66 69 72  until.** the fir
a7b10 73 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  st call to sqlit
a7b20 65 33 50 61 67 65 72 47 65 74 28 29 20 61 6e 64  e3PagerGet() and
a7b30 20 69 73 20 6f 6e 6c 79 20 68 65 6c 64 20 6f 70   is only held op
a7b40 65 6e 20 75 6e 74 69 6c 20 74 68 65 0a 2a 2a 20  en until the.** 
a7b50 6c 61 73 74 20 70 61 67 65 20 69 73 20 72 65 6c  last page is rel
a7b60 65 61 73 65 64 20 75 73 69 6e 67 20 73 71 6c 69  eased using sqli
a7b70 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 29 2e  te3PagerUnref().
a7b80 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 46 69 6c 65 6e  .**.** If zFilen
a7b90 61 6d 65 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e  ame is NULL then
a7ba0 20 61 20 72 61 6e 64 6f 6d 6c 79 2d 6e 61 6d 65   a randomly-name
a7bb0 64 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  d temporary file
a7bc0 20 69 73 20 63 72 65 61 74 65 64 0a 2a 2a 20 61   is created.** a
a7bd0 6e 64 20 75 73 65 64 20 61 73 20 74 68 65 20 66  nd used as the f
a7be0 69 6c 65 20 74 6f 20 62 65 20 63 61 63 68 65 64  ile to be cached
a7bf0 2e 20 20 54 68 65 20 66 69 6c 65 20 77 69 6c 6c  .  The file will
a7c00 20 62 65 20 64 65 6c 65 74 65 64 0a 2a 2a 20 61   be deleted.** a
a7c10 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 77 68 65  utomatically whe
a7c20 6e 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 0a  n it is closed..
a7c30 2a 2a 0a 2a 2a 20 49 66 20 7a 46 69 6c 65 6e 61  **.** If zFilena
a7c40 6d 65 20 69 73 20 22 3a 6d 65 6d 6f 72 79 3a 22  me is ":memory:"
a7c50 20 74 68 65 6e 20 61 6c 6c 20 69 6e 66 6f 72 6d   then all inform
a7c60 61 74 69 6f 6e 20 69 73 20 68 65 6c 64 20 69 6e  ation is held in
a7c70 20 63 61 63 68 65 2e 0a 2a 2a 20 49 74 20 69 73   cache..** It is
a7c80 20 6e 65 76 65 72 20 77 72 69 74 74 65 6e 20 74   never written t
a7c90 6f 20 64 69 73 6b 2e 20 20 54 68 69 73 20 63 61  o disk.  This ca
a7ca0 6e 20 62 65 20 75 73 65 64 20 74 6f 20 69 6d 70  n be used to imp
a7cb0 6c 65 6d 65 6e 74 20 61 6e 0a 2a 2a 20 69 6e 2d  lement an.** in-
a7cc0 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2e  memory database.
a7cd0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
a7ce0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
a7cf0 67 65 72 4f 70 65 6e 28 0a 20 20 50 61 67 65 72  gerOpen(.  Pager
a7d00 20 2a 2a 70 70 50 61 67 65 72 2c 20 20 20 20 20   **ppPager,     
a7d10 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 68      /* Return th
a7d20 65 20 50 61 67 65 72 20 73 74 72 75 63 74 75 72  e Pager structur
a7d30 65 20 68 65 72 65 20 2a 2f 0a 20 20 63 6f 6e 73  e here */.  cons
a7d40 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d  t char *zFilenam
a7d50 65 2c 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20  e,   /* Name of 
a7d60 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
a7d70 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69  e to open */.  i
a7d80 6e 74 20 6e 45 78 74 72 61 2c 20 20 20 20 20 20  nt nExtra,      
a7d90 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61          /* Extra
a7da0 20 62 79 74 65 73 20 61 70 70 65 6e 64 20 74 6f   bytes append to
a7db0 20 65 61 63 68 20 69 6e 2d 6d 65 6d 6f 72 79 20   each in-memory 
a7dc0 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c  page */.  int fl
a7dd0 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20 20  ags             
a7de0 20 20 20 2f 2a 20 66 6c 61 67 73 20 63 6f 6e 74     /* flags cont
a7df0 72 6f 6c 6c 69 6e 67 20 74 68 69 73 20 66 69 6c  rolling this fil
a7e00 65 20 2a 2f 0a 29 7b 0a 20 20 50 61 67 65 72 20  e */.){.  Pager 
a7e10 2a 70 50 61 67 65 72 20 3d 20 30 3b 0a 20 20 63  *pPager = 0;.  c
a7e20 68 61 72 20 2a 7a 46 75 6c 6c 50 61 74 68 6e 61  har *zFullPathna
a7e30 6d 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 61  me = 0;.  int na
a7e40 6d 65 4c 65 6e 3b 20 20 2f 2a 20 43 6f 6d 70 69  meLen;  /* Compi
a7e50 6c 65 72 20 69 73 20 77 72 6f 6e 67 2e 20 54 68  ler is wrong. Th
a7e60 69 73 20 69 73 20 61 6c 77 61 79 73 20 69 6e 69  is is always ini
a7e70 74 69 61 6c 69 7a 65 64 20 62 65 66 6f 72 65 20  tialized before 
a7e80 75 73 65 20 2a 2f 0a 20 20 4f 73 46 69 6c 65 20  use */.  OsFile 
a7e90 2a 66 64 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72  *fd = 0;.  int r
a7ea0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
a7eb0 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 74 65   int i;.  int te
a7ec0 6d 70 46 69 6c 65 20 3d 20 30 3b 0a 20 20 69 6e  mpFile = 0;.  in
a7ed0 74 20 6d 65 6d 44 62 20 3d 20 30 3b 0a 20 20 69  t memDb = 0;.  i
a7ee0 6e 74 20 72 65 61 64 4f 6e 6c 79 20 3d 20 30 3b  nt readOnly = 0;
a7ef0 0a 20 20 69 6e 74 20 75 73 65 4a 6f 75 72 6e 61  .  int useJourna
a7f00 6c 20 3d 20 28 66 6c 61 67 73 20 26 20 50 41 47  l = (flags & PAG
a7f10 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 29  ER_OMIT_JOURNAL)
a7f20 3d 3d 30 3b 0a 20 20 69 6e 74 20 6e 6f 52 65 61  ==0;.  int noRea
a7f30 64 6c 6f 63 6b 20 3d 20 28 66 6c 61 67 73 20 26  dlock = (flags &
a7f40 20 50 41 47 45 52 5f 4e 4f 5f 52 45 41 44 4c 4f   PAGER_NO_READLO
a7f50 43 4b 29 21 3d 30 3b 0a 20 20 63 68 61 72 20 7a  CK)!=0;.  char z
a7f60 54 65 6d 70 5b 53 51 4c 49 54 45 5f 54 45 4d 50  Temp[SQLITE_TEMP
a7f70 4e 41 4d 45 5f 53 49 5a 45 5d 3b 0a 23 69 66 64  NAME_SIZE];.#ifd
a7f80 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
a7f90 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45  _MEMORY_MANAGEME
a7fa0 4e 54 0a 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63  NT.  /* A malloc
a7fb0 28 29 20 63 61 6e 6e 6f 74 20 66 61 69 6c 20 69  () cannot fail i
a7fc0 6e 20 73 71 6c 69 74 65 33 54 68 72 65 61 64 44  n sqlite3ThreadD
a7fd0 61 74 61 28 29 20 61 73 20 6f 6e 65 20 6f 72 20  ata() as one or 
a7fe0 6d 6f 72 65 20 63 61 6c 6c 73 20 74 6f 20 0a 20  more calls to . 
a7ff0 20 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 6d 75 73   ** malloc() mus
a8000 74 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62  t have already b
a8010 65 65 6e 20 6d 61 64 65 20 62 79 20 74 68 69 73  een made by this
a8020 20 74 68 72 65 61 64 20 62 65 66 6f 72 65 20 69   thread before i
a8030 74 20 67 65 74 73 0a 20 20 2a 2a 20 74 6f 20 74  t gets.  ** to t
a8040 68 69 73 20 70 6f 69 6e 74 2e 20 54 68 69 73 20  his point. This 
a8050 6d 65 61 6e 73 20 74 68 65 20 54 68 72 65 61 64  means the Thread
a8060 44 61 74 61 20 6d 75 73 74 20 68 61 76 65 20 62  Data must have b
a8070 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 6c  een allocated al
a8080 72 65 61 64 79 0a 20 20 2a 2a 20 73 6f 20 74 68  ready.  ** so th
a8090 61 74 20 54 68 72 65 61 64 44 61 74 61 2e 6e 41  at ThreadData.nA
a80a0 6c 6c 6f 63 20 63 61 6e 20 62 65 20 73 65 74 2e  lloc can be set.
a80b0 20 49 74 20 77 6f 75 6c 64 20 62 65 20 6e 69 63   It would be nic
a80c0 65 20 74 6f 20 61 73 73 65 72 74 0a 20 20 2a 2a  e to assert.  **
a80d0 20 74 68 61 74 20 54 68 72 65 61 64 44 61 74 61   that ThreadData
a80e0 2e 6e 41 6c 6c 6f 63 20 69 73 20 6e 6f 6e 2d 7a  .nAlloc is non-z
a80f0 65 72 6f 2c 20 62 75 74 20 61 6c 61 73 20 74 68  ero, but alas th
a8100 69 73 20 62 72 65 61 6b 73 20 74 65 73 74 20 63  is breaks test c
a8110 61 73 65 73 20 0a 20 20 2a 2a 20 77 72 69 74 74  ases .  ** writt
a8120 65 6e 20 74 6f 20 69 6e 76 6f 6b 65 20 74 68 65  en to invoke the
a8130 20 70 61 67 65 72 20 64 69 72 65 63 74 6c 79 2e   pager directly.
a8140 0a 20 20 2a 2f 0a 20 20 54 68 72 65 61 64 44 61  .  */.  ThreadDa
a8150 74 61 20 2a 70 54 73 64 20 3d 20 73 71 6c 69 74  ta *pTsd = sqlit
a8160 65 33 54 68 72 65 61 64 44 61 74 61 28 29 3b 0a  e3ThreadData();.
a8170 20 20 61 73 73 65 72 74 28 20 70 54 73 64 20 29    assert( pTsd )
a8180 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 57  ;.#endif..  /* W
a8190 65 20 75 73 65 64 20 74 6f 20 74 65 73 74 20 69  e used to test i
a81a0 66 20 6d 61 6c 6c 6f 63 28 29 20 68 61 64 20 61  f malloc() had a
a81b0 6c 72 65 61 64 79 20 66 61 69 6c 65 64 20 62 65  lready failed be
a81c0 66 6f 72 65 20 70 72 6f 63 65 65 64 69 6e 67 2e  fore proceeding.
a81d0 20 0a 20 20 2a 2a 20 42 75 74 20 74 68 65 20 77   .  ** But the w
a81e0 61 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ay this function
a81f0 20 69 73 20 75 73 65 64 20 69 6e 20 53 51 4c 69   is used in SQLi
a8200 74 65 20 6d 65 61 6e 73 20 74 68 61 74 20 63 61  te means that ca
a8210 6e 20 6e 65 76 65 72 0a 20 20 2a 2a 20 68 61 70  n never.  ** hap
a8220 70 65 6e 2e 20 46 75 72 74 68 65 72 6d 6f 72 65  pen. Furthermore
a8230 2c 20 69 66 20 74 68 65 20 6d 61 6c 6c 6f 63 2d  , if the malloc-
a8240 66 61 69 6c 65 64 20 66 6c 61 67 20 69 73 20 61  failed flag is a
a8250 6c 72 65 61 64 79 20 73 65 74 2c 20 0a 20 20 2a  lready set, .  *
a8260 2a 20 65 69 74 68 65 72 20 74 68 65 20 63 61 6c  * either the cal
a8270 6c 20 74 6f 20 73 71 6c 69 74 65 53 74 72 44 75  l to sqliteStrDu
a8280 70 28 29 20 6f 72 20 73 71 6c 69 74 65 4d 61 6c  p() or sqliteMal
a8290 6c 6f 63 28 29 20 62 65 6c 6f 77 20 77 69 6c 6c  loc() below will
a82a0 0a 20 20 2a 2a 20 66 61 69 6c 20 73 68 6f 72 74  .  ** fail short
a82b0 6c 79 20 61 6e 64 20 53 51 4c 49 54 45 5f 4e 4f  ly and SQLITE_NO
a82c0 4d 45 4d 20 72 65 74 75 72 6e 65 64 20 61 6e 79  MEM returned any
a82d0 77 61 79 2e 0a 20 20 2a 2f 0a 20 20 2a 70 70 50  way..  */.  *ppP
a82e0 61 67 65 72 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20  ager = 0;..  /* 
a82f0 4f 70 65 6e 20 74 68 65 20 70 61 67 65 72 20 66  Open the pager f
a8300 69 6c 65 20 61 6e 64 20 73 65 74 20 7a 46 75 6c  ile and set zFul
a8310 6c 50 61 74 68 6e 61 6d 65 20 74 6f 20 70 6f 69  lPathname to poi
a8320 6e 74 20 61 74 20 6d 61 6c 6c 6f 63 28 29 65 64  nt at malloc()ed
a8330 20 0a 20 20 2a 2a 20 6d 65 6d 6f 72 79 20 63 6f   .  ** memory co
a8340 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 63 6f 6d  ntaining the com
a8350 70 6c 65 74 65 20 66 69 6c 65 6e 61 6d 65 20 28  plete filename (
a8360 69 2e 65 2e 20 69 6e 63 6c 75 64 69 6e 67 20 74  i.e. including t
a8370 68 65 20 64 69 72 65 63 74 6f 72 79 29 2e 0a 20  he directory).. 
a8380 20 2a 2f 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e   */.  if( zFilen
a8390 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65  ame && zFilename
a83a0 5b 30 5d 20 29 7b 0a 23 69 66 6e 64 65 66 20 53  [0] ){.#ifndef S
a83b0 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52  QLITE_OMIT_MEMOR
a83c0 59 44 42 0a 20 20 20 20 69 66 28 20 73 74 72 63  YDB.    if( strc
a83d0 6d 70 28 7a 46 69 6c 65 6e 61 6d 65 2c 22 3a 6d  mp(zFilename,":m
a83e0 65 6d 6f 72 79 3a 22 29 3d 3d 30 20 29 7b 0a 20  emory:")==0 ){. 
a83f0 20 20 20 20 20 6d 65 6d 44 62 20 3d 20 31 3b 0a       memDb = 1;.
a8400 20 20 20 20 20 20 7a 46 75 6c 6c 50 61 74 68 6e        zFullPathn
a8410 61 6d 65 20 3d 20 73 71 6c 69 74 65 53 74 72 44  ame = sqliteStrD
a8420 75 70 28 22 22 29 3b 0a 20 20 20 20 7d 65 6c 73  up("");.    }els
a8430 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a 20  e.#endif.    {. 
a8440 20 20 20 20 20 7a 46 75 6c 6c 50 61 74 68 6e 61       zFullPathna
a8450 6d 65 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 75  me = sqlite3OsFu
a8460 6c 6c 50 61 74 68 6e 61 6d 65 28 7a 46 69 6c 65  llPathname(zFile
a8470 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 69 66 28  name);.      if(
a8480 20 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 29   zFullPathname )
a8490 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  {.        rc = s
a84a0 71 6c 69 74 65 33 4f 73 4f 70 65 6e 52 65 61 64  qlite3OsOpenRead
a84b0 57 72 69 74 65 28 7a 46 75 6c 6c 50 61 74 68 6e  Write(zFullPathn
a84c0 61 6d 65 2c 20 26 66 64 2c 20 26 72 65 61 64 4f  ame, &fd, &readO
a84d0 6e 6c 79 29 3b 0a 20 20 20 20 20 20 20 20 61 73  nly);.        as
a84e0 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45  sert( rc!=SQLITE
a84f0 5f 4f 4b 20 7c 7c 20 66 64 20 29 3b 0a 20 20 20  _OK || fd );.   
a8500 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c     }.    }.  }el
a8510 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  se{.    rc = sql
a8520 69 74 65 33 50 61 67 65 72 4f 70 65 6e 74 65 6d  ite3PagerOpentem
a8530 70 28 26 66 64 29 3b 0a 20 20 20 20 73 71 6c 69  p(&fd);.    sqli
a8540 74 65 33 4f 73 54 65 6d 70 46 69 6c 65 4e 61 6d  te3OsTempFileNam
a8550 65 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20 7a 46  e(zTemp);.    zF
a8560 69 6c 65 6e 61 6d 65 20 3d 20 7a 54 65 6d 70 3b  ilename = zTemp;
a8570 0a 20 20 20 20 7a 46 75 6c 6c 50 61 74 68 6e 61  .    zFullPathna
a8580 6d 65 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 75  me = sqlite3OsFu
a8590 6c 6c 50 61 74 68 6e 61 6d 65 28 7a 46 69 6c 65  llPathname(zFile
a85a0 6e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 72  name);.    if( r
a85b0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
a85c0 20 20 20 20 20 20 74 65 6d 70 46 69 6c 65 20 3d        tempFile =
a85d0 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20   1;.    }.  }.. 
a85e0 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68 65   /* Allocate the
a85f0 20 50 61 67 65 72 20 73 74 72 75 63 74 75 72 65   Pager structure
a8600 2e 20 41 73 20 70 61 72 74 20 6f 66 20 74 68 65  . As part of the
a8610 20 73 61 6d 65 20 61 6c 6c 6f 63 61 74 69 6f 6e   same allocation
a8620 2c 20 61 6c 6c 6f 63 61 74 65 0a 20 20 2a 2a 20  , allocate.  ** 
a8630 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 66 75  space for the fu
a8640 6c 6c 20 70 61 74 68 73 20 6f 66 20 74 68 65 20  ll paths of the 
a8650 66 69 6c 65 2c 20 64 69 72 65 63 74 6f 72 79 20  file, directory 
a8660 61 6e 64 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a  and journal .  *
a8670 2a 20 28 50 61 67 65 72 2e 7a 46 69 6c 65 6e 61  * (Pager.zFilena
a8680 6d 65 2c 20 50 61 67 65 72 2e 7a 44 69 72 65 63  me, Pager.zDirec
a8690 74 6f 72 79 20 61 6e 64 20 50 61 67 65 72 2e 7a  tory and Pager.z
a86a0 4a 6f 75 72 6e 61 6c 29 2e 0a 20 20 2a 2f 0a 20  Journal)..  */. 
a86b0 20 69 66 28 20 7a 46 75 6c 6c 50 61 74 68 6e 61   if( zFullPathna
a86c0 6d 65 20 29 7b 0a 20 20 20 20 6e 61 6d 65 4c 65  me ){.    nameLe
a86d0 6e 20 3d 20 73 74 72 6c 65 6e 28 7a 46 75 6c 6c  n = strlen(zFull
a86e0 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 70  Pathname);.    p
a86f0 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 4d 61  Pager = sqliteMa
a8700 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 50  lloc( sizeof(*pP
a8710 61 67 65 72 29 20 2b 20 6e 61 6d 65 4c 65 6e 2a  ager) + nameLen*
a8720 33 20 2b 20 33 30 20 29 3b 0a 20 20 20 20 69 66  3 + 30 );.    if
a8730 28 20 70 50 61 67 65 72 20 26 26 20 72 63 3d 3d  ( pPager && rc==
a8740 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
a8750 20 20 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53     pPager->pTmpS
a8760 70 61 63 65 20 3d 20 28 63 68 61 72 20 2a 29 73  pace = (char *)s
a8770 71 6c 69 74 65 4d 61 6c 6c 6f 63 52 61 77 28 53  qliteMallocRaw(S
a8780 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41  QLITE_DEFAULT_PA
a8790 47 45 5f 53 49 5a 45 29 3b 0a 20 20 20 20 7d 0a  GE_SIZE);.    }.
a87a0 20 20 7d 0a 0a 0a 20 20 2f 2a 20 49 66 20 61 6e    }...  /* If an
a87b0 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64 20 69   error occured i
a87c0 6e 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 20  n either of the 
a87d0 62 6c 6f 63 6b 73 20 61 62 6f 76 65 2c 20 66 72  blocks above, fr
a87e0 65 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 0a 20  ee the memory . 
a87f0 20 2a 2a 20 70 6f 69 6e 74 65 64 20 74 6f 20 62   ** pointed to b
a8800 79 20 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c  y zFullPathname,
a8810 20 66 72 65 65 20 74 68 65 20 50 61 67 65 72 20   free the Pager 
a8820 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 63 6c  structure and cl
a8830 6f 73 65 20 74 68 65 20 0a 20 20 2a 2a 20 66 69  ose the .  ** fi
a8840 6c 65 2e 20 53 69 6e 63 65 20 74 68 65 20 70 61  le. Since the pa
a8850 67 65 72 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 63  ger is not alloc
a8860 61 74 65 64 20 74 68 65 72 65 20 69 73 20 6e 6f  ated there is no
a8870 20 6e 65 65 64 20 74 6f 20 73 65 74 20 0a 20 20   need to set .  
a8880 2a 2a 20 61 6e 79 20 50 61 67 65 72 2e 65 72 72  ** any Pager.err
a8890 4d 61 73 6b 20 76 61 72 69 61 62 6c 65 73 2e 0a  Mask variables..
a88a0 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 50 61 67    */.  if( !pPag
a88b0 65 72 20 7c 7c 20 21 7a 46 75 6c 6c 50 61 74 68  er || !zFullPath
a88c0 6e 61 6d 65 20 7c 7c 20 21 70 50 61 67 65 72 2d  name || !pPager-
a88d0 3e 70 54 6d 70 53 70 61 63 65 20 7c 7c 20 72 63  >pTmpSpace || rc
a88e0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
a88f0 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73     sqlite3OsClos
a8900 65 28 26 66 64 29 3b 0a 20 20 20 20 73 71 6c 69  e(&fd);.    sqli
a8910 74 65 46 72 65 65 28 7a 46 75 6c 6c 50 61 74 68  teFree(zFullPath
a8920 6e 61 6d 65 29 3b 0a 20 20 20 20 73 71 6c 69 74  name);.    sqlit
a8930 65 46 72 65 65 28 70 50 61 67 65 72 29 3b 0a 20  eFree(pPager);. 
a8940 20 20 20 72 65 74 75 72 6e 20 28 28 72 63 3d 3d     return ((rc==
a8950 53 51 4c 49 54 45 5f 4f 4b 29 3f 53 51 4c 49 54  SQLITE_OK)?SQLIT
a8960 45 5f 4e 4f 4d 45 4d 3a 72 63 29 3b 0a 20 20 7d  E_NOMEM:rc);.  }
a8970 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45 33 28  ..  PAGERTRACE3(
a8980 22 4f 50 45 4e 20 25 64 20 25 73 5c 6e 22 2c 20  "OPEN %d %s\n", 
a8990 46 49 4c 45 48 41 4e 44 4c 45 49 44 28 66 64 29  FILEHANDLEID(fd)
a89a0 2c 20 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29  , zFullPathname)
a89b0 3b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 4f 50  ;.  IOTRACE(("OP
a89c0 45 4e 20 25 70 20 25 73 5c 6e 22 2c 20 70 50 61  EN %p %s\n", pPa
a89d0 67 65 72 2c 20 7a 46 75 6c 6c 50 61 74 68 6e 61  ger, zFullPathna
a89e0 6d 65 29 29 0a 20 20 70 50 61 67 65 72 2d 3e 7a  me)).  pPager->z
a89f0 46 69 6c 65 6e 61 6d 65 20 3d 20 28 63 68 61 72  Filename = (char
a8a00 2a 29 26 70 50 61 67 65 72 5b 31 5d 3b 0a 20 20  *)&pPager[1];.  
a8a10 70 50 61 67 65 72 2d 3e 7a 44 69 72 65 63 74 6f  pPager->zDirecto
a8a20 72 79 20 3d 20 26 70 50 61 67 65 72 2d 3e 7a 46  ry = &pPager->zF
a8a30 69 6c 65 6e 61 6d 65 5b 6e 61 6d 65 4c 65 6e 2b  ilename[nameLen+
a8a40 31 5d 3b 0a 20 20 70 50 61 67 65 72 2d 3e 7a 4a  1];.  pPager->zJ
a8a50 6f 75 72 6e 61 6c 20 3d 20 26 70 50 61 67 65 72  ournal = &pPager
a8a60 2d 3e 7a 44 69 72 65 63 74 6f 72 79 5b 6e 61 6d  ->zDirectory[nam
a8a70 65 4c 65 6e 2b 31 5d 3b 0a 20 20 6d 65 6d 63 70  eLen+1];.  memcp
a8a80 79 28 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e  y(pPager->zFilen
a8a90 61 6d 65 2c 20 7a 46 75 6c 6c 50 61 74 68 6e 61  ame, zFullPathna
a8aa0 6d 65 2c 20 6e 61 6d 65 4c 65 6e 2b 31 29 3b 0a  me, nameLen+1);.
a8ab0 20 20 6d 65 6d 63 70 79 28 70 50 61 67 65 72 2d    memcpy(pPager-
a8ac0 3e 7a 44 69 72 65 63 74 6f 72 79 2c 20 7a 46 75  >zDirectory, zFu
a8ad0 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 6e 61 6d 65  llPathname, name
a8ae0 4c 65 6e 2b 31 29 3b 0a 0a 20 20 66 6f 72 28 69  Len+1);..  for(i
a8af0 3d 6e 61 6d 65 4c 65 6e 3b 20 69 3e 30 20 26 26  =nameLen; i>0 &&
a8b00 20 70 50 61 67 65 72 2d 3e 7a 44 69 72 65 63 74   pPager->zDirect
a8b10 6f 72 79 5b 69 2d 31 5d 21 3d 27 2f 27 3b 20 69  ory[i-1]!='/'; i
a8b20 2d 2d 29 7b 7d 0a 20 20 69 66 28 20 69 3e 30 20  --){}.  if( i>0 
a8b30 29 20 70 50 61 67 65 72 2d 3e 7a 44 69 72 65 63  ) pPager->zDirec
a8b40 74 6f 72 79 5b 69 2d 31 5d 20 3d 20 30 3b 0a 20  tory[i-1] = 0;. 
a8b50 20 6d 65 6d 63 70 79 28 70 50 61 67 65 72 2d 3e   memcpy(pPager->
a8b60 7a 4a 6f 75 72 6e 61 6c 2c 20 7a 46 75 6c 6c 50  zJournal, zFullP
a8b70 61 74 68 6e 61 6d 65 2c 6e 61 6d 65 4c 65 6e 29  athname,nameLen)
a8b80 3b 0a 20 20 73 71 6c 69 74 65 46 72 65 65 28 7a  ;.  sqliteFree(z
a8b90 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20  FullPathname);. 
a8ba0 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d   memcpy(&pPager-
a8bb0 3e 7a 4a 6f 75 72 6e 61 6c 5b 6e 61 6d 65 4c 65  >zJournal[nameLe
a8bc0 6e 5d 2c 20 22 2d 6a 6f 75 72 6e 61 6c 22 2c 73  n], "-journal",s
a8bd0 69 7a 65 6f 66 28 22 2d 6a 6f 75 72 6e 61 6c 22  izeof("-journal"
a8be0 29 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 66 64  ));.  pPager->fd
a8bf0 20 3d 20 66 64 3b 0a 20 20 2f 2a 20 70 50 61 67   = fd;.  /* pPag
a8c00 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20  er->journalOpen 
a8c10 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72  = 0; */.  pPager
a8c20 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 3d 20 75  ->useJournal = u
a8c30 73 65 4a 6f 75 72 6e 61 6c 20 26 26 20 21 6d 65  seJournal && !me
a8c40 6d 44 62 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e  mDb;.  pPager->n
a8c50 6f 52 65 61 64 6c 6f 63 6b 20 3d 20 6e 6f 52 65  oReadlock = noRe
a8c60 61 64 6c 6f 63 6b 20 26 26 20 72 65 61 64 4f 6e  adlock && readOn
a8c70 6c 79 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d  ly;.  /* pPager-
a8c80 3e 73 74 6d 74 4f 70 65 6e 20 3d 20 30 3b 20 2a  >stmtOpen = 0; *
a8c90 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73  /.  /* pPager->s
a8ca0 74 6d 74 49 6e 55 73 65 20 3d 20 30 3b 20 2a 2f  tmtInUse = 0; */
a8cb0 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e 52  .  /* pPager->nR
a8cc0 65 66 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61  ef = 0; */.  pPa
a8cd0 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6d 65  ger->dbSize = me
a8ce0 6d 44 62 2d 31 3b 0a 20 20 70 50 61 67 65 72 2d  mDb-1;.  pPager-
a8cf0 3e 70 61 67 65 53 69 7a 65 20 3d 20 53 51 4c 49  >pageSize = SQLI
a8d00 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f  TE_DEFAULT_PAGE_
a8d10 53 49 5a 45 3b 0a 20 20 2f 2a 20 70 50 61 67 65  SIZE;.  /* pPage
a8d20 72 2d 3e 73 74 6d 74 53 69 7a 65 20 3d 20 30 3b  r->stmtSize = 0;
a8d30 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d   */.  /* pPager-
a8d40 3e 73 74 6d 74 4a 53 69 7a 65 20 3d 20 30 3b 20  >stmtJSize = 0; 
a8d50 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e  */.  /* pPager->
a8d60 6e 50 61 67 65 20 3d 20 30 3b 20 2a 2f 0a 20 20  nPage = 0; */.  
a8d70 70 50 61 67 65 72 2d 3e 6d 78 50 61 67 65 20 3d  pPager->mxPage =
a8d80 20 31 30 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e   100;.  pPager->
a8d90 6d 78 50 67 6e 6f 20 3d 20 53 51 4c 49 54 45 5f  mxPgno = SQLITE_
a8da0 4d 41 58 5f 50 41 47 45 5f 43 4f 55 4e 54 3b 0a  MAX_PAGE_COUNT;.
a8db0 20 20 61 73 73 65 72 74 28 20 50 41 47 45 52 5f    assert( PAGER_
a8dc0 55 4e 4c 4f 43 4b 3d 3d 30 20 29 3b 0a 20 20 2f  UNLOCK==0 );.  /
a8dd0 2a 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20  * pPager->state 
a8de0 3d 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 3b 20  = PAGER_UNLOCK; 
a8df0 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e  */.  /* pPager->
a8e00 65 72 72 4d 61 73 6b 20 3d 20 30 3b 20 2a 2f 0a  errMask = 0; */.
a8e10 20 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69    pPager->tempFi
a8e20 6c 65 20 3d 20 74 65 6d 70 46 69 6c 65 3b 0a 20  le = tempFile;. 
a8e30 20 61 73 73 65 72 74 28 20 74 65 6d 70 46 69 6c   assert( tempFil
a8e40 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  e==PAGER_LOCKING
a8e50 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 0a 20 20 20  MODE_NORMAL .   
a8e60 20 20 20 20 20 20 20 7c 7c 20 74 65 6d 70 46 69         || tempFi
a8e70 6c 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e  le==PAGER_LOCKIN
a8e80 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20  GMODE_EXCLUSIVE 
a8e90 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47  );.  assert( PAG
a8ea0 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45  ER_LOCKINGMODE_E
a8eb0 58 43 4c 55 53 49 56 45 3d 3d 31 20 29 3b 0a 20  XCLUSIVE==1 );. 
a8ec0 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69   pPager->exclusi
a8ed0 76 65 4d 6f 64 65 20 3d 20 74 65 6d 70 46 69 6c  veMode = tempFil
a8ee0 65 3b 20 0a 20 20 70 50 61 67 65 72 2d 3e 6d 65  e; .  pPager->me
a8ef0 6d 44 62 20 3d 20 6d 65 6d 44 62 3b 0a 20 20 70  mDb = memDb;.  p
a8f00 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 20  Pager->readOnly 
a8f10 3d 20 72 65 61 64 4f 6e 6c 79 3b 0a 20 20 2f 2a  = readOnly;.  /*
a8f20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e   pPager->needSyn
a8f30 63 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67  c = 0; */.  pPag
a8f40 65 72 2d 3e 6e 6f 53 79 6e 63 20 3d 20 70 50 61  er->noSync = pPa
a8f50 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 7c 7c  ger->tempFile ||
a8f60 20 21 75 73 65 4a 6f 75 72 6e 61 6c 3b 0a 20 20   !useJournal;.  
a8f70 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63  pPager->fullSync
a8f80 20 3d 20 28 70 50 61 67 65 72 2d 3e 6e 6f 53 79   = (pPager->noSy
a8f90 6e 63 3f 30 3a 31 29 3b 0a 20 20 2f 2a 20 70 50  nc?0:1);.  /* pP
a8fa0 61 67 65 72 2d 3e 70 46 69 72 73 74 20 3d 20 30  ager->pFirst = 0
a8fb0 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72  ; */.  /* pPager
a8fc0 2d 3e 70 46 69 72 73 74 53 79 6e 63 65 64 20 3d  ->pFirstSynced =
a8fd0 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67   0; */.  /* pPag
a8fe0 65 72 2d 3e 70 4c 61 73 74 20 3d 20 30 3b 20 2a  er->pLast = 0; *
a8ff0 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6e 45 78 74  /.  pPager->nExt
a9000 72 61 20 3d 20 46 4f 52 43 45 5f 41 4c 49 47 4e  ra = FORCE_ALIGN
a9010 4d 45 4e 54 28 6e 45 78 74 72 61 29 3b 0a 20 20  MENT(nExtra);.  
a9020 61 73 73 65 72 74 28 66 64 7c 7c 6d 65 6d 44 62  assert(fd||memDb
a9030 29 3b 0a 20 20 69 66 28 20 21 6d 65 6d 44 62 20  );.  if( !memDb 
a9040 29 7b 0a 20 20 20 20 73 65 74 53 65 63 74 6f 72  ){.    setSector
a9050 53 69 7a 65 28 70 50 61 67 65 72 29 3b 0a 20 20  Size(pPager);.  
a9060 7d 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70  }.  /* pPager->p
a9070 42 75 73 79 48 61 6e 64 6c 65 72 20 3d 20 30 3b  BusyHandler = 0;
a9080 20 2a 2f 0a 20 20 2f 2a 20 6d 65 6d 73 65 74 28   */.  /* memset(
a9090 70 50 61 67 65 72 2d 3e 61 48 61 73 68 2c 20 30  pPager->aHash, 0
a90a0 2c 20 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d  , sizeof(pPager-
a90b0 3e 61 48 61 73 68 29 29 3b 20 2a 2f 0a 20 20 2a  >aHash)); */.  *
a90c0 70 70 50 61 67 65 72 20 3d 20 70 50 61 67 65 72  ppPager = pPager
a90d0 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
a90e0 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41  ENABLE_MEMORY_MA
a90f0 4e 41 47 45 4d 45 4e 54 0a 20 20 70 50 61 67 65  NAGEMENT.  pPage
a9100 72 2d 3e 70 4e 65 78 74 20 3d 20 70 54 73 64 2d  r->pNext = pTsd-
a9110 3e 70 50 61 67 65 72 3b 0a 20 20 70 54 73 64 2d  >pPager;.  pTsd-
a9120 3e 70 50 61 67 65 72 20 3d 20 70 50 61 67 65 72  >pPager = pPager
a9130 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72  ;.#endif.  retur
a9140 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
a9150 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 62 75  /*.** Set the bu
a9160 73 79 20 68 61 6e 64 6c 65 72 20 66 75 6e 63 74  sy handler funct
a9170 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion..*/.SQLITE_P
a9180 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
a9190 74 65 33 50 61 67 65 72 53 65 74 42 75 73 79 68  te3PagerSetBusyh
a91a0 61 6e 64 6c 65 72 28 50 61 67 65 72 20 2a 70 50  andler(Pager *pP
a91b0 61 67 65 72 2c 20 42 75 73 79 48 61 6e 64 6c 65  ager, BusyHandle
a91c0 72 20 2a 70 42 75 73 79 48 61 6e 64 6c 65 72 29  r *pBusyHandler)
a91d0 7b 0a 20 20 70 50 61 67 65 72 2d 3e 70 42 75 73  {.  pPager->pBus
a91e0 79 48 61 6e 64 6c 65 72 20 3d 20 70 42 75 73 79  yHandler = pBusy
a91f0 48 61 6e 64 6c 65 72 3b 0a 7d 0a 0a 2f 2a 0a 2a  Handler;.}../*.*
a9200 2a 20 53 65 74 20 74 68 65 20 64 65 73 74 72 75  * Set the destru
a9210 63 74 6f 72 20 66 6f 72 20 74 68 69 73 20 70 61  ctor for this pa
a9220 67 65 72 2e 20 20 49 66 20 6e 6f 74 20 4e 55 4c  ger.  If not NUL
a9230 4c 2c 20 74 68 65 20 64 65 73 74 72 75 63 74 6f  L, the destructo
a9240 72 20 69 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 77  r is called.** w
a9250 68 65 6e 20 74 68 65 20 72 65 66 65 72 65 6e 63  hen the referenc
a9260 65 20 63 6f 75 6e 74 20 6f 6e 20 65 61 63 68 20  e count on each 
a9270 70 61 67 65 20 72 65 61 63 68 65 73 20 7a 65 72  page reaches zer
a9280 6f 2e 20 20 54 68 65 20 64 65 73 74 72 75 63 74  o.  The destruct
a9290 6f 72 20 63 61 6e 0a 2a 2a 20 62 65 20 75 73 65  or can.** be use
a92a0 64 20 74 6f 20 63 6c 65 61 6e 20 75 70 20 69 6e  d to clean up in
a92b0 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 74 68 65  formation in the
a92c0 20 65 78 74 72 61 20 73 65 67 6d 65 6e 74 20 61   extra segment a
a92d0 70 70 65 6e 64 65 64 20 74 6f 20 65 61 63 68 20  ppended to each 
a92e0 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  page..**.** The 
a92f0 64 65 73 74 72 75 63 74 6f 72 20 69 73 20 6e 6f  destructor is no
a9300 74 20 63 61 6c 6c 65 64 20 61 73 20 61 20 72 65  t called as a re
a9310 73 75 6c 74 20 73 71 6c 69 74 65 33 50 61 67 65  sult sqlite3Page
a9320 72 43 6c 6f 73 65 28 29 2e 20 20 0a 2a 2a 20 44  rClose().  .** D
a9330 65 73 74 72 75 63 74 6f 72 73 20 61 72 65 20 6f  estructors are o
a9340 6e 6c 79 20 63 61 6c 6c 65 64 20 62 79 20 73 71  nly called by sq
a9350 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28  lite3PagerUnref(
a9360 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
a9370 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
a9380 33 50 61 67 65 72 53 65 74 44 65 73 74 72 75 63  3PagerSetDestruc
a9390 74 6f 72 28 50 61 67 65 72 20 2a 70 50 61 67 65  tor(Pager *pPage
a93a0 72 2c 20 76 6f 69 64 20 28 2a 78 44 65 73 63 29  r, void (*xDesc)
a93b0 28 44 62 50 61 67 65 2a 2c 69 6e 74 29 29 7b 0a  (DbPage*,int)){.
a93c0 20 20 70 50 61 67 65 72 2d 3e 78 44 65 73 74 72    pPager->xDestr
a93d0 75 63 74 6f 72 20 3d 20 78 44 65 73 63 3b 0a 7d  uctor = xDesc;.}
a93e0 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
a93f0 72 65 69 6e 69 74 69 61 6c 69 7a 65 72 20 66 6f  reinitializer fo
a9400 72 20 74 68 69 73 20 70 61 67 65 72 2e 20 20 49  r this pager.  I
a9410 66 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 20  f not NULL, the 
a9420 72 65 69 6e 69 74 69 61 6c 69 7a 65 72 0a 2a 2a  reinitializer.**
a9430 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20   is called when 
a9440 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61  the content of a
a9450 20 70 61 67 65 20 69 6e 20 63 61 63 68 65 20 69   page in cache i
a9460 73 20 72 65 73 74 6f 72 65 64 20 74 6f 20 69 74  s restored to it
a9470 73 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 76 61  s original.** va
a9480 6c 75 65 20 61 73 20 61 20 72 65 73 75 6c 74 20  lue as a result 
a9490 6f 66 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 20 20  of a rollback.  
a94a0 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 67 69 76  The callback giv
a94b0 65 73 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 20  es higher-level 
a94c0 63 6f 64 65 0a 2a 2a 20 61 6e 20 6f 70 70 6f 72  code.** an oppor
a94d0 74 75 6e 69 74 79 20 74 6f 20 72 65 73 74 6f 72  tunity to restor
a94e0 65 20 74 68 65 20 45 58 54 52 41 20 73 65 63 74  e the EXTRA sect
a94f0 69 6f 6e 20 74 6f 20 61 67 72 65 65 20 77 69 74  ion to agree wit
a9500 68 20 74 68 65 20 72 65 73 74 6f 72 65 64 0a 2a  h the restored.*
a9510 2a 20 70 61 67 65 20 64 61 74 61 2e 0a 2a 2f 0a  * page data..*/.
a9520 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
a9530 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72  oid sqlite3Pager
a9540 53 65 74 52 65 69 6e 69 74 65 72 28 50 61 67 65  SetReiniter(Page
a9550 72 20 2a 70 50 61 67 65 72 2c 20 76 6f 69 64 20  r *pPager, void 
a9560 28 2a 78 52 65 69 6e 69 74 29 28 44 62 50 61 67  (*xReinit)(DbPag
a9570 65 2a 2c 69 6e 74 29 29 7b 0a 20 20 70 50 61 67  e*,int)){.  pPag
a9580 65 72 2d 3e 78 52 65 69 6e 69 74 65 72 20 3d 20  er->xReiniter = 
a9590 78 52 65 69 6e 69 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  xReinit;.}../*.*
a95a0 2a 20 53 65 74 20 74 68 65 20 70 61 67 65 20 73  * Set the page s
a95b0 69 7a 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65  ize.  Return the
a95c0 20 6e 65 77 20 73 69 7a 65 2e 20 20 49 66 20 74   new size.  If t
a95d0 68 65 20 73 75 67 67 65 73 74 20 6e 65 77 20 70  he suggest new p
a95e0 61 67 65 0a 2a 2a 20 73 69 7a 65 20 69 73 20 69  age.** size is i
a95f0 6e 61 70 70 72 6f 70 72 69 61 74 65 2c 20 74 68  nappropriate, th
a9600 65 6e 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76  en an alternativ
a9610 65 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 73  e page size is s
a9620 65 6c 65 63 74 65 64 0a 2a 2a 20 61 6e 64 20 72  elected.** and r
a9630 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49  eturned..*/.SQLI
a9640 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
a9650 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61  qlite3PagerSetPa
a9660 67 65 73 69 7a 65 28 50 61 67 65 72 20 2a 70 50  gesize(Pager *pP
a9670 61 67 65 72 2c 20 69 6e 74 20 70 61 67 65 53 69  ager, int pageSi
a9680 7a 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  ze){.  assert( p
a9690 61 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 26 20  ageSize>=512 && 
a96a0 70 61 67 65 53 69 7a 65 3c 3d 53 51 4c 49 54 45  pageSize<=SQLITE
a96b0 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 29  _MAX_PAGE_SIZE )
a96c0 3b 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 2d  ;.  if( !pPager-
a96d0 3e 6d 65 6d 44 62 20 26 26 20 70 50 61 67 65 72  >memDb && pPager
a96e0 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20  ->nRef==0 ){.   
a96f0 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50 61   pager_reset(pPa
a9700 67 65 72 29 3b 0a 20 20 20 20 70 50 61 67 65 72  ger);.    pPager
a9710 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 70 61 67  ->pageSize = pag
a9720 65 53 69 7a 65 3b 0a 20 20 20 20 70 50 61 67 65  eSize;.    pPage
a9730 72 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d 20 73  r->pTmpSpace = s
a9740 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 4f 72 46  qlite3ReallocOrF
a9750 72 65 65 28 70 50 61 67 65 72 2d 3e 70 54 6d 70  ree(pPager->pTmp
a9760 53 70 61 63 65 2c 20 70 61 67 65 53 69 7a 65 29  Space, pageSize)
a9770 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
a9780 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b  Pager->pageSize;
a9790 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70  .}../*.** Attemp
a97a0 74 20 74 6f 20 73 65 74 20 74 68 65 20 6d 61 78  t to set the max
a97b0 69 6d 75 6d 20 64 61 74 61 62 61 73 65 20 70 61  imum database pa
a97c0 67 65 20 63 6f 75 6e 74 20 69 66 20 6d 78 50 61  ge count if mxPa
a97d0 67 65 20 69 73 20 70 6f 73 69 74 69 76 65 2e 20  ge is positive. 
a97e0 0a 2a 2a 20 4d 61 6b 65 20 6e 6f 20 63 68 61 6e  .** Make no chan
a97f0 67 65 73 20 69 66 20 6d 78 50 61 67 65 20 69 73  ges if mxPage is
a9800 20 7a 65 72 6f 20 6f 72 20 6e 65 67 61 74 69 76   zero or negativ
a9810 65 2e 20 20 41 6e 64 20 6e 65 76 65 72 20 72 65  e.  And never re
a9820 64 75 63 65 20 74 68 65 0a 2a 2a 20 6d 61 78 69  duce the.** maxi
a9830 6d 75 6d 20 70 61 67 65 20 63 6f 75 6e 74 20 62  mum page count b
a9840 65 6c 6f 77 20 74 68 65 20 63 75 72 72 65 6e 74  elow the current
a9850 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74   size of the dat
a9860 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 67  abase..**.** Reg
a9870 61 72 64 6c 65 73 73 20 6f 66 20 6d 78 50 61 67  ardless of mxPag
a9880 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20 63 75  e, return the cu
a9890 72 72 65 6e 74 20 6d 61 78 69 6d 75 6d 20 70 61  rrent maximum pa
a98a0 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c  ge count..*/.SQL
a98b0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
a98c0 73 71 6c 69 74 65 33 50 61 67 65 72 4d 61 78 50  sqlite3PagerMaxP
a98d0 61 67 65 43 6f 75 6e 74 28 50 61 67 65 72 20 2a  ageCount(Pager *
a98e0 70 50 61 67 65 72 2c 20 69 6e 74 20 6d 78 50 61  pPager, int mxPa
a98f0 67 65 29 7b 0a 20 20 69 66 28 20 6d 78 50 61 67  ge){.  if( mxPag
a9900 65 3e 30 20 29 7b 0a 20 20 20 20 70 50 61 67 65  e>0 ){.    pPage
a9910 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20 6d 78 50 61  r->mxPgno = mxPa
a9920 67 65 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  ge;.  }.  sqlite
a9930 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28  3PagerPagecount(
a9940 70 50 61 67 65 72 29 3b 0a 20 20 72 65 74 75 72  pPager);.  retur
a9950 6e 20 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f  n pPager->mxPgno
a9960 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  ;.}../*.** The f
a9970 6f 6c 6c 6f 77 69 6e 67 20 73 65 74 20 6f 66 20  ollowing set of 
a9980 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65  routines are use
a9990 64 20 74 6f 20 64 69 73 61 62 6c 65 20 74 68 65  d to disable the
a99a0 20 73 69 6d 75 6c 61 74 65 64 0a 2a 2a 20 49 2f   simulated.** I/
a99b0 4f 20 65 72 72 6f 72 20 6d 65 63 68 61 6e 69 73  O error mechanis
a99c0 6d 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e  m.  These routin
a99d0 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 61  es are used to a
a99e0 76 6f 69 64 20 73 69 6d 75 6c 61 74 65 64 0a 2a  void simulated.*
a99f0 2a 20 65 72 72 6f 72 73 20 69 6e 20 70 6c 61 63  * errors in plac
a9a00 65 73 20 77 68 65 72 65 20 77 65 20 64 6f 20 6e  es where we do n
a9a10 6f 74 20 63 61 72 65 20 61 62 6f 75 74 20 65 72  ot care about er
a9a20 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 65  rors..**.** Unle
a9a30 73 73 20 2d 44 53 51 4c 49 54 45 5f 54 45 53 54  ss -DSQLITE_TEST
a9a40 3d 31 20 69 73 20 75 73 65 64 2c 20 74 68 65 73  =1 is used, thes
a9a50 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 61  e routines are a
a9a60 6c 6c 20 6e 6f 2d 6f 70 73 0a 2a 2a 20 61 6e 64  ll no-ops.** and
a9a70 20 67 65 6e 65 72 61 74 65 20 6e 6f 20 63 6f 64   generate no cod
a9a80 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  e..*/.#ifdef SQL
a9a90 49 54 45 5f 54 45 53 54 0a 65 78 74 65 72 6e 20  ITE_TEST.extern 
a9aa0 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  int sqlite3_io_e
a9ab0 72 72 6f 72 5f 70 65 6e 64 69 6e 67 3b 0a 65 78  rror_pending;.ex
a9ac0 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33  tern int sqlite3
a9ad0 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 3b 0a 73  _io_error_hit;.s
a9ae0 74 61 74 69 63 20 69 6e 74 20 73 61 76 65 64 5f  tatic int saved_
a9af0 63 6e 74 3b 0a 76 6f 69 64 20 64 69 73 61 62 6c  cnt;.void disabl
a9b00 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65  e_simulated_io_e
a9b10 72 72 6f 72 73 28 76 6f 69 64 29 7b 0a 20 20 73  rrors(void){.  s
a9b20 61 76 65 64 5f 63 6e 74 20 3d 20 73 71 6c 69 74  aved_cnt = sqlit
a9b30 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64  e3_io_error_pend
a9b40 69 6e 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69  ing;.  sqlite3_i
a9b50 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20  o_error_pending 
a9b60 3d 20 2d 31 3b 0a 7d 0a 76 6f 69 64 20 65 6e 61  = -1;.}.void ena
a9b70 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f  ble_simulated_io
a9b80 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 7b 0a 20  _errors(void){. 
a9b90 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
a9ba0 72 5f 70 65 6e 64 69 6e 67 20 3d 20 73 61 76 65  r_pending = save
a9bb0 64 5f 63 6e 74 3b 0a 7d 0a 23 65 6c 73 65 0a 23  d_cnt;.}.#else.#
a9bc0 20 64 65 66 69 6e 65 20 64 69 73 61 62 6c 65 5f   define disable_
a9bd0 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72  simulated_io_err
a9be0 6f 72 73 28 29 0a 23 20 64 65 66 69 6e 65 20 65  ors().# define e
a9bf0 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f  nable_simulated_
a9c00 69 6f 5f 65 72 72 6f 72 73 28 29 0a 23 65 6e 64  io_errors().#end
a9c10 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74  if../*.** Read t
a9c20 68 65 20 66 69 72 73 74 20 4e 20 62 79 74 65 73  he first N bytes
a9c30 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e   from the beginn
a9c40 69 6e 67 20 6f 66 20 74 68 65 20 66 69 6c 65 20  ing of the file 
a9c50 69 6e 74 6f 20 6d 65 6d 6f 72 79 0a 2a 2a 20 74  into memory.** t
a9c60 68 61 74 20 70 44 65 73 74 20 70 6f 69 6e 74 73  hat pDest points
a9c70 20 74 6f 2e 20 0a 2a 2a 0a 2a 2a 20 4e 6f 20 65   to. .**.** No e
a9c80 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 20 69 73  rror checking is
a9c90 20 64 6f 6e 65 2e 20 54 68 65 20 72 61 74 69 6f   done. The ratio
a9ca0 6e 61 6c 20 66 6f 72 20 74 68 69 73 20 69 73 20  nal for this is 
a9cb0 74 68 61 74 20 74 68 69 73 20 66 75 6e 63 74 69  that this functi
a9cc0 6f 6e 20 0a 2a 2a 20 6d 61 79 20 62 65 20 63 61  on .** may be ca
a9cd0 6c 6c 65 64 20 65 76 65 6e 20 69 66 20 74 68 65  lled even if the
a9ce0 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 65   file does not e
a9cf0 78 69 73 74 20 6f 72 20 63 6f 6e 74 61 69 6e 20  xist or contain 
a9d00 61 20 68 65 61 64 65 72 2e 20 49 6e 20 0a 2a 2a  a header. In .**
a9d10 20 74 68 65 73 65 20 63 61 73 65 73 20 73 71 6c   these cases sql
a9d20 69 74 65 33 4f 73 52 65 61 64 28 29 20 77 69 6c  ite3OsRead() wil
a9d30 6c 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f  l return an erro
a9d40 72 2c 20 74 6f 20 77 68 69 63 68 20 74 68 65 20  r, to which the 
a9d50 63 6f 72 72 65 63 74 20 0a 2a 2a 20 72 65 73 70  correct .** resp
a9d60 6f 6e 73 65 20 69 73 20 74 6f 20 7a 65 72 6f 20  onse is to zero 
a9d70 74 68 65 20 6d 65 6d 6f 72 79 20 61 74 20 70 44  the memory at pD
a9d80 65 73 74 20 61 6e 64 20 63 6f 6e 74 69 6e 75 65  est and continue
a9d90 2e 20 20 41 20 72 65 61 6c 20 49 4f 20 65 72 72  .  A real IO err
a9da0 6f 72 20 0a 2a 2a 20 77 69 6c 6c 20 70 72 65 73  or .** will pres
a9db0 75 6d 61 62 6c 79 20 72 65 63 75 72 20 61 6e 64  umably recur and
a9dc0 20 62 65 20 70 69 63 6b 65 64 20 75 70 20 6c 61   be picked up la
a9dd0 74 65 72 20 28 54 6f 64 6f 3a 20 54 68 69 6e 6b  ter (Todo: Think
a9de0 20 61 62 6f 75 74 20 74 68 69 73 29 2e 0a 2a 2f   about this)..*/
a9df0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
a9e00 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
a9e10 52 65 61 64 46 69 6c 65 68 65 61 64 65 72 28 50  ReadFileheader(P
a9e20 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e  ager *pPager, in
a9e30 74 20 4e 2c 20 75 6e 73 69 67 6e 65 64 20 63 68  t N, unsigned ch
a9e40 61 72 20 2a 70 44 65 73 74 29 7b 0a 20 20 69 6e  ar *pDest){.  in
a9e50 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
a9e60 3b 0a 20 20 6d 65 6d 73 65 74 28 70 44 65 73 74  ;.  memset(pDest
a9e70 2c 20 30 2c 20 4e 29 3b 0a 20 20 69 66 28 20 4d  , 0, N);.  if( M
a9e80 45 4d 44 42 3d 3d 30 20 29 7b 0a 20 20 20 20 64  EMDB==0 ){.    d
a9e90 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64  isable_simulated
a9ea0 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a 20 20  _io_errors();.  
a9eb0 20 20 73 71 6c 69 74 65 33 4f 73 53 65 65 6b 28    sqlite3OsSeek(
a9ec0 70 50 61 67 65 72 2d 3e 66 64 2c 20 30 29 3b 0a  pPager->fd, 0);.
a9ed0 20 20 20 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c      enable_simul
a9ee0 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29  ated_io_errors()
a9ef0 3b 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22  ;.    IOTRACE(("
a9f00 44 42 48 44 52 20 25 70 20 30 20 25 64 5c 6e 22  DBHDR %p 0 %d\n"
a9f10 2c 20 70 50 61 67 65 72 2c 20 4e 29 29 0a 20 20  , pPager, N)).  
a9f20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
a9f30 52 65 61 64 28 70 50 61 67 65 72 2d 3e 66 64 2c  Read(pPager->fd,
a9f40 20 70 44 65 73 74 2c 20 4e 29 3b 0a 20 20 20 20   pDest, N);.    
a9f50 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49  if( rc==SQLITE_I
a9f60 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20  OERR_SHORT_READ 
a9f70 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
a9f80 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20  LITE_OK;.    }. 
a9f90 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
a9fa0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
a9fb0 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72  the total number
a9fc0 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65   of pages in the
a9fd0 20 64 69 73 6b 20 66 69 6c 65 20 61 73 73 6f 63   disk file assoc
a9fe0 69 61 74 65 64 20 77 69 74 68 0a 2a 2a 20 70 50  iated with.** pP
a9ff0 61 67 65 72 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20  ager. .**.** If 
aa000 74 68 65 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  the PENDING_BYTE
aa010 20 6c 69 65 73 20 6f 6e 20 74 68 65 20 70 61 67   lies on the pag
aa020 65 20 64 69 72 65 63 74 6c 79 20 61 66 74 65 72  e directly after
aa030 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a   the end of the.
aa040 2a 2a 20 66 69 6c 65 2c 20 74 68 65 6e 20 63 6f  ** file, then co
aa050 6e 73 69 64 65 72 20 74 68 69 73 20 70 61 67 65  nsider this page
aa060 20 70 61 72 74 20 6f 66 20 74 68 65 20 66 69 6c   part of the fil
aa070 65 20 74 6f 6f 2e 20 46 6f 72 20 65 78 61 6d 70  e too. For examp
aa080 6c 65 2c 20 69 66 0a 2a 2a 20 50 45 4e 44 49 4e  le, if.** PENDIN
aa090 47 5f 42 59 54 45 20 69 73 20 62 79 74 65 20 34  G_BYTE is byte 4
aa0a0 30 39 36 20 28 74 68 65 20 66 69 72 73 74 20 62  096 (the first b
aa0b0 79 74 65 20 6f 66 20 70 61 67 65 20 35 29 20 61  yte of page 5) a
aa0c0 6e 64 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  nd the size of t
aa0d0 68 65 0a 2a 2a 20 66 69 6c 65 20 69 73 20 34 30  he.** file is 40
aa0e0 39 36 20 62 79 74 65 73 2c 20 35 20 69 73 20 72  96 bytes, 5 is r
aa0f0 65 74 75 72 6e 65 64 20 69 6e 73 74 65 61 64 20  eturned instead 
aa100 6f 66 20 34 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  of 4..*/.SQLITE_
aa110 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
aa120 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e  te3PagerPagecoun
aa130 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  t(Pager *pPager)
aa140 7b 0a 20 20 69 36 34 20 6e 3b 0a 20 20 69 6e 74  {.  i64 n;.  int
aa150 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70   rc;.  assert( p
aa160 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 69 66  Pager!=0 );.  if
aa170 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  ( pPager->errCod
aa180 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  e ){.    return 
aa190 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61  0;.  }.  if( pPa
aa1a0 67 65 72 2d 3e 64 62 53 69 7a 65 3e 3d 30 20 29  ger->dbSize>=0 )
aa1b0 7b 0a 20 20 20 20 6e 20 3d 20 70 50 61 67 65 72  {.    n = pPager
aa1c0 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 7d 20 65 6c  ->dbSize;.  } el
aa1d0 73 65 20 7b 0a 20 20 20 20 69 66 28 20 28 72 63  se {.    if( (rc
aa1e0 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65   = sqlite3OsFile
aa1f0 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 66 64 2c  Size(pPager->fd,
aa200 20 26 6e 29 29 21 3d 53 51 4c 49 54 45 5f 4f 4b   &n))!=SQLITE_OK
aa210 20 29 7b 0a 20 20 20 20 20 20 70 61 67 65 72 5f   ){.      pager_
aa220 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63  error(pPager, rc
aa230 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
aa240 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  0;.    }.    if(
aa250 20 6e 3e 30 20 26 26 20 6e 3c 70 50 61 67 65 72   n>0 && n<pPager
aa260 2d 3e 70 61 67 65 53 69 7a 65 20 29 7b 0a 20 20  ->pageSize ){.  
aa270 20 20 20 20 6e 20 3d 20 31 3b 0a 20 20 20 20 7d      n = 1;.    }
aa280 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 20 2f 3d  else{.      n /=
aa290 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a   pPager->pageSiz
aa2a0 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  e;.    }.    if(
aa2b0 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 21 3d   pPager->state!=
aa2c0 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 7b 0a  PAGER_UNLOCK ){.
aa2d0 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62        pPager->db
aa2e0 53 69 7a 65 20 3d 20 6e 3b 0a 20 20 20 20 7d 0a  Size = n;.    }.
aa2f0 20 20 7d 0a 20 20 69 66 28 20 6e 3d 3d 28 50 45    }.  if( n==(PE
aa300 4e 44 49 4e 47 5f 42 59 54 45 2f 70 50 61 67 65  NDING_BYTE/pPage
aa310 72 2d 3e 70 61 67 65 53 69 7a 65 29 20 29 7b 0a  r->pageSize) ){.
aa320 20 20 20 20 6e 2b 2b 3b 0a 20 20 7d 0a 20 20 69      n++;.  }.  i
aa330 66 28 20 6e 3e 70 50 61 67 65 72 2d 3e 6d 78 50  f( n>pPager->mxP
aa340 67 6e 6f 20 29 7b 0a 20 20 20 20 70 50 61 67 65  gno ){.    pPage
aa350 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20 6e 3b 0a 20  r->mxPgno = n;. 
aa360 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d   }.  return n;.}
aa370 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
aa380 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 0a  E_OMIT_MEMORYDB.
aa390 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 20 50 67  /*.** Clear a Pg
aa3a0 48 69 73 74 6f 72 79 20 62 6c 6f 63 6b 0a 2a 2f  History block.*/
aa3b0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c 65  .static void cle
aa3c0 61 72 48 69 73 74 6f 72 79 28 50 67 48 69 73 74  arHistory(PgHist
aa3d0 6f 72 79 20 2a 70 48 69 73 74 29 7b 0a 20 20 73  ory *pHist){.  s
aa3e0 71 6c 69 74 65 46 72 65 65 28 70 48 69 73 74 2d  qliteFree(pHist-
aa3f0 3e 70 4f 72 69 67 29 3b 0a 20 20 73 71 6c 69 74  >pOrig);.  sqlit
aa400 65 46 72 65 65 28 70 48 69 73 74 2d 3e 70 53 74  eFree(pHist->pSt
aa410 6d 74 29 3b 0a 20 20 70 48 69 73 74 2d 3e 70 4f  mt);.  pHist->pO
aa420 72 69 67 20 3d 20 30 3b 0a 20 20 70 48 69 73 74  rig = 0;.  pHist
aa430 2d 3e 70 53 74 6d 74 20 3d 20 30 3b 0a 7d 0a 23  ->pStmt = 0;.}.#
aa440 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 63 6c 65  else.#define cle
aa450 61 72 48 69 73 74 6f 72 79 28 78 29 0a 23 65 6e  arHistory(x).#en
aa460 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 61  dif../*.** Forwa
aa470 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 0a 2a  rd declaration.*
aa480 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 79 6e  /.static int syn
aa490 63 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 2a 29  cJournal(Pager*)
aa4a0 3b 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20  ;../*.** Unlink 
aa4b0 70 50 67 20 66 72 6f 6d 20 69 74 27 73 20 68 61  pPg from it's ha
aa4c0 73 68 20 63 68 61 69 6e 2e 20 41 6c 73 6f 20 73  sh chain. Also s
aa4d0 65 74 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62  et the page numb
aa4e0 65 72 20 74 6f 20 30 20 74 6f 20 69 6e 64 69 63  er to 0 to indic
aa4f0 61 74 65 0a 2a 2a 20 74 68 61 74 20 74 68 65 20  ate.** that the 
aa500 70 61 67 65 20 69 73 20 6e 6f 74 20 70 61 72 74  page is not part
aa510 20 6f 66 20 61 6e 79 20 68 61 73 68 20 63 68 61   of any hash cha
aa520 69 6e 2e 20 54 68 69 73 20 69 73 20 72 65 71 75  in. This is requ
aa530 69 72 65 64 20 62 65 63 61 75 73 65 20 74 68 65  ired because the
aa540 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72  .** sqlite3Pager
aa550 4d 6f 76 65 70 61 67 65 28 29 20 72 6f 75 74 69  Movepage() routi
aa560 6e 65 20 63 61 6e 20 6c 65 61 76 65 20 61 20 70  ne can leave a p
aa570 61 67 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20 70  age in the .** p
aa580 4e 65 78 74 46 72 65 65 2f 70 50 72 65 76 46 72  NextFree/pPrevFr
aa590 65 65 20 6c 69 73 74 20 74 68 61 74 20 69 73 20  ee list that is 
aa5a0 6e 6f 74 20 61 20 70 61 72 74 20 6f 66 20 61 6e  not a part of an
aa5b0 79 20 68 61 73 68 2d 63 68 61 69 6e 2e 0a 2a 2f  y hash-chain..*/
aa5c0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e 6c  .static void unl
aa5d0 69 6e 6b 48 61 73 68 43 68 61 69 6e 28 50 61 67  inkHashChain(Pag
aa5e0 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 48 64  er *pPager, PgHd
aa5f0 72 20 2a 70 50 67 29 7b 0a 20 20 69 66 28 20 70  r *pPg){.  if( p
aa600 50 67 2d 3e 70 67 6e 6f 3d 3d 30 20 29 7b 0a 20  Pg->pgno==0 ){. 
aa610 20 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e     assert( pPg->
aa620 70 4e 65 78 74 48 61 73 68 3d 3d 30 20 26 26 20  pNextHash==0 && 
aa630 70 50 67 2d 3e 70 50 72 65 76 48 61 73 68 3d 3d  pPg->pPrevHash==
aa640 30 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  0 );.    return;
aa650 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 67 2d 3e  .  }.  if( pPg->
aa660 70 4e 65 78 74 48 61 73 68 20 29 7b 0a 20 20 20  pNextHash ){.   
aa670 20 70 50 67 2d 3e 70 4e 65 78 74 48 61 73 68 2d   pPg->pNextHash-
aa680 3e 70 50 72 65 76 48 61 73 68 20 3d 20 70 50 67  >pPrevHash = pPg
aa690 2d 3e 70 50 72 65 76 48 61 73 68 3b 0a 20 20 7d  ->pPrevHash;.  }
aa6a0 0a 20 20 69 66 28 20 70 50 67 2d 3e 70 50 72 65  .  if( pPg->pPre
aa6b0 76 48 61 73 68 20 29 7b 0a 20 20 20 20 61 73 73  vHash ){.    ass
aa6c0 65 72 74 28 20 70 50 61 67 65 72 2d 3e 61 48 61  ert( pPager->aHa
aa6d0 73 68 5b 70 50 67 2d 3e 70 67 6e 6f 20 26 20 28  sh[pPg->pgno & (
aa6e0 70 50 61 67 65 72 2d 3e 6e 48 61 73 68 2d 31 29  pPager->nHash-1)
aa6f0 5d 21 3d 70 50 67 20 29 3b 0a 20 20 20 20 70 50  ]!=pPg );.    pP
aa700 67 2d 3e 70 50 72 65 76 48 61 73 68 2d 3e 70 4e  g->pPrevHash->pN
aa710 65 78 74 48 61 73 68 20 3d 20 70 50 67 2d 3e 70  extHash = pPg->p
aa720 4e 65 78 74 48 61 73 68 3b 0a 20 20 7d 65 6c 73  NextHash;.  }els
aa730 65 7b 0a 20 20 20 20 69 6e 74 20 68 20 3d 20 70  e{.    int h = p
aa740 50 67 2d 3e 70 67 6e 6f 20 26 20 28 70 50 61 67  Pg->pgno & (pPag
aa750 65 72 2d 3e 6e 48 61 73 68 2d 31 29 3b 0a 20 20  er->nHash-1);.  
aa760 20 20 70 50 61 67 65 72 2d 3e 61 48 61 73 68 5b    pPager->aHash[
aa770 68 5d 20 3d 20 70 50 67 2d 3e 70 4e 65 78 74 48  h] = pPg->pNextH
aa780 61 73 68 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4d  ash;.  }.  if( M
aa790 45 4d 44 42 20 29 7b 0a 20 20 20 20 63 6c 65 61  EMDB ){.    clea
aa7a0 72 48 69 73 74 6f 72 79 28 50 47 48 44 52 5f 54  rHistory(PGHDR_T
aa7b0 4f 5f 48 49 53 54 28 70 50 67 2c 20 70 50 61 67  O_HIST(pPg, pPag
aa7c0 65 72 29 29 3b 0a 20 20 7d 0a 20 20 70 50 67 2d  er));.  }.  pPg-
aa7d0 3e 70 67 6e 6f 20 3d 20 30 3b 0a 20 20 70 50 67  >pgno = 0;.  pPg
aa7e0 2d 3e 70 4e 65 78 74 48 61 73 68 20 3d 20 70 50  ->pNextHash = pP
aa7f0 67 2d 3e 70 50 72 65 76 48 61 73 68 20 3d 20 30  g->pPrevHash = 0
aa800 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e  ;.}../*.** Unlin
aa810 6b 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68  k a page from th
aa820 65 20 66 72 65 65 20 6c 69 73 74 20 28 74 68 65  e free list (the
aa830 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 70 61 67   list of all pag
aa840 65 73 20 77 68 65 72 65 20 6e 52 65 66 3d 3d 30  es where nRef==0
aa850 29 0a 2a 2a 20 61 6e 64 20 66 72 6f 6d 20 69 74  ).** and from it
aa860 73 20 68 61 73 68 20 63 6f 6c 6c 69 73 69 6f 6e  s hash collision
aa870 20 63 68 61 69 6e 2e 0a 2a 2f 0a 73 74 61 74 69   chain..*/.stati
aa880 63 20 76 6f 69 64 20 75 6e 6c 69 6e 6b 50 61 67  c void unlinkPag
aa890 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20  e(PgHdr *pPg){. 
aa8a0 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d   Pager *pPager =
aa8b0 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 0a 20   pPg->pPager;.. 
aa8c0 20 2f 2a 20 4b 65 65 70 20 74 68 65 20 70 46 69   /* Keep the pFi
aa8d0 72 73 74 53 79 6e 63 65 64 20 70 6f 69 6e 74 65  rstSynced pointe
aa8e0 72 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 74 68  r pointing at th
aa8f0 65 20 66 69 72 73 74 20 73 79 6e 63 68 72 6f 6e  e first synchron
aa900 69 7a 65 64 20 70 61 67 65 20 2a 2f 0a 20 20 69  ized page */.  i
aa910 66 28 20 70 50 67 3d 3d 70 50 61 67 65 72 2d 3e  f( pPg==pPager->
aa920 70 46 69 72 73 74 53 79 6e 63 65 64 20 29 7b 0a  pFirstSynced ){.
aa930 20 20 20 20 50 67 48 64 72 20 2a 70 20 3d 20 70      PgHdr *p = p
aa940 50 67 2d 3e 70 4e 65 78 74 46 72 65 65 3b 0a 20  Pg->pNextFree;. 
aa950 20 20 20 77 68 69 6c 65 28 20 70 20 26 26 20 70     while( p && p
aa960 2d 3e 6e 65 65 64 53 79 6e 63 20 29 7b 20 70 20  ->needSync ){ p 
aa970 3d 20 70 2d 3e 70 4e 65 78 74 46 72 65 65 3b 20  = p->pNextFree; 
aa980 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 46  }.    pPager->pF
aa990 69 72 73 74 53 79 6e 63 65 64 20 3d 20 70 3b 0a  irstSynced = p;.
aa9a0 20 20 7d 0a 0a 20 20 2f 2a 20 55 6e 6c 69 6e 6b    }..  /* Unlink
aa9b0 20 66 72 6f 6d 20 74 68 65 20 66 72 65 65 6c 69   from the freeli
aa9c0 73 74 20 2a 2f 0a 20 20 69 66 28 20 70 50 67 2d  st */.  if( pPg-
aa9d0 3e 70 50 72 65 76 46 72 65 65 20 29 7b 0a 20 20  >pPrevFree ){.  
aa9e0 20 20 70 50 67 2d 3e 70 50 72 65 76 46 72 65 65    pPg->pPrevFree
aa9f0 2d 3e 70 4e 65 78 74 46 72 65 65 20 3d 20 70 50  ->pNextFree = pP
aaa00 67 2d 3e 70 4e 65 78 74 46 72 65 65 3b 0a 20 20  g->pNextFree;.  
aaa10 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72  }else{.    asser
aaa20 74 28 20 70 50 61 67 65 72 2d 3e 70 46 69 72 73  t( pPager->pFirs
aaa30 74 3d 3d 70 50 67 20 29 3b 0a 20 20 20 20 70 50  t==pPg );.    pP
aaa40 61 67 65 72 2d 3e 70 46 69 72 73 74 20 3d 20 70  ager->pFirst = p
aaa50 50 67 2d 3e 70 4e 65 78 74 46 72 65 65 3b 0a 20  Pg->pNextFree;. 
aaa60 20 7d 0a 20 20 69 66 28 20 70 50 67 2d 3e 70 4e   }.  if( pPg->pN
aaa70 65 78 74 46 72 65 65 20 29 7b 0a 20 20 20 20 70  extFree ){.    p
aaa80 50 67 2d 3e 70 4e 65 78 74 46 72 65 65 2d 3e 70  Pg->pNextFree->p
aaa90 50 72 65 76 46 72 65 65 20 3d 20 70 50 67 2d 3e  PrevFree = pPg->
aaaa0 70 50 72 65 76 46 72 65 65 3b 0a 20 20 7d 65 6c  pPrevFree;.  }el
aaab0 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  se{.    assert( 
aaac0 70 50 61 67 65 72 2d 3e 70 4c 61 73 74 3d 3d 70  pPager->pLast==p
aaad0 50 67 20 29 3b 0a 20 20 20 20 70 50 61 67 65 72  Pg );.    pPager
aaae0 2d 3e 70 4c 61 73 74 20 3d 20 70 50 67 2d 3e 70  ->pLast = pPg->p
aaaf0 50 72 65 76 46 72 65 65 3b 0a 20 20 7d 0a 20 20  PrevFree;.  }.  
aab00 70 50 67 2d 3e 70 4e 65 78 74 46 72 65 65 20 3d  pPg->pNextFree =
aab10 20 70 50 67 2d 3e 70 50 72 65 76 46 72 65 65 20   pPg->pPrevFree 
aab20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 55 6e 6c 69 6e  = 0;..  /* Unlin
aab30 6b 20 66 72 6f 6d 20 74 68 65 20 70 67 6e 6f 20  k from the pgno 
aab40 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20  hash table */.  
aab50 75 6e 6c 69 6e 6b 48 61 73 68 43 68 61 69 6e 28  unlinkHashChain(
aab60 70 50 61 67 65 72 2c 20 70 50 67 29 3b 0a 7d 0a  pPager, pPg);.}.
aab70 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
aab80 69 6e 65 20 69 73 20 75 73 65 64 20 74 6f 20 74  ine is used to t
aab90 72 75 6e 63 61 74 65 20 74 68 65 20 63 61 63 68  runcate the cach
aaba0 65 20 77 68 65 6e 20 61 20 64 61 74 61 62 61 73  e when a databas
aabb0 65 0a 2a 2a 20 69 73 20 74 72 75 6e 63 61 74 65  e.** is truncate
aabc0 64 2e 20 20 44 72 6f 70 20 66 72 6f 6d 20 74 68  d.  Drop from th
aabd0 65 20 63 61 63 68 65 20 61 6c 6c 20 70 61 67 65  e cache all page
aabe0 73 20 77 68 6f 73 65 20 70 67 6e 6f 20 69 73 0a  s whose pgno is.
aabf0 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20 70  ** larger than p
aac00 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 61 6e  Pager->dbSize an
aac10 64 20 69 73 20 75 6e 72 65 66 65 72 65 6e 63 65  d is unreference
aac20 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 66 65 72 65 6e  d..**.** Referen
aac30 63 65 64 20 70 61 67 65 73 20 6c 61 72 67 65 72  ced pages larger
aac40 20 74 68 61 6e 20 70 50 61 67 65 72 2d 3e 64 62   than pPager->db
aac50 53 69 7a 65 20 61 72 65 20 7a 65 72 6f 65 64 2e  Size are zeroed.
aac60 0a 2a 2a 0a 2a 2a 20 41 63 74 75 61 6c 6c 79 2c  .**.** Actually,
aac70 20 61 74 20 74 68 65 20 70 6f 69 6e 74 20 74 68   at the point th
aac80 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
aac90 6c 6c 65 64 2c 20 69 74 20 77 6f 75 6c 64 20 62  lled, it would b
aaca0 65 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 74 6f  e.** an error to
aacb0 20 68 61 76 65 20 61 20 72 65 66 65 72 65 6e 63   have a referenc
aacc0 65 64 20 70 61 67 65 2e 20 20 42 75 74 20 72 61  ed page.  But ra
aacd0 74 68 65 72 20 74 68 61 6e 20 64 65 6c 65 74 65  ther than delete
aace0 0a 2a 2a 20 74 68 61 74 20 70 61 67 65 20 61 6e  .** that page an
aacf0 64 20 67 75 61 72 61 6e 74 65 65 20 61 20 73 75  d guarantee a su
aad00 62 73 65 71 75 65 6e 74 20 73 65 67 66 61 75 6c  bsequent segfaul
aad10 74 2c 20 69 74 20 73 65 65 6d 73 20 62 65 74 74  t, it seems bett
aad20 65 72 0a 2a 2a 20 74 6f 20 7a 65 72 6f 20 69 74  er.** to zero it
aad30 20 61 6e 64 20 68 6f 70 65 20 74 68 61 74 20 77   and hope that w
aad40 65 20 65 72 72 6f 72 20 6f 75 74 20 73 61 6e 65  e error out sane
aad50 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ly..*/.static vo
aad60 69 64 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74  id pager_truncat
aad70 65 5f 63 61 63 68 65 28 50 61 67 65 72 20 2a 70  e_cache(Pager *p
aad80 50 61 67 65 72 29 7b 0a 20 20 50 67 48 64 72 20  Pager){.  PgHdr 
aad90 2a 70 50 67 3b 0a 20 20 50 67 48 64 72 20 2a 2a  *pPg;.  PgHdr **
aada0 70 70 50 67 3b 0a 20 20 69 6e 74 20 64 62 53 69  ppPg;.  int dbSi
aadb0 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53  ze = pPager->dbS
aadc0 69 7a 65 3b 0a 0a 20 20 70 70 50 67 20 3d 20 26  ize;..  ppPg = &
aadd0 70 50 61 67 65 72 2d 3e 70 41 6c 6c 3b 0a 20 20  pPager->pAll;.  
aade0 77 68 69 6c 65 28 20 28 70 50 67 20 3d 20 2a 70  while( (pPg = *p
aadf0 70 50 67 29 21 3d 30 20 29 7b 0a 20 20 20 20 69  pPg)!=0 ){.    i
aae00 66 28 20 70 50 67 2d 3e 70 67 6e 6f 3c 3d 64 62  f( pPg->pgno<=db
aae10 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 70 70  Size ){.      pp
aae20 50 67 20 3d 20 26 70 50 67 2d 3e 70 4e 65 78 74  Pg = &pPg->pNext
aae30 41 6c 6c 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  All;.    }else i
aae40 66 28 20 70 50 67 2d 3e 6e 52 65 66 3e 30 20 29  f( pPg->nRef>0 )
aae50 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 50  {.      memset(P
aae60 47 48 44 52 5f 54 4f 5f 44 41 54 41 28 70 50 67  GHDR_TO_DATA(pPg
aae70 29 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e 70 61  ), 0, pPager->pa
aae80 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 70  geSize);.      p
aae90 70 50 67 20 3d 20 26 70 50 67 2d 3e 70 4e 65 78  pPg = &pPg->pNex
aaea0 74 41 6c 6c 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  tAll;.    }else{
aaeb0 0a 20 20 20 20 20 20 2a 70 70 50 67 20 3d 20 70  .      *ppPg = p
aaec0 50 67 2d 3e 70 4e 65 78 74 41 6c 6c 3b 0a 20 20  Pg->pNextAll;.  
aaed0 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 50 47      IOTRACE(("PG
aaee0 46 52 45 45 20 25 70 20 25 64 5c 6e 22 2c 20 70  FREE %p %d\n", p
aaef0 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f  Pager, pPg->pgno
aaf00 29 29 3b 0a 20 20 20 20 20 20 50 41 47 45 52 5f  ));.      PAGER_
aaf10 49 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 61 67  INCR(sqlite3_pag
aaf20 65 72 5f 70 67 66 72 65 65 5f 63 6f 75 6e 74 29  er_pgfree_count)
aaf30 3b 0a 20 20 20 20 20 20 75 6e 6c 69 6e 6b 50 61  ;.      unlinkPa
aaf40 67 65 28 70 50 67 29 3b 0a 20 20 20 20 20 20 6d  ge(pPg);.      m
aaf50 61 6b 65 43 6c 65 61 6e 28 70 50 67 29 3b 0a 20  akeClean(pPg);. 
aaf60 20 20 20 20 20 73 71 6c 69 74 65 46 72 65 65 28       sqliteFree(
aaf70 70 50 67 29 3b 0a 20 20 20 20 20 20 70 50 61 67  pPg);.      pPag
aaf80 65 72 2d 3e 6e 50 61 67 65 2d 2d 3b 0a 20 20 20  er->nPage--;.   
aaf90 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
aafa0 54 72 79 20 74 6f 20 6f 62 74 61 69 6e 20 61 20  Try to obtain a 
aafb0 6c 6f 63 6b 20 6f 6e 20 61 20 66 69 6c 65 2e 20  lock on a file. 
aafc0 20 49 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79   Invoke the busy
aafd0 20 63 61 6c 6c 62 61 63 6b 20 69 66 20 74 68 65   callback if the
aafe0 20 6c 6f 63 6b 0a 2a 2a 20 69 73 20 63 75 72 72   lock.** is curr
aaff0 65 6e 74 6c 79 20 6e 6f 74 20 61 76 61 69 6c 61  ently not availa
ab000 62 6c 65 2e 20 20 52 65 70 65 61 74 20 75 6e 74  ble.  Repeat unt
ab010 69 6c 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c  il the busy call
ab020 62 61 63 6b 20 72 65 74 75 72 6e 73 0a 2a 2a 20  back returns.** 
ab030 66 61 6c 73 65 20 6f 72 20 75 6e 74 69 6c 20 74  false or until t
ab040 68 65 20 6c 6f 63 6b 20 73 75 63 63 65 65 64 73  he lock succeeds
ab050 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53  ..**.** Return S
ab060 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63  QLITE_OK on succ
ab070 65 73 73 20 61 6e 64 20 61 6e 20 65 72 72 6f 72  ess and an error
ab080 20 63 6f 64 65 20 69 66 20 77 65 20 63 61 6e 6e   code if we cann
ab090 6f 74 20 6f 62 74 61 69 6e 0a 2a 2a 20 74 68 65  ot obtain.** the
ab0a0 20 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63   lock..*/.static
ab0b0 20 69 6e 74 20 70 61 67 65 72 5f 77 61 69 74 5f   int pager_wait_
ab0c0 6f 6e 5f 6c 6f 63 6b 28 50 61 67 65 72 20 2a 70  on_lock(Pager *p
ab0d0 50 61 67 65 72 2c 20 69 6e 74 20 6c 6f 63 6b 74  Pager, int lockt
ab0e0 79 70 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  ype){.  int rc;.
ab0f0 0a 20 20 2f 2a 20 54 68 65 20 4f 53 20 6c 6f 63  .  /* The OS loc
ab100 6b 20 76 61 6c 75 65 73 20 6d 75 73 74 20 62 65  k values must be
ab110 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65   the same as the
ab120 20 50 61 67 65 72 20 6c 6f 63 6b 20 76 61 6c 75   Pager lock valu
ab130 65 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  es */.  assert( 
ab140 50 41 47 45 52 5f 53 48 41 52 45 44 3d 3d 53 48  PAGER_SHARED==SH
ab150 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61  ARED_LOCK );.  a
ab160 73 73 65 72 74 28 20 50 41 47 45 52 5f 52 45 53  ssert( PAGER_RES
ab170 45 52 56 45 44 3d 3d 52 45 53 45 52 56 45 44 5f  ERVED==RESERVED_
ab180 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74  LOCK );.  assert
ab190 28 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56  ( PAGER_EXCLUSIV
ab1a0 45 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  E==EXCLUSIVE_LOC
ab1b0 4b 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  K );..  /* If th
ab1c0 65 20 66 69 6c 65 20 69 73 20 63 75 72 72 65 6e  e file is curren
ab1d0 74 6c 79 20 75 6e 6c 6f 63 6b 65 64 20 74 68 65  tly unlocked the
ab1e0 6e 20 74 68 65 20 73 69 7a 65 20 6d 75 73 74 20  n the size must 
ab1f0 62 65 20 75 6e 6b 6e 6f 77 6e 20 2a 2f 0a 20 20  be unknown */.  
ab200 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
ab210 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41  state>=PAGER_SHA
ab220 52 45 44 20 7c 7c 20 70 50 61 67 65 72 2d 3e 64  RED || pPager->d
ab230 62 53 69 7a 65 3c 30 20 7c 7c 20 4d 45 4d 44 42  bSize<0 || MEMDB
ab240 20 29 3b 0a 0a 20 20 69 66 28 20 70 50 61 67 65   );..  if( pPage
ab250 72 2d 3e 73 74 61 74 65 3e 3d 6c 6f 63 6b 74 79  r->state>=lockty
ab260 70 65 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  pe ){.    rc = S
ab270 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73  QLITE_OK;.  }els
ab280 65 7b 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20  e{.    do {.    
ab290 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
ab2a0 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c  Lock(pPager->fd,
ab2b0 20 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 20 20   locktype);.    
ab2c0 7d 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49  }while( rc==SQLI
ab2d0 54 45 5f 42 55 53 59 20 26 26 20 73 71 6c 69 74  TE_BUSY && sqlit
ab2e0 65 33 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64  e3InvokeBusyHand
ab2f0 6c 65 72 28 70 50 61 67 65 72 2d 3e 70 42 75 73  ler(pPager->pBus
ab300 79 48 61 6e 64 6c 65 72 29 20 29 3b 0a 20 20 20  yHandler) );.   
ab310 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
ab320 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67  OK ){.      pPag
ab330 65 72 2d 3e 73 74 61 74 65 20 3d 20 6c 6f 63 6b  er->state = lock
ab340 74 79 70 65 3b 0a 20 20 20 20 20 20 49 4f 54 52  type;.      IOTR
ab350 41 43 45 28 28 22 4c 4f 43 4b 20 25 70 20 25 64  ACE(("LOCK %p %d
ab360 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 6c 6f 63  \n", pPager, loc
ab370 6b 74 79 70 65 29 29 0a 20 20 20 20 7d 0a 20 20  ktype)).    }.  
ab380 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
ab390 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65  ../*.** Truncate
ab3a0 20 74 68 65 20 66 69 6c 65 20 74 6f 20 74 68 65   the file to the
ab3b0 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
ab3c0 20 73 70 65 63 69 66 69 65 64 2e 0a 2a 2f 0a 53   specified..*/.S
ab3d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
ab3e0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72  t sqlite3PagerTr
ab3f0 75 6e 63 61 74 65 28 50 61 67 65 72 20 2a 70 50  uncate(Pager *pP
ab400 61 67 65 72 2c 20 50 67 6e 6f 20 6e 50 61 67 65  ager, Pgno nPage
ab410 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61  ){.  int rc;.  a
ab420 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
ab430 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 52  tate>=PAGER_SHAR
ab440 45 44 20 7c 7c 20 4d 45 4d 44 42 20 29 3b 0a 20  ED || MEMDB );. 
ab450 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
ab460 65 63 6f 75 6e 74 28 70 50 61 67 65 72 29 3b 0a  ecount(pPager);.
ab470 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72    if( pPager->er
ab480 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 72 63 20  rCode ){.    rc 
ab490 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  = pPager->errCod
ab4a0 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  e;.    return rc
ab4b0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 50 61 67  ;.  }.  if( nPag
ab4c0 65 3e 3d 28 75 6e 73 69 67 6e 65 64 29 70 50 61  e>=(unsigned)pPa
ab4d0 67 65 72 2d 3e 64 62 53 69 7a 65 20 29 7b 0a 20  ger->dbSize ){. 
ab4e0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
ab4f0 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4d  _OK;.  }.  if( M
ab500 45 4d 44 42 20 29 7b 0a 20 20 20 20 70 50 61 67  EMDB ){.    pPag
ab510 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6e 50 61  er->dbSize = nPa
ab520 67 65 3b 0a 20 20 20 20 70 61 67 65 72 5f 74 72  ge;.    pager_tr
ab530 75 6e 63 61 74 65 5f 63 61 63 68 65 28 70 50 61  uncate_cache(pPa
ab540 67 65 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ger);.    return
ab550 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
ab560 20 20 72 63 20 3d 20 73 79 6e 63 4a 6f 75 72 6e    rc = syncJourn
ab570 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 69 66  al(pPager);.  if
ab580 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
ab590 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
ab5a0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 74 20  ;.  }..  /* Get 
ab5b0 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63  an exclusive loc
ab5c0 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
ab5d0 65 20 62 65 66 6f 72 65 20 74 72 75 6e 63 61 74  e before truncat
ab5e0 69 6e 67 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 70  ing. */.  rc = p
ab5f0 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63  ager_wait_on_loc
ab600 6b 28 70 50 61 67 65 72 2c 20 45 58 43 4c 55 53  k(pPager, EXCLUS
ab610 49 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 69 66 28  IVE_LOCK);.  if(
ab620 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
ab630 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  {.    return rc;
ab640 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 70 61 67  .  }..  rc = pag
ab650 65 72 5f 74 72 75 6e 63 61 74 65 28 70 50 61 67  er_truncate(pPag
ab660 65 72 2c 20 6e 50 61 67 65 29 3b 0a 20 20 72 65  er, nPage);.  re
ab670 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
ab680 2a 20 53 68 75 74 64 6f 77 6e 20 74 68 65 20 70  * Shutdown the p
ab690 61 67 65 20 63 61 63 68 65 2e 20 20 46 72 65 65  age cache.  Free
ab6a0 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 6e 64 20   all memory and 
ab6b0 63 6c 6f 73 65 20 61 6c 6c 20 66 69 6c 65 73 2e  close all files.
ab6c0 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 74 72 61 6e  .**.** If a tran
ab6d0 73 61 63 74 69 6f 6e 20 77 61 73 20 69 6e 20 70  saction was in p
ab6e0 72 6f 67 72 65 73 73 20 77 68 65 6e 20 74 68 69  rogress when thi
ab6f0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
ab700 6c 65 64 2c 20 74 68 61 74 0a 2a 2a 20 74 72 61  led, that.** tra
ab710 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c  nsaction is roll
ab720 65 64 20 62 61 63 6b 2e 20 20 41 6c 6c 20 6f 75  ed back.  All ou
ab730 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 73 20  tstanding pages 
ab740 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65 64 0a  are invalidated.
ab750 2a 2a 20 61 6e 64 20 74 68 65 69 72 20 6d 65 6d  ** and their mem
ab760 6f 72 79 20 69 73 20 66 72 65 65 64 2e 20 20 41  ory is freed.  A
ab770 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73  ny attempt to us
ab780 65 20 61 20 70 61 67 65 20 61 73 73 6f 63 69 61  e a page associa
ab790 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 69 73  ted.** with this
ab7a0 20 70 61 67 65 20 63 61 63 68 65 20 61 66 74 65   page cache afte
ab7b0 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  r this function 
ab7c0 72 65 74 75 72 6e 73 20 77 69 6c 6c 20 6c 69 6b  returns will lik
ab7d0 65 6c 79 0a 2a 2a 20 72 65 73 75 6c 74 20 69 6e  ely.** result in
ab7e0 20 61 20 63 6f 72 65 64 75 6d 70 2e 0a 2a 2a 0a   a coredump..**.
ab7f0 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
ab800 20 61 6c 77 61 79 73 20 73 75 63 63 65 65 64 73   always succeeds
ab810 2e 20 49 66 20 61 20 74 72 61 6e 73 61 63 74 69  . If a transacti
ab820 6f 6e 20 69 73 20 61 63 74 69 76 65 20 61 6e 20  on is active an 
ab830 61 74 74 65 6d 70 74 0a 2a 2a 20 69 73 20 6d 61  attempt.** is ma
ab840 64 65 20 74 6f 20 72 6f 6c 6c 20 69 74 20 62 61  de to roll it ba
ab850 63 6b 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20  ck. If an error 
ab860 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 74 68  occurs during th
ab870 65 20 72 6f 6c 6c 62 61 63 6b 20 0a 2a 2a 20 61  e rollback .** a
ab880 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 6d 61 79   hot journal may
ab890 20 62 65 20 6c 65 66 74 20 69 6e 20 74 68 65 20   be left in the 
ab8a0 66 69 6c 65 73 79 73 74 65 6d 20 62 75 74 20 6e  filesystem but n
ab8b0 6f 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72  o error is retur
ab8c0 6e 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61  ned.** to the ca
ab8d0 6c 6c 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ller..*/.SQLITE_
ab8e0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
ab8f0 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 50 61  te3PagerClose(Pa
ab900 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 23 69  ger *pPager){.#i
ab910 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
ab920 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45  LE_MEMORY_MANAGE
ab930 4d 45 4e 54 0a 20 20 2f 2a 20 41 20 6d 61 6c 6c  MENT.  /* A mall
ab940 6f 63 28 29 20 63 61 6e 6e 6f 74 20 66 61 69 6c  oc() cannot fail
ab950 20 69 6e 20 73 71 6c 69 74 65 33 54 68 72 65 61   in sqlite3Threa
ab960 64 44 61 74 61 28 29 20 61 73 20 6f 6e 65 20 6f  dData() as one o
ab970 72 20 6d 6f 72 65 20 63 61 6c 6c 73 20 74 6f 20  r more calls to 
ab980 0a 20 20 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 6d  .  ** malloc() m
ab990 75 73 74 20 68 61 76 65 20 61 6c 72 65 61 64 79  ust have already
ab9a0 20 62 65 65 6e 20 6d 61 64 65 20 62 79 20 74 68   been made by th
ab9b0 69 73 20 74 68 72 65 61 64 20 62 65 66 6f 72 65  is thread before
ab9c0 20 69 74 20 67 65 74 73 0a 20 20 2a 2a 20 74 6f   it gets.  ** to
ab9d0 20 74 68 69 73 20 70 6f 69 6e 74 2e 20 54 68 69   this point. Thi
ab9e0 73 20 6d 65 61 6e 73 20 74 68 65 20 54 68 72 65  s means the Thre
ab9f0 61 64 44 61 74 61 20 6d 75 73 74 20 68 61 76 65  adData must have
aba00 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20   been allocated 
aba10 61 6c 72 65 61 64 79 0a 20 20 2a 2a 20 73 6f 20  already.  ** so 
aba20 74 68 61 74 20 54 68 72 65 61 64 44 61 74 61 2e  that ThreadData.
aba30 6e 41 6c 6c 6f 63 20 63 61 6e 20 62 65 20 73 65  nAlloc can be se
aba40 74 2e 0a 20 20 2a 2f 0a 20 20 54 68 72 65 61 64  t..  */.  Thread
aba50 44 61 74 61 20 2a 70 54 73 64 20 3d 20 73 71 6c  Data *pTsd = sql
aba60 69 74 65 33 54 68 72 65 61 64 44 61 74 61 28 29  ite3ThreadData()
aba70 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
aba80 65 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  er );.  assert( 
aba90 70 54 73 64 20 26 26 20 70 54 73 64 2d 3e 6e 41  pTsd && pTsd->nA
abaa0 6c 6c 6f 63 20 29 3b 0a 23 65 6e 64 69 66 0a 0a  lloc );.#endif..
abab0 20 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61    disable_simula
abac0 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b  ted_io_errors();
abad0 0a 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f  .  pPager->errCo
abae0 64 65 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72  de = 0;.  pPager
abaf0 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20  ->exclusiveMode 
abb00 3d 20 30 3b 0a 20 20 70 61 67 65 72 5f 72 65 73  = 0;.  pager_res
abb10 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 70 61  et(pPager);.  pa
abb20 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c  gerUnlockAndRoll
abb30 62 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20  back(pPager);.  
abb40 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64  enable_simulated
abb50 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a 20 20  _io_errors();.  
abb60 50 41 47 45 52 54 52 41 43 45 32 28 22 43 4c 4f  PAGERTRACE2("CLO
abb70 53 45 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49  SE %d\n", PAGERI
abb80 44 28 70 50 61 67 65 72 29 29 3b 0a 20 20 49 4f  D(pPager));.  IO
abb90 54 52 41 43 45 28 28 22 43 4c 4f 53 45 20 25 70  TRACE(("CLOSE %p
abba0 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 20 20  \n", pPager)).  
abbb0 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
abbc0 65 72 72 43 6f 64 65 20 7c 7c 20 28 70 50 61 67  errCode || (pPag
abbd0 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 3d  er->journalOpen=
abbe0 3d 30 20 26 26 20 70 50 61 67 65 72 2d 3e 73 74  =0 && pPager->st
abbf0 6d 74 4f 70 65 6e 3d 3d 30 29 20 29 3b 0a 20 20  mtOpen==0) );.  
abc00 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  if( pPager->jour
abc10 6e 61 6c 4f 70 65 6e 20 29 7b 0a 20 20 20 20 73  nalOpen ){.    s
abc20 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 26 70  qlite3OsClose(&p
abc30 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 7d  Pager->jfd);.  }
abc40 0a 20 20 73 71 6c 69 74 65 46 72 65 65 28 70 50  .  sqliteFree(pP
abc50 61 67 65 72 2d 3e 61 49 6e 4a 6f 75 72 6e 61 6c  ager->aInJournal
abc60 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  );.  if( pPager-
abc70 3e 73 74 6d 74 4f 70 65 6e 20 29 7b 0a 20 20 20  >stmtOpen ){.   
abc80 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28   sqlite3OsClose(
abc90 26 70 50 61 67 65 72 2d 3e 73 74 66 64 29 3b 0a  &pPager->stfd);.
abca0 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 4f 73 43    }.  sqlite3OsC
abcb0 6c 6f 73 65 28 26 70 50 61 67 65 72 2d 3e 66 64  lose(&pPager->fd
abcc0 29 3b 0a 20 20 2f 2a 20 54 65 6d 70 20 66 69 6c  );.  /* Temp fil
abcd0 65 73 20 61 72 65 20 61 75 74 6f 6d 61 74 69 63  es are automatic
abce0 61 6c 6c 79 20 64 65 6c 65 74 65 64 20 62 79 20  ally deleted by 
abcf0 74 68 65 20 4f 53 0a 20 20 2a 2a 20 69 66 28 20  the OS.  ** if( 
abd00 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65  pPager->tempFile
abd10 20 29 7b 0a 20 20 2a 2a 20 20 20 73 71 6c 69 74   ){.  **   sqlit
abd20 65 33 4f 73 44 65 6c 65 74 65 28 70 50 61 67 65  e3OsDelete(pPage
abd30 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  r->zFilename);. 
abd40 20 2a 2a 20 7d 0a 20 20 2a 2f 0a 0a 23 69 66 64   ** }.  */..#ifd
abd50 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
abd60 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45  _MEMORY_MANAGEME
abd70 4e 54 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74  NT.  /* Remove t
abd80 68 65 20 70 61 67 65 72 20 66 72 6f 6d 20 74 68  he pager from th
abd90 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66  e linked list of
abda0 20 70 61 67 65 72 73 20 73 74 61 72 74 69 6e 67   pagers starting
abdb0 20 61 74 20 0a 20 20 2a 2a 20 54 68 72 65 61 64   at .  ** Thread
abdc0 44 61 74 61 2e 70 50 61 67 65 72 20 69 66 20 6d  Data.pPager if m
abdd0 65 6d 6f 72 79 2d 6d 61 6e 61 67 65 6d 65 6e 74  emory-management
abde0 20 69 73 20 65 6e 61 62 6c 65 64 2e 0a 20 20 2a   is enabled..  *
abdf0 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 3d 3d  /.  if( pPager==
abe00 70 54 73 64 2d 3e 70 50 61 67 65 72 20 29 7b 0a  pTsd->pPager ){.
abe10 20 20 20 20 70 54 73 64 2d 3e 70 50 61 67 65 72      pTsd->pPager
abe20 20 3d 20 70 50 61 67 65 72 2d 3e 70 4e 65 78 74   = pPager->pNext
abe30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 50  ;.  }else{.    P
abe40 61 67 65 72 20 2a 70 54 6d 70 3b 0a 20 20 20 20  ager *pTmp;.    
abe50 66 6f 72 28 70 54 6d 70 20 3d 20 70 54 73 64 2d  for(pTmp = pTsd-
abe60 3e 70 50 61 67 65 72 3b 20 70 54 6d 70 2d 3e 70  >pPager; pTmp->p
abe70 4e 65 78 74 21 3d 70 50 61 67 65 72 3b 20 70 54  Next!=pPager; pT
abe80 6d 70 3d 70 54 6d 70 2d 3e 70 4e 65 78 74 29 7b  mp=pTmp->pNext){
abe90 7d 0a 20 20 20 20 70 54 6d 70 2d 3e 70 4e 65 78  }.    pTmp->pNex
abea0 74 20 3d 20 70 50 61 67 65 72 2d 3e 70 4e 65 78  t = pPager->pNex
abeb0 74 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  t;.  }.#endif.  
abec0 73 71 6c 69 74 65 46 72 65 65 28 70 50 61 67 65  sqliteFree(pPage
abed0 72 2d 3e 61 48 61 73 68 29 3b 0a 20 20 73 71 6c  r->aHash);.  sql
abee0 69 74 65 46 72 65 65 28 70 50 61 67 65 72 2d 3e  iteFree(pPager->
abef0 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 73 71  pTmpSpace);.  sq
abf00 6c 69 74 65 46 72 65 65 28 70 50 61 67 65 72 29  liteFree(pPager)
abf10 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
abf20 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 21 64 65  E_OK;.}..#if !de
abf30 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c  fined(NDEBUG) ||
abf40 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
abf50 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 52 65 74 75  TEST)./*.** Retu
abf60 72 6e 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62  rn the page numb
abf70 65 72 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e  er for the given
abf80 20 70 61 67 65 20 64 61 74 61 2e 0a 2a 2f 0a 53   page data..*/.S
abf90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 50 67  QLITE_PRIVATE Pg
abfa0 6e 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 50  no sqlite3PagerP
abfb0 61 67 65 6e 75 6d 62 65 72 28 44 62 50 61 67 65  agenumber(DbPage
abfc0 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70   *p){.  return p
abfd0 2d 3e 70 67 6e 6f 3b 0a 7d 0a 23 65 6e 64 69 66  ->pgno;.}.#endif
abfe0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65  ../*.** The page
abff0 5f 72 65 66 28 29 20 66 75 6e 63 74 69 6f 6e 20  _ref() function 
ac000 69 6e 63 72 65 6d 65 6e 74 73 20 74 68 65 20 72  increments the r
ac010 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 66  eference count f
ac020 6f 72 20 61 20 70 61 67 65 2e 0a 2a 2a 20 49 66  or a page..** If
ac030 20 74 68 65 20 70 61 67 65 20 69 73 20 63 75 72   the page is cur
ac040 72 65 6e 74 6c 79 20 6f 6e 20 74 68 65 20 66 72  rently on the fr
ac050 65 65 6c 69 73 74 20 28 74 68 65 20 72 65 66 65  eelist (the refe
ac060 72 65 6e 63 65 20 63 6f 75 6e 74 20 69 73 20 7a  rence count is z
ac070 65 72 6f 29 20 74 68 65 6e 0a 2a 2a 20 72 65 6d  ero) then.** rem
ac080 6f 76 65 20 69 74 20 66 72 6f 6d 20 74 68 65 20  ove it from the 
ac090 66 72 65 65 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20  freelist..**.** 
ac0a0 46 6f 72 20 6e 6f 6e 2d 74 65 73 74 20 73 79 73  For non-test sys
ac0b0 74 65 6d 73 2c 20 70 61 67 65 5f 72 65 66 28 29  tems, page_ref()
ac0c0 20 69 73 20 61 20 6d 61 63 72 6f 20 74 68 61 74   is a macro that
ac0d0 20 63 61 6c 6c 73 20 5f 70 61 67 65 5f 72 65 66   calls _page_ref
ac0e0 28 29 0a 2a 2a 20 6f 6e 6c 69 6e 65 20 6f 66 20  ().** online of 
ac0f0 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f  the reference co
ac100 75 6e 74 20 69 73 20 7a 65 72 6f 2e 20 20 46 6f  unt is zero.  Fo
ac110 72 20 74 65 73 74 20 73 79 73 74 65 6d 73 2c 20  r test systems, 
ac120 70 61 67 65 5f 72 65 66 28 29 0a 2a 2a 20 69 73  page_ref().** is
ac130 20 61 20 72 65 61 6c 20 66 75 6e 63 74 69 6f 6e   a real function
ac140 20 73 6f 20 74 68 61 74 20 77 65 20 63 61 6e 20   so that we can 
ac150 73 65 74 20 62 72 65 61 6b 70 6f 69 6e 74 73 20  set breakpoints 
ac160 61 6e 64 20 74 72 61 63 65 20 69 74 2e 0a 2a 2f  and trace it..*/
ac170 0a 73 74 61 74 69 63 20 76 6f 69 64 20 5f 70 61  .static void _pa
ac180 67 65 5f 72 65 66 28 50 67 48 64 72 20 2a 70 50  ge_ref(PgHdr *pP
ac190 67 29 7b 0a 20 20 69 66 28 20 70 50 67 2d 3e 6e  g){.  if( pPg->n
ac1a0 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a  Ref==0 ){.    /*
ac1b0 20 54 68 65 20 70 61 67 65 20 69 73 20 63 75 72   The page is cur
ac1c0 72 65 6e 74 6c 79 20 6f 6e 20 74 68 65 20 66 72  rently on the fr
ac1d0 65 65 6c 69 73 74 2e 20 20 52 65 6d 6f 76 65 20  eelist.  Remove 
ac1e0 69 74 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70  it. */.    if( p
ac1f0 50 67 3d 3d 70 50 67 2d 3e 70 50 61 67 65 72 2d  Pg==pPg->pPager-
ac200 3e 70 46 69 72 73 74 53 79 6e 63 65 64 20 29 7b  >pFirstSynced ){
ac210 0a 20 20 20 20 20 20 50 67 48 64 72 20 2a 70 20  .      PgHdr *p 
ac220 3d 20 70 50 67 2d 3e 70 4e 65 78 74 46 72 65 65  = pPg->pNextFree
ac230 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 70  ;.      while( p
ac240 20 26 26 20 70 2d 3e 6e 65 65 64 53 79 6e 63 20   && p->needSync 
ac250 29 7b 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 46  ){ p = p->pNextF
ac260 72 65 65 3b 20 7d 0a 20 20 20 20 20 20 70 50 67  ree; }.      pPg
ac270 2d 3e 70 50 61 67 65 72 2d 3e 70 46 69 72 73 74  ->pPager->pFirst
ac280 53 79 6e 63 65 64 20 3d 20 70 3b 0a 20 20 20 20  Synced = p;.    
ac290 7d 0a 20 20 20 20 69 66 28 20 70 50 67 2d 3e 70  }.    if( pPg->p
ac2a0 50 72 65 76 46 72 65 65 20 29 7b 0a 20 20 20 20  PrevFree ){.    
ac2b0 20 20 70 50 67 2d 3e 70 50 72 65 76 46 72 65 65    pPg->pPrevFree
ac2c0 2d 3e 70 4e 65 78 74 46 72 65 65 20 3d 20 70 50  ->pNextFree = pP
ac2d0 67 2d 3e 70 4e 65 78 74 46 72 65 65 3b 0a 20 20  g->pNextFree;.  
ac2e0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
ac2f0 50 67 2d 3e 70 50 61 67 65 72 2d 3e 70 46 69 72  Pg->pPager->pFir
ac300 73 74 20 3d 20 70 50 67 2d 3e 70 4e 65 78 74 46  st = pPg->pNextF
ac310 72 65 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ree;.    }.    i
ac320 66 28 20 70 50 67 2d 3e 70 4e 65 78 74 46 72 65  f( pPg->pNextFre
ac330 65 20 29 7b 0a 20 20 20 20 20 20 70 50 67 2d 3e  e ){.      pPg->
ac340 70 4e 65 78 74 46 72 65 65 2d 3e 70 50 72 65 76  pNextFree->pPrev
ac350 46 72 65 65 20 3d 20 70 50 67 2d 3e 70 50 72 65  Free = pPg->pPre
ac360 76 46 72 65 65 3b 0a 20 20 20 20 7d 65 6c 73 65  vFree;.    }else
ac370 7b 0a 20 20 20 20 20 20 70 50 67 2d 3e 70 50 61  {.      pPg->pPa
ac380 67 65 72 2d 3e 70 4c 61 73 74 20 3d 20 70 50 67  ger->pLast = pPg
ac390 2d 3e 70 50 72 65 76 46 72 65 65 3b 0a 20 20 20  ->pPrevFree;.   
ac3a0 20 7d 0a 20 20 20 20 70 50 67 2d 3e 70 50 61 67   }.    pPg->pPag
ac3b0 65 72 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 7d 0a  er->nRef++;.  }.
ac3c0 20 20 70 50 67 2d 3e 6e 52 65 66 2b 2b 3b 0a 20    pPg->nRef++;. 
ac3d0 20 52 45 46 49 4e 46 4f 28 70 50 67 29 3b 0a 7d   REFINFO(pPg);.}
ac3e0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
ac3f0 45 42 55 47 0a 20 20 73 74 61 74 69 63 20 76 6f  EBUG.  static vo
ac400 69 64 20 70 61 67 65 5f 72 65 66 28 50 67 48 64  id page_ref(PgHd
ac410 72 20 2a 70 50 67 29 7b 0a 20 20 20 20 69 66 28  r *pPg){.    if(
ac420 20 70 50 67 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b   pPg->nRef==0 ){
ac430 0a 20 20 20 20 20 20 5f 70 61 67 65 5f 72 65 66  .      _page_ref
ac440 28 70 50 67 29 3b 0a 20 20 20 20 7d 65 6c 73 65  (pPg);.    }else
ac450 7b 0a 20 20 20 20 20 20 70 50 67 2d 3e 6e 52 65  {.      pPg->nRe
ac460 66 2b 2b 3b 0a 20 20 20 20 20 20 52 45 46 49 4e  f++;.      REFIN
ac470 46 4f 28 70 50 67 29 3b 0a 20 20 20 20 7d 0a 20  FO(pPg);.    }. 
ac480 20 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e   }.#else.# defin
ac490 65 20 70 61 67 65 5f 72 65 66 28 50 29 20 20 20  e page_ref(P)   
ac4a0 28 28 50 29 2d 3e 6e 52 65 66 3d 3d 30 3f 5f 70  ((P)->nRef==0?_p
ac4b0 61 67 65 5f 72 65 66 28 50 29 3a 28 76 6f 69 64  age_ref(P):(void
ac4c0 29 28 50 29 2d 3e 6e 52 65 66 2b 2b 29 0a 23 65  )(P)->nRef++).#e
ac4d0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72  ndif../*.** Incr
ac4e0 65 6d 65 6e 74 20 74 68 65 20 72 65 66 65 72 65  ement the refere
ac4f0 6e 63 65 20 63 6f 75 6e 74 20 66 6f 72 20 61 20  nce count for a 
ac500 70 61 67 65 2e 20 20 54 68 65 20 69 6e 70 75 74  page.  The input
ac510 20 70 6f 69 6e 74 65 72 20 69 73 0a 2a 2a 20 61   pointer is.** a
ac520 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68   reference to th
ac530 65 20 70 61 67 65 20 64 61 74 61 2e 0a 2a 2f 0a  e page data..*/.
ac540 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
ac550 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 52  nt sqlite3PagerR
ac560 65 66 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b  ef(DbPage *pPg){
ac570 0a 20 20 70 61 67 65 5f 72 65 66 28 70 50 67 29  .  page_ref(pPg)
ac580 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
ac590 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  E_OK;.}../*.** S
ac5a0 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e  ync the journal.
ac5b0 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73    In other words
ac5c0 2c 20 6d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20  , make sure all 
ac5d0 74 68 65 20 70 61 67 65 73 20 74 68 61 74 20 68  the pages that h
ac5e0 61 76 65 0a 2a 2a 20 62 65 65 6e 20 77 72 69 74  ave.** been writ
ac5f0 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e  ten to the journ
ac600 61 6c 20 68 61 76 65 20 61 63 74 75 61 6c 6c 79  al have actually
ac610 20 72 65 61 63 68 65 64 20 74 68 65 20 73 75 72   reached the sur
ac620 66 61 63 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64  face of the.** d
ac630 69 73 6b 2e 20 20 49 74 20 69 73 20 6e 6f 74 20  isk.  It is not 
ac640 73 61 66 65 20 74 6f 20 6d 6f 64 69 66 79 20 74  safe to modify t
ac650 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61  he original data
ac660 62 61 73 65 20 66 69 6c 65 20 75 6e 74 69 6c 20  base file until 
ac670 61 66 74 65 72 0a 2a 2a 20 74 68 65 20 6a 6f 75  after.** the jou
ac680 72 6e 61 6c 20 68 61 73 20 62 65 65 6e 20 73 79  rnal has been sy
ac690 6e 63 65 64 2e 20 20 49 66 20 74 68 65 20 6f 72  nced.  If the or
ac6a0 69 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20  iginal database 
ac6b0 69 73 20 6d 6f 64 69 66 69 65 64 20 62 65 66 6f  is modified befo
ac6c0 72 65 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61  re.** the journa
ac6d0 6c 20 69 73 20 73 79 6e 63 65 64 20 61 6e 64 20  l is synced and 
ac6e0 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20  a power failure 
ac6f0 6f 63 63 75 72 73 2c 20 74 68 65 20 75 6e 73 79  occurs, the unsy
ac700 6e 63 65 64 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20  nced journal.** 
ac710 64 61 74 61 20 77 6f 75 6c 64 20 62 65 20 6c 6f  data would be lo
ac720 73 74 20 61 6e 64 20 77 65 20 77 6f 75 6c 64 20  st and we would 
ac730 62 65 20 75 6e 61 62 6c 65 20 74 6f 20 63 6f 6d  be unable to com
ac740 70 6c 65 74 65 6c 79 20 72 6f 6c 6c 62 61 63 6b  pletely rollback
ac750 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65   the.** database
ac760 20 63 68 61 6e 67 65 73 2e 20 20 44 61 74 61 62   changes.  Datab
ac770 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 77  ase corruption w
ac780 6f 75 6c 64 20 6f 63 63 75 72 2e 0a 2a 2a 20 0a  ould occur..** .
ac790 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
ac7a0 61 6c 73 6f 20 75 70 64 61 74 65 73 20 74 68 65  also updates the
ac7b0 20 6e 52 65 63 20 66 69 65 6c 64 20 69 6e 20 74   nRec field in t
ac7c0 68 65 20 68 65 61 64 65 72 20 6f 66 20 74 68 65  he header of the
ac7d0 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 28 53 65   journal..** (Se
ac7e0 65 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 74 68  e comments on th
ac7f0 65 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b  e pager_playback
ac800 28 29 20 72 6f 75 74 69 6e 65 20 66 6f 72 20 61  () routine for a
ac810 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
ac820 61 74 69 6f 6e 2e 29 0a 2a 2a 20 49 66 20 74 68  ation.).** If th
ac830 65 20 73 79 6e 63 20 6d 6f 64 65 20 69 73 20 46  e sync mode is F
ac840 55 4c 4c 2c 20 74 77 6f 20 73 79 6e 63 73 20 77  ULL, two syncs w
ac850 69 6c 6c 20 6f 63 63 75 72 2e 20 20 46 69 72 73  ill occur.  Firs
ac860 74 20 74 68 65 20 77 68 6f 6c 65 20 6a 6f 75 72  t the whole jour
ac870 6e 61 6c 0a 2a 2a 20 69 73 20 73 79 6e 63 65 64  nal.** is synced
ac880 2c 20 74 68 65 6e 20 74 68 65 20 6e 52 65 63 20  , then the nRec 
ac890 66 69 65 6c 64 20 69 73 20 75 70 64 61 74 65 64  field is updated
ac8a0 2c 20 74 68 65 6e 20 61 20 73 65 63 6f 6e 64 20  , then a second 
ac8b0 73 79 6e 63 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a  sync occurs..**.
ac8c0 2a 2a 20 46 6f 72 20 74 65 6d 70 6f 72 61 72 79  ** For temporary
ac8d0 20 64 61 74 61 62 61 73 65 73 2c 20 77 65 20 64   databases, we d
ac8e0 6f 20 6e 6f 74 20 63 61 72 65 20 69 66 20 77 65  o not care if we
ac8f0 20 61 72 65 20 61 62 6c 65 20 74 6f 20 72 6f 6c   are able to rol
ac900 6c 62 61 63 6b 0a 2a 2a 20 61 66 74 65 72 20 61  lback.** after a
ac910 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 2c 20   power failure, 
ac920 73 6f 20 73 79 6e 63 20 6f 63 63 75 72 73 2e 0a  so sync occurs..
ac930 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
ac940 6e 65 20 63 6c 65 61 72 73 20 74 68 65 20 6e 65  ne clears the ne
ac950 65 64 53 79 6e 63 20 66 69 65 6c 64 20 6f 66 20  edSync field of 
ac960 65 76 65 72 79 20 70 61 67 65 20 63 75 72 72 65  every page curre
ac970 6e 74 20 68 65 6c 64 20 69 6e 0a 2a 2a 20 6d 65  nt held in.** me
ac980 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  mory..*/.static 
ac990 69 6e 74 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28  int syncJournal(
ac9a0 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
ac9b0 20 20 50 67 48 64 72 20 2a 70 50 67 3b 0a 20 20    PgHdr *pPg;.  
ac9c0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
ac9d0 4f 4b 3b 0a 0a 20 20 2f 2a 20 53 79 6e 63 20 74  OK;..  /* Sync t
ac9e0 68 65 20 6a 6f 75 72 6e 61 6c 20 62 65 66 6f 72  he journal befor
ac9f0 65 20 6d 6f 64 69 66 79 69 6e 67 20 74 68 65 20  e modifying the 
aca00 6d 61 69 6e 20 64 61 74 61 62 61 73 65 0a 20 20  main database.  
aca10 2a 2a 20 28 61 73 73 75 6d 69 6e 67 20 74 68 65  ** (assuming the
aca20 72 65 20 69 73 20 61 20 6a 6f 75 72 6e 61 6c 20  re is a journal 
aca30 61 6e 64 20 69 74 20 6e 65 65 64 73 20 74 6f 20  and it needs to 
aca40 62 65 20 73 79 6e 63 65 64 2e 29 0a 20 20 2a 2f  be synced.).  */
aca50 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6e  .  if( pPager->n
aca60 65 65 64 53 79 6e 63 20 29 7b 0a 20 20 20 20 69  eedSync ){.    i
aca70 66 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70  f( !pPager->temp
aca80 46 69 6c 65 20 29 7b 0a 20 20 20 20 20 20 61 73  File ){.      as
aca90 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f  sert( pPager->jo
acaa0 75 72 6e 61 6c 4f 70 65 6e 20 29 3b 0a 20 20 20  urnalOpen );.   
acab0 20 20 20 2f 2a 20 61 73 73 65 72 74 28 20 21 70     /* assert( !p
acac0 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 3b  Pager->noSync );
acad0 20 2f 2f 20 6e 6f 53 79 6e 63 20 6d 69 67 68 74   // noSync might
acae0 20 62 65 20 73 65 74 20 69 66 20 73 79 6e 63 68   be set if synch
acaf0 72 6f 6e 6f 75 73 0a 20 20 20 20 20 20 2a 2a 20  ronous.      ** 
acb00 77 61 73 20 74 75 72 6e 65 64 20 6f 66 66 20 61  was turned off a
acb10 66 74 65 72 20 74 68 65 20 74 72 61 6e 73 61 63  fter the transac
acb20 74 69 6f 6e 20 77 61 73 20 73 74 61 72 74 65 64  tion was started
acb30 2e 20 20 54 69 63 6b 65 74 20 23 36 31 35 20 2a  .  Ticket #615 *
acb40 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  /.#ifndef NDEBUG
acb50 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20  .      {.       
acb60 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68   /* Make sure th
acb70 65 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 63  e pPager->nRec c
acb80 6f 75 6e 74 65 72 20 77 65 20 61 72 65 20 6b 65  ounter we are ke
acb90 65 70 69 6e 67 20 61 67 72 65 65 73 0a 20 20 20  eping agrees.   
acba0 20 20 20 20 20 2a 2a 20 77 69 74 68 20 74 68 65       ** with the
acbb0 20 6e 52 65 63 20 63 6f 6d 70 75 74 65 64 20 66   nRec computed f
acbc0 72 6f 6d 20 74 68 65 20 73 69 7a 65 20 6f 66 20  rom the size of 
acbd0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
acbe0 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
acbf0 20 20 20 20 20 69 36 34 20 6a 53 7a 3b 0a 20 20       i64 jSz;.  
acc00 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
acc10 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61  e3OsFileSize(pPa
acc20 67 65 72 2d 3e 6a 66 64 2c 20 26 6a 53 7a 29 3b  ger->jfd, &jSz);
acc30 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
acc40 3d 30 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  =0 ) return rc;.
acc50 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
acc60 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
acc70 66 66 3d 3d 6a 53 7a 20 29 3b 0a 20 20 20 20 20  ff==jSz );.     
acc80 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20   }.#endif.      
acc90 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69  {.        /* Wri
acca0 74 65 20 74 68 65 20 6e 52 65 63 20 76 61 6c 75  te the nRec valu
accb0 65 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e  e into the journ
accc0 61 6c 20 66 69 6c 65 20 68 65 61 64 65 72 2e 20  al file header. 
accd0 49 66 20 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a  If in.        **
acce0 20 66 75 6c 6c 2d 73 79 6e 63 68 72 6f 6e 6f 75   full-synchronou
accf0 73 20 6d 6f 64 65 2c 20 73 79 6e 63 20 74 68 65  s mode, sync the
acd00 20 6a 6f 75 72 6e 61 6c 20 66 69 72 73 74 2e 20   journal first. 
acd10 54 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 61  This ensures tha
acd20 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 6c  t.        ** all
acd30 20 64 61 74 61 20 68 61 73 20 72 65 61 6c 6c 79   data has really
acd40 20 68 69 74 20 74 68 65 20 64 69 73 6b 20 62 65   hit the disk be
acd50 66 6f 72 65 20 6e 52 65 63 20 69 73 20 75 70 64  fore nRec is upd
acd60 61 74 65 64 20 74 6f 20 6d 61 72 6b 0a 20 20 20  ated to mark.   
acd70 20 20 20 20 20 2a 2a 20 69 74 20 61 73 20 61 20       ** it as a 
acd80 63 61 6e 64 69 64 61 74 65 20 66 6f 72 20 72 6f  candidate for ro
acd90 6c 6c 62 61 63 6b 2e 20 0a 20 20 20 20 20 20 20  llback. .       
acda0 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
acdb0 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63  pPager->fullSync
acdc0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 50 41   ){.          PA
acdd0 47 45 52 54 52 41 43 45 32 28 22 53 59 4e 43 20  GERTRACE2("SYNC 
acde0 6a 6f 75 72 6e 61 6c 20 6f 66 20 25 64 5c 6e 22  journal of %d\n"
acdf0 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72  , PAGERID(pPager
ace00 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 49 4f  ));.          IO
ace10 54 52 41 43 45 28 28 22 4a 53 59 4e 43 20 25 70  TRACE(("JSYNC %p
ace20 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 20 20  \n", pPager)).  
ace30 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
ace40 69 74 65 33 4f 73 53 79 6e 63 28 70 50 61 67 65  ite3OsSync(pPage
ace50 72 2d 3e 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20  r->jfd, 0);.    
ace60 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 30 20        if( rc!=0 
ace70 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
ace80 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72       }.        r
ace90 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 65 65  c = sqlite3OsSee
acea0 6b 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 0a 20  k(pPager->jfd,. 
aceb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acec0 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72            pPager
aced0 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 2b 20 73  ->journalHdr + s
acee0 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61  izeof(aJournalMa
acef0 67 69 63 29 29 3b 0a 20 20 20 20 20 20 20 20 69  gic));.        i
acf00 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
acf10 63 3b 0a 20 20 20 20 20 20 20 20 49 4f 54 52 41  c;.        IOTRA
acf20 43 45 28 28 22 4a 48 44 52 20 25 70 20 25 6c 6c  CE(("JHDR %p %ll
acf30 64 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c  d %d\n", pPager,
acf40 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
acf50 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e     pPager->journ
acf60 61 6c 48 64 72 20 2b 20 73 69 7a 65 6f 66 28 61  alHdr + sizeof(a
acf70 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2c 20 34  JournalMagic), 4
acf80 29 29 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  )).        rc = 
acf90 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61 67  write32bits(pPag
acfa0 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d  er->jfd, pPager-
acfb0 3e 6e 52 65 63 29 3b 0a 20 20 20 20 20 20 20 20  >nRec);.        
acfc0 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
acfd0 72 63 3b 0a 0a 20 20 20 20 20 20 20 20 72 63 20  rc;..        rc 
acfe0 3d 20 73 71 6c 69 74 65 33 4f 73 53 65 65 6b 28  = sqlite3OsSeek(
acff0 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61  pPager->jfd, pPa
ad000 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29  ger->journalOff)
ad010 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
ad020 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
ad030 20 20 20 20 7d 0a 20 20 20 20 20 20 50 41 47 45      }.      PAGE
ad040 52 54 52 41 43 45 32 28 22 53 59 4e 43 20 6a 6f  RTRACE2("SYNC jo
ad050 75 72 6e 61 6c 20 6f 66 20 25 64 5c 6e 22 2c 20  urnal of %d\n", 
ad060 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 29  PAGERID(pPager))
ad070 3b 0a 20 20 20 20 20 20 49 4f 54 52 41 43 45 28  ;.      IOTRACE(
ad080 28 22 4a 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70  ("JSYNC %p\n", p
ad090 50 61 67 65 72 29 29 0a 20 20 20 20 20 20 72 63  Pager)).      rc
ad0a0 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63   = sqlite3OsSync
ad0b0 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50  (pPager->jfd, pP
ad0c0 61 67 65 72 2d 3e 66 75 6c 6c 5f 66 73 79 6e 63  ager->full_fsync
ad0d0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
ad0e0 3d 30 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  =0 ) return rc;.
ad0f0 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f        pPager->jo
ad100 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20 31  urnalStarted = 1
ad110 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67  ;.    }.    pPag
ad120 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30  er->needSync = 0
ad130 3b 0a 0a 20 20 20 20 2f 2a 20 45 72 61 73 65 20  ;..    /* Erase 
ad140 74 68 65 20 6e 65 65 64 53 79 6e 63 20 66 6c 61  the needSync fla
ad150 67 20 66 72 6f 6d 20 65 76 65 72 79 20 70 61 67  g from every pag
ad160 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f  e..    */.    fo
ad170 72 28 70 50 67 3d 70 50 61 67 65 72 2d 3e 70 41  r(pPg=pPager->pA
ad180 6c 6c 3b 20 70 50 67 3b 20 70 50 67 3d 70 50 67  ll; pPg; pPg=pPg
ad190 2d 3e 70 4e 65 78 74 41 6c 6c 29 7b 0a 20 20 20  ->pNextAll){.   
ad1a0 20 20 20 70 50 67 2d 3e 6e 65 65 64 53 79 6e 63     pPg->needSync
ad1b0 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
ad1c0 70 50 61 67 65 72 2d 3e 70 46 69 72 73 74 53 79  pPager->pFirstSy
ad1d0 6e 63 65 64 20 3d 20 70 50 61 67 65 72 2d 3e 70  nced = pPager->p
ad1e0 46 69 72 73 74 3b 0a 20 20 7d 0a 0a 23 69 66 6e  First;.  }..#ifn
ad1f0 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a 20  def NDEBUG.  /* 
ad200 49 66 20 74 68 65 20 50 61 67 65 72 2e 6e 65 65  If the Pager.nee
ad210 64 53 79 6e 63 20 66 6c 61 67 20 69 73 20 63 6c  dSync flag is cl
ad220 65 61 72 20 74 68 65 6e 20 74 68 65 20 50 67 48  ear then the PgH
ad230 64 72 2e 6e 65 65 64 53 79 6e 63 0a 20 20 2a 2a  dr.needSync.  **
ad240 20 66 6c 61 67 20 6d 75 73 74 20 61 6c 73 6f 20   flag must also 
ad250 62 65 20 63 6c 65 61 72 20 66 6f 72 20 61 6c 6c  be clear for all
ad260 20 70 61 67 65 73 2e 20 20 56 65 72 69 66 79 20   pages.  Verify 
ad270 74 68 61 74 20 74 68 69 73 0a 20 20 2a 2a 20 69  that this.  ** i
ad280 6e 76 61 72 69 61 6e 74 20 69 73 20 74 72 75 65  nvariant is true
ad290 2e 0a 20 20 2a 2f 0a 20 20 65 6c 73 65 7b 0a 20  ..  */.  else{. 
ad2a0 20 20 20 66 6f 72 28 70 50 67 3d 70 50 61 67 65     for(pPg=pPage
ad2b0 72 2d 3e 70 41 6c 6c 3b 20 70 50 67 3b 20 70 50  r->pAll; pPg; pP
ad2c0 67 3d 70 50 67 2d 3e 70 4e 65 78 74 41 6c 6c 29  g=pPg->pNextAll)
ad2d0 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
ad2e0 70 50 67 2d 3e 6e 65 65 64 53 79 6e 63 3d 3d 30  pPg->needSync==0
ad2f0 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73   );.    }.    as
ad300 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 46  sert( pPager->pF
ad310 69 72 73 74 53 79 6e 63 65 64 3d 3d 70 50 61 67  irstSynced==pPag
ad320 65 72 2d 3e 70 46 69 72 73 74 20 29 3b 0a 20 20  er->pFirst );.  
ad330 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75  }.#endif..  retu
ad340 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
ad350 4d 65 72 67 65 20 74 77 6f 20 6c 69 73 74 73 20  Merge two lists 
ad360 6f 66 20 70 61 67 65 73 20 63 6f 6e 6e 65 63 74  of pages connect
ad370 65 64 20 62 79 20 70 44 69 72 74 79 20 61 6e 64  ed by pDirty and
ad380 20 69 6e 20 70 67 6e 6f 20 6f 72 64 65 72 2e 0a   in pgno order..
ad390 2a 2a 20 44 6f 20 6e 6f 74 20 62 6f 74 68 20 66  ** Do not both f
ad3a0 69 78 69 6e 67 20 74 68 65 20 70 50 72 65 76 44  ixing the pPrevD
ad3b0 69 72 74 79 20 70 6f 69 6e 74 65 72 73 2e 0a 2a  irty pointers..*
ad3c0 2f 0a 73 74 61 74 69 63 20 50 67 48 64 72 20 2a  /.static PgHdr *
ad3d0 6d 65 72 67 65 5f 70 61 67 65 6c 69 73 74 28 50  merge_pagelist(P
ad3e0 67 48 64 72 20 2a 70 41 2c 20 50 67 48 64 72 20  gHdr *pA, PgHdr 
ad3f0 2a 70 42 29 7b 0a 20 20 50 67 48 64 72 20 72 65  *pB){.  PgHdr re
ad400 73 75 6c 74 2c 20 2a 70 54 61 69 6c 3b 0a 20 20  sult, *pTail;.  
ad410 70 54 61 69 6c 20 3d 20 26 72 65 73 75 6c 74 3b  pTail = &result;
ad420 0a 20 20 77 68 69 6c 65 28 20 70 41 20 26 26 20  .  while( pA && 
ad430 70 42 20 29 7b 0a 20 20 20 20 69 66 28 20 70 41  pB ){.    if( pA
ad440 2d 3e 70 67 6e 6f 3c 70 42 2d 3e 70 67 6e 6f 20  ->pgno<pB->pgno 
ad450 29 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e  ){.      pTail->
ad460 70 44 69 72 74 79 20 3d 20 70 41 3b 0a 20 20 20  pDirty = pA;.   
ad470 20 20 20 70 54 61 69 6c 20 3d 20 70 41 3b 0a 20     pTail = pA;. 
ad480 20 20 20 20 20 70 41 20 3d 20 70 41 2d 3e 70 44       pA = pA->pD
ad490 69 72 74 79 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  irty;.    }else{
ad4a0 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70 44  .      pTail->pD
ad4b0 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 20 20 20  irty = pB;.     
ad4c0 20 70 54 61 69 6c 20 3d 20 70 42 3b 0a 20 20 20   pTail = pB;.   
ad4d0 20 20 20 70 42 20 3d 20 70 42 2d 3e 70 44 69 72     pB = pB->pDir
ad4e0 74 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ty;.    }.  }.  
ad4f0 69 66 28 20 70 41 20 29 7b 0a 20 20 20 20 70 54  if( pA ){.    pT
ad500 61 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 41  ail->pDirty = pA
ad510 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 42  ;.  }else if( pB
ad520 20 29 7b 0a 20 20 20 20 70 54 61 69 6c 2d 3e 70   ){.    pTail->p
ad530 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 7d 65  Dirty = pB;.  }e
ad540 6c 73 65 7b 0a 20 20 20 20 70 54 61 69 6c 2d 3e  lse{.    pTail->
ad550 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 7d 0a  pDirty = 0;.  }.
ad560 20 20 72 65 74 75 72 6e 20 72 65 73 75 6c 74 2e    return result.
ad570 70 44 69 72 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pDirty;.}../*.**
ad580 20 53 6f 72 74 20 74 68 65 20 6c 69 73 74 20 6f   Sort the list o
ad590 66 20 70 61 67 65 73 20 69 6e 20 61 63 63 65 6e  f pages in accen
ad5a0 64 69 6e 67 20 6f 72 64 65 72 20 62 79 20 70 67  ding order by pg
ad5b0 6e 6f 2e 20 20 50 61 67 65 73 20 61 72 65 0a 2a  no.  Pages are.*
ad5c0 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 70  * connected by p
ad5d0 44 69 72 74 79 20 70 6f 69 6e 74 65 72 73 2e 20  Dirty pointers. 
ad5e0 20 54 68 65 20 70 50 72 65 76 44 69 72 74 79 20   The pPrevDirty 
ad5f0 70 6f 69 6e 74 65 72 73 20 61 72 65 0a 2a 2a 20  pointers are.** 
ad600 63 6f 72 72 75 70 74 65 64 20 62 79 20 74 68 69  corrupted by thi
ad610 73 20 73 6f 72 74 2e 0a 2a 2f 0a 23 64 65 66 69  s sort..*/.#defi
ad620 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54  ne N_SORT_BUCKET
ad630 5f 41 4c 4c 4f 43 20 32 35 0a 23 64 65 66 69 6e  _ALLOC 25.#defin
ad640 65 20 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 20  e N_SORT_BUCKET 
ad650 20 20 20 20 20 20 32 35 0a 23 69 66 64 65 66 20        25.#ifdef 
ad660 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 6e  SQLITE_TEST.  in
ad670 74 20 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f  t sqlite3_pager_
ad680 6e 5f 73 6f 72 74 5f 62 75 63 6b 65 74 20 3d 20  n_sort_bucket = 
ad690 30 3b 0a 20 20 23 75 6e 64 65 66 20 4e 5f 53 4f  0;.  #undef N_SO
ad6a0 52 54 5f 42 55 43 4b 45 54 0a 20 20 23 64 65 66  RT_BUCKET.  #def
ad6b0 69 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 43 4b 45  ine N_SORT_BUCKE
ad6c0 54 20 5c 0a 20 20 20 28 73 71 6c 69 74 65 33 5f  T \.   (sqlite3_
ad6d0 70 61 67 65 72 5f 6e 5f 73 6f 72 74 5f 62 75 63  pager_n_sort_buc
ad6e0 6b 65 74 3f 73 71 6c 69 74 65 33 5f 70 61 67 65  ket?sqlite3_page
ad6f0 72 5f 6e 5f 73 6f 72 74 5f 62 75 63 6b 65 74 3a  r_n_sort_bucket:
ad700 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 5f 41 4c  N_SORT_BUCKET_AL
ad710 4c 4f 43 29 0a 23 65 6e 64 69 66 0a 73 74 61 74  LOC).#endif.stat
ad720 69 63 20 50 67 48 64 72 20 2a 73 6f 72 74 5f 70  ic PgHdr *sort_p
ad730 61 67 65 6c 69 73 74 28 50 67 48 64 72 20 2a 70  agelist(PgHdr *p
ad740 49 6e 29 7b 0a 20 20 50 67 48 64 72 20 2a 61 5b  In){.  PgHdr *a[
ad750 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 5f 41 4c  N_SORT_BUCKET_AL
ad760 4c 4f 43 5d 2c 20 2a 70 3b 0a 20 20 69 6e 74 20  LOC], *p;.  int 
ad770 69 3b 0a 20 20 6d 65 6d 73 65 74 28 61 2c 20 30  i;.  memset(a, 0
ad780 2c 20 73 69 7a 65 6f 66 28 61 29 29 3b 0a 20 20  , sizeof(a));.  
ad790 77 68 69 6c 65 28 20 70 49 6e 20 29 7b 0a 20 20  while( pIn ){.  
ad7a0 20 20 70 20 3d 20 70 49 6e 3b 0a 20 20 20 20 70    p = pIn;.    p
ad7b0 49 6e 20 3d 20 70 2d 3e 70 44 69 72 74 79 3b 0a  In = p->pDirty;.
ad7c0 20 20 20 20 70 2d 3e 70 44 69 72 74 79 20 3d 20      p->pDirty = 
ad7d0 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  0;.    for(i=0; 
ad7e0 69 3c 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 2d  i<N_SORT_BUCKET-
ad7f0 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  1; i++){.      i
ad800 66 28 20 61 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20  f( a[i]==0 ){.  
ad810 20 20 20 20 20 20 61 5b 69 5d 20 3d 20 70 3b 0a        a[i] = p;.
ad820 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
ad830 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
ad840 20 20 20 20 70 20 3d 20 6d 65 72 67 65 5f 70 61      p = merge_pa
ad850 67 65 6c 69 73 74 28 61 5b 69 5d 2c 20 70 29 3b  gelist(a[i], p);
ad860 0a 20 20 20 20 20 20 20 20 61 5b 69 5d 20 3d 20  .        a[i] = 
ad870 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  0;.      }.    }
ad880 0a 20 20 20 20 69 66 28 20 69 3d 3d 4e 5f 53 4f  .    if( i==N_SO
ad890 52 54 5f 42 55 43 4b 45 54 2d 31 20 29 7b 0a 20  RT_BUCKET-1 ){. 
ad8a0 20 20 20 20 20 2f 2a 20 43 6f 76 65 72 61 67 65       /* Coverage
ad8b0 3a 20 54 6f 20 67 65 74 20 68 65 72 65 2c 20 74  : To get here, t
ad8c0 68 65 72 65 20 6e 65 65 64 20 74 6f 20 62 65 20  here need to be 
ad8d0 32 5e 28 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54  2^(N_SORT_BUCKET
ad8e0 29 20 0a 20 20 20 20 20 20 2a 2a 20 65 6c 65 6d  ) .      ** elem
ad8f0 65 6e 74 73 20 69 6e 20 74 68 65 20 69 6e 70 75  ents in the inpu
ad900 74 20 6c 69 73 74 2e 20 54 68 69 73 20 69 73 20  t list. This is 
ad910 70 6f 73 73 69 62 6c 65 2c 20 62 75 74 20 69 6d  possible, but im
ad920 70 72 61 63 74 69 63 61 6c 2e 0a 20 20 20 20 20  practical..     
ad930 20 2a 2a 20 54 65 73 74 69 6e 67 20 74 68 69 73   ** Testing this
ad940 20 6c 69 6e 65 20 69 73 20 74 68 65 20 70 6f 69   line is the poi
ad950 6e 74 20 6f 66 20 67 6c 6f 62 61 6c 20 76 61 72  nt of global var
ad960 69 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20 73  iable.      ** s
ad970 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 6e 5f 73  qlite3_pager_n_s
ad980 6f 72 74 5f 62 75 63 6b 65 74 2e 0a 20 20 20 20  ort_bucket..    
ad990 20 20 2a 2f 0a 20 20 20 20 20 20 61 5b 69 5d 20    */.      a[i] 
ad9a0 3d 20 6d 65 72 67 65 5f 70 61 67 65 6c 69 73 74  = merge_pagelist
ad9b0 28 61 5b 69 5d 2c 20 70 29 3b 0a 20 20 20 20 7d  (a[i], p);.    }
ad9c0 0a 20 20 7d 0a 20 20 70 20 3d 20 61 5b 30 5d 3b  .  }.  p = a[0];
ad9d0 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 4e 5f  .  for(i=1; i<N_
ad9e0 53 4f 52 54 5f 42 55 43 4b 45 54 3b 20 69 2b 2b  SORT_BUCKET; i++
ad9f0 29 7b 0a 20 20 20 20 70 20 3d 20 6d 65 72 67 65  ){.    p = merge
ada00 5f 70 61 67 65 6c 69 73 74 28 70 2c 20 61 5b 69  _pagelist(p, a[i
ada10 5d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ]);.  }.  return
ada20 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76   p;.}../*.** Giv
ada30 65 6e 20 61 20 6c 69 73 74 20 6f 66 20 70 61 67  en a list of pag
ada40 65 73 20 28 63 6f 6e 6e 65 63 74 65 64 20 62 79  es (connected by
ada50 20 74 68 65 20 50 67 48 64 72 2e 70 44 69 72 74   the PgHdr.pDirt
ada60 79 20 70 6f 69 6e 74 65 72 29 20 77 72 69 74 65  y pointer) write
ada70 0a 2a 2a 20 65 76 65 72 79 20 6f 6e 65 20 6f 66  .** every one of
ada80 20 74 68 6f 73 65 20 70 61 67 65 73 20 6f 75 74   those pages out
ada90 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
adaa0 20 66 69 6c 65 20 61 6e 64 20 6d 61 72 6b 20 74   file and mark t
adab0 68 65 6d 20 61 6c 6c 0a 2a 2a 20 61 73 20 63 6c  hem all.** as cl
adac0 65 61 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ean..*/.static i
adad0 6e 74 20 70 61 67 65 72 5f 77 72 69 74 65 5f 70  nt pager_write_p
adae0 61 67 65 6c 69 73 74 28 50 67 48 64 72 20 2a 70  agelist(PgHdr *p
adaf0 4c 69 73 74 29 7b 0a 20 20 50 61 67 65 72 20 2a  List){.  Pager *
adb00 70 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 72 63  pPager;.  int rc
adb10 3b 0a 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d  ;..  if( pList==
adb20 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  0 ) return SQLIT
adb30 45 5f 4f 4b 3b 0a 20 20 70 50 61 67 65 72 20 3d  E_OK;.  pPager =
adb40 20 70 4c 69 73 74 2d 3e 70 50 61 67 65 72 3b 0a   pList->pPager;.
adb50 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f  .  /* At this po
adb60 69 6e 74 20 74 68 65 72 65 20 6d 61 79 20 62 65  int there may be
adb70 20 65 69 74 68 65 72 20 61 20 52 45 53 45 52 56   either a RESERV
adb80 45 44 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20  ED or EXCLUSIVE 
adb90 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20 2a 2a  lock on the.  **
adba0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
adbb0 49 66 20 74 68 65 72 65 20 69 73 20 61 6c 72 65  If there is alre
adbc0 61 64 79 20 61 6e 20 45 58 43 4c 55 53 49 56 45  ady an EXCLUSIVE
adbd0 20 6c 6f 63 6b 2c 20 74 68 65 20 66 6f 6c 6c 6f   lock, the follo
adbe0 77 69 6e 67 0a 20 20 2a 2a 20 63 61 6c 6c 73 20  wing.  ** calls 
adbf0 74 6f 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b  to sqlite3OsLock
adc00 28 29 20 61 72 65 20 6e 6f 2d 6f 70 73 2e 0a 20  () are no-ops.. 
adc10 20 2a 2a 0a 20 20 2a 2a 20 4d 6f 76 69 6e 67 20   **.  ** Moving 
adc20 74 68 65 20 6c 6f 63 6b 20 66 72 6f 6d 20 52 45  the lock from RE
adc30 53 45 52 56 45 44 20 74 6f 20 45 58 43 4c 55 53  SERVED to EXCLUS
adc40 49 56 45 20 61 63 74 75 61 6c 6c 79 20 69 6e 76  IVE actually inv
adc50 6f 6c 76 65 73 20 67 6f 69 6e 67 0a 20 20 2a 2a  olves going.  **
adc60 20 74 68 72 6f 75 67 68 20 61 6e 20 69 6e 74 65   through an inte
adc70 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 20 50  rmediate state P
adc80 45 4e 44 49 4e 47 2e 20 20 20 41 20 50 45 4e 44  ENDING.   A PEND
adc90 49 4e 47 20 6c 6f 63 6b 20 70 72 65 76 65 6e 74  ING lock prevent
adca0 73 20 6e 65 77 0a 20 20 2a 2a 20 72 65 61 64 65  s new.  ** reade
adcb0 72 73 20 66 72 6f 6d 20 61 74 74 61 63 68 69 6e  rs from attachin
adcc0 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  g to the databas
adcd0 65 20 62 75 74 20 69 73 20 75 6e 73 75 66 66 69  e but is unsuffi
adce0 63 69 65 6e 74 20 66 6f 72 20 75 73 20 74 6f 0a  cient for us to.
adcf0 20 20 2a 2a 20 77 72 69 74 65 2e 20 20 54 68 65    ** write.  The
add00 20 69 64 65 61 20 6f 66 20 61 20 50 45 4e 44 49   idea of a PENDI
add10 4e 47 20 6c 6f 63 6b 20 69 73 20 74 6f 20 70 72  NG lock is to pr
add20 65 76 65 6e 74 20 6e 65 77 20 72 65 61 64 65 72  event new reader
add30 73 20 66 72 6f 6d 0a 20 20 2a 2a 20 63 6f 6d 69  s from.  ** comi
add40 6e 67 20 69 6e 20 77 68 69 6c 65 20 77 65 20 77  ng in while we w
add50 61 69 74 20 66 6f 72 20 65 78 69 73 74 69 6e 67  ait for existing
add60 20 72 65 61 64 65 72 73 20 74 6f 20 63 6c 65 61   readers to clea
add70 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 69  r..  **.  ** Whi
add80 6c 65 20 74 68 65 20 70 61 67 65 72 20 69 73 20  le the pager is 
add90 69 6e 20 74 68 65 20 52 45 53 45 52 56 45 44 20  in the RESERVED 
adda0 73 74 61 74 65 2c 20 74 68 65 20 6f 72 69 67 69  state, the origi
addb0 6e 61 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c  nal database fil
addc0 65 0a 20 20 2a 2a 20 69 73 20 75 6e 63 68 61 6e  e.  ** is unchan
addd0 67 65 64 20 61 6e 64 20 77 65 20 63 61 6e 20 72  ged and we can r
adde0 6f 6c 6c 62 61 63 6b 20 77 69 74 68 6f 75 74 20  ollback without 
addf0 68 61 76 69 6e 67 20 74 6f 20 70 6c 61 79 62 61  having to playba
ade00 63 6b 20 74 68 65 0a 20 20 2a 2a 20 6a 6f 75 72  ck the.  ** jour
ade10 6e 61 6c 20 69 6e 74 6f 20 74 68 65 20 6f 72 69  nal into the ori
ade20 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20 66  ginal database f
ade30 69 6c 65 2e 20 20 4f 6e 63 65 20 77 65 20 74 72  ile.  Once we tr
ade40 61 6e 73 69 74 69 6f 6e 20 74 6f 0a 20 20 2a 2a  ansition to.  **
ade50 20 45 58 43 4c 55 53 49 56 45 2c 20 69 74 20 6d   EXCLUSIVE, it m
ade60 65 61 6e 73 20 74 68 65 20 64 61 74 61 62 61 73  eans the databas
ade70 65 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20  e file has been 
ade80 63 68 61 6e 67 65 64 20 61 6e 64 20 61 6e 79 20  changed and any 
ade90 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 77 69  rollback.  ** wi
adea0 6c 6c 20 72 65 71 75 69 72 65 20 61 20 6a 6f 75  ll require a jou
adeb0 72 6e 61 6c 20 70 6c 61 79 62 61 63 6b 2e 0a 20  rnal playback.. 
adec0 20 2a 2f 0a 20 20 72 63 20 3d 20 70 61 67 65 72   */.  rc = pager
aded0 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50  _wait_on_lock(pP
adee0 61 67 65 72 2c 20 45 58 43 4c 55 53 49 56 45 5f  ager, EXCLUSIVE_
adef0 4c 4f 43 4b 29 3b 0a 20 20 69 66 28 20 72 63 21  LOCK);.  if( rc!
adf00 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
adf10 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
adf20 0a 0a 20 20 70 4c 69 73 74 20 3d 20 73 6f 72 74  ..  pList = sort
adf30 5f 70 61 67 65 6c 69 73 74 28 70 4c 69 73 74 29  _pagelist(pList)
adf40 3b 0a 20 20 77 68 69 6c 65 28 20 70 4c 69 73 74  ;.  while( pList
adf50 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
adf60 70 4c 69 73 74 2d 3e 64 69 72 74 79 20 29 3b 0a  pList->dirty );.
adf70 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
adf80 4f 73 53 65 65 6b 28 70 50 61 67 65 72 2d 3e 66  OsSeek(pPager->f
adf90 64 2c 20 28 70 4c 69 73 74 2d 3e 70 67 6e 6f 2d  d, (pList->pgno-
adfa0 31 29 2a 28 69 36 34 29 70 50 61 67 65 72 2d 3e  1)*(i64)pPager->
adfb0 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 69  pageSize);.    i
adfc0 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
adfd0 63 3b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  c;.    /* If the
adfe0 72 65 20 61 72 65 20 64 69 72 74 79 20 70 61 67  re are dirty pag
adff0 65 73 20 69 6e 20 74 68 65 20 70 61 67 65 20 63  es in the page c
ae000 61 63 68 65 20 77 69 74 68 20 70 61 67 65 20 6e  ache with page n
ae010 75 6d 62 65 72 73 20 67 72 65 61 74 65 72 0a 20  umbers greater. 
ae020 20 20 20 2a 2a 20 74 68 61 6e 20 50 61 67 65 72     ** than Pager
ae030 2e 64 62 53 69 7a 65 2c 20 74 68 69 73 20 6d 65  .dbSize, this me
ae040 61 6e 73 20 73 71 6c 69 74 65 33 50 61 67 65 72  ans sqlite3Pager
ae050 54 72 75 6e 63 61 74 65 28 29 20 77 61 73 20 63  Truncate() was c
ae060 61 6c 6c 65 64 20 74 6f 0a 20 20 20 20 2a 2a 20  alled to.    ** 
ae070 6d 61 6b 65 20 74 68 65 20 66 69 6c 65 20 73 6d  make the file sm
ae080 61 6c 6c 65 72 20 28 70 72 65 73 75 6d 61 62 6c  aller (presumabl
ae090 79 20 62 79 20 61 75 74 6f 2d 76 61 63 75 75 6d  y by auto-vacuum
ae0a0 20 63 6f 64 65 29 2e 20 44 6f 20 6e 6f 74 20 77   code). Do not w
ae0b0 72 69 74 65 0a 20 20 20 20 2a 2a 20 61 6e 79 20  rite.    ** any 
ae0c0 73 75 63 68 20 70 61 67 65 73 20 74 6f 20 74 68  such pages to th
ae0d0 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20  e file..    */. 
ae0e0 20 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 70 67     if( pList->pg
ae0f0 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 53 69  no<=pPager->dbSi
ae100 7a 65 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72  ze ){.      char
ae110 20 2a 70 44 61 74 61 20 3d 20 43 4f 44 45 43 32   *pData = CODEC2
ae120 28 70 50 61 67 65 72 2c 20 50 47 48 44 52 5f 54  (pPager, PGHDR_T
ae130 4f 5f 44 41 54 41 28 70 4c 69 73 74 29 2c 20 70  O_DATA(pList), p
ae140 4c 69 73 74 2d 3e 70 67 6e 6f 2c 20 36 29 3b 0a  List->pgno, 6);.
ae150 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 45        PAGERTRACE
ae160 34 28 22 53 54 4f 52 45 20 25 64 20 70 61 67 65  4("STORE %d page
ae170 20 25 64 20 68 61 73 68 28 25 30 38 78 29 5c 6e   %d hash(%08x)\n
ae180 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
ae190 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70 50        PAGERID(pP
ae1a0 61 67 65 72 29 2c 20 70 4c 69 73 74 2d 3e 70 67  ager), pList->pg
ae1b0 6e 6f 2c 20 70 61 67 65 72 5f 70 61 67 65 68 61  no, pager_pageha
ae1c0 73 68 28 70 4c 69 73 74 29 29 3b 0a 20 20 20 20  sh(pList));.    
ae1d0 20 20 49 4f 54 52 41 43 45 28 28 22 50 47 4f 55    IOTRACE(("PGOU
ae1e0 54 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67  T %p %d\n", pPag
ae1f0 65 72 2c 20 70 4c 69 73 74 2d 3e 70 67 6e 6f 29  er, pList->pgno)
ae200 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  );.      rc = sq
ae210 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61  lite3OsWrite(pPa
ae220 67 65 72 2d 3e 66 64 2c 20 70 44 61 74 61 2c 20  ger->fd, pData, 
ae230 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
ae240 29 3b 0a 20 20 20 20 20 20 50 41 47 45 52 5f 49  );.      PAGER_I
ae250 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 61 67 65  NCR(sqlite3_page
ae260 72 5f 77 72 69 74 65 64 62 5f 63 6f 75 6e 74 29  r_writedb_count)
ae270 3b 0a 20 20 20 20 20 20 50 41 47 45 52 5f 49 4e  ;.      PAGER_IN
ae280 43 52 28 70 50 61 67 65 72 2d 3e 6e 57 72 69 74  CR(pPager->nWrit
ae290 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4c  e);.      if( pL
ae2a0 69 73 74 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a  ist->pgno==1 ){.
ae2b0 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26          memcpy(&
ae2c0 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65  pPager->dbFileVe
ae2d0 72 73 2c 20 26 70 44 61 74 61 5b 32 34 5d 2c 20  rs, &pData[24], 
ae2e0 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 64  sizeof(pPager->d
ae2f0 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 20  bFileVers));.   
ae300 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 6e 64     }.    }.#ifnd
ae310 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 65 6c  ef NDEBUG.    el
ae320 73 65 7b 0a 20 20 20 20 20 20 50 41 47 45 52 54  se{.      PAGERT
ae330 52 41 43 45 33 28 22 4e 4f 53 54 4f 52 45 20 25  RACE3("NOSTORE %
ae340 64 20 70 61 67 65 20 25 64 5c 6e 22 2c 20 50 41  d page %d\n", PA
ae350 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70  GERID(pPager), p
ae360 4c 69 73 74 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20  List->pgno);.   
ae370 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66   }.#endif.    if
ae380 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
ae390 3b 0a 20 20 20 20 70 4c 69 73 74 2d 3e 64 69 72  ;.    pList->dir
ae3a0 74 79 20 3d 20 30 3b 0a 23 69 66 64 65 66 20 53  ty = 0;.#ifdef S
ae3b0 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45  QLITE_CHECK_PAGE
ae3c0 53 0a 20 20 20 20 70 4c 69 73 74 2d 3e 70 61 67  S.    pList->pag
ae3d0 65 48 61 73 68 20 3d 20 70 61 67 65 72 5f 70 61  eHash = pager_pa
ae3e0 67 65 68 61 73 68 28 70 4c 69 73 74 29 3b 0a 23  gehash(pList);.#
ae3f0 65 6e 64 69 66 0a 20 20 20 20 70 4c 69 73 74 20  endif.    pList 
ae400 3d 20 70 4c 69 73 74 2d 3e 70 44 69 72 74 79 3b  = pList->pDirty;
ae410 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
ae420 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
ae430 2a 20 43 6f 6c 6c 65 63 74 20 65 76 65 72 79 20  * Collect every 
ae440 64 69 72 74 79 20 70 61 67 65 20 69 6e 74 6f 20  dirty page into 
ae450 61 20 64 69 72 74 79 20 6c 69 73 74 20 61 6e 64  a dirty list and
ae460 0a 2a 2a 20 72 65 74 75 72 6e 20 61 20 70 6f 69  .** return a poi
ae470 6e 74 65 72 20 74 6f 20 74 68 65 20 68 65 61 64  nter to the head
ae480 20 6f 66 20 74 68 61 74 20 6c 69 73 74 2e 20 20   of that list.  
ae490 41 6c 6c 20 70 61 67 65 73 20 61 72 65 0a 2a 2a  All pages are.**
ae4a0 20 63 6f 6c 6c 65 63 74 65 64 20 65 76 65 6e 20   collected even 
ae4b0 69 66 20 74 68 65 79 20 61 72 65 20 73 74 69 6c  if they are stil
ae4c0 6c 20 69 6e 20 75 73 65 2e 0a 2a 2f 0a 73 74 61  l in use..*/.sta
ae4d0 74 69 63 20 50 67 48 64 72 20 2a 70 61 67 65 72  tic PgHdr *pager
ae4e0 5f 67 65 74 5f 61 6c 6c 5f 64 69 72 74 79 5f 70  _get_all_dirty_p
ae4f0 61 67 65 73 28 50 61 67 65 72 20 2a 70 50 61 67  ages(Pager *pPag
ae500 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50  er){.  return pP
ae510 61 67 65 72 2d 3e 70 44 69 72 74 79 3b 0a 7d 0a  ager->pDirty;.}.
ae520 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52  ./*.** Return TR
ae530 55 45 20 69 66 20 74 68 65 72 65 20 69 73 20 61  UE if there is a
ae540 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 6f 6e 20   hot journal on 
ae550 74 68 65 20 67 69 76 65 6e 20 70 61 67 65 72 2e  the given pager.
ae560 0a 2a 2a 20 41 20 68 6f 74 20 6a 6f 75 72 6e 61  .** A hot journa
ae570 6c 20 69 73 20 6f 6e 65 20 74 68 61 74 20 6e 65  l is one that ne
ae580 65 64 73 20 74 6f 20 62 65 20 70 6c 61 79 65 64  eds to be played
ae590 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   back..**.** If 
ae5a0 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65  the current size
ae5b0 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
ae5c0 20 66 69 6c 65 20 69 73 20 30 20 62 75 74 20 61   file is 0 but a
ae5d0 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a   journal file.**
ae5e0 20 65 78 69 73 74 73 2c 20 74 68 61 74 20 69 73   exists, that is
ae5f0 20 70 72 6f 62 61 62 6c 79 20 61 6e 20 6f 6c 64   probably an old
ae600 20 6a 6f 75 72 6e 61 6c 20 6c 65 66 74 20 6f 76   journal left ov
ae610 65 72 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 0a  er from a prior.
ae620 2a 2a 20 64 61 74 61 62 61 73 65 20 77 69 74 68  ** database with
ae630 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2e 20   the same name. 
ae640 20 4a 75 73 74 20 64 65 6c 65 74 65 20 74 68 65   Just delete the
ae650 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2f 0a 73 74 61   journal..*/.sta
ae660 74 69 63 20 69 6e 74 20 68 61 73 48 6f 74 4a 6f  tic int hasHotJo
ae670 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61  urnal(Pager *pPa
ae680 67 65 72 29 7b 0a 20 20 69 66 28 20 21 70 50 61  ger){.  if( !pPa
ae690 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20  ger->useJournal 
ae6a0 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66  ) return 0;.  if
ae6b0 28 20 21 73 71 6c 69 74 65 33 4f 73 46 69 6c 65  ( !sqlite3OsFile
ae6c0 45 78 69 73 74 73 28 70 50 61 67 65 72 2d 3e 7a  Exists(pPager->z
ae6d0 4a 6f 75 72 6e 61 6c 29 20 29 7b 0a 20 20 20 20  Journal) ){.    
ae6e0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
ae6f0 69 66 28 20 73 71 6c 69 74 65 33 4f 73 43 68 65  if( sqlite3OsChe
ae700 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 70  ckReservedLock(p
ae710 50 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a 20 20  Pager->fd) ){.  
ae720 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
ae730 20 20 69 66 28 20 73 71 6c 69 74 65 33 50 61 67    if( sqlite3Pag
ae740 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67  erPagecount(pPag
ae750 65 72 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  er)==0 ){.    sq
ae760 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 50  lite3OsDelete(pP
ae770 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 29 3b  ager->zJournal);
ae780 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
ae790 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75   }else{.    retu
ae7a0 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  rn 1;.  }.}../*.
ae7b0 2a 2a 20 54 72 79 20 74 6f 20 66 69 6e 64 20 61  ** Try to find a
ae7c0 20 70 61 67 65 20 69 6e 20 74 68 65 20 63 61 63   page in the cac
ae7d0 68 65 20 74 68 61 74 20 63 61 6e 20 62 65 20 72  he that can be r
ae7e0 65 63 79 63 6c 65 64 2e 20 0a 2a 2a 0a 2a 2a 20  ecycled. .**.** 
ae7f0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 61 79  This routine may
ae800 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
ae810 4f 45 52 52 2c 20 53 51 4c 49 54 45 5f 46 55 4c  OERR, SQLITE_FUL
ae820 4c 20 6f 72 20 53 51 4c 49 54 45 5f 4f 4b 2e 20  L or SQLITE_OK. 
ae830 49 74 20 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20  It .** does not 
ae840 73 65 74 20 74 68 65 20 70 50 61 67 65 72 2d 3e  set the pPager->
ae850 65 72 72 43 6f 64 65 20 76 61 72 69 61 62 6c 65  errCode variable
ae860 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
ae870 70 61 67 65 72 5f 72 65 63 79 63 6c 65 28 50 61  pager_recycle(Pa
ae880 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74  ger *pPager, int
ae890 20 73 79 6e 63 4f 6b 2c 20 50 67 48 64 72 20 2a   syncOk, PgHdr *
ae8a0 2a 70 70 50 67 29 7b 0a 20 20 50 67 48 64 72 20  *ppPg){.  PgHdr 
ae8b0 2a 70 50 67 3b 0a 20 20 2a 70 70 50 67 20 3d 20  *pPg;.  *ppPg = 
ae8c0 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 21 4d 45  0;..  assert(!ME
ae8d0 4d 44 42 29 3b 0a 0a 20 20 2f 2a 20 46 69 6e 64  MDB);..  /* Find
ae8e0 20 61 20 70 61 67 65 20 74 6f 20 72 65 63 79 63   a page to recyc
ae8f0 6c 65 2e 20 20 54 72 79 20 74 6f 20 6c 6f 63 61  le.  Try to loca
ae900 74 65 20 61 20 70 61 67 65 20 74 68 61 74 20 64  te a page that d
ae910 6f 65 73 20 6e 6f 74 0a 20 20 2a 2a 20 72 65 71  oes not.  ** req
ae920 75 69 72 65 20 75 73 20 74 6f 20 64 6f 20 61 6e  uire us to do an
ae930 20 66 73 79 6e 63 28 29 20 6f 6e 20 74 68 65 20   fsync() on the 
ae940 6a 6f 75 72 6e 61 6c 2e 0a 20 20 2a 2f 0a 20 20  journal..  */.  
ae950 70 50 67 20 3d 20 70 50 61 67 65 72 2d 3e 70 46  pPg = pPager->pF
ae960 69 72 73 74 53 79 6e 63 65 64 3b 0a 0a 20 20 2f  irstSynced;..  /
ae970 2a 20 49 66 20 77 65 20 63 6f 75 6c 64 20 6e 6f  * If we could no
ae980 74 20 66 69 6e 64 20 61 20 70 61 67 65 20 74 68  t find a page th
ae990 61 74 20 64 6f 65 73 20 6e 6f 74 20 72 65 71 75  at does not requ
ae9a0 69 72 65 20 61 6e 20 66 73 79 6e 63 28 29 0a 20  ire an fsync(). 
ae9b0 20 2a 2a 20 6f 6e 20 74 68 65 20 6a 6f 75 72 6e   ** on the journ
ae9c0 61 6c 20 66 69 6c 65 20 74 68 65 6e 20 66 73 79  al file then fsy
ae9d0 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  nc the journal f
ae9e0 69 6c 65 2e 20 20 54 68 69 73 20 69 73 20 61 0a  ile.  This is a.
ae9f0 20 20 2a 2a 20 76 65 72 79 20 73 6c 6f 77 20 6f    ** very slow o
aea00 70 65 72 61 74 69 6f 6e 2c 20 73 6f 20 77 65 20  peration, so we 
aea10 77 6f 72 6b 20 68 61 72 64 20 74 6f 20 61 76 6f  work hard to avo
aea20 69 64 20 69 74 2e 20 20 42 75 74 20 73 6f 6d 65  id it.  But some
aea30 74 69 6d 65 73 0a 20 20 2a 2a 20 69 74 20 63 61  times.  ** it ca
aea40 6e 27 74 20 62 65 20 68 65 6c 70 65 64 2e 0a 20  n't be helped.. 
aea50 20 2a 2f 0a 20 20 69 66 28 20 70 50 67 3d 3d 30   */.  if( pPg==0
aea60 20 26 26 20 70 50 61 67 65 72 2d 3e 70 46 69 72   && pPager->pFir
aea70 73 74 20 26 26 20 73 79 6e 63 4f 6b 20 26 26 20  st && syncOk && 
aea80 21 4d 45 4d 44 42 29 7b 0a 20 20 20 20 69 6e 74  !MEMDB){.    int
aea90 20 72 63 20 3d 20 73 79 6e 63 4a 6f 75 72 6e 61   rc = syncJourna
aeaa0 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69  l(pPager);.    i
aeab0 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20  f( rc!=0 ){.    
aeac0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
aead0 20 7d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65   }.    if( pPage
aeae0 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 29 7b 0a 20  r->fullSync ){. 
aeaf0 20 20 20 20 20 2f 2a 20 49 66 20 69 6e 20 66 75       /* If in fu
aeb00 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 77 72  ll-sync mode, wr
aeb10 69 74 65 20 61 20 6e 65 77 20 6a 6f 75 72 6e 61  ite a new journa
aeb20 6c 20 68 65 61 64 65 72 20 69 6e 74 6f 20 74 68  l header into th
aeb30 65 0a 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e  e.      ** journ
aeb40 61 6c 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73  al file. This is
aeb50 20 64 6f 6e 65 20 74 6f 20 61 76 6f 69 64 20 65   done to avoid e
aeb60 76 65 72 20 6d 6f 64 69 66 79 69 6e 67 20 61 20  ver modifying a 
aeb70 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 20 2a 2a  journal.      **
aeb80 20 68 65 61 64 65 72 20 74 68 61 74 20 69 73 20   header that is 
aeb90 69 6e 76 6f 6c 76 65 64 20 69 6e 20 74 68 65 20  involved in the 
aeba0 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 70 61 67 65  rollback of page
aebb0 73 20 74 68 61 74 20 68 61 76 65 0a 20 20 20 20  s that have.    
aebc0 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65    ** already bee
aebd0 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65  n written to the
aebe0 20 64 61 74 61 62 61 73 65 20 28 69 6e 20 63 61   database (in ca
aebf0 73 65 20 74 68 65 20 68 65 61 64 65 72 20 69 73  se the header is
aec00 0a 20 20 20 20 20 20 2a 2a 20 74 72 61 73 68 65  .      ** trashe
aec10 64 20 77 68 65 6e 20 74 68 65 20 6e 52 65 63 20  d when the nRec 
aec20 66 69 65 6c 64 20 69 73 20 75 70 64 61 74 65 64  field is updated
aec30 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  )..      */.    
aec40 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d    pPager->nRec =
aec50 20 30 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   0;.      assert
aec60 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  ( pPager->journa
aec70 6c 4f 66 66 20 3e 20 30 20 29 3b 0a 20 20 20 20  lOff > 0 );.    
aec80 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
aec90 2d 3e 64 6f 4e 6f 74 53 79 6e 63 3d 3d 30 20 29  ->doNotSync==0 )
aeca0 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 77 72 69  ;.      rc = wri
aecb0 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61  teJournalHdr(pPa
aecc0 67 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ger);.      if( 
aecd0 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  rc!=0 ){.       
aece0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
aecf0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 50    }.    }.    pP
aed00 67 20 3d 20 70 50 61 67 65 72 2d 3e 70 46 69 72  g = pPager->pFir
aed10 73 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50  st;.  }.  if( pP
aed20 67 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  g==0 ){.    retu
aed30 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
aed40 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 67  }..  assert( pPg
aed50 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 0a 20 20  ->nRef==0 );..  
aed60 2f 2a 20 57 72 69 74 65 20 74 68 65 20 70 61 67  /* Write the pag
aed70 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  e to the databas
aed80 65 20 66 69 6c 65 20 69 66 20 69 74 20 69 73 20  e file if it is 
aed90 64 69 72 74 79 2e 0a 20 20 2a 2f 0a 20 20 69 66  dirty..  */.  if
aeda0 28 20 70 50 67 2d 3e 64 69 72 74 79 20 29 7b 0a  ( pPg->dirty ){.
aedb0 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20      int rc;.    
aedc0 61 73 73 65 72 74 28 20 70 50 67 2d 3e 6e 65 65  assert( pPg->nee
aedd0 64 53 79 6e 63 3d 3d 30 20 29 3b 0a 20 20 20 20  dSync==0 );.    
aede0 6d 61 6b 65 43 6c 65 61 6e 28 70 50 67 29 3b 0a  makeClean(pPg);.
aedf0 20 20 20 20 70 50 67 2d 3e 64 69 72 74 79 20 3d      pPg->dirty =
aee00 20 31 3b 0a 20 20 20 20 70 50 67 2d 3e 70 44 69   1;.    pPg->pDi
aee10 72 74 79 20 3d 20 30 3b 0a 20 20 20 20 72 63 20  rty = 0;.    rc 
aee20 3d 20 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61  = pager_write_pa
aee30 67 65 6c 69 73 74 28 20 70 50 67 20 29 3b 0a 20  gelist( pPg );. 
aee40 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
aee50 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65  E_OK ){.      re
aee60 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
aee70 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 50 67   }.  assert( pPg
aee80 2d 3e 64 69 72 74 79 3d 3d 30 20 29 3b 0a 0a 20  ->dirty==0 );.. 
aee90 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 20   /* If the page 
aeea0 77 65 20 61 72 65 20 72 65 63 79 63 6c 69 6e 67  we are recycling
aeeb0 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 61 6c   is marked as al
aeec0 77 61 79 73 52 6f 6c 6c 62 61 63 6b 2c 20 74 68  waysRollback, th
aeed0 65 6e 0a 20 20 2a 2a 20 73 65 74 20 74 68 65 20  en.  ** set the 
aeee0 67 6c 6f 62 61 6c 20 61 6c 77 61 79 73 52 6f 6c  global alwaysRol
aeef0 6c 62 61 63 6b 20 66 6c 61 67 2c 20 74 68 75 73  lback flag, thus
aef00 20 64 69 73 61 62 6c 69 6e 67 20 74 68 65 0a 20   disabling the. 
aef10 20 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72   ** sqlite3Pager
aef20 44 6f 6e 74 52 6f 6c 6c 62 61 63 6b 28 29 20 6f  DontRollback() o
aef30 70 74 69 6d 69 7a 61 74 69 6f 6e 20 66 6f 72 20  ptimization for 
aef40 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 69 73  the rest of this
aef50 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20   transaction..  
aef60 2a 2a 20 49 74 20 69 73 20 6e 65 63 65 73 73 61  ** It is necessa
aef70 72 79 20 74 6f 20 64 6f 20 74 68 69 73 20 62 65  ry to do this be
aef80 63 61 75 73 65 20 74 68 65 20 70 61 67 65 20 6d  cause the page m
aef90 61 72 6b 65 64 20 61 6c 77 61 79 73 52 6f 6c 6c  arked alwaysRoll
aefa0 62 61 63 6b 0a 20 20 2a 2a 20 6d 69 67 68 74 20  back.  ** might 
aefb0 62 65 20 72 65 6c 6f 61 64 65 64 20 61 74 20 61  be reloaded at a
aefc0 20 6c 61 74 65 72 20 74 69 6d 65 20 62 75 74 20   later time but 
aefd0 61 74 20 74 68 61 74 20 70 6f 69 6e 74 20 77 65  at that point we
aefe0 20 77 6f 6e 27 74 20 72 65 6d 65 6d 62 65 72 0a   won't remember.
aeff0 20 20 2a 2a 20 74 68 61 74 20 69 73 20 77 61 73    ** that is was
af000 20 6d 61 72 6b 65 64 20 61 6c 77 61 79 73 52 6f   marked alwaysRo
af010 6c 6c 62 61 63 6b 2e 20 20 54 68 69 73 20 6d 65  llback.  This me
af020 61 6e 73 20 74 68 61 74 20 61 6c 6c 20 70 61 67  ans that all pag
af030 65 73 20 6d 75 73 74 0a 20 20 2a 2a 20 62 65 20  es must.  ** be 
af040 6d 61 72 6b 65 64 20 61 73 20 61 6c 77 61 79 73  marked as always
af050 52 6f 6c 6c 62 61 63 6b 20 66 72 6f 6d 20 68 65  Rollback from he
af060 72 65 20 6f 6e 20 6f 75 74 2e 0a 20 20 2a 2f 0a  re on out..  */.
af070 20 20 69 66 28 20 70 50 67 2d 3e 61 6c 77 61 79    if( pPg->alway
af080 73 52 6f 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20  sRollback ){.   
af090 20 49 4f 54 52 41 43 45 28 28 22 41 4c 57 41 59   IOTRACE(("ALWAY
af0a0 53 5f 52 4f 4c 4c 42 41 43 4b 20 25 70 5c 6e 22  S_ROLLBACK %p\n"
af0b0 2c 20 70 50 61 67 65 72 29 29 0a 20 20 20 20 70  , pPager)).    p
af0c0 50 61 67 65 72 2d 3e 61 6c 77 61 79 73 52 6f 6c  Pager->alwaysRol
af0d0 6c 62 61 63 6b 20 3d 20 31 3b 0a 20 20 7d 0a 0a  lback = 1;.  }..
af0e0 20 20 2f 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20    /* Unlink the 
af0f0 6f 6c 64 20 70 61 67 65 20 66 72 6f 6d 20 74 68  old page from th
af100 65 20 66 72 65 65 20 6c 69 73 74 20 61 6e 64 20  e free list and 
af110 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 0a 20  the hash table. 
af120 20 2a 2f 0a 20 20 75 6e 6c 69 6e 6b 50 61 67 65   */.  unlinkPage
af130 28 70 50 67 29 3b 0a 20 20 61 73 73 65 72 74 28  (pPg);.  assert(
af140 20 70 50 67 2d 3e 70 67 6e 6f 3d 3d 30 20 29 3b   pPg->pgno==0 );
af150 0a 0a 20 20 2a 70 70 50 67 20 3d 20 70 50 67 3b  ..  *ppPg = pPg;
af160 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
af170 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  _OK;.}../*.** Th
af180 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
af190 61 6c 6c 65 64 20 74 6f 20 66 72 65 65 20 73 75  alled to free su
af1a0 70 65 72 66 6c 75 6f 75 73 20 64 79 6e 61 6d 69  perfluous dynami
af1b0 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20  cally allocated 
af1c0 6d 65 6d 6f 72 79 0a 2a 2a 20 68 65 6c 64 20 62  memory.** held b
af1d0 79 20 74 68 65 20 70 61 67 65 72 20 73 79 73 74  y the pager syst
af1e0 65 6d 2e 20 4d 65 6d 6f 72 79 20 69 6e 20 75 73  em. Memory in us
af1f0 65 20 62 79 20 61 6e 79 20 53 51 4c 69 74 65 20  e by any SQLite 
af200 70 61 67 65 72 20 61 6c 6c 6f 63 61 74 65 64 0a  pager allocated.
af210 2a 2a 20 62 79 20 74 68 65 20 63 75 72 72 65 6e  ** by the curren
af220 74 20 74 68 72 65 61 64 20 6d 61 79 20 62 65 20  t thread may be 
af230 73 71 6c 69 74 65 46 72 65 65 28 29 65 64 2e 0a  sqliteFree()ed..
af240 2a 2a 0a 2a 2a 20 6e 52 65 71 20 69 73 20 74 68  **.** nReq is th
af250 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
af260 73 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65 71 75  s of memory requ
af270 69 72 65 64 2e 20 4f 6e 63 65 20 74 68 69 73 20  ired. Once this 
af280 6d 75 63 68 20 68 61 73 0a 2a 2a 20 62 65 65 6e  much has.** been
af290 20 72 65 6c 65 61 73 65 64 2c 20 74 68 65 20 66   released, the f
af2a0 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e  unction returns.
af2b0 20 41 20 6e 65 67 61 74 69 76 65 20 76 61 6c 75   A negative valu
af2c0 65 20 66 6f 72 20 6e 52 65 71 20 6d 65 61 6e 73  e for nReq means
af2d0 0a 2a 2a 20 66 72 65 65 20 61 73 20 6d 75 63 68  .** free as much
af2e0 20 6d 65 6d 6f 72 79 20 61 73 20 70 6f 73 73 69   memory as possi
af2f0 62 6c 65 2e 20 54 68 65 20 72 65 74 75 72 6e 20  ble. The return 
af300 76 61 6c 75 65 20 69 73 20 74 68 65 20 74 6f 74  value is the tot
af310 61 6c 20 6e 75 6d 62 65 72 20 0a 2a 2a 20 6f 66  al number .** of
af320 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79   bytes of memory
af330 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2f 0a 23 69   released..*/.#i
af340 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
af350 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d  _ENABLE_MEMORY_M
af360 41 4e 41 47 45 4d 45 4e 54 29 20 26 26 20 21 64  ANAGEMENT) && !d
af370 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
af380 49 54 5f 44 49 53 4b 49 4f 29 0a 53 51 4c 49 54  IT_DISKIO).SQLIT
af390 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
af3a0 6c 69 74 65 33 50 61 67 65 72 52 65 6c 65 61 73  lite3PagerReleas
af3b0 65 4d 65 6d 6f 72 79 28 69 6e 74 20 6e 52 65 71  eMemory(int nReq
af3c0 29 7b 0a 20 20 63 6f 6e 73 74 20 54 68 72 65 61  ){.  const Threa
af3d0 64 44 61 74 61 20 2a 70 54 73 64 72 6f 20 3d 20  dData *pTsdro = 
af3e0 73 71 6c 69 74 65 33 54 68 72 65 61 64 44 61 74  sqlite3ThreadDat
af3f0 61 52 65 61 64 4f 6e 6c 79 28 29 3b 0a 20 20 69  aReadOnly();.  i
af400 6e 74 20 6e 52 65 6c 65 61 73 65 64 20 3d 20 30  nt nReleased = 0
af410 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 2f 2a  ;.  int i;..  /*
af420 20 49 66 20 74 68 65 20 74 68 65 20 67 6c 6f 62   If the the glob
af430 61 6c 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64  al mutex is held
af440 2c 20 74 68 69 73 20 73 75 62 72 6f 75 74 69 6e  , this subroutin
af450 65 20 62 65 63 6f 6d 65 73 20 61 0a 20 20 2a 2a  e becomes a.  **
af460 20 6f 2d 6f 70 3b 20 7a 65 72 6f 20 62 79 74 65   o-op; zero byte
af470 73 20 6f 66 20 6d 65 6d 6f 72 79 20 61 72 65 20  s of memory are 
af480 66 72 65 65 64 2e 20 20 54 68 69 73 20 69 73 20  freed.  This is 
af490 62 65 63 61 75 73 65 0a 20 20 2a 2a 20 73 6f 6d  because.  ** som
af4a0 65 20 6f 66 20 74 68 65 20 63 6f 64 65 20 69 6e  e of the code in
af4b0 76 6f 6b 65 64 20 62 79 20 74 68 69 73 20 66 75  voked by this fu
af4c0 6e 63 74 69 6f 6e 20 6d 61 79 20 61 6c 73 6f 0a  nction may also.
af4d0 20 20 2a 2a 20 74 72 79 20 74 6f 20 6f 62 74 61    ** try to obta
af4e0 69 6e 20 74 68 65 20 6d 75 74 65 78 2c 20 72 65  in the mutex, re
af4f0 73 75 6c 74 69 6e 67 20 69 6e 20 61 20 64 65 61  sulting in a dea
af500 64 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66  dlock..  */.  if
af510 28 20 73 71 6c 69 74 65 33 4f 73 49 6e 4d 75 74  ( sqlite3OsInMut
af520 65 78 28 30 29 20 29 7b 0a 20 20 20 20 72 65 74  ex(0) ){.    ret
af530 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  urn 0;.  }..  /*
af540 20 4f 75 74 65 72 6d 6f 73 74 20 6c 6f 6f 70 20   Outermost loop 
af550 72 75 6e 73 20 66 6f 72 20 61 74 20 6d 6f 73 74  runs for at most
af560 20 74 77 6f 20 69 74 65 72 61 74 69 6f 6e 73 2e   two iterations.
af570 20 46 69 72 73 74 20 69 74 65 72 61 74 69 6f 6e   First iteration
af580 20 77 65 0a 20 20 2a 2a 20 74 72 79 20 74 6f 20   we.  ** try to 
af590 66 69 6e 64 20 6d 65 6d 6f 72 79 20 74 68 61 74  find memory that
af5a0 20 63 61 6e 20 62 65 20 72 65 6c 65 61 73 65 64   can be released
af5b0 20 77 69 74 68 6f 75 74 20 63 61 6c 6c 69 6e 67   without calling
af5c0 20 66 73 79 6e 63 28 29 2e 20 53 65 63 6f 6e 64   fsync(). Second
af5d0 0a 20 20 2a 2a 20 69 74 65 72 61 74 69 6f 6e 20  .  ** iteration 
af5e0 28 77 68 69 63 68 20 6f 6e 6c 79 20 72 75 6e 73  (which only runs
af5f0 20 69 66 20 74 68 65 20 66 69 72 73 74 20 66 61   if the first fa
af600 69 6c 65 64 20 74 6f 20 66 72 65 65 20 6e 52 65  iled to free nRe
af610 71 20 62 79 74 65 73 20 6f 66 0a 20 20 2a 2a 20  q bytes of.  ** 
af620 6d 65 6d 6f 72 79 29 20 69 73 20 70 65 72 6d 69  memory) is permi
af630 74 74 65 64 20 74 6f 20 63 61 6c 6c 20 66 73 79  tted to call fsy
af640 6e 63 28 29 2e 20 54 68 69 73 20 69 73 20 6f 66  nc(). This is of
af650 20 63 6f 75 72 73 65 20 6d 75 63 68 20 6d 6f 72   course much mor
af660 65 20 0a 20 20 2a 2a 20 65 78 70 65 6e 73 69 76  e .  ** expensiv
af670 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d  e..  */.  for(i=
af680 30 3b 20 69 3c 3d 31 3b 20 69 2b 2b 29 7b 0a 0a  0; i<=1; i++){..
af690 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f      /* Loop thro
af6a0 75 67 68 20 61 6c 6c 20 74 68 65 20 53 51 4c 69  ugh all the SQLi
af6b0 74 65 20 70 61 67 65 72 73 20 6f 70 65 6e 65 64  te pagers opened
af6c0 20 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 20   by the current 
af6d0 74 68 72 65 61 64 2e 20 2a 2f 0a 20 20 20 20 50  thread. */.    P
af6e0 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70  ager *pPager = p
af6f0 54 73 64 72 6f 2d 3e 70 50 61 67 65 72 3b 0a 20  Tsdro->pPager;. 
af700 20 20 20 66 6f 72 28 20 3b 20 70 50 61 67 65 72     for( ; pPager
af710 20 26 26 20 28 6e 52 65 71 3c 30 20 7c 7c 20 6e   && (nReq<0 || n
af720 52 65 6c 65 61 73 65 64 3c 6e 52 65 71 29 3b 20  Released<nReq); 
af730 70 50 61 67 65 72 3d 70 50 61 67 65 72 2d 3e 70  pPager=pPager->p
af740 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 50 67 48  Next){.      PgH
af750 64 72 20 2a 70 50 67 3b 0a 20 20 20 20 20 20 69  dr *pPg;.      i
af760 6e 74 20 72 63 3b 0a 0a 20 20 20 20 20 20 69 66  nt rc;..      if
af770 28 20 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 20  ( MEMDB ){.     
af780 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
af790 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 46     }..      /* F
af7a0 6f 72 20 65 61 63 68 20 70 61 67 65 72 2c 20 74  or each pager, t
af7b0 72 79 20 74 6f 20 66 72 65 65 20 61 73 20 6d 61  ry to free as ma
af7c0 6e 79 20 70 61 67 65 73 20 61 73 20 70 6f 73 73  ny pages as poss
af7d0 69 62 6c 65 20 28 77 69 74 68 6f 75 74 20 0a 20  ible (without . 
af7e0 20 20 20 20 20 2a 2a 20 63 61 6c 6c 69 6e 67 20       ** calling 
af7f0 66 73 79 6e 63 28 29 20 69 66 20 74 68 69 73 20  fsync() if this 
af800 69 73 20 74 68 65 20 66 69 72 73 74 20 69 74 65  is the first ite
af810 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6f 75  ration of the ou
af820 74 65 72 6d 6f 73 74 20 0a 20 20 20 20 20 20 2a  termost .      *
af830 2a 20 6c 6f 6f 70 29 2e 0a 20 20 20 20 20 20 2a  * loop)..      *
af840 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 53  /.      while( S
af850 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20  QLITE_OK==(rc = 
af860 70 61 67 65 72 5f 72 65 63 79 63 6c 65 28 70 50  pager_recycle(pP
af870 61 67 65 72 2c 20 69 2c 20 26 70 50 67 29 29 20  ager, i, &pPg)) 
af880 26 26 20 70 50 67 29 20 7b 0a 20 20 20 20 20 20  && pPg) {.      
af890 20 20 2f 2a 20 57 65 27 76 65 20 66 6f 75 6e 64    /* We've found
af8a0 20 61 20 70 61 67 65 20 74 6f 20 66 72 65 65 2e   a page to free.
af8b0 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74   At this point t
af8c0 68 65 20 70 61 67 65 20 68 61 73 20 62 65 65 6e  he page has been
af8d0 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 65 6d   .        ** rem
af8e0 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 70 61  oved from the pa
af8f0 67 65 20 68 61 73 68 2d 74 61 62 6c 65 2c 20 66  ge hash-table, f
af900 72 65 65 2d 6c 69 73 74 20 61 6e 64 20 73 79 6e  ree-list and syn
af910 63 65 64 2d 6c 69 73 74 20 0a 20 20 20 20 20 20  ced-list .      
af920 20 20 2a 2a 20 28 70 46 69 72 73 74 53 79 6e 63    ** (pFirstSync
af930 65 64 29 2e 20 49 74 20 69 73 20 73 74 69 6c 6c  ed). It is still
af940 20 69 6e 20 74 68 65 20 61 6c 6c 20 70 61 67 65   in the all page
af950 73 20 28 70 41 6c 6c 29 20 6c 69 73 74 2e 20 0a  s (pAll) list. .
af960 20 20 20 20 20 20 20 20 2a 2a 20 52 65 6d 6f 76          ** Remov
af970 65 20 69 74 20 66 72 6f 6d 20 74 68 69 73 20 6c  e it from this l
af980 69 73 74 20 62 65 66 6f 72 65 20 66 72 65 65 69  ist before freei
af990 6e 67 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20  ng..        **. 
af9a0 20 20 20 20 20 20 20 2a 2a 20 54 6f 64 6f 3a 20         ** Todo: 
af9b0 43 68 65 63 6b 20 74 68 65 20 50 61 67 65 72 2e  Check the Pager.
af9c0 70 53 74 6d 74 20 6c 69 73 74 20 74 6f 20 6d 61  pStmt list to ma
af9d0 6b 65 20 73 75 72 65 20 74 68 69 73 20 69 73 20  ke sure this is 
af9e0 4f 6b 2e 20 49 74 20 0a 20 20 20 20 20 20 20 20  Ok. It .        
af9f0 2a 2a 20 70 72 6f 62 61 62 6c 79 20 69 73 20 74  ** probably is t
afa00 68 6f 75 67 68 2e 0a 20 20 20 20 20 20 20 20 2a  hough..        *
afa10 2f 0a 20 20 20 20 20 20 20 20 50 67 48 64 72 20  /.        PgHdr 
afa20 2a 70 54 6d 70 3b 0a 20 20 20 20 20 20 20 20 61  *pTmp;.        a
afa30 73 73 65 72 74 28 20 70 50 67 20 29 3b 0a 20 20  ssert( pPg );.  
afa40 20 20 20 20 20 20 69 66 28 20 70 50 67 3d 3d 70        if( pPg==p
afa50 50 61 67 65 72 2d 3e 70 41 6c 6c 20 29 7b 0a 20  Pager->pAll ){. 
afa60 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72            pPager
afa70 2d 3e 70 41 6c 6c 20 3d 20 70 50 67 2d 3e 70 4e  ->pAll = pPg->pN
afa80 65 78 74 41 6c 6c 3b 0a 20 20 20 20 20 20 20 20  extAll;.        
afa90 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
afaa0 20 66 6f 72 28 20 70 54 6d 70 3d 70 50 61 67 65   for( pTmp=pPage
afab0 72 2d 3e 70 41 6c 6c 3b 20 70 54 6d 70 2d 3e 70  r->pAll; pTmp->p
afac0 4e 65 78 74 41 6c 6c 21 3d 70 50 67 3b 20 70 54  NextAll!=pPg; pT
afad0 6d 70 3d 70 54 6d 70 2d 3e 70 4e 65 78 74 41 6c  mp=pTmp->pNextAl
afae0 6c 20 29 7b 7d 0a 20 20 20 20 20 20 20 20 20 20  l ){}.          
afaf0 70 54 6d 70 2d 3e 70 4e 65 78 74 41 6c 6c 20 3d  pTmp->pNextAll =
afb00 20 70 50 67 2d 3e 70 4e 65 78 74 41 6c 6c 3b 0a   pPg->pNextAll;.
afb10 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
afb20 20 20 6e 52 65 6c 65 61 73 65 64 20 2b 3d 20 73    nReleased += s
afb30 71 6c 69 74 65 41 6c 6c 6f 63 53 69 7a 65 28 70  qliteAllocSize(p
afb40 50 67 29 3b 0a 20 20 20 20 20 20 20 20 49 4f 54  Pg);.        IOT
afb50 52 41 43 45 28 28 22 50 47 46 52 45 45 20 25 70  RACE(("PGFREE %p
afb60 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20   %d\n", pPager, 
afb70 70 50 67 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20  pPg->pgno));.   
afb80 20 20 20 20 20 50 41 47 45 52 5f 49 4e 43 52 28       PAGER_INCR(
afb90 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 70 67  sqlite3_pager_pg
afba0 66 72 65 65 5f 63 6f 75 6e 74 29 3b 0a 20 20 20  free_count);.   
afbb0 20 20 20 20 20 73 71 6c 69 74 65 46 72 65 65 28       sqliteFree(
afbc0 70 50 67 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  pPg);.      }.. 
afbd0 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
afbe0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
afbf0 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20 6f 63    /* An error oc
afc00 63 75 72 65 64 20 77 68 69 6c 73 74 20 77 72 69  cured whilst wri
afc10 74 69 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61  ting to the data
afc20 62 61 73 65 20 66 69 6c 65 20 6f 72 20 0a 20 20  base file or .  
afc30 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c        ** journal
afc40 20 69 6e 20 70 61 67 65 72 5f 72 65 63 79 63 6c   in pager_recycl
afc50 65 28 29 2e 20 54 68 65 20 65 72 72 6f 72 20 69  e(). The error i
afc60 73 20 6e 6f 74 20 72 65 74 75 72 6e 65 64 20 74  s not returned t
afc70 6f 20 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a  o the .        *
afc80 2a 20 63 61 6c 6c 65 72 20 6f 66 20 74 68 69 73  * caller of this
afc90 20 66 75 6e 63 74 69 6f 6e 2e 20 49 6e 73 74 65   function. Inste
afca0 61 64 2c 20 73 65 74 20 74 68 65 20 50 61 67 65  ad, set the Page
afcb0 72 2e 65 72 72 43 6f 64 65 20 76 61 72 69 61 62  r.errCode variab
afcc0 6c 65 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 54  le..        ** T
afcd0 68 65 20 65 72 72 6f 72 20 77 69 6c 6c 20 62 65  he error will be
afce0 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65   returned to the
afcf0 20 75 73 65 72 20 28 6f 72 20 75 73 65 72 73 2c   user (or users,
afd00 20 69 6e 20 74 68 65 20 63 61 73 65 20 0a 20 20   in the case .  
afd10 20 20 20 20 20 20 2a 2a 20 6f 66 20 61 20 73 68        ** of a sh
afd20 61 72 65 64 20 70 61 67 65 72 20 63 61 63 68 65  ared pager cache
afd30 29 20 6f 66 20 74 68 65 20 70 61 67 65 72 20 66  ) of the pager f
afd40 6f 72 20 77 68 69 63 68 20 74 68 65 20 65 72 72  or which the err
afd50 6f 72 20 6f 63 63 75 72 65 64 2e 0a 20 20 20 20  or occured..    
afd60 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 61      */.        a
afd70 73 73 65 72 74 28 20 28 72 63 26 30 78 66 66 29  ssert( (rc&0xff)
afd80 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c  ==SQLITE_IOERR |
afd90 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 46 55 4c  | rc==SQLITE_FUL
afda0 4c 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73  L );.        ass
afdb0 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61  ert( pPager->sta
afdc0 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 56  te>=PAGER_RESERV
afdd0 45 44 20 29 3b 0a 20 20 20 20 20 20 20 20 70 61  ED );.        pa
afde0 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72  ger_error(pPager
afdf0 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20  , rc);.      }. 
afe00 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
afe10 72 6e 20 6e 52 65 6c 65 61 73 65 64 3b 0a 7d 0a  rn nReleased;.}.
afe20 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
afe30 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d  _ENABLE_MEMORY_M
afe40 41 4e 41 47 45 4d 45 4e 54 20 26 26 20 21 53 51  ANAGEMENT && !SQ
afe50 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f  LITE_OMIT_DISKIO
afe60 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20   */../*.** Read 
afe70 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 70  the content of p
afe80 61 67 65 20 70 50 67 20 6f 75 74 20 6f 66 20 74  age pPg out of t
afe90 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
afea0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
afeb0 72 65 61 64 44 62 50 61 67 65 28 50 61 67 65 72  readDbPage(Pager
afec0 20 2a 70 50 61 67 65 72 2c 20 50 67 48 64 72 20   *pPager, PgHdr 
afed0 2a 70 50 67 2c 20 50 67 6e 6f 20 70 67 6e 6f 29  *pPg, Pgno pgno)
afee0 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73  {.  int rc;.  as
afef0 73 65 72 74 28 20 4d 45 4d 44 42 3d 3d 30 20 29  sert( MEMDB==0 )
aff00 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
aff10 4f 73 53 65 65 6b 28 70 50 61 67 65 72 2d 3e 66  OsSeek(pPager->f
aff20 64 2c 20 28 70 67 6e 6f 2d 31 29 2a 28 69 36 34  d, (pgno-1)*(i64
aff30 29 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a  )pPager->pageSiz
aff40 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  e);.  if( rc==SQ
aff50 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
aff60 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61  c = sqlite3OsRea
aff70 64 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 50 47  d(pPager->fd, PG
aff80 48 44 52 5f 54 4f 5f 44 41 54 41 28 70 50 67 29  HDR_TO_DATA(pPg)
aff90 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
affa0 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61 67              pPag
affb0 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20  er->pageSize);. 
affc0 20 7d 0a 20 20 50 41 47 45 52 5f 49 4e 43 52 28   }.  PAGER_INCR(
affd0 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 72 65  sqlite3_pager_re
affe0 61 64 64 62 5f 63 6f 75 6e 74 29 3b 0a 20 20 50  addb_count);.  P
afff0 41 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65 72  AGER_INCR(pPager
b0000 2d 3e 6e 52 65 61 64 29 3b 0a 20 20 49 4f 54 52  ->nRead);.  IOTR
b0010 41 43 45 28 28 22 50 47 49 4e 20 25 70 20 25 64  ACE(("PGIN %p %d
b0020 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 67 6e  \n", pPager, pgn
b0030 6f 29 29 3b 0a 20 20 69 66 28 20 70 67 6e 6f 3d  o));.  if( pgno=
b0040 3d 31 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79  =1 ){.    memcpy
b0050 28 26 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65  (&pPager->dbFile
b0060 56 65 72 73 2c 20 26 28 28 75 38 2a 29 50 47 48  Vers, &((u8*)PGH
b0070 44 52 5f 54 4f 5f 44 41 54 41 28 70 50 67 29 29  DR_TO_DATA(pPg))
b0080 5b 32 34 5d 2c 0a 20 20 20 20 20 20 20 20 20 20  [24],.          
b0090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b00a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b00b0 20 20 20 20 73 69 7a 65 6f 66 28 70 50 61 67 65      sizeof(pPage
b00c0 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 29 3b  r->dbFileVers));
b00d0 0a 20 20 7d 0a 20 20 43 4f 44 45 43 31 28 70 50  .  }.  CODEC1(pP
b00e0 61 67 65 72 2c 20 50 47 48 44 52 5f 54 4f 5f 44  ager, PGHDR_TO_D
b00f0 41 54 41 28 70 50 67 29 2c 20 70 50 67 2d 3e 70  ATA(pPg), pPg->p
b0100 67 6e 6f 2c 20 33 29 3b 0a 20 20 50 41 47 45 52  gno, 3);.  PAGER
b0110 54 52 41 43 45 34 28 22 46 45 54 43 48 20 25 64  TRACE4("FETCH %d
b0120 20 70 61 67 65 20 25 64 20 68 61 73 68 28 25 30   page %d hash(%0
b0130 38 78 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20  8x)\n",.        
b0140 20 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70         PAGERID(p
b0150 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e  Pager), pPg->pgn
b0160 6f 2c 20 70 61 67 65 72 5f 70 61 67 65 68 61 73  o, pager_pagehas
b0170 68 28 70 50 67 29 29 3b 0a 20 20 72 65 74 75 72  h(pPg));.  retur
b0180 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  n rc;.}.../*.** 
b0190 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
b01a0 20 63 61 6c 6c 65 64 20 74 6f 20 6f 62 74 61 69   called to obtai
b01b0 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63  n the shared loc
b01c0 6b 20 72 65 71 75 69 72 65 64 20 62 65 66 6f 72  k required befor
b01d0 65 0a 2a 2a 20 64 61 74 61 20 6d 61 79 20 62 65  e.** data may be
b01e0 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 70   read from the p
b01f0 61 67 65 72 20 63 61 63 68 65 2e 20 49 66 20 74  ager cache. If t
b0200 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 68  he shared lock h
b0210 61 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 62 65  as already.** be
b0220 65 6e 20 6f 62 74 61 69 6e 65 64 2c 20 74 68 69  en obtained, thi
b0230 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20  s function is a 
b0240 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 6d 6d  no-op..**.** Imm
b0250 65 64 69 61 74 65 6c 79 20 61 66 74 65 72 20 6f  ediately after o
b0260 62 74 61 69 6e 69 6e 67 20 74 68 65 20 73 68 61  btaining the sha
b0270 72 65 64 20 6c 6f 63 6b 20 28 69 66 20 72 65 71  red lock (if req
b0280 75 69 72 65 64 29 2c 20 74 68 69 73 20 66 75 6e  uired), this fun
b0290 63 74 69 6f 6e 0a 2a 2a 20 63 68 65 63 6b 73 20  ction.** checks 
b02a0 66 6f 72 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61  for a hot-journa
b02b0 6c 20 66 69 6c 65 2e 20 49 66 20 6f 6e 65 20 69  l file. If one i
b02c0 73 20 66 6f 75 6e 64 2c 20 61 6e 20 65 6d 65 72  s found, an emer
b02d0 67 65 6e 63 79 20 72 6f 6c 6c 62 61 63 6b 0a 2a  gency rollback.*
b02e0 2a 20 69 73 20 70 65 72 66 6f 72 6d 65 64 20 69  * is performed i
b02f0 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 2a 2f 0a 73  mmediately..*/.s
b0300 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 53  tatic int pagerS
b0310 68 61 72 65 64 4c 6f 63 6b 28 50 61 67 65 72 20  haredLock(Pager 
b0320 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20  *pPager){.  int 
b0330 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
b0340 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73  .  if( pPager->s
b0350 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f  tate==PAGER_UNLO
b0360 43 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 21 4d  CK ){.    if( !M
b0370 45 4d 44 42 20 29 7b 0a 20 20 20 20 20 20 61 73  EMDB ){.      as
b0380 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 52  sert( pPager->nR
b0390 65 66 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 69  ef==0 );.      i
b03a0 66 28 20 21 70 50 61 67 65 72 2d 3e 6e 6f 52 65  f( !pPager->noRe
b03b0 61 64 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20  adlock ){.      
b03c0 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 61 69    rc = pager_wai
b03d0 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72  t_on_lock(pPager
b03e0 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a  , SHARED_LOCK);.
b03f0 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
b0400 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
b0410 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 61         return pa
b0420 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72  ger_error(pPager
b0430 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 20 20 7d  , rc);.        }
b0440 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
b0450 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d   pPager->state>=
b0460 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20  SHARED_LOCK );. 
b0470 20 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 20 20       }.  .      
b0480 2f 2a 20 49 66 20 61 20 6a 6f 75 72 6e 61 6c 20  /* If a journal 
b0490 66 69 6c 65 20 65 78 69 73 74 73 2c 20 61 6e 64  file exists, and
b04a0 20 74 68 65 72 65 20 69 73 20 6e 6f 20 52 45 53   there is no RES
b04b0 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68  ERVED lock on th
b04c0 65 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62  e.      ** datab
b04d0 61 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 69  ase file, then i
b04e0 74 20 65 69 74 68 65 72 20 6e 65 65 64 73 20 74  t either needs t
b04f0 6f 20 62 65 20 70 6c 61 79 65 64 20 62 61 63 6b  o be played back
b0500 20 6f 72 20 64 65 6c 65 74 65 64 2e 0a 20 20 20   or deleted..   
b0510 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20     */.      if( 
b0520 68 61 73 48 6f 74 4a 6f 75 72 6e 61 6c 28 70 50  hasHotJournal(pP
b0530 61 67 65 72 29 20 29 7b 0a 20 20 20 20 20 20 20  ager) ){.       
b0540 20 2f 2a 20 47 65 74 20 61 6e 20 45 58 43 4c 55   /* Get an EXCLU
b0550 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  SIVE lock on the
b0560 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
b0570 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 69 74  At this point it
b0580 20 69 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 69   is.        ** i
b0590 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20 61 20  mportant that a 
b05a0 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 73  RESERVED lock is
b05b0 20 6e 6f 74 20 6f 62 74 61 69 6e 65 64 20 6f 6e   not obtained on
b05c0 20 74 68 65 20 77 61 79 20 74 6f 20 74 68 65 0a   the way to the.
b05d0 20 20 20 20 20 20 20 20 2a 2a 20 45 58 43 4c 55          ** EXCLU
b05e0 53 49 56 45 20 6c 6f 63 6b 2e 20 49 66 20 69 74  SIVE lock. If it
b05f0 20 77 65 72 65 2c 20 61 6e 6f 74 68 65 72 20 70   were, another p
b0600 72 6f 63 65 73 73 20 6d 69 67 68 74 20 6f 70 65  rocess might ope
b0610 6e 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a  n the.        **
b0620 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20   database file, 
b0630 64 65 74 65 63 74 20 74 68 65 20 52 45 53 45 52  detect the RESER
b0640 56 45 44 20 6c 6f 63 6b 2c 20 61 6e 64 20 63 6f  VED lock, and co
b0650 6e 63 6c 75 64 65 20 74 68 61 74 20 74 68 65 0a  nclude that the.
b0660 20 20 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62          ** datab
b0670 61 73 65 20 69 73 20 73 61 66 65 20 74 6f 20 72  ase is safe to r
b0680 65 61 64 20 77 68 69 6c 65 20 74 68 69 73 20 70  ead while this p
b0690 72 6f 63 65 73 73 20 69 73 20 73 74 69 6c 6c 20  rocess is still 
b06a0 72 6f 6c 6c 69 6e 67 20 69 74 20 0a 20 20 20 20  rolling it .    
b06b0 20 20 20 20 2a 2a 20 62 61 63 6b 2e 0a 20 20 20      ** back..   
b06c0 20 20 20 20 20 2a 2a 20 0a 20 20 20 20 20 20 20       ** .       
b06d0 20 2a 2a 20 42 65 63 61 75 73 65 20 74 68 65 20   ** Because the 
b06e0 69 6e 74 65 72 6d 65 64 69 61 74 65 20 52 45 53  intermediate RES
b06f0 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 20 6e 6f  ERVED lock is no
b0700 74 20 72 65 71 75 65 73 74 65 64 2c 20 74 68 65  t requested, the
b0710 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 65 63 6f  .        ** seco
b0720 6e 64 20 70 72 6f 63 65 73 73 20 77 69 6c 6c 20  nd process will 
b0730 67 65 74 20 74 6f 20 74 68 69 73 20 70 6f 69 6e  get to this poin
b0740 74 20 69 6e 20 74 68 65 20 63 6f 64 65 20 61 6e  t in the code an
b0750 64 20 66 61 69 6c 20 74 6f 0a 20 20 20 20 20 20  d fail to.      
b0760 20 20 2a 2a 20 6f 62 74 61 69 6e 20 69 74 27 73    ** obtain it's
b0770 20 6f 77 6e 20 45 58 43 4c 55 53 49 56 45 20 6c   own EXCLUSIVE l
b0780 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ock on the datab
b0790 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 20 20  ase file..      
b07a0 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 20    */.        rc 
b07b0 3d 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28  = sqlite3OsLock(
b07c0 70 50 61 67 65 72 2d 3e 66 64 2c 20 45 58 43 4c  pPager->fd, EXCL
b07d0 55 53 49 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20  USIVE_LOCK);.   
b07e0 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
b07f0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
b0800 20 20 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b      pager_unlock
b0810 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20  (pPager);.      
b0820 20 20 20 20 72 65 74 75 72 6e 20 70 61 67 65 72      return pager
b0830 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72  _error(pPager, r
b0840 63 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  c);.        }.  
b0850 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74        pPager->st
b0860 61 74 65 20 3d 20 50 41 47 45 52 5f 45 58 43 4c  ate = PAGER_EXCL
b0870 55 53 49 56 45 3b 0a 20 0a 20 20 20 20 20 20 20  USIVE;. .       
b0880 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 6a 6f 75   /* Open the jou
b0890 72 6e 61 6c 20 66 6f 72 20 72 65 61 64 69 6e 67  rnal for reading
b08a0 20 6f 6e 6c 79 2e 20 20 52 65 74 75 72 6e 20 53   only.  Return S
b08b0 51 4c 49 54 45 5f 42 55 53 59 20 69 66 0a 20 20  QLITE_BUSY if.  
b08c0 20 20 20 20 20 20 2a 2a 20 77 65 20 61 72 65 20        ** we are 
b08d0 75 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 74  unable to open t
b08e0 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  he journal file.
b08f0 20 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20   .        **.   
b0900 20 20 20 20 20 2a 2a 20 54 68 65 20 6a 6f 75 72       ** The jour
b0910 6e 61 6c 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f  nal file does no
b0920 74 20 6e 65 65 64 20 74 6f 20 62 65 20 6c 6f 63  t need to be loc
b0930 6b 65 64 20 69 74 73 65 6c 66 2e 20 20 54 68 65  ked itself.  The
b0940 0a 20 20 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72  .        ** jour
b0950 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e 65 76 65  nal file is neve
b0960 72 20 6f 70 65 6e 20 75 6e 6c 65 73 73 20 74 68  r open unless th
b0970 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
b0980 66 69 6c 65 20 68 6f 6c 64 73 0a 20 20 20 20 20  file holds.     
b0990 20 20 20 2a 2a 20 61 20 77 72 69 74 65 20 6c 6f     ** a write lo
b09a0 63 6b 2c 20 73 6f 20 74 68 65 72 65 20 69 73 20  ck, so there is 
b09b0 6e 65 76 65 72 20 61 6e 79 20 63 68 61 6e 63 65  never any chance
b09c0 20 6f 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 0a   of two or more.
b09d0 20 20 20 20 20 20 20 20 2a 2a 20 70 72 6f 63 65          ** proce
b09e0 73 73 65 73 20 6f 70 65 6e 69 6e 67 20 74 68 65  sses opening the
b09f0 20 6a 6f 75 72 6e 61 6c 20 61 74 20 74 68 65 20   journal at the 
b0a00 73 61 6d 65 20 74 69 6d 65 2e 0a 20 20 20 20 20  same time..     
b0a10 20 20 20 2a 2a 0a 09 2a 2a 20 4f 70 65 6e 20 74     **..** Open t
b0a20 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 72  he journal for r
b0a30 65 61 64 2f 77 72 69 74 65 20 61 63 63 65 73 73  ead/write access
b0a40 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73  . This is becaus
b0a50 65 20 69 6e 20 0a 09 2a 2a 20 65 78 63 6c 75 73  e in ..** exclus
b0a60 69 76 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 20  ive-access mode 
b0a70 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  the file descrip
b0a80 74 6f 72 20 77 69 6c 6c 20 62 65 20 6b 65 70 74  tor will be kept
b0a90 20 6f 70 65 6e 20 61 6e 64 0a 20 20 20 20 20 20   open and.      
b0aa0 20 20 2a 2a 20 70 6f 73 73 69 62 6c 79 20 75 73    ** possibly us
b0ab0 65 64 20 66 6f 72 20 61 20 74 72 61 6e 73 61 63  ed for a transac
b0ac0 74 69 6f 6e 20 6c 61 74 65 72 20 6f 6e 2e 20 4f  tion later on. O
b0ad0 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2c 20  n some systems, 
b0ae0 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f  the.        ** O
b0af0 73 54 72 75 6e 63 61 74 65 28 29 20 63 61 6c 6c  sTruncate() call
b0b00 20 75 73 65 64 20 69 6e 20 65 78 63 6c 75 73 69   used in exclusi
b0b10 76 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 20 61  ve-access mode a
b0b20 6c 73 6f 20 72 65 71 75 69 72 65 73 0a 20 20 20  lso requires.   
b0b30 20 20 20 20 20 2a 2a 20 61 20 72 65 61 64 2f 77       ** a read/w
b0b40 72 69 74 65 20 66 69 6c 65 20 68 61 6e 64 6c 65  rite file handle
b0b50 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
b0b60 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
b0b70 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20 69  _BUSY;.        i
b0b80 66 28 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65  f( sqlite3OsFile
b0b90 45 78 69 73 74 73 28 70 50 61 67 65 72 2d 3e 7a  Exists(pPager->z
b0ba0 4a 6f 75 72 6e 61 6c 29 20 29 7b 0a 20 20 20 20  Journal) ){.    
b0bb0 20 20 20 20 20 20 69 6e 74 20 72 6f 3b 0a 20 20        int ro;.  
b0bc0 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
b0bd0 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c  !pPager->tempFil
b0be0 65 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72  e );.          r
b0bf0 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65  c = sqlite3OsOpe
b0c00 6e 52 65 61 64 57 72 69 74 65 28 70 50 61 67 65  nReadWrite(pPage
b0c10 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 26 70 50  r->zJournal, &pP
b0c20 61 67 65 72 2d 3e 6a 66 64 2c 20 26 72 6f 29 3b  ager->jfd, &ro);
b0c30 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
b0c40 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc!=SQLITE_OK
b0c50 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 66 64 20   || pPager->jfd 
b0c60 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
b0c70 20 72 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 20   ro ){.         
b0c80 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42     rc = SQLITE_B
b0c90 55 53 59 3b 0a 20 20 20 20 20 20 20 20 20 20 20  USY;.           
b0ca0 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28   sqlite3OsClose(
b0cb0 26 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20  &pPager->jfd);. 
b0cc0 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
b0cd0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
b0ce0 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
b0cf0 7b 0a 20 20 20 20 20 20 20 20 20 20 70 61 67 65  {.          page
b0d00 72 5f 75 6e 6c 6f 63 6b 28 70 50 61 67 65 72 29  r_unlock(pPager)
b0d10 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  ;.          retu
b0d20 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a  rn SQLITE_BUSY;.
b0d30 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
b0d40 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
b0d50 6c 4f 70 65 6e 20 3d 20 31 3b 0a 20 20 20 20 20  lOpen = 1;.     
b0d60 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e     pPager->journ
b0d70 61 6c 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20  alStarted = 0;. 
b0d80 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a         pPager->j
b0d90 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20  ournalOff = 0;. 
b0da0 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73         pPager->s
b0db0 65 74 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20  etMaster = 0;.  
b0dc0 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f        pPager->jo
b0dd0 75 72 6e 61 6c 48 64 72 20 3d 20 30 3b 0a 20 0a  urnalHdr = 0;. .
b0de0 20 20 20 20 20 20 20 20 2f 2a 20 50 6c 61 79 62          /* Playb
b0df0 61 63 6b 20 61 6e 64 20 64 65 6c 65 74 65 20 74  ack and delete t
b0e00 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 44 72 6f  he journal.  Dro
b0e10 70 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77  p the database w
b0e20 72 69 74 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  rite.        ** 
b0e30 6c 6f 63 6b 20 61 6e 64 20 72 65 61 63 71 75 69  lock and reacqui
b0e40 72 65 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b  re the read lock
b0e50 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
b0e60 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f       rc = pager_
b0e70 70 6c 61 79 62 61 63 6b 28 70 50 61 67 65 72 2c  playback(pPager,
b0e80 20 31 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28   1);.        if(
b0e90 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
b0ea0 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  {.          retu
b0eb0 72 6e 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70  rn pager_error(p
b0ec0 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 20  Pager, rc);.    
b0ed0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 73      }.        as
b0ee0 73 65 72 74 28 70 50 61 67 65 72 2d 3e 73 74 61  sert(pPager->sta
b0ef0 74 65 3d 3d 50 41 47 45 52 5f 53 48 41 52 45 44  te==PAGER_SHARED
b0f00 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 20 20 20   || .           
b0f10 20 28 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73   (pPager->exclus
b0f20 69 76 65 4d 6f 64 65 20 26 26 20 70 50 61 67 65  iveMode && pPage
b0f30 72 2d 3e 73 74 61 74 65 3e 50 41 47 45 52 5f 53  r->state>PAGER_S
b0f40 48 41 52 45 44 29 0a 20 20 20 20 20 20 20 20 29  HARED).        )
b0f50 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
b0f60 20 69 66 28 20 70 50 61 67 65 72 2d 3e 70 41 6c   if( pPager->pAl
b0f70 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  l ){.        /* 
b0f80 54 68 65 20 73 68 61 72 65 64 2d 6c 6f 63 6b 20  The shared-lock 
b0f90 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 61 63  has just been ac
b0fa0 71 75 69 72 65 64 20 6f 6e 20 74 68 65 20 64 61  quired on the da
b0fb0 74 61 62 61 73 65 20 66 69 6c 65 0a 20 20 20 20  tabase file.    
b0fc0 20 20 20 20 2a 2a 20 61 6e 64 20 74 68 65 72 65      ** and there
b0fd0 20 61 72 65 20 61 6c 72 65 61 64 79 20 70 61 67   are already pag
b0fe0 65 73 20 69 6e 20 74 68 65 20 63 61 63 68 65 20  es in the cache 
b0ff0 28 66 72 6f 6d 20 61 20 70 72 65 76 69 6f 75 73  (from a previous
b1000 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 65 61 64  .        ** read
b1010 20 6f 72 20 77 72 69 74 65 20 74 72 61 6e 73 61   or write transa
b1020 63 74 69 6f 6e 29 2e 20 20 43 68 65 63 6b 20 74  ction).  Check t
b1030 6f 20 73 65 65 20 69 66 20 74 68 65 20 64 61 74  o see if the dat
b1040 61 62 61 73 65 0a 20 20 20 20 20 20 20 20 2a 2a  abase.        **
b1050 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 69   has been modifi
b1060 65 64 2e 20 20 49 66 20 74 68 65 20 64 61 74 61  ed.  If the data
b1070 62 61 73 65 20 68 61 73 20 63 68 61 6e 67 65 64  base has changed
b1080 2c 20 66 6c 75 73 68 20 74 68 65 0a 20 20 20 20  , flush the.    
b1090 20 20 20 20 2a 2a 20 63 61 63 68 65 2e 0a 20 20      ** cache..  
b10a0 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20        **.       
b10b0 20 2a 2a 20 44 61 74 61 62 61 73 65 20 63 68 61   ** Database cha
b10c0 6e 67 65 73 20 69 73 20 64 65 74 65 63 74 65 64  nges is detected
b10d0 20 62 79 20 6c 6f 6f 6b 69 6e 67 20 61 74 20 31   by looking at 1
b10e0 35 20 62 79 74 65 73 20 62 65 67 69 6e 6e 69 6e  5 bytes beginnin
b10f0 67 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 74 20  g.        ** at 
b1100 6f 66 66 73 65 74 20 32 34 20 69 6e 74 6f 20 74  offset 24 into t
b1110 68 65 20 66 69 6c 65 2e 20 20 54 68 65 20 66 69  he file.  The fi
b1120 72 73 74 20 34 20 6f 66 20 74 68 65 73 65 20 31  rst 4 of these 1
b1130 36 20 62 79 74 65 73 20 61 72 65 0a 20 20 20 20  6 bytes are.    
b1140 20 20 20 20 2a 2a 20 61 20 33 32 2d 62 69 74 20      ** a 32-bit 
b1150 63 6f 75 6e 74 65 72 20 74 68 61 74 20 69 73 20  counter that is 
b1160 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 69 74 68  incremented with
b1170 20 65 61 63 68 20 63 68 61 6e 67 65 2e 20 20 54   each change.  T
b1180 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 74  he.        ** ot
b1190 68 65 72 20 62 79 74 65 73 20 63 68 61 6e 67 65  her bytes change
b11a0 20 72 61 6e 64 6f 6d 6c 79 20 77 69 74 68 20 65   randomly with e
b11b0 61 63 68 20 66 69 6c 65 20 63 68 61 6e 67 65 20  ach file change 
b11c0 77 68 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20  when.        ** 
b11d0 61 20 63 6f 64 65 63 20 69 73 20 69 6e 20 75 73  a codec is in us
b11e0 65 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 0a 20  e..        ** . 
b11f0 20 20 20 20 20 20 20 2a 2a 20 54 68 65 72 65 20         ** There 
b1200 69 73 20 61 20 76 61 6e 69 73 68 69 6e 67 6c 79  is a vanishingly
b1210 20 73 6d 61 6c 6c 20 63 68 61 6e 63 65 20 74 68   small chance th
b1220 61 74 20 61 20 63 68 61 6e 67 65 20 77 69 6c 6c  at a change will
b1230 20 6e 6f 74 20 62 65 20 0a 20 20 20 20 20 20 20   not be .       
b1240 20 2a 2a 20 64 65 74 65 63 74 65 64 2e 20 20 54   ** detected.  T
b1250 68 65 20 63 68 61 6e 63 65 20 6f 66 20 61 6e 20  he chance of an 
b1260 75 6e 64 65 74 65 63 74 65 64 20 63 68 61 6e 67  undetected chang
b1270 65 20 69 73 20 73 6f 20 73 6d 61 6c 6c 20 74 68  e is so small th
b1280 61 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 74  at.        ** it
b1290 20 63 61 6e 20 62 65 20 6e 65 67 6c 65 63 74 65   can be neglecte
b12a0 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  d..        */.  
b12b0 20 20 20 20 20 20 63 68 61 72 20 64 62 46 69 6c        char dbFil
b12c0 65 56 65 72 73 5b 73 69 7a 65 6f 66 28 70 50 61  eVers[sizeof(pPa
b12d0 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29  ger->dbFileVers)
b12e0 5d 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ];.        sqlit
b12f0 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74  e3PagerPagecount
b1300 28 70 50 61 67 65 72 29 3b 0a 0a 20 20 20 20 20  (pPager);..     
b1310 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65     if( pPager->e
b1320 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 20 20  rrCode ){.      
b1330 20 20 20 20 72 65 74 75 72 6e 20 70 50 61 67 65      return pPage
b1340 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 20 20  r->errCode;.    
b1350 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 69      }..        i
b1360 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a  f( pPager->dbSiz
b1370 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  e>0 ){.         
b1380 20 49 4f 54 52 41 43 45 28 28 22 43 4b 56 45 52   IOTRACE(("CKVER
b1390 53 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67  S %p %d\n", pPag
b13a0 65 72 2c 20 73 69 7a 65 6f 66 28 64 62 46 69 6c  er, sizeof(dbFil
b13b0 65 56 65 72 73 29 29 29 3b 0a 20 20 20 20 20 20  eVers)));.      
b13c0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
b13d0 4f 73 53 65 65 6b 28 70 50 61 67 65 72 2d 3e 66  OsSeek(pPager->f
b13e0 64 2c 20 32 34 29 3b 0a 20 20 20 20 20 20 20 20  d, 24);.        
b13f0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
b1400 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
b1410 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
b1420 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
b1430 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
b1440 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d 3e 66  OsRead(pPager->f
b1450 64 2c 20 26 64 62 46 69 6c 65 56 65 72 73 2c 20  d, &dbFileVers, 
b1460 73 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72  sizeof(dbFileVer
b1470 73 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  s));.          i
b1480 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
b1490 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
b14a0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
b14b0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
b14c0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
b14d0 6d 65 6d 73 65 74 28 64 62 46 69 6c 65 56 65 72  memset(dbFileVer
b14e0 73 2c 20 30 2c 20 73 69 7a 65 6f 66 28 64 62 46  s, 0, sizeof(dbF
b14f0 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 20 20 20  ileVers));.     
b1500 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 69 66     }..        if
b1510 28 20 6d 65 6d 63 6d 70 28 70 50 61 67 65 72 2d  ( memcmp(pPager-
b1520 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 64 62 46  >dbFileVers, dbF
b1530 69 6c 65 56 65 72 73 2c 20 73 69 7a 65 6f 66 28  ileVers, sizeof(
b1540 64 62 46 69 6c 65 56 65 72 73 29 29 21 3d 30 20  dbFileVers))!=0 
b1550 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 61 67  ){.          pag
b1560 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 29  er_reset(pPager)
b1570 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
b1580 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73    }.    }.    as
b1590 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 78  sert( pPager->ex
b15a0 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c 20 70  clusiveMode || p
b15b0 50 61 67 65 72 2d 3e 73 74 61 74 65 3c 3d 50 41  Pager->state<=PA
b15c0 47 45 52 5f 53 48 41 52 45 44 20 29 3b 0a 20 20  GER_SHARED );.  
b15d0 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74    if( pPager->st
b15e0 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f 43  ate==PAGER_UNLOC
b15f0 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65  K ){.      pPage
b1600 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52  r->state = PAGER
b1610 5f 53 48 41 52 45 44 3b 0a 20 20 20 20 7d 0a 20  _SHARED;.    }. 
b1620 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
b1630 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61  .}../*.** Alloca
b1640 74 65 20 61 20 50 67 48 64 72 20 6f 62 6a 65 63  te a PgHdr objec
b1650 74 2e 20 20 20 45 69 74 68 65 72 20 63 72 65 61  t.   Either crea
b1660 74 65 20 61 20 6e 65 77 20 6f 6e 65 20 6f 72 20  te a new one or 
b1670 72 65 75 73 65 0a 2a 2a 20 61 6e 20 65 78 69 73  reuse.** an exis
b1680 74 69 6e 67 20 6f 6e 65 20 74 68 61 74 20 69 73  ting one that is
b1690 20 6e 6f 74 20 6f 74 68 65 72 77 69 73 65 20 69   not otherwise i
b16a0 6e 20 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e  n use..**.** A n
b16b0 65 77 20 50 67 48 64 72 20 73 74 72 75 63 74 75  ew PgHdr structu
b16c0 72 65 20 69 73 20 63 72 65 61 74 65 64 20 69 66  re is created if
b16d0 20 61 6e 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c   any of the foll
b16e0 6f 77 69 6e 67 20 61 72 65 0a 2a 2a 20 74 72 75  owing are.** tru
b16f0 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29  e:.**.**     (1)
b1700 20 20 57 65 20 68 61 76 65 20 6e 6f 74 20 65 78    We have not ex
b1710 63 65 65 64 65 64 20 6f 75 72 20 6d 61 78 69 6d  ceeded our maxim
b1720 75 6d 20 61 6c 6c 6f 63 61 74 65 64 20 63 61 63  um allocated cac
b1730 68 65 20 73 69 7a 65 0a 2a 2a 20 20 20 20 20 20  he size.**      
b1740 20 20 20 20 61 73 20 73 65 74 20 62 79 20 74 68      as set by th
b1750 65 20 22 50 52 41 47 4d 41 20 63 61 63 68 65 5f  e "PRAGMA cache_
b1760 73 69 7a 65 22 20 63 6f 6d 6d 61 6e 64 2e 0a 2a  size" command..*
b1770 2a 0a 2a 2a 20 20 20 20 20 28 32 29 20 20 54 68  *.**     (2)  Th
b1780 65 72 65 20 61 72 65 20 6e 6f 20 75 6e 75 73 65  ere are no unuse
b1790 64 20 50 67 48 64 72 20 6f 62 6a 65 63 74 73 20  d PgHdr objects 
b17a0 61 76 61 69 6c 61 62 6c 65 20 61 74 20 74 68 69  available at thi
b17b0 73 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 20 20  s time..**.**   
b17c0 20 20 28 33 29 20 20 54 68 69 73 20 69 73 20 61    (3)  This is a
b17d0 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61  n in-memory data
b17e0 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  base..**.**     
b17f0 28 34 29 20 20 54 68 65 72 65 20 61 72 65 20 6e  (4)  There are n
b1800 6f 20 50 67 48 64 72 20 6f 62 6a 65 63 74 73 20  o PgHdr objects 
b1810 74 68 61 74 20 64 6f 20 6e 6f 74 20 72 65 71 75  that do not requ
b1820 69 72 65 20 61 20 6a 6f 75 72 6e 61 6c 0a 2a 2a  ire a journal.**
b1830 20 20 20 20 20 20 20 20 20 20 66 69 6c 65 20 73            file s
b1840 79 6e 63 20 61 6e 64 20 61 20 73 79 6e 63 20 6f  ync and a sync o
b1850 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  f the journal fi
b1860 6c 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79 0a  le is currently.
b1870 2a 2a 20 20 20 20 20 20 20 20 20 20 70 72 6f 68  **          proh
b1880 69 62 69 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 74  ibited..**.** Ot
b1890 68 65 72 77 69 73 65 2c 20 72 65 75 73 65 20 61  herwise, reuse a
b18a0 6e 20 65 78 69 73 74 69 6e 67 20 50 67 48 64 72  n existing PgHdr
b18b0 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  .  In other word
b18c0 73 2c 20 72 65 75 73 65 20 61 6e 0a 2a 2a 20 65  s, reuse an.** e
b18d0 78 69 73 74 69 6e 67 20 50 67 48 64 72 20 69 66  xisting PgHdr if
b18e0 20 61 6c 6c 20 6f 66 20 74 68 65 20 66 6f 6c 6c   all of the foll
b18f0 6f 77 69 6e 67 20 61 72 65 20 74 72 75 65 3a 0a  owing are true:.
b1900 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 20 57  **.**     (1)  W
b1910 65 20 68 61 76 65 20 72 65 61 63 68 65 64 20 6f  e have reached o
b1920 72 20 65 78 63 65 65 64 65 64 20 74 68 65 20 6d  r exceeded the m
b1930 61 78 69 6d 75 6d 20 63 61 63 68 65 20 73 69 7a  aximum cache siz
b1940 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6c  e.**          al
b1950 6c 6f 77 65 64 20 62 79 20 22 50 52 41 47 4d 41  lowed by "PRAGMA
b1960 20 63 61 63 68 65 5f 73 69 7a 65 22 2e 0a 2a 2a   cache_size"..**
b1970 0a 2a 2a 20 20 20 20 20 28 32 29 20 20 54 68 65  .**     (2)  The
b1980 72 65 20 69 73 20 61 20 50 67 48 64 72 20 61 76  re is a PgHdr av
b1990 61 69 6c 61 62 6c 65 20 77 69 74 68 20 50 67 48  ailable with PgH
b19a0 64 72 2d 3e 6e 52 65 66 3d 3d 30 0a 2a 2a 0a 2a  dr->nRef==0.**.*
b19b0 2a 20 20 20 20 20 28 33 29 20 20 57 65 20 61 72  *     (3)  We ar
b19c0 65 20 6e 6f 74 20 69 6e 20 61 6e 20 69 6e 2d 6d  e not in an in-m
b19d0 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 0a 2a  emory database.*
b19e0 2a 0a 2a 2a 20 20 20 20 20 28 34 29 20 20 45 69  *.**     (4)  Ei
b19f0 74 68 65 72 20 74 68 65 72 65 20 69 73 20 61 6e  ther there is an
b1a00 20 61 76 61 69 6c 61 62 6c 65 20 50 67 48 64 72   available PgHdr
b1a10 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e   that does not n
b1a20 65 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  eed.**          
b1a30 74 6f 20 62 65 20 73 79 6e 63 65 64 20 74 6f 20  to be synced to 
b1a40 64 69 73 6b 20 6f 72 20 65 6c 73 65 20 64 69 73  disk or else dis
b1a50 6b 20 73 79 6e 63 69 6e 67 20 69 73 20 63 75 72  k syncing is cur
b1a60 72 65 6e 74 6c 79 0a 2a 2a 20 20 20 20 20 20 20  rently.**       
b1a70 20 20 20 61 6c 6c 6f 77 65 64 2e 0a 2a 2f 0a 73     allowed..*/.s
b1a80 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 41  tatic int pagerA
b1a90 6c 6c 6f 63 61 74 65 50 61 67 65 28 50 61 67 65  llocatePage(Page
b1aa0 72 20 2a 70 50 61 67 65 72 2c 20 50 67 48 64 72  r *pPager, PgHdr
b1ab0 20 2a 2a 70 70 50 67 29 7b 0a 20 20 69 6e 74 20   **ppPg){.  int 
b1ac0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
b1ad0 20 20 50 67 48 64 72 20 2a 70 50 67 3b 0a 0a 20    PgHdr *pPg;.. 
b1ae0 20 2f 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77   /* Create a new
b1af0 20 50 67 48 64 72 20 69 66 20 61 6e 79 20 6f 66   PgHdr if any of
b1b00 20 74 68 65 20 66 6f 75 72 20 63 6f 6e 64 69 74   the four condit
b1b10 69 6f 6e 73 20 64 65 66 69 6e 65 64 20 0a 20 20  ions defined .  
b1b20 2a 2a 20 61 62 6f 76 65 20 69 73 20 6d 65 74 3a  ** above is met:
b1b30 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72   */.  if( pPager
b1b40 2d 3e 6e 50 61 67 65 3c 70 50 61 67 65 72 2d 3e  ->nPage<pPager->
b1b50 6d 78 50 61 67 65 0a 20 20 20 7c 7c 20 70 50 61  mxPage.   || pPa
b1b60 67 65 72 2d 3e 70 46 69 72 73 74 3d 3d 30 20 0a  ger->pFirst==0 .
b1b70 20 20 20 7c 7c 20 4d 45 4d 44 42 0a 20 20 20 7c     || MEMDB.   |
b1b80 7c 20 28 70 50 61 67 65 72 2d 3e 70 46 69 72 73  | (pPager->pFirs
b1b90 74 53 79 6e 63 65 64 3d 3d 30 20 26 26 20 70 50  tSynced==0 && pP
b1ba0 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 29  ager->doNotSync)
b1bb0 0a 20 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50  .  ){.    if( pP
b1bc0 61 67 65 72 2d 3e 6e 50 61 67 65 3e 3d 70 50 61  ager->nPage>=pPa
b1bd0 67 65 72 2d 3e 6e 48 61 73 68 20 29 7b 0a 20 20  ger->nHash ){.  
b1be0 20 20 20 20 70 61 67 65 72 5f 72 65 73 69 7a 65      pager_resize
b1bf0 5f 68 61 73 68 5f 74 61 62 6c 65 28 70 50 61 67  _hash_table(pPag
b1c00 65 72 2c 0a 20 20 20 20 20 20 20 20 20 70 50 61  er,.         pPa
b1c10 67 65 72 2d 3e 6e 48 61 73 68 3c 32 35 36 20 3f  ger->nHash<256 ?
b1c20 20 32 35 36 20 3a 20 70 50 61 67 65 72 2d 3e 6e   256 : pPager->n
b1c30 48 61 73 68 2a 32 29 3b 0a 20 20 20 20 20 20 69  Hash*2);.      i
b1c40 66 28 20 70 50 61 67 65 72 2d 3e 6e 48 61 73 68  f( pPager->nHash
b1c50 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ==0 ){.        r
b1c60 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
b1c70 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 70  ;.        goto p
b1c80 61 67 65 72 5f 61 6c 6c 6f 63 61 74 65 5f 6f 75  ager_allocate_ou
b1c90 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  t;.      }.    }
b1ca0 0a 20 20 20 20 70 50 67 20 3d 20 73 71 6c 69 74  .    pPg = sqlit
b1cb0 65 4d 61 6c 6c 6f 63 52 61 77 28 20 73 69 7a 65  eMallocRaw( size
b1cc0 6f 66 28 2a 70 50 67 29 20 2b 20 70 50 61 67 65  of(*pPg) + pPage
b1cd0 72 2d 3e 70 61 67 65 53 69 7a 65 0a 20 20 20 20  r->pageSize.    
b1ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1cf0 20 20 20 20 20 20 20 20 2b 20 73 69 7a 65 6f 66          + sizeof
b1d00 28 75 33 32 29 20 2b 20 70 50 61 67 65 72 2d 3e  (u32) + pPager->
b1d10 6e 45 78 74 72 61 0a 20 20 20 20 20 20 20 20 20  nExtra.         
b1d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1d30 20 20 20 2b 20 4d 45 4d 44 42 2a 73 69 7a 65 6f     + MEMDB*sizeo
b1d40 66 28 50 67 48 69 73 74 6f 72 79 29 20 29 3b 0a  f(PgHistory) );.
b1d50 20 20 20 20 69 66 28 20 70 50 67 3d 3d 30 20 29      if( pPg==0 )
b1d60 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
b1d70 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
b1d80 20 67 6f 74 6f 20 70 61 67 65 72 5f 61 6c 6c 6f   goto pager_allo
b1d90 63 61 74 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a  cate_out;.    }.
b1da0 20 20 20 20 6d 65 6d 73 65 74 28 70 50 67 2c 20      memset(pPg, 
b1db0 30 2c 20 73 69 7a 65 6f 66 28 2a 70 50 67 29 29  0, sizeof(*pPg))
b1dc0 3b 0a 20 20 20 20 69 66 28 20 4d 45 4d 44 42 20  ;.    if( MEMDB 
b1dd0 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28  ){.      memset(
b1de0 50 47 48 44 52 5f 54 4f 5f 48 49 53 54 28 70 50  PGHDR_TO_HIST(pP
b1df0 67 2c 20 70 50 61 67 65 72 29 2c 20 30 2c 20 73  g, pPager), 0, s
b1e00 69 7a 65 6f 66 28 50 67 48 69 73 74 6f 72 79 29  izeof(PgHistory)
b1e10 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 67  );.    }.    pPg
b1e20 2d 3e 70 50 61 67 65 72 20 3d 20 70 50 61 67 65  ->pPager = pPage
b1e30 72 3b 0a 20 20 20 20 70 50 67 2d 3e 70 4e 65 78  r;.    pPg->pNex
b1e40 74 41 6c 6c 20 3d 20 70 50 61 67 65 72 2d 3e 70  tAll = pPager->p
b1e50 41 6c 6c 3b 0a 20 20 20 20 70 50 61 67 65 72 2d  All;.    pPager-
b1e60 3e 70 41 6c 6c 20 3d 20 70 50 67 3b 0a 20 20 20  >pAll = pPg;.   
b1e70 20 70 50 61 67 65 72 2d 3e 6e 50 61 67 65 2b 2b   pPager->nPage++
b1e80 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  ;.  }else{.    /
b1e90 2a 20 52 65 63 79 63 6c 65 20 61 6e 20 65 78 69  * Recycle an exi
b1ea0 73 74 69 6e 67 20 70 61 67 65 20 77 69 74 68 20  sting page with 
b1eb0 61 20 7a 65 72 6f 20 72 65 66 2d 63 6f 75 6e 74  a zero ref-count
b1ec0 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 61  . */.    rc = pa
b1ed0 67 65 72 5f 72 65 63 79 63 6c 65 28 70 50 61 67  ger_recycle(pPag
b1ee0 65 72 2c 20 31 2c 20 26 70 50 67 29 3b 0a 20 20  er, 1, &pPg);.  
b1ef0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
b1f00 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20 20 72  _BUSY ){.      r
b1f10 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  c = SQLITE_IOERR
b1f20 5f 42 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 7d 0a  _BLOCKED;.    }.
b1f30 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
b1f40 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67  TE_OK ){.      g
b1f50 6f 74 6f 20 70 61 67 65 72 5f 61 6c 6c 6f 63 61  oto pager_alloca
b1f60 74 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20  te_out;.    }.  
b1f70 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
b1f80 2d 3e 73 74 61 74 65 3e 3d 53 48 41 52 45 44 5f  ->state>=SHARED_
b1f90 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 61 73 73 65  LOCK );.    asse
b1fa0 72 74 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20 2a  rt(pPg);.  }.  *
b1fb0 70 70 50 67 20 3d 20 70 50 67 3b 0a 0a 70 61 67  ppPg = pPg;..pag
b1fc0 65 72 5f 61 6c 6c 6f 63 61 74 65 5f 6f 75 74 3a  er_allocate_out:
b1fd0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
b1fe0 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65  ./*.** Make sure
b1ff0 20 77 65 20 68 61 76 65 20 74 68 65 20 63 6f 6e   we have the con
b2000 74 65 6e 74 20 66 6f 72 20 61 20 70 61 67 65 2e  tent for a page.
b2010 20 20 49 66 20 74 68 65 20 70 61 67 65 20 77 61    If the page wa
b2020 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20  s.** previously 
b2030 61 63 71 75 69 72 65 64 20 77 69 74 68 20 6e 6f  acquired with no
b2040 43 6f 6e 74 65 6e 74 3d 3d 31 2c 20 74 68 65 6e  Content==1, then
b2050 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 77 61 73   the content was
b2060 0a 2a 2a 20 6a 75 73 74 20 69 6e 69 74 69 61 6c  .** just initial
b2070 69 7a 65 64 20 74 6f 20 7a 65 72 6f 73 20 69 6e  ized to zeros in
b2080 73 74 65 61 64 20 6f 66 20 62 65 69 6e 67 20 72  stead of being r
b2090 65 61 64 20 66 72 6f 6d 20 64 69 73 6b 2e 0a 2a  ead from disk..*
b20a0 2a 20 42 75 74 20 6e 6f 77 20 77 65 20 6e 65 65  * But now we nee
b20b0 64 20 74 68 65 20 72 65 61 6c 20 64 61 74 61 20  d the real data 
b20c0 6f 66 66 20 6f 66 20 64 69 73 6b 2e 20 20 53 6f  off of disk.  So
b20d0 20 6d 61 6b 65 20 73 75 72 65 20 77 65 0a 2a 2a   make sure we.**
b20e0 20 68 61 76 65 20 69 74 2e 20 20 52 65 61 64 20   have it.  Read 
b20f0 69 74 20 69 6e 20 69 66 20 77 65 20 64 6f 20 6e  it in if we do n
b2100 6f 74 20 68 61 76 65 20 69 74 20 61 6c 72 65 61  ot have it alrea
b2110 64 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  dy..*/.static in
b2120 74 20 70 61 67 65 72 5f 67 65 74 5f 63 6f 6e 74  t pager_get_cont
b2130 65 6e 74 28 50 67 48 64 72 20 2a 70 50 67 29 7b  ent(PgHdr *pPg){
b2140 0a 20 20 69 66 28 20 70 50 67 2d 3e 6e 65 65 64  .  if( pPg->need
b2150 52 65 61 64 20 29 7b 0a 20 20 20 20 69 6e 74 20  Read ){.    int 
b2160 72 63 20 3d 20 72 65 61 64 44 62 50 61 67 65 28  rc = readDbPage(
b2170 70 50 67 2d 3e 70 50 61 67 65 72 2c 20 70 50 67  pPg->pPager, pPg
b2180 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20  , pPg->pgno);.  
b2190 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
b21a0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 67  _OK ){.      pPg
b21b0 2d 3e 6e 65 65 64 52 65 61 64 20 3d 20 30 3b 0a  ->needRead = 0;.
b21c0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
b21d0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
b21e0 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  }.  }.  return S
b21f0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
b2200 2a 2a 20 41 63 71 75 69 72 65 20 61 20 70 61 67  ** Acquire a pag
b2210 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 61 64 20  e..**.** A read 
b2220 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 69 73 6b  lock on the disk
b2230 20 66 69 6c 65 20 69 73 20 6f 62 74 61 69 6e 65   file is obtaine
b2240 64 20 77 68 65 6e 20 74 68 65 20 66 69 72 73 74  d when the first
b2250 20 70 61 67 65 20 69 73 20 61 63 71 75 69 72 65   page is acquire
b2260 64 2e 20 0a 2a 2a 20 54 68 69 73 20 72 65 61 64  d. .** This read
b2270 20 6c 6f 63 6b 20 69 73 20 64 72 6f 70 70 65 64   lock is dropped
b2280 20 77 68 65 6e 20 74 68 65 20 6c 61 73 74 20 70   when the last p
b2290 61 67 65 20 69 73 20 72 65 6c 65 61 73 65 64 2e  age is released.
b22a0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
b22b0 69 6e 65 20 77 6f 72 6b 73 20 66 6f 72 20 61 6e  ine works for an
b22c0 79 20 70 61 67 65 20 6e 75 6d 62 65 72 20 67 72  y page number gr
b22d0 65 61 74 65 72 20 74 68 61 6e 20 30 2e 20 20 49  eater than 0.  I
b22e0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a  f the database.*
b22f0 2a 20 66 69 6c 65 20 69 73 20 73 6d 61 6c 6c 65  * file is smalle
b2300 72 20 74 68 61 6e 20 74 68 65 20 72 65 71 75 65  r than the reque
b2310 73 74 65 64 20 70 61 67 65 2c 20 74 68 65 6e 20  sted page, then 
b2320 6e 6f 20 61 63 74 75 61 6c 20 64 69 73 6b 0a 2a  no actual disk.*
b2330 2a 20 72 65 61 64 20 6f 63 63 75 72 73 20 61 6e  * read occurs an
b2340 64 20 74 68 65 20 6d 65 6d 6f 72 79 20 69 6d 61  d the memory ima
b2350 67 65 20 6f 66 20 74 68 65 20 70 61 67 65 20 69  ge of the page i
b2360 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f  s initialized to
b2370 0a 2a 2a 20 61 6c 6c 20 7a 65 72 6f 73 2e 20 20  .** all zeros.  
b2380 54 68 65 20 65 78 74 72 61 20 64 61 74 61 20 61  The extra data a
b2390 70 70 65 6e 64 65 64 20 74 6f 20 61 20 70 61 67  ppended to a pag
b23a0 65 20 69 73 20 61 6c 77 61 79 73 20 69 6e 69 74  e is always init
b23b0 69 61 6c 69 7a 65 64 0a 2a 2a 20 74 6f 20 7a 65  ialized.** to ze
b23c0 72 6f 73 20 74 68 65 20 66 69 72 73 74 20 74 69  ros the first ti
b23d0 6d 65 20 61 20 70 61 67 65 20 69 73 20 6c 6f 61  me a page is loa
b23e0 64 65 64 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 2e  ded into memory.
b23f0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 63 71 75 69  .**.** The acqui
b2400 73 69 74 69 6f 6e 20 6d 69 67 68 74 20 66 61 69  sition might fai
b2410 6c 20 66 6f 72 20 73 65 76 65 72 61 6c 20 72 65  l for several re
b2420 61 73 6f 6e 73 2e 20 20 49 6e 20 61 6c 6c 20 63  asons.  In all c
b2430 61 73 65 73 2c 0a 2a 2a 20 61 6e 20 61 70 70 72  ases,.** an appr
b2440 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 63 6f  opriate error co
b2450 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  de is returned a
b2460 6e 64 20 2a 70 70 50 61 67 65 20 69 73 20 73 65  nd *ppPage is se
b2470 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a  t to NULL..**.**
b2480 20 53 65 65 20 61 6c 73 6f 20 73 71 6c 69 74 65   See also sqlite
b2490 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 29 2e 20  3PagerLookup(). 
b24a0 20 42 6f 74 68 20 74 68 69 73 20 72 6f 75 74 69   Both this routi
b24b0 6e 65 20 61 6e 64 20 4c 6f 6f 6b 75 70 28 29 20  ne and Lookup() 
b24c0 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 66 69  attempt.** to fi
b24d0 6e 64 20 61 20 70 61 67 65 20 69 6e 20 74 68 65  nd a page in the
b24e0 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65   in-memory cache
b24f0 20 66 69 72 73 74 2e 20 20 49 66 20 74 68 65 20   first.  If the 
b2500 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65  page is not alre
b2510 61 64 79 0a 2a 2a 20 69 6e 20 6d 65 6d 6f 72 79  ady.** in memory
b2520 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 67  , this routine g
b2530 6f 65 73 20 74 6f 20 64 69 73 6b 20 74 6f 20 72  oes to disk to r
b2540 65 61 64 20 69 74 20 69 6e 20 77 68 65 72 65 61  ead it in wherea
b2550 73 20 4c 6f 6f 6b 75 70 28 29 0a 2a 2a 20 6a 75  s Lookup().** ju
b2560 73 74 20 72 65 74 75 72 6e 73 20 30 2e 20 20 54  st returns 0.  T
b2570 68 69 73 20 72 6f 75 74 69 6e 65 20 61 63 71 75  his routine acqu
b2580 69 72 65 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b  ires a read-lock
b2590 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20   the first time 
b25a0 69 74 0a 2a 2a 20 68 61 73 20 74 6f 20 67 6f 20  it.** has to go 
b25b0 74 6f 20 64 69 73 6b 2c 20 61 6e 64 20 63 6f 75  to disk, and cou
b25c0 6c 64 20 61 6c 73 6f 20 70 6c 61 79 62 61 63 6b  ld also playback
b25d0 20 61 6e 20 6f 6c 64 20 6a 6f 75 72 6e 61 6c 20   an old journal 
b25e0 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a  if necessary..**
b25f0 20 53 69 6e 63 65 20 4c 6f 6f 6b 75 70 28 29 20   Since Lookup() 
b2600 6e 65 76 65 72 20 67 6f 65 73 20 74 6f 20 64 69  never goes to di
b2610 73 6b 2c 20 69 74 20 6e 65 76 65 72 20 68 61 73  sk, it never has
b2620 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 6c 6f   to deal with lo
b2630 63 6b 73 0a 2a 2a 20 6f 72 20 6a 6f 75 72 6e 61  cks.** or journa
b2640 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 49  l files..**.** I
b2650 66 20 6e 6f 43 6f 6e 74 65 6e 74 20 69 73 20 66  f noContent is f
b2660 61 6c 73 65 2c 20 74 68 65 20 70 61 67 65 20 63  alse, the page c
b2670 6f 6e 74 65 6e 74 73 20 61 72 65 20 61 63 74 75  ontents are actu
b2680 61 6c 6c 79 20 72 65 61 64 20 66 72 6f 6d 20 64  ally read from d
b2690 69 73 6b 2e 0a 2a 2a 20 49 66 20 6e 6f 43 6f 6e  isk..** If noCon
b26a0 74 65 6e 74 20 69 73 20 74 72 75 65 2c 20 69 74  tent is true, it
b26b0 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 64   means that we d
b26c0 6f 20 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 74  o not care about
b26d0 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a   the contents.**
b26e0 20 6f 66 20 74 68 65 20 70 61 67 65 20 61 74 20   of the page at 
b26f0 74 68 69 73 20 74 69 6d 65 2c 20 73 6f 20 64 6f  this time, so do
b2700 20 6e 6f 74 20 64 6f 20 61 20 64 69 73 6b 20 72   not do a disk r
b2710 65 61 64 2e 20 20 4a 75 73 74 20 66 69 6c 6c 20  ead.  Just fill 
b2720 69 6e 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 63  in the.** page c
b2730 6f 6e 74 65 6e 74 20 77 69 74 68 20 7a 65 72 6f  ontent with zero
b2740 73 2e 20 20 42 75 74 20 6d 61 72 6b 20 74 68 65  s.  But mark the
b2750 20 66 61 63 74 20 74 68 61 74 20 77 65 20 68 61   fact that we ha
b2760 76 65 20 6e 6f 74 20 72 65 61 64 20 74 68 65 0a  ve not read the.
b2770 2a 2a 20 63 6f 6e 74 65 6e 74 20 62 79 20 73 65  ** content by se
b2780 74 74 69 6e 67 20 74 68 65 20 50 67 48 64 72 2e  tting the PgHdr.
b2790 6e 65 65 64 52 65 61 64 20 66 6c 61 67 2e 20 20  needRead flag.  
b27a0 4c 61 74 65 72 20 6f 6e 2c 20 69 66 20 0a 2a 2a  Later on, if .**
b27b0 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
b27c0 74 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20 6f  te() is called o
b27d0 6e 20 74 68 69 73 20 70 61 67 65 20 6f 72 20 69  n this page or i
b27e0 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  f this routine i
b27f0 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 61 67 61 69  s.** called agai
b2800 6e 20 77 69 74 68 20 6e 6f 43 6f 6e 74 65 6e 74  n with noContent
b2810 3d 3d 30 2c 20 74 68 61 74 20 6d 65 61 6e 73 20  ==0, that means 
b2820 74 68 61 74 20 74 68 65 20 63 6f 6e 74 65 6e 74  that the content
b2830 20 69 73 20 6e 65 65 64 65 64 0a 2a 2a 20 61 6e   is needed.** an
b2840 64 20 74 68 65 20 64 69 73 6b 20 72 65 61 64 20  d the disk read 
b2850 73 68 6f 75 6c 64 20 6f 63 63 75 72 20 61 74 20  should occur at 
b2860 74 68 61 74 20 70 6f 69 6e 74 2e 0a 2a 2f 0a 53  that point..*/.S
b2870 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
b2880 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 63  t sqlite3PagerAc
b2890 71 75 69 72 65 28 0a 20 20 50 61 67 65 72 20 2a  quire(.  Pager *
b28a0 70 50 61 67 65 72 2c 20 20 20 20 20 20 2f 2a 20  pPager,      /* 
b28b0 54 68 65 20 70 61 67 65 72 20 6f 70 65 6e 20 6f  The pager open o
b28c0 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
b28d0 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67  ile */.  Pgno pg
b28e0 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  no,          /* 
b28f0 50 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 66  Page number to f
b2900 65 74 63 68 20 2a 2f 0a 20 20 44 62 50 61 67 65  etch */.  DbPage
b2910 20 2a 2a 70 70 50 61 67 65 2c 20 20 20 20 2f 2a   **ppPage,    /*
b2920 20 57 72 69 74 65 20 61 20 70 6f 69 6e 74 65 72   Write a pointer
b2930 20 74 6f 20 74 68 65 20 70 61 67 65 20 68 65 72   to the page her
b2940 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e  e */.  int noCon
b2950 74 65 6e 74 20 20 20 20 20 20 20 2f 2a 20 44 6f  tent       /* Do
b2960 20 6e 6f 74 20 62 6f 74 68 65 72 20 72 65 61 64   not bother read
b2970 69 6e 67 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d  ing content from
b2980 20 64 69 73 6b 20 69 66 20 74 72 75 65 20 2a 2f   disk if true */
b2990 0a 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 67  .){.  PgHdr *pPg
b29a0 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61  ;.  int rc;..  a
b29b0 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
b29c0 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f  tate==PAGER_UNLO
b29d0 43 4b 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6e 52  CK || pPager->nR
b29e0 65 66 3e 30 20 7c 7c 20 70 67 6e 6f 3d 3d 31 20  ef>0 || pgno==1 
b29f0 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 61 78  );..  /* The max
b2a00 69 6d 75 6d 20 70 61 67 65 20 6e 75 6d 62 65 72  imum page number
b2a10 20 69 73 20 32 5e 33 31 2e 20 52 65 74 75 72 6e   is 2^31. Return
b2a20 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20   SQLITE_CORRUPT 
b2a30 69 66 20 61 20 70 61 67 65 0a 20 20 2a 2a 20 6e  if a page.  ** n
b2a40 75 6d 62 65 72 20 67 72 65 61 74 65 72 20 74 68  umber greater th
b2a50 61 6e 20 74 68 69 73 2c 20 6f 72 20 7a 65 72 6f  an this, or zero
b2a60 2c 20 69 73 20 72 65 71 75 65 73 74 65 64 2e 0a  , is requested..
b2a70 20 20 2a 2f 0a 20 20 69 66 28 20 70 67 6e 6f 3e    */.  if( pgno>
b2a80 50 41 47 45 52 5f 4d 41 58 5f 50 47 4e 4f 20 7c  PAGER_MAX_PGNO |
b2a90 7c 20 70 67 6e 6f 3d 3d 30 20 7c 7c 20 70 67 6e  | pgno==0 || pgn
b2aa0 6f 3d 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f  o==PAGER_MJ_PGNO
b2ab0 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20  (pPager) ){.    
b2ac0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
b2ad0 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a  RRUPT_BKPT;.  }.
b2ae0 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20  .  /* Make sure 
b2af0 77 65 20 68 61 76 65 20 6e 6f 74 20 68 69 74 20  we have not hit 
b2b00 61 6e 79 20 63 72 69 74 69 63 61 6c 20 65 72 72  any critical err
b2b10 6f 72 73 2e 0a 20 20 2a 2f 20 0a 20 20 61 73 73  ors..  */ .  ass
b2b20 65 72 74 28 20 70 50 61 67 65 72 21 3d 30 20 29  ert( pPager!=0 )
b2b30 3b 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 30 3b  ;.  *ppPage = 0;
b2b40 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65  .  if( pPager->e
b2b50 72 72 43 6f 64 65 20 26 26 20 70 50 61 67 65 72  rrCode && pPager
b2b60 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51 4c 49 54  ->errCode!=SQLIT
b2b70 45 5f 46 55 4c 4c 20 29 7b 0a 20 20 20 20 72 65  E_FULL ){.    re
b2b80 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72  turn pPager->err
b2b90 43 6f 64 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  Code;.  }..  /* 
b2ba0 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66  If this is the f
b2bb0 69 72 73 74 20 70 61 67 65 20 61 63 63 65 73 73  irst page access
b2bc0 65 64 2c 20 74 68 65 6e 20 67 65 74 20 61 20 53  ed, then get a S
b2bd0 48 41 52 45 44 20 6c 6f 63 6b 0a 20 20 2a 2a 20  HARED lock.  ** 
b2be0 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
b2bf0 66 69 6c 65 2e 20 70 61 67 65 72 53 68 61 72 65  file. pagerShare
b2c00 64 4c 6f 63 6b 28 29 20 69 73 20 61 20 6e 6f 2d  dLock() is a no-
b2c10 6f 70 20 69 66 20 0a 20 20 2a 2a 20 61 20 64 61  op if .  ** a da
b2c20 74 61 62 61 73 65 20 6c 6f 63 6b 20 69 73 20 61  tabase lock is a
b2c30 6c 72 65 61 64 79 20 68 65 6c 64 2e 0a 20 20 2a  lready held..  *
b2c40 2f 0a 20 20 72 63 20 3d 20 70 61 67 65 72 53 68  /.  rc = pagerSh
b2c50 61 72 65 64 4c 6f 63 6b 28 70 50 61 67 65 72 29  aredLock(pPager)
b2c60 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
b2c70 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74  TE_OK ){.    ret
b2c80 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73  urn rc;.  }.  as
b2c90 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74  sert( pPager->st
b2ca0 61 74 65 21 3d 50 41 47 45 52 5f 55 4e 4c 4f 43  ate!=PAGER_UNLOC
b2cb0 4b 20 29 3b 0a 0a 20 20 70 50 67 20 3d 20 70 61  K );..  pPg = pa
b2cc0 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65  ger_lookup(pPage
b2cd0 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20  r, pgno);.  if( 
b2ce0 70 50 67 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a  pPg==0 ){.    /*
b2cf0 20 54 68 65 20 72 65 71 75 65 73 74 65 64 20 70   The requested p
b2d00 61 67 65 20 69 73 20 6e 6f 74 20 69 6e 20 74 68  age is not in th
b2d10 65 20 70 61 67 65 20 63 61 63 68 65 2e 20 2a 2f  e page cache. */
b2d20 0a 20 20 20 20 69 6e 74 20 6e 4d 61 78 3b 0a 20  .    int nMax;. 
b2d30 20 20 20 69 6e 74 20 68 3b 0a 20 20 20 20 50 41     int h;.    PA
b2d40 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65 72 2d  GER_INCR(pPager-
b2d50 3e 6e 4d 69 73 73 29 3b 0a 20 20 20 20 72 63 20  >nMiss);.    rc 
b2d60 3d 20 70 61 67 65 72 41 6c 6c 6f 63 61 74 65 50  = pagerAllocateP
b2d70 61 67 65 28 70 50 61 67 65 72 2c 20 26 70 50 67  age(pPager, &pPg
b2d80 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
b2d90 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
b2da0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
b2db0 20 7d 0a 0a 20 20 20 20 70 50 67 2d 3e 70 67 6e   }..    pPg->pgn
b2dc0 6f 20 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 61 73  o = pgno;.    as
b2dd0 73 65 72 74 28 20 21 4d 45 4d 44 42 20 7c 7c 20  sert( !MEMDB || 
b2de0 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 73 74 6d  pgno>pPager->stm
b2df0 74 53 69 7a 65 20 29 3b 0a 20 20 20 20 69 66 28  tSize );.    if(
b2e00 20 70 50 61 67 65 72 2d 3e 61 49 6e 4a 6f 75 72   pPager->aInJour
b2e10 6e 61 6c 20 26 26 20 28 69 6e 74 29 70 67 6e 6f  nal && (int)pgno
b2e20 3c 3d 70 50 61 67 65 72 2d 3e 6f 72 69 67 44 62  <=pPager->origDb
b2e30 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 73 71  Size ){.      sq
b2e40 6c 69 74 65 33 43 68 65 63 6b 4d 65 6d 6f 72 79  lite3CheckMemory
b2e50 28 70 50 61 67 65 72 2d 3e 61 49 6e 4a 6f 75 72  (pPager->aInJour
b2e60 6e 61 6c 2c 20 70 67 6e 6f 2f 38 29 3b 0a 20 20  nal, pgno/8);.  
b2e70 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
b2e80 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20  er->journalOpen 
b2e90 29 3b 0a 20 20 20 20 20 20 70 50 67 2d 3e 69 6e  );.      pPg->in
b2ea0 4a 6f 75 72 6e 61 6c 20 3d 20 28 70 50 61 67 65  Journal = (pPage
b2eb0 72 2d 3e 61 49 6e 4a 6f 75 72 6e 61 6c 5b 70 67  r->aInJournal[pg
b2ec0 6e 6f 2f 38 5d 20 26 20 28 31 3c 3c 28 70 67 6e  no/8] & (1<<(pgn
b2ed0 6f 26 37 29 29 29 21 3d 30 3b 0a 20 20 20 20 20  o&7)))!=0;.     
b2ee0 20 70 50 67 2d 3e 6e 65 65 64 53 79 6e 63 20 3d   pPg->needSync =
b2ef0 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   0;.    }else{. 
b2f00 20 20 20 20 20 70 50 67 2d 3e 69 6e 4a 6f 75 72       pPg->inJour
b2f10 6e 61 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  nal = 0;.      p
b2f20 50 67 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30  Pg->needSync = 0
b2f30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6d 61 6b  ;.    }..    mak
b2f40 65 43 6c 65 61 6e 28 70 50 67 29 3b 0a 20 20 20  eClean(pPg);.   
b2f50 20 70 50 67 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a   pPg->nRef = 1;.
b2f60 20 20 20 20 52 45 46 49 4e 46 4f 28 70 50 67 29      REFINFO(pPg)
b2f70 3b 0a 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e  ;..    pPager->n
b2f80 52 65 66 2b 2b 3b 0a 20 20 20 20 69 66 28 20 70  Ref++;.    if( p
b2f90 50 61 67 65 72 2d 3e 6e 45 78 74 72 61 3e 30 20  Pager->nExtra>0 
b2fa0 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28  ){.      memset(
b2fb0 50 47 48 44 52 5f 54 4f 5f 45 58 54 52 41 28 70  PGHDR_TO_EXTRA(p
b2fc0 50 67 2c 20 70 50 61 67 65 72 29 2c 20 30 2c 20  Pg, pPager), 0, 
b2fd0 70 50 61 67 65 72 2d 3e 6e 45 78 74 72 61 29 3b  pPager->nExtra);
b2fe0 0a 20 20 20 20 7d 0a 20 20 20 20 6e 4d 61 78 20  .    }.    nMax 
b2ff0 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61  = sqlite3PagerPa
b3000 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 29 3b  gecount(pPager);
b3010 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d  .    if( pPager-
b3020 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20  >errCode ){.    
b3030 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e    sqlite3PagerUn
b3040 72 65 66 28 70 50 67 29 3b 0a 20 20 20 20 20 20  ref(pPg);.      
b3050 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72  rc = pPager->err
b3060 43 6f 64 65 3b 0a 20 20 20 20 20 20 72 65 74 75  Code;.      retu
b3070 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20  rn rc;.    }..  
b3080 20 20 2f 2a 20 50 6f 70 75 6c 61 74 65 20 74 68    /* Populate th
b3090 65 20 70 61 67 65 20 77 69 74 68 20 64 61 74 61  e page with data
b30a0 2c 20 65 69 74 68 65 72 20 62 79 20 72 65 61 64  , either by read
b30b0 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 64 61 74  ing from the dat
b30c0 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 66 69 6c  abase.    ** fil
b30d0 65 2c 20 6f 72 20 62 79 20 73 65 74 74 69 6e 67  e, or by setting
b30e0 20 74 68 65 20 65 6e 74 69 72 65 20 70 61 67 65   the entire page
b30f0 20 74 6f 20 7a 65 72 6f 2e 0a 20 20 20 20 2a 2f   to zero..    */
b3100 0a 20 20 20 20 69 66 28 20 6e 4d 61 78 3c 28 69  .    if( nMax<(i
b3110 6e 74 29 70 67 6e 6f 20 7c 7c 20 4d 45 4d 44 42  nt)pgno || MEMDB
b3120 20 7c 7c 20 28 6e 6f 43 6f 6e 74 65 6e 74 20 26   || (noContent &
b3130 26 20 21 70 50 61 67 65 72 2d 3e 61 6c 77 61 79  & !pPager->alway
b3140 73 52 6f 6c 6c 62 61 63 6b 29 20 29 7b 0a 20 20  sRollback) ){.  
b3150 20 20 20 20 69 66 28 20 70 67 6e 6f 3e 70 50 61      if( pgno>pPa
b3160 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 29 7b 0a 20  ger->mxPgno ){. 
b3170 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61         sqlite3Pa
b3180 67 65 72 55 6e 72 65 66 28 70 50 67 29 3b 0a 20  gerUnref(pPg);. 
b3190 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51         return SQ
b31a0 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 20  LITE_FULL;.     
b31b0 20 7d 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28   }.      memset(
b31c0 50 47 48 44 52 5f 54 4f 5f 44 41 54 41 28 70 50  PGHDR_TO_DATA(pP
b31d0 67 29 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e 70  g), 0, pPager->p
b31e0 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20  ageSize);.      
b31f0 70 50 67 2d 3e 6e 65 65 64 52 65 61 64 20 3d 20  pPg->needRead = 
b3200 6e 6f 43 6f 6e 74 65 6e 74 20 26 26 20 21 70 50  noContent && !pP
b3210 61 67 65 72 2d 3e 61 6c 77 61 79 73 52 6f 6c 6c  ager->alwaysRoll
b3220 62 61 63 6b 3b 0a 20 20 20 20 20 20 49 4f 54 52  back;.      IOTR
b3230 41 43 45 28 28 22 5a 45 52 4f 20 25 70 20 25 64  ACE(("ZERO %p %d
b3240 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 67 6e  \n", pPager, pgn
b3250 6f 29 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  o));.    }else{.
b3260 20 20 20 20 20 20 72 63 20 3d 20 72 65 61 64 44        rc = readD
b3270 62 50 61 67 65 28 70 50 61 67 65 72 2c 20 70 50  bPage(pPager, pP
b3280 67 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20  g, pgno);.      
b3290 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
b32a0 4b 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f  K && rc!=SQLITE_
b32b0 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44  IOERR_SHORT_READ
b32c0 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50 67 2d   ){.        pPg-
b32d0 3e 70 67 6e 6f 20 3d 20 30 3b 0a 20 20 20 20 20  >pgno = 0;.     
b32e0 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55     sqlite3PagerU
b32f0 6e 72 65 66 28 70 50 67 29 3b 0a 20 20 20 20 20  nref(pPg);.     
b3300 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
b3310 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50 67 2d      }.      pPg-
b3320 3e 6e 65 65 64 52 65 61 64 20 3d 20 30 3b 0a 20  >needRead = 0;. 
b3330 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4c 69 6e     }..    /* Lin
b3340 6b 20 74 68 65 20 70 61 67 65 20 69 6e 74 6f 20  k the page into 
b3350 74 68 65 20 70 61 67 65 20 68 61 73 68 20 74 61  the page hash ta
b3360 62 6c 65 20 2a 2f 0a 20 20 20 20 68 20 3d 20 70  ble */.    h = p
b3370 67 6e 6f 20 26 20 28 70 50 61 67 65 72 2d 3e 6e  gno & (pPager->n
b3380 48 61 73 68 2d 31 29 3b 0a 20 20 20 20 61 73 73  Hash-1);.    ass
b3390 65 72 74 28 20 70 67 6e 6f 21 3d 30 20 29 3b 0a  ert( pgno!=0 );.
b33a0 20 20 20 20 70 50 67 2d 3e 70 4e 65 78 74 48 61      pPg->pNextHa
b33b0 73 68 20 3d 20 70 50 61 67 65 72 2d 3e 61 48 61  sh = pPager->aHa
b33c0 73 68 5b 68 5d 3b 0a 20 20 20 20 70 50 61 67 65  sh[h];.    pPage
b33d0 72 2d 3e 61 48 61 73 68 5b 68 5d 20 3d 20 70 50  r->aHash[h] = pP
b33e0 67 3b 0a 20 20 20 20 69 66 28 20 70 50 67 2d 3e  g;.    if( pPg->
b33f0 70 4e 65 78 74 48 61 73 68 20 29 7b 0a 20 20 20  pNextHash ){.   
b3400 20 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e     assert( pPg->
b3410 70 4e 65 78 74 48 61 73 68 2d 3e 70 50 72 65 76  pNextHash->pPrev
b3420 48 61 73 68 3d 3d 30 20 29 3b 0a 20 20 20 20 20  Hash==0 );.     
b3430 20 70 50 67 2d 3e 70 4e 65 78 74 48 61 73 68 2d   pPg->pNextHash-
b3440 3e 70 50 72 65 76 48 61 73 68 20 3d 20 70 50 67  >pPrevHash = pPg
b3450 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 64 65 66 20  ;.    }..#ifdef 
b3460 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47  SQLITE_CHECK_PAG
b3470 45 53 0a 20 20 20 20 70 50 67 2d 3e 70 61 67 65  ES.    pPg->page
b3480 48 61 73 68 20 3d 20 70 61 67 65 72 5f 70 61 67  Hash = pager_pag
b3490 65 68 61 73 68 28 70 50 67 29 3b 0a 23 65 6e 64  ehash(pPg);.#end
b34a0 69 66 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  if.  }else{.    
b34b0 2f 2a 20 54 68 65 20 72 65 71 75 65 73 74 65 64  /* The requested
b34c0 20 70 61 67 65 20 69 73 20 69 6e 20 74 68 65 20   page is in the 
b34d0 70 61 67 65 20 63 61 63 68 65 2e 20 2a 2f 0a 20  page cache. */. 
b34e0 20 20 20 61 73 73 65 72 74 28 70 50 61 67 65 72     assert(pPager
b34f0 2d 3e 6e 52 65 66 3e 30 20 7c 7c 20 70 67 6e 6f  ->nRef>0 || pgno
b3500 3d 3d 31 29 3b 0a 20 20 20 20 50 41 47 45 52 5f  ==1);.    PAGER_
b3510 49 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e 48 69  INCR(pPager->nHi
b3520 74 29 3b 0a 20 20 20 20 69 66 28 20 21 6e 6f 43  t);.    if( !noC
b3530 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 20  ontent ){.      
b3540 72 63 20 3d 20 70 61 67 65 72 5f 67 65 74 5f 63  rc = pager_get_c
b3550 6f 6e 74 65 6e 74 28 70 50 67 29 3b 0a 20 20 20  ontent(pPg);.   
b3560 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20     if( rc ){.   
b3570 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
b3580 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
b3590 20 20 70 61 67 65 5f 72 65 66 28 70 50 67 29 3b    page_ref(pPg);
b35a0 0a 20 20 7d 0a 20 20 2a 70 70 50 61 67 65 20 3d  .  }.  *ppPage =
b35b0 20 70 50 67 3b 0a 20 20 72 65 74 75 72 6e 20 53   pPg;.  return S
b35c0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
b35d0 2a 2a 20 41 63 71 75 69 72 65 20 61 20 70 61 67  ** Acquire a pag
b35e0 65 20 69 66 20 69 74 20 69 73 20 61 6c 72 65 61  e if it is alrea
b35f0 64 79 20 69 6e 20 74 68 65 20 69 6e 2d 6d 65 6d  dy in the in-mem
b3600 6f 72 79 20 63 61 63 68 65 2e 20 20 44 6f 0a 2a  ory cache.  Do.*
b3610 2a 20 6e 6f 74 20 72 65 61 64 20 74 68 65 20 70  * not read the p
b3620 61 67 65 20 66 72 6f 6d 20 64 69 73 6b 2e 20 20  age from disk.  
b3630 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
b3640 20 74 6f 20 74 68 65 20 70 61 67 65 2c 0a 2a 2a   to the page,.**
b3650 20 6f 72 20 30 20 69 66 20 74 68 65 20 70 61 67   or 0 if the pag
b3660 65 20 69 73 20 6e 6f 74 20 69 6e 20 63 61 63 68  e is not in cach
b3670 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  e..**.** See als
b3680 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  o sqlite3PagerGe
b3690 74 28 29 2e 20 20 54 68 65 20 64 69 66 66 65 72  t().  The differ
b36a0 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 69  ence between thi
b36b0 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 61 6e 64  s routine.** and
b36c0 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
b36d0 28 29 20 69 73 20 74 68 61 74 20 5f 67 65 74 28  () is that _get(
b36e0 29 20 77 69 6c 6c 20 67 6f 20 74 6f 20 74 68 65  ) will go to the
b36f0 20 64 69 73 6b 20 61 6e 64 20 72 65 61 64 0a 2a   disk and read.*
b3700 2a 20 69 6e 20 74 68 65 20 70 61 67 65 20 69 66  * in the page if
b3710 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74   the page is not
b3720 20 61 6c 72 65 61 64 79 20 69 6e 20 63 61 63 68   already in cach
b3730 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  e.  This routine
b3740 0a 2a 2a 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c  .** returns NULL
b3750 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20   if the page is 
b3760 6e 6f 74 20 69 6e 20 63 61 63 68 65 20 6f 72 20  not in cache or 
b3770 69 66 20 61 20 64 69 73 6b 20 49 2f 4f 20 65 72  if a disk I/O er
b3780 72 6f 72 20 0a 2a 2a 20 68 61 73 20 65 76 65 72  ror .** has ever
b3790 20 68 61 70 70 65 6e 65 64 2e 0a 2a 2f 0a 53 51   happened..*/.SQ
b37a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 44 62 50  LITE_PRIVATE DbP
b37b0 61 67 65 20 2a 73 71 6c 69 74 65 33 50 61 67 65  age *sqlite3Page
b37c0 72 4c 6f 6f 6b 75 70 28 50 61 67 65 72 20 2a 70  rLookup(Pager *p
b37d0 50 61 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f  Pager, Pgno pgno
b37e0 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 67 3b  ){.  PgHdr *pPg;
b37f0 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ..  assert( pPag
b3800 65 72 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  er!=0 );.  asser
b3810 74 28 20 70 67 6e 6f 21 3d 30 20 29 3b 0a 0a 20  t( pgno!=0 );.. 
b3820 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61   if( pPager->sta
b3830 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b  te==PAGER_UNLOCK
b3840 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
b3850 21 70 50 61 67 65 72 2d 3e 70 41 6c 6c 20 7c 7c  !pPager->pAll ||
b3860 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69   pPager->exclusi
b3870 76 65 4d 6f 64 65 20 29 3b 0a 20 20 20 20 72 65  veMode );.    re
b3880 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66  turn 0;.  }.  if
b3890 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  ( pPager->errCod
b38a0 65 20 26 26 20 70 50 61 67 65 72 2d 3e 65 72 72  e && pPager->err
b38b0 43 6f 64 65 21 3d 53 51 4c 49 54 45 5f 46 55 4c  Code!=SQLITE_FUL
b38c0 4c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  L ){.    return 
b38d0 30 3b 0a 20 20 7d 0a 20 20 70 50 67 20 3d 20 70  0;.  }.  pPg = p
b38e0 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67  ager_lookup(pPag
b38f0 65 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 69 66 28  er, pgno);.  if(
b3900 20 70 50 67 3d 3d 30 20 29 20 72 65 74 75 72 6e   pPg==0 ) return
b3910 20 30 3b 0a 20 20 70 61 67 65 5f 72 65 66 28 70   0;.  page_ref(p
b3920 50 67 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 50  Pg);.  return pP
b3930 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65  g;.}../*.** Rele
b3940 61 73 65 20 61 20 70 61 67 65 2e 0a 2a 2a 0a 2a  ase a page..**.*
b3950 2a 20 49 66 20 74 68 65 20 6e 75 6d 62 65 72 20  * If the number 
b3960 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f  of references to
b3970 20 74 68 65 20 70 61 67 65 20 64 72 6f 70 20 74   the page drop t
b3980 6f 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65  o zero, then the
b3990 0a 2a 2a 20 70 61 67 65 20 69 73 20 61 64 64 65  .** page is adde
b39a0 64 20 74 6f 20 74 68 65 20 4c 52 55 20 6c 69 73  d to the LRU lis
b39b0 74 2e 20 20 57 68 65 6e 20 61 6c 6c 20 72 65 66  t.  When all ref
b39c0 65 72 65 6e 63 65 73 20 74 6f 20 61 6c 6c 20 70  erences to all p
b39d0 61 67 65 73 0a 2a 2a 20 61 72 65 20 72 65 6c 65  ages.** are rele
b39e0 61 73 65 64 2c 20 61 20 72 6f 6c 6c 62 61 63 6b  ased, a rollback
b39f0 20 6f 63 63 75 72 73 20 61 6e 64 20 74 68 65 20   occurs and the 
b3a00 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61  lock on the data
b3a10 62 61 73 65 20 69 73 0a 2a 2a 20 72 65 6d 6f 76  base is.** remov
b3a20 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
b3a30 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
b3a40 33 50 61 67 65 72 55 6e 72 65 66 28 44 62 50 61  3PagerUnref(DbPa
b3a50 67 65 20 2a 70 50 67 29 7b 0a 0a 20 20 2f 2a 20  ge *pPg){..  /* 
b3a60 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65  Decrement the re
b3a70 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 66 6f  ference count fo
b3a80 72 20 74 68 69 73 20 70 61 67 65 0a 20 20 2a 2f  r this page.  */
b3a90 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e  .  assert( pPg->
b3aa0 6e 52 65 66 3e 30 20 29 3b 0a 20 20 70 50 67 2d  nRef>0 );.  pPg-
b3ab0 3e 6e 52 65 66 2d 2d 3b 0a 20 20 52 45 46 49 4e  >nRef--;.  REFIN
b3ac0 46 4f 28 70 50 67 29 3b 0a 0a 20 20 43 48 45 43  FO(pPg);..  CHEC
b3ad0 4b 5f 50 41 47 45 28 70 50 67 29 3b 0a 0a 20 20  K_PAGE(pPg);..  
b3ae0 2f 2a 20 57 68 65 6e 20 74 68 65 20 6e 75 6d 62  /* When the numb
b3af0 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73  er of references
b3b00 20 74 6f 20 61 20 70 61 67 65 20 72 65 61 63 68   to a page reach
b3b10 20 30 2c 20 63 61 6c 6c 20 74 68 65 0a 20 20 2a   0, call the.  *
b3b20 2a 20 64 65 73 74 72 75 63 74 6f 72 20 61 6e 64  * destructor and
b3b30 20 61 64 64 20 74 68 65 20 70 61 67 65 20 74 6f   add the page to
b3b40 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 0a 20   the freelist.. 
b3b50 20 2a 2f 0a 20 20 69 66 28 20 70 50 67 2d 3e 6e   */.  if( pPg->n
b3b60 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 50 61  Ref==0 ){.    Pa
b3b70 67 65 72 20 2a 70 50 61 67 65 72 3b 0a 20 20 20  ger *pPager;.   
b3b80 20 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70   pPager = pPg->p
b3b90 50 61 67 65 72 3b 0a 20 20 20 20 70 50 67 2d 3e  Pager;.    pPg->
b3ba0 70 4e 65 78 74 46 72 65 65 20 3d 20 30 3b 0a 20  pNextFree = 0;. 
b3bb0 20 20 20 70 50 67 2d 3e 70 50 72 65 76 46 72 65     pPg->pPrevFre
b3bc0 65 20 3d 20 70 50 61 67 65 72 2d 3e 70 4c 61 73  e = pPager->pLas
b3bd0 74 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70  t;.    pPager->p
b3be0 4c 61 73 74 20 3d 20 70 50 67 3b 0a 20 20 20 20  Last = pPg;.    
b3bf0 69 66 28 20 70 50 67 2d 3e 70 50 72 65 76 46 72  if( pPg->pPrevFr
b3c00 65 65 20 29 7b 0a 20 20 20 20 20 20 70 50 67 2d  ee ){.      pPg-
b3c10 3e 70 50 72 65 76 46 72 65 65 2d 3e 70 4e 65 78  >pPrevFree->pNex
b3c20 74 46 72 65 65 20 3d 20 70 50 67 3b 0a 20 20 20  tFree = pPg;.   
b3c30 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 50   }else{.      pP
b3c40 61 67 65 72 2d 3e 70 46 69 72 73 74 20 3d 20 70  ager->pFirst = p
b3c50 50 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  Pg;.    }.    if
b3c60 28 20 70 50 67 2d 3e 6e 65 65 64 53 79 6e 63 3d  ( pPg->needSync=
b3c70 3d 30 20 26 26 20 70 50 61 67 65 72 2d 3e 70 46  =0 && pPager->pF
b3c80 69 72 73 74 53 79 6e 63 65 64 3d 3d 30 20 29 7b  irstSynced==0 ){
b3c90 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 70  .      pPager->p
b3ca0 46 69 72 73 74 53 79 6e 63 65 64 20 3d 20 70 50  FirstSynced = pP
b3cb0 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  g;.    }.    if(
b3cc0 20 70 50 61 67 65 72 2d 3e 78 44 65 73 74 72 75   pPager->xDestru
b3cd0 63 74 6f 72 20 29 7b 0a 20 20 20 20 20 20 70 50  ctor ){.      pP
b3ce0 61 67 65 72 2d 3e 78 44 65 73 74 72 75 63 74 6f  ager->xDestructo
b3cf0 72 28 70 50 67 2c 20 70 50 61 67 65 72 2d 3e 70  r(pPg, pPager->p
b3d00 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 7d 0a  ageSize);.    }.
b3d10 20 20 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 61    .    /* When a
b3d20 6c 6c 20 70 61 67 65 73 20 72 65 61 63 68 20 74  ll pages reach t
b3d30 68 65 20 66 72 65 65 6c 69 73 74 2c 20 64 72 6f  he freelist, dro
b3d40 70 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 20  p the read lock 
b3d50 66 72 6f 6d 0a 20 20 20 20 2a 2a 20 74 68 65 20  from.    ** the 
b3d60 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20  database file.. 
b3d70 20 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72     */.    pPager
b3d80 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 61 73  ->nRef--;.    as
b3d90 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 52  sert( pPager->nR
b3da0 65 66 3e 3d 30 20 29 3b 0a 20 20 20 20 69 66 28  ef>=0 );.    if(
b3db0 20 70 50 61 67 65 72 2d 3e 6e 52 65 66 3d 3d 30   pPager->nRef==0
b3dc0 20 26 26 20 28 21 70 50 61 67 65 72 2d 3e 65 78   && (!pPager->ex
b3dd0 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c 20 70  clusiveMode || p
b3de0 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
b3df0 66 3e 30 29 20 29 7b 0a 20 20 20 20 20 20 70 61  f>0) ){.      pa
b3e00 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c  gerUnlockAndRoll
b3e10 62 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20  back(pPager);.  
b3e20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
b3e30 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
b3e40 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6a 6f  *.** Create a jo
b3e50 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 70  urnal file for p
b3e60 50 61 67 65 72 2e 20 20 54 68 65 72 65 20 73 68  Pager.  There sh
b3e70 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 62 65 20  ould already be 
b3e80 61 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 6f 72  a RESERVED.** or
b3e90 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20   EXCLUSIVE lock 
b3ea0 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
b3eb0 66 69 6c 65 20 77 68 65 6e 20 74 68 69 73 20 72  file when this r
b3ec0 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
b3ed0 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53  ..**.** Return S
b3ee0 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72  QLITE_OK if ever
b3ef0 79 74 68 69 6e 67 2e 20 20 52 65 74 75 72 6e 20  ything.  Return 
b3f00 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e  an error code an
b3f10 64 20 72 65 6c 65 61 73 65 20 74 68 65 0a 2a 2a  d release the.**
b3f20 20 77 72 69 74 65 20 6c 6f 63 6b 20 69 66 20 61   write lock if a
b3f30 6e 79 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f  nything goes wro
b3f40 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ng..*/.static in
b3f50 74 20 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75  t pager_open_jou
b3f60 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67  rnal(Pager *pPag
b3f70 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  er){.  int rc;. 
b3f80 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20   assert( !MEMDB 
b3f90 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
b3fa0 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45  ger->state>=PAGE
b3fb0 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20 20  R_RESERVED );.  
b3fc0 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
b3fd0 6a 6f 75 72 6e 61 6c 4f 70 65 6e 3d 3d 30 20 29  journalOpen==0 )
b3fe0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
b3ff0 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29  er->useJournal )
b4000 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
b4010 65 72 2d 3e 61 49 6e 4a 6f 75 72 6e 61 6c 3d 3d  er->aInJournal==
b4020 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 61  0 );.  sqlite3Pa
b4030 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61  gerPagecount(pPa
b4040 67 65 72 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e  ger);.  pPager->
b4050 61 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 73 71 6c  aInJournal = sql
b4060 69 74 65 4d 61 6c 6c 6f 63 28 20 70 50 61 67 65  iteMalloc( pPage
b4070 72 2d 3e 64 62 53 69 7a 65 2f 38 20 2b 20 31 20  r->dbSize/8 + 1 
b4080 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  );.  if( pPager-
b4090 3e 61 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29  >aInJournal==0 )
b40a0 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
b40b0 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f 74  E_NOMEM;.    got
b40c0 6f 20 66 61 69 6c 65 64 5f 74 6f 5f 6f 70 65 6e  o failed_to_open
b40d0 5f 6a 6f 75 72 6e 61 6c 3b 0a 20 20 7d 0a 20 20  _journal;.  }.  
b40e0 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70  rc = sqlite3OsOp
b40f0 65 6e 45 78 63 6c 75 73 69 76 65 28 70 50 61 67  enExclusive(pPag
b4100 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 26 70  er->zJournal, &p
b4110 50 61 67 65 72 2d 3e 6a 66 64 2c 0a 20 20 20 20  Pager->jfd,.    
b4120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b4130 20 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61               pPa
b4140 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29 3b 0a  ger->tempFile);.
b4150 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51    assert( rc!=SQ
b4160 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65  LITE_OK || pPage
b4170 72 2d 3e 6a 66 64 20 29 3b 0a 20 20 70 50 61 67  r->jfd );.  pPag
b4180 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d  er->journalOff =
b4190 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 65   0;.  pPager->se
b41a0 74 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 70  tMaster = 0;.  p
b41b0 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64  Pager->journalHd
b41c0 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 72 63 21  r = 0;.  if( rc!
b41d0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
b41e0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
b41f0 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20  _NOMEM ){.      
b4200 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28  sqlite3OsDelete(
b4210 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c  pPager->zJournal
b4220 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74  );.    }.    got
b4230 6f 20 66 61 69 6c 65 64 5f 74 6f 5f 6f 70 65 6e  o failed_to_open
b4240 5f 6a 6f 75 72 6e 61 6c 3b 0a 20 20 7d 0a 20 20  _journal;.  }.  
b4250 73 71 6c 69 74 65 33 4f 73 53 65 74 46 75 6c 6c  sqlite3OsSetFull
b4260 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 6a 66 64  Sync(pPager->jfd
b4270 2c 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 5f 66  , pPager->full_f
b4280 73 79 6e 63 29 3b 0a 20 20 73 71 6c 69 74 65 33  sync);.  sqlite3
b4290 4f 73 53 65 74 46 75 6c 6c 53 79 6e 63 28 70 50  OsSetFullSync(pP
b42a0 61 67 65 72 2d 3e 66 64 2c 20 70 50 61 67 65 72  ager->fd, pPager
b42b0 2d 3e 66 75 6c 6c 5f 66 73 79 6e 63 29 3b 0a 20  ->full_fsync);. 
b42c0 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 44 69   sqlite3OsOpenDi
b42d0 72 65 63 74 6f 72 79 28 70 50 61 67 65 72 2d 3e  rectory(pPager->
b42e0 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 7a 44 69  jfd, pPager->zDi
b42f0 72 65 63 74 6f 72 79 29 3b 0a 20 20 70 50 61 67  rectory);.  pPag
b4300 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20  er->journalOpen 
b4310 3d 20 31 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6a  = 1;.  pPager->j
b4320 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20  ournalStarted = 
b4330 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 65 65  0;.  pPager->nee
b4340 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 70 50 61  dSync = 0;.  pPa
b4350 67 65 72 2d 3e 61 6c 77 61 79 73 52 6f 6c 6c 62  ger->alwaysRollb
b4360 61 63 6b 20 3d 20 30 3b 0a 20 20 70 50 61 67 65  ack = 0;.  pPage
b4370 72 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20 20 69  r->nRec = 0;.  i
b4380 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f  f( pPager->errCo
b4390 64 65 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70  de ){.    rc = p
b43a0 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a  Pager->errCode;.
b43b0 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 5f      goto failed_
b43c0 74 6f 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 3b  to_open_journal;
b43d0 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 6f  .  }.  pPager->o
b43e0 72 69 67 44 62 53 69 7a 65 20 3d 20 70 50 61 67  rigDbSize = pPag
b43f0 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 0a 20 20 72  er->dbSize;..  r
b4400 63 20 3d 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c  c = writeJournal
b4410 48 64 72 28 70 50 61 67 65 72 29 3b 0a 0a 20 20  Hdr(pPager);..  
b4420 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 6d 74  if( pPager->stmt
b4430 41 75 74 6f 6f 70 65 6e 20 26 26 20 72 63 3d 3d  Autoopen && rc==
b4440 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
b4450 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
b4460 65 72 53 74 6d 74 42 65 67 69 6e 28 70 50 61 67  erStmtBegin(pPag
b4470 65 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72  er);.  }.  if( r
b4480 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c!=SQLITE_OK && 
b4490 72 63 21 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc!=SQLITE_NOMEM
b44a0 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67   ){.    rc = pag
b44b0 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69  er_end_transacti
b44c0 6f 6e 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  on(pPager);.    
b44d0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
b44e0 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  K ){.      rc = 
b44f0 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20  SQLITE_FULL;.   
b4500 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
b4510 72 63 3b 0a 0a 66 61 69 6c 65 64 5f 74 6f 5f 6f  rc;..failed_to_o
b4520 70 65 6e 5f 6a 6f 75 72 6e 61 6c 3a 0a 20 20 73  pen_journal:.  s
b4530 71 6c 69 74 65 46 72 65 65 28 70 50 61 67 65 72  qliteFree(pPager
b4540 2d 3e 61 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20  ->aInJournal);. 
b4550 20 70 50 61 67 65 72 2d 3e 61 49 6e 4a 6f 75 72   pPager->aInJour
b4560 6e 61 6c 20 3d 20 30 3b 0a 20 20 72 65 74 75 72  nal = 0;.  retur
b4570 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  n rc;.}../*.** A
b4580 63 71 75 69 72 65 20 61 20 77 72 69 74 65 2d 6c  cquire a write-l
b4590 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ock on the datab
b45a0 61 73 65 2e 20 20 54 68 65 20 6c 6f 63 6b 20 69  ase.  The lock i
b45b0 73 20 72 65 6d 6f 76 65 64 20 77 68 65 6e 0a 2a  s removed when.*
b45c0 2a 20 74 68 65 20 61 6e 79 20 6f 66 20 74 68 65  * the any of the
b45d0 20 66 6f 6c 6c 6f 77 69 6e 67 20 68 61 70 70 65   following happe
b45e0 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 73 71  n:.**.**   *  sq
b45f0 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74  lite3PagerCommit
b4600 50 68 61 73 65 54 77 6f 28 29 20 69 73 20 63 61  PhaseTwo() is ca
b4610 6c 6c 65 64 2e 0a 2a 2a 20 20 20 2a 20 20 73 71  lled..**   *  sq
b4620 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61  lite3PagerRollba
b4630 63 6b 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 0a  ck() is called..
b4640 2a 2a 20 20 20 2a 20 20 73 71 6c 69 74 65 33 50  **   *  sqlite3P
b4650 61 67 65 72 43 6c 6f 73 65 28 29 20 69 73 20 63  agerClose() is c
b4660 61 6c 6c 65 64 2e 0a 2a 2a 20 20 20 2a 20 20 73  alled..**   *  s
b4670 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
b4680 28 29 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20  () is called to 
b4690 6f 6e 20 65 76 65 72 79 20 6f 75 74 73 74 61 6e  on every outstan
b46a0 64 69 6e 67 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a  ding page..**.**
b46b0 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d   The first param
b46c0 65 74 65 72 20 74 6f 20 74 68 69 73 20 72 6f 75  eter to this rou
b46d0 74 69 6e 65 20 69 73 20 61 20 70 6f 69 6e 74 65  tine is a pointe
b46e0 72 20 74 6f 20 61 6e 79 20 6f 70 65 6e 20 70 61  r to any open pa
b46f0 67 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74  ge of the.** dat
b4700 61 62 61 73 65 20 66 69 6c 65 2e 20 20 4e 6f 74  abase file.  Not
b4710 68 69 6e 67 20 63 68 61 6e 67 65 73 20 61 62 6f  hing changes abo
b4720 75 74 20 74 68 65 20 70 61 67 65 20 2d 20 69 74  ut the page - it
b4730 20 69 73 20 75 73 65 64 20 6d 65 72 65 6c 79 20   is used merely 
b4740 74 6f 0a 2a 2a 20 61 63 71 75 69 72 65 20 61 20  to.** acquire a 
b4750 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 50  pointer to the P
b4760 61 67 65 72 20 73 74 72 75 63 74 75 72 65 20 61  ager structure a
b4770 6e 64 20 61 73 20 70 72 6f 6f 66 20 74 68 61 74  nd as proof that
b4780 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 61 6c 72   there is.** alr
b4790 65 61 64 79 20 61 20 72 65 61 64 2d 6c 6f 63 6b  eady a read-lock
b47a0 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
b47b0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f  ..**.** The seco
b47c0 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 6e 64  nd parameter ind
b47d0 69 63 61 74 65 73 20 68 6f 77 20 6d 75 63 68 20  icates how much 
b47e0 73 70 61 63 65 20 69 6e 20 62 79 74 65 73 20 74  space in bytes t
b47f0 6f 20 72 65 73 65 72 76 65 20 66 6f 72 20 61 0a  o reserve for a.
b4800 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  ** master journa
b4810 6c 20 66 69 6c 65 2d 6e 61 6d 65 20 61 74 20 74  l file-name at t
b4820 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20  he start of the 
b4830 6a 6f 75 72 6e 61 6c 20 77 68 65 6e 20 69 74 20  journal when it 
b4840 69 73 20 63 72 65 61 74 65 64 2e 0a 2a 2a 0a 2a  is created..**.*
b4850 2a 20 41 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  * A journal file
b4860 20 69 73 20 6f 70 65 6e 65 64 20 69 66 20 74 68   is opened if th
b4870 69 73 20 69 73 20 6e 6f 74 20 61 20 74 65 6d 70  is is not a temp
b4880 6f 72 61 72 79 20 66 69 6c 65 2e 20 20 46 6f 72  orary file.  For
b4890 20 74 65 6d 70 6f 72 61 72 79 0a 2a 2a 20 66 69   temporary.** fi
b48a0 6c 65 73 2c 20 74 68 65 20 6f 70 65 6e 69 6e 67  les, the opening
b48b0 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
b48c0 66 69 6c 65 20 69 73 20 64 65 66 65 72 72 65 64  file is deferred
b48d0 20 75 6e 74 69 6c 20 74 68 65 72 65 20 69 73 20   until there is 
b48e0 61 6e 0a 2a 2a 20 61 63 74 75 61 6c 20 6e 65 65  an.** actual nee
b48f0 64 20 74 6f 20 77 72 69 74 65 20 74 6f 20 74 68  d to write to th
b4900 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a  e journal..**.**
b4910 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65   If the database
b4920 20 69 73 20 61 6c 72 65 61 64 79 20 72 65 73 65   is already rese
b4930 72 76 65 64 20 66 6f 72 20 77 72 69 74 69 6e 67  rved for writing
b4940 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  , this routine i
b4950 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a  s a no-op..**.**
b4960 20 49 66 20 65 78 46 6c 61 67 20 69 73 20 74 72   If exFlag is tr
b4970 75 65 2c 20 67 6f 20 61 68 65 61 64 20 61 6e 64  ue, go ahead and
b4980 20 67 65 74 20 61 6e 20 45 58 43 4c 55 53 49 56   get an EXCLUSIV
b4990 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69  E lock on the fi
b49a0 6c 65 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c  le.** immediatel
b49b0 79 20 69 6e 73 74 65 61 64 20 6f 66 20 77 61 69  y instead of wai
b49c0 74 69 6e 67 20 75 6e 74 69 6c 20 77 65 20 74 72  ting until we tr
b49d0 79 20 74 6f 20 66 6c 75 73 68 20 74 68 65 20 63  y to flush the c
b49e0 61 63 68 65 2e 20 20 54 68 65 0a 2a 2a 20 65 78  ache.  The.** ex
b49f0 46 6c 61 67 20 69 73 20 69 67 6e 6f 72 65 64 20  Flag is ignored 
b4a00 69 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  if a transaction
b4a10 20 69 73 20 61 6c 72 65 61 64 79 20 61 63 74 69   is already acti
b4a20 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ve..*/.SQLITE_PR
b4a30 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
b4a40 33 50 61 67 65 72 42 65 67 69 6e 28 44 62 50 61  3PagerBegin(DbPa
b4a50 67 65 20 2a 70 50 67 2c 20 69 6e 74 20 65 78 46  ge *pPg, int exF
b4a60 6c 61 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70  lag){.  Pager *p
b4a70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61  Pager = pPg->pPa
b4a80 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ger;.  int rc = 
b4a90 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73  SQLITE_OK;.  ass
b4aa0 65 72 74 28 20 70 50 67 2d 3e 6e 52 65 66 3e 30  ert( pPg->nRef>0
b4ab0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
b4ac0 61 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47  ager->state!=PAG
b4ad0 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20 69  ER_UNLOCK );.  i
b4ae0 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  f( pPager->state
b4af0 3d 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 29  ==PAGER_SHARED )
b4b00 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  {.    assert( pP
b4b10 61 67 65 72 2d 3e 61 49 6e 4a 6f 75 72 6e 61 6c  ager->aInJournal
b4b20 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 4d  ==0 );.    if( M
b4b30 45 4d 44 42 20 29 7b 0a 20 20 20 20 20 20 70 50  EMDB ){.      pP
b4b40 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41  ager->state = PA
b4b50 47 45 52 5f 45 58 43 4c 55 53 49 56 45 3b 0a 20  GER_EXCLUSIVE;. 
b4b60 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6f 72 69       pPager->ori
b4b70 67 44 62 53 69 7a 65 20 3d 20 70 50 61 67 65 72  gDbSize = pPager
b4b80 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 20 20 7d 65  ->dbSize;.    }e
b4b90 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  lse{.      rc = 
b4ba0 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50  sqlite3OsLock(pP
b4bb0 61 67 65 72 2d 3e 66 64 2c 20 52 45 53 45 52 56  ager->fd, RESERV
b4bc0 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20  ED_LOCK);.      
b4bd0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
b4be0 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50 61  K ){.        pPa
b4bf0 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47  ger->state = PAG
b4c00 45 52 5f 52 45 53 45 52 56 45 44 3b 0a 20 20 20  ER_RESERVED;.   
b4c10 20 20 20 20 20 69 66 28 20 65 78 46 6c 61 67 20       if( exFlag 
b4c20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  ){.          rc 
b4c30 3d 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f  = pager_wait_on_
b4c40 6c 6f 63 6b 28 70 50 61 67 65 72 2c 20 45 58 43  lock(pPager, EXC
b4c50 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20  LUSIVE_LOCK);.  
b4c60 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
b4c70 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
b4c80 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
b4c90 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
b4ca0 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67      }.      pPag
b4cb0 65 72 2d 3e 64 69 72 74 79 43 61 63 68 65 20 3d  er->dirtyCache =
b4cc0 20 30 3b 0a 20 20 20 20 20 20 50 41 47 45 52 54   0;.      PAGERT
b4cd0 52 41 43 45 32 28 22 54 52 41 4e 53 41 43 54 49  RACE2("TRANSACTI
b4ce0 4f 4e 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49  ON %d\n", PAGERI
b4cf0 44 28 70 50 61 67 65 72 29 29 3b 0a 20 20 20 20  D(pPager));.    
b4d00 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 75 73    if( pPager->us
b4d10 65 4a 6f 75 72 6e 61 6c 20 26 26 20 21 70 50 61  eJournal && !pPa
b4d20 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b  ger->tempFile ){
b4d30 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61  .        rc = pa
b4d40 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c  ger_open_journal
b4d50 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20  (pPager);.      
b4d60 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20  }.    }.  }else 
b4d70 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  if( pPager->jour
b4d80 6e 61 6c 4f 70 65 6e 20 26 26 20 70 50 61 67 65  nalOpen && pPage
b4d90 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30  r->journalOff==0
b4da0 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20   ){.    /* This 
b4db0 68 61 70 70 65 6e 73 20 77 68 65 6e 20 74 68 65  happens when the
b4dc0 20 70 61 67 65 72 20 77 61 73 20 69 6e 20 65 78   pager was in ex
b4dd0 63 6c 75 73 69 76 65 2d 61 63 63 65 73 73 20 6d  clusive-access m
b4de0 6f 64 65 20 6c 61 73 74 0a 20 20 20 20 2a 2a 20  ode last.    ** 
b4df0 74 69 6d 65 20 61 20 28 72 65 61 64 20 6f 72 20  time a (read or 
b4e00 77 72 69 74 65 29 20 74 72 61 6e 73 61 63 74 69  write) transacti
b4e10 6f 6e 20 77 61 73 20 73 75 63 63 65 73 73 66 75  on was successfu
b4e20 6c 6c 79 20 63 6f 6e 63 6c 75 64 65 64 0a 20 20  lly concluded.  
b4e30 20 20 2a 2a 20 62 79 20 74 68 69 73 20 63 6f 6e    ** by this con
b4e40 6e 65 63 74 69 6f 6e 2e 20 49 6e 73 74 65 61 64  nection. Instead
b4e50 20 6f 66 20 64 65 6c 65 74 69 6e 67 20 74 68 65   of deleting the
b4e60 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 74   journal file it
b4e70 20 77 61 73 20 0a 20 20 20 20 2a 2a 20 6b 65 70   was .    ** kep
b4e80 74 20 6f 70 65 6e 20 61 6e 64 20 74 72 75 6e 63  t open and trunc
b4e90 61 74 65 64 20 74 6f 20 30 20 62 79 74 65 73 2e  ated to 0 bytes.
b4ea0 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65  .    */.    asse
b4eb0 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 52 65 63  rt( pPager->nRec
b4ec0 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ==0 );.    asser
b4ed0 74 28 20 70 50 61 67 65 72 2d 3e 6f 72 69 67 44  t( pPager->origD
b4ee0 62 53 69 7a 65 3d 3d 30 20 29 3b 0a 20 20 20 20  bSize==0 );.    
b4ef0 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
b4f00 61 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b  aInJournal==0 );
b4f10 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65  .    sqlite3Page
b4f20 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65  rPagecount(pPage
b4f30 72 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e  r);.    pPager->
b4f40 61 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 73 71 6c  aInJournal = sql
b4f50 69 74 65 4d 61 6c 6c 6f 63 28 20 70 50 61 67 65  iteMalloc( pPage
b4f60 72 2d 3e 64 62 53 69 7a 65 2f 38 20 2b 20 31 20  r->dbSize/8 + 1 
b4f70 29 3b 0a 20 20 20 20 69 66 28 20 21 70 50 61 67  );.    if( !pPag
b4f80 65 72 2d 3e 61 49 6e 4a 6f 75 72 6e 61 6c 20 29  er->aInJournal )
b4f90 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
b4fa0 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
b4fb0 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 50 61 67  else{.      pPag
b4fc0 65 72 2d 3e 6f 72 69 67 44 62 53 69 7a 65 20 3d  er->origDbSize =
b4fd0 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b   pPager->dbSize;
b4fe0 0a 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74  .      rc = writ
b4ff0 65 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67  eJournalHdr(pPag
b5000 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  er);.    }.  }. 
b5010 20 61 73 73 65 72 74 28 20 21 70 50 61 67 65 72   assert( !pPager
b5020 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 7c 7c  ->journalOpen ||
b5030 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
b5040 4f 66 66 3e 30 20 7c 7c 20 72 63 21 3d 53 51 4c  Off>0 || rc!=SQL
b5050 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 72 65 74 75  ITE_OK );.  retu
b5060 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
b5070 4d 61 6b 65 20 61 20 70 61 67 65 20 64 69 72 74  Make a page dirt
b5080 79 2e 20 20 53 65 74 20 69 74 73 20 64 69 72 74  y.  Set its dirt
b5090 79 20 66 6c 61 67 20 61 6e 64 20 61 64 64 20 69  y flag and add i
b50a0 74 20 74 6f 20 74 68 65 20 64 69 72 74 79 0a 2a  t to the dirty.*
b50b0 2a 20 70 61 67 65 20 6c 69 73 74 2e 0a 2a 2f 0a  * page list..*/.
b50c0 73 74 61 74 69 63 20 76 6f 69 64 20 6d 61 6b 65  static void make
b50d0 44 69 72 74 79 28 50 67 48 64 72 20 2a 70 50 67  Dirty(PgHdr *pPg
b50e0 29 7b 0a 20 20 69 66 28 20 70 50 67 2d 3e 64 69  ){.  if( pPg->di
b50f0 72 74 79 3d 3d 30 20 29 7b 0a 20 20 20 20 50 61  rty==0 ){.    Pa
b5100 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50  ger *pPager = pP
b5110 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 20 20 70  g->pPager;.    p
b5120 50 67 2d 3e 64 69 72 74 79 20 3d 20 31 3b 0a 20  Pg->dirty = 1;. 
b5130 20 20 20 70 50 67 2d 3e 70 44 69 72 74 79 20 3d     pPg->pDirty =
b5140 20 70 50 61 67 65 72 2d 3e 70 44 69 72 74 79 3b   pPager->pDirty;
b5150 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d  .    if( pPager-
b5160 3e 70 44 69 72 74 79 20 29 7b 0a 20 20 20 20 20  >pDirty ){.     
b5170 20 70 50 61 67 65 72 2d 3e 70 44 69 72 74 79 2d   pPager->pDirty-
b5180 3e 70 50 72 65 76 44 69 72 74 79 20 3d 20 70 50  >pPrevDirty = pP
b5190 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 67  g;.    }.    pPg
b51a0 2d 3e 70 50 72 65 76 44 69 72 74 79 20 3d 20 30  ->pPrevDirty = 0
b51b0 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 44  ;.    pPager->pD
b51c0 69 72 74 79 20 3d 20 70 50 67 3b 0a 20 20 7d 0a  irty = pPg;.  }.
b51d0 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20  }../*.** Make a 
b51e0 70 61 67 65 20 63 6c 65 61 6e 2e 20 20 43 6c 65  page clean.  Cle
b51f0 61 72 20 69 74 73 20 64 69 72 74 79 20 62 69 74  ar its dirty bit
b5200 20 61 6e 64 20 72 65 6d 6f 76 65 20 69 74 20 66   and remove it f
b5210 72 6f 6d 20 74 68 65 0a 2a 2a 20 64 69 72 74 79  rom the.** dirty
b5220 20 70 61 67 65 20 6c 69 73 74 2e 0a 2a 2f 0a 73   page list..*/.s
b5230 74 61 74 69 63 20 76 6f 69 64 20 6d 61 6b 65 43  tatic void makeC
b5240 6c 65 61 6e 28 50 67 48 64 72 20 2a 70 50 67 29  lean(PgHdr *pPg)
b5250 7b 0a 20 20 69 66 28 20 70 50 67 2d 3e 64 69 72  {.  if( pPg->dir
b5260 74 79 20 29 7b 0a 20 20 20 20 70 50 67 2d 3e 64  ty ){.    pPg->d
b5270 69 72 74 79 20 3d 20 30 3b 0a 20 20 20 20 69 66  irty = 0;.    if
b5280 28 20 70 50 67 2d 3e 70 44 69 72 74 79 20 29 7b  ( pPg->pDirty ){
b5290 0a 20 20 20 20 20 20 70 50 67 2d 3e 70 44 69 72  .      pPg->pDir
b52a0 74 79 2d 3e 70 50 72 65 76 44 69 72 74 79 20 3d  ty->pPrevDirty =
b52b0 20 70 50 67 2d 3e 70 50 72 65 76 44 69 72 74 79   pPg->pPrevDirty
b52c0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
b52d0 70 50 67 2d 3e 70 50 72 65 76 44 69 72 74 79 20  pPg->pPrevDirty 
b52e0 29 7b 0a 20 20 20 20 20 20 70 50 67 2d 3e 70 50  ){.      pPg->pP
b52f0 72 65 76 44 69 72 74 79 2d 3e 70 44 69 72 74 79  revDirty->pDirty
b5300 20 3d 20 70 50 67 2d 3e 70 44 69 72 74 79 3b 0a   = pPg->pDirty;.
b5310 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
b5320 20 70 50 67 2d 3e 70 50 61 67 65 72 2d 3e 70 44   pPg->pPager->pD
b5330 69 72 74 79 20 3d 20 70 50 67 2d 3e 70 44 69 72  irty = pPg->pDir
b5340 74 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  ty;.    }.  }.}.
b5350 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 61 20 64  ../*.** Mark a d
b5360 61 74 61 20 70 61 67 65 20 61 73 20 77 72 69 74  ata page as writ
b5370 65 61 62 6c 65 2e 20 20 54 68 65 20 70 61 67 65  eable.  The page
b5380 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f   is written into
b5390 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2a   the journal .**
b53a0 20 69 66 20 69 74 20 69 73 20 6e 6f 74 20 74 68   if it is not th
b53b0 65 72 65 20 61 6c 72 65 61 64 79 2e 20 20 54 68  ere already.  Th
b53c0 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20  is routine must 
b53d0 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65  be called before
b53e0 20 6d 61 6b 69 6e 67 0a 2a 2a 20 63 68 61 6e 67   making.** chang
b53f0 65 73 20 74 6f 20 61 20 70 61 67 65 2e 0a 2a 2a  es to a page..**
b5400 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 69  .** The first ti
b5410 6d 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  me this routine 
b5420 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 70  is called, the p
b5430 61 67 65 72 20 63 72 65 61 74 65 73 20 61 20 6e  ager creates a n
b5440 65 77 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 61 6e  ew.** journal an
b5450 64 20 61 63 71 75 69 72 65 73 20 61 20 52 45 53  d acquires a RES
b5460 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68  ERVED lock on th
b5470 65 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 20  e database.  If 
b5480 74 68 65 20 52 45 53 45 52 56 45 44 0a 2a 2a 20  the RESERVED.** 
b5490 6c 6f 63 6b 20 63 6f 75 6c 64 20 6e 6f 74 20 62  lock could not b
b54a0 65 20 61 63 71 75 69 72 65 64 2c 20 74 68 69 73  e acquired, this
b54b0 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
b54c0 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 54   SQLITE_BUSY.  T
b54d0 68 65 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 72 6f  he.** calling ro
b54e0 75 74 69 6e 65 20 6d 75 73 74 20 63 68 65 63 6b  utine must check
b54f0 20 66 6f 72 20 74 68 61 74 20 72 65 74 75 72 6e   for that return
b5500 20 76 61 6c 75 65 20 61 6e 64 20 62 65 20 63 61   value and be ca
b5510 72 65 66 75 6c 20 6e 6f 74 20 74 6f 0a 2a 2a 20  reful not to.** 
b5520 63 68 61 6e 67 65 20 61 6e 79 20 70 61 67 65 20  change any page 
b5530 64 61 74 61 20 75 6e 74 69 6c 20 74 68 69 73 20  data until this 
b5540 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
b5550 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a  SQLITE_OK..**.**
b5560 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   If the journal 
b5570 66 69 6c 65 20 63 6f 75 6c 64 20 6e 6f 74 20 62  file could not b
b5580 65 20 77 72 69 74 74 65 6e 20 62 65 63 61 75 73  e written becaus
b5590 65 20 74 68 65 20 64 69 73 6b 20 69 73 20 66 75  e the disk is fu
b55a0 6c 6c 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 69 73  ll,.** then this
b55b0 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
b55c0 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 61 6e 64   SQLITE_FULL and
b55d0 20 64 6f 65 73 20 61 6e 20 69 6d 6d 65 64 69 61   does an immedia
b55e0 74 65 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 20  te rollback..** 
b55f0 41 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20 77  All subsequent w
b5600 72 69 74 65 20 61 74 74 65 6d 70 74 73 20 61 6c  rite attempts al
b5610 73 6f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  so return SQLITE
b5620 5f 46 55 4c 4c 20 75 6e 74 69 6c 20 74 68 65 72  _FULL until ther
b5630 65 0a 2a 2a 20 69 73 20 61 20 63 61 6c 6c 20 74  e.** is a call t
b5640 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f  o sqlite3PagerCo
b5650 6d 6d 69 74 28 29 20 6f 72 20 73 71 6c 69 74 65  mmit() or sqlite
b5660 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 29  3PagerRollback()
b5670 20 74 6f 0a 2a 2a 20 72 65 73 65 74 2e 0a 2a 2f   to.** reset..*/
b5680 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65  .static int page
b5690 72 5f 77 72 69 74 65 28 50 67 48 64 72 20 2a 70  r_write(PgHdr *p
b56a0 50 67 29 7b 0a 20 20 76 6f 69 64 20 2a 70 44 61  Pg){.  void *pDa
b56b0 74 61 20 3d 20 50 47 48 44 52 5f 54 4f 5f 44 41  ta = PGHDR_TO_DA
b56c0 54 41 28 70 50 67 29 3b 0a 20 20 50 61 67 65 72  TA(pPg);.  Pager
b56d0 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e   *pPager = pPg->
b56e0 70 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 72 63  pPager;.  int rc
b56f0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20   = SQLITE_OK;.. 
b5700 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 65 72   /* Check for er
b5710 72 6f 72 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20  rors.  */.  if( 
b5720 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20  pPager->errCode 
b5730 29 7b 20 0a 20 20 20 20 72 65 74 75 72 6e 20 70  ){ .    return p
b5740 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a  Pager->errCode;.
b5750 20 20 7d 0a 20 20 69 66 28 20 70 50 61 67 65 72    }.  if( pPager
b5760 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20  ->readOnly ){.  
b5770 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
b5780 50 45 52 4d 3b 0a 20 20 7d 0a 0a 20 20 61 73 73  PERM;.  }..  ass
b5790 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e 73 65  ert( !pPager->se
b57a0 74 4d 61 73 74 65 72 20 29 3b 0a 0a 20 20 43 48  tMaster );..  CH
b57b0 45 43 4b 5f 50 41 47 45 28 70 50 67 29 3b 0a 0a  ECK_PAGE(pPg);..
b57c0 20 20 2f 2a 20 49 66 20 74 68 69 73 20 70 61 67    /* If this pag
b57d0 65 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79  e was previously
b57e0 20 61 63 71 75 69 72 65 64 20 77 69 74 68 20 6e   acquired with n
b57f0 6f 43 6f 6e 74 65 6e 74 3d 3d 31 2c 20 74 68 61  oContent==1, tha
b5800 74 20 6d 65 61 6e 73 0a 20 20 2a 2a 20 77 65 20  t means.  ** we 
b5810 64 69 64 6e 27 74 20 72 65 61 6c 6c 79 20 72 65  didn't really re
b5820 61 64 20 69 6e 20 74 68 65 20 63 6f 6e 74 65 6e  ad in the conten
b5830 74 20 6f 66 20 74 68 65 20 70 61 67 65 2e 20 20  t of the page.  
b5840 54 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e 0a  This can happen.
b5850 20 20 2a 2a 20 28 66 6f 72 20 65 78 61 6d 70 6c    ** (for exampl
b5860 65 29 20 77 68 65 6e 20 74 68 65 20 70 61 67 65  e) when the page
b5870 20 69 73 20 62 65 69 6e 67 20 6d 6f 76 65 64 20   is being moved 
b5880 74 6f 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e  to the freelist.
b5890 20 20 42 75 74 0a 20 20 2a 2a 20 6e 6f 77 20 77    But.  ** now w
b58a0 65 20 61 72 65 20 28 70 65 72 68 61 70 73 29 20  e are (perhaps) 
b58b0 6d 6f 76 69 6e 67 20 74 68 65 20 70 61 67 65 20  moving the page 
b58c0 6f 66 66 20 6f 66 20 74 68 65 20 66 72 65 65 6c  off of the freel
b58d0 69 73 74 20 66 6f 72 0a 20 20 2a 2a 20 72 65 75  ist for.  ** reu
b58e0 73 65 20 61 6e 64 20 77 65 20 6e 65 65 64 20 74  se and we need t
b58f0 6f 20 6b 6e 6f 77 20 69 74 73 20 6f 72 69 67 69  o know its origi
b5900 6e 61 6c 20 63 6f 6e 74 65 6e 74 20 73 6f 20 74  nal content so t
b5910 68 61 74 20 63 6f 6e 74 65 6e 74 0a 20 20 2a 2a  hat content.  **
b5920 20 63 61 6e 20 62 65 20 73 74 6f 72 65 64 20 69   can be stored i
b5930 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a  n the rollback j
b5940 6f 75 72 6e 61 6c 2e 20 20 53 6f 20 64 6f 20 74  ournal.  So do t
b5950 68 65 20 72 65 61 64 20 61 74 20 74 68 69 73 0a  he read at this.
b5960 20 20 2a 2a 20 74 69 6d 65 2e 0a 20 20 2a 2f 0a    ** time..  */.
b5970 20 20 72 63 20 3d 20 70 61 67 65 72 5f 67 65 74    rc = pager_get
b5980 5f 63 6f 6e 74 65 6e 74 28 70 50 67 29 3b 0a 20  _content(pPg);. 
b5990 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72   if( rc ){.    r
b59a0 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20  eturn rc;.  }.. 
b59b0 20 2f 2a 20 4d 61 72 6b 20 74 68 65 20 70 61 67   /* Mark the pag
b59c0 65 20 61 73 20 64 69 72 74 79 2e 20 20 49 66 20  e as dirty.  If 
b59d0 74 68 65 20 70 61 67 65 20 68 61 73 20 61 6c 72  the page has alr
b59e0 65 61 64 79 20 62 65 65 6e 20 77 72 69 74 74 65  eady been writte
b59f0 6e 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 6a 6f  n.  ** to the jo
b5a00 75 72 6e 61 6c 20 74 68 65 6e 20 77 65 20 63 61  urnal then we ca
b5a10 6e 20 72 65 74 75 72 6e 20 72 69 67 68 74 20 61  n return right a
b5a20 77 61 79 2e 0a 20 20 2a 2f 0a 20 20 6d 61 6b 65  way..  */.  make
b5a30 44 69 72 74 79 28 70 50 67 29 3b 0a 20 20 69 66  Dirty(pPg);.  if
b5a40 28 20 70 50 67 2d 3e 69 6e 4a 6f 75 72 6e 61 6c  ( pPg->inJournal
b5a50 20 26 26 20 28 70 61 67 65 49 6e 53 74 61 74 65   && (pageInState
b5a60 6d 65 6e 74 28 70 50 67 29 20 7c 7c 20 70 50 61  ment(pPg) || pPa
b5a70 67 65 72 2d 3e 73 74 6d 74 49 6e 55 73 65 3d 3d  ger->stmtInUse==
b5a80 30 29 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72  0) ){.    pPager
b5a90 2d 3e 64 69 72 74 79 43 61 63 68 65 20 3d 20 31  ->dirtyCache = 1
b5aa0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 0a 20 20 20 20  ;.  }else{..    
b5ab0 2f 2a 20 49 66 20 77 65 20 67 65 74 20 74 68 69  /* If we get thi
b5ac0 73 20 66 61 72 2c 20 69 74 20 6d 65 61 6e 73 20  s far, it means 
b5ad0 74 68 61 74 20 74 68 65 20 70 61 67 65 20 6e 65  that the page ne
b5ae0 65 64 73 20 74 6f 20 62 65 0a 20 20 20 20 2a 2a  eds to be.    **
b5af0 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20   written to the 
b5b00 74 72 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72  transaction jour
b5b10 6e 61 6c 20 6f 72 20 74 68 65 20 63 6b 65 63 6b  nal or the ckeck
b5b20 70 6f 69 6e 74 20 6a 6f 75 72 6e 61 6c 0a 20 20  point journal.  
b5b30 20 20 2a 2a 20 6f 72 20 62 6f 74 68 2e 0a 20 20    ** or both..  
b5b40 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 69 72 73    **.    ** Firs
b5b50 74 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 74  t check to see t
b5b60 68 61 74 20 74 68 65 20 74 72 61 6e 73 61 63 74  hat the transact
b5b70 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 65 78 69 73  ion journal exis
b5b80 74 73 20 61 6e 64 0a 20 20 20 20 2a 2a 20 63 72  ts and.    ** cr
b5b90 65 61 74 65 20 69 74 20 69 66 20 69 74 20 64 6f  eate it if it do
b5ba0 65 73 20 6e 6f 74 2e 0a 20 20 20 20 2a 2f 0a 20  es not..    */. 
b5bb0 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
b5bc0 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 52 5f  r->state!=PAGER_
b5bd0 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 63  UNLOCK );.    rc
b5be0 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 42   = sqlite3PagerB
b5bf0 65 67 69 6e 28 70 50 67 2c 20 30 29 3b 0a 20 20  egin(pPg, 0);.  
b5c00 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
b5c10 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74  _OK ){.      ret
b5c20 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
b5c30 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
b5c40 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52  ->state>=PAGER_R
b5c50 45 53 45 52 56 45 44 20 29 3b 0a 20 20 20 20 69  ESERVED );.    i
b5c60 66 28 20 21 70 50 61 67 65 72 2d 3e 6a 6f 75 72  f( !pPager->jour
b5c70 6e 61 6c 4f 70 65 6e 20 26 26 20 70 50 61 67 65  nalOpen && pPage
b5c80 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 7b  r->useJournal ){
b5c90 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65  .      rc = page
b5ca0 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 70  r_open_journal(p
b5cb0 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 69 66  Pager);.      if
b5cc0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
b5cd0 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
b5ce0 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70   }.    assert( p
b5cf0 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70  Pager->journalOp
b5d00 65 6e 20 7c 7c 20 21 70 50 61 67 65 72 2d 3e 75  en || !pPager->u
b5d10 73 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 20  seJournal );.   
b5d20 20 70 50 61 67 65 72 2d 3e 64 69 72 74 79 43 61   pPager->dirtyCa
b5d30 63 68 65 20 3d 20 31 3b 0a 20 20 0a 20 20 20 20  che = 1;.  .    
b5d40 2f 2a 20 54 68 65 20 74 72 61 6e 73 61 63 74 69  /* The transacti
b5d50 6f 6e 20 6a 6f 75 72 6e 61 6c 20 6e 6f 77 20 65  on journal now e
b5d60 78 69 73 74 73 20 61 6e 64 20 77 65 20 68 61 76  xists and we hav
b5d70 65 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20  e a RESERVED or 
b5d80 61 6e 0a 20 20 20 20 2a 2a 20 45 58 43 4c 55 53  an.    ** EXCLUS
b5d90 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  IVE lock on the 
b5da0 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69  main database fi
b5db0 6c 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 63  le.  Write the c
b5dc0 75 72 72 65 6e 74 20 70 61 67 65 20 74 6f 0a 20  urrent page to. 
b5dd0 20 20 20 2a 2a 20 74 68 65 20 74 72 61 6e 73 61     ** the transa
b5de0 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 69 66  ction journal if
b5df0 20 69 74 20 69 73 20 6e 6f 74 20 74 68 65 72 65   it is not there
b5e00 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f   already..    */
b5e10 0a 20 20 20 20 69 66 28 20 21 70 50 67 2d 3e 69  .    if( !pPg->i
b5e20 6e 4a 6f 75 72 6e 61 6c 20 26 26 20 28 70 50 61  nJournal && (pPa
b5e30 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20  ger->useJournal 
b5e40 7c 7c 20 4d 45 4d 44 42 29 20 29 7b 0a 20 20 20  || MEMDB) ){.   
b5e50 20 20 20 69 66 28 20 28 69 6e 74 29 70 50 67 2d     if( (int)pPg-
b5e60 3e 70 67 6e 6f 20 3c 3d 20 70 50 61 67 65 72 2d  >pgno <= pPager-
b5e70 3e 6f 72 69 67 44 62 53 69 7a 65 20 29 7b 0a 20  >origDbSize ){. 
b5e80 20 20 20 20 20 20 20 69 6e 74 20 73 7a 50 67 3b         int szPg;
b5e90 0a 20 20 20 20 20 20 20 20 69 66 28 20 4d 45 4d  .        if( MEM
b5ea0 44 42 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  DB ){.          
b5eb0 50 67 48 69 73 74 6f 72 79 20 2a 70 48 69 73 74  PgHistory *pHist
b5ec0 20 3d 20 50 47 48 44 52 5f 54 4f 5f 48 49 53 54   = PGHDR_TO_HIST
b5ed0 28 70 50 67 2c 20 70 50 61 67 65 72 29 3b 0a 20  (pPg, pPager);. 
b5ee0 20 20 20 20 20 20 20 20 20 50 41 47 45 52 54 52           PAGERTR
b5ef0 41 43 45 33 28 22 4a 4f 55 52 4e 41 4c 20 25 64  ACE3("JOURNAL %d
b5f00 20 70 61 67 65 20 25 64 5c 6e 22 2c 20 50 41 47   page %d\n", PAG
b5f10 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 50  ERID(pPager), pP
b5f20 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20  g->pgno);.      
b5f30 20 20 20 20 61 73 73 65 72 74 28 20 70 48 69 73      assert( pHis
b5f40 74 2d 3e 70 4f 72 69 67 3d 3d 30 20 29 3b 0a 20  t->pOrig==0 );. 
b5f50 20 20 20 20 20 20 20 20 20 70 48 69 73 74 2d 3e           pHist->
b5f60 70 4f 72 69 67 20 3d 20 73 71 6c 69 74 65 4d 61  pOrig = sqliteMa
b5f70 6c 6c 6f 63 52 61 77 28 20 70 50 61 67 65 72 2d  llocRaw( pPager-
b5f80 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 20  >pageSize );.   
b5f90 20 20 20 20 20 20 20 69 66 28 20 70 48 69 73 74         if( pHist
b5fa0 2d 3e 70 4f 72 69 67 20 29 7b 0a 20 20 20 20 20  ->pOrig ){.     
b5fb0 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 48         memcpy(pH
b5fc0 69 73 74 2d 3e 70 4f 72 69 67 2c 20 50 47 48 44  ist->pOrig, PGHD
b5fd0 52 5f 54 4f 5f 44 41 54 41 28 70 50 67 29 2c 20  R_TO_DATA(pPg), 
b5fe0 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
b5ff0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  );.          }. 
b6000 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
b6010 20 20 20 20 20 20 20 20 75 33 32 20 63 6b 73 75          u32 cksu
b6020 6d 2c 20 73 61 76 65 64 3b 0a 20 20 20 20 20 20  m, saved;.      
b6030 20 20 20 20 63 68 61 72 20 2a 70 44 61 74 61 32      char *pData2
b6040 2c 20 2a 70 45 6e 64 3b 0a 20 20 20 20 20 20 20  , *pEnd;.       
b6050 20 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20     /* We should 
b6060 6e 65 76 65 72 20 77 72 69 74 65 20 74 6f 20 74  never write to t
b6070 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
b6080 74 68 65 20 70 61 67 65 20 74 68 61 74 0a 20 20  the page that.  
b6090 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61          ** conta
b60a0 69 6e 73 20 74 68 65 20 64 61 74 61 62 61 73 65  ins the database
b60b0 20 6c 6f 63 6b 73 2e 20 20 54 68 65 20 66 6f 6c   locks.  The fol
b60c0 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 20 76 65  lowing assert ve
b60d0 72 69 66 69 65 73 0a 20 20 20 20 20 20 20 20 20  rifies.         
b60e0 20 2a 2a 20 74 68 61 74 20 77 65 20 64 6f 20 6e   ** that we do n
b60f0 6f 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  ot. */.         
b6100 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 70 67   assert( pPg->pg
b6110 6e 6f 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e  no!=PAGER_MJ_PGN
b6120 4f 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 20  O(pPager) );.   
b6130 20 20 20 20 20 20 20 70 44 61 74 61 32 20 3d 20         pData2 = 
b6140 43 4f 44 45 43 32 28 70 50 61 67 65 72 2c 20 70  CODEC2(pPager, p
b6150 44 61 74 61 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c  Data, pPg->pgno,
b6160 20 37 29 3b 0a 20 20 20 20 20 20 20 20 20 20 63   7);.          c
b6170 6b 73 75 6d 20 3d 20 70 61 67 65 72 5f 63 6b 73  ksum = pager_cks
b6180 75 6d 28 70 50 61 67 65 72 2c 20 28 75 38 2a 29  um(pPager, (u8*)
b6190 70 44 61 74 61 32 29 3b 0a 20 20 20 20 20 20 20  pData2);.       
b61a0 20 20 20 70 45 6e 64 20 3d 20 70 44 61 74 61 32     pEnd = pData2
b61b0 20 2b 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53   + pPager->pageS
b61c0 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 20 20 70  ize;.          p
b61d0 44 61 74 61 32 20 2d 3d 20 34 3b 0a 20 20 20 20  Data2 -= 4;.    
b61e0 20 20 20 20 20 20 73 61 76 65 64 20 3d 20 2a 28        saved = *(
b61f0 75 33 32 2a 29 70 45 6e 64 3b 0a 20 20 20 20 20  u32*)pEnd;.     
b6200 20 20 20 20 20 70 75 74 33 32 62 69 74 73 28 70       put32bits(p
b6210 45 6e 64 2c 20 63 6b 73 75 6d 29 3b 0a 20 20 20  End, cksum);.   
b6220 20 20 20 20 20 20 20 73 7a 50 67 20 3d 20 70 50         szPg = pP
b6230 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2b 38  ager->pageSize+8
b6240 3b 0a 20 20 20 20 20 20 20 20 20 20 70 75 74 33  ;.          put3
b6250 32 62 69 74 73 28 70 44 61 74 61 32 2c 20 70 50  2bits(pData2, pP
b6260 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20  g->pgno);.      
b6270 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
b6280 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e  OsWrite(pPager->
b6290 6a 66 64 2c 20 70 44 61 74 61 32 2c 20 73 7a 50  jfd, pData2, szP
b62a0 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 49 4f  g);.          IO
b62b0 54 52 41 43 45 28 28 22 4a 4f 55 54 20 25 70 20  TRACE(("JOUT %p 
b62c0 25 64 20 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 70  %d %lld %d\n", p
b62d0 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f  Pager, pPg->pgno
b62e0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
b62f0 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75       pPager->jou
b6300 72 6e 61 6c 4f 66 66 2c 20 73 7a 50 67 29 29 3b  rnalOff, szPg));
b6310 0a 20 20 20 20 20 20 20 20 20 20 50 41 47 45 52  .          PAGER
b6320 5f 49 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 61  _INCR(sqlite3_pa
b6330 67 65 72 5f 77 72 69 74 65 6a 5f 63 6f 75 6e 74  ger_writej_count
b6340 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50 61  );.          pPa
b6350 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20  ger->journalOff 
b6360 2b 3d 20 73 7a 50 67 3b 0a 20 20 20 20 20 20 20  += szPg;.       
b6370 20 20 20 50 41 47 45 52 54 52 41 43 45 35 28 22     PAGERTRACE5("
b6380 4a 4f 55 52 4e 41 4c 20 25 64 20 70 61 67 65 20  JOURNAL %d page 
b6390 25 64 20 6e 65 65 64 53 79 6e 63 3d 25 64 20 68  %d needSync=%d h
b63a0 61 73 68 28 25 30 38 78 29 5c 6e 22 2c 0a 20 20  ash(%08x)\n",.  
b63b0 20 20 20 20 20 20 20 20 20 20 20 20 20 50 41 47               PAG
b63c0 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 50  ERID(pPager), pP
b63d0 67 2d 3e 70 67 6e 6f 2c 20 70 50 67 2d 3e 6e 65  g->pgno, pPg->ne
b63e0 65 64 53 79 6e 63 2c 20 70 61 67 65 72 5f 70 61  edSync, pager_pa
b63f0 67 65 68 61 73 68 28 70 50 67 29 29 3b 0a 20 20  gehash(pPg));.  
b6400 20 20 20 20 20 20 20 20 2a 28 75 33 32 2a 29 70          *(u32*)p
b6410 45 6e 64 20 3d 20 73 61 76 65 64 3b 0a 0a 09 20  End = saved;... 
b6420 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20 68 61 73   /* An error has
b6430 20 6f 63 63 75 72 65 64 20 77 72 69 74 69 6e 67   occured writing
b6440 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   to the journal 
b6450 66 69 6c 65 2e 20 54 68 65 20 0a 20 20 20 20 20  file. The .     
b6460 20 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74       ** transact
b6470 69 6f 6e 20 77 69 6c 6c 20 62 65 20 72 6f 6c 6c  ion will be roll
b6480 65 64 20 62 61 63 6b 20 62 79 20 74 68 65 20 6c  ed back by the l
b6490 61 79 65 72 20 61 62 6f 76 65 2e 0a 20 20 20 20  ayer above..    
b64a0 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
b64b0 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
b64c0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
b64d0 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
b64e0 20 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20           }..    
b64f0 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52        pPager->nR
b6500 65 63 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20  ec++;.          
b6510 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
b6520 61 49 6e 4a 6f 75 72 6e 61 6c 21 3d 30 20 29 3b  aInJournal!=0 );
b6530 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65  .          pPage
b6540 72 2d 3e 61 49 6e 4a 6f 75 72 6e 61 6c 5b 70 50  r->aInJournal[pP
b6550 67 2d 3e 70 67 6e 6f 2f 38 5d 20 7c 3d 20 31 3c  g->pgno/8] |= 1<
b6560 3c 28 70 50 67 2d 3e 70 67 6e 6f 26 37 29 3b 0a  <(pPg->pgno&7);.
b6570 20 20 20 20 20 20 20 20 20 20 70 50 67 2d 3e 6e            pPg->n
b6580 65 65 64 53 79 6e 63 20 3d 20 21 70 50 61 67 65  eedSync = !pPage
b6590 72 2d 3e 6e 6f 53 79 6e 63 3b 0a 20 20 20 20 20  r->noSync;.     
b65a0 20 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d       if( pPager-
b65b0 3e 73 74 6d 74 49 6e 55 73 65 20 29 7b 0a 20 20  >stmtInUse ){.  
b65c0 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72            pPager
b65d0 2d 3e 61 49 6e 53 74 6d 74 5b 70 50 67 2d 3e 70  ->aInStmt[pPg->p
b65e0 67 6e 6f 2f 38 5d 20 7c 3d 20 31 3c 3c 28 70 50  gno/8] |= 1<<(pP
b65f0 67 2d 3e 70 67 6e 6f 26 37 29 3b 0a 20 20 20 20  g->pgno&7);.    
b6600 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
b6610 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  }.      }else{. 
b6620 20 20 20 20 20 20 20 70 50 67 2d 3e 6e 65 65 64         pPg->need
b6630 53 79 6e 63 20 3d 20 21 70 50 61 67 65 72 2d 3e  Sync = !pPager->
b6640 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 26  journalStarted &
b6650 26 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e  & !pPager->noSyn
b6660 63 3b 0a 20 20 20 20 20 20 20 20 50 41 47 45 52  c;.        PAGER
b6670 54 52 41 43 45 34 28 22 41 50 50 45 4e 44 20 25  TRACE4("APPEND %
b6680 64 20 70 61 67 65 20 25 64 20 6e 65 65 64 53 79  d page %d needSy
b6690 6e 63 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20  nc=%d\n",.      
b66a0 20 20 20 20 20 20 20 20 20 20 50 41 47 45 52 49            PAGERI
b66b0 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e  D(pPager), pPg->
b66c0 70 67 6e 6f 2c 20 70 50 67 2d 3e 6e 65 65 64 53  pgno, pPg->needS
b66d0 79 6e 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ync);.      }.  
b66e0 20 20 20 20 69 66 28 20 70 50 67 2d 3e 6e 65 65      if( pPg->nee
b66f0 64 53 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 20  dSync ){.       
b6700 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e   pPager->needSyn
b6710 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  c = 1;.      }. 
b6720 20 20 20 20 20 70 50 67 2d 3e 69 6e 4a 6f 75 72       pPg->inJour
b6730 6e 61 6c 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  nal = 1;.    }. 
b6740 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20   .    /* If the 
b6750 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61  statement journa
b6760 6c 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 74 68  l is open and th
b6770 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e  e page is not in
b6780 20 69 74 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e   it,.    ** then
b6790 20 77 72 69 74 65 20 74 68 65 20 63 75 72 72 65   write the curre
b67a0 6e 74 20 70 61 67 65 20 74 6f 20 74 68 65 20 73  nt page to the s
b67b0 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c  tatement journal
b67c0 2e 20 20 4e 6f 74 65 20 74 68 61 74 0a 20 20 20  .  Note that.   
b67d0 20 2a 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e   ** the statemen
b67e0 74 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74  t journal format
b67f0 20 64 69 66 66 65 72 73 20 66 72 6f 6d 20 74 68   differs from th
b6800 65 20 73 74 61 6e 64 61 72 64 20 6a 6f 75 72 6e  e standard journ
b6810 61 6c 20 66 6f 72 6d 61 74 0a 20 20 20 20 2a 2a  al format.    **
b6820 20 69 6e 20 74 68 61 74 20 69 74 20 6f 6d 69 74   in that it omit
b6830 73 20 74 68 65 20 63 68 65 63 6b 73 75 6d 73 20  s the checksums 
b6840 61 6e 64 20 74 68 65 20 68 65 61 64 65 72 2e 0a  and the header..
b6850 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
b6860 50 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55 73 65  Pager->stmtInUse
b6870 20 0a 20 20 20 20 20 26 26 20 21 70 61 67 65 49   .     && !pageI
b6880 6e 53 74 61 74 65 6d 65 6e 74 28 70 50 67 29 20  nStatement(pPg) 
b6890 0a 20 20 20 20 20 26 26 20 28 69 6e 74 29 70 50  .     && (int)pP
b68a0 67 2d 3e 70 67 6e 6f 3c 3d 70 50 61 67 65 72 2d  g->pgno<=pPager-
b68b0 3e 73 74 6d 74 53 69 7a 65 20 0a 20 20 20 20 29  >stmtSize .    )
b68c0 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
b68d0 70 50 67 2d 3e 69 6e 4a 6f 75 72 6e 61 6c 20 7c  pPg->inJournal |
b68e0 7c 20 28 69 6e 74 29 70 50 67 2d 3e 70 67 6e 6f  | (int)pPg->pgno
b68f0 3e 70 50 61 67 65 72 2d 3e 6f 72 69 67 44 62 53  >pPager->origDbS
b6900 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 69 66 28  ize );.      if(
b6910 20 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 20 20   MEMDB ){.      
b6920 20 20 50 67 48 69 73 74 6f 72 79 20 2a 70 48 69    PgHistory *pHi
b6930 73 74 20 3d 20 50 47 48 44 52 5f 54 4f 5f 48 49  st = PGHDR_TO_HI
b6940 53 54 28 70 50 67 2c 20 70 50 61 67 65 72 29 3b  ST(pPg, pPager);
b6950 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
b6960 20 70 48 69 73 74 2d 3e 70 53 74 6d 74 3d 3d 30   pHist->pStmt==0
b6970 20 29 3b 0a 20 20 20 20 20 20 20 20 70 48 69 73   );.        pHis
b6980 74 2d 3e 70 53 74 6d 74 20 3d 20 73 71 6c 69 74  t->pStmt = sqlit
b6990 65 4d 61 6c 6c 6f 63 52 61 77 28 20 70 50 61 67  eMallocRaw( pPag
b69a0 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a  er->pageSize );.
b69b0 20 20 20 20 20 20 20 20 69 66 28 20 70 48 69 73          if( pHis
b69c0 74 2d 3e 70 53 74 6d 74 20 29 7b 0a 20 20 20 20  t->pStmt ){.    
b69d0 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 48 69        memcpy(pHi
b69e0 73 74 2d 3e 70 53 74 6d 74 2c 20 50 47 48 44 52  st->pStmt, PGHDR
b69f0 5f 54 4f 5f 44 41 54 41 28 70 50 67 29 2c 20 70  _TO_DATA(pPg), p
b6a00 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29  Pager->pageSize)
b6a10 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
b6a20 20 20 20 20 50 41 47 45 52 54 52 41 43 45 33 28      PAGERTRACE3(
b6a30 22 53 54 4d 54 2d 4a 4f 55 52 4e 41 4c 20 25 64  "STMT-JOURNAL %d
b6a40 20 70 61 67 65 20 25 64 5c 6e 22 2c 20 50 41 47   page %d\n", PAG
b6a50 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 50  ERID(pPager), pP
b6a60 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20  g->pgno);.      
b6a70 20 20 70 61 67 65 5f 61 64 64 5f 74 6f 5f 73 74    page_add_to_st
b6a80 6d 74 5f 6c 69 73 74 28 70 50 67 29 3b 0a 20 20  mt_list(pPg);.  
b6a90 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
b6aa0 20 20 20 63 68 61 72 20 2a 70 44 61 74 61 32 20     char *pData2 
b6ab0 3d 20 43 4f 44 45 43 32 28 70 50 61 67 65 72 2c  = CODEC2(pPager,
b6ac0 20 70 44 61 74 61 2c 20 70 50 67 2d 3e 70 67 6e   pData, pPg->pgn
b6ad0 6f 2c 20 37 29 2d 34 3b 0a 20 20 20 20 20 20 20  o, 7)-4;.       
b6ae0 20 70 75 74 33 32 62 69 74 73 28 70 44 61 74 61   put32bits(pData
b6af0 32 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20  2, pPg->pgno);. 
b6b00 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
b6b10 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65  te3OsWrite(pPage
b6b20 72 2d 3e 73 74 66 64 2c 20 70 44 61 74 61 32 2c  r->stfd, pData2,
b6b30 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a   pPager->pageSiz
b6b40 65 2b 34 29 3b 0a 20 20 20 20 20 20 20 20 50 41  e+4);.        PA
b6b50 47 45 52 54 52 41 43 45 33 28 22 53 54 4d 54 2d  GERTRACE3("STMT-
b6b60 4a 4f 55 52 4e 41 4c 20 25 64 20 70 61 67 65 20  JOURNAL %d page 
b6b70 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70  %d\n", PAGERID(p
b6b80 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e  Pager), pPg->pgn
b6b90 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  o);.        if( 
b6ba0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
b6bb0 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
b6bc0 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a  n rc;.        }.
b6bd0 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e          pPager->
b6be0 73 74 6d 74 4e 52 65 63 2b 2b 3b 0a 20 20 20 20  stmtNRec++;.    
b6bf0 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
b6c00 65 72 2d 3e 61 49 6e 53 74 6d 74 21 3d 30 20 29  er->aInStmt!=0 )
b6c10 3b 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72  ;.        pPager
b6c20 2d 3e 61 49 6e 53 74 6d 74 5b 70 50 67 2d 3e 70  ->aInStmt[pPg->p
b6c30 67 6e 6f 2f 38 5d 20 7c 3d 20 31 3c 3c 28 70 50  gno/8] |= 1<<(pP
b6c40 67 2d 3e 70 67 6e 6f 26 37 29 3b 0a 20 20 20 20  g->pgno&7);.    
b6c50 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20    }.    }.  }.. 
b6c60 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 64   /* Update the d
b6c70 61 74 61 62 61 73 65 20 73 69 7a 65 20 61 6e 64  atabase size and
b6c80 20 72 65 74 75 72 6e 2e 0a 20 20 2a 2f 0a 20 20   return..  */.  
b6c90 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
b6ca0 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41  state>=PAGER_SHA
b6cb0 52 45 44 20 29 3b 0a 20 20 69 66 28 20 70 50 61  RED );.  if( pPa
b6cc0 67 65 72 2d 3e 64 62 53 69 7a 65 3c 28 69 6e 74  ger->dbSize<(int
b6cd0 29 70 50 67 2d 3e 70 67 6e 6f 20 29 7b 0a 20 20  )pPg->pgno ){.  
b6ce0 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65    pPager->dbSize
b6cf0 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20   = pPg->pgno;.  
b6d00 20 20 69 66 28 20 21 4d 45 4d 44 42 20 26 26 20    if( !MEMDB && 
b6d10 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d  pPager->dbSize==
b6d20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2f 70 50 61  PENDING_BYTE/pPa
b6d30 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 29 7b  ger->pageSize ){
b6d40 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64  .      pPager->d
b6d50 62 53 69 7a 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20  bSize++;.    }. 
b6d60 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
b6d70 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
b6d80 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74  nction is used t
b6d90 6f 20 6d 61 72 6b 20 61 20 64 61 74 61 2d 70 61  o mark a data-pa
b6da0 67 65 20 61 73 20 77 72 69 74 61 62 6c 65 2e 20  ge as writable. 
b6db0 49 74 20 75 73 65 73 20 0a 2a 2a 20 70 61 67 65  It uses .** page
b6dc0 72 5f 77 72 69 74 65 28 29 20 74 6f 20 6f 70 65  r_write() to ope
b6dd0 6e 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  n a journal file
b6de0 20 28 69 66 20 69 74 20 69 73 20 6e 6f 74 20 61   (if it is not a
b6df0 6c 72 65 61 64 79 20 6f 70 65 6e 29 0a 2a 2a 20  lready open).** 
b6e00 61 6e 64 20 77 72 69 74 65 20 74 68 65 20 70 61  and write the pa
b6e10 67 65 20 2a 70 44 61 74 61 20 74 6f 20 74 68 65  ge *pData to the
b6e20 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20   journal..**.** 
b6e30 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 62  The difference b
b6e40 65 74 77 65 65 6e 20 74 68 69 73 20 66 75 6e 63  etween this func
b6e50 74 69 6f 6e 20 61 6e 64 20 70 61 67 65 72 5f 77  tion and pager_w
b6e60 72 69 74 65 28 29 20 69 73 20 74 68 61 74 20 74  rite() is that t
b6e70 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  his.** function 
b6e80 61 6c 73 6f 20 64 65 61 6c 73 20 77 69 74 68 20  also deals with 
b6e90 74 68 65 20 73 70 65 63 69 61 6c 20 63 61 73 65  the special case
b6ea0 20 77 68 65 72 65 20 32 20 6f 72 20 6d 6f 72 65   where 2 or more
b6eb0 20 70 61 67 65 73 0a 2a 2a 20 66 69 74 20 6f 6e   pages.** fit on
b6ec0 20 61 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 73   a single disk s
b6ed0 65 63 74 6f 72 2e 20 49 6e 20 74 68 69 73 20 63  ector. In this c
b6ee0 61 73 65 20 61 6c 6c 20 63 6f 2d 72 65 73 69 64  ase all co-resid
b6ef0 65 6e 74 20 70 61 67 65 73 0a 2a 2a 20 6d 75 73  ent pages.** mus
b6f00 74 20 68 61 76 65 20 62 65 65 6e 20 77 72 69 74  t have been writ
b6f10 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e  ten to the journ
b6f20 61 6c 20 66 69 6c 65 20 62 65 66 6f 72 65 20 72  al file before r
b6f30 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c  eturning..*/.SQL
b6f40 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
b6f50 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
b6f60 65 28 44 62 50 61 67 65 20 2a 70 44 62 50 61 67  e(DbPage *pDbPag
b6f70 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  e){.  int rc = S
b6f80 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 50 67 48  QLITE_OK;..  PgH
b6f90 64 72 20 2a 70 50 67 20 3d 20 70 44 62 50 61 67  dr *pPg = pDbPag
b6fa0 65 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  e;.  Pager *pPag
b6fb0 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72  er = pPg->pPager
b6fc0 3b 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65 50 65  ;.  Pgno nPagePe
b6fd0 72 53 65 63 74 6f 72 20 3d 20 28 70 50 61 67 65  rSector = (pPage
b6fe0 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 2f 70 50  r->sectorSize/pP
b6ff0 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b  ager->pageSize);
b7000 0a 0a 20 20 69 66 28 20 21 4d 45 4d 44 42 20 26  ..  if( !MEMDB &
b7010 26 20 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72  & nPagePerSector
b7020 3e 31 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6e  >1 ){.    Pgno n
b7030 50 61 67 65 43 6f 75 6e 74 3b 20 20 20 20 20 20  PageCount;      
b7040 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d      /* Total num
b7050 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
b7060 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f  database file */
b7070 0a 20 20 20 20 50 67 6e 6f 20 70 67 31 3b 20 20  .    Pgno pg1;  
b7080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
b7090 2a 20 46 69 72 73 74 20 70 61 67 65 20 6f 66 20  * First page of 
b70a0 74 68 65 20 73 65 63 74 6f 72 20 70 50 67 20 69  the sector pPg i
b70b0 73 20 6c 6f 63 61 74 65 64 20 6f 6e 2e 20 2a 2f  s located on. */
b70c0 0a 20 20 20 20 69 6e 74 20 6e 50 61 67 65 3b 20  .    int nPage; 
b70d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
b70e0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  * Number of page
b70f0 73 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 67  s starting at pg
b7100 31 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a  1 to journal */.
b7110 20 20 20 20 69 6e 74 20 69 69 3b 0a 0a 20 20 20      int ii;..   
b7120 20 2f 2a 20 53 65 74 20 74 68 65 20 64 6f 4e 6f   /* Set the doNo
b7130 74 53 79 6e 63 20 66 6c 61 67 20 74 6f 20 31 2e  tSync flag to 1.
b7140 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73 65   This is because
b7150 20 77 65 20 63 61 6e 6e 6f 74 20 61 6c 6c 6f 77   we cannot allow
b7160 20 61 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a   a journal.    *
b7170 2a 20 68 65 61 64 65 72 20 74 6f 20 62 65 20 77  * header to be w
b7180 72 69 74 74 65 6e 20 62 65 74 77 65 65 6e 20 74  ritten between t
b7190 68 65 20 70 61 67 65 73 20 6a 6f 75 72 6e 61 6c  he pages journal
b71a0 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74  ed by this funct
b71b0 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ion..    */.    
b71c0 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
b71d0 64 6f 4e 6f 74 53 79 6e 63 3d 3d 30 20 29 3b 0a  doNotSync==0 );.
b71e0 20 20 20 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f      pPager->doNo
b71f0 74 53 79 6e 63 20 3d 20 31 3b 0a 0a 20 20 20 20  tSync = 1;..    
b7200 2f 2a 20 54 68 69 73 20 74 72 69 63 6b 20 61 73  /* This trick as
b7210 73 75 6d 65 73 20 74 68 61 74 20 62 6f 74 68 20  sumes that both 
b7220 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 61 6e  the page-size an
b7230 64 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 61 72  d sector-size ar
b7240 65 0a 20 20 20 20 2a 2a 20 61 6e 20 69 6e 74 65  e.    ** an inte
b7250 67 65 72 20 70 6f 77 65 72 20 6f 66 20 32 2e 20  ger power of 2. 
b7260 49 74 20 73 65 74 73 20 76 61 72 69 61 62 6c 65  It sets variable
b7270 20 70 67 31 20 74 6f 20 74 68 65 20 69 64 65 6e   pg1 to the iden
b7280 74 69 66 69 65 72 0a 20 20 20 20 2a 2a 20 6f 66  tifier.    ** of
b7290 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20   the first page 
b72a0 6f 66 20 74 68 65 20 73 65 63 74 6f 72 20 70 50  of the sector pP
b72b0 67 20 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e 2e  g is located on.
b72c0 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 67 31 20  .    */.    pg1 
b72d0 3d 20 28 28 70 50 67 2d 3e 70 67 6e 6f 2d 31 29  = ((pPg->pgno-1)
b72e0 20 26 20 7e 28 6e 50 61 67 65 50 65 72 53 65 63   & ~(nPagePerSec
b72f0 74 6f 72 2d 31 29 29 20 2b 20 31 3b 0a 0a 20 20  tor-1)) + 1;..  
b7300 20 20 6e 50 61 67 65 43 6f 75 6e 74 20 3d 20 73    nPageCount = s
b7310 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63  qlite3PagerPagec
b7320 6f 75 6e 74 28 70 50 61 67 65 72 29 3b 0a 20 20  ount(pPager);.  
b7330 20 20 69 66 28 20 70 50 67 2d 3e 70 67 6e 6f 3e    if( pPg->pgno>
b7340 6e 50 61 67 65 43 6f 75 6e 74 20 29 7b 0a 20 20  nPageCount ){.  
b7350 20 20 20 20 6e 50 61 67 65 20 3d 20 28 70 50 67      nPage = (pPg
b7360 2d 3e 70 67 6e 6f 20 2d 20 70 67 31 29 2b 31 3b  ->pgno - pg1)+1;
b7370 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28  .    }else if( (
b7380 70 67 31 2b 6e 50 61 67 65 50 65 72 53 65 63 74  pg1+nPagePerSect
b7390 6f 72 2d 31 29 3e 6e 50 61 67 65 43 6f 75 6e 74  or-1)>nPageCount
b73a0 20 29 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 20   ){.      nPage 
b73b0 3d 20 6e 50 61 67 65 43 6f 75 6e 74 2b 31 2d 70  = nPageCount+1-p
b73c0 67 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  g1;.    }else{. 
b73d0 20 20 20 20 20 6e 50 61 67 65 20 3d 20 6e 50 61       nPage = nPa
b73e0 67 65 50 65 72 53 65 63 74 6f 72 3b 0a 20 20 20  gePerSector;.   
b73f0 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 6e 50   }.    assert(nP
b7400 61 67 65 3e 30 29 3b 0a 20 20 20 20 61 73 73 65  age>0);.    asse
b7410 72 74 28 70 67 31 3c 3d 70 50 67 2d 3e 70 67 6e  rt(pg1<=pPg->pgn
b7420 6f 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 28  o);.    assert((
b7430 70 67 31 2b 6e 50 61 67 65 29 3e 70 50 67 2d 3e  pg1+nPage)>pPg->
b7440 70 67 6e 6f 29 3b 0a 0a 20 20 20 20 66 6f 72 28  pgno);..    for(
b7450 69 69 3d 30 3b 20 69 69 3c 6e 50 61 67 65 20 26  ii=0; ii<nPage &
b7460 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b  & rc==SQLITE_OK;
b7470 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 50 67   ii++){.      Pg
b7480 6e 6f 20 70 67 20 3d 20 70 67 31 2b 69 69 3b 0a  no pg = pg1+ii;.
b7490 20 20 20 20 20 20 69 66 28 20 21 70 50 61 67 65        if( !pPage
b74a0 72 2d 3e 61 49 6e 4a 6f 75 72 6e 61 6c 20 7c 7c  r->aInJournal ||
b74b0 20 70 67 3d 3d 70 50 67 2d 3e 70 67 6e 6f 20 7c   pg==pPg->pgno |
b74c0 7c 20 0a 20 20 20 20 20 20 20 20 20 20 70 67 3e  | .          pg>
b74d0 70 50 61 67 65 72 2d 3e 6f 72 69 67 44 62 53 69  pPager->origDbSi
b74e0 7a 65 20 7c 7c 20 21 28 70 50 61 67 65 72 2d 3e  ze || !(pPager->
b74f0 61 49 6e 4a 6f 75 72 6e 61 6c 5b 70 67 2f 38 5d  aInJournal[pg/8]
b7500 26 28 31 3c 3c 28 70 67 26 37 29 29 29 0a 20 20  &(1<<(pg&7))).  
b7510 20 20 20 20 29 20 7b 0a 20 20 20 20 20 20 20 20      ) {.        
b7520 69 66 28 20 70 67 21 3d 50 41 47 45 52 5f 4d 4a  if( pg!=PAGER_MJ
b7530 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 29 7b  _PGNO(pPager) ){
b7540 0a 20 20 20 20 20 20 20 20 20 20 50 67 48 64 72  .          PgHdr
b7550 20 2a 70 50 61 67 65 3b 0a 20 20 20 20 20 20 20   *pPage;.       
b7560 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
b7570 61 67 65 72 47 65 74 28 70 50 61 67 65 72 2c 20  agerGet(pPager, 
b7580 70 67 2c 20 26 70 50 61 67 65 29 3b 0a 20 20 20  pg, &pPage);.   
b7590 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
b75a0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
b75b0 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 67          rc = pag
b75c0 65 72 5f 77 72 69 74 65 28 70 50 61 67 65 29 3b  er_write(pPage);
b75d0 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
b75e0 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
b75f0 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20  Page);.         
b7600 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
b7610 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
b7620 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
b7630 64 6f 4e 6f 74 53 79 6e 63 3d 3d 31 20 29 3b 0a  doNotSync==1 );.
b7640 20 20 20 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f      pPager->doNo
b7650 74 53 79 6e 63 20 3d 20 30 3b 0a 20 20 7d 65 6c  tSync = 0;.  }el
b7660 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67  se{.    rc = pag
b7670 65 72 5f 77 72 69 74 65 28 70 44 62 50 61 67 65  er_write(pDbPage
b7680 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
b7690 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  rc;.}../*.** Ret
b76a0 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20  urn TRUE if the 
b76b0 70 61 67 65 20 67 69 76 65 6e 20 69 6e 20 74 68  page given in th
b76c0 65 20 61 72 67 75 6d 65 6e 74 20 77 61 73 20 70  e argument was p
b76d0 72 65 76 69 6f 75 73 6c 79 20 70 61 73 73 65 64  reviously passed
b76e0 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 50 61  .** to sqlite3Pa
b76f0 67 65 72 57 72 69 74 65 28 29 2e 20 20 49 6e 20  gerWrite().  In 
b7700 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 72 65 74  other words, ret
b7710 75 72 6e 20 54 52 55 45 20 69 66 20 69 74 20 69  urn TRUE if it i
b7720 73 20 6f 6b 0a 2a 2a 20 74 6f 20 63 68 61 6e 67  s ok.** to chang
b7730 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  e the content of
b7740 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 69   the page..*/.#i
b7750 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 53 51 4c  fndef NDEBUG.SQL
b7760 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
b7770 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72  sqlite3PagerIswr
b7780 69 74 65 61 62 6c 65 28 44 62 50 61 67 65 20 2a  iteable(DbPage *
b7790 70 50 67 29 7b 0a 20 20 72 65 74 75 72 6e 20 70  pPg){.  return p
b77a0 50 67 2d 3e 64 69 72 74 79 3b 0a 7d 0a 23 65 6e  Pg->dirty;.}.#en
b77b0 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
b77c0 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 0a  ITE_OMIT_VACUUM.
b77d0 2f 2a 0a 2a 2a 20 52 65 70 6c 61 63 65 20 74 68  /*.** Replace th
b77e0 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 73  e content of a s
b77f0 69 6e 67 6c 65 20 70 61 67 65 20 77 69 74 68 20  ingle page with 
b7800 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  the information 
b7810 69 6e 20 74 68 65 20 74 68 69 72 64 0a 2a 2a 20  in the third.** 
b7820 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c  argument..*/.SQL
b7830 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
b7840 73 71 6c 69 74 65 33 50 61 67 65 72 4f 76 65 72  sqlite3PagerOver
b7850 77 72 69 74 65 28 50 61 67 65 72 20 2a 70 50 61  write(Pager *pPa
b7860 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20  ger, Pgno pgno, 
b7870 76 6f 69 64 20 2a 70 44 61 74 61 29 7b 0a 20 20  void *pData){.  
b7880 50 67 48 64 72 20 2a 70 50 67 3b 0a 20 20 69 6e  PgHdr *pPg;.  in
b7890 74 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71  t rc;..  rc = sq
b78a0 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 50  lite3PagerGet(pP
b78b0 61 67 65 72 2c 20 70 67 6e 6f 2c 20 26 70 50 67  ager, pgno, &pPg
b78c0 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
b78d0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63  ITE_OK ){.    rc
b78e0 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57   = sqlite3PagerW
b78f0 72 69 74 65 28 70 50 67 29 3b 0a 20 20 20 20 69  rite(pPg);.    i
b7900 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
b7910 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79   ){.      memcpy
b7920 28 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74  (sqlite3PagerGet
b7930 44 61 74 61 28 70 50 67 29 2c 20 70 44 61 74 61  Data(pPg), pData
b7940 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  , pPager->pageSi
b7950 7a 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  ze);.    }.    s
b7960 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
b7970 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20 72 65 74  (pPg);.  }.  ret
b7980 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66  urn rc;.}.#endif
b7990 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74  ../*.** A call t
b79a0 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74  o this routine t
b79b0 65 6c 6c 73 20 74 68 65 20 70 61 67 65 72 20 74  ells the pager t
b79c0 68 61 74 20 69 74 20 69 73 20 6e 6f 74 20 6e 65  hat it is not ne
b79d0 63 65 73 73 61 72 79 20 74 6f 0a 2a 2a 20 77 72  cessary to.** wr
b79e0 69 74 65 20 74 68 65 20 69 6e 66 6f 72 6d 61 74  ite the informat
b79f0 69 6f 6e 20 6f 6e 20 70 61 67 65 20 70 50 67 20  ion on page pPg 
b7a00 62 61 63 6b 20 74 6f 20 74 68 65 20 64 69 73 6b  back to the disk
b7a10 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 0a 2a 2a  , even though.**
b7a20 20 74 68 61 74 20 70 61 67 65 20 6d 69 67 68 74   that page might
b7a30 20 62 65 20 6d 61 72 6b 65 64 20 61 73 20 64 69   be marked as di
b7a40 72 74 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f  rty..**.** The o
b7a50 76 65 72 6c 79 69 6e 67 20 73 6f 66 74 77 61 72  verlying softwar
b7a60 65 20 6c 61 79 65 72 20 63 61 6c 6c 73 20 74 68  e layer calls th
b7a70 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20  is routine when 
b7a80 61 6c 6c 20 6f 66 20 74 68 65 20 64 61 74 61 0a  all of the data.
b7a90 2a 2a 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20  ** on the given 
b7aa0 70 61 67 65 20 69 73 20 75 6e 75 73 65 64 2e 20  page is unused. 
b7ab0 20 54 68 65 20 70 61 67 65 72 20 6d 61 72 6b 73   The pager marks
b7ac0 20 74 68 65 20 70 61 67 65 20 61 73 20 63 6c 65   the page as cle
b7ad0 61 6e 20 73 6f 0a 2a 2a 20 74 68 61 74 20 69 74  an so.** that it
b7ae0 20 64 6f 65 73 20 6e 6f 74 20 67 65 74 20 77 72   does not get wr
b7af0 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 2e 0a 2a  itten to disk..*
b7b00 2a 0a 2a 2a 20 54 65 73 74 73 20 73 68 6f 77 20  *.** Tests show 
b7b10 74 68 61 74 20 74 68 69 73 20 6f 70 74 69 6d 69  that this optimi
b7b20 7a 61 74 69 6f 6e 2c 20 74 6f 67 65 74 68 65 72  zation, together
b7b30 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 73 71 6c   with the.** sql
b7b40 69 74 65 33 50 61 67 65 72 44 6f 6e 74 52 6f 6c  ite3PagerDontRol
b7b50 6c 62 61 63 6b 28 29 20 62 65 6c 6f 77 2c 20 6d  lback() below, m
b7b60 6f 72 65 20 74 68 61 6e 20 64 6f 75 62 6c 65 20  ore than double 
b7b70 74 68 65 20 73 70 65 65 64 0a 2a 2a 20 6f 66 20  the speed.** of 
b7b80 6c 61 72 67 65 20 49 4e 53 45 52 54 20 6f 70 65  large INSERT ope
b7b90 72 61 74 69 6f 6e 73 20 61 6e 64 20 71 75 61 64  rations and quad
b7ba0 72 75 70 6c 65 20 74 68 65 20 73 70 65 65 64 20  ruple the speed 
b7bb0 6f 66 20 6c 61 72 67 65 20 44 45 4c 45 54 45 73  of large DELETEs
b7bc0 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69  ..**.** When thi
b7bd0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
b7be0 6c 65 64 2c 20 73 65 74 20 74 68 65 20 61 6c 77  led, set the alw
b7bf0 61 79 73 52 6f 6c 6c 62 61 63 6b 20 66 6c 61 67  aysRollback flag
b7c00 20 74 6f 20 74 72 75 65 2e 0a 2a 2a 20 53 75 62   to true..** Sub
b7c10 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f  sequent calls to
b7c20 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e   sqlite3PagerDon
b7c30 74 52 6f 6c 6c 62 61 63 6b 28 29 20 66 6f 72 20  tRollback() for 
b7c40 74 68 65 20 73 61 6d 65 20 70 61 67 65 0a 2a 2a  the same page.**
b7c50 20 77 69 6c 6c 20 74 68 65 72 65 61 66 74 65 72   will thereafter
b7c60 20 62 65 20 69 67 6e 6f 72 65 64 2e 20 20 54 68   be ignored.  Th
b7c70 69 73 20 69 73 20 6e 65 63 65 73 73 61 72 79 20  is is necessary 
b7c80 74 6f 20 61 76 6f 69 64 20 61 20 70 72 6f 62 6c  to avoid a probl
b7c90 65 6d 0a 2a 2a 20 77 68 65 72 65 20 61 20 70 61  em.** where a pa
b7ca0 67 65 20 77 69 74 68 20 64 61 74 61 20 69 73 20  ge with data is 
b7cb0 61 64 64 65 64 20 74 6f 20 74 68 65 20 66 72 65  added to the fre
b7cc0 65 6c 69 73 74 20 64 75 72 69 6e 67 20 6f 6e 65  elist during one
b7cd0 20 70 61 72 74 20 6f 66 0a 2a 2a 20 61 20 74 72   part of.** a tr
b7ce0 61 6e 73 61 63 74 69 6f 6e 20 74 68 65 6e 20 72  ansaction then r
b7cf0 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20  emoved from the 
b7d00 66 72 65 65 6c 69 73 74 20 64 75 72 69 6e 67 20  freelist during 
b7d10 61 20 6c 61 74 65 72 20 70 61 72 74 0a 2a 2a 20  a later part.** 
b7d20 6f 66 20 74 68 65 20 73 61 6d 65 20 74 72 61 6e  of the same tran
b7d30 73 61 63 74 69 6f 6e 20 61 6e 64 20 72 65 75 73  saction and reus
b7d40 65 64 20 66 6f 72 20 73 6f 6d 65 20 6f 74 68 65  ed for some othe
b7d50 72 20 70 75 72 70 6f 73 65 2e 20 20 57 68 65 6e  r purpose.  When
b7d60 20 69 74 0a 2a 2a 20 69 73 20 66 69 72 73 74 20   it.** is first 
b7d70 61 64 64 65 64 20 74 6f 20 74 68 65 20 66 72 65  added to the fre
b7d80 65 6c 69 73 74 2c 20 74 68 69 73 20 72 6f 75 74  elist, this rout
b7d90 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 20  ine is called.  
b7da0 57 68 65 6e 20 72 65 75 73 65 64 2c 0a 2a 2a 20  When reused,.** 
b7db0 74 68 65 20 73 71 6c 69 74 65 33 50 61 67 65 72  the sqlite3Pager
b7dc0 44 6f 6e 74 52 6f 6c 6c 62 61 63 6b 28 29 20 72  DontRollback() r
b7dd0 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
b7de0 2e 20 20 42 75 74 20 62 65 63 61 75 73 65 20 74  .  But because t
b7df0 68 65 0a 2a 2a 20 70 61 67 65 20 63 6f 6e 74 61  he.** page conta
b7e00 69 6e 73 20 63 72 69 74 69 63 61 6c 20 64 61 74  ins critical dat
b7e10 61 2c 20 77 65 20 73 74 69 6c 6c 20 6e 65 65 64  a, we still need
b7e20 20 74 6f 20 62 65 20 73 75 72 65 20 69 74 20 67   to be sure it g
b7e30 65 74 73 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 61  ets.** rolled ba
b7e40 63 6b 20 69 6e 20 73 70 69 74 65 20 6f 66 20 74  ck in spite of t
b7e50 68 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 44  he sqlite3PagerD
b7e60 6f 6e 74 52 6f 6c 6c 62 61 63 6b 28 29 20 63 61  ontRollback() ca
b7e70 6c 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ll..*/.SQLITE_PR
b7e80 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
b7e90 65 33 50 61 67 65 72 44 6f 6e 74 57 72 69 74 65  e3PagerDontWrite
b7ea0 28 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65  (DbPage *pDbPage
b7eb0 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 67 20  ){.  PgHdr *pPg 
b7ec0 3d 20 70 44 62 50 61 67 65 3b 0a 20 20 50 61 67  = pDbPage;.  Pag
b7ed0 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67  er *pPager = pPg
b7ee0 2d 3e 70 50 61 67 65 72 3b 0a 0a 20 20 69 66 28  ->pPager;..  if(
b7ef0 20 4d 45 4d 44 42 20 29 20 72 65 74 75 72 6e 3b   MEMDB ) return;
b7f00 0a 20 20 70 50 67 2d 3e 61 6c 77 61 79 73 52 6f  .  pPg->alwaysRo
b7f10 6c 6c 62 61 63 6b 20 3d 20 31 3b 0a 20 20 69 66  llback = 1;.  if
b7f20 28 20 70 50 67 2d 3e 64 69 72 74 79 20 26 26 20  ( pPg->dirty && 
b7f30 21 70 50 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55  !pPager->stmtInU
b7f40 73 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  se ){.    assert
b7f50 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e  ( pPager->state>
b7f60 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b  =PAGER_SHARED );
b7f70 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d  .    if( pPager-
b7f80 3e 64 62 53 69 7a 65 3d 3d 28 69 6e 74 29 70 50  >dbSize==(int)pP
b7f90 67 2d 3e 70 67 6e 6f 20 26 26 20 70 50 61 67 65  g->pgno && pPage
b7fa0 72 2d 3e 6f 72 69 67 44 62 53 69 7a 65 3c 70 50  r->origDbSize<pP
b7fb0 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 29 7b 0a  ager->dbSize ){.
b7fc0 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73        /* If this
b7fd0 20 70 61 67 65 73 20 69 73 20 74 68 65 20 6c 61   pages is the la
b7fe0 73 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 66  st page in the f
b7ff0 69 6c 65 20 61 6e 64 20 74 68 65 20 66 69 6c 65  ile and the file
b8000 20 68 61 73 20 67 72 6f 77 6e 0a 20 20 20 20 20   has grown.     
b8010 20 2a 2a 20 64 75 72 69 6e 67 20 74 68 65 20 63   ** during the c
b8020 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69  urrent transacti
b8030 6f 6e 2c 20 74 68 65 6e 20 64 6f 20 4e 4f 54 20  on, then do NOT 
b8040 6d 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 73  mark the page as
b8050 20 63 6c 65 61 6e 2e 0a 20 20 20 20 20 20 2a 2a   clean..      **
b8060 20 57 68 65 6e 20 74 68 65 20 64 61 74 61 62 61   When the databa
b8070 73 65 20 66 69 6c 65 20 67 72 6f 77 73 2c 20 77  se file grows, w
b8080 65 20 6d 75 73 74 20 6d 61 6b 65 20 73 75 72 65  e must make sure
b8090 20 74 68 61 74 20 74 68 65 20 6c 61 73 74 20 70   that the last p
b80a0 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 67 65 74  age.      ** get
b80b0 73 20 77 72 69 74 74 65 6e 20 61 74 20 6c 65 61  s written at lea
b80c0 73 74 20 6f 6e 63 65 20 73 6f 20 74 68 61 74 20  st once so that 
b80d0 74 68 65 20 64 69 73 6b 20 66 69 6c 65 20 77 69  the disk file wi
b80e0 6c 6c 20 62 65 20 74 68 65 20 63 6f 72 72 65 63  ll be the correc
b80f0 74 0a 20 20 20 20 20 20 2a 2a 20 73 69 7a 65 2e  t.      ** size.
b8100 20 49 66 20 79 6f 75 20 64 6f 20 6e 6f 74 20 77   If you do not w
b8110 72 69 74 65 20 74 68 69 73 20 70 61 67 65 20 61  rite this page a
b8120 6e 64 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  nd the size of t
b8130 68 65 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a  he file.      **
b8140 20 6f 6e 20 74 68 65 20 64 69 73 6b 20 65 6e 64   on the disk end
b8150 73 20 75 70 20 62 65 69 6e 67 20 74 6f 6f 20 73  s up being too s
b8160 6d 61 6c 6c 2c 20 74 68 61 74 20 63 61 6e 20 6c  mall, that can l
b8170 65 61 64 20 74 6f 20 64 61 74 61 62 61 73 65 0a  ead to database.
b8180 20 20 20 20 20 20 2a 2a 20 63 6f 72 72 75 70 74        ** corrupt
b8190 69 6f 6e 20 64 75 72 69 6e 67 20 74 68 65 20 6e  ion during the n
b81a0 65 78 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  ext transaction.
b81b0 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 7d 65  .      */.    }e
b81c0 6c 73 65 7b 0a 20 20 20 20 20 20 50 41 47 45 52  lse{.      PAGER
b81d0 54 52 41 43 45 33 28 22 44 4f 4e 54 5f 57 52 49  TRACE3("DONT_WRI
b81e0 54 45 20 70 61 67 65 20 25 64 20 6f 66 20 25 64  TE page %d of %d
b81f0 5c 6e 22 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20  \n", pPg->pgno, 
b8200 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 29  PAGERID(pPager))
b8210 3b 0a 20 20 20 20 20 20 49 4f 54 52 41 43 45 28  ;.      IOTRACE(
b8220 28 22 43 4c 45 41 4e 20 25 70 20 25 64 5c 6e 22  ("CLEAN %p %d\n"
b8230 2c 20 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70  , pPager, pPg->p
b8240 67 6e 6f 29 29 0a 20 20 20 20 20 20 6d 61 6b 65  gno)).      make
b8250 43 6c 65 61 6e 28 70 50 67 29 3b 0a 23 69 66 64  Clean(pPg);.#ifd
b8260 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f  ef SQLITE_CHECK_
b8270 50 41 47 45 53 0a 20 20 20 20 20 20 70 50 67 2d  PAGES.      pPg-
b8280 3e 70 61 67 65 48 61 73 68 20 3d 20 70 61 67 65  >pageHash = page
b8290 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 29 3b  r_pagehash(pPg);
b82a0 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20  .#endif.    }.  
b82b0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 61 6c  }.}../*.** A cal
b82c0 6c 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e  l to this routin
b82d0 65 20 74 65 6c 6c 73 20 74 68 65 20 70 61 67 65  e tells the page
b82e0 72 20 74 68 61 74 20 69 66 20 61 20 72 6f 6c 6c  r that if a roll
b82f0 62 61 63 6b 20 6f 63 63 75 72 73 2c 0a 2a 2a 20  back occurs,.** 
b8300 69 74 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73  it is not necess
b8310 61 72 79 20 74 6f 20 72 65 73 74 6f 72 65 20 74  ary to restore t
b8320 68 65 20 64 61 74 61 20 6f 6e 20 74 68 65 20 67  he data on the g
b8330 69 76 65 6e 20 70 61 67 65 2e 20 20 54 68 69 73  iven page.  This
b8340 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74 20 74  .** means that t
b8350 68 65 20 70 61 67 65 72 20 64 6f 65 73 20 6e 6f  he pager does no
b8360 74 20 68 61 76 65 20 74 6f 20 72 65 63 6f 72 64  t have to record
b8370 20 74 68 65 20 67 69 76 65 6e 20 70 61 67 65 20   the given page 
b8380 69 6e 20 74 68 65 0a 2a 2a 20 72 6f 6c 6c 62 61  in the.** rollba
b8390 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a  ck journal..**.*
b83a0 2a 20 49 66 20 77 65 20 68 61 76 65 20 6e 6f 74  * If we have not
b83b0 20 79 65 74 20 61 63 74 75 61 6c 6c 79 20 72 65   yet actually re
b83c0 61 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  ad the content o
b83d0 66 20 74 68 69 73 20 70 61 67 65 20 28 69 66 0a  f this page (if.
b83e0 2a 2a 20 74 68 65 20 50 67 48 64 72 2e 6e 65 65  ** the PgHdr.nee
b83f0 64 52 65 61 64 20 66 6c 61 67 20 69 73 20 73 65  dRead flag is se
b8400 74 29 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75  t) then this rou
b8410 74 69 6e 65 20 61 63 74 73 20 61 73 20 61 20 70  tine acts as a p
b8420 72 6f 6d 69 73 65 0a 2a 2a 20 74 68 61 74 20 77  romise.** that w
b8430 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 6e 65 65  e will never nee
b8440 64 20 74 6f 20 72 65 61 64 20 74 68 65 20 70 61  d to read the pa
b8450 67 65 20 63 6f 6e 74 65 6e 74 20 69 6e 20 74 68  ge content in th
b8460 65 20 66 75 74 75 72 65 2e 0a 2a 2a 20 73 6f 20  e future..** so 
b8470 74 68 65 20 6e 65 65 64 52 65 61 64 20 66 6c 61  the needRead fla
b8480 67 20 63 61 6e 20 62 65 20 63 6c 65 61 72 65 64  g can be cleared
b8490 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 2e 0a   at this point..
b84a0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
b84b0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61  E void sqlite3Pa
b84c0 67 65 72 44 6f 6e 74 52 6f 6c 6c 62 61 63 6b 28  gerDontRollback(
b84d0 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20  DbPage *pPg){.  
b84e0 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20  Pager *pPager = 
b84f0 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 0a 20 20  pPg->pPager;..  
b8500 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
b8510 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53  state>=PAGER_RES
b8520 45 52 56 45 44 20 29 3b 0a 20 20 69 66 28 20 70  ERVED );.  if( p
b8530 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70  Pager->journalOp
b8540 65 6e 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  en==0 ) return;.
b8550 20 20 69 66 28 20 70 50 67 2d 3e 61 6c 77 61 79    if( pPg->alway
b8560 73 52 6f 6c 6c 62 61 63 6b 20 7c 7c 20 70 50 61  sRollback || pPa
b8570 67 65 72 2d 3e 61 6c 77 61 79 73 52 6f 6c 6c 62  ger->alwaysRollb
b8580 61 63 6b 20 7c 7c 20 4d 45 4d 44 42 20 29 20 72  ack || MEMDB ) r
b8590 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 21 70 50  eturn;.  if( !pP
b85a0 67 2d 3e 69 6e 4a 6f 75 72 6e 61 6c 20 26 26 20  g->inJournal && 
b85b0 28 69 6e 74 29 70 50 67 2d 3e 70 67 6e 6f 20 3c  (int)pPg->pgno <
b85c0 3d 20 70 50 61 67 65 72 2d 3e 6f 72 69 67 44 62  = pPager->origDb
b85d0 53 69 7a 65 20 29 7b 0a 20 20 20 20 61 73 73 65  Size ){.    asse
b85e0 72 74 28 20 70 50 61 67 65 72 2d 3e 61 49 6e 4a  rt( pPager->aInJ
b85f0 6f 75 72 6e 61 6c 21 3d 30 20 29 3b 0a 20 20 20  ournal!=0 );.   
b8600 20 70 50 61 67 65 72 2d 3e 61 49 6e 4a 6f 75 72   pPager->aInJour
b8610 6e 61 6c 5b 70 50 67 2d 3e 70 67 6e 6f 2f 38 5d  nal[pPg->pgno/8]
b8620 20 7c 3d 20 31 3c 3c 28 70 50 67 2d 3e 70 67 6e   |= 1<<(pPg->pgn
b8630 6f 26 37 29 3b 0a 20 20 20 20 70 50 67 2d 3e 69  o&7);.    pPg->i
b8640 6e 4a 6f 75 72 6e 61 6c 20 3d 20 31 3b 0a 20 20  nJournal = 1;.  
b8650 20 20 70 50 67 2d 3e 6e 65 65 64 52 65 61 64 20    pPg->needRead 
b8660 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 70 50 61  = 0;.    if( pPa
b8670 67 65 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20 29  ger->stmtInUse )
b8680 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e  {.      pPager->
b8690 61 49 6e 53 74 6d 74 5b 70 50 67 2d 3e 70 67 6e  aInStmt[pPg->pgn
b86a0 6f 2f 38 5d 20 7c 3d 20 31 3c 3c 28 70 50 67 2d  o/8] |= 1<<(pPg-
b86b0 3e 70 67 6e 6f 26 37 29 3b 0a 20 20 20 20 7d 0a  >pgno&7);.    }.
b86c0 20 20 20 20 50 41 47 45 52 54 52 41 43 45 33 28      PAGERTRACE3(
b86d0 22 44 4f 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 70  "DONT_ROLLBACK p
b86e0 61 67 65 20 25 64 20 6f 66 20 25 64 5c 6e 22 2c  age %d of %d\n",
b86f0 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 50 41 47 45   pPg->pgno, PAGE
b8700 52 49 44 28 70 50 61 67 65 72 29 29 3b 0a 20 20  RID(pPager));.  
b8710 20 20 49 4f 54 52 41 43 45 28 28 22 47 41 52 42    IOTRACE(("GARB
b8720 41 47 45 20 25 70 20 25 64 5c 6e 22 2c 20 70 50  AGE %p %d\n", pP
b8730 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 29  ager, pPg->pgno)
b8740 29 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 67  ).  }.  if( pPag
b8750 65 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20 0a 20  er->stmtInUse . 
b8760 20 20 26 26 20 21 70 61 67 65 49 6e 53 74 61 74    && !pageInStat
b8770 65 6d 65 6e 74 28 70 50 67 29 20 0a 20 20 20 26  ement(pPg) .   &
b8780 26 20 28 69 6e 74 29 70 50 67 2d 3e 70 67 6e 6f  & (int)pPg->pgno
b8790 3c 3d 70 50 61 67 65 72 2d 3e 73 74 6d 74 53 69  <=pPager->stmtSi
b87a0 7a 65 20 0a 20 20 29 7b 0a 20 20 20 20 61 73 73  ze .  ){.    ass
b87b0 65 72 74 28 20 70 50 67 2d 3e 69 6e 4a 6f 75 72  ert( pPg->inJour
b87c0 6e 61 6c 20 7c 7c 20 28 69 6e 74 29 70 50 67 2d  nal || (int)pPg-
b87d0 3e 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 6f 72  >pgno>pPager->or
b87e0 69 67 44 62 53 69 7a 65 20 29 3b 0a 20 20 20 20  igDbSize );.    
b87f0 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
b8800 61 49 6e 53 74 6d 74 21 3d 30 20 29 3b 0a 20 20  aInStmt!=0 );.  
b8810 20 20 70 50 61 67 65 72 2d 3e 61 49 6e 53 74 6d    pPager->aInStm
b8820 74 5b 70 50 67 2d 3e 70 67 6e 6f 2f 38 5d 20 7c  t[pPg->pgno/8] |
b8830 3d 20 31 3c 3c 28 70 50 67 2d 3e 70 67 6e 6f 26  = 1<<(pPg->pgno&
b8840 37 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a  7);.  }.}.../*.*
b8850 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
b8860 73 20 63 61 6c 6c 65 64 20 74 6f 20 69 6e 63 72  s called to incr
b8870 65 6d 65 6e 74 20 74 68 65 20 64 61 74 61 62 61  ement the databa
b8880 73 65 20 66 69 6c 65 20 63 68 61 6e 67 65 2d 63  se file change-c
b8890 6f 75 6e 74 65 72 2c 0a 2a 2a 20 73 74 6f 72 65  ounter,.** store
b88a0 64 20 61 74 20 62 79 74 65 20 32 34 20 6f 66 20  d at byte 24 of 
b88b0 74 68 65 20 70 61 67 65 72 20 66 69 6c 65 2e 0a  the pager file..
b88c0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61  */.static int pa
b88d0 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63  ger_incr_changec
b88e0 6f 75 6e 74 65 72 28 50 61 67 65 72 20 2a 70 50  ounter(Pager *pP
b88f0 61 67 65 72 29 7b 0a 20 20 50 67 48 64 72 20 2a  ager){.  PgHdr *
b8900 70 50 67 48 64 72 3b 0a 20 20 75 33 32 20 63 68  pPgHdr;.  u32 ch
b8910 61 6e 67 65 5f 63 6f 75 6e 74 65 72 3b 0a 20 20  ange_counter;.  
b8920 69 6e 74 20 72 63 3b 0a 0a 20 20 69 66 28 20 21  int rc;..  if( !
b8930 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f  pPager->changeCo
b8940 75 6e 74 44 6f 6e 65 20 29 7b 0a 20 20 20 20 2f  untDone ){.    /
b8950 2a 20 4f 70 65 6e 20 70 61 67 65 20 31 20 6f 66  * Open page 1 of
b8960 20 74 68 65 20 66 69 6c 65 20 66 6f 72 20 77 72   the file for wr
b8970 69 74 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 72 63  iting. */.    rc
b8980 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47   = sqlite3PagerG
b8990 65 74 28 70 50 61 67 65 72 2c 20 31 2c 20 26 70  et(pPager, 1, &p
b89a0 50 67 48 64 72 29 3b 0a 20 20 20 20 69 66 28 20  PgHdr);.    if( 
b89b0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
b89c0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 72  return rc;.    r
b89d0 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
b89e0 57 72 69 74 65 28 70 50 67 48 64 72 29 3b 0a 20  Write(pPgHdr);. 
b89f0 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
b8a00 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
b8a10 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 49 6e 63 72  ;.  .    /* Incr
b8a20 65 6d 65 6e 74 20 74 68 65 20 76 61 6c 75 65 20  ement the value 
b8a30 6a 75 73 74 20 72 65 61 64 20 61 6e 64 20 77 72  just read and wr
b8a40 69 74 65 20 69 74 20 62 61 63 6b 20 74 6f 20 62  ite it back to b
b8a50 79 74 65 20 32 34 2e 20 2a 2f 0a 20 20 20 20 63  yte 24. */.    c
b8a60 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 20 3d 20  hange_counter = 
b8a70 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28  sqlite3Get4byte(
b8a80 28 75 38 2a 29 70 50 61 67 65 72 2d 3e 64 62 46  (u8*)pPager->dbF
b8a90 69 6c 65 56 65 72 73 29 3b 0a 20 20 20 20 63 68  ileVers);.    ch
b8aa0 61 6e 67 65 5f 63 6f 75 6e 74 65 72 2b 2b 3b 0a  ange_counter++;.
b8ab0 20 20 20 20 70 75 74 33 32 62 69 74 73 28 28 28      put32bits(((
b8ac0 63 68 61 72 2a 29 50 47 48 44 52 5f 54 4f 5f 44  char*)PGHDR_TO_D
b8ad0 41 54 41 28 70 50 67 48 64 72 29 29 2b 32 34 2c  ATA(pPgHdr))+24,
b8ae0 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 29   change_counter)
b8af0 3b 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 65  ;.    /* Release
b8b00 20 74 68 65 20 70 61 67 65 20 72 65 66 65 72 65   the page refere
b8b10 6e 63 65 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69  nce. */.    sqli
b8b20 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50  te3PagerUnref(pP
b8b30 67 48 64 72 29 3b 0a 20 20 20 20 70 50 61 67 65  gHdr);.    pPage
b8b40 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f  r->changeCountDo
b8b50 6e 65 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65  ne = 1;.  }.  re
b8b60 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
b8b70 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68  }../*.** Sync th
b8b80 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
b8b90 66 6f 72 20 74 68 65 20 70 61 67 65 72 20 70 50  for the pager pP
b8ba0 61 67 65 72 2e 20 7a 4d 61 73 74 65 72 20 70 6f  ager. zMaster po
b8bb0 69 6e 74 73 20 74 6f 20 74 68 65 20 6e 61 6d 65  ints to the name
b8bc0 0a 2a 2a 20 6f 66 20 61 20 6d 61 73 74 65 72 20  .** of a master 
b8bd0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68 61  journal file tha
b8be0 74 20 73 68 6f 75 6c 64 20 62 65 20 77 72 69 74  t should be writ
b8bf0 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 69 6e 64  ten into the ind
b8c00 69 76 69 64 75 61 6c 0a 2a 2a 20 6a 6f 75 72 6e  ividual.** journ
b8c10 61 6c 20 66 69 6c 65 2e 20 7a 4d 61 73 74 65 72  al file. zMaster
b8c20 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2c 20 77 68   may be NULL, wh
b8c30 69 63 68 20 69 73 20 69 6e 74 65 72 70 72 65 74  ich is interpret
b8c40 65 64 20 61 73 20 6e 6f 20 6d 61 73 74 65 72 0a  ed as no master.
b8c50 2a 2a 20 6a 6f 75 72 6e 61 6c 20 28 61 20 73 69  ** journal (a si
b8c60 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 74 72  ngle database tr
b8c70 61 6e 73 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a 2a  ansaction)..**.*
b8c80 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65  * This routine e
b8c90 6e 73 75 72 65 73 20 74 68 61 74 20 74 68 65 20  nsures that the 
b8ca0 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 65  journal is synce
b8cb0 64 2c 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67  d, all dirty pag
b8cc0 65 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f  es written.** to
b8cd0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
b8ce0 6c 65 20 61 6e 64 20 74 68 65 20 64 61 74 61 62  le and the datab
b8cf0 61 73 65 20 66 69 6c 65 20 73 79 6e 63 65 64 2e  ase file synced.
b8d00 20 54 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20   The only thing 
b8d10 74 68 61 74 0a 2a 2a 20 72 65 6d 61 69 6e 73 20  that.** remains 
b8d20 74 6f 20 63 6f 6d 6d 69 74 20 74 68 65 20 74 72  to commit the tr
b8d30 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 74 6f 20  ansaction is to 
b8d40 64 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e  delete the journ
b8d50 61 6c 20 66 69 6c 65 20 28 6f 72 0a 2a 2a 20 6d  al file (or.** m
b8d60 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
b8d70 6c 65 20 69 66 20 73 70 65 63 69 66 69 65 64 29  le if specified)
b8d80 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ..**.** Note tha
b8d90 74 20 69 66 20 7a 4d 61 73 74 65 72 3d 3d 4e 55  t if zMaster==NU
b8da0 4c 4c 2c 20 74 68 69 73 20 64 6f 65 73 20 6e 6f  LL, this does no
b8db0 74 20 6f 76 65 72 77 72 69 74 65 20 61 20 70 72  t overwrite a pr
b8dc0 65 76 69 6f 75 73 20 76 61 6c 75 65 0a 2a 2a 20  evious value.** 
b8dd0 70 61 73 73 65 64 20 74 6f 20 61 6e 20 73 71 6c  passed to an sql
b8de0 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50  ite3PagerCommitP
b8df0 68 61 73 65 4f 6e 65 28 29 20 63 61 6c 6c 2e 0a  haseOne() call..
b8e00 2a 2a 0a 2a 2a 20 49 66 20 70 61 72 61 6d 65 74  **.** If paramet
b8e10 65 72 20 6e 54 72 75 6e 63 20 69 73 20 6e 6f 6e  er nTrunc is non
b8e20 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20  -zero, then the 
b8e30 70 61 67 65 72 20 66 69 6c 65 20 69 73 20 74 72  pager file is tr
b8e40 75 6e 63 61 74 65 64 20 74 6f 0a 2a 2a 20 6e 54  uncated to.** nT
b8e50 72 75 6e 63 20 70 61 67 65 73 20 28 74 68 69 73  runc pages (this
b8e60 20 69 73 20 75 73 65 64 20 62 79 20 61 75 74 6f   is used by auto
b8e70 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65  -vacuum database
b8e80 73 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  s)..*/.SQLITE_PR
b8e90 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
b8ea0 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73  3PagerCommitPhas
b8eb0 65 4f 6e 65 28 50 61 67 65 72 20 2a 70 50 61 67  eOne(Pager *pPag
b8ec0 65 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  er, const char *
b8ed0 7a 4d 61 73 74 65 72 2c 20 50 67 6e 6f 20 6e 54  zMaster, Pgno nT
b8ee0 72 75 6e 63 29 7b 0a 20 20 69 6e 74 20 72 63 20  runc){.  int rc 
b8ef0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20  = SQLITE_OK;..  
b8f00 50 41 47 45 52 54 52 41 43 45 34 28 22 44 41 54  PAGERTRACE4("DAT
b8f10 41 42 41 53 45 20 53 59 4e 43 3a 20 46 69 6c 65  ABASE SYNC: File
b8f20 3d 25 73 20 7a 4d 61 73 74 65 72 3d 25 73 20 6e  =%s zMaster=%s n
b8f30 54 72 75 6e 63 3d 25 64 5c 6e 22 2c 20 0a 20 20  Trunc=%d\n", .  
b8f40 20 20 20 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c      pPager->zFil
b8f50 65 6e 61 6d 65 2c 20 7a 4d 61 73 74 65 72 2c 20  ename, zMaster, 
b8f60 6e 54 72 75 6e 63 29 3b 0a 0a 20 20 2f 2a 20 49  nTrunc);..  /* I
b8f70 66 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e 2d  f this is an in-
b8f80 6d 65 6d 6f 72 79 20 64 62 2c 20 6f 72 20 6e 6f  memory db, or no
b8f90 20 70 61 67 65 73 20 68 61 76 65 20 62 65 65 6e   pages have been
b8fa0 20 77 72 69 74 74 65 6e 20 74 6f 2c 20 6f 72 20   written to, or 
b8fb0 74 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 69  this.  ** functi
b8fc0 6f 6e 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  on has already b
b8fd0 65 65 6e 20 63 61 6c 6c 65 64 2c 20 69 74 20 69  een called, it i
b8fe0 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2f 0a  s a no-op..  */.
b8ff0 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74    if( pPager->st
b9000 61 74 65 21 3d 50 41 47 45 52 5f 53 59 4e 43 45  ate!=PAGER_SYNCE
b9010 44 20 26 26 20 21 4d 45 4d 44 42 20 26 26 20 70  D && !MEMDB && p
b9020 50 61 67 65 72 2d 3e 64 69 72 74 79 43 61 63 68  Pager->dirtyCach
b9030 65 20 29 7b 0a 20 20 20 20 50 67 48 64 72 20 2a  e ){.    PgHdr *
b9040 70 50 67 3b 0a 20 20 20 20 61 73 73 65 72 74 28  pPg;.    assert(
b9050 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
b9060 4f 70 65 6e 20 29 3b 0a 0a 20 20 20 20 2f 2a 20  Open );..    /* 
b9070 49 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72  If a master jour
b9080 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 68 61  nal file name ha
b9090 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 77  s already been w
b90a0 72 69 74 74 65 6e 20 74 6f 20 74 68 65 0a 20 20  ritten to the.  
b90b0 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c    ** journal fil
b90c0 65 2c 20 74 68 65 6e 20 6e 6f 20 73 79 6e 63 20  e, then no sync 
b90d0 69 73 20 72 65 71 75 69 72 65 64 2e 20 54 68 69  is required. Thi
b90e0 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 69  s happens when i
b90f0 74 20 69 73 0a 20 20 20 20 2a 2a 20 77 72 69 74  t is.    ** writ
b9100 74 65 6e 2c 20 74 68 65 6e 20 74 68 65 20 70 72  ten, then the pr
b9110 6f 63 65 73 73 20 66 61 69 6c 73 20 74 6f 20 75  ocess fails to u
b9120 70 67 72 61 64 65 20 66 72 6f 6d 20 61 20 52 45  pgrade from a RE
b9130 53 45 52 56 45 44 20 74 6f 20 61 6e 0a 20 20 20  SERVED to an.   
b9140 20 2a 2a 20 45 58 43 4c 55 53 49 56 45 20 6c 6f   ** EXCLUSIVE lo
b9150 63 6b 2e 20 54 68 65 20 6e 65 78 74 20 74 69 6d  ck. The next tim
b9160 65 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 72  e the process tr
b9170 69 65 73 20 74 6f 20 63 6f 6d 6d 69 74 20 74 68  ies to commit th
b9180 65 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63  e.    ** transac
b9190 74 69 6f 6e 20 74 68 65 20 6d 2d 6a 20 6e 61 6d  tion the m-j nam
b91a0 65 20 77 69 6c 6c 20 68 61 76 65 20 61 6c 72 65  e will have alre
b91b0 61 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e  ady been written
b91c0 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
b91d0 20 21 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73   !pPager->setMas
b91e0 74 65 72 20 29 7b 0a 20 20 20 20 20 20 72 63 20  ter ){.      rc 
b91f0 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61  = pager_incr_cha
b9200 6e 67 65 63 6f 75 6e 74 65 72 28 70 50 61 67 65  ngecounter(pPage
b9210 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  r);.      if( rc
b9220 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
b9230 74 6f 20 73 79 6e 63 5f 65 78 69 74 3b 0a 23 69  to sync_exit;.#i
b9240 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
b9250 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20  T_AUTOVACUUM.   
b9260 20 20 20 69 66 28 20 6e 54 72 75 6e 63 21 3d 30     if( nTrunc!=0
b9270 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49   ){.        /* I
b9280 66 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69  f this transacti
b9290 6f 6e 20 68 61 73 20 6d 61 64 65 20 74 68 65 20  on has made the 
b92a0 64 61 74 61 62 61 73 65 20 73 6d 61 6c 6c 65 72  database smaller
b92b0 2c 20 74 68 65 6e 20 61 6c 6c 20 70 61 67 65 73  , then all pages
b92c0 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 65 69 6e  .        ** bein
b92d0 67 20 64 69 73 63 61 72 64 65 64 20 62 79 20 74  g discarded by t
b92e0 68 65 20 74 72 75 6e 63 61 74 69 6f 6e 20 6d 75  he truncation mu
b92f0 73 74 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f  st be written to
b9300 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20   the journal.   
b9310 20 20 20 20 20 2a 2a 20 66 69 6c 65 2e 0a 20 20       ** file..  
b9320 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
b9330 20 50 67 6e 6f 20 69 3b 0a 20 20 20 20 20 20 20   Pgno i;.       
b9340 20 69 6e 74 20 69 53 6b 69 70 20 3d 20 50 41 47   int iSkip = PAG
b9350 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65  ER_MJ_PGNO(pPage
b9360 72 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28  r);.        for(
b9370 20 69 3d 6e 54 72 75 6e 63 2b 31 3b 20 69 3c 3d   i=nTrunc+1; i<=
b9380 70 50 61 67 65 72 2d 3e 6f 72 69 67 44 62 53 69  pPager->origDbSi
b9390 7a 65 3b 20 69 2b 2b 20 29 7b 0a 20 20 20 20 20  ze; i++ ){.     
b93a0 20 20 20 20 20 69 66 28 20 21 28 70 50 61 67 65       if( !(pPage
b93b0 72 2d 3e 61 49 6e 4a 6f 75 72 6e 61 6c 5b 69 2f  r->aInJournal[i/
b93c0 38 5d 20 26 20 28 31 3c 3c 28 69 26 37 29 29 29  8] & (1<<(i&7)))
b93d0 20 26 26 20 69 21 3d 69 53 6b 69 70 20 29 7b 0a   && i!=iSkip ){.
b93e0 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d              rc =
b93f0 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
b9400 28 70 50 61 67 65 72 2c 20 69 2c 20 26 70 50 67  (pPager, i, &pPg
b9410 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  );.            i
b9420 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
b9430 20 29 20 67 6f 74 6f 20 73 79 6e 63 5f 65 78 69   ) goto sync_exi
b9440 74 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  t;.            r
b9450 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
b9460 57 72 69 74 65 28 70 50 67 29 3b 0a 20 20 20 20  Write(pPg);.    
b9470 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50          sqlite3P
b9480 61 67 65 72 55 6e 72 65 66 28 70 50 67 29 3b 0a  agerUnref(pPg);.
b9490 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
b94a0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
b94b0 67 6f 74 6f 20 73 79 6e 63 5f 65 78 69 74 3b 0a  goto sync_exit;.
b94c0 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
b94d0 20 20 20 20 7d 20 0a 20 20 20 20 20 20 7d 0a 23      } .      }.#
b94e0 65 6e 64 69 66 0a 20 20 20 20 20 20 72 63 20 3d  endif.      rc =
b94f0 20 77 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 72   writeMasterJour
b9500 6e 61 6c 28 70 50 61 67 65 72 2c 20 7a 4d 61 73  nal(pPager, zMas
b9510 74 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ter);.      if( 
b9520 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
b9530 67 6f 74 6f 20 73 79 6e 63 5f 65 78 69 74 3b 0a  goto sync_exit;.
b9540 20 20 20 20 20 20 72 63 20 3d 20 73 79 6e 63 4a        rc = syncJ
b9550 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a  ournal(pPager);.
b9560 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
b9570 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 73  LITE_OK ) goto s
b9580 79 6e 63 5f 65 78 69 74 3b 0a 20 20 20 20 7d 0a  ync_exit;.    }.
b9590 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
b95a0 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
b95b0 20 20 20 20 69 66 28 20 6e 54 72 75 6e 63 21 3d      if( nTrunc!=
b95c0 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  0 ){.      rc = 
b95d0 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e  sqlite3PagerTrun
b95e0 63 61 74 65 28 70 50 61 67 65 72 2c 20 6e 54 72  cate(pPager, nTr
b95f0 75 6e 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20  unc);.      if( 
b9600 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
b9610 67 6f 74 6f 20 73 79 6e 63 5f 65 78 69 74 3b 0a  goto sync_exit;.
b9620 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20      }.#endif..  
b9630 20 20 2f 2a 20 57 72 69 74 65 20 61 6c 6c 20 64    /* Write all d
b9640 69 72 74 79 20 70 61 67 65 73 20 74 6f 20 74 68  irty pages to th
b9650 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
b9660 2a 2f 0a 20 20 20 20 70 50 67 20 3d 20 70 61 67  */.    pPg = pag
b9670 65 72 5f 67 65 74 5f 61 6c 6c 5f 64 69 72 74 79  er_get_all_dirty
b9680 5f 70 61 67 65 73 28 70 50 61 67 65 72 29 3b 0a  _pages(pPager);.
b9690 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77      rc = pager_w
b96a0 72 69 74 65 5f 70 61 67 65 6c 69 73 74 28 70 50  rite_pagelist(pP
b96b0 67 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  g);.    if( rc!=
b96c0 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
b96d0 20 73 79 6e 63 5f 65 78 69 74 3b 0a 20 20 20 20   sync_exit;.    
b96e0 70 50 61 67 65 72 2d 3e 70 44 69 72 74 79 20 3d  pPager->pDirty =
b96f0 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 53 79 6e 63   0;..    /* Sync
b9700 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
b9710 6c 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 21  le. */.    if( !
b9720 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29  pPager->noSync )
b9730 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
b9740 69 74 65 33 4f 73 53 79 6e 63 28 70 50 61 67 65  ite3OsSync(pPage
b9750 72 2d 3e 66 64 2c 20 30 29 3b 0a 20 20 20 20 7d  r->fd, 0);.    }
b9760 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 44  .    IOTRACE(("D
b9770 42 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50 61  BSYNC %p\n", pPa
b9780 67 65 72 29 29 0a 0a 20 20 20 20 70 50 61 67 65  ger))..    pPage
b9790 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52  r->state = PAGER
b97a0 5f 53 59 4e 43 45 44 3b 0a 20 20 7d 65 6c 73 65  _SYNCED;.  }else
b97b0 20 69 66 28 20 4d 45 4d 44 42 20 26 26 20 6e 54   if( MEMDB && nT
b97c0 72 75 6e 63 21 3d 30 20 29 7b 0a 20 20 20 20 72  runc!=0 ){.    r
b97d0 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
b97e0 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2c  Truncate(pPager,
b97f0 20 6e 54 72 75 6e 63 29 3b 0a 20 20 7d 0a 0a 73   nTrunc);.  }..s
b9800 79 6e 63 5f 65 78 69 74 3a 0a 20 20 69 66 28 20  ync_exit:.  if( 
b9810 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52  rc==SQLITE_IOERR
b9820 5f 42 4c 4f 43 4b 45 44 20 29 7b 0a 20 20 20 20  _BLOCKED ){.    
b9830 2f 2a 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68  /* pager_incr_ch
b9840 61 6e 67 65 63 6f 75 6e 74 65 72 28 29 20 6d 61  angecounter() ma
b9850 79 20 61 74 74 65 6d 70 74 20 74 6f 20 6f 62 74  y attempt to obt
b9860 61 69 6e 20 61 6e 20 65 78 63 6c 75 73 69 76 65  ain an exclusive
b9870 0a 20 20 20 20 20 2a 20 6c 6f 63 6b 20 74 6f 20  .     * lock to 
b9880 73 70 69 6c 6c 20 74 68 65 20 63 61 63 68 65 20  spill the cache 
b9890 61 6e 64 20 72 65 74 75 72 6e 20 49 4f 45 52 52  and return IOERR
b98a0 5f 42 4c 4f 43 4b 45 44 2e 20 42 75 74 20 73 69  _BLOCKED. But si
b98b0 6e 63 65 20 0a 20 20 20 20 20 2a 20 74 68 65 72  nce .     * ther
b98c0 65 20 69 73 20 6e 6f 20 63 68 61 6e 63 65 20 74  e is no chance t
b98d0 68 65 20 63 61 63 68 65 20 69 73 20 69 6e 63 6f  he cache is inco
b98e0 6e 73 69 73 74 65 6e 74 2c 20 69 74 27 73 0a 20  nsistent, it's. 
b98f0 20 20 20 20 2a 20 62 65 74 74 65 72 20 74 6f 20      * better to 
b9900 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55  return SQLITE_BU
b9910 53 59 2e 0a 20 20 20 20 20 2a 2f 0a 20 20 20 20  SY..     */.    
b9920 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59  rc = SQLITE_BUSY
b9930 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
b9940 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d  c;.}.../*.** Com
b9950 6d 69 74 20 61 6c 6c 20 63 68 61 6e 67 65 73 20  mit all changes 
b9960 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
b9970 61 6e 64 20 72 65 6c 65 61 73 65 20 74 68 65 20  and release the 
b9980 77 72 69 74 65 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a  write lock..**.*
b9990 2a 20 49 66 20 74 68 65 20 63 6f 6d 6d 69 74 20  * If the commit 
b99a0 66 61 69 6c 73 20 66 6f 72 20 61 6e 79 20 72 65  fails for any re
b99b0 61 73 6f 6e 2c 20 61 20 72 6f 6c 6c 62 61 63 6b  ason, a rollback
b99c0 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65   attempt is made
b99d0 0a 2a 2a 20 61 6e 64 20 61 6e 20 65 72 72 6f 72  .** and an error
b99e0 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
b99f0 64 2e 20 20 49 66 20 74 68 65 20 63 6f 6d 6d 69  d.  If the commi
b9a00 74 20 77 6f 72 6b 65 64 2c 20 53 51 4c 49 54 45  t worked, SQLITE
b9a10 5f 4f 4b 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e  _OK.** is return
b9a20 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
b9a30 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
b9a40 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73  3PagerCommitPhas
b9a50 65 54 77 6f 28 50 61 67 65 72 20 2a 70 50 61 67  eTwo(Pager *pPag
b9a60 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  er){.  int rc;. 
b9a70 20 50 67 48 64 72 20 2a 70 50 67 3b 0a 0a 20 20   PgHdr *pPg;..  
b9a80 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43  if( pPager->errC
b9a90 6f 64 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ode ){.    retur
b9aa0 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  n pPager->errCod
b9ab0 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61  e;.  }.  if( pPa
b9ac0 67 65 72 2d 3e 73 74 61 74 65 3c 50 41 47 45 52  ger->state<PAGER
b9ad0 5f 52 45 53 45 52 56 45 44 20 29 7b 0a 20 20 20  _RESERVED ){.   
b9ae0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
b9af0 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 50 41 47 45  RROR;.  }.  PAGE
b9b00 52 54 52 41 43 45 32 28 22 43 4f 4d 4d 49 54 20  RTRACE2("COMMIT 
b9b10 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70  %d\n", PAGERID(p
b9b20 50 61 67 65 72 29 29 3b 0a 20 20 69 66 28 20 4d  Pager));.  if( M
b9b30 45 4d 44 42 20 29 7b 0a 20 20 20 20 70 50 67 20  EMDB ){.    pPg 
b9b40 3d 20 70 61 67 65 72 5f 67 65 74 5f 61 6c 6c 5f  = pager_get_all_
b9b50 64 69 72 74 79 5f 70 61 67 65 73 28 70 50 61 67  dirty_pages(pPag
b9b60 65 72 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  er);.    while( 
b9b70 70 50 67 20 29 7b 0a 20 20 20 20 20 20 50 67 48  pPg ){.      PgH
b9b80 69 73 74 6f 72 79 20 2a 70 48 69 73 74 20 3d 20  istory *pHist = 
b9b90 50 47 48 44 52 5f 54 4f 5f 48 49 53 54 28 70 50  PGHDR_TO_HIST(pP
b9ba0 67 2c 20 70 50 61 67 65 72 29 3b 0a 20 20 20 20  g, pPager);.    
b9bb0 20 20 63 6c 65 61 72 48 69 73 74 6f 72 79 28 70    clearHistory(p
b9bc0 48 69 73 74 29 3b 0a 20 20 20 20 20 20 70 50 67  Hist);.      pPg
b9bd0 2d 3e 64 69 72 74 79 20 3d 20 30 3b 0a 20 20 20  ->dirty = 0;.   
b9be0 20 20 20 70 50 67 2d 3e 69 6e 4a 6f 75 72 6e 61     pPg->inJourna
b9bf0 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 48 69  l = 0;.      pHi
b9c00 73 74 2d 3e 69 6e 53 74 6d 74 20 3d 20 30 3b 0a  st->inStmt = 0;.
b9c10 20 20 20 20 20 20 70 50 67 2d 3e 6e 65 65 64 53        pPg->needS
b9c20 79 6e 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  ync = 0;.      p
b9c30 48 69 73 74 2d 3e 70 50 72 65 76 53 74 6d 74 20  Hist->pPrevStmt 
b9c40 3d 20 70 48 69 73 74 2d 3e 70 4e 65 78 74 53 74  = pHist->pNextSt
b9c50 6d 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50  mt = 0;.      pP
b9c60 67 20 3d 20 70 50 67 2d 3e 70 44 69 72 74 79 3b  g = pPg->pDirty;
b9c70 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65  .    }.    pPage
b9c80 72 2d 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a 23  r->pDirty = 0;.#
b9c90 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20  ifndef NDEBUG.  
b9ca0 20 20 66 6f 72 28 70 50 67 3d 70 50 61 67 65 72    for(pPg=pPager
b9cb0 2d 3e 70 41 6c 6c 3b 20 70 50 67 3b 20 70 50 67  ->pAll; pPg; pPg
b9cc0 3d 70 50 67 2d 3e 70 4e 65 78 74 41 6c 6c 29 7b  =pPg->pNextAll){
b9cd0 0a 20 20 20 20 20 20 50 67 48 69 73 74 6f 72 79  .      PgHistory
b9ce0 20 2a 70 48 69 73 74 20 3d 20 50 47 48 44 52 5f   *pHist = PGHDR_
b9cf0 54 4f 5f 48 49 53 54 28 70 50 67 2c 20 70 50 61  TO_HIST(pPg, pPa
b9d00 67 65 72 29 3b 0a 20 20 20 20 20 20 61 73 73 65  ger);.      asse
b9d10 72 74 28 20 21 70 50 67 2d 3e 61 6c 77 61 79 73  rt( !pPg->always
b9d20 52 6f 6c 6c 62 61 63 6b 20 29 3b 0a 20 20 20 20  Rollback );.    
b9d30 20 20 61 73 73 65 72 74 28 20 21 70 48 69 73 74    assert( !pHist
b9d40 2d 3e 70 4f 72 69 67 20 29 3b 0a 20 20 20 20 20  ->pOrig );.     
b9d50 20 61 73 73 65 72 74 28 20 21 70 48 69 73 74 2d   assert( !pHist-
b9d60 3e 70 53 74 6d 74 20 29 3b 0a 20 20 20 20 7d 0a  >pStmt );.    }.
b9d70 23 65 6e 64 69 66 0a 20 20 20 20 70 50 61 67 65  #endif.    pPage
b9d80 72 2d 3e 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20  r->pStmt = 0;.  
b9d90 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20    pPager->state 
b9da0 3d 20 50 41 47 45 52 5f 53 48 41 52 45 44 3b 0a  = PAGER_SHARED;.
b9db0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
b9dc0 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 61 73 73 65  E_OK;.  }.  asse
b9dd0 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  rt( pPager->jour
b9de0 6e 61 6c 4f 70 65 6e 20 7c 7c 20 21 70 50 61 67  nalOpen || !pPag
b9df0 65 72 2d 3e 64 69 72 74 79 43 61 63 68 65 20 29  er->dirtyCache )
b9e00 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
b9e10 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52  er->state==PAGER
b9e20 5f 53 59 4e 43 45 44 20 7c 7c 20 21 70 50 61 67  _SYNCED || !pPag
b9e30 65 72 2d 3e 64 69 72 74 79 43 61 63 68 65 20 29  er->dirtyCache )
b9e40 3b 0a 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65  ;.  rc = pager_e
b9e50 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70  nd_transaction(p
b9e60 50 61 67 65 72 29 3b 0a 20 20 72 65 74 75 72 6e  Pager);.  return
b9e70 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61   pager_error(pPa
b9e80 67 65 72 2c 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a  ger, rc);.}../*.
b9e90 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 6c 6c 20  ** Rollback all 
b9ea0 63 68 61 6e 67 65 73 2e 20 20 54 68 65 20 64 61  changes.  The da
b9eb0 74 61 62 61 73 65 20 66 61 6c 6c 73 20 62 61 63  tabase falls bac
b9ec0 6b 20 74 6f 20 50 41 47 45 52 5f 53 48 41 52 45  k to PAGER_SHARE
b9ed0 44 20 6d 6f 64 65 2e 0a 2a 2a 20 41 6c 6c 20 69  D mode..** All i
b9ee0 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 20 70  n-memory cache p
b9ef0 61 67 65 73 20 72 65 76 65 72 74 20 74 6f 20 74  ages revert to t
b9f00 68 65 69 72 20 6f 72 69 67 69 6e 61 6c 20 64 61  heir original da
b9f10 74 61 20 63 6f 6e 74 65 6e 74 73 2e 0a 2a 2a 20  ta contents..** 
b9f20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 64  The journal is d
b9f30 65 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  eleted..**.** Th
b9f40 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 6e 6f  is routine canno
b9f50 74 20 66 61 69 6c 20 75 6e 6c 65 73 73 20 73 6f  t fail unless so
b9f60 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  me other process
b9f70 20 69 73 20 6e 6f 74 20 66 6f 6c 6c 6f 77 69 6e   is not followin
b9f80 67 0a 2a 2a 20 74 68 65 20 63 6f 72 72 65 63 74  g.** the correct
b9f90 20 6c 6f 63 6b 69 6e 67 20 70 72 6f 74 6f 63 6f   locking protoco
b9fa0 6c 20 6f 72 20 75 6e 6c 65 73 73 20 73 6f 6d 65  l or unless some
b9fb0 20 6f 74 68 65 72 0a 2a 2a 20 70 72 6f 63 65 73   other.** proces
b9fc0 73 20 69 73 20 77 72 69 74 69 6e 67 20 74 72 61  s is writing tra
b9fd0 73 68 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72  sh into the jour
b9fe0 6e 61 6c 20 66 69 6c 65 20 28 53 51 4c 49 54 45  nal file (SQLITE
b9ff0 5f 43 4f 52 52 55 50 54 29 20 6f 72 0a 2a 2a 20  _CORRUPT) or.** 
ba000 75 6e 6c 65 73 73 20 61 20 70 72 69 6f 72 20 6d  unless a prior m
ba010 61 6c 6c 6f 63 28 29 20 66 61 69 6c 65 64 20 28  alloc() failed (
ba020 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 2e 20 20  SQLITE_NOMEM).  
ba030 41 70 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f  Appropriate erro
ba040 72 0a 2a 2a 20 63 6f 64 65 73 20 61 72 65 20 72  r.** codes are r
ba050 65 74 75 72 6e 65 64 20 66 6f 72 20 61 6c 6c 20  eturned for all 
ba060 74 68 65 73 65 20 6f 63 63 61 73 69 6f 6e 73 2e  these occasions.
ba070 20 20 4f 74 68 65 72 77 69 73 65 2c 0a 2a 2a 20    Otherwise,.** 
ba080 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
ba090 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  urned..*/.SQLITE
ba0a0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
ba0b0 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63  ite3PagerRollbac
ba0c0 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  k(Pager *pPager)
ba0d0 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 50 41  {.  int rc;.  PA
ba0e0 47 45 52 54 52 41 43 45 32 28 22 52 4f 4c 4c 42  GERTRACE2("ROLLB
ba0f0 41 43 4b 20 25 64 5c 6e 22 2c 20 50 41 47 45 52  ACK %d\n", PAGER
ba100 49 44 28 70 50 61 67 65 72 29 29 3b 0a 20 20 69  ID(pPager));.  i
ba110 66 28 20 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20  f( MEMDB ){.    
ba120 50 67 48 64 72 20 2a 70 3b 0a 20 20 20 20 66 6f  PgHdr *p;.    fo
ba130 72 28 70 3d 70 50 61 67 65 72 2d 3e 70 41 6c 6c  r(p=pPager->pAll
ba140 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 41  ; p; p=p->pNextA
ba150 6c 6c 29 7b 0a 20 20 20 20 20 20 50 67 48 69 73  ll){.      PgHis
ba160 74 6f 72 79 20 2a 70 48 69 73 74 3b 0a 20 20 20  tory *pHist;.   
ba170 20 20 20 61 73 73 65 72 74 28 20 21 70 2d 3e 61     assert( !p->a
ba180 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b 20 29 3b  lwaysRollback );
ba190 0a 20 20 20 20 20 20 69 66 28 20 21 70 2d 3e 64  .      if( !p->d
ba1a0 69 72 74 79 20 29 7b 0a 20 20 20 20 20 20 20 20  irty ){.        
ba1b0 61 73 73 65 72 74 28 20 21 28 28 50 67 48 69 73  assert( !((PgHis
ba1c0 74 6f 72 79 20 2a 29 50 47 48 44 52 5f 54 4f 5f  tory *)PGHDR_TO_
ba1d0 48 49 53 54 28 70 2c 20 70 50 61 67 65 72 29 29  HIST(p, pPager))
ba1e0 2d 3e 70 4f 72 69 67 20 29 3b 0a 20 20 20 20 20  ->pOrig );.     
ba1f0 20 20 20 61 73 73 65 72 74 28 20 21 28 28 50 67     assert( !((Pg
ba200 48 69 73 74 6f 72 79 20 2a 29 50 47 48 44 52 5f  History *)PGHDR_
ba210 54 4f 5f 48 49 53 54 28 70 2c 20 70 50 61 67 65  TO_HIST(p, pPage
ba220 72 29 29 2d 3e 70 53 74 6d 74 20 29 3b 0a 20 20  r))->pStmt );.  
ba230 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a        continue;.
ba240 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 70        }..      p
ba250 48 69 73 74 20 3d 20 50 47 48 44 52 5f 54 4f 5f  Hist = PGHDR_TO_
ba260 48 49 53 54 28 70 2c 20 70 50 61 67 65 72 29 3b  HIST(p, pPager);
ba270 0a 20 20 20 20 20 20 69 66 28 20 70 48 69 73 74  .      if( pHist
ba280 2d 3e 70 4f 72 69 67 20 29 7b 0a 20 20 20 20 20  ->pOrig ){.     
ba290 20 20 20 6d 65 6d 63 70 79 28 50 47 48 44 52 5f     memcpy(PGHDR_
ba2a0 54 4f 5f 44 41 54 41 28 70 29 2c 20 70 48 69 73  TO_DATA(p), pHis
ba2b0 74 2d 3e 70 4f 72 69 67 2c 20 70 50 61 67 65 72  t->pOrig, pPager
ba2c0 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20  ->pageSize);.   
ba2d0 20 20 20 20 20 50 41 47 45 52 54 52 41 43 45 33       PAGERTRACE3
ba2e0 28 22 52 4f 4c 4c 42 41 43 4b 2d 50 41 47 45 20  ("ROLLBACK-PAGE 
ba2f0 25 64 20 6f 66 20 25 64 5c 6e 22 2c 20 70 2d 3e  %d of %d\n", p->
ba300 70 67 6e 6f 2c 20 50 41 47 45 52 49 44 28 70 50  pgno, PAGERID(pP
ba310 61 67 65 72 29 29 3b 0a 20 20 20 20 20 20 7d 65  ager));.      }e
ba320 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 50 41 47  lse{.        PAG
ba330 45 52 54 52 41 43 45 33 28 22 50 41 47 45 20 25  ERTRACE3("PAGE %
ba340 64 20 69 73 20 63 6c 65 61 6e 20 6f 6e 20 25 64  d is clean on %d
ba350 5c 6e 22 2c 20 70 2d 3e 70 67 6e 6f 2c 20 50 41  \n", p->pgno, PA
ba360 47 45 52 49 44 28 70 50 61 67 65 72 29 29 3b 0a  GERID(pPager));.
ba370 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 6c        }.      cl
ba380 65 61 72 48 69 73 74 6f 72 79 28 70 48 69 73 74  earHistory(pHist
ba390 29 3b 0a 20 20 20 20 20 20 70 2d 3e 64 69 72 74  );.      p->dirt
ba3a0 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e  y = 0;.      p->
ba3b0 69 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20  inJournal = 0;. 
ba3c0 20 20 20 20 20 70 48 69 73 74 2d 3e 69 6e 53 74       pHist->inSt
ba3d0 6d 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 48  mt = 0;.      pH
ba3e0 69 73 74 2d 3e 70 50 72 65 76 53 74 6d 74 20 3d  ist->pPrevStmt =
ba3f0 20 70 48 69 73 74 2d 3e 70 4e 65 78 74 53 74 6d   pHist->pNextStm
ba400 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28  t = 0;.      if(
ba410 20 70 50 61 67 65 72 2d 3e 78 52 65 69 6e 69 74   pPager->xReinit
ba420 65 72 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50  er ){.        pP
ba430 61 67 65 72 2d 3e 78 52 65 69 6e 69 74 65 72 28  ager->xReiniter(
ba440 70 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53  p, pPager->pageS
ba450 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ize);.      }.  
ba460 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e    }.    pPager->
ba470 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 20 20  pDirty = 0;.    
ba480 70 50 61 67 65 72 2d 3e 70 53 74 6d 74 20 3d 20  pPager->pStmt = 
ba490 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64  0;.    pPager->d
ba4a0 62 53 69 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e  bSize = pPager->
ba4b0 6f 72 69 67 44 62 53 69 7a 65 3b 0a 20 20 20 20  origDbSize;.    
ba4c0 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65 5f 63  pager_truncate_c
ba4d0 61 63 68 65 28 70 50 61 67 65 72 29 3b 0a 20 20  ache(pPager);.  
ba4e0 20 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 49 6e    pPager->stmtIn
ba4f0 55 73 65 20 3d 20 30 3b 0a 20 20 20 20 70 50 61  Use = 0;.    pPa
ba500 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47  ger->state = PAG
ba510 45 52 5f 53 48 41 52 45 44 3b 0a 20 20 20 20 72  ER_SHARED;.    r
ba520 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
ba530 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 61  .  }..  if( !pPa
ba540 67 65 72 2d 3e 64 69 72 74 79 43 61 63 68 65 20  ger->dirtyCache 
ba550 7c 7c 20 21 70 50 61 67 65 72 2d 3e 6a 6f 75 72  || !pPager->jour
ba560 6e 61 6c 4f 70 65 6e 20 29 7b 0a 20 20 20 20 72  nalOpen ){.    r
ba570 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72  c = pager_end_tr
ba580 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72  ansaction(pPager
ba590 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  );.    return rc
ba5a0 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 61  ;.  }..  if( pPa
ba5b0 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 26 26 20  ger->errCode && 
ba5c0 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 21  pPager->errCode!
ba5d0 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 7b 0a  =SQLITE_FULL ){.
ba5e0 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e      if( pPager->
ba5f0 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43  state>=PAGER_EXC
ba600 4c 55 53 49 56 45 20 29 7b 0a 20 20 20 20 20 20  LUSIVE ){.      
ba610 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 70  pager_playback(p
ba620 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 7d  Pager, 0);.    }
ba630 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50 61 67  .    return pPag
ba640 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d  er->errCode;.  }
ba650 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73  .  if( pPager->s
ba660 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 45 53 45  tate==PAGER_RESE
ba670 52 56 45 44 20 29 7b 0a 20 20 20 20 69 6e 74 20  RVED ){.    int 
ba680 72 63 32 3b 0a 20 20 20 20 72 63 20 3d 20 70 61  rc2;.    rc = pa
ba690 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 70 50 61  ger_playback(pPa
ba6a0 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 72 63 32  ger, 0);.    rc2
ba6b0 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61   = pager_end_tra
ba6c0 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 29  nsaction(pPager)
ba6d0 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
ba6e0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
ba6f0 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 20 20 7d   rc = rc2;.    }
ba700 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63  .  }else{.    rc
ba710 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63   = pager_playbac
ba720 6b 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20  k(pPager, 0);.  
ba730 7d 0a 20 20 2f 2a 20 70 61 67 65 72 5f 72 65 73  }.  /* pager_res
ba740 65 74 28 70 50 61 67 65 72 29 3b 20 2a 2f 0a 20  et(pPager); */. 
ba750 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20   pPager->dbSize 
ba760 3d 20 2d 31 3b 0a 0a 20 20 2f 2a 20 49 66 20 61  = -1;..  /* If a
ba770 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64  n error occurs d
ba780 75 72 69 6e 67 20 61 20 52 4f 4c 4c 42 41 43 4b  uring a ROLLBACK
ba790 2c 20 77 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e 67  , we can no long
ba7a0 65 72 20 74 72 75 73 74 20 74 68 65 20 70 61 67  er trust the pag
ba7b0 65 72 0a 20 20 2a 2a 20 63 61 63 68 65 2e 20 53  er.  ** cache. S
ba7c0 6f 20 63 61 6c 6c 20 70 61 67 65 72 5f 65 72 72  o call pager_err
ba7d0 6f 72 28 29 20 6f 6e 20 74 68 65 20 77 61 79 20  or() on the way 
ba7e0 6f 75 74 20 74 6f 20 6d 61 6b 65 20 61 6e 79 20  out to make any 
ba7f0 65 72 72 6f 72 20 0a 20 20 2a 2a 20 70 65 72 73  error .  ** pers
ba800 69 73 74 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 72  istent..  */.  r
ba810 65 74 75 72 6e 20 70 61 67 65 72 5f 65 72 72 6f  eturn pager_erro
ba820 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 7d  r(pPager, rc);.}
ba830 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54  ../*.** Return T
ba840 52 55 45 20 69 66 20 74 68 65 20 64 61 74 61 62  RUE if the datab
ba850 61 73 65 20 66 69 6c 65 20 69 73 20 6f 70 65 6e  ase file is open
ba860 65 64 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 20 52  ed read-only.  R
ba870 65 74 75 72 6e 20 46 41 4c 53 45 0a 2a 2a 20 69  eturn FALSE.** i
ba880 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  f the database i
ba890 73 20 28 69 6e 20 74 68 65 6f 72 79 29 20 77 72  s (in theory) wr
ba8a0 69 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54  itable..*/.SQLIT
ba8b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
ba8c0 6c 69 74 65 33 50 61 67 65 72 49 73 72 65 61 64  lite3PagerIsread
ba8d0 6f 6e 6c 79 28 50 61 67 65 72 20 2a 70 50 61 67  only(Pager *pPag
ba8e0 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50  er){.  return pP
ba8f0 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 3b 0a  ager->readOnly;.
ba900 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
ba910 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65  the number of re
ba920 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20  ferences to the 
ba930 70 61 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  pager..*/.SQLITE
ba940 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
ba950 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e  ite3PagerRefcoun
ba960 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  t(Pager *pPager)
ba970 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65  {.  return pPage
ba980 72 2d 3e 6e 52 65 66 3b 0a 7d 0a 0a 23 69 66 64  r->nRef;.}..#ifd
ba990 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f  ef SQLITE_TEST./
ba9a0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
ba9b0 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65  e is used for te
ba9c0 73 74 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73  sting and analys
ba9d0 69 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49  is only..*/.SQLI
ba9e0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 2a  TE_PRIVATE int *
ba9f0 73 71 6c 69 74 65 33 50 61 67 65 72 53 74 61 74  sqlite3PagerStat
baa00 73 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  s(Pager *pPager)
baa10 7b 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20 61  {.  static int a
baa20 5b 31 31 5d 3b 0a 20 20 61 5b 30 5d 20 3d 20 70  [11];.  a[0] = p
baa30 50 61 67 65 72 2d 3e 6e 52 65 66 3b 0a 20 20 61  Pager->nRef;.  a
baa40 5b 31 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e 50  [1] = pPager->nP
baa50 61 67 65 3b 0a 20 20 61 5b 32 5d 20 3d 20 70 50  age;.  a[2] = pP
baa60 61 67 65 72 2d 3e 6d 78 50 61 67 65 3b 0a 20 20  ager->mxPage;.  
baa70 61 5b 33 5d 20 3d 20 70 50 61 67 65 72 2d 3e 64  a[3] = pPager->d
baa80 62 53 69 7a 65 3b 0a 20 20 61 5b 34 5d 20 3d 20  bSize;.  a[4] = 
baa90 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3b 0a 20  pPager->state;. 
baaa0 20 61 5b 35 5d 20 3d 20 70 50 61 67 65 72 2d 3e   a[5] = pPager->
baab0 65 72 72 43 6f 64 65 3b 0a 20 20 61 5b 36 5d 20  errCode;.  a[6] 
baac0 3d 20 70 50 61 67 65 72 2d 3e 6e 48 69 74 3b 0a  = pPager->nHit;.
baad0 20 20 61 5b 37 5d 20 3d 20 70 50 61 67 65 72 2d    a[7] = pPager-
baae0 3e 6e 4d 69 73 73 3b 0a 20 20 61 5b 38 5d 20 3d  >nMiss;.  a[8] =
baaf0 20 30 3b 20 20 2f 2a 20 55 73 65 64 20 74 6f 20   0;  /* Used to 
bab00 62 65 20 70 50 61 67 65 72 2d 3e 6e 4f 76 66 6c  be pPager->nOvfl
bab10 20 2a 2f 0a 20 20 61 5b 39 5d 20 3d 20 70 50 61   */.  a[9] = pPa
bab20 67 65 72 2d 3e 6e 52 65 61 64 3b 0a 20 20 61 5b  ger->nRead;.  a[
bab30 31 30 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e 57  10] = pPager->nW
bab40 72 69 74 65 3b 0a 20 20 72 65 74 75 72 6e 20 61  rite;.  return a
bab50 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
bab60 2a 20 53 65 74 20 74 68 65 20 73 74 61 74 65 6d  * Set the statem
bab70 65 6e 74 20 72 6f 6c 6c 62 61 63 6b 20 70 6f 69  ent rollback poi
bab80 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  nt..**.** This r
bab90 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 62 65  outine should be
baba0 20 63 61 6c 6c 65 64 20 77 69 74 68 20 74 68 65   called with the
babb0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75   transaction jou
babc0 72 6e 61 6c 20 61 6c 72 65 61 64 79 0a 2a 2a 20  rnal already.** 
babd0 6f 70 65 6e 2e 20 20 41 20 6e 65 77 20 73 74 61  open.  A new sta
babe0 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 69  tement journal i
babf0 73 20 63 72 65 61 74 65 64 20 74 68 61 74 20 63  s created that c
bac00 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 72 6f  an be used to ro
bac10 6c 6c 62 61 63 6b 0a 2a 2a 20 63 68 61 6e 67 65  llback.** change
bac20 73 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 53 51  s of a single SQ
bac30 4c 20 63 6f 6d 6d 61 6e 64 20 77 69 74 68 69 6e  L command within
bac40 20 61 20 6c 61 72 67 65 72 20 74 72 61 6e 73 61   a larger transa
bac50 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ction..*/.SQLITE
bac60 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
bac70 69 74 65 33 50 61 67 65 72 53 74 6d 74 42 65 67  ite3PagerStmtBeg
bac80 69 6e 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  in(Pager *pPager
bac90 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61  ){.  int rc;.  a
baca0 73 73 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e  ssert( !pPager->
bacb0 73 74 6d 74 49 6e 55 73 65 20 29 3b 0a 20 20 61  stmtInUse );.  a
bacc0 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
bacd0 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 52  tate>=PAGER_SHAR
bace0 45 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ED );.  assert( 
bacf0 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3e 3d  pPager->dbSize>=
bad00 30 20 29 3b 0a 20 20 50 41 47 45 52 54 52 41 43  0 );.  PAGERTRAC
bad10 45 32 28 22 53 54 4d 54 2d 42 45 47 49 4e 20 25  E2("STMT-BEGIN %
bad20 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50  d\n", PAGERID(pP
bad30 61 67 65 72 29 29 3b 0a 20 20 69 66 28 20 4d 45  ager));.  if( ME
bad40 4d 44 42 20 29 7b 0a 20 20 20 20 70 50 61 67 65  MDB ){.    pPage
bad50 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20 3d 20 31  r->stmtInUse = 1
bad60 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74  ;.    pPager->st
bad70 6d 74 53 69 7a 65 20 3d 20 70 50 61 67 65 72 2d  mtSize = pPager-
bad80 3e 64 62 53 69 7a 65 3b 0a 20 20 20 20 72 65 74  >dbSize;.    ret
bad90 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
bada0 20 7d 0a 20 20 69 66 28 20 21 70 50 61 67 65 72   }.  if( !pPager
badb0 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 29 7b  ->journalOpen ){
badc0 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 6d  .    pPager->stm
badd0 74 41 75 74 6f 6f 70 65 6e 20 3d 20 31 3b 0a 20  tAutoopen = 1;. 
bade0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
badf0 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  _OK;.  }.  asser
bae00 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  t( pPager->journ
bae10 61 6c 4f 70 65 6e 20 29 3b 0a 20 20 70 50 61 67  alOpen );.  pPag
bae20 65 72 2d 3e 61 49 6e 53 74 6d 74 20 3d 20 73 71  er->aInStmt = sq
bae30 6c 69 74 65 4d 61 6c 6c 6f 63 28 20 70 50 61 67  liteMalloc( pPag
bae40 65 72 2d 3e 64 62 53 69 7a 65 2f 38 20 2b 20 31  er->dbSize/8 + 1
bae50 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72   );.  if( pPager
bae60 2d 3e 61 49 6e 53 74 6d 74 3d 3d 30 20 29 7b 0a  ->aInStmt==0 ){.
bae70 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 4f 73      /* sqlite3Os
bae80 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c  Lock(pPager->fd,
bae90 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 20 2a   SHARED_LOCK); *
baea0 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  /.    return SQL
baeb0 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 23  ITE_NOMEM;.  }.#
baec0 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20  ifndef NDEBUG.  
baed0 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69  rc = sqlite3OsFi
baee0 6c 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a  leSize(pPager->j
baef0 66 64 2c 20 26 70 50 61 67 65 72 2d 3e 73 74 6d  fd, &pPager->stm
baf00 74 4a 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 72  tJSize);.  if( r
baf10 63 20 29 20 67 6f 74 6f 20 73 74 6d 74 5f 62 65  c ) goto stmt_be
baf20 67 69 6e 5f 66 61 69 6c 65 64 3b 0a 20 20 61 73  gin_failed;.  as
baf30 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74  sert( pPager->st
baf40 6d 74 4a 53 69 7a 65 20 3d 3d 20 70 50 61 67 65  mtJSize == pPage
baf50 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 29 3b  r->journalOff );
baf60 0a 23 65 6e 64 69 66 0a 20 20 70 50 61 67 65 72  .#endif.  pPager
baf70 2d 3e 73 74 6d 74 4a 53 69 7a 65 20 3d 20 70 50  ->stmtJSize = pP
baf80 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
baf90 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 74 6d 74  ;.  pPager->stmt
bafa0 53 69 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 64  Size = pPager->d
bafb0 62 53 69 7a 65 3b 0a 20 20 70 50 61 67 65 72 2d  bSize;.  pPager-
bafc0 3e 73 74 6d 74 48 64 72 4f 66 66 20 3d 20 30 3b  >stmtHdrOff = 0;
bafd0 0a 20 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 43  .  pPager->stmtC
bafe0 6b 73 75 6d 20 3d 20 70 50 61 67 65 72 2d 3e 63  ksum = pPager->c
baff0 6b 73 75 6d 49 6e 69 74 3b 0a 20 20 69 66 28 20  ksumInit;.  if( 
bb000 21 70 50 61 67 65 72 2d 3e 73 74 6d 74 4f 70 65  !pPager->stmtOpe
bb010 6e 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  n ){.    rc = sq
bb020 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 74 65  lite3PagerOpente
bb030 6d 70 28 26 70 50 61 67 65 72 2d 3e 73 74 66 64  mp(&pPager->stfd
bb040 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20  );.    if( rc ) 
bb050 67 6f 74 6f 20 73 74 6d 74 5f 62 65 67 69 6e 5f  goto stmt_begin_
bb060 66 61 69 6c 65 64 3b 0a 20 20 20 20 70 50 61 67  failed;.    pPag
bb070 65 72 2d 3e 73 74 6d 74 4f 70 65 6e 20 3d 20 31  er->stmtOpen = 1
bb080 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74  ;.    pPager->st
bb090 6d 74 4e 52 65 63 20 3d 20 30 3b 0a 20 20 7d 0a  mtNRec = 0;.  }.
bb0a0 20 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 49 6e    pPager->stmtIn
bb0b0 55 73 65 20 3d 20 31 3b 0a 20 20 72 65 74 75 72  Use = 1;.  retur
bb0c0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 0a 73  n SQLITE_OK;. .s
bb0d0 74 6d 74 5f 62 65 67 69 6e 5f 66 61 69 6c 65 64  tmt_begin_failed
bb0e0 3a 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  :.  if( pPager->
bb0f0 61 49 6e 53 74 6d 74 20 29 7b 0a 20 20 20 20 73  aInStmt ){.    s
bb100 71 6c 69 74 65 46 72 65 65 28 70 50 61 67 65 72  qliteFree(pPager
bb110 2d 3e 61 49 6e 53 74 6d 74 29 3b 0a 20 20 20 20  ->aInStmt);.    
bb120 70 50 61 67 65 72 2d 3e 61 49 6e 53 74 6d 74 20  pPager->aInStmt 
bb130 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  = 0;.  }.  retur
bb140 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  n rc;.}../*.** C
bb150 6f 6d 6d 69 74 20 61 20 73 74 61 74 65 6d 65 6e  ommit a statemen
bb160 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
bb170 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
bb180 50 61 67 65 72 53 74 6d 74 43 6f 6d 6d 69 74 28  PagerStmtCommit(
bb190 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
bb1a0 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74    if( pPager->st
bb1b0 6d 74 49 6e 55 73 65 20 29 7b 0a 20 20 20 20 50  mtInUse ){.    P
bb1c0 67 48 64 72 20 2a 70 50 67 2c 20 2a 70 4e 65 78  gHdr *pPg, *pNex
bb1d0 74 3b 0a 20 20 20 20 50 41 47 45 52 54 52 41 43  t;.    PAGERTRAC
bb1e0 45 32 28 22 53 54 4d 54 2d 43 4f 4d 4d 49 54 20  E2("STMT-COMMIT 
bb1f0 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70  %d\n", PAGERID(p
bb200 50 61 67 65 72 29 29 3b 0a 20 20 20 20 69 66 28  Pager));.    if(
bb210 20 21 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 20   !MEMDB ){.     
bb220 20 73 71 6c 69 74 65 33 4f 73 53 65 65 6b 28 70   sqlite3OsSeek(p
bb230 50 61 67 65 72 2d 3e 73 74 66 64 2c 20 30 29 3b  Pager->stfd, 0);
bb240 0a 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65  .      /* sqlite
bb250 33 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61 67  3OsTruncate(pPag
bb260 65 72 2d 3e 73 74 66 64 2c 20 30 29 3b 20 2a 2f  er->stfd, 0); */
bb270 0a 20 20 20 20 20 20 73 71 6c 69 74 65 46 72 65  .      sqliteFre
bb280 65 28 20 70 50 61 67 65 72 2d 3e 61 49 6e 53 74  e( pPager->aInSt
bb290 6d 74 20 29 3b 0a 20 20 20 20 20 20 70 50 61 67  mt );.      pPag
bb2a0 65 72 2d 3e 61 49 6e 53 74 6d 74 20 3d 20 30 3b  er->aInStmt = 0;
bb2b0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
bb2c0 20 20 66 6f 72 28 70 50 67 3d 70 50 61 67 65 72    for(pPg=pPager
bb2d0 2d 3e 70 53 74 6d 74 3b 20 70 50 67 3b 20 70 50  ->pStmt; pPg; pP
bb2e0 67 3d 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20  g=pNext){.      
bb2f0 20 20 50 67 48 69 73 74 6f 72 79 20 2a 70 48 69    PgHistory *pHi
bb300 73 74 20 3d 20 50 47 48 44 52 5f 54 4f 5f 48 49  st = PGHDR_TO_HI
bb310 53 54 28 70 50 67 2c 20 70 50 61 67 65 72 29 3b  ST(pPg, pPager);
bb320 0a 20 20 20 20 20 20 20 20 70 4e 65 78 74 20 3d  .        pNext =
bb330 20 70 48 69 73 74 2d 3e 70 4e 65 78 74 53 74 6d   pHist->pNextStm
bb340 74 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  t;.        asser
bb350 74 28 20 70 48 69 73 74 2d 3e 69 6e 53 74 6d 74  t( pHist->inStmt
bb360 20 29 3b 0a 20 20 20 20 20 20 20 20 70 48 69 73   );.        pHis
bb370 74 2d 3e 69 6e 53 74 6d 74 20 3d 20 30 3b 0a 20  t->inStmt = 0;. 
bb380 20 20 20 20 20 20 20 70 48 69 73 74 2d 3e 70 50         pHist->pP
bb390 72 65 76 53 74 6d 74 20 3d 20 70 48 69 73 74 2d  revStmt = pHist-
bb3a0 3e 70 4e 65 78 74 53 74 6d 74 20 3d 20 30 3b 0a  >pNextStmt = 0;.
bb3b0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 46 72          sqliteFr
bb3c0 65 65 28 70 48 69 73 74 2d 3e 70 53 74 6d 74 29  ee(pHist->pStmt)
bb3d0 3b 0a 20 20 20 20 20 20 20 20 70 48 69 73 74 2d  ;.        pHist-
bb3e0 3e 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20  >pStmt = 0;.    
bb3f0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 50    }.    }.    pP
bb400 61 67 65 72 2d 3e 73 74 6d 74 4e 52 65 63 20 3d  ager->stmtNRec =
bb410 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   0;.    pPager->
bb420 73 74 6d 74 49 6e 55 73 65 20 3d 20 30 3b 0a 20  stmtInUse = 0;. 
bb430 20 20 20 70 50 61 67 65 72 2d 3e 70 53 74 6d 74     pPager->pStmt
bb440 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 50 61 67   = 0;.  }.  pPag
bb450 65 72 2d 3e 73 74 6d 74 41 75 74 6f 6f 70 65 6e  er->stmtAutoopen
bb460 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 53   = 0;.  return S
bb470 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
bb480 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 20 73 74  ** Rollback a st
bb490 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49  atement..*/.SQLI
bb4a0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
bb4b0 71 6c 69 74 65 33 50 61 67 65 72 53 74 6d 74 52  qlite3PagerStmtR
bb4c0 6f 6c 6c 62 61 63 6b 28 50 61 67 65 72 20 2a 70  ollback(Pager *p
bb4d0 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63  Pager){.  int rc
bb4e0 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  ;.  if( pPager->
bb4f0 73 74 6d 74 49 6e 55 73 65 20 29 7b 0a 20 20 20  stmtInUse ){.   
bb500 20 50 41 47 45 52 54 52 41 43 45 32 28 22 53 54   PAGERTRACE2("ST
bb510 4d 54 2d 52 4f 4c 4c 42 41 43 4b 20 25 64 5c 6e  MT-ROLLBACK %d\n
bb520 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65  ", PAGERID(pPage
bb530 72 29 29 3b 0a 20 20 20 20 69 66 28 20 4d 45 4d  r));.    if( MEM
bb540 44 42 20 29 7b 0a 20 20 20 20 20 20 50 67 48 64  DB ){.      PgHd
bb550 72 20 2a 70 50 67 3b 0a 20 20 20 20 20 20 50 67  r *pPg;.      Pg
bb560 48 69 73 74 6f 72 79 20 2a 70 48 69 73 74 3b 0a  History *pHist;.
bb570 20 20 20 20 20 20 66 6f 72 28 70 50 67 3d 70 50        for(pPg=pP
bb580 61 67 65 72 2d 3e 70 53 74 6d 74 3b 20 70 50 67  ager->pStmt; pPg
bb590 3b 20 70 50 67 3d 70 48 69 73 74 2d 3e 70 4e 65  ; pPg=pHist->pNe
bb5a0 78 74 53 74 6d 74 29 7b 0a 20 20 20 20 20 20 20  xtStmt){.       
bb5b0 20 70 48 69 73 74 20 3d 20 50 47 48 44 52 5f 54   pHist = PGHDR_T
bb5c0 4f 5f 48 49 53 54 28 70 50 67 2c 20 70 50 61 67  O_HIST(pPg, pPag
bb5d0 65 72 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  er);.        if(
bb5e0 20 70 48 69 73 74 2d 3e 70 53 74 6d 74 20 29 7b   pHist->pStmt ){
bb5f0 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70  .          memcp
bb600 79 28 50 47 48 44 52 5f 54 4f 5f 44 41 54 41 28  y(PGHDR_TO_DATA(
bb610 70 50 67 29 2c 20 70 48 69 73 74 2d 3e 70 53 74  pPg), pHist->pSt
bb620 6d 74 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65  mt, pPager->page
bb630 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 20  Size);.         
bb640 20 73 71 6c 69 74 65 46 72 65 65 28 70 48 69 73   sqliteFree(pHis
bb650 74 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20 20  t->pStmt);.     
bb660 20 20 20 20 20 70 48 69 73 74 2d 3e 70 53 74 6d       pHist->pStm
bb670 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d  t = 0;.        }
bb680 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
bb690 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20  Pager->dbSize = 
bb6a0 70 50 61 67 65 72 2d 3e 73 74 6d 74 53 69 7a 65  pPager->stmtSize
bb6b0 3b 0a 20 20 20 20 20 20 70 61 67 65 72 5f 74 72  ;.      pager_tr
bb6c0 75 6e 63 61 74 65 5f 63 61 63 68 65 28 70 50 61  uncate_cache(pPa
bb6d0 67 65 72 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  ger);.      rc =
bb6e0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
bb6f0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20  }else{.      rc 
bb700 3d 20 70 61 67 65 72 5f 73 74 6d 74 5f 70 6c 61  = pager_stmt_pla
bb710 79 62 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20  yback(pPager);. 
bb720 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
bb730 50 61 67 65 72 53 74 6d 74 43 6f 6d 6d 69 74 28  PagerStmtCommit(
bb740 70 50 61 67 65 72 29 3b 0a 20 20 7d 65 6c 73 65  pPager);.  }else
bb750 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
bb760 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 50 61 67  E_OK;.  }.  pPag
bb770 65 72 2d 3e 73 74 6d 74 41 75 74 6f 6f 70 65 6e  er->stmtAutoopen
bb780 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 72   = 0;.  return r
bb790 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  c;.}../*.** Retu
bb7a0 72 6e 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68  rn the full path
bb7b0 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  name of the data
bb7c0 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51  base file..*/.SQ
bb7d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
bb7e0 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
bb7f0 50 61 67 65 72 46 69 6c 65 6e 61 6d 65 28 50 61  PagerFilename(Pa
bb800 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
bb810 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 7a  return pPager->z
bb820 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a  Filename;.}../*.
bb830 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 64 69  ** Return the di
bb840 72 65 63 74 6f 72 79 20 6f 66 20 74 68 65 20 64  rectory of the d
bb850 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f  atabase file..*/
bb860 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
bb870 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
bb880 74 65 33 50 61 67 65 72 44 69 72 6e 61 6d 65 28  te3PagerDirname(
bb890 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
bb8a0 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d    return pPager-
bb8b0 3e 7a 44 69 72 65 63 74 6f 72 79 3b 0a 7d 0a 0a  >zDirectory;.}..
bb8c0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
bb8d0 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f   full pathname o
bb8e0 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  f the journal fi
bb8f0 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  le..*/.SQLITE_PR
bb900 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72  IVATE const char
bb910 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f   *sqlite3PagerJo
bb920 75 72 6e 61 6c 6e 61 6d 65 28 50 61 67 65 72 20  urnalname(Pager 
bb930 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75  *pPager){.  retu
bb940 72 6e 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72  rn pPager->zJour
bb950 6e 61 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  nal;.}../*.** Re
bb960 74 75 72 6e 20 74 72 75 65 20 69 66 20 66 73 79  turn true if fsy
bb970 6e 63 28 29 20 63 61 6c 6c 73 20 61 72 65 20 64  nc() calls are d
bb980 69 73 61 62 6c 65 64 20 66 6f 72 20 74 68 69 73  isabled for this
bb990 20 70 61 67 65 72 2e 20 20 52 65 74 75 72 6e 20   pager.  Return 
bb9a0 46 41 4c 53 45 0a 2a 2a 20 69 66 20 66 73 79 6e  FALSE.** if fsyn
bb9b0 63 28 29 73 20 61 72 65 20 65 78 65 63 75 74 65  c()s are execute
bb9c0 64 20 6e 6f 72 6d 61 6c 6c 79 2e 0a 2a 2f 0a 53  d normally..*/.S
bb9d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
bb9e0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4e 6f  t sqlite3PagerNo
bb9f0 73 79 6e 63 28 50 61 67 65 72 20 2a 70 50 61 67  sync(Pager *pPag
bba00 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50  er){.  return pP
bba10 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 3b 0a 7d 0a  ager->noSync;.}.
bba20 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48  .#ifdef SQLITE_H
bba30 41 53 5f 43 4f 44 45 43 0a 2f 2a 0a 2a 2a 20 53  AS_CODEC./*.** S
bba40 65 74 20 74 68 65 20 63 6f 64 65 63 20 66 6f 72  et the codec for
bba50 20 74 68 69 73 20 70 61 67 65 72 0a 2a 2f 0a 53   this pager.*/.S
bba60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
bba70 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 53  id sqlite3PagerS
bba80 65 74 43 6f 64 65 63 28 0a 20 20 50 61 67 65 72  etCodec(.  Pager
bba90 20 2a 70 50 61 67 65 72 2c 0a 20 20 76 6f 69 64   *pPager,.  void
bbaa0 20 2a 28 2a 78 43 6f 64 65 63 29 28 76 6f 69 64   *(*xCodec)(void
bbab0 2a 2c 76 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74  *,void*,Pgno,int
bbac0 29 2c 0a 20 20 76 6f 69 64 20 2a 70 43 6f 64 65  ),.  void *pCode
bbad0 63 41 72 67 0a 29 7b 0a 20 20 70 50 61 67 65 72  cArg.){.  pPager
bbae0 2d 3e 78 43 6f 64 65 63 20 3d 20 78 43 6f 64 65  ->xCodec = xCode
bbaf0 63 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70 43 6f  c;.  pPager->pCo
bbb00 64 65 63 41 72 67 20 3d 20 70 43 6f 64 65 63 41  decArg = pCodecA
bbb10 72 67 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69  rg;.}.#endif..#i
bbb20 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
bbb30 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a  T_AUTOVACUUM./*.
bbb40 2a 2a 20 4d 6f 76 65 20 74 68 65 20 70 61 67 65  ** Move the page
bbb50 20 70 50 67 20 74 6f 20 6c 6f 63 61 74 69 6f 6e   pPg to location
bbb60 20 70 67 6e 6f 20 69 6e 20 74 68 65 20 66 69 6c   pgno in the fil
bbb70 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20  e. .**.** There 
bbb80 6d 75 73 74 20 62 65 20 6e 6f 20 72 65 66 65 72  must be no refer
bbb90 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61 67  ences to the pag
bbba0 65 20 70 72 65 76 69 6f 75 73 6c 79 20 6c 6f 63  e previously loc
bbbb0 61 74 65 64 20 61 74 0a 2a 2a 20 70 67 6e 6f 20  ated at.** pgno 
bbbc0 28 77 68 69 63 68 20 77 65 20 63 61 6c 6c 20 70  (which we call p
bbbd0 50 67 4f 6c 64 29 20 74 68 6f 75 67 68 20 74 68  PgOld) though th
bbbe0 61 74 20 70 61 67 65 20 69 73 20 61 6c 6c 6f 77  at page is allow
bbbf0 65 64 20 74 6f 20 62 65 0a 2a 2a 20 69 6e 20 63  ed to be.** in c
bbc00 61 63 68 65 2e 20 20 49 66 20 74 68 65 20 70 61  ache.  If the pa
bbc10 67 65 20 70 72 65 76 69 6f 75 73 20 6c 6f 63 61  ge previous loca
bbc20 74 65 64 20 61 74 20 70 67 6e 6f 20 69 73 20 6e  ted at pgno is n
bbc30 6f 74 20 61 6c 72 65 61 64 79 0a 2a 2a 20 69 6e  ot already.** in
bbc40 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f   the rollback jo
bbc50 75 72 6e 61 6c 2c 20 69 74 20 69 73 20 6e 6f 74  urnal, it is not
bbc60 20 70 75 74 20 74 68 65 72 65 20 62 79 20 62 79   put there by by
bbc70 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a   this routine..*
bbc80 2a 0a 2a 2a 20 52 65 66 65 72 65 6e 63 65 73 20  *.** References 
bbc90 74 6f 20 74 68 65 20 70 61 67 65 20 70 50 67 20  to the page pPg 
bbca0 72 65 6d 61 69 6e 20 76 61 6c 69 64 2e 20 55 70  remain valid. Up
bbcb0 64 61 74 69 6e 67 20 61 6e 79 0a 2a 2a 20 6d 65  dating any.** me
bbcc0 74 61 2d 64 61 74 61 20 61 73 73 6f 63 69 61 74  ta-data associat
bbcd0 65 64 20 77 69 74 68 20 70 50 67 20 28 69 2e 65  ed with pPg (i.e
bbce0 2e 20 64 61 74 61 20 73 74 6f 72 65 64 20 69 6e  . data stored in
bbcf0 20 74 68 65 20 6e 45 78 74 72 61 20 62 79 74 65   the nExtra byte
bbd00 73 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61  s.** allocated a
bbd10 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 70 61  long with the pa
bbd20 67 65 29 20 69 73 20 74 68 65 20 72 65 73 70 6f  ge) is the respo
bbd30 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65  nsibility of the
bbd40 20 63 61 6c 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 41   caller..**.** A
bbd50 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73   transaction mus
bbd60 74 20 62 65 20 61 63 74 69 76 65 20 77 68 65 6e  t be active when
bbd70 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
bbd80 20 63 61 6c 6c 65 64 2e 20 49 74 20 75 73 65 64   called. It used
bbd90 20 74 6f 20 62 65 0a 2a 2a 20 72 65 71 75 69 72   to be.** requir
bbda0 65 64 20 74 68 61 74 20 61 20 73 74 61 74 65 6d  ed that a statem
bbdb0 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ent transaction 
bbdc0 77 61 73 20 6e 6f 74 20 61 63 74 69 76 65 2c 20  was not active, 
bbdd0 62 75 74 20 74 68 69 73 20 72 65 73 74 72 69 63  but this restric
bbde0 74 69 6f 6e 0a 2a 2a 20 68 61 73 20 62 65 65 6e  tion.** has been
bbdf0 20 72 65 6d 6f 76 65 64 20 28 43 52 45 41 54 45   removed (CREATE
bbe00 20 49 4e 44 45 58 20 6e 65 65 64 73 20 74 6f 20   INDEX needs to 
bbe10 6d 6f 76 65 20 61 20 70 61 67 65 20 77 68 65 6e  move a page when
bbe20 20 61 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20   a statement.** 
bbe30 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61  transaction is a
bbe40 63 74 69 76 65 29 2e 0a 2a 2f 0a 53 51 4c 49 54  ctive)..*/.SQLIT
bbe50 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
bbe60 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65 70 61  lite3PagerMovepa
bbe70 67 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ge(Pager *pPager
bbe80 2c 20 44 62 50 61 67 65 20 2a 70 50 67 2c 20 50  , DbPage *pPg, P
bbe90 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 50 67 48  gno pgno){.  PgH
bbea0 64 72 20 2a 70 50 67 4f 6c 64 3b 20 20 2f 2a 20  dr *pPgOld;  /* 
bbeb0 54 68 65 20 70 61 67 65 20 62 65 69 6e 67 20 6f  The page being o
bbec0 76 65 72 77 72 69 74 74 65 6e 2e 20 2a 2f 0a 20  verwritten. */. 
bbed0 20 69 6e 74 20 68 3b 0a 20 20 50 67 6e 6f 20 6e   int h;.  Pgno n
bbee0 65 65 64 53 79 6e 63 50 67 6e 6f 20 3d 20 30 3b  eedSyncPgno = 0;
bbef0 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 2d  ..  assert( pPg-
bbf00 3e 6e 52 65 66 3e 30 20 29 3b 0a 0a 20 20 50 41  >nRef>0 );..  PA
bbf10 47 45 52 54 52 41 43 45 35 28 22 4d 4f 56 45 20  GERTRACE5("MOVE 
bbf20 25 64 20 70 61 67 65 20 25 64 20 28 6e 65 65 64  %d page %d (need
bbf30 53 79 6e 63 3d 25 64 29 20 6d 6f 76 65 73 20 74  Sync=%d) moves t
bbf40 6f 20 25 64 5c 6e 22 2c 20 0a 20 20 20 20 20 20  o %d\n", .      
bbf50 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c  PAGERID(pPager),
bbf60 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 70 50 67 2d   pPg->pgno, pPg-
bbf70 3e 6e 65 65 64 53 79 6e 63 2c 20 70 67 6e 6f 29  >needSync, pgno)
bbf80 3b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 4d 4f  ;.  IOTRACE(("MO
bbf90 56 45 20 25 70 20 25 64 20 25 64 5c 6e 22 2c 20  VE %p %d %d\n", 
bbfa0 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e  pPager, pPg->pgn
bbfb0 6f 2c 20 70 67 6e 6f 29 29 0a 0a 20 20 70 61 67  o, pgno))..  pag
bbfc0 65 72 5f 67 65 74 5f 63 6f 6e 74 65 6e 74 28 70  er_get_content(p
bbfd0 50 67 29 3b 0a 20 20 69 66 28 20 70 50 67 2d 3e  Pg);.  if( pPg->
bbfe0 6e 65 65 64 53 79 6e 63 20 29 7b 0a 20 20 20 20  needSync ){.    
bbff0 6e 65 65 64 53 79 6e 63 50 67 6e 6f 20 3d 20 70  needSyncPgno = p
bc000 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 61 73  Pg->pgno;.    as
bc010 73 65 72 74 28 20 70 50 67 2d 3e 69 6e 4a 6f 75  sert( pPg->inJou
bc020 72 6e 61 6c 20 7c 7c 20 28 69 6e 74 29 70 67 6e  rnal || (int)pgn
bc030 6f 3e 70 50 61 67 65 72 2d 3e 6f 72 69 67 44 62  o>pPager->origDb
bc040 53 69 7a 65 20 29 3b 0a 20 20 20 20 61 73 73 65  Size );.    asse
bc050 72 74 28 20 70 50 67 2d 3e 64 69 72 74 79 20 29  rt( pPg->dirty )
bc060 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  ;.    assert( pP
bc070 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 29  ager->needSync )
bc080 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 6e 6c 69  ;.  }..  /* Unli
bc090 6e 6b 20 70 50 67 20 66 72 6f 6d 20 69 74 27 73  nk pPg from it's
bc0a0 20 68 61 73 68 2d 63 68 61 69 6e 20 2a 2f 0a 20   hash-chain */. 
bc0b0 20 75 6e 6c 69 6e 6b 48 61 73 68 43 68 61 69 6e   unlinkHashChain
bc0c0 28 70 50 61 67 65 72 2c 20 70 50 67 29 3b 0a 0a  (pPager, pPg);..
bc0d0 20 20 2f 2a 20 49 66 20 74 68 65 20 63 61 63 68    /* If the cach
bc0e0 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 61 67  e contains a pag
bc0f0 65 20 77 69 74 68 20 70 61 67 65 2d 6e 75 6d 62  e with page-numb
bc100 65 72 20 70 67 6e 6f 2c 20 72 65 6d 6f 76 65 20  er pgno, remove 
bc110 69 74 0a 20 20 2a 2a 20 66 72 6f 6d 20 69 74 27  it.  ** from it'
bc120 73 20 68 61 73 68 20 63 68 61 69 6e 2e 20 41 6c  s hash chain. Al
bc130 73 6f 2c 20 69 66 20 74 68 65 20 50 67 48 64 72  so, if the PgHdr
bc140 2e 6e 65 65 64 53 79 6e 63 20 77 61 73 20 73 65  .needSync was se
bc150 74 20 66 6f 72 20 0a 20 20 2a 2a 20 70 61 67 65  t for .  ** page
bc160 20 70 67 6e 6f 20 62 65 66 6f 72 65 20 74 68 65   pgno before the
bc170 20 27 6d 6f 76 65 27 20 6f 70 65 72 61 74 69 6f   'move' operatio
bc180 6e 2c 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62  n, it needs to b
bc190 65 20 72 65 74 61 69 6e 65 64 20 0a 20 20 2a 2a  e retained .  **
bc1a0 20 66 6f 72 20 74 68 65 20 70 61 67 65 20 6d 6f   for the page mo
bc1b0 76 65 64 20 74 68 65 72 65 2e 0a 20 20 2a 2f 0a  ved there..  */.
bc1c0 20 20 70 50 67 2d 3e 6e 65 65 64 53 79 6e 63 20    pPg->needSync 
bc1d0 3d 20 30 3b 0a 20 20 70 50 67 4f 6c 64 20 3d 20  = 0;.  pPgOld = 
bc1e0 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61  pager_lookup(pPa
bc1f0 67 65 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 69 66  ger, pgno);.  if
bc200 28 20 70 50 67 4f 6c 64 20 29 7b 0a 20 20 20 20  ( pPgOld ){.    
bc210 61 73 73 65 72 74 28 20 70 50 67 4f 6c 64 2d 3e  assert( pPgOld->
bc220 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 20 20 75  nRef==0 );.    u
bc230 6e 6c 69 6e 6b 48 61 73 68 43 68 61 69 6e 28 70  nlinkHashChain(p
bc240 50 61 67 65 72 2c 20 70 50 67 4f 6c 64 29 3b 0a  Pager, pPgOld);.
bc250 20 20 20 20 6d 61 6b 65 43 6c 65 61 6e 28 70 50      makeClean(pP
bc260 67 4f 6c 64 29 3b 0a 20 20 20 20 70 50 67 2d 3e  gOld);.    pPg->
bc270 6e 65 65 64 53 79 6e 63 20 3d 20 70 50 67 4f 6c  needSync = pPgOl
bc280 64 2d 3e 6e 65 65 64 53 79 6e 63 3b 0a 20 20 7d  d->needSync;.  }
bc290 65 6c 73 65 7b 0a 20 20 20 20 70 50 67 2d 3e 6e  else{.    pPg->n
bc2a0 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 7d  eedSync = 0;.  }
bc2b0 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 61  .  if( pPager->a
bc2c0 49 6e 4a 6f 75 72 6e 61 6c 20 26 26 20 28 69 6e  InJournal && (in
bc2d0 74 29 70 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e  t)pgno<=pPager->
bc2e0 6f 72 69 67 44 62 53 69 7a 65 20 29 7b 0a 20 20  origDbSize ){.  
bc2f0 20 20 70 50 67 2d 3e 69 6e 4a 6f 75 72 6e 61 6c    pPg->inJournal
bc300 20 3d 20 20 28 70 50 61 67 65 72 2d 3e 61 49 6e   =  (pPager->aIn
bc310 4a 6f 75 72 6e 61 6c 5b 70 67 6e 6f 2f 38 5d 20  Journal[pgno/8] 
bc320 26 20 28 31 3c 3c 28 70 67 6e 6f 26 37 29 29 29  & (1<<(pgno&7)))
bc330 21 3d 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  !=0;.  }else{.  
bc340 20 20 70 50 67 2d 3e 69 6e 4a 6f 75 72 6e 61 6c    pPg->inJournal
bc350 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74   = 0;.    assert
bc360 28 20 70 50 67 2d 3e 6e 65 65 64 53 79 6e 63 3d  ( pPg->needSync=
bc370 3d 30 20 7c 7c 20 28 69 6e 74 29 70 67 6e 6f 3e  =0 || (int)pgno>
bc380 70 50 61 67 65 72 2d 3e 6f 72 69 67 44 62 53 69  pPager->origDbSi
bc390 7a 65 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ze );.  }..  /* 
bc3a0 43 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 20  Change the page 
bc3b0 6e 75 6d 62 65 72 20 66 6f 72 20 70 50 67 20 61  number for pPg a
bc3c0 6e 64 20 69 6e 73 65 72 74 20 69 74 20 69 6e 74  nd insert it int
bc3d0 6f 20 74 68 65 20 6e 65 77 20 68 61 73 68 2d 63  o the new hash-c
bc3e0 68 61 69 6e 2e 20 2a 2f 0a 20 20 61 73 73 65 72  hain. */.  asser
bc3f0 74 28 20 70 67 6e 6f 21 3d 30 20 29 3b 0a 20 20  t( pgno!=0 );.  
bc400 70 50 67 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e 6f  pPg->pgno = pgno
bc410 3b 0a 20 20 68 20 3d 20 70 67 6e 6f 20 26 20 28  ;.  h = pgno & (
bc420 70 50 61 67 65 72 2d 3e 6e 48 61 73 68 2d 31 29  pPager->nHash-1)
bc430 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  ;.  if( pPager->
bc440 61 48 61 73 68 5b 68 5d 20 29 7b 0a 20 20 20 20  aHash[h] ){.    
bc450 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
bc460 61 48 61 73 68 5b 68 5d 2d 3e 70 50 72 65 76 48  aHash[h]->pPrevH
bc470 61 73 68 3d 3d 30 20 29 3b 0a 20 20 20 20 70 50  ash==0 );.    pP
bc480 61 67 65 72 2d 3e 61 48 61 73 68 5b 68 5d 2d 3e  ager->aHash[h]->
bc490 70 50 72 65 76 48 61 73 68 20 3d 20 70 50 67 3b  pPrevHash = pPg;
bc4a0 0a 20 20 7d 0a 20 20 70 50 67 2d 3e 70 4e 65 78  .  }.  pPg->pNex
bc4b0 74 48 61 73 68 20 3d 20 70 50 61 67 65 72 2d 3e  tHash = pPager->
bc4c0 61 48 61 73 68 5b 68 5d 3b 0a 20 20 70 50 61 67  aHash[h];.  pPag
bc4d0 65 72 2d 3e 61 48 61 73 68 5b 68 5d 20 3d 20 70  er->aHash[h] = p
bc4e0 50 67 3b 0a 20 20 70 50 67 2d 3e 70 50 72 65 76  Pg;.  pPg->pPrev
bc4f0 48 61 73 68 20 3d 20 30 3b 0a 0a 20 20 6d 61 6b  Hash = 0;..  mak
bc500 65 44 69 72 74 79 28 70 50 67 29 3b 0a 20 20 70  eDirty(pPg);.  p
bc510 50 61 67 65 72 2d 3e 64 69 72 74 79 43 61 63 68  Pager->dirtyCach
bc520 65 20 3d 20 31 3b 0a 0a 20 20 69 66 28 20 6e 65  e = 1;..  if( ne
bc530 65 64 53 79 6e 63 50 67 6e 6f 20 29 7b 0a 20 20  edSyncPgno ){.  
bc540 20 20 2f 2a 20 49 66 20 6e 65 65 64 53 79 6e 63    /* If needSync
bc550 50 67 6e 6f 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  Pgno is non-zero
bc560 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e  , then the journ
bc570 61 6c 20 66 69 6c 65 20 6e 65 65 64 73 20 74 6f  al file needs to
bc580 20 62 65 20 0a 20 20 20 20 2a 2a 20 73 79 6e 63   be .    ** sync
bc590 28 29 65 64 20 62 65 66 6f 72 65 20 61 6e 79 20  ()ed before any 
bc5a0 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 20  data is written 
bc5b0 74 6f 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  to database file
bc5c0 20 70 61 67 65 20 6e 65 65 64 53 79 6e 63 50 67   page needSyncPg
bc5d0 6e 6f 2e 0a 20 20 20 20 2a 2a 20 43 75 72 72 65  no..    ** Curre
bc5e0 6e 74 6c 79 2c 20 6e 6f 20 73 75 63 68 20 70 61  ntly, no such pa
bc5f0 67 65 20 65 78 69 73 74 73 20 69 6e 20 74 68 65  ge exists in the
bc600 20 70 61 67 65 2d 63 61 63 68 65 20 61 6e 64 20   page-cache and 
bc610 74 68 65 20 0a 20 20 20 20 2a 2a 20 50 61 67 65  the .    ** Page
bc620 72 2e 61 49 6e 4a 6f 75 72 6e 61 6c 20 62 69 74  r.aInJournal bit
bc630 20 68 61 73 20 62 65 65 6e 20 73 65 74 2e 20 54   has been set. T
bc640 68 69 73 20 6e 65 65 64 73 20 74 6f 20 62 65 20  his needs to be 
bc650 72 65 6d 65 64 69 65 64 20 62 79 20 6c 6f 61 64  remedied by load
bc660 69 6e 67 0a 20 20 20 20 2a 2a 20 74 68 65 20 70  ing.    ** the p
bc670 61 67 65 20 69 6e 74 6f 20 74 68 65 20 70 61 67  age into the pag
bc680 65 72 2d 63 61 63 68 65 20 61 6e 64 20 73 65 74  er-cache and set
bc690 74 69 6e 67 20 74 68 65 20 50 67 48 64 72 2e 6e  ting the PgHdr.n
bc6a0 65 65 64 53 79 6e 63 20 66 6c 61 67 2e 0a 20 20  eedSync flag..  
bc6b0 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20    **.    ** The 
bc6c0 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28  sqlite3PagerGet(
bc6d0 29 20 63 61 6c 6c 20 6d 61 79 20 63 61 75 73 65  ) call may cause
bc6e0 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 74 6f 20   the journal to 
bc6f0 73 79 6e 63 2e 20 53 6f 20 6d 61 6b 65 0a 20 20  sync. So make.  
bc700 20 20 2a 2a 20 73 75 72 65 20 74 68 65 20 50 61    ** sure the Pa
bc710 67 65 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c 61  ger.needSync fla
bc720 67 20 69 73 20 73 65 74 20 74 6f 6f 2e 0a 20 20  g is set too..  
bc730 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 63 3b    */.    int rc;
bc740 0a 20 20 20 20 50 67 48 64 72 20 2a 70 50 67 48  .    PgHdr *pPgH
bc750 64 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  dr;.    assert( 
bc760 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63  pPager->needSync
bc770 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   );.    rc = sql
bc780 69 74 65 33 50 61 67 65 72 47 65 74 28 70 50 61  ite3PagerGet(pPa
bc790 67 65 72 2c 20 6e 65 65 64 53 79 6e 63 50 67 6e  ger, needSyncPgn
bc7a0 6f 2c 20 26 70 50 67 48 64 72 29 3b 0a 20 20 20  o, &pPgHdr);.   
bc7b0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
bc7c0 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
bc7d0 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64      pPager->need
bc7e0 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 70 50  Sync = 1;.    pP
bc7f0 67 48 64 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d  gHdr->needSync =
bc800 20 31 3b 0a 20 20 20 20 70 50 67 48 64 72 2d 3e   1;.    pPgHdr->
bc810 69 6e 4a 6f 75 72 6e 61 6c 20 3d 20 31 3b 0a 20  inJournal = 1;. 
bc820 20 20 20 6d 61 6b 65 44 69 72 74 79 28 70 50 67     makeDirty(pPg
bc830 48 64 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Hdr);.    sqlite
bc840 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 67 48  3PagerUnref(pPgH
bc850 64 72 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  dr);.  }..  retu
bc860 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
bc870 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65  #endif../*.** Re
bc880 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
bc890 6f 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 74  o the data for t
bc8a0 68 65 20 73 70 65 63 69 66 69 65 64 20 70 61 67  he specified pag
bc8b0 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
bc8c0 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
bc8d0 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 44  e3PagerGetData(D
bc8e0 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 72  bPage *pPg){.  r
bc8f0 65 74 75 72 6e 20 50 47 48 44 52 5f 54 4f 5f 44  eturn PGHDR_TO_D
bc900 41 54 41 28 70 50 67 29 3b 0a 7d 0a 0a 2f 2a 0a  ATA(pPg);.}../*.
bc910 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  ** Return a poin
bc920 74 65 72 20 74 6f 20 74 68 65 20 50 61 67 65 72  ter to the Pager
bc930 2e 6e 45 78 74 72 61 20 62 79 74 65 73 20 6f 66  .nExtra bytes of
bc940 20 22 65 78 74 72 61 22 20 73 70 61 63 65 20 0a   "extra" space .
bc950 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 6c 6f  ** allocated alo
bc960 6e 67 20 77 69 74 68 20 74 68 65 20 73 70 65 63  ng with the spec
bc970 69 66 69 65 64 20 70 61 67 65 2e 0a 2a 2f 0a 53  ified page..*/.S
bc980 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
bc990 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72  id *sqlite3Pager
bc9a0 47 65 74 45 78 74 72 61 28 44 62 50 61 67 65 20  GetExtra(DbPage 
bc9b0 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a  *pPg){.  Pager *
bc9c0 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50  pPager = pPg->pP
bc9d0 61 67 65 72 3b 0a 20 20 72 65 74 75 72 6e 20 28  ager;.  return (
bc9e0 70 50 61 67 65 72 3f 50 47 48 44 52 5f 54 4f 5f  pPager?PGHDR_TO_
bc9f0 45 58 54 52 41 28 70 50 67 2c 20 70 50 61 67 65  EXTRA(pPg, pPage
bca00 72 29 3a 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  r):0);.}../*.** 
bca10 47 65 74 2f 73 65 74 20 74 68 65 20 6c 6f 63 6b  Get/set the lock
bca20 69 6e 67 2d 6d 6f 64 65 20 66 6f 72 20 74 68 69  ing-mode for thi
bca30 73 20 70 61 67 65 72 2e 20 50 61 72 61 6d 65 74  s pager. Paramet
bca40 65 72 20 65 4d 6f 64 65 20 6d 75 73 74 20 62 65  er eMode must be
bca50 20 6f 6e 65 0a 2a 2a 20 6f 66 20 50 41 47 45 52   one.** of PAGER
bca60 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45  _LOCKINGMODE_QUE
bca70 52 59 2c 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e  RY, PAGER_LOCKIN
bca80 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 6f 72 20  GMODE_NORMAL or 
bca90 0a 2a 2a 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e  .** PAGER_LOCKIN
bcaa0 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 2e  GMODE_EXCLUSIVE.
bcab0 20 49 66 20 74 68 65 20 70 61 72 61 6d 65 74 65   If the paramete
bcac0 72 20 69 73 20 6e 6f 74 20 5f 51 55 45 52 59 2c  r is not _QUERY,
bcad0 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6c 6f 63   then.** the loc
bcae0 6b 69 6e 67 2d 6d 6f 64 65 20 69 73 20 73 65 74  king-mode is set
bcaf0 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 73 70   to the value sp
bcb00 65 63 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  ecified..**.** T
bcb10 68 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75  he returned valu
bcb20 65 20 69 73 20 65 69 74 68 65 72 20 50 41 47 45  e is either PAGE
bcb30 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f  R_LOCKINGMODE_NO
bcb40 52 4d 41 4c 20 6f 72 0a 2a 2a 20 50 41 47 45 52  RMAL or.** PAGER
bcb50 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43  _LOCKINGMODE_EXC
bcb60 4c 55 53 49 56 45 2c 20 69 6e 64 69 63 61 74 69  LUSIVE, indicati
bcb70 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 28  ng the current (
bcb80 70 6f 73 73 69 62 6c 79 20 75 70 64 61 74 65 64  possibly updated
bcb90 29 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64  ).** locking-mod
bcba0 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
bcbb0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
bcbc0 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65  PagerLockingMode
bcbd0 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
bcbe0 69 6e 74 20 65 4d 6f 64 65 29 7b 0a 20 20 61 73  int eMode){.  as
bcbf0 73 65 72 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47  sert( eMode==PAG
bcc00 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51  ER_LOCKINGMODE_Q
bcc10 55 45 52 59 0a 20 20 20 20 20 20 20 20 20 20 20  UERY.           
bcc20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52   || eMode==PAGER
bcc30 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52  _LOCKINGMODE_NOR
bcc40 4d 41 4c 0a 20 20 20 20 20 20 20 20 20 20 20 20  MAL.            
bcc50 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  || eMode==PAGER_
bcc60 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c  LOCKINGMODE_EXCL
bcc70 55 53 49 56 45 20 29 3b 0a 20 20 61 73 73 65 72  USIVE );.  asser
bcc80 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  t( PAGER_LOCKING
bcc90 4d 4f 44 45 5f 51 55 45 52 59 3c 30 20 29 3b 0a  MODE_QUERY<0 );.
bcca0 20 20 61 73 73 65 72 74 28 20 50 41 47 45 52 5f    assert( PAGER_
bccb0 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d  LOCKINGMODE_NORM
bccc0 41 4c 3e 3d 30 20 26 26 20 50 41 47 45 52 5f 4c  AL>=0 && PAGER_L
bccd0 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55  OCKINGMODE_EXCLU
bcce0 53 49 56 45 3e 3d 30 20 29 3b 0a 20 20 69 66 28  SIVE>=0 );.  if(
bccf0 20 65 4d 6f 64 65 3e 3d 30 20 26 26 20 21 70 50   eMode>=0 && !pP
bcd00 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29  ager->tempFile )
bcd10 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65 78  {.    pPager->ex
bcd20 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 65 4d  clusiveMode = eM
bcd30 6f 64 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ode;.  }.  retur
bcd40 6e 20 28 69 6e 74 29 70 50 61 67 65 72 2d 3e 65  n (int)pPager->e
bcd50 78 63 6c 75 73 69 76 65 4d 6f 64 65 3b 0a 7d 0a  xclusiveMode;.}.
bcd60 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
bcd70 49 54 45 5f 44 45 42 55 47 29 20 7c 7c 20 64 65  ITE_DEBUG) || de
bcd80 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53  fined(SQLITE_TES
bcd90 54 29 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  T)./*.** Return 
bcda0 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 74  the current stat
bcdb0 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 6c 6f  e of the file lo
bcdc0 63 6b 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e  ck for the given
bcdd0 20 70 61 67 65 72 2e 0a 2a 2a 20 54 68 65 20 72   pager..** The r
bcde0 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 6f  eturn value is o
bcdf0 6e 65 20 6f 66 20 4e 4f 5f 4c 4f 43 4b 2c 20 53  ne of NO_LOCK, S
bce00 48 41 52 45 44 5f 4c 4f 43 4b 2c 20 52 45 53 45  HARED_LOCK, RESE
bce10 52 56 45 44 5f 4c 4f 43 4b 2c 0a 2a 2a 20 50 45  RVED_LOCK,.** PE
bce20 4e 44 49 4e 47 5f 4c 4f 43 4b 2c 20 6f 72 20 45  NDING_LOCK, or E
bce30 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 2e 0a 2a  XCLUSIVE_LOCK..*
bce40 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
bce50 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
bce60 72 4c 6f 63 6b 73 74 61 74 65 28 50 61 67 65 72  rLockstate(Pager
bce70 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74   *pPager){.  ret
bce80 75 72 6e 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63  urn sqlite3OsLoc
bce90 6b 53 74 61 74 65 28 70 50 61 67 65 72 2d 3e 66  kState(pPager->f
bcea0 64 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69  d);.}.#endif..#i
bceb0 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
bcec0 47 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 61 20  G./*.** Print a 
bced0 6c 69 73 74 69 6e 67 20 6f 66 20 61 6c 6c 20 72  listing of all r
bcee0 65 66 65 72 65 6e 63 65 64 20 70 61 67 65 73 20  eferenced pages 
bcef0 61 6e 64 20 74 68 65 69 72 20 72 65 66 20 63 6f  and their ref co
bcf00 75 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  unt..*/.SQLITE_P
bcf10 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
bcf20 74 65 33 50 61 67 65 72 52 65 66 64 75 6d 70 28  te3PagerRefdump(
bcf30 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
bcf40 20 20 50 67 48 64 72 20 2a 70 50 67 3b 0a 20 20    PgHdr *pPg;.  
bcf50 66 6f 72 28 70 50 67 3d 70 50 61 67 65 72 2d 3e  for(pPg=pPager->
bcf60 70 41 6c 6c 3b 20 70 50 67 3b 20 70 50 67 3d 70  pAll; pPg; pPg=p
bcf70 50 67 2d 3e 70 4e 65 78 74 41 6c 6c 29 7b 0a 20  Pg->pNextAll){. 
bcf80 20 20 20 69 66 28 20 70 50 67 2d 3e 6e 52 65 66     if( pPg->nRef
bcf90 3c 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  <=0 ) continue;.
bcfa0 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67      sqlite3Debug
bcfb0 50 72 69 6e 74 66 28 22 50 41 47 45 20 25 33 64  Printf("PAGE %3d
bcfc0 20 61 64 64 72 3d 25 70 20 6e 52 65 66 3d 25 64   addr=%p nRef=%d
bcfd0 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20 70 50 67  \n", .       pPg
bcfe0 2d 3e 70 67 6e 6f 2c 20 50 47 48 44 52 5f 54 4f  ->pgno, PGHDR_TO
bcff0 5f 44 41 54 41 28 70 50 67 29 2c 20 70 50 67 2d  _DATA(pPg), pPg-
bd000 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 7d 0a 23 65  >nRef);.  }.}.#e
bd010 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  ndif..#endif /* 
bd020 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b  SQLITE_OMIT_DISK
bd030 49 4f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  IO */../********
bd040 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61  ****** End of pa
bd050 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ger.c **********
bd060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd080 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
bd090 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
bd0a0 65 20 62 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a  e btree.c ******
bd0b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd0d0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
bd0e0 34 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20  4 April 6.**.** 
bd0f0 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
bd100 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
bd110 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
bd120 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
bd130 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
bd140 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
bd150 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
bd160 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
bd170 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
bd180 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
bd190 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
bd1a0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
bd1b0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
bd1c0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
bd1d0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
bd1e0 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
bd1f0 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
bd200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd240 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 24 49 64 3a  ********.** $Id:
bd250 20 62 74 72 65 65 2e 63 2c 76 20 31 2e 33 39 33   btree.c,v 1.393
bd260 20 32 30 30 37 2f 30 37 2f 32 33 20 31 39 3a 32   2007/07/23 19:2
bd270 36 3a 31 37 20 64 72 68 20 45 78 70 20 24 0a 2a  6:17 drh Exp $.*
bd280 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69  *.** This file i
bd290 6d 70 6c 65 6d 65 6e 74 73 20 61 20 65 78 74 65  mplements a exte
bd2a0 72 6e 61 6c 20 28 64 69 73 6b 2d 62 61 73 65 64  rnal (disk-based
bd2b0 29 20 64 61 74 61 62 61 73 65 20 75 73 69 6e 67  ) database using
bd2c0 20 42 54 72 65 65 73 2e 0a 2a 2a 20 53 65 65 20   BTrees..** See 
bd2d0 74 68 65 20 68 65 61 64 65 72 20 63 6f 6d 6d 65  the header comme
bd2e0 6e 74 20 6f 6e 20 22 62 74 72 65 65 49 6e 74 2e  nt on "btreeInt.
bd2f0 68 22 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61  h" for additiona
bd300 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a  l information..*
bd310 2a 20 49 6e 63 6c 75 64 69 6e 67 20 61 20 64 65  * Including a de
bd320 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 66 69 6c  scription of fil
bd330 65 20 66 6f 72 6d 61 74 20 61 6e 64 20 61 6e 20  e format and an 
bd340 6f 76 65 72 76 69 65 77 20 6f 66 20 6f 70 65 72  overview of oper
bd350 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ation..*/./*****
bd360 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64  ********* Includ
bd370 65 20 62 74 72 65 65 49 6e 74 2e 68 20 69 6e 20  e btreeInt.h in 
bd380 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 62 74  the middle of bt
bd390 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ree.c **********
bd3a0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
bd3b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
bd3c0 66 69 6c 65 20 62 74 72 65 65 49 6e 74 2e 68 20  file btreeInt.h 
bd3d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd3e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd3f0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
bd400 32 30 30 34 20 41 70 72 69 6c 20 36 0a 2a 2a 0a  2004 April 6.**.
bd410 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
bd420 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
bd430 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
bd440 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
bd450 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
bd460 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
bd470 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
bd480 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
bd490 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
bd4a0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
bd4b0 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
bd4c0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
bd4d0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
bd4e0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
bd4f0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
bd500 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
bd510 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
bd520 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
bd530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 24  ***********.** $
bd570 49 64 3a 20 62 74 72 65 65 49 6e 74 2e 68 2c 76  Id: btreeInt.h,v
bd580 20 31 2e 35 20 32 30 30 37 2f 30 36 2f 31 35 20   1.5 2007/06/15 
bd590 31 32 3a 30 36 3a 35 39 20 64 72 68 20 45 78 70  12:06:59 drh Exp
bd5a0 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69   $.**.** This fi
bd5b0 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20  le implements a 
bd5c0 65 78 74 65 72 6e 61 6c 20 28 64 69 73 6b 2d 62  external (disk-b
bd5d0 61 73 65 64 29 20 64 61 74 61 62 61 73 65 20 75  ased) database u
bd5e0 73 69 6e 67 20 42 54 72 65 65 73 2e 0a 2a 2a 20  sing BTrees..** 
bd5f0 46 6f 72 20 61 20 64 65 74 61 69 6c 65 64 20 64  For a detailed d
bd600 69 73 63 75 73 73 69 6f 6e 20 6f 66 20 42 54 72  iscussion of BTr
bd610 65 65 73 2c 20 72 65 66 65 72 20 74 6f 0a 2a 2a  ees, refer to.**
bd620 0a 2a 2a 20 20 20 20 20 44 6f 6e 61 6c 64 20 45  .**     Donald E
bd630 2e 20 4b 6e 75 74 68 2c 20 54 48 45 20 41 52 54  . Knuth, THE ART
bd640 20 4f 46 20 43 4f 4d 50 55 54 45 52 20 50 52 4f   OF COMPUTER PRO
bd650 47 52 41 4d 4d 49 4e 47 2c 20 56 6f 6c 75 6d 65  GRAMMING, Volume
bd660 20 33 3a 0a 2a 2a 20 20 20 20 20 22 53 6f 72 74   3:.**     "Sort
bd670 69 6e 67 20 41 6e 64 20 53 65 61 72 63 68 69 6e  ing And Searchin
bd680 67 22 2c 20 70 61 67 65 73 20 34 37 33 2d 34 38  g", pages 473-48
bd690 30 2e 20 41 64 64 69 73 6f 6e 2d 57 65 73 6c 65  0. Addison-Wesle
bd6a0 79 0a 2a 2a 20 20 20 20 20 50 75 62 6c 69 73 68  y.**     Publish
bd6b0 69 6e 67 20 43 6f 6d 70 61 6e 79 2c 20 52 65 61  ing Company, Rea
bd6c0 64 69 6e 67 2c 20 4d 61 73 73 61 63 68 75 73 65  ding, Massachuse
bd6d0 74 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62  tts..**.** The b
bd6e0 61 73 69 63 20 69 64 65 61 20 69 73 20 74 68 61  asic idea is tha
bd6f0 74 20 65 61 63 68 20 70 61 67 65 20 6f 66 20 74  t each page of t
bd700 68 65 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  he file contains
bd710 20 4e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 65   N database.** e
bd720 6e 74 72 69 65 73 20 61 6e 64 20 4e 2b 31 20 70  ntries and N+1 p
bd730 6f 69 6e 74 65 72 73 20 74 6f 20 73 75 62 70 61  ointers to subpa
bd740 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 2d 2d 2d  ges..**.**   ---
bd750 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
bd760 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
bd770 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
bd780 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a  -------------.**
bd790 20 20 20 7c 20 20 50 74 72 28 30 29 20 7c 20 4b     |  Ptr(0) | K
bd7a0 65 79 28 30 29 20 7c 20 50 74 72 28 31 29 20 7c  ey(0) | Ptr(1) |
bd7b0 20 4b 65 79 28 31 29 20 7c 20 2e 2e 2e 20 7c 20   Key(1) | ... | 
bd7c0 4b 65 79 28 4e 2d 31 29 20 7c 20 50 74 72 28 4e  Key(N-1) | Ptr(N
bd7d0 29 20 7c 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d  ) |.**   -------
bd7e0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
bd7f0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
bd800 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
bd810 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20  ---------.**.** 
bd820 41 6c 6c 20 6f 66 20 74 68 65 20 6b 65 79 73 20  All of the keys 
bd830 6f 6e 20 74 68 65 20 70 61 67 65 20 74 68 61 74  on the page that
bd840 20 50 74 72 28 30 29 20 70 6f 69 6e 74 73 20 74   Ptr(0) points t
bd850 6f 20 68 61 76 65 20 76 61 6c 75 65 73 20 6c 65  o have values le
bd860 73 73 0a 2a 2a 20 74 68 61 6e 20 4b 65 79 28 30  ss.** than Key(0
bd870 29 2e 20 20 41 6c 6c 20 6f 66 20 74 68 65 20 6b  ).  All of the k
bd880 65 79 73 20 6f 6e 20 70 61 67 65 20 50 74 72 28  eys on page Ptr(
bd890 31 29 20 61 6e 64 20 69 74 73 20 73 75 62 70 61  1) and its subpa
bd8a0 67 65 73 20 68 61 76 65 0a 2a 2a 20 76 61 6c 75  ges have.** valu
bd8b0 65 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  es greater than 
bd8c0 4b 65 79 28 30 29 20 61 6e 64 20 6c 65 73 73 20  Key(0) and less 
bd8d0 74 68 61 6e 20 4b 65 79 28 31 29 2e 20 20 41 6c  than Key(1).  Al
bd8e0 6c 20 6f 66 20 74 68 65 20 6b 65 79 73 0a 2a 2a  l of the keys.**
bd8f0 20 6f 6e 20 50 74 72 28 4e 29 20 61 6e 64 20 69   on Ptr(N) and i
bd900 74 73 20 73 75 62 70 61 67 65 73 20 68 61 76 65  ts subpages have
bd910 20 76 61 6c 75 65 73 20 67 72 65 61 74 65 72 20   values greater 
bd920 74 68 61 6e 20 4b 65 79 28 4e 2d 31 29 2e 20 20  than Key(N-1).  
bd930 41 6e 64 0a 2a 2a 20 73 6f 20 66 6f 72 74 68 2e  And.** so forth.
bd940 0a 2a 2a 0a 2a 2a 20 46 69 6e 64 69 6e 67 20 61  .**.** Finding a
bd950 20 70 61 72 74 69 63 75 6c 61 72 20 6b 65 79 20   particular key 
bd960 72 65 71 75 69 72 65 73 20 72 65 61 64 69 6e 67  requires reading
bd970 20 4f 28 6c 6f 67 28 4d 29 29 20 70 61 67 65 73   O(log(M)) pages
bd980 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 64 69   from the .** di
bd990 73 6b 20 77 68 65 72 65 20 4d 20 69 73 20 74 68  sk where M is th
bd9a0 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  e number of entr
bd9b0 69 65 73 20 69 6e 20 74 68 65 20 74 72 65 65 2e  ies in the tree.
bd9c0 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 69  .**.** In this i
bd9d0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 61  mplementation, a
bd9e0 20 73 69 6e 67 6c 65 20 66 69 6c 65 20 63 61 6e   single file can
bd9f0 20 68 6f 6c 64 20 6f 6e 65 20 6f 72 20 6d 6f 72   hold one or mor
bda00 65 20 73 65 70 61 72 61 74 65 20 0a 2a 2a 20 42  e separate .** B
bda10 54 72 65 65 73 2e 20 20 45 61 63 68 20 42 54 72  Trees.  Each BTr
bda20 65 65 20 69 73 20 69 64 65 6e 74 69 66 69 65 64  ee is identified
bda30 20 62 79 20 74 68 65 20 69 6e 64 65 78 20 6f 66   by the index of
bda40 20 69 74 73 20 72 6f 6f 74 20 70 61 67 65 2e 20   its root page. 
bda50 20 54 68 65 0a 2a 2a 20 6b 65 79 20 61 6e 64 20   The.** key and 
bda60 64 61 74 61 20 66 6f 72 20 61 6e 79 20 65 6e 74  data for any ent
bda70 72 79 20 61 72 65 20 63 6f 6d 62 69 6e 65 64 20  ry are combined 
bda80 74 6f 20 66 6f 72 6d 20 74 68 65 20 22 70 61 79  to form the "pay
bda90 6c 6f 61 64 22 2e 20 20 41 0a 2a 2a 20 66 69 78  load".  A.** fix
bdaa0 65 64 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79  ed amount of pay
bdab0 6c 6f 61 64 20 63 61 6e 20 62 65 20 63 61 72 72  load can be carr
bdac0 69 65 64 20 64 69 72 65 63 74 6c 79 20 6f 6e 20  ied directly on 
bdad0 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  the database.** 
bdae0 70 61 67 65 2e 20 20 49 66 20 74 68 65 20 70 61  page.  If the pa
bdaf0 79 6c 6f 61 64 20 69 73 20 6c 61 72 67 65 72 20  yload is larger 
bdb00 74 68 61 6e 20 74 68 65 20 70 72 65 73 65 74 20  than the preset 
bdb10 61 6d 6f 75 6e 74 20 74 68 65 6e 20 73 75 72 70  amount then surp
bdb20 6c 75 73 0a 2a 2a 20 62 79 74 65 73 20 61 72 65  lus.** bytes are
bdb30 20 73 74 6f 72 65 64 20 6f 6e 20 6f 76 65 72 66   stored on overf
bdb40 6c 6f 77 20 70 61 67 65 73 2e 20 20 54 68 65 20  low pages.  The 
bdb50 70 61 79 6c 6f 61 64 20 66 6f 72 20 61 6e 20 65  payload for an e
bdb60 6e 74 72 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20  ntry.** and the 
bdb70 70 72 65 63 65 64 69 6e 67 20 70 6f 69 6e 74 65  preceding pointe
bdb80 72 20 61 72 65 20 63 6f 6d 62 69 6e 65 64 20 74  r are combined t
bdb90 6f 20 66 6f 72 6d 20 61 20 22 43 65 6c 6c 22 2e  o form a "Cell".
bdba0 20 20 45 61 63 68 20 0a 2a 2a 20 70 61 67 65 20    Each .** page 
bdbb0 68 61 73 20 61 20 73 6d 61 6c 6c 20 68 65 61 64  has a small head
bdbc0 65 72 20 77 68 69 63 68 20 63 6f 6e 74 61 69 6e  er which contain
bdbd0 73 20 74 68 65 20 50 74 72 28 4e 29 20 70 6f 69  s the Ptr(N) poi
bdbe0 6e 74 65 72 20 61 6e 64 20 6f 74 68 65 72 0a 2a  nter and other.*
bdbf0 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 75  * information su
bdc00 63 68 20 61 73 20 74 68 65 20 73 69 7a 65 20 6f  ch as the size o
bdc10 66 20 6b 65 79 20 61 6e 64 20 64 61 74 61 2e 0a  f key and data..
bdc20 2a 2a 0a 2a 2a 20 46 4f 52 4d 41 54 20 44 45 54  **.** FORMAT DET
bdc30 41 49 4c 53 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  AILS.**.** The f
bdc40 69 6c 65 20 69 73 20 64 69 76 69 64 65 64 20 69  ile is divided i
bdc50 6e 74 6f 20 70 61 67 65 73 2e 20 20 54 68 65 20  nto pages.  The 
bdc60 66 69 72 73 74 20 70 61 67 65 20 69 73 20 63 61  first page is ca
bdc70 6c 6c 65 64 20 70 61 67 65 20 31 2c 0a 2a 2a 20  lled page 1,.** 
bdc80 74 68 65 20 73 65 63 6f 6e 64 20 69 73 20 70 61  the second is pa
bdc90 67 65 20 32 2c 20 61 6e 64 20 73 6f 20 66 6f 72  ge 2, and so for
bdca0 74 68 2e 20 20 41 20 70 61 67 65 20 6e 75 6d 62  th.  A page numb
bdcb0 65 72 20 6f 66 20 7a 65 72 6f 20 69 6e 64 69 63  er of zero indic
bdcc0 61 74 65 73 0a 2a 2a 20 22 6e 6f 20 73 75 63 68  ates.** "no such
bdcd0 20 70 61 67 65 22 2e 20 20 54 68 65 20 70 61 67   page".  The pag
bdce0 65 20 73 69 7a 65 20 63 61 6e 20 62 65 20 61 6e  e size can be an
bdcf0 79 74 68 69 6e 67 20 62 65 74 77 65 65 6e 20 35  ything between 5
bdd00 31 32 20 61 6e 64 20 36 35 35 33 36 2e 0a 2a 2a  12 and 65536..**
bdd10 20 45 61 63 68 20 70 61 67 65 20 63 61 6e 20 62   Each page can b
bdd20 65 20 65 69 74 68 65 72 20 61 20 62 74 72 65 65  e either a btree
bdd30 20 70 61 67 65 2c 20 61 20 66 72 65 65 6c 69 73   page, a freelis
bdd40 74 20 70 61 67 65 20 6f 72 20 61 6e 20 6f 76 65  t page or an ove
bdd50 72 66 6c 6f 77 0a 2a 2a 20 70 61 67 65 2e 0a 2a  rflow.** page..*
bdd60 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70  *.** The first p
bdd70 61 67 65 20 69 73 20 61 6c 77 61 79 73 20 61 20  age is always a 
bdd80 62 74 72 65 65 20 70 61 67 65 2e 20 20 54 68 65  btree page.  The
bdd90 20 66 69 72 73 74 20 31 30 30 20 62 79 74 65 73   first 100 bytes
bdda0 20 6f 66 20 74 68 65 20 66 69 72 73 74 0a 2a 2a   of the first.**
bddb0 20 70 61 67 65 20 63 6f 6e 74 61 69 6e 20 61 20   page contain a 
bddc0 73 70 65 63 69 61 6c 20 68 65 61 64 65 72 20 28  special header (
bddd0 74 68 65 20 22 66 69 6c 65 20 68 65 61 64 65 72  the "file header
bdde0 22 29 20 74 68 61 74 20 64 65 73 63 72 69 62 65  ") that describe
bddf0 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 54  s the file..** T
bde00 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 65  he format of the
bde10 20 66 69 6c 65 20 68 65 61 64 65 72 20 69 73 20   file header is 
bde20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
bde30 2a 20 20 20 4f 46 46 53 45 54 20 20 20 53 49 5a  *   OFFSET   SIZ
bde40 45 20 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e  E    DESCRIPTION
bde50 0a 2a 2a 20 20 20 20 20 20 30 20 20 20 20 20 20  .**      0      
bde60 31 36 20 20 20 20 20 48 65 61 64 65 72 20 73 74  16     Header st
bde70 72 69 6e 67 3a 20 22 53 51 4c 69 74 65 20 66 6f  ring: "SQLite fo
bde80 72 6d 61 74 20 33 5c 30 30 30 22 0a 2a 2a 20 20  rmat 3\000".**  
bde90 20 20 20 31 36 20 20 20 20 20 20 20 32 20 20 20     16       2   
bdea0 20 20 50 61 67 65 20 73 69 7a 65 20 69 6e 20 62    Page size in b
bdeb0 79 74 65 73 2e 20 20 0a 2a 2a 20 20 20 20 20 31  ytes.  .**     1
bdec0 38 20 20 20 20 20 20 20 31 20 20 20 20 20 46 69  8       1     Fi
bded0 6c 65 20 66 6f 72 6d 61 74 20 77 72 69 74 65 20  le format write 
bdee0 76 65 72 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 31  version.**     1
bdef0 39 20 20 20 20 20 20 20 31 20 20 20 20 20 46 69  9       1     Fi
bdf00 6c 65 20 66 6f 72 6d 61 74 20 72 65 61 64 20 76  le format read v
bdf10 65 72 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 30  ersion.**     20
bdf20 20 20 20 20 20 20 20 31 20 20 20 20 20 42 79 74         1     Byt
bdf30 65 73 20 6f 66 20 75 6e 75 73 65 64 20 73 70 61  es of unused spa
bdf40 63 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66  ce at the end of
bdf50 20 65 61 63 68 20 70 61 67 65 0a 2a 2a 20 20 20   each page.**   
bdf60 20 20 32 31 20 20 20 20 20 20 20 31 20 20 20 20    21       1    
bdf70 20 4d 61 78 20 65 6d 62 65 64 64 65 64 20 70 61   Max embedded pa
bdf80 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a  yload fraction.*
bdf90 2a 20 20 20 20 20 32 32 20 20 20 20 20 20 20 31  *     22       1
bdfa0 20 20 20 20 20 4d 69 6e 20 65 6d 62 65 64 64 65       Min embedde
bdfb0 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69  d payload fracti
bdfc0 6f 6e 0a 2a 2a 20 20 20 20 20 32 33 20 20 20 20  on.**     23    
bdfd0 20 20 20 31 20 20 20 20 20 4d 69 6e 20 6c 65 61     1     Min lea
bdfe0 66 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69  f payload fracti
bdff0 6f 6e 0a 2a 2a 20 20 20 20 20 32 34 20 20 20 20  on.**     24    
be000 20 20 20 34 20 20 20 20 20 46 69 6c 65 20 63 68     4     File ch
be010 61 6e 67 65 20 63 6f 75 6e 74 65 72 0a 2a 2a 20  ange counter.** 
be020 20 20 20 20 32 38 20 20 20 20 20 20 20 34 20 20      28       4  
be030 20 20 20 52 65 73 65 72 76 65 64 20 66 6f 72 20     Reserved for 
be040 66 75 74 75 72 65 20 75 73 65 0a 2a 2a 20 20 20  future use.**   
be050 20 20 33 32 20 20 20 20 20 20 20 34 20 20 20 20    32       4    
be060 20 46 69 72 73 74 20 66 72 65 65 6c 69 73 74 20   First freelist 
be070 70 61 67 65 0a 2a 2a 20 20 20 20 20 33 36 20 20  page.**     36  
be080 20 20 20 20 20 34 20 20 20 20 20 4e 75 6d 62 65       4     Numbe
be090 72 20 6f 66 20 66 72 65 65 6c 69 73 74 20 70 61  r of freelist pa
be0a0 67 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65 0a  ges in the file.
be0b0 2a 2a 20 20 20 20 20 34 30 20 20 20 20 20 20 36  **     40      6
be0c0 30 20 20 20 20 20 31 35 20 34 2d 62 79 74 65 20  0     15 4-byte 
be0d0 6d 65 74 61 20 76 61 6c 75 65 73 20 70 61 73 73  meta values pass
be0e0 65 64 20 74 6f 20 68 69 67 68 65 72 20 6c 61 79  ed to higher lay
be0f0 65 72 73 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66  ers.**.** All of
be100 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c   the integer val
be110 75 65 73 20 61 72 65 20 62 69 67 2d 65 6e 64 69  ues are big-endi
be120 61 6e 20 28 6d 6f 73 74 20 73 69 67 6e 69 66 69  an (most signifi
be130 63 61 6e 74 20 62 79 74 65 20 66 69 72 73 74 29  cant byte first)
be140 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65  ..**.** The file
be150 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20   change counter 
be160 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77  is incremented w
be170 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  hen the database
be180 20 69 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 54   is changed.** T
be190 68 69 73 20 63 6f 75 6e 74 65 72 20 61 6c 6c 6f  his counter allo
be1a0 77 73 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  ws other process
be1b0 65 73 20 74 6f 20 6b 6e 6f 77 20 77 68 65 6e 20  es to know when 
be1c0 74 68 65 20 66 69 6c 65 20 68 61 73 20 63 68 61  the file has cha
be1d0 6e 67 65 64 0a 2a 2a 20 61 6e 64 20 74 68 75 73  nged.** and thus
be1e0 20 77 68 65 6e 20 74 68 65 79 20 6e 65 65 64 20   when they need 
be1f0 74 6f 20 66 6c 75 73 68 20 74 68 65 69 72 20 63  to flush their c
be200 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ache..**.** The 
be210 6d 61 78 20 65 6d 62 65 64 64 65 64 20 70 61 79  max embedded pay
be220 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 69 73  load fraction is
be230 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74   the amount of t
be240 68 65 20 74 6f 74 61 6c 20 75 73 61 62 6c 65 0a  he total usable.
be250 2a 2a 20 73 70 61 63 65 20 69 6e 20 61 20 70 61  ** space in a pa
be260 67 65 20 74 68 61 74 20 63 61 6e 20 62 65 20 63  ge that can be c
be270 6f 6e 73 75 6d 65 64 20 62 79 20 61 20 73 69 6e  onsumed by a sin
be280 67 6c 65 20 63 65 6c 6c 20 66 6f 72 20 73 74 61  gle cell for sta
be290 6e 64 61 72 64 0a 2a 2a 20 42 2d 74 72 65 65 20  ndard.** B-tree 
be2a0 28 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 29 20 74  (non-LEAFDATA) t
be2b0 61 62 6c 65 73 2e 20 20 41 20 76 61 6c 75 65 20  ables.  A value 
be2c0 6f 66 20 32 35 35 20 6d 65 61 6e 73 20 31 30 30  of 255 means 100
be2d0 25 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 0a  %.  The default.
be2e0 2a 2a 20 69 73 20 74 6f 20 6c 69 6d 69 74 20 74  ** is to limit t
be2f0 68 65 20 6d 61 78 69 6d 75 6d 20 63 65 6c 6c 20  he maximum cell 
be300 73 69 7a 65 20 73 6f 20 74 68 61 74 20 61 74 20  size so that at 
be310 6c 65 61 73 74 20 34 20 63 65 6c 6c 73 20 77 69  least 4 cells wi
be320 6c 6c 20 66 69 74 0a 2a 2a 20 6f 6e 20 6f 6e 65  ll fit.** on one
be330 20 70 61 67 65 2e 20 20 54 68 75 73 20 74 68 65   page.  Thus the
be340 20 64 65 66 61 75 6c 74 20 6d 61 78 20 65 6d 62   default max emb
be350 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72  edded payload fr
be360 61 63 74 69 6f 6e 20 69 73 20 36 34 2e 0a 2a 2a  action is 64..**
be370 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 79 6c 6f  .** If the paylo
be380 61 64 20 66 6f 72 20 61 20 63 65 6c 6c 20 69 73  ad for a cell is
be390 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65   larger than the
be3a0 20 6d 61 78 20 70 61 79 6c 6f 61 64 2c 20 74 68   max payload, th
be3b0 65 6e 20 65 78 74 72 61 0a 2a 2a 20 70 61 79 6c  en extra.** payl
be3c0 6f 61 64 20 69 73 20 73 70 69 6c 6c 65 64 20 74  oad is spilled t
be3d0 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  o overflow pages
be3e0 2e 20 20 4f 6e 63 65 20 61 6e 20 6f 76 65 72 66  .  Once an overf
be3f0 6c 6f 77 20 70 61 67 65 20 69 73 20 61 6c 6c 6f  low page is allo
be400 63 61 74 65 64 2c 0a 2a 2a 20 61 73 20 6d 61 6e  cated,.** as man
be410 79 20 62 79 74 65 73 20 61 73 20 70 6f 73 73 69  y bytes as possi
be420 62 6c 65 20 61 72 65 20 6d 6f 76 65 64 20 69 6e  ble are moved in
be430 74 6f 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  to the overflow 
be440 70 61 67 65 73 20 77 69 74 68 6f 75 74 20 6c 65  pages without le
be450 74 74 69 6e 67 0a 2a 2a 20 74 68 65 20 63 65 6c  tting.** the cel
be460 6c 20 73 69 7a 65 20 64 72 6f 70 20 62 65 6c 6f  l size drop belo
be470 77 20 74 68 65 20 6d 69 6e 20 65 6d 62 65 64 64  w the min embedd
be480 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74  ed payload fract
be490 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d  ion..**.** The m
be4a0 69 6e 20 6c 65 61 66 20 70 61 79 6c 6f 61 64 20  in leaf payload 
be4b0 66 72 61 63 74 69 6f 6e 20 69 73 20 6c 69 6b 65  fraction is like
be4c0 20 74 68 65 20 6d 69 6e 20 65 6d 62 65 64 64 65   the min embedde
be4d0 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69  d payload fracti
be4e0 6f 6e 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61  on.** except tha
be4f0 74 20 69 74 20 61 70 70 6c 69 65 73 20 74 6f 20  t it applies to 
be500 6c 65 61 66 20 6e 6f 64 65 73 20 69 6e 20 61 20  leaf nodes in a 
be510 4c 45 41 46 44 41 54 41 20 74 72 65 65 2e 20 20  LEAFDATA tree.  
be520 54 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 70  The maximum.** p
be530 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20  ayload fraction 
be540 66 6f 72 20 61 20 4c 45 41 46 44 41 54 41 20 74  for a LEAFDATA t
be550 72 65 65 20 69 73 20 61 6c 77 61 79 73 20 31 30  ree is always 10
be560 30 25 20 28 6f 72 20 32 35 35 29 20 61 6e 64 20  0% (or 255) and 
be570 69 74 0a 2a 2a 20 6e 6f 74 20 73 70 65 63 69 66  it.** not specif
be580 69 65 64 20 69 6e 20 74 68 65 20 68 65 61 64 65  ied in the heade
be590 72 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 62 74  r..**.** Each bt
be5a0 72 65 65 20 70 61 67 65 73 20 69 73 20 64 69 76  ree pages is div
be5b0 69 64 65 64 20 69 6e 74 6f 20 74 68 72 65 65 20  ided into three 
be5c0 73 65 63 74 69 6f 6e 73 3a 20 20 54 68 65 20 68  sections:  The h
be5d0 65 61 64 65 72 2c 20 74 68 65 0a 2a 2a 20 63 65  eader, the.** ce
be5e0 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79  ll pointer array
be5f0 2c 20 61 6e 64 20 74 68 65 20 63 65 6c 6c 20 63  , and the cell c
be600 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 20 50 61  ontent area.  Pa
be610 67 65 20 31 20 61 6c 73 6f 20 68 61 73 20 61 20  ge 1 also has a 
be620 31 30 30 2d 62 79 74 65 0a 2a 2a 20 66 69 6c 65  100-byte.** file
be630 20 68 65 61 64 65 72 20 74 68 61 74 20 6f 63 63   header that occ
be640 75 72 73 20 62 65 66 6f 72 65 20 74 68 65 20 70  urs before the p
be650 61 67 65 20 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a  age header..**.*
be660 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d  *      |--------
be670 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20  --------|.**    
be680 20 20 7c 20 66 69 6c 65 20 68 65 61 64 65 72 20    | file header 
be690 20 20 20 7c 20 20 20 31 30 30 20 62 79 74 65 73     |   100 bytes
be6a0 2e 20 20 50 61 67 65 20 31 20 6f 6e 6c 79 2e 0a  .  Page 1 only..
be6b0 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d  **      |-------
be6c0 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20  ---------|.**   
be6d0 20 20 20 7c 20 70 61 67 65 20 68 65 61 64 65 72     | page header
be6e0 20 20 20 20 7c 20 20 20 38 20 62 79 74 65 73 20      |   8 bytes 
be6f0 66 6f 72 20 6c 65 61 76 65 73 2e 20 20 31 32 20  for leaves.  12 
be700 62 79 74 65 73 20 66 6f 72 20 69 6e 74 65 72 69  bytes for interi
be710 6f 72 20 6e 6f 64 65 73 0a 2a 2a 20 20 20 20 20  or nodes.**     
be720 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   |--------------
be730 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 63 65  --|.**      | ce
be740 6c 6c 20 70 6f 69 6e 74 65 72 20 20 20 7c 20 20  ll pointer   |  
be750 20 7c 20 20 32 20 62 79 74 65 73 20 70 65 72 20   |  2 bytes per 
be760 63 65 6c 6c 2e 20 20 53 6f 72 74 65 64 20 6f 72  cell.  Sorted or
be770 64 65 72 2e 0a 2a 2a 20 20 20 20 20 20 7c 20 61  der..**      | a
be780 72 72 61 79 20 20 20 20 20 20 20 20 20 20 7c 20  rray          | 
be790 20 20 7c 20 20 47 72 6f 77 73 20 64 6f 77 6e 77    |  Grows downw
be7a0 61 72 64 0a 2a 2a 20 20 20 20 20 20 7c 20 20 20  ard.**      |   
be7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 20               |  
be7c0 20 76 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d   v.**      |----
be7d0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a  ------------|.**
be7e0 20 20 20 20 20 20 7c 20 75 6e 61 6c 6c 6f 63 61        | unalloca
be7f0 74 65 64 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20  ted    |.**     
be800 20 7c 20 73 70 61 63 65 20 20 20 20 20 20 20 20   | space        
be810 20 20 7c 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d    |.**      |---
be820 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 20 20  -------------|  
be830 20 5e 20 20 47 72 6f 77 73 20 75 70 77 61 72 64   ^  Grows upward
be840 73 0a 2a 2a 20 20 20 20 20 20 7c 20 63 65 6c 6c  s.**      | cell
be850 20 63 6f 6e 74 65 6e 74 20 20 20 7c 20 20 20 7c   content   |   |
be860 20 20 41 72 62 69 74 72 61 72 79 20 6f 72 64 65    Arbitrary orde
be870 72 20 69 6e 74 65 72 73 70 65 72 73 65 64 20 77  r interspersed w
be880 69 74 68 20 66 72 65 65 62 6c 6f 63 6b 73 2e 0a  ith freeblocks..
be890 2a 2a 20 20 20 20 20 20 7c 20 61 72 65 61 20 20  **      | area  
be8a0 20 20 20 20 20 20 20 20 20 7c 20 20 20 7c 20 20           |   |  
be8b0 61 6e 64 20 66 72 65 65 20 73 70 61 63 65 20 66  and free space f
be8c0 72 61 67 6d 65 6e 74 73 2e 0a 2a 2a 20 20 20 20  ragments..**    
be8d0 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    |-------------
be8e0 2d 2d 2d 7c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70  ---|.**.** The p
be8f0 61 67 65 20 68 65 61 64 65 72 73 20 6c 6f 6f 6b  age headers look
be900 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a  s like this:.**.
be910 2a 2a 20 20 20 4f 46 46 53 45 54 20 20 20 53 49  **   OFFSET   SI
be920 5a 45 20 20 20 20 20 44 45 53 43 52 49 50 54 49  ZE     DESCRIPTI
be930 4f 4e 0a 2a 2a 20 20 20 20 20 20 30 20 20 20 20  ON.**      0    
be940 20 20 20 31 20 20 20 20 20 20 46 6c 61 67 73 2e     1      Flags.
be950 20 31 3a 20 69 6e 74 6b 65 79 2c 20 32 3a 20 7a   1: intkey, 2: z
be960 65 72 6f 64 61 74 61 2c 20 34 3a 20 6c 65 61 66  erodata, 4: leaf
be970 64 61 74 61 2c 20 38 3a 20 6c 65 61 66 0a 2a 2a  data, 8: leaf.**
be980 20 20 20 20 20 20 31 20 20 20 20 20 20 20 32 20        1       2 
be990 20 20 20 20 20 62 79 74 65 20 6f 66 66 73 65 74       byte offset
be9a0 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 66 72   to the first fr
be9b0 65 65 62 6c 6f 63 6b 0a 2a 2a 20 20 20 20 20 20  eeblock.**      
be9c0 33 20 20 20 20 20 20 20 32 20 20 20 20 20 20 6e  3       2      n
be9d0 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f  umber of cells o
be9e0 6e 20 74 68 69 73 20 70 61 67 65 0a 2a 2a 20 20  n this page.**  
be9f0 20 20 20 20 35 20 20 20 20 20 20 20 32 20 20 20      5       2   
bea00 20 20 20 66 69 72 73 74 20 62 79 74 65 20 6f 66     first byte of
bea10 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e   the cell conten
bea20 74 20 61 72 65 61 0a 2a 2a 20 20 20 20 20 20 37  t area.**      7
bea30 20 20 20 20 20 20 20 31 20 20 20 20 20 20 6e 75         1      nu
bea40 6d 62 65 72 20 6f 66 20 66 72 61 67 6d 65 6e 74  mber of fragment
bea50 65 64 20 66 72 65 65 20 62 79 74 65 73 0a 2a 2a  ed free bytes.**
bea60 20 20 20 20 20 20 38 20 20 20 20 20 20 20 34 20        8       4 
bea70 20 20 20 20 20 52 69 67 68 74 20 63 68 69 6c 64       Right child
bea80 20 28 74 68 65 20 50 74 72 28 4e 29 20 76 61 6c   (the Ptr(N) val
bea90 75 65 29 2e 20 20 4f 6d 69 74 74 65 64 20 6f 6e  ue).  Omitted on
beaa0 20 6c 65 61 76 65 73 2e 0a 2a 2a 0a 2a 2a 20 54   leaves..**.** T
beab0 68 65 20 66 6c 61 67 73 20 64 65 66 69 6e 65 20  he flags define 
beac0 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68  the format of th
bead0 69 73 20 62 74 72 65 65 20 70 61 67 65 2e 20 20  is btree page.  
beae0 54 68 65 20 6c 65 61 66 20 66 6c 61 67 20 6d 65  The leaf flag me
beaf0 61 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68 69 73  ans that.** this
beb00 20 70 61 67 65 20 68 61 73 20 6e 6f 20 63 68 69   page has no chi
beb10 6c 64 72 65 6e 2e 20 20 54 68 65 20 7a 65 72 6f  ldren.  The zero
beb20 64 61 74 61 20 66 6c 61 67 20 6d 65 61 6e 73 20  data flag means 
beb30 74 68 61 74 20 74 68 69 73 20 70 61 67 65 20 63  that this page c
beb40 61 72 72 69 65 73 0a 2a 2a 20 6f 6e 6c 79 20 6b  arries.** only k
beb50 65 79 73 20 61 6e 64 20 6e 6f 20 64 61 74 61 2e  eys and no data.
beb60 20 20 54 68 65 20 69 6e 74 6b 65 79 20 66 6c 61    The intkey fla
beb70 67 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  g means that the
beb80 20 6b 65 79 20 69 73 20 61 20 69 6e 74 65 67 65   key is a intege
beb90 72 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 73 74  r.** which is st
beba0 6f 72 65 64 20 69 6e 20 74 68 65 20 6b 65 79 20  ored in the key 
bebb0 73 69 7a 65 20 65 6e 74 72 79 20 6f 66 20 74 68  size entry of th
bebc0 65 20 63 65 6c 6c 20 68 65 61 64 65 72 20 72 61  e cell header ra
bebd0 74 68 65 72 20 74 68 61 6e 20 69 6e 0a 2a 2a 20  ther than in.** 
bebe0 74 68 65 20 70 61 79 6c 6f 61 64 20 61 72 65 61  the payload area
bebf0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 65 6c 6c  ..**.** The cell
bec00 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 62   pointer array b
bec10 65 67 69 6e 73 20 6f 6e 20 74 68 65 20 66 69 72  egins on the fir
bec20 73 74 20 62 79 74 65 20 61 66 74 65 72 20 74 68  st byte after th
bec30 65 20 70 61 67 65 20 68 65 61 64 65 72 2e 0a 2a  e page header..*
bec40 2a 20 54 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74  * The cell point
bec50 65 72 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e  er array contain
bec60 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 32  s zero or more 2
bec70 2d 62 79 74 65 20 6e 75 6d 62 65 72 73 20 77 68  -byte numbers wh
bec80 69 63 68 20 61 72 65 0a 2a 2a 20 6f 66 66 73 65  ich are.** offse
bec90 74 73 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69  ts from the begi
beca0 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 70 61 67  nning of the pag
becb0 65 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f  e to the cell co
becc0 6e 74 65 6e 74 20 69 6e 20 74 68 65 20 63 65 6c  ntent in the cel
becd0 6c 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 61 72 65  l.** content are
bece0 61 2e 20 20 54 68 65 20 63 65 6c 6c 20 70 6f 69  a.  The cell poi
becf0 6e 74 65 72 73 20 6f 63 63 75 72 20 69 6e 20 73  nters occur in s
bed00 6f 72 74 65 64 20 6f 72 64 65 72 2e 20 20 54 68  orted order.  Th
bed10 65 20 73 79 73 74 65 6d 20 73 74 72 69 76 65 73  e system strives
bed20 0a 2a 2a 20 74 6f 20 6b 65 65 70 20 66 72 65 65  .** to keep free
bed30 20 73 70 61 63 65 20 61 66 74 65 72 20 74 68 65   space after the
bed40 20 6c 61 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74   last cell point
bed50 65 72 20 73 6f 20 74 68 61 74 20 6e 65 77 20 63  er so that new c
bed60 65 6c 6c 73 20 63 61 6e 0a 2a 2a 20 62 65 20 65  ells can.** be e
bed70 61 73 69 6c 79 20 61 64 64 65 64 20 77 69 74 68  asily added with
bed80 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 64 65  out having to de
bed90 66 72 61 67 6d 65 6e 74 20 74 68 65 20 70 61 67  fragment the pag
beda0 65 2e 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f  e..**.** Cell co
bedb0 6e 74 65 6e 74 20 69 73 20 73 74 6f 72 65 64 20  ntent is stored 
bedc0 61 74 20 74 68 65 20 76 65 72 79 20 65 6e 64 20  at the very end 
bedd0 6f 66 20 74 68 65 20 70 61 67 65 20 61 6e 64 20  of the page and 
bede0 67 72 6f 77 73 20 74 6f 77 61 72 64 20 74 68 65  grows toward the
bedf0 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  .** beginning of
bee00 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a   the page..**.**
bee10 20 55 6e 75 73 65 64 20 73 70 61 63 65 20 77 69   Unused space wi
bee20 74 68 69 6e 20 74 68 65 20 63 65 6c 6c 20 63 6f  thin the cell co
bee30 6e 74 65 6e 74 20 61 72 65 61 20 69 73 20 63 6f  ntent area is co
bee40 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20 61 20 6c  llected into a l
bee50 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 0a 2a 2a  inked list of.**
bee60 20 66 72 65 65 62 6c 6f 63 6b 73 2e 20 20 45 61   freeblocks.  Ea
bee70 63 68 20 66 72 65 65 62 6c 6f 63 6b 20 69 73 20  ch freeblock is 
bee80 61 74 20 6c 65 61 73 74 20 34 20 62 79 74 65 73  at least 4 bytes
bee90 20 69 6e 20 73 69 7a 65 2e 20 20 54 68 65 20 62   in size.  The b
beea0 79 74 65 20 6f 66 66 73 65 74 0a 2a 2a 20 74 6f  yte offset.** to
beeb0 20 74 68 65 20 66 69 72 73 74 20 66 72 65 65 62   the first freeb
beec0 6c 6f 63 6b 20 69 73 20 67 69 76 65 6e 20 69 6e  lock is given in
beed0 20 74 68 65 20 68 65 61 64 65 72 2e 20 20 46 72   the header.  Fr
beee0 65 65 62 6c 6f 63 6b 73 20 6f 63 63 75 72 20 69  eeblocks occur i
beef0 6e 0a 2a 2a 20 69 6e 63 72 65 61 73 69 6e 67 20  n.** increasing 
bef00 6f 72 64 65 72 2e 20 20 42 65 63 61 75 73 65 20  order.  Because 
bef10 61 20 66 72 65 65 62 6c 6f 63 6b 20 6d 75 73 74  a freeblock must
bef20 20 62 65 20 61 74 20 6c 65 61 73 74 20 34 20 62   be at least 4 b
bef30 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 0a 2a 2a  ytes in size,.**
bef40 20 61 6e 79 20 67 72 6f 75 70 20 6f 66 20 33 20   any group of 3 
bef50 6f 72 20 66 65 77 65 72 20 75 6e 75 73 65 64 20  or fewer unused 
bef60 62 79 74 65 73 20 69 6e 20 74 68 65 20 63 65 6c  bytes in the cel
bef70 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 63  l content area c
bef80 61 6e 6e 6f 74 0a 2a 2a 20 65 78 69 73 74 20 6f  annot.** exist o
bef90 6e 20 74 68 65 20 66 72 65 65 62 6c 6f 63 6b 20  n the freeblock 
befa0 63 68 61 69 6e 2e 20 20 41 20 67 72 6f 75 70 20  chain.  A group 
befb0 6f 66 20 33 20 6f 72 20 66 65 77 65 72 20 66 72  of 3 or fewer fr
befc0 65 65 20 62 79 74 65 73 20 69 73 20 63 61 6c 6c  ee bytes is call
befd0 65 64 0a 2a 2a 20 61 20 66 72 61 67 6d 65 6e 74  ed.** a fragment
befe0 2e 20 20 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d  .  The total num
beff0 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
bf000 61 6c 6c 20 66 72 61 67 6d 65 6e 74 73 20 69 73  all fragments is
bf010 20 72 65 63 6f 72 64 65 64 2e 0a 2a 2a 20 69 6e   recorded..** in
bf020 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72   the page header
bf030 20 61 74 20 6f 66 66 73 65 74 20 37 2e 0a 2a 2a   at offset 7..**
bf040 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20 20 44  .**    SIZE    D
bf050 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20  ESCRIPTION.**   
bf060 20 20 20 32 20 20 20 20 20 42 79 74 65 20 6f 66     2     Byte of
bf070 66 73 65 74 20 6f 66 20 74 68 65 20 6e 65 78 74  fset of the next
bf080 20 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a 20 20 20   freeblock.**   
bf090 20 20 20 32 20 20 20 20 20 42 79 74 65 73 20 69     2     Bytes i
bf0a0 6e 20 74 68 69 73 20 66 72 65 65 62 6c 6f 63 6b  n this freeblock
bf0b0 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 73 20 61 72 65  .**.** Cells are
bf0c0 20 6f 66 20 76 61 72 69 61 62 6c 65 20 6c 65 6e   of variable len
bf0d0 67 74 68 2e 20 20 43 65 6c 6c 73 20 61 72 65 20  gth.  Cells are 
bf0e0 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 65  stored in the ce
bf0f0 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20  ll content area 
bf100 61 74 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66  at.** the end of
bf110 20 74 68 65 20 70 61 67 65 2e 20 20 50 6f 69 6e   the page.  Poin
bf120 74 65 72 73 20 74 6f 20 74 68 65 20 63 65 6c 6c  ters to the cell
bf130 73 20 61 72 65 20 69 6e 20 74 68 65 20 63 65 6c  s are in the cel
bf140 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 0a  l pointer array.
bf150 2a 2a 20 74 68 61 74 20 69 6d 6d 65 64 69 61 74  ** that immediat
bf160 65 6c 79 20 66 6f 6c 6c 6f 77 73 20 74 68 65 20  ely follows the 
bf170 70 61 67 65 20 68 65 61 64 65 72 2e 20 20 43 65  page header.  Ce
bf180 6c 6c 73 20 69 73 20 6e 6f 74 20 6e 65 63 65 73  lls is not neces
bf190 73 61 72 69 6c 79 0a 2a 2a 20 63 6f 6e 74 69 67  sarily.** contig
bf1a0 75 6f 75 73 20 6f 72 20 69 6e 20 6f 72 64 65 72  uous or in order
bf1b0 2c 20 62 75 74 20 63 65 6c 6c 20 70 6f 69 6e 74  , but cell point
bf1c0 65 72 73 20 61 72 65 20 63 6f 6e 74 69 67 75 6f  ers are contiguo
bf1d0 75 73 20 61 6e 64 20 69 6e 20 6f 72 64 65 72 2e  us and in order.
bf1e0 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f 6e 74  .**.** Cell cont
bf1f0 65 6e 74 20 6d 61 6b 65 73 20 75 73 65 20 6f 66  ent makes use of
bf200 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68   variable length
bf210 20 69 6e 74 65 67 65 72 73 2e 20 20 41 20 76 61   integers.  A va
bf220 72 69 61 62 6c 65 0a 2a 2a 20 6c 65 6e 67 74 68  riable.** length
bf230 20 69 6e 74 65 67 65 72 20 69 73 20 31 20 74 6f   integer is 1 to
bf240 20 39 20 62 79 74 65 73 20 77 68 65 72 65 20 74   9 bytes where t
bf250 68 65 20 6c 6f 77 65 72 20 37 20 62 69 74 73 20  he lower 7 bits 
bf260 6f 66 20 65 61 63 68 20 0a 2a 2a 20 62 79 74 65  of each .** byte
bf270 20 61 72 65 20 75 73 65 64 2e 20 20 54 68 65 20   are used.  The 
bf280 69 6e 74 65 67 65 72 20 63 6f 6e 73 69 73 74 73  integer consists
bf290 20 6f 66 20 61 6c 6c 20 62 79 74 65 73 20 74 68   of all bytes th
bf2a0 61 74 20 68 61 76 65 20 62 69 74 20 38 20 73 65  at have bit 8 se
bf2b0 74 20 61 6e 64 0a 2a 2a 20 74 68 65 20 66 69 72  t and.** the fir
bf2c0 73 74 20 62 79 74 65 20 77 69 74 68 20 62 69 74  st byte with bit
bf2d0 20 38 20 63 6c 65 61 72 2e 20 20 54 68 65 20 6d   8 clear.  The m
bf2e0 6f 73 74 20 73 69 67 6e 69 66 69 63 61 6e 74 20  ost significant 
bf2f0 62 79 74 65 20 6f 66 20 74 68 65 20 69 6e 74 65  byte of the inte
bf300 67 65 72 0a 2a 2a 20 61 70 70 65 61 72 73 20 66  ger.** appears f
bf310 69 72 73 74 2e 20 20 41 20 76 61 72 69 61 62 6c  irst.  A variabl
bf320 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72  e-length integer
bf330 20 6d 61 79 20 6e 6f 74 20 62 65 20 6d 6f 72 65   may not be more
bf340 20 74 68 61 6e 20 39 20 62 79 74 65 73 20 6c 6f   than 9 bytes lo
bf350 6e 67 2e 0a 2a 2a 20 41 73 20 61 20 73 70 65 63  ng..** As a spec
bf360 69 61 6c 20 63 61 73 65 2c 20 61 6c 6c 20 38 20  ial case, all 8 
bf370 62 79 74 65 73 20 6f 66 20 74 68 65 20 39 74 68  bytes of the 9th
bf380 20 62 79 74 65 20 61 72 65 20 75 73 65 64 20 61   byte are used a
bf390 73 20 64 61 74 61 2e 20 20 54 68 69 73 0a 2a 2a  s data.  This.**
bf3a0 20 61 6c 6c 6f 77 73 20 61 20 36 34 2d 62 69 74   allows a 64-bit
bf3b0 20 69 6e 74 65 67 65 72 20 74 6f 20 62 65 20 65   integer to be e
bf3c0 6e 63 6f 64 65 64 20 69 6e 20 39 20 62 79 74 65  ncoded in 9 byte
bf3d0 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30 78 30 30  s..**.**    0x00
bf3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bf3f0 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30        becomes  0
bf400 78 30 30 30 30 30 30 30 30 0a 2a 2a 20 20 20 20  x00000000.**    
bf410 30 78 37 66 20 20 20 20 20 20 20 20 20 20 20 20  0x7f            
bf420 20 20 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65            become
bf430 73 20 20 30 78 30 30 30 30 30 30 37 66 0a 2a 2a  s  0x0000007f.**
bf440 20 20 20 20 30 78 38 31 20 30 78 30 30 20 20 20      0x81 0x00   
bf450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 65                be
bf460 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30 30 38  comes  0x0000008
bf470 30 0a 2a 2a 20 20 20 20 30 78 38 32 20 30 78 30  0.**    0x82 0x0
bf480 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0               
bf490 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30    becomes  0x000
bf4a0 30 30 31 30 30 0a 2a 2a 20 20 20 20 30 78 38 30  00100.**    0x80
bf4b0 20 30 78 37 66 20 20 20 20 20 20 20 20 20 20 20   0x7f           
bf4c0 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30        becomes  0
bf4d0 78 30 30 30 30 30 30 37 66 0a 2a 2a 20 20 20 20  x0000007f.**    
bf4e0 30 78 38 61 20 30 78 39 31 20 30 78 64 31 20 30  0x8a 0x91 0xd1 0
bf4f0 78 61 63 20 30 78 37 38 20 20 62 65 63 6f 6d 65  xac 0x78  become
bf500 73 20 20 30 78 31 32 33 34 35 36 37 38 0a 2a 2a  s  0x12345678.**
bf510 20 20 20 20 30 78 38 31 20 30 78 38 31 20 30 78      0x81 0x81 0x
bf520 38 31 20 30 78 38 31 20 30 78 30 31 20 20 62 65  81 0x81 0x01  be
bf530 63 6f 6d 65 73 20 20 30 78 31 30 32 30 34 30 38  comes  0x1020408
bf540 31 0a 2a 2a 0a 2a 2a 20 56 61 72 69 61 62 6c 65  1.**.** Variable
bf550 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73   length integers
bf560 20 61 72 65 20 75 73 65 64 20 66 6f 72 20 72 6f   are used for ro
bf570 77 69 64 73 20 61 6e 64 20 74 6f 20 68 6f 6c 64  wids and to hold
bf580 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a   the number of.*
bf590 2a 20 62 79 74 65 73 20 6f 66 20 6b 65 79 20 61  * bytes of key a
bf5a0 6e 64 20 64 61 74 61 20 69 6e 20 61 20 62 74 72  nd data in a btr
bf5b0 65 65 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54  ee cell..**.** T
bf5c0 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20  he content of a 
bf5d0 63 65 6c 6c 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20  cell looks like 
bf5e0 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 53  this:.**.**    S
bf5f0 49 5a 45 20 20 20 20 44 45 53 43 52 49 50 54 49  IZE    DESCRIPTI
bf600 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20  ON.**      4    
bf610 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20   Page number of 
bf620 74 68 65 20 6c 65 66 74 20 63 68 69 6c 64 2e 20  the left child. 
bf630 4f 6d 69 74 74 65 64 20 69 66 20 6c 65 61 66 20  Omitted if leaf 
bf640 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20  flag is set..** 
bf650 20 20 20 20 76 61 72 20 20 20 20 4e 75 6d 62 65      var    Numbe
bf660 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61  r of bytes of da
bf670 74 61 2e 20 4f 6d 69 74 74 65 64 20 69 66 20 74  ta. Omitted if t
bf680 68 65 20 7a 65 72 6f 64 61 74 61 20 66 6c 61 67  he zerodata flag
bf690 20 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20   is set..**     
bf6a0 76 61 72 20 20 20 20 4e 75 6d 62 65 72 20 6f 66  var    Number of
bf6b0 20 62 79 74 65 73 20 6f 66 20 6b 65 79 2e 20 4f   bytes of key. O
bf6c0 72 20 74 68 65 20 6b 65 79 20 69 74 73 65 6c 66  r the key itself
bf6d0 20 69 66 20 69 6e 74 6b 65 79 20 66 6c 61 67 20   if intkey flag 
bf6e0 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20 20  is set..**      
bf6f0 2a 20 20 20 20 20 50 61 79 6c 6f 61 64 0a 2a 2a  *     Payload.**
bf700 20 20 20 20 20 20 34 20 20 20 20 20 46 69 72 73        4     Firs
bf710 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 6f 76  t page of the ov
bf720 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20 20 4f  erflow chain.  O
bf730 6d 69 74 74 65 64 20 69 66 20 6e 6f 20 6f 76 65  mitted if no ove
bf740 72 66 6c 6f 77 0a 2a 2a 0a 2a 2a 20 4f 76 65 72  rflow.**.** Over
bf750 66 6c 6f 77 20 70 61 67 65 73 20 66 6f 72 6d 20  flow pages form 
bf760 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 20 20  a linked list.  
bf770 45 61 63 68 20 70 61 67 65 20 65 78 63 65 70 74  Each page except
bf780 20 74 68 65 20 6c 61 73 74 20 69 73 20 63 6f 6d   the last is com
bf790 70 6c 65 74 65 6c 79 0a 2a 2a 20 66 69 6c 6c 65  pletely.** fille
bf7a0 64 20 77 69 74 68 20 64 61 74 61 20 28 70 61 67  d with data (pag
bf7b0 65 73 69 7a 65 20 2d 20 34 20 62 79 74 65 73 29  esize - 4 bytes)
bf7c0 2e 20 20 54 68 65 20 6c 61 73 74 20 70 61 67 65  .  The last page
bf7d0 20 63 61 6e 20 68 61 76 65 20 61 73 20 6c 69 74   can have as lit
bf7e0 74 6c 65 0a 2a 2a 20 61 73 20 31 20 62 79 74 65  tle.** as 1 byte
bf7f0 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20   of data..**.** 
bf800 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52     SIZE    DESCR
bf810 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 34  IPTION.**      4
bf820 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62 65 72       Page number
bf830 20 6f 66 20 6e 65 78 74 20 6f 76 65 72 66 6c 6f   of next overflo
bf840 77 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 2a  w page.**      *
bf850 20 20 20 20 20 44 61 74 61 0a 2a 2a 0a 2a 2a 20       Data.**.** 
bf860 46 72 65 65 6c 69 73 74 20 70 61 67 65 73 20 63  Freelist pages c
bf870 6f 6d 65 20 69 6e 20 74 77 6f 20 73 75 62 74 79  ome in two subty
bf880 70 65 73 3a 20 74 72 75 6e 6b 20 70 61 67 65 73  pes: trunk pages
bf890 20 61 6e 64 20 6c 65 61 66 20 70 61 67 65 73 2e   and leaf pages.
bf8a0 20 20 54 68 65 0a 2a 2a 20 66 69 6c 65 20 68 65    The.** file he
bf8b0 61 64 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 74  ader points to t
bf8c0 68 65 20 66 69 72 73 74 20 69 6e 20 61 20 6c 69  he first in a li
bf8d0 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 72 75  nked list of tru
bf8e0 6e 6b 20 70 61 67 65 2e 20 20 45 61 63 68 20 74  nk page.  Each t
bf8f0 72 75 6e 6b 0a 2a 2a 20 70 61 67 65 20 70 6f 69  runk.** page poi
bf900 6e 74 73 20 74 6f 20 6d 75 6c 74 69 70 6c 65 20  nts to multiple 
bf910 6c 65 61 66 20 70 61 67 65 73 2e 20 20 54 68 65  leaf pages.  The
bf920 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 6c 65   content of a le
bf930 61 66 20 70 61 67 65 20 69 73 0a 2a 2a 20 75 6e  af page is.** un
bf940 73 70 65 63 69 66 69 65 64 2e 20 20 41 20 74 72  specified.  A tr
bf950 75 6e 6b 20 70 61 67 65 20 6c 6f 6f 6b 73 20 6c  unk page looks l
bf960 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20  ike this:.**.** 
bf970 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52     SIZE    DESCR
bf980 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 34  IPTION.**      4
bf990 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62 65 72       Page number
bf9a0 20 6f 66 20 6e 65 78 74 20 74 72 75 6e 6b 20 70   of next trunk p
bf9b0 61 67 65 0a 2a 2a 20 20 20 20 20 20 34 20 20 20  age.**      4   
bf9c0 20 20 4e 75 6d 62 65 72 20 6f 66 20 6c 65 61 66    Number of leaf
bf9d0 20 70 6f 69 6e 74 65 72 73 20 6f 6e 20 74 68 69   pointers on thi
bf9e0 73 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 2a  s page.**      *
bf9f0 20 20 20 20 20 7a 65 72 6f 20 6f 72 20 6d 6f 72       zero or mor
bfa00 65 20 70 61 67 65 73 20 6e 75 6d 62 65 72 73 20  e pages numbers 
bfa10 6f 66 20 6c 65 61 76 65 73 0a 2a 2f 0a 0a 2f 2a  of leaves.*/../*
bfa20 20 52 6f 75 6e 64 20 75 70 20 61 20 6e 75 6d 62   Round up a numb
bfa30 65 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 6c  er to the next l
bfa40 61 72 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f  arger multiple o
bfa50 66 20 38 2e 20 20 54 68 69 73 20 69 73 20 75 73  f 8.  This is us
bfa60 65 64 0a 2a 2a 20 74 6f 20 66 6f 72 63 65 20 38  ed.** to force 8
bfa70 2d 62 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20  -byte alignment 
bfa80 6f 6e 20 36 34 2d 62 69 74 20 61 72 63 68 69 74  on 64-bit archit
bfa90 65 63 74 75 72 65 73 2e 0a 2a 2f 0a 23 64 65 66  ectures..*/.#def
bfaa0 69 6e 65 20 52 4f 55 4e 44 38 28 78 29 20 20 20  ine ROUND8(x)   
bfab0 28 28 78 2b 37 29 26 7e 37 29 0a 0a 0a 2f 2a 20  ((x+7)&~7).../* 
bfac0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61  The following va
bfad0 6c 75 65 20 69 73 20 74 68 65 20 6d 61 78 69 6d  lue is the maxim
bfae0 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 61 73 73  um cell size ass
bfaf0 75 6d 69 6e 67 20 61 20 6d 61 78 69 6d 75 6d 20  uming a maximum 
bfb00 70 61 67 65 0a 2a 2a 20 73 69 7a 65 20 67 69 76  page.** size giv
bfb10 65 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 64 65 66  e above..*/.#def
bfb20 69 6e 65 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45  ine MX_CELL_SIZE
bfb30 28 70 42 74 29 20 20 28 70 42 74 2d 3e 70 61 67  (pBt)  (pBt->pag
bfb40 65 53 69 7a 65 2d 38 29 0a 0a 2f 2a 20 54 68 65  eSize-8)../* The
bfb50 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   maximum number 
bfb60 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 61 20 73 69  of cells on a si
bfb70 6e 67 6c 65 20 70 61 67 65 20 6f 66 20 74 68 65  ngle page of the
bfb80 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69 73   database.  This
bfb90 0a 2a 2a 20 61 73 73 75 6d 65 73 20 61 20 6d 69  .** assumes a mi
bfba0 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20  nimum cell size 
bfbb0 6f 66 20 33 20 62 79 74 65 73 2e 20 20 53 75 63  of 3 bytes.  Suc
bfbc0 68 20 73 6d 61 6c 6c 20 63 65 6c 6c 73 20 77 69  h small cells wi
bfbd0 6c 6c 20 62 65 0a 2a 2a 20 65 78 63 65 65 64 69  ll be.** exceedi
bfbe0 6e 67 6c 79 20 72 61 72 65 2c 20 62 75 74 20 74  ngly rare, but t
bfbf0 68 65 79 20 61 72 65 20 70 6f 73 73 69 62 6c 65  hey are possible
bfc00 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f  ..*/.#define MX_
bfc10 43 45 4c 4c 28 70 42 74 29 20 28 28 70 42 74 2d  CELL(pBt) ((pBt-
bfc20 3e 70 61 67 65 53 69 7a 65 2d 38 29 2f 33 29 0a  >pageSize-8)/3).
bfc30 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c  ./* Forward decl
bfc40 61 72 61 74 69 6f 6e 73 20 2a 2f 0a 74 79 70 65  arations */.type
bfc50 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d 50 61  def struct MemPa
bfc60 67 65 20 4d 65 6d 50 61 67 65 3b 0a 74 79 70 65  ge MemPage;.type
bfc70 64 65 66 20 73 74 72 75 63 74 20 42 74 4c 6f 63  def struct BtLoc
bfc80 6b 20 42 74 4c 6f 63 6b 3b 0a 0a 2f 2a 0a 2a 2a  k BtLock;../*.**
bfc90 20 54 68 69 73 20 69 73 20 61 20 6d 61 67 69 63   This is a magic
bfca0 20 73 74 72 69 6e 67 20 74 68 61 74 20 61 70 70   string that app
bfcb0 65 61 72 73 20 61 74 20 74 68 65 20 62 65 67 69  ears at the begi
bfcc0 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72 79 0a 2a  nning of every.*
bfcd0 2a 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73  * SQLite databas
bfce0 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 69 64  e in order to id
bfcf0 65 6e 74 69 66 79 20 74 68 65 20 66 69 6c 65 20  entify the file 
bfd00 61 73 20 61 20 72 65 61 6c 20 64 61 74 61 62 61  as a real databa
bfd10 73 65 2e 0a 2a 2a 0a 2a 2a 20 59 6f 75 20 63 61  se..**.** You ca
bfd20 6e 20 63 68 61 6e 67 65 20 74 68 69 73 20 76 61  n change this va
bfd30 6c 75 65 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74  lue at compile-t
bfd40 69 6d 65 20 62 79 20 73 70 65 63 69 66 79 69 6e  ime by specifyin
bfd50 67 20 61 0a 2a 2a 20 2d 44 53 51 4c 49 54 45 5f  g a.** -DSQLITE_
bfd60 46 49 4c 45 5f 48 45 41 44 45 52 3d 22 2e 2e 2e  FILE_HEADER="...
bfd70 22 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65  " on the compile
bfd80 72 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 2e 20  r command-line. 
bfd90 20 54 68 65 0a 2a 2a 20 68 65 61 64 65 72 20 6d   The.** header m
bfda0 75 73 74 20 62 65 20 65 78 61 63 74 6c 79 20 31  ust be exactly 1
bfdb0 36 20 62 79 74 65 73 20 69 6e 63 6c 75 64 69 6e  6 bytes includin
bfdc0 67 20 74 68 65 20 7a 65 72 6f 2d 74 65 72 6d 69  g the zero-termi
bfdd0 6e 61 74 6f 72 20 73 6f 0a 2a 2a 20 74 68 65 20  nator so.** the 
bfde0 73 74 72 69 6e 67 20 69 74 73 65 6c 66 20 73 68  string itself sh
bfdf0 6f 75 6c 64 20 62 65 20 31 35 20 63 68 61 72 61  ould be 15 chara
bfe00 63 74 65 72 73 20 6c 6f 6e 67 2e 20 20 49 66 20  cters long.  If 
bfe10 79 6f 75 20 63 68 61 6e 67 65 0a 2a 2a 20 74 68  you change.** th
bfe20 65 20 68 65 61 64 65 72 2c 20 74 68 65 6e 20 79  e header, then y
bfe30 6f 75 72 20 63 75 73 74 6f 6d 20 6c 69 62 72 61  our custom libra
bfe40 72 79 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61  ry will not be a
bfe50 62 6c 65 20 74 6f 20 72 65 61 64 20 0a 2a 2a 20  ble to read .** 
bfe60 64 61 74 61 62 61 73 65 73 20 67 65 6e 65 72 61  databases genera
bfe70 74 65 64 20 62 79 20 74 68 65 20 73 74 61 6e 64  ted by the stand
bfe80 61 72 64 20 74 6f 6f 6c 73 20 61 6e 64 20 74 68  ard tools and th
bfe90 65 20 73 74 61 6e 64 61 72 64 20 74 6f 6f 6c 73  e standard tools
bfea0 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20  .** will not be 
bfeb0 61 62 6c 65 20 74 6f 20 72 65 61 64 20 64 61 74  able to read dat
bfec0 61 62 61 73 65 73 20 63 72 65 61 74 65 64 20 62  abases created b
bfed0 79 20 79 6f 75 72 20 63 75 73 74 6f 6d 20 6c 69  y your custom li
bfee0 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 6e 64 65  brary..*/.#ifnde
bfef0 66 20 53 51 4c 49 54 45 5f 46 49 4c 45 5f 48 45  f SQLITE_FILE_HE
bff00 41 44 45 52 20 2f 2a 20 31 32 33 34 35 36 37 38  ADER /* 12345678
bff10 39 20 31 32 33 34 35 36 20 2a 2f 0a 23 20 20 64  9 123456 */.#  d
bff20 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 49 4c  efine SQLITE_FIL
bff30 45 5f 48 45 41 44 45 52 20 22 53 51 4c 69 74 65  E_HEADER "SQLite
bff40 20 66 6f 72 6d 61 74 20 33 22 0a 23 65 6e 64 69   format 3".#endi
bff50 66 0a 0a 2f 2a 0a 2a 2a 20 50 61 67 65 20 74 79  f../*.** Page ty
bff60 70 65 20 66 6c 61 67 73 2e 20 20 41 6e 20 4f 52  pe flags.  An OR
bff70 65 64 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f  ed combination o
bff80 66 20 74 68 65 73 65 20 66 6c 61 67 73 20 61 70  f these flags ap
bff90 70 65 61 72 20 61 73 20 74 68 65 0a 2a 2a 20 66  pear as the.** f
bffa0 69 72 73 74 20 62 79 74 65 20 6f 66 20 65 76 65  irst byte of eve
bffb0 72 79 20 42 54 72 65 65 20 70 61 67 65 2e 0a 2a  ry BTree page..*
bffc0 2f 0a 23 64 65 66 69 6e 65 20 50 54 46 5f 49 4e  /.#define PTF_IN
bffd0 54 4b 45 59 20 20 20 20 30 78 30 31 0a 23 64 65  TKEY    0x01.#de
bffe0 66 69 6e 65 20 50 54 46 5f 5a 45 52 4f 44 41 54  fine PTF_ZERODAT
bfff0 41 20 20 30 78 30 32 0a 23 64 65 66 69 6e 65 20  A  0x02.#define 
c0000 50 54 46 5f 4c 45 41 46 44 41 54 41 20 20 30 78  PTF_LEAFDATA  0x
c0010 30 34 0a 23 64 65 66 69 6e 65 20 50 54 46 5f 4c  04.#define PTF_L
c0020 45 41 46 20 20 20 20 20 20 30 78 30 38 0a 0a 2f  EAF      0x08../
c0030 2a 0a 2a 2a 20 41 73 20 65 61 63 68 20 70 61 67  *.** As each pag
c0040 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 73  e of the file is
c0050 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20 6d 65 6d   loaded into mem
c0060 6f 72 79 2c 20 61 6e 20 69 6e 73 74 61 6e 63 65  ory, an instance
c0070 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
c0080 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 69  g.** structure i
c0090 73 20 61 70 70 65 6e 64 65 64 20 61 6e 64 20 69  s appended and i
c00a0 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 7a 65  nitialized to ze
c00b0 72 6f 2e 20 20 54 68 69 73 20 73 74 72 75 63 74  ro.  This struct
c00c0 75 72 65 20 73 74 6f 72 65 73 0a 2a 2a 20 69 6e  ure stores.** in
c00d0 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
c00e0 74 68 65 20 70 61 67 65 20 74 68 61 74 20 69 73  the page that is
c00f0 20 64 65 63 6f 64 65 64 20 66 72 6f 6d 20 74 68   decoded from th
c0100 65 20 72 61 77 20 66 69 6c 65 20 70 61 67 65 2e  e raw file page.
c0110 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 50 61 72 65  .**.** The pPare
c0120 6e 74 20 66 69 65 6c 64 20 70 6f 69 6e 74 73 20  nt field points 
c0130 62 61 63 6b 20 74 6f 20 74 68 65 20 70 61 72 65  back to the pare
c0140 6e 74 20 70 61 67 65 2e 20 20 54 68 69 73 20 61  nt page.  This a
c0150 6c 6c 6f 77 73 20 75 73 20 74 6f 0a 2a 2a 20 77  llows us to.** w
c0160 61 6c 6b 20 75 70 20 74 68 65 20 42 54 72 65 65  alk up the BTree
c0170 20 66 72 6f 6d 20 61 6e 79 20 6c 65 61 66 20 74   from any leaf t
c0180 6f 20 74 68 65 20 72 6f 6f 74 2e 20 20 43 61 72  o the root.  Car
c0190 65 20 6d 75 73 74 20 62 65 20 74 61 6b 65 6e 20  e must be taken 
c01a0 74 6f 0a 2a 2a 20 75 6e 72 65 66 28 29 20 74 68  to.** unref() th
c01b0 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 70 6f  e parent page po
c01c0 69 6e 74 65 72 20 77 68 65 6e 20 74 68 69 73 20  inter when this 
c01d0 70 61 67 65 20 69 73 20 6e 6f 20 6c 6f 6e 67 65  page is no longe
c01e0 72 20 72 65 66 65 72 65 6e 63 65 64 2e 0a 2a 2a  r referenced..**
c01f0 20 54 68 65 20 70 61 67 65 44 65 73 74 72 75 63   The pageDestruc
c0200 74 6f 72 28 29 20 72 6f 75 74 69 6e 65 20 68 61  tor() routine ha
c0210 6e 64 6c 65 73 20 74 68 61 74 20 63 68 6f 72 65  ndles that chore
c0220 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 50  ..*/.struct MemP
c0230 61 67 65 20 7b 0a 20 20 75 38 20 69 73 49 6e 69  age {.  u8 isIni
c0240 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t;           /* 
c0250 54 72 75 65 20 69 66 20 70 72 65 76 69 6f 75 73  True if previous
c0260 6c 79 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20  ly initialized. 
c0270 4d 55 53 54 20 42 45 20 46 49 52 53 54 21 20 2a  MUST BE FIRST! *
c0280 2f 0a 20 20 75 38 20 69 64 78 53 68 69 66 74 3b  /.  u8 idxShift;
c0290 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
c02a0 20 69 66 20 43 65 6c 6c 20 69 6e 64 69 63 65 73   if Cell indices
c02b0 20 68 61 76 65 20 63 68 61 6e 67 65 64 20 2a 2f   have changed */
c02c0 0a 20 20 75 38 20 6e 4f 76 65 72 66 6c 6f 77 3b  .  u8 nOverflow;
c02d0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
c02e0 72 20 6f 66 20 6f 76 65 72 66 6c 6f 77 20 63 65  r of overflow ce
c02f0 6c 6c 20 62 6f 64 69 65 73 20 69 6e 20 61 43 65  ll bodies in aCe
c0300 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 69 6e 74  ll[] */.  u8 int
c0310 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 2f  Key;           /
c0320 2a 20 54 72 75 65 20 69 66 20 69 6e 74 6b 65 79  * True if intkey
c0330 20 66 6c 61 67 20 69 73 20 73 65 74 20 2a 2f 0a   flag is set */.
c0340 20 20 75 38 20 6c 65 61 66 3b 20 20 20 20 20 20    u8 leaf;      
c0350 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
c0360 66 20 6c 65 61 66 20 66 6c 61 67 20 69 73 20 73  f leaf flag is s
c0370 65 74 20 2a 2f 0a 20 20 75 38 20 7a 65 72 6f 44  et */.  u8 zeroD
c0380 61 74 61 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ata;         /* 
c0390 54 72 75 65 20 69 66 20 74 61 62 6c 65 20 73 74  True if table st
c03a0 6f 72 65 73 20 6b 65 79 73 20 6f 6e 6c 79 20 2a  ores keys only *
c03b0 2f 0a 20 20 75 38 20 6c 65 61 66 44 61 74 61 3b  /.  u8 leafData;
c03c0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
c03d0 20 69 66 20 74 61 62 6c 65 73 20 73 74 6f 72 65   if tables store
c03e0 73 20 64 61 74 61 20 6f 6e 20 6c 65 61 76 65 73  s data on leaves
c03f0 20 6f 6e 6c 79 20 2a 2f 0a 20 20 75 38 20 68 61   only */.  u8 ha
c0400 73 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20  sData;          
c0410 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20  /* True if this 
c0420 70 61 67 65 20 73 74 6f 72 65 73 20 64 61 74 61  page stores data
c0430 20 2a 2f 0a 20 20 75 38 20 68 64 72 4f 66 66 73   */.  u8 hdrOffs
c0440 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 31 30  et;        /* 10
c0450 30 20 66 6f 72 20 70 61 67 65 20 31 2e 20 20 30  0 for page 1.  0
c0460 20 6f 74 68 65 72 77 69 73 65 20 2a 2f 0a 20 20   otherwise */.  
c0470 75 38 20 63 68 69 6c 64 50 74 72 53 69 7a 65 3b  u8 childPtrSize;
c0480 20 20 20 20 20 2f 2a 20 30 20 69 66 20 6c 65 61       /* 0 if lea
c0490 66 3d 3d 31 2e 20 20 34 20 69 66 20 6c 65 61 66  f==1.  4 if leaf
c04a0 3d 3d 30 20 2a 2f 0a 20 20 75 31 36 20 6d 61 78  ==0 */.  u16 max
c04b0 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20 20 2f 2a  Local;        /*
c04c0 20 43 6f 70 79 20 6f 66 20 42 74 72 65 65 2e 6d   Copy of Btree.m
c04d0 61 78 4c 6f 63 61 6c 20 6f 72 20 42 74 72 65 65  axLocal or Btree
c04e0 2e 6d 61 78 4c 65 61 66 20 2a 2f 0a 20 20 75 31  .maxLeaf */.  u1
c04f0 36 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 20 20 20  6 minLocal;     
c0500 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 42 74     /* Copy of Bt
c0510 72 65 65 2e 6d 69 6e 4c 6f 63 61 6c 20 6f 72 20  ree.minLocal or 
c0520 42 74 72 65 65 2e 6d 69 6e 4c 65 61 66 20 2a 2f  Btree.minLeaf */
c0530 0a 20 20 75 31 36 20 63 65 6c 6c 4f 66 66 73 65  .  u16 cellOffse
c0540 74 3b 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78  t;      /* Index
c0550 20 69 6e 20 61 44 61 74 61 20 6f 66 20 66 69 72   in aData of fir
c0560 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20  st cell pointer 
c0570 2a 2f 0a 20 20 75 31 36 20 69 64 78 50 61 72 65  */.  u16 idxPare
c0580 6e 74 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64  nt;       /* Ind
c0590 65 78 20 69 6e 20 70 61 72 65 6e 74 20 6f 66 20  ex in parent of 
c05a0 74 68 69 73 20 6e 6f 64 65 20 2a 2f 0a 20 20 75  this node */.  u
c05b0 31 36 20 6e 46 72 65 65 3b 20 20 20 20 20 20 20  16 nFree;       
c05c0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
c05d0 20 66 72 65 65 20 62 79 74 65 73 20 6f 6e 20 74   free bytes on t
c05e0 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 31 36  he page */.  u16
c05f0 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20   nCell;         
c0600 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
c0610 65 6c 6c 73 20 6f 6e 20 74 68 69 73 20 70 61 67  ells on this pag
c0620 65 2c 20 6c 6f 63 61 6c 20 61 6e 64 20 6f 76 66  e, local and ovf
c0630 6c 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 5f 4f  l */.  struct _O
c0640 76 66 6c 43 65 6c 6c 20 7b 20 20 20 2f 2a 20 43  vflCell {   /* C
c0650 65 6c 6c 73 20 74 68 61 74 20 77 69 6c 6c 20 6e  ells that will n
c0660 6f 74 20 66 69 74 20 6f 6e 20 61 44 61 74 61 5b  ot fit on aData[
c0670 5d 20 2a 2f 0a 20 20 20 20 75 38 20 2a 70 43 65  ] */.    u8 *pCe
c0680 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ll;          /* 
c0690 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20  Pointers to the 
c06a0 62 6f 64 79 20 6f 66 20 74 68 65 20 6f 76 65 72  body of the over
c06b0 66 6c 6f 77 20 63 65 6c 6c 20 2a 2f 0a 20 20 20  flow cell */.   
c06c0 20 75 31 36 20 69 64 78 3b 20 20 20 20 20 20 20   u16 idx;       
c06d0 20 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 20 74       /* Insert t
c06e0 68 69 73 20 63 65 6c 6c 20 62 65 66 6f 72 65 20  his cell before 
c06f0 69 64 78 2d 74 68 20 6e 6f 6e 2d 6f 76 65 72 66  idx-th non-overf
c0700 6c 6f 77 20 63 65 6c 6c 20 2a 2f 0a 20 20 7d 20  low cell */.  } 
c0710 61 4f 76 66 6c 5b 35 5d 3b 0a 20 20 42 74 53 68  aOvfl[5];.  BtSh
c0720 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20  ared *pBt;      
c0730 20 2f 2a 20 50 6f 69 6e 74 65 72 20 62 61 63 6b   /* Pointer back
c0740 20 74 6f 20 42 54 72 65 65 20 73 74 72 75 63 74   to BTree struct
c0750 75 72 65 20 2a 2f 0a 20 20 75 38 20 2a 61 44 61  ure */.  u8 *aDa
c0760 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ta;           /*
c0770 20 50 6f 69 6e 74 65 72 20 62 61 63 6b 20 74 6f   Pointer back to
c0780 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68   the start of th
c0790 65 20 70 61 67 65 20 2a 2f 0a 20 20 44 62 50 61  e page */.  DbPa
c07a0 67 65 20 2a 70 44 62 50 61 67 65 3b 20 20 20 20  ge *pDbPage;    
c07b0 20 2f 2a 20 50 61 67 65 72 20 70 61 67 65 20 68   /* Pager page h
c07c0 61 6e 64 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20  andle */.  Pgno 
c07d0 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20  pgno;           
c07e0 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 66  /* Page number f
c07f0 6f 72 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a  or this page */.
c0800 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 72 65    MemPage *pPare
c0810 6e 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 70 61  nt;    /* The pa
c0820 72 65 6e 74 20 6f 66 20 74 68 69 73 20 70 61 67  rent of this pag
c0830 65 2e 20 20 4e 55 4c 4c 20 66 6f 72 20 72 6f 6f  e.  NULL for roo
c0840 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54  t */.};../*.** T
c0850 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 69 6d 61  he in-memory ima
c0860 67 65 20 6f 66 20 61 20 64 69 73 6b 20 70 61 67  ge of a disk pag
c0870 65 20 68 61 73 20 74 68 65 20 61 75 78 69 6c 69  e has the auxili
c0880 61 72 79 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ary information 
c0890 61 70 70 65 6e 64 65 64 0a 2a 2a 20 74 6f 20 74  appended.** to t
c08a0 68 65 20 65 6e 64 2e 20 20 45 58 54 52 41 5f 53  he end.  EXTRA_S
c08b0 49 5a 45 20 69 73 20 74 68 65 20 6e 75 6d 62 65  IZE is the numbe
c08c0 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70  r of bytes of sp
c08d0 61 63 65 20 6e 65 65 64 65 64 20 74 6f 20 68 6f  ace needed to ho
c08e0 6c 64 0a 2a 2a 20 74 68 61 74 20 65 78 74 72 61  ld.** that extra
c08f0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f   information..*/
c0900 0a 23 64 65 66 69 6e 65 20 45 58 54 52 41 5f 53  .#define EXTRA_S
c0910 49 5a 45 20 73 69 7a 65 6f 66 28 4d 65 6d 50 61  IZE sizeof(MemPa
c0920 67 65 29 0a 0a 2f 2a 20 42 74 72 65 65 20 68 61  ge)../* Btree ha
c0930 6e 64 6c 65 20 2a 2f 0a 73 74 72 75 63 74 20 42  ndle */.struct B
c0940 74 72 65 65 20 7b 0a 20 20 73 71 6c 69 74 65 33  tree {.  sqlite3
c0950 20 2a 70 53 71 6c 69 74 65 3b 0a 20 20 42 74 53   *pSqlite;.  BtS
c0960 68 61 72 65 64 20 2a 70 42 74 3b 0a 20 20 75 38  hared *pBt;.  u8
c0970 20 69 6e 54 72 61 6e 73 3b 20 20 20 20 20 20 20   inTrans;       
c0980 20 20 20 20 20 2f 2a 20 54 52 41 4e 53 5f 4e 4f       /* TRANS_NO
c0990 4e 45 2c 20 54 52 41 4e 53 5f 52 45 41 44 20 6f  NE, TRANS_READ o
c09a0 72 20 54 52 41 4e 53 5f 57 52 49 54 45 20 2a 2f  r TRANS_WRITE */
c09b0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 42 74 72 65 65  .};../*.** Btree
c09c0 2e 69 6e 54 72 61 6e 73 20 6d 61 79 20 74 61 6b  .inTrans may tak
c09d0 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c  e one of the fol
c09e0 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73 2e 0a 2a  lowing values..*
c09f0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 68 61 72  *.** If the shar
c0a00 65 64 2d 64 61 74 61 20 65 78 74 65 6e 73 69 6f  ed-data extensio
c0a10 6e 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68  n is enabled, th
c0a20 65 72 65 20 6d 61 79 20 62 65 20 6d 75 6c 74 69  ere may be multi
c0a30 70 6c 65 20 75 73 65 72 73 0a 2a 2a 20 6f 66 20  ple users.** of 
c0a40 74 68 65 20 42 74 72 65 65 20 73 74 72 75 63 74  the Btree struct
c0a50 75 72 65 2e 20 41 74 20 6d 6f 73 74 20 6f 6e 65  ure. At most one
c0a60 20 6f 66 20 74 68 65 73 65 20 6d 61 79 20 6f 70   of these may op
c0a70 65 6e 20 61 20 77 72 69 74 65 20 74 72 61 6e 73  en a write trans
c0a80 61 63 74 69 6f 6e 2c 0a 2a 2a 20 62 75 74 20 61  action,.** but a
c0a90 6e 79 20 6e 75 6d 62 65 72 20 6d 61 79 20 68 61  ny number may ha
c0aa0 76 65 20 61 63 74 69 76 65 20 72 65 61 64 20 74  ve active read t
c0ab0 72 61 6e 73 61 63 74 69 6f 6e 73 2e 20 56 61 72  ransactions. Var
c0ac0 69 61 62 6c 65 20 42 74 72 65 65 2e 70 44 62 20  iable Btree.pDb 
c0ad0 0a 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20 74 68  .** points to th
c0ae0 65 20 68 61 6e 64 6c 65 20 74 68 61 74 20 6f 77  e handle that ow
c0af0 6e 73 20 61 6e 79 20 63 75 72 72 65 6e 74 20 77  ns any current w
c0b00 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e  rite-transaction
c0b10 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 52 41  ..*/.#define TRA
c0b20 4e 53 5f 4e 4f 4e 45 20 20 30 0a 23 64 65 66 69  NS_NONE  0.#defi
c0b30 6e 65 20 54 52 41 4e 53 5f 52 45 41 44 20 20 31  ne TRANS_READ  1
c0b40 0a 23 64 65 66 69 6e 65 20 54 52 41 4e 53 5f 57  .#define TRANS_W
c0b50 52 49 54 45 20 32 0a 0a 2f 2a 0a 2a 2a 20 45 76  RITE 2../*.** Ev
c0b60 65 72 79 74 68 69 6e 67 20 77 65 20 6e 65 65 64  erything we need
c0b70 20 74 6f 20 6b 6e 6f 77 20 61 62 6f 75 74 20 61   to know about a
c0b80 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 0a  n open database.
c0b90 2a 2f 0a 73 74 72 75 63 74 20 42 74 53 68 61 72  */.struct BtShar
c0ba0 65 64 20 7b 0a 20 20 50 61 67 65 72 20 2a 70 50  ed {.  Pager *pP
c0bb0 61 67 65 72 3b 20 20 20 20 20 20 20 20 2f 2a 20  ager;        /* 
c0bc0 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20 2a  The page cache *
c0bd0 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43  /.  BtCursor *pC
c0be0 75 72 73 6f 72 3b 20 20 20 20 2f 2a 20 41 20 6c  ursor;    /* A l
c0bf0 69 73 74 20 6f 66 20 61 6c 6c 20 6f 70 65 6e 20  ist of all open 
c0c00 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 4d 65 6d  cursors */.  Mem
c0c10 50 61 67 65 20 2a 70 50 61 67 65 31 3b 20 20 20  Page *pPage1;   
c0c20 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61 67 65     /* First page
c0c30 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
c0c40 20 2a 2f 0a 20 20 75 38 20 69 6e 53 74 6d 74 3b   */.  u8 inStmt;
c0c50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
c0c60 72 75 65 20 69 66 20 77 65 20 61 72 65 20 69 6e  rue if we are in
c0c70 20 61 20 73 74 61 74 65 6d 65 6e 74 20 73 75 62   a statement sub
c0c80 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20  transaction */. 
c0c90 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20 20   u8 readOnly;   
c0ca0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
c0cb0 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  f the underlying
c0cc0 20 66 69 6c 65 20 69 73 20 72 65 61 64 6f 6e 6c   file is readonl
c0cd0 79 20 2a 2f 0a 20 20 75 38 20 6d 61 78 45 6d 62  y */.  u8 maxEmb
c0ce0 65 64 46 72 61 63 3b 20 20 20 20 20 20 2f 2a 20  edFrac;      /* 
c0cf0 4d 61 78 69 6d 75 6d 20 70 61 79 6c 6f 61 64 20  Maximum payload 
c0d00 61 73 20 25 20 6f 66 20 74 6f 74 61 6c 20 70 61  as % of total pa
c0d10 67 65 20 73 69 7a 65 20 2a 2f 0a 20 20 75 38 20  ge size */.  u8 
c0d20 6d 69 6e 45 6d 62 65 64 46 72 61 63 3b 20 20 20  minEmbedFrac;   
c0d30 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 70 61     /* Minimum pa
c0d40 79 6c 6f 61 64 20 61 73 20 25 20 6f 66 20 74 6f  yload as % of to
c0d50 74 61 6c 20 70 61 67 65 20 73 69 7a 65 20 2a 2f  tal page size */
c0d60 0a 20 20 75 38 20 6d 69 6e 4c 65 61 66 46 72 61  .  u8 minLeafFra
c0d70 63 3b 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69  c;       /* Mini
c0d80 6d 75 6d 20 6c 65 61 66 20 70 61 79 6c 6f 61 64  mum leaf payload
c0d90 20 61 73 20 25 20 6f 66 20 74 6f 74 61 6c 20 70   as % of total p
c0da0 61 67 65 20 73 69 7a 65 20 2a 2f 0a 20 20 75 38  age size */.  u8
c0db0 20 70 61 67 65 53 69 7a 65 46 69 78 65 64 3b 20   pageSizeFixed; 
c0dc0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
c0dd0 68 65 20 70 61 67 65 20 73 69 7a 65 20 63 61 6e  he page size can
c0de0 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65 20 63 68   no longer be ch
c0df0 61 6e 67 65 64 20 2a 2f 0a 23 69 66 6e 64 65 66  anged */.#ifndef
c0e00 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
c0e10 4f 56 41 43 55 55 4d 0a 20 20 75 38 20 61 75 74  OVACUUM.  u8 aut
c0e20 6f 56 61 63 75 75 6d 3b 20 20 20 20 20 20 20 20  oVacuum;        
c0e30 2f 2a 20 54 72 75 65 20 69 66 20 61 75 74 6f 2d  /* True if auto-
c0e40 76 61 63 75 75 6d 20 69 73 20 65 6e 61 62 6c 65  vacuum is enable
c0e50 64 20 2a 2f 0a 20 20 75 38 20 69 6e 63 72 56 61  d */.  u8 incrVa
c0e60 63 75 75 6d 3b 20 20 20 20 20 20 20 20 2f 2a 20  cuum;        /* 
c0e70 54 72 75 65 20 69 66 20 69 6e 63 72 2d 76 61 63  True if incr-vac
c0e80 75 75 6d 20 69 73 20 65 6e 61 62 6c 65 64 20 2a  uum is enabled *
c0e90 2f 0a 20 20 50 67 6e 6f 20 6e 54 72 75 6e 63 3b  /.  Pgno nTrunc;
c0ea0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 6e            /* Non
c0eb0 2d 7a 65 72 6f 20 69 66 20 74 68 65 20 64 62 20  -zero if the db 
c0ec0 77 69 6c 6c 20 62 65 20 74 72 75 6e 63 61 74 65  will be truncate
c0ed0 64 20 28 69 6e 63 72 20 76 61 63 75 75 6d 29 20  d (incr vacuum) 
c0ee0 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 75 31 36 20  */.#endif.  u16 
c0ef0 70 61 67 65 53 69 7a 65 3b 20 20 20 20 20 20 20  pageSize;       
c0f00 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65    /* Total numbe
c0f10 72 20 6f 66 20 62 79 74 65 73 20 6f 6e 20 61 20  r of bytes on a 
c0f20 70 61 67 65 20 2a 2f 0a 20 20 75 31 36 20 75 73  page */.  u16 us
c0f30 61 62 6c 65 53 69 7a 65 3b 20 20 20 20 20 20 20  ableSize;       
c0f40 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 61  /* Number of usa
c0f50 62 6c 65 20 62 79 74 65 73 20 6f 6e 20 65 61 63  ble bytes on eac
c0f60 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20  h page */.  int 
c0f70 6d 61 78 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20  maxLocal;       
c0f80 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 6f 63    /* Maximum loc
c0f90 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20 6e 6f  al payload in no
c0fa0 6e 2d 4c 45 41 46 44 41 54 41 20 74 61 62 6c 65  n-LEAFDATA table
c0fb0 73 20 2a 2f 0a 20 20 69 6e 74 20 6d 69 6e 4c 6f  s */.  int minLo
c0fc0 63 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  cal;         /* 
c0fd0 4d 69 6e 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61  Minimum local pa
c0fe0 79 6c 6f 61 64 20 69 6e 20 6e 6f 6e 2d 4c 45 41  yload in non-LEA
c0ff0 46 44 41 54 41 20 74 61 62 6c 65 73 20 2a 2f 0a  FDATA tables */.
c1000 20 20 69 6e 74 20 6d 61 78 4c 65 61 66 3b 20 20    int maxLeaf;  
c1010 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d          /* Maxim
c1020 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64  um local payload
c1030 20 69 6e 20 61 20 4c 45 41 46 44 41 54 41 20 74   in a LEAFDATA t
c1040 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 69  able */.  int mi
c1050 6e 4c 65 61 66 3b 20 20 20 20 20 20 20 20 20 20  nLeaf;          
c1060 2f 2a 20 4d 69 6e 69 6d 75 6d 20 6c 6f 63 61 6c  /* Minimum local
c1070 20 70 61 79 6c 6f 61 64 20 69 6e 20 61 20 4c 45   payload in a LE
c1080 41 46 44 41 54 41 20 74 61 62 6c 65 20 2a 2f 0a  AFDATA table */.
c1090 20 20 42 75 73 79 48 61 6e 64 6c 65 72 20 2a 70    BusyHandler *p
c10a0 42 75 73 79 48 61 6e 64 6c 65 72 3b 20 20 20 2f  BusyHandler;   /
c10b0 2a 20 43 61 6c 6c 62 61 63 6b 20 66 6f 72 20 77  * Callback for w
c10c0 68 65 6e 20 74 68 65 72 65 20 69 73 20 6c 6f 63  hen there is loc
c10d0 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 20 2a 2f 0a  k contention */.
c10e0 20 20 75 38 20 69 6e 54 72 61 6e 73 61 63 74 69    u8 inTransacti
c10f0 6f 6e 3b 20 20 20 20 20 2f 2a 20 54 72 61 6e 73  on;     /* Trans
c1100 61 63 74 69 6f 6e 20 73 74 61 74 65 20 2a 2f 0a  action state */.
c1110 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20    int nRef;     
c1120 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
c1130 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20  r of references 
c1140 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72  to this structur
c1150 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 72 61 6e  e */.  int nTran
c1160 73 61 63 74 69 6f 6e 3b 20 20 20 20 20 2f 2a 20  saction;     /* 
c1170 4e 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20 74  Number of open t
c1180 72 61 6e 73 61 63 74 69 6f 6e 73 20 28 72 65 61  ransactions (rea
c1190 64 20 2b 20 77 72 69 74 65 29 20 2a 2f 0a 20 20  d + write) */.  
c11a0 76 6f 69 64 20 2a 70 53 63 68 65 6d 61 3b 20 20  void *pSchema;  
c11b0 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
c11c0 20 74 6f 20 73 70 61 63 65 20 61 6c 6c 6f 63 61   to space alloca
c11d0 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 42 74  ted by sqlite3Bt
c11e0 72 65 65 53 63 68 65 6d 61 28 29 20 2a 2f 0a 20  reeSchema() */. 
c11f0 20 76 6f 69 64 20 28 2a 78 46 72 65 65 53 63 68   void (*xFreeSch
c1200 65 6d 61 29 28 76 6f 69 64 2a 29 3b 20 20 2f 2a  ema)(void*);  /*
c1210 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20   Destructor for 
c1220 42 74 53 68 61 72 65 64 2e 70 53 63 68 65 6d 61  BtShared.pSchema
c1230 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   */.#ifndef SQLI
c1240 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
c1250 41 43 48 45 0a 20 20 42 74 4c 6f 63 6b 20 2a 70  ACHE.  BtLock *p
c1260 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20  Lock;        /* 
c1270 4c 69 73 74 20 6f 66 20 6c 6f 63 6b 73 20 68 65  List of locks he
c1280 6c 64 20 6f 6e 20 74 68 69 73 20 73 68 61 72 65  ld on this share
c1290 64 2d 62 74 72 65 65 20 73 74 72 75 63 74 20 2a  d-btree struct *
c12a0 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 4e  /.  BtShared *pN
c12b0 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78  ext;      /* Nex
c12c0 74 20 69 6e 20 54 68 72 65 61 64 44 61 74 61 2e  t in ThreadData.
c12d0 70 42 74 72 65 65 20 6c 69 6e 6b 65 64 20 6c 69  pBtree linked li
c12e0 73 74 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a  st */.#endif.};.
c12f0 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  ./*.** An instan
c1300 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
c1310 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73  ing structure is
c1320 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 69 6e   used to hold in
c1330 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f  formation.** abo
c1340 75 74 20 61 20 63 65 6c 6c 2e 20 20 54 68 65 20  ut a cell.  The 
c1350 70 61 72 73 65 43 65 6c 6c 50 74 72 28 29 20 66  parseCellPtr() f
c1360 75 6e 63 74 69 6f 6e 20 66 69 6c 6c 73 20 69 6e  unction fills in
c1370 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 0a   this structure.
c1380 2a 2a 20 62 61 73 65 64 20 6f 6e 20 69 6e 66 6f  ** based on info
c1390 72 6d 61 74 69 6f 6e 20 65 78 74 72 61 63 74 20  rmation extract 
c13a0 66 72 6f 6d 20 74 68 65 20 72 61 77 20 64 69 73  from the raw dis
c13b0 6b 20 70 61 67 65 2e 0a 2a 2f 0a 74 79 70 65 64  k page..*/.typed
c13c0 65 66 20 73 74 72 75 63 74 20 43 65 6c 6c 49 6e  ef struct CellIn
c13d0 66 6f 20 43 65 6c 6c 49 6e 66 6f 3b 0a 73 74 72  fo CellInfo;.str
c13e0 75 63 74 20 43 65 6c 6c 49 6e 66 6f 20 7b 0a 20  uct CellInfo {. 
c13f0 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20 20 20   u8 *pCell;     
c1400 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68  /* Pointer to th
c1410 65 20 73 74 61 72 74 20 6f 66 20 63 65 6c 6c 20  e start of cell 
c1420 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 69 36 34  content */.  i64
c1430 20 6e 4b 65 79 3b 20 20 20 20 20 20 2f 2a 20 54   nKey;      /* T
c1440 68 65 20 6b 65 79 20 66 6f 72 20 49 4e 54 4b 45  he key for INTKE
c1450 59 20 74 61 62 6c 65 73 2c 20 6f 72 20 6e 75 6d  Y tables, or num
c1460 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
c1470 6b 65 79 20 2a 2f 0a 20 20 75 33 32 20 6e 44 61  key */.  u32 nDa
c1480 74 61 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  ta;     /* Numbe
c1490 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61  r of bytes of da
c14a0 74 61 20 2a 2f 0a 20 20 75 33 32 20 6e 50 61 79  ta */.  u32 nPay
c14b0 6c 6f 61 64 3b 20 20 2f 2a 20 54 6f 74 61 6c 20  load;  /* Total 
c14c0 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61  amount of payloa
c14d0 64 20 2a 2f 0a 20 20 75 31 36 20 6e 48 65 61 64  d */.  u16 nHead
c14e0 65 72 3b 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66  er;   /* Size of
c14f0 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e   the cell conten
c1500 74 20 68 65 61 64 65 72 20 69 6e 20 62 79 74 65  t header in byte
c1510 73 20 2a 2f 0a 20 20 75 31 36 20 6e 4c 6f 63 61  s */.  u16 nLoca
c1520 6c 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20  l;    /* Amount 
c1530 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64 20  of payload held 
c1540 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 75 31 36  locally */.  u16
c1550 20 69 4f 76 65 72 66 6c 6f 77 3b 20 2f 2a 20 4f   iOverflow; /* O
c1560 66 66 73 65 74 20 74 6f 20 6f 76 65 72 66 6c 6f  ffset to overflo
c1570 77 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 20  w page number.  
c1580 5a 65 72 6f 20 69 66 20 6e 6f 20 6f 76 65 72 66  Zero if no overf
c1590 6c 6f 77 20 2a 2f 0a 20 20 75 31 36 20 6e 53 69  low */.  u16 nSi
c15a0 7a 65 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20  ze;     /* Size 
c15b0 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  of the cell cont
c15c0 65 6e 74 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20  ent on the main 
c15d0 62 2d 74 72 65 65 20 70 61 67 65 20 2a 2f 0a 7d  b-tree page */.}
c15e0 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 75 72 73 6f  ;../*.** A curso
c15f0 72 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  r is a pointer t
c1600 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 65  o a particular e
c1610 6e 74 72 79 20 69 6e 20 74 68 65 20 42 54 72 65  ntry in the BTre
c1620 65 2e 0a 2a 2a 20 54 68 65 20 65 6e 74 72 79 20  e..** The entry 
c1630 69 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 79  is identified by
c1640 20 69 74 73 20 4d 65 6d 50 61 67 65 20 61 6e 64   its MemPage and
c1650 20 74 68 65 20 69 6e 64 65 78 20 69 6e 0a 2a 2a   the index in.**
c1660 20 4d 65 6d 50 61 67 65 2e 61 43 65 6c 6c 5b 5d   MemPage.aCell[]
c1670 20 6f 66 20 74 68 65 20 65 6e 74 72 79 2e 0a 2a   of the entry..*
c1680 2f 0a 73 74 72 75 63 74 20 42 74 43 75 72 73 6f  /.struct BtCurso
c1690 72 20 7b 0a 20 20 42 74 72 65 65 20 2a 70 42 74  r {.  Btree *pBt
c16a0 72 65 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ree;            
c16b0 2f 2a 20 54 68 65 20 42 74 72 65 65 20 74 6f 20  /* The Btree to 
c16c0 77 68 69 63 68 20 74 68 69 73 20 63 75 72 73 6f  which this curso
c16d0 72 20 62 65 6c 6f 6e 67 73 20 2a 2f 0a 20 20 42  r belongs */.  B
c16e0 74 43 75 72 73 6f 72 20 2a 70 4e 65 78 74 2c 20  tCursor *pNext, 
c16f0 2a 70 50 72 65 76 3b 20 20 2f 2a 20 46 6f 72 6d  *pPrev;  /* Form
c1700 73 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  s a linked list 
c1710 6f 66 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 2a  of all cursors *
c1720 2f 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6d 70 61  /.  int (*xCompa
c1730 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  re)(void*,int,co
c1740 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  nst void*,int,co
c1750 6e 73 74 20 76 6f 69 64 2a 29 3b 20 2f 2a 20 4b  nst void*); /* K
c1760 65 79 20 63 6f 6d 70 20 66 75 6e 63 20 2a 2f 0a  ey comp func */.
c1770 20 20 76 6f 69 64 20 2a 70 41 72 67 3b 20 20 20    void *pArg;   
c1780 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
c1790 69 72 73 74 20 61 72 67 20 74 6f 20 78 43 6f 6d  irst arg to xCom
c17a0 70 61 72 65 28 29 20 2a 2f 0a 20 20 50 67 6e 6f  pare() */.  Pgno
c17b0 20 70 67 6e 6f 52 6f 6f 74 3b 20 20 20 20 20 20   pgnoRoot;      
c17c0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 6f        /* The roo
c17d0 74 20 70 61 67 65 20 6f 66 20 74 68 69 73 20 74  t page of this t
c17e0 72 65 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65  ree */.  MemPage
c17f0 20 2a 70 50 61 67 65 3b 20 20 20 20 20 20 20 20   *pPage;        
c1800 20 20 20 2f 2a 20 50 61 67 65 20 74 68 61 74 20     /* Page that 
c1810 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 65 6e 74  contains the ent
c1820 72 79 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 3b  ry */.  int idx;
c1830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c1840 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68    /* Index of th
c1850 65 20 65 6e 74 72 79 20 69 6e 20 70 50 61 67 65  e entry in pPage
c1860 2d 3e 61 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 43  ->aCell[] */.  C
c1870 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 20 20 20  ellInfo info;   
c1880 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 70 61           /* A pa
c1890 72 73 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20  rse of the cell 
c18a0 77 65 20 61 72 65 20 70 6f 69 6e 74 69 6e 67 20  we are pointing 
c18b0 61 74 20 2a 2f 0a 20 20 75 38 20 77 72 46 6c 61  at */.  u8 wrFla
c18c0 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g;              
c18d0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 77 72 69    /* True if wri
c18e0 74 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20 65 53  table */.  u8 eS
c18f0 74 61 74 65 3b 20 20 20 20 20 20 20 20 20 20 20  tate;           
c1900 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74       /* One of t
c1910 68 65 20 43 55 52 53 4f 52 5f 58 58 58 20 63 6f  he CURSOR_XXX co
c1920 6e 73 74 61 6e 74 73 20 28 73 65 65 20 62 65 6c  nstants (see bel
c1930 6f 77 29 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  ow) */.  void *p
c1940 4b 65 79 3b 20 20 20 20 20 20 2f 2a 20 53 61 76  Key;      /* Sav
c1950 65 64 20 6b 65 79 20 74 68 61 74 20 77 61 73 20  ed key that was 
c1960 63 75 72 73 6f 72 27 73 20 6c 61 73 74 20 6b 6e  cursor's last kn
c1970 6f 77 6e 20 70 6f 73 69 74 69 6f 6e 20 2a 2f 0a  own position */.
c1980 20 20 69 36 34 20 6e 4b 65 79 3b 20 20 20 20 20    i64 nKey;     
c1990 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 4b     /* Size of pK
c19a0 65 79 2c 20 6f 72 20 6c 61 73 74 20 69 6e 74 65  ey, or last inte
c19b0 67 65 72 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74  ger key */.  int
c19c0 20 73 6b 69 70 3b 20 20 20 20 20 20 20 20 2f 2a   skip;        /*
c19d0 20 28 73 6b 69 70 3c 30 29 20 2d 3e 20 50 72 65   (skip<0) -> Pre
c19e0 76 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20  v() is a no-op. 
c19f0 28 73 6b 69 70 3e 30 29 20 2d 3e 20 4e 65 78 74  (skip>0) -> Next
c1a00 28 29 20 69 73 20 2a 2f 0a 23 69 66 6e 64 65 66  () is */.#ifndef
c1a10 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43   SQLITE_OMIT_INC
c1a20 52 42 4c 4f 42 0a 20 20 75 38 20 69 73 49 6e 63  RBLOB.  u8 isInc
c1a30 72 62 6c 6f 62 48 61 6e 64 6c 65 3b 20 20 20 20  rblobHandle;    
c1a40 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69    /* True if thi
c1a50 73 20 63 75 72 73 6f 72 20 69 73 20 61 6e 20 69  s cursor is an i
c1a60 6e 63 72 2e 20 69 6f 20 68 61 6e 64 6c 65 20 2a  ncr. io handle *
c1a70 2f 0a 20 20 50 67 6e 6f 20 2a 61 4f 76 65 72 66  /.  Pgno *aOverf
c1a80 6c 6f 77 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  low;          /*
c1a90 20 43 61 63 68 65 20 6f 66 20 6f 76 65 72 66 6c   Cache of overfl
c1aa0 6f 77 20 70 61 67 65 20 6c 6f 63 61 74 69 6f 6e  ow page location
c1ab0 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a  s */.#endif.};..
c1ac0 2f 2a 0a 2a 2a 20 50 6f 74 65 6e 74 69 61 6c 20  /*.** Potential 
c1ad0 76 61 6c 75 65 73 20 66 6f 72 20 42 74 43 75 72  values for BtCur
c1ae0 73 6f 72 2e 65 53 74 61 74 65 2e 0a 2a 2a 0a 2a  sor.eState..**.*
c1af0 2a 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3a 0a  * CURSOR_VALID:.
c1b00 2a 2a 20 20 20 43 75 72 73 6f 72 20 70 6f 69 6e  **   Cursor poin
c1b10 74 73 20 74 6f 20 61 20 76 61 6c 69 64 20 65 6e  ts to a valid en
c1b20 74 72 79 2e 20 67 65 74 50 61 79 6c 6f 61 64 28  try. getPayload(
c1b30 29 20 65 74 63 2e 20 6d 61 79 20 62 65 20 63 61  ) etc. may be ca
c1b40 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53  lled..**.** CURS
c1b50 4f 52 5f 49 4e 56 41 4c 49 44 3a 0a 2a 2a 20 20  OR_INVALID:.**  
c1b60 20 43 75 72 73 6f 72 20 64 6f 65 73 20 6e 6f 74   Cursor does not
c1b70 20 70 6f 69 6e 74 20 74 6f 20 61 20 76 61 6c 69   point to a vali
c1b80 64 20 65 6e 74 72 79 2e 20 54 68 69 73 20 63 61  d entry. This ca
c1b90 6e 20 68 61 70 70 65 6e 20 28 66 6f 72 20 65 78  n happen (for ex
c1ba0 61 6d 70 6c 65 29 20 0a 2a 2a 20 20 20 62 65 63  ample) .**   bec
c1bb0 61 75 73 65 20 74 68 65 20 74 61 62 6c 65 20 69  ause the table i
c1bc0 73 20 65 6d 70 74 79 20 6f 72 20 62 65 63 61 75  s empty or becau
c1bd0 73 65 20 42 74 72 65 65 43 75 72 73 6f 72 46 69  se BtreeCursorFi
c1be0 72 73 74 28 29 20 68 61 73 20 6e 6f 74 20 62 65  rst() has not be
c1bf0 65 6e 0a 2a 2a 20 20 20 63 61 6c 6c 65 64 2e 0a  en.**   called..
c1c00 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f 52 45 51  **.** CURSOR_REQ
c1c10 55 49 52 45 53 45 45 4b 3a 0a 2a 2a 20 20 20 54  UIRESEEK:.**   T
c1c20 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 74 68  he table that th
c1c30 69 73 20 63 75 72 73 6f 72 20 77 61 73 20 6f 70  is cursor was op
c1c40 65 6e 65 64 20 6f 6e 20 73 74 69 6c 6c 20 65 78  ened on still ex
c1c50 69 73 74 73 2c 20 62 75 74 20 68 61 73 20 62 65  ists, but has be
c1c60 65 6e 20 0a 2a 2a 20 20 20 6d 6f 64 69 66 69 65  en .**   modifie
c1c70 64 20 73 69 6e 63 65 20 74 68 65 20 63 75 72 73  d since the curs
c1c80 6f 72 20 77 61 73 20 6c 61 73 74 20 75 73 65 64  or was last used
c1c90 2e 20 54 68 65 20 63 75 72 73 6f 72 20 70 6f 73  . The cursor pos
c1ca0 69 74 69 6f 6e 20 69 73 20 73 61 76 65 64 0a 2a  ition is saved.*
c1cb0 2a 20 20 20 69 6e 20 76 61 72 69 61 62 6c 65 73  *   in variables
c1cc0 20 42 74 43 75 72 73 6f 72 2e 70 4b 65 79 20 61   BtCursor.pKey a
c1cd0 6e 64 20 42 74 43 75 72 73 6f 72 2e 6e 4b 65 79  nd BtCursor.nKey
c1ce0 2e 20 57 68 65 6e 20 61 20 63 75 72 73 6f 72 20  . When a cursor 
c1cf0 69 73 20 69 6e 20 0a 2a 2a 20 20 20 74 68 69 73  is in .**   this
c1d00 20 73 74 61 74 65 2c 20 72 65 73 74 6f 72 65 4f   state, restoreO
c1d10 72 43 6c 65 61 72 43 75 72 73 6f 72 50 6f 73 69  rClearCursorPosi
c1d20 74 69 6f 6e 28 29 20 63 61 6e 20 62 65 20 63 61  tion() can be ca
c1d30 6c 6c 65 64 20 74 6f 20 61 74 74 65 6d 70 74 20  lled to attempt 
c1d40 74 6f 0a 2a 2a 20 20 20 73 65 65 6b 20 74 68 65  to.**   seek the
c1d50 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 73   cursor to the s
c1d60 61 76 65 64 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a  aved position..*
c1d70 2f 0a 23 64 65 66 69 6e 65 20 43 55 52 53 4f 52  /.#define CURSOR
c1d80 5f 49 4e 56 41 4c 49 44 20 20 20 20 20 20 20 20  _INVALID        
c1d90 20 20 20 30 0a 23 64 65 66 69 6e 65 20 43 55 52     0.#define CUR
c1da0 53 4f 52 5f 56 41 4c 49 44 20 20 20 20 20 20 20  SOR_VALID       
c1db0 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20        1.#define 
c1dc0 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45  CURSOR_REQUIRESE
c1dd0 45 4b 20 20 20 20 20 20 20 32 0a 0a 2f 2a 0a 2a  EK       2../*.*
c1de0 2a 20 54 68 65 20 54 52 41 43 45 20 6d 61 63 72  * The TRACE macr
c1df0 6f 20 77 69 6c 6c 20 70 72 69 6e 74 20 68 69 67  o will print hig
c1e00 68 2d 6c 65 76 65 6c 20 73 74 61 74 75 73 20 69  h-level status i
c1e10 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
c1e20 20 74 68 65 0a 2a 2a 20 62 74 72 65 65 20 6f 70   the.** btree op
c1e30 65 72 61 74 69 6f 6e 20 77 68 65 6e 20 74 68 65  eration when the
c1e40 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65   global variable
c1e50 20 73 71 6c 69 74 65 33 5f 62 74 72 65 65 5f 74   sqlite3_btree_t
c1e60 72 61 63 65 20 69 73 0a 2a 2a 20 65 6e 61 62 6c  race is.** enabl
c1e70 65 64 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54  ed..*/.#if SQLIT
c1e80 45 5f 54 45 53 54 0a 23 20 64 65 66 69 6e 65 20  E_TEST.# define 
c1e90 54 52 41 43 45 28 58 29 20 20 20 69 66 28 20 73  TRACE(X)   if( s
c1ea0 71 6c 69 74 65 33 5f 62 74 72 65 65 5f 74 72 61  qlite3_btree_tra
c1eb0 63 65 20 29 7b 20 70 72 69 6e 74 66 20 58 3b 20  ce ){ printf X; 
c1ec0 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29 3b 20  fflush(stdout); 
c1ed0 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  }.#else.# define
c1ee0 20 54 52 41 43 45 28 58 29 0a 23 65 6e 64 69 66   TRACE(X).#endif
c1ef0 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 73  ../*.** Routines
c1f00 20 74 6f 20 72 65 61 64 20 61 6e 64 20 77 72 69   to read and wri
c1f10 74 65 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67  te variable-leng
c1f20 74 68 20 69 6e 74 65 67 65 72 73 2e 20 20 54 68  th integers.  Th
c1f30 65 73 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 62  ese used to.** b
c1f40 65 20 64 65 66 69 6e 65 64 20 6c 6f 63 61 6c 6c  e defined locall
c1f50 79 2c 20 62 75 74 20 6e 6f 77 20 77 65 20 75 73  y, but now we us
c1f60 65 20 74 68 65 20 76 61 72 69 6e 74 20 72 6f 75  e the varint rou
c1f70 74 69 6e 65 73 20 69 6e 20 74 68 65 20 75 74 69  tines in the uti
c1f80 6c 2e 63 0a 2a 2a 20 66 69 6c 65 2e 0a 2a 2f 0a  l.c.** file..*/.
c1f90 23 64 65 66 69 6e 65 20 67 65 74 56 61 72 69 6e  #define getVarin
c1fa0 74 20 20 20 20 73 71 6c 69 74 65 33 47 65 74 56  t    sqlite3GetV
c1fb0 61 72 69 6e 74 0a 23 64 65 66 69 6e 65 20 67 65  arint.#define ge
c1fc0 74 56 61 72 69 6e 74 33 32 28 41 2c 42 29 20 20  tVarint32(A,B)  
c1fd0 28 28 2a 42 3d 2a 28 41 29 29 3c 3d 30 78 37 66  ((*B=*(A))<=0x7f
c1fe0 3f 31 3a 73 71 6c 69 74 65 33 47 65 74 56 61 72  ?1:sqlite3GetVar
c1ff0 69 6e 74 33 32 28 41 2c 42 29 29 0a 23 64 65 66  int32(A,B)).#def
c2000 69 6e 65 20 70 75 74 56 61 72 69 6e 74 20 20 20  ine putVarint   
c2010 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e   sqlite3PutVarin
c2020 74 0a 0a 2f 2a 20 54 68 65 20 64 61 74 61 62 61  t../* The databa
c2030 73 65 20 70 61 67 65 20 74 68 65 20 50 45 4e 44  se page the PEND
c2040 49 4e 47 5f 42 59 54 45 20 6f 63 63 75 70 69 65  ING_BYTE occupie
c2050 73 2e 20 54 68 69 73 20 70 61 67 65 20 69 73 20  s. This page is 
c2060 6e 65 76 65 72 20 75 73 65 64 2e 0a 2a 2a 20 54  never used..** T
c2070 4f 44 4f 3a 20 54 68 69 73 20 6d 61 63 72 6f 20  ODO: This macro 
c2080 69 73 20 76 65 72 79 20 73 69 6d 69 6c 61 72 79  is very similary
c2090 20 74 6f 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e   to PAGER_MJ_PGN
c20a0 4f 28 29 20 69 6e 20 70 61 67 65 72 2e 63 2e 20  O() in pager.c. 
c20b0 54 68 65 79 0a 2a 2a 20 73 68 6f 75 6c 64 20 70  They.** should p
c20c0 6f 73 73 69 62 6c 79 20 62 65 20 63 6f 6e 73 6f  ossibly be conso
c20d0 6c 69 64 61 74 65 64 20 28 70 72 65 73 75 6d 61  lidated (presuma
c20e0 62 6c 79 20 69 6e 20 70 61 67 65 72 2e 68 29 2e  bly in pager.h).
c20f0 0a 2a 2a 0a 2a 2a 20 49 66 20 64 69 73 6b 20 49  .**.** If disk I
c2100 2f 4f 20 69 73 20 6f 6d 69 74 74 65 64 20 28 6d  /O is omitted (m
c2110 65 61 6e 69 6e 67 20 74 68 61 74 20 74 68 65 20  eaning that the 
c2120 64 61 74 61 62 61 73 65 20 69 73 20 73 74 6f 72  database is stor
c2130 65 64 20 70 75 72 65 6c 79 0a 2a 2a 20 69 6e 20  ed purely.** in 
c2140 6d 65 6d 6f 72 79 29 20 74 68 65 6e 20 74 68 65  memory) then the
c2150 72 65 20 69 73 20 6e 6f 20 70 65 6e 64 69 6e 67  re is no pending
c2160 20 62 79 74 65 2e 0a 2a 2f 0a 23 69 66 64 65 66   byte..*/.#ifdef
c2170 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53   SQLITE_OMIT_DIS
c2180 4b 49 4f 0a 23 20 64 65 66 69 6e 65 20 50 45 4e  KIO.# define PEN
c2190 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
c21a0 42 74 29 20 20 30 78 37 66 66 66 66 66 66 66 0a  Bt)  0x7fffffff.
c21b0 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 50  #else.# define P
c21c0 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45  ENDING_BYTE_PAGE
c21d0 28 70 42 74 29 20 28 28 50 45 4e 44 49 4e 47 5f  (pBt) ((PENDING_
c21e0 42 59 54 45 2f 28 70 42 74 29 2d 3e 70 61 67 65  BYTE/(pBt)->page
c21f0 53 69 7a 65 29 2b 31 29 0a 23 65 6e 64 69 66 0a  Size)+1).#endif.
c2200 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 6e 6b 65 64 20  ./*.** A linked 
c2210 6c 69 73 74 20 6f 66 20 74 68 65 20 66 6f 6c 6c  list of the foll
c2220 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 73  owing structures
c2230 20 69 73 20 73 74 6f 72 65 64 20 61 74 20 42 74   is stored at Bt
c2240 53 68 61 72 65 64 2e 70 4c 6f 63 6b 2e 0a 2a 2a  Shared.pLock..**
c2250 20 4c 6f 63 6b 73 20 61 72 65 20 61 64 64 65 64   Locks are added
c2260 20 28 6f 72 20 75 70 67 72 61 64 65 64 20 66 72   (or upgraded fr
c2270 6f 6d 20 52 45 41 44 5f 4c 4f 43 4b 20 74 6f 20  om READ_LOCK to 
c2280 57 52 49 54 45 5f 4c 4f 43 4b 29 20 77 68 65 6e  WRITE_LOCK) when
c2290 20 61 20 63 75 72 73 6f 72 20 0a 2a 2a 20 69 73   a cursor .** is
c22a0 20 6f 70 65 6e 65 64 20 6f 6e 20 74 68 65 20 74   opened on the t
c22b0 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 20 70  able with root p
c22c0 61 67 65 20 42 74 53 68 61 72 65 64 2e 69 54 61  age BtShared.iTa
c22d0 62 6c 65 2e 20 4c 6f 63 6b 73 20 61 72 65 20 72  ble. Locks are r
c22e0 65 6d 6f 76 65 64 0a 2a 2a 20 66 72 6f 6d 20 74  emoved.** from t
c22f0 68 69 73 20 6c 69 73 74 20 77 68 65 6e 20 61 20  his list when a 
c2300 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63  transaction is c
c2310 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c  ommitted or roll
c2320 65 64 20 62 61 63 6b 2c 20 6f 72 20 77 68 65 6e  ed back, or when
c2330 0a 2a 2a 20 61 20 62 74 72 65 65 20 68 61 6e 64  .** a btree hand
c2340 6c 65 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2f  le is closed..*/
c2350 0a 73 74 72 75 63 74 20 42 74 4c 6f 63 6b 20 7b  .struct BtLock {
c2360 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72 65 65  .  Btree *pBtree
c2370 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 74 72 65  ;        /* Btre
c2380 65 20 68 61 6e 64 6c 65 20 68 6f 6c 64 69 6e 67  e handle holding
c2390 20 74 68 69 73 20 6c 6f 63 6b 20 2a 2f 0a 20 20   this lock */.  
c23a0 50 67 6e 6f 20 69 54 61 62 6c 65 3b 20 20 20 20  Pgno iTable;    
c23b0 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61        /* Root pa
c23c0 67 65 20 6f 66 20 74 61 62 6c 65 20 2a 2f 0a 20  ge of table */. 
c23d0 20 75 38 20 65 4c 6f 63 6b 3b 20 20 20 20 20 20   u8 eLock;      
c23e0 20 20 20 20 20 20 20 2f 2a 20 52 45 41 44 5f 4c         /* READ_L
c23f0 4f 43 4b 20 6f 72 20 57 52 49 54 45 5f 4c 4f 43  OCK or WRITE_LOC
c2400 4b 20 2a 2f 0a 20 20 42 74 4c 6f 63 6b 20 2a 70  K */.  BtLock *p
c2410 4e 65 78 74 3b 20 20 20 20 20 20 20 20 2f 2a 20  Next;        /* 
c2420 4e 65 78 74 20 69 6e 20 42 74 53 68 61 72 65 64  Next in BtShared
c2430 2e 70 4c 6f 63 6b 20 6c 69 73 74 20 2a 2f 0a 7d  .pLock list */.}
c2440 3b 0a 0a 2f 2a 20 43 61 6e 64 69 64 61 74 65 20  ;../* Candidate 
c2450 76 61 6c 75 65 73 20 66 6f 72 20 42 74 4c 6f 63  values for BtLoc
c2460 6b 2e 65 4c 6f 63 6b 20 2a 2f 0a 23 64 65 66 69  k.eLock */.#defi
c2470 6e 65 20 52 45 41 44 5f 4c 4f 43 4b 20 20 20 20  ne READ_LOCK    
c2480 20 31 0a 23 64 65 66 69 6e 65 20 57 52 49 54 45   1.#define WRITE
c2490 5f 4c 4f 43 4b 20 20 20 20 32 0a 0a 2f 2a 0a 2a  _LOCK    2../*.*
c24a0 2a 20 54 68 65 73 65 20 6d 61 63 72 6f 73 20 64  * These macros d
c24b0 65 66 69 6e 65 20 74 68 65 20 6c 6f 63 61 74 69  efine the locati
c24c0 6f 6e 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65  on of the pointe
c24d0 72 2d 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 20  r-map entry for 
c24e0 61 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 70  a .** database p
c24f0 61 67 65 2e 20 54 68 65 20 66 69 72 73 74 20 61  age. The first a
c2500 72 67 75 6d 65 6e 74 20 74 6f 20 65 61 63 68 20  rgument to each 
c2510 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
c2520 20 75 73 61 62 6c 65 0a 2a 2a 20 62 79 74 65 73   usable.** bytes
c2530 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 6f 66   on each page of
c2540 20 74 68 65 20 64 61 74 61 62 61 73 65 20 28 6f   the database (o
c2550 66 74 65 6e 20 31 30 32 34 29 2e 20 54 68 65 20  ften 1024). The 
c2560 73 65 63 6f 6e 64 20 69 73 20 74 68 65 0a 2a 2a  second is the.**
c2570 20 70 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20   page number to 
c2580 6c 6f 6f 6b 20 75 70 20 69 6e 20 74 68 65 20 70  look up in the p
c2590 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 2a 2a 0a 2a  ointer map..**.*
c25a0 2a 20 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 20  * PTRMAP_PAGENO 
c25b0 72 65 74 75 72 6e 73 20 74 68 65 20 64 61 74 61  returns the data
c25c0 62 61 73 65 20 70 61 67 65 20 6e 75 6d 62 65 72  base page number
c25d0 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2d   of the pointer-
c25e0 6d 61 70 0a 2a 2a 20 70 61 67 65 20 74 68 61 74  map.** page that
c25f0 20 73 74 6f 72 65 73 20 74 68 65 20 72 65 71 75   stores the requ
c2600 69 72 65 64 20 70 6f 69 6e 74 65 72 2e 20 50 54  ired pointer. PT
c2610 52 4d 41 50 5f 50 54 52 4f 46 46 53 45 54 20 72  RMAP_PTROFFSET r
c2620 65 74 75 72 6e 73 0a 2a 2a 20 74 68 65 20 6f 66  eturns.** the of
c2630 66 73 65 74 20 6f 66 20 74 68 65 20 72 65 71 75  fset of the requ
c2640 65 73 74 65 64 20 6d 61 70 20 65 6e 74 72 79 2e  ested map entry.
c2650 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 67  .**.** If the pg
c2660 6e 6f 20 61 72 67 75 6d 65 6e 74 20 70 61 73 73  no argument pass
c2670 65 64 20 74 6f 20 50 54 52 4d 41 50 5f 50 41 47  ed to PTRMAP_PAG
c2680 45 4e 4f 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ENO is a pointer
c2690 2d 6d 61 70 20 70 61 67 65 2c 0a 2a 2a 20 74 68  -map page,.** th
c26a0 65 6e 20 70 67 6e 6f 20 69 73 20 72 65 74 75 72  en pgno is retur
c26b0 6e 65 64 2e 20 53 6f 20 28 70 67 6e 6f 3d 3d 50  ned. So (pgno==P
c26c0 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 67 73  TRMAP_PAGENO(pgs
c26d0 7a 2c 20 70 67 6e 6f 29 29 20 63 61 6e 20 62 65  z, pgno)) can be
c26e0 0a 2a 2a 20 75 73 65 64 20 74 6f 20 74 65 73 74  .** used to test
c26f0 20 69 66 20 70 67 6e 6f 20 69 73 20 61 20 70 6f   if pgno is a po
c2700 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2e 20  inter-map page. 
c2710 50 54 52 4d 41 50 5f 49 53 50 41 47 45 20 69 6d  PTRMAP_ISPAGE im
c2720 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 69 73  plements.** this
c2730 20 74 65 73 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e   test..*/.#defin
c2740 65 20 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28  e PTRMAP_PAGENO(
c2750 70 42 74 2c 20 70 67 6e 6f 29 20 70 74 72 6d 61  pBt, pgno) ptrma
c2760 70 50 61 67 65 6e 6f 28 70 42 74 2c 20 70 67 6e  pPageno(pBt, pgn
c2770 6f 29 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41  o).#define PTRMA
c2780 50 5f 50 54 52 4f 46 46 53 45 54 28 70 42 74 2c  P_PTROFFSET(pBt,
c2790 20 70 67 6e 6f 29 20 28 35 2a 28 70 67 6e 6f 2d   pgno) (5*(pgno-
c27a0 70 74 72 6d 61 70 50 61 67 65 6e 6f 28 70 42 74  ptrmapPageno(pBt
c27b0 2c 20 70 67 6e 6f 29 2d 31 29 29 0a 23 64 65 66  , pgno)-1)).#def
c27c0 69 6e 65 20 50 54 52 4d 41 50 5f 49 53 50 41 47  ine PTRMAP_ISPAG
c27d0 45 28 70 42 74 2c 20 70 67 6e 6f 29 20 28 50 54  E(pBt, pgno) (PT
c27e0 52 4d 41 50 5f 50 41 47 45 4e 4f 28 28 70 42 74  RMAP_PAGENO((pBt
c27f0 29 2c 28 70 67 6e 6f 29 29 3d 3d 28 70 67 6e 6f  ),(pgno))==(pgno
c2800 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 6f  ))../*.** The po
c2810 69 6e 74 65 72 20 6d 61 70 20 69 73 20 61 20 6c  inter map is a l
c2820 6f 6f 6b 75 70 20 74 61 62 6c 65 20 74 68 61 74  ookup table that
c2830 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20   identifies the 
c2840 70 61 72 65 6e 74 20 70 61 67 65 20 66 6f 72 0a  parent page for.
c2850 2a 2a 20 65 61 63 68 20 63 68 69 6c 64 20 70 61  ** each child pa
c2860 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  ge in the databa
c2870 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20 70 61  se file.  The pa
c2880 72 65 6e 74 20 70 61 67 65 20 69 73 20 74 68 65  rent page is the
c2890 20 70 61 67 65 20 74 68 61 74 0a 2a 2a 20 63 6f   page that.** co
c28a0 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72  ntains a pointer
c28b0 20 74 6f 20 74 68 65 20 63 68 69 6c 64 2e 20 20   to the child.  
c28c0 45 76 65 72 79 20 70 61 67 65 20 69 6e 20 74 68  Every page in th
c28d0 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61  e database conta
c28e0 69 6e 73 0a 2a 2a 20 30 20 6f 72 20 31 20 70 61  ins.** 0 or 1 pa
c28f0 72 65 6e 74 20 70 61 67 65 73 2e 20 20 28 49 6e  rent pages.  (In
c2900 20 74 68 69 73 20 63 6f 6e 74 65 78 74 20 27 64   this context 'd
c2910 61 74 61 62 61 73 65 20 70 61 67 65 27 20 72 65  atabase page' re
c2920 66 65 72 73 0a 2a 2a 20 74 6f 20 61 6e 79 20 70  fers.** to any p
c2930 61 67 65 20 74 68 61 74 20 69 73 20 6e 6f 74 20  age that is not 
c2940 70 61 72 74 20 6f 66 20 74 68 65 20 70 6f 69 6e  part of the poin
c2950 74 65 72 20 6d 61 70 20 69 74 73 65 6c 66 2e 29  ter map itself.)
c2960 20 20 45 61 63 68 20 70 6f 69 6e 74 65 72 20 6d    Each pointer m
c2970 61 70 0a 2a 2a 20 65 6e 74 72 79 20 63 6f 6e 73  ap.** entry cons
c2980 69 73 74 73 20 6f 66 20 61 20 73 69 6e 67 6c 65  ists of a single
c2990 20 62 79 74 65 20 27 74 79 70 65 27 20 61 6e 64   byte 'type' and
c29a0 20 61 20 34 20 62 79 74 65 20 70 61 72 65 6e 74   a 4 byte parent
c29b0 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a   page number..**
c29c0 20 54 68 65 20 50 54 52 4d 41 50 5f 58 58 58 20   The PTRMAP_XXX 
c29d0 69 64 65 6e 74 69 66 69 65 72 73 20 62 65 6c 6f  identifiers belo
c29e0 77 20 61 72 65 20 74 68 65 20 76 61 6c 69 64 20  w are the valid 
c29f0 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  types..**.** The
c2a00 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68 65 20   purpose of the 
c2a10 70 6f 69 6e 74 65 72 20 6d 61 70 20 69 73 20 74  pointer map is t
c2a20 6f 20 66 61 63 69 6c 69 74 79 20 6d 6f 76 69 6e  o facility movin
c2a30 67 20 70 61 67 65 73 20 66 72 6f 6d 20 6f 6e 65  g pages from one
c2a40 0a 2a 2a 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20  .** position in 
c2a50 74 68 65 20 66 69 6c 65 20 74 6f 20 61 6e 6f 74  the file to anot
c2a60 68 65 72 20 61 73 20 70 61 72 74 20 6f 66 20 61  her as part of a
c2a70 75 74 6f 76 61 63 75 75 6d 2e 20 20 57 68 65 6e  utovacuum.  When
c2a80 20 61 20 70 61 67 65 0a 2a 2a 20 69 73 20 6d 6f   a page.** is mo
c2a90 76 65 64 2c 20 74 68 65 20 70 6f 69 6e 74 65 72  ved, the pointer
c2aa0 20 69 6e 20 69 74 73 20 70 61 72 65 6e 74 20 6d   in its parent m
c2ab0 75 73 74 20 62 65 20 75 70 64 61 74 65 64 20 74  ust be updated t
c2ac0 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a  o point to the.*
c2ad0 2a 20 6e 65 77 20 6c 6f 63 61 74 69 6f 6e 2e 20  * new location. 
c2ae0 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70   The pointer map
c2af0 20 69 73 20 75 73 65 64 20 74 6f 20 6c 6f 63 61   is used to loca
c2b00 74 65 20 74 68 65 20 70 61 72 65 6e 74 20 70 61  te the parent pa
c2b10 67 65 20 71 75 69 63 6b 6c 79 2e 0a 2a 2a 0a 2a  ge quickly..**.*
c2b20 2a 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47  * PTRMAP_ROOTPAG
c2b30 45 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  E: The database 
c2b40 70 61 67 65 20 69 73 20 61 20 72 6f 6f 74 2d 70  page is a root-p
c2b50 61 67 65 2e 20 54 68 65 20 70 61 67 65 2d 6e 75  age. The page-nu
c2b60 6d 62 65 72 20 69 73 20 6e 6f 74 0a 2a 2a 20 20  mber is not.**  
c2b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c2b80 75 73 65 64 20 69 6e 20 74 68 69 73 20 63 61 73  used in this cas
c2b90 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f  e..**.** PTRMAP_
c2ba0 46 52 45 45 50 41 47 45 3a 20 54 68 65 20 64 61  FREEPAGE: The da
c2bb0 74 61 62 61 73 65 20 70 61 67 65 20 69 73 20 61  tabase page is a
c2bc0 6e 20 75 6e 75 73 65 64 20 28 66 72 65 65 29 20  n unused (free) 
c2bd0 70 61 67 65 2e 20 54 68 65 20 70 61 67 65 2d 6e  page. The page-n
c2be0 75 6d 62 65 72 20 0a 2a 2a 20 20 20 20 20 20 20  umber .**       
c2bf0 20 20 20 20 20 20 20 20 20 20 20 69 73 20 6e 6f             is no
c2c00 74 20 75 73 65 64 20 69 6e 20 74 68 69 73 20 63  t used in this c
c2c10 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41  ase..**.** PTRMA
c2c20 50 5f 4f 56 45 52 46 4c 4f 57 31 3a 20 54 68 65  P_OVERFLOW1: The
c2c30 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 69   database page i
c2c40 73 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65  s the first page
c2c50 20 69 6e 20 61 20 6c 69 73 74 20 6f 66 20 0a 2a   in a list of .*
c2c60 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
c2c70 20 20 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67      overflow pag
c2c80 65 73 2e 20 54 68 65 20 70 61 67 65 20 6e 75 6d  es. The page num
c2c90 62 65 72 20 69 64 65 6e 74 69 66 69 65 73 20 74  ber identifies t
c2ca0 68 65 20 70 61 67 65 20 74 68 61 74 0a 2a 2a 20  he page that.** 
c2cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c2cc0 20 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63    contains the c
c2cd0 65 6c 6c 20 77 69 74 68 20 61 20 70 6f 69 6e 74  ell with a point
c2ce0 65 72 20 74 6f 20 74 68 69 73 20 6f 76 65 72 66  er to this overf
c2cf0 6c 6f 77 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20  low page..**.** 
c2d00 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32  PTRMAP_OVERFLOW2
c2d10 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70  : The database p
c2d20 61 67 65 20 69 73 20 74 68 65 20 73 65 63 6f 6e  age is the secon
c2d30 64 20 6f 72 20 6c 61 74 65 72 20 70 61 67 65 20  d or later page 
c2d40 69 6e 20 61 20 6c 69 73 74 20 6f 66 0a 2a 2a 20  in a list of.** 
c2d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c2d60 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73    overflow pages
c2d70 2e 20 54 68 65 20 70 61 67 65 2d 6e 75 6d 62 65  . The page-numbe
c2d80 72 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65  r identifies the
c2d90 20 70 72 65 76 69 6f 75 73 0a 2a 2a 20 20 20 20   previous.**    
c2da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
c2db0 61 67 65 20 69 6e 20 74 68 65 20 6f 76 65 72 66  age in the overf
c2dc0 6c 6f 77 20 70 61 67 65 20 6c 69 73 74 2e 0a 2a  low page list..*
c2dd0 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 42 54 52 45  *.** PTRMAP_BTRE
c2de0 45 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  E: The database 
c2df0 70 61 67 65 20 69 73 20 61 20 6e 6f 6e 2d 72 6f  page is a non-ro
c2e00 6f 74 20 62 74 72 65 65 20 70 61 67 65 2e 20 54  ot btree page. T
c2e10 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 2a  he page number.*
c2e20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
c2e30 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 70  identifies the p
c2e40 61 72 65 6e 74 20 70 61 67 65 20 69 6e 20 74 68  arent page in th
c2e50 65 20 62 74 72 65 65 2e 0a 2a 2f 0a 23 64 65 66  e btree..*/.#def
c2e60 69 6e 65 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50  ine PTRMAP_ROOTP
c2e70 41 47 45 20 31 0a 23 64 65 66 69 6e 65 20 50 54  AGE 1.#define PT
c2e80 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20 32 0a  RMAP_FREEPAGE 2.
c2e90 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 4f  #define PTRMAP_O
c2ea0 56 45 52 46 4c 4f 57 31 20 33 0a 23 64 65 66 69  VERFLOW1 3.#defi
c2eb0 6e 65 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c  ne PTRMAP_OVERFL
c2ec0 4f 57 32 20 34 0a 23 64 65 66 69 6e 65 20 50 54  OW2 4.#define PT
c2ed0 52 4d 41 50 5f 42 54 52 45 45 20 35 0a 0a 2f 2a  RMAP_BTREE 5../*
c2ee0 20 41 20 62 75 6e 63 68 20 6f 66 20 61 73 73 65   A bunch of asse
c2ef0 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20  rt() statements 
c2f00 74 6f 20 63 68 65 63 6b 20 74 68 65 20 74 72 61  to check the tra
c2f10 6e 73 61 63 74 69 6f 6e 20 73 74 61 74 65 20 76  nsaction state v
c2f20 61 72 69 61 62 6c 65 73 0a 2a 2a 20 6f 66 20 68  ariables.** of h
c2f30 61 6e 64 6c 65 20 70 20 28 74 79 70 65 20 42 74  andle p (type Bt
c2f40 72 65 65 2a 29 20 61 72 65 20 69 6e 74 65 72 6e  ree*) are intern
c2f50 61 6c 6c 79 20 63 6f 6e 73 69 73 74 65 6e 74 2e  ally consistent.
c2f60 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 62 74 72 65  .*/.#define btre
c2f70 65 49 6e 74 65 67 72 69 74 79 28 70 29 20 5c 0a  eIntegrity(p) \.
c2f80 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54    assert( p->inT
c2f90 72 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45  rans!=TRANS_NONE
c2fa0 20 7c 7c 20 70 2d 3e 70 42 74 2d 3e 6e 54 72 61   || p->pBt->nTra
c2fb0 6e 73 61 63 74 69 6f 6e 3c 70 2d 3e 70 42 74 2d  nsaction<p->pBt-
c2fc0 3e 6e 52 65 66 20 29 3b 20 5c 0a 20 20 61 73 73  >nRef ); \.  ass
c2fd0 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 6e 54 72  ert( p->pBt->nTr
c2fe0 61 6e 73 61 63 74 69 6f 6e 3c 3d 70 2d 3e 70 42  ansaction<=p->pB
c2ff0 74 2d 3e 6e 52 65 66 20 29 3b 20 5c 0a 20 20 61  t->nRef ); \.  a
c3000 73 73 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 69  ssert( p->pBt->i
c3010 6e 54 72 61 6e 73 61 63 74 69 6f 6e 21 3d 54 52  nTransaction!=TR
c3020 41 4e 53 5f 4e 4f 4e 45 20 7c 7c 20 70 2d 3e 70  ANS_NONE || p->p
c3030 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e  Bt->nTransaction
c3040 3d 3d 30 20 29 3b 20 5c 0a 20 20 61 73 73 65 72  ==0 ); \.  asser
c3050 74 28 20 70 2d 3e 70 42 74 2d 3e 69 6e 54 72 61  t( p->pBt->inTra
c3060 6e 73 61 63 74 69 6f 6e 3e 3d 70 2d 3e 69 6e 54  nsaction>=p->inT
c3070 72 61 6e 73 20 29 3b 20 0a 0a 0a 2f 2a 0a 2a 2a  rans ); .../*.**
c3080 20 54 68 65 20 49 53 41 55 54 4f 56 41 43 55 55   The ISAUTOVACUU
c3090 4d 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64 20  M macro is used 
c30a0 77 69 74 68 69 6e 20 62 61 6c 61 6e 63 65 5f 6e  within balance_n
c30b0 6f 6e 72 6f 6f 74 28 29 20 74 6f 20 64 65 74 65  onroot() to dete
c30c0 72 6d 69 6e 65 0a 2a 2a 20 69 66 20 74 68 65 20  rmine.** if the 
c30d0 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 74  database support
c30e0 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6f 72  s auto-vacuum or
c30f0 20 6e 6f 74 2e 20 42 65 63 61 75 73 65 20 69 74   not. Because it
c3100 20 69 73 20 75 73 65 64 0a 2a 2a 20 77 69 74 68   is used.** with
c3110 69 6e 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  in an expression
c3120 20 74 68 61 74 20 69 73 20 61 6e 20 61 72 67 75   that is an argu
c3130 6d 65 6e 74 20 74 6f 20 61 6e 6f 74 68 65 72 20  ment to another 
c3140 6d 61 63 72 6f 20 0a 2a 2a 20 28 73 71 6c 69 74  macro .** (sqlit
c3150 65 4d 61 6c 6c 6f 63 52 61 77 29 2c 20 69 74 20  eMallocRaw), it 
c3160 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  is not possible 
c3170 74 6f 20 75 73 65 20 63 6f 6e 64 69 74 69 6f 6e  to use condition
c3180 61 6c 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 2e 0a  al compilation..
c3190 2a 2a 20 53 6f 2c 20 74 68 69 73 20 6d 61 63 72  ** So, this macr
c31a0 6f 20 69 73 20 64 65 66 69 6e 65 64 20 69 6e 73  o is defined ins
c31b0 74 65 61 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  tead..*/.#ifndef
c31c0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
c31d0 4f 56 41 43 55 55 4d 0a 23 64 65 66 69 6e 65 20  OVACUUM.#define 
c31e0 49 53 41 55 54 4f 56 41 43 55 55 4d 20 28 70 42  ISAUTOVACUUM (pB
c31f0 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 0a 23  t->autoVacuum).#
c3200 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 49 53 41  else.#define ISA
c3210 55 54 4f 56 41 43 55 55 4d 20 30 0a 23 65 6e 64  UTOVACUUM 0.#end
c3220 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  if.../*.** This 
c3230 73 74 72 75 63 74 75 72 65 20 69 73 20 70 61 73  structure is pas
c3240 73 65 64 20 61 72 6f 75 6e 64 20 74 68 72 6f 75  sed around throu
c3250 67 68 20 61 6c 6c 20 74 68 65 20 73 61 6e 69 74  gh all the sanit
c3260 79 20 63 68 65 63 6b 69 6e 67 20 72 6f 75 74 69  y checking routi
c3270 6e 65 73 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20  nes.** in order 
c3280 74 6f 20 6b 65 65 70 20 74 72 61 63 6b 20 6f 66  to keep track of
c3290 20 73 6f 6d 65 20 67 6c 6f 62 61 6c 20 73 74 61   some global sta
c32a0 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  te information..
c32b0 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
c32c0 74 20 49 6e 74 65 67 72 69 74 79 43 6b 20 49 6e  t IntegrityCk In
c32d0 74 65 67 72 69 74 79 43 6b 3b 0a 73 74 72 75 63  tegrityCk;.struc
c32e0 74 20 49 6e 74 65 67 72 69 74 79 43 6b 20 7b 0a  t IntegrityCk {.
c32f0 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b    BtShared *pBt;
c3300 20 20 20 20 2f 2a 20 54 68 65 20 74 72 65 65 20      /* The tree 
c3310 62 65 69 6e 67 20 63 68 65 63 6b 65 64 20 6f 75  being checked ou
c3320 74 20 2a 2f 0a 20 20 50 61 67 65 72 20 2a 70 50  t */.  Pager *pP
c3330 61 67 65 72 3b 20 20 20 20 2f 2a 20 54 68 65 20  ager;    /* The 
c3340 61 73 73 6f 63 69 61 74 65 64 20 70 61 67 65 72  associated pager
c3350 2e 20 20 41 6c 73 6f 20 61 63 63 65 73 73 69 62  .  Also accessib
c3360 6c 65 20 62 79 20 70 42 74 2d 3e 70 50 61 67 65  le by pBt->pPage
c3370 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 67 65  r */.  int nPage
c3380 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  ;        /* Numb
c3390 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74  er of pages in t
c33a0 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  he database */. 
c33b0 20 69 6e 74 20 2a 61 6e 52 65 66 3b 20 20 20 20   int *anRef;    
c33c0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
c33d0 74 69 6d 65 73 20 65 61 63 68 20 70 61 67 65 20  times each page 
c33e0 69 73 20 72 65 66 65 72 65 6e 63 65 64 20 2a 2f  is referenced */
c33f0 0a 20 20 69 6e 74 20 6d 78 45 72 72 3b 20 20 20  .  int mxErr;   
c3400 20 20 20 20 20 2f 2a 20 53 74 6f 70 20 61 63 63       /* Stop acc
c3410 75 6d 75 6c 61 74 69 6e 67 20 65 72 72 6f 72 73  umulating errors
c3420 20 77 68 65 6e 20 74 68 69 73 20 72 65 61 63 68   when this reach
c3430 65 73 20 7a 65 72 6f 20 2a 2f 0a 20 20 63 68 61  es zero */.  cha
c3440 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 2f  r *zErrMsg;    /
c3450 2a 20 41 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  * An error messa
c3460 67 65 2e 20 20 4e 55 4c 4c 20 69 66 20 6e 6f 20  ge.  NULL if no 
c3470 65 72 72 6f 72 73 20 73 65 65 6e 2e 20 2a 2f 0a  errors seen. */.
c3480 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 20 20 20    int nErr;     
c3490 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
c34a0 20 6d 65 73 73 61 67 65 73 20 77 72 69 74 74 65   messages writte
c34b0 6e 20 74 6f 20 7a 45 72 72 4d 73 67 20 73 6f 20  n to zErrMsg so 
c34c0 66 61 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  far */.};../*.**
c34d0 20 52 65 61 64 20 6f 72 20 77 72 69 74 65 20 61   Read or write a
c34e0 20 74 77 6f 2d 20 61 6e 64 20 66 6f 75 72 2d 62   two- and four-b
c34f0 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69  yte big-endian i
c3500 6e 74 65 67 65 72 20 76 61 6c 75 65 73 2e 0a 2a  nteger values..*
c3510 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 32 62 79  /.#define get2by
c3520 74 65 28 78 29 20 20 20 28 28 78 29 5b 30 5d 3c  te(x)   ((x)[0]<
c3530 3c 38 20 7c 20 28 78 29 5b 31 5d 29 0a 23 64 65  <8 | (x)[1]).#de
c3540 66 69 6e 65 20 70 75 74 32 62 79 74 65 28 70 2c  fine put2byte(p,
c3550 76 29 20 28 28 70 29 5b 30 5d 20 3d 20 28 76 29  v) ((p)[0] = (v)
c3560 3e 3e 38 2c 20 28 70 29 5b 31 5d 20 3d 20 28 76  >>8, (p)[1] = (v
c3570 29 29 0a 23 64 65 66 69 6e 65 20 67 65 74 34 62  )).#define get4b
c3580 79 74 65 20 73 71 6c 69 74 65 33 47 65 74 34 62  yte sqlite3Get4b
c3590 79 74 65 0a 23 64 65 66 69 6e 65 20 70 75 74 34  yte.#define put4
c35a0 62 79 74 65 20 73 71 6c 69 74 65 33 50 75 74 34  byte sqlite3Put4
c35b0 62 79 74 65 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65  byte../*.** Inte
c35c0 72 6e 61 6c 20 72 6f 75 74 69 6e 65 73 20 74 68  rnal routines th
c35d0 61 74 20 73 68 6f 75 6c 64 20 62 65 20 61 63 63  at should be acc
c35e0 65 73 73 65 64 20 62 79 20 74 68 65 20 62 74 72  essed by the btr
c35f0 65 65 20 6c 61 79 65 72 20 6f 6e 6c 79 2e 0a 2a  ee layer only..*
c3600 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
c3610 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
c3620 65 47 65 74 50 61 67 65 28 42 74 53 68 61 72 65  eGetPage(BtShare
c3630 64 2a 2c 20 50 67 6e 6f 2c 20 4d 65 6d 50 61 67  d*, Pgno, MemPag
c3640 65 2a 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  e**, int);.SQLIT
c3650 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
c3660 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61  lite3BtreeInitPa
c3670 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  ge(MemPage *pPag
c3680 65 2c 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 72  e, MemPage *pPar
c3690 65 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ent);.SQLITE_PRI
c36a0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
c36b0 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50  3BtreeParseCellP
c36c0 74 72 28 4d 65 6d 50 61 67 65 2a 2c 20 75 38 2a  tr(MemPage*, u8*
c36d0 2c 20 43 65 6c 6c 49 6e 66 6f 2a 29 3b 0a 53 51  , CellInfo*);.SQ
c36e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
c36f0 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61  d sqlite3BtreePa
c3700 72 73 65 43 65 6c 6c 28 4d 65 6d 50 61 67 65 2a  rseCell(MemPage*
c3710 2c 20 69 6e 74 2c 20 43 65 6c 6c 49 6e 66 6f 2a  , int, CellInfo*
c3720 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
c3730 45 20 75 38 20 2a 73 71 6c 69 74 65 33 42 74 72  E u8 *sqlite3Btr
c3740 65 65 46 69 6e 64 43 65 6c 6c 28 4d 65 6d 50 61  eeFindCell(MemPa
c3750 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 69  ge *pPage, int i
c3760 43 65 6c 6c 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Cell);.SQLITE_PR
c3770 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
c3780 33 42 74 72 65 65 52 65 73 74 6f 72 65 4f 72 43  3BtreeRestoreOrC
c3790 6c 65 61 72 43 75 72 73 6f 72 50 6f 73 69 74 69  learCursorPositi
c37a0 6f 6e 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  on(BtCursor *pCu
c37b0 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  r);.SQLITE_PRIVA
c37c0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
c37d0 74 72 65 65 47 65 74 54 65 6d 70 43 75 72 73 6f  treeGetTempCurso
c37e0 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  r(BtCursor *pCur
c37f0 2c 20 42 74 43 75 72 73 6f 72 20 2a 70 54 65 6d  , BtCursor *pTem
c3800 70 43 75 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52  pCur);.SQLITE_PR
c3810 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
c3820 65 33 42 74 72 65 65 52 65 6c 65 61 73 65 54 65  e3BtreeReleaseTe
c3830 6d 70 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f  mpCursor(BtCurso
c3840 72 20 2a 70 43 75 72 29 3b 0a 53 51 4c 49 54 45  r *pCur);.SQLITE
c3850 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
c3860 69 74 65 33 42 74 72 65 65 49 73 52 6f 6f 74 50  ite3BtreeIsRootP
c3870 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61  age(MemPage *pPa
c3880 67 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  ge);.SQLITE_PRIV
c3890 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
c38a0 42 74 72 65 65 4d 6f 76 65 54 6f 50 61 72 65 6e  BtreeMoveToParen
c38b0 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  t(BtCursor *pCur
c38c0 29 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  );../***********
c38d0 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 72 65 65  *** End of btree
c38e0 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
c38f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c3900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c3910 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
c3920 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77  *** Continuing w
c3930 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66  here we left off
c3940 20 69 6e 20 62 74 72 65 65 2e 63 20 2a 2a 2a 2a   in btree.c ****
c3950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c3960 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68  **/../*.** The h
c3970 65 61 64 65 72 20 73 74 72 69 6e 67 20 74 68 61  eader string tha
c3980 74 20 61 70 70 65 61 72 73 20 61 74 20 74 68 65  t appears at the
c3990 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 65 76   beginning of ev
c39a0 65 72 79 0a 2a 2a 20 53 51 4c 69 74 65 20 64 61  ery.** SQLite da
c39b0 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69  tabase..*/.stati
c39c0 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 4d 61  c const char zMa
c39d0 67 69 63 48 65 61 64 65 72 5b 5d 20 3d 20 53 51  gicHeader[] = SQ
c39e0 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52  LITE_FILE_HEADER
c39f0 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68  ;.../*.** Set th
c3a00 69 73 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62  is global variab
c3a10 6c 65 20 74 6f 20 31 20 74 6f 20 65 6e 61 62 6c  le to 1 to enabl
c3a20 65 20 74 72 61 63 69 6e 67 20 75 73 69 6e 67 20  e tracing using 
c3a30 74 68 65 20 54 52 41 43 45 0a 2a 2a 20 6d 61 63  the TRACE.** mac
c3a40 72 6f 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54  ro..*/.#if SQLIT
c3a50 45 5f 54 45 53 54 0a 69 6e 74 20 73 71 6c 69 74  E_TEST.int sqlit
c3a60 65 33 5f 62 74 72 65 65 5f 74 72 61 63 65 3d 30  e3_btree_trace=0
c3a70 3b 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e  ;  /* True to en
c3a80 61 62 6c 65 20 74 72 61 63 69 6e 67 20 2a 2f 0a  able tracing */.
c3a90 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 6f  #endif../*.** Fo
c3aa0 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f  rward declaratio
c3ab0 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  n.*/.static int 
c3ac0 63 68 65 63 6b 52 65 61 64 4c 6f 63 6b 73 28 42  checkReadLocks(B
c3ad0 74 72 65 65 2a 2c 50 67 6e 6f 2c 42 74 43 75 72  tree*,Pgno,BtCur
c3ae0 73 6f 72 2a 29 3b 0a 0a 0a 23 69 66 64 65 66 20  sor*);...#ifdef 
c3af0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
c3b00 45 44 5f 43 41 43 48 45 0a 20 20 2f 2a 0a 20 20  ED_CACHE.  /*.  
c3b10 2a 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 73  ** The functions
c3b20 20 71 75 65 72 79 54 61 62 6c 65 4c 6f 63 6b 28   queryTableLock(
c3b30 29 2c 20 6c 6f 63 6b 54 61 62 6c 65 28 29 20 61  ), lockTable() a
c3b40 6e 64 20 75 6e 6c 6f 63 6b 41 6c 6c 54 61 62 6c  nd unlockAllTabl
c3b50 65 73 28 29 0a 20 20 2a 2a 20 6d 61 6e 69 70 75  es().  ** manipu
c3b60 6c 61 74 65 20 65 6e 74 72 69 65 73 20 69 6e 20  late entries in 
c3b70 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 4c 6f  the BtShared.pLo
c3b80 63 6b 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 75  ck linked list u
c3b90 73 65 64 20 74 6f 20 73 74 6f 72 65 0a 20 20 2a  sed to store.  *
c3ba0 2a 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 74  * shared-cache t
c3bb0 61 62 6c 65 20 6c 65 76 65 6c 20 6c 6f 63 6b 73  able level locks
c3bc0 2e 20 49 66 20 74 68 65 20 6c 69 62 72 61 72 79  . If the library
c3bd0 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74   is compiled wit
c3be0 68 20 74 68 65 0a 20 20 2a 2a 20 73 68 61 72 65  h the.  ** share
c3bf0 64 2d 63 61 63 68 65 20 66 65 61 74 75 72 65 20  d-cache feature 
c3c00 64 69 73 61 62 6c 65 64 2c 20 74 68 65 6e 20 74  disabled, then t
c3c10 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 65 76 65  here is only eve
c3c20 72 20 6f 6e 65 20 75 73 65 72 0a 20 20 2a 2a 20  r one user.  ** 
c3c30 6f 66 20 65 61 63 68 20 42 74 53 68 61 72 65 64  of each BtShared
c3c40 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 73   structure and s
c3c50 6f 20 74 68 69 73 20 6c 6f 63 6b 69 6e 67 20 69  o this locking i
c3c60 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 2e  s not necessary.
c3c70 20 0a 20 20 2a 2a 20 53 6f 20 64 65 66 69 6e 65   .  ** So define
c3c80 20 74 68 65 20 6c 6f 63 6b 20 72 65 6c 61 74 65   the lock relate
c3c90 64 20 66 75 6e 63 74 69 6f 6e 73 20 61 73 20 6e  d functions as n
c3ca0 6f 2d 6f 70 73 2e 0a 20 20 2a 2f 0a 20 20 23 64  o-ops..  */.  #d
c3cb0 65 66 69 6e 65 20 71 75 65 72 79 54 61 62 6c 65  efine queryTable
c3cc0 4c 6f 63 6b 28 61 2c 62 2c 63 29 20 53 51 4c 49  Lock(a,b,c) SQLI
c3cd0 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20  TE_OK.  #define 
c3ce0 6c 6f 63 6b 54 61 62 6c 65 28 61 2c 62 2c 63 29  lockTable(a,b,c)
c3cf0 20 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 65   SQLITE_OK.  #de
c3d00 66 69 6e 65 20 75 6e 6c 6f 63 6b 41 6c 6c 54 61  fine unlockAllTa
c3d10 62 6c 65 73 28 61 29 0a 23 65 6c 73 65 0a 0a 2f  bles(a).#else../
c3d20 2a 0a 2a 2a 20 51 75 65 72 79 20 74 6f 20 73 65  *.** Query to se
c3d30 65 20 69 66 20 62 74 72 65 65 20 68 61 6e 64 6c  e if btree handl
c3d40 65 20 70 20 6d 61 79 20 6f 62 74 61 69 6e 20 61  e p may obtain a
c3d50 20 6c 6f 63 6b 20 6f 66 20 74 79 70 65 20 65 4c   lock of type eL
c3d60 6f 63 6b 20 0a 2a 2a 20 28 52 45 41 44 5f 4c 4f  ock .** (READ_LO
c3d70 43 4b 20 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b  CK or WRITE_LOCK
c3d80 29 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77  ) on the table w
c3d90 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 54  ith root-page iT
c3da0 61 62 2e 20 52 65 74 75 72 6e 0a 2a 2a 20 53 51  ab. Return.** SQ
c3db0 4c 49 54 45 5f 4f 4b 20 69 66 20 74 68 65 20 6c  LITE_OK if the l
c3dc0 6f 63 6b 20 6d 61 79 20 62 65 20 6f 62 74 61 69  ock may be obtai
c3dd0 6e 65 64 20 28 62 79 20 63 61 6c 6c 69 6e 67 20  ned (by calling 
c3de0 6c 6f 63 6b 54 61 62 6c 65 28 29 29 2c 20 6f 72  lockTable()), or
c3df0 0a 2a 2a 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  .** SQLITE_LOCKE
c3e00 44 20 69 66 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61  D if not..*/.sta
c3e10 74 69 63 20 69 6e 74 20 71 75 65 72 79 54 61 62  tic int queryTab
c3e20 6c 65 4c 6f 63 6b 28 42 74 72 65 65 20 2a 70 2c  leLock(Btree *p,
c3e30 20 50 67 6e 6f 20 69 54 61 62 2c 20 75 38 20 65   Pgno iTab, u8 e
c3e40 4c 6f 63 6b 29 7b 0a 20 20 42 74 53 68 61 72 65  Lock){.  BtShare
c3e50 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
c3e60 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 49 74 65 72  .  BtLock *pIter
c3e70 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 69 73 20  ;..  /* This is 
c3e80 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 73  a no-op if the s
c3e90 68 61 72 65 64 2d 63 61 63 68 65 20 69 73 20 6e  hared-cache is n
c3ea0 6f 74 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20  ot enabled */.  
c3eb0 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 54 68  if( 0==sqlite3Th
c3ec0 72 65 61 64 44 61 74 61 52 65 61 64 4f 6e 6c 79  readDataReadOnly
c3ed0 28 29 2d 3e 75 73 65 53 68 61 72 65 64 44 61 74  ()->useSharedDat
c3ee0 61 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  a ){.    return 
c3ef0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a  SQLITE_OK;.  }..
c3f00 20 20 2f 2a 20 54 68 69 73 20 28 61 6c 6f 6e 67    /* This (along
c3f10 20 77 69 74 68 20 6c 6f 63 6b 54 61 62 6c 65 28   with lockTable(
c3f20 29 29 20 69 73 20 77 68 65 72 65 20 74 68 65 20  )) is where the 
c3f30 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20  ReadUncommitted 
c3f40 66 6c 61 67 20 69 73 0a 20 20 2a 2a 20 64 65 61  flag is.  ** dea
c3f50 6c 74 20 77 69 74 68 2e 20 49 66 20 74 68 65 20  lt with. If the 
c3f60 63 61 6c 6c 65 72 20 69 73 20 71 75 65 72 79 69  caller is queryi
c3f70 6e 67 20 66 6f 72 20 61 20 72 65 61 64 2d 6c 6f  ng for a read-lo
c3f80 63 6b 20 61 6e 64 20 74 68 65 20 66 6c 61 67 20  ck and the flag 
c3f90 69 73 0a 20 20 2a 2a 20 73 65 74 2c 20 69 74 20  is.  ** set, it 
c3fa0 69 73 20 75 6e 63 6f 6e 64 69 74 69 6f 6e 61 6c  is unconditional
c3fb0 6c 79 20 67 72 61 6e 74 65 64 20 2d 20 65 76 65  ly granted - eve
c3fc0 6e 20 69 66 20 74 68 65 72 65 20 61 72 65 20 77  n if there are w
c3fd0 72 69 74 65 2d 6c 6f 63 6b 73 0a 20 20 2a 2a 20  rite-locks.  ** 
c3fe0 6f 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 49 66  on the table. If
c3ff0 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 69 73   a write-lock is
c4000 20 72 65 71 75 65 73 74 65 64 2c 20 74 68 65 20   requested, the 
c4010 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20  ReadUncommitted 
c4020 66 6c 61 67 0a 20 20 2a 2a 20 69 73 20 6e 6f 74  flag.  ** is not
c4030 20 63 6f 6e 73 69 64 65 72 65 64 2e 0a 20 20 2a   considered..  *
c4040 2a 0a 20 20 2a 2a 20 49 6e 20 66 75 6e 63 74 69  *.  ** In functi
c4050 6f 6e 20 6c 6f 63 6b 54 61 62 6c 65 28 29 2c 20  on lockTable(), 
c4060 69 66 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 69  if a read-lock i
c4070 73 20 64 65 6d 61 6e 64 65 64 20 61 6e 64 20 74  s demanded and t
c4080 68 65 20 0a 20 20 2a 2a 20 52 65 61 64 55 6e 63  he .  ** ReadUnc
c4090 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 20 69 73  ommitted flag is
c40a0 20 73 65 74 2c 20 6e 6f 20 65 6e 74 72 79 20 69   set, no entry i
c40b0 73 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 6c  s added to the l
c40c0 6f 63 6b 73 20 6c 69 73 74 20 0a 20 20 2a 2a 20  ocks list .  ** 
c40d0 28 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 29  (BtShared.pLock)
c40e0 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 6f 20 73  ..  **.  ** To s
c40f0 75 6d 6d 61 72 69 7a 65 3a 20 49 66 20 74 68 65  ummarize: If the
c4100 20 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64   ReadUncommitted
c4110 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68   flag is set, th
c4120 65 6e 20 72 65 61 64 20 63 75 72 73 6f 72 73 20  en read cursors 
c4130 64 6f 0a 20 20 2a 2a 20 6e 6f 74 20 63 72 65 61  do.  ** not crea
c4140 74 65 20 6f 72 20 72 65 73 70 65 63 74 20 74 61  te or respect ta
c4150 62 6c 65 20 6c 6f 63 6b 73 2e 20 54 68 65 20 6c  ble locks. The l
c4160 6f 63 6b 69 6e 67 20 70 72 6f 63 65 64 75 72 65  ocking procedure
c4170 20 66 6f 72 20 61 20 0a 20 20 2a 2a 20 77 72 69   for a .  ** wri
c4180 74 65 2d 63 75 72 73 6f 72 20 64 6f 65 73 20 6e  te-cursor does n
c4190 6f 74 20 63 68 61 6e 67 65 2e 0a 20 20 2a 2f 0a  ot change..  */.
c41a0 20 20 69 66 28 20 0a 20 20 20 20 21 70 2d 3e 70    if( .    !p->p
c41b0 53 71 6c 69 74 65 20 7c 7c 20 0a 20 20 20 20 30  Sqlite || .    0
c41c0 3d 3d 28 70 2d 3e 70 53 71 6c 69 74 65 2d 3e 66  ==(p->pSqlite->f
c41d0 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 65 61 64  lags&SQLITE_Read
c41e0 55 6e 63 6f 6d 6d 69 74 74 65 64 29 20 7c 7c 20  Uncommitted) || 
c41f0 0a 20 20 20 20 65 4c 6f 63 6b 3d 3d 57 52 49 54  .    eLock==WRIT
c4200 45 5f 4c 4f 43 4b 20 7c 7c 0a 20 20 20 20 69 54  E_LOCK ||.    iT
c4210 61 62 3d 3d 4d 41 53 54 45 52 5f 52 4f 4f 54 0a  ab==MASTER_ROOT.
c4220 20 20 29 7b 0a 20 20 20 20 66 6f 72 28 70 49 74    ){.    for(pIt
c4230 65 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70  er=pBt->pLock; p
c4240 49 74 65 72 3b 20 70 49 74 65 72 3d 70 49 74 65  Iter; pIter=pIte
c4250 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20  r->pNext){.     
c4260 20 69 66 28 20 70 49 74 65 72 2d 3e 70 42 74 72   if( pIter->pBtr
c4270 65 65 21 3d 70 20 26 26 20 70 49 74 65 72 2d 3e  ee!=p && pIter->
c4280 69 54 61 62 6c 65 3d 3d 69 54 61 62 20 26 26 20  iTable==iTab && 
c4290 0a 20 20 20 20 20 20 20 20 20 20 28 70 49 74 65  .          (pIte
c42a0 72 2d 3e 65 4c 6f 63 6b 21 3d 65 4c 6f 63 6b 20  r->eLock!=eLock 
c42b0 7c 7c 20 65 4c 6f 63 6b 21 3d 52 45 41 44 5f 4c  || eLock!=READ_L
c42c0 4f 43 4b 29 20 29 7b 0a 20 20 20 20 20 20 20 20  OCK) ){.        
c42d0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f  return SQLITE_LO
c42e0 43 4b 45 44 3b 0a 20 20 20 20 20 20 7d 0a 20 20  CKED;.      }.  
c42f0 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
c4300 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
c4310 2a 0a 2a 2a 20 41 64 64 20 61 20 6c 6f 63 6b 20  *.** Add a lock 
c4320 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69 74  on the table wit
c4330 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62  h root-page iTab
c4340 6c 65 20 74 6f 20 74 68 65 20 73 68 61 72 65 64  le to the shared
c4350 2d 62 74 72 65 65 20 75 73 65 64 0a 2a 2a 20 62  -btree used.** b
c4360 79 20 42 74 72 65 65 20 68 61 6e 64 6c 65 20 70  y Btree handle p
c4370 2e 20 50 61 72 61 6d 65 74 65 72 20 65 4c 6f 63  . Parameter eLoc
c4380 6b 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72  k must be either
c4390 20 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 0a 2a   READ_LOCK or .*
c43a0 2a 20 57 52 49 54 45 5f 4c 4f 43 4b 2e 0a 2a 2a  * WRITE_LOCK..**
c43b0 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  .** SQLITE_OK is
c43c0 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65   returned if the
c43d0 20 6c 6f 63 6b 20 69 73 20 61 64 64 65 64 20 73   lock is added s
c43e0 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 53 51 4c  uccessfully. SQL
c43f0 49 54 45 5f 42 55 53 59 20 61 6e 64 0a 2a 2a 20  ITE_BUSY and.** 
c4400 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 6d 61 79  SQLITE_NOMEM may
c4410 20 61 6c 73 6f 20 62 65 20 72 65 74 75 72 6e 65   also be returne
c4420 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
c4430 20 6c 6f 63 6b 54 61 62 6c 65 28 42 74 72 65 65   lockTable(Btree
c4440 20 2a 70 2c 20 50 67 6e 6f 20 69 54 61 62 6c 65   *p, Pgno iTable
c4450 2c 20 75 38 20 65 4c 6f 63 6b 29 7b 0a 20 20 42  , u8 eLock){.  B
c4460 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
c4470 2d 3e 70 42 74 3b 0a 20 20 42 74 4c 6f 63 6b 20  ->pBt;.  BtLock 
c4480 2a 70 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 42 74  *pLock = 0;.  Bt
c4490 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a 0a 20 20  Lock *pIter;..  
c44a0 2f 2a 20 54 68 69 73 20 69 73 20 61 20 6e 6f 2d  /* This is a no-
c44b0 6f 70 20 69 66 20 74 68 65 20 73 68 61 72 65 64  op if the shared
c44c0 2d 63 61 63 68 65 20 69 73 20 6e 6f 74 20 65 6e  -cache is not en
c44d0 61 62 6c 65 64 20 2a 2f 0a 20 20 69 66 28 20 30  abled */.  if( 0
c44e0 3d 3d 73 71 6c 69 74 65 33 54 68 72 65 61 64 44  ==sqlite3ThreadD
c44f0 61 74 61 52 65 61 64 4f 6e 6c 79 28 29 2d 3e 75  ataReadOnly()->u
c4500 73 65 53 68 61 72 65 64 44 61 74 61 20 29 7b 0a  seSharedData ){.
c4510 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
c4520 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 61 73 73  E_OK;.  }..  ass
c4530 65 72 74 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d  ert( SQLITE_OK==
c4540 71 75 65 72 79 54 61 62 6c 65 4c 6f 63 6b 28 70  queryTableLock(p
c4550 2c 20 69 54 61 62 6c 65 2c 20 65 4c 6f 63 6b 29  , iTable, eLock)
c4560 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65   );..  /* If the
c4570 20 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74 74 65   read-uncommitte
c4580 64 20 66 6c 61 67 20 69 73 20 73 65 74 20 61 6e  d flag is set an
c4590 64 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 69 73  d a read-lock is
c45a0 20 72 65 71 75 65 73 74 65 64 2c 0a 20 20 2a 2a   requested,.  **
c45b0 20 72 65 74 75 72 6e 20 65 61 72 6c 79 20 77 69   return early wi
c45c0 74 68 6f 75 74 20 61 64 64 69 6e 67 20 61 6e 20  thout adding an 
c45d0 65 6e 74 72 79 20 74 6f 20 74 68 65 20 42 74 53  entry to the BtS
c45e0 68 61 72 65 64 2e 70 4c 6f 63 6b 20 6c 69 73 74  hared.pLock list
c45f0 2e 20 53 65 65 0a 20 20 2a 2a 20 63 6f 6d 6d 65  . See.  ** comme
c4600 6e 74 20 69 6e 20 66 75 6e 63 74 69 6f 6e 20 71  nt in function q
c4610 75 65 72 79 54 61 62 6c 65 4c 6f 63 6b 28 29 20  ueryTableLock() 
c4620 66 6f 72 20 6d 6f 72 65 20 69 6e 66 6f 20 6f 6e  for more info on
c4630 20 68 61 6e 64 6c 69 6e 67 20 0a 20 20 2a 2a 20   handling .  ** 
c4640 74 68 65 20 52 65 61 64 55 6e 63 6f 6d 6d 69 74  the ReadUncommit
c4650 74 65 64 20 66 6c 61 67 2e 0a 20 20 2a 2f 0a 20  ted flag..  */. 
c4660 20 69 66 28 20 0a 20 20 20 20 28 70 2d 3e 70 53   if( .    (p->pS
c4670 71 6c 69 74 65 29 20 26 26 20 0a 20 20 20 20 28  qlite) && .    (
c4680 70 2d 3e 70 53 71 6c 69 74 65 2d 3e 66 6c 61 67  p->pSqlite->flag
c4690 73 26 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63  s&SQLITE_ReadUnc
c46a0 6f 6d 6d 69 74 74 65 64 29 20 26 26 20 0a 20 20  ommitted) && .  
c46b0 20 20 28 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c    (eLock==READ_L
c46c0 4f 43 4b 29 20 26 26 0a 20 20 20 20 69 54 61 62  OCK) &&.    iTab
c46d0 6c 65 21 3d 4d 41 53 54 45 52 5f 52 4f 4f 54 0a  le!=MASTER_ROOT.
c46e0 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20    ){.    return 
c46f0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a  SQLITE_OK;.  }..
c4700 20 20 2f 2a 20 46 69 72 73 74 20 73 65 61 72 63    /* First searc
c4710 68 20 74 68 65 20 6c 69 73 74 20 66 6f 72 20 61  h the list for a
c4720 6e 20 65 78 69 73 74 69 6e 67 20 6c 6f 63 6b 20  n existing lock 
c4730 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2e 20 2a  on this table. *
c4740 2f 0a 20 20 66 6f 72 28 70 49 74 65 72 3d 70 42  /.  for(pIter=pB
c4750 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b  t->pLock; pIter;
c4760 20 70 49 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e   pIter=pIter->pN
c4770 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 49  ext){.    if( pI
c4780 74 65 72 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61  ter->iTable==iTa
c4790 62 6c 65 20 26 26 20 70 49 74 65 72 2d 3e 70 42  ble && pIter->pB
c47a0 74 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20  tree==p ){.     
c47b0 20 70 4c 6f 63 6b 20 3d 20 70 49 74 65 72 3b 0a   pLock = pIter;.
c47c0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
c47d0 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20   }.  }..  /* If 
c47e0 74 68 65 20 61 62 6f 76 65 20 73 65 61 72 63 68  the above search
c47f0 20 64 69 64 20 6e 6f 74 20 66 69 6e 64 20 61 20   did not find a 
c4800 42 74 4c 6f 63 6b 20 73 74 72 75 63 74 20 61 73  BtLock struct as
c4810 73 6f 63 69 61 74 69 6e 67 20 42 74 72 65 65 20  sociating Btree 
c4820 70 0a 20 20 2a 2a 20 77 69 74 68 20 74 61 62 6c  p.  ** with tabl
c4830 65 20 69 54 61 62 6c 65 2c 20 61 6c 6c 6f 63 61  e iTable, alloca
c4840 74 65 20 6f 6e 65 20 61 6e 64 20 6c 69 6e 6b 20  te one and link 
c4850 69 74 20 69 6e 74 6f 20 74 68 65 20 6c 69 73 74  it into the list
c4860 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 4c  ..  */.  if( !pL
c4870 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b  ock ){.    pLock
c4880 20 3d 20 28 42 74 4c 6f 63 6b 20 2a 29 73 71 6c   = (BtLock *)sql
c4890 69 74 65 4d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66  iteMalloc(sizeof
c48a0 28 42 74 4c 6f 63 6b 29 29 3b 0a 20 20 20 20 69  (BtLock));.    i
c48b0 66 28 20 21 70 4c 6f 63 6b 20 29 7b 0a 20 20 20  f( !pLock ){.   
c48c0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
c48d0 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20  _NOMEM;.    }.  
c48e0 20 20 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65 20    pLock->iTable 
c48f0 3d 20 69 54 61 62 6c 65 3b 0a 20 20 20 20 70 4c  = iTable;.    pL
c4900 6f 63 6b 2d 3e 70 42 74 72 65 65 20 3d 20 70 3b  ock->pBtree = p;
c4910 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78  .    pLock->pNex
c4920 74 20 3d 20 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a  t = pBt->pLock;.
c4930 20 20 20 20 70 42 74 2d 3e 70 4c 6f 63 6b 20 3d      pBt->pLock =
c4940 20 70 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f   pLock;.  }..  /
c4950 2a 20 53 65 74 20 74 68 65 20 42 74 4c 6f 63 6b  * Set the BtLock
c4960 2e 65 4c 6f 63 6b 20 76 61 72 69 61 62 6c 65 20  .eLock variable 
c4970 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f  to the maximum o
c4980 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 6f  f the current lo
c4990 63 6b 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20  ck.  ** and the 
c49a0 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 2e 20  requested lock. 
c49b0 54 68 69 73 20 6d 65 61 6e 73 20 69 66 20 61 20  This means if a 
c49c0 77 72 69 74 65 2d 6c 6f 63 6b 20 77 61 73 20 61  write-lock was a
c49d0 6c 72 65 61 64 79 20 68 65 6c 64 0a 20 20 2a 2a  lready held.  **
c49e0 20 61 6e 64 20 61 20 72 65 61 64 2d 6c 6f 63 6b   and a read-lock
c49f0 20 72 65 71 75 65 73 74 65 64 2c 20 77 65 20 64   requested, we d
c4a00 6f 6e 27 74 20 69 6e 63 6f 72 72 65 63 74 6c 79  on't incorrectly
c4a10 20 64 6f 77 6e 67 72 61 64 65 20 74 68 65 20 6c   downgrade the l
c4a20 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ock..  */.  asse
c4a30 72 74 28 20 57 52 49 54 45 5f 4c 4f 43 4b 3e 52  rt( WRITE_LOCK>R
c4a40 45 41 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66  EAD_LOCK );.  if
c4a50 28 20 65 4c 6f 63 6b 3e 70 4c 6f 63 6b 2d 3e 65  ( eLock>pLock->e
c4a60 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63  Lock ){.    pLoc
c4a70 6b 2d 3e 65 4c 6f 63 6b 20 3d 20 65 4c 6f 63 6b  k->eLock = eLock
c4a80 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
c4a90 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
c4aa0 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 6c 6c 20  .** Release all 
c4ab0 74 68 65 20 74 61 62 6c 65 20 6c 6f 63 6b 73 20  the table locks 
c4ac0 28 6c 6f 63 6b 73 20 6f 62 74 61 69 6e 65 64 20  (locks obtained 
c4ad0 76 69 61 20 63 61 6c 6c 73 20 74 6f 20 74 68 65  via calls to the
c4ae0 20 6c 6f 63 6b 54 61 62 6c 65 28 29 0a 2a 2a 20   lockTable().** 
c4af0 70 72 6f 63 65 64 75 72 65 29 20 68 65 6c 64 20  procedure) held 
c4b00 62 79 20 42 74 72 65 65 20 68 61 6e 64 6c 65 20  by Btree handle 
c4b10 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  p..*/.static voi
c4b20 64 20 75 6e 6c 6f 63 6b 41 6c 6c 54 61 62 6c 65  d unlockAllTable
c4b30 73 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42  s(Btree *p){.  B
c4b40 74 4c 6f 63 6b 20 2a 2a 70 70 49 74 65 72 20 3d  tLock **ppIter =
c4b50 20 26 70 2d 3e 70 42 74 2d 3e 70 4c 6f 63 6b 3b   &p->pBt->pLock;
c4b60 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73 68  ..  /* If the sh
c4b70 61 72 65 64 2d 63 61 63 68 65 20 65 78 74 65 6e  ared-cache exten
c4b80 73 69 6f 6e 20 69 73 20 6e 6f 74 20 65 6e 61 62  sion is not enab
c4b90 6c 65 64 2c 20 74 68 65 72 65 20 73 68 6f 75 6c  led, there shoul
c4ba0 64 20 62 65 20 6e 6f 0a 20 20 2a 2a 20 6c 6f 63  d be no.  ** loc
c4bb0 6b 73 20 69 6e 20 74 68 65 20 42 74 53 68 61 72  ks in the BtShar
c4bc0 65 64 2e 70 4c 6f 63 6b 20 6c 69 73 74 2c 20 6d  ed.pLock list, m
c4bd0 61 6b 69 6e 67 20 74 68 69 73 20 70 72 6f 63 65  aking this proce
c4be0 64 75 72 65 20 61 20 6e 6f 2d 6f 70 2e 20 41 73  dure a no-op. As
c4bf0 73 65 72 74 0a 20 20 2a 2a 20 74 68 61 74 20 74  sert.  ** that t
c4c00 68 69 73 20 69 73 20 74 68 65 20 63 61 73 65 2e  his is the case.
c4c10 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
c4c20 73 71 6c 69 74 65 33 54 68 72 65 61 64 44 61 74  sqlite3ThreadDat
c4c30 61 52 65 61 64 4f 6e 6c 79 28 29 2d 3e 75 73 65  aReadOnly()->use
c4c40 53 68 61 72 65 64 44 61 74 61 20 7c 7c 20 30 3d  SharedData || 0=
c4c50 3d 2a 70 70 49 74 65 72 20 29 3b 0a 0a 20 20 77  =*ppIter );..  w
c4c60 68 69 6c 65 28 20 2a 70 70 49 74 65 72 20 29 7b  hile( *ppIter ){
c4c70 0a 20 20 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f  .    BtLock *pLo
c4c80 63 6b 20 3d 20 2a 70 70 49 74 65 72 3b 0a 20 20  ck = *ppIter;.  
c4c90 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 42 74    if( pLock->pBt
c4ca0 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20  ree==p ){.      
c4cb0 2a 70 70 49 74 65 72 20 3d 20 70 4c 6f 63 6b 2d  *ppIter = pLock-
c4cc0 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 73 71  >pNext;.      sq
c4cd0 6c 69 74 65 46 72 65 65 28 70 4c 6f 63 6b 29 3b  liteFree(pLock);
c4ce0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
c4cf0 20 20 70 70 49 74 65 72 20 3d 20 26 70 4c 6f 63    ppIter = &pLoc
c4d00 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a  k->pNext;.    }.
c4d10 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20    }.}.#endif /* 
c4d20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
c4d30 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 73 74 61  ED_CACHE */..sta
c4d40 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61 73 65  tic void release
c4d50 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50  Page(MemPage *pP
c4d60 61 67 65 29 3b 20 20 2f 2a 20 46 6f 72 77 61 72  age);  /* Forwar
c4d70 64 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 0a  d reference */..
c4d80 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
c4d90 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a  MIT_INCRBLOB./*.
c4da0 2a 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 74 68  ** Invalidate th
c4db0 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d  e overflow page-
c4dc0 6c 69 73 74 20 63 61 63 68 65 20 66 6f 72 20 63  list cache for c
c4dd0 75 72 73 6f 72 20 70 43 75 72 2c 20 69 66 20 61  ursor pCur, if a
c4de0 6e 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ny..*/.static vo
c4df0 69 64 20 69 6e 76 61 6c 69 64 61 74 65 4f 76 65  id invalidateOve
c4e00 72 66 6c 6f 77 43 61 63 68 65 28 42 74 43 75 72  rflowCache(BtCur
c4e10 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 73 71  sor *pCur){.  sq
c4e20 6c 69 74 65 46 72 65 65 28 70 43 75 72 2d 3e 61  liteFree(pCur->a
c4e30 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20 70 43 75  Overflow);.  pCu
c4e40 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 3d 20 30  r->aOverflow = 0
c4e50 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 6c  ;.}../*.** Inval
c4e60 69 64 61 74 65 20 74 68 65 20 6f 76 65 72 66 6c  idate the overfl
c4e70 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63  ow page-list cac
c4e80 68 65 20 66 6f 72 20 61 6c 6c 20 63 75 72 73 6f  he for all curso
c4e90 72 73 20 6f 70 65 6e 65 64 0a 2a 2a 20 6f 6e 20  rs opened.** on 
c4ea0 74 68 65 20 73 68 61 72 65 64 20 62 74 72 65 65  the shared btree
c4eb0 20 73 74 72 75 63 74 75 72 65 20 70 42 74 2e 0a   structure pBt..
c4ec0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69  */.static void i
c4ed0 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72  nvalidateAllOver
c4ee0 66 6c 6f 77 43 61 63 68 65 28 42 74 53 68 61 72  flowCache(BtShar
c4ef0 65 64 20 2a 70 42 74 29 7b 0a 20 20 42 74 43 75  ed *pBt){.  BtCu
c4f00 72 73 6f 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70  rsor *p;.  for(p
c4f10 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70  =pBt->pCursor; p
c4f20 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20  ; p=p->pNext){. 
c4f30 20 20 20 69 6e 76 61 6c 69 64 61 74 65 4f 76 65     invalidateOve
c4f40 72 66 6c 6f 77 43 61 63 68 65 28 70 29 3b 0a 20  rflowCache(p);. 
c4f50 20 7d 0a 7d 0a 23 65 6c 73 65 0a 20 20 23 64 65   }.}.#else.  #de
c4f60 66 69 6e 65 20 69 6e 76 61 6c 69 64 61 74 65 4f  fine invalidateO
c4f70 76 65 72 66 6c 6f 77 43 61 63 68 65 28 78 29 0a  verflowCache(x).
c4f80 20 20 23 64 65 66 69 6e 65 20 69 6e 76 61 6c 69    #define invali
c4f90 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 43  dateAllOverflowC
c4fa0 61 63 68 65 28 78 29 0a 23 65 6e 64 69 66 0a 0a  ache(x).#endif..
c4fb0 2f 2a 0a 2a 2a 20 53 61 76 65 20 74 68 65 20 63  /*.** Save the c
c4fc0 75 72 72 65 6e 74 20 63 75 72 73 6f 72 20 70 6f  urrent cursor po
c4fd0 73 69 74 69 6f 6e 20 69 6e 20 74 68 65 20 76 61  sition in the va
c4fe0 72 69 61 62 6c 65 73 20 42 74 43 75 72 73 6f 72  riables BtCursor
c4ff0 2e 6e 4b 65 79 20 0a 2a 2a 20 61 6e 64 20 42 74  .nKey .** and Bt
c5000 43 75 72 73 6f 72 2e 70 4b 65 79 2e 20 54 68 65  Cursor.pKey. The
c5010 20 63 75 72 73 6f 72 27 73 20 73 74 61 74 65 20   cursor's state 
c5020 69 73 20 73 65 74 20 74 6f 20 43 55 52 53 4f 52  is set to CURSOR
c5030 5f 52 45 51 55 49 52 45 53 45 45 4b 2e 0a 2a 2f  _REQUIRESEEK..*/
c5040 0a 73 74 61 74 69 63 20 69 6e 74 20 73 61 76 65  .static int save
c5050 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 42  CursorPosition(B
c5060 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a  tCursor *pCur){.
c5070 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73    int rc;..  ass
c5080 65 72 74 28 20 43 55 52 53 4f 52 5f 56 41 4c 49  ert( CURSOR_VALI
c5090 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20  D==pCur->eState 
c50a0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 30 3d 3d  );.  assert( 0==
c50b0 70 43 75 72 2d 3e 70 4b 65 79 20 29 3b 0a 0a 20  pCur->pKey );.. 
c50c0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
c50d0 65 65 4b 65 79 53 69 7a 65 28 70 43 75 72 2c 20  eeKeySize(pCur, 
c50e0 26 70 43 75 72 2d 3e 6e 4b 65 79 29 3b 0a 0a 20  &pCur->nKey);.. 
c50f0 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61   /* If this is a
c5100 6e 20 69 6e 74 4b 65 79 20 74 61 62 6c 65 2c 20  n intKey table, 
c5110 74 68 65 6e 20 74 68 65 20 61 62 6f 76 65 20 63  then the above c
c5120 61 6c 6c 20 74 6f 20 42 74 72 65 65 4b 65 79 53  all to BtreeKeyS
c5130 69 7a 65 28 29 0a 20 20 2a 2a 20 73 74 6f 72 65  ize().  ** store
c5140 73 20 74 68 65 20 69 6e 74 65 67 65 72 20 6b 65  s the integer ke
c5150 79 20 69 6e 20 70 43 75 72 2d 3e 6e 4b 65 79 2e  y in pCur->nKey.
c5160 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68   In this case th
c5170 69 73 20 76 61 6c 75 65 20 69 73 0a 20 20 2a 2a  is value is.  **
c5180 20 61 6c 6c 20 74 68 61 74 20 69 73 20 72 65 71   all that is req
c5190 75 69 72 65 64 2e 20 4f 74 68 65 72 77 69 73 65  uired. Otherwise
c51a0 2c 20 69 66 20 70 43 75 72 20 69 73 20 6e 6f 74  , if pCur is not
c51b0 20 6f 70 65 6e 20 6f 6e 20 61 6e 20 69 6e 74 4b   open on an intK
c51c0 65 79 0a 20 20 2a 2a 20 74 61 62 6c 65 2c 20 74  ey.  ** table, t
c51d0 68 65 6e 20 6d 61 6c 6c 6f 63 20 73 70 61 63 65  hen malloc space
c51e0 20 66 6f 72 20 61 6e 64 20 73 74 6f 72 65 20 74   for and store t
c51f0 68 65 20 70 43 75 72 2d 3e 6e 4b 65 79 20 62 79  he pCur->nKey by
c5200 74 65 73 20 6f 66 20 6b 65 79 20 0a 20 20 2a 2a  tes of key .  **
c5210 20 64 61 74 61 2e 0a 20 20 2a 2f 0a 20 20 69 66   data..  */.  if
c5220 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
c5230 26 26 20 30 3d 3d 70 43 75 72 2d 3e 70 50 61 67  && 0==pCur->pPag
c5240 65 2d 3e 69 6e 74 4b 65 79 29 7b 0a 20 20 20 20  e->intKey){.    
c5250 76 6f 69 64 20 2a 70 4b 65 79 20 3d 20 73 71 6c  void *pKey = sql
c5260 69 74 65 4d 61 6c 6c 6f 63 28 70 43 75 72 2d 3e  iteMalloc(pCur->
c5270 6e 4b 65 79 29 3b 0a 20 20 20 20 69 66 28 20 70  nKey);.    if( p
c5280 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20  Key ){.      rc 
c5290 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65  = sqlite3BtreeKe
c52a0 79 28 70 43 75 72 2c 20 30 2c 20 70 43 75 72 2d  y(pCur, 0, pCur-
c52b0 3e 6e 4b 65 79 2c 20 70 4b 65 79 29 3b 0a 20 20  >nKey, pKey);.  
c52c0 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
c52d0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
c52e0 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 70 4b   pCur->pKey = pK
c52f0 65 79 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ey;.      }else{
c5300 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 46  .        sqliteF
c5310 72 65 65 28 70 4b 65 79 29 3b 0a 20 20 20 20 20  ree(pKey);.     
c5320 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
c5330 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
c5340 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d  NOMEM;.    }.  }
c5350 0a 20 20 61 73 73 65 72 74 28 20 21 70 43 75 72  .  assert( !pCur
c5360 2d 3e 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20  ->pPage->intKey 
c5370 7c 7c 20 21 70 43 75 72 2d 3e 70 4b 65 79 20 29  || !pCur->pKey )
c5380 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  ;..  if( rc==SQL
c5390 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65  ITE_OK ){.    re
c53a0 6c 65 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e  leasePage(pCur->
c53b0 70 50 61 67 65 29 3b 0a 20 20 20 20 70 43 75 72  pPage);.    pCur
c53c0 2d 3e 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 20  ->pPage = 0;.   
c53d0 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20   pCur->eState = 
c53e0 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45  CURSOR_REQUIRESE
c53f0 45 4b 3b 0a 20 20 7d 0a 0a 20 20 69 6e 76 61 6c  EK;.  }..  inval
c5400 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 63  idateOverflowCac
c5410 68 65 28 70 43 75 72 29 3b 0a 20 20 72 65 74 75  he(pCur);.  retu
c5420 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
c5430 53 61 76 65 20 74 68 65 20 70 6f 73 69 74 69 6f  Save the positio
c5440 6e 73 20 6f 66 20 61 6c 6c 20 63 75 72 73 6f 72  ns of all cursor
c5450 73 20 65 78 63 65 70 74 20 70 45 78 63 65 70 74  s except pExcept
c5460 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 74 61 62   open on the tab
c5470 6c 65 20 0a 2a 2a 20 77 69 74 68 20 72 6f 6f 74  le .** with root
c5480 2d 70 61 67 65 20 69 52 6f 6f 74 2e 20 55 73 75  -page iRoot. Usu
c5490 61 6c 6c 79 2c 20 74 68 69 73 20 69 73 20 63 61  ally, this is ca
c54a0 6c 6c 65 64 20 6a 75 73 74 20 62 65 66 6f 72 65  lled just before
c54b0 20 63 75 72 73 6f 72 0a 2a 2a 20 70 45 78 63 65   cursor.** pExce
c54c0 70 74 20 69 73 20 75 73 65 64 20 74 6f 20 6d 6f  pt is used to mo
c54d0 64 69 66 79 20 74 68 65 20 74 61 62 6c 65 20 28  dify the table (
c54e0 42 74 72 65 65 44 65 6c 65 74 65 28 29 20 6f 72  BtreeDelete() or
c54f0 20 42 74 72 65 65 49 6e 73 65 72 74 28 29 29 2e   BtreeInsert()).
c5500 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
c5510 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 42 74  aveAllCursors(Bt
c5520 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e  Shared *pBt, Pgn
c5530 6f 20 69 52 6f 6f 74 2c 20 42 74 43 75 72 73 6f  o iRoot, BtCurso
c5540 72 20 2a 70 45 78 63 65 70 74 29 7b 0a 20 20 42  r *pExcept){.  B
c5550 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 66 6f  tCursor *p;.  fo
c5560 72 28 70 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72  r(p=pBt->pCursor
c5570 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29  ; p; p=p->pNext)
c5580 7b 0a 20 20 20 20 69 66 28 20 70 21 3d 70 45 78  {.    if( p!=pEx
c5590 63 65 70 74 20 26 26 20 28 30 3d 3d 69 52 6f 6f  cept && (0==iRoo
c55a0 74 20 7c 7c 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74  t || p->pgnoRoot
c55b0 3d 3d 69 52 6f 6f 74 29 20 26 26 20 0a 20 20 20  ==iRoot) && .   
c55c0 20 20 20 20 20 70 2d 3e 65 53 74 61 74 65 3d 3d       p->eState==
c55d0 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 7b 0a  CURSOR_VALID ){.
c55e0 20 20 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73        int rc = s
c55f0 61 76 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f  aveCursorPositio
c5600 6e 28 70 29 3b 0a 20 20 20 20 20 20 69 66 28 20  n(p);.      if( 
c5610 53 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 20 29 7b  SQLITE_OK!=rc ){
c5620 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
c5630 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rc;.      }.    
c5640 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  }.  }.  return S
c5650 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
c5660 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 63 75 72  ** Clear the cur
c5670 72 65 6e 74 20 63 75 72 73 6f 72 20 70 6f 73 69  rent cursor posi
c5680 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
c5690 76 6f 69 64 20 63 6c 65 61 72 43 75 72 73 6f 72  void clearCursor
c56a0 50 6f 73 69 74 69 6f 6e 28 42 74 43 75 72 73 6f  Position(BtCurso
c56b0 72 20 2a 70 43 75 72 29 7b 0a 20 20 73 71 6c 69  r *pCur){.  sqli
c56c0 74 65 46 72 65 65 28 70 43 75 72 2d 3e 70 4b 65  teFree(pCur->pKe
c56d0 79 29 3b 0a 20 20 70 43 75 72 2d 3e 70 4b 65 79  y);.  pCur->pKey
c56e0 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 65 53   = 0;.  pCur->eS
c56f0 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e  tate = CURSOR_IN
c5700 56 41 4c 49 44 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  VALID;.}../*.** 
c5710 52 65 73 74 6f 72 65 20 74 68 65 20 63 75 72 73  Restore the curs
c5720 6f 72 20 74 6f 20 74 68 65 20 70 6f 73 69 74 69  or to the positi
c5730 6f 6e 20 69 74 20 77 61 73 20 69 6e 20 28 6f 72  on it was in (or
c5740 20 61 73 20 63 6c 6f 73 65 20 74 6f 20 61 73 20   as close to as 
c5750 70 6f 73 73 69 62 6c 65 29 0a 2a 2a 20 77 68 65  possible).** whe
c5760 6e 20 73 61 76 65 43 75 72 73 6f 72 50 6f 73 69  n saveCursorPosi
c5770 74 69 6f 6e 28 29 20 77 61 73 20 63 61 6c 6c 65  tion() was calle
c5780 64 2e 20 4e 6f 74 65 20 74 68 61 74 20 74 68 69  d. Note that thi
c5790 73 20 63 61 6c 6c 20 64 65 6c 65 74 65 73 20 74  s call deletes t
c57a0 68 65 20 0a 2a 2a 20 73 61 76 65 64 20 70 6f 73  he .** saved pos
c57b0 69 74 69 6f 6e 20 69 6e 66 6f 20 73 74 6f 72 65  ition info store
c57c0 64 20 62 79 20 73 61 76 65 43 75 72 73 6f 72 50  d by saveCursorP
c57d0 6f 73 69 74 69 6f 6e 28 29 2c 20 73 6f 20 74 68  osition(), so th
c57e0 65 72 65 20 63 61 6e 20 62 65 0a 2a 2a 20 61 74  ere can be.** at
c57f0 20 6d 6f 73 74 20 6f 6e 65 20 65 66 66 65 63 74   most one effect
c5800 69 76 65 20 72 65 73 74 6f 72 65 4f 72 43 6c 65  ive restoreOrCle
c5810 61 72 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e  arCursorPosition
c5820 28 29 20 63 61 6c 6c 20 61 66 74 65 72 20 65 61  () call after ea
c5830 63 68 20 0a 2a 2a 20 73 61 76 65 43 75 72 73 6f  ch .** saveCurso
c5840 72 50 6f 73 69 74 69 6f 6e 28 29 2e 0a 2a 2a 0a  rPosition()..**.
c5850 2a 2a 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64  ** If the second
c5860 20 61 72 67 75 6d 65 6e 74 20 61 72 67 75 6d 65   argument argume
c5870 6e 74 20 2d 20 64 6f 53 65 65 6b 20 2d 20 69 73  nt - doSeek - is
c5880 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 69 6e 73   false, then ins
c5890 74 65 61 64 20 6f 66 20 0a 2a 2a 20 72 65 74 75  tead of .** retu
c58a0 72 6e 69 6e 67 20 74 68 65 20 63 75 72 73 6f 72  rning the cursor
c58b0 20 74 6f 20 69 74 27 73 20 73 61 76 65 64 20 70   to it's saved p
c58c0 6f 73 69 74 69 6f 6e 2c 20 61 6e 79 20 73 61 76  osition, any sav
c58d0 65 64 20 70 6f 73 69 74 69 6f 6e 20 69 73 20 64  ed position is d
c58e0 65 6c 65 74 65 64 0a 2a 2a 20 61 6e 64 20 74 68  eleted.** and th
c58f0 65 20 63 75 72 73 6f 72 20 73 74 61 74 65 20 73  e cursor state s
c5900 65 74 20 74 6f 20 43 55 52 53 4f 52 5f 49 4e 56  et to CURSOR_INV
c5910 41 4c 49 44 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ALID..*/.SQLITE_
c5920 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
c5930 74 65 33 42 74 72 65 65 52 65 73 74 6f 72 65 4f  te3BtreeRestoreO
c5940 72 43 6c 65 61 72 43 75 72 73 6f 72 50 6f 73 69  rClearCursorPosi
c5950 74 69 6f 6e 28 42 74 43 75 72 73 6f 72 20 2a 70  tion(BtCursor *p
c5960 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  Cur){.  int rc;.
c5970 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
c5980 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 52  eState==CURSOR_R
c5990 45 51 55 49 52 45 53 45 45 4b 20 29 3b 0a 23 69  EQUIRESEEK );.#i
c59a0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
c59b0 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 69 66 28  T_INCRBLOB.  if(
c59c0 20 70 43 75 72 2d 3e 69 73 49 6e 63 72 62 6c 6f   pCur->isIncrblo
c59d0 62 48 61 6e 64 6c 65 20 29 7b 0a 20 20 20 20 72  bHandle ){.    r
c59e0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 42 4f  eturn SQLITE_ABO
c59f0 52 54 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20  RT;.  }.#endif. 
c5a00 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20   pCur->eState = 
c5a10 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a  CURSOR_INVALID;.
c5a20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
c5a30 72 65 65 4d 6f 76 65 74 6f 28 70 43 75 72 2c 20  reeMoveto(pCur, 
c5a40 70 43 75 72 2d 3e 70 4b 65 79 2c 20 70 43 75 72  pCur->pKey, pCur
c5a50 2d 3e 6e 4b 65 79 2c 20 30 2c 20 26 70 43 75 72  ->nKey, 0, &pCur
c5a60 2d 3e 73 6b 69 70 29 3b 0a 20 20 69 66 28 20 72  ->skip);.  if( r
c5a70 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
c5a80 20 20 20 20 73 71 6c 69 74 65 46 72 65 65 28 70      sqliteFree(p
c5a90 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 20 20 20 20  Cur->pKey);.    
c5aa0 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 30 3b 0a  pCur->pKey = 0;.
c5ab0 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72      assert( pCur
c5ac0 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
c5ad0 5f 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d 3e  _VALID || pCur->
c5ae0 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49  eState==CURSOR_I
c5af0 4e 56 41 4c 49 44 20 29 3b 0a 20 20 7d 0a 20 20  NVALID );.  }.  
c5b00 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 64  return rc;.}..#d
c5b10 65 66 69 6e 65 20 72 65 73 74 6f 72 65 4f 72 43  efine restoreOrC
c5b20 6c 65 61 72 43 75 72 73 6f 72 50 6f 73 69 74 69  learCursorPositi
c5b30 6f 6e 28 70 29 20 5c 0a 20 20 28 70 2d 3e 65 53  on(p) \.  (p->eS
c5b40 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 52 45 51  tate==CURSOR_REQ
c5b50 55 49 52 45 53 45 45 4b 20 3f 20 5c 0a 20 20 20  UIRESEEK ? \.   
c5b60 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
c5b70 65 65 52 65 73 74 6f 72 65 4f 72 43 6c 65 61 72  eeRestoreOrClear
c5b80 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70  CursorPosition(p
c5b90 29 20 3a 20 5c 0a 20 20 20 20 20 20 20 20 20 53  ) : \.         S
c5ba0 51 4c 49 54 45 5f 4f 4b 29 0a 0a 23 69 66 6e 64  QLITE_OK)..#ifnd
c5bb0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
c5bc0 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20  UTOVACUUM./*.** 
c5bd0 47 69 76 65 6e 20 61 20 70 61 67 65 20 6e 75 6d  Given a page num
c5be0 62 65 72 20 6f 66 20 61 20 72 65 67 75 6c 61 72  ber of a regular
c5bf0 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2c 20   database page, 
c5c00 72 65 74 75 72 6e 20 74 68 65 20 70 61 67 65 0a  return the page.
c5c10 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68  ** number for th
c5c20 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61  e pointer-map pa
c5c30 67 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  ge that contains
c5c40 20 74 68 65 20 65 6e 74 72 79 20 66 6f 72 20 74   the entry for t
c5c50 68 65 0a 2a 2a 20 69 6e 70 75 74 20 70 61 67 65  he.** input page
c5c60 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74   number..*/.stat
c5c70 69 63 20 50 67 6e 6f 20 70 74 72 6d 61 70 50 61  ic Pgno ptrmapPa
c5c80 67 65 6e 6f 28 42 74 53 68 61 72 65 64 20 2a 70  geno(BtShared *p
c5c90 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a  Bt, Pgno pgno){.
c5ca0 20 20 69 6e 74 20 6e 50 61 67 65 73 50 65 72 4d    int nPagesPerM
c5cb0 61 70 50 61 67 65 20 3d 20 28 70 42 74 2d 3e 75  apPage = (pBt->u
c5cc0 73 61 62 6c 65 53 69 7a 65 2f 35 29 2b 31 3b 0a  sableSize/5)+1;.
c5cd0 20 20 69 6e 74 20 69 50 74 72 4d 61 70 20 3d 20    int iPtrMap = 
c5ce0 28 70 67 6e 6f 2d 32 29 2f 6e 50 61 67 65 73 50  (pgno-2)/nPagesP
c5cf0 65 72 4d 61 70 50 61 67 65 3b 0a 20 20 69 6e 74  erMapPage;.  int
c5d00 20 72 65 74 20 3d 20 28 69 50 74 72 4d 61 70 2a   ret = (iPtrMap*
c5d10 6e 50 61 67 65 73 50 65 72 4d 61 70 50 61 67 65  nPagesPerMapPage
c5d20 29 20 2b 20 32 3b 20 0a 20 20 69 66 28 20 72 65  ) + 2; .  if( re
c5d30 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  t==PENDING_BYTE_
c5d40 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20  PAGE(pBt) ){.   
c5d50 20 72 65 74 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65   ret++;.  }.  re
c5d60 74 75 72 6e 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a  turn ret;.}../*.
c5d70 2a 2a 20 57 72 69 74 65 20 61 6e 20 65 6e 74 72  ** Write an entr
c5d80 79 20 69 6e 74 6f 20 74 68 65 20 70 6f 69 6e 74  y into the point
c5d90 65 72 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 54 68  er map..**.** Th
c5da0 69 73 20 72 6f 75 74 69 6e 65 20 75 70 64 61 74  is routine updat
c5db0 65 73 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d  es the pointer m
c5dc0 61 70 20 65 6e 74 72 79 20 66 6f 72 20 70 61 67  ap entry for pag
c5dd0 65 20 6e 75 6d 62 65 72 20 27 6b 65 79 27 0a 2a  e number 'key'.*
c5de0 2a 20 73 6f 20 74 68 61 74 20 69 74 20 6d 61 70  * so that it map
c5df0 73 20 74 6f 20 74 79 70 65 20 27 65 54 79 70 65  s to type 'eType
c5e00 27 20 61 6e 64 20 70 61 72 65 6e 74 20 70 61 67  ' and parent pag
c5e10 65 20 6e 75 6d 62 65 72 20 27 70 67 6e 6f 27 2e  e number 'pgno'.
c5e20 0a 2a 2a 20 41 6e 20 65 72 72 6f 72 20 63 6f 64  .** An error cod
c5e30 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66  e is returned if
c5e40 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20   something goes 
c5e50 77 72 6f 6e 67 2c 20 6f 74 68 65 72 77 69 73 65  wrong, otherwise
c5e60 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 73   SQLITE_OK..*/.s
c5e70 74 61 74 69 63 20 69 6e 74 20 70 74 72 6d 61 70  tatic int ptrmap
c5e80 50 75 74 28 42 74 53 68 61 72 65 64 20 2a 70 42  Put(BtShared *pB
c5e90 74 2c 20 50 67 6e 6f 20 6b 65 79 2c 20 75 38 20  t, Pgno key, u8 
c5ea0 65 54 79 70 65 2c 20 50 67 6e 6f 20 70 61 72 65  eType, Pgno pare
c5eb0 6e 74 29 7b 0a 20 20 44 62 50 61 67 65 20 2a 70  nt){.  DbPage *p
c5ec0 44 62 50 61 67 65 3b 20 20 2f 2a 20 54 68 65 20  DbPage;  /* The 
c5ed0 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65  pointer map page
c5ee0 20 2a 2f 0a 20 20 75 38 20 2a 70 50 74 72 6d 61   */.  u8 *pPtrma
c5ef0 70 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70  p;      /* The p
c5f00 6f 69 6e 74 65 72 20 6d 61 70 20 64 61 74 61 20  ointer map data 
c5f10 2a 2f 0a 20 20 50 67 6e 6f 20 69 50 74 72 6d 61  */.  Pgno iPtrma
c5f20 70 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 70 6f  p;     /* The po
c5f30 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 6e  inter map page n
c5f40 75 6d 62 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6f  umber */.  int o
c5f50 66 66 73 65 74 3b 20 20 20 20 20 20 20 2f 2a 20  ffset;       /* 
c5f60 4f 66 66 73 65 74 20 69 6e 20 70 6f 69 6e 74 65  Offset in pointe
c5f70 72 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 20  r map page */.  
c5f80 69 6e 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 54 68  int rc;..  /* Th
c5f90 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c  e master-journal
c5fa0 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6d 75 73   page number mus
c5fb0 74 20 6e 65 76 65 72 20 62 65 20 75 73 65 64 20  t never be used 
c5fc0 61 73 20 61 20 70 6f 69 6e 74 65 72 20 6d 61 70  as a pointer map
c5fd0 20 70 61 67 65 20 2a 2f 0a 20 20 61 73 73 65 72   page */.  asser
c5fe0 74 28 20 30 3d 3d 50 54 52 4d 41 50 5f 49 53 50  t( 0==PTRMAP_ISP
c5ff0 41 47 45 28 70 42 74 2c 20 50 45 4e 44 49 4e 47  AGE(pBt, PENDING
c6000 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 29  _BYTE_PAGE(pBt))
c6010 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70   );..  assert( p
c6020 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29  Bt->autoVacuum )
c6030 3b 0a 20 20 69 66 28 20 6b 65 79 3d 3d 30 20 29  ;.  if( key==0 )
c6040 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
c6050 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
c6060 3b 0a 20 20 7d 0a 20 20 69 50 74 72 6d 61 70 20  ;.  }.  iPtrmap 
c6070 3d 20 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28  = PTRMAP_PAGENO(
c6080 70 42 74 2c 20 6b 65 79 29 3b 0a 20 20 72 63 20  pBt, key);.  rc 
c6090 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  = sqlite3PagerGe
c60a0 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69  t(pBt->pPager, i
c60b0 50 74 72 6d 61 70 2c 20 26 70 44 62 50 61 67 65  Ptrmap, &pDbPage
c60c0 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
c60d0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65  ITE_OK ){.    re
c60e0 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 6f  turn rc;.  }.  o
c60f0 66 66 73 65 74 20 3d 20 50 54 52 4d 41 50 5f 50  ffset = PTRMAP_P
c6100 54 52 4f 46 46 53 45 54 28 70 42 74 2c 20 6b 65  TROFFSET(pBt, ke
c6110 79 29 3b 0a 20 20 70 50 74 72 6d 61 70 20 3d 20  y);.  pPtrmap = 
c6120 28 75 38 20 2a 29 73 71 6c 69 74 65 33 50 61 67  (u8 *)sqlite3Pag
c6130 65 72 47 65 74 44 61 74 61 28 70 44 62 50 61 67  erGetData(pDbPag
c6140 65 29 3b 0a 0a 20 20 69 66 28 20 65 54 79 70 65  e);..  if( eType
c6150 21 3d 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74  !=pPtrmap[offset
c6160 5d 20 7c 7c 20 67 65 74 34 62 79 74 65 28 26 70  ] || get4byte(&p
c6170 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d  Ptrmap[offset+1]
c6180 29 21 3d 70 61 72 65 6e 74 20 29 7b 0a 20 20 20  )!=parent ){.   
c6190 20 54 52 41 43 45 28 28 22 50 54 52 4d 41 50 5f   TRACE(("PTRMAP_
c61a0 55 50 44 41 54 45 3a 20 25 64 2d 3e 28 25 64 2c  UPDATE: %d->(%d,
c61b0 25 64 29 5c 6e 22 2c 20 6b 65 79 2c 20 65 54 79  %d)\n", key, eTy
c61c0 70 65 2c 20 70 61 72 65 6e 74 29 29 3b 0a 20 20  pe, parent));.  
c61d0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
c61e0 67 65 72 57 72 69 74 65 28 70 44 62 50 61 67 65  gerWrite(pDbPage
c61f0 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
c6200 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
c6210 20 20 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74    pPtrmap[offset
c6220 5d 20 3d 20 65 54 79 70 65 3b 0a 20 20 20 20 20  ] = eType;.     
c6230 20 70 75 74 34 62 79 74 65 28 26 70 50 74 72 6d   put4byte(&pPtrm
c6240 61 70 5b 6f 66 66 73 65 74 2b 31 5d 2c 20 70 61  ap[offset+1], pa
c6250 72 65 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  rent);.    }.  }
c6260 0a 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72  ..  sqlite3Pager
c6270 55 6e 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a  Unref(pDbPage);.
c6280 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
c6290 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 6e 20 65 6e  /*.** Read an en
c62a0 74 72 79 20 66 72 6f 6d 20 74 68 65 20 70 6f 69  try from the poi
c62b0 6e 74 65 72 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20  nter map..**.** 
c62c0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  This routine ret
c62d0 72 69 65 76 65 73 20 74 68 65 20 70 6f 69 6e 74  rieves the point
c62e0 65 72 20 6d 61 70 20 65 6e 74 72 79 20 66 6f 72  er map entry for
c62f0 20 70 61 67 65 20 27 6b 65 79 27 2c 20 77 72 69   page 'key', wri
c6300 74 69 6e 67 0a 2a 2a 20 74 68 65 20 74 79 70 65  ting.** the type
c6310 20 61 6e 64 20 70 61 72 65 6e 74 20 70 61 67 65   and parent page
c6320 20 6e 75 6d 62 65 72 20 74 6f 20 2a 70 45 54 79   number to *pETy
c6330 70 65 20 61 6e 64 20 2a 70 50 67 6e 6f 20 72 65  pe and *pPgno re
c6340 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 41  spectively..** A
c6350 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  n error code is 
c6360 72 65 74 75 72 6e 65 64 20 69 66 20 73 6f 6d 65  returned if some
c6370 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67  thing goes wrong
c6380 2c 20 6f 74 68 65 72 77 69 73 65 20 53 51 4c 49  , otherwise SQLI
c6390 54 45 5f 4f 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63  TE_OK..*/.static
c63a0 20 69 6e 74 20 70 74 72 6d 61 70 47 65 74 28 42   int ptrmapGet(B
c63b0 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67  tShared *pBt, Pg
c63c0 6e 6f 20 6b 65 79 2c 20 75 38 20 2a 70 45 54 79  no key, u8 *pETy
c63d0 70 65 2c 20 50 67 6e 6f 20 2a 70 50 67 6e 6f 29  pe, Pgno *pPgno)
c63e0 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 50  {.  DbPage *pDbP
c63f0 61 67 65 3b 20 20 20 2f 2a 20 54 68 65 20 70 6f  age;   /* The po
c6400 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 2a  inter map page *
c6410 2f 0a 20 20 69 6e 74 20 69 50 74 72 6d 61 70 3b  /.  int iPtrmap;
c6420 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
c6430 72 20 6d 61 70 20 70 61 67 65 20 69 6e 64 65 78  r map page index
c6440 20 2a 2f 0a 20 20 75 38 20 2a 70 50 74 72 6d 61   */.  u8 *pPtrma
c6450 70 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e  p;       /* Poin
c6460 74 65 72 20 6d 61 70 20 70 61 67 65 20 64 61 74  ter map page dat
c6470 61 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 73 65  a */.  int offse
c6480 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66  t;        /* Off
c6490 73 65 74 20 6f 66 20 65 6e 74 72 79 20 69 6e 20  set of entry in 
c64a0 70 6f 69 6e 74 65 72 20 6d 61 70 20 2a 2f 0a 20  pointer map */. 
c64b0 20 69 6e 74 20 72 63 3b 0a 0a 20 20 69 50 74 72   int rc;..  iPtr
c64c0 6d 61 70 20 3d 20 50 54 52 4d 41 50 5f 50 41 47  map = PTRMAP_PAG
c64d0 45 4e 4f 28 70 42 74 2c 20 6b 65 79 29 3b 0a 20  ENO(pBt, key);. 
c64e0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
c64f0 65 72 47 65 74 28 70 42 74 2d 3e 70 50 61 67 65  erGet(pBt->pPage
c6500 72 2c 20 69 50 74 72 6d 61 70 2c 20 26 70 44 62  r, iPtrmap, &pDb
c6510 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 21  Page);.  if( rc!
c6520 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
c6530 20 72 63 3b 0a 20 20 7d 0a 20 20 70 50 74 72 6d   rc;.  }.  pPtrm
c6540 61 70 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74  ap = (u8 *)sqlit
c6550 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70  e3PagerGetData(p
c6560 44 62 50 61 67 65 29 3b 0a 0a 20 20 6f 66 66 73  DbPage);..  offs
c6570 65 74 20 3d 20 50 54 52 4d 41 50 5f 50 54 52 4f  et = PTRMAP_PTRO
c6580 46 46 53 45 54 28 70 42 74 2c 20 6b 65 79 29 3b  FFSET(pBt, key);
c6590 0a 20 20 61 73 73 65 72 74 28 20 70 45 54 79 70  .  assert( pETyp
c65a0 65 21 3d 30 20 29 3b 0a 20 20 2a 70 45 54 79 70  e!=0 );.  *pETyp
c65b0 65 20 3d 20 70 50 74 72 6d 61 70 5b 6f 66 66 73  e = pPtrmap[offs
c65c0 65 74 5d 3b 0a 20 20 69 66 28 20 70 50 67 6e 6f  et];.  if( pPgno
c65d0 20 29 20 2a 70 50 67 6e 6f 20 3d 20 67 65 74 34   ) *pPgno = get4
c65e0 62 79 74 65 28 26 70 50 74 72 6d 61 70 5b 6f 66  byte(&pPtrmap[of
c65f0 66 73 65 74 2b 31 5d 29 3b 0a 0a 20 20 73 71 6c  fset+1]);..  sql
c6600 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
c6610 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 2a  DbPage);.  if( *
c6620 70 45 54 79 70 65 3c 31 20 7c 7c 20 2a 70 45 54  pEType<1 || *pET
c6630 79 70 65 3e 35 20 29 20 72 65 74 75 72 6e 20 53  ype>5 ) return S
c6640 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
c6650 50 54 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  PT;.  return SQL
c6660 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69  ITE_OK;.}..#endi
c6670 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
c6680 5f 41 55 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 0a  _AUTOVACUUM */..
c6690 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 62 74  /*.** Given a bt
c66a0 72 65 65 20 70 61 67 65 20 61 6e 64 20 61 20 63  ree page and a c
c66b0 65 6c 6c 20 69 6e 64 65 78 20 28 30 20 6d 65 61  ell index (0 mea
c66c0 6e 73 20 74 68 65 20 66 69 72 73 74 20 63 65 6c  ns the first cel
c66d0 6c 20 6f 6e 0a 2a 2a 20 74 68 65 20 70 61 67 65  l on.** the page
c66e0 2c 20 31 20 6d 65 61 6e 73 20 74 68 65 20 73 65  , 1 means the se
c66f0 63 6f 6e 64 20 63 65 6c 6c 2c 20 61 6e 64 20 73  cond cell, and s
c6700 6f 20 66 6f 72 74 68 29 20 72 65 74 75 72 6e 20  o forth) return 
c6710 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20  a pointer.** to 
c6720 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74  the cell content
c6730 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
c6740 74 69 6e 65 20 77 6f 72 6b 73 20 6f 6e 6c 79 20  tine works only 
c6750 66 6f 72 20 70 61 67 65 73 20 74 68 61 74 20 64  for pages that d
c6760 6f 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 6f 76  o not contain ov
c6770 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2e 0a 2a 2f  erflow cells..*/
c6780 0a 23 64 65 66 69 6e 65 20 66 69 6e 64 43 65 6c  .#define findCel
c6790 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 29 20  l(pPage, iCell) 
c67a0 5c 0a 20 20 28 28 70 50 61 67 65 29 2d 3e 61 44  \.  ((pPage)->aD
c67b0 61 74 61 20 2b 20 67 65 74 32 62 79 74 65 28 26  ata + get2byte(&
c67c0 28 70 50 61 67 65 29 2d 3e 61 44 61 74 61 5b 28  (pPage)->aData[(
c67d0 70 50 61 67 65 29 2d 3e 63 65 6c 6c 4f 66 66 73  pPage)->cellOffs
c67e0 65 74 2b 32 2a 28 69 43 65 6c 6c 29 5d 29 29 0a  et+2*(iCell)])).
c67f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75  SQLITE_PRIVATE u
c6800 38 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 46  8 *sqlite3BtreeF
c6810 69 6e 64 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20  indCell(MemPage 
c6820 2a 70 50 61 67 65 2c 20 69 6e 74 20 69 43 65 6c  *pPage, int iCel
c6830 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69 43  l){.  assert( iC
c6840 65 6c 6c 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65  ell>=0 );.  asse
c6850 72 74 28 20 69 43 65 6c 6c 3c 67 65 74 32 62 79  rt( iCell<get2by
c6860 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61  te(&pPage->aData
c6870 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65  [pPage->hdrOffse
c6880 74 2b 33 5d 29 20 29 3b 0a 20 20 72 65 74 75 72  t+3]) );.  retur
c6890 6e 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65  n findCell(pPage
c68a0 2c 20 69 43 65 6c 6c 29 3b 0a 7d 0a 0a 2f 2a 0a  , iCell);.}../*.
c68b0 2a 2a 20 54 68 69 73 20 61 20 6d 6f 72 65 20 63  ** This a more c
c68c0 6f 6d 70 6c 65 78 20 76 65 72 73 69 6f 6e 20 6f  omplex version o
c68d0 66 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 69  f sqlite3BtreeFi
c68e0 6e 64 43 65 6c 6c 28 29 20 74 68 61 74 20 77 6f  ndCell() that wo
c68f0 72 6b 73 20 66 6f 72 0a 2a 2a 20 70 61 67 65 73  rks for.** pages
c6900 20 74 68 61 74 20 64 6f 20 63 6f 6e 74 61 69 6e   that do contain
c6910 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2e   overflow cells.
c6920 20 20 53 65 65 20 69 6e 73 65 72 74 0a 2a 2f 0a    See insert.*/.
c6930 73 74 61 74 69 63 20 75 38 20 2a 66 69 6e 64 4f  static u8 *findO
c6940 76 65 72 66 6c 6f 77 43 65 6c 6c 28 4d 65 6d 50  verflowCell(MemP
c6950 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20  age *pPage, int 
c6960 69 43 65 6c 6c 29 7b 0a 20 20 69 6e 74 20 69 3b  iCell){.  int i;
c6970 0a 20 20 66 6f 72 28 69 3d 70 50 61 67 65 2d 3e  .  for(i=pPage->
c6980 6e 4f 76 65 72 66 6c 6f 77 2d 31 3b 20 69 3e 3d  nOverflow-1; i>=
c6990 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 69 6e 74  0; i--){.    int
c69a0 20 6b 3b 0a 20 20 20 20 73 74 72 75 63 74 20 5f   k;.    struct _
c69b0 4f 76 66 6c 43 65 6c 6c 20 2a 70 4f 76 66 6c 3b  OvflCell *pOvfl;
c69c0 0a 20 20 20 20 70 4f 76 66 6c 20 3d 20 26 70 50  .    pOvfl = &pP
c69d0 61 67 65 2d 3e 61 4f 76 66 6c 5b 69 5d 3b 0a 20  age->aOvfl[i];. 
c69e0 20 20 20 6b 20 3d 20 70 4f 76 66 6c 2d 3e 69 64     k = pOvfl->id
c69f0 78 3b 0a 20 20 20 20 69 66 28 20 6b 3c 3d 69 43  x;.    if( k<=iC
c6a00 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ell ){.      if(
c6a10 20 6b 3d 3d 69 43 65 6c 6c 20 29 7b 0a 20 20 20   k==iCell ){.   
c6a20 20 20 20 20 20 72 65 74 75 72 6e 20 70 4f 76 66       return pOvf
c6a30 6c 2d 3e 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20  l->pCell;.      
c6a40 7d 0a 20 20 20 20 20 20 69 43 65 6c 6c 2d 2d 3b  }.      iCell--;
c6a50 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
c6a60 75 72 6e 20 66 69 6e 64 43 65 6c 6c 28 70 50 61  urn findCell(pPa
c6a70 67 65 2c 20 69 43 65 6c 6c 29 3b 0a 7d 0a 0a 2f  ge, iCell);.}../
c6a80 2a 0a 2a 2a 20 50 61 72 73 65 20 61 20 63 65 6c  *.** Parse a cel
c6a90 6c 20 63 6f 6e 74 65 6e 74 20 62 6c 6f 63 6b 20  l content block 
c6aa0 61 6e 64 20 66 69 6c 6c 20 69 6e 20 74 68 65 20  and fill in the 
c6ab0 43 65 6c 6c 49 6e 66 6f 20 73 74 72 75 63 74 75  CellInfo structu
c6ac0 72 65 2e 20 20 54 68 65 72 65 0a 2a 2a 20 61 72  re.  There.** ar
c6ad0 65 20 74 77 6f 20 76 65 72 73 69 6f 6e 73 20 6f  e two versions o
c6ae0 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  f this function.
c6af0 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61    sqlite3BtreePa
c6b00 72 73 65 43 65 6c 6c 28 29 20 74 61 6b 65 73 20  rseCell() takes 
c6b10 61 20 0a 2a 2a 20 63 65 6c 6c 20 69 6e 64 65 78  a .** cell index
c6b20 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61   as the second a
c6b30 72 67 75 6d 65 6e 74 20 61 6e 64 20 73 71 6c 69  rgument and sqli
c6b40 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c  te3BtreeParseCel
c6b50 6c 50 74 72 28 29 20 0a 2a 2a 20 74 61 6b 65 73  lPtr() .** takes
c6b60 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
c6b70 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20 63 65  e body of the ce
c6b80 6c 6c 20 61 73 20 69 74 73 20 73 65 63 6f 6e 64  ll as its second
c6b90 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a   argument..**.**
c6ba0 20 57 69 74 68 69 6e 20 74 68 69 73 20 66 69 6c   Within this fil
c6bb0 65 2c 20 74 68 65 20 70 61 72 73 65 43 65 6c 6c  e, the parseCell
c6bc0 28 29 20 6d 61 63 72 6f 20 63 61 6e 20 62 65 20  () macro can be 
c6bd0 63 61 6c 6c 65 64 20 69 6e 73 74 65 61 64 20 6f  called instead o
c6be0 66 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65  f.** sqlite3Btre
c6bf0 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29 2e  eParseCellPtr().
c6c00 20 55 73 69 6e 67 20 73 6f 6d 65 20 63 6f 6d 70   Using some comp
c6c10 69 6c 65 72 73 2c 20 74 68 69 73 20 77 69 6c 6c  ilers, this will
c6c20 20 62 65 20 66 61 73 74 65 72 2e 0a 2a 2f 0a 53   be faster..*/.S
c6c30 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
c6c40 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 50  id sqlite3BtreeP
c6c50 61 72 73 65 43 65 6c 6c 50 74 72 28 0a 20 20 4d  arseCellPtr(.  M
c6c60 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20  emPage *pPage,  
c6c70 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63         /* Page c
c6c80 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 63 65  ontaining the ce
c6c90 6c 6c 20 2a 2f 0a 20 20 75 38 20 2a 70 43 65 6c  ll */.  u8 *pCel
c6ca0 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l,              
c6cb0 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68  /* Pointer to th
c6cc0 65 20 63 65 6c 6c 20 74 65 78 74 2e 20 2a 2f 0a  e cell text. */.
c6cd0 20 20 43 65 6c 6c 49 6e 66 6f 20 2a 70 49 6e 66    CellInfo *pInf
c6ce0 6f 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c  o         /* Fil
c6cf0 6c 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 74  l in this struct
c6d00 75 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ure */.){.  int 
c6d10 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
c6d20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 62 79      /* Number by
c6d30 74 65 73 20 69 6e 20 63 65 6c 6c 20 63 6f 6e 74  tes in cell cont
c6d40 65 6e 74 20 68 65 61 64 65 72 20 2a 2f 0a 20 20  ent header */.  
c6d50 75 33 32 20 6e 50 61 79 6c 6f 61 64 3b 20 20 20  u32 nPayload;   
c6d60 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
c6d70 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 63 65  r of bytes of ce
c6d80 6c 6c 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 0a 20  ll payload */.. 
c6d90 20 70 49 6e 66 6f 2d 3e 70 43 65 6c 6c 20 3d 20   pInfo->pCell = 
c6da0 70 43 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28  pCell;.  assert(
c6db0 20 70 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 30 20   pPage->leaf==0 
c6dc0 7c 7c 20 70 50 61 67 65 2d 3e 6c 65 61 66 3d 3d  || pPage->leaf==
c6dd0 31 20 29 3b 0a 20 20 6e 20 3d 20 70 50 61 67 65  1 );.  n = pPage
c6de0 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 3b 0a  ->childPtrSize;.
c6df0 20 20 61 73 73 65 72 74 28 20 6e 3d 3d 34 2d 34    assert( n==4-4
c6e00 2a 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a  *pPage->leaf );.
c6e10 20 20 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73    if( pPage->has
c6e20 44 61 74 61 20 29 7b 0a 20 20 20 20 6e 20 2b 3d  Data ){.    n +=
c6e30 20 67 65 74 56 61 72 69 6e 74 33 32 28 26 70 43   getVarint32(&pC
c6e40 65 6c 6c 5b 6e 5d 2c 20 26 6e 50 61 79 6c 6f 61  ell[n], &nPayloa
c6e50 64 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  d);.  }else{.   
c6e60 20 6e 50 61 79 6c 6f 61 64 20 3d 20 30 3b 0a 20   nPayload = 0;. 
c6e70 20 7d 0a 20 20 70 49 6e 66 6f 2d 3e 6e 44 61 74   }.  pInfo->nDat
c6e80 61 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20  a = nPayload;.  
c6e90 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65  if( pPage->intKe
c6ea0 79 20 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 67 65  y ){.    n += ge
c6eb0 74 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b 6e  tVarint(&pCell[n
c6ec0 5d 2c 20 28 75 36 34 20 2a 29 26 70 49 6e 66 6f  ], (u64 *)&pInfo
c6ed0 2d 3e 6e 4b 65 79 29 3b 0a 20 20 7d 65 6c 73 65  ->nKey);.  }else
c6ee0 7b 0a 20 20 20 20 75 33 32 20 78 3b 0a 20 20 20  {.    u32 x;.   
c6ef0 20 6e 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33   n += getVarint3
c6f00 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 26 78 29  2(&pCell[n], &x)
c6f10 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4b 65  ;.    pInfo->nKe
c6f20 79 20 3d 20 78 3b 0a 20 20 20 20 6e 50 61 79 6c  y = x;.    nPayl
c6f30 6f 61 64 20 2b 3d 20 78 3b 0a 20 20 7d 0a 20 20  oad += x;.  }.  
c6f40 70 49 6e 66 6f 2d 3e 6e 50 61 79 6c 6f 61 64 20  pInfo->nPayload 
c6f50 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 70 49  = nPayload;.  pI
c6f60 6e 66 6f 2d 3e 6e 48 65 61 64 65 72 20 3d 20 6e  nfo->nHeader = n
c6f70 3b 0a 20 20 69 66 28 20 6e 50 61 79 6c 6f 61 64  ;.  if( nPayload
c6f80 3c 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61  <=pPage->maxLoca
c6f90 6c 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73  l ){.    /* This
c6fa0 20 69 73 20 74 68 65 20 28 65 61 73 79 29 20 63   is the (easy) c
c6fb0 6f 6d 6d 6f 6e 20 63 61 73 65 20 77 68 65 72 65  ommon case where
c6fc0 20 74 68 65 20 65 6e 74 69 72 65 20 70 61 79 6c   the entire payl
c6fd0 6f 61 64 20 66 69 74 73 0a 20 20 20 20 2a 2a 20  oad fits.    ** 
c6fe0 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 67  on the local pag
c6ff0 65 2e 20 20 4e 6f 20 6f 76 65 72 66 6c 6f 77 20  e.  No overflow 
c7000 69 73 20 72 65 71 75 69 72 65 64 2e 0a 20 20 20  is required..   
c7010 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 53 69 7a   */.    int nSiz
c7020 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  e;          /* T
c7030 6f 74 61 6c 20 73 69 7a 65 20 6f 66 20 63 65 6c  otal size of cel
c7040 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 20 62 79 74  l content in byt
c7050 65 73 20 2a 2f 0a 20 20 20 20 70 49 6e 66 6f 2d  es */.    pInfo-
c7060 3e 6e 4c 6f 63 61 6c 20 3d 20 6e 50 61 79 6c 6f  >nLocal = nPaylo
c7070 61 64 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 69  ad;.    pInfo->i
c7080 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20  Overflow = 0;.  
c7090 20 20 6e 53 69 7a 65 20 3d 20 6e 50 61 79 6c 6f    nSize = nPaylo
c70a0 61 64 20 2b 20 6e 3b 0a 20 20 20 20 69 66 28 20  ad + n;.    if( 
c70b0 6e 53 69 7a 65 3c 34 20 29 7b 0a 20 20 20 20 20  nSize<4 ){.     
c70c0 20 6e 53 69 7a 65 20 3d 20 34 3b 20 20 20 20 20   nSize = 4;     
c70d0 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65     /* Minimum ce
c70e0 6c 6c 20 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a  ll size is 4 */.
c70f0 20 20 20 20 7d 0a 20 20 20 20 70 49 6e 66 6f 2d      }.    pInfo-
c7100 3e 6e 53 69 7a 65 20 3d 20 6e 53 69 7a 65 3b 0a  >nSize = nSize;.
c7110 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
c7120 49 66 20 74 68 65 20 70 61 79 6c 6f 61 64 20 77  If the payload w
c7130 69 6c 6c 20 6e 6f 74 20 66 69 74 20 63 6f 6d 70  ill not fit comp
c7140 6c 65 74 65 6c 79 20 6f 6e 20 74 68 65 20 6c 6f  letely on the lo
c7150 63 61 6c 20 70 61 67 65 2c 20 77 65 20 68 61 76  cal page, we hav
c7160 65 0a 20 20 20 20 2a 2a 20 74 6f 20 64 65 63 69  e.    ** to deci
c7170 64 65 20 68 6f 77 20 6d 75 63 68 20 74 6f 20 73  de how much to s
c7180 74 6f 72 65 20 6c 6f 63 61 6c 6c 79 20 61 6e 64  tore locally and
c7190 20 68 6f 77 20 6d 75 63 68 20 74 6f 20 73 70 69   how much to spi
c71a0 6c 6c 20 6f 6e 74 6f 0a 20 20 20 20 2a 2a 20 6f  ll onto.    ** o
c71b0 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 20  verflow pages.  
c71c0 54 68 65 20 73 74 72 61 74 65 67 79 20 69 73 20  The strategy is 
c71d0 74 6f 20 6d 69 6e 69 6d 69 7a 65 20 74 68 65 20  to minimize the 
c71e0 61 6d 6f 75 6e 74 20 6f 66 20 75 6e 75 73 65 64  amount of unused
c71f0 0a 20 20 20 20 2a 2a 20 73 70 61 63 65 20 6f 6e  .    ** space on
c7200 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20   overflow pages 
c7210 77 68 69 6c 65 20 6b 65 65 70 69 6e 67 20 74 68  while keeping th
c7220 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6c 6f 63 61  e amount of loca
c7230 6c 20 73 74 6f 72 61 67 65 0a 20 20 20 20 2a 2a  l storage.    **
c7240 20 69 6e 20 62 65 74 77 65 65 6e 20 6d 69 6e 4c   in between minL
c7250 6f 63 61 6c 20 61 6e 64 20 6d 61 78 4c 6f 63 61  ocal and maxLoca
c7260 6c 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  l..    **.    **
c7270 20 57 61 72 6e 69 6e 67 3a 20 20 63 68 61 6e 67   Warning:  chang
c7280 69 6e 67 20 74 68 65 20 77 61 79 20 6f 76 65 72  ing the way over
c7290 66 6c 6f 77 20 70 61 79 6c 6f 61 64 20 69 73 20  flow payload is 
c72a0 64 69 73 74 72 69 62 75 74 65 64 20 69 6e 20 61  distributed in a
c72b0 6e 79 0a 20 20 20 20 2a 2a 20 77 61 79 20 77 69  ny.    ** way wi
c72c0 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 61 6e 20  ll result in an 
c72d0 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 66 69 6c  incompatible fil
c72e0 65 20 66 6f 72 6d 61 74 2e 0a 20 20 20 20 2a 2f  e format..    */
c72f0 0a 20 20 20 20 69 6e 74 20 6d 69 6e 4c 6f 63 61  .    int minLoca
c7300 6c 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 61  l;  /* Minimum a
c7310 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64  mount of payload
c7320 20 68 65 6c 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f   held locally */
c7330 0a 20 20 20 20 69 6e 74 20 6d 61 78 4c 6f 63 61  .    int maxLoca
c7340 6c 3b 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61  l;  /* Maximum a
c7350 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64  mount of payload
c7360 20 68 65 6c 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f   held locally */
c7370 0a 20 20 20 20 69 6e 74 20 73 75 72 70 6c 75 73  .    int surplus
c7380 3b 20 20 20 2f 2a 20 4f 76 65 72 66 6c 6f 77 20  ;   /* Overflow 
c7390 70 61 79 6c 6f 61 64 20 61 76 61 69 6c 61 62 6c  payload availabl
c73a0 65 20 66 6f 72 20 6c 6f 63 61 6c 20 73 74 6f 72  e for local stor
c73b0 61 67 65 20 2a 2f 0a 0a 20 20 20 20 6d 69 6e 4c  age */..    minL
c73c0 6f 63 61 6c 20 3d 20 70 50 61 67 65 2d 3e 6d 69  ocal = pPage->mi
c73d0 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 6d 61 78 4c  nLocal;.    maxL
c73e0 6f 63 61 6c 20 3d 20 70 50 61 67 65 2d 3e 6d 61  ocal = pPage->ma
c73f0 78 4c 6f 63 61 6c 3b 0a 20 20 20 20 73 75 72 70  xLocal;.    surp
c7400 6c 75 73 20 3d 20 6d 69 6e 4c 6f 63 61 6c 20 2b  lus = minLocal +
c7410 20 28 6e 50 61 79 6c 6f 61 64 20 2d 20 6d 69 6e   (nPayload - min
c7420 4c 6f 63 61 6c 29 25 28 70 50 61 67 65 2d 3e 70  Local)%(pPage->p
c7430 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d  Bt->usableSize -
c7440 20 34 29 3b 0a 20 20 20 20 69 66 28 20 73 75 72   4);.    if( sur
c7450 70 6c 75 73 20 3c 3d 20 6d 61 78 4c 6f 63 61 6c  plus <= maxLocal
c7460 20 29 7b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d   ){.      pInfo-
c7470 3e 6e 4c 6f 63 61 6c 20 3d 20 73 75 72 70 6c 75  >nLocal = surplu
c7480 73 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  s;.    }else{.  
c7490 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61      pInfo->nLoca
c74a0 6c 20 3d 20 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20  l = minLocal;.  
c74b0 20 20 7d 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 69    }.    pInfo->i
c74c0 4f 76 65 72 66 6c 6f 77 20 3d 20 70 49 6e 66 6f  Overflow = pInfo
c74d0 2d 3e 6e 4c 6f 63 61 6c 20 2b 20 6e 3b 0a 20 20  ->nLocal + n;.  
c74e0 20 20 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d    pInfo->nSize =
c74f0 20 70 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f   pInfo->iOverflo
c7500 77 20 2b 20 34 3b 0a 20 20 7d 0a 7d 0a 23 64 65  w + 4;.  }.}.#de
c7510 66 69 6e 65 20 70 61 72 73 65 43 65 6c 6c 28 70  fine parseCell(p
c7520 50 61 67 65 2c 20 69 43 65 6c 6c 2c 20 70 49 6e  Page, iCell, pIn
c7530 66 6f 29 20 5c 0a 20 20 73 71 6c 69 74 65 33 42  fo) \.  sqlite3B
c7540 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72  treeParseCellPtr
c7550 28 28 70 50 61 67 65 29 2c 20 66 69 6e 64 43 65  ((pPage), findCe
c7560 6c 6c 28 28 70 50 61 67 65 29 2c 20 28 69 43 65  ll((pPage), (iCe
c7570 6c 6c 29 29 2c 20 28 70 49 6e 66 6f 29 29 0a 53  ll)), (pInfo)).S
c7580 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
c7590 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 50  id sqlite3BtreeP
c75a0 61 72 73 65 43 65 6c 6c 28 0a 20 20 4d 65 6d 50  arseCell(.  MemP
c75b0 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 20 20  age *pPage,     
c75c0 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74      /* Page cont
c75d0 61 69 6e 69 6e 67 20 74 68 65 20 63 65 6c 6c 20  aining the cell 
c75e0 2a 2f 0a 20 20 69 6e 74 20 69 43 65 6c 6c 2c 20  */.  int iCell, 
c75f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
c7600 54 68 65 20 63 65 6c 6c 20 69 6e 64 65 78 2e 20  The cell index. 
c7610 20 46 69 72 73 74 20 63 65 6c 6c 20 69 73 20 30   First cell is 0
c7620 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 2a   */.  CellInfo *
c7630 70 49 6e 66 6f 20 20 20 20 20 20 20 20 20 2f 2a  pInfo         /*
c7640 20 46 69 6c 6c 20 69 6e 20 74 68 69 73 20 73 74   Fill in this st
c7650 72 75 63 74 75 72 65 20 2a 2f 0a 29 7b 0a 20 20  ructure */.){.  
c7660 70 61 72 73 65 43 65 6c 6c 28 70 50 61 67 65 2c  parseCell(pPage,
c7670 20 69 43 65 6c 6c 2c 20 70 49 6e 66 6f 29 3b 0a   iCell, pInfo);.
c7680 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65  }../*.** Compute
c7690 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65   the total numbe
c76a0 72 20 6f 66 20 62 79 74 65 73 20 74 68 61 74 20  r of bytes that 
c76b0 61 20 43 65 6c 6c 20 6e 65 65 64 73 20 69 6e 20  a Cell needs in 
c76c0 74 68 65 20 63 65 6c 6c 0a 2a 2a 20 64 61 74 61  the cell.** data
c76d0 20 61 72 65 61 20 6f 66 20 74 68 65 20 62 74 72   area of the btr
c76e0 65 65 2d 70 61 67 65 2e 20 20 54 68 65 20 72 65  ee-page.  The re
c76f0 74 75 72 6e 20 6e 75 6d 62 65 72 20 69 6e 63 6c  turn number incl
c7700 75 64 65 73 20 74 68 65 20 63 65 6c 6c 0a 2a 2a  udes the cell.**
c7710 20 64 61 74 61 20 68 65 61 64 65 72 20 61 6e 64   data header and
c7720 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 79 6c 6f   the local paylo
c7730 61 64 2c 20 62 75 74 20 6e 6f 74 20 61 6e 79 20  ad, but not any 
c7740 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6f 72  overflow page or
c7750 0a 2a 2a 20 74 68 65 20 73 70 61 63 65 20 75 73  .** the space us
c7760 65 64 20 62 79 20 74 68 65 20 63 65 6c 6c 20 70  ed by the cell p
c7770 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 23 69 66 6e 64  ointer..*/.#ifnd
c7780 65 66 20 4e 44 45 42 55 47 0a 73 74 61 74 69 63  ef NDEBUG.static
c7790 20 69 6e 74 20 63 65 6c 6c 53 69 7a 65 28 4d 65   int cellSize(Me
c77a0 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e  mPage *pPage, in
c77b0 74 20 69 43 65 6c 6c 29 7b 0a 20 20 43 65 6c 6c  t iCell){.  Cell
c77c0 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 73 71 6c  Info info;.  sql
c77d0 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65  ite3BtreeParseCe
c77e0 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 2c  ll(pPage, iCell,
c77f0 20 26 69 6e 66 6f 29 3b 0a 20 20 72 65 74 75 72   &info);.  retur
c7800 6e 20 69 6e 66 6f 2e 6e 53 69 7a 65 3b 0a 7d 0a  n info.nSize;.}.
c7810 23 65 6e 64 69 66 0a 73 74 61 74 69 63 20 69 6e  #endif.static in
c7820 74 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 4d 65  t cellSizePtr(Me
c7830 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 75 38  mPage *pPage, u8
c7840 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 43 65 6c 6c   *pCell){.  Cell
c7850 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 73 71 6c  Info info;.  sql
c7860 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65  ite3BtreeParseCe
c7870 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65  llPtr(pPage, pCe
c7880 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 72 65  ll, &info);.  re
c7890 74 75 72 6e 20 69 6e 66 6f 2e 6e 53 69 7a 65 3b  turn info.nSize;
c78a0 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
c78b0 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
c78c0 55 4d 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20  UM./*.** If the 
c78d0 63 65 6c 6c 20 70 43 65 6c 6c 2c 20 70 61 72 74  cell pCell, part
c78e0 20 6f 66 20 70 61 67 65 20 70 50 61 67 65 20 63   of page pPage c
c78f0 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65  ontains a pointe
c7900 72 0a 2a 2a 20 74 6f 20 61 6e 20 6f 76 65 72 66  r.** to an overf
c7910 6c 6f 77 20 70 61 67 65 2c 20 69 6e 73 65 72 74  low page, insert
c7920 20 61 6e 20 65 6e 74 72 79 20 69 6e 74 6f 20 74   an entry into t
c7930 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 2a  he pointer-map.*
c7940 2a 20 66 6f 72 20 74 68 65 20 6f 76 65 72 66 6c  * for the overfl
c7950 6f 77 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74  ow page..*/.stat
c7960 69 63 20 69 6e 74 20 70 74 72 6d 61 70 50 75 74  ic int ptrmapPut
c7970 4f 76 66 6c 50 74 72 28 4d 65 6d 50 61 67 65 20  OvflPtr(MemPage 
c7980 2a 70 50 61 67 65 2c 20 75 38 20 2a 70 43 65 6c  *pPage, u8 *pCel
c7990 6c 29 7b 0a 20 20 69 66 28 20 70 43 65 6c 6c 20  l){.  if( pCell 
c79a0 29 7b 0a 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20  ){.    CellInfo 
c79b0 69 6e 66 6f 3b 0a 20 20 20 20 73 71 6c 69 74 65  info;.    sqlite
c79c0 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50  3BtreeParseCellP
c79d0 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c  tr(pPage, pCell,
c79e0 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 61 73 73   &info);.    ass
c79f0 65 72 74 28 20 28 69 6e 66 6f 2e 6e 44 61 74 61  ert( (info.nData
c7a00 2b 28 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 3f  +(pPage->intKey?
c7a10 30 3a 69 6e 66 6f 2e 6e 4b 65 79 29 29 3d 3d 69  0:info.nKey))==i
c7a20 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a  nfo.nPayload );.
c7a30 20 20 20 20 69 66 28 20 28 69 6e 66 6f 2e 6e 44      if( (info.nD
c7a40 61 74 61 2b 28 70 50 61 67 65 2d 3e 69 6e 74 4b  ata+(pPage->intK
c7a50 65 79 3f 30 3a 69 6e 66 6f 2e 6e 4b 65 79 29 29  ey?0:info.nKey))
c7a60 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 7b 0a  >info.nLocal ){.
c7a70 20 20 20 20 20 20 50 67 6e 6f 20 6f 76 66 6c 20        Pgno ovfl 
c7a80 3d 20 67 65 74 34 62 79 74 65 28 26 70 43 65 6c  = get4byte(&pCel
c7a90 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77  l[info.iOverflow
c7aa0 5d 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  ]);.      return
c7ab0 20 70 74 72 6d 61 70 50 75 74 28 70 50 61 67 65   ptrmapPut(pPage
c7ac0 2d 3e 70 42 74 2c 20 6f 76 66 6c 2c 20 50 54 52  ->pBt, ovfl, PTR
c7ad0 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 2c 20 70  MAP_OVERFLOW1, p
c7ae0 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20  Page->pgno);.   
c7af0 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
c7b00 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 2f 2a 0a  SQLITE_OK;.}./*.
c7b10 2a 2a 20 49 66 20 74 68 65 20 63 65 6c 6c 20 77  ** If the cell w
c7b20 69 74 68 20 69 6e 64 65 78 20 69 43 65 6c 6c 20  ith index iCell 
c7b30 6f 6e 20 70 61 67 65 20 70 50 61 67 65 20 63 6f  on page pPage co
c7b40 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72  ntains a pointer
c7b50 0a 2a 2a 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c  .** to an overfl
c7b60 6f 77 20 70 61 67 65 2c 20 69 6e 73 65 72 74 20  ow page, insert 
c7b70 61 6e 20 65 6e 74 72 79 20 69 6e 74 6f 20 74 68  an entry into th
c7b80 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 2a 2a  e pointer-map.**
c7b90 20 66 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f   for the overflo
c7ba0 77 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69  w page..*/.stati
c7bb0 63 20 69 6e 74 20 70 74 72 6d 61 70 50 75 74 4f  c int ptrmapPutO
c7bc0 76 66 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61  vfl(MemPage *pPa
c7bd0 67 65 2c 20 69 6e 74 20 69 43 65 6c 6c 29 7b 0a  ge, int iCell){.
c7be0 20 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 70    u8 *pCell;.  p
c7bf0 43 65 6c 6c 20 3d 20 66 69 6e 64 4f 76 65 72 66  Cell = findOverf
c7c00 6c 6f 77 43 65 6c 6c 28 70 50 61 67 65 2c 20 69  lowCell(pPage, i
c7c10 43 65 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20  Cell);.  return 
c7c20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72  ptrmapPutOvflPtr
c7c30 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a  (pPage, pCell);.
c7c40 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  }.#endif.../*.**
c7c50 20 44 65 66 72 61 67 6d 65 6e 74 20 74 68 65 20   Defragment the 
c7c60 70 61 67 65 20 67 69 76 65 6e 2e 20 20 41 6c 6c  page given.  All
c7c70 20 43 65 6c 6c 73 20 61 72 65 20 6d 6f 76 65 64   Cells are moved
c7c80 20 74 6f 20 74 68 65 0a 2a 2a 20 65 6e 64 20 6f   to the.** end o
c7c90 66 20 74 68 65 20 70 61 67 65 20 61 6e 64 20 61  f the page and a
c7ca0 6c 6c 20 66 72 65 65 20 73 70 61 63 65 20 69 73  ll free space is
c7cb0 20 63 6f 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20   collected into 
c7cc0 6f 6e 65 0a 2a 2a 20 62 69 67 20 46 72 65 65 42  one.** big FreeB
c7cd0 6c 6b 20 74 68 61 74 20 6f 63 63 75 72 73 20 69  lk that occurs i
c7ce0 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 68 65  n between the he
c7cf0 61 64 65 72 20 61 6e 64 20 63 65 6c 6c 0a 2a 2a  ader and cell.**
c7d00 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 61   pointer array a
c7d10 6e 64 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  nd the cell cont
c7d20 65 6e 74 20 61 72 65 61 2e 0a 2a 2f 0a 73 74 61  ent area..*/.sta
c7d30 74 69 63 20 69 6e 74 20 64 65 66 72 61 67 6d 65  tic int defragme
c7d40 6e 74 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a  ntPage(MemPage *
c7d50 70 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 69 3b  pPage){.  int i;
c7d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c7d70 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
c7d80 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 63  nter */.  int pc
c7d90 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
c7da0 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20       /* Address 
c7db0 6f 66 20 61 20 69 2d 74 68 20 63 65 6c 6c 20 2a  of a i-th cell *
c7dc0 2f 0a 20 20 69 6e 74 20 61 64 64 72 3b 20 20 20  /.  int addr;   
c7dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
c7de0 2a 20 4f 66 66 73 65 74 20 6f 66 20 66 69 72 73  * Offset of firs
c7df0 74 20 62 79 74 65 20 61 66 74 65 72 20 63 65 6c  t byte after cel
c7e00 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20  l pointer array 
c7e10 2a 2f 0a 20 20 69 6e 74 20 68 64 72 3b 20 20 20  */.  int hdr;   
c7e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c7e30 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 68 65  /* Offset to the
c7e40 20 70 61 67 65 20 68 65 61 64 65 72 20 2a 2f 0a   page header */.
c7e50 20 20 69 6e 74 20 73 69 7a 65 3b 20 20 20 20 20    int size;     
c7e60 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
c7e70 53 69 7a 65 20 6f 66 20 61 20 63 65 6c 6c 20 2a  Size of a cell *
c7e80 2f 0a 20 20 69 6e 74 20 75 73 61 62 6c 65 53 69  /.  int usableSi
c7e90 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ze;            /
c7ea0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 61 62  * Number of usab
c7eb0 6c 65 20 62 79 74 65 73 20 6f 6e 20 61 20 70 61  le bytes on a pa
c7ec0 67 65 20 2a 2f 0a 20 20 69 6e 74 20 63 65 6c 6c  ge */.  int cell
c7ed0 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20  Offset;         
c7ee0 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20     /* Offset to 
c7ef0 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72  the cell pointer
c7f00 20 61 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20   array */.  int 
c7f10 62 72 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20  brk;            
c7f20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74         /* Offset
c7f30 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e   to the cell con
c7f40 74 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20 20 69  tent area */.  i
c7f50 6e 74 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20  nt nCell;       
c7f60 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
c7f70 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20  ber of cells on 
c7f80 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 6e  the page */.  un
c7f90 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74  signed char *dat
c7fa0 61 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  a;       /* The 
c7fb0 70 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 75  page data */.  u
c7fc0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 74 65  nsigned char *te
c7fd0 6d 70 3b 20 20 20 20 20 20 20 2f 2a 20 54 65 6d  mp;       /* Tem
c7fe0 70 20 61 72 65 61 20 66 6f 72 20 63 65 6c 6c 20  p area for cell 
c7ff0 63 6f 6e 74 65 6e 74 20 2a 2f 0a 0a 20 20 61 73  content */..  as
c8000 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67  sert( sqlite3Pag
c8010 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50  erIswriteable(pP
c8020 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b  age->pDbPage) );
c8030 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
c8040 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73  ->pBt!=0 );.  as
c8050 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74  sert( pPage->pBt
c8060 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3c 3d 20  ->usableSize <= 
c8070 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f  SQLITE_MAX_PAGE_
c8080 53 49 5a 45 20 29 3b 0a 20 20 61 73 73 65 72 74  SIZE );.  assert
c8090 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c  ( pPage->nOverfl
c80a0 6f 77 3d 3d 30 20 29 3b 0a 20 20 74 65 6d 70 20  ow==0 );.  temp 
c80b0 3d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 20  = sqliteMalloc( 
c80c0 70 50 61 67 65 2d 3e 70 42 74 2d 3e 70 61 67 65  pPage->pBt->page
c80d0 53 69 7a 65 20 29 3b 0a 20 20 69 66 28 20 74 65  Size );.  if( te
c80e0 6d 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53  mp==0 ) return S
c80f0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 64  QLITE_NOMEM;.  d
c8100 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61  ata = pPage->aDa
c8110 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50 61 67  ta;.  hdr = pPag
c8120 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20  e->hdrOffset;.  
c8130 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 70 50 61  cellOffset = pPa
c8140 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 3b 0a  ge->cellOffset;.
c8150 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d    nCell = pPage-
c8160 3e 6e 43 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74  >nCell;.  assert
c8170 28 20 6e 43 65 6c 6c 3d 3d 67 65 74 32 62 79 74  ( nCell==get2byt
c8180 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 29 20  e(&data[hdr+3]) 
c8190 29 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a 65 20  );.  usableSize 
c81a0 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73  = pPage->pBt->us
c81b0 61 62 6c 65 53 69 7a 65 3b 0a 20 20 62 72 6b 20  ableSize;.  brk 
c81c0 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  = get2byte(&data
c81d0 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 6d 65 6d 63  [hdr+5]);.  memc
c81e0 70 79 28 26 74 65 6d 70 5b 62 72 6b 5d 2c 20 26  py(&temp[brk], &
c81f0 64 61 74 61 5b 62 72 6b 5d 2c 20 75 73 61 62 6c  data[brk], usabl
c8200 65 53 69 7a 65 20 2d 20 62 72 6b 29 3b 0a 20 20  eSize - brk);.  
c8210 62 72 6b 20 3d 20 75 73 61 62 6c 65 53 69 7a 65  brk = usableSize
c8220 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e  ;.  for(i=0; i<n
c8230 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Cell; i++){.    
c8240 75 38 20 2a 70 41 64 64 72 3b 20 20 20 20 20 2f  u8 *pAddr;     /
c8250 2a 20 54 68 65 20 69 2d 74 68 20 63 65 6c 6c 20  * The i-th cell 
c8260 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 20 20 70  pointer */.    p
c8270 41 64 64 72 20 3d 20 26 64 61 74 61 5b 63 65 6c  Addr = &data[cel
c8280 6c 4f 66 66 73 65 74 20 2b 20 69 2a 32 5d 3b 0a  lOffset + i*2];.
c8290 20 20 20 20 70 63 20 3d 20 67 65 74 32 62 79 74      pc = get2byt
c82a0 65 28 70 41 64 64 72 29 3b 0a 20 20 20 20 61 73  e(pAddr);.    as
c82b0 73 65 72 74 28 20 70 63 3c 70 50 61 67 65 2d 3e  sert( pc<pPage->
c82c0 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20  pBt->usableSize 
c82d0 29 3b 0a 20 20 20 20 73 69 7a 65 20 3d 20 63 65  );.    size = ce
c82e0 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c  llSizePtr(pPage,
c82f0 20 26 74 65 6d 70 5b 70 63 5d 29 3b 0a 20 20 20   &temp[pc]);.   
c8300 20 62 72 6b 20 2d 3d 20 73 69 7a 65 3b 0a 20 20   brk -= size;.  
c8310 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61 5b 62    memcpy(&data[b
c8320 72 6b 5d 2c 20 26 74 65 6d 70 5b 70 63 5d 2c 20  rk], &temp[pc], 
c8330 73 69 7a 65 29 3b 0a 20 20 20 20 70 75 74 32 62  size);.    put2b
c8340 79 74 65 28 70 41 64 64 72 2c 20 62 72 6b 29 3b  yte(pAddr, brk);
c8350 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 62  .  }.  assert( b
c8360 72 6b 3e 3d 63 65 6c 6c 4f 66 66 73 65 74 2b 32  rk>=cellOffset+2
c8370 2a 6e 43 65 6c 6c 20 29 3b 0a 20 20 70 75 74 32  *nCell );.  put2
c8380 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35  byte(&data[hdr+5
c8390 5d 2c 20 62 72 6b 29 3b 0a 20 20 64 61 74 61 5b  ], brk);.  data[
c83a0 68 64 72 2b 31 5d 20 3d 20 30 3b 0a 20 20 64 61  hdr+1] = 0;.  da
c83b0 74 61 5b 68 64 72 2b 32 5d 20 3d 20 30 3b 0a 20  ta[hdr+2] = 0;. 
c83c0 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 3d 20 30   data[hdr+7] = 0
c83d0 3b 0a 20 20 61 64 64 72 20 3d 20 63 65 6c 6c 4f  ;.  addr = cellO
c83e0 66 66 73 65 74 2b 32 2a 6e 43 65 6c 6c 3b 0a 20  ffset+2*nCell;. 
c83f0 20 6d 65 6d 73 65 74 28 26 64 61 74 61 5b 61 64   memset(&data[ad
c8400 64 72 5d 2c 20 30 2c 20 62 72 6b 2d 61 64 64 72  dr], 0, brk-addr
c8410 29 3b 0a 20 20 73 71 6c 69 74 65 46 72 65 65 28  );.  sqliteFree(
c8420 74 65 6d 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  temp);.  return 
c8430 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
c8440 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79  .** Allocate nBy
c8450 74 65 20 62 79 74 65 73 20 6f 66 20 73 70 61 63  te bytes of spac
c8460 65 20 6f 6e 20 61 20 70 61 67 65 2e 0a 2a 2a 0a  e on a page..**.
c8470 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e  ** Return the in
c8480 64 65 78 20 69 6e 74 6f 20 70 50 61 67 65 2d 3e  dex into pPage->
c8490 61 44 61 74 61 5b 5d 20 6f 66 20 74 68 65 20 66  aData[] of the f
c84a0 69 72 73 74 20 62 79 74 65 20 6f 66 0a 2a 2a 20  irst byte of.** 
c84b0 74 68 65 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69  the new allocati
c84c0 6f 6e 2e 20 4f 72 20 72 65 74 75 72 6e 20 30 20  on. Or return 0 
c84d0 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20  if there is not 
c84e0 65 6e 6f 75 67 68 20 66 72 65 65 0a 2a 2a 20 73  enough free.** s
c84f0 70 61 63 65 20 6f 6e 20 74 68 65 20 70 61 67 65  pace on the page
c8500 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 20   to satisfy the 
c8510 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 65  allocation reque
c8520 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  st..**.** If the
c8530 20 70 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 6e   page contains n
c8540 42 79 74 65 73 20 6f 66 20 66 72 65 65 20 73 70  Bytes of free sp
c8550 61 63 65 20 62 75 74 20 64 6f 65 73 20 6e 6f 74  ace but does not
c8560 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20 6e 42 79 74   contain.** nByt
c8570 65 73 20 6f 66 20 63 6f 6e 74 69 67 75 6f 75 73  es of contiguous
c8580 20 66 72 65 65 20 73 70 61 63 65 2c 20 74 68 65   free space, the
c8590 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61  n this routine a
c85a0 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20  utomatically.** 
c85b0 63 61 6c 6c 73 20 64 65 66 72 61 67 65 6d 65 6e  calls defragemen
c85c0 74 50 61 67 65 28 29 20 74 6f 20 63 6f 6e 73 6f  tPage() to conso
c85d0 6c 69 64 61 74 65 20 61 6c 6c 20 66 72 65 65 20  lidate all free 
c85e0 73 70 61 63 65 20 62 65 66 6f 72 65 20 0a 2a 2a  space before .**
c85f0 20 61 6c 6c 6f 63 61 74 69 6e 67 20 74 68 65 20   allocating the 
c8600 6e 65 77 20 63 68 75 6e 6b 2e 0a 2a 2f 0a 73 74  new chunk..*/.st
c8610 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74  atic int allocat
c8620 65 53 70 61 63 65 28 4d 65 6d 50 61 67 65 20 2a  eSpace(MemPage *
c8630 70 50 61 67 65 2c 20 69 6e 74 20 6e 42 79 74 65  pPage, int nByte
c8640 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 2c 20 70  ){.  int addr, p
c8650 63 2c 20 68 64 72 3b 0a 20 20 69 6e 74 20 73 69  c, hdr;.  int si
c8660 7a 65 3b 0a 20 20 69 6e 74 20 6e 46 72 61 67 3b  ze;.  int nFrag;
c8670 0a 20 20 69 6e 74 20 74 6f 70 3b 0a 20 20 69 6e  .  int top;.  in
c8680 74 20 6e 43 65 6c 6c 3b 0a 20 20 69 6e 74 20 63  t nCell;.  int c
c8690 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 75 6e 73  ellOffset;.  uns
c86a0 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61  igned char *data
c86b0 3b 0a 20 20 0a 20 20 64 61 74 61 20 3d 20 70 50  ;.  .  data = pP
c86c0 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 61 73  age->aData;.  as
c86d0 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67  sert( sqlite3Pag
c86e0 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50  erIswriteable(pP
c86f0 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b  age->pDbPage) );
c8700 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
c8710 2d 3e 70 42 74 20 29 3b 0a 20 20 69 66 28 20 6e  ->pBt );.  if( n
c8720 42 79 74 65 3c 34 20 29 20 6e 42 79 74 65 20 3d  Byte<4 ) nByte =
c8730 20 34 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d   4;.  if( pPage-
c8740 3e 6e 46 72 65 65 3c 6e 42 79 74 65 20 7c 7c 20  >nFree<nByte || 
c8750 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77  pPage->nOverflow
c8760 3e 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  >0 ) return 0;. 
c8770 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 2d 3d   pPage->nFree -=
c8780 20 6e 42 79 74 65 3b 0a 20 20 68 64 72 20 3d 20   nByte;.  hdr = 
c8790 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74  pPage->hdrOffset
c87a0 3b 0a 0a 20 20 6e 46 72 61 67 20 3d 20 64 61 74  ;..  nFrag = dat
c87b0 61 5b 68 64 72 2b 37 5d 3b 0a 20 20 69 66 28 20  a[hdr+7];.  if( 
c87c0 6e 46 72 61 67 3c 36 30 20 29 7b 0a 20 20 20 20  nFrag<60 ){.    
c87d0 2f 2a 20 53 65 61 72 63 68 20 74 68 65 20 66 72  /* Search the fr
c87e0 65 65 6c 69 73 74 20 6c 6f 6f 6b 69 6e 67 20 66  eelist looking f
c87f0 6f 72 20 61 20 73 6c 6f 74 20 62 69 67 20 65 6e  or a slot big en
c8800 6f 75 67 68 20 74 6f 20 73 61 74 69 73 66 79 20  ough to satisfy 
c8810 74 68 65 0a 20 20 20 20 2a 2a 20 73 70 61 63 65  the.    ** space
c8820 20 72 65 71 75 65 73 74 2e 20 2a 2f 0a 20 20 20   request. */.   
c8830 20 61 64 64 72 20 3d 20 68 64 72 2b 31 3b 0a 20   addr = hdr+1;. 
c8840 20 20 20 77 68 69 6c 65 28 20 28 70 63 20 3d 20     while( (pc = 
c8850 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 61  get2byte(&data[a
c8860 64 64 72 5d 29 29 3e 30 20 29 7b 0a 20 20 20 20  ddr]))>0 ){.    
c8870 20 20 73 69 7a 65 20 3d 20 67 65 74 32 62 79 74    size = get2byt
c8880 65 28 26 64 61 74 61 5b 70 63 2b 32 5d 29 3b 0a  e(&data[pc+2]);.
c8890 20 20 20 20 20 20 69 66 28 20 73 69 7a 65 3e 3d        if( size>=
c88a0 6e 42 79 74 65 20 29 7b 0a 20 20 20 20 20 20 20  nByte ){.       
c88b0 20 69 66 28 20 73 69 7a 65 3c 6e 42 79 74 65 2b   if( size<nByte+
c88c0 34 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6d  4 ){.          m
c88d0 65 6d 63 70 79 28 26 64 61 74 61 5b 61 64 64 72  emcpy(&data[addr
c88e0 5d 2c 20 26 64 61 74 61 5b 70 63 5d 2c 20 32 29  ], &data[pc], 2)
c88f0 3b 0a 20 20 20 20 20 20 20 20 20 20 64 61 74 61  ;.          data
c8900 5b 68 64 72 2b 37 5d 20 3d 20 6e 46 72 61 67 20  [hdr+7] = nFrag 
c8910 2b 20 73 69 7a 65 20 2d 20 6e 42 79 74 65 3b 0a  + size - nByte;.
c8920 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
c8930 20 70 63 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c   pc;.        }el
c8940 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70 75  se{.          pu
c8950 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 63 2b  t2byte(&data[pc+
c8960 32 5d 2c 20 73 69 7a 65 2d 6e 42 79 74 65 29 3b  2], size-nByte);
c8970 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
c8980 6e 20 70 63 20 2b 20 73 69 7a 65 20 2d 20 6e 42  n pc + size - nB
c8990 79 74 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  yte;.        }. 
c89a0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 64 64       }.      add
c89b0 72 20 3d 20 70 63 3b 0a 20 20 20 20 7d 0a 20 20  r = pc;.    }.  
c89c0 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  }..  /* Allocate
c89d0 20 6d 65 6d 6f 72 79 20 66 72 6f 6d 20 74 68 65   memory from the
c89e0 20 67 61 70 20 69 6e 20 62 65 74 77 65 65 6e 20   gap in between 
c89f0 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72  the cell pointer
c8a00 20 61 72 72 61 79 0a 20 20 2a 2a 20 61 6e 64 20   array.  ** and 
c8a10 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74  the cell content
c8a20 20 61 72 65 61 2e 0a 20 20 2a 2f 0a 20 20 74 6f   area..  */.  to
c8a30 70 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61  p = get2byte(&da
c8a40 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 6e 43  ta[hdr+5]);.  nC
c8a50 65 6c 6c 20 3d 20 67 65 74 32 62 79 74 65 28 26  ell = get2byte(&
c8a60 64 61 74 61 5b 68 64 72 2b 33 5d 29 3b 0a 20 20  data[hdr+3]);.  
c8a70 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 70 50 61  cellOffset = pPa
c8a80 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 3b 0a  ge->cellOffset;.
c8a90 20 20 69 66 28 20 6e 46 72 61 67 3e 3d 36 30 20    if( nFrag>=60 
c8aa0 7c 7c 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20  || cellOffset + 
c8ab0 32 2a 6e 43 65 6c 6c 20 3e 20 74 6f 70 20 2d 20  2*nCell > top - 
c8ac0 6e 42 79 74 65 20 29 7b 0a 20 20 20 20 69 66 28  nByte ){.    if(
c8ad0 20 64 65 66 72 61 67 6d 65 6e 74 50 61 67 65 28   defragmentPage(
c8ae0 70 50 61 67 65 29 20 29 20 72 65 74 75 72 6e 20  pPage) ) return 
c8af0 30 3b 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74  0;.    top = get
c8b00 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b  2byte(&data[hdr+
c8b10 35 5d 29 3b 0a 20 20 7d 0a 20 20 74 6f 70 20 2d  5]);.  }.  top -
c8b20 3d 20 6e 42 79 74 65 3b 0a 20 20 61 73 73 65 72  = nByte;.  asser
c8b30 74 28 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20  t( cellOffset + 
c8b40 32 2a 6e 43 65 6c 6c 20 3c 3d 20 74 6f 70 20 29  2*nCell <= top )
c8b50 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61  ;.  put2byte(&da
c8b60 74 61 5b 68 64 72 2b 35 5d 2c 20 74 6f 70 29 3b  ta[hdr+5], top);
c8b70 0a 20 20 72 65 74 75 72 6e 20 74 6f 70 3b 0a 7d  .  return top;.}
c8b80 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ../*.** Return a
c8b90 20 73 65 63 74 69 6f 6e 20 6f 66 20 74 68 65 20   section of the 
c8ba0 70 50 61 67 65 2d 3e 61 44 61 74 61 20 74 6f 20  pPage->aData to 
c8bb0 74 68 65 20 66 72 65 65 6c 69 73 74 2e 0a 2a 2a  the freelist..**
c8bc0 20 54 68 65 20 66 69 72 73 74 20 62 79 74 65 20   The first byte 
c8bd0 6f 66 20 74 68 65 20 6e 65 77 20 66 72 65 65 20  of the new free 
c8be0 62 6c 6f 63 6b 20 69 73 20 70 50 61 67 65 2d 3e  block is pPage->
c8bf0 61 44 69 73 6b 5b 73 74 61 72 74 5d 0a 2a 2a 20  aDisk[start].** 
c8c00 61 6e 64 20 74 68 65 20 73 69 7a 65 20 6f 66 20  and the size of 
c8c10 74 68 65 20 62 6c 6f 63 6b 20 69 73 20 22 73 69  the block is "si
c8c20 7a 65 22 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a  ze" bytes..**.**
c8c30 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 65 66 66   Most of the eff
c8c40 6f 72 74 20 68 65 72 65 20 69 73 20 69 6e 76 6f  ort here is invo
c8c50 6c 76 65 64 20 69 6e 20 63 6f 61 6c 65 73 69 6e  lved in coalesin
c8c60 67 20 61 64 6a 61 63 65 6e 74 0a 2a 2a 20 66 72  g adjacent.** fr
c8c70 65 65 20 62 6c 6f 63 6b 73 20 69 6e 74 6f 20 61  ee blocks into a
c8c80 20 73 69 6e 67 6c 65 20 62 69 67 20 66 72 65 65   single big free
c8c90 20 62 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69   block..*/.stati
c8ca0 63 20 76 6f 69 64 20 66 72 65 65 53 70 61 63 65  c void freeSpace
c8cb0 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c  (MemPage *pPage,
c8cc0 20 69 6e 74 20 73 74 61 72 74 2c 20 69 6e 74 20   int start, int 
c8cd0 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 61 64 64  size){.  int add
c8ce0 72 2c 20 70 62 65 67 69 6e 2c 20 68 64 72 3b 0a  r, pbegin, hdr;.
c8cf0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
c8d00 2a 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61  *data = pPage->a
c8d10 44 61 74 61 3b 0a 0a 20 20 61 73 73 65 72 74 28  Data;..  assert(
c8d20 20 70 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 29   pPage->pBt!=0 )
c8d30 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
c8d40 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61  te3PagerIswritea
c8d50 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ble(pPage->pDbPa
c8d60 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ge) );.  assert(
c8d70 20 73 74 61 72 74 3e 3d 70 50 61 67 65 2d 3e 68   start>=pPage->h
c8d80 64 72 4f 66 66 73 65 74 2b 36 2b 28 70 50 61 67  drOffset+6+(pPag
c8d90 65 2d 3e 6c 65 61 66 3f 30 3a 34 29 20 29 3b 0a  e->leaf?0:4) );.
c8da0 20 20 61 73 73 65 72 74 28 20 28 73 74 61 72 74    assert( (start
c8db0 20 2b 20 73 69 7a 65 29 3c 3d 70 50 61 67 65 2d   + size)<=pPage-
c8dc0 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65  >pBt->usableSize
c8dd0 20 29 3b 0a 20 20 69 66 28 20 73 69 7a 65 3c 34   );.  if( size<4
c8de0 20 29 20 73 69 7a 65 20 3d 20 34 3b 0a 0a 23 69   ) size = 4;..#i
c8df0 66 64 65 66 20 53 51 4c 49 54 45 5f 53 45 43 55  fdef SQLITE_SECU
c8e00 52 45 5f 44 45 4c 45 54 45 0a 20 20 2f 2a 20 4f  RE_DELETE.  /* O
c8e10 76 65 72 77 72 69 74 65 20 64 65 6c 65 74 65 64  verwrite deleted
c8e20 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 69 74   information wit
c8e30 68 20 7a 65 72 6f 73 20 77 68 65 6e 20 74 68 65  h zeros when the
c8e40 20 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20 0a   SECURE_DELETE .
c8e50 20 20 2a 2a 20 6f 70 74 69 6f 6e 20 69 73 20 65    ** option is e
c8e60 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d 70 69 6c  nabled at compil
c8e70 65 2d 74 69 6d 65 20 2a 2f 0a 20 20 6d 65 6d 73  e-time */.  mems
c8e80 65 74 28 26 64 61 74 61 5b 73 74 61 72 74 5d 2c  et(&data[start],
c8e90 20 30 2c 20 73 69 7a 65 29 3b 0a 23 65 6e 64 69   0, size);.#endi
c8ea0 66 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20  f..  /* Add the 
c8eb0 73 70 61 63 65 20 62 61 63 6b 20 69 6e 74 6f 20  space back into 
c8ec0 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  the linked list 
c8ed0 6f 66 20 66 72 65 65 62 6c 6f 63 6b 73 20 2a 2f  of freeblocks */
c8ee0 0a 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e  .  hdr = pPage->
c8ef0 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 61 64 64  hdrOffset;.  add
c8f00 72 20 3d 20 68 64 72 20 2b 20 31 3b 0a 20 20 77  r = hdr + 1;.  w
c8f10 68 69 6c 65 28 20 28 70 62 65 67 69 6e 20 3d 20  hile( (pbegin = 
c8f20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 61  get2byte(&data[a
c8f30 64 64 72 5d 29 29 3c 73 74 61 72 74 20 26 26 20  ddr]))<start && 
c8f40 70 62 65 67 69 6e 3e 30 20 29 7b 0a 20 20 20 20  pbegin>0 ){.    
c8f50 61 73 73 65 72 74 28 20 70 62 65 67 69 6e 3c 3d  assert( pbegin<=
c8f60 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62  pPage->pBt->usab
c8f70 6c 65 53 69 7a 65 2d 34 20 29 3b 0a 20 20 20 20  leSize-4 );.    
c8f80 61 73 73 65 72 74 28 20 70 62 65 67 69 6e 3e 61  assert( pbegin>a
c8f90 64 64 72 20 29 3b 0a 20 20 20 20 61 64 64 72 20  ddr );.    addr 
c8fa0 3d 20 70 62 65 67 69 6e 3b 0a 20 20 7d 0a 20 20  = pbegin;.  }.  
c8fb0 61 73 73 65 72 74 28 20 70 62 65 67 69 6e 3c 3d  assert( pbegin<=
c8fc0 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62  pPage->pBt->usab
c8fd0 6c 65 53 69 7a 65 2d 34 20 29 3b 0a 20 20 61 73  leSize-4 );.  as
c8fe0 73 65 72 74 28 20 70 62 65 67 69 6e 3e 61 64 64  sert( pbegin>add
c8ff0 72 20 7c 7c 20 70 62 65 67 69 6e 3d 3d 30 20 29  r || pbegin==0 )
c9000 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61  ;.  put2byte(&da
c9010 74 61 5b 61 64 64 72 5d 2c 20 73 74 61 72 74 29  ta[addr], start)
c9020 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61  ;.  put2byte(&da
c9030 74 61 5b 73 74 61 72 74 5d 2c 20 70 62 65 67 69  ta[start], pbegi
c9040 6e 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26  n);.  put2byte(&
c9050 64 61 74 61 5b 73 74 61 72 74 2b 32 5d 2c 20 73  data[start+2], s
c9060 69 7a 65 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e  ize);.  pPage->n
c9070 46 72 65 65 20 2b 3d 20 73 69 7a 65 3b 0a 0a 20  Free += size;.. 
c9080 20 2f 2a 20 43 6f 61 6c 65 73 63 65 20 61 64 6a   /* Coalesce adj
c9090 61 63 65 6e 74 20 66 72 65 65 20 62 6c 6f 63 6b  acent free block
c90a0 73 20 2a 2f 0a 20 20 61 64 64 72 20 3d 20 70 50  s */.  addr = pP
c90b0 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 20 2b  age->hdrOffset +
c90c0 20 31 3b 0a 20 20 77 68 69 6c 65 28 20 28 70 62   1;.  while( (pb
c90d0 65 67 69 6e 20 3d 20 67 65 74 32 62 79 74 65 28  egin = get2byte(
c90e0 26 64 61 74 61 5b 61 64 64 72 5d 29 29 3e 30 20  &data[addr]))>0 
c90f0 29 7b 0a 20 20 20 20 69 6e 74 20 70 6e 65 78 74  ){.    int pnext
c9100 2c 20 70 73 69 7a 65 3b 0a 20 20 20 20 61 73 73  , psize;.    ass
c9110 65 72 74 28 20 70 62 65 67 69 6e 3e 61 64 64 72  ert( pbegin>addr
c9120 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
c9130 70 62 65 67 69 6e 3c 3d 70 50 61 67 65 2d 3e 70  pbegin<=pPage->p
c9140 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 34  Bt->usableSize-4
c9150 20 29 3b 0a 20 20 20 20 70 6e 65 78 74 20 3d 20   );.    pnext = 
c9160 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70  get2byte(&data[p
c9170 62 65 67 69 6e 5d 29 3b 0a 20 20 20 20 70 73 69  begin]);.    psi
c9180 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28 26 64  ze = get2byte(&d
c9190 61 74 61 5b 70 62 65 67 69 6e 2b 32 5d 29 3b 0a  ata[pbegin+2]);.
c91a0 20 20 20 20 69 66 28 20 70 62 65 67 69 6e 20 2b      if( pbegin +
c91b0 20 70 73 69 7a 65 20 2b 20 33 20 3e 3d 20 70 6e   psize + 3 >= pn
c91c0 65 78 74 20 26 26 20 70 6e 65 78 74 3e 30 20 29  ext && pnext>0 )
c91d0 7b 0a 20 20 20 20 20 20 69 6e 74 20 66 72 61 67  {.      int frag
c91e0 20 3d 20 70 6e 65 78 74 20 2d 20 28 70 62 65 67   = pnext - (pbeg
c91f0 69 6e 2b 70 73 69 7a 65 29 3b 0a 20 20 20 20 20  in+psize);.     
c9200 20 61 73 73 65 72 74 28 20 66 72 61 67 3c 3d 64   assert( frag<=d
c9210 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66  ata[pPage->hdrOf
c9220 66 73 65 74 2b 37 5d 20 29 3b 0a 20 20 20 20 20  fset+7] );.     
c9230 20 64 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72   data[pPage->hdr
c9240 4f 66 66 73 65 74 2b 37 5d 20 2d 3d 20 66 72 61  Offset+7] -= fra
c9250 67 3b 0a 20 20 20 20 20 20 70 75 74 32 62 79 74  g;.      put2byt
c9260 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e 5d 2c  e(&data[pbegin],
c9270 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
c9280 70 6e 65 78 74 5d 29 29 3b 0a 20 20 20 20 20 20  pnext]));.      
c9290 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 70  put2byte(&data[p
c92a0 62 65 67 69 6e 2b 32 5d 2c 20 70 6e 65 78 74 2b  begin+2], pnext+
c92b0 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70  get2byte(&data[p
c92c0 6e 65 78 74 2b 32 5d 29 2d 70 62 65 67 69 6e 29  next+2])-pbegin)
c92d0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
c92e0 20 20 20 61 64 64 72 20 3d 20 70 62 65 67 69 6e     addr = pbegin
c92f0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
c9300 2a 20 49 66 20 74 68 65 20 63 65 6c 6c 20 63 6f  * If the cell co
c9310 6e 74 65 6e 74 20 61 72 65 61 20 62 65 67 69 6e  ntent area begin
c9320 73 20 77 69 74 68 20 61 20 66 72 65 65 62 6c 6f  s with a freeblo
c9330 63 6b 2c 20 72 65 6d 6f 76 65 20 69 74 2e 20 2a  ck, remove it. *
c9340 2f 0a 20 20 69 66 28 20 64 61 74 61 5b 68 64 72  /.  if( data[hdr
c9350 2b 31 5d 3d 3d 64 61 74 61 5b 68 64 72 2b 35 5d  +1]==data[hdr+5]
c9360 20 26 26 20 64 61 74 61 5b 68 64 72 2b 32 5d 3d   && data[hdr+2]=
c9370 3d 64 61 74 61 5b 68 64 72 2b 36 5d 20 29 7b 0a  =data[hdr+6] ){.
c9380 20 20 20 20 69 6e 74 20 74 6f 70 3b 0a 20 20 20      int top;.   
c9390 20 70 62 65 67 69 6e 20 3d 20 67 65 74 32 62 79   pbegin = get2by
c93a0 74 65 28 26 64 61 74 61 5b 68 64 72 2b 31 5d 29  te(&data[hdr+1])
c93b0 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61  ;.    memcpy(&da
c93c0 74 61 5b 68 64 72 2b 31 5d 2c 20 26 64 61 74 61  ta[hdr+1], &data
c93d0 5b 70 62 65 67 69 6e 5d 2c 20 32 29 3b 0a 20 20  [pbegin], 2);.  
c93e0 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79 74 65    top = get2byte
c93f0 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a  (&data[hdr+5]);.
c9400 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61      put2byte(&da
c9410 74 61 5b 68 64 72 2b 35 5d 2c 20 74 6f 70 20 2b  ta[hdr+5], top +
c9420 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
c9430 70 62 65 67 69 6e 2b 32 5d 29 29 3b 0a 20 20 7d  pbegin+2]));.  }
c9440 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6f 64 65  .}../*.** Decode
c9450 20 74 68 65 20 66 6c 61 67 73 20 62 79 74 65 20   the flags byte 
c9460 28 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20  (the first byte 
c9470 6f 66 20 74 68 65 20 68 65 61 64 65 72 29 20 66  of the header) f
c9480 6f 72 20 61 20 70 61 67 65 0a 2a 2a 20 61 6e 64  or a page.** and
c9490 20 69 6e 69 74 69 61 6c 69 7a 65 20 66 69 65 6c   initialize fiel
c94a0 64 73 20 6f 66 20 74 68 65 20 4d 65 6d 50 61 67  ds of the MemPag
c94b0 65 20 73 74 72 75 63 74 75 72 65 20 61 63 63 6f  e structure acco
c94c0 72 64 69 6e 67 6c 79 2e 0a 2a 2f 0a 73 74 61 74  rdingly..*/.stat
c94d0 69 63 20 76 6f 69 64 20 64 65 63 6f 64 65 46 6c  ic void decodeFl
c94e0 61 67 73 28 4d 65 6d 50 61 67 65 20 2a 70 50 61  ags(MemPage *pPa
c94f0 67 65 2c 20 69 6e 74 20 66 6c 61 67 42 79 74 65  ge, int flagByte
c9500 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  ){.  BtShared *p
c9510 42 74 3b 20 20 20 20 20 2f 2a 20 41 20 63 6f 70  Bt;     /* A cop
c9520 79 20 6f 66 20 70 50 61 67 65 2d 3e 70 42 74 20  y of pPage->pBt 
c9530 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50  */..  assert( pP
c9540 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3d 3d  age->hdrOffset==
c9550 28 70 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d 31 20  (pPage->pgno==1 
c9560 3f 20 31 30 30 20 3a 20 30 29 20 29 3b 0a 20 20  ? 100 : 0) );.  
c9570 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 3d 20  pPage->intKey = 
c9580 28 66 6c 61 67 42 79 74 65 20 26 20 28 50 54 46  (flagByte & (PTF
c9590 5f 49 4e 54 4b 45 59 7c 50 54 46 5f 4c 45 41 46  _INTKEY|PTF_LEAF
c95a0 44 41 54 41 29 29 21 3d 30 3b 0a 20 20 70 50 61  DATA))!=0;.  pPa
c95b0 67 65 2d 3e 7a 65 72 6f 44 61 74 61 20 3d 20 28  ge->zeroData = (
c95c0 66 6c 61 67 42 79 74 65 20 26 20 50 54 46 5f 5a  flagByte & PTF_Z
c95d0 45 52 4f 44 41 54 41 29 21 3d 30 3b 0a 20 20 70  ERODATA)!=0;.  p
c95e0 50 61 67 65 2d 3e 6c 65 61 66 20 3d 20 28 66 6c  Page->leaf = (fl
c95f0 61 67 42 79 74 65 20 26 20 50 54 46 5f 4c 45 41  agByte & PTF_LEA
c9600 46 29 21 3d 30 3b 0a 20 20 70 50 61 67 65 2d 3e  F)!=0;.  pPage->
c9610 63 68 69 6c 64 50 74 72 53 69 7a 65 20 3d 20 34  childPtrSize = 4
c9620 2a 28 70 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 30  *(pPage->leaf==0
c9630 29 3b 0a 20 20 70 42 74 20 3d 20 70 50 61 67 65  );.  pBt = pPage
c9640 2d 3e 70 42 74 3b 0a 20 20 69 66 28 20 66 6c 61  ->pBt;.  if( fla
c9650 67 42 79 74 65 20 26 20 50 54 46 5f 4c 45 41 46  gByte & PTF_LEAF
c9660 44 41 54 41 20 29 7b 0a 20 20 20 20 70 50 61 67  DATA ){.    pPag
c9670 65 2d 3e 6c 65 61 66 44 61 74 61 20 3d 20 31 3b  e->leafData = 1;
c9680 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 61 78 4c  .    pPage->maxL
c9690 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 78 4c  ocal = pBt->maxL
c96a0 65 61 66 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e  eaf;.    pPage->
c96b0 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e  minLocal = pBt->
c96c0 6d 69 6e 4c 65 61 66 3b 0a 20 20 7d 65 6c 73 65  minLeaf;.  }else
c96d0 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6c 65 61  {.    pPage->lea
c96e0 66 44 61 74 61 20 3d 20 30 3b 0a 20 20 20 20 70  fData = 0;.    p
c96f0 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d  Page->maxLocal =
c9700 20 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c 3b 0a   pBt->maxLocal;.
c9710 20 20 20 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f      pPage->minLo
c9720 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 69 6e 4c 6f  cal = pBt->minLo
c9730 63 61 6c 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65  cal;.  }.  pPage
c9740 2d 3e 68 61 73 44 61 74 61 20 3d 20 21 28 70 50  ->hasData = !(pP
c9750 61 67 65 2d 3e 7a 65 72 6f 44 61 74 61 20 7c 7c  age->zeroData ||
c9760 20 28 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 26   (!pPage->leaf &
c9770 26 20 70 50 61 67 65 2d 3e 6c 65 61 66 44 61 74  & pPage->leafDat
c9780 61 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  a));.}../*.** In
c9790 69 74 69 61 6c 69 7a 65 20 74 68 65 20 61 75 78  itialize the aux
c97a0 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d 61 74 69  iliary informati
c97b0 6f 6e 20 66 6f 72 20 61 20 64 69 73 6b 20 62 6c  on for a disk bl
c97c0 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70  ock..**.** The p
c97d0 50 61 72 65 6e 74 20 70 61 72 61 6d 65 74 65 72  Parent parameter
c97e0 20 6d 75 73 74 20 62 65 20 61 20 70 6f 69 6e 74   must be a point
c97f0 65 72 20 74 6f 20 74 68 65 20 4d 65 6d 50 61 67  er to the MemPag
c9800 65 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 74 68  e which.** is th
c9810 65 20 70 61 72 65 6e 74 20 6f 66 20 74 68 65 20  e parent of the 
c9820 70 61 67 65 20 62 65 69 6e 67 20 69 6e 69 74 69  page being initi
c9830 61 6c 69 7a 65 64 2e 20 20 54 68 65 20 72 6f 6f  alized.  The roo
c9840 74 20 6f 66 20 61 0a 2a 2a 20 42 54 72 65 65 20  t of a.** BTree 
c9850 68 61 73 20 6e 6f 20 70 61 72 65 6e 74 20 61 6e  has no parent an
c9860 64 20 73 6f 20 66 6f 72 20 74 68 61 74 20 70 61  d so for that pa
c9870 67 65 2c 20 70 50 61 72 65 6e 74 3d 3d 4e 55 4c  ge, pParent==NUL
c9880 4c 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  L..**.** Return 
c9890 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63  SQLITE_OK on suc
c98a0 63 65 73 73 2e 20 20 49 66 20 77 65 20 73 65 65  cess.  If we see
c98b0 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 64   that the page d
c98c0 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61  oes.** not conta
c98d0 69 6e 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 64  in a well-formed
c98e0 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2c 20   database page, 
c98f0 74 68 65 6e 20 72 65 74 75 72 6e 20 0a 2a 2a 20  then return .** 
c9900 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2e 20  SQLITE_CORRUPT. 
c9910 20 4e 6f 74 65 20 74 68 61 74 20 61 20 72 65 74   Note that a ret
c9920 75 72 6e 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4b  urn of SQLITE_OK
c9930 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 67 75 61   does not.** gua
c9940 72 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 20  rantee that the 
c9950 70 61 67 65 20 69 73 20 77 65 6c 6c 2d 66 6f 72  page is well-for
c9960 6d 65 64 2e 20 20 49 74 20 6f 6e 6c 79 20 73 68  med.  It only sh
c9970 6f 77 73 20 74 68 61 74 0a 2a 2a 20 77 65 20 66  ows that.** we f
c9980 61 69 6c 65 64 20 74 6f 20 64 65 74 65 63 74 20  ailed to detect 
c9990 61 6e 79 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a  any corruption..
c99a0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
c99b0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
c99c0 65 65 49 6e 69 74 50 61 67 65 28 0a 20 20 4d 65  eeInitPage(.  Me
c99d0 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20  mPage *pPage,   
c99e0 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65       /* The page
c99f0 20 74 6f 20 62 65 20 69 6e 69 74 69 61 6c 69 7a   to be initializ
c9a00 65 64 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20  ed */.  MemPage 
c9a10 2a 70 50 61 72 65 6e 74 20 20 20 20 20 20 20 2f  *pParent       /
c9a20 2a 20 54 68 65 20 70 61 72 65 6e 74 2e 20 20 4d  * The parent.  M
c9a30 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a  ight be NULL */.
c9a40 29 7b 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 20  ){.  int pc;    
c9a50 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
c9a60 73 73 20 6f 66 20 61 20 66 72 65 65 62 6c 6f 63  ss of a freebloc
c9a70 6b 20 77 69 74 68 69 6e 20 70 50 61 67 65 2d 3e  k within pPage->
c9a80 61 44 61 74 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74  aData[] */.  int
c9a90 20 68 64 72 3b 20 20 20 20 20 20 20 20 20 20 20   hdr;           
c9aa0 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 62 65 67  /* Offset to beg
c9ab0 69 6e 6e 69 6e 67 20 6f 66 20 70 61 67 65 20 68  inning of page h
c9ac0 65 61 64 65 72 20 2a 2f 0a 20 20 75 38 20 2a 64  eader */.  u8 *d
c9ad0 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ata;          /*
c9ae0 20 45 71 75 61 6c 20 74 6f 20 70 50 61 67 65 2d   Equal to pPage-
c9af0 3e 61 44 61 74 61 20 2a 2f 0a 20 20 42 74 53 68  >aData */.  BtSh
c9b00 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20  ared *pBt;      
c9b10 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 62 74    /* The main bt
c9b20 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f  ree structure */
c9b30 0a 20 20 69 6e 74 20 75 73 61 62 6c 65 53 69 7a  .  int usableSiz
c9b40 65 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20  e;    /* Amount 
c9b50 6f 66 20 75 73 61 62 6c 65 20 73 70 61 63 65 20  of usable space 
c9b60 6f 6e 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a  on each page */.
c9b70 20 20 69 6e 74 20 63 65 6c 6c 4f 66 66 73 65 74    int cellOffset
c9b80 3b 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 66  ;    /* Offset f
c9b90 72 6f 6d 20 73 74 61 72 74 20 6f 66 20 70 61 67  rom start of pag
c9ba0 65 20 74 6f 20 66 69 72 73 74 20 63 65 6c 6c 20  e to first cell 
c9bb0 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74  pointer */.  int
c9bc0 20 6e 46 72 65 65 3b 20 20 20 20 20 20 20 20 20   nFree;         
c9bd0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 6e 75  /* Number of unu
c9be0 73 65 64 20 62 79 74 65 73 20 6f 6e 20 74 68 65  sed bytes on the
c9bf0 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 74   page */.  int t
c9c00 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  op;           /*
c9c10 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 20 74   First byte of t
c9c20 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20  he cell content 
c9c30 61 72 65 61 20 2a 2f 0a 0a 20 20 70 42 74 20 3d  area */..  pBt =
c9c40 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 61   pPage->pBt;.  a
c9c50 73 73 65 72 74 28 20 70 42 74 21 3d 30 20 29 3b  ssert( pBt!=0 );
c9c60 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 65  .  assert( pPare
c9c70 6e 74 3d 3d 30 20 7c 7c 20 70 50 61 72 65 6e 74  nt==0 || pParent
c9c80 2d 3e 70 42 74 3d 3d 70 42 74 20 29 3b 0a 20 20  ->pBt==pBt );.  
c9c90 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70  assert( pPage->p
c9ca0 67 6e 6f 3d 3d 73 71 6c 69 74 65 33 50 61 67 65  gno==sqlite3Page
c9cb0 72 50 61 67 65 6e 75 6d 62 65 72 28 70 50 61 67  rPagenumber(pPag
c9cc0 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20  e->pDbPage) );. 
c9cd0 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
c9ce0 61 44 61 74 61 20 3d 3d 20 26 28 28 75 6e 73 69  aData == &((unsi
c9cf0 67 6e 65 64 20 63 68 61 72 2a 29 70 50 61 67 65  gned char*)pPage
c9d00 29 5b 2d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  )[-pBt->pageSize
c9d10 5d 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65  ] );.  if( pPage
c9d20 2d 3e 70 50 61 72 65 6e 74 21 3d 70 50 61 72 65  ->pParent!=pPare
c9d30 6e 74 20 26 26 20 28 70 50 61 67 65 2d 3e 70 50  nt && (pPage->pP
c9d40 61 72 65 6e 74 21 3d 30 20 7c 7c 20 70 50 61 67  arent!=0 || pPag
c9d50 65 2d 3e 69 73 49 6e 69 74 29 20 29 7b 0a 20 20  e->isInit) ){.  
c9d60 20 20 2f 2a 20 54 68 65 20 70 61 72 65 6e 74 20    /* The parent 
c9d70 70 61 67 65 20 73 68 6f 75 6c 64 20 6e 65 76 65  page should neve
c9d80 72 20 63 68 61 6e 67 65 20 75 6e 6c 65 73 73 20  r change unless 
c9d90 74 68 65 20 66 69 6c 65 20 69 73 20 63 6f 72 72  the file is corr
c9da0 75 70 74 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  upt */.    retur
c9db0 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
c9dc0 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 69 66 28  _BKPT;.  }.  if(
c9dd0 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29   pPage->isInit )
c9de0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
c9df0 4b 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e  K;.  if( pPage->
c9e00 70 50 61 72 65 6e 74 3d 3d 30 20 26 26 20 70 50  pParent==0 && pP
c9e10 61 72 65 6e 74 21 3d 30 20 29 7b 0a 20 20 20 20  arent!=0 ){.    
c9e20 70 50 61 67 65 2d 3e 70 50 61 72 65 6e 74 20 3d  pPage->pParent =
c9e30 20 70 50 61 72 65 6e 74 3b 0a 20 20 20 20 73 71   pParent;.    sq
c9e40 6c 69 74 65 33 50 61 67 65 72 52 65 66 28 70 50  lite3PagerRef(pP
c9e50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29 3b  arent->pDbPage);
c9e60 0a 20 20 7d 0a 20 20 68 64 72 20 3d 20 70 50 61  .  }.  hdr = pPa
c9e70 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20  ge->hdrOffset;. 
c9e80 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61   data = pPage->a
c9e90 44 61 74 61 3b 0a 20 20 64 65 63 6f 64 65 46 6c  Data;.  decodeFl
c9ea0 61 67 73 28 70 50 61 67 65 2c 20 64 61 74 61 5b  ags(pPage, data[
c9eb0 68 64 72 5d 29 3b 0a 20 20 70 50 61 67 65 2d 3e  hdr]);.  pPage->
c9ec0 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20  nOverflow = 0;. 
c9ed0 20 70 50 61 67 65 2d 3e 69 64 78 53 68 69 66 74   pPage->idxShift
c9ee0 20 3d 20 30 3b 0a 20 20 75 73 61 62 6c 65 53 69   = 0;.  usableSi
c9ef0 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65  ze = pBt->usable
c9f00 53 69 7a 65 3b 0a 20 20 70 50 61 67 65 2d 3e 63  Size;.  pPage->c
c9f10 65 6c 6c 4f 66 66 73 65 74 20 3d 20 63 65 6c 6c  ellOffset = cell
c9f20 4f 66 66 73 65 74 20 3d 20 68 64 72 20 2b 20 31  Offset = hdr + 1
c9f30 32 20 2d 20 34 2a 70 50 61 67 65 2d 3e 6c 65 61  2 - 4*pPage->lea
c9f40 66 3b 0a 20 20 74 6f 70 20 3d 20 67 65 74 32 62  f;.  top = get2b
c9f50 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d  yte(&data[hdr+5]
c9f60 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c  );.  pPage->nCel
c9f70 6c 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61  l = get2byte(&da
c9f80 74 61 5b 68 64 72 2b 33 5d 29 3b 0a 20 20 69 66  ta[hdr+3]);.  if
c9f90 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 4d  ( pPage->nCell>M
c9fa0 58 5f 43 45 4c 4c 28 70 42 74 29 20 29 7b 0a 20  X_CELL(pBt) ){. 
c9fb0 20 20 20 2f 2a 20 54 6f 20 6d 61 6e 79 20 63 65     /* To many ce
c9fc0 6c 6c 73 20 66 6f 72 20 61 20 73 69 6e 67 6c 65  lls for a single
c9fd0 20 70 61 67 65 2e 20 20 54 68 65 20 70 61 67 65   page.  The page
c9fe0 20 6d 75 73 74 20 62 65 20 63 6f 72 72 75 70 74   must be corrupt
c9ff0 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53   */.    return S
ca000 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
ca010 50 54 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50  PT;.  }.  if( pP
ca020 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 26 26  age->nCell==0 &&
ca030 20 70 50 61 72 65 6e 74 21 3d 30 20 26 26 20 70   pParent!=0 && p
ca040 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 21 3d 31 20  Parent->pgno!=1 
ca050 29 7b 0a 20 20 20 20 2f 2a 20 41 6c 6c 20 70 61  ){.    /* All pa
ca060 67 65 73 20 6d 75 73 74 20 68 61 76 65 20 61 74  ges must have at
ca070 20 6c 65 61 73 74 20 6f 6e 65 20 63 65 6c 6c 2c   least one cell,
ca080 20 65 78 63 65 70 74 20 66 6f 72 20 72 6f 6f 74   except for root
ca090 20 70 61 67 65 73 20 2a 2f 0a 20 20 20 20 72 65   pages */.    re
ca0a0 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
ca0b0 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20  UPT_BKPT;.  }.. 
ca0c0 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20   /* Compute the 
ca0d0 74 6f 74 61 6c 20 66 72 65 65 20 73 70 61 63 65  total free space
ca0e0 20 6f 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0a   on the page */.
ca0f0 20 20 70 63 20 3d 20 67 65 74 32 62 79 74 65 28    pc = get2byte(
ca100 26 64 61 74 61 5b 68 64 72 2b 31 5d 29 3b 0a 20  &data[hdr+1]);. 
ca110 20 6e 46 72 65 65 20 3d 20 64 61 74 61 5b 68 64   nFree = data[hd
ca120 72 2b 37 5d 20 2b 20 74 6f 70 20 2d 20 28 63 65  r+7] + top - (ce
ca130 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70 50 61  llOffset + 2*pPa
ca140 67 65 2d 3e 6e 43 65 6c 6c 29 3b 0a 20 20 77 68  ge->nCell);.  wh
ca150 69 6c 65 28 20 70 63 3e 30 20 29 7b 0a 20 20 20  ile( pc>0 ){.   
ca160 20 69 6e 74 20 6e 65 78 74 2c 20 73 69 7a 65 3b   int next, size;
ca170 0a 20 20 20 20 69 66 28 20 70 63 3e 75 73 61 62  .    if( pc>usab
ca180 6c 65 53 69 7a 65 2d 34 20 29 7b 0a 20 20 20 20  leSize-4 ){.    
ca190 20 20 2f 2a 20 46 72 65 65 20 62 6c 6f 63 6b 20    /* Free block 
ca1a0 69 73 20 6f 66 66 20 74 68 65 20 70 61 67 65 20  is off the page 
ca1b0 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  */.      return 
ca1c0 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
ca1d0 4b 50 54 3b 20 0a 20 20 20 20 7d 0a 20 20 20 20  KPT; .    }.    
ca1e0 6e 65 78 74 20 3d 20 67 65 74 32 62 79 74 65 28  next = get2byte(
ca1f0 26 64 61 74 61 5b 70 63 5d 29 3b 0a 20 20 20 20  &data[pc]);.    
ca200 73 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28  size = get2byte(
ca210 26 64 61 74 61 5b 70 63 2b 32 5d 29 3b 0a 20 20  &data[pc+2]);.  
ca220 20 20 69 66 28 20 6e 65 78 74 3e 30 20 26 26 20    if( next>0 && 
ca230 6e 65 78 74 3c 3d 70 63 2b 73 69 7a 65 2b 33 20  next<=pc+size+3 
ca240 29 7b 0a 20 20 20 20 20 20 2f 2a 20 46 72 65 65  ){.      /* Free
ca250 20 62 6c 6f 63 6b 73 20 6d 75 73 74 20 62 65 20   blocks must be 
ca260 69 6e 20 61 63 63 65 6e 64 69 6e 67 20 6f 72 64  in accending ord
ca270 65 72 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75  er */.      retu
ca280 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
ca290 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20 7d 0a 20  T_BKPT; .    }. 
ca2a0 20 20 20 6e 46 72 65 65 20 2b 3d 20 73 69 7a 65     nFree += size
ca2b0 3b 0a 20 20 20 20 70 63 20 3d 20 6e 65 78 74 3b  ;.    pc = next;
ca2c0 0a 20 20 7d 0a 20 20 70 50 61 67 65 2d 3e 6e 46  .  }.  pPage->nF
ca2d0 72 65 65 20 3d 20 6e 46 72 65 65 3b 0a 20 20 69  ree = nFree;.  i
ca2e0 66 28 20 6e 46 72 65 65 3e 3d 75 73 61 62 6c 65  f( nFree>=usable
ca2f0 53 69 7a 65 20 29 7b 0a 20 20 20 20 2f 2a 20 46  Size ){.    /* F
ca300 72 65 65 20 73 70 61 63 65 20 63 61 6e 6e 6f 74  ree space cannot
ca310 20 65 78 63 65 65 64 20 74 6f 74 61 6c 20 70 61   exceed total pa
ca320 67 65 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 72  ge size */.    r
ca330 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
ca340 52 55 50 54 5f 42 4b 50 54 3b 20 0a 20 20 7d 0a  RUPT_BKPT; .  }.
ca350 0a 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74  .  pPage->isInit
ca360 20 3d 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 53   = 1;.  return S
ca370 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
ca380 2a 2a 20 53 65 74 20 75 70 20 61 20 72 61 77 20  ** Set up a raw 
ca390 70 61 67 65 20 73 6f 20 74 68 61 74 20 69 74 20  page so that it 
ca3a0 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 64 61 74  looks like a dat
ca3b0 61 62 61 73 65 20 70 61 67 65 20 68 6f 6c 64 69  abase page holdi
ca3c0 6e 67 0a 2a 2a 20 6e 6f 20 65 6e 74 72 69 65 73  ng.** no entries
ca3d0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
ca3e0 20 7a 65 72 6f 50 61 67 65 28 4d 65 6d 50 61 67   zeroPage(MemPag
ca3f0 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 66 6c  e *pPage, int fl
ca400 61 67 73 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64  ags){.  unsigned
ca410 20 63 68 61 72 20 2a 64 61 74 61 20 3d 20 70 50   char *data = pP
ca420 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 42 74  age->aData;.  Bt
ca430 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50  Shared *pBt = pP
ca440 61 67 65 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 20  age->pBt;.  int 
ca450 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72  hdr = pPage->hdr
ca460 4f 66 66 73 65 74 3b 0a 20 20 69 6e 74 20 66 69  Offset;.  int fi
ca470 72 73 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  rst;..  assert( 
ca480 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
ca490 6e 75 6d 62 65 72 28 70 50 61 67 65 2d 3e 70 44  number(pPage->pD
ca4a0 62 50 61 67 65 29 3d 3d 70 50 61 67 65 2d 3e 70  bPage)==pPage->p
ca4b0 67 6e 6f 20 29 3b 0a 20 20 61 73 73 65 72 74 28  gno );.  assert(
ca4c0 20 26 64 61 74 61 5b 70 42 74 2d 3e 70 61 67 65   &data[pBt->page
ca4d0 53 69 7a 65 5d 20 3d 3d 20 28 75 6e 73 69 67 6e  Size] == (unsign
ca4e0 65 64 20 63 68 61 72 2a 29 70 50 61 67 65 20 29  ed char*)pPage )
ca4f0 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
ca500 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61  te3PagerIswritea
ca510 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ble(pPage->pDbPa
ca520 67 65 29 20 29 3b 0a 20 20 6d 65 6d 73 65 74 28  ge) );.  memset(
ca530 26 64 61 74 61 5b 68 64 72 5d 2c 20 30 2c 20 70  &data[hdr], 0, p
ca540 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d  Bt->usableSize -
ca550 20 68 64 72 29 3b 0a 20 20 64 61 74 61 5b 68 64   hdr);.  data[hd
ca560 72 5d 20 3d 20 66 6c 61 67 73 3b 0a 20 20 66 69  r] = flags;.  fi
ca570 72 73 74 20 3d 20 68 64 72 20 2b 20 38 20 2b 20  rst = hdr + 8 + 
ca580 34 2a 28 28 66 6c 61 67 73 26 50 54 46 5f 4c 45  4*((flags&PTF_LE
ca590 41 46 29 3d 3d 30 29 3b 0a 20 20 6d 65 6d 73 65  AF)==0);.  memse
ca5a0 74 28 26 64 61 74 61 5b 68 64 72 2b 31 5d 2c 20  t(&data[hdr+1], 
ca5b0 30 2c 20 34 29 3b 0a 20 20 64 61 74 61 5b 68 64  0, 4);.  data[hd
ca5c0 72 2b 37 5d 20 3d 20 30 3b 0a 20 20 70 75 74 32  r+7] = 0;.  put2
ca5d0 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35  byte(&data[hdr+5
ca5e0 5d 2c 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  ], pBt->usableSi
ca5f0 7a 65 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46  ze);.  pPage->nF
ca600 72 65 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c  ree = pBt->usabl
ca610 65 53 69 7a 65 20 2d 20 66 69 72 73 74 3b 0a 20  eSize - first;. 
ca620 20 64 65 63 6f 64 65 46 6c 61 67 73 28 70 50 61   decodeFlags(pPa
ca630 67 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 50  ge, flags);.  pP
ca640 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 20 3d  age->hdrOffset =
ca650 20 68 64 72 3b 0a 20 20 70 50 61 67 65 2d 3e 63   hdr;.  pPage->c
ca660 65 6c 6c 4f 66 66 73 65 74 20 3d 20 66 69 72 73  ellOffset = firs
ca670 74 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65  t;.  pPage->nOve
ca680 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 70 50 61  rflow = 0;.  pPa
ca690 67 65 2d 3e 69 64 78 53 68 69 66 74 20 3d 20 30  ge->idxShift = 0
ca6a0 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  ;.  pPage->nCell
ca6b0 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 2d 3e 69   = 0;.  pPage->i
ca6c0 73 49 6e 69 74 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a  sInit = 1;.}../*
ca6d0 0a 2a 2a 20 47 65 74 20 61 20 70 61 67 65 20 66  .** Get a page f
ca6e0 72 6f 6d 20 74 68 65 20 70 61 67 65 72 2e 20 20  rom the pager.  
ca6f0 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 4d  Initialize the M
ca700 65 6d 50 61 67 65 2e 70 42 74 20 61 6e 64 0a 2a  emPage.pBt and.*
ca710 2a 20 4d 65 6d 50 61 67 65 2e 61 44 61 74 61 20  * MemPage.aData 
ca720 65 6c 65 6d 65 6e 74 73 20 69 66 20 6e 65 65 64  elements if need
ca730 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ed..**.** If the
ca740 20 6e 6f 43 6f 6e 74 65 6e 74 20 66 6c 61 67 20   noContent flag 
ca750 69 73 20 73 65 74 2c 20 69 74 20 6d 65 61 6e 73  is set, it means
ca760 20 74 68 61 74 20 77 65 20 64 6f 20 6e 6f 74 20   that we do not 
ca770 63 61 72 65 20 61 62 6f 75 74 0a 2a 2a 20 74 68  care about.** th
ca780 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65  e content of the
ca790 20 70 61 67 65 20 61 74 20 74 68 69 73 20 74 69   page at this ti
ca7a0 6d 65 2e 20 20 53 6f 20 64 6f 20 6e 6f 74 20 67  me.  So do not g
ca7b0 6f 20 74 6f 20 74 68 65 20 64 69 73 6b 0a 2a 2a  o to the disk.**
ca7c0 20 74 6f 20 66 65 74 63 68 20 74 68 65 20 63 6f   to fetch the co
ca7d0 6e 74 65 6e 74 2e 20 20 4a 75 73 74 20 66 69 6c  ntent.  Just fil
ca7e0 6c 20 69 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74  l in the content
ca7f0 20 77 69 74 68 20 7a 65 72 6f 73 20 66 6f 72 20   with zeros for 
ca800 6e 6f 77 2e 0a 2a 2a 20 49 66 20 69 6e 20 74 68  now..** If in th
ca810 65 20 66 75 74 75 72 65 20 77 65 20 63 61 6c 6c  e future we call
ca820 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
ca830 74 65 28 29 20 6f 6e 20 74 68 69 73 20 70 61 67  te() on this pag
ca840 65 2c 20 74 68 61 74 0a 2a 2a 20 6d 65 61 6e 73  e, that.** means
ca850 20 77 65 20 68 61 76 65 20 73 74 61 72 74 65 64   we have started
ca860 20 74 6f 20 62 65 20 63 6f 6e 63 65 72 6e 65 64   to be concerned
ca870 20 61 62 6f 75 74 20 63 6f 6e 74 65 6e 74 20 61   about content a
ca880 6e 64 20 74 68 65 20 64 69 73 6b 0a 2a 2a 20 72  nd the disk.** r
ca890 65 61 64 20 73 68 6f 75 6c 64 20 6f 63 63 75 72  ead should occur
ca8a0 20 61 74 20 74 68 61 74 20 70 6f 69 6e 74 2e 0a   at that point..
ca8b0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
ca8c0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
ca8d0 65 65 47 65 74 50 61 67 65 28 0a 20 20 42 74 53  eeGetPage(.  BtS
ca8e0 68 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20  hared *pBt,     
ca8f0 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20 2a    /* The btree *
ca900 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20  /.  Pgno pgno,  
ca910 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
ca920 65 72 20 6f 66 20 74 68 65 20 70 61 67 65 20 74  er of the page t
ca930 6f 20 66 65 74 63 68 20 2a 2f 0a 20 20 4d 65 6d  o fetch */.  Mem
ca940 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 20  Page **ppPage,  
ca950 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20    /* Return the 
ca960 70 61 67 65 20 69 6e 20 74 68 69 73 20 70 61 72  page in this par
ca970 61 6d 65 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  ameter */.  int 
ca980 6e 6f 43 6f 6e 74 65 6e 74 20 20 20 20 20 20 20  noContent       
ca990 20 2f 2a 20 44 6f 20 6e 6f 74 20 6c 6f 61 64 20   /* Do not load 
ca9a0 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 69 66 20  page content if 
ca9b0 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  true */.){.  int
ca9c0 20 72 63 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a   rc;.  MemPage *
ca9d0 70 50 61 67 65 3b 0a 20 20 44 62 50 61 67 65 20  pPage;.  DbPage 
ca9e0 2a 70 44 62 50 61 67 65 3b 0a 0a 20 20 72 63 20  *pDbPage;..  rc 
ca9f0 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 63  = sqlite3PagerAc
caa00 71 75 69 72 65 28 70 42 74 2d 3e 70 50 61 67 65  quire(pBt->pPage
caa10 72 2c 20 70 67 6e 6f 2c 20 28 44 62 50 61 67 65  r, pgno, (DbPage
caa20 2a 2a 29 26 70 44 62 50 61 67 65 2c 20 6e 6f 43  **)&pDbPage, noC
caa30 6f 6e 74 65 6e 74 29 3b 0a 20 20 69 66 28 20 72  ontent);.  if( r
caa40 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  c ) return rc;. 
caa50 20 70 50 61 67 65 20 3d 20 28 4d 65 6d 50 61 67   pPage = (MemPag
caa60 65 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72  e *)sqlite3Pager
caa70 47 65 74 45 78 74 72 61 28 70 44 62 50 61 67 65  GetExtra(pDbPage
caa80 29 3b 0a 20 20 70 50 61 67 65 2d 3e 61 44 61 74  );.  pPage->aDat
caa90 61 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  a = sqlite3Pager
caaa0 47 65 74 44 61 74 61 28 70 44 62 50 61 67 65 29  GetData(pDbPage)
caab0 3b 0a 20 20 70 50 61 67 65 2d 3e 70 44 62 50 61  ;.  pPage->pDbPa
caac0 67 65 20 3d 20 70 44 62 50 61 67 65 3b 0a 20 20  ge = pDbPage;.  
caad0 70 50 61 67 65 2d 3e 70 42 74 20 3d 20 70 42 74  pPage->pBt = pBt
caae0 3b 0a 20 20 70 50 61 67 65 2d 3e 70 67 6e 6f 20  ;.  pPage->pgno 
caaf0 3d 20 70 67 6e 6f 3b 0a 20 20 70 50 61 67 65 2d  = pgno;.  pPage-
cab00 3e 68 64 72 4f 66 66 73 65 74 20 3d 20 70 50 61  >hdrOffset = pPa
cab10 67 65 2d 3e 70 67 6e 6f 3d 3d 31 20 3f 20 31 30  ge->pgno==1 ? 10
cab20 30 20 3a 20 30 3b 0a 20 20 2a 70 70 50 61 67 65  0 : 0;.  *ppPage
cab30 20 3d 20 70 50 61 67 65 3b 0a 20 20 72 65 74 75   = pPage;.  retu
cab40 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
cab50 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 70 61 67  ./*.** Get a pag
cab60 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72  e from the pager
cab70 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 20   and initialize 
cab80 69 74 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  it.  This routin
cab90 65 0a 2a 2a 20 69 73 20 6a 75 73 74 20 61 20 63  e.** is just a c
caba0 6f 6e 76 65 6e 69 65 6e 63 65 20 77 72 61 70 70  onvenience wrapp
cabb0 65 72 20 61 72 6f 75 6e 64 20 73 65 70 61 72 61  er around separa
cabc0 74 65 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73  te calls to.** s
cabd0 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61  qlite3BtreeGetPa
cabe0 67 65 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  ge() and sqlite3
cabf0 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 2e  BtreeInitPage().
cac00 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67  .*/.static int g
cac10 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 0a 20  etAndInitPage(. 
cac20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20   BtShared *pBt, 
cac30 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
cac40 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f  database file */
cac50 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20  .  Pgno pgno,   
cac60 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
cac70 72 20 6f 66 20 74 68 65 20 70 61 67 65 20 74 6f  r of the page to
cac80 20 67 65 74 20 2a 2f 0a 20 20 4d 65 6d 50 61 67   get */.  MemPag
cac90 65 20 2a 2a 70 70 50 61 67 65 2c 20 20 20 20 2f  e **ppPage,    /
caca0 2a 20 57 72 69 74 65 20 74 68 65 20 70 61 67 65  * Write the page
cacb0 20 70 6f 69 6e 74 65 72 20 68 65 72 65 20 2a 2f   pointer here */
cacc0 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 72  .  MemPage *pPar
cacd0 65 6e 74 20 20 20 20 20 2f 2a 20 50 61 72 65 6e  ent     /* Paren
cace0 74 20 6f 66 20 74 68 65 20 70 61 67 65 20 2a 2f  t of the page */
cacf0 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
cad00 69 66 28 20 70 67 6e 6f 3d 3d 30 20 29 7b 0a 20  if( pgno==0 ){. 
cad10 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
cad20 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 20 0a  _CORRUPT_BKPT; .
cad30 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74    }.  rc = sqlit
cad40 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70  e3BtreeGetPage(p
cad50 42 74 2c 20 70 67 6e 6f 2c 20 70 70 50 61 67 65  Bt, pgno, ppPage
cad60 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  , 0);.  if( rc==
cad70 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 28 2a 70  SQLITE_OK && (*p
cad80 70 50 61 67 65 29 2d 3e 69 73 49 6e 69 74 3d 3d  pPage)->isInit==
cad90 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  0 ){.    rc = sq
cada0 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61  lite3BtreeInitPa
cadb0 67 65 28 2a 70 70 50 61 67 65 2c 20 70 50 61 72  ge(*ppPage, pPar
cadc0 65 6e 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ent);.  }.  retu
cadd0 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
cade0 52 65 6c 65 61 73 65 20 61 20 4d 65 6d 50 61 67  Release a MemPag
cadf0 65 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64 20  e.  This should 
cae00 62 65 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 66  be called once f
cae10 6f 72 20 65 61 63 68 20 70 72 69 6f 72 0a 2a 2a  or each prior.**
cae20 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
cae30 42 74 72 65 65 47 65 74 50 61 67 65 2e 0a 2a 2f  BtreeGetPage..*/
cae40 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c  .static void rel
cae50 65 61 73 65 50 61 67 65 28 4d 65 6d 50 61 67 65  easePage(MemPage
cae60 20 2a 70 50 61 67 65 29 7b 0a 20 20 69 66 28 20   *pPage){.  if( 
cae70 70 50 61 67 65 20 29 7b 0a 20 20 20 20 61 73 73  pPage ){.    ass
cae80 65 72 74 28 20 70 50 61 67 65 2d 3e 61 44 61 74  ert( pPage->aDat
cae90 61 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  a );.    assert(
caea0 20 70 50 61 67 65 2d 3e 70 42 74 20 29 3b 0a 20   pPage->pBt );. 
caeb0 20 20 20 61 73 73 65 72 74 28 20 26 70 50 61 67     assert( &pPag
caec0 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e  e->aData[pPage->
caed0 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d 3d 3d  pBt->pageSize]==
caee0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29  (unsigned char*)
caef0 70 50 61 67 65 20 29 3b 0a 20 20 20 20 73 71 6c  pPage );.    sql
caf00 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
caf10 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a  Page->pDbPage);.
caf20 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69    }.}../*.** Thi
caf30 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
caf40 6c 65 64 20 77 68 65 6e 20 74 68 65 20 72 65 66  led when the ref
caf50 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 66 6f 72  erence count for
caf60 20 61 20 70 61 67 65 0a 2a 2a 20 72 65 61 63 68   a page.** reach
caf70 65 73 20 7a 65 72 6f 2e 20 20 57 65 20 6e 65 65  es zero.  We nee
caf80 64 20 74 6f 20 75 6e 72 65 66 20 74 68 65 20 70  d to unref the p
caf90 50 61 72 65 6e 74 20 70 6f 69 6e 74 65 72 20 77  Parent pointer w
cafa0 68 65 6e 20 74 68 61 74 0a 2a 2a 20 68 61 70 70  hen that.** happ
cafb0 65 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ens..*/.static v
cafc0 6f 69 64 20 70 61 67 65 44 65 73 74 72 75 63 74  oid pageDestruct
cafd0 6f 72 28 44 62 50 61 67 65 20 2a 70 44 61 74 61  or(DbPage *pData
cafe0 2c 20 69 6e 74 20 70 61 67 65 53 69 7a 65 29 7b  , int pageSize){
caff0 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  .  MemPage *pPag
cb000 65 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 61  e;.  assert( (pa
cb010 67 65 53 69 7a 65 20 26 20 37 29 3d 3d 30 20 29  geSize & 7)==0 )
cb020 3b 0a 20 20 70 50 61 67 65 20 3d 20 28 4d 65 6d  ;.  pPage = (Mem
cb030 50 61 67 65 20 2a 29 73 71 6c 69 74 65 33 50 61  Page *)sqlite3Pa
cb040 67 65 72 47 65 74 45 78 74 72 61 28 70 44 61 74  gerGetExtra(pDat
cb050 61 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d  a);.  if( pPage-
cb060 3e 70 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20  >pParent ){.    
cb070 4d 65 6d 50 61 67 65 20 2a 70 50 61 72 65 6e 74  MemPage *pParent
cb080 20 3d 20 70 50 61 67 65 2d 3e 70 50 61 72 65 6e   = pPage->pParen
cb090 74 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 50  t;.    pPage->pP
cb0a0 61 72 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20 72  arent = 0;.    r
cb0b0 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 72 65  eleasePage(pPare
cb0c0 6e 74 29 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65  nt);.  }.  pPage
cb0d0 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 7d 0a  ->isInit = 0;.}.
cb0e0 0a 2f 2a 0a 2a 2a 20 44 75 72 69 6e 67 20 61 20  ./*.** During a 
cb0f0 72 6f 6c 6c 62 61 63 6b 2c 20 77 68 65 6e 20 74  rollback, when t
cb100 68 65 20 70 61 67 65 72 20 72 65 6c 6f 61 64 73  he pager reloads
cb110 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 74   information int
cb120 6f 20 74 68 65 20 63 61 63 68 65 0a 2a 2a 20 73  o the cache.** s
cb130 6f 20 74 68 61 74 20 74 68 65 20 63 61 63 68 65  o that the cache
cb140 20 69 73 20 72 65 73 74 6f 72 65 64 20 74 6f 20   is restored to 
cb150 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 74 61  its original sta
cb160 74 65 20 61 74 20 74 68 65 20 73 74 61 72 74 20  te at the start 
cb170 6f 66 0a 2a 2a 20 74 68 65 20 74 72 61 6e 73 61  of.** the transa
cb180 63 74 69 6f 6e 2c 20 66 6f 72 20 65 61 63 68 20  ction, for each 
cb190 70 61 67 65 20 72 65 73 74 6f 72 65 64 20 74 68  page restored th
cb1a0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
cb1b0 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  lled..**.** This
cb1c0 20 72 6f 75 74 69 6e 65 20 6e 65 65 64 73 20 74   routine needs t
cb1d0 6f 20 72 65 73 65 74 20 74 68 65 20 65 78 74 72  o reset the extr
cb1e0 61 20 64 61 74 61 20 73 65 63 74 69 6f 6e 20 61  a data section a
cb1f0 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
cb200 0a 2a 2a 20 70 61 67 65 20 74 6f 20 61 67 72 65  .** page to agre
cb210 65 20 77 69 74 68 20 74 68 65 20 72 65 73 74 6f  e with the resto
cb220 72 65 64 20 64 61 74 61 2e 0a 2a 2f 0a 73 74 61  red data..*/.sta
cb230 74 69 63 20 76 6f 69 64 20 70 61 67 65 52 65 69  tic void pageRei
cb240 6e 69 74 28 44 62 50 61 67 65 20 2a 70 44 61 74  nit(DbPage *pDat
cb250 61 2c 20 69 6e 74 20 70 61 67 65 53 69 7a 65 29  a, int pageSize)
cb260 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  {.  MemPage *pPa
cb270 67 65 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70  ge;.  assert( (p
cb280 61 67 65 53 69 7a 65 20 26 20 37 29 3d 3d 30 20  ageSize & 7)==0 
cb290 29 3b 0a 20 20 70 50 61 67 65 20 3d 20 28 4d 65  );.  pPage = (Me
cb2a0 6d 50 61 67 65 20 2a 29 73 71 6c 69 74 65 33 50  mPage *)sqlite3P
cb2b0 61 67 65 72 47 65 74 45 78 74 72 61 28 70 44 61  agerGetExtra(pDa
cb2c0 74 61 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65  ta);.  if( pPage
cb2d0 2d 3e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20  ->isInit ){.    
cb2e0 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20  pPage->isInit = 
cb2f0 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  0;.    sqlite3Bt
cb300 72 65 65 49 6e 69 74 50 61 67 65 28 70 50 61 67  reeInitPage(pPag
cb310 65 2c 20 70 50 61 67 65 2d 3e 70 50 61 72 65 6e  e, pPage->pParen
cb320 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  t);.  }.}../*.**
cb330 20 4f 70 65 6e 20 61 20 64 61 74 61 62 61 73 65   Open a database
cb340 20 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 7a 46   file..** .** zF
cb350 69 6c 65 6e 61 6d 65 20 69 73 20 74 68 65 20 6e  ilename is the n
cb360 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
cb370 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 7a 46  ase file.  If zF
cb380 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c 0a  ilename is NULL.
cb390 2a 2a 20 61 20 6e 65 77 20 64 61 74 61 62 61 73  ** a new databas
cb3a0 65 20 77 69 74 68 20 61 20 72 61 6e 64 6f 6d 20  e with a random 
cb3b0 6e 61 6d 65 20 69 73 20 63 72 65 61 74 65 64 2e  name is created.
cb3c0 20 20 54 68 69 73 20 72 61 6e 64 6f 6d 6c 79 20    This randomly 
cb3d0 6e 61 6d 65 64 0a 2a 2a 20 64 61 74 61 62 61 73  named.** databas
cb3e0 65 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 64  e file will be d
cb3f0 65 6c 65 74 65 64 20 77 68 65 6e 20 73 71 6c 69  eleted when sqli
cb400 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 29 20  te3BtreeClose() 
cb410 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51  is called..*/.SQ
cb420 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
cb430 20 73 71 6c 69 74 65 33 42 74 72 65 65 4f 70 65   sqlite3BtreeOpe
cb440 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  n(.  const char 
cb450 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 2f 2a 20  *zFilename,  /* 
cb460 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65  Name of the file
cb470 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
cb480 42 54 72 65 65 20 64 61 74 61 62 61 73 65 20 2a  BTree database *
cb490 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 53 71  /.  sqlite3 *pSq
cb4a0 6c 69 74 65 2c 20 20 20 20 20 20 20 2f 2a 20 41  lite,       /* A
cb4b0 73 73 6f 63 69 61 74 65 64 20 64 61 74 61 62 61  ssociated databa
cb4c0 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 42  se handle */.  B
cb4d0 74 72 65 65 20 2a 2a 70 70 42 74 72 65 65 2c 20  tree **ppBtree, 
cb4e0 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
cb4f0 72 20 74 6f 20 6e 65 77 20 42 74 72 65 65 20 6f  r to new Btree o
cb500 62 6a 65 63 74 20 77 72 69 74 74 65 6e 20 68 65  bject written he
cb510 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67  re */.  int flag
cb520 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s               
cb530 2f 2a 20 4f 70 74 69 6f 6e 73 20 2a 2f 0a 29 7b  /* Options */.){
cb540 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
cb550 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68  ;          /* Sh
cb560 61 72 65 64 20 70 61 72 74 20 6f 66 20 62 74 72  ared part of btr
cb570 65 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a  ee structure */.
cb580 20 20 42 74 72 65 65 20 2a 70 3b 20 20 20 20 20    Btree *p;     
cb590 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e            /* Han
cb5a0 64 6c 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f  dle to return */
cb5b0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
cb5c0 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6e 52 65  TE_OK;.  int nRe
cb5d0 73 65 72 76 65 3b 0a 20 20 75 6e 73 69 67 6e 65  serve;.  unsigne
cb5e0 64 20 63 68 61 72 20 7a 44 62 48 65 61 64 65 72  d char zDbHeader
cb5f0 5b 31 30 30 5d 3b 0a 23 69 66 20 21 64 65 66 69  [100];.#if !defi
cb600 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
cb610 53 48 41 52 45 44 5f 43 41 43 48 45 29 20 26 26  SHARED_CACHE) &&
cb620 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
cb630 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 0a 20 20  _OMIT_DISKIO).  
cb640 63 6f 6e 73 74 20 54 68 72 65 61 64 44 61 74 61  const ThreadData
cb650 20 2a 70 54 73 64 72 6f 3b 0a 23 65 6e 64 69 66   *pTsdro;.#endif
cb660 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 76  ..  /* Set the v
cb670 61 72 69 61 62 6c 65 20 69 73 4d 65 6d 64 62 20  ariable isMemdb 
cb680 74 6f 20 74 72 75 65 20 66 6f 72 20 61 6e 20 69  to true for an i
cb690 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73  n-memory databas
cb6a0 65 2c 20 6f 72 20 0a 20 20 2a 2a 20 66 61 6c 73  e, or .  ** fals
cb6b0 65 20 66 6f 72 20 61 20 66 69 6c 65 2d 62 61 73  e for a file-bas
cb6c0 65 64 20 64 61 74 61 62 61 73 65 2e 20 54 68 69  ed database. Thi
cb6d0 73 20 73 79 6d 62 6f 6c 20 69 73 20 6f 6e 6c 79  s symbol is only
cb6e0 20 72 65 71 75 69 72 65 64 20 69 66 0a 20 20 2a   required if.  *
cb6f0 2a 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 20  * either of the 
cb700 73 68 61 72 65 64 2d 64 61 74 61 20 6f 72 20 61  shared-data or a
cb710 75 74 6f 76 61 63 75 75 6d 20 66 65 61 74 75 72  utovacuum featur
cb720 65 73 20 61 72 65 20 63 6f 6d 70 69 6c 65 64 20  es are compiled 
cb730 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 6c  .  ** into the l
cb740 69 62 72 61 72 79 2e 0a 20 20 2a 2f 0a 23 69 66  ibrary..  */.#if
cb750 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
cb760 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
cb770 48 45 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28  HE) || !defined(
cb780 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
cb790 56 41 43 55 55 4d 29 0a 20 20 23 69 66 64 65 66  VACUUM).  #ifdef
cb7a0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d   SQLITE_OMIT_MEM
cb7b0 4f 52 59 44 42 0a 20 20 20 20 63 6f 6e 73 74 20  ORYDB.    const 
cb7c0 69 6e 74 20 69 73 4d 65 6d 64 62 20 3d 20 30 3b  int isMemdb = 0;
cb7d0 0a 20 20 23 65 6c 73 65 0a 20 20 20 20 63 6f 6e  .  #else.    con
cb7e0 73 74 20 69 6e 74 20 69 73 4d 65 6d 64 62 20 3d  st int isMemdb =
cb7f0 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 21 73   zFilename && !s
cb800 74 72 63 6d 70 28 7a 46 69 6c 65 6e 61 6d 65 2c  trcmp(zFilename,
cb810 20 22 3a 6d 65 6d 6f 72 79 3a 22 29 3b 0a 20 20   ":memory:");.  
cb820 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 20  #endif.#endif.. 
cb830 20 70 20 3d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f   p = sqliteMallo
cb840 63 28 73 69 7a 65 6f 66 28 42 74 72 65 65 29 29  c(sizeof(Btree))
cb850 3b 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20  ;.  if( !p ){.  
cb860 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
cb870 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 2d 3e  NOMEM;.  }.  p->
cb880 69 6e 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f  inTrans = TRANS_
cb890 4e 4f 4e 45 3b 0a 20 20 70 2d 3e 70 53 71 6c 69  NONE;.  p->pSqli
cb8a0 74 65 20 3d 20 70 53 71 6c 69 74 65 3b 0a 0a 20  te = pSqlite;.. 
cb8b0 20 2f 2a 20 54 72 79 20 74 6f 20 66 69 6e 64 20   /* Try to find 
cb8c0 61 6e 20 65 78 69 73 74 69 6e 67 20 42 74 72 65  an existing Btre
cb8d0 65 20 73 74 72 75 63 74 75 72 65 20 6f 70 65 6e  e structure open
cb8e0 65 64 20 6f 6e 20 7a 46 69 6c 65 6e 61 6d 65 2e  ed on zFilename.
cb8f0 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64   */.#if !defined
cb900 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41  (SQLITE_OMIT_SHA
cb910 52 45 44 5f 43 41 43 48 45 29 20 26 26 20 21 64  RED_CACHE) && !d
cb920 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
cb930 49 54 5f 44 49 53 4b 49 4f 29 0a 20 20 70 54 73  IT_DISKIO).  pTs
cb940 64 72 6f 20 3d 20 73 71 6c 69 74 65 33 54 68 72  dro = sqlite3Thr
cb950 65 61 64 44 61 74 61 52 65 61 64 4f 6e 6c 79 28  eadDataReadOnly(
cb960 29 3b 0a 20 20 69 66 28 20 70 54 73 64 72 6f 2d  );.  if( pTsdro-
cb970 3e 75 73 65 53 68 61 72 65 64 44 61 74 61 20 26  >useSharedData &
cb980 26 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 21  & zFilename && !
cb990 69 73 4d 65 6d 64 62 20 29 7b 0a 20 20 20 20 63  isMemdb ){.    c
cb9a0 68 61 72 20 2a 7a 46 75 6c 6c 50 61 74 68 6e 61  har *zFullPathna
cb9b0 6d 65 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 75  me = sqlite3OsFu
cb9c0 6c 6c 50 61 74 68 6e 61 6d 65 28 7a 46 69 6c 65  llPathname(zFile
cb9d0 6e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 21  name);.    if( !
cb9e0 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 29 7b  zFullPathname ){
cb9f0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 46 72 65  .      sqliteFre
cba00 65 28 70 29 3b 0a 20 20 20 20 20 20 72 65 74 75  e(p);.      retu
cba10 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
cba20 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 70  .    }.    for(p
cba30 42 74 3d 70 54 73 64 72 6f 2d 3e 70 42 74 72 65  Bt=pTsdro->pBtre
cba40 65 3b 20 70 42 74 3b 20 70 42 74 3d 70 42 74 2d  e; pBt; pBt=pBt-
cba50 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 61  >pNext){.      a
cba60 73 73 65 72 74 28 20 70 42 74 2d 3e 6e 52 65 66  ssert( pBt->nRef
cba70 3e 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  >0 );.      if( 
cba80 30 3d 3d 73 74 72 63 6d 70 28 7a 46 75 6c 6c 50  0==strcmp(zFullP
cba90 61 74 68 6e 61 6d 65 2c 20 73 71 6c 69 74 65 33  athname, sqlite3
cbaa0 50 61 67 65 72 46 69 6c 65 6e 61 6d 65 28 70 42  PagerFilename(pB
cbab0 74 2d 3e 70 50 61 67 65 72 29 29 20 29 7b 0a 20  t->pPager)) ){. 
cbac0 20 20 20 20 20 20 20 70 2d 3e 70 42 74 20 3d 20         p->pBt = 
cbad0 70 42 74 3b 0a 20 20 20 20 20 20 20 20 2a 70 70  pBt;.        *pp
cbae0 42 74 72 65 65 20 3d 20 70 3b 0a 20 20 20 20 20  Btree = p;.     
cbaf0 20 20 20 70 42 74 2d 3e 6e 52 65 66 2b 2b 3b 0a     pBt->nRef++;.
cbb00 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 46 72          sqliteFr
cbb10 65 65 28 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65  ee(zFullPathname
cbb20 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  );.        retur
cbb30 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  n SQLITE_OK;.   
cbb40 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73     }.    }.    s
cbb50 71 6c 69 74 65 46 72 65 65 28 7a 46 75 6c 6c 50  qliteFree(zFullP
cbb60 61 74 68 6e 61 6d 65 29 3b 0a 20 20 7d 0a 23 65  athname);.  }.#e
cbb70 6e 64 69 66 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  ndif..  /*.  ** 
cbb80 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73  The following as
cbb90 73 65 72 74 73 20 6d 61 6b 65 20 73 75 72 65 20  serts make sure 
cbba0 74 68 61 74 20 73 74 72 75 63 74 75 72 65 73 20  that structures 
cbbb0 75 73 65 64 20 62 79 20 74 68 65 20 62 74 72 65  used by the btre
cbbc0 65 20 61 72 65 0a 20 20 2a 2a 20 74 68 65 20 72  e are.  ** the r
cbbd0 69 67 68 74 20 73 69 7a 65 2e 20 20 54 68 69 73  ight size.  This
cbbe0 20 69 73 20 74 6f 20 67 75 61 72 64 20 61 67 61   is to guard aga
cbbf0 69 6e 73 74 20 73 69 7a 65 20 63 68 61 6e 67 65  inst size change
cbc00 73 20 74 68 61 74 20 72 65 73 75 6c 74 0a 20 20  s that result.  
cbc10 2a 2a 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e  ** when compilin
cbc20 67 20 6f 6e 20 61 20 64 69 66 66 65 72 65 6e 74  g on a different
cbc30 20 61 72 63 68 69 74 65 63 74 75 72 65 2e 0a 20   architecture.. 
cbc40 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 69   */.  assert( si
cbc50 7a 65 6f 66 28 69 36 34 29 3d 3d 38 20 7c 7c 20  zeof(i64)==8 || 
cbc60 73 69 7a 65 6f 66 28 69 36 34 29 3d 3d 34 20 29  sizeof(i64)==4 )
cbc70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65  ;.  assert( size
cbc80 6f 66 28 75 36 34 29 3d 3d 38 20 7c 7c 20 73 69  of(u64)==8 || si
cbc90 7a 65 6f 66 28 75 36 34 29 3d 3d 34 20 29 3b 0a  zeof(u64)==4 );.
cbca0 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66    assert( sizeof
cbcb0 28 75 33 32 29 3d 3d 34 20 29 3b 0a 20 20 61 73  (u32)==4 );.  as
cbcc0 73 65 72 74 28 20 73 69 7a 65 6f 66 28 75 31 36  sert( sizeof(u16
cbcd0 29 3d 3d 32 20 29 3b 0a 20 20 61 73 73 65 72 74  )==2 );.  assert
cbce0 28 20 73 69 7a 65 6f 66 28 50 67 6e 6f 29 3d 3d  ( sizeof(Pgno)==
cbcf0 34 20 29 3b 0a 0a 20 20 70 42 74 20 3d 20 73 71  4 );..  pBt = sq
cbd00 6c 69 74 65 4d 61 6c 6c 6f 63 28 20 73 69 7a 65  liteMalloc( size
cbd10 6f 66 28 2a 70 42 74 29 20 29 3b 0a 20 20 69 66  of(*pBt) );.  if
cbd20 28 20 70 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20  ( pBt==0 ){.    
cbd30 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
cbd40 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 62 74 72 65  M;.    goto btre
cbd50 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 7d 0a  e_open_out;.  }.
cbd60 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
cbd70 67 65 72 4f 70 65 6e 28 26 70 42 74 2d 3e 70 50  gerOpen(&pBt->pP
cbd80 61 67 65 72 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c  ager, zFilename,
cbd90 20 45 58 54 52 41 5f 53 49 5a 45 2c 20 66 6c 61   EXTRA_SIZE, fla
cbda0 67 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  gs);.  if( rc==S
cbdb0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
cbdc0 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
cbdd0 72 52 65 61 64 46 69 6c 65 68 65 61 64 65 72 28  rReadFileheader(
cbde0 70 42 74 2d 3e 70 50 61 67 65 72 2c 73 69 7a 65  pBt->pPager,size
cbdf0 6f 66 28 7a 44 62 48 65 61 64 65 72 29 2c 7a 44  of(zDbHeader),zD
cbe00 62 48 65 61 64 65 72 29 3b 0a 20 20 7d 0a 20 20  bHeader);.  }.  
cbe10 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
cbe20 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 62 74  K ){.    goto bt
cbe30 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20  ree_open_out;.  
cbe40 7d 0a 20 20 70 2d 3e 70 42 74 20 3d 20 70 42 74  }.  p->pBt = pBt
cbe50 3b 0a 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65  ;..  sqlite3Page
cbe60 72 53 65 74 44 65 73 74 72 75 63 74 6f 72 28 70  rSetDestructor(p
cbe70 42 74 2d 3e 70 50 61 67 65 72 2c 20 70 61 67 65  Bt->pPager, page
cbe80 44 65 73 74 72 75 63 74 6f 72 29 3b 0a 20 20 73  Destructor);.  s
cbe90 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 52 65  qlite3PagerSetRe
cbea0 69 6e 69 74 65 72 28 70 42 74 2d 3e 70 50 61 67  initer(pBt->pPag
cbeb0 65 72 2c 20 70 61 67 65 52 65 69 6e 69 74 29 3b  er, pageReinit);
cbec0 0a 20 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 20  .  pBt->pCursor 
cbed0 3d 20 30 3b 0a 20 20 70 42 74 2d 3e 70 50 61 67  = 0;.  pBt->pPag
cbee0 65 31 20 3d 20 30 3b 0a 20 20 70 42 74 2d 3e 72  e1 = 0;.  pBt->r
cbef0 65 61 64 4f 6e 6c 79 20 3d 20 73 71 6c 69 74 65  eadOnly = sqlite
cbf00 33 50 61 67 65 72 49 73 72 65 61 64 6f 6e 6c 79  3PagerIsreadonly
cbf10 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20  (pBt->pPager);. 
cbf20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d   pBt->pageSize =
cbf30 20 67 65 74 32 62 79 74 65 28 26 7a 44 62 48 65   get2byte(&zDbHe
cbf40 61 64 65 72 5b 31 36 5d 29 3b 0a 20 20 69 66 28  ader[16]);.  if(
cbf50 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3c 35   pBt->pageSize<5
cbf60 31 32 20 7c 7c 20 70 42 74 2d 3e 70 61 67 65 53  12 || pBt->pageS
cbf70 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50  ize>SQLITE_MAX_P
cbf80 41 47 45 5f 53 49 5a 45 0a 20 20 20 20 20 20 20  AGE_SIZE.       
cbf90 7c 7c 20 28 28 70 42 74 2d 3e 70 61 67 65 53 69  || ((pBt->pageSi
cbfa0 7a 65 2d 31 29 26 70 42 74 2d 3e 70 61 67 65 53  ze-1)&pBt->pageS
cbfb0 69 7a 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 70  ize)!=0 ){.    p
cbfc0 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 53  Bt->pageSize = S
cbfd0 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41  QLITE_DEFAULT_PA
cbfe0 47 45 5f 53 49 5a 45 3b 0a 20 20 20 20 70 42 74  GE_SIZE;.    pBt
cbff0 2d 3e 6d 61 78 45 6d 62 65 64 46 72 61 63 20 3d  ->maxEmbedFrac =
cc000 20 36 34 3b 20 20 20 2f 2a 20 32 35 25 20 2a 2f   64;   /* 25% */
cc010 0a 20 20 20 20 70 42 74 2d 3e 6d 69 6e 45 6d 62  .    pBt->minEmb
cc020 65 64 46 72 61 63 20 3d 20 33 32 3b 20 20 20 2f  edFrac = 32;   /
cc030 2a 20 31 32 2e 35 25 20 2a 2f 0a 20 20 20 20 70  * 12.5% */.    p
cc040 42 74 2d 3e 6d 69 6e 4c 65 61 66 46 72 61 63 20  Bt->minLeafFrac 
cc050 3d 20 33 32 3b 20 20 20 20 2f 2a 20 31 32 2e 35  = 32;    /* 12.5
cc060 25 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  % */.#ifndef SQL
cc070 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
cc080 55 55 4d 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  UUM.    /* If th
cc090 65 20 6d 61 67 69 63 20 6e 61 6d 65 20 22 3a 6d  e magic name ":m
cc0a0 65 6d 6f 72 79 3a 22 20 77 69 6c 6c 20 63 72 65  emory:" will cre
cc0b0 61 74 65 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79  ate an in-memory
cc0c0 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 0a   database, then.
cc0d0 20 20 20 20 2a 2a 20 6c 65 61 76 65 20 74 68 65      ** leave the
cc0e0 20 61 75 74 6f 56 61 63 75 75 6d 20 6d 6f 64 65   autoVacuum mode
cc0f0 20 61 74 20 30 20 28 64 6f 20 6e 6f 74 20 61 75   at 0 (do not au
cc100 74 6f 2d 76 61 63 75 75 6d 29 2c 20 65 76 65 6e  to-vacuum), even
cc110 20 69 66 0a 20 20 20 20 2a 2a 20 53 51 4c 49 54   if.    ** SQLIT
cc120 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41  E_DEFAULT_AUTOVA
cc130 43 55 55 4d 20 69 73 20 74 72 75 65 2e 20 4f 6e  CUUM is true. On
cc140 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c   the other hand,
cc150 20 69 66 0a 20 20 20 20 2a 2a 20 53 51 4c 49 54   if.    ** SQLIT
cc160 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 20  E_OMIT_MEMORYDB 
cc170 68 61 73 20 62 65 65 6e 20 64 65 66 69 6e 65 64  has been defined
cc180 2c 20 74 68 65 6e 20 22 3a 6d 65 6d 6f 72 79 3a  , then ":memory:
cc190 22 20 69 73 20 6a 75 73 74 20 61 0a 20 20 20 20  " is just a.    
cc1a0 2a 2a 20 72 65 67 75 6c 61 72 20 66 69 6c 65 2d  ** regular file-
cc1b0 6e 61 6d 65 2e 20 49 6e 20 74 68 69 73 20 63 61  name. In this ca
cc1c0 73 65 20 74 68 65 20 61 75 74 6f 2d 76 61 63 75  se the auto-vacu
cc1d0 75 6d 20 61 70 70 6c 69 65 73 20 61 73 20 70 65  um applies as pe
cc1e0 72 20 6e 6f 72 6d 61 6c 2e 0a 20 20 20 20 2a 2f  r normal..    */
cc1f0 0a 20 20 20 20 69 66 28 20 7a 46 69 6c 65 6e 61  .    if( zFilena
cc200 6d 65 20 26 26 20 21 69 73 4d 65 6d 64 62 20 29  me && !isMemdb )
cc210 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 61 75 74  {.      pBt->aut
cc220 6f 56 61 63 75 75 6d 20 3d 20 28 53 51 4c 49 54  oVacuum = (SQLIT
cc230 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41  E_DEFAULT_AUTOVA
cc240 43 55 55 4d 20 3f 20 31 20 3a 20 30 29 3b 0a 20  CUUM ? 1 : 0);. 
cc250 20 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61       pBt->incrVa
cc260 63 75 75 6d 20 3d 20 28 53 51 4c 49 54 45 5f 44  cuum = (SQLITE_D
cc270 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55  EFAULT_AUTOVACUU
cc280 4d 3d 3d 32 20 3f 20 31 20 3a 20 30 29 3b 0a 20  M==2 ? 1 : 0);. 
cc290 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
cc2a0 6e 52 65 73 65 72 76 65 20 3d 20 30 3b 0a 20 20  nReserve = 0;.  
cc2b0 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 52 65 73 65  }else{.    nRese
cc2c0 72 76 65 20 3d 20 7a 44 62 48 65 61 64 65 72 5b  rve = zDbHeader[
cc2d0 32 30 5d 3b 0a 20 20 20 20 70 42 74 2d 3e 6d 61  20];.    pBt->ma
cc2e0 78 45 6d 62 65 64 46 72 61 63 20 3d 20 7a 44 62  xEmbedFrac = zDb
cc2f0 48 65 61 64 65 72 5b 32 31 5d 3b 0a 20 20 20 20  Header[21];.    
cc300 70 42 74 2d 3e 6d 69 6e 45 6d 62 65 64 46 72 61  pBt->minEmbedFra
cc310 63 20 3d 20 7a 44 62 48 65 61 64 65 72 5b 32 32  c = zDbHeader[22
cc320 5d 3b 0a 20 20 20 20 70 42 74 2d 3e 6d 69 6e 4c  ];.    pBt->minL
cc330 65 61 66 46 72 61 63 20 3d 20 7a 44 62 48 65 61  eafFrac = zDbHea
cc340 64 65 72 5b 32 33 5d 3b 0a 20 20 20 20 70 42 74  der[23];.    pBt
cc350 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64 20  ->pageSizeFixed 
cc360 3d 20 31 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  = 1;.#ifndef SQL
cc370 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
cc380 55 55 4d 0a 20 20 20 20 70 42 74 2d 3e 61 75 74  UUM.    pBt->aut
cc390 6f 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34 62  oVacuum = (get4b
cc3a0 79 74 65 28 26 7a 44 62 48 65 61 64 65 72 5b 33  yte(&zDbHeader[3
cc3b0 36 20 2b 20 34 2a 34 5d 29 3f 31 3a 30 29 3b 0a  6 + 4*4])?1:0);.
cc3c0 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 63      pBt->incrVac
cc3d0 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28  uum = (get4byte(
cc3e0 26 7a 44 62 48 65 61 64 65 72 5b 33 36 20 2b 20  &zDbHeader[36 + 
cc3f0 37 2a 34 5d 29 3f 31 3a 30 29 3b 0a 23 65 6e 64  7*4])?1:0);.#end
cc400 69 66 0a 20 20 7d 0a 20 20 70 42 74 2d 3e 75 73  if.  }.  pBt->us
cc410 61 62 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e  ableSize = pBt->
cc420 70 61 67 65 53 69 7a 65 20 2d 20 6e 52 65 73 65  pageSize - nRese
cc430 72 76 65 3b 0a 20 20 61 73 73 65 72 74 28 20 28  rve;.  assert( (
cc440 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 26 20  pBt->pageSize & 
cc450 37 29 3d 3d 30 20 29 3b 20 20 2f 2a 20 38 2d 62  7)==0 );  /* 8-b
cc460 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 66  yte alignment of
cc470 20 70 61 67 65 53 69 7a 65 20 2a 2f 0a 20 20 73   pageSize */.  s
cc480 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61  qlite3PagerSetPa
cc490 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67  gesize(pBt->pPag
cc4a0 65 72 2c 20 70 42 74 2d 3e 70 61 67 65 53 69 7a  er, pBt->pageSiz
cc4b0 65 29 3b 0a 0a 23 69 66 20 21 64 65 66 69 6e 65  e);..#if !define
cc4c0 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  d(SQLITE_OMIT_SH
cc4d0 41 52 45 44 5f 43 41 43 48 45 29 20 26 26 20 21  ARED_CACHE) && !
cc4e0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
cc4f0 4d 49 54 5f 44 49 53 4b 49 4f 29 0a 20 20 2f 2a  MIT_DISKIO).  /*
cc500 20 41 64 64 20 74 68 65 20 6e 65 77 20 62 74 72   Add the new btr
cc510 65 65 20 74 6f 20 74 68 65 20 6c 69 6e 6b 65 64  ee to the linked
cc520 20 6c 69 73 74 20 73 74 61 72 74 69 6e 67 20 61   list starting a
cc530 74 20 54 68 72 65 61 64 44 61 74 61 2e 70 42 74  t ThreadData.pBt
cc540 72 65 65 2e 0a 20 20 2a 2a 20 54 68 65 72 65 20  ree..  ** There 
cc550 69 73 20 6e 6f 20 63 68 61 6e 63 65 20 74 68 61  is no chance tha
cc560 74 20 61 20 6d 61 6c 6c 6f 63 28 29 20 6d 61 79  t a malloc() may
cc570 20 66 61 69 6c 20 69 6e 73 69 64 65 20 6f 66 20   fail inside of 
cc580 74 68 65 20 0a 20 20 2a 2a 20 73 71 6c 69 74 65  the .  ** sqlite
cc590 33 54 68 72 65 61 64 44 61 74 61 28 29 20 63 61  3ThreadData() ca
cc5a0 6c 6c 2c 20 61 73 20 74 68 65 20 54 68 72 65 61  ll, as the Threa
cc5b0 64 44 61 74 61 20 73 74 72 75 63 74 75 72 65 20  dData structure 
cc5c0 6d 75 73 74 20 68 61 76 65 20 61 6c 72 65 61 64  must have alread
cc5d0 79 0a 20 20 2a 2a 20 62 65 65 6e 20 61 6c 6c 6f  y.  ** been allo
cc5e0 63 61 74 65 64 20 66 6f 72 20 70 54 73 64 72 6f  cated for pTsdro
cc5f0 2d 3e 75 73 65 53 68 61 72 65 64 44 61 74 61 20  ->useSharedData 
cc600 74 6f 20 62 65 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a  to be non-zero..
cc610 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 73 64 72    */.  if( pTsdr
cc620 6f 2d 3e 75 73 65 53 68 61 72 65 64 44 61 74 61  o->useSharedData
cc630 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26   && zFilename &&
cc640 20 21 69 73 4d 65 6d 64 62 20 29 7b 0a 20 20 20   !isMemdb ){.   
cc650 20 70 42 74 2d 3e 70 4e 65 78 74 20 3d 20 70 54   pBt->pNext = pT
cc660 73 64 72 6f 2d 3e 70 42 74 72 65 65 3b 0a 20 20  sdro->pBtree;.  
cc670 20 20 73 71 6c 69 74 65 33 54 68 72 65 61 64 44    sqlite3ThreadD
cc680 61 74 61 28 29 2d 3e 70 42 74 72 65 65 20 3d 20  ata()->pBtree = 
cc690 70 42 74 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  pBt;.  }.#endif.
cc6a0 20 20 70 42 74 2d 3e 6e 52 65 66 20 3d 20 31 3b    pBt->nRef = 1;
cc6b0 0a 20 20 2a 70 70 42 74 72 65 65 20 3d 20 70 3b  .  *ppBtree = p;
cc6c0 0a 0a 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74  ..btree_open_out
cc6d0 3a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  :.  if( rc!=SQLI
cc6e0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28  TE_OK ){.    if(
cc6f0 20 70 42 74 20 26 26 20 70 42 74 2d 3e 70 50 61   pBt && pBt->pPa
cc700 67 65 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ger ){.      sql
cc710 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 70  ite3PagerClose(p
cc720 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20  Bt->pPager);.   
cc730 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 46 72 65   }.    sqliteFre
cc740 65 28 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69  e(pBt);.    sqli
cc750 74 65 46 72 65 65 28 70 29 3b 0a 20 20 20 20 2a  teFree(p);.    *
cc760 70 70 42 74 72 65 65 20 3d 20 30 3b 0a 20 20 7d  ppBtree = 0;.  }
cc770 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
cc780 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6e 20  ./*.** Close an 
cc790 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 61 6e  open database an
cc7a0 64 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c  d invalidate all
cc7b0 20 63 75 72 73 6f 72 73 2e 0a 2a 2f 0a 53 51 4c   cursors..*/.SQL
cc7c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
cc7d0 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73  sqlite3BtreeClos
cc7e0 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42  e(Btree *p){.  B
cc7f0 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
cc800 2d 3e 70 42 74 3b 0a 20 20 42 74 43 75 72 73 6f  ->pBt;.  BtCurso
cc810 72 20 2a 70 43 75 72 3b 0a 0a 23 69 66 6e 64 65  r *pCur;..#ifnde
cc820 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
cc830 41 52 45 44 5f 43 41 43 48 45 0a 20 20 54 68 72  ARED_CACHE.  Thr
cc840 65 61 64 44 61 74 61 20 2a 70 54 73 64 3b 0a 23  eadData *pTsd;.#
cc850 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 43 6c 6f 73  endif..  /* Clos
cc860 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 6f 70  e all cursors op
cc870 65 6e 65 64 20 76 69 61 20 74 68 69 73 20 68 61  ened via this ha
cc880 6e 64 6c 65 2e 20 20 2a 2f 0a 20 20 70 43 75 72  ndle.  */.  pCur
cc890 20 3d 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b   = pBt->pCursor;
cc8a0 0a 20 20 77 68 69 6c 65 28 20 70 43 75 72 20 29  .  while( pCur )
cc8b0 7b 0a 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a  {.    BtCursor *
cc8c0 70 54 6d 70 20 3d 20 70 43 75 72 3b 0a 20 20 20  pTmp = pCur;.   
cc8d0 20 70 43 75 72 20 3d 20 70 43 75 72 2d 3e 70 4e   pCur = pCur->pN
cc8e0 65 78 74 3b 0a 20 20 20 20 69 66 28 20 70 54 6d  ext;.    if( pTm
cc8f0 70 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 7b 0a  p->pBtree==p ){.
cc900 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
cc910 65 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 70 54  eeCloseCursor(pT
cc920 6d 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  mp);.    }.  }..
cc930 20 20 2f 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 6e    /* Rollback an
cc940 79 20 61 63 74 69 76 65 20 74 72 61 6e 73 61 63  y active transac
cc950 74 69 6f 6e 20 61 6e 64 20 66 72 65 65 20 74 68  tion and free th
cc960 65 20 68 61 6e 64 6c 65 20 73 74 72 75 63 74 75  e handle structu
cc970 72 65 2e 0a 20 20 2a 2a 20 54 68 65 20 63 61 6c  re..  ** The cal
cc980 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65  l to sqlite3Btre
cc990 65 52 6f 6c 6c 62 61 63 6b 28 29 20 64 72 6f 70  eRollback() drop
cc9a0 73 20 61 6e 79 20 74 61 62 6c 65 2d 6c 6f 63 6b  s any table-lock
cc9b0 73 20 68 65 6c 64 20 62 79 0a 20 20 2a 2a 20 74  s held by.  ** t
cc9c0 68 69 73 20 68 61 6e 64 6c 65 2e 0a 20 20 2a 2f  his handle..  */
cc9d0 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 52  .  sqlite3BtreeR
cc9e0 6f 6c 6c 62 61 63 6b 28 70 29 3b 0a 20 20 73 71  ollback(p);.  sq
cc9f0 6c 69 74 65 46 72 65 65 28 70 29 3b 0a 0a 23 69  liteFree(p);..#i
cca00 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
cca10 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20  T_SHARED_CACHE. 
cca20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65   /* If there are
cca30 20 73 74 69 6c 6c 20 6f 74 68 65 72 20 6f 75 74   still other out
cca40 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e  standing referen
cca50 63 65 73 20 74 6f 20 74 68 65 20 73 68 61 72 65  ces to the share
cca60 64 2d 62 74 72 65 65 0a 20 20 2a 2a 20 73 74 72  d-btree.  ** str
cca70 75 63 74 75 72 65 2c 20 72 65 74 75 72 6e 20 6e  ucture, return n
cca80 6f 77 2e 20 54 68 65 20 72 65 6d 61 69 6e 64 65  ow. The remainde
cca90 72 20 6f 66 20 74 68 69 73 20 70 72 6f 63 65 64  r of this proced
ccaa0 75 72 65 20 63 6c 65 61 6e 73 20 0a 20 20 2a 2a  ure cleans .  **
ccab0 20 75 70 20 74 68 65 20 73 68 61 72 65 64 2d 62   up the shared-b
ccac0 74 72 65 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  tree..  */.  ass
ccad0 65 72 74 28 20 70 42 74 2d 3e 6e 52 65 66 3e 30  ert( pBt->nRef>0
ccae0 20 29 3b 0a 20 20 70 42 74 2d 3e 6e 52 65 66 2d   );.  pBt->nRef-
ccaf0 2d 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 6e 52  -;.  if( pBt->nR
ccb00 65 66 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ef ){.    return
ccb10 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
ccb20 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65  .  /* Remove the
ccb30 20 73 68 61 72 65 64 2d 62 74 72 65 65 20 66 72   shared-btree fr
ccb40 6f 6d 20 74 68 65 20 74 68 72 65 61 64 20 77 69  om the thread wi
ccb50 64 65 20 6c 69 73 74 2e 20 43 61 6c 6c 20 0a 20  de list. Call . 
ccb60 20 2a 2a 20 54 68 72 65 61 64 44 61 74 61 52 65   ** ThreadDataRe
ccb70 61 64 4f 6e 6c 79 28 29 20 61 6e 64 20 74 68 65  adOnly() and the
ccb80 6e 20 63 61 73 74 20 61 77 61 79 20 74 68 65 20  n cast away the 
ccb90 63 6f 6e 73 74 20 70 72 6f 70 65 72 74 79 20 6f  const property o
ccba0 66 20 74 68 65 20 0a 20 20 2a 2a 20 70 6f 69 6e  f the .  ** poin
ccbb0 74 65 72 20 74 6f 20 61 76 6f 69 64 20 61 6c 6c  ter to avoid all
ccbc0 6f 63 61 74 69 6e 67 20 74 68 72 65 61 64 20 64  ocating thread d
ccbd0 61 74 61 20 69 66 20 69 74 20 69 73 20 6e 6f 74  ata if it is not
ccbe0 20 72 65 61 6c 6c 79 20 72 65 71 75 69 72 65 64   really required
ccbf0 2e 0a 20 20 2a 2f 0a 20 20 70 54 73 64 20 3d 20  ..  */.  pTsd = 
ccc00 28 54 68 72 65 61 64 44 61 74 61 20 2a 29 73 71  (ThreadData *)sq
ccc10 6c 69 74 65 33 54 68 72 65 61 64 44 61 74 61 52  lite3ThreadDataR
ccc20 65 61 64 4f 6e 6c 79 28 29 3b 0a 20 20 69 66 28  eadOnly();.  if(
ccc30 20 70 54 73 64 2d 3e 70 42 74 72 65 65 3d 3d 70   pTsd->pBtree==p
ccc40 42 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  Bt ){.    assert
ccc50 28 20 70 54 73 64 3d 3d 73 71 6c 69 74 65 33 54  ( pTsd==sqlite3T
ccc60 68 72 65 61 64 44 61 74 61 28 29 20 29 3b 0a 20  hreadData() );. 
ccc70 20 20 20 70 54 73 64 2d 3e 70 42 74 72 65 65 20     pTsd->pBtree 
ccc80 3d 20 70 42 74 2d 3e 70 4e 65 78 74 3b 0a 20 20  = pBt->pNext;.  
ccc90 7d 65 6c 73 65 7b 0a 20 20 20 20 42 74 53 68 61  }else{.    BtSha
ccca0 72 65 64 20 2a 70 50 72 65 76 3b 0a 20 20 20 20  red *pPrev;.    
cccb0 66 6f 72 28 70 50 72 65 76 3d 70 54 73 64 2d 3e  for(pPrev=pTsd->
cccc0 70 42 74 72 65 65 3b 20 70 50 72 65 76 20 26 26  pBtree; pPrev &&
cccd0 20 70 50 72 65 76 2d 3e 70 4e 65 78 74 21 3d 70   pPrev->pNext!=p
ccce0 42 74 3b 20 70 50 72 65 76 3d 70 50 72 65 76 2d  Bt; pPrev=pPrev-
cccf0 3e 70 4e 65 78 74 29 7b 7d 0a 20 20 20 20 69 66  >pNext){}.    if
ccd00 28 20 70 50 72 65 76 20 29 7b 0a 20 20 20 20 20  ( pPrev ){.     
ccd10 20 61 73 73 65 72 74 28 20 70 54 73 64 3d 3d 73   assert( pTsd==s
ccd20 71 6c 69 74 65 33 54 68 72 65 61 64 44 61 74 61  qlite3ThreadData
ccd30 28 29 20 29 3b 0a 20 20 20 20 20 20 70 50 72 65  () );.      pPre
ccd40 76 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e  v->pNext = pBt->
ccd50 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d  pNext;.    }.  }
ccd60 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 43 6c  .#endif..  /* Cl
ccd70 6f 73 65 20 74 68 65 20 70 61 67 65 72 20 61 6e  ose the pager an
ccd80 64 20 66 72 65 65 20 74 68 65 20 73 68 61 72 65  d free the share
ccd90 64 2d 62 74 72 65 65 20 73 74 72 75 63 74 75 72  d-btree structur
ccda0 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 21  e */.  assert( !
ccdb0 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 29 3b 0a  pBt->pCursor );.
ccdc0 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c    sqlite3PagerCl
ccdd0 6f 73 65 28 70 42 74 2d 3e 70 50 61 67 65 72 29  ose(pBt->pPager)
ccde0 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 78 46 72  ;.  if( pBt->xFr
ccdf0 65 65 53 63 68 65 6d 61 20 26 26 20 70 42 74 2d  eeSchema && pBt-
cce00 3e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20  >pSchema ){.    
cce10 70 42 74 2d 3e 78 46 72 65 65 53 63 68 65 6d 61  pBt->xFreeSchema
cce20 28 70 42 74 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  (pBt->pSchema);.
cce30 20 20 7d 0a 20 20 73 71 6c 69 74 65 46 72 65 65    }.  sqliteFree
cce40 28 70 42 74 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  (pBt->pSchema);.
cce50 20 20 73 71 6c 69 74 65 46 72 65 65 28 70 42 74    sqliteFree(pBt
cce60 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
cce70 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
cce80 43 68 61 6e 67 65 20 74 68 65 20 62 75 73 79 20  Change the busy 
cce90 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b  handler callback
ccea0 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51   function..*/.SQ
cceb0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
ccec0 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74   sqlite3BtreeSet
cced0 42 75 73 79 48 61 6e 64 6c 65 72 28 42 74 72 65  BusyHandler(Btre
ccee0 65 20 2a 70 2c 20 42 75 73 79 48 61 6e 64 6c 65  e *p, BusyHandle
ccef0 72 20 2a 70 48 61 6e 64 6c 65 72 29 7b 0a 20 20  r *pHandler){.  
ccf00 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
ccf10 70 2d 3e 70 42 74 3b 0a 20 20 70 42 74 2d 3e 70  p->pBt;.  pBt->p
ccf20 42 75 73 79 48 61 6e 64 6c 65 72 20 3d 20 70 48  BusyHandler = pH
ccf30 61 6e 64 6c 65 72 3b 0a 20 20 73 71 6c 69 74 65  andler;.  sqlite
ccf40 33 50 61 67 65 72 53 65 74 42 75 73 79 68 61 6e  3PagerSetBusyhan
ccf50 64 6c 65 72 28 70 42 74 2d 3e 70 50 61 67 65 72  dler(pBt->pPager
ccf60 2c 20 70 48 61 6e 64 6c 65 72 29 3b 0a 20 20 72  , pHandler);.  r
ccf70 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
ccf80 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65  .}../*.** Change
ccf90 20 74 68 65 20 6c 69 6d 69 74 20 6f 6e 20 74 68   the limit on th
ccfa0 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  e number of page
ccfb0 73 20 61 6c 6c 6f 77 65 64 20 69 6e 20 74 68 65  s allowed in the
ccfc0 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68   cache..**.** Th
ccfd0 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  e maximum number
ccfe0 20 6f 66 20 63 61 63 68 65 20 70 61 67 65 73 20   of cache pages 
ccff0 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 61 62  is set to the ab
cd000 73 6f 6c 75 74 65 0a 2a 2a 20 76 61 6c 75 65 20  solute.** value 
cd010 6f 66 20 6d 78 50 61 67 65 2e 20 20 49 66 20 6d  of mxPage.  If m
cd020 78 50 61 67 65 20 69 73 20 6e 65 67 61 74 69 76  xPage is negativ
cd030 65 2c 20 74 68 65 20 70 61 67 65 72 20 77 69 6c  e, the pager wil
cd040 6c 0a 2a 2a 20 6f 70 65 72 61 74 65 20 61 73 79  l.** operate asy
cd050 6e 63 68 72 6f 6e 6f 75 73 6c 79 20 2d 20 69 74  nchronously - it
cd060 20 77 69 6c 6c 20 6e 6f 74 20 73 74 6f 70 20 74   will not stop t
cd070 6f 20 64 6f 20 66 73 79 6e 63 28 29 73 0a 2a 2a  o do fsync()s.**
cd080 20 74 6f 20 69 6e 73 75 72 65 20 64 61 74 61 20   to insure data 
cd090 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68  is written to th
cd0a0 65 20 64 69 73 6b 20 73 75 72 66 61 63 65 20 62  e disk surface b
cd0b0 65 66 6f 72 65 0a 2a 2a 20 63 6f 6e 74 69 6e 75  efore.** continu
cd0c0 69 6e 67 2e 20 20 54 72 61 6e 73 61 63 74 69 6f  ing.  Transactio
cd0d0 6e 73 20 73 74 69 6c 6c 20 77 6f 72 6b 20 69 66  ns still work if
cd0e0 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73 20   synchronous is 
cd0f0 6f 66 66 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 20  off,.** and the 
cd100 64 61 74 61 62 61 73 65 20 63 61 6e 6e 6f 74 20  database cannot 
cd110 62 65 20 63 6f 72 72 75 70 74 65 64 20 69 66 20  be corrupted if 
cd120 74 68 69 73 20 70 72 6f 67 72 61 6d 0a 2a 2a 20  this program.** 
cd130 63 72 61 73 68 65 73 2e 20 20 42 75 74 20 69 66  crashes.  But if
cd140 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73   the operating s
cd150 79 73 74 65 6d 20 63 72 61 73 68 65 73 20 6f 72  ystem crashes or
cd160 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 61 6e 20   there is.** an 
cd170 61 62 72 75 70 74 20 70 6f 77 65 72 20 66 61 69  abrupt power fai
cd180 6c 75 72 65 20 77 68 65 6e 20 73 79 6e 63 68 72  lure when synchr
cd190 6f 6e 6f 75 73 20 69 73 20 6f 66 66 2c 20 74 68  onous is off, th
cd1a0 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f  e database.** co
cd1b0 75 6c 64 20 62 65 20 6c 65 66 74 20 69 6e 20 61  uld be left in a
cd1c0 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 61  n inconsistent a
cd1d0 6e 64 20 75 6e 72 65 63 6f 76 65 72 61 62 6c 65  nd unrecoverable
cd1e0 20 73 74 61 74 65 2e 0a 2a 2a 20 53 79 6e 63 68   state..** Synch
cd1f0 72 6f 6e 6f 75 73 20 69 73 20 6f 6e 20 62 79 20  ronous is on by 
cd200 64 65 66 61 75 6c 74 20 73 6f 20 64 61 74 61 62  default so datab
cd210 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 69  ase corruption i
cd220 73 20 6e 6f 74 0a 2a 2a 20 6e 6f 72 6d 61 6c 6c  s not.** normall
cd230 79 20 61 20 77 6f 72 72 79 2e 0a 2a 2f 0a 53 51  y a worry..*/.SQ
cd240 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
cd250 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74   sqlite3BtreeSet
cd260 43 61 63 68 65 53 69 7a 65 28 42 74 72 65 65 20  CacheSize(Btree 
cd270 2a 70 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b  *p, int mxPage){
cd280 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
cd290 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 73 71 6c   = p->pBt;.  sql
cd2a0 69 74 65 33 50 61 67 65 72 53 65 74 43 61 63 68  ite3PagerSetCach
cd2b0 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65  esize(pBt->pPage
cd2c0 72 2c 20 6d 78 50 61 67 65 29 3b 0a 20 20 72 65  r, mxPage);.  re
cd2d0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
cd2e0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20  }../*.** Change 
cd2f0 74 68 65 20 77 61 79 20 64 61 74 61 20 69 73 20  the way data is 
cd300 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b 20 69  synced to disk i
cd310 6e 20 6f 72 64 65 72 20 74 6f 20 69 6e 63 72 65  n order to incre
cd320 61 73 65 20 6f 72 20 64 65 63 72 65 61 73 65 0a  ase or decrease.
cd330 2a 2a 20 68 6f 77 20 77 65 6c 6c 20 74 68 65 20  ** how well the 
cd340 64 61 74 61 62 61 73 65 20 72 65 73 69 73 74 73  database resists
cd350 20 64 61 6d 61 67 65 20 64 75 65 20 74 6f 20 4f   damage due to O
cd360 53 20 63 72 61 73 68 65 73 20 61 6e 64 20 70 6f  S crashes and po
cd370 77 65 72 0a 2a 2a 20 66 61 69 6c 75 72 65 73 2e  wer.** failures.
cd380 20 20 4c 65 76 65 6c 20 31 20 69 73 20 74 68 65    Level 1 is the
cd390 20 73 61 6d 65 20 61 73 20 61 73 79 6e 63 68 72   same as asynchr
cd3a0 6f 6e 6f 75 73 20 28 6e 6f 20 73 79 6e 63 73 28  onous (no syncs(
cd3b0 29 20 6f 63 63 75 72 20 61 6e 64 0a 2a 2a 20 74  ) occur and.** t
cd3c0 68 65 72 65 20 69 73 20 61 20 68 69 67 68 20 70  here is a high p
cd3d0 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 64 61  robability of da
cd3e0 6d 61 67 65 29 20 20 4c 65 76 65 6c 20 32 20 69  mage)  Level 2 i
cd3f0 73 20 74 68 65 20 64 65 66 61 75 6c 74 2e 20 20  s the default.  
cd400 54 68 65 72 65 0a 2a 2a 20 69 73 20 61 20 76 65  There.** is a ve
cd410 72 79 20 6c 6f 77 20 62 75 74 20 6e 6f 6e 2d 7a  ry low but non-z
cd420 65 72 6f 20 70 72 6f 62 61 62 69 6c 69 74 79 20  ero probability 
cd430 6f 66 20 64 61 6d 61 67 65 2e 20 20 4c 65 76 65  of damage.  Leve
cd440 6c 20 33 20 72 65 64 75 63 65 73 20 74 68 65 0a  l 3 reduces the.
cd450 2a 2a 20 70 72 6f 62 61 62 69 6c 69 74 79 20 6f  ** probability o
cd460 66 20 64 61 6d 61 67 65 20 74 6f 20 6e 65 61 72  f damage to near
cd470 20 7a 65 72 6f 20 62 75 74 20 77 69 74 68 20 61   zero but with a
cd480 20 77 72 69 74 65 20 70 65 72 66 6f 72 6d 61 6e   write performan
cd490 63 65 20 72 65 64 75 63 74 69 6f 6e 2e 0a 2a 2f  ce reduction..*/
cd4a0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
cd4b0 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d  OMIT_PAGER_PRAGM
cd4c0 41 53 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  AS.SQLITE_PRIVAT
cd4d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
cd4e0 65 65 53 65 74 53 61 66 65 74 79 4c 65 76 65 6c  eeSetSafetyLevel
cd4f0 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6c  (Btree *p, int l
cd500 65 76 65 6c 2c 20 69 6e 74 20 66 75 6c 6c 53 79  evel, int fullSy
cd510 6e 63 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20  nc){.  BtShared 
cd520 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20  *pBt = p->pBt;. 
cd530 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74   sqlite3PagerSet
cd540 53 61 66 65 74 79 4c 65 76 65 6c 28 70 42 74 2d  SafetyLevel(pBt-
cd550 3e 70 50 61 67 65 72 2c 20 6c 65 76 65 6c 2c 20  >pPager, level, 
cd560 66 75 6c 6c 53 79 6e 63 29 3b 0a 20 20 72 65 74  fullSync);.  ret
cd570 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
cd580 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52  .#endif../*.** R
cd590 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68  eturn TRUE if th
cd5a0 65 20 67 69 76 65 6e 20 62 74 72 65 65 20 69 73  e given btree is
cd5b0 20 73 65 74 20 74 6f 20 73 61 66 65 74 79 20 6c   set to safety l
cd5c0 65 76 65 6c 20 31 2e 20 20 49 6e 20 6f 74 68 65  evel 1.  In othe
cd5d0 72 0a 2a 2a 20 77 6f 72 64 73 2c 20 72 65 74 75  r.** words, retu
cd5e0 72 6e 20 54 52 55 45 20 69 66 20 6e 6f 20 73 79  rn TRUE if no sy
cd5f0 6e 63 28 29 20 6f 63 63 75 72 73 20 6f 6e 20 74  nc() occurs on t
cd600 68 65 20 64 69 73 6b 20 66 69 6c 65 73 2e 0a 2a  he disk files..*
cd610 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
cd620 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
cd630 65 53 79 6e 63 44 69 73 61 62 6c 65 64 28 42 74  eSyncDisabled(Bt
cd640 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61  ree *p){.  BtSha
cd650 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
cd660 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74  t;.  assert( pBt
cd670 20 26 26 20 70 42 74 2d 3e 70 50 61 67 65 72 20   && pBt->pPager 
cd680 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  );.  return sqli
cd690 74 65 33 50 61 67 65 72 4e 6f 73 79 6e 63 28 70  te3PagerNosync(p
cd6a0 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 7d 0a 0a  Bt->pPager);.}..
cd6b0 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
cd6c0 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50  ITE_OMIT_PAGER_P
cd6d0 52 41 47 4d 41 53 29 20 7c 7c 20 21 64 65 66 69  RAGMAS) || !defi
cd6e0 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
cd6f0 56 41 43 55 55 4d 29 0a 2f 2a 0a 2a 2a 20 43 68  VACUUM)./*.** Ch
cd700 61 6e 67 65 20 74 68 65 20 64 65 66 61 75 6c 74  ange the default
cd710 20 70 61 67 65 73 20 73 69 7a 65 20 61 6e 64 20   pages size and 
cd720 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65  the number of re
cd730 73 65 72 76 65 64 20 62 79 74 65 73 20 70 65 72  served bytes per
cd740 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   page..**.** The
cd750 20 70 61 67 65 20 73 69 7a 65 20 6d 75 73 74 20   page size must 
cd760 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 32 20  be a power of 2 
cd770 62 65 74 77 65 65 6e 20 35 31 32 20 61 6e 64 20  between 512 and 
cd780 36 35 35 33 36 2e 20 20 49 66 20 74 68 65 20 70  65536.  If the p
cd790 61 67 65 0a 2a 2a 20 73 69 7a 65 20 73 75 70 70  age.** size supp
cd7a0 6c 69 65 64 20 64 6f 65 73 20 6e 6f 74 20 6d 65  lied does not me
cd7b0 65 74 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69  et this constrai
cd7c0 6e 74 20 74 68 65 6e 20 74 68 65 20 70 61 67 65  nt then the page
cd7d0 20 73 69 7a 65 20 69 73 20 6e 6f 74 0a 2a 2a 20   size is not.** 
cd7e0 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 50  changed..**.** P
cd7f0 61 67 65 20 73 69 7a 65 73 20 61 72 65 20 63 6f  age sizes are co
cd800 6e 73 74 72 61 69 6e 65 64 20 74 6f 20 62 65 20  nstrained to be 
cd810 61 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 73  a power of two s
cd820 6f 20 74 68 61 74 20 74 68 65 20 72 65 67 69 6f  o that the regio
cd830 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 64 61 74 61  n.** of the data
cd840 62 61 73 65 20 66 69 6c 65 20 75 73 65 64 20 66  base file used f
cd850 6f 72 20 6c 6f 63 6b 69 6e 67 20 28 62 65 67 69  or locking (begi
cd860 6e 6e 69 6e 67 20 61 74 20 50 45 4e 44 49 4e 47  nning at PENDING
cd870 5f 42 59 54 45 2c 0a 2a 2a 20 74 68 65 20 66 69  _BYTE,.** the fi
cd880 72 73 74 20 62 79 74 65 20 70 61 73 74 20 74 68  rst byte past th
cd890 65 20 31 47 42 20 62 6f 75 6e 64 61 72 79 2c 20  e 1GB boundary, 
cd8a0 30 78 34 30 30 30 30 30 30 30 29 20 6e 65 65 64  0x40000000) need
cd8b0 73 20 74 6f 20 6f 63 63 75 72 0a 2a 2a 20 61 74  s to occur.** at
cd8c0 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f   the beginning o
cd8d0 66 20 61 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20  f a page..**.** 
cd8e0 49 66 20 70 61 72 61 6d 65 74 65 72 20 6e 52 65  If parameter nRe
cd8f0 73 65 72 76 65 20 69 73 20 6c 65 73 73 20 74 68  serve is less th
cd900 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68  an zero, then th
cd910 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 65  e number of rese
cd920 72 76 65 64 0a 2a 2a 20 62 79 74 65 73 20 70 65  rved.** bytes pe
cd930 72 20 70 61 67 65 20 69 73 20 6c 65 66 74 20 75  r page is left u
cd940 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c  nchanged..*/.SQL
cd950 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
cd960 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 50  sqlite3BtreeSetP
cd970 61 67 65 53 69 7a 65 28 42 74 72 65 65 20 2a 70  ageSize(Btree *p
cd980 2c 20 69 6e 74 20 70 61 67 65 53 69 7a 65 2c 20  , int pageSize, 
cd990 69 6e 74 20 6e 52 65 73 65 72 76 65 29 7b 0a 20  int nReserve){. 
cd9a0 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
cd9b0 20 70 2d 3e 70 42 74 3b 0a 20 20 69 66 28 20 70   p->pBt;.  if( p
cd9c0 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65  Bt->pageSizeFixe
cd9d0 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  d ){.    return 
cd9e0 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b  SQLITE_READONLY;
cd9f0 0a 20 20 7d 0a 20 20 69 66 28 20 6e 52 65 73 65  .  }.  if( nRese
cda00 72 76 65 3c 30 20 29 7b 0a 20 20 20 20 6e 52 65  rve<0 ){.    nRe
cda10 73 65 72 76 65 20 3d 20 70 42 74 2d 3e 70 61 67  serve = pBt->pag
cda20 65 53 69 7a 65 20 2d 20 70 42 74 2d 3e 75 73 61  eSize - pBt->usa
cda30 62 6c 65 53 69 7a 65 3b 0a 20 20 7d 0a 20 20 69  bleSize;.  }.  i
cda40 66 28 20 70 61 67 65 53 69 7a 65 3e 3d 35 31 32  f( pageSize>=512
cda50 20 26 26 20 70 61 67 65 53 69 7a 65 3c 3d 53 51   && pageSize<=SQ
cda60 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49  LITE_MAX_PAGE_SI
cda70 5a 45 20 26 26 0a 20 20 20 20 20 20 20 20 28 28  ZE &&.        ((
cda80 70 61 67 65 53 69 7a 65 2d 31 29 26 70 61 67 65  pageSize-1)&page
cda90 53 69 7a 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20  Size)==0 ){.    
cdaa0 61 73 73 65 72 74 28 20 28 70 61 67 65 53 69 7a  assert( (pageSiz
cdab0 65 20 26 20 37 29 3d 3d 30 20 29 3b 0a 20 20 20  e & 7)==0 );.   
cdac0 20 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e 70   assert( !pBt->p
cdad0 50 61 67 65 31 20 26 26 20 21 70 42 74 2d 3e 70  Page1 && !pBt->p
cdae0 43 75 72 73 6f 72 20 29 3b 0a 20 20 20 20 70 42  Cursor );.    pB
cdaf0 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 73 71  t->pageSize = sq
cdb00 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61 67  lite3PagerSetPag
cdb10 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65  esize(pBt->pPage
cdb20 72 2c 20 70 61 67 65 53 69 7a 65 29 3b 0a 20 20  r, pageSize);.  
cdb30 7d 0a 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 53  }.  pBt->usableS
cdb40 69 7a 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53  ize = pBt->pageS
cdb50 69 7a 65 20 2d 20 6e 52 65 73 65 72 76 65 3b 0a  ize - nReserve;.
cdb60 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
cdb70 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  OK;.}../*.** Ret
cdb80 75 72 6e 20 74 68 65 20 63 75 72 72 65 6e 74 6c  urn the currentl
cdb90 79 20 64 65 66 69 6e 65 64 20 70 61 67 65 20 73  y defined page s
cdba0 69 7a 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ize.*/.SQLITE_PR
cdbb0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
cdbc0 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69 7a  3BtreeGetPageSiz
cdbd0 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 72  e(Btree *p){.  r
cdbe0 65 74 75 72 6e 20 70 2d 3e 70 42 74 2d 3e 70 61  eturn p->pBt->pa
cdbf0 67 65 53 69 7a 65 3b 0a 7d 0a 53 51 4c 49 54 45  geSize;.}.SQLITE
cdc00 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
cdc10 69 74 65 33 42 74 72 65 65 47 65 74 52 65 73 65  ite3BtreeGetRese
cdc20 72 76 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20  rve(Btree *p){. 
cdc30 20 72 65 74 75 72 6e 20 70 2d 3e 70 42 74 2d 3e   return p->pBt->
cdc40 70 61 67 65 53 69 7a 65 20 2d 20 70 2d 3e 70 42  pageSize - p->pB
cdc50 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a 7d  t->usableSize;.}
cdc60 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
cdc70 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 63 6f 75  maximum page cou
cdc80 6e 74 20 66 6f 72 20 61 20 64 61 74 61 62 61 73  nt for a databas
cdc90 65 20 69 66 20 6d 78 50 61 67 65 20 69 73 20 70  e if mxPage is p
cdca0 6f 73 69 74 69 76 65 2e 0a 2a 2a 20 4e 6f 20 63  ositive..** No c
cdcb0 68 61 6e 67 65 73 20 61 72 65 20 6d 61 64 65 20  hanges are made 
cdcc0 69 66 20 6d 78 50 61 67 65 20 69 73 20 30 20 6f  if mxPage is 0 o
cdcd0 72 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 20 52  r negative..** R
cdce0 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65  egardless of the
cdcf0 20 76 61 6c 75 65 20 6f 66 20 6d 78 50 61 67 65   value of mxPage
cdd00 2c 20 72 65 74 75 72 6e 20 74 68 65 20 6d 61 78  , return the max
cdd10 69 6d 75 6d 20 70 61 67 65 20 63 6f 75 6e 74 2e  imum page count.
cdd20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
cdd30 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
cdd40 72 65 65 4d 61 78 50 61 67 65 43 6f 75 6e 74 28  reeMaxPageCount(
cdd50 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6d 78  Btree *p, int mx
cdd60 50 61 67 65 29 7b 0a 20 20 72 65 74 75 72 6e 20  Page){.  return 
cdd70 73 71 6c 69 74 65 33 50 61 67 65 72 4d 61 78 50  sqlite3PagerMaxP
cdd80 61 67 65 43 6f 75 6e 74 28 70 2d 3e 70 42 74 2d  ageCount(p->pBt-
cdd90 3e 70 50 61 67 65 72 2c 20 6d 78 50 61 67 65 29  >pPager, mxPage)
cdda0 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64  ;.}.#endif /* !d
cddb0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
cddc0 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53  IT_PAGER_PRAGMAS
cddd0 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51  ) || !defined(SQ
cdde0 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d  LITE_OMIT_VACUUM
cddf0 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e  ) */../*.** Chan
cde00 67 65 20 74 68 65 20 27 61 75 74 6f 2d 76 61 63  ge the 'auto-vac
cde10 75 75 6d 27 20 70 72 6f 70 65 72 74 79 20 6f 66  uum' property of
cde20 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 49   the database. I
cde30 66 20 74 68 65 20 27 61 75 74 6f 56 61 63 75 75  f the 'autoVacuu
cde40 6d 27 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20  m'.** parameter 
cde50 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  is non-zero, the
cde60 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d 6f  n auto-vacuum mo
cde70 64 65 20 69 73 20 65 6e 61 62 6c 65 64 2e 20 49  de is enabled. I
cde80 66 20 7a 65 72 6f 2c 20 69 74 0a 2a 2a 20 69 73  f zero, it.** is
cde90 20 64 69 73 61 62 6c 65 64 2e 20 54 68 65 20 64   disabled. The d
cdea0 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72  efault value for
cdeb0 20 74 68 65 20 61 75 74 6f 2d 76 61 63 75 75 6d   the auto-vacuum
cdec0 20 70 72 6f 70 65 72 74 79 20 69 73 20 0a 2a 2a   property is .**
cded0 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74   determined by t
cdee0 68 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  he SQLITE_DEFAUL
cdef0 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 6d 61 63  T_AUTOVACUUM mac
cdf00 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ro..*/.SQLITE_PR
cdf10 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
cdf20 33 42 74 72 65 65 53 65 74 41 75 74 6f 56 61 63  3BtreeSetAutoVac
cdf30 75 75 6d 28 42 74 72 65 65 20 2a 70 2c 20 69 6e  uum(Btree *p, in
cdf40 74 20 61 75 74 6f 56 61 63 75 75 6d 29 7b 0a 23  t autoVacuum){.#
cdf50 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
cdf60 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 72  T_AUTOVACUUM.  r
cdf70 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41  eturn SQLITE_REA
cdf80 44 4f 4e 4c 59 3b 0a 23 65 6c 73 65 0a 20 20 42  DONLY;.#else.  B
cdf90 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
cdfa0 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 20 61 76 20  ->pBt;.  int av 
cdfb0 3d 20 28 61 75 74 6f 56 61 63 75 75 6d 3f 31 3a  = (autoVacuum?1:
cdfc0 30 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 70  0);.  if( pBt->p
cdfd0 61 67 65 53 69 7a 65 46 69 78 65 64 20 26 26 20  ageSizeFixed && 
cdfe0 61 76 21 3d 70 42 74 2d 3e 61 75 74 6f 56 61 63  av!=pBt->autoVac
cdff0 75 75 6d 20 29 7b 0a 20 20 20 20 72 65 74 75 72  uum ){.    retur
ce000 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c  n SQLITE_READONL
ce010 59 3b 0a 20 20 7d 0a 20 20 70 42 74 2d 3e 61 75  Y;.  }.  pBt->au
ce020 74 6f 56 61 63 75 75 6d 20 3d 20 61 76 3b 0a 20  toVacuum = av;. 
ce030 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
ce040 4b 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a  K;.#endif.}../*.
ce050 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 76 61  ** Return the va
ce060 6c 75 65 20 6f 66 20 74 68 65 20 27 61 75 74 6f  lue of the 'auto
ce070 2d 76 61 63 75 75 6d 27 20 70 72 6f 70 65 72 74  -vacuum' propert
ce080 79 2e 20 49 66 20 61 75 74 6f 2d 76 61 63 75 75  y. If auto-vacuu
ce090 6d 20 69 73 20 0a 2a 2a 20 65 6e 61 62 6c 65 64  m is .** enabled
ce0a0 20 31 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20   1 is returned. 
ce0b0 4f 74 68 65 72 77 69 73 65 20 30 2e 0a 2a 2f 0a  Otherwise 0..*/.
ce0c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
ce0d0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47  nt sqlite3BtreeG
ce0e0 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72  etAutoVacuum(Btr
ce0f0 65 65 20 2a 70 29 7b 0a 23 69 66 64 65 66 20 53  ee *p){.#ifdef S
ce100 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
ce110 41 43 55 55 4d 0a 20 20 72 65 74 75 72 6e 20 42  ACUUM.  return B
ce120 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f  TREE_AUTOVACUUM_
ce130 4e 4f 4e 45 3b 0a 23 65 6c 73 65 0a 20 20 72 65  NONE;.#else.  re
ce140 74 75 72 6e 20 28 0a 20 20 20 20 28 21 70 2d 3e  turn (.    (!p->
ce150 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29  pBt->autoVacuum)
ce160 3f 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55  ?BTREE_AUTOVACUU
ce170 4d 5f 4e 4f 4e 45 3a 0a 20 20 20 20 28 21 70 2d  M_NONE:.    (!p-
ce180 3e 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d  >pBt->incrVacuum
ce190 29 3f 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55  )?BTREE_AUTOVACU
ce1a0 55 4d 5f 46 55 4c 4c 3a 0a 20 20 20 20 42 54 52  UM_FULL:.    BTR
ce1b0 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e  EE_AUTOVACUUM_IN
ce1c0 43 52 0a 20 20 29 3b 0a 23 65 6e 64 69 66 0a 7d  CR.  );.#endif.}
ce1d0 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 72  .../*.** Get a r
ce1e0 65 66 65 72 65 6e 63 65 20 74 6f 20 70 50 61 67  eference to pPag
ce1f0 65 31 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  e1 of the databa
ce200 73 65 20 66 69 6c 65 2e 20 20 54 68 69 73 20 77  se file.  This w
ce210 69 6c 6c 0a 2a 2a 20 61 6c 73 6f 20 61 63 71 75  ill.** also acqu
ce220 69 72 65 20 61 20 72 65 61 64 6c 6f 63 6b 20 6f  ire a readlock o
ce230 6e 20 74 68 61 74 20 66 69 6c 65 2e 0a 2a 2a 0a  n that file..**.
ce240 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  ** SQLITE_OK is 
ce250 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 75 63 63  returned on succ
ce260 65 73 73 2e 20 20 49 66 20 74 68 65 20 66 69 6c  ess.  If the fil
ce270 65 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 77 65  e is not a.** we
ce280 6c 6c 2d 66 6f 72 6d 65 64 20 64 61 74 61 62 61  ll-formed databa
ce290 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 53 51  se file, then SQ
ce2a0 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 69 73 20  LITE_CORRUPT is 
ce2b0 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 53 51 4c  returned..** SQL
ce2c0 49 54 45 5f 42 55 53 59 20 69 73 20 72 65 74 75  ITE_BUSY is retu
ce2d0 72 6e 65 64 20 69 66 20 74 68 65 20 64 61 74 61  rned if the data
ce2e0 62 61 73 65 20 69 73 20 6c 6f 63 6b 65 64 2e 20  base is locked. 
ce2f0 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a   SQLITE_NOMEM.**
ce300 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
ce310 77 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 6d 65  we run out of me
ce320 6d 6f 72 79 2e 20 0a 2a 2f 0a 73 74 61 74 69 63  mory. .*/.static
ce330 20 69 6e 74 20 6c 6f 63 6b 42 74 72 65 65 28 42   int lockBtree(B
ce340 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20  tShared *pBt){. 
ce350 20 69 6e 74 20 72 63 2c 20 70 61 67 65 53 69 7a   int rc, pageSiz
ce360 65 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  e;.  MemPage *pP
ce370 61 67 65 31 3b 0a 20 20 69 66 28 20 70 42 74 2d  age1;.  if( pBt-
ce380 3e 70 50 61 67 65 31 20 29 20 72 65 74 75 72 6e  >pPage1 ) return
ce390 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 72 63   SQLITE_OK;.  rc
ce3a0 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47   = sqlite3BtreeG
ce3b0 65 74 50 61 67 65 28 70 42 74 2c 20 31 2c 20 26  etPage(pBt, 1, &
ce3c0 70 50 61 67 65 31 2c 20 30 29 3b 0a 20 20 69 66  pPage1, 0);.  if
ce3d0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
ce3e0 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 0a  ) return rc;.  .
ce3f0 0a 20 20 2f 2a 20 44 6f 20 73 6f 6d 65 20 63 68  .  /* Do some ch
ce400 65 63 6b 69 6e 67 20 74 6f 20 68 65 6c 70 20 69  ecking to help i
ce410 6e 73 75 72 65 20 74 68 65 20 66 69 6c 65 20 77  nsure the file w
ce420 65 20 6f 70 65 6e 65 64 20 72 65 61 6c 6c 79 20  e opened really 
ce430 69 73 0a 20 20 2a 2a 20 61 20 76 61 6c 69 64 20  is.  ** a valid 
ce440 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 0a  database file. .
ce450 20 20 2a 2f 0a 20 20 72 63 20 3d 20 53 51 4c 49    */.  rc = SQLI
ce460 54 45 5f 4e 4f 54 41 44 42 3b 0a 20 20 69 66 28  TE_NOTADB;.  if(
ce470 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
ce480 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67  ecount(pBt->pPag
ce490 65 72 29 3e 30 20 29 7b 0a 20 20 20 20 75 38 20  er)>0 ){.    u8 
ce4a0 2a 70 61 67 65 31 20 3d 20 70 50 61 67 65 31 2d  *page1 = pPage1-
ce4b0 3e 61 44 61 74 61 3b 0a 20 20 20 20 69 66 28 20  >aData;.    if( 
ce4c0 6d 65 6d 63 6d 70 28 70 61 67 65 31 2c 20 7a 4d  memcmp(page1, zM
ce4d0 61 67 69 63 48 65 61 64 65 72 2c 20 31 36 29 21  agicHeader, 16)!
ce4e0 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  =0 ){.      goto
ce4f0 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c   page1_init_fail
ce500 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ed;.    }.    if
ce510 28 20 70 61 67 65 31 5b 31 38 5d 3e 31 20 29 7b  ( page1[18]>1 ){
ce520 0a 20 20 20 20 20 20 70 42 74 2d 3e 72 65 61 64  .      pBt->read
ce530 4f 6e 6c 79 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  Only = 1;.    }.
ce540 20 20 20 20 69 66 28 20 70 61 67 65 31 5b 31 39      if( page1[19
ce550 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  ]>1 ){.      got
ce560 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69  o page1_init_fai
ce570 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  led;.    }.    p
ce580 61 67 65 53 69 7a 65 20 3d 20 67 65 74 32 62 79  ageSize = get2by
ce590 74 65 28 26 70 61 67 65 31 5b 31 36 5d 29 3b 0a  te(&page1[16]);.
ce5a0 20 20 20 20 69 66 28 20 28 28 70 61 67 65 53 69      if( ((pageSi
ce5b0 7a 65 2d 31 29 26 70 61 67 65 53 69 7a 65 29 21  ze-1)&pageSize)!
ce5c0 3d 30 20 7c 7c 20 70 61 67 65 53 69 7a 65 3c 35  =0 || pageSize<5
ce5d0 31 32 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  12 ){.      goto
ce5e0 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c   page1_init_fail
ce5f0 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  ed;.    }.    as
ce600 73 65 72 74 28 20 28 70 61 67 65 53 69 7a 65 20  sert( (pageSize 
ce610 26 20 37 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70  & 7)==0 );.    p
ce620 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 70  Bt->pageSize = p
ce630 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 70 42 74  ageSize;.    pBt
ce640 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70  ->usableSize = p
ce650 61 67 65 53 69 7a 65 20 2d 20 70 61 67 65 31 5b  ageSize - page1[
ce660 32 30 5d 3b 0a 20 20 20 20 69 66 28 20 70 42 74  20];.    if( pBt
ce670 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3c 35 30 30  ->usableSize<500
ce680 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70   ){.      goto p
ce690 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64  age1_init_failed
ce6a0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74 2d  ;.    }.    pBt-
ce6b0 3e 6d 61 78 45 6d 62 65 64 46 72 61 63 20 3d 20  >maxEmbedFrac = 
ce6c0 70 61 67 65 31 5b 32 31 5d 3b 0a 20 20 20 20 70  page1[21];.    p
ce6d0 42 74 2d 3e 6d 69 6e 45 6d 62 65 64 46 72 61 63  Bt->minEmbedFrac
ce6e0 20 3d 20 70 61 67 65 31 5b 32 32 5d 3b 0a 20 20   = page1[22];.  
ce6f0 20 20 70 42 74 2d 3e 6d 69 6e 4c 65 61 66 46 72    pBt->minLeafFr
ce700 61 63 20 3d 20 70 61 67 65 31 5b 32 33 5d 3b 0a  ac = page1[23];.
ce710 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
ce720 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
ce730 20 20 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75     pBt->autoVacu
ce740 75 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28 26  um = (get4byte(&
ce750 70 61 67 65 31 5b 33 36 20 2b 20 34 2a 34 5d 29  page1[36 + 4*4])
ce760 3f 31 3a 30 29 3b 0a 20 20 20 20 70 42 74 2d 3e  ?1:0);.    pBt->
ce770 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 67 65  incrVacuum = (ge
ce780 74 34 62 79 74 65 28 26 70 61 67 65 31 5b 33 36  t4byte(&page1[36
ce790 20 2b 20 37 2a 34 5d 29 3f 31 3a 30 29 3b 0a 23   + 7*4])?1:0);.#
ce7a0 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 2f 2a 20  endif.  }..  /* 
ce7b0 6d 61 78 4c 6f 63 61 6c 20 69 73 20 74 68 65 20  maxLocal is the 
ce7c0 6d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f  maximum amount o
ce7d0 66 20 70 61 79 6c 6f 61 64 20 74 6f 20 73 74 6f  f payload to sto
ce7e0 72 65 20 6c 6f 63 61 6c 6c 79 20 66 6f 72 0a 20  re locally for. 
ce7f0 20 2a 2a 20 61 20 63 65 6c 6c 2e 20 20 4d 61 6b   ** a cell.  Mak
ce800 65 20 73 75 72 65 20 69 74 20 69 73 20 73 6d 61  e sure it is sma
ce810 6c 6c 20 65 6e 6f 75 67 68 20 73 6f 20 74 68 61  ll enough so tha
ce820 74 20 61 74 20 6c 65 61 73 74 20 6d 69 6e 46 61  t at least minFa
ce830 6e 6f 75 74 0a 20 20 2a 2a 20 63 65 6c 6c 73 20  nout.  ** cells 
ce840 63 61 6e 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20  can will fit on 
ce850 6f 6e 65 20 70 61 67 65 2e 20 20 57 65 20 61 73  one page.  We as
ce860 73 75 6d 65 20 61 20 31 30 2d 62 79 74 65 20 70  sume a 10-byte p
ce870 61 67 65 20 68 65 61 64 65 72 2e 0a 20 20 2a 2a  age header..  **
ce880 20 42 65 73 69 64 65 73 20 74 68 65 20 70 61 79   Besides the pay
ce890 6c 6f 61 64 2c 20 74 68 65 20 63 65 6c 6c 20 6d  load, the cell m
ce8a0 75 73 74 20 73 74 6f 72 65 3a 0a 20 20 2a 2a 20  ust store:.  ** 
ce8b0 20 20 20 20 32 2d 62 79 74 65 20 70 6f 69 6e 74      2-byte point
ce8c0 65 72 20 74 6f 20 74 68 65 20 63 65 6c 6c 0a 20  er to the cell. 
ce8d0 20 2a 2a 20 20 20 20 20 34 2d 62 79 74 65 20 63   **     4-byte c
ce8e0 68 69 6c 64 20 70 6f 69 6e 74 65 72 0a 20 20 2a  hild pointer.  *
ce8f0 2a 20 20 20 20 20 39 2d 62 79 74 65 20 6e 4b 65  *     9-byte nKe
ce900 79 20 76 61 6c 75 65 0a 20 20 2a 2a 20 20 20 20  y value.  **    
ce910 20 34 2d 62 79 74 65 20 6e 44 61 74 61 20 76 61   4-byte nData va
ce920 6c 75 65 0a 20 20 2a 2a 20 20 20 20 20 34 2d 62  lue.  **     4-b
ce930 79 74 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  yte overflow pag
ce940 65 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 53  e pointer.  ** S
ce950 6f 20 61 20 63 65 6c 6c 20 63 6f 6e 73 69 73 74  o a cell consist
ce960 73 20 6f 66 20 61 20 32 2d 62 79 74 65 20 70 6f  s of a 2-byte po
ce970 69 6e 65 72 2c 20 61 20 68 65 61 64 65 72 20 77  iner, a header w
ce980 68 69 63 68 20 69 73 20 61 73 20 6d 75 63 68 20  hich is as much 
ce990 61 73 0a 20 20 2a 2a 20 31 37 20 62 79 74 65 73  as.  ** 17 bytes
ce9a0 20 6c 6f 6e 67 2c 20 30 20 74 6f 20 4e 20 62 79   long, 0 to N by
ce9b0 74 65 73 20 6f 66 20 70 61 79 6c 6f 61 64 2c 20  tes of payload, 
ce9c0 61 6e 64 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20  and an optional 
ce9d0 34 20 62 79 74 65 20 6f 76 65 72 66 6c 6f 77 0a  4 byte overflow.
ce9e0 20 20 2a 2a 20 70 61 67 65 20 70 6f 69 6e 74 65    ** page pointe
ce9f0 72 2e 0a 20 20 2a 2f 0a 20 20 70 42 74 2d 3e 6d  r..  */.  pBt->m
cea00 61 78 4c 6f 63 61 6c 20 3d 20 28 70 42 74 2d 3e  axLocal = (pBt->
cea10 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 29 2a 70  usableSize-12)*p
cea20 42 74 2d 3e 6d 61 78 45 6d 62 65 64 46 72 61 63  Bt->maxEmbedFrac
cea30 2f 32 35 35 20 2d 20 32 33 3b 0a 20 20 70 42 74  /255 - 23;.  pBt
cea40 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 28 70 42  ->minLocal = (pB
cea50 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32  t->usableSize-12
cea60 29 2a 70 42 74 2d 3e 6d 69 6e 45 6d 62 65 64 46  )*pBt->minEmbedF
cea70 72 61 63 2f 32 35 35 20 2d 20 32 33 3b 0a 20 20  rac/255 - 23;.  
cea80 70 42 74 2d 3e 6d 61 78 4c 65 61 66 20 3d 20 70  pBt->maxLeaf = p
cea90 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d  Bt->usableSize -
ceaa0 20 33 35 3b 0a 20 20 70 42 74 2d 3e 6d 69 6e 4c   35;.  pBt->minL
ceab0 65 61 66 20 3d 20 28 70 42 74 2d 3e 75 73 61 62  eaf = (pBt->usab
ceac0 6c 65 53 69 7a 65 2d 31 32 29 2a 70 42 74 2d 3e  leSize-12)*pBt->
cead0 6d 69 6e 4c 65 61 66 46 72 61 63 2f 32 35 35 20  minLeafFrac/255 
ceae0 2d 20 32 33 3b 0a 20 20 69 66 28 20 70 42 74 2d  - 23;.  if( pBt-
ceaf0 3e 6d 69 6e 4c 6f 63 61 6c 3e 70 42 74 2d 3e 6d  >minLocal>pBt->m
ceb00 61 78 4c 6f 63 61 6c 20 7c 7c 20 70 42 74 2d 3e  axLocal || pBt->
ceb10 6d 61 78 4c 6f 63 61 6c 3c 30 20 29 7b 0a 20 20  maxLocal<0 ){.  
ceb20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69    goto page1_ini
ceb30 74 5f 66 61 69 6c 65 64 3b 0a 20 20 7d 0a 20 20  t_failed;.  }.  
ceb40 61 73 73 65 72 74 28 20 70 42 74 2d 3e 6d 61 78  assert( pBt->max
ceb50 4c 65 61 66 20 2b 20 32 33 20 3c 3d 20 4d 58 5f  Leaf + 23 <= MX_
ceb60 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 29  CELL_SIZE(pBt) )
ceb70 3b 0a 20 20 70 42 74 2d 3e 70 50 61 67 65 31 20  ;.  pBt->pPage1 
ceb80 3d 20 70 50 61 67 65 31 3b 0a 20 20 72 65 74 75  = pPage1;.  retu
ceb90 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 70  rn SQLITE_OK;..p
ceba0 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64  age1_init_failed
cebb0 3a 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28  :.  releasePage(
cebc0 70 50 61 67 65 31 29 3b 0a 20 20 70 42 74 2d 3e  pPage1);.  pBt->
cebd0 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 72 65  pPage1 = 0;.  re
cebe0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
cebf0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77  * This routine w
cec00 6f 72 6b 73 20 6c 69 6b 65 20 6c 6f 63 6b 42 74  orks like lockBt
cec10 72 65 65 28 29 20 65 78 63 65 70 74 20 74 68 61  ree() except tha
cec20 74 20 69 74 20 61 6c 73 6f 20 69 6e 76 6f 6b 65  t it also invoke
cec30 73 20 74 68 65 0a 2a 2a 20 62 75 73 79 20 63 61  s the.** busy ca
cec40 6c 6c 62 61 63 6b 20 69 66 20 74 68 65 72 65 20  llback if there 
cec50 69 73 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 69  is lock contenti
cec60 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  on..*/.static in
cec70 74 20 6c 6f 63 6b 42 74 72 65 65 57 69 74 68 52  t lockBtreeWithR
cec80 65 74 72 79 28 42 74 72 65 65 20 2a 70 52 65 66  etry(Btree *pRef
cec90 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
ceca0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70  LITE_OK;.  if( p
cecb0 52 65 66 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52  Ref->inTrans==TR
cecc0 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20  ANS_NONE ){.    
cecd0 75 38 20 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  u8 inTransaction
cece0 20 3d 20 70 52 65 66 2d 3e 70 42 74 2d 3e 69 6e   = pRef->pBt->in
cecf0 54 72 61 6e 73 61 63 74 69 6f 6e 3b 0a 20 20 20  Transaction;.   
ced00 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28   btreeIntegrity(
ced10 70 52 65 66 29 3b 0a 20 20 20 20 72 63 20 3d 20  pRef);.    rc = 
ced20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69  sqlite3BtreeBegi
ced30 6e 54 72 61 6e 73 28 70 52 65 66 2c 20 30 29 3b  nTrans(pRef, 0);
ced40 0a 20 20 20 20 70 52 65 66 2d 3e 70 42 74 2d 3e  .    pRef->pBt->
ced50 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20  inTransaction = 
ced60 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3b 0a 20  inTransaction;. 
ced70 20 20 20 70 52 65 66 2d 3e 69 6e 54 72 61 6e 73     pRef->inTrans
ced80 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20   = TRANS_NONE;. 
ced90 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
ceda0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 52  E_OK ){.      pR
cedb0 65 66 2d 3e 70 42 74 2d 3e 6e 54 72 61 6e 73 61  ef->pBt->nTransa
cedc0 63 74 69 6f 6e 2d 2d 3b 0a 20 20 20 20 7d 0a 20  ction--;.    }. 
cedd0 20 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 74     btreeIntegrit
cede0 79 28 70 52 65 66 29 3b 0a 20 20 7d 0a 20 20 72  y(pRef);.  }.  r
cedf0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 20 20 20 20  eturn rc;.}.    
cee00 20 20 20 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68     ../*.** If th
cee10 65 72 65 20 61 72 65 20 6e 6f 20 6f 75 74 73 74  ere are no outst
cee20 61 6e 64 69 6e 67 20 63 75 72 73 6f 72 73 20 61  anding cursors a
cee30 6e 64 20 77 65 20 61 72 65 20 6e 6f 74 20 69 6e  nd we are not in
cee40 20 74 68 65 20 6d 69 64 64 6c 65 0a 2a 2a 20 6f   the middle.** o
cee50 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  f a transaction 
cee60 62 75 74 20 74 68 65 72 65 20 69 73 20 61 20 72  but there is a r
cee70 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  ead lock on the 
cee80 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 0a 2a  database, then.*
cee90 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 75  * this routine u
ceea0 6e 72 65 66 73 20 74 68 65 20 66 69 72 73 74 20  nrefs the first 
ceeb0 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61  page of the data
ceec0 62 61 73 65 20 66 69 6c 65 20 77 68 69 63 68 20  base file which 
ceed0 0a 2a 2a 20 68 61 73 20 74 68 65 20 65 66 66 65  .** has the effe
ceee0 63 74 20 6f 66 20 72 65 6c 65 61 73 69 6e 67 20  ct of releasing 
ceef0 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a  the read lock..*
cef00 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72  *.** If there ar
cef10 65 20 61 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e  e any outstandin
cef20 67 20 63 75 72 73 6f 72 73 2c 20 74 68 69 73 20  g cursors, this 
cef30 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d  routine is a no-
cef40 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  op..**.** If the
cef50 72 65 20 69 73 20 61 20 74 72 61 6e 73 61 63 74  re is a transact
cef60 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 2c  ion in progress,
cef70 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
cef80 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61   a no-op..*/.sta
cef90 74 69 63 20 76 6f 69 64 20 75 6e 6c 6f 63 6b 42  tic void unlockB
cefa0 74 72 65 65 49 66 55 6e 75 73 65 64 28 42 74 53  treeIfUnused(BtS
cefb0 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 69  hared *pBt){.  i
cefc0 66 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61  f( pBt->inTransa
cefd0 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 4e 4f 4e  ction==TRANS_NON
cefe0 45 20 26 26 20 70 42 74 2d 3e 70 43 75 72 73 6f  E && pBt->pCurso
ceff0 72 3d 3d 30 20 26 26 20 70 42 74 2d 3e 70 50 61  r==0 && pBt->pPa
cf000 67 65 31 21 3d 30 20 29 7b 0a 20 20 20 20 69 66  ge1!=0 ){.    if
cf010 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65  ( sqlite3PagerRe
cf020 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67  fcount(pBt->pPag
cf030 65 72 29 3e 3d 31 20 29 7b 0a 20 20 20 20 20 20  er)>=1 ){.      
cf040 69 66 28 20 70 42 74 2d 3e 70 50 61 67 65 31 2d  if( pBt->pPage1-
cf050 3e 61 44 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20  >aData==0 ){.   
cf060 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50       MemPage *pP
cf070 61 67 65 20 3d 20 70 42 74 2d 3e 70 50 61 67 65  age = pBt->pPage
cf080 31 3b 0a 20 20 20 20 20 20 20 20 70 50 61 67 65  1;.        pPage
cf090 2d 3e 61 44 61 74 61 20 3d 20 26 28 28 75 38 2a  ->aData = &((u8*
cf0a0 29 70 50 61 67 65 29 5b 2d 70 42 74 2d 3e 70 61  )pPage)[-pBt->pa
cf0b0 67 65 53 69 7a 65 5d 3b 0a 20 20 20 20 20 20 20  geSize];.       
cf0c0 20 70 50 61 67 65 2d 3e 70 42 74 20 3d 20 70 42   pPage->pBt = pB
cf0d0 74 3b 0a 20 20 20 20 20 20 20 20 70 50 61 67 65  t;.        pPage
cf0e0 2d 3e 70 67 6e 6f 20 3d 20 31 3b 0a 20 20 20 20  ->pgno = 1;.    
cf0f0 20 20 7d 0a 20 20 20 20 20 20 72 65 6c 65 61 73    }.      releas
cf100 65 50 61 67 65 28 70 42 74 2d 3e 70 50 61 67 65  ePage(pBt->pPage
cf110 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42  1);.    }.    pB
cf120 74 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20  t->pPage1 = 0;. 
cf130 20 20 20 70 42 74 2d 3e 69 6e 53 74 6d 74 20 3d     pBt->inStmt =
cf140 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a   0;.  }.}../*.**
cf150 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 64 61   Create a new da
cf160 74 61 62 61 73 65 20 62 79 20 69 6e 69 74 69 61  tabase by initia
cf170 6c 69 7a 69 6e 67 20 74 68 65 20 66 69 72 73 74  lizing the first
cf180 20 70 61 67 65 20 6f 66 20 74 68 65 0a 2a 2a 20   page of the.** 
cf190 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  file..*/.static 
cf1a0 69 6e 74 20 6e 65 77 44 61 74 61 62 61 73 65 28  int newDatabase(
cf1b0 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a  BtShared *pBt){.
cf1c0 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 31 3b 0a    MemPage *pP1;.
cf1d0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
cf1e0 2a 64 61 74 61 3b 0a 20 20 69 6e 74 20 72 63 3b  *data;.  int rc;
cf1f0 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 50 61  .  if( sqlite3Pa
cf200 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74  gerPagecount(pBt
cf210 2d 3e 70 50 61 67 65 72 29 3e 30 20 29 20 72 65  ->pPager)>0 ) re
cf220 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
cf230 20 20 70 50 31 20 3d 20 70 42 74 2d 3e 70 50 61    pP1 = pBt->pPa
cf240 67 65 31 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ge1;.  assert( p
cf250 50 31 21 3d 30 20 29 3b 0a 20 20 64 61 74 61 20  P1!=0 );.  data 
cf260 3d 20 70 50 31 2d 3e 61 44 61 74 61 3b 0a 20 20  = pP1->aData;.  
cf270 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
cf280 72 57 72 69 74 65 28 70 50 31 2d 3e 70 44 62 50  rWrite(pP1->pDbP
cf290 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29  age);.  if( rc )
cf2a0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 6d 65   return rc;.  me
cf2b0 6d 63 70 79 28 64 61 74 61 2c 20 7a 4d 61 67 69  mcpy(data, zMagi
cf2c0 63 48 65 61 64 65 72 2c 20 73 69 7a 65 6f 66 28  cHeader, sizeof(
cf2d0 7a 4d 61 67 69 63 48 65 61 64 65 72 29 29 3b 0a  zMagicHeader));.
cf2e0 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66    assert( sizeof
cf2f0 28 7a 4d 61 67 69 63 48 65 61 64 65 72 29 3d 3d  (zMagicHeader)==
cf300 31 36 20 29 3b 0a 20 20 70 75 74 32 62 79 74 65  16 );.  put2byte
cf310 28 26 64 61 74 61 5b 31 36 5d 2c 20 70 42 74 2d  (&data[16], pBt-
cf320 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 64 61  >pageSize);.  da
cf330 74 61 5b 31 38 5d 20 3d 20 31 3b 0a 20 20 64 61  ta[18] = 1;.  da
cf340 74 61 5b 31 39 5d 20 3d 20 31 3b 0a 20 20 64 61  ta[19] = 1;.  da
cf350 74 61 5b 32 30 5d 20 3d 20 70 42 74 2d 3e 70 61  ta[20] = pBt->pa
cf360 67 65 53 69 7a 65 20 2d 20 70 42 74 2d 3e 75 73  geSize - pBt->us
cf370 61 62 6c 65 53 69 7a 65 3b 0a 20 20 64 61 74 61  ableSize;.  data
cf380 5b 32 31 5d 20 3d 20 70 42 74 2d 3e 6d 61 78 45  [21] = pBt->maxE
cf390 6d 62 65 64 46 72 61 63 3b 0a 20 20 64 61 74 61  mbedFrac;.  data
cf3a0 5b 32 32 5d 20 3d 20 70 42 74 2d 3e 6d 69 6e 45  [22] = pBt->minE
cf3b0 6d 62 65 64 46 72 61 63 3b 0a 20 20 64 61 74 61  mbedFrac;.  data
cf3c0 5b 32 33 5d 20 3d 20 70 42 74 2d 3e 6d 69 6e 4c  [23] = pBt->minL
cf3d0 65 61 66 46 72 61 63 3b 0a 20 20 6d 65 6d 73 65  eafFrac;.  memse
cf3e0 74 28 26 64 61 74 61 5b 32 34 5d 2c 20 30 2c 20  t(&data[24], 0, 
cf3f0 31 30 30 2d 32 34 29 3b 0a 20 20 7a 65 72 6f 50  100-24);.  zeroP
cf400 61 67 65 28 70 50 31 2c 20 50 54 46 5f 49 4e 54  age(pP1, PTF_INT
cf410 4b 45 59 7c 50 54 46 5f 4c 45 41 46 7c 50 54 46  KEY|PTF_LEAF|PTF
cf420 5f 4c 45 41 46 44 41 54 41 20 29 3b 0a 20 20 70  _LEAFDATA );.  p
cf430 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65  Bt->pageSizeFixe
cf440 64 20 3d 20 31 3b 0a 23 69 66 6e 64 65 66 20 53  d = 1;.#ifndef S
cf450 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
cf460 41 43 55 55 4d 0a 20 20 61 73 73 65 72 74 28 20  ACUUM.  assert( 
cf470 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 3d  pBt->autoVacuum=
cf480 3d 31 20 7c 7c 20 70 42 74 2d 3e 61 75 74 6f 56  =1 || pBt->autoV
cf490 61 63 75 75 6d 3d 3d 30 20 29 3b 0a 20 20 61 73  acuum==0 );.  as
cf4a0 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 63 72 56  sert( pBt->incrV
cf4b0 61 63 75 75 6d 3d 3d 31 20 7c 7c 20 70 42 74 2d  acuum==1 || pBt-
cf4c0 3e 69 6e 63 72 56 61 63 75 75 6d 3d 3d 30 20 29  >incrVacuum==0 )
cf4d0 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26 64 61  ;.  put4byte(&da
cf4e0 74 61 5b 33 36 20 2b 20 34 2a 34 5d 2c 20 70 42  ta[36 + 4*4], pB
cf4f0 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 3b 0a  t->autoVacuum);.
cf500 20 20 70 75 74 34 62 79 74 65 28 26 64 61 74 61    put4byte(&data
cf510 5b 33 36 20 2b 20 37 2a 34 5d 2c 20 70 42 74 2d  [36 + 7*4], pBt-
cf520 3e 69 6e 63 72 56 61 63 75 75 6d 29 3b 0a 23 65  >incrVacuum);.#e
cf530 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 53 51  ndif.  return SQ
cf540 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
cf550 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 73 74 61  * Attempt to sta
cf560 72 74 20 61 20 6e 65 77 20 74 72 61 6e 73 61 63  rt a new transac
cf570 74 69 6f 6e 2e 20 41 20 77 72 69 74 65 2d 74 72  tion. A write-tr
cf580 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 20  ansaction.** is 
cf590 73 74 61 72 74 65 64 20 69 66 20 74 68 65 20 73  started if the s
cf5a0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69  econd argument i
cf5b0 73 20 6e 6f 6e 7a 65 72 6f 2c 20 6f 74 68 65 72  s nonzero, other
cf5c0 77 69 73 65 20 61 20 72 65 61 64 2d 0a 2a 2a 20  wise a read-.** 
cf5d0 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 49 66  transaction.  If
cf5e0 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
cf5f0 6d 65 6e 74 20 69 73 20 32 20 6f 72 20 6d 6f 72  ment is 2 or mor
cf600 65 20 61 6e 64 20 65 78 63 6c 75 73 69 76 65 0a  e and exclusive.
cf610 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  ** transaction i
cf620 73 20 73 74 61 72 74 65 64 2c 20 6d 65 61 6e 69  s started, meani
cf630 6e 67 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72  ng that no other
cf640 20 70 72 6f 63 65 73 73 20 69 73 20 61 6c 6c 6f   process is allo
cf650 77 65 64 0a 2a 2a 20 74 6f 20 61 63 63 65 73 73  wed.** to access
cf660 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20   the database.  
cf670 41 20 70 72 65 65 78 69 73 74 69 6e 67 20 74 72  A preexisting tr
cf680 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 6e 6f  ansaction may no
cf690 74 20 62 65 0a 2a 2a 20 75 70 67 72 61 64 65 64  t be.** upgraded
cf6a0 20 74 6f 20 65 78 63 6c 75 73 69 76 65 20 62 79   to exclusive by
cf6b0 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f   calling this ro
cf6c0 75 74 69 6e 65 20 61 20 73 65 63 6f 6e 64 20 74  utine a second t
cf6d0 69 6d 65 20 2d 20 74 68 65 0a 2a 2a 20 65 78 63  ime - the.** exc
cf6e0 6c 75 73 69 76 69 74 79 20 66 6c 61 67 20 6f 6e  lusivity flag on
cf6f0 6c 79 20 77 6f 72 6b 73 20 66 6f 72 20 61 20 6e  ly works for a n
cf700 65 77 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a  ew transaction..
cf710 2a 2a 0a 2a 2a 20 41 20 77 72 69 74 65 2d 74 72  **.** A write-tr
cf720 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62  ansaction must b
cf730 65 20 73 74 61 72 74 65 64 20 62 65 66 6f 72 65  e started before
cf740 20 61 74 74 65 6d 70 74 69 6e 67 20 61 6e 79 20   attempting any 
cf750 0a 2a 2a 20 63 68 61 6e 67 65 73 20 74 6f 20 74  .** changes to t
cf760 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 4e 6f  he database.  No
cf770 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ne of the follow
cf780 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 0a 2a 2a  ing routines .**
cf790 20 77 69 6c 6c 20 77 6f 72 6b 20 75 6e 6c 65 73   will work unles
cf7a0 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  s a transaction 
cf7b0 69 73 20 73 74 61 72 74 65 64 20 66 69 72 73 74  is started first
cf7c0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 73 71 6c  :.**.**      sql
cf7d0 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54  ite3BtreeCreateT
cf7e0 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20 73  able().**      s
cf7f0 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74  qlite3BtreeCreat
cf800 65 49 6e 64 65 78 28 29 0a 2a 2a 20 20 20 20 20  eIndex().**     
cf810 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65   sqlite3BtreeCle
cf820 61 72 54 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20  arTable().**    
cf830 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 72    sqlite3BtreeDr
cf840 6f 70 54 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20  opTable().**    
cf850 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e    sqlite3BtreeIn
cf860 73 65 72 74 28 29 0a 2a 2a 20 20 20 20 20 20 73  sert().**      s
cf870 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74  qlite3BtreeDelet
cf880 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69  e().**      sqli
cf890 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d 65  te3BtreeUpdateMe
cf8a0 74 61 28 29 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  ta().**.** If an
cf8b0 20 69 6e 69 74 69 61 6c 20 61 74 74 65 6d 70 74   initial attempt
cf8c0 20 74 6f 20 61 63 71 75 69 72 65 20 74 68 65 20   to acquire the 
cf8d0 6c 6f 63 6b 20 66 61 69 6c 73 20 62 65 63 61 75  lock fails becau
cf8e0 73 65 20 6f 66 20 6c 6f 63 6b 20 63 6f 6e 74 65  se of lock conte
cf8f0 6e 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 74 68 65  ntion.** and the
cf900 20 64 61 74 61 62 61 73 65 20 77 61 73 20 70 72   database was pr
cf910 65 76 69 6f 75 73 6c 79 20 75 6e 6c 6f 63 6b 65  eviously unlocke
cf920 64 2c 20 74 68 65 6e 20 69 6e 76 6f 6b 65 20 74  d, then invoke t
cf930 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 0a  he busy handler.
cf940 2a 2a 20 69 66 20 74 68 65 72 65 20 69 73 20 6f  ** if there is o
cf950 6e 65 2e 20 20 42 75 74 20 69 66 20 74 68 65 72  ne.  But if ther
cf960 65 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79  e was previously
cf970 20 61 20 72 65 61 64 2d 6c 6f 63 6b 2c 20 64 6f   a read-lock, do
cf980 20 6e 6f 74 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74   not.** invoke t
cf990 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20  he busy handler 
cf9a0 2d 20 6a 75 73 74 20 72 65 74 75 72 6e 20 53 51  - just return SQ
cf9b0 4c 49 54 45 5f 42 55 53 59 2e 20 20 53 51 4c 49  LITE_BUSY.  SQLI
cf9c0 54 45 5f 42 55 53 59 20 69 73 20 0a 2a 2a 20 72  TE_BUSY is .** r
cf9d0 65 74 75 72 6e 65 64 20 77 68 65 6e 20 74 68 65  eturned when the
cf9e0 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20  re is already a 
cf9f0 72 65 61 64 2d 6c 6f 63 6b 20 69 6e 20 6f 72 64  read-lock in ord
cfa00 65 72 20 74 6f 20 61 76 6f 69 64 20 61 20 64 65  er to avoid a de
cfa10 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 75  adlock..**.** Su
cfa20 70 70 6f 73 65 20 74 68 65 72 65 20 61 72 65 20  ppose there are 
cfa30 74 77 6f 20 70 72 6f 63 65 73 73 65 73 20 41 20  two processes A 
cfa40 61 6e 64 20 42 2e 20 20 41 20 68 61 73 20 61 20  and B.  A has a 
cfa50 72 65 61 64 20 6c 6f 63 6b 20 61 6e 64 20 42 20  read lock and B 
cfa60 68 61 73 0a 2a 2a 20 61 20 72 65 73 65 72 76 65  has.** a reserve
cfa70 64 20 6c 6f 63 6b 2e 20 20 42 20 74 72 69 65 73  d lock.  B tries
cfa80 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f 20 65   to promote to e
cfa90 78 63 6c 75 73 69 76 65 20 62 75 74 20 69 73 20  xclusive but is 
cfaa0 62 6c 6f 63 6b 65 64 20 62 65 63 61 75 73 65 0a  blocked because.
cfab0 2a 2a 20 6f 66 20 41 27 73 20 72 65 61 64 20 6c  ** of A's read l
cfac0 6f 63 6b 2e 20 20 41 20 74 72 69 65 73 20 74 6f  ock.  A tries to
cfad0 20 70 72 6f 6d 6f 74 65 20 74 6f 20 72 65 73 65   promote to rese
cfae0 72 76 65 64 20 62 75 74 20 69 73 20 62 6c 6f 63  rved but is bloc
cfaf0 6b 65 64 20 62 79 20 42 2e 0a 2a 2a 20 4f 6e 65  ked by B..** One
cfb00 20 6f 72 20 74 68 65 20 6f 74 68 65 72 20 6f 66   or the other of
cfb10 20 74 68 65 20 74 77 6f 20 70 72 6f 63 65 73 73   the two process
cfb20 65 73 20 6d 75 73 74 20 67 69 76 65 20 77 61 79  es must give way
cfb30 20 6f 72 20 74 68 65 72 65 20 63 61 6e 20 62 65   or there can be
cfb40 0a 2a 2a 20 6e 6f 20 70 72 6f 67 72 65 73 73 2e  .** no progress.
cfb50 20 20 42 79 20 72 65 74 75 72 6e 69 6e 67 20 53    By returning S
cfb60 51 4c 49 54 45 5f 42 55 53 59 20 61 6e 64 20 6e  QLITE_BUSY and n
cfb70 6f 74 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20  ot invoking the 
cfb80 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a  busy callback.**
cfb90 20 77 68 65 6e 20 41 20 61 6c 72 65 61 64 79 20   when A already 
cfba0 68 61 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 2c  has a read lock,
cfbb0 20 77 65 20 65 6e 63 6f 75 72 61 67 65 20 41 20   we encourage A 
cfbc0 74 6f 20 67 69 76 65 20 75 70 20 61 6e 64 20 6c  to give up and l
cfbd0 65 74 20 42 0a 2a 2a 20 70 72 6f 63 65 65 64 2e  et B.** proceed.
cfbe0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
cfbf0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
cfc00 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 42 74  reeBeginTrans(Bt
cfc10 72 65 65 20 2a 70 2c 20 69 6e 74 20 77 72 66 6c  ree *p, int wrfl
cfc20 61 67 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20  ag){.  BtShared 
cfc30 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20  *pBt = p->pBt;. 
cfc40 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
cfc50 5f 4f 4b 3b 0a 0a 20 20 62 74 72 65 65 49 6e 74  _OK;..  btreeInt
cfc60 65 67 72 69 74 79 28 70 29 3b 0a 0a 20 20 2f 2a  egrity(p);..  /*
cfc70 20 49 66 20 74 68 65 20 62 74 72 65 65 20 69 73   If the btree is
cfc80 20 61 6c 72 65 61 64 79 20 69 6e 20 61 20 77 72   already in a wr
cfc90 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2c  ite-transaction,
cfca0 20 6f 72 20 69 74 0a 20 20 2a 2a 20 69 73 20 61   or it.  ** is a
cfcb0 6c 72 65 61 64 79 20 69 6e 20 61 20 72 65 61 64  lready in a read
cfcc0 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64  -transaction and
cfcd0 20 61 20 72 65 61 64 2d 74 72 61 6e 73 61 63 74   a read-transact
cfce0 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 72 65 71 75  ion.  ** is requ
cfcf0 65 73 74 65 64 2c 20 74 68 69 73 20 69 73 20 61  ested, this is a
cfd00 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2f 0a 20 20 69   no-op..  */.  i
cfd10 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54  f( p->inTrans==T
cfd20 52 41 4e 53 5f 57 52 49 54 45 20 7c 7c 20 28 70  RANS_WRITE || (p
cfd30 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53  ->inTrans==TRANS
cfd40 5f 52 45 41 44 20 26 26 20 21 77 72 66 6c 61 67  _READ && !wrflag
cfd50 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
cfd60 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a  SQLITE_OK;.  }..
cfd70 20 20 2f 2a 20 57 72 69 74 65 20 74 72 61 6e 73    /* Write trans
cfd80 61 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20  actions are not 
cfd90 70 6f 73 73 69 62 6c 65 20 6f 6e 20 61 20 72 65  possible on a re
cfda0 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73 65  ad-only database
cfdb0 20 2a 2f 0a 20 20 69 66 28 20 70 42 74 2d 3e 72   */.  if( pBt->r
cfdc0 65 61 64 4f 6e 6c 79 20 26 26 20 77 72 66 6c 61  eadOnly && wrfla
cfdd0 67 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  g ){.    return 
cfde0 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b  SQLITE_READONLY;
cfdf0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 6e  .  }..  /* If an
cfe00 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 68  other database h
cfe10 61 6e 64 6c 65 20 68 61 73 20 61 6c 72 65 61 64  andle has alread
cfe20 79 20 6f 70 65 6e 65 64 20 61 20 77 72 69 74 65  y opened a write
cfe30 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 20 20   transaction .  
cfe40 2a 2a 20 6f 6e 20 74 68 69 73 20 73 68 61 72 65  ** on this share
cfe50 64 2d 62 74 72 65 65 20 73 74 72 75 63 74 75 72  d-btree structur
cfe60 65 20 61 6e 64 20 61 20 73 65 63 6f 6e 64 20 77  e and a second w
cfe70 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  rite transaction
cfe80 20 69 73 0a 20 20 2a 2a 20 72 65 71 75 65 73 74   is.  ** request
cfe90 65 64 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ed, return SQLIT
cfea0 45 5f 42 55 53 59 2e 0a 20 20 2a 2f 0a 20 20 69  E_BUSY..  */.  i
cfeb0 66 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61  f( pBt->inTransa
cfec0 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49  ction==TRANS_WRI
cfed0 54 45 20 26 26 20 77 72 66 6c 61 67 20 29 7b 0a  TE && wrflag ){.
cfee0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
cfef0 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 0a 20 20 64  E_BUSY;.  }..  d
cff00 6f 20 7b 0a 20 20 20 20 69 66 28 20 70 42 74 2d  o {.    if( pBt-
cff10 3e 70 50 61 67 65 31 3d 3d 30 20 29 7b 0a 20 20  >pPage1==0 ){.  
cff20 20 20 20 20 72 63 20 3d 20 6c 6f 63 6b 42 74 72      rc = lockBtr
cff30 65 65 28 70 42 74 29 3b 0a 20 20 20 20 7d 0a 0a  ee(pBt);.    }..
cff40 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
cff50 54 45 5f 4f 4b 20 26 26 20 77 72 66 6c 61 67 20  TE_OK && wrflag 
cff60 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74  ){.      if( pBt
cff70 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20  ->readOnly ){.  
cff80 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
cff90 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20  E_READONLY;.    
cffa0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
cffb0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
cffc0 65 72 42 65 67 69 6e 28 70 42 74 2d 3e 70 50 61  erBegin(pBt->pPa
cffd0 67 65 31 2d 3e 70 44 62 50 61 67 65 2c 20 77 72  ge1->pDbPage, wr
cffe0 66 6c 61 67 3e 31 29 3b 0a 20 20 20 20 20 20 20  flag>1);.       
cfff0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
d0000 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
d0010 72 63 20 3d 20 6e 65 77 44 61 74 61 62 61 73 65  rc = newDatabase
d0020 28 70 42 74 29 3b 0a 20 20 20 20 20 20 20 20 7d  (pBt);.        }
d0030 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
d0040 20 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51   .    if( rc==SQ
d0050 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
d0060 20 69 66 28 20 77 72 66 6c 61 67 20 29 20 70 42   if( wrflag ) pB
d0070 74 2d 3e 69 6e 53 74 6d 74 20 3d 20 30 3b 0a 20  t->inStmt = 0;. 
d0080 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
d0090 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75  unlockBtreeIfUnu
d00a0 73 65 64 28 70 42 74 29 3b 0a 20 20 20 20 7d 0a  sed(pBt);.    }.
d00b0 20 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d 53 51    }while( rc==SQ
d00c0 4c 49 54 45 5f 42 55 53 59 20 26 26 20 70 42 74  LITE_BUSY && pBt
d00d0 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d  ->inTransaction=
d00e0 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 0a 20  =TRANS_NONE &&. 
d00f0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
d0100 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65  InvokeBusyHandle
d0110 72 28 70 42 74 2d 3e 70 42 75 73 79 48 61 6e 64  r(pBt->pBusyHand
d0120 6c 65 72 29 20 29 3b 0a 0a 20 20 69 66 28 20 72  ler) );..  if( r
d0130 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
d0140 20 20 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61      if( p->inTra
d0150 6e 73 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29  ns==TRANS_NONE )
d0160 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 54 72  {.      pBt->nTr
d0170 61 6e 73 61 63 74 69 6f 6e 2b 2b 3b 0a 20 20 20  ansaction++;.   
d0180 20 7d 0a 20 20 20 20 70 2d 3e 69 6e 54 72 61 6e   }.    p->inTran
d0190 73 20 3d 20 28 77 72 66 6c 61 67 3f 54 52 41 4e  s = (wrflag?TRAN
d01a0 53 5f 57 52 49 54 45 3a 54 52 41 4e 53 5f 52 45  S_WRITE:TRANS_RE
d01b0 41 44 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  AD);.    if( p->
d01c0 69 6e 54 72 61 6e 73 3e 70 42 74 2d 3e 69 6e 54  inTrans>pBt->inT
d01d0 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20  ransaction ){.  
d01e0 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73      pBt->inTrans
d01f0 61 63 74 69 6f 6e 20 3d 20 70 2d 3e 69 6e 54 72  action = p->inTr
d0200 61 6e 73 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ans;.    }.  }..
d0210 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 79    btreeIntegrity
d0220 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  (p);.  return rc
d0230 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
d0240 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
d0250 55 55 4d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74  UUM../*.** Set t
d0260 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65  he pointer-map e
d0270 6e 74 72 69 65 73 20 66 6f 72 20 61 6c 6c 20 63  ntries for all c
d0280 68 69 6c 64 72 65 6e 20 6f 66 20 70 61 67 65 20  hildren of page 
d0290 70 50 61 67 65 2e 20 41 6c 73 6f 2c 20 69 66 0a  pPage. Also, if.
d02a0 2a 2a 20 70 50 61 67 65 20 63 6f 6e 74 61 69 6e  ** pPage contain
d02b0 73 20 63 65 6c 6c 73 20 74 68 61 74 20 70 6f 69  s cells that poi
d02c0 6e 74 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70  nt to overflow p
d02d0 61 67 65 73 2c 20 73 65 74 20 74 68 65 20 70 6f  ages, set the po
d02e0 69 6e 74 65 72 0a 2a 2a 20 6d 61 70 20 65 6e 74  inter.** map ent
d02f0 72 69 65 73 20 66 6f 72 20 74 68 65 20 6f 76 65  ries for the ove
d0300 72 66 6c 6f 77 20 70 61 67 65 73 20 61 73 20 77  rflow pages as w
d0310 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ell..*/.static i
d0320 6e 74 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61  nt setChildPtrma
d0330 70 73 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  ps(MemPage *pPag
d0340 65 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20  e){.  int i;    
d0350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d0360 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e           /* Coun
d0370 74 65 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a  ter variable */.
d0380 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 20 20 20 20    int nCell;    
d0390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d03a0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
d03b0 66 20 63 65 6c 6c 73 20 69 6e 20 70 61 67 65 20  f cells in page 
d03c0 70 50 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72  pPage */.  int r
d03d0 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
d03e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
d03f0 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
d0400 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
d0410 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20  = pPage->pBt;.  
d0420 69 6e 74 20 69 73 49 6e 69 74 4f 72 69 67 20 3d  int isInitOrig =
d0430 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 3b 0a   pPage->isInit;.
d0440 20 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 50    Pgno pgno = pP
d0450 61 67 65 2d 3e 70 67 6e 6f 3b 0a 0a 20 20 72 63  age->pgno;..  rc
d0460 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 49   = sqlite3BtreeI
d0470 6e 69 74 50 61 67 65 28 70 50 61 67 65 2c 20 70  nitPage(pPage, p
d0480 50 61 67 65 2d 3e 70 50 61 72 65 6e 74 29 3b 0a  Page->pParent);.
d0490 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
d04a0 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  _OK ){.    goto 
d04b0 73 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d 61 70  set_child_ptrmap
d04c0 73 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 6e 43 65  s_out;.  }.  nCe
d04d0 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c  ll = pPage->nCel
d04e0 6c 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  l;..  for(i=0; i
d04f0 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20  <nCell; i++){.  
d0500 20 20 75 38 20 2a 70 43 65 6c 6c 20 3d 20 66 69    u8 *pCell = fi
d0510 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 29  ndCell(pPage, i)
d0520 3b 0a 0a 20 20 20 20 72 63 20 3d 20 70 74 72 6d  ;..    rc = ptrm
d0530 61 70 50 75 74 4f 76 66 6c 50 74 72 28 70 50 61  apPutOvflPtr(pPa
d0540 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20  ge, pCell);.    
d0550 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
d0560 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  K ){.      goto 
d0570 73 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d 61 70  set_child_ptrmap
d0580 73 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20  s_out;.    }..  
d0590 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65    if( !pPage->le
d05a0 61 66 20 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f  af ){.      Pgno
d05b0 20 63 68 69 6c 64 50 67 6e 6f 20 3d 20 67 65 74   childPgno = get
d05c0 34 62 79 74 65 28 70 43 65 6c 6c 29 3b 0a 20 20  4byte(pCell);.  
d05d0 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50      rc = ptrmapP
d05e0 75 74 28 70 42 74 2c 20 63 68 69 6c 64 50 67 6e  ut(pBt, childPgn
d05f0 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c  o, PTRMAP_BTREE,
d0600 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66   pgno);.      if
d0610 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
d0620 29 20 67 6f 74 6f 20 73 65 74 5f 63 68 69 6c 64  ) goto set_child
d0630 5f 70 74 72 6d 61 70 73 5f 6f 75 74 3b 0a 20 20  _ptrmaps_out;.  
d0640 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21    }.  }..  if( !
d0650 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20  pPage->leaf ){. 
d0660 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 50 67 6e     Pgno childPgn
d0670 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50  o = get4byte(&pP
d0680 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65  age->aData[pPage
d0690 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b  ->hdrOffset+8]);
d06a0 0a 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70  .    rc = ptrmap
d06b0 50 75 74 28 70 42 74 2c 20 63 68 69 6c 64 50 67  Put(pBt, childPg
d06c0 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45  no, PTRMAP_BTREE
d06d0 2c 20 70 67 6e 6f 29 3b 0a 20 20 7d 0a 0a 73 65  , pgno);.  }..se
d06e0 74 5f 63 68 69 6c 64 5f 70 74 72 6d 61 70 73 5f  t_child_ptrmaps_
d06f0 6f 75 74 3a 0a 20 20 70 50 61 67 65 2d 3e 69 73  out:.  pPage->is
d0700 49 6e 69 74 20 3d 20 69 73 49 6e 69 74 4f 72 69  Init = isInitOri
d0710 67 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  g;.  return rc;.
d0720 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 77 68 65  }../*.** Somewhe
d0730 72 65 20 6f 6e 20 70 50 61 67 65 2c 20 77 68 69  re on pPage, whi
d0740 63 68 20 69 73 20 67 75 61 72 65 6e 74 65 65 64  ch is guarenteed
d0750 20 74 6f 20 62 65 20 61 20 62 74 72 65 65 20 70   to be a btree p
d0760 61 67 65 2c 20 6e 6f 74 20 61 6e 20 6f 76 65 72  age, not an over
d0770 66 6c 6f 77 0a 2a 2a 20 70 61 67 65 2c 20 69 73  flow.** page, is
d0780 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 61   a pointer to pa
d0790 67 65 20 69 46 72 6f 6d 2e 20 4d 6f 64 69 66 79  ge iFrom. Modify
d07a0 20 74 68 69 73 20 70 6f 69 6e 74 65 72 20 73 6f   this pointer so
d07b0 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20   that it points 
d07c0 74 6f 0a 2a 2a 20 69 54 6f 2e 20 50 61 72 61 6d  to.** iTo. Param
d07d0 65 74 65 72 20 65 54 79 70 65 20 64 65 73 63 72  eter eType descr
d07e0 69 62 65 73 20 74 68 65 20 74 79 70 65 20 6f 66  ibes the type of
d07f0 20 70 6f 69 6e 74 65 72 20 74 6f 20 62 65 20 6d   pointer to be m
d0800 6f 64 69 66 69 65 64 2c 20 61 73 20 0a 2a 2a 20  odified, as .** 
d0810 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 50  follows:.**.** P
d0820 54 52 4d 41 50 5f 42 54 52 45 45 3a 20 20 20 20  TRMAP_BTREE:    
d0830 20 70 50 61 67 65 20 69 73 20 61 20 62 74 72 65   pPage is a btre
d0840 65 2d 70 61 67 65 2e 20 54 68 65 20 70 6f 69 6e  e-page. The poin
d0850 74 65 72 20 70 6f 69 6e 74 73 20 61 74 20 61 20  ter points at a 
d0860 63 68 69 6c 64 20 0a 2a 2a 20 20 20 20 20 20 20  child .**       
d0870 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67 65              page
d0880 20 6f 66 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a   of pPage..**.**
d0890 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57   PTRMAP_OVERFLOW
d08a0 31 3a 20 70 50 61 67 65 20 69 73 20 61 20 62 74  1: pPage is a bt
d08b0 72 65 65 2d 70 61 67 65 2e 20 54 68 65 20 70 6f  ree-page. The po
d08c0 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 61 74 20  inter points at 
d08d0 61 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 20  an overflow.**  
d08e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d08f0 20 70 61 67 65 20 70 6f 69 6e 74 65 64 20 74 6f   page pointed to
d0900 20 62 79 20 6f 6e 65 20 6f 66 20 74 68 65 20 63   by one of the c
d0910 65 6c 6c 73 20 6f 6e 20 70 50 61 67 65 2e 0a 2a  ells on pPage..*
d0920 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52  *.** PTRMAP_OVER
d0930 46 4c 4f 57 32 3a 20 70 50 61 67 65 20 69 73 20  FLOW2: pPage is 
d0940 61 6e 20 6f 76 65 72 66 6c 6f 77 2d 70 61 67 65  an overflow-page
d0950 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 70 6f  . The pointer po
d0960 69 6e 74 73 20 61 74 20 74 68 65 20 6e 65 78 74  ints at the next
d0970 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
d0980 20 20 20 20 20 20 6f 76 65 72 66 6c 6f 77 20 70        overflow p
d0990 61 67 65 20 69 6e 20 74 68 65 20 6c 69 73 74 2e  age in the list.
d09a0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  .*/.static int m
d09b0 6f 64 69 66 79 50 61 67 65 50 6f 69 6e 74 65 72  odifyPagePointer
d09c0 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c  (MemPage *pPage,
d09d0 20 50 67 6e 6f 20 69 46 72 6f 6d 2c 20 50 67 6e   Pgno iFrom, Pgn
d09e0 6f 20 69 54 6f 2c 20 75 38 20 65 54 79 70 65 29  o iTo, u8 eType)
d09f0 7b 0a 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50  {.  if( eType==P
d0a00 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 20  TRMAP_OVERFLOW2 
d0a10 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 6f  ){.    /* The po
d0a20 69 6e 74 65 72 20 69 73 20 61 6c 77 61 79 73 20  inter is always 
d0a30 74 68 65 20 66 69 72 73 74 20 34 20 62 79 74 65  the first 4 byte
d0a40 73 20 6f 66 20 74 68 65 20 70 61 67 65 20 69 6e  s of the page in
d0a50 20 74 68 69 73 20 63 61 73 65 2e 20 20 2a 2f 0a   this case.  */.
d0a60 20 20 20 20 69 66 28 20 67 65 74 34 62 79 74 65      if( get4byte
d0a70 28 70 50 61 67 65 2d 3e 61 44 61 74 61 29 21 3d  (pPage->aData)!=
d0a80 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20 72  iFrom ){.      r
d0a90 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
d0aa0 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d  RUPT_BKPT;.    }
d0ab0 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 70 50  .    put4byte(pP
d0ac0 61 67 65 2d 3e 61 44 61 74 61 2c 20 69 54 6f 29  age->aData, iTo)
d0ad0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  ;.  }else{.    i
d0ae0 6e 74 20 69 73 49 6e 69 74 4f 72 69 67 20 3d 20  nt isInitOrig = 
d0af0 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 3b 0a 20  pPage->isInit;. 
d0b00 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 69 6e     int i;.    in
d0b10 74 20 6e 43 65 6c 6c 3b 0a 0a 20 20 20 20 73 71  t nCell;..    sq
d0b20 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61  lite3BtreeInitPa
d0b30 67 65 28 70 50 61 67 65 2c 20 30 29 3b 0a 20 20  ge(pPage, 0);.  
d0b40 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d    nCell = pPage-
d0b50 3e 6e 43 65 6c 6c 3b 0a 0a 20 20 20 20 66 6f 72  >nCell;..    for
d0b60 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69  (i=0; i<nCell; i
d0b70 2b 2b 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 70  ++){.      u8 *p
d0b80 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28  Cell = findCell(
d0b90 70 50 61 67 65 2c 20 69 29 3b 0a 20 20 20 20 20  pPage, i);.     
d0ba0 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d   if( eType==PTRM
d0bb0 41 50 5f 4f 56 45 52 46 4c 4f 57 31 20 29 7b 0a  AP_OVERFLOW1 ){.
d0bc0 20 20 20 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f          CellInfo
d0bd0 20 69 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 73   info;.        s
d0be0 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65  qlite3BtreeParse
d0bf0 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70  CellPtr(pPage, p
d0c00 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20  Cell, &info);.  
d0c10 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f 2e 69        if( info.i
d0c20 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20  Overflow ){.    
d0c30 20 20 20 20 20 20 69 66 28 20 69 46 72 6f 6d 3d        if( iFrom=
d0c40 3d 67 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c  =get4byte(&pCell
d0c50 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d  [info.iOverflow]
d0c60 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ) ){.           
d0c70 20 70 75 74 34 62 79 74 65 28 26 70 43 65 6c 6c   put4byte(&pCell
d0c80 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d  [info.iOverflow]
d0c90 2c 20 69 54 6f 29 3b 0a 20 20 20 20 20 20 20 20  , iTo);.        
d0ca0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
d0cb0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
d0cc0 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
d0cd0 20 20 20 20 20 20 69 66 28 20 67 65 74 34 62 79        if( get4by
d0ce0 74 65 28 70 43 65 6c 6c 29 3d 3d 69 46 72 6f 6d  te(pCell)==iFrom
d0cf0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 75   ){.          pu
d0d00 74 34 62 79 74 65 28 70 43 65 6c 6c 2c 20 69 54  t4byte(pCell, iT
d0d10 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72  o);.          br
d0d20 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
d0d30 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a       }.    }.  .
d0d40 20 20 20 20 69 66 28 20 69 3d 3d 6e 43 65 6c 6c      if( i==nCell
d0d50 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 65 54   ){.      if( eT
d0d60 79 70 65 21 3d 50 54 52 4d 41 50 5f 42 54 52 45  ype!=PTRMAP_BTRE
d0d70 45 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 20 20  E || .          
d0d80 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d  get4byte(&pPage-
d0d90 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64  >aData[pPage->hd
d0da0 72 4f 66 66 73 65 74 2b 38 5d 29 21 3d 69 46 72  rOffset+8])!=iFr
d0db0 6f 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  om ){.        re
d0dc0 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
d0dd0 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20  UPT_BKPT;.      
d0de0 7d 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65  }.      put4byte
d0df0 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70  (&pPage->aData[p
d0e00 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b  Page->hdrOffset+
d0e10 38 5d 2c 20 69 54 6f 29 3b 0a 20 20 20 20 7d 0a  8], iTo);.    }.
d0e20 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 73 49 6e  .    pPage->isIn
d0e30 69 74 20 3d 20 69 73 49 6e 69 74 4f 72 69 67 3b  it = isInitOrig;
d0e40 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
d0e50 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a  LITE_OK;.}.../*.
d0e60 2a 2a 20 4d 6f 76 65 20 74 68 65 20 6f 70 65 6e  ** Move the open
d0e70 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 70   database page p
d0e80 44 62 50 61 67 65 20 74 6f 20 6c 6f 63 61 74 69  DbPage to locati
d0e90 6f 6e 20 69 46 72 65 65 50 61 67 65 20 69 6e 20  on iFreePage in 
d0ea0 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65  the .** database
d0eb0 2e 20 54 68 65 20 70 44 62 50 61 67 65 20 72 65  . The pDbPage re
d0ec0 66 65 72 65 6e 63 65 20 72 65 6d 61 69 6e 73 20  ference remains 
d0ed0 76 61 6c 69 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  valid..*/.static
d0ee0 20 69 6e 74 20 72 65 6c 6f 63 61 74 65 50 61 67   int relocatePag
d0ef0 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  e(.  BtShared *p
d0f00 42 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  Bt,           /*
d0f10 20 42 74 72 65 65 20 2a 2f 0a 20 20 4d 65 6d 50   Btree */.  MemP
d0f20 61 67 65 20 2a 70 44 62 50 61 67 65 2c 20 20 20  age *pDbPage,   
d0f30 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 70 61 67       /* Open pag
d0f40 65 20 74 6f 20 6d 6f 76 65 20 2a 2f 0a 20 20 75  e to move */.  u
d0f50 38 20 65 54 79 70 65 2c 20 20 20 20 20 20 20 20  8 eType,        
d0f60 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
d0f70 65 72 20 6d 61 70 20 27 74 79 70 65 27 20 65 6e  er map 'type' en
d0f80 74 72 79 20 66 6f 72 20 70 44 62 50 61 67 65 20  try for pDbPage 
d0f90 2a 2f 0a 20 20 50 67 6e 6f 20 69 50 74 72 50 61  */.  Pgno iPtrPa
d0fa0 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ge,           /*
d0fb0 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 27 70 61   Pointer map 'pa
d0fc0 67 65 2d 6e 6f 27 20 65 6e 74 72 79 20 66 6f 72  ge-no' entry for
d0fd0 20 70 44 62 50 61 67 65 20 2a 2f 0a 20 20 50 67   pDbPage */.  Pg
d0fe0 6e 6f 20 69 46 72 65 65 50 61 67 65 20 20 20 20  no iFreePage    
d0ff0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f         /* The lo
d1000 63 61 74 69 6f 6e 20 74 6f 20 6d 6f 76 65 20 70  cation to move p
d1010 44 62 50 61 67 65 20 74 6f 20 2a 2f 0a 29 7b 0a  DbPage to */.){.
d1020 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 74 72 50    MemPage *pPtrP
d1030 61 67 65 3b 20 20 20 2f 2a 20 54 68 65 20 70 61  age;   /* The pa
d1040 67 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  ge that contains
d1050 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 44   a pointer to pD
d1060 62 50 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20  bPage */.  Pgno 
d1070 69 44 62 50 61 67 65 20 3d 20 70 44 62 50 61 67  iDbPage = pDbPag
d1080 65 2d 3e 70 67 6e 6f 3b 0a 20 20 50 61 67 65 72  e->pgno;.  Pager
d1090 20 2a 70 50 61 67 65 72 20 3d 20 70 42 74 2d 3e   *pPager = pBt->
d10a0 70 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 72 63  pPager;.  int rc
d10b0 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 65 54 79  ;..  assert( eTy
d10c0 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46  pe==PTRMAP_OVERF
d10d0 4c 4f 57 32 20 7c 7c 20 65 54 79 70 65 3d 3d 50  LOW2 || eType==P
d10e0 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 20  TRMAP_OVERFLOW1 
d10f0 7c 7c 20 0a 20 20 20 20 20 20 65 54 79 70 65 3d  || .      eType=
d1100 3d 50 54 52 4d 41 50 5f 42 54 52 45 45 20 7c 7c  =PTRMAP_BTREE ||
d1110 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52   eType==PTRMAP_R
d1120 4f 4f 54 50 41 47 45 20 29 3b 0a 0a 20 20 2f 2a  OOTPAGE );..  /*
d1130 20 4d 6f 76 65 20 70 61 67 65 20 69 44 62 50 61   Move page iDbPa
d1140 67 65 20 66 72 6f 6d 20 69 74 27 73 20 63 75 72  ge from it's cur
d1150 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 20 74 6f  rent location to
d1160 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 46 72   page number iFr
d1170 65 65 50 61 67 65 20 2a 2f 0a 20 20 54 52 41 43  eePage */.  TRAC
d1180 45 28 28 22 41 55 54 4f 56 41 43 55 55 4d 3a 20  E(("AUTOVACUUM: 
d1190 4d 6f 76 69 6e 67 20 25 64 20 74 6f 20 66 72 65  Moving %d to fre
d11a0 65 20 70 61 67 65 20 25 64 20 28 70 74 72 20 70  e page %d (ptr p
d11b0 61 67 65 20 25 64 20 74 79 70 65 20 25 64 29 5c  age %d type %d)\
d11c0 6e 22 2c 20 0a 20 20 20 20 20 20 69 44 62 50 61  n", .      iDbPa
d11d0 67 65 2c 20 69 46 72 65 65 50 61 67 65 2c 20 69  ge, iFreePage, i
d11e0 50 74 72 50 61 67 65 2c 20 65 54 79 70 65 29 29  PtrPage, eType))
d11f0 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
d1200 50 61 67 65 72 4d 6f 76 65 70 61 67 65 28 70 50  PagerMovepage(pP
d1210 61 67 65 72 2c 20 70 44 62 50 61 67 65 2d 3e 70  ager, pDbPage->p
d1220 44 62 50 61 67 65 2c 20 69 46 72 65 65 50 61 67  DbPage, iFreePag
d1230 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  e);.  if( rc!=SQ
d1240 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
d1250 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20  eturn rc;.  }.  
d1260 70 44 62 50 61 67 65 2d 3e 70 67 6e 6f 20 3d 20  pDbPage->pgno = 
d1270 69 46 72 65 65 50 61 67 65 3b 0a 0a 20 20 2f 2a  iFreePage;..  /*
d1280 20 49 66 20 70 44 62 50 61 67 65 20 77 61 73 20   If pDbPage was 
d1290 61 20 62 74 72 65 65 2d 70 61 67 65 2c 20 74 68  a btree-page, th
d12a0 65 6e 20 69 74 20 6d 61 79 20 68 61 76 65 20 63  en it may have c
d12b0 68 69 6c 64 20 70 61 67 65 73 20 61 6e 64 2f 6f  hild pages and/o
d12c0 72 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 74 68 61  r cells.  ** tha
d12d0 74 20 70 6f 69 6e 74 20 74 6f 20 6f 76 65 72 66  t point to overf
d12e0 6c 6f 77 20 70 61 67 65 73 2e 20 54 68 65 20 70  low pages. The p
d12f0 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 69  ointer map entri
d1300 65 73 20 66 6f 72 20 61 6c 6c 20 74 68 65 73 65  es for all these
d1310 0a 20 20 2a 2a 20 70 61 67 65 73 20 6e 65 65 64  .  ** pages need
d1320 20 74 6f 20 62 65 20 63 68 61 6e 67 65 64 2e 0a   to be changed..
d1330 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 44 62    **.  ** If pDb
d1340 50 61 67 65 20 69 73 20 61 6e 20 6f 76 65 72 66  Page is an overf
d1350 6c 6f 77 20 70 61 67 65 2c 20 74 68 65 6e 20 74  low page, then t
d1360 68 65 20 66 69 72 73 74 20 34 20 62 79 74 65 73  he first 4 bytes
d1370 20 6d 61 79 20 73 74 6f 72 65 20 61 0a 20 20 2a   may store a.  *
d1380 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73  * pointer to a s
d1390 75 62 73 65 71 75 65 6e 74 20 6f 76 65 72 66 6c  ubsequent overfl
d13a0 6f 77 20 70 61 67 65 2e 20 49 66 20 74 68 69 73  ow page. If this
d13b0 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 68   is the case, th
d13c0 65 6e 0a 20 20 2a 2a 20 74 68 65 20 70 6f 69 6e  en.  ** the poin
d13d0 74 65 72 20 6d 61 70 20 6e 65 65 64 73 20 74 6f  ter map needs to
d13e0 20 62 65 20 75 70 64 61 74 65 64 20 66 6f 72 20   be updated for 
d13f0 74 68 65 20 73 75 62 73 65 71 75 65 6e 74 20 6f  the subsequent o
d1400 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a 20 20  verflow page..  
d1410 2a 2f 0a 20 20 69 66 28 20 65 54 79 70 65 3d 3d  */.  if( eType==
d1420 50 54 52 4d 41 50 5f 42 54 52 45 45 20 7c 7c 20  PTRMAP_BTREE || 
d1430 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f  eType==PTRMAP_RO
d1440 4f 54 50 41 47 45 20 29 7b 0a 20 20 20 20 72 63  OTPAGE ){.    rc
d1450 20 3d 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61   = setChildPtrma
d1460 70 73 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20  ps(pDbPage);.   
d1470 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
d1480 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  OK ){.      retu
d1490 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d  rn rc;.    }.  }
d14a0 65 6c 73 65 7b 0a 20 20 20 20 50 67 6e 6f 20 6e  else{.    Pgno n
d14b0 65 78 74 4f 76 66 6c 20 3d 20 67 65 74 34 62 79  extOvfl = get4by
d14c0 74 65 28 70 44 62 50 61 67 65 2d 3e 61 44 61 74  te(pDbPage->aDat
d14d0 61 29 3b 0a 20 20 20 20 69 66 28 20 6e 65 78 74  a);.    if( next
d14e0 4f 76 66 6c 21 3d 30 20 29 7b 0a 20 20 20 20 20  Ovfl!=0 ){.     
d14f0 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74 28   rc = ptrmapPut(
d1500 70 42 74 2c 20 6e 65 78 74 4f 76 66 6c 2c 20 50  pBt, nextOvfl, P
d1510 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 2c  TRMAP_OVERFLOW2,
d1520 20 69 46 72 65 65 50 61 67 65 29 3b 0a 20 20 20   iFreePage);.   
d1530 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
d1540 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
d1550 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
d1560 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
d1570 2f 2a 20 46 69 78 20 74 68 65 20 64 61 74 61 62  /* Fix the datab
d1580 61 73 65 20 70 6f 69 6e 74 65 72 20 6f 6e 20 70  ase pointer on p
d1590 61 67 65 20 69 50 74 72 50 61 67 65 20 74 68 61  age iPtrPage tha
d15a0 74 20 70 6f 69 6e 74 65 64 20 61 74 20 69 44 62  t pointed at iDb
d15b0 50 61 67 65 20 73 6f 0a 20 20 2a 2a 20 74 68 61  Page so.  ** tha
d15c0 74 20 69 74 20 70 6f 69 6e 74 73 20 61 74 20 69  t it points at i
d15d0 46 72 65 65 50 61 67 65 2e 20 41 6c 73 6f 20 66  FreePage. Also f
d15e0 69 78 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d  ix the pointer m
d15f0 61 70 20 65 6e 74 72 79 20 66 6f 72 0a 20 20 2a  ap entry for.  *
d1600 2a 20 69 50 74 72 50 61 67 65 2e 0a 20 20 2a 2f  * iPtrPage..  */
d1610 0a 20 20 69 66 28 20 65 54 79 70 65 21 3d 50 54  .  if( eType!=PT
d1620 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 7b  RMAP_ROOTPAGE ){
d1630 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
d1640 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42  3BtreeGetPage(pB
d1650 74 2c 20 69 50 74 72 50 61 67 65 2c 20 26 70 50  t, iPtrPage, &pP
d1660 74 72 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 20  trPage, 0);.    
d1670 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
d1680 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  K ){.      retur
d1690 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20  n rc;.    }.    
d16a0 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
d16b0 72 57 72 69 74 65 28 70 50 74 72 50 61 67 65 2d  rWrite(pPtrPage-
d16c0 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69  >pDbPage);.    i
d16d0 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
d16e0 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73   ){.      releas
d16f0 65 50 61 67 65 28 70 50 74 72 50 61 67 65 29 3b  ePage(pPtrPage);
d1700 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
d1710 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d  ;.    }.    rc =
d1720 20 6d 6f 64 69 66 79 50 61 67 65 50 6f 69 6e 74   modifyPagePoint
d1730 65 72 28 70 50 74 72 50 61 67 65 2c 20 69 44 62  er(pPtrPage, iDb
d1740 50 61 67 65 2c 20 69 46 72 65 65 50 61 67 65 2c  Page, iFreePage,
d1750 20 65 54 79 70 65 29 3b 0a 20 20 20 20 72 65 6c   eType);.    rel
d1760 65 61 73 65 50 61 67 65 28 70 50 74 72 50 61 67  easePage(pPtrPag
d1770 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  e);.    if( rc==
d1780 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
d1790 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75     rc = ptrmapPu
d17a0 74 28 70 42 74 2c 20 69 46 72 65 65 50 61 67 65  t(pBt, iFreePage
d17b0 2c 20 65 54 79 70 65 2c 20 69 50 74 72 50 61 67  , eType, iPtrPag
d17c0 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  e);.    }.  }.  
d17d0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
d17e0 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61   Forward declara
d17f0 74 69 6f 6e 20 72 65 71 75 69 72 65 64 20 62 79  tion required by
d1800 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28   incrVacuumStep(
d1810 29 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  ). */.static int
d1820 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61   allocateBtreePa
d1830 67 65 28 42 74 53 68 61 72 65 64 20 2a 2c 20 4d  ge(BtShared *, M
d1840 65 6d 50 61 67 65 20 2a 2a 2c 20 50 67 6e 6f 20  emPage **, Pgno 
d1850 2a 2c 20 50 67 6e 6f 2c 20 75 38 29 3b 0a 0a 2f  *, Pgno, u8);../
d1860 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20 73  *.** Perform a s
d1870 69 6e 67 6c 65 20 73 74 65 70 20 6f 66 20 61 6e  ingle step of an
d1880 20 69 6e 63 72 65 6d 65 6e 74 61 6c 2d 76 61 63   incremental-vac
d1890 75 75 6d 2e 20 49 66 20 73 75 63 63 65 73 73 66  uum. If successf
d18a0 75 6c 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 53 51  ul,.** return SQ
d18b0 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 74 68 65 72  LITE_OK. If ther
d18c0 65 20 69 73 20 6e 6f 20 77 6f 72 6b 20 74 6f 20  e is no work to 
d18d0 64 6f 20 28 61 6e 64 20 74 68 65 72 65 66 6f 72  do (and therefor
d18e0 65 20 6e 6f 0a 2a 2a 20 70 6f 69 6e 74 20 69 6e  e no.** point in
d18f0 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 66 75   calling this fu
d1900 6e 63 74 69 6f 6e 20 61 67 61 69 6e 29 2c 20 72  nction again), r
d1910 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e  eturn SQLITE_DON
d1920 45 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 72 65 20 73 70  E..**.** More sp
d1930 65 63 69 66 69 63 6c 79 2c 20 74 68 69 73 20 66  ecificly, this f
d1940 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73  unction attempts
d1950 20 74 6f 20 72 65 2d 6f 72 67 61 6e 69 7a 65 20   to re-organize 
d1960 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65  the .** database
d1970 20 73 6f 20 74 68 61 74 20 74 68 65 20 6c 61 73   so that the las
d1980 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 66 69  t page of the fi
d1990 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20  le currently in 
d19a0 75 73 65 0a 2a 2a 20 69 73 20 6e 6f 20 6c 6f 6e  use.** is no lon
d19b0 67 65 72 20 69 6e 20 75 73 65 2e 0a 2a 2a 0a 2a  ger in use..**.*
d19c0 2a 20 49 66 20 74 68 65 20 6e 46 69 6e 20 70 61  * If the nFin pa
d19d0 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a  rameter is non-z
d19e0 65 72 6f 2c 20 74 68 65 20 69 6d 70 6c 65 6d 65  ero, the impleme
d19f0 6e 74 61 74 69 6f 6e 20 61 73 73 75 6d 65 73 0a  ntation assumes.
d1a00 2a 2a 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c  ** that the call
d1a10 65 72 20 77 69 6c 6c 20 6b 65 65 70 20 63 61 6c  er will keep cal
d1a20 6c 69 6e 67 20 69 6e 63 72 56 61 63 75 75 6d 53  ling incrVacuumS
d1a30 74 65 70 28 29 20 75 6e 74 69 6c 0a 2a 2a 20 69  tep() until.** i
d1a40 74 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45  t returns SQLITE
d1a50 5f 44 4f 4e 45 20 6f 72 20 61 6e 20 65 72 72 6f  _DONE or an erro
d1a60 72 2c 20 61 6e 64 20 74 68 61 74 20 6e 46 69 6e  r, and that nFin
d1a70 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65   is the.** numbe
d1a80 72 20 6f 66 20 70 61 67 65 73 20 74 68 65 20 64  r of pages the d
d1a90 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 69 6c  atabase file wil
d1aa0 6c 20 63 6f 6e 74 61 69 6e 20 61 66 74 65 72 20  l contain after 
d1ab0 74 68 69 73 20 0a 2a 2a 20 70 72 6f 63 65 73 73  this .** process
d1ac0 20 69 73 20 63 6f 6d 70 6c 65 74 65 2e 0a 2a 2f   is complete..*/
d1ad0 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e 63 72  .static int incr
d1ae0 56 61 63 75 75 6d 53 74 65 70 28 42 74 53 68 61  VacuumStep(BtSha
d1af0 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 6e  red *pBt, Pgno n
d1b00 46 69 6e 29 7b 0a 20 20 50 67 6e 6f 20 69 4c 61  Fin){.  Pgno iLa
d1b10 73 74 50 67 3b 20 20 20 20 20 20 20 20 20 20 20  stPg;           
d1b20 20 20 2f 2a 20 4c 61 73 74 20 70 61 67 65 20 69    /* Last page i
d1b30 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a  n the database *
d1b40 2f 0a 20 20 50 67 6e 6f 20 6e 46 72 65 65 4c 69  /.  Pgno nFreeLi
d1b50 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  st;           /*
d1b60 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   Number of pages
d1b70 20 73 74 69 6c 6c 20 6f 6e 20 74 68 65 20 66 72   still on the fr
d1b80 65 65 2d 6c 69 73 74 20 2a 2f 0a 0a 20 20 69 4c  ee-list */..  iL
d1b90 61 73 74 50 67 20 3d 20 70 42 74 2d 3e 6e 54 72  astPg = pBt->nTr
d1ba0 75 6e 63 3b 0a 20 20 69 66 28 20 69 4c 61 73 74  unc;.  if( iLast
d1bb0 50 67 3d 3d 30 20 29 7b 0a 20 20 20 20 69 4c 61  Pg==0 ){.    iLa
d1bc0 73 74 50 67 20 3d 20 73 71 6c 69 74 65 33 50 61  stPg = sqlite3Pa
d1bd0 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74  gerPagecount(pBt
d1be0 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a  ->pPager);.  }..
d1bf0 20 20 69 66 28 20 21 50 54 52 4d 41 50 5f 49 53    if( !PTRMAP_IS
d1c00 50 41 47 45 28 70 42 74 2c 20 69 4c 61 73 74 50  PAGE(pBt, iLastP
d1c10 67 29 20 26 26 20 69 4c 61 73 74 50 67 21 3d 50  g) && iLastPg!=P
d1c20 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45  ENDING_BYTE_PAGE
d1c30 28 70 42 74 29 20 29 7b 0a 20 20 20 20 69 6e 74  (pBt) ){.    int
d1c40 20 72 63 3b 0a 20 20 20 20 75 38 20 65 54 79 70   rc;.    u8 eTyp
d1c50 65 3b 0a 20 20 20 20 50 67 6e 6f 20 69 50 74 72  e;.    Pgno iPtr
d1c60 50 61 67 65 3b 0a 0a 20 20 20 20 6e 46 72 65 65  Page;..    nFree
d1c70 4c 69 73 74 20 3d 20 67 65 74 34 62 79 74 65 28  List = get4byte(
d1c80 26 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44  &pBt->pPage1->aD
d1c90 61 74 61 5b 33 36 5d 29 3b 0a 20 20 20 20 69 66  ata[36]);.    if
d1ca0 28 20 6e 46 72 65 65 4c 69 73 74 3d 3d 30 20 7c  ( nFreeList==0 |
d1cb0 7c 20 6e 46 69 6e 3d 3d 69 4c 61 73 74 50 67 20  | nFin==iLastPg 
d1cc0 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
d1cd0 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20  SQLITE_DONE;.   
d1ce0 20 7d 0a 0a 20 20 20 20 72 63 20 3d 20 70 74 72   }..    rc = ptr
d1cf0 6d 61 70 47 65 74 28 70 42 74 2c 20 69 4c 61 73  mapGet(pBt, iLas
d1d00 74 50 67 2c 20 26 65 54 79 70 65 2c 20 26 69 50  tPg, &eType, &iP
d1d10 74 72 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28  trPage);.    if(
d1d20 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
d1d30 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  {.      return r
d1d40 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  c;.    }.    if(
d1d50 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52   eType==PTRMAP_R
d1d60 4f 4f 54 50 41 47 45 20 29 7b 0a 20 20 20 20 20  OOTPAGE ){.     
d1d70 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
d1d80 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
d1d90 20 7d 0a 0a 20 20 20 20 69 66 28 20 65 54 79 70   }..    if( eTyp
d1da0 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 41  e==PTRMAP_FREEPA
d1db0 47 45 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  GE ){.      if( 
d1dc0 6e 46 69 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20  nFin==0 ){.     
d1dd0 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65     /* Remove the
d1de0 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 66   page from the f
d1df0 69 6c 65 73 20 66 72 65 65 2d 6c 69 73 74 2e 20  iles free-list. 
d1e00 54 68 69 73 20 69 73 20 6e 6f 74 20 72 65 71 75  This is not requ
d1e10 69 72 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20  ired.        ** 
d1e20 69 66 20 6e 46 69 6e 20 69 73 20 6e 6f 6e 2d 7a  if nFin is non-z
d1e30 65 72 6f 2e 20 49 6e 20 74 68 61 74 20 63 61 73  ero. In that cas
d1e40 65 2c 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74  e, the free-list
d1e50 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 20 20 20   will be.       
d1e60 20 2a 2a 20 74 72 75 6e 63 61 74 65 64 20 74 6f   ** truncated to
d1e70 20 7a 65 72 6f 20 61 66 74 65 72 20 74 68 69 73   zero after this
d1e80 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
d1e90 73 2c 20 73 6f 20 69 74 20 64 6f 65 73 6e 27 74  s, so it doesn't
d1ea0 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d 61 74   .        ** mat
d1eb0 74 65 72 20 69 66 20 69 74 20 73 74 69 6c 6c 20  ter if it still 
d1ec0 63 6f 6e 74 61 69 6e 73 20 73 6f 6d 65 20 67 61  contains some ga
d1ed0 72 62 61 67 65 20 65 6e 74 72 69 65 73 2e 0a 20  rbage entries.. 
d1ee0 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
d1ef0 20 20 50 67 6e 6f 20 69 46 72 65 65 50 67 3b 0a    Pgno iFreePg;.
d1f00 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20          MemPage 
d1f10 2a 70 46 72 65 65 50 67 3b 0a 20 20 20 20 20 20  *pFreePg;.      
d1f20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42    rc = allocateB
d1f30 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 70  treePage(pBt, &p
d1f40 46 72 65 65 50 67 2c 20 26 69 46 72 65 65 50 67  FreePg, &iFreePg
d1f50 2c 20 69 4c 61 73 74 50 67 2c 20 31 29 3b 0a 20  , iLastPg, 1);. 
d1f60 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
d1f70 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
d1f80 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
d1f90 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
d1fa0 20 20 20 61 73 73 65 72 74 28 20 69 46 72 65 65     assert( iFree
d1fb0 50 67 3d 3d 69 4c 61 73 74 50 67 20 29 3b 0a 20  Pg==iLastPg );. 
d1fc0 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61         releasePa
d1fd0 67 65 28 70 46 72 65 65 50 67 29 3b 0a 20 20 20  ge(pFreePg);.   
d1fe0 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c 73 65 20     }.    } else 
d1ff0 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 46 72  {.      Pgno iFr
d2000 65 65 50 67 3b 20 20 20 20 20 20 20 20 20 20 20  eePg;           
d2010 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 66 72    /* Index of fr
d2020 65 65 20 70 61 67 65 20 74 6f 20 6d 6f 76 65 20  ee page to move 
d2030 70 4c 61 73 74 50 67 20 74 6f 20 2a 2f 0a 20 20  pLastPg to */.  
d2040 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4c 61      MemPage *pLa
d2050 73 74 50 67 3b 0a 0a 20 20 20 20 20 20 72 63 20  stPg;..      rc 
d2060 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  = sqlite3BtreeGe
d2070 74 50 61 67 65 28 70 42 74 2c 20 69 4c 61 73 74  tPage(pBt, iLast
d2080 50 67 2c 20 26 70 4c 61 73 74 50 67 2c 20 30 29  Pg, &pLastPg, 0)
d2090 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
d20a0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
d20b0 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
d20c0 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
d20d0 2a 20 49 66 20 6e 46 69 6e 20 69 73 20 7a 65 72  * If nFin is zer
d20e0 6f 2c 20 74 68 69 73 20 6c 6f 6f 70 20 72 75 6e  o, this loop run
d20f0 73 20 65 78 61 63 74 6c 79 20 6f 6e 63 65 20 61  s exactly once a
d2100 6e 64 20 70 61 67 65 20 70 4c 61 73 74 50 67 0a  nd page pLastPg.
d2110 20 20 20 20 20 20 2a 2a 20 69 73 20 73 77 61 70        ** is swap
d2120 70 65 64 20 77 69 74 68 20 74 68 65 20 66 69 72  ped with the fir
d2130 73 74 20 66 72 65 65 20 70 61 67 65 20 70 75 6c  st free page pul
d2140 6c 65 64 20 6f 66 66 20 74 68 65 20 66 72 65 65  led off the free
d2150 20 6c 69 73 74 2e 0a 20 20 20 20 20 20 2a 2a 0a   list..      **.
d2160 20 20 20 20 20 20 2a 2a 20 4f 6e 20 74 68 65 20        ** On the 
d2170 6f 74 68 65 72 20 68 61 6e 64 2c 20 69 66 20 6e  other hand, if n
d2180 46 69 6e 20 69 73 20 67 72 65 61 74 65 72 20 74  Fin is greater t
d2190 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6b  han zero, then k
d21a0 65 65 70 0a 20 20 20 20 20 20 2a 2a 20 6c 6f 6f  eep.      ** loo
d21b0 70 69 6e 67 20 75 6e 74 69 6c 20 61 20 66 72 65  ping until a fre
d21c0 65 2d 70 61 67 65 20 6c 6f 63 61 74 65 64 20 77  e-page located w
d21d0 69 74 68 69 6e 20 74 68 65 20 66 69 72 73 74 20  ithin the first 
d21e0 6e 46 69 6e 20 70 61 67 65 73 0a 20 20 20 20 20  nFin pages.     
d21f0 20 2a 2a 20 6f 66 20 74 68 65 20 66 69 6c 65 20   ** of the file 
d2200 69 73 20 66 6f 75 6e 64 2e 0a 20 20 20 20 20 20  is found..      
d2210 2a 2f 0a 20 20 20 20 20 20 64 6f 20 7b 0a 20 20  */.      do {.  
d2220 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70        MemPage *p
d2230 46 72 65 65 50 67 3b 0a 20 20 20 20 20 20 20 20  FreePg;.        
d2240 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72  rc = allocateBtr
d2250 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 46 72  eePage(pBt, &pFr
d2260 65 65 50 67 2c 20 26 69 46 72 65 65 50 67 2c 20  eePg, &iFreePg, 
d2270 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69  0, 0);.        i
d2280 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
d2290 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65   ){.          re
d22a0 6c 65 61 73 65 50 61 67 65 28 70 4c 61 73 74 50  leasePage(pLastP
d22b0 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65  g);.          re
d22c0 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20  turn rc;.       
d22d0 20 7d 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61   }.        relea
d22e0 73 65 50 61 67 65 28 70 46 72 65 65 50 67 29 3b  sePage(pFreePg);
d22f0 0a 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20 6e  .      }while( n
d2300 46 69 6e 21 3d 30 20 26 26 20 69 46 72 65 65 50  Fin!=0 && iFreeP
d2310 67 3e 6e 46 69 6e 20 29 3b 0a 20 20 20 20 20 20  g>nFin );.      
d2320 61 73 73 65 72 74 28 20 69 46 72 65 65 50 67 3c  assert( iFreePg<
d2330 69 4c 61 73 74 50 67 20 29 3b 0a 20 20 20 20 20  iLastPg );.     
d2340 20 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c   .      rc = sql
d2350 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
d2360 4c 61 73 74 50 67 2d 3e 70 44 62 50 61 67 65 29  LastPg->pDbPage)
d2370 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
d2380 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
d2390 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
d23a0 20 20 20 20 20 20 7d 20 0a 20 20 20 20 20 20 72        } .      r
d23b0 63 20 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65  c = relocatePage
d23c0 28 70 42 74 2c 20 70 4c 61 73 74 50 67 2c 20 65  (pBt, pLastPg, e
d23d0 54 79 70 65 2c 20 69 50 74 72 50 61 67 65 2c 20  Type, iPtrPage, 
d23e0 69 46 72 65 65 50 67 29 3b 0a 20 20 20 20 20 20  iFreePg);.      
d23f0 72 65 6c 65 61 73 65 50 61 67 65 28 70 4c 61 73  releasePage(pLas
d2400 74 50 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20  tPg);.      if( 
d2410 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
d2420 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
d2430 72 63 3b 0a 20 20 20 20 20 20 7d 20 0a 20 20 20  rc;.      } .   
d2440 20 7d 0a 20 20 7d 0a 0a 20 20 70 42 74 2d 3e 6e   }.  }..  pBt->n
d2450 54 72 75 6e 63 20 3d 20 69 4c 61 73 74 50 67 20  Trunc = iLastPg 
d2460 2d 20 31 3b 0a 20 20 77 68 69 6c 65 28 20 70 42  - 1;.  while( pB
d2470 74 2d 3e 6e 54 72 75 6e 63 3d 3d 50 45 4e 44 49  t->nTrunc==PENDI
d2480 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74  NG_BYTE_PAGE(pBt
d2490 29 7c 7c 50 54 52 4d 41 50 5f 49 53 50 41 47 45  )||PTRMAP_ISPAGE
d24a0 28 70 42 74 2c 20 70 42 74 2d 3e 6e 54 72 75 6e  (pBt, pBt->nTrun
d24b0 63 29 20 29 7b 0a 20 20 20 20 70 42 74 2d 3e 6e  c) ){.    pBt->n
d24c0 54 72 75 6e 63 2d 2d 3b 0a 20 20 7d 0a 20 20 72  Trunc--;.  }.  r
d24d0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
d24e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 77 72 69 74  .}../*.** A writ
d24f0 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75  e-transaction mu
d2500 73 74 20 62 65 20 6f 70 65 6e 65 64 20 62 65 66  st be opened bef
d2510 6f 72 65 20 63 61 6c 6c 69 6e 67 20 74 68 69 73  ore calling this
d2520 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 49 74   function..** It
d2530 20 70 65 72 66 6f 72 6d 73 20 61 20 73 69 6e 67   performs a sing
d2540 6c 65 20 75 6e 69 74 20 6f 66 20 77 6f 72 6b 20  le unit of work 
d2550 74 6f 77 61 72 64 73 20 61 6e 20 69 6e 63 72 65  towards an incre
d2560 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 2e 0a 2a  mental vacuum..*
d2570 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 63 72  *.** If the incr
d2580 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 69  emental vacuum i
d2590 73 20 66 69 6e 69 73 68 65 64 20 61 66 74 65 72  s finished after
d25a0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 68   this function h
d25b0 61 73 20 72 75 6e 2c 0a 2a 2a 20 53 51 4c 49 54  as run,.** SQLIT
d25c0 45 5f 44 4f 4e 45 20 69 73 20 72 65 74 75 72 6e  E_DONE is return
d25d0 65 64 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 74  ed. If it is not
d25e0 20 66 69 6e 69 73 68 65 64 2c 20 62 75 74 20 6e   finished, but n
d25f0 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64 2c  o error occured,
d2600 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  .** SQLITE_OK is
d2610 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72   returned. Other
d2620 77 69 73 65 20 61 6e 20 53 51 4c 69 74 65 20 65  wise an SQLite e
d2630 72 72 6f 72 20 63 6f 64 65 2e 20 0a 2a 2f 0a 53  rror code. .*/.S
d2640 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
d2650 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e  t sqlite3BtreeIn
d2660 63 72 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a  crVacuum(Btree *
d2670 70 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  p){.  BtShared *
d2680 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
d2690 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54  assert( pBt->inT
d26a0 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e  ransaction==TRAN
d26b0 53 5f 57 52 49 54 45 20 26 26 20 70 2d 3e 69 6e  S_WRITE && p->in
d26c0 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49  Trans==TRANS_WRI
d26d0 54 45 20 29 3b 0a 20 20 69 66 28 20 21 70 42 74  TE );.  if( !pBt
d26e0 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a  ->autoVacuum ){.
d26f0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
d2700 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 69 6e  E_DONE;.  }.  in
d2710 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66  validateAllOverf
d2720 6c 6f 77 43 61 63 68 65 28 70 42 74 29 3b 0a 20  lowCache(pBt);. 
d2730 20 72 65 74 75 72 6e 20 69 6e 63 72 56 61 63 75   return incrVacu
d2740 75 6d 53 74 65 70 28 70 42 74 2c 20 30 29 3b 0a  umStep(pBt, 0);.
d2750 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
d2760 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
d2770 70 72 69 6f 72 20 74 6f 20 73 71 6c 69 74 65 33  prior to sqlite3
d2780 50 61 67 65 72 43 6f 6d 6d 69 74 20 77 68 65 6e  PagerCommit when
d2790 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a   a transaction.*
d27a0 2a 20 69 73 20 63 6f 6d 6d 69 74 65 64 20 66 6f  * is commited fo
d27b0 72 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d  r an auto-vacuum
d27c0 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a   database..**.**
d27d0 20 49 66 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73   If SQLITE_OK is
d27e0 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65 6e 20   returned, then 
d27f0 2a 70 6e 54 72 75 6e 63 20 69 73 20 73 65 74 20  *pnTrunc is set 
d2800 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  to the number of
d2810 20 70 61 67 65 73 0a 2a 2a 20 74 68 65 20 64 61   pages.** the da
d2820 74 61 62 61 73 65 20 66 69 6c 65 20 73 68 6f 75  tabase file shou
d2830 6c 64 20 62 65 20 74 72 75 6e 63 61 74 65 64 20  ld be truncated 
d2840 74 6f 20 64 75 72 69 6e 67 20 74 68 65 20 63 6f  to during the co
d2850 6d 6d 69 74 20 70 72 6f 63 65 73 73 2e 20 0a 2a  mmit process. .*
d2860 2a 20 69 2e 65 2e 20 74 68 65 20 64 61 74 61 62  * i.e. the datab
d2870 61 73 65 20 68 61 73 20 62 65 65 6e 20 72 65 6f  ase has been reo
d2880 72 67 61 6e 69 7a 65 64 20 73 6f 20 74 68 61 74  rganized so that
d2890 20 6f 6e 6c 79 20 74 68 65 20 66 69 72 73 74 20   only the first 
d28a0 2a 70 6e 54 72 75 6e 63 0a 2a 2a 20 70 61 67 65  *pnTrunc.** page
d28b0 73 20 61 72 65 20 69 6e 20 75 73 65 2e 0a 2a 2f  s are in use..*/
d28c0 0a 73 74 61 74 69 63 20 69 6e 74 20 61 75 74 6f  .static int auto
d28d0 56 61 63 75 75 6d 43 6f 6d 6d 69 74 28 42 74 53  VacuumCommit(BtS
d28e0 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f  hared *pBt, Pgno
d28f0 20 2a 70 6e 54 72 75 6e 63 29 7b 0a 20 20 69 6e   *pnTrunc){.  in
d2900 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
d2910 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65  ;.  Pager *pPage
d2920 72 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 72 3b  r = pBt->pPager;
d2930 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
d2940 20 20 69 6e 74 20 6e 52 65 66 20 3d 20 73 71 6c    int nRef = sql
d2950 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e  ite3PagerRefcoun
d2960 74 28 70 50 61 67 65 72 29 3b 0a 23 65 6e 64 69  t(pPager);.#endi
d2970 66 0a 0a 20 20 69 6e 76 61 6c 69 64 61 74 65 41  f..  invalidateA
d2980 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28  llOverflowCache(
d2990 70 42 74 29 3b 0a 20 20 61 73 73 65 72 74 28 70  pBt);.  assert(p
d29a0 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 3b  Bt->autoVacuum);
d29b0 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 69 6e 63  .  if( !pBt->inc
d29c0 72 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 50  rVacuum ){.    P
d29d0 67 6e 6f 20 6e 46 69 6e 20 3d 20 30 3b 0a 0a 20  gno nFin = 0;.. 
d29e0 20 20 20 69 66 28 20 70 42 74 2d 3e 6e 54 72 75     if( pBt->nTru
d29f0 6e 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 50  nc==0 ){.      P
d2a00 67 6e 6f 20 6e 46 72 65 65 3b 0a 20 20 20 20 20  gno nFree;.     
d2a10 20 50 67 6e 6f 20 6e 50 74 72 6d 61 70 3b 0a 20   Pgno nPtrmap;. 
d2a20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 70       const int p
d2a30 67 73 7a 20 3d 20 70 42 74 2d 3e 70 61 67 65 53  gsz = pBt->pageS
d2a40 69 7a 65 3b 0a 20 20 20 20 20 20 50 67 6e 6f 20  ize;.      Pgno 
d2a50 6e 4f 72 69 67 20 3d 20 73 71 6c 69 74 65 33 50  nOrig = sqlite3P
d2a60 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42  agerPagecount(pB
d2a70 74 2d 3e 70 50 61 67 65 72 29 3b 0a 0a 20 20 20  t->pPager);..   
d2a80 20 20 20 69 66 28 20 50 54 52 4d 41 50 5f 49 53     if( PTRMAP_IS
d2a90 50 41 47 45 28 70 42 74 2c 20 6e 4f 72 69 67 29  PAGE(pBt, nOrig)
d2aa0 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
d2ab0 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
d2ac0 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d 0a  T_BKPT;.      }.
d2ad0 20 20 20 20 20 20 69 66 28 20 6e 4f 72 69 67 3d        if( nOrig=
d2ae0 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41  =PENDING_BYTE_PA
d2af0 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20  GE(pBt) ){.     
d2b00 20 20 20 6e 4f 72 69 67 2d 2d 3b 0a 20 20 20 20     nOrig--;.    
d2b10 20 20 7d 0a 20 20 20 20 20 20 6e 46 72 65 65 20    }.      nFree 
d2b20 3d 20 67 65 74 34 62 79 74 65 28 26 70 42 74 2d  = get4byte(&pBt-
d2b30 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  >pPage1->aData[3
d2b40 36 5d 29 3b 0a 20 20 20 20 20 20 6e 50 74 72 6d  6]);.      nPtrm
d2b50 61 70 20 3d 20 28 6e 46 72 65 65 2d 6e 4f 72 69  ap = (nFree-nOri
d2b60 67 2b 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28  g+PTRMAP_PAGENO(
d2b70 70 42 74 2c 20 6e 4f 72 69 67 29 2b 70 67 73 7a  pBt, nOrig)+pgsz
d2b80 2f 35 29 2f 28 70 67 73 7a 2f 35 29 3b 0a 20 20  /5)/(pgsz/5);.  
d2b90 20 20 20 20 6e 46 69 6e 20 3d 20 6e 4f 72 69 67      nFin = nOrig
d2ba0 20 2d 20 6e 46 72 65 65 20 2d 20 6e 50 74 72 6d   - nFree - nPtrm
d2bb0 61 70 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 4f  ap;.      if( nO
d2bc0 72 69 67 3e 50 45 4e 44 49 4e 47 5f 42 59 54 45  rig>PENDING_BYTE
d2bd0 5f 50 41 47 45 28 70 42 74 29 20 26 26 20 6e 46  _PAGE(pBt) && nF
d2be0 69 6e 3c 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45  in<=PENDING_BYTE
d2bf0 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20  _PAGE(pBt) ){.  
d2c00 20 20 20 20 20 20 6e 46 69 6e 2d 2d 3b 0a 20 20        nFin--;.  
d2c10 20 20 20 20 7d 0a 20 20 20 20 20 20 77 68 69 6c      }.      whil
d2c20 65 28 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45  e( PTRMAP_ISPAGE
d2c30 28 70 42 74 2c 20 6e 46 69 6e 29 20 7c 7c 20 6e  (pBt, nFin) || n
d2c40 46 69 6e 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54  Fin==PENDING_BYT
d2c50 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20  E_PAGE(pBt) ){. 
d2c60 20 20 20 20 20 20 20 6e 46 69 6e 2d 2d 3b 0a 20         nFin--;. 
d2c70 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
d2c80 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c    while( rc==SQL
d2c90 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
d2ca0 72 63 20 3d 20 69 6e 63 72 56 61 63 75 75 6d 53  rc = incrVacuumS
d2cb0 74 65 70 28 70 42 74 2c 20 6e 46 69 6e 29 3b 0a  tep(pBt, nFin);.
d2cc0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63      }.    if( rc
d2cd0 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b  ==SQLITE_DONE ){
d2ce0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 6e 46  .      assert(nF
d2cf0 69 6e 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 6e 54  in==0 || pBt->nT
d2d00 72 75 6e 63 3d 3d 30 20 7c 7c 20 6e 46 69 6e 3c  runc==0 || nFin<
d2d10 3d 70 42 74 2d 3e 6e 54 72 75 6e 63 29 3b 0a 20  =pBt->nTrunc);. 
d2d20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
d2d30 5f 4f 4b 3b 0a 20 20 20 20 20 20 69 66 28 20 70  _OK;.      if( p
d2d40 42 74 2d 3e 6e 54 72 75 6e 63 20 29 7b 0a 20 20  Bt->nTrunc ){.  
d2d50 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
d2d60 65 33 50 61 67 65 72 57 72 69 74 65 28 70 42 74  e3PagerWrite(pBt
d2d70 2d 3e 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67  ->pPage1->pDbPag
d2d80 65 29 3b 0a 20 20 20 20 20 20 20 20 70 75 74 34  e);.        put4
d2d90 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65  byte(&pBt->pPage
d2da0 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 30 29  1->aData[32], 0)
d2db0 3b 0a 20 20 20 20 20 20 20 20 70 75 74 34 62 79  ;.        put4by
d2dc0 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d  te(&pBt->pPage1-
d2dd0 3e 61 44 61 74 61 5b 33 36 5d 2c 20 30 29 3b 0a  >aData[36], 0);.
d2de0 20 20 20 20 20 20 20 20 70 42 74 2d 3e 6e 54 72          pBt->nTr
d2df0 75 6e 63 20 3d 20 6e 46 69 6e 3b 0a 20 20 20 20  unc = nFin;.    
d2e00 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
d2e10 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
d2e20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
d2e30 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70 50  PagerRollback(pP
d2e40 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ager);.    }.  }
d2e50 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
d2e60 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 6e  TE_OK ){.    *pn
d2e70 54 72 75 6e 63 20 3d 20 70 42 74 2d 3e 6e 54 72  Trunc = pBt->nTr
d2e80 75 6e 63 3b 0a 20 20 20 20 70 42 74 2d 3e 6e 54  unc;.    pBt->nT
d2e90 72 75 6e 63 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  runc = 0;.  }.  
d2ea0 61 73 73 65 72 74 28 20 6e 52 65 66 3d 3d 73 71  assert( nRef==sq
d2eb0 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75  lite3PagerRefcou
d2ec0 6e 74 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20  nt(pPager) );.  
d2ed0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65  return rc;.}..#e
d2ee0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ndif../*.** This
d2ef0 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 74 68   routine does th
d2f00 65 20 66 69 72 73 74 20 70 68 61 73 65 20 6f 66  e first phase of
d2f10 20 61 20 74 77 6f 2d 70 68 61 73 65 20 63 6f 6d   a two-phase com
d2f20 6d 69 74 2e 20 20 54 68 69 73 20 72 6f 75 74 69  mit.  This routi
d2f30 6e 65 0a 2a 2a 20 63 61 75 73 65 73 20 61 20 72  ne.** causes a r
d2f40 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20  ollback journal 
d2f50 74 6f 20 62 65 20 63 72 65 61 74 65 64 20 28 69  to be created (i
d2f60 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 61 6c  f it does not al
d2f70 72 65 61 64 79 20 65 78 69 73 74 29 0a 2a 2a 20  ready exist).** 
d2f80 61 6e 64 20 70 6f 70 75 6c 61 74 65 64 20 77 69  and populated wi
d2f90 74 68 20 65 6e 6f 75 67 68 20 69 6e 66 6f 72 6d  th enough inform
d2fa0 61 74 69 6f 6e 20 73 6f 20 74 68 61 74 20 69 66  ation so that if
d2fb0 20 61 20 70 6f 77 65 72 20 6c 6f 73 73 20 6f 63   a power loss oc
d2fc0 63 75 72 73 0a 2a 2a 20 74 68 65 20 64 61 74 61  curs.** the data
d2fd0 62 61 73 65 20 63 61 6e 20 62 65 20 72 65 73 74  base can be rest
d2fe0 6f 72 65 64 20 74 6f 20 69 74 73 20 6f 72 69 67  ored to its orig
d2ff0 69 6e 61 6c 20 73 74 61 74 65 20 62 79 20 70 6c  inal state by pl
d3000 61 79 69 6e 67 20 62 61 63 6b 0a 2a 2a 20 74 68  aying back.** th
d3010 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 6e  e journal.  Then
d3020 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
d3030 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 72 65   the journal are
d3040 20 66 6c 75 73 68 65 64 20 6f 75 74 20 74 6f 0a   flushed out to.
d3050 2a 2a 20 74 68 65 20 64 69 73 6b 2e 20 20 41 66  ** the disk.  Af
d3060 74 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ter the journal 
d3070 69 73 20 73 61 66 65 6c 79 20 6f 6e 20 6f 78 69  is safely on oxi
d3080 64 65 2c 20 74 68 65 20 63 68 61 6e 67 65 73 20  de, the changes 
d3090 74 6f 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61  to the.** databa
d30a0 73 65 20 61 72 65 20 77 72 69 74 74 65 6e 20 69  se are written i
d30b0 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65  nto the database
d30c0 20 66 69 6c 65 20 61 6e 64 20 66 6c 75 73 68 65   file and flushe
d30d0 64 20 74 6f 20 6f 78 69 64 65 2e 0a 2a 2a 20 41  d to oxide..** A
d30e0 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 69  t the end of thi
d30f0 73 20 63 61 6c 6c 2c 20 74 68 65 20 72 6f 6c 6c  s call, the roll
d3100 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 73 74 69  back journal sti
d3110 6c 6c 20 65 78 69 73 74 73 20 6f 6e 20 74 68 65  ll exists on the
d3120 0a 2a 2a 20 64 69 73 6b 20 61 6e 64 20 77 65 20  .** disk and we 
d3130 61 72 65 20 73 74 69 6c 6c 20 68 6f 6c 64 69 6e  are still holdin
d3140 67 20 61 6c 6c 20 6c 6f 63 6b 73 2c 20 73 6f 20  g all locks, so 
d3150 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
d3160 68 61 73 20 6e 6f 74 0a 2a 2a 20 63 6f 6d 6d 69  has not.** commi
d3170 74 74 65 64 2e 20 20 53 65 65 20 73 71 6c 69 74  tted.  See sqlit
d3180 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 28 29 20  e3BtreeCommit() 
d3190 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 70  for the second p
d31a0 68 61 73 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63  hase of the.** c
d31b0 6f 6d 6d 69 74 20 70 72 6f 63 65 73 73 2e 0a 2a  ommit process..*
d31c0 2a 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 69  *.** This call i
d31d0 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 6e 6f 20  s a no-op if no 
d31e0 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f  write-transactio
d31f0 6e 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61  n is currently a
d3200 63 74 69 76 65 20 6f 6e 20 70 42 74 2e 0a 2a 2a  ctive on pBt..**
d3210 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 73  .** Otherwise, s
d3220 79 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 65  ync the database
d3230 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 62 74   file for the bt
d3240 72 65 65 20 70 42 74 2e 20 7a 4d 61 73 74 65 72  ree pBt. zMaster
d3250 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68   points to.** th
d3260 65 20 6e 61 6d 65 20 6f 66 20 61 20 6d 61 73 74  e name of a mast
d3270 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  er journal file 
d3280 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 77  that should be w
d3290 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 0a  ritten into the.
d32a0 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f  ** individual jo
d32b0 75 72 6e 61 6c 20 66 69 6c 65 2c 20 6f 72 20 69  urnal file, or i
d32c0 73 20 4e 55 4c 4c 2c 20 69 6e 64 69 63 61 74 69  s NULL, indicati
d32d0 6e 67 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75  ng no master jou
d32e0 72 6e 61 6c 20 66 69 6c 65 20 0a 2a 2a 20 28 73  rnal file .** (s
d32f0 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 74  ingle database t
d3300 72 61 6e 73 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a  ransaction)..**.
d3310 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 69 73 20  ** When this is 
d3320 63 61 6c 6c 65 64 2c 20 74 68 65 20 6d 61 73 74  called, the mast
d3330 65 72 20 6a 6f 75 72 6e 61 6c 20 73 68 6f 75 6c  er journal shoul
d3340 64 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 62  d already have b
d3350 65 65 6e 0a 2a 2a 20 63 72 65 61 74 65 64 2c 20  een.** created, 
d3360 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 74  populated with t
d3370 68 69 73 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e  his journal poin
d3380 74 65 72 20 61 6e 64 20 73 79 6e 63 65 64 20 74  ter and synced t
d3390 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 4f 6e  o disk..**.** On
d33a0 63 65 20 74 68 69 73 20 69 73 20 72 6f 75 74 69  ce this is routi
d33b0 6e 65 20 68 61 73 20 72 65 74 75 72 6e 65 64 2c  ne has returned,
d33c0 20 74 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20   the only thing 
d33d0 72 65 71 75 69 72 65 64 20 74 6f 20 63 6f 6d 6d  required to comm
d33e0 69 74 0a 2a 2a 20 74 68 65 20 77 72 69 74 65 2d  it.** the write-
d33f0 74 72 61 6e 73 61 63 74 69 6f 6e 20 66 6f 72 20  transaction for 
d3400 74 68 69 73 20 64 61 74 61 62 61 73 65 20 66 69  this database fi
d3410 6c 65 20 69 73 20 74 6f 20 64 65 6c 65 74 65 20  le is to delete 
d3420 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2f 0a  the journal..*/.
d3430 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
d3440 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
d3450 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 42 74  ommitPhaseOne(Bt
d3460 72 65 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68  ree *p, const ch
d3470 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20  ar *zMaster){.  
d3480 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
d3490 4f 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54  OK;.  if( p->inT
d34a0 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54  rans==TRANS_WRIT
d34b0 45 20 29 7b 0a 20 20 20 20 42 74 53 68 61 72 65  E ){.    BtShare
d34c0 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
d34d0 0a 20 20 20 20 50 67 6e 6f 20 6e 54 72 75 6e 63  .    Pgno nTrunc
d34e0 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 51   = 0;.#ifndef SQ
d34f0 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
d3500 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 42 74  CUUM.    if( pBt
d3510 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a  ->autoVacuum ){.
d3520 20 20 20 20 20 20 72 63 20 3d 20 61 75 74 6f 56        rc = autoV
d3530 61 63 75 75 6d 43 6f 6d 6d 69 74 28 70 42 74 2c  acuumCommit(pBt,
d3540 20 26 6e 54 72 75 6e 63 29 3b 20 0a 20 20 20 20   &nTrunc); .    
d3550 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
d3560 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  _OK ){.        r
d3570 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
d3580 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  }.    }.#endif. 
d3590 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
d35a0 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f  agerCommitPhaseO
d35b0 6e 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20  ne(pBt->pPager, 
d35c0 7a 4d 61 73 74 65 72 2c 20 6e 54 72 75 6e 63 29  zMaster, nTrunc)
d35d0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
d35e0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d  c;.}../*.** Comm
d35f0 69 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  it the transacti
d3600 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20  on currently in 
d3610 70 72 6f 67 72 65 73 73 2e 0a 2a 2a 0a 2a 2a 20  progress..**.** 
d3620 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6d 70  This routine imp
d3630 6c 65 6d 65 6e 74 73 20 74 68 65 20 73 65 63 6f  lements the seco
d3640 6e 64 20 70 68 61 73 65 20 6f 66 20 61 20 32 2d  nd phase of a 2-
d3650 70 68 61 73 65 20 63 6f 6d 6d 69 74 2e 20 20 54  phase commit.  T
d3660 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72  he.** sqlite3Btr
d3670 65 65 53 79 6e 63 28 29 20 72 6f 75 74 69 6e 65  eeSync() routine
d3680 20 64 6f 65 73 20 74 68 65 20 66 69 72 73 74 20   does the first 
d3690 70 68 61 73 65 20 61 6e 64 20 73 68 6f 75 6c 64  phase and should
d36a0 20 62 65 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 70   be invoked.** p
d36b0 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 20  rior to calling 
d36c0 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 54  this routine.  T
d36d0 68 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 53  he sqlite3BtreeS
d36e0 79 6e 63 28 29 20 72 6f 75 74 69 6e 65 20 64 69  ync() routine di
d36f0 64 0a 2a 2a 20 61 6c 6c 20 74 68 65 20 77 6f 72  d.** all the wor
d3700 6b 20 6f 66 20 77 72 69 74 69 6e 67 20 69 6e 66  k of writing inf
d3710 6f 72 6d 61 74 69 6f 6e 20 6f 75 74 20 74 6f 20  ormation out to 
d3720 64 69 73 6b 20 61 6e 64 20 66 6c 75 73 68 69 6e  disk and flushin
d3730 67 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74  g the.** content
d3740 73 20 73 6f 20 74 68 61 74 20 74 68 65 79 20 61  s so that they a
d3750 72 65 20 77 72 69 74 74 65 6e 20 6f 6e 74 6f 20  re written onto 
d3760 74 68 65 20 64 69 73 6b 20 70 6c 61 74 74 65 72  the disk platter
d3770 2e 20 20 41 6c 6c 20 74 68 69 73 0a 2a 2a 20 72  .  All this.** r
d3780 6f 75 74 69 6e 65 20 68 61 73 20 74 6f 20 64 6f  outine has to do
d3790 20 69 73 20 64 65 6c 65 74 65 20 6f 72 20 74 72   is delete or tr
d37a0 75 6e 63 61 74 65 20 74 68 65 20 72 6f 6c 6c 62  uncate the rollb
d37b0 61 63 6b 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 28  ack journal.** (
d37c0 77 68 69 63 68 20 63 61 75 73 65 73 20 74 68 65  which causes the
d37d0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20   transaction to 
d37e0 63 6f 6d 6d 69 74 29 20 61 6e 64 20 64 72 6f 70  commit) and drop
d37f0 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 54 68   locks..**.** Th
d3800 69 73 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 20  is will release 
d3810 74 68 65 20 77 72 69 74 65 20 6c 6f 63 6b 20 6f  the write lock o
d3820 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
d3830 69 6c 65 2e 20 20 49 66 20 74 68 65 72 65 0a 2a  ile.  If there.*
d3840 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20  * are no active 
d3850 63 75 72 73 6f 72 73 2c 20 69 74 20 61 6c 73 6f  cursors, it also
d3860 20 72 65 6c 65 61 73 65 73 20 74 68 65 20 72 65   releases the re
d3870 61 64 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49  ad lock..*/.SQLI
d3880 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
d3890 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69  qlite3BtreeCommi
d38a0 74 50 68 61 73 65 54 77 6f 28 42 74 72 65 65 20  tPhaseTwo(Btree 
d38b0 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20  *p){.  BtShared 
d38c0 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a  *pBt = p->pBt;..
d38d0 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 79    btreeIntegrity
d38e0 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  (p);..  /* If th
d38f0 65 20 68 61 6e 64 6c 65 20 68 61 73 20 61 20 77  e handle has a w
d3900 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e  rite-transaction
d3910 20 6f 70 65 6e 2c 20 63 6f 6d 6d 69 74 20 74 68   open, commit th
d3920 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 73 20  e shared-btrees 
d3930 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f  .  ** transactio
d3940 6e 20 61 6e 64 20 73 65 74 20 74 68 65 20 73 68  n and set the sh
d3950 61 72 65 64 20 73 74 61 74 65 20 74 6f 20 54 52  ared state to TR
d3960 41 4e 53 5f 52 45 41 44 2e 0a 20 20 2a 2f 0a 20  ANS_READ..  */. 
d3970 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d   if( p->inTrans=
d3980 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a  =TRANS_WRITE ){.
d3990 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20      int rc;.    
d39a0 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54  assert( pBt->inT
d39b0 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e  ransaction==TRAN
d39c0 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 20 20 61  S_WRITE );.    a
d39d0 73 73 65 72 74 28 20 70 42 74 2d 3e 6e 54 72 61  ssert( pBt->nTra
d39e0 6e 73 61 63 74 69 6f 6e 3e 30 20 29 3b 0a 20 20  nsaction>0 );.  
d39f0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
d3a00 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77  gerCommitPhaseTw
d3a10 6f 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a  o(pBt->pPager);.
d3a20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
d3a30 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
d3a40 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
d3a50 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73      pBt->inTrans
d3a60 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 52  action = TRANS_R
d3a70 45 41 44 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e  EAD;.    pBt->in
d3a80 53 74 6d 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  Stmt = 0;.  }.  
d3a90 75 6e 6c 6f 63 6b 41 6c 6c 54 61 62 6c 65 73 28  unlockAllTables(
d3aa0 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65  p);..  /* If the
d3ab0 20 68 61 6e 64 6c 65 20 68 61 73 20 61 6e 79 20   handle has any 
d3ac0 6b 69 6e 64 20 6f 66 20 74 72 61 6e 73 61 63 74  kind of transact
d3ad0 69 6f 6e 20 6f 70 65 6e 2c 20 64 65 63 72 65 6d  ion open, decrem
d3ae0 65 6e 74 20 74 68 65 20 74 72 61 6e 73 61 63 74  ent the transact
d3af0 69 6f 6e 0a 20 20 2a 2a 20 63 6f 75 6e 74 20 6f  ion.  ** count o
d3b00 66 20 74 68 65 20 73 68 61 72 65 64 20 62 74 72  f the shared btr
d3b10 65 65 2e 20 49 66 20 74 68 65 20 74 72 61 6e 73  ee. If the trans
d3b20 61 63 74 69 6f 6e 20 63 6f 75 6e 74 20 72 65 61  action count rea
d3b30 63 68 65 73 20 30 2c 20 73 65 74 0a 20 20 2a 2a  ches 0, set.  **
d3b40 20 74 68 65 20 73 68 61 72 65 64 20 73 74 61 74   the shared stat
d3b50 65 20 74 6f 20 54 52 41 4e 53 5f 4e 4f 4e 45 2e  e to TRANS_NONE.
d3b60 20 54 68 65 20 75 6e 6c 6f 63 6b 42 74 72 65 65   The unlockBtree
d3b70 49 66 55 6e 75 73 65 64 28 29 20 63 61 6c 6c 20  IfUnused() call 
d3b80 62 65 6c 6f 77 0a 20 20 2a 2a 20 77 69 6c 6c 20  below.  ** will 
d3b90 75 6e 6c 6f 63 6b 20 74 68 65 20 70 61 67 65 72  unlock the pager
d3ba0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e  ..  */.  if( p->
d3bb0 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 4e  inTrans!=TRANS_N
d3bc0 4f 4e 45 20 29 7b 0a 20 20 20 20 70 42 74 2d 3e  ONE ){.    pBt->
d3bd0 6e 54 72 61 6e 73 61 63 74 69 6f 6e 2d 2d 3b 0a  nTransaction--;.
d3be0 20 20 20 20 69 66 28 20 30 3d 3d 70 42 74 2d 3e      if( 0==pBt->
d3bf0 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a  nTransaction ){.
d3c00 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61        pBt->inTra
d3c10 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53  nsaction = TRANS
d3c20 5f 4e 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d  _NONE;.    }.  }
d3c30 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 68  ..  /* Set the h
d3c40 61 6e 64 6c 65 73 20 63 75 72 72 65 6e 74 20 74  andles current t
d3c50 72 61 6e 73 61 63 74 69 6f 6e 20 73 74 61 74 65  ransaction state
d3c60 20 74 6f 20 54 52 41 4e 53 5f 4e 4f 4e 45 20 61   to TRANS_NONE a
d3c70 6e 64 20 75 6e 6c 6f 63 6b 0a 20 20 2a 2a 20 74  nd unlock.  ** t
d3c80 68 65 20 70 61 67 65 72 20 69 66 20 74 68 69 73  he pager if this
d3c90 20 63 61 6c 6c 20 63 6c 6f 73 65 64 20 74 68 65   call closed the
d3ca0 20 6f 6e 6c 79 20 72 65 61 64 20 6f 72 20 77 72   only read or wr
d3cb0 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  ite transaction.
d3cc0 0a 20 20 2a 2f 0a 20 20 70 2d 3e 69 6e 54 72 61  .  */.  p->inTra
d3cd0 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b  ns = TRANS_NONE;
d3ce0 0a 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66  .  unlockBtreeIf
d3cf0 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 0a 20 20  Unused(pBt);..  
d3d00 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70  btreeIntegrity(p
d3d10 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
d3d20 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
d3d30 44 6f 20 62 6f 74 68 20 70 68 61 73 65 73 20 6f  Do both phases o
d3d40 66 20 61 20 63 6f 6d 6d 69 74 2e 0a 2a 2f 0a 53  f a commit..*/.S
d3d50 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
d3d60 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  t sqlite3BtreeCo
d3d70 6d 6d 69 74 28 42 74 72 65 65 20 2a 70 29 7b 0a  mmit(Btree *p){.
d3d80 20 20 69 6e 74 20 72 63 3b 0a 20 20 72 63 20 3d    int rc;.  rc =
d3d90 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d   sqlite3BtreeCom
d3da0 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 2c 20 30  mitPhaseOne(p, 0
d3db0 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
d3dc0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63  ITE_OK ){.    rc
d3dd0 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43   = sqlite3BtreeC
d3de0 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70 29  ommitPhaseTwo(p)
d3df0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
d3e00 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44  c;.}..#ifndef ND
d3e10 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  EBUG./*.** Retur
d3e20 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
d3e30 77 72 69 74 65 2d 63 75 72 73 6f 72 73 20 6f 70  write-cursors op
d3e40 65 6e 20 6f 6e 20 74 68 69 73 20 68 61 6e 64 6c  en on this handl
d3e50 65 2e 20 54 68 69 73 20 69 73 20 66 6f 72 20 75  e. This is for u
d3e60 73 65 0a 2a 2a 20 69 6e 20 61 73 73 65 72 74 28  se.** in assert(
d3e70 29 20 65 78 70 72 65 73 73 69 6f 6e 73 2c 20 73  ) expressions, s
d3e80 6f 20 69 74 20 69 73 20 6f 6e 6c 79 20 63 6f 6d  o it is only com
d3e90 70 69 6c 65 64 20 69 66 20 4e 44 45 42 55 47 20  piled if NDEBUG 
d3ea0 69 73 20 6e 6f 74 0a 2a 2a 20 64 65 66 69 6e 65  is not.** define
d3eb0 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
d3ec0 20 63 6f 75 6e 74 57 72 69 74 65 43 75 72 73 6f   countWriteCurso
d3ed0 72 73 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  rs(BtShared *pBt
d3ee0 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  ){.  BtCursor *p
d3ef0 43 75 72 3b 0a 20 20 69 6e 74 20 72 20 3d 20 30  Cur;.  int r = 0
d3f00 3b 0a 20 20 66 6f 72 28 70 43 75 72 3d 70 42 74  ;.  for(pCur=pBt
d3f10 2d 3e 70 43 75 72 73 6f 72 3b 20 70 43 75 72 3b  ->pCursor; pCur;
d3f20 20 70 43 75 72 3d 70 43 75 72 2d 3e 70 4e 65 78   pCur=pCur->pNex
d3f30 74 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72  t){.    if( pCur
d3f40 2d 3e 77 72 46 6c 61 67 20 29 20 72 2b 2b 3b 20  ->wrFlag ) r++; 
d3f50 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 3b  .  }.  return r;
d3f60 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
d3f70 20 52 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72   Rollback the tr
d3f80 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20 70 72 6f  ansaction in pro
d3f90 67 72 65 73 73 2e 20 20 41 6c 6c 20 63 75 72 73  gress.  All curs
d3fa0 6f 72 73 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 69  ors will be.** i
d3fb0 6e 76 61 6c 69 64 65 64 20 62 79 20 74 68 69 73  nvalided by this
d3fc0 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 41 6e 79   operation.  Any
d3fd0 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20   attempt to use 
d3fe0 61 20 63 75 72 73 6f 72 0a 2a 2a 20 74 68 61 74  a cursor.** that
d3ff0 20 77 61 73 20 6f 70 65 6e 20 61 74 20 74 68 65   was open at the
d4000 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68   beginning of th
d4010 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 77 69 6c  is operation wil
d4020 6c 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 61  l result.** in a
d4030 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54  n error..**.** T
d4040 68 69 73 20 77 69 6c 6c 20 72 65 6c 65 61 73 65  his will release
d4050 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63 6b 20   the write lock 
d4060 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
d4070 66 69 6c 65 2e 20 20 49 66 20 74 68 65 72 65 0a  file.  If there.
d4080 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65  ** are no active
d4090 20 63 75 72 73 6f 72 73 2c 20 69 74 20 61 6c 73   cursors, it als
d40a0 6f 20 72 65 6c 65 61 73 65 73 20 74 68 65 20 72  o releases the r
d40b0 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c  ead lock..*/.SQL
d40c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
d40d0 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c  sqlite3BtreeRoll
d40e0 62 61 63 6b 28 42 74 72 65 65 20 2a 70 29 7b 0a  back(Btree *p){.
d40f0 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68    int rc;.  BtSh
d4100 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70  ared *pBt = p->p
d4110 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  Bt;.  MemPage *p
d4120 50 61 67 65 31 3b 0a 0a 20 20 72 63 20 3d 20 73  Page1;..  rc = s
d4130 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42  aveAllCursors(pB
d4140 74 2c 20 30 2c 20 30 29 3b 0a 23 69 66 6e 64 65  t, 0, 0);.#ifnde
d4150 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
d4160 41 52 45 44 5f 43 41 43 48 45 0a 20 20 69 66 28  ARED_CACHE.  if(
d4170 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
d4180 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73  {.    /* This is
d4190 20 61 20 68 6f 72 72 69 62 6c 65 20 73 69 74 75   a horrible situ
d41a0 61 74 69 6f 6e 2e 20 41 6e 20 49 4f 20 6f 72 20  ation. An IO or 
d41b0 6d 61 6c 6c 6f 63 28 29 20 65 72 72 6f 72 20 6f  malloc() error o
d41c0 63 63 75 72 65 64 20 77 68 69 6c 73 74 0a 20 20  ccured whilst.  
d41d0 20 20 2a 2a 20 74 72 79 69 6e 67 20 74 6f 20 73    ** trying to s
d41e0 61 76 65 20 63 75 72 73 6f 72 20 70 6f 73 69 74  ave cursor posit
d41f0 69 6f 6e 73 2e 20 49 66 20 74 68 69 73 20 69 73  ions. If this is
d4200 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 72 6f   an automatic ro
d4210 6c 6c 62 61 63 6b 20 28 61 73 0a 20 20 20 20 2a  llback (as.    *
d4220 2a 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20  * the result of 
d4230 61 20 63 6f 6e 73 74 72 61 69 6e 74 2c 20 6d 61  a constraint, ma
d4240 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 20 6f  lloc() failure o
d4250 72 20 49 4f 20 65 72 72 6f 72 29 20 74 68 65 6e  r IO error) then
d4260 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 61 63   .    ** the cac
d4270 68 65 20 6d 61 79 20 62 65 20 69 6e 74 65 72 6e  he may be intern
d4280 61 6c 6c 79 20 69 6e 63 6f 6e 73 69 73 74 65 6e  ally inconsisten
d4290 74 20 28 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 76  t (not contain v
d42a0 61 6c 69 64 20 74 72 65 65 73 29 20 73 6f 0a 20  alid trees) so. 
d42b0 20 20 20 2a 2a 20 77 65 20 63 61 6e 6e 6f 74 20     ** we cannot 
d42c0 73 69 6d 70 6c 79 20 72 65 74 75 72 6e 20 74 68  simply return th
d42d0 65 20 65 72 72 6f 72 20 74 6f 20 74 68 65 20 63  e error to the c
d42e0 61 6c 6c 65 72 2e 20 49 6e 73 74 65 61 64 2c 20  aller. Instead, 
d42f0 61 62 6f 72 74 20 0a 20 20 20 20 2a 2a 20 61 6c  abort .    ** al
d4300 6c 20 71 75 65 72 69 65 73 20 74 68 61 74 20 6d  l queries that m
d4310 61 79 20 62 65 20 75 73 69 6e 67 20 61 6e 79 20  ay be using any 
d4320 6f 66 20 74 68 65 20 63 75 72 73 6f 72 73 20 74  of the cursors t
d4330 68 61 74 20 66 61 69 6c 65 64 20 74 6f 20 73 61  hat failed to sa
d4340 76 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77  ve..    */.    w
d4350 68 69 6c 65 28 20 70 42 74 2d 3e 70 43 75 72 73  hile( pBt->pCurs
d4360 6f 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  or ){.      sqli
d4370 74 65 33 20 2a 64 62 20 3d 20 70 42 74 2d 3e 70  te3 *db = pBt->p
d4380 43 75 72 73 6f 72 2d 3e 70 42 74 72 65 65 2d 3e  Cursor->pBtree->
d4390 70 53 71 6c 69 74 65 3b 0a 20 20 20 20 20 20 69  pSqlite;.      i
d43a0 66 28 20 64 62 20 29 7b 0a 20 20 20 20 20 20 20  f( db ){.       
d43b0 20 73 71 6c 69 74 65 33 41 62 6f 72 74 4f 74 68   sqlite3AbortOth
d43c0 65 72 41 63 74 69 76 65 56 64 62 65 73 28 64 62  erActiveVdbes(db
d43d0 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  , 0);.      }.  
d43e0 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20    }.  }.#endif. 
d43f0 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28   btreeIntegrity(
d4400 70 29 3b 0a 20 20 75 6e 6c 6f 63 6b 41 6c 6c 54  p);.  unlockAllT
d4410 61 62 6c 65 73 28 70 29 3b 0a 0a 20 20 69 66 28  ables(p);..  if(
d4420 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41   p->inTrans==TRA
d4430 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20  NS_WRITE ){.    
d4440 69 6e 74 20 72 63 32 3b 0a 0a 23 69 66 6e 64 65  int rc2;..#ifnde
d4450 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
d4460 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 70 42 74  TOVACUUM.    pBt
d4470 2d 3e 6e 54 72 75 6e 63 20 3d 20 30 3b 0a 23 65  ->nTrunc = 0;.#e
d4480 6e 64 69 66 0a 0a 20 20 20 20 61 73 73 65 72 74  ndif..    assert
d4490 28 20 54 52 41 4e 53 5f 57 52 49 54 45 3d 3d 70  ( TRANS_WRITE==p
d44a0 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f  Bt->inTransactio
d44b0 6e 20 29 3b 0a 20 20 20 20 72 63 32 20 3d 20 73  n );.    rc2 = s
d44c0 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62  qlite3PagerRollb
d44d0 61 63 6b 28 70 42 74 2d 3e 70 50 61 67 65 72 29  ack(pBt->pPager)
d44e0 3b 0a 20 20 20 20 69 66 28 20 72 63 32 21 3d 53  ;.    if( rc2!=S
d44f0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
d4500 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 20 20    rc = rc2;.    
d4510 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 6f  }..    /* The ro
d4520 6c 6c 62 61 63 6b 20 6d 61 79 20 68 61 76 65 20  llback may have 
d4530 64 65 73 74 72 6f 79 65 64 20 74 68 65 20 70 50  destroyed the pP
d4540 61 67 65 31 2d 3e 61 44 61 74 61 20 76 61 6c 75  age1->aData valu
d4550 65 2e 20 20 53 6f 0a 20 20 20 20 2a 2a 20 63 61  e.  So.    ** ca
d4560 6c 6c 20 73 71 6c 69 74 65 33 42 74 72 65 65 47  ll sqlite3BtreeG
d4570 65 74 50 61 67 65 28 29 20 6f 6e 20 70 61 67 65  etPage() on page
d4580 20 31 20 61 67 61 69 6e 20 74 6f 20 6d 61 6b 65   1 again to make
d4590 0a 20 20 20 20 2a 2a 20 73 75 72 65 20 70 50 61  .    ** sure pPa
d45a0 67 65 31 2d 3e 61 44 61 74 61 20 69 73 20 73 65  ge1->aData is se
d45b0 74 20 63 6f 72 72 65 63 74 6c 79 2e 20 2a 2f 0a  t correctly. */.
d45c0 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 42      if( sqlite3B
d45d0 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c  treeGetPage(pBt,
d45e0 20 31 2c 20 26 70 50 61 67 65 31 2c 20 30 29 3d   1, &pPage1, 0)=
d45f0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
d4600 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
d4610 70 50 61 67 65 31 29 3b 0a 20 20 20 20 7d 0a 20  pPage1);.    }. 
d4620 20 20 20 61 73 73 65 72 74 28 20 63 6f 75 6e 74     assert( count
d4630 57 72 69 74 65 43 75 72 73 6f 72 73 28 70 42 74  WriteCursors(pBt
d4640 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70 42 74 2d  )==0 );.    pBt-
d4650 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d  >inTransaction =
d4660 20 54 52 41 4e 53 5f 52 45 41 44 3b 0a 20 20 7d   TRANS_READ;.  }
d4670 0a 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61  ..  if( p->inTra
d4680 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29  ns!=TRANS_NONE )
d4690 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42  {.    assert( pB
d46a0 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3e  t->nTransaction>
d46b0 30 20 29 3b 0a 20 20 20 20 70 42 74 2d 3e 6e 54  0 );.    pBt->nT
d46c0 72 61 6e 73 61 63 74 69 6f 6e 2d 2d 3b 0a 20 20  ransaction--;.  
d46d0 20 20 69 66 28 20 30 3d 3d 70 42 74 2d 3e 6e 54    if( 0==pBt->nT
d46e0 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20  ransaction ){.  
d46f0 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73      pBt->inTrans
d4700 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 4e  action = TRANS_N
d4710 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ONE;.    }.  }..
d4720 20 20 70 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 54    p->inTrans = T
d4730 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 70 42 74  RANS_NONE;.  pBt
d4740 2d 3e 69 6e 53 74 6d 74 20 3d 20 30 3b 0a 20 20  ->inStmt = 0;.  
d4750 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75  unlockBtreeIfUnu
d4760 73 65 64 28 70 42 74 29 3b 0a 0a 20 20 62 74 72  sed(pBt);..  btr
d4770 65 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0a  eeIntegrity(p);.
d4780 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
d4790 2f 2a 0a 2a 2a 20 53 74 61 72 74 20 61 20 73 74  /*.** Start a st
d47a0 61 74 65 6d 65 6e 74 20 73 75 62 74 72 61 6e 73  atement subtrans
d47b0 61 63 74 69 6f 6e 2e 20 20 54 68 65 20 73 75 62  action.  The sub
d47c0 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e 0a  transaction can.
d47d0 2a 2a 20 63 61 6e 20 62 65 20 72 6f 6c 6c 65 64  ** can be rolled
d47e0 20 62 61 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e   back independen
d47f0 74 6c 79 20 6f 66 20 74 68 65 20 6d 61 69 6e 20  tly of the main 
d4800 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20  transaction..** 
d4810 59 6f 75 20 6d 75 73 74 20 73 74 61 72 74 20 61  You must start a
d4820 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 66   transaction bef
d4830 6f 72 65 20 73 74 61 72 74 69 6e 67 20 61 20 73  ore starting a s
d4840 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a  ubtransaction..*
d4850 2a 20 54 68 65 20 73 75 62 74 72 61 6e 73 61 63  * The subtransac
d4860 74 69 6f 6e 20 69 73 20 65 6e 64 65 64 20 61 75  tion is ended au
d4870 74 6f 6d 61 74 69 63 61 6c 6c 79 20 69 66 20 74  tomatically if t
d4880 68 65 20 6d 61 69 6e 20 74 72 61 6e 73 61 63 74  he main transact
d4890 69 6f 6e 0a 2a 2a 20 63 6f 6d 6d 69 74 73 20 6f  ion.** commits o
d48a0 72 20 72 6f 6c 6c 73 20 62 61 63 6b 2e 0a 2a 2a  r rolls back..**
d48b0 0a 2a 2a 20 4f 6e 6c 79 20 6f 6e 65 20 73 75 62  .** Only one sub
d48c0 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20  transaction may 
d48d0 62 65 20 61 63 74 69 76 65 20 61 74 20 61 20 74  be active at a t
d48e0 69 6d 65 2e 20 20 49 74 20 69 73 20 61 6e 20 65  ime.  It is an e
d48f0 72 72 6f 72 20 74 6f 20 74 72 79 0a 2a 2a 20 74  rror to try.** t
d4900 6f 20 73 74 61 72 74 20 61 20 6e 65 77 20 73 75  o start a new su
d4910 62 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 66 20  btransaction if 
d4920 61 6e 6f 74 68 65 72 20 73 75 62 74 72 61 6e 73  another subtrans
d4930 61 63 74 69 6f 6e 20 69 73 20 61 6c 72 65 61 64  action is alread
d4940 79 20 61 63 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20  y active..**.** 
d4950 53 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72 61  Statement subtra
d4960 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 20 75 73  nsactions are us
d4970 65 64 20 61 72 6f 75 6e 64 20 69 6e 64 69 76 69  ed around indivi
d4980 64 75 61 6c 20 53 51 4c 20 73 74 61 74 65 6d 65  dual SQL stateme
d4990 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20  nts.** that are 
d49a0 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e  contained within
d49b0 20 61 20 42 45 47 49 4e 2e 2e 2e 43 4f 4d 4d 49   a BEGIN...COMMI
d49c0 54 20 62 6c 6f 63 6b 2e 20 20 49 66 20 61 20 63  T block.  If a c
d49d0 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 72 72  onstraint.** err
d49e0 6f 72 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e  or occurs within
d49f0 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c 20   the statement, 
d4a00 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 74 68  the effect of th
d4a10 61 74 20 6f 6e 65 20 73 74 61 74 65 6d 65 6e 74  at one statement
d4a20 0a 2a 2a 20 63 61 6e 20 62 65 20 72 6f 6c 6c 65  .** can be rolle
d4a30 64 20 62 61 63 6b 20 77 69 74 68 6f 75 74 20 68  d back without h
d4a40 61 76 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63  aving to rollbac
d4a50 6b 20 74 68 65 20 65 6e 74 69 72 65 20 74 72 61  k the entire tra
d4a60 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  nsaction..*/.SQL
d4a70 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
d4a80 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69  sqlite3BtreeBegi
d4a90 6e 53 74 6d 74 28 42 74 72 65 65 20 2a 70 29 7b  nStmt(Btree *p){
d4aa0 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 53  .  int rc;.  BtS
d4ab0 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
d4ac0 70 42 74 3b 0a 20 20 69 66 28 20 28 70 2d 3e 69  pBt;.  if( (p->i
d4ad0 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 57 52  nTrans!=TRANS_WR
d4ae0 49 54 45 29 20 7c 7c 20 70 42 74 2d 3e 69 6e 53  ITE) || pBt->inS
d4af0 74 6d 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72  tmt ){.    retur
d4b00 6e 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20  n pBt->readOnly 
d4b10 3f 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c  ? SQLITE_READONL
d4b20 59 20 3a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  Y : SQLITE_ERROR
d4b30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
d4b40 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69  pBt->inTransacti
d4b50 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20  on==TRANS_WRITE 
d4b60 29 3b 0a 20 20 72 63 20 3d 20 70 42 74 2d 3e 72  );.  rc = pBt->r
d4b70 65 61 64 4f 6e 6c 79 20 3f 20 53 51 4c 49 54 45  eadOnly ? SQLITE
d4b80 5f 4f 4b 20 3a 20 73 71 6c 69 74 65 33 50 61 67  _OK : sqlite3Pag
d4b90 65 72 53 74 6d 74 42 65 67 69 6e 28 70 42 74 2d  erStmtBegin(pBt-
d4ba0 3e 70 50 61 67 65 72 29 3b 0a 20 20 70 42 74 2d  >pPager);.  pBt-
d4bb0 3e 69 6e 53 74 6d 74 20 3d 20 31 3b 0a 20 20 72  >inStmt = 1;.  r
d4bc0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a  eturn rc;.}.../*
d4bd0 0a 2a 2a 20 43 6f 6d 6d 69 74 20 74 68 65 20 73  .** Commit the s
d4be0 74 61 74 6d 65 6e 74 20 73 75 62 74 72 61 6e 73  tatment subtrans
d4bf0 61 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79  action currently
d4c00 20 69 6e 20 70 72 6f 67 72 65 73 73 2e 20 20 49   in progress.  I
d4c10 66 20 6e 6f 0a 2a 2a 20 73 75 62 74 72 61 6e 73  f no.** subtrans
d4c20 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65  action is active
d4c30 2c 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f  , this is a no-o
d4c40 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  p..*/.SQLITE_PRI
d4c50 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
d4c60 42 74 72 65 65 43 6f 6d 6d 69 74 53 74 6d 74 28  BtreeCommitStmt(
d4c70 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74  Btree *p){.  int
d4c80 20 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20   rc;.  BtShared 
d4c90 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20  *pBt = p->pBt;. 
d4ca0 20 69 66 28 20 70 42 74 2d 3e 69 6e 53 74 6d 74   if( pBt->inStmt
d4cb0 20 26 26 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e   && !pBt->readOn
d4cc0 6c 79 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  ly ){.    rc = s
d4cd0 71 6c 69 74 65 33 50 61 67 65 72 53 74 6d 74 43  qlite3PagerStmtC
d4ce0 6f 6d 6d 69 74 28 70 42 74 2d 3e 70 50 61 67 65  ommit(pBt->pPage
d4cf0 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  r);.  }else{.   
d4d00 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
d4d10 0a 20 20 7d 0a 20 20 70 42 74 2d 3e 69 6e 53 74  .  }.  pBt->inSt
d4d20 6d 74 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e  mt = 0;.  return
d4d30 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f   rc;.}../*.** Ro
d4d40 6c 6c 62 61 63 6b 20 74 68 65 20 61 63 74 69 76  llback the activ
d4d50 65 20 73 74 61 74 65 6d 65 6e 74 20 73 75 62 74  e statement subt
d4d60 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 49 66 20  ransaction.  If 
d4d70 6e 6f 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f  no subtransactio
d4d80 6e 0a 2a 2a 20 69 73 20 61 63 74 69 76 65 20 74  n.** is active t
d4d90 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
d4da0 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 41 6c   no-op..**.** Al
d4db0 6c 20 63 75 72 73 6f 72 73 20 77 69 6c 6c 20 62  l cursors will b
d4dc0 65 20 69 6e 76 61 6c 69 64 61 74 65 64 20 62 79  e invalidated by
d4dd0 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e   this operation.
d4de0 20 20 41 6e 79 20 61 74 74 65 6d 70 74 0a 2a 2a    Any attempt.**
d4df0 20 74 6f 20 75 73 65 20 61 20 63 75 72 73 6f 72   to use a cursor
d4e00 20 74 68 61 74 20 77 61 73 20 6f 70 65 6e 20 61   that was open a
d4e10 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  t the beginning 
d4e20 6f 66 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f  of this operatio
d4e30 6e 0a 2a 2a 20 77 69 6c 6c 20 72 65 73 75 6c 74  n.** will result
d4e40 20 69 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f   in an error..*/
d4e50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
d4e60 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
d4e70 52 6f 6c 6c 62 61 63 6b 53 74 6d 74 28 42 74 72  RollbackStmt(Btr
d4e80 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63  ee *p){.  int rc
d4e90 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
d4ea0 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
d4eb0 70 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65  p->pBt;.  sqlite
d4ec0 33 4d 61 6c 6c 6f 63 44 69 73 61 6c 6c 6f 77 28  3MallocDisallow(
d4ed0 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 69 6e  );.  if( pBt->in
d4ee0 53 74 6d 74 20 26 26 20 21 70 42 74 2d 3e 72 65  Stmt && !pBt->re
d4ef0 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 72 63  adOnly ){.    rc
d4f00 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53   = sqlite3PagerS
d4f10 74 6d 74 52 6f 6c 6c 62 61 63 6b 28 70 42 74 2d  tmtRollback(pBt-
d4f20 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 61 73  >pPager);.    as
d4f30 73 65 72 74 28 20 63 6f 75 6e 74 57 72 69 74 65  sert( countWrite
d4f40 43 75 72 73 6f 72 73 28 70 42 74 29 3d 3d 30 20  Cursors(pBt)==0 
d4f50 29 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e 53 74  );.    pBt->inSt
d4f60 6d 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71  mt = 0;.  }.  sq
d4f70 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 6c 6f 77  lite3MallocAllow
d4f80 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  ();.  return rc;
d4f90 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 61 75 6c  .}../*.** Defaul
d4fa0 74 20 6b 65 79 20 63 6f 6d 70 61 72 69 73 6f 6e  t key comparison
d4fb0 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20   function to be 
d4fc0 75 73 65 64 20 69 66 20 6e 6f 20 63 6f 6d 70 61  used if no compa
d4fd0 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 0a 2a  rison function.*
d4fe0 2a 20 69 73 20 73 70 65 63 69 66 69 65 64 20 6f  * is specified o
d4ff0 6e 20 74 68 65 20 73 71 6c 69 74 65 33 42 74 72  n the sqlite3Btr
d5000 65 65 43 75 72 73 6f 72 28 29 20 63 61 6c 6c 2e  eeCursor() call.
d5010 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64  .*/.static int d
d5020 66 6c 74 43 6f 6d 70 61 72 65 28 0a 20 20 76 6f  fltCompare(.  vo
d5030 69 64 20 2a 4e 6f 74 55 73 65 64 2c 20 20 20 20  id *NotUsed,    
d5040 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 72           /* User
d5050 20 64 61 74 61 20 69 73 20 6e 6f 74 20 75 73 65   data is not use
d5060 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 31 2c 20 63  d */.  int n1, c
d5070 6f 6e 73 74 20 76 6f 69 64 20 2a 70 31 2c 20 20  onst void *p1,  
d5080 20 20 2f 2a 20 46 69 72 73 74 20 6b 65 79 20 74    /* First key t
d5090 6f 20 63 6f 6d 70 61 72 65 20 2a 2f 0a 20 20 69  o compare */.  i
d50a0 6e 74 20 6e 32 2c 20 63 6f 6e 73 74 20 76 6f 69  nt n2, const voi
d50b0 64 20 2a 70 32 20 20 20 20 20 2f 2a 20 53 65 63  d *p2     /* Sec
d50c0 6f 6e 64 20 6b 65 79 20 74 6f 20 63 6f 6d 70 61  ond key to compa
d50d0 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 63  re */.){.  int c
d50e0 3b 0a 20 20 63 20 3d 20 6d 65 6d 63 6d 70 28 70  ;.  c = memcmp(p
d50f0 31 2c 20 70 32 2c 20 6e 31 3c 6e 32 20 3f 20 6e  1, p2, n1<n2 ? n
d5100 31 20 3a 20 6e 32 29 3b 0a 20 20 69 66 28 20 63  1 : n2);.  if( c
d5110 3d 3d 30 20 29 7b 0a 20 20 20 20 63 20 3d 20 6e  ==0 ){.    c = n
d5120 31 20 2d 20 6e 32 3b 0a 20 20 7d 0a 20 20 72 65  1 - n2;.  }.  re
d5130 74 75 72 6e 20 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn c;.}../*.**
d5140 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 63 75   Create a new cu
d5150 72 73 6f 72 20 66 6f 72 20 74 68 65 20 42 54 72  rsor for the BTr
d5160 65 65 20 77 68 6f 73 65 20 72 6f 6f 74 20 69 73  ee whose root is
d5170 20 6f 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a 20   on the page.** 
d5180 69 54 61 62 6c 65 2e 20 20 54 68 65 20 61 63 74  iTable.  The act
d5190 20 6f 66 20 61 63 71 75 69 72 69 6e 67 20 61 20   of acquiring a 
d51a0 63 75 72 73 6f 72 20 67 65 74 73 20 61 20 72 65  cursor gets a re
d51b0 61 64 20 6c 6f 63 6b 20 6f 6e 20 0a 2a 2a 20 74  ad lock on .** t
d51c0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
d51d0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77 72 46 6c 61  ..**.** If wrFla
d51e0 67 3d 3d 30 2c 20 74 68 65 6e 20 74 68 65 20 63  g==0, then the c
d51f0 75 72 73 6f 72 20 63 61 6e 20 6f 6e 6c 79 20 62  ursor can only b
d5200 65 20 75 73 65 64 20 66 6f 72 20 72 65 61 64 69  e used for readi
d5210 6e 67 2e 0a 2a 2a 20 49 66 20 77 72 46 6c 61 67  ng..** If wrFlag
d5220 3d 3d 31 2c 20 74 68 65 6e 20 74 68 65 20 63 75  ==1, then the cu
d5230 72 73 6f 72 20 63 61 6e 20 62 65 20 75 73 65 64  rsor can be used
d5240 20 66 6f 72 20 72 65 61 64 69 6e 67 20 6f 72 20   for reading or 
d5250 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e 67 20 69  for.** writing i
d5260 66 20 6f 74 68 65 72 20 63 6f 6e 64 69 74 69 6f  f other conditio
d5270 6e 73 20 66 6f 72 20 77 72 69 74 69 6e 67 20 61  ns for writing a
d5280 72 65 20 61 6c 73 6f 20 6d 65 74 2e 20 20 54 68  re also met.  Th
d5290 65 73 65 0a 2a 2a 20 61 72 65 20 74 68 65 20 63  ese.** are the c
d52a0 6f 6e 64 69 74 69 6f 6e 73 20 74 68 61 74 20 6d  onditions that m
d52b0 75 73 74 20 62 65 20 6d 65 74 20 69 6e 20 6f 72  ust be met in or
d52c0 64 65 72 20 66 6f 72 20 77 72 69 74 69 6e 67 20  der for writing 
d52d0 74 6f 0a 2a 2a 20 62 65 20 61 6c 6c 6f 77 65 64  to.** be allowed
d52e0 3a 0a 2a 2a 0a 2a 2a 20 31 3a 20 20 54 68 65 20  :.**.** 1:  The 
d52f0 63 75 72 73 6f 72 20 6d 75 73 74 20 68 61 76 65  cursor must have
d5300 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 77 69 74   been opened wit
d5310 68 20 77 72 46 6c 61 67 3d 3d 31 0a 2a 2a 0a 2a  h wrFlag==1.**.*
d5320 2a 20 32 3a 20 20 4f 74 68 65 72 20 64 61 74 61  * 2:  Other data
d5330 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
d5340 20 74 68 61 74 20 73 68 61 72 65 20 74 68 65 20   that share the 
d5350 73 61 6d 65 20 70 61 67 65 72 20 63 61 63 68 65  same pager cache
d5360 0a 2a 2a 20 20 20 20 20 62 75 74 20 77 68 69 63  .**     but whic
d5370 68 20 61 72 65 20 6e 6f 74 20 69 6e 20 74 68 65  h are not in the
d5380 20 52 45 41 44 5f 55 4e 43 4f 4d 4d 49 54 54 45   READ_UNCOMMITTE
d5390 44 20 73 74 61 74 65 20 6d 61 79 20 6e 6f 74 20  D state may not 
d53a0 68 61 76 65 0a 2a 2a 20 20 20 20 20 63 75 72 73  have.**     curs
d53b0 6f 72 73 20 6f 70 65 6e 20 77 69 74 68 20 77 72  ors open with wr
d53c0 46 6c 61 67 3d 3d 30 20 6f 6e 20 74 68 65 20 73  Flag==0 on the s
d53d0 61 6d 65 20 74 61 62 6c 65 2e 20 20 4f 74 68 65  ame table.  Othe
d53e0 72 77 69 73 65 0a 2a 2a 20 20 20 20 20 74 68 65  rwise.**     the
d53f0 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79   changes made by
d5400 20 74 68 69 73 20 77 72 69 74 65 20 63 75 72 73   this write curs
d5410 6f 72 20 77 6f 75 6c 64 20 62 65 20 76 69 73 69  or would be visi
d5420 62 6c 65 20 74 6f 0a 2a 2a 20 20 20 20 20 74 68  ble to.**     th
d5430 65 20 72 65 61 64 20 63 75 72 73 6f 72 73 20 69  e read cursors i
d5440 6e 20 74 68 65 20 6f 74 68 65 72 20 64 61 74 61  n the other data
d5450 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  base connection.
d5460 0a 2a 2a 0a 2a 2a 20 33 3a 20 20 54 68 65 20 64  .**.** 3:  The d
d5470 61 74 61 62 61 73 65 20 6d 75 73 74 20 62 65 20  atabase must be 
d5480 77 72 69 74 61 62 6c 65 20 28 6e 6f 74 20 6f 6e  writable (not on
d5490 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 65 64 69 61   read-only media
d54a0 29 0a 2a 2a 0a 2a 2a 20 34 3a 20 20 54 68 65 72  ).**.** 4:  Ther
d54b0 65 20 6d 75 73 74 20 62 65 20 61 6e 20 61 63 74  e must be an act
d54c0 69 76 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  ive transaction.
d54d0 0a 2a 2a 0a 2a 2a 20 4e 6f 20 63 68 65 63 6b 69  .**.** No checki
d54e0 6e 67 20 69 73 20 64 6f 6e 65 20 74 6f 20 6d 61  ng is done to ma
d54f0 6b 65 20 73 75 72 65 20 74 68 61 74 20 70 61 67  ke sure that pag
d5500 65 20 69 54 61 62 6c 65 20 72 65 61 6c 6c 79 20  e iTable really 
d5510 69 73 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 70  is the.** root p
d5520 61 67 65 20 6f 66 20 61 20 62 2d 74 72 65 65 2e  age of a b-tree.
d5530 20 20 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20    If it is not, 
d5540 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20  then the cursor 
d5550 61 63 71 75 69 72 65 64 0a 2a 2a 20 77 69 6c 6c  acquired.** will
d5560 20 6e 6f 74 20 77 6f 72 6b 20 63 6f 72 72 65 63   not work correc
d5570 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  tly..**.** The c
d5580 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69  omparison functi
d5590 6f 6e 20 6d 75 73 74 20 62 65 20 6c 6f 67 69 63  on must be logic
d55a0 61 6c 6c 79 20 74 68 65 20 73 61 6d 65 20 66 6f  ally the same fo
d55b0 72 20 65 76 65 72 79 20 63 75 72 73 6f 72 0a 2a  r every cursor.*
d55c0 2a 20 6f 6e 20 61 20 70 61 72 74 69 63 75 6c 61  * on a particula
d55d0 72 20 74 61 62 6c 65 2e 20 20 43 68 61 6e 67 69  r table.  Changi
d55e0 6e 67 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  ng the compariso
d55f0 6e 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 20  n function will 
d5600 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 69 6e 63  result.** in inc
d5610 6f 72 72 65 63 74 20 6f 70 65 72 61 74 69 6f 6e  orrect operation
d5620 73 2e 20 20 49 66 20 74 68 65 20 63 6f 6d 70 61  s.  If the compa
d5630 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 69  rison function i
d5640 73 20 4e 55 4c 4c 2c 20 61 0a 2a 2a 20 64 65 66  s NULL, a.** def
d5650 61 75 6c 74 20 63 6f 6d 70 61 72 69 73 6f 6e 20  ault comparison 
d5660 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64  function is used
d5670 2e 20 20 54 68 65 20 63 6f 6d 70 61 72 69 73 6f  .  The compariso
d5680 6e 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a  n function is.**
d5690 20 61 6c 77 61 79 73 20 69 67 6e 6f 72 65 64 20   always ignored 
d56a0 66 6f 72 20 49 4e 54 4b 45 59 20 74 61 62 6c 65  for INTKEY table
d56b0 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
d56c0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
d56d0 42 74 72 65 65 43 75 72 73 6f 72 28 0a 20 20 42  BtreeCursor(.  B
d56e0 74 72 65 65 20 2a 70 2c 20 20 20 20 20 20 20 20  tree *p,        
d56f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d5700 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
d5710 65 20 62 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74  e btree */.  int
d5720 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20   iTable,        
d5730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d5740 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74           /* Root
d5750 20 70 61 67 65 20 6f 66 20 74 61 62 6c 65 20 74   page of table t
d5760 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20  o open */.  int 
d5770 77 72 46 6c 61 67 2c 20 20 20 20 20 20 20 20 20  wrFlag,         
d5780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d5790 20 20 20 20 20 20 20 20 2f 2a 20 31 20 74 6f 20          /* 1 to 
d57a0 77 72 69 74 65 2e 20 30 20 72 65 61 64 2d 6f 6e  write. 0 read-on
d57b0 6c 79 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43  ly */.  int (*xC
d57c0 6d 70 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  mp)(void*,int,co
d57d0 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  nst void*,int,co
d57e0 6e 73 74 20 76 6f 69 64 2a 29 2c 20 2f 2a 20 4b  nst void*), /* K
d57f0 65 79 20 43 6f 6d 70 61 72 69 73 6f 6e 20 66 75  ey Comparison fu
d5800 6e 63 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41  nc */.  void *pA
d5810 72 67 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  rg,             
d5820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d5830 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67      /* First arg
d5840 20 74 6f 20 78 43 6f 6d 70 61 72 65 28 29 20 2a   to xCompare() *
d5850 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 2a 70  /.  BtCursor **p
d5860 70 43 75 72 20 20 20 20 20 20 20 20 20 20 20 20  pCur            
d5870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d5880 2f 2a 20 57 72 69 74 65 20 6e 65 77 20 63 75 72  /* Write new cur
d5890 73 6f 72 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  sor here */.){. 
d58a0 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 43 75 72   int rc;.  BtCur
d58b0 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 42 74 53  sor *pCur;.  BtS
d58c0 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
d58d0 70 42 74 3b 0a 0a 20 20 2a 70 70 43 75 72 20 3d  pBt;..  *ppCur =
d58e0 20 30 3b 0a 20 20 69 66 28 20 77 72 46 6c 61 67   0;.  if( wrFlag
d58f0 20 29 7b 0a 20 20 20 20 69 66 28 20 70 42 74 2d   ){.    if( pBt-
d5900 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20  >readOnly ){.   
d5910 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
d5920 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 7d  _READONLY;.    }
d5930 0a 20 20 20 20 69 66 28 20 63 68 65 63 6b 52 65  .    if( checkRe
d5940 61 64 4c 6f 63 6b 73 28 70 2c 20 69 54 61 62 6c  adLocks(p, iTabl
d5950 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 20 72  e, 0) ){.      r
d5960 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43  eturn SQLITE_LOC
d5970 4b 45 44 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  KED;.    }.  }..
d5980 20 20 69 66 28 20 70 42 74 2d 3e 70 50 61 67 65    if( pBt->pPage
d5990 31 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d  1==0 ){.    rc =
d59a0 20 6c 6f 63 6b 42 74 72 65 65 57 69 74 68 52 65   lockBtreeWithRe
d59b0 74 72 79 28 70 29 3b 0a 20 20 20 20 69 66 28 20  try(p);.    if( 
d59c0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
d59d0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
d59e0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
d59f0 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 26 26  pBt->readOnly &&
d5a00 20 77 72 46 6c 61 67 20 29 7b 0a 20 20 20 20 20   wrFlag ){.     
d5a10 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52   return SQLITE_R
d5a20 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 7d 0a 20  EADONLY;.    }. 
d5a30 20 7d 0a 20 20 70 43 75 72 20 3d 20 73 71 6c 69   }.  pCur = sqli
d5a40 74 65 4d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66  teMalloc( sizeof
d5a50 28 2a 70 43 75 72 29 20 29 3b 0a 20 20 69 66 28  (*pCur) );.  if(
d5a60 20 70 43 75 72 3d 3d 30 20 29 7b 0a 20 20 20 20   pCur==0 ){.    
d5a70 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
d5a80 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 63 72 65 61  M;.    goto crea
d5a90 74 65 5f 63 75 72 73 6f 72 5f 65 78 63 65 70 74  te_cursor_except
d5aa0 69 6f 6e 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d  ion;.  }.  pCur-
d5ab0 3e 70 67 6e 6f 52 6f 6f 74 20 3d 20 28 50 67 6e  >pgnoRoot = (Pgn
d5ac0 6f 29 69 54 61 62 6c 65 3b 0a 20 20 69 66 28 20  o)iTable;.  if( 
d5ad0 69 54 61 62 6c 65 3d 3d 31 20 26 26 20 73 71 6c  iTable==1 && sql
d5ae0 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75  ite3PagerPagecou
d5af0 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3d  nt(pBt->pPager)=
d5b00 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  =0 ){.    rc = S
d5b10 51 4c 49 54 45 5f 45 4d 50 54 59 3b 0a 20 20 20  QLITE_EMPTY;.   
d5b20 20 67 6f 74 6f 20 63 72 65 61 74 65 5f 63 75 72   goto create_cur
d5b30 73 6f 72 5f 65 78 63 65 70 74 69 6f 6e 3b 0a 20  sor_exception;. 
d5b40 20 7d 0a 20 20 72 63 20 3d 20 67 65 74 41 6e 64   }.  rc = getAnd
d5b50 49 6e 69 74 50 61 67 65 28 70 42 74 2c 20 70 43  InitPage(pBt, pC
d5b60 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 26 70  ur->pgnoRoot, &p
d5b70 43 75 72 2d 3e 70 50 61 67 65 2c 20 30 29 3b 0a  Cur->pPage, 0);.
d5b80 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
d5b90 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  _OK ){.    goto 
d5ba0 63 72 65 61 74 65 5f 63 75 72 73 6f 72 5f 65 78  create_cursor_ex
d5bb0 63 65 70 74 69 6f 6e 3b 0a 20 20 7d 0a 0a 20 20  ception;.  }..  
d5bc0 2f 2a 20 4e 6f 77 20 74 68 61 74 20 6e 6f 20 6f  /* Now that no o
d5bd0 74 68 65 72 20 65 72 72 6f 72 73 20 63 61 6e 20  ther errors can 
d5be0 6f 63 63 75 72 2c 20 66 69 6e 69 73 68 20 66 69  occur, finish fi
d5bf0 6c 6c 69 6e 67 20 69 6e 20 74 68 65 20 42 74 43  lling in the BtC
d5c00 75 72 73 6f 72 0a 20 20 2a 2a 20 76 61 72 69 61  ursor.  ** varia
d5c10 62 6c 65 73 2c 20 6c 69 6e 6b 20 74 68 65 20 63  bles, link the c
d5c20 75 72 73 6f 72 20 69 6e 74 6f 20 74 68 65 20 42  ursor into the B
d5c30 74 53 68 61 72 65 64 20 6c 69 73 74 20 61 6e 64  tShared list and
d5c40 20 73 65 74 20 2a 70 70 43 75 72 20 28 74 68 65   set *ppCur (the
d5c50 0a 20 20 2a 2a 20 6f 75 74 70 75 74 20 61 72 67  .  ** output arg
d5c60 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75  ument to this fu
d5c70 6e 63 74 69 6f 6e 29 2e 0a 20 20 2a 2f 0a 20 20  nction)..  */.  
d5c80 70 43 75 72 2d 3e 78 43 6f 6d 70 61 72 65 20 3d  pCur->xCompare =
d5c90 20 78 43 6d 70 20 3f 20 78 43 6d 70 20 3a 20 64   xCmp ? xCmp : d
d5ca0 66 6c 74 43 6f 6d 70 61 72 65 3b 0a 20 20 70 43  fltCompare;.  pC
d5cb0 75 72 2d 3e 70 41 72 67 20 3d 20 70 41 72 67 3b  ur->pArg = pArg;
d5cc0 0a 20 20 70 43 75 72 2d 3e 70 42 74 72 65 65 20  .  pCur->pBtree 
d5cd0 3d 20 70 3b 0a 20 20 70 43 75 72 2d 3e 77 72 46  = p;.  pCur->wrF
d5ce0 6c 61 67 20 3d 20 77 72 46 6c 61 67 3b 0a 20 20  lag = wrFlag;.  
d5cf0 70 43 75 72 2d 3e 70 4e 65 78 74 20 3d 20 70 42  pCur->pNext = pB
d5d00 74 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 66  t->pCursor;.  if
d5d10 28 20 70 43 75 72 2d 3e 70 4e 65 78 74 20 29 7b  ( pCur->pNext ){
d5d20 0a 20 20 20 20 70 43 75 72 2d 3e 70 4e 65 78 74  .    pCur->pNext
d5d30 2d 3e 70 50 72 65 76 20 3d 20 70 43 75 72 3b 0a  ->pPrev = pCur;.
d5d40 20 20 7d 0a 20 20 70 42 74 2d 3e 70 43 75 72 73    }.  pBt->pCurs
d5d50 6f 72 20 3d 20 70 43 75 72 3b 0a 20 20 70 43 75  or = pCur;.  pCu
d5d60 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53  r->eState = CURS
d5d70 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 2a 70  OR_INVALID;.  *p
d5d80 70 43 75 72 20 3d 20 70 43 75 72 3b 0a 0a 20 20  pCur = pCur;..  
d5d90 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
d5da0 3b 0a 63 72 65 61 74 65 5f 63 75 72 73 6f 72 5f  ;.create_cursor_
d5db0 65 78 63 65 70 74 69 6f 6e 3a 0a 20 20 69 66 28  exception:.  if(
d5dc0 20 70 43 75 72 20 29 7b 0a 20 20 20 20 72 65 6c   pCur ){.    rel
d5dd0 65 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e 70  easePage(pCur->p
d5de0 50 61 67 65 29 3b 0a 20 20 20 20 73 71 6c 69 74  Page);.    sqlit
d5df0 65 46 72 65 65 28 70 43 75 72 29 3b 0a 20 20 7d  eFree(pCur);.  }
d5e00 0a 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66  .  unlockBtreeIf
d5e10 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 20 72  Unused(pBt);.  r
d5e20 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
d5e30 2a 2a 20 43 6c 6f 73 65 20 61 20 63 75 72 73 6f  ** Close a curso
d5e40 72 2e 20 20 54 68 65 20 72 65 61 64 20 6c 6f 63  r.  The read loc
d5e50 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
d5e60 65 20 66 69 6c 65 20 69 73 20 72 65 6c 65 61 73  e file is releas
d5e70 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 6c  ed.** when the l
d5e80 61 73 74 20 63 75 72 73 6f 72 20 69 73 20 63 6c  ast cursor is cl
d5e90 6f 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  osed..*/.SQLITE_
d5ea0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
d5eb0 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72  te3BtreeCloseCur
d5ec0 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43  sor(BtCursor *pC
d5ed0 75 72 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20  ur){.  BtShared 
d5ee0 2a 70 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 74  *pBt = pCur->pBt
d5ef0 72 65 65 2d 3e 70 42 74 3b 0a 20 20 63 6c 65 61  ree->pBt;.  clea
d5f00 72 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28  rCursorPosition(
d5f10 70 43 75 72 29 3b 0a 20 20 69 66 28 20 70 43 75  pCur);.  if( pCu
d5f20 72 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20  r->pPrev ){.    
d5f30 70 43 75 72 2d 3e 70 50 72 65 76 2d 3e 70 4e 65  pCur->pPrev->pNe
d5f40 78 74 20 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74  xt = pCur->pNext
d5f50 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
d5f60 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20 70 43  Bt->pCursor = pC
d5f70 75 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20  ur->pNext;.  }. 
d5f80 20 69 66 28 20 70 43 75 72 2d 3e 70 4e 65 78 74   if( pCur->pNext
d5f90 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 70 4e   ){.    pCur->pN
d5fa0 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 43 75  ext->pPrev = pCu
d5fb0 72 2d 3e 70 50 72 65 76 3b 0a 20 20 7d 0a 20 20  r->pPrev;.  }.  
d5fc0 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 75 72  releasePage(pCur
d5fd0 2d 3e 70 50 61 67 65 29 3b 0a 20 20 75 6e 6c 6f  ->pPage);.  unlo
d5fe0 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64 28  ckBtreeIfUnused(
d5ff0 70 42 74 29 3b 0a 20 20 69 6e 76 61 6c 69 64 61  pBt);.  invalida
d6000 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28  teOverflowCache(
d6010 70 43 75 72 29 3b 0a 20 20 73 71 6c 69 74 65 46  pCur);.  sqliteF
d6020 72 65 65 28 70 43 75 72 29 3b 0a 20 20 72 65 74  ree(pCur);.  ret
d6030 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
d6040 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 74  ../*.** Make a t
d6050 65 6d 70 6f 72 61 72 79 20 63 75 72 73 6f 72 20  emporary cursor 
d6060 62 79 20 66 69 6c 6c 69 6e 67 20 69 6e 20 74 68  by filling in th
d6070 65 20 66 69 65 6c 64 73 20 6f 66 20 70 54 65 6d  e fields of pTem
d6080 70 43 75 72 2e 0a 2a 2a 20 54 68 65 20 74 65 6d  pCur..** The tem
d6090 70 6f 72 61 72 79 20 63 75 72 73 6f 72 20 69 73  porary cursor is
d60a0 20 6e 6f 74 20 6f 6e 20 74 68 65 20 63 75 72 73   not on the curs
d60b0 6f 72 20 6c 69 73 74 20 66 6f 72 20 74 68 65 20  or list for the 
d60c0 42 74 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  Btree..*/.SQLITE
d60d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
d60e0 6c 69 74 65 33 42 74 72 65 65 47 65 74 54 65 6d  lite3BtreeGetTem
d60f0 70 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72  pCursor(BtCursor
d6100 20 2a 70 43 75 72 2c 20 42 74 43 75 72 73 6f 72   *pCur, BtCursor
d6110 20 2a 70 54 65 6d 70 43 75 72 29 7b 0a 20 20 6d   *pTempCur){.  m
d6120 65 6d 63 70 79 28 70 54 65 6d 70 43 75 72 2c 20  emcpy(pTempCur, 
d6130 70 43 75 72 2c 20 73 69 7a 65 6f 66 28 2a 70 43  pCur, sizeof(*pC
d6140 75 72 29 29 3b 0a 20 20 70 54 65 6d 70 43 75 72  ur));.  pTempCur
d6150 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 70  ->pNext = 0;.  p
d6160 54 65 6d 70 43 75 72 2d 3e 70 50 72 65 76 20 3d  TempCur->pPrev =
d6170 20 30 3b 0a 20 20 69 66 28 20 70 54 65 6d 70 43   0;.  if( pTempC
d6180 75 72 2d 3e 70 50 61 67 65 20 29 7b 0a 20 20 20  ur->pPage ){.   
d6190 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66   sqlite3PagerRef
d61a0 28 70 54 65 6d 70 43 75 72 2d 3e 70 50 61 67 65  (pTempCur->pPage
d61b0 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 7d 0a  ->pDbPage);.  }.
d61c0 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20  }../*.** Delete 
d61d0 61 20 74 65 6d 70 6f 72 61 72 79 20 63 75 72 73  a temporary curs
d61e0 6f 72 20 73 75 63 68 20 61 73 20 77 61 73 20 6d  or such as was m
d61f0 61 64 65 20 62 79 20 74 68 65 20 43 72 65 61 74  ade by the Creat
d6200 65 54 65 6d 70 6f 72 61 72 79 43 75 72 73 6f 72  eTemporaryCursor
d6210 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61  ().** function a
d6220 62 6f 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  bove..*/.SQLITE_
d6230 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
d6240 69 74 65 33 42 74 72 65 65 52 65 6c 65 61 73 65  ite3BtreeRelease
d6250 54 65 6d 70 43 75 72 73 6f 72 28 42 74 43 75 72  TempCursor(BtCur
d6260 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 69 66  sor *pCur){.  if
d6270 28 20 70 43 75 72 2d 3e 70 50 61 67 65 20 29 7b  ( pCur->pPage ){
d6280 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65  .    sqlite3Page
d6290 72 55 6e 72 65 66 28 70 43 75 72 2d 3e 70 50 61  rUnref(pCur->pPa
d62a0 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  ge->pDbPage);.  
d62b0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20  }.}../*.** Make 
d62c0 73 75 72 65 20 74 68 65 20 42 74 43 75 72 73 6f  sure the BtCurso
d62d0 72 2a 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20  r* given in the 
d62e0 61 72 67 75 6d 65 6e 74 20 68 61 73 20 61 20 76  argument has a v
d62f0 61 6c 69 64 0a 2a 2a 20 42 74 43 75 72 73 6f 72  alid.** BtCursor
d6300 2e 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e  .info structure.
d6310 20 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20 61    If it is not a
d6320 6c 72 65 61 64 79 20 76 61 6c 69 64 2c 20 63 61  lready valid, ca
d6330 6c 6c 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72  ll.** sqlite3Btr
d6340 65 65 50 61 72 73 65 43 65 6c 6c 28 29 20 74 6f  eeParseCell() to
d6350 20 66 69 6c 6c 20 69 74 20 69 6e 2e 0a 2a 2a 0a   fill it in..**.
d6360 2a 2a 20 42 74 43 75 72 73 6f 72 2e 69 6e 66 6f  ** BtCursor.info
d6370 20 69 73 20 61 20 63 61 63 68 65 20 6f 66 20 74   is a cache of t
d6380 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  he information i
d6390 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 65  n the current ce
d63a0 6c 6c 2e 0a 2a 2a 20 55 73 69 6e 67 20 74 68 69  ll..** Using thi
d63b0 73 20 63 61 63 68 65 20 72 65 64 75 63 65 73 20  s cache reduces 
d63c0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 61  the number of ca
d63d0 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 42 74  lls to sqlite3Bt
d63e0 72 65 65 50 61 72 73 65 43 65 6c 6c 28 29 2e 0a  reeParseCell()..
d63f0 2a 2a 0a 2a 2a 20 32 30 30 37 2d 30 36 2d 32 35  **.** 2007-06-25
d6400 3a 20 20 54 68 65 72 65 20 69 73 20 61 20 62 75  :  There is a bu
d6410 67 20 69 6e 20 73 6f 6d 65 20 76 65 72 73 69 6f  g in some versio
d6420 6e 73 20 6f 66 20 4d 53 56 43 20 74 68 61 74 20  ns of MSVC that 
d6430 63 61 75 73 65 20 74 68 65 0a 2a 2a 20 63 6f 6d  cause the.** com
d6440 70 69 6c 65 72 20 74 6f 20 63 72 61 73 68 20 77  piler to crash w
d6450 68 65 6e 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28  hen getCellInfo(
d6460 29 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64  ) is implemented
d6470 20 61 73 20 61 20 6d 61 63 72 6f 2e 0a 2a 2a 20   as a macro..** 
d6480 42 75 74 20 74 68 65 72 65 20 69 73 20 61 20 6d  But there is a m
d6490 65 61 73 75 72 65 61 62 6c 65 20 73 70 65 65 64  easureable speed
d64a0 20 61 64 76 61 6e 74 61 67 65 20 74 6f 20 75 73   advantage to us
d64b0 69 6e 67 20 74 68 65 20 6d 61 63 72 6f 20 6f 6e  ing the macro on
d64c0 20 67 63 63 0a 2a 2a 20 28 77 68 65 6e 20 6c 65   gcc.** (when le
d64d0 73 73 20 63 6f 6d 70 69 6c 65 72 20 6f 70 74 69  ss compiler opti
d64e0 6d 69 7a 61 74 69 6f 6e 73 20 6c 69 6b 65 20 2d  mizations like -
d64f0 4f 73 20 6f 72 20 2d 4f 30 20 61 72 65 20 75 73  Os or -O0 are us
d6500 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 63 6f  ed and the.** co
d6510 6d 70 69 6c 65 72 20 69 73 20 6e 6f 74 20 64 6f  mpiler is not do
d6520 69 6e 67 20 61 67 72 65 73 73 69 76 65 20 69 6e  ing agressive in
d6530 6c 69 6e 69 6e 67 2e 29 20 20 53 6f 20 77 65 20  lining.)  So we 
d6540 75 73 65 20 61 20 72 65 61 6c 20 66 75 6e 63 74  use a real funct
d6550 69 6f 6e 0a 2a 2a 20 66 6f 72 20 4d 53 56 43 20  ion.** for MSVC 
d6560 61 6e 64 20 61 20 6d 61 63 72 6f 20 66 6f 72 20  and a macro for 
d6570 65 76 65 72 79 74 68 69 6e 67 20 65 6c 73 65 2e  everything else.
d6580 20 20 54 69 63 6b 65 74 20 23 32 34 35 37 2e 0a    Ticket #2457..
d6590 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  */.#ifndef NDEBU
d65a0 47 0a 20 20 73 74 61 74 69 63 20 76 6f 69 64 20  G.  static void 
d65b0 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 42  assertCellInfo(B
d65c0 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a  tCursor *pCur){.
d65d0 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66      CellInfo inf
d65e0 6f 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 69  o;.    memset(&i
d65f0 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 69  nfo, 0, sizeof(i
d6600 6e 66 6f 29 29 3b 0a 20 20 20 20 73 71 6c 69 74  nfo));.    sqlit
d6610 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c  e3BtreeParseCell
d6620 28 70 43 75 72 2d 3e 70 50 61 67 65 2c 20 70 43  (pCur->pPage, pC
d6630 75 72 2d 3e 69 64 78 2c 20 26 69 6e 66 6f 29 3b  ur->idx, &info);
d6640 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d  .    assert( mem
d6650 63 6d 70 28 26 69 6e 66 6f 2c 20 26 70 43 75 72  cmp(&info, &pCur
d6660 2d 3e 69 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 69  ->info, sizeof(i
d6670 6e 66 6f 29 29 3d 3d 30 20 29 3b 0a 20 20 7d 0a  nfo))==0 );.  }.
d6680 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20  #else.  #define 
d6690 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 78  assertCellInfo(x
d66a0 29 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  ).#endif.#ifdef 
d66b0 5f 4d 53 43 5f 56 45 52 0a 20 20 2f 2a 20 55 73  _MSC_VER.  /* Us
d66c0 65 20 61 20 72 65 61 6c 20 66 75 6e 63 74 69 6f  e a real functio
d66d0 6e 20 69 6e 20 4d 53 56 43 20 74 6f 20 77 6f 72  n in MSVC to wor
d66e0 6b 20 61 72 6f 75 6e 64 20 62 75 67 73 20 69 6e  k around bugs in
d66f0 20 74 68 61 74 20 63 6f 6d 70 69 6c 65 72 2e 20   that compiler. 
d6700 2a 2f 0a 20 20 73 74 61 74 69 63 20 76 6f 69 64  */.  static void
d6710 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 42 74 43   getCellInfo(BtC
d6720 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20  ursor *pCur){.  
d6730 20 20 69 66 28 20 70 43 75 72 2d 3e 69 6e 66 6f    if( pCur->info
d6740 2e 6e 53 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20  .nSize==0 ){.   
d6750 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 50     sqlite3BtreeP
d6760 61 72 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e 70  arseCell(pCur->p
d6770 50 61 67 65 2c 20 70 43 75 72 2d 3e 69 64 78 2c  Page, pCur->idx,
d6780 20 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 0a 20   &pCur->info);. 
d6790 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
d67a0 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 70  assertCellInfo(p
d67b0 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Cur);.    }.  }.
d67c0 23 65 6c 73 65 20 2f 2a 20 69 66 20 6e 6f 74 20  #else /* if not 
d67d0 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a 20 20 2f 2a  _MSC_VER */.  /*
d67e0 20 55 73 65 20 61 20 6d 61 63 72 6f 20 69 6e 20   Use a macro in 
d67f0 61 6c 6c 20 6f 74 68 65 72 20 63 6f 6d 70 69 6c  all other compil
d6800 65 72 73 20 73 6f 20 74 68 61 74 20 74 68 65 20  ers so that the 
d6810 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 6c 69  function is inli
d6820 6e 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 67  ned */.#define g
d6830 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29  etCellInfo(pCur)
d6840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d6850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d6860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
d6870 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 69 6e 66  .  if( pCur->inf
d6880 6f 2e 6e 53 69 7a 65 3d 3d 30 20 29 7b 20 20 20  o.nSize==0 ){   
d6890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d68a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d68b0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 73           \.    s
d68c0 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65  qlite3BtreeParse
d68d0 43 65 6c 6c 28 70 43 75 72 2d 3e 70 50 61 67 65  Cell(pCur->pPage
d68e0 2c 20 70 43 75 72 2d 3e 69 64 78 2c 20 26 70 43  , pCur->idx, &pC
d68f0 75 72 2d 3e 69 6e 66 6f 29 3b 20 20 20 20 20 20  ur->info);      
d6900 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20 20 20     \.  }else{   
d6910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d6920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d6930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d6940 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
d6950 20 20 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66     assertCellInf
d6960 6f 28 70 43 75 72 29 3b 20 20 20 20 20 20 20 20  o(pCur);        
d6970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d6980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d6990 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 23 65 6e         \.  }.#en
d69a0 64 69 66 20 2f 2a 20 5f 4d 53 43 5f 56 45 52 20  dif /* _MSC_VER 
d69b0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 2a 70  */../*.** Set *p
d69c0 53 69 7a 65 20 74 6f 20 74 68 65 20 73 69 7a 65  Size to the size
d69d0 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 6e   of the buffer n
d69e0 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 20 74 68  eeded to hold th
d69f0 65 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 74 68  e value of.** th
d6a00 65 20 6b 65 79 20 66 6f 72 20 74 68 65 20 63 75  e key for the cu
d6a10 72 72 65 6e 74 20 65 6e 74 72 79 2e 20 20 49 66  rrent entry.  If
d6a20 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 6e   the cursor is n
d6a30 6f 74 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20 74  ot pointing.** t
d6a40 6f 20 61 20 76 61 6c 69 64 20 65 6e 74 72 79 2c  o a valid entry,
d6a50 20 2a 70 53 69 7a 65 20 69 73 20 73 65 74 20 74   *pSize is set t
d6a60 6f 20 30 2e 20 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  o 0. .**.** For 
d6a70 61 20 74 61 62 6c 65 20 77 69 74 68 20 74 68 65  a table with the
d6a80 20 49 4e 54 4b 45 59 20 66 6c 61 67 20 73 65 74   INTKEY flag set
d6a90 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72  , this routine r
d6aa0 65 74 75 72 6e 73 20 74 68 65 20 6b 65 79 0a 2a  eturns the key.*
d6ab0 2a 20 69 74 73 65 6c 66 2c 20 6e 6f 74 20 74 68  * itself, not th
d6ac0 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
d6ad0 73 20 69 6e 20 74 68 65 20 6b 65 79 2e 0a 2a 2f  s in the key..*/
d6ae0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
d6af0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
d6b00 4b 65 79 53 69 7a 65 28 42 74 43 75 72 73 6f 72  KeySize(BtCursor
d6b10 20 2a 70 43 75 72 2c 20 69 36 34 20 2a 70 53 69   *pCur, i64 *pSi
d6b20 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ze){.  int rc = 
d6b30 72 65 73 74 6f 72 65 4f 72 43 6c 65 61 72 43 75  restoreOrClearCu
d6b40 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75  rsorPosition(pCu
d6b50 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  r);.  if( rc==SQ
d6b60 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61  LITE_OK ){.    a
d6b70 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74  ssert( pCur->eSt
d6b80 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41  ate==CURSOR_INVA
d6b90 4c 49 44 20 7c 7c 20 70 43 75 72 2d 3e 65 53 74  LID || pCur->eSt
d6ba0 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49  ate==CURSOR_VALI
d6bb0 44 20 29 3b 0a 20 20 20 20 69 66 28 20 70 43 75  D );.    if( pCu
d6bc0 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  r->eState==CURSO
d6bd0 52 5f 49 4e 56 41 4c 49 44 20 29 7b 0a 20 20 20  R_INVALID ){.   
d6be0 20 20 20 2a 70 53 69 7a 65 20 3d 20 30 3b 0a 20     *pSize = 0;. 
d6bf0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
d6c00 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72  getCellInfo(pCur
d6c10 29 3b 0a 20 20 20 20 20 20 2a 70 53 69 7a 65 20  );.      *pSize 
d6c20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65  = pCur->info.nKe
d6c30 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  y;.    }.  }.  r
d6c40 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
d6c50 2a 2a 20 53 65 74 20 2a 70 53 69 7a 65 20 74 6f  ** Set *pSize to
d6c60 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
d6c70 79 74 65 73 20 6f 66 20 64 61 74 61 20 69 6e 20  ytes of data in 
d6c80 74 68 65 20 65 6e 74 72 79 20 74 68 65 0a 2a 2a  the entry the.**
d6c90 20 63 75 72 73 6f 72 20 63 75 72 72 65 6e 74 6c   cursor currentl
d6ca0 79 20 70 6f 69 6e 74 73 20 74 6f 2e 20 20 41 6c  y points to.  Al
d6cb0 77 61 79 73 20 72 65 74 75 72 6e 20 53 51 4c 49  ways return SQLI
d6cc0 54 45 5f 4f 4b 2e 0a 2a 2a 20 46 61 69 6c 75 72  TE_OK..** Failur
d6cd0 65 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c  e is not possibl
d6ce0 65 2e 20 20 49 66 20 74 68 65 20 63 75 72 73 6f  e.  If the curso
d6cf0 72 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74  r is not current
d6d00 6c 79 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 74  ly.** pointing t
d6d10 6f 20 61 6e 20 65 6e 74 72 79 20 28 77 68 69 63  o an entry (whic
d6d20 68 20 63 61 6e 20 68 61 70 70 65 6e 2c 20 66 6f  h can happen, fo
d6d30 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 0a 2a 2a  r example, if.**
d6d40 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
d6d50 20 65 6d 70 74 79 29 20 74 68 65 6e 20 2a 70 53   empty) then *pS
d6d60 69 7a 65 20 69 73 20 73 65 74 20 74 6f 20 30 2e  ize is set to 0.
d6d70 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
d6d80 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
d6d90 72 65 65 44 61 74 61 53 69 7a 65 28 42 74 43 75  reeDataSize(BtCu
d6da0 72 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 20  rsor *pCur, u32 
d6db0 2a 70 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72  *pSize){.  int r
d6dc0 63 20 3d 20 72 65 73 74 6f 72 65 4f 72 43 6c 65  c = restoreOrCle
d6dd0 61 72 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e  arCursorPosition
d6de0 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63  (pCur);.  if( rc
d6df0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
d6e00 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d     assert( pCur-
d6e10 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
d6e20 49 4e 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d  INVALID || pCur-
d6e30 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
d6e40 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 69 66 28  VALID );.    if(
d6e50 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
d6e60 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b  URSOR_INVALID ){
d6e70 0a 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 70 6f  .      /* Not po
d6e80 69 6e 74 69 6e 67 20 61 74 20 61 20 76 61 6c 69  inting at a vali
d6e90 64 20 65 6e 74 72 79 20 2d 20 73 65 74 20 2a 70  d entry - set *p
d6ea0 53 69 7a 65 20 74 6f 20 30 2e 20 2a 2f 0a 20 20  Size to 0. */.  
d6eb0 20 20 20 20 2a 70 53 69 7a 65 20 3d 20 30 3b 0a      *pSize = 0;.
d6ec0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
d6ed0 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75   getCellInfo(pCu
d6ee0 72 29 3b 0a 20 20 20 20 20 20 2a 70 53 69 7a 65  r);.      *pSize
d6ef0 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44   = pCur->info.nD
d6f00 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ata;.    }.  }. 
d6f10 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
d6f20 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 70  *.** Given the p
d6f30 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 6e  age number of an
d6f40 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69   overflow page i
d6f50 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 28  n the database (
d6f60 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 6f 76 66  parameter.** ovf
d6f70 6c 29 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  l), this functio
d6f80 6e 20 66 69 6e 64 73 20 74 68 65 20 70 61 67 65  n finds the page
d6f90 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e   number of the n
d6fa0 65 78 74 20 70 61 67 65 20 69 6e 20 74 68 65 20  ext page in the 
d6fb0 0a 2a 2a 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  .** linked list 
d6fc0 6f 66 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  of overflow page
d6fd0 73 2e 20 49 66 20 70 6f 73 73 69 62 6c 65 2c 20  s. If possible, 
d6fe0 69 74 20 75 73 65 73 20 74 68 65 20 61 75 74 6f  it uses the auto
d6ff0 2d 76 61 63 75 75 6d 0a 2a 2a 20 70 6f 69 6e 74  -vacuum.** point
d7000 65 72 2d 6d 61 70 20 64 61 74 61 20 69 6e 73 74  er-map data inst
d7010 65 61 64 20 6f 66 20 72 65 61 64 69 6e 67 20 74  ead of reading t
d7020 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 70 61  he content of pa
d7030 67 65 20 6f 76 66 6c 20 74 6f 20 64 6f 20 73 6f  ge ovfl to do so
d7040 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65  . .**.** If an e
d7050 72 72 6f 72 20 6f 63 63 75 72 73 20 61 6e 20 53  rror occurs an S
d7060 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65  QLite error code
d7070 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74   is returned. Ot
d7080 68 65 72 77 69 73 65 3a 0a 2a 2a 0a 2a 2a 20 55  herwise:.**.** U
d7090 6e 6c 65 73 73 20 70 50 67 6e 6f 4e 65 78 74 20  nless pPgnoNext 
d70a0 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 70 61 67  is NULL, the pag
d70b0 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
d70c0 6e 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 0a 2a  next overflow .*
d70d0 2a 20 70 61 67 65 20 69 6e 20 74 68 65 20 6c 69  * page in the li
d70e0 6e 6b 65 64 20 6c 69 73 74 20 69 73 20 77 72 69  nked list is wri
d70f0 74 74 65 6e 20 74 6f 20 2a 70 50 67 6e 6f 4e 65  tten to *pPgnoNe
d7100 78 74 2e 20 49 66 20 70 61 67 65 20 6f 76 66 6c  xt. If page ovfl
d7110 0a 2a 2a 20 69 73 20 74 68 65 20 6c 61 73 74 20  .** is the last 
d7120 70 61 67 65 20 69 6e 20 69 74 27 73 20 6c 69 6e  page in it's lin
d7130 6b 65 64 20 6c 69 73 74 2c 20 2a 70 50 67 6e 6f  ked list, *pPgno
d7140 4e 65 78 74 20 69 73 20 73 65 74 20 74 6f 20 7a  Next is set to z
d7150 65 72 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 70  ero. .**.** If p
d7160 70 50 61 67 65 20 69 73 20 6e 6f 74 20 4e 55 4c  pPage is not NUL
d7170 4c 2c 20 2a 70 70 50 61 67 65 20 69 73 20 73 65  L, *ppPage is se
d7180 74 20 74 6f 20 74 68 65 20 4d 65 6d 50 61 67 65  t to the MemPage
d7190 2a 20 68 61 6e 64 6c 65 0a 2a 2a 20 66 6f 72 20  * handle.** for 
d71a0 70 61 67 65 20 6f 76 66 6c 2e 20 54 68 65 20 75  page ovfl. The u
d71b0 6e 64 65 72 6c 79 69 6e 67 20 70 61 67 65 72 20  nderlying pager 
d71c0 70 61 67 65 20 6d 61 79 20 68 61 76 65 20 62 65  page may have be
d71d0 65 6e 20 72 65 71 75 65 73 74 65 64 0a 2a 2a 20  en requested.** 
d71e0 77 69 74 68 20 74 68 65 20 6e 6f 43 6f 6e 74 65  with the noConte
d71f0 6e 74 20 66 6c 61 67 20 73 65 74 2c 20 73 6f 20  nt flag set, so 
d7200 74 68 65 20 70 61 67 65 20 64 61 74 61 20 61 63  the page data ac
d7210 63 65 73 73 61 62 6c 65 20 76 69 61 0a 2a 2a 20  cessable via.** 
d7220 74 68 69 73 20 68 61 6e 64 6c 65 20 6d 61 79 20  this handle may 
d7230 6e 6f 74 20 62 65 20 74 72 75 73 74 65 64 2e 0a  not be trusted..
d7240 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65  */.static int ge
d7250 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 0a 20  tOverflowPage(. 
d7260 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20   BtShared *pBt, 
d7270 0a 20 20 50 67 6e 6f 20 6f 76 66 6c 2c 20 20 20  .  Pgno ovfl,   
d7280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d7290 2f 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65  /* Overflow page
d72a0 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a   */.  MemPage **
d72b0 70 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 20  ppPage,         
d72c0 20 20 20 2f 2a 20 4f 55 54 3a 20 4d 65 6d 50 61     /* OUT: MemPa
d72d0 67 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 50  ge handle */.  P
d72e0 67 6e 6f 20 2a 70 50 67 6e 6f 4e 65 78 74 20 20  gno *pPgnoNext  
d72f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
d7300 55 54 3a 20 4e 65 78 74 20 6f 76 65 72 66 6c 6f  UT: Next overflo
d7310 77 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f  w page number */
d7320 0a 29 7b 0a 20 20 50 67 6e 6f 20 6e 65 78 74 20  .){.  Pgno next 
d7330 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  = 0;.  int rc;..
d7340 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 73    /* One of thes
d7350 65 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 4e 55  e must not be NU
d7360 4c 4c 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 77  LL. Otherwise, w
d7370 68 79 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e  hy call this fun
d7380 63 74 69 6f 6e 3f 20 2a 2f 0a 20 20 61 73 73 65  ction? */.  asse
d7390 72 74 28 70 70 50 61 67 65 20 7c 7c 20 70 50 67  rt(ppPage || pPg
d73a0 6e 6f 4e 65 78 74 29 3b 0a 0a 20 20 2f 2a 20 49  noNext);..  /* I
d73b0 66 20 70 50 67 6e 6f 4e 65 78 74 20 69 73 20 4e  f pPgnoNext is N
d73c0 55 4c 4c 2c 20 74 68 65 6e 20 74 68 69 73 20 66  ULL, then this f
d73d0 75 6e 63 74 69 6f 6e 20 69 73 20 62 65 69 6e 67  unction is being
d73e0 20 63 61 6c 6c 65 64 20 74 6f 20 6f 62 74 61 69   called to obtai
d73f0 6e 0a 20 20 2a 2a 20 61 20 4d 65 6d 50 61 67 65  n.  ** a MemPage
d7400 2a 20 72 65 66 65 72 65 6e 63 65 20 6f 6e 6c 79  * reference only
d7410 2e 20 4e 6f 20 70 61 67 65 2d 64 61 74 61 20 69  . No page-data i
d7420 73 20 72 65 71 75 69 72 65 64 20 69 6e 20 74 68  s required in th
d7430 69 73 20 63 61 73 65 2e 0a 20 20 2a 2f 0a 20 20  is case..  */.  
d7440 69 66 28 20 21 70 50 67 6e 6f 4e 65 78 74 20 29  if( !pPgnoNext )
d7450 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c  {.    return sql
d7460 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65  ite3BtreeGetPage
d7470 28 70 42 74 2c 20 6f 76 66 6c 2c 20 70 70 50 61  (pBt, ovfl, ppPa
d7480 67 65 2c 20 31 29 3b 0a 20 20 7d 0a 0a 23 69 66  ge, 1);.  }..#if
d7490 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
d74a0 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 2f 2a  _AUTOVACUUM.  /*
d74b0 20 54 72 79 20 74 6f 20 66 69 6e 64 20 74 68 65   Try to find the
d74c0 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20 74 68   next page in th
d74d0 65 20 6f 76 65 72 66 6c 6f 77 20 6c 69 73 74 20  e overflow list 
d74e0 75 73 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 61  using the.  ** a
d74f0 75 74 6f 76 61 63 75 75 6d 20 70 6f 69 6e 74 65  utovacuum pointe
d7500 72 2d 6d 61 70 20 70 61 67 65 73 2e 20 47 75 65  r-map pages. Gue
d7510 73 73 20 74 68 61 74 20 74 68 65 20 6e 65 78 74  ss that the next
d7520 20 70 61 67 65 20 69 6e 20 0a 20 20 2a 2a 20 74   page in .  ** t
d7530 68 65 20 6f 76 65 72 66 6c 6f 77 20 6c 69 73 74  he overflow list
d7540 20 69 73 20 70 61 67 65 20 6e 75 6d 62 65 72 20   is page number 
d7550 28 6f 76 66 6c 2b 31 29 2e 20 49 66 20 74 68 61  (ovfl+1). If tha
d7560 74 20 67 75 65 73 73 20 74 75 72 6e 73 20 0a 20  t guess turns . 
d7570 20 2a 2a 20 6f 75 74 20 74 6f 20 62 65 20 77 72   ** out to be wr
d7580 6f 6e 67 2c 20 66 61 6c 6c 20 62 61 63 6b 20 74  ong, fall back t
d7590 6f 20 6c 6f 61 64 69 6e 67 20 74 68 65 20 64 61  o loading the da
d75a0 74 61 20 6f 66 20 70 61 67 65 20 0a 20 20 2a 2a  ta of page .  **
d75b0 20 6e 75 6d 62 65 72 20 6f 76 66 6c 20 74 6f 20   number ovfl to 
d75c0 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 6e 65  determine the ne
d75d0 78 74 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a  xt page number..
d75e0 20 20 2a 2f 0a 20 20 69 66 28 20 70 42 74 2d 3e    */.  if( pBt->
d75f0 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20  autoVacuum ){.  
d7600 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 20    Pgno pgno;.   
d7610 20 50 67 6e 6f 20 69 47 75 65 73 73 20 3d 20 6f   Pgno iGuess = o
d7620 76 66 6c 2b 31 3b 0a 20 20 20 20 75 38 20 65 54  vfl+1;.    u8 eT
d7630 79 70 65 3b 0a 0a 20 20 20 20 77 68 69 6c 65 28  ype;..    while(
d7640 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70   PTRMAP_ISPAGE(p
d7650 42 74 2c 20 69 47 75 65 73 73 29 20 7c 7c 20 69  Bt, iGuess) || i
d7660 47 75 65 73 73 3d 3d 50 45 4e 44 49 4e 47 5f 42  Guess==PENDING_B
d7670 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b  YTE_PAGE(pBt) ){
d7680 0a 20 20 20 20 20 20 69 47 75 65 73 73 2b 2b 3b  .      iGuess++;
d7690 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20  .    }..    if( 
d76a0 69 47 75 65 73 73 3c 3d 73 71 6c 69 74 65 33 50  iGuess<=sqlite3P
d76b0 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42  agerPagecount(pB
d76c0 74 2d 3e 70 50 61 67 65 72 29 20 29 7b 0a 20 20  t->pPager) ){.  
d76d0 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47      rc = ptrmapG
d76e0 65 74 28 70 42 74 2c 20 69 47 75 65 73 73 2c 20  et(pBt, iGuess, 
d76f0 26 65 54 79 70 65 2c 20 26 70 67 6e 6f 29 3b 0a  &eType, &pgno);.
d7700 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
d7710 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
d7720 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
d7730 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
d7740 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56  eType==PTRMAP_OV
d7750 45 52 46 4c 4f 57 32 20 26 26 20 70 67 6e 6f 3d  ERFLOW2 && pgno=
d7760 3d 6f 76 66 6c 20 29 7b 0a 20 20 20 20 20 20 20  =ovfl ){.       
d7770 20 6e 65 78 74 20 3d 20 69 47 75 65 73 73 3b 0a   next = iGuess;.
d7780 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
d7790 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20  }.#endif..  if( 
d77a0 6e 65 78 74 3d 3d 30 20 7c 7c 20 70 70 50 61 67  next==0 || ppPag
d77b0 65 20 29 7b 0a 20 20 20 20 4d 65 6d 50 61 67 65  e ){.    MemPage
d77c0 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 0a 20 20   *pPage = 0;..  
d77d0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
d77e0 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20  reeGetPage(pBt, 
d77f0 6f 76 66 6c 2c 20 26 70 50 61 67 65 2c 20 6e 65  ovfl, &pPage, ne
d7800 78 74 21 3d 30 29 3b 0a 20 20 20 20 61 73 73 65  xt!=0);.    asse
d7810 72 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  rt(rc==SQLITE_OK
d7820 20 7c 7c 20 70 50 61 67 65 3d 3d 30 29 3b 0a 20   || pPage==0);. 
d7830 20 20 20 69 66 28 20 6e 65 78 74 3d 3d 30 20 26     if( next==0 &
d7840 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  & rc==SQLITE_OK 
d7850 29 7b 0a 20 20 20 20 20 20 6e 65 78 74 20 3d 20  ){.      next = 
d7860 67 65 74 34 62 79 74 65 28 70 50 61 67 65 2d 3e  get4byte(pPage->
d7870 61 44 61 74 61 29 3b 0a 20 20 20 20 7d 0a 0a 20  aData);.    }.. 
d7880 20 20 20 69 66 28 20 70 70 50 61 67 65 20 29 7b     if( ppPage ){
d7890 0a 20 20 20 20 20 20 2a 70 70 50 61 67 65 20 3d  .      *ppPage =
d78a0 20 70 50 61 67 65 3b 0a 20 20 20 20 7d 65 6c 73   pPage;.    }els
d78b0 65 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65  e{.      release
d78c0 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20  Page(pPage);.   
d78d0 20 7d 0a 20 20 7d 0a 20 20 2a 70 50 67 6e 6f 4e   }.  }.  *pPgnoN
d78e0 65 78 74 20 3d 20 6e 65 78 74 3b 0a 0a 20 20 72  ext = next;..  r
d78f0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
d7900 2a 2a 20 43 6f 70 79 20 64 61 74 61 20 66 72 6f  ** Copy data fro
d7910 6d 20 61 20 62 75 66 66 65 72 20 74 6f 20 61 20  m a buffer to a 
d7920 70 61 67 65 2c 20 6f 72 20 66 72 6f 6d 20 61 20  page, or from a 
d7930 70 61 67 65 20 74 6f 20 61 20 62 75 66 66 65 72  page to a buffer
d7940 2e 0a 2a 2a 0a 2a 2a 20 70 50 61 79 6c 6f 61 64  ..**.** pPayload
d7950 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
d7960 20 64 61 74 61 20 73 74 6f 72 65 64 20 6f 6e 20   data stored on 
d7970 64 61 74 61 62 61 73 65 20 70 61 67 65 20 70 44  database page pD
d7980 62 50 61 67 65 2e 0a 2a 2a 20 49 66 20 61 72 67  bPage..** If arg
d7990 75 6d 65 6e 74 20 65 4f 70 20 69 73 20 66 61 6c  ument eOp is fal
d79a0 73 65 2c 20 74 68 65 6e 20 6e 42 79 74 65 20 62  se, then nByte b
d79b0 79 74 65 73 20 6f 66 20 64 61 74 61 20 61 72 65  ytes of data are
d79c0 20 63 6f 70 69 65 64 0a 2a 2a 20 66 72 6f 6d 20   copied.** from 
d79d0 70 50 61 79 6c 6f 61 64 20 74 6f 20 74 68 65 20  pPayload to the 
d79e0 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20 61  buffer pointed a
d79f0 74 20 62 79 20 70 42 75 66 2e 20 49 66 20 65 4f  t by pBuf. If eO
d7a00 70 20 69 73 20 74 72 75 65 2c 0a 2a 2a 20 74 68  p is true,.** th
d7a10 65 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 57  en sqlite3PagerW
d7a20 72 69 74 65 28 29 20 69 73 20 63 61 6c 6c 65 64  rite() is called
d7a30 20 6f 6e 20 70 44 62 50 61 67 65 20 61 6e 64 20   on pDbPage and 
d7a40 6e 42 79 74 65 20 62 79 74 65 73 0a 2a 2a 20 6f  nByte bytes.** o
d7a50 66 20 64 61 74 61 20 61 72 65 20 63 6f 70 69 65  f data are copie
d7a60 64 20 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65  d from the buffe
d7a70 72 20 70 42 75 66 20 74 6f 20 70 50 61 79 6c 6f  r pBuf to pPaylo
d7a80 61 64 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45  ad..**.** SQLITE
d7a90 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20  _OK is returned 
d7aa0 6f 6e 20 73 75 63 63 65 73 73 2c 20 6f 74 68 65  on success, othe
d7ab0 72 77 69 73 65 20 61 6e 20 65 72 72 6f 72 20 63  rwise an error c
d7ac0 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ode..*/.static i
d7ad0 6e 74 20 63 6f 70 79 50 61 79 6c 6f 61 64 28 0a  nt copyPayload(.
d7ae0 20 20 76 6f 69 64 20 2a 70 50 61 79 6c 6f 61 64    void *pPayload
d7af0 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50  ,           /* P
d7b00 6f 69 6e 74 65 72 20 74 6f 20 70 61 67 65 20 64  ointer to page d
d7b10 61 74 61 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  ata */.  void *p
d7b20 42 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20  Buf,            
d7b30 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
d7b40 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74   buffer */.  int
d7b50 20 6e 42 79 74 65 2c 20 20 20 20 20 20 20 20 20   nByte,         
d7b60 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
d7b70 20 6f 66 20 62 79 74 65 73 20 74 6f 20 63 6f 70   of bytes to cop
d7b80 79 20 2a 2f 0a 20 20 69 6e 74 20 65 4f 70 2c 20  y */.  int eOp, 
d7b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d7ba0 20 2f 2a 20 30 20 2d 3e 20 63 6f 70 79 20 66 72   /* 0 -> copy fr
d7bb0 6f 6d 20 70 61 67 65 2c 20 31 20 2d 3e 20 63 6f  om page, 1 -> co
d7bc0 70 79 20 74 6f 20 70 61 67 65 20 2a 2f 0a 20 20  py to page */.  
d7bd0 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 20  DbPage *pDbPage 
d7be0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67            /* Pag
d7bf0 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 50 61  e containing pPa
d7c00 79 6c 6f 61 64 20 2a 2f 0a 29 7b 0a 20 20 69 66  yload */.){.  if
d7c10 28 20 65 4f 70 20 29 7b 0a 20 20 20 20 2f 2a 20  ( eOp ){.    /* 
d7c20 43 6f 70 79 20 64 61 74 61 20 66 72 6f 6d 20 62  Copy data from b
d7c30 75 66 66 65 72 20 74 6f 20 70 61 67 65 20 28 61  uffer to page (a
d7c40 20 77 72 69 74 65 20 6f 70 65 72 61 74 69 6f 6e   write operation
d7c50 29 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 63 20  ) */.    int rc 
d7c60 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
d7c70 69 74 65 28 70 44 62 50 61 67 65 29 3b 0a 20 20  ite(pDbPage);.  
d7c80 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
d7c90 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74  _OK ){.      ret
d7ca0 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
d7cb0 20 20 6d 65 6d 63 70 79 28 70 50 61 79 6c 6f 61    memcpy(pPayloa
d7cc0 64 2c 20 70 42 75 66 2c 20 6e 42 79 74 65 29 3b  d, pBuf, nByte);
d7cd0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
d7ce0 20 43 6f 70 79 20 64 61 74 61 20 66 72 6f 6d 20   Copy data from 
d7cf0 70 61 67 65 20 74 6f 20 62 75 66 66 65 72 20 28  page to buffer (
d7d00 61 20 72 65 61 64 20 6f 70 65 72 61 74 69 6f 6e  a read operation
d7d10 29 20 2a 2f 0a 20 20 20 20 6d 65 6d 63 70 79 28  ) */.    memcpy(
d7d20 70 42 75 66 2c 20 70 50 61 79 6c 6f 61 64 2c 20  pBuf, pPayload, 
d7d30 6e 42 79 74 65 29 3b 0a 20 20 7d 0a 20 20 72 65  nByte);.  }.  re
d7d40 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
d7d50 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
d7d60 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74  nction is used t
d7d70 6f 20 72 65 61 64 20 6f 72 20 6f 76 65 72 77 72  o read or overwr
d7d80 69 74 65 20 70 61 79 6c 6f 61 64 20 69 6e 66 6f  ite payload info
d7d90 72 6d 61 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74  rmation.** for t
d7da0 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 74 68  he entry that th
d7db0 65 20 70 43 75 72 20 63 75 72 73 6f 72 20 69 73  e pCur cursor is
d7dc0 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 49 66   pointing to. If
d7dd0 20 74 68 65 20 65 4f 70 0a 2a 2a 20 70 61 72 61   the eOp.** para
d7de0 6d 65 74 65 72 20 69 73 20 30 2c 20 74 68 69 73  meter is 0, this
d7df0 20 69 73 20 61 20 72 65 61 64 20 6f 70 65 72 61   is a read opera
d7e00 74 69 6f 6e 20 28 64 61 74 61 20 63 6f 70 69 65  tion (data copie
d7e10 64 20 69 6e 74 6f 0a 2a 2a 20 62 75 66 66 65 72  d into.** buffer
d7e20 20 70 42 75 66 29 2e 20 49 66 20 69 74 20 69 73   pBuf). If it is
d7e30 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 61 20 77 72 69   non-zero, a wri
d7e40 74 65 20 28 64 61 74 61 20 63 6f 70 69 65 64 20  te (data copied 
d7e50 66 72 6f 6d 0a 2a 2a 20 62 75 66 66 65 72 20 70  from.** buffer p
d7e60 42 75 66 29 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 6f  Buf)..**.** A to
d7e70 74 61 6c 20 6f 66 20 22 61 6d 74 22 20 62 79 74  tal of "amt" byt
d7e80 65 73 20 61 72 65 20 72 65 61 64 20 6f 72 20 77  es are read or w
d7e90 72 69 74 74 65 6e 20 62 65 67 69 6e 6e 69 6e 67  ritten beginning
d7ea0 20 61 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a   at "offset"..**
d7eb0 20 44 61 74 61 20 69 73 20 72 65 61 64 20 74 6f   Data is read to
d7ec0 20 6f 72 20 66 72 6f 6d 20 74 68 65 20 62 75 66   or from the buf
d7ed0 66 65 72 20 70 42 75 66 2e 0a 2a 2a 0a 2a 2a 20  fer pBuf..**.** 
d7ee0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65  This routine doe
d7ef0 73 20 6e 6f 74 20 6d 61 6b 65 20 61 20 64 69 73  s not make a dis
d7f00 74 69 6e 63 74 69 6f 6e 20 62 65 74 77 65 65 6e  tinction between
d7f10 20 6b 65 79 20 61 6e 64 20 64 61 74 61 2e 0a 2a   key and data..*
d7f20 2a 20 49 74 20 6a 75 73 74 20 72 65 61 64 73 20  * It just reads 
d7f30 6f 72 20 77 72 69 74 65 73 20 62 79 74 65 73 20  or writes bytes 
d7f40 66 72 6f 6d 20 74 68 65 20 70 61 79 6c 6f 61 64  from the payload
d7f50 20 61 72 65 61 2e 20 20 44 61 74 61 20 6d 69 67   area.  Data mig
d7f60 68 74 20 0a 2a 2a 20 61 70 70 65 61 72 20 6f 6e  ht .** appear on
d7f70 20 74 68 65 20 6d 61 69 6e 20 70 61 67 65 20 6f   the main page o
d7f80 72 20 62 65 20 73 63 61 74 74 65 72 65 64 20 6f  r be scattered o
d7f90 75 74 20 6f 6e 20 6d 75 6c 74 69 70 6c 65 20 6f  ut on multiple o
d7fa0 76 65 72 66 6c 6f 77 20 0a 2a 2a 20 70 61 67 65  verflow .** page
d7fb0 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  s..**.** If the 
d7fc0 42 74 43 75 72 73 6f 72 2e 69 73 49 6e 63 72 62  BtCursor.isIncrb
d7fd0 6c 6f 62 48 61 6e 64 6c 65 20 66 6c 61 67 20 69  lobHandle flag i
d7fe0 73 20 73 65 74 2c 20 61 6e 64 20 74 68 65 20 63  s set, and the c
d7ff0 75 72 72 65 6e 74 0a 2a 2a 20 63 75 72 73 6f 72  urrent.** cursor
d8000 20 65 6e 74 72 79 20 75 73 65 73 20 6f 6e 65 20   entry uses one 
d8010 6f 72 20 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77  or more overflow
d8020 20 70 61 67 65 73 2c 20 74 68 69 73 20 66 75 6e   pages, this fun
d8030 63 74 69 6f 6e 0a 2a 2a 20 61 6c 6c 6f 63 61 74  ction.** allocat
d8040 65 73 20 73 70 61 63 65 20 66 6f 72 20 61 6e 64  es space for and
d8050 20 6c 61 7a 69 6c 79 20 70 6f 70 6c 75 61 74 65   lazily popluate
d8060 73 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70  s the overflow p
d8070 61 67 65 2d 6c 69 73 74 20 0a 2a 2a 20 63 61 63  age-list .** cac
d8080 68 65 20 61 72 72 61 79 20 28 42 74 43 75 72 73  he array (BtCurs
d8090 6f 72 2e 61 4f 76 65 72 66 6c 6f 77 29 2e 20 53  or.aOverflow). S
d80a0 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20  ubsequent calls 
d80b0 75 73 65 20 74 68 69 73 0a 2a 2a 20 63 61 63 68  use this.** cach
d80c0 65 20 74 6f 20 6d 61 6b 65 20 73 65 65 6b 69 6e  e to make seekin
d80d0 67 20 74 6f 20 74 68 65 20 73 75 70 70 6c 69 65  g to the supplie
d80e0 64 20 6f 66 66 73 65 74 20 6d 6f 72 65 20 65 66  d offset more ef
d80f0 66 69 63 69 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f  ficient..**.** O
d8100 6e 63 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20  nce an overflow 
d8110 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 20  page-list cache 
d8120 68 61 73 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74  has been allocat
d8130 65 64 2c 20 69 74 20 6d 61 79 20 62 65 0a 2a 2a  ed, it may be.**
d8140 20 69 6e 76 61 6c 69 64 61 74 65 64 20 69 66 20   invalidated if 
d8150 73 6f 6d 65 20 6f 74 68 65 72 20 63 75 72 73 6f  some other curso
d8160 72 20 77 72 69 74 65 73 20 74 6f 20 74 68 65 20  r writes to the 
d8170 73 61 6d 65 20 74 61 62 6c 65 2c 20 6f 72 20 69  same table, or i
d8180 66 0a 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 20  f.** the cursor 
d8190 69 73 20 6d 6f 76 65 64 20 74 6f 20 61 20 64 69  is moved to a di
d81a0 66 66 65 72 65 6e 74 20 72 6f 77 2e 20 41 64 64  fferent row. Add
d81b0 69 74 69 6f 6e 61 6c 6c 79 2c 20 69 6e 20 61 75  itionally, in au
d81c0 74 6f 2d 76 61 63 75 75 6d 0a 2a 2a 20 6d 6f 64  to-vacuum.** mod
d81d0 65 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  e, the following
d81e0 20 65 76 65 6e 74 73 20 6d 61 79 20 69 6e 76 61   events may inva
d81f0 6c 69 64 61 74 65 20 61 6e 20 6f 76 65 72 66 6c  lidate an overfl
d8200 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63  ow page-list cac
d8210 68 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 41 6e  he..**.**   * An
d8220 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63   incremental vac
d8230 75 75 6d 2c 0a 2a 2a 20 20 20 2a 20 41 20 63 6f  uum,.**   * A co
d8240 6d 6d 69 74 20 69 6e 20 61 75 74 6f 5f 76 61 63  mmit in auto_vac
d8250 75 75 6d 3d 22 66 75 6c 6c 22 20 6d 6f 64 65 2c  uum="full" mode,
d8260 0a 2a 2a 20 20 20 2a 20 43 72 65 61 74 69 6e 67  .**   * Creating
d8270 20 61 20 74 61 62 6c 65 20 28 6d 61 79 20 72 65   a table (may re
d8280 71 75 69 72 65 20 6d 6f 76 69 6e 67 20 61 6e 20  quire moving an 
d8290 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 29 2e 0a  overflow page)..
d82a0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 63  */.static int ac
d82b0 63 65 73 73 50 61 79 6c 6f 61 64 28 0a 20 20 42  cessPayload(.  B
d82c0 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 20  tCursor *pCur,  
d82d0 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f      /* Cursor po
d82e0 69 6e 74 69 6e 67 20 74 6f 20 65 6e 74 72 79 20  inting to entry 
d82f0 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a  to read from */.
d8300 20 20 69 6e 74 20 6f 66 66 73 65 74 2c 20 20 20    int offset,   
d8310 20 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20         /* Begin 
d8320 72 65 61 64 69 6e 67 20 74 68 69 73 20 66 61 72  reading this far
d8330 20 69 6e 74 6f 20 70 61 79 6c 6f 61 64 20 2a 2f   into payload */
d8340 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20  .  int amt,     
d8350 20 20 20 20 20 20 20 20 2f 2a 20 52 65 61 64 20          /* Read 
d8360 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73 20  this many bytes 
d8370 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  */.  unsigned ch
d8380 61 72 20 2a 70 42 75 66 2c 20 2f 2a 20 57 72 69  ar *pBuf, /* Wri
d8390 74 65 20 74 68 65 20 62 79 74 65 73 20 69 6e 74  te the bytes int
d83a0 6f 20 74 68 69 73 20 62 75 66 66 65 72 20 2a 2f  o this buffer */
d83b0 20 0a 20 20 69 6e 74 20 73 6b 69 70 4b 65 79 2c   .  int skipKey,
d83c0 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 66 66 73           /* offs
d83d0 65 74 20 62 65 67 69 6e 73 20 61 74 20 64 61 74  et begins at dat
d83e0 61 20 69 66 20 74 68 69 73 20 69 73 20 74 72 75  a if this is tru
d83f0 65 20 2a 2f 0a 20 20 69 6e 74 20 65 4f 70 20 20  e */.  int eOp  
d8400 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a              /* z
d8410 65 72 6f 20 74 6f 20 72 65 61 64 2e 20 6e 6f 6e  ero to read. non
d8420 2d 7a 65 72 6f 20 74 6f 20 77 72 69 74 65 2e 20  -zero to write. 
d8430 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64  */.){.  unsigned
d8440 20 63 68 61 72 20 2a 61 50 61 79 6c 6f 61 64 3b   char *aPayload;
d8450 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
d8460 54 45 5f 4f 4b 3b 0a 20 20 75 33 32 20 6e 4b 65  TE_OK;.  u32 nKe
d8470 79 3b 0a 20 20 69 6e 74 20 69 49 64 78 20 3d 20  y;.  int iIdx = 
d8480 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  0;.  MemPage *pP
d8490 61 67 65 20 3d 20 70 43 75 72 2d 3e 70 50 61 67  age = pCur->pPag
d84a0 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 74 72  e;        /* Btr
d84b0 65 65 20 70 61 67 65 20 6f 66 20 63 75 72 72 65  ee page of curre
d84c0 6e 74 20 63 75 72 73 6f 72 20 65 6e 74 72 79 20  nt cursor entry 
d84d0 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  */.  BtShared *p
d84e0 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 65  Bt = pCur->pBtre
d84f0 65 2d 3e 70 42 74 3b 20 20 20 2f 2a 20 42 74 72  e->pBt;   /* Btr
d8500 65 65 20 74 68 69 73 20 63 75 72 73 6f 72 20 62  ee this cursor b
d8510 65 6c 6f 6e 67 73 20 74 6f 20 2a 2f 0a 0a 20 20  elongs to */..  
d8520 61 73 73 65 72 74 28 20 70 50 61 67 65 20 29 3b  assert( pPage );
d8530 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
d8540 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
d8550 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72  VALID );.  asser
d8560 74 28 20 70 43 75 72 2d 3e 69 64 78 3e 3d 30 20  t( pCur->idx>=0 
d8570 26 26 20 70 43 75 72 2d 3e 69 64 78 3c 70 50 61  && pCur->idx<pPa
d8580 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 61  ge->nCell );.  a
d8590 73 73 65 72 74 28 20 6f 66 66 73 65 74 3e 3d 30  ssert( offset>=0
d85a0 20 29 3b 0a 0a 20 20 67 65 74 43 65 6c 6c 49 6e   );..  getCellIn
d85b0 66 6f 28 70 43 75 72 29 3b 0a 20 20 61 50 61 79  fo(pCur);.  aPay
d85c0 6c 6f 61 64 20 3d 20 70 43 75 72 2d 3e 69 6e 66  load = pCur->inf
d85d0 6f 2e 70 43 65 6c 6c 20 2b 20 70 43 75 72 2d 3e  o.pCell + pCur->
d85e0 69 6e 66 6f 2e 6e 48 65 61 64 65 72 3b 0a 20 20  info.nHeader;.  
d85f0 6e 4b 65 79 20 3d 20 28 70 50 61 67 65 2d 3e 69  nKey = (pPage->i
d8600 6e 74 4b 65 79 20 3f 20 30 20 3a 20 70 43 75 72  ntKey ? 0 : pCur
d8610 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 29 3b 0a 0a 20  ->info.nKey);.. 
d8620 20 69 66 28 20 73 6b 69 70 4b 65 79 20 29 7b 0a   if( skipKey ){.
d8630 20 20 20 20 6f 66 66 73 65 74 20 2b 3d 20 6e 4b      offset += nK
d8640 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6f 66  ey;.  }.  if( of
d8650 66 73 65 74 2b 61 6d 74 20 3e 20 6e 4b 65 79 2b  fset+amt > nKey+
d8660 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44 61 74 61  pCur->info.nData
d8670 20 29 7b 0a 20 20 20 20 2f 2a 20 54 72 79 69 6e   ){.    /* Tryin
d8680 67 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69  g to read or wri
d8690 74 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20  te past the end 
d86a0 6f 66 20 74 68 65 20 64 61 74 61 20 69 73 20 61  of the data is a
d86b0 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 72  n error */.    r
d86c0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
d86d0 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68  OR;.  }..  /* Ch
d86e0 65 63 6b 20 69 66 20 64 61 74 61 20 6d 75 73 74  eck if data must
d86f0 20 62 65 20 72 65 61 64 2f 77 72 69 74 74 65 6e   be read/written
d8700 20 74 6f 2f 66 72 6f 6d 20 74 68 65 20 62 74 72   to/from the btr
d8710 65 65 20 70 61 67 65 20 69 74 73 65 6c 66 2e 20  ee page itself. 
d8720 2a 2f 0a 20 20 69 66 28 20 6f 66 66 73 65 74 3c  */.  if( offset<
d8730 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61  pCur->info.nLoca
d8740 6c 20 29 7b 0a 20 20 20 20 69 6e 74 20 61 20 3d  l ){.    int a =
d8750 20 61 6d 74 3b 0a 20 20 20 20 69 66 28 20 61 2b   amt;.    if( a+
d8760 6f 66 66 73 65 74 3e 70 43 75 72 2d 3e 69 6e 66  offset>pCur->inf
d8770 6f 2e 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20  o.nLocal ){.    
d8780 20 20 61 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f    a = pCur->info
d8790 2e 6e 4c 6f 63 61 6c 20 2d 20 6f 66 66 73 65 74  .nLocal - offset
d87a0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d  ;.    }.    rc =
d87b0 20 63 6f 70 79 50 61 79 6c 6f 61 64 28 26 61 50   copyPayload(&aP
d87c0 61 79 6c 6f 61 64 5b 6f 66 66 73 65 74 5d 2c 20  ayload[offset], 
d87d0 70 42 75 66 2c 20 61 2c 20 65 4f 70 2c 20 70 50  pBuf, a, eOp, pP
d87e0 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  age->pDbPage);. 
d87f0 20 20 20 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20     offset = 0;. 
d8800 20 20 20 70 42 75 66 20 2b 3d 20 61 3b 0a 20 20     pBuf += a;.  
d8810 20 20 61 6d 74 20 2d 3d 20 61 3b 0a 20 20 7d 65    amt -= a;.  }e
d8820 6c 73 65 7b 0a 20 20 20 20 6f 66 66 73 65 74 20  lse{.    offset 
d8830 2d 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c  -= pCur->info.nL
d8840 6f 63 61 6c 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  ocal;.  }..  if(
d8850 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
d8860 26 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20 63  & amt>0 ){.    c
d8870 6f 6e 73 74 20 69 6e 74 20 6f 76 66 6c 53 69 7a  onst int ovflSiz
d8880 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53  e = pBt->usableS
d8890 69 7a 65 20 2d 20 34 3b 20 20 2f 2a 20 42 79 74  ize - 4;  /* Byt
d88a0 65 73 20 63 6f 6e 74 65 6e 74 20 70 65 72 20 6f  es content per o
d88b0 76 66 6c 20 70 61 67 65 20 2a 2f 0a 20 20 20 20  vfl page */.    
d88c0 50 67 6e 6f 20 6e 65 78 74 50 61 67 65 3b 0a 0a  Pgno nextPage;..
d88d0 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20 67      nextPage = g
d88e0 65 74 34 62 79 74 65 28 26 61 50 61 79 6c 6f 61  et4byte(&aPayloa
d88f0 64 5b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f  d[pCur->info.nLo
d8900 63 61 6c 5d 29 3b 0a 0a 23 69 66 6e 64 65 66 20  cal]);..#ifndef 
d8910 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52  SQLITE_OMIT_INCR
d8920 42 4c 4f 42 0a 20 20 20 20 2f 2a 20 49 66 20 74  BLOB.    /* If t
d8930 68 65 20 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e  he isIncrblobHan
d8940 64 6c 65 20 66 6c 61 67 20 69 73 20 73 65 74 20  dle flag is set 
d8950 61 6e 64 20 74 68 65 20 42 74 43 75 72 73 6f 72  and the BtCursor
d8960 2e 61 4f 76 65 72 66 6c 6f 77 5b 5d 0a 20 20 20  .aOverflow[].   
d8970 20 2a 2a 20 68 61 73 20 6e 6f 74 20 62 65 65 6e   ** has not been
d8980 20 61 6c 6c 6f 63 61 74 65 64 2c 20 61 6c 6c 6f   allocated, allo
d8990 63 61 74 65 20 69 74 20 6e 6f 77 2e 20 54 68 65  cate it now. The
d89a0 20 61 72 72 61 79 20 69 73 20 73 69 7a 65 64 20   array is sized 
d89b0 61 74 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 65 6e  at.    ** one en
d89c0 74 72 79 20 66 6f 72 20 65 61 63 68 20 6f 76 65  try for each ove
d89d0 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 68  rflow page in th
d89e0 65 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e  e overflow chain
d89f0 2e 20 54 68 65 0a 20 20 20 20 2a 2a 20 70 61 67  . The.    ** pag
d8a00 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
d8a10 66 69 72 73 74 20 6f 76 65 72 66 6c 6f 77 20 70  first overflow p
d8a20 61 67 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e  age is stored in
d8a30 20 61 4f 76 65 72 66 6c 6f 77 5b 30 5d 2c 0a 20   aOverflow[0],. 
d8a40 20 20 20 2a 2a 20 65 74 63 2e 20 41 20 76 61 6c     ** etc. A val
d8a50 75 65 20 6f 66 20 30 20 69 6e 20 74 68 65 20 61  ue of 0 in the a
d8a60 4f 76 65 72 66 6c 6f 77 5b 5d 20 61 72 72 61 79  Overflow[] array
d8a70 20 6d 65 61 6e 73 20 22 6e 6f 74 20 79 65 74 20   means "not yet 
d8a80 6b 6e 6f 77 6e 22 0a 20 20 20 20 2a 2a 20 28 74  known".    ** (t
d8a90 68 65 20 63 61 63 68 65 20 69 73 20 6c 61 7a 69  he cache is lazi
d8aa0 6c 79 20 70 6f 70 75 6c 61 74 65 64 29 2e 0a 20  ly populated).. 
d8ab0 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 43     */.    if( pC
d8ac0 75 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61  ur->isIncrblobHa
d8ad0 6e 64 6c 65 20 26 26 20 21 70 43 75 72 2d 3e 61  ndle && !pCur->a
d8ae0 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20  Overflow ){.    
d8af0 20 20 69 6e 74 20 6e 4f 76 66 6c 20 3d 20 28 70    int nOvfl = (p
d8b00 43 75 72 2d 3e 69 6e 66 6f 2e 6e 50 61 79 6c 6f  Cur->info.nPaylo
d8b10 61 64 2d 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c  ad-pCur->info.nL
d8b20 6f 63 61 6c 2b 6f 76 66 6c 53 69 7a 65 2d 31 29  ocal+ovflSize-1)
d8b30 2f 6f 76 66 6c 53 69 7a 65 3b 0a 20 20 20 20 20  /ovflSize;.     
d8b40 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77   pCur->aOverflow
d8b50 20 3d 20 28 50 67 6e 6f 20 2a 29 73 71 6c 69 74   = (Pgno *)sqlit
d8b60 65 4d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 50  eMalloc(sizeof(P
d8b70 67 6e 6f 29 2a 6e 4f 76 66 6c 29 3b 0a 20 20 20  gno)*nOvfl);.   
d8b80 20 20 20 69 66 28 20 6e 4f 76 66 6c 20 26 26 20     if( nOvfl && 
d8b90 21 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77  !pCur->aOverflow
d8ba0 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
d8bb0 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
d8bc0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
d8bd0 20 20 2f 2a 20 49 66 20 74 68 65 20 6f 76 65 72    /* If the over
d8be0 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63  flow page-list c
d8bf0 61 63 68 65 20 68 61 73 20 62 65 65 6e 20 61 6c  ache has been al
d8c00 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65 0a  located and the.
d8c10 20 20 20 20 2a 2a 20 65 6e 74 72 79 20 66 6f 72      ** entry for
d8c20 20 74 68 65 20 66 69 72 73 74 20 72 65 71 75 69   the first requi
d8c30 72 65 64 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  red overflow pag
d8c40 65 20 69 73 20 76 61 6c 69 64 2c 20 73 6b 69 70  e is valid, skip
d8c50 0a 20 20 20 20 2a 2a 20 64 69 72 65 63 74 6c 79  .    ** directly
d8c60 20 74 6f 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20   to it..    */. 
d8c70 20 20 20 69 66 28 20 70 43 75 72 2d 3e 61 4f 76     if( pCur->aOv
d8c80 65 72 66 6c 6f 77 20 26 26 20 70 43 75 72 2d 3e  erflow && pCur->
d8c90 61 4f 76 65 72 66 6c 6f 77 5b 6f 66 66 73 65 74  aOverflow[offset
d8ca0 2f 6f 76 66 6c 53 69 7a 65 5d 20 29 7b 0a 20 20  /ovflSize] ){.  
d8cb0 20 20 20 20 69 49 64 78 20 3d 20 28 6f 66 66 73      iIdx = (offs
d8cc0 65 74 2f 6f 76 66 6c 53 69 7a 65 29 3b 0a 20 20  et/ovflSize);.  
d8cd0 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20 70      nextPage = p
d8ce0 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69  Cur->aOverflow[i
d8cf0 49 64 78 5d 3b 0a 20 20 20 20 20 20 6f 66 66 73  Idx];.      offs
d8d00 65 74 20 3d 20 28 6f 66 66 73 65 74 25 6f 76 66  et = (offset%ovf
d8d10 6c 53 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 23 65  lSize);.    }.#e
d8d20 6e 64 69 66 0a 0a 20 20 20 20 66 6f 72 28 20 3b  ndif..    for( ;
d8d30 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
d8d40 26 20 61 6d 74 3e 30 20 26 26 20 6e 65 78 74 50  & amt>0 && nextP
d8d50 61 67 65 3b 20 69 49 64 78 2b 2b 29 7b 0a 0a 23  age; iIdx++){..#
d8d60 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
d8d70 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20  IT_INCRBLOB.    
d8d80 20 20 2f 2a 20 49 66 20 72 65 71 75 69 72 65 64    /* If required
d8d90 2c 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6f  , populate the o
d8da0 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73  verflow page-lis
d8db0 74 20 63 61 63 68 65 2e 20 2a 2f 0a 20 20 20 20  t cache. */.    
d8dc0 20 20 69 66 28 20 70 43 75 72 2d 3e 61 4f 76 65    if( pCur->aOve
d8dd0 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20  rflow ){.       
d8de0 20 61 73 73 65 72 74 28 21 70 43 75 72 2d 3e 61   assert(!pCur->a
d8df0 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d 20 7c  Overflow[iIdx] |
d8e00 7c 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f  | pCur->aOverflo
d8e10 77 5b 69 49 64 78 5d 3d 3d 6e 65 78 74 50 61 67  w[iIdx]==nextPag
d8e20 65 29 3b 0a 20 20 20 20 20 20 20 20 70 43 75 72  e);.        pCur
d8e30 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78  ->aOverflow[iIdx
d8e40 5d 20 3d 20 6e 65 78 74 50 61 67 65 3b 0a 20 20  ] = nextPage;.  
d8e50 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20      }.#endif..  
d8e60 20 20 20 20 69 66 28 20 6f 66 66 73 65 74 3e 3d      if( offset>=
d8e70 6f 76 66 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20  ovflSize ){.    
d8e80 20 20 20 20 2f 2a 20 54 68 65 20 6f 6e 6c 79 20      /* The only 
d8e90 72 65 61 73 6f 6e 20 74 6f 20 72 65 61 64 20 74  reason to read t
d8ea0 68 69 73 20 70 61 67 65 20 69 73 20 74 6f 20 6f  his page is to o
d8eb0 62 74 61 69 6e 20 74 68 65 20 70 61 67 65 0a 20  btain the page. 
d8ec0 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72         ** number
d8ed0 20 66 6f 72 20 74 68 65 20 6e 65 78 74 20 70 61   for the next pa
d8ee0 67 65 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c  ge in the overfl
d8ef0 6f 77 20 63 68 61 69 6e 2e 20 54 68 65 20 70 61  ow chain. The pa
d8f00 67 65 0a 09 2a 2a 20 64 61 74 61 20 69 73 20 6e  ge..** data is n
d8f10 6f 74 20 72 65 71 75 69 72 65 64 2e 20 53 6f 20  ot required. So 
d8f20 66 69 72 73 74 20 74 72 79 20 74 6f 20 6c 6f 6f  first try to loo
d8f30 6b 75 70 20 74 68 65 20 6f 76 65 72 66 6c 6f 77  kup the overflow
d8f40 0a 09 2a 2a 20 70 61 67 65 2d 6c 69 73 74 20 63  ..** page-list c
d8f50 61 63 68 65 2c 20 69 66 20 61 6e 79 2c 20 74 68  ache, if any, th
d8f60 65 6e 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20  en fall back to 
d8f70 74 68 65 20 67 65 74 4f 76 65 72 66 6c 6f 77 50  the getOverflowP
d8f80 61 67 65 28 29 0a 20 20 20 20 20 20 20 20 2a 2a  age().        **
d8f90 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 20 20 20   function..     
d8fa0 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51     */.#ifndef SQ
d8fb0 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c  LITE_OMIT_INCRBL
d8fc0 4f 42 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  OB.        if( p
d8fd0 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 26  Cur->aOverflow &
d8fe0 26 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f  & pCur->aOverflo
d8ff0 77 5b 69 49 64 78 2b 31 5d 20 29 7b 0a 20 20 20  w[iIdx+1] ){.   
d9000 20 20 20 20 20 20 20 6e 65 78 74 50 61 67 65 20         nextPage 
d9010 3d 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f  = pCur->aOverflo
d9020 77 5b 69 49 64 78 2b 31 5d 3b 0a 20 20 20 20 20  w[iIdx+1];.     
d9030 20 20 20 7d 20 65 6c 73 65 20 0a 23 65 6e 64 69     } else .#endi
d9040 66 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  f.          rc =
d9050 20 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 67 65   getOverflowPage
d9060 28 70 42 74 2c 20 6e 65 78 74 50 61 67 65 2c 20  (pBt, nextPage, 
d9070 30 2c 20 26 6e 65 78 74 50 61 67 65 29 3b 0a 20  0, &nextPage);. 
d9080 20 20 20 20 20 20 20 6f 66 66 73 65 74 20 2d 3d         offset -=
d9090 20 6f 76 66 6c 53 69 7a 65 3b 0a 20 20 20 20 20   ovflSize;.     
d90a0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
d90b0 2f 2a 20 4e 65 65 64 20 74 6f 20 72 65 61 64 20  /* Need to read 
d90c0 74 68 69 73 20 70 61 67 65 20 70 72 6f 70 65 72  this page proper
d90d0 6c 79 2e 20 49 74 20 63 6f 6e 74 61 69 6e 73 20  ly. It contains 
d90e0 73 6f 6d 65 20 6f 66 20 74 68 65 0a 20 20 20 20  some of the.    
d90f0 20 20 20 20 2a 2a 20 72 61 6e 67 65 20 6f 66 20      ** range of 
d9100 64 61 74 61 20 74 68 61 74 20 69 73 20 62 65 69  data that is bei
d9110 6e 67 20 72 65 61 64 20 28 65 4f 70 3d 3d 30 29  ng read (eOp==0)
d9120 20 6f 72 20 77 72 69 74 74 65 6e 20 28 65 4f 70   or written (eOp
d9130 21 3d 30 29 2e 0a 20 20 20 20 20 20 20 20 2a 2f  !=0)..        */
d9140 0a 20 20 20 20 20 20 20 20 44 62 50 61 67 65 20  .        DbPage 
d9150 2a 70 44 62 50 61 67 65 3b 0a 20 20 20 20 20 20  *pDbPage;.      
d9160 20 20 69 6e 74 20 61 20 3d 20 61 6d 74 3b 0a 20    int a = amt;. 
d9170 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
d9180 74 65 33 50 61 67 65 72 47 65 74 28 70 42 74 2d  te3PagerGet(pBt-
d9190 3e 70 50 61 67 65 72 2c 20 6e 65 78 74 50 61 67  >pPager, nextPag
d91a0 65 2c 20 26 70 44 62 50 61 67 65 29 3b 0a 20 20  e, &pDbPage);.  
d91b0 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
d91c0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
d91d0 20 20 20 20 20 61 50 61 79 6c 6f 61 64 20 3d 20       aPayload = 
d91e0 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44  sqlite3PagerGetD
d91f0 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20  ata(pDbPage);.  
d9200 20 20 20 20 20 20 20 20 6e 65 78 74 50 61 67 65          nextPage
d9210 20 3d 20 67 65 74 34 62 79 74 65 28 61 50 61 79   = get4byte(aPay
d9220 6c 6f 61 64 29 3b 0a 20 20 20 20 20 20 20 20 20  load);.         
d9230 20 69 66 28 20 61 20 2b 20 6f 66 66 73 65 74 20   if( a + offset 
d9240 3e 20 6f 76 66 6c 53 69 7a 65 20 29 7b 0a 20 20  > ovflSize ){.  
d9250 20 20 20 20 20 20 20 20 20 20 61 20 3d 20 6f 76            a = ov
d9260 66 6c 53 69 7a 65 20 2d 20 6f 66 66 73 65 74 3b  flSize - offset;
d9270 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
d9280 20 20 20 20 20 20 20 72 63 20 3d 20 63 6f 70 79         rc = copy
d9290 50 61 79 6c 6f 61 64 28 26 61 50 61 79 6c 6f 61  Payload(&aPayloa
d92a0 64 5b 6f 66 66 73 65 74 2b 34 5d 2c 20 70 42 75  d[offset+4], pBu
d92b0 66 2c 20 61 2c 20 65 4f 70 2c 20 70 44 62 50 61  f, a, eOp, pDbPa
d92c0 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  ge);.          s
d92d0 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
d92e0 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20  (pDbPage);.     
d92f0 20 20 20 20 20 6f 66 66 73 65 74 20 3d 20 30 3b       offset = 0;
d9300 0a 20 20 20 20 20 20 20 20 20 20 61 6d 74 20 2d  .          amt -
d9310 3d 20 61 3b 0a 20 20 20 20 20 20 20 20 20 20 70  = a;.          p
d9320 42 75 66 20 2b 3d 20 61 3b 0a 20 20 20 20 20 20  Buf += a;.      
d9330 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
d9340 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d  }.  }..  if( rc=
d9350 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 61 6d  =SQLITE_OK && am
d9360 74 3e 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  t>0 ){.    retur
d9370 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
d9380 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74  _BKPT;.  }.  ret
d9390 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
d93a0 20 52 65 61 64 20 70 61 72 74 20 6f 66 20 74 68   Read part of th
d93b0 65 20 6b 65 79 20 61 73 73 6f 63 69 61 74 65 64  e key associated
d93c0 20 77 69 74 68 20 63 75 72 73 6f 72 20 70 43 75   with cursor pCu
d93d0 72 2e 20 20 45 78 61 63 74 6c 79 0a 2a 2a 20 22  r.  Exactly.** "
d93e0 61 6d 74 22 20 62 79 74 65 73 20 77 69 6c 6c 20  amt" bytes will 
d93f0 62 65 20 74 72 61 6e 73 66 65 72 65 64 20 69 6e  be transfered in
d9400 74 6f 20 70 42 75 66 5b 5d 2e 20 20 54 68 65 20  to pBuf[].  The 
d9410 74 72 61 6e 73 66 65 72 0a 2a 2a 20 62 65 67 69  transfer.** begi
d9420 6e 73 20 61 74 20 22 6f 66 66 73 65 74 22 2e 0a  ns at "offset"..
d9430 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c  **.** Return SQL
d9440 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73  ITE_OK on succes
d9450 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f  s or an error co
d9460 64 65 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67  de if anything g
d9470 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20 41  oes.** wrong.  A
d9480 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72  n error is retur
d9490 6e 65 64 20 69 66 20 22 6f 66 66 73 65 74 2b 61  ned if "offset+a
d94a0 6d 74 22 20 69 73 20 6c 61 72 67 65 72 20 74 68  mt" is larger th
d94b0 61 6e 0a 2a 2a 20 74 68 65 20 61 76 61 69 6c 61  an.** the availa
d94c0 62 6c 65 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a  ble payload..*/.
d94d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
d94e0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b  nt sqlite3BtreeK
d94f0 65 79 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  ey(BtCursor *pCu
d9500 72 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75  r, u32 offset, u
d9510 33 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42  32 amt, void *pB
d9520 75 66 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  uf){.  int rc = 
d9530 72 65 73 74 6f 72 65 4f 72 43 6c 65 61 72 43 75  restoreOrClearCu
d9540 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75  rsorPosition(pCu
d9550 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  r);.  if( rc==SQ
d9560 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61  LITE_OK ){.    a
d9570 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74  ssert( pCur->eSt
d9580 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49  ate==CURSOR_VALI
d9590 44 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  D );.    assert(
d95a0 20 70 43 75 72 2d 3e 70 50 61 67 65 21 3d 30 20   pCur->pPage!=0 
d95b0 29 3b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d  );.    if( pCur-
d95c0 3e 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29  >pPage->intKey )
d95d0 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
d95e0 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
d95f0 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  PT;.    }.    as
d9600 73 65 72 74 28 20 70 43 75 72 2d 3e 70 50 61 67  sert( pCur->pPag
d9610 65 2d 3e 69 6e 74 4b 65 79 3d 3d 30 20 29 3b 0a  e->intKey==0 );.
d9620 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72      assert( pCur
d9630 2d 3e 69 64 78 3e 3d 30 20 26 26 20 70 43 75 72  ->idx>=0 && pCur
d9640 2d 3e 69 64 78 3c 70 43 75 72 2d 3e 70 50 61 67  ->idx<pCur->pPag
d9650 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20  e->nCell );.    
d9660 72 63 20 3d 20 61 63 63 65 73 73 50 61 79 6c 6f  rc = accessPaylo
d9670 61 64 28 70 43 75 72 2c 20 6f 66 66 73 65 74 2c  ad(pCur, offset,
d9680 20 61 6d 74 2c 20 28 75 6e 73 69 67 6e 65 64 20   amt, (unsigned 
d9690 63 68 61 72 2a 29 70 42 75 66 2c 20 30 2c 20 30  char*)pBuf, 0, 0
d96a0 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
d96b0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61  rc;.}../*.** Rea
d96c0 64 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61  d part of the da
d96d0 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ta associated wi
d96e0 74 68 20 63 75 72 73 6f 72 20 70 43 75 72 2e 20  th cursor pCur. 
d96f0 20 45 78 61 63 74 6c 79 0a 2a 2a 20 22 61 6d 74   Exactly.** "amt
d9700 22 20 62 79 74 65 73 20 77 69 6c 6c 20 62 65 20  " bytes will be 
d9710 74 72 61 6e 73 66 65 72 65 64 20 69 6e 74 6f 20  transfered into 
d9720 70 42 75 66 5b 5d 2e 20 20 54 68 65 20 74 72 61  pBuf[].  The tra
d9730 6e 73 66 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20  nsfer.** begins 
d9740 61 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a  at "offset"..**.
d9750 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  ** Return SQLITE
d9760 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 6f  _OK on success o
d9770 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  r an error code 
d9780 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73  if anything goes
d9790 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20 41 6e 20 65  .** wrong.  An e
d97a0 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64  rror is returned
d97b0 20 69 66 20 22 6f 66 66 73 65 74 2b 61 6d 74 22   if "offset+amt"
d97c0 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 0a   is larger than.
d97d0 2a 2a 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65  ** the available
d97e0 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 51 4c   payload..*/.SQL
d97f0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
d9800 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61  sqlite3BtreeData
d9810 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c  (BtCursor *pCur,
d9820 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32   u32 offset, u32
d9830 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42 75 66   amt, void *pBuf
d9840 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 72 65  ){.  int rc = re
d9850 73 74 6f 72 65 4f 72 43 6c 65 61 72 43 75 72 73  storeOrClearCurs
d9860 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29  orPosition(pCur)
d9870 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
d9880 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73  TE_OK ){.    ass
d9890 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74  ert( pCur->eStat
d98a0 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20  e==CURSOR_VALID 
d98b0 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
d98c0 43 75 72 2d 3e 70 50 61 67 65 21 3d 30 20 29 3b  Cur->pPage!=0 );
d98d0 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75  .    assert( pCu
d98e0 72 2d 3e 69 64 78 3e 3d 30 20 26 26 20 70 43 75  r->idx>=0 && pCu
d98f0 72 2d 3e 69 64 78 3c 70 43 75 72 2d 3e 70 50 61  r->idx<pCur->pPa
d9900 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20  ge->nCell );.   
d9910 20 72 63 20 3d 20 61 63 63 65 73 73 50 61 79 6c   rc = accessPayl
d9920 6f 61 64 28 70 43 75 72 2c 20 6f 66 66 73 65 74  oad(pCur, offset
d9930 2c 20 61 6d 74 2c 20 70 42 75 66 2c 20 31 2c 20  , amt, pBuf, 1, 
d9940 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  0);.  }.  return
d9950 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
d9960 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
d9970 6f 20 70 61 79 6c 6f 61 64 20 69 6e 66 6f 72 6d  o payload inform
d9980 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 65  ation from the e
d9990 6e 74 72 79 20 74 68 61 74 20 74 68 65 20 0a 2a  ntry that the .*
d99a0 2a 20 70 43 75 72 20 63 75 72 73 6f 72 20 69 73  * pCur cursor is
d99b0 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 54   pointing to.  T
d99c0 68 65 20 70 6f 69 6e 74 65 72 20 69 73 20 74 6f  he pointer is to
d99d0 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f   the beginning o
d99e0 66 0a 2a 2a 20 74 68 65 20 6b 65 79 20 69 66 20  f.** the key if 
d99f0 73 6b 69 70 4b 65 79 3d 3d 30 20 61 6e 64 20 69  skipKey==0 and i
d9a00 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20  t points to the 
d9a10 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 64 61 74  beginning of dat
d9a20 61 20 69 66 0a 2a 2a 20 73 6b 69 70 4b 65 79 3d  a if.** skipKey=
d9a30 3d 31 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 20  =1.  The number 
d9a40 6f 66 20 62 79 74 65 73 20 6f 66 20 61 76 61 69  of bytes of avai
d9a50 6c 61 62 6c 65 20 6b 65 79 2f 64 61 74 61 20 69  lable key/data i
d9a60 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 69 6e 74  s written.** int
d9a70 6f 20 2a 70 41 6d 74 2e 20 20 49 66 20 2a 70 41  o *pAmt.  If *pA
d9a80 6d 74 3d 3d 30 2c 20 74 68 65 6e 20 74 68 65 20  mt==0, then the 
d9a90 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 77  value returned w
d9aa0 69 6c 6c 20 6e 6f 74 20 62 65 0a 2a 2a 20 61 20  ill not be.** a 
d9ab0 76 61 6c 69 64 20 70 6f 69 6e 74 65 72 2e 0a 2a  valid pointer..*
d9ac0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
d9ad0 65 20 69 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61  e is an optimiza
d9ae0 74 69 6f 6e 2e 20 20 49 74 20 69 73 20 63 6f 6d  tion.  It is com
d9af0 6d 6f 6e 20 66 6f 72 20 74 68 65 20 65 6e 74 69  mon for the enti
d9b00 72 65 20 6b 65 79 0a 2a 2a 20 61 6e 64 20 64 61  re key.** and da
d9b10 74 61 20 74 6f 20 66 69 74 20 6f 6e 20 74 68 65  ta to fit on the
d9b20 20 6c 6f 63 61 6c 20 70 61 67 65 20 61 6e 64 20   local page and 
d9b30 66 6f 72 20 74 68 65 72 65 20 74 6f 20 62 65 20  for there to be 
d9b40 6e 6f 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 70  no overflow.** p
d9b50 61 67 65 73 2e 20 20 57 68 65 6e 20 74 68 61 74  ages.  When that
d9b60 20 69 73 20 73 6f 2c 20 74 68 69 73 20 72 6f 75   is so, this rou
d9b70 74 69 6e 65 20 63 61 6e 20 62 65 20 75 73 65 64  tine can be used
d9b80 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 0a 2a   to access the.*
d9b90 2a 20 6b 65 79 20 61 6e 64 20 64 61 74 61 20 77  * key and data w
d9ba0 69 74 68 6f 75 74 20 6d 61 6b 69 6e 67 20 61 20  ithout making a 
d9bb0 63 6f 70 79 2e 20 20 49 66 20 74 68 65 20 6b 65  copy.  If the ke
d9bc0 79 20 61 6e 64 2f 6f 72 20 64 61 74 61 20 73 70  y and/or data sp
d9bd0 69 6c 6c 73 0a 2a 2a 20 6f 6e 74 6f 20 6f 76 65  ills.** onto ove
d9be0 72 66 6c 6f 77 20 70 61 67 65 73 2c 20 74 68 65  rflow pages, the
d9bf0 6e 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28  n accessPayload(
d9c00 29 20 6d 75 73 74 20 62 65 20 75 73 65 64 20 74  ) must be used t
d9c10 6f 20 72 65 61 73 73 65 6d 62 6c 79 0a 2a 2a 20  o reassembly.** 
d9c20 74 68 65 20 6b 65 79 2f 64 61 74 61 20 61 6e 64  the key/data and
d9c30 20 63 6f 70 79 20 69 74 20 69 6e 74 6f 20 61 20   copy it into a 
d9c40 70 72 65 61 6c 6c 6f 63 61 74 65 64 20 62 75 66  preallocated buf
d9c50 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70  fer..**.** The p
d9c60 6f 69 6e 74 65 72 20 72 65 74 75 72 6e 65 64 20  ointer returned 
d9c70 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  by this routine 
d9c80 6c 6f 6f 6b 73 20 64 69 72 65 63 74 6c 79 20 69  looks directly i
d9c90 6e 74 6f 20 74 68 65 20 63 61 63 68 65 64 0a 2a  nto the cached.*
d9ca0 2a 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61  * page of the da
d9cb0 74 61 62 61 73 65 2e 20 20 54 68 65 20 64 61 74  tabase.  The dat
d9cc0 61 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 6f  a might change o
d9cd0 72 20 6d 6f 76 65 20 74 68 65 20 6e 65 78 74 20  r move the next 
d9ce0 74 69 6d 65 0a 2a 2a 20 61 6e 79 20 62 74 72 65  time.** any btre
d9cf0 65 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  e routine is cal
d9d00 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  led..*/.static c
d9d10 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
d9d20 61 72 20 2a 66 65 74 63 68 50 61 79 6c 6f 61 64  ar *fetchPayload
d9d30 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43  (.  BtCursor *pC
d9d40 75 72 2c 20 20 20 20 20 20 2f 2a 20 43 75 72 73  ur,      /* Curs
d9d50 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 65  or pointing to e
d9d60 6e 74 72 79 20 74 6f 20 72 65 61 64 20 66 72 6f  ntry to read fro
d9d70 6d 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 41 6d 74  m */.  int *pAmt
d9d80 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57  ,           /* W
d9d90 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20  rite the number 
d9da0 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 62 79 74  of available byt
d9db0 65 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74  es here */.  int
d9dc0 20 73 6b 69 70 4b 65 79 20 20 20 20 20 20 20 20   skipKey        
d9dd0 20 20 2f 2a 20 72 65 61 64 20 62 65 67 69 6e 6e    /* read beginn
d9de0 69 6e 67 20 61 74 20 64 61 74 61 20 69 66 20 74  ing at data if t
d9df0 68 69 73 20 69 73 20 74 72 75 65 20 2a 2f 0a 29  his is true */.)
d9e00 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  {.  unsigned cha
d9e10 72 20 2a 61 50 61 79 6c 6f 61 64 3b 0a 20 20 4d  r *aPayload;.  M
d9e20 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20  emPage *pPage;. 
d9e30 20 75 33 32 20 6e 4b 65 79 3b 0a 20 20 69 6e 74   u32 nKey;.  int
d9e40 20 6e 4c 6f 63 61 6c 3b 0a 0a 20 20 61 73 73 65   nLocal;..  asse
d9e50 72 74 28 20 70 43 75 72 21 3d 30 20 26 26 20 70  rt( pCur!=0 && p
d9e60 43 75 72 2d 3e 70 50 61 67 65 21 3d 30 20 29 3b  Cur->pPage!=0 );
d9e70 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
d9e80 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
d9e90 56 41 4c 49 44 20 29 3b 0a 20 20 70 50 61 67 65  VALID );.  pPage
d9ea0 20 3d 20 70 43 75 72 2d 3e 70 50 61 67 65 3b 0a   = pCur->pPage;.
d9eb0 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
d9ec0 69 64 78 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e  idx>=0 && pCur->
d9ed0 69 64 78 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  idx<pPage->nCell
d9ee0 20 29 3b 0a 20 20 67 65 74 43 65 6c 6c 49 6e 66   );.  getCellInf
d9ef0 6f 28 70 43 75 72 29 3b 0a 20 20 61 50 61 79 6c  o(pCur);.  aPayl
d9f00 6f 61 64 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f  oad = pCur->info
d9f10 2e 70 43 65 6c 6c 3b 0a 20 20 61 50 61 79 6c 6f  .pCell;.  aPaylo
d9f20 61 64 20 2b 3d 20 70 43 75 72 2d 3e 69 6e 66 6f  ad += pCur->info
d9f30 2e 6e 48 65 61 64 65 72 3b 0a 20 20 69 66 28 20  .nHeader;.  if( 
d9f40 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b  pPage->intKey ){
d9f50 0a 20 20 20 20 6e 4b 65 79 20 3d 20 30 3b 0a 20  .    nKey = 0;. 
d9f60 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 4b 65 79   }else{.    nKey
d9f70 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b   = pCur->info.nK
d9f80 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 6b  ey;.  }.  if( sk
d9f90 69 70 4b 65 79 20 29 7b 0a 20 20 20 20 61 50 61  ipKey ){.    aPa
d9fa0 79 6c 6f 61 64 20 2b 3d 20 6e 4b 65 79 3b 0a 20  yload += nKey;. 
d9fb0 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70 43 75 72     nLocal = pCur
d9fc0 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2d 20  ->info.nLocal - 
d9fd0 6e 4b 65 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  nKey;.  }else{. 
d9fe0 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70 43 75 72     nLocal = pCur
d9ff0 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20  ->info.nLocal;. 
da000 20 20 20 69 66 28 20 6e 4c 6f 63 61 6c 3e 6e 4b     if( nLocal>nK
da010 65 79 20 29 7b 0a 20 20 20 20 20 20 6e 4c 6f 63  ey ){.      nLoc
da020 61 6c 20 3d 20 6e 4b 65 79 3b 0a 20 20 20 20 7d  al = nKey;.    }
da030 0a 20 20 7d 0a 20 20 2a 70 41 6d 74 20 3d 20 6e  .  }.  *pAmt = n
da040 4c 6f 63 61 6c 3b 0a 20 20 72 65 74 75 72 6e 20  Local;.  return 
da050 61 50 61 79 6c 6f 61 64 3b 0a 7d 0a 0a 0a 2f 2a  aPayload;.}.../*
da060 0a 2a 2a 20 46 6f 72 20 74 68 65 20 65 6e 74 72  .** For the entr
da070 79 20 74 68 61 74 20 63 75 72 73 6f 72 20 70 43  y that cursor pC
da080 75 72 20 69 73 20 70 6f 69 6e 74 20 74 6f 2c 20  ur is point to, 
da090 72 65 74 75 72 6e 20 61 73 0a 2a 2a 20 6d 61 6e  return as.** man
da0a0 79 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 6b  y bytes of the k
da0b0 65 79 20 6f 72 20 64 61 74 61 20 61 73 20 61 72  ey or data as ar
da0c0 65 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74  e available on t
da0d0 68 65 20 6c 6f 63 61 6c 0a 2a 2a 20 62 2d 74 72  he local.** b-tr
da0e0 65 65 20 70 61 67 65 2e 20 20 57 72 69 74 65 20  ee page.  Write 
da0f0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 76  the number of av
da100 61 69 6c 61 62 6c 65 20 62 79 74 65 73 20 69 6e  ailable bytes in
da110 74 6f 20 2a 70 41 6d 74 2e 0a 2a 2a 0a 2a 2a 20  to *pAmt..**.** 
da120 54 68 65 20 70 6f 69 6e 74 65 72 20 72 65 74 75  The pointer retu
da130 72 6e 65 64 20 69 73 20 65 70 68 65 6d 65 72 61  rned is ephemera
da140 6c 2e 20 20 54 68 65 20 6b 65 79 2f 64 61 74 61  l.  The key/data
da150 20 6d 61 79 20 6d 6f 76 65 0a 2a 2a 20 6f 72 20   may move.** or 
da160 62 65 20 64 65 73 74 72 6f 79 65 64 20 6f 6e 20  be destroyed on 
da170 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f  the next call to
da180 20 61 6e 79 20 42 74 72 65 65 20 72 6f 75 74 69   any Btree routi
da190 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  ne..**.** These 
da1a0 72 6f 75 74 69 6e 65 73 20 69 73 20 75 73 65 64  routines is used
da1b0 20 74 6f 20 67 65 74 20 71 75 69 63 6b 20 61 63   to get quick ac
da1c0 63 65 73 73 20 74 6f 20 6b 65 79 20 61 6e 64 20  cess to key and 
da1d0 64 61 74 61 0a 2a 2a 20 69 6e 20 74 68 65 20 63  data.** in the c
da1e0 6f 6d 6d 6f 6e 20 63 61 73 65 20 77 68 65 72 65  ommon case where
da1f0 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   no overflow pag
da200 65 73 20 61 72 65 20 75 73 65 64 2e 0a 2a 2f 0a  es are used..*/.
da210 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
da220 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
da230 65 33 42 74 72 65 65 4b 65 79 46 65 74 63 68 28  e3BtreeKeyFetch(
da240 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20  BtCursor *pCur, 
da250 69 6e 74 20 2a 70 41 6d 74 29 7b 0a 20 20 69 66  int *pAmt){.  if
da260 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d  ( pCur->eState==
da270 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 7b 0a  CURSOR_VALID ){.
da280 20 20 20 20 72 65 74 75 72 6e 20 28 63 6f 6e 73      return (cons
da290 74 20 76 6f 69 64 2a 29 66 65 74 63 68 50 61 79  t void*)fetchPay
da2a0 6c 6f 61 64 28 70 43 75 72 2c 20 70 41 6d 74 2c  load(pCur, pAmt,
da2b0 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72   0);.  }.  retur
da2c0 6e 20 30 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  n 0;.}.SQLITE_PR
da2d0 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64  IVATE const void
da2e0 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 44 61   *sqlite3BtreeDa
da2f0 74 61 46 65 74 63 68 28 42 74 43 75 72 73 6f 72  taFetch(BtCursor
da300 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 41 6d   *pCur, int *pAm
da310 74 29 7b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e  t){.  if( pCur->
da320 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56  eState==CURSOR_V
da330 41 4c 49 44 20 29 7b 0a 20 20 20 20 72 65 74 75  ALID ){.    retu
da340 72 6e 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 29  rn (const void*)
da350 66 65 74 63 68 50 61 79 6c 6f 61 64 28 70 43 75  fetchPayload(pCu
da360 72 2c 20 70 41 6d 74 2c 20 31 29 3b 0a 20 20 7d  r, pAmt, 1);.  }
da370 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
da380 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20  ./*.** Move the 
da390 63 75 72 73 6f 72 20 64 6f 77 6e 20 74 6f 20 61  cursor down to a
da3a0 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65 2e   new child page.
da3b0 20 20 54 68 65 20 6e 65 77 50 67 6e 6f 20 61 72    The newPgno ar
da3c0 67 75 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a 2a  gument is the.**
da3d0 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20   page number of 
da3e0 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 20 74  the child page t
da3f0 6f 20 6d 6f 76 65 20 74 6f 2e 0a 2a 2f 0a 73 74  o move to..*/.st
da400 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f 43  atic int moveToC
da410 68 69 6c 64 28 42 74 43 75 72 73 6f 72 20 2a 70  hild(BtCursor *p
da420 43 75 72 2c 20 75 33 32 20 6e 65 77 50 67 6e 6f  Cur, u32 newPgno
da430 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d  ){.  int rc;.  M
da440 65 6d 50 61 67 65 20 2a 70 4e 65 77 50 61 67 65  emPage *pNewPage
da450 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f 6c  ;.  MemPage *pOl
da460 64 50 61 67 65 3b 0a 20 20 42 74 53 68 61 72 65  dPage;.  BtShare
da470 64 20 2a 70 42 74 20 3d 20 70 43 75 72 2d 3e 70  d *pBt = pCur->p
da480 42 74 72 65 65 2d 3e 70 42 74 3b 0a 0a 20 20 61  Btree->pBt;..  a
da490 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74  ssert( pCur->eSt
da4a0 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49  ate==CURSOR_VALI
da4b0 44 20 29 3b 0a 20 20 72 63 20 3d 20 67 65 74 41  D );.  rc = getA
da4c0 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74 2c 20  ndInitPage(pBt, 
da4d0 6e 65 77 50 67 6e 6f 2c 20 26 70 4e 65 77 50 61  newPgno, &pNewPa
da4e0 67 65 2c 20 70 43 75 72 2d 3e 70 50 61 67 65 29  ge, pCur->pPage)
da4f0 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74  ;.  if( rc ) ret
da500 75 72 6e 20 72 63 3b 0a 20 20 70 4e 65 77 50 61  urn rc;.  pNewPa
da510 67 65 2d 3e 69 64 78 50 61 72 65 6e 74 20 3d 20  ge->idxParent = 
da520 70 43 75 72 2d 3e 69 64 78 3b 0a 20 20 70 4f 6c  pCur->idx;.  pOl
da530 64 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 70 50  dPage = pCur->pP
da540 61 67 65 3b 0a 20 20 70 4f 6c 64 50 61 67 65 2d  age;.  pOldPage-
da550 3e 69 64 78 53 68 69 66 74 20 3d 20 30 3b 0a 20  >idxShift = 0;. 
da560 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4f 6c   releasePage(pOl
da570 64 50 61 67 65 29 3b 0a 20 20 70 43 75 72 2d 3e  dPage);.  pCur->
da580 70 50 61 67 65 20 3d 20 70 4e 65 77 50 61 67 65  pPage = pNewPage
da590 3b 0a 20 20 70 43 75 72 2d 3e 69 64 78 20 3d 20  ;.  pCur->idx = 
da5a0 30 3b 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e  0;.  pCur->info.
da5b0 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 69 66 28  nSize = 0;.  if(
da5c0 20 70 4e 65 77 50 61 67 65 2d 3e 6e 43 65 6c 6c   pNewPage->nCell
da5d0 3c 31 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  <1 ){.    return
da5e0 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
da5f0 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75  BKPT;.  }.  retu
da600 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
da610 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72  ./*.** Return tr
da620 75 65 20 69 66 20 74 68 65 20 70 61 67 65 20 69  ue if the page i
da630 73 20 74 68 65 20 76 69 72 74 75 61 6c 20 72 6f  s the virtual ro
da640 6f 74 20 6f 66 20 69 74 73 20 74 61 62 6c 65 2e  ot of its table.
da650 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 69 72 74 75  .**.** The virtu
da660 61 6c 20 72 6f 6f 74 20 70 61 67 65 20 69 73 20  al root page is 
da670 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 66 6f  the root page fo
da680 72 20 6d 6f 73 74 20 74 61 62 6c 65 73 2e 20 20  r most tables.  
da690 42 75 74 0a 2a 2a 20 66 6f 72 20 74 68 65 20 74  But.** for the t
da6a0 61 62 6c 65 20 72 6f 6f 74 65 64 20 6f 6e 20 70  able rooted on p
da6b0 61 67 65 20 31 2c 20 73 6f 6d 65 74 69 6d 65 20  age 1, sometime 
da6c0 74 68 65 20 72 65 61 6c 20 72 6f 6f 74 20 70 61  the real root pa
da6d0 67 65 0a 2a 2a 20 69 73 20 65 6d 70 74 79 20 65  ge.** is empty e
da6e0 78 63 65 70 74 20 66 6f 72 20 74 68 65 20 72 69  xcept for the ri
da6f0 67 68 74 2d 70 6f 69 6e 74 65 72 2e 20 20 49 6e  ght-pointer.  In
da700 20 73 75 63 68 20 63 61 73 65 73 20 74 68 65 0a   such cases the.
da710 2a 2a 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20  ** virtual root 
da720 70 61 67 65 20 69 73 20 74 68 65 20 70 61 67 65  page is the page
da730 20 74 68 61 74 20 74 68 65 20 72 69 67 68 74 2d   that the right-
da740 70 6f 69 6e 74 65 72 20 6f 66 20 70 61 67 65 0a  pointer of page.
da750 2a 2a 20 31 20 69 73 20 70 6f 69 6e 74 69 6e 67  ** 1 is pointing
da760 20 74 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50   to..*/.SQLITE_P
da770 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
da780 65 33 42 74 72 65 65 49 73 52 6f 6f 74 50 61 67  e3BtreeIsRootPag
da790 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65  e(MemPage *pPage
da7a0 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  ){.  MemPage *pP
da7b0 61 72 65 6e 74 20 3d 20 70 50 61 67 65 2d 3e 70  arent = pPage->p
da7c0 50 61 72 65 6e 74 3b 0a 20 20 69 66 28 20 70 50  Parent;.  if( pP
da7d0 61 72 65 6e 74 3d 3d 30 20 29 20 72 65 74 75 72  arent==0 ) retur
da7e0 6e 20 31 3b 0a 20 20 69 66 28 20 70 50 61 72 65  n 1;.  if( pPare
da7f0 6e 74 2d 3e 70 67 6e 6f 3e 31 20 29 20 72 65 74  nt->pgno>1 ) ret
da800 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 67 65 74  urn 0;.  if( get
da810 32 62 79 74 65 28 26 70 50 61 72 65 6e 74 2d 3e  2byte(&pParent->
da820 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68  aData[pParent->h
da830 64 72 4f 66 66 73 65 74 2b 33 5d 29 3d 3d 30 20  drOffset+3])==0 
da840 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 72 65  ) return 1;.  re
da850 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
da860 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72   Move the cursor
da870 20 75 70 20 74 6f 20 74 68 65 20 70 61 72 65 6e   up to the paren
da880 74 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 70 43  t page..**.** pC
da890 75 72 2d 3e 69 64 78 20 69 73 20 73 65 74 20 74  ur->idx is set t
da8a0 6f 20 74 68 65 20 63 65 6c 6c 20 69 6e 64 65 78  o the cell index
da8b0 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74   that contains t
da8c0 68 65 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f  he pointer.** to
da8d0 20 74 68 65 20 70 61 67 65 20 77 65 20 61 72 65   the page we are
da8e0 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 2e 20 20 49   coming from.  I
da8f0 66 20 77 65 20 61 72 65 20 63 6f 6d 69 6e 67 20  f we are coming 
da900 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 72 69 67 68  from the.** righ
da910 74 2d 6d 6f 73 74 20 63 68 69 6c 64 20 70 61 67  t-most child pag
da920 65 20 74 68 65 6e 20 70 43 75 72 2d 3e 69 64 78  e then pCur->idx
da930 20 69 73 20 73 65 74 20 74 6f 20 6f 6e 65 20 6d   is set to one m
da940 6f 72 65 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20  ore than.** the 
da950 6c 61 72 67 65 73 74 20 63 65 6c 6c 20 69 6e 64  largest cell ind
da960 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ex..*/.SQLITE_PR
da970 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
da980 65 33 42 74 72 65 65 4d 6f 76 65 54 6f 50 61 72  e3BtreeMoveToPar
da990 65 6e 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43  ent(BtCursor *pC
da9a0 75 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  ur){.  MemPage *
da9b0 70 50 61 72 65 6e 74 3b 0a 20 20 4d 65 6d 50 61  pParent;.  MemPa
da9c0 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 69 6e 74  ge *pPage;.  int
da9d0 20 69 64 78 50 61 72 65 6e 74 3b 0a 0a 20 20 61   idxParent;..  a
da9e0 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74  ssert( pCur->eSt
da9f0 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49  ate==CURSOR_VALI
daa00 44 20 29 3b 0a 20 20 70 50 61 67 65 20 3d 20 70  D );.  pPage = p
daa10 43 75 72 2d 3e 70 50 61 67 65 3b 0a 20 20 61 73  Cur->pPage;.  as
daa20 73 65 72 74 28 20 70 50 61 67 65 21 3d 30 20 29  sert( pPage!=0 )
daa30 3b 0a 20 20 61 73 73 65 72 74 28 20 21 73 71 6c  ;.  assert( !sql
daa40 69 74 65 33 42 74 72 65 65 49 73 52 6f 6f 74 50  ite3BtreeIsRootP
daa50 61 67 65 28 70 50 61 67 65 29 20 29 3b 0a 20 20  age(pPage) );.  
daa60 70 50 61 72 65 6e 74 20 3d 20 70 50 61 67 65 2d  pParent = pPage-
daa70 3e 70 50 61 72 65 6e 74 3b 0a 20 20 61 73 73 65  >pParent;.  asse
daa80 72 74 28 20 70 50 61 72 65 6e 74 21 3d 30 20 29  rt( pParent!=0 )
daa90 3b 0a 20 20 69 64 78 50 61 72 65 6e 74 20 3d 20  ;.  idxParent = 
daaa0 70 50 61 67 65 2d 3e 69 64 78 50 61 72 65 6e 74  pPage->idxParent
daab0 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72  ;.  sqlite3Pager
daac0 52 65 66 28 70 50 61 72 65 6e 74 2d 3e 70 44 62  Ref(pParent->pDb
daad0 50 61 67 65 29 3b 0a 20 20 72 65 6c 65 61 73 65  Page);.  release
daae0 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 70  Page(pPage);.  p
daaf0 43 75 72 2d 3e 70 50 61 67 65 20 3d 20 70 50 61  Cur->pPage = pPa
dab00 72 65 6e 74 3b 0a 20 20 70 43 75 72 2d 3e 69 6e  rent;.  pCur->in
dab10 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20  fo.nSize = 0;.  
dab20 61 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 2d  assert( pParent-
dab30 3e 69 64 78 53 68 69 66 74 3d 3d 30 20 29 3b 0a  >idxShift==0 );.
dab40 20 20 70 43 75 72 2d 3e 69 64 78 20 3d 20 69 64    pCur->idx = id
dab50 78 50 61 72 65 6e 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  xParent;.}../*.*
dab60 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f  * Move the curso
dab70 72 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 70 61  r to the root pa
dab80 67 65 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  ge.*/.static int
dab90 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 42 74 43 75   moveToRoot(BtCu
daba0 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 4d  rsor *pCur){.  M
dabb0 65 6d 50 61 67 65 20 2a 70 52 6f 6f 74 3b 0a 20  emPage *pRoot;. 
dabc0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
dabd0 5f 4f 4b 3b 0a 20 20 42 74 53 68 61 72 65 64 20  _OK;.  BtShared 
dabe0 2a 70 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 74  *pBt = pCur->pBt
dabf0 72 65 65 2d 3e 70 42 74 3b 0a 0a 20 20 69 66 28  ree->pBt;..  if(
dac00 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
dac10 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45  URSOR_REQUIRESEE
dac20 4b 20 29 7b 0a 20 20 20 20 63 6c 65 61 72 43 75  K ){.    clearCu
dac30 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75  rsorPosition(pCu
dac40 72 29 3b 0a 20 20 7d 0a 20 20 70 52 6f 6f 74 20  r);.  }.  pRoot 
dac50 3d 20 70 43 75 72 2d 3e 70 50 61 67 65 3b 0a 20  = pCur->pPage;. 
dac60 20 69 66 28 20 70 52 6f 6f 74 20 26 26 20 70 52   if( pRoot && pR
dac70 6f 6f 74 2d 3e 70 67 6e 6f 3d 3d 70 43 75 72 2d  oot->pgno==pCur-
dac80 3e 70 67 6e 6f 52 6f 6f 74 20 29 7b 0a 20 20 20  >pgnoRoot ){.   
dac90 20 61 73 73 65 72 74 28 20 70 52 6f 6f 74 2d 3e   assert( pRoot->
daca0 69 73 49 6e 69 74 20 29 3b 0a 20 20 7d 65 6c 73  isInit );.  }els
dacb0 65 7b 0a 20 20 20 20 69 66 28 20 0a 20 20 20 20  e{.    if( .    
dacc0 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63    SQLITE_OK!=(rc
dacd0 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67   = getAndInitPag
dace0 65 28 70 42 74 2c 20 70 43 75 72 2d 3e 70 67 6e  e(pBt, pCur->pgn
dacf0 6f 52 6f 6f 74 2c 20 26 70 52 6f 6f 74 2c 20 30  oRoot, &pRoot, 0
dad00 29 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  )).    ){.      
dad10 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43  pCur->eState = C
dad20 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20  URSOR_INVALID;. 
dad30 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
dad40 20 20 20 20 7d 0a 20 20 20 20 72 65 6c 65 61 73      }.    releas
dad50 65 50 61 67 65 28 70 43 75 72 2d 3e 70 50 61 67  ePage(pCur->pPag
dad60 65 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e 70 50  e);.    pCur->pP
dad70 61 67 65 20 3d 20 70 52 6f 6f 74 3b 0a 20 20 7d  age = pRoot;.  }
dad80 0a 20 20 70 43 75 72 2d 3e 69 64 78 20 3d 20 30  .  pCur->idx = 0
dad90 3b 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e  ;.  pCur->info.n
dada0 53 69 7a 65 20 3d 20 30 3b 0a 20 20 69 66 28 20  Size = 0;.  if( 
dadb0 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 3d 3d 30 20  pRoot->nCell==0 
dadc0 26 26 20 21 70 52 6f 6f 74 2d 3e 6c 65 61 66 20  && !pRoot->leaf 
dadd0 29 7b 0a 20 20 20 20 50 67 6e 6f 20 73 75 62 70  ){.    Pgno subp
dade0 61 67 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28  age;.    assert(
dadf0 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 3d 3d 31 20   pRoot->pgno==1 
dae00 29 3b 0a 20 20 20 20 73 75 62 70 61 67 65 20 3d  );.    subpage =
dae10 20 67 65 74 34 62 79 74 65 28 26 70 52 6f 6f 74   get4byte(&pRoot
dae20 2d 3e 61 44 61 74 61 5b 70 52 6f 6f 74 2d 3e 68  ->aData[pRoot->h
dae30 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20  drOffset+8]);.  
dae40 20 20 61 73 73 65 72 74 28 20 73 75 62 70 61 67    assert( subpag
dae50 65 3e 30 20 29 3b 0a 20 20 20 20 70 43 75 72 2d  e>0 );.    pCur-
dae60 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52  >eState = CURSOR
dae70 5f 56 41 4c 49 44 3b 0a 20 20 20 20 72 63 20 3d  _VALID;.    rc =
dae80 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75   moveToChild(pCu
dae90 72 2c 20 73 75 62 70 61 67 65 29 3b 0a 20 20 7d  r, subpage);.  }
daea0 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20  .  pCur->eState 
daeb0 3d 20 28 28 70 43 75 72 2d 3e 70 50 61 67 65 2d  = ((pCur->pPage-
daec0 3e 6e 43 65 6c 6c 3e 30 29 3f 43 55 52 53 4f 52  >nCell>0)?CURSOR
daed0 5f 56 41 4c 49 44 3a 43 55 52 53 4f 52 5f 49 4e  _VALID:CURSOR_IN
daee0 56 41 4c 49 44 29 3b 0a 20 20 72 65 74 75 72 6e  VALID);.  return
daef0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f   rc;.}../*.** Mo
daf00 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 64 6f  ve the cursor do
daf10 77 6e 20 74 6f 20 74 68 65 20 6c 65 66 74 2d 6d  wn to the left-m
daf20 6f 73 74 20 6c 65 61 66 20 65 6e 74 72 79 20 62  ost leaf entry b
daf30 65 6e 65 61 74 68 20 74 68 65 0a 2a 2a 20 65 6e  eneath the.** en
daf40 74 72 79 20 74 6f 20 77 68 69 63 68 20 69 74 20  try to which it 
daf50 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69  is currently poi
daf60 6e 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  nting..**.** The
daf70 20 6c 65 66 74 2d 6d 6f 73 74 20 6c 65 61 66 20   left-most leaf 
daf80 69 73 20 74 68 65 20 6f 6e 65 20 77 69 74 68 20  is the one with 
daf90 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 6b 65 79  the smallest key
dafa0 20 2d 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20   - the first.** 
dafb0 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64  in ascending ord
dafc0 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  er..*/.static in
dafd0 74 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74  t moveToLeftmost
dafe0 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29  (BtCursor *pCur)
daff0 7b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20  {.  Pgno pgno;. 
db000 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61   int rc;.  MemPa
db010 67 65 20 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73  ge *pPage;..  as
db020 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61  sert( pCur->eSta
db030 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44  te==CURSOR_VALID
db040 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 21 28 70   );.  while( !(p
db050 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 70 50 61  Page = pCur->pPa
db060 67 65 29 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  ge)->leaf ){.   
db070 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69   assert( pCur->i
db080 64 78 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e 69  dx>=0 && pCur->i
db090 64 78 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20  dx<pPage->nCell 
db0a0 29 3b 0a 20 20 20 20 70 67 6e 6f 20 3d 20 67 65  );.    pgno = ge
db0b0 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28  t4byte(findCell(
db0c0 70 50 61 67 65 2c 20 70 43 75 72 2d 3e 69 64 78  pPage, pCur->idx
db0d0 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76  ));.    rc = mov
db0e0 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 70  eToChild(pCur, p
db0f0 67 6e 6f 29 3b 0a 20 20 20 20 69 66 28 20 72 63  gno);.    if( rc
db100 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
db110 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
db120 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d  E_OK;.}../*.** M
db130 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 64  ove the cursor d
db140 6f 77 6e 20 74 6f 20 74 68 65 20 72 69 67 68 74  own to the right
db150 2d 6d 6f 73 74 20 6c 65 61 66 20 65 6e 74 72 79  -most leaf entry
db160 20 62 65 6e 65 61 74 68 20 74 68 65 0a 2a 2a 20   beneath the.** 
db170 70 61 67 65 20 74 6f 20 77 68 69 63 68 20 69 74  page to which it
db180 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f   is currently po
db190 69 6e 74 69 6e 67 2e 20 20 4e 6f 74 69 63 65 20  inting.  Notice 
db1a0 74 68 65 20 64 69 66 66 65 72 65 6e 63 65 0a 2a  the difference.*
db1b0 2a 20 62 65 74 77 65 65 6e 20 6d 6f 76 65 54 6f  * between moveTo
db1c0 4c 65 66 74 6d 6f 73 74 28 29 20 61 6e 64 20 6d  Leftmost() and m
db1d0 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 29  oveToRightmost()
db1e0 2e 20 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73  .  moveToLeftmos
db1f0 74 28 29 0a 2a 2a 20 66 69 6e 64 73 20 74 68 65  t().** finds the
db200 20 6c 65 66 74 2d 6d 6f 73 74 20 65 6e 74 72 79   left-most entry
db210 20 62 65 6e 65 61 74 68 20 74 68 65 20 2a 65 6e   beneath the *en
db220 74 72 79 2a 20 77 68 65 72 65 61 73 20 6d 6f 76  try* whereas mov
db230 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 29 0a 2a  eToRightmost().*
db240 2a 20 66 69 6e 64 73 20 74 68 65 20 72 69 67 68  * finds the righ
db250 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 62 65 6e  t-most entry ben
db260 65 61 74 68 20 74 68 65 20 2a 70 61 67 65 2a 2e  eath the *page*.
db270 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 69 67 68 74  .**.** The right
db280 2d 6d 6f 73 74 20 65 6e 74 72 79 20 69 73 20 74  -most entry is t
db290 68 65 20 6f 6e 65 20 77 69 74 68 20 74 68 65 20  he one with the 
db2a0 6c 61 72 67 65 73 74 20 6b 65 79 20 2d 20 74 68  largest key - th
db2b0 65 20 6c 61 73 74 0a 2a 2a 20 6b 65 79 20 69 6e  e last.** key in
db2c0 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72   ascending order
db2d0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
db2e0 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28  moveToRightmost(
db2f0 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b  BtCursor *pCur){
db300 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20  .  Pgno pgno;.  
db310 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61 67  int rc;.  MemPag
db320 65 20 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73 73  e *pPage;..  ass
db330 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74  ert( pCur->eStat
db340 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20  e==CURSOR_VALID 
db350 29 3b 0a 20 20 77 68 69 6c 65 28 20 21 28 70 50  );.  while( !(pP
db360 61 67 65 20 3d 20 70 43 75 72 2d 3e 70 50 61 67  age = pCur->pPag
db370 65 29 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20  e)->leaf ){.    
db380 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28  pgno = get4byte(
db390 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50  &pPage->aData[pP
db3a0 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38  age->hdrOffset+8
db3b0 5d 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e 69 64  ]);.    pCur->id
db3c0 78 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  x = pPage->nCell
db3d0 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54  ;.    rc = moveT
db3e0 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 70 67 6e  oChild(pCur, pgn
db3f0 6f 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  o);.    if( rc )
db400 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
db410 20 20 70 43 75 72 2d 3e 69 64 78 20 3d 20 70 50    pCur->idx = pP
db420 61 67 65 2d 3e 6e 43 65 6c 6c 20 2d 20 31 3b 0a  age->nCell - 1;.
db430 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69    pCur->info.nSi
db440 7a 65 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e  ze = 0;.  return
db450 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
db460 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f  * Move the curso
db470 72 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 65  r to the first e
db480 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61 62 6c  ntry in the tabl
db490 65 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54  e.  Return SQLIT
db4a0 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63 63 65  E_OK.** on succe
db4b0 73 73 2e 20 20 53 65 74 20 2a 70 52 65 73 20 74  ss.  Set *pRes t
db4c0 6f 20 30 20 69 66 20 74 68 65 20 63 75 72 73 6f  o 0 if the curso
db4d0 72 20 61 63 74 75 61 6c 6c 79 20 70 6f 69 6e 74  r actually point
db4e0 73 20 74 6f 20 73 6f 6d 65 74 68 69 6e 67 0a 2a  s to something.*
db4f0 2a 20 6f 72 20 73 65 74 20 2a 70 52 65 73 20 74  * or set *pRes t
db500 6f 20 31 20 69 66 20 74 68 65 20 74 61 62 6c 65  o 1 if the table
db510 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2f 0a 53 51   is empty..*/.SQ
db520 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
db530 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 69 72   sqlite3BtreeFir
db540 73 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  st(BtCursor *pCu
db550 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20  r, int *pRes){. 
db560 20 69 6e 74 20 72 63 3b 0a 20 20 72 63 20 3d 20   int rc;.  rc = 
db570 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29  moveToRoot(pCur)
db580 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74  ;.  if( rc ) ret
db590 75 72 6e 20 72 63 3b 0a 20 20 69 66 28 20 70 43  urn rc;.  if( pC
db5a0 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53  ur->eState==CURS
db5b0 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b 0a 20 20  OR_INVALID ){.  
db5c0 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
db5d0 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20  pPage->nCell==0 
db5e0 29 3b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 31  );.    *pRes = 1
db5f0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
db600 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 61 73  ITE_OK;.  }.  as
db610 73 65 72 74 28 20 70 43 75 72 2d 3e 70 50 61 67  sert( pCur->pPag
db620 65 2d 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20 20  e->nCell>0 );.  
db630 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 72 63 20  *pRes = 0;.  rc 
db640 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74  = moveToLeftmost
db650 28 70 43 75 72 29 3b 0a 20 20 72 65 74 75 72 6e  (pCur);.  return
db660 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65 20   rc;.}../* Move 
db670 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68  the cursor to th
db680 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e 20  e last entry in 
db690 74 68 65 20 74 61 62 6c 65 2e 20 20 52 65 74 75  the table.  Retu
db6a0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20  rn SQLITE_OK.** 
db6b0 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 53 65 74  on success.  Set
db6c0 20 2a 70 52 65 73 20 74 6f 20 30 20 69 66 20 74   *pRes to 0 if t
db6d0 68 65 20 63 75 72 73 6f 72 20 61 63 74 75 61 6c  he cursor actual
db6e0 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 73 6f 6d  ly points to som
db6f0 65 74 68 69 6e 67 0a 2a 2a 20 6f 72 20 73 65 74  ething.** or set
db700 20 2a 70 52 65 73 20 74 6f 20 31 20 69 66 20 74   *pRes to 1 if t
db710 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74  he table is empt
db720 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
db730 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
db740 42 74 72 65 65 4c 61 73 74 28 42 74 43 75 72 73  BtreeLast(BtCurs
db750 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70  or *pCur, int *p
db760 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  Res){.  int rc;.
db770 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f    rc = moveToRoo
db780 74 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72  t(pCur);.  if( r
db790 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  c ) return rc;. 
db7a0 20 69 66 28 20 43 55 52 53 4f 52 5f 49 4e 56 41   if( CURSOR_INVA
db7b0 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74  LID==pCur->eStat
db7c0 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  e ){.    assert(
db7d0 20 70 43 75 72 2d 3e 70 50 61 67 65 2d 3e 6e 43   pCur->pPage->nC
db7e0 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 2a 70  ell==0 );.    *p
db7f0 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 72 65 74  Res = 1;.    ret
db800 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
db810 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 43 75   }.  assert( pCu
db820 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  r->eState==CURSO
db830 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 2a 70 52  R_VALID );.  *pR
db840 65 73 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 6d  es = 0;.  rc = m
db850 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 70  oveToRightmost(p
db860 43 75 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  Cur);.  return r
db870 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68  c;.}../* Move th
db880 65 20 63 75 72 73 6f 72 20 73 6f 20 74 68 61 74  e cursor so that
db890 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e   it points to an
db8a0 20 65 6e 74 72 79 20 6e 65 61 72 20 70 4b 65 79   entry near pKey
db8b0 2f 6e 4b 65 79 2e 0a 2a 2a 20 52 65 74 75 72 6e  /nKey..** Return
db8c0 20 61 20 73 75 63 63 65 73 73 20 63 6f 64 65 2e   a success code.
db8d0 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 49 4e 54 4b 45  .**.** For INTKE
db8e0 59 20 74 61 62 6c 65 73 2c 20 6f 6e 6c 79 20 74  Y tables, only t
db8f0 68 65 20 6e 4b 65 79 20 70 61 72 61 6d 65 74 65  he nKey paramete
db900 72 20 69 73 20 75 73 65 64 2e 20 20 70 4b 65 79  r is used.  pKey
db910 20 69 73 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e 20   is.** ignored. 
db920 20 46 6f 72 20 6f 74 68 65 72 20 74 61 62 6c 65   For other table
db930 73 2c 20 6e 4b 65 79 20 69 73 20 74 68 65 20 6e  s, nKey is the n
db940 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
db950 66 20 64 61 74 61 0a 2a 2a 20 69 6e 20 70 4b 65  f data.** in pKe
db960 79 2e 20 20 54 68 65 20 63 6f 6d 70 61 72 69 73  y.  The comparis
db970 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 73 70 65 63  on function spec
db980 69 66 69 65 64 20 77 68 65 6e 20 74 68 65 20 63  ified when the c
db990 75 72 73 6f 72 20 77 61 73 0a 2a 2a 20 63 72 65  ursor was.** cre
db9a0 61 74 65 64 20 69 73 20 75 73 65 64 20 74 6f 20  ated is used to 
db9b0 63 6f 6d 70 61 72 65 20 6b 65 79 73 2e 0a 2a 2a  compare keys..**
db9c0 0a 2a 2a 20 49 66 20 61 6e 20 65 78 61 63 74 20  .** If an exact 
db9d0 6d 61 74 63 68 20 69 73 20 6e 6f 74 20 66 6f 75  match is not fou
db9e0 6e 64 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72  nd, then the cur
db9f0 73 6f 72 20 69 73 20 61 6c 77 61 79 73 0a 2a 2a  sor is always.**
dba00 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61   left pointing a
dba10 74 20 61 20 6c 65 61 66 20 70 61 67 65 20 77 68  t a leaf page wh
dba20 69 63 68 20 77 6f 75 6c 64 20 68 6f 6c 64 20 74  ich would hold t
dba30 68 65 20 65 6e 74 72 79 20 69 66 20 69 74 0a 2a  he entry if it.*
dba40 2a 20 77 65 72 65 20 70 72 65 73 65 6e 74 2e 20  * were present. 
dba50 20 54 68 65 20 63 75 72 73 6f 72 20 6d 69 67 68   The cursor migh
dba60 74 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20 65 6e  t point to an en
dba70 74 72 79 20 74 68 61 74 20 63 6f 6d 65 73 0a 2a  try that comes.*
dba80 2a 20 62 65 66 6f 72 65 20 6f 72 20 61 66 74 65  * before or afte
dba90 72 20 74 68 65 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a  r the key..**.**
dbaa0 20 54 68 65 20 72 65 73 75 6c 74 20 6f 66 20 63   The result of c
dbab0 6f 6d 70 61 72 69 6e 67 20 74 68 65 20 6b 65 79  omparing the key
dbac0 20 77 69 74 68 20 74 68 65 20 65 6e 74 72 79 20   with the entry 
dbad0 74 6f 20 77 68 69 63 68 20 74 68 65 0a 2a 2a 20  to which the.** 
dbae0 63 75 72 73 6f 72 20 69 73 20 77 72 69 74 74 65  cursor is writte
dbaf0 6e 20 74 6f 20 2a 70 52 65 73 20 69 66 20 70 52  n to *pRes if pR
dbb00 65 73 21 3d 4e 55 4c 4c 2e 20 20 54 68 65 20 6d  es!=NULL.  The m
dbb10 65 61 6e 69 6e 67 20 6f 66 0a 2a 2a 20 74 68 69  eaning of.** thi
dbb20 73 20 76 61 6c 75 65 20 69 73 20 61 73 20 66 6f  s value is as fo
dbb30 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  llows:.**.**    
dbb40 20 2a 70 52 65 73 3c 30 20 20 20 20 20 20 54 68   *pRes<0      Th
dbb50 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74  e cursor is left
dbb60 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 6e 20   pointing at an 
dbb70 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a 20 20 20  entry that.**   
dbb80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
dbb90 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 70  s smaller than p
dbba0 4b 65 79 20 6f 72 20 69 66 20 74 68 65 20 74 61  Key or if the ta
dbbb0 62 6c 65 20 69 73 20 65 6d 70 74 79 0a 2a 2a 20  ble is empty.** 
dbbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dbbd0 20 61 6e 64 20 74 68 65 20 63 75 72 73 6f 72 20   and the cursor 
dbbe0 69 73 20 74 68 65 72 65 66 6f 72 65 20 6c 65 66  is therefore lef
dbbf0 74 20 70 6f 69 6e 74 20 74 6f 20 6e 6f 74 68 69  t point to nothi
dbc00 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 70  ng..**.**     *p
dbc10 52 65 73 3d 3d 30 20 20 20 20 20 54 68 65 20 63  Res==0     The c
dbc20 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20 70 6f  ursor is left po
dbc30 69 6e 74 69 6e 67 20 61 74 20 61 6e 20 65 6e 74  inting at an ent
dbc40 72 79 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20  ry that.**      
dbc50 20 20 20 20 20 20 20 20 20 20 20 20 65 78 61 63              exac
dbc60 74 6c 79 20 6d 61 74 63 68 65 73 20 70 4b 65 79  tly matches pKey
dbc70 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 70 52 65  ..**.**     *pRe
dbc80 73 3e 30 20 20 20 20 20 20 54 68 65 20 63 75 72  s>0      The cur
dbc90 73 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e  sor is left poin
dbca0 74 69 6e 67 20 61 74 20 61 6e 20 65 6e 74 72 79  ting at an entry
dbcb0 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20   that.**        
dbcc0 20 20 20 20 20 20 20 20 20 20 69 73 20 6c 61 72            is lar
dbcd0 67 65 72 20 74 68 61 6e 20 70 4b 65 79 2e 0a 2a  ger than pKey..*
dbce0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
dbcf0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
dbd00 65 4d 6f 76 65 74 6f 28 0a 20 20 42 74 43 75 72  eMoveto(.  BtCur
dbd10 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20  sor *pCur,      
dbd20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20    /* The cursor 
dbd30 74 6f 20 62 65 20 6d 6f 76 65 64 20 2a 2f 0a 20  to be moved */. 
dbd40 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65   const void *pKe
dbd50 79 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6b  y,      /* The k
dbd60 65 79 20 63 6f 6e 74 65 6e 74 20 66 6f 72 20 69  ey content for i
dbd70 6e 64 69 63 65 73 2e 20 20 4e 6f 74 20 75 73 65  ndices.  Not use
dbd80 64 20 62 79 20 74 61 62 6c 65 73 20 2a 2f 0a 20  d by tables */. 
dbd90 20 69 36 34 20 6e 4b 65 79 2c 20 20 20 20 20 20   i64 nKey,      
dbda0 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
dbdb0 6f 66 20 70 4b 65 79 2e 20 20 4f 72 20 74 68 65  of pKey.  Or the
dbdc0 20 6b 65 79 20 66 6f 72 20 74 61 62 6c 65 73 20   key for tables 
dbdd0 2a 2f 0a 20 20 69 6e 74 20 62 69 61 73 52 69 67  */.  int biasRig
dbde0 68 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 49  ht,         /* I
dbdf0 66 20 74 72 75 65 2c 20 62 69 61 73 20 74 68 65  f true, bias the
dbe00 20 73 65 61 72 63 68 20 74 6f 20 74 68 65 20 68   search to the h
dbe10 69 67 68 20 65 6e 64 20 2a 2f 0a 20 20 69 6e 74  igh end */.  int
dbe20 20 2a 70 52 65 73 20 20 20 20 20 20 20 20 20 20   *pRes          
dbe30 20 20 20 20 2f 2a 20 53 65 61 72 63 68 20 72 65      /* Search re
dbe40 73 75 6c 74 20 66 6c 61 67 20 2a 2f 0a 29 7b 0a  sult flag */.){.
dbe50 20 20 69 6e 74 20 72 63 3b 0a 20 20 72 63 20 3d    int rc;.  rc =
dbe60 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72   moveToRoot(pCur
dbe70 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65  );.  if( rc ) re
dbe80 74 75 72 6e 20 72 63 3b 0a 20 20 61 73 73 65 72  turn rc;.  asser
dbe90 74 28 20 70 43 75 72 2d 3e 70 50 61 67 65 20 29  t( pCur->pPage )
dbea0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  ;.  assert( pCur
dbeb0 2d 3e 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20  ->pPage->isInit 
dbec0 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65  );.  if( pCur->e
dbed0 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e  State==CURSOR_IN
dbee0 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 2a 70 52  VALID ){.    *pR
dbef0 65 73 20 3d 20 2d 31 3b 0a 20 20 20 20 61 73 73  es = -1;.    ass
dbf00 65 72 74 28 20 70 43 75 72 2d 3e 70 50 61 67 65  ert( pCur->pPage
dbf10 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20  ->nCell==0 );.  
dbf20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
dbf30 4f 4b 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 3b 3b  OK;.  }.  for(;;
dbf40 29 7b 0a 20 20 20 20 69 6e 74 20 6c 77 72 2c 20  ){.    int lwr, 
dbf50 75 70 72 3b 0a 20 20 20 20 50 67 6e 6f 20 63 68  upr;.    Pgno ch
dbf60 6c 64 50 67 3b 0a 20 20 20 20 4d 65 6d 50 61 67  ldPg;.    MemPag
dbf70 65 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d  e *pPage = pCur-
dbf80 3e 70 50 61 67 65 3b 0a 20 20 20 20 69 6e 74 20  >pPage;.    int 
dbf90 63 20 3d 20 2d 31 3b 20 20 2f 2a 20 70 52 65 73  c = -1;  /* pRes
dbfa0 20 72 65 74 75 72 6e 20 69 66 20 74 61 62 6c 65   return if table
dbfb0 20 69 73 20 65 6d 70 74 79 20 6d 75 73 74 20 62   is empty must b
dbfc0 65 20 2d 31 20 2a 2f 0a 20 20 20 20 6c 77 72 20  e -1 */.    lwr 
dbfd0 3d 20 30 3b 0a 20 20 20 20 75 70 72 20 3d 20 70  = 0;.    upr = p
dbfe0 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 31 3b 0a 20  Page->nCell-1;. 
dbff0 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 69     if( !pPage->i
dc000 6e 74 4b 65 79 20 26 26 20 70 4b 65 79 3d 3d 30  ntKey && pKey==0
dc010 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
dc020 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
dc030 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20  BKPT;.    }.    
dc040 69 66 28 20 62 69 61 73 52 69 67 68 74 20 29 7b  if( biasRight ){
dc050 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 69 64 78  .      pCur->idx
dc060 20 3d 20 75 70 72 3b 0a 20 20 20 20 7d 65 6c 73   = upr;.    }els
dc070 65 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 69  e{.      pCur->i
dc080 64 78 20 3d 20 28 75 70 72 2b 6c 77 72 29 2f 32  dx = (upr+lwr)/2
dc090 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
dc0a0 6c 77 72 3c 3d 75 70 72 20 29 20 66 6f 72 28 3b  lwr<=upr ) for(;
dc0b0 3b 29 7b 0a 20 20 20 20 20 20 76 6f 69 64 20 2a  ;){.      void *
dc0c0 70 43 65 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 20  pCellKey;.      
dc0d0 69 36 34 20 6e 43 65 6c 6c 4b 65 79 3b 0a 20 20  i64 nCellKey;.  
dc0e0 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e      pCur->info.n
dc0f0 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 20 20  Size = 0;.      
dc100 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65  if( pPage->intKe
dc110 79 20 29 7b 0a 20 20 20 20 20 20 20 20 75 38 20  y ){.        u8 
dc120 2a 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20 20 20  *pCell;.        
dc130 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c  pCell = findCell
dc140 28 70 50 61 67 65 2c 20 70 43 75 72 2d 3e 69 64  (pPage, pCur->id
dc150 78 29 20 2b 20 70 50 61 67 65 2d 3e 63 68 69 6c  x) + pPage->chil
dc160 64 50 74 72 53 69 7a 65 3b 0a 20 20 20 20 20 20  dPtrSize;.      
dc170 20 20 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73    if( pPage->has
dc180 44 61 74 61 20 29 7b 0a 20 20 20 20 20 20 20 20  Data ){.        
dc190 20 20 75 33 32 20 64 75 6d 6d 79 3b 0a 20 20 20    u32 dummy;.   
dc1a0 20 20 20 20 20 20 20 70 43 65 6c 6c 20 2b 3d 20         pCell += 
dc1b0 67 65 74 56 61 72 69 6e 74 33 32 28 70 43 65 6c  getVarint32(pCel
dc1c0 6c 2c 20 26 64 75 6d 6d 79 29 3b 0a 20 20 20 20  l, &dummy);.    
dc1d0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 67 65      }.        ge
dc1e0 74 56 61 72 69 6e 74 28 70 43 65 6c 6c 2c 20 28  tVarint(pCell, (
dc1f0 75 36 34 20 2a 29 26 6e 43 65 6c 6c 4b 65 79 29  u64 *)&nCellKey)
dc200 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 43  ;.        if( nC
dc210 65 6c 6c 4b 65 79 3c 6e 4b 65 79 20 29 7b 0a 20  ellKey<nKey ){. 
dc220 20 20 20 20 20 20 20 20 20 63 20 3d 20 2d 31 3b           c = -1;
dc230 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69  .        }else i
dc240 66 28 20 6e 43 65 6c 6c 4b 65 79 3e 6e 4b 65 79  f( nCellKey>nKey
dc250 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 20   ){.          c 
dc260 3d 20 2b 31 3b 0a 20 20 20 20 20 20 20 20 7d 65  = +1;.        }e
dc270 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 63  lse{.          c
dc280 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = 0;.        }.
dc290 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
dc2a0 20 20 20 20 20 69 6e 74 20 61 76 61 69 6c 61 62       int availab
dc2b0 6c 65 3b 0a 20 20 20 20 20 20 20 20 70 43 65 6c  le;.        pCel
dc2c0 6c 4b 65 79 20 3d 20 28 76 6f 69 64 20 2a 29 66  lKey = (void *)f
dc2d0 65 74 63 68 50 61 79 6c 6f 61 64 28 70 43 75 72  etchPayload(pCur
dc2e0 2c 20 26 61 76 61 69 6c 61 62 6c 65 2c 20 30 29  , &available, 0)
dc2f0 3b 0a 20 20 20 20 20 20 20 20 6e 43 65 6c 6c 4b  ;.        nCellK
dc300 65 79 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e  ey = pCur->info.
dc310 6e 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 69 66  nKey;.        if
dc320 28 20 61 76 61 69 6c 61 62 6c 65 3e 3d 6e 43 65  ( available>=nCe
dc330 6c 6c 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20  llKey ){.       
dc340 20 20 20 63 20 3d 20 70 43 75 72 2d 3e 78 43 6f     c = pCur->xCo
dc350 6d 70 61 72 65 28 70 43 75 72 2d 3e 70 41 72 67  mpare(pCur->pArg
dc360 2c 20 6e 43 65 6c 6c 4b 65 79 2c 20 70 43 65 6c  , nCellKey, pCel
dc370 6c 4b 65 79 2c 20 6e 4b 65 79 2c 20 70 4b 65 79  lKey, nKey, pKey
dc380 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  );.        }else
dc390 7b 0a 20 20 20 20 20 20 20 20 20 20 70 43 65 6c  {.          pCel
dc3a0 6c 4b 65 79 20 3d 20 73 71 6c 69 74 65 4d 61 6c  lKey = sqliteMal
dc3b0 6c 6f 63 52 61 77 28 20 6e 43 65 6c 6c 4b 65 79  locRaw( nCellKey
dc3c0 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66   );.          if
dc3d0 28 20 70 43 65 6c 6c 4b 65 79 3d 3d 30 20 29 20  ( pCellKey==0 ) 
dc3e0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
dc3f0 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20 20 72  MEM;.          r
dc400 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
dc410 4b 65 79 28 70 43 75 72 2c 20 30 2c 20 6e 43 65  Key(pCur, 0, nCe
dc420 6c 6c 4b 65 79 2c 20 28 76 6f 69 64 20 2a 29 70  llKey, (void *)p
dc430 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 20 20  CellKey);.      
dc440 20 20 20 20 63 20 3d 20 70 43 75 72 2d 3e 78 43      c = pCur->xC
dc450 6f 6d 70 61 72 65 28 70 43 75 72 2d 3e 70 41 72  ompare(pCur->pAr
dc460 67 2c 20 6e 43 65 6c 6c 4b 65 79 2c 20 70 43 65  g, nCellKey, pCe
dc470 6c 6c 4b 65 79 2c 20 6e 4b 65 79 2c 20 70 4b 65  llKey, nKey, pKe
dc480 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  y);.          sq
dc490 6c 69 74 65 46 72 65 65 28 70 43 65 6c 6c 4b 65  liteFree(pCellKe
dc4a0 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  y);.          if
dc4b0 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
dc4c0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
dc4d0 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63 3d    }.      if( c=
dc4e0 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  =0 ){.        if
dc4f0 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 44 61 74  ( pPage->leafDat
dc500 61 20 26 26 20 21 70 50 61 67 65 2d 3e 6c 65 61  a && !pPage->lea
dc510 66 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6c  f ){.          l
dc520 77 72 20 3d 20 70 43 75 72 2d 3e 69 64 78 3b 0a  wr = pCur->idx;.
dc530 20 20 20 20 20 20 20 20 20 20 75 70 72 20 3d 20            upr = 
dc540 6c 77 72 20 2d 20 31 3b 0a 20 20 20 20 20 20 20  lwr - 1;.       
dc550 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
dc560 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
dc570 20 20 20 69 66 28 20 70 52 65 73 20 29 20 2a 70     if( pRes ) *p
dc580 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  Res = 0;.       
dc590 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
dc5a0 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  _OK;.        }. 
dc5b0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
dc5c0 20 63 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20   c<0 ){.        
dc5d0 6c 77 72 20 3d 20 70 43 75 72 2d 3e 69 64 78 2b  lwr = pCur->idx+
dc5e0 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  1;.      }else{.
dc5f0 20 20 20 20 20 20 20 20 75 70 72 20 3d 20 70 43          upr = pC
dc600 75 72 2d 3e 69 64 78 2d 31 3b 0a 20 20 20 20 20  ur->idx-1;.     
dc610 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6c 77 72   }.      if( lwr
dc620 3e 75 70 72 20 29 7b 0a 20 20 20 20 20 20 20 20  >upr ){.        
dc630 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
dc640 20 20 20 20 20 70 43 75 72 2d 3e 69 64 78 20 3d       pCur->idx =
dc650 20 28 6c 77 72 2b 75 70 72 29 2f 32 3b 0a 20 20   (lwr+upr)/2;.  
dc660 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
dc670 6c 77 72 3d 3d 75 70 72 2b 31 20 29 3b 0a 20 20  lwr==upr+1 );.  
dc680 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
dc690 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 69  >isInit );.    i
dc6a0 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  f( pPage->leaf )
dc6b0 7b 0a 20 20 20 20 20 20 63 68 6c 64 50 67 20 3d  {.      chldPg =
dc6c0 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66   0;.    }else if
dc6d0 28 20 6c 77 72 3e 3d 70 50 61 67 65 2d 3e 6e 43  ( lwr>=pPage->nC
dc6e0 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 63 68 6c  ell ){.      chl
dc6f0 64 50 67 20 3d 20 67 65 74 34 62 79 74 65 28 26  dPg = get4byte(&
dc700 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61  pPage->aData[pPa
dc710 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d  ge->hdrOffset+8]
dc720 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
dc730 20 20 20 20 63 68 6c 64 50 67 20 3d 20 67 65 74      chldPg = get
dc740 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70  4byte(findCell(p
dc750 50 61 67 65 2c 20 6c 77 72 29 29 3b 0a 20 20 20  Page, lwr));.   
dc760 20 7d 0a 20 20 20 20 69 66 28 20 63 68 6c 64 50   }.    if( chldP
dc770 67 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73  g==0 ){.      as
dc780 73 65 72 74 28 20 70 43 75 72 2d 3e 69 64 78 3e  sert( pCur->idx>
dc790 3d 30 20 26 26 20 70 43 75 72 2d 3e 69 64 78 3c  =0 && pCur->idx<
dc7a0 70 43 75 72 2d 3e 70 50 61 67 65 2d 3e 6e 43 65  pCur->pPage->nCe
dc7b0 6c 6c 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ll );.      if( 
dc7c0 70 52 65 73 20 29 20 2a 70 52 65 73 20 3d 20 63  pRes ) *pRes = c
dc7d0 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
dc7e0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a  QLITE_OK;.    }.
dc7f0 20 20 20 20 70 43 75 72 2d 3e 69 64 78 20 3d 20      pCur->idx = 
dc800 6c 77 72 3b 0a 20 20 20 20 70 43 75 72 2d 3e 69  lwr;.    pCur->i
dc810 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20  nfo.nSize = 0;. 
dc820 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68     rc = moveToCh
dc830 69 6c 64 28 70 43 75 72 2c 20 63 68 6c 64 50 67  ild(pCur, chldPg
dc840 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b  );.    if( rc ){
dc850 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
dc860 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a  ;.    }.  }.  /*
dc870 20 4e 4f 54 20 52 45 41 43 48 45 44 20 2a 2f 0a   NOT REACHED */.
dc880 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
dc890 54 52 55 45 20 69 66 20 74 68 65 20 63 75 72 73  TRUE if the curs
dc8a0 6f 72 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69  or is not pointi
dc8b0 6e 67 20 61 74 20 61 6e 20 65 6e 74 72 79 20 6f  ng at an entry o
dc8c0 66 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a  f the table..**.
dc8d0 2a 2a 20 54 52 55 45 20 77 69 6c 6c 20 62 65 20  ** TRUE will be 
dc8e0 72 65 74 75 72 6e 65 64 20 61 66 74 65 72 20 61  returned after a
dc8f0 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
dc900 42 74 72 65 65 4e 65 78 74 28 29 20 6d 6f 76 65  BtreeNext() move
dc910 73 0a 2a 2a 20 70 61 73 74 20 74 68 65 20 6c 61  s.** past the la
dc920 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  st entry in the 
dc930 74 61 62 6c 65 20 6f 72 20 73 71 6c 69 74 65 33  table or sqlite3
dc940 42 74 72 65 65 50 72 65 76 28 29 20 6d 6f 76 65  BtreePrev() move
dc950 73 20 70 61 73 74 0a 2a 2a 20 74 68 65 20 66 69  s past.** the fi
dc960 72 73 74 20 65 6e 74 72 79 2e 20 20 54 52 55 45  rst entry.  TRUE
dc970 20 69 73 20 61 6c 73 6f 20 72 65 74 75 72 6e 65   is also returne
dc980 64 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 69  d if the table i
dc990 73 20 65 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49  s empty..*/.SQLI
dc9a0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
dc9b0 71 6c 69 74 65 33 42 74 72 65 65 45 6f 66 28 42  qlite3BtreeEof(B
dc9c0 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a  tCursor *pCur){.
dc9d0 20 20 2f 2a 20 54 4f 44 4f 3a 20 57 68 61 74 20    /* TODO: What 
dc9e0 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  if the cursor is
dc9f0 20 69 6e 20 43 55 52 53 4f 52 5f 52 45 51 55 49   in CURSOR_REQUI
dca00 52 45 53 45 45 4b 20 62 75 74 20 61 6c 6c 20 74  RESEEK but all t
dca10 61 62 6c 65 20 65 6e 74 72 69 65 73 0a 20 20 2a  able entries.  *
dca20 2a 20 68 61 76 65 20 62 65 65 6e 20 64 65 6c 65  * have been dele
dca30 74 65 64 3f 20 54 68 69 73 20 41 50 49 20 77 69  ted? This API wi
dca40 6c 6c 20 6e 65 65 64 20 74 6f 20 63 68 61 6e 67  ll need to chang
dca50 65 20 74 6f 20 72 65 74 75 72 6e 20 61 6e 20 65  e to return an e
dca60 72 72 6f 72 20 63 6f 64 65 0a 20 20 2a 2a 20 61  rror code.  ** a
dca70 73 20 77 65 6c 6c 20 61 73 20 74 68 65 20 62 6f  s well as the bo
dca80 6f 6c 65 61 6e 20 72 65 73 75 6c 74 20 76 61 6c  olean result val
dca90 75 65 2e 0a 20 20 2a 2f 0a 20 20 72 65 74 75 72  ue..  */.  retur
dcaa0 6e 20 28 43 55 52 53 4f 52 5f 56 41 4c 49 44 21  n (CURSOR_VALID!
dcab0 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 29 3b 0a  =pCur->eState);.
dcac0 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 76 61 6e 63 65  }../*.** Advance
dcad0 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74   the cursor to t
dcae0 68 65 20 6e 65 78 74 20 65 6e 74 72 79 20 69 6e  he next entry in
dcaf0 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20   the database.  
dcb00 49 66 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c  If.** successful
dcb10 20 74 68 65 6e 20 73 65 74 20 2a 70 52 65 73 3d   then set *pRes=
dcb20 30 2e 20 20 49 66 20 74 68 65 20 63 75 72 73 6f  0.  If the curso
dcb30 72 0a 2a 2a 20 77 61 73 20 61 6c 72 65 61 64 79  r.** was already
dcb40 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65   pointing to the
dcb50 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e 20 74   last entry in t
dcb60 68 65 20 64 61 74 61 62 61 73 65 20 62 65 66 6f  he database befo
dcb70 72 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69  re.** this routi
dcb80 6e 65 20 77 61 73 20 63 61 6c 6c 65 64 2c 20 74  ne was called, t
dcb90 68 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 31 2e  hen set *pRes=1.
dcba0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
dcbb0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
dcbc0 72 65 65 4e 65 78 74 28 42 74 43 75 72 73 6f 72  reeNext(BtCursor
dcbd0 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 65   *pCur, int *pRe
dcbe0 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  s){.  int rc;.  
dcbf0 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a  MemPage *pPage;.
dcc00 0a 20 20 72 63 20 3d 20 72 65 73 74 6f 72 65 4f  .  rc = restoreO
dcc10 72 43 6c 65 61 72 43 75 72 73 6f 72 50 6f 73 69  rClearCursorPosi
dcc20 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66  tion(pCur);.  if
dcc30 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
dcc40 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
dcc50 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
dcc60 70 52 65 73 21 3d 30 20 29 3b 0a 20 20 70 50 61  pRes!=0 );.  pPa
dcc70 67 65 20 3d 20 70 43 75 72 2d 3e 70 50 61 67 65  ge = pCur->pPage
dcc80 3b 0a 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49  ;.  if( CURSOR_I
dcc90 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53  NVALID==pCur->eS
dcca0 74 61 74 65 20 29 7b 0a 20 20 20 20 2a 70 52 65  tate ){.    *pRe
dccb0 73 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72  s = 1;.    retur
dccc0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
dccd0 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 73 6b 69  .  if( pCur->ski
dcce0 70 3e 30 20 29 7b 0a 20 20 20 20 70 43 75 72 2d  p>0 ){.    pCur-
dccf0 3e 73 6b 69 70 20 3d 20 30 3b 0a 20 20 20 20 2a  >skip = 0;.    *
dcd00 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 65  pRes = 0;.    re
dcd10 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
dcd20 20 20 7d 0a 20 20 70 43 75 72 2d 3e 73 6b 69 70    }.  pCur->skip
dcd30 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28   = 0;..  assert(
dcd40 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29   pPage->isInit )
dcd50 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  ;.  assert( pCur
dcd60 2d 3e 69 64 78 3c 70 50 61 67 65 2d 3e 6e 43 65  ->idx<pPage->nCe
dcd70 6c 6c 20 29 3b 0a 0a 20 20 70 43 75 72 2d 3e 69  ll );..  pCur->i
dcd80 64 78 2b 2b 3b 0a 20 20 70 43 75 72 2d 3e 69 6e  dx++;.  pCur->in
dcd90 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20  fo.nSize = 0;.  
dcda0 69 66 28 20 70 43 75 72 2d 3e 69 64 78 3e 3d 70  if( pCur->idx>=p
dcdb0 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20  Page->nCell ){. 
dcdc0 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c     if( !pPage->l
dcdd0 65 61 66 20 29 7b 0a 20 20 20 20 20 20 72 63 20  eaf ){.      rc 
dcde0 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43  = moveToChild(pC
dcdf0 75 72 2c 20 67 65 74 34 62 79 74 65 28 26 70 50  ur, get4byte(&pP
dce00 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65  age->aData[pPage
dce10 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 29  ->hdrOffset+8]))
dce20 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29  ;.      if( rc )
dce30 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
dce40 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66    rc = moveToLef
dce50 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 20  tmost(pCur);.   
dce60 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20     *pRes = 0;.  
dce70 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
dce80 20 20 20 7d 0a 20 20 20 20 64 6f 7b 0a 20 20 20     }.    do{.   
dce90 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 74     if( sqlite3Bt
dcea0 72 65 65 49 73 52 6f 6f 74 50 61 67 65 28 70 50  reeIsRootPage(pP
dceb0 61 67 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20  age) ){.        
dcec0 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20  *pRes = 1;.     
dced0 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20     pCur->eState 
dcee0 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  = CURSOR_INVALID
dcef0 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
dcf00 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
dcf10 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
dcf20 33 42 74 72 65 65 4d 6f 76 65 54 6f 50 61 72 65  3BtreeMoveToPare
dcf30 6e 74 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20  nt(pCur);.      
dcf40 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 70 50  pPage = pCur->pP
dcf50 61 67 65 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28  age;.    }while(
dcf60 20 70 43 75 72 2d 3e 69 64 78 3e 3d 70 50 61 67   pCur->idx>=pPag
dcf70 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20  e->nCell );.    
dcf80 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 69  *pRes = 0;.    i
dcf90 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 44 61  f( pPage->leafDa
dcfa0 74 61 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  ta ){.      rc =
dcfb0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78   sqlite3BtreeNex
dcfc0 74 28 70 43 75 72 2c 20 70 52 65 73 29 3b 0a 20  t(pCur, pRes);. 
dcfd0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
dcfe0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
dcff0 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
dd000 20 72 63 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 73   rc;.  }.  *pRes
dd010 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 50 61 67   = 0;.  if( pPag
dd020 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 72  e->leaf ){.    r
dd030 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
dd040 0a 20 20 7d 0a 20 20 72 63 20 3d 20 6d 6f 76 65  .  }.  rc = move
dd050 54 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75 72 29  ToLeftmost(pCur)
dd060 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
dd070 0a 0a 2f 2a 0a 2a 2a 20 53 74 65 70 20 74 68 65  ../*.** Step the
dd080 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 62   cursor to the b
dd090 61 63 6b 20 74 6f 20 74 68 65 20 70 72 65 76 69  ack to the previ
dd0a0 6f 75 73 20 65 6e 74 72 79 20 69 6e 20 74 68 65  ous entry in the
dd0b0 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 0a 2a   database.  If.*
dd0c0 2a 20 73 75 63 63 65 73 73 66 75 6c 20 74 68 65  * successful the
dd0d0 6e 20 73 65 74 20 2a 70 52 65 73 3d 30 2e 20 20  n set *pRes=0.  
dd0e0 49 66 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a  If the cursor.**
dd0f0 20 77 61 73 20 61 6c 72 65 61 64 79 20 70 6f 69   was already poi
dd100 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 66 69 72  nting to the fir
dd110 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  st entry in the 
dd120 64 61 74 61 62 61 73 65 20 62 65 66 6f 72 65 0a  database before.
dd130 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ** this routine 
dd140 77 61 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e  was called, then
dd150 20 73 65 74 20 2a 70 52 65 73 3d 31 2e 0a 2a 2f   set *pRes=1..*/
dd160 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
dd170 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
dd180 50 72 65 76 69 6f 75 73 28 42 74 43 75 72 73 6f  Previous(BtCurso
dd190 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 52  r *pCur, int *pR
dd1a0 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  es){.  int rc;. 
dd1b0 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 4d 65   Pgno pgno;.  Me
dd1c0 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 0a 20  mPage *pPage;.. 
dd1d0 20 72 63 20 3d 20 72 65 73 74 6f 72 65 4f 72 43   rc = restoreOrC
dd1e0 6c 65 61 72 43 75 72 73 6f 72 50 6f 73 69 74 69  learCursorPositi
dd1f0 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20  on(pCur);.  if( 
dd200 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
dd210 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
dd220 20 20 7d 0a 20 20 69 66 28 20 43 55 52 53 4f 52    }.  if( CURSOR
dd230 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e  _INVALID==pCur->
dd240 65 53 74 61 74 65 20 29 7b 0a 20 20 20 20 2a 70  eState ){.    *p
dd250 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 72 65 74  Res = 1;.    ret
dd260 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
dd270 20 7d 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 73   }.  if( pCur->s
dd280 6b 69 70 3c 30 20 29 7b 0a 20 20 20 20 70 43 75  kip<0 ){.    pCu
dd290 72 2d 3e 73 6b 69 70 20 3d 20 30 3b 0a 20 20 20  r->skip = 0;.   
dd2a0 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20   *pRes = 0;.    
dd2b0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
dd2c0 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 73 6b  ;.  }.  pCur->sk
dd2d0 69 70 20 3d 20 30 3b 0a 0a 20 20 70 50 61 67 65  ip = 0;..  pPage
dd2e0 20 3d 20 70 43 75 72 2d 3e 70 50 61 67 65 3b 0a   = pCur->pPage;.
dd2f0 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
dd300 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 61 73 73  >isInit );.  ass
dd310 65 72 74 28 20 70 43 75 72 2d 3e 69 64 78 3e 3d  ert( pCur->idx>=
dd320 30 20 29 3b 0a 20 20 69 66 28 20 21 70 50 61 67  0 );.  if( !pPag
dd330 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 70  e->leaf ){.    p
dd340 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 20  gno = get4byte( 
dd350 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20  findCell(pPage, 
dd360 70 43 75 72 2d 3e 69 64 78 29 20 29 3b 0a 20 20  pCur->idx) );.  
dd370 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69    rc = moveToChi
dd380 6c 64 28 70 43 75 72 2c 20 70 67 6e 6f 29 3b 0a  ld(pCur, pgno);.
dd390 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
dd3a0 75 72 6e 20 72 63 3b 0a 20 20 20 20 72 63 20 3d  urn rc;.    rc =
dd3b0 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74   moveToRightmost
dd3c0 28 70 43 75 72 29 3b 0a 20 20 7d 65 6c 73 65 7b  (pCur);.  }else{
dd3d0 0a 20 20 20 20 77 68 69 6c 65 28 20 70 43 75 72  .    while( pCur
dd3e0 2d 3e 69 64 78 3d 3d 30 20 29 7b 0a 20 20 20 20  ->idx==0 ){.    
dd3f0 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 74 72    if( sqlite3Btr
dd400 65 65 49 73 52 6f 6f 74 50 61 67 65 28 70 50 61  eeIsRootPage(pPa
dd410 67 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ge) ){.        p
dd420 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55  Cur->eState = CU
dd430 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20  RSOR_INVALID;.  
dd440 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b        *pRes = 1;
dd450 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
dd460 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20  SQLITE_OK;.     
dd470 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
dd480 42 74 72 65 65 4d 6f 76 65 54 6f 50 61 72 65 6e  BtreeMoveToParen
dd490 74 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20 70  t(pCur);.      p
dd4a0 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 70 50 61  Page = pCur->pPa
dd4b0 67 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43  ge;.    }.    pC
dd4c0 75 72 2d 3e 69 64 78 2d 2d 3b 0a 20 20 20 20 70  ur->idx--;.    p
dd4d0 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20  Cur->info.nSize 
dd4e0 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 70 50 61  = 0;.    if( pPa
dd4f0 67 65 2d 3e 6c 65 61 66 44 61 74 61 20 26 26 20  ge->leafData && 
dd500 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a  !pPage->leaf ){.
dd510 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
dd520 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28  e3BtreePrevious(
dd530 70 43 75 72 2c 20 70 52 65 73 29 3b 0a 20 20 20  pCur, pRes);.   
dd540 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63   }else{.      rc
dd550 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
dd560 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 52 65 73 20    }.  }.  *pRes 
dd570 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  = 0;.  return rc
dd580 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63  ;.}../*.** Alloc
dd590 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 20 66  ate a new page f
dd5a0 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65  rom the database
dd5b0 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   file..**.** The
dd5c0 20 6e 65 77 20 70 61 67 65 20 69 73 20 6d 61 72   new page is mar
dd5d0 6b 65 64 20 61 73 20 64 69 72 74 79 2e 20 20 28  ked as dirty.  (
dd5e0 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
dd5f0 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
dd600 65 28 29 0a 2a 2a 20 68 61 73 20 61 6c 72 65 61  e().** has alrea
dd610 64 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 6f  dy been called o
dd620 6e 20 74 68 65 20 6e 65 77 20 70 61 67 65 2e 29  n the new page.)
dd630 20 20 54 68 65 20 6e 65 77 20 70 61 67 65 20 68    The new page h
dd640 61 73 20 61 6c 73 6f 0a 2a 2a 20 62 65 65 6e 20  as also.** been 
dd650 72 65 66 65 72 65 6e 63 65 64 20 61 6e 64 20 74  referenced and t
dd660 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69  he calling routi
dd670 6e 65 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c  ne is responsibl
dd680 65 20 66 6f 72 20 63 61 6c 6c 69 6e 67 0a 2a 2a  e for calling.**
dd690 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72   sqlite3PagerUnr
dd6a0 65 66 28 29 20 6f 6e 20 74 68 65 20 6e 65 77 20  ef() on the new 
dd6b0 70 61 67 65 20 77 68 65 6e 20 69 74 20 69 73 20  page when it is 
dd6c0 64 6f 6e 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49  done..**.** SQLI
dd6d0 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
dd6e0 64 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 41  d on success.  A
dd6f0 6e 79 20 6f 74 68 65 72 20 72 65 74 75 72 6e 20  ny other return 
dd700 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73 0a  value indicates.
dd710 2a 2a 20 61 6e 20 65 72 72 6f 72 2e 20 20 2a 70  ** an error.  *p
dd720 70 50 61 67 65 20 61 6e 64 20 2a 70 50 67 6e 6f  pPage and *pPgno
dd730 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 20 69   are undefined i
dd740 6e 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61  n the event of a
dd750 6e 20 65 72 72 6f 72 2e 0a 2a 2a 20 44 6f 20 6e  n error..** Do n
dd760 6f 74 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65  ot invoke sqlite
dd770 33 50 61 67 65 72 55 6e 72 65 66 28 29 20 6f 6e  3PagerUnref() on
dd780 20 2a 70 70 50 61 67 65 20 69 66 20 61 6e 20 65   *ppPage if an e
dd790 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64  rror is returned
dd7a0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22  ..**.** If the "
dd7b0 6e 65 61 72 62 79 22 20 70 61 72 61 6d 65 74 65  nearby" paramete
dd7c0 72 20 69 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e  r is not 0, then
dd7d0 20 61 20 28 66 65 65 62 6c 65 29 20 65 66 66 6f   a (feeble) effo
dd7e0 72 74 20 69 73 20 6d 61 64 65 20 74 6f 20 0a 2a  rt is made to .*
dd7f0 2a 20 6c 6f 63 61 74 65 20 61 20 70 61 67 65 20  * locate a page 
dd800 63 6c 6f 73 65 20 74 6f 20 74 68 65 20 70 61 67  close to the pag
dd810 65 20 6e 75 6d 62 65 72 20 22 6e 65 61 72 62 79  e number "nearby
dd820 22 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20  ".  This can be 
dd830 75 73 65 64 20 69 6e 20 61 6e 0a 2a 2a 20 61 74  used in an.** at
dd840 74 65 6d 70 74 20 74 6f 20 6b 65 65 70 20 72 65  tempt to keep re
dd850 6c 61 74 65 64 20 70 61 67 65 73 20 63 6c 6f 73  lated pages clos
dd860 65 20 74 6f 20 65 61 63 68 20 6f 74 68 65 72 20  e to each other 
dd870 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
dd880 66 69 6c 65 2c 0a 2a 2a 20 77 68 69 63 68 20 69  file,.** which i
dd890 6e 20 74 75 72 6e 20 63 61 6e 20 6d 61 6b 65 20  n turn can make 
dd8a0 64 61 74 61 62 61 73 65 20 61 63 63 65 73 73 20  database access 
dd8b0 66 61 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66  faster..**.** If
dd8c0 20 74 68 65 20 22 65 78 61 63 74 22 20 70 61 72   the "exact" par
dd8d0 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 30 2c  ameter is not 0,
dd8e0 20 61 6e 64 20 74 68 65 20 70 61 67 65 2d 6e 75   and the page-nu
dd8f0 6d 62 65 72 20 6e 65 61 72 62 79 20 65 78 69 73  mber nearby exis
dd900 74 73 20 0a 2a 2a 20 61 6e 79 77 68 65 72 65 20  ts .** anywhere 
dd910 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74  on the free-list
dd920 2c 20 74 68 65 6e 20 69 74 20 69 73 20 67 75 61  , then it is gua
dd930 72 65 6e 74 65 65 64 20 74 6f 20 62 65 20 72 65  renteed to be re
dd940 74 75 72 6e 65 64 2e 20 54 68 69 73 0a 2a 2a 20  turned. This.** 
dd950 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 62 79 20  is only used by 
dd960 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61  auto-vacuum data
dd970 62 61 73 65 73 20 77 68 65 6e 20 61 6c 6c 6f 63  bases when alloc
dd980 61 74 69 6e 67 20 61 20 6e 65 77 20 74 61 62 6c  ating a new tabl
dd990 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
dd9a0 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61   allocateBtreePa
dd9b0 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a  ge(.  BtShared *
dd9c0 70 42 74 2c 20 0a 20 20 4d 65 6d 50 61 67 65 20  pBt, .  MemPage 
dd9d0 2a 2a 70 70 50 61 67 65 2c 20 0a 20 20 50 67 6e  **ppPage, .  Pgn
dd9e0 6f 20 2a 70 50 67 6e 6f 2c 20 0a 20 20 50 67 6e  o *pPgno, .  Pgn
dd9f0 6f 20 6e 65 61 72 62 79 2c 0a 20 20 75 38 20 65  o nearby,.  u8 e
dda00 78 61 63 74 0a 29 7b 0a 20 20 4d 65 6d 50 61 67  xact.){.  MemPag
dda10 65 20 2a 70 50 61 67 65 31 3b 0a 20 20 69 6e 74  e *pPage1;.  int
dda20 20 72 63 3b 0a 20 20 69 6e 74 20 6e 3b 20 20 20   rc;.  int n;   
dda30 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70    /* Number of p
dda40 61 67 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65  ages on the free
dda50 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6b 3b  list */.  int k;
dda60 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
dda70 66 20 6c 65 61 76 65 73 20 6f 6e 20 74 68 65 20  f leaves on the 
dda80 74 72 75 6e 6b 20 6f 66 20 74 68 65 20 66 72 65  trunk of the fre
dda90 65 6c 69 73 74 20 2a 2f 0a 20 20 4d 65 6d 50 61  elist */.  MemPa
ddaa0 67 65 20 2a 70 54 72 75 6e 6b 20 3d 20 30 3b 0a  ge *pTrunk = 0;.
ddab0 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 72 65 76    MemPage *pPrev
ddac0 54 72 75 6e 6b 20 3d 20 30 3b 0a 0a 20 20 70 50  Trunk = 0;..  pP
ddad0 61 67 65 31 20 3d 20 70 42 74 2d 3e 70 50 61 67  age1 = pBt->pPag
ddae0 65 31 3b 0a 20 20 6e 20 3d 20 67 65 74 34 62 79  e1;.  n = get4by
ddaf0 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74  te(&pPage1->aDat
ddb00 61 5b 33 36 5d 29 3b 0a 20 20 69 66 28 20 6e 3e  a[36]);.  if( n>
ddb10 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 72  0 ){.    /* Ther
ddb20 65 20 61 72 65 20 70 61 67 65 73 20 6f 6e 20 74  e are pages on t
ddb30 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 52 65  he freelist.  Re
ddb40 75 73 65 20 6f 6e 65 20 6f 66 20 74 68 6f 73 65  use one of those
ddb50 20 70 61 67 65 73 2e 20 2a 2f 0a 20 20 20 20 50   pages. */.    P
ddb60 67 6e 6f 20 69 54 72 75 6e 6b 3b 0a 20 20 20 20  gno iTrunk;.    
ddb70 75 38 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20  u8 searchList = 
ddb80 30 3b 20 2f 2a 20 49 66 20 74 68 65 20 66 72 65  0; /* If the fre
ddb90 65 2d 6c 69 73 74 20 6d 75 73 74 20 62 65 20 73  e-list must be s
ddba0 65 61 72 63 68 65 64 20 66 6f 72 20 27 6e 65 61  earched for 'nea
ddbb0 72 62 79 27 20 2a 2f 0a 20 20 20 20 0a 20 20 20  rby' */.    .   
ddbc0 20 2f 2a 20 49 66 20 74 68 65 20 27 65 78 61 63   /* If the 'exac
ddbd0 74 27 20 70 61 72 61 6d 65 74 65 72 20 77 61 73  t' parameter was
ddbe0 20 74 72 75 65 20 61 6e 64 20 61 20 71 75 65 72   true and a quer
ddbf0 79 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72  y of the pointer
ddc00 2d 6d 61 70 0a 20 20 20 20 2a 2a 20 73 68 6f 77  -map.    ** show
ddc10 73 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20  s that the page 
ddc20 27 6e 65 61 72 62 79 27 20 69 73 20 73 6f 6d 65  'nearby' is some
ddc30 77 68 65 72 65 20 6f 6e 20 74 68 65 20 66 72 65  where on the fre
ddc40 65 2d 6c 69 73 74 2c 20 74 68 65 6e 0a 20 20 20  e-list, then.   
ddc50 20 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 2d 6c   ** the entire-l
ddc60 69 73 74 20 77 69 6c 6c 20 62 65 20 73 65 61 72  ist will be sear
ddc70 63 68 65 64 20 66 6f 72 20 74 68 61 74 20 70 61  ched for that pa
ddc80 67 65 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64  ge..    */.#ifnd
ddc90 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
ddca0 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66  UTOVACUUM.    if
ddcb0 28 20 65 78 61 63 74 20 26 26 20 6e 65 61 72 62  ( exact && nearb
ddcc0 79 3c 3d 73 71 6c 69 74 65 33 50 61 67 65 72 50  y<=sqlite3PagerP
ddcd0 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50  agecount(pBt->pP
ddce0 61 67 65 72 29 20 29 7b 0a 20 20 20 20 20 20 75  ager) ){.      u
ddcf0 38 20 65 54 79 70 65 3b 0a 20 20 20 20 20 20 61  8 eType;.      a
ddd00 73 73 65 72 74 28 20 6e 65 61 72 62 79 3e 30 20  ssert( nearby>0 
ddd10 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
ddd20 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d   pBt->autoVacuum
ddd30 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 70   );.      rc = p
ddd40 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 6e 65  trmapGet(pBt, ne
ddd50 61 72 62 79 2c 20 26 65 54 79 70 65 2c 20 30 29  arby, &eType, 0)
ddd60 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29  ;.      if( rc )
ddd70 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
ddd80 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52    if( eType==PTR
ddd90 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29 7b 0a  MAP_FREEPAGE ){.
ddda0 20 20 20 20 20 20 20 20 73 65 61 72 63 68 4c 69          searchLi
dddb0 73 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a  st = 1;.      }.
dddc0 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 6e        *pPgno = n
dddd0 65 61 72 62 79 3b 0a 20 20 20 20 7d 0a 23 65 6e  earby;.    }.#en
ddde0 64 69 66 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72  dif..    /* Decr
dddf0 65 6d 65 6e 74 20 74 68 65 20 66 72 65 65 2d 6c  ement the free-l
dde00 69 73 74 20 63 6f 75 6e 74 20 62 79 20 31 2e 20  ist count by 1. 
dde10 53 65 74 20 69 54 72 75 6e 6b 20 74 6f 20 74 68  Set iTrunk to th
dde20 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 0a 20  e index of the. 
dde30 20 20 20 2a 2a 20 66 69 72 73 74 20 66 72 65 65     ** first free
dde40 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65  -list trunk page
dde50 2e 20 69 50 72 65 76 54 72 75 6e 6b 20 69 73 20  . iPrevTrunk is 
dde60 69 6e 69 74 69 61 6c 6c 79 20 31 2e 0a 20 20 20  initially 1..   
dde70 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   */.    rc = sql
dde80 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
dde90 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b  Page1->pDbPage);
ddea0 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65  .    if( rc ) re
ddeb0 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 70 75 74  turn rc;.    put
ddec0 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61  4byte(&pPage1->a
dded0 44 61 74 61 5b 33 36 5d 2c 20 6e 2d 31 29 3b 0a  Data[36], n-1);.
ddee0 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 64 65  .    /* The code
ddef0 20 77 69 74 68 69 6e 20 74 68 69 73 20 6c 6f 6f   within this loo
ddf00 70 20 69 73 20 72 75 6e 20 6f 6e 6c 79 20 6f 6e  p is run only on
ddf10 63 65 20 69 66 20 74 68 65 20 27 73 65 61 72 63  ce if the 'searc
ddf20 68 4c 69 73 74 27 20 76 61 72 69 61 62 6c 65 0a  hList' variable.
ddf30 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20 74 72      ** is not tr
ddf40 75 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69  ue. Otherwise, i
ddf50 74 20 72 75 6e 73 20 6f 6e 63 65 20 66 6f 72 20  t runs once for 
ddf60 65 61 63 68 20 74 72 75 6e 6b 2d 70 61 67 65 20  each trunk-page 
ddf70 6f 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 72  on the.    ** fr
ddf80 65 65 2d 6c 69 73 74 20 75 6e 74 69 6c 20 74 68  ee-list until th
ddf90 65 20 70 61 67 65 20 27 6e 65 61 72 62 79 27 20  e page 'nearby' 
ddfa0 69 73 20 6c 6f 63 61 74 65 64 2e 0a 20 20 20 20  is located..    
ddfb0 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20  */.    do {.    
ddfc0 20 20 70 50 72 65 76 54 72 75 6e 6b 20 3d 20 70    pPrevTrunk = p
ddfd0 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 69 66 28  Trunk;.      if(
ddfe0 20 70 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20   pPrevTrunk ){. 
ddff0 20 20 20 20 20 20 20 69 54 72 75 6e 6b 20 3d 20         iTrunk = 
de000 67 65 74 34 62 79 74 65 28 26 70 50 72 65 76 54  get4byte(&pPrevT
de010 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 29 3b  runk->aData[0]);
de020 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
de030 20 20 20 20 20 20 69 54 72 75 6e 6b 20 3d 20 67        iTrunk = g
de040 65 74 34 62 79 74 65 28 26 70 50 61 67 65 31 2d  et4byte(&pPage1-
de050 3e 61 44 61 74 61 5b 33 32 5d 29 3b 0a 20 20 20  >aData[32]);.   
de060 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20     }.      rc = 
de070 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50  sqlite3BtreeGetP
de080 61 67 65 28 70 42 74 2c 20 69 54 72 75 6e 6b 2c  age(pBt, iTrunk,
de090 20 26 70 54 72 75 6e 6b 2c 20 30 29 3b 0a 20 20   &pTrunk, 0);.  
de0a0 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20      if( rc ){.  
de0b0 20 20 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 30        pTrunk = 0
de0c0 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65  ;.        goto e
de0d0 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65  nd_allocate_page
de0e0 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
de0f0 20 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26 70   k = get4byte(&p
de100 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 29  Trunk->aData[4])
de110 3b 0a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 30  ;.      if( k==0
de120 20 26 26 20 21 73 65 61 72 63 68 4c 69 73 74 20   && !searchList 
de130 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ){.        /* Th
de140 65 20 74 72 75 6e 6b 20 68 61 73 20 6e 6f 20 6c  e trunk has no l
de150 65 61 76 65 73 20 61 6e 64 20 74 68 65 20 6c 69  eaves and the li
de160 73 74 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20  st is not being 
de170 73 65 61 72 63 68 65 64 2e 20 0a 20 20 20 20 20  searched. .     
de180 20 20 20 2a 2a 20 53 6f 20 65 78 74 72 61 63 74     ** So extract
de190 20 74 68 65 20 74 72 75 6e 6b 20 70 61 67 65 20   the trunk page 
de1a0 69 74 73 65 6c 66 20 61 6e 64 20 75 73 65 20 69  itself and use i
de1b0 74 20 61 73 20 74 68 65 20 6e 65 77 6c 79 20 0a  t as the newly .
de1c0 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 6c 6f 63          ** alloc
de1d0 61 74 65 64 20 70 61 67 65 20 2a 2f 0a 20 20 20  ated page */.   
de1e0 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 72       assert( pPr
de1f0 65 76 54 72 75 6e 6b 3d 3d 30 20 29 3b 0a 20 20  evTrunk==0 );.  
de200 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
de210 65 33 50 61 67 65 72 57 72 69 74 65 28 70 54 72  e3PagerWrite(pTr
de220 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  unk->pDbPage);. 
de230 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b         if( rc ){
de240 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
de250 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67  end_allocate_pag
de260 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  e;.        }.   
de270 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 69 54       *pPgno = iT
de280 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 6d 65  runk;.        me
de290 6d 63 70 79 28 26 70 50 61 67 65 31 2d 3e 61 44  mcpy(&pPage1->aD
de2a0 61 74 61 5b 33 32 5d 2c 20 26 70 54 72 75 6e 6b  ata[32], &pTrunk
de2b0 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a  ->aData[0], 4);.
de2c0 20 20 20 20 20 20 20 20 2a 70 70 50 61 67 65 20          *ppPage 
de2d0 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20  = pTrunk;.      
de2e0 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20    pTrunk = 0;.  
de2f0 20 20 20 20 20 20 54 52 41 43 45 28 28 22 41 4c        TRACE(("AL
de300 4c 4f 43 41 54 45 3a 20 25 64 20 74 72 75 6e 6b  LOCATE: %d trunk
de310 20 2d 20 25 64 20 66 72 65 65 20 70 61 67 65 73   - %d free pages
de320 20 6c 65 66 74 5c 6e 22 2c 20 2a 70 50 67 6e 6f   left\n", *pPgno
de330 2c 20 6e 2d 31 29 29 3b 0a 20 20 20 20 20 20 7d  , n-1));.      }
de340 65 6c 73 65 20 69 66 28 20 6b 3e 70 42 74 2d 3e  else if( k>pBt->
de350 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38  usableSize/4 - 8
de360 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 56   ){.        /* V
de370 61 6c 75 65 20 6f 66 20 6b 20 69 73 20 6f 75 74  alue of k is out
de380 20 6f 66 20 72 61 6e 67 65 2e 20 20 44 61 74 61   of range.  Data
de390 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20  base corruption 
de3a0 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  */.        rc = 
de3b0 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
de3c0 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 67 6f 74  KPT;.        got
de3d0 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70  o end_allocate_p
de3e0 61 67 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  age;.#ifndef SQL
de3f0 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
de400 55 55 4d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  UUM.      }else 
de410 69 66 28 20 73 65 61 72 63 68 4c 69 73 74 20 26  if( searchList &
de420 26 20 6e 65 61 72 62 79 3d 3d 69 54 72 75 6e 6b  & nearby==iTrunk
de430 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54   ){.        /* T
de440 68 65 20 6c 69 73 74 20 69 73 20 62 65 69 6e 67  he list is being
de450 20 73 65 61 72 63 68 65 64 20 61 6e 64 20 74 68   searched and th
de460 69 73 20 74 72 75 6e 6b 20 70 61 67 65 20 69 73  is trunk page is
de470 20 74 68 65 20 70 61 67 65 0a 20 20 20 20 20 20   the page.      
de480 20 20 2a 2a 20 74 6f 20 61 6c 6c 6f 63 61 74 65    ** to allocate
de490 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20  , regardless of 
de4a0 77 68 65 74 68 65 72 20 69 74 20 68 61 73 20 6c  whether it has l
de4b0 65 61 76 65 73 2e 0a 20 20 20 20 20 20 20 20 2a  eaves..        *
de4c0 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  /.        assert
de4d0 28 20 2a 70 50 67 6e 6f 3d 3d 69 54 72 75 6e 6b  ( *pPgno==iTrunk
de4e0 20 29 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 50   );.        *ppP
de4f0 61 67 65 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 20  age = pTrunk;.  
de500 20 20 20 20 20 20 73 65 61 72 63 68 4c 69 73 74        searchList
de510 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 72 63   = 0;.        rc
de520 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57   = sqlite3PagerW
de530 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62  rite(pTrunk->pDb
de540 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69  Page);.        i
de550 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20  f( rc ){.       
de560 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f     goto end_allo
de570 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20  cate_page;.     
de580 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
de590 20 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20   k==0 ){.       
de5a0 20 20 20 69 66 28 20 21 70 50 72 65 76 54 72 75     if( !pPrevTru
de5b0 6e 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  nk ){.          
de5c0 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 31    memcpy(&pPage1
de5d0 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 26 70 54  ->aData[32], &pT
de5e0 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20  runk->aData[0], 
de5f0 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65  4);.          }e
de600 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
de610 20 6d 65 6d 63 70 79 28 26 70 50 72 65 76 54 72   memcpy(&pPrevTr
de620 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 26  unk->aData[0], &
de630 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d  pTrunk->aData[0]
de640 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 4);.          
de650 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  }.        }else{
de660 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  .          /* Th
de670 65 20 74 72 75 6e 6b 20 70 61 67 65 20 69 73 20  e trunk page is 
de680 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65 20  required by the 
de690 63 61 6c 6c 65 72 20 62 75 74 20 69 74 20 63 6f  caller but it co
de6a0 6e 74 61 69 6e 73 20 0a 20 20 20 20 20 20 20 20  ntains .        
de6b0 20 20 2a 2a 20 70 6f 69 6e 74 65 72 73 20 74 6f    ** pointers to
de6c0 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 76 65   free-list leave
de6d0 73 2e 20 54 68 65 20 66 69 72 73 74 20 6c 65 61  s. The first lea
de6e0 66 20 62 65 63 6f 6d 65 73 20 61 20 74 72 75 6e  f becomes a trun
de6f0 6b 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70  k.          ** p
de700 61 67 65 20 69 6e 20 74 68 69 73 20 63 61 73 65  age in this case
de710 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  ..          */. 
de720 20 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65           MemPage
de730 20 2a 70 4e 65 77 54 72 75 6e 6b 3b 0a 20 20 20   *pNewTrunk;.   
de740 20 20 20 20 20 20 20 50 67 6e 6f 20 69 4e 65 77         Pgno iNew
de750 54 72 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 65  Trunk = get4byte
de760 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b  (&pTrunk->aData[
de770 38 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72  8]);.          r
de780 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
de790 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 4e 65  GetPage(pBt, iNe
de7a0 77 54 72 75 6e 6b 2c 20 26 70 4e 65 77 54 72 75  wTrunk, &pNewTru
de7b0 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  nk, 0);.        
de7c0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
de7d0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
de7e0 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f     goto end_allo
de7f0 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20  cate_page;.     
de800 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
de810 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
de820 65 72 57 72 69 74 65 28 70 4e 65 77 54 72 75 6e  erWrite(pNewTrun
de830 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  k->pDbPage);.   
de840 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
de850 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
de860 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50          releaseP
de870 61 67 65 28 70 4e 65 77 54 72 75 6e 6b 29 3b 0a  age(pNewTrunk);.
de880 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f              goto
de890 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61   end_allocate_pa
de8a0 67 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ge;.          }.
de8b0 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79            memcpy
de8c0 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 61  (&pNewTrunk->aDa
de8d0 74 61 5b 30 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e  ta[0], &pTrunk->
de8e0 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20  aData[0], 4);.  
de8f0 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65          put4byte
de900 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 61  (&pNewTrunk->aDa
de910 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b 0a 20 20 20  ta[4], k-1);.   
de920 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70         memcpy(&p
de930 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b  NewTrunk->aData[
de940 38 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61  8], &pTrunk->aDa
de950 74 61 5b 31 32 5d 2c 20 28 6b 2d 31 29 2a 34 29  ta[12], (k-1)*4)
de960 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 6c 65  ;.          rele
de970 61 73 65 50 61 67 65 28 70 4e 65 77 54 72 75 6e  asePage(pNewTrun
de980 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  k);.          if
de990 28 20 21 70 50 72 65 76 54 72 75 6e 6b 20 29 7b  ( !pPrevTrunk ){
de9a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 75 74  .            put
de9b0 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61  4byte(&pPage1->a
de9c0 44 61 74 61 5b 33 32 5d 2c 20 69 4e 65 77 54 72  Data[32], iNewTr
de9d0 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20  unk);.          
de9e0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
de9f0 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
dea00 61 67 65 72 57 72 69 74 65 28 70 50 72 65 76 54  agerWrite(pPrevT
dea10 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a  runk->pDbPage);.
dea20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
dea30 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  rc ){.          
dea40 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c      goto end_all
dea50 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20  ocate_page;.    
dea60 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
dea70 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26        put4byte(&
dea80 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61 44 61 74  pPrevTrunk->aDat
dea90 61 5b 30 5d 2c 20 69 4e 65 77 54 72 75 6e 6b 29  a[0], iNewTrunk)
deaa0 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
deab0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
deac0 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20  pTrunk = 0;.    
dead0 20 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f      TRACE(("ALLO
deae0 43 41 54 45 3a 20 25 64 20 74 72 75 6e 6b 20 2d  CATE: %d trunk -
deaf0 20 25 64 20 66 72 65 65 20 70 61 67 65 73 20 6c   %d free pages l
deb00 65 66 74 5c 6e 22 2c 20 2a 70 50 67 6e 6f 2c 20  eft\n", *pPgno, 
deb10 6e 2d 31 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20  n-1));.#endif.  
deb20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
deb30 20 20 20 2f 2a 20 45 78 74 72 61 63 74 20 61 20     /* Extract a 
deb40 6c 65 61 66 20 66 72 6f 6d 20 74 68 65 20 74 72  leaf from the tr
deb50 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 69  unk */.        i
deb60 6e 74 20 63 6c 6f 73 65 73 74 3b 0a 20 20 20 20  nt closest;.    
deb70 20 20 20 20 50 67 6e 6f 20 69 50 61 67 65 3b 0a      Pgno iPage;.
deb80 20 20 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64          unsigned
deb90 20 63 68 61 72 20 2a 61 44 61 74 61 20 3d 20 70   char *aData = p
deba0 54 72 75 6e 6b 2d 3e 61 44 61 74 61 3b 0a 20 20  Trunk->aData;.  
debb0 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
debc0 65 33 50 61 67 65 72 57 72 69 74 65 28 70 54 72  e3PagerWrite(pTr
debd0 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  unk->pDbPage);. 
debe0 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b         if( rc ){
debf0 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
dec00 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67  end_allocate_pag
dec10 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  e;.        }.   
dec20 20 20 20 20 20 69 66 28 20 6e 65 61 72 62 79 3e       if( nearby>
dec30 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  0 ){.          i
dec40 6e 74 20 69 2c 20 64 69 73 74 3b 0a 20 20 20 20  nt i, dist;.    
dec50 20 20 20 20 20 20 63 6c 6f 73 65 73 74 20 3d 20        closest = 
dec60 30 3b 0a 20 20 20 20 20 20 20 20 20 20 64 69 73  0;.          dis
dec70 74 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 44  t = get4byte(&aD
dec80 61 74 61 5b 38 5d 29 20 2d 20 6e 65 61 72 62 79  ata[8]) - nearby
dec90 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
deca0 64 69 73 74 3c 30 20 29 20 64 69 73 74 20 3d 20  dist<0 ) dist = 
decb0 2d 64 69 73 74 3b 0a 20 20 20 20 20 20 20 20 20  -dist;.         
decc0 20 66 6f 72 28 69 3d 31 3b 20 69 3c 6b 3b 20 69   for(i=1; i<k; i
decd0 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ++){.           
dece0 20 69 6e 74 20 64 32 20 3d 20 67 65 74 34 62 79   int d2 = get4by
decf0 74 65 28 26 61 44 61 74 61 5b 38 2b 69 2a 34 5d  te(&aData[8+i*4]
ded00 29 20 2d 20 6e 65 61 72 62 79 3b 0a 20 20 20 20  ) - nearby;.    
ded10 20 20 20 20 20 20 20 20 69 66 28 20 64 32 3c 30          if( d2<0
ded20 20 29 20 64 32 20 3d 20 2d 64 32 3b 0a 20 20 20   ) d2 = -d2;.   
ded30 20 20 20 20 20 20 20 20 20 69 66 28 20 64 32 3c           if( d2<
ded40 64 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20  dist ){.        
ded50 20 20 20 20 20 20 63 6c 6f 73 65 73 74 20 3d 20        closest = 
ded60 69 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  i;.             
ded70 20 64 69 73 74 20 3d 20 64 32 3b 0a 20 20 20 20   dist = d2;.    
ded80 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
ded90 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65      }.        }e
deda0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 63  lse{.          c
dedb0 6c 6f 73 65 73 74 20 3d 20 30 3b 0a 20 20 20 20  losest = 0;.    
dedc0 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 69      }..        i
dedd0 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28  Page = get4byte(
dede0 26 61 44 61 74 61 5b 38 2b 63 6c 6f 73 65 73 74  &aData[8+closest
dedf0 2a 34 5d 29 3b 0a 20 20 20 20 20 20 20 20 69 66  *4]);.        if
dee00 28 20 21 73 65 61 72 63 68 4c 69 73 74 20 7c 7c  ( !searchList ||
dee10 20 69 50 61 67 65 3d 3d 6e 65 61 72 62 79 20 29   iPage==nearby )
dee20 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 70 50 67  {.          *pPg
dee30 6e 6f 20 3d 20 69 50 61 67 65 3b 0a 20 20 20 20  no = iPage;.    
dee40 20 20 20 20 20 20 69 66 28 20 2a 70 50 67 6e 6f        if( *pPgno
dee50 3e 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67  >sqlite3PagerPag
dee60 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67  ecount(pBt->pPag
dee70 65 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  er) ){.         
dee80 20 20 20 2f 2a 20 46 72 65 65 20 70 61 67 65 20     /* Free page 
dee90 6f 66 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74  off the end of t
deea0 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 20  he file */.     
deeb0 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51         return SQ
deec0 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
deed0 54 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  T;.          }. 
deee0 20 20 20 20 20 20 20 20 20 54 52 41 43 45 28 28           TRACE((
deef0 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 77 61  "ALLOCATE: %d wa
def00 73 20 6c 65 61 66 20 25 64 20 6f 66 20 25 64 20  s leaf %d of %d 
def10 6f 6e 20 74 72 75 6e 6b 20 25 64 22 0a 20 20 20  on trunk %d".   
def20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 3a                ":
def30 20 25 64 20 6d 6f 72 65 20 66 72 65 65 20 70 61   %d more free pa
def40 67 65 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20  ges\n",.        
def50 20 20 20 20 20 20 20 20 20 2a 70 50 67 6e 6f 2c           *pPgno,
def60 20 63 6c 6f 73 65 73 74 2b 31 2c 20 6b 2c 20 70   closest+1, k, p
def70 54 72 75 6e 6b 2d 3e 70 67 6e 6f 2c 20 6e 2d 31  Trunk->pgno, n-1
def80 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  ));.          if
def90 28 20 63 6c 6f 73 65 73 74 3c 6b 2d 31 20 29 7b  ( closest<k-1 ){
defa0 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d  .            mem
defb0 63 70 79 28 26 61 44 61 74 61 5b 38 2b 63 6c 6f  cpy(&aData[8+clo
defc0 73 65 73 74 2a 34 5d 2c 20 26 61 44 61 74 61 5b  sest*4], &aData[
defd0 34 2b 6b 2a 34 5d 2c 20 34 29 3b 0a 20 20 20 20  4+k*4], 4);.    
defe0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
deff0 20 20 70 75 74 34 62 79 74 65 28 26 61 44 61 74    put4byte(&aDat
df000 61 5b 34 5d 2c 20 6b 2d 31 29 3b 0a 20 20 20 20  a[4], k-1);.    
df010 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
df020 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70  e3BtreeGetPage(p
df030 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 70 70 50 61  Bt, *pPgno, ppPa
df040 67 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20  ge, 1);.        
df050 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
df060 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
df070 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 44     sqlite3PagerD
df080 6f 6e 74 52 6f 6c 6c 62 61 63 6b 28 28 2a 70 70  ontRollback((*pp
df090 50 61 67 65 29 2d 3e 70 44 62 50 61 67 65 29 3b  Page)->pDbPage);
df0a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20  .            rc 
df0b0 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
df0c0 69 74 65 28 28 2a 70 70 50 61 67 65 29 2d 3e 70  ite((*ppPage)->p
df0d0 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  DbPage);.       
df0e0 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
df0f0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
df100 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50          releaseP
df110 61 67 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20  age(*ppPage);.  
df120 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
df130 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
df140 20 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30    searchList = 0
df150 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
df160 20 20 7d 0a 20 20 20 20 20 20 72 65 6c 65 61 73    }.      releas
df170 65 50 61 67 65 28 70 50 72 65 76 54 72 75 6e 6b  ePage(pPrevTrunk
df180 29 3b 0a 20 20 20 20 20 20 70 50 72 65 76 54 72  );.      pPrevTr
df190 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 7d 77 68  unk = 0;.    }wh
df1a0 69 6c 65 28 20 73 65 61 72 63 68 4c 69 73 74 20  ile( searchList 
df1b0 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
df1c0 2f 2a 20 54 68 65 72 65 20 61 72 65 20 6e 6f 20  /* There are no 
df1d0 70 61 67 65 73 20 6f 6e 20 74 68 65 20 66 72 65  pages on the fre
df1e0 65 6c 69 73 74 2c 20 73 6f 20 63 72 65 61 74 65  elist, so create
df1f0 20 61 20 6e 65 77 20 70 61 67 65 20 61 74 20 74   a new page at t
df200 68 65 0a 20 20 20 20 2a 2a 20 65 6e 64 20 6f 66  he.    ** end of
df210 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20   the file */.   
df220 20 2a 70 50 67 6e 6f 20 3d 20 73 71 6c 69 74 65   *pPgno = sqlite
df230 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28  3PagerPagecount(
df240 70 42 74 2d 3e 70 50 61 67 65 72 29 20 2b 20 31  pBt->pPager) + 1
df250 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
df260 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
df270 4d 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 6e  M.    if( pBt->n
df280 54 72 75 6e 63 20 29 7b 0a 20 20 20 20 20 20 2f  Trunc ){.      /
df290 2a 20 41 6e 20 69 6e 63 72 2d 76 61 63 75 75 6d  * An incr-vacuum
df2a0 20 68 61 73 20 61 6c 72 65 61 64 79 20 72 75 6e   has already run
df2b0 20 77 69 74 68 69 6e 20 74 68 69 73 20 74 72 61   within this tra
df2c0 6e 73 61 63 74 69 6f 6e 2e 20 53 6f 20 74 68 65  nsaction. So the
df2d0 0a 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 74  .      ** page t
df2e0 6f 20 61 6c 6c 6f 63 61 74 65 20 69 73 20 6e 6f  o allocate is no
df2f0 74 20 66 72 6f 6d 20 74 68 65 20 70 68 79 73 69  t from the physi
df300 63 61 6c 20 65 6e 64 20 6f 66 20 74 68 65 20 66  cal end of the f
df310 69 6c 65 2c 20 62 75 74 0a 20 20 20 20 20 20 2a  ile, but.      *
df320 2a 20 61 74 20 70 42 74 2d 3e 6e 54 72 75 6e 63  * at pBt->nTrunc
df330 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  . .      */.    
df340 20 20 2a 70 50 67 6e 6f 20 3d 20 70 42 74 2d 3e    *pPgno = pBt->
df350 6e 54 72 75 6e 63 2b 31 3b 0a 20 20 20 20 20 20  nTrunc+1;.      
df360 69 66 28 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44  if( *pPgno==PEND
df370 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
df380 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 28 2a  t) ){.        (*
df390 70 50 67 6e 6f 29 2b 2b 3b 0a 20 20 20 20 20 20  pPgno)++;.      
df3a0 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
df3b0 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20  pBt->autoVacuum 
df3c0 26 26 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45  && PTRMAP_ISPAGE
df3d0 28 70 42 74 2c 20 2a 70 50 67 6e 6f 29 20 29 7b  (pBt, *pPgno) ){
df3e0 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 2a 70 50  .      /* If *pP
df3f0 67 6e 6f 20 72 65 66 65 72 73 20 74 6f 20 61 20  gno refers to a 
df400 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65  pointer-map page
df410 2c 20 61 6c 6c 6f 63 61 74 65 20 74 77 6f 20 6e  , allocate two n
df420 65 77 20 70 61 67 65 73 0a 20 20 20 20 20 20 2a  ew pages.      *
df430 2a 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20  * at the end of 
df440 74 68 65 20 66 69 6c 65 20 69 6e 73 74 65 61 64  the file instead
df450 20 6f 66 20 6f 6e 65 2e 20 54 68 65 20 66 69 72   of one. The fir
df460 73 74 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67  st allocated pag
df470 65 0a 20 20 20 20 20 20 2a 2a 20 62 65 63 6f 6d  e.      ** becom
df480 65 73 20 61 20 6e 65 77 20 70 6f 69 6e 74 65 72  es a new pointer
df490 2d 6d 61 70 20 70 61 67 65 2c 20 74 68 65 20 73  -map page, the s
df4a0 65 63 6f 6e 64 20 69 73 20 75 73 65 64 20 62 79  econd is used by
df4b0 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 20 20 20   the caller..   
df4c0 20 20 20 2a 2f 0a 20 20 20 20 20 20 54 52 41 43     */.      TRAC
df4d0 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64  E(("ALLOCATE: %d
df4e0 20 66 72 6f 6d 20 65 6e 64 20 6f 66 20 66 69 6c   from end of fil
df4f0 65 20 28 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70  e (pointer-map p
df500 61 67 65 29 5c 6e 22 2c 20 2a 70 50 67 6e 6f 29  age)\n", *pPgno)
df510 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
df520 20 2a 70 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47   *pPgno!=PENDING
df530 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20  _BYTE_PAGE(pBt) 
df540 29 3b 0a 20 20 20 20 20 20 28 2a 70 50 67 6e 6f  );.      (*pPgno
df550 29 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  )++;.    }.    i
df560 66 28 20 70 42 74 2d 3e 6e 54 72 75 6e 63 20 29  f( pBt->nTrunc )
df570 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 54 72  {.      pBt->nTr
df580 75 6e 63 20 3d 20 2a 70 50 67 6e 6f 3b 0a 20 20  unc = *pPgno;.  
df590 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20    }.#endif..    
df5a0 61 73 73 65 72 74 28 20 2a 70 50 67 6e 6f 21 3d  assert( *pPgno!=
df5b0 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47  PENDING_BYTE_PAG
df5c0 45 28 70 42 74 29 20 29 3b 0a 20 20 20 20 72 63  E(pBt) );.    rc
df5d0 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47   = sqlite3BtreeG
df5e0 65 74 50 61 67 65 28 70 42 74 2c 20 2a 70 50 67  etPage(pBt, *pPg
df5f0 6e 6f 2c 20 70 70 50 61 67 65 2c 20 30 29 3b 0a  no, ppPage, 0);.
df600 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
df610 75 72 6e 20 72 63 3b 0a 20 20 20 20 72 63 20 3d  urn rc;.    rc =
df620 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
df630 74 65 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44  te((*ppPage)->pD
df640 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20  bPage);.    if( 
df650 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
df660 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61  .      releasePa
df670 67 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20  ge(*ppPage);.   
df680 20 7d 0a 20 20 20 20 54 52 41 43 45 28 28 22 41   }.    TRACE(("A
df690 4c 4c 4f 43 41 54 45 3a 20 25 64 20 66 72 6f 6d  LLOCATE: %d from
df6a0 20 65 6e 64 20 6f 66 20 66 69 6c 65 5c 6e 22 2c   end of file\n",
df6b0 20 2a 70 50 67 6e 6f 29 29 3b 0a 20 20 7d 0a 0a   *pPgno));.  }..
df6c0 20 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e 6f    assert( *pPgno
df6d0 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  !=PENDING_BYTE_P
df6e0 41 47 45 28 70 42 74 29 20 29 3b 0a 0a 65 6e 64  AGE(pBt) );..end
df6f0 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3a 0a  _allocate_page:.
df700 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54    releasePage(pT
df710 72 75 6e 6b 29 3b 0a 20 20 72 65 6c 65 61 73 65  runk);.  release
df720 50 61 67 65 28 70 50 72 65 76 54 72 75 6e 6b 29  Page(pPrevTrunk)
df730 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
df740 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 70 61  ../*.** Add a pa
df750 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ge of the databa
df760 73 65 20 66 69 6c 65 20 74 6f 20 74 68 65 20 66  se file to the f
df770 72 65 65 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 73  reelist..**.** s
df780 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
df790 28 29 20 69 73 20 4e 4f 54 20 63 61 6c 6c 65 64  () is NOT called
df7a0 20 66 6f 72 20 70 50 61 67 65 2e 0a 2a 2f 0a 73   for pPage..*/.s
df7b0 74 61 74 69 63 20 69 6e 74 20 66 72 65 65 50 61  tatic int freePa
df7c0 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  ge(MemPage *pPag
df7d0 65 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  e){.  BtShared *
df7e0 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74  pBt = pPage->pBt
df7f0 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  ;.  MemPage *pPa
df800 67 65 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65  ge1 = pBt->pPage
df810 31 3b 0a 20 20 69 6e 74 20 72 63 2c 20 6e 2c 20  1;.  int rc, n, 
df820 6b 3b 0a 0a 20 20 2f 2a 20 50 72 65 70 61 72 65  k;..  /* Prepare
df830 20 74 68 65 20 70 61 67 65 20 66 6f 72 20 66 72   the page for fr
df840 65 65 69 6e 67 20 2a 2f 0a 20 20 61 73 73 65 72  eeing */.  asser
df850 74 28 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3e 31  t( pPage->pgno>1
df860 20 29 3b 0a 20 20 70 50 61 67 65 2d 3e 69 73 49   );.  pPage->isI
df870 6e 69 74 20 3d 20 30 3b 0a 20 20 72 65 6c 65 61  nit = 0;.  relea
df880 73 65 50 61 67 65 28 70 50 61 67 65 2d 3e 70 50  sePage(pPage->pP
df890 61 72 65 6e 74 29 3b 0a 20 20 70 50 61 67 65 2d  arent);.  pPage-
df8a0 3e 70 50 61 72 65 6e 74 20 3d 20 30 3b 0a 0a 20  >pParent = 0;.. 
df8b0 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68   /* Increment th
df8c0 65 20 66 72 65 65 20 70 61 67 65 20 63 6f 75 6e  e free page coun
df8d0 74 20 6f 6e 20 70 50 61 67 65 31 20 2a 2f 0a 20  t on pPage1 */. 
df8e0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
df8f0 65 72 57 72 69 74 65 28 70 50 61 67 65 31 2d 3e  erWrite(pPage1->
df900 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20  pDbPage);.  if( 
df910 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
df920 20 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28 26    n = get4byte(&
df930 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36  pPage1->aData[36
df940 5d 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26  ]);.  put4byte(&
df950 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36  pPage1->aData[36
df960 5d 2c 20 6e 2b 31 29 3b 0a 0a 23 69 66 64 65 66  ], n+1);..#ifdef
df970 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44   SQLITE_SECURE_D
df980 45 4c 45 54 45 0a 20 20 2f 2a 20 49 66 20 74 68  ELETE.  /* If th
df990 65 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f  e SQLITE_SECURE_
df9a0 44 45 4c 45 54 45 20 63 6f 6d 70 69 6c 65 2d 74  DELETE compile-t
df9b0 69 6d 65 20 6f 70 74 69 6f 6e 20 69 73 20 65 6e  ime option is en
df9c0 61 62 6c 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a  abled, then.  **
df9d0 20 61 6c 77 61 79 73 20 66 75 6c 6c 79 20 6f 76   always fully ov
df9e0 65 72 77 72 69 74 65 20 64 65 6c 65 74 65 64 20  erwrite deleted 
df9f0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 69 74 68  information with
dfa00 20 7a 65 72 6f 73 2e 0a 20 20 2a 2f 0a 20 20 72   zeros..  */.  r
dfa10 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
dfa20 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62  Write(pPage->pDb
dfa30 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20  Page);.  if( rc 
dfa40 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 6d  ) return rc;.  m
dfa50 65 6d 73 65 74 28 70 50 61 67 65 2d 3e 61 44 61  emset(pPage->aDa
dfa60 74 61 2c 20 30 2c 20 70 50 61 67 65 2d 3e 70 42  ta, 0, pPage->pB
dfa70 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 23 65  t->pageSize);.#e
dfa80 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
dfa90 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
dfaa0 43 55 55 4d 0a 20 20 2f 2a 20 49 66 20 74 68 65  CUUM.  /* If the
dfab0 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72   database suppor
dfac0 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 2c 20  ts auto-vacuum, 
dfad0 77 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20 69  write an entry i
dfae0 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61  n the pointer-ma
dfaf0 70 0a 20 20 2a 2a 20 74 6f 20 69 6e 64 69 63 61  p.  ** to indica
dfb00 74 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65  te that the page
dfb10 20 69 73 20 66 72 65 65 2e 0a 20 20 2a 2f 0a 20   is free..  */. 
dfb20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61   if( pBt->autoVa
dfb30 63 75 75 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d  cuum ){.    rc =
dfb40 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20   ptrmapPut(pBt, 
dfb50 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 50 54 52  pPage->pgno, PTR
dfb60 4d 41 50 5f 46 52 45 45 50 41 47 45 2c 20 30 29  MAP_FREEPAGE, 0)
dfb70 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72  ;.    if( rc ) r
dfb80 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 23 65  eturn rc;.  }.#e
dfb90 6e 64 69 66 0a 0a 20 20 69 66 28 20 6e 3d 3d 30  ndif..  if( n==0
dfba0 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20   ){.    /* This 
dfbb0 69 73 20 74 68 65 20 66 69 72 73 74 20 66 72 65  is the first fre
dfbc0 65 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 72 63  e page */.    rc
dfbd0 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57   = sqlite3PagerW
dfbe0 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50  rite(pPage->pDbP
dfbf0 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63  age);.    if( rc
dfc00 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
dfc10 20 20 6d 65 6d 73 65 74 28 70 50 61 67 65 2d 3e    memset(pPage->
dfc20 61 44 61 74 61 2c 20 30 2c 20 38 29 3b 0a 20 20  aData, 0, 8);.  
dfc30 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67    put4byte(&pPag
dfc40 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 70  e1->aData[32], p
dfc50 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20  Page->pgno);.   
dfc60 20 54 52 41 43 45 28 28 22 46 52 45 45 2d 50 41   TRACE(("FREE-PA
dfc70 47 45 3a 20 25 64 20 66 69 72 73 74 5c 6e 22 2c  GE: %d first\n",
dfc80 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 29 3b 0a   pPage->pgno));.
dfc90 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
dfca0 4f 74 68 65 72 20 66 72 65 65 20 70 61 67 65 73  Other free pages
dfcb0 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 2e 20   already exist. 
dfcc0 20 52 65 74 72 69 76 65 20 74 68 65 20 66 69 72   Retrive the fir
dfcd0 73 74 20 74 72 75 6e 6b 20 70 61 67 65 0a 20 20  st trunk page.  
dfce0 20 20 2a 2a 20 6f 66 20 74 68 65 20 66 72 65 65    ** of the free
dfcf0 6c 69 73 74 20 61 6e 64 20 66 69 6e 64 20 6f 75  list and find ou
dfd00 74 20 68 6f 77 20 6d 61 6e 79 20 6c 65 61 76 65  t how many leave
dfd10 73 20 69 74 20 68 61 73 2e 20 2a 2f 0a 20 20 20  s it has. */.   
dfd20 20 4d 65 6d 50 61 67 65 20 2a 70 54 72 75 6e 6b   MemPage *pTrunk
dfd30 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
dfd40 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70  e3BtreeGetPage(p
dfd50 42 74 2c 20 67 65 74 34 62 79 74 65 28 26 70 50  Bt, get4byte(&pP
dfd60 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29  age1->aData[32])
dfd70 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29 3b 0a 20  , &pTrunk, 0);. 
dfd80 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75     if( rc ) retu
dfd90 72 6e 20 72 63 3b 0a 20 20 20 20 6b 20 3d 20 67  rn rc;.    k = g
dfda0 65 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d  et4byte(&pTrunk-
dfdb0 3e 61 44 61 74 61 5b 34 5d 29 3b 0a 20 20 20 20  >aData[4]);.    
dfdc0 69 66 28 20 6b 3e 3d 70 42 74 2d 3e 75 73 61 62  if( k>=pBt->usab
dfdd0 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 29 7b 0a  leSize/4 - 8 ){.
dfde0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 72 75        /* The tru
dfdf0 6e 6b 20 69 73 20 66 75 6c 6c 2e 20 20 54 75 72  nk is full.  Tur
dfe00 6e 20 74 68 65 20 70 61 67 65 20 62 65 69 6e 67  n the page being
dfe10 20 66 72 65 65 64 20 69 6e 74 6f 20 61 20 6e 65   freed into a ne
dfe20 77 0a 20 20 20 20 20 20 2a 2a 20 74 72 75 6e 6b  w.      ** trunk
dfe30 20 70 61 67 65 20 77 69 74 68 20 6e 6f 20 6c 65   page with no le
dfe40 61 76 65 73 2e 20 2a 2f 0a 20 20 20 20 20 20 72  aves. */.      r
dfe50 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
dfe60 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62  Write(pPage->pDb
dfe70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28  Page);.      if(
dfe80 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
dfe90 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28  .      put4byte(
dfea0 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 70 54  pPage->aData, pT
dfeb0 72 75 6e 6b 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20  runk->pgno);.   
dfec0 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61     put4byte(&pPa
dfed0 67 65 2d 3e 61 44 61 74 61 5b 34 5d 2c 20 30 29  ge->aData[4], 0)
dfee0 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65  ;.      put4byte
dfef0 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b  (&pPage1->aData[
dff00 33 32 5d 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f  32], pPage->pgno
dff10 29 3b 0a 20 20 20 20 20 20 54 52 41 43 45 28 28  );.      TRACE((
dff20 22 46 52 45 45 2d 50 41 47 45 3a 20 25 64 20 6e  "FREE-PAGE: %d n
dff30 65 77 20 74 72 75 6e 6b 20 70 61 67 65 20 72 65  ew trunk page re
dff40 70 6c 61 63 69 6e 67 20 25 64 5c 6e 22 2c 0a 20  placing %d\n",. 
dff50 20 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61               pPa
dff60 67 65 2d 3e 70 67 6e 6f 2c 20 70 54 72 75 6e 6b  ge->pgno, pTrunk
dff70 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 7d 65  ->pgno));.    }e
dff80 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41 64  lse{.      /* Ad
dff90 64 20 74 68 65 20 6e 65 77 6c 79 20 66 72 65 65  d the newly free
dffa0 64 20 70 61 67 65 20 61 73 20 61 20 6c 65 61 66  d page as a leaf
dffb0 20 6f 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20   on the current 
dffc0 74 72 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 20 72  trunk */.      r
dffd0 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
dffe0 57 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70 44  Write(pTrunk->pD
dfff0 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66  bPage);.      if
e0000 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
e0010 29 7b 0a 20 20 20 20 20 20 20 20 70 75 74 34 62  ){.        put4b
e0020 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61  yte(&pTrunk->aDa
e0030 74 61 5b 34 5d 2c 20 6b 2b 31 29 3b 0a 20 20 20  ta[4], k+1);.   
e0040 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70       put4byte(&p
e0050 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 2b 6b  Trunk->aData[8+k
e0060 2a 34 5d 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f  *4], pPage->pgno
e0070 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
e0080 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 0a  E_SECURE_DELETE.
e0090 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50          sqlite3P
e00a0 61 67 65 72 44 6f 6e 74 57 72 69 74 65 28 70 50  agerDontWrite(pP
e00b0 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 23  age->pDbPage);.#
e00c0 65 6e 64 69 66 0a 20 20 20 20 20 20 7d 0a 20 20  endif.      }.  
e00d0 20 20 20 20 54 52 41 43 45 28 28 22 46 52 45 45      TRACE(("FREE
e00e0 2d 50 41 47 45 3a 20 25 64 20 6c 65 61 66 20 6f  -PAGE: %d leaf o
e00f0 6e 20 74 72 75 6e 6b 20 70 61 67 65 20 25 64 5c  n trunk page %d\
e0100 6e 22 2c 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 70  n",pPage->pgno,p
e0110 54 72 75 6e 6b 2d 3e 70 67 6e 6f 29 29 3b 0a 20  Trunk->pgno));. 
e0120 20 20 20 7d 0a 20 20 20 20 72 65 6c 65 61 73 65     }.    release
e0130 50 61 67 65 28 70 54 72 75 6e 6b 29 3b 0a 20 20  Page(pTrunk);.  
e0140 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
e0150 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 79  ../*.** Free any
e0160 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20   overflow pages 
e0170 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
e0180 74 68 65 20 67 69 76 65 6e 20 43 65 6c 6c 2e 0a  the given Cell..
e0190 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6c  */.static int cl
e01a0 65 61 72 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20  earCell(MemPage 
e01b0 2a 70 50 61 67 65 2c 20 75 6e 73 69 67 6e 65 64  *pPage, unsigned
e01c0 20 63 68 61 72 20 2a 70 43 65 6c 6c 29 7b 0a 20   char *pCell){. 
e01d0 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
e01e0 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 43   pPage->pBt;.  C
e01f0 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20  ellInfo info;.  
e0200 50 67 6e 6f 20 6f 76 66 6c 50 67 6e 6f 3b 0a 20  Pgno ovflPgno;. 
e0210 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 6e   int rc;.  int n
e0220 4f 76 66 6c 3b 0a 20 20 69 6e 74 20 6f 76 66 6c  Ovfl;.  int ovfl
e0230 50 61 67 65 53 69 7a 65 3b 0a 0a 20 20 73 71 6c  PageSize;..  sql
e0240 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65  ite3BtreeParseCe
e0250 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65  llPtr(pPage, pCe
e0260 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 69 66  ll, &info);.  if
e0270 28 20 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77  ( info.iOverflow
e0280 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
e0290 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 2f 2a  n SQLITE_OK;  /*
e02a0 20 4e 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   No overflow pag
e02b0 65 73 2e 20 52 65 74 75 72 6e 20 77 69 74 68 6f  es. Return witho
e02c0 75 74 20 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e  ut doing anythin
e02d0 67 20 2a 2f 0a 20 20 7d 0a 20 20 6f 76 66 6c 50  g */.  }.  ovflP
e02e0 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26  gno = get4byte(&
e02f0 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72  pCell[info.iOver
e0300 66 6c 6f 77 5d 29 3b 0a 20 20 6f 76 66 6c 50 61  flow]);.  ovflPa
e0310 67 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73  geSize = pBt->us
e0320 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20  ableSize - 4;.  
e0330 6e 4f 76 66 6c 20 3d 20 28 69 6e 66 6f 2e 6e 50  nOvfl = (info.nP
e0340 61 79 6c 6f 61 64 20 2d 20 69 6e 66 6f 2e 6e 4c  ayload - info.nL
e0350 6f 63 61 6c 20 2b 20 6f 76 66 6c 50 61 67 65 53  ocal + ovflPageS
e0360 69 7a 65 20 2d 20 31 29 2f 6f 76 66 6c 50 61 67  ize - 1)/ovflPag
e0370 65 53 69 7a 65 3b 0a 20 20 61 73 73 65 72 74 28  eSize;.  assert(
e0380 20 6f 76 66 6c 50 67 6e 6f 3d 3d 30 20 7c 7c 20   ovflPgno==0 || 
e0390 6e 4f 76 66 6c 3e 30 20 29 3b 0a 20 20 77 68 69  nOvfl>0 );.  whi
e03a0 6c 65 28 20 6e 4f 76 66 6c 2d 2d 20 29 7b 0a 20  le( nOvfl-- ){. 
e03b0 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f 76 66     MemPage *pOvf
e03c0 6c 3b 0a 20 20 20 20 69 66 28 20 6f 76 66 6c 50  l;.    if( ovflP
e03d0 67 6e 6f 3d 3d 30 20 7c 7c 20 6f 76 66 6c 50 67  gno==0 || ovflPg
e03e0 6e 6f 3e 73 71 6c 69 74 65 33 50 61 67 65 72 50  no>sqlite3PagerP
e03f0 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50  agecount(pBt->pP
e0400 61 67 65 72 29 20 29 7b 0a 20 20 20 20 20 20 72  ager) ){.      r
e0410 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
e0420 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d  RUPT_BKPT;.    }
e0430 0a 0a 20 20 20 20 72 63 20 3d 20 67 65 74 4f 76  ..    rc = getOv
e0440 65 72 66 6c 6f 77 50 61 67 65 28 70 42 74 2c 20  erflowPage(pBt, 
e0450 6f 76 66 6c 50 67 6e 6f 2c 20 26 70 4f 76 66 6c  ovflPgno, &pOvfl
e0460 2c 20 28 6e 4f 76 66 6c 3d 3d 30 29 3f 30 3a 26  , (nOvfl==0)?0:&
e0470 6f 76 66 6c 50 67 6e 6f 29 3b 0a 20 20 20 20 69  ovflPgno);.    i
e0480 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
e0490 63 3b 0a 20 20 20 20 72 63 20 3d 20 66 72 65 65  c;.    rc = free
e04a0 50 61 67 65 28 70 4f 76 66 6c 29 3b 0a 20 20 20  Page(pOvfl);.   
e04b0 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72   sqlite3PagerUnr
e04c0 65 66 28 70 4f 76 66 6c 2d 3e 70 44 62 50 61 67  ef(pOvfl->pDbPag
e04d0 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  e);.    if( rc )
e04e0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
e04f0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
e0500 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65  OK;.}../*.** Cre
e0510 61 74 65 20 74 68 65 20 62 79 74 65 20 73 65 71  ate the byte seq
e0520 75 65 6e 63 65 20 75 73 65 64 20 74 6f 20 72 65  uence used to re
e0530 70 72 65 73 65 6e 74 20 61 20 63 65 6c 6c 20 6f  present a cell o
e0540 6e 20 70 61 67 65 20 70 50 61 67 65 0a 2a 2a 20  n page pPage.** 
e0550 61 6e 64 20 77 72 69 74 65 20 74 68 61 74 20 62  and write that b
e0560 79 74 65 20 73 65 71 75 65 6e 63 65 20 69 6e 74  yte sequence int
e0570 6f 20 70 43 65 6c 6c 5b 5d 2e 20 20 4f 76 65 72  o pCell[].  Over
e0580 66 6c 6f 77 20 70 61 67 65 73 20 61 72 65 0a 2a  flow pages are.*
e0590 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20  * allocated and 
e05a0 66 69 6c 6c 65 64 20 69 6e 20 61 73 20 6e 65 63  filled in as nec
e05b0 65 73 73 61 72 79 2e 20 20 54 68 65 20 63 61 6c  essary.  The cal
e05c0 6c 69 6e 67 20 70 72 6f 63 65 64 75 72 65 0a 2a  ling procedure.*
e05d0 2a 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65  * is responsible
e05e0 20 66 6f 72 20 6d 61 6b 69 6e 67 20 73 75 72 65   for making sure
e05f0 20 73 75 66 66 69 63 69 65 6e 74 20 73 70 61 63   sufficient spac
e0600 65 20 68 61 73 20 62 65 65 6e 20 61 6c 6c 6f 63  e has been alloc
e0610 61 74 65 64 0a 2a 2a 20 66 6f 72 20 70 43 65 6c  ated.** for pCel
e0620 6c 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20  l[]..**.** Note 
e0630 74 68 61 74 20 70 43 65 6c 6c 20 64 6f 65 73 20  that pCell does 
e0640 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 20 6e 65  not necessary ne
e0650 65 64 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74  ed to point to t
e0660 68 65 20 70 50 61 67 65 2d 3e 61 44 61 74 61 0a  he pPage->aData.
e0670 2a 2a 20 61 72 65 61 2e 20 20 70 43 65 6c 6c 20  ** area.  pCell 
e0680 6d 69 67 68 74 20 70 6f 69 6e 74 20 74 6f 20 73  might point to s
e0690 6f 6d 65 20 74 65 6d 70 6f 72 61 72 79 20 73 74  ome temporary st
e06a0 6f 72 61 67 65 2e 20 20 54 68 65 20 63 65 6c 6c  orage.  The cell
e06b0 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 63 6f 6e 73   will.** be cons
e06c0 74 72 75 63 74 65 64 20 69 6e 20 74 68 69 73 20  tructed in this 
e06d0 74 65 6d 70 6f 72 61 72 79 20 61 72 65 61 20 74  temporary area t
e06e0 68 65 6e 20 63 6f 70 69 65 64 20 69 6e 74 6f 20  hen copied into 
e06f0 70 50 61 67 65 2d 3e 61 44 61 74 61 0a 2a 2a 20  pPage->aData.** 
e0700 6c 61 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  later..*/.static
e0710 20 69 6e 74 20 66 69 6c 6c 49 6e 43 65 6c 6c 28   int fillInCell(
e0720 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  .  MemPage *pPag
e0730 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
e0740 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 74 68    /* The page th
e0750 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  at contains the 
e0760 63 65 6c 6c 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  cell */.  unsign
e0770 65 64 20 63 68 61 72 20 2a 70 43 65 6c 6c 2c 20  ed char *pCell, 
e0780 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 70           /* Comp
e0790 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74 68 65  lete text of the
e07a0 20 63 65 6c 6c 20 2a 2f 0a 20 20 63 6f 6e 73 74   cell */.  const
e07b0 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 36 34   void *pKey, i64
e07c0 20 6e 4b 65 79 2c 20 20 20 20 2f 2a 20 54 68 65   nKey,    /* The
e07d0 20 6b 65 79 20 2a 2f 0a 20 20 63 6f 6e 73 74 20   key */.  const 
e07e0 76 6f 69 64 20 2a 70 44 61 74 61 2c 69 6e 74 20  void *pData,int 
e07f0 6e 44 61 74 61 2c 20 20 20 2f 2a 20 54 68 65 20  nData,   /* The 
e0800 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a  data */.  int nZ
e0810 65 72 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20  ero,            
e0820 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72           /* Extr
e0830 61 20 7a 65 72 6f 20 62 79 74 65 73 20 74 6f 20  a zero bytes to 
e0840 61 70 70 65 6e 64 20 74 6f 20 70 44 61 74 61 20  append to pData 
e0850 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 53 69 7a 65  */.  int *pnSize
e0860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0870 20 20 20 20 2f 2a 20 57 72 69 74 65 20 63 65 6c      /* Write cel
e0880 6c 20 73 69 7a 65 20 68 65 72 65 20 2a 2f 0a 29  l size here */.)
e0890 7b 0a 20 20 69 6e 74 20 6e 50 61 79 6c 6f 61 64  {.  int nPayload
e08a0 3b 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a 70 53  ;.  const u8 *pS
e08b0 72 63 3b 0a 20 20 69 6e 74 20 6e 53 72 63 2c 20  rc;.  int nSrc, 
e08c0 6e 2c 20 72 63 3b 0a 20 20 69 6e 74 20 73 70 61  n, rc;.  int spa
e08d0 63 65 4c 65 66 74 3b 0a 20 20 4d 65 6d 50 61 67  ceLeft;.  MemPag
e08e0 65 20 2a 70 4f 76 66 6c 20 3d 20 30 3b 0a 20 20  e *pOvfl = 0;.  
e08f0 4d 65 6d 50 61 67 65 20 2a 70 54 6f 52 65 6c 65  MemPage *pToRele
e0900 61 73 65 20 3d 20 30 3b 0a 20 20 75 6e 73 69 67  ase = 0;.  unsig
e0910 6e 65 64 20 63 68 61 72 20 2a 70 50 72 69 6f 72  ned char *pPrior
e0920 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  ;.  unsigned cha
e0930 72 20 2a 70 50 61 79 6c 6f 61 64 3b 0a 20 20 42  r *pPayload;.  B
e0940 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
e0950 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 50 67 6e  Page->pBt;.  Pgn
e0960 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d 20 30 3b 0a  o pgnoOvfl = 0;.
e0970 20 20 69 6e 74 20 6e 48 65 61 64 65 72 3b 0a 20    int nHeader;. 
e0980 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a   CellInfo info;.
e0990 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68  .  /* Fill in th
e09a0 65 20 68 65 61 64 65 72 2e 20 2a 2f 0a 20 20 6e  e header. */.  n
e09b0 48 65 61 64 65 72 20 3d 20 30 3b 0a 20 20 69 66  Header = 0;.  if
e09c0 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  ( !pPage->leaf )
e09d0 7b 0a 20 20 20 20 6e 48 65 61 64 65 72 20 2b 3d  {.    nHeader +=
e09e0 20 34 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50   4;.  }.  if( pP
e09f0 61 67 65 2d 3e 68 61 73 44 61 74 61 20 29 7b 0a  age->hasData ){.
e0a00 20 20 20 20 6e 48 65 61 64 65 72 20 2b 3d 20 70      nHeader += p
e0a10 75 74 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b  utVarint(&pCell[
e0a20 6e 48 65 61 64 65 72 5d 2c 20 6e 44 61 74 61 2b  nHeader], nData+
e0a30 6e 5a 65 72 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b  nZero);.  }else{
e0a40 0a 20 20 20 20 6e 44 61 74 61 20 3d 20 6e 5a 65  .    nData = nZe
e0a50 72 6f 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 6e 48  ro = 0;.  }.  nH
e0a60 65 61 64 65 72 20 2b 3d 20 70 75 74 56 61 72 69  eader += putVari
e0a70 6e 74 28 26 70 43 65 6c 6c 5b 6e 48 65 61 64 65  nt(&pCell[nHeade
e0a80 72 5d 2c 20 2a 28 75 36 34 2a 29 26 6e 4b 65 79  r], *(u64*)&nKey
e0a90 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
e0aa0 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50  eParseCellPtr(pP
e0ab0 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66  age, pCell, &inf
e0ac0 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e  o);.  assert( in
e0ad0 66 6f 2e 6e 48 65 61 64 65 72 3d 3d 6e 48 65 61  fo.nHeader==nHea
e0ae0 64 65 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28  der );.  assert(
e0af0 20 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 6e 4b 65 79   info.nKey==nKey
e0b00 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e   );.  assert( in
e0b10 66 6f 2e 6e 44 61 74 61 3d 3d 6e 44 61 74 61 2b  fo.nData==nData+
e0b20 6e 5a 65 72 6f 20 29 3b 0a 20 20 0a 20 20 2f 2a  nZero );.  .  /*
e0b30 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 70 61 79   Fill in the pay
e0b40 6c 6f 61 64 20 2a 2f 0a 20 20 6e 50 61 79 6c 6f  load */.  nPaylo
e0b50 61 64 20 3d 20 6e 44 61 74 61 20 2b 20 6e 5a 65  ad = nData + nZe
e0b60 72 6f 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d  ro;.  if( pPage-
e0b70 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 70  >intKey ){.    p
e0b80 53 72 63 20 3d 20 70 44 61 74 61 3b 0a 20 20 20  Src = pData;.   
e0b90 20 6e 53 72 63 20 3d 20 6e 44 61 74 61 3b 0a 20   nSrc = nData;. 
e0ba0 20 20 20 6e 44 61 74 61 20 3d 20 30 3b 0a 20 20     nData = 0;.  
e0bb0 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 50 61 79 6c  }else{.    nPayl
e0bc0 6f 61 64 20 2b 3d 20 6e 4b 65 79 3b 0a 20 20 20  oad += nKey;.   
e0bd0 20 70 53 72 63 20 3d 20 70 4b 65 79 3b 0a 20 20   pSrc = pKey;.  
e0be0 20 20 6e 53 72 63 20 3d 20 6e 4b 65 79 3b 0a 20    nSrc = nKey;. 
e0bf0 20 7d 0a 20 20 2a 70 6e 53 69 7a 65 20 3d 20 69   }.  *pnSize = i
e0c00 6e 66 6f 2e 6e 53 69 7a 65 3b 0a 20 20 73 70 61  nfo.nSize;.  spa
e0c10 63 65 4c 65 66 74 20 3d 20 69 6e 66 6f 2e 6e 4c  ceLeft = info.nL
e0c20 6f 63 61 6c 3b 0a 20 20 70 50 61 79 6c 6f 61 64  ocal;.  pPayload
e0c30 20 3d 20 26 70 43 65 6c 6c 5b 6e 48 65 61 64 65   = &pCell[nHeade
e0c40 72 5d 3b 0a 20 20 70 50 72 69 6f 72 20 3d 20 26  r];.  pPrior = &
e0c50 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72  pCell[info.iOver
e0c60 66 6c 6f 77 5d 3b 0a 0a 20 20 77 68 69 6c 65 28  flow];..  while(
e0c70 20 6e 50 61 79 6c 6f 61 64 3e 30 20 29 7b 0a 20   nPayload>0 ){. 
e0c80 20 20 20 69 66 28 20 73 70 61 63 65 4c 65 66 74     if( spaceLeft
e0c90 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  ==0 ){.      int
e0ca0 20 69 73 45 78 61 63 74 20 3d 20 30 3b 0a 23 69   isExact = 0;.#i
e0cb0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
e0cc0 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20  T_AUTOVACUUM.   
e0cd0 20 20 20 50 67 6e 6f 20 70 67 6e 6f 50 74 72 6d     Pgno pgnoPtrm
e0ce0 61 70 20 3d 20 70 67 6e 6f 4f 76 66 6c 3b 20 2f  ap = pgnoOvfl; /
e0cf0 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 20  * Overflow page 
e0d00 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72  pointer-map entr
e0d10 79 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 20  y page */.      
e0d20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63  if( pBt->autoVac
e0d30 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 64  uum ){.        d
e0d40 6f 7b 0a 20 20 20 20 20 20 20 20 20 20 70 67 6e  o{.          pgn
e0d50 6f 4f 76 66 6c 2b 2b 3b 0a 20 20 20 20 20 20 20  oOvfl++;.       
e0d60 20 7d 20 77 68 69 6c 65 28 20 0a 20 20 20 20 20   } while( .     
e0d70 20 20 20 20 20 50 54 52 4d 41 50 5f 49 53 50 41       PTRMAP_ISPA
e0d80 47 45 28 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c  GE(pBt, pgnoOvfl
e0d90 29 20 7c 7c 20 70 67 6e 6f 4f 76 66 6c 3d 3d 50  ) || pgnoOvfl==P
e0da0 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45  ENDING_BYTE_PAGE
e0db0 28 70 42 74 29 20 0a 20 20 20 20 20 20 20 20 29  (pBt) .        )
e0dc0 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 67  ;.        if( pg
e0dd0 6e 6f 4f 76 66 6c 3e 31 20 29 7b 0a 20 20 20 20  noOvfl>1 ){.    
e0de0 20 20 20 20 20 20 2f 2a 20 69 73 45 78 61 63 74        /* isExact
e0df0 20 3d 20 31 3b 20 2a 2f 0a 20 20 20 20 20 20 20   = 1; */.       
e0e00 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69   }.      }.#endi
e0e10 66 0a 20 20 20 20 20 20 72 63 20 3d 20 61 6c 6c  f.      rc = all
e0e20 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70  ocateBtreePage(p
e0e30 42 74 2c 20 26 70 4f 76 66 6c 2c 20 26 70 67 6e  Bt, &pOvfl, &pgn
e0e40 6f 4f 76 66 6c 2c 20 70 67 6e 6f 4f 76 66 6c 2c  oOvfl, pgnoOvfl,
e0e50 20 69 73 45 78 61 63 74 29 3b 0a 23 69 66 6e 64   isExact);.#ifnd
e0e60 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
e0e70 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20  UTOVACUUM.      
e0e80 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61  /* If the databa
e0e90 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f  se supports auto
e0ea0 2d 76 61 63 75 75 6d 2c 20 61 6e 64 20 74 68 65  -vacuum, and the
e0eb0 20 73 65 63 6f 6e 64 20 6f 72 20 73 75 62 73 65   second or subse
e0ec0 71 75 65 6e 74 0a 20 20 20 20 20 20 2a 2a 20 6f  quent.      ** o
e0ed0 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 73 20  verflow page is 
e0ee0 62 65 69 6e 67 20 61 6c 6c 6f 63 61 74 65 64 2c  being allocated,
e0ef0 20 61 64 64 20 61 6e 20 65 6e 74 72 79 20 74 6f   add an entry to
e0f00 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70   the pointer-map
e0f10 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 74 68  .      ** for th
e0f20 61 74 20 70 61 67 65 20 6e 6f 77 2e 20 0a 20 20  at page now. .  
e0f30 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
e0f40 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66  If this is the f
e0f50 69 72 73 74 20 6f 76 65 72 66 6c 6f 77 20 70 61  irst overflow pa
e0f60 67 65 2c 20 74 68 65 6e 20 77 72 69 74 65 20 61  ge, then write a
e0f70 20 70 61 72 74 69 61 6c 20 65 6e 74 72 79 20 0a   partial entry .
e0f80 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20        ** to the 
e0f90 70 6f 69 6e 74 65 72 2d 6d 61 70 2e 20 49 66 20  pointer-map. If 
e0fa0 77 65 20 77 72 69 74 65 20 6e 6f 74 68 69 6e 67  we write nothing
e0fb0 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 65 72   to this pointer
e0fc0 2d 6d 61 70 20 73 6c 6f 74 2c 0a 20 20 20 20 20  -map slot,.     
e0fd0 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 6f 70 74   ** then the opt
e0fe0 69 6d 69 73 74 69 63 20 6f 76 65 72 66 6c 6f 77  imistic overflow
e0ff0 20 63 68 61 69 6e 20 70 72 6f 63 65 73 73 69 6e   chain processin
e1000 67 20 69 6e 20 63 6c 65 61 72 43 65 6c 6c 28 29  g in clearCell()
e1010 0a 20 20 20 20 20 20 2a 2a 20 6d 61 79 20 6d 69  .      ** may mi
e1020 73 69 6e 74 65 72 70 72 65 74 20 74 68 65 20 75  sinterpret the u
e1030 6e 69 6e 69 74 69 61 6c 69 73 65 64 20 76 61 6c  ninitialised val
e1040 75 65 73 20 61 6e 64 20 64 65 6c 65 74 65 20 74  ues and delete t
e1050 68 65 0a 20 20 20 20 20 20 2a 2a 20 77 72 6f 6e  he.      ** wron
e1060 67 20 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65  g pages from the
e1070 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 20   database..     
e1080 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 42   */.      if( pB
e1090 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 26  t->autoVacuum &&
e10a0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
e10b0 7b 0a 20 20 20 20 20 20 20 20 75 38 20 65 54 79  {.        u8 eTy
e10c0 70 65 20 3d 20 28 70 67 6e 6f 50 74 72 6d 61 70  pe = (pgnoPtrmap
e10d0 3f 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57  ?PTRMAP_OVERFLOW
e10e0 32 3a 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f  2:PTRMAP_OVERFLO
e10f0 57 31 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  W1);.        rc 
e1100 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c  = ptrmapPut(pBt,
e1110 20 70 67 6e 6f 4f 76 66 6c 2c 20 65 54 79 70 65   pgnoOvfl, eType
e1120 2c 20 70 67 6e 6f 50 74 72 6d 61 70 29 3b 0a 20  , pgnoPtrmap);. 
e1130 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b         if( rc ){
e1140 0a 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 61  .          relea
e1150 73 65 50 61 67 65 28 70 4f 76 66 6c 29 3b 0a 20  sePage(pOvfl);. 
e1160 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
e1170 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69 66  .#endif.      if
e1180 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20  ( rc ){.        
e1190 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 6f 52  releasePage(pToR
e11a0 65 6c 65 61 73 65 29 3b 0a 20 20 20 20 20 20 20  elease);.       
e11b0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
e11c0 20 20 7d 0a 20 20 20 20 20 20 70 75 74 34 62 79    }.      put4by
e11d0 74 65 28 70 50 72 69 6f 72 2c 20 70 67 6e 6f 4f  te(pPrior, pgnoO
e11e0 76 66 6c 29 3b 0a 20 20 20 20 20 20 72 65 6c 65  vfl);.      rele
e11f0 61 73 65 50 61 67 65 28 70 54 6f 52 65 6c 65 61  asePage(pToRelea
e1200 73 65 29 3b 0a 20 20 20 20 20 20 70 54 6f 52 65  se);.      pToRe
e1210 6c 65 61 73 65 20 3d 20 70 4f 76 66 6c 3b 0a 20  lease = pOvfl;. 
e1220 20 20 20 20 20 70 50 72 69 6f 72 20 3d 20 70 4f       pPrior = pO
e1230 76 66 6c 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20  vfl->aData;.    
e1240 20 20 70 75 74 34 62 79 74 65 28 70 50 72 69 6f    put4byte(pPrio
e1250 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 50 61  r, 0);.      pPa
e1260 79 6c 6f 61 64 20 3d 20 26 70 4f 76 66 6c 2d 3e  yload = &pOvfl->
e1270 61 44 61 74 61 5b 34 5d 3b 0a 20 20 20 20 20 20  aData[4];.      
e1280 73 70 61 63 65 4c 65 66 74 20 3d 20 70 42 74 2d  spaceLeft = pBt-
e1290 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b  >usableSize - 4;
e12a0 0a 20 20 20 20 7d 0a 20 20 20 20 6e 20 3d 20 6e  .    }.    n = n
e12b0 50 61 79 6c 6f 61 64 3b 0a 20 20 20 20 69 66 28  Payload;.    if(
e12c0 20 6e 3e 73 70 61 63 65 4c 65 66 74 20 29 20 6e   n>spaceLeft ) n
e12d0 20 3d 20 73 70 61 63 65 4c 65 66 74 3b 0a 20 20   = spaceLeft;.  
e12e0 20 20 69 66 28 20 6e 53 72 63 3e 30 20 29 7b 0a    if( nSrc>0 ){.
e12f0 20 20 20 20 20 20 69 66 28 20 6e 3e 6e 53 72 63        if( n>nSrc
e1300 20 29 20 6e 20 3d 20 6e 53 72 63 3b 0a 20 20 20   ) n = nSrc;.   
e1310 20 20 20 61 73 73 65 72 74 28 20 70 53 72 63 20     assert( pSrc 
e1320 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  );.      memcpy(
e1330 70 50 61 79 6c 6f 61 64 2c 20 70 53 72 63 2c 20  pPayload, pSrc, 
e1340 6e 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  n);.    }else{. 
e1350 20 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61 79       memset(pPay
e1360 6c 6f 61 64 2c 20 30 2c 20 6e 29 3b 0a 20 20 20  load, 0, n);.   
e1370 20 7d 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 20   }.    nPayload 
e1380 2d 3d 20 6e 3b 0a 20 20 20 20 70 50 61 79 6c 6f  -= n;.    pPaylo
e1390 61 64 20 2b 3d 20 6e 3b 0a 20 20 20 20 70 53 72  ad += n;.    pSr
e13a0 63 20 2b 3d 20 6e 3b 0a 20 20 20 20 6e 53 72 63  c += n;.    nSrc
e13b0 20 2d 3d 20 6e 3b 0a 20 20 20 20 73 70 61 63 65   -= n;.    space
e13c0 4c 65 66 74 20 2d 3d 20 6e 3b 0a 20 20 20 20 69  Left -= n;.    i
e13d0 66 28 20 6e 53 72 63 3d 3d 30 20 29 7b 0a 20 20  f( nSrc==0 ){.  
e13e0 20 20 20 20 6e 53 72 63 20 3d 20 6e 44 61 74 61      nSrc = nData
e13f0 3b 0a 20 20 20 20 20 20 70 53 72 63 20 3d 20 70  ;.      pSrc = p
e1400 44 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Data;.    }.  }.
e1410 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54    releasePage(pT
e1420 6f 52 65 6c 65 61 73 65 29 3b 0a 20 20 72 65 74  oRelease);.  ret
e1430 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
e1440 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74  ../*.** Change t
e1450 68 65 20 4d 65 6d 50 61 67 65 2e 70 50 61 72 65  he MemPage.pPare
e1460 6e 74 20 70 6f 69 6e 74 65 72 20 6f 6e 20 74 68  nt pointer on th
e1470 65 20 70 61 67 65 20 77 68 6f 73 65 20 6e 75 6d  e page whose num
e1480 62 65 72 20 69 73 0a 2a 2a 20 67 69 76 65 6e 20  ber is.** given 
e1490 69 6e 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  in the second ar
e14a0 67 75 6d 65 6e 74 20 73 6f 20 74 68 61 74 20 4d  gument so that M
e14b0 65 6d 50 61 67 65 2e 70 50 61 72 65 6e 74 20 68  emPage.pParent h
e14c0 6f 6c 64 73 20 74 68 65 0a 2a 2a 20 70 6f 69 6e  olds the.** poin
e14d0 74 65 72 20 69 6e 20 74 68 65 20 74 68 69 72 64  ter in the third
e14e0 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74   argument..*/.st
e14f0 61 74 69 63 20 69 6e 74 20 72 65 70 61 72 65 6e  atic int reparen
e1500 74 50 61 67 65 28 42 74 53 68 61 72 65 64 20 2a  tPage(BtShared *
e1510 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20  pBt, Pgno pgno, 
e1520 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 50 61 72  MemPage *pNewPar
e1530 65 6e 74 2c 20 69 6e 74 20 69 64 78 29 7b 0a 20  ent, int idx){. 
e1540 20 4d 65 6d 50 61 67 65 20 2a 70 54 68 69 73 3b   MemPage *pThis;
e1550 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61  .  DbPage *pDbPa
e1560 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  ge;..  assert( p
e1570 4e 65 77 50 61 72 65 6e 74 21 3d 30 20 29 3b 0a  NewParent!=0 );.
e1580 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 29 20    if( pgno==0 ) 
e1590 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
e15a0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d  ;.  assert( pBt-
e15b0 3e 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20  >pPager!=0 );.  
e15c0 70 44 62 50 61 67 65 20 3d 20 73 71 6c 69 74 65  pDbPage = sqlite
e15d0 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 70 42 74  3PagerLookup(pBt
e15e0 2d 3e 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 3b  ->pPager, pgno);
e15f0 0a 20 20 69 66 28 20 70 44 62 50 61 67 65 20 29  .  if( pDbPage )
e1600 7b 0a 20 20 20 20 70 54 68 69 73 20 3d 20 28 4d  {.    pThis = (M
e1610 65 6d 50 61 67 65 20 2a 29 73 71 6c 69 74 65 33  emPage *)sqlite3
e1620 50 61 67 65 72 47 65 74 45 78 74 72 61 28 70 44  PagerGetExtra(pD
e1630 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20  bPage);.    if( 
e1640 70 54 68 69 73 2d 3e 69 73 49 6e 69 74 20 29 7b  pThis->isInit ){
e1650 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
e1660 54 68 69 73 2d 3e 61 44 61 74 61 3d 3d 28 73 71  This->aData==(sq
e1670 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74  lite3PagerGetDat
e1680 61 28 70 44 62 50 61 67 65 29 29 20 29 3b 0a 20  a(pDbPage)) );. 
e1690 20 20 20 20 20 69 66 28 20 70 54 68 69 73 2d 3e       if( pThis->
e16a0 70 50 61 72 65 6e 74 21 3d 70 4e 65 77 50 61 72  pParent!=pNewPar
e16b0 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ent ){.        i
e16c0 66 28 20 70 54 68 69 73 2d 3e 70 50 61 72 65 6e  f( pThis->pParen
e16d0 74 20 29 20 73 71 6c 69 74 65 33 50 61 67 65 72  t ) sqlite3Pager
e16e0 55 6e 72 65 66 28 70 54 68 69 73 2d 3e 70 50 61  Unref(pThis->pPa
e16f0 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a  rent->pDbPage);.
e1700 20 20 20 20 20 20 20 20 70 54 68 69 73 2d 3e 70          pThis->p
e1710 50 61 72 65 6e 74 20 3d 20 70 4e 65 77 50 61 72  Parent = pNewPar
e1720 65 6e 74 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ent;.        sql
e1730 69 74 65 33 50 61 67 65 72 52 65 66 28 70 4e 65  ite3PagerRef(pNe
e1740 77 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65  wParent->pDbPage
e1750 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
e1760 20 70 54 68 69 73 2d 3e 69 64 78 50 61 72 65 6e   pThis->idxParen
e1770 74 20 3d 20 69 64 78 3b 0a 20 20 20 20 7d 0a 20  t = idx;.    }. 
e1780 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55     sqlite3PagerU
e1790 6e 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a 20  nref(pDbPage);. 
e17a0 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   }..#ifndef SQLI
e17b0 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
e17c0 55 4d 0a 20 20 69 66 28 20 70 42 74 2d 3e 61 75  UM.  if( pBt->au
e17d0 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20  toVacuum ){.    
e17e0 72 65 74 75 72 6e 20 70 74 72 6d 61 70 50 75 74  return ptrmapPut
e17f0 28 70 42 74 2c 20 70 67 6e 6f 2c 20 50 54 52 4d  (pBt, pgno, PTRM
e1800 41 50 5f 42 54 52 45 45 2c 20 70 4e 65 77 50 61  AP_BTREE, pNewPa
e1810 72 65 6e 74 2d 3e 70 67 6e 6f 29 3b 0a 20 20 7d  rent->pgno);.  }
e1820 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e  .#endif.  return
e1830 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a   SQLITE_OK;.}...
e1840 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
e1850 65 20 70 50 61 72 65 6e 74 20 70 6f 69 6e 74 65  e pParent pointe
e1860 72 20 6f 66 20 61 6c 6c 20 63 68 69 6c 64 72 65  r of all childre
e1870 6e 20 6f 66 20 70 50 61 67 65 20 74 6f 20 70 6f  n of pPage to po
e1880 69 6e 74 20 62 61 63 6b 0a 2a 2a 20 74 6f 20 70  int back.** to p
e1890 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f  Page..**.** In o
e18a0 74 68 65 72 20 77 6f 72 64 73 2c 20 66 6f 72 20  ther words, for 
e18b0 65 76 65 72 79 20 63 68 69 6c 64 20 6f 66 20 70  every child of p
e18c0 50 61 67 65 2c 20 69 6e 76 6f 6b 65 20 72 65 70  Page, invoke rep
e18d0 61 72 65 6e 74 50 61 67 65 28 29 0a 2a 2a 20 74  arentPage().** t
e18e0 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74  o make sure that
e18f0 20 65 61 63 68 20 63 68 69 6c 64 20 6b 6e 6f 77   each child know
e1900 73 20 74 68 61 74 20 70 50 61 67 65 20 69 73 20  s that pPage is 
e1910 69 74 73 20 70 61 72 65 6e 74 2e 0a 2a 2a 0a 2a  its parent..**.*
e1920 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67  * This routine g
e1930 65 74 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72  ets called after
e1940 20 79 6f 75 20 6d 65 6d 63 70 79 28 29 20 6f 6e   you memcpy() on
e1950 65 20 70 61 67 65 20 69 6e 74 6f 0a 2a 2a 20 61  e page into.** a
e1960 6e 6f 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  nother..*/.stati
e1970 63 20 69 6e 74 20 72 65 70 61 72 65 6e 74 43 68  c int reparentCh
e1980 69 6c 64 50 61 67 65 73 28 4d 65 6d 50 61 67 65  ildPages(MemPage
e1990 20 2a 70 50 61 67 65 29 7b 0a 20 20 69 6e 74 20   *pPage){.  int 
e19a0 69 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  i;.  BtShared *p
e19b0 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b  Bt = pPage->pBt;
e19c0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
e19d0 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20 70 50  TE_OK;..  if( pP
e19e0 61 67 65 2d 3e 6c 65 61 66 20 29 20 72 65 74 75  age->leaf ) retu
e19f0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20  rn SQLITE_OK;.. 
e1a00 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 67   for(i=0; i<pPag
e1a10 65 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a  e->nCell; i++){.
e1a20 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 20 3d 20      u8 *pCell = 
e1a30 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20  findCell(pPage, 
e1a40 69 29 3b 0a 20 20 20 20 69 66 28 20 21 70 50 61  i);.    if( !pPa
e1a50 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20  ge->leaf ){.    
e1a60 20 20 72 63 20 3d 20 72 65 70 61 72 65 6e 74 50    rc = reparentP
e1a70 61 67 65 28 70 42 74 2c 20 67 65 74 34 62 79 74  age(pBt, get4byt
e1a80 65 28 70 43 65 6c 6c 29 2c 20 70 50 61 67 65 2c  e(pCell), pPage,
e1a90 20 69 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72   i);.      if( r
e1aa0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
e1ab0 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
e1ac0 20 20 7d 0a 20 20 69 66 28 20 21 70 50 61 67 65    }.  if( !pPage
e1ad0 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 72 63  ->leaf ){.    rc
e1ae0 20 3d 20 72 65 70 61 72 65 6e 74 50 61 67 65 28   = reparentPage(
e1af0 70 42 74 2c 20 67 65 74 34 62 79 74 65 28 26 70  pBt, get4byte(&p
e1b00 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67  Page->aData[pPag
e1b10 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29  e->hdrOffset+8])
e1b20 2c 20 0a 20 20 20 20 20 20 20 70 50 61 67 65 2c  , .       pPage,
e1b30 20 69 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e   i);.    pPage->
e1b40 69 64 78 53 68 69 66 74 20 3d 20 30 3b 0a 20 20  idxShift = 0;.  
e1b50 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
e1b60 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74  ../*.** Remove t
e1b70 68 65 20 69 2d 74 68 20 63 65 6c 6c 20 66 72 6f  he i-th cell fro
e1b80 6d 20 70 50 61 67 65 2e 20 20 54 68 69 73 20 72  m pPage.  This r
e1b90 6f 75 74 69 6e 65 20 65 66 66 65 63 74 73 20 70  outine effects p
e1ba0 50 61 67 65 20 6f 6e 6c 79 2e 0a 2a 2a 20 54 68  Page only..** Th
e1bb0 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69  e cell content i
e1bc0 73 20 6e 6f 74 20 66 72 65 65 64 20 6f 72 20 64  s not freed or d
e1bd0 65 61 6c 6c 6f 63 61 74 65 64 2e 20 20 49 74 20  eallocated.  It 
e1be0 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 0a  is assumed that.
e1bf0 2a 2a 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  ** the cell cont
e1c00 65 6e 74 20 68 61 73 20 62 65 65 6e 20 63 6f 70  ent has been cop
e1c10 69 65 64 20 73 6f 6d 65 70 6c 61 63 65 20 65 6c  ied someplace el
e1c20 73 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  se.  This routin
e1c30 65 20 6a 75 73 74 0a 2a 2a 20 72 65 6d 6f 76 65  e just.** remove
e1c40 73 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20  s the reference 
e1c50 74 6f 20 74 68 65 20 63 65 6c 6c 20 66 72 6f 6d  to the cell from
e1c60 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 22 73   pPage..**.** "s
e1c70 7a 22 20 6d 75 73 74 20 62 65 20 74 68 65 20 6e  z" must be the n
e1c80 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
e1c90 6e 20 74 68 65 20 63 65 6c 6c 2e 0a 2a 2f 0a 73  n the cell..*/.s
e1ca0 74 61 74 69 63 20 76 6f 69 64 20 64 72 6f 70 43  tatic void dropC
e1cb0 65 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61  ell(MemPage *pPa
e1cc0 67 65 2c 20 69 6e 74 20 69 64 78 2c 20 69 6e 74  ge, int idx, int
e1cd0 20 73 7a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20   sz){.  int i;  
e1ce0 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
e1cf0 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74  counter */.  int
e1d00 20 70 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20   pc;         /* 
e1d10 4f 66 66 73 65 74 20 74 6f 20 63 65 6c 6c 20 63  Offset to cell c
e1d20 6f 6e 74 65 6e 74 20 6f 66 20 63 65 6c 6c 20 62  ontent of cell b
e1d30 65 69 6e 67 20 64 65 6c 65 74 65 64 20 2a 2f 0a  eing deleted */.
e1d40 20 20 75 38 20 2a 64 61 74 61 3b 20 20 20 20 20    u8 *data;     
e1d50 20 20 2f 2a 20 70 50 61 67 65 2d 3e 61 44 61 74    /* pPage->aDat
e1d60 61 20 2a 2f 0a 20 20 75 38 20 2a 70 74 72 3b 20  a */.  u8 *ptr; 
e1d70 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74         /* Used t
e1d80 6f 20 6d 6f 76 65 20 62 79 74 65 73 20 61 72 6f  o move bytes aro
e1d90 75 6e 64 20 77 69 74 68 69 6e 20 64 61 74 61 5b  und within data[
e1da0 5d 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  ] */..  assert( 
e1db0 69 64 78 3e 3d 30 20 26 26 20 69 64 78 3c 70 50  idx>=0 && idx<pP
e1dc0 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20  age->nCell );.  
e1dd0 61 73 73 65 72 74 28 20 73 7a 3d 3d 63 65 6c 6c  assert( sz==cell
e1de0 53 69 7a 65 28 70 50 61 67 65 2c 20 69 64 78 29  Size(pPage, idx)
e1df0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
e1e00 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74  lite3PagerIswrit
e1e10 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62  eable(pPage->pDb
e1e20 50 61 67 65 29 20 29 3b 0a 20 20 64 61 74 61 20  Page) );.  data 
e1e30 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a  = pPage->aData;.
e1e40 20 20 70 74 72 20 3d 20 26 64 61 74 61 5b 70 50    ptr = &data[pP
e1e50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20  age->cellOffset 
e1e60 2b 20 32 2a 69 64 78 5d 3b 0a 20 20 70 63 20 3d  + 2*idx];.  pc =
e1e70 20 67 65 74 32 62 79 74 65 28 70 74 72 29 3b 0a   get2byte(ptr);.
e1e80 20 20 61 73 73 65 72 74 28 20 70 63 3e 31 30 20    assert( pc>10 
e1e90 26 26 20 70 63 2b 73 7a 3c 3d 70 50 61 67 65 2d  && pc+sz<=pPage-
e1ea0 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65  >pBt->usableSize
e1eb0 20 29 3b 0a 20 20 66 72 65 65 53 70 61 63 65 28   );.  freeSpace(
e1ec0 70 50 61 67 65 2c 20 70 63 2c 20 73 7a 29 3b 0a  pPage, pc, sz);.
e1ed0 20 20 66 6f 72 28 69 3d 69 64 78 2b 31 3b 20 69    for(i=idx+1; i
e1ee0 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20 69  <pPage->nCell; i
e1ef0 2b 2b 2c 20 70 74 72 2b 3d 32 29 7b 0a 20 20 20  ++, ptr+=2){.   
e1f00 20 70 74 72 5b 30 5d 20 3d 20 70 74 72 5b 32 5d   ptr[0] = ptr[2]
e1f10 3b 0a 20 20 20 20 70 74 72 5b 31 5d 20 3d 20 70  ;.    ptr[1] = p
e1f20 74 72 5b 33 5d 3b 0a 20 20 7d 0a 20 20 70 50 61  tr[3];.  }.  pPa
e1f30 67 65 2d 3e 6e 43 65 6c 6c 2d 2d 3b 0a 20 20 70  ge->nCell--;.  p
e1f40 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 50  ut2byte(&data[pP
e1f50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 33  age->hdrOffset+3
e1f60 5d 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29  ], pPage->nCell)
e1f70 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65  ;.  pPage->nFree
e1f80 20 2b 3d 20 32 3b 0a 20 20 70 50 61 67 65 2d 3e   += 2;.  pPage->
e1f90 69 64 78 53 68 69 66 74 20 3d 20 31 3b 0a 7d 0a  idxShift = 1;.}.
e1fa0 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20  ./*.** Insert a 
e1fb0 6e 65 77 20 63 65 6c 6c 20 6f 6e 20 70 50 61 67  new cell on pPag
e1fc0 65 20 61 74 20 63 65 6c 6c 20 69 6e 64 65 78 20  e at cell index 
e1fd0 22 69 22 2e 20 20 70 43 65 6c 6c 20 70 6f 69 6e  "i".  pCell poin
e1fe0 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63 6f 6e  ts to the.** con
e1ff0 74 65 6e 74 20 6f 66 20 74 68 65 20 63 65 6c 6c  tent of the cell
e2000 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63  ..**.** If the c
e2010 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 77 69 6c 6c  ell content will
e2020 20 66 69 74 20 6f 6e 20 74 68 65 20 70 61 67 65   fit on the page
e2030 2c 20 74 68 65 6e 20 70 75 74 20 69 74 20 74 68  , then put it th
e2040 65 72 65 2e 20 20 49 66 20 69 74 0a 2a 2a 20 77  ere.  If it.** w
e2050 69 6c 6c 20 6e 6f 74 20 66 69 74 2c 20 74 68 65  ill not fit, the
e2060 6e 20 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66  n make a copy of
e2070 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e   the cell conten
e2080 74 20 69 6e 74 6f 20 70 54 65 6d 70 20 69 66 0a  t into pTemp if.
e2090 2a 2a 20 70 54 65 6d 70 20 69 73 20 6e 6f 74 20  ** pTemp is not 
e20a0 6e 75 6c 6c 2e 20 20 52 65 67 61 72 64 6c 65 73  null.  Regardles
e20b0 73 20 6f 66 20 70 54 65 6d 70 2c 20 61 6c 6c 6f  s of pTemp, allo
e20c0 63 61 74 65 20 61 20 6e 65 77 20 65 6e 74 72 79  cate a new entry
e20d0 0a 2a 2a 20 69 6e 20 70 50 61 67 65 2d 3e 61 4f  .** in pPage->aO
e20e0 76 66 6c 5b 5d 20 61 6e 64 20 6d 61 6b 65 20 69  vfl[] and make i
e20f0 74 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 63  t point to the c
e2100 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 28 65 69 74  ell content (eit
e2110 68 65 72 0a 2a 2a 20 69 6e 20 70 54 65 6d 70 20  her.** in pTemp 
e2120 6f 72 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  or the original 
e2130 70 43 65 6c 6c 29 20 61 6e 64 20 61 6c 73 6f 20  pCell) and also 
e2140 72 65 63 6f 72 64 20 69 74 73 20 69 6e 64 65 78  record its index
e2150 2e 20 0a 2a 2a 20 41 6c 6c 6f 63 61 74 69 6e 67  . .** Allocating
e2160 20 61 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 20   a new entry in 
e2170 70 50 61 67 65 2d 3e 61 43 65 6c 6c 5b 5d 20 69  pPage->aCell[] i
e2180 6d 70 6c 69 65 73 20 74 68 61 74 20 0a 2a 2a 20  mplies that .** 
e2190 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77  pPage->nOverflow
e21a0 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e   is incremented.
e21b0 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 53 6b 69 70 20  .**.** If nSkip 
e21c0 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  is non-zero, the
e21d0 6e 20 64 6f 20 6e 6f 74 20 63 6f 70 79 20 74 68  n do not copy th
e21e0 65 20 66 69 72 73 74 20 6e 53 6b 69 70 20 62 79  e first nSkip by
e21f0 74 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 63 65  tes of the.** ce
e2200 6c 6c 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 77  ll. The caller w
e2210 69 6c 6c 20 6f 76 65 72 77 72 69 74 65 20 74 68  ill overwrite th
e2220 65 6d 20 61 66 74 65 72 20 74 68 69 73 20 66 75  em after this fu
e2230 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e 20  nction returns. 
e2240 49 66 0a 2a 2a 20 6e 53 6b 69 70 20 69 73 20 6e  If.** nSkip is n
e2250 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 70 43  on-zero, then pC
e2260 65 6c 6c 20 6d 61 79 20 6e 6f 74 20 70 6f 69 6e  ell may not poin
e2270 74 20 74 6f 20 61 6e 20 69 6e 76 61 6c 69 64 20  t to an invalid 
e2280 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20  memory location 
e2290 0a 2a 2a 20 28 62 75 74 20 70 43 65 6c 6c 2b 6e  .** (but pCell+n
e22a0 53 6b 69 70 20 69 73 20 61 6c 77 61 79 73 20 76  Skip is always v
e22b0 61 6c 69 64 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  alid)..*/.static
e22c0 20 69 6e 74 20 69 6e 73 65 72 74 43 65 6c 6c 28   int insertCell(
e22d0 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  .  MemPage *pPag
e22e0 65 2c 20 20 20 2f 2a 20 50 61 67 65 20 69 6e 74  e,   /* Page int
e22f0 6f 20 77 68 69 63 68 20 77 65 20 61 72 65 20 63  o which we are c
e2300 6f 70 79 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20  opying */.  int 
e2310 69 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  i,            /*
e2320 20 4e 65 77 20 63 65 6c 6c 20 62 65 63 6f 6d 65   New cell become
e2330 73 20 74 68 65 20 69 2d 74 68 20 63 65 6c 6c 20  s the i-th cell 
e2340 6f 66 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20  of the page */. 
e2350 20 75 38 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20   u8 *pCell,     
e2360 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66     /* Content of
e2370 20 74 68 65 20 6e 65 77 20 63 65 6c 6c 20 2a 2f   the new cell */
e2380 0a 20 20 69 6e 74 20 73 7a 2c 20 20 20 20 20 20  .  int sz,      
e2390 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f 66       /* Bytes of
e23a0 20 63 6f 6e 74 65 6e 74 20 69 6e 20 70 43 65 6c   content in pCel
e23b0 6c 20 2a 2f 0a 20 20 75 38 20 2a 70 54 65 6d 70  l */.  u8 *pTemp
e23c0 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70  ,        /* Temp
e23d0 20 73 74 6f 72 61 67 65 20 73 70 61 63 65 20 66   storage space f
e23e0 6f 72 20 70 43 65 6c 6c 2c 20 69 66 20 6e 65 65  or pCell, if nee
e23f0 64 65 64 20 2a 2f 0a 20 20 75 38 20 6e 53 6b 69  ded */.  u8 nSki
e2400 70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f  p          /* Do
e2410 20 6e 6f 74 20 77 72 69 74 65 20 74 68 65 20 66   not write the f
e2420 69 72 73 74 20 6e 53 6b 69 70 20 62 79 74 65 73  irst nSkip bytes
e2430 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a   of the cell */.
e2440 29 7b 0a 20 20 69 6e 74 20 69 64 78 3b 20 20 20  ){.  int idx;   
e2450 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20         /* Where 
e2460 74 6f 20 77 72 69 74 65 20 6e 65 77 20 63 65 6c  to write new cel
e2470 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 20 64 61 74  l content in dat
e2480 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20  a[] */.  int j; 
e2490 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
e24a0 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
e24b0 69 6e 74 20 74 6f 70 3b 20 20 20 20 20 20 20 20  int top;        
e24c0 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20    /* First byte 
e24d0 6f 66 20 63 6f 6e 74 65 6e 74 20 66 6f 72 20 61  of content for a
e24e0 6e 79 20 63 65 6c 6c 20 69 6e 20 64 61 74 61 5b  ny cell in data[
e24f0 5d 20 2a 2f 0a 20 20 69 6e 74 20 65 6e 64 3b 20  ] */.  int end; 
e2500 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73           /* Firs
e2510 74 20 62 79 74 65 20 70 61 73 74 20 74 68 65 20  t byte past the 
e2520 6c 61 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65  last cell pointe
e2530 72 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20  r in data[] */. 
e2540 20 69 6e 74 20 69 6e 73 3b 20 20 20 20 20 20 20   int ins;       
e2550 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 64     /* Index in d
e2560 61 74 61 5b 5d 20 77 68 65 72 65 20 6e 65 77 20  ata[] where new 
e2570 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 69 73 20  cell pointer is 
e2580 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 69 6e  inserted */.  in
e2590 74 20 68 64 72 3b 20 20 20 20 20 20 20 20 20 20  t hdr;          
e25a0 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 64  /* Offset into d
e25b0 61 74 61 5b 5d 20 6f 66 20 74 68 65 20 70 61 67  ata[] of the pag
e25c0 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e  e header */.  in
e25d0 74 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20 20  t cellOffset;   
e25e0 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 66 69  /* Address of fi
e25f0 72 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72  rst cell pointer
e2600 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20   in data[] */.  
e2610 75 38 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20  u8 *data;       
e2620 20 20 2f 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74    /* The content
e2630 20 6f 66 20 74 68 65 20 77 68 6f 6c 65 20 70 61   of the whole pa
e2640 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70 74 72 3b  ge */.  u8 *ptr;
e2650 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65            /* Use
e2660 64 20 66 6f 72 20 6d 6f 76 69 6e 67 20 69 6e 66  d for moving inf
e2670 6f 72 6d 61 74 69 6f 6e 20 61 72 6f 75 6e 64 20  ormation around 
e2680 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 0a 20 20  in data[] */..  
e2690 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20  assert( i>=0 && 
e26a0 69 3c 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b  i<=pPage->nCell+
e26b0 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77  pPage->nOverflow
e26c0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a   );.  assert( sz
e26d0 3d 3d 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50  ==cellSizePtr(pP
e26e0 61 67 65 2c 20 70 43 65 6c 6c 29 20 29 3b 0a 20  age, pCell) );. 
e26f0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
e2700 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65  PagerIswriteable
e2710 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
e2720 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d   );.  if( pPage-
e2730 3e 6e 4f 76 65 72 66 6c 6f 77 20 7c 7c 20 73 7a  >nOverflow || sz
e2740 2b 32 3e 70 50 61 67 65 2d 3e 6e 46 72 65 65 20  +2>pPage->nFree 
e2750 29 7b 0a 20 20 20 20 69 66 28 20 70 54 65 6d 70  ){.    if( pTemp
e2760 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79   ){.      memcpy
e2770 28 70 54 65 6d 70 2b 6e 53 6b 69 70 2c 20 70 43  (pTemp+nSkip, pC
e2780 65 6c 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e 53  ell+nSkip, sz-nS
e2790 6b 69 70 29 3b 0a 20 20 20 20 20 20 70 43 65 6c  kip);.      pCel
e27a0 6c 20 3d 20 70 54 65 6d 70 3b 0a 20 20 20 20 7d  l = pTemp;.    }
e27b0 0a 20 20 20 20 6a 20 3d 20 70 50 61 67 65 2d 3e  .    j = pPage->
e27c0 6e 4f 76 65 72 66 6c 6f 77 2b 2b 3b 0a 20 20 20  nOverflow++;.   
e27d0 20 61 73 73 65 72 74 28 20 6a 3c 73 69 7a 65 6f   assert( j<sizeo
e27e0 66 28 70 50 61 67 65 2d 3e 61 4f 76 66 6c 29 2f  f(pPage->aOvfl)/
e27f0 73 69 7a 65 6f 66 28 70 50 61 67 65 2d 3e 61 4f  sizeof(pPage->aO
e2800 76 66 6c 5b 30 5d 29 20 29 3b 0a 20 20 20 20 70  vfl[0]) );.    p
e2810 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 6a 5d 2e 70  Page->aOvfl[j].p
e2820 43 65 6c 6c 20 3d 20 70 43 65 6c 6c 3b 0a 20 20  Cell = pCell;.  
e2830 20 20 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 6a    pPage->aOvfl[j
e2840 5d 2e 69 64 78 20 3d 20 69 3b 0a 20 20 20 20 70  ].idx = i;.    p
e2850 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20 30 3b  Page->nFree = 0;
e2860 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 61  .  }else{.    da
e2870 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74  ta = pPage->aDat
e2880 61 3b 0a 20 20 20 20 68 64 72 20 3d 20 70 50 61  a;.    hdr = pPa
e2890 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20  ge->hdrOffset;. 
e28a0 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79 74     top = get2byt
e28b0 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b  e(&data[hdr+5]);
e28c0 0a 20 20 20 20 63 65 6c 6c 4f 66 66 73 65 74 20  .    cellOffset 
e28d0 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66  = pPage->cellOff
e28e0 73 65 74 3b 0a 20 20 20 20 65 6e 64 20 3d 20 63  set;.    end = c
e28f0 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70 50  ellOffset + 2*pP
e2900 61 67 65 2d 3e 6e 43 65 6c 6c 20 2b 20 32 3b 0a  age->nCell + 2;.
e2910 20 20 20 20 69 6e 73 20 3d 20 63 65 6c 6c 4f 66      ins = cellOf
e2920 66 73 65 74 20 2b 20 32 2a 69 3b 0a 20 20 20 20  fset + 2*i;.    
e2930 69 66 28 20 65 6e 64 20 3e 20 74 6f 70 20 2d 20  if( end > top - 
e2940 73 7a 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  sz ){.      int 
e2950 72 63 20 3d 20 64 65 66 72 61 67 6d 65 6e 74 50  rc = defragmentP
e2960 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20  age(pPage);.    
e2970 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
e2980 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
e2990 0a 20 20 20 20 20 20 74 6f 70 20 3d 20 67 65 74  .      top = get
e29a0 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b  2byte(&data[hdr+
e29b0 35 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  5]);.      asser
e29c0 74 28 20 65 6e 64 20 2b 20 73 7a 20 3c 3d 20 74  t( end + sz <= t
e29d0 6f 70 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  op );.    }.    
e29e0 69 64 78 20 3d 20 61 6c 6c 6f 63 61 74 65 53 70  idx = allocateSp
e29f0 61 63 65 28 70 50 61 67 65 2c 20 73 7a 29 3b 0a  ace(pPage, sz);.
e2a00 20 20 20 20 61 73 73 65 72 74 28 20 69 64 78 3e      assert( idx>
e2a10 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
e2a20 20 65 6e 64 20 3c 3d 20 67 65 74 32 62 79 74 65   end <= get2byte
e2a30 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 20 29  (&data[hdr+5]) )
e2a40 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 43 65  ;.    pPage->nCe
e2a50 6c 6c 2b 2b 3b 0a 20 20 20 20 70 50 61 67 65 2d  ll++;.    pPage-
e2a60 3e 6e 46 72 65 65 20 2d 3d 20 32 3b 0a 20 20 20  >nFree -= 2;.   
e2a70 20 6d 65 6d 63 70 79 28 26 64 61 74 61 5b 69 64   memcpy(&data[id
e2a80 78 2b 6e 53 6b 69 70 5d 2c 20 70 43 65 6c 6c 2b  x+nSkip], pCell+
e2a90 6e 53 6b 69 70 2c 20 73 7a 2d 6e 53 6b 69 70 29  nSkip, sz-nSkip)
e2aa0 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 65 6e 64 2d  ;.    for(j=end-
e2ab0 32 2c 20 70 74 72 3d 26 64 61 74 61 5b 6a 5d 3b  2, ptr=&data[j];
e2ac0 20 6a 3e 69 6e 73 3b 20 6a 2d 3d 32 2c 20 70 74   j>ins; j-=2, pt
e2ad0 72 2d 3d 32 29 7b 0a 20 20 20 20 20 20 70 74 72  r-=2){.      ptr
e2ae0 5b 30 5d 20 3d 20 70 74 72 5b 2d 32 5d 3b 0a 20  [0] = ptr[-2];. 
e2af0 20 20 20 20 20 70 74 72 5b 31 5d 20 3d 20 70 74       ptr[1] = pt
e2b00 72 5b 2d 31 5d 3b 0a 20 20 20 20 7d 0a 20 20 20  r[-1];.    }.   
e2b10 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b   put2byte(&data[
e2b20 69 6e 73 5d 2c 20 69 64 78 29 3b 0a 20 20 20 20  ins], idx);.    
e2b30 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  put2byte(&data[h
e2b40 64 72 2b 33 5d 2c 20 70 50 61 67 65 2d 3e 6e 43  dr+3], pPage->nC
e2b50 65 6c 6c 29 3b 0a 20 20 20 20 70 50 61 67 65 2d  ell);.    pPage-
e2b60 3e 69 64 78 53 68 69 66 74 20 3d 20 31 3b 0a 23  >idxShift = 1;.#
e2b70 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
e2b80 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
e2b90 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 42 74    if( pPage->pBt
e2ba0 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a  ->autoVacuum ){.
e2bb0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 65 6c        /* The cel
e2bc0 6c 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 61 20  l may contain a 
e2bd0 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 6f 76  pointer to an ov
e2be0 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 49 66 20  erflow page. If 
e2bf0 73 6f 2c 20 77 72 69 74 65 0a 20 20 20 20 20 20  so, write.      
e2c00 2a 2a 20 74 68 65 20 65 6e 74 72 79 20 66 6f 72  ** the entry for
e2c10 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61   the overflow pa
e2c20 67 65 20 69 6e 74 6f 20 74 68 65 20 70 6f 69 6e  ge into the poin
e2c30 74 65 72 20 6d 61 70 2e 0a 20 20 20 20 20 20 2a  ter map..      *
e2c40 2f 0a 20 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f  /.      CellInfo
e2c50 20 69 6e 66 6f 3b 0a 20 20 20 20 20 20 73 71 6c   info;.      sql
e2c60 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65  ite3BtreeParseCe
e2c70 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65  llPtr(pPage, pCe
e2c80 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20  ll, &info);.    
e2c90 20 20 61 73 73 65 72 74 28 20 28 69 6e 66 6f 2e    assert( (info.
e2ca0 6e 44 61 74 61 2b 28 70 50 61 67 65 2d 3e 69 6e  nData+(pPage->in
e2cb0 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e 6e 4b 65 79  tKey?0:info.nKey
e2cc0 29 29 3d 3d 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61  ))==info.nPayloa
e2cd0 64 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28  d );.      if( (
e2ce0 69 6e 66 6f 2e 6e 44 61 74 61 2b 28 70 50 61 67  info.nData+(pPag
e2cf0 65 2d 3e 69 6e 74 4b 65 79 3f 30 3a 69 6e 66 6f  e->intKey?0:info
e2d00 2e 6e 4b 65 79 29 29 3e 69 6e 66 6f 2e 6e 4c 6f  .nKey))>info.nLo
e2d10 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 50  cal ){.        P
e2d20 67 6e 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d 20 67  gno pgnoOvfl = g
e2d30 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69  et4byte(&pCell[i
e2d40 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b  nfo.iOverflow]);
e2d50 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 63 20  .        int rc 
e2d60 3d 20 70 74 72 6d 61 70 50 75 74 28 70 50 61 67  = ptrmapPut(pPag
e2d70 65 2d 3e 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c  e->pBt, pgnoOvfl
e2d80 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f  , PTRMAP_OVERFLO
e2d90 57 31 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29  W1, pPage->pgno)
e2da0 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
e2db0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
e2dc0 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d  turn rc;.      }
e2dd0 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
e2de0 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  }..  return SQLI
e2df0 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
e2e00 41 64 64 20 61 20 6c 69 73 74 20 6f 66 20 63 65  Add a list of ce
e2e10 6c 6c 73 20 74 6f 20 61 20 70 61 67 65 2e 20 20  lls to a page.  
e2e20 54 68 65 20 70 61 67 65 20 73 68 6f 75 6c 64 20  The page should 
e2e30 62 65 20 69 6e 69 74 69 61 6c 6c 79 20 65 6d 70  be initially emp
e2e40 74 79 2e 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 73  ty..** The cells
e2e50 20 61 72 65 20 67 75 61 72 61 6e 74 65 65 64 20   are guaranteed 
e2e60 74 6f 20 66 69 74 20 6f 6e 20 74 68 65 20 70 61  to fit on the pa
e2e70 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ge..*/.static vo
e2e80 69 64 20 61 73 73 65 6d 62 6c 65 50 61 67 65 28  id assemblePage(
e2e90 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  .  MemPage *pPag
e2ea0 65 2c 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65  e,   /* The page
e2eb0 20 74 6f 20 62 65 20 61 73 73 65 6d 62 6c 69 65   to be assemblie
e2ec0 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 65 6c 6c  d */.  int nCell
e2ed0 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ,        /* The 
e2ee0 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20  number of cells 
e2ef0 74 6f 20 61 64 64 20 74 6f 20 74 68 69 73 20 70  to add to this p
e2f00 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 2a 61 70  age */.  u8 **ap
e2f10 43 65 6c 6c 2c 20 20 20 20 20 20 2f 2a 20 50 6f  Cell,      /* Po
e2f20 69 6e 74 65 72 73 20 74 6f 20 63 65 6c 6c 20 62  inters to cell b
e2f30 6f 64 69 65 73 20 2a 2f 0a 20 20 69 6e 74 20 2a  odies */.  int *
e2f40 61 53 69 7a 65 20 20 20 20 20 20 20 20 2f 2a 20  aSize        /* 
e2f50 53 69 7a 65 73 20 6f 66 20 74 68 65 20 63 65 6c  Sizes of the cel
e2f60 6c 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  ls */.){.  int i
e2f70 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
e2f80 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  Loop counter */.
e2f90 20 20 69 6e 74 20 74 6f 74 61 6c 53 69 7a 65 3b    int totalSize;
e2fa0 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73 69 7a      /* Total siz
e2fb0 65 20 6f 66 20 61 6c 6c 20 63 65 6c 6c 73 20 2a  e of all cells *
e2fc0 2f 0a 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20  /.  int hdr;    
e2fd0 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
e2fe0 66 20 70 61 67 65 20 68 65 61 64 65 72 20 2a 2f  f page header */
e2ff0 0a 20 20 69 6e 74 20 63 65 6c 6c 70 74 72 3b 20  .  int cellptr; 
e3000 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20       /* Address 
e3010 6f 66 20 6e 65 78 74 20 63 65 6c 6c 20 70 6f 69  of next cell poi
e3020 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 63 65  nter */.  int ce
e3030 6c 6c 62 6f 64 79 3b 20 20 20 20 20 2f 2a 20 41  llbody;     /* A
e3040 64 64 72 65 73 73 20 6f 66 20 6e 65 78 74 20 63  ddress of next c
e3050 65 6c 6c 20 62 6f 64 79 20 2a 2f 0a 20 20 75 38  ell body */.  u8
e3060 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20 20 20   *data;         
e3070 2f 2a 20 44 61 74 61 20 66 6f 72 20 74 68 65 20  /* Data for the 
e3080 70 61 67 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72  page */..  asser
e3090 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66  t( pPage->nOverf
e30a0 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 74 6f 74 61  low==0 );.  tota
e30b0 6c 53 69 7a 65 20 3d 20 30 3b 0a 20 20 66 6f 72  lSize = 0;.  for
e30c0 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69  (i=0; i<nCell; i
e30d0 2b 2b 29 7b 0a 20 20 20 20 74 6f 74 61 6c 53 69  ++){.    totalSi
e30e0 7a 65 20 2b 3d 20 61 53 69 7a 65 5b 69 5d 3b 0a  ze += aSize[i];.
e30f0 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 74 6f    }.  assert( to
e3100 74 61 6c 53 69 7a 65 2b 32 2a 6e 43 65 6c 6c 3c  talSize+2*nCell<
e3110 3d 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 29 3b  =pPage->nFree );
e3120 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
e3130 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20  ->nCell==0 );.  
e3140 63 65 6c 6c 70 74 72 20 3d 20 70 50 61 67 65 2d  cellptr = pPage-
e3150 3e 63 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 64  >cellOffset;.  d
e3160 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61  ata = pPage->aDa
e3170 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50 61 67  ta;.  hdr = pPag
e3180 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20  e->hdrOffset;.  
e3190 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  put2byte(&data[h
e31a0 64 72 2b 33 5d 2c 20 6e 43 65 6c 6c 29 3b 0a 20  dr+3], nCell);. 
e31b0 20 69 66 28 20 6e 43 65 6c 6c 20 29 7b 0a 20 20   if( nCell ){.  
e31c0 20 20 63 65 6c 6c 62 6f 64 79 20 3d 20 61 6c 6c    cellbody = all
e31d0 6f 63 61 74 65 53 70 61 63 65 28 70 50 61 67 65  ocateSpace(pPage
e31e0 2c 20 74 6f 74 61 6c 53 69 7a 65 29 3b 0a 20 20  , totalSize);.  
e31f0 20 20 61 73 73 65 72 74 28 20 63 65 6c 6c 62 6f    assert( cellbo
e3200 64 79 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65  dy>0 );.    asse
e3210 72 74 28 20 70 50 61 67 65 2d 3e 6e 46 72 65 65  rt( pPage->nFree
e3220 20 3e 3d 20 32 2a 6e 43 65 6c 6c 20 29 3b 0a 20   >= 2*nCell );. 
e3230 20 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20     pPage->nFree 
e3240 2d 3d 20 32 2a 6e 43 65 6c 6c 3b 0a 20 20 20 20  -= 2*nCell;.    
e3250 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c  for(i=0; i<nCell
e3260 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 75  ; i++){.      pu
e3270 74 32 62 79 74 65 28 26 64 61 74 61 5b 63 65 6c  t2byte(&data[cel
e3280 6c 70 74 72 5d 2c 20 63 65 6c 6c 62 6f 64 79 29  lptr], cellbody)
e3290 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26  ;.      memcpy(&
e32a0 64 61 74 61 5b 63 65 6c 6c 62 6f 64 79 5d 2c 20  data[cellbody], 
e32b0 61 70 43 65 6c 6c 5b 69 5d 2c 20 61 53 69 7a 65  apCell[i], aSize
e32c0 5b 69 5d 29 3b 0a 20 20 20 20 20 20 63 65 6c 6c  [i]);.      cell
e32d0 70 74 72 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20  ptr += 2;.      
e32e0 63 65 6c 6c 62 6f 64 79 20 2b 3d 20 61 53 69 7a  cellbody += aSiz
e32f0 65 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  e[i];.    }.    
e3300 61 73 73 65 72 74 28 20 63 65 6c 6c 62 6f 64 79  assert( cellbody
e3310 3d 3d 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73  ==pPage->pBt->us
e3320 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 7d 0a  ableSize );.  }.
e3330 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d    pPage->nCell =
e3340 20 6e 43 65 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   nCell;.}../*.**
e3350 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 70   The following p
e3360 61 72 61 6d 65 74 65 72 73 20 64 65 74 65 72 6d  arameters determ
e3370 69 6e 65 20 68 6f 77 20 6d 61 6e 79 20 61 64 6a  ine how many adj
e3380 61 63 65 6e 74 20 70 61 67 65 73 20 67 65 74 20  acent pages get 
e3390 69 6e 76 6f 6c 76 65 64 0a 2a 2a 20 69 6e 20 61  involved.** in a
e33a0 20 62 61 6c 61 6e 63 69 6e 67 20 6f 70 65 72 61   balancing opera
e33b0 74 69 6f 6e 2e 20 20 4e 4e 20 69 73 20 74 68 65  tion.  NN is the
e33c0 20 6e 75 6d 62 65 72 20 6f 66 20 6e 65 69 67 68   number of neigh
e33d0 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 20 73  bors on either s
e33e0 69 64 65 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61  ide.** of the pa
e33f0 67 65 20 74 68 61 74 20 70 61 72 74 69 63 69 70  ge that particip
e3400 61 74 65 20 69 6e 20 74 68 65 20 62 61 6c 61 6e  ate in the balan
e3410 63 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 2e 20  cing operation. 
e3420 20 4e 42 20 69 73 20 74 68 65 0a 2a 2a 20 74 6f   NB is the.** to
e3430 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  tal number of pa
e3440 67 65 73 20 74 68 61 74 20 70 61 72 74 69 63 69  ges that partici
e3450 70 61 74 65 2c 20 69 6e 63 6c 75 64 69 6e 67 20  pate, including 
e3460 74 68 65 20 74 61 72 67 65 74 20 70 61 67 65 20  the target page 
e3470 61 6e 64 0a 2a 2a 20 4e 4e 20 6e 65 69 67 68 62  and.** NN neighb
e3480 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 20 73 69  ors on either si
e3490 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 69  de..**.** The mi
e34a0 6e 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 4e  nimum value of N
e34b0 4e 20 69 73 20 31 20 28 6f 66 20 63 6f 75 72 73  N is 1 (of cours
e34c0 65 29 2e 20 20 49 6e 63 72 65 61 73 69 6e 67 20  e).  Increasing 
e34d0 4e 4e 20 61 62 6f 76 65 20 31 0a 2a 2a 20 28 74  NN above 1.** (t
e34e0 6f 20 32 20 6f 72 20 33 29 20 67 69 76 65 73 20  o 2 or 3) gives 
e34f0 61 20 6d 6f 64 65 73 74 20 69 6d 70 72 6f 76 65  a modest improve
e3500 6d 65 6e 74 20 69 6e 20 53 45 4c 45 43 54 20 61  ment in SELECT a
e3510 6e 64 20 44 45 4c 45 54 45 20 70 65 72 66 6f 72  nd DELETE perfor
e3520 6d 61 6e 63 65 0a 2a 2a 20 69 6e 20 65 78 63 68  mance.** in exch
e3530 61 6e 67 65 20 66 6f 72 20 61 20 6c 61 72 67 65  ange for a large
e3540 72 20 64 65 67 72 61 64 61 74 69 6f 6e 20 69 6e  r degradation in
e3550 20 49 4e 53 45 52 54 20 61 6e 64 20 55 50 44 41   INSERT and UPDA
e3560 54 45 20 70 65 72 66 6f 72 6d 61 6e 63 65 2e 0a  TE performance..
e3570 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20  ** The value of 
e3580 4e 4e 20 61 70 70 65 61 72 73 20 74 6f 20 67 69  NN appears to gi
e3590 76 65 20 74 68 65 20 62 65 73 74 20 72 65 73 75  ve the best resu
e35a0 6c 74 73 20 6f 76 65 72 61 6c 6c 2e 0a 2a 2f 0a  lts overall..*/.
e35b0 23 64 65 66 69 6e 65 20 4e 4e 20 31 20 20 20 20  #define NN 1    
e35c0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
e35d0 65 72 20 6f 66 20 6e 65 69 67 68 62 6f 72 73 20  er of neighbors 
e35e0 6f 6e 20 65 69 74 68 65 72 20 73 69 64 65 20 6f  on either side o
e35f0 66 20 70 50 61 67 65 20 2a 2f 0a 23 64 65 66 69  f pPage */.#defi
e3600 6e 65 20 4e 42 20 28 4e 4e 2a 32 2b 31 29 20 20  ne NB (NN*2+1)  
e3610 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 70 61 67      /* Total pag
e3620 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 74  es involved in t
e3630 68 65 20 62 61 6c 61 6e 63 65 20 2a 2f 0a 0a 2f  he balance */../
e3640 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65  * Forward refere
e3650 6e 63 65 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e  nce */.static in
e3660 74 20 62 61 6c 61 6e 63 65 28 4d 65 6d 50 61 67  t balance(MemPag
e3670 65 2a 2c 20 69 6e 74 29 3b 0a 0a 23 69 66 6e 64  e*, int);..#ifnd
e3680 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 51  ef SQLITE_OMIT_Q
e3690 55 49 43 4b 42 41 4c 41 4e 43 45 0a 2f 2a 0a 2a  UICKBALANCE./*.*
e36a0 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f  * This version o
e36b0 66 20 62 61 6c 61 6e 63 65 28 29 20 68 61 6e 64  f balance() hand
e36c0 6c 65 73 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 73  les the common s
e36d0 70 65 63 69 61 6c 20 63 61 73 65 20 77 68 65 72  pecial case wher
e36e0 65 0a 2a 2a 20 61 20 6e 65 77 20 65 6e 74 72 79  e.** a new entry
e36f0 20 69 73 20 62 65 69 6e 67 20 69 6e 73 65 72 74   is being insert
e3700 65 64 20 6f 6e 20 74 68 65 20 65 78 74 72 65 6d  ed on the extrem
e3710 65 20 72 69 67 68 74 2d 65 6e 64 20 6f 66 20 74  e right-end of t
e3720 68 65 0a 2a 2a 20 74 72 65 65 2c 20 69 6e 20 6f  he.** tree, in o
e3730 74 68 65 72 20 77 6f 72 64 73 2c 20 77 68 65 6e  ther words, when
e3740 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20 77   the new entry w
e3750 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 6c  ill become the l
e3760 61 72 67 65 73 74 0a 2a 2a 20 65 6e 74 72 79 20  argest.** entry 
e3770 69 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a 0a  in the tree..**.
e3780 2a 2a 20 49 6e 73 74 65 61 64 20 6f 66 20 74 72  ** Instead of tr
e3790 79 69 6e 67 20 62 61 6c 61 6e 63 65 20 74 68 65  ying balance the
e37a0 20 33 20 72 69 67 68 74 2d 6d 6f 73 74 20 6c 65   3 right-most le
e37b0 61 66 20 70 61 67 65 73 2c 20 6a 75 73 74 20 61  af pages, just a
e37c0 64 64 0a 2a 2a 20 61 20 6e 65 77 20 70 61 67 65  dd.** a new page
e37d0 20 74 6f 20 74 68 65 20 72 69 67 68 74 2d 68 61   to the right-ha
e37e0 6e 64 20 73 69 64 65 20 61 6e 64 20 70 75 74 20  nd side and put 
e37f0 74 68 65 20 6f 6e 65 20 6e 65 77 20 65 6e 74 72  the one new entr
e3800 79 20 69 6e 0a 2a 2a 20 74 68 61 74 20 70 61 67  y in.** that pag
e3810 65 2e 20 20 54 68 69 73 20 6c 65 61 76 65 73 20  e.  This leaves 
e3820 74 68 65 20 72 69 67 68 74 20 73 69 64 65 20 6f  the right side o
e3830 66 20 74 68 65 20 74 72 65 65 20 73 6f 6d 65 77  f the tree somew
e3840 68 61 74 0a 2a 2a 20 75 6e 62 61 6c 61 6e 63 65  hat.** unbalance
e3850 64 2e 20 20 42 75 74 20 6f 64 64 73 20 61 72 65  d.  But odds are
e3860 20 74 68 61 74 20 77 65 20 77 69 6c 6c 20 62 65   that we will be
e3870 20 69 6e 73 65 72 74 69 6e 67 20 6e 65 77 20 65   inserting new e
e3880 6e 74 72 69 65 73 0a 2a 2a 20 61 74 20 74 68 65  ntries.** at the
e3890 20 65 6e 64 20 73 6f 6f 6e 20 61 66 74 65 72 77   end soon afterw
e38a0 61 72 64 73 20 73 6f 20 74 68 65 20 6e 65 61 72  ards so the near
e38b0 6c 79 20 65 6d 70 74 79 20 70 61 67 65 20 77 69  ly empty page wi
e38c0 6c 6c 20 71 75 69 63 6b 6c 79 0a 2a 2a 20 66 69  ll quickly.** fi
e38d0 6c 6c 20 75 70 2e 20 20 4f 6e 20 61 76 65 72 61  ll up.  On avera
e38e0 67 65 2e 0a 2a 2a 0a 2a 2a 20 70 50 61 67 65 20  ge..**.** pPage 
e38f0 69 73 20 74 68 65 20 6c 65 61 66 20 70 61 67 65  is the leaf page
e3900 20 77 68 69 63 68 20 69 73 20 74 68 65 20 72 69   which is the ri
e3910 67 68 74 2d 6d 6f 73 74 20 70 61 67 65 20 69 6e  ght-most page in
e3920 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a 20 70 50   the tree..** pP
e3930 61 72 65 6e 74 20 69 73 20 69 74 73 20 70 61 72  arent is its par
e3940 65 6e 74 2e 20 20 70 50 61 67 65 20 6d 75 73 74  ent.  pPage must
e3950 20 68 61 76 65 20 61 20 73 69 6e 67 6c 65 20 6f   have a single o
e3960 76 65 72 66 6c 6f 77 20 65 6e 74 72 79 0a 2a 2a  verflow entry.**
e3970 20 77 68 69 63 68 20 69 73 20 61 6c 73 6f 20 74   which is also t
e3980 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 65 6e  he right-most en
e3990 74 72 79 20 6f 6e 20 74 68 65 20 70 61 67 65 2e  try on the page.
e39a0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62  .*/.static int b
e39b0 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 4d 65 6d  alance_quick(Mem
e39c0 50 61 67 65 20 2a 70 50 61 67 65 2c 20 4d 65 6d  Page *pPage, Mem
e39d0 50 61 67 65 20 2a 70 50 61 72 65 6e 74 29 7b 0a  Page *pParent){.
e39e0 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 50    int rc;.  MemP
e39f0 61 67 65 20 2a 70 4e 65 77 3b 0a 20 20 50 67 6e  age *pNew;.  Pgn
e3a00 6f 20 70 67 6e 6f 4e 65 77 3b 0a 20 20 75 38 20  o pgnoNew;.  u8 
e3a10 2a 70 43 65 6c 6c 3b 0a 20 20 69 6e 74 20 73 7a  *pCell;.  int sz
e3a20 43 65 6c 6c 3b 0a 20 20 43 65 6c 6c 49 6e 66 6f  Cell;.  CellInfo
e3a30 20 69 6e 66 6f 3b 0a 20 20 42 74 53 68 61 72 65   info;.  BtShare
e3a40 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e  d *pBt = pPage->
e3a50 70 42 74 3b 0a 20 20 69 6e 74 20 70 61 72 65 6e  pBt;.  int paren
e3a60 74 49 64 78 20 3d 20 70 50 61 72 65 6e 74 2d 3e  tIdx = pParent->
e3a70 6e 43 65 6c 6c 3b 20 20 20 2f 2a 20 70 50 61 72  nCell;   /* pPar
e3a80 65 6e 74 20 6e 65 77 20 64 69 76 69 64 65 72 20  ent new divider 
e3a90 63 65 6c 6c 20 69 6e 64 65 78 20 2a 2f 0a 20 20  cell index */.  
e3aa0 69 6e 74 20 70 61 72 65 6e 74 53 69 7a 65 3b 20  int parentSize; 
e3ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e3ac0 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6e 65 77    /* Size of new
e3ad0 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 2a 2f   divider cell */
e3ae0 0a 20 20 75 38 20 70 61 72 65 6e 74 43 65 6c 6c  .  u8 parentCell
e3af0 5b 36 34 5d 3b 20 20 20 20 20 20 20 20 20 20 20  [64];           
e3b00 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 66 6f       /* Space fo
e3b10 72 20 74 68 65 20 6e 65 77 20 64 69 76 69 64 65  r the new divide
e3b20 72 20 63 65 6c 6c 20 2a 2f 0a 0a 20 20 2f 2a 20  r cell */..  /* 
e3b30 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70  Allocate a new p
e3b40 61 67 65 2e 20 49 6e 73 65 72 74 20 74 68 65 20  age. Insert the 
e3b50 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 66 72  overflow cell fr
e3b60 6f 6d 20 70 50 61 67 65 0a 20 20 2a 2a 20 69 6e  om pPage.  ** in
e3b70 74 6f 20 69 74 2e 20 54 68 65 6e 20 72 65 6d 6f  to it. Then remo
e3b80 76 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  ve the overflow 
e3b90 63 65 6c 6c 20 66 72 6f 6d 20 70 50 61 67 65 2e  cell from pPage.
e3ba0 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 61 6c 6c  .  */.  rc = all
e3bb0 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70  ocateBtreePage(p
e3bc0 42 74 2c 20 26 70 4e 65 77 2c 20 26 70 67 6e 6f  Bt, &pNew, &pgno
e3bd0 4e 65 77 2c 20 30 2c 20 30 29 3b 0a 20 20 69 66  New, 0, 0);.  if
e3be0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
e3bf0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
e3c00 3b 0a 20 20 7d 0a 20 20 70 43 65 6c 6c 20 3d 20  ;.  }.  pCell = 
e3c10 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 30 5d 2e  pPage->aOvfl[0].
e3c20 70 43 65 6c 6c 3b 0a 20 20 73 7a 43 65 6c 6c 20  pCell;.  szCell 
e3c30 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50  = cellSizePtr(pP
e3c40 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 7a  age, pCell);.  z
e3c50 65 72 6f 50 61 67 65 28 70 4e 65 77 2c 20 70 50  eroPage(pNew, pP
e3c60 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d 29 3b 0a  age->aData[0]);.
e3c70 20 20 61 73 73 65 6d 62 6c 65 50 61 67 65 28 70    assemblePage(p
e3c80 4e 65 77 2c 20 31 2c 20 26 70 43 65 6c 6c 2c 20  New, 1, &pCell, 
e3c90 26 73 7a 43 65 6c 6c 29 3b 0a 20 20 70 50 61 67  &szCell);.  pPag
e3ca0 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30  e->nOverflow = 0
e3cb0 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20  ;..  /* Set the 
e3cc0 70 61 72 65 6e 74 20 6f 66 20 74 68 65 20 6e 65  parent of the ne
e3cd0 77 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 70 61  wly allocated pa
e3ce0 67 65 20 74 6f 20 70 50 61 72 65 6e 74 2e 20 2a  ge to pParent. *
e3cf0 2f 0a 20 20 70 4e 65 77 2d 3e 70 50 61 72 65 6e  /.  pNew->pParen
e3d00 74 20 3d 20 70 50 61 72 65 6e 74 3b 0a 20 20 73  t = pParent;.  s
e3d10 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 28 70  qlite3PagerRef(p
e3d20 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29  Parent->pDbPage)
e3d30 3b 0a 0a 20 20 2f 2a 20 70 50 61 67 65 20 69 73  ;..  /* pPage is
e3d40 20 63 75 72 72 65 6e 74 6c 79 20 74 68 65 20 72   currently the r
e3d50 69 67 68 74 2d 63 68 69 6c 64 20 6f 66 20 70 50  ight-child of pP
e3d60 61 72 65 6e 74 2e 20 43 68 61 6e 67 65 20 74 68  arent. Change th
e3d70 69 73 0a 20 20 2a 2a 20 73 6f 20 74 68 61 74 20  is.  ** so that 
e3d80 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20  the right-child 
e3d90 69 73 20 74 68 65 20 6e 65 77 20 70 61 67 65 20  is the new page 
e3da0 61 6c 6c 6f 63 61 74 65 64 20 61 62 6f 76 65 20  allocated above 
e3db0 61 6e 64 0a 20 20 2a 2a 20 70 50 61 67 65 20 69  and.  ** pPage i
e3dc0 73 20 74 68 65 20 6e 65 78 74 2d 74 6f 2d 72 69  s the next-to-ri
e3dd0 67 68 74 20 63 68 69 6c 64 2e 20 0a 20 20 2a 2f  ght child. .  */
e3de0 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
e3df0 2d 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20 20 70  ->nCell>0 );.  p
e3e00 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28  Cell = findCell(
e3e10 70 50 61 67 65 2c 20 70 50 61 67 65 2d 3e 6e 43  pPage, pPage->nC
e3e20 65 6c 6c 2d 31 29 3b 0a 20 20 73 71 6c 69 74 65  ell-1);.  sqlite
e3e30 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50  3BtreeParseCellP
e3e40 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c  tr(pPage, pCell,
e3e50 20 26 69 6e 66 6f 29 3b 0a 20 20 72 63 20 3d 20   &info);.  rc = 
e3e60 66 69 6c 6c 49 6e 43 65 6c 6c 28 70 50 61 72 65  fillInCell(pPare
e3e70 6e 74 2c 20 70 61 72 65 6e 74 43 65 6c 6c 2c 20  nt, parentCell, 
e3e80 30 2c 20 69 6e 66 6f 2e 6e 4b 65 79 2c 20 30 2c  0, info.nKey, 0,
e3e90 20 30 2c 20 30 2c 20 26 70 61 72 65 6e 74 53 69   0, 0, &parentSi
e3ea0 7a 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  ze);.  if( rc!=S
e3eb0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
e3ec0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20  return rc;.  }. 
e3ed0 20 61 73 73 65 72 74 28 20 70 61 72 65 6e 74 53   assert( parentS
e3ee0 69 7a 65 3c 36 34 20 29 3b 0a 20 20 72 63 20 3d  ize<64 );.  rc =
e3ef0 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 72   insertCell(pPar
e3f00 65 6e 74 2c 20 70 61 72 65 6e 74 49 64 78 2c 20  ent, parentIdx, 
e3f10 70 61 72 65 6e 74 43 65 6c 6c 2c 20 70 61 72 65  parentCell, pare
e3f20 6e 74 53 69 7a 65 2c 20 30 2c 20 34 29 3b 0a 20  ntSize, 0, 4);. 
e3f30 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
e3f40 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OK ){.    return
e3f50 20 72 63 3b 0a 20 20 7d 0a 20 20 70 75 74 34 62   rc;.  }.  put4b
e3f60 79 74 65 28 66 69 6e 64 4f 76 65 72 66 6c 6f 77  yte(findOverflow
e3f70 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 70 61 72  Cell(pParent,par
e3f80 65 6e 74 49 64 78 29 2c 20 70 50 61 67 65 2d 3e  entIdx), pPage->
e3f90 70 67 6e 6f 29 3b 0a 20 20 70 75 74 34 62 79 74  pgno);.  put4byt
e3fa0 65 28 26 70 50 61 72 65 6e 74 2d 3e 61 44 61 74  e(&pParent->aDat
e3fb0 61 5b 70 50 61 72 65 6e 74 2d 3e 68 64 72 4f 66  a[pParent->hdrOf
e3fc0 66 73 65 74 2b 38 5d 2c 20 70 67 6e 6f 4e 65 77  fset+8], pgnoNew
e3fd0 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  );..#ifndef SQLI
e3fe0 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
e3ff0 55 4d 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20  UM.  /* If this 
e4000 69 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75  is an auto-vacuu
e4010 6d 20 64 61 74 61 62 61 73 65 2c 20 75 70 64 61  m database, upda
e4020 74 65 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d  te the pointer m
e4030 61 70 0a 20 20 2a 2a 20 77 69 74 68 20 65 6e 74  ap.  ** with ent
e4040 72 69 65 73 20 66 6f 72 20 74 68 65 20 6e 65 77  ries for the new
e4050 20 70 61 67 65 2c 20 61 6e 64 20 61 6e 79 20 70   page, and any p
e4060 6f 69 6e 74 65 72 20 66 72 6f 6d 20 74 68 65 20  ointer from the 
e4070 0a 20 20 2a 2a 20 63 65 6c 6c 20 6f 6e 20 74 68  .  ** cell on th
e4080 65 20 70 61 67 65 20 74 6f 20 61 6e 20 6f 76 65  e page to an ove
e4090 72 66 6c 6f 77 20 70 61 67 65 2e 0a 20 20 2a 2f  rflow page..  */
e40a0 0a 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f  .  if( pBt->auto
e40b0 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 72 63  Vacuum ){.    rc
e40c0 20 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42 74   = ptrmapPut(pBt
e40d0 2c 20 70 67 6e 6f 4e 65 77 2c 20 50 54 52 4d 41  , pgnoNew, PTRMA
e40e0 50 5f 42 54 52 45 45 2c 20 70 50 61 72 65 6e 74  P_BTREE, pParent
e40f0 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 69 66 28  ->pgno);.    if(
e4100 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
e4110 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72  {.      rc = ptr
e4120 6d 61 70 50 75 74 4f 76 66 6c 28 70 4e 65 77 2c  mapPutOvfl(pNew,
e4130 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69   0);.    }.    i
e4140 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
e4150 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73   ){.      releas
e4160 65 50 61 67 65 28 70 4e 65 77 29 3b 0a 20 20 20  ePage(pNew);.   
e4170 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
e4180 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a    }.  }.#endif..
e4190 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65    /* Release the
e41a0 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68   reference to th
e41b0 65 20 6e 65 77 20 70 61 67 65 20 61 6e 64 20 62  e new page and b
e41c0 61 6c 61 6e 63 65 20 74 68 65 20 70 61 72 65 6e  alance the paren
e41d0 74 20 70 61 67 65 2c 0a 20 20 2a 2a 20 69 6e 20  t page,.  ** in 
e41e0 63 61 73 65 20 74 68 65 20 64 69 76 69 64 65 72  case the divider
e41f0 20 63 65 6c 6c 20 69 6e 73 65 72 74 65 64 20 63   cell inserted c
e4200 61 75 73 65 64 20 69 74 20 74 6f 20 62 65 63 6f  aused it to beco
e4210 6d 65 20 6f 76 65 72 66 75 6c 6c 2e 0a 20 20 2a  me overfull..  *
e4220 2f 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28  /.  releasePage(
e4230 70 4e 65 77 29 3b 0a 20 20 72 65 74 75 72 6e 20  pNew);.  return 
e4240 62 61 6c 61 6e 63 65 28 70 50 61 72 65 6e 74 2c  balance(pParent,
e4250 20 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a   0);.}.#endif /*
e4260 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 51 55 49   SQLITE_OMIT_QUI
e4270 43 4b 42 41 4c 41 4e 43 45 20 2a 2f 0a 0a 2f 2a  CKBALANCE */../*
e4280 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
e4290 20 72 65 64 69 73 74 72 69 62 75 74 65 73 20 43   redistributes C
e42a0 65 6c 6c 73 20 6f 6e 20 70 50 61 67 65 20 61 6e  ells on pPage an
e42b0 64 20 75 70 20 74 6f 20 4e 4e 2a 32 20 73 69 62  d up to NN*2 sib
e42c0 6c 69 6e 67 73 0a 2a 2a 20 6f 66 20 70 50 61 67  lings.** of pPag
e42d0 65 20 73 6f 20 74 68 61 74 20 61 6c 6c 20 70 61  e so that all pa
e42e0 67 65 73 20 68 61 76 65 20 61 62 6f 75 74 20 74  ges have about t
e42f0 68 65 20 73 61 6d 65 20 61 6d 6f 75 6e 74 20 6f  he same amount o
e4300 66 20 66 72 65 65 20 73 70 61 63 65 2e 0a 2a 2a  f free space..**
e4310 20 55 73 75 61 6c 6c 79 20 4e 4e 20 73 69 62 6c   Usually NN sibl
e4320 69 6e 67 73 20 6f 6e 20 65 69 74 68 65 72 20 73  ings on either s
e4330 69 64 65 20 6f 66 20 70 50 61 67 65 20 69 73 20  ide of pPage is 
e4340 75 73 65 64 20 69 6e 20 74 68 65 20 62 61 6c 61  used in the bala
e4350 6e 63 69 6e 67 2c 0a 2a 2a 20 74 68 6f 75 67 68  ncing,.** though
e4360 20 6d 6f 72 65 20 73 69 62 6c 69 6e 67 73 20 6d   more siblings m
e4370 69 67 68 74 20 63 6f 6d 65 20 66 72 6f 6d 20 6f  ight come from o
e4380 6e 65 20 73 69 64 65 20 69 66 20 70 50 61 67 65  ne side if pPage
e4390 20 69 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a   is the first.**
e43a0 20 6f 72 20 6c 61 73 74 20 63 68 69 6c 64 20 6f   or last child o
e43b0 66 20 69 74 73 20 70 61 72 65 6e 74 2e 20 20 49  f its parent.  I
e43c0 66 20 70 50 61 67 65 20 68 61 73 20 66 65 77 65  f pPage has fewe
e43d0 72 20 74 68 61 6e 20 32 2a 4e 4e 20 73 69 62 6c  r than 2*NN sibl
e43e0 69 6e 67 73 0a 2a 2a 20 28 73 6f 6d 65 74 68 69  ings.** (somethi
e43f0 6e 67 20 77 68 69 63 68 20 63 61 6e 20 6f 6e 6c  ng which can onl
e4400 79 20 68 61 70 70 65 6e 20 69 66 20 70 50 61 67  y happen if pPag
e4410 65 20 69 73 20 74 68 65 20 72 6f 6f 74 20 70 61  e is the root pa
e4420 67 65 20 6f 72 20 61 20 0a 2a 2a 20 63 68 69 6c  ge or a .** chil
e4430 64 20 6f 66 20 72 6f 6f 74 29 20 74 68 65 6e 20  d of root) then 
e4440 61 6c 6c 20 61 76 61 69 6c 61 62 6c 65 20 73 69  all available si
e4450 62 6c 69 6e 67 73 20 70 61 72 74 69 63 69 70 61  blings participa
e4460 74 65 20 69 6e 20 74 68 65 20 62 61 6c 61 6e 63  te in the balanc
e4470 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e  ing..**.** The n
e4480 75 6d 62 65 72 20 6f 66 20 73 69 62 6c 69 6e 67  umber of sibling
e4490 73 20 6f 66 20 70 50 61 67 65 20 6d 69 67 68 74  s of pPage might
e44a0 20 62 65 20 69 6e 63 72 65 61 73 65 64 20 6f 72   be increased or
e44b0 20 64 65 63 72 65 61 73 65 64 20 62 79 20 6f 6e   decreased by on
e44c0 65 20 6f 72 0a 2a 2a 20 74 77 6f 20 69 6e 20 61  e or.** two in a
e44d0 6e 20 65 66 66 6f 72 74 20 74 6f 20 6b 65 65 70  n effort to keep
e44e0 20 70 61 67 65 73 20 6e 65 61 72 6c 79 20 66 75   pages nearly fu
e44f0 6c 6c 20 62 75 74 20 6e 6f 74 20 6f 76 65 72 20  ll but not over 
e4500 66 75 6c 6c 2e 20 54 68 65 20 72 6f 6f 74 20 70  full. The root p
e4510 61 67 65 0a 2a 2a 20 69 73 20 73 70 65 63 69 61  age.** is specia
e4520 6c 20 61 6e 64 20 69 73 20 61 6c 6c 6f 77 65 64  l and is allowed
e4530 20 74 6f 20 62 65 20 6e 65 61 72 6c 79 20 65 6d   to be nearly em
e4540 70 74 79 2e 20 49 66 20 70 50 61 67 65 20 69 73  pty. If pPage is
e4550 20 0a 2a 2a 20 74 68 65 20 72 6f 6f 74 20 70 61   .** the root pa
e4560 67 65 2c 20 74 68 65 6e 20 74 68 65 20 64 65 70  ge, then the dep
e4570 74 68 20 6f 66 20 74 68 65 20 74 72 65 65 20 6d  th of the tree m
e4580 69 67 68 74 20 62 65 20 69 6e 63 72 65 61 73 65  ight be increase
e4590 64 0a 2a 2a 20 6f 72 20 64 65 63 72 65 61 73 65  d.** or decrease
e45a0 64 20 62 79 20 6f 6e 65 2c 20 61 73 20 6e 65 63  d by one, as nec
e45b0 65 73 73 61 72 79 2c 20 74 6f 20 6b 65 65 70 20  essary, to keep 
e45c0 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 66 72  the root page fr
e45d0 6f 6d 20 62 65 69 6e 67 0a 2a 2a 20 6f 76 65 72  om being.** over
e45e0 66 75 6c 6c 20 6f 72 20 63 6f 6d 70 6c 65 74 65  full or complete
e45f0 6c 79 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a 20  ly empty..**.** 
e4600 4e 6f 74 65 20 74 68 61 74 20 77 68 65 6e 20 74  Note that when t
e4610 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
e4620 61 6c 6c 65 64 2c 20 73 6f 6d 65 20 6f 66 20 74  alled, some of t
e4630 68 65 20 43 65 6c 6c 73 20 6f 6e 20 70 50 61 67  he Cells on pPag
e4640 65 0a 2a 2a 20 6d 69 67 68 74 20 6e 6f 74 20 61  e.** might not a
e4650 63 74 75 61 6c 6c 79 20 62 65 20 73 74 6f 72 65  ctually be store
e4660 64 20 69 6e 20 70 50 61 67 65 2d 3e 61 44 61 74  d in pPage->aDat
e4670 61 5b 5d 2e 20 20 54 68 69 73 20 63 61 6e 20 68  a[].  This can h
e4680 61 70 70 65 6e 0a 2a 2a 20 69 66 20 74 68 65 20  appen.** if the 
e4690 70 61 67 65 20 69 73 20 6f 76 65 72 66 75 6c 6c  page is overfull
e46a0 2e 20 20 50 61 72 74 20 6f 66 20 74 68 65 20 6a  .  Part of the j
e46b0 6f 62 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69  ob of this routi
e46c0 6e 65 20 69 73 20 74 6f 0a 2a 2a 20 6d 61 6b 65  ne is to.** make
e46d0 20 73 75 72 65 20 61 6c 6c 20 43 65 6c 6c 73 20   sure all Cells 
e46e0 66 6f 72 20 70 50 61 67 65 20 6f 6e 63 65 20 61  for pPage once a
e46f0 67 61 69 6e 20 66 69 74 20 69 6e 20 70 50 61 67  gain fit in pPag
e4700 65 2d 3e 61 44 61 74 61 5b 5d 2e 0a 2a 2a 0a 2a  e->aData[]..**.*
e4710 2a 20 49 6e 20 74 68 65 20 63 6f 75 72 73 65 20  * In the course 
e4720 6f 66 20 62 61 6c 61 6e 63 69 6e 67 20 74 68 65  of balancing the
e4730 20 73 69 62 6c 69 6e 67 73 20 6f 66 20 70 50 61   siblings of pPa
e4740 67 65 2c 20 74 68 65 20 70 61 72 65 6e 74 20 6f  ge, the parent o
e4750 66 20 70 50 61 67 65 0a 2a 2a 20 6d 69 67 68 74  f pPage.** might
e4760 20 62 65 63 6f 6d 65 20 6f 76 65 72 66 75 6c 6c   become overfull
e4770 20 6f 72 20 75 6e 64 65 72 66 75 6c 6c 2e 20 20   or underfull.  
e4780 49 66 20 74 68 61 74 20 68 61 70 70 65 6e 73 2c  If that happens,
e4790 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   then this routi
e47a0 6e 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20  ne.** is called 
e47b0 72 65 63 75 72 73 69 76 65 6c 79 20 6f 6e 20 74  recursively on t
e47c0 68 65 20 70 61 72 65 6e 74 2e 0a 2a 2a 0a 2a 2a  he parent..**.**
e47d0 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   If this routine
e47e0 20 66 61 69 6c 73 20 66 6f 72 20 61 6e 79 20 72   fails for any r
e47f0 65 61 73 6f 6e 2c 20 69 74 20 6d 69 67 68 74 20  eason, it might 
e4800 6c 65 61 76 65 20 74 68 65 20 64 61 74 61 62 61  leave the databa
e4810 73 65 0a 2a 2a 20 69 6e 20 61 20 63 6f 72 72 75  se.** in a corru
e4820 70 74 65 64 20 73 74 61 74 65 2e 20 20 53 6f 20  pted state.  So 
e4830 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  if this routine 
e4840 66 61 69 6c 73 2c 20 74 68 65 20 64 61 74 61 62  fails, the datab
e4850 61 73 65 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65  ase should.** be
e4860 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2f   rolled back..*/
e4870 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61  .static int bala
e4880 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 4d 65 6d 50  nce_nonroot(MemP
e4890 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 4d  age *pPage){.  M
e48a0 65 6d 50 61 67 65 20 2a 70 50 61 72 65 6e 74 3b  emPage *pParent;
e48b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
e48c0 68 65 20 70 61 72 65 6e 74 20 6f 66 20 70 50 61  he parent of pPa
e48d0 67 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64  ge */.  BtShared
e48e0 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20 20 20   *pBt;          
e48f0 20 20 20 20 20 2f 2a 20 54 68 65 20 77 68 6f 6c       /* The whol
e4900 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  e database */.  
e4910 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 30 3b 20 20  int nCell = 0;  
e4920 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e4930 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20  Number of cells 
e4940 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 2a 2f 0a 20  in apCell[] */. 
e4950 20 69 6e 74 20 6e 4d 61 78 43 65 6c 6c 73 20 3d   int nMaxCells =
e4960 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a   0;           /*
e4970 20 41 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20   Allocated size 
e4980 6f 66 20 61 70 43 65 6c 6c 2c 20 73 7a 43 65 6c  of apCell, szCel
e4990 6c 2c 20 61 46 72 6f 6d 2e 20 2a 2f 0a 20 20 69  l, aFrom. */.  i
e49a0 6e 74 20 6e 4f 6c 64 3b 20 20 20 20 20 20 20 20  nt nOld;        
e49b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
e49c0 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
e49d0 6e 20 61 70 4f 6c 64 5b 5d 20 2a 2f 0a 20 20 69  n apOld[] */.  i
e49e0 6e 74 20 6e 4e 65 77 3b 20 20 20 20 20 20 20 20  nt nNew;        
e49f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
e4a00 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
e4a10 6e 20 61 70 4e 65 77 5b 5d 20 2a 2f 0a 20 20 69  n apNew[] */.  i
e4a20 6e 74 20 6e 44 69 76 3b 20 20 20 20 20 20 20 20  nt nDiv;        
e4a30 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
e4a40 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 69  umber of cells i
e4a50 6e 20 61 70 44 69 76 5b 5d 20 2a 2f 0a 20 20 69  n apDiv[] */.  i
e4a60 6e 74 20 69 2c 20 6a 2c 20 6b 3b 20 20 20 20 20  nt i, j, k;     
e4a70 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
e4a80 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a  oop counters */.
e4a90 20 20 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20    int idx;      
e4aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
e4ab0 2a 20 49 6e 64 65 78 20 6f 66 20 70 50 61 67 65  * Index of pPage
e4ac0 20 69 6e 20 70 50 61 72 65 6e 74 2d 3e 61 43 65   in pParent->aCe
e4ad0 6c 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 78  ll[] */.  int nx
e4ae0 44 69 76 3b 20 20 20 20 20 20 20 20 20 20 20 20  Div;            
e4af0 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 64         /* Next d
e4b00 69 76 69 64 65 72 20 73 6c 6f 74 20 69 6e 20 70  ivider slot in p
e4b10 50 61 72 65 6e 74 2d 3e 61 43 65 6c 6c 5b 5d 20  Parent->aCell[] 
e4b20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  */.  int rc;    
e4b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e4b40 20 20 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 20    /* The return 
e4b50 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6c 65  code */.  int le
e4b60 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b 20 20 20  afCorrection;   
e4b70 20 20 20 20 20 20 20 2f 2a 20 34 20 69 66 20 70         /* 4 if p
e4b80 50 61 67 65 20 69 73 20 61 20 6c 65 61 66 2e 20  Page is a leaf. 
e4b90 20 30 20 69 66 20 6e 6f 74 20 2a 2f 0a 20 20 69   0 if not */.  i
e4ba0 6e 74 20 6c 65 61 66 44 61 74 61 3b 20 20 20 20  nt leafData;    
e4bb0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
e4bc0 72 75 65 20 69 66 20 70 50 61 67 65 20 69 73 20  rue if pPage is 
e4bd0 61 20 6c 65 61 66 20 6f 66 20 61 20 4c 45 41 46  a leaf of a LEAF
e4be0 44 41 54 41 20 74 72 65 65 20 2a 2f 0a 20 20 69  DATA tree */.  i
e4bf0 6e 74 20 75 73 61 62 6c 65 53 70 61 63 65 3b 20  nt usableSpace; 
e4c00 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
e4c10 79 74 65 73 20 69 6e 20 70 50 61 67 65 20 62 65  ytes in pPage be
e4c20 79 6f 6e 64 20 74 68 65 20 68 65 61 64 65 72 20  yond the header 
e4c30 2a 2f 0a 20 20 69 6e 74 20 70 61 67 65 46 6c 61  */.  int pageFla
e4c40 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  gs;             
e4c50 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20 70 50    /* Value of pP
e4c60 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d 20 2a 2f  age->aData[0] */
e4c70 0a 20 20 69 6e 74 20 73 75 62 74 6f 74 61 6c 3b  .  int subtotal;
e4c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e4c90 2f 2a 20 53 75 62 74 6f 74 61 6c 20 6f 66 20 62  /* Subtotal of b
e4ca0 79 74 65 73 20 69 6e 20 63 65 6c 6c 73 20 6f 6e  ytes in cells on
e4cb0 20 6f 6e 65 20 70 61 67 65 20 2a 2f 0a 20 20 69   one page */.  i
e4cc0 6e 74 20 69 53 70 61 63 65 20 3d 20 30 3b 20 20  nt iSpace = 0;  
e4cd0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
e4ce0 69 72 73 74 20 75 6e 75 73 65 64 20 62 79 74 65  irst unused byte
e4cf0 20 6f 66 20 61 53 70 61 63 65 5b 5d 20 2a 2f 0a   of aSpace[] */.
e4d00 20 20 4d 65 6d 50 61 67 65 20 2a 61 70 4f 6c 64    MemPage *apOld
e4d10 5b 4e 42 5d 3b 20 20 20 20 20 20 20 20 20 20 2f  [NB];          /
e4d20 2a 20 70 50 61 67 65 20 61 6e 64 20 75 70 20 74  * pPage and up t
e4d30 6f 20 74 77 6f 20 73 69 62 6c 69 6e 67 73 20 2a  o two siblings *
e4d40 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 4f 6c 64  /.  Pgno pgnoOld
e4d50 5b 4e 42 5d 3b 20 20 20 20 20 20 20 20 20 20 20  [NB];           
e4d60 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 73   /* Page numbers
e4d70 20 66 6f 72 20 65 61 63 68 20 70 61 67 65 20 69   for each page i
e4d80 6e 20 61 70 4f 6c 64 5b 5d 20 2a 2f 0a 20 20 4d  n apOld[] */.  M
e4d90 65 6d 50 61 67 65 20 2a 61 70 43 6f 70 79 5b 4e  emPage *apCopy[N
e4da0 42 5d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 50  B];         /* P
e4db0 72 69 76 61 74 65 20 63 6f 70 69 65 73 20 6f 66  rivate copies of
e4dc0 20 61 70 4f 6c 64 5b 5d 20 70 61 67 65 73 20 2a   apOld[] pages *
e4dd0 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61 70 4e  /.  MemPage *apN
e4de0 65 77 5b 4e 42 2b 32 5d 3b 20 20 20 20 20 20 20  ew[NB+2];       
e4df0 20 2f 2a 20 70 50 61 67 65 20 61 6e 64 20 75 70   /* pPage and up
e4e00 20 74 6f 20 4e 42 20 73 69 62 6c 69 6e 67 73 20   to NB siblings 
e4e10 61 66 74 65 72 20 62 61 6c 61 6e 63 69 6e 67 20  after balancing 
e4e20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 4e 65  */.  Pgno pgnoNe
e4e30 77 5b 4e 42 2b 32 5d 3b 20 20 20 20 20 20 20 20  w[NB+2];        
e4e40 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72    /* Page number
e4e50 73 20 66 6f 72 20 65 61 63 68 20 70 61 67 65 20  s for each page 
e4e60 69 6e 20 61 70 4e 65 77 5b 5d 20 2a 2f 0a 20 20  in apNew[] */.  
e4e70 75 38 20 2a 61 70 44 69 76 5b 4e 42 5d 3b 20 20  u8 *apDiv[NB];  
e4e80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e4e90 44 69 76 69 64 65 72 20 63 65 6c 6c 73 20 69 6e  Divider cells in
e4ea0 20 70 50 61 72 65 6e 74 20 2a 2f 0a 20 20 69 6e   pParent */.  in
e4eb0 74 20 63 6e 74 4e 65 77 5b 4e 42 2b 32 5d 3b 20  t cntNew[NB+2]; 
e4ec0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
e4ed0 64 65 78 20 69 6e 20 61 43 65 6c 6c 5b 5d 20 6f  dex in aCell[] o
e4ee0 66 20 63 65 6c 6c 20 61 66 74 65 72 20 69 2d 74  f cell after i-t
e4ef0 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20  h page */.  int 
e4f00 73 7a 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20 20 20  szNew[NB+2];    
e4f10 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 62           /* Comb
e4f20 69 6e 65 64 20 73 69 7a 65 20 6f 66 20 63 65 6c  ined size of cel
e4f30 6c 73 20 70 6c 61 63 65 20 6f 6e 20 69 2d 74 68  ls place on i-th
e4f40 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 2a   page */.  u8 **
e4f50 61 70 43 65 6c 6c 20 3d 20 30 3b 20 20 20 20 20  apCell = 0;     
e4f60 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 63          /* All c
e4f70 65 6c 6c 73 20 62 65 67 69 6e 20 62 61 6c 61 6e  ells begin balan
e4f80 63 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 73 7a  ced */.  int *sz
e4f90 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20  Cell;           
e4fa0 20 20 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 73        /* Local s
e4fb0 69 7a 65 20 6f 66 20 61 6c 6c 20 63 65 6c 6c 73  ize of all cells
e4fc0 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 2a 2f 0a   in apCell[] */.
e4fd0 20 20 75 38 20 2a 61 43 6f 70 79 5b 4e 42 5d 3b    u8 *aCopy[NB];
e4fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
e4ff0 2a 20 53 70 61 63 65 20 66 6f 72 20 68 6f 6c 64  * Space for hold
e5000 69 6e 67 20 64 61 74 61 20 6f 66 20 61 70 43 6f  ing data of apCo
e5010 70 79 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 53  py[] */.  u8 *aS
e5020 70 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 20  pace;           
e5030 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20         /* Space 
e5040 74 6f 20 68 6f 6c 64 20 63 6f 70 69 65 73 20 6f  to hold copies o
e5050 66 20 64 69 76 69 64 65 72 73 20 63 65 6c 6c 73  f dividers cells
e5060 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   */.#ifndef SQLI
e5070 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
e5080 55 4d 0a 20 20 75 38 20 2a 61 46 72 6f 6d 20 3d  UM.  u8 *aFrom =
e5090 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a   0;.#endif..  /*
e50a0 20 0a 20 20 2a 2a 20 46 69 6e 64 20 74 68 65 20   .  ** Find the 
e50b0 70 61 72 65 6e 74 20 70 61 67 65 2e 0a 20 20 2a  parent page..  *
e50c0 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  /.  assert( pPag
e50d0 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 61  e->isInit );.  a
e50e0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
e50f0 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
e5100 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29  Page->pDbPage) )
e5110 3b 0a 20 20 70 42 74 20 3d 20 70 50 61 67 65 2d  ;.  pBt = pPage-
e5120 3e 70 42 74 3b 0a 20 20 70 50 61 72 65 6e 74 20  >pBt;.  pParent 
e5130 3d 20 70 50 61 67 65 2d 3e 70 50 61 72 65 6e 74  = pPage->pParent
e5140 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72  ;.  assert( pPar
e5150 65 6e 74 20 29 3b 0a 20 20 69 66 28 20 53 51 4c  ent );.  if( SQL
e5160 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71  ITE_OK!=(rc = sq
e5170 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
e5180 70 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65  pParent->pDbPage
e5190 29 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  )) ){.    return
e51a0 20 72 63 3b 0a 20 20 7d 0a 20 20 54 52 41 43 45   rc;.  }.  TRACE
e51b0 28 28 22 42 41 4c 41 4e 43 45 3a 20 62 65 67 69  (("BALANCE: begi
e51c0 6e 20 70 61 67 65 20 25 64 20 63 68 69 6c 64 20  n page %d child 
e51d0 6f 66 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 2d  of %d\n", pPage-
e51e0 3e 70 67 6e 6f 2c 20 70 50 61 72 65 6e 74 2d 3e  >pgno, pParent->
e51f0 70 67 6e 6f 29 29 3b 0a 0a 23 69 66 6e 64 65 66  pgno));..#ifndef
e5200 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 51 55 49   SQLITE_OMIT_QUI
e5210 43 4b 42 41 4c 41 4e 43 45 0a 20 20 2f 2a 0a 20  CKBALANCE.  /*. 
e5220 20 2a 2a 20 41 20 73 70 65 63 69 61 6c 20 63 61   ** A special ca
e5230 73 65 3a 20 20 49 66 20 61 20 6e 65 77 20 65 6e  se:  If a new en
e5240 74 72 79 20 68 61 73 20 6a 75 73 74 20 62 65 65  try has just bee
e5250 6e 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20  n inserted into 
e5260 61 0a 20 20 2a 2a 20 74 61 62 6c 65 20 28 74 68  a.  ** table (th
e5270 61 74 20 69 73 2c 20 61 20 62 74 72 65 65 20 77  at is, a btree w
e5280 69 74 68 20 69 6e 74 65 67 65 72 20 6b 65 79 73  ith integer keys
e5290 20 61 6e 64 20 61 6c 6c 20 64 61 74 61 20 61 74   and all data at
e52a0 20 74 68 65 20 6c 65 61 76 65 73 29 0a 20 20 2a   the leaves).  *
e52b0 2a 20 61 6e 64 20 74 68 65 20 6e 65 77 20 65 6e  * and the new en
e52c0 74 72 79 20 69 73 20 74 68 65 20 72 69 67 68 74  try is the right
e52d0 2d 6d 6f 73 74 20 65 6e 74 72 79 20 69 6e 20 74  -most entry in t
e52e0 68 65 20 74 72 65 65 20 28 69 74 20 68 61 73 20  he tree (it has 
e52f0 74 68 65 0a 20 20 2a 2a 20 6c 61 72 67 65 73 74  the.  ** largest
e5300 20 6b 65 79 29 20 74 68 65 6e 20 75 73 65 20 74   key) then use t
e5310 68 65 20 73 70 65 63 69 61 6c 20 62 61 6c 61 6e  he special balan
e5320 63 65 5f 71 75 69 63 6b 28 29 20 72 6f 75 74 69  ce_quick() routi
e5330 6e 65 20 66 6f 72 0a 20 20 2a 2a 20 62 61 6c 61  ne for.  ** bala
e5340 6e 63 69 6e 67 2e 20 20 62 61 6c 61 6e 63 65 5f  ncing.  balance_
e5350 71 75 69 63 6b 28 29 20 69 73 20 6d 75 63 68 20  quick() is much 
e5360 66 61 73 74 65 72 20 61 6e 64 20 72 65 73 75 6c  faster and resul
e5370 74 73 20 69 6e 20 61 20 74 69 67 68 74 65 72 0a  ts in a tighter.
e5380 20 20 2a 2a 20 70 61 63 6b 69 6e 67 20 6f 66 20    ** packing of 
e5390 64 61 74 61 20 69 6e 20 74 68 65 20 63 6f 6d 6d  data in the comm
e53a0 6f 6e 20 63 61 73 65 2e 0a 20 20 2a 2f 0a 20 20  on case..  */.  
e53b0 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20  if( pPage->leaf 
e53c0 26 26 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e  &&.      pPage->
e53d0 69 6e 74 4b 65 79 20 26 26 0a 20 20 20 20 20 20  intKey &&.      
e53e0 70 50 61 67 65 2d 3e 6c 65 61 66 44 61 74 61 20  pPage->leafData 
e53f0 26 26 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e  &&.      pPage->
e5400 6e 4f 76 65 72 66 6c 6f 77 3d 3d 31 20 26 26 0a  nOverflow==1 &&.
e5410 20 20 20 20 20 20 70 50 61 67 65 2d 3e 61 4f 76        pPage->aOv
e5420 66 6c 5b 30 5d 2e 69 64 78 3d 3d 70 50 61 67 65  fl[0].idx==pPage
e5430 2d 3e 6e 43 65 6c 6c 20 26 26 0a 20 20 20 20 20  ->nCell &&.     
e5440 20 70 50 61 67 65 2d 3e 70 50 61 72 65 6e 74 2d   pPage->pParent-
e5450 3e 70 67 6e 6f 21 3d 31 20 26 26 0a 20 20 20 20  >pgno!=1 &&.    
e5460 20 20 67 65 74 34 62 79 74 65 28 26 70 50 61 72    get4byte(&pPar
e5470 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65  ent->aData[pPare
e5480 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d  nt->hdrOffset+8]
e5490 29 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 0a 20  )==pPage->pgno. 
e54a0 20 29 7b 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a   ){.    /*.    *
e54b0 2a 20 54 4f 44 4f 3a 20 43 68 65 63 6b 20 74 68  * TODO: Check th
e54c0 65 20 73 69 62 6c 69 6e 67 73 20 74 6f 20 74 68  e siblings to th
e54d0 65 20 6c 65 66 74 20 6f 66 20 70 50 61 67 65 2e  e left of pPage.
e54e0 20 49 74 20 6d 61 79 20 62 65 20 74 68 61 74 0a   It may be that.
e54f0 20 20 20 20 2a 2a 20 74 68 65 79 20 61 72 65 20      ** they are 
e5500 6e 6f 74 20 66 75 6c 6c 20 61 6e 64 20 6e 6f 20  not full and no 
e5510 6e 65 77 20 70 61 67 65 20 69 73 20 72 65 71 75  new page is requ
e5520 69 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ired..    */.   
e5530 20 72 65 74 75 72 6e 20 62 61 6c 61 6e 63 65 5f   return balance_
e5540 71 75 69 63 6b 28 70 50 61 67 65 2c 20 70 50 61  quick(pPage, pPa
e5550 72 65 6e 74 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  rent);.  }.#endi
e5560 66 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46 69 6e  f..  /*.  ** Fin
e5570 64 20 74 68 65 20 63 65 6c 6c 20 69 6e 20 74 68  d the cell in th
e5580 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 77 68  e parent page wh
e5590 6f 73 65 20 6c 65 66 74 20 63 68 69 6c 64 20 70  ose left child p
e55a0 6f 69 6e 74 73 20 62 61 63 6b 0a 20 20 2a 2a 20  oints back.  ** 
e55b0 74 6f 20 70 50 61 67 65 2e 20 20 54 68 65 20 22  to pPage.  The "
e55c0 69 64 78 22 20 76 61 72 69 61 62 6c 65 20 69 73  idx" variable is
e55d0 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   the index of th
e55e0 61 74 20 63 65 6c 6c 2e 20 20 49 66 20 70 50 61  at cell.  If pPa
e55f0 67 65 0a 20 20 2a 2a 20 69 73 20 74 68 65 20 72  ge.  ** is the r
e5600 69 67 68 74 6d 6f 73 74 20 63 68 69 6c 64 20 6f  ightmost child o
e5610 66 20 70 50 61 72 65 6e 74 20 74 68 65 6e 20 73  f pParent then s
e5620 65 74 20 69 64 78 20 74 6f 20 70 50 61 72 65 6e  et idx to pParen
e5630 74 2d 3e 6e 43 65 6c 6c 20 0a 20 20 2a 2f 0a 20  t->nCell .  */. 
e5640 20 69 66 28 20 70 50 61 72 65 6e 74 2d 3e 69 64   if( pParent->id
e5650 78 53 68 69 66 74 20 29 7b 0a 20 20 20 20 50 67  xShift ){.    Pg
e5660 6e 6f 20 70 67 6e 6f 3b 0a 20 20 20 20 70 67 6e  no pgno;.    pgn
e5670 6f 20 3d 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3b  o = pPage->pgno;
e5680 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 67 6e  .    assert( pgn
e5690 6f 3d 3d 73 71 6c 69 74 65 33 50 61 67 65 72 50  o==sqlite3PagerP
e56a0 61 67 65 6e 75 6d 62 65 72 28 70 50 61 67 65 2d  agenumber(pPage-
e56b0 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 20  >pDbPage) );.   
e56c0 20 66 6f 72 28 69 64 78 3d 30 3b 20 69 64 78 3c   for(idx=0; idx<
e56d0 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3b 20  pParent->nCell; 
e56e0 69 64 78 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  idx++){.      if
e56f0 28 20 67 65 74 34 62 79 74 65 28 66 69 6e 64 43  ( get4byte(findC
e5700 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 69 64 78  ell(pParent, idx
e5710 29 29 3d 3d 70 67 6e 6f 20 29 7b 0a 20 20 20 20  ))==pgno ){.    
e5720 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
e5730 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73   }.    }.    ass
e5740 65 72 74 28 20 69 64 78 3c 70 50 61 72 65 6e 74  ert( idx<pParent
e5750 2d 3e 6e 43 65 6c 6c 0a 20 20 20 20 20 20 20 20  ->nCell.        
e5760 20 20 20 20 20 7c 7c 20 67 65 74 34 62 79 74 65       || get4byte
e5770 28 26 70 50 61 72 65 6e 74 2d 3e 61 44 61 74 61  (&pParent->aData
e5780 5b 70 50 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66  [pParent->hdrOff
e5790 73 65 74 2b 38 5d 29 3d 3d 70 67 6e 6f 20 29 3b  set+8])==pgno );
e57a0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 64  .  }else{.    id
e57b0 78 20 3d 20 70 50 61 67 65 2d 3e 69 64 78 50 61  x = pPage->idxPa
e57c0 72 65 6e 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a  rent;.  }..  /*.
e57d0 20 20 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20    ** Initialize 
e57e0 76 61 72 69 61 62 6c 65 73 20 73 6f 20 74 68 61  variables so tha
e57f0 74 20 69 74 20 77 69 6c 6c 20 62 65 20 73 61 66  t it will be saf
e5800 65 20 74 6f 20 6a 75 6d 70 0a 20 20 2a 2a 20 64  e to jump.  ** d
e5810 69 72 65 63 74 6c 79 20 74 6f 20 62 61 6c 61 6e  irectly to balan
e5820 63 65 5f 63 6c 65 61 6e 75 70 20 61 74 20 61 6e  ce_cleanup at an
e5830 79 20 6d 6f 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20  y moment..  */. 
e5840 20 6e 4f 6c 64 20 3d 20 6e 4e 65 77 20 3d 20 30   nOld = nNew = 0
e5850 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72  ;.  sqlite3Pager
e5860 52 65 66 28 70 50 61 72 65 6e 74 2d 3e 70 44 62  Ref(pParent->pDb
e5870 50 61 67 65 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a  Page);..  /*.  *
e5880 2a 20 46 69 6e 64 20 73 69 62 6c 69 6e 67 20 70  * Find sibling p
e5890 61 67 65 73 20 74 6f 20 70 50 61 67 65 20 61 6e  ages to pPage an
e58a0 64 20 74 68 65 20 63 65 6c 6c 73 20 69 6e 20 70  d the cells in p
e58b0 50 61 72 65 6e 74 20 74 68 61 74 20 64 69 76 69  Parent that divi
e58c0 64 65 0a 20 20 2a 2a 20 74 68 65 20 73 69 62 6c  de.  ** the sibl
e58d0 69 6e 67 73 2e 20 20 41 6e 20 61 74 74 65 6d 70  ings.  An attemp
e58e0 74 20 69 73 20 6d 61 64 65 20 74 6f 20 66 69 6e  t is made to fin
e58f0 64 20 4e 4e 20 73 69 62 6c 69 6e 67 73 20 6f 6e  d NN siblings on
e5900 20 65 69 74 68 65 72 0a 20 20 2a 2a 20 73 69 64   either.  ** sid
e5910 65 20 6f 66 20 70 50 61 67 65 2e 20 20 4d 6f 72  e of pPage.  Mor
e5920 65 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 74  e siblings are t
e5930 61 6b 65 6e 20 66 72 6f 6d 20 6f 6e 65 20 73 69  aken from one si
e5940 64 65 2c 20 68 6f 77 65 76 65 72 2c 20 69 66 0a  de, however, if.
e5950 20 20 2a 2a 20 70 50 61 67 65 20 74 68 65 72 65    ** pPage there
e5960 20 61 72 65 20 66 65 77 65 72 20 74 68 61 6e 20   are fewer than 
e5970 4e 4e 20 73 69 62 6c 69 6e 67 73 20 6f 6e 20 74  NN siblings on t
e5980 68 65 20 6f 74 68 65 72 20 73 69 64 65 2e 20 20  he other side.  
e5990 49 66 20 70 50 61 72 65 6e 74 0a 20 20 2a 2a 20  If pParent.  ** 
e59a0 68 61 73 20 4e 42 20 6f 72 20 66 65 77 65 72 20  has NB or fewer 
e59b0 63 68 69 6c 64 72 65 6e 20 74 68 65 6e 20 61 6c  children then al
e59c0 6c 20 63 68 69 6c 64 72 65 6e 20 6f 66 20 70 50  l children of pP
e59d0 61 72 65 6e 74 20 61 72 65 20 74 61 6b 65 6e 2e  arent are taken.
e59e0 0a 20 20 2a 2f 0a 20 20 6e 78 44 69 76 20 3d 20  .  */.  nxDiv = 
e59f0 69 64 78 20 2d 20 4e 4e 3b 0a 20 20 69 66 28 20  idx - NN;.  if( 
e5a00 6e 78 44 69 76 20 2b 20 4e 42 20 3e 20 70 50 61  nxDiv + NB > pPa
e5a10 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20  rent->nCell ){. 
e5a20 20 20 20 6e 78 44 69 76 20 3d 20 70 50 61 72 65     nxDiv = pPare
e5a30 6e 74 2d 3e 6e 43 65 6c 6c 20 2d 20 4e 42 20 2b  nt->nCell - NB +
e5a40 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 78   1;.  }.  if( nx
e5a50 44 69 76 3c 30 20 29 7b 0a 20 20 20 20 6e 78 44  Div<0 ){.    nxD
e5a60 69 76 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 6e 44  iv = 0;.  }.  nD
e5a70 69 76 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d  iv = 0;.  for(i=
e5a80 30 2c 20 6b 3d 6e 78 44 69 76 3b 20 69 3c 4e 42  0, k=nxDiv; i<NB
e5a90 3b 20 69 2b 2b 2c 20 6b 2b 2b 29 7b 0a 20 20 20  ; i++, k++){.   
e5aa0 20 69 66 28 20 6b 3c 70 50 61 72 65 6e 74 2d 3e   if( k<pParent->
e5ab0 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 61  nCell ){.      a
e5ac0 70 44 69 76 5b 69 5d 20 3d 20 66 69 6e 64 43 65  pDiv[i] = findCe
e5ad0 6c 6c 28 70 50 61 72 65 6e 74 2c 20 6b 29 3b 0a  ll(pParent, k);.
e5ae0 20 20 20 20 20 20 6e 44 69 76 2b 2b 3b 0a 20 20        nDiv++;.  
e5af0 20 20 20 20 61 73 73 65 72 74 28 20 21 70 50 61      assert( !pPa
e5b00 72 65 6e 74 2d 3e 6c 65 61 66 20 29 3b 0a 20 20  rent->leaf );.  
e5b10 20 20 20 20 70 67 6e 6f 4f 6c 64 5b 69 5d 20 3d      pgnoOld[i] =
e5b20 20 67 65 74 34 62 79 74 65 28 61 70 44 69 76 5b   get4byte(apDiv[
e5b30 69 5d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  i]);.    }else i
e5b40 66 28 20 6b 3d 3d 70 50 61 72 65 6e 74 2d 3e 6e  f( k==pParent->n
e5b50 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 70 67  Cell ){.      pg
e5b60 6e 6f 4f 6c 64 5b 69 5d 20 3d 20 67 65 74 34 62  noOld[i] = get4b
e5b70 79 74 65 28 26 70 50 61 72 65 6e 74 2d 3e 61 44  yte(&pParent->aD
e5b80 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68 64 72  ata[pParent->hdr
e5b90 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20  Offset+8]);.    
e5ba0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 62 72 65  }else{.      bre
e5bb0 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63  ak;.    }.    rc
e5bc0 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67   = getAndInitPag
e5bd0 65 28 70 42 74 2c 20 70 67 6e 6f 4f 6c 64 5b 69  e(pBt, pgnoOld[i
e5be0 5d 2c 20 26 61 70 4f 6c 64 5b 69 5d 2c 20 70 50  ], &apOld[i], pP
e5bf0 61 72 65 6e 74 29 3b 0a 20 20 20 20 69 66 28 20  arent);.    if( 
e5c00 72 63 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63  rc ) goto balanc
e5c10 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 61  e_cleanup;.    a
e5c20 70 4f 6c 64 5b 69 5d 2d 3e 69 64 78 50 61 72 65  pOld[i]->idxPare
e5c30 6e 74 20 3d 20 6b 3b 0a 20 20 20 20 61 70 43 6f  nt = k;.    apCo
e5c40 70 79 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 61  py[i] = 0;.    a
e5c50 73 73 65 72 74 28 20 69 3d 3d 6e 4f 6c 64 20 29  ssert( i==nOld )
e5c60 3b 0a 20 20 20 20 6e 4f 6c 64 2b 2b 3b 0a 20 20  ;.    nOld++;.  
e5c70 20 20 6e 4d 61 78 43 65 6c 6c 73 20 2b 3d 20 31    nMaxCells += 1
e5c80 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e 43 65 6c 6c  +apOld[i]->nCell
e5c90 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e 4f 76 65 72  +apOld[i]->nOver
e5ca0 66 6c 6f 77 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  flow;.  }..  /* 
e5cb0 4d 61 6b 65 20 6e 4d 61 78 43 65 6c 6c 73 20 61  Make nMaxCells a
e5cc0 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 32 20 69   multiple of 2 i
e5cd0 6e 20 6f 72 64 65 72 20 74 6f 20 70 72 65 73 65  n order to prese
e5ce0 72 76 65 20 38 2d 62 79 74 65 0a 20 20 2a 2a 20  rve 8-byte.  ** 
e5cf0 61 6c 69 67 6e 6d 65 6e 74 20 2a 2f 0a 20 20 6e  alignment */.  n
e5d00 4d 61 78 43 65 6c 6c 73 20 3d 20 28 6e 4d 61 78  MaxCells = (nMax
e5d10 43 65 6c 6c 73 20 2b 20 31 29 26 7e 31 3b 0a 0a  Cells + 1)&~1;..
e5d20 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61    /*.  ** Alloca
e5d30 74 65 20 73 70 61 63 65 20 66 6f 72 20 6d 65 6d  te space for mem
e5d40 6f 72 79 20 73 74 72 75 63 74 75 72 65 73 0a 20  ory structures. 
e5d50 20 2a 2f 0a 20 20 61 70 43 65 6c 6c 20 3d 20 73   */.  apCell = s
e5d60 71 6c 69 74 65 4d 61 6c 6c 6f 63 52 61 77 28 20  qliteMallocRaw( 
e5d70 0a 20 20 20 20 20 20 20 6e 4d 61 78 43 65 6c 6c  .       nMaxCell
e5d80 73 2a 73 69 7a 65 6f 66 28 75 38 2a 29 20 20 20  s*sizeof(u8*)   
e5d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e5da0 20 20 20 20 20 20 20 20 2f 2a 20 61 70 43 65 6c          /* apCel
e5db0 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 6e 4d 61 78  l */.     + nMax
e5dc0 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 69 6e 74  Cells*sizeof(int
e5dd0 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  )               
e5de0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
e5df0 7a 43 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 2b 20  zCell */.     + 
e5e00 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 4d 65  ROUND8(sizeof(Me
e5e10 6d 50 61 67 65 29 29 2a 4e 42 20 20 20 20 20 20  mPage))*NB      
e5e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e5e30 2f 2a 20 61 43 6f 70 79 20 2a 2f 0a 20 20 20 20  /* aCopy */.    
e5e40 20 2b 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65   + pBt->pageSize
e5e50 2a 28 35 2b 4e 42 29 20 20 20 20 20 20 20 20 20  *(5+NB)         
e5e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e5e70 20 20 20 2f 2a 20 61 53 70 61 63 65 20 2a 2f 0a     /* aSpace */.
e5e80 20 20 20 20 20 2b 20 28 49 53 41 55 54 4f 56 41       + (ISAUTOVA
e5e90 43 55 55 4d 20 3f 20 6e 4d 61 78 43 65 6c 6c 73  CUUM ? nMaxCells
e5ea0 20 3a 20 30 29 20 20 20 20 20 20 20 20 20 20 20   : 0)           
e5eb0 20 20 20 20 20 20 20 2f 2a 20 61 46 72 6f 6d 20         /* aFrom 
e5ec0 2a 2f 0a 20 20 29 3b 0a 20 20 69 66 28 20 61 70  */.  );.  if( ap
e5ed0 43 65 6c 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 72  Cell==0 ){.    r
e5ee0 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
e5ef0 3b 0a 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e  ;.    goto balan
e5f00 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a  ce_cleanup;.  }.
e5f10 20 20 73 7a 43 65 6c 6c 20 3d 20 28 69 6e 74 2a    szCell = (int*
e5f20 29 26 61 70 43 65 6c 6c 5b 6e 4d 61 78 43 65 6c  )&apCell[nMaxCel
e5f30 6c 73 5d 3b 0a 20 20 61 43 6f 70 79 5b 30 5d 20  ls];.  aCopy[0] 
e5f40 3d 20 28 75 38 2a 29 26 73 7a 43 65 6c 6c 5b 6e  = (u8*)&szCell[n
e5f50 4d 61 78 43 65 6c 6c 73 5d 3b 0a 20 20 61 73 73  MaxCells];.  ass
e5f60 65 72 74 28 20 28 28 61 43 6f 70 79 5b 30 5d 20  ert( ((aCopy[0] 
e5f70 2d 20 28 75 38 2a 29 61 70 43 65 6c 6c 29 20 26  - (u8*)apCell) &
e5f80 20 37 29 3d 3d 30 20 29 3b 20 2f 2a 20 38 2d 62   7)==0 ); /* 8-b
e5f90 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20 72 65  yte alignment re
e5fa0 71 75 69 72 65 64 20 2a 2f 0a 20 20 66 6f 72 28  quired */.  for(
e5fb0 69 3d 31 3b 20 69 3c 4e 42 3b 20 69 2b 2b 29 7b  i=1; i<NB; i++){
e5fc0 0a 20 20 20 20 61 43 6f 70 79 5b 69 5d 20 3d 20  .    aCopy[i] = 
e5fd0 26 61 43 6f 70 79 5b 69 2d 31 5d 5b 70 42 74 2d  &aCopy[i-1][pBt-
e5fe0 3e 70 61 67 65 53 69 7a 65 2b 52 4f 55 4e 44 38  >pageSize+ROUND8
e5ff0 28 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 29  (sizeof(MemPage)
e6000 29 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  )];.    assert( 
e6010 28 28 61 43 6f 70 79 5b 69 5d 20 2d 20 28 75 38  ((aCopy[i] - (u8
e6020 2a 29 61 70 43 65 6c 6c 29 20 26 20 37 29 3d 3d  *)apCell) & 7)==
e6030 30 20 29 3b 20 2f 2a 20 38 2d 62 79 74 65 20 61  0 ); /* 8-byte a
e6040 6c 69 67 6e 6d 65 6e 74 20 72 65 71 75 69 72 65  lignment require
e6050 64 20 2a 2f 0a 20 20 7d 0a 20 20 61 53 70 61 63  d */.  }.  aSpac
e6060 65 20 3d 20 26 61 43 6f 70 79 5b 4e 42 2d 31 5d  e = &aCopy[NB-1]
e6070 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2b 52  [pBt->pageSize+R
e6080 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 4d 65 6d  OUND8(sizeof(Mem
e6090 50 61 67 65 29 29 5d 3b 0a 20 20 61 73 73 65 72  Page))];.  asser
e60a0 74 28 20 28 28 61 53 70 61 63 65 20 2d 20 28 75  t( ((aSpace - (u
e60b0 38 2a 29 61 70 43 65 6c 6c 29 20 26 20 37 29 3d  8*)apCell) & 7)=
e60c0 3d 30 20 29 3b 20 2f 2a 20 38 2d 62 79 74 65 20  =0 ); /* 8-byte 
e60d0 61 6c 69 67 6e 6d 65 6e 74 20 72 65 71 75 69 72  alignment requir
e60e0 65 64 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  ed */.#ifndef SQ
e60f0 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
e6100 43 55 55 4d 0a 20 20 69 66 28 20 70 42 74 2d 3e  CUUM.  if( pBt->
e6110 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20  autoVacuum ){.  
e6120 20 20 61 46 72 6f 6d 20 3d 20 26 61 53 70 61 63    aFrom = &aSpac
e6130 65 5b 35 2a 70 42 74 2d 3e 70 61 67 65 53 69 7a  e[5*pBt->pageSiz
e6140 65 5d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20  e];.  }.#endif. 
e6150 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 61 6b 65   .  /*.  ** Make
e6160 20 63 6f 70 69 65 73 20 6f 66 20 74 68 65 20 63   copies of the c
e6170 6f 6e 74 65 6e 74 20 6f 66 20 70 50 61 67 65 20  ontent of pPage 
e6180 61 6e 64 20 69 74 73 20 73 69 62 6c 69 6e 67 73  and its siblings
e6190 20 69 6e 74 6f 20 61 4f 6c 64 5b 5d 2e 0a 20 20   into aOld[]..  
e61a0 2a 2a 20 54 68 65 20 72 65 73 74 20 6f 66 20 74  ** The rest of t
e61b0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c  his function wil
e61c0 6c 20 75 73 65 20 64 61 74 61 20 66 72 6f 6d 20  l use data from 
e61d0 74 68 65 20 63 6f 70 69 65 73 20 72 61 74 68 65  the copies rathe
e61e0 72 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20  r.  ** that the 
e61f0 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 73 20 73  original pages s
e6200 69 6e 63 65 20 74 68 65 20 6f 72 69 67 69 6e 61  ince the origina
e6210 6c 20 70 61 67 65 73 20 77 69 6c 6c 20 62 65 20  l pages will be 
e6220 69 6e 20 74 68 65 0a 20 20 2a 2a 20 70 72 6f 63  in the.  ** proc
e6230 65 73 73 20 6f 66 20 62 65 69 6e 67 20 6f 76 65  ess of being ove
e6240 72 77 72 69 74 74 65 6e 2e 0a 20 20 2a 2f 0a 20  rwritten..  */. 
e6250 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 6c 64   for(i=0; i<nOld
e6260 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 4d 65 6d 50  ; i++){.    MemP
e6270 61 67 65 20 2a 70 20 3d 20 61 70 43 6f 70 79 5b  age *p = apCopy[
e6280 69 5d 20 3d 20 28 4d 65 6d 50 61 67 65 2a 29 26  i] = (MemPage*)&
e6290 61 43 6f 70 79 5b 69 5d 5b 70 42 74 2d 3e 70 61  aCopy[i][pBt->pa
e62a0 67 65 53 69 7a 65 5d 3b 0a 20 20 20 20 70 2d 3e  geSize];.    p->
e62b0 61 44 61 74 61 20 3d 20 26 28 28 75 38 2a 29 70  aData = &((u8*)p
e62c0 29 5b 2d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  )[-pBt->pageSize
e62d0 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 2d  ];.    memcpy(p-
e62e0 3e 61 44 61 74 61 2c 20 61 70 4f 6c 64 5b 69 5d  >aData, apOld[i]
e62f0 2d 3e 61 44 61 74 61 2c 20 70 42 74 2d 3e 70 61  ->aData, pBt->pa
e6300 67 65 53 69 7a 65 20 2b 20 73 69 7a 65 6f 66 28  geSize + sizeof(
e6310 4d 65 6d 50 61 67 65 29 29 3b 0a 20 20 20 20 2f  MemPage));.    /
e6320 2a 20 54 68 65 20 6d 65 6d 63 70 79 28 29 20 61  * The memcpy() a
e6330 62 6f 76 65 20 63 68 61 6e 67 65 73 20 74 68 65  bove changes the
e6340 20 76 61 6c 75 65 20 6f 66 20 70 2d 3e 61 44 61   value of p->aDa
e6350 74 61 20 73 6f 20 77 65 20 68 61 76 65 20 74 6f  ta so we have to
e6360 0a 20 20 20 20 2a 2a 20 73 65 74 20 69 74 20 61  .    ** set it a
e6370 67 61 69 6e 2e 20 2a 2f 0a 20 20 20 20 70 2d 3e  gain. */.    p->
e6380 61 44 61 74 61 20 3d 20 26 28 28 75 38 2a 29 70  aData = &((u8*)p
e6390 29 5b 2d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  )[-pBt->pageSize
e63a0 5d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a  ];.  }..  /*.  *
e63b0 2a 20 4c 6f 61 64 20 70 6f 69 6e 74 65 72 73 20  * Load pointers 
e63c0 74 6f 20 61 6c 6c 20 63 65 6c 6c 73 20 6f 6e 20  to all cells on 
e63d0 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 61 6e  sibling pages an
e63e0 64 20 74 68 65 20 64 69 76 69 64 65 72 20 63 65  d the divider ce
e63f0 6c 6c 73 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 68  lls.  ** into th
e6400 65 20 6c 6f 63 61 6c 20 61 70 43 65 6c 6c 5b 5d  e local apCell[]
e6410 20 61 72 72 61 79 2e 20 20 4d 61 6b 65 20 63 6f   array.  Make co
e6420 70 69 65 73 20 6f 66 20 74 68 65 20 64 69 76 69  pies of the divi
e6430 64 65 72 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 69  der cells.  ** i
e6440 6e 74 6f 20 73 70 61 63 65 20 6f 62 74 61 69 6e  nto space obtain
e6450 65 64 20 66 6f 72 6d 20 61 53 70 61 63 65 5b 5d  ed form aSpace[]
e6460 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 68 65 20   and remove the 
e6470 74 68 65 20 64 69 76 69 64 65 72 20 43 65 6c 6c  the divider Cell
e6480 73 0a 20 20 2a 2a 20 66 72 6f 6d 20 70 50 61 72  s.  ** from pPar
e6490 65 6e 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49  ent..  **.  ** I
e64a0 66 20 74 68 65 20 73 69 62 6c 69 6e 67 73 20 61  f the siblings a
e64b0 72 65 20 6f 6e 20 6c 65 61 66 20 70 61 67 65 73  re on leaf pages
e64c0 2c 20 74 68 65 6e 20 74 68 65 20 63 68 69 6c 64  , then the child
e64d0 20 70 6f 69 6e 74 65 72 73 20 6f 66 20 74 68 65   pointers of the
e64e0 0a 20 20 2a 2a 20 64 69 76 69 64 65 72 20 63 65  .  ** divider ce
e64f0 6c 6c 73 20 61 72 65 20 73 74 72 69 70 70 65 64  lls are stripped
e6500 20 66 72 6f 6d 20 74 68 65 20 63 65 6c 6c 73 20   from the cells 
e6510 62 65 66 6f 72 65 20 74 68 65 79 20 61 72 65 20  before they are 
e6520 63 6f 70 69 65 64 0a 20 20 2a 2a 20 69 6e 74 6f  copied.  ** into
e6530 20 61 53 70 61 63 65 5b 5d 2e 20 20 49 6e 20 74   aSpace[].  In t
e6540 68 69 73 20 77 61 79 2c 20 61 6c 6c 20 63 65 6c  his way, all cel
e6550 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 61  ls in apCell[] a
e6560 72 65 20 77 69 74 68 6f 75 74 0a 20 20 2a 2a 20  re without.  ** 
e6570 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 73 2e 20  child pointers. 
e6580 20 49 66 20 73 69 62 6c 69 6e 67 73 20 61 72 65   If siblings are
e6590 20 6e 6f 74 20 6c 65 61 76 65 73 2c 20 74 68 65   not leaves, the
e65a0 6e 20 61 6c 6c 20 63 65 6c 6c 20 69 6e 0a 20 20  n all cell in.  
e65b0 2a 2a 20 61 70 43 65 6c 6c 5b 5d 20 69 6e 63 6c  ** apCell[] incl
e65c0 75 64 65 20 63 68 69 6c 64 20 70 6f 69 6e 74 65  ude child pointe
e65d0 72 73 2e 20 20 45 69 74 68 65 72 20 77 61 79 2c  rs.  Either way,
e65e0 20 61 6c 6c 20 63 65 6c 6c 73 20 69 6e 20 61 70   all cells in ap
e65f0 43 65 6c 6c 5b 5d 0a 20 20 2a 2a 20 61 72 65 20  Cell[].  ** are 
e6600 61 6c 69 6b 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  alike..  **.  **
e6610 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3a   leafCorrection:
e6620 20 20 34 20 69 66 20 70 50 61 67 65 20 69 73 20    4 if pPage is 
e6630 61 20 6c 65 61 66 2e 20 20 30 20 69 66 20 70 50  a leaf.  0 if pP
e6640 61 67 65 20 69 73 20 6e 6f 74 20 61 20 6c 65 61  age is not a lea
e6650 66 2e 0a 20 20 2a 2a 20 20 20 20 20 20 20 6c 65  f..  **       le
e6660 61 66 44 61 74 61 3a 20 20 31 20 69 66 20 70 50  afData:  1 if pP
e6670 61 67 65 20 68 6f 6c 64 73 20 6b 65 79 2b 64 61  age holds key+da
e6680 74 61 20 61 6e 64 20 70 50 61 72 65 6e 74 20 68  ta and pParent h
e6690 6f 6c 64 73 20 6f 6e 6c 79 20 6b 65 79 73 2e 0a  olds only keys..
e66a0 20 20 2a 2f 0a 20 20 6e 43 65 6c 6c 20 3d 20 30    */.  nCell = 0
e66b0 3b 0a 20 20 6c 65 61 66 43 6f 72 72 65 63 74 69  ;.  leafCorrecti
e66c0 6f 6e 20 3d 20 70 50 61 67 65 2d 3e 6c 65 61 66  on = pPage->leaf
e66d0 2a 34 3b 0a 20 20 6c 65 61 66 44 61 74 61 20 3d  *4;.  leafData =
e66e0 20 70 50 61 67 65 2d 3e 6c 65 61 66 44 61 74 61   pPage->leafData
e66f0 20 26 26 20 70 50 61 67 65 2d 3e 6c 65 61 66 3b   && pPage->leaf;
e6700 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f  .  for(i=0; i<nO
e6710 6c 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 4d 65  ld; i++){.    Me
e6720 6d 50 61 67 65 20 2a 70 4f 6c 64 20 3d 20 61 70  mPage *pOld = ap
e6730 43 6f 70 79 5b 69 5d 3b 0a 20 20 20 20 69 6e 74  Copy[i];.    int
e6740 20 6c 69 6d 69 74 20 3d 20 70 4f 6c 64 2d 3e 6e   limit = pOld->n
e6750 43 65 6c 6c 2b 70 4f 6c 64 2d 3e 6e 4f 76 65 72  Cell+pOld->nOver
e6760 66 6c 6f 77 3b 0a 20 20 20 20 66 6f 72 28 6a 3d  flow;.    for(j=
e6770 30 3b 20 6a 3c 6c 69 6d 69 74 3b 20 6a 2b 2b 29  0; j<limit; j++)
e6780 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
e6790 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c 73 20  nCell<nMaxCells 
e67a0 29 3b 0a 20 20 20 20 20 20 61 70 43 65 6c 6c 5b  );.      apCell[
e67b0 6e 43 65 6c 6c 5d 20 3d 20 66 69 6e 64 4f 76 65  nCell] = findOve
e67c0 72 66 6c 6f 77 43 65 6c 6c 28 70 4f 6c 64 2c 20  rflowCell(pOld, 
e67d0 6a 29 3b 0a 20 20 20 20 20 20 73 7a 43 65 6c 6c  j);.      szCell
e67e0 5b 6e 43 65 6c 6c 5d 20 3d 20 63 65 6c 6c 53 69  [nCell] = cellSi
e67f0 7a 65 50 74 72 28 70 4f 6c 64 2c 20 61 70 43 65  zePtr(pOld, apCe
e6800 6c 6c 5b 6e 43 65 6c 6c 5d 29 3b 0a 23 69 66 6e  ll[nCell]);.#ifn
e6810 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
e6820 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20  AUTOVACUUM.     
e6830 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61   if( pBt->autoVa
e6840 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20  cuum ){.        
e6850 69 6e 74 20 61 3b 0a 20 20 20 20 20 20 20 20 61  int a;.        a
e6860 46 72 6f 6d 5b 6e 43 65 6c 6c 5d 20 3d 20 69 3b  From[nCell] = i;
e6870 0a 20 20 20 20 20 20 20 20 66 6f 72 28 61 3d 30  .        for(a=0
e6880 3b 20 61 3c 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66  ; a<pOld->nOverf
e6890 6c 6f 77 3b 20 61 2b 2b 29 7b 0a 20 20 20 20 20  low; a++){.     
e68a0 20 20 20 20 20 69 66 28 20 70 4f 6c 64 2d 3e 61       if( pOld->a
e68b0 4f 76 66 6c 5b 61 5d 2e 70 43 65 6c 6c 3d 3d 61  Ovfl[a].pCell==a
e68c0 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 29 7b 0a  pCell[nCell] ){.
e68d0 20 20 20 20 20 20 20 20 20 20 20 20 61 46 72 6f              aFro
e68e0 6d 5b 6e 43 65 6c 6c 5d 20 3d 20 30 78 46 46 3b  m[nCell] = 0xFF;
e68f0 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65  .            bre
e6900 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ak;.          }.
e6910 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
e6920 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 6e  }.#endif.      n
e6930 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20  Cell++;.    }.  
e6940 20 20 69 66 28 20 69 3c 6e 4f 6c 64 2d 31 20 29    if( i<nOld-1 )
e6950 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 7a 20 3d  {.      int sz =
e6960 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61   cellSizePtr(pPa
e6970 72 65 6e 74 2c 20 61 70 44 69 76 5b 69 5d 29 3b  rent, apDiv[i]);
e6980 0a 20 20 20 20 20 20 69 66 28 20 6c 65 61 66 44  .      if( leafD
e6990 61 74 61 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ata ){.        /
e69a0 2a 20 57 69 74 68 20 74 68 65 20 4c 45 41 46 44  * With the LEAFD
e69b0 41 54 41 20 66 6c 61 67 2c 20 70 50 61 72 65 6e  ATA flag, pParen
e69c0 74 20 63 65 6c 6c 73 20 68 6f 6c 64 20 6f 6e 6c  t cells hold onl
e69d0 79 20 49 4e 54 4b 45 59 73 20 74 68 61 74 0a 20  y INTKEYs that. 
e69e0 20 20 20 20 20 20 20 2a 2a 20 61 72 65 20 64 75         ** are du
e69f0 70 6c 69 63 61 74 65 73 20 6f 66 20 6b 65 79 73  plicates of keys
e6a00 20 6f 6e 20 74 68 65 20 63 68 69 6c 64 20 70 61   on the child pa
e6a10 67 65 73 2e 20 20 57 65 20 6e 65 65 64 20 74 6f  ges.  We need to
e6a20 20 72 65 6d 6f 76 65 0a 20 20 20 20 20 20 20 20   remove.        
e6a30 2a 2a 20 74 68 65 20 64 69 76 69 64 65 72 20 63  ** the divider c
e6a40 65 6c 6c 73 20 66 72 6f 6d 20 70 50 61 72 65 6e  ells from pParen
e6a50 74 2c 20 62 75 74 20 74 68 65 20 64 69 76 69 64  t, but the divid
e6a60 65 72 73 20 63 65 6c 6c 73 20 61 72 65 20 6e 6f  ers cells are no
e6a70 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 64 64  t.        ** add
e6a80 65 64 20 74 6f 20 61 70 43 65 6c 6c 5b 5d 20 62  ed to apCell[] b
e6a90 65 63 61 75 73 65 20 74 68 65 79 20 61 72 65 20  ecause they are 
e6aa0 64 75 70 6c 69 63 61 74 65 73 20 6f 66 20 63 68  duplicates of ch
e6ab0 69 6c 64 20 63 65 6c 6c 73 2e 0a 20 20 20 20 20  ild cells..     
e6ac0 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 64 72     */.        dr
e6ad0 6f 70 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20  opCell(pParent, 
e6ae0 6e 78 44 69 76 2c 20 73 7a 29 3b 0a 20 20 20 20  nxDiv, sz);.    
e6af0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
e6b00 20 75 38 20 2a 70 54 65 6d 70 3b 0a 20 20 20 20   u8 *pTemp;.    
e6b10 20 20 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c      assert( nCel
e6b20 6c 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20  l<nMaxCells );. 
e6b30 20 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43         szCell[nC
e6b40 65 6c 6c 5d 20 3d 20 73 7a 3b 0a 20 20 20 20 20  ell] = sz;.     
e6b50 20 20 20 70 54 65 6d 70 20 3d 20 26 61 53 70 61     pTemp = &aSpa
e6b60 63 65 5b 69 53 70 61 63 65 5d 3b 0a 20 20 20 20  ce[iSpace];.    
e6b70 20 20 20 20 69 53 70 61 63 65 20 2b 3d 20 73 7a      iSpace += sz
e6b80 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
e6b90 28 20 69 53 70 61 63 65 3c 3d 70 42 74 2d 3e 70  ( iSpace<=pBt->p
e6ba0 61 67 65 53 69 7a 65 2a 35 20 29 3b 0a 20 20 20  ageSize*5 );.   
e6bb0 20 20 20 20 20 6d 65 6d 63 70 79 28 70 54 65 6d       memcpy(pTem
e6bc0 70 2c 20 61 70 44 69 76 5b 69 5d 2c 20 73 7a 29  p, apDiv[i], sz)
e6bd0 3b 0a 20 20 20 20 20 20 20 20 61 70 43 65 6c 6c  ;.        apCell
e6be0 5b 6e 43 65 6c 6c 5d 20 3d 20 70 54 65 6d 70 2b  [nCell] = pTemp+
e6bf0 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b 0a  leafCorrection;.
e6c00 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
e6c10 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
e6c20 20 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e         if( pBt->
e6c30 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20  autoVacuum ){.  
e6c40 20 20 20 20 20 20 20 20 61 46 72 6f 6d 5b 6e 43          aFrom[nC
e6c50 65 6c 6c 5d 20 3d 20 30 78 46 46 3b 0a 20 20 20  ell] = 0xFF;.   
e6c60 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20       }.#endif.  
e6c70 20 20 20 20 20 20 64 72 6f 70 43 65 6c 6c 28 70        dropCell(p
e6c80 50 61 72 65 6e 74 2c 20 6e 78 44 69 76 2c 20 73  Parent, nxDiv, s
e6c90 7a 29 3b 0a 20 20 20 20 20 20 20 20 73 7a 43 65  z);.        szCe
e6ca0 6c 6c 5b 6e 43 65 6c 6c 5d 20 2d 3d 20 6c 65 61  ll[nCell] -= lea
e6cb0 66 43 6f 72 72 65 63 74 69 6f 6e 3b 0a 20 20 20  fCorrection;.   
e6cc0 20 20 20 20 20 61 73 73 65 72 74 28 20 67 65 74       assert( get
e6cd0 34 62 79 74 65 28 70 54 65 6d 70 29 3d 3d 70 67  4byte(pTemp)==pg
e6ce0 6e 6f 4f 6c 64 5b 69 5d 20 29 3b 0a 20 20 20 20  noOld[i] );.    
e6cf0 20 20 20 20 69 66 28 20 21 70 4f 6c 64 2d 3e 6c      if( !pOld->l
e6d00 65 61 66 20 29 7b 0a 20 20 20 20 20 20 20 20 20  eaf ){.         
e6d10 20 61 73 73 65 72 74 28 20 6c 65 61 66 43 6f 72   assert( leafCor
e6d20 72 65 63 74 69 6f 6e 3d 3d 30 20 29 3b 0a 20 20  rection==0 );.  
e6d30 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72          /* The r
e6d40 69 67 68 74 20 70 6f 69 6e 74 65 72 20 6f 66 20  ight pointer of 
e6d50 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 20 70  the child page p
e6d60 4f 6c 64 20 62 65 63 6f 6d 65 73 20 74 68 65 20  Old becomes the 
e6d70 6c 65 66 74 0a 20 20 20 20 20 20 20 20 20 20 2a  left.          *
e6d80 2a 20 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 65  * pointer of the
e6d90 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 2a 2f   divider cell */
e6da0 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70  .          memcp
e6db0 79 28 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 2c  y(apCell[nCell],
e6dc0 20 26 70 4f 6c 64 2d 3e 61 44 61 74 61 5b 70 4f   &pOld->aData[pO
e6dd0 6c 64 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d  ld->hdrOffset+8]
e6de0 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 7d 65  , 4);.        }e
e6df0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61  lse{.          a
e6e00 73 73 65 72 74 28 20 6c 65 61 66 43 6f 72 72 65  ssert( leafCorre
e6e10 63 74 69 6f 6e 3d 3d 34 20 29 3b 0a 20 20 20 20  ction==4 );.    
e6e20 20 20 20 20 20 20 69 66 28 20 73 7a 43 65 6c 6c        if( szCell
e6e30 5b 6e 43 65 6c 6c 5d 3c 34 20 29 7b 0a 20 20 20  [nCell]<4 ){.   
e6e40 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e           /* Do n
e6e50 6f 74 20 61 6c 6c 6f 77 20 61 6e 79 20 63 65 6c  ot allow any cel
e6e60 6c 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20  ls smaller than 
e6e70 34 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 20 20  4 bytes. */.    
e6e80 20 20 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e          szCell[n
e6e90 43 65 6c 6c 5d 20 3d 20 34 3b 0a 20 20 20 20 20  Cell] = 4;.     
e6ea0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
e6eb0 0a 20 20 20 20 20 20 20 20 6e 43 65 6c 6c 2b 2b  .        nCell++
e6ec0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
e6ed0 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46    }..  /*.  ** F
e6ee0 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 6e 75  igure out the nu
e6ef0 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6e 65  mber of pages ne
e6f00 65 64 65 64 20 74 6f 20 68 6f 6c 64 20 61 6c 6c  eded to hold all
e6f10 20 6e 43 65 6c 6c 20 63 65 6c 6c 73 2e 0a 20 20   nCell cells..  
e6f20 2a 2a 20 53 74 6f 72 65 20 74 68 69 73 20 6e 75  ** Store this nu
e6f30 6d 62 65 72 20 69 6e 20 22 6b 22 2e 20 20 41 6c  mber in "k".  Al
e6f40 73 6f 20 63 6f 6d 70 75 74 65 20 73 7a 4e 65 77  so compute szNew
e6f50 5b 5d 20 77 68 69 63 68 20 69 73 20 74 68 65 20  [] which is the 
e6f60 74 6f 74 61 6c 0a 20 20 2a 2a 20 73 69 7a 65 20  total.  ** size 
e6f70 6f 66 20 61 6c 6c 20 63 65 6c 6c 73 20 6f 6e 20  of all cells on 
e6f80 74 68 65 20 69 2d 74 68 20 70 61 67 65 20 61 6e  the i-th page an
e6f90 64 20 63 6e 74 4e 65 77 5b 5d 20 77 68 69 63 68  d cntNew[] which
e6fa0 20 69 73 20 74 68 65 20 69 6e 64 65 78 0a 20 20   is the index.  
e6fb0 2a 2a 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 6f  ** in apCell[] o
e6fc0 66 20 74 68 65 20 63 65 6c 6c 20 74 68 61 74 20  f the cell that 
e6fd0 64 69 76 69 64 65 73 20 70 61 67 65 20 69 20 66  divides page i f
e6fe0 72 6f 6d 20 70 61 67 65 20 69 2b 31 2e 20 20 0a  rom page i+1.  .
e6ff0 20 20 2a 2a 20 63 6e 74 4e 65 77 5b 6b 5d 20 73    ** cntNew[k] s
e7000 68 6f 75 6c 64 20 65 71 75 61 6c 20 6e 43 65 6c  hould equal nCel
e7010 6c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 6c  l..  **.  ** Val
e7020 75 65 73 20 63 6f 6d 70 75 74 65 64 20 62 79 20  ues computed by 
e7030 74 68 69 73 20 62 6c 6f 63 6b 3a 0a 20 20 2a 2a  this block:.  **
e7040 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20  .  **           
e7050 6b 3a 20 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d  k: The total num
e7060 62 65 72 20 6f 66 20 73 69 62 6c 69 6e 67 20 70  ber of sibling p
e7070 61 67 65 73 0a 20 20 2a 2a 20 20 20 20 73 7a 4e  ages.  **    szN
e7080 65 77 5b 69 5d 3a 20 53 70 61 63 65 64 20 75 73  ew[i]: Spaced us
e7090 65 64 20 6f 6e 20 74 68 65 20 69 2d 74 68 20 73  ed on the i-th s
e70a0 69 62 6c 69 6e 67 20 70 61 67 65 2e 0a 20 20 2a  ibling page..  *
e70b0 2a 20 20 20 63 6e 74 4e 65 77 5b 69 5d 3a 20 49  *   cntNew[i]: I
e70c0 6e 64 65 78 20 69 6e 20 61 70 43 65 6c 6c 5b 5d  ndex in apCell[]
e70d0 20 61 6e 64 20 73 7a 43 65 6c 6c 5b 5d 20 66 6f   and szCell[] fo
e70e0 72 20 74 68 65 20 66 69 72 73 74 20 63 65 6c 6c  r the first cell
e70f0 20 74 6f 0a 20 20 2a 2a 20 20 20 20 20 20 20 20   to.  **        
e7100 20 20 20 20 20 20 74 68 65 20 72 69 67 68 74 20        the right 
e7110 6f 66 20 74 68 65 20 69 2d 74 68 20 73 69 62 6c  of the i-th sibl
e7120 69 6e 67 20 70 61 67 65 2e 0a 20 20 2a 2a 20 75  ing page..  ** u
e7130 73 61 62 6c 65 53 70 61 63 65 3a 20 4e 75 6d 62  sableSpace: Numb
e7140 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73  er of bytes of s
e7150 70 61 63 65 20 61 76 61 69 6c 61 62 6c 65 20 6f  pace available o
e7160 6e 20 65 61 63 68 20 73 69 62 6c 69 6e 67 2e 0a  n each sibling..
e7170 20 20 2a 2a 20 0a 20 20 2a 2f 0a 20 20 75 73 61    ** .  */.  usa
e7180 62 6c 65 53 70 61 63 65 20 3d 20 70 42 74 2d 3e  bleSpace = pBt->
e7190 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 31 32 20  usableSize - 12 
e71a0 2b 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e  + leafCorrection
e71b0 3b 0a 20 20 66 6f 72 28 73 75 62 74 6f 74 61 6c  ;.  for(subtotal
e71c0 3d 6b 3d 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b  =k=i=0; i<nCell;
e71d0 20 69 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72   i++){.    asser
e71e0 74 28 20 69 3c 6e 4d 61 78 43 65 6c 6c 73 20 29  t( i<nMaxCells )
e71f0 3b 0a 20 20 20 20 73 75 62 74 6f 74 61 6c 20 2b  ;.    subtotal +
e7200 3d 20 73 7a 43 65 6c 6c 5b 69 5d 20 2b 20 32 3b  = szCell[i] + 2;
e7210 0a 20 20 20 20 69 66 28 20 73 75 62 74 6f 74 61  .    if( subtota
e7220 6c 20 3e 20 75 73 61 62 6c 65 53 70 61 63 65 20  l > usableSpace 
e7230 29 7b 0a 20 20 20 20 20 20 73 7a 4e 65 77 5b 6b  ){.      szNew[k
e7240 5d 20 3d 20 73 75 62 74 6f 74 61 6c 20 2d 20 73  ] = subtotal - s
e7250 7a 43 65 6c 6c 5b 69 5d 3b 0a 20 20 20 20 20 20  zCell[i];.      
e7260 63 6e 74 4e 65 77 5b 6b 5d 20 3d 20 69 3b 0a 20  cntNew[k] = i;. 
e7270 20 20 20 20 20 69 66 28 20 6c 65 61 66 44 61 74       if( leafDat
e7280 61 20 29 7b 20 69 2d 2d 3b 20 7d 0a 20 20 20 20  a ){ i--; }.    
e7290 20 20 73 75 62 74 6f 74 61 6c 20 3d 20 30 3b 0a    subtotal = 0;.
e72a0 20 20 20 20 20 20 6b 2b 2b 3b 0a 20 20 20 20 7d        k++;.    }
e72b0 0a 20 20 7d 0a 20 20 73 7a 4e 65 77 5b 6b 5d 20  .  }.  szNew[k] 
e72c0 3d 20 73 75 62 74 6f 74 61 6c 3b 0a 20 20 63 6e  = subtotal;.  cn
e72d0 74 4e 65 77 5b 6b 5d 20 3d 20 6e 43 65 6c 6c 3b  tNew[k] = nCell;
e72e0 0a 20 20 6b 2b 2b 3b 0a 0a 20 20 2f 2a 0a 20 20  .  k++;..  /*.  
e72f0 2a 2a 20 54 68 65 20 70 61 63 6b 69 6e 67 20 63  ** The packing c
e7300 6f 6d 70 75 74 65 64 20 62 79 20 74 68 65 20 70  omputed by the p
e7310 72 65 76 69 6f 75 73 20 62 6c 6f 63 6b 20 69 73  revious block is
e7320 20 62 69 61 73 65 64 20 74 6f 77 61 72 64 20 74   biased toward t
e7330 68 65 20 73 69 62 6c 69 6e 67 73 0a 20 20 2a 2a  he siblings.  **
e7340 20 6f 6e 20 74 68 65 20 6c 65 66 74 20 73 69 64   on the left sid
e7350 65 2e 20 20 54 68 65 20 6c 65 66 74 20 73 69 62  e.  The left sib
e7360 6c 69 6e 67 73 20 61 72 65 20 61 6c 77 61 79 73  lings are always
e7370 20 6e 65 61 72 6c 79 20 66 75 6c 6c 2c 20 77 68   nearly full, wh
e7380 69 6c 65 20 74 68 65 0a 20 20 2a 2a 20 72 69 67  ile the.  ** rig
e7390 68 74 2d 6d 6f 73 74 20 73 69 62 6c 69 6e 67 20  ht-most sibling 
e73a0 6d 69 67 68 74 20 62 65 20 6e 65 61 72 6c 79 20  might be nearly 
e73b0 65 6d 70 74 79 2e 20 20 54 68 69 73 20 62 6c 6f  empty.  This blo
e73c0 63 6b 20 6f 66 20 63 6f 64 65 20 61 74 74 65 6d  ck of code attem
e73d0 70 74 73 0a 20 20 2a 2a 20 74 6f 20 61 64 6a 75  pts.  ** to adju
e73e0 73 74 20 74 68 65 20 70 61 63 6b 69 6e 67 20 6f  st the packing o
e73f0 66 20 73 69 62 6c 69 6e 67 73 20 74 6f 20 67 65  f siblings to ge
e7400 74 20 61 20 62 65 74 74 65 72 20 62 61 6c 61 6e  t a better balan
e7410 63 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  ce..  **.  ** Th
e7420 69 73 20 61 64 6a 75 73 74 6d 65 6e 74 20 69 73  is adjustment is
e7430 20 6d 6f 72 65 20 74 68 61 6e 20 61 6e 20 6f 70   more than an op
e7440 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 54 68 65  timization.  The
e7450 20 70 61 63 6b 69 6e 67 20 61 62 6f 76 65 20 6d   packing above m
e7460 69 67 68 74 0a 20 20 2a 2a 20 62 65 20 73 6f 20  ight.  ** be so 
e7470 6f 75 74 20 6f 66 20 62 61 6c 61 6e 63 65 20 61  out of balance a
e7480 73 20 74 6f 20 62 65 20 69 6c 6c 65 67 61 6c 2e  s to be illegal.
e7490 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74    For example, t
e74a0 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 0a 20 20  he right-most.  
e74b0 2a 2a 20 73 69 62 6c 69 6e 67 20 6d 69 67 68 74  ** sibling might
e74c0 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65   be completely e
e74d0 6d 70 74 79 2e 20 20 54 68 69 73 20 61 64 6a 75  mpty.  This adju
e74e0 73 74 6d 65 6e 74 20 69 73 20 6e 6f 74 20 6f 70  stment is not op
e74f0 74 69 6f 6e 61 6c 2e 0a 20 20 2a 2f 0a 20 20 66  tional..  */.  f
e7500 6f 72 28 69 3d 6b 2d 31 3b 20 69 3e 30 3b 20 69  or(i=k-1; i>0; i
e7510 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 20 73 7a 52  --){.    int szR
e7520 69 67 68 74 20 3d 20 73 7a 4e 65 77 5b 69 5d 3b  ight = szNew[i];
e7530 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 69 62    /* Size of sib
e7540 6c 69 6e 67 20 6f 6e 20 74 68 65 20 72 69 67 68  ling on the righ
e7550 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 7a 4c  t */.    int szL
e7560 65 66 74 20 3d 20 73 7a 4e 65 77 5b 69 2d 31 5d  eft = szNew[i-1]
e7570 3b 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 69 62  ; /* Size of sib
e7580 6c 69 6e 67 20 6f 6e 20 74 68 65 20 6c 65 66 74  ling on the left
e7590 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 3b 20 20   */.    int r;  
e75a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
e75b0 6e 64 65 78 20 6f 66 20 72 69 67 68 74 2d 6d 6f  ndex of right-mo
e75c0 73 74 20 63 65 6c 6c 20 69 6e 20 6c 65 66 74 20  st cell in left 
e75d0 73 69 62 6c 69 6e 67 20 2a 2f 0a 20 20 20 20 69  sibling */.    i
e75e0 6e 74 20 64 3b 20 20 20 20 20 20 20 20 20 20 20  nt d;           
e75f0 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 66     /* Index of f
e7600 69 72 73 74 20 63 65 6c 6c 20 74 6f 20 74 68 65  irst cell to the
e7610 20 6c 65 66 74 20 6f 66 20 72 69 67 68 74 20 73   left of right s
e7620 69 62 6c 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 72  ibling */..    r
e7630 20 3d 20 63 6e 74 4e 65 77 5b 69 2d 31 5d 20 2d   = cntNew[i-1] -
e7640 20 31 3b 0a 20 20 20 20 64 20 3d 20 72 20 2b 20   1;.    d = r + 
e7650 31 20 2d 20 6c 65 61 66 44 61 74 61 3b 0a 20 20  1 - leafData;.  
e7660 20 20 61 73 73 65 72 74 28 20 64 3c 6e 4d 61 78    assert( d<nMax
e7670 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 61 73 73  Cells );.    ass
e7680 65 72 74 28 20 72 3c 6e 4d 61 78 43 65 6c 6c 73  ert( r<nMaxCells
e7690 20 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 73   );.    while( s
e76a0 7a 52 69 67 68 74 3d 3d 30 20 7c 7c 20 73 7a 52  zRight==0 || szR
e76b0 69 67 68 74 2b 73 7a 43 65 6c 6c 5b 64 5d 2b 32  ight+szCell[d]+2
e76c0 3c 3d 73 7a 4c 65 66 74 2d 28 73 7a 43 65 6c 6c  <=szLeft-(szCell
e76d0 5b 72 5d 2b 32 29 20 29 7b 0a 20 20 20 20 20 20  [r]+2) ){.      
e76e0 73 7a 52 69 67 68 74 20 2b 3d 20 73 7a 43 65 6c  szRight += szCel
e76f0 6c 5b 64 5d 20 2b 20 32 3b 0a 20 20 20 20 20 20  l[d] + 2;.      
e7700 73 7a 4c 65 66 74 20 2d 3d 20 73 7a 43 65 6c 6c  szLeft -= szCell
e7710 5b 72 5d 20 2b 20 32 3b 0a 20 20 20 20 20 20 63  [r] + 2;.      c
e7720 6e 74 4e 65 77 5b 69 2d 31 5d 2d 2d 3b 0a 20 20  ntNew[i-1]--;.  
e7730 20 20 20 20 72 20 3d 20 63 6e 74 4e 65 77 5b 69      r = cntNew[i
e7740 2d 31 5d 20 2d 20 31 3b 0a 20 20 20 20 20 20 64  -1] - 1;.      d
e7750 20 3d 20 72 20 2b 20 31 20 2d 20 6c 65 61 66 44   = r + 1 - leafD
e7760 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  ata;.    }.    s
e7770 7a 4e 65 77 5b 69 5d 20 3d 20 73 7a 52 69 67 68  zNew[i] = szRigh
e7780 74 3b 0a 20 20 20 20 73 7a 4e 65 77 5b 69 2d 31  t;.    szNew[i-1
e7790 5d 20 3d 20 73 7a 4c 65 66 74 3b 0a 20 20 7d 0a  ] = szLeft;.  }.
e77a0 0a 20 20 2f 2a 20 45 69 74 68 65 72 20 77 65 20  .  /* Either we 
e77b0 66 6f 75 6e 64 20 6f 6e 65 20 6f 72 20 6d 6f 72  found one or mor
e77c0 65 20 63 65 6c 6c 73 20 28 63 6e 74 6e 65 77 5b  e cells (cntnew[
e77d0 30 5d 29 3e 30 29 20 6f 72 20 77 65 20 61 72 65  0])>0) or we are
e77e0 20 74 68 65 0a 20 20 2a 2a 20 61 20 76 69 72 74   the.  ** a virt
e77f0 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 2e 20 20  ual root page.  
e7800 41 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70  A virtual root p
e7810 61 67 65 20 69 73 20 77 68 65 6e 20 74 68 65 20  age is when the 
e7820 72 65 61 6c 20 72 6f 6f 74 0a 20 20 2a 2a 20 70  real root.  ** p
e7830 61 67 65 20 69 73 20 70 61 67 65 20 31 20 61 6e  age is page 1 an
e7840 64 20 77 65 20 61 72 65 20 74 68 65 20 6f 6e 6c  d we are the onl
e7850 79 20 63 68 69 6c 64 20 6f 66 20 74 68 61 74 20  y child of that 
e7860 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  page..  */.  ass
e7870 65 72 74 28 20 63 6e 74 4e 65 77 5b 30 5d 3e 30  ert( cntNew[0]>0
e7880 20 7c 7c 20 28 70 50 61 72 65 6e 74 2d 3e 70 67   || (pParent->pg
e7890 6e 6f 3d 3d 31 20 26 26 20 70 50 61 72 65 6e 74  no==1 && pParent
e78a0 2d 3e 6e 43 65 6c 6c 3d 3d 30 29 20 29 3b 0a 0a  ->nCell==0) );..
e78b0 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61    /*.  ** Alloca
e78c0 74 65 20 6b 20 6e 65 77 20 70 61 67 65 73 2e 20  te k new pages. 
e78d0 20 52 65 75 73 65 20 6f 6c 64 20 70 61 67 65 73   Reuse old pages
e78e0 20 77 68 65 72 65 20 70 6f 73 73 69 62 6c 65 2e   where possible.
e78f0 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
e7900 70 50 61 67 65 2d 3e 70 67 6e 6f 3e 31 20 29 3b  pPage->pgno>1 );
e7910 0a 20 20 70 61 67 65 46 6c 61 67 73 20 3d 20 70  .  pageFlags = p
e7920 50 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d 3b 0a  Page->aData[0];.
e7930 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6b 3b 20    for(i=0; i<k; 
e7940 69 2b 2b 29 7b 0a 20 20 20 20 4d 65 6d 50 61 67  i++){.    MemPag
e7950 65 20 2a 70 4e 65 77 3b 0a 20 20 20 20 69 66 28  e *pNew;.    if(
e7960 20 69 3c 6e 4f 6c 64 20 29 7b 0a 20 20 20 20 20   i<nOld ){.     
e7970 20 70 4e 65 77 20 3d 20 61 70 4e 65 77 5b 69 5d   pNew = apNew[i]
e7980 20 3d 20 61 70 4f 6c 64 5b 69 5d 3b 0a 20 20 20   = apOld[i];.   
e7990 20 20 20 70 67 6e 6f 4e 65 77 5b 69 5d 20 3d 20     pgnoNew[i] = 
e79a0 70 67 6e 6f 4f 6c 64 5b 69 5d 3b 0a 20 20 20 20  pgnoOld[i];.    
e79b0 20 20 61 70 4f 6c 64 5b 69 5d 20 3d 20 30 3b 0a    apOld[i] = 0;.
e79c0 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
e79d0 65 33 50 61 67 65 72 57 72 69 74 65 28 70 4e 65  e3PagerWrite(pNe
e79e0 77 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  w->pDbPage);.   
e79f0 20 20 20 6e 4e 65 77 2b 2b 3b 0a 20 20 20 20 20     nNew++;.     
e7a00 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 62   if( rc ) goto b
e7a10 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a  alance_cleanup;.
e7a20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
e7a30 20 61 73 73 65 72 74 28 20 69 3e 30 20 29 3b 0a   assert( i>0 );.
e7a40 20 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63        rc = alloc
e7a50 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74  ateBtreePage(pBt
e7a60 2c 20 26 70 4e 65 77 2c 20 26 70 67 6e 6f 4e 65  , &pNew, &pgnoNe
e7a70 77 5b 69 5d 2c 20 70 67 6e 6f 4e 65 77 5b 69 2d  w[i], pgnoNew[i-
e7a80 31 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66  1], 0);.      if
e7a90 28 20 72 63 20 29 20 67 6f 74 6f 20 62 61 6c 61  ( rc ) goto bala
e7aa0 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20  nce_cleanup;.   
e7ab0 20 20 20 61 70 4e 65 77 5b 69 5d 20 3d 20 70 4e     apNew[i] = pN
e7ac0 65 77 3b 0a 20 20 20 20 20 20 6e 4e 65 77 2b 2b  ew;.      nNew++
e7ad0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 65 72 6f  ;.    }.    zero
e7ae0 50 61 67 65 28 70 4e 65 77 2c 20 70 61 67 65 46  Page(pNew, pageF
e7af0 6c 61 67 73 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  lags);.  }..  /*
e7b00 20 46 72 65 65 20 61 6e 79 20 6f 6c 64 20 70 61   Free any old pa
e7b10 67 65 73 20 74 68 61 74 20 77 65 72 65 20 6e 6f  ges that were no
e7b20 74 20 72 65 75 73 65 64 20 61 73 20 6e 65 77 20  t reused as new 
e7b30 70 61 67 65 73 2e 0a 20 20 2a 2f 0a 20 20 77 68  pages..  */.  wh
e7b40 69 6c 65 28 20 69 3c 6e 4f 6c 64 20 29 7b 0a 20  ile( i<nOld ){. 
e7b50 20 20 20 72 63 20 3d 20 66 72 65 65 50 61 67 65     rc = freePage
e7b60 28 61 70 4f 6c 64 5b 69 5d 29 3b 0a 20 20 20 20  (apOld[i]);.    
e7b70 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 62 61  if( rc ) goto ba
e7b80 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20  lance_cleanup;. 
e7b90 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 61     releasePage(a
e7ba0 70 4f 6c 64 5b 69 5d 29 3b 0a 20 20 20 20 61 70  pOld[i]);.    ap
e7bb0 4f 6c 64 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20  Old[i] = 0;.    
e7bc0 69 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20  i++;.  }..  /*. 
e7bd0 20 2a 2a 20 50 75 74 20 74 68 65 20 6e 65 77 20   ** Put the new 
e7be0 70 61 67 65 73 20 69 6e 20 61 63 63 65 6e 64 69  pages in accendi
e7bf0 6e 67 20 6f 72 64 65 72 2e 20 20 54 68 69 73 20  ng order.  This 
e7c00 68 65 6c 70 73 20 74 6f 0a 20 20 2a 2a 20 6b 65  helps to.  ** ke
e7c10 65 70 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68  ep entries in th
e7c20 65 20 64 69 73 6b 20 66 69 6c 65 20 69 6e 20 6f  e disk file in o
e7c30 72 64 65 72 20 73 6f 20 74 68 61 74 20 61 20 73  rder so that a s
e7c40 63 61 6e 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20  can.  ** of the 
e7c50 74 61 62 6c 65 20 69 73 20 61 20 6c 69 6e 65 61  table is a linea
e7c60 72 20 73 63 61 6e 20 74 68 72 6f 75 67 68 20 74  r scan through t
e7c70 68 65 20 66 69 6c 65 2e 20 20 54 68 61 74 0a 20  he file.  That. 
e7c80 20 2a 2a 20 69 6e 20 74 75 72 6e 20 68 65 6c 70   ** in turn help
e7c90 73 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20  s the operating 
e7ca0 73 79 73 74 65 6d 20 74 6f 20 64 65 6c 69 76 65  system to delive
e7cb0 72 20 70 61 67 65 73 0a 20 20 2a 2a 20 66 72 6f  r pages.  ** fro
e7cc0 6d 20 74 68 65 20 64 69 73 6b 20 6d 6f 72 65 20  m the disk more 
e7cd0 72 61 70 69 64 6c 79 2e 0a 20 20 2a 2a 0a 20 20  rapidly..  **.  
e7ce0 2a 2a 20 41 6e 20 4f 28 6e 5e 32 29 20 69 6e 73  ** An O(n^2) ins
e7cf0 65 72 74 69 6f 6e 20 73 6f 72 74 20 61 6c 67 6f  ertion sort algo
e7d00 72 69 74 68 6d 20 69 73 20 75 73 65 64 2c 20 62  rithm is used, b
e7d10 75 74 20 73 69 6e 63 65 0a 20 20 2a 2a 20 6e 20  ut since.  ** n 
e7d20 69 73 20 6e 65 76 65 72 20 6d 6f 72 65 20 74 68  is never more th
e7d30 61 6e 20 4e 42 20 28 61 20 73 6d 61 6c 6c 20 63  an NB (a small c
e7d40 6f 6e 73 74 61 6e 74 29 2c 20 74 68 61 74 20 73  onstant), that s
e7d50 68 6f 75 6c 64 0a 20 20 2a 2a 20 6e 6f 74 20 62  hould.  ** not b
e7d60 65 20 61 20 70 72 6f 62 6c 65 6d 2e 0a 20 20 2a  e a problem..  *
e7d70 2a 0a 20 20 2a 2a 20 57 68 65 6e 20 4e 42 3d 3d  *.  ** When NB==
e7d80 33 2c 20 74 68 69 73 20 6f 6e 65 20 6f 70 74 69  3, this one opti
e7d90 6d 69 7a 61 74 69 6f 6e 20 6d 61 6b 65 73 20 74  mization makes t
e7da0 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a  he database.  **
e7db0 20 61 62 6f 75 74 20 32 35 25 20 66 61 73 74 65   about 25% faste
e7dc0 72 20 66 6f 72 20 6c 61 72 67 65 20 69 6e 73 65  r for large inse
e7dd0 72 74 69 6f 6e 73 20 61 6e 64 20 64 65 6c 65 74  rtions and delet
e7de0 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  ions..  */.  for
e7df0 28 69 3d 30 3b 20 69 3c 6b 2d 31 3b 20 69 2b 2b  (i=0; i<k-1; i++
e7e00 29 7b 0a 20 20 20 20 69 6e 74 20 6d 69 6e 56 20  ){.    int minV 
e7e10 3d 20 70 67 6e 6f 4e 65 77 5b 69 5d 3b 0a 20 20  = pgnoNew[i];.  
e7e20 20 20 69 6e 74 20 6d 69 6e 49 20 3d 20 69 3b 0a    int minI = i;.
e7e30 20 20 20 20 66 6f 72 28 6a 3d 69 2b 31 3b 20 6a      for(j=i+1; j
e7e40 3c 6b 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  <k; j++){.      
e7e50 69 66 28 20 70 67 6e 6f 4e 65 77 5b 6a 5d 3c 28  if( pgnoNew[j]<(
e7e60 75 6e 73 69 67 6e 65 64 29 6d 69 6e 56 20 29 7b  unsigned)minV ){
e7e70 0a 20 20 20 20 20 20 20 20 6d 69 6e 49 20 3d 20  .        minI = 
e7e80 6a 3b 0a 20 20 20 20 20 20 20 20 6d 69 6e 56 20  j;.        minV 
e7e90 3d 20 70 67 6e 6f 4e 65 77 5b 6a 5d 3b 0a 20 20  = pgnoNew[j];.  
e7ea0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
e7eb0 69 66 28 20 6d 69 6e 49 3e 69 20 29 7b 0a 20 20  if( minI>i ){.  
e7ec0 20 20 20 20 69 6e 74 20 74 3b 0a 20 20 20 20 20      int t;.     
e7ed0 20 4d 65 6d 50 61 67 65 20 2a 70 54 3b 0a 20 20   MemPage *pT;.  
e7ee0 20 20 20 20 74 20 3d 20 70 67 6e 6f 4e 65 77 5b      t = pgnoNew[
e7ef0 69 5d 3b 0a 20 20 20 20 20 20 70 54 20 3d 20 61  i];.      pT = a
e7f00 70 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 20 20 70  pNew[i];.      p
e7f10 67 6e 6f 4e 65 77 5b 69 5d 20 3d 20 70 67 6e 6f  gnoNew[i] = pgno
e7f20 4e 65 77 5b 6d 69 6e 49 5d 3b 0a 20 20 20 20 20  New[minI];.     
e7f30 20 61 70 4e 65 77 5b 69 5d 20 3d 20 61 70 4e 65   apNew[i] = apNe
e7f40 77 5b 6d 69 6e 49 5d 3b 0a 20 20 20 20 20 20 70  w[minI];.      p
e7f50 67 6e 6f 4e 65 77 5b 6d 69 6e 49 5d 20 3d 20 74  gnoNew[minI] = t
e7f60 3b 0a 20 20 20 20 20 20 61 70 4e 65 77 5b 6d 69  ;.      apNew[mi
e7f70 6e 49 5d 20 3d 20 70 54 3b 0a 20 20 20 20 7d 0a  nI] = pT;.    }.
e7f80 20 20 7d 0a 20 20 54 52 41 43 45 28 28 22 42 41    }.  TRACE(("BA
e7f90 4c 41 4e 43 45 3a 20 6f 6c 64 3a 20 25 64 20 25  LANCE: old: %d %
e7fa0 64 20 25 64 20 20 6e 65 77 3a 20 25 64 28 25 64  d %d  new: %d(%d
e7fb0 29 20 25 64 28 25 64 29 20 25 64 28 25 64 29 20  ) %d(%d) %d(%d) 
e7fc0 25 64 28 25 64 29 20 25 64 28 25 64 29 5c 6e 22  %d(%d) %d(%d)\n"
e7fd0 2c 0a 20 20 20 20 70 67 6e 6f 4f 6c 64 5b 30 5d  ,.    pgnoOld[0]
e7fe0 2c 20 0a 20 20 20 20 6e 4f 6c 64 3e 3d 32 20 3f  , .    nOld>=2 ?
e7ff0 20 70 67 6e 6f 4f 6c 64 5b 31 5d 20 3a 20 30 2c   pgnoOld[1] : 0,
e8000 0a 20 20 20 20 6e 4f 6c 64 3e 3d 33 20 3f 20 70  .    nOld>=3 ? p
e8010 67 6e 6f 4f 6c 64 5b 32 5d 20 3a 20 30 2c 0a 20  gnoOld[2] : 0,. 
e8020 20 20 20 70 67 6e 6f 4e 65 77 5b 30 5d 2c 20 73     pgnoNew[0], s
e8030 7a 4e 65 77 5b 30 5d 2c 0a 20 20 20 20 6e 4e 65  zNew[0],.    nNe
e8040 77 3e 3d 32 20 3f 20 70 67 6e 6f 4e 65 77 5b 31  w>=2 ? pgnoNew[1
e8050 5d 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d 32 20 3f  ] : 0, nNew>=2 ?
e8060 20 73 7a 4e 65 77 5b 31 5d 20 3a 20 30 2c 0a 20   szNew[1] : 0,. 
e8070 20 20 20 6e 4e 65 77 3e 3d 33 20 3f 20 70 67 6e     nNew>=3 ? pgn
e8080 6f 4e 65 77 5b 32 5d 20 3a 20 30 2c 20 6e 4e 65  oNew[2] : 0, nNe
e8090 77 3e 3d 33 20 3f 20 73 7a 4e 65 77 5b 32 5d 20  w>=3 ? szNew[2] 
e80a0 3a 20 30 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d 34  : 0,.    nNew>=4
e80b0 20 3f 20 70 67 6e 6f 4e 65 77 5b 33 5d 20 3a 20   ? pgnoNew[3] : 
e80c0 30 2c 20 6e 4e 65 77 3e 3d 34 20 3f 20 73 7a 4e  0, nNew>=4 ? szN
e80d0 65 77 5b 33 5d 20 3a 20 30 2c 0a 20 20 20 20 6e  ew[3] : 0,.    n
e80e0 4e 65 77 3e 3d 35 20 3f 20 70 67 6e 6f 4e 65 77  New>=5 ? pgnoNew
e80f0 5b 34 5d 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d 35  [4] : 0, nNew>=5
e8100 20 3f 20 73 7a 4e 65 77 5b 34 5d 20 3a 20 30 29   ? szNew[4] : 0)
e8110 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 45 76  );..  /*.  ** Ev
e8120 65 6e 6c 79 20 64 69 73 74 72 69 62 75 74 65 20  enly distribute 
e8130 74 68 65 20 64 61 74 61 20 69 6e 20 61 70 43 65  the data in apCe
e8140 6c 6c 5b 5d 20 61 63 72 6f 73 73 20 74 68 65 20  ll[] across the 
e8150 6e 65 77 20 70 61 67 65 73 2e 0a 20 20 2a 2a 20  new pages..  ** 
e8160 49 6e 73 65 72 74 20 64 69 76 69 64 65 72 20 63  Insert divider c
e8170 65 6c 6c 73 20 69 6e 74 6f 20 70 50 61 72 65 6e  ells into pParen
e8180 74 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e 0a  t as necessary..
e8190 20 20 2a 2f 0a 20 20 6a 20 3d 20 30 3b 0a 20 20    */.  j = 0;.  
e81a0 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e 65 77 3b  for(i=0; i<nNew;
e81b0 20 69 2b 2b 29 7b 0a 20 20 20 20 2f 2a 20 41 73   i++){.    /* As
e81c0 73 65 6d 62 6c 65 20 74 68 65 20 6e 65 77 20 73  semble the new s
e81d0 69 62 6c 69 6e 67 20 70 61 67 65 2e 20 2a 2f 0a  ibling page. */.
e81e0 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65      MemPage *pNe
e81f0 77 20 3d 20 61 70 4e 65 77 5b 69 5d 3b 0a 20 20  w = apNew[i];.  
e8200 20 20 61 73 73 65 72 74 28 20 6a 3c 6e 4d 61 78    assert( j<nMax
e8210 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 61 73 73  Cells );.    ass
e8220 65 72 74 28 20 70 4e 65 77 2d 3e 70 67 6e 6f 3d  ert( pNew->pgno=
e8230 3d 70 67 6e 6f 4e 65 77 5b 69 5d 20 29 3b 0a 20  =pgnoNew[i] );. 
e8240 20 20 20 61 73 73 65 6d 62 6c 65 50 61 67 65 28     assemblePage(
e8250 70 4e 65 77 2c 20 63 6e 74 4e 65 77 5b 69 5d 2d  pNew, cntNew[i]-
e8260 6a 2c 20 26 61 70 43 65 6c 6c 5b 6a 5d 2c 20 26  j, &apCell[j], &
e8270 73 7a 43 65 6c 6c 5b 6a 5d 29 3b 0a 20 20 20 20  szCell[j]);.    
e8280 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e 43  assert( pNew->nC
e8290 65 6c 6c 3e 30 20 7c 7c 20 28 6e 4e 65 77 3d 3d  ell>0 || (nNew==
e82a0 31 20 26 26 20 63 6e 74 4e 65 77 5b 30 5d 3d 3d  1 && cntNew[0]==
e82b0 30 29 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  0) );.    assert
e82c0 28 20 70 4e 65 77 2d 3e 6e 4f 76 65 72 66 6c 6f  ( pNew->nOverflo
e82d0 77 3d 3d 30 20 29 3b 0a 0a 23 69 66 6e 64 65 66  w==0 );..#ifndef
e82e0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
e82f0 4f 56 41 43 55 55 4d 0a 20 20 20 20 2f 2a 20 49  OVACUUM.    /* I
e8300 66 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 74  f this is an aut
e8310 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73  o-vacuum databas
e8320 65 2c 20 75 70 64 61 74 65 20 74 68 65 20 70 6f  e, update the po
e8330 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 69 65  inter map entrie
e8340 73 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 70 6f  s.    ** that po
e8350 69 6e 74 20 74 6f 20 74 68 65 20 73 69 62 6c 69  int to the sibli
e8360 6e 67 73 20 74 68 61 74 20 77 65 72 65 20 72 65  ngs that were re
e8370 61 72 72 61 6e 67 65 64 2e 20 54 68 65 73 65 20  arranged. These 
e8380 63 61 6e 20 62 65 3a 20 6c 65 66 74 0a 20 20 20  can be: left.   
e8390 20 2a 2a 20 63 68 69 6c 64 72 65 6e 20 6f 66 20   ** children of 
e83a0 63 65 6c 6c 73 2c 20 74 68 65 20 72 69 67 68 74  cells, the right
e83b0 2d 63 68 69 6c 64 20 6f 66 20 74 68 65 20 70 61  -child of the pa
e83c0 67 65 2c 20 6f 72 20 6f 76 65 72 66 6c 6f 77 20  ge, or overflow 
e83d0 70 61 67 65 73 0a 20 20 20 20 2a 2a 20 70 6f 69  pages.    ** poi
e83e0 6e 74 65 64 20 74 6f 20 62 79 20 63 65 6c 6c 73  nted to by cells
e83f0 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
e8400 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d   pBt->autoVacuum
e8410 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6b 3d   ){.      for(k=
e8420 6a 3b 20 6b 3c 63 6e 74 4e 65 77 5b 69 5d 3b 20  j; k<cntNew[i]; 
e8430 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 61 73  k++){.        as
e8440 73 65 72 74 28 20 6b 3c 6e 4d 61 78 43 65 6c 6c  sert( k<nMaxCell
e8450 73 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  s );.        if(
e8460 20 61 46 72 6f 6d 5b 6b 5d 3d 3d 30 78 46 46 20   aFrom[k]==0xFF 
e8470 7c 7c 20 61 70 43 6f 70 79 5b 61 46 72 6f 6d 5b  || apCopy[aFrom[
e8480 6b 5d 5d 2d 3e 70 67 6e 6f 21 3d 70 4e 65 77 2d  k]]->pgno!=pNew-
e8490 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20  >pgno ){.       
e84a0 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75     rc = ptrmapPu
e84b0 74 4f 76 66 6c 28 70 4e 65 77 2c 20 6b 2d 6a 29  tOvfl(pNew, k-j)
e84c0 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
e84d0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
e84e0 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74  .            got
e84f0 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75  o balance_cleanu
e8500 70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  p;.          }. 
e8510 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
e8520 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  .    }.#endif.. 
e8530 20 20 20 6a 20 3d 20 63 6e 74 4e 65 77 5b 69 5d     j = cntNew[i]
e8540 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  ;..    /* If the
e8550 20 73 69 62 6c 69 6e 67 20 70 61 67 65 20 61 73   sibling page as
e8560 73 65 6d 62 6c 65 64 20 61 62 6f 76 65 20 77 61  sembled above wa
e8570 73 20 6e 6f 74 20 74 68 65 20 72 69 67 68 74 2d  s not the right-
e8580 6d 6f 73 74 20 73 69 62 6c 69 6e 67 2c 0a 20 20  most sibling,.  
e8590 20 20 2a 2a 20 69 6e 73 65 72 74 20 61 20 64 69    ** insert a di
e85a0 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 74 6f 20  vider cell into 
e85b0 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e  the parent page.
e85c0 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
e85d0 69 3c 6e 4e 65 77 2d 31 20 26 26 20 6a 3c 6e 43  i<nNew-1 && j<nC
e85e0 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 75 38 20  ell ){.      u8 
e85f0 2a 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20 75 38  *pCell;.      u8
e8600 20 2a 70 54 65 6d 70 3b 0a 20 20 20 20 20 20 69   *pTemp;.      i
e8610 6e 74 20 73 7a 3b 0a 0a 20 20 20 20 20 20 61 73  nt sz;..      as
e8620 73 65 72 74 28 20 6a 3c 6e 4d 61 78 43 65 6c 6c  sert( j<nMaxCell
e8630 73 20 29 3b 0a 20 20 20 20 20 20 70 43 65 6c 6c  s );.      pCell
e8640 20 3d 20 61 70 43 65 6c 6c 5b 6a 5d 3b 0a 20 20   = apCell[j];.  
e8650 20 20 20 20 73 7a 20 3d 20 73 7a 43 65 6c 6c 5b      sz = szCell[
e8660 6a 5d 20 2b 20 6c 65 61 66 43 6f 72 72 65 63 74  j] + leafCorrect
e8670 69 6f 6e 3b 0a 20 20 20 20 20 20 69 66 28 20 21  ion;.      if( !
e8680 70 4e 65 77 2d 3e 6c 65 61 66 20 29 7b 0a 20 20  pNew->leaf ){.  
e8690 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 4e        memcpy(&pN
e86a0 65 77 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 70 43  ew->aData[8], pC
e86b0 65 6c 6c 2c 20 34 29 3b 0a 20 20 20 20 20 20 20  ell, 4);.       
e86c0 20 70 54 65 6d 70 20 3d 20 30 3b 0a 20 20 20 20   pTemp = 0;.    
e86d0 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 65 61 66    }else if( leaf
e86e0 44 61 74 61 20 29 7b 0a 09 2f 2a 20 49 66 20 74  Data ){../* If t
e86f0 68 65 20 74 72 65 65 20 69 73 20 61 20 6c 65 61  he tree is a lea
e8700 66 2d 64 61 74 61 20 74 72 65 65 2c 20 61 6e 64  f-data tree, and
e8710 20 74 68 65 20 73 69 62 6c 69 6e 67 73 20 61 72   the siblings ar
e8720 65 20 6c 65 61 76 65 73 2c 20 0a 20 20 20 20 20  e leaves, .     
e8730 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 72 65     ** then there
e8740 20 69 73 20 6e 6f 20 64 69 76 69 64 65 72 20 63   is no divider c
e8750 65 6c 6c 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 2e  ell in apCell[].
e8760 20 49 6e 73 74 65 61 64 2c 20 74 68 65 20 64 69   Instead, the di
e8770 76 69 64 65 72 20 0a 20 20 20 20 20 20 20 20 2a  vider .        *
e8780 2a 20 63 65 6c 6c 20 63 6f 6e 73 69 73 74 73 20  * cell consists 
e8790 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 20 6b  of the integer k
e87a0 65 79 20 66 6f 72 20 74 68 65 20 72 69 67 68 74  ey for the right
e87b0 2d 6d 6f 73 74 20 63 65 6c 6c 20 6f 66 20 0a 20  -most cell of . 
e87c0 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 73 69         ** the si
e87d0 62 6c 69 6e 67 2d 70 61 67 65 20 61 73 73 65 6d  bling-page assem
e87e0 62 6c 65 64 20 61 62 6f 76 65 20 6f 6e 6c 79 2e  bled above only.
e87f0 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
e8800 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66      CellInfo inf
e8810 6f 3b 0a 20 20 20 20 20 20 20 20 6a 2d 2d 3b 0a  o;.        j--;.
e8820 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42          sqlite3B
e8830 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72  treeParseCellPtr
e8840 28 70 4e 65 77 2c 20 61 70 43 65 6c 6c 5b 6a 5d  (pNew, apCell[j]
e8850 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20  , &info);.      
e8860 20 20 70 43 65 6c 6c 20 3d 20 26 61 53 70 61 63    pCell = &aSpac
e8870 65 5b 69 53 70 61 63 65 5d 3b 0a 20 20 20 20 20  e[iSpace];.     
e8880 20 20 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 70 50     fillInCell(pP
e8890 61 72 65 6e 74 2c 20 70 43 65 6c 6c 2c 20 30 2c  arent, pCell, 0,
e88a0 20 69 6e 66 6f 2e 6e 4b 65 79 2c 20 30 2c 20 30   info.nKey, 0, 0
e88b0 2c 20 30 2c 20 26 73 7a 29 3b 0a 20 20 20 20 20  , 0, &sz);.     
e88c0 20 20 20 69 53 70 61 63 65 20 2b 3d 20 73 7a 3b     iSpace += sz;
e88d0 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
e88e0 20 69 53 70 61 63 65 3c 3d 70 42 74 2d 3e 70 61   iSpace<=pBt->pa
e88f0 67 65 53 69 7a 65 2a 35 20 29 3b 0a 20 20 20 20  geSize*5 );.    
e8900 20 20 20 20 70 54 65 6d 70 20 3d 20 30 3b 0a 20      pTemp = 0;. 
e8910 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
e8920 20 20 20 20 70 43 65 6c 6c 20 2d 3d 20 34 3b 0a      pCell -= 4;.
e8930 20 20 20 20 20 20 20 20 70 54 65 6d 70 20 3d 20          pTemp = 
e8940 26 61 53 70 61 63 65 5b 69 53 70 61 63 65 5d 3b  &aSpace[iSpace];
e8950 0a 20 20 20 20 20 20 20 20 69 53 70 61 63 65 20  .        iSpace 
e8960 2b 3d 20 73 7a 3b 0a 20 20 20 20 20 20 20 20 61  += sz;.        a
e8970 73 73 65 72 74 28 20 69 53 70 61 63 65 3c 3d 70  ssert( iSpace<=p
e8980 42 74 2d 3e 70 61 67 65 53 69 7a 65 2a 35 20 29  Bt->pageSize*5 )
e8990 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4f 62 73  ;.        /* Obs
e89a0 63 75 72 65 20 63 61 73 65 20 66 6f 72 20 6e 6f  cure case for no
e89b0 6e 2d 6c 65 61 66 2d 64 61 74 61 20 74 72 65 65  n-leaf-data tree
e89c0 73 3a 20 49 66 20 74 68 65 20 63 65 6c 6c 20 61  s: If the cell a
e89d0 74 20 70 43 65 6c 6c 20 77 61 73 0a 20 20 20 20  t pCell was.    
e89e0 20 20 20 20 2a 2a 20 70 72 65 76 69 6f 75 73 6c      ** previousl
e89f0 79 20 73 74 6f 72 65 64 20 6f 6e 20 61 20 6c 65  y stored on a le
e8a00 61 66 20 6e 6f 64 65 2c 20 61 6e 64 20 69 74 27  af node, and it'
e8a10 73 20 72 65 70 6f 72 74 65 64 20 73 69 7a 65 20  s reported size 
e8a20 77 61 73 20 34 0a 20 20 20 20 20 20 20 20 2a 2a  was 4.        **
e8a30 20 62 79 74 65 73 2c 20 74 68 65 6e 20 69 74 20   bytes, then it 
e8a40 6d 61 79 20 61 63 74 75 61 6c 6c 79 20 62 65 20  may actually be 
e8a50 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 69  smaller than thi
e8a60 73 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 28 73  s .        ** (s
e8a70 65 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 50  ee sqlite3BtreeP
e8a80 61 72 73 65 43 65 6c 6c 50 74 72 28 29 2c 20 34  arseCellPtr(), 4
e8a90 20 62 79 74 65 73 20 69 73 20 74 68 65 20 6d 69   bytes is the mi
e8aa0 6e 69 6d 75 6d 20 73 69 7a 65 20 6f 66 0a 20 20  nimum size of.  
e8ab0 20 20 20 20 20 20 2a 2a 20 61 6e 79 20 63 65 6c        ** any cel
e8ac0 6c 29 2e 20 42 75 74 20 69 74 27 73 20 69 6d 70  l). But it's imp
e8ad0 6f 72 74 61 6e 74 20 74 6f 20 70 61 73 73 20 74  ortant to pass t
e8ae0 68 65 20 63 6f 72 72 65 63 74 20 73 69 7a 65 20  he correct size 
e8af0 74 6f 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 69  to .        ** i
e8b00 6e 73 65 72 74 43 65 6c 6c 28 29 2c 20 73 6f 20  nsertCell(), so 
e8b10 72 65 70 61 72 73 65 20 74 68 65 20 63 65 6c 6c  reparse the cell
e8b20 20 6e 6f 77 2e 0a 20 20 20 20 20 20 20 20 2a 2a   now..        **
e8b30 0a 20 20 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65  .        ** Note
e8b40 20 74 68 61 74 20 74 68 69 73 20 63 61 6e 20 6e   that this can n
e8b50 65 76 65 72 20 68 61 70 70 65 6e 20 69 6e 20 61  ever happen in a
e8b60 6e 20 53 51 4c 69 74 65 20 64 61 74 61 20 66 69  n SQLite data fi
e8b70 6c 65 2c 20 61 73 20 61 6c 6c 0a 20 20 20 20 20  le, as all.     
e8b80 20 20 20 2a 2a 20 63 65 6c 6c 73 20 61 72 65 20     ** cells are 
e8b90 61 74 20 6c 65 61 73 74 20 34 20 62 79 74 65 73  at least 4 bytes
e8ba0 2e 20 49 74 20 6f 6e 6c 79 20 68 61 70 70 65 6e  . It only happen
e8bb0 73 20 69 6e 20 62 2d 74 72 65 65 73 20 75 73 65  s in b-trees use
e8bc0 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20  d.        ** to 
e8bd0 65 76 61 6c 75 61 74 65 20 22 49 4e 20 28 53 45  evaluate "IN (SE
e8be0 4c 45 43 54 20 2e 2e 2e 29 22 20 61 6e 64 20 73  LECT ...)" and s
e8bf0 69 6d 69 6c 61 72 20 63 6c 61 75 73 65 73 2e 0a  imilar clauses..
e8c00 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
e8c10 20 20 20 69 66 28 20 73 7a 43 65 6c 6c 5b 6a 5d     if( szCell[j]
e8c20 3d 3d 34 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==4 ){.         
e8c30 20 61 73 73 65 72 74 28 6c 65 61 66 43 6f 72 72   assert(leafCorr
e8c40 65 63 74 69 6f 6e 3d 3d 34 29 3b 0a 20 20 20 20  ection==4);.    
e8c50 20 20 20 20 20 20 73 7a 20 3d 20 63 65 6c 6c 53        sz = cellS
e8c60 69 7a 65 50 74 72 28 70 50 61 72 65 6e 74 2c 20  izePtr(pParent, 
e8c70 70 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20  pCell);.        
e8c80 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
e8c90 72 63 20 3d 20 69 6e 73 65 72 74 43 65 6c 6c 28  rc = insertCell(
e8ca0 70 50 61 72 65 6e 74 2c 20 6e 78 44 69 76 2c 20  pParent, nxDiv, 
e8cb0 70 43 65 6c 6c 2c 20 73 7a 2c 20 70 54 65 6d 70  pCell, sz, pTemp
e8cc0 2c 20 34 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , 4);.      if( 
e8cd0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
e8ce0 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65  goto balance_cle
e8cf0 61 6e 75 70 3b 0a 20 20 20 20 20 20 70 75 74 34  anup;.      put4
e8d00 62 79 74 65 28 66 69 6e 64 4f 76 65 72 66 6c 6f  byte(findOverflo
e8d10 77 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 6e 78  wCell(pParent,nx
e8d20 44 69 76 29 2c 20 70 4e 65 77 2d 3e 70 67 6e 6f  Div), pNew->pgno
e8d30 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
e8d40 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
e8d50 4d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68  M.      /* If th
e8d60 69 73 20 69 73 20 61 6e 20 61 75 74 6f 2d 76 61  is is an auto-va
e8d70 63 75 75 6d 20 64 61 74 61 62 61 73 65 2c 20 61  cuum database, a
e8d80 6e 64 20 6e 6f 74 20 61 20 6c 65 61 66 2d 64 61  nd not a leaf-da
e8d90 74 61 20 74 72 65 65 2c 0a 20 20 20 20 20 20 2a  ta tree,.      *
e8da0 2a 20 74 68 65 6e 20 75 70 64 61 74 65 20 74 68  * then update th
e8db0 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 77 69  e pointer map wi
e8dc0 74 68 20 61 6e 20 65 6e 74 72 79 20 66 6f 72 20  th an entry for 
e8dd0 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  the overflow pag
e8de0 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20  e.      ** that 
e8df0 74 68 65 20 63 65 6c 6c 20 6a 75 73 74 20 69 6e  the cell just in
e8e00 73 65 72 74 65 64 20 70 6f 69 6e 74 73 20 74 6f  serted points to
e8e10 20 28 69 66 20 61 6e 79 29 2e 0a 20 20 20 20 20   (if any)..     
e8e20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 42   */.      if( pB
e8e30 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 26  t->autoVacuum &&
e8e40 20 21 6c 65 61 66 44 61 74 61 20 29 7b 0a 20 20   !leafData ){.  
e8e50 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61        rc = ptrma
e8e60 70 50 75 74 4f 76 66 6c 28 70 50 61 72 65 6e 74  pPutOvfl(pParent
e8e70 2c 20 6e 78 44 69 76 29 3b 0a 20 20 20 20 20 20  , nxDiv);.      
e8e80 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
e8e90 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
e8ea0 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c   goto balance_cl
e8eb0 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 20 20 7d  eanup;.        }
e8ec0 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  .      }.#endif.
e8ed0 20 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 20        j++;.     
e8ee0 20 6e 78 44 69 76 2b 2b 3b 0a 20 20 20 20 7d 0a   nxDiv++;.    }.
e8ef0 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6a 3d    }.  assert( j=
e8f00 3d 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 73 73 65  =nCell );.  asse
e8f10 72 74 28 20 6e 4f 6c 64 3e 30 20 29 3b 0a 20 20  rt( nOld>0 );.  
e8f20 61 73 73 65 72 74 28 20 6e 4e 65 77 3e 30 20 29  assert( nNew>0 )
e8f30 3b 0a 20 20 69 66 28 20 28 70 61 67 65 46 6c 61  ;.  if( (pageFla
e8f40 67 73 20 26 20 50 54 46 5f 4c 45 41 46 29 3d 3d  gs & PTF_LEAF)==
e8f50 30 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28  0 ){.    memcpy(
e8f60 26 61 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d 2d 3e  &apNew[nNew-1]->
e8f70 61 44 61 74 61 5b 38 5d 2c 20 26 61 70 43 6f 70  aData[8], &apCop
e8f80 79 5b 6e 4f 6c 64 2d 31 5d 2d 3e 61 44 61 74 61  y[nOld-1]->aData
e8f90 5b 38 5d 2c 20 34 29 3b 0a 20 20 7d 0a 20 20 69  [8], 4);.  }.  i
e8fa0 66 28 20 6e 78 44 69 76 3d 3d 70 50 61 72 65 6e  f( nxDiv==pParen
e8fb0 74 2d 3e 6e 43 65 6c 6c 2b 70 50 61 72 65 6e 74  t->nCell+pParent
e8fc0 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20  ->nOverflow ){. 
e8fd0 20 20 20 2f 2a 20 52 69 67 68 74 2d 6d 6f 73 74     /* Right-most
e8fe0 20 73 69 62 6c 69 6e 67 20 69 73 20 74 68 65 20   sibling is the 
e8ff0 72 69 67 68 74 2d 6d 6f 73 74 20 63 68 69 6c 64  right-most child
e9000 20 6f 66 20 70 50 61 72 65 6e 74 20 2a 2f 0a 20   of pParent */. 
e9010 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61     put4byte(&pPa
e9020 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72  rent->aData[pPar
e9030 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38  ent->hdrOffset+8
e9040 5d 2c 20 70 67 6e 6f 4e 65 77 5b 6e 4e 65 77 2d  ], pgnoNew[nNew-
e9050 31 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  1]);.  }else{.  
e9060 20 20 2f 2a 20 52 69 67 68 74 2d 6d 6f 73 74 20    /* Right-most 
e9070 73 69 62 6c 69 6e 67 20 69 73 20 74 68 65 20 6c  sibling is the l
e9080 65 66 74 20 63 68 69 6c 64 20 6f 66 20 74 68 65  eft child of the
e9090 20 66 69 72 73 74 20 65 6e 74 72 79 20 69 6e 20   first entry in 
e90a0 70 50 61 72 65 6e 74 0a 20 20 20 20 2a 2a 20 70  pParent.    ** p
e90b0 61 73 74 20 74 68 65 20 72 69 67 68 74 2d 6d 6f  ast the right-mo
e90c0 73 74 20 64 69 76 69 64 65 72 20 65 6e 74 72 79  st divider entry
e90d0 20 2a 2f 0a 20 20 20 20 70 75 74 34 62 79 74 65   */.    put4byte
e90e0 28 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43 65 6c  (findOverflowCel
e90f0 6c 28 70 50 61 72 65 6e 74 2c 20 6e 78 44 69 76  l(pParent, nxDiv
e9100 29 2c 20 70 67 6e 6f 4e 65 77 5b 6e 4e 65 77 2d  ), pgnoNew[nNew-
e9110 31 5d 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20  1]);.  }..  /*. 
e9120 20 2a 2a 20 52 65 70 61 72 65 6e 74 20 63 68 69   ** Reparent chi
e9130 6c 64 72 65 6e 20 6f 66 20 61 6c 6c 20 63 65 6c  ldren of all cel
e9140 6c 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69  ls..  */.  for(i
e9150 3d 30 3b 20 69 3c 6e 4e 65 77 3b 20 69 2b 2b 29  =0; i<nNew; i++)
e9160 7b 0a 20 20 20 20 72 63 20 3d 20 72 65 70 61 72  {.    rc = repar
e9170 65 6e 74 43 68 69 6c 64 50 61 67 65 73 28 61 70  entChildPages(ap
e9180 4e 65 77 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28  New[i]);.    if(
e9190 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
e91a0 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c   goto balance_cl
e91b0 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 72 63 20  eanup;.  }.  rc 
e91c0 3d 20 72 65 70 61 72 65 6e 74 43 68 69 6c 64 50  = reparentChildP
e91d0 61 67 65 73 28 70 50 61 72 65 6e 74 29 3b 0a 20  ages(pParent);. 
e91e0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
e91f0 4f 4b 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63  OK ) goto balanc
e9200 65 5f 63 6c 65 61 6e 75 70 3b 0a 0a 20 20 2f 2a  e_cleanup;..  /*
e9210 0a 20 20 2a 2a 20 42 61 6c 61 6e 63 65 20 74 68  .  ** Balance th
e9220 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e 20 20  e parent page.  
e9230 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 63 75  Note that the cu
e9240 72 72 65 6e 74 20 70 61 67 65 20 28 70 50 61 67  rrent page (pPag
e9250 65 29 20 6d 69 67 68 74 0a 20 20 2a 2a 20 68 61  e) might.  ** ha
e9260 76 65 20 62 65 65 6e 20 61 64 64 65 64 20 74 6f  ve been added to
e9270 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 73 6f   the freelist so
e9280 20 69 74 20 6d 69 67 68 74 20 6e 6f 20 6c 6f 6e   it might no lon
e9290 67 65 72 20 62 65 20 69 6e 69 74 69 61 6c 69 7a  ger be initializ
e92a0 65 64 2e 0a 20 20 2a 2a 20 42 75 74 20 74 68 65  ed..  ** But the
e92b0 20 70 61 72 65 6e 74 20 70 61 67 65 20 77 69 6c   parent page wil
e92c0 6c 20 61 6c 77 61 79 73 20 62 65 20 69 6e 69 74  l always be init
e92d0 69 61 6c 69 7a 65 64 2e 0a 20 20 2a 2f 0a 20 20  ialized..  */.  
e92e0 61 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 2d  assert( pParent-
e92f0 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 72 63 20  >isInit );.  rc 
e9300 3d 20 62 61 6c 61 6e 63 65 28 70 50 61 72 65 6e  = balance(pParen
e9310 74 2c 20 30 29 3b 0a 20 20 0a 20 20 2f 2a 0a 20  t, 0);.  .  /*. 
e9320 20 2a 2a 20 43 6c 65 61 6e 75 70 20 62 65 66 6f   ** Cleanup befo
e9330 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 20 20  re returning..  
e9340 2a 2f 0a 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e  */.balance_clean
e9350 75 70 3a 0a 20 20 73 71 6c 69 74 65 46 72 65 65  up:.  sqliteFree
e9360 28 61 70 43 65 6c 6c 29 3b 0a 20 20 66 6f 72 28  (apCell);.  for(
e9370 69 3d 30 3b 20 69 3c 6e 4f 6c 64 3b 20 69 2b 2b  i=0; i<nOld; i++
e9380 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61  ){.    releasePa
e9390 67 65 28 61 70 4f 6c 64 5b 69 5d 29 3b 0a 20 20  ge(apOld[i]);.  
e93a0 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e  }.  for(i=0; i<n
e93b0 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 72  New; i++){.    r
e93c0 65 6c 65 61 73 65 50 61 67 65 28 61 70 4e 65 77  eleasePage(apNew
e93d0 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 72 65 6c 65  [i]);.  }.  rele
e93e0 61 73 65 50 61 67 65 28 70 50 61 72 65 6e 74 29  asePage(pParent)
e93f0 3b 0a 20 20 54 52 41 43 45 28 28 22 42 41 4c 41  ;.  TRACE(("BALA
e9400 4e 43 45 3a 20 66 69 6e 69 73 68 65 64 20 77 69  NCE: finished wi
e9410 74 68 20 25 64 3a 20 6f 6c 64 3d 25 64 20 6e 65  th %d: old=%d ne
e9420 77 3d 25 64 20 63 65 6c 6c 73 3d 25 64 5c 6e 22  w=%d cells=%d\n"
e9430 2c 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 67  ,.          pPag
e9440 65 2d 3e 70 67 6e 6f 2c 20 6e 4f 6c 64 2c 20 6e  e->pgno, nOld, n
e9450 4e 65 77 2c 20 6e 43 65 6c 6c 29 29 3b 0a 20 20  New, nCell));.  
e9460 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
e9470 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
e9480 20 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 74   is called for t
e9490 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20  he root page of 
e94a0 61 20 62 74 72 65 65 20 77 68 65 6e 20 74 68 65  a btree when the
e94b0 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 63 6f   root.** page co
e94c0 6e 74 61 69 6e 73 20 6e 6f 20 63 65 6c 6c 73 2e  ntains no cells.
e94d0 20 20 54 68 69 73 20 69 73 20 61 6e 20 6f 70 70    This is an opp
e94e0 6f 72 74 75 6e 69 74 79 20 74 6f 20 6d 61 6b 65  ortunity to make
e94f0 20 74 68 65 20 74 72 65 65 0a 2a 2a 20 73 68 61   the tree.** sha
e9500 6c 6c 6f 77 65 72 20 62 79 20 6f 6e 65 20 6c 65  llower by one le
e9510 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  vel..*/.static i
e9520 6e 74 20 62 61 6c 61 6e 63 65 5f 73 68 61 6c 6c  nt balance_shall
e9530 6f 77 65 72 28 4d 65 6d 50 61 67 65 20 2a 70 50  ower(MemPage *pP
e9540 61 67 65 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20  age){.  MemPage 
e9550 2a 70 43 68 69 6c 64 3b 20 20 20 20 20 20 20 20  *pChild;        
e9560 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 6e 6c 79       /* The only
e9570 20 63 68 69 6c 64 20 70 61 67 65 20 6f 66 20 70   child page of p
e9580 50 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70  Page */.  Pgno p
e9590 67 6e 6f 43 68 69 6c 64 3b 20 20 20 20 20 20 20  gnoChild;       
e95a0 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e         /* Page n
e95b0 75 6d 62 65 72 20 66 6f 72 20 70 43 68 69 6c 64  umber for pChild
e95c0 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53   */.  int rc = S
e95d0 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20  QLITE_OK;       
e95e0 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
e95f0 65 20 66 72 6f 6d 20 73 75 62 70 72 6f 63 65 64  e from subproced
e9600 75 72 65 73 20 2a 2f 0a 20 20 42 74 53 68 61 72  ures */.  BtShar
e9610 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20  ed *pBt;        
e9620 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
e9630 20 6d 61 69 6e 20 42 54 72 65 65 20 73 74 72 75   main BTree stru
e9640 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6d  cture */.  int m
e9650 78 43 65 6c 6c 50 65 72 50 61 67 65 3b 20 20 20  xCellPerPage;   
e9660 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d          /* Maxim
e9670 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c  um number of cel
e9680 6c 73 20 70 65 72 20 70 61 67 65 20 2a 2f 0a 20  ls per page */. 
e9690 20 75 38 20 2a 2a 61 70 43 65 6c 6c 3b 20 20 20   u8 **apCell;   
e96a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e96b0 20 41 6c 6c 20 63 65 6c 6c 73 20 66 72 6f 6d 20   All cells from 
e96c0 70 61 67 65 73 20 62 65 69 6e 67 20 62 61 6c 61  pages being bala
e96d0 6e 63 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 73  nced */.  int *s
e96e0 7a 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20  zCell;          
e96f0 20 20 20 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20         /* Local 
e9700 73 69 7a 65 20 6f 66 20 61 6c 6c 20 63 65 6c 6c  size of all cell
e9710 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  s */..  assert( 
e9720 70 50 61 67 65 2d 3e 70 50 61 72 65 6e 74 3d 3d  pPage->pParent==
e9730 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
e9740 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29  Page->nCell==0 )
e9750 3b 0a 20 20 70 42 74 20 3d 20 70 50 61 67 65 2d  ;.  pBt = pPage-
e9760 3e 70 42 74 3b 0a 20 20 6d 78 43 65 6c 6c 50 65  >pBt;.  mxCellPe
e9770 72 50 61 67 65 20 3d 20 4d 58 5f 43 45 4c 4c 28  rPage = MX_CELL(
e9780 70 42 74 29 3b 0a 20 20 61 70 43 65 6c 6c 20 3d  pBt);.  apCell =
e9790 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 52 61 77   sqliteMallocRaw
e97a0 28 20 6d 78 43 65 6c 6c 50 65 72 50 61 67 65 2a  ( mxCellPerPage*
e97b0 28 73 69 7a 65 6f 66 28 75 38 2a 29 2b 73 69 7a  (sizeof(u8*)+siz
e97c0 65 6f 66 28 69 6e 74 29 29 20 29 3b 0a 20 20 69  eof(int)) );.  i
e97d0 66 28 20 61 70 43 65 6c 6c 3d 3d 30 20 29 20 72  f( apCell==0 ) r
e97e0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
e97f0 45 4d 3b 0a 20 20 73 7a 43 65 6c 6c 20 3d 20 28  EM;.  szCell = (
e9800 69 6e 74 2a 29 26 61 70 43 65 6c 6c 5b 6d 78 43  int*)&apCell[mxC
e9810 65 6c 6c 50 65 72 50 61 67 65 5d 3b 0a 20 20 69  ellPerPage];.  i
e9820 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  f( pPage->leaf )
e9830 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62  {.    /* The tab
e9840 6c 65 20 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79  le is completely
e9850 20 65 6d 70 74 79 20 2a 2f 0a 20 20 20 20 54 52   empty */.    TR
e9860 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 65  ACE(("BALANCE: e
e9870 6d 70 74 79 20 74 61 62 6c 65 20 25 64 5c 6e 22  mpty table %d\n"
e9880 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 29 3b  , pPage->pgno));
e9890 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
e98a0 20 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20 69   The root page i
e98b0 73 20 65 6d 70 74 79 20 62 75 74 20 68 61 73 20  s empty but has 
e98c0 6f 6e 65 20 63 68 69 6c 64 2e 20 20 54 72 61 6e  one child.  Tran
e98d0 73 66 65 72 20 74 68 65 0a 20 20 20 20 2a 2a 20  sfer the.    ** 
e98e0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d  information from
e98f0 20 74 68 61 74 20 6f 6e 65 20 63 68 69 6c 64 20   that one child 
e9900 69 6e 74 6f 20 74 68 65 20 72 6f 6f 74 20 70 61  into the root pa
e9910 67 65 20 69 66 20 69 74 20 0a 20 20 20 20 2a 2a  ge if it .    **
e9920 20 77 69 6c 6c 20 66 69 74 2e 20 20 54 68 69 73   will fit.  This
e9930 20 72 65 64 75 63 65 73 20 74 68 65 20 64 65 70   reduces the dep
e9940 74 68 20 6f 66 20 74 68 65 20 74 72 65 65 20 62  th of the tree b
e9950 79 20 6f 6e 65 2e 0a 20 20 20 20 2a 2a 0a 20 20  y one..    **.  
e9960 20 20 2a 2a 20 49 66 20 74 68 65 20 72 6f 6f 74    ** If the root
e9970 20 70 61 67 65 20 69 73 20 70 61 67 65 20 31 2c   page is page 1,
e9980 20 69 74 20 68 61 73 20 6c 65 73 73 20 73 70 61   it has less spa
e9990 63 65 20 61 76 61 69 6c 61 62 6c 65 20 74 68 61  ce available tha
e99a0 6e 0a 20 20 20 20 2a 2a 20 69 74 73 20 63 68 69  n.    ** its chi
e99b0 6c 64 20 28 64 75 65 20 74 6f 20 74 68 65 20 31  ld (due to the 1
e99c0 30 30 20 62 79 74 65 20 68 65 61 64 65 72 20 74  00 byte header t
e99d0 68 61 74 20 6f 63 63 75 72 73 20 61 74 20 74 68  hat occurs at th
e99e0 65 20 62 65 67 69 6e 6e 69 6e 67 0a 20 20 20 20  e beginning.    
e99f0 2a 2a 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ** of the databa
e9a00 73 65 20 66 6c 65 29 2c 20 73 6f 20 69 74 20 6d  se fle), so it m
e9a10 69 67 68 74 20 6e 6f 74 20 62 65 20 61 62 6c 65  ight not be able
e9a20 20 74 6f 20 68 6f 6c 64 20 61 6c 6c 20 6f 66 20   to hold all of 
e9a30 74 68 65 20 0a 20 20 20 20 2a 2a 20 69 6e 66 6f  the .    ** info
e9a40 72 6d 61 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c  rmation currentl
e9a50 79 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74  y contained in t
e9a60 68 65 20 63 68 69 6c 64 2e 20 20 49 66 20 74 68  he child.  If th
e9a70 69 73 20 69 73 20 74 68 65 20 0a 20 20 20 20 2a  is is the .    *
e9a80 2a 20 63 61 73 65 2c 20 74 68 65 6e 20 64 6f 20  * case, then do 
e9a90 6e 6f 74 20 64 6f 20 74 68 65 20 74 72 61 6e 73  not do the trans
e9aa0 66 65 72 2e 20 20 4c 65 61 76 65 20 70 61 67 65  fer.  Leave page
e9ab0 20 31 20 65 6d 70 74 79 20 65 78 63 65 70 74 0a   1 empty except.
e9ac0 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 72      ** for the r
e9ad0 69 67 68 74 2d 70 6f 69 6e 74 65 72 20 74 6f 20  ight-pointer to 
e9ae0 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 2e 20  the child page. 
e9af0 20 54 68 65 20 63 68 69 6c 64 20 70 61 67 65 20   The child page 
e9b00 62 65 63 6f 6d 65 73 0a 20 20 20 20 2a 2a 20 74  becomes.    ** t
e9b10 68 65 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20  he virtual root 
e9b20 6f 66 20 74 68 65 20 74 72 65 65 2e 0a 20 20 20  of the tree..   
e9b30 20 2a 2f 0a 20 20 20 20 70 67 6e 6f 43 68 69 6c   */.    pgnoChil
e9b40 64 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50  d = get4byte(&pP
e9b50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65  age->aData[pPage
e9b60 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b  ->hdrOffset+8]);
e9b70 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 67 6e  .    assert( pgn
e9b80 6f 43 68 69 6c 64 3e 30 20 29 3b 0a 20 20 20 20  oChild>0 );.    
e9b90 61 73 73 65 72 74 28 20 70 67 6e 6f 43 68 69 6c  assert( pgnoChil
e9ba0 64 3c 3d 73 71 6c 69 74 65 33 50 61 67 65 72 50  d<=sqlite3PagerP
e9bb0 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 2d 3e  agecount(pPage->
e9bc0 70 42 74 2d 3e 70 50 61 67 65 72 29 20 29 3b 0a  pBt->pPager) );.
e9bd0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
e9be0 42 74 72 65 65 47 65 74 50 61 67 65 28 70 50 61  BtreeGetPage(pPa
e9bf0 67 65 2d 3e 70 42 74 2c 20 70 67 6e 6f 43 68 69  ge->pBt, pgnoChi
e9c00 6c 64 2c 20 26 70 43 68 69 6c 64 2c 20 30 29 3b  ld, &pChild, 0);
e9c10 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f  .    if( rc ) go
e9c20 74 6f 20 65 6e 64 5f 73 68 61 6c 6c 6f 77 5f 62  to end_shallow_b
e9c30 61 6c 61 6e 63 65 3b 0a 20 20 20 20 69 66 28 20  alance;.    if( 
e9c40 70 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d 31 20 29  pPage->pgno==1 )
e9c50 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
e9c60 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67  ite3BtreeInitPag
e9c70 65 28 70 43 68 69 6c 64 2c 20 70 50 61 67 65 29  e(pChild, pPage)
e9c80 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29  ;.      if( rc )
e9c90 20 67 6f 74 6f 20 65 6e 64 5f 73 68 61 6c 6c 6f   goto end_shallo
e9ca0 77 5f 62 61 6c 61 6e 63 65 3b 0a 20 20 20 20 20  w_balance;.     
e9cb0 20 61 73 73 65 72 74 28 20 70 43 68 69 6c 64 2d   assert( pChild-
e9cc0 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b  >nOverflow==0 );
e9cd0 0a 20 20 20 20 20 20 69 66 28 20 70 43 68 69 6c  .      if( pChil
e9ce0 64 2d 3e 6e 46 72 65 65 3e 3d 31 30 30 20 29 7b  d->nFree>=100 ){
e9cf0 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  .        /* The 
e9d00 63 68 69 6c 64 20 69 6e 66 6f 72 6d 61 74 69 6f  child informatio
e9d10 6e 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 74 68  n will fit on th
e9d20 65 20 72 6f 6f 74 20 70 61 67 65 2c 20 73 6f 20  e root page, so 
e9d30 64 6f 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a  do the.        *
e9d40 2a 20 63 6f 70 79 20 2a 2f 0a 20 20 20 20 20 20  * copy */.      
e9d50 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20    int i;.       
e9d60 20 7a 65 72 6f 50 61 67 65 28 70 50 61 67 65 2c   zeroPage(pPage,
e9d70 20 70 43 68 69 6c 64 2d 3e 61 44 61 74 61 5b 30   pChild->aData[0
e9d80 5d 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28  ]);.        for(
e9d90 69 3d 30 3b 20 69 3c 70 43 68 69 6c 64 2d 3e 6e  i=0; i<pChild->n
e9da0 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Cell; i++){.    
e9db0 20 20 20 20 20 20 61 70 43 65 6c 6c 5b 69 5d 20        apCell[i] 
e9dc0 3d 20 66 69 6e 64 43 65 6c 6c 28 70 43 68 69 6c  = findCell(pChil
e9dd0 64 2c 69 29 3b 0a 20 20 20 20 20 20 20 20 20 20  d,i);.          
e9de0 73 7a 43 65 6c 6c 5b 69 5d 20 3d 20 63 65 6c 6c  szCell[i] = cell
e9df0 53 69 7a 65 50 74 72 28 70 43 68 69 6c 64 2c 20  SizePtr(pChild, 
e9e00 61 70 43 65 6c 6c 5b 69 5d 29 3b 0a 20 20 20 20  apCell[i]);.    
e9e10 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 73      }.        as
e9e20 73 65 6d 62 6c 65 50 61 67 65 28 70 50 61 67 65  semblePage(pPage
e9e30 2c 20 70 43 68 69 6c 64 2d 3e 6e 43 65 6c 6c 2c  , pChild->nCell,
e9e40 20 61 70 43 65 6c 6c 2c 20 73 7a 43 65 6c 6c 29   apCell, szCell)
e9e50 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70  ;.        /* Cop
e9e60 79 20 74 68 65 20 72 69 67 68 74 2d 70 6f 69 6e  y the right-poin
e9e70 74 65 72 20 6f 66 20 74 68 65 20 63 68 69 6c 64  ter of the child
e9e80 20 74 6f 20 74 68 65 20 70 61 72 65 6e 74 2e 20   to the parent. 
e9e90 2a 2f 0a 20 20 20 20 20 20 20 20 70 75 74 34 62  */.        put4b
e9ea0 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74  yte(&pPage->aDat
e9eb0 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73  a[pPage->hdrOffs
e9ec0 65 74 2b 38 5d 2c 20 0a 20 20 20 20 20 20 20 20  et+8], .        
e9ed0 20 20 20 20 67 65 74 34 62 79 74 65 28 26 70 43      get4byte(&pC
e9ee0 68 69 6c 64 2d 3e 61 44 61 74 61 5b 70 43 68 69  hild->aData[pChi
e9ef0 6c 64 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d  ld->hdrOffset+8]
e9f00 29 29 3b 0a 20 20 20 20 20 20 20 20 66 72 65 65  ));.        free
e9f10 50 61 67 65 28 70 43 68 69 6c 64 29 3b 0a 20 20  Page(pChild);.  
e9f20 20 20 20 20 20 20 54 52 41 43 45 28 28 22 42 41        TRACE(("BA
e9f30 4c 41 4e 43 45 3a 20 63 68 69 6c 64 20 25 64 20  LANCE: child %d 
e9f40 74 72 61 6e 73 66 65 72 20 74 6f 20 70 61 67 65  transfer to page
e9f50 20 31 5c 6e 22 2c 20 70 43 68 69 6c 64 2d 3e 70   1\n", pChild->p
e9f60 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 7d 65 6c  gno));.      }el
e9f70 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54  se{.        /* T
e9f80 68 65 20 63 68 69 6c 64 20 68 61 73 20 6d 6f 72  he child has mor
e9f90 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68  e information th
e9fa0 61 74 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 74  at will fit on t
e9fb0 68 65 20 72 6f 6f 74 2e 0a 20 20 20 20 20 20 20  he root..       
e9fc0 20 2a 2a 20 54 68 65 20 74 72 65 65 20 69 73 20   ** The tree is 
e9fd0 61 6c 72 65 61 64 79 20 62 61 6c 61 6e 63 65 64  already balanced
e9fe0 2e 20 20 44 6f 20 6e 6f 74 68 69 6e 67 2e 20 2a  .  Do nothing. *
e9ff0 2f 0a 20 20 20 20 20 20 20 20 54 52 41 43 45 28  /.        TRACE(
ea000 28 22 42 41 4c 41 4e 43 45 3a 20 63 68 69 6c 64  ("BALANCE: child
ea010 20 25 64 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74   %d will not fit
ea020 20 6f 6e 20 70 61 67 65 20 31 5c 6e 22 2c 20 70   on page 1\n", p
ea030 43 68 69 6c 64 2d 3e 70 67 6e 6f 29 29 3b 0a 20  Child->pgno));. 
ea040 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
ea050 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70  {.      memcpy(p
ea060 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 70 43 68  Page->aData, pCh
ea070 69 6c 64 2d 3e 61 44 61 74 61 2c 20 70 50 61 67  ild->aData, pPag
ea080 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  e->pBt->usableSi
ea090 7a 65 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65  ze);.      pPage
ea0a0 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20  ->isInit = 0;.  
ea0b0 20 20 20 20 70 50 61 67 65 2d 3e 70 50 61 72 65      pPage->pPare
ea0c0 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63  nt = 0;.      rc
ea0d0 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 49   = sqlite3BtreeI
ea0e0 6e 69 74 50 61 67 65 28 70 50 61 67 65 2c 20 30  nitPage(pPage, 0
ea0f0 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
ea100 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
ea110 3b 0a 20 20 20 20 20 20 66 72 65 65 50 61 67 65  ;.      freePage
ea120 28 70 43 68 69 6c 64 29 3b 0a 20 20 20 20 20 20  (pChild);.      
ea130 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a  TRACE(("BALANCE:
ea140 20 74 72 61 6e 73 66 65 72 20 63 68 69 6c 64 20   transfer child 
ea150 25 64 20 69 6e 74 6f 20 72 6f 6f 74 20 25 64 5c  %d into root %d\
ea160 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
ea170 20 20 70 43 68 69 6c 64 2d 3e 70 67 6e 6f 2c 20    pChild->pgno, 
ea180 70 50 61 67 65 2d 3e 70 67 6e 6f 29 29 3b 0a 20  pPage->pgno));. 
ea190 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 72 65     }.    rc = re
ea1a0 70 61 72 65 6e 74 43 68 69 6c 64 50 61 67 65 73  parentChildPages
ea1b0 28 70 50 61 67 65 29 3b 0a 20 20 20 20 61 73 73  (pPage);.    ass
ea1c0 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65  ert( pPage->nOve
ea1d0 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 23 69 66 6e  rflow==0 );.#ifn
ea1e0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
ea1f0 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69  AUTOVACUUM.    i
ea200 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  f( pBt->autoVacu
ea210 75 6d 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  um ){.      int 
ea220 69 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  i;.      for(i=0
ea230 3b 20 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  ; i<pPage->nCell
ea240 3b 20 69 2b 2b 29 7b 20 0a 20 20 20 20 20 20 20  ; i++){ .       
ea250 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74 4f   rc = ptrmapPutO
ea260 76 66 6c 28 70 50 61 67 65 2c 20 69 29 3b 0a 20  vfl(pPage, i);. 
ea270 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
ea280 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
ea290 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 73        goto end_s
ea2a0 68 61 6c 6c 6f 77 5f 62 61 6c 61 6e 63 65 3b 0a  hallow_balance;.
ea2b0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
ea2c0 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  }.    }.#endif. 
ea2d0 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
ea2e0 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f  E_OK ) goto end_
ea2f0 73 68 61 6c 6c 6f 77 5f 62 61 6c 61 6e 63 65 3b  shallow_balance;
ea300 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65  .    releasePage
ea310 28 70 43 68 69 6c 64 29 3b 0a 20 20 7d 0a 65 6e  (pChild);.  }.en
ea320 64 5f 73 68 61 6c 6c 6f 77 5f 62 61 6c 61 6e 63  d_shallow_balanc
ea330 65 3a 0a 20 20 73 71 6c 69 74 65 46 72 65 65 28  e:.  sqliteFree(
ea340 61 70 43 65 6c 6c 29 3b 0a 20 20 72 65 74 75 72  apCell);.  retur
ea350 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  n rc;.}.../*.** 
ea360 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20 69 73  The root page is
ea370 20 6f 76 65 72 66 75 6c 6c 0a 2a 2a 0a 2a 2a 20   overfull.**.** 
ea380 57 68 65 6e 20 74 68 69 73 20 68 61 70 70 65 6e  When this happen
ea390 73 2c 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  s, Create a new 
ea3a0 63 68 69 6c 64 20 70 61 67 65 20 61 6e 64 20 63  child page and c
ea3b0 6f 70 79 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65  opy the.** conte
ea3c0 6e 74 73 20 6f 66 20 74 68 65 20 72 6f 6f 74 20  nts of the root 
ea3d0 69 6e 74 6f 20 74 68 65 20 63 68 69 6c 64 2e 20  into the child. 
ea3e0 20 54 68 65 6e 20 6d 61 6b 65 20 74 68 65 20 72   Then make the r
ea3f0 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 61 6e 20 65  oot.** page an e
ea400 6d 70 74 79 20 70 61 67 65 20 77 69 74 68 20 72  mpty page with r
ea410 69 67 68 74 43 68 69 6c 64 20 70 6f 69 6e 74 69  ightChild pointi
ea420 6e 67 20 74 6f 20 74 68 65 20 6e 65 77 0a 2a 2a  ng to the new.**
ea430 20 63 68 69 6c 64 2e 20 20 20 46 69 6e 61 6c 6c   child.   Finall
ea440 79 2c 20 63 61 6c 6c 20 62 61 6c 61 6e 63 65 5f  y, call balance_
ea450 69 6e 74 65 72 6e 61 6c 28 29 20 6f 6e 20 74 68  internal() on th
ea460 65 20 6e 65 77 20 63 68 69 6c 64 0a 2a 2a 20 74  e new child.** t
ea470 6f 20 63 61 75 73 65 20 69 74 20 74 6f 20 73 70  o cause it to sp
ea480 6c 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  lit..*/.static i
ea490 6e 74 20 62 61 6c 61 6e 63 65 5f 64 65 65 70 65  nt balance_deepe
ea4a0 72 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65  r(MemPage *pPage
ea4b0 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  ){.  int rc;    
ea4c0 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
ea4d0 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 73 75  rn value from su
ea4e0 62 70 72 6f 63 65 64 75 72 65 73 20 2a 2f 0a 20  bprocedures */. 
ea4f0 20 4d 65 6d 50 61 67 65 20 2a 70 43 68 69 6c 64   MemPage *pChild
ea500 3b 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20  ;    /* Pointer 
ea510 74 6f 20 61 20 6e 65 77 20 63 68 69 6c 64 20 70  to a new child p
ea520 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67  age */.  Pgno pg
ea530 6e 6f 43 68 69 6c 64 3b 20 20 20 20 20 2f 2a 20  noChild;     /* 
ea540 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74  Page number of t
ea550 68 65 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67  he new child pag
ea560 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20  e */.  BtShared 
ea570 2a 70 42 74 3b 20 20 20 20 20 20 20 20 20 2f 2a  *pBt;         /*
ea580 20 54 68 65 20 42 54 72 65 65 20 2a 2f 0a 20 20   The BTree */.  
ea590 69 6e 74 20 75 73 61 62 6c 65 53 69 7a 65 3b 20  int usableSize; 
ea5a0 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 75 73 61      /* Total usa
ea5b0 62 6c 65 20 73 69 7a 65 20 6f 66 20 61 20 70 61  ble size of a pa
ea5c0 67 65 20 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61  ge */.  u8 *data
ea5d0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43  ;           /* C
ea5e0 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61  ontent of the pa
ea5f0 72 65 6e 74 20 70 61 67 65 20 2a 2f 0a 20 20 75  rent page */.  u
ea600 38 20 2a 63 64 61 74 61 3b 20 20 20 20 20 20 20  8 *cdata;       
ea610 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66     /* Content of
ea620 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 20   the child page 
ea630 2a 2f 0a 20 20 69 6e 74 20 68 64 72 3b 20 20 20  */.  int hdr;   
ea640 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73           /* Offs
ea650 65 74 20 74 6f 20 70 61 67 65 20 68 65 61 64 65  et to page heade
ea660 72 20 69 6e 20 70 61 72 65 6e 74 20 2a 2f 0a 20  r in parent */. 
ea670 20 69 6e 74 20 62 72 6b 3b 20 20 20 20 20 20 20   int brk;       
ea680 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74       /* Offset t
ea690 6f 20 63 6f 6e 74 65 6e 74 20 6f 66 20 66 69 72  o content of fir
ea6a0 73 74 20 63 65 6c 6c 20 69 6e 20 70 61 72 65 6e  st cell in paren
ea6b0 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  t */..  assert( 
ea6c0 70 50 61 67 65 2d 3e 70 50 61 72 65 6e 74 3d 3d  pPage->pParent==
ea6d0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
ea6e0 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e  Page->nOverflow>
ea6f0 30 20 29 3b 0a 20 20 70 42 74 20 3d 20 70 50 61  0 );.  pBt = pPa
ea700 67 65 2d 3e 70 42 74 3b 0a 20 20 72 63 20 3d 20  ge->pBt;.  rc = 
ea710 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67  allocateBtreePag
ea720 65 28 70 42 74 2c 20 26 70 43 68 69 6c 64 2c 20  e(pBt, &pChild, 
ea730 26 70 67 6e 6f 43 68 69 6c 64 2c 20 70 50 61 67  &pgnoChild, pPag
ea740 65 2d 3e 70 67 6e 6f 2c 20 30 29 3b 0a 20 20 69  e->pgno, 0);.  i
ea750 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
ea760 63 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  c;.  assert( sql
ea770 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65  ite3PagerIswrite
ea780 61 62 6c 65 28 70 43 68 69 6c 64 2d 3e 70 44 62  able(pChild->pDb
ea790 50 61 67 65 29 20 29 3b 0a 20 20 75 73 61 62 6c  Page) );.  usabl
ea7a0 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61  eSize = pBt->usa
ea7b0 62 6c 65 53 69 7a 65 3b 0a 20 20 64 61 74 61 20  bleSize;.  data 
ea7c0 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a  = pPage->aData;.
ea7d0 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68    hdr = pPage->h
ea7e0 64 72 4f 66 66 73 65 74 3b 0a 20 20 62 72 6b 20  drOffset;.  brk 
ea7f0 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  = get2byte(&data
ea800 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 63 64 61 74  [hdr+5]);.  cdat
ea810 61 20 3d 20 70 43 68 69 6c 64 2d 3e 61 44 61 74  a = pChild->aDat
ea820 61 3b 0a 20 20 6d 65 6d 63 70 79 28 63 64 61 74  a;.  memcpy(cdat
ea830 61 2c 20 26 64 61 74 61 5b 68 64 72 5d 2c 20 70  a, &data[hdr], p
ea840 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74  Page->cellOffset
ea850 2b 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d  +2*pPage->nCell-
ea860 68 64 72 29 3b 0a 20 20 6d 65 6d 63 70 79 28 26  hdr);.  memcpy(&
ea870 63 64 61 74 61 5b 62 72 6b 5d 2c 20 26 64 61 74  cdata[brk], &dat
ea880 61 5b 62 72 6b 5d 2c 20 75 73 61 62 6c 65 53 69  a[brk], usableSi
ea890 7a 65 2d 62 72 6b 29 3b 0a 20 20 61 73 73 65 72  ze-brk);.  asser
ea8a0 74 28 20 70 43 68 69 6c 64 2d 3e 69 73 49 6e 69  t( pChild->isIni
ea8b0 74 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73  t==0 );.  rc = s
ea8c0 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50  qlite3BtreeInitP
ea8d0 61 67 65 28 70 43 68 69 6c 64 2c 20 70 50 61 67  age(pChild, pPag
ea8e0 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 67  e);.  if( rc ) g
ea8f0 6f 74 6f 20 62 61 6c 61 6e 63 65 64 65 65 70 65  oto balancedeepe
ea900 72 5f 6f 75 74 3b 0a 20 20 6d 65 6d 63 70 79 28  r_out;.  memcpy(
ea910 70 43 68 69 6c 64 2d 3e 61 4f 76 66 6c 2c 20 70  pChild->aOvfl, p
ea920 50 61 67 65 2d 3e 61 4f 76 66 6c 2c 20 70 50 61  Page->aOvfl, pPa
ea930 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2a 73 69  ge->nOverflow*si
ea940 7a 65 6f 66 28 70 50 61 67 65 2d 3e 61 4f 76 66  zeof(pPage->aOvf
ea950 6c 5b 30 5d 29 29 3b 0a 20 20 70 43 68 69 6c 64  l[0]));.  pChild
ea960 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 70 50  ->nOverflow = pP
ea970 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a  age->nOverflow;.
ea980 20 20 69 66 28 20 70 43 68 69 6c 64 2d 3e 6e 4f    if( pChild->nO
ea990 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 70  verflow ){.    p
ea9a0 43 68 69 6c 64 2d 3e 6e 46 72 65 65 20 3d 20 30  Child->nFree = 0
ea9b0 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
ea9c0 70 43 68 69 6c 64 2d 3e 6e 43 65 6c 6c 3d 3d 70  pChild->nCell==p
ea9d0 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20  Page->nCell );. 
ea9e0 20 7a 65 72 6f 50 61 67 65 28 70 50 61 67 65 2c   zeroPage(pPage,
ea9f0 20 70 43 68 69 6c 64 2d 3e 61 44 61 74 61 5b 30   pChild->aData[0
eaa00 5d 20 26 20 7e 50 54 46 5f 4c 45 41 46 29 3b 0a  ] & ~PTF_LEAF);.
eaa10 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67    put4byte(&pPag
eaa20 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e  e->aData[pPage->
eaa30 68 64 72 4f 66 66 73 65 74 2b 38 5d 2c 20 70 67  hdrOffset+8], pg
eaa40 6e 6f 43 68 69 6c 64 29 3b 0a 20 20 54 52 41 43  noChild);.  TRAC
eaa50 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 63 6f 70  E(("BALANCE: cop
eaa60 79 20 72 6f 6f 74 20 25 64 20 69 6e 74 6f 20 25  y root %d into %
eaa70 64 5c 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e  d\n", pPage->pgn
eaa80 6f 2c 20 70 43 68 69 6c 64 2d 3e 70 67 6e 6f 29  o, pChild->pgno)
eaa90 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
eaaa0 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
eaab0 4d 0a 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74  M.  if( pBt->aut
eaac0 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 69  oVacuum ){.    i
eaad0 6e 74 20 69 3b 0a 20 20 20 20 72 63 20 3d 20 70  nt i;.    rc = p
eaae0 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70 43  trmapPut(pBt, pC
eaaf0 68 69 6c 64 2d 3e 70 67 6e 6f 2c 20 50 54 52 4d  hild->pgno, PTRM
eab00 41 50 5f 42 54 52 45 45 2c 20 70 50 61 67 65 2d  AP_BTREE, pPage-
eab10 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 69 66 28 20  >pgno);.    if( 
eab20 72 63 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63  rc ) goto balanc
eab30 65 64 65 65 70 65 72 5f 6f 75 74 3b 0a 20 20 20  edeeper_out;.   
eab40 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43 68 69   for(i=0; i<pChi
eab50 6c 64 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b  ld->nCell; i++){
eab60 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d  .      rc = ptrm
eab70 61 70 50 75 74 4f 76 66 6c 28 70 43 68 69 6c 64  apPutOvfl(pChild
eab80 2c 20 69 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , i);.      if( 
eab90 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
eaba0 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
eabb0 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rc;.      }.    
eabc0 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72  }.  }.#endif.  r
eabd0 63 20 3d 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72  c = balance_nonr
eabe0 6f 6f 74 28 70 43 68 69 6c 64 29 3b 0a 0a 62 61  oot(pChild);..ba
eabf0 6c 61 6e 63 65 64 65 65 70 65 72 5f 6f 75 74 3a  lancedeeper_out:
eac00 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70  .  releasePage(p
eac10 43 68 69 6c 64 29 3b 0a 20 20 72 65 74 75 72 6e  Child);.  return
eac20 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65   rc;.}../*.** De
eac30 63 69 64 65 20 69 66 20 74 68 65 20 70 61 67 65  cide if the page
eac40 20 70 50 61 67 65 20 6e 65 65 64 73 20 74 6f 20   pPage needs to 
eac50 62 65 20 62 61 6c 61 6e 63 65 64 2e 20 20 49 66  be balanced.  If
eac60 20 62 61 6c 61 6e 63 69 6e 67 20 69 73 0a 2a 2a   balancing is.**
eac70 20 72 65 71 75 69 72 65 64 2c 20 63 61 6c 6c 20   required, call 
eac80 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  the appropriate 
eac90 62 61 6c 61 6e 63 69 6e 67 20 72 6f 75 74 69 6e  balancing routin
eaca0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
eacb0 20 62 61 6c 61 6e 63 65 28 4d 65 6d 50 61 67 65   balance(MemPage
eacc0 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 69 6e 73   *pPage, int ins
eacd0 65 72 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  ert){.  int rc =
eace0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66   SQLITE_OK;.  if
eacf0 28 20 70 50 61 67 65 2d 3e 70 50 61 72 65 6e 74  ( pPage->pParent
ead00 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70  ==0 ){.    if( p
ead10 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e  Page->nOverflow>
ead20 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  0 ){.      rc = 
ead30 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 70  balance_deeper(p
ead40 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20  Page);.    }.   
ead50 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
ead60 4f 4b 20 26 26 20 70 50 61 67 65 2d 3e 6e 43 65  OK && pPage->nCe
ead70 6c 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72  ll==0 ){.      r
ead80 63 20 3d 20 62 61 6c 61 6e 63 65 5f 73 68 61 6c  c = balance_shal
ead90 6c 6f 77 65 72 28 70 50 61 67 65 29 3b 0a 20 20  lower(pPage);.  
eada0 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
eadb0 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65   if( pPage->nOve
eadc0 72 66 6c 6f 77 3e 30 20 7c 7c 20 0a 20 20 20 20  rflow>0 || .    
eadd0 20 20 20 20 28 21 69 6e 73 65 72 74 20 26 26 20      (!insert && 
eade0 70 50 61 67 65 2d 3e 6e 46 72 65 65 3e 70 50 61  pPage->nFree>pPa
eadf0 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53  ge->pBt->usableS
eae00 69 7a 65 2a 32 2f 33 29 20 29 7b 0a 20 20 20 20  ize*2/3) ){.    
eae10 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f 6e    rc = balance_n
eae20 6f 6e 72 6f 6f 74 28 70 50 61 67 65 29 3b 0a 20  onroot(pPage);. 
eae30 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
eae40 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
eae50 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63  his routine chec
eae60 6b 73 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 74  ks all cursors t
eae70 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 74 61 62  hat point to tab
eae80 6c 65 20 70 67 6e 6f 52 6f 6f 74 2e 0a 2a 2a 20  le pgnoRoot..** 
eae90 49 66 20 61 6e 79 20 6f 66 20 74 68 6f 73 65 20  If any of those 
eaea0 63 75 72 73 6f 72 73 20 77 65 72 65 20 6f 70 65  cursors were ope
eaeb0 6e 65 64 20 77 69 74 68 20 77 72 46 6c 61 67 3d  ned with wrFlag=
eaec0 3d 30 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e  =0 in a differen
eaed0 74 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f  t.** database co
eaee0 6e 6e 65 63 74 69 6f 6e 20 28 61 20 64 61 74 61  nnection (a data
eaef0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
eaf00 74 68 61 74 20 73 68 61 72 65 73 20 74 68 65 20  that shares the 
eaf10 70 61 67 65 72 0a 2a 2a 20 63 61 63 68 65 20 77  pager.** cache w
eaf20 69 74 68 20 74 68 65 20 63 75 72 72 65 6e 74 20  ith the current 
eaf30 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 61 6e 64 20  connection) and 
eaf40 74 68 61 74 20 6f 74 68 65 72 20 63 6f 6e 6e 65  that other conne
eaf50 63 74 69 6f 6e 20 0a 2a 2a 20 69 73 20 6e 6f 74  ction .** is not
eaf60 20 69 6e 20 74 68 65 20 52 65 61 64 55 6e 63 6f   in the ReadUnco
eaf70 6d 6d 6d 69 74 74 65 64 20 73 74 61 74 65 2c 20  mmmitted state, 
eaf80 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  then this routin
eaf90 65 20 72 65 74 75 72 6e 73 20 0a 2a 2a 20 53 51  e returns .** SQ
eafa0 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2a 0a  LITE_LOCKED..**.
eafb0 2a 2a 20 49 6e 20 61 64 64 69 74 69 6f 6e 20 74  ** In addition t
eafc0 6f 20 63 68 65 63 6b 69 6e 67 20 66 6f 72 20 72  o checking for r
eafd0 65 61 64 2d 6c 6f 63 6b 73 20 28 77 68 65 72 65  ead-locks (where
eafe0 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 0a 2a 2a   a read-lock .**
eaff0 20 6d 65 61 6e 73 20 61 20 63 75 72 73 6f 72 20   means a cursor 
eb000 6f 70 65 6e 65 64 20 77 69 74 68 20 77 72 46 6c  opened with wrFl
eb010 61 67 3d 3d 30 29 20 74 68 69 73 20 72 6f 75 74  ag==0) this rout
eb020 69 6e 65 20 61 6c 73 6f 20 6d 6f 76 65 73 0a 2a  ine also moves.*
eb030 2a 20 61 6c 6c 20 77 72 69 74 65 20 63 75 72 73  * all write curs
eb040 6f 72 73 20 73 6f 20 74 68 61 74 20 74 68 65 79  ors so that they
eb050 20 61 72 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f   are pointing to
eb060 20 74 68 65 20 0a 2a 2a 20 66 69 72 73 74 20 43   the .** first C
eb070 65 6c 6c 20 6f 6e 20 74 68 65 20 72 6f 6f 74 20  ell on the root 
eb080 70 61 67 65 2e 20 20 54 68 69 73 20 69 73 20 6e  page.  This is n
eb090 65 63 65 73 73 61 72 79 20 62 65 63 61 75 73 65  ecessary because
eb0a0 20 61 6e 20 69 6e 73 65 72 74 20 0a 2a 2a 20 6f   an insert .** o
eb0b0 72 20 64 65 6c 65 74 65 20 6d 69 67 68 74 20 63  r delete might c
eb0c0 68 61 6e 67 65 20 74 68 65 20 6e 75 6d 62 65 72  hange the number
eb0d0 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 61 20 70   of cells on a p
eb0e0 61 67 65 20 6f 72 20 64 65 6c 65 74 65 0a 2a 2a  age or delete.**
eb0f0 20 61 20 70 61 67 65 20 65 6e 74 69 72 65 6c 79   a page entirely
eb100 20 61 6e 64 20 77 65 20 64 6f 20 6e 6f 74 20 77   and we do not w
eb110 61 6e 74 20 74 6f 20 6c 65 61 76 65 20 61 6e 79  ant to leave any
eb120 20 63 75 72 73 6f 72 73 20 0a 2a 2a 20 70 6f 69   cursors .** poi
eb130 6e 74 69 6e 67 20 74 6f 20 6e 6f 6e 2d 65 78 69  nting to non-exi
eb140 73 74 61 6e 74 20 70 61 67 65 73 20 6f 72 20 63  stant pages or c
eb150 65 6c 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ells..*/.static 
eb160 69 6e 74 20 63 68 65 63 6b 52 65 61 64 4c 6f 63  int checkReadLoc
eb170 6b 73 28 42 74 72 65 65 20 2a 70 42 74 72 65 65  ks(Btree *pBtree
eb180 2c 20 50 67 6e 6f 20 70 67 6e 6f 52 6f 6f 74 2c  , Pgno pgnoRoot,
eb190 20 42 74 43 75 72 73 6f 72 20 2a 70 45 78 63 6c   BtCursor *pExcl
eb1a0 75 64 65 29 7b 0a 20 20 42 74 43 75 72 73 6f 72  ude){.  BtCursor
eb1b0 20 2a 70 3b 0a 20 20 42 74 53 68 61 72 65 64 20   *p;.  BtShared 
eb1c0 2a 70 42 74 20 3d 20 70 42 74 72 65 65 2d 3e 70  *pBt = pBtree->p
eb1d0 42 74 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  Bt;.  sqlite3 *d
eb1e0 62 20 3d 20 70 42 74 72 65 65 2d 3e 70 53 71 6c  b = pBtree->pSql
eb1f0 69 74 65 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74  ite;.  for(p=pBt
eb200 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d  ->pCursor; p; p=
eb210 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69  p->pNext){.    i
eb220 66 28 20 70 3d 3d 70 45 78 63 6c 75 64 65 20 29  f( p==pExclude )
eb230 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69   continue;.    i
eb240 66 28 20 70 2d 3e 65 53 74 61 74 65 21 3d 43 55  f( p->eState!=CU
eb250 52 53 4f 52 5f 56 41 4c 49 44 20 29 20 63 6f 6e  RSOR_VALID ) con
eb260 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 70  tinue;.    if( p
eb270 2d 3e 70 67 6e 6f 52 6f 6f 74 21 3d 70 67 6e 6f  ->pgnoRoot!=pgno
eb280 52 6f 6f 74 20 29 20 63 6f 6e 74 69 6e 75 65 3b  Root ) continue;
eb290 0a 20 20 20 20 69 66 28 20 70 2d 3e 77 72 46 6c  .    if( p->wrFl
eb2a0 61 67 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73  ag==0 ){.      s
eb2b0 71 6c 69 74 65 33 20 2a 64 62 4f 74 68 65 72 20  qlite3 *dbOther 
eb2c0 3d 20 70 2d 3e 70 42 74 72 65 65 2d 3e 70 53 71  = p->pBtree->pSq
eb2d0 6c 69 74 65 3b 0a 20 20 20 20 20 20 69 66 28 20  lite;.      if( 
eb2e0 64 62 4f 74 68 65 72 3d 3d 30 20 7c 7c 0a 20 20  dbOther==0 ||.  
eb2f0 20 20 20 20 20 20 20 28 64 62 4f 74 68 65 72 21         (dbOther!
eb300 3d 64 62 20 26 26 20 28 64 62 4f 74 68 65 72 2d  =db && (dbOther-
eb310 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
eb320 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29  ReadUncommitted)
eb330 3d 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20  ==0) ){.        
eb340 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f  return SQLITE_LO
eb350 43 4b 45 44 3b 0a 20 20 20 20 20 20 7d 0a 20 20  CKED;.      }.  
eb360 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 70    }else if( p->p
eb370 50 61 67 65 2d 3e 70 67 6e 6f 21 3d 70 2d 3e 70  Page->pgno!=p->p
eb380 67 6e 6f 52 6f 6f 74 20 29 7b 0a 20 20 20 20 20  gnoRoot ){.     
eb390 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 29 3b 0a   moveToRoot(p);.
eb3a0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
eb3b0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
eb3c0 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20  ./*.** Insert a 
eb3d0 6e 65 77 20 72 65 63 6f 72 64 20 69 6e 74 6f 20  new record into 
eb3e0 74 68 65 20 42 54 72 65 65 2e 20 20 54 68 65 20  the BTree.  The 
eb3f0 6b 65 79 20 69 73 20 67 69 76 65 6e 20 62 79 20  key is given by 
eb400 28 70 4b 65 79 2c 6e 4b 65 79 29 0a 2a 2a 20 61  (pKey,nKey).** a
eb410 6e 64 20 74 68 65 20 64 61 74 61 20 69 73 20 67  nd the data is g
eb420 69 76 65 6e 20 62 79 20 28 70 44 61 74 61 2c 6e  iven by (pData,n
eb430 44 61 74 61 29 2e 20 20 54 68 65 20 63 75 72 73  Data).  The curs
eb440 6f 72 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20  or is used only 
eb450 74 6f 0a 2a 2a 20 64 65 66 69 6e 65 20 77 68 61  to.** define wha
eb460 74 20 74 61 62 6c 65 20 74 68 65 20 72 65 63 6f  t table the reco
eb470 72 64 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 73  rd should be ins
eb480 65 72 74 65 64 20 69 6e 74 6f 2e 20 20 54 68 65  erted into.  The
eb490 20 63 75 72 73 6f 72 0a 2a 2a 20 69 73 20 6c 65   cursor.** is le
eb4a0 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61  ft pointing at a
eb4b0 20 72 61 6e 64 6f 6d 20 6c 6f 63 61 74 69 6f 6e   random location
eb4c0 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 6e 20 49  ..**.** For an I
eb4d0 4e 54 4b 45 59 20 74 61 62 6c 65 2c 20 6f 6e 6c  NTKEY table, onl
eb4e0 79 20 74 68 65 20 6e 4b 65 79 20 76 61 6c 75 65  y the nKey value
eb4f0 20 6f 66 20 74 68 65 20 6b 65 79 20 69 73 20 75   of the key is u
eb500 73 65 64 2e 20 20 70 4b 65 79 20 69 73 0a 2a 2a  sed.  pKey is.**
eb510 20 69 67 6e 6f 72 65 64 2e 20 20 46 6f 72 20 61   ignored.  For a
eb520 20 5a 45 52 4f 44 41 54 41 20 74 61 62 6c 65 2c   ZERODATA table,
eb530 20 74 68 65 20 70 44 61 74 61 20 61 6e 64 20 6e   the pData and n
eb540 44 61 74 61 20 61 72 65 20 62 6f 74 68 20 69 67  Data are both ig
eb550 6e 6f 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  nored..*/.SQLITE
eb560 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
eb570 69 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 28  ite3BtreeInsert(
eb580 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75  .  BtCursor *pCu
eb590 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
eb5a0 20 20 2f 2a 20 49 6e 73 65 72 74 20 64 61 74 61    /* Insert data
eb5b0 20 69 6e 74 6f 20 74 68 65 20 74 61 62 6c 65 20   into the table 
eb5c0 6f 66 20 74 68 69 73 20 63 75 72 73 6f 72 20 2a  of this cursor *
eb5d0 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  /.  const void *
eb5e0 70 4b 65 79 2c 20 69 36 34 20 6e 4b 65 79 2c 20  pKey, i64 nKey, 
eb5f0 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 6f 66     /* The key of
eb600 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20   the new record 
eb610 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  */.  const void 
eb620 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74  *pData, int nDat
eb630 61 2c 20 20 2f 2a 20 54 68 65 20 64 61 74 61 20  a,  /* The data 
eb640 6f 66 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72  of the new recor
eb650 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65 72 6f  d */.  int nZero
eb660 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
eb670 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
eb680 6f 66 20 65 78 74 72 61 20 30 20 62 79 74 65 73  of extra 0 bytes
eb690 20 74 6f 20 61 70 70 65 6e 64 20 74 6f 20 64 61   to append to da
eb6a0 74 61 20 2a 2f 0a 20 20 69 6e 74 20 61 70 70 65  ta */.  int appe
eb6b0 6e 64 42 69 61 73 20 20 20 20 20 20 20 20 20 20  ndBias          
eb6c0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
eb6d0 66 20 74 68 69 73 20 69 73 20 6c 69 6b 65 6c 79  f this is likely
eb6e0 20 61 6e 20 61 70 70 65 6e 64 20 2a 2f 0a 29 7b   an append */.){
eb6f0 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74  .  int rc;.  int
eb700 20 6c 6f 63 3b 0a 20 20 69 6e 74 20 73 7a 4e 65   loc;.  int szNe
eb710 77 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  w;.  MemPage *pP
eb720 61 67 65 3b 0a 20 20 42 74 53 68 61 72 65 64 20  age;.  BtShared 
eb730 2a 70 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 74  *pBt = pCur->pBt
eb740 72 65 65 2d 3e 70 42 74 3b 0a 20 20 75 6e 73 69  ree->pBt;.  unsi
eb750 67 6e 65 64 20 63 68 61 72 20 2a 6f 6c 64 43 65  gned char *oldCe
eb760 6c 6c 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  ll;.  unsigned c
eb770 68 61 72 20 2a 6e 65 77 43 65 6c 6c 20 3d 20 30  har *newCell = 0
eb780 3b 0a 0a 20 20 69 66 28 20 70 42 74 2d 3e 69 6e  ;..  if( pBt->in
eb790 54 72 61 6e 73 61 63 74 69 6f 6e 21 3d 54 52 41  Transaction!=TRA
eb7a0 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20  NS_WRITE ){.    
eb7b0 2f 2a 20 4d 75 73 74 20 73 74 61 72 74 20 61 20  /* Must start a 
eb7c0 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 66 6f  transaction befo
eb7d0 72 65 20 64 6f 69 6e 67 20 61 6e 20 69 6e 73 65  re doing an inse
eb7e0 72 74 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e  rt */.    return
eb7f0 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3f   pBt->readOnly ?
eb800 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59   SQLITE_READONLY
eb810 20 3a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   : SQLITE_ERROR;
eb820 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21  .  }.  assert( !
eb830 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 3b  pBt->readOnly );
eb840 0a 20 20 69 66 28 20 21 70 43 75 72 2d 3e 77 72  .  if( !pCur->wr
eb850 46 6c 61 67 20 29 7b 0a 20 20 20 20 72 65 74 75  Flag ){.    retu
eb860 72 6e 20 53 51 4c 49 54 45 5f 50 45 52 4d 3b 20  rn SQLITE_PERM; 
eb870 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 6f 74 20    /* Cursor not 
eb880 6f 70 65 6e 20 66 6f 72 20 77 72 69 74 69 6e 67  open for writing
eb890 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 63 68   */.  }.  if( ch
eb8a0 65 63 6b 52 65 61 64 4c 6f 63 6b 73 28 70 43 75  eckReadLocks(pCu
eb8b0 72 2d 3e 70 42 74 72 65 65 2c 20 70 43 75 72 2d  r->pBtree, pCur-
eb8c0 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 29  >pgnoRoot, pCur)
eb8d0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
eb8e0 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b 20 2f 2a  QLITE_LOCKED; /*
eb8f0 20 54 68 65 20 74 61 62 6c 65 20 70 43 75 72 20   The table pCur 
eb900 70 6f 69 6e 74 73 20 74 6f 20 68 61 73 20 61 20  points to has a 
eb910 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 7d  read lock */.  }
eb920 0a 0a 20 20 2f 2a 20 53 61 76 65 20 74 68 65 20  ..  /* Save the 
eb930 70 6f 73 69 74 69 6f 6e 73 20 6f 66 20 61 6e 79  positions of any
eb940 20 6f 74 68 65 72 20 63 75 72 73 6f 72 73 20 6f   other cursors o
eb950 70 65 6e 20 6f 6e 20 74 68 69 73 20 74 61 62 6c  pen on this tabl
eb960 65 20 2a 2f 0a 20 20 63 6c 65 61 72 43 75 72 73  e */.  clearCurs
eb970 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29  orPosition(pCur)
eb980 3b 0a 20 20 69 66 28 20 0a 20 20 20 20 53 51 4c  ;.  if( .    SQL
eb990 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 61  ITE_OK!=(rc = sa
eb9a0 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74  veAllCursors(pBt
eb9b0 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74  , pCur->pgnoRoot
eb9c0 2c 20 70 43 75 72 29 29 20 7c 7c 0a 20 20 20 20  , pCur)) ||.    
eb9d0 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d  SQLITE_OK!=(rc =
eb9e0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76   sqlite3BtreeMov
eb9f0 65 74 6f 28 70 43 75 72 2c 20 70 4b 65 79 2c 20  eto(pCur, pKey, 
eba00 6e 4b 65 79 2c 20 61 70 70 65 6e 64 42 69 61 73  nKey, appendBias
eba10 2c 20 26 6c 6f 63 29 29 0a 20 20 29 7b 0a 20 20  , &loc)).  ){.  
eba20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
eba30 0a 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72  ..  pPage = pCur
eba40 2d 3e 70 50 61 67 65 3b 0a 20 20 61 73 73 65 72  ->pPage;.  asser
eba50 74 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79  t( pPage->intKey
eba60 20 7c 7c 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20   || nKey>=0 );. 
eba70 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
eba80 6c 65 61 66 20 7c 7c 20 21 70 50 61 67 65 2d 3e  leaf || !pPage->
eba90 6c 65 61 66 44 61 74 61 20 29 3b 0a 20 20 54 52  leafData );.  TR
ebaa0 41 43 45 28 28 22 49 4e 53 45 52 54 3a 20 74 61  ACE(("INSERT: ta
ebab0 62 6c 65 3d 25 64 20 6e 6b 65 79 3d 25 6c 6c 64  ble=%d nkey=%lld
ebac0 20 6e 64 61 74 61 3d 25 64 20 70 61 67 65 3d 25   ndata=%d page=%
ebad0 64 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20  d %s\n",.       
ebae0 20 20 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f     pCur->pgnoRoo
ebaf0 74 2c 20 6e 4b 65 79 2c 20 6e 44 61 74 61 2c 20  t, nKey, nData, 
ebb00 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 0a 20 20 20  pPage->pgno,.   
ebb10 20 20 20 20 20 20 20 6c 6f 63 3d 3d 30 20 3f 20         loc==0 ? 
ebb20 22 6f 76 65 72 77 72 69 74 65 22 20 3a 20 22 6e  "overwrite" : "n
ebb30 65 77 20 65 6e 74 72 79 22 29 29 3b 0a 20 20 61  ew entry"));.  a
ebb40 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73  ssert( pPage->is
ebb50 49 6e 69 74 20 29 3b 0a 20 20 72 63 20 3d 20 73  Init );.  rc = s
ebb60 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
ebb70 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
ebb80 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74  ;.  if( rc ) ret
ebb90 75 72 6e 20 72 63 3b 0a 20 20 6e 65 77 43 65 6c  urn rc;.  newCel
ebba0 6c 20 3d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63  l = sqliteMalloc
ebbb0 52 61 77 28 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a  Raw( MX_CELL_SIZ
ebbc0 45 28 70 42 74 29 20 29 3b 0a 20 20 69 66 28 20  E(pBt) );.  if( 
ebbd0 6e 65 77 43 65 6c 6c 3d 3d 30 20 29 20 72 65 74  newCell==0 ) ret
ebbe0 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
ebbf0 3b 0a 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e 43  ;.  rc = fillInC
ebc00 65 6c 6c 28 70 50 61 67 65 2c 20 6e 65 77 43 65  ell(pPage, newCe
ebc10 6c 6c 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20  ll, pKey, nKey, 
ebc20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 6e 5a  pData, nData, nZ
ebc30 65 72 6f 2c 20 26 73 7a 4e 65 77 29 3b 0a 20 20  ero, &szNew);.  
ebc40 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e  if( rc ) goto en
ebc50 64 5f 69 6e 73 65 72 74 3b 0a 20 20 61 73 73 65  d_insert;.  asse
ebc60 72 74 28 20 73 7a 4e 65 77 3d 3d 63 65 6c 6c 53  rt( szNew==cellS
ebc70 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 6e 65  izePtr(pPage, ne
ebc80 77 43 65 6c 6c 29 20 29 3b 0a 20 20 61 73 73 65  wCell) );.  asse
ebc90 72 74 28 20 73 7a 4e 65 77 3c 3d 4d 58 5f 43 45  rt( szNew<=MX_CE
ebca0 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 29 3b 0a  LL_SIZE(pBt) );.
ebcb0 20 20 69 66 28 20 6c 6f 63 3d 3d 30 20 26 26 20    if( loc==0 && 
ebcc0 43 55 52 53 4f 52 5f 56 41 4c 49 44 3d 3d 70 43  CURSOR_VALID==pC
ebcd0 75 72 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20 20  ur->eState ){.  
ebce0 20 20 69 6e 74 20 73 7a 4f 6c 64 3b 0a 20 20 20    int szOld;.   
ebcf0 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69   assert( pCur->i
ebd00 64 78 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e 69  dx>=0 && pCur->i
ebd10 64 78 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20  dx<pPage->nCell 
ebd20 29 3b 0a 20 20 20 20 6f 6c 64 43 65 6c 6c 20 3d  );.    oldCell =
ebd30 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c   findCell(pPage,
ebd40 20 70 43 75 72 2d 3e 69 64 78 29 3b 0a 20 20 20   pCur->idx);.   
ebd50 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61   if( !pPage->lea
ebd60 66 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70  f ){.      memcp
ebd70 79 28 6e 65 77 43 65 6c 6c 2c 20 6f 6c 64 43 65  y(newCell, oldCe
ebd80 6c 6c 2c 20 34 29 3b 0a 20 20 20 20 7d 0a 20 20  ll, 4);.    }.  
ebd90 20 20 73 7a 4f 6c 64 20 3d 20 63 65 6c 6c 53 69    szOld = cellSi
ebda0 7a 65 50 74 72 28 70 50 61 67 65 2c 20 6f 6c 64  zePtr(pPage, old
ebdb0 43 65 6c 6c 29 3b 0a 20 20 20 20 72 63 20 3d 20  Cell);.    rc = 
ebdc0 63 6c 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c  clearCell(pPage,
ebdd0 20 6f 6c 64 43 65 6c 6c 29 3b 0a 20 20 20 20 69   oldCell);.    i
ebde0 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64  f( rc ) goto end
ebdf0 5f 69 6e 73 65 72 74 3b 0a 20 20 20 20 64 72 6f  _insert;.    dro
ebe00 70 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 43 75  pCell(pPage, pCu
ebe10 72 2d 3e 69 64 78 2c 20 73 7a 4f 6c 64 29 3b 0a  r->idx, szOld);.
ebe20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 3c    }else if( loc<
ebe30 30 20 26 26 20 70 50 61 67 65 2d 3e 6e 43 65 6c  0 && pPage->nCel
ebe40 6c 3e 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72  l>0 ){.    asser
ebe50 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  t( pPage->leaf )
ebe60 3b 0a 20 20 20 20 70 43 75 72 2d 3e 69 64 78 2b  ;.    pCur->idx+
ebe70 2b 3b 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e 66  +;.    pCur->inf
ebe80 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 7d  o.nSize = 0;.  }
ebe90 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
ebea0 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b  ( pPage->leaf );
ebeb0 0a 20 20 7d 0a 20 20 72 63 20 3d 20 69 6e 73 65  .  }.  rc = inse
ebec0 72 74 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 43  rtCell(pPage, pC
ebed0 75 72 2d 3e 69 64 78 2c 20 6e 65 77 43 65 6c 6c  ur->idx, newCell
ebee0 2c 20 73 7a 4e 65 77 2c 20 30 2c 20 30 29 3b 0a  , szNew, 0, 0);.
ebef0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
ebf00 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 69  _OK ) goto end_i
ebf10 6e 73 65 72 74 3b 0a 20 20 72 63 20 3d 20 62 61  nsert;.  rc = ba
ebf20 6c 61 6e 63 65 28 70 50 61 67 65 2c 20 31 29 3b  lance(pPage, 1);
ebf30 0a 20 20 2f 2a 20 73 71 6c 69 74 65 33 42 74 72  .  /* sqlite3Btr
ebf40 65 65 50 61 67 65 44 75 6d 70 28 70 43 75 72 2d  eePageDump(pCur-
ebf50 3e 70 42 74 2c 20 70 43 75 72 2d 3e 70 67 6e 6f  >pBt, pCur->pgno
ebf60 52 6f 6f 74 2c 20 31 29 3b 20 2a 2f 0a 20 20 2f  Root, 1); */.  /
ebf70 2a 20 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29  * fflush(stdout)
ebf80 3b 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53  ; */.  if( rc==S
ebf90 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
ebfa0 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29  moveToRoot(pCur)
ebfb0 3b 0a 20 20 7d 0a 65 6e 64 5f 69 6e 73 65 72 74  ;.  }.end_insert
ebfc0 3a 0a 20 20 73 71 6c 69 74 65 46 72 65 65 28 6e  :.  sqliteFree(n
ebfd0 65 77 43 65 6c 6c 29 3b 0a 20 20 72 65 74 75 72  ewCell);.  retur
ebfe0 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  n rc;.}../*.** D
ebff0 65 6c 65 74 65 20 74 68 65 20 65 6e 74 72 79 20  elete the entry 
ec000 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20  that the cursor 
ec010 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20  is pointing to. 
ec020 20 54 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69   The cursor.** i
ec030 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20  s left pointing 
ec040 61 74 20 61 20 72 61 6e 64 6f 6d 20 6c 6f 63 61  at a random loca
ec050 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
ec060 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
ec070 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 28 42  te3BtreeDelete(B
ec080 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a  tCursor *pCur){.
ec090 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
ec0a0 20 3d 20 70 43 75 72 2d 3e 70 50 61 67 65 3b 0a   = pCur->pPage;.
ec0b0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
ec0c0 2a 70 43 65 6c 6c 3b 0a 20 20 69 6e 74 20 72 63  *pCell;.  int rc
ec0d0 3b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 43 68 69  ;.  Pgno pgnoChi
ec0e0 6c 64 20 3d 20 30 3b 0a 20 20 42 74 53 68 61 72  ld = 0;.  BtShar
ec0f0 65 64 20 2a 70 42 74 20 3d 20 70 43 75 72 2d 3e  ed *pBt = pCur->
ec100 70 42 74 72 65 65 2d 3e 70 42 74 3b 0a 0a 20 20  pBtree->pBt;..  
ec110 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69  assert( pPage->i
ec120 73 49 6e 69 74 20 29 3b 0a 20 20 69 66 28 20 70  sInit );.  if( p
ec130 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f  Bt->inTransactio
ec140 6e 21 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29  n!=TRANS_WRITE )
ec150 7b 0a 20 20 20 20 2f 2a 20 4d 75 73 74 20 73 74  {.    /* Must st
ec160 61 72 74 20 61 20 74 72 61 6e 73 61 63 74 69 6f  art a transactio
ec170 6e 20 62 65 66 6f 72 65 20 64 6f 69 6e 67 20 61  n before doing a
ec180 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 20 20 72   delete */.    r
ec190 65 74 75 72 6e 20 70 42 74 2d 3e 72 65 61 64 4f  eturn pBt->readO
ec1a0 6e 6c 79 20 3f 20 53 51 4c 49 54 45 5f 52 45 41  nly ? SQLITE_REA
ec1b0 44 4f 4e 4c 59 20 3a 20 53 51 4c 49 54 45 5f 45  DONLY : SQLITE_E
ec1c0 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 61 73 73 65  RROR;.  }.  asse
ec1d0 72 74 28 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e  rt( !pBt->readOn
ec1e0 6c 79 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72  ly );.  if( pCur
ec1f0 2d 3e 69 64 78 20 3e 3d 20 70 50 61 67 65 2d 3e  ->idx >= pPage->
ec200 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74  nCell ){.    ret
ec210 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
ec220 3b 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72  ;  /* The cursor
ec230 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67   is not pointing
ec240 20 74 6f 20 61 6e 79 74 68 69 6e 67 20 2a 2f 0a   to anything */.
ec250 20 20 7d 0a 20 20 69 66 28 20 21 70 43 75 72 2d    }.  if( !pCur-
ec260 3e 77 72 46 6c 61 67 20 29 7b 0a 20 20 20 20 72  >wrFlag ){.    r
ec270 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 45 52  eturn SQLITE_PER
ec280 4d 3b 20 20 20 2f 2a 20 44 69 64 20 6e 6f 74 20  M;   /* Did not 
ec290 6f 70 65 6e 20 74 68 69 73 20 63 75 72 73 6f 72  open this cursor
ec2a0 20 66 6f 72 20 77 72 69 74 69 6e 67 20 2a 2f 0a   for writing */.
ec2b0 20 20 7d 0a 20 20 69 66 28 20 63 68 65 63 6b 52    }.  if( checkR
ec2c0 65 61 64 4c 6f 63 6b 73 28 70 43 75 72 2d 3e 70  eadLocks(pCur->p
ec2d0 42 74 72 65 65 2c 20 70 43 75 72 2d 3e 70 67 6e  Btree, pCur->pgn
ec2e0 6f 52 6f 6f 74 2c 20 70 43 75 72 29 20 29 7b 0a  oRoot, pCur) ){.
ec2f0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
ec300 45 5f 4c 4f 43 4b 45 44 3b 20 2f 2a 20 54 68 65  E_LOCKED; /* The
ec310 20 74 61 62 6c 65 20 70 43 75 72 20 70 6f 69 6e   table pCur poin
ec320 74 73 20 74 6f 20 68 61 73 20 61 20 72 65 61 64  ts to has a read
ec330 20 6c 6f 63 6b 20 2a 2f 0a 20 20 7d 0a 0a 20 20   lock */.  }..  
ec340 2f 2a 20 52 65 73 74 6f 72 65 20 74 68 65 20 63  /* Restore the c
ec350 75 72 72 65 6e 74 20 63 75 72 73 6f 72 20 70 6f  urrent cursor po
ec360 73 69 74 69 6f 6e 20 28 61 20 6e 6f 2d 6f 70 20  sition (a no-op 
ec370 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  if the cursor is
ec380 20 6e 6f 74 20 69 6e 20 0a 20 20 2a 2a 20 43 55   not in .  ** CU
ec390 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b  RSOR_REQUIRESEEK
ec3a0 20 73 74 61 74 65 29 20 61 6e 64 20 73 61 76 65   state) and save
ec3b0 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 73 20 6f   the positions o
ec3c0 66 20 61 6e 79 20 6f 74 68 65 72 20 63 75 72 73  f any other curs
ec3d0 6f 72 73 20 0a 20 20 2a 2a 20 6f 70 65 6e 20 6f  ors .  ** open o
ec3e0 6e 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65  n the same table
ec3f0 2e 20 54 68 65 6e 20 63 61 6c 6c 20 73 71 6c 69  . Then call sqli
ec400 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20  te3PagerWrite() 
ec410 6f 6e 20 74 68 65 20 70 61 67 65 0a 20 20 2a 2a  on the page.  **
ec420 20 74 68 61 74 20 74 68 65 20 65 6e 74 72 79 20   that the entry 
ec430 77 69 6c 6c 20 62 65 20 64 65 6c 65 74 65 64 20  will be deleted 
ec440 66 72 6f 6d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  from..  */.  if(
ec450 20 0a 20 20 20 20 28 72 63 20 3d 20 72 65 73 74   .    (rc = rest
ec460 6f 72 65 4f 72 43 6c 65 61 72 43 75 72 73 6f 72  oreOrClearCursor
ec470 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 29 21  Position(pCur))!
ec480 3d 30 20 7c 7c 0a 20 20 20 20 28 72 63 20 3d 20  =0 ||.    (rc = 
ec490 73 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 70  saveAllCursors(p
ec4a0 42 74 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f  Bt, pCur->pgnoRo
ec4b0 6f 74 2c 20 70 43 75 72 29 29 21 3d 30 20 7c 7c  ot, pCur))!=0 ||
ec4c0 0a 20 20 20 20 28 72 63 20 3d 20 73 71 6c 69 74  .    (rc = sqlit
ec4d0 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61  e3PagerWrite(pPa
ec4e0 67 65 2d 3e 70 44 62 50 61 67 65 29 29 21 3d 30  ge->pDbPage))!=0
ec4f0 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  .  ){.    return
ec500 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c   rc;.  }..  /* L
ec510 6f 63 61 74 65 20 74 68 65 20 63 65 6c 6c 20 77  ocate the cell w
ec520 69 74 68 69 6e 20 69 74 27 73 20 70 61 67 65 20  ithin it's page 
ec530 61 6e 64 20 6c 65 61 76 65 20 70 43 65 6c 6c 20  and leave pCell 
ec540 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 0a  pointing to the.
ec550 20 20 2a 2a 20 64 61 74 61 2e 20 54 68 65 20 63    ** data. The c
ec560 6c 65 61 72 43 65 6c 6c 28 29 20 63 61 6c 6c 20  learCell() call 
ec570 66 72 65 65 73 20 61 6e 79 20 6f 76 65 72 66 6c  frees any overfl
ec580 6f 77 20 70 61 67 65 73 20 61 73 73 6f 63 69 61  ow pages associa
ec590 74 65 64 20 77 69 74 68 20 74 68 65 0a 20 20 2a  ted with the.  *
ec5a0 2a 20 63 65 6c 6c 2e 20 54 68 65 20 63 65 6c 6c  * cell. The cell
ec5b0 20 69 74 73 65 6c 66 20 69 73 20 73 74 69 6c 6c   itself is still
ec5c0 20 69 6e 74 61 63 74 2e 0a 20 20 2a 2f 0a 20 20   intact..  */.  
ec5d0 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c  pCell = findCell
ec5e0 28 70 50 61 67 65 2c 20 70 43 75 72 2d 3e 69 64  (pPage, pCur->id
ec5f0 78 29 3b 0a 20 20 69 66 28 20 21 70 50 61 67 65  x);.  if( !pPage
ec600 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 70 67  ->leaf ){.    pg
ec610 6e 6f 43 68 69 6c 64 20 3d 20 67 65 74 34 62 79  noChild = get4by
ec620 74 65 28 70 43 65 6c 6c 29 3b 0a 20 20 7d 0a 20  te(pCell);.  }. 
ec630 20 72 63 20 3d 20 63 6c 65 61 72 43 65 6c 6c 28   rc = clearCell(
ec640 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20  pPage, pCell);. 
ec650 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
ec660 20 72 63 3b 0a 0a 20 20 69 66 28 20 21 70 50 61   rc;..  if( !pPa
ec670 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20  ge->leaf ){.    
ec680 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 65 6e  /*.    ** The en
ec690 74 72 79 20 77 65 20 61 72 65 20 61 62 6f 75 74  try we are about
ec6a0 20 74 6f 20 64 65 6c 65 74 65 20 69 73 20 6e 6f   to delete is no
ec6b0 74 20 61 20 6c 65 61 66 20 73 6f 20 69 66 20 77  t a leaf so if w
ec6c0 65 20 64 6f 20 6e 6f 74 0a 20 20 20 20 2a 2a 20  e do not.    ** 
ec6d0 64 6f 20 73 6f 6d 65 74 68 69 6e 67 20 77 65 20  do something we 
ec6e0 77 69 6c 6c 20 6c 65 61 76 65 20 61 20 68 6f 6c  will leave a hol
ec6f0 65 20 6f 6e 20 61 6e 20 69 6e 74 65 72 6e 61 6c  e on an internal
ec700 20 70 61 67 65 2e 0a 20 20 20 20 2a 2a 20 57 65   page..    ** We
ec710 20 68 61 76 65 20 74 6f 20 66 69 6c 6c 20 74 68   have to fill th
ec720 65 20 68 6f 6c 65 20 62 79 20 6d 6f 76 69 6e 67  e hole by moving
ec730 20 69 6e 20 61 20 63 65 6c 6c 20 66 72 6f 6d 20   in a cell from 
ec740 61 20 6c 65 61 66 2e 20 20 54 68 65 0a 20 20 20  a leaf.  The.   
ec750 20 2a 2a 20 6e 65 78 74 20 43 65 6c 6c 20 61 66   ** next Cell af
ec760 74 65 72 20 74 68 65 20 6f 6e 65 20 74 6f 20 62  ter the one to b
ec770 65 20 64 65 6c 65 74 65 64 20 69 73 20 67 75 61  e deleted is gua
ec780 72 61 6e 74 65 65 64 20 74 6f 20 65 78 69 73 74  ranteed to exist
ec790 20 61 6e 64 0a 20 20 20 20 2a 2a 20 74 6f 20 62   and.    ** to b
ec7a0 65 20 61 20 6c 65 61 66 20 73 6f 20 77 65 20 63  e a leaf so we c
ec7b0 61 6e 20 75 73 65 20 69 74 2e 0a 20 20 20 20 2a  an use it..    *
ec7c0 2f 0a 20 20 20 20 42 74 43 75 72 73 6f 72 20 6c  /.    BtCursor l
ec7d0 65 61 66 43 75 72 3b 0a 20 20 20 20 75 6e 73 69  eafCur;.    unsi
ec7e0 67 6e 65 64 20 63 68 61 72 20 2a 70 4e 65 78 74  gned char *pNext
ec7f0 3b 0a 20 20 20 20 69 6e 74 20 73 7a 4e 65 78 74  ;.    int szNext
ec800 3b 20 20 2f 2a 20 54 68 65 20 63 6f 6d 70 69 6c  ;  /* The compil
ec810 65 72 20 77 61 72 6e 69 6e 67 20 69 73 20 77 72  er warning is wr
ec820 6f 6e 67 3a 20 73 7a 4e 65 78 74 20 69 73 20 61  ong: szNext is a
ec830 6c 77 61 79 73 20 0a 20 20 20 20 20 20 20 20 20  lways .         
ec840 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 69 74 69          ** initi
ec850 61 6c 69 7a 65 64 20 62 65 66 6f 72 65 20 75 73  alized before us
ec860 65 2e 20 20 41 64 64 69 6e 67 20 61 6e 20 65 78  e.  Adding an ex
ec870 74 72 61 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  tra initializati
ec880 6f 6e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  on.             
ec890 20 20 20 20 2a 2a 20 74 6f 20 73 69 6c 65 6e 63      ** to silenc
ec8a0 65 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 73  e the compiler s
ec8b0 6c 6f 77 73 20 64 6f 77 6e 20 74 68 65 20 63 6f  lows down the co
ec8c0 64 65 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e  de. */.    int n
ec8d0 6f 74 55 73 65 64 3b 0a 20 20 20 20 75 6e 73 69  otUsed;.    unsi
ec8e0 67 6e 65 64 20 63 68 61 72 20 2a 74 65 6d 70 43  gned char *tempC
ec8f0 65 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 61 73 73  ell = 0;.    ass
ec900 65 72 74 28 20 21 70 50 61 67 65 2d 3e 6c 65 61  ert( !pPage->lea
ec910 66 44 61 74 61 20 29 3b 0a 20 20 20 20 73 71 6c  fData );.    sql
ec920 69 74 65 33 42 74 72 65 65 47 65 74 54 65 6d 70  ite3BtreeGetTemp
ec930 43 75 72 73 6f 72 28 70 43 75 72 2c 20 26 6c 65  Cursor(pCur, &le
ec940 61 66 43 75 72 29 3b 0a 20 20 20 20 72 63 20 3d  afCur);.    rc =
ec950 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78   sqlite3BtreeNex
ec960 74 28 26 6c 65 61 66 43 75 72 2c 20 26 6e 6f 74  t(&leafCur, &not
ec970 55 73 65 64 29 3b 0a 20 20 20 20 69 66 28 20 72  Used);.    if( r
ec980 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
ec990 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
ec9a0 65 33 50 61 67 65 72 57 72 69 74 65 28 6c 65 61  e3PagerWrite(lea
ec9b0 66 43 75 72 2e 70 50 61 67 65 2d 3e 70 44 62 50  fCur.pPage->pDbP
ec9c0 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  age);.    }.    
ec9d0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
ec9e0 4b 20 29 7b 0a 20 20 20 20 20 20 54 52 41 43 45  K ){.      TRACE
ec9f0 28 28 22 44 45 4c 45 54 45 3a 20 74 61 62 6c 65  (("DELETE: table
eca00 3d 25 64 20 64 65 6c 65 74 65 20 69 6e 74 65 72  =%d delete inter
eca10 6e 61 6c 20 66 72 6f 6d 20 25 64 20 72 65 70 6c  nal from %d repl
eca20 61 63 65 20 66 72 6f 6d 20 6c 65 61 66 20 25 64  ace from leaf %d
eca30 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 70 43  \n",.         pC
eca40 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 50  ur->pgnoRoot, pP
eca50 61 67 65 2d 3e 70 67 6e 6f 2c 20 6c 65 61 66 43  age->pgno, leafC
eca60 75 72 2e 70 50 61 67 65 2d 3e 70 67 6e 6f 29 29  ur.pPage->pgno))
eca70 3b 0a 20 20 20 20 20 20 64 72 6f 70 43 65 6c 6c  ;.      dropCell
eca80 28 70 50 61 67 65 2c 20 70 43 75 72 2d 3e 69 64  (pPage, pCur->id
eca90 78 2c 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70  x, cellSizePtr(p
ecaa0 50 61 67 65 2c 20 70 43 65 6c 6c 29 29 3b 0a 20  Page, pCell));. 
ecab0 20 20 20 20 20 70 4e 65 78 74 20 3d 20 66 69 6e       pNext = fin
ecac0 64 43 65 6c 6c 28 6c 65 61 66 43 75 72 2e 70 50  dCell(leafCur.pP
ecad0 61 67 65 2c 20 6c 65 61 66 43 75 72 2e 69 64 78  age, leafCur.idx
ecae0 29 3b 0a 20 20 20 20 20 20 73 7a 4e 65 78 74 20  );.      szNext 
ecaf0 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 6c 65  = cellSizePtr(le
ecb00 61 66 43 75 72 2e 70 50 61 67 65 2c 20 70 4e 65  afCur.pPage, pNe
ecb10 78 74 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  xt);.      asser
ecb20 74 28 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28  t( MX_CELL_SIZE(
ecb30 70 42 74 29 3e 3d 73 7a 4e 65 78 74 2b 34 20 29  pBt)>=szNext+4 )
ecb40 3b 0a 20 20 20 20 20 20 74 65 6d 70 43 65 6c 6c  ;.      tempCell
ecb50 20 3d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 52   = sqliteMallocR
ecb60 61 77 28 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45  aw( MX_CELL_SIZE
ecb70 28 70 42 74 29 20 29 3b 0a 20 20 20 20 20 20 69  (pBt) );.      i
ecb80 66 28 20 74 65 6d 70 43 65 6c 6c 3d 3d 30 20 29  f( tempCell==0 )
ecb90 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53  {.        rc = S
ecba0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
ecbb0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
ecbc0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
ecbd0 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 69   ){.      rc = i
ecbe0 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 67 65 2c  nsertCell(pPage,
ecbf0 20 70 43 75 72 2d 3e 69 64 78 2c 20 70 4e 65 78   pCur->idx, pNex
ecc00 74 2d 34 2c 20 73 7a 4e 65 78 74 2b 34 2c 20 74  t-4, szNext+4, t
ecc10 65 6d 70 43 65 6c 6c 2c 20 30 29 3b 0a 20 20 20  empCell, 0);.   
ecc20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53   }.    if( rc==S
ecc30 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
ecc40 20 20 70 75 74 34 62 79 74 65 28 66 69 6e 64 4f    put4byte(findO
ecc50 76 65 72 66 6c 6f 77 43 65 6c 6c 28 70 50 61 67  verflowCell(pPag
ecc60 65 2c 20 70 43 75 72 2d 3e 69 64 78 29 2c 20 70  e, pCur->idx), p
ecc70 67 6e 6f 43 68 69 6c 64 29 3b 0a 20 20 20 20 20  gnoChild);.     
ecc80 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 50   rc = balance(pP
ecc90 61 67 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20  age, 0);.    }. 
ecca0 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
eccb0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 64 72  E_OK ){.      dr
eccc0 6f 70 43 65 6c 6c 28 6c 65 61 66 43 75 72 2e 70  opCell(leafCur.p
eccd0 50 61 67 65 2c 20 6c 65 61 66 43 75 72 2e 69 64  Page, leafCur.id
ecce0 78 2c 20 73 7a 4e 65 78 74 29 3b 0a 20 20 20 20  x, szNext);.    
eccf0 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 6c    rc = balance(l
ecd00 65 61 66 43 75 72 2e 70 50 61 67 65 2c 20 30 29  eafCur.pPage, 0)
ecd10 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
ecd20 74 65 46 72 65 65 28 74 65 6d 70 43 65 6c 6c 29  teFree(tempCell)
ecd30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ;.    sqlite3Btr
ecd40 65 65 52 65 6c 65 61 73 65 54 65 6d 70 43 75 72  eeReleaseTempCur
ecd50 73 6f 72 28 26 6c 65 61 66 43 75 72 29 3b 0a 20  sor(&leafCur);. 
ecd60 20 7d 65 6c 73 65 7b 0a 20 20 20 20 54 52 41 43   }else{.    TRAC
ecd70 45 28 28 22 44 45 4c 45 54 45 3a 20 74 61 62 6c  E(("DELETE: tabl
ecd80 65 3d 25 64 20 64 65 6c 65 74 65 20 66 72 6f 6d  e=%d delete from
ecd90 20 6c 65 61 66 20 25 64 5c 6e 22 2c 0a 20 20 20   leaf %d\n",.   
ecda0 20 20 20 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f      pCur->pgnoRo
ecdb0 6f 74 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29  ot, pPage->pgno)
ecdc0 29 3b 0a 20 20 20 20 64 72 6f 70 43 65 6c 6c 28  );.    dropCell(
ecdd0 70 50 61 67 65 2c 20 70 43 75 72 2d 3e 69 64 78  pPage, pCur->idx
ecde0 2c 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50  , cellSizePtr(pP
ecdf0 61 67 65 2c 20 70 43 65 6c 6c 29 29 3b 0a 20 20  age, pCell));.  
ece00 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 70    rc = balance(p
ece10 50 61 67 65 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  Page, 0);.  }.  
ece20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
ece30 4b 20 29 7b 0a 20 20 20 20 6d 6f 76 65 54 6f 52  K ){.    moveToR
ece40 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d 0a 20  oot(pCur);.  }. 
ece50 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
ece60 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65  *.** Create a ne
ece70 77 20 42 54 72 65 65 20 74 61 62 6c 65 2e 20 20  w BTree table.  
ece80 57 72 69 74 65 20 69 6e 74 6f 20 2a 70 69 54 61  Write into *piTa
ece90 62 6c 65 20 74 68 65 20 70 61 67 65 0a 2a 2a 20  ble the page.** 
ecea0 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 72  number for the r
eceb0 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20  oot page of the 
ecec0 6e 65 77 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  new table..**.**
eced0 20 54 68 65 20 74 79 70 65 20 6f 66 20 74 79 70   The type of typ
ecee0 65 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20  e is determined 
ecef0 62 79 20 74 68 65 20 66 6c 61 67 73 20 70 61 72  by the flags par
ecf00 61 6d 65 74 65 72 2e 20 20 4f 6e 6c 79 20 74 68  ameter.  Only th
ecf10 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 76  e.** following v
ecf20 61 6c 75 65 73 20 6f 66 20 66 6c 61 67 73 20 61  alues of flags a
ecf30 72 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20  re currently in 
ecf40 75 73 65 2e 20 20 4f 74 68 65 72 20 76 61 6c 75  use.  Other valu
ecf50 65 73 20 66 6f 72 0a 2a 2a 20 66 6c 61 67 73 20  es for.** flags 
ecf60 6d 69 67 68 74 20 6e 6f 74 20 77 6f 72 6b 3a 0a  might not work:.
ecf70 2a 2a 0a 2a 2a 20 20 20 20 20 42 54 52 45 45 5f  **.**     BTREE_
ecf80 49 4e 54 4b 45 59 7c 42 54 52 45 45 5f 4c 45 41  INTKEY|BTREE_LEA
ecf90 46 44 41 54 41 20 20 20 20 20 55 73 65 64 20 66  FDATA     Used f
ecfa0 6f 72 20 53 51 4c 20 74 61 62 6c 65 73 20 77 69  or SQL tables wi
ecfb0 74 68 20 72 6f 77 69 64 20 6b 65 79 73 0a 2a 2a  th rowid keys.**
ecfc0 20 20 20 20 20 42 54 52 45 45 5f 5a 45 52 4f 44       BTREE_ZEROD
ecfd0 41 54 41 20 20 20 20 20 20 20 20 20 20 20 20 20  ATA             
ecfe0 20 20 20 20 20 55 73 65 64 20 66 6f 72 20 53 51       Used for SQ
ecff0 4c 20 69 6e 64 69 63 65 73 0a 2a 2f 0a 53 51 4c  L indices.*/.SQL
ed000 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
ed010 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61  sqlite3BtreeCrea
ed020 74 65 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70  teTable(Btree *p
ed030 2c 20 69 6e 74 20 2a 70 69 54 61 62 6c 65 2c 20  , int *piTable, 
ed040 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 42 74  int flags){.  Bt
ed050 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d  Shared *pBt = p-
ed060 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20  >pBt;.  MemPage 
ed070 2a 70 52 6f 6f 74 3b 0a 20 20 50 67 6e 6f 20 70  *pRoot;.  Pgno p
ed080 67 6e 6f 52 6f 6f 74 3b 0a 20 20 69 6e 74 20 72  gnoRoot;.  int r
ed090 63 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 69 6e  c;.  if( pBt->in
ed0a0 54 72 61 6e 73 61 63 74 69 6f 6e 21 3d 54 52 41  Transaction!=TRA
ed0b0 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20  NS_WRITE ){.    
ed0c0 2f 2a 20 4d 75 73 74 20 73 74 61 72 74 20 61 20  /* Must start a 
ed0d0 74 72 61 6e 73 61 63 74 69 6f 6e 20 66 69 72 73  transaction firs
ed0e0 74 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20  t */.    return 
ed0f0 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3f 20  pBt->readOnly ? 
ed100 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20  SQLITE_READONLY 
ed110 3a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  : SQLITE_ERROR;.
ed120 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21 70    }.  assert( !p
ed130 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 3b 0a  Bt->readOnly );.
ed140 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
ed150 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
ed160 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74   rc = allocateBt
ed170 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 52  reePage(pBt, &pR
ed180 6f 6f 74 2c 20 26 70 67 6e 6f 52 6f 6f 74 2c 20  oot, &pgnoRoot, 
ed190 31 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 20  1, 0);.  if( rc 
ed1a0 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6c  ) return rc;.#el
ed1b0 73 65 0a 20 20 69 66 28 20 70 42 74 2d 3e 61 75  se.  if( pBt->au
ed1c0 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20  toVacuum ){.    
ed1d0 50 67 6e 6f 20 70 67 6e 6f 4d 6f 76 65 3b 20 20  Pgno pgnoMove;  
ed1e0 20 20 20 20 2f 2a 20 4d 6f 76 65 20 61 20 70 61      /* Move a pa
ed1f0 67 65 20 68 65 72 65 20 74 6f 20 6d 61 6b 65 20  ge here to make 
ed200 72 6f 6f 6d 20 66 6f 72 20 74 68 65 20 72 6f 6f  room for the roo
ed210 74 2d 70 61 67 65 20 2a 2f 0a 20 20 20 20 4d 65  t-page */.    Me
ed220 6d 50 61 67 65 20 2a 70 50 61 67 65 4d 6f 76 65  mPage *pPageMove
ed230 3b 20 2f 2a 20 54 68 65 20 70 61 67 65 20 74 6f  ; /* The page to
ed240 20 6d 6f 76 65 20 74 6f 2e 20 2a 2f 0a 0a 20 20   move to. */..  
ed250 20 20 2f 2a 20 43 72 65 61 74 69 6e 67 20 61 20    /* Creating a 
ed260 6e 65 77 20 74 61 62 6c 65 20 6d 61 79 20 70 72  new table may pr
ed270 6f 62 61 62 6c 79 20 72 65 71 75 69 72 65 20 6d  obably require m
ed280 6f 76 69 6e 67 20 61 6e 20 65 78 69 73 74 69 6e  oving an existin
ed290 67 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 2a  g database.    *
ed2a0 2a 20 74 6f 20 6d 61 6b 65 20 72 6f 6f 6d 20 66  * to make room f
ed2b0 6f 72 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65  or the new table
ed2c0 73 20 72 6f 6f 74 20 70 61 67 65 2e 20 49 6e 20  s root page. In 
ed2d0 63 61 73 65 20 74 68 69 73 20 70 61 67 65 20 74  case this page t
ed2e0 75 72 6e 73 0a 20 20 20 20 2a 2a 20 6f 75 74 20  urns.    ** out 
ed2f0 74 6f 20 62 65 20 61 6e 20 6f 76 65 72 66 6c 6f  to be an overflo
ed300 77 20 70 61 67 65 2c 20 64 65 6c 65 74 65 20 61  w page, delete a
ed310 6c 6c 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  ll overflow page
ed320 2d 6d 61 70 20 63 61 63 68 65 73 0a 20 20 20 20  -map caches.    
ed330 2a 2a 20 68 65 6c 64 20 62 79 20 6f 70 65 6e 20  ** held by open 
ed340 63 75 72 73 6f 72 73 2e 0a 20 20 20 20 2a 2f 0a  cursors..    */.
ed350 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 41 6c      invalidateAl
ed360 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70  lOverflowCache(p
ed370 42 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61  Bt);..    /* Rea
ed380 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6d  d the value of m
ed390 65 74 61 5b 33 5d 20 66 72 6f 6d 20 74 68 65 20  eta[3] from the 
ed3a0 64 61 74 61 62 61 73 65 20 74 6f 20 64 65 74 65  database to dete
ed3b0 72 6d 69 6e 65 20 77 68 65 72 65 20 74 68 65 0a  rmine where the.
ed3c0 20 20 20 20 2a 2a 20 72 6f 6f 74 20 70 61 67 65      ** root page
ed3d0 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c   of the new tabl
ed3e0 65 20 73 68 6f 75 6c 64 20 67 6f 2e 20 6d 65 74  e should go. met
ed3f0 61 5b 33 5d 20 69 73 20 74 68 65 20 6c 61 72 67  a[3] is the larg
ed400 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20  est root-page.  
ed410 20 20 2a 2a 20 63 72 65 61 74 65 64 20 73 6f 20    ** created so 
ed420 66 61 72 2c 20 73 6f 20 74 68 65 20 6e 65 77 20  far, so the new 
ed430 72 6f 6f 74 2d 70 61 67 65 20 69 73 20 28 6d 65  root-page is (me
ed440 74 61 5b 33 5d 2b 31 29 2e 0a 20 20 20 20 2a 2f  ta[3]+1)..    */
ed450 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
ed460 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 70 2c  3BtreeGetMeta(p,
ed470 20 34 2c 20 26 70 67 6e 6f 52 6f 6f 74 29 3b 0a   4, &pgnoRoot);.
ed480 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
ed490 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
ed4a0 63 3b 0a 20 20 20 20 70 67 6e 6f 52 6f 6f 74 2b  c;.    pgnoRoot+
ed4b0 2b 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 6e  +;..    /* The n
ed4c0 65 77 20 72 6f 6f 74 2d 70 61 67 65 20 6d 61 79  ew root-page may
ed4d0 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 65   not be allocate
ed4e0 64 20 6f 6e 20 61 20 70 6f 69 6e 74 65 72 2d 6d  d on a pointer-m
ed4f0 61 70 20 70 61 67 65 2c 20 6f 72 20 74 68 65 0a  ap page, or the.
ed500 20 20 20 20 2a 2a 20 50 45 4e 44 49 4e 47 5f 42      ** PENDING_B
ed510 59 54 45 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f  YTE page..    */
ed520 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 52 6f 6f  .    if( pgnoRoo
ed530 74 3d 3d 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f  t==PTRMAP_PAGENO
ed540 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 29 20  (pBt, pgnoRoot) 
ed550 7c 7c 0a 20 20 20 20 20 20 20 20 70 67 6e 6f 52  ||.        pgnoR
ed560 6f 6f 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54  oot==PENDING_BYT
ed570 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20  E_PAGE(pBt) ){. 
ed580 20 20 20 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b       pgnoRoot++;
ed590 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
ed5a0 74 28 20 70 67 6e 6f 52 6f 6f 74 3e 3d 33 20 29  t( pgnoRoot>=3 )
ed5b0 3b 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61  ;..    /* Alloca
ed5c0 74 65 20 61 20 70 61 67 65 2e 20 54 68 65 20 70  te a page. The p
ed5d0 61 67 65 20 74 68 61 74 20 63 75 72 72 65 6e 74  age that current
ed5e0 6c 79 20 72 65 73 69 64 65 73 20 61 74 20 70 67  ly resides at pg
ed5f0 6e 6f 52 6f 6f 74 20 77 69 6c 6c 0a 20 20 20 20  noRoot will.    
ed600 2a 2a 20 62 65 20 6d 6f 76 65 64 20 74 6f 20 74  ** be moved to t
ed610 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67  he allocated pag
ed620 65 20 28 75 6e 6c 65 73 73 20 74 68 65 20 61 6c  e (unless the al
ed630 6c 6f 63 61 74 65 64 20 70 61 67 65 20 68 61 70  located page hap
ed640 70 65 6e 73 0a 20 20 20 20 2a 2a 20 74 6f 20 72  pens.    ** to r
ed650 65 73 69 64 65 20 61 74 20 70 67 6e 6f 52 6f 6f  eside at pgnoRoo
ed660 74 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72  t)..    */.    r
ed670 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65  c = allocateBtre
ed680 65 50 61 67 65 28 70 42 74 2c 20 26 70 50 61 67  ePage(pBt, &pPag
ed690 65 4d 6f 76 65 2c 20 26 70 67 6e 6f 4d 6f 76 65  eMove, &pgnoMove
ed6a0 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 31 29 3b 0a  , pgnoRoot, 1);.
ed6b0 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
ed6c0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
ed6d0 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
ed6e0 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 4d 6f 76  .    if( pgnoMov
ed6f0 65 21 3d 70 67 6e 6f 52 6f 6f 74 20 29 7b 0a 20  e!=pgnoRoot ){. 
ed700 20 20 20 20 20 2f 2a 20 70 67 6e 6f 52 6f 6f 74       /* pgnoRoot
ed710 20 69 73 20 74 68 65 20 70 61 67 65 20 74 68 61   is the page tha
ed720 74 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 66  t will be used f
ed730 6f 72 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65  or the root-page
ed740 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20 74 68 65   of.      ** the
ed750 20 6e 65 77 20 74 61 62 6c 65 20 28 61 73 73 75   new table (assu
ed760 6d 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 64 69  ming an error di
ed770 64 20 6e 6f 74 20 6f 63 63 75 72 29 2e 20 42 75  d not occur). Bu
ed780 74 20 77 65 20 77 65 72 65 0a 20 20 20 20 20 20  t we were.      
ed790 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 70 67 6e  ** allocated pgn
ed7a0 6f 4d 6f 76 65 2e 20 49 66 20 72 65 71 75 69 72  oMove. If requir
ed7b0 65 64 20 28 69 2e 65 2e 20 69 66 20 69 74 20 77  ed (i.e. if it w
ed7c0 61 73 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 64  as not allocated
ed7d0 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 65 78 74  .      ** by ext
ed7e0 65 6e 64 69 6e 67 20 74 68 65 20 66 69 6c 65 29  ending the file)
ed7f0 2c 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 61  , the current pa
ed800 67 65 20 61 74 20 70 6f 73 69 74 69 6f 6e 20 70  ge at position p
ed810 67 6e 6f 4d 6f 76 65 0a 20 20 20 20 20 20 2a 2a  gnoMove.      **
ed820 20 69 73 20 61 6c 72 65 61 64 79 20 6a 6f 75 72   is already jour
ed830 6e 61 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a  naled..      */.
ed840 20 20 20 20 20 20 75 38 20 65 54 79 70 65 3b 0a        u8 eType;.
ed850 20 20 20 20 20 20 50 67 6e 6f 20 69 50 74 72 50        Pgno iPtrP
ed860 61 67 65 3b 0a 0a 20 20 20 20 20 20 72 65 6c 65  age;..      rele
ed870 61 73 65 50 61 67 65 28 70 50 61 67 65 4d 6f 76  asePage(pPageMov
ed880 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4d 6f  e);..      /* Mo
ed890 76 65 20 74 68 65 20 70 61 67 65 20 63 75 72 72  ve the page curr
ed8a0 65 6e 74 6c 79 20 61 74 20 70 67 6e 6f 52 6f 6f  ently at pgnoRoo
ed8b0 74 20 74 6f 20 70 67 6e 6f 4d 6f 76 65 2e 20 2a  t to pgnoMove. *
ed8c0 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  /.      rc = sql
ed8d0 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65  ite3BtreeGetPage
ed8e0 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20  (pBt, pgnoRoot, 
ed8f0 26 70 52 6f 6f 74 2c 20 30 29 3b 0a 20 20 20 20  &pRoot, 0);.    
ed900 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
ed910 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  _OK ){.        r
ed920 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
ed930 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72  }.      rc = ptr
ed940 6d 61 70 47 65 74 28 70 42 74 2c 20 70 67 6e 6f  mapGet(pBt, pgno
ed950 52 6f 6f 74 2c 20 26 65 54 79 70 65 2c 20 26 69  Root, &eType, &i
ed960 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20 20 20  PtrPage);.      
ed970 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
ed980 4b 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d  K || eType==PTRM
ed990 41 50 5f 52 4f 4f 54 50 41 47 45 20 7c 7c 20 65  AP_ROOTPAGE || e
ed9a0 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45  Type==PTRMAP_FRE
ed9b0 45 50 41 47 45 20 29 7b 0a 20 20 20 20 20 20 20  EPAGE ){.       
ed9c0 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f   releasePage(pRo
ed9d0 6f 74 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74  ot);.        ret
ed9e0 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a  urn rc;.      }.
ed9f0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 65 54        assert( eT
eda00 79 70 65 21 3d 50 54 52 4d 41 50 5f 52 4f 4f 54  ype!=PTRMAP_ROOT
eda10 50 41 47 45 20 29 3b 0a 20 20 20 20 20 20 61 73  PAGE );.      as
eda20 73 65 72 74 28 20 65 54 79 70 65 21 3d 50 54 52  sert( eType!=PTR
eda30 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29 3b 0a  MAP_FREEPAGE );.
eda40 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
eda50 65 33 50 61 67 65 72 57 72 69 74 65 28 70 52 6f  e3PagerWrite(pRo
eda60 6f 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  ot->pDbPage);.  
eda70 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
eda80 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
eda90 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f   releasePage(pRo
edaa0 6f 74 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74  ot);.        ret
edab0 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a  urn rc;.      }.
edac0 20 20 20 20 20 20 72 63 20 3d 20 72 65 6c 6f 63        rc = reloc
edad0 61 74 65 50 61 67 65 28 70 42 74 2c 20 70 52 6f  atePage(pBt, pRo
edae0 6f 74 2c 20 65 54 79 70 65 2c 20 69 50 74 72 50  ot, eType, iPtrP
edaf0 61 67 65 2c 20 70 67 6e 6f 4d 6f 76 65 29 3b 0a  age, pgnoMove);.
edb00 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67        releasePag
edb10 65 28 70 52 6f 6f 74 29 3b 0a 0a 20 20 20 20 20  e(pRoot);..     
edb20 20 2f 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 70   /* Obtain the p
edb30 61 67 65 20 61 74 20 70 67 6e 6f 52 6f 6f 74 20  age at pgnoRoot 
edb40 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  */.      if( rc!
edb50 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
edb60 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
edb70 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
edb80 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
edb90 47 65 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e  GetPage(pBt, pgn
edba0 6f 52 6f 6f 74 2c 20 26 70 52 6f 6f 74 2c 20 30  oRoot, &pRoot, 0
edbb0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
edbc0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
edbd0 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
edbe0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
edbf0 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
edc00 57 72 69 74 65 28 70 52 6f 6f 74 2d 3e 70 44 62  Write(pRoot->pDb
edc10 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28  Page);.      if(
edc20 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
edc30 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73  {.        releas
edc40 65 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a 20 20  ePage(pRoot);.  
edc50 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
edc60 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
edc70 73 65 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74 20  se{.      pRoot 
edc80 3d 20 70 50 61 67 65 4d 6f 76 65 3b 0a 20 20 20  = pPageMove;.   
edc90 20 7d 20 0a 0a 20 20 20 20 2f 2a 20 55 70 64 61   } ..    /* Upda
edca0 74 65 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d  te the pointer-m
edcb0 61 70 20 61 6e 64 20 6d 65 74 61 2d 64 61 74 61  ap and meta-data
edcc0 20 77 69 74 68 20 74 68 65 20 6e 65 77 20 72 6f   with the new ro
edcd0 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 2e 20  ot-page number. 
edce0 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 74 72 6d  */.    rc = ptrm
edcf0 61 70 50 75 74 28 70 42 74 2c 20 70 67 6e 6f 52  apPut(pBt, pgnoR
edd00 6f 6f 74 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54  oot, PTRMAP_ROOT
edd10 50 41 47 45 2c 20 30 29 3b 0a 20 20 20 20 69 66  PAGE, 0);.    if
edd20 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65  ( rc ){.      re
edd30 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29  leasePage(pRoot)
edd40 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  ;.      return r
edd50 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20  c;.    }.    rc 
edd60 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70  = sqlite3BtreeUp
edd70 64 61 74 65 4d 65 74 61 28 70 2c 20 34 2c 20 70  dateMeta(p, 4, p
edd80 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 20 69 66  gnoRoot);.    if
edd90 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65  ( rc ){.      re
edda0 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29  leasePage(pRoot)
eddb0 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  ;.      return r
eddc0 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 7d 65 6c 73  c;.    }..  }els
eddd0 65 7b 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f  e{.    rc = allo
edde0 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42  cateBtreePage(pB
eddf0 74 2c 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f  t, &pRoot, &pgno
ede00 52 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20  Root, 1, 0);.   
ede10 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
ede20 20 72 63 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a   rc;.  }.#endif.
ede30 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
ede40 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c  3PagerIswriteabl
ede50 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65  e(pRoot->pDbPage
ede60 29 20 29 3b 0a 20 20 7a 65 72 6f 50 61 67 65 28  ) );.  zeroPage(
ede70 70 52 6f 6f 74 2c 20 66 6c 61 67 73 20 7c 20 50  pRoot, flags | P
ede80 54 46 5f 4c 45 41 46 29 3b 0a 20 20 73 71 6c 69  TF_LEAF);.  sqli
ede90 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 52  te3PagerUnref(pR
edea0 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  oot->pDbPage);. 
edeb0 20 2a 70 69 54 61 62 6c 65 20 3d 20 28 69 6e 74   *piTable = (int
edec0 29 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 72 65 74  )pgnoRoot;.  ret
eded0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
edee0 0a 0a 2f 2a 0a 2a 2a 20 45 72 61 73 65 20 74 68  ../*.** Erase th
edef0 65 20 67 69 76 65 6e 20 64 61 74 61 62 61 73 65  e given database
edf00 20 70 61 67 65 20 61 6e 64 20 61 6c 6c 20 69 74   page and all it
edf10 73 20 63 68 69 6c 64 72 65 6e 2e 20 20 52 65 74  s children.  Ret
edf20 75 72 6e 0a 2a 2a 20 74 68 65 20 70 61 67 65 20  urn.** the page 
edf30 74 6f 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e  to the freelist.
edf40 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
edf50 6c 65 61 72 44 61 74 61 62 61 73 65 50 61 67 65  learDatabasePage
edf60 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  (.  BtShared *pB
edf70 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t,           /* 
edf80 54 68 65 20 42 54 72 65 65 20 74 68 61 74 20 63  The BTree that c
edf90 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 61 62 6c  ontains the tabl
edfa0 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f  e */.  Pgno pgno
edfb0 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
edfc0 50 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 63  Page number to c
edfd0 6c 65 61 72 20 2a 2f 0a 20 20 4d 65 6d 50 61 67  lear */.  MemPag
edfe0 65 20 2a 70 50 61 72 65 6e 74 2c 20 20 20 20 20  e *pParent,     
edff0 2f 2a 20 50 61 72 65 6e 74 20 70 61 67 65 2e 20  /* Parent page. 
ee000 20 4e 55 4c 4c 20 66 6f 72 20 74 68 65 20 72 6f   NULL for the ro
ee010 6f 74 20 2a 2f 0a 20 20 69 6e 74 20 66 72 65 65  ot */.  int free
ee020 50 61 67 65 46 6c 61 67 20 20 20 20 20 20 2f 2a  PageFlag      /*
ee030 20 44 65 61 6c 6c 6f 63 61 74 65 20 70 61 67 65   Deallocate page
ee040 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20   if true */.){. 
ee050 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20   MemPage *pPage 
ee060 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  = 0;.  int rc;. 
ee070 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
ee080 70 43 65 6c 6c 3b 0a 20 20 69 6e 74 20 69 3b 0a  pCell;.  int i;.
ee090 0a 20 20 69 66 28 20 70 67 6e 6f 3e 73 71 6c 69  .  if( pgno>sqli
ee0a0 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e  te3PagerPagecoun
ee0b0 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29 20 29  t(pBt->pPager) )
ee0c0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
ee0d0 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
ee0e0 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 67 65  ;.  }..  rc = ge
ee0f0 74 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74  tAndInitPage(pBt
ee100 2c 20 70 67 6e 6f 2c 20 26 70 50 61 67 65 2c 20  , pgno, &pPage, 
ee110 70 50 61 72 65 6e 74 29 3b 0a 20 20 69 66 28 20  pParent);.  if( 
ee120 72 63 20 29 20 67 6f 74 6f 20 63 6c 65 61 72 64  rc ) goto cleard
ee130 61 74 61 62 61 73 65 70 61 67 65 5f 6f 75 74 3b  atabasepage_out;
ee140 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50  .  for(i=0; i<pP
ee150 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29  age->nCell; i++)
ee160 7b 0a 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 69  {.    pCell = fi
ee170 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 29  ndCell(pPage, i)
ee180 3b 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65  ;.    if( !pPage
ee190 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20  ->leaf ){.      
ee1a0 72 63 20 3d 20 63 6c 65 61 72 44 61 74 61 62 61  rc = clearDataba
ee1b0 73 65 50 61 67 65 28 70 42 74 2c 20 67 65 74 34  sePage(pBt, get4
ee1c0 62 79 74 65 28 70 43 65 6c 6c 29 2c 20 70 50 61  byte(pCell), pPa
ee1d0 67 65 2d 3e 70 50 61 72 65 6e 74 2c 20 31 29 3b  ge->pParent, 1);
ee1e0 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20  .      if( rc ) 
ee1f0 67 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 62 61  goto cleardataba
ee200 73 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20  sepage_out;.    
ee210 7d 0a 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72  }.    rc = clear
ee220 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 43 65 6c  Cell(pPage, pCel
ee230 6c 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  l);.    if( rc )
ee240 20 67 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 62   goto cleardatab
ee250 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d  asepage_out;.  }
ee260 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c  .  if( !pPage->l
ee270 65 61 66 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  eaf ){.    rc = 
ee280 63 6c 65 61 72 44 61 74 61 62 61 73 65 50 61 67  clearDatabasePag
ee290 65 28 70 42 74 2c 20 67 65 74 34 62 79 74 65 28  e(pBt, get4byte(
ee2a0 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 38 5d  &pPage->aData[8]
ee2b0 29 2c 20 70 50 61 67 65 2d 3e 70 50 61 72 65 6e  ), pPage->pParen
ee2c0 74 2c 20 31 29 3b 0a 20 20 20 20 69 66 28 20 72  t, 1);.    if( r
ee2d0 63 20 29 20 67 6f 74 6f 20 63 6c 65 61 72 64 61  c ) goto clearda
ee2e0 74 61 62 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a  tabasepage_out;.
ee2f0 20 20 7d 0a 20 20 69 66 28 20 66 72 65 65 50 61    }.  if( freePa
ee300 67 65 46 6c 61 67 20 29 7b 0a 20 20 20 20 72 63  geFlag ){.    rc
ee310 20 3d 20 66 72 65 65 50 61 67 65 28 70 50 61 67   = freePage(pPag
ee320 65 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  e);.  }else if( 
ee330 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67  (rc = sqlite3Pag
ee340 65 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70  erWrite(pPage->p
ee350 44 62 50 61 67 65 29 29 3d 3d 30 20 29 7b 0a 20  DbPage))==0 ){. 
ee360 20 20 20 7a 65 72 6f 50 61 67 65 28 70 50 61 67     zeroPage(pPag
ee370 65 2c 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b  e, pPage->aData[
ee380 30 5d 20 7c 20 50 54 46 5f 4c 45 41 46 29 3b 0a  0] | PTF_LEAF);.
ee390 20 20 7d 0a 0a 63 6c 65 61 72 64 61 74 61 62 61    }..cleardataba
ee3a0 73 65 70 61 67 65 5f 6f 75 74 3a 0a 20 20 72 65  sepage_out:.  re
ee3b0 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29  leasePage(pPage)
ee3c0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
ee3d0 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61  ../*.** Delete a
ee3e0 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  ll information f
ee3f0 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20 74 61 62  rom a single tab
ee400 6c 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  le in the databa
ee410 73 65 2e 20 20 69 54 61 62 6c 65 20 69 73 0a 2a  se.  iTable is.*
ee420 2a 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65  * the page numbe
ee430 72 20 6f 66 20 74 68 65 20 72 6f 6f 74 20 6f 66  r of the root of
ee440 20 74 68 65 20 74 61 62 6c 65 2e 20 20 41 66 74   the table.  Aft
ee450 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  er this routine 
ee460 72 65 74 75 72 6e 73 2c 0a 2a 2a 20 74 68 65 20  returns,.** the 
ee470 72 6f 6f 74 20 70 61 67 65 20 69 73 20 65 6d 70  root page is emp
ee480 74 79 2c 20 62 75 74 20 73 74 69 6c 6c 20 65 78  ty, but still ex
ee490 69 73 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ists..**.** This
ee4a0 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 66 61   routine will fa
ee4b0 69 6c 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4c  il with SQLITE_L
ee4c0 4f 43 4b 45 44 20 69 66 20 74 68 65 72 65 20 61  OCKED if there a
ee4d0 72 65 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a 20 72  re any open.** r
ee4e0 65 61 64 20 63 75 72 73 6f 72 73 20 6f 6e 20 74  ead cursors on t
ee4f0 68 65 20 74 61 62 6c 65 2e 20 20 4f 70 65 6e 20  he table.  Open 
ee500 77 72 69 74 65 20 63 75 72 73 6f 72 73 20 61 72  write cursors ar
ee510 65 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 0a 2a  e moved to the.*
ee520 2a 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 61  * root of the ta
ee530 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ble..*/.SQLITE_P
ee540 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
ee550 65 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c  e3BtreeClearTabl
ee560 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20  e(Btree *p, int 
ee570 69 54 61 62 6c 65 29 7b 0a 20 20 69 6e 74 20 72  iTable){.  int r
ee580 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  c;.  BtShared *p
ee590 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 69  Bt = p->pBt;.  i
ee5a0 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 21 3d 54  f( p->inTrans!=T
ee5b0 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20  RANS_WRITE ){.  
ee5c0 20 20 72 65 74 75 72 6e 20 70 42 74 2d 3e 72 65    return pBt->re
ee5d0 61 64 4f 6e 6c 79 20 3f 20 53 51 4c 49 54 45 5f  adOnly ? SQLITE_
ee5e0 52 45 41 44 4f 4e 4c 59 20 3a 20 53 51 4c 49 54  READONLY : SQLIT
ee5f0 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 72  E_ERROR;.  }.  r
ee600 63 20 3d 20 63 68 65 63 6b 52 65 61 64 4c 6f 63  c = checkReadLoc
ee610 6b 73 28 70 2c 20 69 54 61 62 6c 65 2c 20 30 29  ks(p, iTable, 0)
ee620 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20  ;.  if( rc ){.  
ee630 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
ee640 0a 0a 20 20 2f 2a 20 53 61 76 65 20 74 68 65 20  ..  /* Save the 
ee650 70 6f 73 69 74 69 6f 6e 20 6f 66 20 61 6c 6c 20  position of all 
ee660 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20  cursors open on 
ee670 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20  this table */.  
ee680 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28  if( SQLITE_OK!=(
ee690 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 73  rc = saveAllCurs
ee6a0 6f 72 73 28 70 42 74 2c 20 69 54 61 62 6c 65 2c  ors(pBt, iTable,
ee6b0 20 30 29 29 20 29 7b 0a 20 20 20 20 72 65 74 75   0)) ){.    retu
ee6c0 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 72 65  rn rc;.  }..  re
ee6d0 74 75 72 6e 20 63 6c 65 61 72 44 61 74 61 62 61  turn clearDataba
ee6e0 73 65 50 61 67 65 28 70 42 74 2c 20 28 50 67 6e  sePage(pBt, (Pgn
ee6f0 6f 29 69 54 61 62 6c 65 2c 20 30 2c 20 30 29 3b  o)iTable, 0, 0);
ee700 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72 61 73 65 20  .}../*.** Erase 
ee710 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  all information 
ee720 69 6e 20 61 20 74 61 62 6c 65 20 61 6e 64 20 61  in a table and a
ee730 64 64 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74  dd the root of t
ee740 68 65 20 74 61 62 6c 65 20 74 6f 0a 2a 2a 20 74  he table to.** t
ee750 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 45 78  he freelist.  Ex
ee760 63 65 70 74 2c 20 74 68 65 20 72 6f 6f 74 20 6f  cept, the root o
ee770 66 20 74 68 65 20 70 72 69 6e 63 69 70 6c 65 20  f the principle 
ee780 74 61 62 6c 65 20 28 74 68 65 20 6f 6e 65 20 6f  table (the one o
ee790 6e 0a 2a 2a 20 70 61 67 65 20 31 29 20 69 73 20  n.** page 1) is 
ee7a0 6e 65 76 65 72 20 61 64 64 65 64 20 74 6f 20 74  never added to t
ee7b0 68 65 20 66 72 65 65 6c 69 73 74 2e 0a 2a 2a 0a  he freelist..**.
ee7c0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
ee7d0 77 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 53  will fail with S
ee7e0 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66 20  QLITE_LOCKED if 
ee7f0 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 6f 70  there are any op
ee800 65 6e 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f 6e  en.** cursors on
ee810 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a   the table..**.*
ee820 2a 20 49 66 20 41 55 54 4f 56 41 43 55 55 4d 20  * If AUTOVACUUM 
ee830 69 73 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 74  is enabled and t
ee840 68 65 20 70 61 67 65 20 61 74 20 69 54 61 62 6c  he page at iTabl
ee850 65 20 69 73 20 6e 6f 74 20 74 68 65 20 6c 61 73  e is not the las
ee860 74 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 69  t.** root page i
ee870 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
ee880 69 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 6c 61  ile, then the la
ee890 73 74 20 72 6f 6f 74 20 70 61 67 65 20 0a 2a 2a  st root page .**
ee8a0 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
ee8b0 20 66 69 6c 65 20 69 73 20 6d 6f 76 65 64 20 69   file is moved i
ee8c0 6e 74 6f 20 74 68 65 20 73 6c 6f 74 20 66 6f 72  nto the slot for
ee8d0 6d 65 72 6c 79 20 6f 63 63 75 70 69 65 64 20 62  merly occupied b
ee8e0 79 0a 2a 2a 20 69 54 61 62 6c 65 20 61 6e 64 20  y.** iTable and 
ee8f0 74 68 61 74 20 6c 61 73 74 20 73 6c 6f 74 20 66  that last slot f
ee900 6f 72 6d 65 72 6c 79 20 6f 63 63 75 70 69 65 64  ormerly occupied
ee910 20 62 79 20 74 68 65 20 6c 61 73 74 20 72 6f 6f   by the last roo
ee920 74 20 70 61 67 65 0a 2a 2a 20 69 73 20 61 64 64  t page.** is add
ee930 65 64 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69  ed to the freeli
ee940 73 74 20 69 6e 73 74 65 61 64 20 6f 66 20 69 54  st instead of iT
ee950 61 62 6c 65 2e 20 20 49 6e 20 74 68 69 73 20 73  able.  In this s
ee960 61 79 2c 20 61 6c 6c 0a 2a 2a 20 72 6f 6f 74 20  ay, all.** root 
ee970 70 61 67 65 73 20 61 72 65 20 6b 65 70 74 20 61  pages are kept a
ee980 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  t the beginning 
ee990 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
ee9a0 66 69 6c 65 2c 20 77 68 69 63 68 0a 2a 2a 20 69  file, which.** i
ee9b0 73 20 6e 65 63 65 73 73 61 72 79 20 66 6f 72 20  s necessary for 
ee9c0 41 55 54 4f 56 41 43 55 55 4d 20 74 6f 20 77 6f  AUTOVACUUM to wo
ee9d0 72 6b 20 72 69 67 68 74 2e 20 20 2a 70 69 4d 6f  rk right.  *piMo
ee9e0 76 65 64 20 69 73 20 73 65 74 20 74 6f 20 74 68  ved is set to th
ee9f0 65 20 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65  e .** page numbe
eea00 72 20 74 68 61 74 20 75 73 65 64 20 74 6f 20 62  r that used to b
eea10 65 20 74 68 65 20 6c 61 73 74 20 72 6f 6f 74 20  e the last root 
eea20 70 61 67 65 20 69 6e 20 74 68 65 20 66 69 6c 65  page in the file
eea30 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65 20 6d   before.** the m
eea40 6f 76 65 2e 20 20 49 66 20 6e 6f 20 70 61 67 65  ove.  If no page
eea50 20 67 65 74 73 20 6d 6f 76 65 64 2c 20 2a 70 69   gets moved, *pi
eea60 4d 6f 76 65 64 20 69 73 20 73 65 74 20 74 6f 20  Moved is set to 
eea70 30 2e 0a 2a 2a 20 54 68 65 20 6c 61 73 74 20 72  0..** The last r
eea80 6f 6f 74 20 70 61 67 65 20 69 73 20 72 65 63 6f  oot page is reco
eea90 72 64 65 64 20 69 6e 20 6d 65 74 61 5b 33 5d 20  rded in meta[3] 
eeaa0 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66  and the value of
eeab0 0a 2a 2a 20 6d 65 74 61 5b 33 5d 20 69 73 20 75  .** meta[3] is u
eeac0 70 64 61 74 65 64 20 62 79 20 74 68 69 73 20 70  pdated by this p
eead0 72 6f 63 65 64 75 72 65 2e 0a 2a 2f 0a 53 51 4c  rocedure..*/.SQL
eeae0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
eeaf0 73 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70  sqlite3BtreeDrop
eeb00 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20  Table(Btree *p, 
eeb10 69 6e 74 20 69 54 61 62 6c 65 2c 20 69 6e 74 20  int iTable, int 
eeb20 2a 70 69 4d 6f 76 65 64 29 7b 0a 20 20 69 6e 74  *piMoved){.  int
eeb30 20 72 63 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a   rc;.  MemPage *
eeb40 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 42 74 53  pPage = 0;.  BtS
eeb50 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
eeb60 70 42 74 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 69  pBt;..  if( p->i
eeb70 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 57 52  nTrans!=TRANS_WR
eeb80 49 54 45 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ITE ){.    retur
eeb90 6e 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20  n pBt->readOnly 
eeba0 3f 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c  ? SQLITE_READONL
eebb0 59 20 3a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  Y : SQLITE_ERROR
eebc0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 74 20 69  ;.  }..  /* It i
eebd0 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 64 72 6f  s illegal to dro
eebe0 70 20 61 20 74 61 62 6c 65 20 69 66 20 61 6e 79  p a table if any
eebf0 20 63 75 72 73 6f 72 73 20 61 72 65 20 6f 70 65   cursors are ope
eec00 6e 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61  n on the.  ** da
eec10 74 61 62 61 73 65 2e 20 54 68 69 73 20 69 73 20  tabase. This is 
eec20 62 65 63 61 75 73 65 20 69 6e 20 61 75 74 6f 2d  because in auto-
eec30 76 61 63 75 75 6d 20 6d 6f 64 65 20 74 68 65 20  vacuum mode the 
eec40 62 61 63 6b 65 6e 64 20 6d 61 79 0a 20 20 2a 2a  backend may.  **
eec50 20 6e 65 65 64 20 74 6f 20 6d 6f 76 65 20 61 6e   need to move an
eec60 6f 74 68 65 72 20 72 6f 6f 74 2d 70 61 67 65 20  other root-page 
eec70 74 6f 20 66 69 6c 6c 20 61 20 67 61 70 20 6c 65  to fill a gap le
eec80 66 74 20 62 79 20 74 68 65 20 64 65 6c 65 74 65  ft by the delete
eec90 64 0a 20 20 2a 2a 20 72 6f 6f 74 20 70 61 67 65  d.  ** root page
eeca0 2e 20 49 66 20 61 6e 20 6f 70 65 6e 20 63 75 72  . If an open cur
eecb0 73 6f 72 20 77 61 73 20 75 73 69 6e 67 20 74 68  sor was using th
eecc0 69 73 20 70 61 67 65 20 61 20 70 72 6f 62 6c 65  is page a proble
eecd0 6d 20 77 6f 75 6c 64 20 0a 20 20 2a 2a 20 6f 63  m would .  ** oc
eece0 63 75 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  cur..  */.  if( 
eecf0 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0a  pBt->pCursor ){.
eed00 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
eed10 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 7d 0a 0a 20  E_LOCKED;.  }.. 
eed20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
eed30 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 28  eeGetPage(pBt, (
eed40 50 67 6e 6f 29 69 54 61 62 6c 65 2c 20 26 70 50  Pgno)iTable, &pP
eed50 61 67 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 72  age, 0);.  if( r
eed60 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  c ) return rc;. 
eed70 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
eed80 65 65 43 6c 65 61 72 54 61 62 6c 65 28 70 2c 20  eeClearTable(p, 
eed90 69 54 61 62 6c 65 29 3b 0a 20 20 69 66 28 20 72  iTable);.  if( r
eeda0 63 20 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65  c ){.    release
eedb0 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20  Page(pPage);.   
eedc0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
eedd0 0a 20 20 2a 70 69 4d 6f 76 65 64 20 3d 20 30 3b  .  *piMoved = 0;
eede0 0a 0a 20 20 69 66 28 20 69 54 61 62 6c 65 3e 31  ..  if( iTable>1
eedf0 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54   ){.#ifdef SQLIT
eee00 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
eee10 4d 0a 20 20 20 20 72 63 20 3d 20 66 72 65 65 50  M.    rc = freeP
eee20 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20  age(pPage);.    
eee30 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67  releasePage(pPag
eee40 65 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 69 66  e);.#else.    if
eee50 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  ( pBt->autoVacuu
eee60 6d 20 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20  m ){.      Pgno 
eee70 6d 61 78 52 6f 6f 74 50 67 6e 6f 3b 0a 20 20 20  maxRootPgno;.   
eee80 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
eee90 74 72 65 65 47 65 74 4d 65 74 61 28 70 2c 20 34  treeGetMeta(p, 4
eeea0 2c 20 26 6d 61 78 52 6f 6f 74 50 67 6e 6f 29 3b  , &maxRootPgno);
eeeb0 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
eeec0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
eeed0 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
eeee0 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20  pPage);.        
eeef0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
eef00 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 69 54   }..      if( iT
eef10 61 62 6c 65 3d 3d 6d 61 78 52 6f 6f 74 50 67 6e  able==maxRootPgn
eef20 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  o ){.        /* 
eef30 49 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 69  If the table bei
eef40 6e 67 20 64 72 6f 70 70 65 64 20 69 73 20 74 68  ng dropped is th
eef50 65 20 74 61 62 6c 65 20 77 69 74 68 20 74 68 65  e table with the
eef60 20 6c 61 72 67 65 73 74 20 72 6f 6f 74 2d 70 61   largest root-pa
eef70 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 75  ge.        ** nu
eef80 6d 62 65 72 20 69 6e 20 74 68 65 20 64 61 74 61  mber in the data
eef90 62 61 73 65 2c 20 70 75 74 20 74 68 65 20 72 6f  base, put the ro
eefa0 6f 74 20 70 61 67 65 20 6f 6e 20 74 68 65 20 66  ot page on the f
eefb0 72 65 65 20 6c 69 73 74 2e 20 0a 20 20 20 20 20  ree list. .     
eefc0 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63     */.        rc
eefd0 20 3d 20 66 72 65 65 50 61 67 65 28 70 50 61 67   = freePage(pPag
eefe0 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65  e);.        rele
eeff0 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a  asePage(pPage);.
ef000 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
ef010 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
ef020 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
ef030 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
ef040 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
ef050 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65   /* The table be
ef060 69 6e 67 20 64 72 6f 70 70 65 64 20 64 6f 65 73  ing dropped does
ef070 20 6e 6f 74 20 68 61 76 65 20 74 68 65 20 6c 61   not have the la
ef080 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a  rgest root-page.
ef090 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65          ** numbe
ef0a0 72 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  r in the databas
ef0b0 65 2e 20 53 6f 20 6d 6f 76 65 20 74 68 65 20 70  e. So move the p
ef0c0 61 67 65 20 74 68 61 74 20 64 6f 65 73 20 69 6e  age that does in
ef0d0 74 6f 20 74 68 65 20 0a 20 20 20 20 20 20 20 20  to the .        
ef0e0 2a 2a 20 67 61 70 20 6c 65 66 74 20 62 79 20 74  ** gap left by t
ef0f0 68 65 20 64 65 6c 65 74 65 64 20 72 6f 6f 74 2d  he deleted root-
ef100 70 61 67 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f  page..        */
ef110 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65  .        MemPage
ef120 20 2a 70 4d 6f 76 65 3b 0a 20 20 20 20 20 20 20   *pMove;.       
ef130 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61   releasePage(pPa
ef140 67 65 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  ge);.        rc 
ef150 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  = sqlite3BtreeGe
ef160 74 50 61 67 65 28 70 42 74 2c 20 6d 61 78 52 6f  tPage(pBt, maxRo
ef170 6f 74 50 67 6e 6f 2c 20 26 70 4d 6f 76 65 2c 20  otPgno, &pMove, 
ef180 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  0);.        if( 
ef190 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
ef1a0 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
ef1b0 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a  n rc;.        }.
ef1c0 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 65 6c          rc = rel
ef1d0 6f 63 61 74 65 50 61 67 65 28 70 42 74 2c 20 70  ocatePage(pBt, p
ef1e0 4d 6f 76 65 2c 20 50 54 52 4d 41 50 5f 52 4f 4f  Move, PTRMAP_ROO
ef1f0 54 50 41 47 45 2c 20 30 2c 20 69 54 61 62 6c 65  TPAGE, 0, iTable
ef200 29 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61  );.        relea
ef210 73 65 50 61 67 65 28 70 4d 6f 76 65 29 3b 0a 20  sePage(pMove);. 
ef220 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
ef230 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
ef240 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
ef250 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
ef260 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
ef270 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c  treeGetPage(pBt,
ef280 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2c 20 26 70   maxRootPgno, &p
ef290 4d 6f 76 65 2c 20 30 29 3b 0a 20 20 20 20 20 20  Move, 0);.      
ef2a0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
ef2b0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
ef2c0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
ef2d0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63      }.        rc
ef2e0 20 3d 20 66 72 65 65 50 61 67 65 28 70 4d 6f 76   = freePage(pMov
ef2f0 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65  e);.        rele
ef300 61 73 65 50 61 67 65 28 70 4d 6f 76 65 29 3b 0a  asePage(pMove);.
ef310 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
ef320 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
ef330 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
ef340 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
ef350 20 20 20 20 2a 70 69 4d 6f 76 65 64 20 3d 20 6d      *piMoved = m
ef360 61 78 52 6f 6f 74 50 67 6e 6f 3b 0a 20 20 20 20  axRootPgno;.    
ef370 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 53 65    }..      /* Se
ef380 74 20 74 68 65 20 6e 65 77 20 27 6d 61 78 2d 72  t the new 'max-r
ef390 6f 6f 74 2d 70 61 67 65 27 20 76 61 6c 75 65 20  oot-page' value 
ef3a0 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
ef3b0 68 65 61 64 65 72 2e 20 54 68 69 73 0a 20 20 20  header. This.   
ef3c0 20 20 20 2a 2a 20 69 73 20 74 68 65 20 6f 6c 64     ** is the old
ef3d0 20 76 61 6c 75 65 20 6c 65 73 73 20 6f 6e 65 2c   value less one,
ef3e0 20 6c 65 73 73 20 6f 6e 65 20 6d 6f 72 65 20 69   less one more i
ef3f0 66 20 74 68 61 74 20 68 61 70 70 65 6e 73 20 74  f that happens t
ef400 6f 0a 20 20 20 20 20 20 2a 2a 20 62 65 20 61 20  o.      ** be a 
ef410 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72  root-page number
ef420 2c 20 6c 65 73 73 20 6f 6e 65 20 61 67 61 69 6e  , less one again
ef430 20 69 66 20 74 68 61 74 20 69 73 20 74 68 65 0a   if that is the.
ef440 20 20 20 20 20 20 2a 2a 20 50 45 4e 44 49 4e 47        ** PENDING
ef450 5f 42 59 54 45 5f 50 41 47 45 2e 0a 20 20 20 20  _BYTE_PAGE..    
ef460 20 20 2a 2f 0a 20 20 20 20 20 20 6d 61 78 52 6f    */.      maxRo
ef470 6f 74 50 67 6e 6f 2d 2d 3b 0a 20 20 20 20 20 20  otPgno--;.      
ef480 69 66 28 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3d  if( maxRootPgno=
ef490 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41  =PENDING_BYTE_PA
ef4a0 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20  GE(pBt) ){.     
ef4b0 20 20 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d 2d     maxRootPgno--
ef4c0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
ef4d0 69 66 28 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3d  if( maxRootPgno=
ef4e0 3d 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70  =PTRMAP_PAGENO(p
ef4f0 42 74 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 29  Bt, maxRootPgno)
ef500 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 61 78 52   ){.        maxR
ef510 6f 6f 74 50 67 6e 6f 2d 2d 3b 0a 20 20 20 20 20  ootPgno--;.     
ef520 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   }.      assert(
ef530 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 21 3d 50 45   maxRootPgno!=PE
ef540 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
ef550 70 42 74 29 20 29 3b 0a 0a 20 20 20 20 20 20 72  pBt) );..      r
ef560 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
ef570 55 70 64 61 74 65 4d 65 74 61 28 70 2c 20 34 2c  UpdateMeta(p, 4,
ef580 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 29 3b 0a 20   maxRootPgno);. 
ef590 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
ef5a0 72 63 20 3d 20 66 72 65 65 50 61 67 65 28 70 50  rc = freePage(pP
ef5b0 61 67 65 29 3b 0a 20 20 20 20 20 20 72 65 6c 65  age);.      rele
ef5c0 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a  asePage(pPage);.
ef5d0 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d      }.#endif.  }
ef5e0 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20  else{.    /* If 
ef5f0 73 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70  sqlite3BtreeDrop
ef600 54 61 62 6c 65 20 77 61 73 20 63 61 6c 6c 65 64  Table was called
ef610 20 6f 6e 20 70 61 67 65 20 31 2e 20 2a 2f 0a 20   on page 1. */. 
ef620 20 20 20 7a 65 72 6f 50 61 67 65 28 70 50 61 67     zeroPage(pPag
ef630 65 2c 20 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54  e, PTF_INTKEY|PT
ef640 46 5f 4c 45 41 46 20 29 3b 0a 20 20 20 20 72 65  F_LEAF );.    re
ef650 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29  leasePage(pPage)
ef660 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
ef670 63 3b 20 20 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52  c;  .}.../*.** R
ef680 65 61 64 20 74 68 65 20 6d 65 74 61 2d 69 6e 66  ead the meta-inf
ef690 6f 72 6d 61 74 69 6f 6e 20 6f 75 74 20 6f 66 20  ormation out of 
ef6a0 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  a database file.
ef6b0 20 20 4d 65 74 61 5b 30 5d 0a 2a 2a 20 69 73 20    Meta[0].** is 
ef6c0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 72  the number of fr
ef6d0 65 65 20 70 61 67 65 73 20 63 75 72 72 65 6e 74  ee pages current
ef6e0 6c 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  ly in the databa
ef6f0 73 65 2e 20 20 4d 65 74 61 5b 31 5d 0a 2a 2a 20  se.  Meta[1].** 
ef700 74 68 72 6f 75 67 68 20 6d 65 74 61 5b 31 35 5d  through meta[15]
ef710 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 20 66   are available f
ef720 6f 72 20 75 73 65 20 62 79 20 68 69 67 68 65 72  or use by higher
ef730 20 6c 61 79 65 72 73 2e 20 20 4d 65 74 61 5b 30   layers.  Meta[0
ef740 5d 0a 2a 2a 20 69 73 20 72 65 61 64 2d 6f 6e 6c  ].** is read-onl
ef750 79 2c 20 74 68 65 20 6f 74 68 65 72 73 20 61 72  y, the others ar
ef760 65 20 72 65 61 64 2f 77 72 69 74 65 2e 0a 2a 2a  e read/write..**
ef770 20 0a 2a 2a 20 54 68 65 20 73 63 68 65 6d 61 20   .** The schema 
ef780 6c 61 79 65 72 20 6e 75 6d 62 65 72 73 20 6d 65  layer numbers me
ef790 74 61 20 76 61 6c 75 65 73 20 64 69 66 66 65 72  ta values differ
ef7a0 65 6e 74 6c 79 2e 20 20 41 74 20 74 68 65 20 73  ently.  At the s
ef7b0 63 68 65 6d 61 0a 2a 2a 20 6c 61 79 65 72 20 28  chema.** layer (
ef7c0 61 6e 64 20 74 68 65 20 53 65 74 43 6f 6f 6b 69  and the SetCooki
ef7d0 65 20 61 6e 64 20 52 65 61 64 43 6f 6f 6b 69 65  e and ReadCookie
ef7e0 20 6f 70 63 6f 64 65 73 29 20 74 68 65 20 6e 75   opcodes) the nu
ef7f0 6d 62 65 72 20 6f 66 0a 2a 2a 20 66 72 65 65 20  mber of.** free 
ef800 70 61 67 65 73 20 69 73 20 6e 6f 74 20 76 69 73  pages is not vis
ef810 69 62 6c 65 2e 20 20 53 6f 20 43 6f 6f 6b 69 65  ible.  So Cookie
ef820 5b 30 5d 20 69 73 20 74 68 65 20 73 61 6d 65 20  [0] is the same 
ef830 61 73 20 4d 65 74 61 5b 31 5d 2e 0a 2a 2f 0a 53  as Meta[1]..*/.S
ef840 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
ef850 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  t sqlite3BtreeGe
ef860 74 4d 65 74 61 28 42 74 72 65 65 20 2a 70 2c 20  tMeta(Btree *p, 
ef870 69 6e 74 20 69 64 78 2c 20 75 33 32 20 2a 70 4d  int idx, u32 *pM
ef880 65 74 61 29 7b 0a 20 20 44 62 50 61 67 65 20 2a  eta){.  DbPage *
ef890 70 44 62 50 61 67 65 3b 0a 20 20 69 6e 74 20 72  pDbPage;.  int r
ef8a0 63 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  c;.  unsigned ch
ef8b0 61 72 20 2a 70 50 31 3b 0a 20 20 42 74 53 68 61  ar *pP1;.  BtSha
ef8c0 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
ef8d0 74 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 69 6e 67  t;..  /* Reading
ef8e0 20 61 20 6d 65 74 61 2d 64 61 74 61 20 76 61 6c   a meta-data val
ef8f0 75 65 20 72 65 71 75 69 72 65 73 20 61 20 72 65  ue requires a re
ef900 61 64 2d 6c 6f 63 6b 20 6f 6e 20 70 61 67 65 20  ad-lock on page 
ef910 31 20 28 61 6e 64 20 68 65 6e 63 65 0a 20 20 2a  1 (and hence.  *
ef920 2a 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73  * the sqlite_mas
ef930 74 65 72 20 74 61 62 6c 65 2e 20 57 65 20 67 72  ter table. We gr
ef940 61 62 20 74 68 69 73 20 6c 6f 63 6b 20 72 65 67  ab this lock reg
ef950 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68  ardless of wheth
ef960 65 72 20 6f 72 0a 20 20 2a 2a 20 6e 6f 74 20 74  er or.  ** not t
ef970 68 65 20 53 51 4c 49 54 45 5f 52 65 61 64 55 6e  he SQLITE_ReadUn
ef980 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 20 69  committed flag i
ef990 73 20 73 65 74 20 28 74 68 65 20 74 61 62 6c 65  s set (the table
ef9a0 20 72 6f 6f 74 65 64 20 61 74 20 70 61 67 65 0a   rooted at page.
ef9b0 20 20 2a 2a 20 31 20 69 73 20 74 72 65 61 74 65    ** 1 is treate
ef9c0 64 20 61 73 20 61 20 73 70 65 63 69 61 6c 20 63  d as a special c
ef9d0 61 73 65 20 62 79 20 71 75 65 72 79 54 61 62 6c  ase by queryTabl
ef9e0 65 4c 6f 63 6b 28 29 20 61 6e 64 20 6c 6f 63 6b  eLock() and lock
ef9f0 54 61 62 6c 65 28 29 29 2e 0a 20 20 2a 2f 0a 20  Table())..  */. 
efa00 20 72 63 20 3d 20 71 75 65 72 79 54 61 62 6c 65   rc = queryTable
efa10 4c 6f 63 6b 28 70 2c 20 31 2c 20 52 45 41 44 5f  Lock(p, 1, READ_
efa20 4c 4f 43 4b 29 3b 0a 20 20 69 66 28 20 72 63 21  LOCK);.  if( rc!
efa30 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
efa40 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
efa50 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 78 3e  ..  assert( idx>
efa60 3d 30 20 26 26 20 69 64 78 3c 3d 31 35 20 29 3b  =0 && idx<=15 );
efa70 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50  .  rc = sqlite3P
efa80 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70 50 61  agerGet(pBt->pPa
efa90 67 65 72 2c 20 31 2c 20 26 70 44 62 50 61 67 65  ger, 1, &pDbPage
efaa0 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65  );.  if( rc ) re
efab0 74 75 72 6e 20 72 63 3b 0a 20 20 70 50 31 20 3d  turn rc;.  pP1 =
efac0 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
efad0 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  *)sqlite3PagerGe
efae0 74 44 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a  tData(pDbPage);.
efaf0 20 20 2a 70 4d 65 74 61 20 3d 20 67 65 74 34 62    *pMeta = get4b
efb00 79 74 65 28 26 70 50 31 5b 33 36 20 2b 20 69 64  yte(&pP1[36 + id
efb10 78 2a 34 5d 29 3b 0a 20 20 73 71 6c 69 74 65 33  x*4]);.  sqlite3
efb20 50 61 67 65 72 55 6e 72 65 66 28 70 44 62 50 61  PagerUnref(pDbPa
efb30 67 65 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 75  ge);..  /* If au
efb40 74 6f 76 61 63 75 75 6d 65 64 20 69 73 20 64 69  tovacuumed is di
efb50 73 61 62 6c 65 64 20 69 6e 20 74 68 69 73 20 62  sabled in this b
efb60 75 69 6c 64 20 62 75 74 20 77 65 20 61 72 65 20  uild but we are 
efb70 74 72 79 69 6e 67 20 74 6f 20 0a 20 20 2a 2a 20  trying to .  ** 
efb80 61 63 63 65 73 73 20 61 6e 20 61 75 74 6f 76 61  access an autova
efb90 63 75 75 6d 65 64 20 64 61 74 61 62 61 73 65 2c  cuumed database,
efba0 20 74 68 65 6e 20 6d 61 6b 65 20 74 68 65 20 64   then make the d
efbb0 61 74 61 62 61 73 65 20 72 65 61 64 6f 6e 6c 79  atabase readonly
efbc0 2e 20 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53  . .  */.#ifdef S
efbd0 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
efbe0 41 43 55 55 4d 0a 20 20 69 66 28 20 69 64 78 3d  ACUUM.  if( idx=
efbf0 3d 34 20 26 26 20 2a 70 4d 65 74 61 3e 30 20 29  =4 && *pMeta>0 )
efc00 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d   pBt->readOnly =
efc10 20 31 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a   1;.#endif..  /*
efc20 20 47 72 61 62 20 74 68 65 20 72 65 61 64 2d 6c   Grab the read-l
efc30 6f 63 6b 20 6f 6e 20 70 61 67 65 20 31 2e 20 2a  ock on page 1. *
efc40 2f 0a 20 20 72 63 20 3d 20 6c 6f 63 6b 54 61 62  /.  rc = lockTab
efc50 6c 65 28 70 2c 20 31 2c 20 52 45 41 44 5f 4c 4f  le(p, 1, READ_LO
efc60 43 4b 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  CK);.  return rc
efc70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  ;.}../*.** Write
efc80 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f   meta-informatio
efc90 6e 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20  n back into the 
efca0 64 61 74 61 62 61 73 65 2e 20 20 4d 65 74 61 5b  database.  Meta[
efcb0 30 5d 20 69 73 0a 2a 2a 20 72 65 61 64 2d 6f 6e  0] is.** read-on
efcc0 6c 79 20 61 6e 64 20 6d 61 79 20 6e 6f 74 20 62  ly and may not b
efcd0 65 20 77 72 69 74 74 65 6e 2e 0a 2a 2f 0a 53 51  e written..*/.SQ
efce0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
efcf0 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64   sqlite3BtreeUpd
efd00 61 74 65 4d 65 74 61 28 42 74 72 65 65 20 2a 70  ateMeta(Btree *p
efd10 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32 20 69  , int idx, u32 i
efd20 4d 65 74 61 29 7b 0a 20 20 42 74 53 68 61 72 65  Meta){.  BtShare
efd30 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
efd40 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
efd50 20 2a 70 50 31 3b 0a 20 20 69 6e 74 20 72 63 3b   *pP1;.  int rc;
efd60 0a 20 20 61 73 73 65 72 74 28 20 69 64 78 3e 3d  .  assert( idx>=
efd70 31 20 26 26 20 69 64 78 3c 3d 31 35 20 29 3b 0a  1 && idx<=15 );.
efd80 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73    if( p->inTrans
efd90 21 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b  !=TRANS_WRITE ){
efda0 0a 20 20 20 20 72 65 74 75 72 6e 20 70 42 74 2d  .    return pBt-
efdb0 3e 72 65 61 64 4f 6e 6c 79 20 3f 20 53 51 4c 49  >readOnly ? SQLI
efdc0 54 45 5f 52 45 41 44 4f 4e 4c 59 20 3a 20 53 51  TE_READONLY : SQ
efdd0 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
efde0 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70    assert( pBt->p
efdf0 50 61 67 65 31 21 3d 30 20 29 3b 0a 20 20 70 50  Page1!=0 );.  pP
efe00 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 2d  1 = pBt->pPage1-
efe10 3e 61 44 61 74 61 3b 0a 20 20 72 63 20 3d 20 73  >aData;.  rc = s
efe20 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
efe30 28 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 70 44  (pBt->pPage1->pD
efe40 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63  bPage);.  if( rc
efe50 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
efe60 70 75 74 34 62 79 74 65 28 26 70 50 31 5b 33 36  put4byte(&pP1[36
efe70 20 2b 20 69 64 78 2a 34 5d 2c 20 69 4d 65 74 61   + idx*4], iMeta
efe80 29 3b 0a 20 20 69 66 28 20 69 64 78 3d 3d 37 20  );.  if( idx==7 
efe90 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
efea0 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 7c  Bt->autoVacuum |
efeb0 7c 20 69 4d 65 74 61 3d 3d 30 20 29 3b 0a 20 20  | iMeta==0 );.  
efec0 20 20 61 73 73 65 72 74 28 20 69 4d 65 74 61 3d    assert( iMeta=
efed0 3d 30 20 7c 7c 20 69 4d 65 74 61 3d 3d 31 20 29  =0 || iMeta==1 )
efee0 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 56  ;.    pBt->incrV
efef0 61 63 75 75 6d 20 3d 20 69 4d 65 74 61 3b 0a 20  acuum = iMeta;. 
eff00 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
eff10 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
eff20 52 65 74 75 72 6e 20 74 68 65 20 66 6c 61 67 20  Return the flag 
eff30 62 79 74 65 20 61 74 20 74 68 65 20 62 65 67 69  byte at the begi
eff40 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 70 61 67  nning of the pag
eff50 65 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f  e that the curso
eff60 72 0a 2a 2a 20 69 73 20 63 75 72 72 65 6e 74 6c  r.** is currentl
eff70 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 0a 2a  y pointing to..*
eff80 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
eff90 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
effa0 65 46 6c 61 67 73 28 42 74 43 75 72 73 6f 72 20  eFlags(BtCursor 
effb0 2a 70 43 75 72 29 7b 0a 20 20 2f 2a 20 54 4f 44  *pCur){.  /* TOD
effc0 4f 3a 20 57 68 61 74 20 61 62 6f 75 74 20 43 55  O: What about CU
effd0 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b  RSOR_REQUIRESEEK
effe0 20 73 74 61 74 65 3f 20 50 72 6f 62 61 62 6c 79   state? Probably
efff0 20 6e 65 65 64 20 74 6f 20 63 61 6c 6c 0a 20 20   need to call.  
f0000 2a 2a 20 72 65 73 74 6f 72 65 4f 72 43 6c 65 61  ** restoreOrClea
f0010 72 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28  rCursorPosition(
f0020 29 20 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 4d  ) here..  */.  M
f0030 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20  emPage *pPage = 
f0040 70 43 75 72 2d 3e 70 50 61 67 65 3b 0a 20 20 72  pCur->pPage;.  r
f0050 65 74 75 72 6e 20 70 50 61 67 65 20 3f 20 70 50  eturn pPage ? pP
f0060 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65  age->aData[pPage
f0070 2d 3e 68 64 72 4f 66 66 73 65 74 5d 20 3a 20 30  ->hdrOffset] : 0
f0080 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ;.}.../*.** Retu
f0090 72 6e 20 74 68 65 20 70 61 67 65 72 20 61 73 73  rn the pager ass
f00a0 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 42  ociated with a B
f00b0 54 72 65 65 2e 20 20 54 68 69 73 20 72 6f 75 74  Tree.  This rout
f00c0 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f 72 0a  ine is used for.
f00d0 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64  ** testing and d
f00e0 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a  ebugging only..*
f00f0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
f0100 20 50 61 67 65 72 20 2a 73 71 6c 69 74 65 33 42   Pager *sqlite3B
f0110 74 72 65 65 50 61 67 65 72 28 42 74 72 65 65 20  treePager(Btree 
f0120 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d  *p){.  return p-
f0130 3e 70 42 74 2d 3e 70 50 61 67 65 72 3b 0a 7d 0a  >pBt->pPager;.}.
f0140 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
f0150 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43  OMIT_INTEGRITY_C
f0160 48 45 43 4b 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e  HECK./*.** Appen
f0170 64 20 61 20 6d 65 73 73 61 67 65 20 74 6f 20 74  d a message to t
f0180 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  he error message
f0190 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74   string..*/.stat
f01a0 69 63 20 76 6f 69 64 20 63 68 65 63 6b 41 70 70  ic void checkApp
f01b0 65 6e 64 4d 73 67 28 0a 20 20 49 6e 74 65 67 72  endMsg(.  Integr
f01c0 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 0a 20  ityCk *pCheck,. 
f01d0 20 63 68 61 72 20 2a 7a 4d 73 67 31 2c 0a 20 20   char *zMsg1,.  
f01e0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72  const char *zFor
f01f0 6d 61 74 2c 0a 20 20 2e 2e 2e 0a 29 7b 0a 20 20  mat,.  ....){.  
f0200 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68  va_list ap;.  ch
f0210 61 72 20 2a 7a 4d 73 67 32 3b 0a 20 20 69 66 28  ar *zMsg2;.  if(
f0220 20 21 70 43 68 65 63 6b 2d 3e 6d 78 45 72 72 20   !pCheck->mxErr 
f0230 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 43 68 65  ) return;.  pChe
f0240 63 6b 2d 3e 6d 78 45 72 72 2d 2d 3b 0a 20 20 70  ck->mxErr--;.  p
f0250 43 68 65 63 6b 2d 3e 6e 45 72 72 2b 2b 3b 0a 20  Check->nErr++;. 
f0260 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46   va_start(ap, zF
f0270 6f 72 6d 61 74 29 3b 0a 20 20 7a 4d 73 67 32 20  ormat);.  zMsg2 
f0280 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74  = sqlite3VMPrint
f0290 66 28 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a  f(zFormat, ap);.
f02a0 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20    va_end(ap);.  
f02b0 69 66 28 20 7a 4d 73 67 31 3d 3d 30 20 29 20 7a  if( zMsg1==0 ) z
f02c0 4d 73 67 31 20 3d 20 22 22 3b 0a 20 20 69 66 28  Msg1 = "";.  if(
f02d0 20 70 43 68 65 63 6b 2d 3e 7a 45 72 72 4d 73 67   pCheck->zErrMsg
f02e0 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4f   ){.    char *zO
f02f0 6c 64 20 3d 20 70 43 68 65 63 6b 2d 3e 7a 45 72  ld = pCheck->zEr
f0300 72 4d 73 67 3b 0a 20 20 20 20 70 43 68 65 63 6b  rMsg;.    pCheck
f0310 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20  ->zErrMsg = 0;. 
f0320 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72     sqlite3SetStr
f0330 69 6e 67 28 26 70 43 68 65 63 6b 2d 3e 7a 45 72  ing(&pCheck->zEr
f0340 72 4d 73 67 2c 20 7a 4f 6c 64 2c 20 22 5c 6e 22  rMsg, zOld, "\n"
f0350 2c 20 7a 4d 73 67 31 2c 20 7a 4d 73 67 32 2c 20  , zMsg1, zMsg2, 
f0360 28 63 68 61 72 2a 29 30 29 3b 0a 20 20 20 20 73  (char*)0);.    s
f0370 71 6c 69 74 65 46 72 65 65 28 7a 4f 6c 64 29 3b  qliteFree(zOld);
f0380 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
f0390 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26  lite3SetString(&
f03a0 70 43 68 65 63 6b 2d 3e 7a 45 72 72 4d 73 67 2c  pCheck->zErrMsg,
f03b0 20 7a 4d 73 67 31 2c 20 7a 4d 73 67 32 2c 20 28   zMsg1, zMsg2, (
f03c0 63 68 61 72 2a 29 30 29 3b 0a 20 20 7d 0a 20 20  char*)0);.  }.  
f03d0 73 71 6c 69 74 65 46 72 65 65 28 7a 4d 73 67 32  sqliteFree(zMsg2
f03e0 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  );.}.#endif /* S
f03f0 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47  QLITE_OMIT_INTEG
f0400 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23  RITY_CHECK */..#
f0410 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
f0420 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45  IT_INTEGRITY_CHE
f0430 43 4b 0a 2f 2a 0a 2a 2a 20 41 64 64 20 31 20 74  CK./*.** Add 1 t
f0440 6f 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20  o the reference 
f0450 63 6f 75 6e 74 20 66 6f 72 20 70 61 67 65 20 69  count for page i
f0460 50 61 67 65 2e 20 20 49 66 20 74 68 69 73 20 69  Page.  If this i
f0470 73 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20  s the second.** 
f0480 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65  reference to the
f0490 20 70 61 67 65 2c 20 61 64 64 20 61 6e 20 65 72   page, add an er
f04a0 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20 70  ror message to p
f04b0 43 68 65 63 6b 2d 3e 7a 45 72 72 4d 73 67 2e 0a  Check->zErrMsg..
f04c0 2a 2a 20 52 65 74 75 72 6e 20 31 20 69 66 20 74  ** Return 1 if t
f04d0 68 65 72 65 20 61 72 65 20 32 20 6f 72 65 20 6d  here are 2 ore m
f04e0 6f 72 65 20 72 65 66 65 72 65 6e 63 65 73 20 74  ore references t
f04f0 6f 20 74 68 65 20 70 61 67 65 20 61 6e 64 20 30  o the page and 0
f0500 20 69 66 0a 2a 2a 20 69 66 20 74 68 69 73 20 69   if.** if this i
f0510 73 20 74 68 65 20 66 69 72 73 74 20 72 65 66 65  s the first refe
f0520 72 65 6e 63 65 20 74 6f 20 74 68 65 20 70 61 67  rence to the pag
f0530 65 2e 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 20 63 68  e..**.** Also ch
f0540 65 63 6b 20 74 68 61 74 20 74 68 65 20 70 61 67  eck that the pag
f0550 65 20 6e 75 6d 62 65 72 20 69 73 20 69 6e 20 62  e number is in b
f0560 6f 75 6e 64 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  ounds..*/.static
f0570 20 69 6e 74 20 63 68 65 63 6b 52 65 66 28 49 6e   int checkRef(In
f0580 74 65 67 72 69 74 79 43 6b 20 2a 70 43 68 65 63  tegrityCk *pChec
f0590 6b 2c 20 69 6e 74 20 69 50 61 67 65 2c 20 63 68  k, int iPage, ch
f05a0 61 72 20 2a 7a 43 6f 6e 74 65 78 74 29 7b 0a 20  ar *zContext){. 
f05b0 20 69 66 28 20 69 50 61 67 65 3d 3d 30 20 29 20   if( iPage==0 ) 
f05c0 72 65 74 75 72 6e 20 31 3b 0a 20 20 69 66 28 20  return 1;.  if( 
f05d0 69 50 61 67 65 3e 70 43 68 65 63 6b 2d 3e 6e 50  iPage>pCheck->nP
f05e0 61 67 65 20 7c 7c 20 69 50 61 67 65 3c 30 20 29  age || iPage<0 )
f05f0 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e  {.    checkAppen
f0600 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f  dMsg(pCheck, zCo
f0610 6e 74 65 78 74 2c 20 22 69 6e 76 61 6c 69 64 20  ntext, "invalid 
f0620 70 61 67 65 20 6e 75 6d 62 65 72 20 25 64 22 2c  page number %d",
f0630 20 69 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74   iPage);.    ret
f0640 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28  urn 1;.  }.  if(
f0650 20 70 43 68 65 63 6b 2d 3e 61 6e 52 65 66 5b 69   pCheck->anRef[i
f0660 50 61 67 65 5d 3d 3d 31 20 29 7b 0a 20 20 20 20  Page]==1 ){.    
f0670 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70  checkAppendMsg(p
f0680 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c  Check, zContext,
f0690 20 22 32 6e 64 20 72 65 66 65 72 65 6e 63 65 20   "2nd reference 
f06a0 74 6f 20 70 61 67 65 20 25 64 22 2c 20 69 50 61  to page %d", iPa
f06b0 67 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ge);.    return 
f06c0 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  1;.  }.  return 
f06d0 20 28 70 43 68 65 63 6b 2d 3e 61 6e 52 65 66 5b   (pCheck->anRef[
f06e0 69 50 61 67 65 5d 2b 2b 29 3e 31 3b 0a 7d 0a 0a  iPage]++)>1;.}..
f06f0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
f0700 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f  MIT_AUTOVACUUM./
f0710 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 20  *.** Check that 
f0720 74 68 65 20 65 6e 74 72 79 20 69 6e 20 74 68 65  the entry in the
f0730 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 66 6f 72   pointer-map for
f0740 20 70 61 67 65 20 69 43 68 69 6c 64 20 6d 61 70   page iChild map
f0750 73 20 74 6f 20 0a 2a 2a 20 70 61 67 65 20 69 50  s to .** page iP
f0760 61 72 65 6e 74 2c 20 70 6f 69 6e 74 65 72 20 74  arent, pointer t
f0770 79 70 65 20 70 74 72 54 79 70 65 2e 20 49 66 20  ype ptrType. If 
f0780 6e 6f 74 2c 20 61 70 70 65 6e 64 20 61 6e 20 65  not, append an e
f0790 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20  rror message.** 
f07a0 74 6f 20 70 43 68 65 63 6b 2e 0a 2a 2f 0a 73 74  to pCheck..*/.st
f07b0 61 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 50  atic void checkP
f07c0 74 72 6d 61 70 28 0a 20 20 49 6e 74 65 67 72 69  trmap(.  Integri
f07d0 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20 20  tyCk *pCheck,   
f07e0 2f 2a 20 49 6e 74 65 67 72 69 74 79 20 63 68 65  /* Integrity che
f07f0 63 6b 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ck context */.  
f0800 50 67 6e 6f 20 69 43 68 69 6c 64 2c 20 20 20 20  Pgno iChild,    
f0810 20 20 20 20 20 20 20 2f 2a 20 43 68 69 6c 64 20         /* Child 
f0820 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20  page number */. 
f0830 20 75 38 20 65 54 79 70 65 2c 20 20 20 20 20 20   u8 eType,      
f0840 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63          /* Expec
f0850 74 65 64 20 70 6f 69 6e 74 65 72 20 6d 61 70 20  ted pointer map 
f0860 74 79 70 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69  type */.  Pgno i
f0870 50 61 72 65 6e 74 2c 20 20 20 20 20 20 20 20 20  Parent,         
f0880 20 2f 2a 20 45 78 70 65 63 74 65 64 20 70 6f 69   /* Expected poi
f0890 6e 74 65 72 20 6d 61 70 20 70 61 72 65 6e 74 20  nter map parent 
f08a0 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20  page number */. 
f08b0 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74 20   char *zContext 
f08c0 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65          /* Conte
f08d0 78 74 20 64 65 73 63 72 69 70 74 69 6f 6e 20 28  xt description (
f08e0 75 73 65 64 20 66 6f 72 20 65 72 72 6f 72 20 6d  used for error m
f08f0 73 67 29 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  sg) */.){.  int 
f0900 72 63 3b 0a 20 20 75 38 20 65 50 74 72 6d 61 70  rc;.  u8 ePtrmap
f0910 54 79 70 65 3b 0a 20 20 50 67 6e 6f 20 69 50 74  Type;.  Pgno iPt
f0920 72 6d 61 70 50 61 72 65 6e 74 3b 0a 0a 20 20 72  rmapParent;..  r
f0930 63 20 3d 20 70 74 72 6d 61 70 47 65 74 28 70 43  c = ptrmapGet(pC
f0940 68 65 63 6b 2d 3e 70 42 74 2c 20 69 43 68 69 6c  heck->pBt, iChil
f0950 64 2c 20 26 65 50 74 72 6d 61 70 54 79 70 65 2c  d, &ePtrmapType,
f0960 20 26 69 50 74 72 6d 61 70 50 61 72 65 6e 74 29   &iPtrmapParent)
f0970 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
f0980 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 63 68 65  TE_OK ){.    che
f0990 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65  ckAppendMsg(pChe
f09a0 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 46  ck, zContext, "F
f09b0 61 69 6c 65 64 20 74 6f 20 72 65 61 64 20 70 74  ailed to read pt
f09c0 72 6d 61 70 20 6b 65 79 3d 25 64 22 2c 20 69 43  rmap key=%d", iC
f09d0 68 69 6c 64 29 3b 0a 20 20 20 20 72 65 74 75 72  hild);.    retur
f09e0 6e 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 65 50  n;.  }..  if( eP
f09f0 74 72 6d 61 70 54 79 70 65 21 3d 65 54 79 70 65  trmapType!=eType
f0a00 20 7c 7c 20 69 50 74 72 6d 61 70 50 61 72 65 6e   || iPtrmapParen
f0a10 74 21 3d 69 50 61 72 65 6e 74 20 29 7b 0a 20 20  t!=iParent ){.  
f0a20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67    checkAppendMsg
f0a30 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78  (pCheck, zContex
f0a40 74 2c 20 0a 20 20 20 20 20 20 22 42 61 64 20 70  t, .      "Bad p
f0a50 74 72 20 6d 61 70 20 65 6e 74 72 79 20 6b 65 79  tr map entry key
f0a60 3d 25 64 20 65 78 70 65 63 74 65 64 3d 28 25 64  =%d expected=(%d
f0a70 2c 25 64 29 20 67 6f 74 3d 28 25 64 2c 25 64 29  ,%d) got=(%d,%d)
f0a80 22 2c 20 0a 20 20 20 20 20 20 69 43 68 69 6c 64  ", .      iChild
f0a90 2c 20 65 54 79 70 65 2c 20 69 50 61 72 65 6e 74  , eType, iParent
f0aa0 2c 20 65 50 74 72 6d 61 70 54 79 70 65 2c 20 69  , ePtrmapType, i
f0ab0 50 74 72 6d 61 70 50 61 72 65 6e 74 29 3b 0a 20  PtrmapParent);. 
f0ac0 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a   }.}.#endif../*.
f0ad0 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 69 6e 74  ** Check the int
f0ae0 65 67 72 69 74 79 20 6f 66 20 74 68 65 20 66 72  egrity of the fr
f0af0 65 65 6c 69 73 74 20 6f 72 20 6f 66 20 61 6e 20  eelist or of an 
f0b00 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6c 69  overflow page li
f0b10 73 74 2e 0a 2a 2a 20 56 65 72 69 66 79 20 74 68  st..** Verify th
f0b20 61 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  at the number of
f0b30 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 6c 69   pages on the li
f0b40 73 74 20 69 73 20 4e 2e 0a 2a 2f 0a 73 74 61 74  st is N..*/.stat
f0b50 69 63 20 76 6f 69 64 20 63 68 65 63 6b 4c 69 73  ic void checkLis
f0b60 74 28 0a 20 20 49 6e 74 65 67 72 69 74 79 43 6b  t(.  IntegrityCk
f0b70 20 2a 70 43 68 65 63 6b 2c 20 20 2f 2a 20 49 6e   *pCheck,  /* In
f0b80 74 65 67 72 69 74 79 20 63 68 65 63 6b 69 6e 67  tegrity checking
f0b90 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e   context */.  in
f0ba0 74 20 69 73 46 72 65 65 4c 69 73 74 2c 20 20 20  t isFreeList,   
f0bb0 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20      /* True for 
f0bc0 61 20 66 72 65 65 6c 69 73 74 2e 20 20 46 61 6c  a freelist.  Fal
f0bd0 73 65 20 66 6f 72 20 6f 76 65 72 66 6c 6f 77 20  se for overflow 
f0be0 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69  page list */.  i
f0bf0 6e 74 20 69 50 61 67 65 2c 20 20 20 20 20 20 20  nt iPage,       
f0c00 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d       /* Page num
f0c10 62 65 72 20 66 6f 72 20 66 69 72 73 74 20 70 61  ber for first pa
f0c20 67 65 20 69 6e 20 74 68 65 20 6c 69 73 74 20 2a  ge in the list *
f0c30 2f 0a 20 20 69 6e 74 20 4e 2c 20 20 20 20 20 20  /.  int N,      
f0c40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70            /* Exp
f0c50 65 63 74 65 64 20 6e 75 6d 62 65 72 20 6f 66 20  ected number of 
f0c60 70 61 67 65 73 20 69 6e 20 74 68 65 20 6c 69 73  pages in the lis
f0c70 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f  t */.  char *zCo
f0c80 6e 74 65 78 74 20 20 20 20 20 20 20 20 2f 2a 20  ntext        /* 
f0c90 43 6f 6e 74 65 78 74 20 66 6f 72 20 65 72 72 6f  Context for erro
f0ca0 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a 29 7b  r messages */.){
f0cb0 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
f0cc0 65 78 70 65 63 74 65 64 20 3d 20 4e 3b 0a 20 20  expected = N;.  
f0cd0 69 6e 74 20 69 46 69 72 73 74 20 3d 20 69 50 61  int iFirst = iPa
f0ce0 67 65 3b 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d  ge;.  while( N--
f0cf0 20 3e 20 30 20 26 26 20 70 43 68 65 63 6b 2d 3e   > 0 && pCheck->
f0d00 6d 78 45 72 72 20 29 7b 0a 20 20 20 20 44 62 50  mxErr ){.    DbP
f0d10 61 67 65 20 2a 70 4f 76 66 6c 50 61 67 65 3b 0a  age *pOvflPage;.
f0d20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61      unsigned cha
f0d30 72 20 2a 70 4f 76 66 6c 44 61 74 61 3b 0a 20 20  r *pOvflData;.  
f0d40 20 20 69 66 28 20 69 50 61 67 65 3c 31 20 29 7b    if( iPage<1 ){
f0d50 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65  .      checkAppe
f0d60 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43  ndMsg(pCheck, zC
f0d70 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20  ontext,.        
f0d80 20 22 25 64 20 6f 66 20 25 64 20 70 61 67 65 73   "%d of %d pages
f0d90 20 6d 69 73 73 69 6e 67 20 66 72 6f 6d 20 6f 76   missing from ov
f0da0 65 72 66 6c 6f 77 20 6c 69 73 74 20 73 74 61 72  erflow list star
f0db0 74 69 6e 67 20 61 74 20 25 64 22 2c 0a 20 20 20  ting at %d",.   
f0dc0 20 20 20 20 20 20 20 4e 2b 31 2c 20 65 78 70 65         N+1, expe
f0dd0 63 74 65 64 2c 20 69 46 69 72 73 74 29 3b 0a 20  cted, iFirst);. 
f0de0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
f0df0 7d 0a 20 20 20 20 69 66 28 20 63 68 65 63 6b 52  }.    if( checkR
f0e00 65 66 28 70 43 68 65 63 6b 2c 20 69 50 61 67 65  ef(pCheck, iPage
f0e10 2c 20 7a 43 6f 6e 74 65 78 74 29 20 29 20 62 72  , zContext) ) br
f0e20 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  eak;.    if( sql
f0e30 69 74 65 33 50 61 67 65 72 47 65 74 28 70 43 68  ite3PagerGet(pCh
f0e40 65 63 6b 2d 3e 70 50 61 67 65 72 2c 20 28 50 67  eck->pPager, (Pg
f0e50 6e 6f 29 69 50 61 67 65 2c 20 26 70 4f 76 66 6c  no)iPage, &pOvfl
f0e60 50 61 67 65 29 20 29 7b 0a 20 20 20 20 20 20 63  Page) ){.      c
f0e70 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43  heckAppendMsg(pC
f0e80 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20  heck, zContext, 
f0e90 22 66 61 69 6c 65 64 20 74 6f 20 67 65 74 20 70  "failed to get p
f0ea0 61 67 65 20 25 64 22 2c 20 69 50 61 67 65 29 3b  age %d", iPage);
f0eb0 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
f0ec0 20 20 7d 0a 20 20 20 20 70 4f 76 66 6c 44 61 74    }.    pOvflDat
f0ed0 61 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68  a = (unsigned ch
f0ee0 61 72 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65  ar *)sqlite3Page
f0ef0 72 47 65 74 44 61 74 61 28 70 4f 76 66 6c 50 61  rGetData(pOvflPa
f0f00 67 65 29 3b 0a 20 20 20 20 69 66 28 20 69 73 46  ge);.    if( isF
f0f10 72 65 65 4c 69 73 74 20 29 7b 0a 20 20 20 20 20  reeList ){.     
f0f20 20 69 6e 74 20 6e 20 3d 20 67 65 74 34 62 79 74   int n = get4byt
f0f30 65 28 26 70 4f 76 66 6c 44 61 74 61 5b 34 5d 29  e(&pOvflData[4])
f0f40 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
f0f50 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
f0f60 0a 20 20 20 20 20 20 69 66 28 20 70 43 68 65 63  .      if( pChec
f0f70 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  k->pBt->autoVacu
f0f80 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68  um ){.        ch
f0f90 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b  eckPtrmap(pCheck
f0fa0 2c 20 69 50 61 67 65 2c 20 50 54 52 4d 41 50 5f  , iPage, PTRMAP_
f0fb0 46 52 45 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f  FREEPAGE, 0, zCo
f0fc0 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a  ntext);.      }.
f0fd0 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69 66 28  #endif.      if(
f0fe0 20 6e 3e 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e   n>pCheck->pBt->
f0ff0 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d 38 20 29  usableSize/4-8 )
f1000 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 41  {.        checkA
f1010 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c  ppendMsg(pCheck,
f1020 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20   zContext,.     
f1030 20 20 20 20 20 20 22 66 72 65 65 6c 69 73 74 20        "freelist 
f1040 6c 65 61 66 20 63 6f 75 6e 74 20 74 6f 6f 20 62  leaf count too b
f1050 69 67 20 6f 6e 20 70 61 67 65 20 25 64 22 2c 20  ig on page %d", 
f1060 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20  iPage);.        
f1070 4e 2d 2d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  N--;.      }else
f1080 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d  {.        for(i=
f1090 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20  0; i<n; i++){.  
f10a0 20 20 20 20 20 20 20 20 50 67 6e 6f 20 69 46 72          Pgno iFr
f10b0 65 65 50 61 67 65 20 3d 20 67 65 74 34 62 79 74  eePage = get4byt
f10c0 65 28 26 70 4f 76 66 6c 44 61 74 61 5b 38 2b 69  e(&pOvflData[8+i
f10d0 2a 34 5d 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  *4]);.#ifndef SQ
f10e0 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
f10f0 43 55 55 4d 0a 20 20 20 20 20 20 20 20 20 20 69  CUUM.          i
f1100 66 28 20 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e  f( pCheck->pBt->
f1110 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20  autoVacuum ){.  
f1120 20 20 20 20 20 20 20 20 20 20 63 68 65 63 6b 50            checkP
f1130 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 69 46  trmap(pCheck, iF
f1140 72 65 65 50 61 67 65 2c 20 50 54 52 4d 41 50 5f  reePage, PTRMAP_
f1150 46 52 45 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f  FREEPAGE, 0, zCo
f1160 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20  ntext);.        
f1170 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20    }.#endif.     
f1180 20 20 20 20 20 63 68 65 63 6b 52 65 66 28 70 43       checkRef(pC
f1190 68 65 63 6b 2c 20 69 46 72 65 65 50 61 67 65 2c  heck, iFreePage,
f11a0 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20   zContext);.    
f11b0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 4e 20      }.        N 
f11c0 2d 3d 20 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  -= n;.      }.  
f11d0 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
f11e0 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
f11f0 55 4d 0a 20 20 20 20 65 6c 73 65 7b 0a 20 20 20  UM.    else{.   
f1200 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 64 61     /* If this da
f1210 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 73 20  tabase supports 
f1220 61 75 74 6f 2d 76 61 63 75 75 6d 20 61 6e 64 20  auto-vacuum and 
f1230 69 50 61 67 65 20 69 73 20 6e 6f 74 20 74 68 65  iPage is not the
f1240 20 6c 61 73 74 0a 20 20 20 20 20 20 2a 2a 20 70   last.      ** p
f1250 61 67 65 20 69 6e 20 74 68 69 73 20 6f 76 65 72  age in this over
f1260 66 6c 6f 77 20 6c 69 73 74 2c 20 63 68 65 63 6b  flow list, check
f1270 20 74 68 61 74 20 74 68 65 20 70 6f 69 6e 74 65   that the pointe
f1280 72 2d 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 0a  r-map entry for.
f1290 20 20 20 20 20 20 2a 2a 20 74 68 65 20 66 6f 6c        ** the fol
f12a0 6c 6f 77 69 6e 67 20 70 61 67 65 20 6d 61 74 63  lowing page matc
f12b0 68 65 73 20 69 50 61 67 65 2e 0a 20 20 20 20 20  hes iPage..     
f12c0 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 43   */.      if( pC
f12d0 68 65 63 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56  heck->pBt->autoV
f12e0 61 63 75 75 6d 20 26 26 20 4e 3e 30 20 29 7b 0a  acuum && N>0 ){.
f12f0 20 20 20 20 20 20 20 20 69 20 3d 20 67 65 74 34          i = get4
f1300 62 79 74 65 28 70 4f 76 66 6c 44 61 74 61 29 3b  byte(pOvflData);
f1310 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 50 74  .        checkPt
f1320 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 69 2c 20  rmap(pCheck, i, 
f1330 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32  PTRMAP_OVERFLOW2
f1340 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78  , iPage, zContex
f1350 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  t);.      }.    
f1360 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 50 61  }.#endif.    iPa
f1370 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 70 4f  ge = get4byte(pO
f1380 76 66 6c 44 61 74 61 29 3b 0a 20 20 20 20 73 71  vflData);.    sq
f1390 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28  lite3PagerUnref(
f13a0 70 4f 76 66 6c 50 61 67 65 29 3b 0a 20 20 7d 0a  pOvflPage);.  }.
f13b0 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
f13c0 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54  TE_OMIT_INTEGRIT
f13d0 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e  Y_CHECK */..#ifn
f13e0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
f13f0 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a  INTEGRITY_CHECK.
f1400 2f 2a 0a 2a 2a 20 44 6f 20 76 61 72 69 6f 75 73  /*.** Do various
f1410 20 73 61 6e 69 74 79 20 63 68 65 63 6b 73 20 6f   sanity checks o
f1420 6e 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20  n a single page 
f1430 6f 66 20 61 20 74 72 65 65 2e 20 20 52 65 74 75  of a tree.  Retu
f1440 72 6e 0a 2a 2a 20 74 68 65 20 74 72 65 65 20 64  rn.** the tree d
f1450 65 70 74 68 2e 20 20 52 6f 6f 74 20 70 61 67 65  epth.  Root page
f1460 73 20 72 65 74 75 72 6e 20 30 2e 20 20 50 61 72  s return 0.  Par
f1470 65 6e 74 73 20 6f 66 20 72 6f 6f 74 20 70 61 67  ents of root pag
f1480 65 73 0a 2a 2a 20 72 65 74 75 72 6e 20 31 2c 20  es.** return 1, 
f1490 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 0a 2a 2a  and so forth..**
f14a0 20 0a 2a 2a 20 54 68 65 73 65 20 63 68 65 63 6b   .** These check
f14b0 73 20 61 72 65 20 64 6f 6e 65 3a 0a 2a 2a 0a 2a  s are done:.**.*
f14c0 2a 20 20 20 20 20 20 31 2e 20 20 4d 61 6b 65 20  *      1.  Make 
f14d0 73 75 72 65 20 74 68 61 74 20 63 65 6c 6c 73 20  sure that cells 
f14e0 61 6e 64 20 66 72 65 65 62 6c 6f 63 6b 73 20 64  and freeblocks d
f14f0 6f 20 6e 6f 74 20 6f 76 65 72 6c 61 70 0a 2a 2a  o not overlap.**
f1500 20 20 20 20 20 20 20 20 20 20 62 75 74 20 63 6f            but co
f1510 6d 62 69 6e 65 20 74 6f 20 63 6f 6d 70 6c 65 74  mbine to complet
f1520 65 6c 79 20 63 6f 76 65 72 20 74 68 65 20 70 61  ely cover the pa
f1530 67 65 2e 0a 2a 2a 20 20 4e 4f 20 20 32 2e 20 20  ge..**  NO  2.  
f1540 4d 61 6b 65 20 73 75 72 65 20 63 65 6c 6c 20 6b  Make sure cell k
f1550 65 79 73 20 61 72 65 20 69 6e 20 6f 72 64 65 72  eys are in order
f1560 2e 0a 2a 2a 20 20 4e 4f 20 20 33 2e 20 20 4d 61  ..**  NO  3.  Ma
f1570 6b 65 20 73 75 72 65 20 6e 6f 20 6b 65 79 20 69  ke sure no key i
f1580 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65  s less than or e
f1590 71 75 61 6c 20 74 6f 20 7a 4c 6f 77 65 72 42 6f  qual to zLowerBo
f15a0 75 6e 64 2e 0a 2a 2a 20 20 4e 4f 20 20 34 2e 20  und..**  NO  4. 
f15b0 20 4d 61 6b 65 20 73 75 72 65 20 6e 6f 20 6b 65   Make sure no ke
f15c0 79 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  y is greater tha
f15d0 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 7a 55  n or equal to zU
f15e0 70 70 65 72 42 6f 75 6e 64 2e 0a 2a 2a 20 20 20  pperBound..**   
f15f0 20 20 20 35 2e 20 20 43 68 65 63 6b 20 74 68 65     5.  Check the
f1600 20 69 6e 74 65 67 72 69 74 79 20 6f 66 20 6f 76   integrity of ov
f1610 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 0a 2a 2a  erflow pages..**
f1620 20 20 20 20 20 20 36 2e 20 20 52 65 63 75 72 73        6.  Recurs
f1630 69 76 65 6c 79 20 63 61 6c 6c 20 63 68 65 63 6b  ively call check
f1640 54 72 65 65 50 61 67 65 20 6f 6e 20 61 6c 6c 20  TreePage on all 
f1650 63 68 69 6c 64 72 65 6e 2e 0a 2a 2a 20 20 20 20  children..**    
f1660 20 20 37 2e 20 20 56 65 72 69 66 79 20 74 68 61    7.  Verify tha
f1670 74 20 74 68 65 20 64 65 70 74 68 20 6f 66 20 61  t the depth of a
f1680 6c 6c 20 63 68 69 6c 64 72 65 6e 20 69 73 20 74  ll children is t
f1690 68 65 20 73 61 6d 65 2e 0a 2a 2a 20 20 20 20 20  he same..**     
f16a0 20 38 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74   8.  Make sure t
f16b0 68 69 73 20 70 61 67 65 20 69 73 20 61 74 20 6c  his page is at l
f16c0 65 61 73 74 20 33 33 25 20 66 75 6c 6c 20 6f 72  east 33% full or
f16d0 20 65 6c 73 65 20 69 74 20 69 73 0a 2a 2a 20 20   else it is.**  
f16e0 20 20 20 20 20 20 20 20 74 68 65 20 72 6f 6f 74          the root
f16f0 20 6f 66 20 74 68 65 20 74 72 65 65 2e 0a 2a 2f   of the tree..*/
f1700 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 65 63  .static int chec
f1710 6b 54 72 65 65 50 61 67 65 28 0a 20 20 49 6e 74  kTreePage(.  Int
f1720 65 67 72 69 74 79 43 6b 20 2a 70 43 68 65 63 6b  egrityCk *pCheck
f1730 2c 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 66 6f  ,  /* Context fo
f1740 72 20 74 68 65 20 73 61 6e 69 74 79 20 63 68 65  r the sanity che
f1750 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 67  ck */.  int iPag
f1760 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e,            /*
f1770 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20   Page number of 
f1780 74 68 65 20 70 61 67 65 20 74 6f 20 63 68 65 63  the page to chec
f1790 6b 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a  k */.  MemPage *
f17a0 70 50 61 72 65 6e 74 2c 20 20 20 20 20 2f 2a 20  pParent,     /* 
f17b0 50 61 72 65 6e 74 20 70 61 67 65 20 2a 2f 0a 20  Parent page */. 
f17c0 20 63 68 61 72 20 2a 7a 50 61 72 65 6e 74 43 6f   char *zParentCo
f17d0 6e 74 65 78 74 20 20 2f 2a 20 50 61 72 65 6e 74  ntext  /* Parent
f17e0 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 29 7b 0a 20   context */.){. 
f17f0 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b   MemPage *pPage;
f1800 0a 20 20 69 6e 74 20 69 2c 20 72 63 2c 20 64 65  .  int i, rc, de
f1810 70 74 68 2c 20 64 32 2c 20 70 67 6e 6f 2c 20 63  pth, d2, pgno, c
f1820 6e 74 3b 0a 20 20 69 6e 74 20 68 64 72 2c 20 63  nt;.  int hdr, c
f1830 65 6c 6c 53 74 61 72 74 3b 0a 20 20 69 6e 74 20  ellStart;.  int 
f1840 6e 43 65 6c 6c 3b 0a 20 20 75 38 20 2a 64 61 74  nCell;.  u8 *dat
f1850 61 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  a;.  BtShared *p
f1860 42 74 3b 0a 20 20 69 6e 74 20 75 73 61 62 6c 65  Bt;.  int usable
f1870 53 69 7a 65 3b 0a 20 20 63 68 61 72 20 7a 43 6f  Size;.  char zCo
f1880 6e 74 65 78 74 5b 31 30 30 5d 3b 0a 20 20 63 68  ntext[100];.  ch
f1890 61 72 20 2a 68 69 74 3b 0a 0a 20 20 73 71 6c 69  ar *hit;..  sqli
f18a0 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
f18b0 65 6f 66 28 7a 43 6f 6e 74 65 78 74 29 2c 20 7a  eof(zContext), z
f18c0 43 6f 6e 74 65 78 74 2c 20 22 50 61 67 65 20 25  Context, "Page %
f18d0 64 3a 20 22 2c 20 69 50 61 67 65 29 3b 0a 0a 20  d: ", iPage);.. 
f18e0 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74   /* Check that t
f18f0 68 65 20 70 61 67 65 20 65 78 69 73 74 73 0a 20  he page exists. 
f1900 20 2a 2f 0a 20 20 70 42 74 20 3d 20 70 43 68 65   */.  pBt = pChe
f1910 63 6b 2d 3e 70 42 74 3b 0a 20 20 75 73 61 62 6c  ck->pBt;.  usabl
f1920 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61  eSize = pBt->usa
f1930 62 6c 65 53 69 7a 65 3b 0a 20 20 69 66 28 20 69  bleSize;.  if( i
f1940 50 61 67 65 3d 3d 30 20 29 20 72 65 74 75 72 6e  Page==0 ) return
f1950 20 30 3b 0a 20 20 69 66 28 20 63 68 65 63 6b 52   0;.  if( checkR
f1960 65 66 28 70 43 68 65 63 6b 2c 20 69 50 61 67 65  ef(pCheck, iPage
f1970 2c 20 7a 50 61 72 65 6e 74 43 6f 6e 74 65 78 74  , zParentContext
f1980 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  ) ) return 0;.  
f1990 69 66 28 20 28 72 63 20 3d 20 73 71 6c 69 74 65  if( (rc = sqlite
f19a0 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42  3BtreeGetPage(pB
f19b0 74 2c 20 28 50 67 6e 6f 29 69 50 61 67 65 2c 20  t, (Pgno)iPage, 
f19c0 26 70 50 61 67 65 2c 20 30 29 29 21 3d 30 20 29  &pPage, 0))!=0 )
f19d0 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e  {.    checkAppen
f19e0 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f  dMsg(pCheck, zCo
f19f0 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 22 75  ntext,.       "u
f1a00 6e 61 62 6c 65 20 74 6f 20 67 65 74 20 74 68 65  nable to get the
f1a10 20 70 61 67 65 2e 20 65 72 72 6f 72 20 63 6f 64   page. error cod
f1a20 65 3d 25 64 22 2c 20 72 63 29 3b 0a 20 20 20 20  e=%d", rc);.    
f1a30 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
f1a40 69 66 28 20 28 72 63 20 3d 20 73 71 6c 69 74 65  if( (rc = sqlite
f1a50 33 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 70  3BtreeInitPage(p
f1a60 50 61 67 65 2c 20 70 50 61 72 65 6e 74 29 29 21  Page, pParent))!
f1a70 3d 30 20 29 7b 0a 20 20 20 20 63 68 65 63 6b 41  =0 ){.    checkA
f1a80 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c  ppendMsg(pCheck,
f1a90 20 7a 43 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20   zContext, .    
f1aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
f1ab0 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74  sqlite3BtreeInit
f1ac0 50 61 67 65 28 29 20 72 65 74 75 72 6e 73 20 65  Page() returns e
f1ad0 72 72 6f 72 20 63 6f 64 65 20 25 64 22 2c 20 72  rror code %d", r
f1ae0 63 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50  c);.    releaseP
f1af0 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20  age(pPage);.    
f1b00 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20  return 0;.  }.. 
f1b10 20 2f 2a 20 43 68 65 63 6b 20 6f 75 74 20 61 6c   /* Check out al
f1b20 6c 20 74 68 65 20 63 65 6c 6c 73 2e 0a 20 20 2a  l the cells..  *
f1b30 2f 0a 20 20 64 65 70 74 68 20 3d 20 30 3b 0a 20  /.  depth = 0;. 
f1b40 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 67   for(i=0; i<pPag
f1b50 65 2d 3e 6e 43 65 6c 6c 20 26 26 20 70 43 68 65  e->nCell && pChe
f1b60 63 6b 2d 3e 6d 78 45 72 72 3b 20 69 2b 2b 29 7b  ck->mxErr; i++){
f1b70 0a 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b 0a  .    u8 *pCell;.
f1b80 20 20 20 20 69 6e 74 20 73 7a 3b 0a 20 20 20 20      int sz;.    
f1b90 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a  CellInfo info;..
f1ba0 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 70 61 79      /* Check pay
f1bb0 6c 6f 61 64 20 6f 76 65 72 66 6c 6f 77 20 70 61  load overflow pa
f1bc0 67 65 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73  ges.    */.    s
f1bd0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
f1be0 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 78 74 29  sizeof(zContext)
f1bf0 2c 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20  , zContext,.    
f1c00 20 20 20 20 20 20 20 20 20 22 4f 6e 20 74 72 65           "On tre
f1c10 65 20 70 61 67 65 20 25 64 20 63 65 6c 6c 20 25  e page %d cell %
f1c20 64 3a 20 22 2c 20 69 50 61 67 65 2c 20 69 29 3b  d: ", iPage, i);
f1c30 0a 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e  .    pCell = fin
f1c40 64 43 65 6c 6c 28 70 50 61 67 65 2c 69 29 3b 0a  dCell(pPage,i);.
f1c50 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
f1c60 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61  ParseCellPtr(pPa
f1c70 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f  ge, pCell, &info
f1c80 29 3b 0a 20 20 20 20 73 7a 20 3d 20 69 6e 66 6f  );.    sz = info
f1c90 2e 6e 44 61 74 61 3b 0a 20 20 20 20 69 66 28 20  .nData;.    if( 
f1ca0 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29  !pPage->intKey )
f1cb0 20 73 7a 20 2b 3d 20 69 6e 66 6f 2e 6e 4b 65 79   sz += info.nKey
f1cc0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 7a  ;.    assert( sz
f1cd0 3d 3d 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20  ==info.nPayload 
f1ce0 29 3b 0a 20 20 20 20 69 66 28 20 73 7a 3e 69 6e  );.    if( sz>in
f1cf0 66 6f 2e 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20  fo.nLocal ){.   
f1d00 20 20 20 69 6e 74 20 6e 50 61 67 65 20 3d 20 28     int nPage = (
f1d10 73 7a 20 2d 20 69 6e 66 6f 2e 6e 4c 6f 63 61 6c  sz - info.nLocal
f1d20 20 2b 20 75 73 61 62 6c 65 53 69 7a 65 20 2d 20   + usableSize - 
f1d30 35 29 2f 28 75 73 61 62 6c 65 53 69 7a 65 20 2d  5)/(usableSize -
f1d40 20 34 29 3b 0a 20 20 20 20 20 20 50 67 6e 6f 20   4);.      Pgno 
f1d50 70 67 6e 6f 4f 76 66 6c 20 3d 20 67 65 74 34 62  pgnoOvfl = get4b
f1d60 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e  yte(&pCell[info.
f1d70 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 23 69 66  iOverflow]);.#if
f1d80 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
f1d90 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20  _AUTOVACUUM.    
f1da0 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56    if( pBt->autoV
f1db0 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20  acuum ){.       
f1dc0 20 63 68 65 63 6b 50 74 72 6d 61 70 28 70 43 68   checkPtrmap(pCh
f1dd0 65 63 6b 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 50  eck, pgnoOvfl, P
f1de0 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 2c  TRMAP_OVERFLOW1,
f1df0 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74   iPage, zContext
f1e00 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  );.      }.#endi
f1e10 66 0a 20 20 20 20 20 20 63 68 65 63 6b 4c 69 73  f.      checkLis
f1e20 74 28 70 43 68 65 63 6b 2c 20 30 2c 20 70 67 6e  t(pCheck, 0, pgn
f1e30 6f 4f 76 66 6c 2c 20 6e 50 61 67 65 2c 20 7a 43  oOvfl, nPage, zC
f1e40 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 7d 0a 0a  ontext);.    }..
f1e50 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 73 61 6e      /* Check san
f1e60 69 74 79 20 6f 66 20 6c 65 66 74 20 63 68 69 6c  ity of left chil
f1e70 64 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20  d page..    */. 
f1e80 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c     if( !pPage->l
f1e90 65 61 66 20 29 7b 0a 20 20 20 20 20 20 70 67 6e  eaf ){.      pgn
f1ea0 6f 20 3d 20 67 65 74 34 62 79 74 65 28 70 43 65  o = get4byte(pCe
f1eb0 6c 6c 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  ll);.#ifndef SQL
f1ec0 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
f1ed0 55 55 4d 0a 20 20 20 20 20 20 69 66 28 20 70 42  UUM.      if( pB
f1ee0 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b  t->autoVacuum ){
f1ef0 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 50 74  .        checkPt
f1f00 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 70 67 6e  rmap(pCheck, pgn
f1f10 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c  o, PTRMAP_BTREE,
f1f20 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74   iPage, zContext
f1f30 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  );.      }.#endi
f1f40 66 0a 20 20 20 20 20 20 64 32 20 3d 20 63 68 65  f.      d2 = che
f1f50 63 6b 54 72 65 65 50 61 67 65 28 70 43 68 65 63  ckTreePage(pChec
f1f60 6b 2c 70 67 6e 6f 2c 70 50 61 67 65 2c 7a 43 6f  k,pgno,pPage,zCo
f1f70 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 69 66  ntext);.      if
f1f80 28 20 69 3e 30 20 26 26 20 64 32 21 3d 64 65 70  ( i>0 && d2!=dep
f1f90 74 68 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68  th ){.        ch
f1fa0 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68  eckAppendMsg(pCh
f1fb0 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22  eck, zContext, "
f1fc0 43 68 69 6c 64 20 70 61 67 65 20 64 65 70 74 68  Child page depth
f1fd0 20 64 69 66 66 65 72 73 22 29 3b 0a 20 20 20 20   differs");.    
f1fe0 20 20 7d 0a 20 20 20 20 20 20 64 65 70 74 68 20    }.      depth 
f1ff0 3d 20 64 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  = d2;.    }.  }.
f2000 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65    if( !pPage->le
f2010 61 66 20 29 7b 0a 20 20 20 20 70 67 6e 6f 20 3d  af ){.    pgno =
f2020 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65   get4byte(&pPage
f2030 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68  ->aData[pPage->h
f2040 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20  drOffset+8]);.  
f2050 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
f2060 74 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65  tf(sizeof(zConte
f2070 78 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a  xt), zContext, .
f2080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f2090 20 20 20 20 20 22 4f 6e 20 70 61 67 65 20 25 64       "On page %d
f20a0 20 61 74 20 72 69 67 68 74 20 63 68 69 6c 64 3a   at right child:
f20b0 20 22 2c 20 69 50 61 67 65 29 3b 0a 23 69 66 6e   ", iPage);.#ifn
f20c0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
f20d0 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69  AUTOVACUUM.    i
f20e0 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  f( pBt->autoVacu
f20f0 75 6d 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63  um ){.      chec
f2100 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20  kPtrmap(pCheck, 
f2110 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52  pgno, PTRMAP_BTR
f2120 45 45 2c 20 69 50 61 67 65 2c 20 30 29 3b 0a 20  EE, iPage, 0);. 
f2130 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
f2140 63 68 65 63 6b 54 72 65 65 50 61 67 65 28 70 43  checkTreePage(pC
f2150 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 70 50 61 67  heck, pgno, pPag
f2160 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20  e, zContext);.  
f2170 7d 0a 20 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66  }. .  /* Check f
f2180 6f 72 20 63 6f 6d 70 6c 65 74 65 20 63 6f 76 65  or complete cove
f2190 72 61 67 65 20 6f 66 20 74 68 65 20 70 61 67 65  rage of the page
f21a0 0a 20 20 2a 2f 0a 20 20 64 61 74 61 20 3d 20 70  .  */.  data = p
f21b0 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 68  Page->aData;.  h
f21c0 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f  dr = pPage->hdrO
f21d0 66 66 73 65 74 3b 0a 20 20 68 69 74 20 3d 20 73  ffset;.  hit = s
f21e0 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 20 75 73 61  qliteMalloc( usa
f21f0 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 69 66 28  bleSize );.  if(
f2200 20 68 69 74 20 29 7b 0a 20 20 20 20 6d 65 6d 73   hit ){.    mems
f2210 65 74 28 68 69 74 2c 20 31 2c 20 67 65 74 32 62  et(hit, 1, get2b
f2220 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d  yte(&data[hdr+5]
f2230 29 29 3b 0a 20 20 20 20 6e 43 65 6c 6c 20 3d 20  ));.    nCell = 
f2240 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  get2byte(&data[h
f2250 64 72 2b 33 5d 29 3b 0a 20 20 20 20 63 65 6c 6c  dr+3]);.    cell
f2260 53 74 61 72 74 20 3d 20 68 64 72 20 2b 20 31 32  Start = hdr + 12
f2270 20 2d 20 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66   - 4*pPage->leaf
f2280 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
f2290 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20  <nCell; i++){.  
f22a0 20 20 20 20 69 6e 74 20 70 63 20 3d 20 67 65 74      int pc = get
f22b0 32 62 79 74 65 28 26 64 61 74 61 5b 63 65 6c 6c  2byte(&data[cell
f22c0 53 74 61 72 74 2b 69 2a 32 5d 29 3b 0a 20 20 20  Start+i*2]);.   
f22d0 20 20 20 69 6e 74 20 73 69 7a 65 20 3d 20 63 65     int size = ce
f22e0 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c  llSizePtr(pPage,
f22f0 20 26 64 61 74 61 5b 70 63 5d 29 3b 0a 20 20 20   &data[pc]);.   
f2300 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20     int j;.      
f2310 69 66 28 20 28 70 63 2b 73 69 7a 65 2d 31 29 3e  if( (pc+size-1)>
f2320 3d 75 73 61 62 6c 65 53 69 7a 65 20 7c 7c 20 70  =usableSize || p
f2330 63 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63  c<0 ){.        c
f2340 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43  heckAppendMsg(pC
f2350 68 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20 20 20  heck, 0, .      
f2360 20 20 20 20 20 20 22 43 6f 72 72 75 70 74 69 6f        "Corruptio
f2370 6e 20 64 65 74 65 63 74 65 64 20 69 6e 20 63 65  n detected in ce
f2380 6c 6c 20 25 64 20 6f 6e 20 70 61 67 65 20 25 64  ll %d on page %d
f2390 22 2c 69 2c 69 50 61 67 65 2c 30 29 3b 0a 20 20  ",i,iPage,0);.  
f23a0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
f23b0 20 20 20 66 6f 72 28 6a 3d 70 63 2b 73 69 7a 65     for(j=pc+size
f23c0 2d 31 3b 20 6a 3e 3d 70 63 3b 20 6a 2d 2d 29 20  -1; j>=pc; j--) 
f23d0 68 69 74 5b 6a 5d 2b 2b 3b 0a 20 20 20 20 20 20  hit[j]++;.      
f23e0 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28  }.    }.    for(
f23f0 63 6e 74 3d 30 2c 20 69 3d 67 65 74 32 62 79 74  cnt=0, i=get2byt
f2400 65 28 26 64 61 74 61 5b 68 64 72 2b 31 5d 29 3b  e(&data[hdr+1]);
f2410 20 69 3e 30 20 26 26 20 69 3c 75 73 61 62 6c 65   i>0 && i<usable
f2420 53 69 7a 65 20 26 26 20 63 6e 74 3c 31 30 30 30  Size && cnt<1000
f2430 30 3b 20 0a 20 20 20 20 20 20 20 20 20 20 20 63  0; .           c
f2440 6e 74 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74  nt++){.      int
f2450 20 73 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65   size = get2byte
f2460 28 26 64 61 74 61 5b 69 2b 32 5d 29 3b 0a 20 20  (&data[i+2]);.  
f2470 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20      int j;.     
f2480 20 69 66 28 20 28 69 2b 73 69 7a 65 2d 31 29 3e   if( (i+size-1)>
f2490 3d 75 73 61 62 6c 65 53 69 7a 65 20 7c 7c 20 69  =usableSize || i
f24a0 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68  <0 ){.        ch
f24b0 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68  eckAppendMsg(pCh
f24c0 65 63 6b 2c 20 30 2c 20 20 0a 20 20 20 20 20 20  eck, 0,  .      
f24d0 20 20 20 20 20 20 22 43 6f 72 72 75 70 74 69 6f        "Corruptio
f24e0 6e 20 64 65 74 65 63 74 65 64 20 69 6e 20 63 65  n detected in ce
f24f0 6c 6c 20 25 64 20 6f 6e 20 70 61 67 65 20 25 64  ll %d on page %d
f2500 22 2c 69 2c 69 50 61 67 65 2c 30 29 3b 0a 20 20  ",i,iPage,0);.  
f2510 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
f2520 20 20 20 66 6f 72 28 6a 3d 69 2b 73 69 7a 65 2d     for(j=i+size-
f2530 31 3b 20 6a 3e 3d 69 3b 20 6a 2d 2d 29 20 68 69  1; j>=i; j--) hi
f2540 74 5b 6a 5d 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a  t[j]++;.      }.
f2550 20 20 20 20 20 20 69 20 3d 20 67 65 74 32 62 79        i = get2by
f2560 74 65 28 26 64 61 74 61 5b 69 5d 29 3b 0a 20 20  te(&data[i]);.  
f2570 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 63 6e    }.    for(i=cn
f2580 74 3d 30 3b 20 69 3c 75 73 61 62 6c 65 53 69 7a  t=0; i<usableSiz
f2590 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  e; i++){.      i
f25a0 66 28 20 68 69 74 5b 69 5d 3d 3d 30 20 29 7b 0a  f( hit[i]==0 ){.
f25b0 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20          cnt++;. 
f25c0 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68       }else if( h
f25d0 69 74 5b 69 5d 3e 31 20 29 7b 0a 20 20 20 20 20  it[i]>1 ){.     
f25e0 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73     checkAppendMs
f25f0 67 28 70 43 68 65 63 6b 2c 20 30 2c 0a 20 20 20  g(pCheck, 0,.   
f2600 20 20 20 20 20 20 20 22 4d 75 6c 74 69 70 6c 65         "Multiple
f2610 20 75 73 65 73 20 66 6f 72 20 62 79 74 65 20 25   uses for byte %
f2620 64 20 6f 66 20 70 61 67 65 20 25 64 22 2c 20 69  d of page %d", i
f2630 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20  , iPage);.      
f2640 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
f2650 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 63  .    }.    if( c
f2660 6e 74 21 3d 64 61 74 61 5b 68 64 72 2b 37 5d 20  nt!=data[hdr+7] 
f2670 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70  ){.      checkAp
f2680 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20  pendMsg(pCheck, 
f2690 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 22 46  0, .          "F
f26a0 72 61 67 6d 65 6e 74 65 64 20 73 70 61 63 65 20  ragmented space 
f26b0 69 73 20 25 64 20 62 79 74 65 20 72 65 70 6f 72  is %d byte repor
f26c0 74 65 64 20 61 73 20 25 64 20 6f 6e 20 70 61 67  ted as %d on pag
f26d0 65 20 25 64 22 2c 0a 20 20 20 20 20 20 20 20 20  e %d",.         
f26e0 20 63 6e 74 2c 20 64 61 74 61 5b 68 64 72 2b 37   cnt, data[hdr+7
f26f0 5d 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 7d  ], iPage);.    }
f2700 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 46 72 65  .  }.  sqliteFre
f2710 65 28 68 69 74 29 3b 0a 0a 20 20 72 65 6c 65 61  e(hit);..  relea
f2720 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20  sePage(pPage);. 
f2730 20 72 65 74 75 72 6e 20 64 65 70 74 68 2b 31 3b   return depth+1;
f2740 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
f2750 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49  ITE_OMIT_INTEGRI
f2760 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66  TY_CHECK */..#if
f2770 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
f2780 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b  _INTEGRITY_CHECK
f2790 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
f27a0 69 6e 65 20 64 6f 65 73 20 61 20 63 6f 6d 70 6c  ine does a compl
f27b0 65 74 65 20 63 68 65 63 6b 20 6f 66 20 74 68 65  ete check of the
f27c0 20 67 69 76 65 6e 20 42 54 72 65 65 20 66 69 6c   given BTree fil
f27d0 65 2e 20 20 61 52 6f 6f 74 5b 5d 20 69 73 0a 2a  e.  aRoot[] is.*
f27e0 2a 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70 61  * an array of pa
f27f0 67 65 73 20 6e 75 6d 62 65 72 73 20 77 65 72 65  ges numbers were
f2800 20 65 61 63 68 20 70 61 67 65 20 6e 75 6d 62 65   each page numbe
f2810 72 20 69 73 20 74 68 65 20 72 6f 6f 74 20 70 61  r is the root pa
f2820 67 65 20 6f 66 0a 2a 2a 20 61 20 74 61 62 6c 65  ge of.** a table
f2830 2e 20 20 6e 52 6f 6f 74 20 69 73 20 74 68 65 20  .  nRoot is the 
f2840 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  number of entrie
f2850 73 20 69 6e 20 61 52 6f 6f 74 2e 0a 2a 2a 0a 2a  s in aRoot..**.*
f2860 2a 20 49 66 20 65 76 65 72 79 74 68 69 6e 67 20  * If everything 
f2870 63 68 65 63 6b 73 20 6f 75 74 2c 20 74 68 69 73  checks out, this
f2880 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
f2890 20 4e 55 4c 4c 2e 20 20 49 66 20 73 6f 6d 65 74   NULL.  If somet
f28a0 68 69 6e 67 20 69 73 0a 2a 2a 20 61 6d 69 73 73  hing is.** amiss
f28b0 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  , an error messa
f28c0 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e  ge is written in
f28d0 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e  to memory obtain
f28e0 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29  ed from malloc()
f28f0 0a 2a 2a 20 61 6e 64 20 61 20 70 6f 69 6e 74 65  .** and a pointe
f2900 72 20 74 6f 20 74 68 61 74 20 65 72 72 6f 72 20  r to that error 
f2910 6d 65 73 73 61 67 65 20 69 73 20 72 65 74 75 72  message is retur
f2920 6e 65 64 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e  ned.  The callin
f2930 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73  g function.** is
f2940 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72   responsible for
f2950 20 66 72 65 65 69 6e 67 20 74 68 65 20 65 72 72   freeing the err
f2960 6f 72 20 6d 65 73 73 61 67 65 20 77 68 65 6e 20  or message when 
f2970 69 74 20 69 73 20 64 6f 6e 65 2e 0a 2a 2f 0a 53  it is done..*/.S
f2980 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
f2990 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65  ar *sqlite3Btree
f29a0 49 6e 74 65 67 72 69 74 79 43 68 65 63 6b 28 0a  IntegrityCheck(.
f29b0 20 20 42 74 72 65 65 20 2a 70 2c 20 20 20 20 20    Btree *p,     
f29c0 2f 2a 20 54 68 65 20 62 74 72 65 65 20 74 6f 20  /* The btree to 
f29d0 62 65 20 63 68 65 63 6b 65 64 20 2a 2f 0a 20 20  be checked */.  
f29e0 69 6e 74 20 2a 61 52 6f 6f 74 2c 20 20 20 2f 2a  int *aRoot,   /*
f29f0 20 41 6e 20 61 72 72 61 79 20 6f 66 20 72 6f 6f   An array of roo
f2a00 74 20 70 61 67 65 73 20 6e 75 6d 62 65 72 73 20  t pages numbers 
f2a10 66 6f 72 20 69 6e 64 69 76 69 64 75 61 6c 20 74  for individual t
f2a20 72 65 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 52  rees */.  int nR
f2a30 6f 6f 74 2c 20 20 20 20 2f 2a 20 4e 75 6d 62 65  oot,    /* Numbe
f2a40 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
f2a50 61 52 6f 6f 74 5b 5d 20 2a 2f 0a 20 20 69 6e 74  aRoot[] */.  int
f2a60 20 6d 78 45 72 72 2c 20 20 20 20 2f 2a 20 53 74   mxErr,    /* St
f2a70 6f 70 20 72 65 70 6f 72 74 69 6e 67 20 65 72 72  op reporting err
f2a80 6f 72 73 20 61 66 74 65 72 20 74 68 69 73 20 6d  ors after this m
f2a90 61 6e 79 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e  any */.  int *pn
f2aa0 45 72 72 20 20 20 20 2f 2a 20 57 72 69 74 65 20  Err    /* Write 
f2ab0 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73  number of errors
f2ac0 20 73 65 65 6e 20 74 6f 20 74 68 69 73 20 76 61   seen to this va
f2ad0 72 69 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69  riable */.){.  i
f2ae0 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 52 65 66  nt i;.  int nRef
f2af0 3b 0a 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20  ;.  IntegrityCk 
f2b00 73 43 68 65 63 6b 3b 0a 20 20 42 74 53 68 61 72  sCheck;.  BtShar
f2b10 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
f2b20 3b 0a 0a 20 20 6e 52 65 66 20 3d 20 73 71 6c 69  ;..  nRef = sqli
f2b30 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74  te3PagerRefcount
f2b40 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20  (pBt->pPager);. 
f2b50 20 69 66 28 20 6c 6f 63 6b 42 74 72 65 65 57 69   if( lockBtreeWi
f2b60 74 68 52 65 74 72 79 28 70 29 21 3d 53 51 4c 49  thRetry(p)!=SQLI
f2b70 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74  TE_OK ){.    ret
f2b80 75 72 6e 20 73 71 6c 69 74 65 53 74 72 44 75 70  urn sqliteStrDup
f2b90 28 22 55 6e 61 62 6c 65 20 74 6f 20 61 63 71 75  ("Unable to acqu
f2ba0 69 72 65 20 61 20 72 65 61 64 20 6c 6f 63 6b 20  ire a read lock 
f2bb0 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 22  on the database"
f2bc0 29 3b 0a 20 20 7d 0a 20 20 73 43 68 65 63 6b 2e  );.  }.  sCheck.
f2bd0 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 73 43 68  pBt = pBt;.  sCh
f2be0 65 63 6b 2e 70 50 61 67 65 72 20 3d 20 70 42 74  eck.pPager = pBt
f2bf0 2d 3e 70 50 61 67 65 72 3b 0a 20 20 73 43 68 65  ->pPager;.  sChe
f2c00 63 6b 2e 6e 50 61 67 65 20 3d 20 73 71 6c 69 74  ck.nPage = sqlit
f2c10 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74  e3PagerPagecount
f2c20 28 73 43 68 65 63 6b 2e 70 50 61 67 65 72 29 3b  (sCheck.pPager);
f2c30 0a 20 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 20  .  sCheck.mxErr 
f2c40 3d 20 6d 78 45 72 72 3b 0a 20 20 73 43 68 65 63  = mxErr;.  sChec
f2c50 6b 2e 6e 45 72 72 20 3d 20 30 3b 0a 20 20 2a 70  k.nErr = 0;.  *p
f2c60 6e 45 72 72 20 3d 20 30 3b 0a 23 69 66 6e 64 65  nErr = 0;.#ifnde
f2c70 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
f2c80 54 4f 56 41 43 55 55 4d 0a 20 20 69 66 28 20 70  TOVACUUM.  if( p
f2c90 42 74 2d 3e 6e 54 72 75 6e 63 21 3d 30 20 29 7b  Bt->nTrunc!=0 ){
f2ca0 0a 20 20 20 20 73 43 68 65 63 6b 2e 6e 50 61 67  .    sCheck.nPag
f2cb0 65 20 3d 20 70 42 74 2d 3e 6e 54 72 75 6e 63 3b  e = pBt->nTrunc;
f2cc0 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66  .  }.#endif.  if
f2cd0 28 20 73 43 68 65 63 6b 2e 6e 50 61 67 65 3d 3d  ( sCheck.nPage==
f2ce0 30 20 29 7b 0a 20 20 20 20 75 6e 6c 6f 63 6b 42  0 ){.    unlockB
f2cf0 74 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74  treeIfUnused(pBt
f2d00 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  );.    return 0;
f2d10 0a 20 20 7d 0a 20 20 73 43 68 65 63 6b 2e 61 6e  .  }.  sCheck.an
f2d20 52 65 66 20 3d 20 73 71 6c 69 74 65 4d 61 6c 6c  Ref = sqliteMall
f2d30 6f 63 52 61 77 28 20 28 73 43 68 65 63 6b 2e 6e  ocRaw( (sCheck.n
f2d40 50 61 67 65 2b 31 29 2a 73 69 7a 65 6f 66 28 73  Page+1)*sizeof(s
f2d50 43 68 65 63 6b 2e 61 6e 52 65 66 5b 30 5d 29 20  Check.anRef[0]) 
f2d60 29 3b 0a 20 20 69 66 28 20 21 73 43 68 65 63 6b  );.  if( !sCheck
f2d70 2e 61 6e 52 65 66 20 29 7b 0a 20 20 20 20 75 6e  .anRef ){.    un
f2d80 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65  lockBtreeIfUnuse
f2d90 64 28 70 42 74 29 3b 0a 20 20 20 20 2a 70 6e 45  d(pBt);.    *pnE
f2da0 72 72 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75  rr = 1;.    retu
f2db0 72 6e 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74  rn sqlite3MPrint
f2dc0 66 28 22 55 6e 61 62 6c 65 20 74 6f 20 6d 61 6c  f("Unable to mal
f2dd0 6c 6f 63 20 25 64 20 62 79 74 65 73 22 2c 20 0a  loc %d bytes", .
f2de0 20 20 20 20 20 20 20 20 28 73 43 68 65 63 6b 2e          (sCheck.
f2df0 6e 50 61 67 65 2b 31 29 2a 73 69 7a 65 6f 66 28  nPage+1)*sizeof(
f2e00 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 30 5d 29  sCheck.anRef[0])
f2e10 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30  );.  }.  for(i=0
f2e20 3b 20 69 3c 3d 73 43 68 65 63 6b 2e 6e 50 61 67  ; i<=sCheck.nPag
f2e30 65 3b 20 69 2b 2b 29 7b 20 73 43 68 65 63 6b 2e  e; i++){ sCheck.
f2e40 61 6e 52 65 66 5b 69 5d 20 3d 20 30 3b 20 7d 0a  anRef[i] = 0; }.
f2e50 20 20 69 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59    i = PENDING_BY
f2e60 54 45 5f 50 41 47 45 28 70 42 74 29 3b 0a 20 20  TE_PAGE(pBt);.  
f2e70 69 66 28 20 69 3c 3d 73 43 68 65 63 6b 2e 6e 50  if( i<=sCheck.nP
f2e80 61 67 65 20 29 7b 0a 20 20 20 20 73 43 68 65 63  age ){.    sChec
f2e90 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d 20 31 3b 0a  k.anRef[i] = 1;.
f2ea0 20 20 7d 0a 20 20 73 43 68 65 63 6b 2e 7a 45 72    }.  sCheck.zEr
f2eb0 72 4d 73 67 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20  rMsg = 0;..  /* 
f2ec0 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67 72  Check the integr
f2ed0 69 74 79 20 6f 66 20 74 68 65 20 66 72 65 65 6c  ity of the freel
f2ee0 69 73 74 0a 20 20 2a 2f 0a 20 20 63 68 65 63 6b  ist.  */.  check
f2ef0 4c 69 73 74 28 26 73 43 68 65 63 6b 2c 20 31 2c  List(&sCheck, 1,
f2f00 20 67 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e   get4byte(&pBt->
f2f10 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32  pPage1->aData[32
f2f20 5d 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ]),.            
f2f30 67 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70  get4byte(&pBt->p
f2f40 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d  Page1->aData[36]
f2f50 29 2c 20 22 4d 61 69 6e 20 66 72 65 65 6c 69 73  ), "Main freelis
f2f60 74 3a 20 22 29 3b 0a 0a 20 20 2f 2a 20 43 68 65  t: ");..  /* Che
f2f70 63 6b 20 61 6c 6c 20 74 68 65 20 74 61 62 6c 65  ck all the table
f2f80 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d  s..  */.  for(i=
f2f90 30 3b 20 69 3c 6e 52 6f 6f 74 20 26 26 20 73 43  0; i<nRoot && sC
f2fa0 68 65 63 6b 2e 6d 78 45 72 72 3b 20 69 2b 2b 29  heck.mxErr; i++)
f2fb0 7b 0a 20 20 20 20 69 66 28 20 61 52 6f 6f 74 5b  {.    if( aRoot[
f2fc0 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65  i]==0 ) continue
f2fd0 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
f2fe0 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
f2ff0 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75  .    if( pBt->au
f3000 74 6f 56 61 63 75 75 6d 20 26 26 20 61 52 6f 6f  toVacuum && aRoo
f3010 74 5b 69 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20  t[i]>1 ){.      
f3020 63 68 65 63 6b 50 74 72 6d 61 70 28 26 73 43 68  checkPtrmap(&sCh
f3030 65 63 6b 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 50  eck, aRoot[i], P
f3040 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 2c 20  TRMAP_ROOTPAGE, 
f3050 30 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 23 65 6e  0, 0);.    }.#en
f3060 64 69 66 0a 20 20 20 20 63 68 65 63 6b 54 72 65  dif.    checkTre
f3070 65 50 61 67 65 28 26 73 43 68 65 63 6b 2c 20 61  ePage(&sCheck, a
f3080 52 6f 6f 74 5b 69 5d 2c 20 30 2c 20 22 4c 69 73  Root[i], 0, "Lis
f3090 74 20 6f 66 20 74 72 65 65 20 72 6f 6f 74 73 3a  t of tree roots:
f30a0 20 22 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d   ");.  }..  /* M
f30b0 61 6b 65 20 73 75 72 65 20 65 76 65 72 79 20 70  ake sure every p
f30c0 61 67 65 20 69 6e 20 74 68 65 20 66 69 6c 65 20  age in the file 
f30d0 69 73 20 72 65 66 65 72 65 6e 63 65 64 0a 20 20  is referenced.  
f30e0 2a 2f 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c  */.  for(i=1; i<
f30f0 3d 73 43 68 65 63 6b 2e 6e 50 61 67 65 20 26 26  =sCheck.nPage &&
f3100 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 3b 20 69   sCheck.mxErr; i
f3110 2b 2b 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49  ++){.#ifdef SQLI
f3120 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
f3130 55 4d 0a 20 20 20 20 69 66 28 20 73 43 68 65 63  UM.    if( sChec
f3140 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 20 29 7b  k.anRef[i]==0 ){
f3150 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65  .      checkAppe
f3160 6e 64 4d 73 67 28 26 73 43 68 65 63 6b 2c 20 30  ndMsg(&sCheck, 0
f3170 2c 20 22 50 61 67 65 20 25 64 20 69 73 20 6e 65  , "Page %d is ne
f3180 76 65 72 20 75 73 65 64 22 2c 20 69 29 3b 0a 20  ver used", i);. 
f3190 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 2f     }.#else.    /
f31a0 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73  * If the databas
f31b0 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d  e supports auto-
f31c0 76 61 63 75 75 6d 2c 20 6d 61 6b 65 20 73 75 72  vacuum, make sur
f31d0 65 20 6e 6f 20 74 61 62 6c 65 73 20 63 6f 6e 74  e no tables cont
f31e0 61 69 6e 0a 20 20 20 20 2a 2a 20 72 65 66 65 72  ain.    ** refer
f31f0 65 6e 63 65 73 20 74 6f 20 70 6f 69 6e 74 65 72  ences to pointer
f3200 2d 6d 61 70 20 70 61 67 65 73 2e 0a 20 20 20 20  -map pages..    
f3210 2a 2f 0a 20 20 20 20 69 66 28 20 73 43 68 65 63  */.    if( sChec
f3220 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 20 26 26  k.anRef[i]==0 &&
f3230 20 0a 20 20 20 20 20 20 20 28 50 54 52 4d 41 50   .       (PTRMAP
f3240 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 69 29 21  _PAGENO(pBt, i)!
f3250 3d 69 20 7c 7c 20 21 70 42 74 2d 3e 61 75 74 6f  =i || !pBt->auto
f3260 56 61 63 75 75 6d 29 20 29 7b 0a 20 20 20 20 20  Vacuum) ){.     
f3270 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28   checkAppendMsg(
f3280 26 73 43 68 65 63 6b 2c 20 30 2c 20 22 50 61 67  &sCheck, 0, "Pag
f3290 65 20 25 64 20 69 73 20 6e 65 76 65 72 20 75 73  e %d is never us
f32a0 65 64 22 2c 20 69 29 3b 0a 20 20 20 20 7d 0a 20  ed", i);.    }. 
f32b0 20 20 20 69 66 28 20 73 43 68 65 63 6b 2e 61 6e     if( sCheck.an
f32c0 52 65 66 5b 69 5d 21 3d 30 20 26 26 20 0a 20 20  Ref[i]!=0 && .  
f32d0 20 20 20 20 20 28 50 54 52 4d 41 50 5f 50 41 47       (PTRMAP_PAG
f32e0 45 4e 4f 28 70 42 74 2c 20 69 29 3d 3d 69 20 26  ENO(pBt, i)==i &
f32f0 26 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  & pBt->autoVacuu
f3300 6d 29 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63  m) ){.      chec
f3310 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 43 68 65  kAppendMsg(&sChe
f3320 63 6b 2c 20 30 2c 20 22 50 6f 69 6e 74 65 72 20  ck, 0, "Pointer 
f3330 6d 61 70 20 70 61 67 65 20 25 64 20 69 73 20 72  map page %d is r
f3340 65 66 65 72 65 6e 63 65 64 22 2c 20 69 29 3b 0a  eferenced", i);.
f3350 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d      }.#endif.  }
f3360 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
f3370 20 74 68 69 73 20 61 6e 61 6c 79 73 69 73 20 64   this analysis d
f3380 69 64 20 6e 6f 74 20 6c 65 61 76 65 20 61 6e 79  id not leave any
f3390 20 75 6e 72 65 66 28 29 20 70 61 67 65 73 0a 20   unref() pages. 
f33a0 20 2a 2f 0a 20 20 75 6e 6c 6f 63 6b 42 74 72 65   */.  unlockBtre
f33b0 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a  eIfUnused(pBt);.
f33c0 20 20 69 66 28 20 6e 52 65 66 20 21 3d 20 73 71    if( nRef != sq
f33d0 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75  lite3PagerRefcou
f33e0 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29 20  nt(pBt->pPager) 
f33f0 29 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65  ){.    checkAppe
f3400 6e 64 4d 73 67 28 26 73 43 68 65 63 6b 2c 20 30  ndMsg(&sCheck, 0
f3410 2c 20 0a 20 20 20 20 20 20 22 4f 75 74 73 74 61  , .      "Outsta
f3420 6e 64 69 6e 67 20 70 61 67 65 20 63 6f 75 6e 74  nding page count
f3430 20 67 6f 65 73 20 66 72 6f 6d 20 25 64 20 74 6f   goes from %d to
f3440 20 25 64 20 64 75 72 69 6e 67 20 74 68 69 73 20   %d during this 
f3450 61 6e 61 6c 79 73 69 73 22 2c 0a 20 20 20 20 20  analysis",.     
f3460 20 6e 52 65 66 2c 20 73 71 6c 69 74 65 33 50 61   nRef, sqlite3Pa
f3470 67 65 72 52 65 66 63 6f 75 6e 74 28 70 42 74 2d  gerRefcount(pBt-
f3480 3e 70 50 61 67 65 72 29 0a 20 20 20 20 29 3b 0a  >pPager).    );.
f3490 20 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e 20    }..  /* Clean 
f34a0 20 75 70 20 61 6e 64 20 72 65 70 6f 72 74 20 65   up and report e
f34b0 72 72 6f 72 73 2e 0a 20 20 2a 2f 0a 20 20 73 71  rrors..  */.  sq
f34c0 6c 69 74 65 46 72 65 65 28 73 43 68 65 63 6b 2e  liteFree(sCheck.
f34d0 61 6e 52 65 66 29 3b 0a 20 20 2a 70 6e 45 72 72  anRef);.  *pnErr
f34e0 20 3d 20 73 43 68 65 63 6b 2e 6e 45 72 72 3b 0a   = sCheck.nErr;.
f34f0 20 20 72 65 74 75 72 6e 20 73 43 68 65 63 6b 2e    return sCheck.
f3500 7a 45 72 72 4d 73 67 3b 0a 7d 0a 23 65 6e 64 69  zErrMsg;.}.#endi
f3510 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
f3520 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b  _INTEGRITY_CHECK
f3530 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72   */../*.** Retur
f3540 6e 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68 6e  n the full pathn
f3550 61 6d 65 20 6f 66 20 74 68 65 20 75 6e 64 65 72  ame of the under
f3560 6c 79 69 6e 67 20 64 61 74 61 62 61 73 65 20 66  lying database f
f3570 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ile..*/.SQLITE_P
f3580 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61  RIVATE const cha
f3590 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 47  r *sqlite3BtreeG
f35a0 65 74 46 69 6c 65 6e 61 6d 65 28 42 74 72 65 65  etFilename(Btree
f35b0 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20   *p){.  assert( 
f35c0 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 21 3d  p->pBt->pPager!=
f35d0 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71  0 );.  return sq
f35e0 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 6e 61  lite3PagerFilena
f35f0 6d 65 28 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65  me(p->pBt->pPage
f3600 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  r);.}../*.** Ret
f3610 75 72 6e 20 74 68 65 20 70 61 74 68 6e 61 6d 65  urn the pathname
f3620 20 6f 66 20 74 68 65 20 64 69 72 65 63 74 6f 72   of the director
f3630 79 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20  y that contains 
f3640 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
f3650 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
f3660 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20  VATE const char 
f3670 2a 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74  *sqlite3BtreeGet
f3680 44 69 72 6e 61 6d 65 28 42 74 72 65 65 20 2a 70  Dirname(Btree *p
f3690 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  ){.  assert( p->
f36a0 70 42 74 2d 3e 70 50 61 67 65 72 21 3d 30 20 29  pBt->pPager!=0 )
f36b0 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  ;.  return sqlit
f36c0 65 33 50 61 67 65 72 44 69 72 6e 61 6d 65 28 70  e3PagerDirname(p
f36d0 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a  ->pBt->pPager);.
f36e0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
f36f0 74 68 65 20 70 61 74 68 6e 61 6d 65 20 6f 66 20  the pathname of 
f3700 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
f3710 20 66 6f 72 20 74 68 69 73 20 64 61 74 61 62 61   for this databa
f3720 73 65 2e 20 54 68 65 20 72 65 74 75 72 6e 0a 2a  se. The return.*
f3730 2a 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20  * value of this 
f3740 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 73  routine is the s
f3750 61 6d 65 20 72 65 67 61 72 64 6c 65 73 73 20 6f  ame regardless o
f3760 66 20 77 68 65 74 68 65 72 20 74 68 65 20 6a 6f  f whether the jo
f3770 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 68 61  urnal file.** ha
f3780 73 20 62 65 65 6e 20 63 72 65 61 74 65 64 20 6f  s been created o
f3790 72 20 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  r not..*/.SQLITE
f37a0 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63  _PRIVATE const c
f37b0 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65  har *sqlite3Btre
f37c0 65 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28  eGetJournalname(
f37d0 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73  Btree *p){.  ass
f37e0 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 70 50 61  ert( p->pBt->pPa
f37f0 67 65 72 21 3d 30 20 29 3b 0a 20 20 72 65 74 75  ger!=0 );.  retu
f3800 72 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a  rn sqlite3PagerJ
f3810 6f 75 72 6e 61 6c 6e 61 6d 65 28 70 2d 3e 70 42  ournalname(p->pB
f3820 74 2d 3e 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 23  t->pPager);.}..#
f3830 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
f3840 49 54 5f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20  IT_VACUUM./*.** 
f3850 43 6f 70 79 20 74 68 65 20 63 6f 6d 70 6c 65 74  Copy the complet
f3860 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 70 42 74  e content of pBt
f3870 46 72 6f 6d 20 69 6e 74 6f 20 70 42 74 54 6f 2e  From into pBtTo.
f3880 20 20 41 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a    A transaction.
f3890 2a 2a 20 6d 75 73 74 20 62 65 20 61 63 74 69 76  ** must be activ
f38a0 65 20 66 6f 72 20 62 6f 74 68 20 66 69 6c 65 73  e for both files
f38b0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65  ..**.** The size
f38c0 20 6f 66 20 66 69 6c 65 20 70 42 74 46 72 6f 6d   of file pBtFrom
f38d0 20 6d 61 79 20 62 65 20 72 65 64 75 63 65 64 20   may be reduced 
f38e0 62 79 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f  by this operatio
f38f0 6e 2e 0a 2a 2a 20 49 66 20 61 6e 79 74 68 69 6e  n..** If anythin
f3900 67 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20 74 68  g goes wrong, th
f3910 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e  e transaction on
f3920 20 70 42 74 46 72 6f 6d 20 69 73 20 72 6f 6c 6c   pBtFrom is roll
f3930 65 64 20 62 61 63 6b 2e 0a 2a 2f 0a 53 51 4c 49  ed back..*/.SQLI
f3940 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
f3950 71 6c 69 74 65 33 42 74 72 65 65 43 6f 70 79 46  qlite3BtreeCopyF
f3960 69 6c 65 28 42 74 72 65 65 20 2a 70 54 6f 2c 20  ile(Btree *pTo, 
f3970 42 74 72 65 65 20 2a 70 46 72 6f 6d 29 7b 0a 20  Btree *pFrom){. 
f3980 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
f3990 5f 4f 4b 3b 0a 20 20 50 67 6e 6f 20 69 2c 20 6e  _OK;.  Pgno i, n
f39a0 50 61 67 65 2c 20 6e 54 6f 50 61 67 65 2c 20 69  Page, nToPage, i
f39b0 53 6b 69 70 3b 0a 0a 20 20 42 74 53 68 61 72 65  Skip;..  BtShare
f39c0 64 20 2a 70 42 74 54 6f 20 3d 20 70 54 6f 2d 3e  d *pBtTo = pTo->
f39d0 70 42 74 3b 0a 20 20 42 74 53 68 61 72 65 64 20  pBt;.  BtShared 
f39e0 2a 70 42 74 46 72 6f 6d 20 3d 20 70 46 72 6f 6d  *pBtFrom = pFrom
f39f0 2d 3e 70 42 74 3b 0a 0a 20 20 69 66 28 20 70 54  ->pBt;..  if( pT
f3a00 6f 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e  o->inTrans!=TRAN
f3a10 53 5f 57 52 49 54 45 20 7c 7c 20 70 46 72 6f 6d  S_WRITE || pFrom
f3a20 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53  ->inTrans!=TRANS
f3a30 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20 72 65  _WRITE ){.    re
f3a40 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
f3a50 52 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 42 74  R;.  }.  if( pBt
f3a60 54 6f 2d 3e 70 43 75 72 73 6f 72 20 29 20 72 65  To->pCursor ) re
f3a70 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59  turn SQLITE_BUSY
f3a80 3b 0a 20 20 6e 54 6f 50 61 67 65 20 3d 20 73 71  ;.  nToPage = sq
f3a90 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f  lite3PagerPageco
f3aa0 75 6e 74 28 70 42 74 54 6f 2d 3e 70 50 61 67 65  unt(pBtTo->pPage
f3ab0 72 29 3b 0a 20 20 6e 50 61 67 65 20 3d 20 73 71  r);.  nPage = sq
f3ac0 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f  lite3PagerPageco
f3ad0 75 6e 74 28 70 42 74 46 72 6f 6d 2d 3e 70 50 61  unt(pBtFrom->pPa
f3ae0 67 65 72 29 3b 0a 20 20 69 53 6b 69 70 20 3d 20  ger);.  iSkip = 
f3af0 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47  PENDING_BYTE_PAG
f3b00 45 28 70 42 74 54 6f 29 3b 0a 20 20 66 6f 72 28  E(pBtTo);.  for(
f3b10 69 3d 31 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f  i=1; rc==SQLITE_
f3b20 4f 4b 20 26 26 20 69 3c 3d 6e 50 61 67 65 3b 20  OK && i<=nPage; 
f3b30 69 2b 2b 29 7b 0a 20 20 20 20 44 62 50 61 67 65  i++){.    DbPage
f3b40 20 2a 70 44 62 50 61 67 65 3b 0a 20 20 20 20 69   *pDbPage;.    i
f3b50 66 28 20 69 3d 3d 69 53 6b 69 70 20 29 20 63 6f  f( i==iSkip ) co
f3b60 6e 74 69 6e 75 65 3b 0a 20 20 20 20 72 63 20 3d  ntinue;.    rc =
f3b70 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
f3b80 28 70 42 74 46 72 6f 6d 2d 3e 70 50 61 67 65 72  (pBtFrom->pPager
f3b90 2c 20 69 2c 20 26 70 44 62 50 61 67 65 29 3b 0a  , i, &pDbPage);.
f3ba0 20 20 20 20 69 66 28 20 72 63 20 29 20 62 72 65      if( rc ) bre
f3bb0 61 6b 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  ak;.    rc = sql
f3bc0 69 74 65 33 50 61 67 65 72 4f 76 65 72 77 72 69  ite3PagerOverwri
f3bd0 74 65 28 70 42 74 54 6f 2d 3e 70 50 61 67 65 72  te(pBtTo->pPager
f3be0 2c 20 69 2c 20 73 71 6c 69 74 65 33 50 61 67 65  , i, sqlite3Page
f3bf0 72 47 65 74 44 61 74 61 28 70 44 62 50 61 67 65  rGetData(pDbPage
f3c00 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50  ));.    sqlite3P
f3c10 61 67 65 72 55 6e 72 65 66 28 70 44 62 50 61 67  agerUnref(pDbPag
f3c20 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66  e);.  }..  /* If
f3c30 20 74 68 65 20 66 69 6c 65 20 69 73 20 73 68 72   the file is shr
f3c40 69 6e 6b 69 6e 67 2c 20 6a 6f 75 72 6e 61 6c 20  inking, journal 
f3c50 74 68 65 20 70 61 67 65 73 20 74 68 61 74 20 61  the pages that a
f3c60 72 65 20 62 65 69 6e 67 20 74 72 75 6e 63 61 74  re being truncat
f3c70 65 64 0a 20 20 2a 2a 20 73 6f 20 74 68 61 74 20  ed.  ** so that 
f3c80 74 68 65 79 20 63 61 6e 20 62 65 20 72 6f 6c 6c  they can be roll
f3c90 65 64 20 62 61 63 6b 20 69 66 20 74 68 65 20 63  ed back if the c
f3ca0 6f 6d 6d 69 74 20 66 61 69 6c 73 2e 0a 20 20 2a  ommit fails..  *
f3cb0 2f 0a 20 20 66 6f 72 28 69 3d 6e 50 61 67 65 2b  /.  for(i=nPage+
f3cc0 31 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  1; rc==SQLITE_OK
f3cd0 20 26 26 20 69 3c 3d 6e 54 6f 50 61 67 65 3b 20   && i<=nToPage; 
f3ce0 69 2b 2b 29 7b 0a 20 20 20 20 44 62 50 61 67 65  i++){.    DbPage
f3cf0 20 2a 70 44 62 50 61 67 65 3b 0a 20 20 20 20 69   *pDbPage;.    i
f3d00 66 28 20 69 3d 3d 69 53 6b 69 70 20 29 20 63 6f  f( i==iSkip ) co
f3d10 6e 74 69 6e 75 65 3b 0a 20 20 20 20 72 63 20 3d  ntinue;.    rc =
f3d20 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
f3d30 28 70 42 74 54 6f 2d 3e 70 50 61 67 65 72 2c 20  (pBtTo->pPager, 
f3d40 69 2c 20 26 70 44 62 50 61 67 65 29 3b 0a 20 20  i, &pDbPage);.  
f3d50 20 20 69 66 28 20 72 63 20 29 20 62 72 65 61 6b    if( rc ) break
f3d60 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
f3d70 65 33 50 61 67 65 72 57 72 69 74 65 28 70 44 62  e3PagerWrite(pDb
f3d80 50 61 67 65 29 3b 0a 20 20 20 20 73 71 6c 69 74  Page);.    sqlit
f3d90 65 33 50 61 67 65 72 44 6f 6e 74 57 72 69 74 65  e3PagerDontWrite
f3da0 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 2f  (pDbPage);.    /
f3db0 2a 20 59 65 61 68 2e 20 20 49 74 20 73 65 65 6d  * Yeah.  It seem
f3dc0 73 20 77 69 65 72 64 20 74 6f 20 63 61 6c 6c 20  s wierd to call 
f3dd0 44 6f 6e 74 57 72 69 74 65 28 29 20 72 69 67 68  DontWrite() righ
f3de0 74 20 61 66 74 65 72 20 57 72 69 74 65 28 29 2e  t after Write().
f3df0 20 20 42 75 74 0a 20 20 20 20 2a 2a 20 74 68 61    But.    ** tha
f3e00 74 20 69 73 20 62 65 63 61 75 73 65 20 74 68 65  t is because the
f3e10 20 6e 61 6d 65 73 20 6f 66 20 74 68 6f 73 65 20   names of those 
f3e20 70 72 6f 63 65 64 75 72 65 73 20 64 6f 20 6e 6f  procedures do no
f3e30 74 20 65 78 61 63 74 6c 79 20 0a 20 20 20 20 2a  t exactly .    *
f3e40 2a 20 72 65 70 72 65 73 65 6e 74 20 77 68 61 74  * represent what
f3e50 20 74 68 65 79 20 64 6f 2e 20 20 57 72 69 74 65   they do.  Write
f3e60 28 29 20 72 65 61 6c 6c 79 20 6d 65 61 6e 73 20  () really means 
f3e70 22 70 75 74 20 74 68 69 73 20 70 61 67 65 20 69  "put this page i
f3e80 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 6f 6c  n the.    ** rol
f3e90 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 61 6e  lback journal an
f3ea0 64 20 6d 61 72 6b 20 69 74 20 61 73 20 64 69 72  d mark it as dir
f3eb0 74 79 20 73 6f 20 74 68 61 74 20 69 74 20 77 69  ty so that it wi
f3ec0 6c 6c 20 62 65 20 77 72 69 74 74 65 6e 0a 20 20  ll be written.  
f3ed0 20 20 2a 2a 20 74 6f 20 74 68 65 20 64 61 74 61    ** to the data
f3ee0 62 61 73 65 20 66 69 6c 65 20 6c 61 74 65 72 2e  base file later.
f3ef0 22 20 20 44 6f 6e 74 57 72 69 74 65 28 29 20 75  "  DontWrite() u
f3f00 6e 64 6f 65 73 20 74 68 65 20 73 65 63 6f 6e 64  ndoes the second
f3f10 20 70 61 72 74 20 6f 66 0a 20 20 20 20 2a 2a 20   part of.    ** 
f3f20 74 68 61 74 20 61 6e 64 20 70 72 65 76 65 6e 74  that and prevent
f3f30 73 20 74 68 65 20 70 61 67 65 20 66 72 6f 6d 20  s the page from 
f3f40 62 65 69 6e 67 20 77 72 69 74 74 65 6e 20 74 6f  being written to
f3f50 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20   the database.  
f3f60 54 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 65 20  The.    ** page 
f3f70 69 73 20 73 74 69 6c 6c 20 6f 6e 20 74 68 65 20  is still on the 
f3f80 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
f3f90 2c 20 74 68 6f 75 67 68 2e 20 20 41 6e 64 20 74  , though.  And t
f3fa0 68 61 74 20 69 73 20 74 68 65 20 77 68 6f 6c 65  hat is the whole
f3fb0 0a 20 20 20 20 2a 2a 20 70 6f 69 6e 74 20 6f 66  .    ** point of
f3fc0 20 74 68 69 73 20 6c 6f 6f 70 3a 20 74 6f 20 70   this loop: to p
f3fd0 75 74 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20  ut pages on the 
f3fe0 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
f3ff0 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  . */.    sqlite3
f4000 50 61 67 65 72 55 6e 72 65 66 28 70 44 62 50 61  PagerUnref(pDbPa
f4010 67 65 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21  ge);.  }.  if( !
f4020 72 63 20 26 26 20 6e 50 61 67 65 3c 6e 54 6f 50  rc && nPage<nToP
f4030 61 67 65 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  age ){.    rc = 
f4040 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e  sqlite3PagerTrun
f4050 63 61 74 65 28 70 42 74 54 6f 2d 3e 70 50 61 67  cate(pBtTo->pPag
f4060 65 72 2c 20 6e 50 61 67 65 29 3b 0a 20 20 7d 0a  er, nPage);.  }.
f4070 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20  .  if( rc ){.   
f4080 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c   sqlite3BtreeRol
f4090 6c 62 61 63 6b 28 70 54 6f 29 3b 0a 20 20 7d 0a  lback(pTo);.  }.
f40a0 20 20 72 65 74 75 72 6e 20 72 63 3b 20 20 0a 7d    return rc;  .}
f40b0 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
f40c0 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 20 2a 2f  E_OMIT_VACUUM */
f40d0 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 6e  ../*.** Return n
f40e0 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20 74 72 61  on-zero if a tra
f40f0 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69  nsaction is acti
f4100 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ve..*/.SQLITE_PR
f4110 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
f4120 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28  3BtreeIsInTrans(
f4130 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 72 65 74  Btree *p){.  ret
f4140 75 72 6e 20 28 70 20 26 26 20 28 70 2d 3e 69 6e  urn (p && (p->in
f4150 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49  Trans==TRANS_WRI
f4160 54 45 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  TE));.}../*.** R
f4170 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69  eturn non-zero i
f4180 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72  f a statement tr
f4190 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74  ansaction is act
f41a0 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ive..*/.SQLITE_P
f41b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
f41c0 65 33 42 74 72 65 65 49 73 49 6e 53 74 6d 74 28  e3BtreeIsInStmt(
f41d0 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 72 65 74  Btree *p){.  ret
f41e0 75 72 6e 20 28 70 2d 3e 70 42 74 20 26 26 20 70  urn (p->pBt && p
f41f0 2d 3e 70 42 74 2d 3e 69 6e 53 74 6d 74 29 3b 0a  ->pBt->inStmt);.
f4200 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
f4210 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20 72 65  non-zero if a re
f4220 61 64 20 28 6f 72 20 77 72 69 74 65 29 20 74 72  ad (or write) tr
f4230 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74  ansaction is act
f4240 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ive..*/.SQLITE_P
f4250 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
f4260 65 33 42 74 72 65 65 49 73 49 6e 52 65 61 64 54  e3BtreeIsInReadT
f4270 72 61 6e 73 28 42 74 72 65 65 20 2a 70 29 7b 0a  rans(Btree *p){.
f4280 20 20 72 65 74 75 72 6e 20 28 70 20 26 26 20 28    return (p && (
f4290 70 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e  p->inTrans!=TRAN
f42a0 53 5f 4e 4f 4e 45 29 29 3b 0a 7d 0a 0a 2f 2a 0a  S_NONE));.}../*.
f42b0 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
f42c0 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
f42d0 65 72 20 74 6f 20 61 20 62 6c 6f 62 20 6f 66 20  er to a blob of 
f42e0 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65  memory associate
f42f0 64 20 77 69 74 68 0a 2a 2a 20 61 20 73 69 6e 67  d with.** a sing
f4300 6c 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 2e  le shared-btree.
f4310 20 54 68 65 20 6d 65 6d 6f 72 79 20 69 73 20 75   The memory is u
f4320 73 65 64 20 62 79 20 63 6c 69 65 6e 74 20 63 6f  sed by client co
f4330 64 65 20 66 6f 72 20 69 74 27 73 20 6f 77 6e 0a  de for it's own.
f4340 2a 2a 20 70 75 72 70 6f 73 65 73 20 28 66 6f 72  ** purposes (for
f4350 20 65 78 61 6d 70 6c 65 2c 20 74 6f 20 73 74 6f   example, to sto
f4360 72 65 20 61 20 68 69 67 68 2d 6c 65 76 65 6c 20  re a high-level 
f4370 73 63 68 65 6d 61 20 61 73 73 6f 63 69 61 74 65  schema associate
f4380 64 20 77 69 74 68 20 0a 2a 2a 20 74 68 65 20 73  d with .** the s
f4390 68 61 72 65 64 2d 62 74 72 65 65 29 2e 20 54 68  hared-btree). Th
f43a0 65 20 62 74 72 65 65 20 6c 61 79 65 72 20 6d 61  e btree layer ma
f43b0 6e 61 67 65 73 20 72 65 66 65 72 65 6e 63 65 20  nages reference 
f43c0 63 6f 75 6e 74 69 6e 67 20 69 73 73 75 65 73 2e  counting issues.
f43d0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  .**.** The first
f43e0 20 74 69 6d 65 20 74 68 69 73 20 69 73 20 63 61   time this is ca
f43f0 6c 6c 65 64 20 6f 6e 20 61 20 73 68 61 72 65 64  lled on a shared
f4400 2d 62 74 72 65 65 2c 20 6e 42 79 74 65 73 20 62  -btree, nBytes b
f4410 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a  ytes of memory.*
f4420 2a 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 2c  * are allocated,
f4430 20 7a 65 72 6f 65 64 2c 20 61 6e 64 20 72 65 74   zeroed, and ret
f4440 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c  urned to the cal
f4450 6c 65 72 2e 20 46 6f 72 20 65 61 63 68 20 73 75  ler. For each su
f4460 62 73 65 71 75 65 6e 74 20 0a 2a 2a 20 63 61 6c  bsequent .** cal
f4470 6c 20 74 68 65 20 6e 42 79 74 65 73 20 70 61 72  l the nBytes par
f4480 61 6d 65 74 65 72 20 69 73 20 69 67 6e 6f 72 65  ameter is ignore
f4490 64 20 61 6e 64 20 61 20 70 6f 69 6e 74 65 72 20  d and a pointer 
f44a0 74 6f 20 74 68 65 20 73 61 6d 65 20 62 6c 6f 62  to the same blob
f44b0 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65  .** of memory re
f44c0 74 75 72 6e 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 4a  turned. .**.** J
f44d0 75 73 74 20 62 65 66 6f 72 65 20 74 68 65 20 73  ust before the s
f44e0 68 61 72 65 64 2d 62 74 72 65 65 20 69 73 20 63  hared-btree is c
f44f0 6c 6f 73 65 64 2c 20 74 68 65 20 66 75 6e 63 74  losed, the funct
f4500 69 6f 6e 20 70 61 73 73 65 64 20 61 73 20 74 68  ion passed as th
f4510 65 20 0a 2a 2a 20 78 46 72 65 65 20 61 72 67 75  e .** xFree argu
f4520 6d 65 6e 74 20 77 68 65 6e 20 74 68 65 20 6d 65  ment when the me
f4530 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
f4540 77 61 73 20 6d 61 64 65 20 69 73 20 69 6e 76 6f  was made is invo
f4550 6b 65 64 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 62  ked on the .** b
f4560 6c 6f 62 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64  lob of allocated
f4570 20 6d 65 6d 6f 72 79 2e 20 54 68 69 73 20 66 75   memory. This fu
f4580 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f  nction should no
f4590 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 46 72 65  t call sqliteFre
f45a0 65 28 29 0a 2a 2a 20 6f 6e 20 74 68 65 20 6d 65  e().** on the me
f45b0 6d 6f 72 79 2c 20 74 68 65 20 62 74 72 65 65 20  mory, the btree 
f45c0 6c 61 79 65 72 20 64 6f 65 73 20 74 68 61 74 2e  layer does that.
f45d0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
f45e0 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
f45f0 42 74 72 65 65 53 63 68 65 6d 61 28 42 74 72 65  BtreeSchema(Btre
f4600 65 20 2a 70 2c 20 69 6e 74 20 6e 42 79 74 65 73  e *p, int nBytes
f4610 2c 20 76 6f 69 64 28 2a 78 46 72 65 65 29 28 76  , void(*xFree)(v
f4620 6f 69 64 20 2a 29 29 7b 0a 20 20 42 74 53 68 61  oid *)){.  BtSha
f4630 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
f4640 74 3b 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 70  t;.  if( !pBt->p
f4650 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 70 42  Schema ){.    pB
f4660 74 2d 3e 70 53 63 68 65 6d 61 20 3d 20 73 71 6c  t->pSchema = sql
f4670 69 74 65 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 73  iteMalloc(nBytes
f4680 29 3b 0a 20 20 20 20 70 42 74 2d 3e 78 46 72 65  );.    pBt->xFre
f4690 65 53 63 68 65 6d 61 20 3d 20 78 46 72 65 65 3b  eSchema = xFree;
f46a0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 42  .  }.  return pB
f46b0 74 2d 3e 70 53 63 68 65 6d 61 3b 0a 7d 0a 0a 2f  t->pSchema;.}../
f46c0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65  *.** Return true
f46d0 20 69 66 20 61 6e 6f 74 68 65 72 20 75 73 65 72   if another user
f46e0 20 6f 66 20 74 68 65 20 73 61 6d 65 20 73 68 61   of the same sha
f46f0 72 65 64 20 62 74 72 65 65 20 61 73 20 74 68 65  red btree as the
f4700 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 68 61 6e   argument.** han
f4710 64 6c 65 20 68 6f 6c 64 73 20 61 6e 20 65 78 63  dle holds an exc
f4720 6c 75 73 69 76 65 20 6c 6f 63 6b 20 6f 6e 20 74  lusive lock on t
f4730 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  he sqlite_master
f4740 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54   table..*/.SQLIT
f4750 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
f4760 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61  lite3BtreeSchema
f4770 4c 6f 63 6b 65 64 28 42 74 72 65 65 20 2a 70 29  Locked(Btree *p)
f4780 7b 0a 20 20 72 65 74 75 72 6e 20 28 71 75 65 72  {.  return (quer
f4790 79 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 4d 41  yTableLock(p, MA
f47a0 53 54 45 52 5f 52 4f 4f 54 2c 20 52 45 41 44 5f  STER_ROOT, READ_
f47b0 4c 4f 43 4b 29 21 3d 53 51 4c 49 54 45 5f 4f 4b  LOCK)!=SQLITE_OK
f47c0 29 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53  );.}...#ifndef S
f47d0 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
f47e0 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 4f 62  D_CACHE./*.** Ob
f47f0 74 61 69 6e 20 61 20 6c 6f 63 6b 20 6f 6e 20 74  tain a lock on t
f4800 68 65 20 74 61 62 6c 65 20 77 68 6f 73 65 20 72  he table whose r
f4810 6f 6f 74 20 70 61 67 65 20 69 73 20 69 54 61 62  oot page is iTab
f4820 2e 20 20 54 68 65 0a 2a 2a 20 6c 6f 63 6b 20 69  .  The.** lock i
f4830 73 20 61 20 77 72 69 74 65 20 6c 6f 63 6b 20 69  s a write lock i
f4840 66 20 69 73 57 72 69 74 65 6c 6f 63 6b 20 69 73  f isWritelock is
f4850 20 74 72 75 65 20 6f 72 20 61 20 72 65 61 64 20   true or a read 
f4860 6c 6f 63 6b 0a 2a 2a 20 69 66 20 69 74 20 69 73  lock.** if it is
f4870 20 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54   false..*/.SQLIT
f4880 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
f4890 6c 69 74 65 33 42 74 72 65 65 4c 6f 63 6b 54 61  lite3BtreeLockTa
f48a0 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e  ble(Btree *p, in
f48b0 74 20 69 54 61 62 2c 20 75 38 20 69 73 57 72 69  t iTab, u8 isWri
f48c0 74 65 4c 6f 63 6b 29 7b 0a 20 20 69 6e 74 20 72  teLock){.  int r
f48d0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
f48e0 20 75 38 20 6c 6f 63 6b 54 79 70 65 20 3d 20 28   u8 lockType = (
f48f0 69 73 57 72 69 74 65 4c 6f 63 6b 3f 57 52 49 54  isWriteLock?WRIT
f4900 45 5f 4c 4f 43 4b 3a 52 45 41 44 5f 4c 4f 43 4b  E_LOCK:READ_LOCK
f4910 29 3b 0a 20 20 72 63 20 3d 20 71 75 65 72 79 54  );.  rc = queryT
f4920 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62  ableLock(p, iTab
f4930 2c 20 6c 6f 63 6b 54 79 70 65 29 3b 0a 20 20 69  , lockType);.  i
f4940 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
f4950 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 6c 6f 63   ){.    rc = loc
f4960 6b 54 61 62 6c 65 28 70 2c 20 69 54 61 62 2c 20  kTable(p, iTab, 
f4970 6c 6f 63 6b 54 79 70 65 29 3b 0a 20 20 7d 0a 20  lockType);.  }. 
f4980 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65   return rc;.}.#e
f4990 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
f49a0 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c  LITE_OMIT_INCRBL
f49b0 4f 42 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e  OB./*.** Argumen
f49c0 74 20 70 43 73 72 20 6d 75 73 74 20 62 65 20 61  t pCsr must be a
f49d0 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20 66   cursor opened f
f49e0 6f 72 20 77 72 69 74 69 6e 67 20 6f 6e 20 61 6e  or writing on an
f49f0 20 0a 2a 2a 20 49 4e 54 4b 45 59 20 74 61 62 6c   .** INTKEY tabl
f4a00 65 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e  e currently poin
f4a10 74 69 6e 67 20 61 74 20 61 20 76 61 6c 69 64 20  ting at a valid 
f4a20 74 61 62 6c 65 20 65 6e 74 72 79 2e 20 0a 2a 2a  table entry. .**
f4a30 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d   This function m
f4a40 6f 64 69 66 69 65 73 20 74 68 65 20 64 61 74 61  odifies the data
f4a50 20 73 74 6f 72 65 64 20 61 73 20 70 61 72 74 20   stored as part 
f4a60 6f 66 20 74 68 61 74 20 65 6e 74 72 79 2e 0a 2a  of that entry..*
f4a70 2a 20 4f 6e 6c 79 20 74 68 65 20 64 61 74 61 20  * Only the data 
f4a80 63 6f 6e 74 65 6e 74 20 6d 61 79 20 6f 6e 6c 79  content may only
f4a90 20 62 65 20 6d 6f 64 69 66 69 65 64 2c 20 69 74   be modified, it
f4aa0 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
f4ab0 0a 2a 2a 20 74 6f 20 63 68 61 6e 67 65 20 74 68  .** to change th
f4ac0 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20  e length of the 
f4ad0 64 61 74 61 20 73 74 6f 72 65 64 2e 0a 2a 2f 0a  data stored..*/.
f4ae0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
f4af0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 50  nt sqlite3BtreeP
f4b00 75 74 44 61 74 61 28 42 74 43 75 72 73 6f 72 20  utData(BtCursor 
f4b10 2a 70 43 73 72 2c 20 75 33 32 20 6f 66 66 73 65  *pCsr, u32 offse
f4b20 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64  t, u32 amt, void
f4b30 20 2a 7a 29 7b 0a 0a 20 20 61 73 73 65 72 74 28   *z){..  assert(
f4b40 70 43 73 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62  pCsr->isIncrblob
f4b50 48 61 6e 64 6c 65 29 3b 0a 20 20 69 66 28 20 70  Handle);.  if( p
f4b60 43 73 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52  Csr->eState==CUR
f4b70 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20  SOR_REQUIRESEEK 
f4b80 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
f4b90 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a  LITE_ABORT;.  }.
f4ba0 0a 20 20 2f 2a 20 43 68 65 63 6b 20 73 6f 6d 65  .  /* Check some
f4bb0 20 70 72 65 63 6f 6e 64 69 74 69 6f 6e 73 3a 20   preconditions: 
f4bc0 0a 20 20 2a 2a 20 20 20 28 61 29 20 74 68 65 20  .  **   (a) the 
f4bd0 63 75 72 73 6f 72 20 69 73 20 6f 70 65 6e 20 66  cursor is open f
f4be0 6f 72 20 77 72 69 74 69 6e 67 2c 0a 20 20 2a 2a  or writing,.  **
f4bf0 20 20 20 28 62 29 20 74 68 65 72 65 20 69 73 20     (b) there is 
f4c00 6e 6f 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20  no read-lock on 
f4c10 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20  the table being 
f4c20 6d 6f 64 69 66 69 65 64 20 61 6e 64 0a 20 20 2a  modified and.  *
f4c30 2a 20 20 20 28 63 29 20 74 68 65 20 63 75 72 73  *   (c) the curs
f4c40 6f 72 20 70 6f 69 6e 74 73 20 61 74 20 61 20 76  or points at a v
f4c50 61 6c 69 64 20 72 6f 77 20 6f 66 20 61 6e 20 69  alid row of an i
f4c60 6e 74 4b 65 79 20 74 61 62 6c 65 2e 0a 20 20 2a  ntKey table..  *
f4c70 2f 0a 20 20 69 66 28 20 21 70 43 73 72 2d 3e 77  /.  if( !pCsr->w
f4c80 72 46 6c 61 67 20 29 7b 0a 20 20 20 20 72 65 74  rFlag ){.    ret
f4c90 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f  urn SQLITE_READO
f4ca0 4e 4c 59 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  NLY;.  }.  asser
f4cb0 74 28 20 21 70 43 73 72 2d 3e 70 42 74 72 65 65  t( !pCsr->pBtree
f4cc0 2d 3e 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20  ->pBt->readOnly 
f4cd0 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 70 43  .          && pC
f4ce0 73 72 2d 3e 70 42 74 72 65 65 2d 3e 70 42 74 2d  sr->pBtree->pBt-
f4cf0 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d  >inTransaction==
f4d00 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20  TRANS_WRITE );. 
f4d10 20 69 66 28 20 63 68 65 63 6b 52 65 61 64 4c 6f   if( checkReadLo
f4d20 63 6b 73 28 70 43 73 72 2d 3e 70 42 74 72 65 65  cks(pCsr->pBtree
f4d30 2c 20 70 43 73 72 2d 3e 70 67 6e 6f 52 6f 6f 74  , pCsr->pgnoRoot
f4d40 2c 20 70 43 73 72 29 20 29 7b 0a 20 20 20 20 72  , pCsr) ){.    r
f4d50 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43  eturn SQLITE_LOC
f4d60 4b 45 44 3b 20 2f 2a 20 54 68 65 20 74 61 62 6c  KED; /* The tabl
f4d70 65 20 70 43 75 72 20 70 6f 69 6e 74 73 20 74 6f  e pCur points to
f4d80 20 68 61 73 20 61 20 72 65 61 64 20 6c 6f 63 6b   has a read lock
f4d90 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 43   */.  }.  if( pC
f4da0 73 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53  sr->eState==CURS
f4db0 4f 52 5f 49 4e 56 41 4c 49 44 20 7c 7c 20 21 70  OR_INVALID || !p
f4dc0 43 73 72 2d 3e 70 50 61 67 65 2d 3e 69 6e 74 4b  Csr->pPage->intK
f4dd0 65 79 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ey ){.    return
f4de0 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
f4df0 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 61 63 63   }..  return acc
f4e00 65 73 73 50 61 79 6c 6f 61 64 28 70 43 73 72 2c  essPayload(pCsr,
f4e10 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 28 75   offset, amt, (u
f4e20 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a  nsigned char *)z
f4e30 2c 20 30 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 20 0a  , 0, 1);.}../* .
f4e40 2a 2a 20 53 65 74 20 61 20 66 6c 61 67 20 6f 6e  ** Set a flag on
f4e50 20 74 68 69 73 20 63 75 72 73 6f 72 20 74 6f 20   this cursor to 
f4e60 63 61 63 68 65 20 74 68 65 20 6c 6f 63 61 74 69  cache the locati
f4e70 6f 6e 73 20 6f 66 20 70 61 67 65 73 20 66 72 6f  ons of pages fro
f4e80 6d 20 74 68 65 20 0a 2a 2a 20 6f 76 65 72 66 6c  m the .** overfl
f4e90 6f 77 20 6c 69 73 74 20 66 6f 72 20 74 68 65 20  ow list for the 
f4ea0 63 75 72 72 65 6e 74 20 72 6f 77 2e 20 54 68 69  current row. Thi
f4eb0 73 20 69 73 20 75 73 65 64 20 62 79 20 63 75 72  s is used by cur
f4ec0 73 6f 72 73 20 6f 70 65 6e 65 64 0a 2a 2a 20 66  sors opened.** f
f4ed0 6f 72 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 62  or incremental b
f4ee0 6c 6f 62 20 49 4f 20 6f 6e 6c 79 2e 0a 2a 2a 0a  lob IO only..**.
f4ef0 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
f4f00 20 73 65 74 73 20 61 20 66 6c 61 67 20 6f 6e 6c   sets a flag onl
f4f10 79 2e 20 54 68 65 20 61 63 74 75 61 6c 20 70 61  y. The actual pa
f4f20 67 65 20 6c 6f 63 61 74 69 6f 6e 20 63 61 63 68  ge location cach
f4f30 65 0a 2a 2a 20 28 73 74 6f 72 65 64 20 69 6e 20  e.** (stored in 
f4f40 42 74 43 75 72 73 6f 72 2e 61 4f 76 65 72 66 6c  BtCursor.aOverfl
f4f50 6f 77 5b 5d 29 20 69 73 20 61 6c 6c 6f 63 61 74  ow[]) is allocat
f4f60 65 64 20 61 6e 64 20 75 73 65 64 20 62 79 20 66  ed and used by f
f4f70 75 6e 63 74 69 6f 6e 0a 2a 2a 20 61 63 63 65 73  unction.** acces
f4f80 73 50 61 79 6c 6f 61 64 28 29 20 28 74 68 65 20  sPayload() (the 
f4f90 77 6f 72 6b 65 72 20 66 75 6e 63 74 69 6f 6e 20  worker function 
f4fa0 66 6f 72 20 73 71 6c 69 74 65 33 42 74 72 65 65  for sqlite3Btree
f4fb0 44 61 74 61 28 29 20 61 6e 64 0a 2a 2a 20 73 71  Data() and.** sq
f4fc0 6c 69 74 65 33 42 74 72 65 65 50 75 74 44 61 74  lite3BtreePutDat
f4fd0 61 28 29 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  a())..*/.SQLITE_
f4fe0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
f4ff0 69 74 65 33 42 74 72 65 65 43 61 63 68 65 4f 76  ite3BtreeCacheOv
f5000 65 72 66 6c 6f 77 28 42 74 43 75 72 73 6f 72 20  erflow(BtCursor 
f5010 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 65 72 74  *pCur){.  assert
f5020 28 21 70 43 75 72 2d 3e 69 73 49 6e 63 72 62 6c  (!pCur->isIncrbl
f5030 6f 62 48 61 6e 64 6c 65 29 3b 0a 20 20 61 73 73  obHandle);.  ass
f5040 65 72 74 28 21 70 43 75 72 2d 3e 61 4f 76 65 72  ert(!pCur->aOver
f5050 66 6c 6f 77 29 3b 0a 20 20 70 43 75 72 2d 3e 69  flow);.  pCur->i
f5060 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20  sIncrblobHandle 
f5070 3d 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  = 1;.}.#endif../
f5080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
f5090 6e 64 20 6f 66 20 62 74 72 65 65 2e 63 20 2a 2a  nd of btree.c **
f50a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f50b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f50c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
f50d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
f50e0 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 66 69  egin file vdbefi
f50f0 66 6f 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  fo.c ***********
f5100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
f5120 2a 0a 2a 2a 20 32 30 30 35 20 4a 75 6e 65 20 31  *.** 2005 June 1
f5130 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  6.**.** The auth
f5140 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
f5150 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
f5160 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
f5170 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
f5180 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
f5190 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
f51a0 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
f51b0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
f51c0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
f51d0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
f51e0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
f51f0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
f5200 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
f5210 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
f5220 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
f5230 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
f5240 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
f5250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5290 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d  .** This file im
f52a0 70 6c 65 6d 65 6e 74 73 20 61 20 46 49 46 4f 20  plements a FIFO 
f52b0 71 75 65 75 65 20 6f 66 20 72 6f 77 69 64 73 20  queue of rowids 
f52c0 75 73 65 64 20 66 6f 72 20 70 72 6f 63 65 73 73  used for process
f52d0 69 6e 67 0a 2a 2a 20 55 50 44 41 54 45 20 61 6e  ing.** UPDATE an
f52e0 64 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65  d DELETE stateme
f52f0 6e 74 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41  nts..*/../*.** A
f5300 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 46 69  llocate a new Fi
f5310 66 6f 50 61 67 65 20 61 6e 64 20 72 65 74 75 72  foPage and retur
f5320 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69  n a pointer to i
f5330 74 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20  t.  Return NULL 
f5340 69 66 0a 2a 2a 20 77 65 20 72 75 6e 20 6f 75 74  if.** we run out
f5350 20 6f 66 20 6d 65 6d 6f 72 79 2e 20 20 4c 65 61   of memory.  Lea
f5360 76 65 20 73 70 61 63 65 20 6f 6e 20 74 68 65 20  ve space on the 
f5370 70 61 67 65 20 66 6f 72 20 6e 45 6e 74 72 79 20  page for nEntry 
f5380 65 6e 74 72 69 65 73 2e 0a 2a 2f 0a 73 74 61 74  entries..*/.stat
f5390 69 63 20 46 69 66 6f 50 61 67 65 20 2a 61 6c 6c  ic FifoPage *all
f53a0 6f 63 61 74 65 46 69 66 6f 50 61 67 65 28 69 6e  ocateFifoPage(in
f53b0 74 20 6e 45 6e 74 72 79 29 7b 0a 20 20 46 69 66  t nEntry){.  Fif
f53c0 6f 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20  oPage *pPage;.  
f53d0 69 66 28 20 6e 45 6e 74 72 79 3e 33 32 37 36 37  if( nEntry>32767
f53e0 20 29 7b 0a 20 20 20 20 6e 45 6e 74 72 79 20 3d   ){.    nEntry =
f53f0 20 33 32 37 36 37 3b 0a 20 20 7d 0a 20 20 70 50   32767;.  }.  pP
f5400 61 67 65 20 3d 20 73 71 6c 69 74 65 4d 61 6c 6c  age = sqliteMall
f5410 6f 63 52 61 77 28 20 73 69 7a 65 6f 66 28 46 69  ocRaw( sizeof(Fi
f5420 66 6f 50 61 67 65 29 20 2b 20 73 69 7a 65 6f 66  foPage) + sizeof
f5430 28 69 36 34 29 2a 28 6e 45 6e 74 72 79 2d 31 29  (i64)*(nEntry-1)
f5440 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 20   );.  if( pPage 
f5450 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 53  ){.    pPage->nS
f5460 6c 6f 74 20 3d 20 6e 45 6e 74 72 79 3b 0a 20 20  lot = nEntry;.  
f5470 20 20 70 50 61 67 65 2d 3e 69 57 72 69 74 65 20    pPage->iWrite 
f5480 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e  = 0;.    pPage->
f5490 69 52 65 61 64 20 3d 20 30 3b 0a 20 20 20 20 70  iRead = 0;.    p
f54a0 50 61 67 65 2d 3e 70 4e 65 78 74 20 3d 20 30 3b  Page->pNext = 0;
f54b0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 50  .  }.  return pP
f54c0 61 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  age;.}../*.** In
f54d0 69 74 69 61 6c 69 7a 65 20 61 20 46 69 66 6f 20  itialize a Fifo 
f54e0 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51  structure..*/.SQ
f54f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
f5500 64 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 66  d sqlite3VdbeFif
f5510 6f 49 6e 69 74 28 46 69 66 6f 20 2a 70 46 69 66  oInit(Fifo *pFif
f5520 6f 29 7b 0a 20 20 6d 65 6d 73 65 74 28 70 46 69  o){.  memset(pFi
f5530 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70  fo, 0, sizeof(*p
f5540 46 69 66 6f 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Fifo));.}../*.**
f5550 20 50 75 73 68 20 61 20 73 69 6e 67 6c 65 20 36   Push a single 6
f5560 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 76 61  4-bit integer va
f5570 6c 75 65 20 69 6e 74 6f 20 74 68 65 20 46 69 66  lue into the Fif
f5580 6f 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54  o.  Return SQLIT
f5590 45 5f 4f 4b 0a 2a 2a 20 6e 6f 72 6d 61 6c 6c 79  E_OK.** normally
f55a0 2e 20 20 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  .   SQLITE_NOMEM
f55b0 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
f55c0 77 65 20 61 72 65 20 75 6e 61 62 6c 65 20 74 6f  we are unable to
f55d0 20 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 6d 65 6d   allocate.** mem
f55e0 6f 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ory..*/.SQLITE_P
f55f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
f5600 65 33 56 64 62 65 46 69 66 6f 50 75 73 68 28 46  e3VdbeFifoPush(F
f5610 69 66 6f 20 2a 70 46 69 66 6f 2c 20 69 36 34 20  ifo *pFifo, i64 
f5620 76 61 6c 29 7b 0a 20 20 46 69 66 6f 50 61 67 65  val){.  FifoPage
f5630 20 2a 70 50 61 67 65 3b 0a 20 20 70 50 61 67 65   *pPage;.  pPage
f5640 20 3d 20 70 46 69 66 6f 2d 3e 70 4c 61 73 74 3b   = pFifo->pLast;
f5650 0a 20 20 69 66 28 20 70 50 61 67 65 3d 3d 30 20  .  if( pPage==0 
f5660 29 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70  ){.    pPage = p
f5670 46 69 66 6f 2d 3e 70 4c 61 73 74 20 3d 20 70 46  Fifo->pLast = pF
f5680 69 66 6f 2d 3e 70 46 69 72 73 74 20 3d 20 61 6c  ifo->pFirst = al
f5690 6c 6f 63 61 74 65 46 69 66 6f 50 61 67 65 28 32  locateFifoPage(2
f56a0 30 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67  0);.    if( pPag
f56b0 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65  e==0 ){.      re
f56c0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
f56d0 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  M;.    }.  }else
f56e0 20 69 66 28 20 70 50 61 67 65 2d 3e 69 57 72 69   if( pPage->iWri
f56f0 74 65 3e 3d 70 50 61 67 65 2d 3e 6e 53 6c 6f 74  te>=pPage->nSlot
f5700 20 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70   ){.    pPage->p
f5710 4e 65 78 74 20 3d 20 61 6c 6c 6f 63 61 74 65 46  Next = allocateF
f5720 69 66 6f 50 61 67 65 28 70 46 69 66 6f 2d 3e 6e  ifoPage(pFifo->n
f5730 45 6e 74 72 79 29 3b 0a 20 20 20 20 69 66 28 20  Entry);.    if( 
f5740 70 50 61 67 65 2d 3e 70 4e 65 78 74 3d 3d 30 20  pPage->pNext==0 
f5750 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
f5760 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
f5770 20 20 7d 0a 20 20 20 20 70 50 61 67 65 20 3d 20    }.    pPage = 
f5780 70 46 69 66 6f 2d 3e 70 4c 61 73 74 20 3d 20 70  pFifo->pLast = p
f5790 50 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d  Page->pNext;.  }
f57a0 0a 20 20 70 50 61 67 65 2d 3e 61 53 6c 6f 74 5b  .  pPage->aSlot[
f57b0 70 50 61 67 65 2d 3e 69 57 72 69 74 65 2b 2b 5d  pPage->iWrite++]
f57c0 20 3d 20 76 61 6c 3b 0a 20 20 70 46 69 66 6f 2d   = val;.  pFifo-
f57d0 3e 6e 45 6e 74 72 79 2b 2b 3b 0a 20 20 72 65 74  >nEntry++;.  ret
f57e0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
f57f0 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20  ../*.** Extract 
f5800 61 20 73 69 6e 67 6c 65 20 36 34 2d 62 69 74 20  a single 64-bit 
f5810 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 66 72  integer value fr
f5820 6f 6d 20 74 68 65 20 46 69 66 6f 2e 20 20 54 68  om the Fifo.  Th
f5830 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 65 78 74  e integer.** ext
f5840 72 61 63 74 65 64 20 69 73 20 74 68 65 20 6f 6e  racted is the on
f5850 65 20 6c 65 61 73 74 20 72 65 63 65 6e 74 6c 79  e least recently
f5860 20 69 6e 73 65 72 74 65 64 2e 20 20 49 66 20 74   inserted.  If t
f5870 68 65 20 46 69 66 6f 20 69 73 20 65 6d 70 74 79  he Fifo is empty
f5880 0a 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 54  .** return SQLIT
f5890 45 5f 44 4f 4e 45 2e 0a 2a 2f 0a 53 51 4c 49 54  E_DONE..*/.SQLIT
f58a0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
f58b0 6c 69 74 65 33 56 64 62 65 46 69 66 6f 50 6f 70  lite3VdbeFifoPop
f58c0 28 46 69 66 6f 20 2a 70 46 69 66 6f 2c 20 69 36  (Fifo *pFifo, i6
f58d0 34 20 2a 70 56 61 6c 29 7b 0a 20 20 46 69 66 6f  4 *pVal){.  Fifo
f58e0 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 69  Page *pPage;.  i
f58f0 66 28 20 70 46 69 66 6f 2d 3e 6e 45 6e 74 72 79  f( pFifo->nEntry
f5900 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
f5910 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20  n SQLITE_DONE;. 
f5920 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 46 69   }.  assert( pFi
f5930 66 6f 2d 3e 6e 45 6e 74 72 79 3e 30 20 29 3b 0a  fo->nEntry>0 );.
f5940 20 20 70 50 61 67 65 20 3d 20 70 46 69 66 6f 2d    pPage = pFifo-
f5950 3e 70 46 69 72 73 74 3b 0a 20 20 61 73 73 65 72  >pFirst;.  asser
f5960 74 28 20 70 50 61 67 65 21 3d 30 20 29 3b 0a 20  t( pPage!=0 );. 
f5970 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
f5980 69 57 72 69 74 65 3e 70 50 61 67 65 2d 3e 69 52  iWrite>pPage->iR
f5990 65 61 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ead );.  assert(
f59a0 20 70 50 61 67 65 2d 3e 69 57 72 69 74 65 3c 3d   pPage->iWrite<=
f59b0 70 50 61 67 65 2d 3e 6e 53 6c 6f 74 20 29 3b 0a  pPage->nSlot );.
f59c0 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
f59d0 3e 69 52 65 61 64 3c 70 50 61 67 65 2d 3e 6e 53  >iRead<pPage->nS
f59e0 6c 6f 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28  lot );.  assert(
f59f0 20 70 50 61 67 65 2d 3e 69 52 65 61 64 3e 3d 30   pPage->iRead>=0
f5a00 20 29 3b 0a 20 20 2a 70 56 61 6c 20 3d 20 70 50   );.  *pVal = pP
f5a10 61 67 65 2d 3e 61 53 6c 6f 74 5b 70 50 61 67 65  age->aSlot[pPage
f5a20 2d 3e 69 52 65 61 64 2b 2b 5d 3b 0a 20 20 70 46  ->iRead++];.  pF
f5a30 69 66 6f 2d 3e 6e 45 6e 74 72 79 2d 2d 3b 0a 20  ifo->nEntry--;. 
f5a40 20 69 66 28 20 70 50 61 67 65 2d 3e 69 52 65 61   if( pPage->iRea
f5a50 64 3e 3d 70 50 61 67 65 2d 3e 69 57 72 69 74 65  d>=pPage->iWrite
f5a60 20 29 7b 0a 20 20 20 20 70 46 69 66 6f 2d 3e 70   ){.    pFifo->p
f5a70 46 69 72 73 74 20 3d 20 70 50 61 67 65 2d 3e 70  First = pPage->p
f5a80 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65  Next;.    sqlite
f5a90 46 72 65 65 28 70 50 61 67 65 29 3b 0a 20 20 20  Free(pPage);.   
f5aa0 20 69 66 28 20 70 46 69 66 6f 2d 3e 6e 45 6e 74   if( pFifo->nEnt
f5ab0 72 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61  ry==0 ){.      a
f5ac0 73 73 65 72 74 28 20 70 46 69 66 6f 2d 3e 70 4c  ssert( pFifo->pL
f5ad0 61 73 74 3d 3d 70 50 61 67 65 20 29 3b 0a 20 20  ast==pPage );.  
f5ae0 20 20 20 20 70 46 69 66 6f 2d 3e 70 4c 61 73 74      pFifo->pLast
f5af0 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b   = 0;.    }else{
f5b00 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
f5b10 46 69 66 6f 2d 3e 70 46 69 72 73 74 21 3d 30 20  Fifo->pFirst!=0 
f5b20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
f5b30 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46  {.    assert( pF
f5b40 69 66 6f 2d 3e 6e 45 6e 74 72 79 3e 30 20 29 3b  ifo->nEntry>0 );
f5b50 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
f5b60 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
f5b70 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20 69 6e 66  * Delete all inf
f5b80 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20  ormation from a 
f5b90 46 69 66 6f 20 6f 62 6a 65 63 74 2e 20 20 20 46  Fifo object.   F
f5ba0 72 65 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 68  ree all memory h
f5bb0 65 6c 64 0a 2a 2a 20 62 79 20 74 68 65 20 46 69  eld.** by the Fi
f5bc0 66 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  fo..*/.SQLITE_PR
f5bd0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
f5be0 65 33 56 64 62 65 46 69 66 6f 43 6c 65 61 72 28  e3VdbeFifoClear(
f5bf0 46 69 66 6f 20 2a 70 46 69 66 6f 29 7b 0a 20 20  Fifo *pFifo){.  
f5c00 46 69 66 6f 50 61 67 65 20 2a 70 50 61 67 65 2c  FifoPage *pPage,
f5c10 20 2a 70 4e 65 78 74 50 61 67 65 3b 0a 20 20 66   *pNextPage;.  f
f5c20 6f 72 28 70 50 61 67 65 3d 70 46 69 66 6f 2d 3e  or(pPage=pFifo->
f5c30 70 46 69 72 73 74 3b 20 70 50 61 67 65 3b 20 70  pFirst; pPage; p
f5c40 50 61 67 65 3d 70 4e 65 78 74 50 61 67 65 29 7b  Page=pNextPage){
f5c50 0a 20 20 20 20 70 4e 65 78 74 50 61 67 65 20 3d  .    pNextPage =
f5c60 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 20   pPage->pNext;. 
f5c70 20 20 20 73 71 6c 69 74 65 46 72 65 65 28 70 50     sqliteFree(pP
f5c80 61 67 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  age);.  }.  sqli
f5c90 74 65 33 56 64 62 65 46 69 66 6f 49 6e 69 74 28  te3VdbeFifoInit(
f5ca0 70 46 69 66 6f 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  pFifo);.}../****
f5cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
f5cc0 66 20 76 64 62 65 66 69 66 6f 2e 63 20 2a 2a 2a  f vdbefifo.c ***
f5cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
f5d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
f5d10 20 66 69 6c 65 20 76 64 62 65 6d 65 6d 2e 63 20   file vdbemem.c 
f5d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
f5d50 20 32 30 30 34 20 4d 61 79 20 32 36 0a 2a 2a 0a   2004 May 26.**.
f5d60 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
f5d70 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
f5d80 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
f5d90 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
f5da0 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
f5db0 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
f5dc0 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
f5dd0 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
f5de0 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
f5df0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
f5e00 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
f5e10 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
f5e20 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
f5e30 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
f5e40 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
f5e50 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
f5e60 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
f5e70 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
f5e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
f5ec0 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
f5ed0 61 69 6e 73 20 63 6f 64 65 20 75 73 65 20 74 6f  ains code use to
f5ee0 20 6d 61 6e 69 70 75 6c 61 74 65 20 22 4d 65 6d   manipulate "Mem
f5ef0 22 20 73 74 72 75 63 74 75 72 65 2e 20 20 41 20  " structure.  A 
f5f00 22 4d 65 6d 22 0a 2a 2a 20 73 74 6f 72 65 73 20  "Mem".** stores 
f5f10 61 20 73 69 6e 67 6c 65 20 76 61 6c 75 65 20 69  a single value i
f5f20 6e 20 74 68 65 20 56 44 42 45 2e 20 20 4d 65 6d  n the VDBE.  Mem
f5f30 20 69 73 20 61 6e 20 6f 70 61 71 75 65 20 73 74   is an opaque st
f5f40 72 75 63 74 75 72 65 20 76 69 73 69 62 6c 65 0a  ructure visible.
f5f50 2a 2a 20 6f 6e 6c 79 20 77 69 74 68 69 6e 20 74  ** only within t
f5f60 68 65 20 56 44 42 45 2e 20 20 49 6e 74 65 72 66  he VDBE.  Interf
f5f70 61 63 65 20 72 6f 75 74 69 6e 65 73 20 72 65 66  ace routines ref
f5f80 65 72 20 74 6f 20 61 20 4d 65 6d 20 75 73 69 6e  er to a Mem usin
f5f90 67 20 74 68 65 0a 2a 2a 20 6e 61 6d 65 20 73 71  g the.** name sq
f5fa0 6c 69 74 65 5f 76 61 6c 75 65 0a 2a 2f 0a 0a 2f  lite_value.*/../
f5fb0 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65  *.** Call sqlite
f5fc0 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c  3VdbeMemExpandBl
f5fd0 6f 62 28 29 20 6f 6e 20 74 68 65 20 73 75 70 70  ob() on the supp
f5fe0 6c 69 65 64 20 76 61 6c 75 65 20 28 74 79 70 65  lied value (type
f5ff0 20 4d 65 6d 2a 29 0a 2a 2a 20 50 20 69 66 20 72   Mem*).** P if r
f6000 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 23 64 65 66  equired..*/.#def
f6010 69 6e 65 20 65 78 70 61 6e 64 42 6c 6f 62 28 50  ine expandBlob(P
f6020 29 20 28 28 28 50 29 2d 3e 66 6c 61 67 73 26 4d  ) (((P)->flags&M
f6030 45 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69 74 65 33  EM_Zero)?sqlite3
f6040 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f  VdbeMemExpandBlo
f6050 62 28 50 29 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20 49  b(P):0)../*.** I
f6060 66 20 70 4d 65 6d 20 69 73 20 61 6e 20 6f 62 6a  f pMem is an obj
f6070 65 63 74 20 77 69 74 68 20 61 20 76 61 6c 69 64  ect with a valid
f6080 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e   string represen
f6090 74 61 74 69 6f 6e 2c 20 74 68 69 73 20 72 6f 75  tation, this rou
f60a0 74 69 6e 65 0a 2a 2a 20 65 6e 73 75 72 65 73 20  tine.** ensures 
f60b0 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 65 6e 63  the internal enc
f60c0 6f 64 69 6e 67 20 66 6f 72 20 74 68 65 20 73 74  oding for the st
f60d0 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74  ring representat
f60e0 69 6f 6e 20 69 73 0a 2a 2a 20 27 64 65 73 69 72  ion is.** 'desir
f60f0 65 64 45 6e 63 27 2c 20 6f 6e 65 20 6f 66 20 53  edEnc', one of S
f6100 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49  QLITE_UTF8, SQLI
f6110 54 45 5f 55 54 46 31 36 4c 45 20 6f 72 20 53 51  TE_UTF16LE or SQ
f6120 4c 49 54 45 5f 55 54 46 31 36 42 45 2e 0a 2a 2a  LITE_UTF16BE..**
f6130 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73 20 6e  .** If pMem is n
f6140 6f 74 20 61 20 73 74 72 69 6e 67 20 6f 62 6a 65  ot a string obje
f6150 63 74 2c 20 6f 72 20 74 68 65 20 65 6e 63 6f 64  ct, or the encod
f6160 69 6e 67 20 6f 66 20 74 68 65 20 73 74 72 69 6e  ing of the strin
f6170 67 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74  g.** representat
f6180 69 6f 6e 20 69 73 20 61 6c 72 65 61 64 79 20 73  ion is already s
f6190 74 6f 72 65 64 20 75 73 69 6e 67 20 74 68 65 20  tored using the 
f61a0 72 65 71 75 65 73 74 65 64 20 65 6e 63 6f 64 69  requested encodi
f61b0 6e 67 2c 20 74 68 65 6e 20 74 68 69 73 0a 2a 2a  ng, then this.**
f61c0 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f   routine is a no
f61d0 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54  -op..**.** SQLIT
f61e0 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
f61f0 20 69 66 20 74 68 65 20 63 6f 6e 76 65 72 73 69   if the conversi
f6200 6f 6e 20 69 73 20 73 75 63 63 65 73 73 66 75 6c  on is successful
f6210 20 28 6f 72 20 6e 6f 74 20 72 65 71 75 69 72 65   (or not require
f6220 64 29 2e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f  d)..** SQLITE_NO
f6230 4d 45 4d 20 6d 61 79 20 62 65 20 72 65 74 75 72  MEM may be retur
f6240 6e 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28  ned if a malloc(
f6250 29 20 66 61 69 6c 73 20 64 75 72 69 6e 67 20 63  ) fails during c
f6260 6f 6e 76 65 72 73 69 6f 6e 0a 2a 2a 20 62 65 74  onversion.** bet
f6270 77 65 65 6e 20 66 6f 72 6d 61 74 73 2e 0a 2a 2f  ween formats..*/
f6280 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
f6290 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43  int sqlite3VdbeC
f62a0 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 4d 65  hangeEncoding(Me
f62b0 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 64 65 73  m *pMem, int des
f62c0 69 72 65 64 45 6e 63 29 7b 0a 20 20 69 6e 74 20  iredEnc){.  int 
f62d0 72 63 3b 0a 20 20 69 66 28 20 21 28 70 4d 65 6d  rc;.  if( !(pMem
f62e0 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 29  ->flags&MEM_Str)
f62f0 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 64   || pMem->enc==d
f6300 65 73 69 72 65 64 45 6e 63 20 29 7b 0a 20 20 20  esiredEnc ){.   
f6310 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
f6320 4b 3b 0a 20 20 7d 0a 23 69 66 64 65 66 20 53 51  K;.  }.#ifdef SQ
f6330 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a  LITE_OMIT_UTF16.
f6340 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
f6350 45 52 52 4f 52 3b 0a 23 65 6c 73 65 0a 0a 0a 20  ERROR;.#else... 
f6360 20 2f 2a 20 4d 65 6d 54 72 61 6e 73 6c 61 74 65   /* MemTranslate
f6370 28 29 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51  () may return SQ
f6380 4c 49 54 45 5f 4f 4b 20 6f 72 20 53 51 4c 49 54  LITE_OK or SQLIT
f6390 45 5f 4e 4f 4d 45 4d 2e 20 49 66 20 4e 4f 4d 45  E_NOMEM. If NOME
f63a0 4d 20 69 73 20 72 65 74 75 72 6e 65 64 2c 0a 20  M is returned,. 
f63b0 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 65 6e 63   ** then the enc
f63c0 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 76 61 6c  oding of the val
f63d0 75 65 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20  ue may not have 
f63e0 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a 20 20  changed..  */.  
f63f0 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
f6400 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 70 4d 65  MemTranslate(pMe
f6410 6d 2c 20 64 65 73 69 72 65 64 45 6e 63 29 3b 0a  m, desiredEnc);.
f6420 20 20 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c    assert(rc==SQL
f6430 49 54 45 5f 4f 4b 20 20 20 20 7c 7c 20 72 63 3d  ITE_OK    || rc=
f6440 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a  =SQLITE_NOMEM);.
f6450 20 20 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c    assert(rc==SQL
f6460 49 54 45 5f 4f 4b 20 20 20 20 7c 7c 20 70 4d 65  ITE_OK    || pMe
f6470 6d 2d 3e 65 6e 63 21 3d 64 65 73 69 72 65 64 45  m->enc!=desiredE
f6480 6e 63 29 3b 0a 20 20 61 73 73 65 72 74 28 72 63  nc);.  assert(rc
f6490 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c  ==SQLITE_NOMEM |
f64a0 7c 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 64 65 73  | pMem->enc==des
f64b0 69 72 65 64 45 6e 63 29 3b 0a 20 20 72 65 74 75  iredEnc);.  retu
f64c0 72 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a  rn rc;.#endif.}.
f64d0 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 74 68 65 20  ./*.** Make the 
f64e0 67 69 76 65 6e 20 4d 65 6d 20 6f 62 6a 65 63 74  given Mem object
f64f0 20 4d 45 4d 5f 44 79 6e 2e 0a 2a 2a 0a 2a 2a 20   MEM_Dyn..**.** 
f6500 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
f6510 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 53   on success or S
f6520 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 6d  QLITE_NOMEM if m
f6530 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a  alloc fails..*/.
f6540 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
f6550 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
f6560 6d 44 79 6e 61 6d 69 63 69 66 79 28 4d 65 6d 20  mDynamicify(Mem 
f6570 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 6e 3b  *pMem){.  int n;
f6580 0a 20 20 75 38 20 2a 7a 3b 0a 20 20 65 78 70 61  .  u8 *z;.  expa
f6590 6e 64 42 6c 6f 62 28 70 4d 65 6d 29 3b 0a 20 20  ndBlob(pMem);.  
f65a0 69 66 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73  if( (pMem->flags
f65b0 20 26 20 28 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45   & (MEM_Ephem|ME
f65c0 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 53 68 6f  M_Static|MEM_Sho
f65d0 72 74 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72  rt))==0 ){.    r
f65e0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
f65f0 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 28  .  }.  assert( (
f6600 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pMem->flags & ME
f6610 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a 20 20 6e  M_Dyn)==0 );.  n
f6620 20 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 61 73   = pMem->n;.  as
f6630 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67  sert( pMem->flag
f6640 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d  s & (MEM_Str|MEM
f6650 5f 42 6c 6f 62 29 20 29 3b 0a 20 20 7a 20 3d 20  _Blob) );.  z = 
f6660 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 52 61 77 28  sqliteMallocRaw(
f6670 20 6e 2b 32 20 29 3b 0a 20 20 69 66 28 20 7a 3d   n+2 );.  if( z=
f6680 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
f6690 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
f66a0 20 7d 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73   }.  pMem->flags
f66b0 20 7c 3d 20 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f   |= MEM_Dyn|MEM_
f66c0 54 65 72 6d 3b 0a 20 20 70 4d 65 6d 2d 3e 78 44  Term;.  pMem->xD
f66d0 65 6c 20 3d 20 30 3b 0a 20 20 6d 65 6d 63 70 79  el = 0;.  memcpy
f66e0 28 7a 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 6e 20 29  (z, pMem->z, n )
f66f0 3b 0a 20 20 7a 5b 6e 5d 20 3d 20 30 3b 0a 20 20  ;.  z[n] = 0;.  
f6700 7a 5b 6e 2b 31 5d 20 3d 20 30 3b 0a 20 20 70 4d  z[n+1] = 0;.  pM
f6710 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 2a 29 7a  em->z = (char*)z
f6720 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  ;.  pMem->flags 
f6730 26 3d 20 7e 28 4d 45 4d 5f 45 70 68 65 6d 7c 4d  &= ~(MEM_Ephem|M
f6740 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 53 68  EM_Static|MEM_Sh
f6750 6f 72 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  ort);.  return S
f6760 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
f6770 2a 2a 20 49 66 20 74 68 65 20 67 69 76 65 6e 20  ** If the given 
f6780 4d 65 6d 2a 20 68 61 73 20 61 20 7a 65 72 6f 2d  Mem* has a zero-
f6790 66 69 6c 6c 65 64 20 74 61 69 6c 2c 20 74 75 72  filled tail, tur
f67a0 6e 20 69 74 20 69 6e 74 6f 20 61 6e 20 6f 72 64  n it into an ord
f67b0 69 6e 61 72 79 0a 2a 2a 20 62 6c 6f 62 20 73 74  inary.** blob st
f67c0 6f 72 65 64 20 69 6e 20 64 79 6e 61 6d 69 63 61  ored in dynamica
f67d0 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 70  lly allocated sp
f67e0 61 63 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ace..*/.#ifndef 
f67f0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52  SQLITE_OMIT_INCR
f6800 42 4c 4f 42 0a 53 51 4c 49 54 45 5f 50 52 49 56  BLOB.SQLITE_PRIV
f6810 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
f6820 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62  dbeMemExpandBlob
f6830 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69  (Mem *pMem){.  i
f6840 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  f( pMem->flags &
f6850 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20   MEM_Zero ){.   
f6860 20 63 68 61 72 20 2a 70 4e 65 77 3b 0a 20 20 20   char *pNew;.   
f6870 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 20 20   int nByte;.    
f6880 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66  assert( (pMem->f
f6890 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 29  lags & MEM_Blob)
f68a0 21 3d 30 20 29 3b 0a 20 20 20 20 6e 42 79 74 65  !=0 );.    nByte
f68b0 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2b 20 70 4d 65   = pMem->n + pMe
f68c0 6d 2d 3e 75 2e 69 3b 0a 20 20 20 20 69 66 28 20  m->u.i;.    if( 
f68d0 6e 42 79 74 65 3c 3d 30 20 29 20 6e 42 79 74 65  nByte<=0 ) nByte
f68e0 20 3d 20 31 3b 0a 20 20 20 20 70 4e 65 77 20 3d   = 1;.    pNew =
f68f0 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 6e 42   sqliteMalloc(nB
f6900 79 74 65 29 3b 0a 20 20 20 20 69 66 28 20 70 4e  yte);.    if( pN
f6910 65 77 3d 3d 30 20 29 7b 20 0a 20 20 20 20 20 20  ew==0 ){ .      
f6920 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
f6930 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d  MEM;.    }.    m
f6940 65 6d 63 70 79 28 70 4e 65 77 2c 20 70 4d 65 6d  emcpy(pNew, pMem
f6950 2d 3e 7a 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20  ->z, pMem->n);. 
f6960 20 20 20 6d 65 6d 73 65 74 28 26 70 4e 65 77 5b     memset(&pNew[
f6970 70 4d 65 6d 2d 3e 6e 5d 2c 20 30 2c 20 70 4d 65  pMem->n], 0, pMe
f6980 6d 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 73 71 6c  m->u.i);.    sql
f6990 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
f69a0 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d  se(pMem);.    pM
f69b0 65 6d 2d 3e 7a 20 3d 20 70 4e 65 77 3b 0a 20 20  em->z = pNew;.  
f69c0 20 20 70 4d 65 6d 2d 3e 6e 20 2b 3d 20 70 4d 65    pMem->n += pMe
f69d0 6d 2d 3e 75 2e 69 3b 0a 20 20 20 20 70 4d 65 6d  m->u.i;.    pMem
f69e0 2d 3e 75 2e 69 20 3d 20 30 3b 0a 20 20 20 20 70  ->u.i = 0;.    p
f69f0 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28  Mem->flags &= ~(
f6a00 4d 45 4d 5f 5a 65 72 6f 7c 4d 45 4d 5f 53 74 61  MEM_Zero|MEM_Sta
f6a10 74 69 63 7c 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45  tic|MEM_Ephem|ME
f6a20 4d 5f 53 68 6f 72 74 7c 4d 45 4d 5f 54 65 72 6d  M_Short|MEM_Term
f6a30 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  );.    pMem->fla
f6a40 67 73 20 7c 3d 20 4d 45 4d 5f 44 79 6e 3b 0a 20  gs |= MEM_Dyn;. 
f6a50 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
f6a60 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a  TE_OK;.}.#endif.
f6a70 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 74 68 65  ../*.** Make the
f6a80 20 67 69 76 65 6e 20 4d 65 6d 20 6f 62 6a 65 63   given Mem objec
f6a90 74 20 65 69 74 68 65 72 20 4d 45 4d 5f 53 68 6f  t either MEM_Sho
f6aa0 72 74 20 6f 72 20 4d 45 4d 5f 44 79 6e 20 73 6f  rt or MEM_Dyn so
f6ab0 20 74 68 61 74 20 62 79 74 65 73 0a 2a 2a 20 6f   that bytes.** o
f6ac0 66 20 74 68 65 20 4d 65 6d 2e 7a 5b 5d 20 61 72  f the Mem.z[] ar
f6ad0 72 61 79 20 63 61 6e 20 62 65 20 6d 6f 64 69 66  ray can be modif
f6ae0 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  ied..**.** Retur
f6af0 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73  n SQLITE_OK on s
f6b00 75 63 63 65 73 73 20 6f 72 20 53 51 4c 49 54 45  uccess or SQLITE
f6b10 5f 4e 4f 4d 45 4d 20 69 66 20 6d 61 6c 6c 6f 63  _NOMEM if malloc
f6b20 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54   fails..*/.SQLIT
f6b30 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
f6b40 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65  lite3VdbeMemMake
f6b50 57 72 69 74 65 61 62 6c 65 28 4d 65 6d 20 2a 70  Writeable(Mem *p
f6b60 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20  Mem){.  int n;. 
f6b70 20 75 38 20 2a 7a 3b 0a 20 20 65 78 70 61 6e 64   u8 *z;.  expand
f6b80 42 6c 6f 62 28 70 4d 65 6d 29 3b 0a 20 20 69 66  Blob(pMem);.  if
f6b90 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  ( (pMem->flags &
f6ba0 20 28 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f   (MEM_Ephem|MEM_
f6bb0 53 74 61 74 69 63 29 29 3d 3d 30 20 29 7b 0a 20  Static))==0 ){. 
f6bc0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
f6bd0 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  _OK;.  }.  asser
f6be0 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  t( (pMem->flags 
f6bf0 26 20 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b  & MEM_Dyn)==0 );
f6c00 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d  .  assert( pMem-
f6c10 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74  >flags & (MEM_St
f6c20 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 3b 0a 20  r|MEM_Blob) );. 
f6c30 20 69 66 28 20 28 6e 20 3d 20 70 4d 65 6d 2d 3e   if( (n = pMem->
f6c40 6e 29 2b 32 3c 73 69 7a 65 6f 66 28 70 4d 65 6d  n)+2<sizeof(pMem
f6c50 2d 3e 7a 53 68 6f 72 74 29 20 29 7b 0a 20 20 20  ->zShort) ){.   
f6c60 20 7a 20 3d 20 28 75 38 2a 29 70 4d 65 6d 2d 3e   z = (u8*)pMem->
f6c70 7a 53 68 6f 72 74 3b 0a 20 20 20 20 70 4d 65 6d  zShort;.    pMem
f6c80 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53  ->flags |= MEM_S
f6c90 68 6f 72 74 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20  hort|MEM_Term;. 
f6ca0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20   }else{.    z = 
f6cb0 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 52 61 77 28  sqliteMallocRaw(
f6cc0 20 6e 2b 32 20 29 3b 0a 20 20 20 20 69 66 28 20   n+2 );.    if( 
f6cd0 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65  z==0 ){.      re
f6ce0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
f6cf0 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65  M;.    }.    pMe
f6d00 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f  m->flags |= MEM_
f6d10 44 79 6e 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20  Dyn|MEM_Term;.  
f6d20 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 30    pMem->xDel = 0
f6d30 3b 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 7a  ;.  }.  memcpy(z
f6d40 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 6e 20 29 3b 0a  , pMem->z, n );.
f6d50 20 20 7a 5b 6e 5d 20 3d 20 30 3b 0a 20 20 7a 5b    z[n] = 0;.  z[
f6d60 6e 2b 31 5d 20 3d 20 30 3b 0a 20 20 70 4d 65 6d  n+1] = 0;.  pMem
f6d70 2d 3e 7a 20 3d 20 28 63 68 61 72 2a 29 7a 3b 0a  ->z = (char*)z;.
f6d80 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 3d    pMem->flags &=
f6d90 20 7e 28 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d   ~(MEM_Ephem|MEM
f6da0 5f 53 74 61 74 69 63 29 3b 0a 20 20 61 73 73 65  _Static);.  asse
f6db0 72 74 28 30 3d 3d 28 31 26 28 69 6e 74 29 70 4d  rt(0==(1&(int)pM
f6dc0 65 6d 2d 3e 7a 29 29 3b 0a 20 20 72 65 74 75 72  em->z));.  retur
f6dd0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
f6de0 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20  /*.** Make sure 
f6df0 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 20 69 73  the given Mem is
f6e00 20 5c 75 30 30 30 30 20 74 65 72 6d 69 6e 61 74   \u0000 terminat
f6e10 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
f6e20 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
f6e30 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69  3VdbeMemNulTermi
f6e40 6e 61 74 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b  nate(Mem *pMem){
f6e50 0a 20 20 69 66 28 20 28 70 4d 65 6d 2d 3e 66 6c  .  if( (pMem->fl
f6e60 61 67 73 20 26 20 4d 45 4d 5f 54 65 72 6d 29 21  ags & MEM_Term)!
f6e70 3d 30 20 7c 7c 20 28 70 4d 65 6d 2d 3e 66 6c 61  =0 || (pMem->fla
f6e80 67 73 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d 30  gs & MEM_Str)==0
f6e90 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
f6ea0 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 2f 2a 20 4e  QLITE_OK;   /* N
f6eb0 6f 74 68 69 6e 67 20 74 6f 20 64 6f 20 2a 2f 0a  othing to do */.
f6ec0 20 20 7d 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e    }.  if( pMem->
f6ed0 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 61  flags & (MEM_Sta
f6ee0 74 69 63 7c 4d 45 4d 5f 45 70 68 65 6d 29 20 29  tic|MEM_Ephem) )
f6ef0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c  {.    return sql
f6f00 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57  ite3VdbeMemMakeW
f6f10 72 69 74 65 61 62 6c 65 28 70 4d 65 6d 29 3b 0a  riteable(pMem);.
f6f20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61    }else{.    cha
f6f30 72 20 2a 7a 3b 20 0a 20 20 20 20 73 71 6c 69 74  r *z; .    sqlit
f6f40 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42  e3VdbeMemExpandB
f6f50 6c 6f 62 28 70 4d 65 6d 29 3b 0a 20 20 20 20 7a  lob(pMem);.    z
f6f60 20 3d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28   = sqliteMalloc(
f6f70 70 4d 65 6d 2d 3e 6e 2b 32 29 3b 0a 0a 20 20 20  pMem->n+2);..   
f6f80 20 69 66 28 20 21 7a 20 29 20 72 65 74 75 72 6e   if( !z ) return
f6f90 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
f6fa0 20 20 20 6d 65 6d 63 70 79 28 7a 2c 20 70 4d 65     memcpy(z, pMe
f6fb0 6d 2d 3e 7a 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a  m->z, pMem->n);.
f6fc0 20 20 20 20 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d      z[pMem->n] =
f6fd0 20 30 3b 0a 20 20 20 20 7a 5b 70 4d 65 6d 2d 3e   0;.    z[pMem->
f6fe0 6e 2b 31 5d 20 3d 20 30 3b 0a 20 20 20 20 69 66  n+1] = 0;.    if
f6ff0 28 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 29 7b 0a  ( pMem->xDel ){.
f7000 20 20 20 20 20 20 70 4d 65 6d 2d 3e 78 44 65 6c        pMem->xDel
f7010 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 7d  (pMem->z);.    }
f7020 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
f7030 74 65 46 72 65 65 28 70 4d 65 6d 2d 3e 7a 29 3b  teFree(pMem->z);
f7040 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d  .    }.    pMem-
f7050 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20 70  >xDel = 0;.    p
f7060 4d 65 6d 2d 3e 7a 20 3d 20 7a 3b 0a 20 20 20 20  Mem->z = z;.    
f7070 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d  pMem->flags |= M
f7080 45 4d 5f 54 65 72 6d 3b 0a 20 20 7d 0a 20 20 72  EM_Term;.  }.  r
f7090 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
f70a0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 4d 45  .}../*.** Add ME
f70b0 4d 5f 53 74 72 20 74 6f 20 74 68 65 20 73 65 74  M_Str to the set
f70c0 20 6f 66 20 72 65 70 72 65 73 65 6e 74 61 74 69   of representati
f70d0 6f 6e 73 20 66 6f 72 20 74 68 65 20 67 69 76 65  ons for the give
f70e0 6e 20 4d 65 6d 2e 20 20 4e 75 6d 62 65 72 73 0a  n Mem.  Numbers.
f70f0 2a 2a 20 61 72 65 20 63 6f 6e 76 65 72 74 65 64  ** are converted
f7100 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 73   using sqlite3_s
f7110 6e 70 72 69 6e 74 66 28 29 2e 20 20 43 6f 6e 76  nprintf().  Conv
f7120 65 72 74 69 6e 67 20 61 20 42 4c 4f 42 20 74 6f  erting a BLOB to
f7130 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 69 73 20   a string.** is 
f7140 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 45  a no-op..**.** E
f7150 78 69 73 74 69 6e 67 20 72 65 70 72 65 73 65 6e  xisting represen
f7160 74 61 74 69 6f 6e 73 20 4d 45 4d 5f 49 6e 74 20  tations MEM_Int 
f7170 61 6e 64 20 4d 45 4d 5f 52 65 61 6c 20 61 72 65  and MEM_Real are
f7180 20 2a 6e 6f 74 2a 20 69 6e 76 61 6c 69 64 61 74   *not* invalidat
f7190 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 4d 45 4d 5f  ed..**.** A MEM_
f71a0 4e 75 6c 6c 20 76 61 6c 75 65 20 77 69 6c 6c 20  Null value will 
f71b0 6e 65 76 65 72 20 62 65 20 70 61 73 73 65 64 20  never be passed 
f71c0 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
f71d0 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
f71e0 69 73 0a 2a 2a 20 75 73 65 64 20 66 6f 72 20 63  is.** used for c
f71f0 6f 6e 76 65 72 74 69 6e 67 20 76 61 6c 75 65 73  onverting values
f7200 20 74 6f 20 74 65 78 74 20 66 6f 72 20 72 65 74   to text for ret
f7210 75 72 6e 69 6e 67 20 74 6f 20 74 68 65 20 75 73  urning to the us
f7220 65 72 20 28 69 2e 65 2e 20 76 69 61 0a 2a 2a 20  er (i.e. via.** 
f7230 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
f7240 78 74 28 29 29 2c 20 6f 72 20 66 6f 72 20 65 6e  xt()), or for en
f7250 73 75 72 69 6e 67 20 74 68 61 74 20 76 61 6c 75  suring that valu
f7260 65 73 20 74 6f 20 62 65 20 75 73 65 64 20 61 73  es to be used as
f7270 20 62 74 72 65 65 0a 2a 2a 20 6b 65 79 73 20 61   btree.** keys a
f7280 72 65 20 73 74 72 69 6e 67 73 2e 20 49 6e 20 74  re strings. In t
f7290 68 65 20 66 6f 72 6d 65 72 20 63 61 73 65 20 61  he former case a
f72a0 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73   NULL pointer is
f72b0 20 72 65 74 75 72 6e 65 64 20 74 68 65 0a 2a 2a   returned the.**
f72c0 20 75 73 65 72 20 61 6e 64 20 74 68 65 20 6c 61   user and the la
f72d0 74 65 72 20 69 73 20 61 6e 20 69 6e 74 65 72 6e  ter is an intern
f72e0 61 6c 20 70 72 6f 67 72 61 6d 6d 69 6e 67 20 65  al programming e
f72f0 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rror..*/.SQLITE_
f7300 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
f7310 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 6e 67  te3VdbeMemString
f7320 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69  ify(Mem *pMem, i
f7330 6e 74 20 65 6e 63 29 7b 0a 20 20 69 6e 74 20 72  nt enc){.  int r
f7340 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
f7350 20 69 6e 74 20 66 67 20 3d 20 70 4d 65 6d 2d 3e   int fg = pMem->
f7360 66 6c 61 67 73 3b 0a 20 20 63 68 61 72 20 2a 7a  flags;.  char *z
f7370 20 3d 20 70 4d 65 6d 2d 3e 7a 53 68 6f 72 74 3b   = pMem->zShort;
f7380 0a 0a 20 20 61 73 73 65 72 74 28 20 21 28 66 67  ..  assert( !(fg
f7390 26 4d 45 4d 5f 5a 65 72 6f 29 20 29 3b 0a 20 20  &MEM_Zero) );.  
f73a0 61 73 73 65 72 74 28 20 21 28 66 67 26 28 4d 45  assert( !(fg&(ME
f73b0 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29  M_Str|MEM_Blob))
f73c0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 66 67   );.  assert( fg
f73d0 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65  &(MEM_Int|MEM_Re
f73e0 61 6c 29 20 29 3b 0a 0a 20 20 2f 2a 20 46 6f 72  al) );..  /* For
f73f0 20 61 20 52 65 61 6c 20 6f 72 20 49 6e 74 65 67   a Real or Integ
f7400 65 72 2c 20 75 73 65 20 73 71 6c 69 74 65 33 5f  er, use sqlite3_
f7410 73 6e 70 72 69 6e 74 66 28 29 20 74 6f 20 70 72  snprintf() to pr
f7420 6f 64 75 63 65 20 74 68 65 20 55 54 46 2d 38 0a  oduce the UTF-8.
f7430 20 20 2a 2a 20 73 74 72 69 6e 67 20 72 65 70 72    ** string repr
f7440 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  esentation of th
f7450 65 20 76 61 6c 75 65 2e 20 54 68 65 6e 2c 20 69  e value. Then, i
f7460 66 20 74 68 65 20 72 65 71 75 69 72 65 64 20 65  f the required e
f7470 6e 63 6f 64 69 6e 67 0a 20 20 2a 2a 20 69 73 20  ncoding.  ** is 
f7480 55 54 46 2d 31 36 6c 65 20 6f 72 20 55 54 46 2d  UTF-16le or UTF-
f7490 31 36 62 65 20 64 6f 20 61 20 74 72 61 6e 73 6c  16be do a transl
f74a0 61 74 69 6f 6e 2e 0a 20 20 2a 2a 20 0a 20 20 2a  ation..  ** .  *
f74b0 2a 20 46 49 58 20 4d 45 3a 20 49 74 20 77 6f 75  * FIX ME: It wou
f74c0 6c 64 20 62 65 20 62 65 74 74 65 72 20 69 66 20  ld be better if 
f74d0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
f74e0 28 29 20 63 6f 75 6c 64 20 64 6f 20 55 54 46 2d  () could do UTF-
f74f0 31 36 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 66  16..  */.  if( f
f7500 67 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20  g & MEM_Int ){. 
f7510 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
f7520 6e 74 66 28 4e 42 46 53 2c 20 7a 2c 20 22 25 6c  ntf(NBFS, z, "%l
f7530 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75 2e 69 29 3b  ld", pMem->u.i);
f7540 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
f7550 73 65 72 74 28 20 66 67 20 26 20 4d 45 4d 5f 52  sert( fg & MEM_R
f7560 65 61 6c 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  eal );.    sqlit
f7570 65 33 5f 73 6e 70 72 69 6e 74 66 28 4e 42 46 53  e3_snprintf(NBFS
f7580 2c 20 7a 2c 20 22 25 21 2e 31 35 67 22 2c 20 70  , z, "%!.15g", p
f7590 4d 65 6d 2d 3e 72 29 3b 0a 20 20 7d 0a 20 20 70  Mem->r);.  }.  p
f75a0 4d 65 6d 2d 3e 6e 20 3d 20 73 74 72 6c 65 6e 28  Mem->n = strlen(
f75b0 7a 29 3b 0a 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20  z);.  pMem->z = 
f75c0 7a 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d  z;.  pMem->enc =
f75d0 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20   SQLITE_UTF8;.  
f75e0 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d  pMem->flags |= M
f75f0 45 4d 5f 53 74 72 20 7c 20 4d 45 4d 5f 53 68 6f  EM_Str | MEM_Sho
f7600 72 74 20 7c 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20  rt | MEM_Term;. 
f7610 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
f7620 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c  geEncoding(pMem,
f7630 20 65 6e 63 29 3b 0a 20 20 72 65 74 75 72 6e 20   enc);.  return 
f7640 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 6d  rc;.}../*.** Mem
f7650 6f 72 79 20 63 65 6c 6c 20 70 4d 65 6d 20 63 6f  ory cell pMem co
f7660 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6e 74 65  ntains the conte
f7670 78 74 20 6f 66 20 61 6e 20 61 67 67 72 65 67 61  xt of an aggrega
f7680 74 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20  te function..** 
f7690 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6c  This routine cal
f76a0 6c 73 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 20  ls the finalize 
f76b0 6d 65 74 68 6f 64 20 66 6f 72 20 74 68 61 74 20  method for that 
f76c0 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 0a 2a  function.  The.*
f76d0 2a 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20  * result of the 
f76e0 61 67 67 72 65 67 61 74 65 20 69 73 20 73 74 6f  aggregate is sto
f76f0 72 65 64 20 62 61 63 6b 20 69 6e 74 6f 20 70 4d  red back into pM
f7700 65 6d 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  em..**.** Return
f7710 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 66   SQLITE_ERROR if
f7720 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 20 72   the finalizer r
f7730 65 70 6f 72 74 73 20 61 6e 20 65 72 72 6f 72 2e  eports an error.
f7740 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f    SQLITE_OK.** o
f7750 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c  therwise..*/.SQL
f7760 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
f7770 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 69  sqlite3VdbeMemFi
f7780 6e 61 6c 69 7a 65 28 4d 65 6d 20 2a 70 4d 65 6d  nalize(Mem *pMem
f7790 2c 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e 63  , FuncDef *pFunc
f77a0 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
f77b0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70  LITE_OK;.  if( p
f77c0 46 75 6e 63 20 26 26 20 70 46 75 6e 63 2d 3e 78  Func && pFunc->x
f77d0 46 69 6e 61 6c 69 7a 65 20 29 7b 0a 20 20 20 20  Finalize ){.    
f77e0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
f77f0 63 74 78 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ctx;.    assert(
f7800 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20   (pMem->flags & 
f7810 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20 7c 7c 20  MEM_Null)!=0 || 
f7820 70 46 75 6e 63 3d 3d 70 4d 65 6d 2d 3e 75 2e 70  pFunc==pMem->u.p
f7830 44 65 66 20 29 3b 0a 20 20 20 20 63 74 78 2e 73  Def );.    ctx.s
f7840 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c  .flags = MEM_Nul
f7850 6c 3b 0a 20 20 20 20 63 74 78 2e 73 2e 7a 20 3d  l;.    ctx.s.z =
f7860 20 70 4d 65 6d 2d 3e 7a 53 68 6f 72 74 3b 0a 20   pMem->zShort;. 
f7870 20 20 20 63 74 78 2e 70 4d 65 6d 20 3d 20 70 4d     ctx.pMem = pM
f7880 65 6d 3b 0a 20 20 20 20 63 74 78 2e 70 46 75 6e  em;.    ctx.pFun
f7890 63 20 3d 20 70 46 75 6e 63 3b 0a 20 20 20 20 63  c = pFunc;.    c
f78a0 74 78 2e 69 73 45 72 72 6f 72 20 3d 20 30 3b 0a  tx.isError = 0;.
f78b0 20 20 20 20 70 46 75 6e 63 2d 3e 78 46 69 6e 61      pFunc->xFina
f78c0 6c 69 7a 65 28 26 63 74 78 29 3b 0a 20 20 20 20  lize(&ctx);.    
f78d0 69 66 28 20 70 4d 65 6d 2d 3e 7a 20 26 26 20 70  if( pMem->z && p
f78e0 4d 65 6d 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e 7a 53  Mem->z!=pMem->zS
f78f0 68 6f 72 74 20 29 7b 0a 20 20 20 20 20 20 73 71  hort ){.      sq
f7900 6c 69 74 65 46 72 65 65 28 20 70 4d 65 6d 2d 3e  liteFree( pMem->
f7910 7a 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a  z );.    }.    *
f7920 70 4d 65 6d 20 3d 20 63 74 78 2e 73 3b 0a 20 20  pMem = ctx.s;.  
f7930 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67    if( pMem->flag
f7940 73 20 26 20 4d 45 4d 5f 53 68 6f 72 74 20 29 7b  s & MEM_Short ){
f7950 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d  .      pMem->z =
f7960 20 70 4d 65 6d 2d 3e 7a 53 68 6f 72 74 3b 0a 20   pMem->zShort;. 
f7970 20 20 20 7d 0a 20 20 20 20 69 66 28 20 63 74 78     }.    if( ctx
f7980 2e 69 73 45 72 72 6f 72 20 29 7b 0a 20 20 20 20  .isError ){.    
f7990 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
f79a0 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ROR;.    }.  }. 
f79b0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
f79c0 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 6e 79  *.** Release any
f79d0 20 6d 65 6d 6f 72 79 20 68 65 6c 64 20 62 79 20   memory held by 
f79e0 74 68 65 20 4d 65 6d 2e 20 54 68 69 73 20 6d 61  the Mem. This ma
f79f0 79 20 6c 65 61 76 65 20 74 68 65 20 4d 65 6d 20  y leave the Mem 
f7a00 69 6e 20 61 6e 0a 2a 2a 20 69 6e 63 6f 6e 73 69  in an.** inconsi
f7a10 73 74 65 6e 74 20 73 74 61 74 65 2c 20 66 6f 72  stent state, for
f7a20 20 65 78 61 6d 70 6c 65 20 77 69 74 68 20 28 4d   example with (M
f7a30 65 6d 2e 7a 3d 3d 30 29 20 61 6e 64 0a 2a 2a 20  em.z==0) and.** 
f7a40 28 4d 65 6d 2e 74 79 70 65 3d 3d 53 51 4c 49 54  (Mem.type==SQLIT
f7a50 45 5f 54 45 58 54 29 2e 0a 2a 2f 0a 53 51 4c 49  E_TEXT)..*/.SQLI
f7a60 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
f7a70 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
f7a80 6c 65 61 73 65 28 4d 65 6d 20 2a 70 29 7b 0a 20  lease(Mem *p){. 
f7a90 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20   if( p->flags & 
f7aa0 28 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 41 67 67  (MEM_Dyn|MEM_Agg
f7ab0 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e  ) ){.    if( p->
f7ac0 78 44 65 6c 20 29 7b 0a 20 20 20 20 20 20 69 66  xDel ){.      if
f7ad0 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  ( p->flags & MEM
f7ae0 5f 41 67 67 20 29 7b 0a 20 20 20 20 20 20 20 20  _Agg ){.        
f7af0 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 69  sqlite3VdbeMemFi
f7b00 6e 61 6c 69 7a 65 28 70 2c 20 70 2d 3e 75 2e 70  nalize(p, p->u.p
f7b10 44 65 66 29 3b 0a 20 20 20 20 20 20 20 20 61 73  Def);.        as
f7b20 73 65 72 74 28 20 28 70 2d 3e 66 6c 61 67 73 20  sert( (p->flags 
f7b30 26 20 4d 45 4d 5f 41 67 67 29 3d 3d 30 20 29 3b  & MEM_Agg)==0 );
f7b40 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
f7b50 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70  VdbeMemRelease(p
f7b60 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
f7b70 20 20 20 20 20 20 20 20 70 2d 3e 78 44 65 6c 28          p->xDel(
f7b80 28 76 6f 69 64 20 2a 29 70 2d 3e 7a 29 3b 0a 20  (void *)p->z);. 
f7b90 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
f7ba0 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 46 72  {.      sqliteFr
f7bb0 65 65 28 70 2d 3e 7a 29 3b 0a 20 20 20 20 7d 0a  ee(p->z);.    }.
f7bc0 20 20 20 20 70 2d 3e 7a 20 3d 20 30 3b 0a 20 20      p->z = 0;.  
f7bd0 20 20 70 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20    p->xDel = 0;. 
f7be0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75   }.}../*.** Retu
f7bf0 72 6e 20 73 6f 6d 65 20 6b 69 6e 64 20 6f 66 20  rn some kind of 
f7c00 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 77 68  integer value wh
f7c10 69 63 68 20 69 73 20 74 68 65 20 62 65 73 74 20  ich is the best 
f7c20 77 65 20 63 61 6e 20 64 6f 0a 2a 2a 20 61 74 20  we can do.** at 
f7c30 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65  representing the
f7c40 20 76 61 6c 75 65 20 74 68 61 74 20 2a 70 4d 65   value that *pMe
f7c50 6d 20 64 65 73 63 72 69 62 65 73 20 61 73 20 61  m describes as a
f7c60 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 49 66  n integer..** If
f7c70 20 70 4d 65 6d 20 69 73 20 61 6e 20 69 6e 74 65   pMem is an inte
f7c80 67 65 72 2c 20 74 68 65 6e 20 74 68 65 20 76 61  ger, then the va
f7c90 6c 75 65 20 69 73 20 65 78 61 63 74 2e 20 20 49  lue is exact.  I
f7ca0 66 20 70 4d 65 6d 20 69 73 0a 2a 2a 20 61 20 66  f pMem is.** a f
f7cb0 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20 74 68  loating-point th
f7cc0 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 74  en the value ret
f7cd0 75 72 6e 65 64 20 69 73 20 74 68 65 20 69 6e 74  urned is the int
f7ce0 65 67 65 72 20 70 61 72 74 2e 0a 2a 2a 20 49 66  eger part..** If
f7cf0 20 70 4d 65 6d 20 69 73 20 61 20 73 74 72 69 6e   pMem is a strin
f7d00 67 20 6f 72 20 62 6c 6f 62 2c 20 74 68 65 6e 20  g or blob, then 
f7d10 77 65 20 6d 61 6b 65 20 61 6e 20 61 74 74 65 6d  we make an attem
f7d20 70 74 20 74 6f 20 63 6f 6e 76 65 72 74 0a 2a 2a  pt to convert.**
f7d30 20 69 74 20 69 6e 74 6f 20 61 20 69 6e 74 65 67   it into a integ
f7d40 65 72 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68  er and return th
f7d50 61 74 2e 20 20 49 66 20 70 4d 65 6d 20 69 73 20  at.  If pMem is 
f7d60 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 30 2e 0a  NULL, return 0..
f7d70 2a 2a 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73  **.** If pMem is
f7d80 20 61 20 73 74 72 69 6e 67 2c 20 69 74 73 20 65   a string, its e
f7d90 6e 63 6f 64 69 6e 67 20 6d 69 67 68 74 20 62 65  ncoding might be
f7da0 20 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c   changed..*/.SQL
f7db0 49 54 45 5f 50 52 49 56 41 54 45 20 69 36 34 20  ITE_PRIVATE i64 
f7dc0 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61  sqlite3VdbeIntVa
f7dd0 6c 75 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a  lue(Mem *pMem){.
f7de0 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 70 4d    int flags = pM
f7df0 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 28  em->flags;.  if(
f7e00 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74   flags & MEM_Int
f7e10 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70   ){.    return p
f7e20 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 7d 65 6c 73  Mem->u.i;.  }els
f7e30 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45  e if( flags & ME
f7e40 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 65  M_Real ){.    re
f7e50 74 75 72 6e 20 28 69 36 34 29 70 4d 65 6d 2d 3e  turn (i64)pMem->
f7e60 72 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66  r;.  }else if( f
f7e70 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c  lags & (MEM_Str|
f7e80 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20  MEM_Blob) ){.   
f7e90 20 69 36 34 20 76 61 6c 75 65 3b 0a 20 20 20 20   i64 value;.    
f7ea0 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d  pMem->flags |= M
f7eb0 45 4d 5f 53 74 72 3b 0a 20 20 20 20 69 66 28 20  EM_Str;.    if( 
f7ec0 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
f7ed0 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20  eEncoding(pMem, 
f7ee0 53 51 4c 49 54 45 5f 55 54 46 38 29 0a 20 20 20  SQLITE_UTF8).   
f7ef0 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 56 64      || sqlite3Vd
f7f00 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74  beMemNulTerminat
f7f10 65 28 70 4d 65 6d 29 20 29 7b 0a 20 20 20 20 20  e(pMem) ){.     
f7f20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d   return 0;.    }
f7f30 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65  .    assert( pMe
f7f40 6d 2d 3e 7a 20 29 3b 0a 20 20 20 20 73 71 6c 69  m->z );.    sqli
f7f50 74 65 33 41 74 6f 69 36 34 28 70 4d 65 6d 2d 3e  te3Atoi64(pMem->
f7f60 7a 2c 20 26 76 61 6c 75 65 29 3b 0a 20 20 20 20  z, &value);.    
f7f70 72 65 74 75 72 6e 20 76 61 6c 75 65 3b 0a 20 20  return value;.  
f7f80 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72  }else{.    retur
f7f90 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  n 0;.  }.}../*.*
f7fa0 2a 20 52 65 74 75 72 6e 20 74 68 65 20 62 65 73  * Return the bes
f7fb0 74 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  t representation
f7fc0 20 6f 66 20 70 4d 65 6d 20 74 68 61 74 20 77 65   of pMem that we
f7fd0 20 63 61 6e 20 67 65 74 20 69 6e 74 6f 20 61 0a   can get into a.
f7fe0 2a 2a 20 64 6f 75 62 6c 65 2e 20 20 49 66 20 70  ** double.  If p
f7ff0 4d 65 6d 20 69 73 20 61 6c 72 65 61 64 79 20 61  Mem is already a
f8000 20 64 6f 75 62 6c 65 20 6f 72 20 61 6e 20 69 6e   double or an in
f8010 74 65 67 65 72 2c 20 72 65 74 75 72 6e 20 69 74  teger, return it
f8020 73 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 49 66 20  s.** value.  If 
f8030 69 74 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f  it is a string o
f8040 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f 20 63  r blob, try to c
f8050 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 20 64  onvert it to a d
f8060 6f 75 62 6c 65 2e 0a 2a 2a 20 49 66 20 69 74 20  ouble..** If it 
f8070 69 73 20 61 20 4e 55 4c 4c 2c 20 72 65 74 75 72  is a NULL, retur
f8080 6e 20 30 2e 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45  n 0.0..*/.SQLITE
f8090 5f 50 52 49 56 41 54 45 20 64 6f 75 62 6c 65 20  _PRIVATE double 
f80a0 73 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56  sqlite3VdbeRealV
f80b0 61 6c 75 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b  alue(Mem *pMem){
f80c0 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61  .  if( pMem->fla
f80d0 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b  gs & MEM_Real ){
f80e0 0a 20 20 20 20 72 65 74 75 72 6e 20 70 4d 65 6d  .    return pMem
f80f0 2d 3e 72 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ->r;.  }else if(
f8100 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d   pMem->flags & M
f8110 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 72 65  EM_Int ){.    re
f8120 74 75 72 6e 20 28 64 6f 75 62 6c 65 29 70 4d 65  turn (double)pMe
f8130 6d 2d 3e 75 2e 69 3b 0a 20 20 7d 65 6c 73 65 20  m->u.i;.  }else 
f8140 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  if( pMem->flags 
f8150 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42  & (MEM_Str|MEM_B
f8160 6c 6f 62 29 20 29 7b 0a 20 20 20 20 64 6f 75 62  lob) ){.    doub
f8170 6c 65 20 76 61 6c 20 3d 20 30 2e 30 3b 0a 20 20  le val = 0.0;.  
f8180 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d    pMem->flags |=
f8190 20 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20 69 66   MEM_Str;.    if
f81a0 28 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61  ( sqlite3VdbeCha
f81b0 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d  ngeEncoding(pMem
f81c0 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 0a 20  , SQLITE_UTF8). 
f81d0 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33        || sqlite3
f81e0 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e  VdbeMemNulTermin
f81f0 61 74 65 28 70 4d 65 6d 29 20 29 7b 0a 20 20 20  ate(pMem) ){.   
f8200 20 20 20 72 65 74 75 72 6e 20 30 2e 30 3b 0a 20     return 0.0;. 
f8210 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
f8220 20 70 4d 65 6d 2d 3e 7a 20 29 3b 0a 20 20 20 20   pMem->z );.    
f8230 73 71 6c 69 74 65 33 41 74 6f 46 28 70 4d 65 6d  sqlite3AtoF(pMem
f8240 2d 3e 7a 2c 20 26 76 61 6c 29 3b 0a 20 20 20 20  ->z, &val);.    
f8250 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 65  return val;.  }e
f8260 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  lse{.    return 
f8270 30 2e 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  0.0;.  }.}../*.*
f8280 2a 20 54 68 65 20 4d 45 4d 20 73 74 72 75 63 74  * The MEM struct
f8290 75 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61  ure is already a
f82a0 20 4d 45 4d 5f 52 65 61 6c 2e 20 20 54 72 79 20   MEM_Real.  Try 
f82b0 74 6f 20 61 6c 73 6f 20 6d 61 6b 65 20 69 74 20  to also make it 
f82c0 61 0a 2a 2a 20 4d 45 4d 5f 49 6e 74 20 69 66 20  a.** MEM_Int if 
f82d0 77 65 20 63 61 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  we can..*/.SQLIT
f82e0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
f82f0 71 6c 69 74 65 33 56 64 62 65 49 6e 74 65 67 65  qlite3VdbeIntege
f8300 72 41 66 66 69 6e 69 74 79 28 4d 65 6d 20 2a 70  rAffinity(Mem *p
f8310 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20  Mem){.  assert( 
f8320 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pMem->flags & ME
f8330 4d 5f 52 65 61 6c 20 29 3b 0a 20 20 70 4d 65 6d  M_Real );.  pMem
f8340 2d 3e 75 2e 69 20 3d 20 70 4d 65 6d 2d 3e 72 3b  ->u.i = pMem->r;
f8350 0a 20 20 69 66 28 20 28 28 64 6f 75 62 6c 65 29  .  if( ((double)
f8360 70 4d 65 6d 2d 3e 75 2e 69 29 3d 3d 70 4d 65 6d  pMem->u.i)==pMem
f8370 2d 3e 72 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d  ->r ){.    pMem-
f8380 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 49 6e  >flags |= MEM_In
f8390 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  t;.  }.}../*.** 
f83a0 43 6f 6e 76 65 72 74 20 70 4d 65 6d 20 74 6f 20  Convert pMem to 
f83b0 74 79 70 65 20 69 6e 74 65 67 65 72 2e 20 20 49  type integer.  I
f83c0 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70 72  nvalidate any pr
f83d0 69 6f 72 20 72 65 70 72 65 73 65 6e 74 61 74 69  ior representati
f83e0 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ons..*/.SQLITE_P
f83f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
f8400 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72  e3VdbeMemInteger
f8410 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a  ify(Mem *pMem){.
f8420 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 73 71    pMem->u.i = sq
f8430 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75  lite3VdbeIntValu
f8440 65 28 70 4d 65 6d 29 3b 0a 20 20 73 71 6c 69 74  e(pMem);.  sqlit
f8450 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
f8460 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d 3e  (pMem);.  pMem->
f8470 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
f8480 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
f8490 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  _OK;.}../*.** Co
f84a0 6e 76 65 72 74 20 70 4d 65 6d 20 73 6f 20 74 68  nvert pMem so th
f84b0 61 74 20 69 74 20 69 73 20 6f 66 20 74 79 70 65  at it is of type
f84c0 20 4d 45 4d 5f 52 65 61 6c 2e 0a 2a 2a 20 49 6e   MEM_Real..** In
f84d0 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70 72 69  validate any pri
f84e0 6f 72 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  or representatio
f84f0 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ns..*/.SQLITE_PR
f8500 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
f8510 33 56 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28  3VdbeMemRealify(
f8520 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 70 4d  Mem *pMem){.  pM
f8530 65 6d 2d 3e 72 20 3d 20 73 71 6c 69 74 65 33 56  em->r = sqlite3V
f8540 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70 4d 65  dbeRealValue(pMe
f8550 6d 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  m);.  sqlite3Vdb
f8560 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d  eMemRelease(pMem
f8570 29 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  );.  pMem->flags
f8580 20 3d 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20 20 72   = MEM_Real;.  r
f8590 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
f85a0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  .}../*.** Conver
f85b0 74 20 70 4d 65 6d 20 73 6f 20 74 68 61 74 20 69  t pMem so that i
f85c0 74 20 68 61 73 20 74 79 70 65 73 20 4d 45 4d 5f  t has types MEM_
f85d0 52 65 61 6c 20 6f 72 20 4d 45 4d 5f 49 6e 74 20  Real or MEM_Int 
f85e0 6f 72 20 62 6f 74 68 2e 0a 2a 2a 20 49 6e 76 61  or both..** Inva
f85f0 6c 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72  lidate any prior
f8600 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73   representations
f8610 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
f8620 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
f8630 64 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28 4d  dbeMemNumerify(M
f8640 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 64 6f 75  em *pMem){.  dou
f8650 62 6c 65 20 72 31 2c 20 72 32 3b 0a 20 20 69 36  ble r1, r2;.  i6
f8660 34 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 28  4 i;.  assert( (
f8670 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d  pMem->flags & (M
f8680 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c  EM_Int|MEM_Real|
f8690 4d 45 4d 5f 4e 75 6c 6c 29 29 3d 3d 30 20 29 3b  MEM_Null))==0 );
f86a0 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d  .  assert( (pMem
f86b0 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 42  ->flags & (MEM_B
f86c0 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 29 21 3d 30  lob|MEM_Str))!=0
f86d0 20 29 3b 0a 20 20 72 31 20 3d 20 73 71 6c 69 74   );.  r1 = sqlit
f86e0 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28  e3VdbeRealValue(
f86f0 70 4d 65 6d 29 3b 0a 20 20 69 20 3d 20 28 69 36  pMem);.  i = (i6
f8700 34 29 72 31 3b 0a 20 20 72 32 20 3d 20 28 64 6f  4)r1;.  r2 = (do
f8710 75 62 6c 65 29 69 3b 0a 20 20 69 66 28 20 72 31  uble)i;.  if( r1
f8720 3d 3d 72 32 20 29 7b 0a 20 20 20 20 73 71 6c 69  ==r2 ){.    sqli
f8730 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65  te3VdbeMemIntege
f8740 72 69 66 79 28 70 4d 65 6d 29 3b 0a 20 20 7d 65  rify(pMem);.  }e
f8750 6c 73 65 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 72  lse{.    pMem->r
f8760 20 3d 20 72 31 3b 0a 20 20 20 20 70 4d 65 6d 2d   = r1;.    pMem-
f8770 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 52 65 61  >flags = MEM_Rea
f8780 6c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  l;.    sqlite3Vd
f8790 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65  beMemRelease(pMe
f87a0 6d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  m);.  }.  return
f87b0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
f87c0 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20  *.** Delete any 
f87d0 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61  previous value a
f87e0 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 65  nd set the value
f87f0 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d   stored in *pMem
f8800 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c   to NULL..*/.SQL
f8810 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
f8820 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
f8830 65 74 4e 75 6c 6c 28 4d 65 6d 20 2a 70 4d 65 6d  etNull(Mem *pMem
f8840 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  ){.  sqlite3Vdbe
f8850 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29  MemRelease(pMem)
f8860 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  ;.  pMem->flags 
f8870 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 70 4d  = MEM_Null;.  pM
f8880 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54  em->type = SQLIT
f8890 45 5f 4e 55 4c 4c 3b 0a 20 20 70 4d 65 6d 2d 3e  E_NULL;.  pMem->
f88a0 6e 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n = 0;.}../*.** 
f88b0 44 65 6c 65 74 65 20 61 6e 79 20 70 72 65 76 69  Delete any previ
f88c0 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 20 73 65  ous value and se
f88d0 74 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20 62  t the value to b
f88e0 65 20 61 20 42 4c 4f 42 20 6f 66 20 6c 65 6e 67  e a BLOB of leng
f88f0 74 68 0a 2a 2a 20 6e 20 63 6f 6e 74 61 69 6e 69  th.** n containi
f8900 6e 67 20 61 6c 6c 20 7a 65 72 6f 73 2e 0a 2a 2f  ng all zeros..*/
f8910 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
f8920 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
f8930 4d 65 6d 53 65 74 5a 65 72 6f 42 6c 6f 62 28 4d  MemSetZeroBlob(M
f8940 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 6e 29  em *pMem, int n)
f8950 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  {.  sqlite3VdbeM
f8960 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b  emRelease(pMem);
f8970 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d  .  pMem->flags =
f8980 20 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 5a 65   MEM_Blob|MEM_Ze
f8990 72 6f 7c 4d 45 4d 5f 53 68 6f 72 74 3b 0a 20 20  ro|MEM_Short;.  
f89a0 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
f89b0 49 54 45 5f 42 4c 4f 42 3b 0a 20 20 70 4d 65 6d  ITE_BLOB;.  pMem
f89c0 2d 3e 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e  ->n = 0;.  if( n
f89d0 3c 30 20 29 20 6e 20 3d 20 30 3b 0a 20 20 70 4d  <0 ) n = 0;.  pM
f89e0 65 6d 2d 3e 75 2e 69 20 3d 20 6e 3b 0a 20 20 70  em->u.i = n;.  p
f89f0 4d 65 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d 3e 7a  Mem->z = pMem->z
f8a00 53 68 6f 72 74 3b 0a 20 20 70 4d 65 6d 2d 3e 65  Short;.  pMem->e
f8a10 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38  nc = SQLITE_UTF8
f8a20 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74  ;.}../*.** Delet
f8a30 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76  e any previous v
f8a40 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68 65  alue and set the
f8a50 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e   value stored in
f8a60 20 2a 70 4d 65 6d 20 74 6f 20 76 61 6c 2c 0a 2a   *pMem to val,.*
f8a70 2a 20 6d 61 6e 69 66 65 73 74 20 74 79 70 65 20  * manifest type 
f8a80 49 4e 54 45 47 45 52 2e 0a 2a 2f 0a 53 51 4c 49  INTEGER..*/.SQLI
f8a90 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
f8aa0 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
f8ab0 74 49 6e 74 36 34 28 4d 65 6d 20 2a 70 4d 65 6d  tInt64(Mem *pMem
f8ac0 2c 20 69 36 34 20 76 61 6c 29 7b 0a 20 20 73 71  , i64 val){.  sq
f8ad0 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
f8ae0 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65  ase(pMem);.  pMe
f8af0 6d 2d 3e 75 2e 69 20 3d 20 76 61 6c 3b 0a 20 20  m->u.i = val;.  
f8b00 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
f8b10 4d 5f 49 6e 74 3b 0a 20 20 70 4d 65 6d 2d 3e 74  M_Int;.  pMem->t
f8b20 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54  ype = SQLITE_INT
f8b30 45 47 45 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  EGER;.}../*.** D
f8b40 65 6c 65 74 65 20 61 6e 79 20 70 72 65 76 69 6f  elete any previo
f8b50 75 73 20 76 61 6c 75 65 20 61 6e 64 20 73 65 74  us value and set
f8b60 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72 65   the value store
f8b70 64 20 69 6e 20 2a 70 4d 65 6d 20 74 6f 20 76 61  d in *pMem to va
f8b80 6c 2c 0a 2a 2a 20 6d 61 6e 69 66 65 73 74 20 74  l,.** manifest t
f8b90 79 70 65 20 52 45 41 4c 2e 0a 2a 2f 0a 53 51 4c  ype REAL..*/.SQL
f8ba0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
f8bb0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
f8bc0 65 74 44 6f 75 62 6c 65 28 4d 65 6d 20 2a 70 4d  etDouble(Mem *pM
f8bd0 65 6d 2c 20 64 6f 75 62 6c 65 20 76 61 6c 29 7b  em, double val){
f8be0 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69  .  if( sqlite3_i
f8bf0 73 6e 61 6e 28 76 61 6c 29 20 29 7b 0a 20 20 20  snan(val) ){.   
f8c00 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
f8c10 65 74 4e 75 6c 6c 28 70 4d 65 6d 29 3b 0a 20 20  etNull(pMem);.  
f8c20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
f8c30 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
f8c40 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d  (pMem);.    pMem
f8c50 2d 3e 72 20 3d 20 76 61 6c 3b 0a 20 20 20 20 70  ->r = val;.    p
f8c60 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
f8c70 5f 52 65 61 6c 3b 0a 20 20 20 20 70 4d 65 6d 2d  _Real;.    pMem-
f8c80 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 46  >type = SQLITE_F
f8c90 4c 4f 41 54 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  LOAT;.  }.}../*.
f8ca0 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69  ** Return true i
f8cb0 66 20 74 68 65 20 4d 65 6d 20 6f 62 6a 65 63 74  f the Mem object
f8cc0 20 63 6f 6e 74 61 69 6e 73 20 61 20 54 45 58 54   contains a TEXT
f8cd0 20 6f 72 20 42 4c 4f 42 20 74 68 61 74 20 69 73   or BLOB that is
f8ce0 0a 2a 2a 20 74 6f 6f 20 6c 61 72 67 65 20 2d 20  .** too large - 
f8cf0 77 68 6f 73 65 20 73 69 7a 65 20 65 78 63 65 65  whose size excee
f8d00 64 73 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45  ds SQLITE_MAX_LE
f8d10 4e 47 54 48 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  NGTH..*/.SQLITE_
f8d20 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
f8d30 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67  te3VdbeMemTooBig
f8d40 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20  (Mem *p){.  if( 
f8d50 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f  p->flags & (MEM_
f8d60 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b  Str|MEM_Blob) ){
f8d70 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 2d 3e  .    int n = p->
f8d80 6e 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 6c  n;.    if( p->fl
f8d90 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29  ags & MEM_Zero )
f8da0 7b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 70 2d 3e  {.      n += p->
f8db0 75 2e 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  u.i;.    }.    r
f8dc0 65 74 75 72 6e 20 6e 3e 53 51 4c 49 54 45 5f 4d  eturn n>SQLITE_M
f8dd0 41 58 5f 4c 45 4e 47 54 48 3b 0a 20 20 7d 0a 20  AX_LENGTH;.  }. 
f8de0 20 72 65 74 75 72 6e 20 30 3b 20 0a 7d 0a 0a 2f   return 0; .}../
f8df0 2a 0a 2a 2a 20 4d 61 6b 65 20 61 6e 20 73 68 61  *.** Make an sha
f8e00 6c 6c 6f 77 20 63 6f 70 79 20 6f 66 20 70 46 72  llow copy of pFr
f8e10 6f 6d 20 69 6e 74 6f 20 70 54 6f 2e 20 20 50 72  om into pTo.  Pr
f8e20 69 6f 72 20 63 6f 6e 74 65 6e 74 73 20 6f 66 0a  ior contents of.
f8e30 2a 2a 20 70 54 6f 20 61 72 65 20 6f 76 65 72 77  ** pTo are overw
f8e40 72 69 74 74 65 6e 2e 20 20 54 68 65 20 70 46 72  ritten.  The pFr
f8e50 6f 6d 2d 3e 7a 20 66 69 65 6c 64 20 69 73 20 6e  om->z field is n
f8e60 6f 74 20 64 75 70 6c 69 63 61 74 65 64 2e 20 20  ot duplicated.  
f8e70 49 66 0a 2a 2a 20 70 46 72 6f 6d 2d 3e 7a 20 69  If.** pFrom->z i
f8e80 73 20 75 73 65 64 2c 20 74 68 65 6e 20 70 54 6f  s used, then pTo
f8e90 2d 3e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 74 68  ->z points to th
f8ea0 65 20 73 61 6d 65 20 74 68 69 6e 67 20 61 73 20  e same thing as 
f8eb0 70 46 72 6f 6d 2d 3e 7a 0a 2a 2a 20 61 6e 64 20  pFrom->z.** and 
f8ec0 66 6c 61 67 73 20 67 65 74 73 20 73 72 63 54 79  flags gets srcTy
f8ed0 70 65 20 28 65 69 74 68 65 72 20 4d 45 4d 5f 45  pe (either MEM_E
f8ee0 70 68 65 6d 20 6f 72 20 4d 45 4d 5f 53 74 61 74  phem or MEM_Stat
f8ef0 69 63 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ic)..*/.SQLITE_P
f8f00 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
f8f10 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f  te3VdbeMemShallo
f8f20 77 43 6f 70 79 28 4d 65 6d 20 2a 70 54 6f 2c 20  wCopy(Mem *pTo, 
f8f30 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 46 72 6f 6d  const Mem *pFrom
f8f40 2c 20 69 6e 74 20 73 72 63 54 79 70 65 29 7b 0a  , int srcType){.
f8f50 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70 46    memcpy(pTo, pF
f8f60 72 6f 6d 2c 20 73 69 7a 65 6f 66 28 2a 70 46 72  rom, sizeof(*pFr
f8f70 6f 6d 29 2d 73 69 7a 65 6f 66 28 70 46 72 6f 6d  om)-sizeof(pFrom
f8f80 2d 3e 7a 53 68 6f 72 74 29 29 3b 0a 20 20 70 54  ->zShort));.  pT
f8f90 6f 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 69  o->xDel = 0;.  i
f8fa0 66 28 20 70 54 6f 2d 3e 66 6c 61 67 73 20 26 20  f( pTo->flags & 
f8fb0 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f  (MEM_Str|MEM_Blo
f8fc0 62 29 20 29 7b 0a 20 20 20 20 70 54 6f 2d 3e 66  b) ){.    pTo->f
f8fd0 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 44 79  lags &= ~(MEM_Dy
f8fe0 6e 7c 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d  n|MEM_Static|MEM
f8ff0 5f 53 68 6f 72 74 7c 4d 45 4d 5f 45 70 68 65 6d  _Short|MEM_Ephem
f9000 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  );.    assert( s
f9010 72 63 54 79 70 65 3d 3d 4d 45 4d 5f 45 70 68 65  rcType==MEM_Ephe
f9020 6d 20 7c 7c 20 73 72 63 54 79 70 65 3d 3d 4d 45  m || srcType==ME
f9030 4d 5f 53 74 61 74 69 63 20 29 3b 0a 20 20 20 20  M_Static );.    
f9040 70 54 6f 2d 3e 66 6c 61 67 73 20 7c 3d 20 73 72  pTo->flags |= sr
f9050 63 54 79 70 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  cType;.  }.}../*
f9060 0a 2a 2a 20 4d 61 6b 65 20 61 20 66 75 6c 6c 20  .** Make a full 
f9070 63 6f 70 79 20 6f 66 20 70 46 72 6f 6d 20 69 6e  copy of pFrom in
f9080 74 6f 20 70 54 6f 2e 20 20 50 72 69 6f 72 20 63  to pTo.  Prior c
f9090 6f 6e 74 65 6e 74 73 20 6f 66 20 70 54 6f 20 61  ontents of pTo a
f90a0 72 65 0a 2a 2a 20 66 72 65 65 64 20 62 65 66 6f  re.** freed befo
f90b0 72 65 20 74 68 65 20 63 6f 70 79 20 69 73 20 6d  re the copy is m
f90c0 61 64 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ade..*/.SQLITE_P
f90d0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
f90e0 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 4d 65  e3VdbeMemCopy(Me
f90f0 6d 20 2a 70 54 6f 2c 20 63 6f 6e 73 74 20 4d 65  m *pTo, const Me
f9100 6d 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 69 6e 74  m *pFrom){.  int
f9110 20 72 63 3b 0a 20 20 69 66 28 20 70 54 6f 2d 3e   rc;.  if( pTo->
f9120 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 20  flags & MEM_Dyn 
f9130 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
f9140 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 54 6f  beMemRelease(pTo
f9150 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
f9160 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f  VdbeMemShallowCo
f9170 70 79 28 70 54 6f 2c 20 70 46 72 6f 6d 2c 20 4d  py(pTo, pFrom, M
f9180 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 69 66 28  EM_Ephem);.  if(
f9190 20 70 54 6f 2d 3e 66 6c 61 67 73 20 26 20 4d 45   pTo->flags & ME
f91a0 4d 5f 45 70 68 65 6d 20 29 7b 0a 20 20 20 20 72  M_Ephem ){.    r
f91b0 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  c = sqlite3VdbeM
f91c0 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28  emMakeWriteable(
f91d0 70 54 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  pTo);.  }else{. 
f91e0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
f91f0 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  K;.  }.  return 
f9200 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61  rc;.}../*.** Tra
f9210 6e 73 66 65 72 20 74 68 65 20 63 6f 6e 74 65 6e  nsfer the conten
f9220 74 73 20 6f 66 20 70 46 72 6f 6d 20 74 6f 20 70  ts of pFrom to p
f9230 54 6f 2e 20 41 6e 79 20 65 78 69 73 74 69 6e 67  To. Any existing
f9240 20 76 61 6c 75 65 20 69 6e 20 70 54 6f 20 69 73   value in pTo is
f9250 0a 2a 2a 20 66 72 65 65 64 2e 20 49 66 20 70 46  .** freed. If pF
f9260 72 6f 6d 20 63 6f 6e 74 61 69 6e 73 20 65 70 68  rom contains eph
f9270 65 6d 65 72 61 6c 20 64 61 74 61 2c 20 61 20 63  emeral data, a c
f9280 6f 70 79 20 69 73 20 6d 61 64 65 2e 0a 2a 2a 0a  opy is made..**.
f9290 2a 2a 20 70 46 72 6f 6d 20 63 6f 6e 74 61 69 6e  ** pFrom contain
f92a0 73 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 77 68  s an SQL NULL wh
f92b0 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  en this routine 
f92c0 72 65 74 75 72 6e 73 2e 20 20 53 51 4c 49 54 45  returns.  SQLITE
f92d0 5f 4e 4f 4d 45 4d 0a 2a 2a 20 6d 69 67 68 74 20  _NOMEM.** might 
f92e0 62 65 20 72 65 74 75 72 6e 65 64 20 69 66 20 70  be returned if p
f92f0 46 72 6f 6d 20 68 65 6c 64 20 65 70 68 65 6d 65  From held epheme
f9300 72 61 6c 20 64 61 74 61 20 61 6e 64 20 77 65 20  ral data and we 
f9310 77 65 72 65 20 75 6e 61 62 6c 65 0a 2a 2a 20 74  were unable.** t
f9320 6f 20 61 6c 6c 6f 63 61 74 65 20 65 6e 6f 75 67  o allocate enoug
f9330 68 20 73 70 61 63 65 20 74 6f 20 6d 61 6b 65 20  h space to make 
f9340 61 20 63 6f 70 79 2e 0a 2a 2f 0a 53 51 4c 49 54  a copy..*/.SQLIT
f9350 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
f9360 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65  lite3VdbeMemMove
f9370 28 4d 65 6d 20 2a 70 54 6f 2c 20 4d 65 6d 20 2a  (Mem *pTo, Mem *
f9380 70 46 72 6f 6d 29 7b 0a 20 20 69 6e 74 20 72 63  pFrom){.  int rc
f9390 3b 0a 20 20 69 66 28 20 70 54 6f 2d 3e 66 6c 61  ;.  if( pTo->fla
f93a0 67 73 20 26 20 4d 45 4d 5f 44 79 6e 20 29 7b 0a  gs & MEM_Dyn ){.
f93b0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
f93c0 65 6d 52 65 6c 65 61 73 65 28 70 54 6f 29 3b 0a  emRelease(pTo);.
f93d0 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 70 54 6f    }.  memcpy(pTo
f93e0 2c 20 70 46 72 6f 6d 2c 20 73 69 7a 65 6f 66 28  , pFrom, sizeof(
f93f0 4d 65 6d 29 29 3b 0a 20 20 69 66 28 20 70 46 72  Mem));.  if( pFr
f9400 6f 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  om->flags & MEM_
f9410 53 68 6f 72 74 20 29 7b 0a 20 20 20 20 70 54 6f  Short ){.    pTo
f9420 2d 3e 7a 20 3d 20 70 54 6f 2d 3e 7a 53 68 6f 72  ->z = pTo->zShor
f9430 74 3b 0a 20 20 7d 0a 20 20 70 46 72 6f 6d 2d 3e  t;.  }.  pFrom->
f9440 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
f9450 3b 0a 20 20 70 46 72 6f 6d 2d 3e 78 44 65 6c 20  ;.  pFrom->xDel 
f9460 3d 20 30 3b 0a 20 20 69 66 28 20 70 54 6f 2d 3e  = 0;.  if( pTo->
f9470 66 6c 61 67 73 20 26 20 4d 45 4d 5f 45 70 68 65  flags & MEM_Ephe
f9480 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  m ){.    rc = sq
f9490 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65  lite3VdbeMemMake
f94a0 57 72 69 74 65 61 62 6c 65 28 70 54 6f 29 3b 0a  Writeable(pTo);.
f94b0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
f94c0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  = SQLITE_OK;.  }
f94d0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
f94e0 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
f94f0 65 20 76 61 6c 75 65 20 6f 66 20 61 20 4d 65 6d  e value of a Mem
f9500 20 74 6f 20 62 65 20 61 20 73 74 72 69 6e 67 20   to be a string 
f9510 6f 72 20 61 20 42 4c 4f 42 2e 0a 2a 2f 0a 53 51  or a BLOB..*/.SQ
f9520 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
f9530 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
f9540 65 74 53 74 72 28 0a 20 20 4d 65 6d 20 2a 70 4d  etStr(.  Mem *pM
f9550 65 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  em,          /* 
f9560 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20 73  Memory cell to s
f9570 65 74 20 74 6f 20 73 74 72 69 6e 67 20 76 61 6c  et to string val
f9580 75 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ue */.  const ch
f9590 61 72 20 2a 7a 2c 20 20 20 20 20 20 2f 2a 20 53  ar *z,      /* S
f95a0 74 72 69 6e 67 20 70 6f 69 6e 74 65 72 20 2a 2f  tring pointer */
f95b0 0a 20 20 69 6e 74 20 6e 2c 20 20 20 20 20 20 20  .  int n,       
f95c0 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20         /* Bytes 
f95d0 69 6e 20 73 74 72 69 6e 67 2c 20 6f 72 20 6e 65  in string, or ne
f95e0 67 61 74 69 76 65 20 2a 2f 0a 20 20 75 38 20 65  gative */.  u8 e
f95f0 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  nc,             
f9600 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 6f 66 20 7a  /* Encoding of z
f9610 2e 20 20 30 20 66 6f 72 20 42 4c 4f 42 73 20 2a  .  0 for BLOBs *
f9620 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29  /.  void (*xDel)
f9630 28 76 6f 69 64 2a 29 20 2f 2a 20 44 65 73 74 72  (void*) /* Destr
f9640 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a  uctor function *
f9650 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64  /.){.  sqlite3Vd
f9660 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65  beMemRelease(pMe
f9670 6d 29 3b 0a 20 20 69 66 28 20 21 7a 20 29 7b 0a  m);.  if( !z ){.
f9680 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
f9690 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20  = MEM_Null;.    
f96a0 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
f96b0 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20 20 20 72 65  ITE_NULL;.    re
f96c0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
f96d0 20 20 7d 0a 0a 20 20 70 4d 65 6d 2d 3e 7a 20 3d    }..  pMem->z =
f96e0 20 28 63 68 61 72 20 2a 29 7a 3b 0a 20 20 69 66   (char *)z;.  if
f96f0 28 20 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 53  ( xDel==SQLITE_S
f9700 54 41 54 49 43 20 29 7b 0a 20 20 20 20 70 4d 65  TATIC ){.    pMe
f9710 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 53  m->flags = MEM_S
f9720 74 61 74 69 63 3b 0a 20 20 7d 65 6c 73 65 20 69  tatic;.  }else i
f9730 66 28 20 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f  f( xDel==SQLITE_
f9740 54 52 41 4e 53 49 45 4e 54 20 29 7b 0a 20 20 20  TRANSIENT ){.   
f9750 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
f9760 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 7d 65 6c 73  EM_Ephem;.  }els
f9770 65 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  e{.    pMem->fla
f9780 67 73 20 3d 20 4d 45 4d 5f 44 79 6e 3b 0a 20 20  gs = MEM_Dyn;.  
f9790 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 78    pMem->xDel = x
f97a0 44 65 6c 3b 0a 20 20 7d 0a 0a 20 20 70 4d 65 6d  Del;.  }..  pMem
f97b0 2d 3e 65 6e 63 20 3d 20 65 6e 63 3b 0a 20 20 70  ->enc = enc;.  p
f97c0 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 65 6e 63 3d  Mem->type = enc=
f97d0 3d 30 20 3f 20 53 51 4c 49 54 45 5f 42 4c 4f 42  =0 ? SQLITE_BLOB
f97e0 20 3a 20 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a   : SQLITE_TEXT;.
f97f0 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 3b 0a 0a    pMem->n = n;..
f9800 20 20 61 73 73 65 72 74 28 20 65 6e 63 3d 3d 30    assert( enc==0
f9810 20 7c 7c 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f   || enc==SQLITE_
f9820 55 54 46 38 20 7c 7c 20 65 6e 63 3d 3d 53 51 4c  UTF8 || enc==SQL
f9830 49 54 45 5f 55 54 46 31 36 4c 45 20 0a 20 20 20  ITE_UTF16LE .   
f9840 20 20 20 7c 7c 20 65 6e 63 3d 3d 53 51 4c 49 54     || enc==SQLIT
f9850 45 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 20 73  E_UTF16BE );.  s
f9860 77 69 74 63 68 28 20 65 6e 63 20 29 7b 0a 20 20  witch( enc ){.  
f9870 20 20 63 61 73 65 20 30 3a 0a 20 20 20 20 20 20    case 0:.      
f9880 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d  pMem->flags |= M
f9890 45 4d 5f 42 6c 6f 62 3b 0a 20 20 20 20 20 20 70  EM_Blob;.      p
f98a0 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54  Mem->enc = SQLIT
f98b0 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 62 72  E_UTF8;.      br
f98c0 65 61 6b 3b 0a 0a 20 20 20 20 63 61 73 65 20 53  eak;..    case S
f98d0 51 4c 49 54 45 5f 55 54 46 38 3a 0a 20 20 20 20  QLITE_UTF8:.    
f98e0 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d    pMem->flags |=
f98f0 20 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20 20 20   MEM_Str;.      
f9900 69 66 28 20 6e 3c 30 20 29 7b 0a 20 20 20 20 20  if( n<0 ){.     
f9910 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 74 72     pMem->n = str
f9920 6c 65 6e 28 7a 29 3b 0a 20 20 20 20 20 20 20 20  len(z);.        
f9930 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d  pMem->flags |= M
f9940 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 7d  EM_Term;.      }
f9950 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a 23  .      break;..#
f9960 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
f9970 49 54 5f 55 54 46 31 36 0a 20 20 20 20 63 61 73  IT_UTF16.    cas
f9980 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45  e SQLITE_UTF16LE
f9990 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  :.    case SQLIT
f99a0 45 5f 55 54 46 31 36 42 45 3a 0a 20 20 20 20 20  E_UTF16BE:.     
f99b0 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20   pMem->flags |= 
f99c0 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20 20 20 69  MEM_Str;.      i
f99d0 66 28 20 70 4d 65 6d 2d 3e 6e 3c 30 20 29 7b 0a  f( pMem->n<0 ){.
f99e0 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20          pMem->n 
f99f0 3d 20 73 71 6c 69 74 65 33 55 74 66 31 36 42 79  = sqlite3Utf16By
f9a00 74 65 4c 65 6e 28 70 4d 65 6d 2d 3e 7a 2c 2d 31  teLen(pMem->z,-1
f9a10 29 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d  );.        pMem-
f9a20 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65  >flags |= MEM_Te
f9a30 72 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rm;.      }.    
f9a40 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62    if( sqlite3Vdb
f9a50 65 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 70 4d  eMemHandleBom(pM
f9a60 65 6d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72  em) ){.        r
f9a70 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
f9a80 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64  EM;.      }.#end
f9a90 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
f9aa0 54 5f 55 54 46 31 36 20 2a 2f 0a 20 20 7d 0a 20  T_UTF16 */.  }. 
f9ab0 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73   if( pMem->flags
f9ac0 26 4d 45 4d 5f 45 70 68 65 6d 20 29 7b 0a 20 20  &MEM_Ephem ){.  
f9ad0 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
f9ae0 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65  VdbeMemMakeWrite
f9af0 61 62 6c 65 28 70 4d 65 6d 29 3b 0a 20 20 7d 0a  able(pMem);.  }.
f9b00 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
f9b10 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d  OK;.}../*.** Com
f9b20 70 61 72 65 20 74 68 65 20 76 61 6c 75 65 73 20  pare the values 
f9b30 63 6f 6e 74 61 69 6e 65 64 20 62 79 20 74 68 65  contained by the
f9b40 20 74 77 6f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c   two memory cell
f9b50 73 2c 20 72 65 74 75 72 6e 69 6e 67 0a 2a 2a 20  s, returning.** 
f9b60 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 20 6f  negative, zero o
f9b70 72 20 70 6f 73 69 74 69 76 65 20 69 66 20 70 4d  r positive if pM
f9b80 65 6d 31 20 69 73 20 6c 65 73 73 20 74 68 61 6e  em1 is less than
f9b90 2c 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67  , equal to, or g
f9ba0 72 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 70  reater.** than p
f9bb0 4d 65 6d 32 2e 20 53 6f 72 74 69 6e 67 20 6f 72  Mem2. Sorting or
f9bc0 64 65 72 20 69 73 20 4e 55 4c 4c 27 73 20 66 69  der is NULL's fi
f9bd0 72 73 74 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79  rst, followed by
f9be0 20 6e 75 6d 62 65 72 73 20 28 69 6e 74 65 67 65   numbers (intege
f9bf0 72 73 0a 2a 2a 20 61 6e 64 20 72 65 61 6c 73 29  rs.** and reals)
f9c00 20 73 6f 72 74 65 64 20 6e 75 6d 65 72 69 63 61   sorted numerica
f9c10 6c 6c 79 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79  lly, followed by
f9c20 20 74 65 78 74 20 6f 72 64 65 72 65 64 20 62 79   text ordered by
f9c30 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a   the collating.*
f9c40 2a 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c  * sequence pColl
f9c50 20 61 6e 64 20 66 69 6e 61 6c 6c 79 20 62 6c 6f   and finally blo
f9c60 62 27 73 20 6f 72 64 65 72 65 64 20 62 79 20 6d  b's ordered by m
f9c70 65 6d 63 6d 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 54  emcmp()..**.** T
f9c80 77 6f 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20 61  wo NULL values a
f9c90 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 65 71  re considered eq
f9ca0 75 61 6c 20 62 79 20 74 68 69 73 20 66 75 6e 63  ual by this func
f9cb0 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
f9cc0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
f9cd0 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 63 6f  te3MemCompare(co
f9ce0 6e 73 74 20 4d 65 6d 20 2a 70 4d 65 6d 31 2c 20  nst Mem *pMem1, 
f9cf0 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 4d 65 6d 32  const Mem *pMem2
f9d00 2c 20 63 6f 6e 73 74 20 43 6f 6c 6c 53 65 71 20  , const CollSeq 
f9d10 2a 70 43 6f 6c 6c 29 7b 0a 20 20 69 6e 74 20 72  *pColl){.  int r
f9d20 63 3b 0a 20 20 69 6e 74 20 66 31 2c 20 66 32 3b  c;.  int f1, f2;
f9d30 0a 20 20 69 6e 74 20 63 6f 6d 62 69 6e 65 64 5f  .  int combined_
f9d40 66 6c 61 67 73 3b 0a 0a 20 20 2f 2a 20 49 6e 74  flags;..  /* Int
f9d50 65 72 63 68 61 6e 67 65 20 70 4d 65 6d 31 20 61  erchange pMem1 a
f9d60 6e 64 20 70 4d 65 6d 32 20 69 66 20 74 68 65 20  nd pMem2 if the 
f9d70 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
f9d80 63 65 20 73 70 65 63 69 66 69 65 73 0a 20 20 2a  ce specifies.  *
f9d90 2a 20 44 45 53 43 20 6f 72 64 65 72 2e 0a 20 20  * DESC order..  
f9da0 2a 2f 0a 20 20 66 31 20 3d 20 70 4d 65 6d 31 2d  */.  f1 = pMem1-
f9db0 3e 66 6c 61 67 73 3b 0a 20 20 66 32 20 3d 20 70  >flags;.  f2 = p
f9dc0 4d 65 6d 32 2d 3e 66 6c 61 67 73 3b 0a 20 20 63  Mem2->flags;.  c
f9dd0 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 20 3d 20  ombined_flags = 
f9de0 66 31 7c 66 32 3b 0a 20 0a 20 20 2f 2a 20 49 66  f1|f2;. .  /* If
f9df0 20 6f 6e 65 20 76 61 6c 75 65 20 69 73 20 4e 55   one value is NU
f9e00 4c 4c 2c 20 69 74 20 69 73 20 6c 65 73 73 20 74  LL, it is less t
f9e10 68 61 6e 20 74 68 65 20 6f 74 68 65 72 2e 20 49  han the other. I
f9e20 66 20 62 6f 74 68 20 76 61 6c 75 65 73 0a 20 20  f both values.  
f9e30 2a 2a 20 61 72 65 20 4e 55 4c 4c 2c 20 72 65 74  ** are NULL, ret
f9e40 75 72 6e 20 30 2e 0a 20 20 2a 2f 0a 20 20 69 66  urn 0..  */.  if
f9e50 28 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73  ( combined_flags
f9e60 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20  &MEM_Null ){.   
f9e70 20 72 65 74 75 72 6e 20 28 66 32 26 4d 45 4d 5f   return (f2&MEM_
f9e80 4e 75 6c 6c 29 20 2d 20 28 66 31 26 4d 45 4d 5f  Null) - (f1&MEM_
f9e90 4e 75 6c 6c 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  Null);.  }..  /*
f9ea0 20 49 66 20 6f 6e 65 20 76 61 6c 75 65 20 69 73   If one value is
f9eb0 20 61 20 6e 75 6d 62 65 72 20 61 6e 64 20 74 68   a number and th
f9ec0 65 20 6f 74 68 65 72 20 69 73 20 6e 6f 74 2c 20  e other is not, 
f9ed0 74 68 65 20 6e 75 6d 62 65 72 20 69 73 20 6c 65  the number is le
f9ee0 73 73 2e 0a 20 20 2a 2a 20 49 66 20 62 6f 74 68  ss..  ** If both
f9ef0 20 61 72 65 20 6e 75 6d 62 65 72 73 2c 20 63 6f   are numbers, co
f9f00 6d 70 61 72 65 20 61 73 20 72 65 61 6c 73 20 69  mpare as reals i
f9f10 66 20 6f 6e 65 20 69 73 20 61 20 72 65 61 6c 2c  f one is a real,
f9f20 20 6f 72 20 61 73 20 69 6e 74 65 67 65 72 73 0a   or as integers.
f9f30 20 20 2a 2a 20 69 66 20 62 6f 74 68 20 76 61 6c    ** if both val
f9f40 75 65 73 20 61 72 65 20 69 6e 74 65 67 65 72 73  ues are integers
f9f50 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f 6d  ..  */.  if( com
f9f60 62 69 6e 65 64 5f 66 6c 61 67 73 26 28 4d 45 4d  bined_flags&(MEM
f9f70 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 20 29  _Int|MEM_Real) )
f9f80 7b 0a 20 20 20 20 69 66 28 20 21 28 66 31 26 28  {.    if( !(f1&(
f9f90 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c  MEM_Int|MEM_Real
f9fa0 29 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  )) ){.      retu
f9fb0 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 1;.    }.    
f9fc0 69 66 28 20 21 28 66 32 26 28 4d 45 4d 5f 49 6e  if( !(f2&(MEM_In
f9fd0 74 7c 4d 45 4d 5f 52 65 61 6c 29 29 20 29 7b 0a  t|MEM_Real)) ){.
f9fe0 20 20 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b        return -1;
f9ff0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28  .    }.    if( (
fa000 66 31 20 26 20 66 32 20 26 20 4d 45 4d 5f 49 6e  f1 & f2 & MEM_In
fa010 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 64  t)==0 ){.      d
fa020 6f 75 62 6c 65 20 72 31 2c 20 72 32 3b 0a 20 20  ouble r1, r2;.  
fa030 20 20 20 20 69 66 28 20 28 66 31 26 4d 45 4d 5f      if( (f1&MEM_
fa040 52 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20  Real)==0 ){.    
fa050 20 20 20 20 72 31 20 3d 20 70 4d 65 6d 31 2d 3e      r1 = pMem1->
fa060 75 2e 69 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  u.i;.      }else
fa070 7b 0a 20 20 20 20 20 20 20 20 72 31 20 3d 20 70  {.        r1 = p
fa080 4d 65 6d 31 2d 3e 72 3b 0a 20 20 20 20 20 20 7d  Mem1->r;.      }
fa090 0a 20 20 20 20 20 20 69 66 28 20 28 66 32 26 4d  .      if( (f2&M
fa0a0 45 4d 5f 52 65 61 6c 29 3d 3d 30 20 29 7b 0a 20  EM_Real)==0 ){. 
fa0b0 20 20 20 20 20 20 20 72 32 20 3d 20 70 4d 65 6d         r2 = pMem
fa0c0 32 2d 3e 75 2e 69 3b 0a 20 20 20 20 20 20 7d 65  2->u.i;.      }e
fa0d0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 32 20  lse{.        r2 
fa0e0 3d 20 70 4d 65 6d 32 2d 3e 72 3b 0a 20 20 20 20  = pMem2->r;.    
fa0f0 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 31    }.      if( r1
fa100 3c 72 32 20 29 20 72 65 74 75 72 6e 20 2d 31 3b  <r2 ) return -1;
fa110 0a 20 20 20 20 20 20 69 66 28 20 72 31 3e 72 32  .      if( r1>r2
fa120 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20   ) return 1;.   
fa130 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20     return 0;.   
fa140 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73   }else{.      as
fa150 73 65 72 74 28 20 66 31 26 4d 45 4d 5f 49 6e 74  sert( f1&MEM_Int
fa160 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
fa170 28 20 66 32 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a  ( f2&MEM_Int );.
fa180 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d 31 2d        if( pMem1-
fa190 3e 75 2e 69 20 3c 20 70 4d 65 6d 32 2d 3e 75 2e  >u.i < pMem2->u.
fa1a0 69 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20  i ) return -1;. 
fa1b0 20 20 20 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e       if( pMem1->
fa1c0 75 2e 69 20 3e 20 70 4d 65 6d 32 2d 3e 75 2e 69  u.i > pMem2->u.i
fa1d0 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20   ) return 1;.   
fa1e0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20     return 0;.   
fa1f0 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20   }.  }..  /* If 
fa200 6f 6e 65 20 76 61 6c 75 65 20 69 73 20 61 20 73  one value is a s
fa210 74 72 69 6e 67 20 61 6e 64 20 74 68 65 20 6f 74  tring and the ot
fa220 68 65 72 20 69 73 20 61 20 62 6c 6f 62 2c 20 74  her is a blob, t
fa230 68 65 20 73 74 72 69 6e 67 20 69 73 20 6c 65 73  he string is les
fa240 73 2e 0a 20 20 2a 2a 20 49 66 20 62 6f 74 68 20  s..  ** If both 
fa250 61 72 65 20 73 74 72 69 6e 67 73 2c 20 63 6f 6d  are strings, com
fa260 70 61 72 65 20 75 73 69 6e 67 20 74 68 65 20 63  pare using the c
fa270 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f  ollating functio
fa280 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63  ns..  */.  if( c
fa290 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26 4d 45  ombined_flags&ME
fa2a0 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 69 66 28  M_Str ){.    if(
fa2b0 20 28 66 31 20 26 20 4d 45 4d 5f 53 74 72 29 3d   (f1 & MEM_Str)=
fa2c0 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  =0 ){.      retu
fa2d0 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 1;.    }.    
fa2e0 69 66 28 20 28 66 32 20 26 20 4d 45 4d 5f 53 74  if( (f2 & MEM_St
fa2f0 72 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72  r)==0 ){.      r
fa300 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a  eturn -1;.    }.
fa310 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65  .    assert( pMe
fa320 6d 31 2d 3e 65 6e 63 3d 3d 70 4d 65 6d 32 2d 3e  m1->enc==pMem2->
fa330 65 6e 63 20 29 3b 0a 20 20 20 20 61 73 73 65 72  enc );.    asser
fa340 74 28 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 53  t( pMem1->enc==S
fa350 51 4c 49 54 45 5f 55 54 46 38 20 7c 7c 20 0a 20  QLITE_UTF8 || . 
fa360 20 20 20 20 20 20 20 20 20 20 20 70 4d 65 6d 31             pMem1
fa370 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  ->enc==SQLITE_UT
fa380 46 31 36 4c 45 20 7c 7c 20 70 4d 65 6d 31 2d 3e  F16LE || pMem1->
fa390 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31  enc==SQLITE_UTF1
fa3a0 36 42 45 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 54  6BE );..    /* T
fa3b0 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  he collation seq
fa3c0 75 65 6e 63 65 20 6d 75 73 74 20 62 65 20 64 65  uence must be de
fa3d0 66 69 6e 65 64 20 61 74 20 74 68 69 73 20 70 6f  fined at this po
fa3e0 69 6e 74 2c 20 65 76 65 6e 20 69 66 0a 20 20 20  int, even if.   
fa3f0 20 2a 2a 20 74 68 65 20 75 73 65 72 20 64 65 6c   ** the user del
fa400 65 74 65 73 20 74 68 65 20 63 6f 6c 6c 61 74 69  etes the collati
fa410 6f 6e 20 73 65 71 75 65 6e 63 65 20 61 66 74 65  on sequence afte
fa420 72 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72  r the vdbe progr
fa430 61 6d 20 69 73 0a 20 20 20 20 2a 2a 20 63 6f 6d  am is.    ** com
fa440 70 69 6c 65 64 20 28 74 68 69 73 20 77 61 73 20  piled (this was 
fa450 6e 6f 74 20 61 6c 77 61 79 73 20 74 68 65 20 63  not always the c
fa460 61 73 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ase)..    */.   
fa470 20 61 73 73 65 72 74 28 20 21 70 43 6f 6c 6c 20   assert( !pColl 
fa480 7c 7c 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 20 29  || pColl->xCmp )
fa490 3b 0a 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c  ;..    if( pColl
fa4a0 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4d   ){.      if( pM
fa4b0 65 6d 31 2d 3e 65 6e 63 3d 3d 70 43 6f 6c 6c 2d  em1->enc==pColl-
fa4c0 3e 65 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20  >enc ){.        
fa4d0 2f 2a 20 54 68 65 20 73 74 72 69 6e 67 73 20 61  /* The strings a
fa4e0 72 65 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68  re already in th
fa4f0 65 20 63 6f 72 72 65 63 74 20 65 6e 63 6f 64 69  e correct encodi
fa500 6e 67 2e 20 20 43 61 6c 6c 20 74 68 65 0a 20 20  ng.  Call the.  
fa510 20 20 20 20 20 20 2a 2a 20 63 6f 6d 70 61 72 69        ** compari
fa520 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 64 69 72  son function dir
fa530 65 63 74 6c 79 20 2a 2f 0a 20 20 20 20 20 20 20  ectly */.       
fa540 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 2d 3e 78   return pColl->x
fa550 43 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55 73 65 72  Cmp(pColl->pUser
fa560 2c 70 4d 65 6d 31 2d 3e 6e 2c 70 4d 65 6d 31 2d  ,pMem1->n,pMem1-
fa570 3e 7a 2c 70 4d 65 6d 32 2d 3e 6e 2c 70 4d 65 6d  >z,pMem2->n,pMem
fa580 32 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 7d 65 6c  2->z);.      }el
fa590 73 65 7b 0a 20 20 20 20 20 20 20 20 75 38 20 6f  se{.        u8 o
fa5a0 72 69 67 45 6e 63 20 3d 20 70 4d 65 6d 31 2d 3e  rigEnc = pMem1->
fa5b0 65 6e 63 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e  enc;.        con
fa5c0 73 74 20 76 6f 69 64 20 2a 76 31 2c 20 2a 76 32  st void *v1, *v2
fa5d0 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 31  ;.        int n1
fa5e0 2c 20 6e 32 3b 0a 20 20 20 20 20 20 20 20 2f 2a  , n2;.        /*
fa5f0 20 43 6f 6e 76 65 72 74 20 74 68 65 20 73 74 72   Convert the str
fa600 69 6e 67 73 20 69 6e 74 6f 20 74 68 65 20 65 6e  ings into the en
fa610 63 6f 64 69 6e 67 20 74 68 61 74 20 74 68 65 20  coding that the 
fa620 63 6f 6d 70 61 72 69 73 6f 6e 0a 20 20 20 20 20  comparison.     
fa630 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 65     ** function e
fa640 78 70 65 63 74 73 20 2a 2f 0a 20 20 20 20 20 20  xpects */.      
fa650 20 20 76 31 20 3d 20 73 71 6c 69 74 65 33 56 61    v1 = sqlite3Va
fa660 6c 75 65 54 65 78 74 28 28 73 71 6c 69 74 65 33  lueText((sqlite3
fa670 5f 76 61 6c 75 65 2a 29 70 4d 65 6d 31 2c 20 70  _value*)pMem1, p
fa680 43 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20 20  Coll->enc);.    
fa690 20 20 20 20 6e 31 20 3d 20 76 31 3d 3d 30 20 3f      n1 = v1==0 ?
fa6a0 20 30 20 3a 20 70 4d 65 6d 31 2d 3e 6e 3b 0a 20   0 : pMem1->n;. 
fa6b0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e         assert( n
fa6c0 31 3d 3d 73 71 6c 69 74 65 33 56 61 6c 75 65 42  1==sqlite3ValueB
fa6d0 79 74 65 73 28 28 73 71 6c 69 74 65 33 5f 76 61  ytes((sqlite3_va
fa6e0 6c 75 65 2a 29 70 4d 65 6d 31 2c 20 70 43 6f 6c  lue*)pMem1, pCol
fa6f0 6c 2d 3e 65 6e 63 29 20 29 3b 0a 20 20 20 20 20  l->enc) );.     
fa700 20 20 20 76 32 20 3d 20 73 71 6c 69 74 65 33 56     v2 = sqlite3V
fa710 61 6c 75 65 54 65 78 74 28 28 73 71 6c 69 74 65  alueText((sqlite
fa720 33 5f 76 61 6c 75 65 2a 29 70 4d 65 6d 32 2c 20  3_value*)pMem2, 
fa730 70 43 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20  pColl->enc);.   
fa740 20 20 20 20 20 6e 32 20 3d 20 76 32 3d 3d 30 20       n2 = v2==0 
fa750 3f 20 30 20 3a 20 70 4d 65 6d 32 2d 3e 6e 3b 0a  ? 0 : pMem2->n;.
fa760 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
fa770 6e 32 3d 3d 73 71 6c 69 74 65 33 56 61 6c 75 65  n2==sqlite3Value
fa780 42 79 74 65 73 28 28 73 71 6c 69 74 65 33 5f 76  Bytes((sqlite3_v
fa790 61 6c 75 65 2a 29 70 4d 65 6d 32 2c 20 70 43 6f  alue*)pMem2, pCo
fa7a0 6c 6c 2d 3e 65 6e 63 29 20 29 3b 0a 20 20 20 20  ll->enc) );.    
fa7b0 20 20 20 20 2f 2a 20 44 6f 20 74 68 65 20 63 6f      /* Do the co
fa7c0 6d 70 61 72 69 73 6f 6e 20 2a 2f 0a 20 20 20 20  mparison */.    
fa7d0 20 20 20 20 72 63 20 3d 20 70 43 6f 6c 6c 2d 3e      rc = pColl->
fa7e0 78 43 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55 73 65  xCmp(pColl->pUse
fa7f0 72 2c 20 6e 31 2c 20 76 31 2c 20 6e 32 2c 20 76  r, n1, v1, n2, v
fa800 32 29 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43  2);.        /* C
fa810 6f 6e 76 65 72 74 20 74 68 65 20 73 74 72 69 6e  onvert the strin
fa820 67 73 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65  gs back into the
fa830 20 64 61 74 61 62 61 73 65 20 65 6e 63 6f 64 69   database encodi
fa840 6e 67 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71  ng */.        sq
fa850 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 28  lite3ValueText((
fa860 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 70  sqlite3_value*)p
fa870 4d 65 6d 31 2c 20 6f 72 69 67 45 6e 63 29 3b 0a  Mem1, origEnc);.
fa880 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
fa890 61 6c 75 65 54 65 78 74 28 28 73 71 6c 69 74 65  alueText((sqlite
fa8a0 33 5f 76 61 6c 75 65 2a 29 70 4d 65 6d 32 2c 20  3_value*)pMem2, 
fa8b0 6f 72 69 67 45 6e 63 29 3b 0a 20 20 20 20 20 20  origEnc);.      
fa8c0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
fa8d0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 2f     }.    }.    /
fa8e0 2a 20 49 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  * If a NULL poin
fa8f0 74 65 72 20 77 61 73 20 70 61 73 73 65 64 20 61  ter was passed a
fa900 73 20 74 68 65 20 63 6f 6c 6c 61 74 65 20 66 75  s the collate fu
fa910 6e 63 74 69 6f 6e 2c 20 66 61 6c 6c 20 74 68 72  nction, fall thr
fa920 6f 75 67 68 0a 20 20 20 20 2a 2a 20 74 6f 20 74  ough.    ** to t
fa930 68 65 20 62 6c 6f 62 20 63 61 73 65 20 61 6e 64  he blob case and
fa940 20 75 73 65 20 6d 65 6d 63 6d 70 28 29 2e 20 20   use memcmp().  
fa950 2a 2f 0a 20 20 7d 0a 20 0a 20 20 2f 2a 20 42 6f  */.  }. .  /* Bo
fa960 74 68 20 76 61 6c 75 65 73 20 6d 75 73 74 20 62  th values must b
fa970 65 20 62 6c 6f 62 73 2e 20 20 43 6f 6d 70 61 72  e blobs.  Compar
fa980 65 20 75 73 69 6e 67 20 6d 65 6d 63 6d 70 28 29  e using memcmp()
fa990 2e 20 20 2a 2f 0a 20 20 72 63 20 3d 20 6d 65 6d  .  */.  rc = mem
fa9a0 63 6d 70 28 70 4d 65 6d 31 2d 3e 7a 2c 20 70 4d  cmp(pMem1->z, pM
fa9b0 65 6d 32 2d 3e 7a 2c 20 28 70 4d 65 6d 31 2d 3e  em2->z, (pMem1->
fa9c0 6e 3e 70 4d 65 6d 32 2d 3e 6e 29 3f 70 4d 65 6d  n>pMem2->n)?pMem
fa9d0 32 2d 3e 6e 3a 70 4d 65 6d 31 2d 3e 6e 29 3b 0a  2->n:pMem1->n);.
fa9e0 20 20 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20    if( rc==0 ){. 
fa9f0 20 20 20 72 63 20 3d 20 70 4d 65 6d 31 2d 3e 6e     rc = pMem1->n
faa00 20 2d 20 70 4d 65 6d 32 2d 3e 6e 3b 0a 20 20 7d   - pMem2->n;.  }
faa10 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
faa20 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 64 61 74 61  ./*.** Move data
faa30 20 6f 75 74 20 6f 66 20 61 20 62 74 72 65 65 20   out of a btree 
faa40 6b 65 79 20 6f 72 20 64 61 74 61 20 66 69 65 6c  key or data fiel
faa50 64 20 61 6e 64 20 69 6e 74 6f 20 61 20 4d 65 6d  d and into a Mem
faa60 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54   structure..** T
faa70 68 65 20 64 61 74 61 20 6f 72 20 6b 65 79 20 69  he data or key i
faa80 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65  s taken from the
faa90 20 65 6e 74 72 79 20 74 68 61 74 20 70 43 75 72   entry that pCur
faaa0 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f   is currently po
faab0 69 6e 74 69 6e 67 0a 2a 2a 20 74 6f 2e 20 20 6f  inting.** to.  o
faac0 66 66 73 65 74 20 61 6e 64 20 61 6d 74 20 64 65  ffset and amt de
faad0 74 65 72 6d 69 6e 65 20 77 68 61 74 20 70 6f 72  termine what por
faae0 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61  tion of the data
faaf0 20 6f 72 20 6b 65 79 20 74 6f 20 72 65 74 72 69   or key to retri
fab00 65 76 65 2e 0a 2a 2a 20 6b 65 79 20 69 73 20 74  eve..** key is t
fab10 72 75 65 20 74 6f 20 67 65 74 20 74 68 65 20 6b  rue to get the k
fab20 65 79 20 6f 72 20 66 61 6c 73 65 20 74 6f 20 67  ey or false to g
fab30 65 74 20 64 61 74 61 2e 20 20 54 68 65 20 72 65  et data.  The re
fab40 73 75 6c 74 20 69 73 20 77 72 69 74 74 65 6e 0a  sult is written.
fab50 2a 2a 20 69 6e 74 6f 20 74 68 65 20 70 4d 65 6d  ** into the pMem
fab60 20 65 6c 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20   element..**.** 
fab70 54 68 65 20 70 4d 65 6d 20 73 74 72 75 63 74 75  The pMem structu
fab80 72 65 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f  re is assumed to
fab90 20 62 65 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65   be uninitialize
faba0 64 2e 20 20 41 6e 79 20 70 72 69 6f 72 20 63 6f  d.  Any prior co
fabb0 6e 74 65 6e 74 0a 2a 2a 20 69 73 20 6f 76 65 72  ntent.** is over
fabc0 77 72 69 74 74 65 6e 20 77 69 74 68 6f 75 74 20  written without 
fabd0 62 65 69 6e 67 20 66 72 65 65 64 2e 0a 2a 2a 0a  being freed..**.
fabe0 2a 2a 20 49 66 20 74 68 69 73 20 72 6f 75 74 69  ** If this routi
fabf0 6e 65 20 66 61 69 6c 73 20 66 6f 72 20 61 6e 79  ne fails for any
fac00 20 72 65 61 73 6f 6e 20 28 6d 61 6c 6c 6f 63 20   reason (malloc 
fac10 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20 6f 72 20  returns NULL or 
fac20 75 6e 61 62 6c 65 0a 2a 2a 20 74 6f 20 72 65 61  unable.** to rea
fac30 64 20 66 72 6f 6d 20 74 68 65 20 64 69 73 6b 29  d from the disk)
fac40 20 74 68 65 6e 20 74 68 65 20 70 4d 65 6d 20 69   then the pMem i
fac50 73 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e 63  s left in an inc
fac60 6f 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65 2e  onsistent state.
fac70 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
fac80 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
fac90 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 0a  beMemFromBtree(.
faca0 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72    BtCursor *pCur
facb0 2c 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f  ,   /* Cursor po
facc0 69 6e 74 69 6e 67 20 61 74 20 72 65 63 6f 72 64  inting at record
facd0 20 74 6f 20 72 65 74 72 69 65 76 65 2e 20 2a 2f   to retrieve. */
face0 0a 20 20 69 6e 74 20 6f 66 66 73 65 74 2c 20 20  .  int offset,  
facf0 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 66       /* Offset f
fad00 72 6f 6d 20 74 68 65 20 73 74 61 72 74 20 6f 66  rom the start of
fad10 20 64 61 74 61 20 74 6f 20 72 65 74 75 72 6e 20   data to return 
fad20 62 79 74 65 73 20 66 72 6f 6d 2e 20 2a 2f 0a 20  bytes from. */. 
fad30 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20   int amt,       
fad40 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
fad50 62 79 74 65 73 20 74 6f 20 72 65 74 75 72 6e 2e  bytes to return.
fad60 20 2a 2f 0a 20 20 69 6e 74 20 6b 65 79 2c 20 20   */.  int key,  
fad70 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72          /* If tr
fad80 75 65 2c 20 72 65 74 72 69 65 76 65 20 66 72 6f  ue, retrieve fro
fad90 6d 20 74 68 65 20 62 74 72 65 65 20 6b 65 79 2c  m the btree key,
fada0 20 6e 6f 74 20 64 61 74 61 2e 20 2a 2f 0a 20 20   not data. */.  
fadb0 4d 65 6d 20 2a 70 4d 65 6d 20 20 20 20 20 20 20  Mem *pMem       
fadc0 20 20 2f 2a 20 4f 55 54 3a 20 52 65 74 75 72 6e    /* OUT: Return
fadd0 20 64 61 74 61 20 69 6e 20 74 68 69 73 20 4d 65   data in this Me
fade0 6d 20 73 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a  m structure. */.
fadf0 29 7b 0a 20 20 63 68 61 72 20 2a 7a 44 61 74 61  ){.  char *zData
fae00 3b 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20  ;       /* Data 
fae10 66 72 6f 6d 20 74 68 65 20 62 74 72 65 65 20 6c  from the btree l
fae20 61 79 65 72 20 2a 2f 0a 20 20 69 6e 74 20 61 76  ayer */.  int av
fae30 61 69 6c 61 62 6c 65 20 3d 20 30 3b 20 2f 2a 20  ailable = 0; /* 
fae40 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
fae50 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65  available on the
fae60 20 6c 6f 63 61 6c 20 62 74 72 65 65 20 70 61 67   local btree pag
fae70 65 20 2a 2f 0a 0a 20 20 69 66 28 20 6b 65 79 20  e */..  if( key 
fae80 29 7b 0a 20 20 20 20 7a 44 61 74 61 20 3d 20 28  ){.    zData = (
fae90 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 42 74  char *)sqlite3Bt
faea0 72 65 65 4b 65 79 46 65 74 63 68 28 70 43 75 72  reeKeyFetch(pCur
faeb0 2c 20 26 61 76 61 69 6c 61 62 6c 65 29 3b 0a 20  , &available);. 
faec0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 44 61 74   }else{.    zDat
faed0 61 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69  a = (char *)sqli
faee0 74 65 33 42 74 72 65 65 44 61 74 61 46 65 74 63  te3BtreeDataFetc
faef0 68 28 70 43 75 72 2c 20 26 61 76 61 69 6c 61 62  h(pCur, &availab
faf00 6c 65 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  le);.  }.  asser
faf10 74 28 20 7a 44 61 74 61 21 3d 30 20 29 3b 0a 0a  t( zData!=0 );..
faf20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 61 6d 74 3b    pMem->n = amt;
faf30 0a 20 20 69 66 28 20 6f 66 66 73 65 74 2b 61 6d  .  if( offset+am
faf40 74 3c 3d 61 76 61 69 6c 61 62 6c 65 20 29 7b 0a  t<=available ){.
faf50 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 26 7a      pMem->z = &z
faf60 44 61 74 61 5b 6f 66 66 73 65 74 5d 3b 0a 20 20  Data[offset];.  
faf70 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
faf80 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 45 70 68  MEM_Blob|MEM_Eph
faf90 65 6d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  em;.  }else{.   
fafa0 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 69 66 28   int rc;.    if(
fafb0 20 61 6d 74 3e 4e 42 46 53 2d 32 20 29 7b 0a 20   amt>NBFS-2 ){. 
fafc0 20 20 20 20 20 7a 44 61 74 61 20 3d 20 28 63 68       zData = (ch
fafd0 61 72 20 2a 29 73 71 6c 69 74 65 4d 61 6c 6c 6f  ar *)sqliteMallo
fafe0 63 52 61 77 28 61 6d 74 2b 32 29 3b 0a 20 20 20  cRaw(amt+2);.   
faff0 20 20 20 69 66 28 20 21 7a 44 61 74 61 20 29 7b     if( !zData ){
fb000 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
fb010 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
fb020 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4d 65 6d      }.      pMem
fb030 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c  ->flags = MEM_Bl
fb040 6f 62 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 54  ob|MEM_Dyn|MEM_T
fb050 65 72 6d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d  erm;.      pMem-
fb060 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20 7d  >xDel = 0;.    }
fb070 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 44 61 74  else{.      zDat
fb080 61 20 3d 20 26 28 70 4d 65 6d 2d 3e 7a 53 68 6f  a = &(pMem->zSho
fb090 72 74 5b 30 5d 29 3b 0a 20 20 20 20 20 20 70 4d  rt[0]);.      pM
fb0a0 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
fb0b0 42 6c 6f 62 7c 4d 45 4d 5f 53 68 6f 72 74 7c 4d  Blob|MEM_Short|M
fb0c0 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 7d 0a 20  EM_Term;.    }. 
fb0d0 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 7a 44 61     pMem->z = zDa
fb0e0 74 61 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e  ta;.    pMem->en
fb0f0 63 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d  c = 0;.    pMem-
fb100 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 42  >type = SQLITE_B
fb110 4c 4f 42 3b 0a 0a 20 20 20 20 69 66 28 20 6b 65  LOB;..    if( ke
fb120 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  y ){.      rc = 
fb130 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 28  sqlite3BtreeKey(
fb140 70 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d  pCur, offset, am
fb150 74 2c 20 7a 44 61 74 61 29 3b 0a 20 20 20 20 7d  t, zData);.    }
fb160 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d  else{.      rc =
fb170 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74   sqlite3BtreeDat
fb180 61 28 70 43 75 72 2c 20 6f 66 66 73 65 74 2c 20  a(pCur, offset, 
fb190 61 6d 74 2c 20 7a 44 61 74 61 29 3b 0a 20 20 20  amt, zData);.   
fb1a0 20 7d 0a 20 20 20 20 7a 44 61 74 61 5b 61 6d 74   }.    zData[amt
fb1b0 5d 20 3d 20 30 3b 0a 20 20 20 20 7a 44 61 74 61  ] = 0;.    zData
fb1c0 5b 61 6d 74 2b 31 5d 20 3d 20 30 3b 0a 20 20 20  [amt+1] = 0;.   
fb1d0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
fb1e0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  OK ){.      if( 
fb1f0 61 6d 74 3e 4e 42 46 53 2d 32 20 29 7b 0a 20 20  amt>NBFS-2 ){.  
fb200 20 20 20 20 20 20 61 73 73 65 72 74 28 20 7a 44        assert( zD
fb210 61 74 61 21 3d 70 4d 65 6d 2d 3e 7a 53 68 6f 72  ata!=pMem->zShor
fb220 74 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73  t );.        ass
fb230 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  ert( pMem->flags
fb240 20 26 20 4d 45 4d 5f 44 79 6e 20 29 3b 0a 20 20   & MEM_Dyn );.  
fb250 20 20 20 20 20 20 73 71 6c 69 74 65 46 72 65 65        sqliteFree
fb260 28 7a 44 61 74 61 29 3b 0a 20 20 20 20 20 20 7d  (zData);.      }
fb270 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 20   else {.        
fb280 61 73 73 65 72 74 28 20 7a 44 61 74 61 3d 3d 70  assert( zData==p
fb290 4d 65 6d 2d 3e 7a 53 68 6f 72 74 20 29 3b 0a 20  Mem->zShort );. 
fb2a0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
fb2b0 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Mem->flags & MEM
fb2c0 5f 53 68 6f 72 74 20 29 3b 0a 20 20 20 20 20 20  _Short );.      
fb2d0 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  }.      return r
fb2e0 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  c;.    }.  }..  
fb2f0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
fb300 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45  ;.}..#ifndef NDE
fb310 42 55 47 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72  BUG./*.** Perfor
fb320 6d 20 76 61 72 69 6f 75 73 20 63 68 65 63 6b 73  m various checks
fb330 20 6f 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 63   on the memory c
fb340 65 6c 6c 20 70 4d 65 6d 2e 20 41 6e 20 61 73 73  ell pMem. An ass
fb350 65 72 74 28 29 20 77 69 6c 6c 0a 2a 2a 20 66 61  ert() will.** fa
fb360 69 6c 20 69 66 20 70 4d 65 6d 20 69 73 20 69 6e  il if pMem is in
fb370 74 65 72 6e 61 6c 6c 79 20 69 6e 63 6f 6e 73 69  ternally inconsi
fb380 73 74 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  stent..*/.SQLITE
fb390 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
fb3a0 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 61 6e 69  lite3VdbeMemSani
fb3b0 74 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20  ty(Mem *pMem){. 
fb3c0 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 70 4d 65   int flags = pMe
fb3d0 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 61 73 73 65  m->flags;.  asse
fb3e0 72 74 28 20 66 6c 61 67 73 21 3d 30 20 29 3b 20  rt( flags!=0 ); 
fb3f0 20 2f 2a 20 4d 75 73 74 20 64 65 66 69 6e 65 20   /* Must define 
fb400 73 6f 6d 65 20 74 79 70 65 20 2a 2f 0a 20 20 69  some type */.  i
fb410 66 28 20 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f  f( flags & (MEM_
fb420 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b  Str|MEM_Blob) ){
fb430 0a 20 20 20 20 69 6e 74 20 78 20 3d 20 66 6c 61  .    int x = fla
fb440 67 73 20 26 20 28 4d 45 4d 5f 53 74 61 74 69 63  gs & (MEM_Static
fb450 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68  |MEM_Dyn|MEM_Eph
fb460 65 6d 7c 4d 45 4d 5f 53 68 6f 72 74 29 3b 0a 20  em|MEM_Short);. 
fb470 20 20 20 61 73 73 65 72 74 28 20 78 21 3d 30 20     assert( x!=0 
fb480 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  );            /*
fb490 20 53 74 72 69 6e 67 73 20 6d 75 73 74 20 64 65   Strings must de
fb4a0 66 69 6e 65 20 61 20 73 74 72 69 6e 67 20 73 75  fine a string su
fb4b0 62 74 79 70 65 20 2a 2f 0a 20 20 20 20 61 73 73  btype */.    ass
fb4c0 65 72 74 28 20 28 78 20 26 20 28 78 2d 31 29 29  ert( (x & (x-1))
fb4d0 3d 3d 30 20 29 3b 20 20 2f 2a 20 4f 6e 6c 79 20  ==0 );  /* Only 
fb4e0 6f 6e 65 20 73 74 72 69 6e 67 20 73 75 62 74 79  one string subty
fb4f0 70 65 20 63 61 6e 20 62 65 20 64 65 66 69 6e 65  pe can be define
fb500 64 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28  d */.    assert(
fb510 20 70 4d 65 6d 2d 3e 7a 21 3d 30 20 29 3b 20 20   pMem->z!=0 );  
fb520 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 73 20 6d      /* Strings m
fb530 75 73 74 20 68 61 76 65 20 61 20 76 61 6c 75 65  ust have a value
fb540 20 2a 2f 0a 20 20 20 20 2f 2a 20 4d 65 6d 2e 7a   */.    /* Mem.z
fb550 20 70 6f 69 6e 74 73 20 74 6f 20 4d 65 6d 2e 7a   points to Mem.z
fb560 53 68 6f 72 74 20 69 66 66 20 74 68 65 20 73 75  Short iff the su
fb570 62 74 79 70 65 20 69 73 20 4d 45 4d 5f 53 68 6f  btype is MEM_Sho
fb580 72 74 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74  rt */.    assert
fb590 28 20 28 78 20 26 20 4d 45 4d 5f 53 68 6f 72 74  ( (x & MEM_Short
fb5a0 29 3d 3d 30 20 7c 7c 20 70 4d 65 6d 2d 3e 7a 3d  )==0 || pMem->z=
fb5b0 3d 70 4d 65 6d 2d 3e 7a 53 68 6f 72 74 20 29 3b  =pMem->zShort );
fb5c0 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 78 20  .    assert( (x 
fb5d0 26 20 4d 45 4d 5f 53 68 6f 72 74 29 21 3d 30 20  & MEM_Short)!=0 
fb5e0 7c 7c 20 70 4d 65 6d 2d 3e 7a 21 3d 70 4d 65 6d  || pMem->z!=pMem
fb5f0 2d 3e 7a 53 68 6f 72 74 20 29 3b 0a 20 20 20 20  ->zShort );.    
fb600 2f 2a 20 4e 6f 20 64 65 73 74 72 75 63 74 6f 72  /* No destructor
fb610 20 75 6e 6c 65 73 73 20 74 68 65 72 65 20 69 73   unless there is
fb620 20 4d 45 4d 5f 44 79 6e 20 2a 2f 0a 20 20 20 20   MEM_Dyn */.    
fb630 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 78 44  assert( pMem->xD
fb640 65 6c 3d 3d 30 20 7c 7c 20 28 70 4d 65 6d 2d 3e  el==0 || (pMem->
fb650 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29  flags & MEM_Dyn)
fb660 21 3d 30 20 29 3b 0a 0a 20 20 20 20 69 66 28 20  !=0 );..    if( 
fb670 28 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72  (flags & MEM_Str
fb680 29 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  ) ){.      asser
fb690 74 28 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51  t( pMem->enc==SQ
fb6a0 4c 49 54 45 5f 55 54 46 38 20 7c 7c 20 0a 20 20  LITE_UTF8 || .  
fb6b0 20 20 20 20 20 20 20 20 20 20 20 20 70 4d 65 6d              pMem
fb6c0 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  ->enc==SQLITE_UT
fb6d0 46 31 36 42 45 20 7c 7c 0a 20 20 20 20 20 20 20  F16BE ||.       
fb6e0 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63         pMem->enc
fb6f0 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45  ==SQLITE_UTF16LE
fb700 20 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20   .      );.     
fb710 20 2f 2a 20 49 66 20 74 68 65 20 73 74 72 69 6e   /* If the strin
fb720 67 20 69 73 20 55 54 46 2d 38 20 65 6e 63 6f 64  g is UTF-8 encod
fb730 65 64 20 61 6e 64 20 6e 75 6c 20 74 65 72 6d 69  ed and nul termi
fb740 6e 61 74 65 64 2c 20 74 68 65 6e 20 70 4d 65 6d  nated, then pMem
fb750 2d 3e 6e 0a 20 20 20 20 20 20 2a 2a 20 6d 75 73  ->n.      ** mus
fb760 74 20 62 65 20 74 68 65 20 6c 65 6e 67 74 68 20  t be the length 
fb770 6f 66 20 74 68 65 20 73 74 72 69 6e 67 2e 20 20  of the string.  
fb780 28 4c 61 74 65 72 3a 29 20 20 49 66 20 74 68 65  (Later:)  If the
fb790 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 20   database file. 
fb7a0 20 20 20 20 20 2a 2a 20 68 61 73 20 62 65 65 6e       ** has been
fb7b0 20 63 6f 72 72 75 70 74 65 64 2c 20 27 5c 30 30   corrupted, '\00
fb7c0 30 27 20 63 68 61 72 61 63 74 65 72 73 20 6d 69  0' characters mi
fb7d0 67 68 74 20 68 61 76 65 20 62 65 65 6e 20 69 6e  ght have been in
fb7e0 73 65 72 74 65 64 0a 20 20 20 20 20 20 2a 2a 20  serted.      ** 
fb7f0 69 6e 74 6f 20 74 68 65 20 6d 69 64 64 6c 65 20  into the middle 
fb800 6f 66 20 74 68 65 20 73 74 72 69 6e 67 2e 20 20  of the string.  
fb810 49 6e 20 74 68 61 74 20 63 61 73 65 2c 20 74 68  In that case, th
fb820 65 20 73 74 72 6c 65 6e 28 29 20 6d 69 67 68 74  e strlen() might
fb830 0a 20 20 20 20 20 20 2a 2a 20 62 65 20 6c 65 73  .      ** be les
fb840 73 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  s..      */.    
fb850 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 6e 63 3d    if( pMem->enc=
fb860 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 26 26 20  =SQLITE_UTF8 && 
fb870 28 66 6c 61 67 73 20 26 20 4d 45 4d 5f 54 65 72  (flags & MEM_Ter
fb880 6d 29 20 29 7b 20 0a 20 20 20 20 20 20 20 20 61  m) ){ .        a
fb890 73 73 65 72 74 28 20 73 74 72 6c 65 6e 28 70 4d  ssert( strlen(pM
fb8a0 65 6d 2d 3e 7a 29 3c 3d 70 4d 65 6d 2d 3e 6e 20  em->z)<=pMem->n 
fb8b0 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  );.        asser
fb8c0 74 28 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d  t( pMem->z[pMem-
fb8d0 3e 6e 5d 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  >n]==0 );.      
fb8e0 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  }.    }.  }else{
fb8f0 0a 20 20 20 20 2f 2a 20 43 61 6e 6e 6f 74 20 64  .    /* Cannot d
fb900 65 66 69 6e 65 20 61 20 73 74 72 69 6e 67 20 73  efine a string s
fb910 75 62 74 79 70 65 20 66 6f 72 20 6e 6f 6e 2d 73  ubtype for non-s
fb920 74 72 69 6e 67 20 6f 62 6a 65 63 74 73 20 2a 2f  tring objects */
fb930 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 4d  .    assert( (pM
fb940 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d  em->flags & (MEM
fb950 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 7c  _Static|MEM_Dyn|
fb960 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 68  MEM_Ephem|MEM_Sh
fb970 6f 72 74 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20  ort))==0 );.    
fb980 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 78 44  assert( pMem->xD
fb990 65 6c 3d 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 2f  el==0 );.  }.  /
fb9a0 2a 20 4d 45 4d 5f 4e 75 6c 6c 20 65 78 63 6c 75  * MEM_Null exclu
fb9b0 64 65 73 20 61 6c 6c 20 6f 74 68 65 72 20 74 79  des all other ty
fb9c0 70 65 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  pes */.  assert(
fb9d0 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 28 4d   (pMem->flags&(M
fb9e0 45 4d 5f 53 74 72 7c 4d 45 4d 5f 49 6e 74 7c 4d  EM_Str|MEM_Int|M
fb9f0 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 42 6c 6f 62  EM_Real|MEM_Blob
fba00 29 29 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 20  ))==0.          
fba10 7c 7c 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26  || (pMem->flags&
fba20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 3b 0a  MEM_Null)==0 );.
fba30 20 20 2f 2a 20 49 66 20 74 68 65 20 4d 45 4d 20    /* If the MEM 
fba40 69 73 20 62 6f 74 68 20 72 65 61 6c 20 61 6e 64  is both real and
fba50 20 69 6e 74 65 67 65 72 2c 20 74 68 65 20 76 61   integer, the va
fba60 6c 75 65 73 20 61 72 65 20 65 71 75 61 6c 20 2a  lues are equal *
fba70 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65  /.  assert( (pMe
fba80 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f  m->flags & (MEM_
fba90 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 29 21 3d  Int|MEM_Real))!=
fbaa0 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61  (MEM_Int|MEM_Rea
fbab0 6c 29 20 0a 20 20 20 20 20 20 20 20 20 20 7c 7c  l) .          ||
fbac0 20 70 4d 65 6d 2d 3e 72 3d 3d 70 4d 65 6d 2d 3e   pMem->r==pMem->
fbad0 75 2e 69 20 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  u.i );.}.#endif.
fbae0 0a 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  ./* This functio
fbaf0 6e 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61  n is only availa
fbb00 62 6c 65 20 69 6e 74 65 72 6e 61 6c 6c 79 2c 20  ble internally, 
fbb10 69 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f  it is not part o
fbb20 66 20 74 68 65 0a 2a 2a 20 65 78 74 65 72 6e 61  f the.** externa
fbb30 6c 20 41 50 49 2e 20 49 74 20 77 6f 72 6b 73 20  l API. It works 
fbb40 69 6e 20 61 20 73 69 6d 69 6c 61 72 20 77 61 79  in a similar way
fbb50 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   to sqlite3_valu
fbb60 65 5f 74 65 78 74 28 29 2c 0a 2a 2a 20 65 78 63  e_text(),.** exc
fbb70 65 70 74 20 74 68 65 20 64 61 74 61 20 72 65 74  ept the data ret
fbb80 75 72 6e 65 64 20 69 73 20 69 6e 20 74 68 65 20  urned is in the 
fbb90 65 6e 63 6f 64 69 6e 67 20 73 70 65 63 69 66 69  encoding specifi
fbba0 65 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64  ed by the second
fbbb0 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 2c 20 77  .** parameter, w
fbbc0 68 69 63 68 20 6d 75 73 74 20 62 65 20 6f 6e 65  hich must be one
fbbd0 20 6f 66 20 53 51 4c 49 54 45 5f 55 54 46 31 36   of SQLITE_UTF16
fbbe0 42 45 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36  BE, SQLITE_UTF16
fbbf0 4c 45 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f  LE or.** SQLITE_
fbc00 55 54 46 38 2e 0a 2a 2a 0a 2a 2a 20 28 32 30 30  UTF8..**.** (200
fbc10 36 2d 30 32 2d 31 36 3a 29 20 20 54 68 65 20 65  6-02-16:)  The e
fbc20 6e 63 20 76 61 6c 75 65 20 63 61 6e 20 62 65 20  nc value can be 
fbc30 6f 72 2d 65 64 20 77 69 74 68 20 53 51 4c 49 54  or-ed with SQLIT
fbc40 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 2e  E_UTF16_ALIGNED.
fbc50 0a 2a 2a 20 49 66 20 74 68 61 74 20 69 73 20 74  .** If that is t
fbc60 68 65 20 63 61 73 65 2c 20 74 68 65 6e 20 74 68  he case, then th
fbc70 65 20 72 65 73 75 6c 74 20 6d 75 73 74 20 62 65  e result must be
fbc80 20 61 6c 69 67 6e 65 64 20 6f 6e 20 61 6e 20 65   aligned on an e
fbc90 76 65 6e 20 62 79 74 65 0a 2a 2a 20 62 6f 75 6e  ven byte.** boun
fbca0 64 61 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  dary..*/.SQLITE_
fbcb0 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f  PRIVATE const vo
fbcc0 69 64 20 2a 73 71 6c 69 74 65 33 56 61 6c 75 65  id *sqlite3Value
fbcd0 54 65 78 74 28 73 71 6c 69 74 65 33 5f 76 61 6c  Text(sqlite3_val
fbce0 75 65 2a 20 70 56 61 6c 2c 20 75 38 20 65 6e 63  ue* pVal, u8 enc
fbcf0 29 7b 0a 20 20 69 66 28 20 21 70 56 61 6c 20 29  ){.  if( !pVal )
fbd00 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 61 73 73   return 0;.  ass
fbd10 65 72 74 28 20 28 65 6e 63 26 33 29 3d 3d 28 65  ert( (enc&3)==(e
fbd20 6e 63 26 7e 53 51 4c 49 54 45 5f 55 54 46 31 36  nc&~SQLITE_UTF16
fbd30 5f 41 4c 49 47 4e 45 44 29 20 29 3b 0a 0a 20 20  _ALIGNED) );..  
fbd40 69 66 28 20 70 56 61 6c 2d 3e 66 6c 61 67 73 26  if( pVal->flags&
fbd50 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20  MEM_Null ){.    
fbd60 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
fbd70 61 73 73 65 72 74 28 20 28 4d 45 4d 5f 42 6c 6f  assert( (MEM_Blo
fbd80 62 3e 3e 33 29 20 3d 3d 20 4d 45 4d 5f 53 74 72  b>>3) == MEM_Str
fbd90 20 29 3b 0a 20 20 70 56 61 6c 2d 3e 66 6c 61 67   );.  pVal->flag
fbda0 73 20 7c 3d 20 28 70 56 61 6c 2d 3e 66 6c 61 67  s |= (pVal->flag
fbdb0 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 29 3e 3e 33  s & MEM_Blob)>>3
fbdc0 3b 0a 20 20 65 78 70 61 6e 64 42 6c 6f 62 28 70  ;.  expandBlob(p
fbdd0 56 61 6c 29 3b 0a 20 20 69 66 28 20 70 56 61 6c  Val);.  if( pVal
fbde0 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 20  ->flags&MEM_Str 
fbdf0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
fbe00 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67  beChangeEncoding
fbe10 28 70 56 61 6c 2c 20 65 6e 63 20 26 20 7e 53 51  (pVal, enc & ~SQ
fbe20 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e  LITE_UTF16_ALIGN
fbe30 45 44 29 3b 0a 20 20 20 20 69 66 28 20 28 65 6e  ED);.    if( (en
fbe40 63 20 26 20 53 51 4c 49 54 45 5f 55 54 46 31 36  c & SQLITE_UTF16
fbe50 5f 41 4c 49 47 4e 45 44 29 21 3d 30 20 26 26 20  _ALIGNED)!=0 && 
fbe60 31 3d 3d 28 31 26 28 69 6e 74 29 70 56 61 6c 2d  1==(1&(int)pVal-
fbe70 3e 7a 29 20 29 7b 0a 20 20 20 20 20 20 61 73 73  >z) ){.      ass
fbe80 65 72 74 28 20 28 70 56 61 6c 2d 3e 66 6c 61 67  ert( (pVal->flag
fbe90 73 20 26 20 28 4d 45 4d 5f 45 70 68 65 6d 7c 4d  s & (MEM_Ephem|M
fbea0 45 4d 5f 53 74 61 74 69 63 29 29 21 3d 30 20 29  EM_Static))!=0 )
fbeb0 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  ;.      if( sqli
fbec0 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72  te3VdbeMemMakeWr
fbed0 69 74 65 61 62 6c 65 28 70 56 61 6c 29 21 3d 53  iteable(pVal)!=S
fbee0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
fbef0 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
fbf00 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
fbf10 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75  sqlite3VdbeMemNu
fbf20 6c 54 65 72 6d 69 6e 61 74 65 28 70 56 61 6c 29  lTerminate(pVal)
fbf30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
fbf40 73 73 65 72 74 28 20 28 70 56 61 6c 2d 3e 66 6c  ssert( (pVal->fl
fbf50 61 67 73 26 4d 45 4d 5f 42 6c 6f 62 29 3d 3d 30  ags&MEM_Blob)==0
fbf60 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   );.    sqlite3V
fbf70 64 62 65 4d 65 6d 53 74 72 69 6e 67 69 66 79 28  dbeMemStringify(
fbf80 70 56 61 6c 2c 20 65 6e 63 29 3b 0a 20 20 20 20  pVal, enc);.    
fbf90 61 73 73 65 72 74 28 20 30 3d 3d 28 31 26 28 69  assert( 0==(1&(i
fbfa0 6e 74 29 70 56 61 6c 2d 3e 7a 29 20 29 3b 0a 20  nt)pVal->z) );. 
fbfb0 20 7d 0a 20 20 61 73 73 65 72 74 28 70 56 61 6c   }.  assert(pVal
fbfc0 2d 3e 65 6e 63 3d 3d 28 65 6e 63 20 26 20 7e 53  ->enc==(enc & ~S
fbfd0 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47  QLITE_UTF16_ALIG
fbfe0 4e 45 44 29 20 7c 7c 20 73 71 6c 69 74 65 33 4d  NED) || sqlite3M
fbff0 61 6c 6c 6f 63 46 61 69 6c 65 64 28 29 20 29 3b  allocFailed() );
fc000 0a 20 20 69 66 28 20 70 56 61 6c 2d 3e 65 6e 63  .  if( pVal->enc
fc010 3d 3d 28 65 6e 63 20 26 20 7e 53 51 4c 49 54 45  ==(enc & ~SQLITE
fc020 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 20  _UTF16_ALIGNED) 
fc030 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 56  ){.    return pV
fc040 61 6c 2d 3e 7a 3b 0a 20 20 7d 65 6c 73 65 7b 0a  al->z;.  }else{.
fc050 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
fc060 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74  }.}../*.** Creat
fc070 65 20 61 20 6e 65 77 20 73 71 6c 69 74 65 33 5f  e a new sqlite3_
fc080 76 61 6c 75 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f  value object..*/
fc090 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
fc0a0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 73  sqlite3_value *s
fc0b0 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 76  qlite3ValueNew(v
fc0c0 6f 69 64 29 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d  oid){.  Mem *p =
fc0d0 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 73 69   sqliteMalloc(si
fc0e0 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 20 69 66 28  zeof(*p));.  if(
fc0f0 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e 66 6c 61   p ){.    p->fla
fc100 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20  gs = MEM_Null;. 
fc110 20 20 20 70 2d 3e 74 79 70 65 20 3d 20 53 51 4c     p->type = SQL
fc120 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20 7d 0a 20 20  ITE_NULL;.  }.  
fc130 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a  return p;.}../*.
fc140 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  ** Create a new 
fc150 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62  sqlite3_value ob
fc160 6a 65 63 74 2c 20 63 6f 6e 74 61 69 6e 69 6e 67  ject, containing
fc170 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 70 45   the value of pE
fc180 78 70 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  xpr..**.** This 
fc190 6f 6e 6c 79 20 77 6f 72 6b 73 20 66 6f 72 20 76  only works for v
fc1a0 65 72 79 20 73 69 6d 70 6c 65 20 65 78 70 72 65  ery simple expre
fc1b0 73 73 69 6f 6e 73 20 74 68 61 74 20 63 6f 6e 73  ssions that cons
fc1c0 69 73 74 20 6f 66 20 6f 6e 65 20 63 6f 6e 73 74  ist of one const
fc1d0 61 6e 74 0a 2a 2a 20 74 6f 6b 65 6e 20 28 69 2e  ant.** token (i.
fc1e0 65 2e 20 22 35 22 2c 20 22 35 2e 31 22 2c 20 22  e. "5", "5.1", "
fc1f0 4e 55 4c 4c 22 2c 20 22 27 61 20 73 74 72 69 6e  NULL", "'a strin
fc200 67 27 22 29 2e 20 49 66 20 74 68 65 20 65 78 70  g'"). If the exp
fc210 72 65 73 73 69 6f 6e 20 63 61 6e 0a 2a 2a 20 62  ression can.** b
fc220 65 20 63 6f 6e 76 65 72 74 65 64 20 64 69 72 65  e converted dire
fc230 63 74 6c 79 20 69 6e 74 6f 20 61 20 76 61 6c 75  ctly into a valu
fc240 65 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75  e, then the valu
fc250 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61  e is allocated a
fc260 6e 64 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20  nd.** a pointer 
fc270 77 72 69 74 74 65 6e 20 74 6f 20 2a 70 70 56 61  written to *ppVa
fc280 6c 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 69 73  l. The caller is
fc290 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72   responsible for
fc2a0 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67 0a 2a 2a   deallocating.**
fc2b0 20 74 68 65 20 76 61 6c 75 65 20 62 79 20 70 61   the value by pa
fc2c0 73 73 69 6e 67 20 69 74 20 74 6f 20 73 71 6c 69  ssing it to sqli
fc2d0 74 65 33 56 61 6c 75 65 46 72 65 65 28 29 20 6c  te3ValueFree() l
fc2e0 61 74 65 72 20 6f 6e 2e 20 49 66 20 74 68 65 20  ater on. If the 
fc2f0 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 63 61  expression.** ca
fc300 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72 74 65  nnot be converte
fc310 64 20 74 6f 20 61 20 76 61 6c 75 65 2c 20 74 68  d to a value, th
fc320 65 6e 20 2a 70 70 56 61 6c 20 69 73 20 73 65 74  en *ppVal is set
fc330 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c   to NULL..*/.SQL
fc340 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
fc350 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d  sqlite3ValueFrom
fc360 45 78 70 72 28 0a 20 20 45 78 70 72 20 2a 70 45  Expr(.  Expr *pE
fc370 78 70 72 2c 20 0a 20 20 75 38 20 65 6e 63 2c 20  xpr, .  u8 enc, 
fc380 0a 20 20 75 38 20 61 66 66 69 6e 69 74 79 2c 0a  .  u8 affinity,.
fc390 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
fc3a0 2a 2a 70 70 56 61 6c 0a 29 7b 0a 20 20 69 6e 74  **ppVal.){.  int
fc3b0 20 6f 70 3b 0a 20 20 63 68 61 72 20 2a 7a 56 61   op;.  char *zVa
fc3c0 6c 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33  l = 0;.  sqlite3
fc3d0 5f 76 61 6c 75 65 20 2a 70 56 61 6c 20 3d 20 30  _value *pVal = 0
fc3e0 3b 0a 0a 20 20 69 66 28 20 21 70 45 78 70 72 20  ;..  if( !pExpr 
fc3f0 29 7b 0a 20 20 20 20 2a 70 70 56 61 6c 20 3d 20  ){.    *ppVal = 
fc400 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  0;.    return SQ
fc410 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 6f  LITE_OK;.  }.  o
fc420 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 0a  p = pExpr->op;..
fc430 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 53 54 52    if( op==TK_STR
fc440 49 4e 47 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 46 4c  ING || op==TK_FL
fc450 4f 41 54 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 49 4e  OAT || op==TK_IN
fc460 54 45 47 45 52 20 29 7b 0a 20 20 20 20 7a 56 61  TEGER ){.    zVa
fc470 6c 20 3d 20 73 71 6c 69 74 65 53 74 72 4e 44 75  l = sqliteStrNDu
fc480 70 28 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e  p((char*)pExpr->
fc490 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e  token.z, pExpr->
fc4a0 74 6f 6b 65 6e 2e 6e 29 3b 0a 20 20 20 20 70 56  token.n);.    pV
fc4b0 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75  al = sqlite3Valu
fc4c0 65 4e 65 77 28 29 3b 0a 20 20 20 20 69 66 28 20  eNew();.    if( 
fc4d0 21 7a 56 61 6c 20 7c 7c 20 21 70 56 61 6c 20 29  !zVal || !pVal )
fc4e0 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20   goto no_mem;.  
fc4f0 20 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65    sqlite3Dequote
fc500 28 7a 56 61 6c 29 3b 0a 20 20 20 20 73 71 6c 69  (zVal);.    sqli
fc510 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 70  te3ValueSetStr(p
fc520 56 61 6c 2c 20 2d 31 2c 20 7a 56 61 6c 2c 20 53  Val, -1, zVal, S
fc530 51 4c 49 54 45 5f 55 54 46 38 2c 20 73 71 6c 69  QLITE_UTF8, sqli
fc540 74 65 33 46 72 65 65 58 29 3b 0a 20 20 20 20 69  te3FreeX);.    i
fc550 66 28 20 28 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47  f( (op==TK_INTEG
fc560 45 52 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 46 4c 4f  ER || op==TK_FLO
fc570 41 54 20 29 20 26 26 20 61 66 66 69 6e 69 74 79  AT ) && affinity
fc580 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  ==SQLITE_AFF_NON
fc590 45 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  E ){.      sqlit
fc5a0 65 33 56 61 6c 75 65 41 70 70 6c 79 41 66 66 69  e3ValueApplyAffi
fc5b0 6e 69 74 79 28 70 56 61 6c 2c 20 53 51 4c 49 54  nity(pVal, SQLIT
fc5c0 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 2c 20 65  E_AFF_NUMERIC, e
fc5d0 6e 63 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  nc);.    }else{.
fc5e0 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c        sqlite3Val
fc5f0 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28  ueApplyAffinity(
fc600 70 56 61 6c 2c 20 61 66 66 69 6e 69 74 79 2c 20  pVal, affinity, 
fc610 65 6e 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  enc);.    }.  }e
fc620 6c 73 65 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 55  lse if( op==TK_U
fc630 4d 49 4e 55 53 20 29 20 7b 0a 20 20 20 20 69 66  MINUS ) {.    if
fc640 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71 6c  ( SQLITE_OK==sql
fc650 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70  ite3ValueFromExp
fc660 72 28 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  r(pExpr->pLeft, 
fc670 65 6e 63 2c 20 61 66 66 69 6e 69 74 79 2c 20 26  enc, affinity, &
fc680 70 56 61 6c 29 20 29 7b 0a 20 20 20 20 20 20 70  pVal) ){.      p
fc690 56 61 6c 2d 3e 75 2e 69 20 3d 20 2d 31 20 2a 20  Val->u.i = -1 * 
fc6a0 70 56 61 6c 2d 3e 75 2e 69 3b 0a 20 20 20 20 20  pVal->u.i;.     
fc6b0 20 70 56 61 6c 2d 3e 72 20 3d 20 2d 31 2e 30 20   pVal->r = -1.0 
fc6c0 2a 20 70 56 61 6c 2d 3e 72 3b 0a 20 20 20 20 7d  * pVal->r;.    }
fc6d0 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c  .  }.#ifndef SQL
fc6e0 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49  ITE_OMIT_BLOB_LI
fc6f0 54 45 52 41 4c 0a 20 20 65 6c 73 65 20 69 66 28  TERAL.  else if(
fc700 20 6f 70 3d 3d 54 4b 5f 42 4c 4f 42 20 29 7b 0a   op==TK_BLOB ){.
fc710 20 20 20 20 69 6e 74 20 6e 56 61 6c 3b 0a 20 20      int nVal;.  
fc720 20 20 70 56 61 6c 20 3d 20 73 71 6c 69 74 65 33    pVal = sqlite3
fc730 56 61 6c 75 65 4e 65 77 28 29 3b 0a 20 20 20 20  ValueNew();.    
fc740 7a 56 61 6c 20 3d 20 73 71 6c 69 74 65 53 74 72  zVal = sqliteStr
fc750 4e 44 75 70 28 28 63 68 61 72 2a 29 70 45 78 70  NDup((char*)pExp
fc760 72 2d 3e 74 6f 6b 65 6e 2e 7a 2b 31 2c 20 70 45  r->token.z+1, pE
fc770 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 2d 31 29 3b  xpr->token.n-1);
fc780 0a 20 20 20 20 69 66 28 20 21 7a 56 61 6c 20 7c  .    if( !zVal |
fc790 7c 20 21 70 56 61 6c 20 29 20 67 6f 74 6f 20 6e  | !pVal ) goto n
fc7a0 6f 5f 6d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74  o_mem;.    sqlit
fc7b0 65 33 44 65 71 75 6f 74 65 28 7a 56 61 6c 29 3b  e3Dequote(zVal);
fc7c0 0a 20 20 20 20 6e 56 61 6c 20 3d 20 73 74 72 6c  .    nVal = strl
fc7d0 65 6e 28 7a 56 61 6c 29 2f 32 3b 0a 20 20 20 20  en(zVal)/2;.    
fc7e0 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
fc7f0 74 53 74 72 28 70 56 61 6c 2c 20 73 71 6c 69 74  tStr(pVal, sqlit
fc800 65 33 48 65 78 54 6f 42 6c 6f 62 28 7a 56 61 6c  e3HexToBlob(zVal
fc810 29 2c 20 6e 56 61 6c 2c 20 30 2c 20 73 71 6c 69  ), nVal, 0, sqli
fc820 74 65 33 46 72 65 65 58 29 3b 0a 20 20 20 20 73  te3FreeX);.    s
fc830 71 6c 69 74 65 46 72 65 65 28 7a 56 61 6c 29 3b  qliteFree(zVal);
fc840 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2a  .  }.#endif..  *
fc850 70 70 56 61 6c 20 3d 20 70 56 61 6c 3b 0a 20 20  ppVal = pVal;.  
fc860 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
fc870 3b 0a 0a 6e 6f 5f 6d 65 6d 3a 0a 20 20 73 71 6c  ;..no_mem:.  sql
fc880 69 74 65 46 72 65 65 28 7a 56 61 6c 29 3b 0a 20  iteFree(zVal);. 
fc890 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65   sqlite3ValueFre
fc8a0 65 28 70 56 61 6c 29 3b 0a 20 20 2a 70 70 56 61  e(pVal);.  *ppVa
fc8b0 6c 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20  l = 0;.  return 
fc8c0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 7d 0a  SQLITE_NOMEM;.}.
fc8d0 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
fc8e0 65 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 6f  e string value o
fc8f0 66 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c  f an sqlite3_val
fc900 75 65 20 6f 62 6a 65 63 74 0a 2a 2f 0a 53 51 4c  ue object.*/.SQL
fc910 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
fc920 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74   sqlite3ValueSet
fc930 53 74 72 28 0a 20 20 73 71 6c 69 74 65 33 5f 76  Str(.  sqlite3_v
fc940 61 6c 75 65 20 2a 76 2c 20 0a 20 20 69 6e 74 20  alue *v, .  int 
fc950 6e 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  n, .  const void
fc960 20 2a 7a 2c 20 0a 20 20 75 38 20 65 6e 63 2c 0a   *z, .  u8 enc,.
fc970 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76    void (*xDel)(v
fc980 6f 69 64 2a 29 0a 29 7b 0a 20 20 69 66 28 20 76  oid*).){.  if( v
fc990 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65   ) sqlite3VdbeMe
fc9a0 6d 53 65 74 53 74 72 28 28 4d 65 6d 20 2a 29 76  mSetStr((Mem *)v
fc9b0 2c 20 7a 2c 20 6e 2c 20 65 6e 63 2c 20 78 44 65  , z, n, enc, xDe
fc9c0 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65  l);.}../*.** Fre
fc9d0 65 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c  e an sqlite3_val
fc9e0 75 65 20 6f 62 6a 65 63 74 0a 2a 2f 0a 53 51 4c  ue object.*/.SQL
fc9f0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
fca00 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65   sqlite3ValueFre
fca10 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  e(sqlite3_value 
fca20 2a 76 29 7b 0a 20 20 69 66 28 20 21 76 20 29 20  *v){.  if( !v ) 
fca30 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65  return;.  sqlite
fca40 33 56 61 6c 75 65 53 65 74 53 74 72 28 76 2c 20  3ValueSetStr(v, 
fca50 30 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55 54 46  0, 0, SQLITE_UTF
fca60 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  8, SQLITE_STATIC
fca70 29 3b 0a 20 20 73 71 6c 69 74 65 46 72 65 65 28  );.  sqliteFree(
fca80 76 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  v);.}../*.** Ret
fca90 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
fcaa0 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 73  f bytes in the s
fcab0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a  qlite3_value obj
fcac0 65 63 74 20 61 73 73 75 6d 69 6e 67 0a 2a 2a 20  ect assuming.** 
fcad0 74 68 61 74 20 69 74 20 75 73 65 73 20 74 68 65  that it uses the
fcae0 20 65 6e 63 6f 64 69 6e 67 20 22 65 6e 63 22 0a   encoding "enc".
fcaf0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
fcb00 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 6c  E int sqlite3Val
fcb10 75 65 42 79 74 65 73 28 73 71 6c 69 74 65 33 5f  ueBytes(sqlite3_
fcb20 76 61 6c 75 65 20 2a 70 56 61 6c 2c 20 75 38 20  value *pVal, u8 
fcb30 65 6e 63 29 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d  enc){.  Mem *p =
fcb40 20 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a 20 20 69   (Mem*)pVal;.  i
fcb50 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 4d  f( (p->flags & M
fcb60 45 4d 5f 42 6c 6f 62 29 21 3d 30 20 7c 7c 20 73  EM_Blob)!=0 || s
fcb70 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28  qlite3ValueText(
fcb80 70 56 61 6c 2c 20 65 6e 63 29 20 29 7b 0a 20 20  pVal, enc) ){.  
fcb90 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26    if( p->flags &
fcba0 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20   MEM_Zero ){.   
fcbb0 20 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 2b 70     return p->n+p
fcbc0 2d 3e 75 2e 69 3b 0a 20 20 20 20 7d 65 6c 73 65  ->u.i;.    }else
fcbd0 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 70  {.      return p
fcbe0 2d 3e 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ->n;.    }.  }. 
fcbf0 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
fcc00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
fcc10 64 20 6f 66 20 76 64 62 65 6d 65 6d 2e 63 20 2a  d of vdbemem.c *
fcc20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fcc30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fcc40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
fcc50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
fcc60 67 69 6e 20 66 69 6c 65 20 76 64 62 65 61 75 78  gin file vdbeaux
fcc70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
fcc80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fcc90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
fcca0 0a 2a 2a 20 32 30 30 33 20 53 65 70 74 65 6d 62  .** 2003 Septemb
fccb0 65 72 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  er 6.**.** The a
fccc0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
fccd0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
fcce0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
fccf0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
fcd00 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
fcd10 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
fcd20 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
fcd30 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
fcd40 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
fcd50 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
fcd60 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
fcd70 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
fcd80 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
fcd90 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
fcda0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
fcdb0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
fcdc0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
fcdd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fcde0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fcdf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fce00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fce10 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
fce20 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75   contains code u
fce30 73 65 64 20 66 6f 72 20 63 72 65 61 74 69 6e 67  sed for creating
fce40 2c 20 64 65 73 74 72 6f 79 69 6e 67 2c 20 61 6e  , destroying, an
fce50 64 20 70 6f 70 75 6c 61 74 69 6e 67 0a 2a 2a 20  d populating.** 
fce60 61 20 56 44 42 45 20 28 6f 72 20 61 6e 20 22 73  a VDBE (or an "s
fce70 71 6c 69 74 65 33 5f 73 74 6d 74 22 20 61 73 20  qlite3_stmt" as 
fce80 69 74 20 69 73 20 6b 6e 6f 77 6e 20 74 6f 20 74  it is known to t
fce90 68 65 20 6f 75 74 73 69 64 65 20 77 6f 72 6c 64  he outside world
fcea0 2e 29 20 20 50 72 69 6f 72 0a 2a 2a 20 74 6f 20  .)  Prior.** to 
fceb0 76 65 72 73 69 6f 6e 20 32 2e 38 2e 37 2c 20 61  version 2.8.7, a
fcec0 6c 6c 20 74 68 69 73 20 63 6f 64 65 20 77 61 73  ll this code was
fced0 20 63 6f 6d 62 69 6e 65 64 20 69 6e 74 6f 20 74   combined into t
fcee0 68 65 20 76 64 62 65 2e 63 20 73 6f 75 72 63 65  he vdbe.c source
fcef0 20 66 69 6c 65 2e 0a 2a 2a 20 42 75 74 20 74 68   file..** But th
fcf00 61 74 20 66 69 6c 65 20 77 61 73 20 67 65 74 74  at file was gett
fcf10 69 6e 67 20 74 6f 6f 20 62 69 67 20 73 6f 20 74  ing too big so t
fcf20 68 69 73 20 73 75 62 72 6f 75 74 69 6e 65 73 20  his subroutines 
fcf30 77 65 72 65 20 73 70 6c 69 74 20 6f 75 74 2e 0a  were split out..
fcf40 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20  */.../*.** When 
fcf50 64 65 62 75 67 67 69 6e 67 20 74 68 65 20 63 6f  debugging the co
fcf60 64 65 20 67 65 6e 65 72 61 74 6f 72 20 69 6e 20  de generator in 
fcf70 61 20 73 79 6d 62 6f 6c 69 63 20 64 65 62 75 67  a symbolic debug
fcf80 67 65 72 2c 20 6f 6e 65 20 63 61 6e 0a 2a 2a 20  ger, one can.** 
fcf90 73 65 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f  set the sqlite3_
fcfa0 76 64 62 65 5f 61 64 64 6f 70 5f 74 72 61 63 65  vdbe_addop_trace
fcfb0 20 74 6f 20 31 20 61 6e 64 20 61 6c 6c 20 6f 70   to 1 and all op
fcfc0 63 6f 64 65 73 20 77 69 6c 6c 20 62 65 20 70 72  codes will be pr
fcfd0 69 6e 74 65 64 0a 2a 2a 20 61 73 20 74 68 65 79  inted.** as they
fcfe0 20 61 72 65 20 61 64 64 65 64 20 74 6f 20 74 68   are added to th
fcff0 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 73 74  e instruction st
fd000 72 65 61 6d 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  ream..*/.#ifdef 
fd010 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 69 6e 74  SQLITE_DEBUG.int
fd020 20 73 71 6c 69 74 65 33 5f 76 64 62 65 5f 61 64   sqlite3_vdbe_ad
fd030 64 6f 70 5f 74 72 61 63 65 20 3d 20 30 3b 0a 23  dop_trace = 0;.#
fd040 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43 72  endif.../*.** Cr
fd050 65 61 74 65 20 61 20 6e 65 77 20 76 69 72 74 75  eate a new virtu
fd060 61 6c 20 64 61 74 61 62 61 73 65 20 65 6e 67 69  al database engi
fd070 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ne..*/.SQLITE_PR
fd080 49 56 41 54 45 20 56 64 62 65 20 2a 73 71 6c 69  IVATE Vdbe *sqli
fd090 74 65 33 56 64 62 65 43 72 65 61 74 65 28 73 71  te3VdbeCreate(sq
fd0a0 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 56 64  lite3 *db){.  Vd
fd0b0 62 65 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c  be *p;.  p = sql
fd0c0 69 74 65 4d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f  iteMalloc( sizeo
fd0d0 66 28 56 64 62 65 29 20 29 3b 0a 20 20 69 66 28  f(Vdbe) );.  if(
fd0e0 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30   p==0 ) return 0
fd0f0 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a  ;.  p->db = db;.
fd100 20 20 69 66 28 20 64 62 2d 3e 70 56 64 62 65 20    if( db->pVdbe 
fd110 29 7b 0a 20 20 20 20 64 62 2d 3e 70 56 64 62 65  ){.    db->pVdbe
fd120 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20 20 7d  ->pPrev = p;.  }
fd130 0a 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 64 62  .  p->pNext = db
fd140 2d 3e 70 56 64 62 65 3b 0a 20 20 70 2d 3e 70 50  ->pVdbe;.  p->pP
fd150 72 65 76 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 70  rev = 0;.  db->p
fd160 56 64 62 65 20 3d 20 70 3b 0a 20 20 70 2d 3e 6d  Vdbe = p;.  p->m
fd170 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49  agic = VDBE_MAGI
fd180 43 5f 49 4e 49 54 3b 0a 20 20 72 65 74 75 72 6e  C_INIT;.  return
fd190 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d   p;.}../*.** Rem
fd1a0 65 6d 62 65 72 20 74 68 65 20 53 51 4c 20 73 74  ember the SQL st
fd1b0 72 69 6e 67 20 66 6f 72 20 61 20 70 72 65 70 61  ring for a prepa
fd1c0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  red statement..*
fd1d0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
fd1e0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
fd1f0 65 53 65 74 53 71 6c 28 56 64 62 65 20 2a 70 2c  eSetSql(Vdbe *p,
fd200 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20   const char *z, 
fd210 69 6e 74 20 6e 29 7b 0a 20 20 69 66 28 20 70 3d  int n){.  if( p=
fd220 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61  =0 ) return;.  a
fd230 73 73 65 72 74 28 20 70 2d 3e 7a 53 71 6c 3d 3d  ssert( p->zSql==
fd240 30 20 29 3b 0a 20 20 70 2d 3e 7a 53 71 6c 20 3d  0 );.  p->zSql =
fd250 20 73 71 6c 69 74 65 33 53 74 72 4e 44 75 70 28   sqlite3StrNDup(
fd260 7a 2c 20 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  z, n);.}../*.** 
fd270 52 65 74 75 72 6e 20 74 68 65 20 53 51 4c 20 61  Return the SQL a
fd280 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61  ssociated with a
fd290 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
fd2a0 65 6e 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ent.*/.SQLITE_PR
fd2b0 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72  IVATE const char
fd2c0 20 2a 73 71 6c 69 74 65 33 56 64 62 65 47 65 74   *sqlite3VdbeGet
fd2d0 53 71 6c 28 56 64 62 65 20 2a 70 29 7b 0a 20 20  Sql(Vdbe *p){.  
fd2e0 72 65 74 75 72 6e 20 70 2d 3e 7a 53 71 6c 3b 0a  return p->zSql;.
fd2f0 7d 0a 0a 2f 2a 0a 2a 2a 20 53 77 61 70 20 61 6c  }../*.** Swap al
fd300 6c 20 63 6f 6e 74 65 6e 74 20 62 65 74 77 65 65  l content betwee
fd310 6e 20 74 77 6f 20 56 44 42 45 20 73 74 72 75 63  n two VDBE struc
fd320 74 75 72 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  tures..*/.SQLITE
fd330 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
fd340 6c 69 74 65 33 56 64 62 65 53 77 61 70 28 56 64  lite3VdbeSwap(Vd
fd350 62 65 20 2a 70 41 2c 20 56 64 62 65 20 2a 70 42  be *pA, Vdbe *pB
fd360 29 7b 0a 20 20 56 64 62 65 20 74 6d 70 2c 20 2a  ){.  Vdbe tmp, *
fd370 70 54 6d 70 3b 0a 20 20 63 68 61 72 20 2a 7a 54  pTmp;.  char *zT
fd380 6d 70 3b 0a 20 20 69 6e 74 20 6e 54 6d 70 3b 0a  mp;.  int nTmp;.
fd390 20 20 74 6d 70 20 3d 20 2a 70 41 3b 0a 20 20 2a    tmp = *pA;.  *
fd3a0 70 41 20 3d 20 2a 70 42 3b 0a 20 20 2a 70 42 20  pA = *pB;.  *pB 
fd3b0 3d 20 74 6d 70 3b 0a 20 20 70 54 6d 70 20 3d 20  = tmp;.  pTmp = 
fd3c0 70 41 2d 3e 70 4e 65 78 74 3b 0a 20 20 70 41 2d  pA->pNext;.  pA-
fd3d0 3e 70 4e 65 78 74 20 3d 20 70 42 2d 3e 70 4e 65  >pNext = pB->pNe
fd3e0 78 74 3b 0a 20 20 70 42 2d 3e 70 4e 65 78 74 20  xt;.  pB->pNext 
fd3f0 3d 20 70 54 6d 70 3b 0a 20 20 70 54 6d 70 20 3d  = pTmp;.  pTmp =
fd400 20 70 41 2d 3e 70 50 72 65 76 3b 0a 20 20 70 41   pA->pPrev;.  pA
fd410 2d 3e 70 50 72 65 76 20 3d 20 70 42 2d 3e 70 50  ->pPrev = pB->pP
fd420 72 65 76 3b 0a 20 20 70 42 2d 3e 70 50 72 65 76  rev;.  pB->pPrev
fd430 20 3d 20 70 54 6d 70 3b 0a 20 20 7a 54 6d 70 20   = pTmp;.  zTmp 
fd440 3d 20 70 41 2d 3e 7a 53 71 6c 3b 0a 20 20 70 41  = pA->zSql;.  pA
fd450 2d 3e 7a 53 71 6c 20 3d 20 70 42 2d 3e 7a 53 71  ->zSql = pB->zSq
fd460 6c 3b 0a 20 20 70 42 2d 3e 7a 53 71 6c 20 3d 20  l;.  pB->zSql = 
fd470 7a 54 6d 70 3b 0a 20 20 6e 54 6d 70 20 3d 20 70  zTmp;.  nTmp = p
fd480 41 2d 3e 6e 53 71 6c 3b 0a 20 20 70 41 2d 3e 6e  A->nSql;.  pA->n
fd490 53 71 6c 20 3d 20 70 42 2d 3e 6e 53 71 6c 3b 0a  Sql = pB->nSql;.
fd4a0 20 20 70 42 2d 3e 6e 53 71 6c 20 3d 20 6e 54 6d    pB->nSql = nTm
fd4b0 70 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c  p;.}..#ifdef SQL
fd4c0 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20  ITE_DEBUG./*.** 
fd4d0 54 75 72 6e 20 74 72 61 63 69 6e 67 20 6f 6e 20  Turn tracing on 
fd4e0 6f 72 20 6f 66 66 0a 2a 2f 0a 53 51 4c 49 54 45  or off.*/.SQLITE
fd4f0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
fd500 6c 69 74 65 33 56 64 62 65 54 72 61 63 65 28 56  lite3VdbeTrace(V
fd510 64 62 65 20 2a 70 2c 20 46 49 4c 45 20 2a 74 72  dbe *p, FILE *tr
fd520 61 63 65 29 7b 0a 20 20 70 2d 3e 74 72 61 63 65  ace){.  p->trace
fd530 20 3d 20 74 72 61 63 65 3b 0a 7d 0a 23 65 6e 64   = trace;.}.#end
fd540 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 69 7a 65  if../*.** Resize
fd550 20 74 68 65 20 56 64 62 65 2e 61 4f 70 20 61 72   the Vdbe.aOp ar
fd560 72 61 79 20 73 6f 20 74 68 61 74 20 69 74 20 63  ray so that it c
fd570 6f 6e 74 61 69 6e 73 20 61 74 20 6c 65 61 73 74  ontains at least
fd580 20 4e 0a 2a 2a 20 65 6c 65 6d 65 6e 74 73 2e 20   N.** elements. 
fd590 49 66 20 74 68 65 20 56 64 62 65 20 69 73 20 69  If the Vdbe is i
fd5a0 6e 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e  n VDBE_MAGIC_RUN
fd5b0 20 73 74 61 74 65 2c 20 74 68 65 6e 0a 2a 2a 20   state, then.** 
fd5c0 74 68 65 20 56 64 62 65 2e 61 4f 70 20 61 72 72  the Vdbe.aOp arr
fd5d0 61 79 20 77 69 6c 6c 20 62 65 20 73 69 7a 65 64  ay will be sized
fd5e0 20 74 6f 20 63 6f 6e 74 61 69 6e 20 65 78 61 63   to contain exac
fd5f0 74 6c 79 20 4e 0a 2a 2a 20 65 6c 65 6d 65 6e 74  tly N.** element
fd600 73 2e 20 56 64 62 65 2e 6e 4f 70 41 6c 6c 6f 63  s. Vdbe.nOpAlloc
fd610 20 69 73 20 73 65 74 20 74 6f 20 72 65 66 6c 65   is set to refle
fd620 63 74 20 74 68 65 20 6e 65 77 20 73 69 7a 65 20  ct the new size 
fd630 6f 66 0a 2a 2a 20 74 68 65 20 61 72 72 61 79 2e  of.** the array.
fd640 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 6f 75 74  .**.** If an out
fd650 2d 6f 66 2d 6d 65 6d 6f 72 79 20 65 72 72 6f 72  -of-memory error
fd660 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 72 65   occurs while re
fd670 73 69 7a 69 6e 67 20 74 68 65 20 61 72 72 61 79  sizing the array
fd680 2c 0a 2a 2a 20 56 64 62 65 2e 61 4f 70 20 61 6e  ,.** Vdbe.aOp an
fd690 64 20 56 64 62 65 2e 6e 4f 70 41 6c 6c 6f 63 20  d Vdbe.nOpAlloc 
fd6a0 72 65 6d 61 69 6e 20 75 6e 63 68 61 6e 67 65 64  remain unchanged
fd6b0 20 28 74 68 69 73 20 69 73 20 73 6f 20 74 68 61   (this is so tha
fd6c0 74 0a 2a 2a 20 61 6e 79 20 6f 70 63 6f 64 65 73  t.** any opcodes
fd6d0 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63 61 74   already allocat
fd6e0 65 64 20 63 61 6e 20 62 65 20 63 6f 72 72 65 63  ed can be correc
fd6f0 74 6c 79 20 64 65 61 6c 6c 6f 63 61 74 65 64 0a  tly deallocated.
fd700 2a 2a 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68  ** along with th
fd710 65 20 72 65 73 74 20 6f 66 20 74 68 65 20 56 64  e rest of the Vd
fd720 62 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  be)..*/.static v
fd730 6f 69 64 20 72 65 73 69 7a 65 4f 70 41 72 72 61  oid resizeOpArra
fd740 79 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 4e  y(Vdbe *p, int N
fd750 29 7b 0a 20 20 69 6e 74 20 72 75 6e 4d 6f 64 65  ){.  int runMode
fd760 20 3d 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42   = p->magic==VDB
fd770 45 5f 4d 41 47 49 43 5f 52 55 4e 3b 0a 20 20 69  E_MAGIC_RUN;.  i
fd780 66 28 20 72 75 6e 4d 6f 64 65 20 7c 7c 20 70 2d  f( runMode || p-
fd790 3e 6e 4f 70 41 6c 6c 6f 63 3c 4e 20 29 7b 0a 20  >nOpAlloc<N ){. 
fd7a0 20 20 20 56 64 62 65 4f 70 20 2a 70 4e 65 77 3b     VdbeOp *pNew;
fd7b0 0a 20 20 20 20 69 6e 74 20 6e 4e 65 77 20 3d 20  .    int nNew = 
fd7c0 4e 20 2b 20 31 30 30 2a 28 21 72 75 6e 4d 6f 64  N + 100*(!runMod
fd7d0 65 29 3b 0a 20 20 20 20 69 6e 74 20 6f 6c 64 53  e);.    int oldS
fd7e0 69 7a 65 20 3d 20 70 2d 3e 6e 4f 70 41 6c 6c 6f  ize = p->nOpAllo
fd7f0 63 3b 0a 20 20 20 20 70 4e 65 77 20 3d 20 73 71  c;.    pNew = sq
fd800 6c 69 74 65 52 65 61 6c 6c 6f 63 28 70 2d 3e 61  liteRealloc(p->a
fd810 4f 70 2c 20 6e 4e 65 77 2a 73 69 7a 65 6f 66 28  Op, nNew*sizeof(
fd820 4f 70 29 29 3b 0a 20 20 20 20 69 66 28 20 70 4e  Op));.    if( pN
fd830 65 77 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e  ew ){.      p->n
fd840 4f 70 41 6c 6c 6f 63 20 3d 20 6e 4e 65 77 3b 0a  OpAlloc = nNew;.
fd850 20 20 20 20 20 20 70 2d 3e 61 4f 70 20 3d 20 70        p->aOp = p
fd860 4e 65 77 3b 0a 20 20 20 20 20 20 69 66 28 20 6e  New;.      if( n
fd870 4e 65 77 3e 6f 6c 64 53 69 7a 65 20 29 7b 0a 20  New>oldSize ){. 
fd880 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 70         memset(&p
fd890 2d 3e 61 4f 70 5b 6f 6c 64 53 69 7a 65 5d 2c 20  ->aOp[oldSize], 
fd8a0 30 2c 20 28 6e 4e 65 77 2d 6f 6c 64 53 69 7a 65  0, (nNew-oldSize
fd8b0 29 2a 73 69 7a 65 6f 66 28 4f 70 29 29 3b 0a 20  )*sizeof(Op));. 
fd8c0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
fd8d0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20  .}../*.** Add a 
fd8e0 6e 65 77 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  new instruction 
fd8f0 74 6f 20 74 68 65 20 6c 69 73 74 20 6f 66 20 69  to the list of i
fd900 6e 73 74 72 75 63 74 69 6f 6e 73 20 63 75 72 72  nstructions curr
fd910 65 6e 74 20 69 6e 20 74 68 65 0a 2a 2a 20 56 44  ent in the.** VD
fd920 42 45 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20  BE.  Return the 
fd930 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 6e  address of the n
fd940 65 77 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a  ew instruction..
fd950 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 73  **.** Parameters
fd960 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 70 20 20 20 20  :.**.**    p    
fd970 20 20 20 20 20 20 20 20 20 20 20 50 6f 69 6e 74             Point
fd980 65 72 20 74 6f 20 74 68 65 20 56 44 42 45 0a 2a  er to the VDBE.*
fd990 2a 0a 2a 2a 20 20 20 20 6f 70 20 20 20 20 20 20  *.**    op      
fd9a0 20 20 20 20 20 20 20 20 54 68 65 20 6f 70 63 6f          The opco
fd9b0 64 65 20 66 6f 72 20 74 68 69 73 20 69 6e 73 74  de for this inst
fd9c0 72 75 63 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 20 20  ruction.**.**   
fd9d0 20 70 31 2c 20 70 32 20 20 20 20 20 20 20 20 20   p1, p2         
fd9e0 20 46 69 72 73 74 20 74 77 6f 20 6f 66 20 74 68   First two of th
fd9f0 65 20 74 68 72 65 65 20 70 6f 73 73 69 62 6c 65  e three possible
fda00 20 6f 70 65 72 61 6e 64 73 2e 0a 2a 2a 0a 2a 2a   operands..**.**
fda10 20 55 73 65 20 74 68 65 20 73 71 6c 69 74 65 33   Use the sqlite3
fda20 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
fda30 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 66  () function to f
fda40 69 78 20 61 6e 20 61 64 64 72 65 73 73 20 61 6e  ix an address an
fda50 64 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33  d.** the sqlite3
fda60 56 64 62 65 43 68 61 6e 67 65 50 33 28 29 20 66  VdbeChangeP3() f
fda70 75 6e 63 74 69 6f 6e 20 74 6f 20 63 68 61 6e 67  unction to chang
fda80 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  e the value of t
fda90 68 65 20 50 33 0a 2a 2a 20 6f 70 65 72 61 6e 64  he P3.** operand
fdaa0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
fdab0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
fdac0 64 62 65 41 64 64 4f 70 28 56 64 62 65 20 2a 70  dbeAddOp(Vdbe *p
fdad0 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31  , int op, int p1
fdae0 2c 20 69 6e 74 20 70 32 29 7b 0a 20 20 69 6e 74  , int p2){.  int
fdaf0 20 69 3b 0a 20 20 56 64 62 65 4f 70 20 2a 70 4f   i;.  VdbeOp *pO
fdb00 70 3b 0a 0a 20 20 69 20 3d 20 70 2d 3e 6e 4f 70  p;..  i = p->nOp
fdb10 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d  ;.  assert( p->m
fdb20 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43  agic==VDBE_MAGIC
fdb30 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 70  _INIT );.  if( p
fdb40 2d 3e 6e 4f 70 41 6c 6c 6f 63 3c 3d 69 20 29 7b  ->nOpAlloc<=i ){
fdb50 0a 20 20 20 20 72 65 73 69 7a 65 4f 70 41 72 72  .    resizeOpArr
fdb60 61 79 28 70 2c 20 69 2b 31 29 3b 0a 20 20 20 20  ay(p, i+1);.    
fdb70 69 66 28 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  if( sqlite3Mallo
fdb80 63 46 61 69 6c 65 64 28 29 20 29 7b 0a 20 20 20  cFailed() ){.   
fdb90 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20     return 0;.   
fdba0 20 7d 0a 20 20 7d 0a 20 20 70 2d 3e 6e 4f 70 2b   }.  }.  p->nOp+
fdbb0 2b 3b 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61  +;.  pOp = &p->a
fdbc0 4f 70 5b 69 5d 3b 0a 20 20 70 4f 70 2d 3e 6f 70  Op[i];.  pOp->op
fdbd0 63 6f 64 65 20 3d 20 6f 70 3b 0a 20 20 70 4f 70  code = op;.  pOp
fdbe0 2d 3e 70 31 20 3d 20 70 31 3b 0a 20 20 70 4f 70  ->p1 = p1;.  pOp
fdbf0 2d 3e 70 32 20 3d 20 70 32 3b 0a 20 20 70 4f 70  ->p2 = p2;.  pOp
fdc00 2d 3e 70 33 20 3d 20 30 3b 0a 20 20 70 4f 70 2d  ->p3 = 0;.  pOp-
fdc10 3e 70 33 74 79 70 65 20 3d 20 50 33 5f 4e 4f 54  >p3type = P3_NOT
fdc20 55 53 45 44 3b 0a 20 20 70 2d 3e 65 78 70 69 72  USED;.  p->expir
fdc30 65 64 20 3d 20 30 3b 0a 23 69 66 64 65 66 20 53  ed = 0;.#ifdef S
fdc40 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 69 66  QLITE_DEBUG.  if
fdc50 28 20 73 71 6c 69 74 65 33 5f 76 64 62 65 5f 61  ( sqlite3_vdbe_a
fdc60 64 64 6f 70 5f 74 72 61 63 65 20 29 20 73 71 6c  ddop_trace ) sql
fdc70 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28  ite3VdbePrintOp(
fdc80 30 2c 20 69 2c 20 26 70 2d 3e 61 4f 70 5b 69 5d  0, i, &p->aOp[i]
fdc90 29 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75  );.#endif.  retu
fdca0 72 6e 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  rn i;.}../*.** A
fdcb0 64 64 20 61 6e 20 6f 70 63 6f 64 65 20 74 68 61  dd an opcode tha
fdcc0 74 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20 70  t includes the p
fdcd0 33 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49  3 value..*/.SQLI
fdce0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
fdcf0 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28 56 64  qlite3VdbeOp3(Vd
fdd00 62 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69  be *p, int op, i
fdd10 6e 74 20 70 31 2c 20 69 6e 74 20 70 32 2c 20 63  nt p1, int p2, c
fdd20 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 33 2c 69  onst char *zP3,i
fdd30 6e 74 20 70 33 74 79 70 65 29 7b 0a 20 20 69 6e  nt p3type){.  in
fdd40 74 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33  t addr = sqlite3
fdd50 56 64 62 65 41 64 64 4f 70 28 70 2c 20 6f 70 2c  VdbeAddOp(p, op,
fdd60 20 70 31 2c 20 70 32 29 3b 0a 20 20 73 71 6c 69   p1, p2);.  sqli
fdd70 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28  te3VdbeChangeP3(
fdd80 70 2c 20 61 64 64 72 2c 20 7a 50 33 2c 20 70 33  p, addr, zP3, p3
fdd90 74 79 70 65 29 3b 0a 20 20 72 65 74 75 72 6e 20  type);.  return 
fdda0 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  addr;.}../*.** C
fddb0 72 65 61 74 65 20 61 20 6e 65 77 20 73 79 6d 62  reate a new symb
fddc0 6f 6c 69 63 20 6c 61 62 65 6c 20 66 6f 72 20 61  olic label for a
fddd0 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68  n instruction th
fdde0 61 74 20 68 61 73 20 79 65 74 20 74 6f 20 62 65  at has yet to be
fddf0 0a 2a 2a 20 63 6f 64 65 64 2e 20 20 54 68 65 20  .** coded.  The 
fde00 73 79 6d 62 6f 6c 69 63 20 6c 61 62 65 6c 20 69  symbolic label i
fde10 73 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 61 20  s really just a 
fde20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65 72 2e  negative number.
fde30 20 20 54 68 65 0a 2a 2a 20 6c 61 62 65 6c 20 63    The.** label c
fde40 61 6e 20 62 65 20 75 73 65 64 20 61 73 20 74 68  an be used as th
fde50 65 20 50 32 20 76 61 6c 75 65 20 6f 66 20 61 6e  e P2 value of an
fde60 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 4c 61 74   operation.  Lat
fde70 65 72 2c 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20  er, when.** the 
fde80 6c 61 62 65 6c 20 69 73 20 72 65 73 6f 6c 76 65  label is resolve
fde90 64 20 74 6f 20 61 20 73 70 65 63 69 66 69 63 20  d to a specific 
fdea0 61 64 64 72 65 73 73 2c 20 74 68 65 20 56 44 42  address, the VDB
fdeb0 45 20 77 69 6c 6c 20 73 63 61 6e 0a 2a 2a 20 74  E will scan.** t
fdec0 68 72 6f 75 67 68 20 69 74 73 20 6f 70 65 72 61  hrough its opera
fded0 74 69 6f 6e 20 6c 69 73 74 20 61 6e 64 20 63 68  tion list and ch
fdee0 61 6e 67 65 20 61 6c 6c 20 76 61 6c 75 65 73 20  ange all values 
fdef0 6f 66 20 50 32 20 77 68 69 63 68 20 6d 61 74 63  of P2 which matc
fdf00 68 0a 2a 2a 20 74 68 65 20 6c 61 62 65 6c 20 69  h.** the label i
fdf10 6e 74 6f 20 74 68 65 20 72 65 73 6f 6c 76 65 64  nto the resolved
fdf20 20 61 64 64 72 65 73 73 2e 0a 2a 2a 0a 2a 2a 20   address..**.** 
fdf30 54 68 65 20 56 44 42 45 20 6b 6e 6f 77 73 20 74  The VDBE knows t
fdf40 68 61 74 20 61 20 50 32 20 76 61 6c 75 65 20 69  hat a P2 value i
fdf50 73 20 61 20 6c 61 62 65 6c 20 62 65 63 61 75 73  s a label becaus
fdf60 65 20 6c 61 62 65 6c 73 20 61 72 65 0a 2a 2a 20  e labels are.** 
fdf70 61 6c 77 61 79 73 20 6e 65 67 61 74 69 76 65 20  always negative 
fdf80 61 6e 64 20 50 32 20 76 61 6c 75 65 73 20 61 72  and P2 values ar
fdf90 65 20 73 75 70 70 6f 73 65 20 74 6f 20 62 65 20  e suppose to be 
fdfa0 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 0a 2a 2a  non-negative..**
fdfb0 20 48 65 6e 63 65 2c 20 61 20 6e 65 67 61 74 69   Hence, a negati
fdfc0 76 65 20 50 32 20 76 61 6c 75 65 20 69 73 20 61  ve P2 value is a
fdfd0 20 6c 61 62 65 6c 20 74 68 61 74 20 68 61 73 20   label that has 
fdfe0 79 65 74 20 74 6f 20 62 65 20 72 65 73 6f 6c 76  yet to be resolv
fdff0 65 64 2e 0a 2a 2a 0a 2a 2a 20 5a 65 72 6f 20 69  ed..**.** Zero i
fe000 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 61 20  s returned if a 
fe010 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e 0a  malloc() fails..
fe020 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
fe030 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
fe040 65 4d 61 6b 65 4c 61 62 65 6c 28 56 64 62 65 20  eMakeLabel(Vdbe 
fe050 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  *p){.  int i;.  
fe060 69 20 3d 20 70 2d 3e 6e 4c 61 62 65 6c 2b 2b 3b  i = p->nLabel++;
fe070 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61  .  assert( p->ma
fe080 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f  gic==VDBE_MAGIC_
fe090 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 69 3e  INIT );.  if( i>
fe0a0 3d 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c 6f 63 20  =p->nLabelAlloc 
fe0b0 29 7b 0a 20 20 20 20 70 2d 3e 6e 4c 61 62 65 6c  ){.    p->nLabel
fe0c0 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 4c 61 62 65  Alloc = p->nLabe
fe0d0 6c 41 6c 6c 6f 63 2a 32 20 2b 20 31 30 3b 0a 20  lAlloc*2 + 10;. 
fe0e0 20 20 20 70 2d 3e 61 4c 61 62 65 6c 20 3d 20 73     p->aLabel = s
fe0f0 71 6c 69 74 65 52 65 61 6c 6c 6f 63 4f 72 46 72  qliteReallocOrFr
fe100 65 65 28 70 2d 3e 61 4c 61 62 65 6c 2c 0a 20 20  ee(p->aLabel,.  
fe110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe130 20 20 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c 6f 63    p->nLabelAlloc
fe140 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 4c 61 62 65  *sizeof(p->aLabe
fe150 6c 5b 30 5d 29 29 3b 0a 20 20 7d 0a 20 20 69 66  l[0]));.  }.  if
fe160 28 20 70 2d 3e 61 4c 61 62 65 6c 20 29 7b 0a 20  ( p->aLabel ){. 
fe170 20 20 20 70 2d 3e 61 4c 61 62 65 6c 5b 69 5d 20     p->aLabel[i] 
fe180 3d 20 2d 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75  = -1;.  }.  retu
fe190 72 6e 20 2d 31 2d 69 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn -1-i;.}../*.*
fe1a0 2a 20 52 65 73 6f 6c 76 65 20 6c 61 62 65 6c 20  * Resolve label 
fe1b0 22 78 22 20 74 6f 20 62 65 20 74 68 65 20 61 64  "x" to be the ad
fe1c0 64 72 65 73 73 20 6f 66 20 74 68 65 20 6e 65 78  dress of the nex
fe1d0 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f  t instruction to
fe1e0 0a 2a 2a 20 62 65 20 69 6e 73 65 72 74 65 64 2e  .** be inserted.
fe1f0 20 20 54 68 65 20 70 61 72 61 6d 65 74 65 72 20    The parameter 
fe200 22 78 22 20 6d 75 73 74 20 68 61 76 65 20 62 65  "x" must have be
fe210 65 6e 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  en obtained from
fe220 0a 2a 2a 20 61 20 70 72 69 6f 72 20 63 61 6c 6c  .** a prior call
fe230 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62 65 4d   to sqlite3VdbeM
fe240 61 6b 65 4c 61 62 65 6c 28 29 2e 0a 2a 2f 0a 53  akeLabel()..*/.S
fe250 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
fe260 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  id sqlite3VdbeRe
fe270 73 6f 6c 76 65 4c 61 62 65 6c 28 56 64 62 65 20  solveLabel(Vdbe 
fe280 2a 70 2c 20 69 6e 74 20 78 29 7b 0a 20 20 69 6e  *p, int x){.  in
fe290 74 20 6a 20 3d 20 2d 31 2d 78 3b 0a 20 20 61 73  t j = -1-x;.  as
fe2a0 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d  sert( p->magic==
fe2b0 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20  VDBE_MAGIC_INIT 
fe2c0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6a 3e 3d  );.  assert( j>=
fe2d0 30 20 26 26 20 6a 3c 70 2d 3e 6e 4c 61 62 65 6c  0 && j<p->nLabel
fe2e0 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 4c 61   );.  if( p->aLa
fe2f0 62 65 6c 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4c  bel ){.    p->aL
fe300 61 62 65 6c 5b 6a 5d 20 3d 20 70 2d 3e 6e 4f 70  abel[j] = p->nOp
fe310 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ;.  }.}../*.** R
fe320 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69  eturn non-zero i
fe330 66 20 6f 70 63 6f 64 65 20 27 6f 70 27 20 69 73  f opcode 'op' is
fe340 20 67 75 61 72 65 6e 74 65 65 64 20 6e 6f 74 20   guarenteed not 
fe350 74 6f 20 70 75 73 68 20 6d 6f 72 65 20 76 61 6c  to push more val
fe360 75 65 73 0a 2a 2a 20 6f 6e 74 6f 20 74 68 65 20  ues.** onto the 
fe370 56 44 42 45 20 73 74 61 63 6b 20 74 68 61 6e 20  VDBE stack than 
fe380 69 74 20 70 6f 70 73 20 6f 66 66 2e 0a 2a 2f 0a  it pops off..*/.
fe390 73 74 61 74 69 63 20 69 6e 74 20 6f 70 63 6f 64  static int opcod
fe3a0 65 4e 6f 50 75 73 68 28 75 38 20 6f 70 29 7b 0a  eNoPush(u8 op){.
fe3b0 20 20 2f 2a 20 54 68 65 20 31 30 20 4e 4f 50 55    /* The 10 NOPU
fe3c0 53 48 5f 4d 41 53 4b 5f 6e 20 63 6f 6e 73 74 61  SH_MASK_n consta
fe3d0 6e 74 73 20 61 72 65 20 64 65 66 69 6e 65 64 20  nts are defined 
fe3e0 69 6e 20 74 68 65 20 61 75 74 6f 6d 61 74 69 63  in the automatic
fe3f0 61 6c 6c 79 0a 20 20 2a 2a 20 67 65 6e 65 72 61  ally.  ** genera
fe400 74 65 64 20 68 65 61 64 65 72 20 66 69 6c 65 20  ted header file 
fe410 6f 70 63 6f 64 65 73 2e 68 2e 20 45 61 63 68 20  opcodes.h. Each 
fe420 69 73 20 61 20 31 36 2d 62 69 74 20 62 69 74 6d  is a 16-bit bitm
fe430 61 73 6b 2c 20 6f 6e 65 0a 20 20 2a 2a 20 62 69  ask, one.  ** bi
fe440 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  t corresponding 
fe450 74 6f 20 65 61 63 68 20 6f 70 63 6f 64 65 20 69  to each opcode i
fe460 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 74 68  mplemented by th
fe470 65 20 76 69 72 74 75 61 6c 0a 20 20 2a 2a 20 6d  e virtual.  ** m
fe480 61 63 68 69 6e 65 20 69 6e 20 76 64 62 65 2e 63  achine in vdbe.c
fe490 2e 20 54 68 65 20 62 69 74 20 69 73 20 74 72 75  . The bit is tru
fe4a0 65 20 69 66 20 74 68 65 20 77 6f 72 64 20 22 6e  e if the word "n
fe4b0 6f 2d 70 75 73 68 22 20 61 70 70 65 61 72 73 0a  o-push" appears.
fe4c0 20 20 2a 2a 20 69 6e 20 61 20 63 6f 6d 6d 65 6e    ** in a commen
fe4d0 74 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 6c 69  t on the same li
fe4e0 6e 65 20 61 73 20 74 68 65 20 22 63 61 73 65 20  ne as the "case 
fe4f0 4f 50 5f 58 58 58 3a 22 20 69 6e 20 0a 20 20 2a  OP_XXX:" in .  *
fe500 2a 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65  * sqlite3VdbeExe
fe510 63 28 29 20 69 6e 20 76 64 62 65 2e 63 2e 0a 20  c() in vdbe.c.. 
fe520 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65 20   **.  ** If the 
fe530 62 69 74 20 69 73 20 74 72 75 65 2c 20 74 68 65  bit is true, the
fe540 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  n the correspond
fe550 69 6e 67 20 6f 70 63 6f 64 65 20 69 73 20 67 75  ing opcode is gu
fe560 61 72 65 6e 74 65 65 64 20 6e 6f 74 0a 20 20 2a  arenteed not.  *
fe570 2a 20 74 6f 20 67 72 6f 77 20 74 68 65 20 73 74  * to grow the st
fe580 61 63 6b 20 77 68 65 6e 20 69 74 20 69 73 20 65  ack when it is e
fe590 78 65 63 75 74 65 64 2e 20 4f 74 68 65 72 77 69  xecuted. Otherwi
fe5a0 73 65 2c 20 69 74 20 6d 61 79 20 67 72 6f 77 20  se, it may grow 
fe5b0 74 68 65 0a 20 20 2a 2a 20 73 74 61 63 6b 20 62  the.  ** stack b
fe5c0 79 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20 65 6e  y at most one en
fe5d0 74 72 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e  try..  **.  ** N
fe5e0 4f 50 55 53 48 5f 4d 41 53 4b 5f 30 20 63 6f 72  OPUSH_MASK_0 cor
fe5f0 72 65 73 70 6f 6e 64 73 20 74 6f 20 6f 70 63 6f  responds to opco
fe600 64 65 73 20 30 20 74 6f 20 31 35 2e 20 4e 4f 50  des 0 to 15. NOP
fe610 55 53 48 5f 4d 41 53 4b 5f 31 20 63 6f 6e 74 61  USH_MASK_1 conta
fe620 69 6e 73 0a 20 20 2a 2a 20 6f 6e 65 20 62 69 74  ins.  ** one bit
fe630 20 66 6f 72 20 6f 70 63 6f 64 65 73 20 31 36 20   for opcodes 16 
fe640 74 6f 20 33 31 2c 20 61 6e 64 20 73 6f 20 6f 6e  to 31, and so on
fe650 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 31 36 2d 62  ..  **.  ** 16-b
fe660 69 74 20 62 69 74 6d 61 73 6b 73 20 28 72 61 74  it bitmasks (rat
fe670 68 65 72 20 74 68 61 6e 20 33 32 2d 62 69 74 29  her than 32-bit)
fe680 20 61 72 65 20 73 70 65 63 69 66 69 65 64 20 69   are specified i
fe690 6e 20 6f 70 63 6f 64 65 73 2e 68 20 0a 20 20 2a  n opcodes.h .  *
fe6a0 2a 20 62 65 63 61 75 73 65 20 74 68 65 20 66 69  * because the fi
fe6b0 6c 65 20 69 73 20 67 65 6e 65 72 61 74 65 64 20  le is generated 
fe6c0 62 79 20 61 6e 20 61 77 6b 20 70 72 6f 67 72 61  by an awk progra
fe6d0 6d 2e 20 41 77 6b 20 6d 61 6e 69 70 75 6c 61 74  m. Awk manipulat
fe6e0 65 73 0a 20 20 2a 2a 20 61 6c 6c 20 6e 75 6d 62  es.  ** all numb
fe6f0 65 72 73 20 61 73 20 66 6c 6f 61 74 69 6e 67 2d  ers as floating-
fe700 70 6f 69 6e 74 20 61 6e 64 20 77 65 20 64 6f 6e  point and we don
fe710 27 74 20 77 61 6e 74 20 74 6f 20 72 69 73 6b 20  't want to risk 
fe720 61 20 72 6f 75 6e 64 69 6e 67 0a 20 20 2a 2a 20  a rounding.  ** 
fe730 65 72 72 6f 72 20 69 66 20 73 6f 6d 65 6f 6e 65  error if someone
fe740 20 62 75 69 6c 64 73 20 77 69 74 68 20 61 6e 20   builds with an 
fe750 61 77 6b 20 74 68 61 74 20 75 73 65 73 20 28 66  awk that uses (f
fe760 6f 72 20 65 78 61 6d 70 6c 65 29 20 33 32 2d 62  or example) 32-b
fe770 69 74 20 0a 20 20 2a 2a 20 49 45 45 45 20 66 6c  it .  ** IEEE fl
fe780 6f 61 74 73 2e 0a 20 20 2a 2f 20 0a 20 20 73 74  oats..  */ .  st
fe790 61 74 69 63 20 63 6f 6e 73 74 20 75 33 32 20 6d  atic const u32 m
fe7a0 61 73 6b 73 5b 35 5d 20 3d 20 7b 0a 20 20 20 20  asks[5] = {.    
fe7b0 4e 4f 50 55 53 48 5f 4d 41 53 4b 5f 30 20 2b 20  NOPUSH_MASK_0 + 
fe7c0 28 28 28 75 6e 73 69 67 6e 65 64 29 4e 4f 50 55  (((unsigned)NOPU
fe7d0 53 48 5f 4d 41 53 4b 5f 31 29 3c 3c 31 36 29 2c  SH_MASK_1)<<16),
fe7e0 0a 20 20 20 20 4e 4f 50 55 53 48 5f 4d 41 53 4b  .    NOPUSH_MASK
fe7f0 5f 32 20 2b 20 28 28 28 75 6e 73 69 67 6e 65 64  _2 + (((unsigned
fe800 29 4e 4f 50 55 53 48 5f 4d 41 53 4b 5f 33 29 3c  )NOPUSH_MASK_3)<
fe810 3c 31 36 29 2c 0a 20 20 20 20 4e 4f 50 55 53 48  <16),.    NOPUSH
fe820 5f 4d 41 53 4b 5f 34 20 2b 20 28 28 28 75 6e 73  _MASK_4 + (((uns
fe830 69 67 6e 65 64 29 4e 4f 50 55 53 48 5f 4d 41 53  igned)NOPUSH_MAS
fe840 4b 5f 35 29 3c 3c 31 36 29 2c 0a 20 20 20 20 4e  K_5)<<16),.    N
fe850 4f 50 55 53 48 5f 4d 41 53 4b 5f 36 20 2b 20 28  OPUSH_MASK_6 + (
fe860 28 28 75 6e 73 69 67 6e 65 64 29 4e 4f 50 55 53  ((unsigned)NOPUS
fe870 48 5f 4d 41 53 4b 5f 37 29 3c 3c 31 36 29 2c 0a  H_MASK_7)<<16),.
fe880 20 20 20 20 4e 4f 50 55 53 48 5f 4d 41 53 4b 5f      NOPUSH_MASK_
fe890 38 20 2b 20 28 28 28 75 6e 73 69 67 6e 65 64 29  8 + (((unsigned)
fe8a0 4e 4f 50 55 53 48 5f 4d 41 53 4b 5f 39 29 3c 3c  NOPUSH_MASK_9)<<
fe8b0 31 36 29 0a 20 20 7d 3b 0a 20 20 61 73 73 65 72  16).  };.  asser
fe8c0 74 28 20 6f 70 3c 33 32 2a 35 20 29 3b 0a 20 20  t( op<32*5 );.  
fe8d0 72 65 74 75 72 6e 20 28 6d 61 73 6b 73 5b 6f 70  return (masks[op
fe8e0 3e 3e 35 5d 20 26 20 28 31 3c 3c 28 6f 70 26 30  >>5] & (1<<(op&0
fe8f0 78 31 46 29 29 29 3b 0a 7d 0a 0a 23 69 66 6e 64  x1F)));.}..#ifnd
fe900 65 66 20 4e 44 45 42 55 47 0a 53 51 4c 49 54 45  ef NDEBUG.SQLITE
fe910 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
fe920 69 74 65 33 56 64 62 65 4f 70 63 6f 64 65 4e 6f  ite3VdbeOpcodeNo
fe930 50 75 73 68 28 75 38 20 6f 70 29 7b 0a 20 20 72  Push(u8 op){.  r
fe940 65 74 75 72 6e 20 6f 70 63 6f 64 65 4e 6f 50 75  eturn opcodeNoPu
fe950 73 68 28 6f 70 29 3b 0a 7d 0a 23 65 6e 64 69 66  sh(op);.}.#endif
fe960 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 70 20 74 68 72  ../*.** Loop thr
fe970 6f 75 67 68 20 74 68 65 20 70 72 6f 67 72 61 6d  ough the program
fe980 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 50 32 20   looking for P2 
fe990 76 61 6c 75 65 73 20 74 68 61 74 20 61 72 65 20  values that are 
fe9a0 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 20 45 61 63  negative..** Eac
fe9b0 68 20 73 75 63 68 20 76 61 6c 75 65 20 69 73 20  h such value is 
fe9c0 61 20 6c 61 62 65 6c 2e 20 20 52 65 73 6f 6c 76  a label.  Resolv
fe9d0 65 20 74 68 65 20 6c 61 62 65 6c 20 62 79 20 73  e the label by s
fe9e0 65 74 74 69 6e 67 20 74 68 65 20 50 32 0a 2a 2a  etting the P2.**
fe9f0 20 76 61 6c 75 65 20 74 6f 20 69 74 73 20 63 6f   value to its co
fea00 72 72 65 63 74 20 6e 6f 6e 2d 7a 65 72 6f 20 76  rrect non-zero v
fea10 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  alue..**.** This
fea20 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
fea30 65 64 20 6f 6e 63 65 20 61 66 74 65 72 20 61 6c  ed once after al
fea40 6c 20 6f 70 63 6f 64 65 73 20 68 61 76 65 20 62  l opcodes have b
fea50 65 65 6e 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2a  een inserted..**
fea60 0a 2a 2a 20 56 61 72 69 61 62 6c 65 20 2a 70 4d  .** Variable *pM
fea70 61 78 46 75 6e 63 41 72 67 73 20 69 73 20 73 65  axFuncArgs is se
fea80 74 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d  t to the maximum
fea90 20 76 61 6c 75 65 20 6f 66 20 61 6e 79 20 50 32   value of any P2
feaa0 20 61 72 67 75 6d 65 6e 74 20 0a 2a 2a 20 74 6f   argument .** to
feab0 20 61 6e 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 2c   an OP_Function,
feac0 20 4f 50 5f 41 67 67 53 74 65 70 20 6f 72 20 4f   OP_AggStep or O
fead0 50 5f 56 46 69 6c 74 65 72 20 6f 70 63 6f 64 65  P_VFilter opcode
feae0 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20 62  . This is used b
feaf0 79 20 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62  y .** sqlite3Vdb
feb00 65 4d 61 6b 65 52 65 61 64 79 28 29 20 74 6f 20  eMakeReady() to 
feb10 73 69 7a 65 20 74 68 65 20 56 64 62 65 2e 61 70  size the Vdbe.ap
feb20 41 72 67 5b 5d 20 61 72 72 61 79 2e 0a 2a 2a 0a  Arg[] array..**.
feb30 2a 2a 20 54 68 65 20 69 6e 74 65 67 65 72 20 2a  ** The integer *
feb40 70 4d 61 78 53 74 61 63 6b 20 69 73 20 73 65 74  pMaxStack is set
feb50 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20   to the maximum 
feb60 6e 75 6d 62 65 72 20 6f 66 20 76 64 62 65 20 73  number of vdbe s
feb70 74 61 63 6b 0a 2a 2a 20 65 6e 74 72 69 65 73 20  tack.** entries 
feb80 74 68 61 74 20 73 74 61 74 69 63 20 61 6e 61 6c  that static anal
feb90 79 73 69 73 20 72 65 76 65 61 6c 73 20 74 68 69  ysis reveals thi
feba0 73 20 70 72 6f 67 72 61 6d 20 6d 69 67 68 74 20  s program might 
febb0 6e 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  need..**.** This
febc0 20 72 6f 75 74 69 6e 65 20 61 6c 73 6f 20 64 6f   routine also do
febd0 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  es the following
febe0 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 3a 20 20   optimization:  
febf0 49 74 20 73 63 61 6e 73 20 66 6f 72 0a 2a 2a 20  It scans for.** 
fec00 48 61 6c 74 20 69 6e 73 74 72 75 63 74 69 6f 6e  Halt instruction
fec10 73 20 77 68 65 72 65 20 50 31 3d 3d 53 51 4c 49  s where P1==SQLI
fec20 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 6f 72  TE_CONSTRAINT or
fec30 20 50 32 3d 3d 4f 45 5f 41 62 6f 72 74 20 6f 72   P2==OE_Abort or
fec40 20 66 6f 72 0a 2a 2a 20 49 64 78 49 6e 73 65 72   for.** IdxInser
fec50 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 77  t instructions w
fec60 68 65 72 65 20 50 32 21 3d 30 2e 20 20 49 66 20  here P2!=0.  If 
fec70 6e 6f 20 73 75 63 68 20 69 6e 73 74 72 75 63 74  no such instruct
fec80 69 6f 6e 20 69 73 0a 2a 2a 20 66 6f 75 6e 64 2c  ion is.** found,
fec90 20 74 68 65 6e 20 65 76 65 72 79 20 53 74 61 74   then every Stat
feca0 65 6d 65 6e 74 20 69 6e 73 74 72 75 63 74 69 6f  ement instructio
fecb0 6e 20 69 73 20 63 68 61 6e 67 65 64 20 74 6f 20  n is changed to 
fecc0 61 20 4e 6f 6f 70 2e 20 20 49 6e 0a 2a 2a 20 74  a Noop.  In.** t
fecd0 68 69 73 20 77 61 79 2c 20 77 65 20 61 76 6f 69  his way, we avoi
fece0 64 20 63 72 65 61 74 69 6e 67 20 74 68 65 20 73  d creating the s
fecf0 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c  tatement journal
fed00 20 66 69 6c 65 20 75 6e 6e 65 63 65 73 73 61 72   file unnecessar
fed10 69 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ily..*/.static v
fed20 6f 69 64 20 72 65 73 6f 6c 76 65 50 32 56 61 6c  oid resolveP2Val
fed30 75 65 73 28 56 64 62 65 20 2a 70 2c 20 69 6e 74  ues(Vdbe *p, int
fed40 20 2a 70 4d 61 78 46 75 6e 63 41 72 67 73 2c 20   *pMaxFuncArgs, 
fed50 69 6e 74 20 2a 70 4d 61 78 53 74 61 63 6b 29 7b  int *pMaxStack){
fed60 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
fed70 6e 4d 61 78 41 72 67 73 20 3d 20 30 3b 0a 20 20  nMaxArgs = 0;.  
fed80 69 6e 74 20 6e 4d 61 78 53 74 61 63 6b 20 3d 20  int nMaxStack = 
fed90 70 2d 3e 6e 4f 70 3b 0a 20 20 4f 70 20 2a 70 4f  p->nOp;.  Op *pO
feda0 70 3b 0a 20 20 69 6e 74 20 2a 61 4c 61 62 65 6c  p;.  int *aLabel
fedb0 20 3d 20 70 2d 3e 61 4c 61 62 65 6c 3b 0a 20 20   = p->aLabel;.  
fedc0 69 6e 74 20 64 6f 65 73 53 74 61 74 65 6d 65 6e  int doesStatemen
fedd0 74 52 6f 6c 6c 62 61 63 6b 20 3d 20 30 3b 0a 20  tRollback = 0;. 
fede0 20 69 6e 74 20 68 61 73 53 74 61 74 65 6d 65 6e   int hasStatemen
fedf0 74 42 65 67 69 6e 20 3d 20 30 3b 0a 20 20 66 6f  tBegin = 0;.  fo
fee00 72 28 70 4f 70 3d 70 2d 3e 61 4f 70 2c 20 69 3d  r(pOp=p->aOp, i=
fee10 70 2d 3e 6e 4f 70 2d 31 3b 20 69 3e 3d 30 3b 20  p->nOp-1; i>=0; 
fee20 69 2d 2d 2c 20 70 4f 70 2b 2b 29 7b 0a 20 20 20  i--, pOp++){.   
fee30 20 75 38 20 6f 70 63 6f 64 65 20 3d 20 70 4f 70   u8 opcode = pOp
fee40 2d 3e 6f 70 63 6f 64 65 3b 0a 0a 20 20 20 20 69  ->opcode;..    i
fee50 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46 75  f( opcode==OP_Fu
fee60 6e 63 74 69 6f 6e 20 7c 7c 20 6f 70 63 6f 64 65  nction || opcode
fee70 3d 3d 4f 50 5f 41 67 67 53 74 65 70 20 0a 23 69  ==OP_AggStep .#i
fee80 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
fee90 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
feea0 20 20 20 20 20 20 20 7c 7c 20 6f 70 63 6f 64 65         || opcode
feeb0 3d 3d 4f 50 5f 56 55 70 64 61 74 65 0a 23 65 6e  ==OP_VUpdate.#en
feec0 64 69 66 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  dif.    ){.     
feed0 20 69 66 28 20 70 4f 70 2d 3e 70 32 3e 6e 4d 61   if( pOp->p2>nMa
feee0 78 41 72 67 73 20 29 20 6e 4d 61 78 41 72 67 73  xArgs ) nMaxArgs
feef0 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 20 20   = pOp->p2;.    
fef00 7d 0a 20 20 20 20 69 66 28 20 6f 70 63 6f 64 65  }.    if( opcode
fef10 3d 3d 4f 50 5f 48 61 6c 74 20 29 7b 0a 20 20 20  ==OP_Halt ){.   
fef20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 3d 3d     if( pOp->p1==
fef30 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e  SQLITE_CONSTRAIN
fef40 54 20 26 26 20 70 4f 70 2d 3e 70 32 3d 3d 4f 45  T && pOp->p2==OE
fef50 5f 41 62 6f 72 74 20 29 7b 0a 20 20 20 20 20 20  _Abort ){.      
fef60 20 20 64 6f 65 73 53 74 61 74 65 6d 65 6e 74 52    doesStatementR
fef70 6f 6c 6c 62 61 63 6b 20 3d 20 31 3b 0a 20 20 20  ollback = 1;.   
fef80 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69     }.    }else i
fef90 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 53 74  f( opcode==OP_St
fefa0 61 74 65 6d 65 6e 74 20 29 7b 0a 20 20 20 20 20  atement ){.     
fefb0 20 68 61 73 53 74 61 74 65 6d 65 6e 74 42 65 67   hasStatementBeg
fefc0 69 6e 20 3d 20 31 3b 0a 23 69 66 6e 64 65 66 20  in = 1;.#ifndef 
fefd0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
fefe0 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 7d 65 6c  UALTABLE.    }el
feff0 73 65 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f  se if( opcode==O
ff000 50 5f 56 55 70 64 61 74 65 20 7c 7c 20 6f 70 63  P_VUpdate || opc
ff010 6f 64 65 3d 3d 4f 50 5f 56 52 65 6e 61 6d 65 20  ode==OP_VRename 
ff020 29 7b 0a 20 20 20 20 20 20 64 6f 65 73 53 74 61  ){.      doesSta
ff030 74 65 6d 65 6e 74 52 6f 6c 6c 62 61 63 6b 20 3d  tementRollback =
ff040 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66   1;.    }else if
ff050 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 46 69  ( opcode==OP_VFi
ff060 6c 74 65 72 20 29 7b 0a 20 20 20 20 20 20 69 6e  lter ){.      in
ff070 74 20 6e 3b 0a 20 20 20 20 20 20 61 73 73 65 72  t n;.      asser
ff080 74 28 20 70 2d 3e 6e 4f 70 20 2d 20 69 20 3e 3d  t( p->nOp - i >=
ff090 20 33 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65   3 );.      asse
ff0a0 72 74 28 20 70 4f 70 5b 2d 32 5d 2e 6f 70 63 6f  rt( pOp[-2].opco
ff0b0 64 65 3d 3d 4f 50 5f 49 6e 74 65 67 65 72 20 29  de==OP_Integer )
ff0c0 3b 0a 20 20 20 20 20 20 6e 20 3d 20 70 4f 70 5b  ;.      n = pOp[
ff0d0 2d 32 5d 2e 70 31 3b 0a 20 20 20 20 20 20 69 66  -2].p1;.      if
ff0e0 28 20 6e 3e 6e 4d 61 78 41 72 67 73 20 29 20 6e  ( n>nMaxArgs ) n
ff0f0 4d 61 78 41 72 67 73 20 3d 20 6e 3b 0a 23 65 6e  MaxArgs = n;.#en
ff100 64 69 66 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  dif.    }.    if
ff110 28 20 6f 70 63 6f 64 65 4e 6f 50 75 73 68 28 6f  ( opcodeNoPush(o
ff120 70 63 6f 64 65 29 20 29 7b 0a 20 20 20 20 20 20  pcode) ){.      
ff130 6e 4d 61 78 53 74 61 63 6b 2d 2d 3b 0a 20 20 20  nMaxStack--;.   
ff140 20 7d 0a 0a 20 20 20 20 69 66 28 20 70 4f 70 2d   }..    if( pOp-
ff150 3e 70 32 3e 3d 30 20 29 20 63 6f 6e 74 69 6e 75  >p2>=0 ) continu
ff160 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 2d  e;.    assert( -
ff170 31 2d 70 4f 70 2d 3e 70 32 3c 70 2d 3e 6e 4c 61  1-pOp->p2<p->nLa
ff180 62 65 6c 20 29 3b 0a 20 20 20 20 70 4f 70 2d 3e  bel );.    pOp->
ff190 70 32 20 3d 20 61 4c 61 62 65 6c 5b 2d 31 2d 70  p2 = aLabel[-1-p
ff1a0 4f 70 2d 3e 70 32 5d 3b 0a 20 20 7d 0a 20 20 73  Op->p2];.  }.  s
ff1b0 71 6c 69 74 65 46 72 65 65 28 70 2d 3e 61 4c 61  qliteFree(p->aLa
ff1c0 62 65 6c 29 3b 0a 20 20 70 2d 3e 61 4c 61 62 65  bel);.  p->aLabe
ff1d0 6c 20 3d 20 30 3b 0a 0a 20 20 2a 70 4d 61 78 46  l = 0;..  *pMaxF
ff1e0 75 6e 63 41 72 67 73 20 3d 20 6e 4d 61 78 41 72  uncArgs = nMaxAr
ff1f0 67 73 3b 0a 20 20 2a 70 4d 61 78 53 74 61 63 6b  gs;.  *pMaxStack
ff200 20 3d 20 6e 4d 61 78 53 74 61 63 6b 3b 0a 0a 20   = nMaxStack;.. 
ff210 20 2f 2a 20 49 66 20 77 65 20 6e 65 76 65 72 20   /* If we never 
ff220 72 6f 6c 6c 62 61 63 6b 20 61 20 73 74 61 74 65  rollback a state
ff230 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  ment transaction
ff240 2c 20 74 68 65 6e 20 73 74 61 74 65 6d 65 6e 74  , then statement
ff250 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f  .  ** transactio
ff260 6e 73 20 61 72 65 20 6e 6f 74 20 6e 65 65 64 65  ns are not neede
ff270 64 2e 20 20 53 6f 20 63 68 61 6e 67 65 20 65 76  d.  So change ev
ff280 65 72 79 20 4f 50 5f 53 74 61 74 65 6d 65 6e 74  ery OP_Statement
ff290 0a 20 20 2a 2a 20 6f 70 63 6f 64 65 20 69 6e 74  .  ** opcode int
ff2a0 6f 20 61 6e 20 4f 50 5f 4e 6f 6f 70 2e 20 20 54  o an OP_Noop.  T
ff2b0 68 69 73 20 61 76 6f 69 64 20 61 20 63 61 6c 6c  his avoid a call
ff2c0 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 4f 70 65   to sqlite3OsOpe
ff2d0 6e 45 78 63 6c 75 73 69 76 65 28 29 0a 20 20 2a  nExclusive().  *
ff2e0 2a 20 77 68 69 63 68 20 63 61 6e 20 62 65 20 65  * which can be e
ff2f0 78 70 65 6e 73 69 76 65 20 6f 6e 20 73 6f 6d 65  xpensive on some
ff300 20 70 6c 61 74 66 6f 72 6d 73 2e 0a 20 20 2a 2f   platforms..  */
ff310 0a 20 20 69 66 28 20 68 61 73 53 74 61 74 65 6d  .  if( hasStatem
ff320 65 6e 74 42 65 67 69 6e 20 26 26 20 21 64 6f 65  entBegin && !doe
ff330 73 53 74 61 74 65 6d 65 6e 74 52 6f 6c 6c 62 61  sStatementRollba
ff340 63 6b 20 29 7b 0a 20 20 20 20 66 6f 72 28 70 4f  ck ){.    for(pO
ff350 70 3d 70 2d 3e 61 4f 70 2c 20 69 3d 70 2d 3e 6e  p=p->aOp, i=p->n
ff360 4f 70 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c  Op-1; i>=0; i--,
ff370 20 70 4f 70 2b 2b 29 7b 0a 20 20 20 20 20 20 69   pOp++){.      i
ff380 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d  f( pOp->opcode==
ff390 4f 50 5f 53 74 61 74 65 6d 65 6e 74 20 29 7b 0a  OP_Statement ){.
ff3a0 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63          pOp->opc
ff3b0 6f 64 65 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20  ode = OP_Noop;. 
ff3c0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
ff3d0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
ff3e0 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20   the address of 
ff3f0 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63  the next instruc
ff400 74 69 6f 6e 20 74 6f 20 62 65 20 69 6e 73 65 72  tion to be inser
ff410 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ted..*/.SQLITE_P
ff420 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
ff430 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
ff440 72 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 61 73  r(Vdbe *p){.  as
ff450 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d  sert( p->magic==
ff460 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20  VDBE_MAGIC_INIT 
ff470 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e  );.  return p->n
ff480 4f 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64  Op;.}../*.** Add
ff490 20 61 20 77 68 6f 6c 65 20 6c 69 73 74 20 6f 66   a whole list of
ff4a0 20 6f 70 65 72 61 74 69 6f 6e 73 20 74 6f 20 74   operations to t
ff4b0 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 73 74 61  he operation sta
ff4c0 63 6b 2e 20 20 52 65 74 75 72 6e 20 74 68 65 0a  ck.  Return the.
ff4d0 2a 2a 20 61 64 64 72 65 73 73 20 6f 66 20 74 68  ** address of th
ff4e0 65 20 66 69 72 73 74 20 6f 70 65 72 61 74 69 6f  e first operatio
ff4f0 6e 20 61 64 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49  n added..*/.SQLI
ff500 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
ff510 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c  qlite3VdbeAddOpL
ff520 69 73 74 28 56 64 62 65 20 2a 70 2c 20 69 6e 74  ist(Vdbe *p, int
ff530 20 6e 4f 70 2c 20 56 64 62 65 4f 70 4c 69 73 74   nOp, VdbeOpList
ff540 20 63 6f 6e 73 74 20 2a 61 4f 70 29 7b 0a 20 20   const *aOp){.  
ff550 69 6e 74 20 61 64 64 72 3b 0a 20 20 61 73 73 65  int addr;.  asse
ff560 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44  rt( p->magic==VD
ff570 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b  BE_MAGIC_INIT );
ff580 0a 20 20 72 65 73 69 7a 65 4f 70 41 72 72 61 79  .  resizeOpArray
ff590 28 70 2c 20 70 2d 3e 6e 4f 70 20 2b 20 6e 4f 70  (p, p->nOp + nOp
ff5a0 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  );.  if( sqlite3
ff5b0 4d 61 6c 6c 6f 63 46 61 69 6c 65 64 28 29 20 29  MallocFailed() )
ff5c0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
ff5d0 20 20 7d 0a 20 20 61 64 64 72 20 3d 20 70 2d 3e    }.  addr = p->
ff5e0 6e 4f 70 3b 0a 20 20 69 66 28 20 6e 4f 70 3e 30  nOp;.  if( nOp>0
ff5f0 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   ){.    int i;. 
ff600 20 20 20 56 64 62 65 4f 70 4c 69 73 74 20 63 6f     VdbeOpList co
ff610 6e 73 74 20 2a 70 49 6e 20 3d 20 61 4f 70 3b 0a  nst *pIn = aOp;.
ff620 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
ff630 4f 70 3b 20 69 2b 2b 2c 20 70 49 6e 2b 2b 29 7b  Op; i++, pIn++){
ff640 0a 20 20 20 20 20 20 69 6e 74 20 70 32 20 3d 20  .      int p2 = 
ff650 70 49 6e 2d 3e 70 32 3b 0a 20 20 20 20 20 20 56  pIn->p2;.      V
ff660 64 62 65 4f 70 20 2a 70 4f 75 74 20 3d 20 26 70  dbeOp *pOut = &p
ff670 2d 3e 61 4f 70 5b 69 2b 61 64 64 72 5d 3b 0a 20  ->aOp[i+addr];. 
ff680 20 20 20 20 20 70 4f 75 74 2d 3e 6f 70 63 6f 64       pOut->opcod
ff690 65 20 3d 20 70 49 6e 2d 3e 6f 70 63 6f 64 65 3b  e = pIn->opcode;
ff6a0 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 31 20  .      pOut->p1 
ff6b0 3d 20 70 49 6e 2d 3e 70 31 3b 0a 20 20 20 20 20  = pIn->p1;.     
ff6c0 20 70 4f 75 74 2d 3e 70 32 20 3d 20 70 32 3c 30   pOut->p2 = p2<0
ff6d0 20 3f 20 61 64 64 72 20 2b 20 41 44 44 52 28 70   ? addr + ADDR(p
ff6e0 32 29 20 3a 20 70 32 3b 0a 20 20 20 20 20 20 70  2) : p2;.      p
ff6f0 4f 75 74 2d 3e 70 33 20 3d 20 70 49 6e 2d 3e 70  Out->p3 = pIn->p
ff700 33 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70  3;.      pOut->p
ff710 33 74 79 70 65 20 3d 20 70 49 6e 2d 3e 70 33 20  3type = pIn->p3 
ff720 3f 20 50 33 5f 53 54 41 54 49 43 20 3a 20 50 33  ? P3_STATIC : P3
ff730 5f 4e 4f 54 55 53 45 44 3b 0a 23 69 66 64 65 66  _NOTUSED;.#ifdef
ff740 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20   SQLITE_DEBUG.  
ff750 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f      if( sqlite3_
ff760 76 64 62 65 5f 61 64 64 6f 70 5f 74 72 61 63 65  vdbe_addop_trace
ff770 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
ff780 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 30  te3VdbePrintOp(0
ff790 2c 20 69 2b 61 64 64 72 2c 20 26 70 2d 3e 61 4f  , i+addr, &p->aO
ff7a0 70 5b 69 2b 61 64 64 72 5d 29 3b 0a 20 20 20 20  p[i+addr]);.    
ff7b0 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d    }.#endif.    }
ff7c0 0a 20 20 20 20 70 2d 3e 6e 4f 70 20 2b 3d 20 6e  .    p->nOp += n
ff7d0 4f 70 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  Op;.  }.  return
ff7e0 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   addr;.}../*.** 
ff7f0 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65  Change the value
ff800 20 6f 66 20 74 68 65 20 50 31 20 6f 70 65 72 61   of the P1 opera
ff810 6e 64 20 66 6f 72 20 61 20 73 70 65 63 69 66 69  nd for a specifi
ff820 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a  c instruction..*
ff830 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
ff840 73 20 75 73 65 66 75 6c 20 77 68 65 6e 20 61 20  s useful when a 
ff850 6c 61 72 67 65 20 70 72 6f 67 72 61 6d 20 69 73  large program is
ff860 20 6c 6f 61 64 65 64 20 66 72 6f 6d 20 61 0a 2a   loaded from a.*
ff870 2a 20 73 74 61 74 69 63 20 61 72 72 61 79 20 75  * static array u
ff880 73 69 6e 67 20 73 71 6c 69 74 65 33 56 64 62 65  sing sqlite3Vdbe
ff890 41 64 64 4f 70 4c 69 73 74 20 62 75 74 20 77 65  AddOpList but we
ff8a0 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20 61 0a   want to make a.
ff8b0 2a 2a 20 66 65 77 20 6d 69 6e 6f 72 20 63 68 61  ** few minor cha
ff8c0 6e 67 65 73 20 74 6f 20 74 68 65 20 70 72 6f 67  nges to the prog
ff8d0 72 61 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ram..*/.SQLITE_P
ff8e0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
ff8f0 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28  te3VdbeChangeP1(
ff900 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64  Vdbe *p, int add
ff910 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20 61  r, int val){.  a
ff920 73 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c 20 70  ssert( p==0 || p
ff930 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41  ->magic==VDBE_MA
ff940 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66  GIC_INIT );.  if
ff950 28 20 70 20 26 26 20 61 64 64 72 3e 3d 30 20 26  ( p && addr>=0 &
ff960 26 20 70 2d 3e 6e 4f 70 3e 61 64 64 72 20 26 26  & p->nOp>addr &&
ff970 20 70 2d 3e 61 4f 70 20 29 7b 0a 20 20 20 20 70   p->aOp ){.    p
ff980 2d 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 31 20 3d  ->aOp[addr].p1 =
ff990 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a   val;.  }.}../*.
ff9a0 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 61  ** Change the va
ff9b0 6c 75 65 20 6f 66 20 74 68 65 20 50 32 20 6f 70  lue of the P2 op
ff9c0 65 72 61 6e 64 20 66 6f 72 20 61 20 73 70 65 63  erand for a spec
ff9d0 69 66 69 63 20 69 6e 73 74 72 75 63 74 69 6f 6e  ific instruction
ff9e0 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
ff9f0 65 20 69 73 20 75 73 65 66 75 6c 20 66 6f 72 20  e is useful for 
ffa00 73 65 74 74 69 6e 67 20 61 20 6a 75 6d 70 20 64  setting a jump d
ffa10 65 73 74 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 53  estination..*/.S
ffa20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
ffa30 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  id sqlite3VdbeCh
ffa40 61 6e 67 65 50 32 28 56 64 62 65 20 2a 70 2c 20  angeP2(Vdbe *p, 
ffa50 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 76 61  int addr, int va
ffa60 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 76 61  l){.  assert( va
ffa70 6c 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  l>=0 );.  assert
ffa80 28 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6d 61 67  ( p==0 || p->mag
ffa90 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49  ic==VDBE_MAGIC_I
ffaa0 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 70 20 26  NIT );.  if( p &
ffab0 26 20 61 64 64 72 3e 3d 30 20 26 26 20 70 2d 3e  & addr>=0 && p->
ffac0 6e 4f 70 3e 61 64 64 72 20 26 26 20 70 2d 3e 61  nOp>addr && p->a
ffad0 4f 70 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4f 70  Op ){.    p->aOp
ffae0 5b 61 64 64 72 5d 2e 70 32 20 3d 20 76 61 6c 3b  [addr].p2 = val;
ffaf0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68  .  }.}../*.** Ch
ffb00 61 6e 67 65 20 74 68 65 20 50 32 20 6f 70 65 72  ange the P2 oper
ffb10 61 6e 64 20 6f 66 20 69 6e 73 74 72 75 63 74 69  and of instructi
ffb20 6f 6e 20 61 64 64 72 20 73 6f 20 74 68 61 74 20  on addr so that 
ffb30 69 74 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20  it points to.** 
ffb40 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 74  the address of t
ffb50 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74  he next instruct
ffb60 69 6f 6e 20 74 6f 20 62 65 20 63 6f 64 65 64 2e  ion to be coded.
ffb70 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
ffb80 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
ffb90 64 62 65 4a 75 6d 70 48 65 72 65 28 56 64 62 65  dbeJumpHere(Vdbe
ffba0 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 29 7b 0a   *p, int addr){.
ffbb0 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
ffbc0 6e 67 65 50 32 28 70 2c 20 61 64 64 72 2c 20 70  ngeP2(p, addr, p
ffbd0 2d 3e 6e 4f 70 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  ->nOp);.}.../*.*
ffbe0 2a 20 49 66 20 74 68 65 20 69 6e 70 75 74 20 46  * If the input F
ffbf0 75 6e 63 44 65 66 20 73 74 72 75 63 74 75 72 65  uncDef structure
ffc00 20 69 73 20 65 70 68 65 6d 65 72 61 6c 2c 20 74   is ephemeral, t
ffc10 68 65 6e 20 66 72 65 65 20 69 74 2e 20 20 49 66  hen free it.  If
ffc20 0a 2a 2a 20 74 68 65 20 46 75 6e 63 44 65 66 20  .** the FuncDef 
ffc30 69 73 20 6e 6f 74 20 65 70 68 65 72 6d 61 6c 2c  is not ephermal,
ffc40 20 74 68 65 6e 20 64 6f 20 6e 6f 74 68 69 6e 67   then do nothing
ffc50 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
ffc60 20 66 72 65 65 45 70 68 65 6d 65 72 61 6c 46 75   freeEphemeralFu
ffc70 6e 63 74 69 6f 6e 28 46 75 6e 63 44 65 66 20 2a  nction(FuncDef *
ffc80 70 44 65 66 29 7b 0a 20 20 69 66 28 20 70 44 65  pDef){.  if( pDe
ffc90 66 20 26 26 20 28 70 44 65 66 2d 3e 66 6c 61 67  f && (pDef->flag
ffca0 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f  s & SQLITE_FUNC_
ffcb0 45 50 48 45 4d 29 21 3d 30 20 29 7b 0a 20 20 20  EPHEM)!=0 ){.   
ffcc0 20 73 71 6c 69 74 65 46 72 65 65 28 70 44 65 66   sqliteFree(pDef
ffcd0 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
ffce0 44 65 6c 65 74 65 20 61 20 50 33 20 76 61 6c 75  Delete a P3 valu
ffcf0 65 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a  e if necessary..
ffd00 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  */.static void f
ffd10 72 65 65 50 33 28 69 6e 74 20 70 33 74 79 70 65  reeP3(int p3type
ffd20 2c 20 76 6f 69 64 20 2a 70 33 29 7b 0a 20 20 69  , void *p3){.  i
ffd30 66 28 20 70 33 20 29 7b 0a 20 20 20 20 73 77 69  f( p3 ){.    swi
ffd40 74 63 68 28 20 70 33 74 79 70 65 20 29 7b 0a 20  tch( p3type ){. 
ffd50 20 20 20 20 20 63 61 73 65 20 50 33 5f 44 59 4e       case P3_DYN
ffd60 41 4d 49 43 3a 0a 20 20 20 20 20 20 63 61 73 65  AMIC:.      case
ffd70 20 50 33 5f 4b 45 59 49 4e 46 4f 3a 0a 20 20 20   P3_KEYINFO:.   
ffd80 20 20 20 63 61 73 65 20 50 33 5f 4b 45 59 49 4e     case P3_KEYIN
ffd90 46 4f 5f 48 41 4e 44 4f 46 46 3a 20 7b 0a 20 20  FO_HANDOFF: {.  
ffda0 20 20 20 20 20 20 73 71 6c 69 74 65 46 72 65 65        sqliteFree
ffdb0 28 70 33 29 3b 0a 20 20 20 20 20 20 20 20 62 72  (p3);.        br
ffdc0 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
ffdd0 20 20 20 63 61 73 65 20 50 33 5f 4d 50 52 49 4e     case P3_MPRIN
ffde0 54 46 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71  TF: {.        sq
ffdf0 6c 69 74 65 33 5f 66 72 65 65 28 70 33 29 3b 0a  lite3_free(p3);.
ffe00 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
ffe10 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73       }.      cas
ffe20 65 20 50 33 5f 56 44 42 45 46 55 4e 43 3a 20 7b  e P3_VDBEFUNC: {
ffe30 0a 20 20 20 20 20 20 20 20 56 64 62 65 46 75 6e  .        VdbeFun
ffe40 63 20 2a 70 56 64 62 65 46 75 6e 63 20 3d 20 28  c *pVdbeFunc = (
ffe50 56 64 62 65 46 75 6e 63 20 2a 29 70 33 3b 0a 20  VdbeFunc *)p3;. 
ffe60 20 20 20 20 20 20 20 66 72 65 65 45 70 68 65 6d         freeEphem
ffe70 65 72 61 6c 46 75 6e 63 74 69 6f 6e 28 70 56 64  eralFunction(pVd
ffe80 62 65 46 75 6e 63 2d 3e 70 46 75 6e 63 29 3b 0a  beFunc->pFunc);.
ffe90 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
ffea0 64 62 65 44 65 6c 65 74 65 41 75 78 44 61 74 61  dbeDeleteAuxData
ffeb0 28 70 56 64 62 65 46 75 6e 63 2c 20 30 29 3b 0a  (pVdbeFunc, 0);.
ffec0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 46 72          sqliteFr
ffed0 65 65 28 70 56 64 62 65 46 75 6e 63 29 3b 0a 20  ee(pVdbeFunc);. 
ffee0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
ffef0 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65      }.      case
fff00 20 50 33 5f 46 55 4e 43 44 45 46 3a 20 7b 0a 20   P3_FUNCDEF: {. 
fff10 20 20 20 20 20 20 20 66 72 65 65 45 70 68 65 6d         freeEphem
fff20 65 72 61 6c 46 75 6e 63 74 69 6f 6e 28 28 46 75  eralFunction((Fu
fff30 6e 63 44 65 66 2a 29 70 33 29 3b 0a 20 20 20 20  ncDef*)p3);.    
fff40 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
fff50 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50 33   }.      case P3
fff60 5f 4d 45 4d 3a 20 7b 0a 20 20 20 20 20 20 20 20  _MEM: {.        
fff70 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65  sqlite3ValueFree
fff80 28 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  ((sqlite3_value*
fff90 29 70 33 29 3b 0a 20 20 20 20 20 20 20 20 62 72  )p3);.        br
fffa0 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
fffb0 20 7d 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a   }.  }.}.../*.**
fffc0 20 43 68 61 6e 67 65 20 4e 20 6f 70 63 6f 64 65   Change N opcode
fffd0 73 20 73 74 61 72 74 69 6e 67 20 61 74 20 61 64  s starting at ad
fffe0 64 72 20 74 6f 20 4e 6f 2d 6f 70 73 2e 0a 2a 2f  dr to No-ops..*/
ffff0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
10000 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
10001 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 56 64 62  ChangeToNoop(Vdb
10002 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c 20  e *p, int addr, 
10003 69 6e 74 20 4e 29 7b 0a 20 20 69 66 28 20 70 20  int N){.  if( p 
10004 26 26 20 70 2d 3e 61 4f 70 20 29 7b 0a 20 20 20  && p->aOp ){.   
10005 20 56 64 62 65 4f 70 20 2a 70 4f 70 20 3d 20 26   VdbeOp *pOp = &
10006 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 3b 0a 20 20  p->aOp[addr];.  
10007 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a    while( N-- ){.
10008 20 20 20 20 20 20 66 72 65 65 50 33 28 70 4f 70        freeP3(pOp
10009 2d 3e 70 33 74 79 70 65 2c 20 70 4f 70 2d 3e 70  ->p3type, pOp->p
1000a 33 29 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74  3);.      memset
1000b 28 70 4f 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28  (pOp, 0, sizeof(
1000c 70 4f 70 5b 30 5d 29 29 3b 0a 20 20 20 20 20 20  pOp[0]));.      
1000d 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50  pOp->opcode = OP
1000e 5f 4e 6f 6f 70 3b 0a 20 20 20 20 20 20 70 4f 70  _Noop;.      pOp
1000f 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  ++;.    }.  }.}.
10010 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
10011 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50  e value of the P
10012 33 20 6f 70 65 72 61 6e 64 20 66 6f 72 20 61 20  3 operand for a 
10013 73 70 65 63 69 66 69 63 20 69 6e 73 74 72 75 63  specific instruc
10014 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 72 6f  tion..** This ro
10015 75 74 69 6e 65 20 69 73 20 75 73 65 66 75 6c 20  utine is useful 
10016 77 68 65 6e 20 61 20 6c 61 72 67 65 20 70 72 6f  when a large pro
10017 67 72 61 6d 20 69 73 20 6c 6f 61 64 65 64 20 66  gram is loaded f
10018 72 6f 6d 20 61 0a 2a 2a 20 73 74 61 74 69 63 20  rom a.** static 
10019 61 72 72 61 79 20 75 73 69 6e 67 20 73 71 6c 69  array using sqli
1001a 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74  te3VdbeAddOpList
1001b 20 62 75 74 20 77 65 20 77 61 6e 74 20 74 6f 20   but we want to 
1001c 6d 61 6b 65 20 61 0a 2a 2a 20 66 65 77 20 6d 69  make a.** few mi
1001d 6e 6f 72 20 63 68 61 6e 67 65 73 20 74 6f 20 74  nor changes to t
1001e 68 65 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a  he program..**.*
1001f 2a 20 49 66 20 6e 3e 3d 30 20 74 68 65 6e 20 74  * If n>=0 then t
10020 68 65 20 50 33 20 6f 70 65 72 61 6e 64 20 69 73  he P3 operand is
10021 20 64 79 6e 61 6d 69 63 2c 20 6d 65 61 6e 69 6e   dynamic, meanin
10022 67 20 74 68 61 74 20 61 20 63 6f 70 79 20 6f 66  g that a copy of
10023 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 69  .** the string i
10024 73 20 6d 61 64 65 20 69 6e 74 6f 20 6d 65 6d 6f  s made into memo
10025 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ry obtained from
10026 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e   sqliteMalloc().
10027 0a 2a 2a 20 41 20 76 61 6c 75 65 20 6f 66 20 6e  .** A value of n
10028 3d 3d 30 20 6d 65 61 6e 73 20 63 6f 70 79 20 62  ==0 means copy b
10029 79 74 65 73 20 6f 66 20 7a 50 33 20 75 70 20 74  ytes of zP3 up t
1002a 6f 20 61 6e 64 20 69 6e 63 6c 75 64 69 6e 67 20  o and including 
1002b 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 6e 75 6c  the.** first nul
1002c 6c 20 62 79 74 65 2e 20 20 49 66 20 6e 3e 30 20  l byte.  If n>0 
1002d 74 68 65 6e 20 63 6f 70 79 20 6e 2b 31 20 62 79  then copy n+1 by
1002e 74 65 73 20 6f 66 20 7a 50 33 2e 0a 2a 2a 0a 2a  tes of zP3..**.*
1002f 2a 20 49 66 20 6e 3d 3d 50 33 5f 4b 45 59 49 4e  * If n==P3_KEYIN
10030 46 4f 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74  FO it means that
10031 20 7a 50 33 20 69 73 20 61 20 70 6f 69 6e 74 65   zP3 is a pointe
10032 72 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 73  r to a KeyInfo s
10033 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 41 20 63  tructure..** A c
10034 6f 70 79 20 69 73 20 6d 61 64 65 20 6f 66 20 74  opy is made of t
10035 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63  he KeyInfo struc
10036 74 75 72 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 79  ture into memory
10037 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a   obtained from.*
10038 2a 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 2c 20  * sqliteMalloc, 
10039 74 6f 20 62 65 20 66 72 65 65 64 20 77 68 65 6e  to be freed when
1003a 20 74 68 65 20 56 64 62 65 20 69 73 20 66 69 6e   the Vdbe is fin
1003b 61 6c 69 7a 65 64 2e 0a 2a 2a 20 6e 3d 3d 50 33  alized..** n==P3
1003c 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46  _KEYINFO_HANDOFF
1003d 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20   indicates that 
1003e 7a 50 33 20 70 6f 69 6e 74 73 20 74 6f 20 61 20  zP3 points to a 
1003f 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72  KeyInfo structur
10040 65 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 6d  e.** stored in m
10041 65 6d 6f 72 79 20 74 68 61 74 20 74 68 65 20 63  emory that the c
10042 61 6c 6c 65 72 20 68 61 73 20 6f 62 74 61 69 6e  aller has obtain
10043 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61  ed from sqliteMa
10044 6c 6c 6f 63 2e 20 54 68 65 20 0a 2a 2a 20 63 61  lloc. The .** ca
10045 6c 6c 65 72 20 73 68 6f 75 6c 64 20 6e 6f 74 20  ller should not 
10046 66 72 65 65 20 74 68 65 20 61 6c 6c 6f 63 61 74  free the allocat
10047 69 6f 6e 2c 20 69 74 20 77 69 6c 6c 20 62 65 20  ion, it will be 
10048 66 72 65 65 64 20 77 68 65 6e 20 74 68 65 20 56  freed when the V
10049 64 62 65 20 69 73 0a 2a 2a 20 66 69 6e 61 6c 69  dbe is.** finali
1004a 7a 65 64 2e 0a 2a 2a 20 0a 2a 2a 20 4f 74 68 65  zed..** .** Othe
1004b 72 20 76 61 6c 75 65 73 20 6f 66 20 6e 20 28 50  r values of n (P
1004c 33 5f 53 54 41 54 49 43 2c 20 50 33 5f 43 4f 4c  3_STATIC, P3_COL
1004d 4c 53 45 51 20 65 74 63 2e 29 20 69 6e 64 69 63  LSEQ etc.) indic
1004e 61 74 65 20 74 68 61 74 20 7a 50 33 20 70 6f 69  ate that zP3 poi
1004f 6e 74 73 0a 2a 2a 20 74 6f 20 61 20 73 74 72 69  nts.** to a stri
10050 6e 67 20 6f 72 20 73 74 72 75 63 74 75 72 65 20  ng or structure 
10051 74 68 61 74 20 69 73 20 67 75 61 72 61 6e 74 65  that is guarante
10052 65 64 20 74 6f 20 65 78 69 73 74 20 66 6f 72 20  ed to exist for 
10053 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 0a  the lifetime of.
10054 2a 2a 20 74 68 65 20 56 64 62 65 2e 20 49 6e 20  ** the Vdbe. In 
10055 74 68 65 73 65 20 63 61 73 65 73 20 77 65 20 63  these cases we c
10056 61 6e 20 6a 75 73 74 20 63 6f 70 79 20 74 68 65  an just copy the
10057 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20   pointer..**.** 
10058 49 66 20 61 64 64 72 3c 30 20 74 68 65 6e 20 63  If addr<0 then c
10059 68 61 6e 67 65 20 50 33 20 6f 6e 20 74 68 65 20  hange P3 on the 
1005a 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 69 6e  most recently in
1005b 73 65 72 74 65 64 20 69 6e 73 74 72 75 63 74 69  serted instructi
1005c 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
1005d 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1005e 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 56  e3VdbeChangeP3(V
1005f 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72  dbe *p, int addr
10060 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50  , const char *zP
10061 33 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 4f 70 20  3, int n){.  Op 
10062 2a 70 4f 70 3b 0a 20 20 61 73 73 65 72 74 28 20  *pOp;.  assert( 
10063 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6d 61 67 69 63  p==0 || p->magic
10064 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49  ==VDBE_MAGIC_INI
10065 54 20 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20  T );.  if( p==0 
10066 7c 7c 20 70 2d 3e 61 4f 70 3d 3d 30 20 7c 7c 20  || p->aOp==0 || 
10067 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69  sqlite3MallocFai
10068 6c 65 64 28 29 20 29 7b 0a 20 20 20 20 69 66 20  led() ){.    if 
10069 28 6e 20 21 3d 20 50 33 5f 4b 45 59 49 4e 46 4f  (n != P3_KEYINFO
1006a 29 20 7b 0a 20 20 20 20 20 20 66 72 65 65 50 33  ) {.      freeP3
1006b 28 6e 2c 20 28 76 6f 69 64 2a 29 2a 28 63 68 61  (n, (void*)*(cha
1006c 72 2a 2a 29 26 7a 50 33 29 3b 0a 20 20 20 20 7d  r**)&zP3);.    }
1006d 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
1006e 0a 20 20 69 66 28 20 61 64 64 72 3c 30 20 7c 7c  .  if( addr<0 ||
1006f 20 61 64 64 72 3e 3d 70 2d 3e 6e 4f 70 20 29 7b   addr>=p->nOp ){
10070 0a 20 20 20 20 61 64 64 72 20 3d 20 70 2d 3e 6e  .    addr = p->n
10071 4f 70 20 2d 20 31 3b 0a 20 20 20 20 69 66 28 20  Op - 1;.    if( 
10072 61 64 64 72 3c 30 20 29 20 72 65 74 75 72 6e 3b  addr<0 ) return;
10073 0a 20 20 7d 0a 20 20 70 4f 70 20 3d 20 26 70 2d  .  }.  pOp = &p-
10074 3e 61 4f 70 5b 61 64 64 72 5d 3b 0a 20 20 66 72  >aOp[addr];.  fr
10075 65 65 50 33 28 70 4f 70 2d 3e 70 33 74 79 70 65  eeP3(pOp->p3type
10076 2c 20 70 4f 70 2d 3e 70 33 29 3b 0a 20 20 70 4f  , pOp->p3);.  pO
10077 70 2d 3e 70 33 20 3d 20 30 3b 0a 20 20 69 66 28  p->p3 = 0;.  if(
10078 20 7a 50 33 3d 3d 30 20 29 7b 0a 20 20 20 20 70   zP3==0 ){.    p
10079 4f 70 2d 3e 70 33 20 3d 20 30 3b 0a 20 20 20 20  Op->p3 = 0;.    
1007a 70 4f 70 2d 3e 70 33 74 79 70 65 20 3d 20 50 33  pOp->p3type = P3
1007b 5f 4e 4f 54 55 53 45 44 3b 0a 20 20 7d 65 6c 73  _NOTUSED;.  }els
1007c 65 20 69 66 28 20 6e 3d 3d 50 33 5f 4b 45 59 49  e if( n==P3_KEYI
1007d 4e 46 4f 20 29 7b 0a 20 20 20 20 4b 65 79 49 6e  NFO ){.    KeyIn
1007e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20  fo *pKeyInfo;.  
1007f 20 20 69 6e 74 20 6e 46 69 65 6c 64 2c 20 6e 42    int nField, nB
10080 79 74 65 3b 0a 0a 20 20 20 20 6e 46 69 65 6c 64  yte;..    nField
10081 20 3d 20 28 28 4b 65 79 49 6e 66 6f 2a 29 7a 50   = ((KeyInfo*)zP
10082 33 29 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 20 20  3)->nField;.    
10083 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 2a  nByte = sizeof(*
10084 70 4b 65 79 49 6e 66 6f 29 20 2b 20 28 6e 46 69  pKeyInfo) + (nFi
10085 65 6c 64 2d 31 29 2a 73 69 7a 65 6f 66 28 70 4b  eld-1)*sizeof(pK
10086 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 30 5d  eyInfo->aColl[0]
10087 29 20 2b 20 6e 46 69 65 6c 64 3b 0a 20 20 20 20  ) + nField;.    
10088 70 4b 65 79 49 6e 66 6f 20 3d 20 73 71 6c 69 74  pKeyInfo = sqlit
10089 65 4d 61 6c 6c 6f 63 52 61 77 28 20 6e 42 79 74  eMallocRaw( nByt
1008a 65 20 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 33  e );.    pOp->p3
1008b 20 3d 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e   = (char*)pKeyIn
1008c 66 6f 3b 0a 20 20 20 20 69 66 28 20 70 4b 65 79  fo;.    if( pKey
1008d 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 75 6e  Info ){.      un
1008e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 53 6f  signed char *aSo
1008f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 20 20 6d  rtOrder;.      m
10090 65 6d 63 70 79 28 70 4b 65 79 49 6e 66 6f 2c 20  emcpy(pKeyInfo, 
10091 7a 50 33 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20  zP3, nByte);.   
10092 20 20 20 61 53 6f 72 74 4f 72 64 65 72 20 3d 20     aSortOrder = 
10093 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f  pKeyInfo->aSortO
10094 72 64 65 72 3b 0a 20 20 20 20 20 20 69 66 28 20  rder;.      if( 
10095 61 53 6f 72 74 4f 72 64 65 72 20 29 7b 0a 20 20  aSortOrder ){.  
10096 20 20 20 20 20 20 70 4b 65 79 49 6e 66 6f 2d 3e        pKeyInfo->
10097 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 6e  aSortOrder = (un
10098 73 69 67 6e 65 64 20 63 68 61 72 2a 29 26 70 4b  signed char*)&pK
10099 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 6e 46  eyInfo->aColl[nF
1009a 69 65 6c 64 5d 3b 0a 20 20 20 20 20 20 20 20 6d  ield];.        m
1009b 65 6d 63 70 79 28 70 4b 65 79 49 6e 66 6f 2d 3e  emcpy(pKeyInfo->
1009c 61 53 6f 72 74 4f 72 64 65 72 2c 20 61 53 6f 72  aSortOrder, aSor
1009d 74 4f 72 64 65 72 2c 20 6e 46 69 65 6c 64 29 3b  tOrder, nField);
1009e 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
1009f 4f 70 2d 3e 70 33 74 79 70 65 20 3d 20 50 33 5f  Op->p3type = P3_
100a0 4b 45 59 49 4e 46 4f 3b 0a 20 20 20 20 7d 65 6c  KEYINFO;.    }el
100a1 73 65 7b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 70  se{.      pOp->p
100a2 33 74 79 70 65 20 3d 20 50 33 5f 4e 4f 54 55 53  3type = P3_NOTUS
100a3 45 44 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  ED;.    }.  }els
100a4 65 20 69 66 28 20 6e 3d 3d 50 33 5f 4b 45 59 49  e if( n==P3_KEYI
100a5 4e 46 4f 5f 48 41 4e 44 4f 46 46 20 29 7b 0a 20  NFO_HANDOFF ){. 
100a6 20 20 20 70 4f 70 2d 3e 70 33 20 3d 20 28 63 68     pOp->p3 = (ch
100a7 61 72 2a 29 7a 50 33 3b 0a 20 20 20 20 70 4f 70  ar*)zP3;.    pOp
100a8 2d 3e 70 33 74 79 70 65 20 3d 20 50 33 5f 4b 45  ->p3type = P3_KE
100a9 59 49 4e 46 4f 3b 0a 20 20 7d 65 6c 73 65 20 69  YINFO;.  }else i
100aa 66 28 20 6e 3c 30 20 29 7b 0a 20 20 20 20 70 4f  f( n<0 ){.    pO
100ab 70 2d 3e 70 33 20 3d 20 28 63 68 61 72 2a 29 7a  p->p3 = (char*)z
100ac 50 33 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 33 74  P3;.    pOp->p3t
100ad 79 70 65 20 3d 20 6e 3b 0a 20 20 7d 65 6c 73 65  ype = n;.  }else
100ae 7b 0a 20 20 20 20 69 66 28 20 6e 3d 3d 30 20 29  {.    if( n==0 )
100af 20 6e 20 3d 20 73 74 72 6c 65 6e 28 7a 50 33 29   n = strlen(zP3)
100b0 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 33 20 3d 20  ;.    pOp->p3 = 
100b1 73 71 6c 69 74 65 53 74 72 4e 44 75 70 28 7a 50  sqliteStrNDup(zP
100b2 33 2c 20 6e 29 3b 0a 20 20 20 20 70 4f 70 2d 3e  3, n);.    pOp->
100b3 70 33 74 79 70 65 20 3d 20 50 33 5f 44 59 4e 41  p3type = P3_DYNA
100b4 4d 49 43 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e  MIC;.  }.}..#ifn
100b5 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a  def NDEBUG./*.**
100b6 20 52 65 70 6c 61 63 65 20 74 68 65 20 50 33 20   Replace the P3 
100b7 66 69 65 6c 64 20 6f 66 20 74 68 65 20 6d 6f 73  field of the mos
100b8 74 20 72 65 63 65 6e 74 6c 79 20 63 6f 64 65 64  t recently coded
100b9 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 69 74   instruction wit
100ba 68 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 20 74 65 78  h.** comment tex
100bb 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
100bc 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
100bd 33 56 64 62 65 43 6f 6d 6d 65 6e 74 28 56 64 62  3VdbeComment(Vdb
100be 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72  e *p, const char
100bf 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b   *zFormat, ...){
100c0 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20  .  va_list ap;. 
100c1 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e   assert( p->nOp>
100c2 30 20 7c 7c 20 70 2d 3e 61 4f 70 3d 3d 30 20 29  0 || p->aOp==0 )
100c3 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61  ;.  assert( p->a
100c4 4f 70 3d 3d 30 20 7c 7c 20 70 2d 3e 61 4f 70 5b  Op==0 || p->aOp[
100c5 70 2d 3e 6e 4f 70 2d 31 5d 2e 70 33 3d 3d 30 20  p->nOp-1].p3==0 
100c6 7c 7c 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  || sqlite3Malloc
100c7 46 61 69 6c 65 64 28 29 20 29 3b 0a 20 20 76 61  Failed() );.  va
100c8 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d  _start(ap, zForm
100c9 61 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  at);.  sqlite3Vd
100ca 62 65 43 68 61 6e 67 65 50 33 28 70 2c 20 2d 31  beChangeP3(p, -1
100cb 2c 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74  , sqlite3VMPrint
100cc 66 28 7a 46 6f 72 6d 61 74 2c 20 61 70 29 2c 20  f(zFormat, ap), 
100cd 50 33 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 76  P3_DYNAMIC);.  v
100ce 61 5f 65 6e 64 28 61 70 29 3b 0a 7d 0a 23 65 6e  a_end(ap);.}.#en
100cf 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  dif../*.** Retur
100d0 6e 20 74 68 65 20 6f 70 63 6f 64 65 20 66 6f 72  n the opcode for
100d1 20 61 20 67 69 76 65 6e 20 61 64 64 72 65 73 73   a given address
100d2 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
100d3 41 54 45 20 56 64 62 65 4f 70 20 2a 73 71 6c 69  ATE VdbeOp *sqli
100d4 74 65 33 56 64 62 65 47 65 74 4f 70 28 56 64 62  te3VdbeGetOp(Vdb
100d5 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 29 7b  e *p, int addr){
100d6 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61  .  assert( p->ma
100d7 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f  gic==VDBE_MAGIC_
100d8 49 4e 49 54 20 29 3b 0a 20 20 61 73 73 65 72 74  INIT );.  assert
100d9 28 20 28 61 64 64 72 3e 3d 30 20 26 26 20 61 64  ( (addr>=0 && ad
100da 64 72 3c 70 2d 3e 6e 4f 70 29 20 7c 7c 20 73 71  dr<p->nOp) || sq
100db 6c 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69 6c 65  lite3MallocFaile
100dc 64 28 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  d() );.  return 
100dd 28 28 61 64 64 72 3e 3d 30 20 26 26 20 61 64 64  ((addr>=0 && add
100de 72 3c 70 2d 3e 6e 4f 70 29 3f 28 26 70 2d 3e 61  r<p->nOp)?(&p->a
100df 4f 70 5b 61 64 64 72 5d 29 3a 30 29 3b 0a 7d 0a  Op[addr]):0);.}.
100e0 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
100e1 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49  LITE_OMIT_EXPLAI
100e2 4e 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 4e  N) || !defined(N
100e3 44 45 42 55 47 29 20 5c 0a 20 20 20 20 20 7c 7c  DEBUG) \.     ||
100e4 20 64 65 66 69 6e 65 64 28 56 44 42 45 5f 50 52   defined(VDBE_PR
100e5 4f 46 49 4c 45 29 20 7c 7c 20 64 65 66 69 6e 65  OFILE) || define
100e6 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a  d(SQLITE_DEBUG).
100e7 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 61 20  /*.** Compute a 
100e8 73 74 72 69 6e 67 20 74 68 61 74 20 64 65 73 63  string that desc
100e9 72 69 62 65 73 20 74 68 65 20 50 33 20 70 61 72  ribes the P3 par
100ea 61 6d 65 74 65 72 20 66 6f 72 20 61 6e 20 6f 70  ameter for an op
100eb 63 6f 64 65 2e 0a 2a 2a 20 55 73 65 20 7a 54 65  code..** Use zTe
100ec 6d 70 20 66 6f 72 20 61 6e 79 20 72 65 71 75 69  mp for any requi
100ed 72 65 64 20 74 65 6d 70 6f 72 61 72 79 20 62 75  red temporary bu
100ee 66 66 65 72 20 73 70 61 63 65 2e 0a 2a 2f 0a 73  ffer space..*/.s
100ef 74 61 74 69 63 20 63 68 61 72 20 2a 64 69 73 70  tatic char *disp
100f0 6c 61 79 50 33 28 4f 70 20 2a 70 4f 70 2c 20 63  layP3(Op *pOp, c
100f1 68 61 72 20 2a 7a 54 65 6d 70 2c 20 69 6e 74 20  har *zTemp, int 
100f2 6e 54 65 6d 70 29 7b 0a 20 20 63 68 61 72 20 2a  nTemp){.  char *
100f3 7a 50 33 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  zP3;.  assert( n
100f4 54 65 6d 70 3e 3d 32 30 20 29 3b 0a 20 20 73 77  Temp>=20 );.  sw
100f5 69 74 63 68 28 20 70 4f 70 2d 3e 70 33 74 79 70  itch( pOp->p3typ
100f6 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 50 33  e ){.    case P3
100f7 5f 4b 45 59 49 4e 46 4f 3a 20 7b 0a 20 20 20 20  _KEYINFO: {.    
100f8 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 20 20    int i, j;.    
100f9 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49    KeyInfo *pKeyI
100fa 6e 66 6f 20 3d 20 28 4b 65 79 49 6e 66 6f 2a 29  nfo = (KeyInfo*)
100fb 70 4f 70 2d 3e 70 33 3b 0a 20 20 20 20 20 20 73  pOp->p3;.      s
100fc 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
100fd 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 6b  nTemp, zTemp, "k
100fe 65 79 69 6e 66 6f 28 25 64 22 2c 20 70 4b 65 79  eyinfo(%d", pKey
100ff 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 29 3b 0a 20  Info->nField);. 
10100 20 20 20 20 20 69 20 3d 20 73 74 72 6c 65 6e 28       i = strlen(
10101 7a 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 66 6f  zTemp);.      fo
10102 72 28 6a 3d 30 3b 20 6a 3c 70 4b 65 79 49 6e 66  r(j=0; j<pKeyInf
10103 6f 2d 3e 6e 46 69 65 6c 64 3b 20 6a 2b 2b 29 7b  o->nField; j++){
10104 0a 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71  .        CollSeq
10105 20 2a 70 43 6f 6c 6c 20 3d 20 70 4b 65 79 49 6e   *pColl = pKeyIn
10106 66 6f 2d 3e 61 43 6f 6c 6c 5b 6a 5d 3b 0a 20 20  fo->aColl[j];.  
10107 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20        if( pColl 
10108 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74  ){.          int
10109 20 6e 20 3d 20 73 74 72 6c 65 6e 28 70 43 6f 6c   n = strlen(pCol
1010a 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  l->zName);.     
1010b 20 20 20 20 20 69 66 28 20 69 2b 6e 3e 6e 54 65       if( i+n>nTe
1010c 6d 70 2d 36 20 29 7b 0a 20 20 20 20 20 20 20 20  mp-6 ){.        
1010d 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d      memcpy(&zTem
1010e 70 5b 69 5d 2c 22 2c 2e 2e 2e 22 2c 34 29 3b 0a  p[i],",...",4);.
1010f 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61              brea
10110 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  k;.          }. 
10111 20 20 20 20 20 20 20 20 20 7a 54 65 6d 70 5b 69           zTemp[i
10112 2b 2b 5d 20 3d 20 27 2c 27 3b 0a 20 20 20 20 20  ++] = ',';.     
10113 20 20 20 20 20 69 66 28 20 70 4b 65 79 49 6e 66       if( pKeyInf
10114 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 26 26  o->aSortOrder &&
10115 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74   pKeyInfo->aSort
10116 4f 72 64 65 72 5b 6a 5d 20 29 7b 0a 20 20 20 20  Order[j] ){.    
10117 20 20 20 20 20 20 20 20 7a 54 65 6d 70 5b 69 2b          zTemp[i+
10118 2b 5d 20 3d 20 27 2d 27 3b 0a 20 20 20 20 20 20  +] = '-';.      
10119 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
1011a 6d 65 6d 63 70 79 28 26 7a 54 65 6d 70 5b 69 5d  memcpy(&zTemp[i]
1011b 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 2c 6e  , pColl->zName,n
1011c 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  +1);.          i
1011d 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 20 20 20 7d   += n;.        }
1011e 65 6c 73 65 20 69 66 28 20 69 2b 34 3c 6e 54 65  else if( i+4<nTe
1011f 6d 70 2d 36 20 29 7b 0a 20 20 20 20 20 20 20 20  mp-6 ){.        
10120 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d 70 5b    memcpy(&zTemp[
10121 69 5d 2c 22 2c 6e 69 6c 22 2c 34 29 3b 0a 20 20  i],",nil",4);.  
10122 20 20 20 20 20 20 20 20 69 20 2b 3d 20 34 3b 0a          i += 4;.
10123 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
10124 7d 0a 20 20 20 20 20 20 7a 54 65 6d 70 5b 69 2b  }.      zTemp[i+
10125 2b 5d 20 3d 20 27 29 27 3b 0a 20 20 20 20 20 20  +] = ')';.      
10126 7a 54 65 6d 70 5b 69 5d 20 3d 20 30 3b 0a 20 20  zTemp[i] = 0;.  
10127 20 20 20 20 61 73 73 65 72 74 28 20 69 3c 6e 54      assert( i<nT
10128 65 6d 70 20 29 3b 0a 20 20 20 20 20 20 7a 50 33  emp );.      zP3
10129 20 3d 20 7a 54 65 6d 70 3b 0a 20 20 20 20 20 20   = zTemp;.      
1012a 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
1012b 20 63 61 73 65 20 50 33 5f 43 4f 4c 4c 53 45 51   case P3_COLLSEQ
1012c 3a 20 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65  : {.      CollSe
1012d 71 20 2a 70 43 6f 6c 6c 20 3d 20 28 43 6f 6c 6c  q *pColl = (Coll
1012e 53 65 71 2a 29 70 4f 70 2d 3e 70 33 3b 0a 20 20  Seq*)pOp->p3;.  
1012f 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
10130 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d  intf(nTemp, zTem
10131 70 2c 20 22 63 6f 6c 6c 73 65 71 28 25 2e 32 30  p, "collseq(%.20
10132 73 29 22 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d  s)", pColl->zNam
10133 65 29 3b 0a 20 20 20 20 20 20 7a 50 33 20 3d 20  e);.      zP3 = 
10134 7a 54 65 6d 70 3b 0a 20 20 20 20 20 20 62 72 65  zTemp;.      bre
10135 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
10136 73 65 20 50 33 5f 46 55 4e 43 44 45 46 3a 20 7b  se P3_FUNCDEF: {
10137 0a 20 20 20 20 20 20 46 75 6e 63 44 65 66 20 2a  .      FuncDef *
10138 70 44 65 66 20 3d 20 28 46 75 6e 63 44 65 66 2a  pDef = (FuncDef*
10139 29 70 4f 70 2d 3e 70 33 3b 0a 20 20 20 20 20 20  )pOp->p3;.      
1013a 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1013b 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22  (nTemp, zTemp, "
1013c 25 73 28 25 64 29 22 2c 20 70 44 65 66 2d 3e 7a  %s(%d)", pDef->z
1013d 4e 61 6d 65 2c 20 70 44 65 66 2d 3e 6e 41 72 67  Name, pDef->nArg
1013e 29 3b 0a 20 20 20 20 20 20 7a 50 33 20 3d 20 7a  );.      zP3 = z
1013f 54 65 6d 70 3b 0a 20 20 20 20 20 20 62 72 65 61  Temp;.      brea
10140 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66  k;.    }.#ifndef
10141 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
10142 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 63 61  TUALTABLE.    ca
10143 73 65 20 50 33 5f 56 54 41 42 3a 20 7b 0a 20 20  se P3_VTAB: {.  
10144 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62      sqlite3_vtab
10145 20 2a 70 56 74 61 62 20 3d 20 28 73 71 6c 69 74   *pVtab = (sqlit
10146 65 33 5f 76 74 61 62 2a 29 70 4f 70 2d 3e 70 33  e3_vtab*)pOp->p3
10147 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
10148 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20  snprintf(nTemp, 
10149 7a 54 65 6d 70 2c 20 22 76 74 61 62 3a 25 70 3a  zTemp, "vtab:%p:
1014a 25 70 22 2c 20 70 56 74 61 62 2c 20 70 56 74 61  %p", pVtab, pVta
1014b 62 2d 3e 70 4d 6f 64 75 6c 65 29 3b 0a 20 20 20  b->pModule);.   
1014c 20 20 20 7a 50 33 20 3d 20 7a 54 65 6d 70 3b 0a     zP3 = zTemp;.
1014d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1014e 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 64 65   }.#endif.    de
1014f 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 7a  fault: {.      z
10150 50 33 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20  P3 = pOp->p3;.  
10151 20 20 20 20 69 66 28 20 7a 50 33 3d 3d 30 20 7c      if( zP3==0 |
10152 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f  | pOp->opcode==O
10153 50 5f 4e 6f 6f 70 20 29 7b 0a 20 20 20 20 20 20  P_Noop ){.      
10154 20 20 7a 50 33 20 3d 20 22 22 3b 0a 20 20 20 20    zP3 = "";.    
10155 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
10156 61 73 73 65 72 74 28 20 7a 50 33 21 3d 30 20 29  assert( zP3!=0 )
10157 3b 0a 20 20 72 65 74 75 72 6e 20 7a 50 33 3b 0a  ;.  return zP3;.
10158 7d 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 20 64  }.#endif...#if d
10159 65 66 69 6e 65 64 28 56 44 42 45 5f 50 52 4f 46  efined(VDBE_PROF
1015a 49 4c 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  ILE) || defined(
1015b 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a  SQLITE_DEBUG)./*
1015c 0a 2a 2a 20 50 72 69 6e 74 20 61 20 73 69 6e 67  .** Print a sing
1015d 6c 65 20 6f 70 63 6f 64 65 2e 20 20 54 68 69 73  le opcode.  This
1015e 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64   routine is used
1015f 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67 20 6f   for debugging o
10160 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  nly..*/.SQLITE_P
10161 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
10162 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 46  te3VdbePrintOp(F
10163 49 4c 45 20 2a 70 4f 75 74 2c 20 69 6e 74 20 70  ILE *pOut, int p
10164 63 2c 20 4f 70 20 2a 70 4f 70 29 7b 0a 20 20 63  c, Op *pOp){.  c
10165 68 61 72 20 2a 7a 50 33 3b 0a 20 20 63 68 61 72  har *zP3;.  char
10166 20 7a 50 74 72 5b 35 30 5d 3b 0a 20 20 73 74 61   zPtr[50];.  sta
10167 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
10168 7a 46 6f 72 6d 61 74 31 20 3d 20 22 25 34 64 20  zFormat1 = "%4d 
10169 25 2d 31 33 73 20 25 34 64 20 25 34 64 20 25 73  %-13s %4d %4d %s
1016a 5c 6e 22 3b 0a 20 20 69 66 28 20 70 4f 75 74 3d  \n";.  if( pOut=
1016b 3d 30 20 29 20 70 4f 75 74 20 3d 20 73 74 64 6f  =0 ) pOut = stdo
1016c 75 74 3b 0a 20 20 7a 50 33 20 3d 20 64 69 73 70  ut;.  zP3 = disp
1016d 6c 61 79 50 33 28 70 4f 70 2c 20 7a 50 74 72 2c  layP3(pOp, zPtr,
1016e 20 73 69 7a 65 6f 66 28 7a 50 74 72 29 29 3b 0a   sizeof(zPtr));.
1016f 20 20 66 70 72 69 6e 74 66 28 70 4f 75 74 2c 20    fprintf(pOut, 
10170 7a 46 6f 72 6d 61 74 31 2c 0a 20 20 20 20 20 20  zFormat1,.      
10171 70 63 2c 20 73 71 6c 69 74 65 33 4f 70 63 6f 64  pc, sqlite3Opcod
10172 65 4e 61 6d 65 73 5b 70 4f 70 2d 3e 6f 70 63 6f  eNames[pOp->opco
10173 64 65 5d 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f  de], pOp->p1, pO
10174 70 2d 3e 70 32 2c 20 7a 50 33 29 3b 0a 20 20 66  p->p2, zP3);.  f
10175 66 6c 75 73 68 28 70 4f 75 74 29 3b 0a 7d 0a 23  flush(pOut);.}.#
10176 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c  endif../*.** Rel
10177 65 61 73 65 20 61 6e 20 61 72 72 61 79 20 6f 66  ease an array of
10178 20 4e 20 4d 65 6d 20 65 6c 65 6d 65 6e 74 73 0a   N Mem elements.
10179 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72  */.static void r
1017a 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 4d  eleaseMemArray(M
1017b 65 6d 20 2a 70 2c 20 69 6e 74 20 4e 29 7b 0a 20  em *p, int N){. 
1017c 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 77 68   if( p ){.    wh
1017d 69 6c 65 28 20 4e 2d 2d 3e 30 20 29 7b 0a 20 20  ile( N-->0 ){.  
1017e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1017f 65 6d 52 65 6c 65 61 73 65 28 70 2b 2b 29 3b 0a  emRelease(p++);.
10180 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 69 66      }.  }.}..#if
10181 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
10182 5f 45 58 50 4c 41 49 4e 0a 2f 2a 0a 2a 2a 20 47  _EXPLAIN./*.** G
10183 69 76 65 20 61 20 6c 69 73 74 69 6e 67 20 6f 66  ive a listing of
10184 20 74 68 65 20 70 72 6f 67 72 61 6d 20 69 6e 20   the program in 
10185 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68  the virtual mach
10186 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69  ine..**.** The i
10187 6e 74 65 72 66 61 63 65 20 69 73 20 74 68 65 20  nterface is the 
10188 73 61 6d 65 20 61 73 20 73 71 6c 69 74 65 33 56  same as sqlite3V
10189 64 62 65 45 78 65 63 28 29 2e 20 20 42 75 74 20  dbeExec().  But 
1018a 69 6e 73 74 65 61 64 20 6f 66 0a 2a 2a 20 72 75  instead of.** ru
1018b 6e 6e 69 6e 67 20 74 68 65 20 63 6f 64 65 2c 20  nning the code, 
1018c 69 74 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 63  it invokes the c
1018d 61 6c 6c 62 61 63 6b 20 6f 6e 63 65 20 66 6f 72  allback once for
1018e 20 65 61 63 68 20 69 6e 73 74 72 75 63 74 69 6f   each instructio
1018f 6e 2e 0a 2a 2a 20 54 68 69 73 20 66 65 61 74 75  n..** This featu
10190 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 69 6d  re is used to im
10191 70 6c 65 6d 65 6e 74 20 22 45 58 50 4c 41 49 4e  plement "EXPLAIN
10192 22 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  "..*/.SQLITE_PRI
10193 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
10194 56 64 62 65 4c 69 73 74 28 0a 20 20 56 64 62 65  VdbeList(.  Vdbe
10195 20 2a 70 20 20 20 20 20 20 20 20 20 20 20 20 20   *p             
10196 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42        /* The VDB
10197 45 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65  E */.){.  sqlite
10198 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20  3 *db = p->db;. 
10199 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 72 63   int i;.  int rc
1019a 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20   = SQLITE_OK;.. 
1019b 20 61 73 73 65 72 74 28 20 70 2d 3e 65 78 70 6c   assert( p->expl
1019c 61 69 6e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e  ain );.  if( p->
1019d 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 49  magic!=VDBE_MAGI
1019e 43 5f 52 55 4e 20 29 20 72 65 74 75 72 6e 20 53  C_RUN ) return S
1019f 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20  QLITE_MISUSE;.  
101a0 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 67 69  assert( db->magi
101a1 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f  c==SQLITE_MAGIC_
101a2 42 55 53 59 20 29 3b 0a 20 20 61 73 73 65 72 74  BUSY );.  assert
101a3 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f  ( p->rc==SQLITE_
101a4 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c  OK || p->rc==SQL
101a5 49 54 45 5f 42 55 53 59 20 29 3b 0a 0a 20 20 2f  ITE_BUSY );..  /
101a6 2a 20 45 76 65 6e 20 74 68 6f 75 67 68 20 74 68  * Even though th
101a7 69 73 20 6f 70 63 6f 64 65 20 64 6f 65 73 20 6e  is opcode does n
101a8 6f 74 20 70 75 74 20 64 79 6e 61 6d 69 63 20 73  ot put dynamic s
101a9 74 72 69 6e 67 73 20 6f 6e 74 6f 20 74 68 65 0a  trings onto the.
101aa 20 20 2a 2a 20 74 68 65 20 73 74 61 63 6b 2c 20    ** the stack, 
101ab 74 68 65 79 20 6d 61 79 20 62 65 63 6f 6d 65 20  they may become 
101ac 64 79 6e 61 6d 69 63 20 69 66 20 74 68 65 20 75  dynamic if the u
101ad 73 65 72 20 63 61 6c 6c 73 0a 20 20 2a 2a 20 73  ser calls.  ** s
101ae 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
101af 78 74 31 36 28 29 2c 20 63 61 75 73 69 6e 67 20  xt16(), causing 
101b0 61 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 74 6f  a translation to
101b1 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 69 6e 67   UTF-16 encoding
101b2 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e  ..  */.  if( p->
101b3 70 54 6f 73 3d 3d 26 70 2d 3e 61 53 74 61 63 6b  pTos==&p->aStack
101b4 5b 34 5d 20 29 7b 0a 20 20 20 20 72 65 6c 65 61  [4] ){.    relea
101b5 73 65 4d 65 6d 41 72 72 61 79 28 70 2d 3e 61 53  seMemArray(p->aS
101b6 74 61 63 6b 2c 20 35 29 3b 0a 20 20 7d 0a 20 20  tack, 5);.  }.  
101b7 70 2d 3e 72 65 73 4f 6e 53 74 61 63 6b 20 3d 20  p->resOnStack = 
101b8 30 3b 0a 0a 20 20 64 6f 7b 0a 20 20 20 20 69 20  0;..  do{.    i 
101b9 3d 20 70 2d 3e 70 63 2b 2b 3b 0a 20 20 7d 77 68  = p->pc++;.  }wh
101ba 69 6c 65 28 20 69 3c 70 2d 3e 6e 4f 70 20 26 26  ile( i<p->nOp &&
101bb 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 26   p->explain==2 &
101bc 26 20 70 2d 3e 61 4f 70 5b 69 5d 2e 6f 70 63 6f  & p->aOp[i].opco
101bd 64 65 21 3d 4f 50 5f 45 78 70 6c 61 69 6e 20 29  de!=OP_Explain )
101be 3b 0a 20 20 69 66 28 20 69 3e 3d 70 2d 3e 6e 4f  ;.  if( i>=p->nO
101bf 70 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d  p ){.    p->rc =
101c0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
101c1 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45  rc = SQLITE_DONE
101c2 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 64 62  ;.  }else if( db
101c3 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74  ->u1.isInterrupt
101c4 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20  ed ){.    p->rc 
101c5 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55  = SQLITE_INTERRU
101c6 50 54 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  PT;.    rc = SQL
101c7 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73  ITE_ERROR;.    s
101c8 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
101c9 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 73 71 6c  &p->zErrMsg, sql
101ca 69 74 65 33 45 72 72 53 74 72 28 70 2d 3e 72 63  ite3ErrStr(p->rc
101cb 29 2c 20 28 63 68 61 72 2a 29 30 29 3b 0a 20 20  ), (char*)0);.  
101cc 7d 65 6c 73 65 7b 0a 20 20 20 20 4f 70 20 2a 70  }else{.    Op *p
101cd 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 69 5d 3b  Op = &p->aOp[i];
101ce 0a 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d  .    Mem *pMem =
101cf 20 70 2d 3e 61 53 74 61 63 6b 3b 0a 20 20 20 20   p->aStack;.    
101d0 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
101d1 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 4d 65 6d 2d  M_Int;.    pMem-
101d2 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49  >type = SQLITE_I
101d3 4e 54 45 47 45 52 3b 0a 20 20 20 20 70 4d 65 6d  NTEGER;.    pMem
101d4 2d 3e 75 2e 69 20 3d 20 69 3b 20 20 20 20 20 20  ->u.i = i;      
101d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
101d6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 6f            /* Pro
101d7 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  gram counter */.
101d8 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20      pMem++;..   
101d9 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
101da 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 53 74  EM_Static|MEM_St
101db 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20  r|MEM_Term;.    
101dc 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 2a  pMem->z = (char*
101dd 29 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61  )sqlite3OpcodeNa
101de 6d 65 73 5b 70 4f 70 2d 3e 6f 70 63 6f 64 65 5d  mes[pOp->opcode]
101df 3b 20 20 2f 2a 20 4f 70 63 6f 64 65 20 2a 2f 0a  ;  /* Opcode */.
101e0 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d      assert( pMem
101e1 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 20 20 70 4d  ->z!=0 );.    pM
101e2 65 6d 2d 3e 6e 20 3d 20 73 74 72 6c 65 6e 28 70  em->n = strlen(p
101e3 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 70 4d 65  Mem->z);.    pMe
101e4 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45  m->type = SQLITE
101e5 5f 54 45 58 54 3b 0a 20 20 20 20 70 4d 65 6d 2d  _TEXT;.    pMem-
101e6 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54  >enc = SQLITE_UT
101e7 46 38 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a  F8;.    pMem++;.
101e8 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  .    pMem->flags
101e9 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20   = MEM_Int;.    
101ea 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d  pMem->u.i = pOp-
101eb 3e 70 31 3b 20 20 20 20 20 20 20 20 20 20 20 20  >p1;            
101ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
101ed 20 50 31 20 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d   P1 */.    pMem-
101ee 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49  >type = SQLITE_I
101ef 4e 54 45 47 45 52 3b 0a 20 20 20 20 70 4d 65 6d  NTEGER;.    pMem
101f0 2b 2b 3b 0a 0a 20 20 20 20 70 4d 65 6d 2d 3e 66  ++;..    pMem->f
101f1 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a  lags = MEM_Int;.
101f2 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20      pMem->u.i = 
101f3 70 4f 70 2d 3e 70 32 3b 20 20 20 20 20 20 20 20  pOp->p2;        
101f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
101f5 20 20 2f 2a 20 50 32 20 2a 2f 0a 20 20 20 20 70    /* P2 */.    p
101f6 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49  Mem->type = SQLI
101f7 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20  TE_INTEGER;.    
101f8 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20 70 4d 65  pMem++;..    pMe
101f9 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 45  m->flags = MEM_E
101fa 70 68 65 6d 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d  phem|MEM_Str|MEM
101fb 5f 54 65 72 6d 3b 20 20 20 2f 2a 20 50 33 20 2a  _Term;   /* P3 *
101fc 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20  /.    pMem->z = 
101fd 64 69 73 70 6c 61 79 50 33 28 70 4f 70 2c 20 70  displayP3(pOp, p
101fe 4d 65 6d 2d 3e 7a 53 68 6f 72 74 2c 20 73 69 7a  Mem->zShort, siz
101ff 65 6f 66 28 70 4d 65 6d 2d 3e 7a 53 68 6f 72 74  eof(pMem->zShort
10200 29 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ));.    assert( 
10201 70 4d 65 6d 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20  pMem->z!=0 );.  
10202 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 74 72 6c    pMem->n = strl
10203 65 6e 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20  en(pMem->z);.   
10204 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51   pMem->type = SQ
10205 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 70  LITE_TEXT;.    p
10206 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54  Mem->enc = SQLIT
10207 45 5f 55 54 46 38 3b 0a 0a 20 20 20 20 70 2d 3e  E_UTF8;..    p->
10208 6e 52 65 73 43 6f 6c 75 6d 6e 20 3d 20 35 20 2d  nResColumn = 5 -
10209 20 32 2a 28 70 2d 3e 65 78 70 6c 61 69 6e 2d 31   2*(p->explain-1
1020a 29 3b 0a 20 20 20 20 70 2d 3e 70 54 6f 73 20 3d  );.    p->pTos =
1020b 20 70 4d 65 6d 3b 0a 20 20 20 20 70 2d 3e 72 63   pMem;.    p->rc
1020c 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
1020d 20 20 70 2d 3e 72 65 73 4f 6e 53 74 61 63 6b 20    p->resOnStack 
1020e 3d 20 31 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  = 1;.    rc = SQ
1020f 4c 49 54 45 5f 52 4f 57 3b 0a 20 20 7d 0a 20 20  LITE_ROW;.  }.  
10210 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e  return rc;.}.#en
10211 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
10212 49 54 5f 45 58 50 4c 41 49 4e 20 2a 2f 0a 0a 23  IT_EXPLAIN */..#
10213 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
10214 55 47 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 74  UG./*.** Print t
10215 68 65 20 53 51 4c 20 74 68 61 74 20 77 61 73 20  he SQL that was 
10216 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65  used to generate
10217 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d 2e   a VDBE program.
10218 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
10219 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
1021a 64 62 65 50 72 69 6e 74 53 71 6c 28 56 64 62 65  dbePrintSql(Vdbe
1021b 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 4f 70 20   *p){.  int nOp 
1021c 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 56 64 62 65  = p->nOp;.  Vdbe
1021d 4f 70 20 2a 70 4f 70 3b 0a 20 20 69 66 28 20 6e  Op *pOp;.  if( n
1021e 4f 70 3c 31 20 29 20 72 65 74 75 72 6e 3b 0a 20  Op<1 ) return;. 
1021f 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 6e   pOp = &p->aOp[n
10220 4f 70 2d 31 5d 3b 0a 20 20 69 66 28 20 70 4f 70  Op-1];.  if( pOp
10221 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4e 6f 6f  ->opcode==OP_Noo
10222 70 20 26 26 20 70 4f 70 2d 3e 70 33 21 3d 30 20  p && pOp->p3!=0 
10223 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  ){.    const cha
10224 72 20 2a 7a 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a  r *z = pOp->p3;.
10225 20 20 20 20 77 68 69 6c 65 28 20 69 73 73 70 61      while( isspa
10226 63 65 28 2a 28 75 38 2a 29 7a 29 20 29 20 7a 2b  ce(*(u8*)z) ) z+
10227 2b 3b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 53  +;.    printf("S
10228 51 4c 3a 20 5b 25 73 5d 5c 6e 22 2c 20 7a 29 3b  QL: [%s]\n", z);
10229 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23  .  }.}.#endif..#
1022a 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
1022b 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 29 20 26  TE_OMIT_TRACE) &
1022c 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  & defined(SQLITE
1022d 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 29  _ENABLE_IOTRACE)
1022e 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 61 6e 20  ./*.** Print an 
1022f 49 4f 54 52 41 43 45 20 6d 65 73 73 61 67 65 20  IOTRACE message 
10230 73 68 6f 77 69 6e 67 20 53 51 4c 20 63 6f 6e 74  showing SQL cont
10231 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ent..*/.SQLITE_P
10232 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
10233 74 65 33 56 64 62 65 49 4f 54 72 61 63 65 53 71  te3VdbeIOTraceSq
10234 6c 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e  l(Vdbe *p){.  in
10235 74 20 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a  t nOp = p->nOp;.
10236 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20    VdbeOp *pOp;. 
10237 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6f 5f   if( sqlite3_io_
10238 74 72 61 63 65 3d 3d 30 20 29 20 72 65 74 75 72  trace==0 ) retur
10239 6e 3b 0a 20 20 69 66 28 20 6e 4f 70 3c 31 20 29  n;.  if( nOp<1 )
1023a 20 72 65 74 75 72 6e 3b 0a 20 20 70 4f 70 20 3d   return;.  pOp =
1023b 20 26 70 2d 3e 61 4f 70 5b 6e 4f 70 2d 31 5d 3b   &p->aOp[nOp-1];
1023c 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f  .  if( pOp->opco
1023d 64 65 3d 3d 4f 50 5f 4e 6f 6f 70 20 26 26 20 70  de==OP_Noop && p
1023e 4f 70 2d 3e 70 33 21 3d 30 20 29 7b 0a 20 20 20  Op->p3!=0 ){.   
1023f 20 63 68 61 72 20 2a 7a 20 3d 20 73 71 6c 69 74   char *z = sqlit
10240 65 33 53 74 72 44 75 70 28 70 4f 70 2d 3e 70 33  e3StrDup(pOp->p3
10241 29 3b 0a 20 20 20 20 69 6e 74 20 69 2c 20 6a 3b  );.    int i, j;
10242 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 73  .    for(i=0; is
10243 73 70 61 63 65 28 28 75 6e 73 69 67 6e 65 64 20  space((unsigned 
10244 63 68 61 72 29 7a 5b 69 5d 29 3b 20 69 2b 2b 29  char)z[i]); i++)
10245 7b 7d 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20  {}.    for(j=0; 
10246 7a 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  z[i]; i++){.    
10247 20 20 69 66 28 20 69 73 73 70 61 63 65 28 28 75    if( isspace((u
10248 6e 73 69 67 6e 65 64 20 63 68 61 72 29 7a 5b 69  nsigned char)z[i
10249 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  ]) ){.        if
1024a 28 20 7a 5b 69 2d 31 5d 21 3d 27 20 27 20 29 7b  ( z[i-1]!=' ' ){
1024b 0a 20 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b  .          z[j++
1024c 5d 20 3d 20 27 20 27 3b 0a 20 20 20 20 20 20 20  ] = ' ';.       
1024d 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a   }.      }else{.
1024e 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d          z[j++] =
1024f 20 7a 5b 69 5d 3b 0a 20 20 20 20 20 20 7d 0a 20   z[i];.      }. 
10250 20 20 20 7d 0a 20 20 20 20 7a 5b 6a 5d 20 3d 20     }.    z[j] = 
10251 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 69  0;.    sqlite3_i
10252 6f 5f 74 72 61 63 65 28 22 53 51 4c 20 25 73 5c  o_trace("SQL %s\
10253 6e 22 2c 20 7a 29 3b 0a 20 20 20 20 73 71 6c 69  n", z);.    sqli
10254 74 65 46 72 65 65 28 7a 29 3b 0a 20 20 7d 0a 7d  teFree(z);.  }.}
10255 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49  .#endif /* !SQLI
10256 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 20 26 26  TE_OMIT_TRACE &&
10257 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49   SQLITE_ENABLE_I
10258 4f 54 52 41 43 45 20 2a 2f 0a 0a 0a 2f 2a 0a 2a  OTRACE */.../*.*
10259 2a 20 50 72 65 70 61 72 65 20 61 20 76 69 72 74  * Prepare a virt
1025a 75 61 6c 20 6d 61 63 68 69 6e 65 20 66 6f 72 20  ual machine for 
1025b 65 78 65 63 75 74 69 6f 6e 2e 20 20 54 68 69 73  execution.  This
1025c 20 69 6e 76 6f 6c 76 65 73 20 74 68 69 6e 67 73   involves things
1025d 20 73 75 63 68 0a 2a 2a 20 61 73 20 61 6c 6c 6f   such.** as allo
1025e 63 61 74 69 6e 67 20 73 74 61 63 6b 20 73 70 61  cating stack spa
1025f 63 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a  ce and initializ
10260 69 6e 67 20 74 68 65 20 70 72 6f 67 72 61 6d 20  ing the program 
10261 63 6f 75 6e 74 65 72 2e 0a 2a 2a 20 41 66 74 65  counter..** Afte
10262 72 20 74 68 65 20 56 44 42 45 20 68 61 73 20 62  r the VDBE has b
10263 65 20 70 72 65 70 70 65 64 2c 20 69 74 20 63 61  e prepped, it ca
10264 6e 20 62 65 20 65 78 65 63 75 74 65 64 20 62 79  n be executed by
10265 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20   one or more.** 
10266 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33  calls to sqlite3
10267 56 64 62 65 45 78 65 63 28 29 2e 20 20 0a 2a 2a  VdbeExec().  .**
10268 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20  .** This is the 
10269 6f 6e 6c 79 20 77 61 79 20 74 6f 20 6d 6f 76 65  only way to move
1026a 20 61 20 56 44 42 45 20 66 72 6f 6d 20 56 44 42   a VDBE from VDB
1026b 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 74 6f 0a  E_MAGIC_INIT to.
1026c 2a 2a 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55  ** VDBE_MAGIC_RU
1026d 4e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  N..*/.SQLITE_PRI
1026e 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1026f 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28 0a  3VdbeMakeReady(.
10270 20 20 56 64 62 65 20 2a 70 2c 20 20 20 20 20 20    Vdbe *p,      
10271 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10272 20 2f 2a 20 54 68 65 20 56 44 42 45 20 2a 2f 0a   /* The VDBE */.
10273 20 20 69 6e 74 20 6e 56 61 72 2c 20 20 20 20 20    int nVar,     
10274 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10275 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 27 3f   /* Number of '?
10276 27 20 73 65 65 20 69 6e 20 74 68 65 20 53 51 4c  ' see in the SQL
10277 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
10278 69 6e 74 20 6e 4d 65 6d 2c 20 20 20 20 20 20 20  int nMem,       
10279 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1027a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f  * Number of memo
1027b 72 79 20 63 65 6c 6c 73 20 74 6f 20 61 6c 6c 6f  ry cells to allo
1027c 63 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  cate */.  int nC
1027d 75 72 73 6f 72 2c 20 20 20 20 20 20 20 20 20 20  ursor,          
1027e 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
1027f 65 72 20 6f 66 20 63 75 72 73 6f 72 73 20 74 6f  er of cursors to
10280 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 20 69   allocate */.  i
10281 6e 74 20 69 73 45 78 70 6c 61 69 6e 20 20 20 20  nt isExplain    
10282 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10283 20 54 72 75 65 20 69 66 20 74 68 65 20 45 58 50   True if the EXP
10284 4c 41 49 4e 20 6b 65 79 77 6f 72 64 73 20 69 73  LAIN keywords is
10285 20 70 72 65 73 65 6e 74 20 2a 2f 0a 29 7b 0a 20   present */.){. 
10286 20 69 6e 74 20 6e 3b 0a 0a 20 20 61 73 73 65 72   int n;..  asser
10287 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73  t( p!=0 );.  ass
10288 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56  ert( p->magic==V
10289 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29  DBE_MAGIC_INIT )
1028a 3b 0a 0a 20 20 2f 2a 20 54 68 65 72 65 20 73 68  ;..  /* There sh
1028b 6f 75 6c 64 20 62 65 20 61 74 20 6c 65 61 73 74  ould be at least
1028c 20 6f 6e 65 20 6f 70 63 6f 64 65 2e 0a 20 20 2a   one opcode..  *
1028d 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e  /.  assert( p->n
1028e 4f 70 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 53 65  Op>0 );..  /* Se
1028f 74 20 74 68 65 20 6d 61 67 69 63 20 74 6f 20 56  t the magic to V
10290 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 73 6f  DBE_MAGIC_RUN so
10291 6f 6e 65 72 20 72 61 74 68 65 72 20 74 68 61 6e  oner rather than
10292 20 6c 61 74 65 72 2e 20 54 68 69 73 0a 20 20 20   later. This.   
10293 2a 20 69 73 20 62 65 63 61 75 73 65 20 74 68 65  * is because the
10294 20 63 61 6c 6c 20 74 6f 20 72 65 73 69 7a 65 4f   call to resizeO
10295 70 41 72 72 61 79 28 29 20 62 65 6c 6f 77 20 6d  pArray() below m
10296 61 79 20 73 68 72 69 6e 6b 20 74 68 65 0a 20 20  ay shrink the.  
10297 20 2a 20 70 2d 3e 61 4f 70 5b 5d 20 61 72 72 61   * p->aOp[] arra
10298 79 20 74 6f 20 73 61 76 65 20 6d 65 6d 6f 72 79  y to save memory
10299 20 69 66 20 63 61 6c 6c 65 64 20 77 68 65 6e 20   if called when 
1029a 69 6e 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55  in VDBE_MAGIC_RU
1029b 4e 20 0a 20 20 20 2a 20 73 74 61 74 65 2e 0a 20  N .   * state.. 
1029c 20 20 2a 2f 0a 20 20 70 2d 3e 6d 61 67 69 63 20    */.  p->magic 
1029d 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e  = VDBE_MAGIC_RUN
1029e 3b 0a 0a 20 20 2f 2a 20 4e 6f 20 69 6e 73 74 72  ;..  /* No instr
1029f 75 63 74 69 6f 6e 20 65 76 65 72 20 70 75 73 68  uction ever push
102a0 65 73 20 6d 6f 72 65 20 74 68 61 6e 20 61 20 73  es more than a s
102a1 69 6e 67 6c 65 20 65 6c 65 6d 65 6e 74 20 6f 6e  ingle element on
102a2 74 6f 20 74 68 65 0a 20 20 2a 2a 20 73 74 61 63  to the.  ** stac
102a3 6b 2e 20 20 41 6e 64 20 74 68 65 20 73 74 61 63  k.  And the stac
102a4 6b 20 6e 65 76 65 72 20 67 72 6f 77 73 20 6f 6e  k never grows on
102a5 20 73 75 63 63 65 73 73 69 76 65 20 65 78 65 63   successive exec
102a6 75 74 69 6f 6e 73 20 6f 66 20 74 68 65 0a 20 20  utions of the.  
102a7 2a 2a 20 73 61 6d 65 20 6c 6f 6f 70 2e 20 20 53  ** same loop.  S
102a8 6f 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62  o the total numb
102a9 65 72 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f  er of instructio
102aa 6e 73 20 69 73 20 61 6e 20 75 70 70 65 72 20 62  ns is an upper b
102ab 6f 75 6e 64 0a 20 20 2a 2a 20 6f 6e 20 74 68 65  ound.  ** on the
102ac 20 6d 61 78 69 6d 75 6d 20 73 74 61 63 6b 20 64   maximum stack d
102ad 65 70 74 68 20 72 65 71 75 69 72 65 64 2e 20 20  epth required.  
102ae 28 41 64 64 65 64 20 6c 61 74 65 72 3a 29 20 20  (Added later:)  
102af 54 68 65 0a 20 20 2a 2a 20 72 65 73 6f 6c 76 65  The.  ** resolve
102b0 50 32 56 61 6c 75 65 73 28 29 20 63 61 6c 6c 20  P2Values() call 
102b1 63 6f 6d 70 75 74 65 73 20 61 20 74 69 67 68 74  computes a tight
102b2 65 72 20 75 70 70 65 72 20 62 6f 75 6e 64 20 6f  er upper bound o
102b3 6e 20 74 68 65 0a 20 20 2a 2a 20 73 74 61 63 6b  n the.  ** stack
102b4 20 73 69 7a 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a   size..  **.  **
102b5 20 41 6c 6c 6f 63 61 74 69 6f 6e 20 61 6c 6c 20   Allocation all 
102b6 74 68 65 20 73 74 61 63 6b 20 73 70 61 63 65 20  the stack space 
102b7 77 65 20 77 69 6c 6c 20 65 76 65 72 20 6e 65 65  we will ever nee
102b8 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d  d..  */.  if( p-
102b9 3e 61 53 74 61 63 6b 3d 3d 30 20 29 7b 0a 20 20  >aStack==0 ){.  
102ba 20 20 69 6e 74 20 6e 41 72 67 3b 20 20 20 20 20    int nArg;     
102bb 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d    /* Maximum num
102bc 62 65 72 20 6f 66 20 61 72 67 73 20 70 61 73 73  ber of args pass
102bd 65 64 20 74 6f 20 61 20 75 73 65 72 20 66 75 6e  ed to a user fun
102be 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20 69 6e  ction. */.    in
102bf 74 20 6e 53 74 61 63 6b 3b 20 20 20 20 20 2f 2a  t nStack;     /*
102c0 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   Maximum number 
102c1 6f 66 20 73 74 61 63 6b 20 65 6e 74 72 69 65 73  of stack entries
102c2 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 20   required */.   
102c3 20 72 65 73 6f 6c 76 65 50 32 56 61 6c 75 65 73   resolveP2Values
102c4 28 70 2c 20 26 6e 41 72 67 2c 20 26 6e 53 74 61  (p, &nArg, &nSta
102c5 63 6b 29 3b 0a 20 20 20 20 72 65 73 69 7a 65 4f  ck);.    resizeO
102c6 70 41 72 72 61 79 28 70 2c 20 70 2d 3e 6e 4f 70  pArray(p, p->nOp
102c7 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e  );.    assert( n
102c8 56 61 72 3e 3d 30 20 29 3b 0a 20 20 20 20 61 73  Var>=0 );.    as
102c9 73 65 72 74 28 20 6e 53 74 61 63 6b 3c 70 2d 3e  sert( nStack<p->
102ca 6e 4f 70 20 29 3b 0a 20 20 20 20 69 66 28 20 69  nOp );.    if( i
102cb 73 45 78 70 6c 61 69 6e 20 29 7b 0a 20 20 20 20  sExplain ){.    
102cc 20 20 6e 53 74 61 63 6b 20 3d 20 31 30 3b 0a 20    nStack = 10;. 
102cd 20 20 20 7d 0a 20 20 20 20 70 2d 3e 61 53 74 61     }.    p->aSta
102ce 63 6b 20 3d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f  ck = sqliteMallo
102cf 63 28 0a 20 20 20 20 20 20 20 20 6e 53 74 61 63  c(.        nStac
102d0 6b 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 53 74 61  k*sizeof(p->aSta
102d1 63 6b 5b 30 5d 29 20 20 20 20 2f 2a 20 61 53 74  ck[0])    /* aSt
102d2 61 63 6b 20 2a 2f 0a 20 20 20 20 20 20 2b 20 6e  ack */.      + n
102d3 41 72 67 2a 73 69 7a 65 6f 66 28 4d 65 6d 2a 29  Arg*sizeof(Mem*)
102d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
102d5 20 61 70 41 72 67 20 2a 2f 0a 20 20 20 20 20 20   apArg */.      
102d6 2b 20 6e 56 61 72 2a 73 69 7a 65 6f 66 28 4d 65  + nVar*sizeof(Me
102d7 6d 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m)              
102d8 20 2f 2a 20 61 56 61 72 20 2a 2f 0a 20 20 20 20   /* aVar */.    
102d9 20 20 2b 20 6e 56 61 72 2a 73 69 7a 65 6f 66 28    + nVar*sizeof(
102da 63 68 61 72 2a 29 20 20 20 20 20 20 20 20 20 20  char*)          
102db 20 20 20 2f 2a 20 61 7a 56 61 72 20 2a 2f 0a 20     /* azVar */. 
102dc 20 20 20 20 20 2b 20 6e 4d 65 6d 2a 73 69 7a 65       + nMem*size
102dd 6f 66 28 4d 65 6d 29 20 20 20 20 20 20 20 20 20  of(Mem)         
102de 20 20 20 20 20 20 2f 2a 20 61 4d 65 6d 20 2a 2f        /* aMem */
102df 0a 20 20 20 20 20 20 2b 20 6e 43 75 72 73 6f 72  .      + nCursor
102e0 2a 73 69 7a 65 6f 66 28 43 75 72 73 6f 72 2a 29  *sizeof(Cursor*)
102e1 20 20 20 20 20 20 20 20 2f 2a 20 61 70 43 73 72          /* apCsr
102e2 20 2a 2f 0a 20 20 20 20 29 3b 0a 20 20 20 20 69   */.    );.    i
102e3 66 28 20 21 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  f( !sqlite3Mallo
102e4 63 46 61 69 6c 65 64 28 29 20 29 7b 0a 20 20 20  cFailed() ){.   
102e5 20 20 20 70 2d 3e 61 4d 65 6d 20 3d 20 26 70 2d     p->aMem = &p-
102e6 3e 61 53 74 61 63 6b 5b 6e 53 74 61 63 6b 5d 3b  >aStack[nStack];
102e7 0a 20 20 20 20 20 20 70 2d 3e 6e 4d 65 6d 20 3d  .      p->nMem =
102e8 20 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 70 2d 3e   nMem;.      p->
102e9 61 56 61 72 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  aVar = &p->aMem[
102ea 6e 4d 65 6d 5d 3b 0a 20 20 20 20 20 20 70 2d 3e  nMem];.      p->
102eb 6e 56 61 72 20 3d 20 6e 56 61 72 3b 0a 20 20 20  nVar = nVar;.   
102ec 20 20 20 70 2d 3e 6f 6b 56 61 72 20 3d 20 30 3b     p->okVar = 0;
102ed 0a 20 20 20 20 20 20 70 2d 3e 61 70 41 72 67 20  .      p->apArg 
102ee 3d 20 28 4d 65 6d 2a 2a 29 26 70 2d 3e 61 56 61  = (Mem**)&p->aVa
102ef 72 5b 6e 56 61 72 5d 3b 0a 20 20 20 20 20 20 70  r[nVar];.      p
102f0 2d 3e 61 7a 56 61 72 20 3d 20 28 63 68 61 72 2a  ->azVar = (char*
102f1 2a 29 26 70 2d 3e 61 70 41 72 67 5b 6e 41 72 67  *)&p->apArg[nArg
102f2 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 61 70 43 73  ];.      p->apCs
102f3 72 20 3d 20 28 43 75 72 73 6f 72 2a 2a 29 26 70  r = (Cursor**)&p
102f4 2d 3e 61 7a 56 61 72 5b 6e 56 61 72 5d 3b 0a 20  ->azVar[nVar];. 
102f5 20 20 20 20 20 70 2d 3e 6e 43 75 72 73 6f 72 20       p->nCursor 
102f6 3d 20 6e 43 75 72 73 6f 72 3b 0a 20 20 20 20 20  = nCursor;.     
102f7 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c 6e 56 61 72   for(n=0; n<nVar
102f8 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; n++){.        
102f9 70 2d 3e 61 56 61 72 5b 6e 5d 2e 66 6c 61 67 73  p->aVar[n].flags
102fa 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20   = MEM_Null;.   
102fb 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
102fc 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c 70 2d 3e 6e   for(n=0; n<p->n
102fd 4d 65 6d 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 70  Mem; n++){.    p
102fe 2d 3e 61 4d 65 6d 5b 6e 5d 2e 66 6c 61 67 73 20  ->aMem[n].flags 
102ff 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 0a  = MEM_Null;.  }.
10300 0a 20 20 70 2d 3e 70 54 6f 73 20 3d 20 26 70 2d  .  p->pTos = &p-
10301 3e 61 53 74 61 63 6b 5b 2d 31 5d 3b 0a 20 20 70  >aStack[-1];.  p
10302 2d 3e 70 63 20 3d 20 2d 31 3b 0a 20 20 70 2d 3e  ->pc = -1;.  p->
10303 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
10304 20 20 70 2d 3e 75 6e 69 71 75 65 43 6e 74 20 3d    p->uniqueCnt =
10305 20 30 3b 0a 20 20 70 2d 3e 72 65 74 75 72 6e 44   0;.  p->returnD
10306 65 70 74 68 20 3d 20 30 3b 0a 20 20 70 2d 3e 65  epth = 0;.  p->e
10307 72 72 6f 72 41 63 74 69 6f 6e 20 3d 20 4f 45 5f  rrorAction = OE_
10308 41 62 6f 72 74 3b 0a 20 20 70 2d 3e 70 6f 70 53  Abort;.  p->popS
10309 74 61 63 6b 20 3d 20 20 30 3b 0a 20 20 70 2d 3e  tack =  0;.  p->
1030a 65 78 70 6c 61 69 6e 20 7c 3d 20 69 73 45 78 70  explain |= isExp
1030b 6c 61 69 6e 3b 0a 20 20 70 2d 3e 6d 61 67 69 63  lain;.  p->magic
1030c 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55   = VDBE_MAGIC_RU
1030d 4e 3b 0a 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20  N;.  p->nChange 
1030e 3d 20 30 3b 0a 20 20 70 2d 3e 63 61 63 68 65 43  = 0;.  p->cacheC
1030f 74 72 20 3d 20 31 3b 0a 20 20 70 2d 3e 6d 69 6e  tr = 1;.  p->min
10310 57 72 69 74 65 46 69 6c 65 46 6f 72 6d 61 74 20  WriteFileFormat 
10311 3d 20 32 35 35 3b 0a 20 20 70 2d 3e 6f 70 65 6e  = 255;.  p->open
10312 65 64 53 74 61 74 65 6d 65 6e 74 20 3d 20 30 3b  edStatement = 0;
10313 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f  .#ifdef VDBE_PRO
10314 46 49 4c 45 0a 20 20 7b 0a 20 20 20 20 69 6e 74  FILE.  {.    int
10315 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   i;.    for(i=0;
10316 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b   i<p->nOp; i++){
10317 0a 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d  .      p->aOp[i]
10318 2e 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  .cnt = 0;.      
10319 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73  p->aOp[i].cycles
1031a 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 0;.    }.  }.
1031b 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  #endif.}../*.** 
1031c 43 6c 6f 73 65 20 61 20 63 75 72 73 6f 72 20 61  Close a cursor a
1031d 6e 64 20 72 65 6c 65 61 73 65 20 61 6c 6c 20 74  nd release all t
1031e 68 65 20 72 65 73 6f 75 72 63 65 73 20 74 68 61  he resources tha
1031f 74 20 63 75 72 73 6f 72 20 68 61 70 70 65 6e 73  t cursor happens
10320 0a 2a 2a 20 74 6f 20 68 6f 6c 64 2e 0a 2a 2f 0a  .** to hold..*/.
10321 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
10322 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 46  oid sqlite3VdbeF
10323 72 65 65 43 75 72 73 6f 72 28 56 64 62 65 20 2a  reeCursor(Vdbe *
10324 70 2c 20 43 75 72 73 6f 72 20 2a 70 43 78 29 7b  p, Cursor *pCx){
10325 0a 20 20 69 66 28 20 70 43 78 3d 3d 30 20 29 7b  .  if( pCx==0 ){
10326 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
10327 0a 20 20 69 66 28 20 70 43 78 2d 3e 70 43 75 72  .  if( pCx->pCur
10328 73 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  sor ){.    sqlit
10329 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72 73  e3BtreeCloseCurs
1032a 6f 72 28 70 43 78 2d 3e 70 43 75 72 73 6f 72 29  or(pCx->pCursor)
1032b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 78 2d  ;.  }.  if( pCx-
1032c 3e 70 42 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  >pBt ){.    sqli
1032d 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 70 43  te3BtreeClose(pC
1032e 78 2d 3e 70 42 74 29 3b 0a 20 20 7d 0a 23 69 66  x->pBt);.  }.#if
1032f 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
10330 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
10331 69 66 28 20 70 43 78 2d 3e 70 56 74 61 62 43 75  if( pCx->pVtabCu
10332 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c 69  rsor ){.    sqli
10333 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
10334 2a 70 56 74 61 62 43 75 72 73 6f 72 20 3d 20 70  *pVtabCursor = p
10335 43 78 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 3b  Cx->pVtabCursor;
10336 0a 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  .    const sqlit
10337 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75  e3_module *pModu
10338 6c 65 20 3d 20 70 43 78 2d 3e 70 4d 6f 64 75 6c  le = pCx->pModul
10339 65 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 62  e;.    p->inVtab
1033a 4d 65 74 68 6f 64 20 3d 20 31 3b 0a 20 20 20 20  Method = 1;.    
1033b 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
1033c 28 70 2d 3e 64 62 29 3b 0a 20 20 20 20 70 4d 6f  (p->db);.    pMo
1033d 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 56 74  dule->xClose(pVt
1033e 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 73  abCursor);.    s
1033f 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 70  qlite3SafetyOn(p
10340 2d 3e 64 62 29 3b 0a 20 20 20 20 70 2d 3e 69 6e  ->db);.    p->in
10341 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a  VtabMethod = 0;.
10342 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c    }.#endif.  sql
10343 69 74 65 46 72 65 65 28 70 43 78 2d 3e 70 44 61  iteFree(pCx->pDa
10344 74 61 29 3b 0a 20 20 73 71 6c 69 74 65 46 72 65  ta);.  sqliteFre
10345 65 28 70 43 78 2d 3e 61 54 79 70 65 29 3b 0a 20  e(pCx->aType);. 
10346 20 73 71 6c 69 74 65 46 72 65 65 28 70 43 78 29   sqliteFree(pCx)
10347 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65  ;.}../*.** Close
10348 20 61 6c 6c 20 63 75 72 73 6f 72 73 0a 2a 2f 0a   all cursors.*/.
10349 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c 6f 73  static void clos
1034a 65 41 6c 6c 43 75 72 73 6f 72 73 28 56 64 62 65  eAllCursors(Vdbe
1034b 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20   *p){.  int i;. 
1034c 20 69 66 28 20 70 2d 3e 61 70 43 73 72 3d 3d 30   if( p->apCsr==0
1034d 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72   ) return;.  for
1034e 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 75 72 73  (i=0; i<p->nCurs
1034f 6f 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  or; i++){.    if
10350 28 20 21 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68  ( !p->inVtabMeth
10351 6f 64 20 7c 7c 20 28 70 2d 3e 61 70 43 73 72 5b  od || (p->apCsr[
10352 69 5d 20 26 26 20 21 70 2d 3e 61 70 43 73 72 5b  i] && !p->apCsr[
10353 69 5d 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 29  i]->pVtabCursor)
10354 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
10355 33 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 28  3VdbeFreeCursor(
10356 70 2c 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 3b  p, p->apCsr[i]);
10357 0a 20 20 20 20 20 20 70 2d 3e 61 70 43 73 72 5b  .      p->apCsr[
10358 69 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  i] = 0;.    }.  
10359 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 6e  }.}../*.** Clean
1035a 20 75 70 20 74 68 65 20 56 4d 20 61 66 74 65 72   up the VM after
1035b 20 65 78 65 63 75 74 69 6f 6e 2e 0a 2a 2a 0a 2a   execution..**.*
1035c 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77  * This routine w
1035d 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  ill automaticall
1035e 79 20 63 6c 6f 73 65 20 61 6e 79 20 63 75 72 73  y close any curs
1035f 6f 72 73 2c 20 6c 69 73 74 73 2c 20 61 6e 64 2f  ors, lists, and/
10360 6f 72 0a 2a 2a 20 73 6f 72 74 65 72 73 20 74 68  or.** sorters th
10361 61 74 20 77 65 72 65 20 6c 65 66 74 20 6f 70 65  at were left ope
10362 6e 2e 20 20 49 74 20 61 6c 73 6f 20 64 65 6c 65  n.  It also dele
10363 74 65 73 20 74 68 65 20 76 61 6c 75 65 73 20 6f  tes the values o
10364 66 0a 2a 2a 20 76 61 72 69 61 62 6c 65 73 20 69  f.** variables i
10365 6e 20 74 68 65 20 61 56 61 72 5b 5d 20 61 72 72  n the aVar[] arr
10366 61 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ay..*/.static vo
10367 69 64 20 43 6c 65 61 6e 75 70 28 56 64 62 65 20  id Cleanup(Vdbe 
10368 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  *p){.  int i;.  
10369 69 66 28 20 70 2d 3e 61 53 74 61 63 6b 20 29 7b  if( p->aStack ){
1036a 0a 20 20 20 20 72 65 6c 65 61 73 65 4d 65 6d 41  .    releaseMemA
1036b 72 72 61 79 28 70 2d 3e 61 53 74 61 63 6b 2c 20  rray(p->aStack, 
1036c 31 20 2b 20 28 70 2d 3e 70 54 6f 73 20 2d 20 70  1 + (p->pTos - p
1036d 2d 3e 61 53 74 61 63 6b 29 29 3b 0a 20 20 20 20  ->aStack));.    
1036e 70 2d 3e 70 54 6f 73 20 3d 20 26 70 2d 3e 61 53  p->pTos = &p->aS
1036f 74 61 63 6b 5b 2d 31 5d 3b 0a 20 20 7d 0a 20 20  tack[-1];.  }.  
10370 63 6c 6f 73 65 41 6c 6c 43 75 72 73 6f 72 73 28  closeAllCursors(
10371 70 29 3b 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d  p);.  releaseMem
10372 41 72 72 61 79 28 70 2d 3e 61 4d 65 6d 2c 20 70  Array(p->aMem, p
10373 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 73 71 6c 69 74  ->nMem);.  sqlit
10374 65 33 56 64 62 65 46 69 66 6f 43 6c 65 61 72 28  e3VdbeFifoClear(
10375 26 70 2d 3e 73 46 69 66 6f 29 3b 0a 20 20 69 66  &p->sFifo);.  if
10376 28 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63  ( p->contextStac
10377 6b 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30  k ){.    for(i=0
10378 3b 20 69 3c 70 2d 3e 63 6f 6e 74 65 78 74 53 74  ; i<p->contextSt
10379 61 63 6b 54 6f 70 3b 20 69 2b 2b 29 7b 0a 20 20  ackTop; i++){.  
1037a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 46      sqlite3VdbeF
1037b 69 66 6f 43 6c 65 61 72 28 26 70 2d 3e 63 6f 6e  ifoClear(&p->con
1037c 74 65 78 74 53 74 61 63 6b 5b 69 5d 2e 73 46 69  textStack[i].sFi
1037d 66 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  fo);.    }.    s
1037e 71 6c 69 74 65 46 72 65 65 28 70 2d 3e 63 6f 6e  qliteFree(p->con
1037f 74 65 78 74 53 74 61 63 6b 29 3b 0a 20 20 7d 0a  textStack);.  }.
10380 20 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63    p->contextStac
10381 6b 20 3d 20 30 3b 0a 20 20 70 2d 3e 63 6f 6e 74  k = 0;.  p->cont
10382 65 78 74 53 74 61 63 6b 44 65 70 74 68 20 3d 20  extStackDepth = 
10383 30 3b 0a 20 20 70 2d 3e 63 6f 6e 74 65 78 74 53  0;.  p->contextS
10384 74 61 63 6b 54 6f 70 20 3d 20 30 3b 0a 20 20 73  tackTop = 0;.  s
10385 71 6c 69 74 65 46 72 65 65 28 70 2d 3e 7a 45 72  qliteFree(p->zEr
10386 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72  rMsg);.  p->zErr
10387 4d 73 67 20 3d 20 30 3b 0a 20 20 70 2d 3e 72 65  Msg = 0;.  p->re
10388 73 4f 6e 53 74 61 63 6b 20 3d 20 30 3b 0a 7d 0a  sOnStack = 0;.}.
10389 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 6e  ./*.** Set the n
1038a 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20  umber of result 
1038b 63 6f 6c 75 6d 6e 73 20 74 68 61 74 20 77 69 6c  columns that wil
1038c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 79  l be returned by
1038d 20 74 68 69 73 20 53 51 4c 0a 2a 2a 20 73 74 61   this SQL.** sta
1038e 74 65 6d 65 6e 74 2e 20 54 68 69 73 20 69 73 20  tement. This is 
1038f 6e 6f 77 20 73 65 74 20 61 74 20 63 6f 6d 70 69  now set at compi
10390 6c 65 20 74 69 6d 65 2c 20 72 61 74 68 65 72 20  le time, rather 
10391 74 68 61 6e 20 64 75 72 69 6e 67 0a 2a 2a 20 65  than during.** e
10392 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 65 20  xecution of the 
10393 76 64 62 65 20 70 72 6f 67 72 61 6d 20 73 6f 20  vdbe program so 
10394 74 68 61 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c  that sqlite3_col
10395 75 6d 6e 5f 63 6f 75 6e 74 28 29 20 63 61 6e 0a  umn_count() can.
10396 2a 2a 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 20  ** be called on 
10397 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  an SQL statement
10398 20 62 65 66 6f 72 65 20 73 71 6c 69 74 65 33 5f   before sqlite3_
10399 73 74 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54  step()..*/.SQLIT
1039a 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1039b 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
1039c 43 6f 6c 73 28 56 64 62 65 20 2a 70 2c 20 69 6e  Cols(Vdbe *p, in
1039d 74 20 6e 52 65 73 43 6f 6c 75 6d 6e 29 7b 0a 20  t nResColumn){. 
1039e 20 4d 65 6d 20 2a 70 43 6f 6c 4e 61 6d 65 3b 0a   Mem *pColName;.
1039f 20 20 69 6e 74 20 6e 3b 0a 20 20 72 65 6c 65 61    int n;.  relea
103a0 73 65 4d 65 6d 41 72 72 61 79 28 70 2d 3e 61 43  seMemArray(p->aC
103a1 6f 6c 4e 61 6d 65 2c 20 70 2d 3e 6e 52 65 73 43  olName, p->nResC
103a2 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f 4e 29  olumn*COLNAME_N)
103a3 3b 0a 20 20 73 71 6c 69 74 65 46 72 65 65 28 70  ;.  sqliteFree(p
103a4 2d 3e 61 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 6e  ->aColName);.  n
103a5 20 3d 20 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f   = nResColumn*CO
103a6 4c 4e 41 4d 45 5f 4e 3b 0a 20 20 70 2d 3e 6e 52  LNAME_N;.  p->nR
103a7 65 73 43 6f 6c 75 6d 6e 20 3d 20 6e 52 65 73 43  esColumn = nResC
103a8 6f 6c 75 6d 6e 3b 0a 20 20 70 2d 3e 61 43 6f 6c  olumn;.  p->aCol
103a9 4e 61 6d 65 20 3d 20 70 43 6f 6c 4e 61 6d 65 20  Name = pColName 
103aa 3d 20 28 4d 65 6d 2a 29 73 71 6c 69 74 65 4d 61  = (Mem*)sqliteMa
103ab 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 4d 65 6d  lloc( sizeof(Mem
103ac 29 2a 6e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e  )*n );.  if( p->
103ad 61 43 6f 6c 4e 61 6d 65 3d 3d 30 20 29 20 72 65  aColName==0 ) re
103ae 74 75 72 6e 3b 0a 20 20 77 68 69 6c 65 28 20 6e  turn;.  while( n
103af 2d 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 28 70  -- > 0 ){.    (p
103b0 43 6f 6c 4e 61 6d 65 2b 2b 29 2d 3e 66 6c 61 67  ColName++)->flag
103b1 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20  s = MEM_Null;.  
103b2 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74  }.}../*.** Set t
103b3 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 69  he name of the i
103b4 64 78 27 74 68 20 63 6f 6c 75 6d 6e 20 74 6f 20  dx'th column to 
103b5 62 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74  be returned by t
103b6 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
103b7 2e 0a 2a 2a 20 7a 4e 61 6d 65 20 6d 75 73 74 20  ..** zName must 
103b8 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  be a pointer to 
103b9 61 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64  a nul terminated
103ba 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54   string..**.** T
103bb 68 69 73 20 63 61 6c 6c 20 6d 75 73 74 20 62 65  his call must be
103bc 20 6d 61 64 65 20 61 66 74 65 72 20 61 20 63 61   made after a ca
103bd 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62  ll to sqlite3Vdb
103be 65 53 65 74 4e 75 6d 43 6f 6c 73 28 29 2e 0a 2a  eSetNumCols()..*
103bf 2a 0a 2a 2a 20 49 66 20 4e 3d 3d 50 33 5f 53 54  *.** If N==P3_ST
103c0 41 54 49 43 20 20 69 74 20 6d 65 61 6e 73 20 74  ATIC  it means t
103c1 68 61 74 20 7a 4e 61 6d 65 20 69 73 20 61 20 70  hat zName is a p
103c2 6f 69 6e 74 65 72 20 74 6f 20 61 20 63 6f 6e 73  ointer to a cons
103c3 74 61 6e 74 20 73 74 61 74 69 63 0a 2a 2a 20 73  tant static.** s
103c4 74 72 69 6e 67 20 61 6e 64 20 77 65 20 63 61 6e  tring and we can
103c5 20 6a 75 73 74 20 63 6f 70 79 20 74 68 65 20 70   just copy the p
103c6 6f 69 6e 74 65 72 2e 20 49 66 20 69 74 20 69 73  ointer. If it is
103c7 20 50 33 5f 44 59 4e 41 4d 49 43 2c 20 74 68 65   P3_DYNAMIC, the
103c8 6e 20 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67  n .** the string
103c9 20 69 73 20 66 72 65 65 64 20 75 73 69 6e 67 20   is freed using 
103ca 73 71 6c 69 74 65 46 72 65 65 28 29 20 77 68 65  sqliteFree() whe
103cb 6e 20 74 68 65 20 76 64 62 65 20 69 73 20 66 69  n the vdbe is fi
103cc 6e 69 73 68 65 64 20 77 69 74 68 0a 2a 2a 20 69  nished with.** i
103cd 74 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 4e 20  t. Otherwise, N 
103ce 62 79 74 65 73 20 6f 66 20 7a 4e 61 6d 65 20 61  bytes of zName a
103cf 72 65 20 63 6f 70 69 65 64 2e 0a 2a 2f 0a 53 51  re copied..*/.SQ
103d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
103d1 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
103d2 6f 6c 4e 61 6d 65 28 56 64 62 65 20 2a 70 2c 20  olName(Vdbe *p, 
103d3 69 6e 74 20 69 64 78 2c 20 69 6e 74 20 76 61 72  int idx, int var
103d4 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  , const char *zN
103d5 61 6d 65 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 69  ame, int N){.  i
103d6 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 20 2a 70 43  nt rc;.  Mem *pC
103d7 6f 6c 4e 61 6d 65 3b 0a 20 20 61 73 73 65 72 74  olName;.  assert
103d8 28 20 69 64 78 3c 70 2d 3e 6e 52 65 73 43 6f 6c  ( idx<p->nResCol
103d9 75 6d 6e 20 29 3b 0a 20 20 61 73 73 65 72 74 28  umn );.  assert(
103da 20 76 61 72 3c 43 4f 4c 4e 41 4d 45 5f 4e 20 29   var<COLNAME_N )
103db 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 4d  ;.  if( sqlite3M
103dc 61 6c 6c 6f 63 46 61 69 6c 65 64 28 29 20 29 20  allocFailed() ) 
103dd 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
103de 4d 45 4d 3b 0a 20 20 61 73 73 65 72 74 28 20 70  MEM;.  assert( p
103df 2d 3e 61 43 6f 6c 4e 61 6d 65 21 3d 30 20 29 3b  ->aColName!=0 );
103e0 0a 20 20 70 43 6f 6c 4e 61 6d 65 20 3d 20 26 28  .  pColName = &(
103e1 70 2d 3e 61 43 6f 6c 4e 61 6d 65 5b 69 64 78 2b  p->aColName[idx+
103e2 76 61 72 2a 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d  var*p->nResColum
103e3 6e 5d 29 3b 0a 20 20 69 66 28 20 4e 3d 3d 50 33  n]);.  if( N==P3
103e4 5f 44 59 4e 41 4d 49 43 20 7c 7c 20 4e 3d 3d 50  _DYNAMIC || N==P
103e5 33 5f 53 54 41 54 49 43 20 29 7b 0a 20 20 20 20  3_STATIC ){.    
103e6 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
103e7 4d 65 6d 53 65 74 53 74 72 28 70 43 6f 6c 4e 61  MemSetStr(pColNa
103e8 6d 65 2c 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20 53  me, zName, -1, S
103e9 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49  QLITE_UTF8, SQLI
103ea 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 65  TE_STATIC);.  }e
103eb 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  lse{.    rc = sq
103ec 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53  lite3VdbeMemSetS
103ed 74 72 28 70 43 6f 6c 4e 61 6d 65 2c 20 7a 4e 61  tr(pColName, zNa
103ee 6d 65 2c 20 4e 2c 20 53 51 4c 49 54 45 5f 55 54  me, N, SQLITE_UT
103ef 46 38 2c 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  F8,SQLITE_TRANSI
103f0 45 4e 54 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  ENT);.  }.  if( 
103f1 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
103f2 20 4e 3d 3d 50 33 5f 44 59 4e 41 4d 49 43 20 29   N==P3_DYNAMIC )
103f3 7b 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65 2d 3e  {.    pColName->
103f4 66 6c 61 67 73 20 3d 20 28 70 43 6f 6c 4e 61 6d  flags = (pColNam
103f5 65 2d 3e 66 6c 61 67 73 26 28 7e 4d 45 4d 5f 53  e->flags&(~MEM_S
103f6 74 61 74 69 63 29 29 7c 4d 45 4d 5f 44 79 6e 3b  tatic))|MEM_Dyn;
103f7 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65 2d 3e 78  .    pColName->x
103f8 44 65 6c 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72  Del = 0;.  }.  r
103f9 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
103fa 2a 2a 20 41 20 72 65 61 64 20 6f 72 20 77 72 69  ** A read or wri
103fb 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d  te transaction m
103fc 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65  ay or may not be
103fd 20 61 63 74 69 76 65 20 6f 6e 20 64 61 74 61 62   active on datab
103fe 61 73 65 20 68 61 6e 64 6c 65 0a 2a 2a 20 64 62  ase handle.** db
103ff 2e 20 49 66 20 61 20 74 72 61 6e 73 61 63 74 69  . If a transacti
10400 6f 6e 20 69 73 20 61 63 74 69 76 65 2c 20 63 6f  on is active, co
10401 6d 6d 69 74 20 69 74 2e 20 49 66 20 74 68 65 72  mmit it. If ther
10402 65 20 69 73 20 61 0a 2a 2a 20 77 72 69 74 65 2d  e is a.** write-
10403 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 70 61 6e  transaction span
10404 6e 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 6f  ning more than o
10405 6e 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ne database file
10406 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a  , this routine.*
10407 2a 20 74 61 6b 65 73 20 63 61 72 65 20 6f 66 20  * takes care of 
10408 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
10409 61 6c 20 74 72 69 63 6b 65 72 79 2e 0a 2a 2f 0a  al trickery..*/.
1040a 73 74 61 74 69 63 20 69 6e 74 20 76 64 62 65 43  static int vdbeC
1040b 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33 20 2a 64  ommit(sqlite3 *d
1040c 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  b){.  int i;.  i
1040d 6e 74 20 6e 54 72 61 6e 73 20 3d 20 30 3b 20 20  nt nTrans = 0;  
1040e 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 61 74  /* Number of dat
1040f 61 62 61 73 65 73 20 77 69 74 68 20 61 6e 20 61  abases with an a
10410 63 74 69 76 65 20 77 72 69 74 65 2d 74 72 61 6e  ctive write-tran
10411 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74  saction */.  int
10412 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
10413 0a 20 20 69 6e 74 20 6e 65 65 64 58 63 6f 6d 6d  .  int needXcomm
10414 69 74 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 42 65  it = 0;..  /* Be
10415 66 6f 72 65 20 64 6f 69 6e 67 20 61 6e 79 74 68  fore doing anyth
10416 69 6e 67 20 65 6c 73 65 2c 20 63 61 6c 6c 20 74  ing else, call t
10417 68 65 20 78 53 79 6e 63 28 29 20 63 61 6c 6c 62  he xSync() callb
10418 61 63 6b 20 66 6f 72 20 61 6e 79 0a 20 20 2a 2a  ack for any.  **
10419 20 76 69 72 74 75 61 6c 20 6d 6f 64 75 6c 65 20   virtual module 
1041a 74 61 62 6c 65 73 20 77 72 69 74 74 65 6e 20 69  tables written i
1041b 6e 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69  n this transacti
1041c 6f 6e 2e 20 54 68 69 73 20 68 61 73 20 74 6f 0a  on. This has to.
1041d 20 20 2a 2a 20 62 65 20 64 6f 6e 65 20 62 65 66    ** be done bef
1041e 6f 72 65 20 64 65 74 65 72 6d 69 6e 69 6e 67 20  ore determining 
1041f 77 68 65 74 68 65 72 20 61 20 6d 61 73 74 65 72  whether a master
10420 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73   journal file is
10421 20 0a 20 20 2a 2a 20 72 65 71 75 69 72 65 64 2c   .  ** required,
10422 20 61 73 20 61 6e 20 78 53 79 6e 63 28 29 20 63   as an xSync() c
10423 61 6c 6c 62 61 63 6b 20 6d 61 79 20 61 64 64 20  allback may add 
10424 61 6e 20 61 74 74 61 63 68 65 64 20 64 61 74 61  an attached data
10425 62 61 73 65 0a 20 20 2a 2a 20 74 6f 20 74 68 65  base.  ** to the
10426 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20   transaction..  
10427 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  */.  rc = sqlite
10428 33 56 74 61 62 53 79 6e 63 28 64 62 2c 20 72 63  3VtabSync(db, rc
10429 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
1042a 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65  ITE_OK ){.    re
1042b 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
1042c 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20 64 65 74  /* This loop det
1042d 65 72 6d 69 6e 65 73 20 28 61 29 20 69 66 20 74  ermines (a) if t
1042e 68 65 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 73  he commit hook s
1042f 68 6f 75 6c 64 20 62 65 20 69 6e 76 6f 6b 65 64  hould be invoked
10430 20 61 6e 64 0a 20 20 2a 2a 20 28 62 29 20 68 6f   and.  ** (b) ho
10431 77 20 6d 61 6e 79 20 64 61 74 61 62 61 73 65 20  w many database 
10432 66 69 6c 65 73 20 68 61 76 65 20 6f 70 65 6e 20  files have open 
10433 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f  write transactio
10434 6e 73 2c 20 6e 6f 74 20 0a 20 20 2a 2a 20 69 6e  ns, not .  ** in
10435 63 6c 75 64 69 6e 67 20 74 68 65 20 74 65 6d 70  cluding the temp
10436 20 64 61 74 61 62 61 73 65 2e 20 28 62 29 20 69   database. (b) i
10437 73 20 69 6d 70 6f 72 74 61 6e 74 20 62 65 63 61  s important beca
10438 75 73 65 20 69 66 20 6d 6f 72 65 20 74 68 61 6e  use if more than
10439 20 0a 20 20 2a 2a 20 6f 6e 65 20 64 61 74 61 62   .  ** one datab
1043a 61 73 65 20 66 69 6c 65 20 68 61 73 20 61 6e 20  ase file has an 
1043b 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73  open write trans
1043c 61 63 74 69 6f 6e 2c 20 61 20 6d 61 73 74 65 72  action, a master
1043d 20 6a 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69   journal.  ** fi
1043e 6c 65 20 69 73 20 72 65 71 75 69 72 65 64 20 66  le is required f
1043f 6f 72 20 61 6e 20 61 74 6f 6d 69 63 20 63 6f 6d  or an atomic com
10440 6d 69 74 2e 0a 20 20 2a 2f 20 0a 20 20 66 6f 72  mit..  */ .  for
10441 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b  (i=0; i<db->nDb;
10442 20 69 2b 2b 29 7b 20 0a 20 20 20 20 42 74 72 65   i++){ .    Btre
10443 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62  e *pBt = db->aDb
10444 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28  [i].pBt;.    if(
10445 20 70 42 74 20 26 26 20 73 71 6c 69 74 65 33 42   pBt && sqlite3B
10446 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70 42  treeIsInTrans(pB
10447 74 29 20 29 7b 0a 20 20 20 20 20 20 6e 65 65 64  t) ){.      need
10448 58 63 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20  Xcommit = 1;.   
10449 20 20 20 69 66 28 20 69 21 3d 31 20 29 20 6e 54     if( i!=1 ) nT
1044a 72 61 6e 73 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20  rans++;.    }.  
1044b 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65  }..  /* If there
1044c 20 61 72 65 20 61 6e 79 20 77 72 69 74 65 2d 74   are any write-t
1044d 72 61 6e 73 61 63 74 69 6f 6e 73 20 61 74 20 61  ransactions at a
1044e 6c 6c 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 63  ll, invoke the c
1044f 6f 6d 6d 69 74 20 68 6f 6f 6b 20 2a 2f 0a 20 20  ommit hook */.  
10450 69 66 28 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20  if( needXcommit 
10451 26 26 20 64 62 2d 3e 78 43 6f 6d 6d 69 74 43 61  && db->xCommitCa
10452 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 73 71  llback ){.    sq
10453 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64  lite3SafetyOff(d
10454 62 29 3b 0a 20 20 20 20 72 63 20 3d 20 64 62 2d  b);.    rc = db-
10455 3e 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b  >xCommitCallback
10456 28 64 62 2d 3e 70 43 6f 6d 6d 69 74 41 72 67 29  (db->pCommitArg)
10457 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 61 66  ;.    sqlite3Saf
10458 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 69  etyOn(db);.    i
10459 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72  f( rc ){.      r
1045a 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 4e  eturn SQLITE_CON
1045b 53 54 52 41 49 4e 54 3b 0a 20 20 20 20 7d 0a 20  STRAINT;.    }. 
1045c 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 73 69 6d   }..  /* The sim
1045d 70 6c 65 20 63 61 73 65 20 2d 20 6e 6f 20 6d 6f  ple case - no mo
1045e 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 61 74 61  re than one data
1045f 62 61 73 65 20 66 69 6c 65 20 28 6e 6f 74 20 63  base file (not c
10460 6f 75 6e 74 69 6e 67 20 74 68 65 0a 20 20 2a 2a  ounting the.  **
10461 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 29 20   TEMP database) 
10462 68 61 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f  has a transactio
10463 6e 20 61 63 74 69 76 65 2e 20 20 20 54 68 65 72  n active.   Ther
10464 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 66 6f 72  e is no need for
10465 20 74 68 65 0a 20 20 2a 2a 20 6d 61 73 74 65 72   the.  ** master
10466 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 20 2a 2a 0a 20  -journal..  **. 
10467 20 2a 2a 20 49 66 20 74 68 65 20 72 65 74 75 72   ** If the retur
10468 6e 20 76 61 6c 75 65 20 6f 66 20 73 71 6c 69 74  n value of sqlit
10469 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61  e3BtreeGetFilena
1046a 6d 65 28 29 20 69 73 20 61 20 7a 65 72 6f 20 6c  me() is a zero l
1046b 65 6e 67 74 68 0a 20 20 2a 2a 20 73 74 72 69 6e  ength.  ** strin
1046c 67 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 65 20  g, it means the 
1046d 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 69 73  main database is
1046e 20 3a 6d 65 6d 6f 72 79 3a 2e 20 20 49 6e 20 74   :memory:.  In t
1046f 68 61 74 20 63 61 73 65 20 77 65 20 64 6f 0a 20  hat case we do. 
10470 20 2a 2a 20 6e 6f 74 20 73 75 70 70 6f 72 74 20   ** not support 
10471 61 74 6f 6d 69 63 20 6d 75 6c 74 69 2d 66 69 6c  atomic multi-fil
10472 65 20 63 6f 6d 6d 69 74 73 2c 20 73 6f 20 75 73  e commits, so us
10473 65 20 74 68 65 20 73 69 6d 70 6c 65 20 63 61 73  e the simple cas
10474 65 20 74 68 65 6e 0a 20 20 2a 2a 20 74 6f 6f 2e  e then.  ** too.
10475 0a 20 20 2a 2f 0a 20 20 69 66 28 20 30 3d 3d 73  .  */.  if( 0==s
10476 74 72 6c 65 6e 28 73 71 6c 69 74 65 33 42 74 72  trlen(sqlite3Btr
10477 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 64 62  eeGetFilename(db
10478 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 29 29 20 7c  ->aDb[0].pBt)) |
10479 7c 20 6e 54 72 61 6e 73 3c 3d 31 20 29 7b 0a 20  | nTrans<=1 ){. 
1047a 20 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d     for(i=0; rc==
1047b 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64  SQLITE_OK && i<d
1047c 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20  b->nDb; i++){ . 
1047d 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20       Btree *pBt 
1047e 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74  = db->aDb[i].pBt
1047f 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 20  ;.      if( pBt 
10480 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
10481 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d  sqlite3BtreeComm
10482 69 74 50 68 61 73 65 4f 6e 65 28 70 42 74 2c 20  itPhaseOne(pBt, 
10483 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  0);.      }.    
10484 7d 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 74 68 65  }..    /* Do the
10485 20 63 6f 6d 6d 69 74 20 6f 6e 6c 79 20 69 66 20   commit only if 
10486 61 6c 6c 20 64 61 74 61 62 61 73 65 73 20 73 75  all databases su
10487 63 63 65 73 73 66 75 6c 6c 79 20 63 6f 6d 70 6c  ccessfully compl
10488 65 74 65 20 70 68 61 73 65 20 31 2e 20 0a 20 20  ete phase 1. .  
10489 20 20 2a 2a 20 49 66 20 6f 6e 65 20 6f 66 20 74    ** If one of t
1048a 68 65 20 42 74 72 65 65 43 6f 6d 6d 69 74 50 68  he BtreeCommitPh
1048b 61 73 65 4f 6e 65 28 29 20 63 61 6c 6c 73 20 66  aseOne() calls f
1048c 61 69 6c 73 2c 20 74 68 69 73 20 69 6e 64 69 63  ails, this indic
1048d 61 74 65 73 20 61 6e 0a 20 20 20 20 2a 2a 20 49  ates an.    ** I
1048e 4f 20 65 72 72 6f 72 20 77 68 69 6c 65 20 64 65  O error while de
1048f 6c 65 74 69 6e 67 20 6f 72 20 74 72 75 6e 63 61  leting or trunca
10490 74 69 6e 67 20 61 20 6a 6f 75 72 6e 61 6c 20 66  ting a journal f
10491 69 6c 65 2e 20 49 74 20 69 73 20 75 6e 6c 69 6b  ile. It is unlik
10492 65 6c 79 2c 0a 20 20 20 20 2a 2a 20 62 75 74 20  ely,.    ** but 
10493 63 6f 75 6c 64 20 68 61 70 70 65 6e 2e 20 49 6e  could happen. In
10494 20 74 68 69 73 20 63 61 73 65 20 61 62 61 6e 64   this case aband
10495 6f 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 61 6e  on processing an
10496 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 72  d return the err
10497 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66  or..    */.    f
10498 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49  or(i=0; rc==SQLI
10499 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e  TE_OK && i<db->n
1049a 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  Db; i++){.      
1049b 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d  Btree *pBt = db-
1049c 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20  >aDb[i].pBt;.   
1049d 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20     if( pBt ){.  
1049e 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1049f 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61  e3BtreeCommitPha
104a0 73 65 54 77 6f 28 70 42 74 29 3b 0a 20 20 20 20  seTwo(pBt);.    
104a1 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
104a2 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
104a3 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
104a4 56 74 61 62 43 6f 6d 6d 69 74 28 64 62 29 3b 0a  VtabCommit(db);.
104a5 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
104a6 54 68 65 20 63 6f 6d 70 6c 65 78 20 63 61 73 65  The complex case
104a7 20 2d 20 54 68 65 72 65 20 69 73 20 61 20 6d 75   - There is a mu
104a8 6c 74 69 2d 66 69 6c 65 20 77 72 69 74 65 2d 74  lti-file write-t
104a9 72 61 6e 73 61 63 74 69 6f 6e 20 61 63 74 69 76  ransaction activ
104aa 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 72 65 71  e..  ** This req
104ab 75 69 72 65 73 20 61 20 6d 61 73 74 65 72 20 6a  uires a master j
104ac 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 65  ournal file to e
104ad 6e 73 75 72 65 20 74 68 65 20 74 72 61 6e 73 61  nsure the transa
104ae 63 74 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 63 6f  ction is.  ** co
104af 6d 6d 69 74 74 65 64 20 61 74 6f 6d 69 63 6c 79  mmitted atomicly
104b0 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ..  */.#ifndef S
104b1 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49  QLITE_OMIT_DISKI
104b2 4f 0a 20 20 65 6c 73 65 7b 0a 20 20 20 20 69 6e  O.  else{.    in
104b3 74 20 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a  t needSync = 0;.
104b4 20 20 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 65      char *zMaste
104b5 72 20 3d 20 30 3b 20 20 20 2f 2a 20 46 69 6c 65  r = 0;   /* File
104b6 2d 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 6d 61  -name for the ma
104b7 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a  ster journal */.
104b8 20 20 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a      char const *
104b9 7a 4d 61 69 6e 46 69 6c 65 20 3d 20 73 71 6c 69  zMainFile = sqli
104ba 74 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e  te3BtreeGetFilen
104bb 61 6d 65 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70  ame(db->aDb[0].p
104bc 42 74 29 3b 0a 20 20 20 20 4f 73 46 69 6c 65 20  Bt);.    OsFile 
104bd 2a 6d 61 73 74 65 72 20 3d 20 30 3b 0a 0a 20 20  *master = 0;..  
104be 20 20 2f 2a 20 53 65 6c 65 63 74 20 61 20 6d 61    /* Select a ma
104bf 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
104c0 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 64 6f  e name */.    do
104c1 20 7b 0a 20 20 20 20 20 20 75 33 32 20 72 61 6e   {.      u32 ran
104c2 64 6f 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  dom;.      sqlit
104c3 65 46 72 65 65 28 7a 4d 61 73 74 65 72 29 3b 0a  eFree(zMaster);.
104c4 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 61 6e        sqlite3Ran
104c5 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 72  domness(sizeof(r
104c6 61 6e 64 6f 6d 29 2c 20 26 72 61 6e 64 6f 6d 29  andom), &random)
104c7 3b 0a 20 20 20 20 20 20 7a 4d 61 73 74 65 72 20  ;.      zMaster 
104c8 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
104c9 28 22 25 73 2d 6d 6a 25 30 38 58 22 2c 20 7a 4d  ("%s-mj%08X", zM
104ca 61 69 6e 46 69 6c 65 2c 20 72 61 6e 64 6f 6d 26  ainFile, random&
104cb 30 78 37 66 66 66 66 66 66 66 29 3b 0a 20 20 20  0x7fffffff);.   
104cc 20 20 20 69 66 28 20 21 7a 4d 61 73 74 65 72 20     if( !zMaster 
104cd 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
104ce 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
104cf 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 77 68 69        }.    }whi
104d0 6c 65 28 20 73 71 6c 69 74 65 33 4f 73 46 69 6c  le( sqlite3OsFil
104d1 65 45 78 69 73 74 73 28 7a 4d 61 73 74 65 72 29  eExists(zMaster)
104d2 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 4f 70 65 6e   );..    /* Open
104d3 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
104d4 6e 61 6c 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d  nal. */.    rc =
104d5 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 45 78   sqlite3OsOpenEx
104d6 63 6c 75 73 69 76 65 28 7a 4d 61 73 74 65 72 2c  clusive(zMaster,
104d7 20 26 6d 61 73 74 65 72 2c 20 30 29 3b 0a 20 20   &master, 0);.  
104d8 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
104d9 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  _OK ){.      sql
104da 69 74 65 46 72 65 65 28 7a 4d 61 73 74 65 72 29  iteFree(zMaster)
104db 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  ;.      return r
104dc 63 3b 0a 20 20 20 20 7d 0a 20 0a 20 20 20 20 2f  c;.    }. .    /
104dd 2a 20 57 72 69 74 65 20 74 68 65 20 6e 61 6d 65  * Write the name
104de 20 6f 66 20 65 61 63 68 20 64 61 74 61 62 61 73   of each databas
104df 65 20 66 69 6c 65 20 69 6e 20 74 68 65 20 74 72  e file in the tr
104e0 61 6e 73 61 63 74 69 6f 6e 20 69 6e 74 6f 20 74  ansaction into t
104e1 68 65 20 6e 65 77 0a 20 20 20 20 2a 2a 20 6d 61  he new.    ** ma
104e2 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
104e3 65 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  e. If an error o
104e4 63 63 75 72 73 20 61 74 20 74 68 69 73 20 70 6f  ccurs at this po
104e5 69 6e 74 20 63 6c 6f 73 65 0a 20 20 20 20 2a 2a  int close.    **
104e6 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68 65 20   and delete the 
104e7 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
104e8 69 6c 65 2e 20 41 6c 6c 20 74 68 65 20 69 6e 64  ile. All the ind
104e9 69 76 69 64 75 61 6c 20 6a 6f 75 72 6e 61 6c 20  ividual journal 
104ea 66 69 6c 65 73 0a 20 20 20 20 2a 2a 20 73 74 69  files.    ** sti
104eb 6c 6c 20 68 61 76 65 20 27 6e 75 6c 6c 27 20 61  ll have 'null' a
104ec 73 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  s the master jou
104ed 72 6e 61 6c 20 70 6f 69 6e 74 65 72 2c 20 73 6f  rnal pointer, so
104ee 20 74 68 65 79 20 77 69 6c 6c 20 72 6f 6c 6c 0a   they will roll.
104ef 20 20 20 20 2a 2a 20 62 61 63 6b 20 69 6e 64 65      ** back inde
104f0 70 65 6e 64 65 6e 74 6c 79 20 69 66 20 61 20 66  pendently if a f
104f1 61 69 6c 75 72 65 20 6f 63 63 75 72 73 2e 0a 20  ailure occurs.. 
104f2 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d     */.    for(i=
104f3 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  0; i<db->nDb; i+
104f4 2b 29 7b 20 0a 20 20 20 20 20 20 42 74 72 65 65  +){ .      Btree
104f5 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b   *pBt = db->aDb[
104f6 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66  i].pBt;.      if
104f7 28 20 69 3d 3d 31 20 29 20 63 6f 6e 74 69 6e 75  ( i==1 ) continu
104f8 65 3b 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74  e;   /* Ignore t
104f9 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65  he TEMP database
104fa 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 42   */.      if( pB
104fb 74 20 26 26 20 73 71 6c 69 74 65 33 42 74 72 65  t && sqlite3Btre
104fc 65 49 73 49 6e 54 72 61 6e 73 28 70 42 74 29 20  eIsInTrans(pBt) 
104fd 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20  ){.        char 
104fe 63 6f 6e 73 74 20 2a 7a 46 69 6c 65 20 3d 20 73  const *zFile = s
104ff 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4a 6f  qlite3BtreeGetJo
10500 75 72 6e 61 6c 6e 61 6d 65 28 70 42 74 29 3b 0a  urnalname(pBt);.
10501 20 20 20 20 20 20 20 20 69 66 28 20 7a 46 69 6c          if( zFil
10502 65 5b 30 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e  e[0]==0 ) contin
10503 75 65 3b 20 20 2f 2a 20 49 67 6e 6f 72 65 20 3a  ue;  /* Ignore :
10504 6d 65 6d 6f 72 79 3a 20 64 61 74 61 62 61 73 65  memory: database
10505 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28  s */.        if(
10506 20 21 6e 65 65 64 53 79 6e 63 20 26 26 20 21 73   !needSync && !s
10507 71 6c 69 74 65 33 42 74 72 65 65 53 79 6e 63 44  qlite3BtreeSyncD
10508 69 73 61 62 6c 65 64 28 70 42 74 29 20 29 7b 0a  isabled(pBt) ){.
10509 20 20 20 20 20 20 20 20 20 20 6e 65 65 64 53 79            needSy
1050a 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  nc = 1;.        
1050b 7d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  }.        rc = s
1050c 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 6d 61  qlite3OsWrite(ma
1050d 73 74 65 72 2c 20 7a 46 69 6c 65 2c 20 73 74 72  ster, zFile, str
1050e 6c 65 6e 28 7a 46 69 6c 65 29 2b 31 29 3b 0a 20  len(zFile)+1);. 
1050f 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
10510 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
10511 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43        sqlite3OsC
10512 6c 6f 73 65 28 26 6d 61 73 74 65 72 29 3b 0a 20  lose(&master);. 
10513 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
10514 4f 73 44 65 6c 65 74 65 28 7a 4d 61 73 74 65 72  OsDelete(zMaster
10515 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
10516 69 74 65 46 72 65 65 28 7a 4d 61 73 74 65 72 29  iteFree(zMaster)
10517 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  ;.          retu
10518 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d  rn rc;.        }
10519 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
1051a 0a 20 20 20 20 2f 2a 20 53 79 6e 63 20 74 68 65  .    /* Sync the
1051b 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
1051c 66 69 6c 65 2e 20 42 65 66 6f 72 65 20 64 6f 69  file. Before doi
1051d 6e 67 20 74 68 69 73 2c 20 6f 70 65 6e 20 74 68  ng this, open th
1051e 65 20 64 69 72 65 63 74 6f 72 79 0a 20 20 20 20  e directory.    
1051f 2a 2a 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  ** the master jo
10520 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 73 74  urnal file is st
10521 6f 72 65 20 69 6e 20 73 6f 20 74 68 61 74 20 69  ore in so that i
10522 74 20 67 65 74 73 20 73 79 6e 63 65 64 20 74 6f  t gets synced to
10523 6f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7a 4d  o..    */.    zM
10524 61 69 6e 46 69 6c 65 20 3d 20 73 71 6c 69 74 65  ainFile = sqlite
10525 33 42 74 72 65 65 47 65 74 44 69 72 6e 61 6d 65  3BtreeGetDirname
10526 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 29  (db->aDb[0].pBt)
10527 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
10528 65 33 4f 73 4f 70 65 6e 44 69 72 65 63 74 6f 72  e3OsOpenDirector
10529 79 28 6d 61 73 74 65 72 2c 20 7a 4d 61 69 6e 46  y(master, zMainF
1052a 69 6c 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ile);.    if( rc
1052b 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 0a 20  !=SQLITE_OK ||. 
1052c 20 20 20 20 20 20 20 20 20 28 6e 65 65 64 53 79           (needSy
1052d 6e 63 20 26 26 20 28 72 63 3d 73 71 6c 69 74 65  nc && (rc=sqlite
1052e 33 4f 73 53 79 6e 63 28 6d 61 73 74 65 72 2c 30  3OsSync(master,0
1052f 29 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29  ))!=SQLITE_OK) )
10530 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f  {.      sqlite3O
10531 73 43 6c 6f 73 65 28 26 6d 61 73 74 65 72 29 3b  sClose(&master);
10532 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73  .      sqlite3Os
10533 44 65 6c 65 74 65 28 7a 4d 61 73 74 65 72 29 3b  Delete(zMaster);
10534 0a 20 20 20 20 20 20 73 71 6c 69 74 65 46 72 65  .      sqliteFre
10535 65 28 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20  e(zMaster);.    
10536 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
10537 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 79 6e 63 20   }..    /* Sync 
10538 61 6c 6c 20 74 68 65 20 64 62 20 66 69 6c 65 73  all the db files
10539 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 74 68 65   involved in the
1053a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68   transaction. Th
1053b 65 20 73 61 6d 65 20 63 61 6c 6c 0a 20 20 20 20  e same call.    
1053c 2a 2a 20 73 65 74 73 20 74 68 65 20 6d 61 73 74  ** sets the mast
1053d 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74  er journal point
1053e 65 72 20 69 6e 20 65 61 63 68 20 69 6e 64 69 76  er in each indiv
1053f 69 64 75 61 6c 20 6a 6f 75 72 6e 61 6c 2e 20 49  idual journal. I
10540 66 0a 20 20 20 20 2a 2a 20 61 6e 20 65 72 72 6f  f.    ** an erro
10541 72 20 6f 63 63 75 72 73 20 68 65 72 65 2c 20 64  r occurs here, d
10542 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 74 68 65  o not delete the
10543 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
10544 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  file..    **.   
10545 20 2a 2a 20 49 66 20 74 68 65 20 65 72 72 6f 72   ** If the error
10546 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 74   occurs during t
10547 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20 74 6f  he first call to
10548 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 42  .    ** sqlite3B
10549 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f  treeCommitPhaseO
1054a 6e 65 28 29 2c 20 74 68 65 6e 20 74 68 65 72 65  ne(), then there
1054b 20 69 73 20 61 20 63 68 61 6e 63 65 20 74 68 61   is a chance tha
1054c 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 6d 61 73  t the.    ** mas
1054d 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
1054e 20 77 69 6c 6c 20 62 65 20 6f 72 70 68 61 6e 65   will be orphane
1054f 64 2e 20 42 75 74 20 77 65 20 63 61 6e 6e 6f 74  d. But we cannot
10550 20 64 65 6c 65 74 65 20 69 74 2c 0a 20 20 20 20   delete it,.    
10551 2a 2a 20 69 6e 20 63 61 73 65 20 74 68 65 20 6d  ** in case the m
10552 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
10553 6c 65 20 6e 61 6d 65 20 77 61 73 20 77 72 69 74  le name was writ
10554 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75  ten into the jou
10555 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 66 69 6c 65  rnal.    ** file
10556 20 62 65 66 6f 72 65 20 74 68 65 20 66 61 69 6c   before the fail
10557 75 72 65 20 6f 63 63 75 72 65 64 2e 0a 20 20 20  ure occured..   
10558 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   */.    for(i=0;
10559 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
1055a 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  & i<db->nDb; i++
1055b 29 7b 20 0a 20 20 20 20 20 20 42 74 72 65 65 20  ){ .      Btree 
1055c 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69  *pBt = db->aDb[i
1055d 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66 28  ].pBt;.      if(
1055e 20 70 42 74 20 26 26 20 73 71 6c 69 74 65 33 42   pBt && sqlite3B
1055f 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70 42  treeIsInTrans(pB
10560 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  t) ){.        rc
10561 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43   = sqlite3BtreeC
10562 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 42  ommitPhaseOne(pB
10563 74 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20  t, zMaster);.   
10564 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73     }.    }.    s
10565 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 26 6d  qlite3OsClose(&m
10566 61 73 74 65 72 29 3b 0a 20 20 20 20 69 66 28 20  aster);.    if( 
10567 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
10568 0a 20 20 20 20 20 20 73 71 6c 69 74 65 46 72 65  .      sqliteFre
10569 65 28 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20  e(zMaster);.    
1056a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
1056b 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65 74   }..    /* Delet
1056c 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  e the master jou
1056d 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69 73 20  rnal file. This 
1056e 63 6f 6d 6d 69 74 73 20 74 68 65 20 74 72 61 6e  commits the tran
1056f 73 61 63 74 69 6f 6e 2e 20 41 66 74 65 72 0a 20  saction. After. 
10570 20 20 20 2a 2a 20 64 6f 69 6e 67 20 74 68 69 73     ** doing this
10571 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 69   the directory i
10572 73 20 73 79 6e 63 65 64 20 61 67 61 69 6e 20 62  s synced again b
10573 65 66 6f 72 65 20 61 6e 79 20 69 6e 64 69 76 69  efore any indivi
10574 64 75 61 6c 0a 20 20 20 20 2a 2a 20 74 72 61 6e  dual.    ** tran
10575 73 61 63 74 69 6f 6e 20 66 69 6c 65 73 20 61 72  saction files ar
10576 65 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a  e deleted..    *
10577 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  /.    rc = sqlit
10578 65 33 4f 73 44 65 6c 65 74 65 28 7a 4d 61 73 74  e3OsDelete(zMast
10579 65 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 46  er);.    sqliteF
1057a 72 65 65 28 7a 4d 61 73 74 65 72 29 3b 0a 20 20  ree(zMaster);.  
1057b 20 20 7a 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20    zMaster = 0;. 
1057c 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20     if( rc ){.   
1057d 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
1057e 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c    }.    rc = sql
1057f 69 74 65 33 4f 73 53 79 6e 63 44 69 72 65 63 74  ite3OsSyncDirect
10580 6f 72 79 28 7a 4d 61 69 6e 46 69 6c 65 29 3b 0a  ory(zMainFile);.
10581 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
10582 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 2f  TE_OK ){.      /
10583 2a 20 54 68 69 73 20 69 73 20 6e 6f 74 20 67 6f  * This is not go
10584 6f 64 2e 20 54 68 65 20 6d 61 73 74 65 72 20 6a  od. The master j
10585 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 73 20  ournal file has 
10586 62 65 65 6e 20 64 65 6c 65 74 65 64 2c 20 62 75  been deleted, bu
10587 74 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 64  t.      ** the d
10588 69 72 65 63 74 6f 72 79 20 73 79 6e 63 20 66 61  irectory sync fa
10589 69 6c 65 64 2e 20 54 68 65 72 65 20 69 73 20 6e  iled. There is n
1058a 6f 20 63 6f 6d 70 6c 65 74 65 6c 79 20 73 61 66  o completely saf
1058b 65 20 63 6f 75 72 73 65 20 6f 66 0a 20 20 20 20  e course of.    
1058c 20 20 2a 2a 20 61 63 74 69 6f 6e 20 66 72 6f 6d    ** action from
1058d 20 68 65 72 65 2e 20 54 68 65 20 69 6e 64 69 76   here. The indiv
1058e 69 64 75 61 6c 20 6a 6f 75 72 6e 61 6c 73 20 63  idual journals c
1058f 6f 6e 74 61 69 6e 20 74 68 65 20 6e 61 6d 65 20  ontain the name 
10590 6f 66 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20  of the.      ** 
10591 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
10592 69 6c 65 2c 20 62 75 74 20 74 68 65 72 65 20 69  ile, but there i
10593 73 20 6e 6f 20 77 61 79 20 6f 66 20 6b 6e 6f 77  s no way of know
10594 69 6e 67 20 69 66 20 74 68 61 74 0a 20 20 20 20  ing if that.    
10595 20 20 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72    ** master jour
10596 6e 61 6c 20 65 78 69 73 74 73 20 6e 6f 77 20 6f  nal exists now o
10597 72 20 69 66 20 69 74 20 77 69 6c 6c 20 65 78 69  r if it will exi
10598 73 74 20 61 66 74 65 72 20 74 68 65 20 6f 70 65  st after the ope
10599 72 61 74 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20  rating.      ** 
1059a 73 79 73 74 65 6d 20 63 72 61 73 68 20 74 68 61  system crash tha
1059b 74 20 6d 61 79 20 66 6f 6c 6c 6f 77 20 74 68 65  t may follow the
1059c 20 66 73 79 6e 63 28 29 20 66 61 69 6c 75 72 65   fsync() failure
1059d 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
1059e 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
1059f 7d 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c 20 66 69  }..    /* All fi
105a0 6c 65 73 20 61 6e 64 20 64 69 72 65 63 74 6f 72  les and director
105a1 69 65 73 20 68 61 76 65 20 61 6c 72 65 61 64 79  ies have already
105a2 20 62 65 65 6e 20 73 79 6e 63 65 64 2c 20 73 6f   been synced, so
105a3 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20   the following. 
105a4 20 20 20 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73     ** calls to s
105a5 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69  qlite3BtreeCommi
105a6 74 50 68 61 73 65 54 77 6f 28 29 20 61 72 65 20  tPhaseTwo() are 
105a7 6f 6e 6c 79 20 63 6c 6f 73 69 6e 67 20 66 69 6c  only closing fil
105a8 65 73 20 61 6e 64 0a 20 20 20 20 2a 2a 20 64 65  es and.    ** de
105a9 6c 65 74 69 6e 67 20 6f 72 20 74 72 75 6e 63 61  leting or trunca
105aa 74 69 6e 67 20 6a 6f 75 72 6e 61 6c 73 2e 20 49  ting journals. I
105ab 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73  f something goes
105ac 20 77 72 6f 6e 67 20 77 68 69 6c 65 0a 20 20 20   wrong while.   
105ad 20 2a 2a 20 74 68 69 73 20 69 73 20 68 61 70 70   ** this is happ
105ae 65 6e 69 6e 67 20 77 65 20 64 6f 6e 27 74 20 72  ening we don't r
105af 65 61 6c 6c 79 20 63 61 72 65 2e 20 54 68 65 20  eally care. The 
105b0 69 6e 74 65 67 72 69 74 79 20 6f 66 20 74 68 65  integrity of the
105b1 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74  .    ** transact
105b2 69 6f 6e 20 69 73 20 61 6c 72 65 61 64 79 20 67  ion is already g
105b3 75 61 72 61 6e 74 65 65 64 2c 20 62 75 74 20 73  uaranteed, but s
105b4 6f 6d 65 20 73 74 72 61 79 20 27 63 6f 6c 64 27  ome stray 'cold'
105b5 20 6a 6f 75 72 6e 61 6c 73 0a 20 20 20 20 2a 2a   journals.    **
105b6 20 6d 61 79 20 62 65 20 6c 79 69 6e 67 20 61 72   may be lying ar
105b7 6f 75 6e 64 2e 20 52 65 74 75 72 6e 69 6e 67 20  ound. Returning 
105b8 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 77 6f  an error code wo
105b9 6e 27 74 20 68 65 6c 70 20 6d 61 74 74 65 72 73  n't help matters
105ba 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 64 69 73  ..    */.    dis
105bb 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69  able_simulated_i
105bc 6f 5f 65 72 72 6f 72 73 28 29 3b 0a 20 20 20 20  o_errors();.    
105bd 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e  for(i=0; i<db->n
105be 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 20 20 20  Db; i++){ .     
105bf 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62   Btree *pBt = db
105c0 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20  ->aDb[i].pBt;.  
105c1 20 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20      if( pBt ){. 
105c2 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74         sqlite3Bt
105c3 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77  reeCommitPhaseTw
105c4 6f 28 70 42 74 29 3b 0a 20 20 20 20 20 20 7d 0a  o(pBt);.      }.
105c5 20 20 20 20 7d 0a 20 20 20 20 65 6e 61 62 6c 65      }.    enable
105c6 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72  _simulated_io_er
105c7 72 6f 72 73 28 29 3b 0a 0a 20 20 20 20 73 71 6c  rors();..    sql
105c8 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74 28 64  ite3VtabCommit(d
105c9 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  b);.  }.#endif..
105ca 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
105cb 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  /* .** This rout
105cc 69 6e 65 20 63 68 65 63 6b 73 20 74 68 61 74 20  ine checks that 
105cd 74 68 65 20 73 71 6c 69 74 65 33 2e 61 63 74 69  the sqlite3.acti
105ce 76 65 56 64 62 65 43 6e 74 20 63 6f 75 6e 74 20  veVdbeCnt count 
105cf 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d 61 74 63  variable.** matc
105d0 68 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  hes the number o
105d1 66 20 76 64 62 65 27 73 20 69 6e 20 74 68 65 20  f vdbe's in the 
105d2 6c 69 73 74 20 73 71 6c 69 74 65 33 2e 70 56 64  list sqlite3.pVd
105d3 62 65 20 74 68 61 74 20 61 72 65 0a 2a 2a 20 63  be that are.** c
105d4 75 72 72 65 6e 74 6c 79 20 61 63 74 69 76 65 2e  urrently active.
105d5 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61   An assertion fa
105d6 69 6c 73 20 69 66 20 74 68 65 20 74 77 6f 20 63  ils if the two c
105d7 6f 75 6e 74 73 20 64 6f 20 6e 6f 74 20 6d 61 74  ounts do not mat
105d8 63 68 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 61  ch..** This is a
105d9 6e 20 69 6e 74 65 72 6e 61 6c 20 73 65 6c 66 2d  n internal self-
105da 63 68 65 63 6b 20 6f 6e 6c 79 20 2d 20 69 74 20  check only - it 
105db 69 73 20 6e 6f 74 20 61 6e 20 65 73 73 65 6e 74  is not an essent
105dc 69 61 6c 20 70 72 6f 63 65 73 73 69 6e 67 0a 2a  ial processing.*
105dd 2a 20 73 74 65 70 2e 0a 2a 2a 0a 2a 2a 20 54 68  * step..**.** Th
105de 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66  is is a no-op if
105df 20 4e 44 45 42 55 47 20 69 73 20 64 65 66 69 6e   NDEBUG is defin
105e0 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4e  ed..*/.#ifndef N
105e1 44 45 42 55 47 0a 73 74 61 74 69 63 20 76 6f 69  DEBUG.static voi
105e2 64 20 63 68 65 63 6b 41 63 74 69 76 65 56 64 62  d checkActiveVdb
105e3 65 43 6e 74 28 73 71 6c 69 74 65 33 20 2a 64 62  eCnt(sqlite3 *db
105e4 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 20  ){.  Vdbe *p;.  
105e5 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 70  int cnt = 0;.  p
105e6 20 3d 20 64 62 2d 3e 70 56 64 62 65 3b 0a 20 20   = db->pVdbe;.  
105e7 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 20  while( p ){.    
105e8 69 66 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44  if( p->magic==VD
105e9 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 26 26 20  BE_MAGIC_RUN && 
105ea 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20  p->pc>=0 ){.    
105eb 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20    cnt++;.    }. 
105ec 20 20 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b     p = p->pNext;
105ed 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 63  .  }.  assert( c
105ee 6e 74 3d 3d 64 62 2d 3e 61 63 74 69 76 65 56 64  nt==db->activeVd
105ef 62 65 43 6e 74 20 29 3b 0a 7d 0a 23 65 6c 73 65  beCnt );.}.#else
105f0 0a 23 64 65 66 69 6e 65 20 63 68 65 63 6b 41 63  .#define checkAc
105f1 74 69 76 65 56 64 62 65 43 6e 74 28 78 29 0a 23  tiveVdbeCnt(x).#
105f2 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e  endif../*.** Fin
105f3 64 20 65 76 65 72 79 20 61 63 74 69 76 65 20 56  d every active V
105f4 4d 20 6f 74 68 65 72 20 74 68 61 6e 20 70 56 64  M other than pVd
105f5 62 65 20 61 6e 64 20 63 68 61 6e 67 65 20 69 74  be and change it
105f6 73 20 73 74 61 74 75 73 20 74 6f 0a 2a 2a 20 61  s status to.** a
105f7 62 6f 72 74 65 64 2e 20 20 54 68 69 73 20 68 61  borted.  This ha
105f8 70 70 65 6e 73 20 77 68 65 6e 20 6f 6e 65 20 56  ppens when one V
105f9 4d 20 63 61 75 73 65 73 20 61 20 72 6f 6c 6c 62  M causes a rollb
105fa 61 63 6b 20 64 75 65 20 74 6f 20 61 6e 0a 2a 2a  ack due to an.**
105fb 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 52 4f 4c   ON CONFLICT ROL
105fc 4c 42 41 43 4b 20 63 6c 61 75 73 65 20 28 66 6f  LBACK clause (fo
105fd 72 20 65 78 61 6d 70 6c 65 29 2e 20 20 54 68 65  r example).  The
105fe 20 6f 74 68 65 72 20 56 4d 73 20 6d 75 73 74 20   other VMs must 
105ff 62 65 0a 2a 2a 20 61 62 6f 72 74 65 64 20 73 6f  be.** aborted so
10600 20 74 68 61 74 20 74 68 65 79 20 64 6f 20 6e 6f   that they do no
10601 74 20 68 61 76 65 20 64 61 74 61 20 72 6f 6c 6c  t have data roll
10602 65 64 20 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65  ed out from unde
10603 72 6e 65 61 74 68 0a 2a 2a 20 74 68 65 6d 20 6c  rneath.** them l
10604 65 61 64 69 6e 67 20 74 6f 20 61 20 73 65 67 66  eading to a segf
10605 61 75 6c 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ault..*/.SQLITE_
10606 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
10607 69 74 65 33 41 62 6f 72 74 4f 74 68 65 72 41 63  ite3AbortOtherAc
10608 74 69 76 65 56 64 62 65 73 28 73 71 6c 69 74 65  tiveVdbes(sqlite
10609 33 20 2a 64 62 2c 20 56 64 62 65 20 2a 70 45 78  3 *db, Vdbe *pEx
1060a 63 65 70 74 29 7b 0a 20 20 56 64 62 65 20 2a 70  cept){.  Vdbe *p
1060b 4f 74 68 65 72 3b 0a 20 20 66 6f 72 28 70 4f 74  Other;.  for(pOt
1060c 68 65 72 3d 64 62 2d 3e 70 56 64 62 65 3b 20 70  her=db->pVdbe; p
1060d 4f 74 68 65 72 3b 20 70 4f 74 68 65 72 3d 70 4f  Other; pOther=pO
1060e 74 68 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  ther->pNext){.  
1060f 20 20 69 66 28 20 70 4f 74 68 65 72 3d 3d 70 45    if( pOther==pE
10610 78 63 65 70 74 20 29 20 63 6f 6e 74 69 6e 75 65  xcept ) continue
10611 3b 0a 20 20 20 20 69 66 28 20 70 4f 74 68 65 72  ;.    if( pOther
10612 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d 41  ->magic!=VDBE_MA
10613 47 49 43 5f 52 55 4e 20 7c 7c 20 70 4f 74 68 65  GIC_RUN || pOthe
10614 72 2d 3e 70 63 3c 30 20 29 20 63 6f 6e 74 69 6e  r->pc<0 ) contin
10615 75 65 3b 0a 20 20 20 20 63 68 65 63 6b 41 63 74  ue;.    checkAct
10616 69 76 65 56 64 62 65 43 6e 74 28 64 62 29 3b 0a  iveVdbeCnt(db);.
10617 20 20 20 20 63 6c 6f 73 65 41 6c 6c 43 75 72 73      closeAllCurs
10618 6f 72 73 28 70 4f 74 68 65 72 29 3b 0a 20 20 20  ors(pOther);.   
10619 20 63 68 65 63 6b 41 63 74 69 76 65 56 64 62 65   checkActiveVdbe
1061a 43 6e 74 28 64 62 29 3b 0a 20 20 20 20 70 4f 74  Cnt(db);.    pOt
1061b 68 65 72 2d 3e 61 62 6f 72 74 65 64 20 3d 20 31  her->aborted = 1
1061c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
1061d 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
1061e 61 6c 6c 65 64 20 74 68 65 20 77 68 65 6e 20 61  alled the when a
1061f 20 56 44 42 45 20 74 72 69 65 73 20 74 6f 20 68   VDBE tries to h
10620 61 6c 74 2e 20 20 49 66 20 74 68 65 20 56 44 42  alt.  If the VDB
10621 45 0a 2a 2a 20 68 61 73 20 6d 61 64 65 20 63 68  E.** has made ch
10622 61 6e 67 65 73 20 61 6e 64 20 69 73 20 69 6e 20  anges and is in 
10623 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 2c  autocommit mode,
10624 20 74 68 65 6e 20 63 6f 6d 6d 69 74 20 74 68 6f   then commit tho
10625 73 65 0a 2a 2a 20 63 68 61 6e 67 65 73 2e 20 20  se.** changes.  
10626 49 66 20 61 20 72 6f 6c 6c 62 61 63 6b 20 69 73  If a rollback is
10627 20 6e 65 65 64 65 64 2c 20 74 68 65 6e 20 64 6f   needed, then do
10628 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a   the rollback..*
10629 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1062a 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 77 61  e is the only wa
1062b 79 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 73 74  y to move the st
1062c 61 74 65 20 6f 66 20 61 20 56 4d 20 66 72 6f 6d  ate of a VM from
1062d 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 47 49 43  .** SQLITE_MAGIC
1062e 5f 52 55 4e 20 74 6f 20 53 51 4c 49 54 45 5f 4d  _RUN to SQLITE_M
1062f 41 47 49 43 5f 48 41 4c 54 2e 0a 2a 2a 0a 2a 2a  AGIC_HALT..**.**
10630 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72   Return an error
10631 20 63 6f 64 65 2e 20 20 49 66 20 74 68 65 20 63   code.  If the c
10632 6f 6d 6d 69 74 20 63 6f 75 6c 64 20 6e 6f 74 20  ommit could not 
10633 63 6f 6d 70 6c 65 74 65 20 62 65 63 61 75 73 65  complete because
10634 20 6f 66 0a 2a 2a 20 6c 6f 63 6b 20 63 6f 6e 74   of.** lock cont
10635 65 6e 74 69 6f 6e 2c 20 72 65 74 75 72 6e 20 53  ention, return S
10636 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 49 66 20  QLITE_BUSY.  If 
10637 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72  SQLITE_BUSY is r
10638 65 74 75 72 6e 65 64 2c 20 69 74 0a 2a 2a 20 6d  eturned, it.** m
10639 65 61 6e 73 20 74 68 65 20 63 6c 6f 73 65 20 64  eans the close d
1063a 69 64 20 6e 6f 74 20 68 61 70 70 65 6e 20 61 6e  id not happen an
1063b 64 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65  d needs to be re
1063c 70 65 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  peated..*/.SQLIT
1063d 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1063e 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28 56 64  lite3VdbeHalt(Vd
1063f 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65  be *p){.  sqlite
10640 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20  3 *db = p->db;. 
10641 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 28 2a   int i;.  int (*
10642 78 46 75 6e 63 29 28 42 74 72 65 65 20 2a 70 42  xFunc)(Btree *pB
10643 74 29 20 3d 20 30 3b 20 20 2f 2a 20 46 75 6e 63  t) = 0;  /* Func
10644 74 69 6f 6e 20 74 6f 20 63 61 6c 6c 20 6f 6e 20  tion to call on 
10645 65 61 63 68 20 62 74 72 65 65 20 62 61 63 6b 65  each btree backe
10646 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 69 73 53 70  nd */.  int isSp
10647 65 63 69 61 6c 45 72 72 6f 72 3b 20 20 20 20 20  ecialError;     
10648 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 74 6f         /* Set to
10649 20 74 72 75 65 20 69 66 20 53 51 4c 49 54 45 5f   true if SQLITE_
1064a 4e 4f 4d 45 4d 20 6f 72 20 49 4f 45 52 52 20 2a  NOMEM or IOERR *
1064b 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 66 75 6e  /..  /* This fun
1064c 63 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74  ction contains t
1064d 68 65 20 6c 6f 67 69 63 20 74 68 61 74 20 64 65  he logic that de
1064e 74 65 72 6d 69 6e 65 73 20 69 66 20 61 20 73 74  termines if a st
1064f 61 74 65 6d 65 6e 74 20 6f 72 0a 20 20 2a 2a 20  atement or.  ** 
10650 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 69 6c 6c  transaction will
10651 20 62 65 20 63 6f 6d 6d 69 74 74 65 64 20 6f 72   be committed or
10652 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 73 20   rolled back as 
10653 61 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 0a  a result of the.
10654 20 20 2a 2a 20 65 78 65 63 75 74 69 6f 6e 20 6f    ** execution o
10655 66 20 74 68 69 73 20 76 69 72 74 75 61 6c 20 6d  f this virtual m
10656 61 63 68 69 6e 65 2e 20 0a 20 20 2a 2a 0a 20 20  achine. .  **.  
10657 2a 2a 20 53 70 65 63 69 61 6c 20 65 72 72 6f 72  ** Special error
10658 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  s:.  **.  **    
10659 20 49 66 20 61 6e 20 53 51 4c 49 54 45 5f 4e 4f   If an SQLITE_NO
1065a 4d 45 4d 20 65 72 72 6f 72 20 68 61 73 20 6f 63  MEM error has oc
1065b 63 75 72 65 64 20 69 6e 20 61 20 73 74 61 74 65  cured in a state
1065c 6d 65 6e 74 20 74 68 61 74 20 77 72 69 74 65 73  ment that writes
1065d 20 74 6f 0a 20 20 2a 2a 20 20 20 20 20 74 68 65   to.  **     the
1065e 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20   database, then 
1065f 65 69 74 68 65 72 20 61 20 73 74 61 74 65 6d 65  either a stateme
10660 6e 74 20 6f 72 20 74 72 61 6e 73 61 63 74 69 6f  nt or transactio
10661 6e 20 6d 75 73 74 20 62 65 20 72 6f 6c 6c 65 64  n must be rolled
10662 0a 20 20 2a 2a 20 20 20 20 20 62 61 63 6b 20 74  .  **     back t
10663 6f 20 65 6e 73 75 72 65 20 74 68 65 20 74 72 65  o ensure the tre
10664 65 2d 73 74 72 75 63 74 75 72 65 73 20 61 72 65  e-structures are
10665 20 69 6e 20 61 20 63 6f 6e 73 69 73 74 65 6e 74   in a consistent
10666 20 73 74 61 74 65 2e 20 41 0a 20 20 2a 2a 20 20   state. A.  **  
10667 20 20 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61     statement tra
10668 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c  nsaction is roll
10669 65 64 20 62 61 63 6b 20 69 66 20 6f 6e 65 20 69  ed back if one i
1066a 73 20 6f 70 65 6e 2c 20 6f 74 68 65 72 77 69 73  s open, otherwis
1066b 65 20 74 68 65 0a 20 20 2a 2a 20 20 20 20 20 65  e the.  **     e
1066c 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74 69 6f  ntire transactio
1066d 6e 20 6d 75 73 74 20 62 65 20 72 6f 6c 6c 65 64  n must be rolled
1066e 20 62 61 63 6b 2e 0a 20 20 2a 2a 0a 20 20 2a 2a   back..  **.  **
1066f 20 20 20 20 20 49 66 20 61 6e 20 53 51 4c 49 54       If an SQLIT
10670 45 5f 49 4f 45 52 52 20 65 72 72 6f 72 20 68 61  E_IOERR error ha
10671 73 20 6f 63 63 75 72 65 64 20 69 6e 20 61 20 73  s occured in a s
10672 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 77 72  tatement that wr
10673 69 74 65 73 20 74 6f 0a 20 20 2a 2a 20 20 20 20  ites to.  **    
10674 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 74   the database, t
10675 68 65 6e 20 74 68 65 20 65 6e 74 69 72 65 20 74  hen the entire t
10676 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20  ransaction must 
10677 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20  be rolled back. 
10678 54 68 65 0a 20 20 2a 2a 20 20 20 20 20 49 2f 4f  The.  **     I/O
10679 20 65 72 72 6f 72 20 6d 61 79 20 68 61 76 65 20   error may have 
1067a 63 61 75 73 65 64 20 67 61 72 62 61 67 65 20 74  caused garbage t
1067b 6f 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 20  o be written to 
1067c 74 68 65 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a  the journal .  *
1067d 2a 20 20 20 20 20 66 69 6c 65 2e 20 57 65 72 65  *     file. Were
1067e 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
1067f 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 61 6e 64   to continue and
10680 20 65 76 65 6e 74 75 61 6c 6c 79 20 62 65 20 72   eventually be r
10681 6f 6c 6c 65 64 20 0a 20 20 2a 2a 20 20 20 20 20  olled .  **     
10682 62 61 63 6b 20 74 68 61 74 20 67 61 72 62 61 67  back that garbag
10683 65 20 6d 69 67 68 74 20 65 6e 64 20 75 70 20 69  e might end up i
10684 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
10685 69 6c 65 2e 0a 20 20 2a 2a 20 20 20 20 20 0a 20  ile..  **     . 
10686 20 2a 2a 20 20 20 20 20 49 6e 20 62 6f 74 68 20   **     In both 
10687 6f 66 20 74 68 65 20 61 62 6f 76 65 20 63 61 73  of the above cas
10688 65 73 2c 20 74 68 65 20 56 64 62 65 2e 65 72 72  es, the Vdbe.err
10689 6f 72 41 63 74 69 6f 6e 20 76 61 72 69 61 62 6c  orAction variabl
1068a 65 20 69 73 20 0a 20 20 2a 2a 20 20 20 20 20 69  e is .  **     i
1068b 67 6e 6f 72 65 64 2e 20 49 66 20 74 68 65 20 73  gnored. If the s
1068c 71 6c 69 74 65 33 2e 61 75 74 6f 43 6f 6d 6d 69  qlite3.autoCommi
1068d 74 20 66 6c 61 67 20 69 73 20 66 61 6c 73 65 20  t flag is false 
1068e 61 6e 64 20 61 20 74 72 61 6e 73 61 63 74 69 6f  and a transactio
1068f 6e 0a 20 20 2a 2a 20 20 20 20 20 69 73 20 72 6f  n.  **     is ro
10690 6c 6c 65 64 20 62 61 63 6b 2c 20 69 74 20 77 69  lled back, it wi
10691 6c 6c 20 62 65 20 73 65 74 20 74 6f 20 74 72 75  ll be set to tru
10692 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4f 74 68  e..  **.  ** Oth
10693 65 72 20 65 72 72 6f 72 73 3a 0a 20 20 2a 2a 0a  er errors:.  **.
10694 20 20 2a 2a 20 4e 6f 20 65 72 72 6f 72 3a 0a 20    ** No error:. 
10695 20 2a 2a 0a 20 20 2a 2f 0a 0a 20 20 69 66 28 20   **.  */..  if( 
10696 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69  sqlite3MallocFai
10697 6c 65 64 28 29 20 29 7b 0a 20 20 20 20 70 2d 3e  led() ){.    p->
10698 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
10699 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e  M;.  }.  if( p->
1069a 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 49  magic!=VDBE_MAGI
1069b 43 5f 52 55 4e 20 29 7b 0a 20 20 20 20 2f 2a 20  C_RUN ){.    /* 
1069c 41 6c 72 65 61 64 79 20 68 61 6c 74 65 64 2e 20  Already halted. 
1069d 20 4e 6f 74 68 69 6e 67 20 74 6f 20 64 6f 2e 20   Nothing to do. 
1069e 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  */.    assert( p
1069f 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41  ->magic==VDBE_MA
106a0 47 49 43 5f 48 41 4c 54 20 29 3b 0a 23 69 66 6e  GIC_HALT );.#ifn
106a1 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
106a2 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20  VIRTUALTABLE.   
106a3 20 63 6c 6f 73 65 41 6c 6c 43 75 72 73 6f 72 73   closeAllCursors
106a4 28 70 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  (p);.#endif.    
106a5 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
106a6 3b 0a 20 20 7d 0a 20 20 63 6c 6f 73 65 41 6c 6c  ;.  }.  closeAll
106a7 43 75 72 73 6f 72 73 28 70 29 3b 0a 20 20 63 68  Cursors(p);.  ch
106a8 65 63 6b 41 63 74 69 76 65 56 64 62 65 43 6e 74  eckActiveVdbeCnt
106a9 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 4e 6f 20 63  (db);..  /* No c
106aa 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c 6c 62 61 63  ommit or rollbac
106ab 6b 20 6e 65 65 64 65 64 20 69 66 20 74 68 65 20  k needed if the 
106ac 70 72 6f 67 72 61 6d 20 6e 65 76 65 72 20 73 74  program never st
106ad 61 72 74 65 64 20 2a 2f 0a 20 20 69 66 28 20 70  arted */.  if( p
106ae 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20 69  ->pc>=0 ){.    i
106af 6e 74 20 6d 72 63 3b 20 20 20 2f 2a 20 50 72 69  nt mrc;   /* Pri
106b0 6d 61 72 79 20 65 72 72 6f 72 20 63 6f 64 65 20  mary error code 
106b1 66 72 6f 6d 20 70 2d 3e 72 63 20 2a 2f 0a 20 20  from p->rc */.  
106b2 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 6f    /* Check for o
106b3 6e 65 20 6f 66 20 74 68 65 20 73 70 65 63 69 61  ne of the specia
106b4 6c 20 65 72 72 6f 72 73 20 2d 20 53 51 4c 49 54  l errors - SQLIT
106b5 45 5f 4e 4f 4d 45 4d 20 6f 72 20 53 51 4c 49 54  E_NOMEM or SQLIT
106b6 45 5f 49 4f 45 52 52 20 2a 2f 0a 20 20 20 20 6d  E_IOERR */.    m
106b7 72 63 20 3d 20 70 2d 3e 72 63 20 26 20 30 78 66  rc = p->rc & 0xf
106b8 66 3b 0a 20 20 20 20 69 73 53 70 65 63 69 61 6c  f;.    isSpecial
106b9 45 72 72 6f 72 20 3d 20 28 0a 20 20 20 20 20 20  Error = (.      
106ba 20 20 28 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 4e    (mrc==SQLITE_N
106bb 4f 4d 45 4d 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c  OMEM || mrc==SQL
106bc 49 54 45 5f 49 4f 45 52 52 20 7c 7c 20 6d 72 63  ITE_IOERR || mrc
106bd 3d 3d 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55  ==SQLITE_INTERRU
106be 50 54 29 3f 31 3a 30 29 3b 0a 20 20 20 20 69 66  PT)?1:0);.    if
106bf 28 20 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72  ( isSpecialError
106c0 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69   ){.      /* Thi
106c1 73 20 6c 6f 6f 70 20 64 6f 65 73 20 73 74 61 74  s loop does stat
106c2 69 63 20 61 6e 61 6c 79 73 69 73 20 6f 66 20 74  ic analysis of t
106c3 68 65 20 71 75 65 72 79 20 74 6f 20 73 65 65 20  he query to see 
106c4 77 68 69 63 68 20 6f 66 20 74 68 65 0a 20 20 20  which of the.   
106c5 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20     ** following 
106c6 74 68 72 65 65 20 63 61 74 65 67 6f 72 69 65 73  three categories
106c7 20 69 74 20 66 61 6c 6c 73 20 69 6e 74 6f 3a 0a   it falls into:.
106c8 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
106c9 2a 20 20 20 20 20 52 65 61 64 2d 6f 6e 6c 79 0a  *     Read-only.
106ca 20 20 20 20 20 20 2a 2a 20 20 20 20 20 51 75 65        **     Que
106cb 72 79 20 77 69 74 68 20 73 74 61 74 65 6d 65 6e  ry with statemen
106cc 74 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 20  t journal.      
106cd 2a 2a 20 20 20 20 20 51 75 65 72 79 20 77 69 74  **     Query wit
106ce 68 6f 75 74 20 73 74 61 74 65 6d 65 6e 74 20 6a  hout statement j
106cf 6f 75 72 6e 61 6c 0a 20 20 20 20 20 20 2a 2a 0a  ournal.      **.
106d0 20 20 20 20 20 20 2a 2a 20 57 65 20 63 6f 75 6c        ** We coul
106d1 64 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67 20 6d  d do something m
106d2 6f 72 65 20 65 6c 65 67 61 6e 74 20 74 68 61 6e  ore elegant than
106d3 20 74 68 69 73 20 73 74 61 74 69 63 20 61 6e 61   this static ana
106d4 6c 79 73 69 73 20 28 69 2e 65 2e 0a 20 20 20 20  lysis (i.e..    
106d5 20 20 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 74    ** store the t
106d6 79 70 65 20 6f 66 20 71 75 65 72 79 20 61 73 20  ype of query as 
106d7 70 61 72 74 20 6f 66 20 74 68 65 20 63 6f 6d 70  part of the comp
106d8 6c 69 61 74 69 6f 6e 20 70 68 61 73 65 29 2c 20  liation phase), 
106d9 62 75 74 20 0a 20 20 20 20 20 20 2a 2a 20 68 61  but .      ** ha
106da 6e 64 6c 69 6e 67 20 6d 61 6c 6c 6f 63 28 29 20  ndling malloc() 
106db 6f 72 20 49 4f 20 66 61 69 6c 75 72 65 20 69 73  or IO failure is
106dc 20 61 20 66 61 69 72 6c 79 20 6f 62 73 63 75 72   a fairly obscur
106dd 65 20 65 64 67 65 20 63 61 73 65 20 73 6f 20 0a  e edge case so .
106de 20 20 20 20 20 20 2a 2a 20 74 68 69 73 20 69 73        ** this is
106df 20 70 72 6f 62 61 62 6c 79 20 65 61 73 69 65 72   probably easier
106e0 2e 20 54 6f 64 6f 3a 20 4d 69 67 68 74 20 62 65  . Todo: Might be
106e1 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79 20   an opportunity 
106e2 74 6f 20 72 65 64 75 63 65 20 0a 20 20 20 20 20  to reduce .     
106e3 20 2a 2a 20 63 6f 64 65 20 73 69 7a 65 20 61 20   ** code size a 
106e4 76 65 72 79 20 73 6d 61 6c 6c 20 61 6d 6f 75 6e  very small amoun
106e5 74 20 74 68 6f 75 67 68 2e 2e 2e 0a 20 20 20 20  t though....    
106e6 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69    */.      int i
106e7 73 52 65 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a 20  sReadOnly = 1;. 
106e8 20 20 20 20 20 69 6e 74 20 69 73 53 74 61 74 65       int isState
106e9 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  ment = 0;.      
106ea 61 73 73 65 72 74 28 70 2d 3e 61 4f 70 20 7c 7c  assert(p->aOp ||
106eb 20 70 2d 3e 6e 4f 70 3d 3d 30 29 3b 0a 20 20 20   p->nOp==0);.   
106ec 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d     for(i=0; i<p-
106ed 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 20 0a 20 20 20  >nOp; i++){ .   
106ee 20 20 20 20 20 73 77 69 74 63 68 28 20 70 2d 3e       switch( p->
106ef 61 4f 70 5b 69 5d 2e 6f 70 63 6f 64 65 20 29 7b  aOp[i].opcode ){
106f0 0a 20 20 20 20 20 20 20 20 20 20 63 61 73 65 20  .          case 
106f1 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 3a 0a  OP_Transaction:.
106f2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
106f3 68 69 73 20 69 73 20 61 20 62 69 74 20 73 74 72  his is a bit str
106f4 61 6e 67 65 2e 20 49 66 20 77 65 20 68 69 74 20  ange. If we hit 
106f5 61 20 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 49 4f  a malloc() or IO
106f6 20 65 72 72 6f 72 20 61 6e 64 0a 20 20 20 20 20   error and.     
106f7 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 73 74         ** the st
106f8 61 74 65 6d 65 6e 74 20 64 69 64 20 6e 6f 74 20  atement did not 
106f9 6f 70 65 6e 20 61 20 73 74 61 74 65 6d 65 6e 74  open a statement
106fa 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 77 65   transaction, we
106fb 20 77 69 6c 6c 0a 20 20 20 20 20 20 20 20 20 20   will.          
106fc 20 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 61 6e    ** rollback an
106fd 79 20 61 63 74 69 76 65 20 74 72 61 6e 73 61 63  y active transac
106fe 74 69 6f 6e 20 61 6e 64 20 61 62 6f 72 74 20 61  tion and abort a
106ff 6c 6c 20 6f 74 68 65 72 20 61 63 74 69 76 65 0a  ll other active.
10700 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73              ** s
10701 74 61 74 65 6d 65 6e 74 73 2e 20 4f 72 2c 20 69  tatements. Or, i
10702 66 20 74 68 69 73 20 69 73 20 61 6e 20 53 51 4c  f this is an SQL
10703 49 54 45 5f 49 4e 54 45 52 52 55 50 54 20 65 72  ITE_INTERRUPT er
10704 72 6f 72 2c 20 77 65 0a 20 20 20 20 20 20 20 20  ror, we.        
10705 20 20 20 20 2a 2a 20 77 69 6c 6c 20 6f 6e 6c 79      ** will only
10706 20 72 6f 6c 6c 62 61 63 6b 20 69 66 20 74 68 65   rollback if the
10707 20 69 6e 74 65 72 72 75 70 74 65 64 20 73 74 61   interrupted sta
10708 74 65 6d 65 6e 74 20 77 61 73 20 61 20 77 72 69  tement was a wri
10709 74 65 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20  te..            
1070a 2a 2a 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  **.            *
1070b 2a 20 49 74 20 63 6f 75 6c 64 20 62 65 20 61 72  * It could be ar
1070c 67 75 65 64 20 74 68 61 74 20 72 65 61 64 2d 6f  gued that read-o
1070d 6e 6c 79 20 73 74 61 74 65 6d 65 6e 74 73 20 73  nly statements s
1070e 68 6f 75 6c 64 20 6e 65 76 65 72 0a 20 20 20 20  hould never.    
1070f 20 20 20 20 20 20 20 20 2a 2a 20 72 6f 6c 6c 62          ** rollb
10710 61 63 6b 20 61 6e 79 74 68 69 6e 67 2e 20 42 75  ack anything. Bu
10711 74 20 63 61 72 65 66 75 6c 20 61 6e 61 6c 79 73  t careful analys
10712 69 73 20 69 73 20 72 65 71 75 69 72 65 64 20 62  is is required b
10713 65 66 6f 72 65 0a 20 20 20 20 20 20 20 20 20 20  efore.          
10714 20 20 2a 2a 20 6d 61 6b 69 6e 67 20 74 68 69 73    ** making this
10715 20 63 68 61 6e 67 65 0a 20 20 20 20 20 20 20 20   change.        
10716 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20      */.         
10717 20 20 20 69 66 28 20 70 2d 3e 61 4f 70 5b 69 5d     if( p->aOp[i]
10718 2e 70 32 20 7c 7c 20 6d 72 63 21 3d 53 51 4c 49  .p2 || mrc!=SQLI
10719 54 45 5f 49 4e 54 45 52 52 55 50 54 20 29 7b 0a  TE_INTERRUPT ){.
1071a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73                is
1071b 52 65 61 64 4f 6e 6c 79 20 3d 20 30 3b 0a 20 20  ReadOnly = 0;.  
1071c 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
1071d 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
1071e 20 20 20 20 20 20 20 20 20 63 61 73 65 20 4f 50           case OP
1071f 5f 53 74 61 74 65 6d 65 6e 74 3a 0a 20 20 20 20  _Statement:.    
10720 20 20 20 20 20 20 20 20 69 73 53 74 61 74 65 6d          isStatem
10721 65 6e 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  ent = 1;.       
10722 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
10723 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
10724 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65  .      /* If the
10725 20 71 75 65 72 79 20 77 61 73 20 72 65 61 64 2d   query was read-
10726 6f 6e 6c 79 2c 20 77 65 20 6e 65 65 64 20 64 6f  only, we need do
10727 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 74 20   no rollback at 
10728 61 6c 6c 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a  all. Otherwise,.
10729 20 20 20 20 20 20 2a 2a 20 70 72 6f 63 65 65 64        ** proceed
1072a 20 77 69 74 68 20 74 68 65 20 73 70 65 63 69 61   with the specia
1072b 6c 20 68 61 6e 64 6c 69 6e 67 2e 0a 20 20 20 20  l handling..    
1072c 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 21    */.      if( !
1072d 69 73 52 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20  isReadOnly ){.  
1072e 20 20 20 20 20 20 69 66 28 20 70 2d 3e 72 63 3d        if( p->rc=
1072f 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c  =SQLITE_IOERR_BL
10730 4f 43 4b 45 44 20 26 26 20 69 73 53 74 61 74 65  OCKED && isState
10731 6d 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20  ment ){.        
10732 20 20 78 46 75 6e 63 20 3d 20 73 71 6c 69 74 65    xFunc = sqlite
10733 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 53 74  3BtreeRollbackSt
10734 6d 74 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d  mt;.          p-
10735 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53  >rc = SQLITE_BUS
10736 59 3b 0a 20 20 20 20 20 20 20 20 7d 20 65 6c 73  Y;.        } els
10737 65 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c  e if( p->rc==SQL
10738 49 54 45 5f 4e 4f 4d 45 4d 20 26 26 20 69 73 53  ITE_NOMEM && isS
10739 74 61 74 65 6d 65 6e 74 20 29 7b 0a 20 20 20 20  tatement ){.    
1073a 20 20 20 20 20 20 78 46 75 6e 63 20 3d 20 73 71        xFunc = sq
1073b 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61  lite3BtreeRollba
1073c 63 6b 53 74 6d 74 3b 0a 20 20 20 20 20 20 20 20  ckStmt;.        
1073d 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
1073e 20 2f 2a 20 57 65 20 61 72 65 20 66 6f 72 63 65   /* We are force
1073f 64 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 74  d to roll back t
10740 68 65 20 61 63 74 69 76 65 20 74 72 61 6e 73 61  he active transa
10741 63 74 69 6f 6e 2e 20 42 65 66 6f 72 65 20 64 6f  ction. Before do
10742 69 6e 67 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  ing.          **
10743 20 73 6f 2c 20 61 62 6f 72 74 20 61 6e 79 20 6f   so, abort any o
10744 74 68 65 72 20 73 74 61 74 65 6d 65 6e 74 73 20  ther statements 
10745 74 68 69 73 20 68 61 6e 64 6c 65 20 63 75 72 72  this handle curr
10746 65 6e 74 6c 79 20 68 61 73 20 61 63 74 69 76 65  ently has active
10747 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  ..          */. 
10748 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
10749 41 62 6f 72 74 4f 74 68 65 72 41 63 74 69 76 65  AbortOtherActive
1074a 56 64 62 65 73 28 64 62 2c 20 70 29 3b 0a 20 20  Vdbes(db, p);.  
1074b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52          sqlite3R
1074c 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a  ollbackAll(db);.
1074d 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75            db->au
1074e 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20  toCommit = 1;.  
1074f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
10750 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20      }.  .    /* 
10751 49 66 20 74 68 65 20 61 75 74 6f 2d 63 6f 6d 6d  If the auto-comm
10752 69 74 20 66 6c 61 67 20 69 73 20 73 65 74 20 61  it flag is set a
10753 6e 64 20 74 68 69 73 20 69 73 20 74 68 65 20 6f  nd this is the o
10754 6e 6c 79 20 61 63 74 69 76 65 20 76 64 62 65 2c  nly active vdbe,
10755 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 77 65 20   then.    ** we 
10756 64 6f 20 65 69 74 68 65 72 20 61 20 63 6f 6d 6d  do either a comm
10757 69 74 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6f  it or rollback o
10758 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72  f the current tr
10759 61 6e 73 61 63 74 69 6f 6e 2e 20 0a 20 20 20 20  ansaction. .    
1075a 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 3a 20  **.    ** Note: 
1075b 54 68 69 73 20 62 6c 6f 63 6b 20 61 6c 73 6f 20  This block also 
1075c 72 75 6e 73 20 69 66 20 6f 6e 65 20 6f 66 20 74  runs if one of t
1075d 68 65 20 73 70 65 63 69 61 6c 20 65 72 72 6f 72  he special error
1075e 73 20 68 61 6e 64 6c 65 64 20 0a 20 20 20 20 2a  s handled .    *
1075f 2a 20 61 62 6f 76 65 20 68 61 73 20 6f 63 63 75  * above has occu
10760 72 65 64 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20  red. .    */.   
10761 20 69 66 28 20 64 62 2d 3e 61 75 74 6f 43 6f 6d   if( db->autoCom
10762 6d 69 74 20 26 26 20 64 62 2d 3e 61 63 74 69 76  mit && db->activ
10763 65 56 64 62 65 43 6e 74 3d 3d 31 20 29 7b 0a 20  eVdbeCnt==1 ){. 
10764 20 20 20 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d       if( p->rc==
10765 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 28 70 2d  SQLITE_OK || (p-
10766 3e 65 72 72 6f 72 41 63 74 69 6f 6e 3d 3d 4f 45  >errorAction==OE
10767 5f 46 61 69 6c 20 26 26 20 21 69 73 53 70 65 63  _Fail && !isSpec
10768 69 61 6c 45 72 72 6f 72 29 20 29 7b 0a 09 2f 2a  ialError) ){../*
10769 20 54 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74   The auto-commit
1076a 20 66 6c 61 67 20 69 73 20 74 72 75 65 2c 20 61   flag is true, a
1076b 6e 64 20 74 68 65 20 76 64 62 65 20 70 72 6f 67  nd the vdbe prog
1076c 72 61 6d 20 77 61 73 20 0a 20 20 20 20 20 20 20  ram was .       
1076d 20 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 6f   ** successful o
1076e 72 20 68 69 74 20 61 6e 20 27 4f 52 20 46 41 49  r hit an 'OR FAI
1076f 4c 27 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20 54  L' constraint. T
10770 68 69 73 20 6d 65 61 6e 73 20 61 20 63 6f 6d 6d  his means a comm
10771 69 74 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 69  it .        ** i
10772 73 20 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20  s required..    
10773 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69      */.        i
10774 6e 74 20 72 63 20 3d 20 76 64 62 65 43 6f 6d 6d  nt rc = vdbeComm
10775 69 74 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20  it(db);.        
10776 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42  if( rc==SQLITE_B
10777 55 53 59 20 29 7b 0a 20 20 20 20 20 20 20 20 20  USY ){.         
10778 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42   return SQLITE_B
10779 55 53 59 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  USY;.        }el
1077a 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  se if( rc!=SQLIT
1077b 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
1077c 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20    p->rc = rc;.  
1077d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52          sqlite3R
1077e 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a  ollbackAll(db);.
1077f 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
10780 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
10781 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43 68  CommitInternalCh
10782 61 6e 67 65 73 28 64 62 29 3b 0a 20 20 20 20 20  anges(db);.     
10783 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
10784 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
10785 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29  3RollbackAll(db)
10786 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
10787 6c 73 65 20 69 66 28 20 21 78 46 75 6e 63 20 29  lse if( !xFunc )
10788 7b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 72  {.      if( p->r
10789 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c==SQLITE_OK || 
1078a 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e 3d 3d  p->errorAction==
1078b 4f 45 5f 46 61 69 6c 20 29 7b 0a 20 20 20 20 20  OE_Fail ){.     
1078c 20 20 20 69 66 28 20 70 2d 3e 6f 70 65 6e 65 64     if( p->opened
1078d 53 74 61 74 65 6d 65 6e 74 20 29 7b 0a 20 20 20  Statement ){.   
1078e 20 20 20 20 20 20 20 78 46 75 6e 63 20 3d 20 73         xFunc = s
1078f 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69  qlite3BtreeCommi
10790 74 53 74 6d 74 3b 0a 20 20 20 20 20 20 20 20 7d  tStmt;.        }
10791 20 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66   .      }else if
10792 28 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e  ( p->errorAction
10793 3d 3d 4f 45 5f 41 62 6f 72 74 20 29 7b 0a 20 20  ==OE_Abort ){.  
10794 20 20 20 20 20 20 78 46 75 6e 63 20 3d 20 73 71        xFunc = sq
10795 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61  lite3BtreeRollba
10796 63 6b 53 74 6d 74 3b 0a 20 20 20 20 20 20 7d 65  ckStmt;.      }e
10797 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  lse{.        sql
10798 69 74 65 33 41 62 6f 72 74 4f 74 68 65 72 41 63  ite3AbortOtherAc
10799 74 69 76 65 56 64 62 65 73 28 64 62 2c 20 70 29  tiveVdbes(db, p)
1079a 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
1079b 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29  3RollbackAll(db)
1079c 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75  ;.        db->au
1079d 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20  toCommit = 1;.  
1079e 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20      }.    }.  . 
1079f 20 20 20 2f 2a 20 49 66 20 78 46 75 6e 63 20 69     /* If xFunc i
107a0 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e  s not NULL, then
107a1 20 69 74 20 69 73 20 6f 6e 65 20 6f 66 20 73 71   it is one of sq
107a2 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61  lite3BtreeRollba
107a3 63 6b 53 74 6d 74 20 6f 72 0a 20 20 20 20 2a 2a  ckStmt or.    **
107a4 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d   sqlite3BtreeCom
107a5 6d 69 74 53 74 6d 74 2e 20 43 61 6c 6c 20 69 74  mitStmt. Call it
107a6 20 6f 6e 63 65 20 6f 6e 20 65 61 63 68 20 62 61   once on each ba
107a7 63 6b 65 6e 64 2e 20 49 66 20 61 6e 20 65 72 72  ckend. If an err
107a8 6f 72 20 6f 63 63 75 72 73 0a 20 20 20 20 2a 2a  or occurs.    **
107a9 20 61 6e 64 20 74 68 65 20 72 65 74 75 72 6e 20   and the return 
107aa 63 6f 64 65 20 69 73 20 73 74 69 6c 6c 20 53 51  code is still SQ
107ab 4c 49 54 45 5f 4f 4b 2c 20 73 65 74 20 74 68 65  LITE_OK, set the
107ac 20 72 65 74 75 72 6e 20 63 6f 64 65 20 74 6f 20   return code to 
107ad 74 68 65 20 6e 65 77 0a 20 20 20 20 2a 2a 20 65  the new.    ** e
107ae 72 72 6f 72 20 76 61 6c 75 65 2e 0a 20 20 20 20  rror value..    
107af 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 21 78  */.    assert(!x
107b0 46 75 6e 63 20 7c 7c 0a 20 20 20 20 20 20 78 46  Func ||.      xF
107b1 75 6e 63 3d 3d 73 71 6c 69 74 65 33 42 74 72 65  unc==sqlite3Btre
107b2 65 43 6f 6d 6d 69 74 53 74 6d 74 20 7c 7c 0a 20  eCommitStmt ||. 
107b3 20 20 20 20 20 78 46 75 6e 63 3d 3d 73 71 6c 69       xFunc==sqli
107b4 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b  te3BtreeRollback
107b5 53 74 6d 74 0a 20 20 20 20 29 3b 0a 20 20 20 20  Stmt.    );.    
107b6 66 6f 72 28 69 3d 30 3b 20 78 46 75 6e 63 20 26  for(i=0; xFunc &
107b7 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  & i<db->nDb; i++
107b8 29 7b 20 0a 20 20 20 20 20 20 69 6e 74 20 72 63  ){ .      int rc
107b9 3b 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70  ;.      Btree *p
107ba 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e  Bt = db->aDb[i].
107bb 70 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70  pBt;.      if( p
107bc 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  Bt ){.        rc
107bd 20 3d 20 78 46 75 6e 63 28 70 42 74 29 3b 0a 20   = xFunc(pBt);. 
107be 20 20 20 20 20 20 20 69 66 28 20 72 63 20 26 26         if( rc &&
107bf 20 28 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f   (p->rc==SQLITE_
107c0 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c  OK || p->rc==SQL
107c1 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 29 20  ITE_CONSTRAINT) 
107c2 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e  ){.          p->
107c3 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 20 20 20  rc = rc;.       
107c4 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72     sqlite3SetStr
107c5 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c  ing(&p->zErrMsg,
107c6 20 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20   0);.        }. 
107c7 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a       }.    }.  .
107c8 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 77      /* If this w
107c9 61 73 20 61 6e 20 49 4e 53 45 52 54 2c 20 55 50  as an INSERT, UP
107ca 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 20 61  DATE or DELETE a
107cb 6e 64 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  nd the statement
107cc 20 77 61 73 20 63 6f 6d 6d 69 74 74 65 64 2c 20   was committed, 
107cd 0a 20 20 20 20 2a 2a 20 73 65 74 20 74 68 65 20  .    ** set the 
107ce 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 2e 20  change counter. 
107cf 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
107d0 70 2d 3e 63 68 61 6e 67 65 43 6e 74 4f 6e 20 26  p->changeCntOn &
107d1 26 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20  & p->pc>=0 ){.  
107d2 20 20 20 20 69 66 28 20 21 78 46 75 6e 63 20 7c      if( !xFunc |
107d3 7c 20 78 46 75 6e 63 3d 3d 73 71 6c 69 74 65 33  | xFunc==sqlite3
107d4 42 74 72 65 65 43 6f 6d 6d 69 74 53 74 6d 74 20  BtreeCommitStmt 
107d5 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
107d6 65 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73  e3VdbeSetChanges
107d7 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29  (db, p->nChange)
107d8 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
107d9 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
107da 62 65 53 65 74 43 68 61 6e 67 65 73 28 64 62 2c  beSetChanges(db,
107db 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   0);.      }.   
107dc 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20     p->nChange = 
107dd 30 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20  0;.    }.  .    
107de 2f 2a 20 52 6f 6c 6c 62 61 63 6b 20 6f 72 20 63  /* Rollback or c
107df 6f 6d 6d 69 74 20 61 6e 79 20 73 63 68 65 6d 61  ommit any schema
107e0 20 63 68 61 6e 67 65 73 20 74 68 61 74 20 6f 63   changes that oc
107e1 63 75 72 72 65 64 2e 20 2a 2f 0a 20 20 20 20 69  curred. */.    i
107e2 66 28 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45  f( p->rc!=SQLITE
107e3 5f 4f 4b 20 26 26 20 64 62 2d 3e 66 6c 61 67 73  _OK && db->flags
107e4 26 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68  &SQLITE_InternCh
107e5 61 6e 67 65 73 20 29 7b 0a 20 20 20 20 20 20 73  anges ){.      s
107e6 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72  qlite3ResetInter
107e7 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29  nalSchema(db, 0)
107e8 3b 0a 20 20 20 20 20 20 64 62 2d 3e 66 6c 61 67  ;.      db->flag
107e9 73 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 7c  s = (db->flags |
107ea 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68   SQLITE_InternCh
107eb 61 6e 67 65 73 29 3b 0a 20 20 20 20 7d 0a 20 20  anges);.    }.  
107ec 7d 0a 0a 20 20 2f 2a 20 57 65 20 68 61 76 65 20  }..  /* We have 
107ed 73 75 63 63 65 73 73 66 75 6c 6c 79 20 68 61 6c  successfully hal
107ee 74 65 64 20 61 6e 64 20 63 6c 6f 73 65 64 20 74  ted and closed t
107ef 68 65 20 56 4d 2e 20 20 52 65 63 6f 72 64 20 74  he VM.  Record t
107f0 68 69 73 20 66 61 63 74 2e 20 2a 2f 0a 20 20 69  his fact. */.  i
107f1 66 28 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20  f( p->pc>=0 ){. 
107f2 20 20 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62     db->activeVdb
107f3 65 43 6e 74 2d 2d 3b 0a 20 20 7d 0a 20 20 70 2d  eCnt--;.  }.  p-
107f4 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41  >magic = VDBE_MA
107f5 47 49 43 5f 48 41 4c 54 3b 0a 20 20 63 68 65 63  GIC_HALT;.  chec
107f6 6b 41 63 74 69 76 65 56 64 62 65 43 6e 74 28 64  kActiveVdbeCnt(d
107f7 62 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51  b);..  return SQ
107f8 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
107f9 2a 20 45 61 63 68 20 56 44 42 45 20 68 6f 6c 64  * Each VDBE hold
107fa 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20  s the result of 
107fb 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
107fc 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 63  sqlite3_step() c
107fd 61 6c 6c 0a 2a 2a 20 69 6e 20 70 2d 3e 72 63 2e  all.** in p->rc.
107fe 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73    This routine s
107ff 65 74 73 20 74 68 61 74 20 72 65 73 75 6c 74 20  ets that result 
10800 62 61 63 6b 20 74 6f 20 53 51 4c 49 54 45 5f 4f  back to SQLITE_O
10801 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  K..*/.SQLITE_PRI
10802 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
10803 33 56 64 62 65 52 65 73 65 74 53 74 65 70 52 65  3VdbeResetStepRe
10804 73 75 6c 74 28 56 64 62 65 20 2a 70 29 7b 0a 20  sult(Vdbe *p){. 
10805 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f   p->rc = SQLITE_
10806 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65  OK;.}../*.** Cle
10807 61 6e 20 75 70 20 61 20 56 44 42 45 20 61 66 74  an up a VDBE aft
10808 65 72 20 65 78 65 63 75 74 69 6f 6e 20 62 75 74  er execution but
10809 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 74   do not delete t
1080a 68 65 20 56 44 42 45 20 6a 75 73 74 20 79 65 74  he VDBE just yet
1080b 2e 0a 2a 2a 20 57 72 69 74 65 20 61 6e 79 20 65  ..** Write any e
1080c 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 69 6e  rror messages in
1080d 74 6f 20 2a 70 7a 45 72 72 4d 73 67 2e 20 20 52  to *pzErrMsg.  R
1080e 65 74 75 72 6e 20 74 68 65 20 72 65 73 75 6c 74  eturn the result
1080f 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 66 74   code..**.** Aft
10810 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  er this routine 
10811 69 73 20 72 75 6e 2c 20 74 68 65 20 56 44 42 45  is run, the VDBE
10812 20 73 68 6f 75 6c 64 20 62 65 20 72 65 61 64 79   should be ready
10813 20 74 6f 20 62 65 20 65 78 65 63 75 74 65 64 0a   to be executed.
10814 2a 2a 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a 20  ** again..**.** 
10815 54 6f 20 6c 6f 6f 6b 20 61 74 20 69 74 20 61 6e  To look at it an
10816 6f 74 68 65 72 20 77 61 79 2c 20 74 68 69 73 20  other way, this 
10817 72 6f 75 74 69 6e 65 20 72 65 73 65 74 73 20 74  routine resets t
10818 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 0a  he state of the.
10819 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69  ** virtual machi
1081a 6e 65 20 66 72 6f 6d 20 56 44 42 45 5f 4d 41 47  ne from VDBE_MAG
1081b 49 43 5f 52 55 4e 20 6f 72 20 56 44 42 45 5f 4d  IC_RUN or VDBE_M
1081c 41 47 49 43 5f 48 41 4c 54 20 62 61 63 6b 20 74  AGIC_HALT back t
1081d 6f 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 49 43 5f  o.** VDBE_MAGIC_
1081e 49 4e 49 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  INIT..*/.SQLITE_
1081f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
10820 74 65 33 56 64 62 65 52 65 73 65 74 28 56 64 62  te3VdbeReset(Vdb
10821 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33  e *p){.  sqlite3
10822 20 2a 64 62 3b 0a 20 20 64 62 20 3d 20 70 2d 3e   *db;.  db = p->
10823 64 62 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65  db;..  /* If the
10824 20 56 4d 20 64 69 64 20 6e 6f 74 20 72 75 6e 20   VM did not run 
10825 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 72  to completion or
10826 20 69 66 20 69 74 20 65 6e 63 6f 75 6e 74 65 72   if it encounter
10827 65 64 20 61 6e 0a 20 20 2a 2a 20 65 72 72 6f 72  ed an.  ** error
10828 2c 20 74 68 65 6e 20 69 74 20 6d 69 67 68 74 20  , then it might 
10829 6e 6f 74 20 68 61 76 65 20 62 65 65 6e 20 68 61  not have been ha
1082a 6c 74 65 64 20 70 72 6f 70 65 72 6c 79 2e 20 20  lted properly.  
1082b 53 6f 20 68 61 6c 74 0a 20 20 2a 2a 20 69 74 20  So halt.  ** it 
1082c 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  now..  */.  sqli
1082d 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b  te3SafetyOn(db);
1082e 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 48 61  .  sqlite3VdbeHa
1082f 6c 74 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33  lt(p);.  sqlite3
10830 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 0a  SafetyOff(db);..
10831 20 20 2f 2a 20 49 66 20 74 68 65 20 56 44 42 45    /* If the VDBE
10832 20 68 61 73 20 62 65 20 72 75 6e 20 65 76 65 6e   has be run even
10833 20 70 61 72 74 69 61 6c 6c 79 2c 20 74 68 65 6e   partially, then
10834 20 74 72 61 6e 73 66 65 72 20 74 68 65 20 65 72   transfer the er
10835 72 6f 72 20 63 6f 64 65 0a 20 20 2a 2a 20 61 6e  ror code.  ** an
10836 64 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  d error message 
10837 66 72 6f 6d 20 74 68 65 20 56 44 42 45 20 69 6e  from the VDBE in
10838 74 6f 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  to the main data
10839 62 61 73 65 20 73 74 72 75 63 74 75 72 65 2e 20  base structure. 
1083a 20 42 75 74 0a 20 20 2a 2a 20 69 66 20 74 68 65   But.  ** if the
1083b 20 56 44 42 45 20 68 61 73 20 6a 75 73 74 20 62   VDBE has just b
1083c 65 65 6e 20 73 65 74 20 74 6f 20 72 75 6e 20 62  een set to run b
1083d 75 74 20 68 61 73 20 6e 6f 74 20 61 63 74 75 61  ut has not actua
1083e 6c 6c 79 20 65 78 65 63 75 74 65 64 20 61 6e 79  lly executed any
1083f 0a 20 20 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f  .  ** instructio
10840 6e 73 20 79 65 74 2c 20 6c 65 61 76 65 20 74 68  ns yet, leave th
10841 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
10842 65 72 72 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f  error informatio
10843 6e 20 75 6e 63 68 61 6e 67 65 64 2e 0a 20 20 2a  n unchanged..  *
10844 2f 0a 20 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30  /.  if( p->pc>=0
10845 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 7a   ){.    if( p->z
10846 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20  ErrMsg ){.      
10847 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53  sqlite3ValueSetS
10848 74 72 28 64 62 2d 3e 70 45 72 72 2c 20 2d 31 2c  tr(db->pErr, -1,
10849 20 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 53 51 4c   p->zErrMsg, SQL
1084a 49 54 45 5f 55 54 46 38 2c 20 73 71 6c 69 74 65  ITE_UTF8, sqlite
1084b 33 46 72 65 65 58 29 3b 0a 20 20 20 20 20 20 64  3FreeX);.      d
1084c 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 70 2d 3e  b->errCode = p->
1084d 72 63 3b 0a 20 20 20 20 20 20 70 2d 3e 7a 45 72  rc;.      p->zEr
1084e 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 7d 65  rMsg = 0;.    }e
1084f 6c 73 65 20 69 66 28 20 70 2d 3e 72 63 20 29 7b  lse if( p->rc ){
10850 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
10851 72 6f 72 28 64 62 2c 20 70 2d 3e 72 63 2c 20 30  ror(db, p->rc, 0
10852 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
10853 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
10854 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20  (db, SQLITE_OK, 
10855 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  0);.    }.  }els
10856 65 20 69 66 28 20 70 2d 3e 72 63 20 26 26 20 70  e if( p->rc && p
10857 2d 3e 65 78 70 69 72 65 64 20 29 7b 0a 20 20 20  ->expired ){.   
10858 20 2f 2a 20 54 68 65 20 65 78 70 69 72 65 64 20   /* The expired 
10859 66 6c 61 67 20 77 61 73 20 73 65 74 20 6f 6e 20  flag was set on 
1085a 74 68 65 20 56 44 42 45 20 62 65 66 6f 72 65 20  the VDBE before 
1085b 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c 0a 20  the first call. 
1085c 20 20 20 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33     ** to sqlite3
1085d 5f 73 74 65 70 28 29 2e 20 46 6f 72 20 63 6f 6e  _step(). For con
1085e 73 69 73 74 65 6e 63 79 20 28 73 69 6e 63 65 20  sistency (since 
1085f 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 77  sqlite3_step() w
10860 61 73 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 65 64  as.    ** called
10861 29 2c 20 73 65 74 20 74 68 65 20 64 61 74 61 62  ), set the datab
10862 61 73 65 20 65 72 72 6f 72 20 69 6e 20 74 68 69  ase error in thi
10863 73 20 63 61 73 65 20 61 73 20 77 65 6c 6c 2e 0a  s case as well..
10864 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74      */.    sqlit
10865 65 33 45 72 72 6f 72 28 64 62 2c 20 70 2d 3e 72  e3Error(db, p->r
10866 63 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  c, 0);.  }..  /*
10867 20 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d   Reclaim all mem
10868 6f 72 79 20 75 73 65 64 20 62 79 20 74 68 65 20  ory used by the 
10869 56 44 42 45 0a 20 20 2a 2f 0a 20 20 43 6c 65 61  VDBE.  */.  Clea
1086a 6e 75 70 28 70 29 3b 0a 0a 20 20 2f 2a 20 53 61  nup(p);..  /* Sa
1086b 76 65 20 70 72 6f 66 69 6c 69 6e 67 20 69 6e 66  ve profiling inf
1086c 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68  ormation from th
1086d 69 73 20 56 44 42 45 20 72 75 6e 2e 0a 20 20 2a  is VDBE run..  *
1086e 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70  /.  assert( p->p
1086f 54 6f 73 3c 26 70 2d 3e 61 53 74 61 63 6b 5b 70  Tos<&p->aStack[p
10870 2d 3e 70 63 3c 30 3f 30 3a 70 2d 3e 70 63 5d 20  ->pc<0?0:p->pc] 
10871 7c 7c 20 21 70 2d 3e 61 53 74 61 63 6b 20 29 3b  || !p->aStack );
10872 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f  .#ifdef VDBE_PRO
10873 46 49 4c 45 0a 20 20 7b 0a 20 20 20 20 46 49 4c  FILE.  {.    FIL
10874 45 20 2a 6f 75 74 20 3d 20 66 6f 70 65 6e 28 22  E *out = fopen("
10875 76 64 62 65 5f 70 72 6f 66 69 6c 65 2e 6f 75 74  vdbe_profile.out
10876 22 2c 20 22 61 22 29 3b 0a 20 20 20 20 69 66 28  ", "a");.    if(
10877 20 6f 75 74 20 29 7b 0a 20 20 20 20 20 20 69 6e   out ){.      in
10878 74 20 69 3b 0a 20 20 20 20 20 20 66 70 72 69 6e  t i;.      fprin
10879 74 66 28 6f 75 74 2c 20 22 2d 2d 2d 2d 20 22 29  tf(out, "---- ")
1087a 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b  ;.      for(i=0;
1087b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b   i<p->nOp; i++){
1087c 0a 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66  .        fprintf
1087d 28 6f 75 74 2c 20 22 25 30 32 78 22 2c 20 70 2d  (out, "%02x", p-
1087e 3e 61 4f 70 5b 69 5d 2e 6f 70 63 6f 64 65 29 3b  >aOp[i].opcode);
1087f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66  .      }.      f
10880 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22  printf(out, "\n"
10881 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  );.      for(i=0
10882 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29  ; i<p->nOp; i++)
10883 7b 0a 20 20 20 20 20 20 20 20 66 70 72 69 6e 74  {.        fprint
10884 66 28 6f 75 74 2c 20 22 25 36 64 20 25 31 30 6c  f(out, "%6d %10l
10885 6c 64 20 25 38 6c 6c 64 20 22 2c 0a 20 20 20 20  ld %8lld ",.    
10886 20 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d         p->aOp[i]
10887 2e 63 6e 74 2c 0a 20 20 20 20 20 20 20 20 20 20  .cnt,.          
10888 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65   p->aOp[i].cycle
10889 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d  s,.           p-
1088a 3e 61 4f 70 5b 69 5d 2e 63 6e 74 3e 30 20 3f 20  >aOp[i].cnt>0 ? 
1088b 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73  p->aOp[i].cycles
1088c 2f 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e 74 20 3a  /p->aOp[i].cnt :
1088d 20 30 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20   0.        );.  
1088e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1088f 65 50 72 69 6e 74 4f 70 28 6f 75 74 2c 20 69 2c  ePrintOp(out, i,
10890 20 26 70 2d 3e 61 4f 70 5b 69 5d 29 3b 0a 20 20   &p->aOp[i]);.  
10891 20 20 20 20 7d 0a 20 20 20 20 20 20 66 63 6c 6f      }.      fclo
10892 73 65 28 6f 75 74 29 3b 0a 20 20 20 20 7d 0a 20  se(out);.    }. 
10893 20 7d 0a 23 65 6e 64 69 66 0a 20 20 70 2d 3e 6d   }.#endif.  p->m
10894 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49  agic = VDBE_MAGI
10895 43 5f 49 4e 49 54 3b 0a 20 20 70 2d 3e 61 62 6f  C_INIT;.  p->abo
10896 72 74 65 64 20 3d 20 30 3b 0a 20 20 69 66 28 20  rted = 0;.  if( 
10897 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43  p->rc==SQLITE_SC
10898 48 45 4d 41 20 29 7b 0a 20 20 20 20 73 71 6c 69  HEMA ){.    sqli
10899 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c  te3ResetInternal
1089a 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20  Schema(db, 0);. 
1089b 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 72   }.  return p->r
1089c 63 20 26 20 64 62 2d 3e 65 72 72 4d 61 73 6b 3b  c & db->errMask;
1089d 0a 7d 0a 20 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 6e  .}. ./*.** Clean
1089e 20 75 70 20 61 6e 64 20 64 65 6c 65 74 65 20 61   up and delete a
1089f 20 56 44 42 45 20 61 66 74 65 72 20 65 78 65 63   VDBE after exec
108a0 75 74 69 6f 6e 2e 20 20 52 65 74 75 72 6e 20 61  ution.  Return a
108a1 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20  n integer which 
108a2 69 73 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74  is.** the result
108a3 20 63 6f 64 65 2e 20 20 57 72 69 74 65 20 61 6e   code.  Write an
108a4 79 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  y error message 
108a5 74 65 78 74 20 69 6e 74 6f 20 2a 70 7a 45 72 72  text into *pzErr
108a6 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  Msg..*/.SQLITE_P
108a7 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
108a8 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 56  e3VdbeFinalize(V
108a9 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72  dbe *p){.  int r
108aa 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
108ab 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56   if( p->magic==V
108ac 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 7c 7c  DBE_MAGIC_RUN ||
108ad 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f   p->magic==VDBE_
108ae 4d 41 47 49 43 5f 48 41 4c 54 20 29 7b 0a 20 20  MAGIC_HALT ){.  
108af 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
108b0 62 65 52 65 73 65 74 28 70 29 3b 0a 20 20 20 20  beReset(p);.    
108b1 61 73 73 65 72 74 28 20 28 72 63 20 26 20 70 2d  assert( (rc & p-
108b2 3e 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3d 3d 72  >db->errMask)==r
108b3 63 20 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  c );.  }else if(
108b4 20 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f   p->magic!=VDBE_
108b5 4d 41 47 49 43 5f 49 4e 49 54 20 29 7b 0a 20 20  MAGIC_INIT ){.  
108b6 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
108b7 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 73 71  MISUSE;.  }.  sq
108b8 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 28  lite3VdbeDelete(
108b9 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  p);.  return rc;
108ba 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 74  .}../*.** Call t
108bb 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f  he destructor fo
108bc 72 20 65 61 63 68 20 61 75 78 64 61 74 61 20 65  r each auxdata e
108bd 6e 74 72 79 20 69 6e 20 70 56 64 62 65 46 75 6e  ntry in pVdbeFun
108be 63 20 66 6f 72 20 77 68 69 63 68 0a 2a 2a 20 74  c for which.** t
108bf 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
108c0 20 62 69 74 20 69 6e 20 6d 61 73 6b 20 69 73 20   bit in mask is 
108c1 63 6c 65 61 72 2e 20 20 41 75 78 64 61 74 61 20  clear.  Auxdata 
108c2 65 6e 74 72 69 65 73 20 62 65 79 6f 6e 64 20 33  entries beyond 3
108c3 31 0a 2a 2a 20 61 72 65 20 61 6c 77 61 79 73 20  1.** are always 
108c4 64 65 73 74 72 6f 79 65 64 2e 20 20 54 6f 20 64  destroyed.  To d
108c5 65 73 74 72 6f 79 20 61 6c 6c 20 61 75 78 64 61  estroy all auxda
108c6 74 61 20 65 6e 74 72 69 65 73 2c 20 63 61 6c 6c  ta entries, call
108c7 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65   this.** routine
108c8 20 77 69 74 68 20 6d 61 73 6b 3d 3d 30 2e 0a 2a   with mask==0..*
108c9 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
108ca 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
108cb 65 44 65 6c 65 74 65 41 75 78 44 61 74 61 28 56  eDeleteAuxData(V
108cc 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75  dbeFunc *pVdbeFu
108cd 6e 63 2c 20 69 6e 74 20 6d 61 73 6b 29 7b 0a 20  nc, int mask){. 
108ce 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d   int i;.  for(i=
108cf 30 3b 20 69 3c 70 56 64 62 65 46 75 6e 63 2d 3e  0; i<pVdbeFunc->
108d0 6e 41 75 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nAux; i++){.    
108d1 73 74 72 75 63 74 20 41 75 78 44 61 74 61 20 2a  struct AuxData *
108d2 70 41 75 78 20 3d 20 26 70 56 64 62 65 46 75 6e  pAux = &pVdbeFun
108d3 63 2d 3e 61 70 41 75 78 5b 69 5d 3b 0a 20 20 20  c->apAux[i];.   
108d4 20 69 66 28 20 28 69 3e 33 31 20 7c 7c 20 21 28   if( (i>31 || !(
108d5 6d 61 73 6b 26 28 31 3c 3c 69 29 29 29 20 26 26  mask&(1<<i))) &&
108d6 20 70 41 75 78 2d 3e 70 41 75 78 20 29 7b 0a 20   pAux->pAux ){. 
108d7 20 20 20 20 20 69 66 28 20 70 41 75 78 2d 3e 78       if( pAux->x
108d8 44 65 6c 65 74 65 20 29 7b 0a 20 20 20 20 20 20  Delete ){.      
108d9 20 20 70 41 75 78 2d 3e 78 44 65 6c 65 74 65 28    pAux->xDelete(
108da 70 41 75 78 2d 3e 70 41 75 78 29 3b 0a 20 20 20  pAux->pAux);.   
108db 20 20 20 7d 0a 20 20 20 20 20 20 70 41 75 78 2d     }.      pAux-
108dc 3e 70 41 75 78 20 3d 20 30 3b 0a 20 20 20 20 7d  >pAux = 0;.    }
108dd 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  .  }.}../*.** De
108de 6c 65 74 65 20 61 6e 20 65 6e 74 69 72 65 20 56  lete an entire V
108df 44 42 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  DBE..*/.SQLITE_P
108e0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
108e1 74 65 33 56 64 62 65 44 65 6c 65 74 65 28 56 64  te3VdbeDelete(Vd
108e2 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b  be *p){.  int i;
108e3 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65  .  if( p==0 ) re
108e4 74 75 72 6e 3b 0a 20 20 43 6c 65 61 6e 75 70 28  turn;.  Cleanup(
108e5 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 50 72  p);.  if( p->pPr
108e6 65 76 20 29 7b 0a 20 20 20 20 70 2d 3e 70 50 72  ev ){.    p->pPr
108e7 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 2d 3e 70  ev->pNext = p->p
108e8 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Next;.  }else{. 
108e9 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62     assert( p->db
108ea 2d 3e 70 56 64 62 65 3d 3d 70 20 29 3b 0a 20 20  ->pVdbe==p );.  
108eb 20 20 70 2d 3e 64 62 2d 3e 70 56 64 62 65 20 3d    p->db->pVdbe =
108ec 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20   p->pNext;.  }. 
108ed 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 7b   if( p->pNext ){
108ee 0a 20 20 20 20 70 2d 3e 70 4e 65 78 74 2d 3e 70  .    p->pNext->p
108ef 50 72 65 76 20 3d 20 70 2d 3e 70 50 72 65 76 3b  Prev = p->pPrev;
108f0 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 61 4f  .  }.  if( p->aO
108f1 70 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30  p ){.    for(i=0
108f2 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29  ; i<p->nOp; i++)
108f3 7b 0a 20 20 20 20 20 20 4f 70 20 2a 70 4f 70 20  {.      Op *pOp 
108f4 3d 20 26 70 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20  = &p->aOp[i];.  
108f5 20 20 20 20 66 72 65 65 50 33 28 70 4f 70 2d 3e      freeP3(pOp->
108f6 70 33 74 79 70 65 2c 20 70 4f 70 2d 3e 70 33 29  p3type, pOp->p3)
108f7 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
108f8 74 65 46 72 65 65 28 70 2d 3e 61 4f 70 29 3b 0a  teFree(p->aOp);.
108f9 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d    }.  releaseMem
108fa 41 72 72 61 79 28 70 2d 3e 61 56 61 72 2c 20 70  Array(p->aVar, p
108fb 2d 3e 6e 56 61 72 29 3b 0a 20 20 73 71 6c 69 74  ->nVar);.  sqlit
108fc 65 46 72 65 65 28 70 2d 3e 61 4c 61 62 65 6c 29  eFree(p->aLabel)
108fd 3b 0a 20 20 73 71 6c 69 74 65 46 72 65 65 28 70  ;.  sqliteFree(p
108fe 2d 3e 61 53 74 61 63 6b 29 3b 0a 20 20 72 65 6c  ->aStack);.  rel
108ff 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70 2d 3e  easeMemArray(p->
10900 61 43 6f 6c 4e 61 6d 65 2c 20 70 2d 3e 6e 52 65  aColName, p->nRe
10901 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f  sColumn*COLNAME_
10902 4e 29 3b 0a 20 20 73 71 6c 69 74 65 46 72 65 65  N);.  sqliteFree
10903 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65 29 3b 0a 20  (p->aColName);. 
10904 20 73 71 6c 69 74 65 46 72 65 65 28 70 2d 3e 7a   sqliteFree(p->z
10905 53 71 6c 29 3b 0a 20 20 70 2d 3e 6d 61 67 69 63  Sql);.  p->magic
10906 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 44 45   = VDBE_MAGIC_DE
10907 41 44 3b 0a 20 20 73 71 6c 69 74 65 46 72 65 65  AD;.  sqliteFree
10908 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66  (p);.}../*.** If
10909 20 61 20 4d 6f 76 65 54 6f 20 6f 70 65 72 61 74   a MoveTo operat
1090a 69 6f 6e 20 69 73 20 70 65 6e 64 69 6e 67 20 6f  ion is pending o
1090b 6e 20 74 68 65 20 67 69 76 65 6e 20 63 75 72 73  n the given curs
1090c 6f 72 2c 20 74 68 65 6e 20 64 6f 20 74 68 61 74  or, then do that
1090d 0a 2a 2a 20 4d 6f 76 65 54 6f 20 6e 6f 77 2e 20  .** MoveTo now. 
1090e 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72   Return an error
1090f 20 63 6f 64 65 2e 20 20 49 66 20 6e 6f 20 4d 6f   code.  If no Mo
10910 76 65 54 6f 20 69 73 20 70 65 6e 64 69 6e 67 2c  veTo is pending,
10911 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65   this.** routine
10912 20 64 6f 65 73 20 6e 6f 74 68 69 6e 67 20 61 6e   does nothing an
10913 64 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45  d returns SQLITE
10914 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  _OK..*/.SQLITE_P
10915 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
10916 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65  e3VdbeCursorMove
10917 74 6f 28 43 75 72 73 6f 72 20 2a 70 29 7b 0a 20  to(Cursor *p){. 
10918 20 69 66 28 20 70 2d 3e 64 65 66 65 72 72 65 64   if( p->deferred
10919 4d 6f 76 65 74 6f 20 29 7b 0a 20 20 20 20 69 6e  Moveto ){.    in
1091a 74 20 72 65 73 2c 20 72 63 3b 0a 23 69 66 64 65  t res, rc;.#ifde
1091b 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20  f SQLITE_TEST.  
1091c 20 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c    extern int sql
1091d 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e  ite3_search_coun
1091e 74 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 61 73  t;.#endif.    as
1091f 73 65 72 74 28 20 70 2d 3e 69 73 54 61 62 6c 65  sert( p->isTable
10920 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   );.    rc = sql
10921 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28  ite3BtreeMoveto(
10922 70 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 70  p->pCursor, 0, p
10923 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 65 74 2c 20  ->movetoTarget, 
10924 30 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 69 66  0, &res);.    if
10925 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
10926 3b 0a 20 20 20 20 2a 70 2d 3e 70 49 6e 63 72 4b  ;.    *p->pIncrK
10927 65 79 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 6c  ey = 0;.    p->l
10928 61 73 74 52 6f 77 69 64 20 3d 20 6b 65 79 54 6f  astRowid = keyTo
10929 49 6e 74 28 70 2d 3e 6d 6f 76 65 74 6f 54 61 72  Int(p->movetoTar
1092a 67 65 74 29 3b 0a 20 20 20 20 70 2d 3e 72 6f 77  get);.    p->row
1092b 69 64 49 73 56 61 6c 69 64 20 3d 20 72 65 73 3d  idIsValid = res=
1092c 3d 30 3b 0a 20 20 20 20 69 66 28 20 72 65 73 3c  =0;.    if( res<
1092d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  0 ){.      rc = 
1092e 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74  sqlite3BtreeNext
1092f 28 70 2d 3e 70 43 75 72 73 6f 72 2c 20 26 72 65  (p->pCursor, &re
10930 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  s);.      if( rc
10931 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
10932 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54    }.#ifdef SQLIT
10933 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c 69 74  E_TEST.    sqlit
10934 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 2b  e3_search_count+
10935 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 2d  +;.#endif.    p-
10936 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20  >deferredMoveto 
10937 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 63 61 63 68  = 0;.    p->cach
10938 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f  eStatus = CACHE_
10939 53 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 72 65 74  STALE;.  }.  ret
1093a 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1093b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
1093c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e 73 3a  owing functions:
1093d 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64  .**.** sqlite3Vd
1093e 62 65 53 65 72 69 61 6c 54 79 70 65 28 29 0a 2a  beSerialType().*
1093f 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  * sqlite3VdbeSer
10940 69 61 6c 54 79 70 65 4c 65 6e 28 29 0a 2a 2a 20  ialTypeLen().** 
10941 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
10942 6c 52 65 61 64 28 29 0a 2a 2a 20 73 71 6c 69 74  lRead().** sqlit
10943 65 33 56 64 62 65 53 65 72 69 61 6c 4c 65 6e 28  e3VdbeSerialLen(
10944 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65  ).** sqlite3Vdbe
10945 53 65 72 69 61 6c 57 72 69 74 65 28 29 0a 2a 2a  SerialWrite().**
10946 0a 2a 2a 20 65 6e 63 61 70 73 75 6c 61 74 65 20  .** encapsulate 
10947 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 73 65  the code that se
10948 72 69 61 6c 69 7a 65 73 20 76 61 6c 75 65 73 20  rializes values 
10949 66 6f 72 20 73 74 6f 72 61 67 65 20 69 6e 20 53  for storage in S
1094a 51 4c 69 74 65 0a 2a 2a 20 64 61 74 61 20 61 6e  QLite.** data an
1094b 64 20 69 6e 64 65 78 20 72 65 63 6f 72 64 73 2e  d index records.
1094c 20 45 61 63 68 20 73 65 72 69 61 6c 69 7a 65 64   Each serialized
1094d 20 76 61 6c 75 65 20 63 6f 6e 73 69 73 74 73 20   value consists 
1094e 6f 66 20 61 0a 2a 2a 20 27 73 65 72 69 61 6c 2d  of a.** 'serial-
1094f 74 79 70 65 27 20 61 6e 64 20 61 20 62 6c 6f 62  type' and a blob
10950 20 6f 66 20 64 61 74 61 2e 20 54 68 65 20 73 65   of data. The se
10951 72 69 61 6c 20 74 79 70 65 20 69 73 20 61 6e 20  rial type is an 
10952 38 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 0a  8-byte unsigned.
10953 2a 2a 20 69 6e 74 65 67 65 72 2c 20 73 74 6f 72  ** integer, stor
10954 65 64 20 61 73 20 61 20 76 61 72 69 6e 74 2e 0a  ed as a varint..
10955 2a 2a 0a 2a 2a 20 49 6e 20 61 6e 20 53 51 4c 69  **.** In an SQLi
10956 74 65 20 69 6e 64 65 78 20 72 65 63 6f 72 64 2c  te index record,
10957 20 74 68 65 20 73 65 72 69 61 6c 20 74 79 70 65   the serial type
10958 20 69 73 20 73 74 6f 72 65 64 20 64 69 72 65 63   is stored direc
10959 74 6c 79 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68  tly before.** th
1095a 65 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 20 74  e blob of data t
1095b 68 61 74 20 69 74 20 63 6f 72 72 65 73 70 6f 6e  hat it correspon
1095c 64 73 20 74 6f 2e 20 49 6e 20 61 20 74 61 62 6c  ds to. In a tabl
1095d 65 20 72 65 63 6f 72 64 2c 20 61 6c 6c 20 73 65  e record, all se
1095e 72 69 61 6c 0a 2a 2a 20 74 79 70 65 73 20 61 72  rial.** types ar
1095f 65 20 73 74 6f 72 65 64 20 61 74 20 74 68 65 20  e stored at the 
10960 73 74 61 72 74 20 6f 66 20 74 68 65 20 72 65 63  start of the rec
10961 6f 72 64 2c 20 61 6e 64 20 74 68 65 20 62 6c 6f  ord, and the blo
10962 62 73 20 6f 66 20 64 61 74 61 20 61 74 0a 2a 2a  bs of data at.**
10963 20 74 68 65 20 65 6e 64 2e 20 48 65 6e 63 65 20   the end. Hence 
10964 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20  these functions 
10965 61 6c 6c 6f 77 20 74 68 65 20 63 61 6c 6c 65 72  allow the caller
10966 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 65 0a 2a   to handle the.*
10967 2a 20 73 65 72 69 61 6c 2d 74 79 70 65 20 61 6e  * serial-type an
10968 64 20 64 61 74 61 20 62 6c 6f 62 20 73 65 70 65  d data blob sepe
10969 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68  rately..**.** Th
1096a 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c  e following tabl
1096b 65 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20  e describes the 
1096c 76 61 72 69 6f 75 73 20 73 74 6f 72 61 67 65 20  various storage 
1096d 63 6c 61 73 73 65 73 20 66 6f 72 20 64 61 74 61  classes for data
1096e 3a 0a 2a 2a 0a 2a 2a 20 20 20 73 65 72 69 61 6c  :.**.**   serial
1096f 20 74 79 70 65 20 20 20 20 20 20 20 20 62 79 74   type        byt
10970 65 73 20 6f 66 20 64 61 74 61 20 20 20 20 20 20  es of data      
10971 74 79 70 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d  type.**   ------
10972 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d 2d 2d  --------     ---
10973 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20  ------------    
10974 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
10975 2a 2a 20 20 20 20 20 20 30 20 20 20 20 20 20 20  **      0       
10976 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20                0 
10977 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 0a             NULL.
10978 2a 2a 20 20 20 20 20 20 31 20 20 20 20 20 20 20  **      1       
10979 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 20                1 
1097a 20 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 65             signe
1097b 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20  d integer.**    
1097c 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20 20    2             
1097d 20 20 20 20 20 20 20 20 32 20 20 20 20 20 20 20          2       
1097e 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65       signed inte
1097f 67 65 72 0a 2a 2a 20 20 20 20 20 20 33 20 20 20  ger.**      3   
10980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10981 20 20 33 20 20 20 20 20 20 20 20 20 20 20 20 73    3            s
10982 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a  igned integer.**
10983 20 20 20 20 20 20 34 20 20 20 20 20 20 20 20 20        4         
10984 20 20 20 20 20 20 20 20 20 20 20 20 34 20 20 20              4   
10985 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 64 20           signed 
10986 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20  integer.**      
10987 35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  5               
10988 20 20 20 20 20 20 36 20 20 20 20 20 20 20 20 20        6         
10989 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65     signed intege
1098a 72 0a 2a 2a 20 20 20 20 20 20 36 20 20 20 20 20  r.**      6     
1098b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1098c 38 20 20 20 20 20 20 20 20 20 20 20 20 73 69 67  8            sig
1098d 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20  ned integer.**  
1098e 20 20 20 20 37 20 20 20 20 20 20 20 20 20 20 20      7           
1098f 20 20 20 20 20 20 20 20 20 20 38 20 20 20 20 20            8     
10990 20 20 20 20 20 20 20 49 45 45 45 20 66 6c 6f 61         IEEE floa
10991 74 0a 2a 2a 20 20 20 20 20 20 38 20 20 20 20 20  t.**      8     
10992 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10993 30 20 20 20 20 20 20 20 20 20 20 20 20 49 6e 74  0            Int
10994 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 20 30 0a  eger constant 0.
10995 2a 2a 20 20 20 20 20 20 39 20 20 20 20 20 20 20  **      9       
10996 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20                0 
10997 20 20 20 20 20 20 20 20 20 20 20 49 6e 74 65 67             Integ
10998 65 72 20 63 6f 6e 73 74 61 6e 74 20 31 0a 2a 2a  er constant 1.**
10999 20 20 20 20 20 31 30 2c 31 31 20 20 20 20 20 20       10,11      
1099a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1099b 20 20 20 20 20 20 20 20 20 72 65 73 65 72 76 65           reserve
1099c 64 20 66 6f 72 20 65 78 70 61 6e 73 69 6f 6e 0a  d for expansion.
1099d 2a 2a 20 20 20 20 4e 3e 3d 31 32 20 61 6e 64 20  **    N>=12 and 
1099e 65 76 65 6e 20 20 20 20 20 20 20 28 4e 2d 31 32  even       (N-12
1099f 29 2f 32 20 20 20 20 20 20 20 20 42 4c 4f 42 0a  )/2        BLOB.
109a0 2a 2a 20 20 20 20 4e 3e 3d 31 33 20 61 6e 64 20  **    N>=13 and 
109a1 6f 64 64 20 20 20 20 20 20 20 20 28 4e 2d 31 33  odd        (N-13
109a2 29 2f 32 20 20 20 20 20 20 20 20 74 65 78 74 0a  )/2        text.
109a3 2a 2a 0a 2a 2a 20 54 68 65 20 38 20 61 6e 64 20  **.** The 8 and 
109a4 39 20 74 79 70 65 73 20 77 65 72 65 20 61 64 64  9 types were add
109a5 65 64 20 69 6e 20 33 2e 33 2e 30 2c 20 66 69 6c  ed in 3.3.0, fil
109a6 65 20 66 6f 72 6d 61 74 20 34 2e 20 20 50 72 69  e format 4.  Pri
109a7 6f 72 20 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 6f  or versions.** o
109a8 66 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f  f SQLite will no
109a9 74 20 75 6e 64 65 72 73 74 61 6e 64 20 74 68 6f  t understand tho
109aa 73 65 20 73 65 72 69 61 6c 20 74 79 70 65 73 2e  se serial types.
109ab 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  .*/../*.** Retur
109ac 6e 20 74 68 65 20 73 65 72 69 61 6c 2d 74 79 70  n the serial-typ
109ad 65 20 66 6f 72 20 74 68 65 20 76 61 6c 75 65 20  e for the value 
109ae 73 74 6f 72 65 64 20 69 6e 20 70 4d 65 6d 2e 0a  stored in pMem..
109af 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
109b0 45 20 75 33 32 20 73 71 6c 69 74 65 33 56 64 62  E u32 sqlite3Vdb
109b1 65 53 65 72 69 61 6c 54 79 70 65 28 4d 65 6d 20  eSerialType(Mem 
109b2 2a 70 4d 65 6d 2c 20 69 6e 74 20 66 69 6c 65 5f  *pMem, int file_
109b3 66 6f 72 6d 61 74 29 7b 0a 20 20 69 6e 74 20 66  format){.  int f
109b4 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61  lags = pMem->fla
109b5 67 73 3b 0a 20 20 69 6e 74 20 6e 3b 0a 0a 20 20  gs;.  int n;..  
109b6 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 4e 75  if( flags&MEM_Nu
109b7 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ll ){.    return
109b8 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 6c   0;.  }.  if( fl
109b9 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20  ags&MEM_Int ){. 
109ba 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74     /* Figure out
109bb 20 77 68 65 74 68 65 72 20 74 6f 20 75 73 65 20   whether to use 
109bc 31 2c 20 32 2c 20 34 2c 20 36 20 6f 72 20 38 20  1, 2, 4, 6 or 8 
109bd 62 79 74 65 73 2e 20 2a 2f 0a 23 20 20 20 64 65  bytes. */.#   de
109be 66 69 6e 65 20 4d 41 58 5f 36 42 59 54 45 20 28  fine MAX_6BYTE (
109bf 28 28 28 69 36 34 29 30 78 30 30 30 30 31 30 30  (((i64)0x0000100
109c0 30 29 3c 3c 33 32 29 2d 31 29 0a 20 20 20 20 69  0)<<32)-1).    i
109c1 36 34 20 69 20 3d 20 70 4d 65 6d 2d 3e 75 2e 69  64 i = pMem->u.i
109c2 3b 0a 20 20 20 20 75 36 34 20 75 3b 0a 20 20 20  ;.    u64 u;.   
109c3 20 69 66 28 20 66 69 6c 65 5f 66 6f 72 6d 61 74   if( file_format
109c4 3e 3d 34 20 26 26 20 28 69 26 31 29 3d 3d 69 20  >=4 && (i&1)==i 
109c5 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
109c6 38 2b 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75  8+i;.    }.    u
109c7 20 3d 20 69 3c 30 20 3f 20 2d 69 20 3a 20 69 3b   = i<0 ? -i : i;
109c8 0a 20 20 20 20 69 66 28 20 75 3c 3d 31 32 37 20  .    if( u<=127 
109c9 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20  ) return 1;.    
109ca 69 66 28 20 75 3c 3d 33 32 37 36 37 20 29 20 72  if( u<=32767 ) r
109cb 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 69 66 28  eturn 2;.    if(
109cc 20 75 3c 3d 38 33 38 38 36 30 37 20 29 20 72 65   u<=8388607 ) re
109cd 74 75 72 6e 20 33 3b 0a 20 20 20 20 69 66 28 20  turn 3;.    if( 
109ce 75 3c 3d 32 31 34 37 34 38 33 36 34 37 20 29 20  u<=2147483647 ) 
109cf 72 65 74 75 72 6e 20 34 3b 0a 20 20 20 20 69 66  return 4;.    if
109d0 28 20 75 3c 3d 4d 41 58 5f 36 42 59 54 45 20 29  ( u<=MAX_6BYTE )
109d1 20 72 65 74 75 72 6e 20 35 3b 0a 20 20 20 20 72   return 5;.    r
109d2 65 74 75 72 6e 20 36 3b 0a 20 20 7d 0a 20 20 69  eturn 6;.  }.  i
109d3 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 52 65 61  f( flags&MEM_Rea
109d4 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  l ){.    return 
109d5 37 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  7;.  }.  assert(
109d6 20 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c   flags&(MEM_Str|
109d7 4d 45 4d 5f 42 6c 6f 62 29 20 29 3b 0a 20 20 6e  MEM_Blob) );.  n
109d8 20 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 69 66   = pMem->n;.  if
109d9 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65  ( flags & MEM_Ze
109da 72 6f 20 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 70  ro ){.    n += p
109db 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 7d 0a 20 20  Mem->u.i;.  }.  
109dc 61 73 73 65 72 74 28 20 6e 3e 3d 30 20 29 3b 0a  assert( n>=0 );.
109dd 20 20 72 65 74 75 72 6e 20 28 28 6e 2a 32 29 20    return ((n*2) 
109de 2b 20 31 32 20 2b 20 28 28 66 6c 61 67 73 26 4d  + 12 + ((flags&M
109df 45 4d 5f 53 74 72 29 21 3d 30 29 29 3b 0a 7d 0a  EM_Str)!=0));.}.
109e0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
109e1 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20  e length of the 
109e2 64 61 74 61 20 63 6f 72 72 65 73 70 6f 6e 64 69  data correspondi
109e3 6e 67 20 74 6f 20 74 68 65 20 73 75 70 70 6c 69  ng to the suppli
109e4 65 64 20 73 65 72 69 61 6c 2d 74 79 70 65 2e 0a  ed serial-type..
109e5 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
109e6 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
109e7 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 75  eSerialTypeLen(u
109e8 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 29 7b  32 serial_type){
109e9 0a 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79  .  if( serial_ty
109ea 70 65 3e 3d 31 32 20 29 7b 0a 20 20 20 20 72 65  pe>=12 ){.    re
109eb 74 75 72 6e 20 28 73 65 72 69 61 6c 5f 74 79 70  turn (serial_typ
109ec 65 2d 31 32 29 2f 32 3b 0a 20 20 7d 65 6c 73 65  e-12)/2;.  }else
109ed 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e  {.    static con
109ee 73 74 20 75 38 20 61 53 69 7a 65 5b 5d 20 3d 20  st u8 aSize[] = 
109ef 7b 20 30 2c 20 31 2c 20 32 2c 20 33 2c 20 34 2c  { 0, 1, 2, 3, 4,
109f0 20 36 2c 20 38 2c 20 38 2c 20 30 2c 20 30 2c 20   6, 8, 8, 0, 0, 
109f1 30 2c 20 30 20 7d 3b 0a 20 20 20 20 72 65 74 75  0, 0 };.    retu
109f2 72 6e 20 61 53 69 7a 65 5b 73 65 72 69 61 6c 5f  rn aSize[serial_
109f3 74 79 70 65 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  type];.  }.}../*
109f4 0a 2a 2a 20 49 66 20 77 65 20 61 72 65 20 6f 6e  .** If we are on
109f5 20 61 6e 20 61 72 63 68 69 74 65 63 74 75 72 65   an architecture
109f6 20 77 69 74 68 20 6d 69 78 65 64 2d 65 6e 64 69   with mixed-endi
109f7 61 6e 20 66 6c 6f 61 74 69 6e 67 20 0a 2a 2a 20  an floating .** 
109f8 70 6f 69 6e 74 73 20 28 65 78 3a 20 41 52 4d 37  points (ex: ARM7
109f9 29 20 74 68 65 6e 20 73 77 61 70 20 74 68 65 20  ) then swap the 
109fa 6c 6f 77 65 72 20 34 20 62 79 74 65 73 20 77 69  lower 4 bytes wi
109fb 74 68 20 74 68 65 20 0a 2a 2a 20 75 70 70 65 72  th the .** upper
109fc 20 34 20 62 79 74 65 73 2e 20 20 52 65 74 75 72   4 bytes.  Retur
109fd 6e 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2a  n the result..**
109fe 0a 2a 2a 20 46 6f 72 20 6d 6f 73 74 20 61 72 63  .** For most arc
109ff 68 69 74 65 63 74 75 72 65 73 2c 20 74 68 69 73  hitectures, this
10a00 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a   is a no-op..**.
10a01 2a 2a 20 28 6c 61 74 65 72 29 3a 20 20 49 74 20  ** (later):  It 
10a02 69 73 20 72 65 70 6f 72 74 65 64 20 74 6f 20 6d  is reported to m
10a03 65 20 74 68 61 74 20 74 68 65 20 6d 69 78 65 64  e that the mixed
10a04 2d 65 6e 64 69 61 6e 20 70 72 6f 62 6c 65 6d 0a  -endian problem.
10a05 2a 2a 20 6f 6e 20 41 52 4d 37 20 69 73 20 61 6e  ** on ARM7 is an
10a06 20 69 73 73 75 65 20 77 69 74 68 20 47 43 43 2c   issue with GCC,
10a07 20 6e 6f 74 20 77 69 74 68 20 74 68 65 20 41 52   not with the AR
10a08 4d 37 20 63 68 69 70 2e 20 20 49 74 20 73 65 65  M7 chip.  It see
10a09 6d 73 0a 2a 2a 20 74 68 61 74 20 65 61 72 6c 79  ms.** that early
10a0a 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 47 43 43   versions of GCC
10a0b 20 73 74 6f 72 65 64 20 74 68 65 20 74 77 6f 20   stored the two 
10a0c 77 6f 72 64 73 20 6f 66 20 61 20 36 34 2d 62 69  words of a 64-bi
10a0d 74 0a 2a 2a 20 66 6c 6f 61 74 20 69 6e 20 74 68  t.** float in th
10a0e 65 20 77 72 6f 6e 67 20 6f 72 64 65 72 2e 20 20  e wrong order.  
10a0f 41 6e 64 20 74 68 61 74 20 65 72 72 6f 72 20 68  And that error h
10a10 61 73 20 62 65 65 6e 20 70 72 6f 70 61 67 61 74  as been propagat
10a11 65 64 0a 2a 2a 20 65 76 65 72 20 73 69 6e 63 65  ed.** ever since
10a12 2e 20 20 54 68 65 20 62 6c 61 6d 65 20 69 73 20  .  The blame is 
10a13 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20  not necessarily 
10a14 77 69 74 68 20 47 43 43 2c 20 74 68 6f 75 67 68  with GCC, though
10a15 2e 0a 2a 2a 20 47 43 43 20 6d 69 67 68 74 20 68  ..** GCC might h
10a16 61 76 65 20 6a 75 73 74 20 63 6f 70 79 69 6e 67  ave just copying
10a17 20 74 68 65 20 70 72 6f 62 6c 65 6d 20 66 72 6f   the problem fro
10a18 6d 20 61 20 70 72 69 6f 72 20 63 6f 6d 70 69 6c  m a prior compil
10a19 65 72 2e 0a 2a 2a 20 49 20 61 6d 20 61 6c 73 6f  er..** I am also
10a1a 20 74 6f 6c 64 20 74 68 61 74 20 6e 65 77 65 72   told that newer
10a1b 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 47 43 43   versions of GCC
10a1c 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 61 20 64   that follow a d
10a1d 69 66 66 65 72 65 6e 74 0a 2a 2a 20 41 42 49 20  ifferent.** ABI 
10a1e 67 65 74 20 74 68 65 20 62 79 74 65 20 6f 72 64  get the byte ord
10a1f 65 72 20 72 69 67 68 74 2e 0a 2a 2a 0a 2a 2a 20  er right..**.** 
10a20 44 65 76 65 6c 6f 70 65 72 73 20 75 73 69 6e 67  Developers using
10a21 20 53 51 4c 69 74 65 20 6f 6e 20 61 6e 20 41 52   SQLite on an AR
10a22 4d 37 20 73 68 6f 75 6c 64 20 63 6f 6d 70 69 6c  M7 should compil
10a23 65 20 61 6e 64 20 72 75 6e 20 74 68 65 69 72 0a  e and run their.
10a24 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 75  ** application u
10a25 73 69 6e 67 20 2d 44 53 51 4c 49 54 45 5f 44 45  sing -DSQLITE_DE
10a26 42 55 47 3d 31 20 61 74 20 6c 65 61 73 74 20 6f  BUG=1 at least o
10a27 6e 63 65 2e 20 20 57 69 74 68 20 44 45 42 55 47  nce.  With DEBUG
10a28 0a 2a 2a 20 65 6e 61 62 6c 65 64 2c 20 73 6f 6d  .** enabled, som
10a29 65 20 61 73 73 65 72 74 73 20 62 65 6c 6f 77 20  e asserts below 
10a2a 77 69 6c 6c 20 65 6e 73 75 72 65 20 74 68 61 74  will ensure that
10a2b 20 74 68 65 20 62 79 74 65 20 6f 72 64 65 72 20   the byte order 
10a2c 6f 66 0a 2a 2a 20 66 6c 6f 61 74 69 6e 67 20 70  of.** floating p
10a2d 6f 69 6e 74 20 76 61 6c 75 65 73 20 69 73 20 63  oint values is c
10a2e 6f 72 72 65 63 74 2e 0a 2a 2f 0a 23 69 66 64 65  orrect..*/.#ifde
10a2f 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f 45  f SQLITE_MIXED_E
10a30 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f 41  NDIAN_64BIT_FLOA
10a31 54 0a 73 74 61 74 69 63 20 64 6f 75 62 6c 65 20  T.static double 
10a32 66 6c 6f 61 74 53 77 61 70 28 64 6f 75 62 6c 65  floatSwap(double
10a33 20 69 6e 29 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a   in){.  union {.
10a34 20 20 20 20 64 6f 75 62 6c 65 20 72 3b 0a 20 20      double r;.  
10a35 20 20 75 33 32 20 69 5b 32 5d 3b 0a 20 20 7d 20    u32 i[2];.  } 
10a36 75 3b 0a 20 20 75 33 32 20 74 3b 0a 0a 20 20 75  u;.  u32 t;..  u
10a37 2e 72 20 3d 20 69 6e 3b 0a 20 20 74 20 3d 20 75  .r = in;.  t = u
10a38 2e 69 5b 30 5d 3b 0a 20 20 75 2e 69 5b 30 5d 20  .i[0];.  u.i[0] 
10a39 3d 20 75 2e 69 5b 31 5d 3b 0a 20 20 75 2e 69 5b  = u.i[1];.  u.i[
10a3a 31 5d 20 3d 20 74 3b 0a 20 20 72 65 74 75 72 6e  1] = t;.  return
10a3b 20 75 2e 72 3b 0a 7d 0a 23 20 64 65 66 69 6e 65   u.r;.}.# define
10a3c 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69 61 6e   swapMixedEndian
10a3d 46 6c 6f 61 74 28 58 29 20 20 58 20 3d 20 66 6c  Float(X)  X = fl
10a3e 6f 61 74 53 77 61 70 28 58 29 0a 23 65 6c 73 65  oatSwap(X).#else
10a3f 0a 23 20 64 65 66 69 6e 65 20 73 77 61 70 4d 69  .# define swapMi
10a40 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 58  xedEndianFloat(X
10a41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ).#endif../*.** 
10a42 57 72 69 74 65 20 74 68 65 20 73 65 72 69 61 6c  Write the serial
10a43 69 7a 65 64 20 64 61 74 61 20 62 6c 6f 62 20 66  ized data blob f
10a44 6f 72 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f  or the value sto
10a45 72 65 64 20 69 6e 20 70 4d 65 6d 20 69 6e 74 6f  red in pMem into
10a46 20 0a 2a 2a 20 62 75 66 2e 20 49 74 20 69 73 20   .** buf. It is 
10a47 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65  assumed that the
10a48 20 63 61 6c 6c 65 72 20 68 61 73 20 61 6c 6c 6f   caller has allo
10a49 63 61 74 65 64 20 73 75 66 66 69 63 69 65 6e 74  cated sufficient
10a4a 20 73 70 61 63 65 2e 0a 2a 2a 20 52 65 74 75 72   space..** Retur
10a4b 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
10a4c 62 79 74 65 73 20 77 72 69 74 74 65 6e 2e 0a 2a  bytes written..*
10a4d 2a 0a 2a 2a 20 6e 42 75 66 20 69 73 20 74 68 65  *.** nBuf is the
10a4e 20 61 6d 6f 75 6e 74 20 6f 66 20 73 70 61 63 65   amount of space
10a4f 20 6c 65 66 74 20 69 6e 20 62 75 66 5b 5d 2e 20   left in buf[]. 
10a50 20 6e 42 75 66 20 6d 75 73 74 20 61 6c 77 61 79   nBuf must alway
10a51 73 20 62 65 0a 2a 2a 20 6c 61 72 67 65 20 65 6e  s be.** large en
10a52 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 74 68 65  ough to hold the
10a53 20 65 6e 74 69 72 65 20 66 69 65 6c 64 2e 20 20   entire field.  
10a54 45 78 63 65 70 74 2c 20 69 66 20 74 68 65 20 66  Except, if the f
10a55 69 65 6c 64 20 69 73 0a 2a 2a 20 61 20 62 6c 6f  ield is.** a blo
10a56 62 20 77 69 74 68 20 61 20 7a 65 72 6f 2d 66 69  b with a zero-fi
10a57 6c 6c 65 64 20 74 61 69 6c 2c 20 74 68 65 6e 20  lled tail, then 
10a58 62 75 66 5b 5d 20 6d 69 67 68 74 20 62 65 20 6a  buf[] might be j
10a59 75 73 74 20 74 68 65 20 72 69 67 68 74 0a 2a 2a  ust the right.**
10a5a 20 73 69 7a 65 20 74 6f 20 68 6f 6c 64 20 65 76   size to hold ev
10a5b 65 72 79 74 68 69 6e 67 20 65 78 63 65 70 74 20  erything except 
10a5c 66 6f 72 20 74 68 65 20 7a 65 72 6f 2d 66 69 6c  for the zero-fil
10a5d 6c 65 64 20 74 61 69 6c 2e 20 20 49 66 20 62 75  led tail.  If bu
10a5e 66 5b 5d 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 62  f[].** is only b
10a5f 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c  ig enough to hol
10a60 64 20 74 68 65 20 6e 6f 6e 2d 7a 65 72 6f 20 70  d the non-zero p
10a61 72 65 66 69 78 2c 20 74 68 65 6e 20 6f 6e 6c 79  refix, then only
10a62 20 77 72 69 74 65 20 74 68 61 74 0a 2a 2a 20 70   write that.** p
10a63 72 65 66 69 78 20 69 6e 74 6f 20 62 75 66 5b 5d  refix into buf[]
10a64 2e 20 20 42 75 74 20 69 66 20 62 75 66 5b 5d 20  .  But if buf[] 
10a65 69 73 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20  is large enough 
10a66 74 6f 20 68 6f 6c 64 20 62 6f 74 68 20 74 68 65  to hold both the
10a67 0a 2a 2a 20 70 72 65 66 69 78 20 61 6e 64 20 74  .** prefix and t
10a68 68 65 20 74 61 69 6c 20 74 68 65 6e 20 77 72 69  he tail then wri
10a69 74 65 20 74 68 65 20 70 72 65 66 69 78 20 61 6e  te the prefix an
10a6a 64 20 73 65 74 20 74 68 65 20 74 61 69 6c 20 74  d set the tail t
10a6b 6f 20 61 6c 6c 0a 2a 2a 20 7a 65 72 6f 73 2e 0a  o all.** zeros..
10a6c 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  **.** Return the
10a6d 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
10a6e 20 61 63 74 75 61 6c 6c 79 20 77 72 69 74 74 65   actually writte
10a6f 6e 20 69 6e 74 6f 20 62 75 66 5b 5d 2e 20 20 54  n into buf[].  T
10a70 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20  he number.** of 
10a71 62 79 74 65 73 20 69 6e 20 74 68 65 20 7a 65 72  bytes in the zer
10a72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c 20 69 73  o-filled tail is
10a73 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 65   included in the
10a74 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 6e   return value on
10a75 6c 79 0a 2a 2a 20 69 66 20 74 68 6f 73 65 20 62  ly.** if those b
10a76 79 74 65 73 20 77 65 72 65 20 7a 65 72 6f 65 64  ytes were zeroed
10a77 20 69 6e 20 62 75 66 5b 5d 2e 0a 2a 2f 20 0a 53   in buf[]..*/ .S
10a78 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
10a79 74 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  t sqlite3VdbeSer
10a7a 69 61 6c 50 75 74 28 75 38 20 2a 62 75 66 2c 20  ialPut(u8 *buf, 
10a7b 69 6e 74 20 6e 42 75 66 2c 20 4d 65 6d 20 2a 70  int nBuf, Mem *p
10a7c 4d 65 6d 2c 20 69 6e 74 20 66 69 6c 65 5f 66 6f  Mem, int file_fo
10a7d 72 6d 61 74 29 7b 0a 20 20 75 33 32 20 73 65 72  rmat){.  u32 ser
10a7e 69 61 6c 5f 74 79 70 65 20 3d 20 73 71 6c 69 74  ial_type = sqlit
10a7f 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65  e3VdbeSerialType
10a80 28 70 4d 65 6d 2c 20 66 69 6c 65 5f 66 6f 72 6d  (pMem, file_form
10a81 61 74 29 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a  at);.  int len;.
10a82 0a 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 61 6e  .  /* Integer an
10a83 64 20 52 65 61 6c 20 2a 2f 0a 20 20 69 66 28 20  d Real */.  if( 
10a84 73 65 72 69 61 6c 5f 74 79 70 65 3c 3d 37 20 26  serial_type<=7 &
10a85 26 20 73 65 72 69 61 6c 5f 74 79 70 65 3e 30 20  & serial_type>0 
10a86 29 7b 0a 20 20 20 20 75 36 34 20 76 3b 0a 20 20  ){.    u64 v;.  
10a87 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 69 66 28    int i;.    if(
10a88 20 73 65 72 69 61 6c 5f 74 79 70 65 3d 3d 37 20   serial_type==7 
10a89 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
10a8a 20 73 69 7a 65 6f 66 28 76 29 3d 3d 73 69 7a 65   sizeof(v)==size
10a8b 6f 66 28 70 4d 65 6d 2d 3e 72 29 20 29 3b 0a 20  of(pMem->r) );. 
10a8c 20 20 20 20 20 73 77 61 70 4d 69 78 65 64 45 6e       swapMixedEn
10a8d 64 69 61 6e 46 6c 6f 61 74 28 70 4d 65 6d 2d 3e  dianFloat(pMem->
10a8e 72 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79  r);.      memcpy
10a8f 28 26 76 2c 20 26 70 4d 65 6d 2d 3e 72 2c 20 73  (&v, &pMem->r, s
10a90 69 7a 65 6f 66 28 76 29 29 3b 0a 20 20 20 20 7d  izeof(v));.    }
10a91 65 6c 73 65 7b 0a 20 20 20 20 20 20 76 20 3d 20  else{.      v = 
10a92 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 20 20 7d  pMem->u.i;.    }
10a93 0a 20 20 20 20 6c 65 6e 20 3d 20 69 20 3d 20 73  .    len = i = s
10a94 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
10a95 54 79 70 65 4c 65 6e 28 73 65 72 69 61 6c 5f 74  TypeLen(serial_t
10a96 79 70 65 29 3b 0a 20 20 20 20 61 73 73 65 72 74  ype);.    assert
10a97 28 20 6c 65 6e 3c 3d 6e 42 75 66 20 29 3b 0a 20  ( len<=nBuf );. 
10a98 20 20 20 77 68 69 6c 65 28 20 69 2d 2d 20 29 7b     while( i-- ){
10a99 0a 20 20 20 20 20 20 62 75 66 5b 69 5d 20 3d 20  .      buf[i] = 
10a9a 28 76 26 30 78 46 46 29 3b 0a 20 20 20 20 20 20  (v&0xFF);.      
10a9b 76 20 3e 3e 3d 20 38 3b 0a 20 20 20 20 7d 0a 20  v >>= 8;.    }. 
10a9c 20 20 20 72 65 74 75 72 6e 20 6c 65 6e 3b 0a 20     return len;. 
10a9d 20 7d 0a 0a 20 20 2f 2a 20 53 74 72 69 6e 67 20   }..  /* String 
10a9e 6f 72 20 62 6c 6f 62 20 2a 2f 0a 20 20 69 66 28  or blob */.  if(
10a9f 20 73 65 72 69 61 6c 5f 74 79 70 65 3e 3d 31 32   serial_type>=12
10aa0 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
10aa1 70 4d 65 6d 2d 3e 6e 20 2b 20 28 28 70 4d 65 6d  pMem->n + ((pMem
10aa2 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65  ->flags & MEM_Ze
10aa3 72 6f 29 3f 70 4d 65 6d 2d 3e 75 2e 69 3a 30 29  ro)?pMem->u.i:0)
10aa4 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 3d 3d  .             ==
10aa5 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
10aa6 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61 6c  alTypeLen(serial
10aa7 5f 74 79 70 65 29 20 29 3b 0a 20 20 20 20 61 73  _type) );.    as
10aa8 73 65 72 74 28 20 70 4d 65 6d 2d 3e 6e 3c 3d 6e  sert( pMem->n<=n
10aa9 42 75 66 20 29 3b 0a 20 20 20 20 6c 65 6e 20 3d  Buf );.    len =
10aaa 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 20 20 6d 65   pMem->n;.    me
10aab 6d 63 70 79 28 62 75 66 2c 20 70 4d 65 6d 2d 3e  mcpy(buf, pMem->
10aac 7a 2c 20 6c 65 6e 29 3b 0a 20 20 20 20 69 66 28  z, len);.    if(
10aad 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d   pMem->flags & M
10aae 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20  EM_Zero ){.     
10aaf 20 6c 65 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75 2e   len += pMem->u.
10ab0 69 3b 0a 20 20 20 20 20 20 69 66 28 20 6c 65 6e  i;.      if( len
10ab1 3e 6e 42 75 66 20 29 7b 0a 20 20 20 20 20 20 20  >nBuf ){.       
10ab2 20 6c 65 6e 20 3d 20 6e 42 75 66 3b 0a 20 20 20   len = nBuf;.   
10ab3 20 20 20 7d 0a 20 20 20 20 20 20 6d 65 6d 73 65     }.      memse
10ab4 74 28 26 62 75 66 5b 70 4d 65 6d 2d 3e 6e 5d 2c  t(&buf[pMem->n],
10ab5 20 30 2c 20 6c 65 6e 2d 70 4d 65 6d 2d 3e 6e 29   0, len-pMem->n)
10ab6 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ;.    }.    retu
10ab7 72 6e 20 6c 65 6e 3b 0a 20 20 7d 0a 0a 20 20 2f  rn len;.  }..  /
10ab8 2a 20 4e 55 4c 4c 20 6f 72 20 63 6f 6e 73 74 61  * NULL or consta
10ab9 6e 74 73 20 30 20 6f 72 20 31 20 2a 2f 0a 20 20  nts 0 or 1 */.  
10aba 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
10abb 2a 2a 20 44 65 73 65 72 69 61 6c 69 7a 65 20 74  ** Deserialize t
10abc 68 65 20 64 61 74 61 20 62 6c 6f 62 20 70 6f 69  he data blob poi
10abd 6e 74 65 64 20 74 6f 20 62 79 20 62 75 66 20 61  nted to by buf a
10abe 73 20 73 65 72 69 61 6c 20 74 79 70 65 20 73 65  s serial type se
10abf 72 69 61 6c 5f 74 79 70 65 0a 2a 2a 20 61 6e 64  rial_type.** and
10ac0 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c   store the resul
10ac1 74 20 69 6e 20 70 4d 65 6d 2e 20 20 52 65 74 75  t in pMem.  Retu
10ac2 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
10ac3 20 62 79 74 65 73 20 72 65 61 64 2e 0a 2a 2f 20   bytes read..*/ 
10ac4 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
10ac5 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 53  int sqlite3VdbeS
10ac6 65 72 69 61 6c 47 65 74 28 0a 20 20 63 6f 6e 73  erialGet(.  cons
10ac7 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
10ac8 2a 62 75 66 2c 20 20 20 20 20 2f 2a 20 42 75 66  *buf,     /* Buf
10ac9 66 65 72 20 74 6f 20 64 65 73 65 72 69 61 6c 69  fer to deseriali
10aca 7a 65 20 66 72 6f 6d 20 2a 2f 0a 20 20 75 33 32  ze from */.  u32
10acb 20 73 65 72 69 61 6c 5f 74 79 70 65 2c 20 20 20   serial_type,   
10acc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65             /* Se
10acd 72 69 61 6c 20 74 79 70 65 20 74 6f 20 64 65 73  rial type to des
10ace 65 72 69 61 6c 69 7a 65 20 2a 2f 0a 20 20 4d 65  erialize */.  Me
10acf 6d 20 2a 70 4d 65 6d 20 20 20 20 20 20 20 20 20  m *pMem         
10ad0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
10ad1 65 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20 77 72  emory cell to wr
10ad2 69 74 65 20 76 61 6c 75 65 20 69 6e 74 6f 20 2a  ite value into *
10ad3 2f 0a 29 7b 0a 20 20 73 77 69 74 63 68 28 20 73  /.){.  switch( s
10ad4 65 72 69 61 6c 5f 74 79 70 65 20 29 7b 0a 20 20  erial_type ){.  
10ad5 20 20 63 61 73 65 20 31 30 3a 20 20 20 2f 2a 20    case 10:   /* 
10ad6 52 65 73 65 72 76 65 64 20 66 6f 72 20 66 75 74  Reserved for fut
10ad7 75 72 65 20 75 73 65 20 2a 2f 0a 20 20 20 20 63  ure use */.    c
10ad8 61 73 65 20 31 31 3a 20 20 20 2f 2a 20 52 65 73  ase 11:   /* Res
10ad9 65 72 76 65 64 20 66 6f 72 20 66 75 74 75 72 65  erved for future
10ada 20 75 73 65 20 2a 2f 0a 20 20 20 20 63 61 73 65   use */.    case
10adb 20 30 3a 20 7b 20 20 2f 2a 20 4e 55 4c 4c 20 2a   0: {  /* NULL *
10adc 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c  /.      pMem->fl
10add 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a  ags = MEM_Null;.
10ade 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
10adf 20 7d 0a 20 20 20 20 63 61 73 65 20 31 3a 20 7b   }.    case 1: {
10ae0 20 2f 2a 20 31 2d 62 79 74 65 20 73 69 67 6e 65   /* 1-byte signe
10ae1 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20  d integer */.   
10ae2 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28     pMem->u.i = (
10ae3 73 69 67 6e 65 64 20 63 68 61 72 29 62 75 66 5b  signed char)buf[
10ae4 30 5d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  0];.      pMem->
10ae5 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
10ae6 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
10ae7 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
10ae8 32 3a 20 7b 20 2f 2a 20 32 2d 62 79 74 65 20 73  2: { /* 2-byte s
10ae9 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f  igned integer */
10aea 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69  .      pMem->u.i
10aeb 20 3d 20 28 28 28 73 69 67 6e 65 64 20 63 68 61   = (((signed cha
10aec 72 29 62 75 66 5b 30 5d 29 3c 3c 38 29 20 7c 20  r)buf[0])<<8) | 
10aed 62 75 66 5b 31 5d 3b 0a 20 20 20 20 20 20 70 4d  buf[1];.      pM
10aee 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
10aef 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 72  Int;.      retur
10af0 6e 20 32 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  n 2;.    }.    c
10af1 61 73 65 20 33 3a 20 7b 20 2f 2a 20 33 2d 62 79  ase 3: { /* 3-by
10af2 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65  te signed intege
10af3 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d  r */.      pMem-
10af4 3e 75 2e 69 20 3d 20 28 28 28 73 69 67 6e 65 64  >u.i = (((signed
10af5 20 63 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c 31   char)buf[0])<<1
10af6 36 29 20 7c 20 28 62 75 66 5b 31 5d 3c 3c 38 29  6) | (buf[1]<<8)
10af7 20 7c 20 62 75 66 5b 32 5d 3b 0a 20 20 20 20 20   | buf[2];.     
10af8 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
10af9 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65  EM_Int;.      re
10afa 74 75 72 6e 20 33 3b 0a 20 20 20 20 7d 0a 20 20  turn 3;.    }.  
10afb 20 20 63 61 73 65 20 34 3a 20 7b 20 2f 2a 20 34    case 4: { /* 4
10afc 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74  -byte signed int
10afd 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d  eger */.      pM
10afe 65 6d 2d 3e 75 2e 69 20 3d 20 28 62 75 66 5b 30  em->u.i = (buf[0
10aff 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 31 5d  ]<<24) | (buf[1]
10b00 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 32 5d 3c  <<16) | (buf[2]<
10b01 3c 38 29 20 7c 20 62 75 66 5b 33 5d 3b 0a 20 20  <8) | buf[3];.  
10b02 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
10b03 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20  = MEM_Int;.     
10b04 20 72 65 74 75 72 6e 20 34 3b 0a 20 20 20 20 7d   return 4;.    }
10b05 0a 20 20 20 20 63 61 73 65 20 35 3a 20 7b 20 2f  .    case 5: { /
10b06 2a 20 36 2d 62 79 74 65 20 73 69 67 6e 65 64 20  * 6-byte signed 
10b07 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20  integer */.     
10b08 20 75 36 34 20 78 20 3d 20 28 28 28 73 69 67 6e   u64 x = (((sign
10b09 65 64 20 63 68 61 72 29 62 75 66 5b 30 5d 29 3c  ed char)buf[0])<
10b0a 3c 38 29 20 7c 20 62 75 66 5b 31 5d 3b 0a 20 20  <8) | buf[1];.  
10b0b 20 20 20 20 75 33 32 20 79 20 3d 20 28 62 75 66      u32 y = (buf
10b0c 5b 32 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b  [2]<<24) | (buf[
10b0d 33 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 34  3]<<16) | (buf[4
10b0e 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 35 5d 3b 0a  ]<<8) | buf[5];.
10b0f 20 20 20 20 20 20 78 20 3d 20 28 78 3c 3c 33 32        x = (x<<32
10b10 29 20 7c 20 79 3b 0a 20 20 20 20 20 20 70 4d 65  ) | y;.      pMe
10b11 6d 2d 3e 75 2e 69 20 3d 20 2a 28 69 36 34 2a 29  m->u.i = *(i64*)
10b12 26 78 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  &x;.      pMem->
10b13 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
10b14 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 36 3b  .      return 6;
10b15 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
10b16 36 3a 20 20 20 2f 2a 20 38 2d 62 79 74 65 20 73  6:   /* 8-byte s
10b17 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f  igned integer */
10b18 0a 20 20 20 20 63 61 73 65 20 37 3a 20 7b 20 2f  .    case 7: { /
10b19 2a 20 49 45 45 45 20 66 6c 6f 61 74 69 6e 67 20  * IEEE floating 
10b1a 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 75  point */.      u
10b1b 36 34 20 78 3b 0a 20 20 20 20 20 20 75 33 32 20  64 x;.      u32 
10b1c 79 3b 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  y;.#if !defined(
10b1d 4e 44 45 42 55 47 29 20 26 26 20 21 64 65 66 69  NDEBUG) && !defi
10b1e 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
10b1f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 29 0a  FLOATING_POINT).
10b20 20 20 20 20 20 20 2f 2a 20 56 65 72 69 66 79 20        /* Verify 
10b21 74 68 61 74 20 69 6e 74 65 67 65 72 73 20 61 6e  that integers an
10b22 64 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  d floating point
10b23 20 76 61 6c 75 65 73 20 75 73 65 20 74 68 65 20   values use the 
10b24 73 61 6d 65 0a 20 20 20 20 20 20 2a 2a 20 62 79  same.      ** by
10b25 74 65 20 6f 72 64 65 72 2e 20 20 4f 72 2c 20 74  te order.  Or, t
10b26 68 61 74 20 69 66 20 53 51 4c 49 54 45 5f 4d 49  hat if SQLITE_MI
10b27 58 45 44 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54  XED_ENDIAN_64BIT
10b28 5f 46 4c 4f 41 54 20 69 73 0a 20 20 20 20 20 20  _FLOAT is.      
10b29 2a 2a 20 64 65 66 69 6e 65 64 20 74 68 61 74 20  ** defined that 
10b2a 36 34 2d 62 69 74 20 66 6c 6f 61 74 69 6e 67 20  64-bit floating 
10b2b 70 6f 69 6e 74 20 76 61 6c 75 65 73 20 72 65 61  point values rea
10b2c 6c 6c 79 20 61 72 65 20 6d 69 78 65 64 0a 20 20  lly are mixed.  
10b2d 20 20 20 20 2a 2a 20 65 6e 64 69 61 6e 2e 0a 20      ** endian.. 
10b2e 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 74       */.      st
10b2f 61 74 69 63 20 63 6f 6e 73 74 20 75 36 34 20 74  atic const u64 t
10b30 31 20 3d 20 28 28 75 36 34 29 30 78 33 66 66 30  1 = ((u64)0x3ff0
10b31 30 30 30 30 29 3c 3c 33 32 3b 0a 20 20 20 20 20  0000)<<32;.     
10b32 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 64 6f   static const do
10b33 75 62 6c 65 20 72 31 20 3d 20 31 2e 30 3b 0a 20  uble r1 = 1.0;. 
10b34 20 20 20 20 20 64 6f 75 62 6c 65 20 72 32 20 3d       double r2 =
10b35 20 72 31 3b 0a 20 20 20 20 20 20 73 77 61 70 4d   r1;.      swapM
10b36 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28  ixedEndianFloat(
10b37 72 32 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  r2);.      asser
10b38 74 28 20 73 69 7a 65 6f 66 28 72 32 29 3d 3d 73  t( sizeof(r2)==s
10b39 69 7a 65 6f 66 28 74 31 29 20 26 26 20 6d 65 6d  izeof(t1) && mem
10b3a 63 6d 70 28 26 72 32 2c 20 26 74 31 2c 20 73 69  cmp(&r2, &t1, si
10b3b 7a 65 6f 66 28 72 31 29 29 3d 3d 30 20 29 3b 0a  zeof(r1))==0 );.
10b3c 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 78 20  #endif..      x 
10b3d 3d 20 28 62 75 66 5b 30 5d 3c 3c 32 34 29 20 7c  = (buf[0]<<24) |
10b3e 20 28 62 75 66 5b 31 5d 3c 3c 31 36 29 20 7c 20   (buf[1]<<16) | 
10b3f 28 62 75 66 5b 32 5d 3c 3c 38 29 20 7c 20 62 75  (buf[2]<<8) | bu
10b40 66 5b 33 5d 3b 0a 20 20 20 20 20 20 79 20 3d 20  f[3];.      y = 
10b41 28 62 75 66 5b 34 5d 3c 3c 32 34 29 20 7c 20 28  (buf[4]<<24) | (
10b42 62 75 66 5b 35 5d 3c 3c 31 36 29 20 7c 20 28 62  buf[5]<<16) | (b
10b43 75 66 5b 36 5d 3c 3c 38 29 20 7c 20 62 75 66 5b  uf[6]<<8) | buf[
10b44 37 5d 3b 0a 20 20 20 20 20 20 78 20 3d 20 28 78  7];.      x = (x
10b45 3c 3c 33 32 29 20 7c 20 79 3b 0a 20 20 20 20 20  <<32) | y;.     
10b46 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79 70 65   if( serial_type
10b47 3d 3d 36 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ==6 ){.        p
10b48 4d 65 6d 2d 3e 75 2e 69 20 3d 20 2a 28 69 36 34  Mem->u.i = *(i64
10b49 2a 29 26 78 3b 0a 20 20 20 20 20 20 20 20 70 4d  *)&x;.        pM
10b4a 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
10b4b 49 6e 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  Int;.      }else
10b4c 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
10b4d 28 20 73 69 7a 65 6f 66 28 78 29 3d 3d 38 20 26  ( sizeof(x)==8 &
10b4e 26 20 73 69 7a 65 6f 66 28 70 4d 65 6d 2d 3e 72  & sizeof(pMem->r
10b4f 29 3d 3d 38 20 29 3b 0a 20 20 20 20 20 20 20 20  )==8 );.        
10b50 6d 65 6d 63 70 79 28 26 70 4d 65 6d 2d 3e 72 2c  memcpy(&pMem->r,
10b51 20 26 78 2c 20 73 69 7a 65 6f 66 28 78 29 29 3b   &x, sizeof(x));
10b52 0a 20 20 20 20 20 20 20 20 73 77 61 70 4d 69 78  .        swapMix
10b53 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 70 4d  edEndianFloat(pM
10b54 65 6d 2d 3e 72 29 3b 0a 20 20 20 20 20 20 20 20  em->r);.        
10b55 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
10b56 4d 5f 52 65 61 6c 3b 0a 20 20 20 20 20 20 7d 0a  M_Real;.      }.
10b57 20 20 20 20 20 20 72 65 74 75 72 6e 20 38 3b 0a        return 8;.
10b58 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 38      }.    case 8
10b59 3a 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20  :    /* Integer 
10b5a 30 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 39 3a  0 */.    case 9:
10b5b 20 7b 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 31   {  /* Integer 1
10b5c 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e   */.      pMem->
10b5d 75 2e 69 20 3d 20 73 65 72 69 61 6c 5f 74 79 70  u.i = serial_typ
10b5e 65 2d 38 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d  e-8;.      pMem-
10b5f 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74  >flags = MEM_Int
10b60 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  ;.      return 0
10b61 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61  ;.    }.    defa
10b62 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74  ult: {.      int
10b63 20 6c 65 6e 20 3d 20 28 73 65 72 69 61 6c 5f 74   len = (serial_t
10b64 79 70 65 2d 31 32 29 2f 32 3b 0a 20 20 20 20 20  ype-12)/2;.     
10b65 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72   pMem->z = (char
10b66 20 2a 29 62 75 66 3b 0a 20 20 20 20 20 20 70 4d   *)buf;.      pM
10b67 65 6d 2d 3e 6e 20 3d 20 6c 65 6e 3b 0a 20 20 20  em->n = len;.   
10b68 20 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20     pMem->xDel = 
10b69 30 3b 0a 20 20 20 20 20 20 69 66 28 20 73 65 72  0;.      if( ser
10b6a 69 61 6c 5f 74 79 70 65 26 30 78 30 31 20 29 7b  ial_type&0x01 ){
10b6b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66  .        pMem->f
10b6c 6c 61 67 73 20 3d 20 4d 45 4d 5f 53 74 72 20 7c  lags = MEM_Str |
10b6d 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20   MEM_Ephem;.    
10b6e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
10b6f 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
10b70 45 4d 5f 42 6c 6f 62 20 7c 20 4d 45 4d 5f 45 70  EM_Blob | MEM_Ep
10b71 68 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  hem;.      }.   
10b72 20 20 20 72 65 74 75 72 6e 20 6c 65 6e 3b 0a 20     return len;. 
10b73 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
10b74 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  n 0;.}../*.** Th
10b75 65 20 68 65 61 64 65 72 20 6f 66 20 61 20 72 65  e header of a re
10b76 63 6f 72 64 20 63 6f 6e 73 69 73 74 73 20 6f 66  cord consists of
10b77 20 61 20 73 65 71 75 65 6e 63 65 20 76 61 72 69   a sequence vari
10b78 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65  able-length inte
10b79 67 65 72 73 2e 0a 2a 2a 20 54 68 65 73 65 20 69  gers..** These i
10b7a 6e 74 65 67 65 72 73 20 61 72 65 20 61 6c 6d 6f  ntegers are almo
10b7b 73 74 20 61 6c 77 61 79 73 20 73 6d 61 6c 6c 20  st always small 
10b7c 61 6e 64 20 61 72 65 20 65 6e 63 6f 64 65 64 20  and are encoded 
10b7d 61 73 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65  as a single byte
10b7e 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ..** The followi
10b7f 6e 67 20 6d 61 63 72 6f 20 74 61 6b 65 73 20 61  ng macro takes a
10b80 64 76 61 6e 74 61 67 65 20 74 68 69 73 20 66 61  dvantage this fa
10b81 63 74 20 74 6f 20 70 72 6f 76 69 64 65 20 61 20  ct to provide a 
10b82 66 61 73 74 20 64 65 63 6f 64 65 0a 2a 2a 20 6f  fast decode.** o
10b83 66 20 74 68 65 20 69 6e 74 65 67 65 72 73 20 69  f the integers i
10b84 6e 20 61 20 72 65 63 6f 72 64 20 68 65 61 64 65  n a record heade
10b85 72 2e 20 20 49 74 20 69 73 20 66 61 73 74 65 72  r.  It is faster
10b86 20 66 6f 72 20 74 68 65 20 63 6f 6d 6d 6f 6e 20   for the common 
10b87 63 61 73 65 0a 2a 2a 20 77 68 65 72 65 20 74 68  case.** where th
10b88 65 20 69 6e 74 65 67 65 72 20 69 73 20 61 20 73  e integer is a s
10b89 69 6e 67 6c 65 20 62 79 74 65 2e 20 20 49 74 20  ingle byte.  It 
10b8a 69 73 20 61 20 6c 69 74 74 6c 65 20 73 6c 6f 77  is a little slow
10b8b 65 72 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 69  er when the.** i
10b8c 6e 74 65 67 65 72 20 69 73 20 74 77 6f 20 6f 72  nteger is two or
10b8d 20 6d 6f 72 65 20 62 79 74 65 73 2e 20 20 42 75   more bytes.  Bu
10b8e 74 20 6f 76 65 72 61 6c 6c 20 69 74 20 69 73 20  t overall it is 
10b8f 66 61 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  faster..**.** Th
10b90 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 78 70 72  e following expr
10b91 65 73 73 69 6f 6e 73 20 61 72 65 20 65 71 75 69  essions are equi
10b92 76 61 6c 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20  valent:.**.**   
10b93 20 20 78 20 3d 20 73 71 6c 69 74 65 33 47 65 74    x = sqlite3Get
10b94 56 61 72 69 6e 74 33 32 28 20 41 2c 20 26 42 20  Varint32( A, &B 
10b95 29 3b 0a 2a 2a 0a 2a 2a 20 20 20 20 20 78 20 3d  );.**.**     x =
10b96 20 47 65 74 56 61 72 69 6e 74 28 20 41 2c 20 42   GetVarint( A, B
10b97 20 29 3b 0a 2a 2a 0a 2a 2f 0a 23 64 65 66 69 6e   );.**.*/.#defin
10b98 65 20 47 65 74 56 61 72 69 6e 74 28 41 2c 42 29  e GetVarint(A,B)
10b99 20 20 28 28 42 20 3d 20 2a 28 41 29 29 3c 3d 30    ((B = *(A))<=0
10b9a 78 37 66 20 3f 20 31 20 3a 20 73 71 6c 69 74 65  x7f ? 1 : sqlite
10b9b 33 47 65 74 56 61 72 69 6e 74 33 32 28 41 2c 20  3GetVarint32(A, 
10b9c 26 42 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  &B))../*.** This
10b9d 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d 70 61 72   function compar
10b9e 65 73 20 74 68 65 20 74 77 6f 20 74 61 62 6c 65  es the two table
10b9f 20 72 6f 77 73 20 6f 72 20 69 6e 64 65 78 20 72   rows or index r
10ba0 65 63 6f 72 64 73 20 73 70 65 63 69 66 69 65 64  ecords specified
10ba1 20 62 79 20 0a 2a 2a 20 7b 6e 4b 65 79 31 2c 20   by .** {nKey1, 
10ba2 70 4b 65 79 31 7d 20 61 6e 64 20 7b 6e 4b 65 79  pKey1} and {nKey
10ba3 32 2c 20 70 4b 65 79 32 7d 2c 20 72 65 74 75 72  2, pKey2}, retur
10ba4 6e 69 6e 67 20 61 20 6e 65 67 61 74 69 76 65 2c  ning a negative,
10ba5 20 7a 65 72 6f 0a 2a 2a 20 6f 72 20 70 6f 73 69   zero.** or posi
10ba6 74 69 76 65 20 69 6e 74 65 67 65 72 20 69 66 20  tive integer if 
10ba7 7b 6e 4b 65 79 31 2c 20 70 4b 65 79 31 7d 20 69  {nKey1, pKey1} i
10ba8 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75  s less than, equ
10ba9 61 6c 20 74 6f 20 6f 72 20 0a 2a 2a 20 67 72 65  al to or .** gre
10baa 61 74 65 72 20 74 68 61 6e 20 7b 6e 4b 65 79 32  ater than {nKey2
10bab 2c 20 70 4b 65 79 32 7d 2e 20 20 42 6f 74 68 20  , pKey2}.  Both 
10bac 4b 65 79 31 20 61 6e 64 20 4b 65 79 32 20 6d 75  Key1 and Key2 mu
10bad 73 74 20 62 65 20 62 79 74 65 20 73 74 72 69 6e  st be byte strin
10bae 67 73 0a 2a 2a 20 63 6f 6d 70 6f 73 65 64 20 62  gs.** composed b
10baf 79 20 74 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63  y the OP_MakeRec
10bb0 6f 72 64 20 6f 70 63 6f 64 65 20 6f 66 20 74 68  ord opcode of th
10bb1 65 20 56 44 42 45 2e 0a 2a 2f 0a 53 51 4c 49 54  e VDBE..*/.SQLIT
10bb2 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
10bb3 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 43  lite3VdbeRecordC
10bb4 6f 6d 70 61 72 65 28 0a 20 20 76 6f 69 64 20 2a  ompare(.  void *
10bb5 75 73 65 72 44 61 74 61 2c 0a 20 20 69 6e 74 20  userData,.  int 
10bb6 6e 4b 65 79 31 2c 20 63 6f 6e 73 74 20 76 6f 69  nKey1, const voi
10bb7 64 20 2a 70 4b 65 79 31 2c 20 0a 20 20 69 6e 74  d *pKey1, .  int
10bb8 20 6e 4b 65 79 32 2c 20 63 6f 6e 73 74 20 76 6f   nKey2, const vo
10bb9 69 64 20 2a 70 4b 65 79 32 0a 29 7b 0a 20 20 4b  id *pKey2.){.  K
10bba 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f  eyInfo *pKeyInfo
10bbb 20 3d 20 28 4b 65 79 49 6e 66 6f 2a 29 75 73 65   = (KeyInfo*)use
10bbc 72 44 61 74 61 3b 0a 20 20 75 33 32 20 64 31 2c  rData;.  u32 d1,
10bbd 20 64 32 3b 20 20 20 20 20 20 20 20 20 20 2f 2a   d2;          /*
10bbe 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 61 4b 65   Offset into aKe
10bbf 79 5b 5d 20 6f 66 20 6e 65 78 74 20 64 61 74 61  y[] of next data
10bc0 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 75 33   element */.  u3
10bc1 32 20 69 64 78 31 2c 20 69 64 78 32 3b 20 20 20  2 idx1, idx2;   
10bc2 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74     /* Offset int
10bc3 6f 20 61 4b 65 79 5b 5d 20 6f 66 20 6e 65 78 74  o aKey[] of next
10bc4 20 68 65 61 64 65 72 20 65 6c 65 6d 65 6e 74 20   header element 
10bc5 2a 2f 0a 20 20 75 33 32 20 73 7a 48 64 72 31 2c  */.  u32 szHdr1,
10bc6 20 73 7a 48 64 72 32 3b 20 20 2f 2a 20 4e 75 6d   szHdr2;  /* Num
10bc7 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
10bc8 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e 74 20  header */.  int 
10bc9 69 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 46 69  i = 0;.  int nFi
10bca 65 6c 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  eld;.  int rc = 
10bcb 30 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  0;.  const unsig
10bcc 6e 65 64 20 63 68 61 72 20 2a 61 4b 65 79 31 20  ned char *aKey1 
10bcd 3d 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  = (const unsigne
10bce 64 20 63 68 61 72 20 2a 29 70 4b 65 79 31 3b 0a  d char *)pKey1;.
10bcf 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
10bd0 20 63 68 61 72 20 2a 61 4b 65 79 32 20 3d 20 28   char *aKey2 = (
10bd1 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
10bd2 68 61 72 20 2a 29 70 4b 65 79 32 3b 0a 0a 20 20  har *)pKey2;..  
10bd3 4d 65 6d 20 6d 65 6d 31 3b 0a 20 20 4d 65 6d 20  Mem mem1;.  Mem 
10bd4 6d 65 6d 32 3b 0a 20 20 6d 65 6d 31 2e 65 6e 63  mem2;.  mem1.enc
10bd5 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63   = pKeyInfo->enc
10bd6 3b 0a 20 20 6d 65 6d 32 2e 65 6e 63 20 3d 20 70  ;.  mem2.enc = p
10bd7 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0a 20 20  KeyInfo->enc;.  
10bd8 0a 20 20 69 64 78 31 20 3d 20 47 65 74 56 61 72  .  idx1 = GetVar
10bd9 69 6e 74 28 61 4b 65 79 31 2c 20 73 7a 48 64 72  int(aKey1, szHdr
10bda 31 29 3b 0a 20 20 64 31 20 3d 20 73 7a 48 64 72  1);.  d1 = szHdr
10bdb 31 3b 0a 20 20 69 64 78 32 20 3d 20 47 65 74 56  1;.  idx2 = GetV
10bdc 61 72 69 6e 74 28 61 4b 65 79 32 2c 20 73 7a 48  arint(aKey2, szH
10bdd 64 72 32 29 3b 0a 20 20 64 32 20 3d 20 73 7a 48  dr2);.  d2 = szH
10bde 64 72 32 3b 0a 20 20 6e 46 69 65 6c 64 20 3d 20  dr2;.  nField = 
10bdf 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64  pKeyInfo->nField
10be0 3b 0a 20 20 77 68 69 6c 65 28 20 69 64 78 31 3c  ;.  while( idx1<
10be1 73 7a 48 64 72 31 20 26 26 20 69 64 78 32 3c 73  szHdr1 && idx2<s
10be2 7a 48 64 72 32 20 29 7b 0a 20 20 20 20 75 33 32  zHdr2 ){.    u32
10be3 20 73 65 72 69 61 6c 5f 74 79 70 65 31 3b 0a 20   serial_type1;. 
10be4 20 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79     u32 serial_ty
10be5 70 65 32 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61  pe2;..    /* Rea
10be6 64 20 74 68 65 20 73 65 72 69 61 6c 20 74 79 70  d the serial typ
10be7 65 73 20 66 6f 72 20 74 68 65 20 6e 65 78 74 20  es for the next 
10be8 65 6c 65 6d 65 6e 74 20 69 6e 20 65 61 63 68 20  element in each 
10be9 6b 65 79 2e 20 2a 2f 0a 20 20 20 20 69 64 78 31  key. */.    idx1
10bea 20 2b 3d 20 47 65 74 56 61 72 69 6e 74 28 20 61   += GetVarint( a
10beb 4b 65 79 31 2b 69 64 78 31 2c 20 73 65 72 69 61  Key1+idx1, seria
10bec 6c 5f 74 79 70 65 31 20 29 3b 0a 20 20 20 20 69  l_type1 );.    i
10bed 66 28 20 64 31 3e 3d 6e 4b 65 79 31 20 26 26 20  f( d1>=nKey1 && 
10bee 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
10bef 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61 6c 5f  lTypeLen(serial_
10bf0 74 79 70 65 31 29 3e 30 20 29 20 62 72 65 61 6b  type1)>0 ) break
10bf1 3b 0a 20 20 20 20 69 64 78 32 20 2b 3d 20 47 65  ;.    idx2 += Ge
10bf2 74 56 61 72 69 6e 74 28 20 61 4b 65 79 32 2b 69  tVarint( aKey2+i
10bf3 64 78 32 2c 20 73 65 72 69 61 6c 5f 74 79 70 65  dx2, serial_type
10bf4 32 20 29 3b 0a 20 20 20 20 69 66 28 20 64 32 3e  2 );.    if( d2>
10bf5 3d 6e 4b 65 79 32 20 26 26 20 73 71 6c 69 74 65  =nKey2 && sqlite
10bf6 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c  3VdbeSerialTypeL
10bf7 65 6e 28 73 65 72 69 61 6c 5f 74 79 70 65 32 29  en(serial_type2)
10bf8 3e 30 20 29 20 62 72 65 61 6b 3b 0a 0a 20 20 20  >0 ) break;..   
10bf9 20 2f 2a 20 45 78 74 72 61 63 74 20 74 68 65 20   /* Extract the 
10bfa 76 61 6c 75 65 73 20 74 6f 20 62 65 20 63 6f 6d  values to be com
10bfb 70 61 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20  pared..    */.  
10bfc 20 20 64 31 20 2b 3d 20 73 71 6c 69 74 65 33 56    d1 += sqlite3V
10bfd 64 62 65 53 65 72 69 61 6c 47 65 74 28 26 61 4b  dbeSerialGet(&aK
10bfe 65 79 31 5b 64 31 5d 2c 20 73 65 72 69 61 6c 5f  ey1[d1], serial_
10bff 74 79 70 65 31 2c 20 26 6d 65 6d 31 29 3b 0a 20  type1, &mem1);. 
10c00 20 20 20 64 32 20 2b 3d 20 73 71 6c 69 74 65 33     d2 += sqlite3
10c01 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 26 61  VdbeSerialGet(&a
10c02 4b 65 79 32 5b 64 32 5d 2c 20 73 65 72 69 61 6c  Key2[d2], serial
10c03 5f 74 79 70 65 32 2c 20 26 6d 65 6d 32 29 3b 0a  _type2, &mem2);.
10c04 0a 20 20 20 20 2f 2a 20 44 6f 20 74 68 65 20 63  .    /* Do the c
10c05 6f 6d 70 61 72 69 73 6f 6e 0a 20 20 20 20 2a 2f  omparison.    */
10c06 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
10c07 33 4d 65 6d 43 6f 6d 70 61 72 65 28 26 6d 65 6d  3MemCompare(&mem
10c08 31 2c 20 26 6d 65 6d 32 2c 20 69 3c 6e 46 69 65  1, &mem2, i<nFie
10c09 6c 64 20 3f 20 70 4b 65 79 49 6e 66 6f 2d 3e 61  ld ? pKeyInfo->a
10c0a 43 6f 6c 6c 5b 69 5d 20 3a 20 30 29 3b 0a 20 20  Coll[i] : 0);.  
10c0b 20 20 69 66 28 20 6d 65 6d 31 2e 66 6c 61 67 73    if( mem1.flags
10c0c 20 26 20 4d 45 4d 5f 44 79 6e 20 29 20 73 71 6c   & MEM_Dyn ) sql
10c0d 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
10c0e 73 65 28 26 6d 65 6d 31 29 3b 0a 20 20 20 20 69  se(&mem1);.    i
10c0f 66 28 20 6d 65 6d 32 2e 66 6c 61 67 73 20 26 20  f( mem2.flags & 
10c10 4d 45 4d 5f 44 79 6e 20 29 20 73 71 6c 69 74 65  MEM_Dyn ) sqlite
10c11 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
10c12 26 6d 65 6d 32 29 3b 0a 20 20 20 20 69 66 28 20  &mem2);.    if( 
10c13 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 62  rc!=0 ){.      b
10c14 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
10c15 69 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f  i++;.  }..  /* O
10c16 6e 65 20 6f 66 20 74 68 65 20 6b 65 79 73 20 72  ne of the keys r
10c17 61 6e 20 6f 75 74 20 6f 66 20 66 69 65 6c 64 73  an out of fields
10c18 2c 20 62 75 74 20 61 6c 6c 20 74 68 65 20 66 69  , but all the fi
10c19 65 6c 64 73 20 75 70 20 74 6f 20 74 68 61 74 20  elds up to that 
10c1a 70 6f 69 6e 74 0a 20 20 2a 2a 20 77 65 72 65 20  point.  ** were 
10c1b 65 71 75 61 6c 2e 20 49 66 20 74 68 65 20 69 6e  equal. If the in
10c1c 63 72 4b 65 79 20 66 6c 61 67 20 69 73 20 74 72  crKey flag is tr
10c1d 75 65 2c 20 74 68 65 6e 20 74 68 65 20 73 65 63  ue, then the sec
10c1e 6f 6e 64 20 6b 65 79 20 69 73 0a 20 20 2a 2a 20  ond key is.  ** 
10c1f 74 72 65 61 74 65 64 20 61 73 20 6c 61 72 67 65  treated as large
10c20 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63  r..  */.  if( rc
10c21 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70  ==0 ){.    if( p
10c22 4b 65 79 49 6e 66 6f 2d 3e 69 6e 63 72 4b 65 79  KeyInfo->incrKey
10c23 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 2d   ){.      rc = -
10c24 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  1;.    }else if(
10c25 20 64 31 3c 6e 4b 65 79 31 20 29 7b 0a 20 20 20   d1<nKey1 ){.   
10c26 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 7d     rc = 1;.    }
10c27 65 6c 73 65 20 69 66 28 20 64 32 3c 6e 4b 65 79  else if( d2<nKey
10c28 32 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  2 ){.      rc = 
10c29 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  -1;.    }.  }els
10c2a 65 20 69 66 28 20 70 4b 65 79 49 6e 66 6f 2d 3e  e if( pKeyInfo->
10c2b 61 53 6f 72 74 4f 72 64 65 72 20 26 26 20 69 3c  aSortOrder && i<
10c2c 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64  pKeyInfo->nField
10c2d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
10c2e 26 26 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f  && pKeyInfo->aSo
10c2f 72 74 4f 72 64 65 72 5b 69 5d 20 29 7b 0a 20 20  rtOrder[i] ){.  
10c30 20 20 72 63 20 3d 20 2d 72 63 3b 0a 20 20 7d 0a    rc = -rc;.  }.
10c31 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
10c32 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d  ./*.** The argum
10c33 65 6e 74 20 69 73 20 61 6e 20 69 6e 64 65 78 20  ent is an index 
10c34 65 6e 74 72 79 20 63 6f 6d 70 6f 73 65 64 20 75  entry composed u
10c35 73 69 6e 67 20 74 68 65 20 4f 50 5f 4d 61 6b 65  sing the OP_Make
10c36 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 2e 0a 2a  Record opcode..*
10c37 2a 20 54 68 65 20 6c 61 73 74 20 65 6e 74 72 79  * The last entry
10c38 20 69 6e 20 74 68 69 73 20 72 65 63 6f 72 64 20   in this record 
10c39 73 68 6f 75 6c 64 20 62 65 20 61 6e 20 69 6e 74  should be an int
10c3a 65 67 65 72 20 28 73 70 65 63 69 66 69 63 61 6c  eger (specifical
10c3b 6c 79 0a 2a 2a 20 61 6e 20 69 6e 74 65 67 65 72  ly.** an integer
10c3c 20 72 6f 77 69 64 29 2e 20 20 54 68 69 73 20 72   rowid).  This r
10c3d 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74  outine returns t
10c3e 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
10c3f 65 73 20 69 6e 0a 2a 2a 20 74 68 61 74 20 69 6e  es in.** that in
10c40 74 65 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  teger..*/.SQLITE
10c41 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
10c42 69 74 65 33 56 64 62 65 49 64 78 52 6f 77 69 64  ite3VdbeIdxRowid
10c43 4c 65 6e 28 63 6f 6e 73 74 20 75 38 20 2a 61 4b  Len(const u8 *aK
10c44 65 79 29 7b 0a 20 20 75 33 32 20 73 7a 48 64 72  ey){.  u32 szHdr
10c45 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65  ;        /* Size
10c46 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 20 2a   of the header *
10c47 2f 0a 20 20 75 33 32 20 74 79 70 65 52 6f 77 69  /.  u32 typeRowi
10c48 64 3b 20 20 20 20 2f 2a 20 53 65 72 69 61 6c 20  d;    /* Serial 
10c49 74 79 70 65 20 6f 66 20 74 68 65 20 72 6f 77 69  type of the rowi
10c4a 64 20 2a 2f 0a 0a 20 20 73 71 6c 69 74 65 33 47  d */..  sqlite3G
10c4b 65 74 56 61 72 69 6e 74 33 32 28 61 4b 65 79 2c  etVarint32(aKey,
10c4c 20 26 73 7a 48 64 72 29 3b 0a 20 20 73 71 6c 69   &szHdr);.  sqli
10c4d 74 65 33 47 65 74 56 61 72 69 6e 74 33 32 28 26  te3GetVarint32(&
10c4e 61 4b 65 79 5b 73 7a 48 64 72 2d 31 5d 2c 20 26  aKey[szHdr-1], &
10c4f 74 79 70 65 52 6f 77 69 64 29 3b 0a 20 20 72 65  typeRowid);.  re
10c50 74 75 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65  turn sqlite3Vdbe
10c51 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 74 79  SerialTypeLen(ty
10c52 70 65 52 6f 77 69 64 29 3b 0a 7d 0a 20 20 0a 0a  peRowid);.}.  ..
10c53 2f 2a 0a 2a 2a 20 70 43 75 72 20 70 6f 69 6e 74  /*.** pCur point
10c54 73 20 61 74 20 61 6e 20 69 6e 64 65 78 20 65 6e  s at an index en
10c55 74 72 79 20 63 72 65 61 74 65 64 20 75 73 69 6e  try created usin
10c56 67 20 74 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63  g the OP_MakeRec
10c57 6f 72 64 20 6f 70 63 6f 64 65 2e 0a 2a 2a 20 52  ord opcode..** R
10c58 65 61 64 20 74 68 65 20 72 6f 77 69 64 20 28 74  ead the rowid (t
10c59 68 65 20 6c 61 73 74 20 66 69 65 6c 64 20 69 6e  he last field in
10c5a 20 74 68 65 20 72 65 63 6f 72 64 29 20 61 6e 64   the record) and
10c5b 20 73 74 6f 72 65 20 69 74 20 69 6e 20 2a 72 6f   store it in *ro
10c5c 77 69 64 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 53  wid..** Return S
10c5d 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72  QLITE_OK if ever
10c5e 79 74 68 69 6e 67 20 77 6f 72 6b 73 2c 20 6f 72  ything works, or
10c5f 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 6f   an error code o
10c60 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c  therwise..*/.SQL
10c61 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
10c62 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 52 6f  sqlite3VdbeIdxRo
10c63 77 69 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43  wid(BtCursor *pC
10c64 75 72 2c 20 69 36 34 20 2a 72 6f 77 69 64 29 7b  ur, i64 *rowid){
10c65 0a 20 20 69 36 34 20 6e 43 65 6c 6c 4b 65 79 20  .  i64 nCellKey 
10c66 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  = 0;.  int rc;. 
10c67 20 75 33 32 20 73 7a 48 64 72 3b 20 20 20 20 20   u32 szHdr;     
10c68 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68     /* Size of th
10c69 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 75 33  e header */.  u3
10c6a 32 20 74 79 70 65 52 6f 77 69 64 3b 20 20 20 20  2 typeRowid;    
10c6b 2f 2a 20 53 65 72 69 61 6c 20 74 79 70 65 20 6f  /* Serial type o
10c6c 66 20 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20  f the rowid */. 
10c6d 20 75 33 32 20 6c 65 6e 52 6f 77 69 64 3b 20 20   u32 lenRowid;  
10c6e 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68     /* Size of th
10c6f 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20 4d 65 6d  e rowid */.  Mem
10c70 20 6d 2c 20 76 3b 0a 0a 20 20 73 71 6c 69 74 65   m, v;..  sqlite
10c71 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43  3BtreeKeySize(pC
10c72 75 72 2c 20 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a  ur, &nCellKey);.
10c73 20 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3c 3d    if( nCellKey<=
10c74 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
10c75 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
10c76 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20  KPT;.  }.  rc = 
10c77 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72  sqlite3VdbeMemFr
10c78 6f 6d 42 74 72 65 65 28 70 43 75 72 2c 20 30 2c  omBtree(pCur, 0,
10c79 20 6e 43 65 6c 6c 4b 65 79 2c 20 31 2c 20 26 6d   nCellKey, 1, &m
10c7a 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20  );.  if( rc ){. 
10c7b 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
10c7c 7d 0a 20 20 73 71 6c 69 74 65 33 47 65 74 56 61  }.  sqlite3GetVa
10c7d 72 69 6e 74 33 32 28 28 75 38 2a 29 6d 2e 7a 2c  rint32((u8*)m.z,
10c7e 20 26 73 7a 48 64 72 29 3b 0a 20 20 73 71 6c 69   &szHdr);.  sqli
10c7f 74 65 33 47 65 74 56 61 72 69 6e 74 33 32 28 28  te3GetVarint32((
10c80 75 38 2a 29 26 6d 2e 7a 5b 73 7a 48 64 72 2d 31  u8*)&m.z[szHdr-1
10c81 5d 2c 20 26 74 79 70 65 52 6f 77 69 64 29 3b 0a  ], &typeRowid);.
10c82 20 20 6c 65 6e 52 6f 77 69 64 20 3d 20 73 71 6c    lenRowid = sql
10c83 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79  ite3VdbeSerialTy
10c84 70 65 4c 65 6e 28 74 79 70 65 52 6f 77 69 64 29  peLen(typeRowid)
10c85 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 53  ;.  sqlite3VdbeS
10c86 65 72 69 61 6c 47 65 74 28 28 75 38 2a 29 26 6d  erialGet((u8*)&m
10c87 2e 7a 5b 6d 2e 6e 2d 6c 65 6e 52 6f 77 69 64 5d  .z[m.n-lenRowid]
10c88 2c 20 74 79 70 65 52 6f 77 69 64 2c 20 26 76 29  , typeRowid, &v)
10c89 3b 0a 20 20 2a 72 6f 77 69 64 20 3d 20 76 2e 75  ;.  *rowid = v.u
10c8a 2e 69 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  .i;.  sqlite3Vdb
10c8b 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b  eMemRelease(&m);
10c8c 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
10c8d 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  _OK;.}../*.** Co
10c8e 6d 70 61 72 65 20 74 68 65 20 6b 65 79 20 6f 66  mpare the key of
10c8f 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79   the index entry
10c90 20 74 68 61 74 20 63 75 72 73 6f 72 20 70 43 20   that cursor pC 
10c91 69 73 20 70 6f 69 6e 74 20 74 6f 20 61 67 61 69  is point to agai
10c92 6e 73 74 0a 2a 2a 20 74 68 65 20 6b 65 79 20 73  nst.** the key s
10c93 74 72 69 6e 67 20 69 6e 20 70 4b 65 79 20 28 6f  tring in pKey (o
10c94 66 20 6c 65 6e 67 74 68 20 6e 4b 65 79 29 2e 20  f length nKey). 
10c95 20 57 72 69 74 65 20 69 6e 74 6f 20 2a 70 52 65   Write into *pRe
10c96 73 20 61 20 6e 75 6d 62 65 72 0a 2a 2a 20 74 68  s a number.** th
10c97 61 74 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20  at is negative, 
10c98 7a 65 72 6f 2c 20 6f 72 20 70 6f 73 69 74 69 76  zero, or positiv
10c99 65 20 69 66 20 70 43 20 69 73 20 6c 65 73 73 20  e if pC is less 
10c9a 74 68 61 6e 2c 20 65 71 75 61 6c 20 74 6f 2c 0a  than, equal to,.
10c9b 2a 2a 20 6f 72 20 67 72 65 61 74 65 72 20 74 68  ** or greater th
10c9c 61 6e 20 70 4b 65 79 2e 20 20 52 65 74 75 72 6e  an pKey.  Return
10c9d 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75   SQLITE_OK on su
10c9e 63 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 70 4b 65  ccess..**.** pKe
10c9f 79 20 69 73 20 65 69 74 68 65 72 20 63 72 65 61  y is either crea
10ca0 74 65 64 20 77 69 74 68 6f 75 74 20 61 20 72 6f  ted without a ro
10ca1 77 69 64 20 6f 72 20 69 73 20 74 72 75 6e 63 61  wid or is trunca
10ca2 74 65 64 20 73 6f 20 74 68 61 74 20 69 74 0a 2a  ted so that it.*
10ca3 2a 20 6f 6d 69 74 73 20 74 68 65 20 72 6f 77 69  * omits the rowi
10ca4 64 20 61 74 20 74 68 65 20 65 6e 64 2e 20 20 54  d at the end.  T
10ca5 68 65 20 72 6f 77 69 64 20 61 74 20 74 68 65 20  he rowid at the 
10ca6 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78  end of the index
10ca7 20 65 6e 74 72 79 0a 2a 2a 20 69 73 20 69 67 6e   entry.** is ign
10ca8 6f 72 65 64 20 61 73 20 77 65 6c 6c 2e 0a 2a 2f  ored as well..*/
10ca9 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
10caa 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 49  int sqlite3VdbeI
10cab 64 78 4b 65 79 43 6f 6d 70 61 72 65 28 0a 20 20  dxKeyCompare(.  
10cac 43 75 72 73 6f 72 20 2a 70 43 2c 20 20 20 20 20  Cursor *pC,     
10cad 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
10cae 68 65 20 63 75 72 73 6f 72 20 74 6f 20 63 6f 6d  he cursor to com
10caf 70 61 72 65 20 61 67 61 69 6e 73 74 20 2a 2f 0a  pare against */.
10cb0 20 20 69 6e 74 20 6e 4b 65 79 2c 20 63 6f 6e 73    int nKey, cons
10cb1 74 20 75 38 20 2a 70 4b 65 79 2c 20 20 20 2f 2a  t u8 *pKey,   /*
10cb2 20 54 68 65 20 6b 65 79 20 74 6f 20 63 6f 6d 70   The key to comp
10cb3 61 72 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 72 65  are */.  int *re
10cb4 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s               
10cb5 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68       /* Write th
10cb6 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 72 65 73  e comparison res
10cb7 75 6c 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  ult here */.){. 
10cb8 20 69 36 34 20 6e 43 65 6c 6c 4b 65 79 20 3d 20   i64 nCellKey = 
10cb9 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42  0;.  int rc;.  B
10cba 74 43 75 72 73 6f 72 20 2a 70 43 75 72 20 3d 20  tCursor *pCur = 
10cbb 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69  pC->pCursor;.  i
10cbc 6e 74 20 6c 65 6e 52 6f 77 69 64 3b 0a 20 20 4d  nt lenRowid;.  M
10cbd 65 6d 20 6d 3b 0a 0a 20 20 73 71 6c 69 74 65 33  em m;..  sqlite3
10cbe 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 75  BtreeKeySize(pCu
10cbf 72 2c 20 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20  r, &nCellKey);. 
10cc0 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3c 3d 30   if( nCellKey<=0
10cc1 20 29 7b 0a 20 20 20 20 2a 72 65 73 20 3d 20 30   ){.    *res = 0
10cc2 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
10cc3 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 63  ITE_OK;.  }.  rc
10cc4 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65   = sqlite3VdbeMe
10cc5 6d 46 72 6f 6d 42 74 72 65 65 28 70 43 2d 3e 70  mFromBtree(pC->p
10cc6 43 75 72 73 6f 72 2c 20 30 2c 20 6e 43 65 6c 6c  Cursor, 0, nCell
10cc7 4b 65 79 2c 20 31 2c 20 26 6d 29 3b 0a 20 20 69  Key, 1, &m);.  i
10cc8 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74  f( rc ){.    ret
10cc9 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 6c 65  urn rc;.  }.  le
10cca 6e 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33  nRowid = sqlite3
10ccb 56 64 62 65 49 64 78 52 6f 77 69 64 4c 65 6e 28  VdbeIdxRowidLen(
10ccc 28 75 38 2a 29 6d 2e 7a 29 3b 0a 20 20 2a 72 65  (u8*)m.z);.  *re
10ccd 73 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52  s = sqlite3VdbeR
10cce 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 70 43 2d  ecordCompare(pC-
10ccf 3e 70 4b 65 79 49 6e 66 6f 2c 20 6d 2e 6e 2d 6c  >pKeyInfo, m.n-l
10cd0 65 6e 52 6f 77 69 64 2c 20 6d 2e 7a 2c 20 6e 4b  enRowid, m.z, nK
10cd1 65 79 2c 20 70 4b 65 79 29 3b 0a 20 20 73 71 6c  ey, pKey);.  sql
10cd2 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
10cd3 73 65 28 26 6d 29 3b 0a 20 20 72 65 74 75 72 6e  se(&m);.  return
10cd4 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
10cd5 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
10cd6 65 20 73 65 74 73 20 74 68 65 20 76 61 6c 75 65  e sets the value
10cd7 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20   to be returned 
10cd8 62 79 20 73 75 62 73 65 71 75 65 6e 74 20 63 61  by subsequent ca
10cd9 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65  lls to.** sqlite
10cda 33 5f 63 68 61 6e 67 65 73 28 29 20 6f 6e 20 74  3_changes() on t
10cdb 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  he database hand
10cdc 6c 65 20 27 64 62 27 2e 20 0a 2a 2f 0a 53 51 4c  le 'db'. .*/.SQL
10cdd 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
10cde 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
10cdf 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 20 2a  hanges(sqlite3 *
10ce0 64 62 2c 20 69 6e 74 20 6e 43 68 61 6e 67 65 29  db, int nChange)
10ce1 7b 0a 20 20 64 62 2d 3e 6e 43 68 61 6e 67 65 20  {.  db->nChange 
10ce2 3d 20 6e 43 68 61 6e 67 65 3b 0a 20 20 64 62 2d  = nChange;.  db-
10ce3 3e 6e 54 6f 74 61 6c 43 68 61 6e 67 65 20 2b 3d  >nTotalChange +=
10ce4 20 6e 43 68 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a 0a   nChange;.}../*.
10ce5 2a 2a 20 53 65 74 20 61 20 66 6c 61 67 20 69 6e  ** Set a flag in
10ce6 20 74 68 65 20 76 64 62 65 20 74 6f 20 75 70 64   the vdbe to upd
10ce7 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 20 63  ate the change c
10ce8 6f 75 6e 74 65 72 20 77 68 65 6e 20 69 74 20 69  ounter when it i
10ce9 73 20 66 69 6e 61 6c 69 73 65 64 0a 2a 2a 20 6f  s finalised.** o
10cea 72 20 72 65 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49  r reset..*/.SQLI
10ceb 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
10cec 73 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e 74  sqlite3VdbeCount
10ced 43 68 61 6e 67 65 73 28 56 64 62 65 20 2a 76 29  Changes(Vdbe *v)
10cee 7b 0a 20 20 76 2d 3e 63 68 61 6e 67 65 43 6e 74  {.  v->changeCnt
10cef 4f 6e 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  On = 1;.}../*.**
10cf0 20 4d 61 72 6b 20 65 76 65 72 79 20 70 72 65 70   Mark every prep
10cf1 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 61  ared statement a
10cf2 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61  ssociated with a
10cf3 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
10cf4 74 69 6f 6e 0a 2a 2a 20 61 73 20 65 78 70 69 72  tion.** as expir
10cf5 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 70  ed..**.** An exp
10cf6 69 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 6d  ired statement m
10cf7 65 61 6e 73 20 74 68 61 74 20 72 65 63 6f 6d 70  eans that recomp
10cf8 69 6c 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73  ilation of the s
10cf9 74 61 74 65 6d 65 6e 74 20 69 73 0a 2a 2a 20 72  tatement is.** r
10cfa 65 63 6f 6d 6d 65 6e 64 2e 20 20 53 74 61 74 65  ecommend.  State
10cfb 6d 65 6e 74 73 20 65 78 70 69 72 65 20 77 68 65  ments expire whe
10cfc 6e 20 74 68 69 6e 67 73 20 68 61 70 70 65 6e 20  n things happen 
10cfd 74 68 61 74 20 6d 61 6b 65 20 74 68 65 69 72 0a  that make their.
10cfe 2a 2a 20 70 72 6f 67 72 61 6d 73 20 6f 62 73 6f  ** programs obso
10cff 6c 65 74 65 2e 20 20 52 65 6d 6f 76 69 6e 67 20  lete.  Removing 
10d00 75 73 65 72 2d 64 65 66 69 6e 65 64 20 66 75 6e  user-defined fun
10d01 63 74 69 6f 6e 73 20 6f 72 20 63 6f 6c 6c 61 74  ctions or collat
10d02 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 73  ing.** sequences
10d03 2c 20 6f 72 20 63 68 61 6e 67 69 6e 67 20 61 6e  , or changing an
10d04 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66   authorization f
10d05 75 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 65 20  unction are the 
10d06 74 79 70 65 73 20 6f 66 0a 2a 2a 20 74 68 69 6e  types of.** thin
10d07 67 73 20 74 68 61 74 20 6d 61 6b 65 20 70 72 65  gs that make pre
10d08 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73  pared statements
10d09 20 6f 62 73 6f 6c 65 74 65 2e 0a 2a 2f 0a 53 51   obsolete..*/.SQ
10d0a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
10d0b 64 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 50  d sqlite3ExpireP
10d0c 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74  reparedStatement
10d0d 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  s(sqlite3 *db){.
10d0e 20 20 56 64 62 65 20 2a 70 3b 0a 20 20 66 6f 72    Vdbe *p;.  for
10d0f 28 70 20 3d 20 64 62 2d 3e 70 56 64 62 65 3b 20  (p = db->pVdbe; 
10d10 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a  p; p=p->pNext){.
10d11 20 20 20 20 70 2d 3e 65 78 70 69 72 65 64 20 3d      p->expired =
10d12 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a   1;.  }.}../*.**
10d13 20 52 65 74 75 72 6e 20 74 68 65 20 64 61 74 61   Return the data
10d14 62 61 73 65 20 61 73 73 6f 63 69 61 74 65 64 20  base associated 
10d15 77 69 74 68 20 74 68 65 20 56 64 62 65 2e 0a 2a  with the Vdbe..*
10d16 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
10d17 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65   sqlite3 *sqlite
10d18 33 56 64 62 65 44 62 28 56 64 62 65 20 2a 76 29  3VdbeDb(Vdbe *v)
10d19 7b 0a 20 20 72 65 74 75 72 6e 20 76 2d 3e 64 62  {.  return v->db
10d1a 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;.}../**********
10d1b 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65  **** End of vdbe
10d1c 61 75 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  aux.c **********
10d1d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d1e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d1f 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
10d20 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
10d21 76 64 62 65 61 70 69 2e 63 20 2a 2a 2a 2a 2a 2a  vdbeapi.c ******
10d22 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d23 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d24 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20  ***/./*.** 2004 
10d25 4d 61 79 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65  May 26.**.** The
10d26 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
10d27 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
10d28 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
10d29 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
10d2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
10d2b 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
10d2c 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
10d2d 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
10d2e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
10d2f 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
10d30 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
10d31 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
10d32 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
10d33 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
10d34 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
10d35 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
10d36 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
10d37 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d38 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d39 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d3a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d3b 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
10d3c 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
10d3d 6f 64 65 20 75 73 65 20 74 6f 20 69 6d 70 6c 65  ode use to imple
10d3e 6d 65 6e 74 20 41 50 49 73 20 74 68 61 74 20 61  ment APIs that a
10d3f 72 65 20 70 61 72 74 20 6f 66 20 74 68 65 0a 2a  re part of the.*
10d40 2a 20 56 44 42 45 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a  * VDBE..*/../*.*
10d41 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 28 6e  * Return TRUE (n
10d42 6f 6e 2d 7a 65 72 6f 29 20 6f 66 20 74 68 65 20  on-zero) of the 
10d43 73 74 61 74 65 6d 65 6e 74 20 73 75 70 70 6c 69  statement suppli
10d44 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e  ed as an argumen
10d45 74 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 62 65  t needs.** to be
10d46 20 72 65 63 6f 6d 70 69 6c 65 64 2e 20 20 41 20   recompiled.  A 
10d47 73 74 61 74 65 6d 65 6e 74 20 6e 65 65 64 73 20  statement needs 
10d48 74 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c 65 64  to be recompiled
10d49 20 77 68 65 6e 65 76 65 72 20 74 68 65 0a 2a 2a   whenever the.**
10d4a 20 65 78 65 63 75 74 69 6f 6e 20 65 6e 76 69 72   execution envir
10d4b 6f 6e 6d 65 6e 74 20 63 68 61 6e 67 65 73 20 69  onment changes i
10d4c 6e 20 61 20 77 61 79 20 74 68 61 74 20 77 6f 75  n a way that wou
10d4d 6c 64 20 61 6c 74 65 72 20 74 68 65 20 70 72 6f  ld alter the pro
10d4e 67 72 61 6d 0a 2a 2a 20 74 68 61 74 20 73 71 6c  gram.** that sql
10d4f 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 67  ite3_prepare() g
10d50 65 6e 65 72 61 74 65 73 2e 20 20 46 6f 72 20 65  enerates.  For e
10d51 78 61 6d 70 6c 65 2c 20 69 66 20 6e 65 77 20 66  xample, if new f
10d52 75 6e 63 74 69 6f 6e 73 20 6f 72 0a 2a 2a 20 63  unctions or.** c
10d53 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
10d54 65 73 20 61 72 65 20 72 65 67 69 73 74 65 72 65  es are registere
10d55 64 20 6f 72 20 69 66 20 61 6e 20 61 75 74 68 6f  d or if an autho
10d56 72 69 7a 65 72 20 66 75 6e 63 74 69 6f 6e 20 69  rizer function i
10d57 73 0a 2a 2a 20 61 64 64 65 64 20 6f 72 20 63 68  s.** added or ch
10d58 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  anged..*/.SQLITE
10d59 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
10d5a 5f 65 78 70 69 72 65 64 28 73 71 6c 69 74 65 33  _expired(sqlite3
10d5b 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20  _stmt *pStmt){. 
10d5c 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65   Vdbe *p = (Vdbe
10d5d 2a 29 70 53 74 6d 74 3b 0a 20 20 72 65 74 75 72  *)pStmt;.  retur
10d5e 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 65 78 70  n p==0 || p->exp
10d5f 69 72 65 64 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  ired;.}../******
10d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d61 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 76  ****** sqlite3_v
10d62 61 6c 75 65 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  alue_  *********
10d63 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d64 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f  ******.** The fo
10d65 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73  llowing routines
10d66 20 65 78 74 72 61 63 74 20 69 6e 66 6f 72 6d 61   extract informa
10d67 74 69 6f 6e 20 66 72 6f 6d 20 61 20 4d 65 6d 20  tion from a Mem 
10d68 6f 72 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  or sqlite3_value
10d69 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  .** structure..*
10d6a 2f 0a 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  /.const void *sq
10d6b 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
10d6c 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a  (sqlite3_value *
10d6d 70 56 61 6c 29 7b 0a 20 20 4d 65 6d 20 2a 70 20  pVal){.  Mem *p 
10d6e 3d 20 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a 20 20  = (Mem*)pVal;.  
10d6f 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 28  if( p->flags & (
10d70 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72  MEM_Blob|MEM_Str
10d71 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) ){.    sqlite3
10d72 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f  VdbeMemExpandBlo
10d73 62 28 70 29 3b 0a 20 20 20 20 70 2d 3e 66 6c 61  b(p);.    p->fla
10d74 67 73 20 26 3d 20 7e 4d 45 4d 5f 53 74 72 3b 0a  gs &= ~MEM_Str;.
10d75 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 7c 3d 20      p->flags |= 
10d76 4d 45 4d 5f 42 6c 6f 62 3b 0a 20 20 20 20 72 65  MEM_Blob;.    re
10d77 74 75 72 6e 20 70 2d 3e 7a 3b 0a 20 20 7d 65 6c  turn p->z;.  }el
10d78 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73  se{.    return s
10d79 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
10d7a 74 28 70 56 61 6c 29 3b 0a 20 20 7d 0a 7d 0a 69  t(pVal);.  }.}.i
10d7b 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt sqlite3_value
10d7c 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f 76  _bytes(sqlite3_v
10d7d 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72  alue *pVal){.  r
10d7e 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 61 6c  eturn sqlite3Val
10d7f 75 65 42 79 74 65 73 28 70 56 61 6c 2c 20 53 51  ueBytes(pVal, SQ
10d80 4c 49 54 45 5f 55 54 46 38 29 3b 0a 7d 0a 69 6e  LITE_UTF8);.}.in
10d81 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  t sqlite3_value_
10d82 62 79 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f  bytes16(sqlite3_
10d83 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20  value *pVal){.  
10d84 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 61  return sqlite3Va
10d85 6c 75 65 42 79 74 65 73 28 70 56 61 6c 2c 20 53  lueBytes(pVal, S
10d86 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56  QLITE_UTF16NATIV
10d87 45 29 3b 0a 7d 0a 64 6f 75 62 6c 65 20 73 71 6c  E);.}.double sql
10d88 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c  ite3_value_doubl
10d89 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  e(sqlite3_value 
10d8a 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e  *pVal){.  return
10d8b 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c   sqlite3VdbeReal
10d8c 56 61 6c 75 65 28 28 4d 65 6d 2a 29 70 56 61 6c  Value((Mem*)pVal
10d8d 29 3b 0a 7d 0a 69 6e 74 20 73 71 6c 69 74 65 33  );.}.int sqlite3
10d8e 5f 76 61 6c 75 65 5f 69 6e 74 28 73 71 6c 69 74  _value_int(sqlit
10d8f 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b  e3_value *pVal){
10d90 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
10d91 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 28 4d  3VdbeIntValue((M
10d92 65 6d 2a 29 70 56 61 6c 29 3b 0a 7d 0a 73 71 6c  em*)pVal);.}.sql
10d93 69 74 65 5f 69 6e 74 36 34 20 73 71 6c 69 74 65  ite_int64 sqlite
10d94 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 73 71  3_value_int64(sq
10d95 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61  lite3_value *pVa
10d96 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  l){.  return sql
10d97 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65  ite3VdbeIntValue
10d98 28 28 4d 65 6d 2a 29 70 56 61 6c 29 3b 0a 7d 0a  ((Mem*)pVal);.}.
10d99 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
10d9a 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c  har *sqlite3_val
10d9b 75 65 5f 74 65 78 74 28 73 71 6c 69 74 65 33 5f  ue_text(sqlite3_
10d9c 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20  value *pVal){.  
10d9d 72 65 74 75 72 6e 20 28 63 6f 6e 73 74 20 75 6e  return (const un
10d9e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 73 71  signed char *)sq
10d9f 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 70  lite3ValueText(p
10da0 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  Val, SQLITE_UTF8
10da1 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c  );.}.#ifndef SQL
10da2 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 63  ITE_OMIT_UTF16.c
10da3 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
10da4 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28  e3_value_text16(
10da5 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20 70  sqlite3_value* p
10da6 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73  Val){.  return s
10da7 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28  qlite3ValueText(
10da8 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46  pVal, SQLITE_UTF
10da9 31 36 4e 41 54 49 56 45 29 3b 0a 7d 0a 63 6f 6e  16NATIVE);.}.con
10daa 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
10dab 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65 28  _value_text16be(
10dac 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
10dad 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73  Val){.  return s
10dae 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28  qlite3ValueText(
10daf 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46  pVal, SQLITE_UTF
10db0 31 36 42 45 29 3b 0a 7d 0a 63 6f 6e 73 74 20 76  16BE);.}.const v
10db1 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c  oid *sqlite3_val
10db2 75 65 5f 74 65 78 74 31 36 6c 65 28 73 71 6c 69  ue_text16le(sqli
10db3 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29  te3_value *pVal)
10db4 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
10db5 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c  e3ValueText(pVal
10db6 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45  , SQLITE_UTF16LE
10db7 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  );.}.#endif /* S
10db8 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
10db9 20 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f   */.int sqlite3_
10dba 76 61 6c 75 65 5f 74 79 70 65 28 73 71 6c 69 74  value_type(sqlit
10dbb 65 33 5f 76 61 6c 75 65 2a 20 70 56 61 6c 29 7b  e3_value* pVal){
10dbc 0a 20 20 72 65 74 75 72 6e 20 70 56 61 6c 2d 3e  .  return pVal->
10dbd 74 79 70 65 3b 0a 7d 0a 2f 2a 20 73 71 6c 69 74  type;.}./* sqlit
10dbe 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63  e3_value_numeric
10dbf 5f 74 79 70 65 28 29 20 64 65 66 69 6e 65 64 20  _type() defined 
10dc0 69 6e 20 76 64 62 65 2e 63 20 2a 2f 0a 0a 2f 2a  in vdbe.c */../*
10dc1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10dc2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69  *********** sqli
10dc3 74 65 33 5f 72 65 73 75 6c 74 5f 20 20 2a 2a 2a  te3_result_  ***
10dc4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10dc5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
10dc6 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f  The following ro
10dc7 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 20  utines are used 
10dc8 62 79 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20  by user-defined 
10dc9 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 73 70 65  functions to spe
10dca 63 69 66 79 0a 2a 2a 20 74 68 65 20 66 75 6e 63  cify.** the func
10dcb 74 69 6f 6e 20 72 65 73 75 6c 74 2e 0a 2a 2f 0a  tion result..*/.
10dcc 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
10dcd 75 6c 74 5f 62 6c 6f 62 28 0a 20 20 73 71 6c 69  ult_blob(.  sqli
10dce 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74  te3_context *pCt
10dcf 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  x, .  const void
10dd0 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a   *z, .  int n, .
10dd1 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76    void (*xDel)(v
10dd2 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65  oid *).){.  asse
10dd3 72 74 28 20 6e 3e 3d 30 20 29 3b 0a 20 20 73 71  rt( n>=0 );.  sq
10dd4 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53  lite3VdbeMemSetS
10dd5 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20  tr(&pCtx->s, z, 
10dd6 6e 2c 20 30 2c 20 78 44 65 6c 29 3b 0a 7d 0a 76  n, 0, xDel);.}.v
10dd7 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
10dd8 6c 74 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65  lt_double(sqlite
10dd9 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c  3_context *pCtx,
10dda 20 64 6f 75 62 6c 65 20 72 56 61 6c 29 7b 0a 20   double rVal){. 
10ddb 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
10ddc 65 74 44 6f 75 62 6c 65 28 26 70 43 74 78 2d 3e  etDouble(&pCtx->
10ddd 73 2c 20 72 56 61 6c 29 3b 0a 7d 0a 76 6f 69 64  s, rVal);.}.void
10dde 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
10ddf 65 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 63 6f  error(sqlite3_co
10de0 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 63 6f 6e  ntext *pCtx, con
10de1 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20  st char *z, int 
10de2 6e 29 7b 0a 20 20 70 43 74 78 2d 3e 69 73 45 72  n){.  pCtx->isEr
10de3 72 6f 72 20 3d 20 31 3b 0a 20 20 73 71 6c 69 74  ror = 1;.  sqlit
10de4 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28  e3VdbeMemSetStr(
10de5 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e 2c 20  &pCtx->s, z, n, 
10de6 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c  SQLITE_UTF8, SQL
10de7 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a  ITE_TRANSIENT);.
10de8 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
10de9 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 76 6f 69 64  _OMIT_UTF16.void
10dea 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
10deb 65 72 72 6f 72 31 36 28 73 71 6c 69 74 65 33 5f  error16(sqlite3_
10dec 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 63  context *pCtx, c
10ded 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e  onst void *z, in
10dee 74 20 6e 29 7b 0a 20 20 70 43 74 78 2d 3e 69 73  t n){.  pCtx->is
10def 45 72 72 6f 72 20 3d 20 31 3b 0a 20 20 73 71 6c  Error = 1;.  sql
10df0 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74  ite3VdbeMemSetSt
10df1 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e  r(&pCtx->s, z, n
10df2 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41  , SQLITE_UTF16NA
10df3 54 49 56 45 2c 20 53 51 4c 49 54 45 5f 54 52 41  TIVE, SQLITE_TRA
10df4 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 65 6e 64 69  NSIENT);.}.#endi
10df5 66 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  f.void sqlite3_r
10df6 65 73 75 6c 74 5f 69 6e 74 28 73 71 6c 69 74 65  esult_int(sqlite
10df7 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c  3_context *pCtx,
10df8 20 69 6e 74 20 69 56 61 6c 29 7b 0a 20 20 73 71   int iVal){.  sq
10df9 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49  lite3VdbeMemSetI
10dfa 6e 74 36 34 28 26 70 43 74 78 2d 3e 73 2c 20 28  nt64(&pCtx->s, (
10dfb 69 36 34 29 69 56 61 6c 29 3b 0a 7d 0a 76 6f 69  i64)iVal);.}.voi
10dfc 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  d sqlite3_result
10dfd 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 63  _int64(sqlite3_c
10dfe 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69 36  ontext *pCtx, i6
10dff 34 20 69 56 61 6c 29 7b 0a 20 20 73 71 6c 69 74  4 iVal){.  sqlit
10e00 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36  e3VdbeMemSetInt6
10e01 34 28 26 70 43 74 78 2d 3e 73 2c 20 69 56 61 6c  4(&pCtx->s, iVal
10e02 29 3b 0a 7d 0a 76 6f 69 64 20 73 71 6c 69 74 65  );.}.void sqlite
10e03 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 73 71  3_result_null(sq
10e04 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
10e05 43 74 78 29 7b 0a 20 20 73 71 6c 69 74 65 33 56  Ctx){.  sqlite3V
10e06 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 26 70  dbeMemSetNull(&p
10e07 43 74 78 2d 3e 73 29 3b 0a 7d 0a 76 6f 69 64 20  Ctx->s);.}.void 
10e08 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
10e09 65 78 74 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  ext(.  sqlite3_c
10e0a 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 0a 20  ontext *pCtx, . 
10e0b 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20   const char *z, 
10e0c 0a 20 20 69 6e 74 20 6e 2c 0a 20 20 76 6f 69 64  .  int n,.  void
10e0d 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a 29   (*xDel)(void *)
10e0e 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  .){.  sqlite3Vdb
10e0f 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 78  eMemSetStr(&pCtx
10e10 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54  ->s, z, n, SQLIT
10e11 45 5f 55 54 46 38 2c 20 78 44 65 6c 29 3b 0a 7d  E_UTF8, xDel);.}
10e12 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
10e13 4f 4d 49 54 5f 55 54 46 31 36 0a 76 6f 69 64 20  OMIT_UTF16.void 
10e14 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
10e15 65 78 74 31 36 28 0a 20 20 73 71 6c 69 74 65 33  ext16(.  sqlite3
10e16 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20  _context *pCtx, 
10e17 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  .  const void *z
10e18 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 76  , .  int n, .  v
10e19 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64  oid (*xDel)(void
10e1a 20 2a 29 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   *).){.  sqlite3
10e1b 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 70  VdbeMemSetStr(&p
10e1c 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51  Ctx->s, z, n, SQ
10e1d 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45  LITE_UTF16NATIVE
10e1e 2c 20 78 44 65 6c 29 3b 0a 7d 0a 76 6f 69 64 20  , xDel);.}.void 
10e1f 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
10e20 65 78 74 31 36 62 65 28 0a 20 20 73 71 6c 69 74  ext16be(.  sqlit
10e21 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
10e22 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  , .  const void 
10e23 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20  *z, .  int n, . 
10e24 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f   void (*xDel)(vo
10e25 69 64 20 2a 29 0a 29 7b 0a 20 20 73 71 6c 69 74  id *).){.  sqlit
10e26 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28  e3VdbeMemSetStr(
10e27 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e 2c 20  &pCtx->s, z, n, 
10e28 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20  SQLITE_UTF16BE, 
10e29 78 44 65 6c 29 3b 0a 7d 0a 76 6f 69 64 20 73 71  xDel);.}.void sq
10e2a 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
10e2b 74 31 36 6c 65 28 0a 20 20 73 71 6c 69 74 65 33  t16le(.  sqlite3
10e2c 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20  _context *pCtx, 
10e2d 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  .  const void *z
10e2e 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 76  , .  int n, .  v
10e2f 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64  oid (*xDel)(void
10e30 20 2a 29 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   *).){.  sqlite3
10e31 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 70  VdbeMemSetStr(&p
10e32 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51  Ctx->s, z, n, SQ
10e33 4c 49 54 45 5f 55 54 46 31 36 4c 45 2c 20 78 44  LITE_UTF16LE, xD
10e34 65 6c 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  el);.}.#endif /*
10e35 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
10e36 31 36 20 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74  16 */.void sqlit
10e37 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28  e3_result_value(
10e38 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
10e39 2a 70 43 74 78 2c 20 73 71 6c 69 74 65 33 5f 76  *pCtx, sqlite3_v
10e3a 61 6c 75 65 20 2a 70 56 61 6c 75 65 29 7b 0a 20  alue *pValue){. 
10e3b 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43   sqlite3VdbeMemC
10e3c 6f 70 79 28 26 70 43 74 78 2d 3e 73 2c 20 70 56  opy(&pCtx->s, pV
10e3d 61 6c 75 65 29 3b 0a 7d 0a 76 6f 69 64 20 73 71  alue);.}.void sq
10e3e 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72  lite3_result_zer
10e3f 6f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 63 6f  oblob(sqlite3_co
10e40 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69 6e 74  ntext *pCtx, int
10e41 20 6e 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64   n){.  sqlite3Vd
10e42 62 65 4d 65 6d 53 65 74 5a 65 72 6f 42 6c 6f 62  beMemSetZeroBlob
10e43 28 26 70 43 74 78 2d 3e 73 2c 20 6e 29 3b 0a 7d  (&pCtx->s, n);.}
10e44 0a 0a 2f 2a 20 46 6f 72 63 65 20 61 6e 20 53 51  ../* Force an SQ
10e45 4c 49 54 45 5f 54 4f 4f 42 49 47 20 65 72 72 6f  LITE_TOOBIG erro
10e46 72 2e 20 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74  r. */.void sqlit
10e47 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
10e48 74 6f 6f 62 69 67 28 73 71 6c 69 74 65 33 5f 63  toobig(sqlite3_c
10e49 6f 6e 74 65 78 74 20 2a 70 43 74 78 29 7b 0a 20  ontext *pCtx){. 
10e4a 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
10e4b 65 74 5a 65 72 6f 42 6c 6f 62 28 26 70 43 74 78  etZeroBlob(&pCtx
10e4c 2d 3e 73 2c 20 53 51 4c 49 54 45 5f 4d 41 58 5f  ->s, SQLITE_MAX_
10e4d 4c 45 4e 47 54 48 2b 31 29 3b 0a 7d 0a 0a 0a 2f  LENGTH+1);.}.../
10e4e 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 74 68 65  *.** Execute the
10e4f 20 73 74 61 74 65 6d 65 6e 74 20 70 53 74 6d 74   statement pStmt
10e50 2c 20 65 69 74 68 65 72 20 75 6e 74 69 6c 20 61  , either until a
10e51 20 72 6f 77 20 6f 66 20 64 61 74 61 20 69 73 20   row of data is 
10e52 72 65 61 64 79 2c 20 74 68 65 0a 2a 2a 20 73 74  ready, the.** st
10e53 61 74 65 6d 65 6e 74 20 69 73 20 63 6f 6d 70 6c  atement is compl
10e54 65 74 65 6c 79 20 65 78 65 63 75 74 65 64 20 6f  etely executed o
10e55 72 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  r an error occur
10e56 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  s..**.** This ro
10e57 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73  utine implements
10e58 20 74 68 65 20 62 75 6c 6b 20 6f 66 20 74 68 65   the bulk of the
10e59 20 6c 6f 67 69 63 20 62 65 68 69 6e 64 20 74 68   logic behind th
10e5a 65 20 73 71 6c 69 74 65 5f 73 74 65 70 28 29 0a  e sqlite_step().
10e5b 2a 2a 20 41 50 49 2e 20 20 54 68 65 20 6f 6e 6c  ** API.  The onl
10e5c 79 20 74 68 69 6e 67 20 6f 6d 69 74 74 65 64 20  y thing omitted 
10e5d 69 73 20 74 68 65 20 61 75 74 6f 6d 61 74 69 63  is the automatic
10e5e 20 72 65 63 6f 6d 70 69 6c 65 20 69 66 20 61 20   recompile if a 
10e5f 0a 2a 2a 20 73 63 68 65 6d 61 20 63 68 61 6e 67  .** schema chang
10e60 65 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20  e has occurred. 
10e61 20 54 68 61 74 20 64 65 74 61 69 6c 20 69 73 20   That detail is 
10e62 68 61 6e 64 6c 65 64 20 62 79 20 74 68 65 0a 2a  handled by the.*
10e63 2a 20 6f 75 74 65 72 20 73 71 6c 69 74 65 33 5f  * outer sqlite3_
10e64 73 74 65 70 28 29 20 77 72 61 70 70 65 72 20 70  step() wrapper p
10e65 72 6f 63 65 64 75 72 65 2e 0a 2a 2f 0a 73 74 61  rocedure..*/.sta
10e66 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 53  tic int sqlite3S
10e67 74 65 70 28 56 64 62 65 20 2a 70 29 7b 0a 20 20  tep(Vdbe *p){.  
10e68 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 69  sqlite3 *db;.  i
10e69 6e 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 41 73 73  nt rc;..  /* Ass
10e6a 65 72 74 20 74 68 61 74 20 6d 61 6c 6c 6f 63 28  ert that malloc(
10e6b 29 20 68 61 73 20 6e 6f 74 20 66 61 69 6c 65 64  ) has not failed
10e6c 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 21 73   */.  assert( !s
10e6d 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69 6c  qlite3MallocFail
10e6e 65 64 28 29 20 29 3b 0a 0a 20 20 69 66 28 20 70  ed() );..  if( p
10e6f 3d 3d 30 20 7c 7c 20 70 2d 3e 6d 61 67 69 63 21  ==0 || p->magic!
10e70 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20  =VDBE_MAGIC_RUN 
10e71 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
10e72 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d  LITE_MISUSE;.  }
10e73 0a 20 20 69 66 28 20 70 2d 3e 61 62 6f 72 74 65  .  if( p->aborte
10e74 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  d ){.    return 
10e75 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20 20  SQLITE_ABORT;.  
10e76 7d 0a 20 20 69 66 28 20 70 2d 3e 70 63 3c 3d 30  }.  if( p->pc<=0
10e77 20 26 26 20 70 2d 3e 65 78 70 69 72 65 64 20 29   && p->expired )
10e78 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 72 63 3d  {.    if( p->rc=
10e79 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
10e7a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49      p->rc = SQLI
10e7b 54 45 5f 53 43 48 45 4d 41 3b 0a 20 20 20 20 7d  TE_SCHEMA;.    }
10e7c 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
10e7d 5f 45 52 52 4f 52 3b 0a 20 20 20 20 67 6f 74 6f  _ERROR;.    goto
10e7e 20 65 6e 64 5f 6f 66 5f 73 74 65 70 3b 0a 20 20   end_of_step;.  
10e7f 7d 0a 20 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a  }.  db = p->db;.
10e80 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66    if( sqlite3Saf
10e81 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20  etyOn(db) ){.   
10e82 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f   p->rc = SQLITE_
10e83 4d 49 53 55 53 45 3b 0a 20 20 20 20 72 65 74 75  MISUSE;.    retu
10e84 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  rn SQLITE_MISUSE
10e85 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70  ;.  }.  if( p->p
10e86 63 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66  c<0 ){.    /* If
10e87 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6f 74   there are no ot
10e88 68 65 72 20 73 74 61 74 65 6d 65 6e 74 73 20 63  her statements c
10e89 75 72 72 65 6e 74 6c 79 20 72 75 6e 6e 69 6e 67  urrently running
10e8a 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 72 65  , then.    ** re
10e8b 73 65 74 20 74 68 65 20 69 6e 74 65 72 72 75 70  set the interrup
10e8c 74 20 66 6c 61 67 2e 20 20 54 68 69 73 20 70 72  t flag.  This pr
10e8d 65 76 65 6e 74 73 20 61 20 63 61 6c 6c 20 74 6f  events a call to
10e8e 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75   sqlite3_interru
10e8f 70 74 0a 20 20 20 20 2a 2a 20 66 72 6f 6d 20 69  pt.    ** from i
10e90 6e 74 65 72 72 75 70 74 69 6e 67 20 61 20 73 74  nterrupting a st
10e91 61 74 65 6d 65 6e 74 20 74 68 61 74 20 68 61 73  atement that has
10e92 20 6e 6f 74 20 79 65 74 20 73 74 61 72 74 65 64   not yet started
10e93 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
10e94 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43   db->activeVdbeC
10e95 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 64  nt==0 ){.      d
10e96 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70  b->u1.isInterrup
10e97 74 65 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a  ted = 0;.    }..
10e98 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
10e99 4d 49 54 5f 54 52 41 43 45 0a 20 20 20 20 2f 2a  MIT_TRACE.    /*
10e9a 20 49 6e 76 6f 6b 65 20 74 68 65 20 74 72 61 63   Invoke the trac
10e9b 65 20 63 61 6c 6c 62 61 63 6b 20 69 66 20 74 68  e callback if th
10e9c 65 72 65 20 69 73 20 6f 6e 65 0a 20 20 20 20 2a  ere is one.    *
10e9d 2f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 78 54  /.    if( db->xT
10e9e 72 61 63 65 20 26 26 20 21 64 62 2d 3e 69 6e 69  race && !db->ini
10e9f 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 20 20  t.busy ){.      
10ea0 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30  assert( p->nOp>0
10ea1 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
10ea2 28 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d  ( p->aOp[p->nOp-
10ea3 31 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4e 6f  1].opcode==OP_No
10ea4 6f 70 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  op );.      asse
10ea5 72 74 28 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f  rt( p->aOp[p->nO
10ea6 70 2d 31 5d 2e 70 33 21 3d 30 20 29 3b 0a 20 20  p-1].p3!=0 );.  
10ea7 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61      assert( p->a
10ea8 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 70 33 74  Op[p->nOp-1].p3t
10ea9 79 70 65 3d 3d 50 33 5f 44 59 4e 41 4d 49 43 20  ype==P3_DYNAMIC 
10eaa 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
10eab 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20  SafetyOff(db);. 
10eac 20 20 20 20 20 64 62 2d 3e 78 54 72 61 63 65 28       db->xTrace(
10ead 64 62 2d 3e 70 54 72 61 63 65 41 72 67 2c 20 70  db->pTraceArg, p
10eae 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e  ->aOp[p->nOp-1].
10eaf 70 33 29 3b 0a 20 20 20 20 20 20 69 66 28 20 73  p3);.      if( s
10eb0 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64  qlite3SafetyOn(d
10eb1 62 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d  b) ){.        p-
10eb2 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53  >rc = SQLITE_MIS
10eb3 55 53 45 3b 0a 20 20 20 20 20 20 20 20 72 65 74  USE;.        ret
10eb4 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  urn SQLITE_MISUS
10eb5 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  E;.      }.    }
10eb6 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 78 50 72  .    if( db->xPr
10eb7 6f 66 69 6c 65 20 26 26 20 21 64 62 2d 3e 69 6e  ofile && !db->in
10eb8 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 20  it.busy ){.     
10eb9 20 64 6f 75 62 6c 65 20 72 4e 6f 77 3b 0a 20 20   double rNow;.  
10eba 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 75 72      sqlite3OsCur
10ebb 72 65 6e 74 54 69 6d 65 28 26 72 4e 6f 77 29 3b  rentTime(&rNow);
10ebc 0a 20 20 20 20 20 20 70 2d 3e 73 74 61 72 74 54  .      p->startT
10ebd 69 6d 65 20 3d 20 28 72 4e 6f 77 20 2d 20 28 69  ime = (rNow - (i
10ebe 6e 74 29 72 4e 6f 77 29 2a 33 36 30 30 2e 30 2a  nt)rNow)*3600.0*
10ebf 32 34 2e 30 2a 31 30 30 30 30 30 30 30 30 30 2e  24.0*1000000000.
10ec0 30 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  0;.    }.#endif.
10ec1 0a 20 20 20 20 2f 2a 20 50 72 69 6e 74 20 61 20  .    /* Print a 
10ec2 63 6f 70 79 20 6f 66 20 53 51 4c 20 61 73 20 69  copy of SQL as i
10ec3 74 20 69 73 20 65 78 65 63 75 74 65 64 20 69 66  t is executed if
10ec4 20 74 68 65 20 53 51 4c 5f 54 52 41 43 45 20 70   the SQL_TRACE p
10ec5 72 61 67 6d 61 20 69 73 20 74 75 72 6e 65 64 0a  ragma is turned.
10ec6 20 20 20 20 2a 2a 20 6f 6e 20 69 6e 20 64 65 62      ** on in deb
10ec7 75 67 67 69 6e 67 20 6d 6f 64 65 2e 0a 20 20 20  ugging mode..   
10ec8 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54   */.#ifdef SQLIT
10ec9 45 5f 44 45 42 55 47 0a 20 20 20 20 69 66 28 20  E_DEBUG.    if( 
10eca 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  (db->flags & SQL
10ecb 49 54 45 5f 53 71 6c 54 72 61 63 65 29 21 3d 30  ITE_SqlTrace)!=0
10ecc 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
10ecd 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 53 51  3DebugPrintf("SQ
10ece 4c 2d 74 72 61 63 65 3a 20 25 73 5c 6e 22 2c 20  L-trace: %s\n", 
10ecf 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d  p->aOp[p->nOp-1]
10ed0 2e 70 33 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  .p3);.    }.#end
10ed1 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42  if /* SQLITE_DEB
10ed2 55 47 20 2a 2f 0a 0a 20 20 20 20 64 62 2d 3e 61  UG */..    db->a
10ed3 63 74 69 76 65 56 64 62 65 43 6e 74 2b 2b 3b 0a  ctiveVdbeCnt++;.
10ed4 20 20 20 20 70 2d 3e 70 63 20 3d 20 30 3b 0a 20      p->pc = 0;. 
10ed5 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
10ed6 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 20  E_OMIT_EXPLAIN. 
10ed7 20 69 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 20   if( p->explain 
10ed8 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
10ed9 74 65 33 56 64 62 65 4c 69 73 74 28 70 29 3b 0a  te3VdbeList(p);.
10eda 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f    }else.#endif /
10edb 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58  * SQLITE_OMIT_EX
10edc 50 4c 41 49 4e 20 2a 2f 0a 20 20 7b 0a 20 20 20  PLAIN */.  {.   
10edd 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
10ede 65 45 78 65 63 28 70 29 3b 0a 20 20 7d 0a 0a 20  eExec(p);.  }.. 
10edf 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65   if( sqlite3Safe
10ee0 74 79 4f 66 66 28 64 62 29 20 29 7b 0a 20 20 20  tyOff(db) ){.   
10ee1 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53   rc = SQLITE_MIS
10ee2 55 53 45 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65  USE;.  }..#ifnde
10ee3 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
10ee4 41 43 45 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20  ACE.  /* Invoke 
10ee5 74 68 65 20 70 72 6f 66 69 6c 65 20 63 61 6c 6c  the profile call
10ee6 62 61 63 6b 20 69 66 20 74 68 65 72 65 20 69 73  back if there is
10ee7 20 6f 6e 65 0a 20 20 2a 2f 0a 20 20 69 66 28 20   one.  */.  if( 
10ee8 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26  rc!=SQLITE_ROW &
10ee9 26 20 64 62 2d 3e 78 50 72 6f 66 69 6c 65 20 26  & db->xProfile &
10eea 26 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  & !db->init.busy
10eeb 20 29 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20 72   ){.    double r
10eec 4e 6f 77 3b 0a 20 20 20 20 75 36 34 20 65 6c 61  Now;.    u64 ela
10eed 70 73 65 54 69 6d 65 3b 0a 0a 20 20 20 20 73 71  pseTime;..    sq
10eee 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69  lite3OsCurrentTi
10eef 6d 65 28 26 72 4e 6f 77 29 3b 0a 20 20 20 20 65  me(&rNow);.    e
10ef0 6c 61 70 73 65 54 69 6d 65 20 3d 20 28 72 4e 6f  lapseTime = (rNo
10ef1 77 20 2d 20 28 69 6e 74 29 72 4e 6f 77 29 2a 33  w - (int)rNow)*3
10ef2 36 30 30 2e 30 2a 32 34 2e 30 2a 31 30 30 30 30  600.0*24.0*10000
10ef3 30 30 30 30 30 2e 30 20 2d 20 70 2d 3e 73 74 61  00000.0 - p->sta
10ef4 72 74 54 69 6d 65 3b 0a 20 20 20 20 61 73 73 65  rtTime;.    asse
10ef5 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 3b 0a  rt( p->nOp>0 );.
10ef6 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61      assert( p->a
10ef7 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 6f 70 63  Op[p->nOp-1].opc
10ef8 6f 64 65 3d 3d 4f 50 5f 4e 6f 6f 70 20 29 3b 0a  ode==OP_Noop );.
10ef9 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61      assert( p->a
10efa 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 70 33 21  Op[p->nOp-1].p3!
10efb 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =0 );.    assert
10efc 28 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d  ( p->aOp[p->nOp-
10efd 31 5d 2e 70 33 74 79 70 65 3d 3d 50 33 5f 44 59  1].p3type==P3_DY
10efe 4e 41 4d 49 43 20 29 3b 0a 20 20 20 20 64 62 2d  NAMIC );.    db-
10eff 3e 78 50 72 6f 66 69 6c 65 28 64 62 2d 3e 70 50  >xProfile(db->pP
10f00 72 6f 66 69 6c 65 41 72 67 2c 20 70 2d 3e 61 4f  rofileArg, p->aO
10f01 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 70 33 2c 20  p[p->nOp-1].p3, 
10f02 65 6c 61 70 73 65 54 69 6d 65 29 3b 0a 20 20 7d  elapseTime);.  }
10f03 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74  .#endif..  sqlit
10f04 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20 72  e3Error(p->db, r
10f05 63 2c 20 30 29 3b 0a 20 20 70 2d 3e 72 63 20 3d  c, 0);.  p->rc =
10f06 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28   sqlite3ApiExit(
10f07 70 2d 3e 64 62 2c 20 70 2d 3e 72 63 29 3b 0a 65  p->db, p->rc);.e
10f08 6e 64 5f 6f 66 5f 73 74 65 70 3a 0a 20 20 61 73  nd_of_step:.  as
10f09 73 65 72 74 28 20 28 72 63 26 30 78 66 66 29 3d  sert( (rc&0xff)=
10f0a 3d 72 63 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e  =rc );.  if( p->
10f0b 7a 53 71 6c 20 26 26 20 28 72 63 26 30 78 66 66  zSql && (rc&0xff
10f0c 29 3c 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a  )<SQLITE_ROW ){.
10f0d 20 20 20 20 2f 2a 20 54 68 69 73 20 62 65 68 61      /* This beha
10f0e 76 69 6f 72 20 6f 63 63 75 72 73 20 69 66 20 73  vior occurs if s
10f0f 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
10f10 32 28 29 20 77 61 73 20 75 73 65 64 20 74 6f 20  2() was used to 
10f11 62 75 69 6c 64 0a 20 20 20 20 2a 2a 20 74 68 65  build.    ** the
10f12 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
10f13 65 6e 74 2e 20 20 52 65 74 75 72 6e 20 65 72 72  ent.  Return err
10f14 6f 72 20 63 6f 64 65 73 20 64 69 72 65 63 74 6c  or codes directl
10f15 79 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  y */.    sqlite3
10f16 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20 70 2d 3e  Error(p->db, p->
10f17 72 63 2c 20 30 29 3b 0a 20 20 20 20 72 65 74 75  rc, 0);.    retu
10f18 72 6e 20 70 2d 3e 72 63 3b 0a 20 20 7d 65 6c 73  rn p->rc;.  }els
10f19 65 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 69  e{.    /* This i
10f1a 73 20 66 6f 72 20 6c 65 67 61 63 79 20 73 71 6c  s for legacy sql
10f1b 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 62  ite3_prepare() b
10f1c 75 69 6c 64 73 20 61 6e 64 20 77 68 65 6e 20 74  uilds and when t
10f1d 68 65 20 63 6f 64 65 0a 20 20 20 20 2a 2a 20 69  he code.    ** i
10f1e 73 20 53 51 4c 49 54 45 5f 52 4f 57 20 6f 72 20  s SQLITE_ROW or 
10f1f 53 51 4c 49 54 45 5f 44 4f 4e 45 20 2a 2f 0a 20  SQLITE_DONE */. 
10f20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
10f21 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  }.}../*.** This 
10f22 69 73 20 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c  is the top-level
10f23 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
10f24 6f 66 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  of sqlite3_step(
10f25 29 2e 20 20 43 61 6c 6c 0a 2a 2a 20 73 71 6c 69  ).  Call.** sqli
10f26 74 65 33 53 74 65 70 28 29 20 74 6f 20 64 6f 20  te3Step() to do 
10f27 6d 6f 73 74 20 6f 66 20 74 68 65 20 77 6f 72 6b  most of the work
10f28 2e 20 20 49 66 20 61 20 73 63 68 65 6d 61 20 65  .  If a schema e
10f29 72 72 6f 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20  rror occurs,.** 
10f2a 63 61 6c 6c 20 73 71 6c 69 74 65 33 52 65 70 72  call sqlite3Repr
10f2b 65 70 61 72 65 28 29 20 61 6e 64 20 74 72 79 20  epare() and try 
10f2c 61 67 61 69 6e 2e 0a 2a 2f 0a 23 69 66 64 65 66  again..*/.#ifdef
10f2d 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 52   SQLITE_OMIT_PAR
10f2e 53 45 52 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  SER.SQLITE_API i
10f2f 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  nt sqlite3_step(
10f30 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
10f31 74 6d 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 73  tmt){.  return s
10f32 71 6c 69 74 65 33 53 74 65 70 28 28 56 64 62 65  qlite3Step((Vdbe
10f33 2a 29 70 53 74 6d 74 29 3b 0a 7d 0a 23 65 6c 73  *)pStmt);.}.#els
10f34 65 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  e.SQLITE_API int
10f35 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 71   sqlite3_step(sq
10f36 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
10f37 74 29 7b 0a 20 20 69 6e 74 20 63 6e 74 20 3d 20  t){.  int cnt = 
10f38 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56  0;.  int rc;.  V
10f39 64 62 65 20 2a 76 20 3d 20 28 56 64 62 65 2a 29  dbe *v = (Vdbe*)
10f3a 70 53 74 6d 74 3b 0a 20 20 77 68 69 6c 65 28 20  pStmt;.  while( 
10f3b 28 72 63 20 3d 20 73 71 6c 69 74 65 33 53 74 65  (rc = sqlite3Ste
10f3c 70 28 76 29 29 3d 3d 53 51 4c 49 54 45 5f 53 43  p(v))==SQLITE_SC
10f3d 48 45 4d 41 0a 20 20 20 20 20 20 20 20 20 26 26  HEMA.         &&
10f3e 20 63 6e 74 2b 2b 20 3c 20 35 0a 20 20 20 20 20   cnt++ < 5.     
10f3f 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 52 65      && sqlite3Re
10f40 70 72 65 70 61 72 65 28 76 29 20 29 7b 0a 20 20  prepare(v) ){.  
10f41 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28    sqlite3_reset(
10f42 70 53 74 6d 74 29 3b 0a 20 20 20 20 76 2d 3e 65  pStmt);.    v->e
10f43 78 70 69 72 65 64 20 3d 20 30 3b 0a 20 20 7d 0a  xpired = 0;.  }.
10f44 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23    return rc;.}.#
10f45 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 78 74  endif../*.** Ext
10f46 72 61 63 74 20 74 68 65 20 75 73 65 72 20 64 61  ract the user da
10f47 74 61 20 66 72 6f 6d 20 61 20 73 71 6c 69 74 65  ta from a sqlite
10f48 33 5f 63 6f 6e 74 65 78 74 20 73 74 72 75 63 74  3_context struct
10f49 75 72 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61  ure and return a
10f4a 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 69  .** pointer to i
10f4b 74 2e 0a 2a 2f 0a 76 6f 69 64 20 2a 73 71 6c 69  t..*/.void *sqli
10f4c 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 73 71  te3_user_data(sq
10f4d 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
10f4e 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 26  ){.  assert( p &
10f4f 26 20 70 2d 3e 70 46 75 6e 63 20 29 3b 0a 20 20  & p->pFunc );.  
10f50 72 65 74 75 72 6e 20 70 2d 3e 70 46 75 6e 63 2d  return p->pFunc-
10f51 3e 70 55 73 65 72 44 61 74 61 3b 0a 7d 0a 0a 2f  >pUserData;.}../
10f52 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
10f53 6e 67 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d  ng is the implem
10f54 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 53  entation of an S
10f55 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74  QL function that
10f56 20 61 6c 77 61 79 73 0a 2a 2a 20 66 61 69 6c 73   always.** fails
10f57 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d   with an error m
10f58 65 73 73 61 67 65 20 73 74 61 74 69 6e 67 20 74  essage stating t
10f59 68 61 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e  hat the function
10f5a 20 69 73 20 75 73 65 64 20 69 6e 20 74 68 65 0a   is used in the.
10f5b 2a 2a 20 77 72 6f 6e 67 20 63 6f 6e 74 65 78 74  ** wrong context
10f5c 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6f  .  The sqlite3_o
10f5d 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e  verload_function
10f5e 28 29 20 41 50 49 20 6d 69 67 68 74 20 63 6f 6e  () API might con
10f5f 73 74 72 75 63 74 0a 2a 2a 20 53 51 4c 20 66 75  struct.** SQL fu
10f60 6e 63 74 69 6f 6e 20 74 68 61 74 20 75 73 65 20  nction that use 
10f61 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73 6f 20  this routine so 
10f62 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 69 6f  that the functio
10f63 6e 73 20 77 69 6c 6c 20 65 78 69 73 74 0a 2a 2a  ns will exist.**
10f64 20 66 6f 72 20 6e 61 6d 65 20 72 65 73 6f 6c 75   for name resolu
10f65 74 69 6f 6e 20 62 75 74 20 61 72 65 20 61 63 74  tion but are act
10f66 75 61 6c 6c 79 20 6f 76 65 72 6c 6f 61 64 65 64  ually overloaded
10f67 20 62 79 20 74 68 65 20 78 46 69 6e 64 46 75 6e   by the xFindFun
10f68 63 74 69 6f 6e 0a 2a 2a 20 6d 65 74 68 6f 64 20  ction.** method 
10f69 6f 66 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  of virtual table
10f6a 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
10f6b 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
10f6c 33 49 6e 76 61 6c 69 64 46 75 6e 63 74 69 6f 6e  3InvalidFunction
10f6d 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
10f6e 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 20 2f  ext *context,  /
10f6f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 63  * The function c
10f70 61 6c 6c 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  alling context *
10f71 2f 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 20 20  /.  int argc,   
10f72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
10f73 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67 75  * Number of argu
10f74 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 66 75 6e  ments to the fun
10f75 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74  ction */.  sqlit
10f76 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 20  e3_value **argv 
10f77 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f        /* Value o
10f78 66 20 65 61 63 68 20 61 72 67 75 6d 65 6e 74 20  f each argument 
10f79 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68  */.){.  const ch
10f7a 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 63 6f 6e 74  ar *zName = cont
10f7b 65 78 74 2d 3e 70 46 75 6e 63 2d 3e 7a 4e 61 6d  ext->pFunc->zNam
10f7c 65 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 3b  e;.  char *zErr;
10f7d 0a 20 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65  .  zErr = sqlite
10f7e 33 4d 50 72 69 6e 74 66 28 0a 20 20 20 20 20 20  3MPrintf(.      
10f7f 22 75 6e 61 62 6c 65 20 74 6f 20 75 73 65 20 66  "unable to use f
10f80 75 6e 63 74 69 6f 6e 20 25 73 20 69 6e 20 74 68  unction %s in th
10f81 65 20 72 65 71 75 65 73 74 65 64 20 63 6f 6e 74  e requested cont
10f82 65 78 74 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20  ext", zName);.  
10f83 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
10f84 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45  rror(context, zE
10f85 72 72 2c 20 2d 31 29 3b 0a 20 20 73 71 6c 69 74  rr, -1);.  sqlit
10f86 65 46 72 65 65 28 7a 45 72 72 29 3b 0a 7d 0a 0a  eFree(zErr);.}..
10f87 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6f  /*.** Allocate o
10f88 72 20 72 65 74 75 72 6e 20 74 68 65 20 61 67 67  r return the agg
10f89 72 65 67 61 74 65 20 63 6f 6e 74 65 78 74 20 66  regate context f
10f8a 6f 72 20 61 20 75 73 65 72 20 66 75 6e 63 74 69  or a user functi
10f8b 6f 6e 2e 20 20 41 20 6e 65 77 0a 2a 2a 20 63 6f  on.  A new.** co
10f8c 6e 74 65 78 74 20 69 73 20 61 6c 6c 6f 63 61 74  ntext is allocat
10f8d 65 64 20 6f 6e 20 74 68 65 20 66 69 72 73 74 20  ed on the first 
10f8e 63 61 6c 6c 2e 20 20 53 75 62 73 65 71 75 65 6e  call.  Subsequen
10f8f 74 20 63 61 6c 6c 73 20 72 65 74 75 72 6e 20 74  t calls return t
10f90 68 65 0a 2a 2a 20 73 61 6d 65 20 63 6f 6e 74 65  he.** same conte
10f91 78 74 20 74 68 61 74 20 77 61 73 20 72 65 74 75  xt that was retu
10f92 72 6e 65 64 20 6f 6e 20 70 72 69 6f 72 20 63 61  rned on prior ca
10f93 6c 6c 73 2e 0a 2a 2f 0a 76 6f 69 64 20 2a 73 71  lls..*/.void *sq
10f94 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f  lite3_aggregate_
10f95 63 6f 6e 74 65 78 74 28 73 71 6c 69 74 65 33 5f  context(sqlite3_
10f96 63 6f 6e 74 65 78 74 20 2a 70 2c 20 69 6e 74 20  context *p, int 
10f97 6e 42 79 74 65 29 7b 0a 20 20 4d 65 6d 20 2a 70  nByte){.  Mem *p
10f98 4d 65 6d 20 3d 20 70 2d 3e 70 4d 65 6d 3b 0a 20  Mem = p->pMem;. 
10f99 20 61 73 73 65 72 74 28 20 70 20 26 26 20 70 2d   assert( p && p-
10f9a 3e 70 46 75 6e 63 20 26 26 20 70 2d 3e 70 46 75  >pFunc && p->pFu
10f9b 6e 63 2d 3e 78 53 74 65 70 20 29 3b 0a 20 20 69  nc->xStep );.  i
10f9c 66 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  f( (pMem->flags 
10f9d 26 20 4d 45 4d 5f 41 67 67 29 3d 3d 30 20 29 7b  & MEM_Agg)==0 ){
10f9e 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65 3d 3d  .    if( nByte==
10f9f 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  0 ){.      asser
10fa0 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3d 3d  t( pMem->flags==
10fa1 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 20 20 20 20  MEM_Null );.    
10fa2 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 30 3b 0a 20    pMem->z = 0;. 
10fa3 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
10fa4 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
10fa5 4d 5f 41 67 67 3b 0a 20 20 20 20 20 20 70 4d 65  M_Agg;.      pMe
10fa6 6d 2d 3e 78 44 65 6c 20 3d 20 73 71 6c 69 74 65  m->xDel = sqlite
10fa7 33 46 72 65 65 58 3b 0a 20 20 20 20 20 20 70 4d  3FreeX;.      pM
10fa8 65 6d 2d 3e 75 2e 70 44 65 66 20 3d 20 70 2d 3e  em->u.pDef = p->
10fa9 70 46 75 6e 63 3b 0a 20 20 20 20 20 20 69 66 28  pFunc;.      if(
10faa 20 6e 42 79 74 65 3c 3d 4e 42 46 53 20 29 7b 0a   nByte<=NBFS ){.
10fab 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20          pMem->z 
10fac 3d 20 70 4d 65 6d 2d 3e 7a 53 68 6f 72 74 3b 0a  = pMem->zShort;.
10fad 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70          memset(p
10fae 4d 65 6d 2d 3e 7a 2c 20 30 2c 20 6e 42 79 74 65  Mem->z, 0, nByte
10faf 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
10fb0 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20          pMem->z 
10fb1 3d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 20  = sqliteMalloc( 
10fb2 6e 42 79 74 65 20 29 3b 0a 20 20 20 20 20 20 7d  nByte );.      }
10fb3 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
10fb4 75 72 6e 20 28 76 6f 69 64 2a 29 70 4d 65 6d 2d  urn (void*)pMem-
10fb5 3e 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  >z;.}../*.** Ret
10fb6 75 72 6e 20 74 68 65 20 61 75 78 69 6c 61 72 79  urn the auxilary
10fb7 20 64 61 74 61 20 70 6f 69 6e 74 65 72 2c 20 69   data pointer, i
10fb8 66 20 61 6e 79 2c 20 66 6f 72 20 74 68 65 20 69  f any, for the i
10fb9 41 72 67 27 74 68 20 61 72 67 75 6d 65 6e 74 20  Arg'th argument 
10fba 74 6f 0a 2a 2a 20 74 68 65 20 75 73 65 72 2d 66  to.** the user-f
10fbb 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 65 64 20  unction defined 
10fbc 62 79 20 70 43 74 78 2e 0a 2a 2f 0a 76 6f 69 64  by pCtx..*/.void
10fbd 20 2a 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75   *sqlite3_get_au
10fbe 78 64 61 74 61 28 73 71 6c 69 74 65 33 5f 63 6f  xdata(sqlite3_co
10fbf 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69 6e 74  ntext *pCtx, int
10fc0 20 69 41 72 67 29 7b 0a 20 20 56 64 62 65 46 75   iArg){.  VdbeFu
10fc1 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 20 3d 20  nc *pVdbeFunc = 
10fc2 70 43 74 78 2d 3e 70 56 64 62 65 46 75 6e 63 3b  pCtx->pVdbeFunc;
10fc3 0a 20 20 69 66 28 20 21 70 56 64 62 65 46 75 6e  .  if( !pVdbeFun
10fc4 63 20 7c 7c 20 69 41 72 67 3e 3d 70 56 64 62 65  c || iArg>=pVdbe
10fc5 46 75 6e 63 2d 3e 6e 41 75 78 20 7c 7c 20 69 41  Func->nAux || iA
10fc6 72 67 3c 30 20 29 7b 0a 20 20 20 20 72 65 74 75  rg<0 ){.    retu
10fc7 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rn 0;.  }.  retu
10fc8 72 6e 20 70 56 64 62 65 46 75 6e 63 2d 3e 61 70  rn pVdbeFunc->ap
10fc9 41 75 78 5b 69 41 72 67 5d 2e 70 41 75 78 3b 0a  Aux[iArg].pAux;.
10fca 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65  }../*.** Set the
10fcb 20 61 75 78 69 6c 61 72 79 20 64 61 74 61 20 70   auxilary data p
10fcc 6f 69 6e 74 65 72 20 61 6e 64 20 64 65 6c 65 74  ointer and delet
10fcd 65 20 66 75 6e 63 74 69 6f 6e 2c 20 66 6f 72 20  e function, for 
10fce 74 68 65 20 69 41 72 67 27 74 68 0a 2a 2a 20 61  the iArg'th.** a
10fcf 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 75  rgument to the u
10fd0 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 64 65 66  ser-function def
10fd1 69 6e 65 64 20 62 79 20 70 43 74 78 2e 20 41 6e  ined by pCtx. An
10fd2 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65  y previous value
10fd3 20 69 73 0a 2a 2a 20 64 65 6c 65 74 65 64 20 62   is.** deleted b
10fd4 79 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 64 65  y calling the de
10fd5 6c 65 74 65 20 66 75 6e 63 74 69 6f 6e 20 73 70  lete function sp
10fd6 65 63 69 66 69 65 64 20 77 68 65 6e 20 69 74 20  ecified when it 
10fd7 77 61 73 20 73 65 74 2e 0a 2a 2f 0a 76 6f 69 64  was set..*/.void
10fd8 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78   sqlite3_set_aux
10fd9 64 61 74 61 28 0a 20 20 73 71 6c 69 74 65 33 5f  data(.  sqlite3_
10fda 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 0a  context *pCtx, .
10fdb 20 20 69 6e 74 20 69 41 72 67 2c 20 0a 20 20 76    int iArg, .  v
10fdc 6f 69 64 20 2a 70 41 75 78 2c 20 0a 20 20 76 6f  oid *pAux, .  vo
10fdd 69 64 20 28 2a 78 44 65 6c 65 74 65 29 28 76 6f  id (*xDelete)(vo
10fde 69 64 2a 29 0a 29 7b 0a 20 20 73 74 72 75 63 74  id*).){.  struct
10fdf 20 41 75 78 44 61 74 61 20 2a 70 41 75 78 44 61   AuxData *pAuxDa
10fe0 74 61 3b 0a 20 20 56 64 62 65 46 75 6e 63 20 2a  ta;.  VdbeFunc *
10fe1 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 69 66 28  pVdbeFunc;.  if(
10fe2 20 69 41 72 67 3c 30 20 29 20 72 65 74 75 72 6e   iArg<0 ) return
10fe3 3b 0a 0a 20 20 70 56 64 62 65 46 75 6e 63 20 3d  ;..  pVdbeFunc =
10fe4 20 70 43 74 78 2d 3e 70 56 64 62 65 46 75 6e 63   pCtx->pVdbeFunc
10fe5 3b 0a 20 20 69 66 28 20 21 70 56 64 62 65 46 75  ;.  if( !pVdbeFu
10fe6 6e 63 20 7c 7c 20 70 56 64 62 65 46 75 6e 63 2d  nc || pVdbeFunc-
10fe7 3e 6e 41 75 78 3c 3d 69 41 72 67 20 29 7b 0a 20  >nAux<=iArg ){. 
10fe8 20 20 20 69 6e 74 20 6e 4d 61 6c 6c 6f 63 20 3d     int nMalloc =
10fe9 20 73 69 7a 65 6f 66 28 56 64 62 65 46 75 6e 63   sizeof(VdbeFunc
10fea 29 20 2b 20 73 69 7a 65 6f 66 28 73 74 72 75 63  ) + sizeof(struc
10feb 74 20 41 75 78 44 61 74 61 29 2a 69 41 72 67 3b  t AuxData)*iArg;
10fec 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63 20 3d  .    pVdbeFunc =
10fed 20 73 71 6c 69 74 65 52 65 61 6c 6c 6f 63 28 70   sqliteRealloc(p
10fee 56 64 62 65 46 75 6e 63 2c 20 6e 4d 61 6c 6c 6f  VdbeFunc, nMallo
10fef 63 29 3b 0a 20 20 20 20 69 66 28 20 21 70 56 64  c);.    if( !pVd
10ff0 62 65 46 75 6e 63 20 29 20 72 65 74 75 72 6e 3b  beFunc ) return;
10ff1 0a 20 20 20 20 70 43 74 78 2d 3e 70 56 64 62 65  .    pCtx->pVdbe
10ff2 46 75 6e 63 20 3d 20 70 56 64 62 65 46 75 6e 63  Func = pVdbeFunc
10ff3 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 70 56  ;.    memset(&pV
10ff4 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b 70  dbeFunc->apAux[p
10ff5 56 64 62 65 46 75 6e 63 2d 3e 6e 41 75 78 5d 2c  VdbeFunc->nAux],
10ff6 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20   0, .           
10ff7 20 20 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20    sizeof(struct 
10ff8 41 75 78 44 61 74 61 29 2a 28 69 41 72 67 2b 31  AuxData)*(iArg+1
10ff9 2d 70 56 64 62 65 46 75 6e 63 2d 3e 6e 41 75 78  -pVdbeFunc->nAux
10ffa 29 29 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e  ));.    pVdbeFun
10ffb 63 2d 3e 6e 41 75 78 20 3d 20 69 41 72 67 2b 31  c->nAux = iArg+1
10ffc 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63 2d  ;.    pVdbeFunc-
10ffd 3e 70 46 75 6e 63 20 3d 20 70 43 74 78 2d 3e 70  >pFunc = pCtx->p
10ffe 46 75 6e 63 3b 0a 20 20 7d 0a 0a 20 20 70 41 75  Func;.  }..  pAu
10fff 78 44 61 74 61 20 3d 20 26 70 56 64 62 65 46 75  xData = &pVdbeFu
11000 6e 63 2d 3e 61 70 41 75 78 5b 69 41 72 67 5d 3b  nc->apAux[iArg];
11001 0a 20 20 69 66 28 20 70 41 75 78 44 61 74 61 2d  .  if( pAuxData-
11002 3e 70 41 75 78 20 26 26 20 70 41 75 78 44 61 74  >pAux && pAuxDat
11003 61 2d 3e 78 44 65 6c 65 74 65 20 29 7b 0a 20 20  a->xDelete ){.  
11004 20 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65 6c    pAuxData->xDel
11005 65 74 65 28 70 41 75 78 44 61 74 61 2d 3e 70 41  ete(pAuxData->pA
11006 75 78 29 3b 0a 20 20 7d 0a 20 20 70 41 75 78 44  ux);.  }.  pAuxD
11007 61 74 61 2d 3e 70 41 75 78 20 3d 20 70 41 75 78  ata->pAux = pAux
11008 3b 0a 20 20 70 41 75 78 44 61 74 61 2d 3e 78 44  ;.  pAuxData->xD
11009 65 6c 65 74 65 20 3d 20 78 44 65 6c 65 74 65 3b  elete = xDelete;
1100a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
1100b 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74   the number of t
1100c 69 6d 65 73 20 74 68 65 20 53 74 65 70 20 66 75  imes the Step fu
1100d 6e 63 74 69 6f 6e 20 6f 66 20 61 20 61 67 67 72  nction of a aggr
1100e 65 67 61 74 65 20 68 61 73 20 62 65 65 6e 20 0a  egate has been .
1100f 2a 2a 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a  ** called..**.**
11010 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
11011 73 20 64 65 70 72 65 63 61 74 65 64 2e 20 20 44  s deprecated.  D
11012 6f 20 6e 6f 74 20 75 73 65 20 69 74 20 66 6f 72  o not use it for
11013 20 6e 65 77 20 63 6f 64 65 2e 20 20 49 74 20 69   new code.  It i
11014 73 0a 2a 2a 20 70 72 6f 76 69 64 65 20 6f 6e 6c  s.** provide onl
11015 79 20 74 6f 20 61 76 6f 69 64 20 62 72 65 61 6b  y to avoid break
11016 69 6e 67 20 6c 65 67 61 63 79 20 63 6f 64 65 2e  ing legacy code.
11017 20 20 4e 65 77 20 61 67 67 72 65 67 61 74 65 20    New aggregate 
11018 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 6d 70 6c  function.** impl
11019 65 6d 65 6e 74 61 74 69 6f 6e 73 20 73 68 6f 75  ementations shou
1101a 6c 64 20 6b 65 65 70 20 74 68 65 69 72 20 6f 77  ld keep their ow
1101b 6e 20 63 6f 75 6e 74 73 20 77 69 74 68 69 6e 20  n counts within 
1101c 74 68 65 69 72 20 61 67 67 72 65 67 61 74 65 0a  their aggregate.
1101d 2a 2a 20 63 6f 6e 74 65 78 74 2e 0a 2a 2f 0a 69  ** context..*/.i
1101e 6e 74 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65  nt sqlite3_aggre
1101f 67 61 74 65 5f 63 6f 75 6e 74 28 73 71 6c 69 74  gate_count(sqlit
11020 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 29 7b 0a  e3_context *p){.
11021 20 20 61 73 73 65 72 74 28 20 70 20 26 26 20 70    assert( p && p
11022 2d 3e 70 46 75 6e 63 20 26 26 20 70 2d 3e 70 46  ->pFunc && p->pF
11023 75 6e 63 2d 3e 78 53 74 65 70 20 29 3b 0a 20 20  unc->xStep );.  
11024 72 65 74 75 72 6e 20 70 2d 3e 70 4d 65 6d 2d 3e  return p->pMem->
11025 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  n;.}../*.** Retu
11026 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
11027 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
11028 72 65 73 75 6c 74 20 73 65 74 20 66 6f 72 20 74  result set for t
11029 68 65 20 73 74 61 74 65 6d 65 6e 74 20 70 53 74  he statement pSt
1102a 6d 74 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74  mt..*/.int sqlit
1102b 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28  e3_column_count(
1102c 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
1102d 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 56  tmt){.  Vdbe *pV
1102e 6d 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d  m = (Vdbe *)pStm
1102f 74 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 6d 20  t;.  return pVm 
11030 3f 20 70 56 6d 2d 3e 6e 52 65 73 43 6f 6c 75 6d  ? pVm->nResColum
11031 6e 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n : 0;.}../*.** 
11032 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
11033 72 20 6f 66 20 76 61 6c 75 65 73 20 61 76 61 69  r of values avai
11034 6c 61 62 6c 65 20 66 72 6f 6d 20 74 68 65 20 63  lable from the c
11035 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68  urrent row of th
11036 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 65  e.** currently e
11037 78 65 63 75 74 69 6e 67 20 73 74 61 74 65 6d 65  xecuting stateme
11038 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 69 6e 74  nt pStmt..*/.int
11039 20 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f   sqlite3_data_co
1103a 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  unt(sqlite3_stmt
1103b 20 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65   *pStmt){.  Vdbe
1103c 20 2a 70 56 6d 20 3d 20 28 56 64 62 65 20 2a 29   *pVm = (Vdbe *)
1103d 70 53 74 6d 74 3b 0a 20 20 69 66 28 20 70 56 6d  pStmt;.  if( pVm
1103e 3d 3d 30 20 7c 7c 20 21 70 56 6d 2d 3e 72 65 73  ==0 || !pVm->res
1103f 4f 6e 53 74 61 63 6b 20 29 20 72 65 74 75 72 6e  OnStack ) return
11040 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 6d   0;.  return pVm
11041 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 3b 0a 7d 0a  ->nResColumn;.}.
11042 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f  ../*.** Check to
11043 20 73 65 65 20 69 66 20 63 6f 6c 75 6d 6e 20 69   see if column i
11044 43 6f 6c 20 6f 66 20 74 68 65 20 67 69 76 65 6e  Col of the given
11045 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 76 61   statement is va
11046 6c 69 64 2e 20 20 49 66 0a 2a 2a 20 69 74 20 69  lid.  If.** it i
11047 73 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e  s, return a poin
11048 74 65 72 20 74 6f 20 74 68 65 20 4d 65 6d 20 66  ter to the Mem f
11049 6f 72 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  or the value of 
1104a 74 68 61 74 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20  that column..** 
1104b 49 66 20 69 43 6f 6c 20 69 73 20 6e 6f 74 20 76  If iCol is not v
1104c 61 6c 69 64 2c 20 72 65 74 75 72 6e 20 61 20 70  alid, return a p
1104d 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d 65 6d 20  ointer to a Mem 
1104e 77 68 69 63 68 20 68 61 73 20 61 20 76 61 6c 75  which has a valu
1104f 65 0a 2a 2a 20 6f 66 20 4e 55 4c 4c 2e 0a 2a 2f  e.** of NULL..*/
11050 0a 73 74 61 74 69 63 20 4d 65 6d 20 2a 63 6f 6c  .static Mem *col
11051 75 6d 6e 4d 65 6d 28 73 71 6c 69 74 65 33 5f 73  umnMem(sqlite3_s
11052 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
11053 69 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 6d 20  i){.  Vdbe *pVm 
11054 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b  = (Vdbe *)pStmt;
11055 0a 20 20 69 6e 74 20 76 61 6c 73 20 3d 20 73 71  .  int vals = sq
11056 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74  lite3_data_count
11057 28 70 53 74 6d 74 29 3b 0a 20 20 69 66 28 20 70  (pStmt);.  if( p
11058 56 6d 3d 3d 30 20 7c 7c 20 70 56 6d 2d 3e 72 65  Vm==0 || pVm->re
11059 73 4f 6e 53 74 61 63 6b 3d 3d 30 20 7c 7c 20 69  sOnStack==0 || i
1105a 3e 3d 70 56 6d 2d 3e 6e 52 65 73 43 6f 6c 75 6d  >=pVm->nResColum
1105b 6e 20 7c 7c 20 69 3c 30 20 29 7b 0a 20 20 20 20  n || i<0 ){.    
1105c 73 74 61 74 69 63 20 63 6f 6e 73 74 20 4d 65 6d  static const Mem
1105d 20 6e 75 6c 6c 4d 65 6d 20 3d 20 7b 7b 30 7d 2c   nullMem = {{0},
1105e 20 30 2e 30 2c 20 22 22 2c 20 30 2c 20 4d 45 4d   0.0, "", 0, MEM
1105f 5f 4e 75 6c 6c 2c 20 53 51 4c 49 54 45 5f 4e 55  _Null, SQLITE_NU
11060 4c 4c 20 7d 3b 0a 20 20 20 20 73 71 6c 69 74 65  LL };.    sqlite
11061 33 45 72 72 6f 72 28 70 56 6d 2d 3e 64 62 2c 20  3Error(pVm->db, 
11062 53 51 4c 49 54 45 5f 52 41 4e 47 45 2c 20 30 29  SQLITE_RANGE, 0)
11063 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 4d 65  ;.    return (Me
11064 6d 2a 29 26 6e 75 6c 6c 4d 65 6d 3b 0a 20 20 7d  m*)&nullMem;.  }
11065 0a 20 20 72 65 74 75 72 6e 20 26 70 56 6d 2d 3e  .  return &pVm->
11066 70 54 6f 73 5b 28 31 2d 76 61 6c 73 29 2b 69 5d  pTos[(1-vals)+i]
11067 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
11068 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
11069 65 64 20 61 66 74 65 72 20 69 6e 76 6f 6b 69 6e  ed after invokin
1106a 67 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c  g an sqlite3_val
1106b 75 65 5f 58 58 58 20 66 75 6e 63 74 69 6f 6e 20  ue_XXX function 
1106c 6f 6e 20 61 20 0a 2a 2a 20 63 6f 6c 75 6d 6e 20  on a .** column 
1106d 76 61 6c 75 65 20 28 69 2e 65 2e 20 61 20 76 61  value (i.e. a va
1106e 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20  lue returned by 
1106f 65 76 61 6c 75 61 74 69 6e 67 20 61 6e 20 53 51  evaluating an SQ
11070 4c 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20  L expression in 
11071 74 68 65 0a 2a 2a 20 73 65 6c 65 63 74 20 6c 69  the.** select li
11072 73 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73  st of a SELECT s
11073 74 61 74 65 6d 65 6e 74 29 20 74 68 61 74 20 6d  tatement) that m
11074 61 79 20 63 61 75 73 65 20 61 20 6d 61 6c 6c 6f  ay cause a mallo
11075 63 28 29 20 66 61 69 6c 75 72 65 2e 20 49 66 20  c() failure. If 
11076 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73  .** malloc() has
11077 20 66 61 69 6c 65 64 2c 20 74 68 65 20 74 68 72   failed, the thr
11078 65 61 64 73 20 6d 61 6c 6c 6f 63 46 61 69 6c 65  eads mallocFaile
11079 64 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72 65  d flag is cleare
1107a 64 20 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74  d and the result
1107b 0a 2a 2a 20 63 6f 64 65 20 6f 66 20 73 74 61 74  .** code of stat
1107c 65 6d 65 6e 74 20 70 53 74 6d 74 20 73 65 74 20  ement pStmt set 
1107d 74 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e  to SQLITE_NOMEM.
1107e 0a 2a 2a 0a 2a 2a 20 53 70 65 63 69 66 69 63 6c  .**.** Specificl
1107f 79 2c 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65  y, this is calle
11080 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 3a 0a 2a  d from within:.*
11081 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33  *.**     sqlite3
11082 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 29 0a 2a 2a  _column_int().**
11083 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c       sqlite3_col
11084 75 6d 6e 5f 69 6e 74 36 34 28 29 0a 2a 2a 20 20  umn_int64().**  
11085 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d     sqlite3_colum
11086 6e 5f 74 65 78 74 28 29 0a 2a 2a 20 20 20 20 20  n_text().**     
11087 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
11088 65 78 74 31 36 28 29 0a 2a 2a 20 20 20 20 20 73  ext16().**     s
11089 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 72 65  qlite3_column_re
1108a 61 6c 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69  al().**     sqli
1108b 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  te3_column_bytes
1108c 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65  ().**     sqlite
1108d 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36  3_column_bytes16
1108e 28 29 0a 2a 2a 0a 2a 2a 20 42 75 74 20 6e 6f 74  ().**.** But not
1108f 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c   for sqlite3_col
11090 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20 77 68 69 63  umn_blob(), whic
11091 68 20 6e 65 76 65 72 20 63 61 6c 6c 73 20 6d 61  h never calls ma
11092 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  lloc()..*/.stati
11093 63 20 76 6f 69 64 20 63 6f 6c 75 6d 6e 4d 61 6c  c void columnMal
11094 6c 6f 63 46 61 69 6c 75 72 65 28 73 71 6c 69 74  locFailure(sqlit
11095 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 0a  e3_stmt *pStmt).
11096 7b 0a 20 20 2f 2a 20 49 66 20 6d 61 6c 6c 6f 63  {.  /* If malloc
11097 28 29 20 66 61 69 6c 65 64 20 64 75 72 69 6e 67  () failed during
11098 20 61 6e 20 65 6e 63 6f 64 69 6e 67 20 63 6f 6e   an encoding con
11099 76 65 72 73 69 6f 6e 20 77 69 74 68 69 6e 20 61  version within a
1109a 6e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63  n.  ** sqlite3_c
1109b 6f 6c 75 6d 6e 5f 58 58 58 20 41 50 49 2c 20 74  olumn_XXX API, t
1109c 68 65 6e 20 73 65 74 20 74 68 65 20 72 65 74 75  hen set the retu
1109d 72 6e 20 63 6f 64 65 20 6f 66 20 74 68 65 20 73  rn code of the s
1109e 74 61 74 65 6d 65 6e 74 20 74 6f 0a 20 20 2a 2a  tatement to.  **
1109f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 54   SQLITE_NOMEM. T
110a0 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20  he next call to 
110a1 5f 73 74 65 70 28 29 20 28 69 66 20 61 6e 79 29  _step() (if any)
110a2 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 53 51 4c   will return SQL
110a3 49 54 45 5f 45 52 52 4f 52 0a 20 20 2a 2a 20 61  ITE_ERROR.  ** a
110a4 6e 64 20 5f 66 69 6e 61 6c 69 7a 65 28 29 20 77  nd _finalize() w
110a5 69 6c 6c 20 72 65 74 75 72 6e 20 4e 4f 4d 45 4d  ill return NOMEM
110a6 2e 0a 20 20 2a 2f 0a 20 20 56 64 62 65 20 2a 70  ..  */.  Vdbe *p
110a7 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74   = (Vdbe *)pStmt
110a8 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 73 71 6c 69  ;.  p->rc = sqli
110a9 74 65 33 41 70 69 45 78 69 74 28 30 2c 20 70 2d  te3ApiExit(0, p-
110aa 3e 72 63 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  >rc);.}../******
110ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
110ac 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 63  ****** sqlite3_c
110ad 6f 6c 75 6d 6e 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a  olumn_  ********
110ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
110af 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66  *******.** The f
110b0 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65  ollowing routine
110b1 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 61 63  s are used to ac
110b2 63 65 73 73 20 65 6c 65 6d 65 6e 74 73 20 6f 66  cess elements of
110b3 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77   the current row
110b4 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 75 6c  .** in the resul
110b5 74 20 73 65 74 2e 0a 2a 2f 0a 63 6f 6e 73 74 20  t set..*/.const 
110b6 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f  void *sqlite3_co
110b7 6c 75 6d 6e 5f 62 6c 6f 62 28 73 71 6c 69 74 65  lumn_blob(sqlite
110b8 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69  3_stmt *pStmt, i
110b9 6e 74 20 69 29 7b 0a 20 20 63 6f 6e 73 74 20 76  nt i){.  const v
110ba 6f 69 64 20 2a 76 61 6c 3b 0a 20 20 76 61 6c 20  oid *val;.  val 
110bb 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
110bc 62 6c 6f 62 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28  blob( columnMem(
110bd 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 2f 2a  pStmt,i) );.  /*
110be 20 45 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65   Even though the
110bf 72 65 20 69 73 20 6e 6f 20 65 6e 63 6f 64 69 6e  re is no encodin
110c0 67 20 63 6f 6e 76 65 72 73 69 6f 6e 2c 20 76 61  g conversion, va
110c1 6c 75 65 5f 62 6c 6f 62 28 29 20 6d 69 67 68 74  lue_blob() might
110c2 0a 20 20 2a 2a 20 6e 65 65 64 20 74 6f 20 63 61  .  ** need to ca
110c3 6c 6c 20 6d 61 6c 6c 6f 63 28 29 20 74 6f 20 65  ll malloc() to e
110c4 78 70 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74  xpand the result
110c5 20 6f 66 20 61 20 7a 65 72 6f 62 6c 6f 62 28 29   of a zeroblob()
110c6 20 0a 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f   .  ** expressio
110c7 6e 2e 20 0a 20 20 2a 2f 0a 20 20 63 6f 6c 75 6d  n. .  */.  colum
110c8 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70  nMallocFailure(p
110c9 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20  Stmt);.  return 
110ca 76 61 6c 3b 0a 7d 0a 69 6e 74 20 73 71 6c 69 74  val;.}.int sqlit
110cb 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
110cc 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
110cd 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69  tmt, int i){.  i
110ce 6e 74 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 33  nt val = sqlite3
110cf 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 20 63 6f  _value_bytes( co
110d0 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29  lumnMem(pStmt,i)
110d1 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c   );.  columnMall
110d2 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29  ocFailure(pStmt)
110d3 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a  ;.  return val;.
110d4 7d 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f  }.int sqlite3_co
110d5 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 73 71 6c  lumn_bytes16(sql
110d6 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
110d7 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20  , int i){.  int 
110d8 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  val = sqlite3_va
110d9 6c 75 65 5f 62 79 74 65 73 31 36 28 20 63 6f 6c  lue_bytes16( col
110da 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20  umnMem(pStmt,i) 
110db 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f  );.  columnMallo
110dc 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b  cFailure(pStmt);
110dd 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d  .  return val;.}
110de 0a 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 33 5f  .double sqlite3_
110df 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 28 73 71  column_double(sq
110e0 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
110e1 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 64 6f 75  t, int i){.  dou
110e2 62 6c 65 20 76 61 6c 20 3d 20 73 71 6c 69 74 65  ble val = sqlite
110e3 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 20  3_value_double( 
110e4 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c  columnMem(pStmt,
110e5 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61  i) );.  columnMa
110e6 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d  llocFailure(pStm
110e7 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c  t);.  return val
110e8 3b 0a 7d 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f  ;.}.int sqlite3_
110e9 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73 71 6c 69 74  column_int(sqlit
110ea 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
110eb 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 76 61  int i){.  int va
110ec 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  l = sqlite3_valu
110ed 65 5f 69 6e 74 28 20 63 6f 6c 75 6d 6e 4d 65 6d  e_int( columnMem
110ee 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63  (pStmt,i) );.  c
110ef 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75  olumnMallocFailu
110f0 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74  re(pStmt);.  ret
110f1 75 72 6e 20 76 61 6c 3b 0a 7d 0a 73 71 6c 69 74  urn val;.}.sqlit
110f2 65 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f  e_int64 sqlite3_
110f3 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71 6c  column_int64(sql
110f4 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
110f5 2c 20 69 6e 74 20 69 29 7b 0a 20 20 73 71 6c 69  , int i){.  sqli
110f6 74 65 5f 69 6e 74 36 34 20 76 61 6c 20 3d 20 73  te_int64 val = s
110f7 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
110f8 36 34 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53  64( columnMem(pS
110f9 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75  tmt,i) );.  colu
110fa 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28  mnMallocFailure(
110fb 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e  pStmt);.  return
110fc 20 76 61 6c 3b 0a 7d 0a 63 6f 6e 73 74 20 75 6e   val;.}.const un
110fd 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73 71 6c  signed char *sql
110fe 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
110ff 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
11100 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20  Stmt, int i){.  
11101 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
11102 68 61 72 20 2a 76 61 6c 20 3d 20 73 71 6c 69 74  har *val = sqlit
11103 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 20 63  e3_value_text( c
11104 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69  olumnMem(pStmt,i
11105 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c  ) );.  columnMal
11106 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74  locFailure(pStmt
11107 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b  );.  return val;
11108 0a 7d 0a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .}.sqlite3_value
11109 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
1110a 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 73  _value(sqlite3_s
1110b 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
1110c 69 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c  i){.  return col
1110d 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 20 69 29  umnMem(pStmt, i)
1110e 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ;.}.#ifndef SQLI
1110f 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 63 6f  TE_OMIT_UTF16.co
11110 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
11111 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28  3_column_text16(
11112 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
11113 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 63  tmt, int i){.  c
11114 6f 6e 73 74 20 76 6f 69 64 20 2a 76 61 6c 20 3d  onst void *val =
11115 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
11116 65 78 74 31 36 28 20 63 6f 6c 75 6d 6e 4d 65 6d  ext16( columnMem
11117 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63  (pStmt,i) );.  c
11118 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75  olumnMallocFailu
11119 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74  re(pStmt);.  ret
1111a 75 72 6e 20 76 61 6c 3b 0a 7d 0a 23 65 6e 64 69  urn val;.}.#endi
1111b 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
1111c 5f 55 54 46 31 36 20 2a 2f 0a 69 6e 74 20 73 71  _UTF16 */.int sq
1111d 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70  lite3_column_typ
1111e 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  e(sqlite3_stmt *
1111f 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20  pStmt, int i){. 
11120 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f   return sqlite3_
11121 76 61 6c 75 65 5f 74 79 70 65 28 20 63 6f 6c 75  value_type( colu
11122 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29  mnMem(pStmt,i) )
11123 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c  ;.}../* The foll
11124 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 69  owing function i
11125 73 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 61  s experimental a
11126 6e 64 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68  nd subject to ch
11127 61 6e 67 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76  ange or.** remov
11128 61 6c 20 2a 2f 0a 2f 2a 69 6e 74 20 73 71 6c 69  al */./*int sqli
11129 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 75 6d 65 72  te3_column_numer
1112a 69 63 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f  ic_type(sqlite3_
1112b 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
1112c 20 69 29 7b 0a 2a 2a 20 20 72 65 74 75 72 6e 20   i){.**  return 
1112d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75  sqlite3_value_nu
1112e 6d 65 72 69 63 5f 74 79 70 65 28 20 63 6f 6c 75  meric_type( colu
1112f 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29  mnMem(pStmt,i) )
11130 3b 0a 2a 2a 7d 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  ;.**}.*/../*.** 
11131 43 6f 6e 76 65 72 74 20 74 68 65 20 4e 2d 74 68  Convert the N-th
11132 20 65 6c 65 6d 65 6e 74 20 6f 66 20 70 53 74 6d   element of pStm
11133 74 2d 3e 70 43 6f 6c 4e 61 6d 65 5b 5d 20 69 6e  t->pColName[] in
11134 74 6f 20 61 20 73 74 72 69 6e 67 20 75 73 69 6e  to a string usin
11135 67 0a 2a 2a 20 78 46 75 6e 63 28 29 20 74 68 65  g.** xFunc() the
11136 6e 20 72 65 74 75 72 6e 20 74 68 61 74 20 73 74  n return that st
11137 72 69 6e 67 2e 20 20 49 66 20 4e 20 69 73 20 6f  ring.  If N is o
11138 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 72 65 74  ut of range, ret
11139 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  urn 0..**.** The
1113a 72 65 20 61 72 65 20 75 70 20 74 6f 20 35 20 6e  re are up to 5 n
1113b 61 6d 65 73 20 66 6f 72 20 65 61 63 68 20 63 6f  ames for each co
1113c 6c 75 6d 6e 2e 20 20 75 73 65 54 79 70 65 20 64  lumn.  useType d
1113d 65 74 65 72 6d 69 6e 65 73 20 77 68 69 63 68 0a  etermines which.
1113e 2a 2a 20 6e 61 6d 65 20 69 73 20 72 65 74 75 72  ** name is retur
1113f 6e 65 64 2e 20 20 48 65 72 65 20 61 72 65 20 74  ned.  Here are t
11140 68 65 20 6e 61 6d 65 73 3a 0a 2a 2a 0a 2a 2a 20  he names:.**.** 
11141 20 20 20 30 20 20 20 20 20 20 54 68 65 20 63 6f     0      The co
11142 6c 75 6d 6e 20 6e 61 6d 65 20 61 73 20 69 74 20  lumn name as it 
11143 73 68 6f 75 6c 64 20 62 65 20 64 69 73 70 6c 61  should be displa
11144 79 65 64 20 66 6f 72 20 6f 75 74 70 75 74 0a 2a  yed for output.*
11145 2a 20 20 20 20 31 20 20 20 20 20 20 54 68 65 20  *    1      The 
11146 64 61 74 61 74 79 70 65 20 6e 61 6d 65 20 66 6f  datatype name fo
11147 72 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20  r the column.** 
11148 20 20 20 32 20 20 20 20 20 20 54 68 65 20 6e 61     2      The na
11149 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  me of the databa
1114a 73 65 20 74 68 61 74 20 74 68 65 20 63 6f 6c 75  se that the colu
1114b 6d 6e 20 64 65 72 69 76 65 73 20 66 72 6f 6d 0a  mn derives from.
1114c 2a 2a 20 20 20 20 33 20 20 20 20 20 20 54 68 65  **    3      The
1114d 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62   name of the tab
1114e 6c 65 20 74 68 61 74 20 74 68 65 20 63 6f 6c 75  le that the colu
1114f 6d 6e 20 64 65 72 69 76 65 73 20 66 72 6f 6d 0a  mn derives from.
11150 2a 2a 20 20 20 20 34 20 20 20 20 20 20 54 68 65  **    4      The
11151 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62   name of the tab
11152 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 74  le column that t
11153 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e  he result column
11154 20 64 65 72 69 76 65 73 20 66 72 6f 6d 0a 2a 2a   derives from.**
11155 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73 75 6c  .** If the resul
11156 74 20 69 73 20 6e 6f 74 20 61 20 73 69 6d 70 6c  t is not a simpl
11157 65 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e  e column referen
11158 63 65 20 28 69 66 20 69 74 20 69 73 20 61 6e 20  ce (if it is an 
11159 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 6f 72  expression.** or
1115a 20 61 20 63 6f 6e 73 74 61 6e 74 29 20 74 68 65   a constant) the
1115b 6e 20 75 73 65 54 79 70 65 73 20 32 2c 20 33 2c  n useTypes 2, 3,
1115c 20 61 6e 64 20 34 20 72 65 74 75 72 6e 20 4e 55   and 4 return NU
1115d 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f  LL..*/.static co
1115e 6e 73 74 20 76 6f 69 64 20 2a 63 6f 6c 75 6d 6e  nst void *column
1115f 4e 61 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f  Name(.  sqlite3_
11160 73 74 6d 74 20 2a 70 53 74 6d 74 2c 0a 20 20 69  stmt *pStmt,.  i
11161 6e 74 20 4e 2c 0a 20 20 63 6f 6e 73 74 20 76 6f  nt N,.  const vo
11162 69 64 20 2a 28 2a 78 46 75 6e 63 29 28 4d 65 6d  id *(*xFunc)(Mem
11163 2a 29 2c 0a 20 20 69 6e 74 20 75 73 65 54 79 70  *),.  int useTyp
11164 65 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 76 6f 69  e.){.  const voi
11165 64 20 2a 72 65 74 3b 0a 20 20 56 64 62 65 20 2a  d *ret;.  Vdbe *
11166 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d  p = (Vdbe *)pStm
11167 74 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c  t;.  int n = sql
11168 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e  ite3_column_coun
11169 74 28 70 53 74 6d 74 29 3b 0a 0a 20 20 69 66 28  t(pStmt);..  if(
1116a 20 70 3d 3d 30 20 7c 7c 20 4e 3e 3d 6e 20 7c 7c   p==0 || N>=n ||
1116b 20 4e 3c 30 20 29 7b 0a 20 20 20 20 72 65 74 75   N<0 ){.    retu
1116c 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 4e 20 2b 3d  rn 0;.  }.  N +=
1116d 20 75 73 65 54 79 70 65 2a 6e 3b 0a 20 20 72 65   useType*n;.  re
1116e 74 20 3d 20 78 46 75 6e 63 28 26 70 2d 3e 61 43  t = xFunc(&p->aC
1116f 6f 6c 4e 61 6d 65 5b 4e 5d 29 3b 0a 0a 20 20 2f  olName[N]);..  /
11170 2a 20 41 20 6d 61 6c 6c 6f 63 20 6d 61 79 20 68  * A malloc may h
11171 61 76 65 20 66 61 69 6c 65 64 20 69 6e 73 69 64  ave failed insid
11172 65 20 6f 66 20 74 68 65 20 78 46 75 6e 63 28 29  e of the xFunc()
11173 20 63 61 6c 6c 2e 20 49 66 20 74 68 69 73 20 69   call. If this i
11174 73 20 74 68 65 20 63 61 73 65 2c 0a 20 20 2a 2a  s the case,.  **
11175 20 63 6c 65 61 72 20 74 68 65 20 6d 61 6c 6c 6f   clear the mallo
11176 63 46 61 69 6c 65 64 20 66 6c 61 67 20 61 6e 64  cFailed flag and
11177 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 20 20   return NULL..  
11178 2a 2f 0a 20 20 73 71 6c 69 74 65 33 41 70 69 45  */.  sqlite3ApiE
11179 78 69 74 28 30 2c 20 30 29 3b 0a 20 20 72 65 74  xit(0, 0);.  ret
1117a 75 72 6e 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  urn ret;.}../*.*
1117b 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 61 6d  * Return the nam
1117c 65 20 6f 66 20 74 68 65 20 4e 74 68 20 63 6f 6c  e of the Nth col
1117d 75 6d 6e 20 6f 66 20 74 68 65 20 72 65 73 75 6c  umn of the resul
1117e 74 20 73 65 74 20 72 65 74 75 72 6e 65 64 20 62  t set returned b
1117f 79 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65  y SQL.** stateme
11180 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 63 6f 6e  nt pStmt..*/.con
11181 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
11182 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 73 71 6c  _column_name(sql
11183 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
11184 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75  , int N){.  retu
11185 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20  rn columnName(. 
11186 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28       pStmt, N, (
11187 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d  const void*(*)(M
11188 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c  em*))sqlite3_val
11189 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45  ue_text, COLNAME
1118a 5f 4e 41 4d 45 29 3b 0a 7d 0a 23 69 66 6e 64 65  _NAME);.}.#ifnde
1118b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  f SQLITE_OMIT_UT
1118c 46 31 36 0a 63 6f 6e 73 74 20 76 6f 69 64 20 2a  F16.const void *
1118d 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e  sqlite3_column_n
1118e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74  ame16(sqlite3_st
1118f 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e  mt *pStmt, int N
11190 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75  ){.  return colu
11191 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53  mnName(.      pS
11192 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76  tmt, N, (const v
11193 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71  oid*(*)(Mem*))sq
11194 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
11195 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45  16, COLNAME_NAME
11196 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  );.}.#endif../*.
11197 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 63 6f  ** Return the co
11198 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e  lumn declaration
11199 20 74 79 70 65 20 28 69 66 20 61 70 70 6c 69 63   type (if applic
1119a 61 62 6c 65 29 20 6f 66 20 74 68 65 20 27 69 27  able) of the 'i'
1119b 74 68 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f 66 20  th column.** of 
1119c 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f  the result set o
1119d 66 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  f SQL statement 
1119e 70 53 74 6d 74 2e 0a 2a 2f 0a 63 6f 6e 73 74 20  pStmt..*/.const 
1119f 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f  char *sqlite3_co
111a0 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 28 73 71  lumn_decltype(sq
111a1 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
111a2 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74  t, int N){.  ret
111a3 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a  urn columnName(.
111a4 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20        pStmt, N, 
111a5 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28  (const void*(*)(
111a6 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61  Mem*))sqlite3_va
111a7 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d  lue_text, COLNAM
111a8 45 5f 44 45 43 4c 54 59 50 45 29 3b 0a 7d 0a 23  E_DECLTYPE);.}.#
111a9 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
111aa 49 54 5f 55 54 46 31 36 0a 63 6f 6e 73 74 20 76  IT_UTF16.const v
111ab 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  oid *sqlite3_col
111ac 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 28 73  umn_decltype16(s
111ad 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
111ae 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65  mt, int N){.  re
111af 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28  turn columnName(
111b0 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c  .      pStmt, N,
111b1 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29   (const void*(*)
111b2 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76  (Mem*))sqlite3_v
111b3 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c  alue_text16, COL
111b4 4e 41 4d 45 5f 44 45 43 4c 54 59 50 45 29 3b 0a  NAME_DECLTYPE);.
111b5 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
111b6 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f  TE_OMIT_UTF16 */
111b7 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
111b8 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45  ENABLE_COLUMN_ME
111b9 54 41 44 41 54 41 0a 2f 2a 0a 2a 2a 20 52 65 74  TADATA./*.** Ret
111ba 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  urn the name of 
111bb 74 68 65 20 64 61 74 61 62 61 73 65 20 66 72 6f  the database fro
111bc 6d 20 77 68 69 63 68 20 61 20 72 65 73 75 6c 74  m which a result
111bd 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 2e   column derives.
111be 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75  .** NULL is retu
111bf 72 6e 65 64 20 69 66 20 74 68 65 20 72 65 73 75  rned if the resu
111c0 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6e 20  lt column is an 
111c1 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 63 6f  expression or co
111c2 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a 20 61 6e 79  nstant or.** any
111c3 74 68 69 6e 67 20 65 6c 73 65 20 77 68 69 63 68  thing else which
111c4 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e 61 62 69   is not an unabi
111c5 67 75 6f 75 73 20 72 65 66 65 72 65 6e 63 65 20  guous reference 
111c6 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 63 6f  to a database co
111c7 6c 75 6d 6e 2e 0a 2a 2f 0a 63 6f 6e 73 74 20 63  lumn..*/.const c
111c8 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  har *sqlite3_col
111c9 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d  umn_database_nam
111ca 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  e(sqlite3_stmt *
111cb 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20  pStmt, int N){. 
111cc 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61   return columnNa
111cd 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c  me(.      pStmt,
111ce 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a   N, (const void*
111cf 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65  (*)(Mem*))sqlite
111d0 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f  3_value_text, CO
111d1 4c 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 29 3b  LNAME_DATABASE);
111d2 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  .}.#ifndef SQLIT
111d3 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 63 6f 6e  E_OMIT_UTF16.con
111d4 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
111d5 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65  _column_database
111d6 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f  _name16(sqlite3_
111d7 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
111d8 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f   N){.  return co
111d9 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20  lumnName(.      
111da 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74  pStmt, N, (const
111db 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29   void*(*)(Mem*))
111dc 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
111dd 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 44 41  xt16, COLNAME_DA
111de 54 41 42 41 53 45 29 3b 0a 7d 0a 23 65 6e 64 69  TABASE);.}.#endi
111df 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
111e0 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  _UTF16 */../*.**
111e1 20 52 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65   Return the name
111e2 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 66 72   of the table fr
111e3 6f 6d 20 77 68 69 63 68 20 61 20 72 65 73 75 6c  om which a resul
111e4 74 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73  t column derives
111e5 2e 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74  ..** NULL is ret
111e6 75 72 6e 65 64 20 69 66 20 74 68 65 20 72 65 73  urned if the res
111e7 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6e  ult column is an
111e8 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 63   expression or c
111e9 6f 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a 20 61 6e  onstant or.** an
111ea 79 74 68 69 6e 67 20 65 6c 73 65 20 77 68 69 63  ything else whic
111eb 68 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e 61 62  h is not an unab
111ec 69 67 75 6f 75 73 20 72 65 66 65 72 65 6e 63 65  iguous reference
111ed 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 63   to a database c
111ee 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 63 6f 6e 73 74 20  olumn..*/.const 
111ef 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f  char *sqlite3_co
111f0 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 28  lumn_table_name(
111f1 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
111f2 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72  tmt, int N){.  r
111f3 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65  eturn columnName
111f4 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e  (.      pStmt, N
111f5 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a  , (const void*(*
111f6 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f  )(Mem*))sqlite3_
111f7 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e  value_text, COLN
111f8 41 4d 45 5f 54 41 42 4c 45 29 3b 0a 7d 0a 23 69  AME_TABLE);.}.#i
111f9 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
111fa 54 5f 55 54 46 31 36 0a 63 6f 6e 73 74 20 76 6f  T_UTF16.const vo
111fb 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  id *sqlite3_colu
111fc 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 28  mn_table_name16(
111fd 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
111fe 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72  tmt, int N){.  r
111ff 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65  eturn columnName
11200 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e  (.      pStmt, N
11201 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a  , (const void*(*
11202 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f  )(Mem*))sqlite3_
11203 76 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f  value_text16, CO
11204 4c 4e 41 4d 45 5f 54 41 42 4c 45 29 3b 0a 7d 0a  LNAME_TABLE);.}.
11205 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
11206 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a  _OMIT_UTF16 */..
11207 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
11208 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62   name of the tab
11209 6c 65 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 77  le column from w
1120a 68 69 63 68 20 61 20 72 65 73 75 6c 74 20 63 6f  hich a result co
1120b 6c 75 6d 6e 20 64 65 72 69 76 65 73 2e 0a 2a 2a  lumn derives..**
1120c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65   NULL is returne
1120d 64 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 20  d if the result 
1120e 63 6f 6c 75 6d 6e 20 69 73 20 61 6e 20 65 78 70  column is an exp
1120f 72 65 73 73 69 6f 6e 20 6f 72 20 63 6f 6e 73 74  ression or const
11210 61 6e 74 20 6f 72 0a 2a 2a 20 61 6e 79 74 68 69  ant or.** anythi
11211 6e 67 20 65 6c 73 65 20 77 68 69 63 68 20 69 73  ng else which is
11212 20 6e 6f 74 20 61 6e 20 75 6e 61 62 69 67 75 6f   not an unabiguo
11213 75 73 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20  us reference to 
11214 61 20 64 61 74 61 62 61 73 65 20 63 6f 6c 75 6d  a database colum
11215 6e 2e 0a 2a 2f 0a 63 6f 6e 73 74 20 63 68 61 72  n..*/.const char
11216 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
11217 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 28 73 71 6c  _origin_name(sql
11218 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
11219 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75  , int N){.  retu
1121a 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20  rn columnName(. 
1121b 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28       pStmt, N, (
1121c 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d  const void*(*)(M
1121d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c  em*))sqlite3_val
1121e 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45  ue_text, COLNAME
1121f 5f 43 4f 4c 55 4d 4e 29 3b 0a 7d 0a 23 69 66 6e  _COLUMN);.}.#ifn
11220 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
11221 55 54 46 31 36 0a 63 6f 6e 73 74 20 76 6f 69 64  UTF16.const void
11222 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
11223 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36 28 73  _origin_name16(s
11224 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
11225 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65  mt, int N){.  re
11226 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28  turn columnName(
11227 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c  .      pStmt, N,
11228 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29   (const void*(*)
11229 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76  (Mem*))sqlite3_v
1122a 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c  alue_text16, COL
1122b 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 29 3b 0a 7d 0a  NAME_COLUMN);.}.
1122c 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1122d 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 23  _OMIT_UTF16 */.#
1122e 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
1122f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45  ENABLE_COLUMN_ME
11230 54 41 44 41 54 41 20 2a 2f 0a 0a 0a 2f 2a 2a 2a  TADATA */.../***
11231 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11232 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c  ************ sql
11233 69 74 65 33 5f 62 69 6e 64 5f 20 20 2a 2a 2a 2a  ite3_bind_  ****
11234 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11235 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 0a 2a 2a 20 52  *******.** .** R
11236 6f 75 74 69 6e 65 73 20 75 73 65 64 20 74 6f 20  outines used to 
11237 61 74 74 61 63 68 20 76 61 6c 75 65 73 20 74 6f  attach values to
11238 20 77 69 6c 64 63 61 72 64 73 20 69 6e 20 61 20   wildcards in a 
11239 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 74 61  compiled SQL sta
1123a 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a  tement..*/./*.**
1123b 20 55 6e 62 69 6e 64 20 74 68 65 20 76 61 6c 75   Unbind the valu
1123c 65 20 62 6f 75 6e 64 20 74 6f 20 76 61 72 69 61  e bound to varia
1123d 62 6c 65 20 69 20 69 6e 20 76 69 72 74 75 61 6c  ble i in virtual
1123e 20 6d 61 63 68 69 6e 65 20 70 2e 20 54 68 69 73   machine p. This
1123f 20 69 73 20 74 68 65 20 0a 2a 2a 20 74 68 65 20   is the .** the 
11240 73 61 6d 65 20 61 73 20 62 69 6e 64 69 6e 67 20  same as binding 
11241 61 20 4e 55 4c 4c 20 76 61 6c 75 65 20 74 6f 20  a NULL value to 
11242 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 49 66 20 74  the column. If t
11243 68 65 20 22 69 22 20 70 61 72 61 6d 65 74 65 72  he "i" parameter
11244 20 69 73 0a 2a 2a 20 6f 75 74 20 6f 66 20 72 61   is.** out of ra
11245 6e 67 65 2c 20 74 68 65 6e 20 53 51 4c 49 54 45  nge, then SQLITE
11246 5f 52 41 4e 47 45 20 69 73 20 72 65 74 75 72 6e  _RANGE is return
11247 65 64 2e 20 4f 74 68 65 77 69 73 65 20 53 51 4c  ed. Othewise SQL
11248 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68  ITE_OK..**.** Th
11249 65 20 65 72 72 6f 72 20 63 6f 64 65 20 73 74 6f  e error code sto
1124a 72 65 64 20 69 6e 20 64 61 74 61 62 61 73 65 20  red in database 
1124b 70 2d 3e 64 62 20 69 73 20 6f 76 65 72 77 72 69  p->db is overwri
1124c 74 74 65 6e 20 77 69 74 68 20 74 68 65 20 72 65  tten with the re
1124d 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 69 6e  turn.** value in
1124e 20 61 6e 79 20 63 61 73 65 2e 0a 2a 2f 0a 73 74   any case..*/.st
1124f 61 74 69 63 20 69 6e 74 20 76 64 62 65 55 6e 62  atic int vdbeUnb
11250 69 6e 64 28 56 64 62 65 20 2a 70 2c 20 69 6e 74  ind(Vdbe *p, int
11251 20 69 29 7b 0a 20 20 4d 65 6d 20 2a 70 56 61 72   i){.  Mem *pVar
11252 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20  ;.  if( p==0 || 
11253 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d  p->magic!=VDBE_M
11254 41 47 49 43 5f 52 55 4e 20 7c 7c 20 70 2d 3e 70  AGIC_RUN || p->p
11255 63 3e 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  c>=0 ){.    if( 
11256 70 20 29 20 73 71 6c 69 74 65 33 45 72 72 6f 72  p ) sqlite3Error
11257 28 70 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f 4d  (p->db, SQLITE_M
11258 49 53 55 53 45 2c 20 30 29 3b 0a 20 20 20 20 72  ISUSE, 0);.    r
11259 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53  eturn SQLITE_MIS
1125a 55 53 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69  USE;.  }.  if( i
1125b 3c 31 20 7c 7c 20 69 3e 70 2d 3e 6e 56 61 72 20  <1 || i>p->nVar 
1125c 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
1125d 72 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49 54  ror(p->db, SQLIT
1125e 45 5f 52 41 4e 47 45 2c 20 30 29 3b 0a 20 20 20  E_RANGE, 0);.   
1125f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52   return SQLITE_R
11260 41 4e 47 45 3b 0a 20 20 7d 0a 20 20 69 2d 2d 3b  ANGE;.  }.  i--;
11261 0a 20 20 70 56 61 72 20 3d 20 26 70 2d 3e 61 56  .  pVar = &p->aV
11262 61 72 5b 69 5d 3b 0a 20 20 73 71 6c 69 74 65 33  ar[i];.  sqlite3
11263 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70  VdbeMemRelease(p
11264 56 61 72 29 3b 0a 20 20 70 56 61 72 2d 3e 66 6c  Var);.  pVar->fl
11265 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a  ags = MEM_Null;.
11266 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70    sqlite3Error(p
11267 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c  ->db, SQLITE_OK,
11268 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51   0);.  return SQ
11269 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
1126a 2a 20 42 69 6e 64 20 61 20 74 65 78 74 20 6f 72  * Bind a text or
1126b 20 42 4c 4f 42 20 76 61 6c 75 65 2e 0a 2a 2f 0a   BLOB value..*/.
1126c 73 74 61 74 69 63 20 69 6e 74 20 62 69 6e 64 54  static int bindT
1126d 65 78 74 28 0a 20 20 73 71 6c 69 74 65 33 5f 73  ext(.  sqlite3_s
1126e 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69  tmt *pStmt, .  i
1126f 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20 76  nt i, .  const v
11270 6f 69 64 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69  oid *zData, .  i
11271 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69  nt nData, .  voi
11272 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29  d (*xDel)(void*)
11273 2c 0a 20 20 69 6e 74 20 65 6e 63 6f 64 69 6e 67  ,.  int encoding
11274 0a 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20  .){.  Vdbe *p = 
11275 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20  (Vdbe *)pStmt;. 
11276 20 4d 65 6d 20 2a 70 56 61 72 3b 0a 20 20 69 6e   Mem *pVar;.  in
11277 74 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 76 64  t rc;..  rc = vd
11278 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a  beUnbind(p, i);.
11279 20 20 69 66 28 20 72 63 20 7c 7c 20 7a 44 61 74    if( rc || zDat
1127a 61 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  a==0 ){.    retu
1127b 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 56 61  rn rc;.  }.  pVa
1127c 72 20 3d 20 26 70 2d 3e 61 56 61 72 5b 69 2d 31  r = &p->aVar[i-1
1127d 5d 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  ];.  rc = sqlite
1127e 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70  3VdbeMemSetStr(p
1127f 56 61 72 2c 20 7a 44 61 74 61 2c 20 6e 44 61 74  Var, zData, nDat
11280 61 2c 20 65 6e 63 6f 64 69 6e 67 2c 20 78 44 65  a, encoding, xDe
11281 6c 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  l);.  if( rc==SQ
11282 4c 49 54 45 5f 4f 4b 20 26 26 20 65 6e 63 6f 64  LITE_OK && encod
11283 69 6e 67 21 3d 30 20 29 7b 0a 20 20 20 20 72 63  ing!=0 ){.    rc
11284 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 68   = sqlite3VdbeCh
11285 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 56 61  angeEncoding(pVa
11286 72 2c 20 45 4e 43 28 70 2d 3e 64 62 29 29 3b 0a  r, ENC(p->db));.
11287 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 45 72    }..  sqlite3Er
11288 72 6f 72 28 28 28 56 64 62 65 20 2a 29 70 53 74  ror(((Vdbe *)pSt
11289 6d 74 29 2d 3e 64 62 2c 20 72 63 2c 20 30 29 3b  mt)->db, rc, 0);
1128a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
1128b 33 41 70 69 45 78 69 74 28 28 28 56 64 62 65 20  3ApiExit(((Vdbe 
1128c 2a 29 70 53 74 6d 74 29 2d 3e 64 62 2c 20 72 63  *)pStmt)->db, rc
1128d 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e  );.}.../*.** Bin
1128e 64 20 61 20 62 6c 6f 62 20 76 61 6c 75 65 20 74  d a blob value t
1128f 6f 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65  o an SQL stateme
11290 6e 74 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2f 0a  nt variable..*/.
11291 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  int sqlite3_bind
11292 5f 62 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65 33  _blob(.  sqlite3
11293 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 20  _stmt *pStmt, . 
11294 20 69 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73 74   int i, .  const
11295 20 76 6f 69 64 20 2a 7a 44 61 74 61 2c 20 0a 20   void *zData, . 
11296 20 69 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 76   int nData, .  v
11297 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64  oid (*xDel)(void
11298 2a 29 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 62  *).){.  return b
11299 69 6e 64 54 65 78 74 28 70 53 74 6d 74 2c 20 69  indText(pStmt, i
1129a 2c 20 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20  , zData, nData, 
1129b 78 44 65 6c 2c 20 30 29 3b 0a 7d 0a 69 6e 74 20  xDel, 0);.}.int 
1129c 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64 6f 75  sqlite3_bind_dou
1129d 62 6c 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ble(sqlite3_stmt
1129e 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 2c 20   *pStmt, int i, 
1129f 64 6f 75 62 6c 65 20 72 56 61 6c 75 65 29 7b 0a  double rValue){.
112a0 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 64 62 65    int rc;.  Vdbe
112a1 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53   *p = (Vdbe *)pS
112a2 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64 62 65  tmt;.  rc = vdbe
112a3 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20 20  Unbind(p, i);.  
112a4 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
112a5 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  K ){.    sqlite3
112a6 56 64 62 65 4d 65 6d 53 65 74 44 6f 75 62 6c 65  VdbeMemSetDouble
112a7 28 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 2c 20  (&p->aVar[i-1], 
112a8 72 56 61 6c 75 65 29 3b 0a 20 20 7d 0a 20 20 72  rValue);.  }.  r
112a9 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 69 6e 74 20  eturn rc;.}.int 
112aa 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
112ab 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
112ac 2c 20 69 6e 74 20 69 2c 20 69 6e 74 20 69 56 61  , int i, int iVa
112ad 6c 75 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 73  lue){.  return s
112ae 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36  qlite3_bind_int6
112af 34 28 70 2c 20 69 2c 20 28 69 36 34 29 69 56 61  4(p, i, (i64)iVa
112b0 6c 75 65 29 3b 0a 7d 0a 69 6e 74 20 73 71 6c 69  lue);.}.int sqli
112b1 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73  te3_bind_int64(s
112b2 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
112b3 6d 74 2c 20 69 6e 74 20 69 2c 20 73 71 6c 69 74  mt, int i, sqlit
112b4 65 5f 69 6e 74 36 34 20 69 56 61 6c 75 65 29 7b  e_int64 iValue){
112b5 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 64 62  .  int rc;.  Vdb
112b6 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70  e *p = (Vdbe *)p
112b7 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64 62  Stmt;.  rc = vdb
112b8 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20  eUnbind(p, i);. 
112b9 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
112ba 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  OK ){.    sqlite
112bb 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34  3VdbeMemSetInt64
112bc 28 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 2c 20  (&p->aVar[i-1], 
112bd 69 56 61 6c 75 65 29 3b 0a 20 20 7d 0a 20 20 72  iValue);.  }.  r
112be 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 69 6e 74 20  eturn rc;.}.int 
112bf 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c  sqlite3_bind_nul
112c0 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 20  l(sqlite3_stmt* 
112c1 70 2c 20 69 6e 74 20 69 29 7b 0a 20 20 72 65 74  p, int i){.  ret
112c2 75 72 6e 20 76 64 62 65 55 6e 62 69 6e 64 28 28  urn vdbeUnbind((
112c3 56 64 62 65 20 2a 29 70 2c 20 69 29 3b 0a 7d 0a  Vdbe *)p, i);.}.
112c4 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  int sqlite3_bind
112c5 5f 74 65 78 74 28 20 0a 20 20 73 71 6c 69 74 65  _text( .  sqlite
112c6 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a  3_stmt *pStmt, .
112c7 20 20 69 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73    int i, .  cons
112c8 74 20 63 68 61 72 20 2a 7a 44 61 74 61 2c 20 0a  t char *zData, .
112c9 20 20 69 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20    int nData, .  
112ca 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69  void (*xDel)(voi
112cb 64 2a 29 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20  d*).){.  return 
112cc 62 69 6e 64 54 65 78 74 28 70 53 74 6d 74 2c 20  bindText(pStmt, 
112cd 69 2c 20 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c  i, zData, nData,
112ce 20 78 44 65 6c 2c 20 53 51 4c 49 54 45 5f 55 54   xDel, SQLITE_UT
112cf 46 38 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53  F8);.}.#ifndef S
112d0 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
112d1 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e  .int sqlite3_bin
112d2 64 5f 74 65 78 74 31 36 28 0a 20 20 73 71 6c 69  d_text16(.  sqli
112d3 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
112d4 20 0a 20 20 69 6e 74 20 69 2c 20 0a 20 20 63 6f   .  int i, .  co
112d5 6e 73 74 20 76 6f 69 64 20 2a 7a 44 61 74 61 2c  nst void *zData,
112d6 20 0a 20 20 69 6e 74 20 6e 44 61 74 61 2c 20 0a   .  int nData, .
112d7 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76    void (*xDel)(v
112d8 6f 69 64 2a 29 0a 29 7b 0a 20 20 72 65 74 75 72  oid*).){.  retur
112d9 6e 20 62 69 6e 64 54 65 78 74 28 70 53 74 6d 74  n bindText(pStmt
112da 2c 20 69 2c 20 7a 44 61 74 61 2c 20 6e 44 61 74  , i, zData, nDat
112db 61 2c 20 78 44 65 6c 2c 20 53 51 4c 49 54 45 5f  a, xDel, SQLITE_
112dc 55 54 46 31 36 4e 41 54 49 56 45 29 3b 0a 7d 0a  UTF16NATIVE);.}.
112dd 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
112de 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 69  _OMIT_UTF16 */.i
112df 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
112e0 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 73 74  value(sqlite3_st
112e1 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69  mt *pStmt, int i
112e2 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  , const sqlite3_
112e3 76 61 6c 75 65 20 2a 70 56 61 6c 75 65 29 7b 0a  value *pValue){.
112e4 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 64 62 65    int rc;.  Vdbe
112e5 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53   *p = (Vdbe *)pS
112e6 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64 62 65  tmt;.  rc = vdbe
112e7 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20 20  Unbind(p, i);.  
112e8 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
112e9 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  K ){.    sqlite3
112ea 56 64 62 65 4d 65 6d 43 6f 70 79 28 26 70 2d 3e  VdbeMemCopy(&p->
112eb 61 56 61 72 5b 69 2d 31 5d 2c 20 70 56 61 6c 75  aVar[i-1], pValu
112ec 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  e);.  }.  return
112ed 20 72 63 3b 0a 7d 0a 69 6e 74 20 73 71 6c 69 74   rc;.}.int sqlit
112ee 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62  e3_bind_zeroblob
112ef 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
112f0 53 74 6d 74 2c 20 69 6e 74 20 69 2c 20 69 6e 74  Stmt, int i, int
112f1 20 6e 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20   n){.  int rc;. 
112f2 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65   Vdbe *p = (Vdbe
112f3 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d   *)pStmt;.  rc =
112f4 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69   vdbeUnbind(p, i
112f5 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
112f6 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71  ITE_OK ){.    sq
112f7 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a  lite3VdbeMemSetZ
112f8 65 72 6f 42 6c 6f 62 28 26 70 2d 3e 61 56 61 72  eroBlob(&p->aVar
112f9 5b 69 2d 31 5d 2c 20 6e 29 3b 0a 20 20 7d 0a 20  [i-1], n);.  }. 
112fa 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
112fb 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
112fc 6e 75 6d 62 65 72 20 6f 66 20 77 69 6c 64 63 61  number of wildca
112fd 72 64 73 20 74 68 61 74 20 63 61 6e 20 62 65 20  rds that can be 
112fe 70 6f 74 65 6e 74 69 61 6c 6c 79 20 62 6f 75 6e  potentially boun
112ff 64 20 74 6f 2e 0a 2a 2a 20 54 68 69 73 20 72 6f  d to..** This ro
11300 75 74 69 6e 65 20 69 73 20 61 64 64 65 64 20 74  utine is added t
11301 6f 20 73 75 70 70 6f 72 74 20 44 42 44 3a 3a 53  o support DBD::S
11302 51 4c 69 74 65 2e 20 20 0a 2a 2f 0a 69 6e 74 20  QLite.  .*/.int 
11303 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72  sqlite3_bind_par
11304 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 73 71 6c  ameter_count(sql
11305 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
11306 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28  ){.  Vdbe *p = (
11307 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 72  Vdbe*)pStmt;.  r
11308 65 74 75 72 6e 20 70 20 3f 20 70 2d 3e 6e 56 61  eturn p ? p->nVa
11309 72 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  r : 0;.}../*.** 
1130a 43 72 65 61 74 65 20 61 20 6d 61 70 70 69 6e 67  Create a mapping
1130b 20 66 72 6f 6d 20 76 61 72 69 61 62 6c 65 20 6e   from variable n
1130c 75 6d 62 65 72 73 20 74 6f 20 76 61 72 69 61 62  umbers to variab
1130d 6c 65 20 6e 61 6d 65 73 0a 2a 2a 20 69 6e 20 74  le names.** in t
1130e 68 65 20 56 64 62 65 2e 61 7a 56 61 72 5b 5d 20  he Vdbe.azVar[] 
1130f 61 72 72 61 79 2c 20 69 66 20 73 75 63 68 20 61  array, if such a
11310 20 6d 61 70 70 69 6e 67 20 64 6f 65 73 20 6e 6f   mapping does no
11311 74 20 61 6c 72 65 61 64 79 0a 2a 2a 20 65 78 69  t already.** exi
11312 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  st..*/.static vo
11313 69 64 20 63 72 65 61 74 65 56 61 72 4d 61 70 28  id createVarMap(
11314 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 66 28 20  Vdbe *p){.  if( 
11315 21 70 2d 3e 6f 6b 56 61 72 20 29 7b 0a 20 20 20  !p->okVar ){.   
11316 20 69 6e 74 20 6a 3b 0a 20 20 20 20 4f 70 20 2a   int j;.    Op *
11317 70 4f 70 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30  pOp;.    for(j=0
11318 2c 20 70 4f 70 3d 70 2d 3e 61 4f 70 3b 20 6a 3c  , pOp=p->aOp; j<
11319 70 2d 3e 6e 4f 70 3b 20 6a 2b 2b 2c 20 70 4f 70  p->nOp; j++, pOp
1131a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  ++){.      if( p
1131b 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56  Op->opcode==OP_V
1131c 61 72 69 61 62 6c 65 20 29 7b 0a 20 20 20 20 20  ariable ){.     
1131d 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
1131e 70 31 3e 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c  p1>0 && pOp->p1<
1131f 3d 70 2d 3e 6e 56 61 72 20 29 3b 0a 20 20 20 20  =p->nVar );.    
11320 20 20 20 20 70 2d 3e 61 7a 56 61 72 5b 70 4f 70      p->azVar[pOp
11321 2d 3e 70 31 2d 31 5d 20 3d 20 70 4f 70 2d 3e 70  ->p1-1] = pOp->p
11322 33 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  3;.      }.    }
11323 0a 20 20 20 20 70 2d 3e 6f 6b 56 61 72 20 3d 20  .    p->okVar = 
11324 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  1;.  }.}../*.** 
11325 52 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20  Return the name 
11326 6f 66 20 61 20 77 69 6c 64 63 61 72 64 20 70 61  of a wildcard pa
11327 72 61 6d 65 74 65 72 2e 20 20 52 65 74 75 72 6e  rameter.  Return
11328 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 69 6e 64   NULL if the ind
11329 65 78 0a 2a 2a 20 69 73 20 6f 75 74 20 6f 66 20  ex.** is out of 
1132a 72 61 6e 67 65 20 6f 72 20 69 66 20 74 68 65 20  range or if the 
1132b 77 69 6c 64 63 61 72 64 20 69 73 20 75 6e 6e 61  wildcard is unna
1132c 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  med..**.** The r
1132d 65 73 75 6c 74 20 69 73 20 61 6c 77 61 79 73 20  esult is always 
1132e 55 54 46 2d 38 2e 0a 2a 2f 0a 63 6f 6e 73 74 20  UTF-8..*/.const 
1132f 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 62 69  char *sqlite3_bi
11330 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d  nd_parameter_nam
11331 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  e(sqlite3_stmt *
11332 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20  pStmt, int i){. 
11333 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65   Vdbe *p = (Vdbe
11334 2a 29 70 53 74 6d 74 3b 0a 20 20 69 66 28 20 70  *)pStmt;.  if( p
11335 3d 3d 30 20 7c 7c 20 69 3c 31 20 7c 7c 20 69 3e  ==0 || i<1 || i>
11336 70 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 20 20 72  p->nVar ){.    r
11337 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 63  eturn 0;.  }.  c
11338 72 65 61 74 65 56 61 72 4d 61 70 28 70 29 3b 0a  reateVarMap(p);.
11339 20 20 72 65 74 75 72 6e 20 70 2d 3e 61 7a 56 61    return p->azVa
1133a 72 5b 69 2d 31 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  r[i-1];.}../*.**
1133b 20 47 69 76 65 6e 20 61 20 77 69 6c 64 63 61 72   Given a wildcar
1133c 64 20 70 61 72 61 6d 65 74 65 72 20 6e 61 6d 65  d parameter name
1133d 2c 20 72 65 74 75 72 6e 20 74 68 65 20 69 6e 64  , return the ind
1133e 65 78 20 6f 66 20 74 68 65 20 76 61 72 69 61 62  ex of the variab
1133f 6c 65 0a 2a 2a 20 77 69 74 68 20 74 68 61 74 20  le.** with that 
11340 6e 61 6d 65 2e 20 20 49 66 20 74 68 65 72 65 20  name.  If there 
11341 69 73 20 6e 6f 20 76 61 72 69 61 62 6c 65 20 77  is no variable w
11342 69 74 68 20 74 68 65 20 67 69 76 65 6e 20 6e 61  ith the given na
11343 6d 65 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e  me,.** return 0.
11344 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f  .*/.int sqlite3_
11345 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69  bind_parameter_i
11346 6e 64 65 78 28 73 71 6c 69 74 65 33 5f 73 74 6d  ndex(sqlite3_stm
11347 74 20 2a 70 53 74 6d 74 2c 20 63 6f 6e 73 74 20  t *pStmt, const 
11348 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20  char *zName){.  
11349 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a  Vdbe *p = (Vdbe*
1134a 29 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 69 3b  )pStmt;.  int i;
1134b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20  .  if( p==0 ){. 
1134c 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
1134d 0a 20 20 63 72 65 61 74 65 56 61 72 4d 61 70 28  .  createVarMap(
1134e 70 29 3b 20 0a 20 20 69 66 28 20 7a 4e 61 6d 65  p); .  if( zName
1134f 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   ){.    for(i=0;
11350 20 69 3c 70 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29   i<p->nVar; i++)
11351 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68  {.      const ch
11352 61 72 20 2a 7a 20 3d 20 70 2d 3e 61 7a 56 61 72  ar *z = p->azVar
11353 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 7a  [i];.      if( z
11354 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 7a 4e 61   && strcmp(z,zNa
11355 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  me)==0 ){.      
11356 20 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20    return i+1;.  
11357 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
11358 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
11359 2a 0a 2a 2a 20 54 72 61 6e 73 66 65 72 20 61 6c  *.** Transfer al
1135a 6c 20 62 69 6e 64 69 6e 67 73 20 66 72 6f 6d 20  l bindings from 
1135b 74 68 65 20 66 69 72 73 74 20 73 74 61 74 65 6d  the first statem
1135c 65 6e 74 20 6f 76 65 72 20 74 6f 20 74 68 65 20  ent over to the 
1135d 73 65 63 6f 6e 64 2e 0a 2a 2a 20 49 66 20 74 68  second..** If th
1135e 65 20 74 77 6f 20 73 74 61 74 65 6d 65 6e 74 73  e two statements
1135f 20 63 6f 6e 74 61 69 6e 20 61 20 64 69 66 66 65   contain a diffe
11360 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 62  rent number of b
11361 69 6e 64 69 6e 67 73 2c 20 74 68 65 6e 0a 2a 2a  indings, then.**
11362 20 61 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52   an SQLITE_ERROR
11363 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
11364 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 72 61  .int sqlite3_tra
11365 6e 73 66 65 72 5f 62 69 6e 64 69 6e 67 73 28 73  nsfer_bindings(s
11366 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 46 72  qlite3_stmt *pFr
11367 6f 6d 53 74 6d 74 2c 20 73 71 6c 69 74 65 33 5f  omStmt, sqlite3_
11368 73 74 6d 74 20 2a 70 54 6f 53 74 6d 74 29 7b 0a  stmt *pToStmt){.
11369 20 20 56 64 62 65 20 2a 70 46 72 6f 6d 20 3d 20    Vdbe *pFrom = 
1136a 28 56 64 62 65 2a 29 70 46 72 6f 6d 53 74 6d 74  (Vdbe*)pFromStmt
1136b 3b 0a 20 20 56 64 62 65 20 2a 70 54 6f 20 3d 20  ;.  Vdbe *pTo = 
1136c 28 56 64 62 65 2a 29 70 54 6f 53 74 6d 74 3b 0a  (Vdbe*)pToStmt;.
1136d 20 20 69 6e 74 20 69 2c 20 72 63 20 3d 20 53 51    int i, rc = SQ
1136e 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 28  LITE_OK;.  if( (
1136f 70 46 72 6f 6d 2d 3e 6d 61 67 69 63 21 3d 56 44  pFrom->magic!=VD
11370 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 26 26 20  BE_MAGIC_RUN && 
11371 70 46 72 6f 6d 2d 3e 6d 61 67 69 63 21 3d 56 44  pFrom->magic!=VD
11372 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 29 0a 20  BE_MAGIC_HALT). 
11373 20 20 20 7c 7c 20 28 70 54 6f 2d 3e 6d 61 67 69     || (pTo->magi
11374 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55  c!=VDBE_MAGIC_RU
11375 4e 20 26 26 20 70 54 6f 2d 3e 6d 61 67 69 63 21  N && pTo->magic!
11376 3d 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54  =VDBE_MAGIC_HALT
11377 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
11378 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
11379 20 7d 0a 20 20 69 66 28 20 70 46 72 6f 6d 2d 3e   }.  if( pFrom->
1137a 6e 56 61 72 21 3d 70 54 6f 2d 3e 6e 56 61 72 20  nVar!=pTo->nVar 
1137b 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
1137c 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
1137d 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53    for(i=0; rc==S
1137e 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 70 46  QLITE_OK && i<pF
1137f 72 6f 6d 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b  rom->nVar; i++){
11380 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c  .    sqlite3Mall
11381 6f 63 44 69 73 61 6c 6c 6f 77 28 29 3b 0a 20 20  ocDisallow();.  
11382 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
11383 62 65 4d 65 6d 4d 6f 76 65 28 26 70 54 6f 2d 3e  beMemMove(&pTo->
11384 61 56 61 72 5b 69 5d 2c 20 26 70 46 72 6f 6d 2d  aVar[i], &pFrom-
11385 3e 61 56 61 72 5b 69 5d 29 3b 0a 20 20 20 20 73  >aVar[i]);.    s
11386 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 6c 6f  qlite3MallocAllo
11387 77 28 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  w();.  }.  asser
11388 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
11389 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e   || rc==SQLITE_N
1138a 4f 4d 45 4d 20 29 3b 0a 20 20 72 65 74 75 72 6e  OMEM );.  return
1138b 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
1138c 74 75 72 6e 20 74 68 65 20 73 71 6c 69 74 65 33  turn the sqlite3
1138d 2a 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  * database handl
1138e 65 20 74 6f 20 77 68 69 63 68 20 74 68 65 20 70  e to which the p
1138f 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
11390 74 20 67 69 76 65 6e 0a 2a 2a 20 69 6e 20 74 68  t given.** in th
11391 65 20 61 72 67 75 6d 65 6e 74 20 62 65 6c 6f 6e  e argument belon
11392 67 73 2e 20 20 54 68 69 73 20 69 73 20 74 68 65  gs.  This is the
11393 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 68   same database h
11394 61 6e 64 6c 65 20 74 68 61 74 20 77 61 73 0a 2a  andle that was.*
11395 2a 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  * the first argu
11396 6d 65 6e 74 20 74 6f 20 74 68 65 20 73 71 6c 69  ment to the sqli
11397 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 74 68  te3_prepare() th
11398 61 74 20 77 61 73 20 75 73 65 64 20 74 6f 20 63  at was used to c
11399 72 65 61 74 65 0a 2a 2a 20 74 68 65 20 73 74 61  reate.** the sta
1139a 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 66 69  tement in the fi
1139b 72 73 74 20 70 6c 61 63 65 2e 0a 2a 2f 0a 73 71  rst place..*/.sq
1139c 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 5f 64  lite3 *sqlite3_d
1139d 62 5f 68 61 6e 64 6c 65 28 73 71 6c 69 74 65 33  b_handle(sqlite3
1139e 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20  _stmt *pStmt){. 
1139f 20 72 65 74 75 72 6e 20 70 53 74 6d 74 20 3f 20   return pStmt ? 
113a0 28 28 56 64 62 65 2a 29 70 53 74 6d 74 29 2d 3e  ((Vdbe*)pStmt)->
113a1 64 62 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  db : 0;.}../****
113a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
113a3 66 20 76 64 62 65 61 70 69 2e 63 20 2a 2a 2a 2a  f vdbeapi.c ****
113a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
113a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
113a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
113a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
113a8 20 66 69 6c 65 20 76 64 62 65 2e 63 20 2a 2a 2a   file vdbe.c ***
113a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
113aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
113ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
113ac 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
113ad 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
113ae 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
113af 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
113b0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
113b1 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
113b2 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
113b3 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
113b4 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
113b5 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
113b6 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
113b7 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
113b8 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
113b9 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
113ba 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
113bb 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
113bc 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
113bd 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
113be 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
113bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
113c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
113c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
113c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
113c3 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e  *.** The code in
113c4 20 74 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65   this file imple
113c5 6d 65 6e 74 73 20 65 78 65 63 75 74 69 6f 6e 20  ments execution 
113c6 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 0a 2a  method of the .*
113c7 2a 20 56 69 72 74 75 61 6c 20 44 61 74 61 62 61  * Virtual Databa
113c8 73 65 20 45 6e 67 69 6e 65 20 28 56 44 42 45 29  se Engine (VDBE)
113c9 2e 20 20 41 20 73 65 70 61 72 61 74 65 20 66 69  .  A separate fi
113ca 6c 65 20 28 22 76 64 62 65 61 75 78 2e 63 22 29  le ("vdbeaux.c")
113cb 0a 2a 2a 20 68 61 6e 64 6c 65 73 20 68 6f 75 73  .** handles hous
113cc 65 6b 65 65 70 69 6e 67 20 64 65 74 61 69 6c 73  ekeeping details
113cd 20 73 75 63 68 20 61 73 20 63 72 65 61 74 69 6e   such as creatin
113ce 67 20 61 6e 64 20 64 65 6c 65 74 69 6e 67 0a 2a  g and deleting.*
113cf 2a 20 56 44 42 45 20 69 6e 73 74 61 6e 63 65 73  * VDBE instances
113d0 2e 20 20 54 68 69 73 20 66 69 6c 65 20 69 73 20  .  This file is 
113d1 73 6f 6c 65 6c 79 20 69 6e 74 65 72 65 73 74 65  solely intereste
113d2 64 20 69 6e 20 65 78 65 63 75 74 69 6e 67 0a 2a  d in executing.*
113d3 2a 20 74 68 65 20 56 44 42 45 20 70 72 6f 67 72  * the VDBE progr
113d4 61 6d 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65  am..**.** In the
113d5 20 65 78 74 65 72 6e 61 6c 20 69 6e 74 65 72 66   external interf
113d6 61 63 65 2c 20 61 6e 20 22 73 71 6c 69 74 65 33  ace, an "sqlite3
113d7 5f 73 74 6d 74 2a 22 20 69 73 20 61 6e 20 6f 70  _stmt*" is an op
113d8 61 71 75 65 20 70 6f 69 6e 74 65 72 0a 2a 2a 20  aque pointer.** 
113d9 74 6f 20 61 20 56 44 42 45 2e 0a 2a 2a 0a 2a 2a  to a VDBE..**.**
113da 20 54 68 65 20 53 51 4c 20 70 61 72 73 65 72 20   The SQL parser 
113db 67 65 6e 65 72 61 74 65 73 20 61 20 70 72 6f 67  generates a prog
113dc 72 61 6d 20 77 68 69 63 68 20 69 73 20 74 68 65  ram which is the
113dd 6e 20 65 78 65 63 75 74 65 64 20 62 79 0a 2a 2a  n executed by.**
113de 20 74 68 65 20 56 44 42 45 20 74 6f 20 64 6f 20   the VDBE to do 
113df 74 68 65 20 77 6f 72 6b 20 6f 66 20 74 68 65 20  the work of the 
113e0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  SQL statement.  
113e1 56 44 42 45 20 70 72 6f 67 72 61 6d 73 20 61 72  VDBE programs ar
113e2 65 20 0a 2a 2a 20 73 69 6d 69 6c 61 72 20 69 6e  e .** similar in
113e3 20 66 6f 72 6d 20 74 6f 20 61 73 73 65 6d 62 6c   form to assembl
113e4 79 20 6c 61 6e 67 75 61 67 65 2e 20 20 54 68 65  y language.  The
113e5 20 70 72 6f 67 72 61 6d 20 63 6f 6e 73 69 73 74   program consist
113e6 73 20 6f 66 0a 2a 2a 20 61 20 6c 69 6e 65 61 72  s of.** a linear
113e7 20 73 65 71 75 65 6e 63 65 20 6f 66 20 6f 70 65   sequence of ope
113e8 72 61 74 69 6f 6e 73 2e 20 20 45 61 63 68 20 6f  rations.  Each o
113e9 70 65 72 61 74 69 6f 6e 20 68 61 73 20 61 6e 20  peration has an 
113ea 6f 70 63 6f 64 65 20 0a 2a 2a 20 61 6e 64 20 33  opcode .** and 3
113eb 20 6f 70 65 72 61 6e 64 73 2e 20 20 4f 70 65 72   operands.  Oper
113ec 61 6e 64 73 20 50 31 20 61 6e 64 20 50 32 20 61  ands P1 and P2 a
113ed 72 65 20 69 6e 74 65 67 65 72 73 2e 20 20 4f 70  re integers.  Op
113ee 65 72 61 6e 64 20 50 33 20 0a 2a 2a 20 69 73 20  erand P3 .** is 
113ef 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65  a null-terminate
113f0 64 20 73 74 72 69 6e 67 2e 20 20 20 54 68 65 20  d string.   The 
113f1 50 32 20 6f 70 65 72 61 6e 64 20 6d 75 73 74 20  P2 operand must 
113f2 62 65 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e  be non-negative.
113f3 0a 2a 2a 20 4f 70 63 6f 64 65 73 20 77 69 6c 6c  .** Opcodes will
113f4 20 74 79 70 69 63 61 6c 6c 79 20 69 67 6e 6f 72   typically ignor
113f5 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 70  e one or more op
113f6 65 72 61 6e 64 73 2e 20 20 4d 61 6e 79 20 6f 70  erands.  Many op
113f7 63 6f 64 65 73 0a 2a 2a 20 69 67 6e 6f 72 65 20  codes.** ignore 
113f8 61 6c 6c 20 74 68 72 65 65 20 6f 70 65 72 61 6e  all three operan
113f9 64 73 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 75 74  ds..**.** Comput
113fa 61 74 69 6f 6e 20 72 65 73 75 6c 74 73 20 61 72  ation results ar
113fb 65 20 73 74 6f 72 65 64 20 6f 6e 20 61 20 73 74  e stored on a st
113fc 61 63 6b 2e 20 20 45 61 63 68 20 65 6e 74 72 79  ack.  Each entry
113fd 20 6f 6e 20 74 68 65 0a 2a 2a 20 73 74 61 63 6b   on the.** stack
113fe 20 69 73 20 65 69 74 68 65 72 20 61 6e 20 69 6e   is either an in
113ff 74 65 67 65 72 2c 20 61 20 6e 75 6c 6c 2d 74 65  teger, a null-te
11400 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67 2c  rminated string,
11401 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e   a floating poin
11402 74 0a 2a 2a 20 6e 75 6d 62 65 72 2c 20 6f 72 20  t.** number, or 
11403 74 68 65 20 53 51 4c 20 22 4e 55 4c 4c 22 20 76  the SQL "NULL" v
11404 61 6c 75 65 2e 20 20 41 6e 20 69 6e 70 6c 69 63  alue.  An inplic
11405 69 74 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 72  it conversion fr
11406 6f 6d 20 6f 6e 65 0a 2a 2a 20 74 79 70 65 20 74  om one.** type t
11407 6f 20 74 68 65 20 6f 74 68 65 72 20 6f 63 63 75  o the other occu
11408 72 73 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e  rs as necessary.
11409 0a 2a 2a 20 0a 2a 2a 20 4d 6f 73 74 20 6f 66 20  .** .** Most of 
1140a 74 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73  the code in this
1140b 20 66 69 6c 65 20 69 73 20 74 61 6b 65 6e 20 75   file is taken u
1140c 70 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33  p by the sqlite3
1140d 56 64 62 65 45 78 65 63 28 29 0a 2a 2a 20 66 75  VdbeExec().** fu
1140e 6e 63 74 69 6f 6e 20 77 68 69 63 68 20 64 6f 65  nction which doe
1140f 73 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 69 6e  s the work of in
11410 74 65 72 70 72 65 74 69 6e 67 20 61 20 56 44 42  terpreting a VDB
11411 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 20 42 75  E program..** Bu
11412 74 20 6f 74 68 65 72 20 72 6f 75 74 69 6e 65 73  t other routines
11413 20 61 72 65 20 61 6c 73 6f 20 70 72 6f 76 69 64   are also provid
11414 65 64 20 74 6f 20 68 65 6c 70 20 69 6e 20 62 75  ed to help in bu
11415 69 6c 64 69 6e 67 20 75 70 0a 2a 2a 20 61 20 70  ilding up.** a p
11416 72 6f 67 72 61 6d 20 69 6e 73 74 72 75 63 74 69  rogram instructi
11417 6f 6e 20 62 79 20 69 6e 73 74 72 75 63 74 69 6f  on by instructio
11418 6e 2e 0a 2a 2a 0a 2a 2a 20 56 61 72 69 6f 75 73  n..**.** Various
11419 20 73 63 72 69 70 74 73 20 73 63 61 6e 20 74 68   scripts scan th
1141a 69 73 20 73 6f 75 72 63 65 20 66 69 6c 65 20 69  is source file i
1141b 6e 20 6f 72 64 65 72 20 74 6f 20 67 65 6e 65 72  n order to gener
1141c 61 74 65 20 48 54 4d 4c 0a 2a 2a 20 64 6f 63 75  ate HTML.** docu
1141d 6d 65 6e 74 61 74 69 6f 6e 2c 20 68 65 61 64 65  mentation, heade
1141e 72 73 20 66 69 6c 65 73 2c 20 6f 72 20 6f 74 68  rs files, or oth
1141f 65 72 20 64 65 72 69 76 65 64 20 66 69 6c 65 73  er derived files
11420 2e 20 20 54 68 65 20 66 6f 72 6d 61 74 74 69 6e  .  The formattin
11421 67 0a 2a 2a 20 6f 66 20 74 68 65 20 63 6f 64 65  g.** of the code
11422 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73   in this file is
11423 2c 20 74 68 65 72 65 66 6f 72 65 2c 20 69 6d 70  , therefore, imp
11424 6f 72 74 61 6e 74 2e 20 20 53 65 65 20 6f 74 68  ortant.  See oth
11425 65 72 20 63 6f 6d 6d 65 6e 74 73 0a 2a 2a 20 69  er comments.** i
11426 6e 20 74 68 69 73 20 66 69 6c 65 20 66 6f 72 20  n this file for 
11427 64 65 74 61 69 6c 73 2e 20 20 49 66 20 69 6e 20  details.  If in 
11428 64 6f 75 62 74 2c 20 64 6f 20 6e 6f 74 20 64 65  doubt, do not de
11429 76 69 61 74 65 20 66 72 6f 6d 20 65 78 69 73 74  viate from exist
1142a 69 6e 67 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 69 6e  ing.** commentin
1142b 67 20 61 6e 64 20 69 6e 64 65 6e 74 61 74 69 6f  g and indentatio
1142c 6e 20 70 72 61 63 74 69 63 65 73 20 77 68 65 6e  n practices when
1142d 20 63 68 61 6e 67 69 6e 67 20 6f 72 20 61 64 64   changing or add
1142e 69 6e 67 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20  ing code..**.** 
1142f 24 49 64 3a 20 76 64 62 65 2e 63 2c 76 20 31 2e  $Id: vdbe.c,v 1.
11430 36 33 38 20 32 30 30 37 2f 30 37 2f 32 32 20 31  638 2007/07/22 1
11431 39 3a 31 30 3a 32 31 20 64 72 68 20 45 78 70 20  9:10:21 drh Exp 
11432 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  $.*/../*.** The 
11433 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c  following global
11434 20 76 61 72 69 61 62 6c 65 20 69 73 20 69 6e 63   variable is inc
11435 72 65 6d 65 6e 74 65 64 20 65 76 65 72 79 20 74  remented every t
11436 69 6d 65 20 61 20 63 75 72 73 6f 72 0a 2a 2a 20  ime a cursor.** 
11437 6d 6f 76 65 73 2c 20 65 69 74 68 65 72 20 62 79  moves, either by
11438 20 74 68 65 20 4f 50 5f 4d 6f 76 65 58 58 2c 20   the OP_MoveXX, 
11439 4f 50 5f 4e 65 78 74 2c 20 6f 72 20 4f 50 5f 50  OP_Next, or OP_P
1143a 72 65 76 20 6f 70 63 6f 64 65 73 2e 20 20 54 68  rev opcodes.  Th
1143b 65 20 74 65 73 74 0a 2a 2a 20 70 72 6f 63 65 64  e test.** proced
1143c 75 72 65 73 20 75 73 65 20 74 68 69 73 20 69 6e  ures use this in
1143d 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 6b  formation to mak
1143e 65 20 73 75 72 65 20 74 68 61 74 20 69 6e 64 69  e sure that indi
1143f 63 65 73 20 61 72 65 0a 2a 2a 20 77 6f 72 6b 69  ces are.** worki
11440 6e 67 20 63 6f 72 72 65 63 74 6c 79 2e 20 20 54  ng correctly.  T
11441 68 69 73 20 76 61 72 69 61 62 6c 65 20 68 61 73  his variable has
11442 20 6e 6f 20 66 75 6e 63 74 69 6f 6e 20 6f 74 68   no function oth
11443 65 72 20 74 68 61 6e 20 74 6f 0a 2a 2a 20 68 65  er than to.** he
11444 6c 70 20 76 65 72 69 66 79 20 74 68 65 20 63 6f  lp verify the co
11445 72 72 65 63 74 20 6f 70 65 72 61 74 69 6f 6e 20  rrect operation 
11446 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 2e 0a  of the library..
11447 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
11448 5f 54 45 53 54 0a 69 6e 74 20 73 71 6c 69 74 65  _TEST.int sqlite
11449 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 20 3d  3_search_count =
1144a 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a   0;.#endif../*.*
1144b 2a 20 57 68 65 6e 20 74 68 69 73 20 67 6c 6f 62  * When this glob
1144c 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73 20 70  al variable is p
1144d 6f 73 69 74 69 76 65 2c 20 69 74 20 67 65 74 73  ositive, it gets
1144e 20 64 65 63 72 65 6d 65 6e 74 65 64 20 6f 6e 63   decremented onc
1144f 65 20 62 65 66 6f 72 65 0a 2a 2a 20 65 61 63 68  e before.** each
11450 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 20   instruction in 
11451 74 68 65 20 56 44 42 45 2e 20 20 57 68 65 6e 20  the VDBE.  When 
11452 72 65 61 63 68 65 73 20 7a 65 72 6f 2c 20 74 68  reaches zero, th
11453 65 20 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74  e u1.isInterrupt
11454 65 64 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 20 74  ed.** field of t
11455 68 65 20 73 71 6c 69 74 65 33 20 73 74 72 75 63  he sqlite3 struc
11456 74 75 72 65 20 69 73 20 73 65 74 20 69 6e 20 6f  ture is set in o
11457 72 64 65 72 20 74 6f 20 73 69 6d 75 6c 61 74 65  rder to simulate
11458 20 61 6e 64 20 69 6e 74 65 72 72 75 70 74 2e 0a   and interrupt..
11459 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 61 63 69 6c  **.** This facil
1145a 69 74 79 20 69 73 20 75 73 65 64 20 66 6f 72 20  ity is used for 
1145b 74 65 73 74 69 6e 67 20 70 75 72 70 6f 73 65 73  testing purposes
1145c 20 6f 6e 6c 79 2e 20 20 49 74 20 64 6f 65 73 20   only.  It does 
1145d 6e 6f 74 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  not function.** 
1145e 69 6e 20 61 6e 20 6f 72 64 69 6e 61 72 79 20 62  in an ordinary b
1145f 75 69 6c 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  uild..*/.#ifdef 
11460 53 51 4c 49 54 45 5f 54 45 53 54 0a 69 6e 74 20  SQLITE_TEST.int 
11461 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70  sqlite3_interrup
11462 74 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e  t_count = 0;.#en
11463 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e  dif../*.** The n
11464 65 78 74 20 67 6c 6f 62 61 6c 20 76 61 72 69 61  ext global varia
11465 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74  ble is increment
11466 65 64 20 65 61 63 68 20 74 79 70 65 20 74 68 65  ed each type the
11467 20 4f 50 5f 53 6f 72 74 20 6f 70 63 6f 64 65 0a   OP_Sort opcode.
11468 2a 2a 20 69 73 20 65 78 65 63 75 74 65 64 2e 20  ** is executed. 
11469 20 54 68 65 20 74 65 73 74 20 70 72 6f 63 65 64   The test proced
1146a 75 72 65 73 20 75 73 65 20 74 68 69 73 20 69 6e  ures use this in
1146b 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 6b  formation to mak
1146c 65 20 73 75 72 65 20 74 68 61 74 0a 2a 2a 20 73  e sure that.** s
1146d 6f 72 74 69 6e 67 20 69 73 20 6f 63 63 75 72 72  orting is occurr
1146e 69 6e 67 20 6f 72 20 6e 6f 74 20 6f 63 63 75 72  ing or not occur
1146f 69 6e 67 20 61 74 20 61 70 70 72 6f 70 72 69 61  ing at appropria
11470 74 65 20 74 69 6d 65 73 2e 20 20 20 54 68 69 73  te times.   This
11471 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 68 61 73   variable.** has
11472 20 6e 6f 20 66 75 6e 63 74 69 6f 6e 20 6f 74 68   no function oth
11473 65 72 20 74 68 61 6e 20 74 6f 20 68 65 6c 70 20  er than to help 
11474 76 65 72 69 66 79 20 74 68 65 20 63 6f 72 72 65  verify the corre
11475 63 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20  ct operation of 
11476 74 68 65 0a 2a 2a 20 6c 69 62 72 61 72 79 2e 0a  the.** library..
11477 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
11478 5f 54 45 53 54 0a 69 6e 74 20 73 71 6c 69 74 65  _TEST.int sqlite
11479 33 5f 73 6f 72 74 5f 63 6f 75 6e 74 20 3d 20 30  3_sort_count = 0
1147a 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ;.#endif../*.** 
1147b 54 68 65 20 6e 65 78 74 20 67 6c 6f 62 61 6c 20  The next global 
1147c 76 61 72 69 61 62 6c 65 20 72 65 63 6f 72 64 73  variable records
1147d 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
1147e 20 6c 61 72 67 65 73 74 20 4d 45 4d 5f 42 6c 6f   largest MEM_Blo
1147f 62 0a 2a 2a 20 6f 72 20 4d 45 4d 5f 53 74 72 20  b.** or MEM_Str 
11480 74 68 61 74 20 68 61 73 20 61 70 70 65 61 72 65  that has appeare
11481 64 20 6f 6e 20 74 68 65 20 56 44 42 45 20 73 74  d on the VDBE st
11482 61 63 6b 2e 20 20 54 68 65 20 74 65 73 74 20 70  ack.  The test p
11483 72 6f 63 65 64 75 72 65 73 0a 2a 2a 20 75 73 65  rocedures.** use
11484 20 74 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f   this informatio
11485 6e 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  n to make sure t
11486 68 61 74 20 74 68 65 20 7a 65 72 6f 2d 62 6c 6f  hat the zero-blo
11487 62 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 0a  b functionality.
11488 2a 2a 20 69 73 20 77 6f 72 6b 69 6e 67 20 63 6f  ** is working co
11489 72 72 65 63 74 6c 79 2e 20 20 20 54 68 69 73 20  rrectly.   This 
1148a 76 61 72 69 61 62 6c 65 20 68 61 73 20 6e 6f 20  variable has no 
1148b 66 75 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20 74  function other t
1148c 68 61 6e 20 74 6f 0a 2a 2a 20 68 65 6c 70 20 76  han to.** help v
1148d 65 72 69 66 79 20 74 68 65 20 63 6f 72 72 65 63  erify the correc
1148e 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74  t operation of t
1148f 68 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23  he library..*/.#
11490 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
11491 54 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 61  T.int sqlite3_ma
11492 78 5f 62 6c 6f 62 73 69 7a 65 20 3d 20 30 3b 0a  x_blobsize = 0;.
11493 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65  #endif../*.** Re
11494 6c 65 61 73 65 20 74 68 65 20 6d 65 6d 6f 72 79  lease the memory
11495 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
11496 20 74 68 65 20 67 69 76 65 6e 20 73 74 61 63 6b   the given stack
11497 20 6c 65 76 65 6c 2e 20 20 54 68 69 73 0a 2a 2a   level.  This.**
11498 20 6c 65 61 76 65 73 20 74 68 65 20 4d 65 6d 2e   leaves the Mem.
11499 66 6c 61 67 73 20 66 69 65 6c 64 20 69 6e 20 61  flags field in a
1149a 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73  n inconsistent s
1149b 74 61 74 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  tate..*/.#define
1149c 20 52 65 6c 65 61 73 65 28 50 29 20 69 66 28 28   Release(P) if((
1149d 50 29 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79  P)->flags&MEM_Dy
1149e 6e 29 7b 20 73 71 6c 69 74 65 33 56 64 62 65 4d  n){ sqlite3VdbeM
1149f 65 6d 52 65 6c 65 61 73 65 28 50 29 3b 20 7d 0a  emRelease(P); }.
114a0 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74  ./*.** Convert t
114a1 68 65 20 67 69 76 65 6e 20 73 74 61 63 6b 20 65  he given stack e
114a2 6e 74 69 74 79 20 69 6e 74 6f 20 61 20 73 74 72  ntity into a str
114a3 69 6e 67 20 69 66 20 69 74 20 69 73 6e 27 74 20  ing if it isn't 
114a4 6f 6e 65 0a 2a 2a 20 61 6c 72 65 61 64 79 2e 20  one.** already. 
114a5 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20  Return non-zero 
114a6 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61  if a malloc() fa
114a7 69 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ils..*/.#define 
114a8 53 74 72 69 6e 67 69 66 79 28 50 2c 20 65 6e 63  Stringify(P, enc
114a9 29 20 5c 0a 20 20 20 69 66 28 28 28 50 29 2d 3e  ) \.   if(((P)->
114aa 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c 4d  flags&(MEM_Str|M
114ab 45 4d 5f 42 6c 6f 62 29 29 3d 3d 30 20 26 26 20  EM_Blob))==0 && 
114ac 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74  sqlite3VdbeMemSt
114ad 72 69 6e 67 69 66 79 28 50 2c 65 6e 63 29 29 20  ringify(P,enc)) 
114ae 5c 0a 20 20 20 20 20 7b 20 67 6f 74 6f 20 6e 6f  \.     { goto no
114af 5f 6d 65 6d 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 43  _mem; }../*.** C
114b0 6f 6e 76 65 72 74 20 74 68 65 20 67 69 76 65 6e  onvert the given
114b1 20 73 74 61 63 6b 20 65 6e 74 69 74 79 20 69 6e   stack entity in
114b2 74 6f 20 61 20 73 74 72 69 6e 67 20 74 68 61 74  to a string that
114b3 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69 6e   has been obtain
114b4 65 64 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74  ed.** from sqlit
114b5 65 4d 61 6c 6c 6f 63 28 29 2e 20 20 54 68 69 73  eMalloc().  This
114b6 20 69 73 20 64 69 66 66 65 72 65 6e 74 20 66 72   is different fr
114b7 6f 6d 20 53 74 72 69 6e 67 69 66 79 28 29 20 61  om Stringify() a
114b8 62 6f 76 65 20 69 6e 20 74 68 61 74 0a 2a 2a 20  bove in that.** 
114b9 53 74 72 69 6e 67 69 66 79 28 29 20 77 69 6c 6c  Stringify() will
114ba 20 75 73 65 20 74 68 65 20 4e 42 46 53 20 62 79   use the NBFS by
114bb 74 65 73 20 6f 66 20 73 74 61 74 69 63 20 73 74  tes of static st
114bc 72 69 6e 67 20 73 70 61 63 65 20 69 66 20 74 68  ring space if th
114bd 65 20 73 74 72 69 6e 67 0a 2a 2a 20 77 69 6c 6c  e string.** will
114be 20 66 69 74 20 62 75 74 20 74 68 69 73 20 72 6f   fit but this ro
114bf 75 74 69 6e 65 20 61 6c 77 61 79 73 20 6d 61 6c  utine always mal
114c0 6c 6f 63 73 20 66 6f 72 20 73 70 61 63 65 2e 0a  locs for space..
114c1 2a 2a 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65  ** Return non-ze
114c2 72 6f 20 69 66 20 77 65 20 72 75 6e 20 6f 75 74  ro if we run out
114c3 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 23   of memory..*/.#
114c4 64 65 66 69 6e 65 20 44 79 6e 61 6d 69 63 69 66  define Dynamicif
114c5 79 28 50 2c 65 6e 63 29 20 73 71 6c 69 74 65 33  y(P,enc) sqlite3
114c6 56 64 62 65 4d 65 6d 44 79 6e 61 6d 69 63 69 66  VdbeMemDynamicif
114c7 79 28 50 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  y(P)../*.** The 
114c8 68 65 61 64 65 72 20 6f 66 20 61 20 72 65 63 6f  header of a reco
114c9 72 64 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61  rd consists of a
114ca 20 73 65 71 75 65 6e 63 65 20 76 61 72 69 61 62   sequence variab
114cb 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65  le-length intege
114cc 72 73 2e 0a 2a 2a 20 54 68 65 73 65 20 69 6e 74  rs..** These int
114cd 65 67 65 72 73 20 61 72 65 20 61 6c 6d 6f 73 74  egers are almost
114ce 20 61 6c 77 61 79 73 20 73 6d 61 6c 6c 20 61 6e   always small an
114cf 64 20 61 72 65 20 65 6e 63 6f 64 65 64 20 61 73  d are encoded as
114d0 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 2e 0a   a single byte..
114d1 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
114d2 20 6d 61 63 72 6f 20 74 61 6b 65 73 20 61 64 76   macro takes adv
114d3 61 6e 74 61 67 65 20 74 68 69 73 20 66 61 63 74  antage this fact
114d4 20 74 6f 20 70 72 6f 76 69 64 65 20 61 20 66 61   to provide a fa
114d5 73 74 20 64 65 63 6f 64 65 0a 2a 2a 20 6f 66 20  st decode.** of 
114d6 74 68 65 20 69 6e 74 65 67 65 72 73 20 69 6e 20  the integers in 
114d7 61 20 72 65 63 6f 72 64 20 68 65 61 64 65 72 2e  a record header.
114d8 20 20 49 74 20 69 73 20 66 61 73 74 65 72 20 66    It is faster f
114d9 6f 72 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 61  or the common ca
114da 73 65 0a 2a 2a 20 77 68 65 72 65 20 74 68 65 20  se.** where the 
114db 69 6e 74 65 67 65 72 20 69 73 20 61 20 73 69 6e  integer is a sin
114dc 67 6c 65 20 62 79 74 65 2e 20 20 49 74 20 69 73  gle byte.  It is
114dd 20 61 20 6c 69 74 74 6c 65 20 73 6c 6f 77 65 72   a little slower
114de 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 69 6e 74   when the.** int
114df 65 67 65 72 20 69 73 20 74 77 6f 20 6f 72 20 6d  eger is two or m
114e0 6f 72 65 20 62 79 74 65 73 2e 20 20 42 75 74 20  ore bytes.  But 
114e1 6f 76 65 72 61 6c 6c 20 69 74 20 69 73 20 66 61  overall it is fa
114e2 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ster..**.** The 
114e3 66 6f 6c 6c 6f 77 69 6e 67 20 65 78 70 72 65 73  following expres
114e4 73 69 6f 6e 73 20 61 72 65 20 65 71 75 69 76 61  sions are equiva
114e5 6c 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  lent:.**.**     
114e6 78 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 61  x = sqlite3GetVa
114e7 72 69 6e 74 33 32 28 20 41 2c 20 26 42 20 29 3b  rint32( A, &B );
114e8 0a 2a 2a 0a 2a 2a 20 20 20 20 20 78 20 3d 20 47  .**.**     x = G
114e9 65 74 56 61 72 69 6e 74 28 20 41 2c 20 42 20 29  etVarint( A, B )
114ea 3b 0a 2a 2a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ;.**.*/.#define 
114eb 47 65 74 56 61 72 69 6e 74 28 41 2c 42 29 20 20  GetVarint(A,B)  
114ec 28 28 42 20 3d 20 2a 28 41 29 29 3c 3d 30 78 37  ((B = *(A))<=0x7
114ed 66 20 3f 20 31 20 3a 20 73 71 6c 69 74 65 33 47  f ? 1 : sqlite3G
114ee 65 74 56 61 72 69 6e 74 33 32 28 41 2c 20 26 42  etVarint32(A, &B
114ef 29 29 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 65 70 68  ))../*.** An eph
114f0 65 6d 65 72 61 6c 20 73 74 72 69 6e 67 20 76 61  emeral string va
114f1 6c 75 65 20 28 73 69 67 6e 69 66 69 65 64 20 62  lue (signified b
114f2 79 20 74 68 65 20 4d 45 4d 5f 45 70 68 65 6d 20  y the MEM_Ephem 
114f3 66 6c 61 67 29 20 63 6f 6e 74 61 69 6e 73 0a 2a  flag) contains.*
114f4 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  * a pointer to a
114f5 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c   dynamically all
114f6 6f 63 61 74 65 64 20 73 74 72 69 6e 67 20 77 68  ocated string wh
114f7 65 72 65 20 73 6f 6d 65 20 6f 74 68 65 72 20 65  ere some other e
114f8 6e 74 69 74 79 0a 2a 2a 20 69 73 20 72 65 73 70  ntity.** is resp
114f9 6f 6e 73 69 62 6c 65 20 66 6f 72 20 64 65 61 6c  onsible for deal
114fa 6c 6f 63 61 74 69 6e 67 20 74 68 61 74 20 73 74  locating that st
114fb 72 69 6e 67 2e 20 20 42 65 63 61 75 73 65 20 74  ring.  Because t
114fc 68 65 20 73 74 61 63 6b 20 65 6e 74 72 79 0a 2a  he stack entry.*
114fd 2a 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 72  * does not contr
114fe 6f 6c 20 74 68 65 20 73 74 72 69 6e 67 2c 20 69  ol the string, i
114ff 74 20 6d 69 67 68 74 20 62 65 20 64 65 6c 65 74  t might be delet
11500 65 64 20 77 69 74 68 6f 75 74 20 74 68 65 20 73  ed without the s
11501 74 61 63 6b 0a 2a 2a 20 65 6e 74 72 79 20 6b 6e  tack.** entry kn
11502 6f 77 69 6e 67 20 69 74 2e 0a 2a 2a 0a 2a 2a 20  owing it..**.** 
11503 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6f 6e  This routine con
11504 76 65 72 74 73 20 61 6e 20 65 70 68 65 6d 65 72  verts an ephemer
11505 61 6c 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61  al string into a
11506 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c   dynamically all
11507 6f 63 61 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67  ocated.** string
11508 20 74 68 61 74 20 74 68 65 20 73 74 61 63 6b 20   that the stack 
11509 65 6e 74 72 79 20 69 74 73 65 6c 66 20 63 6f 6e  entry itself con
1150a 74 72 6f 6c 73 2e 20 20 49 6e 20 6f 74 68 65 72  trols.  In other
1150b 20 77 6f 72 64 73 2c 20 69 74 0a 2a 2a 20 63 6f   words, it.** co
1150c 6e 76 65 72 74 73 20 61 6e 20 4d 45 4d 5f 45 70  nverts an MEM_Ep
1150d 68 65 6d 20 73 74 72 69 6e 67 20 69 6e 74 6f 20  hem string into 
1150e 61 6e 20 4d 45 4d 5f 44 79 6e 20 73 74 72 69 6e  an MEM_Dyn strin
1150f 67 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 44 65  g..*/.#define De
11510 65 70 68 65 6d 65 72 61 6c 69 7a 65 28 50 29 20  ephemeralize(P) 
11511 5c 0a 20 20 20 69 66 28 20 28 28 50 29 2d 3e 66  \.   if( ((P)->f
11512 6c 61 67 73 26 4d 45 4d 5f 45 70 68 65 6d 29 21  lags&MEM_Ephem)!
11513 3d 30 20 5c 0a 20 20 20 20 20 20 20 26 26 20 73  =0 \.       && s
11514 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b  qlite3VdbeMemMak
11515 65 57 72 69 74 65 61 62 6c 65 28 50 29 20 29 7b  eWriteable(P) ){
11516 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 7d 0a 0a   goto no_mem;}..
11517 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74  /*.** Call sqlit
11518 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42  e3VdbeMemExpandB
11519 6c 6f 62 28 29 20 6f 6e 20 74 68 65 20 73 75 70  lob() on the sup
1151a 70 6c 69 65 64 20 76 61 6c 75 65 20 28 74 79 70  plied value (typ
1151b 65 20 4d 65 6d 2a 29 0a 2a 2a 20 50 20 69 66 20  e Mem*).** P if 
1151c 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 23 64 65  required..*/.#de
1151d 66 69 6e 65 20 45 78 70 61 6e 64 42 6c 6f 62 28  fine ExpandBlob(
1151e 50 29 20 28 28 28 50 29 2d 3e 66 6c 61 67 73 26  P) (((P)->flags&
1151f 4d 45 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69 74 65  MEM_Zero)?sqlite
11520 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c  3VdbeMemExpandBl
11521 6f 62 28 50 29 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20  ob(P):0)../*.** 
11522 41 72 67 75 6d 65 6e 74 20 70 4d 65 6d 20 70 6f  Argument pMem po
11523 69 6e 74 73 20 61 74 20 61 20 6d 65 6d 6f 72 79  ints at a memory
11524 20 63 65 6c 6c 20 74 68 61 74 20 77 69 6c 6c 20   cell that will 
11525 62 65 20 70 61 73 73 65 64 20 74 6f 20 61 0a 2a  be passed to a.*
11526 2a 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20 66  * user-defined f
11527 75 6e 63 74 69 6f 6e 20 6f 72 20 72 65 74 75 72  unction or retur
11528 6e 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 20  ned to the user 
11529 61 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66  as the result of
1152a 20 61 20 71 75 65 72 79 2e 0a 2a 2a 20 54 68 65   a query..** The
1152b 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
1152c 2c 20 27 64 62 5f 65 6e 63 27 20 69 73 20 74 68  , 'db_enc' is th
1152d 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20  e text encoding 
1152e 75 73 65 64 20 62 79 20 74 68 65 20 76 64 62 65  used by the vdbe
1152f 20 66 6f 72 0a 2a 2a 20 73 74 61 63 6b 20 76 61   for.** stack va
11530 72 69 61 62 6c 65 73 2e 20 20 54 68 69 73 20 72  riables.  This r
11531 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 20  outine sets the 
11532 70 4d 65 6d 2d 3e 65 6e 63 20 61 6e 64 20 70 4d  pMem->enc and pM
11533 65 6d 2d 3e 74 79 70 65 0a 2a 2a 20 76 61 72 69  em->type.** vari
11534 61 62 6c 65 73 20 75 73 65 64 20 62 79 20 74 68  ables used by th
11535 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  e sqlite3_value_
11536 2a 28 29 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f  *() routines..*/
11537 0a 23 64 65 66 69 6e 65 20 73 74 6f 72 65 54 79  .#define storeTy
11538 70 65 49 6e 66 6f 28 41 2c 42 29 20 5f 73 74 6f  peInfo(A,B) _sto
11539 72 65 54 79 70 65 49 6e 66 6f 28 41 29 0a 73 74  reTypeInfo(A).st
1153a 61 74 69 63 20 76 6f 69 64 20 5f 73 74 6f 72 65  atic void _store
1153b 54 79 70 65 49 6e 66 6f 28 4d 65 6d 20 2a 70 4d  TypeInfo(Mem *pM
1153c 65 6d 29 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73  em){.  int flags
1153d 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a   = pMem->flags;.
1153e 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45    if( flags & ME
1153f 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 70 4d  M_Null ){.    pM
11540 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54  em->type = SQLIT
11541 45 5f 4e 55 4c 4c 3b 0a 20 20 7d 0a 20 20 65 6c  E_NULL;.  }.  el
11542 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d  se if( flags & M
11543 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 70 4d  EM_Int ){.    pM
11544 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54  em->type = SQLIT
11545 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 7d 0a 20  E_INTEGER;.  }. 
11546 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20   else if( flags 
11547 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20  & MEM_Real ){.  
11548 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53    pMem->type = S
11549 51 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 7d  QLITE_FLOAT;.  }
1154a 0a 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67  .  else if( flag
1154b 73 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20  s & MEM_Str ){. 
1154c 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20     pMem->type = 
1154d 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 7d  SQLITE_TEXT;.  }
1154e 65 6c 73 65 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e  else{.    pMem->
1154f 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 42 4c  type = SQLITE_BL
11550 4f 42 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  OB;.  }.}../*.**
11551 20 50 6f 70 20 74 68 65 20 73 74 61 63 6b 20 4e   Pop the stack N
11552 20 74 69 6d 65 73 2e 0a 2a 2f 0a 73 74 61 74 69   times..*/.stati
11553 63 20 76 6f 69 64 20 70 6f 70 53 74 61 63 6b 28  c void popStack(
11554 4d 65 6d 20 2a 2a 70 70 54 6f 73 2c 20 69 6e 74  Mem **ppTos, int
11555 20 4e 29 7b 0a 20 20 4d 65 6d 20 2a 70 54 6f 73   N){.  Mem *pTos
11556 20 3d 20 2a 70 70 54 6f 73 3b 0a 20 20 77 68 69   = *ppTos;.  whi
11557 6c 65 28 20 4e 3e 30 20 29 7b 0a 20 20 20 20 4e  le( N>0 ){.    N
11558 2d 2d 3b 0a 20 20 20 20 52 65 6c 65 61 73 65 28  --;.    Release(
11559 70 54 6f 73 29 3b 0a 20 20 20 20 70 54 6f 73 2d  pTos);.    pTos-
1155a 2d 3b 0a 20 20 7d 0a 20 20 2a 70 70 54 6f 73 20  -;.  }.  *ppTos 
1155b 3d 20 70 54 6f 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  = pTos;.}../*.**
1155c 20 41 6c 6c 6f 63 61 74 65 20 63 75 72 73 6f 72   Allocate cursor
1155d 20 6e 75 6d 62 65 72 20 69 43 75 72 2e 20 20 52   number iCur.  R
1155e 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
1155f 74 6f 20 69 74 2e 20 20 52 65 74 75 72 6e 20 4e  to it.  Return N
11560 55 4c 4c 0a 2a 2a 20 69 66 20 77 65 20 72 75 6e  ULL.** if we run
11561 20 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a   out of memory..
11562 2a 2f 0a 73 74 61 74 69 63 20 43 75 72 73 6f 72  */.static Cursor
11563 20 2a 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72   *allocateCursor
11564 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 69 43  (Vdbe *p, int iC
11565 75 72 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20  ur, int iDb){.  
11566 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20 20 61  Cursor *pCx;.  a
11567 73 73 65 72 74 28 20 69 43 75 72 3c 70 2d 3e 6e  ssert( iCur<p->n
11568 43 75 72 73 6f 72 20 29 3b 0a 20 20 69 66 28 20  Cursor );.  if( 
11569 70 2d 3e 61 70 43 73 72 5b 69 43 75 72 5d 20 29  p->apCsr[iCur] )
1156a 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
1156b 65 46 72 65 65 43 75 72 73 6f 72 28 70 2c 20 70  eFreeCursor(p, p
1156c 2d 3e 61 70 43 73 72 5b 69 43 75 72 5d 29 3b 0a  ->apCsr[iCur]);.
1156d 20 20 7d 0a 20 20 70 2d 3e 61 70 43 73 72 5b 69    }.  p->apCsr[i
1156e 43 75 72 5d 20 3d 20 70 43 78 20 3d 20 73 71 6c  Cur] = pCx = sql
1156f 69 74 65 4d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f  iteMalloc( sizeo
11570 66 28 43 75 72 73 6f 72 29 20 29 3b 0a 20 20 69  f(Cursor) );.  i
11571 66 28 20 70 43 78 20 29 7b 0a 20 20 20 20 70 43  f( pCx ){.    pC
11572 78 2d 3e 69 44 62 20 3d 20 69 44 62 3b 0a 20 20  x->iDb = iDb;.  
11573 7d 0a 20 20 72 65 74 75 72 6e 20 70 43 78 3b 0a  }.  return pCx;.
11574 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20  }../*.** Try to 
11575 63 6f 6e 76 65 72 74 20 61 20 76 61 6c 75 65 20  convert a value 
11576 69 6e 74 6f 20 61 20 6e 75 6d 65 72 69 63 20 72  into a numeric r
11577 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 66  epresentation if
11578 20 77 65 20 63 61 6e 0a 2a 2a 20 64 6f 20 73 6f   we can.** do so
11579 20 77 69 74 68 6f 75 74 20 6c 6f 73 73 20 6f 66   without loss of
1157a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 49   information.  I
1157b 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69  n other words, i
1157c 66 20 74 68 65 20 73 74 72 69 6e 67 0a 2a 2a 20  f the string.** 
1157d 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 6e 75 6d  looks like a num
1157e 62 65 72 2c 20 63 6f 6e 76 65 72 74 20 69 74 20  ber, convert it 
1157f 69 6e 74 6f 20 61 20 6e 75 6d 62 65 72 2e 20 20  into a number.  
11580 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 0a 2a  If it does not.*
11581 2a 20 6c 6f 6f 6b 20 6c 69 6b 65 20 61 20 6e 75  * look like a nu
11582 6d 62 65 72 2c 20 6c 65 61 76 65 20 69 74 20 61  mber, leave it a
11583 6c 6f 6e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  lone..*/.static 
11584 76 6f 69 64 20 61 70 70 6c 79 4e 75 6d 65 72 69  void applyNumeri
11585 63 41 66 66 69 6e 69 74 79 28 4d 65 6d 20 2a 70  cAffinity(Mem *p
11586 52 65 63 29 7b 0a 20 20 69 66 28 20 28 70 52 65  Rec){.  if( (pRe
11587 63 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f  c->flags & (MEM_
11588 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 29 3d 3d  Real|MEM_Int))==
11589 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 61  0 ){.    int rea
1158a 6c 6e 75 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65  lnum;.    sqlite
1158b 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69  3VdbeMemNulTermi
1158c 6e 61 74 65 28 70 52 65 63 29 3b 0a 20 20 20 20  nate(pRec);.    
1158d 69 66 28 20 28 70 52 65 63 2d 3e 66 6c 61 67 73  if( (pRec->flags
1158e 26 4d 45 4d 5f 53 74 72 29 0a 20 20 20 20 20 20  &MEM_Str).      
1158f 20 20 20 26 26 20 73 71 6c 69 74 65 33 49 73 4e     && sqlite3IsN
11590 75 6d 62 65 72 28 70 52 65 63 2d 3e 7a 2c 20 26  umber(pRec->z, &
11591 72 65 61 6c 6e 75 6d 2c 20 70 52 65 63 2d 3e 65  realnum, pRec->e
11592 6e 63 29 20 29 7b 0a 20 20 20 20 20 20 69 36 34  nc) ){.      i64
11593 20 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 73 71   value;.      sq
11594 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45  lite3VdbeChangeE
11595 6e 63 6f 64 69 6e 67 28 70 52 65 63 2c 20 53 51  ncoding(pRec, SQ
11596 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 20 20  LITE_UTF8);.    
11597 20 20 69 66 28 20 21 72 65 61 6c 6e 75 6d 20 26    if( !realnum &
11598 26 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28  & sqlite3Atoi64(
11599 70 52 65 63 2d 3e 7a 2c 20 26 76 61 6c 75 65 29  pRec->z, &value)
1159a 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
1159b 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
1159c 65 28 70 52 65 63 29 3b 0a 20 20 20 20 20 20 20  e(pRec);.       
1159d 20 70 52 65 63 2d 3e 75 2e 69 20 3d 20 76 61 6c   pRec->u.i = val
1159e 75 65 3b 0a 20 20 20 20 20 20 20 20 70 52 65 63  ue;.        pRec
1159f 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e  ->flags = MEM_In
115a0 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  t;.      }else{.
115a1 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
115a2 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 70 52  dbeMemRealify(pR
115a3 65 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ec);.      }.   
115a4 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
115a5 50 72 6f 63 65 73 73 69 6e 67 20 69 73 20 64 65  Processing is de
115a6 74 65 72 6d 69 6e 65 20 62 79 20 74 68 65 20 61  termine by the a
115a7 66 66 69 6e 69 74 79 20 70 61 72 61 6d 65 74 65  ffinity paramete
115a8 72 3a 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f  r:.**.** SQLITE_
115a9 41 46 46 5f 49 4e 54 45 47 45 52 3a 0a 2a 2a 20  AFF_INTEGER:.** 
115aa 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 3a  SQLITE_AFF_REAL:
115ab 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  .** SQLITE_AFF_N
115ac 55 4d 45 52 49 43 3a 0a 2a 2a 20 20 20 20 54 72  UMERIC:.**    Tr
115ad 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 70 52 65  y to convert pRe
115ae 63 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20  c to an integer 
115af 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f  representation o
115b0 72 20 61 20 0a 2a 2a 20 20 20 20 66 6c 6f 61 74  r a .**    float
115b1 69 6e 67 2d 70 6f 69 6e 74 20 72 65 70 72 65 73  ing-point repres
115b2 65 6e 74 61 74 69 6f 6e 20 69 66 20 61 6e 20 69  entation if an i
115b3 6e 74 65 67 65 72 20 72 65 70 72 65 73 65 6e 74  nteger represent
115b4 61 74 69 6f 6e 0a 2a 2a 20 20 20 20 69 73 20 6e  ation.**    is n
115b5 6f 74 20 70 6f 73 73 69 62 6c 65 2e 20 20 4e 6f  ot possible.  No
115b6 74 65 20 74 68 61 74 20 74 68 65 20 69 6e 74 65  te that the inte
115b7 67 65 72 20 72 65 70 72 65 73 65 6e 74 61 74 69  ger representati
115b8 6f 6e 20 69 73 0a 2a 2a 20 20 20 20 61 6c 77 61  on is.**    alwa
115b9 79 73 20 70 72 65 66 65 72 72 65 64 2c 20 65 76  ys preferred, ev
115ba 65 6e 20 69 66 20 74 68 65 20 61 66 66 69 6e 69  en if the affini
115bb 74 79 20 69 73 20 52 45 41 4c 2c 20 62 65 63 61  ty is REAL, beca
115bc 75 73 65 0a 2a 2a 20 20 20 20 61 6e 20 69 6e 74  use.**    an int
115bd 65 67 65 72 20 72 65 70 72 65 73 65 6e 74 61 74  eger representat
115be 69 6f 6e 20 69 73 20 6d 6f 72 65 20 73 70 61 63  ion is more spac
115bf 65 20 65 66 66 69 63 69 65 6e 74 20 6f 6e 20 64  e efficient on d
115c0 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54  isk..**.** SQLIT
115c1 45 5f 41 46 46 5f 54 45 58 54 3a 0a 2a 2a 20 20  E_AFF_TEXT:.**  
115c2 20 20 43 6f 6e 76 65 72 74 20 70 52 65 63 20 74    Convert pRec t
115c3 6f 20 61 20 74 65 78 74 20 72 65 70 72 65 73 65  o a text represe
115c4 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53  ntation..**.** S
115c5 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3a 0a  QLITE_AFF_NONE:.
115c6 2a 2a 20 20 20 20 4e 6f 2d 6f 70 2e 20 20 70 52  **    No-op.  pR
115c7 65 63 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e  ec is unchanged.
115c8 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
115c9 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 4d 65  applyAffinity(Me
115ca 6d 20 2a 70 52 65 63 2c 20 63 68 61 72 20 61 66  m *pRec, char af
115cb 66 69 6e 69 74 79 2c 20 75 38 20 65 6e 63 29 7b  finity, u8 enc){
115cc 0a 20 20 69 66 28 20 61 66 66 69 6e 69 74 79 3d  .  if( affinity=
115cd 3d 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54  =SQLITE_AFF_TEXT
115ce 20 29 7b 0a 20 20 20 20 2f 2a 20 4f 6e 6c 79 20   ){.    /* Only 
115cf 61 74 74 65 6d 70 74 20 74 68 65 20 63 6f 6e 76  attempt the conv
115d0 65 72 73 69 6f 6e 20 74 6f 20 54 45 58 54 20 69  ersion to TEXT i
115d1 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 69 6e  f there is an in
115d2 74 65 67 65 72 20 6f 72 20 72 65 61 6c 0a 20 20  teger or real.  
115d3 20 20 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74    ** representat
115d4 69 6f 6e 20 28 62 6c 6f 62 20 61 6e 64 20 4e 55  ion (blob and NU
115d5 4c 4c 20 64 6f 20 6e 6f 74 20 67 65 74 20 63 6f  LL do not get co
115d6 6e 76 65 72 74 65 64 29 20 62 75 74 20 6e 6f 20  nverted) but no 
115d7 73 74 72 69 6e 67 0a 20 20 20 20 2a 2a 20 72 65  string.    ** re
115d8 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 20 20  presentation..  
115d9 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 30 3d 3d    */.    if( 0==
115da 28 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d 45 4d  (pRec->flags&MEM
115db 5f 53 74 72 29 20 26 26 20 28 70 52 65 63 2d 3e  _Str) && (pRec->
115dc 66 6c 61 67 73 26 28 4d 45 4d 5f 52 65 61 6c 7c  flags&(MEM_Real|
115dd 4d 45 4d 5f 49 6e 74 29 29 20 29 7b 0a 20 20 20  MEM_Int)) ){.   
115de 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
115df 6d 53 74 72 69 6e 67 69 66 79 28 70 52 65 63 2c  mStringify(pRec,
115e0 20 65 6e 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20   enc);.    }.   
115e1 20 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 3d 20   pRec->flags &= 
115e2 7e 28 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49  ~(MEM_Real|MEM_I
115e3 6e 74 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  nt);.  }else if(
115e4 20 61 66 66 69 6e 69 74 79 21 3d 53 51 4c 49 54   affinity!=SQLIT
115e5 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20  E_AFF_NONE ){.  
115e6 20 20 61 73 73 65 72 74 28 20 61 66 66 69 6e 69    assert( affini
115e7 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 49  ty==SQLITE_AFF_I
115e8 4e 54 45 47 45 52 20 7c 7c 20 61 66 66 69 6e 69  NTEGER || affini
115e9 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52  ty==SQLITE_AFF_R
115ea 45 41 4c 0a 20 20 20 20 20 20 20 20 20 20 20 20  EAL.            
115eb 20 7c 7c 20 61 66 66 69 6e 69 74 79 3d 3d 53 51   || affinity==SQ
115ec 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43  LITE_AFF_NUMERIC
115ed 20 29 3b 0a 20 20 20 20 61 70 70 6c 79 4e 75 6d   );.    applyNum
115ee 65 72 69 63 41 66 66 69 6e 69 74 79 28 70 52 65  ericAffinity(pRe
115ef 63 29 3b 0a 20 20 20 20 69 66 28 20 70 52 65 63  c);.    if( pRec
115f0 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65  ->flags & MEM_Re
115f1 61 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  al ){.      sqli
115f2 74 65 33 56 64 62 65 49 6e 74 65 67 65 72 41 66  te3VdbeIntegerAf
115f3 66 69 6e 69 74 79 28 70 52 65 63 29 3b 0a 20 20  finity(pRec);.  
115f4 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
115f5 20 54 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20   Try to convert 
115f6 74 68 65 20 74 79 70 65 20 6f 66 20 61 20 66 75  the type of a fu
115f7 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20  nction argument 
115f8 6f 72 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75  or a result colu
115f9 6d 6e 0a 2a 2a 20 69 6e 74 6f 20 61 20 6e 75 6d  mn.** into a num
115fa 65 72 69 63 20 72 65 70 72 65 73 65 6e 74 61 74  eric representat
115fb 69 6f 6e 2e 20 20 55 73 65 20 65 69 74 68 65 72  ion.  Use either
115fc 20 49 4e 54 45 47 45 52 20 6f 72 20 52 45 41 4c   INTEGER or REAL
115fd 20 77 68 69 63 68 65 76 65 72 0a 2a 2a 20 69 73   whichever.** is
115fe 20 61 70 70 72 6f 70 72 69 61 74 65 2e 20 20 42   appropriate.  B
115ff 75 74 20 6f 6e 6c 79 20 64 6f 20 74 68 65 20 63  ut only do the c
11600 6f 6e 76 65 72 73 69 6f 6e 20 69 66 20 69 74 20  onversion if it 
11601 69 73 20 70 6f 73 73 69 62 6c 65 20 77 69 74 68  is possible with
11602 6f 75 74 0a 2a 2a 20 6c 6f 73 73 20 6f 66 20 69  out.** loss of i
11603 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 6e 64 20 72  nformation and r
11604 65 74 75 72 6e 20 74 68 65 20 72 65 76 69 73 65  eturn the revise
11605 64 20 74 79 70 65 20 6f 66 20 74 68 65 20 61 72  d type of the ar
11606 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  gument..**.** Th
11607 69 73 20 69 73 20 61 6e 20 45 58 50 45 52 49 4d  is is an EXPERIM
11608 45 4e 54 41 4c 20 61 70 69 20 61 6e 64 20 69 73  ENTAL api and is
11609 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e   subject to chan
1160a 67 65 20 6f 72 20 72 65 6d 6f 76 61 6c 2e 0a 2a  ge or removal..*
1160b 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61  /.int sqlite3_va
1160c 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65  lue_numeric_type
1160d 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a  (sqlite3_value *
1160e 70 56 61 6c 29 7b 0a 20 20 4d 65 6d 20 2a 70 4d  pVal){.  Mem *pM
1160f 65 6d 20 3d 20 28 4d 65 6d 2a 29 70 56 61 6c 3b  em = (Mem*)pVal;
11610 0a 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41  .  applyNumericA
11611 66 66 69 6e 69 74 79 28 70 4d 65 6d 29 3b 0a 20  ffinity(pMem);. 
11612 20 73 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 70   storeTypeInfo(p
11613 4d 65 6d 2c 20 30 29 3b 0a 20 20 72 65 74 75 72  Mem, 0);.  retur
11614 6e 20 70 4d 65 6d 2d 3e 74 79 70 65 3b 0a 7d 0a  n pMem->type;.}.
11615 0a 2f 2a 0a 2a 2a 20 45 78 70 6f 72 74 65 64 20  ./*.** Exported 
11616 76 65 72 73 69 6f 6e 20 6f 66 20 61 70 70 6c 79  version of apply
11617 41 66 66 69 6e 69 74 79 28 29 2e 20 54 68 69 73  Affinity(). This
11618 20 6f 6e 65 20 77 6f 72 6b 73 20 6f 6e 20 73 71   one works on sq
11619 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c 20 0a 2a  lite3_value*, .*
1161a 2a 20 6e 6f 74 20 74 68 65 20 69 6e 74 65 72 6e  * not the intern
1161b 61 6c 20 4d 65 6d 2a 20 74 79 70 65 2e 0a 2a 2f  al Mem* type..*/
1161c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1161d 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75  void sqlite3Valu
1161e 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 73  eApplyAffinity(s
1161f 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56  qlite3_value *pV
11620 61 6c 2c 20 75 38 20 61 66 66 69 6e 69 74 79 2c  al, u8 affinity,
11621 20 75 38 20 65 6e 63 29 7b 0a 20 20 61 70 70 6c   u8 enc){.  appl
11622 79 41 66 66 69 6e 69 74 79 28 28 4d 65 6d 20 2a  yAffinity((Mem *
11623 29 70 56 61 6c 2c 20 61 66 66 69 6e 69 74 79 2c  )pVal, affinity,
11624 20 65 6e 63 29 3b 0a 7d 0a 0a 23 69 66 64 65 66   enc);.}..#ifdef
11625 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a   SQLITE_DEBUG./*
11626 0a 2a 2a 20 57 72 69 74 65 20 61 20 6e 69 63 65  .** Write a nice
11627 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e   string represen
11628 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 6f  tation of the co
11629 6e 74 65 6e 74 73 20 6f 66 20 63 65 6c 6c 20 70  ntents of cell p
1162a 4d 65 6d 0a 2a 2a 20 69 6e 74 6f 20 62 75 66 66  Mem.** into buff
1162b 65 72 20 7a 42 75 66 2c 20 6c 65 6e 67 74 68 20  er zBuf, length 
1162c 6e 42 75 66 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  nBuf..*/.SQLITE_
1162d 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1162e 69 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74  ite3VdbeMemPrett
1162f 79 50 72 69 6e 74 28 4d 65 6d 20 2a 70 4d 65 6d  yPrint(Mem *pMem
11630 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20  , char *zBuf){. 
11631 20 63 68 61 72 20 2a 7a 43 73 72 20 3d 20 7a 42   char *zCsr = zB
11632 75 66 3b 0a 20 20 69 6e 74 20 66 20 3d 20 70 4d  uf;.  int f = pM
11633 65 6d 2d 3e 66 6c 61 67 73 3b 0a 0a 20 20 73 74  em->flags;..  st
11634 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
11635 2a 63 6f 6e 73 74 20 65 6e 63 6e 61 6d 65 73 5b  *const encnames[
11636 5d 20 3d 20 7b 22 28 58 29 22 2c 20 22 28 38 29  ] = {"(X)", "(8)
11637 22 2c 20 22 28 31 36 4c 45 29 22 2c 20 22 28 31  ", "(16LE)", "(1
11638 36 42 45 29 22 7d 3b 0a 0a 20 20 69 66 28 20 66  6BE)"};..  if( f
11639 26 4d 45 4d 5f 42 6c 6f 62 20 29 7b 0a 20 20 20  &MEM_Blob ){.   
1163a 20 69 6e 74 20 69 3b 0a 20 20 20 20 63 68 61 72   int i;.    char
1163b 20 63 3b 0a 20 20 20 20 69 66 28 20 66 20 26 20   c;.    if( f & 
1163c 4d 45 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 20 20  MEM_Dyn ){.     
1163d 20 63 20 3d 20 27 7a 27 3b 0a 20 20 20 20 20 20   c = 'z';.      
1163e 61 73 73 65 72 74 28 20 28 66 20 26 20 28 4d 45  assert( (f & (ME
1163f 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 45 70 68  M_Static|MEM_Eph
11640 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d  em))==0 );.    }
11641 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d  else if( f & MEM
11642 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20 20  _Static ){.     
11643 20 63 20 3d 20 27 74 27 3b 0a 20 20 20 20 20 20   c = 't';.      
11644 61 73 73 65 72 74 28 20 28 66 20 26 20 28 4d 45  assert( (f & (ME
11645 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29  M_Dyn|MEM_Ephem)
11646 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73  )==0 );.    }els
11647 65 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 45 70  e if( f & MEM_Ep
11648 68 65 6d 20 29 7b 0a 20 20 20 20 20 20 63 20 3d  hem ){.      c =
11649 20 27 65 27 3b 0a 20 20 20 20 20 20 61 73 73 65   'e';.      asse
1164a 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 74  rt( (f & (MEM_St
1164b 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 29 29 3d 3d  atic|MEM_Dyn))==
1164c 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  0 );.    }else{.
1164d 20 20 20 20 20 20 63 20 3d 20 27 73 27 3b 0a 20        c = 's';. 
1164e 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65     }..    sqlite
1164f 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20  3_snprintf(100, 
11650 7a 43 73 72 2c 20 22 25 63 22 2c 20 63 29 3b 0a  zCsr, "%c", c);.
11651 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 74 72 6c      zCsr += strl
11652 65 6e 28 7a 43 73 72 29 3b 0a 20 20 20 20 73 71  en(zCsr);.    sq
11653 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31  lite3_snprintf(1
11654 30 30 2c 20 7a 43 73 72 2c 20 22 25 64 5b 22 2c  00, zCsr, "%d[",
11655 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20 7a   pMem->n);.    z
11656 43 73 72 20 2b 3d 20 73 74 72 6c 65 6e 28 7a 43  Csr += strlen(zC
11657 73 72 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  sr);.    for(i=0
11658 3b 20 69 3c 31 36 20 26 26 20 69 3c 70 4d 65 6d  ; i<16 && i<pMem
11659 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  ->n; i++){.     
1165a 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
1165b 66 28 31 30 30 2c 20 7a 43 73 72 2c 20 22 25 30  f(100, zCsr, "%0
1165c 32 58 22 2c 20 28 28 69 6e 74 29 70 4d 65 6d 2d  2X", ((int)pMem-
1165d 3e 7a 5b 69 5d 20 26 20 30 78 46 46 29 29 3b 0a  >z[i] & 0xFF));.
1165e 20 20 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 74        zCsr += st
1165f 72 6c 65 6e 28 7a 43 73 72 29 3b 0a 20 20 20 20  rlen(zCsr);.    
11660 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  }.    for(i=0; i
11661 3c 31 36 20 26 26 20 69 3c 70 4d 65 6d 2d 3e 6e  <16 && i<pMem->n
11662 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 68  ; i++){.      ch
11663 61 72 20 7a 20 3d 20 70 4d 65 6d 2d 3e 7a 5b 69  ar z = pMem->z[i
11664 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 3c 33  ];.      if( z<3
11665 32 20 7c 7c 20 7a 3e 31 32 36 20 29 20 2a 7a 43  2 || z>126 ) *zC
11666 73 72 2b 2b 20 3d 20 27 2e 27 3b 0a 20 20 20 20  sr++ = '.';.    
11667 20 20 65 6c 73 65 20 2a 7a 43 73 72 2b 2b 20 3d    else *zCsr++ =
11668 20 7a 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73   z;.    }..    s
11669 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
1166a 31 30 30 2c 20 7a 43 73 72 2c 20 22 5d 25 73 22  100, zCsr, "]%s"
1166b 2c 20 65 6e 63 6e 61 6d 65 73 5b 70 4d 65 6d 2d  , encnames[pMem-
1166c 3e 65 6e 63 5d 29 3b 0a 20 20 20 20 7a 43 73 72  >enc]);.    zCsr
1166d 20 2b 3d 20 73 74 72 6c 65 6e 28 7a 43 73 72 29   += strlen(zCsr)
1166e 3b 0a 20 20 20 20 69 66 28 20 66 20 26 20 4d 45  ;.    if( f & ME
1166f 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20  M_Zero ){.      
11670 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
11671 28 31 30 30 2c 20 7a 43 73 72 2c 22 2b 25 6c 6c  (100, zCsr,"+%ll
11672 64 7a 22 2c 70 4d 65 6d 2d 3e 75 2e 69 29 3b 0a  dz",pMem->u.i);.
11673 20 20 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 74        zCsr += st
11674 72 6c 65 6e 28 7a 43 73 72 29 3b 0a 20 20 20 20  rlen(zCsr);.    
11675 7d 0a 20 20 20 20 2a 7a 43 73 72 20 3d 20 27 5c  }.    *zCsr = '\
11676 30 27 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  0';.  }else if( 
11677 66 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20  f & MEM_Str ){. 
11678 20 20 20 69 6e 74 20 6a 2c 20 6b 3b 0a 20 20 20     int j, k;.   
11679 20 7a 42 75 66 5b 30 5d 20 3d 20 27 20 27 3b 0a   zBuf[0] = ' ';.
1167a 20 20 20 20 69 66 28 20 66 20 26 20 4d 45 4d 5f      if( f & MEM_
1167b 44 79 6e 20 29 7b 0a 20 20 20 20 20 20 7a 42 75  Dyn ){.      zBu
1167c 66 5b 31 5d 20 3d 20 27 7a 27 3b 0a 20 20 20 20  f[1] = 'z';.    
1167d 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20 28    assert( (f & (
1167e 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 45  MEM_Static|MEM_E
1167f 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20  phem))==0 );.   
11680 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d   }else if( f & M
11681 45 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 20  EM_Static ){.   
11682 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 74 27     zBuf[1] = 't'
11683 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
11684 28 66 20 26 20 28 4d 45 4d 5f 44 79 6e 7c 4d 45  (f & (MEM_Dyn|ME
11685 4d 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a  M_Ephem))==0 );.
11686 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 20      }else if( f 
11687 26 20 4d 45 4d 5f 45 70 68 65 6d 20 29 7b 0a 20  & MEM_Ephem ){. 
11688 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27       zBuf[1] = '
11689 65 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  e';.      assert
1168a 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 74 61 74  ( (f & (MEM_Stat
1168b 69 63 7c 4d 45 4d 5f 44 79 6e 29 29 3d 3d 30 20  ic|MEM_Dyn))==0 
1168c 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
1168d 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 73      zBuf[1] = 's
1168e 27 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6b 20 3d  ';.    }.    k =
1168f 20 32 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   2;.    sqlite3_
11690 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 26 7a  snprintf(100, &z
11691 42 75 66 5b 6b 5d 2c 20 22 25 64 22 2c 20 70 4d  Buf[k], "%d", pM
11692 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20 6b 20 2b 3d  em->n);.    k +=
11693 20 73 74 72 6c 65 6e 28 26 7a 42 75 66 5b 6b 5d   strlen(&zBuf[k]
11694 29 3b 0a 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d  );.    zBuf[k++]
11695 20 3d 20 27 5b 27 3b 0a 20 20 20 20 66 6f 72 28   = '[';.    for(
11696 6a 3d 30 3b 20 6a 3c 31 35 20 26 26 20 6a 3c 70  j=0; j<15 && j<p
11697 4d 65 6d 2d 3e 6e 3b 20 6a 2b 2b 29 7b 0a 20 20  Mem->n; j++){.  
11698 20 20 20 20 75 38 20 63 20 3d 20 70 4d 65 6d 2d      u8 c = pMem-
11699 3e 7a 5b 6a 5d 3b 0a 20 20 20 20 20 20 69 66 28  >z[j];.      if(
1169a 20 63 3e 3d 30 78 32 30 20 26 26 20 63 3c 30 78   c>=0x20 && c<0x
1169b 37 66 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 42  7f ){.        zB
1169c 75 66 5b 6b 2b 2b 5d 20 3d 20 63 3b 0a 20 20 20  uf[k++] = c;.   
1169d 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1169e 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 27 2e    zBuf[k++] = '.
1169f 27 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  ';.      }.    }
116a0 0a 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d  .    zBuf[k++] =
116a1 20 27 5d 27 3b 0a 20 20 20 20 73 71 6c 69 74 65   ']';.    sqlite
116a2 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 26  3_snprintf(100,&
116a3 7a 42 75 66 5b 6b 5d 2c 20 65 6e 63 6e 61 6d 65  zBuf[k], encname
116a4 73 5b 70 4d 65 6d 2d 3e 65 6e 63 5d 29 3b 0a 20  s[pMem->enc]);. 
116a5 20 20 20 6b 20 2b 3d 20 73 74 72 6c 65 6e 28 26     k += strlen(&
116a6 7a 42 75 66 5b 6b 5d 29 3b 0a 20 20 20 20 7a 42  zBuf[k]);.    zB
116a7 75 66 5b 6b 2b 2b 5d 20 3d 20 30 3b 0a 20 20 7d  uf[k++] = 0;.  }
116a8 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 64  .}.#endif...#ifd
116a9 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a  ef VDBE_PROFILE.
116aa 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
116ab 69 6e 67 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79  ing routine only
116ac 20 77 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 69 75   works on pentiu
116ad 6d 2d 63 6c 61 73 73 20 70 72 6f 63 65 73 73 6f  m-class processo
116ae 72 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20 74  rs..** It uses t
116af 68 65 20 52 44 54 53 43 20 6f 70 63 6f 64 65 20  he RDTSC opcode 
116b0 74 6f 20 72 65 61 64 20 74 68 65 20 63 79 63 6c  to read the cycl
116b1 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75  e count value ou
116b2 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63  t of the.** proc
116b3 65 73 73 6f 72 20 61 6e 64 20 72 65 74 75 72 6e  essor and return
116b4 73 20 74 68 61 74 20 76 61 6c 75 65 2e 20 20 54  s that value.  T
116b5 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20  his can be used 
116b6 66 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20  for high-res.** 
116b7 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 5f 5f  profiling..*/.__
116b8 69 6e 6c 69 6e 65 5f 5f 20 75 6e 73 69 67 6e 65  inline__ unsigne
116b9 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 20  d long long int 
116ba 68 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20  hwtime(void){.  
116bb 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f  unsigned long lo
116bc 6e 67 20 69 6e 74 20 78 3b 0a 20 20 5f 5f 61 73  ng int x;.  __as
116bd 6d 5f 5f 28 22 72 64 74 73 63 5c 6e 5c 74 22 0a  m__("rdtsc\n\t".
116be 20 20 20 20 20 20 20 20 20 20 22 6d 6f 76 20 25            "mov %
116bf 25 65 64 78 2c 20 25 25 65 63 78 5c 6e 5c 74 22  %edx, %%ecx\n\t"
116c0 0a 20 20 20 20 20 20 20 20 20 20 3a 22 3d 41 22  .          :"=A"
116c1 20 28 78 29 29 3b 0a 20 20 72 65 74 75 72 6e 20   (x));.  return 
116c2 78 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  x;.}.#endif../*.
116c3 2a 2a 20 54 68 65 20 43 48 45 43 4b 5f 46 4f 52  ** The CHECK_FOR
116c4 5f 49 4e 54 45 52 52 55 50 54 20 6d 61 63 72 6f  _INTERRUPT macro
116c5 20 64 65 66 69 6e 65 64 20 68 65 72 65 20 6c 6f   defined here lo
116c6 6f 6b 73 20 74 6f 20 73 65 65 20 69 66 20 74 68  oks to see if th
116c7 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6e 74  e.** sqlite3_int
116c8 65 72 72 75 70 74 28 29 20 72 6f 75 74 69 6e 65  errupt() routine
116c9 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64   has been called
116ca 2e 20 20 49 66 20 69 74 20 68 61 73 20 62 65 65  .  If it has bee
116cb 6e 2c 20 74 68 65 6e 0a 2a 2a 20 70 72 6f 63 65  n, then.** proce
116cc 73 73 69 6e 67 20 6f 66 20 74 68 65 20 56 44 42  ssing of the VDB
116cd 45 20 70 72 6f 67 72 61 6d 20 69 73 20 69 6e 74  E program is int
116ce 65 72 72 75 70 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  errupted..**.** 
116cf 54 68 69 73 20 6d 61 63 72 6f 20 61 64 64 65 64  This macro added
116d0 20 74 6f 20 65 76 65 72 79 20 69 6e 73 74 72 75   to every instru
116d1 63 74 69 6f 6e 20 74 68 61 74 20 64 6f 65 73 20  ction that does 
116d2 61 20 6a 75 6d 70 20 69 6e 20 6f 72 64 65 72 20  a jump in order 
116d3 74 6f 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20  to.** implement 
116d4 61 20 6c 6f 6f 70 2e 20 20 54 68 69 73 20 74 65  a loop.  This te
116d5 73 74 20 75 73 65 64 20 74 6f 20 62 65 20 6f 6e  st used to be on
116d6 20 65 76 65 72 79 20 73 69 6e 67 6c 65 20 69 6e   every single in
116d7 73 74 72 75 63 74 69 6f 6e 2c 0a 2a 2a 20 62 75  struction,.** bu
116d8 74 20 74 68 61 74 20 6d 65 61 6e 74 20 77 65 20  t that meant we 
116d9 6d 6f 72 65 20 74 65 73 74 69 6e 67 20 74 68 61  more testing tha
116da 74 20 77 65 20 6e 65 65 64 65 64 2e 20 20 42 79  t we needed.  By
116db 20 6f 6e 6c 79 20 74 65 73 74 69 6e 67 20 74 68   only testing th
116dc 65 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20 6a 75 6d  e.** flag on jum
116dd 70 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 2c 20  p instructions, 
116de 77 65 20 67 65 74 20 61 20 28 73 6d 61 6c 6c 29  we get a (small)
116df 20 73 70 65 65 64 20 69 6d 70 72 6f 76 65 6d 65   speed improveme
116e0 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43  nt..*/.#define C
116e1 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55  HECK_FOR_INTERRU
116e2 50 54 20 5c 0a 20 20 20 69 66 28 20 64 62 2d 3e  PT \.   if( db->
116e3 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64  u1.isInterrupted
116e4 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75   ) goto abort_du
116e5 65 5f 74 6f 5f 69 6e 74 65 72 72 75 70 74 3b 0a  e_to_interrupt;.
116e6 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20  ../*.** Execute 
116e7 61 73 20 6d 75 63 68 20 6f 66 20 61 20 56 44 42  as much of a VDB
116e8 45 20 70 72 6f 67 72 61 6d 20 61 73 20 77 65 20  E program as we 
116e9 63 61 6e 20 74 68 65 6e 20 72 65 74 75 72 6e 2e  can then return.
116ea 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64  .**.** sqlite3Vd
116eb 62 65 4d 61 6b 65 52 65 61 64 79 28 29 20 6d 75  beMakeReady() mu
116ec 73 74 20 62 65 20 63 61 6c 6c 65 64 20 62 65 66  st be called bef
116ed 6f 72 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ore this routine
116ee 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20   in order to.** 
116ef 63 6c 6f 73 65 20 74 68 65 20 70 72 6f 67 72 61  close the progra
116f0 6d 20 77 69 74 68 20 61 20 66 69 6e 61 6c 20 4f  m with a final O
116f1 50 5f 48 61 6c 74 20 61 6e 64 20 74 6f 20 73 65  P_Halt and to se
116f2 74 20 75 70 20 74 68 65 20 63 61 6c 6c 62 61 63  t up the callbac
116f3 6b 73 0a 2a 2a 20 61 6e 64 20 74 68 65 20 65 72  ks.** and the er
116f4 72 6f 72 20 6d 65 73 73 61 67 65 20 70 6f 69 6e  ror message poin
116f5 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 65  ter..**.** Whene
116f6 76 65 72 20 61 20 72 6f 77 20 6f 72 20 72 65 73  ver a row or res
116f7 75 6c 74 20 64 61 74 61 20 69 73 20 61 76 61 69  ult data is avai
116f8 6c 61 62 6c 65 2c 20 74 68 69 73 20 72 6f 75 74  lable, this rout
116f9 69 6e 65 20 77 69 6c 6c 20 65 69 74 68 65 72 0a  ine will either.
116fa 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 72 65  ** invoke the re
116fb 73 75 6c 74 20 63 61 6c 6c 62 61 63 6b 20 28 69  sult callback (i
116fc 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65 29 20  f there is one) 
116fd 6f 72 20 72 65 74 75 72 6e 20 77 69 74 68 0a 2a  or return with.*
116fe 2a 20 53 51 4c 49 54 45 5f 52 4f 57 2e 0a 2a 2a  * SQLITE_ROW..**
116ff 0a 2a 2a 20 49 66 20 61 6e 20 61 74 74 65 6d 70  .** If an attemp
11700 74 20 69 73 20 6d 61 64 65 20 74 6f 20 6f 70 65  t is made to ope
11701 6e 20 61 20 6c 6f 63 6b 65 64 20 64 61 74 61 62  n a locked datab
11702 61 73 65 2c 20 74 68 65 6e 20 74 68 69 73 20 72  ase, then this r
11703 6f 75 74 69 6e 65 0a 2a 2a 20 77 69 6c 6c 20 65  outine.** will e
11704 69 74 68 65 72 20 69 6e 76 6f 6b 65 20 74 68 65  ither invoke the
11705 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 28   busy callback (
11706 69 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65 29  if there is one)
11707 20 6f 72 20 69 74 20 77 69 6c 6c 0a 2a 2a 20 72   or it will.** r
11708 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53  eturn SQLITE_BUS
11709 59 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65  Y..**.** If an e
1170a 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 61 6e 20  rror occurs, an 
1170b 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73  error message is
1170c 20 77 72 69 74 74 65 6e 20 74 6f 20 6d 65 6d 6f   written to memo
1170d 72 79 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66  ry obtained.** f
1170e 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63  rom sqliteMalloc
1170f 28 29 20 61 6e 64 20 70 2d 3e 7a 45 72 72 4d 73  () and p->zErrMs
11710 67 20 69 73 20 6d 61 64 65 20 74 6f 20 70 6f 69  g is made to poi
11711 6e 74 20 74 6f 20 74 68 61 74 20 6d 65 6d 6f 72  nt to that memor
11712 79 2e 0a 2a 2a 20 54 68 65 20 65 72 72 6f 72 20  y..** The error 
11713 63 6f 64 65 20 69 73 20 73 74 6f 72 65 64 20 69  code is stored i
11714 6e 20 70 2d 3e 72 63 20 61 6e 64 20 74 68 69 73  n p->rc and this
11715 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
11716 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a   SQLITE_ERROR..*
11717 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 61 6c 6c  *.** If the call
11718 62 61 63 6b 20 65 76 65 72 20 72 65 74 75 72 6e  back ever return
11719 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e  s non-zero, then
1171a 20 74 68 65 20 70 72 6f 67 72 61 6d 20 65 78 69   the program exi
1171b 74 73 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c  ts.** immediatel
1171c 79 2e 20 20 54 68 65 72 65 20 77 69 6c 6c 20 62  y.  There will b
1171d 65 20 6e 6f 20 65 72 72 6f 72 20 6d 65 73 73 61  e no error messa
1171e 67 65 20 62 75 74 20 74 68 65 20 70 2d 3e 72 63  ge but the p->rc
1171f 20 66 69 65 6c 64 20 69 73 0a 2a 2a 20 73 65 74   field is.** set
11720 20 74 6f 20 53 51 4c 49 54 45 5f 41 42 4f 52 54   to SQLITE_ABORT
11721 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e   and this routin
11722 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 53 51  e will return SQ
11723 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a  LITE_ERROR..**.*
11724 2a 20 41 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  * A memory alloc
11725 61 74 69 6f 6e 20 65 72 72 6f 72 20 63 61 75 73  ation error caus
11726 65 73 20 70 2d 3e 72 63 20 74 6f 20 62 65 20 73  es p->rc to be s
11727 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d  et to SQLITE_NOM
11728 45 4d 20 61 6e 64 20 74 68 69 73 0a 2a 2a 20 72  EM and this.** r
11729 6f 75 74 69 6e 65 20 74 6f 20 72 65 74 75 72 6e  outine to return
1172a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a   SQLITE_ERROR..*
1172b 2a 0a 2a 2a 20 4f 74 68 65 72 20 66 61 74 61 6c  *.** Other fatal
1172c 20 65 72 72 6f 72 73 20 72 65 74 75 72 6e 20 53   errors return S
1172d 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a  QLITE_ERROR..**.
1172e 2a 2a 20 41 66 74 65 72 20 74 68 69 73 20 72 6f  ** After this ro
1172f 75 74 69 6e 65 20 68 61 73 20 66 69 6e 69 73 68  utine has finish
11730 65 64 2c 20 73 71 6c 69 74 65 33 56 64 62 65 46  ed, sqlite3VdbeF
11731 69 6e 61 6c 69 7a 65 28 29 20 73 68 6f 75 6c 64  inalize() should
11732 20 62 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 63   be.** used to c
11733 6c 65 61 6e 20 75 70 20 74 68 65 20 6d 65 73 73  lean up the mess
11734 20 74 68 61 74 20 77 61 73 20 6c 65 66 74 20 62   that was left b
11735 65 68 69 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ehind..*/.SQLITE
11736 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
11737 69 74 65 33 56 64 62 65 45 78 65 63 28 0a 20 20  ite3VdbeExec(.  
11738 56 64 62 65 20 2a 70 20 20 20 20 20 20 20 20 20  Vdbe *p         
11739 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1173a 65 20 56 44 42 45 20 2a 2f 0a 29 7b 0a 20 20 69  e VDBE */.){.  i
1173b 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20 20 20  nt pc;          
1173c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
1173d 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72   program counter
1173e 20 2a 2f 0a 20 20 4f 70 20 2a 70 4f 70 3b 20 20   */.  Op *pOp;  
1173f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11740 20 2f 2a 20 43 75 72 72 65 6e 74 20 6f 70 65 72   /* Current oper
11741 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72  ation */.  int r
11742 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20  c = SQLITE_OK;  
11743 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74        /* Value t
11744 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 73 71  o return */.  sq
11745 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64  lite3 *db = p->d
11746 62 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  b;       /* The 
11747 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 38  database */.  u8
11748 20 65 6e 63 6f 64 69 6e 67 20 3d 20 45 4e 43 28   encoding = ENC(
11749 64 62 29 3b 20 20 20 20 20 2f 2a 20 54 68 65 20  db);     /* The 
1174a 64 61 74 61 62 61 73 65 20 65 6e 63 6f 64 69 6e  database encodin
1174b 67 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 54 6f 73  g */.  Mem *pTos
1174c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1174d 20 20 2f 2a 20 54 6f 70 20 65 6e 74 72 79 20 69    /* Top entry i
1174e 6e 20 74 68 65 20 6f 70 65 72 61 6e 64 20 73 74  n the operand st
1174f 61 63 6b 20 2a 2f 0a 23 69 66 64 65 66 20 56 44  ack */.#ifdef VD
11750 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 75 6e 73  BE_PROFILE.  uns
11751 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20  igned long long 
11752 73 74 61 72 74 3b 20 20 2f 2a 20 43 50 55 20 63  start;  /* CPU c
11753 6c 6f 63 6b 20 63 6f 75 6e 74 20 61 74 20 73 74  lock count at st
11754 61 72 74 20 6f 66 20 6f 70 63 6f 64 65 20 2a 2f  art of opcode */
11755 0a 20 20 69 6e 74 20 6f 72 69 67 50 63 3b 20 20  .  int origPc;  
11756 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
11757 20 50 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72   Program counter
11758 20 61 74 20 73 74 61 72 74 20 6f 66 20 6f 70 63   at start of opc
11759 6f 64 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69  ode */.#endif.#i
1175a 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1175b 54 5f 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42  T_PROGRESS_CALLB
1175c 41 43 4b 0a 20 20 69 6e 74 20 6e 50 72 6f 67 72  ACK.  int nProgr
1175d 65 73 73 4f 70 73 20 3d 20 30 3b 20 20 20 20 20  essOps = 0;     
1175e 20 2f 2a 20 4f 70 63 6f 64 65 73 20 65 78 65 63   /* Opcodes exec
1175f 75 74 65 64 20 73 69 6e 63 65 20 70 72 6f 67 72  uted since progr
11760 65 73 73 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f  ess callback. */
11761 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20  .#endif.#ifndef 
11762 4e 44 45 42 55 47 0a 20 20 4d 65 6d 20 2a 70 53  NDEBUG.  Mem *pS
11763 74 61 63 6b 4c 69 6d 69 74 3b 0a 23 65 6e 64 69  tackLimit;.#endi
11764 66 0a 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69  f..  if( p->magi
11765 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55  c!=VDBE_MAGIC_RU
11766 4e 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  N ) return SQLIT
11767 45 5f 4d 49 53 55 53 45 3b 0a 20 20 61 73 73 65  E_MISUSE;.  asse
11768 72 74 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53  rt( db->magic==S
11769 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59  QLITE_MAGIC_BUSY
1176a 20 29 3b 0a 20 20 70 54 6f 73 20 3d 20 70 2d 3e   );.  pTos = p->
1176b 70 54 6f 73 3b 0a 20 20 69 66 28 20 70 2d 3e 72  pTos;.  if( p->r
1176c 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
1176d 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 68  ){.    /* This h
1176e 61 70 70 65 6e 73 20 69 66 20 61 20 6d 61 6c 6c  appens if a mall
1176f 6f 63 28 29 20 69 6e 73 69 64 65 20 61 20 63 61  oc() inside a ca
11770 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f  ll to sqlite3_co
11771 6c 75 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a 20  lumn_text() or. 
11772 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f     ** sqlite3_co
11773 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 61  lumn_text16() fa
11774 69 6c 65 64 2e 20 20 2a 2f 0a 20 20 20 20 67 6f  iled.  */.    go
11775 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20  to no_mem;.  }. 
11776 20 61 73 73 65 72 74 28 20 70 2d 3e 72 63 3d 3d   assert( p->rc==
11777 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e  SQLITE_OK || p->
11778 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20  rc==SQLITE_BUSY 
11779 29 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c  );.  p->rc = SQL
1177a 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74  ITE_OK;.  assert
1177b 28 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 30 20  ( p->explain==0 
1177c 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 6f 70 53  );.  if( p->popS
1177d 74 61 63 6b 20 29 7b 0a 20 20 20 20 70 6f 70 53  tack ){.    popS
1177e 74 61 63 6b 28 26 70 54 6f 73 2c 20 70 2d 3e 70  tack(&pTos, p->p
1177f 6f 70 53 74 61 63 6b 29 3b 0a 20 20 20 20 70 2d  opStack);.    p-
11780 3e 70 6f 70 53 74 61 63 6b 20 3d 20 30 3b 0a 20  >popStack = 0;. 
11781 20 7d 0a 20 20 70 2d 3e 72 65 73 4f 6e 53 74 61   }.  p->resOnSta
11782 63 6b 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 62 75  ck = 0;.  db->bu
11783 73 79 48 61 6e 64 6c 65 72 2e 6e 42 75 73 79 20  syHandler.nBusy 
11784 3d 20 30 3b 0a 20 20 43 48 45 43 4b 5f 46 4f 52  = 0;.  CHECK_FOR
11785 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 73 71  _INTERRUPT;.  sq
11786 6c 69 74 65 33 56 64 62 65 49 4f 54 72 61 63 65  lite3VdbeIOTrace
11787 53 71 6c 28 70 29 3b 0a 23 69 66 64 65 66 20 53  Sql(p);.#ifdef S
11788 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 69 66  QLITE_DEBUG.  if
11789 28 20 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 20  ( (p->db->flags 
1178a 26 20 53 51 4c 49 54 45 5f 56 64 62 65 4c 69 73  & SQLITE_VdbeLis
1178b 74 69 6e 67 29 21 3d 30 0a 20 20 20 20 7c 7c 20  ting)!=0.    || 
1178c 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 45 78 69  sqlite3OsFileExi
1178d 73 74 73 28 22 76 64 62 65 5f 65 78 70 6c 61 69  sts("vdbe_explai
1178e 6e 22 29 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74  n").  ){.    int
1178f 20 69 3b 0a 20 20 20 20 70 72 69 6e 74 66 28 22   i;.    printf("
11790 56 44 42 45 20 50 72 6f 67 72 61 6d 20 4c 69 73  VDBE Program Lis
11791 74 69 6e 67 3a 5c 6e 22 29 3b 0a 20 20 20 20 73  ting:\n");.    s
11792 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 53  qlite3VdbePrintS
11793 71 6c 28 70 29 3b 0a 20 20 20 20 66 6f 72 28 69  ql(p);.    for(i
11794 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b  =0; i<p->nOp; i+
11795 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  +){.      sqlite
11796 33 56 64 62 65 50 72 69 6e 74 4f 70 28 73 74 64  3VdbePrintOp(std
11797 6f 75 74 2c 20 69 2c 20 26 70 2d 3e 61 4f 70 5b  out, i, &p->aOp[
11798 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  i]);.    }.  }. 
11799 20 69 66 28 20 73 71 6c 69 74 65 33 4f 73 46 69   if( sqlite3OsFi
1179a 6c 65 45 78 69 73 74 73 28 22 76 64 62 65 5f 74  leExists("vdbe_t
1179b 72 61 63 65 22 29 20 29 7b 0a 20 20 20 20 70 2d  race") ){.    p-
1179c 3e 74 72 61 63 65 20 3d 20 73 74 64 6f 75 74 3b  >trace = stdout;
1179d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 66 6f  .  }.#endif.  fo
1179e 72 28 70 63 3d 70 2d 3e 70 63 3b 20 72 63 3d 3d  r(pc=p->pc; rc==
1179f 53 51 4c 49 54 45 5f 4f 4b 3b 20 70 63 2b 2b 29  SQLITE_OK; pc++)
117a0 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 63  {.    assert( pc
117a1 3e 3d 30 20 26 26 20 70 63 3c 70 2d 3e 6e 4f 70  >=0 && pc<p->nOp
117a2 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
117a3 70 54 6f 73 3c 3d 26 70 2d 3e 61 53 74 61 63 6b  pTos<=&p->aStack
117a4 5b 70 63 5d 20 29 3b 0a 20 20 20 20 69 66 28 20  [pc] );.    if( 
117a5 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69  sqlite3MallocFai
117a6 6c 65 64 28 29 20 29 20 67 6f 74 6f 20 6e 6f 5f  led() ) goto no_
117a7 6d 65 6d 3b 0a 23 69 66 64 65 66 20 56 44 42 45  mem;.#ifdef VDBE
117a8 5f 50 52 4f 46 49 4c 45 0a 20 20 20 20 6f 72 69  _PROFILE.    ori
117a9 67 50 63 20 3d 20 70 63 3b 0a 20 20 20 20 73 74  gPc = pc;.    st
117aa 61 72 74 20 3d 20 68 77 74 69 6d 65 28 29 3b 0a  art = hwtime();.
117ab 23 65 6e 64 69 66 0a 20 20 20 20 70 4f 70 20 3d  #endif.    pOp =
117ac 20 26 70 2d 3e 61 4f 70 5b 70 63 5d 3b 0a 0a 20   &p->aOp[pc];.. 
117ad 20 20 20 2f 2a 20 4f 6e 6c 79 20 61 6c 6c 6f 77     /* Only allow
117ae 20 74 72 61 63 69 6e 67 20 69 66 20 53 51 4c 49   tracing if SQLI
117af 54 45 5f 44 45 42 55 47 20 69 73 20 64 65 66 69  TE_DEBUG is defi
117b0 6e 65 64 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 64  ned..    */.#ifd
117b1 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
117b2 20 20 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65      if( p->trace
117b3 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 63   ){.      if( pc
117b4 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ==0 ){.        p
117b5 72 69 6e 74 66 28 22 56 44 42 45 20 45 78 65 63  rintf("VDBE Exec
117b6 75 74 69 6f 6e 20 54 72 61 63 65 3a 5c 6e 22 29  ution Trace:\n")
117b7 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
117b8 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28 70 29  3VdbePrintSql(p)
117b9 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
117ba 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74  sqlite3VdbePrint
117bb 4f 70 28 70 2d 3e 74 72 61 63 65 2c 20 70 63 2c  Op(p->trace, pc,
117bc 20 70 4f 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20   pOp);.    }.   
117bd 20 69 66 28 20 70 2d 3e 74 72 61 63 65 3d 3d 30   if( p->trace==0
117be 20 26 26 20 70 63 3d 3d 30 20 26 26 20 73 71 6c   && pc==0 && sql
117bf 69 74 65 33 4f 73 46 69 6c 65 45 78 69 73 74 73  ite3OsFileExists
117c0 28 22 76 64 62 65 5f 73 71 6c 74 72 61 63 65 22  ("vdbe_sqltrace"
117c1 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ) ){.      sqlit
117c2 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28 70  e3VdbePrintSql(p
117c3 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  );.    }.#endif.
117c4 20 20 20 20 20 20 0a 0a 20 20 20 20 2f 2a 20 43        ..    /* C
117c5 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 77  heck to see if w
117c6 65 20 6e 65 65 64 20 74 6f 20 73 69 6d 75 6c 61  e need to simula
117c7 74 65 20 61 6e 20 69 6e 74 65 72 72 75 70 74 2e  te an interrupt.
117c8 20 20 54 68 69 73 20 6f 6e 6c 79 20 68 61 70 70    This only happ
117c9 65 6e 73 0a 20 20 20 20 2a 2a 20 69 66 20 77 65  ens.    ** if we
117ca 20 68 61 76 65 20 61 20 73 70 65 63 69 61 6c 20   have a special 
117cb 74 65 73 74 20 62 75 69 6c 64 2e 0a 20 20 20 20  test build..    
117cc 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
117cd 5f 54 45 53 54 0a 20 20 20 20 69 66 28 20 73 71  _TEST.    if( sq
117ce 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 5f  lite3_interrupt_
117cf 63 6f 75 6e 74 3e 30 20 29 7b 0a 20 20 20 20 20  count>0 ){.     
117d0 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75   sqlite3_interru
117d1 70 74 5f 63 6f 75 6e 74 2d 2d 3b 0a 20 20 20 20  pt_count--;.    
117d2 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e    if( sqlite3_in
117d3 74 65 72 72 75 70 74 5f 63 6f 75 6e 74 3d 3d 30  terrupt_count==0
117d4 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
117d5 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 64 62  te3_interrupt(db
117d6 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
117d7 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66  .#endif..#ifndef
117d8 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f   SQLITE_OMIT_PRO
117d9 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 20  GRESS_CALLBACK. 
117da 20 20 20 2f 2a 20 43 61 6c 6c 20 74 68 65 20 70     /* Call the p
117db 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b  rogress callback
117dc 20 69 66 20 69 74 20 69 73 20 63 6f 6e 66 69 67   if it is config
117dd 75 72 65 64 20 61 6e 64 20 74 68 65 20 72 65 71  ured and the req
117de 75 69 72 65 64 20 6e 75 6d 62 65 72 0a 20 20 20  uired number.   
117df 20 2a 2a 20 6f 66 20 56 44 42 45 20 6f 70 73 20   ** of VDBE ops 
117e0 68 61 76 65 20 62 65 65 6e 20 65 78 65 63 75 74  have been execut
117e1 65 64 20 28 65 69 74 68 65 72 20 73 69 6e 63 65  ed (either since
117e2 20 74 68 69 73 20 69 6e 76 6f 63 61 74 69 6f 6e   this invocation
117e3 20 6f 66 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74   of.    ** sqlit
117e4 65 33 56 64 62 65 45 78 65 63 28 29 20 6f 72 20  e3VdbeExec() or 
117e5 73 69 6e 63 65 20 6c 61 73 74 20 74 69 6d 65 20  since last time 
117e6 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c  the progress cal
117e7 6c 62 61 63 6b 20 77 61 73 20 63 61 6c 6c 65 64  lback was called
117e8 29 2e 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65  )..    ** If the
117e9 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61   progress callba
117ea 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a  ck returns non-z
117eb 65 72 6f 2c 20 65 78 69 74 20 74 68 65 20 76 69  ero, exit the vi
117ec 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 77 69  rtual machine wi
117ed 74 68 0a 20 20 20 20 2a 2a 20 61 20 72 65 74 75  th.    ** a retu
117ee 72 6e 20 63 6f 64 65 20 53 51 4c 49 54 45 5f 41  rn code SQLITE_A
117ef 42 4f 52 54 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  BORT..    */.   
117f0 20 69 66 28 20 64 62 2d 3e 78 50 72 6f 67 72 65   if( db->xProgre
117f1 73 73 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ss ){.      if( 
117f2 64 62 2d 3e 6e 50 72 6f 67 72 65 73 73 4f 70 73  db->nProgressOps
117f3 3d 3d 6e 50 72 6f 67 72 65 73 73 4f 70 73 20 29  ==nProgressOps )
117f4 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 70 72  {.        int pr
117f5 63 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73  c;.        if( s
117f6 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
117f7 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74  db) ) goto abort
117f8 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a  _due_to_misuse;.
117f9 20 20 20 20 20 20 20 20 70 72 63 20 3d 64 62 2d          prc =db-
117fa 3e 78 50 72 6f 67 72 65 73 73 28 64 62 2d 3e 70  >xProgress(db->p
117fb 50 72 6f 67 72 65 73 73 41 72 67 29 3b 0a 20 20  ProgressArg);.  
117fc 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
117fd 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20  3SafetyOn(db) ) 
117fe 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
117ff 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 20 20  o_misuse;.      
11800 20 20 69 66 28 20 70 72 63 21 3d 30 20 29 7b 0a    if( prc!=0 ){.
11801 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
11802 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 3b  QLITE_INTERRUPT;
11803 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
11804 76 64 62 65 5f 68 61 6c 74 3b 0a 20 20 20 20 20  vdbe_halt;.     
11805 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 50 72     }.        nPr
11806 6f 67 72 65 73 73 4f 70 73 20 3d 20 30 3b 0a 20  ogressOps = 0;. 
11807 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e 50 72       }.      nPr
11808 6f 67 72 65 73 73 4f 70 73 2b 2b 3b 0a 20 20 20  ogressOps++;.   
11809 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64   }.#endif..#ifnd
1180a 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 2f 2a  ef NDEBUG.    /*
1180b 20 54 68 69 73 20 69 73 20 74 6f 20 63 68 65 63   This is to chec
1180c 6b 20 74 68 61 74 20 74 68 65 20 72 65 74 75 72  k that the retur
1180d 6e 20 76 61 6c 75 65 20 6f 66 20 73 74 61 74 69  n value of stati
1180e 63 20 66 75 6e 63 74 69 6f 6e 0a 20 20 20 20 2a  c function.    *
1180f 2a 20 6f 70 63 6f 64 65 4e 6f 50 75 73 68 28 29  * opcodeNoPush()
11810 20 28 73 65 65 20 76 64 62 65 61 75 78 2e 63 29   (see vdbeaux.c)
11811 20 72 65 74 75 72 6e 73 20 76 61 6c 75 65 73 20   returns values 
11812 74 68 61 74 20 6d 61 74 63 68 20 74 68 65 0a 20  that match the. 
11813 20 20 20 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61     ** implementa
11814 74 69 6f 6e 20 6f 66 20 74 68 65 20 76 69 72 74  tion of the virt
11815 75 61 6c 20 6d 61 63 68 69 6e 65 20 69 6e 20 74  ual machine in t
11816 68 69 73 20 66 69 6c 65 2e 20 49 66 0a 20 20 20  his file. If.   
11817 20 2a 2a 20 6f 70 63 6f 64 65 4e 6f 50 75 73 68   ** opcodeNoPush
11818 28 29 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a  () returns non-z
11819 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 73 74  ero, then the st
1181a 61 63 6b 20 69 73 20 67 75 61 72 65 6e 74 65 65  ack is guarentee
1181b 64 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 74 6f 20  d.    ** not to 
1181c 67 72 6f 77 20 77 68 65 6e 20 74 68 65 20 6f 70  grow when the op
1181d 63 6f 64 65 20 69 73 20 65 78 65 63 75 74 65 64  code is executed
1181e 2e 20 49 66 20 69 74 20 72 65 74 75 72 6e 73 20  . If it returns 
1181f 7a 65 72 6f 2c 20 74 68 65 6e 0a 20 20 20 20 2a  zero, then.    *
11820 2a 20 74 68 65 20 73 74 61 63 6b 20 6d 61 79 20  * the stack may 
11821 67 72 6f 77 20 62 79 20 61 74 20 6d 6f 73 74 20  grow by at most 
11822 31 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  1..    **.    **
11823 20 54 68 65 20 67 6c 6f 62 61 6c 20 77 72 61 70   The global wrap
11824 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 73 71 6c  per function sql
11825 69 74 65 33 56 64 62 65 4f 70 63 6f 64 65 55 73  ite3VdbeOpcodeUs
11826 65 73 53 74 61 63 6b 28 29 20 69 73 20 6e 6f 74  esStack() is not
11827 20 0a 20 20 20 20 2a 2a 20 61 76 61 69 6c 61 62   .    ** availab
11828 6c 65 20 69 66 20 4e 44 45 42 55 47 20 69 73 20  le if NDEBUG is 
11829 64 65 66 69 6e 65 64 20 61 74 20 62 75 69 6c 64  defined at build
1182a 20 74 69 6d 65 2e 0a 20 20 20 20 2a 2f 20 0a 20   time..    */ . 
1182b 20 20 20 70 53 74 61 63 6b 4c 69 6d 69 74 20 3d     pStackLimit =
1182c 20 70 54 6f 73 3b 0a 20 20 20 20 69 66 28 20 21   pTos;.    if( !
1182d 73 71 6c 69 74 65 33 56 64 62 65 4f 70 63 6f 64  sqlite3VdbeOpcod
1182e 65 4e 6f 50 75 73 68 28 70 4f 70 2d 3e 6f 70 63  eNoPush(pOp->opc
1182f 6f 64 65 29 20 29 7b 0a 20 20 20 20 20 20 70 53  ode) ){.      pS
11830 74 61 63 6b 4c 69 6d 69 74 2b 2b 3b 0a 20 20 20  tackLimit++;.   
11831 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 73   }.#endif..    s
11832 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f  witch( pOp->opco
11833 64 65 20 29 7b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  de ){../********
11834 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11835 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11836 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11837 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11838 2a 2a 2a 2a 2a 0a 2a 2a 20 57 68 61 74 20 66 6f  *****.** What fo
11839 6c 6c 6f 77 73 20 69 73 20 61 20 6d 61 73 73 69  llows is a massi
1183a 76 65 20 73 77 69 74 63 68 20 73 74 61 74 65 6d  ve switch statem
1183b 65 6e 74 20 77 68 65 72 65 20 65 61 63 68 20 63  ent where each c
1183c 61 73 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61  ase implements a
1183d 0a 2a 2a 20 73 65 70 61 72 61 74 65 20 69 6e 73  .** separate ins
1183e 74 72 75 63 74 69 6f 6e 20 69 6e 20 74 68 65 20  truction in the 
1183f 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e  virtual machine.
11840 20 20 49 66 20 77 65 20 66 6f 6c 6c 6f 77 20 74    If we follow t
11841 68 65 20 75 73 75 61 6c 0a 2a 2a 20 69 6e 64 65  he usual.** inde
11842 6e 74 61 74 69 6f 6e 20 63 6f 6e 76 65 6e 74 69  ntation conventi
11843 6f 6e 73 2c 20 65 61 63 68 20 63 61 73 65 20 73  ons, each case s
11844 68 6f 75 6c 64 20 62 65 20 69 6e 64 65 6e 74 65  hould be indente
11845 64 20 62 79 20 36 20 73 70 61 63 65 73 2e 20 20  d by 6 spaces.  
11846 42 75 74 0a 2a 2a 20 74 68 61 74 20 69 73 20 61  But.** that is a
11847 20 6c 6f 74 20 6f 66 20 77 61 73 74 65 64 20 73   lot of wasted s
11848 70 61 63 65 20 6f 6e 20 74 68 65 20 6c 65 66 74  pace on the left
11849 20 6d 61 72 67 69 6e 2e 20 20 53 6f 20 74 68 65   margin.  So the
1184a 20 63 6f 64 65 20 77 69 74 68 69 6e 0a 2a 2a 20   code within.** 
1184b 74 68 65 20 73 77 69 74 63 68 20 73 74 61 74 65  the switch state
1184c 6d 65 6e 74 20 77 69 6c 6c 20 62 72 65 61 6b 20  ment will break 
1184d 77 69 74 68 20 63 6f 6e 76 65 6e 74 69 6f 6e 20  with convention 
1184e 61 6e 64 20 62 65 20 66 6c 75 73 68 2d 6c 65 66  and be flush-lef
1184f 74 2e 20 41 6e 6f 74 68 65 72 0a 2a 2a 20 62 69  t. Another.** bi
11850 67 20 63 6f 6d 6d 65 6e 74 20 28 73 69 6d 69 6c  g comment (simil
11851 61 72 20 74 6f 20 74 68 69 73 20 6f 6e 65 29 20  ar to this one) 
11852 77 69 6c 6c 20 6d 61 72 6b 20 74 68 65 20 70 6f  will mark the po
11853 69 6e 74 20 69 6e 20 74 68 65 20 63 6f 64 65 20  int in the code 
11854 77 68 65 72 65 0a 2a 2a 20 77 65 20 74 72 61 6e  where.** we tran
11855 73 69 74 69 6f 6e 20 62 61 63 6b 20 74 6f 20 6e  sition back to n
11856 6f 72 6d 61 6c 20 69 6e 64 65 6e 74 61 74 69 6f  ormal indentatio
11857 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 72  n..**.** The for
11858 6d 61 74 74 69 6e 67 20 6f 66 20 65 61 63 68 20  matting of each 
11859 63 61 73 65 20 69 73 20 69 6d 70 6f 72 74 61 6e  case is importan
1185a 74 2e 20 20 54 68 65 20 6d 61 6b 65 66 69 6c 65  t.  The makefile
1185b 20 66 6f 72 20 53 51 4c 69 74 65 0a 2a 2a 20 67   for SQLite.** g
1185c 65 6e 65 72 61 74 65 73 20 74 77 6f 20 43 20 66  enerates two C f
1185d 69 6c 65 73 20 22 6f 70 63 6f 64 65 73 2e 68 22  iles "opcodes.h"
1185e 20 61 6e 64 20 22 6f 70 63 6f 64 65 73 2e 63 22   and "opcodes.c"
1185f 20 62 79 20 73 63 61 6e 6e 69 6e 67 20 74 68 69   by scanning thi
11860 73 0a 2a 2a 20 66 69 6c 65 20 6c 6f 6f 6b 69 6e  s.** file lookin
11861 67 20 66 6f 72 20 6c 69 6e 65 73 20 74 68 61 74  g for lines that
11862 20 62 65 67 69 6e 20 77 69 74 68 20 22 63 61 73   begin with "cas
11863 65 20 4f 50 5f 22 2e 20 20 54 68 65 20 6f 70 63  e OP_".  The opc
11864 6f 64 65 73 2e 68 20 66 69 6c 65 73 0a 2a 2a 20  odes.h files.** 
11865 77 69 6c 6c 20 62 65 20 66 69 6c 6c 65 64 20 77  will be filled w
11866 69 74 68 20 23 64 65 66 69 6e 65 73 20 74 68 61  ith #defines tha
11867 74 20 67 69 76 65 20 75 6e 69 71 75 65 20 69 6e  t give unique in
11868 74 65 67 65 72 20 76 61 6c 75 65 73 20 74 6f 20  teger values to 
11869 65 61 63 68 0a 2a 2a 20 6f 70 63 6f 64 65 20 61  each.** opcode a
1186a 6e 64 20 74 68 65 20 6f 70 63 6f 64 65 73 2e 63  nd the opcodes.c
1186b 20 66 69 6c 65 20 69 73 20 66 69 6c 6c 65 64 20   file is filled 
1186c 77 69 74 68 20 61 6e 20 61 72 72 61 79 20 6f 66  with an array of
1186d 20 73 74 72 69 6e 67 73 20 77 68 65 72 65 0a 2a   strings where.*
1186e 2a 20 65 61 63 68 20 73 74 72 69 6e 67 20 69 73  * each string is
1186f 20 74 68 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61   the symbolic na
11870 6d 65 20 66 6f 72 20 74 68 65 20 63 6f 72 72 65  me for the corre
11871 73 70 6f 6e 64 69 6e 67 20 6f 70 63 6f 64 65 2e  sponding opcode.
11872 20 20 49 66 20 74 68 65 0a 2a 2a 20 63 61 73 65    If the.** case
11873 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 66 6f   statement is fo
11874 6c 6c 6f 77 65 64 20 62 79 20 61 20 63 6f 6d 6d  llowed by a comm
11875 65 6e 74 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  ent of the form 
11876 22 2f 23 20 73 61 6d 65 20 61 73 20 2e 2e 2e 20  "/# same as ... 
11877 23 2f 22 0a 2a 2a 20 74 68 61 74 20 63 6f 6d 6d  #/".** that comm
11878 65 6e 74 20 69 73 20 75 73 65 64 20 74 6f 20 64  ent is used to d
11879 65 74 65 72 6d 69 6e 65 20 74 68 65 20 70 61 72  etermine the par
1187a 74 69 63 75 6c 61 72 20 76 61 6c 75 65 20 6f 66  ticular value of
1187b 20 74 68 65 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a   the opcode..**.
1187c 2a 2a 20 49 66 20 61 20 63 6f 6d 6d 65 6e 74 20  ** If a comment 
1187d 6f 6e 20 74 68 65 20 73 61 6d 65 20 6c 69 6e 65  on the same line
1187e 20 61 73 20 74 68 65 20 22 63 61 73 65 20 4f 50   as the "case OP
1187f 5f 22 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20  _" construction 
11880 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65 20  contains.** the 
11881 77 6f 72 64 20 22 6e 6f 2d 70 75 73 68 22 2c 20  word "no-push", 
11882 74 68 65 6e 20 74 68 65 20 6f 70 63 6f 64 65 20  then the opcode 
11883 69 73 20 67 75 61 72 65 6e 74 65 65 64 20 6e 6f  is guarenteed no
11884 74 20 74 6f 20 67 72 6f 77 20 74 68 65 20 0a 2a  t to grow the .*
11885 2a 20 76 64 62 65 20 73 74 61 63 6b 20 77 68 65  * vdbe stack whe
11886 6e 20 69 74 20 69 73 20 65 78 65 63 75 74 65 64  n it is executed
11887 2e 20 53 65 65 20 66 75 6e 63 74 69 6f 6e 20 6f  . See function o
11888 70 63 6f 64 65 28 29 20 69 6e 0a 2a 2a 20 76 64  pcode() in.** vd
11889 62 65 61 75 78 2e 63 20 66 6f 72 20 64 65 74 61  beaux.c for deta
1188a 69 6c 73 2e 0a 2a 2a 0a 2a 2a 20 44 6f 63 75 6d  ils..**.** Docum
1188b 65 6e 74 61 74 69 6f 6e 20 61 62 6f 75 74 20 56  entation about V
1188c 44 42 45 20 6f 70 63 6f 64 65 73 20 69 73 20 67  DBE opcodes is g
1188d 65 6e 65 72 61 74 65 64 20 62 79 20 73 63 61 6e  enerated by scan
1188e 6e 69 6e 67 20 74 68 69 73 20 66 69 6c 65 0a 2a  ning this file.*
1188f 2a 20 66 6f 72 20 6c 69 6e 65 73 20 6f 66 20 74  * for lines of t
11890 68 61 74 20 63 6f 6e 74 61 69 6e 20 22 4f 70 63  hat contain "Opc
11891 6f 64 65 3a 22 2e 20 20 54 68 61 74 20 6c 69 6e  ode:".  That lin
11892 65 20 61 6e 64 20 61 6c 6c 20 73 75 62 73 65 71  e and all subseq
11893 75 65 6e 74 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 20  uent.** comment 
11894 6c 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 69  lines are used i
11895 6e 20 74 68 65 20 67 65 6e 65 72 61 74 69 6f 6e  n the generation
11896 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 2e 68   of the opcode.h
11897 74 6d 6c 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f  tml documentatio
11898 6e 0a 2a 2a 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a  n.** file..**.**
11899 20 53 55 4d 4d 41 52 59 3a 0a 2a 2a 0a 2a 2a 20   SUMMARY:.**.** 
1189a 20 20 20 20 46 6f 72 6d 61 74 74 69 6e 67 20 69      Formatting i
1189b 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 73  s important to s
1189c 63 72 69 70 74 73 20 74 68 61 74 20 73 63 61 6e  cripts that scan
1189d 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2a 20 20   this file..**  
1189e 20 20 20 44 6f 20 6e 6f 74 20 64 65 76 69 61 74     Do not deviat
1189f 65 20 66 72 6f 6d 20 74 68 65 20 66 6f 72 6d 61  e from the forma
118a0 74 74 69 6e 67 20 73 74 79 6c 65 20 63 75 72 72  tting style curr
118a1 65 6e 74 6c 79 20 69 6e 20 75 73 65 2e 0a 2a 2a  ently in use..**
118a2 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
118a3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
118a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
118a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
118a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
118a7 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 47 6f 74  ./* Opcode:  Got
118a8 6f 20 2a 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 41  o * P2 *.**.** A
118a9 6e 20 75 6e 63 6f 6e 64 69 74 69 6f 6e 61 6c 20  n unconditional 
118aa 6a 75 6d 70 20 74 6f 20 61 64 64 72 65 73 73 20  jump to address 
118ab 50 32 2e 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20  P2..** The next 
118ac 69 6e 73 74 72 75 63 74 69 6f 6e 20 65 78 65 63  instruction exec
118ad 75 74 65 64 20 77 69 6c 6c 20 62 65 20 0a 2a 2a  uted will be .**
118ae 20 74 68 65 20 6f 6e 65 20 61 74 20 69 6e 64 65   the one at inde
118af 78 20 50 32 20 66 72 6f 6d 20 74 68 65 20 62 65  x P2 from the be
118b0 67 69 6e 6e 69 6e 67 20 6f 66 0a 2a 2a 20 74 68  ginning of.** th
118b1 65 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 63 61  e program..*/.ca
118b2 73 65 20 4f 50 5f 47 6f 74 6f 3a 20 7b 20 20 20  se OP_Goto: {   
118b3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d            /* no-
118b4 70 75 73 68 20 2a 2f 0a 20 20 43 48 45 43 4b 5f  push */.  CHECK_
118b5 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20  FOR_INTERRUPT;. 
118b6 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
118b7 31 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  1;.  break;.}../
118b8 2a 20 4f 70 63 6f 64 65 3a 20 20 47 6f 73 75 62  * Opcode:  Gosub
118b9 20 2a 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 50 75   * P2 *.**.** Pu
118ba 73 68 20 74 68 65 20 63 75 72 72 65 6e 74 20 61  sh the current a
118bb 64 64 72 65 73 73 20 70 6c 75 73 20 31 20 6f 6e  ddress plus 1 on
118bc 74 6f 20 74 68 65 20 72 65 74 75 72 6e 20 61 64  to the return ad
118bd 64 72 65 73 73 20 73 74 61 63 6b 0a 2a 2a 20 61  dress stack.** a
118be 6e 64 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20  nd then jump to 
118bf 61 64 64 72 65 73 73 20 50 32 2e 0a 2a 2a 0a 2a  address P2..**.*
118c0 2a 20 54 68 65 20 72 65 74 75 72 6e 20 61 64 64  * The return add
118c1 72 65 73 73 20 73 74 61 63 6b 20 69 73 20 6f 66  ress stack is of
118c2 20 6c 69 6d 69 74 65 64 20 64 65 70 74 68 2e 20   limited depth. 
118c3 20 49 66 20 74 6f 6f 20 6d 61 6e 79 0a 2a 2a 20   If too many.** 
118c4 4f 50 5f 47 6f 73 75 62 20 6f 70 65 72 61 74 69  OP_Gosub operati
118c5 6f 6e 73 20 6f 63 63 75 72 20 77 69 74 68 6f 75  ons occur withou
118c6 74 20 69 6e 74 65 72 76 65 6e 69 6e 67 20 4f 50  t intervening OP
118c7 5f 52 65 74 75 72 6e 73 2c 20 74 68 65 6e 0a 2a  _Returns, then.*
118c8 2a 20 74 68 65 20 72 65 74 75 72 6e 20 61 64 64  * the return add
118c9 72 65 73 73 20 73 74 61 63 6b 20 77 69 6c 6c 20  ress stack will 
118ca 66 69 6c 6c 20 75 70 20 61 6e 64 20 70 72 6f 63  fill up and proc
118cb 65 73 73 69 6e 67 20 77 69 6c 6c 20 61 62 6f 72  essing will abor
118cc 74 0a 2a 2a 20 77 69 74 68 20 61 20 66 61 74 61  t.** with a fata
118cd 6c 20 65 72 72 6f 72 2e 0a 2a 2f 0a 63 61 73 65  l error..*/.case
118ce 20 4f 50 5f 47 6f 73 75 62 3a 20 7b 20 20 20 20   OP_Gosub: {    
118cf 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75          /* no-pu
118d0 73 68 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  sh */.  assert( 
118d1 70 2d 3e 72 65 74 75 72 6e 44 65 70 74 68 3c 73  p->returnDepth<s
118d2 69 7a 65 6f 66 28 70 2d 3e 72 65 74 75 72 6e 53  izeof(p->returnS
118d3 74 61 63 6b 29 2f 73 69 7a 65 6f 66 28 70 2d 3e  tack)/sizeof(p->
118d4 72 65 74 75 72 6e 53 74 61 63 6b 5b 30 5d 29 20  returnStack[0]) 
118d5 29 3b 0a 20 20 70 2d 3e 72 65 74 75 72 6e 53 74  );.  p->returnSt
118d6 61 63 6b 5b 70 2d 3e 72 65 74 75 72 6e 44 65 70  ack[p->returnDep
118d7 74 68 2b 2b 5d 20 3d 20 70 63 2b 31 3b 0a 20 20  th++] = pc+1;.  
118d8 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31  pc = pOp->p2 - 1
118d9 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
118da 20 4f 70 63 6f 64 65 3a 20 20 52 65 74 75 72 6e   Opcode:  Return
118db 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d   * * *.**.** Jum
118dc 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f  p immediately to
118dd 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75   the next instru
118de 63 74 69 6f 6e 20 61 66 74 65 72 20 74 68 65 20  ction after the 
118df 6c 61 73 74 20 75 6e 72 65 74 75 72 6e 65 64 0a  last unreturned.
118e0 2a 2a 20 4f 50 5f 47 6f 73 75 62 2e 20 20 49 66  ** OP_Gosub.  If
118e1 20 61 6e 20 4f 50 5f 52 65 74 75 72 6e 20 68 61   an OP_Return ha
118e2 73 20 6f 63 63 75 72 72 65 64 20 66 6f 72 20 61  s occurred for a
118e3 6c 6c 20 4f 50 5f 47 6f 73 75 62 73 2c 20 74 68  ll OP_Gosubs, th
118e4 65 6e 0a 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67  en.** processing
118e5 20 61 62 6f 72 74 73 20 77 69 74 68 20 61 20 66   aborts with a f
118e6 61 74 61 6c 20 65 72 72 6f 72 2e 0a 2a 2f 0a 63  atal error..*/.c
118e7 61 73 65 20 4f 50 5f 52 65 74 75 72 6e 3a 20 7b  ase OP_Return: {
118e8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f             /* no
118e9 2d 70 75 73 68 20 2a 2f 0a 20 20 61 73 73 65 72  -push */.  asser
118ea 74 28 20 70 2d 3e 72 65 74 75 72 6e 44 65 70 74  t( p->returnDept
118eb 68 3e 30 20 29 3b 0a 20 20 70 2d 3e 72 65 74 75  h>0 );.  p->retu
118ec 72 6e 44 65 70 74 68 2d 2d 3b 0a 20 20 70 63 20  rnDepth--;.  pc 
118ed 3d 20 70 2d 3e 72 65 74 75 72 6e 53 74 61 63 6b  = p->returnStack
118ee 5b 70 2d 3e 72 65 74 75 72 6e 44 65 70 74 68 5d  [p->returnDepth]
118ef 20 2d 20 31 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d   - 1;.  break;.}
118f0 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 48 61  ../* Opcode:  Ha
118f1 6c 74 20 50 31 20 50 32 20 50 33 0a 2a 2a 0a 2a  lt P1 P2 P3.**.*
118f2 2a 20 45 78 69 74 20 69 6d 6d 65 64 69 61 74 65  * Exit immediate
118f3 6c 79 2e 20 20 41 6c 6c 20 6f 70 65 6e 20 63 75  ly.  All open cu
118f4 72 73 6f 72 73 2c 20 46 69 66 6f 73 2c 20 65 74  rsors, Fifos, et
118f5 63 20 61 72 65 20 63 6c 6f 73 65 64 0a 2a 2a 20  c are closed.** 
118f6 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 0a 2a  automatically..*
118f7 2a 0a 2a 2a 20 50 31 20 69 73 20 74 68 65 20 72  *.** P1 is the r
118f8 65 73 75 6c 74 20 63 6f 64 65 20 72 65 74 75 72  esult code retur
118f9 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 65  ned by sqlite3_e
118fa 78 65 63 28 29 2c 20 73 71 6c 69 74 65 33 5f 72  xec(), sqlite3_r
118fb 65 73 65 74 28 29 2c 0a 2a 2a 20 6f 72 20 73 71  eset(),.** or sq
118fc 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29  lite3_finalize()
118fd 2e 20 20 46 6f 72 20 61 20 6e 6f 72 6d 61 6c 20  .  For a normal 
118fe 68 61 6c 74 2c 20 74 68 69 73 20 73 68 6f 75 6c  halt, this shoul
118ff 64 20 62 65 20 53 51 4c 49 54 45 5f 4f 4b 20 28  d be SQLITE_OK (
11900 30 29 2e 0a 2a 2a 20 46 6f 72 20 65 72 72 6f 72  0)..** For error
11901 73 2c 20 69 74 20 63 61 6e 20 62 65 20 73 6f 6d  s, it can be som
11902 65 20 6f 74 68 65 72 20 76 61 6c 75 65 2e 20 20  e other value.  
11903 49 66 20 50 31 21 3d 30 20 74 68 65 6e 20 50 32  If P1!=0 then P2
11904 20 77 69 6c 6c 20 64 65 74 65 72 6d 69 6e 65 0a   will determine.
11905 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  ** whether or no
11906 74 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68  t to rollback th
11907 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61  e current transa
11908 63 74 69 6f 6e 2e 20 20 44 6f 20 6e 6f 74 20 72  ction.  Do not r
11909 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 69 66 20 50 32  ollback.** if P2
1190a 3d 3d 4f 45 5f 46 61 69 6c 2e 20 44 6f 20 74 68  ==OE_Fail. Do th
1190b 65 20 72 6f 6c 6c 62 61 63 6b 20 69 66 20 50 32  e rollback if P2
1190c 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 2e 20 20  ==OE_Rollback.  
1190d 49 66 20 50 32 3d 3d 4f 45 5f 41 62 6f 72 74 2c  If P2==OE_Abort,
1190e 0a 2a 2a 20 74 68 65 6e 20 62 61 63 6b 20 6f 75  .** then back ou
1190f 74 20 61 6c 6c 20 63 68 61 6e 67 65 73 20 74 68  t all changes th
11910 61 74 20 68 61 76 65 20 6f 63 63 75 72 72 65 64  at have occurred
11911 20 64 75 72 69 6e 67 20 74 68 69 73 20 65 78 65   during this exe
11912 63 75 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a  cution of the.**
11913 20 56 44 42 45 2c 20 62 75 74 20 64 6f 20 6e 6f   VDBE, but do no
11914 74 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74  t rollback the t
11915 72 61 6e 73 61 63 74 69 6f 6e 2e 20 0a 2a 2a 0a  ransaction. .**.
11916 2a 2a 20 49 66 20 50 33 20 69 73 20 6e 6f 74 20  ** If P3 is not 
11917 6e 75 6c 6c 20 74 68 65 6e 20 69 74 20 69 73 20  null then it is 
11918 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
11919 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54   string..**.** T
1191a 68 65 72 65 20 69 73 20 61 6e 20 69 6d 70 6c 69  here is an impli
1191b 65 64 20 22 48 61 6c 74 20 30 20 30 20 30 22 20  ed "Halt 0 0 0" 
1191c 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 73 65  instruction inse
1191d 72 74 65 64 20 61 74 20 74 68 65 20 76 65 72 79  rted at the very
1191e 20 65 6e 64 20 6f 66 0a 2a 2a 20 65 76 65 72 79   end of.** every
1191f 20 70 72 6f 67 72 61 6d 2e 20 20 53 6f 20 61 20   program.  So a 
11920 6a 75 6d 70 20 70 61 73 74 20 74 68 65 20 6c 61  jump past the la
11921 73 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f  st instruction o
11922 66 20 74 68 65 20 70 72 6f 67 72 61 6d 0a 2a 2a  f the program.**
11923 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20   is the same as 
11924 65 78 65 63 75 74 69 6e 67 20 48 61 6c 74 2e 0a  executing Halt..
11925 2a 2f 0a 63 61 73 65 20 4f 50 5f 48 61 6c 74 3a  */.case OP_Halt:
11926 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a   {            /*
11927 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 70 2d   no-push */.  p-
11928 3e 70 54 6f 73 20 3d 20 70 54 6f 73 3b 0a 20 20  >pTos = pTos;.  
11929 70 2d 3e 72 63 20 3d 20 70 4f 70 2d 3e 70 31 3b  p->rc = pOp->p1;
1192a 0a 20 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a 20  .  p->pc = pc;. 
1192b 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e 20   p->errorAction 
1192c 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 69 66 28  = pOp->p2;.  if(
1192d 20 70 4f 70 2d 3e 70 33 20 29 7b 0a 20 20 20 20   pOp->p3 ){.    
1192e 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67  sqlite3SetString
1192f 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 70 4f  (&p->zErrMsg, pO
11930 70 2d 3e 70 33 2c 20 28 63 68 61 72 2a 29 30 29  p->p3, (char*)0)
11931 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c  ;.  }.  rc = sql
11932 69 74 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b  ite3VdbeHalt(p);
11933 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53  .  assert( rc==S
11934 51 4c 49 54 45 5f 42 55 53 59 20 7c 7c 20 72 63  QLITE_BUSY || rc
11935 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20  ==SQLITE_OK );. 
11936 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
11937 42 55 53 59 20 29 7b 0a 20 20 20 20 70 2d 3e 72  BUSY ){.    p->r
11938 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  c = SQLITE_BUSY;
11939 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
1193a 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20 72  TE_BUSY;.  }.  r
1193b 65 74 75 72 6e 20 70 2d 3e 72 63 20 3f 20 53 51  eturn p->rc ? SQ
1193c 4c 49 54 45 5f 45 52 52 4f 52 20 3a 20 53 51 4c  LITE_ERROR : SQL
1193d 49 54 45 5f 44 4f 4e 45 3b 0a 7d 0a 0a 2f 2a 20  ITE_DONE;.}../* 
1193e 4f 70 63 6f 64 65 3a 20 49 6e 74 65 67 65 72 20  Opcode: Integer 
1193f 50 31 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65  P1 * *.**.** The
11940 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20   32-bit integer 
11941 76 61 6c 75 65 20 50 31 20 69 73 20 70 75 73 68  value P1 is push
11942 65 64 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63  ed onto the stac
11943 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e  k..*/.case OP_In
11944 74 65 67 65 72 3a 20 7b 0a 20 20 70 54 6f 73 2b  teger: {.  pTos+
11945 2b 3b 0a 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73  +;.  pTos->flags
11946 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 54   = MEM_Int;.  pT
11947 6f 73 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70  os->u.i = pOp->p
11948 31 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  1;.  break;.}../
11949 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 74 36 34 20  * Opcode: Int64 
1194a 2a 20 2a 20 50 33 0a 2a 2a 0a 2a 2a 20 50 33 20  * * P3.**.** P3 
1194b 69 73 20 61 20 73 74 72 69 6e 67 20 72 65 70 72  is a string repr
1194c 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e  esentation of an
1194d 20 69 6e 74 65 67 65 72 2e 20 20 43 6f 6e 76 65   integer.  Conve
1194e 72 74 20 74 68 61 74 20 69 6e 74 65 67 65 72 0a  rt that integer.
1194f 2a 2a 20 74 6f 20 61 20 36 34 2d 62 69 74 20 76  ** to a 64-bit v
11950 61 6c 75 65 20 61 6e 64 20 70 75 73 68 20 69 74  alue and push it
11951 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 2e   onto the stack.
11952 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 74 36  .*/.case OP_Int6
11953 34 3a 20 7b 0a 20 20 70 54 6f 73 2b 2b 3b 0a 20  4: {.  pTos++;. 
11954 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33   assert( pOp->p3
11955 21 3d 30 20 29 3b 0a 20 20 70 54 6f 73 2d 3e 66  !=0 );.  pTos->f
11956 6c 61 67 73 20 3d 20 4d 45 4d 5f 53 74 72 7c 4d  lags = MEM_Str|M
11957 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 54 65  EM_Static|MEM_Te
11958 72 6d 3b 0a 20 20 70 54 6f 73 2d 3e 7a 20 3d 20  rm;.  pTos->z = 
11959 70 4f 70 2d 3e 70 33 3b 0a 20 20 70 54 6f 73 2d  pOp->p3;.  pTos-
1195a 3e 6e 20 3d 20 73 74 72 6c 65 6e 28 70 54 6f 73  >n = strlen(pTos
1195b 2d 3e 7a 29 3b 0a 20 20 70 54 6f 73 2d 3e 65 6e  ->z);.  pTos->en
1195c 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b  c = SQLITE_UTF8;
1195d 0a 20 20 70 54 6f 73 2d 3e 75 2e 69 20 3d 20 73  .  pTos->u.i = s
1195e 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c  qlite3VdbeIntVal
1195f 75 65 28 70 54 6f 73 29 3b 0a 20 20 70 54 6f 73  ue(pTos);.  pTos
11960 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 49  ->flags |= MEM_I
11961 6e 74 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  nt;.  break;.}..
11962 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 61 6c 20  /* Opcode: Real 
11963 2a 20 2a 20 50 33 0a 2a 2a 0a 2a 2a 20 54 68 65  * * P3.**.** The
11964 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 50 33   string value P3
11965 20 69 73 20 63 6f 6e 76 65 72 74 65 64 20 74 6f   is converted to
11966 20 61 20 72 65 61 6c 20 61 6e 64 20 70 75 73 68   a real and push
11967 65 64 20 6f 6e 20 74 6f 20 74 68 65 20 73 74 61  ed on to the sta
11968 63 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52  ck..*/.case OP_R
11969 65 61 6c 3a 20 7b 20 20 20 20 20 20 20 20 20 20  eal: {          
1196a 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
1196b 46 4c 4f 41 54 2c 20 2a 2f 0a 20 20 70 54 6f 73  FLOAT, */.  pTos
1196c 2b 2b 3b 0a 20 20 70 54 6f 73 2d 3e 66 6c 61 67  ++;.  pTos->flag
1196d 73 20 3d 20 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f  s = MEM_Str|MEM_
1196e 53 74 61 74 69 63 7c 4d 45 4d 5f 54 65 72 6d 3b  Static|MEM_Term;
1196f 0a 20 20 70 54 6f 73 2d 3e 7a 20 3d 20 70 4f 70  .  pTos->z = pOp
11970 2d 3e 70 33 3b 0a 20 20 70 54 6f 73 2d 3e 6e 20  ->p3;.  pTos->n 
11971 3d 20 73 74 72 6c 65 6e 28 70 54 6f 73 2d 3e 7a  = strlen(pTos->z
11972 29 3b 0a 20 20 70 54 6f 73 2d 3e 65 6e 63 20 3d  );.  pTos->enc =
11973 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20   SQLITE_UTF8;.  
11974 70 54 6f 73 2d 3e 72 20 3d 20 73 71 6c 69 74 65  pTos->r = sqlite
11975 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70  3VdbeRealValue(p
11976 54 6f 73 29 3b 0a 20 20 70 54 6f 73 2d 3e 66 6c  Tos);.  pTos->fl
11977 61 67 73 20 7c 3d 20 4d 45 4d 5f 52 65 61 6c 3b  ags |= MEM_Real;
11978 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  .  sqlite3VdbeCh
11979 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 54 6f  angeEncoding(pTo
1197a 73 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20  s, encoding);.  
1197b 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1197c 6f 64 65 3a 20 53 74 72 69 6e 67 38 20 2a 20 2a  ode: String8 * *
1197d 20 50 33 0a 2a 2a 0a 2a 2a 20 50 33 20 70 6f 69   P3.**.** P3 poi
1197e 6e 74 73 20 74 6f 20 61 20 6e 75 6c 20 74 65 72  nts to a nul ter
1197f 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74  minated UTF-8 st
11980 72 69 6e 67 2e 20 54 68 69 73 20 6f 70 63 6f 64  ring. This opcod
11981 65 20 69 73 20 74 72 61 6e 73 66 6f 72 6d 65 64  e is transformed
11982 20 0a 2a 2a 20 69 6e 74 6f 20 61 6e 20 4f 50 5f   .** into an OP_
11983 53 74 72 69 6e 67 20 62 65 66 6f 72 65 20 69 74  String before it
11984 20 69 73 20 65 78 65 63 75 74 65 64 20 66 6f 72   is executed for
11985 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 2e   the first time.
11986 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 74 72 69  .*/.case OP_Stri
11987 6e 67 38 3a 20 7b 20 20 20 20 20 20 20 20 20 2f  ng8: {         /
11988 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 54 52  * same as TK_STR
11989 49 4e 47 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ING */.  assert(
1198a 20 70 4f 70 2d 3e 70 33 21 3d 30 20 29 3b 0a 20   pOp->p3!=0 );. 
1198b 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f   pOp->opcode = O
1198c 50 5f 53 74 72 69 6e 67 3b 0a 20 20 70 4f 70 2d  P_String;.  pOp-
1198d 3e 70 31 20 3d 20 73 74 72 6c 65 6e 28 70 4f 70  >p1 = strlen(pOp
1198e 2d 3e 70 33 29 3b 0a 20 20 61 73 73 65 72 74 28  ->p3);.  assert(
1198f 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f   SQLITE_MAX_SQL_
11990 4c 45 4e 47 54 48 20 3c 20 53 51 4c 49 54 45 5f  LENGTH < SQLITE_
11991 4d 41 58 5f 4c 45 4e 47 54 48 20 29 3b 0a 20 20  MAX_LENGTH );.  
11992 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 20  assert( pOp->p1 
11993 3c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e  < SQLITE_MAX_LEN
11994 47 54 48 20 29 3b 0a 0a 23 69 66 6e 64 65 66 20  GTH );..#ifndef 
11995 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
11996 36 0a 20 20 69 66 28 20 65 6e 63 6f 64 69 6e 67  6.  if( encoding
11997 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b  !=SQLITE_UTF8 ){
11998 0a 20 20 20 20 70 54 6f 73 2b 2b 3b 0a 20 20 20  .    pTos++;.   
11999 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1199a 65 74 53 74 72 28 70 54 6f 73 2c 20 70 4f 70 2d  etStr(pTos, pOp-
1199b 3e 70 33 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f  >p3, -1, SQLITE_
1199c 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41  UTF8, SQLITE_STA
1199d 54 49 43 29 3b 0a 20 20 20 20 69 66 28 20 53 51  TIC);.    if( SQ
1199e 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33  LITE_OK!=sqlite3
1199f 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69  VdbeChangeEncodi
119a0 6e 67 28 70 54 6f 73 2c 20 65 6e 63 6f 64 69 6e  ng(pTos, encodin
119a1 67 29 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d  g) ) goto no_mem
119a2 3b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45  ;.    if( SQLITE
119a3 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 56 64 62 65  _OK!=sqlite3Vdbe
119a4 4d 65 6d 44 79 6e 61 6d 69 63 69 66 79 28 70 54  MemDynamicify(pT
119a5 6f 73 29 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65  os) ) goto no_me
119a6 6d 3b 0a 20 20 20 20 70 54 6f 73 2d 3e 66 6c 61  m;.    pTos->fla
119a7 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 44 79 6e 29  gs &= ~(MEM_Dyn)
119a8 3b 0a 20 20 20 20 70 54 6f 73 2d 3e 66 6c 61 67  ;.    pTos->flag
119a9 73 20 7c 3d 20 4d 45 4d 5f 53 74 61 74 69 63 3b  s |= MEM_Static;
119aa 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 33  .    if( pOp->p3
119ab 74 79 70 65 3d 3d 50 33 5f 44 59 4e 41 4d 49 43  type==P3_DYNAMIC
119ac 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
119ad 46 72 65 65 28 70 4f 70 2d 3e 70 33 29 3b 0a 20  Free(pOp->p3);. 
119ae 20 20 20 7d 0a 20 20 20 20 70 4f 70 2d 3e 70 33     }.    pOp->p3
119af 74 79 70 65 20 3d 20 50 33 5f 44 59 4e 41 4d 49  type = P3_DYNAMI
119b0 43 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 33 20 3d  C;.    pOp->p3 =
119b1 20 70 54 6f 73 2d 3e 7a 3b 0a 20 20 20 20 70 4f   pTos->z;.    pO
119b2 70 2d 3e 70 31 20 3d 20 70 54 6f 73 2d 3e 6e 3b  p->p1 = pTos->n;
119b3 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70  .    assert( pOp
119b4 2d 3e 70 31 20 3c 20 53 51 4c 49 54 45 5f 4d 41  ->p1 < SQLITE_MA
119b5 58 5f 4c 45 4e 47 54 48 20 29 3b 20 2f 2a 20 44  X_LENGTH ); /* D
119b6 75 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 58  ue to SQLITE_MAX
119b7 5f 53 51 4c 5f 4c 45 4e 47 54 48 20 2a 2f 0a 20  _SQL_LENGTH */. 
119b8 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 23 65     break;.  }.#e
119b9 6e 64 69 66 0a 20 20 2f 2a 20 4f 74 68 65 72 77  ndif.  /* Otherw
119ba 69 73 65 20 66 61 6c 6c 20 74 68 72 6f 75 67 68  ise fall through
119bb 20 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73   to the next cas
119bc 65 2c 20 4f 50 5f 53 74 72 69 6e 67 20 2a 2f 0a  e, OP_String */.
119bd 7d 0a 20 20 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  }.  ./* Opcode: 
119be 53 74 72 69 6e 67 20 50 31 20 2a 20 50 33 0a 2a  String P1 * P3.*
119bf 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 20  *.** The string 
119c0 76 61 6c 75 65 20 50 33 20 6f 66 20 6c 65 6e 67  value P3 of leng
119c1 74 68 20 50 31 20 28 62 79 74 65 73 29 20 69 73  th P1 (bytes) is
119c2 20 70 75 73 68 65 64 20 6f 6e 74 6f 20 74 68 65   pushed onto the
119c3 20 73 74 61 63 6b 2e 0a 2a 2f 0a 63 61 73 65 20   stack..*/.case 
119c4 4f 50 5f 53 74 72 69 6e 67 3a 20 7b 0a 20 20 61  OP_String: {.  a
119c5 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 20 3c  ssert( pOp->p1 <
119c6 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47   SQLITE_MAX_LENG
119c7 54 48 20 29 3b 20 2f 2a 20 44 75 65 20 74 6f 20  TH ); /* Due to 
119c8 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c  SQLITE_MAX_SQL_L
119c9 45 4e 47 54 48 20 2a 2f 0a 20 20 70 54 6f 73 2b  ENGTH */.  pTos+
119ca 2b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  +;.  assert( pOp
119cb 2d 3e 70 33 21 3d 30 20 29 3b 0a 20 20 70 54 6f  ->p3!=0 );.  pTo
119cc 73 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 53  s->flags = MEM_S
119cd 74 72 7c 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45  tr|MEM_Static|ME
119ce 4d 5f 54 65 72 6d 3b 0a 20 20 70 54 6f 73 2d 3e  M_Term;.  pTos->
119cf 7a 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 70  z = pOp->p3;.  p
119d0 54 6f 73 2d 3e 6e 20 3d 20 70 4f 70 2d 3e 70 31  Tos->n = pOp->p1
119d1 3b 0a 20 20 70 54 6f 73 2d 3e 65 6e 63 20 3d 20  ;.  pTos->enc = 
119d2 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 62 72 65 61  encoding;.  brea
119d3 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
119d4 20 4e 75 6c 6c 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a   Null * * *.**.*
119d5 2a 20 50 75 73 68 20 61 20 4e 55 4c 4c 20 6f 6e  * Push a NULL on
119d6 74 6f 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2f  to the stack..*/
119d7 0a 63 61 73 65 20 4f 50 5f 4e 75 6c 6c 3a 20 7b  .case OP_Null: {
119d8 0a 20 20 70 54 6f 73 2b 2b 3b 0a 20 20 70 54 6f  .  pTos++;.  pTo
119d9 73 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e  s->flags = MEM_N
119da 75 6c 6c 3b 0a 20 20 70 54 6f 73 2d 3e 6e 20 3d  ull;.  pTos->n =
119db 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a   0;.  break;.}..
119dc 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
119dd 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41  OMIT_BLOB_LITERA
119de 4c 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 48 65 78  L./* Opcode: Hex
119df 42 6c 6f 62 20 2a 20 2a 20 50 33 0a 2a 2a 0a 2a  Blob * * P3.**.*
119e0 2a 20 50 33 20 69 73 20 61 6e 20 55 54 46 2d 38  * P3 is an UTF-8
119e1 20 53 51 4c 20 68 65 78 20 65 6e 63 6f 64 69 6e   SQL hex encodin
119e2 67 20 6f 66 20 61 20 62 6c 6f 62 2e 20 54 68 65  g of a blob. The
119e3 20 62 6c 6f 62 20 69 73 20 70 75 73 68 65 64 20   blob is pushed 
119e4 6f 6e 74 6f 20 74 68 65 0a 2a 2a 20 76 64 62 65  onto the.** vdbe
119e5 20 73 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68   stack..**.** Th
119e6 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 69  e first time thi
119e7 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 65 78  s instruction ex
119e8 65 63 75 74 65 73 2c 20 69 6e 20 74 72 61 6e 73  ecutes, in trans
119e9 66 6f 72 6d 73 20 69 74 73 65 6c 66 20 69 6e 74  forms itself int
119ea 6f 20 61 0a 2a 2a 20 27 42 6c 6f 62 27 20 6f 70  o a.** 'Blob' op
119eb 63 6f 64 65 20 77 69 74 68 20 61 20 62 69 6e 61  code with a bina
119ec 72 79 20 62 6c 6f 62 20 61 73 20 50 33 2e 0a 2a  ry blob as P3..*
119ed 2f 0a 63 61 73 65 20 4f 50 5f 48 65 78 42 6c 6f  /.case OP_HexBlo
119ee 62 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  b: {            
119ef 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 4c  /* same as TK_BL
119f0 4f 42 20 2a 2f 0a 20 20 70 4f 70 2d 3e 6f 70 63  OB */.  pOp->opc
119f1 6f 64 65 20 3d 20 4f 50 5f 42 6c 6f 62 3b 0a 20  ode = OP_Blob;. 
119f2 20 70 4f 70 2d 3e 70 31 20 3d 20 73 74 72 6c 65   pOp->p1 = strle
119f3 6e 28 70 4f 70 2d 3e 70 33 29 2f 32 3b 0a 20 20  n(pOp->p3)/2;.  
119f4 61 73 73 65 72 74 28 20 53 51 4c 49 54 45 5f 4d  assert( SQLITE_M
119f5 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 20 3c 20  AX_SQL_LENGTH < 
119f6 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54  SQLITE_MAX_LENGT
119f7 48 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  H );.  assert( p
119f8 4f 70 2d 3e 70 31 20 3c 20 53 51 4c 49 54 45 5f  Op->p1 < SQLITE_
119f9 4d 41 58 5f 4c 45 4e 47 54 48 20 29 3b 0a 20 20  MAX_LENGTH );.  
119fa 69 66 28 20 70 4f 70 2d 3e 70 31 20 29 7b 0a 20  if( pOp->p1 ){. 
119fb 20 20 20 63 68 61 72 20 2a 7a 42 6c 6f 62 20 3d     char *zBlob =
119fc 20 73 71 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f   sqlite3HexToBlo
119fd 62 28 70 4f 70 2d 3e 70 33 29 3b 0a 20 20 20 20  b(pOp->p3);.    
119fe 69 66 28 20 21 7a 42 6c 6f 62 20 29 20 67 6f 74  if( !zBlob ) got
119ff 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 69 66  o no_mem;.    if
11a00 28 20 70 4f 70 2d 3e 70 33 74 79 70 65 3d 3d 50  ( pOp->p3type==P
11a01 33 5f 44 59 4e 41 4d 49 43 20 29 7b 0a 20 20 20  3_DYNAMIC ){.   
11a02 20 20 20 73 71 6c 69 74 65 46 72 65 65 28 70 4f     sqliteFree(pO
11a03 70 2d 3e 70 33 29 3b 0a 20 20 20 20 7d 0a 20 20  p->p3);.    }.  
11a04 20 20 70 4f 70 2d 3e 70 33 20 3d 20 7a 42 6c 6f    pOp->p3 = zBlo
11a05 62 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 33 74 79  b;.    pOp->p3ty
11a06 70 65 20 3d 20 50 33 5f 44 59 4e 41 4d 49 43 3b  pe = P3_DYNAMIC;
11a07 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66  .  }else{.    if
11a08 28 20 70 4f 70 2d 3e 70 33 74 79 70 65 3d 3d 50  ( pOp->p3type==P
11a09 33 5f 44 59 4e 41 4d 49 43 20 29 7b 0a 20 20 20  3_DYNAMIC ){.   
11a0a 20 20 20 73 71 6c 69 74 65 46 72 65 65 28 70 4f     sqliteFree(pO
11a0b 70 2d 3e 70 33 29 3b 0a 20 20 20 20 7d 0a 20 20  p->p3);.    }.  
11a0c 20 20 70 4f 70 2d 3e 70 33 74 79 70 65 20 3d 20    pOp->p3type = 
11a0d 50 33 5f 53 54 41 54 49 43 3b 0a 20 20 20 20 70  P3_STATIC;.    p
11a0e 4f 70 2d 3e 70 33 20 3d 20 22 22 3b 0a 20 20 7d  Op->p3 = "";.  }
11a0f 0a 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f  ..  /* Fall thro
11a10 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ugh to the next 
11a11 63 61 73 65 2c 20 4f 50 5f 42 6c 6f 62 2e 20 2a  case, OP_Blob. *
11a12 2f 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  /.}../* Opcode: 
11a13 42 6c 6f 62 20 50 31 20 2a 20 50 33 0a 2a 2a 0a  Blob P1 * P3.**.
11a14 2a 2a 20 50 33 20 70 6f 69 6e 74 73 20 74 6f 20  ** P3 points to 
11a15 61 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 20 50  a blob of data P
11a16 31 20 62 79 74 65 73 20 6c 6f 6e 67 2e 20 50 75  1 bytes long. Pu
11a17 73 68 20 74 68 69 73 0a 2a 2a 20 76 61 6c 75 65  sh this.** value
11a18 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 2e   onto the stack.
11a19 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   This instructio
11a1a 6e 20 69 73 20 6e 6f 74 20 63 6f 64 65 64 20 64  n is not coded d
11a1b 69 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 74 68  irectly.** by th
11a1c 65 20 63 6f 6d 70 69 6c 65 72 2e 20 49 6e 73 74  e compiler. Inst
11a1d 65 61 64 2c 20 74 68 65 20 63 6f 6d 70 69 6c 65  ead, the compile
11a1e 72 20 6c 61 79 65 72 20 73 70 65 63 69 66 69 65  r layer specifie
11a1f 73 0a 2a 2a 20 61 6e 20 4f 50 5f 48 65 78 42 6c  s.** an OP_HexBl
11a20 6f 62 20 6f 70 63 6f 64 65 2c 20 77 69 74 68 20  ob opcode, with 
11a21 74 68 65 20 68 65 78 20 73 74 72 69 6e 67 20 72  the hex string r
11a22 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66  epresentation of
11a23 0a 2a 2a 20 74 68 65 20 62 6c 6f 62 20 61 73 20  .** the blob as 
11a24 50 33 2e 20 54 68 69 73 20 6f 70 63 6f 64 65 20  P3. This opcode 
11a25 69 73 20 74 72 61 6e 73 66 6f 72 6d 65 64 20 74  is transformed t
11a26 6f 20 61 6e 20 4f 50 5f 42 6c 6f 62 0a 2a 2a 20  o an OP_Blob.** 
11a27 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 69  the first time i
11a28 74 20 69 73 20 65 78 65 63 75 74 65 64 2e 0a 2a  t is executed..*
11a29 2f 0a 63 61 73 65 20 4f 50 5f 42 6c 6f 62 3a 20  /.case OP_Blob: 
11a2a 7b 0a 20 20 70 54 6f 73 2b 2b 3b 0a 20 20 61 73  {.  pTos++;.  as
11a2b 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 20 3c 20  sert( pOp->p1 < 
11a2c 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54  SQLITE_MAX_LENGT
11a2d 48 20 29 3b 20 2f 2a 20 44 75 65 20 74 6f 20 53  H ); /* Due to S
11a2e 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45  QLITE_MAX_SQL_LE
11a2f 4e 47 54 48 20 2a 2f 0a 20 20 73 71 6c 69 74 65  NGTH */.  sqlite
11a30 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70  3VdbeMemSetStr(p
11a31 54 6f 73 2c 20 70 4f 70 2d 3e 70 33 2c 20 70 4f  Tos, pOp->p3, pO
11a32 70 2d 3e 70 31 2c 20 30 2c 20 30 29 3b 0a 20 20  p->p1, 0, 0);.  
11a33 70 54 6f 73 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f  pTos->enc = enco
11a34 64 69 6e 67 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  ding;.  break;.}
11a35 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
11a36 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45  E_OMIT_BLOB_LITE
11a37 52 41 4c 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64  RAL */../* Opcod
11a38 65 3a 20 56 61 72 69 61 62 6c 65 20 50 31 20 2a  e: Variable P1 *
11a39 20 2a 0a 2a 2a 0a 2a 2a 20 50 75 73 68 20 74 68   *.**.** Push th
11a3a 65 20 76 61 6c 75 65 20 6f 66 20 76 61 72 69 61  e value of varia
11a3b 62 6c 65 20 50 31 20 6f 6e 74 6f 20 74 68 65 20  ble P1 onto the 
11a3c 73 74 61 63 6b 2e 20 20 41 20 76 61 72 69 61 62  stack.  A variab
11a3d 6c 65 20 69 73 0a 2a 2a 20 61 6e 20 75 6e 6b 6e  le is.** an unkn
11a3e 6f 77 6e 20 69 6e 20 74 68 65 20 6f 72 69 67 69  own in the origi
11a3f 6e 61 6c 20 53 51 4c 20 73 74 72 69 6e 67 20 61  nal SQL string a
11a40 73 20 68 61 6e 64 65 64 20 74 6f 20 73 71 6c 69  s handed to sqli
11a41 74 65 33 5f 63 6f 6d 70 69 6c 65 28 29 2e 0a 2a  te3_compile()..*
11a42 2a 20 41 6e 79 20 6f 63 63 75 72 61 6e 63 65 20  * Any occurance 
11a43 6f 66 20 74 68 65 20 27 3f 27 20 63 68 61 72 61  of the '?' chara
11a44 63 74 65 72 20 69 6e 20 74 68 65 20 6f 72 69 67  cter in the orig
11a45 69 6e 61 6c 20 53 51 4c 20 69 73 20 63 6f 6e 73  inal SQL is cons
11a46 69 64 65 72 65 64 0a 2a 2a 20 61 20 76 61 72 69  idered.** a vari
11a47 61 62 6c 65 2e 20 20 56 61 72 69 61 62 6c 65 73  able.  Variables
11a48 20 69 6e 20 74 68 65 20 53 51 4c 20 73 74 72 69   in the SQL stri
11a49 6e 67 20 61 72 65 20 6e 75 6d 62 65 72 20 66 72  ng are number fr
11a4a 6f 6d 20 6c 65 66 74 20 74 6f 0a 2a 2a 20 72 69  om left to.** ri
11a4b 67 68 74 20 62 65 67 69 6e 6e 69 6e 67 20 77 69  ght beginning wi
11a4c 74 68 20 31 2e 20 20 54 68 65 20 76 61 6c 75 65  th 1.  The value
11a4d 73 20 6f 66 20 76 61 72 69 61 62 6c 65 73 20 61  s of variables a
11a4e 72 65 20 73 65 74 20 75 73 69 6e 67 20 74 68 65  re set using the
11a4f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  .** sqlite3_bind
11a50 28 29 20 41 50 49 2e 0a 2a 2f 0a 63 61 73 65 20  () API..*/.case 
11a51 4f 50 5f 56 61 72 69 61 62 6c 65 3a 20 7b 0a 20  OP_Variable: {. 
11a52 20 69 6e 74 20 6a 20 3d 20 70 4f 70 2d 3e 70 31   int j = pOp->p1
11a53 20 2d 20 31 3b 0a 20 20 4d 65 6d 20 2a 70 56 61   - 1;.  Mem *pVa
11a54 72 3b 0a 20 20 61 73 73 65 72 74 28 20 6a 3e 3d  r;.  assert( j>=
11a55 30 20 26 26 20 6a 3c 70 2d 3e 6e 56 61 72 20 29  0 && j<p->nVar )
11a56 3b 0a 0a 20 20 70 56 61 72 20 3d 20 26 70 2d 3e  ;..  pVar = &p->
11a57 61 56 61 72 5b 6a 5d 3b 0a 20 20 69 66 28 20 73  aVar[j];.  if( s
11a58 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f  qlite3VdbeMemToo
11a59 42 69 67 28 70 56 61 72 29 20 29 7b 0a 20 20 20  Big(pVar) ){.   
11a5a 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20   goto too_big;. 
11a5b 20 7d 0a 20 20 70 54 6f 73 2b 2b 3b 0a 20 20 73   }.  pTos++;.  s
11a5c 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61  qlite3VdbeMemSha
11a5d 6c 6c 6f 77 43 6f 70 79 28 70 54 6f 73 2c 20 26  llowCopy(pTos, &
11a5e 70 2d 3e 61 56 61 72 5b 6a 5d 2c 20 4d 45 4d 5f  p->aVar[j], MEM_
11a5f 53 74 61 74 69 63 29 3b 0a 20 20 62 72 65 61 6b  Static);.  break
11a60 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
11a61 50 6f 70 20 50 31 20 2a 20 2a 0a 2a 2a 0a 2a 2a  Pop P1 * *.**.**
11a62 20 50 31 20 65 6c 65 6d 65 6e 74 73 20 61 72 65   P1 elements are
11a63 20 70 6f 70 70 65 64 20 6f 66 66 20 6f 66 20 74   popped off of t
11a64 68 65 20 74 6f 70 20 6f 66 20 73 74 61 63 6b 20  he top of stack 
11a65 61 6e 64 20 64 69 73 63 61 72 64 65 64 2e 0a 2a  and discarded..*
11a66 2f 0a 63 61 73 65 20 4f 50 5f 50 6f 70 3a 20 7b  /.case OP_Pop: {
11a67 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e              /* n
11a68 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 61 73 73 65  o-push */.  asse
11a69 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 29  rt( pOp->p1>=0 )
11a6a 3b 0a 20 20 70 6f 70 53 74 61 63 6b 28 26 70 54  ;.  popStack(&pT
11a6b 6f 73 2c 20 70 4f 70 2d 3e 70 31 29 3b 0a 20 20  os, pOp->p1);.  
11a6c 61 73 73 65 72 74 28 20 70 54 6f 73 3e 3d 26 70  assert( pTos>=&p
11a6d 2d 3e 61 53 74 61 63 6b 5b 2d 31 5d 20 29 3b 0a  ->aStack[-1] );.
11a6e 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
11a6f 70 63 6f 64 65 3a 20 44 75 70 20 50 31 20 50 32  pcode: Dup P1 P2
11a70 20 2a 0a 2a 2a 0a 2a 2a 20 41 20 63 6f 70 79 20   *.**.** A copy 
11a71 6f 66 20 74 68 65 20 50 31 2d 74 68 20 65 6c 65  of the P1-th ele
11a72 6d 65 6e 74 20 6f 66 20 74 68 65 20 73 74 61 63  ment of the stac
11a73 6b 20 0a 2a 2a 20 69 73 20 6d 61 64 65 20 61 6e  k .** is made an
11a74 64 20 70 75 73 68 65 64 20 6f 6e 74 6f 20 74 68  d pushed onto th
11a75 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61  e top of the sta
11a76 63 6b 2e 0a 2a 2a 20 54 68 65 20 74 6f 70 20 6f  ck..** The top o
11a77 66 20 74 68 65 20 73 74 61 63 6b 20 69 73 20 65  f the stack is e
11a78 6c 65 6d 65 6e 74 20 30 2e 20 20 53 6f 20 74 68  lement 0.  So th
11a79 65 0a 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e  e.** instruction
11a7a 20 22 44 75 70 20 30 20 30 20 30 22 20 77 69 6c   "Dup 0 0 0" wil
11a7b 6c 20 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66  l make a copy of
11a7c 20 74 68 65 0a 2a 2a 20 74 6f 70 20 6f 66 20 74   the.** top of t
11a7d 68 65 20 73 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20  he stack..**.** 
11a7e 49 66 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  If the content o
11a7f 66 20 74 68 65 20 50 31 2d 74 68 20 65 6c 65 6d  f the P1-th elem
11a80 65 6e 74 20 69 73 20 61 20 64 79 6e 61 6d 69 63  ent is a dynamic
11a81 61 6c 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65  ally.** allocate
11a82 64 20 73 74 72 69 6e 67 2c 20 74 68 65 6e 20 61  d string, then a
11a83 20 6e 65 77 20 63 6f 70 79 20 6f 66 20 74 68 61   new copy of tha
11a84 74 20 73 74 72 69 6e 67 0a 2a 2a 20 69 73 20 6d  t string.** is m
11a85 61 64 65 20 69 66 20 50 32 3d 3d 30 2e 20 20 49  ade if P2==0.  I
11a86 66 20 50 32 21 3d 30 2c 20 74 68 65 6e 20 6a 75  f P2!=0, then ju
11a87 73 74 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20  st a pointer.** 
11a88 74 6f 20 74 68 65 20 73 74 72 69 6e 67 20 69 73  to the string is
11a89 20 63 6f 70 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 41   copied..**.** A
11a8a 6c 73 6f 20 73 65 65 20 74 68 65 20 50 75 6c 6c  lso see the Pull
11a8b 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f   instruction..*/
11a8c 0a 63 61 73 65 20 4f 50 5f 44 75 70 3a 20 7b 0a  .case OP_Dup: {.
11a8d 20 20 4d 65 6d 20 2a 70 46 72 6f 6d 20 3d 20 26    Mem *pFrom = &
11a8e 70 54 6f 73 5b 2d 70 4f 70 2d 3e 70 31 5d 3b 0a  pTos[-pOp->p1];.
11a8f 20 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d 3c    assert( pFrom<
11a90 3d 70 54 6f 73 20 26 26 20 70 46 72 6f 6d 3e 3d  =pTos && pFrom>=
11a91 70 2d 3e 61 53 74 61 63 6b 20 29 3b 0a 20 20 70  p->aStack );.  p
11a92 54 6f 73 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33  Tos++;.  sqlite3
11a93 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f  VdbeMemShallowCo
11a94 70 79 28 70 54 6f 73 2c 20 70 46 72 6f 6d 2c 20  py(pTos, pFrom, 
11a95 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 69 66  MEM_Ephem);.  if
11a96 28 20 70 4f 70 2d 3e 70 32 20 29 7b 0a 20 20 20  ( pOp->p2 ){.   
11a97 20 44 65 65 70 68 65 6d 65 72 61 6c 69 7a 65 28   Deephemeralize(
11a98 70 54 6f 73 29 3b 0a 20 20 7d 0a 20 20 62 72 65  pTos);.  }.  bre
11a99 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
11a9a 3a 20 50 75 6c 6c 20 50 31 20 2a 20 2a 0a 2a 2a  : Pull P1 * *.**
11a9b 0a 2a 2a 20 54 68 65 20 50 31 2d 74 68 20 65 6c  .** The P1-th el
11a9c 65 6d 65 6e 74 20 69 73 20 72 65 6d 6f 76 65 64  ement is removed
11a9d 20 66 72 6f 6d 20 69 74 73 20 63 75 72 72 65 6e   from its curren
11a9e 74 20 6c 6f 63 61 74 69 6f 6e 20 6f 6e 20 0a 2a  t location on .*
11a9f 2a 20 74 68 65 20 73 74 61 63 6b 20 61 6e 64 20  * the stack and 
11aa0 70 75 73 68 65 64 20 62 61 63 6b 20 6f 6e 20 74  pushed back on t
11aa1 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b 2e  op of the stack.
11aa2 20 20 54 68 65 0a 2a 2a 20 74 6f 70 20 6f 66 20    The.** top of 
11aa3 74 68 65 20 73 74 61 63 6b 20 69 73 20 65 6c 65  the stack is ele
11aa4 6d 65 6e 74 20 30 2c 20 73 6f 20 22 50 75 6c 6c  ment 0, so "Pull
11aa5 20 30 20 30 20 30 22 20 69 73 0a 2a 2a 20 61 20   0 0 0" is.** a 
11aa6 6e 6f 2d 6f 70 2e 20 20 22 50 75 6c 6c 20 31 20  no-op.  "Pull 1 
11aa7 30 20 30 22 20 73 77 61 70 73 20 74 68 65 20 74  0 0" swaps the t
11aa8 6f 70 20 74 77 6f 20 65 6c 65 6d 65 6e 74 73 20  op two elements 
11aa9 6f 66 0a 2a 2a 20 74 68 65 20 73 74 61 63 6b 2e  of.** the stack.
11aaa 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20  .**.** See also 
11aab 74 68 65 20 44 75 70 20 69 6e 73 74 72 75 63 74  the Dup instruct
11aac 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ion..*/.case OP_
11aad 50 75 6c 6c 3a 20 7b 20 20 20 20 20 20 20 20 20  Pull: {         
11aae 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f     /* no-push */
11aaf 0a 20 20 4d 65 6d 20 2a 70 46 72 6f 6d 20 3d 20  .  Mem *pFrom = 
11ab0 26 70 54 6f 73 5b 2d 70 4f 70 2d 3e 70 31 5d 3b  &pTos[-pOp->p1];
11ab1 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d 65 6d 20  .  int i;.  Mem 
11ab2 74 73 3b 0a 0a 20 20 74 73 20 3d 20 2a 70 46 72  ts;..  ts = *pFr
11ab3 6f 6d 3b 0a 20 20 44 65 65 70 68 65 6d 65 72 61  om;.  Deephemera
11ab4 6c 69 7a 65 28 70 54 6f 73 29 3b 0a 20 20 66 6f  lize(pTos);.  fo
11ab5 72 28 69 3d 30 3b 20 69 3c 70 4f 70 2d 3e 70 31  r(i=0; i<pOp->p1
11ab6 3b 20 69 2b 2b 2c 20 70 46 72 6f 6d 2b 2b 29 7b  ; i++, pFrom++){
11ab7 0a 20 20 20 20 44 65 65 70 68 65 6d 65 72 61 6c  .    Deephemeral
11ab8 69 7a 65 28 26 70 46 72 6f 6d 5b 31 5d 29 3b 0a  ize(&pFrom[1]);.
11ab9 20 20 20 20 61 73 73 65 72 74 28 20 28 70 46 72      assert( (pFr
11aba 6f 6d 5b 31 5d 2e 66 6c 61 67 73 20 26 20 4d 45  om[1].flags & ME
11abb 4d 5f 45 70 68 65 6d 29 3d 3d 30 20 29 3b 0a 20  M_Ephem)==0 );. 
11abc 20 20 20 2a 70 46 72 6f 6d 20 3d 20 70 46 72 6f     *pFrom = pFro
11abd 6d 5b 31 5d 3b 0a 20 20 20 20 69 66 28 20 70 46  m[1];.    if( pF
11abe 72 6f 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  rom->flags & MEM
11abf 5f 53 68 6f 72 74 20 29 7b 0a 20 20 20 20 20 20  _Short ){.      
11ac0 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e 66  assert( pFrom->f
11ac1 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c  lags & (MEM_Str|
11ac2 4d 45 4d 5f 42 6c 6f 62 29 20 29 3b 0a 20 20 20  MEM_Blob) );.   
11ac3 20 20 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d     assert( pFrom
11ac4 2d 3e 7a 3d 3d 70 46 72 6f 6d 5b 31 5d 2e 7a 53  ->z==pFrom[1].zS
11ac5 68 6f 72 74 20 29 3b 0a 20 20 20 20 20 20 70 46  hort );.      pF
11ac6 72 6f 6d 2d 3e 7a 20 3d 20 70 46 72 6f 6d 2d 3e  rom->z = pFrom->
11ac7 7a 53 68 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20  zShort;.    }.  
11ac8 7d 0a 20 20 2a 70 54 6f 73 20 3d 20 74 73 3b 0a  }.  *pTos = ts;.
11ac9 20 20 69 66 28 20 70 54 6f 73 2d 3e 66 6c 61 67    if( pTos->flag
11aca 73 20 26 20 4d 45 4d 5f 53 68 6f 72 74 20 29 7b  s & MEM_Short ){
11acb 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 6f  .    assert( pTo
11acc 73 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f  s->flags & (MEM_
11acd 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 3b  Str|MEM_Blob) );
11ace 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 6f  .    assert( pTo
11acf 73 2d 3e 7a 3d 3d 70 54 6f 73 5b 2d 70 4f 70 2d  s->z==pTos[-pOp-
11ad0 3e 70 31 5d 2e 7a 53 68 6f 72 74 20 29 3b 0a 20  >p1].zShort );. 
11ad1 20 20 20 70 54 6f 73 2d 3e 7a 20 3d 20 70 54 6f     pTos->z = pTo
11ad2 73 2d 3e 7a 53 68 6f 72 74 3b 0a 20 20 7d 0a 20  s->zShort;.  }. 
11ad3 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
11ad4 63 6f 64 65 3a 20 50 75 73 68 20 50 31 20 2a 20  code: Push P1 * 
11ad5 2a 0a 2a 2a 0a 2a 2a 20 4f 76 65 72 77 72 69 74  *.**.** Overwrit
11ad6 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  e the value of t
11ad7 68 65 20 50 31 2d 74 68 20 65 6c 65 6d 65 6e 74  he P1-th element
11ad8 20 64 6f 77 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20   down on the.** 
11ad9 73 74 61 63 6b 20 28 50 31 3d 3d 30 20 69 73 20  stack (P1==0 is 
11ada 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73  the top of the s
11adb 74 61 63 6b 29 20 77 69 74 68 20 74 68 65 20 76  tack) with the v
11adc 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20 74  alue.** of the t
11add 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b 2e  op of the stack.
11ade 20 20 54 68 65 6e 20 70 6f 70 20 74 68 65 20 74    Then pop the t
11adf 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b 2e  op of the stack.
11ae0 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 75 73 68  .*/.case OP_Push
11ae1 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f  : {            /
11ae2 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 4d  * no-push */.  M
11ae3 65 6d 20 2a 70 54 6f 20 3d 20 26 70 54 6f 73 5b  em *pTo = &pTos[
11ae4 2d 70 4f 70 2d 3e 70 31 5d 3b 0a 0a 20 20 61 73  -pOp->p1];..  as
11ae5 73 65 72 74 28 20 70 54 6f 3e 3d 70 2d 3e 61 53  sert( pTo>=p->aS
11ae6 74 61 63 6b 20 29 3b 0a 20 20 73 71 6c 69 74 65  tack );.  sqlite
11ae7 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 70 54 6f  3VdbeMemMove(pTo
11ae8 2c 20 70 54 6f 73 29 3b 0a 20 20 70 54 6f 73 2d  , pTos);.  pTos-
11ae9 2d 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  -;.  break;.}../
11aea 2a 20 4f 70 63 6f 64 65 3a 20 43 61 6c 6c 62 61  * Opcode: Callba
11aeb 63 6b 20 50 31 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  ck P1 * *.**.** 
11aec 54 68 65 20 74 6f 70 20 50 31 20 76 61 6c 75 65  The top P1 value
11aed 73 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 20 72  s on the stack r
11aee 65 70 72 65 73 65 6e 74 20 61 20 73 69 6e 67 6c  epresent a singl
11aef 65 20 72 65 73 75 6c 74 20 72 6f 77 20 66 72 6f  e result row fro
11af0 6d 0a 2a 2a 20 61 20 71 75 65 72 79 2e 20 20 54  m.** a query.  T
11af1 68 69 73 20 6f 70 63 6f 64 65 20 63 61 75 73 65  his opcode cause
11af2 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74  s the sqlite3_st
11af3 65 70 28 29 20 63 61 6c 6c 20 74 6f 20 74 65 72  ep() call to ter
11af4 6d 69 6e 61 74 65 0a 2a 2a 20 77 69 74 68 20 61  minate.** with a
11af5 6e 20 53 51 4c 49 54 45 5f 52 4f 57 20 72 65 74  n SQLITE_ROW ret
11af6 75 72 6e 20 63 6f 64 65 20 61 6e 64 20 69 74 20  urn code and it 
11af7 73 65 74 73 20 75 70 20 74 68 65 20 73 71 6c 69  sets up the sqli
11af8 74 65 33 5f 73 74 6d 74 0a 2a 2a 20 73 74 72 75  te3_stmt.** stru
11af9 63 74 75 72 65 20 74 6f 20 70 72 6f 76 69 64 65  cture to provide
11afa 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 74   access to the t
11afb 6f 70 20 50 31 20 76 61 6c 75 65 73 20 61 73 20  op P1 values as 
11afc 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 72 6f  the result.** ro
11afd 77 2e 20 20 57 68 65 6e 20 74 68 65 20 73 71 6c  w.  When the sql
11afe 69 74 65 33 5f 73 74 65 70 28 29 20 66 75 6e 63  ite3_step() func
11aff 74 69 6f 6e 20 69 73 20 72 75 6e 20 61 67 61 69  tion is run agai
11b00 6e 2c 20 74 68 65 20 74 6f 70 20 50 31 0a 2a 2a  n, the top P1.**
11b01 20 76 61 6c 75 65 73 20 77 69 6c 6c 20 62 65 20   values will be 
11b02 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 70 6f  automatically po
11b03 70 70 65 64 20 66 72 6f 6d 20 74 68 65 20 73 74  pped from the st
11b04 61 63 6b 20 62 65 66 6f 72 65 20 74 68 65 20 6e  ack before the n
11b05 65 78 74 0a 2a 2a 20 69 6e 73 74 72 75 63 74 69  ext.** instructi
11b06 6f 6e 20 65 78 65 63 75 74 65 73 2e 0a 2a 2f 0a  on executes..*/.
11b07 63 61 73 65 20 4f 50 5f 43 61 6c 6c 62 61 63 6b  case OP_Callback
11b08 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f  : {            /
11b09 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 4d  * no-push */.  M
11b0a 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 4d 65 6d 20  em *pMem;.  Mem 
11b0b 2a 70 46 69 72 73 74 43 6f 6c 75 6d 6e 3b 0a 20  *pFirstColumn;. 
11b0c 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 73   assert( p->nRes
11b0d 43 6f 6c 75 6d 6e 3d 3d 70 4f 70 2d 3e 70 31 20  Column==pOp->p1 
11b0e 29 3b 0a 0a 20 20 2f 2a 20 44 61 74 61 20 69 6e  );..  /* Data in
11b0f 20 74 68 65 20 70 61 67 65 72 20 6d 69 67 68 74   the pager might
11b10 20 62 65 20 6d 6f 76 65 64 20 6f 72 20 63 68 61   be moved or cha
11b11 6e 67 65 64 20 6f 75 74 20 66 72 6f 6d 20 75 6e  nged out from un
11b12 64 65 72 20 75 73 0a 20 20 2a 2a 20 69 6e 20 62  der us.  ** in b
11b13 65 74 77 65 65 6e 20 74 68 65 20 72 65 74 75 72  etween the retur
11b14 6e 20 66 72 6f 6d 20 74 68 69 73 20 73 71 6c 69  n from this sqli
11b15 74 65 33 5f 73 74 65 70 28 29 20 63 61 6c 6c 20  te3_step() call 
11b16 61 6e 64 20 74 68 65 0a 20 20 2a 2a 20 6e 65 78  and the.  ** nex
11b17 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  t call to sqlite
11b18 33 5f 73 74 65 70 28 29 2e 20 20 53 6f 20 64 65  3_step().  So de
11b19 65 70 68 65 72 6d 65 72 61 6c 69 7a 65 20 65 76  ephermeralize ev
11b1a 65 72 79 74 68 69 6e 67 20 6f 6e 20 0a 20 20 2a  erything on .  *
11b1b 2a 20 74 68 65 20 73 74 61 63 6b 2e 20 20 4e 6f  * the stack.  No
11b1c 74 65 20 74 68 61 74 20 65 70 68 65 6d 65 72 61  te that ephemera
11b1d 6c 20 64 61 74 61 20 69 73 20 6e 65 76 65 72 20  l data is never 
11b1e 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79  stored in memory
11b1f 20 0a 20 20 2a 2a 20 63 65 6c 6c 73 20 73 6f 20   .  ** cells so 
11b20 77 65 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 74  we do not have t
11b21 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 20 74 68  o worry about th
11b22 65 6d 2e 0a 20 20 2a 2f 0a 20 20 70 46 69 72 73  em..  */.  pFirs
11b23 74 43 6f 6c 75 6d 6e 20 3d 20 26 70 54 6f 73 5b  tColumn = &pTos[
11b24 30 2d 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 66 6f  0-pOp->p1];.  fo
11b25 72 28 70 4d 65 6d 20 3d 20 70 2d 3e 61 53 74 61  r(pMem = p->aSta
11b26 63 6b 3b 20 70 4d 65 6d 3c 70 46 69 72 73 74 43  ck; pMem<pFirstC
11b27 6f 6c 75 6d 6e 3b 20 70 4d 65 6d 2b 2b 29 7b 0a  olumn; pMem++){.
11b28 20 20 20 20 44 65 65 70 68 65 6d 65 72 61 6c 69      Deephemerali
11b29 7a 65 28 70 4d 65 6d 29 3b 0a 20 20 7d 0a 0a 20  ze(pMem);.  }.. 
11b2a 20 2f 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 61   /* Invalidate a
11b2b 6c 6c 20 65 70 68 65 6d 65 72 61 6c 20 63 75 72  ll ephemeral cur
11b2c 73 6f 72 20 72 6f 77 20 63 61 63 68 65 73 20 2a  sor row caches *
11b2d 2f 0a 20 20 70 2d 3e 63 61 63 68 65 43 74 72 20  /.  p->cacheCtr 
11b2e 3d 20 28 70 2d 3e 63 61 63 68 65 43 74 72 20 2b  = (p->cacheCtr +
11b2f 20 32 29 7c 31 3b 0a 0a 20 20 2f 2a 20 4d 61 6b   2)|1;..  /* Mak
11b30 65 20 73 75 72 65 20 74 68 65 20 72 65 73 75 6c  e sure the resul
11b31 74 73 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e  ts of the curren
11b32 74 20 72 6f 77 20 61 72 65 20 5c 30 30 30 20 74  t row are \000 t
11b33 65 72 6d 69 6e 61 74 65 64 0a 20 20 2a 2a 20 61  erminated.  ** a
11b34 6e 64 20 68 61 76 65 20 61 6e 20 61 73 73 69 67  nd have an assig
11b35 6e 65 64 20 74 79 70 65 2e 20 20 54 68 65 20 72  ned type.  The r
11b36 65 73 75 6c 74 73 20 61 72 65 20 64 65 65 70 68  esults are deeph
11b37 65 6d 65 72 61 6c 69 7a 65 64 20 61 73 0a 20 20  emeralized as.  
11b38 2a 2a 20 61 73 20 73 69 64 65 20 65 66 66 65 63  ** as side effec
11b39 74 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 3b 20  t..  */.  for(; 
11b3a 70 4d 65 6d 3c 3d 70 54 6f 73 3b 20 70 4d 65 6d  pMem<=pTos; pMem
11b3b 2b 2b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ++ ){.    sqlite
11b3c 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69  3VdbeMemNulTermi
11b3d 6e 61 74 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20  nate(pMem);.    
11b3e 73 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 70 4d  storeTypeInfo(pM
11b3f 65 6d 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20  em, encoding);. 
11b40 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 75 70 20   }..  /* Set up 
11b41 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 73 74  the statement st
11b42 72 75 63 74 75 72 65 20 73 6f 20 74 68 61 74 20  ructure so that 
11b43 69 74 20 77 69 6c 6c 20 70 6f 70 20 74 68 65 20  it will pop the 
11b44 63 75 72 72 65 6e 74 0a 20 20 2a 2a 20 72 65 73  current.  ** res
11b45 75 6c 74 73 20 66 72 6f 6d 20 74 68 65 20 73 74  ults from the st
11b46 61 63 6b 20 77 68 65 6e 20 74 68 65 20 73 74 61  ack when the sta
11b47 74 65 6d 65 6e 74 20 72 65 74 75 72 6e 73 2e 0a  tement returns..
11b48 20 20 2a 2f 0a 20 20 70 2d 3e 72 65 73 4f 6e 53    */.  p->resOnS
11b49 74 61 63 6b 20 3d 20 31 3b 0a 20 20 70 2d 3e 6e  tack = 1;.  p->n
11b4a 43 61 6c 6c 62 61 63 6b 2b 2b 3b 0a 20 20 70 2d  Callback++;.  p-
11b4b 3e 70 6f 70 53 74 61 63 6b 20 3d 20 70 4f 70 2d  >popStack = pOp-
11b4c 3e 70 31 3b 0a 20 20 70 2d 3e 70 63 20 3d 20 70  >p1;.  p->pc = p
11b4d 63 20 2b 20 31 3b 0a 20 20 70 2d 3e 70 54 6f 73  c + 1;.  p->pTos
11b4e 20 3d 20 70 54 6f 73 3b 0a 20 20 72 65 74 75 72   = pTos;.  retur
11b4f 6e 20 53 51 4c 49 54 45 5f 52 4f 57 3b 0a 7d 0a  n SQLITE_ROW;.}.
11b50 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6e 63  ./* Opcode: Conc
11b51 61 74 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a  at P1 P2 *.**.**
11b52 20 4c 6f 6f 6b 20 61 74 20 74 68 65 20 66 69 72   Look at the fir
11b53 73 74 20 50 31 2b 32 20 65 6c 65 6d 65 6e 74 73  st P1+2 elements
11b54 20 6f 66 20 74 68 65 20 73 74 61 63 6b 2e 20 20   of the stack.  
11b55 41 70 70 65 6e 64 20 74 68 65 6d 20 61 6c 6c 20  Append them all 
11b56 0a 2a 2a 20 74 6f 67 65 74 68 65 72 20 77 69 74  .** together wit
11b57 68 20 74 68 65 20 6c 6f 77 65 73 74 20 65 6c 65  h the lowest ele
11b58 6d 65 6e 74 20 66 69 72 73 74 2e 20 20 54 68 65  ment first.  The
11b59 20 6f 72 69 67 69 6e 61 6c 20 50 31 2b 32 20 65   original P1+2 e
11b5a 6c 65 6d 65 6e 74 73 0a 2a 2a 20 61 72 65 20 70  lements.** are p
11b5b 6f 70 70 65 64 20 66 72 6f 6d 20 74 68 65 20 73  opped from the s
11b5c 74 61 63 6b 20 69 66 20 50 32 3d 3d 30 20 61 6e  tack if P2==0 an
11b5d 64 20 72 65 74 61 69 6e 65 64 20 69 66 20 50 32  d retained if P2
11b5e 3d 3d 31 2e 20 20 49 66 0a 2a 2a 20 61 6e 79 20  ==1.  If.** any 
11b5f 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 73  element of the s
11b60 74 61 63 6b 20 69 73 20 4e 55 4c 4c 2c 20 74 68  tack is NULL, th
11b61 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  en the result is
11b62 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 57 68 65   NULL..**.** Whe
11b63 6e 20 50 31 3d 3d 31 2c 20 74 68 69 73 20 72 6f  n P1==1, this ro
11b64 75 74 69 6e 65 20 6d 61 6b 65 73 20 61 20 63 6f  utine makes a co
11b65 70 79 20 6f 66 20 74 68 65 20 74 6f 70 20 73 74  py of the top st
11b66 61 63 6b 20 65 6c 65 6d 65 6e 74 0a 2a 2a 20 69  ack element.** i
11b67 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69  nto memory obtai
11b68 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d  ned from sqliteM
11b69 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 63 61 73 65  alloc()..*/.case
11b6a 20 4f 50 5f 43 6f 6e 63 61 74 3a 20 7b 20 20 20   OP_Concat: {   
11b6b 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20          /* same 
11b6c 61 73 20 54 4b 5f 43 4f 4e 43 41 54 20 2a 2f 0a  as TK_CONCAT */.
11b6d 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20    char *zNew;.  
11b6e 69 36 34 20 6e 42 79 74 65 3b 0a 20 20 69 6e 74  i64 nByte;.  int
11b6f 20 6e 46 69 65 6c 64 3b 0a 20 20 69 6e 74 20 69   nField;.  int i
11b70 2c 20 6a 3b 0a 20 20 4d 65 6d 20 2a 70 54 65 72  , j;.  Mem *pTer
11b71 6d 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 70 20 74 68  m;..  /* Loop th
11b72 72 6f 75 67 68 20 74 68 65 20 73 74 61 63 6b 20  rough the stack 
11b73 65 6c 65 6d 65 6e 74 73 20 74 6f 20 73 65 65 20  elements to see 
11b74 68 6f 77 20 6c 6f 6e 67 20 74 68 65 20 72 65 73  how long the res
11b75 75 6c 74 20 77 69 6c 6c 20 62 65 2e 20 2a 2f 0a  ult will be. */.
11b76 20 20 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e    nField = pOp->
11b77 70 31 20 2b 20 32 3b 0a 20 20 70 54 65 72 6d 20  p1 + 2;.  pTerm 
11b78 3d 20 26 70 54 6f 73 5b 31 2d 6e 46 69 65 6c 64  = &pTos[1-nField
11b79 5d 3b 0a 20 20 6e 42 79 74 65 20 3d 20 30 3b 0a  ];.  nByte = 0;.
11b7a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 46 69    for(i=0; i<nFi
11b7b 65 6c 64 3b 20 69 2b 2b 2c 20 70 54 65 72 6d 2b  eld; i++, pTerm+
11b7c 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  +){.    assert( 
11b7d 70 4f 70 2d 3e 70 32 3d 3d 30 20 7c 7c 20 28 70  pOp->p2==0 || (p
11b7e 54 65 72 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  Term->flags&MEM_
11b7f 53 74 72 29 20 29 3b 0a 20 20 20 20 69 66 28 20  Str) );.    if( 
11b80 70 54 65 72 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d  pTerm->flags&MEM
11b81 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 6e  _Null ){.      n
11b82 42 79 74 65 20 3d 20 2d 31 3b 0a 20 20 20 20 20  Byte = -1;.     
11b83 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
11b84 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 54 65    ExpandBlob(pTe
11b85 72 6d 29 3b 0a 20 20 20 20 53 74 72 69 6e 67 69  rm);.    Stringi
11b86 66 79 28 70 54 65 72 6d 2c 20 65 6e 63 6f 64 69  fy(pTerm, encodi
11b87 6e 67 29 3b 0a 20 20 20 20 6e 42 79 74 65 20 2b  ng);.    nByte +
11b88 3d 20 70 54 65 72 6d 2d 3e 6e 3b 0a 20 20 7d 0a  = pTerm->n;.  }.
11b89 0a 20 20 69 66 28 20 6e 42 79 74 65 3c 30 20 29  .  if( nByte<0 )
11b8a 7b 0a 20 20 20 20 2f 2a 20 49 66 20 6e 42 79 74  {.    /* If nByt
11b8b 65 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 7a  e is less than z
11b8c 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 72 65 20  ero, then there 
11b8d 69 73 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65 20  is a NULL value 
11b8e 6f 6e 20 74 68 65 20 73 74 61 63 6b 2e 0a 20 20  on the stack..  
11b8f 20 20 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73    ** In this cas
11b90 65 20 6a 75 73 74 20 70 6f 70 20 74 68 65 20 76  e just pop the v
11b91 61 6c 75 65 73 20 6f 66 66 20 74 68 65 20 73 74  alues off the st
11b92 61 63 6b 20 28 69 66 20 72 65 71 75 69 72 65 64  ack (if required
11b93 29 20 61 6e 64 0a 20 20 20 20 2a 2a 20 70 75 73  ) and.    ** pus
11b94 68 20 6f 6e 20 61 20 4e 55 4c 4c 2e 0a 20 20 20  h on a NULL..   
11b95 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4f 70 2d   */.    if( pOp-
11b96 3e 70 32 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  >p2==0 ){.      
11b97 70 6f 70 53 74 61 63 6b 28 26 70 54 6f 73 2c 20  popStack(&pTos, 
11b98 6e 46 69 65 6c 64 29 3b 0a 20 20 20 20 7d 0a 20  nField);.    }. 
11b99 20 20 20 70 54 6f 73 2b 2b 3b 0a 20 20 20 20 70     pTos++;.    p
11b9a 54 6f 73 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Tos->flags = MEM
11b9b 5f 4e 75 6c 6c 3b 0a 20 20 7d 65 6c 73 65 7b 0a  _Null;.  }else{.
11b9c 20 20 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65      /* Otherwise
11b9d 20 6d 61 6c 6c 6f 63 28 29 20 73 70 61 63 65 20   malloc() space 
11b9e 66 6f 72 20 74 68 65 20 72 65 73 75 6c 74 20 61  for the result a
11b9f 6e 64 20 63 6f 6e 63 61 74 65 6e 61 74 65 20 61  nd concatenate a
11ba0 6c 6c 20 74 68 65 0a 20 20 20 20 2a 2a 20 73 74  ll the.    ** st
11ba1 61 63 6b 20 76 61 6c 75 65 73 2e 0a 20 20 20 20  ack values..    
11ba2 2a 2f 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65  */.    if( nByte
11ba3 2b 32 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45  +2>SQLITE_MAX_LE
11ba4 4e 47 54 48 20 29 7b 0a 20 20 20 20 20 20 67 6f  NGTH ){.      go
11ba5 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20  to too_big;.    
11ba6 7d 0a 20 20 20 20 7a 4e 65 77 20 3d 20 73 71 6c  }.    zNew = sql
11ba7 69 74 65 4d 61 6c 6c 6f 63 52 61 77 28 20 6e 42  iteMallocRaw( nB
11ba8 79 74 65 2b 32 20 29 3b 0a 20 20 20 20 69 66 28  yte+2 );.    if(
11ba9 20 7a 4e 65 77 3d 3d 30 20 29 20 67 6f 74 6f 20   zNew==0 ) goto 
11baa 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 6a 20 3d 20  no_mem;.    j = 
11bab 30 3b 0a 20 20 20 20 70 54 65 72 6d 20 3d 20 26  0;.    pTerm = &
11bac 70 54 6f 73 5b 31 2d 6e 46 69 65 6c 64 5d 3b 0a  pTos[1-nField];.
11bad 20 20 20 20 66 6f 72 28 69 3d 6a 3d 30 3b 20 69      for(i=j=0; i
11bae 3c 6e 46 69 65 6c 64 3b 20 69 2b 2b 2c 20 70 54  <nField; i++, pT
11baf 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e  erm++){.      in
11bb0 74 20 6e 20 3d 20 70 54 65 72 6d 2d 3e 6e 3b 0a  t n = pTerm->n;.
11bb1 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54        assert( pT
11bb2 65 72 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45  erm->flags & (ME
11bb3 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20  M_Str|MEM_Blob) 
11bb4 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  );.      memcpy(
11bb5 26 7a 4e 65 77 5b 6a 5d 2c 20 70 54 65 72 6d 2d  &zNew[j], pTerm-
11bb6 3e 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20 6a 20  >z, n);.      j 
11bb7 2b 3d 20 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20  += n;.    }.    
11bb8 7a 4e 65 77 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20  zNew[j] = 0;.   
11bb9 20 7a 4e 65 77 5b 6a 2b 31 5d 20 3d 20 30 3b 0a   zNew[j+1] = 0;.
11bba 20 20 20 20 61 73 73 65 72 74 28 20 6a 3d 3d 6e      assert( j==n
11bbb 42 79 74 65 20 29 3b 0a 0a 20 20 20 20 69 66 28  Byte );..    if(
11bbc 20 70 4f 70 2d 3e 70 32 3d 3d 30 20 29 7b 0a 20   pOp->p2==0 ){. 
11bbd 20 20 20 20 20 70 6f 70 53 74 61 63 6b 28 26 70       popStack(&p
11bbe 54 6f 73 2c 20 6e 46 69 65 6c 64 29 3b 0a 20 20  Tos, nField);.  
11bbf 20 20 7d 0a 20 20 20 20 70 54 6f 73 2b 2b 3b 0a    }.    pTos++;.
11bc0 20 20 20 20 70 54 6f 73 2d 3e 6e 20 3d 20 6a 3b      pTos->n = j;
11bc1 0a 20 20 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73  .    pTos->flags
11bc2 20 3d 20 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 44   = MEM_Str|MEM_D
11bc3 79 6e 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20  yn|MEM_Term;.   
11bc4 20 70 54 6f 73 2d 3e 78 44 65 6c 20 3d 20 30 3b   pTos->xDel = 0;
11bc5 0a 20 20 20 20 70 54 6f 73 2d 3e 65 6e 63 20 3d  .    pTos->enc =
11bc6 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 20 20 70   encoding;.    p
11bc7 54 6f 73 2d 3e 7a 20 3d 20 7a 4e 65 77 3b 0a 20  Tos->z = zNew;. 
11bc8 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
11bc9 2a 20 4f 70 63 6f 64 65 3a 20 41 64 64 20 2a 20  * Opcode: Add * 
11bca 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 6f 70 20 74 68  * *.**.** Pop th
11bcb 65 20 74 6f 70 20 74 77 6f 20 65 6c 65 6d 65 6e  e top two elemen
11bcc 74 73 20 66 72 6f 6d 20 74 68 65 20 73 74 61 63  ts from the stac
11bcd 6b 2c 20 61 64 64 20 74 68 65 6d 20 74 6f 67 65  k, add them toge
11bce 74 68 65 72 2c 0a 2a 2a 20 61 6e 64 20 70 75 73  ther,.** and pus
11bcf 68 20 74 68 65 20 72 65 73 75 6c 74 20 62 61 63  h the result bac
11bd0 6b 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b  k onto the stack
11bd1 2e 20 20 49 66 20 65 69 74 68 65 72 20 65 6c 65  .  If either ele
11bd2 6d 65 6e 74 0a 2a 2a 20 69 73 20 61 20 73 74 72  ment.** is a str
11bd3 69 6e 67 20 74 68 65 6e 20 69 74 20 69 73 20 63  ing then it is c
11bd4 6f 6e 76 65 72 74 65 64 20 74 6f 20 61 20 64 6f  onverted to a do
11bd5 75 62 6c 65 20 75 73 69 6e 67 20 74 68 65 20 61  uble using the a
11bd6 74 6f 66 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f  tof().** functio
11bd7 6e 20 62 65 66 6f 72 65 20 74 68 65 20 61 64 64  n before the add
11bd8 69 74 69 6f 6e 2e 0a 2a 2a 20 49 66 20 65 69 74  ition..** If eit
11bd9 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 4e  her operand is N
11bda 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20  ULL, the result 
11bdb 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f  is NULL..*/./* O
11bdc 70 63 6f 64 65 3a 20 4d 75 6c 74 69 70 6c 79 20  pcode: Multiply 
11bdd 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 6f 70 20  * * *.**.** Pop 
11bde 74 68 65 20 74 6f 70 20 74 77 6f 20 65 6c 65 6d  the top two elem
11bdf 65 6e 74 73 20 66 72 6f 6d 20 74 68 65 20 73 74  ents from the st
11be0 61 63 6b 2c 20 6d 75 6c 74 69 70 6c 79 20 74 68  ack, multiply th
11be1 65 6d 20 74 6f 67 65 74 68 65 72 2c 0a 2a 2a 20  em together,.** 
11be2 61 6e 64 20 70 75 73 68 20 74 68 65 20 72 65 73  and push the res
11be3 75 6c 74 20 62 61 63 6b 20 6f 6e 74 6f 20 74 68  ult back onto th
11be4 65 20 73 74 61 63 6b 2e 20 20 49 66 20 65 69 74  e stack.  If eit
11be5 68 65 72 20 65 6c 65 6d 65 6e 74 0a 2a 2a 20 69  her element.** i
11be6 73 20 61 20 73 74 72 69 6e 67 20 74 68 65 6e 20  s a string then 
11be7 69 74 20 69 73 20 63 6f 6e 76 65 72 74 65 64 20  it is converted 
11be8 74 6f 20 61 20 64 6f 75 62 6c 65 20 75 73 69 6e  to a double usin
11be9 67 20 74 68 65 20 61 74 6f 66 28 29 0a 2a 2a 20  g the atof().** 
11bea 66 75 6e 63 74 69 6f 6e 20 62 65 66 6f 72 65 20  function before 
11beb 74 68 65 20 6d 75 6c 74 69 70 6c 69 63 61 74 69  the multiplicati
11bec 6f 6e 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72  on..** If either
11bed 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c   operand is NULL
11bee 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  , the result is 
11bef 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f  NULL..*/./* Opco
11bf0 64 65 3a 20 53 75 62 74 72 61 63 74 20 2a 20 2a  de: Subtract * *
11bf1 20 2a 0a 2a 2a 0a 2a 2a 20 50 6f 70 20 74 68 65   *.**.** Pop the
11bf2 20 74 6f 70 20 74 77 6f 20 65 6c 65 6d 65 6e 74   top two element
11bf3 73 20 66 72 6f 6d 20 74 68 65 20 73 74 61 63 6b  s from the stack
11bf4 2c 20 73 75 62 74 72 61 63 74 20 74 68 65 0a 2a  , subtract the.*
11bf5 2a 20 66 69 72 73 74 20 28 77 68 61 74 20 77 61  * first (what wa
11bf6 73 20 6f 6e 20 74 6f 70 20 6f 66 20 74 68 65 20  s on top of the 
11bf7 73 74 61 63 6b 29 20 66 72 6f 6d 20 74 68 65 20  stack) from the 
11bf8 73 65 63 6f 6e 64 20 28 74 68 65 0a 2a 2a 20 6e  second (the.** n
11bf9 65 78 74 20 6f 6e 20 73 74 61 63 6b 29 0a 2a 2a  ext on stack).**
11bfa 20 61 6e 64 20 70 75 73 68 20 74 68 65 20 72 65   and push the re
11bfb 73 75 6c 74 20 62 61 63 6b 20 6f 6e 74 6f 20 74  sult back onto t
11bfc 68 65 20 73 74 61 63 6b 2e 20 20 49 66 20 65 69  he stack.  If ei
11bfd 74 68 65 72 20 65 6c 65 6d 65 6e 74 0a 2a 2a 20  ther element.** 
11bfe 69 73 20 61 20 73 74 72 69 6e 67 20 74 68 65 6e  is a string then
11bff 20 69 74 20 69 73 20 63 6f 6e 76 65 72 74 65 64   it is converted
11c00 20 74 6f 20 61 20 64 6f 75 62 6c 65 20 75 73 69   to a double usi
11c01 6e 67 20 74 68 65 20 61 74 6f 66 28 29 0a 2a 2a  ng the atof().**
11c02 20 66 75 6e 63 74 69 6f 6e 20 62 65 66 6f 72 65   function before
11c03 20 74 68 65 20 73 75 62 74 72 61 63 74 69 6f 6e   the subtraction
11c04 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 6f  ..** If either o
11c05 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 2c 20  perand is NULL, 
11c06 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55  the result is NU
11c07 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65  LL..*/./* Opcode
11c08 3a 20 44 69 76 69 64 65 20 2a 20 2a 20 2a 0a 2a  : Divide * * *.*
11c09 2a 0a 2a 2a 20 50 6f 70 20 74 68 65 20 74 6f 70  *.** Pop the top
11c0a 20 74 77 6f 20 65 6c 65 6d 65 6e 74 73 20 66 72   two elements fr
11c0b 6f 6d 20 74 68 65 20 73 74 61 63 6b 2c 20 64 69  om the stack, di
11c0c 76 69 64 65 20 74 68 65 0a 2a 2a 20 66 69 72 73  vide the.** firs
11c0d 74 20 28 77 68 61 74 20 77 61 73 20 6f 6e 20 74  t (what was on t
11c0e 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b 29  op of the stack)
11c0f 20 66 72 6f 6d 20 74 68 65 20 73 65 63 6f 6e 64   from the second
11c10 20 28 74 68 65 0a 2a 2a 20 6e 65 78 74 20 6f 6e   (the.** next on
11c11 20 73 74 61 63 6b 29 0a 2a 2a 20 61 6e 64 20 70   stack).** and p
11c12 75 73 68 20 74 68 65 20 72 65 73 75 6c 74 20 62  ush the result b
11c13 61 63 6b 20 6f 6e 74 6f 20 74 68 65 20 73 74 61  ack onto the sta
11c14 63 6b 2e 20 20 49 66 20 65 69 74 68 65 72 20 65  ck.  If either e
11c15 6c 65 6d 65 6e 74 0a 2a 2a 20 69 73 20 61 20 73  lement.** is a s
11c16 74 72 69 6e 67 20 74 68 65 6e 20 69 74 20 69 73  tring then it is
11c17 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 61 20   converted to a 
11c18 64 6f 75 62 6c 65 20 75 73 69 6e 67 20 74 68 65  double using the
11c19 20 61 74 6f 66 28 29 0a 2a 2a 20 66 75 6e 63 74   atof().** funct
11c1a 69 6f 6e 20 62 65 66 6f 72 65 20 74 68 65 20 64  ion before the d
11c1b 69 76 69 73 69 6f 6e 2e 20 20 44 69 76 69 73 69  ivision.  Divisi
11c1c 6f 6e 20 62 79 20 7a 65 72 6f 20 72 65 74 75 72  on by zero retur
11c1d 6e 73 20 4e 55 4c 4c 2e 0a 2a 2a 20 49 66 20 65  ns NULL..** If e
11c1e 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73  ither operand is
11c1f 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c   NULL, the resul
11c20 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a  t is NULL..*/./*
11c21 20 4f 70 63 6f 64 65 3a 20 52 65 6d 61 69 6e 64   Opcode: Remaind
11c22 65 72 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50  er * * *.**.** P
11c23 6f 70 20 74 68 65 20 74 6f 70 20 74 77 6f 20 65  op the top two e
11c24 6c 65 6d 65 6e 74 73 20 66 72 6f 6d 20 74 68 65  lements from the
11c25 20 73 74 61 63 6b 2c 20 64 69 76 69 64 65 20 74   stack, divide t
11c26 68 65 0a 2a 2a 20 66 69 72 73 74 20 28 77 68 61  he.** first (wha
11c27 74 20 77 61 73 20 6f 6e 20 74 6f 70 20 6f 66 20  t was on top of 
11c28 74 68 65 20 73 74 61 63 6b 29 20 66 72 6f 6d 20  the stack) from 
11c29 74 68 65 20 73 65 63 6f 6e 64 20 28 74 68 65 0a  the second (the.
11c2a 2a 2a 20 6e 65 78 74 20 6f 6e 20 73 74 61 63 6b  ** next on stack
11c2b 29 0a 2a 2a 20 61 6e 64 20 70 75 73 68 20 74 68  ).** and push th
11c2c 65 20 72 65 6d 61 69 6e 64 65 72 20 61 66 74 65  e remainder afte
11c2d 72 20 64 69 76 69 73 69 6f 6e 20 6f 6e 74 6f 20  r division onto 
11c2e 74 68 65 20 73 74 61 63 6b 2e 20 20 49 66 20 65  the stack.  If e
11c2f 69 74 68 65 72 20 65 6c 65 6d 65 6e 74 0a 2a 2a  ither element.**
11c30 20 69 73 20 61 20 73 74 72 69 6e 67 20 74 68 65   is a string the
11c31 6e 20 69 74 20 69 73 20 63 6f 6e 76 65 72 74 65  n it is converte
11c32 64 20 74 6f 20 61 20 64 6f 75 62 6c 65 20 75 73  d to a double us
11c33 69 6e 67 20 74 68 65 20 61 74 6f 66 28 29 0a 2a  ing the atof().*
11c34 2a 20 66 75 6e 63 74 69 6f 6e 20 62 65 66 6f 72  * function befor
11c35 65 20 74 68 65 20 64 69 76 69 73 69 6f 6e 2e 20  e the division. 
11c36 20 44 69 76 69 73 69 6f 6e 20 62 79 20 7a 65 72   Division by zer
11c37 6f 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a  o returns NULL..
11c38 2a 2a 20 49 66 20 65 69 74 68 65 72 20 6f 70 65  ** If either ope
11c39 72 61 6e 64 20 69 73 20 4e 55 4c 4c 2c 20 74 68  rand is NULL, th
11c3a 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c  e result is NULL
11c3b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 64 64  ..*/.case OP_Add
11c3c 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  :               
11c3d 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
11c3e 4b 5f 50 4c 55 53 2c 20 6e 6f 2d 70 75 73 68 20  K_PLUS, no-push 
11c3f 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 75 62 74 72  */.case OP_Subtr
11c40 61 63 74 3a 20 20 20 20 20 20 20 20 20 20 20 20  act:            
11c41 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
11c42 4d 49 4e 55 53 2c 20 6e 6f 2d 70 75 73 68 20 2a  MINUS, no-push *
11c43 2f 0a 63 61 73 65 20 4f 50 5f 4d 75 6c 74 69 70  /.case OP_Multip
11c44 6c 79 3a 20 20 20 20 20 20 20 20 20 20 20 20 20  ly:             
11c45 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53   /* same as TK_S
11c46 54 41 52 2c 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a  TAR, no-push */.
11c47 63 61 73 65 20 4f 50 5f 44 69 76 69 64 65 3a 20  case OP_Divide: 
11c48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
11c49 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 4c 41  * same as TK_SLA
11c4a 53 48 2c 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 63  SH, no-push */.c
11c4b 61 73 65 20 4f 50 5f 52 65 6d 61 69 6e 64 65 72  ase OP_Remainder
11c4c 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  : {           /*
11c4d 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 45 4d 2c   same as TK_REM,
11c4e 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 4d 65   no-push */.  Me
11c4f 6d 20 2a 70 4e 6f 73 20 3d 20 26 70 54 6f 73 5b  m *pNos = &pTos[
11c50 2d 31 5d 3b 0a 20 20 69 6e 74 20 66 6c 61 67 73  -1];.  int flags
11c51 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4e 6f 73  ;.  assert( pNos
11c52 3e 3d 70 2d 3e 61 53 74 61 63 6b 20 29 3b 0a 20  >=p->aStack );. 
11c53 20 66 6c 61 67 73 20 3d 20 70 54 6f 73 2d 3e 66   flags = pTos->f
11c54 6c 61 67 73 20 7c 20 70 4e 6f 73 2d 3e 66 6c 61  lags | pNos->fla
11c55 67 73 3b 0a 20 20 69 66 28 20 28 66 6c 61 67 73  gs;.  if( (flags
11c56 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20   & MEM_Null)!=0 
11c57 29 7b 0a 20 20 20 20 52 65 6c 65 61 73 65 28 70  ){.    Release(p
11c58 54 6f 73 29 3b 0a 20 20 20 20 70 54 6f 73 2d 2d  Tos);.    pTos--
11c59 3b 0a 20 20 20 20 52 65 6c 65 61 73 65 28 70 54  ;.    Release(pT
11c5a 6f 73 29 3b 0a 20 20 20 20 70 54 6f 73 2d 3e 66  os);.    pTos->f
11c5b 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b  lags = MEM_Null;
11c5c 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 54  .  }else if( (pT
11c5d 6f 73 2d 3e 66 6c 61 67 73 20 26 20 70 4e 6f 73  os->flags & pNos
11c5e 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e  ->flags & MEM_In
11c5f 74 29 3d 3d 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20  t)==MEM_Int ){. 
11c60 20 20 20 69 36 34 20 61 2c 20 62 3b 0a 20 20 20     i64 a, b;.   
11c61 20 61 20 3d 20 70 54 6f 73 2d 3e 75 2e 69 3b 0a   a = pTos->u.i;.
11c62 20 20 20 20 62 20 3d 20 70 4e 6f 73 2d 3e 75 2e      b = pNos->u.
11c63 69 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20 70  i;.    switch( p
11c64 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20  Op->opcode ){.  
11c65 20 20 20 20 63 61 73 65 20 4f 50 5f 41 64 64 3a      case OP_Add:
11c66 20 20 20 20 20 20 20 20 20 62 20 2b 3d 20 61 3b           b += a;
11c67 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
11c68 20 20 20 20 63 61 73 65 20 4f 50 5f 53 75 62 74      case OP_Subt
11c69 72 61 63 74 3a 20 20 20 20 62 20 2d 3d 20 61 3b  ract:    b -= a;
11c6a 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
11c6b 20 20 20 20 63 61 73 65 20 4f 50 5f 4d 75 6c 74      case OP_Mult
11c6c 69 70 6c 79 3a 20 20 20 20 62 20 2a 3d 20 61 3b  iply:    b *= a;
11c6d 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
11c6e 20 20 20 20 63 61 73 65 20 4f 50 5f 44 69 76 69      case OP_Divi
11c6f 64 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 66  de: {.        if
11c70 28 20 61 3d 3d 30 20 29 20 67 6f 74 6f 20 64 69  ( a==0 ) goto di
11c71 76 69 64 65 5f 62 79 5f 7a 65 72 6f 3b 0a 20 20  vide_by_zero;.  
11c72 20 20 20 20 20 20 2f 2a 20 44 69 76 69 64 69 6e        /* Dividin
11c73 67 20 74 68 65 20 6c 61 72 67 65 73 74 20 70 6f  g the largest po
11c74 73 73 69 62 6c 65 20 6e 65 67 61 74 69 76 65 20  ssible negative 
11c75 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 28  64-bit integer (
11c76 31 3c 3c 36 33 29 20 62 79 20 0a 20 20 20 20 20  1<<63) by .     
11c77 20 20 20 2a 2a 20 2d 31 20 72 65 74 75 72 6e 73     ** -1 returns
11c78 20 61 6e 20 69 6e 74 65 67 65 72 20 74 6f 20 6c   an integer to l
11c79 61 72 67 65 20 74 6f 20 73 74 6f 72 65 20 69 6e  arge to store in
11c7a 20 61 20 36 34 2d 62 69 74 20 64 61 74 61 2d 74   a 64-bit data-t
11c7b 79 70 65 2e 20 4f 6e 0a 20 20 20 20 20 20 20 20  ype. On.        
11c7c 2a 2a 20 73 6f 6d 65 20 61 72 63 68 69 74 65 63  ** some architec
11c7d 74 75 72 65 73 2c 20 74 68 65 20 76 61 6c 75 65  tures, the value
11c7e 20 6f 76 65 72 66 6c 6f 77 73 20 74 6f 20 28 31   overflows to (1
11c7f 3c 3c 36 33 29 2e 20 4f 6e 20 6f 74 68 65 72 73  <<63). On others
11c80 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20 53  ,.        ** a S
11c81 49 47 46 50 45 20 69 73 20 69 73 73 75 65 64 2e  IGFPE is issued.
11c82 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   The following s
11c83 74 61 74 65 6d 65 6e 74 20 6e 6f 72 6d 61 6c 69  tatement normali
11c84 7a 65 73 20 74 68 69 73 0a 20 20 20 20 20 20 20  zes this.       
11c85 20 2a 2a 20 62 65 68 61 76 69 6f 75 72 20 73 6f   ** behaviour so
11c86 20 74 68 61 74 20 61 6c 6c 20 61 72 63 68 69 74   that all archit
11c87 65 63 74 75 72 65 73 20 62 65 68 61 76 65 20 61  ectures behave a
11c88 73 20 69 66 20 69 6e 74 65 67 65 72 20 0a 20 20  s if integer .  
11c89 20 20 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f        ** overflo
11c8a 77 20 6f 63 63 75 72 65 64 2e 0a 20 20 20 20 20  w occured..     
11c8b 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66     */.        if
11c8c 28 20 61 3d 3d 2d 31 20 26 26 20 62 3d 3d 28 28  ( a==-1 && b==((
11c8d 28 69 36 34 29 31 29 3c 3c 36 33 29 20 29 20 61  (i64)1)<<63) ) a
11c8e 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 62 20   = 1;.        b 
11c8f 2f 3d 20 61 3b 0a 20 20 20 20 20 20 20 20 62 72  /= a;.        br
11c90 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
11c91 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
11c92 20 20 20 20 20 20 69 66 28 20 61 3d 3d 30 20 29        if( a==0 )
11c93 20 67 6f 74 6f 20 64 69 76 69 64 65 5f 62 79 5f   goto divide_by_
11c94 7a 65 72 6f 3b 0a 20 20 20 20 20 20 20 20 69 66  zero;.        if
11c95 28 20 61 3d 3d 2d 31 20 29 20 61 20 3d 20 31 3b  ( a==-1 ) a = 1;
11c96 0a 20 20 20 20 20 20 20 20 62 20 25 3d 20 61 3b  .        b %= a;
11c97 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
11c98 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
11c99 20 20 52 65 6c 65 61 73 65 28 70 54 6f 73 29 3b    Release(pTos);
11c9a 0a 20 20 20 20 70 54 6f 73 2d 2d 3b 0a 20 20 20  .    pTos--;.   
11c9b 20 52 65 6c 65 61 73 65 28 70 54 6f 73 29 3b 0a   Release(pTos);.
11c9c 20 20 20 20 70 54 6f 73 2d 3e 75 2e 69 20 3d 20      pTos->u.i = 
11c9d 62 3b 0a 20 20 20 20 70 54 6f 73 2d 3e 66 6c 61  b;.    pTos->fla
11c9e 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20  gs = MEM_Int;.  
11c9f 7d 65 6c 73 65 7b 0a 20 20 20 20 64 6f 75 62 6c  }else{.    doubl
11ca0 65 20 61 2c 20 62 3b 0a 20 20 20 20 61 20 3d 20  e a, b;.    a = 
11ca1 73 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56  sqlite3VdbeRealV
11ca2 61 6c 75 65 28 70 54 6f 73 29 3b 0a 20 20 20 20  alue(pTos);.    
11ca3 62 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52  b = sqlite3VdbeR
11ca4 65 61 6c 56 61 6c 75 65 28 70 4e 6f 73 29 3b 0a  ealValue(pNos);.
11ca5 20 20 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d      switch( pOp-
11ca6 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 20 20  >opcode ){.     
11ca7 20 63 61 73 65 20 4f 50 5f 41 64 64 3a 20 20 20   case OP_Add:   
11ca8 20 20 20 20 20 20 62 20 2b 3d 20 61 3b 20 20 20        b += a;   
11ca9 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
11caa 20 63 61 73 65 20 4f 50 5f 53 75 62 74 72 61 63   case OP_Subtrac
11cab 74 3a 20 20 20 20 62 20 2d 3d 20 61 3b 20 20 20  t:    b -= a;   
11cac 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
11cad 20 63 61 73 65 20 4f 50 5f 4d 75 6c 74 69 70 6c   case OP_Multipl
11cae 79 3a 20 20 20 20 62 20 2a 3d 20 61 3b 20 20 20  y:    b *= a;   
11caf 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
11cb0 20 63 61 73 65 20 4f 50 5f 44 69 76 69 64 65 3a   case OP_Divide:
11cb1 20 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 61   {.        if( a
11cb2 3d 3d 30 2e 30 20 29 20 67 6f 74 6f 20 64 69 76  ==0.0 ) goto div
11cb3 69 64 65 5f 62 79 5f 7a 65 72 6f 3b 0a 20 20 20  ide_by_zero;.   
11cb4 20 20 20 20 20 62 20 2f 3d 20 61 3b 0a 20 20 20       b /= a;.   
11cb5 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
11cb6 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61 75 6c    }.      defaul
11cb7 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 36 34  t: {.        i64
11cb8 20 69 61 20 3d 20 28 69 36 34 29 61 3b 0a 20 20   ia = (i64)a;.  
11cb9 20 20 20 20 20 20 69 36 34 20 69 62 20 3d 20 28        i64 ib = (
11cba 69 36 34 29 62 3b 0a 20 20 20 20 20 20 20 20 69  i64)b;.        i
11cbb 66 28 20 69 61 3d 3d 30 20 29 20 67 6f 74 6f 20  f( ia==0 ) goto 
11cbc 64 69 76 69 64 65 5f 62 79 5f 7a 65 72 6f 3b 0a  divide_by_zero;.
11cbd 20 20 20 20 20 20 20 20 69 66 28 20 69 61 3d 3d          if( ia==
11cbe 2d 31 20 29 20 69 61 20 3d 20 31 3b 0a 20 20 20  -1 ) ia = 1;.   
11cbf 20 20 20 20 20 62 20 3d 20 69 62 20 25 20 69 61       b = ib % ia
11cc0 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
11cc1 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
11cc2 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69     if( sqlite3_i
11cc3 73 6e 61 6e 28 62 29 20 29 7b 0a 20 20 20 20 20  snan(b) ){.     
11cc4 20 67 6f 74 6f 20 64 69 76 69 64 65 5f 62 79 5f   goto divide_by_
11cc5 7a 65 72 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20  zero;.    }.    
11cc6 52 65 6c 65 61 73 65 28 70 54 6f 73 29 3b 0a 20  Release(pTos);. 
11cc7 20 20 20 70 54 6f 73 2d 2d 3b 0a 20 20 20 20 52     pTos--;.    R
11cc8 65 6c 65 61 73 65 28 70 54 6f 73 29 3b 0a 20 20  elease(pTos);.  
11cc9 20 20 70 54 6f 73 2d 3e 72 20 3d 20 62 3b 0a 20    pTos->r = b;. 
11cca 20 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 3d     pTos->flags =
11ccb 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20 20 20 20 69   MEM_Real;.    i
11ccc 66 28 20 28 66 6c 61 67 73 20 26 20 4d 45 4d 5f  f( (flags & MEM_
11ccd 52 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20  Real)==0 ){.    
11cce 20 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74    sqlite3VdbeInt
11ccf 65 67 65 72 41 66 66 69 6e 69 74 79 28 70 54 6f  egerAffinity(pTo
11cd0 73 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  s);.    }.  }.  
11cd1 62 72 65 61 6b 3b 0a 0a 64 69 76 69 64 65 5f 62  break;..divide_b
11cd2 79 5f 7a 65 72 6f 3a 0a 20 20 52 65 6c 65 61 73  y_zero:.  Releas
11cd3 65 28 70 54 6f 73 29 3b 0a 20 20 70 54 6f 73 2d  e(pTos);.  pTos-
11cd4 2d 3b 0a 20 20 52 65 6c 65 61 73 65 28 70 54 6f  -;.  Release(pTo
11cd5 73 29 3b 0a 20 20 70 54 6f 73 2d 3e 66 6c 61 67  s);.  pTos->flag
11cd6 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20  s = MEM_Null;.  
11cd7 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
11cd8 6f 64 65 3a 20 43 6f 6c 6c 53 65 71 20 2a 20 2a  ode: CollSeq * *
11cd9 20 50 33 0a 2a 2a 0a 2a 2a 20 50 33 20 69 73 20   P3.**.** P3 is 
11cda 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 43  a pointer to a C
11cdb 6f 6c 6c 53 65 71 20 73 74 72 75 63 74 2e 20 49  ollSeq struct. I
11cdc 66 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20  f the next call 
11cdd 74 6f 20 61 20 75 73 65 72 20 66 75 6e 63 74 69  to a user functi
11cde 6f 6e 0a 2a 2a 20 6f 72 20 61 67 67 72 65 67 61  on.** or aggrega
11cdf 74 65 20 63 61 6c 6c 73 20 73 71 6c 69 74 65 33  te calls sqlite3
11ce0 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 29  GetFuncCollSeq()
11ce1 2c 20 74 68 69 73 20 63 6f 6c 6c 61 74 69 6f 6e  , this collation
11ce2 20 73 65 71 75 65 6e 63 65 20 77 69 6c 6c 0a 2a   sequence will.*
11ce3 2a 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 54  * be returned. T
11ce4 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20 74  his is used by t
11ce5 68 65 20 62 75 69 6c 74 2d 69 6e 20 6d 69 6e 28  he built-in min(
11ce6 29 2c 20 6d 61 78 28 29 20 61 6e 64 20 6e 75 6c  ), max() and nul
11ce7 6c 69 66 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f  lif().** functio
11ce8 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e  ns..**.** The in
11ce9 74 65 72 66 61 63 65 20 75 73 65 64 20 62 79 20  terface used by 
11cea 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  the implementati
11ceb 6f 6e 20 6f 66 20 74 68 65 20 61 66 6f 72 65 6d  on of the aforem
11cec 65 6e 74 69 6f 6e 65 64 20 66 75 6e 63 74 69 6f  entioned functio
11ced 6e 73 0a 2a 2a 20 74 6f 20 72 65 74 72 69 65 76  ns.** to retriev
11cee 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  e the collation 
11cef 73 65 71 75 65 6e 63 65 20 73 65 74 20 62 79 20  sequence set by 
11cf0 74 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20 6e  this opcode is n
11cf1 6f 74 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20  ot available.** 
11cf2 70 75 62 6c 69 63 6c 79 2c 20 6f 6e 6c 79 20 74  publicly, only t
11cf3 6f 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 73  o user functions
11cf4 20 64 65 66 69 6e 65 64 20 69 6e 20 66 75 6e 63   defined in func
11cf5 2e 63 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43  .c..*/.case OP_C
11cf6 6f 6c 6c 53 65 71 3a 20 7b 20 20 20 20 20 20 20  ollSeq: {       
11cf7 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68        /* no-push
11cf8 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f   */.  assert( pO
11cf9 70 2d 3e 70 33 74 79 70 65 3d 3d 50 33 5f 43 4f  p->p3type==P3_CO
11cfa 4c 4c 53 45 51 20 29 3b 0a 20 20 62 72 65 61 6b  LLSEQ );.  break
11cfb 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
11cfc 46 75 6e 63 74 69 6f 6e 20 50 31 20 50 32 20 50  Function P1 P2 P
11cfd 33 0a 2a 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 61  3.**.** Invoke a
11cfe 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 28   user function (
11cff 50 33 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  P3 is a pointer 
11d00 74 6f 20 61 20 46 75 6e 63 74 69 6f 6e 20 73 74  to a Function st
11d01 72 75 63 74 75 72 65 20 74 68 61 74 0a 2a 2a 20  ructure that.** 
11d02 64 65 66 69 6e 65 73 20 74 68 65 20 66 75 6e 63  defines the func
11d03 74 69 6f 6e 29 20 77 69 74 68 20 50 32 20 61 72  tion) with P2 ar
11d04 67 75 6d 65 6e 74 73 20 74 61 6b 65 6e 20 66 72  guments taken fr
11d05 6f 6d 20 74 68 65 20 73 74 61 63 6b 2e 20 20 50  om the stack.  P
11d06 6f 70 20 61 6c 6c 0a 2a 2a 20 61 72 67 75 6d 65  op all.** argume
11d07 6e 74 73 20 66 72 6f 6d 20 74 68 65 20 73 74 61  nts from the sta
11d08 63 6b 20 61 6e 64 20 70 75 73 68 20 62 61 63 6b  ck and push back
11d09 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a   the result..**.
11d0a 2a 2a 20 50 31 20 69 73 20 61 20 33 32 2d 62 69  ** P1 is a 32-bi
11d0b 74 20 62 69 74 6d 61 73 6b 20 69 6e 64 69 63 61  t bitmask indica
11d0c 74 69 6e 67 20 77 68 65 74 68 65 72 20 6f 72 20  ting whether or 
11d0d 6e 6f 74 20 65 61 63 68 20 61 72 67 75 6d 65 6e  not each argumen
11d0e 74 20 74 6f 20 74 68 65 20 0a 2a 2a 20 66 75 6e  t to the .** fun
11d0f 63 74 69 6f 6e 20 77 61 73 20 64 65 74 65 72 6d  ction was determ
11d10 69 6e 65 64 20 74 6f 20 62 65 20 63 6f 6e 73 74  ined to be const
11d11 61 6e 74 20 61 74 20 63 6f 6d 70 69 6c 65 20 74  ant at compile t
11d12 69 6d 65 2e 20 49 66 20 74 68 65 20 66 69 72 73  ime. If the firs
11d13 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 77 61  t.** argument wa
11d14 73 20 63 6f 6e 73 74 61 6e 74 20 74 68 65 6e 20  s constant then 
11d15 62 69 74 20 30 20 6f 66 20 50 31 20 69 73 20 73  bit 0 of P1 is s
11d16 65 74 2e 20 54 68 69 73 20 69 73 20 75 73 65 64  et. This is used
11d17 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a   to determine.**
11d18 20 77 68 65 74 68 65 72 20 6d 65 74 61 20 64 61   whether meta da
11d19 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ta associated wi
11d1a 74 68 20 61 20 75 73 65 72 20 66 75 6e 63 74 69  th a user functi
11d1b 6f 6e 20 61 72 67 75 6d 65 6e 74 20 75 73 69 6e  on argument usin
11d1c 67 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33  g the.** sqlite3
11d1d 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 41  _set_auxdata() A
11d1e 50 49 20 6d 61 79 20 62 65 20 73 61 66 65 6c 79  PI may be safely
11d1f 20 72 65 74 61 69 6e 65 64 20 75 6e 74 69 6c 20   retained until 
11d20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 69 6e 76 6f  the next.** invo
11d21 63 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 6f  cation of this o
11d22 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  pcode..**.** See
11d23 20 61 6c 73 6f 3a 20 41 67 67 53 74 65 70 20 61   also: AggStep a
11d24 6e 64 20 41 67 67 46 69 6e 61 6c 0a 2a 2f 0a 63  nd AggFinal.*/.c
11d25 61 73 65 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 3a  ase OP_Function:
11d26 20 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d 65   {.  int i;.  Me
11d27 6d 20 2a 70 41 72 67 3b 0a 20 20 73 71 6c 69 74  m *pArg;.  sqlit
11d28 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b 0a  e3_context ctx;.
11d29 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
11d2a 2a 2a 61 70 56 61 6c 3b 0a 20 20 69 6e 74 20 6e  **apVal;.  int n
11d2b 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 0a 20 20 61   = pOp->p2;..  a
11d2c 70 56 61 6c 20 3d 20 70 2d 3e 61 70 41 72 67 3b  pVal = p->apArg;
11d2d 0a 20 20 61 73 73 65 72 74 28 20 61 70 56 61 6c  .  assert( apVal
11d2e 20 7c 7c 20 6e 3d 3d 30 20 29 3b 0a 0a 20 20 70   || n==0 );..  p
11d2f 41 72 67 20 3d 20 26 70 54 6f 73 5b 31 2d 6e 5d  Arg = &pTos[1-n]
11d30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e  ;.  for(i=0; i<n
11d31 3b 20 69 2b 2b 2c 20 70 41 72 67 2b 2b 29 7b 0a  ; i++, pArg++){.
11d32 20 20 20 20 61 70 56 61 6c 5b 69 5d 20 3d 20 70      apVal[i] = p
11d33 41 72 67 3b 0a 20 20 20 20 73 74 6f 72 65 54 79  Arg;.    storeTy
11d34 70 65 49 6e 66 6f 28 70 41 72 67 2c 20 65 6e 63  peInfo(pArg, enc
11d35 6f 64 69 6e 67 29 3b 0a 20 20 7d 0a 0a 20 20 61  oding);.  }..  a
11d36 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 74 79  ssert( pOp->p3ty
11d37 70 65 3d 3d 50 33 5f 46 55 4e 43 44 45 46 20 7c  pe==P3_FUNCDEF |
11d38 7c 20 70 4f 70 2d 3e 70 33 74 79 70 65 3d 3d 50  | pOp->p3type==P
11d39 33 5f 56 44 42 45 46 55 4e 43 20 29 3b 0a 20 20  3_VDBEFUNC );.  
11d3a 69 66 28 20 70 4f 70 2d 3e 70 33 74 79 70 65 3d  if( pOp->p3type=
11d3b 3d 50 33 5f 46 55 4e 43 44 45 46 20 29 7b 0a 20  =P3_FUNCDEF ){. 
11d3c 20 20 20 63 74 78 2e 70 46 75 6e 63 20 3d 20 28     ctx.pFunc = (
11d3d 46 75 6e 63 44 65 66 2a 29 70 4f 70 2d 3e 70 33  FuncDef*)pOp->p3
11d3e 3b 0a 20 20 20 20 63 74 78 2e 70 56 64 62 65 46  ;.    ctx.pVdbeF
11d3f 75 6e 63 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  unc = 0;.  }else
11d40 7b 0a 20 20 20 20 63 74 78 2e 70 56 64 62 65 46  {.    ctx.pVdbeF
11d41 75 6e 63 20 3d 20 28 56 64 62 65 46 75 6e 63 2a  unc = (VdbeFunc*
11d42 29 70 4f 70 2d 3e 70 33 3b 0a 20 20 20 20 63 74  )pOp->p3;.    ct
11d43 78 2e 70 46 75 6e 63 20 3d 20 63 74 78 2e 70 56  x.pFunc = ctx.pV
11d44 64 62 65 46 75 6e 63 2d 3e 70 46 75 6e 63 3b 0a  dbeFunc->pFunc;.
11d45 20 20 7d 0a 0a 20 20 63 74 78 2e 73 2e 66 6c 61    }..  ctx.s.fla
11d46 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20  gs = MEM_Null;. 
11d47 20 63 74 78 2e 73 2e 7a 20 3d 20 30 3b 0a 20 20   ctx.s.z = 0;.  
11d48 63 74 78 2e 73 2e 78 44 65 6c 20 3d 20 30 3b 0a  ctx.s.xDel = 0;.
11d49 20 20 63 74 78 2e 69 73 45 72 72 6f 72 20 3d 20    ctx.isError = 
11d4a 30 3b 0a 20 20 69 66 28 20 63 74 78 2e 70 46 75  0;.  if( ctx.pFu
11d4b 6e 63 2d 3e 6e 65 65 64 43 6f 6c 6c 53 65 71 20  nc->needCollSeq 
11d4c 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
11d4d 4f 70 3e 70 2d 3e 61 4f 70 20 29 3b 0a 20 20 20  Op>p->aOp );.   
11d4e 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d   assert( pOp[-1]
11d4f 2e 70 33 74 79 70 65 3d 3d 50 33 5f 43 4f 4c 4c  .p3type==P3_COLL
11d50 53 45 51 20 29 3b 0a 20 20 20 20 61 73 73 65 72  SEQ );.    asser
11d51 74 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64  t( pOp[-1].opcod
11d52 65 3d 3d 4f 50 5f 43 6f 6c 6c 53 65 71 20 29 3b  e==OP_CollSeq );
11d53 0a 20 20 20 20 63 74 78 2e 70 43 6f 6c 6c 20 3d  .    ctx.pColl =
11d54 20 28 43 6f 6c 6c 53 65 71 20 2a 29 70 4f 70 5b   (CollSeq *)pOp[
11d55 2d 31 5d 2e 70 33 3b 0a 20 20 7d 0a 20 20 69 66  -1].p3;.  }.  if
11d56 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  ( sqlite3SafetyO
11d57 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62  ff(db) ) goto ab
11d58 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73  ort_due_to_misus
11d59 65 3b 0a 20 20 28 2a 63 74 78 2e 70 46 75 6e 63  e;.  (*ctx.pFunc
11d5a 2d 3e 78 46 75 6e 63 29 28 26 63 74 78 2c 20 6e  ->xFunc)(&ctx, n
11d5b 2c 20 61 70 56 61 6c 29 3b 0a 20 20 69 66 28 20  , apVal);.  if( 
11d5c 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
11d5d 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74  db) ) goto abort
11d5e 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a  _due_to_misuse;.
11d5f 20 20 69 66 28 20 73 71 6c 69 74 65 33 4d 61 6c    if( sqlite3Mal
11d60 6c 6f 63 46 61 69 6c 65 64 28 29 20 29 20 67 6f  locFailed() ) go
11d61 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 70 6f 70  to no_mem;.  pop
11d62 53 74 61 63 6b 28 26 70 54 6f 73 2c 20 6e 29 3b  Stack(&pTos, n);
11d63 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 79 20 61 75  ..  /* If any au
11d64 78 69 6c 61 72 79 20 64 61 74 61 20 66 75 6e 63  xilary data func
11d65 74 69 6f 6e 73 20 68 61 76 65 20 62 65 65 6e 20  tions have been 
11d66 63 61 6c 6c 65 64 20 62 79 20 74 68 69 73 20 75  called by this u
11d67 73 65 72 20 66 75 6e 63 74 69 6f 6e 2c 0a 20 20  ser function,.  
11d68 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 63  ** immediately c
11d69 61 6c 6c 20 74 68 65 20 64 65 73 74 72 75 63 74  all the destruct
11d6a 6f 72 20 66 6f 72 20 61 6e 79 20 6e 6f 6e 2d 73  or for any non-s
11d6b 74 61 74 69 63 20 76 61 6c 75 65 73 2e 0a 20 20  tatic values..  
11d6c 2a 2f 0a 20 20 69 66 28 20 63 74 78 2e 70 56 64  */.  if( ctx.pVd
11d6d 62 65 46 75 6e 63 20 29 7b 0a 20 20 20 20 73 71  beFunc ){.    sq
11d6e 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 41  lite3VdbeDeleteA
11d6f 75 78 44 61 74 61 28 63 74 78 2e 70 56 64 62 65  uxData(ctx.pVdbe
11d70 46 75 6e 63 2c 20 70 4f 70 2d 3e 70 31 29 3b 0a  Func, pOp->p1);.
11d71 20 20 20 20 70 4f 70 2d 3e 70 33 20 3d 20 28 63      pOp->p3 = (c
11d72 68 61 72 20 2a 29 63 74 78 2e 70 56 64 62 65 46  har *)ctx.pVdbeF
11d73 75 6e 63 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 33  unc;.    pOp->p3
11d74 74 79 70 65 20 3d 20 50 33 5f 56 44 42 45 46 55  type = P3_VDBEFU
11d75 4e 43 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66  NC;.  }..  /* If
11d76 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65   the function re
11d77 74 75 72 6e 65 64 20 61 6e 20 65 72 72 6f 72 2c  turned an error,
11d78 20 74 68 72 6f 77 20 61 6e 20 65 78 63 65 70 74   throw an except
11d79 69 6f 6e 20 2a 2f 0a 20 20 69 66 28 20 63 74 78  ion */.  if( ctx
11d7a 2e 69 73 45 72 72 6f 72 20 29 7b 0a 20 20 20 20  .isError ){.    
11d7b 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67  sqlite3SetString
11d7c 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 73 71  (&p->zErrMsg, sq
11d7d 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
11d7e 28 26 63 74 78 2e 73 29 2c 20 28 63 68 61 72 2a  (&ctx.s), (char*
11d7f 29 30 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  )0);.    rc = SQ
11d80 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
11d81 0a 20 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 72  .  /* Copy the r
11d82 65 73 75 6c 74 20 6f 66 20 74 68 65 20 66 75 6e  esult of the fun
11d83 63 74 69 6f 6e 20 74 6f 20 74 68 65 20 74 6f 70  ction to the top
11d84 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20 2a 2f   of the stack */
11d85 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  .  sqlite3VdbeCh
11d86 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 26 63 74  angeEncoding(&ct
11d87 78 2e 73 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a  x.s, encoding);.
11d88 20 20 70 54 6f 73 2b 2b 3b 0a 20 20 70 54 6f 73    pTos++;.  pTos
11d89 2d 3e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 73  ->flags = 0;.  s
11d8a 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76  qlite3VdbeMemMov
11d8b 65 28 70 54 6f 73 2c 20 26 63 74 78 2e 73 29 3b  e(pTos, &ctx.s);
11d8c 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64  .  if( sqlite3Vd
11d8d 62 65 4d 65 6d 54 6f 6f 42 69 67 28 70 54 6f 73  beMemTooBig(pTos
11d8e 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f  ) ){.    goto to
11d8f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 62 72 65  o_big;.  }.  bre
11d90 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
11d91 3a 20 42 69 74 41 6e 64 20 2a 20 2a 20 2a 0a 2a  : BitAnd * * *.*
11d92 2a 0a 2a 2a 20 50 6f 70 20 74 68 65 20 74 6f 70  *.** Pop the top
11d93 20 74 77 6f 20 65 6c 65 6d 65 6e 74 73 20 66 72   two elements fr
11d94 6f 6d 20 74 68 65 20 73 74 61 63 6b 2e 20 20 43  om the stack.  C
11d95 6f 6e 76 65 72 74 20 62 6f 74 68 20 65 6c 65 6d  onvert both elem
11d96 65 6e 74 73 0a 2a 2a 20 74 6f 20 69 6e 74 65 67  ents.** to integ
11d97 65 72 73 2e 20 20 50 75 73 68 20 62 61 63 6b 20  ers.  Push back 
11d98 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 20 74  onto the stack t
11d99 68 65 20 62 69 74 2d 77 69 73 65 20 41 4e 44 20  he bit-wise AND 
11d9a 6f 66 20 74 68 65 0a 2a 2a 20 74 77 6f 20 65 6c  of the.** two el
11d9b 65 6d 65 6e 74 73 2e 0a 2a 2a 20 49 66 20 65 69  ements..** If ei
11d9c 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20  ther operand is 
11d9d 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74  NULL, the result
11d9e 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20   is NULL..*/./* 
11d9f 4f 70 63 6f 64 65 3a 20 42 69 74 4f 72 20 2a 20  Opcode: BitOr * 
11da0 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 6f 70 20 74 68  * *.**.** Pop th
11da1 65 20 74 6f 70 20 74 77 6f 20 65 6c 65 6d 65 6e  e top two elemen
11da2 74 73 20 66 72 6f 6d 20 74 68 65 20 73 74 61 63  ts from the stac
11da3 6b 2e 20 20 43 6f 6e 76 65 72 74 20 62 6f 74 68  k.  Convert both
11da4 20 65 6c 65 6d 65 6e 74 73 0a 2a 2a 20 74 6f 20   elements.** to 
11da5 69 6e 74 65 67 65 72 73 2e 20 20 50 75 73 68 20  integers.  Push 
11da6 62 61 63 6b 20 6f 6e 74 6f 20 74 68 65 20 73 74  back onto the st
11da7 61 63 6b 20 74 68 65 20 62 69 74 2d 77 69 73 65  ack the bit-wise
11da8 20 4f 52 20 6f 66 20 74 68 65 0a 2a 2a 20 74 77   OR of the.** tw
11da9 6f 20 65 6c 65 6d 65 6e 74 73 2e 0a 2a 2a 20 49  o elements..** I
11daa 66 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64  f either operand
11dab 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65   is NULL, the re
11dac 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f  sult is NULL..*/
11dad 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 68 69 66  ./* Opcode: Shif
11dae 74 4c 65 66 74 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  tLeft * * *.**.*
11daf 2a 20 50 6f 70 20 74 68 65 20 74 6f 70 20 74 77  * Pop the top tw
11db0 6f 20 65 6c 65 6d 65 6e 74 73 20 66 72 6f 6d 20  o elements from 
11db1 74 68 65 20 73 74 61 63 6b 2e 20 20 43 6f 6e 76  the stack.  Conv
11db2 65 72 74 20 62 6f 74 68 20 65 6c 65 6d 65 6e 74  ert both element
11db3 73 0a 2a 2a 20 74 6f 20 69 6e 74 65 67 65 72 73  s.** to integers
11db4 2e 20 20 50 75 73 68 20 62 61 63 6b 20 6f 6e 74  .  Push back ont
11db5 6f 20 74 68 65 20 73 74 61 63 6b 20 74 68 65 20  o the stack the 
11db6 73 65 63 6f 6e 64 20 65 6c 65 6d 65 6e 74 20 73  second element s
11db7 68 69 66 74 65 64 0a 2a 2a 20 6c 65 66 74 20 62  hifted.** left b
11db8 79 20 4e 20 62 69 74 73 20 77 68 65 72 65 20 4e  y N bits where N
11db9 20 69 73 20 74 68 65 20 74 6f 70 20 65 6c 65 6d   is the top elem
11dba 65 6e 74 20 6f 6e 20 74 68 65 20 73 74 61 63 6b  ent on the stack
11dbb 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 6f  ..** If either o
11dbc 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 2c 20  perand is NULL, 
11dbd 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55  the result is NU
11dbe 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65  LL..*/./* Opcode
11dbf 3a 20 53 68 69 66 74 52 69 67 68 74 20 2a 20 2a  : ShiftRight * *
11dc0 20 2a 0a 2a 2a 0a 2a 2a 20 50 6f 70 20 74 68 65   *.**.** Pop the
11dc1 20 74 6f 70 20 74 77 6f 20 65 6c 65 6d 65 6e 74   top two element
11dc2 73 20 66 72 6f 6d 20 74 68 65 20 73 74 61 63 6b  s from the stack
11dc3 2e 20 20 43 6f 6e 76 65 72 74 20 62 6f 74 68 20  .  Convert both 
11dc4 65 6c 65 6d 65 6e 74 73 0a 2a 2a 20 74 6f 20 69  elements.** to i
11dc5 6e 74 65 67 65 72 73 2e 20 20 50 75 73 68 20 62  ntegers.  Push b
11dc6 61 63 6b 20 6f 6e 74 6f 20 74 68 65 20 73 74 61  ack onto the sta
11dc7 63 6b 20 74 68 65 20 73 65 63 6f 6e 64 20 65 6c  ck the second el
11dc8 65 6d 65 6e 74 20 73 68 69 66 74 65 64 0a 2a 2a  ement shifted.**
11dc9 20 72 69 67 68 74 20 62 79 20 4e 20 62 69 74 73   right by N bits
11dca 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20   where N is the 
11dcb 74 6f 70 20 65 6c 65 6d 65 6e 74 20 6f 6e 20 74  top element on t
11dcc 68 65 20 73 74 61 63 6b 2e 0a 2a 2a 20 49 66 20  he stack..** If 
11dcd 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69  either operand i
11dce 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75  s NULL, the resu
11dcf 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63  lt is NULL..*/.c
11dd0 61 73 65 20 4f 50 5f 42 69 74 41 6e 64 3a 20 20  ase OP_BitAnd:  
11dd1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
11dd2 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54  * same as TK_BIT
11dd3 41 4e 44 2c 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a  AND, no-push */.
11dd4 63 61 73 65 20 4f 50 5f 42 69 74 4f 72 3a 20 20  case OP_BitOr:  
11dd5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11dd6 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49  /* same as TK_BI
11dd7 54 4f 52 2c 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a  TOR, no-push */.
11dd8 63 61 73 65 20 4f 50 5f 53 68 69 66 74 4c 65 66  case OP_ShiftLef
11dd9 74 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t:              
11dda 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 53  /* same as TK_LS
11ddb 48 49 46 54 2c 20 6e 6f 2d 70 75 73 68 20 2a 2f  HIFT, no-push */
11ddc 0a 63 61 73 65 20 4f 50 5f 53 68 69 66 74 52 69  .case OP_ShiftRi
11ddd 67 68 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20  ght: {          
11dde 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52   /* same as TK_R
11ddf 53 48 49 46 54 2c 20 6e 6f 2d 70 75 73 68 20 2a  SHIFT, no-push *
11de0 2f 0a 20 20 4d 65 6d 20 2a 70 4e 6f 73 20 3d 20  /.  Mem *pNos = 
11de1 26 70 54 6f 73 5b 2d 31 5d 3b 0a 20 20 69 36 34  &pTos[-1];.  i64
11de2 20 61 2c 20 62 3b 0a 0a 20 20 61 73 73 65 72 74   a, b;..  assert
11de3 28 20 70 4e 6f 73 3e 3d 70 2d 3e 61 53 74 61 63  ( pNos>=p->aStac
11de4 6b 20 29 3b 0a 20 20 69 66 28 20 28 70 54 6f 73  k );.  if( (pTos
11de5 2d 3e 66 6c 61 67 73 20 7c 20 70 4e 6f 73 2d 3e  ->flags | pNos->
11de6 66 6c 61 67 73 29 20 26 20 4d 45 4d 5f 4e 75 6c  flags) & MEM_Nul
11de7 6c 20 29 7b 0a 20 20 20 20 70 6f 70 53 74 61 63  l ){.    popStac
11de8 6b 28 26 70 54 6f 73 2c 20 32 29 3b 0a 20 20 20  k(&pTos, 2);.   
11de9 20 70 54 6f 73 2b 2b 3b 0a 20 20 20 20 70 54 6f   pTos++;.    pTo
11dea 73 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e  s->flags = MEM_N
11deb 75 6c 6c 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a  ull;.    break;.
11dec 20 20 7d 0a 20 20 61 20 3d 20 73 71 6c 69 74 65    }.  a = sqlite
11ded 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 4e  3VdbeIntValue(pN
11dee 6f 73 29 3b 0a 20 20 62 20 3d 20 73 71 6c 69 74  os);.  b = sqlit
11def 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70  e3VdbeIntValue(p
11df0 54 6f 73 29 3b 0a 20 20 73 77 69 74 63 68 28 20  Tos);.  switch( 
11df1 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20  pOp->opcode ){. 
11df2 20 20 20 63 61 73 65 20 4f 50 5f 42 69 74 41 6e     case OP_BitAn
11df3 64 3a 20 20 20 20 20 20 61 20 26 3d 20 62 3b 20  d:      a &= b; 
11df4 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63      break;.    c
11df5 61 73 65 20 4f 50 5f 42 69 74 4f 72 3a 20 20 20  ase OP_BitOr:   
11df6 20 20 20 20 61 20 7c 3d 20 62 3b 20 20 20 20 20      a |= b;     
11df7 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
11df8 4f 50 5f 53 68 69 66 74 4c 65 66 74 3a 20 20 20  OP_ShiftLeft:   
11df9 61 20 3c 3c 3d 20 62 3b 20 20 20 20 62 72 65 61  a <<= b;    brea
11dfa 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 53  k;.    case OP_S
11dfb 68 69 66 74 52 69 67 68 74 3a 20 20 61 20 3e 3e  hiftRight:  a >>
11dfc 3d 20 62 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20  = b;    break;. 
11dfd 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 2f 2a     default:   /*
11dfe 20 43 41 4e 54 20 48 41 50 50 45 4e 20 2a 2f 20   CANT HAPPEN */ 
11dff 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20      break;.  }. 
11e00 20 52 65 6c 65 61 73 65 28 70 54 6f 73 29 3b 0a   Release(pTos);.
11e01 20 20 70 54 6f 73 2d 2d 3b 0a 20 20 52 65 6c 65    pTos--;.  Rele
11e02 61 73 65 28 70 54 6f 73 29 3b 0a 20 20 70 54 6f  ase(pTos);.  pTo
11e03 73 2d 3e 75 2e 69 20 3d 20 61 3b 0a 20 20 70 54  s->u.i = a;.  pT
11e04 6f 73 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  os->flags = MEM_
11e05 49 6e 74 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  Int;.  break;.}.
11e06 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 64 64 49  ./* Opcode: AddI
11e07 6d 6d 20 20 50 31 20 2a 20 2a 0a 2a 2a 20 0a 2a  mm  P1 * *.** .*
11e08 2a 20 41 64 64 20 74 68 65 20 76 61 6c 75 65 20  * Add the value 
11e09 50 31 20 74 6f 20 77 68 61 74 65 76 65 72 20 69  P1 to whatever i
11e0a 73 20 6f 6e 20 74 6f 70 20 6f 66 20 74 68 65 20  s on top of the 
11e0b 73 74 61 63 6b 2e 20 20 54 68 65 20 72 65 73 75  stack.  The resu
11e0c 6c 74 0a 2a 2a 20 69 73 20 61 6c 77 61 79 73 20  lt.** is always 
11e0d 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a  an integer..**.*
11e0e 2a 20 54 6f 20 66 6f 72 63 65 20 74 68 65 20 74  * To force the t
11e0f 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20  op of the stack 
11e10 74 6f 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72  to be an integer
11e11 2c 20 6a 75 73 74 20 61 64 64 20 30 2e 0a 2a 2f  , just add 0..*/
11e12 0a 63 61 73 65 20 4f 50 5f 41 64 64 49 6d 6d 3a  .case OP_AddImm:
11e13 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a   {            /*
11e14 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 61 73   no-push */.  as
11e15 73 65 72 74 28 20 70 54 6f 73 3e 3d 70 2d 3e 61  sert( pTos>=p->a
11e16 53 74 61 63 6b 20 29 3b 0a 20 20 73 71 6c 69 74  Stack );.  sqlit
11e17 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72  e3VdbeMemInteger
11e18 69 66 79 28 70 54 6f 73 29 3b 0a 20 20 70 54 6f  ify(pTos);.  pTo
11e19 73 2d 3e 75 2e 69 20 2b 3d 20 70 4f 70 2d 3e 70  s->u.i += pOp->p
11e1a 31 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  1;.  break;.}../
11e1b 2a 20 4f 70 63 6f 64 65 3a 20 46 6f 72 63 65 49  * Opcode: ForceI
11e1c 6e 74 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a  nt P1 P2 *.**.**
11e1d 20 43 6f 6e 76 65 72 74 20 74 68 65 20 74 6f 70   Convert the top
11e1e 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20 69 6e   of the stack in
11e1f 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20  to an integer.  
11e20 49 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  If the current t
11e21 6f 70 20 6f 66 0a 2a 2a 20 74 68 65 20 73 74 61  op of.** the sta
11e22 63 6b 20 69 73 20 6e 6f 74 20 6e 75 6d 65 72 69  ck is not numeri
11e23 63 20 28 6d 65 61 6e 69 6e 67 20 74 68 61 74 20  c (meaning that 
11e24 69 73 20 69 73 20 61 20 4e 55 4c 4c 20 6f 72 20  is is a NULL or 
11e25 61 20 73 74 72 69 6e 67 20 74 68 61 74 0a 2a 2a  a string that.**
11e26 20 64 6f 65 73 20 6e 6f 74 20 6c 6f 6f 6b 20 6c   does not look l
11e27 69 6b 65 20 61 6e 20 69 6e 74 65 67 65 72 20 6f  ike an integer o
11e28 72 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  r floating point
11e29 20 6e 75 6d 62 65 72 29 20 74 68 65 6e 20 70 6f   number) then po
11e2a 70 20 74 68 65 0a 2a 2a 20 73 74 61 63 6b 20 61  p the.** stack a
11e2b 6e 64 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20 20  nd jump to P2.  
11e2c 49 66 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68  If the top of th
11e2d 65 20 73 74 61 63 6b 20 69 73 20 6e 75 6d 65 72  e stack is numer
11e2e 69 63 20 74 68 65 6e 0a 2a 2a 20 63 6f 6e 76 65  ic then.** conve
11e2f 72 74 20 69 74 20 69 6e 74 6f 20 74 68 65 20 6c  rt it into the l
11e30 65 61 73 74 20 69 6e 74 65 67 65 72 20 74 68 61  east integer tha
11e31 74 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  t is greater tha
11e32 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 69 74  n or equal to it
11e33 73 0a 2a 2a 20 63 75 72 72 65 6e 74 20 76 61 6c  s.** current val
11e34 75 65 20 69 66 20 50 31 3d 3d 30 2c 20 6f 72 20  ue if P1==0, or 
11e35 74 6f 20 74 68 65 20 6c 65 61 73 74 20 69 6e 74  to the least int
11e36 65 67 65 72 20 74 68 61 74 20 69 73 20 73 74 72  eger that is str
11e37 69 63 74 6c 79 0a 2a 2a 20 67 72 65 61 74 65 72  ictly.** greater
11e38 20 74 68 61 6e 20 69 74 73 20 63 75 72 72 65 6e   than its curren
11e39 74 20 76 61 6c 75 65 20 69 66 20 50 31 3d 3d 31  t value if P1==1
11e3a 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 46 6f 72  ..*/.case OP_For
11e3b 63 65 49 6e 74 3a 20 7b 20 20 20 20 20 20 20 20  ceInt: {        
11e3c 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a      /* no-push *
11e3d 2f 0a 20 20 69 36 34 20 76 3b 0a 20 20 61 73 73  /.  i64 v;.  ass
11e3e 65 72 74 28 20 70 54 6f 73 3e 3d 70 2d 3e 61 53  ert( pTos>=p->aS
11e3f 74 61 63 6b 20 29 3b 0a 20 20 61 70 70 6c 79 41  tack );.  applyA
11e40 66 66 69 6e 69 74 79 28 70 54 6f 73 2c 20 53 51  ffinity(pTos, SQ
11e41 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43  LITE_AFF_NUMERIC
11e42 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 69  , encoding);.  i
11e43 66 28 20 28 70 54 6f 73 2d 3e 66 6c 61 67 73 20  f( (pTos->flags 
11e44 26 20 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52  & (MEM_Int|MEM_R
11e45 65 61 6c 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20  eal))==0 ){.    
11e46 52 65 6c 65 61 73 65 28 70 54 6f 73 29 3b 0a 20  Release(pTos);. 
11e47 20 20 20 70 54 6f 73 2d 2d 3b 0a 20 20 20 20 70     pTos--;.    p
11e48 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b  c = pOp->p2 - 1;
11e49 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a  .    break;.  }.
11e4a 20 20 69 66 28 20 70 54 6f 73 2d 3e 66 6c 61 67    if( pTos->flag
11e4b 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20  s & MEM_Int ){. 
11e4c 20 20 20 76 20 3d 20 70 54 6f 73 2d 3e 75 2e 69     v = pTos->u.i
11e4d 20 2b 20 28 70 4f 70 2d 3e 70 31 21 3d 30 29 3b   + (pOp->p1!=0);
11e4e 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
11e4f 20 46 49 58 20 4d 45 3a 20 20 73 68 6f 75 6c 64   FIX ME:  should
11e50 20 74 68 69 73 20 6e 6f 74 20 62 65 20 61 73 73   this not be ass
11e51 65 72 74 28 20 70 54 6f 73 2d 3e 66 6c 61 67 73  ert( pTos->flags
11e52 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 20 3f 3f   & MEM_Real ) ??
11e53 3f 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  ? */.    sqlite3
11e54 56 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 70  VdbeMemRealify(p
11e55 54 6f 73 29 3b 0a 20 20 20 20 76 20 3d 20 28 69  Tos);.    v = (i
11e56 6e 74 29 70 54 6f 73 2d 3e 72 3b 0a 20 20 20 20  nt)pTos->r;.    
11e57 69 66 28 20 70 54 6f 73 2d 3e 72 3e 28 64 6f 75  if( pTos->r>(dou
11e58 62 6c 65 29 76 20 29 20 76 2b 2b 3b 0a 20 20 20  ble)v ) v++;.   
11e59 20 69 66 28 20 70 4f 70 2d 3e 70 31 20 26 26 20   if( pOp->p1 && 
11e5a 70 54 6f 73 2d 3e 72 3d 3d 28 64 6f 75 62 6c 65  pTos->r==(double
11e5b 29 76 20 29 20 76 2b 2b 3b 0a 20 20 7d 0a 20 20  )v ) v++;.  }.  
11e5c 52 65 6c 65 61 73 65 28 70 54 6f 73 29 3b 0a 20  Release(pTos);. 
11e5d 20 70 54 6f 73 2d 3e 75 2e 69 20 3d 20 76 3b 0a   pTos->u.i = v;.
11e5e 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 3d 20    pTos->flags = 
11e5f 4d 45 4d 5f 49 6e 74 3b 0a 20 20 62 72 65 61 6b  MEM_Int;.  break
11e60 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
11e61 4d 75 73 74 42 65 49 6e 74 20 50 31 20 50 32 20  MustBeInt P1 P2 
11e62 2a 0a 2a 2a 20 0a 2a 2a 20 46 6f 72 63 65 20 74  *.** .** Force t
11e63 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74  he top of the st
11e64 61 63 6b 20 74 6f 20 62 65 20 61 6e 20 69 6e 74  ack to be an int
11e65 65 67 65 72 2e 20 20 49 66 20 74 68 65 20 74 6f  eger.  If the to
11e66 70 20 6f 66 20 74 68 65 0a 2a 2a 20 73 74 61 63  p of the.** stac
11e67 6b 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65  k is not an inte
11e68 67 65 72 20 61 6e 64 20 63 61 6e 6e 6f 74 20 62  ger and cannot b
11e69 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f  e converted into
11e6a 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 77   an integer.** w
11e6b 69 74 68 20 6f 75 74 20 64 61 74 61 20 6c 6f 73  ith out data los
11e6c 73 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d  s, then jump imm
11e6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2c 20  ediately to P2, 
11e6e 6f 72 20 69 66 20 50 32 3d 3d 30 0a 2a 2a 20 72  or if P2==0.** r
11e6f 61 69 73 65 20 61 6e 20 53 51 4c 49 54 45 5f 4d  aise an SQLITE_M
11e70 49 53 4d 41 54 43 48 20 65 78 63 65 70 74 69 6f  ISMATCH exceptio
11e71 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  n..**.** If the 
11e72 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b  top of the stack
11e73 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67   is not an integ
11e74 65 72 20 61 6e 64 20 50 32 20 69 73 20 6e 6f 74  er and P2 is not
11e75 20 7a 65 72 6f 20 61 6e 64 0a 2a 2a 20 50 31 20   zero and.** P1 
11e76 69 73 20 31 2c 20 74 68 65 6e 20 74 68 65 20 73  is 1, then the s
11e77 74 61 63 6b 20 69 73 20 70 6f 70 70 65 64 2e 20  tack is popped. 
11e78 20 49 6e 20 61 6c 6c 20 6f 74 68 65 72 20 63 61   In all other ca
11e79 73 65 73 2c 20 74 68 65 20 64 65 70 74 68 0a 2a  ses, the depth.*
11e7a 2a 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20 69  * of the stack i
11e7b 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a  s unchanged..*/.
11e7c 63 61 73 65 20 4f 50 5f 4d 75 73 74 42 65 49 6e  case OP_MustBeIn
11e7d 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  t: {            
11e7e 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20  /* no-push */.  
11e7f 61 73 73 65 72 74 28 20 70 54 6f 73 3e 3d 70 2d  assert( pTos>=p-
11e80 3e 61 53 74 61 63 6b 20 29 3b 0a 20 20 61 70 70  >aStack );.  app
11e81 6c 79 41 66 66 69 6e 69 74 79 28 70 54 6f 73 2c  lyAffinity(pTos,
11e82 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45   SQLITE_AFF_NUME
11e83 52 49 43 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a  RIC, encoding);.
11e84 20 20 69 66 28 20 28 70 54 6f 73 2d 3e 66 6c 61    if( (pTos->fla
11e85 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d 30  gs & MEM_Int)==0
11e86 20 29 7b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d   ){.    if( pOp-
11e87 3e 70 32 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  >p2==0 ){.      
11e88 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 4d  rc = SQLITE_MISM
11e89 41 54 43 48 3b 0a 20 20 20 20 20 20 67 6f 74 6f  ATCH;.      goto
11e8a 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72   abort_due_to_er
11e8b 72 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ror;.    }else{.
11e8c 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70        if( pOp->p
11e8d 31 20 29 20 70 6f 70 53 74 61 63 6b 28 26 70 54  1 ) popStack(&pT
11e8e 6f 73 2c 20 31 29 3b 0a 20 20 20 20 20 20 70 63  os, 1);.      pc
11e8f 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
11e90 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
11e91 20 20 20 52 65 6c 65 61 73 65 28 70 54 6f 73 29     Release(pTos)
11e92 3b 0a 20 20 20 20 70 54 6f 73 2d 3e 66 6c 61 67  ;.    pTos->flag
11e93 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 7d  s = MEM_Int;.  }
11e94 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
11e95 4f 70 63 6f 64 65 3a 20 52 65 61 6c 41 66 66 69  Opcode: RealAffi
11e96 6e 69 74 79 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  nity * * *.**.**
11e97 20 49 66 20 74 68 65 20 74 6f 70 20 6f 66 20 74   If the top of t
11e98 68 65 20 73 74 61 63 6b 20 69 73 20 61 6e 20 69  he stack is an i
11e99 6e 74 65 67 65 72 2c 20 63 6f 6e 76 65 72 74 20  nteger, convert 
11e9a 69 74 20 74 6f 20 61 20 72 65 61 6c 20 76 61 6c  it to a real val
11e9b 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f  ue..**.** This o
11e9c 70 63 6f 64 65 20 69 73 20 75 73 65 64 20 77 68  pcode is used wh
11e9d 65 6e 20 65 78 74 72 61 63 74 69 6e 67 20 69 6e  en extracting in
11e9e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 61  formation from a
11e9f 20 63 6f 6c 75 6d 6e 20 74 68 61 74 0a 2a 2a 20   column that.** 
11ea0 68 61 73 20 52 45 41 4c 20 61 66 66 69 6e 69 74  has REAL affinit
11ea1 79 2e 20 20 53 75 63 68 20 63 6f 6c 75 6d 6e 20  y.  Such column 
11ea2 76 61 6c 75 65 73 20 6d 61 79 20 73 74 69 6c 6c  values may still
11ea3 20 62 65 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a   be stored as.**
11ea4 20 69 6e 74 65 67 65 72 73 2c 20 66 6f 72 20 73   integers, for s
11ea5 70 61 63 65 20 65 66 66 69 63 69 65 6e 63 79 2c  pace efficiency,
11ea6 20 62 75 74 20 61 66 74 65 72 20 65 78 74 72 61   but after extra
11ea7 63 74 69 6f 6e 20 77 65 20 77 61 6e 74 20 74 68  ction we want th
11ea8 65 6d 0a 2a 2a 20 74 6f 20 68 61 76 65 20 6f 6e  em.** to have on
11ea9 6c 79 20 61 20 72 65 61 6c 20 76 61 6c 75 65 2e  ly a real value.
11eaa 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 61 6c  .*/.case OP_Real
11eab 41 66 66 69 6e 69 74 79 3a 20 7b 20 20 20 20 20  Affinity: {     
11eac 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11ead 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 61 73 73  no-push */.  ass
11eae 65 72 74 28 20 70 54 6f 73 3e 3d 70 2d 3e 61 53  ert( pTos>=p->aS
11eaf 74 61 63 6b 20 29 3b 0a 20 20 69 66 28 20 70 54  tack );.  if( pT
11eb0 6f 73 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  os->flags & MEM_
11eb1 49 6e 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Int ){.    sqlit
11eb2 65 33 56 64 62 65 4d 65 6d 52 65 61 6c 69 66 79  e3VdbeMemRealify
11eb3 28 70 54 6f 73 29 3b 0a 20 20 7d 0a 20 20 62 72  (pTos);.  }.  br
11eb4 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  eak;.}..#ifndef 
11eb5 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54  SQLITE_OMIT_CAST
11eb6 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 54 65  ./* Opcode: ToTe
11eb7 78 74 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46  xt * * *.**.** F
11eb8 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65 20 6f  orce the value o
11eb9 6e 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65  n the top of the
11eba 20 73 74 61 63 6b 20 74 6f 20 62 65 20 74 65 78   stack to be tex
11ebb 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c  t..** If the val
11ebc 75 65 20 69 73 20 6e 75 6d 65 72 69 63 2c 20 63  ue is numeric, c
11ebd 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 20 73  onvert it to a s
11ebe 74 72 69 6e 67 20 75 73 69 6e 67 20 74 68 65 0a  tring using the.
11ebf 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f 66  ** equivalent of
11ec0 20 70 72 69 6e 74 66 28 29 2e 20 20 42 6c 6f 62   printf().  Blob
11ec1 20 76 61 6c 75 65 73 20 61 72 65 20 75 6e 63 68   values are unch
11ec2 61 6e 67 65 64 20 61 6e 64 0a 2a 2a 20 61 72 65  anged and.** are
11ec3 20 61 66 74 65 72 77 61 72 64 73 20 73 69 6d 70   afterwards simp
11ec4 6c 79 20 69 6e 74 65 72 70 72 65 74 65 64 20 61  ly interpreted a
11ec5 73 20 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 41 20  s text..**.** A 
11ec6 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f  NULL value is no
11ec7 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 69  t changed by thi
11ec8 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72  s routine.  It r
11ec9 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a  emains NULL..*/.
11eca 63 61 73 65 20 4f 50 5f 54 6f 54 65 78 74 3a 20  case OP_ToText: 
11ecb 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
11ecc 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
11ecd 5f 54 4f 5f 54 45 58 54 2c 20 6e 6f 2d 70 75 73  _TO_TEXT, no-pus
11ece 68 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  h */.  assert( p
11ecf 54 6f 73 3e 3d 70 2d 3e 61 53 74 61 63 6b 20 29  Tos>=p->aStack )
11ed0 3b 0a 20 20 69 66 28 20 70 54 6f 73 2d 3e 66 6c  ;.  if( pTos->fl
11ed1 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29  ags & MEM_Null )
11ed2 20 62 72 65 61 6b 3b 0a 20 20 61 73 73 65 72 74   break;.  assert
11ed3 28 20 4d 45 4d 5f 53 74 72 3d 3d 28 4d 45 4d 5f  ( MEM_Str==(MEM_
11ed4 42 6c 6f 62 3e 3e 33 29 20 29 3b 0a 20 20 70 54  Blob>>3) );.  pT
11ed5 6f 73 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 70 54  os->flags |= (pT
11ed6 6f 73 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 6c  os->flags&MEM_Bl
11ed7 6f 62 29 3e 3e 33 3b 0a 20 20 61 70 70 6c 79 41  ob)>>3;.  applyA
11ed8 66 66 69 6e 69 74 79 28 70 54 6f 73 2c 20 53 51  ffinity(pTos, SQ
11ed9 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 2c 20 65  LITE_AFF_TEXT, e
11eda 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 72 63 20 3d  ncoding);.  rc =
11edb 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 54 6f 73   ExpandBlob(pTos
11edc 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f  );.  assert( pTo
11edd 73 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53  s->flags & MEM_S
11ede 74 72 20 29 3b 0a 20 20 70 54 6f 73 2d 3e 66 6c  tr );.  pTos->fl
11edf 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 49 6e 74  ags &= ~(MEM_Int
11ee0 7c 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 42 6c  |MEM_Real|MEM_Bl
11ee1 6f 62 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  ob);.  break;.}.
11ee2 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 42 6c  ./* Opcode: ToBl
11ee3 6f 62 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46  ob * * *.**.** F
11ee4 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65 20 6f  orce the value o
11ee5 6e 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65  n the top of the
11ee6 20 73 74 61 63 6b 20 74 6f 20 62 65 20 61 20 42   stack to be a B
11ee7 4c 4f 42 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76  LOB..** If the v
11ee8 61 6c 75 65 20 69 73 20 6e 75 6d 65 72 69 63 2c  alue is numeric,
11ee9 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61   convert it to a
11eea 20 73 74 72 69 6e 67 20 66 69 72 73 74 2e 0a 2a   string first..*
11eeb 2a 20 53 74 72 69 6e 67 73 20 61 72 65 20 73 69  * Strings are si
11eec 6d 70 6c 79 20 72 65 69 6e 74 65 72 70 72 65 74  mply reinterpret
11eed 65 64 20 61 73 20 62 6c 6f 62 73 20 77 69 74 68  ed as blobs with
11eee 20 6e 6f 20 63 68 61 6e 67 65 0a 2a 2a 20 74 6f   no change.** to
11eef 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20   the underlying 
11ef0 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55  data..**.** A NU
11ef1 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20  LL value is not 
11ef2 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20  changed by this 
11ef3 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d  routine.  It rem
11ef4 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61  ains NULL..*/.ca
11ef5 73 65 20 4f 50 5f 54 6f 42 6c 6f 62 3a 20 7b 20  se OP_ToBlob: { 
11ef6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11ef7 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54   /* same as TK_T
11ef8 4f 5f 42 4c 4f 42 2c 20 6e 6f 2d 70 75 73 68 20  O_BLOB, no-push 
11ef9 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f  */.  assert( pTo
11efa 73 3e 3d 70 2d 3e 61 53 74 61 63 6b 20 29 3b 0a  s>=p->aStack );.
11efb 20 20 69 66 28 20 70 54 6f 73 2d 3e 66 6c 61 67    if( pTos->flag
11efc 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 20 62  s & MEM_Null ) b
11efd 72 65 61 6b 3b 0a 20 20 69 66 28 20 28 70 54 6f  reak;.  if( (pTo
11efe 73 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42  s->flags & MEM_B
11eff 6c 6f 62 29 3d 3d 30 20 29 7b 0a 20 20 20 20 61  lob)==0 ){.    a
11f00 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 54 6f  pplyAffinity(pTo
11f01 73 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45  s, SQLITE_AFF_TE
11f02 58 54 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20  XT, encoding);. 
11f03 20 20 20 61 73 73 65 72 74 28 20 70 54 6f 73 2d     assert( pTos-
11f04 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72  >flags & MEM_Str
11f05 20 29 3b 0a 20 20 20 20 70 54 6f 73 2d 3e 66 6c   );.    pTos->fl
11f06 61 67 73 20 7c 3d 20 4d 45 4d 5f 42 6c 6f 62 3b  ags |= MEM_Blob;
11f07 0a 20 20 7d 0a 20 20 70 54 6f 73 2d 3e 66 6c 61  .  }.  pTos->fla
11f08 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 49 6e 74 7c  gs &= ~(MEM_Int|
11f09 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 53 74 72  MEM_Real|MEM_Str
11f0a 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  );.  break;.}../
11f0b 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 4e 75 6d 65  * Opcode: ToNume
11f0c 72 69 63 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  ric * * *.**.** 
11f0d 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65 20  Force the value 
11f0e 6f 6e 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68  on the top of th
11f0f 65 20 73 74 61 63 6b 20 74 6f 20 62 65 20 6e 75  e stack to be nu
11f10 6d 65 72 69 63 20 28 65 69 74 68 65 72 20 61 6e  meric (either an
11f11 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6f 72 20 61  .** integer or a
11f12 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20   floating-point 
11f13 6e 75 6d 62 65 72 2e 29 0a 2a 2a 20 49 66 20 74  number.).** If t
11f14 68 65 20 76 61 6c 75 65 20 69 73 20 74 65 78 74  he value is text
11f15 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f   or blob, try to
11f16 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61   convert it to a
11f17 6e 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65  n using the.** e
11f18 71 75 69 76 61 6c 65 6e 74 20 6f 66 20 61 74 6f  quivalent of ato
11f19 69 28 29 20 6f 72 20 61 74 6f 66 28 29 20 61 6e  i() or atof() an
11f1a 64 20 73 74 6f 72 65 20 30 20 69 66 20 6e 6f 20  d store 0 if no 
11f1b 73 75 63 68 20 63 6f 6e 76 65 72 73 69 6f 6e 20  such conversion 
11f1c 0a 2a 2a 20 69 73 20 70 6f 73 73 69 62 6c 65 2e  .** is possible.
11f1d 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61  .**.** A NULL va
11f1e 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67  lue is not chang
11f1f 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69  ed by this routi
11f20 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20  ne.  It remains 
11f21 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  NULL..*/.case OP
11f22 5f 54 6f 4e 75 6d 65 72 69 63 3a 20 7b 20 20 20  _ToNumeric: {   
11f23 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
11f24 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f  * same as TK_TO_
11f25 4e 55 4d 45 52 49 43 2c 20 6e 6f 2d 70 75 73 68  NUMERIC, no-push
11f26 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 54   */.  assert( pT
11f27 6f 73 3e 3d 70 2d 3e 61 53 74 61 63 6b 20 29 3b  os>=p->aStack );
11f28 0a 20 20 69 66 28 20 28 70 54 6f 73 2d 3e 66 6c  .  if( (pTos->fl
11f29 61 67 73 20 26 20 28 4d 45 4d 5f 4e 75 6c 6c 7c  ags & (MEM_Null|
11f2a 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c  MEM_Int|MEM_Real
11f2b 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  ))==0 ){.    sql
11f2c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6d 65 72  ite3VdbeMemNumer
11f2d 69 66 79 28 70 54 6f 73 29 3b 0a 20 20 7d 0a 20  ify(pTos);.  }. 
11f2e 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66   break;.}.#endif
11f2f 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
11f30 43 41 53 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f  CAST */../* Opco
11f31 64 65 3a 20 54 6f 49 6e 74 20 2a 20 2a 20 2a 0a  de: ToInt * * *.
11f32 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20  **.** Force the 
11f33 76 61 6c 75 65 20 6f 6e 20 74 68 65 20 74 6f 70  value on the top
11f34 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20 74 6f   of the stack to
11f35 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72 2e 20   be an integer. 
11f36 20 49 66 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65   If.** The value
11f37 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 20   is currently a 
11f38 72 65 61 6c 20 6e 75 6d 62 65 72 2c 20 64 72 6f  real number, dro
11f39 70 20 69 74 73 20 66 72 61 63 74 69 6f 6e 61 6c  p its fractional
11f3a 20 70 61 72 74 2e 0a 2a 2a 20 49 66 20 74 68 65   part..** If the
11f3b 20 76 61 6c 75 65 20 69 73 20 74 65 78 74 20 6f   value is text o
11f3c 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f 20 63  r blob, try to c
11f3d 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 6e 20  onvert it to an 
11f3e 69 6e 74 65 67 65 72 20 75 73 69 6e 67 20 74 68  integer using th
11f3f 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20  e.** equivalent 
11f40 6f 66 20 61 74 6f 69 28 29 20 61 6e 64 20 73 74  of atoi() and st
11f41 6f 72 65 20 30 20 69 66 20 6e 6f 20 73 75 63 68  ore 0 if no such
11f42 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70   conversion is p
11f43 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41  ossible..**.** A
11f44 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e   NULL value is n
11f45 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 68  ot changed by th
11f46 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20  is routine.  It 
11f47 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f  remains NULL..*/
11f48 0a 63 61 73 65 20 4f 50 5f 54 6f 49 6e 74 3a 20  .case OP_ToInt: 
11f49 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
11f4a 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
11f4b 5f 54 4f 5f 49 4e 54 2c 20 6e 6f 2d 70 75 73 68  _TO_INT, no-push
11f4c 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 54   */.  assert( pT
11f4d 6f 73 3e 3d 70 2d 3e 61 53 74 61 63 6b 20 29 3b  os>=p->aStack );
11f4e 0a 20 20 69 66 28 20 28 70 54 6f 73 2d 3e 66 6c  .  if( (pTos->fl
11f4f 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d  ags & MEM_Null)=
11f50 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
11f51 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69  3VdbeMemIntegeri
11f52 66 79 28 70 54 6f 73 29 3b 0a 20 20 7d 0a 20 20  fy(pTos);.  }.  
11f53 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 65  break;.}..#ifnde
11f54 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41  f SQLITE_OMIT_CA
11f55 53 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f  ST./* Opcode: To
11f56 52 65 61 6c 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  Real * * *.**.**
11f57 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65   Force the value
11f58 20 6f 6e 20 74 68 65 20 74 6f 70 20 6f 66 20 74   on the top of t
11f59 68 65 20 73 74 61 63 6b 20 74 6f 20 62 65 20 61  he stack to be a
11f5a 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20   floating point 
11f5b 6e 75 6d 62 65 72 2e 0a 2a 2a 20 49 66 20 54 68  number..** If Th
11f5c 65 20 76 61 6c 75 65 20 69 73 20 63 75 72 72 65  e value is curre
11f5d 6e 74 6c 79 20 61 6e 20 69 6e 74 65 67 65 72 2c  ntly an integer,
11f5e 20 63 6f 6e 76 65 72 74 20 69 74 2e 0a 2a 2a 20   convert it..** 
11f5f 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 20  If the value is 
11f60 74 65 78 74 20 6f 72 20 62 6c 6f 62 2c 20 74 72  text or blob, tr
11f61 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 69 74 20  y to convert it 
11f62 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 75 73  to an integer us
11f63 69 6e 67 20 74 68 65 0a 2a 2a 20 65 71 75 69 76  ing the.** equiv
11f64 61 6c 65 6e 74 20 6f 66 20 61 74 6f 69 28 29 20  alent of atoi() 
11f65 61 6e 64 20 73 74 6f 72 65 20 30 20 69 66 20 6e  and store 0 if n
11f66 6f 20 73 75 63 68 20 63 6f 6e 76 65 72 73 69 6f  o such conversio
11f67 6e 20 69 73 20 70 6f 73 73 69 62 6c 65 2e 0a 2a  n is possible..*
11f68 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75  *.** A NULL valu
11f69 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 64  e is not changed
11f6a 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65   by this routine
11f6b 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20 4e 55  .  It remains NU
11f6c 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54  LL..*/.case OP_T
11f6d 6f 52 65 61 6c 3a 20 7b 20 20 20 20 20 20 20 20  oReal: {        
11f6e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d            /* sam
11f6f 65 20 61 73 20 54 4b 5f 54 4f 5f 52 45 41 4c 2c  e as TK_TO_REAL,
11f70 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 61 73   no-push */.  as
11f71 73 65 72 74 28 20 70 54 6f 73 3e 3d 70 2d 3e 61  sert( pTos>=p->a
11f72 53 74 61 63 6b 20 29 3b 0a 20 20 69 66 28 20 28  Stack );.  if( (
11f73 70 54 6f 73 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pTos->flags & ME
11f74 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20  M_Null)==0 ){.  
11f75 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
11f76 52 65 61 6c 69 66 79 28 70 54 6f 73 29 3b 0a 20  Realify(pTos);. 
11f77 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65   }.  break;.}.#e
11f78 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
11f79 4d 49 54 5f 43 41 53 54 20 2a 2f 0a 0a 2f 2a 20  MIT_CAST */../* 
11f7a 4f 70 63 6f 64 65 3a 20 45 71 20 50 31 20 50 32  Opcode: Eq P1 P2
11f7b 20 50 33 0a 2a 2a 0a 2a 2a 20 50 6f 70 20 74 68   P3.**.** Pop th
11f7c 65 20 74 6f 70 20 74 77 6f 20 65 6c 65 6d 65 6e  e top two elemen
11f7d 74 73 20 66 72 6f 6d 20 74 68 65 20 73 74 61 63  ts from the stac
11f7e 6b 2e 20 20 49 66 20 74 68 65 79 20 61 72 65 20  k.  If they are 
11f7f 65 71 75 61 6c 2c 20 74 68 65 6e 0a 2a 2a 20 6a  equal, then.** j
11f80 75 6d 70 20 74 6f 20 69 6e 73 74 72 75 63 74 69  ump to instructi
11f81 6f 6e 20 50 32 2e 20 20 4f 74 68 65 72 77 69 73  on P2.  Otherwis
11f82 65 2c 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 74  e, continue to t
11f83 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74  he next instruct
11f84 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ion..**.** If th
11f85 65 20 30 78 31 30 30 20 62 69 74 20 6f 66 20 50  e 0x100 bit of P
11f86 31 20 69 73 20 74 72 75 65 20 61 6e 64 20 65 69  1 is true and ei
11f87 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20  ther operand is 
11f88 4e 55 4c 4c 20 74 68 65 6e 20 74 61 6b 65 20 74  NULL then take t
11f89 68 65 0a 2a 2a 20 6a 75 6d 70 2e 20 20 49 66 20  he.** jump.  If 
11f8a 74 68 65 20 30 78 31 30 30 20 62 69 74 20 6f 66  the 0x100 bit of
11f8b 20 50 31 20 69 73 20 63 6c 65 61 72 20 74 68 65   P1 is clear the
11f8c 6e 20 66 61 6c 6c 20 74 68 72 75 20 69 66 20 65  n fall thru if e
11f8d 69 74 68 65 72 20 6f 70 65 72 61 6e 64 0a 2a 2a  ither operand.**
11f8e 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20   is NULL..**.** 
11f8f 49 66 20 74 68 65 20 30 78 32 30 30 20 62 69 74  If the 0x200 bit
11f90 20 6f 66 20 50 31 20 69 73 20 73 65 74 20 61 6e   of P1 is set an
11f91 64 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64  d either operand
11f92 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 0a 2a 2a   is NULL then.**
11f93 20 62 6f 74 68 20 6f 70 65 72 61 6e 64 73 20 61   both operands a
11f94 72 65 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 20  re converted to 
11f95 69 6e 74 65 67 65 72 73 20 70 72 69 6f 72 20 74  integers prior t
11f96 6f 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 0a 2a 2a  o comparison..**
11f97 20 4e 55 4c 4c 20 6f 70 65 72 61 6e 64 73 20 61   NULL operands a
11f98 72 65 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 20  re converted to 
11f99 7a 65 72 6f 20 61 6e 64 20 6e 6f 6e 2d 4e 55 4c  zero and non-NUL
11f9a 4c 20 6f 70 65 72 61 6e 64 73 20 61 72 65 0a 2a  L operands are.*
11f9b 2a 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 31  * converted to 1
11f9c 2e 20 20 54 68 75 73 2c 20 66 6f 72 20 65 78 61  .  Thus, for exa
11f9d 6d 70 6c 65 2c 20 77 69 74 68 20 30 78 32 30 30  mple, with 0x200
11f9e 20 73 65 74 2c 20 20 4e 55 4c 4c 3d 3d 4e 55 4c   set,  NULL==NUL
11f9f 4c 20 69 73 20 74 72 75 65 0a 2a 2a 20 77 68 65  L is true.** whe
11fa0 72 65 61 73 20 69 74 20 77 6f 75 6c 64 20 6e 6f  reas it would no
11fa1 72 6d 61 6c 6c 79 20 62 65 20 4e 55 4c 4c 2e 20  rmally be NULL. 
11fa2 20 53 69 6d 69 6c 61 72 6c 79 2c 20 20 4e 55 4c   Similarly,  NUL
11fa3 4c 3d 3d 31 32 33 20 69 73 20 66 61 6c 73 65 20  L==123 is false 
11fa4 77 68 65 6e 0a 2a 2a 20 30 78 32 30 30 20 69 73  when.** 0x200 is
11fa5 20 73 65 74 20 62 75 74 20 69 73 20 4e 55 4c 4c   set but is NULL
11fa6 20 77 68 65 6e 20 74 68 65 20 30 78 32 30 30 20   when the 0x200 
11fa7 62 69 74 20 6f 66 20 50 31 20 69 73 20 63 6c 65  bit of P1 is cle
11fa8 61 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 65  ar..**.** The le
11fa9 61 73 74 20 73 69 67 6e 69 66 69 63 61 6e 74 20  ast significant 
11faa 62 79 74 65 20 6f 66 20 50 31 20 28 6d 61 73 6b  byte of P1 (mask
11fab 20 30 78 66 66 29 20 6d 75 73 74 20 62 65 20 61   0xff) must be a
11fac 6e 20 61 66 66 69 6e 69 74 79 20 63 68 61 72 61  n affinity chara
11fad 63 74 65 72 20 2d 0a 2a 2a 20 53 51 4c 49 54 45  cter -.** SQLITE
11fae 5f 41 46 46 5f 54 45 58 54 2c 20 53 51 4c 49 54  _AFF_TEXT, SQLIT
11faf 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 2c 20 61  E_AFF_INTEGER, a
11fb0 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 41 6e 20  nd so forth. An 
11fb1 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20  attempt is made 
11fb2 0a 2a 2a 20 74 6f 20 63 6f 65 72 63 65 20 62 6f  .** to coerce bo
11fb3 74 68 20 76 61 6c 75 65 73 0a 2a 2a 20 61 63 63  th values.** acc
11fb4 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 61 66  ording to the af
11fb5 66 69 6e 69 74 79 20 62 65 66 6f 72 65 20 74 68  finity before th
11fb6 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20  e comparison is 
11fb7 6d 61 64 65 2e 20 49 66 20 74 68 65 20 62 79 74  made. If the byt
11fb8 65 20 69 73 0a 2a 2a 20 30 78 30 30 2c 20 74 68  e is.** 0x00, th
11fb9 65 6e 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e  en numeric affin
11fba 69 74 79 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a  ity is used..**.
11fbb 2a 2a 20 4f 6e 63 65 20 61 6e 79 20 63 6f 6e 76  ** Once any conv
11fbc 65 72 73 69 6f 6e 73 20 68 61 76 65 20 74 61 6b  ersions have tak
11fbd 65 6e 20 70 6c 61 63 65 2c 20 61 6e 64 20 6e 65  en place, and ne
11fbe 69 74 68 65 72 20 76 61 6c 75 65 20 69 73 20 4e  ither value is N
11fbf 55 4c 4c 2c 20 0a 2a 2a 20 74 68 65 20 76 61 6c  ULL, .** the val
11fc0 75 65 73 20 61 72 65 20 63 6f 6d 70 61 72 65 64  ues are compared
11fc1 2e 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73  . If both values
11fc2 20 61 72 65 20 62 6c 6f 62 73 2c 20 6f 72 20 62   are blobs, or b
11fc3 6f 74 68 20 61 72 65 20 74 65 78 74 2c 0a 2a 2a  oth are text,.**
11fc4 20 74 68 65 6e 20 6d 65 6d 63 6d 70 28 29 20 69   then memcmp() i
11fc5 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d  s used to determ
11fc6 69 6e 65 20 74 68 65 20 72 65 73 75 6c 74 73 20  ine the results 
11fc7 6f 66 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  of the compariso
11fc8 6e 2e 20 49 66 0a 2a 2a 20 62 6f 74 68 20 76 61  n. If.** both va
11fc9 6c 75 65 73 20 61 72 65 20 6e 75 6d 65 72 69 63  lues are numeric
11fca 2c 20 74 68 65 6e 20 61 20 6e 75 6d 65 72 69 63  , then a numeric
11fcb 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 75   comparison is u
11fcc 73 65 64 2e 20 49 66 20 74 68 65 0a 2a 2a 20 74  sed. If the.** t
11fcd 77 6f 20 76 61 6c 75 65 73 20 61 72 65 20 6f 66  wo values are of
11fce 20 64 69 66 66 65 72 65 6e 74 20 74 79 70 65 73   different types
11fcf 2c 20 74 68 65 6e 20 74 68 65 79 20 61 72 65 20  , then they are 
11fd0 69 6e 65 71 75 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49  inequal..**.** I
11fd1 66 20 50 32 20 69 73 20 7a 65 72 6f 2c 20 64 6f  f P2 is zero, do
11fd2 20 6e 6f 74 20 6a 75 6d 70 2e 20 20 49 6e 73 74   not jump.  Inst
11fd3 65 61 64 2c 20 70 75 73 68 20 61 6e 20 69 6e 74  ead, push an int
11fd4 65 67 65 72 20 31 20 6f 6e 74 6f 20 74 68 65 0a  eger 1 onto the.
11fd5 2a 2a 20 73 74 61 63 6b 20 69 66 20 74 68 65 20  ** stack if the 
11fd6 6a 75 6d 70 20 77 6f 75 6c 64 20 68 61 76 65 20  jump would have 
11fd7 62 65 65 6e 20 74 61 6b 65 6e 2c 20 6f 72 20 61  been taken, or a
11fd8 20 30 20 69 66 20 6e 6f 74 2e 20 20 50 75 73 68   0 if not.  Push
11fd9 20 61 0a 2a 2a 20 4e 55 4c 4c 20 69 66 20 65 69   a.** NULL if ei
11fda 74 68 65 72 20 6f 70 65 72 61 6e 64 20 77 61 73  ther operand was
11fdb 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   NULL..**.** If 
11fdc 50 33 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 69  P3 is not NULL i
11fdd 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  t is a pointer t
11fde 6f 20 61 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  o a collating se
11fdf 71 75 65 6e 63 65 20 28 61 20 43 6f 6c 6c 53 65  quence (a CollSe
11fe0 71 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 29 20  q.** structure) 
11fe1 74 68 61 74 20 64 65 66 69 6e 65 73 20 68 6f 77  that defines how
11fe2 20 74 6f 20 63 6f 6d 70 61 72 65 20 74 65 78 74   to compare text
11fe3 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ..*/./* Opcode: 
11fe4 4e 65 20 50 31 20 50 32 20 50 33 0a 2a 2a 0a 2a  Ne P1 P2 P3.**.*
11fe5 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73  * This works jus
11fe6 74 20 6c 69 6b 65 20 74 68 65 20 45 71 20 6f 70  t like the Eq op
11fe7 63 6f 64 65 20 65 78 63 65 70 74 20 74 68 61 74  code except that
11fe8 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b   the jump is tak
11fe9 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 6f 70 65  en if.** the ope
11fea 72 61 6e 64 73 20 66 72 6f 6d 20 74 68 65 20 73  rands from the s
11feb 74 61 63 6b 20 61 72 65 20 6e 6f 74 20 65 71 75  tack are not equ
11fec 61 6c 2e 20 20 53 65 65 20 74 68 65 20 45 71 20  al.  See the Eq 
11fed 6f 70 63 6f 64 65 20 66 6f 72 0a 2a 2a 20 61 64  opcode for.** ad
11fee 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61  ditional informa
11fef 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f  tion..*/./* Opco
11ff0 64 65 3a 20 4c 74 20 50 31 20 50 32 20 50 33 0a  de: Lt P1 P2 P3.
11ff1 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73  **.** This works
11ff2 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 45   just like the E
11ff3 71 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20  q opcode except 
11ff4 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73  that the jump is
11ff5 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65   taken if.** the
11ff6 20 32 6e 64 20 65 6c 65 6d 65 6e 74 20 64 6f 77   2nd element dow
11ff7 6e 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 20 69  n on the stack i
11ff8 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20  s less than the 
11ff9 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b  top of the stack
11ffa 2e 0a 2a 2a 20 53 65 65 20 74 68 65 20 45 71 20  ..** See the Eq 
11ffb 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74  opcode for addit
11ffc 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
11ffd 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a  n..*/./* Opcode:
11ffe 20 4c 65 20 50 31 20 50 32 20 50 33 0a 2a 2a 0a   Le P1 P2 P3.**.
11fff 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75  ** This works ju
12000 73 74 20 6c 69 6b 65 20 74 68 65 20 45 71 20 6f  st like the Eq o
12001 70 63 6f 64 65 20 65 78 63 65 70 74 20 74 68 61  pcode except tha
12002 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61  t the jump is ta
12003 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 32 6e  ken if.** the 2n
12004 64 20 65 6c 65 6d 65 6e 74 20 64 6f 77 6e 20 6f  d element down o
12005 6e 20 74 68 65 20 73 74 61 63 6b 20 69 73 20 6c  n the stack is l
12006 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61  ess than or equa
12007 6c 20 74 6f 20 74 68 65 0a 2a 2a 20 74 6f 70 20  l to the.** top 
12008 6f 66 20 74 68 65 20 73 74 61 63 6b 2e 20 20 53  of the stack.  S
12009 65 65 20 74 68 65 20 45 71 20 6f 70 63 6f 64 65  ee the Eq opcode
1200a 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20   for additional 
1200b 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a  information..*/.
1200c 2f 2a 20 4f 70 63 6f 64 65 3a 20 47 74 20 50 31  /* Opcode: Gt P1
1200d 20 50 32 20 50 33 0a 2a 2a 0a 2a 2a 20 54 68 69   P2 P3.**.** Thi
1200e 73 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b  s works just lik
1200f 65 20 74 68 65 20 45 71 20 6f 70 63 6f 64 65 20  e the Eq opcode 
12010 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20  except that the 
12011 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66  jump is taken if
12012 0a 2a 2a 20 74 68 65 20 32 6e 64 20 65 6c 65 6d  .** the 2nd elem
12013 65 6e 74 20 64 6f 77 6e 20 6f 6e 20 74 68 65 20  ent down on the 
12014 73 74 61 63 6b 20 69 73 20 67 72 65 61 74 65 72  stack is greater
12015 20 74 68 61 6e 20 74 68 65 20 74 6f 70 20 6f 66   than the top of
12016 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2a 20 53   the stack..** S
12017 65 65 20 74 68 65 20 45 71 20 6f 70 63 6f 64 65  ee the Eq opcode
12018 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20   for additional 
12019 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a  information..*/.
1201a 2f 2a 20 4f 70 63 6f 64 65 3a 20 47 65 20 50 31  /* Opcode: Ge P1
1201b 20 50 32 20 50 33 0a 2a 2a 0a 2a 2a 20 54 68 69   P2 P3.**.** Thi
1201c 73 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b  s works just lik
1201d 65 20 74 68 65 20 45 71 20 6f 70 63 6f 64 65 20  e the Eq opcode 
1201e 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20  except that the 
1201f 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66  jump is taken if
12020 0a 2a 2a 20 74 68 65 20 32 6e 64 20 65 6c 65 6d  .** the 2nd elem
12021 65 6e 74 20 64 6f 77 6e 20 6f 6e 20 74 68 65 20  ent down on the 
12022 73 74 61 63 6b 20 69 73 20 67 72 65 61 74 65 72  stack is greater
12023 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
12024 6f 20 74 68 65 0a 2a 2a 20 74 6f 70 20 6f 66 20  o the.** top of 
12025 74 68 65 20 73 74 61 63 6b 2e 20 20 53 65 65 20  the stack.  See 
12026 74 68 65 20 45 71 20 6f 70 63 6f 64 65 20 66 6f  the Eq opcode fo
12027 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66  r additional inf
12028 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73  ormation..*/.cas
12029 65 20 4f 50 5f 45 71 3a 20 20 20 20 20 20 20 20  e OP_Eq:        
1202a 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
1202b 73 20 54 4b 5f 45 51 2c 20 6e 6f 2d 70 75 73 68  s TK_EQ, no-push
1202c 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 3a 20   */.case OP_Ne: 
1202d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1202e 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 45 2c 20   same as TK_NE, 
1202f 6e 6f 2d 70 75 73 68 20 2a 2f 0a 63 61 73 65 20  no-push */.case 
12030 4f 50 5f 4c 74 3a 20 20 20 20 20 20 20 20 20 20  OP_Lt:          
12031 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
12032 54 4b 5f 4c 54 2c 20 6e 6f 2d 70 75 73 68 20 2a  TK_LT, no-push *
12033 2f 0a 63 61 73 65 20 4f 50 5f 4c 65 3a 20 20 20  /.case OP_Le:   
12034 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
12035 61 6d 65 20 61 73 20 54 4b 5f 4c 45 2c 20 6e 6f  ame as TK_LE, no
12036 2d 70 75 73 68 20 2a 2f 0a 63 61 73 65 20 4f 50  -push */.case OP
12037 5f 47 74 3a 20 20 20 20 20 20 20 20 20 20 20 20  _Gt:            
12038 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
12039 5f 47 54 2c 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a  _GT, no-push */.
1203a 63 61 73 65 20 4f 50 5f 47 65 3a 20 7b 20 20 20  case OP_Ge: {   
1203b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d            /* sam
1203c 65 20 61 73 20 54 4b 5f 47 45 2c 20 6e 6f 2d 70  e as TK_GE, no-p
1203d 75 73 68 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4e  ush */.  Mem *pN
1203e 6f 73 3b 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b  os;.  int flags;
1203f 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 63 68  .  int res;.  ch
12040 61 72 20 61 66 66 69 6e 69 74 79 3b 0a 0a 20 20  ar affinity;..  
12041 70 4e 6f 73 20 3d 20 26 70 54 6f 73 5b 2d 31 5d  pNos = &pTos[-1]
12042 3b 0a 20 20 66 6c 61 67 73 20 3d 20 70 54 6f 73  ;.  flags = pTos
12043 2d 3e 66 6c 61 67 73 7c 70 4e 6f 73 2d 3e 66 6c  ->flags|pNos->fl
12044 61 67 73 3b 0a 0a 20 20 2f 2a 20 49 66 20 65 69  ags;..  /* If ei
12045 74 68 65 72 20 76 61 6c 75 65 20 69 73 20 61 20  ther value is a 
12046 4e 55 4c 4c 20 50 32 20 69 73 20 6e 6f 74 20 7a  NULL P2 is not z
12047 65 72 6f 2c 20 74 61 6b 65 20 74 68 65 20 6a 75  ero, take the ju
12048 6d 70 20 69 66 20 74 68 65 20 6c 65 61 73 74 0a  mp if the least.
12049 20 20 2a 2a 20 73 69 67 6e 69 66 69 63 61 6e 74    ** significant
1204a 20 62 79 74 65 20 6f 66 20 50 31 20 69 73 20 74   byte of P1 is t
1204b 72 75 65 2e 20 49 66 20 50 32 20 69 73 20 7a 65  rue. If P2 is ze
1204c 72 6f 2c 20 74 68 65 6e 20 70 75 73 68 20 61 20  ro, then push a 
1204d 4e 55 4c 4c 20 6f 6e 74 6f 0a 20 20 2a 2a 20 74  NULL onto.  ** t
1204e 68 65 20 73 74 61 63 6b 2e 0a 20 20 2a 2f 0a 20  he stack..  */. 
1204f 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 4e   if( flags&MEM_N
12050 75 6c 6c 20 29 7b 0a 20 20 20 20 69 66 28 20 28  ull ){.    if( (
12051 70 4f 70 2d 3e 70 31 20 26 20 30 78 32 30 30 29  pOp->p1 & 0x200)
12052 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  !=0 ){.      /* 
12053 54 68 65 20 30 78 32 30 30 20 62 69 74 20 6f 66  The 0x200 bit of
12054 20 50 31 20 6d 65 61 6e 73 2c 20 72 6f 75 67 68   P1 means, rough
12055 6c 79 20 22 64 6f 20 6e 6f 74 20 74 72 65 61 74  ly "do not treat
12056 20 4e 55 4c 4c 20 61 73 20 74 68 65 0a 20 20 20   NULL as the.   
12057 20 20 20 2a 2a 20 6d 61 67 69 63 20 53 51 4c 20     ** magic SQL 
12058 76 61 6c 75 65 20 69 74 20 6e 6f 72 6d 61 6c 6c  value it normall
12059 79 20 69 73 20 2d 20 74 72 65 61 74 20 69 74 20  y is - treat it 
1205a 61 73 20 69 66 20 69 74 20 77 65 72 65 20 61 6e  as if it were an
1205b 6f 74 68 65 72 0a 20 20 20 20 20 20 2a 2a 20 69  other.      ** i
1205c 6e 74 65 67 65 72 22 2e 0a 20 20 20 20 20 20 2a  nteger"..      *
1205d 2a 0a 20 20 20 20 20 20 2a 2a 20 57 69 74 68 20  *.      ** With 
1205e 30 78 32 30 30 20 73 65 74 2c 20 69 66 20 65 69  0x200 set, if ei
1205f 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20  ther operand is 
12060 4e 55 4c 4c 20 74 68 65 6e 20 62 6f 74 68 20 6f  NULL then both o
12061 70 65 72 61 6e 64 73 0a 20 20 20 20 20 20 2a 2a  perands.      **
12062 20 61 72 65 20 63 6f 6e 76 65 72 74 65 64 20 74   are converted t
12063 6f 20 69 6e 74 65 67 65 72 73 20 70 72 69 6f 72  o integers prior
12064 20 74 6f 20 62 65 69 6e 67 20 70 61 73 73 65 64   to being passed
12065 20 64 6f 77 6e 20 69 6e 74 6f 20 74 68 65 0a 20   down into the. 
12066 20 20 20 20 20 2a 2a 20 6e 6f 72 6d 61 6c 20 63       ** normal c
12067 6f 6d 70 61 72 69 73 6f 6e 20 6c 6f 67 69 63 20  omparison logic 
12068 62 65 6c 6f 77 2e 20 20 4e 55 4c 4c 20 6f 70 65  below.  NULL ope
12069 72 61 6e 64 73 20 61 72 65 20 63 6f 6e 76 65 72  rands are conver
1206a 74 65 64 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20  ted to.      ** 
1206b 7a 65 72 6f 20 61 6e 64 20 6e 6f 6e 2d 4e 55 4c  zero and non-NUL
1206c 4c 20 6f 70 65 72 61 6e 64 73 20 61 72 65 20 63  L operands are c
1206d 6f 6e 76 65 72 74 65 64 20 74 6f 20 31 2e 20 20  onverted to 1.  
1206e 54 68 75 73 2c 20 66 6f 72 20 65 78 61 6d 70 6c  Thus, for exampl
1206f 65 2c 0a 20 20 20 20 20 20 2a 2a 20 77 69 74 68  e,.      ** with
12070 20 30 78 32 30 30 20 73 65 74 2c 20 20 4e 55 4c   0x200 set,  NUL
12071 4c 3d 3d 4e 55 4c 4c 20 69 73 20 74 72 75 65 20  L==NULL is true 
12072 77 68 65 72 65 61 73 20 69 74 20 77 6f 75 6c 64  whereas it would
12073 20 6e 6f 72 6d 61 6c 6c 79 0a 20 20 20 20 20 20   normally.      
12074 2a 2a 20 62 65 20 4e 55 4c 4c 2e 20 20 53 69 6d  ** be NULL.  Sim
12075 69 6c 61 72 6c 79 2c 20 20 4e 55 4c 4c 21 3d 31  ilarly,  NULL!=1
12076 32 33 20 69 73 20 74 72 75 65 2e 0a 20 20 20 20  23 is true..    
12077 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74    */.      sqlit
12078 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36  e3VdbeMemSetInt6
12079 34 28 70 54 6f 73 2c 20 28 70 54 6f 73 2d 3e 66  4(pTos, (pTos->f
1207a 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29  lags & MEM_Null)
1207b 3d 3d 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ==0);.      sqli
1207c 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74  te3VdbeMemSetInt
1207d 36 34 28 70 4e 6f 73 2c 20 28 70 4e 6f 73 2d 3e  64(pNos, (pNos->
1207e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c  flags & MEM_Null
1207f 29 3d 3d 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65  )==0);.    }else
12080 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68  {.      /* If th
12081 65 20 30 78 32 30 30 20 62 69 74 20 6f 66 20 50  e 0x200 bit of P
12082 31 20 69 73 20 63 6c 65 61 72 20 61 6e 64 20 65  1 is clear and e
12083 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73  ither operand is
12084 20 4e 55 4c 4c 20 74 68 65 6e 0a 20 20 20 20 20   NULL then.     
12085 20 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20 69   ** the result i
12086 73 20 61 6c 77 61 79 73 20 4e 55 4c 4c 2e 20 20  s always NULL.  
12087 54 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65  The jump is take
12088 6e 20 69 66 20 74 68 65 20 30 78 31 30 30 20 62  n if the 0x100 b
12089 69 74 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 50  it.      ** of P
1208a 31 20 69 73 20 73 65 74 2e 0a 20 20 20 20 20 20  1 is set..      
1208b 2a 2f 0a 20 20 20 20 20 20 70 6f 70 53 74 61 63  */.      popStac
1208c 6b 28 26 70 54 6f 73 2c 20 32 29 3b 0a 20 20 20  k(&pTos, 2);.   
1208d 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 20 29     if( pOp->p2 )
1208e 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4f  {.        if( pO
1208f 70 2d 3e 70 31 20 26 20 30 78 31 30 30 20 29 7b  p->p1 & 0x100 ){
12090 0a 20 20 20 20 20 20 20 20 20 20 70 63 20 3d 20  .          pc = 
12091 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 20 20 20  pOp->p2-1;.     
12092 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
12093 7b 0a 20 20 20 20 20 20 20 20 70 54 6f 73 2b 2b  {.        pTos++
12094 3b 0a 20 20 20 20 20 20 20 20 70 54 6f 73 2d 3e  ;.        pTos->
12095 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
12096 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
12097 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
12098 0a 0a 20 20 61 66 66 69 6e 69 74 79 20 3d 20 70  ..  affinity = p
12099 4f 70 2d 3e 70 31 20 26 20 30 78 46 46 3b 0a 20  Op->p1 & 0xFF;. 
1209a 20 69 66 28 20 61 66 66 69 6e 69 74 79 20 29 7b   if( affinity ){
1209b 0a 20 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69  .    applyAffini
1209c 74 79 28 70 4e 6f 73 2c 20 61 66 66 69 6e 69 74  ty(pNos, affinit
1209d 79 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20  y, encoding);.  
1209e 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28    applyAffinity(
1209f 70 54 6f 73 2c 20 61 66 66 69 6e 69 74 79 2c 20  pTos, affinity, 
120a0 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 7d 0a 0a  encoding);.  }..
120a1 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
120a2 33 74 79 70 65 3d 3d 50 33 5f 43 4f 4c 4c 53 45  3type==P3_COLLSE
120a3 51 20 7c 7c 20 70 4f 70 2d 3e 70 33 3d 3d 30 20  Q || pOp->p3==0 
120a4 29 3b 0a 20 20 45 78 70 61 6e 64 42 6c 6f 62 28  );.  ExpandBlob(
120a5 70 4e 6f 73 29 3b 0a 20 20 45 78 70 61 6e 64 42  pNos);.  ExpandB
120a6 6c 6f 62 28 70 54 6f 73 29 3b 0a 20 20 72 65 73  lob(pTos);.  res
120a7 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d   = sqlite3MemCom
120a8 70 61 72 65 28 70 4e 6f 73 2c 20 70 54 6f 73 2c  pare(pNos, pTos,
120a9 20 28 43 6f 6c 6c 53 65 71 2a 29 70 4f 70 2d 3e   (CollSeq*)pOp->
120aa 70 33 29 3b 0a 20 20 73 77 69 74 63 68 28 20 70  p3);.  switch( p
120ab 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20  Op->opcode ){.  
120ac 20 20 63 61 73 65 20 4f 50 5f 45 71 3a 20 20 20    case OP_Eq:   
120ad 20 72 65 73 20 3d 20 72 65 73 3d 3d 30 3b 20 20   res = res==0;  
120ae 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
120af 73 65 20 4f 50 5f 4e 65 3a 20 20 20 20 72 65 73  se OP_Ne:    res
120b0 20 3d 20 72 65 73 21 3d 30 3b 20 20 20 20 20 62   = res!=0;     b
120b1 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f  reak;.    case O
120b2 50 5f 4c 74 3a 20 20 20 20 72 65 73 20 3d 20 72  P_Lt:    res = r
120b3 65 73 3c 30 3b 20 20 20 20 20 20 62 72 65 61 6b  es<0;      break
120b4 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 4c 65  ;.    case OP_Le
120b5 3a 20 20 20 20 72 65 73 20 3d 20 72 65 73 3c 3d  :    res = res<=
120b6 30 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  0;     break;.  
120b7 20 20 63 61 73 65 20 4f 50 5f 47 74 3a 20 20 20    case OP_Gt:   
120b8 20 72 65 73 20 3d 20 72 65 73 3e 30 3b 20 20 20   res = res>0;   
120b9 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65     break;.    de
120ba 66 61 75 6c 74 3a 20 20 20 20 20 20 20 72 65 73  fault:       res
120bb 20 3d 20 72 65 73 3e 3d 30 3b 20 20 20 20 20 62   = res>=0;     b
120bc 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 70 6f 70  reak;.  }..  pop
120bd 53 74 61 63 6b 28 26 70 54 6f 73 2c 20 32 29 3b  Stack(&pTos, 2);
120be 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 20 29  .  if( pOp->p2 )
120bf 7b 0a 20 20 20 20 69 66 28 20 72 65 73 20 29 7b  {.    if( res ){
120c0 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  .      pc = pOp-
120c1 3e 70 32 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  >p2-1;.    }.  }
120c2 65 6c 73 65 7b 0a 20 20 20 20 70 54 6f 73 2b 2b  else{.    pTos++
120c3 3b 0a 20 20 20 20 70 54 6f 73 2d 3e 66 6c 61 67  ;.    pTos->flag
120c4 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20  s = MEM_Int;.   
120c5 20 70 54 6f 73 2d 3e 75 2e 69 20 3d 20 72 65 73   pTos->u.i = res
120c6 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
120c7 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 6e 64  ../* Opcode: And
120c8 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 6f 70   * * *.**.** Pop
120c9 20 74 77 6f 20 76 61 6c 75 65 73 20 6f 66 66 20   two values off 
120ca 74 68 65 20 73 74 61 63 6b 2e 20 20 54 61 6b 65  the stack.  Take
120cb 20 74 68 65 20 6c 6f 67 69 63 61 6c 20 41 4e 44   the logical AND
120cc 20 6f 66 20 74 68 65 0a 2a 2a 20 74 77 6f 20 76   of the.** two v
120cd 61 6c 75 65 73 20 61 6e 64 20 70 75 73 68 20 74  alues and push t
120ce 68 65 20 72 65 73 75 6c 74 69 6e 67 20 62 6f 6f  he resulting boo
120cf 6c 65 61 6e 20 76 61 6c 75 65 20 62 61 63 6b 20  lean value back 
120d0 6f 6e 74 6f 20 74 68 65 0a 2a 2a 20 73 74 61 63  onto the.** stac
120d1 6b 2e 20 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65  k. .*/./* Opcode
120d2 3a 20 4f 72 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  : Or * * *.**.**
120d3 20 50 6f 70 20 74 77 6f 20 76 61 6c 75 65 73 20   Pop two values 
120d4 6f 66 66 20 74 68 65 20 73 74 61 63 6b 2e 20 20  off the stack.  
120d5 54 61 6b 65 20 74 68 65 20 6c 6f 67 69 63 61 6c  Take the logical
120d6 20 4f 52 20 6f 66 20 74 68 65 0a 2a 2a 20 74 77   OR of the.** tw
120d7 6f 20 76 61 6c 75 65 73 20 61 6e 64 20 70 75 73  o values and pus
120d8 68 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20  h the resulting 
120d9 62 6f 6f 6c 65 61 6e 20 76 61 6c 75 65 20 62 61  boolean value ba
120da 63 6b 20 6f 6e 74 6f 20 74 68 65 0a 2a 2a 20 73  ck onto the.** s
120db 74 61 63 6b 2e 20 0a 2a 2f 0a 63 61 73 65 20 4f  tack. .*/.case O
120dc 50 5f 41 6e 64 3a 20 20 20 20 20 20 20 20 20 20  P_And:          
120dd 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
120de 4b 5f 41 4e 44 2c 20 6e 6f 2d 70 75 73 68 20 2a  K_AND, no-push *
120df 2f 0a 63 61 73 65 20 4f 50 5f 4f 72 3a 20 7b 20  /.case OP_Or: { 
120e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
120e1 61 6d 65 20 61 73 20 54 4b 5f 4f 52 2c 20 6e 6f  ame as TK_OR, no
120e2 2d 70 75 73 68 20 2a 2f 0a 20 20 4d 65 6d 20 2a  -push */.  Mem *
120e3 70 4e 6f 73 20 3d 20 26 70 54 6f 73 5b 2d 31 5d  pNos = &pTos[-1]
120e4 3b 0a 20 20 69 6e 74 20 76 31 2c 20 76 32 3b 20  ;.  int v1, v2; 
120e5 20 20 20 2f 2a 20 30 3d 3d 54 52 55 45 2c 20 31     /* 0==TRUE, 1
120e6 3d 3d 46 41 4c 53 45 2c 20 32 3d 3d 55 4e 4b 4e  ==FALSE, 2==UNKN
120e7 4f 57 4e 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 0a  OWN or NULL */..
120e8 20 20 61 73 73 65 72 74 28 20 70 4e 6f 73 3e 3d    assert( pNos>=
120e9 70 2d 3e 61 53 74 61 63 6b 20 29 3b 0a 20 20 69  p->aStack );.  i
120ea 66 28 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 26  f( pTos->flags &
120eb 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20   MEM_Null ){.   
120ec 20 76 31 20 3d 20 32 3b 0a 20 20 7d 65 6c 73 65   v1 = 2;.  }else
120ed 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
120ee 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70  eMemIntegerify(p
120ef 54 6f 73 29 3b 0a 20 20 20 20 76 31 20 3d 20 70  Tos);.    v1 = p
120f0 54 6f 73 2d 3e 75 2e 69 3d 3d 30 3b 0a 20 20 7d  Tos->u.i==0;.  }
120f1 0a 20 20 69 66 28 20 70 4e 6f 73 2d 3e 66 6c 61  .  if( pNos->fla
120f2 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b  gs & MEM_Null ){
120f3 0a 20 20 20 20 76 32 20 3d 20 32 3b 0a 20 20 7d  .    v2 = 2;.  }
120f4 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
120f5 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69  3VdbeMemIntegeri
120f6 66 79 28 70 4e 6f 73 29 3b 0a 20 20 20 20 76 32  fy(pNos);.    v2
120f7 20 3d 20 70 4e 6f 73 2d 3e 75 2e 69 3d 3d 30 3b   = pNos->u.i==0;
120f8 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e  .  }.  if( pOp->
120f9 6f 70 63 6f 64 65 3d 3d 4f 50 5f 41 6e 64 20 29  opcode==OP_And )
120fa 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e  {.    static con
120fb 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
120fc 20 61 6e 64 5f 6c 6f 67 69 63 5b 5d 20 3d 20 7b   and_logic[] = {
120fd 20 30 2c 20 31 2c 20 32 2c 20 31 2c 20 31 2c 20   0, 1, 2, 1, 1, 
120fe 31 2c 20 32 2c 20 31 2c 20 32 20 7d 3b 0a 20 20  1, 2, 1, 2 };.  
120ff 20 20 76 31 20 3d 20 61 6e 64 5f 6c 6f 67 69 63    v1 = and_logic
12100 5b 76 31 2a 33 2b 76 32 5d 3b 0a 20 20 7d 65 6c  [v1*3+v2];.  }el
12101 73 65 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63  se{.    static c
12102 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
12103 61 72 20 6f 72 5f 6c 6f 67 69 63 5b 5d 20 3d 20  ar or_logic[] = 
12104 7b 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c  { 0, 0, 0, 0, 1,
12105 20 32 2c 20 30 2c 20 32 2c 20 32 20 7d 3b 0a 20   2, 0, 2, 2 };. 
12106 20 20 20 76 31 20 3d 20 6f 72 5f 6c 6f 67 69 63     v1 = or_logic
12107 5b 76 31 2a 33 2b 76 32 5d 3b 0a 20 20 7d 0a 20  [v1*3+v2];.  }. 
12108 20 70 6f 70 53 74 61 63 6b 28 26 70 54 6f 73 2c   popStack(&pTos,
12109 20 32 29 3b 0a 20 20 70 54 6f 73 2b 2b 3b 0a 20   2);.  pTos++;. 
1210a 20 69 66 28 20 76 31 3d 3d 32 20 29 7b 0a 20 20   if( v1==2 ){.  
1210b 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 3d 20    pTos->flags = 
1210c 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 65 6c 73  MEM_Null;.  }els
1210d 65 7b 0a 20 20 20 20 70 54 6f 73 2d 3e 75 2e 69  e{.    pTos->u.i
1210e 20 3d 20 76 31 3d 3d 30 3b 0a 20 20 20 20 70 54   = v1==0;.    pT
1210f 6f 73 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  os->flags = MEM_
12110 49 6e 74 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  Int;.  }.  break
12111 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
12112 4e 65 67 61 74 69 76 65 20 2a 20 2a 20 2a 0a 2a  Negative * * *.*
12113 2a 0a 2a 2a 20 54 72 65 61 74 20 74 68 65 20 74  *.** Treat the t
12114 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20  op of the stack 
12115 61 73 20 61 20 6e 75 6d 65 72 69 63 20 71 75 61  as a numeric qua
12116 6e 74 69 74 79 2e 20 20 52 65 70 6c 61 63 65 20  ntity.  Replace 
12117 69 74 0a 2a 2a 20 77 69 74 68 20 69 74 73 20 61  it.** with its a
12118 64 64 69 74 69 76 65 20 69 6e 76 65 72 73 65 2e  dditive inverse.
12119 20 20 49 66 20 74 68 65 20 74 6f 70 20 6f 66 20    If the top of 
1211a 74 68 65 20 73 74 61 63 6b 20 69 73 20 4e 55 4c  the stack is NUL
1211b 4c 0a 2a 2a 20 69 74 73 20 76 61 6c 75 65 20 69  L.** its value i
1211c 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a  s unchanged..*/.
1211d 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 62 73 56 61  /* Opcode: AbsVa
1211e 6c 75 65 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  lue * * *.**.** 
1211f 54 72 65 61 74 20 74 68 65 20 74 6f 70 20 6f 66  Treat the top of
12120 20 74 68 65 20 73 74 61 63 6b 20 61 73 20 61 20   the stack as a 
12121 6e 75 6d 65 72 69 63 20 71 75 61 6e 74 69 74 79  numeric quantity
12122 2e 20 20 52 65 70 6c 61 63 65 20 69 74 0a 2a 2a  .  Replace it.**
12123 20 77 69 74 68 20 69 74 73 20 61 62 73 6f 6c 75   with its absolu
12124 74 65 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65  te value. If the
12125 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63   top of the stac
12126 6b 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 69 74 73  k is NULL.** its
12127 20 76 61 6c 75 65 20 69 73 20 75 6e 63 68 61 6e   value is unchan
12128 67 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ged..*/.case OP_
12129 4e 65 67 61 74 69 76 65 3a 20 20 20 20 20 20 20  Negative:       
1212a 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
1212b 73 20 54 4b 5f 55 4d 49 4e 55 53 2c 20 6e 6f 2d  s TK_UMINUS, no-
1212c 70 75 73 68 20 2a 2f 0a 63 61 73 65 20 4f 50 5f  push */.case OP_
1212d 41 62 73 56 61 6c 75 65 3a 20 7b 0a 20 20 61 73  AbsValue: {.  as
1212e 73 65 72 74 28 20 70 54 6f 73 3e 3d 70 2d 3e 61  sert( pTos>=p->a
1212f 53 74 61 63 6b 20 29 3b 0a 20 20 69 66 28 20 28  Stack );.  if( (
12130 70 54 6f 73 2d 3e 66 6c 61 67 73 20 26 20 28 4d  pTos->flags & (M
12131 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 7c  EM_Real|MEM_Int|
12132 4d 45 4d 5f 4e 75 6c 6c 29 29 3d 3d 30 20 29 7b  MEM_Null))==0 ){
12133 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
12134 4d 65 6d 4e 75 6d 65 72 69 66 79 28 70 54 6f 73  MemNumerify(pTos
12135 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 54 6f  );.  }.  if( pTo
12136 73 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52  s->flags & MEM_R
12137 65 61 6c 20 29 7b 0a 20 20 20 20 52 65 6c 65 61  eal ){.    Relea
12138 73 65 28 70 54 6f 73 29 3b 0a 20 20 20 20 69 66  se(pTos);.    if
12139 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f  ( pOp->opcode==O
1213a 50 5f 4e 65 67 61 74 69 76 65 20 7c 7c 20 70 54  P_Negative || pT
1213b 6f 73 2d 3e 72 3c 30 2e 30 20 29 7b 0a 20 20 20  os->r<0.0 ){.   
1213c 20 20 20 70 54 6f 73 2d 3e 72 20 3d 20 2d 70 54     pTos->r = -pT
1213d 6f 73 2d 3e 72 3b 0a 20 20 20 20 7d 0a 20 20 20  os->r;.    }.   
1213e 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 3d 20 4d   pTos->flags = M
1213f 45 4d 5f 52 65 61 6c 3b 0a 20 20 7d 65 6c 73 65  EM_Real;.  }else
12140 20 69 66 28 20 70 54 6f 73 2d 3e 66 6c 61 67 73   if( pTos->flags
12141 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20   & MEM_Int ){.  
12142 20 20 52 65 6c 65 61 73 65 28 70 54 6f 73 29 3b    Release(pTos);
12143 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70  .    if( pOp->op
12144 63 6f 64 65 3d 3d 4f 50 5f 4e 65 67 61 74 69 76  code==OP_Negativ
12145 65 20 7c 7c 20 70 54 6f 73 2d 3e 75 2e 69 3c 30  e || pTos->u.i<0
12146 20 29 7b 0a 20 20 20 20 20 20 70 54 6f 73 2d 3e   ){.      pTos->
12147 75 2e 69 20 3d 20 2d 70 54 6f 73 2d 3e 75 2e 69  u.i = -pTos->u.i
12148 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 54 6f 73  ;.    }.    pTos
12149 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e  ->flags = MEM_In
1214a 74 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  t;.  }.  break;.
1214b 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f  }../* Opcode: No
1214c 74 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 6e  t * * *.**.** In
1214d 74 65 72 70 72 65 74 20 74 68 65 20 74 6f 70 20  terpret the top 
1214e 6f 66 20 74 68 65 20 73 74 61 63 6b 20 61 73 20  of the stack as 
1214f 61 20 62 6f 6f 6c 65 61 6e 20 76 61 6c 75 65 2e  a boolean value.
12150 20 20 52 65 70 6c 61 63 65 20 69 74 0a 2a 2a 20    Replace it.** 
12151 77 69 74 68 20 69 74 73 20 63 6f 6d 70 6c 65 6d  with its complem
12152 65 6e 74 2e 20 20 49 66 20 74 68 65 20 74 6f 70  ent.  If the top
12153 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20 69 73   of the stack is
12154 20 4e 55 4c 4c 20 69 74 73 20 76 61 6c 75 65 0a   NULL its value.
12155 2a 2a 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e  ** is unchanged.
12156 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 3a  .*/.case OP_Not:
12157 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20   {              
12158 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
12159 4e 4f 54 2c 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a  NOT, no-push */.
1215a 20 20 61 73 73 65 72 74 28 20 70 54 6f 73 3e 3d    assert( pTos>=
1215b 70 2d 3e 61 53 74 61 63 6b 20 29 3b 0a 20 20 69  p->aStack );.  i
1215c 66 28 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 26  f( pTos->flags &
1215d 20 4d 45 4d 5f 4e 75 6c 6c 20 29 20 62 72 65 61   MEM_Null ) brea
1215e 6b 3b 20 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e  k;  /* Do nothin
1215f 67 20 74 6f 20 4e 55 4c 4c 73 20 2a 2f 0a 20 20  g to NULLs */.  
12160 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e  sqlite3VdbeMemIn
12161 74 65 67 65 72 69 66 79 28 70 54 6f 73 29 3b 0a  tegerify(pTos);.
12162 20 20 61 73 73 65 72 74 28 20 28 70 54 6f 73 2d    assert( (pTos-
12163 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e  >flags & MEM_Dyn
12164 29 3d 3d 30 20 29 3b 0a 20 20 70 54 6f 73 2d 3e  )==0 );.  pTos->
12165 75 2e 69 20 3d 20 21 70 54 6f 73 2d 3e 75 2e 69  u.i = !pTos->u.i
12166 3b 0a 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20  ;.  pTos->flags 
12167 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 62 72 65  = MEM_Int;.  bre
12168 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
12169 3a 20 42 69 74 4e 6f 74 20 2a 20 2a 20 2a 0a 2a  : BitNot * * *.*
1216a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74  *.** Interpret t
1216b 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74  he top of the st
1216c 61 63 6b 20 61 73 20 61 6e 20 76 61 6c 75 65 2e  ack as an value.
1216d 20 20 52 65 70 6c 61 63 65 20 69 74 0a 2a 2a 20    Replace it.** 
1216e 77 69 74 68 20 69 74 73 20 6f 6e 65 73 2d 63 6f  with its ones-co
1216f 6d 70 6c 65 6d 65 6e 74 2e 20 20 49 66 20 74 68  mplement.  If th
12170 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61  e top of the sta
12171 63 6b 20 69 73 20 4e 55 4c 4c 20 69 74 73 0a 2a  ck is NULL its.*
12172 2a 20 76 61 6c 75 65 20 69 73 20 75 6e 63 68 61  * value is uncha
12173 6e 67 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  nged..*/.case OP
12174 5f 42 69 74 4e 6f 74 3a 20 7b 20 20 20 20 20 20  _BitNot: {      
12175 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
12176 73 20 54 4b 5f 42 49 54 4e 4f 54 2c 20 6e 6f 2d  s TK_BITNOT, no-
12177 70 75 73 68 20 2a 2f 0a 20 20 61 73 73 65 72 74  push */.  assert
12178 28 20 70 54 6f 73 3e 3d 70 2d 3e 61 53 74 61 63  ( pTos>=p->aStac
12179 6b 20 29 3b 0a 20 20 69 66 28 20 70 54 6f 73 2d  k );.  if( pTos-
1217a 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c  >flags & MEM_Nul
1217b 6c 20 29 20 62 72 65 61 6b 3b 20 20 2f 2a 20 44  l ) break;  /* D
1217c 6f 20 6e 6f 74 68 69 6e 67 20 74 6f 20 4e 55 4c  o nothing to NUL
1217d 4c 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56  Ls */.  sqlite3V
1217e 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79  dbeMemIntegerify
1217f 28 70 54 6f 73 29 3b 0a 20 20 61 73 73 65 72 74  (pTos);.  assert
12180 28 20 28 70 54 6f 73 2d 3e 66 6c 61 67 73 20 26  ( (pTos->flags &
12181 20 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a   MEM_Dyn)==0 );.
12182 20 20 70 54 6f 73 2d 3e 75 2e 69 20 3d 20 7e 70    pTos->u.i = ~p
12183 54 6f 73 2d 3e 75 2e 69 3b 0a 20 20 70 54 6f 73  Tos->u.i;.  pTos
12184 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e  ->flags = MEM_In
12185 74 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  t;.  break;.}../
12186 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 6f 70 20 2a  * Opcode: Noop *
12187 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 6f 20 6e 6f   * *.**.** Do no
12188 74 68 69 6e 67 2e 20 20 54 68 69 73 20 69 6e 73  thing.  This ins
12189 74 72 75 63 74 69 6f 6e 20 69 73 20 6f 66 74 65  truction is ofte
1218a 6e 20 75 73 65 66 75 6c 20 61 73 20 61 20 6a 75  n useful as a ju
1218b 6d 70 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f  mp.** destinatio
1218c 6e 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20  n..*/./*.** The 
1218d 6d 61 67 69 63 20 45 78 70 6c 61 69 6e 20 6f 70  magic Explain op
1218e 63 6f 64 65 20 61 72 65 20 6f 6e 6c 79 20 69 6e  code are only in
1218f 73 65 72 74 65 64 20 77 68 65 6e 20 65 78 70 6c  serted when expl
12190 61 69 6e 3d 3d 32 20 28 77 68 69 63 68 0a 2a 2a  ain==2 (which.**
12191 20 69 73 20 74 6f 20 73 61 79 20 77 68 65 6e 20   is to say when 
12192 74 68 65 20 45 58 50 4c 41 49 4e 20 51 55 45 52  the EXPLAIN QUER
12193 59 20 50 4c 41 4e 20 73 79 6e 74 61 78 20 69 73  Y PLAN syntax is
12194 20 75 73 65 64 2e 29 0a 2a 2a 20 54 68 69 73 20   used.).** This 
12195 6f 70 63 6f 64 65 20 72 65 63 6f 72 64 73 20 69  opcode records i
12196 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20  nformation from 
12197 74 68 65 20 6f 70 74 69 6d 69 7a 65 72 2e 20 20  the optimizer.  
12198 49 74 20 69 73 20 74 68 65 0a 2a 2a 20 74 68 65  It is the.** the
12199 20 73 61 6d 65 20 61 73 20 61 20 6e 6f 2d 6f 70   same as a no-op
1219a 2e 20 20 54 68 69 73 20 6f 70 63 6f 64 65 73 6e  .  This opcodesn
1219b 65 76 65 72 20 61 70 70 65 61 72 73 20 69 6e 20  ever appears in 
1219c 61 20 72 65 61 6c 20 56 4d 20 70 72 6f 67 72 61  a real VM progra
1219d 6d 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 45 78  m..*/.case OP_Ex
1219e 70 6c 61 69 6e 3a 0a 63 61 73 65 20 4f 50 5f 4e  plain:.case OP_N
1219f 6f 6f 70 3a 20 7b 20 20 20 20 20 20 20 20 20 20  oop: {          
121a0 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a    /* no-push */.
121a1 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
121a2 70 63 6f 64 65 3a 20 49 66 20 50 31 20 50 32 20  pcode: If P1 P2 
121a3 2a 0a 2a 2a 0a 2a 2a 20 50 6f 70 20 61 20 73 69  *.**.** Pop a si
121a4 6e 67 6c 65 20 62 6f 6f 6c 65 61 6e 20 66 72 6f  ngle boolean fro
121a5 6d 20 74 68 65 20 73 74 61 63 6b 2e 20 20 49 66  m the stack.  If
121a6 20 74 68 65 20 62 6f 6f 6c 65 61 6e 20 70 6f 70   the boolean pop
121a7 70 65 64 20 69 73 0a 2a 2a 20 74 72 75 65 2c 20  ped is.** true, 
121a8 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 70 32 2e  then jump to p2.
121a9 20 20 4f 74 68 65 72 77 69 73 65 20 63 6f 6e 74    Otherwise cont
121aa 69 6e 75 65 20 74 6f 20 74 68 65 20 6e 65 78 74  inue to the next
121ab 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a   instruction..**
121ac 20 41 6e 20 69 6e 74 65 67 65 72 20 69 73 20 66   An integer is f
121ad 61 6c 73 65 20 69 66 20 7a 65 72 6f 20 61 6e 64  alse if zero and
121ae 20 74 72 75 65 20 6f 74 68 65 72 77 69 73 65 2e   true otherwise.
121af 20 20 41 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a    A string is.**
121b0 20 66 61 6c 73 65 20 69 66 20 69 74 20 68 61 73   false if it has
121b1 20 7a 65 72 6f 20 6c 65 6e 67 74 68 20 61 6e 64   zero length and
121b2 20 74 72 75 65 20 6f 74 68 65 72 77 69 73 65 2e   true otherwise.
121b3 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 76 61  .**.** If the va
121b4 6c 75 65 20 70 6f 70 70 65 64 20 6f 66 20 74 68  lue popped of th
121b5 65 20 73 74 61 63 6b 20 69 73 20 4e 55 4c 4c 2c  e stack is NULL,
121b6 20 74 68 65 6e 20 74 61 6b 65 20 74 68 65 20 6a   then take the j
121b7 75 6d 70 20 69 66 20 50 31 0a 2a 2a 20 69 73 20  ump if P1.** is 
121b8 74 72 75 65 20 61 6e 64 20 66 61 6c 6c 20 74 68  true and fall th
121b9 72 6f 75 67 68 20 69 66 20 50 31 20 69 73 20 66  rough if P1 is f
121ba 61 6c 73 65 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f  alse..*/./* Opco
121bb 64 65 3a 20 49 66 4e 6f 74 20 50 31 20 50 32 20  de: IfNot P1 P2 
121bc 2a 0a 2a 2a 0a 2a 2a 20 50 6f 70 20 61 20 73 69  *.**.** Pop a si
121bd 6e 67 6c 65 20 62 6f 6f 6c 65 61 6e 20 66 72 6f  ngle boolean fro
121be 6d 20 74 68 65 20 73 74 61 63 6b 2e 20 20 49 66  m the stack.  If
121bf 20 74 68 65 20 62 6f 6f 6c 65 61 6e 20 70 6f 70   the boolean pop
121c0 70 65 64 20 69 73 0a 2a 2a 20 66 61 6c 73 65 2c  ped is.** false,
121c1 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 70 32   then jump to p2
121c2 2e 20 20 4f 74 68 65 72 77 69 73 65 20 63 6f 6e  .  Otherwise con
121c3 74 69 6e 75 65 20 74 6f 20 74 68 65 20 6e 65 78  tinue to the nex
121c4 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a  t instruction..*
121c5 2a 20 41 6e 20 69 6e 74 65 67 65 72 20 69 73 20  * An integer is 
121c6 66 61 6c 73 65 20 69 66 20 7a 65 72 6f 20 61 6e  false if zero an
121c7 64 20 74 72 75 65 20 6f 74 68 65 72 77 69 73 65  d true otherwise
121c8 2e 20 20 41 20 73 74 72 69 6e 67 20 69 73 0a 2a  .  A string is.*
121c9 2a 20 66 61 6c 73 65 20 69 66 20 69 74 20 68 61  * false if it ha
121ca 73 20 7a 65 72 6f 20 6c 65 6e 67 74 68 20 61 6e  s zero length an
121cb 64 20 74 72 75 65 20 6f 74 68 65 72 77 69 73 65  d true otherwise
121cc 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 76  ..**.** If the v
121cd 61 6c 75 65 20 70 6f 70 70 65 64 20 6f 66 20 74  alue popped of t
121ce 68 65 20 73 74 61 63 6b 20 69 73 20 4e 55 4c 4c  he stack is NULL
121cf 2c 20 74 68 65 6e 20 74 61 6b 65 20 74 68 65 20  , then take the 
121d0 6a 75 6d 70 20 69 66 20 50 31 0a 2a 2a 20 69 73  jump if P1.** is
121d1 20 74 72 75 65 20 61 6e 64 20 66 61 6c 6c 20 74   true and fall t
121d2 68 72 6f 75 67 68 20 69 66 20 50 31 20 69 73 20  hrough if P1 is 
121d3 66 61 6c 73 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f  false..*/.case O
121d4 50 5f 49 66 3a 20 20 20 20 20 20 20 20 20 20 20  P_If:           
121d5 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68        /* no-push
121d6 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 4e 6f   */.case OP_IfNo
121d7 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  t: {            
121d8 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20  /* no-push */.  
121d9 69 6e 74 20 63 3b 0a 20 20 61 73 73 65 72 74 28  int c;.  assert(
121da 20 70 54 6f 73 3e 3d 70 2d 3e 61 53 74 61 63 6b   pTos>=p->aStack
121db 20 29 3b 0a 20 20 69 66 28 20 70 54 6f 73 2d 3e   );.  if( pTos->
121dc 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c  flags & MEM_Null
121dd 20 29 7b 0a 20 20 20 20 63 20 3d 20 70 4f 70 2d   ){.    c = pOp-
121de 3e 70 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 23 69  >p1;.  }else{.#i
121df 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
121e0 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a  _FLOATING_POINT.
121e1 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56      c = sqlite3V
121e2 64 62 65 49 6e 74 56 61 6c 75 65 28 70 54 6f 73  dbeIntValue(pTos
121e3 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 63 20 3d  );.#else.    c =
121e4 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c   sqlite3VdbeReal
121e5 56 61 6c 75 65 28 70 54 6f 73 29 21 3d 30 2e 30  Value(pTos)!=0.0
121e6 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28  ;.#endif.    if(
121e7 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
121e8 5f 49 66 4e 6f 74 20 29 20 63 20 3d 20 21 63 3b  _IfNot ) c = !c;
121e9 0a 20 20 7d 0a 20 20 52 65 6c 65 61 73 65 28 70  .  }.  Release(p
121ea 54 6f 73 29 3b 0a 20 20 70 54 6f 73 2d 2d 3b 0a  Tos);.  pTos--;.
121eb 20 20 69 66 28 20 63 20 29 20 70 63 20 3d 20 70    if( c ) pc = p
121ec 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 62 72 65 61  Op->p2-1;.  brea
121ed 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
121ee 20 49 73 4e 75 6c 6c 20 50 31 20 50 32 20 2a 0a   IsNull P1 P2 *.
121ef 2a 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65 20  **.** Check the 
121f0 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b  top of the stack
121f1 20 61 6e 64 20 6a 75 6d 70 20 74 6f 20 50 32 20   and jump to P2 
121f2 69 66 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68  if the top of th
121f3 65 20 73 74 61 63 6b 0a 2a 2a 20 69 73 20 4e 55  e stack.** is NU
121f4 4c 4c 2e 20 20 49 66 20 50 31 20 69 73 20 70 6f  LL.  If P1 is po
121f5 73 69 74 69 76 65 2c 20 74 68 65 6e 20 70 6f 70  sitive, then pop
121f6 20 50 31 20 65 6c 65 6d 65 6e 74 73 20 66 72 6f   P1 elements fro
121f7 6d 20 74 68 65 20 73 74 61 63 6b 0a 2a 2a 20 72  m the stack.** r
121f8 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65  egardless of whe
121f9 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20  ther or not the 
121fa 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 2e 20 20  jump is taken.  
121fb 49 66 20 50 31 20 69 73 20 6e 65 67 61 74 69 76  If P1 is negativ
121fc 65 2c 0a 2a 2a 20 70 6f 70 20 2d 50 31 20 65 6c  e,.** pop -P1 el
121fd 65 6d 65 6e 74 73 20 66 72 6f 6d 20 74 68 65 20  ements from the 
121fe 73 74 61 63 6b 20 6f 6e 6c 79 20 69 66 20 74 68  stack only if th
121ff 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20  e jump is taken 
12200 61 6e 64 20 6c 65 61 76 65 0a 2a 2a 20 74 68 65  and leave.** the
12201 20 73 74 61 63 6b 20 75 6e 63 68 61 6e 67 65 64   stack unchanged
12202 20 69 66 20 74 68 65 20 6a 75 6d 70 20 69 73 20   if the jump is 
12203 6e 6f 74 20 74 61 6b 65 6e 2e 0a 2a 2f 0a 63 61  not taken..*/.ca
12204 73 65 20 4f 50 5f 49 73 4e 75 6c 6c 3a 20 7b 20  se OP_IsNull: { 
12205 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61             /* sa
12206 6d 65 20 61 73 20 54 4b 5f 49 53 4e 55 4c 4c 2c  me as TK_ISNULL,
12207 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 69 66   no-push */.  if
12208 28 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 26 20  ( pTos->flags & 
12209 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20  MEM_Null ){.    
1220a 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a  pc = pOp->p2-1;.
1220b 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 3c      if( pOp->p1<
1220c 30 20 29 7b 0a 20 20 20 20 20 20 70 6f 70 53 74  0 ){.      popSt
1220d 61 63 6b 28 26 70 54 6f 73 2c 20 2d 70 4f 70 2d  ack(&pTos, -pOp-
1220e 3e 70 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  >p1);.    }.  }.
1220f 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 3e 30 20    if( pOp->p1>0 
12210 29 7b 0a 20 20 20 20 70 6f 70 53 74 61 63 6b 28  ){.    popStack(
12211 26 70 54 6f 73 2c 20 70 4f 70 2d 3e 70 31 29 3b  &pTos, pOp->p1);
12212 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
12213 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 74 4e  ./* Opcode: NotN
12214 75 6c 6c 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a  ull P1 P2 *.**.*
12215 2a 20 4a 75 6d 70 20 74 6f 20 50 32 20 69 66 20  * Jump to P2 if 
12216 74 68 65 20 74 6f 70 20 61 62 73 28 50 31 29 20  the top abs(P1) 
12217 76 61 6c 75 65 73 20 6f 6e 20 74 68 65 20 73 74  values on the st
12218 61 63 6b 20 61 72 65 20 61 6c 6c 20 6e 6f 74 20  ack are all not 
12219 4e 55 4c 4c 2e 20 20 0a 2a 2a 20 52 65 67 61 72  NULL.  .** Regar
1221a 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 72  dless of whether
1221b 20 6f 72 20 6e 6f 74 20 74 68 65 20 6a 75 6d 70   or not the jump
1221c 20 69 73 20 74 61 6b 65 6e 2c 20 70 6f 70 20 74   is taken, pop t
1221d 68 65 20 73 74 61 63 6b 0a 2a 2a 20 50 31 20 74  he stack.** P1 t
1221e 69 6d 65 73 20 69 66 20 50 31 20 69 73 20 67 72  imes if P1 is gr
1221f 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2e  eater than zero.
12220 20 20 42 75 74 20 69 66 20 50 31 20 69 73 20 6e    But if P1 is n
12221 65 67 61 74 69 76 65 2c 0a 2a 2a 20 6c 65 61 76  egative,.** leav
12222 65 20 74 68 65 20 73 74 61 63 6b 20 75 6e 63 68  e the stack unch
12223 61 6e 67 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f  anged..*/.case O
12224 50 5f 4e 6f 74 4e 75 6c 6c 3a 20 7b 20 20 20 20  P_NotNull: {    
12225 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20          /* same 
12226 61 73 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c 20 6e  as TK_NOTNULL, n
12227 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 69 6e 74 20  o-push */.  int 
12228 69 2c 20 63 6e 74 3b 0a 20 20 63 6e 74 20 3d 20  i, cnt;.  cnt = 
12229 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 66 28 20 63  pOp->p1;.  if( c
1222a 6e 74 3c 30 20 29 20 63 6e 74 20 3d 20 2d 63 6e  nt<0 ) cnt = -cn
1222b 74 3b 0a 20 20 61 73 73 65 72 74 28 20 26 70 54  t;.  assert( &pT
1222c 6f 73 5b 31 2d 63 6e 74 5d 20 3e 3d 20 70 2d 3e  os[1-cnt] >= p->
1222d 61 53 74 61 63 6b 20 29 3b 0a 20 20 66 6f 72 28  aStack );.  for(
1222e 69 3d 30 3b 20 69 3c 63 6e 74 20 26 26 20 28 70  i=0; i<cnt && (p
1222f 54 6f 73 5b 31 2b 69 2d 63 6e 74 5d 2e 66 6c 61  Tos[1+i-cnt].fla
12230 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d  gs & MEM_Null)==
12231 30 3b 20 69 2b 2b 29 7b 7d 0a 20 20 69 66 28 20  0; i++){}.  if( 
12232 69 3e 3d 63 6e 74 20 29 20 70 63 20 3d 20 70 4f  i>=cnt ) pc = pO
12233 70 2d 3e 70 32 2d 31 3b 0a 20 20 69 66 28 20 70  p->p2-1;.  if( p
12234 4f 70 2d 3e 70 31 3e 30 20 29 20 70 6f 70 53 74  Op->p1>0 ) popSt
12235 61 63 6b 28 26 70 54 6f 73 2c 20 63 6e 74 29 3b  ack(&pTos, cnt);
12236 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
12237 4f 70 63 6f 64 65 3a 20 53 65 74 4e 75 6d 43 6f  Opcode: SetNumCo
12238 6c 75 6d 6e 73 20 50 31 20 50 32 20 2a 0a 2a 2a  lumns P1 P2 *.**
12239 0a 2a 2a 20 42 65 66 6f 72 65 20 74 68 65 20 4f  .** Before the O
1223a 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 20  P_Column opcode 
1223b 63 61 6e 20 62 65 20 65 78 65 63 75 74 65 64 20  can be executed 
1223c 6f 6e 20 61 20 63 75 72 73 6f 72 2c 20 74 68 69  on a cursor, thi
1223d 73 0a 2a 2a 20 6f 70 63 6f 64 65 20 6d 75 73 74  s.** opcode must
1223e 20 62 65 20 63 61 6c 6c 65 64 20 74 6f 20 73 65   be called to se
1223f 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  t the number of 
12240 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 74 61  fields in the ta
12241 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ble..**.** This 
12242 6f 70 63 6f 64 65 20 73 65 74 73 20 74 68 65 20  opcode sets the 
12243 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
12244 73 20 66 6f 72 20 63 75 72 73 6f 72 20 50 31 20  s for cursor P1 
12245 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  to P2..**.** If 
12246 4f 50 5f 4b 65 79 41 73 44 61 74 61 20 69 73 20  OP_KeyAsData is 
12247 74 6f 20 62 65 20 61 70 70 6c 69 65 64 20 74 6f  to be applied to
12248 20 63 75 72 73 6f 72 20 50 31 2c 20 69 74 20 6d   cursor P1, it m
12249 75 73 74 20 62 65 20 65 78 65 63 75 74 65 64 0a  ust be executed.
1224a 2a 2a 20 62 65 66 6f 72 65 20 74 68 69 73 20 6f  ** before this o
1224b 70 2d 63 6f 64 65 2e 0a 2a 2f 0a 63 61 73 65 20  p-code..*/.case 
1224c 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73  OP_SetNumColumns
1224d 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d  : {       /* no-
1224e 70 75 73 68 20 2a 2f 0a 20 20 43 75 72 73 6f 72  push */.  Cursor
1224f 20 2a 70 43 3b 0a 20 20 61 73 73 65 72 74 28 20   *pC;.  assert( 
12250 28 70 4f 70 2d 3e 70 31 29 3c 70 2d 3e 6e 43 75  (pOp->p1)<p->nCu
12251 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74  rsor );.  assert
12252 28 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e  ( p->apCsr[pOp->
12253 70 31 5d 21 3d 30 20 29 3b 0a 20 20 70 43 20 3d  p1]!=0 );.  pC =
12254 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70   p->apCsr[pOp->p
12255 31 5d 3b 0a 20 20 70 43 2d 3e 6e 46 69 65 6c 64  1];.  pC->nField
12256 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 62 72   = pOp->p2;.  br
12257 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
12258 65 3a 20 43 6f 6c 75 6d 6e 20 50 31 20 50 32 20  e: Column P1 P2 
12259 50 33 0a 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72  P3.**.** Interpr
1225a 65 74 20 74 68 65 20 64 61 74 61 20 74 68 61 74  et the data that
1225b 20 63 75 72 73 6f 72 20 50 31 20 70 6f 69 6e 74   cursor P1 point
1225c 73 20 74 6f 20 61 73 20 61 20 73 74 72 75 63 74  s to as a struct
1225d 75 72 65 20 62 75 69 6c 74 20 75 73 69 6e 67 0a  ure built using.
1225e 2a 2a 20 74 68 65 20 4d 61 6b 65 52 65 63 6f 72  ** the MakeRecor
1225f 64 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20  d instruction.  
12260 28 53 65 65 20 74 68 65 20 4d 61 6b 65 52 65 63  (See the MakeRec
12261 6f 72 64 20 6f 70 63 6f 64 65 20 66 6f 72 20 61  ord opcode for a
12262 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66  dditional.** inf
12263 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74  ormation about t
12264 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 65  he format of the
12265 20 64 61 74 61 2e 29 20 50 75 73 68 20 6f 6e 74   data.) Push ont
12266 6f 20 74 68 65 20 73 74 61 63 6b 20 74 68 65 20  o the stack the 
12267 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20  value.** of the 
12268 50 32 2d 74 68 20 63 6f 6c 75 6d 6e 20 63 6f 6e  P2-th column con
12269 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 64 61  tained in the da
1226a 74 61 2e 20 49 66 20 74 68 65 72 65 20 61 72 65  ta. If there are
1226b 20 6c 65 73 73 20 74 68 61 74 20 28 50 32 2b 31   less that (P2+1
1226c 29 20 0a 2a 2a 20 76 61 6c 75 65 73 20 69 6e 20  ) .** values in 
1226d 74 68 65 20 72 65 63 6f 72 64 2c 20 70 75 73 68  the record, push
1226e 20 61 20 4e 55 4c 4c 20 6f 6e 74 6f 20 74 68 65   a NULL onto the
1226f 20 73 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66   stack..**.** If
12270 20 74 68 65 20 4b 65 79 41 73 44 61 74 61 20 6f   the KeyAsData o
12271 70 63 6f 64 65 20 68 61 73 20 70 72 65 76 69 6f  pcode has previo
12272 75 73 6c 79 20 65 78 65 63 75 74 65 64 20 6f 6e  usly executed on
12273 20 74 68 69 73 20 63 75 72 73 6f 72 2c 20 74 68   this cursor, th
12274 65 6e 20 74 68 65 0a 2a 2a 20 66 69 65 6c 64 20  en the.** field 
12275 6d 69 67 68 74 20 62 65 20 65 78 74 72 61 63 74  might be extract
12276 65 64 20 66 72 6f 6d 20 74 68 65 20 6b 65 79 20  ed from the key 
12277 72 61 74 68 65 72 20 74 68 61 6e 20 74 68 65 20  rather than the 
12278 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  data..**.** If t
12279 68 65 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69  he column contai
1227a 6e 73 20 66 65 77 65 72 20 74 68 61 6e 20 50 32  ns fewer than P2
1227b 20 66 69 65 6c 64 73 2c 20 74 68 65 6e 20 70 75   fields, then pu
1227c 73 68 20 61 20 4e 55 4c 4c 2e 20 20 4f 72 0a 2a  sh a NULL.  Or.*
1227d 2a 20 69 66 20 50 33 20 69 73 20 6f 66 20 74 79  * if P3 is of ty
1227e 70 65 20 50 33 5f 4d 45 4d 2c 20 74 68 65 6e 20  pe P3_MEM, then 
1227f 70 75 73 68 20 74 68 65 20 50 33 20 76 61 6c 75  push the P3 valu
12280 65 2e 20 20 54 68 65 20 50 33 20 76 61 6c 75 65  e.  The P3 value
12281 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 64 65 66 61   will.** be defa
12282 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 61 20  ult value for a 
12283 63 6f 6c 75 6d 6e 20 74 68 61 74 20 68 61 73 20  column that has 
12284 62 65 65 6e 20 61 64 64 65 64 20 75 73 69 6e 67  been added using
12285 20 74 68 65 20 41 4c 54 45 52 20 54 41 42 4c 45   the ALTER TABLE
12286 0a 2a 2a 20 41 44 44 20 43 4f 4c 55 4d 4e 20 63  .** ADD COLUMN c
12287 6f 6d 6d 61 6e 64 2e 20 20 49 66 20 50 33 20 69  ommand.  If P3 i
12288 73 20 61 6e 20 6f 72 64 69 6e 61 72 79 20 73 74  s an ordinary st
12289 72 69 6e 67 2c 20 6a 75 73 74 20 70 75 73 68 20  ring, just push 
1228a 61 20 4e 55 4c 4c 2e 0a 2a 2a 20 57 68 65 6e 20  a NULL..** When 
1228b 50 33 20 69 73 20 61 20 73 74 72 69 6e 67 20 69  P3 is a string i
1228c 74 20 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 74  t is really just
1228d 20 61 20 63 6f 6d 6d 65 6e 74 20 64 65 73 63 72   a comment descr
1228e 69 62 69 6e 67 20 74 68 65 20 76 61 6c 75 65 0a  ibing the value.
1228f 2a 2a 20 74 6f 20 62 65 20 70 75 73 68 65 64 2c  ** to be pushed,
12290 20 6e 6f 74 20 61 20 64 65 66 61 75 6c 74 20 76   not a default v
12291 61 6c 75 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  alue..*/.case OP
12292 5f 43 6f 6c 75 6d 6e 3a 20 7b 0a 20 20 75 33 32  _Column: {.  u32
12293 20 70 61 79 6c 6f 61 64 53 69 7a 65 3b 20 20 20   payloadSize;   
12294 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
12295 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64  es in the record
12296 20 2a 2f 0a 20 20 69 6e 74 20 70 31 20 3d 20 70   */.  int p1 = p
12297 4f 70 2d 3e 70 31 3b 20 20 2f 2a 20 50 31 20 76  Op->p1;  /* P1 v
12298 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 70 63 6f  alue of the opco
12299 64 65 20 2a 2f 0a 20 20 69 6e 74 20 70 32 20 3d  de */.  int p2 =
1229a 20 70 4f 70 2d 3e 70 32 3b 20 20 2f 2a 20 63 6f   pOp->p2;  /* co
1229b 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 74 6f 20 72  lumn number to r
1229c 65 74 72 69 65 76 65 20 2a 2f 0a 20 20 43 75 72  etrieve */.  Cur
1229d 73 6f 72 20 2a 70 43 20 3d 20 30 3b 20 20 20 20  sor *pC = 0;    
1229e 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73  /* The VDBE curs
1229f 6f 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 52  or */.  char *zR
122a0 65 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f  ec;        /* Po
122a1 69 6e 74 65 72 20 74 6f 20 63 6f 6d 70 6c 65 74  inter to complet
122a2 65 20 72 65 63 6f 72 64 2d 64 61 74 61 20 2a 2f  e record-data */
122a3 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72  .  BtCursor *pCr
122a4 73 72 3b 20 20 20 2f 2a 20 54 68 65 20 42 54 72  sr;   /* The BTr
122a5 65 65 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 75  ee cursor */.  u
122a6 33 32 20 2a 61 54 79 70 65 3b 20 20 20 20 20 20  32 *aType;      
122a7 20 20 2f 2a 20 61 54 79 70 65 5b 69 5d 20 68 6f    /* aType[i] ho
122a8 6c 64 73 20 74 68 65 20 6e 75 6d 65 72 69 63 20  lds the numeric 
122a9 74 79 70 65 20 6f 66 20 74 68 65 20 69 2d 74 68  type of the i-th
122aa 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 75 33 32   column */.  u32
122ab 20 2a 61 4f 66 66 73 65 74 3b 20 20 20 20 20 20   *aOffset;      
122ac 2f 2a 20 61 4f 66 66 73 65 74 5b 69 5d 20 69 73  /* aOffset[i] is
122ad 20 6f 66 66 73 65 74 20 74 6f 20 73 74 61 72 74   offset to start
122ae 20 6f 66 20 64 61 74 61 20 66 6f 72 20 69 2d 74   of data for i-t
122af 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 75 33  h column */.  u3
122b0 32 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20  2 nField;       
122b1 20 2f 2a 20 6e 75 6d 62 65 72 20 6f 66 20 66 69   /* number of fi
122b2 65 6c 64 73 20 69 6e 20 74 68 65 20 72 65 63 6f  elds in the reco
122b3 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 3b  rd */.  int len;
122b4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
122b5 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20  e length of the 
122b6 73 65 72 69 61 6c 69 7a 65 64 20 64 61 74 61 20  serialized data 
122b7 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 2a  for the column *
122b8 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  /.  int i;      
122b9 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
122ba 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 63 68 61 72  ounter */.  char
122bb 20 2a 7a 44 61 74 61 3b 20 20 20 20 20 20 20 2f   *zData;       /
122bc 2a 20 50 61 72 74 20 6f 66 20 74 68 65 20 72 65  * Part of the re
122bd 63 6f 72 64 20 62 65 69 6e 67 20 64 65 63 6f 64  cord being decod
122be 65 64 20 2a 2f 0a 20 20 4d 65 6d 20 73 4d 65 6d  ed */.  Mem sMem
122bf 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f  ;          /* Fo
122c0 72 20 73 74 6f 72 69 6e 67 20 74 68 65 20 72 65  r storing the re
122c1 63 6f 72 64 20 62 65 69 6e 67 20 64 65 63 6f 64  cord being decod
122c2 65 64 20 2a 2f 0a 0a 20 20 73 4d 65 6d 2e 66 6c  ed */..  sMem.fl
122c3 61 67 73 20 3d 20 30 3b 0a 20 20 61 73 73 65 72  ags = 0;.  asser
122c4 74 28 20 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72  t( p1<p->nCursor
122c5 20 29 3b 0a 20 20 70 54 6f 73 2b 2b 3b 0a 20 20   );.  pTos++;.  
122c6 70 54 6f 73 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pTos->flags = ME
122c7 4d 5f 4e 75 6c 6c 3b 0a 0a 20 20 2f 2a 20 54 68  M_Null;..  /* Th
122c8 69 73 20 62 6c 6f 63 6b 20 73 65 74 73 20 74 68  is block sets th
122c9 65 20 76 61 72 69 61 62 6c 65 20 70 61 79 6c 6f  e variable paylo
122ca 61 64 53 69 7a 65 20 74 6f 20 62 65 20 74 68 65  adSize to be the
122cb 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   total number of
122cc 0a 20 20 2a 2a 20 62 79 74 65 73 20 69 6e 20 74  .  ** bytes in t
122cd 68 65 20 72 65 63 6f 72 64 2e 0a 20 20 2a 2a 0a  he record..  **.
122ce 20 20 2a 2a 20 7a 52 65 63 20 69 73 20 73 65 74    ** zRec is set
122cf 20 74 6f 20 62 65 20 74 68 65 20 63 6f 6d 70 6c   to be the compl
122d0 65 74 65 20 74 65 78 74 20 6f 66 20 74 68 65 20  ete text of the 
122d1 72 65 63 6f 72 64 20 69 66 20 69 74 20 69 73 20  record if it is 
122d2 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2a 20  available..  ** 
122d3 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 72 65 63  The complete rec
122d4 6f 72 64 20 74 65 78 74 20 69 73 20 61 6c 77 61  ord text is alwa
122d5 79 73 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72  ys available for
122d6 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 73 0a 20   pseudo-tables. 
122d7 20 2a 2a 20 49 66 20 74 68 65 20 72 65 63 6f 72   ** If the recor
122d8 64 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61  d is stored in a
122d9 20 63 75 72 73 6f 72 2c 20 74 68 65 20 63 6f 6d   cursor, the com
122da 70 6c 65 74 65 20 72 65 63 6f 72 64 20 74 65 78  plete record tex
122db 74 0a 20 20 2a 2a 20 6d 69 67 68 74 20 62 65 20  t.  ** might be 
122dc 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65  available in the
122dd 20 20 70 43 2d 3e 61 52 6f 77 20 63 61 63 68 65    pC->aRow cache
122de 2e 20 20 4f 72 20 69 74 20 6d 69 67 68 74 20 6e  .  Or it might n
122df 6f 74 20 62 65 2e 0a 20 20 2a 2a 20 49 66 20 74  ot be..  ** If t
122e0 68 65 20 64 61 74 61 20 69 73 20 75 6e 61 76 61  he data is unava
122e1 69 6c 61 62 6c 65 2c 20 20 7a 52 65 63 20 69 73  ilable,  zRec is
122e2 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20   set to NULL..  
122e3 2a 2a 0a 20 20 2a 2a 20 57 65 20 61 6c 73 6f 20  **.  ** We also 
122e4 63 6f 6d 70 75 74 65 20 74 68 65 20 6e 75 6d 62  compute the numb
122e5 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
122e6 20 74 68 65 20 72 65 63 6f 72 64 2e 20 20 46 6f   the record.  Fo
122e7 72 20 63 75 72 73 6f 72 73 2c 0a 20 20 2a 2a 20  r cursors,.  ** 
122e8 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f  the number of co
122e9 6c 75 6d 6e 73 20 69 73 20 73 74 6f 72 65 64 20  lumns is stored 
122ea 69 6e 20 74 68 65 20 43 75 72 73 6f 72 2e 6e 46  in the Cursor.nF
122eb 69 65 6c 64 20 65 6c 65 6d 65 6e 74 2e 20 20 46  ield element.  F
122ec 6f 72 0a 20 20 2a 2a 20 72 65 63 6f 72 64 73 20  or.  ** records 
122ed 6f 6e 20 74 68 65 20 73 74 61 63 6b 2c 20 74 68  on the stack, th
122ee 65 20 6e 65 78 74 20 65 6e 74 72 79 20 64 6f 77  e next entry dow
122ef 6e 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 20 69  n on the stack i
122f0 73 20 61 6e 20 69 6e 74 65 67 65 72 0a 20 20 2a  s an integer.  *
122f1 2a 20 77 68 69 63 68 20 69 73 20 74 68 65 20 6e  * which is the n
122f2 75 6d 62 65 72 20 6f 66 20 72 65 63 6f 72 64 73  umber of records
122f3 2e 0a 20 20 2a 2f 0a 20 20 70 43 20 3d 20 70 2d  ..  */.  pC = p-
122f4 3e 61 70 43 73 72 5b 70 31 5d 3b 0a 23 69 66 6e  >apCsr[p1];.#ifn
122f5 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
122f6 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 61  VIRTUALTABLE.  a
122f7 73 73 65 72 74 28 20 70 43 2d 3e 70 56 74 61 62  ssert( pC->pVtab
122f8 43 75 72 73 6f 72 3d 3d 30 20 29 3b 0a 23 65 6e  Cursor==0 );.#en
122f9 64 69 66 0a 20 20 61 73 73 65 72 74 28 20 70 43  dif.  assert( pC
122fa 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 43 2d  !=0 );.  if( pC-
122fb 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 7b 0a 20  >pCursor!=0 ){. 
122fc 20 20 20 2f 2a 20 54 68 65 20 72 65 63 6f 72 64     /* The record
122fd 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 20   is stored in a 
122fe 42 2d 54 72 65 65 20 2a 2f 0a 20 20 20 20 72 63  B-Tree */.    rc
122ff 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75   = sqlite3VdbeCu
12300 72 73 6f 72 4d 6f 76 65 74 6f 28 70 43 29 3b 0a  rsorMoveto(pC);.
12301 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74      if( rc ) got
12302 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65  o abort_due_to_e
12303 72 72 6f 72 3b 0a 20 20 20 20 7a 52 65 63 20 3d  rror;.    zRec =
12304 20 30 3b 0a 20 20 20 20 70 43 72 73 72 20 3d 20   0;.    pCrsr = 
12305 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 20  pC->pCursor;.   
12306 20 69 66 28 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77   if( pC->nullRow
12307 20 29 7b 0a 20 20 20 20 20 20 70 61 79 6c 6f 61   ){.      payloa
12308 64 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7d  dSize = 0;.    }
12309 65 6c 73 65 20 69 66 28 20 70 43 2d 3e 63 61 63  else if( pC->cac
1230a 68 65 53 74 61 74 75 73 3d 3d 70 2d 3e 63 61 63  heStatus==p->cac
1230b 68 65 43 74 72 20 29 7b 0a 20 20 20 20 20 20 70  heCtr ){.      p
1230c 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 70 43 2d  ayloadSize = pC-
1230d 3e 70 61 79 6c 6f 61 64 53 69 7a 65 3b 0a 20 20  >payloadSize;.  
1230e 20 20 20 20 7a 52 65 63 20 3d 20 28 63 68 61 72      zRec = (char
1230f 2a 29 70 43 2d 3e 61 52 6f 77 3b 0a 20 20 20 20  *)pC->aRow;.    
12310 7d 65 6c 73 65 20 69 66 28 20 70 43 2d 3e 69 73  }else if( pC->is
12311 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 20 20 69  Index ){.      i
12312 36 34 20 70 61 79 6c 6f 61 64 53 69 7a 65 36 34  64 payloadSize64
12313 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  ;.      sqlite3B
12314 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 72 73  treeKeySize(pCrs
12315 72 2c 20 26 70 61 79 6c 6f 61 64 53 69 7a 65 36  r, &payloadSize6
12316 34 29 3b 0a 20 20 20 20 20 20 70 61 79 6c 6f 61  4);.      payloa
12317 64 53 69 7a 65 20 3d 20 70 61 79 6c 6f 61 64 53  dSize = payloadS
12318 69 7a 65 36 34 3b 0a 20 20 20 20 7d 65 6c 73 65  ize64;.    }else
12319 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  {.      sqlite3B
1231a 74 72 65 65 44 61 74 61 53 69 7a 65 28 70 43 72  treeDataSize(pCr
1231b 73 72 2c 20 26 70 61 79 6c 6f 61 64 53 69 7a 65  sr, &payloadSize
1231c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 46 69  );.    }.    nFi
1231d 65 6c 64 20 3d 20 70 43 2d 3e 6e 46 69 65 6c 64  eld = pC->nField
1231e 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 43  ;.  }else if( pC
1231f 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 20 29 7b  ->pseudoTable ){
12320 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 63 6f  .    /* The reco
12321 72 64 20 69 73 20 74 68 65 20 73 6f 6c 65 20 65  rd is the sole e
12322 6e 74 72 79 20 6f 66 20 61 20 70 73 65 75 64 6f  ntry of a pseudo
12323 2d 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 70 61  -table */.    pa
12324 79 6c 6f 61 64 53 69 7a 65 20 3d 20 70 43 2d 3e  yloadSize = pC->
12325 6e 44 61 74 61 3b 0a 20 20 20 20 7a 52 65 63 20  nData;.    zRec 
12326 3d 20 70 43 2d 3e 70 44 61 74 61 3b 0a 20 20 20  = pC->pData;.   
12327 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73   pC->cacheStatus
12328 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a   = CACHE_STALE;.
12329 20 20 20 20 61 73 73 65 72 74 28 20 70 61 79 6c      assert( payl
1232a 6f 61 64 53 69 7a 65 3d 3d 30 20 7c 7c 20 7a 52  oadSize==0 || zR
1232b 65 63 21 3d 30 20 29 3b 0a 20 20 20 20 6e 46 69  ec!=0 );.    nFi
1232c 65 6c 64 20 3d 20 70 43 2d 3e 6e 46 69 65 6c 64  eld = pC->nField
1232d 3b 0a 20 20 20 20 70 43 72 73 72 20 3d 20 30 3b  ;.    pCrsr = 0;
1232e 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 52  .  }else{.    zR
1232f 65 63 20 3d 20 30 3b 0a 20 20 20 20 70 61 79 6c  ec = 0;.    payl
12330 6f 61 64 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20  oadSize = 0;.   
12331 20 70 43 72 73 72 20 3d 20 30 3b 0a 20 20 20 20   pCrsr = 0;.    
12332 6e 46 69 65 6c 64 20 3d 20 30 3b 0a 20 20 7d 0a  nField = 0;.  }.
12333 0a 20 20 2f 2a 20 49 66 20 70 61 79 6c 6f 61 64  .  /* If payload
12334 53 69 7a 65 20 69 73 20 30 2c 20 74 68 65 6e 20  Size is 0, then 
12335 6a 75 73 74 20 70 75 73 68 20 61 20 4e 55 4c 4c  just push a NULL
12336 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 2e   onto the stack.
12337 20 2a 2f 0a 20 20 69 66 28 20 70 61 79 6c 6f 61   */.  if( payloa
12338 64 53 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20  dSize==0 ){.    
12339 61 73 73 65 72 74 28 20 70 54 6f 73 2d 3e 66 6c  assert( pTos->fl
1233a 61 67 73 3d 3d 4d 45 4d 5f 4e 75 6c 6c 20 29 3b  ags==MEM_Null );
1233b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a  .    break;.  }.
1233c 20 20 69 66 28 20 70 61 79 6c 6f 61 64 53 69 7a    if( payloadSiz
1233d 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e  e>SQLITE_MAX_LEN
1233e 47 54 48 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  GTH ){.    goto 
1233f 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 0a 20 20  too_big;.  }..  
12340 61 73 73 65 72 74 28 20 70 32 3c 6e 46 69 65 6c  assert( p2<nFiel
12341 64 20 29 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20  d );..  /* Read 
12342 61 6e 64 20 70 61 72 73 65 20 74 68 65 20 74 61  and parse the ta
12343 62 6c 65 20 68 65 61 64 65 72 2e 20 20 53 74 6f  ble header.  Sto
12344 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f  re the results o
12345 66 20 74 68 65 20 70 61 72 73 65 0a 20 20 2a 2a  f the parse.  **
12346 20 69 6e 74 6f 20 74 68 65 20 72 65 63 6f 72 64   into the record
12347 20 68 65 61 64 65 72 20 63 61 63 68 65 20 66 69   header cache fi
12348 65 6c 64 73 20 6f 66 20 74 68 65 20 63 75 72 73  elds of the curs
12349 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  or..  */.  if( p
1234a 43 20 26 26 20 70 43 2d 3e 63 61 63 68 65 53 74  C && pC->cacheSt
1234b 61 74 75 73 3d 3d 70 2d 3e 63 61 63 68 65 43 74  atus==p->cacheCt
1234c 72 20 29 7b 0a 20 20 20 20 61 54 79 70 65 20 3d  r ){.    aType =
1234d 20 70 43 2d 3e 61 54 79 70 65 3b 0a 20 20 20 20   pC->aType;.    
1234e 61 4f 66 66 73 65 74 20 3d 20 70 43 2d 3e 61 4f  aOffset = pC->aO
1234f 66 66 73 65 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a  ffset;.  }else{.
12350 20 20 20 20 75 38 20 2a 7a 49 64 78 3b 20 20 20      u8 *zIdx;   
12351 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e       /* Index in
12352 74 6f 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20  to header */.   
12353 20 75 38 20 2a 7a 45 6e 64 48 64 72 3b 20 20 20   u8 *zEndHdr;   
12354 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
12355 66 69 72 73 74 20 62 79 74 65 20 61 66 74 65 72  first byte after
12356 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20   the header */. 
12357 20 20 20 75 33 32 20 6f 66 66 73 65 74 3b 20 20     u32 offset;  
12358 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e      /* Offset in
12359 74 6f 20 74 68 65 20 64 61 74 61 20 2a 2f 0a 20  to the data */. 
1235a 20 20 20 69 6e 74 20 73 7a 48 64 72 53 7a 3b 20     int szHdrSz; 
1235b 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74      /* Size of t
1235c 68 65 20 68 65 61 64 65 72 20 73 69 7a 65 20 66  he header size f
1235d 69 65 6c 64 20 61 74 20 73 74 61 72 74 20 6f 66  ield at start of
1235e 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 69   record */.    i
1235f 6e 74 20 61 76 61 69 6c 3b 20 20 20 20 20 20 20  nt avail;       
12360 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
12361 65 73 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20  es of available 
12362 64 61 74 61 20 2a 2f 0a 0a 20 20 20 20 61 54 79  data */..    aTy
12363 70 65 20 3d 20 70 43 2d 3e 61 54 79 70 65 3b 0a  pe = pC->aType;.
12364 20 20 20 20 69 66 28 20 61 54 79 70 65 3d 3d 30      if( aType==0
12365 20 29 7b 0a 20 20 20 20 20 20 70 43 2d 3e 61 54   ){.      pC->aT
12366 79 70 65 20 3d 20 61 54 79 70 65 20 3d 20 73 71  ype = aType = sq
12367 6c 69 74 65 4d 61 6c 6c 6f 63 52 61 77 28 20 32  liteMallocRaw( 2
12368 2a 6e 46 69 65 6c 64 2a 73 69 7a 65 6f 66 28 61  *nField*sizeof(a
12369 54 79 70 65 29 20 29 3b 0a 20 20 20 20 7d 0a 20  Type) );.    }. 
1236a 20 20 20 69 66 28 20 61 54 79 70 65 3d 3d 30 20     if( aType==0 
1236b 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f  ){.      goto no
1236c 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  _mem;.    }.    
1236d 70 43 2d 3e 61 4f 66 66 73 65 74 20 3d 20 61 4f  pC->aOffset = aO
1236e 66 66 73 65 74 20 3d 20 26 61 54 79 70 65 5b 6e  ffset = &aType[n
1236f 46 69 65 6c 64 5d 3b 0a 20 20 20 20 70 43 2d 3e  Field];.    pC->
12370 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 70 61  payloadSize = pa
12371 79 6c 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20 70  yloadSize;.    p
12372 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d  C->cacheStatus =
12373 20 70 2d 3e 63 61 63 68 65 43 74 72 3b 0a 0a 20   p->cacheCtr;.. 
12374 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74     /* Figure out
12375 20 68 6f 77 20 6d 61 6e 79 20 62 79 74 65 73 20   how many bytes 
12376 61 72 65 20 69 6e 20 74 68 65 20 68 65 61 64 65  are in the heade
12377 72 20 2a 2f 0a 20 20 20 20 69 66 28 20 7a 52 65  r */.    if( zRe
12378 63 20 29 7b 0a 20 20 20 20 20 20 7a 44 61 74 61  c ){.      zData
12379 20 3d 20 7a 52 65 63 3b 0a 20 20 20 20 7d 65 6c   = zRec;.    }el
1237a 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43  se{.      if( pC
1237b 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20  ->isIndex ){.   
1237c 20 20 20 20 20 7a 44 61 74 61 20 3d 20 28 63 68       zData = (ch
1237d 61 72 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65  ar*)sqlite3Btree
1237e 4b 65 79 46 65 74 63 68 28 70 43 72 73 72 2c 20  KeyFetch(pCrsr, 
1237f 26 61 76 61 69 6c 29 3b 0a 20 20 20 20 20 20 7d  &avail);.      }
12380 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 44  else{.        zD
12381 61 74 61 20 3d 20 28 63 68 61 72 2a 29 73 71 6c  ata = (char*)sql
12382 69 74 65 33 42 74 72 65 65 44 61 74 61 46 65 74  ite3BtreeDataFet
12383 63 68 28 70 43 72 73 72 2c 20 26 61 76 61 69 6c  ch(pCrsr, &avail
12384 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
12385 20 2f 2a 20 49 66 20 4b 65 79 46 65 74 63 68 28   /* If KeyFetch(
12386 29 2f 44 61 74 61 46 65 74 63 68 28 29 20 6d 61  )/DataFetch() ma
12387 6e 61 67 65 64 20 74 6f 20 67 65 74 20 74 68 65  naged to get the
12388 20 65 6e 74 69 72 65 20 70 61 79 6c 6f 61 64 2c   entire payload,
12389 0a 20 20 20 20 20 20 2a 2a 20 73 61 76 65 20 74  .      ** save t
1238a 68 65 20 70 61 79 6c 6f 61 64 20 69 6e 20 74 68  he payload in th
1238b 65 20 70 43 2d 3e 61 52 6f 77 20 63 61 63 68 65  e pC->aRow cache
1238c 2e 20 20 54 68 61 74 20 77 69 6c 6c 20 73 61 76  .  That will sav
1238d 65 20 75 73 20 66 72 6f 6d 0a 20 20 20 20 20 20  e us from.      
1238e 2a 2a 20 68 61 76 69 6e 67 20 74 6f 20 6d 61 6b  ** having to mak
1238f 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 61 6c  e additional cal
12390 6c 73 20 74 6f 20 66 65 74 63 68 20 74 68 65 20  ls to fetch the 
12391 63 6f 6e 74 65 6e 74 20 70 6f 72 74 69 6f 6e 20  content portion 
12392 6f 66 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  of.      ** the 
12393 72 65 63 6f 72 64 2e 0a 20 20 20 20 20 20 2a 2f  record..      */
12394 0a 20 20 20 20 20 20 69 66 28 20 61 76 61 69 6c  .      if( avail
12395 3e 3d 70 61 79 6c 6f 61 64 53 69 7a 65 20 29 7b  >=payloadSize ){
12396 0a 20 20 20 20 20 20 20 20 7a 52 65 63 20 3d 20  .        zRec = 
12397 7a 44 61 74 61 3b 0a 20 20 20 20 20 20 20 20 70  zData;.        p
12398 43 2d 3e 61 52 6f 77 20 3d 20 28 75 38 2a 29 7a  C->aRow = (u8*)z
12399 44 61 74 61 3b 0a 20 20 20 20 20 20 7d 65 6c 73  Data;.      }els
1239a 65 7b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 61  e{.        pC->a
1239b 52 6f 77 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  Row = 0;.      }
1239c 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 54 68  .    }.    /* Th
1239d 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65  e following asse
1239e 72 74 20 69 73 20 74 72 75 65 20 69 6e 20 61 6c  rt is true in al
1239f 6c 20 63 61 73 65 73 20 61 63 63 65 70 74 20 77  l cases accept w
123a0 68 65 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 64  hen.    ** the d
123a1 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 73  atabase file has
123a2 20 62 65 65 6e 20 63 6f 72 72 75 70 74 65 64 20   been corrupted 
123a3 65 78 74 65 72 6e 61 6c 6c 79 2e 0a 20 20 20 20  externally..    
123a4 2a 2a 20 20 20 20 61 73 73 65 72 74 28 20 7a 52  **    assert( zR
123a5 65 63 21 3d 30 20 7c 7c 20 61 76 61 69 6c 3e 3d  ec!=0 || avail>=
123a6 70 61 79 6c 6f 61 64 53 69 7a 65 20 7c 7c 20 61  payloadSize || a
123a7 76 61 69 6c 3e 3d 39 20 29 3b 20 2a 2f 0a 20 20  vail>=9 ); */.  
123a8 20 20 73 7a 48 64 72 53 7a 20 3d 20 47 65 74 56    szHdrSz = GetV
123a9 61 72 69 6e 74 28 28 75 38 2a 29 7a 44 61 74 61  arint((u8*)zData
123aa 2c 20 6f 66 66 73 65 74 29 3b 0a 0a 20 20 20 20  , offset);..    
123ab 2f 2a 20 54 68 65 20 4b 65 79 46 65 74 63 68 28  /* The KeyFetch(
123ac 29 20 6f 72 20 44 61 74 61 46 65 74 63 68 28 29  ) or DataFetch()
123ad 20 61 62 6f 76 65 20 61 72 65 20 66 61 73 74 20   above are fast 
123ae 61 6e 64 20 77 69 6c 6c 20 67 65 74 20 74 68 65  and will get the
123af 20 65 6e 74 69 72 65 0a 20 20 20 20 2a 2a 20 72   entire.    ** r
123b0 65 63 6f 72 64 20 68 65 61 64 65 72 20 69 6e 20  ecord header in 
123b1 6d 6f 73 74 20 63 61 73 65 73 2e 20 20 42 75 74  most cases.  But
123b2 20 74 68 65 79 20 77 69 6c 6c 20 66 61 69 6c 20   they will fail 
123b3 74 6f 20 67 65 74 20 74 68 65 20 63 6f 6d 70 6c  to get the compl
123b4 65 74 65 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72  ete.    ** recor
123b5 64 20 68 65 61 64 65 72 20 69 66 20 74 68 65 20  d header if the 
123b6 72 65 63 6f 72 64 20 68 65 61 64 65 72 20 64 6f  record header do
123b7 65 73 20 6e 6f 74 20 66 69 74 20 6f 6e 20 61 20  es not fit on a 
123b8 73 69 6e 67 6c 65 20 70 61 67 65 0a 20 20 20 20  single page.    
123b9 2a 2a 20 69 6e 20 74 68 65 20 42 2d 54 72 65 65  ** in the B-Tree
123ba 2e 20 20 57 68 65 6e 20 74 68 61 74 20 68 61 70  .  When that hap
123bb 70 65 6e 73 2c 20 75 73 65 20 73 71 6c 69 74 65  pens, use sqlite
123bc 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65  3VdbeMemFromBtre
123bd 65 28 29 20 74 6f 0a 20 20 20 20 2a 2a 20 61 63  e() to.    ** ac
123be 71 75 69 72 65 20 74 68 65 20 63 6f 6d 70 6c 65  quire the comple
123bf 74 65 20 68 65 61 64 65 72 20 74 65 78 74 2e 0a  te header text..
123c0 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21      */.    if( !
123c1 7a 52 65 63 20 26 26 20 61 76 61 69 6c 3c 6f 66  zRec && avail<of
123c2 66 73 65 74 20 29 7b 0a 20 20 20 20 20 20 72 63  fset ){.      rc
123c3 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65   = sqlite3VdbeMe
123c4 6d 46 72 6f 6d 42 74 72 65 65 28 70 43 72 73 72  mFromBtree(pCrsr
123c5 2c 20 30 2c 20 6f 66 66 73 65 74 2c 20 70 43 2d  , 0, offset, pC-
123c6 3e 69 73 49 6e 64 65 78 2c 20 26 73 4d 65 6d 29  >isIndex, &sMem)
123c7 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
123c8 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
123c9 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c       goto op_col
123ca 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d  umn_out;.      }
123cb 0a 20 20 20 20 20 20 7a 44 61 74 61 20 3d 20 73  .      zData = s
123cc 4d 65 6d 2e 7a 3b 0a 20 20 20 20 7d 0a 20 20 20  Mem.z;.    }.   
123cd 20 7a 45 6e 64 48 64 72 20 3d 20 28 75 38 20 2a   zEndHdr = (u8 *
123ce 29 26 7a 44 61 74 61 5b 6f 66 66 73 65 74 5d 3b  )&zData[offset];
123cf 0a 20 20 20 20 7a 49 64 78 20 3d 20 28 75 38 20  .    zIdx = (u8 
123d0 2a 29 26 7a 44 61 74 61 5b 73 7a 48 64 72 53 7a  *)&zData[szHdrSz
123d1 5d 3b 0a 0a 20 20 20 20 2f 2a 20 53 63 61 6e 20  ];..    /* Scan 
123d2 74 68 65 20 68 65 61 64 65 72 20 61 6e 64 20 75  the header and u
123d3 73 65 20 69 74 20 74 6f 20 66 69 6c 6c 20 69 6e  se it to fill in
123d4 20 74 68 65 20 61 54 79 70 65 5b 5d 20 61 6e 64   the aType[] and
123d5 20 61 4f 66 66 73 65 74 5b 5d 0a 20 20 20 20 2a   aOffset[].    *
123d6 2a 20 61 72 72 61 79 73 2e 20 20 61 54 79 70 65  * arrays.  aType
123d7 5b 69 5d 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e  [i] will contain
123d8 20 74 68 65 20 74 79 70 65 20 69 6e 74 65 67 65   the type intege
123d9 72 20 66 6f 72 20 74 68 65 20 69 2d 74 68 0a 20  r for the i-th. 
123da 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 61 6e 64     ** column and
123db 20 61 4f 66 66 73 65 74 5b 69 5d 20 77 69 6c 6c   aOffset[i] will
123dc 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 6f 66 66   contain the off
123dd 73 65 74 20 66 72 6f 6d 20 74 68 65 20 62 65 67  set from the beg
123de 69 6e 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 6f 66  inning.    ** of
123df 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 74   the record to t
123e0 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20  he start of the 
123e1 64 61 74 61 20 66 6f 72 20 74 68 65 20 69 2d 74  data for the i-t
123e2 68 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2f 0a  h column.    */.
123e3 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
123e4 46 69 65 6c 64 3b 20 69 2b 2b 29 7b 0a 20 20 20  Field; i++){.   
123e5 20 20 20 69 66 28 20 7a 49 64 78 3c 7a 45 6e 64     if( zIdx<zEnd
123e6 48 64 72 20 29 7b 0a 20 20 20 20 20 20 20 20 61  Hdr ){.        a
123e7 4f 66 66 73 65 74 5b 69 5d 20 3d 20 6f 66 66 73  Offset[i] = offs
123e8 65 74 3b 0a 20 20 20 20 20 20 20 20 7a 49 64 78  et;.        zIdx
123e9 20 2b 3d 20 47 65 74 56 61 72 69 6e 74 28 7a 49   += GetVarint(zI
123ea 64 78 2c 20 61 54 79 70 65 5b 69 5d 29 3b 0a 20  dx, aType[i]);. 
123eb 20 20 20 20 20 20 20 6f 66 66 73 65 74 20 2b 3d         offset +=
123ec 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
123ed 61 6c 54 79 70 65 4c 65 6e 28 61 54 79 70 65 5b  alTypeLen(aType[
123ee 69 5d 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  i]);.      }else
123ef 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20  {.        /* If 
123f0 69 20 69 73 20 6c 65 73 73 20 74 68 61 74 20 6e  i is less that n
123f1 46 69 65 6c 64 2c 20 74 68 65 6e 20 74 68 65 72  Field, then ther
123f2 65 20 61 72 65 20 6c 65 73 73 20 66 69 65 6c 64  e are less field
123f3 73 20 69 6e 20 74 68 69 73 0a 20 20 20 20 20 20  s in this.      
123f4 20 20 2a 2a 20 72 65 63 6f 72 64 20 74 68 61 6e    ** record than
123f5 20 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 20 69   SetNumColumns i
123f6 6e 64 69 63 61 74 65 64 20 74 68 65 72 65 20 61  ndicated there a
123f7 72 65 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  re columns in th
123f8 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 61 62  e.        ** tab
123f9 6c 65 2e 20 53 65 74 20 74 68 65 20 6f 66 66 73  le. Set the offs
123fa 65 74 20 66 6f 72 20 61 6e 79 20 65 78 74 72 61  et for any extra
123fb 20 63 6f 6c 75 6d 6e 73 20 6e 6f 74 20 70 72 65   columns not pre
123fc 73 65 6e 74 20 69 6e 0a 20 20 20 20 20 20 20 20  sent in.        
123fd 2a 2a 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f  ** the record to
123fe 20 30 2e 20 54 68 69 73 20 74 65 6c 6c 73 20 63   0. This tells c
123ff 6f 64 65 20 62 65 6c 6f 77 20 74 6f 20 70 75 73  ode below to pus
12400 68 20 61 20 4e 55 4c 4c 20 6f 6e 74 6f 20 74 68  h a NULL onto th
12401 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 74 61  e.        ** sta
12402 63 6b 20 69 6e 73 74 65 61 64 20 6f 66 20 64 65  ck instead of de
12403 73 65 72 69 61 6c 69 7a 69 6e 67 20 61 20 76 61  serializing a va
12404 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 72 65 63  lue from the rec
12405 6f 72 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  ord..        */.
12406 20 20 20 20 20 20 20 20 61 4f 66 66 73 65 74 5b          aOffset[
12407 69 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  i] = 0;.      }.
12408 20 20 20 20 7d 0a 20 20 20 20 52 65 6c 65 61 73      }.    Releas
12409 65 28 26 73 4d 65 6d 29 3b 0a 20 20 20 20 73 4d  e(&sMem);.    sM
1240a 65 6d 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e  em.flags = MEM_N
1240b 75 6c 6c 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20  ull;..    /* If 
1240c 77 65 20 68 61 76 65 20 72 65 61 64 20 6d 6f 72  we have read mor
1240d 65 20 68 65 61 64 65 72 20 64 61 74 61 20 74 68  e header data th
1240e 61 6e 20 77 61 73 20 63 6f 6e 74 61 69 6e 65 64  an was contained
1240f 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 2c 0a   in the header,.
12410 20 20 20 20 2a 2a 20 6f 72 20 69 66 20 74 68 65      ** or if the
12411 20 65 6e 64 20 6f 66 20 74 68 65 20 6c 61 73 74   end of the last
12412 20 66 69 65 6c 64 20 61 70 70 65 61 72 73 20 74   field appears t
12413 6f 20 62 65 20 70 61 73 74 20 74 68 65 20 65 6e  o be past the en
12414 64 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20  d of the.    ** 
12415 72 65 63 6f 72 64 2c 20 74 68 65 6e 20 77 65 20  record, then we 
12416 6d 75 73 74 20 62 65 20 64 65 61 6c 69 6e 67 20  must be dealing 
12417 77 69 74 68 20 61 20 63 6f 72 72 75 70 74 20 64  with a corrupt d
12418 61 74 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f 0a  atabase..    */.
12419 20 20 20 20 69 66 28 20 7a 49 64 78 3e 7a 45 6e      if( zIdx>zEn
1241a 64 48 64 72 20 7c 7c 20 6f 66 66 73 65 74 3e 70  dHdr || offset>p
1241b 61 79 6c 6f 61 64 53 69 7a 65 20 29 7b 0a 20 20  ayloadSize ){.  
1241c 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1241d 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
1241e 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75      goto op_colu
1241f 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20  mn_out;.    }.  
12420 7d 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68 65 20  }..  /* Get the 
12421 63 6f 6c 75 6d 6e 20 69 6e 66 6f 72 6d 61 74 69  column informati
12422 6f 6e 2e 20 49 66 20 61 4f 66 66 73 65 74 5b 70  on. If aOffset[p
12423 32 5d 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  2] is non-zero, 
12424 74 68 65 6e 20 0a 20 20 2a 2a 20 64 65 73 65 72  then .  ** deser
12425 69 61 6c 69 7a 65 20 74 68 65 20 76 61 6c 75 65  ialize the value
12426 20 66 72 6f 6d 20 74 68 65 20 72 65 63 6f 72 64   from the record
12427 2e 20 49 66 20 61 4f 66 66 73 65 74 5b 70 32 5d  . If aOffset[p2]
12428 20 69 73 20 7a 65 72 6f 2c 0a 20 20 2a 2a 20 74   is zero,.  ** t
12429 68 65 6e 20 74 68 65 72 65 20 61 72 65 20 6e 6f  hen there are no
1242a 74 20 65 6e 6f 75 67 68 20 66 69 65 6c 64 73 20  t enough fields 
1242b 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f  in the record to
1242c 20 73 61 74 69 73 66 79 20 74 68 65 0a 20 20 2a   satisfy the.  *
1242d 2a 20 72 65 71 75 65 73 74 2e 20 20 49 6e 20 74  * request.  In t
1242e 68 69 73 20 63 61 73 65 2c 20 73 65 74 20 74 68  his case, set th
1242f 65 20 76 61 6c 75 65 20 4e 55 4c 4c 20 6f 72 20  e value NULL or 
12430 74 6f 20 50 33 20 69 66 20 50 33 20 69 73 0a 20  to P3 if P3 is. 
12431 20 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   ** a pointer to
12432 20 61 20 4d 65 6d 20 6f 62 6a 65 63 74 2e 0a 20   a Mem object.. 
12433 20 2a 2f 0a 20 20 69 66 28 20 61 4f 66 66 73 65   */.  if( aOffse
12434 74 5b 70 32 5d 20 29 7b 0a 20 20 20 20 61 73 73  t[p2] ){.    ass
12435 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
12436 4f 4b 20 29 3b 0a 20 20 20 20 69 66 28 20 7a 52  OK );.    if( zR
12437 65 63 20 29 7b 0a 20 20 20 20 20 20 7a 44 61 74  ec ){.      zDat
12438 61 20 3d 20 26 7a 52 65 63 5b 61 4f 66 66 73 65  a = &zRec[aOffse
12439 74 5b 70 32 5d 5d 3b 0a 20 20 20 20 7d 65 6c 73  t[p2]];.    }els
1243a 65 7b 0a 20 20 20 20 20 20 6c 65 6e 20 3d 20 73  e{.      len = s
1243b 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
1243c 54 79 70 65 4c 65 6e 28 61 54 79 70 65 5b 70 32  TypeLen(aType[p2
1243d 5d 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  ]);.      rc = s
1243e 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f  qlite3VdbeMemFro
1243f 6d 42 74 72 65 65 28 70 43 72 73 72 2c 20 61 4f  mBtree(pCrsr, aO
12440 66 66 73 65 74 5b 70 32 5d 2c 20 6c 65 6e 2c 20  ffset[p2], len, 
12441 70 43 2d 3e 69 73 49 6e 64 65 78 2c 26 73 4d 65  pC->isIndex,&sMe
12442 6d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  m);.      if( rc
12443 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
12444 20 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63         goto op_c
12445 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 20  olumn_out;.     
12446 20 7d 0a 20 20 20 20 20 20 7a 44 61 74 61 20 3d   }.      zData =
12447 20 73 4d 65 6d 2e 7a 3b 0a 20 20 20 20 7d 0a 20   sMem.z;.    }. 
12448 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
12449 72 69 61 6c 47 65 74 28 28 75 38 2a 29 7a 44 61  rialGet((u8*)zDa
1244a 74 61 2c 20 61 54 79 70 65 5b 70 32 5d 2c 20 70  ta, aType[p2], p
1244b 54 6f 73 29 3b 0a 20 20 20 20 70 54 6f 73 2d 3e  Tos);.    pTos->
1244c 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a  enc = encoding;.
1244d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28    }else{.    if(
1244e 20 70 4f 70 2d 3e 70 33 74 79 70 65 3d 3d 50 33   pOp->p3type==P3
1244f 5f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 73 71  _MEM ){.      sq
12450 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c  lite3VdbeMemShal
12451 6c 6f 77 43 6f 70 79 28 70 54 6f 73 2c 20 28 4d  lowCopy(pTos, (M
12452 65 6d 20 2a 29 28 70 4f 70 2d 3e 70 33 29 2c 20  em *)(pOp->p3), 
12453 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20 20  MEM_Static);.   
12454 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 54   }else{.      pT
12455 6f 73 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  os->flags = MEM_
12456 4e 75 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Null;.    }.  }.
12457 0a 20 20 2f 2a 20 49 66 20 77 65 20 64 79 6e 61  .  /* If we dyna
12458 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65  mically allocate
12459 64 20 73 70 61 63 65 20 74 6f 20 68 6f 6c 64 20  d space to hold 
1245a 74 68 65 20 64 61 74 61 20 28 69 6e 20 74 68 65  the data (in the
1245b 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 64 62  .  ** sqlite3Vdb
1245c 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 29 20  eMemFromBtree() 
1245d 63 61 6c 6c 20 61 62 6f 76 65 29 20 74 68 65 6e  call above) then
1245e 20 74 72 61 6e 73 66 65 72 20 63 6f 6e 74 72 6f   transfer contro
1245f 6c 20 6f 66 20 74 68 61 74 0a 20 20 2a 2a 20 64  l of that.  ** d
12460 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63  ynamically alloc
12461 61 74 65 64 20 73 70 61 63 65 20 6f 76 65 72 20  ated space over 
12462 74 6f 20 74 68 65 20 70 54 6f 73 20 73 74 72 75  to the pTos stru
12463 63 74 75 72 65 2e 0a 20 20 2a 2a 20 54 68 69 73  cture..  ** This
12464 20 70 72 65 76 65 6e 74 73 20 61 20 6d 65 6d 6f   prevents a memo
12465 72 79 20 63 6f 70 79 2e 0a 20 20 2a 2f 0a 20 20  ry copy..  */.  
12466 69 66 28 20 28 73 4d 65 6d 2e 66 6c 61 67 73 20  if( (sMem.flags 
12467 26 20 4d 45 4d 5f 44 79 6e 29 21 3d 30 20 29 7b  & MEM_Dyn)!=0 ){
12468 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 6f  .    assert( pTo
12469 73 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 45  s->flags & MEM_E
1246a 70 68 65 6d 20 29 3b 0a 20 20 20 20 61 73 73 65  phem );.    asse
1246b 72 74 28 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20  rt( pTos->flags 
1246c 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42  & (MEM_Str|MEM_B
1246d 6c 6f 62 29 20 29 3b 0a 20 20 20 20 61 73 73 65  lob) );.    asse
1246e 72 74 28 20 70 54 6f 73 2d 3e 7a 3d 3d 73 4d 65  rt( pTos->z==sMe
1246f 6d 2e 7a 20 29 3b 0a 20 20 20 20 61 73 73 65 72  m.z );.    asser
12470 74 28 20 73 4d 65 6d 2e 66 6c 61 67 73 20 26 20  t( sMem.flags & 
12471 4d 45 4d 5f 54 65 72 6d 20 29 3b 0a 20 20 20 20  MEM_Term );.    
12472 70 54 6f 73 2d 3e 66 6c 61 67 73 20 26 3d 20 7e  pTos->flags &= ~
12473 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20 70  MEM_Ephem;.    p
12474 54 6f 73 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45  Tos->flags |= ME
12475 4d 5f 44 79 6e 7c 4d 45 4d 5f 54 65 72 6d 3b 0a  M_Dyn|MEM_Term;.
12476 20 20 7d 0a 0a 20 20 2f 2a 20 70 54 6f 73 2d 3e    }..  /* pTos->
12477 7a 20 6d 69 67 68 74 20 62 65 20 70 6f 69 6e 74  z might be point
12478 69 6e 67 20 74 6f 20 73 4d 65 6d 2e 7a 53 68 6f  ing to sMem.zSho
12479 72 74 5b 5d 2e 20 20 46 69 78 20 74 68 61 74 20  rt[].  Fix that 
1247a 73 6f 20 74 68 61 74 20 77 65 0a 20 20 2a 2a 20  so that we.  ** 
1247b 63 61 6e 20 61 62 61 6e 64 6f 6e 20 73 4d 65 6d  can abandon sMem
1247c 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   */.  rc = sqlit
1247d 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69  e3VdbeMemMakeWri
1247e 74 65 61 62 6c 65 28 70 54 6f 73 29 3b 0a 0a 6f  teable(pTos);..o
1247f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3a 0a 20 20  p_column_out:.  
12480 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
12481 6f 64 65 3a 20 4d 61 6b 65 52 65 63 6f 72 64 20  ode: MakeRecord 
12482 50 31 20 50 32 20 50 33 0a 2a 2a 0a 2a 2a 20 43  P1 P2 P3.**.** C
12483 6f 6e 76 65 72 74 20 74 68 65 20 74 6f 70 20 61  onvert the top a
12484 62 73 28 50 31 29 20 65 6e 74 72 69 65 73 20 6f  bs(P1) entries o
12485 66 20 74 68 65 20 73 74 61 63 6b 20 69 6e 74 6f  f the stack into
12486 20 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 0a   a single entry.
12487 2a 2a 20 73 75 69 74 61 62 6c 65 20 66 6f 72 20  ** suitable for 
12488 75 73 65 20 61 73 20 61 20 64 61 74 61 20 72 65  use as a data re
12489 63 6f 72 64 20 69 6e 20 61 20 64 61 74 61 62 61  cord in a databa
1248a 73 65 20 74 61 62 6c 65 20 6f 72 20 61 73 20 61  se table or as a
1248b 20 6b 65 79 0a 2a 2a 20 69 6e 20 61 6e 20 69 6e   key.** in an in
1248c 64 65 78 2e 20 20 54 68 65 20 64 65 74 61 69 6c  dex.  The detail
1248d 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 61 74 20  s of the format 
1248e 61 72 65 20 69 72 72 65 6c 61 76 61 6e 74 20 61  are irrelavant a
1248f 73 20 6c 6f 6e 67 20 61 73 0a 2a 2a 20 74 68 65  s long as.** the
12490 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64   OP_Column opcod
12491 65 20 63 61 6e 20 64 65 63 6f 64 65 20 74 68 65  e can decode the
12492 20 72 65 63 6f 72 64 20 6c 61 74 65 72 20 61 6e   record later an
12493 64 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65  d as long as the
12494 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 52  .** sqlite3VdbeR
12495 65 63 6f 72 64 43 6f 6d 70 61 72 65 20 66 75 6e  ecordCompare fun
12496 63 74 69 6f 6e 20 77 69 6c 6c 20 63 6f 72 72 65  ction will corre
12497 63 74 6c 79 20 63 6f 6d 70 61 72 65 20 74 77 6f  ctly compare two
12498 20 65 6e 63 6f 64 65 64 0a 2a 2a 20 72 65 63 6f   encoded.** reco
12499 72 64 73 2e 20 20 52 65 66 65 72 20 74 6f 20 73  rds.  Refer to s
1249a 6f 75 72 63 65 20 63 6f 64 65 20 63 6f 6d 6d 65  ource code comme
1249b 6e 74 73 20 66 6f 72 20 74 68 65 20 64 65 74 61  nts for the deta
1249c 69 6c 73 20 6f 66 20 74 68 65 20 72 65 63 6f 72  ils of the recor
1249d 64 0a 2a 2a 20 66 6f 72 6d 61 74 2e 0a 2a 2a 0a  d.** format..**.
1249e 2a 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20  ** The original 
1249f 73 74 61 63 6b 20 65 6e 74 72 69 65 73 20 61 72  stack entries ar
124a0 65 20 70 6f 70 70 65 64 20 66 72 6f 6d 20 74 68  e popped from th
124a1 65 20 73 74 61 63 6b 20 69 66 20 50 31 3e 30 20  e stack if P1>0 
124a2 62 75 74 0a 2a 2a 20 72 65 6d 61 69 6e 20 6f 6e  but.** remain on
124a3 20 74 68 65 20 73 74 61 63 6b 20 69 66 20 50 31   the stack if P1
124a4 3c 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 32 20  <0..**.** If P2 
124a5 69 73 20 6e 6f 74 20 7a 65 72 6f 20 61 6e 64 20  is not zero and 
124a6 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74  one or more of t
124a7 68 65 20 65 6e 74 72 69 65 73 20 61 72 65 20 4e  he entries are N
124a8 55 4c 4c 2c 20 74 68 65 6e 20 6a 75 6d 70 0a 2a  ULL, then jump.*
124a9 2a 20 74 6f 20 74 68 65 20 61 64 64 72 65 73 73  * to the address
124aa 20 67 69 76 65 6e 20 62 79 20 50 32 2e 20 20 54   given by P2.  T
124ab 68 69 73 20 66 65 61 74 75 72 65 20 63 61 6e 20  his feature can 
124ac 62 65 20 75 73 65 64 20 74 6f 20 73 6b 69 70 20  be used to skip 
124ad 61 0a 2a 2a 20 75 6e 69 71 75 65 6e 65 73 73 20  a.** uniqueness 
124ae 74 65 73 74 20 6f 6e 20 69 6e 64 69 63 65 73 2e  test on indices.
124af 0a 2a 2a 0a 2a 2a 20 50 33 20 6d 61 79 20 62 65  .**.** P3 may be
124b0 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 69   a string that i
124b1 73 20 50 31 20 63 68 61 72 61 63 74 65 72 73 20  s P1 characters 
124b2 6c 6f 6e 67 2e 20 20 54 68 65 20 6e 74 68 20 63  long.  The nth c
124b3 68 61 72 61 63 74 65 72 20 6f 66 20 74 68 65 0a  haracter of the.
124b4 2a 2a 20 73 74 72 69 6e 67 20 69 6e 64 69 63 61  ** string indica
124b5 74 65 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61  tes the column a
124b6 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 68 6f  ffinity that sho
124b7 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72 20  uld be used for 
124b8 74 68 65 20 6e 74 68 0a 2a 2a 20 66 69 65 6c 64  the nth.** field
124b9 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 6b 65   of the index ke
124ba 79 20 28 69 2e 65 2e 20 74 68 65 20 66 69 72 73  y (i.e. the firs
124bb 74 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 50  t character of P
124bc 33 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f  3 corresponds to
124bd 20 74 68 65 0a 2a 2a 20 6c 6f 77 65 73 74 20 65   the.** lowest e
124be 6c 65 6d 65 6e 74 20 6f 6e 20 74 68 65 20 73 74  lement on the st
124bf 61 63 6b 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ack)..**.** The 
124c0 6d 61 70 70 69 6e 67 20 66 72 6f 6d 20 63 68 61  mapping from cha
124c1 72 61 63 74 65 72 20 74 6f 20 61 66 66 69 6e 69  racter to affini
124c2 74 79 20 69 73 20 67 69 76 65 6e 20 62 79 20 74  ty is given by t
124c3 68 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 0a 2a  he SQLITE_AFF_.*
124c4 2a 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 64  * macros defined
124c5 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 2e   in sqliteInt.h.
124c6 0a 2a 2a 0a 2a 2a 20 49 66 20 50 33 20 69 73 20  .**.** If P3 is 
124c7 4e 55 4c 4c 20 74 68 65 6e 20 61 6c 6c 20 69 6e  NULL then all in
124c8 64 65 78 20 66 69 65 6c 64 73 20 68 61 76 65 20  dex fields have 
124c9 74 68 65 20 61 66 66 69 6e 69 74 79 20 4e 4f 4e  the affinity NON
124ca 45 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  E..**.** See als
124cb 6f 20 4f 50 5f 4d 61 6b 65 49 64 78 52 65 63 0a  o OP_MakeIdxRec.
124cc 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 61  */./* Opcode: Ma
124cd 6b 65 49 64 78 52 65 63 20 50 31 20 50 32 20 50  keIdxRec P1 P2 P
124ce 33 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63  3.**.** This opc
124cf 6f 64 65 20 77 6f 72 6b 73 20 6a 75 73 74 20 4f  ode works just O
124d0 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 65 78 63  P_MakeRecord exc
124d1 65 70 74 20 74 68 61 74 20 69 74 20 72 65 61 64  ept that it read
124d2 73 20 61 6e 20 65 78 74 72 61 0a 2a 2a 20 69 6e  s an extra.** in
124d3 74 65 67 65 72 20 66 72 6f 6d 20 74 68 65 20 73  teger from the s
124d4 74 61 63 6b 20 28 74 68 75 73 20 72 65 61 64 69  tack (thus readi
124d5 6e 67 20 61 20 74 6f 74 61 6c 20 6f 66 20 61 62  ng a total of ab
124d6 73 28 50 31 2b 31 29 20 65 6e 74 72 69 65 73 29  s(P1+1) entries)
124d7 0a 2a 2a 20 61 6e 64 20 61 70 70 65 6e 64 73 20  .** and appends 
124d8 74 68 61 74 20 65 78 74 72 61 20 69 6e 74 65 67  that extra integ
124d9 65 72 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66  er to the end of
124da 20 74 68 65 20 72 65 63 6f 72 64 20 61 73 20 61   the record as a
124db 20 76 61 72 69 6e 74 2e 0a 2a 2a 20 54 68 69 73   varint..** This
124dc 20 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 20 69   results in an i
124dd 6e 64 65 78 20 6b 65 79 2e 0a 2a 2f 0a 63 61 73  ndex key..*/.cas
124de 65 20 4f 50 5f 4d 61 6b 65 49 64 78 52 65 63 3a  e OP_MakeIdxRec:
124df 0a 63 61 73 65 20 4f 50 5f 4d 61 6b 65 52 65 63  .case OP_MakeRec
124e0 6f 72 64 3a 20 7b 0a 20 20 2f 2a 20 41 73 73 75  ord: {.  /* Assu
124e1 6d 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64 20  ming the record 
124e2 63 6f 6e 74 61 69 6e 73 20 4e 20 66 69 65 6c 64  contains N field
124e3 73 2c 20 74 68 65 20 72 65 63 6f 72 64 20 66 6f  s, the record fo
124e4 72 6d 61 74 20 6c 6f 6f 6b 73 0a 20 20 2a 2a 20  rmat looks.  ** 
124e5 6c 69 6b 65 20 74 68 69 73 3a 0a 20 20 2a 2a 0a  like this:.  **.
124e6 20 20 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    ** -----------
124e7 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
124e8 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
124e9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
124ea 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 20  -------------.  
124eb 2a 2a 20 7c 20 68 64 72 2d 73 69 7a 65 20 7c 20  ** | hdr-size | 
124ec 74 79 70 65 20 30 20 7c 20 74 79 70 65 20 31 20  type 0 | type 1 
124ed 7c 20 2e 2e 2e 20 7c 20 74 79 70 65 20 4e 2d 31  | ... | type N-1
124ee 20 7c 20 64 61 74 61 30 20 7c 20 2e 2e 2e 20 7c   | data0 | ... |
124ef 20 64 61 74 61 20 4e 2d 31 20 7c 20 0a 20 20 2a   data N-1 | .  *
124f0 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  * --------------
124f1 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
124f2 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
124f3 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
124f4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 0a  ----------.  **.
124f5 20 20 2a 2a 20 44 61 74 61 28 30 29 20 69 73 20    ** Data(0) is 
124f6 74 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20 6c  taken from the l
124f7 6f 77 65 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66  owest element of
124f8 20 74 68 65 20 73 74 61 63 6b 20 61 6e 64 20 64   the stack and d
124f9 61 74 61 28 4e 2d 31 29 20 69 73 0a 20 20 2a 2a  ata(N-1) is.  **
124fa 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20   the top of the 
124fb 73 74 61 63 6b 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  stack..  **.  **
124fc 20 45 61 63 68 20 74 79 70 65 20 66 69 65 6c 64   Each type field
124fd 20 69 73 20 61 20 76 61 72 69 6e 74 20 72 65 70   is a varint rep
124fe 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 73 65  resenting the se
124ff 72 69 61 6c 20 74 79 70 65 20 6f 66 20 74 68 65  rial type of the
12500 20 0a 20 20 2a 2a 20 63 6f 72 72 65 73 70 6f 6e   .  ** correspon
12501 64 69 6e 67 20 64 61 74 61 20 65 6c 65 6d 65 6e  ding data elemen
12502 74 20 28 73 65 65 20 73 71 6c 69 74 65 33 56 64  t (see sqlite3Vd
12503 62 65 53 65 72 69 61 6c 54 79 70 65 28 29 29 2e  beSerialType()).
12504 20 54 68 65 0a 20 20 2a 2a 20 68 64 72 2d 73 69   The.  ** hdr-si
12505 7a 65 20 66 69 65 6c 64 20 69 73 20 61 6c 73 6f  ze field is also
12506 20 61 20 76 61 72 69 6e 74 20 77 68 69 63 68 20   a varint which 
12507 69 73 20 74 68 65 20 6f 66 66 73 65 74 20 66 72  is the offset fr
12508 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  om the beginning
12509 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 65 63  .  ** of the rec
1250a 6f 72 64 20 74 6f 20 64 61 74 61 30 2e 0a 20 20  ord to data0..  
1250b 2a 2f 0a 20 20 75 38 20 2a 7a 4e 65 77 52 65 63  */.  u8 *zNewRec
1250c 6f 72 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 41  ord;        /* A
1250d 20 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20   buffer to hold 
1250e 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 65  the data for the
1250f 20 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20   new record */. 
12510 20 4d 65 6d 20 2a 70 52 65 63 3b 20 20 20 20 20   Mem *pRec;     
12511 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e          /* The n
12512 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 4d  ew record */.  M
12513 65 6d 20 2a 70 52 6f 77 69 64 20 3d 20 30 3b 20  em *pRowid = 0; 
12514 20 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 20 61        /* Rowid a
12515 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20 6e  ppended to the n
12516 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75  ew record */.  u
12517 36 34 20 6e 44 61 74 61 20 3d 20 30 3b 20 20 20  64 nData = 0;   
12518 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
12519 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61  of bytes of data
1251a 20 73 70 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20   space */.  int 
1251b 6e 48 64 72 20 3d 20 30 3b 20 20 20 20 20 20 20  nHdr = 0;       
1251c 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1251d 62 79 74 65 73 20 6f 66 20 68 65 61 64 65 72 20  bytes of header 
1251e 73 70 61 63 65 20 2a 2f 0a 20 20 75 36 34 20 6e  space */.  u64 n
1251f 42 79 74 65 20 3d 20 30 3b 20 20 20 20 20 20 20  Byte = 0;       
12520 20 20 2f 2a 20 44 61 74 61 20 73 70 61 63 65 20    /* Data space 
12521 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 69  required for thi
12522 73 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e  s record */.  in
12523 74 20 6e 5a 65 72 6f 20 3d 20 30 3b 20 20 20 20  t nZero = 0;    
12524 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
12525 66 20 7a 65 72 6f 20 62 79 74 65 73 20 61 74 20  f zero bytes at 
12526 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 72  the end of the r
12527 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  ecord */.  int n
12528 56 61 72 69 6e 74 3b 20 20 20 20 20 20 20 20 20  Varint;         
12529 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
1252a 79 74 65 73 20 69 6e 20 61 20 76 61 72 69 6e 74  ytes in a varint
1252b 20 2a 2f 0a 20 20 75 33 32 20 73 65 72 69 61 6c   */.  u32 serial
1252c 5f 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20  _type;       /* 
1252d 54 79 70 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20  Type field */.  
1252e 69 6e 74 20 63 6f 6e 74 61 69 6e 73 4e 75 6c 6c  int containsNull
1252f 20 3d 20 30 3b 20 20 2f 2a 20 54 72 75 65 20 69   = 0;  /* True i
12530 66 20 61 6e 79 20 6f 66 20 74 68 65 20 64 61 74  f any of the dat
12531 61 20 66 69 65 6c 64 73 20 61 72 65 20 4e 55 4c  a fields are NUL
12532 4c 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 44 61 74  L */.  Mem *pDat
12533 61 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  a0;           /*
12534 20 42 6f 74 74 6f 6d 20 6f 66 20 74 68 65 20 73   Bottom of the s
12535 74 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 6c 65  tack */.  int le
12536 61 76 65 4f 6e 53 74 61 63 6b 3b 20 20 20 20 20  aveOnStack;     
12537 20 2f 2a 20 49 66 20 74 72 75 65 2c 20 6c 65 61   /* If true, lea
12538 76 65 20 74 68 65 20 65 6e 74 72 69 65 73 20 6f  ve the entries o
12539 6e 20 74 68 65 20 73 74 61 63 6b 20 2a 2f 0a 20  n the stack */. 
1253a 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20   int nField;    
1253b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
1253c 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74  r of fields in t
1253d 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69  he record */.  i
1253e 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 3b 20 20  nt jumpIfNull;  
1253f 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65        /* Jump he
12540 72 65 20 69 66 20 6e 6f 6e 2d 7a 65 72 6f 20 61  re if non-zero a
12541 6e 64 20 61 6e 79 20 65 6e 74 72 69 65 73 20 61  nd any entries a
12542 72 65 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 69 6e  re NULL. */.  in
12543 74 20 61 64 64 52 6f 77 69 64 3b 20 20 20 20 20  t addRowid;     
12544 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20       /* True to 
12545 61 70 70 65 6e 64 20 61 20 72 6f 77 69 64 20 63  append a rowid c
12546 6f 6c 75 6d 6e 20 61 74 20 74 68 65 20 65 6e 64  olumn at the end
12547 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 41 66 66   */.  char *zAff
12548 69 6e 69 74 79 3b 20 20 20 20 20 20 20 2f 2a 20  inity;       /* 
12549 54 68 65 20 61 66 66 69 6e 69 74 79 20 73 74 72  The affinity str
1254a 69 6e 67 20 66 6f 72 20 74 68 65 20 72 65 63 6f  ing for the reco
1254b 72 64 20 2a 2f 0a 20 20 69 6e 74 20 66 69 6c 65  rd */.  int file
1254c 5f 66 6f 72 6d 61 74 3b 20 20 20 20 20 20 20 2f  _format;       /
1254d 2a 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 74 6f  * File format to
1254e 20 75 73 65 20 66 6f 72 20 65 6e 63 6f 64 69 6e   use for encodin
1254f 67 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20  g */.  int i;   
12550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
12551 20 53 70 61 63 65 20 75 73 65 64 20 69 6e 20 7a   Space used in z
12552 4e 65 77 52 65 63 6f 72 64 5b 5d 20 2a 2f 0a 20  NewRecord[] */. 
12553 20 63 68 61 72 20 7a 54 65 6d 70 5b 4e 42 46 53   char zTemp[NBFS
12554 5d 3b 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65  ];      /* Space
12555 20 74 6f 20 68 6f 6c 64 20 73 6d 61 6c 6c 20 72   to hold small r
12556 65 63 6f 72 64 73 20 2a 2f 0a 0a 20 20 6c 65 61  ecords */..  lea
12557 76 65 4f 6e 53 74 61 63 6b 20 3d 20 28 28 70 4f  veOnStack = ((pO
12558 70 2d 3e 70 31 3c 30 29 3f 31 3a 30 29 3b 0a 20  p->p1<0)?1:0);. 
12559 20 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70   nField = pOp->p
1255a 31 20 2a 20 28 6c 65 61 76 65 4f 6e 53 74 61 63  1 * (leaveOnStac
1255b 6b 3f 2d 31 3a 31 29 3b 0a 20 20 6a 75 6d 70 49  k?-1:1);.  jumpI
1255c 66 4e 75 6c 6c 20 3d 20 70 4f 70 2d 3e 70 32 3b  fNull = pOp->p2;
1255d 0a 20 20 61 64 64 52 6f 77 69 64 20 3d 20 70 4f  .  addRowid = pO
1255e 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4d 61  p->opcode==OP_Ma
1255f 6b 65 49 64 78 52 65 63 3b 0a 20 20 7a 41 66 66  keIdxRec;.  zAff
12560 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e 70 33 3b  inity = pOp->p3;
12561 0a 0a 20 20 70 44 61 74 61 30 20 3d 20 26 70 54  ..  pData0 = &pT
12562 6f 73 5b 31 2d 6e 46 69 65 6c 64 5d 3b 0a 20 20  os[1-nField];.  
12563 61 73 73 65 72 74 28 20 70 44 61 74 61 30 3e 3d  assert( pData0>=
12564 70 2d 3e 61 53 74 61 63 6b 20 29 3b 0a 20 20 63  p->aStack );.  c
12565 6f 6e 74 61 69 6e 73 4e 75 6c 6c 20 3d 20 30 3b  ontainsNull = 0;
12566 0a 20 20 66 69 6c 65 5f 66 6f 72 6d 61 74 20 3d  .  file_format =
12567 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65   p->minWriteFile
12568 46 6f 72 6d 61 74 3b 0a 0a 20 20 2f 2a 20 4c 6f  Format;..  /* Lo
12569 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 65  op through the e
1256a 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 77 69 6c  lements that wil
1256b 6c 20 6d 61 6b 65 20 75 70 20 74 68 65 20 72 65  l make up the re
1256c 63 6f 72 64 20 74 6f 20 66 69 67 75 72 65 0a 20  cord to figure. 
1256d 20 2a 2a 20 6f 75 74 20 68 6f 77 20 6d 75 63 68   ** out how much
1256e 20 73 70 61 63 65 20 69 73 20 72 65 71 75 69 72   space is requir
1256f 65 64 20 66 6f 72 20 74 68 65 20 6e 65 77 20 72  ed for the new r
12570 65 63 6f 72 64 2e 0a 20 20 2a 2f 0a 20 20 66 6f  ecord..  */.  fo
12571 72 28 70 52 65 63 3d 70 44 61 74 61 30 3b 20 70  r(pRec=pData0; p
12572 52 65 63 3c 3d 70 54 6f 73 3b 20 70 52 65 63 2b  Rec<=pTos; pRec+
12573 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6c 65 6e 3b  +){.    int len;
12574 0a 20 20 20 20 69 66 28 20 7a 41 66 66 69 6e 69  .    if( zAffini
12575 74 79 20 29 7b 0a 20 20 20 20 20 20 61 70 70 6c  ty ){.      appl
12576 79 41 66 66 69 6e 69 74 79 28 70 52 65 63 2c 20  yAffinity(pRec, 
12577 7a 41 66 66 69 6e 69 74 79 5b 70 52 65 63 2d 70  zAffinity[pRec-p
12578 44 61 74 61 30 5d 2c 20 65 6e 63 6f 64 69 6e 67  Data0], encoding
12579 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
1257a 20 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d 45 4d   pRec->flags&MEM
1257b 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 63  _Null ){.      c
1257c 6f 6e 74 61 69 6e 73 4e 75 6c 6c 20 3d 20 31 3b  ontainsNull = 1;
1257d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
1257e 52 65 63 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 5a  Rec->flags&MEM_Z
1257f 65 72 6f 20 26 26 20 70 52 65 63 2d 3e 6e 3e 30  ero && pRec->n>0
12580 20 29 7b 0a 20 20 20 20 20 20 45 78 70 61 6e 64   ){.      Expand
12581 42 6c 6f 62 28 70 52 65 63 29 3b 0a 20 20 20 20  Blob(pRec);.    
12582 7d 0a 20 20 20 20 73 65 72 69 61 6c 5f 74 79 70  }.    serial_typ
12583 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53  e = sqlite3VdbeS
12584 65 72 69 61 6c 54 79 70 65 28 70 52 65 63 2c 20  erialType(pRec, 
12585 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20  file_format);.  
12586 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 56    len = sqlite3V
12587 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e  dbeSerialTypeLen
12588 28 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20  (serial_type);. 
12589 20 20 20 6e 44 61 74 61 20 2b 3d 20 6c 65 6e 3b     nData += len;
1258a 0a 20 20 20 20 6e 48 64 72 20 2b 3d 20 73 71 6c  .    nHdr += sql
1258b 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 73 65  ite3VarintLen(se
1258c 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20  rial_type);.    
1258d 69 66 28 20 70 52 65 63 2d 3e 66 6c 61 67 73 20  if( pRec->flags 
1258e 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20  & MEM_Zero ){.  
1258f 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 70 75 72 65      /* Only pure
12590 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 42 4c 4f   zero-filled BLO
12591 42 73 20 63 61 6e 20 62 65 20 69 6e 70 75 74 20  Bs can be input 
12592 74 6f 20 74 68 69 73 20 4f 70 63 6f 64 65 2e 0a  to this Opcode..
12593 20 20 20 20 20 20 2a 2a 20 57 65 20 64 6f 20 6e        ** We do n
12594 6f 74 20 61 6c 6c 6f 77 20 62 6c 6f 62 73 20 77  ot allow blobs w
12595 69 74 68 20 61 20 70 72 65 66 69 78 20 61 6e 64  ith a prefix and
12596 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74   a zero-filled t
12597 61 69 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 6e 5a  ail. */.      nZ
12598 65 72 6f 20 2b 3d 20 70 52 65 63 2d 3e 75 2e 69  ero += pRec->u.i
12599 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
1259a 6c 65 6e 20 29 7b 0a 20 20 20 20 20 20 6e 5a 65  len ){.      nZe
1259b 72 6f 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  ro = 0;.    }.  
1259c 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 68 61  }..  /* If we ha
1259d 76 65 20 74 6f 20 61 70 70 65 6e 64 20 61 20 76  ve to append a v
1259e 61 72 69 6e 74 20 72 6f 77 69 64 20 74 6f 20 74  arint rowid to t
1259f 68 69 73 20 72 65 63 6f 72 64 2c 20 73 65 74 20  his record, set 
125a0 70 52 6f 77 69 64 0a 20 20 2a 2a 20 74 6f 20 74  pRowid.  ** to t
125a1 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
125a2 72 6f 77 69 64 20 61 6e 64 20 69 6e 63 72 65 61  rowid and increa
125a3 73 65 20 6e 42 79 74 65 20 62 79 20 74 68 65 20  se nByte by the 
125a4 61 6d 6f 75 6e 74 20 6f 66 20 73 70 61 63 65 0a  amount of space.
125a5 20 20 2a 2a 20 72 65 71 75 69 72 65 64 20 74 6f    ** required to
125a6 20 73 74 6f 72 65 20 69 74 2e 0a 20 20 2a 2f 0a   store it..  */.
125a7 20 20 69 66 28 20 61 64 64 52 6f 77 69 64 20 29    if( addRowid )
125a8 7b 0a 20 20 20 20 70 52 6f 77 69 64 20 3d 20 26  {.    pRowid = &
125a9 70 54 6f 73 5b 30 2d 6e 46 69 65 6c 64 5d 3b 0a  pTos[0-nField];.
125aa 20 20 20 20 61 73 73 65 72 74 28 20 70 52 6f 77      assert( pRow
125ab 69 64 3e 3d 70 2d 3e 61 53 74 61 63 6b 20 29 3b  id>=p->aStack );
125ac 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
125ad 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 52  MemIntegerify(pR
125ae 6f 77 69 64 29 3b 0a 20 20 20 20 73 65 72 69 61  owid);.    seria
125af 6c 5f 74 79 70 65 20 3d 20 73 71 6c 69 74 65 33  l_type = sqlite3
125b0 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 70  VdbeSerialType(p
125b1 52 6f 77 69 64 2c 20 30 29 3b 0a 20 20 20 20 6e  Rowid, 0);.    n
125b2 44 61 74 61 20 2b 3d 20 73 71 6c 69 74 65 33 56  Data += sqlite3V
125b3 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e  dbeSerialTypeLen
125b4 28 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20  (serial_type);. 
125b5 20 20 20 6e 48 64 72 20 2b 3d 20 73 71 6c 69 74     nHdr += sqlit
125b6 65 33 56 61 72 69 6e 74 4c 65 6e 28 73 65 72 69  e3VarintLen(seri
125b7 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20 6e 5a  al_type);.    nZ
125b8 65 72 6f 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20  ero = 0;.  }..  
125b9 2f 2a 20 41 64 64 20 74 68 65 20 69 6e 69 74 69  /* Add the initi
125ba 61 6c 20 68 65 61 64 65 72 20 76 61 72 69 6e 74  al header varint
125bb 20 61 6e 64 20 74 6f 74 61 6c 20 74 68 65 20 73   and total the s
125bc 69 7a 65 20 2a 2f 0a 20 20 6e 48 64 72 20 2b 3d  ize */.  nHdr +=
125bd 20 6e 56 61 72 69 6e 74 20 3d 20 73 71 6c 69 74   nVarint = sqlit
125be 65 33 56 61 72 69 6e 74 4c 65 6e 28 6e 48 64 72  e3VarintLen(nHdr
125bf 29 3b 0a 20 20 69 66 28 20 6e 56 61 72 69 6e 74  );.  if( nVarint
125c0 3c 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65  <sqlite3VarintLe
125c1 6e 28 6e 48 64 72 29 20 29 7b 0a 20 20 20 20 6e  n(nHdr) ){.    n
125c2 48 64 72 2b 2b 3b 0a 20 20 7d 0a 20 20 6e 42 79  Hdr++;.  }.  nBy
125c3 74 65 20 3d 20 6e 48 64 72 2b 6e 44 61 74 61 2d  te = nHdr+nData-
125c4 6e 5a 65 72 6f 3b 0a 20 20 69 66 28 20 6e 42 79  nZero;.  if( nBy
125c5 74 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45  te>SQLITE_MAX_LE
125c6 4e 47 54 48 20 29 7b 0a 20 20 20 20 67 6f 74 6f  NGTH ){.    goto
125c7 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 0a 20   too_big;.  }.. 
125c8 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61   /* Allocate spa
125c9 63 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20 72  ce for the new r
125ca 65 63 6f 72 64 2e 20 2a 2f 0a 20 20 69 66 28 20  ecord. */.  if( 
125cb 6e 42 79 74 65 3e 73 69 7a 65 6f 66 28 7a 54 65  nByte>sizeof(zTe
125cc 6d 70 29 20 29 7b 0a 20 20 20 20 7a 4e 65 77 52  mp) ){.    zNewR
125cd 65 63 6f 72 64 20 3d 20 73 71 6c 69 74 65 4d 61  ecord = sqliteMa
125ce 6c 6c 6f 63 52 61 77 28 6e 42 79 74 65 29 3b 0a  llocRaw(nByte);.
125cf 20 20 20 20 69 66 28 20 21 7a 4e 65 77 52 65 63      if( !zNewRec
125d0 6f 72 64 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  ord ){.      got
125d1 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a  o no_mem;.    }.
125d2 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 4e 65    }else{.    zNe
125d3 77 52 65 63 6f 72 64 20 3d 20 28 75 38 2a 29 7a  wRecord = (u8*)z
125d4 54 65 6d 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  Temp;.  }..  /* 
125d5 57 72 69 74 65 20 74 68 65 20 72 65 63 6f 72 64  Write the record
125d6 20 2a 2f 0a 20 20 69 20 3d 20 73 71 6c 69 74 65   */.  i = sqlite
125d7 33 50 75 74 56 61 72 69 6e 74 28 7a 4e 65 77 52  3PutVarint(zNewR
125d8 65 63 6f 72 64 2c 20 6e 48 64 72 29 3b 0a 20 20  ecord, nHdr);.  
125d9 66 6f 72 28 70 52 65 63 3d 70 44 61 74 61 30 3b  for(pRec=pData0;
125da 20 70 52 65 63 3c 3d 70 54 6f 73 3b 20 70 52 65   pRec<=pTos; pRe
125db 63 2b 2b 29 7b 0a 20 20 20 20 73 65 72 69 61 6c  c++){.    serial
125dc 5f 74 79 70 65 20 3d 20 73 71 6c 69 74 65 33 56  _type = sqlite3V
125dd 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 70 52  dbeSerialType(pR
125de 65 63 2c 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29  ec, file_format)
125df 3b 0a 20 20 20 20 69 20 2b 3d 20 73 71 6c 69 74  ;.    i += sqlit
125e0 65 33 50 75 74 56 61 72 69 6e 74 28 26 7a 4e 65  e3PutVarint(&zNe
125e1 77 52 65 63 6f 72 64 5b 69 5d 2c 20 73 65 72 69  wRecord[i], seri
125e2 61 6c 5f 74 79 70 65 29 3b 20 20 20 20 20 20 2f  al_type);      /
125e3 2a 20 73 65 72 69 61 6c 20 74 79 70 65 20 2a 2f  * serial type */
125e4 0a 20 20 7d 0a 20 20 69 66 28 20 61 64 64 52 6f  .  }.  if( addRo
125e5 77 69 64 20 29 7b 0a 20 20 20 20 69 20 2b 3d 20  wid ){.    i += 
125e6 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74  sqlite3PutVarint
125e7 28 26 7a 4e 65 77 52 65 63 6f 72 64 5b 69 5d 2c  (&zNewRecord[i],
125e8 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
125e9 61 6c 54 79 70 65 28 70 52 6f 77 69 64 2c 20 30  alType(pRowid, 0
125ea 29 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 70 52  ));.  }.  for(pR
125eb 65 63 3d 70 44 61 74 61 30 3b 20 70 52 65 63 3c  ec=pData0; pRec<
125ec 3d 70 54 6f 73 3b 20 70 52 65 63 2b 2b 29 7b 20  =pTos; pRec++){ 
125ed 20 2f 2a 20 73 65 72 69 61 6c 20 64 61 74 61 20   /* serial data 
125ee 2a 2f 0a 20 20 20 20 69 20 2b 3d 20 73 71 6c 69  */.    i += sqli
125ef 74 65 33 56 64 62 65 53 65 72 69 61 6c 50 75 74  te3VdbeSerialPut
125f0 28 26 7a 4e 65 77 52 65 63 6f 72 64 5b 69 5d 2c  (&zNewRecord[i],
125f1 20 6e 42 79 74 65 2d 69 2c 20 70 52 65 63 2c 20   nByte-i, pRec, 
125f2 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20  file_format);.  
125f3 7d 0a 20 20 69 66 28 20 61 64 64 52 6f 77 69 64  }.  if( addRowid
125f4 20 29 7b 0a 20 20 20 20 69 20 2b 3d 20 73 71 6c   ){.    i += sql
125f5 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 50 75  ite3VdbeSerialPu
125f6 74 28 26 7a 4e 65 77 52 65 63 6f 72 64 5b 69 5d  t(&zNewRecord[i]
125f7 2c 20 6e 42 79 74 65 2d 69 2c 20 70 52 6f 77 69  , nByte-i, pRowi
125f8 64 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 61 73 73  d, 0);.  }.  ass
125f9 65 72 74 28 20 69 3d 3d 6e 42 79 74 65 20 29 3b  ert( i==nByte );
125fa 0a 0a 20 20 2f 2a 20 50 6f 70 20 65 6e 74 72 69  ..  /* Pop entri
125fb 65 73 20 6f 66 66 20 74 68 65 20 73 74 61 63 6b  es off the stack
125fc 20 69 66 20 72 65 71 75 69 72 65 64 2e 20 50 75   if required. Pu
125fd 73 68 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72  sh the new recor
125fe 64 20 6f 6e 2e 20 2a 2f 0a 20 20 69 66 28 20 21  d on. */.  if( !
125ff 6c 65 61 76 65 4f 6e 53 74 61 63 6b 20 29 7b 0a  leaveOnStack ){.
12600 20 20 20 20 70 6f 70 53 74 61 63 6b 28 26 70 54      popStack(&pT
12601 6f 73 2c 20 6e 46 69 65 6c 64 2b 61 64 64 52 6f  os, nField+addRo
12602 77 69 64 29 3b 0a 20 20 7d 0a 20 20 70 54 6f 73  wid);.  }.  pTos
12603 2b 2b 3b 0a 20 20 70 54 6f 73 2d 3e 6e 20 3d 20  ++;.  pTos->n = 
12604 6e 42 79 74 65 3b 0a 20 20 69 66 28 20 6e 42 79  nByte;.  if( nBy
12605 74 65 3c 3d 73 69 7a 65 6f 66 28 7a 54 65 6d 70  te<=sizeof(zTemp
12606 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  ) ){.    assert(
12607 20 7a 4e 65 77 52 65 63 6f 72 64 3d 3d 28 75 6e   zNewRecord==(un
12608 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 54  signed char *)zT
12609 65 6d 70 20 29 3b 0a 20 20 20 20 70 54 6f 73 2d  emp );.    pTos-
1260a 3e 7a 20 3d 20 70 54 6f 73 2d 3e 7a 53 68 6f 72  >z = pTos->zShor
1260b 74 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 54  t;.    memcpy(pT
1260c 6f 73 2d 3e 7a 53 68 6f 72 74 2c 20 7a 54 65 6d  os->zShort, zTem
1260d 70 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 70  p, nByte);.    p
1260e 54 6f 73 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Tos->flags = MEM
1260f 5f 42 6c 6f 62 20 7c 20 4d 45 4d 5f 53 68 6f 72  _Blob | MEM_Shor
12610 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  t;.  }else{.    
12611 61 73 73 65 72 74 28 20 7a 4e 65 77 52 65 63 6f  assert( zNewReco
12612 72 64 21 3d 28 75 6e 73 69 67 6e 65 64 20 63 68  rd!=(unsigned ch
12613 61 72 20 2a 29 7a 54 65 6d 70 20 29 3b 0a 20 20  ar *)zTemp );.  
12614 20 20 70 54 6f 73 2d 3e 7a 20 3d 20 28 63 68 61    pTos->z = (cha
12615 72 2a 29 7a 4e 65 77 52 65 63 6f 72 64 3b 0a 20  r*)zNewRecord;. 
12616 20 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 3d     pTos->flags =
12617 20 4d 45 4d 5f 42 6c 6f 62 20 7c 20 4d 45 4d 5f   MEM_Blob | MEM_
12618 44 79 6e 3b 0a 20 20 20 20 70 54 6f 73 2d 3e 78  Dyn;.    pTos->x
12619 44 65 6c 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69  Del = 0;.  }.  i
1261a 66 28 20 6e 5a 65 72 6f 20 29 7b 0a 20 20 20 20  f( nZero ){.    
1261b 70 54 6f 73 2d 3e 75 2e 69 20 3d 20 6e 5a 65 72  pTos->u.i = nZer
1261c 6f 3b 0a 20 20 20 20 70 54 6f 73 2d 3e 66 6c 61  o;.    pTos->fla
1261d 67 73 20 7c 3d 20 4d 45 4d 5f 5a 65 72 6f 3b 0a  gs |= MEM_Zero;.
1261e 20 20 7d 0a 20 20 70 54 6f 73 2d 3e 65 6e 63 20    }.  pTos->enc 
1261f 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 20 20  = SQLITE_UTF8;  
12620 2f 2a 20 49 6e 20 63 61 73 65 20 74 68 65 20 62  /* In case the b
12621 6c 6f 62 20 69 73 20 65 76 65 72 20 63 6f 6e 76  lob is ever conv
12622 65 72 74 65 64 20 74 6f 20 74 65 78 74 20 2a 2f  erted to text */
12623 0a 0a 20 20 2f 2a 20 49 66 20 61 20 4e 55 4c 4c  ..  /* If a NULL
12624 20 77 61 73 20 65 6e 63 6f 75 6e 74 65 72 65 64   was encountered
12625 20 61 6e 64 20 6a 75 6d 70 49 66 4e 75 6c 6c 20   and jumpIfNull 
12626 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 61 6b  is non-zero, tak
12627 65 20 74 68 65 20 6a 75 6d 70 2e 20 2a 2f 0a 20  e the jump. */. 
12628 20 69 66 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 20   if( jumpIfNull 
12629 26 26 20 63 6f 6e 74 61 69 6e 73 4e 75 6c 6c 20  && containsNull 
1262a 29 7b 0a 20 20 20 20 70 63 20 3d 20 6a 75 6d 70  ){.    pc = jump
1262b 49 66 4e 75 6c 6c 20 2d 20 31 3b 0a 20 20 7d 0a  IfNull - 1;.  }.
1262c 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1262d 70 63 6f 64 65 3a 20 53 74 61 74 65 6d 65 6e 74  pcode: Statement
1262e 20 50 31 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 42 65   P1 * *.**.** Be
1262f 67 69 6e 20 61 6e 20 69 6e 64 69 76 69 64 75 61  gin an individua
12630 6c 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e  l statement tran
12631 73 61 63 74 69 6f 6e 20 77 68 69 63 68 20 69 73  saction which is
12632 20 70 61 72 74 20 6f 66 20 61 20 6c 61 72 67 65   part of a large
12633 72 0a 2a 2a 20 42 45 47 49 4e 2e 2e 43 4f 4d 4d  r.** BEGIN..COMM
12634 49 54 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  IT transaction. 
12635 20 54 68 69 73 20 69 73 20 6e 65 65 64 65 64 20   This is needed 
12636 73 6f 20 74 68 61 74 20 74 68 65 20 73 74 61 74  so that the stat
12637 65 6d 65 6e 74 0a 2a 2a 20 63 61 6e 20 62 65 20  ement.** can be 
12638 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 66 74 65  rolled back afte
12639 72 20 61 6e 20 65 72 72 6f 72 20 77 69 74 68 6f  r an error witho
1263a 75 74 20 68 61 76 69 6e 67 20 74 6f 20 72 6f 6c  ut having to rol
1263b 6c 20 62 61 63 6b 20 74 68 65 0a 2a 2a 20 65 6e  l back the.** en
1263c 74 69 72 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  tire transaction
1263d 2e 20 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74  .  The statement
1263e 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 69 6c   transaction wil
1263f 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a  l automatically.
12640 2a 2a 20 63 6f 6d 6d 69 74 20 77 68 65 6e 20 74  ** commit when t
12641 68 65 20 56 44 42 45 20 68 61 6c 74 73 2e 0a 2a  he VDBE halts..*
12642 2a 0a 2a 2a 20 54 68 65 20 73 74 61 74 65 6d 65  *.** The stateme
12643 6e 74 20 69 73 20 62 65 67 75 6e 20 6f 6e 20 74  nt is begun on t
12644 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
12645 20 77 69 74 68 20 69 6e 64 65 78 20 50 31 2e 20   with index P1. 
12646 20 54 68 65 20 6d 61 69 6e 0a 2a 2a 20 64 61 74   The main.** dat
12647 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 20 61  abase file has a
12648 6e 20 69 6e 64 65 78 20 6f 66 20 30 20 61 6e 64  n index of 0 and
12649 20 74 68 65 20 66 69 6c 65 20 75 73 65 64 20 66   the file used f
1264a 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  or temporary tab
1264b 6c 65 73 0a 2a 2a 20 68 61 73 20 61 6e 20 69 6e  les.** has an in
1264c 64 65 78 20 6f 66 20 31 2e 0a 2a 2f 0a 63 61 73  dex of 1..*/.cas
1264d 65 20 4f 50 5f 53 74 61 74 65 6d 65 6e 74 3a 20  e OP_Statement: 
1264e 7b 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75  {       /* no-pu
1264f 73 68 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20  sh */.  int i = 
12650 70 4f 70 2d 3e 70 31 3b 0a 20 20 42 74 72 65 65  pOp->p1;.  Btree
12651 20 2a 70 42 74 3b 0a 20 20 69 66 28 20 69 3e 3d   *pBt;.  if( i>=
12652 30 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 20 26  0 && i<db->nDb &
12653 26 20 28 70 42 74 20 3d 20 64 62 2d 3e 61 44 62  & (pBt = db->aDb
12654 5b 69 5d 2e 70 42 74 29 21 3d 30 20 26 26 20 21  [i].pBt)!=0 && !
12655 28 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 29  (db->autoCommit)
12656 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
12657 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e  sqlite3BtreeIsIn
12658 54 72 61 6e 73 28 70 42 74 29 20 29 3b 0a 20 20  Trans(pBt) );.  
12659 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 42 74    if( !sqlite3Bt
1265a 72 65 65 49 73 49 6e 53 74 6d 74 28 70 42 74 29  reeIsInStmt(pBt)
1265b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   ){.      rc = s
1265c 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e  qlite3BtreeBegin
1265d 53 74 6d 74 28 70 42 74 29 3b 0a 20 20 20 20 20  Stmt(pBt);.     
1265e 20 70 2d 3e 6f 70 65 6e 65 64 53 74 61 74 65 6d   p->openedStatem
1265f 65 6e 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  ent = 1;.    }. 
12660 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
12661 2a 20 4f 70 63 6f 64 65 3a 20 41 75 74 6f 43 6f  * Opcode: AutoCo
12662 6d 6d 69 74 20 50 31 20 50 32 20 2a 0a 2a 2a 0a  mmit P1 P2 *.**.
12663 2a 2a 20 53 65 74 20 74 68 65 20 64 61 74 61 62  ** Set the datab
12664 61 73 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20  ase auto-commit 
12665 66 6c 61 67 20 74 6f 20 50 31 20 28 31 20 6f 72  flag to P1 (1 or
12666 20 30 29 2e 20 49 66 20 50 32 20 69 73 20 74 72   0). If P2 is tr
12667 75 65 2c 20 72 6f 6c 6c 0a 2a 2a 20 62 61 63 6b  ue, roll.** back
12668 20 61 6e 79 20 63 75 72 72 65 6e 74 6c 79 20 61   any currently a
12669 63 74 69 76 65 20 62 74 72 65 65 20 74 72 61 6e  ctive btree tran
1266a 73 61 63 74 69 6f 6e 73 2e 20 49 66 20 74 68 65  sactions. If the
1266b 72 65 20 61 72 65 20 61 6e 79 20 61 63 74 69 76  re are any activ
1266c 65 0a 2a 2a 20 56 4d 73 20 28 61 70 61 72 74 20  e.** VMs (apart 
1266d 66 72 6f 6d 20 74 68 69 73 20 6f 6e 65 29 2c 20  from this one), 
1266e 74 68 65 6e 20 74 68 65 20 43 4f 4d 4d 49 54 20  then the COMMIT 
1266f 6f 72 20 52 4f 4c 4c 42 41 43 4b 20 73 74 61 74  or ROLLBACK stat
12670 65 6d 65 6e 74 20 66 61 69 6c 73 2e 0a 2a 2a 0a  ement fails..**.
12671 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74  ** This instruct
12672 69 6f 6e 20 63 61 75 73 65 73 20 74 68 65 20 56  ion causes the V
12673 4d 20 74 6f 20 68 61 6c 74 2e 0a 2a 2f 0a 63 61  M to halt..*/.ca
12674 73 65 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74  se OP_AutoCommit
12675 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d  : {       /* no-
12676 70 75 73 68 20 2a 2f 0a 20 20 75 38 20 69 20 3d  push */.  u8 i =
12677 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 38 20 72   pOp->p1;.  u8 r
12678 6f 6c 6c 62 61 63 6b 20 3d 20 70 4f 70 2d 3e 70  ollback = pOp->p
12679 32 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3d  2;..  assert( i=
1267a 3d 31 20 7c 7c 20 69 3d 3d 30 20 29 3b 0a 20 20  =1 || i==0 );.  
1267b 61 73 73 65 72 74 28 20 69 3d 3d 31 20 7c 7c 20  assert( i==1 || 
1267c 72 6f 6c 6c 62 61 63 6b 3d 3d 30 20 29 3b 0a 0a  rollback==0 );..
1267d 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 63    assert( db->ac
1267e 74 69 76 65 56 64 62 65 43 6e 74 3e 30 20 29 3b  tiveVdbeCnt>0 );
1267f 20 20 2f 2a 20 41 74 20 6c 65 61 73 74 20 74 68    /* At least th
12680 69 73 20 6f 6e 65 20 56 4d 20 69 73 20 61 63 74  is one VM is act
12681 69 76 65 20 2a 2f 0a 0a 20 20 69 66 28 20 64 62  ive */..  if( db
12682 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e  ->activeVdbeCnt>
12683 31 20 26 26 20 69 20 26 26 20 21 64 62 2d 3e 61  1 && i && !db->a
12684 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20  utoCommit ){.   
12685 20 2f 2a 20 49 66 20 74 68 69 73 20 69 6e 73 74   /* If this inst
12686 72 75 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e  ruction implemen
12687 74 73 20 61 20 43 4f 4d 4d 49 54 20 6f 72 20 52  ts a COMMIT or R
12688 4f 4c 4c 42 41 43 4b 2c 20 6f 74 68 65 72 20 56  OLLBACK, other V
12689 4d 73 20 61 72 65 0a 20 20 20 20 2a 2a 20 73 74  Ms are.    ** st
1268a 69 6c 6c 20 72 75 6e 6e 69 6e 67 2c 20 61 6e 64  ill running, and
1268b 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69   a transaction i
1268c 73 20 61 63 74 69 76 65 2c 20 72 65 74 75 72 6e  s active, return
1268d 20 61 6e 20 65 72 72 6f 72 20 69 6e 64 69 63 61   an error indica
1268e 74 69 6e 67 0a 20 20 20 20 2a 2a 20 74 68 61 74  ting.    ** that
1268f 20 74 68 65 20 6f 74 68 65 72 20 56 4d 73 20 6d   the other VMs m
12690 75 73 74 20 63 6f 6d 70 6c 65 74 65 20 66 69 72  ust complete fir
12691 73 74 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20  st. .    */.    
12692 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67  sqlite3SetString
12693 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 22 63  (&p->zErrMsg, "c
12694 61 6e 6e 6f 74 20 22 2c 20 72 6f 6c 6c 62 61 63  annot ", rollbac
12695 6b 3f 22 72 6f 6c 6c 62 61 63 6b 22 3a 22 63 6f  k?"rollback":"co
12696 6d 6d 69 74 22 2c 20 0a 20 20 20 20 20 20 20 20  mmit", .        
12697 22 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2d 20  " transaction - 
12698 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 69  SQL statements i
12699 6e 20 70 72 6f 67 72 65 73 73 22 2c 20 28 63 68  n progress", (ch
1269a 61 72 2a 29 30 29 3b 0a 20 20 20 20 72 63 20 3d  ar*)0);.    rc =
1269b 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
1269c 20 7d 65 6c 73 65 20 69 66 28 20 69 21 3d 64 62   }else if( i!=db
1269d 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a  ->autoCommit ){.
1269e 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 20      if( pOp->p2 
1269f 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
126a0 20 69 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 73   i==1 );.      s
126a1 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c  qlite3RollbackAl
126a2 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 64 62 2d  l(db);.      db-
126a3 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b  >autoCommit = 1;
126a4 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
126a5 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74    db->autoCommit
126a6 20 3d 20 69 3b 0a 20 20 20 20 20 20 69 66 28 20   = i;.      if( 
126a7 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28  sqlite3VdbeHalt(
126a8 70 29 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20  p)==SQLITE_BUSY 
126a9 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 54  ){.        p->pT
126aa 6f 73 20 3d 20 70 54 6f 73 3b 0a 20 20 20 20 20  os = pTos;.     
126ab 20 20 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a 20     p->pc = pc;. 
126ac 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43         db->autoC
126ad 6f 6d 6d 69 74 20 3d 20 31 2d 69 3b 0a 20 20 20  ommit = 1-i;.   
126ae 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c       p->rc = SQL
126af 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20  ITE_BUSY;.      
126b0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
126b1 42 55 53 59 3b 0a 20 20 20 20 20 20 7d 0a 20 20  BUSY;.      }.  
126b2 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 72    }.    if( p->r
126b3 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
126b4 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
126b5 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 65  ITE_DONE;.    }e
126b6 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72  lse{.      retur
126b7 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
126b8 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
126b9 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72     sqlite3SetStr
126ba 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c  ing(&p->zErrMsg,
126bb 0a 20 20 20 20 20 20 20 20 28 21 69 29 3f 22 63  .        (!i)?"c
126bc 61 6e 6e 6f 74 20 73 74 61 72 74 20 61 20 74 72  annot start a tr
126bd 61 6e 73 61 63 74 69 6f 6e 20 77 69 74 68 69 6e  ansaction within
126be 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 22 3a   a transaction":
126bf 28 0a 20 20 20 20 20 20 20 20 28 72 6f 6c 6c 62  (.        (rollb
126c0 61 63 6b 29 3f 22 63 61 6e 6e 6f 74 20 72 6f 6c  ack)?"cannot rol
126c1 6c 62 61 63 6b 20 2d 20 6e 6f 20 74 72 61 6e 73  lback - no trans
126c2 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65  action is active
126c3 22 3a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ":.             
126c4 20 20 20 20 20 20 22 63 61 6e 6e 6f 74 20 63 6f        "cannot co
126c5 6d 6d 69 74 20 2d 20 6e 6f 20 74 72 61 6e 73 61  mmit - no transa
126c6 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 22  ction is active"
126c7 29 2c 20 28 63 68 61 72 2a 29 30 29 3b 0a 20 20  ), (char*)0);.  
126c8 20 20 20 20 20 20 20 0a 20 20 20 20 72 63 20 3d         .    rc =
126c9 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
126ca 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
126cb 2a 20 4f 70 63 6f 64 65 3a 20 54 72 61 6e 73 61  * Opcode: Transa
126cc 63 74 69 6f 6e 20 50 31 20 50 32 20 2a 0a 2a 2a  ction P1 P2 *.**
126cd 0a 2a 2a 20 42 65 67 69 6e 20 61 20 74 72 61 6e  .** Begin a tran
126ce 73 61 63 74 69 6f 6e 2e 20 20 54 68 65 20 74 72  saction.  The tr
126cf 61 6e 73 61 63 74 69 6f 6e 20 65 6e 64 73 20 77  ansaction ends w
126d0 68 65 6e 20 61 20 43 6f 6d 6d 69 74 20 6f 72 20  hen a Commit or 
126d1 52 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6f 70 63 6f  Rollback.** opco
126d2 64 65 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65  de is encountere
126d3 64 2e 20 20 44 65 70 65 6e 64 69 6e 67 20 6f 6e  d.  Depending on
126d4 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54   the ON CONFLICT
126d5 20 73 65 74 74 69 6e 67 2c 20 74 68 65 0a 2a 2a   setting, the.**
126d6 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 69 67   transaction mig
126d7 68 74 20 61 6c 73 6f 20 62 65 20 72 6f 6c 6c 65  ht also be rolle
126d8 64 20 62 61 63 6b 20 69 66 20 61 6e 20 65 72 72  d back if an err
126d9 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65  or is encountere
126da 64 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 74  d..**.** P1 is t
126db 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
126dc 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f 6e  database file on
126dd 20 77 68 69 63 68 20 74 68 65 20 74 72 61 6e 73   which the trans
126de 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 73 74 61  action is.** sta
126df 72 74 65 64 2e 20 20 49 6e 64 65 78 20 30 20 69  rted.  Index 0 i
126e0 73 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  s the main datab
126e1 61 73 65 20 66 69 6c 65 20 61 6e 64 20 69 6e 64  ase file and ind
126e2 65 78 20 31 20 69 73 20 74 68 65 0a 2a 2a 20 66  ex 1 is the.** f
126e3 69 6c 65 20 75 73 65 64 20 66 6f 72 20 74 65 6d  ile used for tem
126e4 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 0a 2a  porary tables..*
126e5 2a 0a 2a 2a 20 49 66 20 50 32 20 69 73 20 6e 6f  *.** If P2 is no
126e6 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 20 77  n-zero, then a w
126e7 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e  rite-transaction
126e8 20 69 73 20 73 74 61 72 74 65 64 2e 20 20 41 20   is started.  A 
126e9 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 73  RESERVED lock is
126ea 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20  .** obtained on 
126eb 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
126ec 65 20 77 68 65 6e 20 61 20 77 72 69 74 65 2d 74  e when a write-t
126ed 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74  ransaction is st
126ee 61 72 74 65 64 2e 20 20 4e 6f 0a 2a 2a 20 6f 74  arted.  No.** ot
126ef 68 65 72 20 70 72 6f 63 65 73 73 20 63 61 6e 20  her process can 
126f0 73 74 61 72 74 20 61 6e 6f 74 68 65 72 20 77 72  start another wr
126f1 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ite transaction 
126f2 77 68 69 6c 65 20 74 68 69 73 20 74 72 61 6e 73  while this trans
126f3 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 75 6e 64  action is.** und
126f4 65 72 77 61 79 2e 20 20 53 74 61 72 74 69 6e 67  erway.  Starting
126f5 20 61 20 77 72 69 74 65 20 74 72 61 6e 73 61 63   a write transac
126f6 74 69 6f 6e 20 61 6c 73 6f 20 63 72 65 61 74 65  tion also create
126f7 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  s a rollback jou
126f8 72 6e 61 6c 2e 20 41 0a 2a 2a 20 77 72 69 74 65  rnal. A.** write
126f9 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73   transaction mus
126fa 74 20 62 65 20 73 74 61 72 74 65 64 20 62 65 66  t be started bef
126fb 6f 72 65 20 61 6e 79 20 63 68 61 6e 67 65 73 20  ore any changes 
126fc 63 61 6e 20 62 65 20 6d 61 64 65 20 74 6f 20 74  can be made to t
126fd 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20  he.** database. 
126fe 20 49 66 20 50 32 20 69 73 20 32 20 6f 72 20 67   If P2 is 2 or g
126ff 72 65 61 74 65 72 20 74 68 65 6e 20 61 6e 20 45  reater then an E
12700 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 69 73  XCLUSIVE lock is
12701 20 61 6c 73 6f 20 6f 62 74 61 69 6e 65 64 0a 2a   also obtained.*
12702 2a 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e 0a 2a  * on the file..*
12703 2a 0a 2a 2a 20 49 66 20 50 32 20 69 73 20 7a 65  *.** If P2 is ze
12704 72 6f 2c 20 74 68 65 6e 20 61 20 72 65 61 64 2d  ro, then a read-
12705 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64  lock is obtained
12706 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
12707 20 66 69 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f   file..*/.case O
12708 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 3a 20 7b  P_Transaction: {
12709 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73         /* no-pus
1270a 68 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70  h */.  int i = p
1270b 4f 70 2d 3e 70 31 3b 0a 20 20 42 74 72 65 65 20  Op->p1;.  Btree 
1270c 2a 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 74 28  *pBt;..  assert(
1270d 20 69 3e 3d 30 20 26 26 20 69 3c 64 62 2d 3e 6e   i>=0 && i<db->n
1270e 44 62 20 29 3b 0a 20 20 70 42 74 20 3d 20 64 62  Db );.  pBt = db
1270f 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 0a 20  ->aDb[i].pBt;.. 
12710 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20   if( pBt ){.    
12711 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
12712 65 42 65 67 69 6e 54 72 61 6e 73 28 70 42 74 2c  eBeginTrans(pBt,
12713 20 70 4f 70 2d 3e 70 32 29 3b 0a 20 20 20 20 69   pOp->p2);.    i
12714 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55  f( rc==SQLITE_BU
12715 53 59 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70  SY ){.      p->p
12716 63 20 3d 20 70 63 3b 0a 20 20 20 20 20 20 70 2d  c = pc;.      p-
12717 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53  >rc = SQLITE_BUS
12718 59 3b 0a 20 20 20 20 20 20 70 2d 3e 70 54 6f 73  Y;.      p->pTos
12719 20 3d 20 70 54 6f 73 3b 0a 20 20 20 20 20 20 72   = pTos;.      r
1271a 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53  eturn SQLITE_BUS
1271b 59 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  Y;.    }.    if(
1271c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc!=SQLITE_OK &
1271d 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 45 41  & rc!=SQLITE_REA
1271e 44 4f 4e 4c 59 20 2f 2a 20 26 26 20 72 63 21 3d  DONLY /* && rc!=
1271f 53 51 4c 49 54 45 5f 42 55 53 59 20 2a 2f 20 29  SQLITE_BUSY */ )
12720 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f  {.      goto abo
12721 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b  rt_due_to_error;
12722 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65  .    }.  }.  bre
12723 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
12724 3a 20 52 65 61 64 43 6f 6f 6b 69 65 20 50 31 20  : ReadCookie P1 
12725 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20  P2 *.**.** Read 
12726 63 6f 6f 6b 69 65 20 6e 75 6d 62 65 72 20 50 32  cookie number P2
12727 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65 20 50   from database P
12728 31 20 61 6e 64 20 70 75 73 68 20 69 74 20 6f 6e  1 and push it on
12729 74 6f 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2a  to the stack..**
1272a 20 50 32 3d 3d 30 20 69 73 20 74 68 65 20 73 63   P2==0 is the sc
1272b 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20 20 50  hema version.  P
1272c 32 3d 3d 31 20 69 73 20 74 68 65 20 64 61 74 61  2==1 is the data
1272d 62 61 73 65 20 66 6f 72 6d 61 74 2e 0a 2a 2a 20  base format..** 
1272e 50 32 3d 3d 32 20 69 73 20 74 68 65 20 72 65 63  P2==2 is the rec
1272f 6f 6d 6d 65 6e 64 65 64 20 70 61 67 65 72 20 63  ommended pager c
12730 61 63 68 65 20 73 69 7a 65 2c 20 61 6e 64 20 73  ache size, and s
12731 6f 20 66 6f 72 74 68 2e 20 20 50 31 3d 3d 30 20  o forth.  P1==0 
12732 69 73 0a 2a 2a 20 74 68 65 20 6d 61 69 6e 20 64  is.** the main d
12733 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64  atabase file and
12734 20 50 31 3d 3d 31 20 69 73 20 74 68 65 20 64 61   P1==1 is the da
12735 74 61 62 61 73 65 20 66 69 6c 65 20 75 73 65 64  tabase file used
12736 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74 65 6d   to store.** tem
12737 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 0a 2a  porary tables..*
12738 2a 0a 2a 2a 20 49 66 20 50 31 20 69 73 20 6e 65  *.** If P1 is ne
12739 67 61 74 69 76 65 2c 20 74 68 65 6e 20 74 68 69  gative, then thi
1273a 73 20 69 73 20 61 20 72 65 71 75 65 73 74 20 74  s is a request t
1273b 6f 20 72 65 61 64 20 74 68 65 20 73 69 7a 65 20  o read the size 
1273c 6f 66 20 61 0a 2a 2a 20 64 61 74 61 62 61 73 65  of a.** database
1273d 73 20 66 72 65 65 2d 6c 69 73 74 2e 20 50 32 20  s free-list. P2 
1273e 6d 75 73 74 20 62 65 20 73 65 74 20 74 6f 20 31  must be set to 1
1273f 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 20 54   in this case. T
12740 68 65 20 61 63 74 75 61 6c 0a 2a 2a 20 64 61 74  he actual.** dat
12741 61 62 61 73 65 20 61 63 63 65 73 73 65 64 20 69  abase accessed i
12742 73 20 28 28 50 31 2b 31 29 2a 2d 31 29 2e 20 46  s ((P1+1)*-1). F
12743 6f 72 20 65 78 61 6d 70 6c 65 2c 20 61 20 50 31  or example, a P1
12744 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 2d 31   parameter of -1
12745 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 73 20  .** corresponds 
12746 74 6f 20 64 61 74 61 62 61 73 65 20 30 20 28 22  to database 0 ("
12747 6d 61 69 6e 22 29 2c 20 61 20 50 31 20 6f 66 20  main"), a P1 of 
12748 2d 32 20 69 73 20 64 61 74 61 62 61 73 65 20 31  -2 is database 1
12749 20 28 22 74 65 6d 70 22 29 2e 0a 2a 2a 0a 2a 2a   ("temp")..**.**
1274a 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 20 61   There must be a
1274b 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68   read-lock on th
1274c 65 20 64 61 74 61 62 61 73 65 20 28 65 69 74 68  e database (eith
1274d 65 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  er a transaction
1274e 0a 2a 2a 20 6d 75 73 74 20 62 65 20 73 74 61 72  .** must be star
1274f 74 65 64 20 6f 72 20 74 68 65 72 65 20 6d 75 73  ted or there mus
12750 74 20 62 65 20 61 6e 20 6f 70 65 6e 20 63 75 72  t be an open cur
12751 73 6f 72 29 20 62 65 66 6f 72 65 0a 2a 2a 20 65  sor) before.** e
12752 78 65 63 75 74 69 6e 67 20 74 68 69 73 20 69 6e  xecuting this in
12753 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61  struction..*/.ca
12754 73 65 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65  se OP_ReadCookie
12755 3a 20 7b 0a 20 20 69 6e 74 20 69 4d 65 74 61 3b  : {.  int iMeta;
12756 0a 20 20 69 6e 74 20 69 44 62 20 3d 20 70 4f 70  .  int iDb = pOp
12757 2d 3e 70 31 3b 0a 20 20 69 6e 74 20 69 43 6f 6f  ->p1;.  int iCoo
12758 6b 69 65 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 0a  kie = pOp->p2;..
12759 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1275a 32 3c 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45 45  2<SQLITE_N_BTREE
1275b 5f 4d 45 54 41 20 29 3b 0a 20 20 69 66 28 20 69  _META );.  if( i
1275c 44 62 3c 30 20 29 7b 0a 20 20 20 20 69 44 62 20  Db<0 ){.    iDb 
1275d 3d 20 28 2d 31 2a 28 69 44 62 2b 31 29 29 3b 0a  = (-1*(iDb+1));.
1275e 20 20 20 20 69 43 6f 6f 6b 69 65 20 2a 3d 20 2d      iCookie *= -
1275f 31 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  1;.  }.  assert(
12760 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64   iDb>=0 && iDb<d
12761 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65  b->nDb );.  asse
12762 72 74 28 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  rt( db->aDb[iDb]
12763 2e 70 42 74 21 3d 30 20 29 3b 0a 20 20 2f 2a 20  .pBt!=0 );.  /* 
12764 54 68 65 20 69 6e 64 65 78 69 6e 67 20 6f 66 20  The indexing of 
12765 6d 65 74 61 20 76 61 6c 75 65 73 20 61 74 20 74  meta values at t
12766 68 65 20 73 63 68 65 6d 61 20 6c 61 79 65 72 20  he schema layer 
12767 69 73 20 6f 66 66 20 62 79 20 6f 6e 65 20 66 72  is off by one fr
12768 6f 6d 0a 20 20 2a 2a 20 74 68 65 20 69 6e 64 65  om.  ** the inde
12769 78 69 6e 67 20 69 6e 20 74 68 65 20 62 74 72 65  xing in the btre
1276a 65 20 6c 61 79 65 72 2e 20 20 54 68 65 20 62 74  e layer.  The bt
1276b 72 65 65 20 63 6f 6e 73 69 64 65 72 73 20 6d 65  ree considers me
1276c 74 61 5b 30 5d 20 74 6f 0a 20 20 2a 2a 20 62 65  ta[0] to.  ** be
1276d 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66   the number of f
1276e 72 65 65 20 70 61 67 65 73 20 69 6e 20 74 68 65  ree pages in the
1276f 20 64 61 74 61 62 61 73 65 20 28 61 20 72 65 61   database (a rea
12770 64 2d 6f 6e 6c 79 20 76 61 6c 75 65 29 0a 20 20  d-only value).  
12771 2a 2a 20 61 6e 64 20 6d 65 74 61 5b 31 5d 20 74  ** and meta[1] t
12772 6f 20 62 65 20 74 68 65 20 73 63 68 65 6d 61 20  o be the schema 
12773 63 6f 6f 6b 69 65 2e 20 20 54 68 65 20 73 63 68  cookie.  The sch
12774 65 6d 61 20 6c 61 79 65 72 20 63 6f 6e 73 69 64  ema layer consid
12775 65 72 73 0a 20 20 2a 2a 20 6d 65 74 61 5b 31 5d  ers.  ** meta[1]
12776 20 74 6f 20 62 65 20 74 68 65 20 73 63 68 65 6d   to be the schem
12777 61 20 63 6f 6f 6b 69 65 2e 20 20 53 6f 20 77 65  a cookie.  So we
12778 20 68 61 76 65 20 74 6f 20 73 68 69 66 74 20 74   have to shift t
12779 68 65 20 69 6e 64 65 78 0a 20 20 2a 2a 20 62 79  he index.  ** by
1277a 20 6f 6e 65 20 69 6e 20 74 68 65 20 66 6f 6c 6c   one in the foll
1277b 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 2e  owing statement.
1277c 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c  .  */.  rc = sql
1277d 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61  ite3BtreeGetMeta
1277e 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42  (db->aDb[iDb].pB
1277f 74 2c 20 31 20 2b 20 69 43 6f 6f 6b 69 65 2c 20  t, 1 + iCookie, 
12780 28 75 33 32 20 2a 29 26 69 4d 65 74 61 29 3b 0a  (u32 *)&iMeta);.
12781 20 20 70 54 6f 73 2b 2b 3b 0a 20 20 70 54 6f 73    pTos++;.  pTos
12782 2d 3e 75 2e 69 20 3d 20 69 4d 65 74 61 3b 0a 20  ->u.i = iMeta;. 
12783 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 3d 20 4d   pTos->flags = M
12784 45 4d 5f 49 6e 74 3b 0a 20 20 62 72 65 61 6b 3b  EM_Int;.  break;
12785 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53  .}../* Opcode: S
12786 65 74 43 6f 6f 6b 69 65 20 50 31 20 50 32 20 2a  etCookie P1 P2 *
12787 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65  .**.** Write the
12788 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63   top of the stac
12789 6b 20 69 6e 74 6f 20 63 6f 6f 6b 69 65 20 6e 75  k into cookie nu
1278a 6d 62 65 72 20 50 32 20 6f 66 20 64 61 74 61 62  mber P2 of datab
1278b 61 73 65 20 50 31 2e 0a 2a 2a 20 50 32 3d 3d 30  ase P1..** P2==0
1278c 20 69 73 20 74 68 65 20 73 63 68 65 6d 61 20 76   is the schema v
1278d 65 72 73 69 6f 6e 2e 20 20 50 32 3d 3d 31 20 69  ersion.  P2==1 i
1278e 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  s the database f
1278f 6f 72 6d 61 74 2e 0a 2a 2a 20 50 32 3d 3d 32 20  ormat..** P2==2 
12790 69 73 20 74 68 65 20 72 65 63 6f 6d 6d 65 6e 64  is the recommend
12791 65 64 20 70 61 67 65 72 20 63 61 63 68 65 20 73  ed pager cache s
12792 69 7a 65 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74  ize, and so fort
12793 68 2e 20 20 50 31 3d 3d 30 20 69 73 0a 2a 2a 20  h.  P1==0 is.** 
12794 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
12795 65 20 66 69 6c 65 20 61 6e 64 20 50 31 3d 3d 31  e file and P1==1
12796 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65   is the database
12797 20 66 69 6c 65 20 75 73 65 64 20 74 6f 20 73 74   file used to st
12798 6f 72 65 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79  ore.** temporary
12799 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 41   tables..**.** A
1279a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73   transaction mus
1279b 74 20 62 65 20 73 74 61 72 74 65 64 20 62 65 66  t be started bef
1279c 6f 72 65 20 65 78 65 63 75 74 69 6e 67 20 74 68  ore executing th
1279d 69 73 20 6f 70 63 6f 64 65 2e 0a 2a 2f 0a 63 61  is opcode..*/.ca
1279e 73 65 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 3a  se OP_SetCookie:
1279f 20 7b 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70   {       /* no-p
127a0 75 73 68 20 2a 2f 0a 20 20 44 62 20 2a 70 44 62  ush */.  Db *pDb
127a1 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
127a2 3e 70 32 3c 53 51 4c 49 54 45 5f 4e 5f 42 54 52  >p2<SQLITE_N_BTR
127a3 45 45 5f 4d 45 54 41 20 29 3b 0a 20 20 61 73 73  EE_META );.  ass
127a4 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20  ert( pOp->p1>=0 
127a5 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e  && pOp->p1<db->n
127a6 44 62 20 29 3b 0a 20 20 70 44 62 20 3d 20 26 64  Db );.  pDb = &d
127a7 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 3b  b->aDb[pOp->p1];
127a8 0a 20 20 61 73 73 65 72 74 28 20 70 44 62 2d 3e  .  assert( pDb->
127a9 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  pBt!=0 );.  asse
127aa 72 74 28 20 70 54 6f 73 3e 3d 70 2d 3e 61 53 74  rt( pTos>=p->aSt
127ab 61 63 6b 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  ack );.  sqlite3
127ac 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66  VdbeMemIntegerif
127ad 79 28 70 54 6f 73 29 3b 0a 20 20 2f 2a 20 53 65  y(pTos);.  /* Se
127ae 65 20 6e 6f 74 65 20 61 62 6f 75 74 20 69 6e 64  e note about ind
127af 65 78 20 73 68 69 66 74 69 6e 67 20 6f 6e 20 4f  ex shifting on O
127b0 50 5f 52 65 61 64 43 6f 6f 6b 69 65 20 2a 2f 0a  P_ReadCookie */.
127b1 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
127b2 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 70 44  reeUpdateMeta(pD
127b3 62 2d 3e 70 42 74 2c 20 31 2b 70 4f 70 2d 3e 70  b->pBt, 1+pOp->p
127b4 32 2c 20 28 69 6e 74 29 70 54 6f 73 2d 3e 75 2e  2, (int)pTos->u.
127b5 69 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70  i);.  if( pOp->p
127b6 32 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 57  2==0 ){.    /* W
127b7 68 65 6e 20 74 68 65 20 73 63 68 65 6d 61 20 63  hen the schema c
127b8 6f 6f 6b 69 65 20 63 68 61 6e 67 65 73 2c 20 72  ookie changes, r
127b9 65 63 6f 72 64 20 74 68 65 20 6e 65 77 20 63 6f  ecord the new co
127ba 6f 6b 69 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20  okie internally 
127bb 2a 2f 0a 20 20 20 20 70 44 62 2d 3e 70 53 63 68  */.    pDb->pSch
127bc 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b  ema->schema_cook
127bd 69 65 20 3d 20 70 54 6f 73 2d 3e 75 2e 69 3b 0a  ie = pTos->u.i;.
127be 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d      db->flags |=
127bf 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68   SQLITE_InternCh
127c0 61 6e 67 65 73 3b 0a 20 20 7d 65 6c 73 65 20 69  anges;.  }else i
127c1 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 31 20 29 7b  f( pOp->p2==1 ){
127c2 0a 20 20 20 20 2f 2a 20 52 65 63 6f 72 64 20 63  .    /* Record c
127c3 68 61 6e 67 65 73 20 69 6e 20 74 68 65 20 66 69  hanges in the fi
127c4 6c 65 20 66 6f 72 6d 61 74 20 2a 2f 0a 20 20 20  le format */.   
127c5 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66   pDb->pSchema->f
127c6 69 6c 65 5f 66 6f 72 6d 61 74 20 3d 20 70 54 6f  ile_format = pTo
127c7 73 2d 3e 75 2e 69 3b 0a 20 20 7d 0a 20 20 61 73  s->u.i;.  }.  as
127c8 73 65 72 74 28 20 28 70 54 6f 73 2d 3e 66 6c 61  sert( (pTos->fla
127c9 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 3d 3d 30  gs & MEM_Dyn)==0
127ca 20 29 3b 0a 20 20 70 54 6f 73 2d 2d 3b 0a 20 20   );.  pTos--;.  
127cb 69 66 28 20 70 4f 70 2d 3e 70 31 3d 3d 31 20 29  if( pOp->p1==1 )
127cc 7b 0a 20 20 20 20 2f 2a 20 49 6e 76 61 6c 69 64  {.    /* Invalid
127cd 61 74 65 20 61 6c 6c 20 70 72 65 70 61 72 65 64  ate all prepared
127ce 20 73 74 61 74 65 6d 65 6e 74 73 20 77 68 65 6e   statements when
127cf 65 76 65 72 20 74 68 65 20 54 45 4d 50 20 64 61  ever the TEMP da
127d0 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 73 63  tabase.    ** sc
127d1 68 65 6d 61 20 69 73 20 63 68 61 6e 67 65 64 2e  hema is changed.
127d2 20 20 54 69 63 6b 65 74 20 23 31 36 34 34 20 2a    Ticket #1644 *
127d3 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  /.    sqlite3Exp
127d4 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74 65  irePreparedState
127d5 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 7d 0a 20  ments(db);.  }. 
127d6 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
127d7 63 6f 64 65 3a 20 56 65 72 69 66 79 43 6f 6f 6b  code: VerifyCook
127d8 69 65 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a  ie P1 P2 *.**.**
127d9 20 43 68 65 63 6b 20 74 68 65 20 76 61 6c 75 65   Check the value
127da 20 6f 66 20 67 6c 6f 62 61 6c 20 64 61 74 61 62   of global datab
127db 61 73 65 20 70 61 72 61 6d 65 74 65 72 20 6e 75  ase parameter nu
127dc 6d 62 65 72 20 30 20 28 74 68 65 0a 2a 2a 20 73  mber 0 (the.** s
127dd 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 29 20 61  chema version) a
127de 6e 64 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20  nd make sure it 
127df 69 73 20 65 71 75 61 6c 20 74 6f 20 50 32 2e 20  is equal to P2. 
127e0 20 0a 2a 2a 20 50 31 20 69 73 20 74 68 65 20 64   .** P1 is the d
127e1 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 77  atabase number w
127e2 68 69 63 68 20 69 73 20 30 20 66 6f 72 20 74 68  hich is 0 for th
127e3 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
127e4 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 31 20 66 6f  file.** and 1 fo
127e5 72 20 74 68 65 20 66 69 6c 65 20 68 6f 6c 64 69  r the file holdi
127e6 6e 67 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  ng temporary tab
127e7 6c 65 73 20 61 6e 64 20 73 6f 6d 65 20 68 69 67  les and some hig
127e8 68 65 72 20 6e 75 6d 62 65 72 0a 2a 2a 20 66 6f  her number.** fo
127e9 72 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61  r auxiliary data
127ea 62 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  bases..**.** The
127eb 20 63 6f 6f 6b 69 65 20 63 68 61 6e 67 65 73 20   cookie changes 
127ec 69 74 73 20 76 61 6c 75 65 20 77 68 65 6e 65 76  its value whenev
127ed 65 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20  er the database 
127ee 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73 2e 0a  schema changes..
127ef 2a 2a 20 54 68 69 73 20 6f 70 65 72 61 74 69 6f  ** This operatio
127f0 6e 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 74  n is used to det
127f1 65 63 74 20 77 68 65 6e 20 74 68 61 74 20 74 68  ect when that th
127f2 65 20 63 6f 6f 6b 69 65 20 68 61 73 20 63 68 61  e cookie has cha
127f3 6e 67 65 64 0a 2a 2a 20 61 6e 64 20 74 68 61 74  nged.** and that
127f4 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 72 6f   the current pro
127f5 63 65 73 73 20 6e 65 65 64 73 20 74 6f 20 72 65  cess needs to re
127f6 72 65 61 64 20 74 68 65 20 73 63 68 65 6d 61 2e  read the schema.
127f7 0a 2a 2a 0a 2a 2a 20 45 69 74 68 65 72 20 61 20  .**.** Either a 
127f8 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e 65 65 64  transaction need
127f9 73 20 74 6f 20 68 61 76 65 20 62 65 65 6e 20 73  s to have been s
127fa 74 61 72 74 65 64 20 6f 72 20 61 6e 20 4f 50 5f  tarted or an OP_
127fb 4f 70 65 6e 20 6e 65 65 64 73 0a 2a 2a 20 74 6f  Open needs.** to
127fc 20 62 65 20 65 78 65 63 75 74 65 64 20 28 74 6f   be executed (to
127fd 20 65 73 74 61 62 6c 69 73 68 20 61 20 72 65 61   establish a rea
127fe 64 20 6c 6f 63 6b 29 20 62 65 66 6f 72 65 20 74  d lock) before t
127ff 68 69 73 20 6f 70 63 6f 64 65 20 69 73 0a 2a 2a  his opcode is.**
12800 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2f 0a 63 61 73   invoked..*/.cas
12801 65 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69  e OP_VerifyCooki
12802 65 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20 6e 6f  e: {       /* no
12803 2d 70 75 73 68 20 2a 2f 0a 20 20 69 6e 74 20 69  -push */.  int i
12804 4d 65 74 61 3b 0a 20 20 42 74 72 65 65 20 2a 70  Meta;.  Btree *p
12805 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f  Bt;.  assert( pO
12806 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d  p->p1>=0 && pOp-
12807 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20  >p1<db->nDb );. 
12808 20 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 70   pBt = db->aDb[p
12809 4f 70 2d 3e 70 31 5d 2e 70 42 74 3b 0a 20 20 69  Op->p1].pBt;.  i
1280a 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20 72 63  f( pBt ){.    rc
1280b 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47   = sqlite3BtreeG
1280c 65 74 4d 65 74 61 28 70 42 74 2c 20 31 2c 20 28  etMeta(pBt, 1, (
1280d 75 33 32 20 2a 29 26 69 4d 65 74 61 29 3b 0a 20  u32 *)&iMeta);. 
1280e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d   }else{.    rc =
1280f 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
12810 69 4d 65 74 61 20 3d 20 30 3b 0a 20 20 7d 0a 20  iMeta = 0;.  }. 
12811 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
12812 4f 4b 20 26 26 20 69 4d 65 74 61 21 3d 70 4f 70  OK && iMeta!=pOp
12813 2d 3e 70 32 20 29 7b 0a 20 20 20 20 73 71 6c 69  ->p2 ){.    sqli
12814 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d  te3SetString(&p-
12815 3e 7a 45 72 72 4d 73 67 2c 20 22 64 61 74 61 62  >zErrMsg, "datab
12816 61 73 65 20 73 63 68 65 6d 61 20 68 61 73 20 63  ase schema has c
12817 68 61 6e 67 65 64 22 2c 20 28 63 68 61 72 2a 29  hanged", (char*)
12818 30 29 3b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  0);.    /* If th
12819 65 20 73 63 68 65 6d 61 2d 63 6f 6f 6b 69 65 20  e schema-cookie 
1281a 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
1281b 65 20 66 69 6c 65 20 6d 61 74 63 68 65 73 20 74  e file matches t
1281c 68 65 20 63 6f 6f 6b 69 65 20 0a 20 20 20 20 2a  he cookie .    *
1281d 2a 20 73 74 6f 72 65 64 20 77 69 74 68 20 74 68  * stored with th
1281e 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72  e in-memory repr
1281f 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  esentation of th
12820 65 20 73 63 68 65 6d 61 2c 20 64 6f 0a 20 20 20  e schema, do.   
12821 20 2a 2a 20 6e 6f 74 20 72 65 6c 6f 61 64 20 74   ** not reload t
12822 68 65 20 73 63 68 65 6d 61 20 66 72 6f 6d 20 74  he schema from t
12823 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
12824 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
12825 49 66 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65  If virtual-table
12826 73 20 61 72 65 20 69 6e 20 75 73 65 2c 20 74 68  s are in use, th
12827 69 73 20 69 73 20 6e 6f 74 20 6a 75 73 74 20 61  is is not just a
12828 6e 20 6f 70 74 69 6d 69 73 61 74 69 6f 6e 2e 0a  n optimisation..
12829 20 20 20 20 2a 2a 20 4f 66 74 65 6e 2c 20 76 2d      ** Often, v-
1282a 74 61 62 6c 65 73 20 73 74 6f 72 65 20 74 68 65  tables store the
1282b 69 72 20 64 61 74 61 20 69 6e 20 6f 74 68 65 72  ir data in other
1282c 20 53 51 4c 69 74 65 20 74 61 62 6c 65 73 2c 20   SQLite tables, 
1282d 77 68 69 63 68 0a 20 20 20 20 2a 2a 20 61 72 65  which.    ** are
1282e 20 71 75 65 72 69 65 64 20 66 72 6f 6d 20 77 69   queried from wi
1282f 74 68 69 6e 20 78 4e 65 78 74 28 29 20 61 6e 64  thin xNext() and
12830 20 6f 74 68 65 72 20 76 2d 74 61 62 6c 65 20 6d   other v-table m
12831 65 74 68 6f 64 73 20 75 73 69 6e 67 0a 20 20 20  ethods using.   
12832 20 2a 2a 20 70 72 65 70 61 72 65 64 20 71 75 65   ** prepared que
12833 72 69 65 73 2e 20 49 66 20 73 75 63 68 20 61 20  ries. If such a 
12834 71 75 65 72 79 20 69 73 20 6f 75 74 2d 6f 66 2d  query is out-of-
12835 64 61 74 65 2c 20 77 65 20 64 6f 20 6e 6f 74 20  date, we do not 
12836 77 61 6e 74 20 74 6f 0a 20 20 20 20 2a 2a 20 64  want to.    ** d
12837 69 73 63 61 72 64 20 74 68 65 20 64 61 74 61 62  iscard the datab
12838 61 73 65 20 73 63 68 65 6d 61 2c 20 61 73 20 74  ase schema, as t
12839 68 65 20 75 73 65 72 20 63 6f 64 65 20 69 6d 70  he user code imp
1283a 6c 65 6d 65 6e 74 69 6e 67 20 74 68 65 0a 20 20  lementing the.  
1283b 20 20 2a 2a 20 76 2d 74 61 62 6c 65 20 77 6f 75    ** v-table wou
1283c 6c 64 20 68 61 76 65 20 74 6f 20 62 65 20 72 65  ld have to be re
1283d 61 64 79 20 66 6f 72 20 74 68 65 20 73 71 6c 69  ady for the sqli
1283e 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75  te3_vtab structu
1283f 72 65 20 69 74 73 65 6c 66 0a 20 20 20 20 2a 2a  re itself.    **
12840 20 74 6f 20 62 65 20 69 6e 76 61 6c 69 64 61 74   to be invalidat
12841 65 64 20 77 68 65 6e 65 76 65 72 20 73 71 6c 69  ed whenever sqli
12842 74 65 33 5f 73 74 65 70 28 29 20 69 73 20 63 61  te3_step() is ca
12843 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e  lled from within
12844 20 0a 20 20 20 20 2a 2a 20 61 20 76 2d 74 61 62   .    ** a v-tab
12845 6c 65 20 6d 65 74 68 6f 64 2e 0a 20 20 20 20 2a  le method..    *
12846 2f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44  /.    if( db->aD
12847 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 53 63 68 65  b[pOp->p1].pSche
12848 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69  ma->schema_cooki
12849 65 21 3d 69 4d 65 74 61 20 29 7b 0a 20 20 20 20  e!=iMeta ){.    
1284a 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e    sqlite3ResetIn
1284b 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c  ternalSchema(db,
1284c 20 70 4f 70 2d 3e 70 31 29 3b 0a 20 20 20 20 7d   pOp->p1);.    }
1284d 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ..    sqlite3Exp
1284e 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74 65  irePreparedState
1284f 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 20 20 72  ments(db);.    r
12850 63 20 3d 20 53 51 4c 49 54 45 5f 53 43 48 45 4d  c = SQLITE_SCHEM
12851 41 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  A;.  }.  break;.
12852 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70  }../* Opcode: Op
12853 65 6e 52 65 61 64 20 50 31 20 50 32 20 50 33 0a  enRead P1 P2 P3.
12854 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 72 65 61  **.** Open a rea
12855 64 2d 6f 6e 6c 79 20 63 75 72 73 6f 72 20 66 6f  d-only cursor fo
12856 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74  r the database t
12857 61 62 6c 65 20 77 68 6f 73 65 20 72 6f 6f 74 20  able whose root 
12858 70 61 67 65 20 69 73 0a 2a 2a 20 50 32 20 69 6e  page is.** P2 in
12859 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65   a database file
1285a 2e 20 20 54 68 65 20 64 61 74 61 62 61 73 65 20  .  The database 
1285b 66 69 6c 65 20 69 73 20 64 65 74 65 72 6d 69 6e  file is determin
1285c 65 64 20 62 79 20 61 6e 20 0a 2a 2a 20 69 6e 74  ed by an .** int
1285d 65 67 65 72 20 66 72 6f 6d 20 74 68 65 20 74 6f  eger from the to
1285e 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b 2e 20  p of the stack. 
1285f 20 30 20 6d 65 61 6e 73 20 74 68 65 20 6d 61 69   0 means the mai
12860 6e 20 64 61 74 61 62 61 73 65 20 61 6e 64 0a 2a  n database and.*
12861 2a 20 31 20 6d 65 61 6e 73 20 74 68 65 20 64 61  * 1 means the da
12862 74 61 62 61 73 65 20 75 73 65 64 20 66 6f 72 20  tabase used for 
12863 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73  temporary tables
12864 2e 20 20 47 69 76 65 20 74 68 65 20 6e 65 77 20  .  Give the new 
12865 0a 2a 2a 20 63 75 72 73 6f 72 20 61 6e 20 69 64  .** cursor an id
12866 65 6e 74 69 66 69 65 72 20 6f 66 20 50 31 2e 20  entifier of P1. 
12867 20 54 68 65 20 50 31 20 76 61 6c 75 65 73 20 6e   The P1 values n
12868 65 65 64 20 6e 6f 74 20 62 65 20 63 6f 6e 74 69  eed not be conti
12869 67 75 6f 75 73 0a 2a 2a 20 62 75 74 20 61 6c 6c  guous.** but all
1286a 20 50 31 20 76 61 6c 75 65 73 20 73 68 6f 75 6c   P1 values shoul
1286b 64 20 62 65 20 73 6d 61 6c 6c 20 69 6e 74 65 67  d be small integ
1286c 65 72 73 2e 20 20 49 74 20 69 73 20 61 6e 20 65  ers.  It is an e
1286d 72 72 6f 72 20 66 6f 72 0a 2a 2a 20 50 31 20 74  rror for.** P1 t
1286e 6f 20 62 65 20 6e 65 67 61 74 69 76 65 2e 0a 2a  o be negative..*
1286f 2a 0a 2a 2a 20 49 66 20 50 32 3d 3d 30 20 74 68  *.** If P2==0 th
12870 65 6e 20 74 61 6b 65 20 74 68 65 20 72 6f 6f 74  en take the root
12871 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66 72 6f   page number fro
12872 6d 20 74 68 65 20 6e 65 78 74 20 6f 66 20 74 68  m the next of th
12873 65 20 73 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54  e stack..**.** T
12874 68 65 72 65 20 77 69 6c 6c 20 62 65 20 61 20 72  here will be a r
12875 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  ead lock on the 
12876 64 61 74 61 62 61 73 65 20 77 68 65 6e 65 76 65  database wheneve
12877 72 20 74 68 65 72 65 20 69 73 20 61 6e 0a 2a 2a  r there is an.**
12878 20 6f 70 65 6e 20 63 75 72 73 6f 72 2e 20 20 49   open cursor.  I
12879 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77  f the database w
1287a 61 73 20 75 6e 6c 6f 63 6b 65 64 20 70 72 69 6f  as unlocked prio
1287b 72 20 74 6f 20 74 68 69 73 20 69 6e 73 74 72 75  r to this instru
1287c 63 74 69 6f 6e 0a 2a 2a 20 74 68 65 6e 20 61 20  ction.** then a 
1287d 72 65 61 64 20 6c 6f 63 6b 20 69 73 20 61 63 71  read lock is acq
1287e 75 69 72 65 64 20 61 73 20 70 61 72 74 20 6f 66  uired as part of
1287f 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   this instructio
12880 6e 2e 20 20 41 20 72 65 61 64 0a 2a 2a 20 6c 6f  n.  A read.** lo
12881 63 6b 20 61 6c 6c 6f 77 73 20 6f 74 68 65 72 20  ck allows other 
12882 70 72 6f 63 65 73 73 65 73 20 74 6f 20 72 65 61  processes to rea
12883 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62  d the database b
12884 75 74 20 70 72 6f 68 69 62 69 74 73 0a 2a 2a 20  ut prohibits.** 
12885 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73  any other proces
12886 73 20 66 72 6f 6d 20 6d 6f 64 69 66 79 69 6e 67  s from modifying
12887 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20   the database.  
12888 54 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 69 73  The read lock is
12889 0a 2a 2a 20 72 65 6c 65 61 73 65 64 20 77 68 65  .** released whe
1288a 6e 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 61 72  n all cursors ar
1288b 65 20 63 6c 6f 73 65 64 2e 20 20 49 66 20 74 68  e closed.  If th
1288c 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 61  is instruction a
1288d 74 74 65 6d 70 74 73 0a 2a 2a 20 74 6f 20 67 65  ttempts.** to ge
1288e 74 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 62 75  t a read lock bu
1288f 74 20 66 61 69 6c 73 2c 20 74 68 65 20 73 63 72  t fails, the scr
12890 69 70 74 20 74 65 72 6d 69 6e 61 74 65 73 20 77  ipt terminates w
12891 69 74 68 20 61 6e 0a 2a 2a 20 53 51 4c 49 54 45  ith an.** SQLITE
12892 5f 42 55 53 59 20 65 72 72 6f 72 20 63 6f 64 65  _BUSY error code
12893 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 33 20 76  ..**.** The P3 v
12894 61 6c 75 65 20 69 73 20 61 20 70 6f 69 6e 74 65  alue is a pointe
12895 72 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 73  r to a KeyInfo s
12896 74 72 75 63 74 75 72 65 20 74 68 61 74 20 64 65  tructure that de
12897 66 69 6e 65 73 20 74 68 65 0a 2a 2a 20 63 6f 6e  fines the.** con
12898 74 65 6e 74 20 61 6e 64 20 63 6f 6c 6c 61 74 69  tent and collati
12899 6e 67 20 73 65 71 75 65 6e 63 65 20 6f 66 20 69  ng sequence of i
1289a 6e 64 69 63 65 73 2e 20 20 50 33 20 69 73 20 4e  ndices.  P3 is N
1289b 55 4c 4c 20 66 6f 72 20 63 75 72 73 6f 72 73 0a  ULL for cursors.
1289c 2a 2a 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20  ** that are not 
1289d 70 6f 69 6e 74 69 6e 67 20 74 6f 20 69 6e 64 69  pointing to indi
1289e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  ces..**.** See a
1289f 6c 73 6f 20 4f 70 65 6e 57 72 69 74 65 2e 0a 2a  lso OpenWrite..*
128a0 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 65  /./* Opcode: Ope
128a1 6e 57 72 69 74 65 20 50 31 20 50 32 20 50 33 0a  nWrite P1 P2 P3.
128a2 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 72 65 61  **.** Open a rea
128a3 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 20 6e  d/write cursor n
128a4 61 6d 65 64 20 50 31 20 6f 6e 20 74 68 65 20 74  amed P1 on the t
128a5 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 77 68  able or index wh
128a6 6f 73 65 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65  ose root.** page
128a7 20 69 73 20 50 32 2e 20 20 49 66 20 50 32 3d 3d   is P2.  If P2==
128a8 30 20 74 68 65 6e 20 74 61 6b 65 20 74 68 65 20  0 then take the 
128a9 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72  root page number
128aa 20 66 72 6f 6d 20 74 68 65 20 73 74 61 63 6b 2e   from the stack.
128ab 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 33 20 76 61  .**.** The P3 va
128ac 6c 75 65 20 69 73 20 61 20 70 6f 69 6e 74 65 72  lue is a pointer
128ad 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 73 74   to a KeyInfo st
128ae 72 75 63 74 75 72 65 20 74 68 61 74 20 64 65 66  ructure that def
128af 69 6e 65 73 20 74 68 65 0a 2a 2a 20 63 6f 6e 74  ines the.** cont
128b0 65 6e 74 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6e  ent and collatin
128b1 67 20 73 65 71 75 65 6e 63 65 20 6f 66 20 69 6e  g sequence of in
128b2 64 69 63 65 73 2e 20 20 50 33 20 69 73 20 4e 55  dices.  P3 is NU
128b3 4c 4c 20 66 6f 72 20 63 75 72 73 6f 72 73 0a 2a  LL for cursors.*
128b4 2a 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20 70  * that are not p
128b5 6f 69 6e 74 69 6e 67 20 74 6f 20 69 6e 64 69 63  ointing to indic
128b6 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  es..**.** This i
128b7 6e 73 74 72 75 63 74 69 6f 6e 20 77 6f 72 6b 73  nstruction works
128b8 20 6a 75 73 74 20 6c 69 6b 65 20 4f 70 65 6e 52   just like OpenR
128b9 65 61 64 20 65 78 63 65 70 74 20 74 68 61 74 20  ead except that 
128ba 69 74 20 6f 70 65 6e 73 20 74 68 65 20 63 75 72  it opens the cur
128bb 73 6f 72 0a 2a 2a 20 69 6e 20 72 65 61 64 2f 77  sor.** in read/w
128bc 72 69 74 65 20 6d 6f 64 65 2e 20 20 46 6f 72 20  rite mode.  For 
128bd 61 20 67 69 76 65 6e 20 74 61 62 6c 65 2c 20 74  a given table, t
128be 68 65 72 65 20 63 61 6e 20 62 65 20 6f 6e 65 20  here can be one 
128bf 6f 72 20 6d 6f 72 65 20 72 65 61 64 2d 6f 6e 6c  or more read-onl
128c0 79 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f 72 20  y.** cursors or 
128c1 61 20 73 69 6e 67 6c 65 20 72 65 61 64 2f 77 72  a single read/wr
128c2 69 74 65 20 63 75 72 73 6f 72 20 62 75 74 20 6e  ite cursor but n
128c3 6f 74 20 62 6f 74 68 2e 0a 2a 2a 0a 2a 2a 20 53  ot both..**.** S
128c4 65 65 20 61 6c 73 6f 20 4f 70 65 6e 52 65 61 64  ee also OpenRead
128c5 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f 70 65  ..*/.case OP_Ope
128c6 6e 52 65 61 64 3a 20 20 20 20 20 20 20 20 20 20  nRead:          
128c7 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 63 61  /* no-push */.ca
128c8 73 65 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 3a  se OP_OpenWrite:
128c9 20 7b 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70   {       /* no-p
128ca 75 73 68 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d  ush */.  int i =
128cb 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 6e 74 20   pOp->p1;.  int 
128cc 70 32 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20  p2 = pOp->p2;.  
128cd 69 6e 74 20 77 72 46 6c 61 67 3b 0a 20 20 42 74  int wrFlag;.  Bt
128ce 72 65 65 20 2a 70 58 3b 0a 20 20 69 6e 74 20 69  ree *pX;.  int i
128cf 44 62 3b 0a 20 20 43 75 72 73 6f 72 20 2a 70 43  Db;.  Cursor *pC
128d0 75 72 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 20  ur;.  Db *pDb;. 
128d1 20 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f 73   .  assert( pTos
128d2 3e 3d 70 2d 3e 61 53 74 61 63 6b 20 29 3b 0a 20  >=p->aStack );. 
128d3 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49   sqlite3VdbeMemI
128d4 6e 74 65 67 65 72 69 66 79 28 70 54 6f 73 29 3b  ntegerify(pTos);
128d5 0a 20 20 69 44 62 20 3d 20 70 54 6f 73 2d 3e 75  .  iDb = pTos->u
128d6 2e 69 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70  .i;.  assert( (p
128d7 54 6f 73 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Tos->flags & MEM
128d8 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a 20 20 70 54  _Dyn)==0 );.  pT
128d9 6f 73 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20  os--;.  assert( 
128da 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62  iDb>=0 && iDb<db
128db 2d 3e 6e 44 62 20 29 3b 0a 20 20 70 44 62 20 3d  ->nDb );.  pDb =
128dc 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a   &db->aDb[iDb];.
128dd 20 20 70 58 20 3d 20 70 44 62 2d 3e 70 42 74 3b    pX = pDb->pBt;
128de 0a 20 20 61 73 73 65 72 74 28 20 70 58 21 3d 30  .  assert( pX!=0
128df 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f   );.  if( pOp->o
128e0 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e 57 72  pcode==OP_OpenWr
128e1 69 74 65 20 29 7b 0a 20 20 20 20 77 72 46 6c 61  ite ){.    wrFla
128e2 67 20 3d 20 31 3b 0a 20 20 20 20 69 66 28 20 70  g = 1;.    if( p
128e3 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c  Db->pSchema->fil
128e4 65 5f 66 6f 72 6d 61 74 20 3c 20 70 2d 3e 6d 69  e_format < p->mi
128e5 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 6d 61 74  nWriteFileFormat
128e6 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6d 69 6e   ){.      p->min
128e7 57 72 69 74 65 46 69 6c 65 46 6f 72 6d 61 74 20  WriteFileFormat 
128e8 3d 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e  = pDb->pSchema->
128e9 66 69 6c 65 5f 66 6f 72 6d 61 74 3b 0a 20 20 20  file_format;.   
128ea 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
128eb 77 72 46 6c 61 67 20 3d 20 30 3b 0a 20 20 7d 0a  wrFlag = 0;.  }.
128ec 20 20 69 66 28 20 70 32 3c 3d 30 20 29 7b 0a 20    if( p2<=0 ){. 
128ed 20 20 20 61 73 73 65 72 74 28 20 70 54 6f 73 3e     assert( pTos>
128ee 3d 70 2d 3e 61 53 74 61 63 6b 20 29 3b 0a 20 20  =p->aStack );.  
128ef 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
128f0 49 6e 74 65 67 65 72 69 66 79 28 70 54 6f 73 29  Integerify(pTos)
128f1 3b 0a 20 20 20 20 70 32 20 3d 20 70 54 6f 73 2d  ;.    p2 = pTos-
128f2 3e 75 2e 69 3b 0a 20 20 20 20 61 73 73 65 72 74  >u.i;.    assert
128f3 28 20 28 70 54 6f 73 2d 3e 66 6c 61 67 73 20 26  ( (pTos->flags &
128f4 20 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a   MEM_Dyn)==0 );.
128f5 20 20 20 20 70 54 6f 73 2d 2d 3b 0a 20 20 20 20      pTos--;.    
128f6 61 73 73 65 72 74 28 20 70 32 3e 3d 32 20 29 3b  assert( p2>=2 );
128f7 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 69  .  }.  assert( i
128f8 3e 3d 30 20 29 3b 0a 20 20 70 43 75 72 20 3d 20  >=0 );.  pCur = 
128f9 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70  allocateCursor(p
128fa 2c 20 69 2c 20 69 44 62 29 3b 0a 20 20 69 66 28  , i, iDb);.  if(
128fb 20 70 43 75 72 3d 3d 30 20 29 20 67 6f 74 6f 20   pCur==0 ) goto 
128fc 6e 6f 5f 6d 65 6d 3b 0a 20 20 70 43 75 72 2d 3e  no_mem;.  pCur->
128fd 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 69  nullRow = 1;.  i
128fe 66 28 20 70 58 3d 3d 30 20 29 20 62 72 65 61 6b  f( pX==0 ) break
128ff 3b 0a 20 20 2f 2a 20 57 65 20 61 6c 77 61 79 73  ;.  /* We always
12900 20 70 72 6f 76 69 64 65 20 61 20 6b 65 79 20 63   provide a key c
12901 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69  omparison functi
12902 6f 6e 2e 20 20 49 66 20 74 68 65 20 74 61 62 6c  on.  If the tabl
12903 65 20 62 65 69 6e 67 0a 20 20 2a 2a 20 6f 70 65  e being.  ** ope
12904 6e 65 64 20 69 73 20 6f 66 20 74 79 70 65 20 49  ned is of type I
12905 4e 54 4b 45 59 2c 20 74 68 65 20 63 6f 6d 70 61  NTKEY, the compa
12906 72 69 73 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20  rision function 
12907 77 69 6c 6c 20 62 65 20 69 67 6e 6f 72 65 64 2e  will be ignored.
12908 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   */.  rc = sqlit
12909 65 33 42 74 72 65 65 43 75 72 73 6f 72 28 70 58  e3BtreeCursor(pX
1290a 2c 20 70 32 2c 20 77 72 46 6c 61 67 2c 0a 20 20  , p2, wrFlag,.  
1290b 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1290c 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72  VdbeRecordCompar
1290d 65 2c 20 70 4f 70 2d 3e 70 33 2c 0a 20 20 20 20  e, pOp->p3,.    
1290e 20 20 20 20 20 20 20 26 70 43 75 72 2d 3e 70 43         &pCur->pC
1290f 75 72 73 6f 72 29 3b 0a 20 20 69 66 28 20 70 4f  ursor);.  if( pO
12910 70 2d 3e 70 33 74 79 70 65 3d 3d 50 33 5f 4b 45  p->p3type==P3_KE
12911 59 49 4e 46 4f 20 29 7b 0a 20 20 20 20 70 43 75  YINFO ){.    pCu
12912 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 28 4b  r->pKeyInfo = (K
12913 65 79 49 6e 66 6f 2a 29 70 4f 70 2d 3e 70 33 3b  eyInfo*)pOp->p3;
12914 0a 20 20 20 20 70 43 75 72 2d 3e 70 49 6e 63 72  .    pCur->pIncr
12915 4b 65 79 20 3d 20 26 70 43 75 72 2d 3e 70 4b 65  Key = &pCur->pKe
12916 79 49 6e 66 6f 2d 3e 69 6e 63 72 4b 65 79 3b 0a  yInfo->incrKey;.
12917 20 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e      pCur->pKeyIn
12918 66 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 70 2d  fo->enc = ENC(p-
12919 3e 64 62 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  >db);.  }else{. 
1291a 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66     pCur->pKeyInf
1291b 6f 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d  o = 0;.    pCur-
1291c 3e 70 49 6e 63 72 4b 65 79 20 3d 20 26 70 43 75  >pIncrKey = &pCu
1291d 72 2d 3e 62 6f 67 75 73 49 6e 63 72 4b 65 79 3b  r->bogusIncrKey;
1291e 0a 20 20 7d 0a 20 20 73 77 69 74 63 68 28 20 72  .  }.  switch( r
1291f 63 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51  c ){.    case SQ
12920 4c 49 54 45 5f 42 55 53 59 3a 20 7b 0a 20 20 20  LITE_BUSY: {.   
12921 20 20 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a 20     p->pc = pc;. 
12922 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c       p->rc = SQL
12923 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20  ITE_BUSY;.      
12924 70 2d 3e 70 54 6f 73 20 3d 20 26 70 54 6f 73 5b  p->pTos = &pTos[
12925 31 20 2b 20 28 70 4f 70 2d 3e 70 32 3c 3d 30 29  1 + (pOp->p2<=0)
12926 5d 3b 20 2f 2a 20 4f 70 65 72 61 6e 64 73 20 6d  ]; /* Operands m
12927 75 73 74 20 72 65 6d 61 69 6e 20 6f 6e 20 73 74  ust remain on st
12928 61 63 6b 20 2a 2f 0a 20 20 20 20 20 20 72 65 74  ack */.      ret
12929 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  urn SQLITE_BUSY;
1292a 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
1292b 53 51 4c 49 54 45 5f 4f 4b 3a 20 7b 0a 20 20 20  SQLITE_OK: {.   
1292c 20 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 73     int flags = s
1292d 71 6c 69 74 65 33 42 74 72 65 65 46 6c 61 67 73  qlite3BtreeFlags
1292e 28 70 43 75 72 2d 3e 70 43 75 72 73 6f 72 29 3b  (pCur->pCursor);
1292f 0a 20 20 20 20 20 20 2f 2a 20 53 61 6e 69 74 79  .      /* Sanity
12930 20 63 68 65 63 6b 69 6e 67 2e 20 20 4f 6e 6c 79   checking.  Only
12931 20 74 68 65 20 6c 6f 77 65 72 20 66 6f 75 72 20   the lower four 
12932 62 69 74 73 20 6f 66 20 74 68 65 20 66 6c 61 67  bits of the flag
12933 73 20 62 79 74 65 20 73 68 6f 75 6c 64 0a 20 20  s byte should.  
12934 20 20 20 20 2a 2a 20 62 65 20 75 73 65 64 2e 20      ** be used. 
12935 20 42 69 74 20 33 20 28 6d 61 73 6b 20 30 78 30   Bit 3 (mask 0x0
12936 38 29 20 69 73 20 75 6e 70 72 65 64 69 74 61 62  8) is unpreditab
12937 6c 65 2e 20 20 54 68 65 20 6c 6f 77 65 72 20 33  le.  The lower 3
12938 20 62 69 74 73 0a 20 20 20 20 20 20 2a 2a 20 28   bits.      ** (
12939 6d 61 73 6b 20 30 78 30 37 29 20 73 68 6f 75 6c  mask 0x07) shoul
1293a 64 20 62 65 20 65 69 74 68 65 72 20 35 20 28 69  d be either 5 (i
1293b 6e 74 6b 65 79 2b 6c 65 61 66 64 61 74 61 20 66  ntkey+leafdata f
1293c 6f 72 20 74 61 62 6c 65 73 29 20 6f 72 0a 20 20  or tables) or.  
1293d 20 20 20 20 2a 2a 20 32 20 28 7a 65 72 6f 64 61      ** 2 (zeroda
1293e 74 61 20 66 6f 72 20 69 6e 64 69 63 65 73 29 2e  ta for indices).
1293f 20 20 49 66 20 74 68 65 73 65 20 63 6f 6e 64 69    If these condi
12940 74 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 6d 65  tions are not me
12941 74 20 69 74 20 63 61 6e 0a 20 20 20 20 20 20 2a  t it can.      *
12942 2a 20 6f 6e 6c 79 20 6d 65 61 6e 20 74 68 61 74  * only mean that
12943 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20   we are dealing 
12944 77 69 74 68 20 61 20 63 6f 72 72 75 70 74 20 64  with a corrupt d
12945 61 74 61 62 61 73 65 20 66 69 6c 65 0a 20 20 20  atabase file.   
12946 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20     */.      if( 
12947 28 66 6c 61 67 73 20 26 20 30 78 66 30 29 21 3d  (flags & 0xf0)!=
12948 30 20 7c 7c 20 28 28 66 6c 61 67 73 20 26 20 30  0 || ((flags & 0
12949 78 30 37 29 21 3d 35 20 26 26 20 28 66 6c 61 67  x07)!=5 && (flag
1294a 73 20 26 20 30 78 30 37 29 21 3d 32 29 20 29 7b  s & 0x07)!=2) ){
1294b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
1294c 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
1294d 54 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  T;.        goto 
1294e 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72  abort_due_to_err
1294f 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  or;.      }.    
12950 20 20 70 43 75 72 2d 3e 69 73 54 61 62 6c 65 20    pCur->isTable 
12951 3d 20 28 66 6c 61 67 73 20 26 20 42 54 52 45 45  = (flags & BTREE
12952 5f 49 4e 54 4b 45 59 29 21 3d 30 3b 0a 20 20 20  _INTKEY)!=0;.   
12953 20 20 20 70 43 75 72 2d 3e 69 73 49 6e 64 65 78     pCur->isIndex
12954 20 3d 20 28 66 6c 61 67 73 20 26 20 42 54 52 45   = (flags & BTRE
12955 45 5f 5a 45 52 4f 44 41 54 41 29 21 3d 30 3b 0a  E_ZERODATA)!=0;.
12956 20 20 20 20 20 20 2f 2a 20 49 66 20 50 33 3d 3d        /* If P3==
12957 30 20 69 74 20 6d 65 61 6e 73 20 77 65 20 61 72  0 it means we ar
12958 65 20 65 78 70 65 63 74 65 64 20 74 6f 20 6f 70  e expected to op
12959 65 6e 20 61 20 74 61 62 6c 65 2e 20 20 49 66 20  en a table.  If 
1295a 50 33 21 3d 30 20 74 68 65 6e 0a 20 20 20 20 20  P3!=0 then.     
1295b 20 2a 2a 20 77 65 20 65 78 70 65 63 74 20 74 6f   ** we expect to
1295c 20 62 65 20 6f 70 65 6e 69 6e 67 20 61 6e 20 69   be opening an i
1295d 6e 64 65 78 2e 20 20 49 66 20 74 68 69 73 20 69  ndex.  If this i
1295e 73 20 6e 6f 74 20 77 68 61 74 20 68 61 70 70 65  s not what happe
1295f 6e 65 64 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68  ned,.      ** th
12960 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  en the database 
12961 69 73 20 63 6f 72 72 75 70 74 0a 20 20 20 20 20  is corrupt.     
12962 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 28 70   */.      if( (p
12963 43 75 72 2d 3e 69 73 54 61 62 6c 65 20 26 26 20  Cur->isTable && 
12964 70 4f 70 2d 3e 70 33 74 79 70 65 3d 3d 50 33 5f  pOp->p3type==P3_
12965 4b 45 59 49 4e 46 4f 29 0a 20 20 20 20 20 20 20  KEYINFO).       
12966 7c 7c 20 28 70 43 75 72 2d 3e 69 73 49 6e 64 65  || (pCur->isInde
12967 78 20 26 26 20 70 4f 70 2d 3e 70 33 74 79 70 65  x && pOp->p3type
12968 21 3d 50 33 5f 4b 45 59 49 4e 46 4f 29 20 29 7b  !=P3_KEYINFO) ){
12969 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
1296a 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
1296b 54 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  T;.        goto 
1296c 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72  abort_due_to_err
1296d 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  or;.      }.    
1296e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
1296f 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 45     case SQLITE_E
12970 4d 50 54 59 3a 20 7b 0a 20 20 20 20 20 20 70 43  MPTY: {.      pC
12971 75 72 2d 3e 69 73 54 61 62 6c 65 20 3d 20 70 4f  ur->isTable = pO
12972 70 2d 3e 70 33 74 79 70 65 21 3d 50 33 5f 4b 45  p->p3type!=P3_KE
12973 59 49 4e 46 4f 3b 0a 20 20 20 20 20 20 70 43 75  YINFO;.      pCu
12974 72 2d 3e 69 73 49 6e 64 65 78 20 3d 20 21 70 43  r->isIndex = !pC
12975 75 72 2d 3e 69 73 54 61 62 6c 65 3b 0a 20 20 20  ur->isTable;.   
12976 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
12977 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  K;.      break;.
12978 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c      }.    defaul
12979 74 3a 20 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  t: {.      goto 
1297a 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72  abort_due_to_err
1297b 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  or;.    }.  }.  
1297c 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1297d 6f 64 65 3a 20 4f 70 65 6e 45 70 68 65 6d 65 72  ode: OpenEphemer
1297e 61 6c 20 50 31 20 50 32 20 50 33 0a 2a 2a 0a 2a  al P1 P2 P3.**.*
1297f 2a 20 4f 70 65 6e 20 61 20 6e 65 77 20 63 75 72  * Open a new cur
12980 73 6f 72 20 50 31 20 74 6f 20 61 20 74 72 61 6e  sor P1 to a tran
12981 73 69 65 6e 74 20 74 61 62 6c 65 2e 0a 2a 2a 20  sient table..** 
12982 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 61 6c  The cursor is al
12983 77 61 79 73 20 6f 70 65 6e 65 64 20 72 65 61 64  ways opened read
12984 2f 77 72 69 74 65 20 65 76 65 6e 20 69 66 20 0a  /write even if .
12985 2a 2a 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  ** the main data
12986 62 61 73 65 20 69 73 20 72 65 61 64 2d 6f 6e 6c  base is read-onl
12987 79 2e 20 20 54 68 65 20 74 72 61 6e 73 69 65 6e  y.  The transien
12988 74 20 6f 72 20 76 69 72 74 75 61 6c 0a 2a 2a 20  t or virtual.** 
12989 74 61 62 6c 65 20 69 73 20 64 65 6c 65 74 65 64  table is deleted
1298a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 77   automatically w
1298b 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69  hen the cursor i
1298c 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20  s closed..**.** 
1298d 50 32 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  P2 is the number
1298e 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74   of columns in t
1298f 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
12990 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 20  ..** The cursor 
12991 70 6f 69 6e 74 73 20 74 6f 20 61 20 42 54 72 65  points to a BTre
12992 65 20 74 61 62 6c 65 20 69 66 20 50 33 3d 3d 30  e table if P3==0
12993 20 61 6e 64 20 74 6f 20 61 20 42 54 72 65 65 20   and to a BTree 
12994 69 6e 64 65 78 0a 2a 2a 20 69 66 20 50 33 20 69  index.** if P3 i
12995 73 20 6e 6f 74 20 30 2e 20 20 49 66 20 50 33 20  s not 0.  If P3 
12996 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 69 74 20  is not NULL, it 
12997 70 6f 69 6e 74 73 20 74 6f 20 61 20 4b 65 79 49  points to a KeyI
12998 6e 66 6f 20 73 74 72 75 63 74 75 72 65 0a 2a 2a  nfo structure.**
12999 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68   that defines th
1299a 65 20 66 6f 72 6d 61 74 20 6f 66 20 6b 65 79 73  e format of keys
1299b 20 69 6e 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a   in the index..*
1299c 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65  *.** This opcode
1299d 20 77 61 73 20 6f 6e 63 65 20 63 61 6c 6c 65 64   was once called
1299e 20 4f 70 65 6e 54 65 6d 70 2e 20 20 42 75 74 20   OpenTemp.  But 
1299f 74 68 61 74 20 63 72 65 61 74 65 64 0a 2a 2a 20  that created.** 
129a0 63 6f 6e 66 75 73 69 6f 6e 20 62 65 63 61 75 73  confusion becaus
129a1 65 20 74 68 65 20 74 65 72 6d 20 22 74 65 6d 70  e the term "temp
129a2 20 74 61 62 6c 65 22 2c 20 6d 69 67 68 74 20 72   table", might r
129a3 65 66 65 72 20 65 69 74 68 65 72 0a 2a 2a 20 74  efer either.** t
129a4 6f 20 61 20 54 45 4d 50 20 74 61 62 6c 65 20 61  o a TEMP table a
129a5 74 20 74 68 65 20 53 51 4c 20 6c 65 76 65 6c 2c  t the SQL level,
129a6 20 6f 72 20 74 6f 20 61 20 74 61 62 6c 65 20 6f   or to a table o
129a7 70 65 6e 65 64 20 62 79 0a 2a 2a 20 74 68 69 73  pened by.** this
129a8 20 6f 70 63 6f 64 65 2e 20 20 54 68 65 6e 20 74   opcode.  Then t
129a9 68 69 73 20 6f 70 63 6f 64 65 20 77 61 73 20 63  his opcode was c
129aa 61 6c 6c 20 4f 70 65 6e 56 69 72 74 75 61 6c 2e  all OpenVirtual.
129ab 20 20 42 75 74 0a 2a 2a 20 74 68 61 74 20 63 72    But.** that cr
129ac 65 61 74 65 64 20 63 6f 6e 66 75 73 69 6f 6e 20  eated confusion 
129ad 77 69 74 68 20 74 68 65 20 77 68 6f 6c 65 20 76  with the whole v
129ae 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 69 64 65  irtual-table ide
129af 61 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f 70  a..*/.case OP_Op
129b0 65 6e 45 70 68 65 6d 65 72 61 6c 3a 20 7b 20 20  enEphemeral: {  
129b1 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20       /* no-push 
129b2 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70  */.  int i = pOp
129b3 2d 3e 70 31 3b 0a 20 20 43 75 72 73 6f 72 20 2a  ->p1;.  Cursor *
129b4 70 43 78 3b 0a 20 20 61 73 73 65 72 74 28 20 69  pCx;.  assert( i
129b5 3e 3d 30 20 29 3b 0a 20 20 70 43 78 20 3d 20 61  >=0 );.  pCx = a
129b6 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70 2c  llocateCursor(p,
129b7 20 69 2c 20 2d 31 29 3b 0a 20 20 69 66 28 20 70   i, -1);.  if( p
129b8 43 78 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f  Cx==0 ) goto no_
129b9 6d 65 6d 3b 0a 20 20 70 43 78 2d 3e 6e 75 6c 6c  mem;.  pCx->null
129ba 52 6f 77 20 3d 20 31 3b 0a 20 20 72 63 20 3d 20  Row = 1;.  rc = 
129bb 73 71 6c 69 74 65 33 42 74 72 65 65 46 61 63 74  sqlite3BtreeFact
129bc 6f 72 79 28 64 62 2c 20 30 2c 20 31 2c 20 53 51  ory(db, 0, 1, SQ
129bd 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 54 45 4d  LITE_DEFAULT_TEM
129be 50 5f 43 41 43 48 45 5f 53 49 5a 45 2c 20 26 70  P_CACHE_SIZE, &p
129bf 43 78 2d 3e 70 42 74 29 3b 0a 20 20 69 66 28 20  Cx->pBt);.  if( 
129c0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
129c1 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
129c2 33 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73  3BtreeBeginTrans
129c3 28 70 43 78 2d 3e 70 42 74 2c 20 31 29 3b 0a 20  (pCx->pBt, 1);. 
129c4 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c   }.  if( rc==SQL
129c5 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a  ITE_OK ){.    /*
129c6 20 49 66 20 61 20 74 72 61 6e 73 69 65 6e 74 20   If a transient 
129c7 69 6e 64 65 78 20 69 73 20 72 65 71 75 69 72 65  index is require
129c8 64 2c 20 63 72 65 61 74 65 20 69 74 20 62 79 20  d, create it by 
129c9 63 61 6c 6c 69 6e 67 0a 20 20 20 20 2a 2a 20 73  calling.    ** s
129ca 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74  qlite3BtreeCreat
129cb 65 54 61 62 6c 65 28 29 20 77 69 74 68 20 74 68  eTable() with th
129cc 65 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41  e BTREE_ZERODATA
129cd 20 66 6c 61 67 20 62 65 66 6f 72 65 0a 20 20 20   flag before.   
129ce 20 2a 2a 20 6f 70 65 6e 69 6e 67 20 69 74 2e 20   ** opening it. 
129cf 49 66 20 61 20 74 72 61 6e 73 69 65 6e 74 20 74  If a transient t
129d0 61 62 6c 65 20 69 73 20 72 65 71 75 69 72 65 64  able is required
129d1 2c 20 6a 75 73 74 20 75 73 65 20 74 68 65 0a 20  , just use the. 
129d2 20 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61     ** automatica
129d3 6c 6c 79 20 63 72 65 61 74 65 64 20 74 61 62 6c  lly created tabl
129d4 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65  e with root-page
129d5 20 31 20 28 61 6e 20 49 4e 54 4b 45 59 20 74 61   1 (an INTKEY ta
129d6 62 6c 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ble)..    */.   
129d7 20 69 66 28 20 70 4f 70 2d 3e 70 33 20 29 7b 0a   if( pOp->p3 ){.
129d8 20 20 20 20 20 20 69 6e 74 20 70 67 6e 6f 3b 0a        int pgno;.
129d9 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
129da 70 2d 3e 70 33 74 79 70 65 3d 3d 50 33 5f 4b 45  p->p3type==P3_KE
129db 59 49 4e 46 4f 20 29 3b 0a 20 20 20 20 20 20 72  YINFO );.      r
129dc 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
129dd 43 72 65 61 74 65 54 61 62 6c 65 28 70 43 78 2d  CreateTable(pCx-
129de 3e 70 42 74 2c 20 26 70 67 6e 6f 2c 20 42 54 52  >pBt, &pgno, BTR
129df 45 45 5f 5a 45 52 4f 44 41 54 41 29 3b 20 0a 20  EE_ZERODATA); . 
129e0 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
129e1 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
129e2 20 20 61 73 73 65 72 74 28 20 70 67 6e 6f 3d 3d    assert( pgno==
129e3 4d 41 53 54 45 52 5f 52 4f 4f 54 2b 31 20 29 3b  MASTER_ROOT+1 );
129e4 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
129e5 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72  lite3BtreeCursor
129e6 28 70 43 78 2d 3e 70 42 74 2c 20 70 67 6e 6f 2c  (pCx->pBt, pgno,
129e7 20 31 2c 20 73 71 6c 69 74 65 33 56 64 62 65 52   1, sqlite3VdbeR
129e8 65 63 6f 72 64 43 6f 6d 70 61 72 65 2c 0a 20 20  ecordCompare,.  
129e9 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 70            pOp->p
129ea 33 2c 20 26 70 43 78 2d 3e 70 43 75 72 73 6f 72  3, &pCx->pCursor
129eb 29 3b 0a 20 20 20 20 20 20 20 20 70 43 78 2d 3e  );.        pCx->
129ec 70 4b 65 79 49 6e 66 6f 20 3d 20 28 4b 65 79 49  pKeyInfo = (KeyI
129ed 6e 66 6f 2a 29 70 4f 70 2d 3e 70 33 3b 0a 20 20  nfo*)pOp->p3;.  
129ee 20 20 20 20 20 20 70 43 78 2d 3e 70 4b 65 79 49        pCx->pKeyI
129ef 6e 66 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 70  nfo->enc = ENC(p
129f0 2d 3e 64 62 29 3b 0a 20 20 20 20 20 20 20 20 70  ->db);.        p
129f1 43 78 2d 3e 70 49 6e 63 72 4b 65 79 20 3d 20 26  Cx->pIncrKey = &
129f2 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e 69  pCx->pKeyInfo->i
129f3 6e 63 72 4b 65 79 3b 0a 20 20 20 20 20 20 7d 0a  ncrKey;.      }.
129f4 20 20 20 20 20 20 70 43 78 2d 3e 69 73 54 61 62        pCx->isTab
129f5 6c 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73  le = 0;.    }els
129f6 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  e{.      rc = sq
129f7 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72  lite3BtreeCursor
129f8 28 70 43 78 2d 3e 70 42 74 2c 20 4d 41 53 54 45  (pCx->pBt, MASTE
129f9 52 5f 52 4f 4f 54 2c 20 31 2c 20 30 2c 20 30 2c  R_ROOT, 1, 0, 0,
129fa 20 26 70 43 78 2d 3e 70 43 75 72 73 6f 72 29 3b   &pCx->pCursor);
129fb 0a 20 20 20 20 20 20 70 43 78 2d 3e 69 73 54 61  .      pCx->isTa
129fc 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 70  ble = 1;.      p
129fd 43 78 2d 3e 70 49 6e 63 72 4b 65 79 20 3d 20 26  Cx->pIncrKey = &
129fe 70 43 78 2d 3e 62 6f 67 75 73 49 6e 63 72 4b 65  pCx->bogusIncrKe
129ff 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70  y;.    }.  }.  p
12a00 43 78 2d 3e 6e 46 69 65 6c 64 20 3d 20 70 4f 70  Cx->nField = pOp
12a01 2d 3e 70 32 3b 0a 20 20 70 43 78 2d 3e 69 73 49  ->p2;.  pCx->isI
12a02 6e 64 65 78 20 3d 20 21 70 43 78 2d 3e 69 73 54  ndex = !pCx->isT
12a03 61 62 6c 65 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  able;.  break;.}
12a04 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 65  ../* Opcode: Ope
12a05 6e 50 73 65 75 64 6f 20 50 31 20 2a 20 2a 0a 2a  nPseudo P1 * *.*
12a06 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e 65 77 20  *.** Open a new 
12a07 63 75 72 73 6f 72 20 74 68 61 74 20 70 6f 69 6e  cursor that poin
12a08 74 73 20 74 6f 20 61 20 66 61 6b 65 20 74 61 62  ts to a fake tab
12a09 6c 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  le that contains
12a0a 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 72 6f 77   a single.** row
12a0b 20 6f 66 20 64 61 74 61 2e 20 20 41 6e 79 20 61   of data.  Any a
12a0c 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 20  ttempt to write 
12a0d 61 20 73 65 63 6f 6e 64 20 72 6f 77 20 6f 66 20  a second row of 
12a0e 64 61 74 61 20 63 61 75 73 65 73 20 74 68 65 0a  data causes the.
12a0f 2a 2a 20 66 69 72 73 74 20 72 6f 77 20 74 6f 20  ** first row to 
12a10 62 65 20 64 65 6c 65 74 65 64 2e 20 20 41 6c 6c  be deleted.  All
12a11 20 64 61 74 61 20 69 73 20 64 65 6c 65 74 65 64   data is deleted
12a12 20 77 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72   when the cursor
12a13 20 69 73 0a 2a 2a 20 63 6c 6f 73 65 64 2e 0a 2a   is.** closed..*
12a14 2a 0a 2a 2a 20 41 20 70 73 65 75 64 6f 2d 74 61  *.** A pseudo-ta
12a15 62 6c 65 20 63 72 65 61 74 65 64 20 62 79 20 74  ble created by t
12a16 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20 75 73  his opcode is us
12a17 65 66 75 6c 20 66 6f 72 20 68 6f 6c 64 69 6e 67  eful for holding
12a18 20 74 68 65 0a 2a 2a 20 4e 45 57 20 6f 72 20 4f   the.** NEW or O
12a19 4c 44 20 74 61 62 6c 65 73 20 69 6e 20 61 20 74  LD tables in a t
12a1a 72 69 67 67 65 72 2e 20 20 41 6c 73 6f 20 75 73  rigger.  Also us
12a1b 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 61  ed to hold the a
12a1c 20 73 69 6e 67 6c 65 0a 2a 2a 20 72 6f 77 20 6f   single.** row o
12a1d 75 74 70 75 74 20 66 72 6f 6d 20 74 68 65 20 73  utput from the s
12a1e 6f 72 74 65 72 20 73 6f 20 74 68 61 74 20 74 68  orter so that th
12a1f 65 20 72 6f 77 20 63 61 6e 20 62 65 20 64 65 63  e row can be dec
12a20 6f 6d 70 6f 73 65 64 20 69 6e 74 6f 0a 2a 2a 20  omposed into.** 
12a21 69 6e 64 69 76 69 64 75 61 6c 20 63 6f 6c 75 6d  individual colum
12a22 6e 73 20 75 73 69 6e 67 20 74 68 65 20 4f 50 5f  ns using the OP_
12a23 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 2e 0a 2a  Column opcode..*
12a24 2f 0a 63 61 73 65 20 4f 50 5f 4f 70 65 6e 50 73  /.case OP_OpenPs
12a25 65 75 64 6f 3a 20 7b 20 20 20 20 20 20 20 2f 2a  eudo: {       /*
12a26 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 69 6e   no-push */.  in
12a27 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20  t i = pOp->p1;. 
12a28 20 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20 20   Cursor *pCx;.  
12a29 61 73 73 65 72 74 28 20 69 3e 3d 30 20 29 3b 0a  assert( i>=0 );.
12a2a 20 20 70 43 78 20 3d 20 61 6c 6c 6f 63 61 74 65    pCx = allocate
12a2b 43 75 72 73 6f 72 28 70 2c 20 69 2c 20 2d 31 29  Cursor(p, i, -1)
12a2c 3b 0a 20 20 69 66 28 20 70 43 78 3d 3d 30 20 29  ;.  if( pCx==0 )
12a2d 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20   goto no_mem;.  
12a2e 70 43 78 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31  pCx->nullRow = 1
12a2f 3b 0a 20 20 70 43 78 2d 3e 70 73 65 75 64 6f 54  ;.  pCx->pseudoT
12a30 61 62 6c 65 20 3d 20 31 3b 0a 20 20 70 43 78 2d  able = 1;.  pCx-
12a31 3e 70 49 6e 63 72 4b 65 79 20 3d 20 26 70 43 78  >pIncrKey = &pCx
12a32 2d 3e 62 6f 67 75 73 49 6e 63 72 4b 65 79 3b 0a  ->bogusIncrKey;.
12a33 20 20 70 43 78 2d 3e 69 73 54 61 62 6c 65 20 3d    pCx->isTable =
12a34 20 31 3b 0a 20 20 70 43 78 2d 3e 69 73 49 6e 64   1;.  pCx->isInd
12a35 65 78 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b  ex = 0;.  break;
12a36 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43  .}../* Opcode: C
12a37 6c 6f 73 65 20 50 31 20 2a 20 2a 0a 2a 2a 0a 2a  lose P1 * *.**.*
12a38 2a 20 43 6c 6f 73 65 20 61 20 63 75 72 73 6f 72  * Close a cursor
12a39 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 70 65 6e   previously open
12a3a 65 64 20 61 73 20 50 31 2e 20 20 49 66 20 50 31  ed as P1.  If P1
12a3b 20 69 73 20 6e 6f 74 0a 2a 2a 20 63 75 72 72 65   is not.** curre
12a3c 6e 74 6c 79 20 6f 70 65 6e 2c 20 74 68 69 73 20  ntly open, this 
12a3d 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 61  instruction is a
12a3e 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 63 61 73 65 20   no-op..*/.case 
12a3f 4f 50 5f 43 6c 6f 73 65 3a 20 7b 20 20 20 20 20  OP_Close: {     
12a40 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a    /* no-push */.
12a41 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70    int i = pOp->p
12a42 31 3b 0a 20 20 69 66 28 20 69 3e 3d 30 20 26 26  1;.  if( i>=0 &&
12a43 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 7b   i<p->nCursor ){
12a44 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
12a45 46 72 65 65 43 75 72 73 6f 72 28 70 2c 20 70 2d  FreeCursor(p, p-
12a46 3e 61 70 43 73 72 5b 69 5d 29 3b 0a 20 20 20 20  >apCsr[i]);.    
12a47 70 2d 3e 61 70 43 73 72 5b 69 5d 20 3d 20 30 3b  p->apCsr[i] = 0;
12a48 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
12a49 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 6f 76 65  ./* Opcode: Move
12a4a 47 65 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a  Ge P1 P2 *.**.**
12a4b 20 50 6f 70 20 74 68 65 20 74 6f 70 20 6f 66 20   Pop the top of 
12a4c 74 68 65 20 73 74 61 63 6b 20 61 6e 64 20 75 73  the stack and us
12a4d 65 20 69 74 73 20 76 61 6c 75 65 20 61 73 20 61  e its value as a
12a4e 20 6b 65 79 2e 20 20 52 65 70 6f 73 69 74 69 6f   key.  Repositio
12a4f 6e 0a 2a 2a 20 63 75 72 73 6f 72 20 50 31 20 73  n.** cursor P1 s
12a50 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73  o that it points
12a51 20 74 6f 20 74 68 65 20 73 6d 61 6c 6c 65 73 74   to the smallest
12a52 20 65 6e 74 72 79 20 74 68 61 74 20 69 73 20 67   entry that is g
12a53 72 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 6f  reater.** than o
12a54 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b  r equal to the k
12a55 65 79 20 74 68 61 74 20 77 61 73 20 70 6f 70 70  ey that was popp
12a56 65 64 20 66 66 72 6f 6d 20 74 68 65 20 73 74 61  ed ffrom the sta
12a57 63 6b 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20  ck..** If there 
12a58 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 67  are no records g
12a59 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65  reater than or e
12a5a 71 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 79 20  qual to the key 
12a5b 61 6e 64 20 50 32 20 0a 2a 2a 20 69 73 20 6e 6f  and P2 .** is no
12a5c 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75 6d  t zero, then jum
12a5d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53  p to P2..**.** S
12a5e 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20  ee also: Found, 
12a5f 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e  NotFound, Distin
12a60 63 74 2c 20 4d 6f 76 65 4c 74 2c 20 4d 6f 76 65  ct, MoveLt, Move
12a61 47 74 2c 20 4d 6f 76 65 4c 65 0a 2a 2f 0a 2f 2a  Gt, MoveLe.*/./*
12a62 20 4f 70 63 6f 64 65 3a 20 4d 6f 76 65 47 74 20   Opcode: MoveGt 
12a63 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 50 6f  P1 P2 *.**.** Po
12a64 70 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65  p the top of the
12a65 20 73 74 61 63 6b 20 61 6e 64 20 75 73 65 20 69   stack and use i
12a66 74 73 20 76 61 6c 75 65 20 61 73 20 61 20 6b 65  ts value as a ke
12a67 79 2e 20 20 52 65 70 6f 73 69 74 69 6f 6e 0a 2a  y.  Reposition.*
12a68 2a 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74  * cursor P1 so t
12a69 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f  hat it points to
12a6a 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 65 6e   the smallest en
12a6b 74 72 79 20 74 68 61 74 20 69 73 20 67 72 65 61  try that is grea
12a6c 74 65 72 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20  ter.** than the 
12a6d 6b 65 79 20 66 72 6f 6d 20 74 68 65 20 73 74 61  key from the sta
12a6e 63 6b 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20  ck..** If there 
12a6f 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 67  are no records g
12a70 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20  reater than the 
12a71 6b 65 79 20 61 6e 64 20 50 32 20 69 73 20 6e 6f  key and P2 is no
12a72 74 20 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20  t zero,.** then 
12a73 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a  jump to P2..**.*
12a74 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e  * See also: Foun
12a75 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73  d, NotFound, Dis
12a76 74 69 6e 63 74 2c 20 4d 6f 76 65 4c 74 2c 20 4d  tinct, MoveLt, M
12a77 6f 76 65 47 65 2c 20 4d 6f 76 65 4c 65 0a 2a 2f  oveGe, MoveLe.*/
12a78 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 6f 76 65  ./* Opcode: Move
12a79 4c 74 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a  Lt P1 P2 *.**.**
12a7a 20 50 6f 70 20 74 68 65 20 74 6f 70 20 6f 66 20   Pop the top of 
12a7b 74 68 65 20 73 74 61 63 6b 20 61 6e 64 20 75 73  the stack and us
12a7c 65 20 69 74 73 20 76 61 6c 75 65 20 61 73 20 61  e its value as a
12a7d 20 6b 65 79 2e 20 20 52 65 70 6f 73 69 74 69 6f   key.  Repositio
12a7e 6e 0a 2a 2a 20 63 75 72 73 6f 72 20 50 31 20 73  n.** cursor P1 s
12a7f 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73  o that it points
12a80 20 74 6f 20 74 68 65 20 6c 61 72 67 65 73 74 20   to the largest 
12a81 65 6e 74 72 79 20 74 68 61 74 20 69 73 20 6c 65  entry that is le
12a82 73 73 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 6b  ss.** than the k
12a83 65 79 20 66 72 6f 6d 20 74 68 65 20 73 74 61 63  ey from the stac
12a84 6b 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61  k..** If there a
12a85 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 6c 65  re no records le
12a86 73 73 20 74 68 61 6e 20 74 68 65 20 6b 65 79 20  ss than the key 
12a87 61 6e 64 20 50 32 20 69 73 20 6e 6f 74 20 7a 65  and P2 is not ze
12a88 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 6a 75 6d 70  ro,.** then jump
12a89 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65   to P2..**.** Se
12a8a 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e  e also: Found, N
12a8b 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63  otFound, Distinc
12a8c 74 2c 20 4d 6f 76 65 47 74 2c 20 4d 6f 76 65 47  t, MoveGt, MoveG
12a8d 65 2c 20 4d 6f 76 65 4c 65 0a 2a 2f 0a 2f 2a 20  e, MoveLe.*/./* 
12a8e 4f 70 63 6f 64 65 3a 20 4d 6f 76 65 4c 65 20 50  Opcode: MoveLe P
12a8f 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 50 6f 70  1 P2 *.**.** Pop
12a90 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20   the top of the 
12a91 73 74 61 63 6b 20 61 6e 64 20 75 73 65 20 69 74  stack and use it
12a92 73 20 76 61 6c 75 65 20 61 73 20 61 20 6b 65 79  s value as a key
12a93 2e 20 20 52 65 70 6f 73 69 74 69 6f 6e 0a 2a 2a  .  Reposition.**
12a94 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68   cursor P1 so th
12a95 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20  at it points to 
12a96 74 68 65 20 6c 61 72 67 65 73 74 20 65 6e 74 72  the largest entr
12a97 79 20 74 68 61 74 20 69 73 20 6c 65 73 73 20 74  y that is less t
12a98 68 61 6e 0a 2a 2a 20 6f 72 20 65 71 75 61 6c 20  han.** or equal 
12a99 74 6f 20 74 68 65 20 6b 65 79 20 74 68 61 74 20  to the key that 
12a9a 77 61 73 20 70 6f 70 70 65 64 20 66 72 6f 6d 20  was popped from 
12a9b 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2a 20 49 66  the stack..** If
12a9c 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72 65   there are no re
12a9d 63 6f 72 64 73 20 6c 65 73 73 20 74 68 61 6e 20  cords less than 
12a9e 6f 72 20 65 71 61 6c 20 74 6f 20 74 68 65 20 6b  or eqal to the k
12a9f 65 79 20 61 6e 64 20 50 32 20 69 73 20 6e 6f 74  ey and P2 is not
12aa0 20 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 6a   zero,.** then j
12aa1 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a  ump to P2..**.**
12aa2 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64   See also: Found
12aa3 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74  , NotFound, Dist
12aa4 69 6e 63 74 2c 20 4d 6f 76 65 47 74 2c 20 4d 6f  inct, MoveGt, Mo
12aa5 76 65 47 65 2c 20 4d 6f 76 65 4c 74 0a 2a 2f 0a  veGe, MoveLt.*/.
12aa6 63 61 73 65 20 4f 50 5f 4d 6f 76 65 4c 74 3a 20  case OP_MoveLt: 
12aa7 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75          /* no-pu
12aa8 73 68 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 6f  sh */.case OP_Mo
12aa9 76 65 4c 65 3a 20 20 20 20 20 20 20 20 20 2f 2a  veLe:         /*
12aaa 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 63 61 73 65   no-push */.case
12aab 20 4f 50 5f 4d 6f 76 65 47 65 3a 20 20 20 20 20   OP_MoveGe:     
12aac 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a      /* no-push *
12aad 2f 0a 63 61 73 65 20 4f 50 5f 4d 6f 76 65 47 74  /.case OP_MoveGt
12aae 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d  : {       /* no-
12aaf 70 75 73 68 20 2a 2f 0a 20 20 69 6e 74 20 69 20  push */.  int i 
12ab0 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 43 75 72  = pOp->p1;.  Cur
12ab1 73 6f 72 20 2a 70 43 3b 0a 0a 20 20 61 73 73 65  sor *pC;..  asse
12ab2 72 74 28 20 70 54 6f 73 3e 3d 70 2d 3e 61 53 74  rt( pTos>=p->aSt
12ab3 61 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ack );.  assert(
12ab4 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43   i>=0 && i<p->nC
12ab5 75 72 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20  ursor );.  pC = 
12ab6 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61  p->apCsr[i];.  a
12ab7 73 73 65 72 74 28 20 70 43 21 3d 30 20 29 3b 0a  ssert( pC!=0 );.
12ab8 20 20 69 66 28 20 70 43 2d 3e 70 43 75 72 73 6f    if( pC->pCurso
12ab9 72 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  r!=0 ){.    int 
12aba 72 65 73 2c 20 6f 63 3b 0a 20 20 20 20 6f 63 20  res, oc;.    oc 
12abb 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0a 20  = pOp->opcode;. 
12abc 20 20 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d     pC->nullRow =
12abd 20 30 3b 0a 20 20 20 20 2a 70 43 2d 3e 70 49 6e   0;.    *pC->pIn
12abe 63 72 4b 65 79 20 3d 20 6f 63 3d 3d 4f 50 5f 4d  crKey = oc==OP_M
12abf 6f 76 65 47 74 20 7c 7c 20 6f 63 3d 3d 4f 50 5f  oveGt || oc==OP_
12ac0 4d 6f 76 65 4c 65 3b 0a 20 20 20 20 69 66 28 20  MoveLe;.    if( 
12ac1 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 7b 0a 20  pC->isTable ){. 
12ac2 20 20 20 20 20 69 36 34 20 69 4b 65 79 3b 0a 20       i64 iKey;. 
12ac3 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
12ac4 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 54  MemIntegerify(pT
12ac5 6f 73 29 3b 0a 20 20 20 20 20 20 69 4b 65 79 20  os);.      iKey 
12ac6 3d 20 69 6e 74 54 6f 4b 65 79 28 70 54 6f 73 2d  = intToKey(pTos-
12ac7 3e 75 2e 69 29 3b 0a 20 20 20 20 20 20 69 66 28  >u.i);.      if(
12ac8 20 70 4f 70 2d 3e 70 32 3d 3d 30 20 26 26 20 70   pOp->p2==0 && p
12ac9 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4d  Op->opcode==OP_M
12aca 6f 76 65 47 65 20 29 7b 0a 20 20 20 20 20 20 20  oveGe ){.       
12acb 20 70 43 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 65   pC->movetoTarge
12acc 74 20 3d 20 69 4b 65 79 3b 0a 20 20 20 20 20 20  t = iKey;.      
12acd 20 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f    pC->deferredMo
12ace 76 65 74 6f 20 3d 20 31 3b 0a 20 20 20 20 20 20  veto = 1;.      
12acf 20 20 61 73 73 65 72 74 28 20 28 70 54 6f 73 2d    assert( (pTos-
12ad0 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e  >flags & MEM_Dyn
12ad1 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  )==0 );.        
12ad2 70 54 6f 73 2d 2d 3b 0a 20 20 20 20 20 20 20 20  pTos--;.        
12ad3 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
12ad4 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
12ad5 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 70 43 2d  3BtreeMoveto(pC-
12ad6 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 28 75 36  >pCursor, 0, (u6
12ad7 34 29 69 4b 65 79 2c 20 30 2c 20 26 72 65 73 29  4)iKey, 0, &res)
12ad8 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
12ad9 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
12ada 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f       goto abort_
12adb 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20  due_to_error;.  
12adc 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43 2d 3e      }.      pC->
12add 6c 61 73 74 52 6f 77 69 64 20 3d 20 70 54 6f 73  lastRowid = pTos
12ade 2d 3e 75 2e 69 3b 0a 20 20 20 20 20 20 70 43 2d  ->u.i;.      pC-
12adf 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20  >rowidIsValid = 
12ae0 72 65 73 3d 3d 30 3b 0a 20 20 20 20 7d 65 6c 73  res==0;.    }els
12ae1 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  e{.      assert(
12ae2 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 26 20 4d   pTos->flags & M
12ae3 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 20  EM_Blob );.     
12ae4 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 54 6f 73   ExpandBlob(pTos
12ae5 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  );.      rc = sq
12ae6 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f  lite3BtreeMoveto
12ae7 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 70 54  (pC->pCursor, pT
12ae8 6f 73 2d 3e 7a 2c 20 70 54 6f 73 2d 3e 6e 2c 20  os->z, pTos->n, 
12ae9 30 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20 20  0, &res);.      
12aea 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
12aeb 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74  K ){.        got
12aec 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65  o abort_due_to_e
12aed 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rror;.      }.  
12aee 20 20 20 20 70 43 2d 3e 72 6f 77 69 64 49 73 56      pC->rowidIsV
12aef 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  alid = 0;.    }.
12af0 20 20 20 20 70 43 2d 3e 64 65 66 65 72 72 65 64      pC->deferred
12af1 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20  Moveto = 0;.    
12af2 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20  pC->cacheStatus 
12af3 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20  = CACHE_STALE;. 
12af4 20 20 20 2a 70 43 2d 3e 70 49 6e 63 72 4b 65 79     *pC->pIncrKey
12af5 20 3d 20 30 3b 0a 23 69 66 64 65 66 20 53 51 4c   = 0;.#ifdef SQL
12af6 49 54 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c  ITE_TEST.    sql
12af7 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e  ite3_search_coun
12af8 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  t++;.#endif.    
12af9 69 66 28 20 6f 63 3d 3d 4f 50 5f 4d 6f 76 65 47  if( oc==OP_MoveG
12afa 65 20 7c 7c 20 6f 63 3d 3d 4f 50 5f 4d 6f 76 65  e || oc==OP_Move
12afb 47 74 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  Gt ){.      if( 
12afc 72 65 73 3c 30 20 29 7b 0a 20 20 20 20 20 20 20  res<0 ){.       
12afd 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
12afe 65 65 4e 65 78 74 28 70 43 2d 3e 70 43 75 72 73  eeNext(pC->pCurs
12aff 6f 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20  or, &res);.     
12b00 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
12b01 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72  E_OK ) goto abor
12b02 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a  t_due_to_error;.
12b03 20 20 20 20 20 20 20 20 70 43 2d 3e 72 6f 77 69          pC->rowi
12b04 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20  dIsValid = 0;.  
12b05 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
12b06 20 20 20 72 65 73 20 3d 20 30 3b 0a 20 20 20 20     res = 0;.    
12b07 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
12b08 20 20 20 20 20 61 73 73 65 72 74 28 20 6f 63 3d       assert( oc=
12b09 3d 4f 50 5f 4d 6f 76 65 4c 74 20 7c 7c 20 6f 63  =OP_MoveLt || oc
12b0a 3d 3d 4f 50 5f 4d 6f 76 65 4c 65 20 29 3b 0a 20  ==OP_MoveLe );. 
12b0b 20 20 20 20 20 69 66 28 20 72 65 73 3e 3d 30 20       if( res>=0 
12b0c 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
12b0d 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 65 76  sqlite3BtreePrev
12b0e 69 6f 75 73 28 70 43 2d 3e 70 43 75 72 73 6f 72  ious(pC->pCursor
12b0f 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20 20 20  , &res);.       
12b10 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
12b11 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f  OK ) goto abort_
12b12 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20  due_to_error;.  
12b13 20 20 20 20 20 20 70 43 2d 3e 72 6f 77 69 64 49        pC->rowidI
12b14 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20  sValid = 0;.    
12b15 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
12b16 20 2f 2a 20 72 65 73 20 6d 69 67 68 74 20 62 65   /* res might be
12b17 20 6e 65 67 61 74 69 76 65 20 62 65 63 61 75 73   negative becaus
12b18 65 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65  e the table is e
12b19 6d 70 74 79 2e 20 20 43 68 65 63 6b 20 74 6f 0a  mpty.  Check to.
12b1a 20 20 20 20 20 20 20 20 2a 2a 20 73 65 65 20 69          ** see i
12b1b 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63 61  f this is the ca
12b1c 73 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  se..        */. 
12b1d 20 20 20 20 20 20 20 72 65 73 20 3d 20 73 71 6c         res = sql
12b1e 69 74 65 33 42 74 72 65 65 45 6f 66 28 70 43 2d  ite3BtreeEof(pC-
12b1f 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20  >pCursor);.     
12b20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
12b21 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20 69 66   res ){.      if
12b22 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 7b 0a 20  ( pOp->p2>0 ){. 
12b23 20 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d         pc = pOp-
12b24 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 7d  >p2 - 1;.      }
12b25 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 43  else{.        pC
12b26 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20  ->nullRow = 1;. 
12b27 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
12b28 0a 20 20 52 65 6c 65 61 73 65 28 70 54 6f 73 29  .  Release(pTos)
12b29 3b 0a 20 20 70 54 6f 73 2d 2d 3b 0a 20 20 62 72  ;.  pTos--;.  br
12b2a 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
12b2b 65 3a 20 44 69 73 74 69 6e 63 74 20 50 31 20 50  e: Distinct P1 P
12b2c 32 20 2a 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68  2 *.**.** Use th
12b2d 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61  e top of the sta
12b2e 63 6b 20 61 73 20 61 20 72 65 63 6f 72 64 20 63  ck as a record c
12b2f 72 65 61 74 65 64 20 75 73 69 6e 67 20 4d 61 6b  reated using Mak
12b30 65 52 65 63 6f 72 64 2e 20 20 50 31 20 69 73 20  eRecord.  P1 is 
12b31 61 0a 2a 2a 20 63 75 72 73 6f 72 20 6f 6e 20 61  a.** cursor on a
12b32 20 74 61 62 6c 65 20 74 68 61 74 20 64 65 63 6c   table that decl
12b33 61 72 65 64 20 61 73 20 61 6e 20 69 6e 64 65 78  ared as an index
12b34 2e 20 20 49 66 20 74 68 61 74 20 74 61 62 6c 65  .  If that table
12b35 20 63 6f 6e 74 61 69 6e 73 20 61 6e 0a 2a 2a 20   contains an.** 
12b36 65 6e 74 72 79 20 74 68 61 74 20 6d 61 74 63 68  entry that match
12b37 65 73 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68  es the top of th
12b38 65 20 73 74 61 63 6b 20 66 61 6c 6c 20 74 68 72  e stack fall thr
12b39 75 2e 20 20 49 66 20 74 68 65 20 74 6f 70 20 6f  u.  If the top o
12b3a 66 20 74 68 65 20 73 74 61 63 6b 0a 2a 2a 20 6d  f the stack.** m
12b3b 61 74 63 68 65 73 20 6e 6f 20 65 6e 74 72 79 20  atches no entry 
12b3c 69 6e 20 50 31 20 74 68 65 6e 20 6a 75 6d 70 20  in P1 then jump 
12b3d 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  to P2..**.** The
12b3e 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20   cursor is left 
12b3f 70 6f 69 6e 74 69 6e 67 20 61 74 20 74 68 65 20  pointing at the 
12b40 6d 61 74 63 68 69 6e 67 20 65 6e 74 72 79 20 69  matching entry i
12b41 66 20 69 74 20 65 78 69 73 74 73 2e 20 20 54 68  f it exists.  Th
12b42 65 0a 2a 2a 20 72 65 63 6f 72 64 20 6f 6e 20 74  e.** record on t
12b43 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74  he top of the st
12b44 61 63 6b 20 69 73 20 6e 6f 74 20 70 6f 70 70 65  ack is not poppe
12b45 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  d..**.** This in
12b46 73 74 72 75 63 74 69 6f 6e 20 69 73 20 73 69 6d  struction is sim
12b47 69 6c 61 72 20 74 6f 20 4e 6f 74 46 6f 75 6e 64  ilar to NotFound
12b48 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68 69   except that thi
12b49 73 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 64  s operation.** d
12b4a 6f 65 73 20 6e 6f 74 20 70 6f 70 20 74 68 65 20  oes not pop the 
12b4b 6b 65 79 20 66 72 6f 6d 20 74 68 65 20 73 74 61  key from the sta
12b4c 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e  ck..**.** The in
12b4d 73 74 72 75 63 74 69 6f 6e 20 69 73 20 75 73 65  struction is use
12b4e 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74  d to implement t
12b4f 68 65 20 44 49 53 54 49 4e 43 54 20 6f 70 65 72  he DISTINCT oper
12b50 61 74 6f 72 20 6f 6e 20 53 45 4c 45 43 54 0a 2a  ator on SELECT.*
12b51 2a 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 54  * statements.  T
12b52 68 65 20 50 31 20 74 61 62 6c 65 20 69 73 20 6e  he P1 table is n
12b53 6f 74 20 61 20 74 72 75 65 20 69 6e 64 65 78 20  ot a true index 
12b54 62 75 74 20 72 61 74 68 65 72 20 61 20 72 65 63  but rather a rec
12b55 6f 72 64 20 6f 66 0a 2a 2a 20 61 6c 6c 20 72 65  ord of.** all re
12b56 73 75 6c 74 73 20 74 68 61 74 20 68 61 76 65 20  sults that have 
12b57 70 72 6f 64 75 63 65 64 20 73 6f 20 66 61 72 2e  produced so far.
12b58 20 20 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73    .**.** See als
12b59 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75  o: Found, NotFou
12b5a 6e 64 2c 20 4d 6f 76 65 54 6f 2c 20 49 73 55 6e  nd, MoveTo, IsUn
12b5b 69 71 75 65 2c 20 4e 6f 74 45 78 69 73 74 73 0a  ique, NotExists.
12b5c 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 6f  */./* Opcode: Fo
12b5d 75 6e 64 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a  und P1 P2 *.**.*
12b5e 2a 20 54 6f 70 20 6f 66 20 74 68 65 20 73 74 61  * Top of the sta
12b5f 63 6b 20 68 6f 6c 64 73 20 61 20 62 6c 6f 62 20  ck holds a blob 
12b60 63 6f 6e 73 74 72 75 63 74 65 64 20 62 79 20 4d  constructed by M
12b61 61 6b 65 52 65 63 6f 72 64 2e 20 20 50 31 20 69  akeRecord.  P1 i
12b62 73 20 61 6e 20 69 6e 64 65 78 2e 0a 2a 2a 20 49  s an index..** I
12b63 66 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 20  f an entry that 
12b64 6d 61 74 63 68 65 73 20 74 68 65 20 74 6f 70 20  matches the top 
12b65 6f 66 20 74 68 65 20 73 74 61 63 6b 20 65 78 69  of the stack exi
12b66 73 74 73 20 69 6e 20 50 31 20 74 68 65 6e 0a 2a  sts in P1 then.*
12b67 2a 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20 20 49  * jump to P2.  I
12b68 66 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65  f the top of the
12b69 20 73 74 61 63 6b 20 64 6f 65 73 20 6e 6f 74 20   stack does not 
12b6a 6d 61 74 63 68 20 61 6e 79 20 65 6e 74 72 79 20  match any entry 
12b6b 69 6e 20 50 31 0a 2a 2a 20 74 68 65 6e 20 66 61  in P1.** then fa
12b6c 6c 6c 20 74 68 72 75 2e 20 20 54 68 65 20 50 31  ll thru.  The P1
12b6d 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20   cursor is left 
12b6e 70 6f 69 6e 74 69 6e 67 20 61 74 20 74 68 65 20  pointing at the 
12b6f 6d 61 74 63 68 69 6e 67 20 65 6e 74 72 79 0a 2a  matching entry.*
12b70 2a 20 69 66 20 69 74 20 65 78 69 73 74 73 2e 20  * if it exists. 
12b71 20 54 68 65 20 62 6c 6f 62 20 69 73 20 70 6f 70   The blob is pop
12b72 70 65 64 20 6f 66 66 20 74 68 65 20 74 6f 70 20  ped off the top 
12b73 6f 66 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2a  of the stack..**
12b74 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63  .** This instruc
12b75 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20  tion is used to 
12b76 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 49 4e  implement the IN
12b77 20 6f 70 65 72 61 74 6f 72 20 77 68 65 72 65 20   operator where 
12b78 74 68 65 0a 2a 2a 20 6c 65 66 74 2d 68 61 6e 64  the.** left-hand
12b79 20 73 69 64 65 20 69 73 20 61 20 53 45 4c 45 43   side is a SELEC
12b7a 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 50 31  T statement.  P1
12b7b 20 69 73 20 6e 6f 74 20 61 20 74 72 75 65 20 69   is not a true i
12b7c 6e 64 65 78 20 62 75 74 0a 2a 2a 20 69 73 20 69  ndex but.** is i
12b7d 6e 73 74 65 61 64 20 61 20 74 65 6d 70 6f 72 61  nstead a tempora
12b7e 72 79 20 69 6e 64 65 78 20 74 68 61 74 20 68 6f  ry index that ho
12b7f 6c 64 73 20 74 68 65 20 72 65 73 75 6c 74 73 20  lds the results 
12b80 6f 66 20 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a  of the SELECT.**
12b81 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 69   statement.  Thi
12b82 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6a 75  s instruction ju
12b83 73 74 20 63 68 65 63 6b 73 20 74 6f 20 73 65 65  st checks to see
12b84 20 69 66 20 74 68 65 20 6c 65 66 74 2d 68 61 6e   if the left-han
12b85 64 20 73 69 64 65 0a 2a 2a 20 6f 66 20 74 68 65  d side.** of the
12b86 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 28 73 74   IN operator (st
12b87 6f 72 65 64 20 6f 6e 20 74 68 65 20 74 6f 70 20  ored on the top 
12b88 6f 66 20 74 68 65 20 73 74 61 63 6b 29 20 65 78  of the stack) ex
12b89 69 73 74 73 20 69 6e 20 74 68 65 0a 2a 2a 20 72  ists in the.** r
12b8a 65 73 75 6c 74 20 6f 66 20 74 68 65 20 53 45 4c  esult of the SEL
12b8b 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  ECT statement..*
12b8c 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 44  *.** See also: D
12b8d 69 73 74 69 6e 63 74 2c 20 4e 6f 74 46 6f 75 6e  istinct, NotFoun
12b8e 64 2c 20 4d 6f 76 65 54 6f 2c 20 49 73 55 6e 69  d, MoveTo, IsUni
12b8f 71 75 65 2c 20 4e 6f 74 45 78 69 73 74 73 0a 2a  que, NotExists.*
12b90 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 74  /./* Opcode: Not
12b91 46 6f 75 6e 64 20 50 31 20 50 32 20 2a 0a 2a 2a  Found P1 P2 *.**
12b92 0a 2a 2a 20 54 68 65 20 74 6f 70 20 6f 66 20 74  .** The top of t
12b93 68 65 20 73 74 61 63 6b 20 68 6f 6c 64 73 20 61  he stack holds a
12b94 20 62 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74 65   blob constructe
12b95 64 20 62 79 20 4d 61 6b 65 52 65 63 6f 72 64 2e  d by MakeRecord.
12b96 20 20 50 31 20 69 73 0a 2a 2a 20 61 6e 20 69 6e    P1 is.** an in
12b97 64 65 78 2e 20 20 49 66 20 6e 6f 20 65 6e 74 72  dex.  If no entr
12b98 79 20 65 78 69 73 74 73 20 69 6e 20 50 31 20 74  y exists in P1 t
12b99 68 61 74 20 6d 61 74 63 68 65 73 20 74 68 65 20  hat matches the 
12b9a 62 6c 6f 62 20 74 68 65 6e 20 6a 75 6d 70 0a 2a  blob then jump.*
12b9b 2a 20 74 6f 20 50 32 2e 20 20 49 66 20 61 6e 20  * to P2.  If an 
12b9c 65 6e 74 72 79 20 64 6f 65 73 20 65 78 69 73 74  entry does exist
12b9d 69 6e 67 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67  ing, fall throug
12b9e 68 2e 20 20 54 68 65 20 63 75 72 73 6f 72 20 69  h.  The cursor i
12b9f 73 20 6c 65 66 74 0a 2a 2a 20 70 6f 69 6e 74 69  s left.** pointi
12ba0 6e 67 20 74 6f 20 74 68 65 20 65 6e 74 72 79 20  ng to the entry 
12ba1 74 68 61 74 20 6d 61 74 63 68 65 73 2e 20 20 54  that matches.  T
12ba2 68 65 20 62 6c 6f 62 20 69 73 20 70 6f 70 70 65  he blob is poppe
12ba3 64 20 66 72 6f 6d 20 74 68 65 20 73 74 61 63 6b  d from the stack
12ba4 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 66 66  ..**.** The diff
12ba5 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74  erence between t
12ba6 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 61 6e  his operation an
12ba7 64 20 44 69 73 74 69 6e 63 74 20 69 73 20 74 68  d Distinct is th
12ba8 61 74 0a 2a 2a 20 44 69 73 74 69 6e 63 74 20 64  at.** Distinct d
12ba9 6f 65 73 20 6e 6f 74 20 70 6f 70 20 74 68 65 20  oes not pop the 
12baa 6b 65 79 20 66 72 6f 6d 20 74 68 65 20 73 74 61  key from the sta
12bab 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c  ck..**.** See al
12bac 73 6f 3a 20 44 69 73 74 69 6e 63 74 2c 20 46 6f  so: Distinct, Fo
12bad 75 6e 64 2c 20 4d 6f 76 65 54 6f 2c 20 4e 6f 74  und, MoveTo, Not
12bae 45 78 69 73 74 73 2c 20 49 73 55 6e 69 71 75 65  Exists, IsUnique
12baf 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 69 73 74  .*/.case OP_Dist
12bb0 69 6e 63 74 3a 20 20 20 20 20 20 20 2f 2a 20 6e  inct:       /* n
12bb1 6f 2d 70 75 73 68 20 2a 2f 0a 63 61 73 65 20 4f  o-push */.case O
12bb2 50 5f 4e 6f 74 46 6f 75 6e 64 3a 20 20 20 20 20  P_NotFound:     
12bb3 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a    /* no-push */.
12bb4 63 61 73 65 20 4f 50 5f 46 6f 75 6e 64 3a 20 7b  case OP_Found: {
12bb5 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75          /* no-pu
12bb6 73 68 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20  sh */.  int i = 
12bb7 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 6e 74 20 61  pOp->p1;.  int a
12bb8 6c 72 65 61 64 79 45 78 69 73 74 73 20 3d 20 30  lreadyExists = 0
12bb9 3b 0a 20 20 43 75 72 73 6f 72 20 2a 70 43 3b 0a  ;.  Cursor *pC;.
12bba 20 20 61 73 73 65 72 74 28 20 70 54 6f 73 3e 3d    assert( pTos>=
12bbb 70 2d 3e 61 53 74 61 63 6b 20 29 3b 0a 20 20 61  p->aStack );.  a
12bbc 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69  ssert( i>=0 && i
12bbd 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20  <p->nCursor );. 
12bbe 20 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73   assert( p->apCs
12bbf 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28  r[i]!=0 );.  if(
12bc0 20 28 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b   (pC = p->apCsr[
12bc1 69 5d 29 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20  i])->pCursor!=0 
12bc2 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 73 2c 20  ){.    int res, 
12bc3 72 78 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  rx;.    assert( 
12bc4 70 43 2d 3e 69 73 54 61 62 6c 65 3d 3d 30 20 29  pC->isTable==0 )
12bc5 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54  ;.    assert( pT
12bc6 6f 73 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  os->flags & MEM_
12bc7 42 6c 6f 62 20 29 3b 0a 20 20 20 20 53 74 72 69  Blob );.    Stri
12bc8 6e 67 69 66 79 28 70 54 6f 73 2c 20 65 6e 63 6f  ngify(pTos, enco
12bc9 64 69 6e 67 29 3b 0a 20 20 20 20 72 78 20 3d 20  ding);.    rx = 
12bca 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65  sqlite3BtreeMove
12bcb 74 6f 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20  to(pC->pCursor, 
12bcc 70 54 6f 73 2d 3e 7a 2c 20 70 54 6f 73 2d 3e 6e  pTos->z, pTos->n
12bcd 2c 20 30 2c 20 26 72 65 73 29 3b 0a 20 20 20 20  , 0, &res);.    
12bce 61 6c 72 65 61 64 79 45 78 69 73 74 73 20 3d 20  alreadyExists = 
12bcf 72 78 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rx==SQLITE_OK &&
12bd0 20 72 65 73 3d 3d 30 3b 0a 20 20 20 20 70 43 2d   res==0;.    pC-
12bd1 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20  >deferredMoveto 
12bd2 3d 20 30 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63  = 0;.    pC->cac
12bd3 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45  heStatus = CACHE
12bd4 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 69 66  _STALE;.  }.  if
12bd5 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f  ( pOp->opcode==O
12bd6 50 5f 46 6f 75 6e 64 20 29 7b 0a 20 20 20 20 69  P_Found ){.    i
12bd7 66 28 20 61 6c 72 65 61 64 79 45 78 69 73 74 73  f( alreadyExists
12bd8 20 29 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20   ) pc = pOp->p2 
12bd9 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  - 1;.  }else{.  
12bda 20 20 69 66 28 20 21 61 6c 72 65 61 64 79 45 78    if( !alreadyEx
12bdb 69 73 74 73 20 29 20 70 63 20 3d 20 70 4f 70 2d  ists ) pc = pOp-
12bdc 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 69  >p2 - 1;.  }.  i
12bdd 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 21 3d  f( pOp->opcode!=
12bde 4f 50 5f 44 69 73 74 69 6e 63 74 20 29 7b 0a 20  OP_Distinct ){. 
12bdf 20 20 20 52 65 6c 65 61 73 65 28 70 54 6f 73 29     Release(pTos)
12be0 3b 0a 20 20 20 20 70 54 6f 73 2d 2d 3b 0a 20 20  ;.    pTos--;.  
12be1 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
12be2 20 4f 70 63 6f 64 65 3a 20 49 73 55 6e 69 71 75   Opcode: IsUniqu
12be3 65 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20  e P1 P2 *.**.** 
12be4 54 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73  The top of the s
12be5 74 61 63 6b 20 69 73 20 61 6e 20 69 6e 74 65 67  tack is an integ
12be6 65 72 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  er record number
12be7 2e 20 20 43 61 6c 6c 20 74 68 69 73 0a 2a 2a 20  .  Call this.** 
12be8 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 52 2e  record number R.
12be9 20 20 54 68 65 20 6e 65 78 74 20 6f 6e 20 74 68    The next on th
12bea 65 20 73 74 61 63 6b 20 69 73 20 61 6e 20 69 6e  e stack is an in
12beb 64 65 78 20 6b 65 79 20 63 72 65 61 74 65 64 0a  dex key created.
12bec 2a 2a 20 75 73 69 6e 67 20 4d 61 6b 65 49 64 78  ** using MakeIdx
12bed 52 65 63 2e 20 20 43 61 6c 6c 20 69 74 20 4b 2e  Rec.  Call it K.
12bee 20 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69    This instructi
12bef 6f 6e 20 70 6f 70 73 20 52 20 66 72 6f 6d 20 74  on pops R from t
12bf0 68 65 0a 2a 2a 20 73 74 61 63 6b 20 62 75 74 20  he.** stack but 
12bf1 69 74 20 6c 65 61 76 65 73 20 4b 20 75 6e 63 68  it leaves K unch
12bf2 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 31 20  anged..**.** P1 
12bf3 69 73 20 61 6e 20 69 6e 64 65 78 2e 20 20 53 6f  is an index.  So
12bf4 20 69 74 20 68 61 73 20 6e 6f 20 64 61 74 61 20   it has no data 
12bf5 61 6e 64 20 69 74 73 20 6b 65 79 20 63 6f 6e 73  and its key cons
12bf6 69 73 74 73 20 6f 66 20 61 0a 2a 2a 20 72 65 63  ists of a.** rec
12bf7 6f 72 64 20 67 65 6e 65 72 61 74 65 64 20 62 79  ord generated by
12bf8 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 77   OP_MakeRecord w
12bf9 68 65 72 65 20 74 68 65 20 6c 61 73 74 20 66 69  here the last fi
12bfa 65 6c 64 20 69 73 20 74 68 65 20 0a 2a 2a 20 72  eld is the .** r
12bfb 6f 77 69 64 20 6f 66 20 74 68 65 20 65 6e 74 72  owid of the entr
12bfc 79 20 74 68 61 74 20 74 68 65 20 69 6e 64 65 78  y that the index
12bfd 20 72 65 66 65 72 73 20 74 6f 2e 0a 2a 2a 20 0a   refers to..** .
12bfe 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74  ** This instruct
12bff 69 6f 6e 20 61 73 6b 73 20 69 66 20 74 68 65 72  ion asks if ther
12c00 65 20 69 73 20 61 6e 20 65 6e 74 72 79 20 69 6e  e is an entry in
12c01 20 50 31 20 77 68 65 72 65 20 74 68 65 0a 2a 2a   P1 where the.**
12c02 20 66 69 65 6c 64 73 20 6d 61 74 63 68 65 73 20   fields matches 
12c03 4b 20 62 75 74 20 74 68 65 20 72 6f 77 69 64 20  K but the rowid 
12c04 69 73 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f  is different fro
12c05 6d 20 52 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65  m R..** If there
12c06 20 69 73 20 6e 6f 20 73 75 63 68 20 65 6e 74 72   is no such entr
12c07 79 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69 73  y, then there is
12c08 20 61 6e 20 69 6d 6d 65 64 69 61 74 65 0a 2a 2a   an immediate.**
12c09 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20 20 49 66   jump to P2.  If
12c0a 20 61 6e 79 20 65 6e 74 72 79 20 64 6f 65 73 20   any entry does 
12c0b 65 78 69 73 74 20 77 68 65 72 65 20 74 68 65 20  exist where the 
12c0c 69 6e 64 65 78 20 73 74 72 69 6e 67 0a 2a 2a 20  index string.** 
12c0d 6d 61 74 63 68 65 73 20 4b 20 62 75 74 20 74 68  matches K but th
12c0e 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20  e record number 
12c0f 69 73 20 6e 6f 74 20 52 2c 20 74 68 65 6e 20 74  is not R, then t
12c10 68 65 20 72 65 63 6f 72 64 0a 2a 2a 20 6e 75 6d  he record.** num
12c11 62 65 72 20 66 6f 72 20 74 68 61 74 20 65 6e 74  ber for that ent
12c12 72 79 20 69 73 20 70 75 73 68 65 64 20 6f 6e 74  ry is pushed ont
12c13 6f 20 74 68 65 20 73 74 61 63 6b 20 61 6e 64 20  o the stack and 
12c14 63 6f 6e 74 72 6f 6c 0a 2a 2a 20 66 61 6c 6c 73  control.** falls
12c15 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20   through to the 
12c16 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e  next instruction
12c17 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
12c18 3a 20 44 69 73 74 69 6e 63 74 2c 20 4e 6f 74 46  : Distinct, NotF
12c19 6f 75 6e 64 2c 20 4e 6f 74 45 78 69 73 74 73 2c  ound, NotExists,
12c1a 20 46 6f 75 6e 64 0a 2a 2f 0a 63 61 73 65 20 4f   Found.*/.case O
12c1b 50 5f 49 73 55 6e 69 71 75 65 3a 20 7b 20 20 20  P_IsUnique: {   
12c1c 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20       /* no-push 
12c1d 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70  */.  int i = pOp
12c1e 2d 3e 70 31 3b 0a 20 20 4d 65 6d 20 2a 70 4e 6f  ->p1;.  Mem *pNo
12c1f 73 20 3d 20 26 70 54 6f 73 5b 2d 31 5d 3b 0a 20  s = &pTos[-1];. 
12c20 20 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20 20   Cursor *pCx;.  
12c21 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b  BtCursor *pCrsr;
12c22 0a 20 20 69 36 34 20 52 3b 0a 0a 20 20 2f 2a 20  .  i64 R;..  /* 
12c23 50 6f 70 20 74 68 65 20 76 61 6c 75 65 20 52 20  Pop the value R 
12c24 6f 66 66 20 74 68 65 20 74 6f 70 20 6f 66 20 74  off the top of t
12c25 68 65 20 73 74 61 63 6b 0a 20 20 2a 2f 0a 20 20  he stack.  */.  
12c26 61 73 73 65 72 74 28 20 70 4e 6f 73 3e 3d 70 2d  assert( pNos>=p-
12c27 3e 61 53 74 61 63 6b 20 29 3b 0a 20 20 73 71 6c  >aStack );.  sql
12c28 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67  ite3VdbeMemInteg
12c29 65 72 69 66 79 28 70 54 6f 73 29 3b 0a 20 20 52  erify(pTos);.  R
12c2a 20 3d 20 70 54 6f 73 2d 3e 75 2e 69 3b 0a 20 20   = pTos->u.i;.  
12c2b 61 73 73 65 72 74 28 20 28 70 54 6f 73 2d 3e 66  assert( (pTos->f
12c2c 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 3d  lags & MEM_Dyn)=
12c2d 3d 30 20 29 3b 0a 20 20 70 54 6f 73 2d 2d 3b 0a  =0 );.  pTos--;.
12c2e 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26    assert( i>=0 &
12c2f 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29  & i<p->nCursor )
12c30 3b 0a 20 20 70 43 78 20 3d 20 70 2d 3e 61 70 43  ;.  pCx = p->apC
12c31 73 72 5b 69 5d 3b 0a 20 20 61 73 73 65 72 74 28  sr[i];.  assert(
12c32 20 70 43 78 21 3d 30 20 29 3b 0a 20 20 70 43 72   pCx!=0 );.  pCr
12c33 73 72 20 3d 20 70 43 78 2d 3e 70 43 75 72 73 6f  sr = pCx->pCurso
12c34 72 3b 0a 20 20 69 66 28 20 70 43 72 73 72 21 3d  r;.  if( pCrsr!=
12c35 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 73  0 ){.    int res
12c36 3b 0a 20 20 20 20 69 36 34 20 76 3b 20 20 20 20  ;.    i64 v;    
12c37 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 63 6f       /* The reco
12c38 72 64 20 6e 75 6d 62 65 72 20 6f 6e 20 74 68 65  rd number on the
12c39 20 50 31 20 65 6e 74 72 79 20 74 68 61 74 20 6d   P1 entry that m
12c3a 61 74 63 68 65 73 20 4b 20 2a 2f 0a 20 20 20 20  atches K */.    
12c3b 63 68 61 72 20 2a 7a 4b 65 79 3b 20 20 20 20 2f  char *zKey;    /
12c3c 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 4b  * The value of K
12c3d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 4b 65 79   */.    int nKey
12c3e 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  ;      /* Number
12c3f 20 6f 66 20 62 79 74 65 73 20 69 6e 20 4b 20 2a   of bytes in K *
12c40 2f 0a 20 20 20 20 69 6e 74 20 6c 65 6e 3b 20 20  /.    int len;  
12c41 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
12c42 66 20 62 79 74 65 73 20 69 6e 20 4b 20 77 69 74  f bytes in K wit
12c43 68 6f 75 74 20 74 68 65 20 72 6f 77 69 64 20 61  hout the rowid a
12c44 74 20 74 68 65 20 65 6e 64 20 2a 2f 0a 20 20 20  t the end */.   
12c45 20 69 6e 74 20 73 7a 52 6f 77 69 64 3b 20 20 20   int szRowid;   
12c46 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 72  /* Size of the r
12c47 6f 77 69 64 20 63 6f 6c 75 6d 6e 20 61 74 20 74  owid column at t
12c48 68 65 20 65 6e 64 20 6f 66 20 7a 4b 65 79 20 2a  he end of zKey *
12c49 2f 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73  /..    /* Make s
12c4a 75 72 65 20 4b 20 69 73 20 61 20 73 74 72 69 6e  ure K is a strin
12c4b 67 20 61 6e 64 20 6d 61 6b 65 20 7a 4b 65 79 20  g and make zKey 
12c4c 70 6f 69 6e 74 20 74 6f 20 4b 0a 20 20 20 20 2a  point to K.    *
12c4d 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e  /.    assert( pN
12c4e 6f 73 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  os->flags & MEM_
12c4f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 53 74 72 69  Blob );.    Stri
12c50 6e 67 69 66 79 28 70 4e 6f 73 2c 20 65 6e 63 6f  ngify(pNos, enco
12c51 64 69 6e 67 29 3b 0a 20 20 20 20 7a 4b 65 79 20  ding);.    zKey 
12c52 3d 20 70 4e 6f 73 2d 3e 7a 3b 0a 20 20 20 20 6e  = pNos->z;.    n
12c53 4b 65 79 20 3d 20 70 4e 6f 73 2d 3e 6e 3b 0a 0a  Key = pNos->n;..
12c54 20 20 20 20 73 7a 52 6f 77 69 64 20 3d 20 73 71      szRowid = sq
12c55 6c 69 74 65 33 56 64 62 65 49 64 78 52 6f 77 69  lite3VdbeIdxRowi
12c56 64 4c 65 6e 28 28 75 38 2a 29 7a 4b 65 79 29 3b  dLen((u8*)zKey);
12c57 0a 20 20 20 20 6c 65 6e 20 3d 20 6e 4b 65 79 2d  .    len = nKey-
12c58 73 7a 52 6f 77 69 64 3b 0a 0a 20 20 20 20 2f 2a  szRowid;..    /*
12c59 20 53 65 61 72 63 68 20 66 6f 72 20 61 6e 20 65   Search for an e
12c5a 6e 74 72 79 20 69 6e 20 50 31 20 77 68 65 72 65  ntry in P1 where
12c5b 20 61 6c 6c 20 62 75 74 20 74 68 65 20 6c 61 73   all but the las
12c5c 74 20 66 6f 75 72 20 62 79 74 65 73 20 6d 61 74  t four bytes mat
12c5d 63 68 20 4b 2e 0a 20 20 20 20 2a 2a 20 49 66 20  ch K..    ** If 
12c5e 74 68 65 72 65 20 69 73 20 6e 6f 20 73 75 63 68  there is no such
12c5f 20 65 6e 74 72 79 2c 20 6a 75 6d 70 20 69 6d 6d   entry, jump imm
12c60 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a  ediately to P2..
12c61 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72      */.    asser
12c62 74 28 20 70 43 78 2d 3e 64 65 66 65 72 72 65 64  t( pCx->deferred
12c63 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20  Moveto==0 );.   
12c64 20 70 43 78 2d 3e 63 61 63 68 65 53 74 61 74 75   pCx->cacheStatu
12c65 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b  s = CACHE_STALE;
12c66 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
12c67 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 70 43 72  3BtreeMoveto(pCr
12c68 73 72 2c 20 7a 4b 65 79 2c 20 6c 65 6e 2c 20 30  sr, zKey, len, 0
12c69 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 69 66 28  , &res);.    if(
12c6a 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
12c6b 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f  {.      goto abo
12c6c 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b  rt_due_to_error;
12c6d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72  .    }.    if( r
12c6e 65 73 3c 30 20 29 7b 0a 20 20 20 20 20 20 72 63  es<0 ){.      rc
12c6f 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e   = sqlite3BtreeN
12c70 65 78 74 28 70 43 72 73 72 2c 20 26 72 65 73 29  ext(pCrsr, &res)
12c71 3b 0a 20 20 20 20 20 20 69 66 28 20 72 65 73 20  ;.      if( res 
12c72 29 7b 0a 20 20 20 20 20 20 20 20 70 63 20 3d 20  ){.        pc = 
12c73 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20  pOp->p2 - 1;.   
12c74 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
12c75 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 72 63    }.    }.    rc
12c76 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 64   = sqlite3VdbeId
12c77 78 4b 65 79 43 6f 6d 70 61 72 65 28 70 43 78 2c  xKeyCompare(pCx,
12c78 20 6c 65 6e 2c 20 28 75 38 2a 29 7a 4b 65 79 2c   len, (u8*)zKey,
12c79 20 26 72 65 73 29 3b 20 0a 20 20 20 20 69 66 28   &res); .    if(
12c7a 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
12c7b 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
12c7c 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 69 66  to_error;.    if
12c7d 28 20 72 65 73 3e 30 20 29 7b 0a 20 20 20 20 20  ( res>0 ){.     
12c7e 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
12c7f 31 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  1;.      break;.
12c80 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 74      }..    /* At
12c81 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 70 43 72   this point, pCr
12c82 73 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74  sr is pointing t
12c83 6f 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 50 31  o an entry in P1
12c84 20 77 68 65 72 65 20 61 6c 6c 20 62 75 74 0a 20   where all but. 
12c85 20 20 20 2a 2a 20 74 68 65 20 66 69 6e 61 6c 20     ** the final 
12c86 65 6e 74 72 79 20 28 74 68 65 20 72 6f 77 69 64  entry (the rowid
12c87 29 20 6d 61 74 63 68 65 73 20 4b 2e 20 20 43 68  ) matches K.  Ch
12c88 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68  eck to see if th
12c89 65 0a 20 20 20 20 2a 2a 20 66 69 6e 61 6c 20 72  e.    ** final r
12c8a 6f 77 69 64 20 63 6f 6c 75 6d 6e 20 69 73 20 64  owid column is d
12c8b 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 52 2e  ifferent from R.
12c8c 20 20 49 66 20 69 74 20 65 71 75 61 6c 73 20 52    If it equals R
12c8d 20 74 68 65 6e 20 6a 75 6d 70 0a 20 20 20 20 2a   then jump.    *
12c8e 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f  * immediately to
12c8f 20 50 32 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20   P2..    */.    
12c90 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
12c91 49 64 78 52 6f 77 69 64 28 70 43 72 73 72 2c 20  IdxRowid(pCrsr, 
12c92 26 76 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  &v);.    if( rc!
12c93 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
12c94 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64      goto abort_d
12c95 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20  ue_to_error;.   
12c96 20 7d 0a 20 20 20 20 69 66 28 20 76 3d 3d 52 20   }.    if( v==R 
12c97 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f  ){.      pc = pO
12c98 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20  p->p2 - 1;.     
12c99 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
12c9a 20 20 20 2f 2a 20 54 68 65 20 66 69 6e 61 6c 20     /* The final 
12c9b 76 61 72 69 6e 74 20 6f 66 20 74 68 65 20 6b 65  varint of the ke
12c9c 79 20 69 73 20 64 69 66 66 65 72 65 6e 74 20 66  y is different f
12c9d 72 6f 6d 20 52 2e 20 20 50 75 73 68 20 69 74 20  rom R.  Push it 
12c9e 6f 6e 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20  onto.    ** the 
12c9f 73 74 61 63 6b 2e 20 20 28 54 68 65 20 72 65 63  stack.  (The rec
12ca0 6f 72 64 20 6e 75 6d 62 65 72 20 6f 66 20 61 6e  ord number of an
12ca1 20 65 6e 74 72 79 20 74 68 61 74 20 76 69 6f 6c   entry that viol
12ca2 61 74 65 73 20 61 20 55 4e 49 51 55 45 0a 20 20  ates a UNIQUE.  
12ca3 20 20 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 2e    ** constraint.
12ca4 29 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 54 6f  ).    */.    pTo
12ca5 73 2b 2b 3b 0a 20 20 20 20 70 54 6f 73 2d 3e 75  s++;.    pTos->u
12ca6 2e 69 20 3d 20 76 3b 0a 20 20 20 20 70 54 6f 73  .i = v;.    pTos
12ca7 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e  ->flags = MEM_In
12ca8 74 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  t;.  }.  break;.
12ca9 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f  }../* Opcode: No
12caa 74 45 78 69 73 74 73 20 50 31 20 50 32 20 2a 0a  tExists P1 P2 *.
12cab 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 74 6f  **.** Use the to
12cac 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20 61  p of the stack a
12cad 73 20 61 20 69 6e 74 65 67 65 72 20 6b 65 79 2e  s a integer key.
12cae 20 20 49 66 20 61 20 72 65 63 6f 72 64 20 77 69    If a record wi
12caf 74 68 20 74 68 61 74 20 6b 65 79 0a 2a 2a 20 64  th that key.** d
12cb0 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 69 6e  oes not exist in
12cb1 20 74 61 62 6c 65 20 6f 66 20 50 31 2c 20 74 68   table of P1, th
12cb2 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20 20  en jump to P2.  
12cb3 49 66 20 74 68 65 20 72 65 63 6f 72 64 0a 2a 2a  If the record.**
12cb4 20 64 6f 65 73 20 65 78 69 73 74 2c 20 74 68 65   does exist, the
12cb5 6e 20 66 61 6c 6c 20 74 68 72 75 2e 20 20 54 68  n fall thru.  Th
12cb6 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74  e cursor is left
12cb7 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65   pointing to the
12cb8 0a 2a 2a 20 72 65 63 6f 72 64 20 69 66 20 69 74  .** record if it
12cb9 20 65 78 69 73 74 73 2e 20 20 54 68 65 20 69 6e   exists.  The in
12cba 74 65 67 65 72 20 6b 65 79 20 69 73 20 70 6f 70  teger key is pop
12cbb 70 65 64 20 66 72 6f 6d 20 74 68 65 20 73 74 61  ped from the sta
12cbc 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69  ck..**.** The di
12cbd 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e  fference between
12cbe 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20   this operation 
12cbf 61 6e 64 20 4e 6f 74 46 6f 75 6e 64 20 69 73 20  and NotFound is 
12cc0 74 68 61 74 20 74 68 69 73 0a 2a 2a 20 6f 70 65  that this.** ope
12cc1 72 61 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74  ration assumes t
12cc2 68 65 20 6b 65 79 20 69 73 20 61 6e 20 69 6e 74  he key is an int
12cc3 65 67 65 72 20 61 6e 64 20 74 68 61 74 20 50 31  eger and that P1
12cc4 20 69 73 20 61 20 74 61 62 6c 65 20 77 68 65 72   is a table wher
12cc5 65 61 73 0a 2a 2a 20 4e 6f 74 46 6f 75 6e 64 20  eas.** NotFound 
12cc6 61 73 73 75 6d 65 73 20 6b 65 79 20 69 73 20 61  assumes key is a
12cc7 20 62 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74 65   blob constructe
12cc8 64 20 66 72 6f 6d 20 4d 61 6b 65 52 65 63 6f 72  d from MakeRecor
12cc9 64 20 61 6e 64 0a 2a 2a 20 50 31 20 69 73 20 61  d and.** P1 is a
12cca 6e 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 53  n index..**.** S
12ccb 65 65 20 61 6c 73 6f 3a 20 44 69 73 74 69 6e 63  ee also: Distinc
12ccc 74 2c 20 46 6f 75 6e 64 2c 20 4d 6f 76 65 54 6f  t, Found, MoveTo
12ccd 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 49 73 55 6e  , NotFound, IsUn
12cce 69 71 75 65 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ique.*/.case OP_
12ccf 4e 6f 74 45 78 69 73 74 73 3a 20 7b 20 20 20 20  NotExists: {    
12cd0 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a      /* no-push *
12cd1 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d  /.  int i = pOp-
12cd2 3e 70 31 3b 0a 20 20 43 75 72 73 6f 72 20 2a 70  >p1;.  Cursor *p
12cd3 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  C;.  BtCursor *p
12cd4 43 72 73 72 3b 0a 20 20 61 73 73 65 72 74 28 20  Crsr;.  assert( 
12cd5 70 54 6f 73 3e 3d 70 2d 3e 61 53 74 61 63 6b 20  pTos>=p->aStack 
12cd6 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d  );.  assert( i>=
12cd7 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f  0 && i<p->nCurso
12cd8 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  r );.  assert( p
12cd9 2d 3e 61 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b  ->apCsr[i]!=0 );
12cda 0a 20 20 69 66 28 20 28 70 43 72 73 72 20 3d 20  .  if( (pCrsr = 
12cdb 28 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69  (pC = p->apCsr[i
12cdc 5d 29 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30 20  ])->pCursor)!=0 
12cdd 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 73 3b 0a  ){.    int res;.
12cde 20 20 20 20 75 36 34 20 69 4b 65 79 3b 0a 20 20      u64 iKey;.  
12cdf 20 20 61 73 73 65 72 74 28 20 70 54 6f 73 2d 3e    assert( pTos->
12ce0 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20  flags & MEM_Int 
12ce1 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
12ce2 2d 3e 61 70 43 73 72 5b 69 5d 2d 3e 69 73 54 61  ->apCsr[i]->isTa
12ce3 62 6c 65 20 29 3b 0a 20 20 20 20 69 4b 65 79 20  ble );.    iKey 
12ce4 3d 20 69 6e 74 54 6f 4b 65 79 28 70 54 6f 73 2d  = intToKey(pTos-
12ce5 3e 75 2e 69 29 3b 0a 20 20 20 20 72 63 20 3d 20  >u.i);.    rc = 
12ce6 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65  sqlite3BtreeMove
12ce7 74 6f 28 70 43 72 73 72 2c 20 30 2c 20 69 4b 65  to(pCrsr, 0, iKe
12ce8 79 2c 20 30 2c 26 72 65 73 29 3b 0a 20 20 20 20  y, 0,&res);.    
12ce9 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20  pC->lastRowid = 
12cea 70 54 6f 73 2d 3e 75 2e 69 3b 0a 20 20 20 20 70  pTos->u.i;.    p
12ceb 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20  C->rowidIsValid 
12cec 3d 20 72 65 73 3d 3d 30 3b 0a 20 20 20 20 70 43  = res==0;.    pC
12ced 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20  ->nullRow = 0;. 
12cee 20 20 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74     pC->cacheStat
12cef 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45  us = CACHE_STALE
12cf0 3b 0a 20 20 20 20 2f 2a 20 72 65 73 20 6d 69 67  ;.    /* res mig
12cf1 68 74 20 62 65 20 75 6e 69 6e 69 74 69 61 6c 69  ht be uninitiali
12cf2 7a 65 64 20 69 66 20 72 63 21 3d 53 51 4c 49 54  zed if rc!=SQLIT
12cf3 45 5f 4f 4b 2e 20 20 42 75 74 20 69 66 20 72 63  E_OK.  But if rc
12cf4 21 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 20 20  !=SQLITE_OK.    
12cf5 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67 20 69 73  ** processing is
12cf6 20 61 62 6f 75 74 20 74 6f 20 61 62 6f 72 74 20   about to abort 
12cf7 73 6f 20 77 65 20 72 65 61 6c 6c 79 20 64 6f 20  so we really do 
12cf8 6e 6f 74 20 63 61 72 65 20 77 68 65 74 68 65 72  not care whether
12cf9 20 6f 72 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 74   or not.    ** t
12cfa 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6a 75 6d  he following jum
12cfb 70 20 69 73 20 74 61 6b 65 6e 2e 20 20 28 49 6e  p is taken.  (In
12cfc 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 64 6f   other words, do
12cfd 20 6e 6f 74 20 73 74 72 65 73 73 20 6f 76 65 72   not stress over
12cfe 0a 20 20 20 20 2a 2a 20 74 68 65 20 65 72 72 6f  .    ** the erro
12cff 72 20 74 68 61 74 20 76 61 6c 67 72 69 6e 64 20  r that valgrind 
12d00 73 6f 6d 65 74 69 6d 65 73 20 73 68 6f 77 73 20  sometimes shows 
12d01 6f 6e 20 74 68 65 20 6e 65 78 74 20 73 74 61 74  on the next stat
12d02 65 6d 65 6e 74 20 77 68 65 6e 0a 20 20 20 20 2a  ement when.    *
12d03 2a 20 72 75 6e 6e 69 6e 67 20 69 6f 65 72 72 2e  * running ioerr.
12d04 74 65 73 74 20 61 6e 64 20 73 69 6d 69 6c 61 72  test and similar
12d05 20 66 61 69 6c 75 72 65 2d 72 65 63 6f 76 65 72   failure-recover
12d06 79 20 74 65 73 74 20 73 63 72 69 70 74 73 2e 29  y test scripts.)
12d07 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 65 73 21   */.    if( res!
12d08 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d  =0 ){.      pc =
12d09 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20   pOp->p2 - 1;.  
12d0a 20 20 20 20 70 43 2d 3e 72 6f 77 69 64 49 73 56      pC->rowidIsV
12d0b 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  alid = 0;.    }.
12d0c 20 20 7d 0a 20 20 52 65 6c 65 61 73 65 28 70 54    }.  Release(pT
12d0d 6f 73 29 3b 0a 20 20 70 54 6f 73 2d 2d 3b 0a 20  os);.  pTos--;. 
12d0e 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
12d0f 63 6f 64 65 3a 20 53 65 71 75 65 6e 63 65 20 50  code: Sequence P
12d10 31 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 75 73 68  1 * *.**.** Push
12d11 20 61 6e 20 69 6e 74 65 67 65 72 20 6f 6e 74 6f   an integer onto
12d12 20 74 68 65 20 73 74 61 63 6b 20 77 68 69 63 68   the stack which
12d13 20 69 73 20 74 68 65 20 6e 65 78 74 20 61 76 61   is the next ava
12d14 69 6c 61 62 6c 65 0a 2a 2a 20 73 65 71 75 65 6e  ilable.** sequen
12d15 63 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 63 75  ce number for cu
12d16 72 73 6f 72 20 50 31 2e 20 20 54 68 65 20 73 65  rsor P1.  The se
12d17 71 75 65 6e 63 65 20 6e 75 6d 62 65 72 20 6f 6e  quence number on
12d18 20 74 68 65 0a 2a 2a 20 63 75 72 73 6f 72 20 69   the.** cursor i
12d19 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 61 66  s incremented af
12d1a 74 65 72 20 74 68 65 20 70 75 73 68 2e 0a 2a 2f  ter the push..*/
12d1b 0a 63 61 73 65 20 4f 50 5f 53 65 71 75 65 6e 63  .case OP_Sequenc
12d1c 65 3a 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70  e: {.  int i = p
12d1d 4f 70 2d 3e 70 31 3b 0a 20 20 61 73 73 65 72 74  Op->p1;.  assert
12d1e 28 20 70 54 6f 73 3e 3d 70 2d 3e 61 53 74 61 63  ( pTos>=p->aStac
12d1f 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  k );.  assert( i
12d20 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72  >=0 && i<p->nCur
12d21 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28  sor );.  assert(
12d22 20 70 2d 3e 61 70 43 73 72 5b 69 5d 21 3d 30 20   p->apCsr[i]!=0 
12d23 29 3b 0a 20 20 70 54 6f 73 2b 2b 3b 0a 20 20 70  );.  pTos++;.  p
12d24 54 6f 73 2d 3e 75 2e 69 20 3d 20 70 2d 3e 61 70  Tos->u.i = p->ap
12d25 43 73 72 5b 69 5d 2d 3e 73 65 71 43 6f 75 6e 74  Csr[i]->seqCount
12d26 2b 2b 3b 0a 20 20 70 54 6f 73 2d 3e 66 6c 61 67  ++;.  pTos->flag
12d27 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 62  s = MEM_Int;.  b
12d28 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a 20 4f 70 63  reak;.}.../* Opc
12d29 6f 64 65 3a 20 4e 65 77 52 6f 77 69 64 20 50 31  ode: NewRowid P1
12d2a 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 47 65 74 20   P2 *.**.** Get 
12d2b 61 20 6e 65 77 20 69 6e 74 65 67 65 72 20 72 65  a new integer re
12d2c 63 6f 72 64 20 6e 75 6d 62 65 72 20 28 61 2e 6b  cord number (a.k
12d2d 2e 61 20 22 72 6f 77 69 64 22 29 20 75 73 65 64  .a "rowid") used
12d2e 20 61 73 20 74 68 65 20 6b 65 79 20 74 6f 20 61   as the key to a
12d2f 20 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 72   table..** The r
12d30 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73 20  ecord number is 
12d31 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20 75  not previously u
12d32 73 65 64 20 61 73 20 61 20 6b 65 79 20 69 6e 20  sed as a key in 
12d33 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  the database.** 
12d34 74 61 62 6c 65 20 74 68 61 74 20 63 75 72 73 6f  table that curso
12d35 72 20 50 31 20 70 6f 69 6e 74 73 20 74 6f 2e 20  r P1 points to. 
12d36 20 54 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20   The new record 
12d37 6e 75 6d 62 65 72 20 69 73 20 70 75 73 68 65 64  number is pushed
12d38 20 0a 2a 2a 20 6f 6e 74 6f 20 74 68 65 20 73 74   .** onto the st
12d39 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 32  ack..**.** If P2
12d3a 3e 30 20 74 68 65 6e 20 50 32 20 69 73 20 61 20  >0 then P2 is a 
12d3b 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68 61 74  memory cell that
12d3c 20 68 6f 6c 64 73 20 74 68 65 20 6c 61 72 67 65   holds the large
12d3d 73 74 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a  st previously.**
12d3e 20 67 65 6e 65 72 61 74 65 64 20 72 65 63 6f 72   generated recor
12d3f 64 20 6e 75 6d 62 65 72 2e 20 20 4e 6f 20 6e 65  d number.  No ne
12d40 77 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 73  w record numbers
12d41 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20   are allowed to 
12d42 62 65 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20  be less.** than 
12d43 74 68 69 73 20 76 61 6c 75 65 2e 20 20 57 68 65  this value.  Whe
12d44 6e 20 74 68 69 73 20 76 61 6c 75 65 20 72 65 61  n this value rea
12d45 63 68 65 73 20 69 74 73 20 6d 61 78 69 6d 75 6d  ches its maximum
12d46 2c 20 61 20 53 51 4c 49 54 45 5f 46 55 4c 4c 0a  , a SQLITE_FULL.
12d47 2a 2a 20 65 72 72 6f 72 20 69 73 20 67 65 6e 65  ** error is gene
12d48 72 61 74 65 64 2e 20 20 54 68 65 20 50 32 20 6d  rated.  The P2 m
12d49 65 6d 6f 72 79 20 63 65 6c 6c 20 69 73 20 75 70  emory cell is up
12d4a 64 61 74 65 64 20 77 69 74 68 20 74 68 65 20 67  dated with the g
12d4b 65 6e 65 72 61 74 65 64 0a 2a 2a 20 72 65 63 6f  enerated.** reco
12d4c 72 64 20 6e 75 6d 62 65 72 2e 20 20 54 68 69 73  rd number.  This
12d4d 20 50 32 20 6d 65 63 68 61 6e 69 73 6d 20 69 73   P2 mechanism is
12d4e 20 75 73 65 64 20 74 6f 20 68 65 6c 70 20 69 6d   used to help im
12d4f 70 6c 65 6d 65 6e 74 20 74 68 65 0a 2a 2a 20 41  plement the.** A
12d50 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 66 65 61  UTOINCREMENT fea
12d51 74 75 72 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  ture..*/.case OP
12d52 5f 4e 65 77 52 6f 77 69 64 3a 20 7b 0a 20 20 69  _NewRowid: {.  i
12d53 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a  nt i = pOp->p1;.
12d54 20 20 69 36 34 20 76 20 3d 20 30 3b 0a 20 20 43    i64 v = 0;.  C
12d55 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 61 73 73  ursor *pC;.  ass
12d56 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70  ert( i>=0 && i<p
12d57 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61  ->nCursor );.  a
12d58 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b  ssert( p->apCsr[
12d59 69 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28  i]!=0 );.  if( (
12d5a 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d  pC = p->apCsr[i]
12d5b 29 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 20 29 7b  )->pCursor==0 ){
12d5c 0a 20 20 20 20 2f 2a 20 54 68 65 20 7a 65 72 6f  .    /* The zero
12d5d 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20   initialization 
12d5e 61 62 6f 76 65 20 69 73 20 61 6c 6c 20 74 68 61  above is all tha
12d5f 74 20 69 73 20 6e 65 65 64 65 64 20 2a 2f 0a 20  t is needed */. 
12d60 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54   }else{.    /* T
12d61 68 65 20 6e 65 78 74 20 72 6f 77 69 64 20 6f 72  he next rowid or
12d62 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 28   record number (
12d63 64 69 66 66 65 72 65 6e 74 20 74 65 72 6d 73 20  different terms 
12d64 66 6f 72 20 74 68 65 20 73 61 6d 65 0a 20 20 20  for the same.   
12d65 20 2a 2a 20 74 68 69 6e 67 29 20 69 73 20 6f 62   ** thing) is ob
12d66 74 61 69 6e 65 64 20 69 6e 20 61 20 74 77 6f 2d  tained in a two-
12d67 73 74 65 70 20 61 6c 67 6f 72 69 74 68 6d 2e 0a  step algorithm..
12d68 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 69      **.    ** Fi
12d69 72 73 74 20 77 65 20 61 74 74 65 6d 70 74 20 74  rst we attempt t
12d6a 6f 20 66 69 6e 64 20 74 68 65 20 6c 61 72 67 65  o find the large
12d6b 73 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69  st existing rowi
12d6c 64 20 61 6e 64 20 61 64 64 20 6f 6e 65 0a 20 20  d and add one.  
12d6d 20 20 2a 2a 20 74 6f 20 74 68 61 74 2e 20 20 42    ** to that.  B
12d6e 75 74 20 69 66 20 74 68 65 20 6c 61 72 67 65 73  ut if the larges
12d6f 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69 64  t existing rowid
12d70 20 69 73 20 61 6c 72 65 61 64 79 20 74 68 65 20   is already the 
12d71 6d 61 78 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 70  maximum.    ** p
12d72 6f 73 69 74 69 76 65 20 69 6e 74 65 67 65 72 2c  ositive integer,
12d73 20 77 65 20 68 61 76 65 20 74 6f 20 66 61 6c 6c   we have to fall
12d74 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20   through to the 
12d75 73 65 63 6f 6e 64 0a 20 20 20 20 2a 2a 20 70 72  second.    ** pr
12d76 6f 62 61 62 69 6c 69 73 74 69 63 20 61 6c 67 6f  obabilistic algo
12d77 72 69 74 68 6d 0a 20 20 20 20 2a 2a 0a 20 20 20  rithm.    **.   
12d78 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61   ** The second a
12d79 6c 67 6f 72 69 74 68 6d 20 69 73 20 74 6f 20 73  lgorithm is to s
12d7a 65 6c 65 63 74 20 61 20 72 6f 77 69 64 20 61 74  elect a rowid at
12d7b 20 72 61 6e 64 6f 6d 20 61 6e 64 20 73 65 65 20   random and see 
12d7c 69 66 0a 20 20 20 20 2a 2a 20 69 74 20 61 6c 72  if.    ** it alr
12d7d 65 61 64 79 20 65 78 69 73 74 73 20 69 6e 20 74  eady exists in t
12d7e 68 65 20 74 61 62 6c 65 2e 20 20 49 66 20 69 74  he table.  If it
12d7f 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c   does not exist,
12d80 20 77 65 20 68 61 76 65 0a 20 20 20 20 2a 2a 20   we have.    ** 
12d81 73 75 63 63 65 65 64 65 64 2e 20 20 49 66 20 74  succeeded.  If t
12d82 68 65 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 20  he random rowid 
12d83 64 6f 65 73 20 65 78 69 73 74 2c 20 77 65 20 73  does exist, we s
12d84 65 6c 65 63 74 20 61 20 6e 65 77 20 6f 6e 65 0a  elect a new one.
12d85 20 20 20 20 2a 2a 20 61 6e 64 20 74 72 79 20 61      ** and try a
12d86 67 61 69 6e 2c 20 75 70 20 74 6f 20 31 30 30 30  gain, up to 1000
12d87 20 74 69 6d 65 73 2e 0a 20 20 20 20 2a 2a 0a 20   times..    **. 
12d88 20 20 20 2a 2a 20 46 6f 72 20 61 20 74 61 62 6c     ** For a tabl
12d89 65 20 77 69 74 68 20 6c 65 73 73 20 74 68 61 6e  e with less than
12d8a 20 32 20 62 69 6c 6c 69 6f 6e 20 65 6e 74 72 69   2 billion entri
12d8b 65 73 2c 20 74 68 65 20 70 72 6f 62 61 62 69 6c  es, the probabil
12d8c 69 74 79 0a 20 20 20 20 2a 2a 20 6f 66 20 6e 6f  ity.    ** of no
12d8d 74 20 66 69 6e 64 69 6e 67 20 61 20 75 6e 75 73  t finding a unus
12d8e 65 64 20 72 6f 77 69 64 20 69 73 20 61 62 6f 75  ed rowid is abou
12d8f 74 20 31 2e 30 65 2d 33 30 30 2e 20 20 54 68 69  t 1.0e-300.  Thi
12d90 73 20 69 73 20 61 20 0a 20 20 20 20 2a 2a 20 6e  s is a .    ** n
12d91 6f 6e 2d 7a 65 72 6f 20 70 72 6f 62 61 62 69 6c  on-zero probabil
12d92 69 74 79 2c 20 62 75 74 20 69 74 20 69 73 20 73  ity, but it is s
12d93 74 69 6c 6c 20 76 61 6e 69 73 68 69 6e 67 6c 79  till vanishingly
12d94 20 73 6d 61 6c 6c 20 61 6e 64 20 73 68 6f 75 6c   small and shoul
12d95 64 0a 20 20 20 20 2a 2a 20 6e 65 76 65 72 20 63  d.    ** never c
12d96 61 75 73 65 20 61 20 70 72 6f 62 6c 65 6d 2e 20  ause a problem. 
12d97 20 59 6f 75 20 61 72 65 20 6d 75 63 68 2c 20 6d   You are much, m
12d98 75 63 68 20 6d 6f 72 65 20 6c 69 6b 65 6c 79 20  uch more likely 
12d99 74 6f 20 68 61 76 65 20 61 0a 20 20 20 20 2a 2a  to have a.    **
12d9a 20 68 61 72 64 77 61 72 65 20 66 61 69 6c 75 72   hardware failur
12d9b 65 20 74 68 61 6e 20 66 6f 72 20 74 68 69 73 20  e than for this 
12d9c 61 6c 67 6f 72 69 74 68 6d 20 74 6f 20 66 61 69  algorithm to fai
12d9d 6c 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  l..    **.    **
12d9e 20 54 68 65 20 61 6e 61 6c 79 73 69 73 20 69 6e   The analysis in
12d9f 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 70 61   the previous pa
12da0 72 61 67 72 61 70 68 20 61 73 73 75 6d 65 73 20  ragraph assumes 
12da1 74 68 61 74 20 79 6f 75 20 68 61 76 65 20 61 20  that you have a 
12da2 67 6f 6f 64 0a 20 20 20 20 2a 2a 20 73 6f 75 72  good.    ** sour
12da3 63 65 20 6f 66 20 72 61 6e 64 6f 6d 20 6e 75 6d  ce of random num
12da4 62 65 72 73 2e 20 20 49 73 20 61 20 6c 69 62 72  bers.  Is a libr
12da5 61 72 79 20 66 75 6e 63 74 69 6f 6e 20 6c 69 6b  ary function lik
12da6 65 20 6c 72 61 6e 64 34 38 28 29 0a 20 20 20 20  e lrand48().    
12da7 2a 2a 20 67 6f 6f 64 20 65 6e 6f 75 67 68 3f 20  ** good enough? 
12da8 20 4d 61 79 62 65 2e 20 4d 61 79 62 65 20 6e 6f   Maybe. Maybe no
12da9 74 2e 20 49 74 27 73 20 68 61 72 64 20 74 6f 20  t. It's hard to 
12daa 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 74 68 65  know whether the
12dab 72 65 0a 20 20 20 20 2a 2a 20 6d 69 67 68 74 20  re.    ** might 
12dac 62 65 20 73 75 62 74 6c 65 20 62 75 67 73 20 69  be subtle bugs i
12dad 73 20 73 6f 6d 65 20 69 6d 70 6c 65 6d 65 6e 74  s some implement
12dae 61 74 69 6f 6e 73 20 6f 66 20 6c 72 61 6e 64 34  ations of lrand4
12daf 38 28 29 20 74 68 61 74 0a 20 20 20 20 2a 2a 20  8() that.    ** 
12db0 63 6f 75 6c 64 20 63 61 75 73 65 20 70 72 6f 62  could cause prob
12db1 6c 65 6d 73 2e 20 54 6f 20 61 76 6f 69 64 20 75  lems. To avoid u
12db2 6e 63 65 72 74 61 69 6e 74 79 2c 20 53 51 4c 69  ncertainty, SQLi
12db3 74 65 20 75 73 65 73 20 69 74 73 20 6f 77 6e 20  te uses its own 
12db4 0a 20 20 20 20 2a 2a 20 72 61 6e 64 6f 6d 20 6e  .    ** random n
12db5 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20  umber generator 
12db6 62 61 73 65 64 20 6f 6e 20 74 68 65 20 52 43 34  based on the RC4
12db7 20 61 6c 67 6f 72 69 74 68 6d 2e 0a 20 20 20 20   algorithm..    
12db8 2a 2a 0a 20 20 20 20 2a 2a 20 54 6f 20 70 72 6f  **.    ** To pro
12db9 6d 6f 74 65 20 6c 6f 63 61 6c 69 74 79 20 6f 66  mote locality of
12dba 20 72 65 66 65 72 65 6e 63 65 20 66 6f 72 20 72   reference for r
12dbb 65 70 65 74 69 74 69 76 65 20 69 6e 73 65 72 74  epetitive insert
12dbc 73 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 69  s, the.    ** fi
12dbd 72 73 74 20 66 65 77 20 61 74 74 65 6d 70 74 73  rst few attempts
12dbe 20 61 74 20 63 68 6f 73 69 6e 67 20 61 20 72 61   at chosing a ra
12dbf 6e 64 6f 6d 20 72 6f 77 69 64 20 70 69 63 6b 20  ndom rowid pick 
12dc0 76 61 6c 75 65 73 20 6a 75 73 74 20 61 20 6c 69  values just a li
12dc1 74 74 6c 65 0a 20 20 20 20 2a 2a 20 6c 61 72 67  ttle.    ** larg
12dc2 65 72 20 74 68 61 6e 20 74 68 65 20 70 72 65 76  er than the prev
12dc3 69 6f 75 73 20 72 6f 77 69 64 2e 20 20 54 68 69  ious rowid.  Thi
12dc4 73 20 68 61 73 20 62 65 65 6e 20 73 68 6f 77 6e  s has been shown
12dc5 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 6c 79 0a   experimentally.
12dc6 20 20 20 20 2a 2a 20 74 6f 20 64 6f 75 62 6c 65      ** to double
12dc7 20 74 68 65 20 73 70 65 65 64 20 6f 66 20 74 68   the speed of th
12dc8 65 20 43 4f 50 59 20 6f 70 65 72 61 74 69 6f 6e  e COPY operation
12dc9 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74  ..    */.    int
12dca 20 72 65 73 2c 20 72 78 3d 53 51 4c 49 54 45 5f   res, rx=SQLITE_
12dcb 4f 4b 2c 20 63 6e 74 3b 0a 20 20 20 20 69 36 34  OK, cnt;.    i64
12dcc 20 78 3b 0a 20 20 20 20 63 6e 74 20 3d 20 30 3b   x;.    cnt = 0;
12dcd 0a 20 20 20 20 69 66 28 20 28 73 71 6c 69 74 65  .    if( (sqlite
12dce 33 42 74 72 65 65 46 6c 61 67 73 28 70 43 2d 3e  3BtreeFlags(pC->
12dcf 70 43 75 72 73 6f 72 29 26 28 42 54 52 45 45 5f  pCursor)&(BTREE_
12dd0 49 4e 54 4b 45 59 7c 42 54 52 45 45 5f 5a 45 52  INTKEY|BTREE_ZER
12dd1 4f 44 41 54 41 29 29 20 21 3d 0a 20 20 20 20 20  ODATA)) !=.     
12dd2 20 20 20 20 20 42 54 52 45 45 5f 49 4e 54 4b 45       BTREE_INTKE
12dd3 59 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  Y ){.      rc = 
12dd4 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
12dd5 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  KPT;.      goto 
12dd6 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72  abort_due_to_err
12dd7 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  or;.    }.    as
12dd8 73 65 72 74 28 20 28 73 71 6c 69 74 65 33 42 74  sert( (sqlite3Bt
12dd9 72 65 65 46 6c 61 67 73 28 70 43 2d 3e 70 43 75  reeFlags(pC->pCu
12dda 72 73 6f 72 29 20 26 20 42 54 52 45 45 5f 49 4e  rsor) & BTREE_IN
12ddb 54 4b 45 59 29 21 3d 30 20 29 3b 0a 20 20 20 20  TKEY)!=0 );.    
12ddc 61 73 73 65 72 74 28 20 28 73 71 6c 69 74 65 33  assert( (sqlite3
12ddd 42 74 72 65 65 46 6c 61 67 73 28 70 43 2d 3e 70  BtreeFlags(pC->p
12dde 43 75 72 73 6f 72 29 20 26 20 42 54 52 45 45 5f  Cursor) & BTREE_
12ddf 5a 45 52 4f 44 41 54 41 29 3d 3d 30 20 29 3b 0a  ZERODATA)==0 );.
12de0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 33  .#ifdef SQLITE_3
12de1 32 42 49 54 5f 52 4f 57 49 44 0a 23 20 20 20 64  2BIT_ROWID.#   d
12de2 65 66 69 6e 65 20 4d 41 58 5f 52 4f 57 49 44 20  efine MAX_ROWID 
12de3 30 78 37 66 66 66 66 66 66 66 0a 23 65 6c 73 65  0x7fffffff.#else
12de4 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d  .    /* Some com
12de5 70 69 6c 65 72 73 20 63 6f 6d 70 6c 61 69 6e 20  pilers complain 
12de6 61 62 6f 75 74 20 63 6f 6e 73 74 61 6e 74 73 20  about constants 
12de7 6f 66 20 74 68 65 20 66 6f 72 6d 20 30 78 37 66  of the form 0x7f
12de8 66 66 66 66 66 66 66 66 66 66 66 66 66 66 2e 0a  ffffffffffffff..
12de9 20 20 20 20 2a 2a 20 4f 74 68 65 72 73 20 63 6f      ** Others co
12dea 6d 70 6c 61 69 6e 20 61 62 6f 75 74 20 30 78 37  mplain about 0x7
12deb 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66  ffffffffffffffff
12dec 4c 4c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69  LL.  The followi
12ded 6e 67 20 6d 61 63 72 6f 20 73 65 65 6d 73 0a 20  ng macro seems. 
12dee 20 20 20 2a 2a 20 74 6f 20 70 72 6f 76 69 64 65     ** to provide
12def 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 77 68   the constant wh
12df0 69 6c 65 20 6d 61 6b 69 6e 67 20 61 6c 6c 20 63  ile making all c
12df1 6f 6d 70 69 6c 65 72 73 20 68 61 70 70 79 2e 0a  ompilers happy..
12df2 20 20 20 20 2a 2f 0a 23 20 20 20 64 65 66 69 6e      */.#   defin
12df3 65 20 4d 41 58 5f 52 4f 57 49 44 20 20 28 20 28  e MAX_ROWID  ( (
12df4 28 28 75 36 34 29 30 78 37 66 66 66 66 66 66 66  ((u64)0x7fffffff
12df5 29 3c 3c 33 32 29 20 7c 20 28 75 36 34 29 30 78  )<<32) | (u64)0x
12df6 66 66 66 66 66 66 66 66 20 29 0a 23 65 6e 64 69  ffffffff ).#endi
12df7 66 0a 0a 20 20 20 20 69 66 28 20 21 70 43 2d 3e  f..    if( !pC->
12df8 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64 20 29  useRandomRowid )
12df9 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43 2d 3e  {.      if( pC->
12dfa 6e 65 78 74 52 6f 77 69 64 56 61 6c 69 64 20 29  nextRowidValid )
12dfb 7b 0a 20 20 20 20 20 20 20 20 76 20 3d 20 70 43  {.        v = pC
12dfc 2d 3e 6e 65 78 74 52 6f 77 69 64 3b 0a 20 20 20  ->nextRowid;.   
12dfd 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
12dfe 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
12dff 72 65 65 4c 61 73 74 28 70 43 2d 3e 70 43 75 72  reeLast(pC->pCur
12e00 73 6f 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20  sor, &res);.    
12e01 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
12e02 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
12e03 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75     goto abort_du
12e04 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20  e_to_error;.    
12e05 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
12e06 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20 20  ( res ){.       
12e07 20 20 20 76 20 3d 20 31 3b 0a 20 20 20 20 20 20     v = 1;.      
12e08 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
12e09 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b     sqlite3BtreeK
12e0a 65 79 53 69 7a 65 28 70 43 2d 3e 70 43 75 72 73  eySize(pC->pCurs
12e0b 6f 72 2c 20 26 76 29 3b 0a 20 20 20 20 20 20 20  or, &v);.       
12e0c 20 20 20 76 20 3d 20 6b 65 79 54 6f 49 6e 74 28     v = keyToInt(
12e0d 76 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  v);.          if
12e0e 28 20 76 3d 3d 4d 41 58 5f 52 4f 57 49 44 20 29  ( v==MAX_ROWID )
12e0f 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 43  {.            pC
12e10 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64  ->useRandomRowid
12e11 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20   = 1;.          
12e12 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
12e13 20 20 20 76 2b 2b 3b 0a 20 20 20 20 20 20 20 20     v++;.        
12e14 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
12e15 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53      }..#ifndef S
12e16 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49  QLITE_OMIT_AUTOI
12e17 4e 43 52 45 4d 45 4e 54 0a 20 20 20 20 20 20 69  NCREMENT.      i
12e18 66 28 20 70 4f 70 2d 3e 70 32 20 29 7b 0a 20 20  f( pOp->p2 ){.  
12e19 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b        Mem *pMem;
12e1a 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
12e1b 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f   pOp->p2>0 && pO
12e1c 70 2d 3e 70 32 3c 70 2d 3e 6e 4d 65 6d 20 29 3b  p->p2<p->nMem );
12e1d 20 20 2f 2a 20 50 32 20 69 73 20 61 20 76 61 6c    /* P2 is a val
12e1e 69 64 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 2a  id memory cell *
12e1f 2f 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 20 3d  /.        pMem =
12e20 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
12e21 32 5d 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  2];.        sqli
12e22 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65  te3VdbeMemIntege
12e23 72 69 66 79 28 70 4d 65 6d 29 3b 0a 20 20 20 20  rify(pMem);.    
12e24 20 20 20 20 61 73 73 65 72 74 28 20 28 70 4d 65      assert( (pMe
12e25 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49  m->flags & MEM_I
12e26 6e 74 29 21 3d 30 20 29 3b 20 20 2f 2a 20 6d 65  nt)!=0 );  /* me
12e27 6d 28 50 32 29 20 68 6f 6c 64 73 20 61 6e 20 69  m(P2) holds an i
12e28 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20  nteger */.      
12e29 20 20 69 66 28 20 70 4d 65 6d 2d 3e 75 2e 69 3d    if( pMem->u.i=
12e2a 3d 4d 41 58 5f 52 4f 57 49 44 20 7c 7c 20 70 43  =MAX_ROWID || pC
12e2b 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64  ->useRandomRowid
12e2c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63   ){.          rc
12e2d 20 3d 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a   = SQLITE_FULL;.
12e2e 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61            goto a
12e2f 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f  bort_due_to_erro
12e30 72 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  r;.        }.   
12e31 20 20 20 20 20 69 66 28 20 76 3c 70 4d 65 6d 2d       if( v<pMem-
12e32 3e 75 2e 69 2b 31 20 29 7b 0a 20 20 20 20 20 20  >u.i+1 ){.      
12e33 20 20 20 20 76 20 3d 20 70 4d 65 6d 2d 3e 75 2e      v = pMem->u.
12e34 69 20 2b 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  i + 1;.        }
12e35 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75  .        pMem->u
12e36 2e 69 20 3d 20 76 3b 0a 20 20 20 20 20 20 7d 0a  .i = v;.      }.
12e37 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 69 66  #endif..      if
12e38 28 20 76 3c 4d 41 58 5f 52 4f 57 49 44 20 29 7b  ( v<MAX_ROWID ){
12e39 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 6e 65 78  .        pC->nex
12e3a 74 52 6f 77 69 64 56 61 6c 69 64 20 3d 20 31 3b  tRowidValid = 1;
12e3b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 6e 65 78  .        pC->nex
12e3c 74 52 6f 77 69 64 20 3d 20 76 2b 31 3b 0a 20 20  tRowid = v+1;.  
12e3d 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
12e3e 20 20 20 70 43 2d 3e 6e 65 78 74 52 6f 77 69 64     pC->nextRowid
12e3f 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 20  Valid = 0;.     
12e40 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
12e41 20 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f   pC->useRandomRo
12e42 77 69 64 20 29 7b 0a 20 20 20 20 20 20 61 73 73  wid ){.      ass
12e43 65 72 74 28 20 70 4f 70 2d 3e 70 32 3d 3d 30 20  ert( pOp->p2==0 
12e44 29 3b 20 20 2f 2a 20 53 51 4c 49 54 45 5f 46 55  );  /* SQLITE_FU
12e45 4c 4c 20 6d 75 73 74 20 68 61 76 65 20 6f 63 63  LL must have occ
12e46 75 72 72 65 64 20 70 72 69 6f 72 20 74 6f 20 74  urred prior to t
12e47 68 69 73 20 2a 2f 0a 20 20 20 20 20 20 76 20 3d  his */.      v =
12e48 20 64 62 2d 3e 70 72 69 6f 72 4e 65 77 52 6f 77   db->priorNewRow
12e49 69 64 3b 0a 20 20 20 20 20 20 63 6e 74 20 3d 20  id;.      cnt = 
12e4a 30 3b 0a 20 20 20 20 20 20 64 6f 7b 0a 20 20 20  0;.      do{.   
12e4b 20 20 20 20 20 69 66 28 20 76 3d 3d 30 20 7c 7c       if( v==0 ||
12e4c 20 63 6e 74 3e 32 20 29 7b 0a 20 20 20 20 20 20   cnt>2 ){.      
12e4d 20 20 20 20 73 71 6c 69 74 65 33 52 61 6e 64 6f      sqlite3Rando
12e4e 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 76 29 2c  mness(sizeof(v),
12e4f 20 26 76 29 3b 0a 20 20 20 20 20 20 20 20 20 20   &v);.          
12e50 69 66 28 20 63 6e 74 3c 35 20 29 20 76 20 26 3d  if( cnt<5 ) v &=
12e51 20 30 78 66 66 66 66 66 66 3b 0a 20 20 20 20 20   0xffffff;.     
12e52 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
12e53 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61      unsigned cha
12e54 72 20 72 3b 0a 20 20 20 20 20 20 20 20 20 20 73  r r;.          s
12e55 71 6c 69 74 65 33 52 61 6e 64 6f 6d 6e 65 73 73  qlite3Randomness
12e56 28 31 2c 20 26 72 29 3b 0a 20 20 20 20 20 20 20  (1, &r);.       
12e57 20 20 20 76 20 2b 3d 20 72 20 2b 20 31 3b 0a 20     v += r + 1;. 
12e58 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
12e59 20 69 66 28 20 76 3d 3d 30 20 29 20 63 6f 6e 74   if( v==0 ) cont
12e5a 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 78 20  inue;.        x 
12e5b 3d 20 69 6e 74 54 6f 4b 65 79 28 76 29 3b 0a 20  = intToKey(v);. 
12e5c 20 20 20 20 20 20 20 72 78 20 3d 20 73 71 6c 69         rx = sqli
12e5d 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 70  te3BtreeMoveto(p
12e5e 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 28  C->pCursor, 0, (
12e5f 75 36 34 29 78 2c 20 30 2c 20 26 72 65 73 29 3b  u64)x, 0, &res);
12e60 0a 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a  .        cnt++;.
12e61 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20 63 6e        }while( cn
12e62 74 3c 31 30 30 30 20 26 26 20 72 78 3d 3d 53 51  t<1000 && rx==SQ
12e63 4c 49 54 45 5f 4f 4b 20 26 26 20 72 65 73 3d 3d  LITE_OK && res==
12e64 30 20 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 70  0 );.      db->p
12e65 72 69 6f 72 4e 65 77 52 6f 77 69 64 20 3d 20 76  riorNewRowid = v
12e66 3b 0a 20 20 20 20 20 20 69 66 28 20 72 78 3d 3d  ;.      if( rx==
12e67 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 65 73  SQLITE_OK && res
12e68 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ==0 ){.        r
12e69 63 20 3d 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b  c = SQLITE_FULL;
12e6a 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62  .        goto ab
12e6b 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72  ort_due_to_error
12e6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
12e6d 20 20 20 20 70 43 2d 3e 72 6f 77 69 64 49 73 56      pC->rowidIsV
12e6e 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 70 43  alid = 0;.    pC
12e6f 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f  ->deferredMoveto
12e70 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d 3e 63 61   = 0;.    pC->ca
12e71 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48  cheStatus = CACH
12e72 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 70  E_STALE;.  }.  p
12e73 54 6f 73 2b 2b 3b 0a 20 20 70 54 6f 73 2d 3e 75  Tos++;.  pTos->u
12e74 2e 69 20 3d 20 76 3b 0a 20 20 70 54 6f 73 2d 3e  .i = v;.  pTos->
12e75 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
12e76 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
12e77 4f 70 63 6f 64 65 3a 20 49 6e 73 65 72 74 20 50  Opcode: Insert P
12e78 31 20 50 32 20 50 33 0a 2a 2a 0a 2a 2a 20 57 72  1 P2 P3.**.** Wr
12e79 69 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e 74  ite an entry int
12e7a 6f 20 74 68 65 20 74 61 62 6c 65 20 6f 66 20 63  o the table of c
12e7b 75 72 73 6f 72 20 50 31 2e 20 20 41 20 6e 65 77  ursor P1.  A new
12e7c 20 65 6e 74 72 79 20 69 73 0a 2a 2a 20 63 72 65   entry is.** cre
12e7d 61 74 65 64 20 69 66 20 69 74 20 64 6f 65 73 6e  ated if it doesn
12e7e 27 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  't already exist
12e7f 20 6f 72 20 74 68 65 20 64 61 74 61 20 66 6f 72   or the data for
12e80 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20   an existing.** 
12e81 65 6e 74 72 79 20 69 73 20 6f 76 65 72 77 72 69  entry is overwri
12e82 74 74 65 6e 2e 20 20 54 68 65 20 64 61 74 61 20  tten.  The data 
12e83 69 73 20 74 68 65 20 76 61 6c 75 65 20 6f 6e 20  is the value on 
12e84 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 0a 2a  the top of the.*
12e85 2a 20 73 74 61 63 6b 2e 20 20 54 68 65 20 6b 65  * stack.  The ke
12e86 79 20 69 73 20 74 68 65 20 6e 65 78 74 20 76 61  y is the next va
12e87 6c 75 65 20 64 6f 77 6e 20 6f 6e 20 74 68 65 20  lue down on the 
12e88 73 74 61 63 6b 2e 20 20 54 68 65 20 6b 65 79 20  stack.  The key 
12e89 6d 75 73 74 0a 2a 2a 20 62 65 20 61 6e 20 69 6e  must.** be an in
12e8a 74 65 67 65 72 2e 20 20 54 68 65 20 73 74 61 63  teger.  The stac
12e8b 6b 20 69 73 20 70 6f 70 70 65 64 20 74 77 69 63  k is popped twic
12e8c 65 20 62 79 20 74 68 69 73 20 69 6e 73 74 72 75  e by this instru
12e8d 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ction..**.** If 
12e8e 74 68 65 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e  the OPFLAG_NCHAN
12e8f 47 45 20 66 6c 61 67 20 6f 66 20 50 32 20 69 73  GE flag of P2 is
12e90 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 72   set, then the r
12e91 6f 77 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 20  ow change count 
12e92 69 73 0a 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 65  is.** incremente
12e93 64 20 28 6f 74 68 65 72 77 69 73 65 20 6e 6f 74  d (otherwise not
12e94 29 2e 20 20 49 66 20 74 68 65 20 4f 50 46 4c 41  ).  If the OPFLA
12e95 47 5f 4c 41 53 54 52 4f 57 49 44 20 66 6c 61 67  G_LASTROWID flag
12e96 20 6f 66 20 50 32 20 69 73 20 73 65 74 2c 0a 2a   of P2 is set,.*
12e97 2a 20 74 68 65 6e 20 72 6f 77 69 64 20 69 73 20  * then rowid is 
12e98 73 74 6f 72 65 64 20 66 6f 72 20 73 75 62 73 65  stored for subse
12e99 71 75 65 6e 74 20 72 65 74 75 72 6e 20 62 79 20  quent return by 
12e9a 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6c  the.** sqlite3_l
12e9b 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64  ast_insert_rowid
12e9c 28 29 20 66 75 6e 63 74 69 6f 6e 20 28 6f 74 68  () function (oth
12e9d 65 72 77 69 73 65 20 69 74 27 73 20 75 6e 6d 6f  erwise it's unmo
12e9e 64 69 66 69 65 64 29 2e 0a 2a 2a 0a 2a 2a 20 50  dified)..**.** P
12e9f 61 72 61 6d 65 74 65 72 20 50 33 20 6d 61 79 20  arameter P3 may 
12ea0 70 6f 69 6e 74 20 74 6f 20 61 20 73 74 72 69 6e  point to a strin
12ea1 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  g containing the
12ea2 20 74 61 62 6c 65 2d 6e 61 6d 65 2c 20 6f 72 0a   table-name, or.
12ea3 2a 2a 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2e 20  ** may be NULL. 
12ea4 49 66 20 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c  If it is not NUL
12ea5 4c 2c 20 74 68 65 6e 20 74 68 65 20 75 70 64 61  L, then the upda
12ea6 74 65 2d 68 6f 6f 6b 20 0a 2a 2a 20 28 73 71 6c  te-hook .** (sql
12ea7 69 74 65 33 2e 78 55 70 64 61 74 65 43 61 6c 6c  ite3.xUpdateCall
12ea8 62 61 63 6b 29 20 69 73 20 69 6e 76 6f 6b 65 64  back) is invoked
12ea9 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 73 75 63   following a suc
12eaa 63 65 73 73 66 75 6c 20 69 6e 73 65 72 74 2e 0a  cessful insert..
12eab 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72  **.** This instr
12eac 75 63 74 69 6f 6e 20 6f 6e 6c 79 20 77 6f 72 6b  uction only work
12ead 73 20 6f 6e 20 74 61 62 6c 65 73 2e 20 20 54 68  s on tables.  Th
12eae 65 20 65 71 75 69 76 61 6c 65 6e 74 20 69 6e 73  e equivalent ins
12eaf 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20  truction.** for 
12eb0 69 6e 64 69 63 65 73 20 69 73 20 4f 50 5f 49 64  indices is OP_Id
12eb1 78 49 6e 73 65 72 74 2e 0a 2a 2f 0a 63 61 73 65  xInsert..*/.case
12eb2 20 4f 50 5f 49 6e 73 65 72 74 3a 20 7b 20 20 20   OP_Insert: {   
12eb3 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68        /* no-push
12eb4 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4e 6f 73 20   */.  Mem *pNos 
12eb5 3d 20 26 70 54 6f 73 5b 2d 31 5d 3b 0a 20 20 69  = &pTos[-1];.  i
12eb6 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a  nt i = pOp->p1;.
12eb7 20 20 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20    Cursor *pC;.  
12eb8 61 73 73 65 72 74 28 20 70 4e 6f 73 3e 3d 70 2d  assert( pNos>=p-
12eb9 3e 61 53 74 61 63 6b 20 29 3b 0a 20 20 61 73 73  >aStack );.  ass
12eba 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70  ert( i>=0 && i<p
12ebb 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61  ->nCursor );.  a
12ebc 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b  ssert( p->apCsr[
12ebd 69 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28  i]!=0 );.  if( (
12ebe 28 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69  (pC = p->apCsr[i
12ebf 5d 29 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 7c  ])->pCursor!=0 |
12ec0 7c 20 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c  | pC->pseudoTabl
12ec1 65 29 20 29 7b 0a 20 20 20 20 69 36 34 20 69 4b  e) ){.    i64 iK
12ec2 65 79 3b 20 20 20 2f 2a 20 54 68 65 20 69 6e 74  ey;   /* The int
12ec3 65 67 65 72 20 52 4f 57 49 44 20 6f 72 20 6b 65  eger ROWID or ke
12ec4 79 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64  y for the record
12ec5 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20   to be inserted 
12ec6 2a 2f 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20  */..    assert( 
12ec7 70 4e 6f 73 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pNos->flags & ME
12ec8 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 20 61 73 73  M_Int );.    ass
12ec9 65 72 74 28 20 70 43 2d 3e 69 73 54 61 62 6c 65  ert( pC->isTable
12eca 20 29 3b 0a 20 20 20 20 69 4b 65 79 20 3d 20 69   );.    iKey = i
12ecb 6e 74 54 6f 4b 65 79 28 70 4e 6f 73 2d 3e 75 2e  ntToKey(pNos->u.
12ecc 69 29 3b 0a 0a 20 20 20 20 69 66 28 20 70 4f 70  i);..    if( pOp
12ecd 2d 3e 70 32 20 26 20 4f 50 46 4c 41 47 5f 4e 43  ->p2 & OPFLAG_NC
12ece 48 41 4e 47 45 20 29 20 70 2d 3e 6e 43 68 61 6e  HANGE ) p->nChan
12ecf 67 65 2b 2b 3b 0a 20 20 20 20 69 66 28 20 70 4f  ge++;.    if( pO
12ed0 70 2d 3e 70 32 20 26 20 4f 50 46 4c 41 47 5f 4c  p->p2 & OPFLAG_L
12ed1 41 53 54 52 4f 57 49 44 20 29 20 64 62 2d 3e 6c  ASTROWID ) db->l
12ed2 61 73 74 52 6f 77 69 64 20 3d 20 70 4e 6f 73 2d  astRowid = pNos-
12ed3 3e 75 2e 69 3b 0a 20 20 20 20 69 66 28 20 70 43  >u.i;.    if( pC
12ed4 2d 3e 6e 65 78 74 52 6f 77 69 64 56 61 6c 69 64  ->nextRowidValid
12ed5 20 26 26 20 70 4e 6f 73 2d 3e 75 2e 69 3e 3d 70   && pNos->u.i>=p
12ed6 43 2d 3e 6e 65 78 74 52 6f 77 69 64 20 29 7b 0a  C->nextRowid ){.
12ed7 20 20 20 20 20 20 70 43 2d 3e 6e 65 78 74 52 6f        pC->nextRo
12ed8 77 69 64 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20  widValid = 0;.  
12ed9 20 20 7d 0a 20 20 20 20 69 66 28 20 70 54 6f 73    }.    if( pTos
12eda 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75  ->flags & MEM_Nu
12edb 6c 6c 20 29 7b 0a 20 20 20 20 20 20 70 54 6f 73  ll ){.      pTos
12edc 2d 3e 7a 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  ->z = 0;.      p
12edd 54 6f 73 2d 3e 6e 20 3d 20 30 3b 0a 20 20 20 20  Tos->n = 0;.    
12ede 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73  }else{.      ass
12edf 65 72 74 28 20 70 54 6f 73 2d 3e 66 6c 61 67 73  ert( pTos->flags
12ee0 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d   & (MEM_Blob|MEM
12ee1 5f 53 74 72 29 20 29 3b 0a 20 20 20 20 7d 0a 20  _Str) );.    }. 
12ee2 20 20 20 69 66 28 20 70 43 2d 3e 70 73 65 75 64     if( pC->pseud
12ee3 6f 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20  oTable ){.      
12ee4 73 71 6c 69 74 65 46 72 65 65 28 70 43 2d 3e 70  sqliteFree(pC->p
12ee5 44 61 74 61 29 3b 0a 20 20 20 20 20 20 70 43 2d  Data);.      pC-
12ee6 3e 69 4b 65 79 20 3d 20 69 4b 65 79 3b 0a 20 20  >iKey = iKey;.  
12ee7 20 20 20 20 70 43 2d 3e 6e 44 61 74 61 20 3d 20      pC->nData = 
12ee8 70 54 6f 73 2d 3e 6e 3b 0a 20 20 20 20 20 20 69  pTos->n;.      i
12ee9 66 28 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 26  f( pTos->flags &
12eea 20 4d 45 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 20   MEM_Dyn ){.    
12eeb 20 20 20 20 70 43 2d 3e 70 44 61 74 61 20 3d 20      pC->pData = 
12eec 70 54 6f 73 2d 3e 7a 3b 0a 20 20 20 20 20 20 20  pTos->z;.       
12eed 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 3d 20 4d   pTos->flags = M
12eee 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 7d  EM_Null;.      }
12eef 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 43  else{.        pC
12ef0 2d 3e 70 44 61 74 61 20 3d 20 73 71 6c 69 74 65  ->pData = sqlite
12ef1 4d 61 6c 6c 6f 63 52 61 77 28 20 70 43 2d 3e 6e  MallocRaw( pC->n
12ef2 44 61 74 61 2b 32 20 29 3b 0a 20 20 20 20 20 20  Data+2 );.      
12ef3 20 20 69 66 28 20 21 70 43 2d 3e 70 44 61 74 61    if( !pC->pData
12ef4 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a   ) goto no_mem;.
12ef5 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70          memcpy(p
12ef6 43 2d 3e 70 44 61 74 61 2c 20 70 54 6f 73 2d 3e  C->pData, pTos->
12ef7 7a 2c 20 70 43 2d 3e 6e 44 61 74 61 29 3b 0a 20  z, pC->nData);. 
12ef8 20 20 20 20 20 20 20 70 43 2d 3e 70 44 61 74 61         pC->pData
12ef9 5b 70 43 2d 3e 6e 44 61 74 61 5d 20 3d 20 30 3b  [pC->nData] = 0;
12efa 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 70 44 61  .        pC->pDa
12efb 74 61 5b 70 43 2d 3e 6e 44 61 74 61 2b 31 5d 20  ta[pC->nData+1] 
12efc 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 0;.      }.   
12efd 20 20 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d     pC->nullRow =
12efe 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   0;.    }else{. 
12eff 20 20 20 20 20 69 6e 74 20 6e 5a 65 72 6f 3b 0a       int nZero;.
12f00 20 20 20 20 20 20 69 66 28 20 70 54 6f 73 2d 3e        if( pTos->
12f01 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f  flags & MEM_Zero
12f02 20 29 7b 0a 20 20 20 20 20 20 20 20 6e 5a 65 72   ){.        nZer
12f03 6f 20 3d 20 70 54 6f 73 2d 3e 75 2e 69 3b 0a 20  o = pTos->u.i;. 
12f04 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
12f05 20 20 20 20 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20      nZero = 0;. 
12f06 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20       }.      rc 
12f07 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e  = sqlite3BtreeIn
12f08 73 65 72 74 28 70 43 2d 3e 70 43 75 72 73 6f 72  sert(pC->pCursor
12f09 2c 20 30 2c 20 69 4b 65 79 2c 0a 20 20 20 20 20  , 0, iKey,.     
12f0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12f0b 20 20 20 20 20 20 20 20 20 70 54 6f 73 2d 3e 7a           pTos->z
12f0c 2c 20 70 54 6f 73 2d 3e 6e 2c 20 6e 5a 65 72 6f  , pTos->n, nZero
12f0d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
12f0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12f0f 70 4f 70 2d 3e 70 32 20 26 20 4f 50 46 4c 41 47  pOp->p2 & OPFLAG
12f10 5f 41 50 50 45 4e 44 29 3b 0a 20 20 20 20 7d 0a  _APPEND);.    }.
12f11 20 20 20 20 0a 20 20 20 20 70 43 2d 3e 72 6f 77      .    pC->row
12f12 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20  idIsValid = 0;. 
12f13 20 20 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d     pC->deferredM
12f14 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 70  oveto = 0;.    p
12f15 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d  C->cacheStatus =
12f16 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 0a 20   CACHE_STALE;.. 
12f17 20 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65     /* Invoke the
12f18 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20 69 66 20   update-hook if 
12f19 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 20  required. */.   
12f1a 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
12f1b 4f 4b 20 26 26 20 64 62 2d 3e 78 55 70 64 61 74  OK && db->xUpdat
12f1c 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70  eCallback && pOp
12f1d 2d 3e 70 33 20 29 7b 0a 20 20 20 20 20 20 63 6f  ->p3 ){.      co
12f1e 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20  nst char *zDb = 
12f1f 64 62 2d 3e 61 44 62 5b 70 43 2d 3e 69 44 62 5d  db->aDb[pC->iDb]
12f20 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 63 6f  .zName;.      co
12f21 6e 73 74 20 63 68 61 72 20 2a 7a 54 62 6c 20 3d  nst char *zTbl =
12f22 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 20 20 20 20   pOp->p3;.      
12f23 69 6e 74 20 6f 70 20 3d 20 28 28 70 4f 70 2d 3e  int op = ((pOp->
12f24 70 32 20 26 20 4f 50 46 4c 41 47 5f 49 53 55 50  p2 & OPFLAG_ISUP
12f25 44 41 54 45 29 20 3f 20 53 51 4c 49 54 45 5f 55  DATE) ? SQLITE_U
12f26 50 44 41 54 45 20 3a 20 53 51 4c 49 54 45 5f 49  PDATE : SQLITE_I
12f27 4e 53 45 52 54 29 3b 0a 20 20 20 20 20 20 61 73  NSERT);.      as
12f28 73 65 72 74 28 20 70 43 2d 3e 69 73 54 61 62 6c  sert( pC->isTabl
12f29 65 20 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 78  e );.      db->x
12f2a 55 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 28 64  UpdateCallback(d
12f2b 62 2d 3e 70 55 70 64 61 74 65 41 72 67 2c 20 6f  b->pUpdateArg, o
12f2c 70 2c 20 7a 44 62 2c 20 7a 54 62 6c 2c 20 69 4b  p, zDb, zTbl, iK
12f2d 65 79 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  ey);.      asser
12f2e 74 28 20 70 43 2d 3e 69 44 62 3e 3d 30 20 29 3b  t( pC->iDb>=0 );
12f2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 6f 70  .    }.  }.  pop
12f30 53 74 61 63 6b 28 26 70 54 6f 73 2c 20 32 29 3b  Stack(&pTos, 2);
12f31 0a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ..  break;.}../*
12f32 20 4f 70 63 6f 64 65 3a 20 44 65 6c 65 74 65 20   Opcode: Delete 
12f33 50 31 20 50 32 20 50 33 0a 2a 2a 0a 2a 2a 20 44  P1 P2 P3.**.** D
12f34 65 6c 65 74 65 20 74 68 65 20 72 65 63 6f 72 64  elete the record
12f35 20 61 74 20 77 68 69 63 68 20 74 68 65 20 50 31   at which the P1
12f36 20 63 75 72 73 6f 72 20 69 73 20 63 75 72 72 65   cursor is curre
12f37 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 2e 0a 2a  ntly pointing..*
12f38 2a 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 20  *.** The cursor 
12f39 77 69 6c 6c 20 62 65 20 6c 65 66 74 20 70 6f 69  will be left poi
12f3a 6e 74 69 6e 67 20 61 74 20 65 69 74 68 65 72 20  nting at either 
12f3b 74 68 65 20 6e 65 78 74 20 6f 72 20 74 68 65 20  the next or the 
12f3c 70 72 65 76 69 6f 75 73 0a 2a 2a 20 72 65 63 6f  previous.** reco
12f3d 72 64 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e  rd in the table.
12f3e 20 49 66 20 69 74 20 69 73 20 6c 65 66 74 20 70   If it is left p
12f3f 6f 69 6e 74 69 6e 67 20 61 74 20 74 68 65 20 6e  ointing at the n
12f40 65 78 74 20 72 65 63 6f 72 64 2c 20 74 68 65 6e  ext record, then
12f41 0a 2a 2a 20 74 68 65 20 6e 65 78 74 20 4e 65 78  .** the next Nex
12f42 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 69  t instruction wi
12f43 6c 6c 20 62 65 20 61 20 6e 6f 2d 6f 70 2e 20 20  ll be a no-op.  
12f44 48 65 6e 63 65 20 69 74 20 69 73 20 4f 4b 20 74  Hence it is OK t
12f45 6f 20 64 65 6c 65 74 65 0a 2a 2a 20 61 20 72 65  o delete.** a re
12f46 63 6f 72 64 20 66 72 6f 6d 20 77 69 74 68 69 6e  cord from within
12f47 20 61 6e 20 4e 65 78 74 20 6c 6f 6f 70 2e 0a 2a   an Next loop..*
12f48 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f 50 46 4c  *.** If the OPFL
12f49 41 47 5f 4e 43 48 41 4e 47 45 20 66 6c 61 67 20  AG_NCHANGE flag 
12f4a 6f 66 20 50 32 20 69 73 20 73 65 74 2c 20 74 68  of P2 is set, th
12f4b 65 6e 20 74 68 65 20 72 6f 77 20 63 68 61 6e 67  en the row chang
12f4c 65 20 63 6f 75 6e 74 20 69 73 0a 2a 2a 20 69 6e  e count is.** in
12f4d 63 72 65 6d 65 6e 74 65 64 20 28 6f 74 68 65 72  cremented (other
12f4e 77 69 73 65 20 6e 6f 74 29 2e 0a 2a 2a 0a 2a 2a  wise not)..**.**
12f4f 20 49 66 20 50 31 20 69 73 20 61 20 70 73 65 75   If P1 is a pseu
12f50 64 6f 2d 74 61 62 6c 65 2c 20 74 68 65 6e 20 74  do-table, then t
12f51 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
12f52 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 63  is a no-op..*/.c
12f53 61 73 65 20 4f 50 5f 44 65 6c 65 74 65 3a 20 7b  ase OP_Delete: {
12f54 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75          /* no-pu
12f55 73 68 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20  sh */.  int i = 
12f56 70 4f 70 2d 3e 70 31 3b 0a 20 20 43 75 72 73 6f  pOp->p1;.  Curso
12f57 72 20 2a 70 43 3b 0a 20 20 61 73 73 65 72 74 28  r *pC;.  assert(
12f58 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43   i>=0 && i<p->nC
12f59 75 72 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20  ursor );.  pC = 
12f5a 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61  p->apCsr[i];.  a
12f5b 73 73 65 72 74 28 20 70 43 21 3d 30 20 29 3b 0a  ssert( pC!=0 );.
12f5c 20 20 69 66 28 20 70 43 2d 3e 70 43 75 72 73 6f    if( pC->pCurso
12f5d 72 21 3d 30 20 29 7b 0a 20 20 20 20 69 36 34 20  r!=0 ){.    i64 
12f5e 69 4b 65 79 3b 0a 0a 20 20 20 20 2f 2a 20 49 66  iKey;..    /* If
12f5f 20 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b   the update-hook
12f60 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64   will be invoked
12f61 2c 20 73 65 74 20 69 4b 65 79 20 74 6f 20 74 68  , set iKey to th
12f62 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 0a 20  e rowid of the. 
12f63 20 20 20 2a 2a 20 72 6f 77 20 62 65 69 6e 67 20     ** row being 
12f64 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a 2f 0a  deleted..    */.
12f65 20 20 20 20 69 66 28 20 64 62 2d 3e 78 55 70 64      if( db->xUpd
12f66 61 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70  ateCallback && p
12f67 4f 70 2d 3e 70 33 20 29 7b 0a 20 20 20 20 20 20  Op->p3 ){.      
12f68 61 73 73 65 72 74 28 20 70 43 2d 3e 69 73 54 61  assert( pC->isTa
12f69 62 6c 65 20 29 3b 0a 20 20 20 20 20 20 69 66 28  ble );.      if(
12f6a 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69   pC->rowidIsVali
12f6b 64 20 29 7b 0a 20 20 20 20 20 20 20 20 69 4b 65  d ){.        iKe
12f6c 79 20 3d 20 70 43 2d 3e 6c 61 73 74 52 6f 77 69  y = pC->lastRowi
12f6d 64 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  d;.      }else{.
12f6e 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
12f6f 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65  ite3BtreeKeySize
12f70 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 69  (pC->pCursor, &i
12f71 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 69 66  Key);.        if
12f72 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20  ( rc ){.        
12f73 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65    goto abort_due
12f74 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20  _to_error;.     
12f75 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 4b 65     }.        iKe
12f76 79 20 3d 20 6b 65 79 54 6f 49 6e 74 28 69 4b 65  y = keyToInt(iKe
12f77 79 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  y);.      }.    
12f78 7d 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  }..    rc = sqli
12f79 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76  te3VdbeCursorMov
12f7a 65 74 6f 28 70 43 29 3b 0a 20 20 20 20 69 66 28  eto(pC);.    if(
12f7b 20 72 63 20 29 20 67 6f 74 6f 20 61 62 6f 72 74   rc ) goto abort
12f7c 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20  _due_to_error;. 
12f7d 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
12f7e 74 72 65 65 44 65 6c 65 74 65 28 70 43 2d 3e 70  treeDelete(pC->p
12f7f 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 70 43 2d  Cursor);.    pC-
12f80 3e 6e 65 78 74 52 6f 77 69 64 56 61 6c 69 64 20  >nextRowidValid 
12f81 3d 20 30 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63  = 0;.    pC->cac
12f82 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45  heStatus = CACHE
12f83 5f 53 54 41 4c 45 3b 0a 0a 20 20 20 20 2f 2a 20  _STALE;..    /* 
12f84 49 6e 76 6f 6b 65 20 74 68 65 20 75 70 64 61 74  Invoke the updat
12f85 65 2d 68 6f 6f 6b 20 69 66 20 72 65 71 75 69 72  e-hook if requir
12f86 65 64 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 72  ed. */.    if( r
12f87 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
12f88 64 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62  db->xUpdateCallb
12f89 61 63 6b 20 26 26 20 70 4f 70 2d 3e 70 33 20 29  ack && pOp->p3 )
12f8a 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68  {.      const ch
12f8b 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44  ar *zDb = db->aD
12f8c 62 5b 70 43 2d 3e 69 44 62 5d 2e 7a 4e 61 6d 65  b[pC->iDb].zName
12f8d 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68  ;.      const ch
12f8e 61 72 20 2a 7a 54 62 6c 20 3d 20 70 4f 70 2d 3e  ar *zTbl = pOp->
12f8f 70 33 3b 0a 20 20 20 20 20 20 64 62 2d 3e 78 55  p3;.      db->xU
12f90 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 28 64 62  pdateCallback(db
12f91 2d 3e 70 55 70 64 61 74 65 41 72 67 2c 20 53 51  ->pUpdateArg, SQ
12f92 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20 7a 44 62  LITE_DELETE, zDb
12f93 2c 20 7a 54 62 6c 2c 20 69 4b 65 79 29 3b 0a 20  , zTbl, iKey);. 
12f94 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d       assert( pC-
12f95 3e 69 44 62 3e 3d 30 20 29 3b 0a 20 20 20 20 7d  >iDb>=0 );.    }
12f96 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e  .  }.  if( pOp->
12f97 70 32 20 26 20 4f 50 46 4c 41 47 5f 4e 43 48 41  p2 & OPFLAG_NCHA
12f98 4e 47 45 20 29 20 70 2d 3e 6e 43 68 61 6e 67 65  NGE ) p->nChange
12f99 2b 2b 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  ++;.  break;.}..
12f9a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 73 65 74  /* Opcode: Reset
12f9b 43 6f 75 6e 74 20 50 31 20 2a 20 2a 0a 2a 2a 0a  Count P1 * *.**.
12f9c 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 72  ** This opcode r
12f9d 65 73 65 74 73 20 74 68 65 20 56 4d 73 20 69 6e  esets the VMs in
12f9e 74 65 72 6e 61 6c 20 63 68 61 6e 67 65 20 63 6f  ternal change co
12f9f 75 6e 74 65 72 20 74 6f 20 30 2e 20 49 66 20 50  unter to 0. If P
12fa0 31 20 69 73 20 74 72 75 65 2c 0a 2a 2a 20 74 68  1 is true,.** th
12fa1 65 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  en the value of 
12fa2 74 68 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74  the change count
12fa3 65 72 20 69 73 20 63 6f 70 69 65 64 20 74 6f 20  er is copied to 
12fa4 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e  the database han
12fa5 64 6c 65 0a 2a 2a 20 63 68 61 6e 67 65 20 63 6f  dle.** change co
12fa6 75 6e 74 65 72 20 28 72 65 74 75 72 6e 65 64 20  unter (returned 
12fa7 62 79 20 73 75 62 73 65 71 75 65 6e 74 20 63 61  by subsequent ca
12fa8 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 63  lls to sqlite3_c
12fa9 68 61 6e 67 65 73 28 29 29 0a 2a 2a 20 62 65 66  hanges()).** bef
12faa 6f 72 65 20 69 74 20 69 73 20 72 65 73 65 74 2e  ore it is reset.
12fab 20 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79   This is used by
12fac 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d   trigger program
12fad 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65  s..*/.case OP_Re
12fae 73 65 74 43 6f 75 6e 74 3a 20 7b 20 20 20 20 20  setCount: {     
12faf 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f     /* no-push */
12fb0 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 20 29  .  if( pOp->p1 )
12fb1 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
12fb2 65 53 65 74 43 68 61 6e 67 65 73 28 64 62 2c 20  eSetChanges(db, 
12fb3 70 2d 3e 6e 43 68 61 6e 67 65 29 3b 0a 20 20 7d  p->nChange);.  }
12fb4 0a 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20  .  p->nChange = 
12fb5 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  0;.  break;.}../
12fb6 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 44 61 74  * Opcode: RowDat
12fb7 61 20 50 31 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50  a P1 * *.**.** P
12fb8 75 73 68 20 6f 6e 74 6f 20 74 68 65 20 73 74 61  ush onto the sta
12fb9 63 6b 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20  ck the complete 
12fba 72 6f 77 20 64 61 74 61 20 66 6f 72 20 63 75 72  row data for cur
12fbb 73 6f 72 20 50 31 2e 0a 2a 2a 20 54 68 65 72 65  sor P1..** There
12fbc 20 69 73 20 6e 6f 20 69 6e 74 65 72 70 72 65 74   is no interpret
12fbd 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 61 74  ation of the dat
12fbe 61 2e 20 20 49 74 20 69 73 20 6a 75 73 74 20 63  a.  It is just c
12fbf 6f 70 69 65 64 0a 2a 2a 20 6f 6e 74 6f 20 74 68  opied.** onto th
12fc0 65 20 73 74 61 63 6b 20 65 78 61 63 74 6c 79 20  e stack exactly 
12fc1 61 73 20 69 74 20 69 73 20 66 6f 75 6e 64 20 69  as it is found i
12fc2 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
12fc3 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ile..**.** If th
12fc4 65 20 63 75 72 73 6f 72 20 69 73 20 6e 6f 74 20  e cursor is not 
12fc5 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 76 61  pointing to a va
12fc6 6c 69 64 20 72 6f 77 2c 20 61 20 4e 55 4c 4c 20  lid row, a NULL 
12fc7 69 73 20 70 75 73 68 65 64 0a 2a 2a 20 6f 6e 74  is pushed.** ont
12fc8 6f 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2f 0a  o the stack..*/.
12fc9 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 4b 65  /* Opcode: RowKe
12fca 79 20 50 31 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50  y P1 * *.**.** P
12fcb 75 73 68 20 6f 6e 74 6f 20 74 68 65 20 73 74 61  ush onto the sta
12fcc 63 6b 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20  ck the complete 
12fcd 72 6f 77 20 6b 65 79 20 66 6f 72 20 63 75 72 73  row key for curs
12fce 6f 72 20 50 31 2e 0a 2a 2a 20 54 68 65 72 65 20  or P1..** There 
12fcf 69 73 20 6e 6f 20 69 6e 74 65 72 70 72 65 74 61  is no interpreta
12fd0 74 69 6f 6e 20 6f 66 20 74 68 65 20 6b 65 79 2e  tion of the key.
12fd1 20 20 49 74 20 69 73 20 6a 75 73 74 20 63 6f 70    It is just cop
12fd2 69 65 64 0a 2a 2a 20 6f 6e 74 6f 20 74 68 65 20  ied.** onto the 
12fd3 73 74 61 63 6b 20 65 78 61 63 74 6c 79 20 61 73  stack exactly as
12fd4 20 69 74 20 69 73 20 66 6f 75 6e 64 20 69 6e 20   it is found in 
12fd5 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
12fd6 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
12fd7 63 75 72 73 6f 72 20 69 73 20 6e 6f 74 20 70 6f  cursor is not po
12fd8 69 6e 74 69 6e 67 20 74 6f 20 61 20 76 61 6c 69  inting to a vali
12fd9 64 20 72 6f 77 2c 20 61 20 4e 55 4c 4c 20 69 73  d row, a NULL is
12fda 20 70 75 73 68 65 64 0a 2a 2a 20 6f 6e 74 6f 20   pushed.** onto 
12fdb 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2f 0a 63 61  the stack..*/.ca
12fdc 73 65 20 4f 50 5f 52 6f 77 4b 65 79 3a 0a 63 61  se OP_RowKey:.ca
12fdd 73 65 20 4f 50 5f 52 6f 77 44 61 74 61 3a 20 7b  se OP_RowData: {
12fde 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e  .  int i = pOp->
12fdf 70 31 3b 0a 20 20 43 75 72 73 6f 72 20 2a 70 43  p1;.  Cursor *pC
12fe0 3b 0a 20 20 75 33 32 20 6e 3b 0a 0a 20 20 2f 2a  ;.  u32 n;..  /*
12fe1 20 4e 6f 74 65 20 74 68 61 74 20 52 6f 77 4b 65   Note that RowKe
12fe2 79 20 61 6e 64 20 52 6f 77 44 61 74 61 20 61 72  y and RowData ar
12fe3 65 20 72 65 61 6c 6c 79 20 65 78 61 63 74 6c 79  e really exactly
12fe4 20 74 68 65 20 73 61 6d 65 20 69 6e 73 74 72 75   the same instru
12fe5 63 74 69 6f 6e 20 2a 2f 0a 20 20 70 54 6f 73 2b  ction */.  pTos+
12fe6 2b 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d  +;.  assert( i>=
12fe7 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f  0 && i<p->nCurso
12fe8 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61  r );.  pC = p->a
12fe9 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73 65 72  pCsr[i];.  asser
12fea 74 28 20 70 43 2d 3e 69 73 54 61 62 6c 65 20 7c  t( pC->isTable |
12feb 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f  | pOp->opcode==O
12fec 50 5f 52 6f 77 4b 65 79 20 29 3b 0a 20 20 61 73  P_RowKey );.  as
12fed 73 65 72 74 28 20 70 43 2d 3e 69 73 49 6e 64 65  sert( pC->isInde
12fee 78 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64 65  x || pOp->opcode
12fef 3d 3d 4f 50 5f 52 6f 77 44 61 74 61 20 29 3b 0a  ==OP_RowData );.
12ff0 20 20 61 73 73 65 72 74 28 20 70 43 21 3d 30 20    assert( pC!=0 
12ff1 29 3b 0a 20 20 69 66 28 20 70 43 2d 3e 6e 75 6c  );.  if( pC->nul
12ff2 6c 52 6f 77 20 29 7b 0a 20 20 20 20 70 54 6f 73  lRow ){.    pTos
12ff3 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75  ->flags = MEM_Nu
12ff4 6c 6c 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  ll;.  }else if( 
12ff5 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29  pC->pCursor!=0 )
12ff6 7b 0a 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a  {.    BtCursor *
12ff7 70 43 72 73 72 20 3d 20 70 43 2d 3e 70 43 75 72  pCrsr = pC->pCur
12ff8 73 6f 72 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  sor;.    rc = sq
12ff9 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d  lite3VdbeCursorM
12ffa 6f 76 65 74 6f 28 70 43 29 3b 0a 20 20 20 20 69  oveto(pC);.    i
12ffb 66 28 20 72 63 20 29 20 67 6f 74 6f 20 61 62 6f  f( rc ) goto abo
12ffc 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b  rt_due_to_error;
12ffd 0a 20 20 20 20 69 66 28 20 70 43 2d 3e 6e 75 6c  .    if( pC->nul
12ffe 6c 52 6f 77 20 29 7b 0a 20 20 20 20 20 20 70 54  lRow ){.      pT
12fff 6f 73 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  os->flags = MEM_
13000 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 62 72 65 61  Null;.      brea
13001 6b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  k;.    }else if(
13002 20 70 43 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a   pC->isIndex ){.
13003 20 20 20 20 20 20 69 36 34 20 6e 36 34 3b 0a 20        i64 n64;. 
13004 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70 43       assert( !pC
13005 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20  ->isTable );.   
13006 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b     sqlite3BtreeK
13007 65 79 53 69 7a 65 28 70 43 72 73 72 2c 20 26 6e  eySize(pCrsr, &n
13008 36 34 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e  64);.      if( n
13009 36 34 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45  64>SQLITE_MAX_LE
1300a 4e 47 54 48 20 29 7b 0a 20 20 20 20 20 20 20 20  NGTH ){.        
1300b 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20  goto too_big;.  
1300c 20 20 20 20 7d 0a 20 20 20 20 20 20 6e 20 3d 20      }.      n = 
1300d 6e 36 34 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  n64;.    }else{.
1300e 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
1300f 65 65 44 61 74 61 53 69 7a 65 28 70 43 72 73 72  eeDataSize(pCrsr
13010 2c 20 26 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20  , &n);.    }.   
13011 20 69 66 28 20 6e 3e 53 51 4c 49 54 45 5f 4d 41   if( n>SQLITE_MA
13012 58 5f 4c 45 4e 47 54 48 20 29 7b 0a 20 20 20 20  X_LENGTH ){.    
13013 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a    goto too_big;.
13014 20 20 20 20 7d 0a 20 20 20 20 70 54 6f 73 2d 3e      }.    pTos->
13015 6e 20 3d 20 6e 3b 0a 20 20 20 20 69 66 28 20 6e  n = n;.    if( n
13016 3c 3d 4e 42 46 53 20 29 7b 0a 20 20 20 20 20 20  <=NBFS ){.      
13017 70 54 6f 73 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pTos->flags = ME
13018 4d 5f 42 6c 6f 62 20 7c 20 4d 45 4d 5f 53 68 6f  M_Blob | MEM_Sho
13019 72 74 3b 0a 20 20 20 20 20 20 70 54 6f 73 2d 3e  rt;.      pTos->
1301a 7a 20 3d 20 70 54 6f 73 2d 3e 7a 53 68 6f 72 74  z = pTos->zShort
1301b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
1301c 20 20 20 63 68 61 72 20 2a 7a 20 3d 20 73 71 6c     char *z = sql
1301d 69 74 65 4d 61 6c 6c 6f 63 52 61 77 28 20 6e 20  iteMallocRaw( n 
1301e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 3d 3d  );.      if( z==
1301f 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b  0 ) goto no_mem;
13020 0a 20 20 20 20 20 20 70 54 6f 73 2d 3e 66 6c 61  .      pTos->fla
13021 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 20 7c 20  gs = MEM_Blob | 
13022 4d 45 4d 5f 44 79 6e 3b 0a 20 20 20 20 20 20 70  MEM_Dyn;.      p
13023 54 6f 73 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20  Tos->xDel = 0;. 
13024 20 20 20 20 20 70 54 6f 73 2d 3e 7a 20 3d 20 7a       pTos->z = z
13025 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
13026 70 43 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20  pC->isIndex ){. 
13027 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
13028 33 42 74 72 65 65 4b 65 79 28 70 43 72 73 72 2c  3BtreeKey(pCrsr,
13029 20 30 2c 20 6e 2c 20 70 54 6f 73 2d 3e 7a 29 3b   0, n, pTos->z);
1302a 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1302b 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1302c 72 65 65 44 61 74 61 28 70 43 72 73 72 2c 20 30  reeData(pCrsr, 0
1302d 2c 20 6e 2c 20 70 54 6f 73 2d 3e 7a 29 3b 0a 20  , n, pTos->z);. 
1302e 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28     }.  }else if(
1302f 20 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65   pC->pseudoTable
13030 20 29 7b 0a 20 20 20 20 70 54 6f 73 2d 3e 6e 20   ){.    pTos->n 
13031 3d 20 70 43 2d 3e 6e 44 61 74 61 3b 0a 20 20 20  = pC->nData;.   
13032 20 61 73 73 65 72 74 28 20 70 43 2d 3e 6e 44 61   assert( pC->nDa
13033 74 61 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 4c  ta<=SQLITE_MAX_L
13034 45 4e 47 54 48 20 29 3b 0a 20 20 20 20 70 54 6f  ENGTH );.    pTo
13035 73 2d 3e 7a 20 3d 20 70 43 2d 3e 70 44 61 74 61  s->z = pC->pData
13036 3b 0a 20 20 20 20 70 54 6f 73 2d 3e 66 6c 61 67  ;.    pTos->flag
13037 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d  s = MEM_Blob|MEM
13038 5f 45 70 68 65 6d 3b 0a 20 20 7d 65 6c 73 65 7b  _Ephem;.  }else{
13039 0a 20 20 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73  .    pTos->flags
1303a 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d   = MEM_Null;.  }
1303b 0a 20 20 70 54 6f 73 2d 3e 65 6e 63 20 3d 20 53  .  pTos->enc = S
1303c 51 4c 49 54 45 5f 55 54 46 38 3b 20 20 2f 2a 20  QLITE_UTF8;  /* 
1303d 49 6e 20 63 61 73 65 20 74 68 65 20 62 6c 6f 62  In case the blob
1303e 20 69 73 20 65 76 65 72 20 63 61 73 74 20 74 6f   is ever cast to
1303f 20 74 65 78 74 20 2a 2f 0a 20 20 62 72 65 61 6b   text */.  break
13040 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
13041 52 6f 77 69 64 20 50 31 20 2a 20 2a 0a 2a 2a 0a  Rowid P1 * *.**.
13042 2a 2a 20 50 75 73 68 20 6f 6e 74 6f 20 74 68 65  ** Push onto the
13043 20 73 74 61 63 6b 20 61 6e 20 69 6e 74 65 67 65   stack an intege
13044 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 6b  r which is the k
13045 65 79 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  ey of the table 
13046 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a 20 50 31  entry that.** P1
13047 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f   is currently po
13048 69 6e 74 20 74 6f 2e 0a 2a 2f 0a 63 61 73 65 20  int to..*/.case 
13049 4f 50 5f 52 6f 77 69 64 3a 20 7b 0a 20 20 69 6e  OP_Rowid: {.  in
1304a 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20  t i = pOp->p1;. 
1304b 20 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 69   Cursor *pC;.  i
1304c 36 34 20 76 3b 0a 0a 20 20 61 73 73 65 72 74 28  64 v;..  assert(
1304d 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43   i>=0 && i<p->nC
1304e 75 72 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20  ursor );.  pC = 
1304f 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61  p->apCsr[i];.  a
13050 73 73 65 72 74 28 20 70 43 21 3d 30 20 29 3b 0a  ssert( pC!=0 );.
13051 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
13052 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 70  beCursorMoveto(p
13053 43 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 67  C);.  if( rc ) g
13054 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
13055 5f 65 72 72 6f 72 3b 0a 20 20 70 54 6f 73 2b 2b  _error;.  pTos++
13056 3b 0a 20 20 69 66 28 20 70 43 2d 3e 72 6f 77 69  ;.  if( pC->rowi
13057 64 49 73 56 61 6c 69 64 20 29 7b 0a 20 20 20 20  dIsValid ){.    
13058 76 20 3d 20 70 43 2d 3e 6c 61 73 74 52 6f 77 69  v = pC->lastRowi
13059 64 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  d;.  }else if( p
1305a 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 20 29  C->pseudoTable )
1305b 7b 0a 20 20 20 20 76 20 3d 20 6b 65 79 54 6f 49  {.    v = keyToI
1305c 6e 74 28 70 43 2d 3e 69 4b 65 79 29 3b 0a 20 20  nt(pC->iKey);.  
1305d 7d 65 6c 73 65 20 69 66 28 20 70 43 2d 3e 6e 75  }else if( pC->nu
1305e 6c 6c 52 6f 77 20 7c 7c 20 70 43 2d 3e 70 43 75  llRow || pC->pCu
1305f 72 73 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 20 70  rsor==0 ){.    p
13060 54 6f 73 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Tos->flags = MEM
13061 5f 4e 75 6c 6c 3b 0a 20 20 20 20 62 72 65 61 6b  _Null;.    break
13062 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
13063 73 73 65 72 74 28 20 70 43 2d 3e 70 43 75 72 73  ssert( pC->pCurs
13064 6f 72 21 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c  or!=0 );.    sql
13065 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65  ite3BtreeKeySize
13066 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 76  (pC->pCursor, &v
13067 29 3b 0a 20 20 20 20 76 20 3d 20 6b 65 79 54 6f  );.    v = keyTo
13068 49 6e 74 28 76 29 3b 0a 20 20 7d 0a 20 20 70 54  Int(v);.  }.  pT
13069 6f 73 2d 3e 75 2e 69 20 3d 20 76 3b 0a 20 20 70  os->u.i = v;.  p
1306a 54 6f 73 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Tos->flags = MEM
1306b 5f 49 6e 74 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  _Int;.  break;.}
1306c 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 75 6c  ../* Opcode: Nul
1306d 6c 52 6f 77 20 50 31 20 2a 20 2a 0a 2a 2a 0a 2a  lRow P1 * *.**.*
1306e 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f  * Move the curso
1306f 72 20 50 31 20 74 6f 20 61 20 6e 75 6c 6c 20 72  r P1 to a null r
13070 6f 77 2e 20 20 41 6e 79 20 4f 50 5f 43 6f 6c 75  ow.  Any OP_Colu
13071 6d 6e 20 6f 70 65 72 61 74 69 6f 6e 73 0a 2a 2a  mn operations.**
13072 20 74 68 61 74 20 6f 63 63 75 72 20 77 68 69 6c   that occur whil
13073 65 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  e the cursor is 
13074 6f 6e 20 74 68 65 20 6e 75 6c 6c 20 72 6f 77 20  on the null row 
13075 77 69 6c 6c 20 61 6c 77 61 79 73 20 70 75 73 68  will always push
13076 20 0a 2a 2a 20 61 20 4e 55 4c 4c 20 6f 6e 74 6f   .** a NULL onto
13077 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2f 0a 63   the stack..*/.c
13078 61 73 65 20 4f 50 5f 4e 75 6c 6c 52 6f 77 3a 20  ase OP_NullRow: 
13079 7b 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70  {        /* no-p
1307a 75 73 68 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d  ush */.  int i =
1307b 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 43 75 72 73   pOp->p1;.  Curs
1307c 6f 72 20 2a 70 43 3b 0a 0a 20 20 61 73 73 65 72  or *pC;..  asser
1307d 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e  t( i>=0 && i<p->
1307e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 70 43 20  nCursor );.  pC 
1307f 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20  = p->apCsr[i];. 
13080 20 61 73 73 65 72 74 28 20 70 43 21 3d 30 20 29   assert( pC!=0 )
13081 3b 0a 20 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20  ;.  pC->nullRow 
13082 3d 20 31 3b 0a 20 20 70 43 2d 3e 72 6f 77 69 64  = 1;.  pC->rowid
13083 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 62  IsValid = 0;.  b
13084 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
13085 64 65 3a 20 4c 61 73 74 20 50 31 20 50 32 20 2a  de: Last P1 P2 *
13086 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20  .**.** The next 
13087 75 73 65 20 6f 66 20 74 68 65 20 52 6f 77 69 64  use of the Rowid
13088 20 6f 72 20 43 6f 6c 75 6d 6e 20 6f 72 20 4e 65   or Column or Ne
13089 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 66  xt instruction f
1308a 6f 72 20 50 31 20 0a 2a 2a 20 77 69 6c 6c 20 72  or P1 .** will r
1308b 65 66 65 72 20 74 6f 20 74 68 65 20 6c 61 73 74  efer to the last
1308c 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61   entry in the da
1308d 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20  tabase table or 
1308e 69 6e 64 65 78 2e 0a 2a 2a 20 49 66 20 74 68 65  index..** If the
1308f 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20   table or index 
13090 69 73 20 65 6d 70 74 79 20 61 6e 64 20 50 32 3e  is empty and P2>
13091 30 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d  0, then jump imm
13092 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a  ediately to P2..
13093 2a 2a 20 49 66 20 50 32 20 69 73 20 30 20 6f 72  ** If P2 is 0 or
13094 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72   if the table or
13095 20 69 6e 64 65 78 20 69 73 20 6e 6f 74 20 65 6d   index is not em
13096 70 74 79 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67  pty, fall throug
13097 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c  h.** to the foll
13098 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f  owing instructio
13099 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 61  n..*/.case OP_La
1309a 73 74 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20  st: {        /* 
1309b 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 69 6e 74  no-push */.  int
1309c 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20   i = pOp->p1;.  
1309d 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74  Cursor *pC;.  Bt
1309e 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 0a  Cursor *pCrsr;..
1309f 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26    assert( i>=0 &
130a0 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29  & i<p->nCursor )
130a1 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70 43 73  ;.  pC = p->apCs
130a2 72 5b 69 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  r[i];.  assert( 
130a3 70 43 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28  pC!=0 );.  if( (
130a4 70 43 72 73 72 20 3d 20 70 43 2d 3e 70 43 75 72  pCrsr = pC->pCur
130a5 73 6f 72 29 21 3d 30 20 29 7b 0a 20 20 20 20 69  sor)!=0 ){.    i
130a6 6e 74 20 72 65 73 3b 0a 20 20 20 20 72 63 20 3d  nt res;.    rc =
130a7 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73   sqlite3BtreeLas
130a8 74 28 70 43 72 73 72 2c 20 26 72 65 73 29 3b 0a  t(pCrsr, &res);.
130a9 20 20 20 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20      pC->nullRow 
130aa 3d 20 72 65 73 3b 0a 20 20 20 20 70 43 2d 3e 64  = res;.    pC->d
130ab 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20  eferredMoveto = 
130ac 30 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63 68 65  0;.    pC->cache
130ad 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53  Status = CACHE_S
130ae 54 41 4c 45 3b 0a 20 20 20 20 69 66 28 20 72 65  TALE;.    if( re
130af 73 20 26 26 20 70 4f 70 2d 3e 70 32 3e 30 20 29  s && pOp->p2>0 )
130b0 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70  {.      pc = pOp
130b1 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d 0a  ->p2 - 1;.    }.
130b2 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43 2d    }else{.    pC-
130b3 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20 20  >nullRow = 0;.  
130b4 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f  }.  break;.}.../
130b5 2a 20 4f 70 63 6f 64 65 3a 20 53 6f 72 74 20 50  * Opcode: Sort P
130b6 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  1 P2 *.**.** Thi
130b7 73 20 6f 70 63 6f 64 65 20 64 6f 65 73 20 65 78  s opcode does ex
130b8 61 63 74 6c 79 20 74 68 65 20 73 61 6d 65 20 74  actly the same t
130b9 68 69 6e 67 20 61 73 20 4f 50 5f 52 65 77 69 6e  hing as OP_Rewin
130ba 64 20 65 78 63 65 70 74 20 74 68 61 74 0a 2a 2a  d except that.**
130bb 20 69 74 20 69 6e 63 72 65 6d 65 6e 74 73 20 61   it increments a
130bc 6e 20 75 6e 64 6f 63 75 6d 65 6e 74 65 64 20 67  n undocumented g
130bd 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 75  lobal variable u
130be 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 2e  sed for testing.
130bf 0a 2a 2a 0a 2a 2a 20 53 6f 72 74 69 6e 67 20 69  .**.** Sorting i
130c0 73 20 61 63 63 6f 6d 70 6c 69 73 68 65 64 20 62  s accomplished b
130c1 79 20 77 72 69 74 69 6e 67 20 72 65 63 6f 72 64  y writing record
130c2 73 20 69 6e 74 6f 20 61 20 73 6f 72 74 69 6e 67  s into a sorting
130c3 20 69 6e 64 65 78 2c 0a 2a 2a 20 74 68 65 6e 20   index,.** then 
130c4 72 65 77 69 6e 64 69 6e 67 20 74 68 61 74 20 69  rewinding that i
130c5 6e 64 65 78 20 61 6e 64 20 70 6c 61 79 69 6e 67  ndex and playing
130c6 20 69 74 20 62 61 63 6b 20 66 72 6f 6d 20 62 65   it back from be
130c7 67 69 6e 6e 69 6e 67 20 74 6f 0a 2a 2a 20 65 6e  ginning to.** en
130c8 64 2e 20 20 57 65 20 75 73 65 20 74 68 65 20 4f  d.  We use the O
130c9 50 5f 53 6f 72 74 20 6f 70 63 6f 64 65 20 69 6e  P_Sort opcode in
130ca 73 74 65 61 64 20 6f 66 20 4f 50 5f 52 65 77 69  stead of OP_Rewi
130cb 6e 64 20 74 6f 20 64 6f 20 74 68 65 0a 2a 2a 20  nd to do the.** 
130cc 72 65 77 69 6e 64 69 6e 67 20 73 6f 20 74 68 61  rewinding so tha
130cd 74 20 74 68 65 20 67 6c 6f 62 61 6c 20 76 61 72  t the global var
130ce 69 61 62 6c 65 20 77 69 6c 6c 20 62 65 20 69 6e  iable will be in
130cf 63 72 65 6d 65 6e 74 65 64 20 61 6e 64 0a 2a 2a  cremented and.**
130d0 20 72 65 67 72 65 73 73 69 6f 6e 20 74 65 73 74   regression test
130d1 73 20 63 61 6e 20 64 65 74 65 72 6d 69 6e 65 20  s can determine 
130d2 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  whether or not t
130d3 68 65 20 6f 70 74 69 6d 69 7a 65 72 20 69 73 0a  he optimizer is.
130d4 2a 2a 20 63 6f 72 72 65 63 74 6c 79 20 6f 70 74  ** correctly opt
130d5 69 6d 69 7a 69 6e 67 20 6f 75 74 20 73 6f 72 74  imizing out sort
130d6 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 6f  s..*/.case OP_So
130d7 72 74 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20  rt: {        /* 
130d8 6e 6f 2d 70 75 73 68 20 2a 2f 0a 23 69 66 64 65  no-push */.#ifde
130d9 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20  f SQLITE_TEST.  
130da 73 71 6c 69 74 65 33 5f 73 6f 72 74 5f 63 6f 75  sqlite3_sort_cou
130db 6e 74 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 5f  nt++;.  sqlite3_
130dc 73 65 61 72 63 68 5f 63 6f 75 6e 74 2d 2d 3b 0a  search_count--;.
130dd 23 65 6e 64 69 66 0a 20 20 2f 2a 20 46 61 6c 6c  #endif.  /* Fall
130de 20 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 4f 50   through into OP
130df 5f 52 65 77 69 6e 64 20 2a 2f 0a 7d 0a 2f 2a 20  _Rewind */.}./* 
130e0 4f 70 63 6f 64 65 3a 20 52 65 77 69 6e 64 20 50  Opcode: Rewind P
130e1 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65  1 P2 *.**.** The
130e2 20 6e 65 78 74 20 75 73 65 20 6f 66 20 74 68 65   next use of the
130e3 20 52 6f 77 69 64 20 6f 72 20 43 6f 6c 75 6d 6e   Rowid or Column
130e4 20 6f 72 20 4e 65 78 74 20 69 6e 73 74 72 75 63   or Next instruc
130e5 74 69 6f 6e 20 66 6f 72 20 50 31 20 0a 2a 2a 20  tion for P1 .** 
130e6 77 69 6c 6c 20 72 65 66 65 72 20 74 6f 20 74 68  will refer to th
130e7 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 69 6e  e first entry in
130e8 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 61   the database ta
130e9 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 0a 2a 2a  ble or index..**
130ea 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72   If the table or
130eb 20 69 6e 64 65 78 20 69 73 20 65 6d 70 74 79 20   index is empty 
130ec 61 6e 64 20 50 32 3e 30 2c 20 74 68 65 6e 20 6a  and P2>0, then j
130ed 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  ump immediately 
130ee 74 6f 20 50 32 2e 0a 2a 2a 20 49 66 20 50 32 20  to P2..** If P2 
130ef 69 73 20 30 20 6f 72 20 69 66 20 74 68 65 20 74  is 0 or if the t
130f0 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 73  able or index is
130f1 20 6e 6f 74 20 65 6d 70 74 79 2c 20 66 61 6c 6c   not empty, fall
130f2 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 74   through.** to t
130f3 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73  he following ins
130f4 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73  truction..*/.cas
130f5 65 20 4f 50 5f 52 65 77 69 6e 64 3a 20 7b 20 20  e OP_Rewind: {  
130f6 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68        /* no-push
130f7 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f   */.  int i = pO
130f8 70 2d 3e 70 31 3b 0a 20 20 43 75 72 73 6f 72 20  p->p1;.  Cursor 
130f9 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20  *pC;.  BtCursor 
130fa 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 65  *pCrsr;.  int re
130fb 73 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e  s;..  assert( i>
130fc 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73  =0 && i<p->nCurs
130fd 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e  or );.  pC = p->
130fe 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73 65  apCsr[i];.  asse
130ff 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 20 69  rt( pC!=0 );.  i
13100 66 28 20 28 70 43 72 73 72 20 3d 20 70 43 2d 3e  f( (pCrsr = pC->
13101 70 43 75 72 73 6f 72 29 21 3d 30 20 29 7b 0a 20  pCursor)!=0 ){. 
13102 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
13103 74 72 65 65 46 69 72 73 74 28 70 43 72 73 72 2c  treeFirst(pCrsr,
13104 20 26 72 65 73 29 3b 0a 20 20 20 20 70 43 2d 3e   &res);.    pC->
13105 61 74 46 69 72 73 74 20 3d 20 72 65 73 3d 3d 30  atFirst = res==0
13106 3b 0a 20 20 20 20 70 43 2d 3e 64 65 66 65 72 72  ;.    pC->deferr
13107 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20  edMoveto = 0;.  
13108 20 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75    pC->cacheStatu
13109 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b  s = CACHE_STALE;
1310a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
1310b 73 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 70 43 2d  s = 1;.  }.  pC-
1310c 3e 6e 75 6c 6c 52 6f 77 20 3d 20 72 65 73 3b 0a  >nullRow = res;.
1310d 20 20 69 66 28 20 72 65 73 20 26 26 20 70 4f 70    if( res && pOp
1310e 2d 3e 70 32 3e 30 20 29 7b 0a 20 20 20 20 70 63  ->p2>0 ){.    pc
1310f 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
13110 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
13111 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 78 74 20  /* Opcode: Next 
13112 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 41 64  P1 P2 *.**.** Ad
13113 76 61 6e 63 65 20 63 75 72 73 6f 72 20 50 31 20  vance cursor P1 
13114 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74  so that it point
13115 73 20 74 6f 20 74 68 65 20 6e 65 78 74 20 6b 65  s to the next ke
13116 79 2f 64 61 74 61 20 70 61 69 72 20 69 6e 20 69  y/data pair in i
13117 74 73 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 20 69  ts.** table or i
13118 6e 64 65 78 2e 20 20 49 66 20 74 68 65 72 65 20  ndex.  If there 
13119 61 72 65 20 6e 6f 20 6d 6f 72 65 20 6b 65 79 2f  are no more key/
1311a 76 61 6c 75 65 20 70 61 69 72 73 20 74 68 65 6e  value pairs then
1311b 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a 2a   fall through.**
1311c 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   to the followin
1311d 67 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20  g instruction.  
1311e 42 75 74 20 69 66 20 74 68 65 20 63 75 72 73 6f  But if the curso
1311f 72 20 61 64 76 61 6e 63 65 20 77 61 73 20 73 75  r advance was su
13120 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 6a 75 6d  ccessful,.** jum
13121 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f  p immediately to
13122 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61   P2..**.** See a
13123 6c 73 6f 3a 20 50 72 65 76 0a 2a 2f 0a 2f 2a 20  lso: Prev.*/./* 
13124 4f 70 63 6f 64 65 3a 20 50 72 65 76 20 50 31 20  Opcode: Prev P1 
13125 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 42 61 63 6b 20  P2 *.**.** Back 
13126 75 70 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20  up cursor P1 so 
13127 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74  that it points t
13128 6f 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 6b  o the previous k
13129 65 79 2f 64 61 74 61 20 70 61 69 72 20 69 6e 20  ey/data pair in 
1312a 69 74 73 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 20  its.** table or 
1312b 69 6e 64 65 78 2e 20 20 49 66 20 74 68 65 72 65  index.  If there
1312c 20 69 73 20 6e 6f 20 70 72 65 76 69 6f 75 73 20   is no previous 
1312d 6b 65 79 2f 76 61 6c 75 65 20 70 61 69 72 73 20  key/value pairs 
1312e 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f 75 67  then fall throug
1312f 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c  h.** to the foll
13130 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f  owing instructio
13131 6e 2e 20 20 42 75 74 20 69 66 20 74 68 65 20 63  n.  But if the c
13132 75 72 73 6f 72 20 62 61 63 6b 75 70 20 77 61 73  ursor backup was
13133 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20   successful,.** 
13134 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79  jump immediately
13135 20 74 6f 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20   to P2..*/.case 
13136 4f 50 5f 50 72 65 76 3a 20 20 20 20 20 20 20 20  OP_Prev:        
13137 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a    /* no-push */.
13138 63 61 73 65 20 4f 50 5f 4e 65 78 74 3a 20 7b 20  case OP_Next: { 
13139 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73         /* no-pus
1313a 68 20 2a 2f 0a 20 20 43 75 72 73 6f 72 20 2a 70  h */.  Cursor *p
1313b 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  C;.  BtCursor *p
1313c 43 72 73 72 3b 0a 0a 20 20 43 48 45 43 4b 5f 46  Crsr;..  CHECK_F
1313d 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20  OR_INTERRUPT;.  
1313e 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e  assert( pOp->p1>
1313f 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d  =0 && pOp->p1<p-
13140 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 70 43  >nCursor );.  pC
13141 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d   = p->apCsr[pOp-
13142 3e 70 31 5d 3b 0a 20 20 69 66 28 20 70 43 3d 3d  >p1];.  if( pC==
13143 30 20 29 7b 0a 20 20 20 20 62 72 65 61 6b 3b 20  0 ){.    break; 
13144 20 2f 2a 20 53 65 65 20 74 69 63 6b 65 74 20 23   /* See ticket #
13145 32 32 37 33 20 2a 2f 0a 20 20 7d 0a 20 20 69 66  2273 */.  }.  if
13146 28 20 28 70 43 72 73 72 20 3d 20 70 43 2d 3e 70  ( (pCrsr = pC->p
13147 43 75 72 73 6f 72 29 21 3d 30 20 29 7b 0a 20 20  Cursor)!=0 ){.  
13148 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 69    int res;.    i
13149 66 28 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 29  f( pC->nullRow )
1314a 7b 0a 20 20 20 20 20 20 72 65 73 20 3d 20 31 3b  {.      res = 1;
1314b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1314c 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 64 65    assert( pC->de
1314d 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20  ferredMoveto==0 
1314e 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 4f  );.      rc = pO
1314f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4e 65  p->opcode==OP_Ne
13150 78 74 20 3f 20 73 71 6c 69 74 65 33 42 74 72 65  xt ? sqlite3Btre
13151 65 4e 65 78 74 28 70 43 72 73 72 2c 20 26 72 65  eNext(pCrsr, &re
13152 73 29 20 3a 0a 20 20 20 20 20 20 20 20 20 20 20  s) :.           
13153 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13154 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74         sqlite3Bt
13155 72 65 65 50 72 65 76 69 6f 75 73 28 70 43 72 73  reePrevious(pCrs
13156 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20 20  r, &res);.      
13157 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 72 65  pC->nullRow = re
13158 73 3b 0a 20 20 20 20 20 20 70 43 2d 3e 63 61 63  s;.      pC->cac
13159 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45  heStatus = CACHE
1315a 5f 53 54 41 4c 45 3b 0a 20 20 20 20 7d 0a 20 20  _STALE;.    }.  
1315b 20 20 69 66 28 20 72 65 73 3d 3d 30 20 29 7b 0a    if( res==0 ){.
1315c 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e        pc = pOp->
1315d 70 32 20 2d 20 31 3b 0a 23 69 66 64 65 66 20 53  p2 - 1;.#ifdef S
1315e 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20 20  QLITE_TEST.     
1315f 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f   sqlite3_search_
13160 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a  count++;.#endif.
13161 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
13162 20 20 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d     pC->nullRow =
13163 20 31 3b 0a 20 20 7d 0a 20 20 70 43 2d 3e 72 6f   1;.  }.  pC->ro
13164 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a  widIsValid = 0;.
13165 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
13166 70 63 6f 64 65 3a 20 49 64 78 49 6e 73 65 72 74  pcode: IdxInsert
13167 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 54   P1 P2 *.**.** T
13168 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74  he top of the st
13169 61 63 6b 20 68 6f 6c 64 73 20 61 20 53 51 4c 20  ack holds a SQL 
1316a 69 6e 64 65 78 20 6b 65 79 20 6d 61 64 65 20 75  index key made u
1316b 73 69 6e 67 20 65 69 74 68 65 72 20 74 68 65 0a  sing either the.
1316c 2a 2a 20 4d 61 6b 65 49 64 78 52 65 63 20 6f 72  ** MakeIdxRec or
1316d 20 4d 61 6b 65 52 65 63 6f 72 64 20 69 6e 73 74   MakeRecord inst
1316e 72 75 63 74 69 6f 6e 73 2e 20 20 54 68 69 73 20  ructions.  This 
1316f 6f 70 63 6f 64 65 20 77 72 69 74 65 73 20 74 68  opcode writes th
13170 61 74 20 6b 65 79 0a 2a 2a 20 69 6e 74 6f 20 74  at key.** into t
13171 68 65 20 69 6e 64 65 78 20 50 31 2e 20 20 44 61  he index P1.  Da
13172 74 61 20 66 6f 72 20 74 68 65 20 65 6e 74 72 79  ta for the entry
13173 20 69 73 20 6e 69 6c 2e 0a 2a 2a 0a 2a 2a 20 50   is nil..**.** P
13174 32 20 69 73 20 61 20 66 6c 61 67 20 74 68 61 74  2 is a flag that
13175 20 70 72 6f 76 69 64 65 73 20 61 20 68 69 6e 74   provides a hint
13176 20 74 6f 20 74 68 65 20 62 2d 74 72 65 65 20 6c   to the b-tree l
13177 61 79 65 72 20 74 68 61 74 20 74 68 69 73 0a 2a  ayer that this.*
13178 2a 20 69 6e 73 65 72 74 20 69 73 20 6c 69 6b 65  * insert is like
13179 6c 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70 65  ly to be an appe
1317a 6e 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  nd..**.** This i
1317b 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e 6c 79 20  nstruction only 
1317c 77 6f 72 6b 73 20 66 6f 72 20 69 6e 64 69 63 65  works for indice
1317d 73 2e 20 20 54 68 65 20 65 71 75 69 76 61 6c 65  s.  The equivale
1317e 6e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a  nt instruction.*
1317f 2a 20 66 6f 72 20 74 61 62 6c 65 73 20 69 73 20  * for tables is 
13180 4f 50 5f 49 6e 73 65 72 74 2e 0a 2a 2f 0a 63 61  OP_Insert..*/.ca
13181 73 65 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 3a  se OP_IdxInsert:
13182 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d   {        /* no-
13183 70 75 73 68 20 2a 2f 0a 20 20 69 6e 74 20 69 20  push */.  int i 
13184 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 43 75 72  = pOp->p1;.  Cur
13185 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72  sor *pC;.  BtCur
13186 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 61 73  sor *pCrsr;.  as
13187 73 65 72 74 28 20 70 54 6f 73 3e 3d 70 2d 3e 61  sert( pTos>=p->a
13188 53 74 61 63 6b 20 29 3b 0a 20 20 61 73 73 65 72  Stack );.  asser
13189 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e  t( i>=0 && i<p->
1318a 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 73  nCursor );.  ass
1318b 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69 5d  ert( p->apCsr[i]
1318c 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
1318d 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 26 20 4d   pTos->flags & M
1318e 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 69 66 28  EM_Blob );.  if(
1318f 20 28 70 43 72 73 72 20 3d 20 28 70 43 20 3d 20   (pCrsr = (pC = 
13190 70 2d 3e 61 70 43 73 72 5b 69 5d 29 2d 3e 70 43  p->apCsr[i])->pC
13191 75 72 73 6f 72 29 21 3d 30 20 29 7b 0a 20 20 20  ursor)!=0 ){.   
13192 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 73 54   assert( pC->isT
13193 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 72  able==0 );.    r
13194 63 20 3d 20 45 78 70 61 6e 64 42 6c 6f 62 28 70  c = ExpandBlob(p
13195 54 6f 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63  Tos);.    if( rc
13196 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
13197 20 20 20 20 20 69 6e 74 20 6e 4b 65 79 20 3d 20       int nKey = 
13198 70 54 6f 73 2d 3e 6e 3b 0a 20 20 20 20 20 20 63  pTos->n;.      c
13199 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4b 65 79 20  onst char *zKey 
1319a 3d 20 70 54 6f 73 2d 3e 7a 3b 0a 20 20 20 20 20  = pTos->z;.     
1319b 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1319c 65 65 49 6e 73 65 72 74 28 70 43 72 73 72 2c 20  eeInsert(pCrsr, 
1319d 7a 4b 65 79 2c 20 6e 4b 65 79 2c 20 22 22 2c 20  zKey, nKey, "", 
1319e 30 2c 20 30 2c 20 70 4f 70 2d 3e 70 32 29 3b 0a  0, 0, pOp->p2);.
1319f 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43        assert( pC
131a0 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f  ->deferredMoveto
131a1 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 43 2d  ==0 );.      pC-
131a2 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43  >cacheStatus = C
131a3 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20 20  ACHE_STALE;.    
131a4 7d 0a 20 20 7d 0a 20 20 52 65 6c 65 61 73 65 28  }.  }.  Release(
131a5 70 54 6f 73 29 3b 0a 20 20 70 54 6f 73 2d 2d 3b  pTos);.  pTos--;
131a6 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
131a7 4f 70 63 6f 64 65 3a 20 49 64 78 44 65 6c 65 74  Opcode: IdxDelet
131a8 65 20 50 31 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54  e P1 * *.**.** T
131a9 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74  he top of the st
131aa 61 63 6b 20 69 73 20 61 6e 20 69 6e 64 65 78 20  ack is an index 
131ab 6b 65 79 20 62 75 69 6c 74 20 75 73 69 6e 67 20  key built using 
131ac 74 68 65 20 65 69 74 68 65 72 20 74 68 65 0a 2a  the either the.*
131ad 2a 20 4d 61 6b 65 49 64 78 52 65 63 20 6f 72 20  * MakeIdxRec or 
131ae 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64  MakeRecord opcod
131af 65 73 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f  es..** This opco
131b0 64 65 20 72 65 6d 6f 76 65 73 20 74 68 61 74 20  de removes that 
131b1 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65 20 69  entry from the i
131b2 6e 64 65 78 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  ndex..*/.case OP
131b3 5f 49 64 78 44 65 6c 65 74 65 3a 20 7b 20 20 20  _IdxDelete: {   
131b4 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20       /* no-push 
131b5 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70  */.  int i = pOp
131b6 2d 3e 70 31 3b 0a 20 20 43 75 72 73 6f 72 20 2a  ->p1;.  Cursor *
131b7 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a  pC;.  BtCursor *
131b8 70 43 72 73 72 3b 0a 20 20 61 73 73 65 72 74 28  pCrsr;.  assert(
131b9 20 70 54 6f 73 3e 3d 70 2d 3e 61 53 74 61 63 6b   pTos>=p->aStack
131ba 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54   );.  assert( pT
131bb 6f 73 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  os->flags & MEM_
131bc 42 6c 6f 62 20 29 3b 0a 20 20 61 73 73 65 72 74  Blob );.  assert
131bd 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e  ( i>=0 && i<p->n
131be 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65  Cursor );.  asse
131bf 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69 5d 21  rt( p->apCsr[i]!
131c0 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70 43 72  =0 );.  if( (pCr
131c1 73 72 20 3d 20 28 70 43 20 3d 20 70 2d 3e 61 70  sr = (pC = p->ap
131c2 43 73 72 5b 69 5d 29 2d 3e 70 43 75 72 73 6f 72  Csr[i])->pCursor
131c3 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  )!=0 ){.    int 
131c4 72 65 73 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  res;.    rc = sq
131c5 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f  lite3BtreeMoveto
131c6 28 70 43 72 73 72 2c 20 70 54 6f 73 2d 3e 7a 2c  (pCrsr, pTos->z,
131c7 20 70 54 6f 73 2d 3e 6e 2c 20 30 2c 20 26 72 65   pTos->n, 0, &re
131c8 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  s);.    if( rc==
131c9 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 65 73  SQLITE_OK && res
131ca 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20  ==0 ){.      rc 
131cb 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65  = sqlite3BtreeDe
131cc 6c 65 74 65 28 70 43 72 73 72 29 3b 0a 20 20 20  lete(pCrsr);.   
131cd 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70   }.    assert( p
131ce 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74  C->deferredMovet
131cf 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 70 43 2d 3e  o==0 );.    pC->
131d0 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41  cacheStatus = CA
131d1 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a 20  CHE_STALE;.  }. 
131d2 20 52 65 6c 65 61 73 65 28 70 54 6f 73 29 3b 0a   Release(pTos);.
131d3 20 20 70 54 6f 73 2d 2d 3b 0a 20 20 62 72 65 61    pTos--;.  brea
131d4 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
131d5 20 49 64 78 52 6f 77 69 64 20 50 31 20 2a 20 2a   IdxRowid P1 * *
131d6 0a 2a 2a 0a 2a 2a 20 50 75 73 68 20 6f 6e 74 6f  .**.** Push onto
131d7 20 74 68 65 20 73 74 61 63 6b 20 61 6e 20 69 6e   the stack an in
131d8 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74  teger which is t
131d9 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e  he last entry in
131da 20 74 68 65 20 72 65 63 6f 72 64 20 61 74 0a 2a   the record at.*
131db 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  * the end of the
131dc 20 69 6e 64 65 78 20 6b 65 79 20 70 6f 69 6e 74   index key point
131dd 65 64 20 74 6f 20 62 79 20 63 75 72 73 6f 72 20  ed to by cursor 
131de 50 31 2e 20 20 54 68 69 73 20 69 6e 74 65 67 65  P1.  This intege
131df 72 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 74  r should be.** t
131e0 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20  he rowid of the 
131e1 74 61 62 6c 65 20 65 6e 74 72 79 20 74 6f 20 77  table entry to w
131e2 68 69 63 68 20 74 68 69 73 20 69 6e 64 65 78 20  hich this index 
131e3 65 6e 74 72 79 20 70 6f 69 6e 74 73 2e 0a 2a 2a  entry points..**
131e4 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 52 6f  .** See also: Ro
131e5 77 69 64 2c 20 4d 61 6b 65 49 64 78 52 65 63 2e  wid, MakeIdxRec.
131e6 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 52  .*/.case OP_IdxR
131e7 6f 77 69 64 3a 20 7b 0a 20 20 69 6e 74 20 69 20  owid: {.  int i 
131e8 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 42 74 43  = pOp->p1;.  BtC
131e9 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20  ursor *pCrsr;.  
131ea 43 75 72 73 6f 72 20 2a 70 43 3b 0a 0a 20 20 61  Cursor *pC;..  a
131eb 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69  ssert( i>=0 && i
131ec 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20  <p->nCursor );. 
131ed 20 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73   assert( p->apCs
131ee 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 70 54 6f  r[i]!=0 );.  pTo
131ef 73 2b 2b 3b 0a 20 20 70 54 6f 73 2d 3e 66 6c 61  s++;.  pTos->fla
131f0 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20  gs = MEM_Null;. 
131f1 20 69 66 28 20 28 70 43 72 73 72 20 3d 20 28 70   if( (pCrsr = (p
131f2 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29  C = p->apCsr[i])
131f3 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30 20 29 7b  ->pCursor)!=0 ){
131f4 0a 20 20 20 20 69 36 34 20 72 6f 77 69 64 3b 0a  .    i64 rowid;.
131f5 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d  .    assert( pC-
131f6 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d  >deferredMoveto=
131f7 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =0 );.    assert
131f8 28 20 70 43 2d 3e 69 73 54 61 62 6c 65 3d 3d 30  ( pC->isTable==0
131f9 20 29 3b 0a 20 20 20 20 69 66 28 20 70 43 2d 3e   );.    if( pC->
131fa 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 20  nullRow ){.     
131fb 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 3d 20 4d   pTos->flags = M
131fc 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 7d 65 6c  EM_Null;.    }el
131fd 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  se{.      rc = s
131fe 71 6c 69 74 65 33 56 64 62 65 49 64 78 52 6f 77  qlite3VdbeIdxRow
131ff 69 64 28 70 43 72 73 72 2c 20 26 72 6f 77 69 64  id(pCrsr, &rowid
13200 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
13201 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
13202 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74        goto abort
13203 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20  _due_to_error;. 
13204 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 54 6f       }.      pTo
13205 73 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49  s->flags = MEM_I
13206 6e 74 3b 0a 20 20 20 20 20 20 70 54 6f 73 2d 3e  nt;.      pTos->
13207 75 2e 69 20 3d 20 72 6f 77 69 64 3b 0a 20 20 20  u.i = rowid;.   
13208 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a   }.  }.  break;.
13209 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64  }../* Opcode: Id
1320a 78 47 54 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a  xGT P1 P2 *.**.*
1320b 2a 20 54 68 65 20 74 6f 70 20 6f 66 20 74 68 65  * The top of the
1320c 20 73 74 61 63 6b 20 69 73 20 61 6e 20 69 6e 64   stack is an ind
1320d 65 78 20 65 6e 74 72 79 20 74 68 61 74 20 6f 6d  ex entry that om
1320e 69 74 73 20 74 68 65 20 52 4f 57 49 44 2e 20 20  its the ROWID.  
1320f 43 6f 6d 70 61 72 65 0a 2a 2a 20 74 68 65 20 74  Compare.** the t
13210 6f 70 20 6f 66 20 73 74 61 63 6b 20 61 67 61 69  op of stack agai
13211 6e 73 74 20 74 68 65 20 69 6e 64 65 78 20 74 68  nst the index th
13212 61 74 20 50 31 20 69 73 20 63 75 72 72 65 6e 74  at P1 is current
13213 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 0a  ly pointing to..
13214 2a 2a 20 49 67 6e 6f 72 65 20 74 68 65 20 52 4f  ** Ignore the RO
13215 57 49 44 20 6f 6e 20 74 68 65 20 50 31 20 69 6e  WID on the P1 in
13216 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74  dex..**.** The t
13217 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20  op of the stack 
13218 6d 69 67 68 74 20 68 61 76 65 20 66 65 77 65 72  might have fewer
13219 20 63 6f 6c 75 6d 6e 73 20 74 68 61 74 20 50 31   columns that P1
1321a 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50  ..**.** If the P
1321b 31 20 69 6e 64 65 78 20 65 6e 74 72 79 20 69 73  1 index entry is
1321c 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68   greater than th
1321d 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61  e top of the sta
1321e 63 6b 0a 2a 2a 20 74 68 65 6e 20 6a 75 6d 70 20  ck.** then jump 
1321f 74 6f 20 50 32 2e 20 20 4f 74 68 65 72 77 69 73  to P2.  Otherwis
13220 65 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74  e fall through t
13221 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72  o the next instr
13222 75 63 74 69 6f 6e 2e 0a 2a 2a 20 49 6e 20 65 69  uction..** In ei
13223 74 68 65 72 20 63 61 73 65 2c 20 74 68 65 20 73  ther case, the s
13224 74 61 63 6b 20 69 73 20 70 6f 70 70 65 64 20 6f  tack is popped o
13225 6e 63 65 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64  nce..*/./* Opcod
13226 65 3a 20 49 64 78 47 45 20 50 31 20 50 32 20 50  e: IdxGE P1 P2 P
13227 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 6f 70 20  3.**.** The top 
13228 6f 66 20 74 68 65 20 73 74 61 63 6b 20 69 73 20  of the stack is 
13229 61 6e 20 69 6e 64 65 78 20 65 6e 74 72 79 20 74  an index entry t
1322a 68 61 74 20 6f 6d 69 74 73 20 74 68 65 20 52 4f  hat omits the RO
1322b 57 49 44 2e 20 20 43 6f 6d 70 61 72 65 0a 2a 2a  WID.  Compare.**
1322c 20 74 68 65 20 74 6f 70 20 6f 66 20 73 74 61 63   the top of stac
1322d 6b 20 61 67 61 69 6e 73 74 20 74 68 65 20 69 6e  k against the in
1322e 64 65 78 20 74 68 61 74 20 50 31 20 69 73 20 63  dex that P1 is c
1322f 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e  urrently pointin
13230 67 20 74 6f 2e 0a 2a 2a 20 49 67 6e 6f 72 65 20  g to..** Ignore 
13231 74 68 65 20 52 4f 57 49 44 20 6f 6e 20 74 68 65  the ROWID on the
13232 20 50 31 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a   P1 index..**.**
13233 20 49 66 20 74 68 65 20 50 31 20 69 6e 64 65 78   If the P1 index
13234 20 65 6e 74 72 79 20 69 73 20 67 72 65 61 74 65   entry is greate
13235 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20  r than or equal 
13236 74 6f 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68  to the top of th
13237 65 20 73 74 61 63 6b 0a 2a 2a 20 74 68 65 6e 20  e stack.** then 
13238 6a 75 6d 70 20 74 6f 20 50 32 2e 20 20 4f 74 68  jump to P2.  Oth
13239 65 72 77 69 73 65 20 66 61 6c 6c 20 74 68 72 6f  erwise fall thro
1323a 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ugh to the next 
1323b 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20  instruction..** 
1323c 49 6e 20 65 69 74 68 65 72 20 63 61 73 65 2c 20  In either case, 
1323d 74 68 65 20 73 74 61 63 6b 20 69 73 20 70 6f 70  the stack is pop
1323e 70 65 64 20 6f 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20  ped once..**.** 
1323f 49 66 20 50 33 20 69 73 20 74 68 65 20 22 2b 22  If P3 is the "+"
13240 20 73 74 72 69 6e 67 20 28 6f 72 20 61 6e 79 20   string (or any 
13241 6f 74 68 65 72 20 6e 6f 6e 2d 4e 55 4c 4c 20 73  other non-NULL s
13242 74 72 69 6e 67 29 20 74 68 65 6e 20 74 68 65 0a  tring) then the.
13243 2a 2a 20 69 6e 64 65 78 20 74 61 6b 65 6e 20 66  ** index taken f
13244 72 6f 6d 20 74 68 65 20 74 6f 70 20 6f 66 20 74  rom the top of t
13245 68 65 20 73 74 61 63 6b 20 69 73 20 74 65 6d 70  he stack is temp
13246 6f 72 61 72 69 6c 79 20 69 6e 63 72 65 61 73 65  orarily increase
13247 64 20 62 79 0a 2a 2a 20 61 6e 20 65 70 73 69 6c  d by.** an epsil
13248 6f 6e 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20  on prior to the 
13249 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 54 68 69  comparison.  Thi
1324a 73 20 6d 61 6b 65 20 74 68 65 20 6f 70 63 6f 64  s make the opcod
1324b 65 20 77 6f 72 6b 0a 2a 2a 20 6c 69 6b 65 20 49  e work.** like I
1324c 64 78 47 54 20 65 78 63 65 70 74 20 74 68 61 74  dxGT except that
1324d 20 69 66 20 74 68 65 20 6b 65 79 20 66 72 6f 6d   if the key from
1324e 20 74 68 65 20 73 74 61 63 6b 20 69 73 20 61 20   the stack is a 
1324f 70 72 65 66 69 78 20 6f 66 0a 2a 2a 20 74 68 65  prefix of.** the
13250 20 6b 65 79 20 69 6e 20 74 68 65 20 63 75 72 73   key in the curs
13251 6f 72 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69  or, the result i
13252 73 20 66 61 6c 73 65 20 77 68 65 72 65 61 73 20  s false whereas 
13253 69 74 20 77 6f 75 6c 64 20 62 65 0a 2a 2a 20 74  it would be.** t
13254 72 75 65 20 77 69 74 68 20 49 64 78 47 54 2e 0a  rue with IdxGT..
13255 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64  */./* Opcode: Id
13256 78 4c 54 20 50 31 20 50 32 20 50 33 0a 2a 2a 0a  xLT P1 P2 P3.**.
13257 2a 2a 20 54 68 65 20 74 6f 70 20 6f 66 20 74 68  ** The top of th
13258 65 20 73 74 61 63 6b 20 69 73 20 61 6e 20 69 6e  e stack is an in
13259 64 65 78 20 65 6e 74 72 79 20 74 68 61 74 20 6f  dex entry that o
1325a 6d 69 74 73 20 74 68 65 20 52 4f 57 49 44 2e 20  mits the ROWID. 
1325b 20 43 6f 6d 70 61 72 65 0a 2a 2a 20 74 68 65 20   Compare.** the 
1325c 74 6f 70 20 6f 66 20 73 74 61 63 6b 20 61 67 61  top of stack aga
1325d 69 6e 73 74 20 74 68 65 20 69 6e 64 65 78 20 74  inst the index t
1325e 68 61 74 20 50 31 20 69 73 20 63 75 72 72 65 6e  hat P1 is curren
1325f 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e  tly pointing to.
13260 0a 2a 2a 20 49 67 6e 6f 72 65 20 74 68 65 20 52  .** Ignore the R
13261 4f 57 49 44 20 6f 6e 20 74 68 65 20 50 31 20 69  OWID on the P1 i
13262 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ndex..**.** If t
13263 68 65 20 50 31 20 69 6e 64 65 78 20 65 6e 74 72  he P1 index entr
13264 79 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 20  y is less than  
13265 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73  the top of the s
13266 74 61 63 6b 0a 2a 2a 20 74 68 65 6e 20 6a 75 6d  tack.** then jum
13267 70 20 74 6f 20 50 32 2e 20 20 4f 74 68 65 72 77  p to P2.  Otherw
13268 69 73 65 20 66 61 6c 6c 20 74 68 72 6f 75 67 68  ise fall through
13269 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73   to the next ins
1326a 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 49 6e 20  truction..** In 
1326b 65 69 74 68 65 72 20 63 61 73 65 2c 20 74 68 65  either case, the
1326c 20 73 74 61 63 6b 20 69 73 20 70 6f 70 70 65 64   stack is popped
1326d 20 6f 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   once..**.** If 
1326e 50 33 20 69 73 20 74 68 65 20 22 2b 22 20 73 74  P3 is the "+" st
1326f 72 69 6e 67 20 28 6f 72 20 61 6e 79 20 6f 74 68  ring (or any oth
13270 65 72 20 6e 6f 6e 2d 4e 55 4c 4c 20 73 74 72 69  er non-NULL stri
13271 6e 67 29 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20  ng) then the.** 
13272 69 6e 64 65 78 20 74 61 6b 65 6e 20 66 72 6f 6d  index taken from
13273 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20   the top of the 
13274 73 74 61 63 6b 20 69 73 20 74 65 6d 70 6f 72 61  stack is tempora
13275 72 69 6c 79 20 69 6e 63 72 65 61 73 65 64 20 62  rily increased b
13276 79 0a 2a 2a 20 61 6e 20 65 70 73 69 6c 6f 6e 20  y.** an epsilon 
13277 70 72 69 6f 72 20 74 6f 20 74 68 65 20 63 6f 6d  prior to the com
13278 70 61 72 69 73 6f 6e 2e 20 20 54 68 69 73 20 6d  parison.  This m
13279 61 6b 65 73 20 74 68 65 20 6f 70 63 6f 64 65 20  akes the opcode 
1327a 77 6f 72 6b 0a 2a 2a 20 6c 69 6b 65 20 49 64 78  work.** like Idx
1327b 4c 45 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49  LE..*/.case OP_I
1327c 64 78 4c 54 3a 20 20 20 20 20 20 20 20 20 20 2f  dxLT:          /
1327d 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 63 61 73  * no-push */.cas
1327e 65 20 4f 50 5f 49 64 78 47 54 3a 20 20 20 20 20  e OP_IdxGT:     
1327f 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20       /* no-push 
13280 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 47 45  */.case OP_IdxGE
13281 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f  : {        /* no
13282 2d 70 75 73 68 20 2a 2f 0a 20 20 69 6e 74 20 69  -push */.  int i
13283 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 43 75 72  = pOp->p1;.  Cur
13284 73 6f 72 20 2a 70 43 3b 0a 0a 20 20 61 73 73 65  sor *pC;..  asse
13285 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d  rt( i>=0 && i<p-
13286 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73  >nCursor );.  as
13287 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69  sert( p->apCsr[i
13288 5d 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  ]!=0 );.  assert
13289 28 20 70 54 6f 73 3e 3d 70 2d 3e 61 53 74 61 63  ( pTos>=p->aStac
1328a 6b 20 29 3b 0a 20 20 69 66 28 20 28 70 43 20 3d  k );.  if( (pC =
1328b 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 2d 3e 70   p->apCsr[i])->p
1328c 43 75 72 73 6f 72 21 3d 30 20 29 7b 0a 20 20 20  Cursor!=0 ){.   
1328d 20 69 6e 74 20 72 65 73 3b 0a 20 0a 20 20 20 20   int res;. .    
1328e 61 73 73 65 72 74 28 20 70 54 6f 73 2d 3e 66 6c  assert( pTos->fl
1328f 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29  ags & MEM_Blob )
13290 3b 20 20 2f 2a 20 43 72 65 61 74 65 64 20 75 73  ;  /* Created us
13291 69 6e 67 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72  ing OP_MakeRecor
13292 64 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28  d */.    assert(
13293 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76   pC->deferredMov
13294 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 45 78  eto==0 );.    Ex
13295 70 61 6e 64 42 6c 6f 62 28 70 54 6f 73 29 3b 0a  pandBlob(pTos);.
13296 20 20 20 20 2a 70 43 2d 3e 70 49 6e 63 72 4b 65      *pC->pIncrKe
13297 79 20 3d 20 70 4f 70 2d 3e 70 33 21 3d 30 3b 0a  y = pOp->p3!=0;.
13298 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
13299 3e 70 33 3d 3d 30 20 7c 7c 20 70 4f 70 2d 3e 6f  >p3==0 || pOp->o
1329a 70 63 6f 64 65 21 3d 4f 50 5f 49 64 78 47 54 20  pcode!=OP_IdxGT 
1329b 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  );.    rc = sqli
1329c 74 65 33 56 64 62 65 49 64 78 4b 65 79 43 6f 6d  te3VdbeIdxKeyCom
1329d 70 61 72 65 28 70 43 2c 20 70 54 6f 73 2d 3e 6e  pare(pC, pTos->n
1329e 2c 20 28 75 38 2a 29 70 54 6f 73 2d 3e 7a 2c 20  , (u8*)pTos->z, 
1329f 26 72 65 73 29 3b 0a 20 20 20 20 2a 70 43 2d 3e  &res);.    *pC->
132a0 70 49 6e 63 72 4b 65 79 20 3d 20 30 3b 0a 20 20  pIncrKey = 0;.  
132a1 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
132a2 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 62 72 65  _OK ){.      bre
132a3 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ak;.    }.    if
132a4 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f  ( pOp->opcode==O
132a5 50 5f 49 64 78 4c 54 20 29 7b 0a 20 20 20 20 20  P_IdxLT ){.     
132a6 20 72 65 73 20 3d 20 2d 72 65 73 3b 0a 20 20 20   res = -res;.   
132a7 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e   }else if( pOp->
132a8 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 64 78 47 45  opcode==OP_IdxGE
132a9 20 29 7b 0a 20 20 20 20 20 20 72 65 73 2b 2b 3b   ){.      res++;
132aa 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72  .    }.    if( r
132ab 65 73 3e 30 20 29 7b 0a 20 20 20 20 20 20 70 63  es>0 ){.      pc
132ac 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 20 3b   = pOp->p2 - 1 ;
132ad 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 52 65 6c  .    }.  }.  Rel
132ae 65 61 73 65 28 70 54 6f 73 29 3b 0a 20 20 70 54  ease(pTos);.  pT
132af 6f 73 2d 2d 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  os--;.  break;.}
132b0 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 65 73  ../* Opcode: Des
132b1 74 72 6f 79 20 50 31 20 50 32 20 2a 0a 2a 2a 0a  troy P1 P2 *.**.
132b2 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65 6e 74  ** Delete an ent
132b3 69 72 65 20 64 61 74 61 62 61 73 65 20 74 61 62  ire database tab
132b4 6c 65 20 6f 72 20 69 6e 64 65 78 20 77 68 6f 73  le or index whos
132b5 65 20 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 74  e root page in t
132b6 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66  he database.** f
132b7 69 6c 65 20 69 73 20 67 69 76 65 6e 20 62 79 20  ile is given by 
132b8 50 31 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61  P1..**.** The ta
132b9 62 6c 65 20 62 65 69 6e 67 20 64 65 73 74 72 6f  ble being destro
132ba 79 65 64 20 69 73 20 69 6e 20 74 68 65 20 6d 61  yed is in the ma
132bb 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  in database file
132bc 20 69 66 20 50 32 3d 3d 30 2e 20 20 49 66 0a 2a   if P2==0.  If.*
132bd 2a 20 50 32 3d 3d 31 20 74 68 65 6e 20 74 68 65  * P2==1 then the
132be 20 74 61 62 6c 65 20 74 6f 20 62 65 20 63 6c 65   table to be cle
132bf 61 72 20 69 73 20 69 6e 20 74 68 65 20 61 75 78  ar is in the aux
132c0 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20  iliary database 
132c1 66 69 6c 65 0a 2a 2a 20 74 68 61 74 20 69 73 20  file.** that is 
132c2 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 61  used to store ta
132c3 62 6c 65 73 20 63 72 65 61 74 65 20 75 73 69 6e  bles create usin
132c4 67 20 43 52 45 41 54 45 20 54 45 4d 50 4f 52 41  g CREATE TEMPORA
132c5 52 59 20 54 41 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20  RY TABLE..**.** 
132c6 49 66 20 41 55 54 4f 56 41 43 55 55 4d 20 69 73  If AUTOVACUUM is
132c7 20 65 6e 61 62 6c 65 64 20 74 68 65 6e 20 69 74   enabled then it
132c8 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 68 61   is possible tha
132c9 74 20 61 6e 6f 74 68 65 72 20 72 6f 6f 74 20 70  t another root p
132ca 61 67 65 0a 2a 2a 20 6d 69 67 68 74 20 62 65 20  age.** might be 
132cb 6d 6f 76 65 64 20 69 6e 74 6f 20 74 68 65 20 6e  moved into the n
132cc 65 77 6c 79 20 64 65 6c 65 74 65 64 20 72 6f 6f  ewly deleted roo
132cd 74 20 70 61 67 65 20 69 6e 20 6f 72 64 65 72 20  t page in order 
132ce 74 6f 20 6b 65 65 70 20 61 6c 6c 0a 2a 2a 20 72  to keep all.** r
132cf 6f 6f 74 20 70 61 67 65 73 20 63 6f 6e 74 69 67  oot pages contig
132d0 75 6f 75 73 20 61 74 20 74 68 65 20 62 65 67 69  uous at the begi
132d1 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 64 61 74  nning of the dat
132d2 61 62 61 73 65 2e 20 20 54 68 65 20 66 6f 72 6d  abase.  The form
132d3 65 72 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 74  er.** value of t
132d4 68 65 20 72 6f 6f 74 20 70 61 67 65 20 74 68 61  he root page tha
132d5 74 20 6d 6f 76 65 64 20 2d 20 69 74 73 20 76 61  t moved - its va
132d6 6c 75 65 20 62 65 66 6f 72 65 20 74 68 65 20 6d  lue before the m
132d7 6f 76 65 20 6f 63 63 75 72 72 65 64 20 2d 0a 2a  ove occurred -.*
132d8 2a 20 69 73 20 70 75 73 68 65 64 20 6f 6e 74 6f  * is pushed onto
132d9 20 74 68 65 20 73 74 61 63 6b 2e 20 20 49 66 20   the stack.  If 
132da 6e 6f 20 70 61 67 65 20 6d 6f 76 65 6d 65 6e 74  no page movement
132db 20 77 61 73 20 72 65 71 75 69 72 65 64 20 28 62   was required (b
132dc 65 63 61 75 73 65 0a 2a 2a 20 74 68 65 20 74 61  ecause.** the ta
132dd 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70 65  ble being droppe
132de 64 20 77 61 73 20 61 6c 72 65 61 64 79 20 74 68  d was already th
132df 65 20 6c 61 73 74 20 6f 6e 65 20 69 6e 20 74 68  e last one in th
132e0 65 20 64 61 74 61 62 61 73 65 29 20 74 68 65 6e  e database) then
132e1 0a 2a 2a 20 61 20 7a 65 72 6f 20 69 73 20 70 75  .** a zero is pu
132e2 73 68 65 64 20 6f 6e 74 6f 20 74 68 65 20 73 74  shed onto the st
132e3 61 63 6b 2e 20 20 49 66 20 41 55 54 4f 56 41 43  ack.  If AUTOVAC
132e4 55 55 4d 20 69 73 20 64 69 73 61 62 6c 65 64 0a  UUM is disabled.
132e5 2a 2a 20 74 68 65 6e 20 61 20 7a 65 72 6f 20 69  ** then a zero i
132e6 73 20 70 75 73 68 65 64 20 6f 6e 74 6f 20 74 68  s pushed onto th
132e7 65 20 73 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53  e stack..**.** S
132e8 65 65 20 61 6c 73 6f 3a 20 43 6c 65 61 72 0a 2a  ee also: Clear.*
132e9 2f 0a 63 61 73 65 20 4f 50 5f 44 65 73 74 72 6f  /.case OP_Destro
132ea 79 3a 20 7b 0a 20 20 69 6e 74 20 69 4d 6f 76 65  y: {.  int iMove
132eb 64 3b 0a 20 20 69 6e 74 20 69 43 6e 74 3b 0a 23  d;.  int iCnt;.#
132ec 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
132ed 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
132ee 20 20 56 64 62 65 20 2a 70 56 64 62 65 3b 0a 20    Vdbe *pVdbe;. 
132ef 20 69 43 6e 74 20 3d 20 30 3b 0a 20 20 66 6f 72   iCnt = 0;.  for
132f0 28 70 56 64 62 65 3d 64 62 2d 3e 70 56 64 62 65  (pVdbe=db->pVdbe
132f1 3b 20 70 56 64 62 65 3b 20 70 56 64 62 65 3d 70  ; pVdbe; pVdbe=p
132f2 56 64 62 65 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  Vdbe->pNext){.  
132f3 20 20 69 66 28 20 70 56 64 62 65 2d 3e 6d 61 67    if( pVdbe->mag
132f4 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52  ic==VDBE_MAGIC_R
132f5 55 4e 20 26 26 20 70 56 64 62 65 2d 3e 69 6e 56  UN && pVdbe->inV
132f6 74 61 62 4d 65 74 68 6f 64 3c 32 20 26 26 20 70  tabMethod<2 && p
132f7 56 64 62 65 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20  Vdbe->pc>=0 ){. 
132f8 20 20 20 20 20 69 43 6e 74 2b 2b 3b 0a 20 20 20       iCnt++;.   
132f9 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 69   }.  }.#else.  i
132fa 43 6e 74 20 3d 20 64 62 2d 3e 61 63 74 69 76 65  Cnt = db->active
132fb 56 64 62 65 43 6e 74 3b 0a 23 65 6e 64 69 66 0a  VdbeCnt;.#endif.
132fc 20 20 69 66 28 20 69 43 6e 74 3e 31 20 29 7b 0a    if( iCnt>1 ){.
132fd 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
132fe 4c 4f 43 4b 45 44 3b 0a 20 20 7d 65 6c 73 65 7b  LOCKED;.  }else{
132ff 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 43 6e  .    assert( iCn
13300 74 3d 3d 31 20 29 3b 0a 20 20 20 20 72 63 20 3d  t==1 );.    rc =
13301 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f   sqlite3BtreeDro
13302 70 54 61 62 6c 65 28 64 62 2d 3e 61 44 62 5b 70  pTable(db->aDb[p
13303 4f 70 2d 3e 70 32 5d 2e 70 42 74 2c 20 70 4f 70  Op->p2].pBt, pOp
13304 2d 3e 70 31 2c 20 26 69 4d 6f 76 65 64 29 3b 0a  ->p1, &iMoved);.
13305 20 20 20 20 70 54 6f 73 2b 2b 3b 0a 20 20 20 20      pTos++;.    
13306 70 54 6f 73 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pTos->flags = ME
13307 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 54 6f 73 2d  M_Int;.    pTos-
13308 3e 75 2e 69 20 3d 20 69 4d 6f 76 65 64 3b 0a 23  >u.i = iMoved;.#
13309 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1330a 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
1330b 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1330c 5f 4f 4b 20 26 26 20 69 4d 6f 76 65 64 21 3d 30  _OK && iMoved!=0
1330d 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1330e 33 52 6f 6f 74 50 61 67 65 4d 6f 76 65 64 28 26  3RootPageMoved(&
1330f 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 32 5d  db->aDb[pOp->p2]
13310 2c 20 69 4d 6f 76 65 64 2c 20 70 4f 70 2d 3e 70  , iMoved, pOp->p
13311 31 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  1);.    }.#endif
13312 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
13313 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6c 65 61  ./* Opcode: Clea
13314 72 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20  r P1 P2 *.**.** 
13315 44 65 6c 65 74 65 20 61 6c 6c 20 63 6f 6e 74 65  Delete all conte
13316 6e 74 73 20 6f 66 20 74 68 65 20 64 61 74 61 62  nts of the datab
13317 61 73 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  ase table or ind
13318 65 78 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 61  ex whose root pa
13319 67 65 0a 2a 2a 20 69 6e 20 74 68 65 20 64 61 74  ge.** in the dat
1331a 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 67 69  abase file is gi
1331b 76 65 6e 20 62 79 20 50 31 2e 20 20 42 75 74 2c  ven by P1.  But,
1331c 20 75 6e 6c 69 6b 65 20 44 65 73 74 72 6f 79 2c   unlike Destroy,
1331d 20 64 6f 20 6e 6f 74 0a 2a 2a 20 72 65 6d 6f 76   do not.** remov
1331e 65 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69  e the table or i
1331f 6e 64 65 78 20 66 72 6f 6d 20 74 68 65 20 64 61  ndex from the da
13320 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a  tabase file..**.
13321 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65 69  ** The table bei
13322 6e 67 20 63 6c 65 61 72 20 69 73 20 69 6e 20 74  ng clear is in t
13323 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
13324 20 66 69 6c 65 20 69 66 20 50 32 3d 3d 30 2e 20   file if P2==0. 
13325 20 49 66 0a 2a 2a 20 50 32 3d 3d 31 20 74 68 65   If.** P2==1 the
13326 6e 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62  n the table to b
13327 65 20 63 6c 65 61 72 20 69 73 20 69 6e 20 74 68  e clear is in th
13328 65 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61  e auxiliary data
13329 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 74 68 61  base file.** tha
1332a 74 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f  t is used to sto
1332b 72 65 20 74 61 62 6c 65 73 20 63 72 65 61 74 65  re tables create
1332c 20 75 73 69 6e 67 20 43 52 45 41 54 45 20 54 45   using CREATE TE
1332d 4d 50 4f 52 41 52 59 20 54 41 42 4c 45 2e 0a 2a  MPORARY TABLE..*
1332e 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 44  *.** See also: D
1332f 65 73 74 72 6f 79 0a 2a 2f 0a 63 61 73 65 20 4f  estroy.*/.case O
13330 50 5f 43 6c 65 61 72 3a 20 7b 20 20 20 20 20 20  P_Clear: {      
13331 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a    /* no-push */.
13332 0a 20 20 2f 2a 20 46 6f 72 20 63 6f 6e 73 69 73  .  /* For consis
13333 74 65 6e 63 79 20 77 69 74 68 20 74 68 65 20 77  tency with the w
13334 61 79 20 6f 74 68 65 72 20 66 65 61 74 75 72 65  ay other feature
13335 73 20 6f 66 20 53 51 4c 69 74 65 20 6f 70 65 72  s of SQLite oper
13336 61 74 65 0a 20 20 2a 2a 20 77 69 74 68 20 61 20  ate.  ** with a 
13337 74 72 75 6e 63 61 74 65 2c 20 77 65 20 77 69 6c  truncate, we wil
13338 6c 20 61 6c 73 6f 20 73 6b 69 70 20 74 68 65 20  l also skip the 
13339 75 70 64 61 74 65 20 63 61 6c 6c 62 61 63 6b 2e  update callback.
1333a 0a 20 20 2a 2f 0a 23 69 66 20 30 0a 20 20 42 74  .  */.#if 0.  Bt
1333b 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61  ree *pBt = db->a
1333c 44 62 5b 70 4f 70 2d 3e 70 32 5d 2e 70 42 74 3b  Db[pOp->p2].pBt;
1333d 0a 20 20 69 66 28 20 64 62 2d 3e 78 55 70 64 61  .  if( db->xUpda
1333e 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f  teCallback && pO
1333f 70 2d 3e 70 33 20 29 7b 0a 20 20 20 20 63 6f 6e  p->p3 ){.    con
13340 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 64  st char *zDb = d
13341 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 32 5d 2e  b->aDb[pOp->p2].
13342 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74  zName;.    const
13343 20 63 68 61 72 20 2a 7a 54 62 6c 20 3d 20 70 4f   char *zTbl = pO
13344 70 2d 3e 70 33 3b 0a 20 20 20 20 42 74 43 75 72  p->p3;.    BtCur
13345 73 6f 72 20 2a 70 43 75 72 20 3d 20 30 3b 0a 20  sor *pCur = 0;. 
13346 20 20 20 69 6e 74 20 66 69 6e 20 3d 20 30 3b 0a     int fin = 0;.
13347 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
13348 33 42 74 72 65 65 43 75 72 73 6f 72 28 70 42 74  3BtreeCursor(pBt
13349 2c 20 70 4f 70 2d 3e 70 31 2c 20 30 2c 20 30 2c  , pOp->p1, 0, 0,
1334a 20 30 2c 20 26 70 43 75 72 29 3b 0a 20 20 20 20   0, &pCur);.    
1334b 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1334c 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  K ){.      goto 
1334d 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72  abort_due_to_err
1334e 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f  or;.    }.    fo
1334f 72 28 0a 20 20 20 20 20 20 72 63 3d 73 71 6c 69  r(.      rc=sqli
13350 74 65 33 42 74 72 65 65 46 69 72 73 74 28 70 43  te3BtreeFirst(pC
13351 75 72 2c 20 26 66 69 6e 29 3b 20 0a 20 20 20 20  ur, &fin); .    
13352 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20    rc==SQLITE_OK 
13353 26 26 20 21 66 69 6e 3b 20 0a 20 20 20 20 20 20  && !fin; .      
13354 72 63 3d 73 71 6c 69 74 65 33 42 74 72 65 65 4e  rc=sqlite3BtreeN
13355 65 78 74 28 70 43 75 72 2c 20 26 66 69 6e 29 0a  ext(pCur, &fin).
13356 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 36 34      ){.      i64
13357 20 69 4b 65 79 3b 0a 20 20 20 20 20 20 72 63 20   iKey;.      rc 
13358 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65  = sqlite3BtreeKe
13359 79 53 69 7a 65 28 70 43 75 72 2c 20 26 69 4b 65  ySize(pCur, &iKe
1335a 79 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  y);.      if( rc
1335b 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61   ){.        brea
1335c 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
1335d 20 69 4b 65 79 20 3d 20 6b 65 79 54 6f 49 6e 74   iKey = keyToInt
1335e 28 69 4b 65 79 29 3b 0a 20 20 20 20 20 20 64 62  (iKey);.      db
1335f 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63  ->xUpdateCallbac
13360 6b 28 64 62 2d 3e 70 55 70 64 61 74 65 41 72 67  k(db->pUpdateArg
13361 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c  , SQLITE_DELETE,
13362 20 7a 44 62 2c 20 7a 54 62 6c 2c 20 69 4b 65 79   zDb, zTbl, iKey
13363 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
13364 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75  ite3BtreeCloseCu
13365 72 73 6f 72 28 70 43 75 72 29 3b 0a 20 20 20 20  rsor(pCur);.    
13366 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
13367 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  K ){.      goto 
13368 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72  abort_due_to_err
13369 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  or;.    }.  }.#e
1336a 6e 64 69 66 0a 20 20 72 63 20 3d 20 73 71 6c 69  ndif.  rc = sqli
1336b 74 65 33 42 74 72 65 65 43 6c 65 61 72 54 61 62  te3BtreeClearTab
1336c 6c 65 28 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e  le(db->aDb[pOp->
1336d 70 32 5d 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 31  p2].pBt, pOp->p1
1336e 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  );.  break;.}../
1336f 2a 20 4f 70 63 6f 64 65 3a 20 43 72 65 61 74 65  * Opcode: Create
13370 54 61 62 6c 65 20 50 31 20 2a 20 2a 0a 2a 2a 0a  Table P1 * *.**.
13371 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65  ** Allocate a ne
13372 77 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 6d  w table in the m
13373 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c  ain database fil
13374 65 20 69 66 20 50 32 3d 3d 30 20 6f 72 20 69 6e  e if P2==0 or in
13375 20 74 68 65 0a 2a 2a 20 61 75 78 69 6c 69 61 72   the.** auxiliar
13376 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  y database file 
13377 69 66 20 50 32 3d 3d 31 2e 20 20 50 75 73 68 20  if P2==1.  Push 
13378 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 0a  the page number.
13379 2a 2a 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 20  ** for the root 
1337a 70 61 67 65 20 6f 66 20 74 68 65 20 6e 65 77 20  page of the new 
1337b 74 61 62 6c 65 20 6f 6e 74 6f 20 74 68 65 20 73  table onto the s
1337c 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  tack..**.** The 
1337d 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65  difference betwe
1337e 65 6e 20 61 20 74 61 62 6c 65 20 61 6e 64 20 61  en a table and a
1337f 6e 20 69 6e 64 65 78 20 69 73 20 74 68 69 73 3a  n index is this:
13380 20 20 41 20 74 61 62 6c 65 20 6d 75 73 74 0a 2a    A table must.*
13381 2a 20 68 61 76 65 20 61 20 34 2d 62 79 74 65 20  * have a 4-byte 
13382 69 6e 74 65 67 65 72 20 6b 65 79 20 61 6e 64 20  integer key and 
13383 63 61 6e 20 68 61 76 65 20 61 72 62 69 74 72 61  can have arbitra
13384 72 79 20 64 61 74 61 2e 20 20 41 6e 20 69 6e 64  ry data.  An ind
13385 65 78 0a 2a 2a 20 68 61 73 20 61 6e 20 61 72 62  ex.** has an arb
13386 69 74 72 61 72 79 20 6b 65 79 20 62 75 74 20 6e  itrary key but n
13387 6f 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 53 65  o data..**.** Se
13388 65 20 61 6c 73 6f 3a 20 43 72 65 61 74 65 49 6e  e also: CreateIn
13389 64 65 78 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65  dex.*/./* Opcode
1338a 3a 20 43 72 65 61 74 65 49 6e 64 65 78 20 50 31  : CreateIndex P1
1338b 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63   * *.**.** Alloc
1338c 61 74 65 20 61 20 6e 65 77 20 69 6e 64 65 78 20  ate a new index 
1338d 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  in the main data
1338e 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 32 3d  base file if P2=
1338f 3d 30 20 6f 72 20 69 6e 20 74 68 65 0a 2a 2a 20  =0 or in the.** 
13390 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61  auxiliary databa
13391 73 65 20 66 69 6c 65 20 69 66 20 50 32 3d 3d 31  se file if P2==1
13392 2e 20 20 50 75 73 68 20 74 68 65 20 70 61 67 65  .  Push the page
13393 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 0a 2a   number of the.*
13394 2a 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74  * root page of t
13395 68 65 20 6e 65 77 20 69 6e 64 65 78 20 6f 6e 74  he new index ont
13396 6f 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2a 0a  o the stack..**.
13397 2a 2a 20 53 65 65 20 64 6f 63 75 6d 65 6e 74 61  ** See documenta
13398 74 69 6f 6e 20 6f 6e 20 4f 50 5f 43 72 65 61 74  tion on OP_Creat
13399 65 54 61 62 6c 65 20 66 6f 72 20 61 64 64 69 74  eTable for addit
1339a 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
1339b 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 72  n..*/.case OP_Cr
1339c 65 61 74 65 49 6e 64 65 78 3a 0a 63 61 73 65 20  eateIndex:.case 
1339d 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 3a 20  OP_CreateTable: 
1339e 7b 0a 20 20 69 6e 74 20 70 67 6e 6f 3b 0a 20 20  {.  int pgno;.  
1339f 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20 44 62 20  int flags;.  Db 
133a0 2a 70 44 62 3b 0a 20 20 61 73 73 65 72 74 28 20  *pDb;.  assert( 
133a1 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f  pOp->p1>=0 && pO
133a2 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b  p->p1<db->nDb );
133a3 0a 20 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44  .  pDb = &db->aD
133a4 62 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73  b[pOp->p1];.  as
133a5 73 65 72 74 28 20 70 44 62 2d 3e 70 42 74 21 3d  sert( pDb->pBt!=
133a6 30 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e  0 );.  if( pOp->
133a7 6f 70 63 6f 64 65 3d 3d 4f 50 5f 43 72 65 61 74  opcode==OP_Creat
133a8 65 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f 2a  eTable ){.    /*
133a9 20 66 6c 61 67 73 20 3d 20 42 54 52 45 45 5f 49   flags = BTREE_I
133aa 4e 54 4b 45 59 3b 20 2a 2f 0a 20 20 20 20 66 6c  NTKEY; */.    fl
133ab 61 67 73 20 3d 20 42 54 52 45 45 5f 4c 45 41 46  ags = BTREE_LEAF
133ac 44 41 54 41 7c 42 54 52 45 45 5f 49 4e 54 4b 45  DATA|BTREE_INTKE
133ad 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  Y;.  }else{.    
133ae 66 6c 61 67 73 20 3d 20 42 54 52 45 45 5f 5a 45  flags = BTREE_ZE
133af 52 4f 44 41 54 41 3b 0a 20 20 7d 0a 20 20 72 63  RODATA;.  }.  rc
133b0 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43   = sqlite3BtreeC
133b1 72 65 61 74 65 54 61 62 6c 65 28 70 44 62 2d 3e  reateTable(pDb->
133b2 70 42 74 2c 20 26 70 67 6e 6f 2c 20 66 6c 61 67  pBt, &pgno, flag
133b3 73 29 3b 0a 20 20 70 54 6f 73 2b 2b 3b 0a 20 20  s);.  pTos++;.  
133b4 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
133b5 4b 20 29 7b 0a 20 20 20 20 70 54 6f 73 2d 3e 75  K ){.    pTos->u
133b6 2e 69 20 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 70  .i = pgno;.    p
133b7 54 6f 73 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Tos->flags = MEM
133b8 5f 49 6e 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  _Int;.  }else{. 
133b9 20 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20 3d     pTos->flags =
133ba 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 0a 20   MEM_Null;.  }. 
133bb 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
133bc 63 6f 64 65 3a 20 50 61 72 73 65 53 63 68 65 6d  code: ParseSchem
133bd 61 20 50 31 20 50 32 20 50 33 0a 2a 2a 0a 2a 2a  a P1 P2 P3.**.**
133be 20 52 65 61 64 20 61 6e 64 20 70 61 72 73 65 20   Read and parse 
133bf 61 6c 6c 20 65 6e 74 72 69 65 73 20 66 72 6f 6d  all entries from
133c0 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 53 54   the SQLITE_MAST
133c1 45 52 20 74 61 62 6c 65 20 6f 66 20 64 61 74 61  ER table of data
133c2 62 61 73 65 20 50 31 0a 2a 2a 20 74 68 61 74 20  base P1.** that 
133c3 6d 61 74 63 68 20 74 68 65 20 57 48 45 52 45 20  match the WHERE 
133c4 63 6c 61 75 73 65 20 50 33 2e 20 20 50 32 20 69  clause P3.  P2 i
133c5 73 20 74 68 65 20 22 66 6f 72 63 65 22 20 66 6c  s the "force" fl
133c6 61 67 2e 20 20 20 41 6c 77 61 79 73 20 64 6f 0a  ag.   Always do.
133c7 2a 2a 20 74 68 65 20 70 61 72 73 69 6e 67 20 69  ** the parsing i
133c8 66 20 50 32 20 69 73 20 74 72 75 65 2e 20 20 49  f P2 is true.  I
133c9 66 20 50 32 20 69 73 20 66 61 6c 73 65 2c 20 74  f P2 is false, t
133ca 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
133cb 20 69 73 20 61 0a 2a 2a 20 6e 6f 2d 6f 70 20 69   is a.** no-op i
133cc 66 20 74 68 65 20 73 63 68 65 6d 61 20 69 73 20  f the schema is 
133cd 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 6c 6f  not currently lo
133ce 61 64 65 64 2e 20 20 49 6e 20 6f 74 68 65 72 20  aded.  In other 
133cf 77 6f 72 64 73 2c 20 69 66 20 50 32 0a 2a 2a 20  words, if P2.** 
133d0 69 73 20 66 61 6c 73 65 2c 20 74 68 65 20 53 51  is false, the SQ
133d1 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c  LITE_MASTER tabl
133d2 65 20 69 73 20 6f 6e 6c 79 20 70 61 72 73 65 64  e is only parsed
133d3 20 69 66 20 74 68 65 20 72 65 73 74 20 6f 66 20   if the rest of 
133d4 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 69 73  the.** schema is
133d5 20 61 6c 72 65 61 64 79 20 6c 6f 61 64 65 64 20   already loaded 
133d6 69 6e 74 6f 20 74 68 65 20 73 79 6d 62 6f 6c 20  into the symbol 
133d7 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  table..**.** Thi
133d8 73 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73  s opcode invokes
133d9 20 74 68 65 20 70 61 72 73 65 72 20 74 6f 20 63   the parser to c
133da 72 65 61 74 65 20 61 20 6e 65 77 20 76 69 72 74  reate a new virt
133db 75 61 6c 20 6d 61 63 68 69 6e 65 2c 0a 2a 2a 20  ual machine,.** 
133dc 74 68 65 6e 20 72 75 6e 73 20 74 68 65 20 6e 65  then runs the ne
133dd 77 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e  w virtual machin
133de 65 2e 20 20 49 74 20 69 73 20 74 68 75 73 20 61  e.  It is thus a
133df 20 72 65 65 6e 74 72 61 6e 74 20 6f 70 63 6f 64   reentrant opcod
133e0 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 61  e..*/.case OP_Pa
133e1 72 73 65 53 63 68 65 6d 61 3a 20 7b 20 20 20 20  rseSchema: {    
133e2 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a      /* no-push *
133e3 2f 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a  /.  char *zSql;.
133e4 20 20 69 6e 74 20 69 44 62 20 3d 20 70 4f 70 2d    int iDb = pOp-
133e5 3e 70 31 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  >p1;.  const cha
133e6 72 20 2a 7a 4d 61 73 74 65 72 3b 0a 20 20 49 6e  r *zMaster;.  In
133e7 69 74 44 61 74 61 20 69 6e 69 74 44 61 74 61 3b  itData initData;
133e8 0a 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e  ..  assert( iDb>
133e9 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44  =0 && iDb<db->nD
133ea 62 20 29 3b 0a 20 20 69 66 28 20 21 70 4f 70 2d  b );.  if( !pOp-
133eb 3e 70 32 20 26 26 20 21 44 62 48 61 73 50 72 6f  >p2 && !DbHasPro
133ec 70 65 72 74 79 28 64 62 2c 20 69 44 62 2c 20 44  perty(db, iDb, D
133ed 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 20  B_SchemaLoaded) 
133ee 29 7b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  ){.    break;.  
133ef 7d 0a 20 20 7a 4d 61 73 74 65 72 20 3d 20 53 43  }.  zMaster = SC
133f0 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 3b  HEMA_TABLE(iDb);
133f1 0a 20 20 69 6e 69 74 44 61 74 61 2e 64 62 20 3d  .  initData.db =
133f2 20 64 62 3b 0a 20 20 69 6e 69 74 44 61 74 61 2e   db;.  initData.
133f3 69 44 62 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20  iDb = pOp->p1;. 
133f4 20 69 6e 69 74 44 61 74 61 2e 70 7a 45 72 72 4d   initData.pzErrM
133f5 73 67 20 3d 20 26 70 2d 3e 7a 45 72 72 4d 73 67  sg = &p->zErrMsg
133f6 3b 0a 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74  ;.  zSql = sqlit
133f7 65 33 4d 50 72 69 6e 74 66 28 0a 20 20 20 20 20  e3MPrintf(.     
133f8 22 53 45 4c 45 43 54 20 6e 61 6d 65 2c 20 72 6f  "SELECT name, ro
133f9 6f 74 70 61 67 65 2c 20 73 71 6c 20 46 52 4f 4d  otpage, sql FROM
133fa 20 27 25 71 27 2e 25 73 20 57 48 45 52 45 20 25   '%q'.%s WHERE %
133fb 73 22 2c 0a 20 20 20 20 20 64 62 2d 3e 61 44 62  s",.     db->aDb
133fc 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 7a 4d 61  [iDb].zName, zMa
133fd 73 74 65 72 2c 20 70 4f 70 2d 3e 70 33 29 3b 0a  ster, pOp->p3);.
133fe 20 20 69 66 28 20 7a 53 71 6c 3d 3d 30 20 29 20    if( zSql==0 ) 
133ff 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 73  goto no_mem;.  s
13400 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
13401 64 62 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64  db);.  assert( d
13402 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30 20  b->init.busy==0 
13403 29 3b 0a 20 20 64 62 2d 3e 69 6e 69 74 2e 62 75  );.  db->init.bu
13404 73 79 20 3d 20 31 3b 0a 20 20 61 73 73 65 72 74  sy = 1;.  assert
13405 28 20 21 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  ( !sqlite3Malloc
13406 46 61 69 6c 65 64 28 29 20 29 3b 0a 20 20 72 63  Failed() );.  rc
13407 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28   = sqlite3_exec(
13408 64 62 2c 20 7a 53 71 6c 2c 20 73 71 6c 69 74 65  db, zSql, sqlite
13409 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 2c 20 26  3InitCallback, &
1340a 69 6e 69 74 44 61 74 61 2c 20 30 29 3b 0a 20 20  initData, 0);.  
1340b 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 41  if( rc==SQLITE_A
1340c 42 4f 52 54 20 29 20 72 63 20 3d 20 69 6e 69 74  BORT ) rc = init
1340d 44 61 74 61 2e 72 63 3b 0a 20 20 73 71 6c 69 74  Data.rc;.  sqlit
1340e 65 46 72 65 65 28 7a 53 71 6c 29 3b 0a 20 20 64  eFree(zSql);.  d
1340f 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 3d 20 30  b->init.busy = 0
13410 3b 0a 20 20 73 71 6c 69 74 65 33 53 61 66 65 74  ;.  sqlite3Safet
13411 79 4f 6e 28 64 62 29 3b 0a 20 20 69 66 28 20 72  yOn(db);.  if( r
13412 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
13413 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 61  ){.    sqlite3Fa
13414 69 6c 65 64 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20  iledMalloc();.  
13415 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20    goto no_mem;. 
13416 20 7d 0a 20 20 62 72 65 61 6b 3b 20 20 0a 7d 0a   }.  break;  .}.
13417 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
13418 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a  LITE_OMIT_ANALYZ
13419 45 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  E) && !defined(S
1341a 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 52 53 45  QLITE_OMIT_PARSE
1341b 52 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 6f  R)./* Opcode: Lo
1341c 61 64 41 6e 61 6c 79 73 69 73 20 50 31 20 2a 20  adAnalysis P1 * 
1341d 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65  *.**.** Read the
1341e 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61   sqlite_stat1 ta
1341f 62 6c 65 20 66 6f 72 20 64 61 74 61 62 61 73 65  ble for database
13420 20 50 31 20 61 6e 64 20 6c 6f 61 64 20 74 68 65   P1 and load the
13421 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 6f 66 20 74   content.** of t
13422 68 61 74 20 74 61 62 6c 65 20 69 6e 74 6f 20 74  hat table into t
13423 68 65 20 69 6e 74 65 72 6e 61 6c 20 69 6e 64 65  he internal inde
13424 78 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54  x hash table.  T
13425 68 69 73 20 77 69 6c 6c 20 63 61 75 73 65 0a 2a  his will cause.*
13426 2a 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20 74  * the analysis t
13427 6f 20 62 65 20 75 73 65 64 20 77 68 65 6e 20 70  o be used when p
13428 72 65 70 61 72 69 6e 67 20 61 6c 6c 20 73 75 62  reparing all sub
13429 73 65 71 75 65 6e 74 20 71 75 65 72 69 65 73 2e  sequent queries.
1342a 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 6f 61 64  .*/.case OP_Load
1342b 41 6e 61 6c 79 73 69 73 3a 20 7b 20 20 20 20 20  Analysis: {     
1342c 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f     /* no-push */
1342d 0a 20 20 69 6e 74 20 69 44 62 20 3d 20 70 4f 70  .  int iDb = pOp
1342e 2d 3e 70 31 3b 0a 20 20 61 73 73 65 72 74 28 20  ->p1;.  assert( 
1342f 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62  iDb>=0 && iDb<db
13430 2d 3e 6e 44 62 20 29 3b 0a 20 20 72 63 20 3d 20  ->nDb );.  rc = 
13431 73 71 6c 69 74 65 33 41 6e 61 6c 79 73 69 73 4c  sqlite3AnalysisL
13432 6f 61 64 28 64 62 2c 20 69 44 62 29 3b 0a 20 20  oad(db, iDb);.  
13433 62 72 65 61 6b 3b 20 20 0a 7d 0a 23 65 6e 64 69  break;  .}.#endi
13434 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51  f /* !defined(SQ
13435 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a  LITE_OMIT_ANALYZ
13436 45 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  E) && !defined(S
13437 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 52 53 45  QLITE_OMIT_PARSE
13438 52 29 20 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64  R)  */../* Opcod
13439 65 3a 20 44 72 6f 70 54 61 62 6c 65 20 50 31 20  e: DropTable P1 
1343a 2a 20 50 33 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f 76  * P3.**.** Remov
1343b 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 28  e the internal (
1343c 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 61 74 61 20  in-memory) data 
1343d 73 74 72 75 63 74 75 72 65 73 20 74 68 61 74 20  structures that 
1343e 64 65 73 63 72 69 62 65 0a 2a 2a 20 74 68 65 20  describe.** the 
1343f 74 61 62 6c 65 20 6e 61 6d 65 64 20 50 33 20 69  table named P3 i
13440 6e 20 64 61 74 61 62 61 73 65 20 50 31 2e 20 20  n database P1.  
13441 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 61  This is called a
13442 66 74 65 72 20 61 20 74 61 62 6c 65 0a 2a 2a 20  fter a table.** 
13443 69 73 20 64 72 6f 70 70 65 64 20 69 6e 20 6f 72  is dropped in or
13444 64 65 72 20 74 6f 20 6b 65 65 70 20 74 68 65 20  der to keep the 
13445 69 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65  internal represe
13446 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a  ntation of the.*
13447 2a 20 73 63 68 65 6d 61 20 63 6f 6e 73 69 73 74  * schema consist
13448 65 6e 74 20 77 69 74 68 20 77 68 61 74 20 69 73  ent with what is
13449 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73   on disk..*/.cas
1344a 65 20 4f 50 5f 44 72 6f 70 54 61 62 6c 65 3a 20  e OP_DropTable: 
1344b 7b 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70  {        /* no-p
1344c 75 73 68 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ush */.  sqlite3
1344d 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54  UnlinkAndDeleteT
1344e 61 62 6c 65 28 64 62 2c 20 70 4f 70 2d 3e 70 31  able(db, pOp->p1
1344f 2c 20 70 4f 70 2d 3e 70 33 29 3b 0a 20 20 62 72  , pOp->p3);.  br
13450 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
13451 65 3a 20 44 72 6f 70 49 6e 64 65 78 20 50 31 20  e: DropIndex P1 
13452 2a 20 50 33 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f 76  * P3.**.** Remov
13453 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 28  e the internal (
13454 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 61 74 61 20  in-memory) data 
13455 73 74 72 75 63 74 75 72 65 73 20 74 68 61 74 20  structures that 
13456 64 65 73 63 72 69 62 65 0a 2a 2a 20 74 68 65 20  describe.** the 
13457 69 6e 64 65 78 20 6e 61 6d 65 64 20 50 33 20 69  index named P3 i
13458 6e 20 64 61 74 61 62 61 73 65 20 50 31 2e 20 20  n database P1.  
13459 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 61  This is called a
1345a 66 74 65 72 20 61 6e 20 69 6e 64 65 78 0a 2a 2a  fter an index.**
1345b 20 69 73 20 64 72 6f 70 70 65 64 20 69 6e 20 6f   is dropped in o
1345c 72 64 65 72 20 74 6f 20 6b 65 65 70 20 74 68 65  rder to keep the
1345d 20 69 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73   internal repres
1345e 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a  entation of the.
1345f 2a 2a 20 73 63 68 65 6d 61 20 63 6f 6e 73 69 73  ** schema consis
13460 74 65 6e 74 20 77 69 74 68 20 77 68 61 74 20 69  tent with what i
13461 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61  s on disk..*/.ca
13462 73 65 20 4f 50 5f 44 72 6f 70 49 6e 64 65 78 3a  se OP_DropIndex:
13463 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d   {        /* no-
13464 70 75 73 68 20 2a 2f 0a 20 20 73 71 6c 69 74 65  push */.  sqlite
13465 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65  3UnlinkAndDelete
13466 49 6e 64 65 78 28 64 62 2c 20 70 4f 70 2d 3e 70  Index(db, pOp->p
13467 31 2c 20 70 4f 70 2d 3e 70 33 29 3b 0a 20 20 62  1, pOp->p3);.  b
13468 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
13469 64 65 3a 20 44 72 6f 70 54 72 69 67 67 65 72 20  de: DropTrigger 
1346a 50 31 20 2a 20 50 33 0a 2a 2a 0a 2a 2a 20 52 65  P1 * P3.**.** Re
1346b 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 72 6e 61  move the interna
1346c 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 61  l (in-memory) da
1346d 74 61 20 73 74 72 75 63 74 75 72 65 73 20 74 68  ta structures th
1346e 61 74 20 64 65 73 63 72 69 62 65 0a 2a 2a 20 74  at describe.** t
1346f 68 65 20 74 72 69 67 67 65 72 20 6e 61 6d 65 64  he trigger named
13470 20 50 33 20 69 6e 20 64 61 74 61 62 61 73 65 20   P3 in database 
13471 50 31 2e 20 20 54 68 69 73 20 69 73 20 63 61 6c  P1.  This is cal
13472 6c 65 64 20 61 66 74 65 72 20 61 20 74 72 69 67  led after a trig
13473 67 65 72 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65  ger.** is droppe
13474 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65  d in order to ke
13475 65 70 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  ep the internal 
13476 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f  representation o
13477 66 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20  f the.** schema 
13478 63 6f 6e 73 69 73 74 65 6e 74 20 77 69 74 68 20  consistent with 
13479 77 68 61 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e  what is on disk.
1347a 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70  .*/.case OP_Drop
1347b 54 72 69 67 67 65 72 3a 20 7b 20 20 20 20 20 20  Trigger: {      
1347c 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a    /* no-push */.
1347d 20 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41    sqlite3UnlinkA
1347e 6e 64 44 65 6c 65 74 65 54 72 69 67 67 65 72 28  ndDeleteTrigger(
1347f 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70  db, pOp->p1, pOp
13480 2d 3e 70 33 29 3b 0a 20 20 62 72 65 61 6b 3b 0a  ->p3);.  break;.
13481 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  }...#ifndef SQLI
13482 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54  TE_OMIT_INTEGRIT
13483 59 5f 43 48 45 43 4b 0a 2f 2a 20 4f 70 63 6f 64  Y_CHECK./* Opcod
13484 65 3a 20 49 6e 74 65 67 72 69 74 79 43 6b 20 50  e: IntegrityCk P
13485 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 44 6f 20  1 P2 *.**.** Do 
13486 61 6e 20 61 6e 61 6c 79 73 69 73 20 6f 66 20 74  an analysis of t
13487 68 65 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 65  he currently ope
13488 6e 20 64 61 74 61 62 61 73 65 2e 20 20 50 75 73  n database.  Pus
13489 68 20 6f 6e 74 6f 20 74 68 65 0a 2a 2a 20 73 74  h onto the.** st
1348a 61 63 6b 20 74 68 65 20 74 65 78 74 20 6f 66 20  ack the text of 
1348b 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
1348c 20 64 65 73 63 72 69 62 69 6e 67 20 61 6e 79 20   describing any 
1348d 70 72 6f 62 6c 65 6d 73 2e 0a 2a 2a 20 49 66 20  problems..** If 
1348e 6e 6f 20 70 72 6f 62 6c 65 6d 73 20 61 72 65 20  no problems are 
1348f 66 6f 75 6e 64 2c 20 70 75 73 68 20 61 20 4e 55  found, push a NU
13490 4c 4c 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63  LL onto the stac
13491 6b 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 74  k..**.** P1 is t
13492 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 61 20  he address of a 
13493 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68 61 74  memory cell that
13494 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6d 61   contains the ma
13495 78 69 6d 75 6d 0a 2a 2a 20 6e 75 6d 62 65 72 20  ximum.** number 
13496 6f 66 20 61 6c 6c 6f 77 65 64 20 65 72 72 6f 72  of allowed error
13497 73 2e 20 20 41 74 20 6d 6f 73 74 20 6d 65 6d 5b  s.  At most mem[
13498 50 31 5d 20 65 72 72 6f 72 73 20 77 69 6c 6c 20  P1] errors will 
13499 62 65 20 72 65 70 6f 72 74 65 64 2e 0a 2a 2a 20  be reported..** 
1349a 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
1349b 74 68 65 20 61 6e 61 6c 79 73 69 73 20 73 74 6f  the analysis sto
1349c 70 73 20 61 73 20 73 6f 6f 6e 20 61 73 20 6d 65  ps as soon as me
1349d 6d 5b 50 31 5d 20 65 72 72 6f 72 73 20 61 72 65  m[P1] errors are
1349e 20 0a 2a 2a 20 73 65 65 6e 2e 20 20 4d 65 6d 5b   .** seen.  Mem[
1349f 50 31 5d 20 69 73 20 75 70 64 61 74 65 64 20 77  P1] is updated w
134a0 69 74 68 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ith the number o
134a1 66 20 65 72 72 6f 72 73 20 72 65 6d 61 69 6e 69  f errors remaini
134a2 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 6f  ng..**.** The ro
134a3 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20  ot page numbers 
134a4 6f 66 20 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e  of all tables in
134a5 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 72   the database ar
134a6 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 76 61 6c  e integer.** val
134a7 75 65 73 20 6f 6e 20 74 68 65 20 73 74 61 63 6b  ues on the stack
134a8 2e 20 20 54 68 69 73 20 6f 70 63 6f 64 65 20 70  .  This opcode p
134a9 75 6c 6c 73 20 61 73 20 6d 61 6e 79 20 69 6e 74  ulls as many int
134aa 65 67 65 72 73 20 61 73 20 69 74 0a 2a 2a 20 63  egers as it.** c
134ab 61 6e 20 6f 66 66 20 6f 66 20 74 68 65 20 73 74  an off of the st
134ac 61 63 6b 20 61 6e 64 20 75 73 65 73 20 74 68 6f  ack and uses tho
134ad 73 65 20 6e 75 6d 62 65 72 73 20 61 73 20 74 68  se numbers as th
134ae 65 20 72 6f 6f 74 20 70 61 67 65 73 2e 0a 2a 2a  e root pages..**
134af 0a 2a 2a 20 49 66 20 50 32 20 69 73 20 6e 6f 74  .** If P2 is not
134b0 20 7a 65 72 6f 2c 20 74 68 65 20 63 68 65 63 6b   zero, the check
134b1 20 69 73 20 64 6f 6e 65 20 6f 6e 20 74 68 65 20   is done on the 
134b2 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61  auxiliary databa
134b3 73 65 0a 2a 2a 20 66 69 6c 65 2c 20 6e 6f 74 20  se.** file, not 
134b4 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
134b5 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  e file..**.** Th
134b6 69 73 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65  is opcode is use
134b7 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74  d to implement t
134b8 68 65 20 69 6e 74 65 67 72 69 74 79 5f 63 68 65  he integrity_che
134b9 63 6b 20 70 72 61 67 6d 61 2e 0a 2a 2f 0a 63 61  ck pragma..*/.ca
134ba 73 65 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43  se OP_IntegrityC
134bb 6b 3a 20 7b 0a 20 20 69 6e 74 20 6e 52 6f 6f 74  k: {.  int nRoot
134bc 3b 0a 20 20 69 6e 74 20 2a 61 52 6f 6f 74 3b 0a  ;.  int *aRoot;.
134bd 20 20 69 6e 74 20 6a 3b 0a 20 20 69 6e 74 20 6e    int j;.  int n
134be 45 72 72 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a  Err;.  char *z;.
134bf 20 20 4d 65 6d 20 2a 70 6e 45 72 72 3b 0a 0a 20    Mem *pnErr;.. 
134c0 20 66 6f 72 28 6e 52 6f 6f 74 3d 30 3b 20 26 70   for(nRoot=0; &p
134c1 54 6f 73 5b 2d 6e 52 6f 6f 74 5d 3e 3d 70 2d 3e  Tos[-nRoot]>=p->
134c2 61 53 74 61 63 6b 3b 20 6e 52 6f 6f 74 2b 2b 29  aStack; nRoot++)
134c3 7b 0a 20 20 20 20 69 66 28 20 28 70 54 6f 73 5b  {.    if( (pTos[
134c4 2d 6e 52 6f 6f 74 5d 2e 66 6c 61 67 73 20 26 20  -nRoot].flags & 
134c5 4d 45 4d 5f 49 6e 74 29 3d 3d 30 20 29 20 62 72  MEM_Int)==0 ) br
134c6 65 61 6b 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  eak;.  }.  asser
134c7 74 28 20 6e 52 6f 6f 74 3e 30 20 29 3b 0a 20 20  t( nRoot>0 );.  
134c8 61 52 6f 6f 74 20 3d 20 73 71 6c 69 74 65 4d 61  aRoot = sqliteMa
134c9 6c 6c 6f 63 52 61 77 28 20 73 69 7a 65 6f 66 28  llocRaw( sizeof(
134ca 69 6e 74 29 2a 28 6e 52 6f 6f 74 2b 31 29 20 29  int)*(nRoot+1) )
134cb 3b 0a 20 20 69 66 28 20 61 52 6f 6f 74 3d 3d 30  ;.  if( aRoot==0
134cc 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a   ) goto no_mem;.
134cd 20 20 6a 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20    j = pOp->p1;. 
134ce 20 61 73 73 65 72 74 28 20 6a 3e 3d 30 20 26 26   assert( j>=0 &&
134cf 20 6a 3c 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20   j<p->nMem );.  
134d0 70 6e 45 72 72 20 3d 20 26 70 2d 3e 61 4d 65 6d  pnErr = &p->aMem
134d1 5b 6a 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 28  [j];.  assert( (
134d2 70 6e 45 72 72 2d 3e 66 6c 61 67 73 20 26 20 4d  pnErr->flags & M
134d3 45 4d 5f 49 6e 74 29 21 3d 30 20 29 3b 0a 20 20  EM_Int)!=0 );.  
134d4 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 52 6f 6f 74  for(j=0; j<nRoot
134d5 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 61 52 6f 6f  ; j++){.    aRoo
134d6 74 5b 6a 5d 20 3d 20 28 70 54 6f 73 2d 6a 29 2d  t[j] = (pTos-j)-
134d7 3e 75 2e 69 3b 0a 20 20 7d 0a 20 20 61 52 6f 6f  >u.i;.  }.  aRoo
134d8 74 5b 6a 5d 20 3d 20 30 3b 0a 20 20 70 6f 70 53  t[j] = 0;.  popS
134d9 74 61 63 6b 28 26 70 54 6f 73 2c 20 6e 52 6f 6f  tack(&pTos, nRoo
134da 74 29 3b 0a 20 20 70 54 6f 73 2b 2b 3b 0a 20 20  t);.  pTos++;.  
134db 7a 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  z = sqlite3Btree
134dc 49 6e 74 65 67 72 69 74 79 43 68 65 63 6b 28 64  IntegrityCheck(d
134dd 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 32 5d 2e  b->aDb[pOp->p2].
134de 70 42 74 2c 20 61 52 6f 6f 74 2c 20 6e 52 6f 6f  pBt, aRoot, nRoo
134df 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t,.             
134e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
134e1 20 20 20 20 70 6e 45 72 72 2d 3e 75 2e 69 2c 20      pnErr->u.i, 
134e2 26 6e 45 72 72 29 3b 0a 20 20 70 6e 45 72 72 2d  &nErr);.  pnErr-
134e3 3e 75 2e 69 20 2d 3d 20 6e 45 72 72 3b 0a 20 20  >u.i -= nErr;.  
134e4 69 66 28 20 6e 45 72 72 3d 3d 30 20 29 7b 0a 20  if( nErr==0 ){. 
134e5 20 20 20 61 73 73 65 72 74 28 20 7a 3d 3d 30 20     assert( z==0 
134e6 29 3b 0a 20 20 20 20 70 54 6f 73 2d 3e 66 6c 61  );.    pTos->fla
134e7 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20  gs = MEM_Null;. 
134e8 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 54 6f 73   }else{.    pTos
134e9 2d 3e 7a 20 3d 20 7a 3b 0a 20 20 20 20 70 54 6f  ->z = z;.    pTo
134ea 73 2d 3e 6e 20 3d 20 73 74 72 6c 65 6e 28 7a 29  s->n = strlen(z)
134eb 3b 0a 20 20 20 20 70 54 6f 73 2d 3e 66 6c 61 67  ;.    pTos->flag
134ec 73 20 3d 20 4d 45 4d 5f 53 74 72 20 7c 20 4d 45  s = MEM_Str | ME
134ed 4d 5f 44 79 6e 20 7c 20 4d 45 4d 5f 54 65 72 6d  M_Dyn | MEM_Term
134ee 3b 0a 20 20 20 20 70 54 6f 73 2d 3e 78 44 65 6c  ;.    pTos->xDel
134ef 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 54 6f 73   = 0;.  }.  pTos
134f0 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55  ->enc = SQLITE_U
134f1 54 46 38 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  TF8;.  sqlite3Vd
134f2 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67  beChangeEncoding
134f3 28 70 54 6f 73 2c 20 65 6e 63 6f 64 69 6e 67 29  (pTos, encoding)
134f4 3b 0a 20 20 73 71 6c 69 74 65 46 72 65 65 28 61  ;.  sqliteFree(a
134f5 52 6f 6f 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a  Root);.  break;.
134f6 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
134f7 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54  TE_OMIT_INTEGRIT
134f8 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 2f 2a 20 4f  Y_CHECK */../* O
134f9 70 63 6f 64 65 3a 20 46 69 66 6f 57 72 69 74 65  pcode: FifoWrite
134fa 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69   * * *.**.** Wri
134fb 74 65 20 74 68 65 20 69 6e 74 65 67 65 72 20 6f  te the integer o
134fc 6e 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65  n the top of the
134fd 20 73 74 61 63 6b 0a 2a 2a 20 69 6e 74 6f 20 74   stack.** into t
134fe 68 65 20 46 69 66 6f 2e 0a 2a 2f 0a 63 61 73 65  he Fifo..*/.case
134ff 20 4f 50 5f 46 69 66 6f 57 72 69 74 65 3a 20 7b   OP_FifoWrite: {
13500 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75          /* no-pu
13501 73 68 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  sh */.  assert( 
13502 70 54 6f 73 3e 3d 70 2d 3e 61 53 74 61 63 6b 20  pTos>=p->aStack 
13503 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
13504 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 54  MemIntegerify(pT
13505 6f 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  os);.  sqlite3Vd
13506 62 65 46 69 66 6f 50 75 73 68 28 26 70 2d 3e 73  beFifoPush(&p->s
13507 46 69 66 6f 2c 20 70 54 6f 73 2d 3e 75 2e 69 29  Fifo, pTos->u.i)
13508 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 54 6f  ;.  assert( (pTo
13509 73 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44  s->flags & MEM_D
1350a 79 6e 29 3d 3d 30 20 29 3b 0a 20 20 70 54 6f 73  yn)==0 );.  pTos
1350b 2d 2d 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  --;.  break;.}..
1350c 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 69 66 6f 52  /* Opcode: FifoR
1350d 65 61 64 20 2a 20 50 32 20 2a 0a 2a 2a 0a 2a 2a  ead * P2 *.**.**
1350e 20 41 74 74 65 6d 70 74 20 74 6f 20 72 65 61 64   Attempt to read
1350f 20 61 20 73 69 6e 67 6c 65 20 69 6e 74 65 67 65   a single intege
13510 72 20 66 72 6f 6d 20 74 68 65 20 46 69 66 6f 0a  r from the Fifo.
13511 2a 2a 20 61 6e 64 20 70 75 73 68 20 69 74 20 6f  ** and push it o
13512 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 2e 20 20  nto the stack.  
13513 49 66 20 74 68 65 20 46 69 66 6f 20 69 73 20 65  If the Fifo is e
13514 6d 70 74 79 0a 2a 2a 20 70 75 73 68 20 6e 6f 74  mpty.** push not
13515 68 69 6e 67 20 62 75 74 20 69 6e 73 74 65 61 64  hing but instead
13516 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2f 0a   jump to P2..*/.
13517 63 61 73 65 20 4f 50 5f 46 69 66 6f 52 65 61 64  case OP_FifoRead
13518 3a 20 7b 0a 20 20 69 36 34 20 76 3b 0a 20 20 43  : {.  i64 v;.  C
13519 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55  HECK_FOR_INTERRU
1351a 50 54 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  PT;.  if( sqlite
1351b 33 56 64 62 65 46 69 66 6f 50 6f 70 28 26 70 2d  3VdbeFifoPop(&p-
1351c 3e 73 46 69 66 6f 2c 20 26 76 29 3d 3d 53 51 4c  >sFifo, &v)==SQL
1351d 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20  ITE_DONE ){.    
1351e 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31  pc = pOp->p2 - 1
1351f 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
13520 54 6f 73 2b 2b 3b 0a 20 20 20 20 70 54 6f 73 2d  Tos++;.    pTos-
13521 3e 75 2e 69 20 3d 20 76 3b 0a 20 20 20 20 70 54  >u.i = v;.    pT
13522 6f 73 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  os->flags = MEM_
13523 49 6e 74 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  Int;.  }.  break
13524 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
13525 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
13526 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6e 74  ./* Opcode: Cont
13527 65 78 74 50 75 73 68 20 2a 20 2a 20 2a 20 0a 2a  extPush * * * .*
13528 2a 0a 2a 2a 20 53 61 76 65 20 74 68 65 20 63 75  *.** Save the cu
13529 72 72 65 6e 74 20 56 64 62 65 20 63 6f 6e 74 65  rrent Vdbe conte
1352a 78 74 20 73 75 63 68 20 74 68 61 74 20 69 74 20  xt such that it 
1352b 63 61 6e 20 62 65 20 72 65 73 74 6f 72 65 64 20  can be restored 
1352c 62 79 20 61 20 43 6f 6e 74 65 78 74 50 6f 70 0a  by a ContextPop.
1352d 2a 2a 20 6f 70 63 6f 64 65 2e 20 54 68 65 20 63  ** opcode. The c
1352e 6f 6e 74 65 78 74 20 73 74 6f 72 65 73 20 74 68  ontext stores th
1352f 65 20 6c 61 73 74 20 69 6e 73 65 72 74 20 72 6f  e last insert ro
13530 77 20 69 64 2c 20 74 68 65 20 6c 61 73 74 20 73  w id, the last s
13531 74 61 74 65 6d 65 6e 74 20 63 68 61 6e 67 65 0a  tatement change.
13532 2a 2a 20 63 6f 75 6e 74 2c 20 61 6e 64 20 74 68  ** count, and th
13533 65 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 6d  e current statem
13534 65 6e 74 20 63 68 61 6e 67 65 20 63 6f 75 6e 74  ent change count
13535 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6e  ..*/.case OP_Con
13536 74 65 78 74 50 75 73 68 3a 20 7b 20 20 20 20 20  textPush: {     
13537 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f     /* no-push */
13538 0a 20 20 69 6e 74 20 69 20 3d 20 70 2d 3e 63 6f  .  int i = p->co
13539 6e 74 65 78 74 53 74 61 63 6b 54 6f 70 2b 2b 3b  ntextStackTop++;
1353a 0a 20 20 43 6f 6e 74 65 78 74 20 2a 70 43 6f 6e  .  Context *pCon
1353b 74 65 78 74 3b 0a 0a 20 20 61 73 73 65 72 74 28  text;..  assert(
1353c 20 69 3e 3d 30 20 29 3b 0a 20 20 2f 2a 20 46 49   i>=0 );.  /* FI
1353d 58 20 4d 45 3a 20 54 68 69 73 20 73 68 6f 75 6c  X ME: This shoul
1353e 64 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 61  d be allocated a
1353f 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 76 64  s part of the vd
13540 62 65 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69  be at compile-ti
13541 6d 65 20 2a 2f 0a 20 20 69 66 28 20 69 3e 3d 70  me */.  if( i>=p
13542 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 44 65  ->contextStackDe
13543 70 74 68 20 29 7b 0a 20 20 20 20 70 2d 3e 63 6f  pth ){.    p->co
13544 6e 74 65 78 74 53 74 61 63 6b 44 65 70 74 68 20  ntextStackDepth 
13545 3d 20 69 2b 31 3b 0a 20 20 20 20 70 2d 3e 63 6f  = i+1;.    p->co
13546 6e 74 65 78 74 53 74 61 63 6b 20 3d 20 73 71 6c  ntextStack = sql
13547 69 74 65 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65  iteReallocOrFree
13548 28 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b  (p->contextStack
13549 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1354a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1354b 20 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65              size
1354c 6f 66 28 43 6f 6e 74 65 78 74 29 2a 28 69 2b 31  of(Context)*(i+1
1354d 29 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 63  ));.    if( p->c
1354e 6f 6e 74 65 78 74 53 74 61 63 6b 3d 3d 30 20 29  ontextStack==0 )
1354f 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20   goto no_mem;.  
13550 7d 0a 20 20 70 43 6f 6e 74 65 78 74 20 3d 20 26  }.  pContext = &
13551 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 5b  p->contextStack[
13552 69 5d 3b 0a 20 20 70 43 6f 6e 74 65 78 74 2d 3e  i];.  pContext->
13553 6c 61 73 74 52 6f 77 69 64 20 3d 20 64 62 2d 3e  lastRowid = db->
13554 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 70 43 6f  lastRowid;.  pCo
13555 6e 74 65 78 74 2d 3e 6e 43 68 61 6e 67 65 20 3d  ntext->nChange =
13556 20 70 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20 20 70   p->nChange;.  p
13557 43 6f 6e 74 65 78 74 2d 3e 73 46 69 66 6f 20 3d  Context->sFifo =
13558 20 70 2d 3e 73 46 69 66 6f 3b 0a 20 20 73 71 6c   p->sFifo;.  sql
13559 69 74 65 33 56 64 62 65 46 69 66 6f 49 6e 69 74  ite3VdbeFifoInit
1355a 28 26 70 2d 3e 73 46 69 66 6f 29 3b 0a 20 20 62  (&p->sFifo);.  b
1355b 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1355c 64 65 3a 20 43 6f 6e 74 65 78 74 50 6f 70 20 2a  de: ContextPop *
1355d 20 2a 20 2a 20 0a 2a 2a 0a 2a 2a 20 52 65 73 74   * * .**.** Rest
1355e 6f 72 65 20 74 68 65 20 56 64 62 65 20 63 6f 6e  ore the Vdbe con
1355f 74 65 78 74 20 74 6f 20 74 68 65 20 73 74 61 74  text to the stat
13560 65 20 69 74 20 77 61 73 20 69 6e 20 77 68 65 6e  e it was in when
13561 20 63 6f 6e 74 65 78 74 50 75 73 68 20 77 61 73   contextPush was
13562 20 6c 61 73 74 0a 2a 2a 20 65 78 65 63 75 74 65   last.** execute
13563 64 2e 20 54 68 65 20 63 6f 6e 74 65 78 74 20 73  d. The context s
13564 74 6f 72 65 73 20 74 68 65 20 6c 61 73 74 20 69  tores the last i
13565 6e 73 65 72 74 20 72 6f 77 20 69 64 2c 20 74 68  nsert row id, th
13566 65 20 6c 61 73 74 20 73 74 61 74 65 6d 65 6e 74  e last statement
13567 0a 2a 2a 20 63 68 61 6e 67 65 20 63 6f 75 6e 74  .** change count
13568 2c 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e  , and the curren
13569 74 20 73 74 61 74 65 6d 65 6e 74 20 63 68 61 6e  t statement chan
1356a 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 63 61 73  ge count..*/.cas
1356b 65 20 4f 50 5f 43 6f 6e 74 65 78 74 50 6f 70 3a  e OP_ContextPop:
1356c 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d   {        /* no-
1356d 70 75 73 68 20 2a 2f 0a 20 20 43 6f 6e 74 65 78  push */.  Contex
1356e 74 20 2a 70 43 6f 6e 74 65 78 74 20 3d 20 26 70  t *pContext = &p
1356f 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 5b 2d  ->contextStack[-
13570 2d 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b  -p->contextStack
13571 54 6f 70 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  Top];.  assert( 
13572 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 54  p->contextStackT
13573 6f 70 3e 3d 30 20 29 3b 0a 20 20 64 62 2d 3e 6c  op>=0 );.  db->l
13574 61 73 74 52 6f 77 69 64 20 3d 20 70 43 6f 6e 74  astRowid = pCont
13575 65 78 74 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a  ext->lastRowid;.
13576 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 70    p->nChange = p
13577 43 6f 6e 74 65 78 74 2d 3e 6e 43 68 61 6e 67 65  Context->nChange
13578 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 46  ;.  sqlite3VdbeF
13579 69 66 6f 43 6c 65 61 72 28 26 70 2d 3e 73 46 69  ifoClear(&p->sFi
1357a 66 6f 29 3b 0a 20 20 70 2d 3e 73 46 69 66 6f 20  fo);.  p->sFifo 
1357b 3d 20 70 43 6f 6e 74 65 78 74 2d 3e 73 46 69 66  = pContext->sFif
1357c 6f 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65  o;.  break;.}.#e
1357d 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 65 66 20  ndif /* #ifndef 
1357e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
1357f 47 45 52 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64  GER */../* Opcod
13580 65 3a 20 4d 65 6d 53 74 6f 72 65 20 50 31 20 50  e: MemStore P1 P
13581 32 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20  2 *.**.** Write 
13582 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73  the top of the s
13583 74 61 63 6b 20 69 6e 74 6f 20 6d 65 6d 6f 72 79  tack into memory
13584 20 6c 6f 63 61 74 69 6f 6e 20 50 31 2e 0a 2a 2a   location P1..**
13585 20 50 31 20 73 68 6f 75 6c 64 20 62 65 20 61 20   P1 should be a 
13586 73 6d 61 6c 6c 20 69 6e 74 65 67 65 72 20 73 69  small integer si
13587 6e 63 65 20 73 70 61 63 65 20 69 73 20 61 6c 6c  nce space is all
13588 6f 63 61 74 65 64 0a 2a 2a 20 66 6f 72 20 61 6c  ocated.** for al
13589 6c 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f  l memory locatio
1358a 6e 73 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64  ns between 0 and
1358b 20 50 31 20 69 6e 63 6c 75 73 69 76 65 2e 0a 2a   P1 inclusive..*
1358c 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68 65 20 64  *.** After the d
1358d 61 74 61 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ata is stored in
1358e 20 74 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61   the memory loca
1358f 74 69 6f 6e 2c 20 74 68 65 0a 2a 2a 20 73 74 61  tion, the.** sta
13590 63 6b 20 69 73 20 70 6f 70 70 65 64 20 6f 6e 63  ck is popped onc
13591 65 20 69 66 20 50 32 20 69 73 20 31 2e 20 20 49  e if P2 is 1.  I
13592 66 20 50 32 20 69 73 20 7a 65 72 6f 2c 20 74 68  f P2 is zero, th
13593 65 6e 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e  en.** the origin
13594 61 6c 20 64 61 74 61 20 72 65 6d 61 69 6e 73 20  al data remains 
13595 6f 6e 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2f  on the stack..*/
13596 0a 63 61 73 65 20 4f 50 5f 4d 65 6d 53 74 6f 72  .case OP_MemStor
13597 65 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6e  e: {        /* n
13598 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 61 73 73 65  o-push */.  asse
13599 72 74 28 20 70 54 6f 73 3e 3d 70 2d 3e 61 53 74  rt( pTos>=p->aSt
1359a 61 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ack );.  assert(
1359b 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70   pOp->p1>=0 && p
1359c 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 4d 65 6d 20 29  Op->p1<p->nMem )
1359d 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
1359e 56 64 62 65 4d 65 6d 4d 6f 76 65 28 26 70 2d 3e  VdbeMemMove(&p->
1359f 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 2c 20 70  aMem[pOp->p1], p
135a0 54 6f 73 29 3b 0a 20 20 70 54 6f 73 2d 2d 3b 0a  Tos);.  pTos--;.
135a1 0a 20 20 2f 2a 20 49 66 20 50 32 20 69 73 20 30  .  /* If P2 is 0
135a2 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 75 20   then fall thru 
135a3 74 6f 20 74 68 65 20 6e 65 78 74 20 6f 70 63 6f  to the next opco
135a4 64 65 2c 20 4f 50 5f 4d 65 6d 4c 6f 61 64 2c 20  de, OP_MemLoad, 
135a5 74 68 61 74 20 77 69 6c 6c 0a 20 20 2a 2a 20 72  that will.  ** r
135a6 65 73 74 6f 72 65 20 74 68 65 20 74 6f 70 20 6f  estore the top o
135a7 66 20 74 68 65 20 73 74 61 63 6b 20 74 6f 20 69  f the stack to i
135a8 74 73 20 6f 72 69 67 69 6e 61 6c 20 76 61 6c 75  ts original valu
135a9 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4f  e..  */.  if( pO
135aa 70 2d 3e 70 32 20 29 7b 0a 20 20 20 20 62 72 65  p->p2 ){.    bre
135ab 61 6b 3b 0a 20 20 7d 0a 7d 0a 2f 2a 20 4f 70 63  ak;.  }.}./* Opc
135ac 6f 64 65 3a 20 4d 65 6d 4c 6f 61 64 20 50 31 20  ode: MemLoad P1 
135ad 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 75 73 68 20 61  * *.**.** Push a
135ae 20 63 6f 70 79 20 6f 66 20 74 68 65 20 76 61 6c   copy of the val
135af 75 65 20 69 6e 20 6d 65 6d 6f 72 79 20 6c 6f 63  ue in memory loc
135b0 61 74 69 6f 6e 20 50 31 20 6f 6e 74 6f 20 74 68  ation P1 onto th
135b1 65 20 73 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49  e stack..**.** I
135b2 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 61  f the value is a
135b3 20 73 74 72 69 6e 67 2c 20 74 68 65 6e 20 74 68   string, then th
135b4 65 20 76 61 6c 75 65 20 70 75 73 68 65 64 20 69  e value pushed i
135b5 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a  s a pointer to.*
135b6 2a 20 74 68 65 20 73 74 72 69 6e 67 20 74 68 61  * the string tha
135b7 74 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74  t is stored in t
135b8 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69  he memory locati
135b9 6f 6e 2e 20 20 49 66 20 74 68 65 20 6d 65 6d 6f  on.  If the memo
135ba 72 79 0a 2a 2a 20 6c 6f 63 61 74 69 6f 6e 20 69  ry.** location i
135bb 73 20 73 75 62 73 65 71 75 65 6e 74 6c 79 20 63  s subsequently c
135bc 68 61 6e 67 65 64 20 28 75 73 69 6e 67 20 4f 50  hanged (using OP
135bd 5f 4d 65 6d 53 74 6f 72 65 29 20 74 68 65 6e 20  _MemStore) then 
135be 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 70 75 73  the.** value pus
135bf 68 65 64 20 6f 6e 74 6f 20 74 68 65 20 73 74 61  hed onto the sta
135c0 63 6b 20 77 69 6c 6c 20 63 68 61 6e 67 65 20 74  ck will change t
135c1 6f 6f 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d  oo..*/.case OP_M
135c2 65 6d 4c 6f 61 64 3a 20 7b 0a 20 20 69 6e 74 20  emLoad: {.  int 
135c3 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 61  i = pOp->p1;.  a
135c4 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69  ssert( i>=0 && i
135c5 3c 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 54  <p->nMem );.  pT
135c6 6f 73 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 56  os++;.  sqlite3V
135c7 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70  dbeMemShallowCop
135c8 79 28 70 54 6f 73 2c 20 26 70 2d 3e 61 4d 65 6d  y(pTos, &p->aMem
135c9 5b 69 5d 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b  [i], MEM_Ephem);
135ca 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66  .  break;.}..#if
135cb 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
135cc 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 2f  _AUTOINCREMENT./
135cd 2a 20 4f 70 63 6f 64 65 3a 20 4d 65 6d 4d 61 78  * Opcode: MemMax
135ce 20 50 31 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 65   P1 * *.**.** Se
135cf 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6d  t the value of m
135d0 65 6d 6f 72 79 20 63 65 6c 6c 20 50 31 20 74 6f  emory cell P1 to
135d1 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 20   the maximum of 
135d2 69 74 73 20 63 75 72 72 65 6e 74 20 76 61 6c 75  its current valu
135d3 65 0a 2a 2a 20 61 6e 64 20 74 68 65 20 76 61 6c  e.** and the val
135d4 75 65 20 6f 6e 20 74 68 65 20 74 6f 70 20 6f 66  ue on the top of
135d5 20 74 68 65 20 73 74 61 63 6b 2e 20 20 54 68 65   the stack.  The
135d6 20 73 74 61 63 6b 20 69 73 20 75 6e 63 68 61 6e   stack is unchan
135d7 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ged..**.** This 
135d8 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 72 6f  instruction thro
135d9 77 73 20 61 6e 20 65 72 72 6f 72 20 69 66 20 74  ws an error if t
135da 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69  he memory cell i
135db 73 20 6e 6f 74 20 69 6e 69 74 69 61 6c 6c 79 0a  s not initially.
135dc 2a 2a 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a  ** an integer..*
135dd 2f 0a 63 61 73 65 20 4f 50 5f 4d 65 6d 4d 61 78  /.case OP_MemMax
135de 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f  : {        /* no
135df 2d 70 75 73 68 20 2a 2f 0a 20 20 69 6e 74 20 69  -push */.  int i
135e0 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 4d 65   = pOp->p1;.  Me
135e1 6d 20 2a 70 4d 65 6d 3b 0a 20 20 61 73 73 65 72  m *pMem;.  asser
135e2 74 28 20 70 54 6f 73 3e 3d 70 2d 3e 61 53 74 61  t( pTos>=p->aSta
135e3 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ck );.  assert( 
135e4 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 4d 65  i>=0 && i<p->nMe
135e5 6d 20 29 3b 0a 20 20 70 4d 65 6d 20 3d 20 26 70  m );.  pMem = &p
135e6 2d 3e 61 4d 65 6d 5b 69 5d 3b 0a 20 20 73 71 6c  ->aMem[i];.  sql
135e7 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67  ite3VdbeMemInteg
135e8 65 72 69 66 79 28 70 4d 65 6d 29 3b 0a 20 20 73  erify(pMem);.  s
135e9 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74  qlite3VdbeMemInt
135ea 65 67 65 72 69 66 79 28 70 54 6f 73 29 3b 0a 20  egerify(pTos);. 
135eb 20 69 66 28 20 70 4d 65 6d 2d 3e 75 2e 69 3c 70   if( pMem->u.i<p
135ec 54 6f 73 2d 3e 75 2e 69 29 7b 0a 20 20 20 20 70  Tos->u.i){.    p
135ed 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70 54 6f 73 2d  Mem->u.i = pTos-
135ee 3e 75 2e 69 3b 0a 20 20 7d 0a 20 20 62 72 65 61  >u.i;.  }.  brea
135ef 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  k;.}.#endif /* S
135f0 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49  QLITE_OMIT_AUTOI
135f1 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 0a 2f 2a 20  NCREMENT */../* 
135f2 4f 70 63 6f 64 65 3a 20 4d 65 6d 49 6e 63 72 20  Opcode: MemIncr 
135f3 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 49 6e  P1 P2 *.**.** In
135f4 63 72 65 6d 65 6e 74 20 74 68 65 20 69 6e 74 65  crement the inte
135f5 67 65 72 20 76 61 6c 75 65 64 20 6d 65 6d 6f 72  ger valued memor
135f6 79 20 63 65 6c 6c 20 50 32 20 62 79 20 74 68 65  y cell P2 by the
135f7 20 76 61 6c 75 65 20 69 6e 20 50 31 2e 0a 2a 2a   value in P1..**
135f8 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61  .** It is illega
135f9 6c 20 74 6f 20 75 73 65 20 74 68 69 73 20 69 6e  l to use this in
135fa 73 74 72 75 63 74 69 6f 6e 20 6f 6e 20 61 20 6d  struction on a m
135fb 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68 61 74 20  emory cell that 
135fc 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74  does.** not cont
135fd 61 69 6e 20 61 6e 20 69 6e 74 65 67 65 72 2e 20  ain an integer. 
135fe 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61   An assertion fa
135ff 75 6c 74 20 77 69 6c 6c 20 72 65 73 75 6c 74 20  ult will result 
13600 69 66 20 79 6f 75 20 74 72 79 2e 0a 2a 2f 0a 63  if you try..*/.c
13601 61 73 65 20 4f 50 5f 4d 65 6d 49 6e 63 72 3a 20  ase OP_MemIncr: 
13602 7b 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70  {        /* no-p
13603 75 73 68 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d  ush */.  int i =
13604 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 4d 65 6d 20   pOp->p2;.  Mem 
13605 2a 70 4d 65 6d 3b 0a 20 20 61 73 73 65 72 74 28  *pMem;.  assert(
13606 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 4d   i>=0 && i<p->nM
13607 65 6d 20 29 3b 0a 20 20 70 4d 65 6d 20 3d 20 26  em );.  pMem = &
13608 70 2d 3e 61 4d 65 6d 5b 69 5d 3b 0a 20 20 61 73  p->aMem[i];.  as
13609 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67  sert( pMem->flag
1360a 73 3d 3d 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20  s==MEM_Int );.  
1360b 70 4d 65 6d 2d 3e 75 2e 69 20 2b 3d 20 70 4f 70  pMem->u.i += pOp
1360c 2d 3e 70 31 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  ->p1;.  break;.}
1360d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 4d  ../* Opcode: IfM
1360e 65 6d 50 6f 73 20 50 31 20 50 32 20 2a 0a 2a 2a  emPos P1 P2 *.**
1360f 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65  .** If the value
13610 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20   of memory cell 
13611 50 31 20 69 73 20 31 20 6f 72 20 67 72 65 61 74  P1 is 1 or great
13612 65 72 2c 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a  er, jump to P2..
13613 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65  **.** It is ille
13614 67 61 6c 20 74 6f 20 75 73 65 20 74 68 69 73 20  gal to use this 
13615 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e 20 61  instruction on a
13616 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68 61   memory cell tha
13617 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f  t does.** not co
13618 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 67 65 72  ntain an integer
13619 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20  .  An assertion 
1361a 66 61 75 6c 74 20 77 69 6c 6c 20 72 65 73 75 6c  fault will resul
1361b 74 20 69 66 20 79 6f 75 20 74 72 79 2e 0a 2a 2f  t if you try..*/
1361c 0a 63 61 73 65 20 4f 50 5f 49 66 4d 65 6d 50 6f  .case OP_IfMemPo
1361d 73 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6e  s: {        /* n
1361e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 69 6e 74 20  o-push */.  int 
1361f 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 4d  i = pOp->p1;.  M
13620 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 61 73 73 65  em *pMem;.  asse
13621 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d  rt( i>=0 && i<p-
13622 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4d 65 6d 20  >nMem );.  pMem 
13623 3d 20 26 70 2d 3e 61 4d 65 6d 5b 69 5d 3b 0a 20  = &p->aMem[i];. 
13624 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66   assert( pMem->f
13625 6c 61 67 73 3d 3d 4d 45 4d 5f 49 6e 74 20 29 3b  lags==MEM_Int );
13626 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 75 2e 69  .  if( pMem->u.i
13627 3e 30 20 29 7b 0a 20 20 20 20 20 70 63 20 3d 20  >0 ){.     pc = 
13628 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d  pOp->p2 - 1;.  }
13629 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1362a 4f 70 63 6f 64 65 3a 20 49 66 4d 65 6d 4e 65 67  Opcode: IfMemNeg
1362b 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 49   P1 P2 *.**.** I
1362c 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6d  f the value of m
1362d 65 6d 6f 72 79 20 63 65 6c 6c 20 50 31 20 69 73  emory cell P1 is
1362e 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c   less than zero,
1362f 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20 0a 2a 2a   jump to P2. .**
13630 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61  .** It is illega
13631 6c 20 74 6f 20 75 73 65 20 74 68 69 73 20 69 6e  l to use this in
13632 73 74 72 75 63 74 69 6f 6e 20 6f 6e 20 61 20 6d  struction on a m
13633 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68 61 74 20  emory cell that 
13634 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74  does.** not cont
13635 61 69 6e 20 61 6e 20 69 6e 74 65 67 65 72 2e 20  ain an integer. 
13636 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61   An assertion fa
13637 75 6c 74 20 77 69 6c 6c 20 72 65 73 75 6c 74 20  ult will result 
13638 69 66 20 79 6f 75 20 74 72 79 2e 0a 2a 2f 0a 63  if you try..*/.c
13639 61 73 65 20 4f 50 5f 49 66 4d 65 6d 4e 65 67 3a  ase OP_IfMemNeg:
1363a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d   {        /* no-
1363b 70 75 73 68 20 2a 2f 0a 20 20 69 6e 74 20 69 20  push */.  int i 
1363c 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 4d 65 6d  = pOp->p1;.  Mem
1363d 20 2a 70 4d 65 6d 3b 0a 20 20 61 73 73 65 72 74   *pMem;.  assert
1363e 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e  ( i>=0 && i<p->n
1363f 4d 65 6d 20 29 3b 0a 20 20 70 4d 65 6d 20 3d 20  Mem );.  pMem = 
13640 26 70 2d 3e 61 4d 65 6d 5b 69 5d 3b 0a 20 20 61  &p->aMem[i];.  a
13641 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61  ssert( pMem->fla
13642 67 73 3d 3d 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20  gs==MEM_Int );. 
13643 20 69 66 28 20 70 4d 65 6d 2d 3e 75 2e 69 3c 30   if( pMem->u.i<0
13644 20 29 7b 0a 20 20 20 20 20 70 63 20 3d 20 70 4f   ){.     pc = pO
13645 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20  p->p2 - 1;.  }. 
13646 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
13647 63 6f 64 65 3a 20 49 66 4d 65 6d 5a 65 72 6f 20  code: IfMemZero 
13648 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 49 66  P1 P2 *.**.** If
13649 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6d 65   the value of me
1364a 6d 6f 72 79 20 63 65 6c 6c 20 50 31 20 69 73 20  mory cell P1 is 
1364b 65 78 61 63 74 6c 79 20 30 2c 20 6a 75 6d 70 20  exactly 0, jump 
1364c 74 6f 20 50 32 2e 20 0a 2a 2a 0a 2a 2a 20 49 74  to P2. .**.** It
1364d 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 75   is illegal to u
1364e 73 65 20 74 68 69 73 20 69 6e 73 74 72 75 63 74  se this instruct
1364f 69 6f 6e 20 6f 6e 20 61 20 6d 65 6d 6f 72 79 20  ion on a memory 
13650 63 65 6c 6c 20 74 68 61 74 20 64 6f 65 73 0a 2a  cell that does.*
13651 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 6e  * not contain an
13652 20 69 6e 74 65 67 65 72 2e 20 20 41 6e 20 61 73   integer.  An as
13653 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 69  sertion fault wi
13654 6c 6c 20 72 65 73 75 6c 74 20 69 66 20 79 6f 75  ll result if you
13655 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50   try..*/.case OP
13656 5f 49 66 4d 65 6d 5a 65 72 6f 3a 20 7b 20 20 20  _IfMemZero: {   
13657 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20       /* no-push 
13658 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70  */.  int i = pOp
13659 2d 3e 70 31 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65  ->p1;.  Mem *pMe
1365a 6d 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d  m;.  assert( i>=
1365b 30 20 26 26 20 69 3c 70 2d 3e 6e 4d 65 6d 20 29  0 && i<p->nMem )
1365c 3b 0a 20 20 70 4d 65 6d 20 3d 20 26 70 2d 3e 61  ;.  pMem = &p->a
1365d 4d 65 6d 5b 69 5d 3b 0a 20 20 61 73 73 65 72 74  Mem[i];.  assert
1365e 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3d 3d 4d  ( pMem->flags==M
1365f 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 66 28 20  EM_Int );.  if( 
13660 70 4d 65 6d 2d 3e 75 2e 69 3d 3d 30 20 29 7b 0a  pMem->u.i==0 ){.
13661 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70       pc = pOp->p
13662 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65  2 - 1;.  }.  bre
13663 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
13664 3a 20 4d 65 6d 4e 75 6c 6c 20 50 31 20 2a 20 2a  : MemNull P1 * *
13665 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65 20 61 20 4e  .**.** Store a N
13666 55 4c 4c 20 69 6e 20 6d 65 6d 6f 72 79 20 63 65  ULL in memory ce
13667 6c 6c 20 50 31 0a 2a 2f 0a 63 61 73 65 20 4f 50  ll P1.*/.case OP
13668 5f 4d 65 6d 4e 75 6c 6c 3a 20 7b 0a 20 20 61 73  _MemNull: {.  as
13669 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30  sert( pOp->p1>=0
1366a 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e   && pOp->p1<p->n
1366b 4d 65 6d 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  Mem );.  sqlite3
1366c 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 26  VdbeMemSetNull(&
1366d 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d  p->aMem[pOp->p1]
1366e 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  );.  break;.}../
1366f 2a 20 4f 70 63 6f 64 65 3a 20 4d 65 6d 49 6e 74  * Opcode: MemInt
13670 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 53   P1 P2 *.**.** S
13671 74 6f 72 65 20 74 68 65 20 69 6e 74 65 67 65 72  tore the integer
13672 20 76 61 6c 75 65 20 50 31 20 69 6e 20 6d 65 6d   value P1 in mem
13673 6f 72 79 20 63 65 6c 6c 20 50 32 2e 0a 2a 2f 0a  ory cell P2..*/.
13674 63 61 73 65 20 4f 50 5f 4d 65 6d 49 6e 74 3a 20  case OP_MemInt: 
13675 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  {.  assert( pOp-
13676 3e 70 32 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70  >p2>=0 && pOp->p
13677 32 3c 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 73  2<p->nMem );.  s
13678 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
13679 49 6e 74 36 34 28 26 70 2d 3e 61 4d 65 6d 5b 70  Int64(&p->aMem[p
1367a 4f 70 2d 3e 70 32 5d 2c 20 70 4f 70 2d 3e 70 31  Op->p2], pOp->p1
1367b 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  );.  break;.}../
1367c 2a 20 4f 70 63 6f 64 65 3a 20 4d 65 6d 4d 6f 76  * Opcode: MemMov
1367d 65 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20  e P1 P2 *.**.** 
1367e 4d 6f 76 65 20 74 68 65 20 63 6f 6e 74 65 6e 74  Move the content
1367f 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20   of memory cell 
13680 50 32 20 6f 76 65 72 20 74 6f 20 6d 65 6d 6f 72  P2 over to memor
13681 79 20 63 65 6c 6c 20 50 31 2e 0a 2a 2a 20 41 6e  y cell P1..** An
13682 79 20 70 72 69 6f 72 20 63 6f 6e 74 65 6e 74 20  y prior content 
13683 6f 66 20 50 31 20 69 73 20 65 72 61 73 65 64 2e  of P1 is erased.
13684 20 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 50 32    Memory cell P2
13685 20 69 73 20 6c 65 66 74 0a 2a 2a 20 63 6f 6e 74   is left.** cont
13686 61 69 6e 69 6e 67 20 61 20 4e 55 4c 4c 2e 0a 2a  aining a NULL..*
13687 2f 0a 63 61 73 65 20 4f 50 5f 4d 65 6d 4d 6f 76  /.case OP_MemMov
13688 65 3a 20 7b 0a 20 20 61 73 73 65 72 74 28 20 70  e: {.  assert( p
13689 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70  Op->p1>=0 && pOp
1368a 2d 3e 70 31 3c 70 2d 3e 6e 4d 65 6d 20 29 3b 0a  ->p1<p->nMem );.
1368b 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1368c 32 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 32 3c  2>=0 && pOp->p2<
1368d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 72 63 20  p->nMem );.  rc 
1368e 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  = sqlite3VdbeMem
1368f 4d 6f 76 65 28 26 70 2d 3e 61 4d 65 6d 5b 70 4f  Move(&p->aMem[pO
13690 70 2d 3e 70 31 5d 2c 20 26 70 2d 3e 61 4d 65 6d  p->p1], &p->aMem
13691 5b 70 4f 70 2d 3e 70 32 5d 29 3b 0a 20 20 62 72  [pOp->p2]);.  br
13692 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
13693 65 3a 20 41 67 67 53 74 65 70 20 50 31 20 50 32  e: AggStep P1 P2
13694 20 50 33 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74   P3.**.** Execut
13695 65 20 74 68 65 20 73 74 65 70 20 66 75 6e 63 74  e the step funct
13696 69 6f 6e 20 66 6f 72 20 61 6e 20 61 67 67 72 65  ion for an aggre
13697 67 61 74 65 2e 20 20 54 68 65 0a 2a 2a 20 66 75  gate.  The.** fu
13698 6e 63 74 69 6f 6e 20 68 61 73 20 50 32 20 61 72  nction has P2 ar
13699 67 75 6d 65 6e 74 73 2e 20 20 50 33 20 69 73 20  guments.  P3 is 
1369a 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
1369b 20 46 75 6e 63 44 65 66 0a 2a 2a 20 73 74 72 75   FuncDef.** stru
1369c 63 74 75 72 65 20 74 68 61 74 20 73 70 65 63 69  cture that speci
1369d 66 69 65 73 20 74 68 65 20 66 75 6e 63 74 69 6f  fies the functio
1369e 6e 2e 20 20 55 73 65 20 6d 65 6d 6f 72 79 20 6c  n.  Use memory l
1369f 6f 63 61 74 69 6f 6e 0a 2a 2a 20 50 31 20 61 73  ocation.** P1 as
136a0 20 74 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72   the accumulator
136a1 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 32 20 61  ..**.** The P2 a
136a2 72 67 75 6d 65 6e 74 73 20 61 72 65 20 70 6f 70  rguments are pop
136a3 70 65 64 20 66 72 6f 6d 20 74 68 65 20 73 74 61  ped from the sta
136a4 63 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41  ck..*/.case OP_A
136a5 67 67 53 74 65 70 3a 20 7b 20 20 20 20 20 20 20  ggStep: {       
136a6 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20   /* no-push */. 
136a7 20 69 6e 74 20 6e 20 3d 20 70 4f 70 2d 3e 70 32   int n = pOp->p2
136a8 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d 65 6d  ;.  int i;.  Mem
136a9 20 2a 70 4d 65 6d 2c 20 2a 70 52 65 63 3b 0a 20   *pMem, *pRec;. 
136aa 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
136ab 20 63 74 78 3b 0a 20 20 73 71 6c 69 74 65 33 5f   ctx;.  sqlite3_
136ac 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c 3b 0a 0a  value **apVal;..
136ad 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 30 20 29    assert( n>=0 )
136ae 3b 0a 20 20 70 52 65 63 20 3d 20 26 70 54 6f 73  ;.  pRec = &pTos
136af 5b 31 2d 6e 5d 3b 0a 20 20 61 73 73 65 72 74 28  [1-n];.  assert(
136b0 20 70 52 65 63 3e 3d 70 2d 3e 61 53 74 61 63 6b   pRec>=p->aStack
136b1 20 29 3b 0a 20 20 61 70 56 61 6c 20 3d 20 70 2d   );.  apVal = p-
136b2 3e 61 70 41 72 67 3b 0a 20 20 61 73 73 65 72 74  >apArg;.  assert
136b3 28 20 61 70 56 61 6c 20 7c 7c 20 6e 3d 3d 30 20  ( apVal || n==0 
136b4 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
136b5 6e 3b 20 69 2b 2b 2c 20 70 52 65 63 2b 2b 29 7b  n; i++, pRec++){
136b6 0a 20 20 20 20 61 70 56 61 6c 5b 69 5d 20 3d 20  .    apVal[i] = 
136b7 70 52 65 63 3b 0a 20 20 20 20 73 74 6f 72 65 54  pRec;.    storeT
136b8 79 70 65 49 6e 66 6f 28 70 52 65 63 2c 20 65 6e  ypeInfo(pRec, en
136b9 63 6f 64 69 6e 67 29 3b 0a 20 20 7d 0a 20 20 63  coding);.  }.  c
136ba 74 78 2e 70 46 75 6e 63 20 3d 20 28 46 75 6e 63  tx.pFunc = (Func
136bb 44 65 66 2a 29 70 4f 70 2d 3e 70 33 3b 0a 20 20  Def*)pOp->p3;.  
136bc 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e  assert( pOp->p1>
136bd 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d  =0 && pOp->p1<p-
136be 3e 6e 4d 65 6d 20 29 3b 0a 20 20 63 74 78 2e 70  >nMem );.  ctx.p
136bf 4d 65 6d 20 3d 20 70 4d 65 6d 20 3d 20 26 70 2d  Mem = pMem = &p-
136c0 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  >aMem[pOp->p1];.
136c1 20 20 70 4d 65 6d 2d 3e 6e 2b 2b 3b 0a 20 20 63    pMem->n++;.  c
136c2 74 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 4d  tx.s.flags = MEM
136c3 5f 4e 75 6c 6c 3b 0a 20 20 63 74 78 2e 73 2e 7a  _Null;.  ctx.s.z
136c4 20 3d 20 30 3b 0a 20 20 63 74 78 2e 73 2e 78 44   = 0;.  ctx.s.xD
136c5 65 6c 20 3d 20 30 3b 0a 20 20 63 74 78 2e 69 73  el = 0;.  ctx.is
136c6 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 63 74 78  Error = 0;.  ctx
136c7 2e 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 69 66  .pColl = 0;.  if
136c8 28 20 63 74 78 2e 70 46 75 6e 63 2d 3e 6e 65 65  ( ctx.pFunc->nee
136c9 64 43 6f 6c 6c 53 65 71 20 29 7b 0a 20 20 20 20  dCollSeq ){.    
136ca 61 73 73 65 72 74 28 20 70 4f 70 3e 70 2d 3e 61  assert( pOp>p->a
136cb 4f 70 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  Op );.    assert
136cc 28 20 70 4f 70 5b 2d 31 5d 2e 70 33 74 79 70 65  ( pOp[-1].p3type
136cd 3d 3d 50 33 5f 43 4f 4c 4c 53 45 51 20 29 3b 0a  ==P3_COLLSEQ );.
136ce 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b      assert( pOp[
136cf 2d 31 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 43  -1].opcode==OP_C
136d0 6f 6c 6c 53 65 71 20 29 3b 0a 20 20 20 20 63 74  ollSeq );.    ct
136d1 78 2e 70 43 6f 6c 6c 20 3d 20 28 43 6f 6c 6c 53  x.pColl = (CollS
136d2 65 71 20 2a 29 70 4f 70 5b 2d 31 5d 2e 70 33 3b  eq *)pOp[-1].p3;
136d3 0a 20 20 7d 0a 20 20 28 63 74 78 2e 70 46 75 6e  .  }.  (ctx.pFun
136d4 63 2d 3e 78 53 74 65 70 29 28 26 63 74 78 2c 20  c->xStep)(&ctx, 
136d5 6e 2c 20 61 70 56 61 6c 29 3b 0a 20 20 70 6f 70  n, apVal);.  pop
136d6 53 74 61 63 6b 28 26 70 54 6f 73 2c 20 6e 29 3b  Stack(&pTos, n);
136d7 0a 20 20 69 66 28 20 63 74 78 2e 69 73 45 72 72  .  if( ctx.isErr
136d8 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  or ){.    sqlite
136d9 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a  3SetString(&p->z
136da 45 72 72 4d 73 67 2c 20 73 71 6c 69 74 65 33 5f  ErrMsg, sqlite3_
136db 76 61 6c 75 65 5f 74 65 78 74 28 26 63 74 78 2e  value_text(&ctx.
136dc 73 29 2c 20 28 63 68 61 72 2a 29 30 29 3b 0a 20  s), (char*)0);. 
136dd 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
136de 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  RROR;.  }.  sqli
136df 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
136e0 65 28 26 63 74 78 2e 73 29 3b 0a 20 20 62 72 65  e(&ctx.s);.  bre
136e1 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
136e2 3a 20 41 67 67 46 69 6e 61 6c 20 50 31 20 50 32  : AggFinal P1 P2
136e3 20 50 33 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74   P3.**.** Execut
136e4 65 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 20  e the finalizer 
136e5 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 6e 20  function for an 
136e6 61 67 67 72 65 67 61 74 65 2e 20 20 50 31 20 69  aggregate.  P1 i
136e7 73 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20  s.** the memory 
136e8 6c 6f 63 61 74 69 6f 6e 20 74 68 61 74 20 69 73  location that is
136e9 20 74 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72   the accumulator
136ea 20 66 6f 72 20 74 68 65 20 61 67 67 72 65 67 61   for the aggrega
136eb 74 65 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 69 73 20  te..**.** P2 is 
136ec 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 72  the number of ar
136ed 67 75 6d 65 6e 74 73 20 74 68 61 74 20 74 68 65  guments that the
136ee 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 74   step function t
136ef 61 6b 65 73 20 61 6e 64 0a 2a 2a 20 50 33 20 69  akes and.** P3 i
136f0 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
136f1 68 65 20 46 75 6e 63 44 65 66 20 66 6f 72 20 74  he FuncDef for t
136f2 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54  his function.  T
136f3 68 65 20 50 32 0a 2a 2a 20 61 72 67 75 6d 65 6e  he P2.** argumen
136f4 74 20 69 73 20 6e 6f 74 20 75 73 65 64 20 62 79  t is not used by
136f5 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 20 20 49   this opcode.  I
136f6 74 20 69 73 20 6f 6e 6c 79 20 74 68 65 72 65 20  t is only there 
136f7 74 6f 20 64 69 73 61 6d 62 69 67 75 61 74 65 0a  to disambiguate.
136f8 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61  ** functions tha
136f9 74 20 63 61 6e 20 74 61 6b 65 20 76 61 72 79 69  t can take varyi
136fa 6e 67 20 6e 75 6d 62 65 72 73 20 6f 66 20 61 72  ng numbers of ar
136fb 67 75 6d 65 6e 74 73 2e 20 20 54 68 65 0a 2a 2a  guments.  The.**
136fc 20 50 33 20 61 72 67 75 6d 65 6e 74 20 69 73 20   P3 argument is 
136fd 6f 6e 6c 79 20 6e 65 65 64 65 64 20 66 6f 72 20  only needed for 
136fe 74 68 65 20 64 65 67 65 6e 65 72 61 74 65 20 63  the degenerate c
136ff 61 73 65 20 77 68 65 72 65 0a 2a 2a 20 74 68 65  ase where.** the
13700 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 77   step function w
13701 61 73 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c  as not previousl
13702 79 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 63 61 73  y called..*/.cas
13703 65 20 4f 50 5f 41 67 67 46 69 6e 61 6c 3a 20 7b  e OP_AggFinal: {
13704 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75          /* no-pu
13705 73 68 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65  sh */.  Mem *pMe
13706 6d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  m;.  assert( pOp
13707 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e  ->p1>=0 && pOp->
13708 70 31 3c 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  p1<p->nMem );.  
13709 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  pMem = &p->aMem[
1370a 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65  pOp->p1];.  asse
1370b 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73  rt( (pMem->flags
1370c 20 26 20 7e 28 4d 45 4d 5f 4e 75 6c 6c 7c 4d 45   & ~(MEM_Null|ME
1370d 4d 5f 41 67 67 29 29 3d 3d 30 20 29 3b 0a 20 20  M_Agg))==0 );.  
1370e 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
1370f 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 70 4d 65 6d  MemFinalize(pMem
13710 2c 20 28 46 75 6e 63 44 65 66 2a 29 70 4f 70 2d  , (FuncDef*)pOp-
13711 3e 70 33 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  >p3);.  if( rc==
13712 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 29 7b 0a  SQLITE_ERROR ){.
13713 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74      sqlite3SetSt
13714 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67  ring(&p->zErrMsg
13715 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  , sqlite3_value_
13716 74 65 78 74 28 70 4d 65 6d 29 2c 20 28 63 68 61  text(pMem), (cha
13717 72 2a 29 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28  r*)0);.  }.  if(
13718 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54   sqlite3VdbeMemT
13719 6f 6f 42 69 67 28 70 4d 65 6d 29 20 29 7b 0a 20  ooBig(pMem) ){. 
1371a 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b     goto too_big;
1371b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1371c 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
1371d 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55  QLITE_OMIT_VACUU
1371e 4d 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  M) && !defined(S
1371f 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43  QLITE_OMIT_ATTAC
13720 48 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 61  H)./* Opcode: Va
13721 63 75 75 6d 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  cuum * * *.**.**
13722 20 56 61 63 75 75 6d 20 74 68 65 20 65 6e 74 69   Vacuum the enti
13723 72 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68  re database.  Th
13724 69 73 20 6f 70 63 6f 64 65 20 77 69 6c 6c 20 63  is opcode will c
13725 61 75 73 65 20 6f 74 68 65 72 20 76 69 72 74 75  ause other virtu
13726 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 73 20 74  al.** machines t
13727 6f 20 62 65 20 63 72 65 61 74 65 64 20 61 6e 64  o be created and
13728 20 72 75 6e 2e 20 20 49 74 20 6d 61 79 20 6e 6f   run.  It may no
13729 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d  t be called from
1372a 20 77 69 74 68 69 6e 0a 2a 2a 20 61 20 74 72 61   within.** a tra
1372b 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73  nsaction..*/.cas
1372c 65 20 4f 50 5f 56 61 63 75 75 6d 3a 20 7b 20 20  e OP_Vacuum: {  
1372d 20 20 20 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68        /* no-push
1372e 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
1372f 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29  3SafetyOff(db) )
13730 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
13731 74 6f 5f 6d 69 73 75 73 65 3b 20 0a 20 20 72 63  to_misuse; .  rc
13732 20 3d 20 73 71 6c 69 74 65 33 52 75 6e 56 61 63   = sqlite3RunVac
13733 75 75 6d 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c  uum(&p->zErrMsg,
13734 20 64 62 29 3b 0a 20 20 69 66 28 20 73 71 6c 69   db);.  if( sqli
13735 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20  te3SafetyOn(db) 
13736 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
13737 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 62 72  _to_misuse;.  br
13738 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23  eak;.}.#endif..#
13739 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
1373a 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
1373b 55 4d 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49  UM)./* Opcode: I
1373c 6e 63 72 56 61 63 75 75 6d 20 50 31 20 50 32 20  ncrVacuum P1 P2 
1373d 2a 0a 2a 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20  *.**.** Perform 
1373e 61 20 73 69 6e 67 6c 65 20 73 74 65 70 20 6f 66  a single step of
1373f 20 74 68 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c   the incremental
13740 20 76 61 63 75 75 6d 20 70 72 6f 63 65 64 75 72   vacuum procedur
13741 65 20 6f 6e 0a 2a 2a 20 74 68 65 20 50 31 20 64  e on.** the P1 d
13742 61 74 61 62 61 73 65 2e 20 49 66 20 74 68 65 20  atabase. If the 
13743 76 61 63 75 75 6d 20 68 61 73 20 66 69 6e 69 73  vacuum has finis
13744 68 65 64 2c 20 6a 75 6d 70 20 74 6f 20 69 6e 73  hed, jump to ins
13745 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 50 32 2e 20  truction.** P2. 
13746 4f 74 68 65 72 77 69 73 65 2c 20 66 61 6c 6c 20  Otherwise, fall 
13747 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e  through to the n
13748 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  ext instruction.
13749 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 63 72  .*/.case OP_Incr
1374a 56 61 63 75 75 6d 3a 20 7b 20 20 20 20 20 20 20  Vacuum: {       
1374b 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20   /* no-push */. 
1374c 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 0a 20 20   Btree *pBt;..  
1374d 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e  assert( pOp->p1>
1374e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62  =0 && pOp->p1<db
1374f 2d 3e 6e 44 62 20 29 3b 0a 20 20 70 42 74 20 3d  ->nDb );.  pBt =
13750 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31   db->aDb[pOp->p1
13751 5d 2e 70 42 74 3b 0a 20 20 72 63 20 3d 20 73 71  ].pBt;.  rc = sq
13752 6c 69 74 65 33 42 74 72 65 65 49 6e 63 72 56 61  lite3BtreeIncrVa
13753 63 75 75 6d 28 70 42 74 29 3b 0a 20 20 69 66 28  cuum(pBt);.  if(
13754 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45   rc==SQLITE_DONE
13755 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70   ){.    pc = pOp
13756 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 72 63  ->p2 - 1;.    rc
13757 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
13758 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e  }.  break;.}.#en
13759 64 69 66 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  dif../* Opcode: 
1375a 45 78 70 69 72 65 20 50 31 20 2a 20 2a 0a 2a 2a  Expire P1 * *.**
1375b 0a 2a 2a 20 43 61 75 73 65 20 70 72 65 63 6f 6d  .** Cause precom
1375c 70 69 6c 65 64 20 73 74 61 74 65 6d 65 6e 74 73  piled statements
1375d 20 74 6f 20 62 65 63 6f 6d 65 20 65 78 70 69 72   to become expir
1375e 65 64 2e 20 41 6e 20 65 78 70 69 72 65 64 20 73  ed. An expired s
1375f 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 66 61 69 6c  tatement.** fail
13760 73 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20  s with an error 
13761 63 6f 64 65 20 6f 66 20 53 51 4c 49 54 45 5f 53  code of SQLITE_S
13762 43 48 45 4d 41 20 69 66 20 69 74 20 69 73 20 65  CHEMA if it is e
13763 76 65 72 20 65 78 65 63 75 74 65 64 20 0a 2a 2a  ver executed .**
13764 20 28 76 69 61 20 73 71 6c 69 74 65 33 5f 73 74   (via sqlite3_st
13765 65 70 28 29 29 2e 0a 2a 2a 20 0a 2a 2a 20 49 66  ep())..** .** If
13766 20 50 31 20 69 73 20 30 2c 20 74 68 65 6e 20 61   P1 is 0, then a
13767 6c 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ll SQL statement
13768 73 20 62 65 63 6f 6d 65 20 65 78 70 69 72 65 64  s become expired
13769 2e 20 49 66 20 50 31 20 69 73 20 6e 6f 6e 2d 7a  . If P1 is non-z
1376a 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 6f 6e 6c  ero,.** then onl
1376b 79 20 74 68 65 20 63 75 72 72 65 6e 74 6c 79 20  y the currently 
1376c 65 78 65 63 75 74 69 6e 67 20 73 74 61 74 65 6d  executing statem
1376d 65 6e 74 20 69 73 20 61 66 66 65 63 74 65 64 2e  ent is affected.
1376e 20 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 45 78 70   .*/.case OP_Exp
1376f 69 72 65 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a  ire: {        /*
13770 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 69 66   no-push */.  if
13771 28 20 21 70 4f 70 2d 3e 70 31 20 29 7b 0a 20 20  ( !pOp->p1 ){.  
13772 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 50    sqlite3ExpireP
13773 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74  reparedStatement
13774 73 28 64 62 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  s(db);.  }else{.
13775 20 20 20 20 70 2d 3e 65 78 70 69 72 65 64 20 3d      p->expired =
13776 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b   1;.  }.  break;
13777 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
13778 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
13779 41 43 48 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ACHE./* Opcode: 
1377a 54 61 62 6c 65 4c 6f 63 6b 20 50 31 20 50 32 20  TableLock P1 P2 
1377b 50 33 0a 2a 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20  P3.**.** Obtain 
1377c 61 20 6c 6f 63 6b 20 6f 6e 20 61 20 70 61 72 74  a lock on a part
1377d 69 63 75 6c 61 72 20 74 61 62 6c 65 2e 20 54 68  icular table. Th
1377e 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69  is instruction i
1377f 73 20 6f 6e 6c 79 20 75 73 65 64 20 77 68 65 6e  s only used when
13780 0a 2a 2a 20 74 68 65 20 73 68 61 72 65 64 2d 63  .** the shared-c
13781 61 63 68 65 20 66 65 61 74 75 72 65 20 69 73 20  ache feature is 
13782 65 6e 61 62 6c 65 64 2e 20 0a 2a 2a 0a 2a 2a 20  enabled. .**.** 
13783 49 66 20 50 31 20 69 73 20 6e 6f 74 20 6e 65 67  If P1 is not neg
13784 61 74 69 76 65 2c 20 74 68 65 6e 20 69 74 20 69  ative, then it i
13785 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74  s the index of t
13786 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 69  he database.** i
13787 6e 20 73 71 6c 69 74 65 33 2e 61 44 62 5b 5d 20  n sqlite3.aDb[] 
13788 61 6e 64 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20  and a read-lock 
13789 69 73 20 72 65 71 75 69 72 65 64 2e 20 49 66 20  is required. If 
1378a 50 31 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20  P1 is negative, 
1378b 61 20 0a 2a 2a 20 77 72 69 74 65 2d 6c 6f 63 6b  a .** write-lock
1378c 20 69 73 20 72 65 71 75 69 72 65 64 2e 20 49 6e   is required. In
1378d 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 69   this case the i
1378e 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61  ndex of the data
1378f 62 61 73 65 20 69 73 20 74 68 65 20 0a 2a 2a 20  base is the .** 
13790 61 62 73 6f 6c 75 74 65 20 76 61 6c 75 65 20 6f  absolute value o
13791 66 20 50 31 20 6d 69 6e 75 73 20 6f 6e 65 20 28  f P1 minus one (
13792 69 44 62 20 3d 20 61 62 73 28 50 31 29 20 2d 20  iDb = abs(P1) - 
13793 31 3b 29 20 61 6e 64 20 61 20 77 72 69 74 65 2d  1;) and a write-
13794 6c 6f 63 6b 20 69 73 0a 2a 2a 20 72 65 71 75 69  lock is.** requi
13795 72 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 50 32 20 63  red. .**.** P2 c
13796 6f 6e 74 61 69 6e 73 20 74 68 65 20 72 6f 6f 74  ontains the root
13797 2d 70 61 67 65 20 6f 66 20 74 68 65 20 74 61 62  -page of the tab
13798 6c 65 20 74 6f 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a  le to lock..**.*
13799 2a 20 50 33 20 63 6f 6e 74 61 69 6e 73 20 61 20  * P3 contains a 
1379a 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e  pointer to the n
1379b 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
1379c 20 62 65 69 6e 67 20 6c 6f 63 6b 65 64 2e 20 54   being locked. T
1379d 68 69 73 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 75  his is only.** u
1379e 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20  sed to generate 
1379f 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
137a0 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 63 61 6e   if the lock can
137a1 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2e  not be obtained.
137a2 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 61 62 6c  .*/.case OP_Tabl
137a3 65 4c 6f 63 6b 3a 20 7b 20 20 20 20 20 20 20 20  eLock: {        
137a4 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20  /* no-push */.  
137a5 69 6e 74 20 70 31 20 3d 20 70 4f 70 2d 3e 70 31  int p1 = pOp->p1
137a6 3b 20 0a 20 20 75 38 20 69 73 57 72 69 74 65 4c  ; .  u8 isWriteL
137a7 6f 63 6b 20 3d 20 28 70 31 3c 30 29 3b 0a 20 20  ock = (p1<0);.  
137a8 69 66 28 20 69 73 57 72 69 74 65 4c 6f 63 6b 20  if( isWriteLock 
137a9 29 7b 0a 20 20 20 20 70 31 20 3d 20 28 2d 31 2a  ){.    p1 = (-1*
137aa 70 31 29 2d 31 3b 0a 20 20 7d 0a 20 20 72 63 20  p1)-1;.  }.  rc 
137ab 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 6f  = sqlite3BtreeLo
137ac 63 6b 54 61 62 6c 65 28 64 62 2d 3e 61 44 62 5b  ckTable(db->aDb[
137ad 70 31 5d 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 32  p1].pBt, pOp->p2
137ae 2c 20 69 73 57 72 69 74 65 4c 6f 63 6b 29 3b 0a  , isWriteLock);.
137af 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
137b0 5f 4c 4f 43 4b 45 44 20 29 7b 0a 20 20 20 20 63  _LOCKED ){.    c
137b1 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 28  onst char *z = (
137b2 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 4f 70  const char *)pOp
137b3 2d 3e 70 33 3b 0a 20 20 20 20 73 71 6c 69 74 65  ->p3;.    sqlite
137b4 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a  3SetString(&p->z
137b5 45 72 72 4d 73 67 2c 20 22 64 61 74 61 62 61 73  ErrMsg, "databas
137b6 65 20 74 61 62 6c 65 20 69 73 20 6c 6f 63 6b 65  e table is locke
137b7 64 3a 20 22 2c 20 7a 2c 20 28 63 68 61 72 2a 29  d: ", z, (char*)
137b8 30 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  0);.  }.  break;
137b9 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
137ba 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
137bb 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65  CACHE */..#ifnde
137bc 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
137bd 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70  RTUALTABLE./* Op
137be 63 6f 64 65 3a 20 56 42 65 67 69 6e 20 2a 20 2a  code: VBegin * *
137bf 20 50 33 0a 2a 2a 0a 2a 2a 20 50 33 20 61 20 70   P3.**.** P3 a p
137c0 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 71 6c  ointer to an sql
137c1 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74  ite3_vtab struct
137c2 75 72 65 2e 20 43 61 6c 6c 20 74 68 65 20 78 42  ure. Call the xB
137c3 65 67 69 6e 20 6d 65 74 68 6f 64 20 0a 2a 2a 20  egin method .** 
137c4 66 6f 72 20 74 68 61 74 20 74 61 62 6c 65 2e 0a  for that table..
137c5 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 42 65 67 69  */.case OP_VBegi
137c6 6e 3a 20 7b 20 20 20 2f 2a 20 6e 6f 2d 70 75 73  n: {   /* no-pus
137c7 68 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69  h */.  rc = sqli
137c8 74 65 33 56 74 61 62 42 65 67 69 6e 28 64 62 2c  te3VtabBegin(db,
137c9 20 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a   (sqlite3_vtab *
137ca 29 70 4f 70 2d 3e 70 33 29 3b 0a 20 20 62 72 65  )pOp->p3);.  bre
137cb 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ak;.}.#endif /* 
137cc 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
137cd 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66  UALTABLE */..#if
137ce 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
137cf 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a  _VIRTUALTABLE./*
137d0 20 4f 70 63 6f 64 65 3a 20 56 43 72 65 61 74 65   Opcode: VCreate
137d1 20 50 31 20 2a 20 50 33 0a 2a 2a 0a 2a 2a 20 50   P1 * P3.**.** P
137d2 33 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  3 is the name of
137d3 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
137d4 20 69 6e 20 64 61 74 61 62 61 73 65 20 50 31 2e   in database P1.
137d5 20 43 61 6c 6c 20 74 68 65 20 78 43 72 65 61 74   Call the xCreat
137d6 65 20 6d 65 74 68 6f 64 0a 2a 2a 20 66 6f 72 20  e method.** for 
137d7 74 68 61 74 20 74 61 62 6c 65 2e 0a 2a 2f 0a 63  that table..*/.c
137d8 61 73 65 20 4f 50 5f 56 43 72 65 61 74 65 3a 20  ase OP_VCreate: 
137d9 7b 20 20 20 2f 2a 20 6e 6f 2d 70 75 73 68 20 2a  {   /* no-push *
137da 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  /.  rc = sqlite3
137db 56 74 61 62 43 61 6c 6c 43 72 65 61 74 65 28 64  VtabCallCreate(d
137dc 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d  b, pOp->p1, pOp-
137dd 3e 70 33 2c 20 26 70 2d 3e 7a 45 72 72 4d 73 67  >p3, &p->zErrMsg
137de 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65  );.  break;.}.#e
137df 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
137e0 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
137e1 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
137e2 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
137e3 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a  TABLE./* Opcode:
137e4 20 56 44 65 73 74 72 6f 79 20 50 31 20 2a 20 50   VDestroy P1 * P
137e5 33 0a 2a 2a 0a 2a 2a 20 50 33 20 69 73 20 74 68  3.**.** P3 is th
137e6 65 20 6e 61 6d 65 20 6f 66 20 61 20 76 69 72 74  e name of a virt
137e7 75 61 6c 20 74 61 62 6c 65 20 69 6e 20 64 61 74  ual table in dat
137e8 61 62 61 73 65 20 50 31 2e 20 20 43 61 6c 6c 20  abase P1.  Call 
137e9 74 68 65 20 78 44 65 73 74 72 6f 79 20 6d 65 74  the xDestroy met
137ea 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 61 74 20 74  hod.** of that t
137eb 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  able..*/.case OP
137ec 5f 56 44 65 73 74 72 6f 79 3a 20 7b 20 20 20 2f  _VDestroy: {   /
137ed 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 70  * no-push */.  p
137ee 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d  ->inVtabMethod =
137ef 20 32 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   2;.  rc = sqlit
137f0 65 33 56 74 61 62 43 61 6c 6c 44 65 73 74 72 6f  e3VtabCallDestro
137f1 79 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70  y(db, pOp->p1, p
137f2 4f 70 2d 3e 70 33 29 3b 0a 20 20 70 2d 3e 69 6e  Op->p3);.  p->in
137f3 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a  VtabMethod = 0;.
137f4 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69    break;.}.#endi
137f5 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
137f6 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f  _VIRTUALTABLE */
137f7 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
137f8 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
137f9 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 4f  LE./* Opcode: VO
137fa 70 65 6e 20 50 31 20 2a 20 50 33 0a 2a 2a 0a 2a  pen P1 * P3.**.*
137fb 2a 20 50 33 20 69 73 20 61 20 70 6f 69 6e 74 65  * P3 is a pointe
137fc 72 20 74 6f 20 61 20 76 69 72 74 75 61 6c 20 74  r to a virtual t
137fd 61 62 6c 65 20 6f 62 6a 65 63 74 2c 20 61 6e 20  able object, an 
137fe 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72  sqlite3_vtab str
137ff 75 63 74 75 72 65 2e 0a 2a 2a 20 50 31 20 69 73  ucture..** P1 is
13800 20 61 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72   a cursor number
13801 2e 20 20 54 68 69 73 20 6f 70 63 6f 64 65 20 6f  .  This opcode o
13802 70 65 6e 73 20 61 20 63 75 72 73 6f 72 20 74 6f  pens a cursor to
13803 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a 20   the virtual.** 
13804 74 61 62 6c 65 20 61 6e 64 20 73 74 6f 72 65 73  table and stores
13805 20 74 68 61 74 20 63 75 72 73 6f 72 20 69 6e 20   that cursor in 
13806 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56  P1..*/.case OP_V
13807 4f 70 65 6e 3a 20 7b 20 20 20 2f 2a 20 6e 6f 2d  Open: {   /* no-
13808 70 75 73 68 20 2a 2f 0a 20 20 43 75 72 73 6f 72  push */.  Cursor
13809 20 2a 70 43 75 72 20 3d 20 30 3b 0a 20 20 73 71   *pCur = 0;.  sq
1380a 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
1380b 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 20 3d  r *pVtabCursor =
1380c 20 30 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 76   0;..  sqlite3_v
1380d 74 61 62 20 2a 70 56 74 61 62 20 3d 20 28 73 71  tab *pVtab = (sq
1380e 6c 69 74 65 33 5f 76 74 61 62 20 2a 29 28 70 4f  lite3_vtab *)(pO
1380f 70 2d 3e 70 33 29 3b 0a 20 20 73 71 6c 69 74 65  p->p3);.  sqlite
13810 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c  3_module *pModul
13811 65 20 3d 20 28 73 71 6c 69 74 65 33 5f 6d 6f 64  e = (sqlite3_mod
13812 75 6c 65 20 2a 29 70 56 74 61 62 2d 3e 70 4d 6f  ule *)pVtab->pMo
13813 64 75 6c 65 3b 0a 0a 20 20 61 73 73 65 72 74 28  dule;..  assert(
13814 70 56 74 61 62 20 26 26 20 70 4d 6f 64 75 6c 65  pVtab && pModule
13815 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  );.  if( sqlite3
13816 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20  SafetyOff(db) ) 
13817 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
13818 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 72 63 20 3d  o_misuse;.  rc =
13819 20 70 4d 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e 28   pModule->xOpen(
1381a 70 56 74 61 62 2c 20 26 70 56 74 61 62 43 75 72  pVtab, &pVtabCur
1381b 73 6f 72 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  sor);.  if( sqli
1381c 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20  te3SafetyOn(db) 
1381d 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
1381e 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 69 66  _to_misuse;.  if
1381f 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20  ( SQLITE_OK==rc 
13820 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 69 74 69 61  ){.    /* Initia
13821 6c 69 73 65 20 73 71 6c 69 74 65 33 5f 76 74 61  lise sqlite3_vta
13822 62 5f 63 75 72 73 6f 72 20 62 61 73 65 20 63 6c  b_cursor base cl
13823 61 73 73 20 2a 2f 0a 20 20 20 20 70 56 74 61 62  ass */.    pVtab
13824 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 20 3d 20  Cursor->pVtab = 
13825 70 56 74 61 62 3b 0a 0a 20 20 20 20 2f 2a 20 49  pVtab;..    /* I
13826 6e 69 74 69 61 6c 69 73 65 20 76 64 62 65 20 63  nitialise vdbe c
13827 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 2a 2f 0a  ursor object */.
13828 20 20 20 20 70 43 75 72 20 3d 20 61 6c 6c 6f 63      pCur = alloc
13829 61 74 65 43 75 72 73 6f 72 28 70 2c 20 70 4f 70  ateCursor(p, pOp
1382a 2d 3e 70 31 2c 20 2d 31 29 3b 0a 20 20 20 20 69  ->p1, -1);.    i
1382b 66 28 20 70 43 75 72 20 29 7b 0a 20 20 20 20 20  f( pCur ){.     
1382c 20 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73   pCur->pVtabCurs
1382d 6f 72 20 3d 20 70 56 74 61 62 43 75 72 73 6f 72  or = pVtabCursor
1382e 3b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 70 4d  ;.      pCur->pM
1382f 6f 64 75 6c 65 20 3d 20 70 56 74 61 62 43 75 72  odule = pVtabCur
13830 73 6f 72 2d 3e 70 56 74 61 62 2d 3e 70 4d 6f 64  sor->pVtab->pMod
13831 75 6c 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ule;.    }else{.
13832 20 20 20 20 20 20 70 4d 6f 64 75 6c 65 2d 3e 78        pModule->x
13833 43 6c 6f 73 65 28 70 56 74 61 62 43 75 72 73 6f  Close(pVtabCurso
13834 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  r);.    }.  }.  
13835 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20  break;.}.#endif 
13836 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  /* SQLITE_OMIT_V
13837 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a  IRTUALTABLE */..
13838 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
13839 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1383a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 46 69 6c  ./* Opcode: VFil
1383b 74 65 72 20 50 31 20 50 32 20 50 33 0a 2a 2a 0a  ter P1 P2 P3.**.
1383c 2a 2a 20 50 31 20 69 73 20 61 20 63 75 72 73 6f  ** P1 is a curso
1383d 72 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20 56  r opened using V
1383e 4f 70 65 6e 2e 20 20 50 32 20 69 73 20 61 6e 20  Open.  P2 is an 
1383f 61 64 64 72 65 73 73 20 74 6f 20 6a 75 6d 70 20  address to jump 
13840 74 6f 20 69 66 0a 2a 2a 20 74 68 65 20 66 69 6c  to if.** the fil
13841 74 65 72 65 64 20 72 65 73 75 6c 74 20 73 65 74  tered result set
13842 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a   is empty..**.**
13843 20 50 33 20 69 73 20 65 69 74 68 65 72 20 4e 55   P3 is either NU
13844 4c 4c 20 6f 72 20 61 20 73 74 72 69 6e 67 20 74  LL or a string t
13845 68 61 74 20 77 61 73 20 67 65 6e 65 72 61 74 65  hat was generate
13846 64 20 62 79 20 74 68 65 20 78 42 65 73 74 49 6e  d by the xBestIn
13847 64 65 78 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f 66  dex.** method of
13848 20 74 68 65 20 6d 6f 64 75 6c 65 2e 20 20 54 68   the module.  Th
13849 65 20 69 6e 74 65 72 70 72 65 74 61 74 69 6f 6e  e interpretation
1384a 20 6f 66 20 74 68 65 20 50 33 20 73 74 72 69 6e   of the P3 strin
1384b 67 20 69 73 20 6c 65 66 74 0a 2a 2a 20 74 6f 20  g is left.** to 
1384c 74 68 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65  the module imple
1384d 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  mentation..**.**
1384e 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 6e 76   This opcode inv
1384f 6f 6b 65 73 20 74 68 65 20 78 46 69 6c 74 65 72  okes the xFilter
13850 20 6d 65 74 68 6f 64 20 6f 6e 20 74 68 65 20 76   method on the v
13851 69 72 74 75 61 6c 20 74 61 62 6c 65 20 73 70 65  irtual table spe
13852 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 50 31 2e  cified.** by P1.
13853 20 20 54 68 65 20 69 6e 74 65 67 65 72 20 71 75    The integer qu
13854 65 72 79 20 70 6c 61 6e 20 70 61 72 61 6d 65 74  ery plan paramet
13855 65 72 20 74 6f 20 78 46 69 6c 74 65 72 20 69 73  er to xFilter is
13856 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 0a   the top of the.
13857 2a 2a 20 73 74 61 63 6b 2e 20 20 4e 65 78 74 20  ** stack.  Next 
13858 64 6f 77 6e 20 6f 6e 20 74 68 65 20 73 74 61 63  down on the stac
13859 6b 20 69 73 20 74 68 65 20 61 72 67 63 20 70 61  k is the argc pa
1385a 72 61 6d 65 74 65 72 2e 20 20 42 65 6e 65 61 74  rameter.  Beneat
1385b 68 20 74 68 65 0a 2a 2a 20 6e 65 78 74 20 6f 66  h the.** next of
1385c 20 73 74 61 63 6b 20 61 72 65 20 61 72 67 63 20   stack are argc 
1385d 61 64 64 69 74 69 6f 6e 61 6c 20 70 61 72 61 6d  additional param
1385e 65 74 65 72 73 20 77 68 69 63 68 20 61 72 65 20  eters which are 
1385f 70 61 73 73 65 64 20 74 6f 0a 2a 2a 20 78 46 69  passed to.** xFi
13860 6c 74 65 72 20 61 73 20 61 72 67 76 2e 20 54 68  lter as argv. Th
13861 65 20 74 6f 70 6d 6f 73 74 20 70 61 72 61 6d 65  e topmost parame
13862 74 65 72 20 28 69 2e 65 2e 20 33 72 64 20 65 6c  ter (i.e. 3rd el
13863 65 6d 65 6e 74 20 70 6f 70 70 65 64 20 66 72 6f  ement popped fro
13864 6d 0a 2a 2a 20 74 68 65 20 73 74 61 63 6b 29 20  m.** the stack) 
13865 62 65 63 6f 6d 65 73 20 61 72 67 76 5b 61 72 67  becomes argv[arg
13866 63 2d 31 5d 20 77 68 65 6e 20 70 61 73 73 65 64  c-1] when passed
13867 20 74 6f 20 78 46 69 6c 74 65 72 2e 0a 2a 2a 0a   to xFilter..**.
13868 2a 2a 20 54 68 65 20 69 6e 74 65 67 65 72 20 71  ** The integer q
13869 75 65 72 79 20 70 6c 61 6e 20 70 61 72 61 6d 65  uery plan parame
1386a 74 65 72 2c 20 61 72 67 63 2c 20 61 6e 64 20 61  ter, argc, and a
1386b 6c 6c 20 61 72 67 76 20 73 74 61 63 6b 20 76 61  ll argv stack va
1386c 6c 75 65 73 20 0a 2a 2a 20 61 72 65 20 70 6f 70  lues .** are pop
1386d 70 65 64 20 66 72 6f 6d 20 74 68 65 20 73 74 61  ped from the sta
1386e 63 6b 20 62 65 66 6f 72 65 20 74 68 69 73 20 69  ck before this i
1386f 6e 73 74 72 75 63 74 69 6f 6e 20 63 6f 6d 70 6c  nstruction compl
13870 65 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 6a 75  etes..**.** A ju
13871 6d 70 20 69 73 20 6d 61 64 65 20 74 6f 20 50 32  mp is made to P2
13872 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 20 73   if the result s
13873 65 74 20 61 66 74 65 72 20 66 69 6c 74 65 72 69  et after filteri
13874 6e 67 20 77 6f 75 6c 64 20 62 65 20 0a 2a 2a 20  ng would be .** 
13875 65 6d 70 74 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f  empty..*/.case O
13876 50 5f 56 46 69 6c 74 65 72 3a 20 7b 20 20 20 2f  P_VFilter: {   /
13877 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 69  * no-push */.  i
13878 6e 74 20 6e 41 72 67 3b 0a 0a 20 20 63 6f 6e 73  nt nArg;..  cons
13879 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  t sqlite3_module
1387a 20 2a 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20 43 75   *pModule;..  Cu
1387b 72 73 6f 72 20 2a 70 43 75 72 20 3d 20 70 2d 3e  rsor *pCur = p->
1387c 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  apCsr[pOp->p1];.
1387d 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
1387e 70 56 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20  pVtabCursor );. 
1387f 20 70 4d 6f 64 75 6c 65 20 3d 20 70 43 75 72 2d   pModule = pCur-
13880 3e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56  >pVtabCursor->pV
13881 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 0a 20  tab->pModule;.. 
13882 20 2f 2a 20 47 72 61 62 20 74 68 65 20 69 6e 64   /* Grab the ind
13883 65 78 20 6e 75 6d 62 65 72 20 61 6e 64 20 61 72  ex number and ar
13884 67 63 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66  gc parameters of
13885 66 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65  f the top of the
13886 20 73 74 61 63 6b 2e 20 2a 2f 0a 20 20 61 73 73   stack. */.  ass
13887 65 72 74 28 20 28 26 70 54 6f 73 5b 2d 31 5d 29  ert( (&pTos[-1])
13888 3e 3d 70 2d 3e 61 53 74 61 63 6b 20 29 3b 0a 20  >=p->aStack );. 
13889 20 61 73 73 65 72 74 28 20 28 70 54 6f 73 5b 30   assert( (pTos[0
1388a 5d 2e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 29  ].flags&MEM_Int)
1388b 21 3d 30 20 26 26 20 70 54 6f 73 5b 2d 31 5d 2e  !=0 && pTos[-1].
1388c 66 6c 61 67 73 3d 3d 4d 45 4d 5f 49 6e 74 20 29  flags==MEM_Int )
1388d 3b 0a 20 20 6e 41 72 67 20 3d 20 70 54 6f 73 5b  ;.  nArg = pTos[
1388e 2d 31 5d 2e 75 2e 69 3b 0a 0a 20 20 2f 2a 20 49  -1].u.i;..  /* I
1388f 6e 76 6f 6b 65 20 74 68 65 20 78 46 69 6c 74 65  nvoke the xFilte
13890 72 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 7b 0a  r method */.  {.
13891 20 20 20 20 69 6e 74 20 72 65 73 20 3d 20 30 3b      int res = 0;
13892 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
13893 4d 65 6d 20 2a 2a 61 70 41 72 67 20 3d 20 70 2d  Mem **apArg = p-
13894 3e 61 70 41 72 67 3b 0a 20 20 20 20 66 6f 72 28  >apArg;.    for(
13895 69 20 3d 20 30 3b 20 69 3c 6e 41 72 67 3b 20 69  i = 0; i<nArg; i
13896 2b 2b 29 7b 0a 20 20 20 20 20 20 61 70 41 72 67  ++){.      apArg
13897 5b 69 5d 20 3d 20 26 70 54 6f 73 5b 69 2b 31 2d  [i] = &pTos[i+1-
13898 32 2d 6e 41 72 67 5d 3b 0a 20 20 20 20 20 20 73  2-nArg];.      s
13899 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 61 70 41  toreTypeInfo(apA
1389a 72 67 5b 69 5d 2c 20 30 29 3b 0a 20 20 20 20 7d  rg[i], 0);.    }
1389b 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ..    if( sqlite
1389c 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29  3SafetyOff(db) )
1389d 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1389e 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 70  to_misuse;.    p
1389f 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d  ->inVtabMethod =
138a0 20 31 3b 0a 20 20 20 20 72 63 20 3d 20 70 4d 6f   1;.    rc = pMo
138a1 64 75 6c 65 2d 3e 78 46 69 6c 74 65 72 28 70 43  dule->xFilter(pC
138a2 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2c  ur->pVtabCursor,
138a3 20 70 54 6f 73 2d 3e 75 2e 69 2c 20 70 4f 70 2d   pTos->u.i, pOp-
138a4 3e 70 33 2c 20 6e 41 72 67 2c 20 61 70 41 72 67  >p3, nArg, apArg
138a5 29 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 62  );.    p->inVtab
138a6 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 20 20  Method = 0;.    
138a7 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
138a8 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 73 20 3d  K ){.      res =
138a9 20 70 4d 6f 64 75 6c 65 2d 3e 78 45 6f 66 28 70   pModule->xEof(p
138aa 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72  Cur->pVtabCursor
138ab 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
138ac 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e   sqlite3SafetyOn
138ad 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72  (db) ) goto abor
138ae 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b  t_due_to_misuse;
138af 0a 0a 20 20 20 20 69 66 28 20 72 65 73 20 29 7b  ..    if( res ){
138b0 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  .      pc = pOp-
138b1 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20  >p2 - 1;.    }. 
138b2 20 7d 0a 0a 20 20 2f 2a 20 50 6f 70 20 74 68 65   }..  /* Pop the
138b3 20 69 6e 64 65 78 20 6e 75 6d 62 65 72 2c 20 61   index number, a
138b4 72 67 63 20 76 61 6c 75 65 20 61 6e 64 20 70 61  rgc value and pa
138b5 72 61 6d 65 74 65 72 73 20 6f 66 66 20 74 68 65  rameters off the
138b6 20 73 74 61 63 6b 20 2a 2f 0a 20 20 70 6f 70 53   stack */.  popS
138b7 74 61 63 6b 28 26 70 54 6f 73 2c 20 32 2b 6e 41  tack(&pTos, 2+nA
138b8 72 67 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  rg);.  break;.}.
138b9 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
138ba 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
138bb 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  LE */..#ifndef S
138bc 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
138bd 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64  ALTABLE./* Opcod
138be 65 3a 20 56 52 6f 77 69 64 20 50 31 20 2a 20 2a  e: VRowid P1 * *
138bf 0a 2a 2a 0a 2a 2a 20 50 75 73 68 20 61 6e 20 69  .**.** Push an i
138c0 6e 74 65 67 65 72 20 6f 6e 74 6f 20 74 68 65 20  nteger onto the 
138c1 73 74 61 63 6b 20 77 68 69 63 68 20 69 73 20 74  stack which is t
138c2 68 65 20 72 6f 77 69 64 20 6f 66 0a 2a 2a 20 74  he rowid of.** t
138c3 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65  he virtual-table
138c4 20 74 68 61 74 20 74 68 65 20 50 31 20 63 75 72   that the P1 cur
138c5 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20  sor is pointing 
138c6 74 6f 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56  to..*/.case OP_V
138c7 52 6f 77 69 64 3a 20 7b 0a 20 20 63 6f 6e 73 74  Rowid: {.  const
138c8 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
138c9 2a 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20 43 75 72  *pModule;..  Cur
138ca 73 6f 72 20 2a 70 43 75 72 20 3d 20 70 2d 3e 61  sor *pCur = p->a
138cb 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20  pCsr[pOp->p1];. 
138cc 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 70   assert( pCur->p
138cd 56 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 20  VtabCursor );.  
138ce 70 4d 6f 64 75 6c 65 20 3d 20 70 43 75 72 2d 3e  pModule = pCur->
138cf 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74  pVtabCursor->pVt
138d0 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 69  ab->pModule;.  i
138d1 66 28 20 70 4d 6f 64 75 6c 65 2d 3e 78 52 6f 77  f( pModule->xRow
138d2 69 64 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  id==0 ){.    sql
138d3 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70  ite3SetString(&p
138d4 2d 3e 7a 45 72 72 4d 73 67 2c 20 22 55 6e 73 75  ->zErrMsg, "Unsu
138d5 70 70 6f 72 74 65 64 20 6d 6f 64 75 6c 65 20 6f  pported module o
138d6 70 65 72 61 74 69 6f 6e 3a 20 78 52 6f 77 69 64  peration: xRowid
138d7 22 2c 20 30 29 3b 0a 20 20 20 20 72 63 20 3d 20  ", 0);.    rc = 
138d8 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
138d9 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 73 71 6c  } else {.    sql
138da 69 74 65 5f 69 6e 74 36 34 20 69 52 6f 77 3b 0a  ite_int64 iRow;.
138db 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
138dc 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20  SafetyOff(db) ) 
138dd 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
138de 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 72 63  o_misuse;.    rc
138df 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 52 6f 77   = pModule->xRow
138e0 69 64 28 70 43 75 72 2d 3e 70 56 74 61 62 43 75  id(pCur->pVtabCu
138e1 72 73 6f 72 2c 20 26 69 52 6f 77 29 3b 0a 20 20  rsor, &iRow);.  
138e2 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66    if( sqlite3Saf
138e3 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f  etyOn(db) ) goto
138e4 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69   abort_due_to_mi
138e5 73 75 73 65 3b 0a 0a 20 20 20 20 70 54 6f 73 2b  suse;..    pTos+
138e6 2b 3b 0a 20 20 20 20 70 54 6f 73 2d 3e 66 6c 61  +;.    pTos->fla
138e7 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20  gs = MEM_Int;.  
138e8 20 20 70 54 6f 73 2d 3e 75 2e 69 20 3d 20 69 52    pTos->u.i = iR
138e9 6f 77 3b 0a 20 20 7d 0a 0a 20 20 62 72 65 61 6b  ow;.  }..  break
138ea 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
138eb 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
138ec 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64  LTABLE */..#ifnd
138ed 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
138ee 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f  IRTUALTABLE./* O
138ef 70 63 6f 64 65 3a 20 56 43 6f 6c 75 6d 6e 20 50  pcode: VColumn P
138f0 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 50 75 73  1 P2 *.**.** Pus
138f1 68 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b  h onto the stack
138f2 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
138f3 65 20 50 32 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f  e P2-th column o
138f4 66 0a 2a 2a 20 74 68 65 20 72 6f 77 20 6f 66 20  f.** the row of 
138f5 74 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c  the virtual-tabl
138f6 65 20 74 68 61 74 20 74 68 65 20 50 31 20 63 75  e that the P1 cu
138f7 72 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67  rsor is pointing
138f8 20 74 6f 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f   to..*/.case OP_
138f9 56 43 6f 6c 75 6d 6e 3a 20 7b 0a 20 20 63 6f 6e  VColumn: {.  con
138fa 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  st sqlite3_modul
138fb 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20 43  e *pModule;..  C
138fc 75 72 73 6f 72 20 2a 70 43 75 72 20 3d 20 70 2d  ursor *pCur = p-
138fd 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b  >apCsr[pOp->p1];
138fe 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
138ff 3e 70 56 74 61 62 43 75 72 73 6f 72 20 29 3b 0a  >pVtabCursor );.
13900 20 20 70 4d 6f 64 75 6c 65 20 3d 20 70 43 75 72    pModule = pCur
13901 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70  ->pVtabCursor->p
13902 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20  Vtab->pModule;. 
13903 20 69 66 28 20 70 4d 6f 64 75 6c 65 2d 3e 78 43   if( pModule->xC
13904 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20 20  olumn==0 ){.    
13905 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67  sqlite3SetString
13906 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 22 55  (&p->zErrMsg, "U
13907 6e 73 75 70 70 6f 72 74 65 64 20 6d 6f 64 75 6c  nsupported modul
13908 65 20 6f 70 65 72 61 74 69 6f 6e 3a 20 78 43 6f  e operation: xCo
13909 6c 75 6d 6e 22 2c 20 30 29 3b 0a 20 20 20 20 72  lumn", 0);.    r
1390a 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
1390b 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20  ;.  } else {.   
1390c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
1390d 20 73 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 6d   sContext;.    m
1390e 65 6d 73 65 74 28 26 73 43 6f 6e 74 65 78 74 2c  emset(&sContext,
1390f 20 30 2c 20 73 69 7a 65 6f 66 28 73 43 6f 6e 74   0, sizeof(sCont
13910 65 78 74 29 29 3b 0a 20 20 20 20 73 43 6f 6e 74  ext));.    sCont
13911 65 78 74 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45  ext.s.flags = ME
13912 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 69 66 28 20  M_Null;.    if( 
13913 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
13914 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72  (db) ) goto abor
13915 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b  t_due_to_misuse;
13916 0a 20 20 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c  .    rc = pModul
13917 65 2d 3e 78 43 6f 6c 75 6d 6e 28 70 43 75 72 2d  e->xColumn(pCur-
13918 3e 70 56 74 61 62 43 75 72 73 6f 72 2c 20 26 73  >pVtabCursor, &s
13919 43 6f 6e 74 65 78 74 2c 20 70 4f 70 2d 3e 70 32  Context, pOp->p2
1391a 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f 70 79 20  );..    /* Copy 
1391b 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68  the result of th
1391c 65 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 74 68  e function to th
1391d 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61  e top of the sta
1391e 63 6b 2e 20 57 65 0a 20 20 20 20 2a 2a 20 64 6f  ck. We.    ** do
1391f 20 74 68 69 73 20 72 65 67 61 72 64 6c 65 73 73   this regardless
13920 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72 20 6e   of whether or n
13921 6f 74 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  ot an error occu
13922 72 65 64 20 74 6f 20 65 6e 73 75 72 65 20 61 6e  red to ensure an
13923 79 0a 20 20 20 20 2a 2a 20 64 79 6e 61 6d 69 63  y.    ** dynamic
13924 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 73   allocation in s
13925 43 6f 6e 74 65 78 74 2e 73 20 28 61 20 4d 65 6d  Context.s (a Mem
13926 20 73 74 72 75 63 74 29 20 69 73 20 20 72 65 6c   struct) is  rel
13927 65 61 73 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20  eased..    */.  
13928 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
13929 6e 67 65 45 6e 63 6f 64 69 6e 67 28 26 73 43 6f  ngeEncoding(&sCo
1392a 6e 74 65 78 74 2e 73 2c 20 65 6e 63 6f 64 69 6e  ntext.s, encodin
1392b 67 29 3b 0a 20 20 20 20 70 54 6f 73 2b 2b 3b 0a  g);.    pTos++;.
1392c 20 20 20 20 70 54 6f 73 2d 3e 66 6c 61 67 73 20      pTos->flags 
1392d 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  = 0;.    sqlite3
1392e 56 64 62 65 4d 65 6d 4d 6f 76 65 28 70 54 6f 73  VdbeMemMove(pTos
1392f 2c 20 26 73 43 6f 6e 74 65 78 74 2e 73 29 3b 0a  , &sContext.s);.
13930 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
13931 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a  SafetyOn(db) ){.
13932 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74        goto abort
13933 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a  _due_to_misuse;.
13934 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71      }.    if( sq
13935 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42  lite3VdbeMemTooB
13936 69 67 28 70 54 6f 73 29 20 29 7b 0a 20 20 20 20  ig(pTos) ){.    
13937 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a    goto too_big;.
13938 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 62      }.  }.  .  b
13939 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  reak;.}.#endif /
1393a 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  * SQLITE_OMIT_VI
1393b 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23  RTUALTABLE */..#
1393c 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1393d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
1393e 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 4e 65 78 74  /* Opcode: VNext
1393f 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 41   P1 P2 *.**.** A
13940 64 76 61 6e 63 65 20 76 69 72 74 75 61 6c 20 74  dvance virtual t
13941 61 62 6c 65 20 50 31 20 74 6f 20 74 68 65 20 6e  able P1 to the n
13942 65 78 74 20 72 6f 77 20 69 6e 20 69 74 73 20 72  ext row in its r
13943 65 73 75 6c 74 20 73 65 74 20 61 6e 64 0a 2a 2a  esult set and.**
13944 20 6a 75 6d 70 20 74 6f 20 69 6e 73 74 72 75 63   jump to instruc
13945 74 69 6f 6e 20 50 32 2e 20 20 4f 72 2c 20 69 66  tion P2.  Or, if
13946 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
13947 6c 65 20 68 61 73 20 72 65 61 63 68 65 64 0a 2a  le has reached.*
13948 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 69 74 73  * the end of its
13949 20 72 65 73 75 6c 74 20 73 65 74 2c 20 74 68 65   result set, the
1394a 6e 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74  n fall through t
1394b 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72  o the next instr
1394c 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20  uction..*/.case 
1394d 4f 50 5f 56 4e 65 78 74 3a 20 7b 20 20 20 2f 2a  OP_VNext: {   /*
1394e 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 63 6f   no-push */.  co
1394f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  nst sqlite3_modu
13950 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 69  le *pModule;.  i
13951 6e 74 20 72 65 73 20 3d 20 30 3b 0a 0a 20 20 43  nt res = 0;..  C
13952 75 72 73 6f 72 20 2a 70 43 75 72 20 3d 20 70 2d  ursor *pCur = p-
13953 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b  >apCsr[pOp->p1];
13954 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
13955 3e 70 56 74 61 62 43 75 72 73 6f 72 20 29 3b 0a  >pVtabCursor );.
13956 20 20 70 4d 6f 64 75 6c 65 20 3d 20 70 43 75 72    pModule = pCur
13957 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70  ->pVtabCursor->p
13958 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20  Vtab->pModule;. 
13959 20 69 66 28 20 70 4d 6f 64 75 6c 65 2d 3e 78 4e   if( pModule->xN
1395a 65 78 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  ext==0 ){.    sq
1395b 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26  lite3SetString(&
1395c 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 22 55 6e 73  p->zErrMsg, "Uns
1395d 75 70 70 6f 72 74 65 64 20 6d 6f 64 75 6c 65 20  upported module 
1395e 6f 70 65 72 61 74 69 6f 6e 3a 20 78 4e 65 78 74  operation: xNext
1395f 22 2c 20 30 29 3b 0a 20 20 20 20 72 63 20 3d 20  ", 0);.    rc = 
13960 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
13961 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 2f 2a 20  } else {.    /* 
13962 49 6e 76 6f 6b 65 20 74 68 65 20 78 4e 65 78 74  Invoke the xNext
13963 28 29 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65  () method of the
13964 20 6d 6f 64 75 6c 65 2e 20 54 68 65 72 65 20 69   module. There i
13965 73 20 6e 6f 20 77 61 79 20 66 6f 72 20 74 68 65  s no way for the
13966 0a 20 20 20 20 2a 2a 20 75 6e 64 65 72 6c 79 69  .    ** underlyi
13967 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ng implementatio
13968 6e 20 74 6f 20 72 65 74 75 72 6e 20 61 6e 20 65  n to return an e
13969 72 72 6f 72 20 69 66 20 6f 6e 65 20 6f 63 63 75  rror if one occu
1396a 72 73 20 64 75 72 69 6e 67 0a 20 20 20 20 2a 2a  rs during.    **
1396b 20 78 4e 65 78 74 28 29 2e 20 49 6e 73 74 65 61   xNext(). Instea
1396c 64 2c 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f  d, if an error o
1396d 63 63 75 72 73 2c 20 74 72 75 65 20 69 73 20 72  ccurs, true is r
1396e 65 74 75 72 6e 65 64 20 28 69 6e 64 69 63 61 74  eturned (indicat
1396f 69 6e 67 20 74 68 61 74 20 0a 20 20 20 20 2a 2a  ing that .    **
13970 20 64 61 74 61 20 69 73 20 61 76 61 69 6c 61 62   data is availab
13971 6c 65 29 20 61 6e 64 20 74 68 65 20 65 72 72 6f  le) and the erro
13972 72 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 20  r code returned 
13973 77 68 65 6e 20 78 43 6f 6c 75 6d 6e 20 6f 72 0a  when xColumn or.
13974 20 20 20 20 2a 2a 20 73 6f 6d 65 20 6f 74 68 65      ** some othe
13975 72 20 6d 65 74 68 6f 64 20 69 73 20 6e 65 78 74  r method is next
13976 20 69 6e 76 6f 6b 65 64 20 6f 6e 20 74 68 65 20   invoked on the 
13977 73 61 76 65 20 76 69 72 74 75 61 6c 20 74 61 62  save virtual tab
13978 6c 65 20 63 75 72 73 6f 72 2e 0a 20 20 20 20 2a  le cursor..    *
13979 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  /.    if( sqlite
1397a 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29  3SafetyOff(db) )
1397b 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1397c 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 70  to_misuse;.    p
1397d 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d  ->inVtabMethod =
1397e 20 31 3b 0a 20 20 20 20 72 63 20 3d 20 70 4d 6f   1;.    rc = pMo
1397f 64 75 6c 65 2d 3e 78 4e 65 78 74 28 70 43 75 72  dule->xNext(pCur
13980 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a  ->pVtabCursor);.
13981 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74      p->inVtabMet
13982 68 6f 64 20 3d 20 30 3b 0a 20 20 20 20 69 66 28  hod = 0;.    if(
13983 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
13984 7b 0a 20 20 20 20 20 20 72 65 73 20 3d 20 70 4d  {.      res = pM
13985 6f 64 75 6c 65 2d 3e 78 45 6f 66 28 70 43 75 72  odule->xEof(pCur
13986 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a  ->pVtabCursor);.
13987 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71      }.    if( sq
13988 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62  lite3SafetyOn(db
13989 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64  ) ) goto abort_d
1398a 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a 20  ue_to_misuse;.. 
1398b 20 20 20 69 66 28 20 21 72 65 73 20 29 7b 0a 20     if( !res ){. 
1398c 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65       /* If there
1398d 20 69 73 20 64 61 74 61 2c 20 6a 75 6d 70 20 74   is data, jump t
1398e 6f 20 50 32 20 2a 2f 0a 20 20 20 20 20 20 70 63  o P2 */.      pc
1398f 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
13990 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 62 72 65      }.  }..  bre
13991 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ak;.}.#endif /* 
13992 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
13993 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66  UALTABLE */..#if
13994 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
13995 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a  _VIRTUALTABLE./*
13996 20 4f 70 63 6f 64 65 3a 20 56 52 65 6e 61 6d 65   Opcode: VRename
13997 20 2a 20 2a 20 50 33 0a 2a 2a 0a 2a 2a 20 50 33   * * P3.**.** P3
13998 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
13999 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
1399a 20 6f 62 6a 65 63 74 2c 20 61 6e 20 73 71 6c 69   object, an sqli
1399b 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75  te3_vtab structu
1399c 72 65 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f  re..** This opco
1399d 64 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 63  de invokes the c
1399e 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 78 52 65  orresponding xRe
1399f 6e 61 6d 65 20 6d 65 74 68 6f 64 2e 20 54 68 65  name method. The
139a0 20 76 61 6c 75 65 0a 2a 2a 20 6f 6e 20 74 68 65   value.** on the
139a1 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63   top of the stac
139a2 6b 20 69 73 20 70 6f 70 70 65 64 20 61 6e 64 20  k is popped and 
139a3 70 61 73 73 65 64 20 61 73 20 74 68 65 20 7a 4e  passed as the zN
139a4 61 6d 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  ame argument.** 
139a5 74 6f 20 74 68 65 20 78 52 65 6e 61 6d 65 20 6d  to the xRename m
139a6 65 74 68 6f 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f  ethod..*/.case O
139a7 50 5f 56 52 65 6e 61 6d 65 3a 20 7b 20 20 20 2f  P_VRename: {   /
139a8 2a 20 6e 6f 2d 70 75 73 68 20 2a 2f 0a 20 20 73  * no-push */.  s
139a9 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
139aa 61 62 20 3d 20 28 73 71 6c 69 74 65 33 5f 76 74  ab = (sqlite3_vt
139ab 61 62 20 2a 29 28 70 4f 70 2d 3e 70 33 29 3b 0a  ab *)(pOp->p3);.
139ac 20 20 61 73 73 65 72 74 28 20 70 56 74 61 62 2d    assert( pVtab-
139ad 3e 70 4d 6f 64 75 6c 65 2d 3e 78 52 65 6e 61 6d  >pModule->xRenam
139ae 65 20 29 3b 0a 0a 20 20 53 74 72 69 6e 67 69 66  e );..  Stringif
139af 79 28 70 54 6f 73 2c 20 65 6e 63 6f 64 69 6e 67  y(pTos, encoding
139b0 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65  );..  if( sqlite
139b1 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29  3SafetyOff(db) )
139b2 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
139b3 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 73 71 6c  to_misuse;.  sql
139b4 69 74 65 33 56 74 61 62 4c 6f 63 6b 28 70 56 74  ite3VtabLock(pVt
139b5 61 62 29 3b 0a 20 20 72 63 20 3d 20 70 56 74 61  ab);.  rc = pVta
139b6 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 52 65 6e  b->pModule->xRen
139b7 61 6d 65 28 70 56 74 61 62 2c 20 70 54 6f 73 2d  ame(pVtab, pTos-
139b8 3e 7a 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 74  >z);.  sqlite3Vt
139b9 61 62 55 6e 6c 6f 63 6b 28 64 62 2c 20 70 56 74  abUnlock(db, pVt
139ba 61 62 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  ab);.  if( sqlit
139bb 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29  e3SafetyOn(db) )
139bc 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
139bd 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 70 6f  to_misuse;..  po
139be 70 53 74 61 63 6b 28 26 70 54 6f 73 2c 20 31 29  pStack(&pTos, 1)
139bf 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e  ;.  break;.}.#en
139c0 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
139c1 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
139c2 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a  TABLE./* Opcode:
139c3 20 56 55 70 64 61 74 65 20 50 31 20 50 32 20 50   VUpdate P1 P2 P
139c4 33 0a 2a 2a 0a 2a 2a 20 50 33 20 69 73 20 61 20  3.**.** P3 is a 
139c5 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 76 69 72  pointer to a vir
139c6 74 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63  tual table objec
139c7 74 2c 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74  t, an sqlite3_vt
139c8 61 62 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  ab structure..**
139c9 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 6e 76   This opcode inv
139ca 6f 6b 65 73 20 74 68 65 20 63 6f 72 72 65 73 70  okes the corresp
139cb 6f 6e 64 69 6e 67 20 78 55 70 64 61 74 65 20 6d  onding xUpdate m
139cc 65 74 68 6f 64 2e 20 50 32 20 76 61 6c 75 65 73  ethod. P2 values
139cd 0a 2a 2a 20 61 72 65 20 74 61 6b 65 6e 20 66 72  .** are taken fr
139ce 6f 6d 20 74 68 65 20 73 74 61 63 6b 20 74 6f 20  om the stack to 
139cf 70 61 73 73 20 74 6f 20 74 68 65 20 78 55 70 64  pass to the xUpd
139d0 61 74 65 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 20  ate invocation. 
139d1 54 68 65 0a 2a 2a 20 76 61 6c 75 65 20 6f 6e 20  The.** value on 
139d2 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73  the top of the s
139d3 74 61 63 6b 20 63 6f 72 72 65 73 70 6f 6e 64 73  tack corresponds
139d4 20 74 6f 20 74 68 65 20 70 32 74 68 20 65 6c 65   to the p2th ele
139d5 6d 65 6e 74 20 0a 2a 2a 20 6f 66 20 74 68 65 20  ment .** of the 
139d6 61 72 67 76 20 61 72 72 61 79 20 70 61 73 73 65  argv array passe
139d7 64 20 74 6f 20 78 55 70 64 61 74 65 2e 0a 2a 2a  d to xUpdate..**
139d8 0a 2a 2a 20 54 68 65 20 78 55 70 64 61 74 65 20  .** The xUpdate 
139d9 6d 65 74 68 6f 64 20 77 69 6c 6c 20 64 6f 20 61  method will do a
139da 20 44 45 4c 45 54 45 20 6f 72 20 61 6e 20 49 4e   DELETE or an IN
139db 53 45 52 54 20 6f 72 20 62 6f 74 68 2e 0a 2a 2a  SERT or both..**
139dc 20 54 68 65 20 61 72 67 76 5b 30 5d 20 65 6c 65   The argv[0] ele
139dd 6d 65 6e 74 20 28 77 68 69 63 68 20 63 6f 72 72  ment (which corr
139de 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 50  esponds to the P
139df 32 2d 74 68 20 65 6c 65 6d 65 6e 74 20 64 6f 77  2-th element dow
139e0 6e 0a 2a 2a 20 6f 6e 20 74 68 65 20 73 74 61 63  n.** on the stac
139e1 6b 29 20 69 73 20 74 68 65 20 72 6f 77 69 64 20  k) is the rowid 
139e2 6f 66 20 61 20 72 6f 77 20 74 6f 20 64 65 6c 65  of a row to dele
139e3 74 65 2e 20 20 49 66 20 61 72 67 76 5b 30 5d 20  te.  If argv[0] 
139e4 69 73 0a 2a 2a 20 4e 55 4c 4c 20 74 68 65 6e 20  is.** NULL then 
139e5 6e 6f 20 64 65 6c 65 74 69 6f 6e 20 6f 63 63 75  no deletion occu
139e6 72 73 2e 20 20 54 68 65 20 61 72 67 76 5b 31 5d  rs.  The argv[1]
139e7 20 65 6c 65 6d 65 6e 74 20 69 73 20 74 68 65 20   element is the 
139e8 72 6f 77 69 64 0a 2a 2a 20 6f 66 20 74 68 65 20  rowid.** of the 
139e9 6e 65 77 20 72 6f 77 2e 20 20 54 68 69 73 20 63  new row.  This c
139ea 61 6e 20 62 65 20 4e 55 4c 4c 20 74 6f 20 68 61  an be NULL to ha
139eb 76 65 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  ve the virtual t
139ec 61 62 6c 65 0a 2a 2a 20 73 65 6c 65 63 74 20 74  able.** select t
139ed 68 65 20 6e 65 77 20 72 6f 77 69 64 20 66 6f 72  he new rowid for
139ee 20 69 74 73 65 6c 66 2e 20 20 54 68 65 20 68 69   itself.  The hi
139ef 67 68 65 72 20 65 6c 65 6d 65 6e 74 73 20 69 6e  gher elements in
139f0 20 74 68 65 0a 2a 2a 20 73 74 61 63 6b 20 61 72   the.** stack ar
139f1 65 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20  e the values of 
139f2 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 6e  columns in the n
139f3 65 77 20 72 6f 77 2e 0a 2a 2a 0a 2a 2a 20 49 66  ew row..**.** If
139f4 20 50 32 3d 3d 31 20 74 68 65 6e 20 6e 6f 20 69   P2==1 then no i
139f5 6e 73 65 72 74 20 69 73 20 70 65 72 66 6f 72 6d  nsert is perform
139f6 65 64 2e 20 20 61 72 67 76 5b 30 5d 20 69 73 20  ed.  argv[0] is 
139f7 74 68 65 20 72 6f 77 69 64 20 6f 66 0a 2a 2a 20  the rowid of.** 
139f8 61 20 72 6f 77 20 74 6f 20 64 65 6c 65 74 65 2e  a row to delete.
139f9 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61 20 62  .**.** P1 is a b
139fa 6f 6f 6c 65 61 6e 20 66 6c 61 67 2e 20 49 66 20  oolean flag. If 
139fb 69 74 20 69 73 20 73 65 74 20 74 6f 20 74 72 75  it is set to tru
139fc 65 20 61 6e 64 20 74 68 65 20 78 55 70 64 61 74  e and the xUpdat
139fd 65 20 63 61 6c 6c 0a 2a 2a 20 69 73 20 73 75 63  e call.** is suc
139fe 63 65 73 73 66 75 6c 2c 20 74 68 65 6e 20 74 68  cessful, then th
139ff 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  e value returned
13a00 20 62 79 20 73 71 6c 69 74 65 33 5f 6c 61 73 74   by sqlite3_last
13a01 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 20  _insert_rowid() 
13a02 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 74 68  .** is set to th
13a03 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72  e value of the r
13a04 6f 77 69 64 20 66 6f 72 20 74 68 65 20 72 6f 77  owid for the row
13a05 20 6a 75 73 74 20 69 6e 73 65 72 74 65 64 2e 0a   just inserted..
13a06 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 55 70 64 61  */.case OP_VUpda
13a07 74 65 3a 20 7b 20 20 20 2f 2a 20 6e 6f 2d 70 75  te: {   /* no-pu
13a08 73 68 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  sh */.  sqlite3_
13a09 76 74 61 62 20 2a 70 56 74 61 62 20 3d 20 28 73  vtab *pVtab = (s
13a0a 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 29 28 70  qlite3_vtab *)(p
13a0b 4f 70 2d 3e 70 33 29 3b 0a 20 20 73 71 6c 69 74  Op->p3);.  sqlit
13a0c 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75  e3_module *pModu
13a0d 6c 65 20 3d 20 28 73 71 6c 69 74 65 33 5f 6d 6f  le = (sqlite3_mo
13a0e 64 75 6c 65 20 2a 29 70 56 74 61 62 2d 3e 70 4d  dule *)pVtab->pM
13a0f 6f 64 75 6c 65 3b 0a 20 20 69 6e 74 20 6e 41 72  odule;.  int nAr
13a10 67 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 61  g = pOp->p2;.  a
13a11 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 74 79  ssert( pOp->p3ty
13a12 70 65 3d 3d 50 33 5f 56 54 41 42 20 29 3b 0a 20  pe==P3_VTAB );. 
13a13 20 69 66 28 20 70 4d 6f 64 75 6c 65 2d 3e 78 55   if( pModule->xU
13a14 70 64 61 74 65 3d 3d 30 20 29 7b 0a 20 20 20 20  pdate==0 ){.    
13a15 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67  sqlite3SetString
13a16 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 22 72  (&p->zErrMsg, "r
13a17 65 61 64 2d 6f 6e 6c 79 20 74 61 62 6c 65 22 2c  ead-only table",
13a18 20 30 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51   0);.    rc = SQ
13a19 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 65  LITE_ERROR;.  }e
13a1a 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  lse{.    int i;.
13a1b 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34      sqlite_int64
13a1c 20 72 6f 77 69 64 3b 0a 20 20 20 20 4d 65 6d 20   rowid;.    Mem 
13a1d 2a 2a 61 70 41 72 67 20 3d 20 70 2d 3e 61 70 41  **apArg = p->apA
13a1e 72 67 3b 0a 20 20 20 20 4d 65 6d 20 2a 70 58 20  rg;.    Mem *pX 
13a1f 3d 20 26 70 54 6f 73 5b 31 2d 6e 41 72 67 5d 3b  = &pTos[1-nArg];
13a20 0a 20 20 20 20 66 6f 72 28 69 20 3d 20 30 3b 20  .    for(i = 0; 
13a21 69 3c 6e 41 72 67 3b 20 69 2b 2b 2c 20 70 58 2b  i<nArg; i++, pX+
13a22 2b 29 7b 0a 20 20 20 20 20 20 73 74 6f 72 65 54  +){.      storeT
13a23 79 70 65 49 6e 66 6f 28 70 58 2c 20 30 29 3b 0a  ypeInfo(pX, 0);.
13a24 20 20 20 20 20 20 61 70 41 72 67 5b 69 5d 20 3d        apArg[i] =
13a25 20 70 58 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69   pX;.    }.    i
13a26 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
13a27 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61  Off(db) ) goto a
13a28 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75  bort_due_to_misu
13a29 73 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  se;.    sqlite3V
13a2a 74 61 62 4c 6f 63 6b 28 70 56 74 61 62 29 3b 0a  tabLock(pVtab);.
13a2b 20 20 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65      rc = pModule
13a2c 2d 3e 78 55 70 64 61 74 65 28 70 56 74 61 62 2c  ->xUpdate(pVtab,
13a2d 20 6e 41 72 67 2c 20 61 70 41 72 67 2c 20 26 72   nArg, apArg, &r
13a2e 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74  owid);.    sqlit
13a2f 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 64 62 2c  e3VtabUnlock(db,
13a30 20 70 56 74 61 62 29 3b 0a 20 20 20 20 69 66 28   pVtab);.    if(
13a31 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e   sqlite3SafetyOn
13a32 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72  (db) ) goto abor
13a33 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b  t_due_to_misuse;
13a34 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 31  .    if( pOp->p1
13a35 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f   && rc==SQLITE_O
13a36 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  K ){.      asser
13a37 74 28 20 6e 41 72 67 3e 31 20 26 26 20 61 70 41  t( nArg>1 && apA
13a38 72 67 5b 30 5d 20 26 26 20 28 61 70 41 72 67 5b  rg[0] && (apArg[
13a39 30 5d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e 75  0]->flags&MEM_Nu
13a3a 6c 6c 29 20 29 3b 0a 20 20 20 20 20 20 64 62 2d  ll) );.      db-
13a3b 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 72 6f 77  >lastRowid = row
13a3c 69 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  id;.    }.  }.  
13a3d 70 6f 70 53 74 61 63 6b 28 26 70 54 6f 73 2c 20  popStack(&pTos, 
13a3e 6e 41 72 67 29 3b 0a 20 20 62 72 65 61 6b 3b 0a  nArg);.  break;.
13a3f 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
13a40 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
13a41 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 20 41 6e 20 6f  ABLE */../* An o
13a42 74 68 65 72 20 6f 70 63 6f 64 65 20 69 73 20 69  ther opcode is i
13a43 6c 6c 65 67 61 6c 2e 2e 2e 0a 2a 2f 0a 64 65 66  llegal....*/.def
13a44 61 75 6c 74 3a 20 7b 0a 20 20 61 73 73 65 72 74  ault: {.  assert
13a45 28 20 30 20 29 3b 0a 20 20 62 72 65 61 6b 3b 0a  ( 0 );.  break;.
13a46 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
13a47 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a48 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a49 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a4a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a4b 2a 0a 2a 2a 20 54 68 65 20 63 61 73 65 73 20 6f  *.** The cases o
13a4c 66 20 74 68 65 20 73 77 69 74 63 68 20 73 74 61  f the switch sta
13a4d 74 65 6d 65 6e 74 20 61 62 6f 76 65 20 74 68 69  tement above thi
13a4e 73 20 6c 69 6e 65 20 73 68 6f 75 6c 64 20 61 6c  s line should al
13a4f 6c 20 62 65 20 69 6e 64 65 6e 74 65 64 0a 2a 2a  l be indented.**
13a50 20 62 79 20 36 20 73 70 61 63 65 73 2e 20 20 42   by 6 spaces.  B
13a51 75 74 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74  ut the left-most
13a52 20 36 20 73 70 61 63 65 73 20 68 61 76 65 20 62   6 spaces have b
13a53 65 65 6e 20 72 65 6d 6f 76 65 64 20 74 6f 20 69  een removed to i
13a54 6d 70 72 6f 76 65 20 74 68 65 0a 2a 2a 20 72 65  mprove the.** re
13a55 61 64 61 62 69 6c 69 74 79 2e 20 20 46 72 6f 6d  adability.  From
13a56 20 74 68 69 73 20 70 6f 69 6e 74 20 6f 6e 20 64   this point on d
13a57 6f 77 6e 2c 20 74 68 65 20 6e 6f 72 6d 61 6c 20  own, the normal 
13a58 69 6e 64 65 6e 74 61 74 69 6f 6e 20 72 75 6c 65  indentation rule
13a59 73 20 61 72 65 0a 2a 2a 20 72 65 73 74 6f 72 65  s are.** restore
13a5a 64 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  d..*************
13a5b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a5c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a5e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a5f 2f 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  /.    }..    /* 
13a60 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 73 74  Make sure the st
13a61 61 63 6b 20 6c 69 6d 69 74 20 77 61 73 20 6e 6f  ack limit was no
13a62 74 20 65 78 63 65 65 64 65 64 20 2a 2f 0a 20 20  t exceeded */.  
13a63 20 20 61 73 73 65 72 74 28 20 70 54 6f 73 3c 3d    assert( pTos<=
13a64 70 53 74 61 63 6b 4c 69 6d 69 74 20 29 3b 0a 0a  pStackLimit );..
13a65 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46  #ifdef VDBE_PROF
13a66 49 4c 45 0a 20 20 20 20 7b 0a 20 20 20 20 20 20  ILE.    {.      
13a67 6c 6f 6e 67 20 6c 6f 6e 67 20 65 6c 61 70 73 65  long long elapse
13a68 20 3d 20 68 77 74 69 6d 65 28 29 20 2d 20 73 74   = hwtime() - st
13a69 61 72 74 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e  art;.      pOp->
13a6a 63 79 63 6c 65 73 20 2b 3d 20 65 6c 61 70 73 65  cycles += elapse
13a6b 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 63 6e 74  ;.      pOp->cnt
13a6c 2b 2b 3b 0a 23 69 66 20 30 0a 20 20 20 20 20 20  ++;.#if 0.      
13a6d 20 20 66 70 72 69 6e 74 66 28 73 74 64 6f 75 74    fprintf(stdout
13a6e 2c 20 22 25 31 30 6c 6c 64 20 22 2c 20 65 6c 61  , "%10lld ", ela
13a6f 70 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71  pse);.        sq
13a70 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70  lite3VdbePrintOp
13a71 28 73 74 64 6f 75 74 2c 20 6f 72 69 67 50 63 2c  (stdout, origPc,
13a72 20 26 70 2d 3e 61 4f 70 5b 6f 72 69 67 50 63 5d   &p->aOp[origPc]
13a73 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a  );.#endif.    }.
13a74 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
13a75 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20 2f  QLITE_TEST.    /
13a76 2a 20 4b 65 65 70 20 74 72 61 63 6b 20 6f 66 20  * Keep track of 
13a77 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
13a78 6c 61 72 67 65 73 74 20 42 4c 4f 42 20 6f 72 20  largest BLOB or 
13a79 53 54 52 20 74 68 61 74 20 68 61 73 20 61 70 70  STR that has app
13a7a 65 61 72 65 64 0a 20 20 20 20 2a 2a 20 6f 6e 20  eared.    ** on 
13a7b 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 56  the top of the V
13a7c 44 42 45 20 73 74 61 63 6b 2e 0a 20 20 20 20 2a  DBE stack..    *
13a7d 2f 0a 20 20 20 20 69 66 28 20 70 54 6f 73 3e 3d  /.    if( pTos>=
13a7e 70 2d 3e 61 53 74 61 63 6b 20 26 26 20 28 70 54  p->aStack && (pT
13a7f 6f 73 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d  os->flags & (MEM
13a80 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 29 21  _Blob|MEM_Str))!
13a81 3d 30 0a 20 20 20 20 20 20 20 20 20 26 26 20 70  =0.         && p
13a82 54 6f 73 2d 3e 6e 3e 73 71 6c 69 74 65 33 5f 6d  Tos->n>sqlite3_m
13a83 61 78 5f 62 6c 6f 62 73 69 7a 65 20 29 7b 0a 20  ax_blobsize ){. 
13a84 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 61 78       sqlite3_max
13a85 5f 62 6c 6f 62 73 69 7a 65 20 3d 20 70 54 6f 73  _blobsize = pTos
13a86 2d 3e 6e 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  ->n;.    }.#endi
13a87 66 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f  f..    /* The fo
13a88 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 61 64 64  llowing code add
13a89 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 65  s nothing to the
13a8a 20 61 63 74 75 61 6c 20 66 75 6e 63 74 69 6f 6e   actual function
13a8b 61 6c 69 74 79 0a 20 20 20 20 2a 2a 20 6f 66 20  ality.    ** of 
13a8c 74 68 65 20 70 72 6f 67 72 61 6d 2e 20 20 49 74  the program.  It
13a8d 20 69 73 20 6f 6e 6c 79 20 68 65 72 65 20 66 6f   is only here fo
13a8e 72 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65  r testing and de
13a8f 62 75 67 67 69 6e 67 2e 0a 20 20 20 20 2a 2a 20  bugging..    ** 
13a90 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e  On the other han
13a91 64 2c 20 69 74 20 64 6f 65 73 20 62 75 72 6e 20  d, it does burn 
13a92 43 50 55 20 63 79 63 6c 65 73 20 65 76 65 72 79  CPU cycles every
13a93 20 74 69 6d 65 20 74 68 72 6f 75 67 68 0a 20 20   time through.  
13a94 20 20 2a 2a 20 74 68 65 20 65 76 61 6c 75 61 74    ** the evaluat
13a95 6f 72 20 6c 6f 6f 70 2e 20 20 53 6f 20 77 65 20  or loop.  So we 
13a96 63 61 6e 20 6c 65 61 76 65 20 69 74 20 6f 75 74  can leave it out
13a97 20 77 68 65 6e 20 4e 44 45 42 55 47 20 69 73 20   when NDEBUG is 
13a98 64 65 66 69 6e 65 64 2e 0a 20 20 20 20 2a 2f 0a  defined..    */.
13a99 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
13a9a 20 20 20 2f 2a 20 53 61 6e 69 74 79 20 63 68 65     /* Sanity che
13a9b 63 6b 69 6e 67 20 6f 6e 20 74 68 65 20 74 6f 70  cking on the top
13a9c 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20   element of the 
13a9d 73 74 61 63 6b 2e 20 49 66 20 74 68 65 20 70 72  stack. If the pr
13a9e 65 76 69 6f 75 73 0a 20 20 20 20 2a 2a 20 69 6e  evious.    ** in
13a9f 73 74 72 75 63 74 69 6f 6e 20 77 61 73 20 56 4e  struction was VN
13aa0 6f 43 68 61 6e 67 65 2c 20 74 68 65 6e 20 74 68  oChange, then th
13aa1 65 20 66 6c 61 67 73 20 66 69 65 6c 64 20 6f 66  e flags field of
13aa2 20 74 68 65 20 74 6f 70 0a 20 20 20 20 2a 2a 20   the top.    ** 
13aa3 6f 66 20 74 68 65 20 73 74 61 63 6b 20 69 73 20  of the stack is 
13aa4 73 65 74 20 74 6f 20 30 2e 20 54 68 69 73 20 69  set to 0. This i
13aa5 73 20 74 65 63 68 6e 69 63 61 6c 6c 79 20 69 6e  s technically in
13aa6 76 61 6c 69 64 20 66 6f 72 20 61 20 6d 65 6d 6f  valid for a memo
13aa7 72 79 0a 20 20 20 20 2a 2a 20 63 65 6c 6c 2c 20  ry.    ** cell, 
13aa8 73 6f 20 61 76 6f 69 64 20 63 61 6c 6c 69 6e 67  so avoid calling
13aa9 20 4d 65 6d 53 61 6e 69 74 79 28 29 20 69 6e 20   MemSanity() in 
13aaa 74 68 69 73 20 63 61 73 65 2e 0a 20 20 20 20 2a  this case..    *
13aab 2f 0a 20 20 20 20 69 66 28 20 70 54 6f 73 3e 3d  /.    if( pTos>=
13aac 70 2d 3e 61 53 74 61 63 6b 20 26 26 20 70 54 6f  p->aStack && pTo
13aad 73 2d 3e 66 6c 61 67 73 20 29 7b 0a 20 20 20 20  s->flags ){.    
13aae 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
13aaf 53 61 6e 69 74 79 28 70 54 6f 73 29 3b 0a 20 20  Sanity(pTos);.  
13ab0 20 20 20 20 61 73 73 65 72 74 28 20 21 73 71 6c      assert( !sql
13ab1 69 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69  ite3VdbeMemTooBi
13ab2 67 28 70 54 6f 73 29 20 29 3b 0a 20 20 20 20 7d  g(pTos) );.    }
13ab3 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 63 3e  .    assert( pc>
13ab4 3d 2d 31 20 26 26 20 70 63 3c 70 2d 3e 6e 4f 70  =-1 && pc<p->nOp
13ab5 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49   );..#ifdef SQLI
13ab6 54 45 5f 44 45 42 55 47 0a 20 20 20 20 2f 2a 20  TE_DEBUG.    /* 
13ab7 43 6f 64 65 20 66 6f 72 20 74 72 61 63 69 6e 67  Code for tracing
13ab8 20 74 68 65 20 76 64 62 65 20 73 74 61 63 6b 2e   the vdbe stack.
13ab9 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 74   */.    if( p->t
13aba 72 61 63 65 20 26 26 20 70 54 6f 73 3e 3d 70 2d  race && pTos>=p-
13abb 3e 61 53 74 61 63 6b 20 29 7b 0a 20 20 20 20 20  >aStack ){.     
13abc 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 66 70   int i;.      fp
13abd 72 69 6e 74 66 28 70 2d 3e 74 72 61 63 65 2c 20  rintf(p->trace, 
13abe 22 53 74 61 63 6b 3a 22 29 3b 0a 20 20 20 20 20  "Stack:");.     
13abf 20 66 6f 72 28 69 3d 30 3b 20 69 3e 2d 35 20 26   for(i=0; i>-5 &
13ac0 26 20 26 70 54 6f 73 5b 69 5d 3e 3d 70 2d 3e 61  & &pTos[i]>=p->a
13ac1 53 74 61 63 6b 3b 20 69 2d 2d 29 7b 0a 20 20 20  Stack; i--){.   
13ac2 20 20 20 20 20 69 66 28 20 70 54 6f 73 5b 69 5d       if( pTos[i]
13ac3 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c  .flags & MEM_Nul
13ac4 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66  l ){.          f
13ac5 70 72 69 6e 74 66 28 70 2d 3e 74 72 61 63 65 2c  printf(p->trace,
13ac6 20 22 20 4e 55 4c 4c 22 29 3b 0a 20 20 20 20 20   " NULL");.     
13ac7 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 54     }else if( (pT
13ac8 6f 73 5b 69 5d 2e 66 6c 61 67 73 20 26 20 28 4d  os[i].flags & (M
13ac9 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 53 74 72 29 29  EM_Int|MEM_Str))
13aca 3d 3d 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 53  ==(MEM_Int|MEM_S
13acb 74 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  tr) ){.         
13acc 20 66 70 72 69 6e 74 66 28 70 2d 3e 74 72 61 63   fprintf(p->trac
13acd 65 2c 20 22 20 73 69 3a 25 6c 6c 64 22 2c 20 70  e, " si:%lld", p
13ace 54 6f 73 5b 69 5d 2e 75 2e 69 29 3b 0a 20 20 20  Tos[i].u.i);.   
13acf 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70       }else if( p
13ad0 54 6f 73 5b 69 5d 2e 66 6c 61 67 73 20 26 20 4d  Tos[i].flags & M
13ad1 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 20 20  EM_Int ){.      
13ad2 20 20 20 20 66 70 72 69 6e 74 66 28 70 2d 3e 74      fprintf(p->t
13ad3 72 61 63 65 2c 20 22 20 69 3a 25 6c 6c 64 22 2c  race, " i:%lld",
13ad4 20 70 54 6f 73 5b 69 5d 2e 75 2e 69 29 3b 0a 20   pTos[i].u.i);. 
13ad5 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28         }else if(
13ad6 20 70 54 6f 73 5b 69 5d 2e 66 6c 61 67 73 20 26   pTos[i].flags &
13ad7 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20   MEM_Real ){.   
13ad8 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 70         fprintf(p
13ad9 2d 3e 74 72 61 63 65 2c 20 22 20 72 3a 25 67 22  ->trace, " r:%g"
13ada 2c 20 70 54 6f 73 5b 69 5d 2e 72 29 3b 0a 20 20  , pTos[i].r);.  
13adb 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
13adc 20 20 20 20 20 20 20 63 68 61 72 20 7a 42 75 66         char zBuf
13add 5b 32 30 30 5d 3b 0a 20 20 20 20 20 20 20 20 20  [200];.         
13ade 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 50   sqlite3VdbeMemP
13adf 72 65 74 74 79 50 72 69 6e 74 28 26 70 54 6f 73  rettyPrint(&pTos
13ae0 5b 69 5d 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20  [i], zBuf);.    
13ae1 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 70 2d        fprintf(p-
13ae2 3e 74 72 61 63 65 2c 20 22 20 22 29 3b 0a 20 20  >trace, " ");.  
13ae3 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28          fprintf(
13ae4 70 2d 3e 74 72 61 63 65 2c 20 22 25 73 22 2c 20  p->trace, "%s", 
13ae5 7a 42 75 66 29 3b 0a 20 20 20 20 20 20 20 20 7d  zBuf);.        }
13ae6 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
13ae7 66 28 20 72 63 21 3d 30 20 29 20 66 70 72 69 6e  f( rc!=0 ) fprin
13ae8 74 66 28 70 2d 3e 74 72 61 63 65 2c 22 20 72 63  tf(p->trace," rc
13ae9 3d 25 64 22 2c 72 63 29 3b 0a 20 20 20 20 20 20  =%d",rc);.      
13aea 66 70 72 69 6e 74 66 28 70 2d 3e 74 72 61 63 65  fprintf(p->trace
13aeb 2c 22 5c 6e 22 29 3b 0a 20 20 20 20 7d 0a 23 65  ,"\n");.    }.#e
13aec 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ndif  /* SQLITE_
13aed 44 45 42 55 47 20 2a 2f 0a 23 65 6e 64 69 66 20  DEBUG */.#endif 
13aee 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 20 20   /* NDEBUG */.  
13aef 7d 20 20 2f 2a 20 54 68 65 20 65 6e 64 20 6f 66  }  /* The end of
13af0 20 74 68 65 20 66 6f 72 28 3b 3b 29 20 6c 6f 6f   the for(;;) loo
13af1 70 20 74 68 65 20 6c 6f 6f 70 73 20 74 68 72 6f  p the loops thro
13af2 75 67 68 20 6f 70 63 6f 64 65 73 20 2a 2f 0a 0a  ugh opcodes */..
13af3 20 20 2f 2a 20 49 66 20 77 65 20 72 65 61 63 68    /* If we reach
13af4 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 69 74 20   this point, it 
13af5 6d 65 61 6e 73 20 74 68 61 74 20 65 78 65 63 75  means that execu
13af6 74 69 6f 6e 20 69 73 20 66 69 6e 69 73 68 65 64  tion is finished
13af7 2e 0a 20 20 2a 2f 0a 76 64 62 65 5f 68 61 6c 74  ..  */.vdbe_halt
13af8 3a 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20  :.  if( rc ){.  
13af9 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20    p->rc = rc;.  
13afa 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
13afb 52 4f 52 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ROR;.  }else{.  
13afc 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f    rc = SQLITE_DO
13afd 4e 45 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  NE;.  }.  sqlite
13afe 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0a 20 20  3VdbeHalt(p);.  
13aff 70 2d 3e 70 54 6f 73 20 3d 20 70 54 6f 73 3b 0a  p->pTos = pTos;.
13b00 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20    return rc;..  
13b01 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20  /* Jump to here 
13b02 69 66 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62  if a string or b
13b03 6c 6f 62 20 6c 61 72 67 65 72 20 74 68 61 6e 20  lob larger than 
13b04 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54  SQLITE_MAX_LENGT
13b05 48 0a 20 20 2a 2a 20 69 73 20 65 6e 63 6f 75 6e  H.  ** is encoun
13b06 74 65 72 65 64 2e 0a 20 20 2a 2f 0a 74 6f 6f 5f  tered..  */.too_
13b07 62 69 67 3a 0a 20 20 73 71 6c 69 74 65 33 53 65  big:.  sqlite3Se
13b08 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72  tString(&p->zErr
13b09 4d 73 67 2c 20 22 73 74 72 69 6e 67 20 6f 72 20  Msg, "string or 
13b0a 62 6c 6f 62 20 74 6f 6f 20 62 69 67 22 2c 20 28  blob too big", (
13b0b 63 68 61 72 2a 29 30 29 3b 0a 20 20 72 63 20 3d  char*)0);.  rc =
13b0c 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a   SQLITE_TOOBIG;.
13b0d 20 20 67 6f 74 6f 20 76 64 62 65 5f 68 61 6c 74    goto vdbe_halt
13b0e 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20  ;..  /* Jump to 
13b0f 68 65 72 65 20 69 66 20 61 20 6d 61 6c 6c 6f 63  here if a malloc
13b10 28 29 20 66 61 69 6c 73 2e 0a 20 20 2a 2f 0a 6e  () fails..  */.n
13b11 6f 5f 6d 65 6d 3a 0a 20 20 73 71 6c 69 74 65 33  o_mem:.  sqlite3
13b12 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45  SetString(&p->zE
13b13 72 72 4d 73 67 2c 20 22 6f 75 74 20 6f 66 20 6d  rrMsg, "out of m
13b14 65 6d 6f 72 79 22 2c 20 28 63 68 61 72 2a 29 30  emory", (char*)0
13b15 29 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45  );.  rc = SQLITE
13b16 5f 4e 4f 4d 45 4d 3b 0a 20 20 67 6f 74 6f 20 76  _NOMEM;.  goto v
13b17 64 62 65 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a 20  dbe_halt;..  /* 
13b18 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 66 6f 72  Jump to here for
13b19 20 61 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53   an SQLITE_MISUS
13b1a 45 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 61 62  E error..  */.ab
13b1b 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73  ort_due_to_misus
13b1c 65 3a 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45  e:.  rc = SQLITE
13b1d 5f 4d 49 53 55 53 45 3b 0a 20 20 2f 2a 20 46 61  _MISUSE;.  /* Fa
13b1e 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20 61 62 6f  ll thru into abo
13b1f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 20  rt_due_to_error 
13b20 2a 2f 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f  */..  /* Jump to
13b21 20 68 65 72 65 20 66 6f 72 20 61 6e 79 20 6f 74   here for any ot
13b22 68 65 72 20 6b 69 6e 64 20 6f 66 20 66 61 74 61  her kind of fata
13b23 6c 20 65 72 72 6f 72 2e 20 20 54 68 65 20 22 72  l error.  The "r
13b24 63 22 20 76 61 72 69 61 62 6c 65 0a 20 20 2a 2a  c" variable.  **
13b25 20 73 68 6f 75 6c 64 20 68 6f 6c 64 20 74 68 65   should hold the
13b26 20 65 72 72 6f 72 20 6e 75 6d 62 65 72 2e 0a 20   error number.. 
13b27 20 2a 2f 0a 61 62 6f 72 74 5f 64 75 65 5f 74 6f   */.abort_due_to
13b28 5f 65 72 72 6f 72 3a 0a 20 20 69 66 28 20 70 2d  _error:.  if( p-
13b29 3e 7a 45 72 72 4d 73 67 3d 3d 30 20 29 7b 0a 20  >zErrMsg==0 ){. 
13b2a 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4d 61     if( sqlite3Ma
13b2b 6c 6c 6f 63 46 61 69 6c 65 64 28 29 20 29 20 72  llocFailed() ) r
13b2c 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
13b2d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74  ;.    sqlite3Set
13b2e 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d  String(&p->zErrM
13b2f 73 67 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74  sg, sqlite3ErrSt
13b30 72 28 72 63 29 2c 20 28 63 68 61 72 2a 29 30 29  r(rc), (char*)0)
13b31 3b 0a 20 20 7d 0a 20 20 67 6f 74 6f 20 76 64 62  ;.  }.  goto vdb
13b32 65 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a 20 4a 75  e_halt;..  /* Ju
13b33 6d 70 20 74 6f 20 68 65 72 65 20 69 66 20 74 68  mp to here if th
13b34 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72  e sqlite3_interr
13b35 75 70 74 28 29 20 41 50 49 20 73 65 74 73 20 74  upt() API sets t
13b36 68 65 20 69 6e 74 65 72 72 75 70 74 0a 20 20 2a  he interrupt.  *
13b37 2a 20 66 6c 61 67 2e 0a 20 20 2a 2f 0a 61 62 6f  * flag..  */.abo
13b38 72 74 5f 64 75 65 5f 74 6f 5f 69 6e 74 65 72 72  rt_due_to_interr
13b39 75 70 74 3a 0a 20 20 61 73 73 65 72 74 28 20 64  upt:.  assert( d
13b3a 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70  b->u1.isInterrup
13b3b 74 65 64 20 29 3b 0a 20 20 69 66 28 20 64 62 2d  ted );.  if( db-
13b3c 3e 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d  >magic!=SQLITE_M
13b3d 41 47 49 43 5f 42 55 53 59 20 29 7b 0a 20 20 20  AGIC_BUSY ){.   
13b3e 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53   rc = SQLITE_MIS
13b3f 55 53 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  USE;.  }else{.  
13b40 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4e    rc = SQLITE_IN
13b41 54 45 52 52 55 50 54 3b 0a 20 20 7d 0a 20 20 70  TERRUPT;.  }.  p
13b42 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 73 71 6c  ->rc = rc;.  sql
13b43 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70  ite3SetString(&p
13b44 2d 3e 7a 45 72 72 4d 73 67 2c 20 73 71 6c 69 74  ->zErrMsg, sqlit
13b45 65 33 45 72 72 53 74 72 28 72 63 29 2c 20 28 63  e3ErrStr(rc), (c
13b46 68 61 72 2a 29 30 29 3b 0a 20 20 67 6f 74 6f 20  har*)0);.  goto 
13b47 76 64 62 65 5f 68 61 6c 74 3b 0a 7d 0a 0a 2f 2a  vdbe_halt;.}../*
13b48 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
13b49 64 20 6f 66 20 76 64 62 65 2e 63 20 2a 2a 2a 2a  d of vdbe.c ****
13b4a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13b4b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13b4c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
13b4d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
13b4e 67 69 6e 20 66 69 6c 65 20 76 64 62 65 62 6c 6f  gin file vdbeblo
13b4f 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  b.c ************
13b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13b51 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
13b52 0a 2a 2a 20 32 30 30 37 20 4d 61 79 20 31 0a 2a  .** 2007 May 1.*
13b53 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
13b54 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
13b55 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
13b56 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
13b57 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
13b58 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
13b59 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
13b5a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
13b5b 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
13b5c 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
13b5d 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
13b5e 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
13b5f 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
13b60 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
13b61 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
13b62 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
13b63 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
13b64 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
13b65 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13b66 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13b67 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13b68 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
13b69 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
13b6a 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64  ntains code used
13b6b 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 69 6e   to implement in
13b6c 63 72 65 6d 65 6e 74 61 6c 20 42 4c 4f 42 20 49  cremental BLOB I
13b6d 2f 4f 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76  /O..**.** $Id: v
13b6e 64 62 65 62 6c 6f 62 2e 63 2c 76 20 31 2e 31 31  dbeblob.c,v 1.11
13b6f 20 32 30 30 37 2f 30 36 2f 32 37 20 30 30 3a 33   2007/06/27 00:3
13b70 36 3a 31 34 20 64 72 68 20 45 78 70 20 24 0a 2a  6:14 drh Exp $.*
13b71 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  /...#ifndef SQLI
13b72 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42  TE_OMIT_INCRBLOB
13b73 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 73 71  ../*.** Valid sq
13b74 6c 69 74 65 33 5f 62 6c 6f 62 2a 20 68 61 6e 64  lite3_blob* hand
13b75 6c 65 73 20 70 6f 69 6e 74 20 74 6f 20 49 6e 63  les point to Inc
13b76 72 62 6c 6f 62 20 73 74 72 75 63 74 75 72 65 73  rblob structures
13b77 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
13b78 75 63 74 20 49 6e 63 72 62 6c 6f 62 20 49 6e 63  uct Incrblob Inc
13b79 72 62 6c 6f 62 3b 0a 73 74 72 75 63 74 20 49 6e  rblob;.struct In
13b7a 63 72 62 6c 6f 62 20 7b 0a 20 20 69 6e 74 20 66  crblob {.  int f
13b7b 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20  lags;           
13b7c 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 22 66     /* Copy of "f
13b7d 6c 61 67 73 22 20 70 61 73 73 65 64 20 74 6f 20  lags" passed to 
13b7e 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65  sqlite3_blob_ope
13b7f 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79  n() */.  int nBy
13b80 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  te;             
13b81 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 70 65 6e   /* Size of open
13b82 20 62 6c 6f 62 2c 20 69 6e 20 62 79 74 65 73 20   blob, in bytes 
13b83 2a 2f 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74  */.  int iOffset
13b84 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
13b85 42 79 74 65 20 6f 66 66 73 65 74 20 6f 66 20 62  Byte offset of b
13b86 6c 6f 62 20 69 6e 20 63 75 72 73 6f 72 20 64 61  lob in cursor da
13b87 74 61 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72  ta */.  BtCursor
13b88 20 2a 70 43 73 72 3b 20 20 20 20 20 20 20 20 20   *pCsr;         
13b89 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69  /* Cursor pointi
13b8a 6e 67 20 61 74 20 62 6c 6f 62 20 72 6f 77 20 2a  ng at blob row *
13b8b 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  /.  sqlite3_stmt
13b8c 20 2a 70 53 74 6d 74 3b 20 20 20 20 2f 2a 20 53   *pStmt;    /* S
13b8d 74 61 74 65 6d 65 6e 74 20 68 6f 6c 64 69 6e 67  tatement holding
13b8e 20 63 75 72 73 6f 72 20 6f 70 65 6e 20 2a 2f 0a   cursor open */.
13b8f 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61  };../*.** Open a
13b90 20 62 6c 6f 62 20 68 61 6e 64 6c 65 2e 0a 2a 2f   blob handle..*/
13b91 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f  .int sqlite3_blo
13b92 62 5f 6f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65  b_open(.  sqlite
13b93 33 2a 20 64 62 2c 20 20 20 20 20 20 20 20 20 20  3* db,          
13b94 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
13b95 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
13b96 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
13b97 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  b,        /* The
13b98 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61   attached databa
13b99 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  se containing th
13b9a 65 20 62 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73  e blob */.  cons
13b9b 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 2c 20  t char *zTable, 
13b9c 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65      /* The table
13b9d 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
13b9e 62 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  blob */.  const 
13b9f 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 2c 20 20  char *zColumn,  
13ba0 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20    /* The column 
13ba1 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 62  containing the b
13ba2 6c 6f 62 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f  lob */.  sqlite_
13ba3 69 6e 74 36 34 20 69 52 6f 77 2c 20 20 20 20 20  int64 iRow,     
13ba4 20 2f 2a 20 54 68 65 20 72 6f 77 20 63 6f 6e 74   /* The row cont
13ba5 61 69 6e 69 6e 67 20 74 68 65 20 67 6c 6f 62 20  aining the glob 
13ba6 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20  */.  int flags, 
13ba7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
13ba8 54 72 75 65 20 2d 3e 20 72 65 61 64 2f 77 72 69  True -> read/wri
13ba9 74 65 20 61 63 63 65 73 73 2c 20 66 61 6c 73 65  te access, false
13baa 20 2d 3e 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f   -> read-only */
13bab 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20  .  sqlite3_blob 
13bac 2a 2a 70 70 42 6c 6f 62 20 20 20 2f 2a 20 48 61  **ppBlob   /* Ha
13bad 6e 64 6c 65 20 66 6f 72 20 61 63 63 65 73 73 69  ndle for accessi
13bae 6e 67 20 74 68 65 20 62 6c 6f 62 20 72 65 74 75  ng the blob retu
13baf 72 6e 65 64 20 68 65 72 65 20 2a 2f 0a 29 7b 0a  rned here */.){.
13bb0 20 20 69 6e 74 20 6e 41 74 74 65 6d 70 74 20 3d    int nAttempt =
13bb1 20 30 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 20   0;.  int iCol; 
13bb2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
13bb3 20 49 6e 64 65 78 20 6f 66 20 7a 43 6f 6c 75 6d   Index of zColum
13bb4 6e 20 69 6e 20 72 6f 77 2d 72 65 63 6f 72 64 20  n in row-record 
13bb5 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 56 44  */..  /* This VD
13bb6 42 45 20 70 72 6f 67 72 61 6d 20 73 65 65 6b 73  BE program seeks
13bb7 20 61 20 62 74 72 65 65 20 63 75 72 73 6f 72 20   a btree cursor 
13bb8 74 6f 20 74 68 65 20 69 64 65 6e 74 69 66 69 65  to the identifie
13bb9 64 20 0a 20 20 2a 2a 20 64 62 2f 74 61 62 6c 65  d .  ** db/table
13bba 2f 72 6f 77 20 65 6e 74 72 79 2e 20 54 68 65 20  /row entry. The 
13bbb 72 65 61 73 6f 6e 20 66 6f 72 20 75 73 69 6e 67  reason for using
13bbc 20 61 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20   a vdbe program 
13bbd 69 6e 73 74 65 61 64 0a 20 20 2a 2a 20 6f 66 20  instead.  ** of 
13bbe 77 72 69 74 69 6e 67 20 63 6f 64 65 20 74 6f 20  writing code to 
13bbf 75 73 65 20 74 68 65 20 62 2d 74 72 65 65 20 6c  use the b-tree l
13bc0 61 79 65 72 20 64 69 72 65 63 74 6c 79 20 69 73  ayer directly is
13bc1 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a 20 76   that the.  ** v
13bc2 64 62 65 20 70 72 6f 67 72 61 6d 20 77 69 6c 6c  dbe program will
13bc3 20 74 61 6b 65 20 61 64 76 61 6e 74 61 67 65 20   take advantage 
13bc4 6f 66 20 74 68 65 20 76 61 72 69 6f 75 73 20 74  of the various t
13bc5 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 20 20 2a 2a  ransaction,.  **
13bc6 20 6c 6f 63 6b 69 6e 67 20 61 6e 64 20 65 72 72   locking and err
13bc7 6f 72 20 68 61 6e 64 6c 69 6e 67 20 69 6e 66 72  or handling infr
13bc8 61 73 74 72 75 63 74 75 72 65 20 62 75 69 6c 74  astructure built
13bc9 20 69 6e 74 6f 20 74 68 65 20 76 64 62 65 2e 0a   into the vdbe..
13bca 20 20 2a 2a 0a 20 20 2a 2a 20 41 66 74 65 72 20    **.  ** After 
13bcb 73 65 65 6b 69 6e 67 20 74 68 65 20 63 75 72 73  seeking the curs
13bcc 6f 72 2c 20 74 68 65 20 76 64 62 65 20 65 78 65  or, the vdbe exe
13bcd 63 75 74 65 73 20 61 6e 20 4f 50 5f 43 61 6c 6c  cutes an OP_Call
13bce 62 61 63 6b 2e 0a 20 20 2a 2a 20 43 6f 64 65 20  back..  ** Code 
13bcf 65 78 74 65 72 6e 61 6c 20 74 6f 20 74 68 65 20  external to the 
13bd0 56 64 62 65 20 74 68 65 6e 20 22 62 6f 72 72 6f  Vdbe then "borro
13bd1 77 73 22 20 74 68 65 20 62 2d 74 72 65 65 20 63  ws" the b-tree c
13bd2 75 72 73 6f 72 20 61 6e 64 0a 20 20 2a 2a 20 75  ursor and.  ** u
13bd3 73 65 73 20 69 74 20 74 6f 20 69 6d 70 6c 65 6d  ses it to implem
13bd4 65 6e 74 20 74 68 65 20 62 6c 6f 62 5f 72 65 61  ent the blob_rea
13bd5 64 28 29 2c 20 62 6c 6f 62 5f 77 72 69 74 65 28  d(), blob_write(
13bd6 29 20 61 6e 64 20 0a 20 20 2a 2a 20 62 6c 6f 62  ) and .  ** blob
13bd7 5f 62 79 74 65 73 28 29 20 66 75 6e 63 74 69 6f  _bytes() functio
13bd8 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  ns..  **.  ** Th
13bd9 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63  e sqlite3_blob_c
13bda 6c 6f 73 65 28 29 20 66 75 6e 63 74 69 6f 6e 20  lose() function 
13bdb 66 69 6e 61 6c 69 7a 65 73 20 74 68 65 20 76 64  finalizes the vd
13bdc 62 65 20 70 72 6f 67 72 61 6d 2c 0a 20 20 2a 2a  be program,.  **
13bdd 20 77 68 69 63 68 20 63 6c 6f 73 65 73 20 74 68   which closes th
13bde 65 20 62 2d 74 72 65 65 20 63 75 72 73 6f 72 20  e b-tree cursor 
13bdf 61 6e 64 20 28 70 6f 73 73 69 62 6c 79 29 20 63  and (possibly) c
13be0 6f 6d 6d 69 74 73 20 74 68 65 20 0a 20 20 2a 2a  ommits the .  **
13be1 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20   transaction..  
13be2 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  */.  static cons
13be3 74 20 56 64 62 65 4f 70 4c 69 73 74 20 6f 70 65  t VdbeOpList ope
13be4 6e 42 6c 6f 62 5b 5d 20 3d 20 7b 0a 20 20 20 20  nBlob[] = {.    
13be5 7b 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c  {OP_Transaction,
13be6 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 2f   0, 0, 0},     /
13be7 2a 20 30 3a 20 53 74 61 72 74 20 61 20 74 72 61  * 0: Start a tra
13be8 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20  nsaction */.    
13be9 7b 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65  {OP_VerifyCookie
13bea 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 2f  , 0, 0, 0},    /
13beb 2a 20 31 3a 20 43 68 65 63 6b 20 74 68 65 20 73  * 1: Check the s
13bec 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 2a 2f 0a  chema cookie */.
13bed 20 20 20 20 7b 4f 50 5f 49 6e 74 65 67 65 72 2c      {OP_Integer,
13bee 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20   0, 0, 0},      
13bef 20 20 20 2f 2a 20 32 3a 20 44 61 74 61 62 61 73     /* 2: Databas
13bf0 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 0a 20 20 20  e number */..   
13bf1 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 66   /* One of the f
13bf2 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 69 6e 73  ollowing two ins
13bf3 74 72 75 63 74 69 6f 6e 73 20 69 73 20 72 65 70  tructions is rep
13bf4 6c 61 63 65 64 20 62 79 20 61 6e 0a 20 20 20 20  laced by an.    
13bf5 2a 2a 20 4f 50 5f 4e 6f 6f 70 20 62 65 66 6f 72  ** OP_Noop befor
13bf6 65 20 65 78 65 63 74 69 6f 6e 2e 0a 20 20 20 20  e exection..    
13bf7 2a 2f 0a 20 20 20 20 7b 4f 50 5f 4f 70 65 6e 52  */.    {OP_OpenR
13bf8 65 61 64 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20  ead, 0, 0, 0},  
13bf9 20 20 20 20 20 20 2f 2a 20 33 3a 20 4f 70 65 6e        /* 3: Open
13bfa 20 63 75 72 73 6f 72 20 30 20 66 6f 72 20 72 65   cursor 0 for re
13bfb 61 64 69 6e 67 20 2a 2f 0a 20 20 20 20 7b 4f 50  ading */.    {OP
13bfc 5f 4f 70 65 6e 57 72 69 74 65 2c 20 30 2c 20 30  _OpenWrite, 0, 0
13bfd 2c 20 30 7d 2c 20 20 20 20 20 20 20 2f 2a 20 34  , 0},       /* 4
13bfe 3a 20 4f 70 65 6e 20 63 75 72 73 6f 72 20 30 20  : Open cursor 0 
13bff 66 6f 72 20 72 65 61 64 2f 77 72 69 74 65 20 2a  for read/write *
13c00 2f 0a 20 20 20 20 7b 4f 50 5f 53 65 74 4e 75 6d  /.    {OP_SetNum
13c01 43 6f 6c 75 6d 6e 73 2c 20 30 2c 20 30 2c 20 30  Columns, 0, 0, 0
13c02 7d 2c 20 20 20 2f 2a 20 35 3a 20 4e 75 6d 20 63  },   /* 5: Num c
13c03 6f 6c 73 20 66 6f 72 20 63 75 72 73 6f 72 20 2a  ols for cursor *
13c04 2f 0a 0a 20 20 20 20 7b 4f 50 5f 56 61 72 69 61  /..    {OP_Varia
13c05 62 6c 65 2c 20 31 2c 20 30 2c 20 30 7d 2c 20 20  ble, 1, 0, 0},  
13c06 20 20 20 20 20 20 2f 2a 20 36 3a 20 50 75 73 68        /* 6: Push
13c07 20 74 68 65 20 72 6f 77 69 64 20 74 6f 20 74 68   the rowid to th
13c08 65 20 73 74 61 63 6b 20 2a 2f 0a 20 20 20 20 7b  e stack */.    {
13c09 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20 30 2c  OP_NotExists, 0,
13c0a 20 31 30 2c 20 30 7d 2c 20 20 20 20 20 20 2f 2a   10, 0},      /*
13c0b 20 37 3a 20 53 65 65 6b 20 74 68 65 20 63 75 72   7: Seek the cur
13c0c 73 6f 72 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 43  sor */.    {OP_C
13c0d 6f 6c 75 6d 6e 2c 20 30 2c 20 30 2c 20 30 7d 2c  olumn, 0, 0, 0},
13c0e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 38 20 20            /* 8  
13c0f 2a 2f 0a 20 20 20 20 7b 4f 50 5f 43 61 6c 6c 62  */.    {OP_Callb
13c10 61 63 6b 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20  ack, 0, 0, 0},  
13c11 20 20 20 20 20 20 2f 2a 20 39 20 20 2a 2f 0a 20        /* 9  */. 
13c12 20 20 20 7b 4f 50 5f 43 6c 6f 73 65 2c 20 30 2c     {OP_Close, 0,
13c13 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 20 20   0, 0},         
13c14 20 20 2f 2a 20 31 30 20 2a 2f 0a 20 20 20 20 7b    /* 10 */.    {
13c15 4f 50 5f 48 61 6c 74 2c 20 30 2c 20 30 2c 20 30  OP_Halt, 0, 0, 0
13c16 7d 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  },            /*
13c17 20 31 31 20 2a 2f 0a 20 20 7d 3b 0a 0a 20 20 56   11 */.  };..  V
13c18 64 62 65 20 2a 76 20 3d 20 30 3b 0a 20 20 69 6e  dbe *v = 0;.  in
13c19 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
13c1a 3b 0a 20 20 63 68 61 72 20 7a 45 72 72 5b 31 32  ;.  char zErr[12
13c1b 38 5d 3b 0a 0a 20 20 7a 45 72 72 5b 30 5d 20 3d  8];..  zErr[0] =
13c1c 20 30 3b 0a 20 20 64 6f 20 7b 0a 20 20 20 20 50   0;.  do {.    P
13c1d 61 72 73 65 20 73 50 61 72 73 65 3b 0a 20 20 20  arse sParse;.   
13c1e 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 0a 20   Table *pTab;.. 
13c1f 20 20 20 6d 65 6d 73 65 74 28 26 73 50 61 72 73     memset(&sPars
13c20 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 50 61 72  e, 0, sizeof(Par
13c21 73 65 29 29 3b 0a 20 20 20 20 73 50 61 72 73 65  se));.    sParse
13c22 2e 64 62 20 3d 20 64 62 3b 0a 0a 20 20 20 20 72  .db = db;..    r
13c23 63 20 3d 20 73 71 6c 69 74 65 33 53 61 66 65 74  c = sqlite3Safet
13c24 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 69 66 28  yOn(db);.    if(
13c25 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
13c26 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  {.      return r
13c27 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 54  c;.    }..    pT
13c28 61 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61  ab = sqlite3Loca
13c29 74 65 54 61 62 6c 65 28 26 73 50 61 72 73 65 2c  teTable(&sParse,
13c2a 20 7a 54 61 62 6c 65 2c 20 7a 44 62 29 3b 0a 20   zTable, zDb);. 
13c2b 20 20 20 69 66 28 20 21 70 54 61 62 20 29 7b 0a     if( !pTab ){.
13c2c 20 20 20 20 20 20 69 66 28 20 73 50 61 72 73 65        if( sParse
13c2d 2e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20  .zErrMsg ){.    
13c2e 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
13c2f 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72  intf(sizeof(zErr
13c30 29 2c 20 7a 45 72 72 2c 20 22 25 73 22 2c 20 73  ), zErr, "%s", s
13c31 50 61 72 73 65 2e 7a 45 72 72 4d 73 67 29 3b 0a  Parse.zErrMsg);.
13c32 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
13c33 6c 69 74 65 46 72 65 65 28 73 50 61 72 73 65 2e  liteFree(sParse.
13c34 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20  zErrMsg);.      
13c35 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
13c36 52 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  R;.      sqlite3
13c37 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20  SafetyOff(db);. 
13c38 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f       goto blob_o
13c39 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a  pen_out;.    }..
13c3a 20 20 20 20 2f 2a 20 4e 6f 77 20 73 65 61 72 63      /* Now searc
13c3b 68 20 70 54 61 62 20 66 6f 72 20 74 68 65 20 65  h pTab for the e
13c3c 78 61 63 74 20 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a  xact column. */.
13c3d 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20      for(iCol=0; 
13c3e 69 43 6f 6c 20 3c 20 70 54 61 62 2d 3e 6e 43 6f  iCol < pTab->nCo
13c3f 6c 3b 20 69 43 6f 6c 2b 2b 29 20 7b 0a 20 20 20  l; iCol++) {.   
13c40 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74     if( sqlite3St
13c41 72 49 43 6d 70 28 70 54 61 62 2d 3e 61 43 6f 6c  rICmp(pTab->aCol
13c42 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 2c 20 7a 43  [iCol].zName, zC
13c43 6f 6c 75 6d 6e 29 3d 3d 30 20 29 7b 0a 20 20 20  olumn)==0 ){.   
13c44 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
13c45 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
13c46 28 20 69 43 6f 6c 3d 3d 70 54 61 62 2d 3e 6e 43  ( iCol==pTab->nC
13c47 6f 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ol ){.      sqli
13c48 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
13c49 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c  eof(zErr), zErr,
13c4a 20 22 6e 6f 20 73 75 63 68 20 63 6f 6c 75 6d 6e   "no such column
13c4b 3a 20 5c 22 25 73 5c 22 22 2c 20 7a 43 6f 6c 75  : \"%s\"", zColu
13c4c 6d 6e 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  mn);.      rc = 
13c4d 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
13c4e 20 20 20 20 73 71 6c 69 74 65 33 53 61 66 65 74      sqlite3Safet
13c4f 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20 20  yOff(db);.      
13c50 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f  goto blob_open_o
13c51 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  ut;.    }..    /
13c52 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69  * If the value i
13c53 73 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 66  s being opened f
13c54 6f 72 20 77 72 69 74 69 6e 67 2c 20 63 68 65 63  or writing, chec
13c55 6b 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 2a  k that the.    *
13c56 2a 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20  * column is not 
13c57 69 6e 64 65 78 65 64 2e 20 49 74 20 69 73 20 61  indexed. It is a
13c58 67 61 69 6e 73 74 20 74 68 65 20 72 75 6c 65 73  gainst the rules
13c59 20 74 6f 20 6f 70 65 6e 20 61 6e 0a 20 20 20 20   to open an.    
13c5a 2a 2a 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d  ** indexed colum
13c5b 6e 20 66 6f 72 20 77 72 69 74 69 6e 67 2e 0a 20  n for writing.. 
13c5c 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 66 6c     */.    if( fl
13c5d 61 67 73 20 29 7b 0a 20 20 20 20 20 20 49 6e 64  ags ){.      Ind
13c5e 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 20 20  ex *pIdx;.      
13c5f 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70  for(pIdx=pTab->p
13c60 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64  Index; pIdx; pId
13c61 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a  x=pIdx->pNext){.
13c62 20 20 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20          int j;. 
13c63 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20         for(j=0; 
13c64 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b  j<pIdx->nColumn;
13c65 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   j++){.         
13c66 20 69 66 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c   if( pIdx->aiCol
13c67 75 6d 6e 5b 6a 5d 3d 3d 69 43 6f 6c 20 29 7b 0a  umn[j]==iCol ){.
13c68 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
13c69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
13c6a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c  eof(zErr), zErr,
13c6b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
13c6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 63                "c
13c6d 61 6e 6e 6f 74 20 6f 70 65 6e 20 69 6e 64 65 78  annot open index
13c6e 65 64 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 77 72  ed column for wr
13c6f 69 74 69 6e 67 22 29 3b 0a 20 20 20 20 20 20 20  iting");.       
13c70 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
13c71 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 20 20  _ERROR;.        
13c72 20 20 20 20 73 71 6c 69 74 65 33 53 61 66 65 74      sqlite3Safet
13c73 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20 20  yOff(db);.      
13c74 20 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f        goto blob_
13c75 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20  open_out;.      
13c76 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
13c77 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
13c78 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 56 64     v = sqlite3Vd
13c79 62 65 43 72 65 61 74 65 28 64 62 29 3b 0a 20 20  beCreate(db);.  
13c7a 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 20    if( v ){.     
13c7b 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74   int iDb = sqlit
13c7c 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
13c7d 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
13c7e 61 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  a);.      sqlite
13c7f 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76  3VdbeAddOpList(v
13c80 2c 20 73 69 7a 65 6f 66 28 6f 70 65 6e 42 6c 6f  , sizeof(openBlo
13c81 62 29 2f 73 69 7a 65 6f 66 28 56 64 62 65 4f 70  b)/sizeof(VdbeOp
13c82 4c 69 73 74 29 2c 20 6f 70 65 6e 42 6c 6f 62 29  List), openBlob)
13c83 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66  ;..      /* Conf
13c84 69 67 75 72 65 20 74 68 65 20 4f 50 5f 54 72 61  igure the OP_Tra
13c85 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20  nsaction */.    
13c86 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
13c87 6e 67 65 50 31 28 76 2c 20 30 2c 20 69 44 62 29  ngeP1(v, 0, iDb)
13c88 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
13c89 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 30  dbeChangeP2(v, 0
13c8a 2c 20 28 66 6c 61 67 73 20 3f 20 31 20 3a 20 30  , (flags ? 1 : 0
13c8b 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f  ));..      /* Co
13c8c 6e 66 69 67 75 72 65 20 74 68 65 20 4f 50 5f 56  nfigure the OP_V
13c8d 65 72 69 66 79 43 6f 6f 6b 69 65 20 2a 2f 0a 20  erifyCookie */. 
13c8e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
13c8f 43 68 61 6e 67 65 50 31 28 76 2c 20 31 2c 20 69  ChangeP1(v, 1, i
13c90 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  Db);.      sqlit
13c91 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 76  e3VdbeChangeP2(v
13c92 2c 20 31 2c 20 70 54 61 62 2d 3e 70 53 63 68 65  , 1, pTab->pSche
13c93 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69  ma->schema_cooki
13c94 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f  e);..      /* Co
13c95 6e 66 69 67 75 72 65 20 74 68 65 20 64 62 20 6e  nfigure the db n
13c96 75 6d 62 65 72 20 70 75 73 68 65 64 20 6f 6e 74  umber pushed ont
13c97 6f 20 74 68 65 20 73 74 61 63 6b 20 2a 2f 0a 20  o the stack */. 
13c98 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
13c99 43 68 61 6e 67 65 50 31 28 76 2c 20 32 2c 20 69  ChangeP1(v, 2, i
13c9a 44 62 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 52  Db);..      /* R
13c9b 65 6d 6f 76 65 20 65 69 74 68 65 72 20 74 68 65  emove either the
13c9c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 6f 72   OP_OpenWrite or
13c9d 20 4f 70 65 6e 52 65 61 64 2e 20 53 65 74 20 74   OpenRead. Set t
13c9e 68 65 20 50 32 20 0a 20 20 20 20 20 20 2a 2a 20  he P2 .      ** 
13c9f 70 61 72 61 6d 65 74 65 72 20 6f 66 20 74 68 65  parameter of the
13ca0 20 6f 74 68 65 72 20 74 6f 20 70 54 61 62 2d 3e   other to pTab->
13ca1 74 6e 75 6d 2e 20 0a 20 20 20 20 20 20 2a 2f 0a  tnum. .      */.
13ca2 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
13ca3 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c  eChangeToNoop(v,
13ca4 20 28 66 6c 61 67 73 20 3f 20 33 20 3a 20 34 29   (flags ? 3 : 4)
13ca5 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 1);.      sqli
13ca6 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28  te3VdbeChangeP2(
13ca7 76 2c 20 28 66 6c 61 67 73 20 3f 20 34 20 3a 20  v, (flags ? 4 : 
13ca8 33 29 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 29 3b  3), pTab->tnum);
13ca9 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69  ..      /* Confi
13caa 67 75 72 65 20 74 68 65 20 4f 50 5f 53 65 74 4e  gure the OP_SetN
13cab 75 6d 43 6f 6c 75 6d 6e 73 2e 20 43 6f 6e 66 69  umColumns. Confi
13cac 67 75 72 65 20 74 68 65 20 63 75 72 73 6f 72 20  gure the cursor 
13cad 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 69 6e  to.      ** thin
13cae 6b 20 74 68 61 74 20 74 68 65 20 74 61 62 6c 65  k that the table
13caf 20 68 61 73 20 6f 6e 65 20 6d 6f 72 65 20 63 6f   has one more co
13cb0 6c 75 6d 6e 20 74 68 61 6e 20 69 74 20 72 65 61  lumn than it rea
13cb1 6c 6c 79 0a 20 20 20 20 20 20 2a 2a 20 64 6f 65  lly.      ** doe
13cb2 73 2e 20 41 6e 20 4f 50 5f 43 6f 6c 75 6d 6e 20  s. An OP_Column 
13cb3 74 6f 20 72 65 74 72 69 65 76 65 20 74 68 69 73  to retrieve this
13cb4 20 69 6d 61 67 69 6e 61 72 79 20 63 6f 6c 75 6d   imaginary colum
13cb5 6e 20 77 69 6c 6c 0a 20 20 20 20 20 20 2a 2a 20  n will.      ** 
13cb6 61 6c 77 61 79 73 20 72 65 74 75 72 6e 20 61 6e  always return an
13cb7 20 53 51 4c 20 4e 55 4c 4c 2e 20 54 68 69 73 20   SQL NULL. This 
13cb8 69 73 20 75 73 65 66 75 6c 20 62 65 63 61 75 73  is useful becaus
13cb9 65 20 69 74 20 6d 65 61 6e 73 0a 20 20 20 20 20  e it means.     
13cba 20 2a 2a 20 77 65 20 63 61 6e 20 69 6e 76 6f 6b   ** we can invok
13cbb 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74 6f 20 66  e OP_Column to f
13cbc 69 6c 6c 20 69 6e 20 74 68 65 20 76 64 62 65 20  ill in the vdbe 
13cbd 63 75 72 73 6f 72 73 20 74 79 70 65 20 0a 20 20  cursors type .  
13cbe 20 20 20 20 2a 2a 20 61 6e 64 20 6f 66 66 73 65      ** and offse
13cbf 74 20 63 61 63 68 65 20 77 69 74 68 6f 75 74 20  t cache without 
13cc0 63 61 75 73 69 6e 67 20 61 6e 79 20 49 4f 2e 0a  causing any IO..
13cc1 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73        */.      s
13cc2 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
13cc3 50 32 28 76 2c 20 35 2c 20 70 54 61 62 2d 3e 6e  P2(v, 5, pTab->n
13cc4 43 6f 6c 2b 31 29 3b 0a 20 20 20 20 20 20 69 66  Col+1);.      if
13cc5 28 20 21 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  ( !sqlite3Malloc
13cc6 46 61 69 6c 65 64 28 29 20 29 7b 0a 20 20 20 20  Failed() ){.    
13cc7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
13cc8 61 6b 65 52 65 61 64 79 28 76 2c 20 31 2c 20 30  akeReady(v, 1, 0
13cc9 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d  , 1, 0);.      }
13cca 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72 63 20 3d  .    }..    rc =
13ccb 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
13ccc 66 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 72  f(db);.    if( r
13ccd 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c!=SQLITE_OK || 
13cce 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69  sqlite3MallocFai
13ccf 6c 65 64 28 29 20 29 7b 0a 20 20 20 20 20 20 67  led() ){.      g
13cd0 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75  oto blob_open_ou
13cd1 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71  t;.    }..    sq
13cd2 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
13cd3 28 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  ((sqlite3_stmt *
13cd4 29 76 2c 20 31 2c 20 69 52 6f 77 29 3b 0a 20 20  )v, 1, iRow);.  
13cd5 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73    rc = sqlite3_s
13cd6 74 65 70 28 28 73 71 6c 69 74 65 33 5f 73 74 6d  tep((sqlite3_stm
13cd7 74 20 2a 29 76 29 3b 0a 20 20 20 20 69 66 28 20  t *)v);.    if( 
13cd8 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  rc!=SQLITE_ROW )
13cd9 7b 0a 20 20 20 20 20 20 6e 41 74 74 65 6d 70 74  {.      nAttempt
13cda 2b 2b 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  ++;.      rc = s
13cdb 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
13cdc 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29  (sqlite3_stmt *)
13cdd 76 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  v);.      sqlite
13cde 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
13cdf 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20 73  f(zErr), zErr, s
13ce0 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 64 62  qlite3_errmsg(db
13ce1 29 29 3b 0a 20 20 20 20 20 20 76 20 3d 20 30 3b  ));.      v = 0;
13ce2 0a 20 20 20 20 7d 0a 20 20 7d 20 77 68 69 6c 65  .    }.  } while
13ce3 28 20 6e 41 74 74 65 6d 70 74 3c 35 20 26 26 20  ( nAttempt<5 && 
13ce4 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d  rc==SQLITE_SCHEM
13ce5 41 20 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d  A );..  if( rc==
13ce6 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20  SQLITE_ROW ){.  
13ce7 20 20 2f 2a 20 54 68 65 20 72 6f 77 2d 72 65 63    /* The row-rec
13ce8 6f 72 64 20 68 61 73 20 62 65 65 6e 20 6f 70 65  ord has been ope
13ce9 6e 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79  ned successfully
13cea 2e 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65  . Check that the
13ceb 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69  .    ** column i
13cec 6e 20 71 75 65 73 74 69 6f 6e 20 63 6f 6e 74 61  n question conta
13ced 69 6e 73 20 74 65 78 74 20 6f 72 20 61 20 62 6c  ins text or a bl
13cee 6f 62 2e 20 49 66 20 69 74 20 63 6f 6e 74 61 69  ob. If it contai
13cef 6e 73 0a 20 20 20 20 2a 2a 20 74 65 78 74 2c 20  ns.    ** text, 
13cf0 69 74 20 69 73 20 75 70 20 74 6f 20 74 68 65 20  it is up to the 
13cf1 63 61 6c 6c 65 72 20 74 6f 20 67 65 74 20 74 68  caller to get th
13cf2 65 20 65 6e 63 6f 64 69 6e 67 20 72 69 67 68 74  e encoding right
13cf3 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 49 6e 63  ..    */.    Inc
13cf4 72 62 6c 6f 62 20 2a 70 42 6c 6f 62 3b 0a 20 20  rblob *pBlob;.  
13cf5 20 20 75 33 32 20 74 79 70 65 20 3d 20 76 2d 3e    u32 type = v->
13cf6 61 70 43 73 72 5b 30 5d 2d 3e 61 54 79 70 65 5b  apCsr[0]->aType[
13cf7 69 43 6f 6c 5d 3b 0a 0a 20 20 20 20 69 66 28 20  iCol];..    if( 
13cf8 74 79 70 65 3c 31 32 20 29 7b 0a 20 20 20 20 20  type<12 ){.     
13cf9 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
13cfa 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20  f(sizeof(zErr), 
13cfb 7a 45 72 72 2c 20 22 63 61 6e 6e 6f 74 20 6f 70  zErr, "cannot op
13cfc 65 6e 20 76 61 6c 75 65 20 6f 66 20 74 79 70 65  en value of type
13cfd 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20 20   %s",.          
13cfe 74 79 70 65 3d 3d 30 3f 22 6e 75 6c 6c 22 3a 20  type==0?"null": 
13cff 74 79 70 65 3d 3d 37 3f 22 72 65 61 6c 22 3a 20  type==7?"real": 
13d00 22 69 6e 74 65 67 65 72 22 0a 20 20 20 20 20 20  "integer".      
13d01 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  );.      rc = SQ
13d02 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
13d03 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e    goto blob_open
13d04 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  _out;.    }.    
13d05 70 42 6c 6f 62 20 3d 20 28 49 6e 63 72 62 6c 6f  pBlob = (Incrblo
13d06 62 20 2a 29 73 71 6c 69 74 65 4d 61 6c 6c 6f 63  b *)sqliteMalloc
13d07 28 73 69 7a 65 6f 66 28 49 6e 63 72 62 6c 6f 62  (sizeof(Incrblob
13d08 29 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  ));.    if( sqli
13d09 74 65 33 4d 61 6c 6c 6f 63 46 61 69 6c 65 64 28  te3MallocFailed(
13d0a 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ) ){.      sqlit
13d0b 65 46 72 65 65 28 70 42 6c 6f 62 29 3b 0a 20 20  eFree(pBlob);.  
13d0c 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70      goto blob_op
13d0d 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20  en_out;.    }.  
13d0e 20 20 70 42 6c 6f 62 2d 3e 66 6c 61 67 73 20 3d    pBlob->flags =
13d0f 20 66 6c 61 67 73 3b 0a 20 20 20 20 70 42 6c 6f   flags;.    pBlo
13d10 62 2d 3e 70 43 73 72 20 3d 20 20 76 2d 3e 61 70  b->pCsr =  v->ap
13d11 43 73 72 5b 30 5d 2d 3e 70 43 75 72 73 6f 72 3b  Csr[0]->pCursor;
13d12 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
13d13 65 43 61 63 68 65 4f 76 65 72 66 6c 6f 77 28 70  eCacheOverflow(p
13d14 42 6c 6f 62 2d 3e 70 43 73 72 29 3b 0a 20 20 20  Blob->pCsr);.   
13d15 20 70 42 6c 6f 62 2d 3e 70 53 74 6d 74 20 3d 20   pBlob->pStmt = 
13d16 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29  (sqlite3_stmt *)
13d17 76 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 69 4f  v;.    pBlob->iO
13d18 66 66 73 65 74 20 3d 20 76 2d 3e 61 70 43 73 72  ffset = v->apCsr
13d19 5b 30 5d 2d 3e 61 4f 66 66 73 65 74 5b 69 43 6f  [0]->aOffset[iCo
13d1a 6c 5d 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 6e  l];.    pBlob->n
13d1b 42 79 74 65 20 3d 20 73 71 6c 69 74 65 33 56 64  Byte = sqlite3Vd
13d1c 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28  beSerialTypeLen(
13d1d 74 79 70 65 29 3b 0a 20 20 20 20 2a 70 70 42 6c  type);.    *ppBl
13d1e 6f 62 20 3d 20 28 73 71 6c 69 74 65 33 5f 62 6c  ob = (sqlite3_bl
13d1f 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 20 20  ob *)pBlob;.    
13d20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
13d21 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d    }else if( rc==
13d22 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
13d23 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
13d24 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20  f(sizeof(zErr), 
13d25 7a 45 72 72 2c 20 22 6e 6f 20 73 75 63 68 20 72  zErr, "no such r
13d26 6f 77 69 64 3a 20 25 6c 6c 64 22 2c 20 69 52 6f  owid: %lld", iRo
13d27 77 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  w);.    rc = SQL
13d28 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a  ITE_ERROR;.  }..
13d29 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3a 0a 20  blob_open_out:. 
13d2a 20 7a 45 72 72 5b 73 69 7a 65 6f 66 28 7a 45 72   zErr[sizeof(zEr
13d2b 72 29 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20  r)-1] = '\0';.  
13d2c 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
13d2d 4b 20 7c 7c 20 73 71 6c 69 74 65 33 4d 61 6c 6c  K || sqlite3Mall
13d2e 6f 63 46 61 69 6c 65 64 28 29 20 29 7b 0a 20 20  ocFailed() ){.  
13d2f 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69    sqlite3_finali
13d30 7a 65 28 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ze((sqlite3_stmt
13d31 20 2a 29 76 29 3b 0a 20 20 7d 0a 20 20 73 71 6c   *)v);.  }.  sql
13d32 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63  ite3Error(db, rc
13d33 2c 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  , (rc==SQLITE_OK
13d34 3f 30 3a 7a 45 72 72 29 29 3b 0a 20 20 72 65 74  ?0:zErr));.  ret
13d35 75 72 6e 20 73 71 6c 69 74 65 33 41 70 69 45 78  urn sqlite3ApiEx
13d36 69 74 28 64 62 2c 20 72 63 29 3b 0a 7d 0a 0a 2f  it(db, rc);.}../
13d37 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 62 6c 6f  *.** Close a blo
13d38 62 20 68 61 6e 64 6c 65 20 74 68 61 74 20 77 61  b handle that wa
13d39 73 20 70 72 65 76 69 6f 75 73 6c 79 20 63 72 65  s previously cre
13d3a 61 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 73 71  ated using.** sq
13d3b 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28  lite3_blob_open(
13d3c 29 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65  )..*/.int sqlite
13d3d 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 73 71 6c  3_blob_close(sql
13d3e 69 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62  ite3_blob *pBlob
13d3f 29 7b 0a 20 20 49 6e 63 72 62 6c 6f 62 20 2a 70  ){.  Incrblob *p
13d40 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20 2a 29 70   = (Incrblob *)p
13d41 42 6c 6f 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f  Blob;.  sqlite3_
13d42 73 74 6d 74 20 2a 70 53 74 6d 74 20 3d 20 70 2d  stmt *pStmt = p-
13d43 3e 70 53 74 6d 74 3b 0a 20 20 73 71 6c 69 74 65  >pStmt;.  sqlite
13d44 46 72 65 65 28 70 29 3b 0a 20 20 72 65 74 75 72  Free(p);.  retur
13d45 6e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69  n sqlite3_finali
13d46 7a 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a 0a 73  ze(pStmt);.}...s
13d47 74 61 74 69 63 20 69 6e 74 20 62 6c 6f 62 52 65  tatic int blobRe
13d48 61 64 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74  adWrite(.  sqlit
13d49 65 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20  e3_blob *pBlob, 
13d4a 0a 20 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69  .  void *z, .  i
13d4b 6e 74 20 6e 2c 20 0a 20 20 69 6e 74 20 69 4f 66  nt n, .  int iOf
13d4c 66 73 65 74 2c 20 0a 20 20 69 6e 74 20 28 2a 78  fset, .  int (*x
13d4d 43 61 6c 6c 29 28 42 74 43 75 72 73 6f 72 2a 2c  Call)(BtCursor*,
13d4e 20 75 33 32 2c 20 75 33 32 2c 20 76 6f 69 64 2a   u32, u32, void*
13d4f 29 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  ).){.  int rc;. 
13d50 20 49 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28   Incrblob *p = (
13d51 49 6e 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62  Incrblob *)pBlob
13d52 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 28 56  ;.  Vdbe *v = (V
13d53 64 62 65 20 2a 29 28 70 2d 3e 70 53 74 6d 74 29  dbe *)(p->pStmt)
13d54 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  ;.  sqlite3 *db;
13d55 20 20 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72    ..  /* If ther
13d56 65 20 69 73 20 6e 6f 20 73 74 61 74 65 6d 65 6e  e is no statemen
13d57 74 20 68 61 6e 64 6c 65 2c 20 74 68 65 6e 20 74  t handle, then t
13d58 68 65 20 62 6c 6f 62 2d 68 61 6e 64 6c 65 20 68  he blob-handle h
13d59 61 73 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20  as.  ** already 
13d5a 62 65 65 6e 20 69 6e 76 61 6c 69 64 61 74 65 64  been invalidated
13d5b 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  . Return SQLITE_
13d5c 41 42 4f 52 54 20 69 6e 20 74 68 69 73 20 63 61  ABORT in this ca
13d5d 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  se..  */.  if( !
13d5e 76 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  v ) return SQLIT
13d5f 45 5f 41 42 4f 52 54 3b 0a 0a 20 20 2f 2a 20 52  E_ABORT;..  /* R
13d60 65 71 75 65 73 74 20 69 73 20 6f 75 74 20 6f 66  equest is out of
13d61 20 72 61 6e 67 65 2e 20 52 65 74 75 72 6e 20 61   range. Return a
13d62 20 74 72 61 6e 73 69 65 6e 74 20 65 72 72 6f 72   transient error
13d63 2e 20 2a 2f 0a 20 20 69 66 28 20 28 69 4f 66 66  . */.  if( (iOff
13d64 73 65 74 2b 6e 29 3e 70 2d 3e 6e 42 79 74 65 20  set+n)>p->nByte 
13d65 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
13d66 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
13d67 0a 20 20 2f 2a 20 43 61 6c 6c 20 65 69 74 68 65  .  /* Call eithe
13d68 72 20 42 74 72 65 65 44 61 74 61 28 29 20 6f 72  r BtreeData() or
13d69 20 42 74 72 65 65 50 75 74 44 61 74 61 28 29 2e   BtreePutData().
13d6a 20 49 66 20 53 51 4c 49 54 45 5f 41 42 4f 52 54   If SQLITE_ABORT
13d6b 20 69 73 0a 20 20 2a 2a 20 72 65 74 75 72 6e 65   is.  ** returne
13d6c 64 2c 20 63 6c 65 61 6e 2d 75 70 20 74 68 65 20  d, clean-up the 
13d6d 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65  statement handle
13d6e 2e 0a 20 20 2a 2f 0a 20 20 64 62 20 3d 20 76 2d  ..  */.  db = v-
13d6f 3e 64 62 3b 0a 20 20 72 63 20 3d 20 78 43 61 6c  >db;.  rc = xCal
13d70 6c 28 70 2d 3e 70 43 73 72 2c 20 69 4f 66 66 73  l(p->pCsr, iOffs
13d71 65 74 2b 70 2d 3e 69 4f 66 66 73 65 74 2c 20 6e  et+p->iOffset, n
13d72 2c 20 7a 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  , z);.  if( rc==
13d73 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 29 7b 0a  SQLITE_ABORT ){.
13d74 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 46      sqlite3VdbeF
13d75 69 6e 61 6c 69 7a 65 28 76 29 3b 0a 20 20 20 20  inalize(v);.    
13d76 70 2d 3e 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20  p->pStmt = 0;.  
13d77 7d 65 6c 73 65 7b 0a 20 20 20 20 64 62 2d 3e 65  }else{.    db->e
13d78 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 20  rrCode = rc;.   
13d79 20 76 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 7d   v->rc = rc;.  }
13d7a 0a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  ..  return sqlit
13d7b 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63  e3ApiExit(db, rc
13d7c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64  );.}../*.** Read
13d7d 20 64 61 74 61 20 66 72 6f 6d 20 61 20 62 6c 6f   data from a blo
13d7e 62 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 69 6e 74  b handle..*/.int
13d7f 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65   sqlite3_blob_re
13d80 61 64 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20  ad(sqlite3_blob 
13d81 2a 70 42 6c 6f 62 2c 20 76 6f 69 64 20 2a 7a 2c  *pBlob, void *z,
13d82 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69 4f 66 66   int n, int iOff
13d83 73 65 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 62  set){.  return b
13d84 6c 6f 62 52 65 61 64 57 72 69 74 65 28 70 42 6c  lobReadWrite(pBl
13d85 6f 62 2c 20 7a 2c 20 6e 2c 20 69 4f 66 66 73 65  ob, z, n, iOffse
13d86 74 2c 20 73 71 6c 69 74 65 33 42 74 72 65 65 44  t, sqlite3BtreeD
13d87 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57  ata);.}../*.** W
13d88 72 69 74 65 20 64 61 74 61 20 74 6f 20 61 20 62  rite data to a b
13d89 6c 6f 62 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 69  lob handle..*/.i
13d8a 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  nt sqlite3_blob_
13d8b 77 72 69 74 65 28 73 71 6c 69 74 65 33 5f 62 6c  write(sqlite3_bl
13d8c 6f 62 20 2a 70 42 6c 6f 62 2c 20 63 6f 6e 73 74  ob *pBlob, const
13d8d 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c   void *z, int n,
13d8e 20 69 6e 74 20 69 4f 66 66 73 65 74 29 7b 0a 20   int iOffset){. 
13d8f 20 72 65 74 75 72 6e 20 62 6c 6f 62 52 65 61 64   return blobRead
13d90 57 72 69 74 65 28 70 42 6c 6f 62 2c 20 28 76 6f  Write(pBlob, (vo
13d91 69 64 20 2a 29 7a 2c 20 6e 2c 20 69 4f 66 66 73  id *)z, n, iOffs
13d92 65 74 2c 20 73 71 6c 69 74 65 33 42 74 72 65 65  et, sqlite3Btree
13d93 50 75 74 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a  PutData);.}../*.
13d94 2a 2a 20 51 75 65 72 79 20 61 20 62 6c 6f 62 20  ** Query a blob 
13d95 68 61 6e 64 6c 65 20 66 6f 72 20 74 68 65 20 73  handle for the s
13d96 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 2e  ize of the data.
13d97 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f  .*/.int sqlite3_
13d98 62 6c 6f 62 5f 62 79 74 65 73 28 73 71 6c 69 74  blob_bytes(sqlit
13d99 65 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b  e3_blob *pBlob){
13d9a 0a 20 20 49 6e 63 72 62 6c 6f 62 20 2a 70 20 3d  .  Incrblob *p =
13d9b 20 28 49 6e 63 72 62 6c 6f 62 20 2a 29 70 42 6c   (Incrblob *)pBl
13d9c 6f 62 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e  ob;.  return p->
13d9d 6e 42 79 74 65 3b 0a 7d 0a 0a 23 65 6e 64 69 66  nByte;.}..#endif
13d9e 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c 49   /* #ifndef SQLI
13d9f 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42  TE_OMIT_INCRBLOB
13da0 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
13da1 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65  **** End of vdbe
13da2 62 6c 6f 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  blob.c *********
13da3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13da4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13da5 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
13da6 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
13da7 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  expr.c *********
13da8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13da9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13daa 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20  ***/./*.** 2001 
13dab 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a  September 15.**.
13dac 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
13dad 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
13dae 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
13daf 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
13db0 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
13db1 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
13db2 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
13db3 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
13db4 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
13db5 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
13db6 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
13db7 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
13db8 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
13db9 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
13dba 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
13dbb 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
13dbc 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
13dbd 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
13dbe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13dbf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13dc1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
13dc2 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
13dc3 73 20 72 6f 75 74 69 6e 65 73 20 75 73 65 64 20  s routines used 
13dc4 66 6f 72 20 61 6e 61 6c 79 7a 69 6e 67 20 65 78  for analyzing ex
13dc5 70 72 65 73 73 69 6f 6e 73 20 61 6e 64 0a 2a 2a  pressions and.**
13dc6 20 66 6f 72 20 67 65 6e 65 72 61 74 69 6e 67 20   for generating 
13dc7 56 44 42 45 20 63 6f 64 65 20 74 68 61 74 20 65  VDBE code that e
13dc8 76 61 6c 75 61 74 65 73 20 65 78 70 72 65 73 73  valuates express
13dc9 69 6f 6e 73 20 69 6e 20 53 51 4c 69 74 65 2e 0a  ions in SQLite..
13dca 2a 2a 0a 2a 2a 20 24 49 64 3a 20 65 78 70 72 2e  **.** $Id: expr.
13dcb 63 2c 76 20 31 2e 33 30 31 20 32 30 30 37 2f 30  c,v 1.301 2007/0
13dcc 37 2f 32 33 20 32 32 3a 35 31 3a 31 35 20 64 72  7/23 22:51:15 dr
13dcd 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a  h Exp $.*/../*.*
13dce 2a 20 52 65 74 75 72 6e 20 74 68 65 20 27 61 66  * Return the 'af
13dcf 66 69 6e 69 74 79 27 20 6f 66 20 74 68 65 20 65  finity' of the e
13dd0 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20  xpression pExpr 
13dd1 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 49 66  if any..**.** If
13dd2 20 70 45 78 70 72 20 69 73 20 61 20 63 6f 6c 75   pExpr is a colu
13dd3 6d 6e 2c 20 61 20 72 65 66 65 72 65 6e 63 65 20  mn, a reference 
13dd4 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 76 69 61 20  to a column via 
13dd5 61 6e 20 27 41 53 27 20 61 6c 69 61 73 2c 0a 2a  an 'AS' alias,.*
13dd6 2a 20 6f 72 20 61 20 73 75 62 2d 73 65 6c 65 63  * or a sub-selec
13dd7 74 20 77 69 74 68 20 61 20 63 6f 6c 75 6d 6e 20  t with a column 
13dd8 61 73 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  as the return va
13dd9 6c 75 65 2c 20 74 68 65 6e 20 74 68 65 20 0a 2a  lue, then the .*
13dda 2a 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68  * affinity of th
13ddb 61 74 20 63 6f 6c 75 6d 6e 20 69 73 20 72 65 74  at column is ret
13ddc 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65  urned. Otherwise
13ddd 2c 20 30 78 30 30 20 69 73 20 72 65 74 75 72 6e  , 0x00 is return
13dde 65 64 2c 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e  ed,.** indicatin
13ddf 67 20 6e 6f 20 61 66 66 69 6e 69 74 79 20 66 6f  g no affinity fo
13de0 72 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  r the expression
13de1 2e 0a 2a 2a 0a 2a 2a 20 69 2e 65 2e 20 74 68 65  ..**.** i.e. the
13de2 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 65 78   WHERE clause ex
13de3 70 72 65 73 73 73 69 6f 6e 73 20 69 6e 20 74 68  presssions in th
13de4 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74  e following stat
13de5 65 6d 65 6e 74 73 20 61 6c 6c 0a 2a 2a 20 68 61  ements all.** ha
13de6 76 65 20 61 6e 20 61 66 66 69 6e 69 74 79 3a 0a  ve an affinity:.
13de7 2a 2a 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 42  **.** CREATE TAB
13de8 4c 45 20 74 31 28 61 29 3b 0a 2a 2a 20 53 45 4c  LE t1(a);.** SEL
13de9 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 57 48  ECT * FROM t1 WH
13dea 45 52 45 20 61 3b 0a 2a 2a 20 53 45 4c 45 43 54  ERE a;.** SELECT
13deb 20 61 20 41 53 20 62 20 46 52 4f 4d 20 74 31 20   a AS b FROM t1 
13dec 57 48 45 52 45 20 62 3b 0a 2a 2a 20 53 45 4c 45  WHERE b;.** SELE
13ded 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 57 48 45  CT * FROM t1 WHE
13dee 52 45 20 28 73 65 6c 65 63 74 20 61 20 66 72 6f  RE (select a fro
13def 6d 20 74 31 29 3b 0a 2a 2f 0a 53 51 4c 49 54 45  m t1);.*/.SQLITE
13df0 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71  _PRIVATE char sq
13df1 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74  lite3ExprAffinit
13df2 79 28 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a  y(Expr *pExpr){.
13df3 20 20 69 6e 74 20 6f 70 20 3d 20 70 45 78 70 72    int op = pExpr
13df4 2d 3e 6f 70 3b 0a 20 20 69 66 28 20 6f 70 3d 3d  ->op;.  if( op==
13df5 54 4b 5f 53 45 4c 45 43 54 20 29 7b 0a 20 20 20  TK_SELECT ){.   
13df6 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 45   return sqlite3E
13df7 78 70 72 41 66 66 69 6e 69 74 79 28 70 45 78 70  xprAffinity(pExp
13df8 72 2d 3e 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69  r->pSelect->pELi
13df9 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 29 3b  st->a[0].pExpr);
13dfa 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c  .  }.#ifndef SQL
13dfb 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 20 20  ITE_OMIT_CAST.  
13dfc 69 66 28 20 6f 70 3d 3d 54 4b 5f 43 41 53 54 20  if( op==TK_CAST 
13dfd 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71  ){.    return sq
13dfe 6c 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 70  lite3AffinityTyp
13dff 65 28 26 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 29  e(&pExpr->token)
13e00 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72  ;.  }.#endif.  r
13e01 65 74 75 72 6e 20 70 45 78 70 72 2d 3e 61 66 66  eturn pExpr->aff
13e02 69 6e 69 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  inity;.}../*.** 
13e03 53 65 74 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e  Set the collatin
13e04 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 65  g sequence for e
13e05 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20  xpression pExpr 
13e06 74 6f 20 62 65 20 74 68 65 20 63 6f 6c 6c 61 74  to be the collat
13e07 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20  ing.** sequence 
13e08 6e 61 6d 65 64 20 62 79 20 70 54 6f 6b 65 6e 2e  named by pToken.
13e09 20 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e     Return a poin
13e0a 74 65 72 20 74 6f 20 74 68 65 20 72 65 76 69 73  ter to the revis
13e0b 65 64 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a  ed expression..*
13e0c 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20  * The collating 
13e0d 73 65 71 75 65 6e 63 65 20 69 73 20 6d 61 72 6b  sequence is mark
13e0e 65 64 20 61 73 20 22 65 78 70 6c 69 63 69 74 22  ed as "explicit"
13e0f 20 75 73 69 6e 67 20 74 68 65 20 45 50 5f 45 78   using the EP_Ex
13e10 70 43 6f 6c 6c 61 74 65 0a 2a 2a 20 66 6c 61 67  pCollate.** flag
13e11 2e 20 20 41 6e 20 65 78 70 6c 69 63 69 74 20 63  .  An explicit c
13e12 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
13e13 65 20 77 69 6c 6c 20 6f 76 65 72 72 69 64 65 20  e will override 
13e14 69 6d 70 6c 69 63 69 74 0a 2a 2a 20 63 6f 6c 6c  implicit.** coll
13e15 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 2e  ating sequences.
13e16 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
13e17 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33  TE Expr *sqlite3
13e18 45 78 70 72 53 65 74 43 6f 6c 6c 28 50 61 72 73  ExprSetColl(Pars
13e19 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20  e *pParse, Expr 
13e1a 2a 70 45 78 70 72 2c 20 54 6f 6b 65 6e 20 2a 70  *pExpr, Token *p
13e1b 4e 61 6d 65 29 7b 0a 20 20 43 6f 6c 6c 53 65 71  Name){.  CollSeq
13e1c 20 2a 70 43 6f 6c 6c 3b 0a 20 20 69 66 28 20 70   *pColl;.  if( p
13e1d 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75 72 6e  Expr==0 ) return
13e1e 20 30 3b 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71   0;.  pColl = sq
13e1f 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53  lite3LocateCollS
13e20 65 71 28 70 50 61 72 73 65 2c 20 28 63 68 61 72  eq(pParse, (char
13e21 2a 29 70 4e 61 6d 65 2d 3e 7a 2c 20 70 4e 61 6d  *)pName->z, pNam
13e22 65 2d 3e 6e 29 3b 0a 20 20 69 66 28 20 70 43 6f  e->n);.  if( pCo
13e23 6c 6c 20 29 7b 0a 20 20 20 20 70 45 78 70 72 2d  ll ){.    pExpr-
13e24 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a  >pColl = pColl;.
13e25 20 20 20 20 70 45 78 70 72 2d 3e 66 6c 61 67 73      pExpr->flags
13e26 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74   |= EP_ExpCollat
13e27 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  e;.  }.  return 
13e28 70 45 78 70 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  pExpr;.}../*.** 
13e29 52 65 74 75 72 6e 20 74 68 65 20 64 65 66 61 75  Return the defau
13e2a 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  lt collation seq
13e2b 75 65 6e 63 65 20 66 6f 72 20 74 68 65 20 65 78  uence for the ex
13e2c 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 2e 20  pression pExpr. 
13e2d 49 66 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 6e  If.** there is n
13e2e 6f 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74  o default collat
13e2f 69 6f 6e 20 74 79 70 65 2c 20 72 65 74 75 72 6e  ion type, return
13e30 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52   0..*/.SQLITE_PR
13e31 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73  IVATE CollSeq *s
13e32 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65  qlite3ExprCollSe
13e33 71 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  q(Parse *pParse,
13e34 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20   Expr *pExpr){. 
13e35 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20   CollSeq *pColl 
13e36 3d 20 30 3b 0a 20 20 69 66 28 20 70 45 78 70 72  = 0;.  if( pExpr
13e37 20 29 7b 0a 20 20 20 20 69 6e 74 20 6f 70 3b 0a   ){.    int op;.
13e38 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 45 78 70      pColl = pExp
13e39 72 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 6f 70  r->pColl;.    op
13e3a 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 20   = pExpr->op;.  
13e3b 20 20 69 66 28 20 28 6f 70 3d 3d 54 4b 5f 43 41    if( (op==TK_CA
13e3c 53 54 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 55 50 4c  ST || op==TK_UPL
13e3d 55 53 29 20 26 26 20 21 70 43 6f 6c 6c 20 29 7b  US) && !pColl ){
13e3e 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 73 71  .      return sq
13e3f 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71  lite3ExprCollSeq
13e40 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
13e41 70 4c 65 66 74 29 3b 0a 20 20 20 20 7d 0a 20 20  pLeft);.    }.  
13e42 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 43  }.  if( sqlite3C
13e43 68 65 63 6b 43 6f 6c 6c 53 65 71 28 70 50 61 72  heckCollSeq(pPar
13e44 73 65 2c 20 70 43 6f 6c 6c 29 20 29 7b 20 0a 20  se, pColl) ){ . 
13e45 20 20 20 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20     pColl = 0;.  
13e46 7d 0a 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c  }.  return pColl
13e47 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 70 72  ;.}../*.** pExpr
13e48 20 69 73 20 61 6e 20 6f 70 65 72 61 6e 64 20 6f   is an operand o
13e49 66 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f  f a comparison o
13e4a 70 65 72 61 74 6f 72 2e 20 20 61 66 66 32 20 69  perator.  aff2 i
13e4b 73 20 74 68 65 0a 2a 2a 20 74 79 70 65 20 61 66  s the.** type af
13e4c 66 69 6e 69 74 79 20 6f 66 20 74 68 65 20 6f 74  finity of the ot
13e4d 68 65 72 20 6f 70 65 72 61 6e 64 2e 20 20 54 68  her operand.  Th
13e4e 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  is routine retur
13e4f 6e 73 20 74 68 65 0a 2a 2a 20 74 79 70 65 20 61  ns the.** type a
13e50 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 68 6f  ffinity that sho
13e51 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72 20  uld be used for 
13e52 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f  the comparison o
13e53 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 53 51 4c 49  perator..*/.SQLI
13e54 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20  TE_PRIVATE char 
13e55 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66  sqlite3CompareAf
13e56 66 69 6e 69 74 79 28 45 78 70 72 20 2a 70 45 78  finity(Expr *pEx
13e57 70 72 2c 20 63 68 61 72 20 61 66 66 32 29 7b 0a  pr, char aff2){.
13e58 20 20 63 68 61 72 20 61 66 66 31 20 3d 20 73 71    char aff1 = sq
13e59 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74  lite3ExprAffinit
13e5a 79 28 70 45 78 70 72 29 3b 0a 20 20 69 66 28 20  y(pExpr);.  if( 
13e5b 61 66 66 31 20 26 26 20 61 66 66 32 20 29 7b 0a  aff1 && aff2 ){.
13e5c 20 20 20 20 2f 2a 20 42 6f 74 68 20 73 69 64 65      /* Both side
13e5d 73 20 6f 66 20 74 68 65 20 63 6f 6d 70 61 72 69  s of the compari
13e5e 73 6f 6e 20 61 72 65 20 63 6f 6c 75 6d 6e 73 2e  son are columns.
13e5f 20 49 66 20 6f 6e 65 20 68 61 73 20 6e 75 6d 65   If one has nume
13e60 72 69 63 0a 20 20 20 20 2a 2a 20 61 66 66 69 6e  ric.    ** affin
13e61 69 74 79 2c 20 75 73 65 20 74 68 61 74 2e 20 4f  ity, use that. O
13e62 74 68 65 72 77 69 73 65 20 75 73 65 20 6e 6f 20  therwise use no 
13e63 61 66 66 69 6e 69 74 79 2e 0a 20 20 20 20 2a 2f  affinity..    */
13e64 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
13e65 49 73 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74  IsNumericAffinit
13e66 79 28 61 66 66 31 29 20 7c 7c 20 73 71 6c 69 74  y(aff1) || sqlit
13e67 65 33 49 73 4e 75 6d 65 72 69 63 41 66 66 69 6e  e3IsNumericAffin
13e68 69 74 79 28 61 66 66 32 29 20 29 7b 0a 20 20 20  ity(aff2) ){.   
13e69 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
13e6a 5f 41 46 46 5f 4e 55 4d 45 52 49 43 3b 0a 20 20  _AFF_NUMERIC;.  
13e6b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
13e6c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 46 46  eturn SQLITE_AFF
13e6d 5f 4e 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d  _NONE;.    }.  }
13e6e 65 6c 73 65 20 69 66 28 20 21 61 66 66 31 20 26  else if( !aff1 &
13e6f 26 20 21 61 66 66 32 20 29 7b 0a 20 20 20 20 2f  & !aff2 ){.    /
13e70 2a 20 4e 65 69 74 68 65 72 20 73 69 64 65 20 6f  * Neither side o
13e71 66 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e  f the comparison
13e72 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2e 20 20 43   is a column.  C
13e73 6f 6d 70 61 72 65 20 74 68 65 0a 20 20 20 20 2a  ompare the.    *
13e74 2a 20 72 65 73 75 6c 74 73 20 64 69 72 65 63 74  * results direct
13e75 6c 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72  ly..    */.    r
13e76 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 46 46  eturn SQLITE_AFF
13e77 5f 4e 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a  _NONE;.  }else{.
13e78 20 20 20 20 2f 2a 20 4f 6e 65 20 73 69 64 65 20      /* One side 
13e79 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 74 68 65  is a column, the
13e7a 20 6f 74 68 65 72 20 69 73 20 6e 6f 74 2e 20 55   other is not. U
13e7b 73 65 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 61  se the columns a
13e7c 66 66 69 6e 69 74 79 2e 20 2a 2f 0a 20 20 20 20  ffinity. */.    
13e7d 61 73 73 65 72 74 28 20 61 66 66 31 3d 3d 30 20  assert( aff1==0 
13e7e 7c 7c 20 61 66 66 32 3d 3d 30 20 29 3b 0a 20 20  || aff2==0 );.  
13e7f 20 20 72 65 74 75 72 6e 20 28 61 66 66 31 20 2b    return (aff1 +
13e80 20 61 66 66 32 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f   aff2);.  }.}../
13e81 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61 20  *.** pExpr is a 
13e82 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 61  comparison opera
13e83 74 6f 72 2e 20 20 52 65 74 75 72 6e 20 74 68 65  tor.  Return the
13e84 20 74 79 70 65 20 61 66 66 69 6e 69 74 79 20 74   type affinity t
13e85 68 61 74 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65  hat should.** be
13e86 20 61 70 70 6c 69 65 64 20 74 6f 20 62 6f 74 68   applied to both
13e87 20 6f 70 65 72 61 6e 64 73 20 70 72 69 6f 72 20   operands prior 
13e88 74 6f 20 64 6f 69 6e 67 20 74 68 65 20 63 6f 6d  to doing the com
13e89 70 61 72 69 73 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  parison..*/.stat
13e8a 69 63 20 63 68 61 72 20 63 6f 6d 70 61 72 69 73  ic char comparis
13e8b 6f 6e 41 66 66 69 6e 69 74 79 28 45 78 70 72 20  onAffinity(Expr 
13e8c 2a 70 45 78 70 72 29 7b 0a 20 20 63 68 61 72 20  *pExpr){.  char 
13e8d 61 66 66 3b 0a 20 20 61 73 73 65 72 74 28 20 70  aff;.  assert( p
13e8e 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 51 20  Expr->op==TK_EQ 
13e8f 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  || pExpr->op==TK
13e90 5f 49 4e 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70  _IN || pExpr->op
13e91 3d 3d 54 4b 5f 4c 54 20 7c 7c 0a 20 20 20 20 20  ==TK_LT ||.     
13e92 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 3d 3d       pExpr->op==
13e93 54 4b 5f 47 54 20 7c 7c 20 70 45 78 70 72 2d 3e  TK_GT || pExpr->
13e94 6f 70 3d 3d 54 4b 5f 47 45 20 7c 7c 20 70 45 78  op==TK_GE || pEx
13e95 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4c 45 20 7c 7c  pr->op==TK_LE ||
13e96 0a 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72  .          pExpr
13e97 2d 3e 6f 70 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20  ->op==TK_NE );. 
13e98 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
13e99 70 4c 65 66 74 20 29 3b 0a 20 20 61 66 66 20 3d  pLeft );.  aff =
13e9a 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69   sqlite3ExprAffi
13e9b 6e 69 74 79 28 70 45 78 70 72 2d 3e 70 4c 65 66  nity(pExpr->pLef
13e9c 74 29 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d  t);.  if( pExpr-
13e9d 3e 70 52 69 67 68 74 20 29 7b 0a 20 20 20 20 61  >pRight ){.    a
13e9e 66 66 20 3d 20 73 71 6c 69 74 65 33 43 6f 6d 70  ff = sqlite3Comp
13e9f 61 72 65 41 66 66 69 6e 69 74 79 28 70 45 78 70  areAffinity(pExp
13ea0 72 2d 3e 70 52 69 67 68 74 2c 20 61 66 66 29 3b  r->pRight, aff);
13ea1 0a 20 20 7d 0a 20 20 65 6c 73 65 20 69 66 28 20  .  }.  else if( 
13ea2 70 45 78 70 72 2d 3e 70 53 65 6c 65 63 74 20 29  pExpr->pSelect )
13ea3 7b 0a 20 20 20 20 61 66 66 20 3d 20 73 71 6c 69  {.    aff = sqli
13ea4 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69  te3CompareAffini
13ea5 74 79 28 70 45 78 70 72 2d 3e 70 53 65 6c 65 63  ty(pExpr->pSelec
13ea6 74 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e  t->pEList->a[0].
13ea7 70 45 78 70 72 2c 20 61 66 66 29 3b 0a 20 20 7d  pExpr, aff);.  }
13ea8 0a 20 20 65 6c 73 65 20 69 66 28 20 21 61 66 66  .  else if( !aff
13ea9 20 29 7b 0a 20 20 20 20 61 66 66 20 3d 20 53 51   ){.    aff = SQ
13eaa 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20  LITE_AFF_NONE;. 
13eab 20 7d 0a 20 20 72 65 74 75 72 6e 20 61 66 66 3b   }.  return aff;
13eac 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 70 72 20  .}../*.** pExpr 
13ead 69 73 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20  is a comparison 
13eae 65 78 70 72 65 73 73 69 6f 6e 2c 20 65 67 2e 20  expression, eg. 
13eaf 27 3d 27 2c 20 27 3c 27 2c 20 49 4e 28 2e 2e 2e  '=', '<', IN(...
13eb0 29 20 65 74 63 2e 0a 2a 2a 20 69 64 78 5f 61 66  ) etc..** idx_af
13eb1 66 69 6e 69 74 79 20 69 73 20 74 68 65 20 61 66  finity is the af
13eb2 66 69 6e 69 74 79 20 6f 66 20 61 6e 20 69 6e 64  finity of an ind
13eb3 65 78 65 64 20 63 6f 6c 75 6d 6e 2e 20 52 65 74  exed column. Ret
13eb4 75 72 6e 20 74 72 75 65 0a 2a 2a 20 69 66 20 74  urn true.** if t
13eb5 68 65 20 69 6e 64 65 78 20 77 69 74 68 20 61 66  he index with af
13eb6 66 69 6e 69 74 79 20 69 64 78 5f 61 66 66 69 6e  finity idx_affin
13eb7 69 74 79 20 6d 61 79 20 62 65 20 75 73 65 64 20  ity may be used 
13eb8 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 0a 2a 2a 20  to implement.** 
13eb9 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69  the comparison i
13eba 6e 20 70 45 78 70 72 2e 0a 2a 2f 0a 53 51 4c 49  n pExpr..*/.SQLI
13ebb 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
13ebc 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e  qlite3IndexAffin
13ebd 69 74 79 4f 6b 28 45 78 70 72 20 2a 70 45 78 70  ityOk(Expr *pExp
13ebe 72 2c 20 63 68 61 72 20 69 64 78 5f 61 66 66 69  r, char idx_affi
13ebf 6e 69 74 79 29 7b 0a 20 20 63 68 61 72 20 61 66  nity){.  char af
13ec0 66 20 3d 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66  f = comparisonAf
13ec1 66 69 6e 69 74 79 28 70 45 78 70 72 29 3b 0a 20  finity(pExpr);. 
13ec2 20 73 77 69 74 63 68 28 20 61 66 66 20 29 7b 0a   switch( aff ){.
13ec3 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
13ec4 41 46 46 5f 4e 4f 4e 45 3a 0a 20 20 20 20 20 20  AFF_NONE:.      
13ec5 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 63 61  return 1;.    ca
13ec6 73 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45  se SQLITE_AFF_TE
13ec7 58 54 3a 0a 20 20 20 20 20 20 72 65 74 75 72 6e  XT:.      return
13ec8 20 69 64 78 5f 61 66 66 69 6e 69 74 79 3d 3d 53   idx_affinity==S
13ec9 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3b 0a  QLITE_AFF_TEXT;.
13eca 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20      default:.   
13ecb 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65     return sqlite
13ecc 33 49 73 4e 75 6d 65 72 69 63 41 66 66 69 6e 69  3IsNumericAffini
13ecd 74 79 28 69 64 78 5f 61 66 66 69 6e 69 74 79 29  ty(idx_affinity)
13ece 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ;.  }.}../*.** R
13ecf 65 74 75 72 6e 20 74 68 65 20 50 31 20 76 61 6c  eturn the P1 val
13ed0 75 65 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62  ue that should b
13ed1 65 20 75 73 65 64 20 66 6f 72 20 61 20 62 69 6e  e used for a bin
13ed2 61 72 79 20 63 6f 6d 70 61 72 69 73 6f 6e 0a 2a  ary comparison.*
13ed3 2a 20 6f 70 63 6f 64 65 20 28 4f 50 5f 45 71 2c  * opcode (OP_Eq,
13ed4 20 4f 50 5f 47 65 20 65 74 63 2e 29 20 75 73 65   OP_Ge etc.) use
13ed5 64 20 74 6f 20 63 6f 6d 70 61 72 65 20 70 45 78  d to compare pEx
13ed6 70 72 31 20 61 6e 64 20 70 45 78 70 72 32 2e 0a  pr1 and pExpr2..
13ed7 2a 2a 20 49 66 20 6a 75 6d 70 49 66 4e 75 6c 6c  ** If jumpIfNull
13ed8 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 73   is true, then s
13ed9 65 74 20 74 68 65 20 6c 6f 77 20 62 79 74 65 20  et the low byte 
13eda 6f 66 20 74 68 65 20 72 65 74 75 72 6e 65 64 0a  of the returned.
13edb 2a 2a 20 50 31 20 76 61 6c 75 65 20 74 6f 20 74  ** P1 value to t
13edc 65 6c 6c 20 74 68 65 20 6f 70 63 6f 64 65 20 74  ell the opcode t
13edd 6f 20 6a 75 6d 70 20 69 66 20 65 69 74 68 65 72  o jump if either
13ede 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 65   expression.** e
13edf 76 61 6c 75 61 74 65 73 20 74 6f 20 4e 55 4c 4c  valuates to NULL
13ee0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
13ee1 62 69 6e 61 72 79 43 6f 6d 70 61 72 65 50 31 28  binaryCompareP1(
13ee2 45 78 70 72 20 2a 70 45 78 70 72 31 2c 20 45 78  Expr *pExpr1, Ex
13ee3 70 72 20 2a 70 45 78 70 72 32 2c 20 69 6e 74 20  pr *pExpr2, int 
13ee4 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a 20 20 63  jumpIfNull){.  c
13ee5 68 61 72 20 61 66 66 20 3d 20 73 71 6c 69 74 65  har aff = sqlite
13ee6 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 45  3ExprAffinity(pE
13ee7 78 70 72 32 29 3b 0a 20 20 72 65 74 75 72 6e 20  xpr2);.  return 
13ee8 28 28 69 6e 74 29 73 71 6c 69 74 65 33 43 6f 6d  ((int)sqlite3Com
13ee9 70 61 72 65 41 66 66 69 6e 69 74 79 28 70 45 78  pareAffinity(pEx
13eea 70 72 31 2c 20 61 66 66 29 29 2b 28 6a 75 6d 70  pr1, aff))+(jump
13eeb 49 66 4e 75 6c 6c 3f 30 78 31 30 30 3a 30 29 3b  IfNull?0x100:0);
13eec 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
13eed 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
13eee 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  e collation sequ
13eef 65 6e 63 65 20 74 68 61 74 20 73 68 6f 75 6c 64  ence that should
13ef0 20 62 65 20 75 73 65 64 20 62 79 0a 2a 2a 20 61   be used by.** a
13ef1 20 62 69 6e 61 72 79 20 63 6f 6d 70 61 72 69 73   binary comparis
13ef2 6f 6e 20 6f 70 65 72 61 74 6f 72 20 63 6f 6d 70  on operator comp
13ef3 61 72 69 6e 67 20 70 4c 65 66 74 20 61 6e 64 20  aring pLeft and 
13ef4 70 52 69 67 68 74 2e 0a 2a 2a 0a 2a 2a 20 49 66  pRight..**.** If
13ef5 20 74 68 65 20 6c 65 66 74 20 68 61 6e 64 20 65   the left hand e
13ef6 78 70 72 65 73 73 69 6f 6e 20 68 61 73 20 61 20  xpression has a 
13ef7 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
13ef8 63 65 20 74 79 70 65 2c 20 74 68 65 6e 20 69 74  ce type, then it
13ef9 20 69 73 0a 2a 2a 20 75 73 65 64 2e 20 4f 74 68   is.** used. Oth
13efa 65 72 77 69 73 65 20 74 68 65 20 63 6f 6c 6c 61  erwise the colla
13efb 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66 6f  tion sequence fo
13efc 72 20 74 68 65 20 72 69 67 68 74 20 68 61 6e 64  r the right hand
13efd 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 69   expression.** i
13efe 73 20 75 73 65 64 2c 20 6f 72 20 74 68 65 20 64  s used, or the d
13eff 65 66 61 75 6c 74 20 28 42 49 4e 41 52 59 29 20  efault (BINARY) 
13f00 69 66 20 6e 65 69 74 68 65 72 20 65 78 70 72 65  if neither expre
13f01 73 73 69 6f 6e 20 68 61 73 20 61 20 63 6f 6c 6c  ssion has a coll
13f02 61 74 69 6e 67 0a 2a 2a 20 74 79 70 65 2e 0a 2a  ating.** type..*
13f03 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 70 52  *.** Argument pR
13f04 69 67 68 74 20 28 62 75 74 20 6e 6f 74 20 70 4c  ight (but not pL
13f05 65 66 74 29 20 6d 61 79 20 62 65 20 61 20 6e 75  eft) may be a nu
13f06 6c 6c 20 70 6f 69 6e 74 65 72 2e 20 49 6e 20 74  ll pointer. In t
13f07 68 69 73 20 63 61 73 65 2c 0a 2a 2a 20 69 74 20  his case,.** it 
13f08 69 73 20 6e 6f 74 20 63 6f 6e 73 69 64 65 72 65  is not considere
13f09 64 2e 0a 2a 2f 0a 43 6f 6c 6c 53 65 71 2a 20 73  d..*/.CollSeq* s
13f0a 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d 70  qlite3BinaryComp
13f0b 61 72 65 43 6f 6c 6c 53 65 71 28 0a 20 20 50 61  areCollSeq(.  Pa
13f0c 72 73 65 20 2a 70 50 61 72 73 65 2c 20 0a 20 20  rse *pParse, .  
13f0d 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 0a 20 20  Expr *pLeft, .  
13f0e 45 78 70 72 20 2a 70 52 69 67 68 74 0a 29 7b 0a  Expr *pRight.){.
13f0f 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
13f10 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 65 66  ;.  assert( pLef
13f11 74 20 29 3b 0a 20 20 69 66 28 20 70 4c 65 66 74  t );.  if( pLeft
13f12 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70  ->flags & EP_Exp
13f13 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 61  Collate ){.    a
13f14 73 73 65 72 74 28 20 70 4c 65 66 74 2d 3e 70 43  ssert( pLeft->pC
13f15 6f 6c 6c 20 29 3b 0a 20 20 20 20 70 43 6f 6c 6c  oll );.    pColl
13f16 20 3d 20 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 3b   = pLeft->pColl;
13f17 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 52 69  .  }else if( pRi
13f18 67 68 74 20 26 26 20 70 52 69 67 68 74 2d 3e 66  ght && pRight->f
13f19 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c  lags & EP_ExpCol
13f1a 6c 61 74 65 20 29 7b 0a 20 20 20 20 61 73 73 65  late ){.    asse
13f1b 72 74 28 20 70 52 69 67 68 74 2d 3e 70 43 6f 6c  rt( pRight->pCol
13f1c 6c 20 29 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d  l );.    pColl =
13f1d 20 70 52 69 67 68 74 2d 3e 70 43 6f 6c 6c 3b 0a   pRight->pColl;.
13f1e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43 6f    }else{.    pCo
13f1f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ll = sqlite3Expr
13f20 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20  CollSeq(pParse, 
13f21 70 4c 65 66 74 29 3b 0a 20 20 20 20 69 66 28 20  pLeft);.    if( 
13f22 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20  !pColl ){.      
13f23 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45  pColl = sqlite3E
13f24 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73  xprCollSeq(pPars
13f25 65 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 20 20  e, pRight);.    
13f26 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  }.  }.  return p
13f27 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  Coll;.}../*.** G
13f28 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72  enerate code for
13f29 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70   a comparison op
13f2a 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69  erator..*/.stati
13f2b 63 20 69 6e 74 20 63 6f 64 65 43 6f 6d 70 61 72  c int codeCompar
13f2c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  e(.  Parse *pPar
13f2d 73 65 2c 20 20 20 20 2f 2a 20 54 68 65 20 70 61  se,    /* The pa
13f2e 72 73 69 6e 67 20 28 61 6e 64 20 63 6f 64 65 20  rsing (and code 
13f2f 67 65 6e 65 72 61 74 69 6e 67 29 20 63 6f 6e 74  generating) cont
13f30 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ext */.  Expr *p
13f31 4c 65 66 74 2c 20 20 20 20 20 20 2f 2a 20 54 68  Left,      /* Th
13f32 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 2a  e left operand *
13f33 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 74  /.  Expr *pRight
13f34 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67  ,     /* The rig
13f35 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20  ht operand */.  
13f36 69 6e 74 20 6f 70 63 6f 64 65 2c 20 20 20 20 20  int opcode,     
13f37 20 20 2f 2a 20 54 68 65 20 63 6f 6d 70 61 72 69    /* The compari
13f38 73 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20  son opcode */.  
13f39 69 6e 74 20 64 65 73 74 2c 20 20 20 20 20 20 20  int dest,       
13f3a 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69    /* Jump here i
13f3b 66 20 74 72 75 65 2e 20 20 2a 2f 0a 20 20 69 6e  f true.  */.  in
13f3c 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 20 20 20 20  t jumpIfNull    
13f3d 2f 2a 20 49 66 20 74 72 75 65 2c 20 6a 75 6d 70  /* If true, jump
13f3e 20 69 66 20 65 69 74 68 65 72 20 6f 70 65 72 61   if either opera
13f3f 6e 64 20 69 73 20 4e 55 4c 4c 20 2a 2f 0a 29 7b  nd is NULL */.){
13f40 0a 20 20 69 6e 74 20 70 31 20 3d 20 62 69 6e 61  .  int p1 = bina
13f41 72 79 43 6f 6d 70 61 72 65 50 31 28 70 4c 65 66  ryCompareP1(pLef
13f42 74 2c 20 70 52 69 67 68 74 2c 20 6a 75 6d 70 49  t, pRight, jumpI
13f43 66 4e 75 6c 6c 29 3b 0a 20 20 43 6f 6c 6c 53 65  fNull);.  CollSe
13f44 71 20 2a 70 33 20 3d 20 73 71 6c 69 74 65 33 42  q *p3 = sqlite3B
13f45 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c  inaryCompareColl
13f46 53 65 71 28 70 50 61 72 73 65 2c 20 70 4c 65 66  Seq(pParse, pLef
13f47 74 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 72 65  t, pRight);.  re
13f48 74 75 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65  turn sqlite3Vdbe
13f49 4f 70 33 28 70 50 61 72 73 65 2d 3e 70 56 64 62  Op3(pParse->pVdb
13f4a 65 2c 20 6f 70 63 6f 64 65 2c 20 70 31 2c 20 64  e, opcode, p1, d
13f4b 65 73 74 2c 20 28 76 6f 69 64 2a 29 70 33 2c 20  est, (void*)p3, 
13f4c 50 33 5f 43 4f 4c 4c 53 45 51 29 3b 0a 7d 0a 0a  P3_COLLSEQ);.}..
13f4d 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20  /*.** Construct 
13f4e 61 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e  a new expression
13f4f 20 6e 6f 64 65 20 61 6e 64 20 72 65 74 75 72 6e   node and return
13f50 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74   a pointer to it
13f51 2e 20 20 4d 65 6d 6f 72 79 0a 2a 2a 20 66 6f 72  .  Memory.** for
13f52 20 74 68 69 73 20 6e 6f 64 65 20 69 73 20 6f 62   this node is ob
13f53 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69  tained from sqli
13f54 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20 54 68 65  teMalloc().  The
13f55 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f   calling functio
13f56 6e 0a 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73 69  n.** is responsi
13f57 62 6c 65 20 66 6f 72 20 6d 61 6b 69 6e 67 20 73  ble for making s
13f58 75 72 65 20 74 68 65 20 6e 6f 64 65 20 65 76 65  ure the node eve
13f59 6e 74 75 61 6c 6c 79 20 67 65 74 73 20 66 72 65  ntually gets fre
13f5a 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
13f5b 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69  IVATE Expr *sqli
13f5c 74 65 33 45 78 70 72 28 69 6e 74 20 6f 70 2c 20  te3Expr(int op, 
13f5d 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 45 78 70  Expr *pLeft, Exp
13f5e 72 20 2a 70 52 69 67 68 74 2c 20 63 6f 6e 73 74  r *pRight, const
13f5f 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b   Token *pToken){
13f60 0a 20 20 45 78 70 72 20 2a 70 4e 65 77 3b 0a 20  .  Expr *pNew;. 
13f61 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 4d 61   pNew = sqliteMa
13f62 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 45 78 70  lloc( sizeof(Exp
13f63 72 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77  r) );.  if( pNew
13f64 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 57 68  ==0 ){.    /* Wh
13f65 65 6e 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c  en malloc fails,
13f66 20 64 65 6c 65 74 65 20 70 4c 65 66 74 20 61 6e   delete pLeft an
13f67 64 20 70 52 69 67 68 74 2e 20 45 78 70 72 65 73  d pRight. Expres
13f68 73 69 6f 6e 73 20 70 61 73 73 65 64 20 74 6f 20  sions passed to 
13f69 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 66 75 6e  .    ** this fun
13f6a 63 74 69 6f 6e 20 6d 75 73 74 20 61 6c 77 61 79  ction must alway
13f6b 73 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 77  s be allocated w
13f6c 69 74 68 20 73 71 6c 69 74 65 33 45 78 70 72 28  ith sqlite3Expr(
13f6d 29 20 66 6f 72 20 74 68 69 73 20 0a 20 20 20 20  ) for this .    
13f6e 2a 2a 20 72 65 61 73 6f 6e 2e 20 0a 20 20 20 20  ** reason. .    
13f6f 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  */.    sqlite3Ex
13f70 70 72 44 65 6c 65 74 65 28 70 4c 65 66 74 29 3b  prDelete(pLeft);
13f71 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
13f72 44 65 6c 65 74 65 28 70 52 69 67 68 74 29 3b 0a  Delete(pRight);.
13f73 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
13f74 7d 0a 20 20 70 4e 65 77 2d 3e 6f 70 20 3d 20 6f  }.  pNew->op = o
13f75 70 3b 0a 20 20 70 4e 65 77 2d 3e 70 4c 65 66 74  p;.  pNew->pLeft
13f76 20 3d 20 70 4c 65 66 74 3b 0a 20 20 70 4e 65 77   = pLeft;.  pNew
13f77 2d 3e 70 52 69 67 68 74 20 3d 20 70 52 69 67 68  ->pRight = pRigh
13f78 74 3b 0a 20 20 70 4e 65 77 2d 3e 69 41 67 67 20  t;.  pNew->iAgg 
13f79 3d 20 2d 31 3b 0a 20 20 69 66 28 20 70 54 6f 6b  = -1;.  if( pTok
13f7a 65 6e 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  en ){.    assert
13f7b 28 20 70 54 6f 6b 65 6e 2d 3e 64 79 6e 3d 3d 30  ( pToken->dyn==0
13f7c 20 29 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 73 70   );.    pNew->sp
13f7d 61 6e 20 3d 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e  an = pNew->token
13f7e 20 3d 20 2a 70 54 6f 6b 65 6e 3b 0a 20 20 7d 65   = *pToken;.  }e
13f7f 6c 73 65 20 69 66 28 20 70 4c 65 66 74 20 29 7b  lse if( pLeft ){
13f80 0a 20 20 20 20 69 66 28 20 70 52 69 67 68 74 20  .    if( pRight 
13f81 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
13f82 45 78 70 72 53 70 61 6e 28 70 4e 65 77 2c 20 26  ExprSpan(pNew, &
13f83 70 4c 65 66 74 2d 3e 73 70 61 6e 2c 20 26 70 52  pLeft->span, &pR
13f84 69 67 68 74 2d 3e 73 70 61 6e 29 3b 0a 20 20 20  ight->span);.   
13f85 20 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 66     if( pRight->f
13f86 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c  lags & EP_ExpCol
13f87 6c 61 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20  late ){.        
13f88 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45  pNew->flags |= E
13f89 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20  P_ExpCollate;.  
13f8a 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 43 6f 6c        pNew->pCol
13f8b 6c 20 3d 20 70 52 69 67 68 74 2d 3e 70 43 6f 6c  l = pRight->pCol
13f8c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  l;.      }.    }
13f8d 0a 20 20 20 20 69 66 28 20 70 4c 65 66 74 2d 3e  .    if( pLeft->
13f8e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f  flags & EP_ExpCo
13f8f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 20 20 70  llate ){.      p
13f90 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50  New->flags |= EP
13f91 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 20  _ExpCollate;.   
13f92 20 20 20 70 4e 65 77 2d 3e 70 43 6f 6c 6c 20 3d     pNew->pColl =
13f93 20 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 3b 0a 20   pLeft->pColl;. 
13f94 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 73 71 6c 69     }.  }..  sqli
13f95 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74  te3ExprSetHeight
13f96 28 70 4e 65 77 29 3b 0a 20 20 72 65 74 75 72 6e  (pNew);.  return
13f97 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   pNew;.}../*.** 
13f98 57 6f 72 6b 73 20 6c 69 6b 65 20 73 71 6c 69 74  Works like sqlit
13f99 65 33 45 78 70 72 28 29 20 62 75 74 20 66 72 65  e3Expr() but fre
13f9a 65 73 20 69 74 73 20 70 4c 65 66 74 20 61 6e 64  es its pLeft and
13f9b 20 70 52 69 67 68 74 20 61 72 67 75 6d 65 6e 74   pRight argument
13f9c 73 0a 2a 2a 20 69 66 20 69 74 20 66 61 69 6c 73  s.** if it fails
13f9d 20 64 75 65 20 74 6f 20 61 20 6d 61 6c 6c 6f 63   due to a malloc
13f9e 20 70 72 6f 62 6c 65 6d 2e 0a 2a 2f 0a 53 51 4c   problem..*/.SQL
13f9f 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72  ITE_PRIVATE Expr
13fa0 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4f 72 46   *sqlite3ExprOrF
13fa1 72 65 65 28 69 6e 74 20 6f 70 2c 20 45 78 70 72  ree(int op, Expr
13fa2 20 2a 70 4c 65 66 74 2c 20 45 78 70 72 20 2a 70   *pLeft, Expr *p
13fa3 52 69 67 68 74 2c 20 63 6f 6e 73 74 20 54 6f 6b  Right, const Tok
13fa4 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20 45  en *pToken){.  E
13fa5 78 70 72 20 2a 70 4e 65 77 20 3d 20 73 71 6c 69  xpr *pNew = sqli
13fa6 74 65 33 45 78 70 72 28 6f 70 2c 20 70 4c 65 66  te3Expr(op, pLef
13fa7 74 2c 20 70 52 69 67 68 74 2c 20 70 54 6f 6b 65  t, pRight, pToke
13fa8 6e 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d  n);.  if( pNew==
13fa9 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
13faa 45 78 70 72 44 65 6c 65 74 65 28 70 4c 65 66 74  ExprDelete(pLeft
13fab 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  );.    sqlite3Ex
13fac 70 72 44 65 6c 65 74 65 28 70 52 69 67 68 74 29  prDelete(pRight)
13fad 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
13fae 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 68  New;.}../*.** Wh
13faf 65 6e 20 64 6f 69 6e 67 20 61 20 6e 65 73 74 65  en doing a neste
13fb0 64 20 70 61 72 73 65 2c 20 79 6f 75 20 63 61 6e  d parse, you can
13fb1 20 69 6e 63 6c 75 64 65 20 74 65 72 6d 73 20 69   include terms i
13fb2 6e 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 0a  n an expression.
13fb3 2a 2a 20 74 68 61 74 20 6c 6f 6f 6b 20 6c 69 6b  ** that look lik
13fb4 65 20 74 68 69 73 3a 20 20 20 23 30 20 23 31 20  e this:   #0 #1 
13fb5 23 32 20 2e 2e 2e 20 20 54 68 65 73 65 20 74 65  #2 ...  These te
13fb6 72 6d 73 20 72 65 66 65 72 20 74 6f 20 65 6c 65  rms refer to ele
13fb7 6d 65 6e 74 73 0a 2a 2a 20 6f 6e 20 74 68 65 20  ments.** on the 
13fb8 73 74 61 63 6b 2e 20 20 22 23 30 22 20 6d 65 61  stack.  "#0" mea
13fb9 6e 73 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68  ns the top of th
13fba 65 20 73 74 61 63 6b 2e 0a 2a 2a 20 22 23 31 22  e stack..** "#1"
13fbb 20 6d 65 61 6e 73 20 74 68 65 20 6e 65 78 74 20   means the next 
13fbc 64 6f 77 6e 20 6f 6e 20 74 68 65 20 73 74 61 63  down on the stac
13fbd 6b 2e 20 20 41 6e 64 20 73 6f 20 66 6f 72 74 68  k.  And so forth
13fbe 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
13fbf 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 62  tine is called b
13fc0 79 20 74 68 65 20 70 61 72 73 65 72 20 74 6f 20  y the parser to 
13fc1 64 65 61 6c 20 77 69 74 68 20 6f 6e 20 6f 66 20  deal with on of 
13fc2 74 68 6f 73 65 20 74 65 72 6d 73 2e 0a 2a 2a 20  those terms..** 
13fc3 49 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 67  It immediately g
13fc4 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20 74 6f  enerates code to
13fc5 20 73 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65   store the value
13fc6 20 69 6e 20 61 20 6d 65 6d 6f 72 79 20 6c 6f 63   in a memory loc
13fc7 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 72 65  ation..** The re
13fc8 74 75 72 6e 73 20 61 6e 20 65 78 70 72 65 73 73  turns an express
13fc9 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 63 6f  ion that will co
13fca 64 65 20 74 6f 20 65 78 74 72 61 63 74 20 74 68  de to extract th
13fcb 65 20 76 61 6c 75 65 20 66 72 6f 6d 0a 2a 2a 20  e value from.** 
13fcc 74 68 61 74 20 6d 65 6d 6f 72 79 20 6c 6f 63 61  that memory loca
13fcd 74 69 6f 6e 20 61 73 20 6e 65 65 64 65 64 2e 0a  tion as needed..
13fce 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
13fcf 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 52  E Expr *sqlite3R
13fd0 65 67 69 73 74 65 72 45 78 70 72 28 50 61 72 73  egisterExpr(Pars
13fd1 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e  e *pParse, Token
13fd2 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20 56 64 62   *pToken){.  Vdb
13fd3 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70  e *v = pParse->p
13fd4 56 64 62 65 3b 0a 20 20 45 78 70 72 20 2a 70 3b  Vdbe;.  Expr *p;
13fd5 0a 20 20 69 6e 74 20 64 65 70 74 68 3b 0a 20 20  .  int depth;.  
13fd6 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74  if( pParse->nest
13fd7 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  ed==0 ){.    sql
13fd8 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
13fd9 72 73 65 2c 20 22 6e 65 61 72 20 5c 22 25 54 5c  rse, "near \"%T\
13fda 22 3a 20 73 79 6e 74 61 78 20 65 72 72 6f 72 22  ": syntax error"
13fdb 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 72  , pToken);.    r
13fdc 65 74 75 72 6e 20 73 71 6c 69 74 65 33 45 78 70  eturn sqlite3Exp
13fdd 72 28 54 4b 5f 4e 55 4c 4c 2c 20 30 2c 20 30 2c  r(TK_NULL, 0, 0,
13fde 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 76   0);.  }.  if( v
13fdf 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
13fe0 20 20 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70    p = sqlite3Exp
13fe1 72 28 54 4b 5f 52 45 47 49 53 54 45 52 2c 20 30  r(TK_REGISTER, 0
13fe2 2c 20 30 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20 20  , 0, pToken);.  
13fe3 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20  if( p==0 ){.    
13fe4 72 65 74 75 72 6e 20 30 3b 20 20 2f 2a 20 4d 61  return 0;  /* Ma
13fe5 6c 6c 6f 63 20 66 61 69 6c 65 64 20 2a 2f 0a 20  lloc failed */. 
13fe6 20 7d 0a 20 20 64 65 70 74 68 20 3d 20 61 74 6f   }.  depth = ato
13fe7 69 28 28 63 68 61 72 2a 29 26 70 54 6f 6b 65 6e  i((char*)&pToken
13fe8 2d 3e 7a 5b 31 5d 29 3b 0a 20 20 70 2d 3e 69 54  ->z[1]);.  p->iT
13fe9 61 62 6c 65 20 3d 20 70 50 61 72 73 65 2d 3e 6e  able = pParse->n
13fea 4d 65 6d 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33  Mem++;.  sqlite3
13feb 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
13fec 44 75 70 2c 20 64 65 70 74 68 2c 20 30 29 3b 0a  Dup, depth, 0);.
13fed 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
13fee 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 53 74 6f 72  Op(v, OP_MemStor
13fef 65 2c 20 70 2d 3e 69 54 61 62 6c 65 2c 20 31 29  e, p->iTable, 1)
13ff0 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  ;.  return p;.}.
13ff1 0a 2f 2a 0a 2a 2a 20 4a 6f 69 6e 20 74 77 6f 20  ./*.** Join two 
13ff2 65 78 70 72 65 73 73 69 6f 6e 73 20 75 73 69 6e  expressions usin
13ff3 67 20 61 6e 20 41 4e 44 20 6f 70 65 72 61 74 6f  g an AND operato
13ff4 72 2e 20 20 49 66 20 65 69 74 68 65 72 20 65 78  r.  If either ex
13ff5 70 72 65 73 73 69 6f 6e 20 69 73 0a 2a 2a 20 4e  pression is.** N
13ff6 55 4c 4c 2c 20 74 68 65 6e 20 6a 75 73 74 20 72  ULL, then just r
13ff7 65 74 75 72 6e 20 74 68 65 20 6f 74 68 65 72 20  eturn the other 
13ff8 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2f 0a 53  expression..*/.S
13ff9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78  QLITE_PRIVATE Ex
13ffa 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 41  pr *sqlite3ExprA
13ffb 6e 64 28 45 78 70 72 20 2a 70 4c 65 66 74 2c 20  nd(Expr *pLeft, 
13ffc 45 78 70 72 20 2a 70 52 69 67 68 74 29 7b 0a 20  Expr *pRight){. 
13ffd 20 69 66 28 20 70 4c 65 66 74 3d 3d 30 20 29 7b   if( pLeft==0 ){
13ffe 0a 20 20 20 20 72 65 74 75 72 6e 20 70 52 69 67  .    return pRig
13fff 68 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  ht;.  }else if( 
14000 70 52 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20  pRight==0 ){.   
14001 20 72 65 74 75 72 6e 20 70 4c 65 66 74 3b 0a 20   return pLeft;. 
14002 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75   }else{.    retu
14003 72 6e 20 73 71 6c 69 74 65 33 45 78 70 72 28 54  rn sqlite3Expr(T
14004 4b 5f 41 4e 44 2c 20 70 4c 65 66 74 2c 20 70 52  K_AND, pLeft, pR
14005 69 67 68 74 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a  ight, 0);.  }.}.
14006 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 45  ./*.** Set the E
14007 78 70 72 2e 73 70 61 6e 20 66 69 65 6c 64 20 6f  xpr.span field o
14008 66 20 74 68 65 20 67 69 76 65 6e 20 65 78 70 72  f the given expr
14009 65 73 73 69 6f 6e 20 74 6f 20 73 70 61 6e 20 61  ession to span a
1400a 6c 6c 0a 2a 2a 20 74 65 78 74 20 62 65 74 77 65  ll.** text betwe
1400b 65 6e 20 74 68 65 20 74 77 6f 20 67 69 76 65 6e  en the two given
1400c 20 74 6f 6b 65 6e 73 2e 0a 2a 2f 0a 53 51 4c 49   tokens..*/.SQLI
1400d 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1400e 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28  sqlite3ExprSpan(
1400f 45 78 70 72 20 2a 70 45 78 70 72 2c 20 54 6f 6b  Expr *pExpr, Tok
14010 65 6e 20 2a 70 4c 65 66 74 2c 20 54 6f 6b 65 6e  en *pLeft, Token
14011 20 2a 70 52 69 67 68 74 29 7b 0a 20 20 61 73 73   *pRight){.  ass
14012 65 72 74 28 20 70 52 69 67 68 74 21 3d 30 20 29  ert( pRight!=0 )
14013 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 65 66  ;.  assert( pLef
14014 74 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 21 73  t!=0 );.  if( !s
14015 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69 6c  qlite3MallocFail
14016 65 64 28 29 20 26 26 20 70 52 69 67 68 74 2d 3e  ed() && pRight->
14017 7a 20 26 26 20 70 4c 65 66 74 2d 3e 7a 20 29 7b  z && pLeft->z ){
14018 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 65  .    assert( pLe
14019 66 74 2d 3e 64 79 6e 3d 3d 30 20 7c 7c 20 70 4c  ft->dyn==0 || pL
1401a 65 66 74 2d 3e 7a 5b 70 4c 65 66 74 2d 3e 6e 5d  eft->z[pLeft->n]
1401b 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70  ==0 );.    if( p
1401c 4c 65 66 74 2d 3e 64 79 6e 3d 3d 30 20 26 26 20  Left->dyn==0 && 
1401d 70 52 69 67 68 74 2d 3e 64 79 6e 3d 3d 30 20 29  pRight->dyn==0 )
1401e 7b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 73  {.      pExpr->s
1401f 70 61 6e 2e 7a 20 3d 20 70 4c 65 66 74 2d 3e 7a  pan.z = pLeft->z
14020 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 73  ;.      pExpr->s
14021 70 61 6e 2e 6e 20 3d 20 70 52 69 67 68 74 2d 3e  pan.n = pRight->
14022 6e 20 2b 20 28 70 52 69 67 68 74 2d 3e 7a 20 2d  n + (pRight->z -
14023 20 70 4c 65 66 74 2d 3e 7a 29 3b 0a 20 20 20 20   pLeft->z);.    
14024 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 45 78  }else{.      pEx
14025 70 72 2d 3e 73 70 61 6e 2e 7a 20 3d 20 30 3b 0a  pr->span.z = 0;.
14026 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
14027 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20 61 20 6e  ** Construct a n
14028 65 77 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f  ew expression no
14029 64 65 20 66 6f 72 20 61 20 66 75 6e 63 74 69 6f  de for a functio
1402a 6e 20 77 69 74 68 20 6d 75 6c 74 69 70 6c 65 0a  n with multiple.
1402b 2a 2a 20 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2f  ** arguments..*/
1402c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1402d 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70  Expr *sqlite3Exp
1402e 72 46 75 6e 63 74 69 6f 6e 28 45 78 70 72 4c 69  rFunction(ExprLi
1402f 73 74 20 2a 70 4c 69 73 74 2c 20 54 6f 6b 65 6e  st *pList, Token
14030 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20 45 78 70   *pToken){.  Exp
14031 72 20 2a 70 4e 65 77 3b 0a 20 20 61 73 73 65 72  r *pNew;.  asser
14032 74 28 20 70 54 6f 6b 65 6e 20 29 3b 0a 20 20 70  t( pToken );.  p
14033 4e 65 77 20 3d 20 73 71 6c 69 74 65 4d 61 6c 6c  New = sqliteMall
14034 6f 63 28 20 73 69 7a 65 6f 66 28 45 78 70 72 29  oc( sizeof(Expr)
14035 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d   );.  if( pNew==
14036 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
14037 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70  ExprListDelete(p
14038 4c 69 73 74 29 3b 20 2f 2a 20 41 76 6f 69 64 20  List); /* Avoid 
14039 6c 65 61 6b 69 6e 67 20 6d 65 6d 6f 72 79 20 77  leaking memory w
1403a 68 65 6e 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73  hen malloc fails
1403b 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 30   */.    return 0
1403c 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 6f 70  ;.  }.  pNew->op
1403d 20 3d 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3b 0a   = TK_FUNCTION;.
1403e 20 20 70 4e 65 77 2d 3e 70 4c 69 73 74 20 3d 20    pNew->pList = 
1403f 70 4c 69 73 74 3b 0a 20 20 61 73 73 65 72 74 28  pList;.  assert(
14040 20 70 54 6f 6b 65 6e 2d 3e 64 79 6e 3d 3d 30 20   pToken->dyn==0 
14041 29 3b 0a 20 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e  );.  pNew->token
14042 20 3d 20 2a 70 54 6f 6b 65 6e 3b 0a 20 20 70 4e   = *pToken;.  pN
14043 65 77 2d 3e 73 70 61 6e 20 3d 20 70 4e 65 77 2d  ew->span = pNew-
14044 3e 74 6f 6b 65 6e 3b 0a 0a 20 20 73 71 6c 69 74  >token;..  sqlit
14045 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28  e3ExprSetHeight(
14046 70 4e 65 77 29 3b 0a 20 20 72 65 74 75 72 6e 20  pNew);.  return 
14047 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  pNew;.}../*.** A
14048 73 73 69 67 6e 20 61 20 76 61 72 69 61 62 6c 65  ssign a variable
14049 20 6e 75 6d 62 65 72 20 74 6f 20 61 6e 20 65 78   number to an ex
1404a 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 65 6e  pression that en
1404b 63 6f 64 65 73 20 61 20 77 69 6c 64 63 61 72 64  codes a wildcard
1404c 0a 2a 2a 20 69 6e 20 74 68 65 20 6f 72 69 67 69  .** in the origi
1404d 6e 61 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  nal SQL statemen
1404e 74 2e 20 20 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 63  t.  .**.** Wildc
1404f 61 72 64 73 20 63 6f 6e 73 69 73 74 69 6e 67 20  ards consisting 
14050 6f 66 20 61 20 73 69 6e 67 6c 65 20 22 3f 22 20  of a single "?" 
14051 61 72 65 20 61 73 73 69 67 6e 65 64 20 74 68 65  are assigned the
14052 20 6e 65 78 74 20 73 65 71 75 65 6e 74 69 61 6c   next sequential
14053 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 6e 75 6d  .** variable num
14054 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 63  ber..**.** Wildc
14055 61 72 64 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  ards of the form
14056 20 22 3f 6e 6e 6e 22 20 61 72 65 20 61 73 73 69   "?nnn" are assi
14057 67 6e 65 64 20 74 68 65 20 6e 75 6d 62 65 72 20  gned the number 
14058 22 6e 6e 6e 22 2e 20 20 57 65 20 6d 61 6b 65 0a  "nnn".  We make.
14059 2a 2a 20 73 75 72 65 20 22 6e 6e 6e 22 20 69 73  ** sure "nnn" is
1405a 20 6e 6f 74 20 74 6f 6f 20 62 65 20 74 6f 20 61   not too be to a
1405b 76 6f 69 64 20 61 20 64 65 6e 69 61 6c 20 6f 66  void a denial of
1405c 20 73 65 72 76 69 63 65 20 61 74 74 61 63 6b 20   service attack 
1405d 77 68 65 6e 0a 2a 2a 20 74 68 65 20 53 51 4c 20  when.** the SQL 
1405e 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 65 73 20  statement comes 
1405f 66 72 6f 6d 20 61 6e 20 65 78 74 65 72 6e 61 6c  from an external
14060 20 73 6f 75 72 63 65 2e 0a 2a 2a 0a 2a 2a 20 57   source..**.** W
14061 69 6c 64 63 61 72 64 73 20 6f 66 20 74 68 65 20  ildcards of the 
14062 66 6f 72 6d 20 22 3a 61 61 61 22 20 6f 72 20 22  form ":aaa" or "
14063 24 61 61 61 22 20 61 72 65 20 61 73 73 69 67 6e  $aaa" are assign
14064 65 64 20 74 68 65 20 73 61 6d 65 20 6e 75 6d 62  ed the same numb
14065 65 72 0a 2a 2a 20 61 73 20 74 68 65 20 70 72 65  er.** as the pre
14066 76 69 6f 75 73 20 69 6e 73 74 61 6e 63 65 20 6f  vious instance o
14067 66 20 74 68 65 20 73 61 6d 65 20 77 69 6c 64 63  f the same wildc
14068 61 72 64 2e 20 20 4f 72 20 69 66 20 74 68 69 73  ard.  Or if this
14069 20 69 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a   is the first.**
1406a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
1406b 20 77 69 6c 64 63 61 72 64 2c 20 74 68 65 20 6e   wildcard, the n
1406c 65 78 74 20 73 65 71 75 65 6e 69 61 6c 20 76 61  ext sequenial va
1406d 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 69 73  riable number is
1406e 0a 2a 2a 20 61 73 73 69 67 6e 65 64 2e 0a 2a 2f  .** assigned..*/
1406f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
14070 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
14071 41 73 73 69 67 6e 56 61 72 4e 75 6d 62 65 72 28  AssignVarNumber(
14072 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45  Parse *pParse, E
14073 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 54  xpr *pExpr){.  T
14074 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 3b 0a 20 20  oken *pToken;.  
14075 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20 72  if( pExpr==0 ) r
14076 65 74 75 72 6e 3b 0a 20 20 70 54 6f 6b 65 6e 20  eturn;.  pToken 
14077 3d 20 26 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 3b  = &pExpr->token;
14078 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f 6b 65  .  assert( pToke
14079 6e 2d 3e 6e 3e 3d 31 20 29 3b 0a 20 20 61 73 73  n->n>=1 );.  ass
1407a 65 72 74 28 20 70 54 6f 6b 65 6e 2d 3e 7a 21 3d  ert( pToken->z!=
1407b 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
1407c 54 6f 6b 65 6e 2d 3e 7a 5b 30 5d 21 3d 30 20 29  Token->z[0]!=0 )
1407d 3b 0a 20 20 69 66 28 20 70 54 6f 6b 65 6e 2d 3e  ;.  if( pToken->
1407e 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 57  n==1 ){.    /* W
1407f 69 6c 64 63 61 72 64 20 6f 66 20 74 68 65 20 66  ildcard of the f
14080 6f 72 6d 20 22 3f 22 2e 20 20 41 73 73 69 67 6e  orm "?".  Assign
14081 20 74 68 65 20 6e 65 78 74 20 76 61 72 69 61 62   the next variab
14082 6c 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20  le number */.   
14083 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d   pExpr->iTable =
14084 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 56 61 72 3b   ++pParse->nVar;
14085 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 54 6f  .  }else if( pTo
14086 6b 65 6e 2d 3e 7a 5b 30 5d 3d 3d 27 3f 27 20 29  ken->z[0]=='?' )
14087 7b 0a 20 20 20 20 2f 2a 20 57 69 6c 64 63 61 72  {.    /* Wildcar
14088 64 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3f  d of the form "?
14089 6e 6e 6e 22 2e 20 20 43 6f 6e 76 65 72 74 20 22  nnn".  Convert "
1408a 6e 6e 6e 22 20 74 6f 20 61 6e 20 69 6e 74 65 67  nnn" to an integ
1408b 65 72 20 61 6e 64 0a 20 20 20 20 2a 2a 20 75 73  er and.    ** us
1408c 65 20 69 74 20 61 73 20 74 68 65 20 76 61 72 69  e it as the vari
1408d 61 62 6c 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20  able number */. 
1408e 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 70 45     int i;.    pE
1408f 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 69 20  xpr->iTable = i 
14090 3d 20 61 74 6f 69 28 28 63 68 61 72 2a 29 26 70  = atoi((char*)&p
14091 54 6f 6b 65 6e 2d 3e 7a 5b 31 5d 29 3b 0a 20 20  Token->z[1]);.  
14092 20 20 69 66 28 20 69 3c 31 20 7c 7c 20 69 3e 53    if( i<1 || i>S
14093 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49 41 42  QLITE_MAX_VARIAB
14094 4c 45 5f 4e 55 4d 42 45 52 20 29 7b 0a 20 20 20  LE_NUMBER ){.   
14095 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
14096 73 67 28 70 50 61 72 73 65 2c 20 22 76 61 72 69  sg(pParse, "vari
14097 61 62 6c 65 20 6e 75 6d 62 65 72 20 6d 75 73 74  able number must
14098 20 62 65 20 62 65 74 77 65 65 6e 20 3f 31 20 61   be between ?1 a
14099 6e 64 20 3f 25 64 22 2c 0a 20 20 20 20 20 20 20  nd ?%d",.       
1409a 20 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41     SQLITE_MAX_VA
1409b 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 29 3b 0a  RIABLE_NUMBER);.
1409c 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3e      }.    if( i>
1409d 70 50 61 72 73 65 2d 3e 6e 56 61 72 20 29 7b 0a  pParse->nVar ){.
1409e 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 56        pParse->nV
1409f 61 72 20 3d 20 69 3b 0a 20 20 20 20 7d 0a 20 20  ar = i;.    }.  
140a0 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 57 69  }else{.    /* Wi
140a1 6c 64 63 61 72 64 73 20 6f 66 20 74 68 65 20 66  ldcards of the f
140a2 6f 72 6d 20 22 3a 61 61 61 22 20 6f 72 20 22 24  orm ":aaa" or "$
140a3 61 61 61 22 2e 20 20 52 65 75 73 65 20 74 68 65  aaa".  Reuse the
140a4 20 73 61 6d 65 20 76 61 72 69 61 62 6c 65 0a 20   same variable. 
140a5 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 61 73 20     ** number as 
140a6 74 68 65 20 70 72 69 6f 72 20 61 70 70 65 61 72  the prior appear
140a7 61 6e 63 65 20 6f 66 20 74 68 65 20 73 61 6d 65  ance of the same
140a8 20 6e 61 6d 65 2c 20 6f 72 20 69 66 20 74 68 65   name, or if the
140a9 20 6e 61 6d 65 0a 20 20 20 20 2a 2a 20 68 61 73   name.    ** has
140aa 20 6e 65 76 65 72 20 61 70 70 65 61 72 65 64 20   never appeared 
140ab 62 65 66 6f 72 65 2c 20 72 65 75 73 65 20 74 68  before, reuse th
140ac 65 20 73 61 6d 65 20 76 61 72 69 61 62 6c 65 20  e same variable 
140ad 6e 75 6d 62 65 72 0a 20 20 20 20 2a 2f 0a 20 20  number.    */.  
140ae 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 20 20    int i, n;.    
140af 6e 20 3d 20 70 54 6f 6b 65 6e 2d 3e 6e 3b 0a 20  n = pToken->n;. 
140b0 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50     for(i=0; i<pP
140b1 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 3b 20  arse->nVarExpr; 
140b2 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78 70 72  i++){.      Expr
140b3 20 2a 70 45 3b 0a 20 20 20 20 20 20 69 66 28 20   *pE;.      if( 
140b4 28 70 45 20 3d 20 70 50 61 72 73 65 2d 3e 61 70  (pE = pParse->ap
140b5 56 61 72 45 78 70 72 5b 69 5d 29 21 3d 30 0a 20  VarExpr[i])!=0. 
140b6 20 20 20 20 20 20 20 20 20 26 26 20 70 45 2d 3e           && pE->
140b7 74 6f 6b 65 6e 2e 6e 3d 3d 6e 0a 20 20 20 20 20  token.n==n.     
140b8 20 20 20 20 20 26 26 20 6d 65 6d 63 6d 70 28 70       && memcmp(p
140b9 45 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 54 6f 6b  E->token.z, pTok
140ba 65 6e 2d 3e 7a 2c 20 6e 29 3d 3d 30 20 29 7b 0a  en->z, n)==0 ){.
140bb 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69          pExpr->i
140bc 54 61 62 6c 65 20 3d 20 70 45 2d 3e 69 54 61 62  Table = pE->iTab
140bd 6c 65 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  le;.        brea
140be 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
140bf 0a 20 20 20 20 69 66 28 20 69 3e 3d 70 50 61 72  .    if( i>=pPar
140c0 73 65 2d 3e 6e 56 61 72 45 78 70 72 20 29 7b 0a  se->nVarExpr ){.
140c1 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61        pExpr->iTa
140c2 62 6c 65 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  ble = ++pParse->
140c3 6e 56 61 72 3b 0a 20 20 20 20 20 20 69 66 28 20  nVar;.      if( 
140c4 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72  pParse->nVarExpr
140c5 3e 3d 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78  >=pParse->nVarEx
140c6 70 72 41 6c 6c 6f 63 2d 31 20 29 7b 0a 20 20 20  prAlloc-1 ){.   
140c7 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 56 61       pParse->nVa
140c8 72 45 78 70 72 41 6c 6c 6f 63 20 2b 3d 20 70 50  rExprAlloc += pP
140c9 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 41 6c  arse->nVarExprAl
140ca 6c 6f 63 20 2b 20 31 30 3b 0a 20 20 20 20 20 20  loc + 10;.      
140cb 20 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72 45    pParse->apVarE
140cc 78 70 72 20 3d 20 73 71 6c 69 74 65 52 65 61 6c  xpr = sqliteReal
140cd 6c 6f 63 4f 72 46 72 65 65 28 70 50 61 72 73 65  locOrFree(pParse
140ce 2d 3e 61 70 56 61 72 45 78 70 72 2c 0a 20 20 20  ->apVarExpr,.   
140cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
140d0 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 56 61 72      pParse->nVar
140d1 45 78 70 72 41 6c 6c 6f 63 2a 73 69 7a 65 6f 66  ExprAlloc*sizeof
140d2 28 70 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78  (pParse->apVarEx
140d3 70 72 5b 30 5d 29 20 29 3b 0a 20 20 20 20 20 20  pr[0]) );.      
140d4 7d 0a 20 20 20 20 20 20 69 66 28 20 21 73 71 6c  }.      if( !sql
140d5 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69 6c 65 64  ite3MallocFailed
140d6 28 29 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73  () ){.        as
140d7 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 61 70  sert( pParse->ap
140d8 56 61 72 45 78 70 72 21 3d 30 20 29 3b 0a 20 20  VarExpr!=0 );.  
140d9 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 70        pParse->ap
140da 56 61 72 45 78 70 72 5b 70 50 61 72 73 65 2d 3e  VarExpr[pParse->
140db 6e 56 61 72 45 78 70 72 2b 2b 5d 20 3d 20 70 45  nVarExpr++] = pE
140dc 78 70 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  xpr;.      }.   
140dd 20 7d 0a 20 20 7d 20 0a 20 20 69 66 28 20 21 70   }.  } .  if( !p
140de 50 61 72 73 65 2d 3e 6e 45 72 72 20 26 26 20 70  Parse->nErr && p
140df 50 61 72 73 65 2d 3e 6e 56 61 72 3e 53 51 4c 49  Parse->nVar>SQLI
140e0 54 45 5f 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f  TE_MAX_VARIABLE_
140e1 4e 55 4d 42 45 52 20 29 7b 0a 20 20 20 20 73 71  NUMBER ){.    sq
140e2 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
140e3 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20  arse, "too many 
140e4 53 51 4c 20 76 61 72 69 61 62 6c 65 73 22 29 3b  SQL variables");
140e5 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
140e6 63 75 72 73 69 76 65 6c 79 20 64 65 6c 65 74 65  cursively delete
140e7 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74   an expression t
140e8 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ree..*/.SQLITE_P
140e9 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
140ea 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 45 78  te3ExprDelete(Ex
140eb 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d  pr *p){.  if( p=
140ec 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69  =0 ) return;.  i
140ed 66 28 20 70 2d 3e 73 70 61 6e 2e 64 79 6e 20 29  f( p->span.dyn )
140ee 20 73 71 6c 69 74 65 46 72 65 65 28 28 63 68 61   sqliteFree((cha
140ef 72 2a 29 70 2d 3e 73 70 61 6e 2e 7a 29 3b 0a 20  r*)p->span.z);. 
140f0 20 69 66 28 20 70 2d 3e 74 6f 6b 65 6e 2e 64 79   if( p->token.dy
140f1 6e 20 29 20 73 71 6c 69 74 65 46 72 65 65 28 28  n ) sqliteFree((
140f2 63 68 61 72 2a 29 70 2d 3e 74 6f 6b 65 6e 2e 7a  char*)p->token.z
140f3 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
140f4 44 65 6c 65 74 65 28 70 2d 3e 70 4c 65 66 74 29  Delete(p->pLeft)
140f5 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44  ;.  sqlite3ExprD
140f6 65 6c 65 74 65 28 70 2d 3e 70 52 69 67 68 74 29  elete(p->pRight)
140f7 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c  ;.  sqlite3ExprL
140f8 69 73 74 44 65 6c 65 74 65 28 70 2d 3e 70 4c 69  istDelete(p->pLi
140f9 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 65  st);.  sqlite3Se
140fa 6c 65 63 74 44 65 6c 65 74 65 28 70 2d 3e 70 53  lectDelete(p->pS
140fb 65 6c 65 63 74 29 3b 0a 20 20 73 71 6c 69 74 65  elect);.  sqlite
140fc 46 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  Free(p);.}../*.*
140fd 2a 20 54 68 65 20 45 78 70 72 2e 74 6f 6b 65 6e  * The Expr.token
140fe 20 66 69 65 6c 64 20 6d 69 67 68 74 20 62 65 20   field might be 
140ff 61 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c  a string literal
14100 20 74 68 61 74 20 69 73 20 71 75 6f 74 65 64 2e   that is quoted.
14101 0a 2a 2a 20 49 66 20 73 6f 2c 20 72 65 6d 6f 76  .** If so, remov
14102 65 20 74 68 65 20 71 75 6f 74 61 74 69 6f 6e 20  e the quotation 
14103 6d 61 72 6b 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  marks..*/.SQLITE
14104 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
14105 6c 69 74 65 33 44 65 71 75 6f 74 65 45 78 70 72  lite3DequoteExpr
14106 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 66 28  (Expr *p){.  if(
14107 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65   ExprHasAnyPrope
14108 72 74 79 28 70 2c 20 45 50 5f 44 65 71 75 6f 74  rty(p, EP_Dequot
14109 65 64 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ed) ){.    retur
1410a 6e 3b 0a 20 20 7d 0a 20 20 45 78 70 72 53 65 74  n;.  }.  ExprSet
1410b 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 44  Property(p, EP_D
1410c 65 71 75 6f 74 65 64 29 3b 0a 20 20 69 66 28 20  equoted);.  if( 
1410d 70 2d 3e 74 6f 6b 65 6e 2e 64 79 6e 3d 3d 30 20  p->token.dyn==0 
1410e 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 54 6f  ){.    sqlite3To
1410f 6b 65 6e 43 6f 70 79 28 26 70 2d 3e 74 6f 6b 65  kenCopy(&p->toke
14110 6e 2c 20 26 70 2d 3e 74 6f 6b 65 6e 29 3b 0a 20  n, &p->token);. 
14111 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 65 71 75   }.  sqlite3Dequ
14112 6f 74 65 28 28 63 68 61 72 2a 29 70 2d 3e 74 6f  ote((char*)p->to
14113 6b 65 6e 2e 7a 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  ken.z);.}.../*.*
14114 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
14115 67 72 6f 75 70 20 6f 66 20 72 6f 75 74 69 6e 65  group of routine
14116 73 20 6d 61 6b 65 20 64 65 65 70 20 63 6f 70 69  s make deep copi
14117 65 73 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e  es of expression
14118 73 2c 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e  s,.** expression
14119 20 6c 69 73 74 73 2c 20 49 44 20 6c 69 73 74 73   lists, ID lists
1411a 2c 20 61 6e 64 20 73 65 6c 65 63 74 20 73 74 61  , and select sta
1411b 74 65 6d 65 6e 74 73 2e 20 20 54 68 65 20 63 6f  tements.  The co
1411c 70 69 65 73 20 63 61 6e 0a 2a 2a 20 62 65 20 64  pies can.** be d
1411d 65 6c 65 74 65 64 20 28 62 79 20 62 65 69 6e 67  eleted (by being
1411e 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 69 72   passed to their
1411f 20 72 65 73 70 65 63 74 69 76 65 20 2e 2e 2e 44   respective ...D
14120 65 6c 65 74 65 28 29 20 72 6f 75 74 69 6e 65 73  elete() routines
14121 29 0a 2a 2a 20 77 69 74 68 6f 75 74 20 65 66 66  ).** without eff
14122 65 63 74 69 6e 67 20 74 68 65 20 6f 72 69 67 69  ecting the origi
14123 6e 61 6c 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  nals..**.** The 
14124 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2c  expression list,
14125 20 49 44 2c 20 61 6e 64 20 73 6f 75 72 63 65 20   ID, and source 
14126 6c 69 73 74 73 20 72 65 74 75 72 6e 20 62 79 20  lists return by 
14127 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
14128 75 70 28 29 2c 0a 2a 2a 20 73 71 6c 69 74 65 33  up(),.** sqlite3
14129 49 64 4c 69 73 74 44 75 70 28 29 2c 20 61 6e 64  IdListDup(), and
1412a 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44   sqlite3SrcListD
1412b 75 70 28 29 20 63 61 6e 20 6e 6f 74 20 62 65 20  up() can not be 
1412c 66 75 72 74 68 65 72 20 65 78 70 61 6e 64 65 64  further expanded
1412d 20 0a 2a 2a 20 62 79 20 73 75 62 73 65 71 75 65   .** by subseque
1412e 6e 74 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69  nt calls to sqli
1412f 74 65 2a 4c 69 73 74 41 70 70 65 6e 64 28 29 20  te*ListAppend() 
14130 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20  routines..**.** 
14131 41 6e 79 20 74 61 62 6c 65 73 20 74 68 61 74 20  Any tables that 
14132 74 68 65 20 53 72 63 4c 69 73 74 20 6d 69 67 68  the SrcList migh
14133 74 20 70 6f 69 6e 74 20 74 6f 20 61 72 65 20 6e  t point to are n
14134 6f 74 20 64 75 70 6c 69 63 61 74 65 64 2e 0a 2a  ot duplicated..*
14135 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
14136 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78   Expr *sqlite3Ex
14137 70 72 44 75 70 28 45 78 70 72 20 2a 70 29 7b 0a  prDup(Expr *p){.
14138 20 20 45 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20    Expr *pNew;.  
14139 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72  if( p==0 ) retur
1413a 6e 20 30 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71  n 0;.  pNew = sq
1413b 6c 69 74 65 4d 61 6c 6c 6f 63 52 61 77 28 20 73  liteMallocRaw( s
1413c 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 69  izeof(*p) );.  i
1413d 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74  f( pNew==0 ) ret
1413e 75 72 6e 20 30 3b 0a 20 20 6d 65 6d 63 70 79 28  urn 0;.  memcpy(
1413f 70 4e 65 77 2c 20 70 2c 20 73 69 7a 65 6f 66 28  pNew, p, sizeof(
14140 2a 70 4e 65 77 29 29 3b 0a 20 20 69 66 28 20 70  *pNew));.  if( p
14141 2d 3e 74 6f 6b 65 6e 2e 7a 21 3d 30 20 29 7b 0a  ->token.z!=0 ){.
14142 20 20 20 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e 2e      pNew->token.
14143 7a 20 3d 20 28 75 38 2a 29 73 71 6c 69 74 65 53  z = (u8*)sqliteS
14144 74 72 4e 44 75 70 28 28 63 68 61 72 2a 29 70 2d  trNDup((char*)p-
14145 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 2d 3e 74 6f 6b  >token.z, p->tok
14146 65 6e 2e 6e 29 3b 0a 20 20 20 20 70 4e 65 77 2d  en.n);.    pNew-
14147 3e 74 6f 6b 65 6e 2e 64 79 6e 20 3d 20 31 3b 0a  >token.dyn = 1;.
14148 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
14149 65 72 74 28 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e  ert( pNew->token
1414a 2e 7a 3d 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 70  .z==0 );.  }.  p
1414b 4e 65 77 2d 3e 73 70 61 6e 2e 7a 20 3d 20 30 3b  New->span.z = 0;
1414c 0a 20 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d  .  pNew->pLeft =
1414d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
1414e 70 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 70 4e 65  p->pLeft);.  pNe
1414f 77 2d 3e 70 52 69 67 68 74 20 3d 20 73 71 6c 69  w->pRight = sqli
14150 74 65 33 45 78 70 72 44 75 70 28 70 2d 3e 70 52  te3ExprDup(p->pR
14151 69 67 68 74 29 3b 0a 20 20 70 4e 65 77 2d 3e 70  ight);.  pNew->p
14152 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78  List = sqlite3Ex
14153 70 72 4c 69 73 74 44 75 70 28 70 2d 3e 70 4c 69  prListDup(p->pLi
14154 73 74 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 53 65  st);.  pNew->pSe
14155 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65  lect = sqlite3Se
14156 6c 65 63 74 44 75 70 28 70 2d 3e 70 53 65 6c 65  lectDup(p->pSele
14157 63 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4e  ct);.  return pN
14158 65 77 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  ew;.}.SQLITE_PRI
14159 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1415a 33 54 6f 6b 65 6e 43 6f 70 79 28 54 6f 6b 65 6e  3TokenCopy(Token
1415b 20 2a 70 54 6f 2c 20 54 6f 6b 65 6e 20 2a 70 46   *pTo, Token *pF
1415c 72 6f 6d 29 7b 0a 20 20 69 66 28 20 70 54 6f 2d  rom){.  if( pTo-
1415d 3e 64 79 6e 20 29 20 73 71 6c 69 74 65 46 72 65  >dyn ) sqliteFre
1415e 65 28 28 63 68 61 72 2a 29 70 54 6f 2d 3e 7a 29  e((char*)pTo->z)
1415f 3b 0a 20 20 69 66 28 20 70 46 72 6f 6d 2d 3e 7a  ;.  if( pFrom->z
14160 20 29 7b 0a 20 20 20 20 70 54 6f 2d 3e 6e 20 3d   ){.    pTo->n =
14161 20 70 46 72 6f 6d 2d 3e 6e 3b 0a 20 20 20 20 70   pFrom->n;.    p
14162 54 6f 2d 3e 7a 20 3d 20 28 75 38 2a 29 73 71 6c  To->z = (u8*)sql
14163 69 74 65 53 74 72 4e 44 75 70 28 28 63 68 61 72  iteStrNDup((char
14164 2a 29 70 46 72 6f 6d 2d 3e 7a 2c 20 70 46 72 6f  *)pFrom->z, pFro
14165 6d 2d 3e 6e 29 3b 0a 20 20 20 20 70 54 6f 2d 3e  m->n);.    pTo->
14166 64 79 6e 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65  dyn = 1;.  }else
14167 7b 0a 20 20 20 20 70 54 6f 2d 3e 7a 20 3d 20 30  {.    pTo->z = 0
14168 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50  ;.  }.}.SQLITE_P
14169 52 49 56 41 54 45 20 45 78 70 72 4c 69 73 74 20  RIVATE ExprList 
1416a 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74  *sqlite3ExprList
1416b 44 75 70 28 45 78 70 72 4c 69 73 74 20 2a 70 29  Dup(ExprList *p)
1416c 7b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4e  {.  ExprList *pN
1416d 65 77 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70  ew;.  struct Exp
1416e 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  rList_item *pIte
1416f 6d 2c 20 2a 70 4f 6c 64 49 74 65 6d 3b 0a 20 20  m, *pOldItem;.  
14170 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 3d 3d  int i;.  if( p==
14171 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
14172 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 4d 61 6c  pNew = sqliteMal
14173 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 4e 65  loc( sizeof(*pNe
14174 77 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77  w) );.  if( pNew
14175 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
14176 20 20 70 4e 65 77 2d 3e 6e 45 78 70 72 20 3d 20    pNew->nExpr = 
14177 70 4e 65 77 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70  pNew->nAlloc = p
14178 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 4e 65 77 2d  ->nExpr;.  pNew-
14179 3e 61 20 3d 20 70 49 74 65 6d 20 3d 20 73 71 6c  >a = pItem = sql
1417a 69 74 65 4d 61 6c 6c 6f 63 28 20 70 2d 3e 6e 45  iteMalloc( p->nE
1417b 78 70 72 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 5b  xpr*sizeof(p->a[
1417c 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 70 49 74  0]) );.  if( pIt
1417d 65 6d 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  em==0 ){.    sql
1417e 69 74 65 46 72 65 65 28 70 4e 65 77 29 3b 0a 20  iteFree(pNew);. 
1417f 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
14180 20 0a 20 20 70 4f 6c 64 49 74 65 6d 20 3d 20 70   .  pOldItem = p
14181 2d 3e 61 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  ->a;.  for(i=0; 
14182 69 3c 70 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c  i<p->nExpr; i++,
14183 20 70 49 74 65 6d 2b 2b 2c 20 70 4f 6c 64 49 74   pItem++, pOldIt
14184 65 6d 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20  em++){.    Expr 
14185 2a 70 4e 65 77 45 78 70 72 2c 20 2a 70 4f 6c 64  *pNewExpr, *pOld
14186 45 78 70 72 3b 0a 20 20 20 20 70 49 74 65 6d 2d  Expr;.    pItem-
14187 3e 70 45 78 70 72 20 3d 20 70 4e 65 77 45 78 70  >pExpr = pNewExp
14188 72 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  r = sqlite3ExprD
14189 75 70 28 70 4f 6c 64 45 78 70 72 20 3d 20 70 4f  up(pOldExpr = pO
1418a 6c 64 49 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0a  ldItem->pExpr);.
1418b 20 20 20 20 69 66 28 20 70 4f 6c 64 45 78 70 72      if( pOldExpr
1418c 2d 3e 73 70 61 6e 2e 7a 21 3d 30 20 26 26 20 70  ->span.z!=0 && p
1418d 4e 65 77 45 78 70 72 20 29 7b 0a 20 20 20 20 20  NewExpr ){.     
1418e 20 2f 2a 20 41 6c 77 61 79 73 20 6d 61 6b 65 20   /* Always make 
1418f 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 73 70  a copy of the sp
14190 61 6e 20 66 6f 72 20 74 6f 70 2d 6c 65 76 65 6c  an for top-level
14191 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20   expressions in 
14192 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 65 78 70  the.      ** exp
14193 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 20 20 54  ression list.  T
14194 68 65 20 6c 6f 67 69 63 20 69 6e 20 53 45 4c 45  he logic in SELE
14195 43 54 20 70 72 6f 63 65 73 73 69 6e 67 20 74 68  CT processing th
14196 61 74 20 64 65 74 65 72 6d 69 6e 65 73 0a 20 20  at determines.  
14197 20 20 20 20 2a 2a 20 74 68 65 20 6e 61 6d 65 73      ** the names
14198 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74   of columns in t
14199 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6e 65  he result set ne
1419a 65 64 73 20 74 68 69 73 20 69 6e 66 6f 72 6d 61  eds this informa
1419b 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 73 71  tion */.      sq
1419c 6c 69 74 65 33 54 6f 6b 65 6e 43 6f 70 79 28 26  lite3TokenCopy(&
1419d 70 4e 65 77 45 78 70 72 2d 3e 73 70 61 6e 2c 20  pNewExpr->span, 
1419e 26 70 4f 6c 64 45 78 70 72 2d 3e 73 70 61 6e 29  &pOldExpr->span)
1419f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  ;.    }.    asse
141a0 72 74 28 20 70 4e 65 77 45 78 70 72 3d 3d 30 20  rt( pNewExpr==0 
141a1 7c 7c 20 70 4e 65 77 45 78 70 72 2d 3e 73 70 61  || pNewExpr->spa
141a2 6e 2e 7a 21 3d 30 20 0a 20 20 20 20 20 20 20 20  n.z!=0 .        
141a3 20 20 20 20 7c 7c 20 70 4f 6c 64 45 78 70 72 2d      || pOldExpr-
141a4 3e 73 70 61 6e 2e 7a 3d 3d 30 0a 20 20 20 20 20  >span.z==0.     
141a5 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65         || sqlite
141a6 33 4d 61 6c 6c 6f 63 46 61 69 6c 65 64 28 29 20  3MallocFailed() 
141a7 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 7a 4e  );.    pItem->zN
141a8 61 6d 65 20 3d 20 73 71 6c 69 74 65 53 74 72 44  ame = sqliteStrD
141a9 75 70 28 70 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61  up(pOldItem->zNa
141aa 6d 65 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e  me);.    pItem->
141ab 73 6f 72 74 4f 72 64 65 72 20 3d 20 70 4f 6c 64  sortOrder = pOld
141ac 49 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 3b  Item->sortOrder;
141ad 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69 73 41 67  .    pItem->isAg
141ae 67 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 73  g = pOldItem->is
141af 41 67 67 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e  Agg;.    pItem->
141b0 64 6f 6e 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  done = 0;.  }.  
141b1 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a  return pNew;.}..
141b2 2f 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 73  /*.** If cursors
141b3 2c 20 74 72 69 67 67 65 72 73 2c 20 76 69 65 77  , triggers, view
141b4 73 20 61 6e 64 20 73 75 62 71 75 65 72 69 65 73  s and subqueries
141b5 20 61 72 65 20 61 6c 6c 20 6f 6d 69 74 74 65 64   are all omitted
141b6 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 62 75 69   from.** the bui
141b7 6c 64 2c 20 74 68 65 6e 20 6e 6f 6e 65 20 6f 66  ld, then none of
141b8 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72   the following r
141b9 6f 75 74 69 6e 65 73 2c 20 65 78 63 65 70 74 20  outines, except 
141ba 66 6f 72 20 0a 2a 2a 20 73 71 6c 69 74 65 33 53  for .** sqlite3S
141bb 65 6c 65 63 74 44 75 70 28 29 2c 20 63 61 6e 20  electDup(), can 
141bc 62 65 20 63 61 6c 6c 65 64 2e 20 73 71 6c 69 74  be called. sqlit
141bd 65 33 53 65 6c 65 63 74 44 75 70 28 29 20 69 73  e3SelectDup() is
141be 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 63 61   sometimes.** ca
141bf 6c 6c 65 64 20 77 69 74 68 20 61 20 4e 55 4c 4c  lled with a NULL
141c0 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 23 69   argument..*/.#i
141c1 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
141c2 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20  E_OMIT_VIEW) || 
141c3 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
141c4 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 20 5c 0a  OMIT_TRIGGER) \.
141c5 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c   || !defined(SQL
141c6 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
141c7 59 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  Y).SQLITE_PRIVAT
141c8 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74  E SrcList *sqlit
141c9 65 33 53 72 63 4c 69 73 74 44 75 70 28 53 72 63  e3SrcListDup(Src
141ca 4c 69 73 74 20 2a 70 29 7b 0a 20 20 53 72 63 4c  List *p){.  SrcL
141cb 69 73 74 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74  ist *pNew;.  int
141cc 20 69 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b   i;.  int nByte;
141cd 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65  .  if( p==0 ) re
141ce 74 75 72 6e 20 30 3b 0a 20 20 6e 42 79 74 65 20  turn 0;.  nByte 
141cf 3d 20 73 69 7a 65 6f 66 28 2a 70 29 20 2b 20 28  = sizeof(*p) + (
141d0 70 2d 3e 6e 53 72 63 3e 30 20 3f 20 73 69 7a 65  p->nSrc>0 ? size
141d1 6f 66 28 70 2d 3e 61 5b 30 5d 29 20 2a 20 28 70  of(p->a[0]) * (p
141d2 2d 3e 6e 53 72 63 2d 31 29 20 3a 20 30 29 3b 0a  ->nSrc-1) : 0);.
141d3 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 4d    pNew = sqliteM
141d4 61 6c 6c 6f 63 52 61 77 28 20 6e 42 79 74 65 20  allocRaw( nByte 
141d5 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30  );.  if( pNew==0
141d6 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70   ) return 0;.  p
141d7 4e 65 77 2d 3e 6e 53 72 63 20 3d 20 70 4e 65 77  New->nSrc = pNew
141d8 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 53  ->nAlloc = p->nS
141d9 72 63 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  rc;.  for(i=0; i
141da 3c 70 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a  <p->nSrc; i++){.
141db 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69      struct SrcLi
141dc 73 74 5f 69 74 65 6d 20 2a 70 4e 65 77 49 74 65  st_item *pNewIte
141dd 6d 20 3d 20 26 70 4e 65 77 2d 3e 61 5b 69 5d 3b  m = &pNew->a[i];
141de 0a 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c  .    struct SrcL
141df 69 73 74 5f 69 74 65 6d 20 2a 70 4f 6c 64 49 74  ist_item *pOldIt
141e0 65 6d 20 3d 20 26 70 2d 3e 61 5b 69 5d 3b 0a 20  em = &p->a[i];. 
141e1 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a     Table *pTab;.
141e2 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 44      pNewItem->zD
141e3 61 74 61 62 61 73 65 20 3d 20 73 71 6c 69 74 65  atabase = sqlite
141e4 53 74 72 44 75 70 28 70 4f 6c 64 49 74 65 6d 2d  StrDup(pOldItem-
141e5 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 20  >zDatabase);.   
141e6 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 4e 61 6d 65   pNewItem->zName
141e7 20 3d 20 73 71 6c 69 74 65 53 74 72 44 75 70 28   = sqliteStrDup(
141e8 70 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29  pOldItem->zName)
141e9 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e  ;.    pNewItem->
141ea 7a 41 6c 69 61 73 20 3d 20 73 71 6c 69 74 65 53  zAlias = sqliteS
141eb 74 72 44 75 70 28 70 4f 6c 64 49 74 65 6d 2d 3e  trDup(pOldItem->
141ec 7a 41 6c 69 61 73 29 3b 0a 20 20 20 20 70 4e 65  zAlias);.    pNe
141ed 77 49 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65 20  wItem->jointype 
141ee 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 6a 6f 69 6e  = pOldItem->join
141ef 74 79 70 65 3b 0a 20 20 20 20 70 4e 65 77 49 74  type;.    pNewIt
141f0 65 6d 2d 3e 69 43 75 72 73 6f 72 20 3d 20 70 4f  em->iCursor = pO
141f1 6c 64 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b  ldItem->iCursor;
141f2 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 69  .    pNewItem->i
141f3 73 50 6f 70 75 6c 61 74 65 64 20 3d 20 70 4f 6c  sPopulated = pOl
141f4 64 49 74 65 6d 2d 3e 69 73 50 6f 70 75 6c 61 74  dItem->isPopulat
141f5 65 64 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 70  ed;.    pTab = p
141f6 4e 65 77 49 74 65 6d 2d 3e 70 54 61 62 20 3d 20  NewItem->pTab = 
141f7 70 4f 6c 64 49 74 65 6d 2d 3e 70 54 61 62 3b 0a  pOldItem->pTab;.
141f8 20 20 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a      if( pTab ){.
141f9 20 20 20 20 20 20 70 54 61 62 2d 3e 6e 52 65 66        pTab->nRef
141fa 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e  ++;.    }.    pN
141fb 65 77 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20  ewItem->pSelect 
141fc 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44  = sqlite3SelectD
141fd 75 70 28 70 4f 6c 64 49 74 65 6d 2d 3e 70 53 65  up(pOldItem->pSe
141fe 6c 65 63 74 29 3b 0a 20 20 20 20 70 4e 65 77 49  lect);.    pNewI
141ff 74 65 6d 2d 3e 70 4f 6e 20 3d 20 73 71 6c 69 74  tem->pOn = sqlit
14200 65 33 45 78 70 72 44 75 70 28 70 4f 6c 64 49 74  e3ExprDup(pOldIt
14201 65 6d 2d 3e 70 4f 6e 29 3b 0a 20 20 20 20 70 4e  em->pOn);.    pN
14202 65 77 49 74 65 6d 2d 3e 70 55 73 69 6e 67 20 3d  ewItem->pUsing =
14203 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 75   sqlite3IdListDu
14204 70 28 70 4f 6c 64 49 74 65 6d 2d 3e 70 55 73 69  p(pOldItem->pUsi
14205 6e 67 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65  ng);.    pNewIte
14206 6d 2d 3e 63 6f 6c 55 73 65 64 20 3d 20 70 4f 6c  m->colUsed = pOl
14207 64 49 74 65 6d 2d 3e 63 6f 6c 55 73 65 64 3b 0a  dItem->colUsed;.
14208 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65    }.  return pNe
14209 77 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  w;.}.SQLITE_PRIV
1420a 41 54 45 20 49 64 4c 69 73 74 20 2a 73 71 6c 69  ATE IdList *sqli
1420b 74 65 33 49 64 4c 69 73 74 44 75 70 28 49 64 4c  te3IdListDup(IdL
1420c 69 73 74 20 2a 70 29 7b 0a 20 20 49 64 4c 69 73  ist *p){.  IdLis
1420d 74 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20 69  t *pNew;.  int i
1420e 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ;.  if( p==0 ) r
1420f 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 20  eturn 0;.  pNew 
14210 3d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 52 61  = sqliteMallocRa
14211 77 28 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29  w( sizeof(*pNew)
14212 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d   );.  if( pNew==
14213 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
14214 70 4e 65 77 2d 3e 6e 49 64 20 3d 20 70 4e 65 77  pNew->nId = pNew
14215 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 49  ->nAlloc = p->nI
14216 64 3b 0a 20 20 70 4e 65 77 2d 3e 61 20 3d 20 73  d;.  pNew->a = s
14217 71 6c 69 74 65 4d 61 6c 6c 6f 63 52 61 77 28 20  qliteMallocRaw( 
14218 70 2d 3e 6e 49 64 2a 73 69 7a 65 6f 66 28 70 2d  p->nId*sizeof(p-
14219 3e 61 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20  >a[0]) );.  if( 
1421a 70 4e 65 77 2d 3e 61 3d 3d 30 20 29 7b 0a 20 20  pNew->a==0 ){.  
1421b 20 20 73 71 6c 69 74 65 46 72 65 65 28 70 4e 65    sqliteFree(pNe
1421c 77 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30  w);.    return 0
1421d 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b  ;.  }.  for(i=0;
1421e 20 69 3c 70 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b   i<p->nId; i++){
1421f 0a 20 20 20 20 73 74 72 75 63 74 20 49 64 4c 69  .    struct IdLi
14220 73 74 5f 69 74 65 6d 20 2a 70 4e 65 77 49 74 65  st_item *pNewIte
14221 6d 20 3d 20 26 70 4e 65 77 2d 3e 61 5b 69 5d 3b  m = &pNew->a[i];
14222 0a 20 20 20 20 73 74 72 75 63 74 20 49 64 4c 69  .    struct IdLi
14223 73 74 5f 69 74 65 6d 20 2a 70 4f 6c 64 49 74 65  st_item *pOldIte
14224 6d 20 3d 20 26 70 2d 3e 61 5b 69 5d 3b 0a 20 20  m = &p->a[i];.  
14225 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 4e 61 6d    pNewItem->zNam
14226 65 20 3d 20 73 71 6c 69 74 65 53 74 72 44 75 70  e = sqliteStrDup
14227 28 70 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65  (pOldItem->zName
14228 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d  );.    pNewItem-
14229 3e 69 64 78 20 3d 20 70 4f 6c 64 49 74 65 6d 2d  >idx = pOldItem-
1422a 3e 69 64 78 3b 0a 20 20 7d 0a 20 20 72 65 74 75  >idx;.  }.  retu
1422b 72 6e 20 70 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54  rn pNew;.}.SQLIT
1422c 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65 63 74  E_PRIVATE Select
1422d 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44   *sqlite3SelectD
1422e 75 70 28 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20  up(Select *p){. 
1422f 20 53 65 6c 65 63 74 20 2a 70 4e 65 77 3b 0a 20   Select *pNew;. 
14230 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75   if( p==0 ) retu
14231 72 6e 20 30 3b 0a 20 20 70 4e 65 77 20 3d 20 73  rn 0;.  pNew = s
14232 71 6c 69 74 65 4d 61 6c 6c 6f 63 52 61 77 28 20  qliteMallocRaw( 
14233 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20  sizeof(*p) );.  
14234 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65  if( pNew==0 ) re
14235 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e  turn 0;.  pNew->
14236 69 73 44 69 73 74 69 6e 63 74 20 3d 20 70 2d 3e  isDistinct = p->
14237 69 73 44 69 73 74 69 6e 63 74 3b 0a 20 20 70 4e  isDistinct;.  pN
14238 65 77 2d 3e 70 45 4c 69 73 74 20 3d 20 73 71 6c  ew->pEList = sql
14239 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28  ite3ExprListDup(
1423a 70 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 70 4e  p->pEList);.  pN
1423b 65 77 2d 3e 70 53 72 63 20 3d 20 73 71 6c 69 74  ew->pSrc = sqlit
1423c 65 33 53 72 63 4c 69 73 74 44 75 70 28 70 2d 3e  e3SrcListDup(p->
1423d 70 53 72 63 29 3b 0a 20 20 70 4e 65 77 2d 3e 70  pSrc);.  pNew->p
1423e 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45  Where = sqlite3E
1423f 78 70 72 44 75 70 28 70 2d 3e 70 57 68 65 72 65  xprDup(p->pWhere
14240 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 47 72 6f 75  );.  pNew->pGrou
14241 70 42 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70  pBy = sqlite3Exp
14242 72 4c 69 73 74 44 75 70 28 70 2d 3e 70 47 72 6f  rListDup(p->pGro
14243 75 70 42 79 29 3b 0a 20 20 70 4e 65 77 2d 3e 70  upBy);.  pNew->p
14244 48 61 76 69 6e 67 20 3d 20 73 71 6c 69 74 65 33  Having = sqlite3
14245 45 78 70 72 44 75 70 28 70 2d 3e 70 48 61 76 69  ExprDup(p->pHavi
14246 6e 67 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f 72  ng);.  pNew->pOr
14247 64 65 72 42 79 20 3d 20 73 71 6c 69 74 65 33 45  derBy = sqlite3E
14248 78 70 72 4c 69 73 74 44 75 70 28 70 2d 3e 70 4f  xprListDup(p->pO
14249 72 64 65 72 42 79 29 3b 0a 20 20 70 4e 65 77 2d  rderBy);.  pNew-
1424a 3e 6f 70 20 3d 20 70 2d 3e 6f 70 3b 0a 20 20 70  >op = p->op;.  p
1424b 4e 65 77 2d 3e 70 50 72 69 6f 72 20 3d 20 73 71  New->pPrior = sq
1424c 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 70  lite3SelectDup(p
1424d 2d 3e 70 50 72 69 6f 72 29 3b 0a 20 20 70 4e 65  ->pPrior);.  pNe
1424e 77 2d 3e 70 4c 69 6d 69 74 20 3d 20 73 71 6c 69  w->pLimit = sqli
1424f 74 65 33 45 78 70 72 44 75 70 28 70 2d 3e 70 4c  te3ExprDup(p->pL
14250 69 6d 69 74 29 3b 0a 20 20 70 4e 65 77 2d 3e 70  imit);.  pNew->p
14251 4f 66 66 73 65 74 20 3d 20 73 71 6c 69 74 65 33  Offset = sqlite3
14252 45 78 70 72 44 75 70 28 70 2d 3e 70 4f 66 66 73  ExprDup(p->pOffs
14253 65 74 29 3b 0a 20 20 70 4e 65 77 2d 3e 69 4c 69  et);.  pNew->iLi
14254 6d 69 74 20 3d 20 2d 31 3b 0a 20 20 70 4e 65 77  mit = -1;.  pNew
14255 2d 3e 69 4f 66 66 73 65 74 20 3d 20 2d 31 3b 0a  ->iOffset = -1;.
14256 20 20 70 4e 65 77 2d 3e 69 73 52 65 73 6f 6c 76    pNew->isResolv
14257 65 64 20 3d 20 70 2d 3e 69 73 52 65 73 6f 6c 76  ed = p->isResolv
14258 65 64 3b 0a 20 20 70 4e 65 77 2d 3e 69 73 41 67  ed;.  pNew->isAg
14259 67 20 3d 20 70 2d 3e 69 73 41 67 67 3b 0a 20 20  g = p->isAgg;.  
1425a 70 4e 65 77 2d 3e 75 73 65 73 45 70 68 6d 20 3d  pNew->usesEphm =
1425b 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 64 69 73 61   0;.  pNew->disa
1425c 6c 6c 6f 77 4f 72 64 65 72 42 79 20 3d 20 30 3b  llowOrderBy = 0;
1425d 0a 20 20 70 4e 65 77 2d 3e 70 52 69 67 68 74 6d  .  pNew->pRightm
1425e 6f 73 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d  ost = 0;.  pNew-
1425f 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d  >addrOpenEphm[0]
14260 20 3d 20 2d 31 3b 0a 20 20 70 4e 65 77 2d 3e 61   = -1;.  pNew->a
14261 64 64 72 4f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d  ddrOpenEphm[1] =
14262 20 2d 31 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64   -1;.  pNew->add
14263 72 4f 70 65 6e 45 70 68 6d 5b 32 5d 20 3d 20 2d  rOpenEphm[2] = -
14264 31 3b 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77  1;.  return pNew
14265 3b 0a 7d 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45  ;.}.#else.SQLITE
14266 5f 50 52 49 56 41 54 45 20 53 65 6c 65 63 74 20  _PRIVATE Select 
14267 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75  *sqlite3SelectDu
14268 70 28 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20  p(Select *p){.  
14269 61 73 73 65 72 74 28 20 70 3d 3d 30 20 29 3b 0a  assert( p==0 );.
1426a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65    return 0;.}.#e
1426b 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64  ndif.../*.** Add
1426c 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20 74   a new element t
1426d 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 61 6e 20  o the end of an 
1426e 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e  expression list.
1426f 20 20 49 66 20 70 4c 69 73 74 20 69 73 0a 2a 2a    If pList is.**
14270 20 69 6e 69 74 69 61 6c 6c 79 20 4e 55 4c 4c 2c   initially NULL,
14271 20 74 68 65 6e 20 63 72 65 61 74 65 20 61 20 6e   then create a n
14272 65 77 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69  ew expression li
14273 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  st..*/.SQLITE_PR
14274 49 56 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a  IVATE ExprList *
14275 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
14276 70 70 65 6e 64 28 45 78 70 72 4c 69 73 74 20 2a  ppend(ExprList *
14277 70 4c 69 73 74 2c 20 45 78 70 72 20 2a 70 45 78  pList, Expr *pEx
14278 70 72 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  pr, Token *pName
14279 29 7b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d  ){.  if( pList==
1427a 30 20 29 7b 0a 20 20 20 20 70 4c 69 73 74 20 3d  0 ){.    pList =
1427b 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 20 73   sqliteMalloc( s
1427c 69 7a 65 6f 66 28 45 78 70 72 4c 69 73 74 29 20  izeof(ExprList) 
1427d 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74  );.    if( pList
1427e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  ==0 ){.      got
1427f 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a  o no_mem;.    }.
14280 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 69 73      assert( pLis
14281 74 2d 3e 6e 41 6c 6c 6f 63 3d 3d 30 20 29 3b 0a  t->nAlloc==0 );.
14282 20 20 7d 0a 20 20 69 66 28 20 70 4c 69 73 74 2d    }.  if( pList-
14283 3e 6e 41 6c 6c 6f 63 3c 3d 70 4c 69 73 74 2d 3e  >nAlloc<=pList->
14284 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 73 74 72  nExpr ){.    str
14285 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
14286 6d 20 2a 61 3b 0a 20 20 20 20 69 6e 74 20 6e 20  m *a;.    int n 
14287 3d 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 2a  = pList->nAlloc*
14288 32 20 2b 20 34 3b 0a 20 20 20 20 61 20 3d 20 73  2 + 4;.    a = s
14289 71 6c 69 74 65 52 65 61 6c 6c 6f 63 28 70 4c 69  qliteRealloc(pLi
1428a 73 74 2d 3e 61 2c 20 6e 2a 73 69 7a 65 6f 66 28  st->a, n*sizeof(
1428b 70 4c 69 73 74 2d 3e 61 5b 30 5d 29 29 3b 0a 20  pList->a[0]));. 
1428c 20 20 20 69 66 28 20 61 3d 3d 30 20 29 7b 0a 20     if( a==0 ){. 
1428d 20 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d       goto no_mem
1428e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 69 73  ;.    }.    pLis
1428f 74 2d 3e 61 20 3d 20 61 3b 0a 20 20 20 20 70 4c  t->a = a;.    pL
14290 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 6e 3b  ist->nAlloc = n;
14291 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
14292 4c 69 73 74 2d 3e 61 21 3d 30 20 29 3b 0a 20 20  List->a!=0 );.  
14293 69 66 28 20 70 45 78 70 72 20 7c 7c 20 70 4e 61  if( pExpr || pNa
14294 6d 65 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74  me ){.    struct
14295 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
14296 70 49 74 65 6d 20 3d 20 26 70 4c 69 73 74 2d 3e  pItem = &pList->
14297 61 5b 70 4c 69 73 74 2d 3e 6e 45 78 70 72 2b 2b  a[pList->nExpr++
14298 5d 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 49  ];.    memset(pI
14299 74 65 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a  tem, 0, sizeof(*
1429a 70 49 74 65 6d 29 29 3b 0a 20 20 20 20 70 49 74  pItem));.    pIt
1429b 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69  em->zName = sqli
1429c 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e  te3NameFromToken
1429d 28 70 4e 61 6d 65 29 3b 0a 20 20 20 20 70 49 74  (pName);.    pIt
1429e 65 6d 2d 3e 70 45 78 70 72 20 3d 20 70 45 78 70  em->pExpr = pExp
1429f 72 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  r;.  }.  return 
142a0 70 4c 69 73 74 3b 0a 0a 6e 6f 5f 6d 65 6d 3a 20  pList;..no_mem: 
142a1 20 20 20 20 0a 20 20 2f 2a 20 41 76 6f 69 64 20      .  /* Avoid 
142a2 6c 65 61 6b 69 6e 67 20 6d 65 6d 6f 72 79 20 69  leaking memory i
142a3 66 20 6d 61 6c 6c 6f 63 20 68 61 73 20 66 61 69  f malloc has fai
142a4 6c 65 64 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65  led. */.  sqlite
142a5 33 45 78 70 72 44 65 6c 65 74 65 28 70 45 78 70  3ExprDelete(pExp
142a6 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  r);.  sqlite3Exp
142a7 72 4c 69 73 74 44 65 6c 65 74 65 28 70 4c 69 73  rListDelete(pLis
142a8 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a  t);.  return 0;.
142a9 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20  }../*.** If the 
142aa 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20  expression list 
142ab 70 45 4c 69 73 74 20 63 6f 6e 74 61 69 6e 73 20  pEList contains 
142ac 6d 6f 72 65 20 74 68 61 6e 20 69 4c 69 6d 69 74  more than iLimit
142ad 20 65 6c 65 6d 65 6e 74 73 2c 0a 2a 2a 20 6c 65   elements,.** le
142ae 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ave an error mes
142af 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 2e 0a  sage in pParse..
142b0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
142b1 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
142b2 70 72 4c 69 73 74 43 68 65 63 6b 4c 65 6e 67 74  prListCheckLengt
142b3 68 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  h(.  Parse *pPar
142b4 73 65 2c 0a 20 20 45 78 70 72 4c 69 73 74 20 2a  se,.  ExprList *
142b5 70 45 4c 69 73 74 2c 0a 20 20 69 6e 74 20 69 4c  pEList,.  int iL
142b6 69 6d 69 74 2c 0a 20 20 63 6f 6e 73 74 20 63 68  imit,.  const ch
142b7 61 72 20 2a 7a 4f 62 6a 65 63 74 0a 29 7b 0a 20  ar *zObject.){. 
142b8 20 69 66 28 20 70 45 4c 69 73 74 20 26 26 20 70   if( pEList && p
142b9 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 69 4c 69  EList->nExpr>iLi
142ba 6d 69 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  mit ){.    sqlit
142bb 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
142bc 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 63 6f 6c  e, "too many col
142bd 75 6d 6e 73 20 69 6e 20 25 73 22 2c 20 7a 4f 62  umns in %s", zOb
142be 6a 65 63 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 23  ject);.  }.}...#
142bf 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58  if SQLITE_MAX_EX
142c0 50 52 5f 44 45 50 54 48 3e 30 0a 2f 2a 20 54 68  PR_DEPTH>0./* Th
142c1 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 72 65  e following thre
142c2 65 20 66 75 6e 63 74 69 6f 6e 73 2c 20 68 65 69  e functions, hei
142c3 67 68 74 4f 66 45 78 70 72 28 29 2c 20 68 65 69  ghtOfExpr(), hei
142c4 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 29 0a  ghtOfExprList().
142c5 2a 2a 20 61 6e 64 20 68 65 69 67 68 74 4f 66 53  ** and heightOfS
142c6 65 6c 65 63 74 28 29 2c 20 61 72 65 20 75 73 65  elect(), are use
142c7 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74  d to determine t
142c8 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 69 67 68  he maximum heigh
142c9 74 0a 2a 2a 20 6f 66 20 61 6e 79 20 65 78 70 72  t.** of any expr
142ca 65 73 73 69 6f 6e 20 74 72 65 65 20 72 65 66 65  ession tree refe
142cb 72 65 6e 63 65 64 20 62 79 20 74 68 65 20 73 74  renced by the st
142cc 72 75 63 74 75 72 65 20 70 61 73 73 65 64 20 61  ructure passed a
142cd 73 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 61  s the.** first a
142ce 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49  rgument..**.** I
142cf 66 20 74 68 69 73 20 6d 61 78 69 6d 75 6d 20 68  f this maximum h
142d0 65 69 67 68 74 20 69 73 20 67 72 65 61 74 65 72  eight is greater
142d1 20 74 68 61 6e 20 74 68 65 20 63 75 72 72 65 6e   than the curren
142d2 74 20 76 61 6c 75 65 20 70 6f 69 6e 74 65 64 0a  t value pointed.
142d3 2a 2a 20 74 6f 20 62 79 20 70 6e 48 65 69 67 68  ** to by pnHeigh
142d4 74 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61  t, the second pa
142d5 72 61 6d 65 74 65 72 2c 20 74 68 65 6e 20 73 65  rameter, then se
142d6 74 20 2a 70 6e 48 65 69 67 68 74 20 74 6f 20 74  t *pnHeight to t
142d7 68 61 74 0a 2a 2a 20 76 61 6c 75 65 2e 0a 2a 2f  hat.** value..*/
142d8 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 65 69  .static void hei
142d9 67 68 74 4f 66 45 78 70 72 28 45 78 70 72 20 2a  ghtOfExpr(Expr *
142da 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69 67 68 74  p, int *pnHeight
142db 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  ){.  if( p ){.  
142dc 20 20 69 66 28 20 70 2d 3e 6e 48 65 69 67 68 74    if( p->nHeight
142dd 3e 2a 70 6e 48 65 69 67 68 74 20 29 7b 0a 20 20  >*pnHeight ){.  
142de 20 20 20 20 2a 70 6e 48 65 69 67 68 74 20 3d 20      *pnHeight = 
142df 70 2d 3e 6e 48 65 69 67 68 74 3b 0a 20 20 20 20  p->nHeight;.    
142e0 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76  }.  }.}.static v
142e1 6f 69 64 20 68 65 69 67 68 74 4f 66 45 78 70 72  oid heightOfExpr
142e2 4c 69 73 74 28 45 78 70 72 4c 69 73 74 20 2a 70  List(ExprList *p
142e3 2c 20 69 6e 74 20 2a 70 6e 48 65 69 67 68 74 29  , int *pnHeight)
142e4 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  {.  if( p ){.   
142e5 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28   int i;.    for(
142e6 69 3d 30 3b 20 69 3c 70 2d 3e 6e 45 78 70 72 3b  i=0; i<p->nExpr;
142e7 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 68 65 69   i++){.      hei
142e8 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 61 5b 69  ghtOfExpr(p->a[i
142e9 5d 2e 70 45 78 70 72 2c 20 70 6e 48 65 69 67 68  ].pExpr, pnHeigh
142ea 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  t);.    }.  }.}.
142eb 73 74 61 74 69 63 20 76 6f 69 64 20 68 65 69 67  static void heig
142ec 68 74 4f 66 53 65 6c 65 63 74 28 53 65 6c 65 63  htOfSelect(Selec
142ed 74 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69  t *p, int *pnHei
142ee 67 68 74 29 7b 0a 20 20 69 66 28 20 70 20 29 7b  ght){.  if( p ){
142ef 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 70  .    heightOfExp
142f0 72 28 70 2d 3e 70 57 68 65 72 65 2c 20 70 6e 48  r(p->pWhere, pnH
142f1 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67  eight);.    heig
142f2 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 48 61 76  htOfExpr(p->pHav
142f3 69 6e 67 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a  ing, pnHeight);.
142f4 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 72      heightOfExpr
142f5 28 70 2d 3e 70 4c 69 6d 69 74 2c 20 70 6e 48 65  (p->pLimit, pnHe
142f6 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68  ight);.    heigh
142f7 74 4f 66 45 78 70 72 28 70 2d 3e 70 4f 66 66 73  tOfExpr(p->pOffs
142f8 65 74 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20  et, pnHeight);. 
142f9 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c     heightOfExprL
142fa 69 73 74 28 70 2d 3e 70 45 4c 69 73 74 2c 20 70  ist(p->pEList, p
142fb 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65  nHeight);.    he
142fc 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70  ightOfExprList(p
142fd 2d 3e 70 47 72 6f 75 70 42 79 2c 20 70 6e 48 65  ->pGroupBy, pnHe
142fe 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68  ight);.    heigh
142ff 74 4f 66 45 78 70 72 4c 69 73 74 28 70 2d 3e 70  tOfExprList(p->p
14300 4f 72 64 65 72 42 79 2c 20 70 6e 48 65 69 67 68  OrderBy, pnHeigh
14301 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66  t);.    heightOf
14302 53 65 6c 65 63 74 28 70 2d 3e 70 50 72 69 6f 72  Select(p->pPrior
14303 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 7d  , pnHeight);.  }
14304 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68  .}../*.** Set th
14305 65 20 45 78 70 72 2e 6e 48 65 69 67 68 74 20 76  e Expr.nHeight v
14306 61 72 69 61 62 6c 65 20 69 6e 20 74 68 65 20 73  ariable in the s
14307 74 72 75 63 74 75 72 65 20 70 61 73 73 65 64 20  tructure passed 
14308 61 73 20 61 6e 20 0a 2a 2a 20 61 72 67 75 6d 65  as an .** argume
14309 6e 74 2e 20 41 6e 20 65 78 70 72 65 73 73 69 6f  nt. An expressio
1430a 6e 20 77 69 74 68 20 6e 6f 20 63 68 69 6c 64 72  n with no childr
1430b 65 6e 2c 20 45 78 70 72 2e 70 4c 69 73 74 20 6f  en, Expr.pList o
1430c 72 20 0a 2a 2a 20 45 78 70 72 2e 70 53 65 6c 65  r .** Expr.pSele
1430d 63 74 20 6d 65 6d 62 65 72 20 68 61 73 20 61 20  ct member has a 
1430e 68 65 69 67 68 74 20 6f 66 20 31 2e 20 41 6e 79  height of 1. Any
1430f 20 6f 74 68 65 72 20 65 78 70 72 65 73 73 69 6f   other expressio
14310 6e 0a 2a 2a 20 68 61 73 20 61 20 68 65 69 67 68  n.** has a heigh
14311 74 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6d  t equal to the m
14312 61 78 69 6d 75 6d 20 68 65 69 67 68 74 20 6f 66  aximum height of
14313 20 61 6e 79 20 6f 74 68 65 72 20 0a 2a 2a 20 72   any other .** r
14314 65 66 65 72 65 6e 63 65 64 20 45 78 70 72 20 70  eferenced Expr p
14315 6c 75 73 20 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c 49  lus one..*/.SQLI
14316 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
14317 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65  sqlite3ExprSetHe
14318 69 67 68 74 28 45 78 70 72 20 2a 70 29 7b 0a 20  ight(Expr *p){. 
14319 20 69 6e 74 20 6e 48 65 69 67 68 74 20 3d 20 30   int nHeight = 0
1431a 3b 0a 20 20 68 65 69 67 68 74 4f 66 45 78 70 72  ;.  heightOfExpr
1431b 28 70 2d 3e 70 4c 65 66 74 2c 20 26 6e 48 65 69  (p->pLeft, &nHei
1431c 67 68 74 29 3b 0a 20 20 68 65 69 67 68 74 4f 66  ght);.  heightOf
1431d 45 78 70 72 28 70 2d 3e 70 52 69 67 68 74 2c 20  Expr(p->pRight, 
1431e 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 68 65 69  &nHeight);.  hei
1431f 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70 2d  ghtOfExprList(p-
14320 3e 70 4c 69 73 74 2c 20 26 6e 48 65 69 67 68 74  >pList, &nHeight
14321 29 3b 0a 20 20 68 65 69 67 68 74 4f 66 53 65 6c  );.  heightOfSel
14322 65 63 74 28 70 2d 3e 70 53 65 6c 65 63 74 2c 20  ect(p->pSelect, 
14323 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 70 2d 3e  &nHeight);.  p->
14324 6e 48 65 69 67 68 74 20 3d 20 6e 48 65 69 67 68  nHeight = nHeigh
14325 74 20 2b 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  t + 1;.}../*.** 
14326 52 65 74 75 72 6e 20 74 68 65 20 6d 61 78 69 6d  Return the maxim
14327 75 6d 20 68 65 69 67 68 74 20 6f 66 20 61 6e 79  um height of any
14328 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65   expression tree
14329 20 72 65 66 65 72 65 6e 63 65 64 0a 2a 2a 20 62   referenced.** b
1432a 79 20 74 68 65 20 73 65 6c 65 63 74 20 73 74 61  y the select sta
1432b 74 65 6d 65 6e 74 20 70 61 73 73 65 64 20 61 73  tement passed as
1432c 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f   an argument..*/
1432d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1432e 69 6e 74 20 73 71 6c 69 74 65 33 53 65 6c 65 63  int sqlite3Selec
1432f 74 45 78 70 72 48 65 69 67 68 74 28 53 65 6c 65  tExprHeight(Sele
14330 63 74 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 48  ct *p){.  int nH
14331 65 69 67 68 74 20 3d 20 30 3b 0a 20 20 68 65 69  eight = 0;.  hei
14332 67 68 74 4f 66 53 65 6c 65 63 74 28 70 2c 20 26  ghtOfSelect(p, &
14333 6e 48 65 69 67 68 74 29 3b 0a 20 20 72 65 74 75  nHeight);.  retu
14334 72 6e 20 6e 48 65 69 67 68 74 3b 0a 7d 0a 23 65  rn nHeight;.}.#e
14335 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65  ndif../*.** Dele
14336 74 65 20 61 6e 20 65 6e 74 69 72 65 20 65 78 70  te an entire exp
14337 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2f  ression list..*/
14338 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
14339 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
1433a 4c 69 73 74 44 65 6c 65 74 65 28 45 78 70 72 4c  ListDelete(ExprL
1433b 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69  ist *pList){.  i
1433c 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 45  nt i;.  struct E
1433d 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  xprList_item *pI
1433e 74 65 6d 3b 0a 20 20 69 66 28 20 70 4c 69 73 74  tem;.  if( pList
1433f 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
14340 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e 61  assert( pList->a
14341 21 3d 30 20 7c 7c 20 28 70 4c 69 73 74 2d 3e 6e  !=0 || (pList->n
14342 45 78 70 72 3d 3d 30 20 26 26 20 70 4c 69 73 74  Expr==0 && pList
14343 2d 3e 6e 41 6c 6c 6f 63 3d 3d 30 29 20 29 3b 0a  ->nAlloc==0) );.
14344 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d    assert( pList-
14345 3e 6e 45 78 70 72 3c 3d 70 4c 69 73 74 2d 3e 6e  >nExpr<=pList->n
14346 41 6c 6c 6f 63 20 29 3b 0a 20 20 66 6f 72 28 70  Alloc );.  for(p
14347 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20 69  Item=pList->a, i
14348 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78  =0; i<pList->nEx
14349 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b  pr; i++, pItem++
1434a 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  ){.    sqlite3Ex
1434b 70 72 44 65 6c 65 74 65 28 70 49 74 65 6d 2d 3e  prDelete(pItem->
1434c 70 45 78 70 72 29 3b 0a 20 20 20 20 73 71 6c 69  pExpr);.    sqli
1434d 74 65 46 72 65 65 28 70 49 74 65 6d 2d 3e 7a 4e  teFree(pItem->zN
1434e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  ame);.  }.  sqli
1434f 74 65 46 72 65 65 28 70 4c 69 73 74 2d 3e 61 29  teFree(pList->a)
14350 3b 0a 20 20 73 71 6c 69 74 65 46 72 65 65 28 70  ;.  sqliteFree(p
14351 4c 69 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  List);.}../*.** 
14352 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 73 69  Walk an expressi
14353 6f 6e 20 74 72 65 65 2e 20 20 43 61 6c 6c 20 78  on tree.  Call x
14354 46 75 6e 63 20 66 6f 72 20 65 61 63 68 20 6e 6f  Func for each no
14355 64 65 20 76 69 73 69 74 65 64 2e 0a 2a 2a 0a 2a  de visited..**.*
14356 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c  * The return val
14357 75 65 20 66 72 6f 6d 20 78 46 75 6e 63 20 64 65  ue from xFunc de
14358 74 65 72 6d 69 6e 65 73 20 77 68 65 74 68 65 72  termines whether
14359 20 74 68 65 20 74 72 65 65 20 77 61 6c 6b 20 63   the tree walk c
1435a 6f 6e 74 69 6e 75 65 73 2e 0a 2a 2a 20 30 20 6d  ontinues..** 0 m
1435b 65 61 6e 73 20 63 6f 6e 74 69 6e 75 65 20 77 61  eans continue wa
1435c 6c 6b 69 6e 67 20 74 68 65 20 74 72 65 65 2e 20  lking the tree. 
1435d 20 31 20 6d 65 61 6e 73 20 64 6f 20 6e 6f 74 20   1 means do not 
1435e 77 61 6c 6b 20 63 68 69 6c 64 72 65 6e 0a 2a 2a  walk children.**
1435f 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20   of the current 
14360 6e 6f 64 65 20 62 75 74 20 63 6f 6e 74 69 6e 75  node but continu
14361 65 20 77 69 74 68 20 73 69 62 6c 69 6e 67 73 2e  e with siblings.
14362 20 20 32 20 6d 65 61 6e 73 20 61 62 61 6e 64 6f    2 means abando
14363 6e 0a 2a 2a 20 74 68 65 20 74 72 65 65 20 77 61  n.** the tree wa
14364 6c 6b 20 63 6f 6d 70 6c 65 74 65 6c 79 2e 0a 2a  lk completely..*
14365 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20  *.** The return 
14366 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 69 73 20  value from this 
14367 72 6f 75 74 69 6e 65 20 69 73 20 31 20 74 6f 20  routine is 1 to 
14368 61 62 61 6e 64 6f 6e 20 74 68 65 20 74 72 65 65  abandon the tree
14369 20 77 61 6c 6b 0a 2a 2a 20 61 6e 64 20 30 20 74   walk.** and 0 t
1436a 6f 20 63 6f 6e 74 69 6e 75 65 2e 0a 2a 2a 0a 2a  o continue..**.*
1436b 2a 20 4e 4f 54 49 43 45 3a 20 20 54 68 69 73 20  * NOTICE:  This 
1436c 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 2a 6e 6f  routine does *no
1436d 74 2a 20 64 65 73 63 65 6e 64 20 69 6e 74 6f 20  t* descend into 
1436e 73 75 62 71 75 65 72 69 65 73 2e 0a 2a 2f 0a 73  subqueries..*/.s
1436f 74 61 74 69 63 20 69 6e 74 20 77 61 6c 6b 45 78  tatic int walkEx
14370 70 72 4c 69 73 74 28 45 78 70 72 4c 69 73 74 20  prList(ExprList 
14371 2a 2c 20 69 6e 74 20 28 2a 29 28 76 6f 69 64 20  *, int (*)(void 
14372 2a 2c 20 45 78 70 72 2a 29 2c 20 76 6f 69 64 20  *, Expr*), void 
14373 2a 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 77  *);.static int w
14374 61 6c 6b 45 78 70 72 54 72 65 65 28 45 78 70 72  alkExprTree(Expr
14375 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 28 2a 78   *pExpr, int (*x
14376 46 75 6e 63 29 28 76 6f 69 64 2a 2c 45 78 70 72  Func)(void*,Expr
14377 2a 29 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b  *), void *pArg){
14378 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28  .  int rc;.  if(
14379 20 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75   pExpr==0 ) retu
1437a 72 6e 20 30 3b 0a 20 20 72 63 20 3d 20 28 2a 78  rn 0;.  rc = (*x
1437b 46 75 6e 63 29 28 70 41 72 67 2c 20 70 45 78 70  Func)(pArg, pExp
1437c 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 30 20  r);.  if( rc==0 
1437d 29 7b 0a 20 20 20 20 69 66 28 20 77 61 6c 6b 45  ){.    if( walkE
1437e 78 70 72 54 72 65 65 28 70 45 78 70 72 2d 3e 70  xprTree(pExpr->p
1437f 4c 65 66 74 2c 20 78 46 75 6e 63 2c 20 70 41 72  Left, xFunc, pAr
14380 67 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20  g) ) return 1;. 
14381 20 20 20 69 66 28 20 77 61 6c 6b 45 78 70 72 54     if( walkExprT
14382 72 65 65 28 70 45 78 70 72 2d 3e 70 52 69 67 68  ree(pExpr->pRigh
14383 74 2c 20 78 46 75 6e 63 2c 20 70 41 72 67 29 20  t, xFunc, pArg) 
14384 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20  ) return 1;.    
14385 69 66 28 20 77 61 6c 6b 45 78 70 72 4c 69 73 74  if( walkExprList
14386 28 70 45 78 70 72 2d 3e 70 4c 69 73 74 2c 20 78  (pExpr->pList, x
14387 46 75 6e 63 2c 20 70 41 72 67 29 20 29 20 72 65  Func, pArg) ) re
14388 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65  turn 1;.  }.  re
14389 74 75 72 6e 20 72 63 3e 31 3b 0a 7d 0a 0a 2f 2a  turn rc>1;.}../*
1438a 0a 2a 2a 20 43 61 6c 6c 20 77 61 6c 6b 45 78 70  .** Call walkExp
1438b 72 54 72 65 65 28 29 20 66 6f 72 20 65 76 65 72  rTree() for ever
1438c 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20  y expression in 
1438d 6c 69 73 74 20 70 2e 0a 2a 2f 0a 73 74 61 74 69  list p..*/.stati
1438e 63 20 69 6e 74 20 77 61 6c 6b 45 78 70 72 4c 69  c int walkExprLi
1438f 73 74 28 45 78 70 72 4c 69 73 74 20 2a 70 2c 20  st(ExprList *p, 
14390 69 6e 74 20 28 2a 78 46 75 6e 63 29 28 76 6f 69  int (*xFunc)(voi
14391 64 20 2a 2c 20 45 78 70 72 2a 29 2c 20 76 6f 69  d *, Expr*), voi
14392 64 20 2a 70 41 72 67 29 7b 0a 20 20 69 6e 74 20  d *pArg){.  int 
14393 69 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72  i;.  struct Expr
14394 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
14395 3b 0a 20 20 69 66 28 20 21 70 20 29 20 72 65 74  ;.  if( !p ) ret
14396 75 72 6e 20 30 3b 0a 20 20 66 6f 72 28 69 3d 70  urn 0;.  for(i=p
14397 2d 3e 6e 45 78 70 72 2c 20 70 49 74 65 6d 3d 70  ->nExpr, pItem=p
14398 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70  ->a; i>0; i--, p
14399 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28  Item++){.    if(
1439a 20 77 61 6c 6b 45 78 70 72 54 72 65 65 28 70 49   walkExprTree(pI
1439b 74 65 6d 2d 3e 70 45 78 70 72 2c 20 78 46 75 6e  tem->pExpr, xFun
1439c 63 2c 20 70 41 72 67 29 20 29 20 72 65 74 75 72  c, pArg) ) retur
1439d 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  n 1;.  }.  retur
1439e 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61  n 0;.}../*.** Ca
1439f 6c 6c 20 77 61 6c 6b 45 78 70 72 54 72 65 65 28  ll walkExprTree(
143a0 29 20 66 6f 72 20 65 76 65 72 79 20 65 78 70 72  ) for every expr
143a1 65 73 73 69 6f 6e 20 69 6e 20 53 65 6c 65 63 74  ession in Select
143a2 20 70 2c 20 6e 6f 74 20 69 6e 63 6c 75 64 69 6e   p, not includin
143a3 67 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 73  g.** expressions
143a4 20 74 68 61 74 20 61 72 65 20 70 61 72 74 20 6f   that are part o
143a5 66 20 73 75 62 2d 73 65 6c 65 63 74 73 20 69 6e  f sub-selects in
143a6 20 61 6e 79 20 46 52 4f 4d 20 63 6c 61 75 73 65   any FROM clause
143a7 20 6f 72 20 74 68 65 20 4c 49 4d 49 54 0a 2a 2a   or the LIMIT.**
143a8 20 6f 72 20 4f 46 46 53 45 54 20 65 78 70 72 65   or OFFSET expre
143a9 73 73 69 6f 6e 73 2e 2e 0a 2a 2f 0a 73 74 61 74  ssions...*/.stat
143aa 69 63 20 69 6e 74 20 77 61 6c 6b 53 65 6c 65 63  ic int walkSelec
143ab 74 45 78 70 72 28 53 65 6c 65 63 74 20 2a 70 2c  tExpr(Select *p,
143ac 20 69 6e 74 20 28 2a 78 46 75 6e 63 29 28 76 6f   int (*xFunc)(vo
143ad 69 64 20 2a 2c 20 45 78 70 72 2a 29 2c 20 76 6f  id *, Expr*), vo
143ae 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 77 61 6c  id *pArg){.  wal
143af 6b 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 45 4c  kExprList(p->pEL
143b0 69 73 74 2c 20 78 46 75 6e 63 2c 20 70 41 72 67  ist, xFunc, pArg
143b1 29 3b 0a 20 20 77 61 6c 6b 45 78 70 72 54 72 65  );.  walkExprTre
143b2 65 28 70 2d 3e 70 57 68 65 72 65 2c 20 78 46 75  e(p->pWhere, xFu
143b3 6e 63 2c 20 70 41 72 67 29 3b 0a 20 20 77 61 6c  nc, pArg);.  wal
143b4 6b 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 47 72  kExprList(p->pGr
143b5 6f 75 70 42 79 2c 20 78 46 75 6e 63 2c 20 70 41  oupBy, xFunc, pA
143b6 72 67 29 3b 0a 20 20 77 61 6c 6b 45 78 70 72 54  rg);.  walkExprT
143b7 72 65 65 28 70 2d 3e 70 48 61 76 69 6e 67 2c 20  ree(p->pHaving, 
143b8 78 46 75 6e 63 2c 20 70 41 72 67 29 3b 0a 20 20  xFunc, pArg);.  
143b9 77 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 2d 3e  walkExprList(p->
143ba 70 4f 72 64 65 72 42 79 2c 20 78 46 75 6e 63 2c  pOrderBy, xFunc,
143bb 20 70 41 72 67 29 3b 0a 20 20 69 66 28 20 70 2d   pArg);.  if( p-
143bc 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20 20 20 77  >pPrior ){.    w
143bd 61 6c 6b 53 65 6c 65 63 74 45 78 70 72 28 70 2d  alkSelectExpr(p-
143be 3e 70 50 72 69 6f 72 2c 20 78 46 75 6e 63 2c 20  >pPrior, xFunc, 
143bf 70 41 72 67 29 3b 0a 20 20 7d 0a 20 20 72 65 74  pArg);.  }.  ret
143c0 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  urn 0;.}.../*.**
143c1 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
143c2 20 64 65 73 69 67 6e 65 64 20 61 73 20 61 6e 20   designed as an 
143c3 78 46 75 6e 63 20 66 6f 72 20 77 61 6c 6b 45 78  xFunc for walkEx
143c4 70 72 54 72 65 65 28 29 2e 0a 2a 2a 0a 2a 2a 20  prTree()..**.** 
143c5 70 41 72 67 20 69 73 20 72 65 61 6c 6c 79 20 61  pArg is really a
143c6 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69   pointer to an i
143c7 6e 74 65 67 65 72 2e 20 20 49 66 20 77 65 20 63  nteger.  If we c
143c8 61 6e 20 74 65 6c 6c 20 62 79 20 6c 6f 6f 6b 69  an tell by looki
143c9 6e 67 0a 2a 2a 20 61 74 20 70 45 78 70 72 20 74  ng.** at pExpr t
143ca 68 61 74 20 74 68 65 20 65 78 70 72 65 73 73 69  hat the expressi
143cb 6f 6e 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  on that contains
143cc 20 70 45 78 70 72 20 69 73 20 6e 6f 74 20 61 20   pExpr is not a 
143cd 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 65 78 70 72  constant.** expr
143ce 65 73 73 69 6f 6e 2c 20 74 68 65 6e 20 73 65 74  ession, then set
143cf 20 2a 70 41 72 67 20 74 6f 20 30 20 61 6e 64 20   *pArg to 0 and 
143d0 72 65 74 75 72 6e 20 32 20 74 6f 20 61 62 61 6e  return 2 to aban
143d1 64 6f 6e 20 74 68 65 20 74 72 65 65 20 77 61 6c  don the tree wal
143d2 6b 2e 0a 2a 2a 20 49 66 20 70 45 78 70 72 20 64  k..** If pExpr d
143d3 6f 65 73 20 64 6f 65 73 20 6e 6f 74 20 64 69 73  oes does not dis
143d4 71 75 61 6c 69 66 79 20 74 68 65 20 65 78 70 72  qualify the expr
143d5 65 73 73 69 6f 6e 20 66 72 6f 6d 20 62 65 69 6e  ession from bein
143d6 67 20 61 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20  g a constant.** 
143d7 74 68 65 6e 20 64 6f 20 6e 6f 74 68 69 6e 67 2e  then do nothing.
143d8 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 77 61 6c  .**.** After wal
143d9 6b 69 6e 67 20 74 68 65 20 77 68 6f 6c 65 20 74  king the whole t
143da 72 65 65 2c 20 69 66 20 6e 6f 20 6e 6f 64 65 73  ree, if no nodes
143db 20 61 72 65 20 66 6f 75 6e 64 20 74 68 61 74 20   are found that 
143dc 64 69 73 71 75 61 6c 69 66 79 0a 2a 2a 20 74 68  disqualify.** th
143dd 65 20 65 78 70 72 65 73 73 69 6f 6e 20 61 73 20  e expression as 
143de 63 6f 6e 73 74 61 6e 74 2c 20 74 68 65 6e 20 77  constant, then w
143df 65 20 61 73 73 75 6d 65 20 74 68 65 20 77 68 6f  e assume the who
143e0 6c 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a  le expression.**
143e1 20 69 73 20 63 6f 6e 73 74 61 6e 74 2e 20 20 53   is constant.  S
143e2 65 65 20 73 71 6c 69 74 65 33 45 78 70 72 49 73  ee sqlite3ExprIs
143e3 43 6f 6e 73 74 61 6e 74 28 29 20 66 6f 72 20 61  Constant() for a
143e4 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
143e5 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ation..*/.static
143e6 20 69 6e 74 20 65 78 70 72 4e 6f 64 65 49 73 43   int exprNodeIsC
143e7 6f 6e 73 74 61 6e 74 28 76 6f 69 64 20 2a 70 41  onstant(void *pA
143e8 72 67 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29  rg, Expr *pExpr)
143e9 7b 0a 20 20 69 6e 74 20 2a 70 4e 20 3d 20 28 69  {.  int *pN = (i
143ea 6e 74 2a 29 70 41 72 67 3b 0a 0a 20 20 2f 2a 20  nt*)pArg;..  /* 
143eb 49 66 20 2a 70 41 72 67 20 69 73 20 33 20 74 68  If *pArg is 3 th
143ec 65 6e 20 61 6e 79 20 74 65 72 6d 20 6f 66 20 74  en any term of t
143ed 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68  he expression th
143ee 61 74 20 63 6f 6d 65 73 20 66 72 6f 6d 0a 20 20  at comes from.  
143ef 2a 2a 20 74 68 65 20 4f 4e 20 6f 72 20 55 53 49  ** the ON or USI
143f0 4e 47 20 63 6c 61 75 73 65 73 20 6f 66 20 61 20  NG clauses of a 
143f1 6a 6f 69 6e 20 64 69 73 71 75 61 6c 69 66 69 65  join disqualifie
143f2 73 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  s the expression
143f3 0a 20 20 2a 2a 20 66 72 6f 6d 20 62 65 69 6e 67  .  ** from being
143f4 20 63 6f 6e 73 69 64 65 72 65 64 20 63 6f 6e 73   considered cons
143f5 74 61 6e 74 2e 20 2a 2f 0a 20 20 69 66 28 20 28  tant. */.  if( (
143f6 2a 70 4e 29 3d 3d 33 20 26 26 20 45 78 70 72 48  *pN)==3 && ExprH
143f7 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45  asAnyProperty(pE
143f8 78 70 72 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e  xpr, EP_FromJoin
143f9 29 20 29 7b 0a 20 20 20 20 2a 70 4e 20 3d 20 30  ) ){.    *pN = 0
143fa 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a  ;.    return 2;.
143fb 20 20 7d 0a 0a 20 20 73 77 69 74 63 68 28 20 70    }..  switch( p
143fc 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20  Expr->op ){.    
143fd 2f 2a 20 43 6f 6e 73 69 64 65 72 20 66 75 6e 63  /* Consider func
143fe 74 69 6f 6e 73 20 74 6f 20 62 65 20 63 6f 6e 73  tions to be cons
143ff 74 61 6e 74 20 69 66 20 61 6c 6c 20 74 68 65 69  tant if all thei
14400 72 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20  r arguments are 
14401 63 6f 6e 73 74 61 6e 74 0a 20 20 20 20 2a 2a 20  constant.    ** 
14402 61 6e 64 20 2a 70 41 72 67 3d 3d 32 20 2a 2f 0a  and *pArg==2 */.
14403 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e 43      case TK_FUNC
14404 54 49 4f 4e 3a 0a 20 20 20 20 20 20 69 66 28 20  TION:.      if( 
14405 28 2a 70 4e 29 3d 3d 32 20 29 20 72 65 74 75 72  (*pN)==2 ) retur
14406 6e 20 30 3b 0a 20 20 20 20 20 20 2f 2a 20 46 61  n 0;.      /* Fa
14407 6c 6c 20 74 68 72 6f 75 67 68 20 2a 2f 0a 20 20  ll through */.  
14408 20 20 63 61 73 65 20 54 4b 5f 49 44 3a 0a 20 20    case TK_ID:.  
14409 20 20 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e    case TK_COLUMN
1440a 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 44 4f  :.    case TK_DO
1440b 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41  T:.    case TK_A
1440c 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20  GG_FUNCTION:.   
1440d 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 43 4f 4c   case TK_AGG_COL
1440e 55 4d 4e 3a 0a 23 69 66 6e 64 65 66 20 53 51 4c  UMN:.#ifndef SQL
1440f 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
14410 59 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 45  Y.    case TK_SE
14411 4c 45 43 54 3a 0a 20 20 20 20 63 61 73 65 20 54  LECT:.    case T
14412 4b 5f 45 58 49 53 54 53 3a 0a 23 65 6e 64 69 66  K_EXISTS:.#endif
14413 0a 20 20 20 20 20 20 2a 70 4e 20 3d 20 30 3b 0a  .      *pN = 0;.
14414 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a        return 2;.
14415 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 3a 0a      case TK_IN:.
14416 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d        if( pExpr-
14417 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20  >pSelect ){.    
14418 20 20 20 20 2a 70 4e 20 3d 20 30 3b 0a 20 20 20      *pN = 0;.   
14419 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20       return 2;. 
1441a 20 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75       }.    defau
1441b 6c 74 3a 0a 20 20 20 20 20 20 72 65 74 75 72 6e  lt:.      return
1441c 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a   0;.  }.}../*.**
1441d 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 73   Walk an express
1441e 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75 72  ion tree.  Retur
1441f 6e 20 31 20 69 66 20 74 68 65 20 65 78 70 72 65  n 1 if the expre
14420 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74 61 6e  ssion is constan
14421 74 0a 2a 2a 20 61 6e 64 20 30 20 69 66 20 69 74  t.** and 0 if it
14422 20 69 6e 76 6f 6c 76 65 73 20 76 61 72 69 61 62   involves variab
14423 6c 65 73 20 6f 72 20 66 75 6e 63 74 69 6f 6e 20  les or function 
14424 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72  calls..**.** For
14425 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66   the purposes of
14426 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20   this function, 
14427 61 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20  a double-quoted 
14428 73 74 72 69 6e 67 20 28 65 78 3a 20 22 61 62 63  string (ex: "abc
14429 22 29 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65  ").** is conside
1442a 72 65 64 20 61 20 76 61 72 69 61 62 6c 65 20 62  red a variable b
1442b 75 74 20 61 20 73 69 6e 67 6c 65 2d 71 75 6f 74  ut a single-quot
1442c 65 64 20 73 74 72 69 6e 67 20 28 65 78 3a 20 27  ed string (ex: '
1442d 61 62 63 27 29 20 69 73 0a 2a 2a 20 61 20 63 6f  abc') is.** a co
1442e 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54  nstant..*/.SQLIT
1442f 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
14430 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74  lite3ExprIsConst
14431 61 6e 74 28 45 78 70 72 20 2a 70 29 7b 0a 20 20  ant(Expr *p){.  
14432 69 6e 74 20 69 73 43 6f 6e 73 74 20 3d 20 31 3b  int isConst = 1;
14433 0a 20 20 77 61 6c 6b 45 78 70 72 54 72 65 65 28  .  walkExprTree(
14434 70 2c 20 65 78 70 72 4e 6f 64 65 49 73 43 6f 6e  p, exprNodeIsCon
14435 73 74 61 6e 74 2c 20 26 69 73 43 6f 6e 73 74 29  stant, &isConst)
14436 3b 0a 20 20 72 65 74 75 72 6e 20 69 73 43 6f 6e  ;.  return isCon
14437 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c  st;.}../*.** Wal
14438 6b 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  k an expression 
14439 74 72 65 65 2e 20 20 52 65 74 75 72 6e 20 31 20  tree.  Return 1 
1443a 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  if the expressio
1443b 6e 20 69 73 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a  n is constant.**
1443c 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 20 6f 72   that does no or
1443d 69 67 69 6e 61 74 65 20 66 72 6f 6d 20 74 68 65  iginate from the
1443e 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61   ON or USING cla
1443f 75 73 65 73 20 6f 66 20 61 20 6a 6f 69 6e 2e 0a  uses of a join..
14440 2a 2a 20 52 65 74 75 72 6e 20 30 20 69 66 20 69  ** Return 0 if i
14441 74 20 69 6e 76 6f 6c 76 65 73 20 76 61 72 69 61  t involves varia
14442 62 6c 65 73 20 6f 72 20 66 75 6e 63 74 69 6f 6e  bles or function
14443 20 63 61 6c 6c 73 20 6f 72 20 74 65 72 6d 73 20   calls or terms 
14444 66 72 6f 6d 0a 2a 2a 20 61 6e 20 4f 4e 20 6f 72  from.** an ON or
14445 20 55 53 49 4e 47 20 63 6c 61 75 73 65 2e 0a 2a   USING clause..*
14446 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
14447 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72   int sqlite3Expr
14448 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69  IsConstantNotJoi
14449 6e 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 6e  n(Expr *p){.  in
1444a 74 20 69 73 43 6f 6e 73 74 20 3d 20 33 3b 0a 20  t isConst = 3;. 
1444b 20 77 61 6c 6b 45 78 70 72 54 72 65 65 28 70 2c   walkExprTree(p,
1444c 20 65 78 70 72 4e 6f 64 65 49 73 43 6f 6e 73 74   exprNodeIsConst
1444d 61 6e 74 2c 20 26 69 73 43 6f 6e 73 74 29 3b 0a  ant, &isConst);.
1444e 20 20 72 65 74 75 72 6e 20 69 73 43 6f 6e 73 74    return isConst
1444f 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61  !=0;.}../*.** Wa
14450 6c 6b 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  lk an expression
14451 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e 20 31   tree.  Return 1
14452 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69   if the expressi
14453 6f 6e 20 69 73 20 63 6f 6e 73 74 61 6e 74 0a 2a  on is constant.*
14454 2a 20 6f 72 20 61 20 66 75 6e 63 74 69 6f 6e 20  * or a function 
14455 63 61 6c 6c 20 77 69 74 68 20 63 6f 6e 73 74 61  call with consta
14456 6e 74 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 52  nt arguments.  R
14457 65 74 75 72 6e 20 61 6e 64 20 30 20 69 66 20 74  eturn and 0 if t
14458 68 65 72 65 0a 2a 2a 20 61 72 65 20 61 6e 79 20  here.** are any 
14459 76 61 72 69 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a  variables..**.**
1445a 20 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65   For the purpose
1445b 73 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69  s of this functi
1445c 6f 6e 2c 20 61 20 64 6f 75 62 6c 65 2d 71 75 6f  on, a double-quo
1445d 74 65 64 20 73 74 72 69 6e 67 20 28 65 78 3a 20  ted string (ex: 
1445e 22 61 62 63 22 29 0a 2a 2a 20 69 73 20 63 6f 6e  "abc").** is con
1445f 73 69 64 65 72 65 64 20 61 20 76 61 72 69 61 62  sidered a variab
14460 6c 65 20 62 75 74 20 61 20 73 69 6e 67 6c 65 2d  le but a single-
14461 71 75 6f 74 65 64 20 73 74 72 69 6e 67 20 28 65  quoted string (e
14462 78 3a 20 27 61 62 63 27 29 20 69 73 0a 2a 2a 20  x: 'abc') is.** 
14463 61 20 63 6f 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53  a constant..*/.S
14464 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
14465 74 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43  t sqlite3ExprIsC
14466 6f 6e 73 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f  onstantOrFunctio
14467 6e 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 6e  n(Expr *p){.  in
14468 74 20 69 73 43 6f 6e 73 74 20 3d 20 32 3b 0a 20  t isConst = 2;. 
14469 20 77 61 6c 6b 45 78 70 72 54 72 65 65 28 70 2c   walkExprTree(p,
1446a 20 65 78 70 72 4e 6f 64 65 49 73 43 6f 6e 73 74   exprNodeIsConst
1446b 61 6e 74 2c 20 26 69 73 43 6f 6e 73 74 29 3b 0a  ant, &isConst);.
1446c 20 20 72 65 74 75 72 6e 20 69 73 43 6f 6e 73 74    return isConst
1446d 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66  !=0;.}../*.** If
1446e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
1446f 70 20 63 6f 64 65 73 20 61 20 63 6f 6e 73 74 61  p codes a consta
14470 6e 74 20 69 6e 74 65 67 65 72 20 74 68 61 74 20  nt integer that 
14471 69 73 20 73 6d 61 6c 6c 20 65 6e 6f 75 67 68 0a  is small enough.
14472 2a 2a 20 74 6f 20 66 69 74 20 69 6e 20 61 20 33  ** to fit in a 3
14473 32 2d 62 69 74 20 69 6e 74 65 67 65 72 2c 20 72  2-bit integer, r
14474 65 74 75 72 6e 20 31 20 61 6e 64 20 70 75 74 20  eturn 1 and put 
14475 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
14476 20 69 6e 74 65 67 65 72 0a 2a 2a 20 69 6e 20 2a   integer.** in *
14477 70 56 61 6c 75 65 2e 20 20 49 66 20 74 68 65 20  pValue.  If the 
14478 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6e 6f  expression is no
14479 74 20 61 6e 20 69 6e 74 65 67 65 72 20 6f 72 20  t an integer or 
1447a 69 66 20 69 74 20 69 73 20 74 6f 6f 20 62 69 67  if it is too big
1447b 0a 2a 2a 20 74 6f 20 66 69 74 20 69 6e 20 61 20  .** to fit in a 
1447c 73 69 67 6e 65 64 20 33 32 2d 62 69 74 20 69 6e  signed 32-bit in
1447d 74 65 67 65 72 2c 20 72 65 74 75 72 6e 20 30 20  teger, return 0 
1447e 61 6e 64 20 6c 65 61 76 65 20 2a 70 56 61 6c 75  and leave *pValu
1447f 65 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a  e unchanged..*/.
14480 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
14481 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 49 73  nt sqlite3ExprIs
14482 49 6e 74 65 67 65 72 28 45 78 70 72 20 2a 70 2c  Integer(Expr *p,
14483 20 69 6e 74 20 2a 70 56 61 6c 75 65 29 7b 0a 20   int *pValue){. 
14484 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70 20 29   switch( p->op )
14485 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e  {.    case TK_IN
14486 54 45 47 45 52 3a 20 7b 0a 20 20 20 20 20 20 69  TEGER: {.      i
14487 66 28 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74  f( sqlite3GetInt
14488 33 32 28 28 63 68 61 72 2a 29 70 2d 3e 74 6f 6b  32((char*)p->tok
14489 65 6e 2e 7a 2c 20 70 56 61 6c 75 65 29 20 29 7b  en.z, pValue) ){
1448a 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
1448b 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  1;.      }.     
1448c 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
1448d 20 20 63 61 73 65 20 54 4b 5f 55 50 4c 55 53 3a    case TK_UPLUS:
1448e 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20   {.      return 
1448f 73 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74  sqlite3ExprIsInt
14490 65 67 65 72 28 70 2d 3e 70 4c 65 66 74 2c 20 70  eger(p->pLeft, p
14491 56 61 6c 75 65 29 3b 0a 20 20 20 20 7d 0a 20 20  Value);.    }.  
14492 20 20 63 61 73 65 20 54 4b 5f 55 4d 49 4e 55 53    case TK_UMINUS
14493 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 76 3b  : {.      int v;
14494 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
14495 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28  e3ExprIsInteger(
14496 70 2d 3e 70 4c 65 66 74 2c 20 26 76 29 20 29 7b  p->pLeft, &v) ){
14497 0a 20 20 20 20 20 20 20 20 2a 70 56 61 6c 75 65  .        *pValue
14498 20 3d 20 2d 76 3b 0a 20 20 20 20 20 20 20 20 72   = -v;.        r
14499 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d  eturn 1;.      }
1449a 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1449b 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a    }.    default:
1449c 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 72 65   break;.  }.  re
1449d 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
1449e 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20   Return TRUE if 
1449f 74 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e 67  the given string
144a0 20 69 73 20 61 20 72 6f 77 2d 69 64 20 63 6f 6c   is a row-id col
144a1 75 6d 6e 20 6e 61 6d 65 2e 0a 2a 2f 0a 53 51 4c  umn name..*/.SQL
144a2 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
144a3 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 63  sqlite3IsRowid(c
144a4 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20  onst char *z){. 
144a5 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
144a6 43 6d 70 28 7a 2c 20 22 5f 52 4f 57 49 44 5f 22  Cmp(z, "_ROWID_"
144a7 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b  )==0 ) return 1;
144a8 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
144a9 72 49 43 6d 70 28 7a 2c 20 22 52 4f 57 49 44 22  rICmp(z, "ROWID"
144aa 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b  )==0 ) return 1;
144ab 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
144ac 72 49 43 6d 70 28 7a 2c 20 22 4f 49 44 22 29 3d  rICmp(z, "OID")=
144ad 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20  =0 ) return 1;. 
144ae 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
144af 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 6e 61  .** Given the na
144b0 6d 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 6f  me of a column o
144b1 66 20 74 68 65 20 66 6f 72 6d 20 58 2e 59 2e 5a  f the form X.Y.Z
144b2 20 6f 72 20 59 2e 5a 20 6f 72 20 6a 75 73 74 20   or Y.Z or just 
144b3 5a 2c 20 6c 6f 6f 6b 20 75 70 0a 2a 2a 20 74 68  Z, look up.** th
144b4 61 74 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 73  at name in the s
144b5 65 74 20 6f 66 20 73 6f 75 72 63 65 20 74 61 62  et of source tab
144b6 6c 65 73 20 69 6e 20 70 53 72 63 4c 69 73 74 20  les in pSrcList 
144b7 61 6e 64 20 6d 61 6b 65 20 74 68 65 20 70 45 78  and make the pEx
144b8 70 72 20 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f  pr .** expressio
144b9 6e 20 6e 6f 64 65 20 72 65 66 65 72 20 62 61 63  n node refer bac
144ba 6b 20 74 6f 20 74 68 61 74 20 73 6f 75 72 63 65  k to that source
144bb 20 63 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 66 6f   column.  The fo
144bc 6c 6c 6f 77 69 6e 67 20 63 68 61 6e 67 65 73 0a  llowing changes.
144bd 2a 2a 20 61 72 65 20 6d 61 64 65 20 74 6f 20 70  ** are made to p
144be 45 78 70 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 70  Expr:.**.**    p
144bf 45 78 70 72 2d 3e 69 44 62 20 20 20 20 20 20 20  Expr->iDb       
144c0 20 20 20 20 53 65 74 20 74 68 65 20 69 6e 64 65      Set the inde
144c1 78 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 20 6f  x in db->aDb[] o
144c2 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68  f the database h
144c3 6f 6c 64 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20  olding.**       
144c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
144c5 20 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 20    the table..** 
144c6 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65     pExpr->iTable
144c7 20 20 20 20 20 20 20 20 53 65 74 20 74 6f 20 74          Set to t
144c8 68 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  he cursor number
144c9 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 6f   for the table o
144ca 62 74 61 69 6e 65 64 0a 2a 2a 20 20 20 20 20 20  btained.**      
144cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
144cc 20 20 20 66 72 6f 6d 20 70 53 72 63 4c 69 73 74     from pSrcList
144cd 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 69  ..**    pExpr->i
144ce 43 6f 6c 75 6d 6e 20 20 20 20 20 20 20 53 65 74  Column       Set
144cf 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e   to the column n
144d0 75 6d 62 65 72 20 77 69 74 68 69 6e 20 74 68 65  umber within the
144d1 20 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 20 70 45   table..**    pE
144d2 78 70 72 2d 3e 6f 70 20 20 20 20 20 20 20 20 20  xpr->op         
144d3 20 20 20 53 65 74 20 74 6f 20 54 4b 5f 43 4f 4c     Set to TK_COL
144d4 55 4d 4e 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72  UMN..**    pExpr
144d5 2d 3e 70 4c 65 66 74 20 20 20 20 20 20 20 20 20  ->pLeft         
144d6 41 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74  Any expression t
144d7 68 69 73 20 70 6f 69 6e 74 73 20 74 6f 20 69 73  his points to is
144d8 20 64 65 6c 65 74 65 64 0a 2a 2a 20 20 20 20 70   deleted.**    p
144d9 45 78 70 72 2d 3e 70 52 69 67 68 74 20 20 20 20  Expr->pRight    
144da 20 20 20 20 41 6e 79 20 65 78 70 72 65 73 73 69      Any expressi
144db 6f 6e 20 74 68 69 73 20 70 6f 69 6e 74 73 20 74  on this points t
144dc 6f 20 69 73 20 64 65 6c 65 74 65 64 2e 0a 2a 2a  o is deleted..**
144dd 0a 2a 2a 20 54 68 65 20 70 44 62 54 6f 6b 65 6e  .** The pDbToken
144de 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   is the name of 
144df 74 68 65 20 64 61 74 61 62 61 73 65 20 28 74 68  the database (th
144e0 65 20 22 58 22 29 2e 20 20 54 68 69 73 20 76 61  e "X").  This va
144e1 6c 75 65 20 6d 61 79 20 62 65 0a 2a 2a 20 4e 55  lue may be.** NU
144e2 4c 4c 20 6d 65 61 6e 69 6e 67 20 74 68 61 74 20  LL meaning that 
144e3 6e 61 6d 65 20 69 73 20 6f 66 20 74 68 65 20 66  name is of the f
144e4 6f 72 6d 20 59 2e 5a 20 6f 72 20 5a 2e 20 20 41  orm Y.Z or Z.  A
144e5 6e 79 20 61 76 61 69 6c 61 62 6c 65 20 64 61 74  ny available dat
144e6 61 62 61 73 65 0a 2a 2a 20 63 61 6e 20 62 65 20  abase.** can be 
144e7 75 73 65 64 2e 20 20 54 68 65 20 70 54 61 62 6c  used.  The pTabl
144e8 65 54 6f 6b 65 6e 20 69 73 20 74 68 65 20 6e 61  eToken is the na
144e9 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
144ea 28 74 68 65 20 22 59 22 29 2e 20 20 54 68 69 73  (the "Y").  This
144eb 0a 2a 2a 20 76 61 6c 75 65 20 63 61 6e 20 62 65  .** value can be
144ec 20 4e 55 4c 4c 20 69 66 20 70 44 62 54 6f 6b 65   NULL if pDbToke
144ed 6e 20 69 73 20 61 6c 73 6f 20 4e 55 4c 4c 2e 20  n is also NULL. 
144ee 20 49 66 20 70 54 61 62 6c 65 54 6f 6b 65 6e 20   If pTableToken 
144ef 69 73 20 4e 55 4c 4c 20 69 74 0a 2a 2a 20 6d 65  is NULL it.** me
144f0 61 6e 73 20 74 68 61 74 20 74 68 65 20 66 6f 72  ans that the for
144f1 6d 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 69 73  m of the name is
144f2 20 5a 20 61 6e 64 20 74 68 61 74 20 63 6f 6c 75   Z and that colu
144f3 6d 6e 73 20 66 72 6f 6d 20 61 6e 79 20 74 61 62  mns from any tab
144f4 6c 65 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65  le.** can be use
144f5 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  d..**.** If the 
144f6 6e 61 6d 65 20 63 61 6e 6e 6f 74 20 62 65 20 72  name cannot be r
144f7 65 73 6f 6c 76 65 64 20 75 6e 61 6d 62 69 67 75  esolved unambigu
144f8 6f 75 73 6c 79 2c 20 6c 65 61 76 65 20 61 6e 20  ously, leave an 
144f9 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a  error message.**
144fa 20 69 6e 20 70 50 61 72 73 65 20 61 6e 64 20 72   in pParse and r
144fb 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e 20  eturn non-zero. 
144fc 20 52 65 74 75 72 6e 20 7a 65 72 6f 20 6f 6e 20   Return zero on 
144fd 73 75 63 63 65 73 73 2e 0a 2a 2f 0a 73 74 61 74  success..*/.stat
144fe 69 63 20 69 6e 74 20 6c 6f 6f 6b 75 70 4e 61 6d  ic int lookupNam
144ff 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  e(.  Parse *pPar
14500 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65  se,       /* The
14501 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   parsing context
14502 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 44 62   */.  Token *pDb
14503 54 6f 6b 65 6e 2c 20 20 20 20 20 2f 2a 20 4e 61  Token,     /* Na
14504 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  me of the databa
14505 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 61  se containing ta
14506 62 6c 65 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a  ble, or NULL */.
14507 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 54    Token *pTableT
14508 6f 6b 65 6e 2c 20 20 2f 2a 20 4e 61 6d 65 20 6f  oken,  /* Name o
14509 66 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69  f table containi
1450a 6e 67 20 63 6f 6c 75 6d 6e 2c 20 6f 72 20 4e 55  ng column, or NU
1450b 4c 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  LL */.  Token *p
1450c 43 6f 6c 75 6d 6e 54 6f 6b 65 6e 2c 20 2f 2a 20  ColumnToken, /* 
1450d 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 75  Name of the colu
1450e 6d 6e 2e 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e  mn. */.  NameCon
1450f 74 65 78 74 20 2a 70 4e 43 2c 20 20 20 20 2f 2a  text *pNC,    /*
14510 20 54 68 65 20 6e 61 6d 65 20 63 6f 6e 74 65 78   The name contex
14511 74 20 75 73 65 64 20 74 6f 20 72 65 73 6f 6c 76  t used to resolv
14512 65 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20  e the name */.  
14513 45 78 70 72 20 2a 70 45 78 70 72 20 20 20 20 20  Expr *pExpr     
14514 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 74 68 69       /* Make thi
14515 73 20 45 58 50 52 20 6e 6f 64 65 20 70 6f 69 6e  s EXPR node poin
14516 74 20 74 6f 20 74 68 65 20 73 65 6c 65 63 74 65  t to the selecte
14517 64 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 29 7b 0a 20  d column */.){. 
14518 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 30 3b 20   char *zDb = 0; 
14519 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
1451a 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20   the database.  
1451b 54 68 65 20 22 58 22 20 69 6e 20 58 2e 59 2e 5a  The "X" in X.Y.Z
1451c 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54 61 62   */.  char *zTab
1451d 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e 61   = 0;      /* Na
1451e 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e  me of the table.
1451f 20 20 54 68 65 20 22 59 22 20 69 6e 20 58 2e 59    The "Y" in X.Y
14520 2e 5a 20 6f 72 20 59 2e 5a 20 2a 2f 0a 20 20 63  .Z or Y.Z */.  c
14521 68 61 72 20 2a 7a 43 6f 6c 20 3d 20 30 3b 20 20  har *zCol = 0;  
14522 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
14523 68 65 20 63 6f 6c 75 6d 6e 2e 20 20 54 68 65 20  he column.  The 
14524 22 5a 22 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20  "Z" */.  int i, 
14525 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  j;            /*
14526 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a   Loop counters *
14527 2f 0a 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b  /.  int cnt = 0;
14528 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
14529 65 72 20 6f 66 20 6d 61 74 63 68 69 6e 67 20 63  er of matching c
1452a 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 2a 2f 0a 20  olumn names */. 
1452b 20 69 6e 74 20 63 6e 74 54 61 62 20 3d 20 30 3b   int cntTab = 0;
1452c 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1452d 6f 66 20 6d 61 74 63 68 69 6e 67 20 74 61 62 6c  of matching tabl
1452e 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 73 71 6c  e names */.  sql
1452f 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
14530 65 2d 3e 64 62 3b 20 20 2f 2a 20 54 68 65 20 64  e->db;  /* The d
14531 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 73 74 72  atabase */.  str
14532 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
14533 20 2a 70 49 74 65 6d 3b 20 20 20 20 20 20 20 2f   *pItem;       /
14534 2a 20 55 73 65 20 66 6f 72 20 6c 6f 6f 70 69 6e  * Use for loopin
14535 67 20 6f 76 65 72 20 70 53 72 63 4c 69 73 74 20  g over pSrcList 
14536 69 74 65 6d 73 20 2a 2f 0a 20 20 73 74 72 75 63  items */.  struc
14537 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
14538 70 4d 61 74 63 68 20 3d 20 30 3b 20 20 2f 2a 20  pMatch = 0;  /* 
14539 54 68 65 20 6d 61 74 63 68 69 6e 67 20 70 53 72  The matching pSr
1453a 63 4c 69 73 74 20 69 74 65 6d 20 2a 2f 0a 20 20  cList item */.  
1453b 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 54 6f  NameContext *pTo
1453c 70 4e 43 20 3d 20 70 4e 43 3b 20 20 20 20 20 20  pNC = pNC;      
1453d 20 20 2f 2a 20 46 69 72 73 74 20 6e 61 6d 65 63    /* First namec
1453e 6f 6e 74 65 78 74 20 69 6e 20 74 68 65 20 6c 69  ontext in the li
1453f 73 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  st */..  assert(
14540 20 70 43 6f 6c 75 6d 6e 54 6f 6b 65 6e 20 26 26   pColumnToken &&
14541 20 70 43 6f 6c 75 6d 6e 54 6f 6b 65 6e 2d 3e 7a   pColumnToken->z
14542 20 29 3b 20 2f 2a 20 54 68 65 20 5a 20 69 6e 20   ); /* The Z in 
14543 58 2e 59 2e 5a 20 63 61 6e 6e 6f 74 20 62 65 20  X.Y.Z cannot be 
14544 4e 55 4c 4c 20 2a 2f 0a 20 20 7a 44 62 20 3d 20  NULL */.  zDb = 
14545 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54  sqlite3NameFromT
14546 6f 6b 65 6e 28 70 44 62 54 6f 6b 65 6e 29 3b 0a  oken(pDbToken);.
14547 20 20 7a 54 61 62 20 3d 20 73 71 6c 69 74 65 33    zTab = sqlite3
14548 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 70 54  NameFromToken(pT
14549 61 62 6c 65 54 6f 6b 65 6e 29 3b 0a 20 20 7a 43  ableToken);.  zC
1454a 6f 6c 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65  ol = sqlite3Name
1454b 46 72 6f 6d 54 6f 6b 65 6e 28 70 43 6f 6c 75 6d  FromToken(pColum
1454c 6e 54 6f 6b 65 6e 29 3b 0a 20 20 69 66 28 20 73  nToken);.  if( s
1454d 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69 6c  qlite3MallocFail
1454e 65 64 28 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  ed() ){.    goto
1454f 20 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 3b   lookupname_end;
14550 0a 20 20 7d 0a 0a 20 20 70 45 78 70 72 2d 3e 69  .  }..  pExpr->i
14551 54 61 62 6c 65 20 3d 20 2d 31 3b 0a 20 20 77 68  Table = -1;.  wh
14552 69 6c 65 28 20 70 4e 43 20 26 26 20 63 6e 74 3d  ile( pNC && cnt=
14553 3d 30 20 29 7b 0a 20 20 20 20 45 78 70 72 4c 69  =0 ){.    ExprLi
14554 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 20 20  st *pEList;.    
14555 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73  SrcList *pSrcLis
14556 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73  t = pNC->pSrcLis
14557 74 3b 0a 0a 20 20 20 20 69 66 28 20 70 53 72 63  t;..    if( pSrc
14558 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 66 6f  List ){.      fo
14559 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 53 72  r(i=0, pItem=pSr
1455a 63 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 53 72 63  cList->a; i<pSrc
1455b 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c  List->nSrc; i++,
1455c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20   pItem++){.     
1455d 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a     Table *pTab;.
1455e 20 20 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b          int iDb;
1455f 0a 20 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20  .        Column 
14560 2a 70 43 6f 6c 3b 0a 20 20 0a 20 20 20 20 20 20  *pCol;.  .      
14561 20 20 70 54 61 62 20 3d 20 70 49 74 65 6d 2d 3e    pTab = pItem->
14562 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 61 73  pTab;.        as
14563 73 65 72 74 28 20 70 54 61 62 21 3d 30 20 29 3b  sert( pTab!=0 );
14564 0a 20 20 20 20 20 20 20 20 69 44 62 20 3d 20 73  .        iDb = s
14565 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
14566 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53  dex(db, pTab->pS
14567 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 20 20 20  chema);.        
14568 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e 6e 43  assert( pTab->nC
14569 6f 6c 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20  ol>0 );.        
1456a 69 66 28 20 7a 54 61 62 20 29 7b 0a 20 20 20 20  if( zTab ){.    
1456b 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d        if( pItem-
1456c 3e 7a 41 6c 69 61 73 20 29 7b 0a 20 20 20 20 20  >zAlias ){.     
1456d 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 61         char *zTa
1456e 62 4e 61 6d 65 20 3d 20 70 49 74 65 6d 2d 3e 7a  bName = pItem->z
1456f 41 6c 69 61 73 3b 0a 20 20 20 20 20 20 20 20 20  Alias;.         
14570 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74     if( sqlite3St
14571 72 49 43 6d 70 28 7a 54 61 62 4e 61 6d 65 2c 20  rICmp(zTabName, 
14572 7a 54 61 62 29 21 3d 30 20 29 20 63 6f 6e 74 69  zTab)!=0 ) conti
14573 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  nue;.          }
14574 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
14575 20 20 63 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65    char *zTabName
14576 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a   = pTab->zName;.
14577 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
14578 7a 54 61 62 4e 61 6d 65 3d 3d 30 20 7c 7c 20 73  zTabName==0 || s
14579 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 54  qlite3StrICmp(zT
1457a 61 62 4e 61 6d 65 2c 20 7a 54 61 62 29 21 3d 30  abName, zTab)!=0
1457b 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
1457c 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 44 62           if( zDb
1457d 21 3d 30 20 26 26 20 73 71 6c 69 74 65 33 53 74  !=0 && sqlite3St
1457e 72 49 43 6d 70 28 64 62 2d 3e 61 44 62 5b 69 44  rICmp(db->aDb[iD
1457f 62 5d 2e 7a 4e 61 6d 65 2c 20 7a 44 62 29 21 3d  b].zName, zDb)!=
14580 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
14581 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
14582 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
14583 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
14584 0a 20 20 20 20 20 20 20 20 69 66 28 20 30 3d 3d  .        if( 0==
14585 28 63 6e 74 54 61 62 2b 2b 29 20 29 7b 0a 20 20  (cntTab++) ){.  
14586 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69          pExpr->i
14587 54 61 62 6c 65 20 3d 20 70 49 74 65 6d 2d 3e 69  Table = pItem->i
14588 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 20 20  Cursor;.        
14589 20 20 70 45 78 70 72 2d 3e 70 53 63 68 65 6d 61    pExpr->pSchema
1458a 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61   = pTab->pSchema
1458b 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4d 61 74  ;.          pMat
1458c 63 68 20 3d 20 70 49 74 65 6d 3b 0a 20 20 20 20  ch = pItem;.    
1458d 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 66 6f      }.        fo
1458e 72 28 6a 3d 30 2c 20 70 43 6f 6c 3d 70 54 61 62  r(j=0, pCol=pTab
1458f 2d 3e 61 43 6f 6c 3b 20 6a 3c 70 54 61 62 2d 3e  ->aCol; j<pTab->
14590 6e 43 6f 6c 3b 20 6a 2b 2b 2c 20 70 43 6f 6c 2b  nCol; j++, pCol+
14591 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  +){.          if
14592 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
14593 28 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43  (pCol->zName, zC
14594 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ol)==0 ){.      
14595 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
14596 20 2a 7a 43 6f 6c 6c 20 3d 20 70 54 61 62 2d 3e   *zColl = pTab->
14597 61 43 6f 6c 5b 6a 5d 2e 7a 43 6f 6c 6c 3b 0a 20  aCol[j].zColl;. 
14598 20 20 20 20 20 20 20 20 20 20 20 49 64 4c 69 73             IdLis
14599 74 20 2a 70 55 73 69 6e 67 3b 0a 20 20 20 20 20  t *pUsing;.     
1459a 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20         cnt++;.  
1459b 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d            pExpr-
1459c 3e 69 54 61 62 6c 65 20 3d 20 70 49 74 65 6d 2d  >iTable = pItem-
1459d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20  >iCursor;.      
1459e 20 20 20 20 20 20 70 4d 61 74 63 68 20 3d 20 70        pMatch = p
1459f 49 74 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 20  Item;.          
145a0 20 20 70 45 78 70 72 2d 3e 70 53 63 68 65 6d 61    pExpr->pSchema
145a1 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61   = pTab->pSchema
145a2 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;.            /*
145a3 20 53 75 62 73 74 69 74 75 74 65 20 74 68 65 20   Substitute the 
145a4 72 6f 77 69 64 20 28 63 6f 6c 75 6d 6e 20 2d 31  rowid (column -1
145a5 29 20 66 6f 72 20 74 68 65 20 49 4e 54 45 47 45  ) for the INTEGE
145a6 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 2a 2f  R PRIMARY KEY */
145a7 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78  .            pEx
145a8 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 6a 3d  pr->iColumn = j=
145a9 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 3f 20 2d  =pTab->iPKey ? -
145aa 31 20 3a 20 6a 3b 0a 20 20 20 20 20 20 20 20 20  1 : j;.         
145ab 20 20 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69     pExpr->affini
145ac 74 79 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b  ty = pTab->aCol[
145ad 6a 5d 2e 61 66 66 69 6e 69 74 79 3b 0a 20 20 20  j].affinity;.   
145ae 20 20 20 20 20 20 20 20 20 69 66 28 20 28 70 45           if( (pE
145af 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f  xpr->flags & EP_
145b0 45 78 70 43 6f 6c 6c 61 74 65 29 3d 3d 30 20 29  ExpCollate)==0 )
145b1 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
145b2 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 20 3d 20 73  pExpr->pColl = s
145b3 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65  qlite3FindCollSe
145b4 71 28 64 62 2c 20 45 4e 43 28 64 62 29 2c 20 7a  q(db, ENC(db), z
145b5 43 6f 6c 6c 2c 2d 31 2c 20 30 29 3b 0a 20 20 20  Coll,-1, 0);.   
145b6 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
145b7 20 20 20 20 20 20 20 69 66 28 20 69 3c 70 53 72         if( i<pSr
145b8 63 4c 69 73 74 2d 3e 6e 53 72 63 2d 31 20 29 7b  cList->nSrc-1 ){
145b9 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69  .              i
145ba 66 28 20 70 49 74 65 6d 5b 31 5d 2e 6a 6f 69 6e  f( pItem[1].join
145bb 74 79 70 65 20 26 20 4a 54 5f 4e 41 54 55 52 41  type & JT_NATURA
145bc 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  L ){.           
145bd 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20       /* If this 
145be 6d 61 74 63 68 20 6f 63 63 75 72 72 65 64 20 69  match occurred i
145bf 6e 20 74 68 65 20 6c 65 66 74 20 74 61 62 6c 65  n the left table
145c0 20 6f 66 20 61 20 6e 61 74 75 72 61 6c 20 6a 6f   of a natural jo
145c1 69 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  in,.            
145c2 20 20 20 20 2a 2a 20 74 68 65 6e 20 73 6b 69 70      ** then skip
145c3 20 74 68 65 20 72 69 67 68 74 20 74 61 62 6c 65   the right table
145c4 20 74 6f 20 61 76 6f 69 64 20 61 20 64 75 70 6c   to avoid a dupl
145c5 69 63 61 74 65 20 6d 61 74 63 68 20 2a 2f 0a 20  icate match */. 
145c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
145c7 49 74 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 20 20  Item++;.        
145c8 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20          i++;.   
145c9 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65             }else
145ca 20 69 66 28 20 28 70 55 73 69 6e 67 20 3d 20 70   if( (pUsing = p
145cb 49 74 65 6d 5b 31 5d 2e 70 55 73 69 6e 67 29 21  Item[1].pUsing)!
145cc 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
145cd 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73        /* If this
145ce 20 6d 61 74 63 68 20 6f 63 63 75 72 73 20 6f 6e   match occurs on
145cf 20 61 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69   a column that i
145d0 73 20 69 6e 20 74 68 65 20 55 53 49 4e 47 20 63  s in the USING c
145d1 6c 61 75 73 65 0a 20 20 20 20 20 20 20 20 20 20  lause.          
145d2 20 20 20 20 20 20 2a 2a 20 6f 66 20 61 20 6a 6f        ** of a jo
145d3 69 6e 2c 20 73 6b 69 70 20 74 68 65 20 73 65 61  in, skip the sea
145d4 72 63 68 20 6f 66 20 74 68 65 20 72 69 67 68 74  rch of the right
145d5 20 74 61 62 6c 65 20 6f 66 20 74 68 65 20 6a 6f   table of the jo
145d6 69 6e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  in.             
145d7 20 20 20 2a 2a 20 74 6f 20 61 76 6f 69 64 20 61     ** to avoid a
145d8 20 64 75 70 6c 69 63 61 74 65 20 6d 61 74 63 68   duplicate match
145d9 20 74 68 65 72 65 2e 20 2a 2f 0a 20 20 20 20 20   there. */.     
145da 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6b             int k
145db 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
145dc 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 55 73    for(k=0; k<pUs
145dd 69 6e 67 2d 3e 6e 49 64 3b 20 6b 2b 2b 29 7b 0a  ing->nId; k++){.
145de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
145df 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
145e0 49 43 6d 70 28 70 55 73 69 6e 67 2d 3e 61 5b 6b  ICmp(pUsing->a[k
145e1 5d 2e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3d 3d  ].zName, zCol)==
145e2 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
145e3 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 2b 2b           pItem++
145e4 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
145e5 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20        i++;.     
145e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
145e7 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
145e8 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
145e9 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
145ea 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
145eb 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
145ec 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
145ed 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
145ee 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
145ef 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
145f0 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20  _OMIT_TRIGGER.  
145f1 20 20 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20    /* If we have 
145f2 6e 6f 74 20 61 6c 72 65 61 64 79 20 72 65 73 6f  not already reso
145f3 6c 76 65 64 20 74 68 65 20 6e 61 6d 65 2c 20 74  lved the name, t
145f4 68 65 6e 20 6d 61 79 62 65 20 0a 20 20 20 20 2a  hen maybe .    *
145f5 2a 20 69 74 20 69 73 20 61 20 6e 65 77 2e 2a 20  * it is a new.* 
145f6 6f 72 20 6f 6c 64 2e 2a 20 74 72 69 67 67 65 72  or old.* trigger
145f7 20 61 72 67 75 6d 65 6e 74 20 72 65 66 65 72 65   argument refere
145f8 6e 63 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  nce.    */.    i
145f9 66 28 20 7a 44 62 3d 3d 30 20 26 26 20 7a 54 61  f( zDb==0 && zTa
145fa 62 21 3d 30 20 26 26 20 63 6e 74 3d 3d 30 20 26  b!=0 && cnt==0 &
145fb 26 20 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74  & pParse->trigSt
145fc 61 63 6b 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  ack!=0 ){.      
145fd 54 72 69 67 67 65 72 53 74 61 63 6b 20 2a 70 54  TriggerStack *pT
145fe 72 69 67 67 65 72 53 74 61 63 6b 20 3d 20 70 50  riggerStack = pP
145ff 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 3b  arse->trigStack;
14600 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54  .      Table *pT
14601 61 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66  ab = 0;.      if
14602 28 20 70 54 72 69 67 67 65 72 53 74 61 63 6b 2d  ( pTriggerStack-
14603 3e 6e 65 77 49 64 78 20 21 3d 20 2d 31 20 26 26  >newIdx != -1 &&
14604 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
14605 22 6e 65 77 22 2c 20 7a 54 61 62 29 20 3d 3d 20  "new", zTab) == 
14606 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 45 78  0 ){.        pEx
14607 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 54 72  pr->iTable = pTr
14608 69 67 67 65 72 53 74 61 63 6b 2d 3e 6e 65 77 49  iggerStack->newI
14609 64 78 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  dx;.        asse
1460a 72 74 28 20 70 54 72 69 67 67 65 72 53 74 61 63  rt( pTriggerStac
1460b 6b 2d 3e 70 54 61 62 20 29 3b 0a 20 20 20 20 20  k->pTab );.     
1460c 20 20 20 70 54 61 62 20 3d 20 70 54 72 69 67 67     pTab = pTrigg
1460d 65 72 53 74 61 63 6b 2d 3e 70 54 61 62 3b 0a 20  erStack->pTab;. 
1460e 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70       }else if( p
1460f 54 72 69 67 67 65 72 53 74 61 63 6b 2d 3e 6f 6c  TriggerStack->ol
14610 64 49 64 78 20 21 3d 20 2d 31 20 26 26 20 73 71  dIdx != -1 && sq
14611 6c 69 74 65 33 53 74 72 49 43 6d 70 28 22 6f 6c  lite3StrICmp("ol
14612 64 22 2c 20 7a 54 61 62 29 3d 3d 30 20 29 7b 0a  d", zTab)==0 ){.
14613 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69          pExpr->i
14614 54 61 62 6c 65 20 3d 20 70 54 72 69 67 67 65 72  Table = pTrigger
14615 53 74 61 63 6b 2d 3e 6f 6c 64 49 64 78 3b 0a 20  Stack->oldIdx;. 
14616 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
14617 54 72 69 67 67 65 72 53 74 61 63 6b 2d 3e 70 54  TriggerStack->pT
14618 61 62 20 29 3b 0a 20 20 20 20 20 20 20 20 70 54  ab );.        pT
14619 61 62 20 3d 20 70 54 72 69 67 67 65 72 53 74 61  ab = pTriggerSta
1461a 63 6b 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20  ck->pTab;.      
1461b 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 70 54 61  }..      if( pTa
1461c 62 20 29 7b 20 0a 20 20 20 20 20 20 20 20 69 6e  b ){ .        in
1461d 74 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20  t iCol;.        
1461e 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20 70  Column *pCol = p
1461f 54 61 62 2d 3e 61 43 6f 6c 3b 0a 0a 20 20 20 20  Tab->aCol;..    
14620 20 20 20 20 70 45 78 70 72 2d 3e 70 53 63 68 65      pExpr->pSche
14621 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65  ma = pTab->pSche
14622 6d 61 3b 0a 20 20 20 20 20 20 20 20 63 6e 74 54  ma;.        cntT
14623 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20 20 66 6f  ab++;.        fo
14624 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 20 3c  r(iCol=0; iCol <
14625 20 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f   pTab->nCol; iCo
14626 6c 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 20 7b 0a 20  l++, pCol++) {. 
14627 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c           if( sql
14628 69 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f 6c  ite3StrICmp(pCol
14629 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3d 3d  ->zName, zCol)==
1462a 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
1462b 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f   const char *zCo
1462c 6c 6c 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b  ll = pTab->aCol[
1462d 69 43 6f 6c 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20 20  iCol].zColl;.   
1462e 20 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a           cnt++;.
1462f 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70              pExp
14630 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 69 43 6f  r->iColumn = iCo
14631 6c 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 3f  l==pTab->iPKey ?
14632 20 2d 31 20 3a 20 69 43 6f 6c 3b 0a 20 20 20 20   -1 : iCol;.    
14633 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 61          pExpr->a
14634 66 66 69 6e 69 74 79 20 3d 20 70 54 61 62 2d 3e  ffinity = pTab->
14635 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 61 66 66 69 6e  aCol[iCol].affin
14636 69 74 79 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ity;.           
14637 20 69 66 28 20 28 70 45 78 70 72 2d 3e 66 6c 61   if( (pExpr->fla
14638 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61  gs & EP_ExpColla
14639 74 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  te)==0 ){.      
1463a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70          pExpr->p
1463b 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69  Coll = sqlite3Fi
1463c 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e  ndCollSeq(db, EN
1463d 43 28 64 62 29 2c 20 7a 43 6f 6c 6c 2c 2d 31 2c  C(db), zColl,-1,
1463e 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20   0);.           
1463f 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 70   }.            p
14640 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 54 61  Expr->pTab = pTa
14641 62 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62  b;.            b
14642 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
14643 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
14644 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66    }.    }.#endif
14645 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c   /* !defined(SQL
14646 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
14647 29 20 2a 2f 0a 0a 20 20 20 20 2f 2a 0a 20 20 20  ) */..    /*.   
14648 20 2a 2a 20 50 65 72 68 61 70 73 20 74 68 65 20   ** Perhaps the 
14649 6e 61 6d 65 20 69 73 20 61 20 72 65 66 65 72 65  name is a refere
1464a 6e 63 65 20 74 6f 20 74 68 65 20 52 4f 57 49 44  nce to the ROWID
1464b 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
1464c 63 6e 74 3d 3d 30 20 26 26 20 63 6e 74 54 61 62  cnt==0 && cntTab
1464d 3d 3d 31 20 26 26 20 73 71 6c 69 74 65 33 49 73  ==1 && sqlite3Is
1464e 52 6f 77 69 64 28 7a 43 6f 6c 29 20 29 7b 0a 20  Rowid(zCol) ){. 
1464f 20 20 20 20 20 63 6e 74 20 3d 20 31 3b 0a 20 20       cnt = 1;.  
14650 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75      pExpr->iColu
14651 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 70  mn = -1;.      p
14652 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 20 3d  Expr->affinity =
14653 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45   SQLITE_AFF_INTE
14654 47 45 52 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  GER;.    }..    
14655 2f 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65  /*.    ** If the
14656 20 69 6e 70 75 74 20 69 73 20 6f 66 20 74 68 65   input is of the
14657 20 66 6f 72 6d 20 5a 20 28 6e 6f 74 20 59 2e 5a   form Z (not Y.Z
14658 20 6f 72 20 58 2e 59 2e 5a 29 20 74 68 65 6e 20   or X.Y.Z) then 
14659 74 68 65 20 6e 61 6d 65 20 5a 0a 20 20 20 20 2a  the name Z.    *
1465a 2a 20 6d 69 67 68 74 20 72 65 66 65 72 20 74 6f  * might refer to
1465b 20 61 6e 20 72 65 73 75 6c 74 2d 73 65 74 20 61   an result-set a
1465c 6c 69 61 73 2e 20 20 54 68 69 73 20 68 61 70 70  lias.  This happ
1465d 65 6e 73 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65  ens, for example
1465e 2c 20 77 68 65 6e 0a 20 20 20 20 2a 2a 20 77 65  , when.    ** we
1465f 20 61 72 65 20 72 65 73 6f 6c 76 69 6e 67 20 6e   are resolving n
14660 61 6d 65 73 20 69 6e 20 74 68 65 20 57 48 45 52  ames in the WHER
14661 45 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20  E clause of the 
14662 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6d 6d 61 6e  following comman
14663 64 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  d:.    **.    **
14664 20 20 20 20 20 53 45 4c 45 43 54 20 61 2b 62 20       SELECT a+b 
14665 41 53 20 78 20 46 52 4f 4d 20 74 61 62 6c 65 20  AS x FROM table 
14666 57 48 45 52 45 20 78 3c 31 30 3b 0a 20 20 20 20  WHERE x<10;.    
14667 2a 2a 0a 20 20 20 20 2a 2a 20 49 6e 20 63 61 73  **.    ** In cas
14668 65 73 20 6c 69 6b 65 20 74 68 69 73 2c 20 72 65  es like this, re
14669 70 6c 61 63 65 20 70 45 78 70 72 20 77 69 74 68  place pExpr with
1466a 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 65   a copy of the e
1466b 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 0a 20  xpression that. 
1466c 20 20 20 2a 2a 20 66 6f 72 6d 73 20 74 68 65 20     ** forms the 
1466d 72 65 73 75 6c 74 20 73 65 74 20 65 6e 74 72 79  result set entry
1466e 20 28 22 61 2b 62 22 20 69 6e 20 74 68 65 20 65   ("a+b" in the e
1466f 78 61 6d 70 6c 65 29 20 61 6e 64 20 72 65 74 75  xample) and retu
14670 72 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a  rn immediately..
14671 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74      ** Note that
14672 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
14673 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  in the result se
14674 74 20 73 68 6f 75 6c 64 20 68 61 76 65 20 61 6c  t should have al
14675 72 65 61 64 79 20 62 65 65 6e 0a 20 20 20 20 2a  ready been.    *
14676 2a 20 72 65 73 6f 6c 76 65 64 20 62 79 20 74 68  * resolved by th
14677 65 20 74 69 6d 65 20 74 68 65 20 57 48 45 52 45  e time the WHERE
14678 20 63 6c 61 75 73 65 20 69 73 20 72 65 73 6f 6c   clause is resol
14679 76 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ved..    */.    
1467a 69 66 28 20 63 6e 74 3d 3d 30 20 26 26 20 28 70  if( cnt==0 && (p
1467b 45 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 45 4c  EList = pNC->pEL
1467c 69 73 74 29 21 3d 30 20 26 26 20 7a 54 61 62 3d  ist)!=0 && zTab=
1467d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28  =0 ){.      for(
1467e 6a 3d 30 3b 20 6a 3c 70 45 4c 69 73 74 2d 3e 6e  j=0; j<pEList->n
1467f 45 78 70 72 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20  Expr; j++){.    
14680 20 20 20 20 63 68 61 72 20 2a 7a 41 73 20 3d 20      char *zAs = 
14681 70 45 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 7a 4e 61  pEList->a[j].zNa
14682 6d 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  me;.        if( 
14683 7a 41 73 21 3d 30 20 26 26 20 73 71 6c 69 74 65  zAs!=0 && sqlite
14684 33 53 74 72 49 43 6d 70 28 7a 41 73 2c 20 7a 43  3StrICmp(zAs, zC
14685 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ol)==0 ){.      
14686 20 20 20 20 45 78 70 72 20 2a 70 44 75 70 2c 20      Expr *pDup, 
14687 2a 70 4f 72 69 67 3b 0a 20 20 20 20 20 20 20 20  *pOrig;.        
14688 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
14689 3e 70 4c 65 66 74 3d 3d 30 20 26 26 20 70 45 78  >pLeft==0 && pEx
1468a 70 72 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b  pr->pRight==0 );
1468b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
1468c 74 28 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 3d  t( pExpr->pList=
1468d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  =0 );.          
1468e 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70  assert( pExpr->p
1468f 53 65 6c 65 63 74 3d 3d 30 20 29 3b 0a 20 20 20  Select==0 );.   
14690 20 20 20 20 20 20 20 70 4f 72 69 67 20 3d 20 70         pOrig = p
14691 45 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 45 78 70  EList->a[j].pExp
14692 72 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  r;.          if(
14693 20 21 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67 20   !pNC->allowAgg 
14694 26 26 20 45 78 70 72 48 61 73 50 72 6f 70 65 72  && ExprHasProper
14695 74 79 28 70 4f 72 69 67 2c 20 45 50 5f 41 67 67  ty(pOrig, EP_Agg
14696 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ) ){.           
14697 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
14698 28 70 50 61 72 73 65 2c 20 22 6d 69 73 75 73 65  (pParse, "misuse
14699 20 6f 66 20 61 6c 69 61 73 65 64 20 61 67 67 72   of aliased aggr
1469a 65 67 61 74 65 20 25 73 22 2c 20 7a 41 73 29 3b  egate %s", zAs);
1469b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
1469c 69 74 65 46 72 65 65 28 7a 43 6f 6c 29 3b 0a 20  iteFree(zCol);. 
1469d 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72             retur
1469e 6e 20 32 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  n 2;.          }
1469f 0a 20 20 20 20 20 20 20 20 20 20 70 44 75 70 20  .          pDup 
146a0 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70  = sqlite3ExprDup
146a1 28 70 4f 72 69 67 29 3b 0a 20 20 20 20 20 20 20  (pOrig);.       
146a2 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 66 6c     if( pExpr->fl
146a3 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c  ags & EP_ExpColl
146a4 61 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ate ){.         
146a5 20 20 20 70 44 75 70 2d 3e 70 43 6f 6c 6c 20 3d     pDup->pColl =
146a6 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 3b 0a 20   pExpr->pColl;. 
146a7 20 20 20 20 20 20 20 20 20 20 20 70 44 75 70 2d             pDup-
146a8 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 45 78 70  >flags |= EP_Exp
146a9 43 6f 6c 6c 61 74 65 3b 0a 20 20 20 20 20 20 20  Collate;.       
146aa 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69     }.          i
146ab 66 28 20 70 45 78 70 72 2d 3e 73 70 61 6e 2e 64  f( pExpr->span.d
146ac 79 6e 20 29 20 73 71 6c 69 74 65 46 72 65 65 28  yn ) sqliteFree(
146ad 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 73 70  (char*)pExpr->sp
146ae 61 6e 2e 7a 29 3b 0a 20 20 20 20 20 20 20 20 20  an.z);.         
146af 20 69 66 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65   if( pExpr->toke
146b0 6e 2e 64 79 6e 20 29 20 73 71 6c 69 74 65 46 72  n.dyn ) sqliteFr
146b1 65 65 28 28 63 68 61 72 2a 29 70 45 78 70 72 2d  ee((char*)pExpr-
146b2 3e 74 6f 6b 65 6e 2e 7a 29 3b 0a 20 20 20 20 20  >token.z);.     
146b3 20 20 20 20 20 6d 65 6d 63 70 79 28 70 45 78 70       memcpy(pExp
146b4 72 2c 20 70 44 75 70 2c 20 73 69 7a 65 6f 66 28  r, pDup, sizeof(
146b5 2a 70 45 78 70 72 29 29 3b 0a 20 20 20 20 20 20  *pExpr));.      
146b6 20 20 20 20 73 71 6c 69 74 65 46 72 65 65 28 70      sqliteFree(p
146b7 44 75 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20  Dup);.          
146b8 63 6e 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  cnt = 1;.       
146b9 20 20 20 70 4d 61 74 63 68 20 3d 20 30 3b 0a 20     pMatch = 0;. 
146ba 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
146bb 20 7a 54 61 62 3d 3d 30 20 26 26 20 7a 44 62 3d   zTab==0 && zDb=
146bc 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  =0 );.          
146bd 67 6f 74 6f 20 6c 6f 6f 6b 75 70 6e 61 6d 65 5f  goto lookupname_
146be 65 6e 64 5f 32 3b 0a 20 20 20 20 20 20 20 20 7d  end_2;.        }
146bf 0a 20 20 20 20 20 20 7d 20 0a 20 20 20 20 7d 0a  .      } .    }.
146c0 0a 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20  .    /* Advance 
146c1 74 6f 20 74 68 65 20 6e 65 78 74 20 6e 61 6d 65  to the next name
146c2 20 63 6f 6e 74 65 78 74 2e 20 20 54 68 65 20 6c   context.  The l
146c3 6f 6f 70 20 77 69 6c 6c 20 65 78 69 74 20 77 68  oop will exit wh
146c4 65 6e 20 65 69 74 68 65 72 0a 20 20 20 20 2a 2a  en either.    **
146c5 20 77 65 20 68 61 76 65 20 61 20 6d 61 74 63 68   we have a match
146c6 20 28 63 6e 74 3e 30 29 20 6f 72 20 77 68 65 6e   (cnt>0) or when
146c7 20 77 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 6e   we run out of n
146c8 61 6d 65 20 63 6f 6e 74 65 78 74 73 2e 0a 20 20  ame contexts..  
146c9 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e 74    */.    if( cnt
146ca 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 4e 43  ==0 ){.      pNC
146cb 20 3d 20 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20   = pNC->pNext;. 
146cc 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20     }.  }..  /*. 
146cd 20 2a 2a 20 49 66 20 58 20 61 6e 64 20 59 20 61   ** If X and Y a
146ce 72 65 20 4e 55 4c 4c 20 28 69 6e 20 6f 74 68 65  re NULL (in othe
146cf 72 20 77 6f 72 64 73 20 69 66 20 6f 6e 6c 79 20  r words if only 
146d0 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20  the column name 
146d1 5a 20 69 73 0a 20 20 2a 2a 20 73 75 70 70 6c 69  Z is.  ** suppli
146d2 65 64 29 20 61 6e 64 20 74 68 65 20 76 61 6c 75  ed) and the valu
146d3 65 20 6f 66 20 5a 20 69 73 20 65 6e 63 6c 6f 73  e of Z is enclos
146d4 65 64 20 69 6e 20 64 6f 75 62 6c 65 2d 71 75 6f  ed in double-quo
146d5 74 65 73 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 5a  tes, then.  ** Z
146d6 20 69 73 20 61 20 73 74 72 69 6e 67 20 6c 69 74   is a string lit
146d7 65 72 61 6c 20 69 66 20 69 74 20 64 6f 65 73 6e  eral if it doesn
146d8 27 74 20 6d 61 74 63 68 20 61 6e 79 20 63 6f 6c  't match any col
146d9 75 6d 6e 20 6e 61 6d 65 73 2e 20 20 49 6e 20 74  umn names.  In t
146da 68 61 74 0a 20 20 2a 2a 20 63 61 73 65 2c 20 77  hat.  ** case, w
146db 65 20 6e 65 65 64 20 74 6f 20 72 65 74 75 72 6e  e need to return
146dc 20 72 69 67 68 74 20 61 77 61 79 20 61 6e 64 20   right away and 
146dd 6e 6f 74 20 6d 61 6b 65 20 61 6e 79 20 63 68 61  not make any cha
146de 6e 67 65 73 20 74 6f 0a 20 20 2a 2a 20 70 45 78  nges to.  ** pEx
146df 70 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 42 65  pr..  **.  ** Be
146e0 63 61 75 73 65 20 6e 6f 20 72 65 66 65 72 65 6e  cause no referen
146e1 63 65 20 77 61 73 20 6d 61 64 65 20 74 6f 20 6f  ce was made to o
146e2 75 74 65 72 20 63 6f 6e 74 65 78 74 73 2c 20 74  uter contexts, t
146e3 68 65 20 70 4e 43 2d 3e 6e 52 65 66 0a 20 20 2a  he pNC->nRef.  *
146e4 2a 20 66 69 65 6c 64 73 20 61 72 65 20 6e 6f 74  * fields are not
146e5 20 63 68 61 6e 67 65 64 20 69 6e 20 61 6e 79 20   changed in any 
146e6 63 6f 6e 74 65 78 74 2e 0a 20 20 2a 2f 0a 20 20  context..  */.  
146e7 69 66 28 20 63 6e 74 3d 3d 30 20 26 26 20 7a 54  if( cnt==0 && zT
146e8 61 62 3d 3d 30 20 26 26 20 70 43 6f 6c 75 6d 6e  ab==0 && pColumn
146e9 54 6f 6b 65 6e 2d 3e 7a 5b 30 5d 3d 3d 27 22 27  Token->z[0]=='"'
146ea 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 46 72   ){.    sqliteFr
146eb 65 65 28 7a 43 6f 6c 29 3b 0a 20 20 20 20 72 65  ee(zCol);.    re
146ec 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f  turn 0;.  }..  /
146ed 2a 0a 20 20 2a 2a 20 63 6e 74 3d 3d 30 20 6d 65  *.  ** cnt==0 me
146ee 61 6e 73 20 74 68 65 72 65 20 77 61 73 20 6e 6f  ans there was no
146ef 74 20 6d 61 74 63 68 2e 20 20 63 6e 74 3e 31 20  t match.  cnt>1 
146f0 6d 65 61 6e 73 20 74 68 65 72 65 20 77 65 72 65  means there were
146f1 20 74 77 6f 20 6f 72 0a 20 20 2a 2a 20 6d 6f 72   two or.  ** mor
146f2 65 20 6d 61 74 63 68 65 73 2e 20 20 45 69 74 68  e matches.  Eith
146f3 65 72 20 77 61 79 2c 20 77 65 20 68 61 76 65 20  er way, we have 
146f4 61 6e 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20  an error..  */. 
146f5 20 69 66 28 20 63 6e 74 21 3d 31 20 29 7b 0a 20   if( cnt!=1 ){. 
146f6 20 20 20 63 68 61 72 20 2a 7a 20 3d 20 30 3b 0a     char *z = 0;.
146f7 20 20 20 20 63 68 61 72 20 2a 7a 45 72 72 3b 0a      char *zErr;.
146f8 20 20 20 20 7a 45 72 72 20 3d 20 63 6e 74 3d 3d      zErr = cnt==
146f9 30 20 3f 20 22 6e 6f 20 73 75 63 68 20 63 6f 6c  0 ? "no such col
146fa 75 6d 6e 3a 20 25 73 22 20 3a 20 22 61 6d 62 69  umn: %s" : "ambi
146fb 67 75 6f 75 73 20 63 6f 6c 75 6d 6e 20 6e 61 6d  guous column nam
146fc 65 3a 20 25 73 22 3b 0a 20 20 20 20 69 66 28 20  e: %s";.    if( 
146fd 7a 44 62 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  zDb ){.      sql
146fe 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 7a  ite3SetString(&z
146ff 2c 20 7a 44 62 2c 20 22 2e 22 2c 20 7a 54 61 62  , zDb, ".", zTab
14700 2c 20 22 2e 22 2c 20 7a 43 6f 6c 2c 20 28 63 68  , ".", zCol, (ch
14701 61 72 2a 29 30 29 3b 0a 20 20 20 20 7d 65 6c 73  ar*)0);.    }els
14702 65 20 69 66 28 20 7a 54 61 62 20 29 7b 0a 20 20  e if( zTab ){.  
14703 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74      sqlite3SetSt
14704 72 69 6e 67 28 26 7a 2c 20 7a 54 61 62 2c 20 22  ring(&z, zTab, "
14705 2e 22 2c 20 7a 43 6f 6c 2c 20 28 63 68 61 72 2a  .", zCol, (char*
14706 29 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  )0);.    }else{.
14707 20 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65        z = sqlite
14708 53 74 72 44 75 70 28 7a 43 6f 6c 29 3b 0a 20 20  StrDup(zCol);.  
14709 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 45    }.    sqlite3E
1470a 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
1470b 7a 45 72 72 2c 20 7a 29 3b 0a 20 20 20 20 73 71  zErr, z);.    sq
1470c 6c 69 74 65 46 72 65 65 28 7a 29 3b 0a 20 20 20  liteFree(z);.   
1470d 20 70 54 6f 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b   pTopNC->nErr++;
1470e 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20  .  }..  /* If a 
1470f 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 61 20 74 61  column from a ta
14710 62 6c 65 20 69 6e 20 70 53 72 63 4c 69 73 74 20  ble in pSrcList 
14711 69 73 20 72 65 66 65 72 65 6e 63 65 64 2c 20 74  is referenced, t
14712 68 65 6e 20 72 65 63 6f 72 64 0a 20 20 2a 2a 20  hen record.  ** 
14713 74 68 69 73 20 66 61 63 74 20 69 6e 20 74 68 65  this fact in the
14714 20 70 53 72 63 4c 69 73 74 2e 61 5b 5d 2e 63 6f   pSrcList.a[].co
14715 6c 55 73 65 64 20 62 69 74 6d 61 73 6b 2e 20 20  lUsed bitmask.  
14716 43 6f 6c 75 6d 6e 20 30 20 63 61 75 73 65 73 0a  Column 0 causes.
14717 20 20 2a 2a 20 62 69 74 20 30 20 74 6f 20 62 65    ** bit 0 to be
14718 20 73 65 74 2e 20 20 43 6f 6c 75 6d 6e 20 31 20   set.  Column 1 
14719 73 65 74 73 20 62 69 74 20 31 2e 20 20 41 6e 64  sets bit 1.  And
1471a 20 73 6f 20 66 6f 72 74 68 2e 20 20 49 66 20 74   so forth.  If t
1471b 68 65 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6e  he.  ** column n
1471c 75 6d 62 65 72 20 69 73 20 67 72 65 61 74 65 72  umber is greater
1471d 20 74 68 61 6e 20 74 68 65 20 6e 75 6d 62 65 72   than the number
1471e 20 6f 66 20 62 69 74 73 20 69 6e 20 74 68 65 20   of bits in the 
1471f 62 69 74 6d 61 73 6b 0a 20 20 2a 2a 20 74 68 65  bitmask.  ** the
14720 6e 20 73 65 74 20 74 68 65 20 68 69 67 68 2d 6f  n set the high-o
14721 72 64 65 72 20 62 69 74 20 6f 66 20 74 68 65 20  rder bit of the 
14722 62 69 74 6d 61 73 6b 2e 0a 20 20 2a 2f 0a 20 20  bitmask..  */.  
14723 69 66 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75  if( pExpr->iColu
14724 6d 6e 3e 3d 30 20 26 26 20 70 4d 61 74 63 68 21  mn>=0 && pMatch!
14725 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 20  =0 ){.    int n 
14726 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e  = pExpr->iColumn
14727 3b 0a 20 20 20 20 69 66 28 20 6e 3e 3d 73 69 7a  ;.    if( n>=siz
14728 65 6f 66 28 42 69 74 6d 61 73 6b 29 2a 38 20 29  eof(Bitmask)*8 )
14729 7b 0a 20 20 20 20 20 20 6e 20 3d 20 73 69 7a 65  {.      n = size
1472a 6f 66 28 42 69 74 6d 61 73 6b 29 2a 38 2d 31 3b  of(Bitmask)*8-1;
1472b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
1472c 74 28 20 70 4d 61 74 63 68 2d 3e 69 43 75 72 73  t( pMatch->iCurs
1472d 6f 72 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c  or==pExpr->iTabl
1472e 65 20 29 3b 0a 20 20 20 20 70 4d 61 74 63 68 2d  e );.    pMatch-
1472f 3e 63 6f 6c 55 73 65 64 20 7c 3d 20 28 28 42 69  >colUsed |= ((Bi
14730 74 6d 61 73 6b 29 31 29 3c 3c 6e 3b 0a 20 20 7d  tmask)1)<<n;.  }
14731 0a 0a 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64  ..lookupname_end
14732 3a 0a 20 20 2f 2a 20 43 6c 65 61 6e 20 75 70 20  :.  /* Clean up 
14733 61 6e 64 20 72 65 74 75 72 6e 0a 20 20 2a 2f 0a  and return.  */.
14734 20 20 73 71 6c 69 74 65 46 72 65 65 28 7a 44 62    sqliteFree(zDb
14735 29 3b 0a 20 20 73 71 6c 69 74 65 46 72 65 65 28  );.  sqliteFree(
14736 7a 54 61 62 29 3b 0a 20 20 73 71 6c 69 74 65 33  zTab);.  sqlite3
14737 45 78 70 72 44 65 6c 65 74 65 28 70 45 78 70 72  ExprDelete(pExpr
14738 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 70 45 78 70  ->pLeft);.  pExp
14739 72 2d 3e 70 4c 65 66 74 20 3d 20 30 3b 0a 20 20  r->pLeft = 0;.  
1473a 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
1473b 65 28 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29  e(pExpr->pRight)
1473c 3b 0a 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68  ;.  pExpr->pRigh
1473d 74 20 3d 20 30 3b 0a 20 20 70 45 78 70 72 2d 3e  t = 0;.  pExpr->
1473e 6f 70 20 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 3b 0a  op = TK_COLUMN;.
1473f 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 5f 32  lookupname_end_2
14740 3a 0a 20 20 73 71 6c 69 74 65 46 72 65 65 28 7a  :.  sqliteFree(z
14741 43 6f 6c 29 3b 0a 20 20 69 66 28 20 63 6e 74 3d  Col);.  if( cnt=
14742 3d 31 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  =1 ){.    assert
14743 28 20 70 4e 43 21 3d 30 20 29 3b 0a 20 20 20 20  ( pNC!=0 );.    
14744 73 71 6c 69 74 65 33 41 75 74 68 52 65 61 64 28  sqlite3AuthRead(
14745 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 70  pParse, pExpr, p
14746 4e 43 2d 3e 70 53 72 63 4c 69 73 74 29 3b 0a 20  NC->pSrcList);. 
14747 20 20 20 69 66 28 20 70 4d 61 74 63 68 20 26 26     if( pMatch &&
14748 20 21 70 4d 61 74 63 68 2d 3e 70 53 65 6c 65 63   !pMatch->pSelec
14749 74 20 29 7b 0a 20 20 20 20 20 20 70 45 78 70 72  t ){.      pExpr
1474a 2d 3e 70 54 61 62 20 3d 20 70 4d 61 74 63 68 2d  ->pTab = pMatch-
1474b 3e 70 54 61 62 3b 0a 20 20 20 20 7d 0a 20 20 20  >pTab;.    }.   
1474c 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68   /* Increment th
1474d 65 20 6e 52 65 66 20 76 61 6c 75 65 20 6f 6e 20  e nRef value on 
1474e 61 6c 6c 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74  all name context
1474f 73 20 66 72 6f 6d 20 54 6f 70 4e 43 20 75 70 20  s from TopNC up 
14750 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 70 6f  to.    ** the po
14751 69 6e 74 20 77 68 65 72 65 20 74 68 65 20 6e 61  int where the na
14752 6d 65 20 6d 61 74 63 68 65 64 2e 20 2a 2f 0a 20  me matched. */. 
14753 20 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 20     for(;;){.    
14754 20 20 61 73 73 65 72 74 28 20 70 54 6f 70 4e 43    assert( pTopNC
14755 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 54 6f  !=0 );.      pTo
14756 70 4e 43 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20  pNC->nRef++;.   
14757 20 20 20 69 66 28 20 70 54 6f 70 4e 43 3d 3d 70     if( pTopNC==p
14758 4e 43 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  NC ) break;.    
14759 20 20 70 54 6f 70 4e 43 20 3d 20 70 54 6f 70 4e    pTopNC = pTopN
1475a 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a  C->pNext;.    }.
1475b 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
1475c 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 72 65 74  } else {.    ret
1475d 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  urn 1;.  }.}../*
1475e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
1475f 20 69 73 20 64 65 73 69 67 6e 65 64 20 61 73 20   is designed as 
14760 61 6e 20 78 46 75 6e 63 20 66 6f 72 20 77 61 6c  an xFunc for wal
14761 6b 45 78 70 72 54 72 65 65 28 29 2e 0a 2a 2a 0a  kExprTree()..**.
14762 2a 2a 20 52 65 73 6f 6c 76 65 20 73 79 6d 62 6f  ** Resolve symbo
14763 6c 69 63 20 6e 61 6d 65 73 20 69 6e 74 6f 20 54  lic names into T
14764 4b 5f 43 4f 4c 55 4d 4e 20 6f 70 65 72 61 74 6f  K_COLUMN operato
14765 72 73 20 66 6f 72 20 74 68 65 20 63 75 72 72 65  rs for the curre
14766 6e 74 0a 2a 2a 20 6e 6f 64 65 20 69 6e 20 74 68  nt.** node in th
14767 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65  e expression tre
14768 65 2e 20 20 52 65 74 75 72 6e 20 30 20 74 6f 20  e.  Return 0 to 
14769 63 6f 6e 74 69 6e 75 65 20 74 68 65 20 73 65 61  continue the sea
1476a 72 63 68 20 64 6f 77 6e 0a 2a 2a 20 74 68 65 20  rch down.** the 
1476b 74 72 65 65 20 6f 72 20 32 20 74 6f 20 61 62 6f  tree or 2 to abo
1476c 72 74 20 74 68 65 20 74 72 65 65 20 77 61 6c 6b  rt the tree walk
1476d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
1476e 74 69 6e 65 20 61 6c 73 6f 20 64 6f 65 73 20 65  tine also does e
1476f 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 20 61 6e  rror checking an
14770 64 20 6e 61 6d 65 20 72 65 73 6f 6c 75 74 69 6f  d name resolutio
14771 6e 20 66 6f 72 0a 2a 2a 20 66 75 6e 63 74 69 6f  n for.** functio
14772 6e 20 6e 61 6d 65 73 2e 20 20 54 68 65 20 6f 70  n names.  The op
14773 65 72 61 74 6f 72 20 66 6f 72 20 61 67 67 72 65  erator for aggre
14774 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 69  gate functions i
14775 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 74 6f 20  s changed.** to 
14776 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 2e  TK_AGG_FUNCTION.
14777 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e  .*/.static int n
14778 61 6d 65 52 65 73 6f 6c 76 65 72 53 74 65 70 28  ameResolverStep(
14779 76 6f 69 64 20 2a 70 41 72 67 2c 20 45 78 70 72  void *pArg, Expr
1477a 20 2a 70 45 78 70 72 29 7b 0a 20 20 4e 61 6d 65   *pExpr){.  Name
1477b 43 6f 6e 74 65 78 74 20 2a 70 4e 43 20 3d 20 28  Context *pNC = (
1477c 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 29 70 41 72  NameContext*)pAr
1477d 67 3b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  g;.  Parse *pPar
1477e 73 65 3b 0a 0a 20 20 69 66 28 20 70 45 78 70 72  se;..  if( pExpr
1477f 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  ==0 ) return 1;.
14780 20 20 61 73 73 65 72 74 28 20 70 4e 43 21 3d 30    assert( pNC!=0
14781 20 29 3b 0a 20 20 70 50 61 72 73 65 20 3d 20 70   );.  pParse = p
14782 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 0a 20 20 69  NC->pParse;..  i
14783 66 28 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f  f( ExprHasAnyPro
14784 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
14785 52 65 73 6f 6c 76 65 64 29 20 29 20 72 65 74 75  Resolved) ) retu
14786 72 6e 20 31 3b 0a 20 20 45 78 70 72 53 65 74 50  rn 1;.  ExprSetP
14787 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
14788 50 5f 52 65 73 6f 6c 76 65 64 29 3b 0a 23 69 66  P_Resolved);.#if
14789 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 69 66  ndef NDEBUG.  if
1478a 28 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 20  ( pNC->pSrcList 
1478b 26 26 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74  && pNC->pSrcList
1478c 2d 3e 6e 41 6c 6c 6f 63 3e 30 20 29 7b 0a 20 20  ->nAlloc>0 ){.  
1478d 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c    SrcList *pSrcL
1478e 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c  ist = pNC->pSrcL
1478f 69 73 74 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  ist;.    int i;.
14790 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
14791 4e 43 2d 3e 70 53 72 63 4c 69 73 74 2d 3e 6e 53  NC->pSrcList->nS
14792 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  rc; i++){.      
14793 61 73 73 65 72 74 28 20 70 53 72 63 4c 69 73 74  assert( pSrcList
14794 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 3e 3d  ->a[i].iCursor>=
14795 30 20 26 26 20 70 53 72 63 4c 69 73 74 2d 3e 61  0 && pSrcList->a
14796 5b 69 5d 2e 69 43 75 72 73 6f 72 3c 70 50 61 72  [i].iCursor<pPar
14797 73 65 2d 3e 6e 54 61 62 29 3b 0a 20 20 20 20 7d  se->nTab);.    }
14798 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 73 77  .  }.#endif.  sw
14799 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20  itch( pExpr->op 
1479a 29 7b 0a 20 20 20 20 2f 2a 20 44 6f 75 62 6c 65  ){.    /* Double
1479b 2d 71 75 6f 74 65 64 20 73 74 72 69 6e 67 73 20  -quoted strings 
1479c 28 65 78 3a 20 22 61 62 63 22 29 20 61 72 65 20  (ex: "abc") are 
1479d 75 73 65 64 20 61 73 20 69 64 65 6e 74 69 66 69  used as identifi
1479e 65 72 73 20 69 66 0a 20 20 20 20 2a 2a 20 70 6f  ers if.    ** po
1479f 73 73 69 62 6c 65 2e 20 20 4f 74 68 65 72 77 69  ssible.  Otherwi
147a0 73 65 20 74 68 65 79 20 72 65 6d 61 69 6e 20 61  se they remain a
147a1 73 20 73 74 72 69 6e 67 73 2e 20 20 53 69 6e 67  s strings.  Sing
147a2 6c 65 2d 71 75 6f 74 65 64 0a 20 20 20 20 2a 2a  le-quoted.    **
147a3 20 73 74 72 69 6e 67 73 20 28 65 78 3a 20 27 61   strings (ex: 'a
147a4 62 63 27 29 20 61 72 65 20 61 6c 77 61 79 73 20  bc') are always 
147a5 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 73 2e  string literals.
147a6 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65  .    */.    case
147a7 20 54 4b 5f 53 54 52 49 4e 47 3a 20 7b 0a 20 20   TK_STRING: {.  
147a8 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 74      if( pExpr->t
147a9 6f 6b 65 6e 2e 7a 5b 30 5d 3d 3d 27 5c 27 27 20  oken.z[0]=='\'' 
147aa 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 2f  ) break;.      /
147ab 2a 20 46 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f  * Fall thru into
147ac 20 74 68 65 20 54 4b 5f 49 44 20 63 61 73 65 20   the TK_ID case 
147ad 69 66 20 74 68 69 73 20 69 73 20 61 20 64 6f 75  if this is a dou
147ae 62 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69 6e  ble-quoted strin
147af 67 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 2f  g */.    }.    /
147b0 2a 20 41 20 6c 6f 6e 65 20 69 64 65 6e 74 69 66  * A lone identif
147b1 69 65 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20  ier is the name 
147b2 6f 66 20 61 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20  of a column..   
147b3 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f   */.    case TK_
147b4 49 44 3a 20 7b 0a 20 20 20 20 20 20 6c 6f 6f 6b  ID: {.      look
147b5 75 70 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 30  upName(pParse, 0
147b6 2c 20 30 2c 20 26 70 45 78 70 72 2d 3e 74 6f 6b  , 0, &pExpr->tok
147b7 65 6e 2c 20 70 4e 43 2c 20 70 45 78 70 72 29 3b  en, pNC, pExpr);
147b8 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
147b9 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a  .    }.  .    /*
147ba 20 41 20 74 61 62 6c 65 20 6e 61 6d 65 20 61 6e   A table name an
147bb 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 3a 20 20  d column name:  
147bc 20 20 20 49 44 2e 49 44 0a 20 20 20 20 2a 2a 20     ID.ID.    ** 
147bd 4f 72 20 61 20 64 61 74 61 62 61 73 65 2c 20 74  Or a database, t
147be 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 3a  able and column:
147bf 20 20 49 44 2e 49 44 2e 49 44 0a 20 20 20 20 2a    ID.ID.ID.    *
147c0 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 44 4f  /.    case TK_DO
147c1 54 3a 20 7b 0a 20 20 20 20 20 20 54 6f 6b 65 6e  T: {.      Token
147c2 20 2a 70 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20   *pColumn;.     
147c3 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 3b 0a   Token *pTable;.
147c4 20 20 20 20 20 20 54 6f 6b 65 6e 20 2a 70 44 62        Token *pDb
147c5 3b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 52  ;.      Expr *pR
147c6 69 67 68 74 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  ight;..      /* 
147c7 69 66 28 20 70 53 72 63 4c 69 73 74 3d 3d 30 20  if( pSrcList==0 
147c8 29 20 62 72 65 61 6b 3b 20 2a 2f 0a 20 20 20 20  ) break; */.    
147c9 20 20 70 52 69 67 68 74 20 3d 20 70 45 78 70 72    pRight = pExpr
147ca 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20  ->pRight;.      
147cb 69 66 28 20 70 52 69 67 68 74 2d 3e 6f 70 3d 3d  if( pRight->op==
147cc 54 4b 5f 49 44 20 29 7b 0a 20 20 20 20 20 20 20  TK_ID ){.       
147cd 20 70 44 62 20 3d 20 30 3b 0a 20 20 20 20 20 20   pDb = 0;.      
147ce 20 20 70 54 61 62 6c 65 20 3d 20 26 70 45 78 70    pTable = &pExp
147cf 72 2d 3e 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e 3b  r->pLeft->token;
147d0 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 75 6d 6e  .        pColumn
147d1 20 3d 20 26 70 52 69 67 68 74 2d 3e 74 6f 6b 65   = &pRight->toke
147d2 6e 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  n;.      }else{.
147d3 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
147d4 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 44  pRight->op==TK_D
147d5 4f 54 20 29 3b 0a 20 20 20 20 20 20 20 20 70 44  OT );.        pD
147d6 62 20 3d 20 26 70 45 78 70 72 2d 3e 70 4c 65 66  b = &pExpr->pLef
147d7 74 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20 20 20  t->token;.      
147d8 20 20 70 54 61 62 6c 65 20 3d 20 26 70 52 69 67    pTable = &pRig
147d9 68 74 2d 3e 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e  ht->pLeft->token
147da 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 75 6d  ;.        pColum
147db 6e 20 3d 20 26 70 52 69 67 68 74 2d 3e 70 52 69  n = &pRight->pRi
147dc 67 68 74 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20  ght->token;.    
147dd 20 20 7d 0a 20 20 20 20 20 20 6c 6f 6f 6b 75 70    }.      lookup
147de 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 44 62  Name(pParse, pDb
147df 2c 20 70 54 61 62 6c 65 2c 20 70 43 6f 6c 75 6d  , pTable, pColum
147e0 6e 2c 20 70 4e 43 2c 20 70 45 78 70 72 29 3b 0a  n, pNC, pExpr);.
147e1 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
147e2 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65      }..    /* Re
147e3 73 6f 6c 76 65 20 66 75 6e 63 74 69 6f 6e 20 6e  solve function n
147e4 61 6d 65 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ames.    */.    
147e5 63 61 73 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55  case TK_CONST_FU
147e6 4e 43 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  NC:.    case TK_
147e7 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 20  FUNCTION: {.    
147e8 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73    ExprList *pLis
147e9 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74  t = pExpr->pList
147ea 3b 20 20 20 20 2f 2a 20 54 68 65 20 61 72 67 75  ;    /* The argu
147eb 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 20 20  ment list */.   
147ec 20 20 20 69 6e 74 20 6e 20 3d 20 70 4c 69 73 74     int n = pList
147ed 20 3f 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20   ? pList->nExpr 
147ee 3a 20 30 3b 20 20 2f 2a 20 4e 75 6d 62 65 72 20  : 0;  /* Number 
147ef 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a  of arguments */.
147f0 20 20 20 20 20 20 69 6e 74 20 6e 6f 5f 73 75 63        int no_suc
147f1 68 5f 66 75 6e 63 20 3d 20 30 3b 20 20 20 20 20  h_func = 0;     
147f2 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6e 6f 20    /* True if no 
147f3 73 75 63 68 20 66 75 6e 63 74 69 6f 6e 20 65 78  such function ex
147f4 69 73 74 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e  ists */.      in
147f5 74 20 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73  t wrong_num_args
147f6 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54 72 75   = 0;     /* Tru
147f7 65 20 69 66 20 77 72 6f 6e 67 20 6e 75 6d 62 65  e if wrong numbe
147f8 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 2a  r of arguments *
147f9 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 73 5f 61  /.      int is_a
147fa 67 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  gg = 0;         
147fb 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69      /* True if i
147fc 73 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 66  s an aggregate f
147fd 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20  unction */.     
147fe 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 69 6e   int i;.      in
147ff 74 20 61 75 74 68 3b 20 20 20 20 20 20 20 20 20  t auth;         
14800 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 75 74            /* Aut
14801 68 6f 72 69 7a 61 74 69 6f 6e 20 74 6f 20 75 73  horization to us
14802 65 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a  e the function *
14803 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 64 3b  /.      int nId;
14804 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14805 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
14806 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 66   characters in f
14807 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a  unction name */.
14808 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
14809 20 2a 7a 49 64 3b 20 20 20 20 20 20 20 20 20 20   *zId;          
1480a 20 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f    /* The functio
1480b 6e 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20  n name. */.     
1480c 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 20   FuncDef *pDef; 
1480d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1480e 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  Information abou
1480f 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a  t the function *
14810 2f 0a 20 20 20 20 20 20 69 6e 74 20 65 6e 63 20  /.      int enc 
14811 3d 20 45 4e 43 28 70 50 61 72 73 65 2d 3e 64 62  = ENC(pParse->db
14812 29 3b 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62  );  /* The datab
14813 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a  ase encoding */.
14814 0a 20 20 20 20 20 20 7a 49 64 20 3d 20 28 63 68  .      zId = (ch
14815 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e  ar*)pExpr->token
14816 2e 7a 3b 0a 20 20 20 20 20 20 6e 49 64 20 3d 20  .z;.      nId = 
14817 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 3b 0a  pExpr->token.n;.
14818 20 20 20 20 20 20 70 44 65 66 20 3d 20 73 71 6c        pDef = sql
14819 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e  ite3FindFunction
1481a 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 49 64  (pParse->db, zId
1481b 2c 20 6e 49 64 2c 20 6e 2c 20 65 6e 63 2c 20 30  , nId, n, enc, 0
1481c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 44 65  );.      if( pDe
1481d 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  f==0 ){.        
1481e 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46 69  pDef = sqlite3Fi
1481f 6e 64 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73  ndFunction(pPars
14820 65 2d 3e 64 62 2c 20 7a 49 64 2c 20 6e 49 64 2c  e->db, zId, nId,
14821 20 2d 31 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20   -1, enc, 0);.  
14822 20 20 20 20 20 20 69 66 28 20 70 44 65 66 3d 3d        if( pDef==
14823 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e  0 ){.          n
14824 6f 5f 73 75 63 68 5f 66 75 6e 63 20 3d 20 31 3b  o_such_func = 1;
14825 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
14826 20 20 20 20 20 20 20 20 20 20 77 72 6f 6e 67 5f            wrong_
14827 6e 75 6d 5f 61 72 67 73 20 3d 20 31 3b 0a 20 20  num_args = 1;.  
14828 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65        }.      }e
14829 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 73 5f  lse{.        is_
1482a 61 67 67 20 3d 20 70 44 65 66 2d 3e 78 46 75 6e  agg = pDef->xFun
1482b 63 3d 3d 30 3b 0a 20 20 20 20 20 20 7d 0a 23 69  c==0;.      }.#i
1482c 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1482d 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a  T_AUTHORIZATION.
1482e 20 20 20 20 20 20 69 66 28 20 70 44 65 66 20 29        if( pDef )
1482f 7b 0a 20 20 20 20 20 20 20 20 61 75 74 68 20 3d  {.        auth =
14830 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
14831 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
14832 5f 46 55 4e 43 54 49 4f 4e 2c 20 30 2c 20 70 44  _FUNCTION, 0, pD
14833 65 66 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20  ef->zName, 0);. 
14834 20 20 20 20 20 20 20 69 66 28 20 61 75 74 68 21         if( auth!
14835 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
14836 20 20 20 20 20 20 20 20 69 66 28 20 61 75 74 68          if( auth
14837 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20 29 7b  ==SQLITE_DENY ){
14838 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
14839 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
1483a 72 73 65 2c 20 22 6e 6f 74 20 61 75 74 68 6f 72  rse, "not author
1483b 69 7a 65 64 20 74 6f 20 75 73 65 20 66 75 6e 63  ized to use func
1483c 74 69 6f 6e 3a 20 25 73 22 2c 0a 20 20 20 20 20  tion: %s",.     
1483d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1483e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
1483f 44 65 66 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  Def->zName);.   
14840 20 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45           pNC->nE
14841 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20  rr++;.          
14842 7d 0a 20 20 20 20 20 20 20 20 20 20 70 45 78 70  }.          pExp
14843 72 2d 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b  r->op = TK_NULL;
14844 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
14845 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  n 1;.        }. 
14846 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20       }.#endif.  
14847 20 20 20 20 69 66 28 20 69 73 5f 61 67 67 20 26      if( is_agg &
14848 26 20 21 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67  & !pNC->allowAgg
14849 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
1484a 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1484b 73 65 2c 20 22 6d 69 73 75 73 65 20 6f 66 20 61  se, "misuse of a
1484c 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
1484d 6e 20 25 2e 2a 73 28 29 22 2c 20 6e 49 64 2c 7a  n %.*s()", nId,z
1484e 49 64 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 43  Id);.        pNC
1484f 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20  ->nErr++;.      
14850 20 20 69 73 5f 61 67 67 20 3d 20 30 3b 0a 20 20    is_agg = 0;.  
14851 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 6f      }else if( no
14852 5f 73 75 63 68 5f 66 75 6e 63 20 29 7b 0a 20 20  _such_func ){.  
14853 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
14854 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e  orMsg(pParse, "n
14855 6f 20 73 75 63 68 20 66 75 6e 63 74 69 6f 6e 3a  o such function:
14856 20 25 2e 2a 73 22 2c 20 6e 49 64 2c 20 7a 49 64   %.*s", nId, zId
14857 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 43 2d 3e  );.        pNC->
14858 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 7d 65  nErr++;.      }e
14859 6c 73 65 20 69 66 28 20 77 72 6f 6e 67 5f 6e 75  lse if( wrong_nu
1485a 6d 5f 61 72 67 73 20 29 7b 0a 20 20 20 20 20 20  m_args ){.      
1485b 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
1485c 67 28 70 50 61 72 73 65 2c 22 77 72 6f 6e 67 20  g(pParse,"wrong 
1485d 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65  number of argume
1485e 6e 74 73 20 74 6f 20 66 75 6e 63 74 69 6f 6e 20  nts to function 
1485f 25 2e 2a 73 28 29 22 2c 0a 20 20 20 20 20 20 20  %.*s()",.       
14860 20 20 20 20 20 20 6e 49 64 2c 20 7a 49 64 29 3b        nId, zId);
14861 0a 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45  .        pNC->nE
14862 72 72 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rr++;.      }.  
14863 20 20 20 20 69 66 28 20 69 73 5f 61 67 67 20 29      if( is_agg )
14864 7b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d  {.        pExpr-
14865 3e 6f 70 20 3d 20 54 4b 5f 41 47 47 5f 46 55 4e  >op = TK_AGG_FUN
14866 43 54 49 4f 4e 3b 0a 20 20 20 20 20 20 20 20 70  CTION;.        p
14867 4e 43 2d 3e 68 61 73 41 67 67 20 3d 20 31 3b 0a  NC->hasAgg = 1;.
14868 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
14869 28 20 69 73 5f 61 67 67 20 29 20 70 4e 43 2d 3e  ( is_agg ) pNC->
1486a 61 6c 6c 6f 77 41 67 67 20 3d 20 30 3b 0a 20 20  allowAgg = 0;.  
1486b 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 70 4e 43      for(i=0; pNC
1486c 2d 3e 6e 45 72 72 3d 3d 30 20 26 26 20 69 3c 6e  ->nErr==0 && i<n
1486d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
1486e 77 61 6c 6b 45 78 70 72 54 72 65 65 28 70 4c 69  walkExprTree(pLi
1486f 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 2c 20  st->a[i].pExpr, 
14870 6e 61 6d 65 52 65 73 6f 6c 76 65 72 53 74 65 70  nameResolverStep
14871 2c 20 70 4e 43 29 3b 0a 20 20 20 20 20 20 7d 0a  , pNC);.      }.
14872 20 20 20 20 20 20 69 66 28 20 69 73 5f 61 67 67        if( is_agg
14873 20 29 20 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67   ) pNC->allowAgg
14874 20 3d 20 31 3b 0a 20 20 20 20 20 20 2f 2a 20 46   = 1;.      /* F
14875 49 58 20 4d 45 3a 20 20 43 6f 6d 70 75 74 65 20  IX ME:  Compute 
14876 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 20  pExpr->affinity 
14877 62 61 73 65 64 20 6f 6e 20 74 68 65 20 65 78 70  based on the exp
14878 65 63 74 65 64 20 72 65 74 75 72 6e 0a 20 20 20  ected return.   
14879 20 20 20 2a 2a 20 74 79 70 65 20 6f 66 20 74 68     ** type of th
1487a 65 20 66 75 6e 63 74 69 6f 6e 20 0a 20 20 20 20  e function .    
1487b 20 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72    */.      retur
1487c 6e 20 69 73 5f 61 67 67 3b 0a 20 20 20 20 7d 0a  n is_agg;.    }.
1487d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1487e 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20 20  MIT_SUBQUERY.   
1487f 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 54 3a   case TK_SELECT:
14880 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 58 49  .    case TK_EXI
14881 53 54 53 3a 0a 23 65 6e 64 69 66 0a 20 20 20 20  STS:.#endif.    
14882 63 61 73 65 20 54 4b 5f 49 4e 3a 20 7b 0a 20 20  case TK_IN: {.  
14883 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 70      if( pExpr->p
14884 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20  Select ){.      
14885 20 20 69 6e 74 20 6e 52 65 66 20 3d 20 70 4e 43    int nRef = pNC
14886 2d 3e 6e 52 65 66 3b 0a 23 69 66 6e 64 65 66 20  ->nRef;.#ifndef 
14887 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43  SQLITE_OMIT_CHEC
14888 4b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4e  K.        if( pN
14889 43 2d 3e 69 73 43 68 65 63 6b 20 29 7b 0a 20 20  C->isCheck ){.  
1488a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
1488b 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 22  rrorMsg(pParse,"
1488c 73 75 62 71 75 65 72 69 65 73 20 70 72 6f 68 69  subqueries prohi
1488d 62 69 74 65 64 20 69 6e 20 43 48 45 43 4b 20 63  bited in CHECK c
1488e 6f 6e 73 74 72 61 69 6e 74 73 22 29 3b 0a 20 20  onstraints");.  
1488f 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20        }.#endif. 
14890 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65         sqlite3Se
14891 6c 65 63 74 52 65 73 6f 6c 76 65 28 70 50 61 72  lectResolve(pPar
14892 73 65 2c 20 70 45 78 70 72 2d 3e 70 53 65 6c 65  se, pExpr->pSele
14893 63 74 2c 20 70 4e 43 29 3b 0a 20 20 20 20 20 20  ct, pNC);.      
14894 20 20 61 73 73 65 72 74 28 20 70 4e 43 2d 3e 6e    assert( pNC->n
14895 52 65 66 3e 3d 6e 52 65 66 20 29 3b 0a 20 20 20  Ref>=nRef );.   
14896 20 20 20 20 20 69 66 28 20 6e 52 65 66 21 3d 70       if( nRef!=p
14897 4e 43 2d 3e 6e 52 65 66 20 29 7b 0a 20 20 20 20  NC->nRef ){.    
14898 20 20 20 20 20 20 45 78 70 72 53 65 74 50 72 6f        ExprSetPro
14899 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
1489a 56 61 72 53 65 6c 65 63 74 29 3b 0a 20 20 20 20  VarSelect);.    
1489b 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
1489c 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1489d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1489e 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 20 20 63  OMIT_CHECK.    c
1489f 61 73 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a  ase TK_VARIABLE:
148a0 20 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4e 43   {.      if( pNC
148a1 2d 3e 69 73 43 68 65 63 6b 20 29 7b 0a 20 20 20  ->isCheck ){.   
148a2 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
148a3 72 4d 73 67 28 70 50 61 72 73 65 2c 22 70 61 72  rMsg(pParse,"par
148a4 61 6d 65 74 65 72 73 20 70 72 6f 68 69 62 69 74  ameters prohibit
148a5 65 64 20 69 6e 20 43 48 45 43 4b 20 63 6f 6e 73  ed in CHECK cons
148a6 74 72 61 69 6e 74 73 22 29 3b 0a 20 20 20 20 20  traints");.     
148a7 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
148a8 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d      }.#endif.  }
148a9 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
148aa 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
148ab 6e 65 20 77 61 6c 6b 73 20 61 6e 20 65 78 70 72  ne walks an expr
148ac 65 73 73 69 6f 6e 20 74 72 65 65 20 61 6e 64 20  ession tree and 
148ad 72 65 73 6f 6c 76 65 73 20 72 65 66 65 72 65 6e  resolves referen
148ae 63 65 73 20 74 6f 0a 2a 2a 20 74 61 62 6c 65 20  ces to.** table 
148af 63 6f 6c 75 6d 6e 73 2e 20 20 4e 6f 64 65 73 20  columns.  Nodes 
148b0 6f 66 20 74 68 65 20 66 6f 72 6d 20 49 44 2e 49  of the form ID.I
148b1 44 20 6f 72 20 49 44 20 72 65 73 6f 6c 76 65 20  D or ID resolve 
148b2 69 6e 74 6f 20 61 6e 0a 2a 2a 20 69 6e 64 65 78  into an.** index
148b3 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 69 6e   to the table in
148b4 20 74 68 65 20 74 61 62 6c 65 20 6c 69 73 74 20   the table list 
148b5 61 6e 64 20 61 20 63 6f 6c 75 6d 6e 20 6f 66 66  and a column off
148b6 73 65 74 2e 20 20 54 68 65 20 0a 2a 2a 20 45 78  set.  The .** Ex
148b7 70 72 2e 6f 70 63 6f 64 65 20 66 6f 72 20 73 75  pr.opcode for su
148b8 63 68 20 6e 6f 64 65 73 20 69 73 20 63 68 61 6e  ch nodes is chan
148b9 67 65 64 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e  ged to TK_COLUMN
148ba 2e 20 20 54 68 65 20 45 78 70 72 2e 69 54 61 62  .  The Expr.iTab
148bb 6c 65 0a 2a 2a 20 76 61 6c 75 65 20 69 73 20 63  le.** value is c
148bc 68 61 6e 67 65 64 20 74 6f 20 74 68 65 20 69 6e  hanged to the in
148bd 64 65 78 20 6f 66 20 74 68 65 20 72 65 66 65 72  dex of the refer
148be 65 6e 63 65 64 20 74 61 62 6c 65 20 69 6e 20 70  enced table in p
148bf 54 61 62 4c 69 73 74 0a 2a 2a 20 70 6c 75 73 20  TabList.** plus 
148c0 74 68 65 20 22 62 61 73 65 22 20 76 61 6c 75 65  the "base" value
148c1 2e 20 20 54 68 65 20 62 61 73 65 20 76 61 6c 75  .  The base valu
148c2 65 20 77 69 6c 6c 20 75 6c 74 69 6d 61 74 65 6c  e will ultimatel
148c3 79 20 62 65 63 6f 6d 65 20 74 68 65 0a 2a 2a 20  y become the.** 
148c4 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62  VDBE cursor numb
148c5 65 72 20 66 6f 72 20 61 20 63 75 72 73 6f 72 20  er for a cursor 
148c6 74 68 61 74 20 69 73 20 70 6f 69 6e 74 69 6e 67  that is pointing
148c7 20 69 6e 74 6f 20 74 68 65 20 72 65 66 65 72 65   into the refere
148c8 6e 63 65 64 0a 2a 2a 20 74 61 62 6c 65 2e 20 20  nced.** table.  
148c9 54 68 65 20 45 78 70 72 2e 69 43 6f 6c 75 6d 6e  The Expr.iColumn
148ca 20 76 61 6c 75 65 20 69 73 20 63 68 61 6e 67 65   value is change
148cb 64 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f  d to the index o
148cc 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 0a 2a 2a  f the column .**
148cd 20 6f 66 20 74 68 65 20 72 65 66 65 72 65 6e 63   of the referenc
148ce 65 64 20 74 61 62 6c 65 2e 20 20 54 68 65 20 45  ed table.  The E
148cf 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 76 61 6c 75  xpr.iColumn valu
148d0 65 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 61  e for the specia
148d1 6c 0a 2a 2a 20 52 4f 57 49 44 20 63 6f 6c 75 6d  l.** ROWID colum
148d2 6e 20 69 73 20 2d 31 2e 20 20 41 6e 79 20 49 4e  n is -1.  Any IN
148d3 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
148d4 59 20 63 6f 6c 75 6d 6e 20 69 73 20 74 72 69 65  Y column is trie
148d5 64 20 61 73 20 61 6e 0a 2a 2a 20 61 6c 69 61 73  d as an.** alias
148d6 20 66 6f 72 20 52 4f 57 49 44 2e 0a 2a 2a 0a 2a   for ROWID..**.*
148d7 2a 20 41 6c 73 6f 20 72 65 73 6f 6c 76 65 20 66  * Also resolve f
148d8 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 73 20 61 6e  unction names an
148d9 64 20 63 68 65 63 6b 20 74 68 65 20 66 75 6e 63  d check the func
148da 74 69 6f 6e 73 20 66 6f 72 20 70 72 6f 70 65 72  tions for proper
148db 0a 2a 2a 20 75 73 61 67 65 2e 20 20 4d 61 6b 65  .** usage.  Make
148dc 20 73 75 72 65 20 61 6c 6c 20 66 75 6e 63 74 69   sure all functi
148dd 6f 6e 20 6e 61 6d 65 73 20 61 72 65 20 72 65 63  on names are rec
148de 6f 67 6e 69 7a 65 64 20 61 6e 64 20 61 6c 6c 20  ognized and all 
148df 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 68 61 76  functions.** hav
148e0 65 20 74 68 65 20 63 6f 72 72 65 63 74 20 6e 75  e the correct nu
148e1 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
148e2 73 2e 20 20 4c 65 61 76 65 20 61 6e 20 65 72 72  s.  Leave an err
148e3 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 6e  or message.** in
148e4 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67   pParse->zErrMsg
148e5 20 69 66 20 61 6e 79 74 68 69 6e 67 20 69 73 20   if anything is 
148e6 61 6d 69 73 73 2e 20 20 52 65 74 75 72 6e 20 74  amiss.  Return t
148e7 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72  he number of err
148e8 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ors..**.** If th
148e9 65 20 65 78 70 72 65 73 73 69 6f 6e 20 63 6f 6e  e expression con
148ea 74 61 69 6e 73 20 61 67 67 72 65 67 61 74 65 20  tains aggregate 
148eb 66 75 6e 63 74 69 6f 6e 73 20 74 68 65 6e 20 73  functions then s
148ec 65 74 20 74 68 65 20 45 50 5f 41 67 67 0a 2a 2a  et the EP_Agg.**
148ed 20 70 72 6f 70 65 72 74 79 20 6f 6e 20 74 68 65   property on the
148ee 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2f 0a   expression..*/.
148ef 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
148f0 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 52 65  nt sqlite3ExprRe
148f1 73 6f 6c 76 65 4e 61 6d 65 73 28 20 0a 20 20 4e  solveNames( .  N
148f2 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c  ameContext *pNC,
148f3 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 73 70         /* Namesp
148f4 61 63 65 20 74 6f 20 72 65 73 6f 6c 76 65 20 65  ace to resolve e
148f5 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 2e 20 2a  xpressions in. *
148f6 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 20  /.  Expr *pExpr 
148f7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
148f8 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f  he expression to
148f9 20 62 65 20 61 6e 61 6c 79 7a 65 64 2e 20 2a 2f   be analyzed. */
148fa 0a 29 7b 0a 20 20 69 6e 74 20 73 61 76 65 64 48  .){.  int savedH
148fb 61 73 41 67 67 3b 0a 20 20 69 66 28 20 70 45 78  asAgg;.  if( pEx
148fc 70 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  pr==0 ) return 0
148fd 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58  ;.#if SQLITE_MAX
148fe 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 20 20  _EXPR_DEPTH>0.  
148ff 69 66 28 20 28 70 45 78 70 72 2d 3e 6e 48 65 69  if( (pExpr->nHei
14900 67 68 74 2b 70 4e 43 2d 3e 70 50 61 72 73 65 2d  ght+pNC->pParse-
14901 3e 6e 48 65 69 67 68 74 29 3e 53 51 4c 49 54 45  >nHeight)>SQLITE
14902 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 20  _MAX_EXPR_DEPTH 
14903 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
14904 72 6f 72 4d 73 67 28 70 4e 43 2d 3e 70 50 61 72  rorMsg(pNC->pPar
14905 73 65 2c 20 0a 20 20 20 20 20 20 20 22 45 78 70  se, .       "Exp
14906 72 65 73 73 69 6f 6e 20 74 72 65 65 20 69 73 20  ression tree is 
14907 74 6f 6f 20 6c 61 72 67 65 20 28 6d 61 78 69 6d  too large (maxim
14908 75 6d 20 64 65 70 74 68 20 25 64 29 22 2c 0a 20  um depth %d)",. 
14909 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4d 41 58        SQLITE_MAX
1490a 5f 45 58 50 52 5f 44 45 50 54 48 0a 20 20 20 20  _EXPR_DEPTH.    
1490b 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  );.    return 1;
1490c 0a 20 20 7d 0a 20 20 70 4e 43 2d 3e 70 50 61 72  .  }.  pNC->pPar
1490d 73 65 2d 3e 6e 48 65 69 67 68 74 20 2b 3d 20 70  se->nHeight += p
1490e 45 78 70 72 2d 3e 6e 48 65 69 67 68 74 3b 0a 23  Expr->nHeight;.#
1490f 65 6e 64 69 66 0a 20 20 73 61 76 65 64 48 61 73  endif.  savedHas
14910 41 67 67 20 3d 20 70 4e 43 2d 3e 68 61 73 41 67  Agg = pNC->hasAg
14911 67 3b 0a 20 20 70 4e 43 2d 3e 68 61 73 41 67 67  g;.  pNC->hasAgg
14912 20 3d 20 30 3b 0a 20 20 77 61 6c 6b 45 78 70 72   = 0;.  walkExpr
14913 54 72 65 65 28 70 45 78 70 72 2c 20 6e 61 6d 65  Tree(pExpr, name
14914 52 65 73 6f 6c 76 65 72 53 74 65 70 2c 20 70 4e  ResolverStep, pN
14915 43 29 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d  C);.#if SQLITE_M
14916 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a  AX_EXPR_DEPTH>0.
14917 20 20 70 4e 43 2d 3e 70 50 61 72 73 65 2d 3e 6e    pNC->pParse->n
14918 48 65 69 67 68 74 20 2d 3d 20 70 45 78 70 72 2d  Height -= pExpr-
14919 3e 6e 48 65 69 67 68 74 3b 0a 23 65 6e 64 69 66  >nHeight;.#endif
1491a 0a 20 20 69 66 28 20 70 4e 43 2d 3e 6e 45 72 72  .  if( pNC->nErr
1491b 3e 30 20 29 7b 0a 20 20 20 20 45 78 70 72 53 65  >0 ){.    ExprSe
1491c 74 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  tProperty(pExpr,
1491d 20 45 50 5f 45 72 72 6f 72 29 3b 0a 20 20 7d 0a   EP_Error);.  }.
1491e 20 20 69 66 28 20 70 4e 43 2d 3e 68 61 73 41 67    if( pNC->hasAg
1491f 67 20 29 7b 0a 20 20 20 20 45 78 70 72 53 65 74  g ){.    ExprSet
14920 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
14921 45 50 5f 41 67 67 29 3b 0a 20 20 7d 65 6c 73 65  EP_Agg);.  }else
14922 20 69 66 28 20 73 61 76 65 64 48 61 73 41 67 67   if( savedHasAgg
14923 20 29 7b 0a 20 20 20 20 70 4e 43 2d 3e 68 61 73   ){.    pNC->has
14924 41 67 67 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72  Agg = 1;.  }.  r
14925 65 74 75 72 6e 20 45 78 70 72 48 61 73 50 72 6f  eturn ExprHasPro
14926 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
14927 45 72 72 6f 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Error);.}../*.**
14928 20 41 20 70 6f 69 6e 74 65 72 20 69 6e 73 74 61   A pointer insta
14929 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75  nce of this stru
1492a 63 74 75 72 65 20 69 73 20 75 73 65 64 20 74 6f  cture is used to
1492b 20 70 61 73 73 20 69 6e 66 6f 72 6d 61 74 69 6f   pass informatio
1492c 6e 0a 2a 2a 20 74 68 72 6f 75 67 68 20 77 61 6c  n.** through wal
1492d 6b 45 78 70 72 54 72 65 65 20 69 6e 74 6f 20 63  kExprTree into c
1492e 6f 64 65 53 75 62 71 75 65 72 79 53 74 65 70 28  odeSubqueryStep(
1492f 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  )..*/.typedef st
14930 72 75 63 74 20 51 75 65 72 79 43 6f 64 65 72 20  ruct QueryCoder 
14931 51 75 65 72 79 43 6f 64 65 72 3b 0a 73 74 72 75  QueryCoder;.stru
14932 63 74 20 51 75 65 72 79 43 6f 64 65 72 20 7b 0a  ct QueryCoder {.
14933 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b    Parse *pParse;
14934 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
14935 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
14936 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a  .  NameContext *
14937 70 4e 43 3b 20 20 20 20 2f 2a 20 4e 61 6d 65 73  pNC;    /* Names
14938 70 61 63 65 20 6f 66 20 66 69 72 73 74 20 65 6e  pace of first en
14939 63 6c 6f 73 69 6e 67 20 71 75 65 72 79 20 2a 2f  closing query */
1493a 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  .};.../*.** Gene
1493b 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 73 63  rate code for sc
1493c 61 6c 61 72 20 73 75 62 71 75 65 72 69 65 73 20  alar subqueries 
1493d 75 73 65 64 20 61 73 20 61 6e 20 65 78 70 72 65  used as an expre
1493e 73 73 69 6f 6e 0a 2a 2a 20 61 6e 64 20 49 4e 20  ssion.** and IN 
1493f 6f 70 65 72 61 74 6f 72 73 2e 20 20 45 78 61 6d  operators.  Exam
14940 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ples:.**.**     
14941 28 53 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 62  (SELECT a FROM b
14942 29 20 20 20 20 20 20 20 20 20 20 2d 2d 20 73 75  )          -- su
14943 62 71 75 65 72 79 0a 2a 2a 20 20 20 20 20 45 58  bquery.**     EX
14944 49 53 54 53 20 28 53 45 4c 45 43 54 20 61 20 46  ISTS (SELECT a F
14945 52 4f 4d 20 62 29 20 20 20 2d 2d 20 45 58 49 53  ROM b)   -- EXIS
14946 54 53 20 73 75 62 71 75 65 72 79 0a 2a 2a 20 20  TS subquery.**  
14947 20 20 20 78 20 49 4e 20 28 34 2c 35 2c 31 31 29     x IN (4,5,11)
14948 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d                --
14949 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 77 69 74   IN operator wit
1494a 68 20 6c 69 73 74 20 6f 6e 20 72 69 67 68 74 2d  h list on right-
1494b 68 61 6e 64 20 73 69 64 65 0a 2a 2a 20 20 20 20  hand side.**    
1494c 20 78 20 49 4e 20 28 53 45 4c 45 43 54 20 61 20   x IN (SELECT a 
1494d 46 52 4f 4d 20 62 29 20 20 20 20 20 2d 2d 20 49  FROM b)     -- I
1494e 4e 20 6f 70 65 72 61 74 6f 72 20 77 69 74 68 20  N operator with 
1494f 73 75 62 71 75 65 72 79 20 6f 6e 20 74 68 65 20  subquery on the 
14950 72 69 67 68 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20  right.**.** The 
14951 70 45 78 70 72 20 70 61 72 61 6d 65 74 65 72 20  pExpr parameter 
14952 64 65 73 63 72 69 62 65 73 20 74 68 65 20 65 78  describes the ex
14953 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 63 6f  pression that co
14954 6e 74 61 69 6e 73 20 74 68 65 20 49 4e 0a 2a 2a  ntains the IN.**
14955 20 6f 70 65 72 61 74 6f 72 20 6f 72 20 73 75 62   operator or sub
14956 71 75 65 72 79 2e 0a 2a 2f 0a 23 69 66 6e 64 65  query..*/.#ifnde
14957 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  f SQLITE_OMIT_SU
14958 42 51 55 45 52 59 0a 53 51 4c 49 54 45 5f 50 52  BQUERY.SQLITE_PR
14959 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1495a 65 33 43 6f 64 65 53 75 62 73 65 6c 65 63 74 28  e3CodeSubselect(
1495b 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45  Parse *pParse, E
1495c 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 69  xpr *pExpr){.  i
1495d 6e 74 20 74 65 73 74 41 64 64 72 20 3d 20 30 3b  nt testAddr = 0;
1495e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1495f 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 2d 74 69         /* One-ti
14960 6d 65 20 74 65 73 74 20 61 64 64 72 65 73 73 20  me test address 
14961 2a 2f 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73  */.  Vdbe *v = s
14962 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
14963 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d  arse);.  if( v==
14964 30 20 29 20 72 65 74 75 72 6e 3b 0a 0a 0a 20 20  0 ) return;...  
14965 2f 2a 20 54 68 69 73 20 63 6f 64 65 20 6d 75 73  /* This code mus
14966 74 20 62 65 20 72 75 6e 20 69 6e 20 69 74 73 20  t be run in its 
14967 65 6e 74 69 72 65 74 79 20 65 76 65 72 79 20 74  entirety every t
14968 69 6d 65 20 69 74 20 69 73 20 65 6e 63 6f 75 6e  ime it is encoun
14969 74 65 72 65 64 0a 20 20 2a 2a 20 69 66 20 61 6e  tered.  ** if an
1496a 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  y of the followi
1496b 6e 67 20 69 73 20 74 72 75 65 3a 0a 20 20 2a 2a  ng is true:.  **
1496c 0a 20 20 2a 2a 20 20 20 20 2a 20 20 54 68 65 20  .  **    *  The 
1496d 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20  right-hand side 
1496e 69 73 20 61 20 63 6f 72 72 65 6c 61 74 65 64 20  is a correlated 
1496f 73 75 62 71 75 65 72 79 0a 20 20 2a 2a 20 20 20  subquery.  **   
14970 20 2a 20 20 54 68 65 20 72 69 67 68 74 2d 68 61   *  The right-ha
14971 6e 64 20 73 69 64 65 20 69 73 20 61 6e 20 65 78  nd side is an ex
14972 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 63 6f  pression list co
14973 6e 74 61 69 6e 69 6e 67 20 76 61 72 69 61 62 6c  ntaining variabl
14974 65 73 0a 20 20 2a 2a 20 20 20 20 2a 20 20 57 65  es.  **    *  We
14975 20 61 72 65 20 69 6e 73 69 64 65 20 61 20 74 72   are inside a tr
14976 69 67 67 65 72 0a 20 20 2a 2a 0a 20 20 2a 2a 20  igger.  **.  ** 
14977 49 66 20 61 6c 6c 20 6f 66 20 74 68 65 20 61 62  If all of the ab
14978 6f 76 65 20 61 72 65 20 66 61 6c 73 65 2c 20 74  ove are false, t
14979 68 65 6e 20 77 65 20 63 61 6e 20 72 75 6e 20 74  hen we can run t
1497a 68 69 73 20 63 6f 64 65 20 6a 75 73 74 20 6f 6e  his code just on
1497b 63 65 0a 20 20 2a 2a 20 73 61 76 65 20 74 68 65  ce.  ** save the
1497c 20 72 65 73 75 6c 74 73 2c 20 61 6e 64 20 72 65   results, and re
1497d 75 73 65 20 74 68 65 20 73 61 6d 65 20 72 65 73  use the same res
1497e 75 6c 74 20 6f 6e 20 73 75 62 73 65 71 75 65 6e  ult on subsequen
1497f 74 20 69 6e 76 6f 63 61 74 69 6f 6e 73 2e 0a 20  t invocations.. 
14980 20 2a 2f 0a 20 20 69 66 28 20 21 45 78 70 72 48   */.  if( !ExprH
14981 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45  asAnyProperty(pE
14982 78 70 72 2c 20 45 50 5f 56 61 72 53 65 6c 65 63  xpr, EP_VarSelec
14983 74 29 20 26 26 20 21 70 50 61 72 73 65 2d 3e 74  t) && !pParse->t
14984 72 69 67 53 74 61 63 6b 20 29 7b 0a 20 20 20 20  rigStack ){.    
14985 69 6e 74 20 6d 65 6d 20 3d 20 70 50 61 72 73 65  int mem = pParse
14986 2d 3e 6e 4d 65 6d 2b 2b 3b 0a 20 20 20 20 73 71  ->nMem++;.    sq
14987 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
14988 2c 20 4f 50 5f 4d 65 6d 4c 6f 61 64 2c 20 6d 65  , OP_MemLoad, me
14989 6d 2c 20 30 29 3b 0a 20 20 20 20 74 65 73 74 41  m, 0);.    testA
1498a 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
1498b 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 66 2c  eAddOp(v, OP_If,
1498c 20 30 2c 20 30 29 3b 0a 20 20 20 20 61 73 73 65   0, 0);.    asse
1498d 72 74 28 20 74 65 73 74 41 64 64 72 3e 30 20 7c  rt( testAddr>0 |
1498e 7c 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 46  | sqlite3MallocF
1498f 61 69 6c 65 64 28 29 20 29 3b 0a 20 20 20 20 73  ailed() );.    s
14990 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
14991 76 2c 20 4f 50 5f 4d 65 6d 49 6e 74 2c 20 31 2c  v, OP_MemInt, 1,
14992 20 6d 65 6d 29 3b 0a 20 20 7d 0a 0a 20 20 73 77   mem);.  }..  sw
14993 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20  itch( pExpr->op 
14994 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49  ){.    case TK_I
14995 4e 3a 20 7b 0a 20 20 20 20 20 20 63 68 61 72 20  N: {.      char 
14996 61 66 66 69 6e 69 74 79 3b 0a 20 20 20 20 20 20  affinity;.      
14997 4b 65 79 49 6e 66 6f 20 6b 65 79 49 6e 66 6f 3b  KeyInfo keyInfo;
14998 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 3b  .      int addr;
14999 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
1499a 73 73 20 6f 66 20 4f 50 5f 4f 70 65 6e 45 70 68  ss of OP_OpenEph
1499b 65 6d 65 72 61 6c 20 69 6e 73 74 72 75 63 74 69  emeral instructi
1499c 6f 6e 20 2a 2f 0a 0a 20 20 20 20 20 20 61 66 66  on */..      aff
1499d 69 6e 69 74 79 20 3d 20 73 71 6c 69 74 65 33 45  inity = sqlite3E
1499e 78 70 72 41 66 66 69 6e 69 74 79 28 70 45 78 70  xprAffinity(pExp
1499f 72 2d 3e 70 4c 65 66 74 29 3b 0a 0a 20 20 20 20  r->pLeft);..    
149a0 20 20 2f 2a 20 57 68 65 74 68 65 72 20 74 68 69    /* Whether thi
149a1 73 20 69 73 20 61 6e 20 27 78 20 49 4e 28 53 45  s is an 'x IN(SE
149a2 4c 45 43 54 2e 2e 2e 29 27 20 6f 72 20 61 6e 20  LECT...)' or an 
149a3 27 78 20 49 4e 28 3c 65 78 70 72 6c 69 73 74 3e  'x IN(<exprlist>
149a4 29 27 0a 20 20 20 20 20 20 2a 2a 20 65 78 70 72  )'.      ** expr
149a5 65 73 73 69 6f 6e 20 69 74 20 69 73 20 68 61 6e  ession it is han
149a6 64 6c 65 64 20 74 68 65 20 73 61 6d 65 20 77 61  dled the same wa
149a7 79 2e 20 41 20 76 69 72 74 75 61 6c 20 74 61 62  y. A virtual tab
149a8 6c 65 20 69 73 20 0a 20 20 20 20 20 20 2a 2a 20  le is .      ** 
149a9 66 69 6c 6c 65 64 20 77 69 74 68 20 73 69 6e 67  filled with sing
149aa 6c 65 2d 66 69 65 6c 64 20 69 6e 64 65 78 20 6b  le-field index k
149ab 65 79 73 20 72 65 70 72 65 73 65 6e 74 69 6e 67  eys representing
149ac 20 74 68 65 20 72 65 73 75 6c 74 73 0a 20 20 20   the results.   
149ad 20 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 53     ** from the S
149ae 45 4c 45 43 54 20 6f 72 20 74 68 65 20 3c 65 78  ELECT or the <ex
149af 70 72 6c 69 73 74 3e 2e 0a 20 20 20 20 20 20 2a  prlist>..      *
149b0 2a 0a 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68  *.      ** If th
149b1 65 20 27 78 27 20 65 78 70 72 65 73 73 69 6f 6e  e 'x' expression
149b2 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 76 61 6c   is a column val
149b3 75 65 2c 20 6f 72 20 74 68 65 20 53 45 4c 45 43  ue, or the SELEC
149b4 54 2e 2e 2e 0a 20 20 20 20 20 20 2a 2a 20 73 74  T....      ** st
149b5 61 74 65 6d 65 6e 74 20 72 65 74 75 72 6e 73 20  atement returns 
149b6 61 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c 20  a column value, 
149b7 74 68 65 6e 20 74 68 65 20 61 66 66 69 6e 69 74  then the affinit
149b8 79 20 6f 66 20 74 68 61 74 0a 20 20 20 20 20 20  y of that.      
149b9 2a 2a 20 63 6f 6c 75 6d 6e 20 69 73 20 75 73 65  ** column is use
149ba 64 20 74 6f 20 62 75 69 6c 64 20 74 68 65 20 69  d to build the i
149bb 6e 64 65 78 20 6b 65 79 73 2e 20 49 66 20 62 6f  ndex keys. If bo
149bc 74 68 20 27 78 27 20 61 6e 64 20 74 68 65 0a 20  th 'x' and the. 
149bd 20 20 20 20 20 2a 2a 20 53 45 4c 45 43 54 2e 2e       ** SELECT..
149be 2e 20 73 74 61 74 65 6d 65 6e 74 20 61 72 65 20  . statement are 
149bf 63 6f 6c 75 6d 6e 73 2c 20 74 68 65 6e 20 6e 75  columns, then nu
149c0 6d 65 72 69 63 20 61 66 66 69 6e 69 74 79 20 69  meric affinity i
149c1 73 20 75 73 65 64 0a 20 20 20 20 20 20 2a 2a 20  s used.      ** 
149c2 69 66 20 65 69 74 68 65 72 20 63 6f 6c 75 6d 6e  if either column
149c3 20 68 61 73 20 4e 55 4d 45 52 49 43 20 6f 72 20   has NUMERIC or 
149c4 49 4e 54 45 47 45 52 20 61 66 66 69 6e 69 74 79  INTEGER affinity
149c5 2e 20 49 66 20 6e 65 69 74 68 65 72 0a 20 20 20  . If neither.   
149c6 20 20 20 2a 2a 20 27 78 27 20 6e 6f 72 20 74 68     ** 'x' nor th
149c7 65 20 53 45 4c 45 43 54 2e 2e 2e 20 73 74 61 74  e SELECT... stat
149c8 65 6d 65 6e 74 20 61 72 65 20 63 6f 6c 75 6d 6e  ement are column
149c9 73 2c 20 74 68 65 6e 20 6e 75 6d 65 72 69 63 20  s, then numeric 
149ca 61 66 66 69 6e 69 74 79 0a 20 20 20 20 20 20 2a  affinity.      *
149cb 2a 20 69 73 20 75 73 65 64 2e 0a 20 20 20 20 20  * is used..     
149cc 20 2a 2f 0a 20 20 20 20 20 20 70 45 78 70 72 2d   */.      pExpr-
149cd 3e 69 54 61 62 6c 65 20 3d 20 70 50 61 72 73 65  >iTable = pParse
149ce 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20  ->nTab++;.      
149cf 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
149d0 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4f 70  beAddOp(v, OP_Op
149d1 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 70 45 78  enEphemeral, pEx
149d2 70 72 2d 3e 69 54 61 62 6c 65 2c 20 30 29 3b 0a  pr->iTable, 0);.
149d3 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 6b 65        memset(&ke
149d4 79 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66  yInfo, 0, sizeof
149d5 28 6b 65 79 49 6e 66 6f 29 29 3b 0a 20 20 20 20  (keyInfo));.    
149d6 20 20 6b 65 79 49 6e 66 6f 2e 6e 46 69 65 6c 64    keyInfo.nField
149d7 20 3d 20 31 3b 0a 20 20 20 20 20 20 73 71 6c 69   = 1;.      sqli
149d8 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
149d9 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73  OP_SetNumColumns
149da 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c  , pExpr->iTable,
149db 20 31 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20   1);..      if( 
149dc 70 45 78 70 72 2d 3e 70 53 65 6c 65 63 74 20 29  pExpr->pSelect )
149dd 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 61 73  {.        /* Cas
149de 65 20 31 3a 20 20 20 20 20 65 78 70 72 20 49 4e  e 1:     expr IN
149df 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29 0a 20 20   (SELECT ...).  
149e0 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20        **.       
149e1 20 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64   ** Generate cod
149e2 65 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 72  e to write the r
149e3 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 73 65  esults of the se
149e4 6c 65 63 74 20 69 6e 74 6f 20 74 68 65 20 74 65  lect into the te
149e5 6d 70 6f 72 61 72 79 0a 20 20 20 20 20 20 20 20  mporary.        
149e6 2a 2a 20 74 61 62 6c 65 20 61 6c 6c 6f 63 61 74  ** table allocat
149e7 65 64 20 61 6e 64 20 6f 70 65 6e 65 64 20 61 62  ed and opened ab
149e8 6f 76 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  ove..        */.
149e9 20 20 20 20 20 20 20 20 69 6e 74 20 69 50 61 72          int iPar
149ea 6d 20 3d 20 70 45 78 70 72 2d 3e 69 54 61 62 6c  m = pExpr->iTabl
149eb 65 20 2b 20 20 28 28 28 69 6e 74 29 61 66 66 69  e +  (((int)affi
149ec 6e 69 74 79 29 3c 3c 31 36 29 3b 0a 20 20 20 20  nity)<<16);.    
149ed 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45      ExprList *pE
149ee 4c 69 73 74 3b 0a 20 20 20 20 20 20 20 20 61 73  List;.        as
149ef 73 65 72 74 28 20 28 70 45 78 70 72 2d 3e 69 54  sert( (pExpr->iT
149f0 61 62 6c 65 26 30 78 30 30 30 30 46 46 46 46 29  able&0x0000FFFF)
149f1 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20  ==pExpr->iTable 
149f2 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73  );.        if( s
149f3 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61  qlite3Select(pPa
149f4 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 53 65 6c  rse, pExpr->pSel
149f5 65 63 74 2c 20 53 52 54 5f 53 65 74 2c 20 69 50  ect, SRT_Set, iP
149f6 61 72 6d 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29  arm, 0, 0, 0, 0)
149f7 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65   ){.          re
149f8 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a  turn;.        }.
149f9 20 20 20 20 20 20 20 20 70 45 4c 69 73 74 20 3d          pEList =
149fa 20 70 45 78 70 72 2d 3e 70 53 65 6c 65 63 74 2d   pExpr->pSelect-
149fb 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20 20 20 20  >pEList;.       
149fc 20 69 66 28 20 70 45 4c 69 73 74 20 26 26 20 70   if( pEList && p
149fd 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 30 20 29  EList->nExpr>0 )
149fe 7b 20 0a 20 20 20 20 20 20 20 20 20 20 6b 65 79  { .          key
149ff 49 6e 66 6f 2e 61 43 6f 6c 6c 5b 30 5d 20 3d 20  Info.aColl[0] = 
14a00 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d  sqlite3BinaryCom
14a01 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 50 61 72  pareCollSeq(pPar
14a02 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  se, pExpr->pLeft
14a03 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
14a04 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78  pEList->a[0].pEx
14a05 70 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  pr);.        }. 
14a06 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70       }else if( p
14a07 45 78 70 72 2d 3e 70 4c 69 73 74 20 29 7b 0a 20  Expr->pList ){. 
14a08 20 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20 32         /* Case 2
14a09 3a 20 20 20 20 20 65 78 70 72 20 49 4e 20 28 65  :     expr IN (e
14a0a 78 70 72 6c 69 73 74 29 0a 20 20 20 20 20 20 20  xprlist).       
14a0b 20 2a 2a 0a 09 2a 2a 20 46 6f 72 20 65 61 63 68   **..** For each
14a0c 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 62 75 69   expression, bui
14a0d 6c 64 20 61 6e 20 69 6e 64 65 78 20 6b 65 79 20  ld an index key 
14a0e 66 72 6f 6d 20 74 68 65 20 65 76 61 6c 75 61 74  from the evaluat
14a0f 69 6f 6e 20 61 6e 64 0a 20 20 20 20 20 20 20 20  ion and.        
14a10 2a 2a 20 73 74 6f 72 65 20 69 74 20 69 6e 20 74  ** store it in t
14a11 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  he temporary tab
14a12 6c 65 2e 20 49 66 20 3c 65 78 70 72 3e 20 69 73  le. If <expr> is
14a13 20 61 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20   a column, then 
14a14 75 73 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74  use.        ** t
14a15 68 61 74 20 63 6f 6c 75 6d 6e 73 20 61 66 66 69  hat columns affi
14a16 6e 69 74 79 20 77 68 65 6e 20 62 75 69 6c 64 69  nity when buildi
14a17 6e 67 20 69 6e 64 65 78 20 6b 65 79 73 2e 20 49  ng index keys. I
14a18 66 20 3c 65 78 70 72 3e 20 69 73 20 6e 6f 74 0a  f <expr> is not.
14a19 20 20 20 20 20 20 20 20 2a 2a 20 61 20 63 6f 6c          ** a col
14a1a 75 6d 6e 2c 20 75 73 65 20 6e 75 6d 65 72 69 63  umn, use numeric
14a1b 20 61 66 66 69 6e 69 74 79 2e 0a 20 20 20 20 20   affinity..     
14a1c 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e     */.        in
14a1d 74 20 69 3b 0a 20 20 20 20 20 20 20 20 45 78 70  t i;.        Exp
14a1e 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70  rList *pList = p
14a1f 45 78 70 72 2d 3e 70 4c 69 73 74 3b 0a 20 20 20  Expr->pList;.   
14a20 20 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72       struct Expr
14a21 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
14a22 3b 0a 0a 20 20 20 20 20 20 20 20 69 66 28 20 21  ;..        if( !
14a23 61 66 66 69 6e 69 74 79 20 29 7b 0a 20 20 20 20  affinity ){.    
14a24 20 20 20 20 20 20 61 66 66 69 6e 69 74 79 20 3d        affinity =
14a25 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45   SQLITE_AFF_NONE
14a26 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
14a27 20 20 20 20 6b 65 79 49 6e 66 6f 2e 61 43 6f 6c      keyInfo.aCol
14a28 6c 5b 30 5d 20 3d 20 70 45 78 70 72 2d 3e 70 4c  l[0] = pExpr->pL
14a29 65 66 74 2d 3e 70 43 6f 6c 6c 3b 0a 0a 20 20 20  eft->pColl;..   
14a2a 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 68 72       /* Loop thr
14a2b 6f 75 67 68 20 65 61 63 68 20 65 78 70 72 65 73  ough each expres
14a2c 73 69 6f 6e 20 69 6e 20 3c 65 78 70 72 6c 69 73  sion in <exprlis
14a2d 74 3e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 66  t>. */.        f
14a2e 6f 72 28 69 3d 70 4c 69 73 74 2d 3e 6e 45 78 70  or(i=pList->nExp
14a2f 72 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e  r, pItem=pList->
14a30 61 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74  a; i>0; i--, pIt
14a31 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20  em++){.         
14a32 20 45 78 70 72 20 2a 70 45 32 20 3d 20 70 49 74   Expr *pE2 = pIt
14a33 65 6d 2d 3e 70 45 78 70 72 3b 0a 0a 20 20 20 20  em->pExpr;..    
14a34 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20        /* If the 
14a35 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6e 6f  expression is no
14a36 74 20 63 6f 6e 73 74 61 6e 74 20 74 68 65 6e 20  t constant then 
14a37 77 65 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f 0a  we will need to.
14a38 20 20 20 20 20 20 20 20 20 20 2a 2a 20 64 69 73            ** dis
14a39 61 62 6c 65 20 74 68 65 20 74 65 73 74 20 74 68  able the test th
14a3a 61 74 20 77 61 73 20 67 65 6e 65 72 61 74 65 64  at was generated
14a3b 20 61 62 6f 76 65 20 74 68 61 74 20 6d 61 6b 65   above that make
14a3c 73 20 73 75 72 65 0a 20 20 20 20 20 20 20 20 20  s sure.         
14a3d 20 2a 2a 20 74 68 69 73 20 63 6f 64 65 20 6f 6e   ** this code on
14a3e 6c 79 20 65 78 65 63 75 74 65 73 20 6f 6e 63 65  ly executes once
14a3f 2e 20 20 42 65 63 61 75 73 65 20 66 6f 72 20 61  .  Because for a
14a40 20 6e 6f 6e 2d 63 6f 6e 73 74 61 6e 74 0a 20 20   non-constant.  
14a41 20 20 20 20 20 20 20 20 2a 2a 20 65 78 70 72 65          ** expre
14a42 73 73 69 6f 6e 20 77 65 20 6e 65 65 64 20 74 6f  ssion we need to
14a43 20 72 65 72 75 6e 20 74 68 69 73 20 63 6f 64 65   rerun this code
14a44 20 65 61 63 68 20 74 69 6d 65 2e 0a 20 20 20 20   each time..    
14a45 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
14a46 20 20 20 69 66 28 20 74 65 73 74 41 64 64 72 3e     if( testAddr>
14a47 30 20 26 26 20 21 73 71 6c 69 74 65 33 45 78 70  0 && !sqlite3Exp
14a48 72 49 73 43 6f 6e 73 74 61 6e 74 28 70 45 32 29  rIsConstant(pE2)
14a49 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
14a4a 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
14a4b 65 54 6f 4e 6f 6f 70 28 76 2c 20 74 65 73 74 41  eToNoop(v, testA
14a4c 64 64 72 2d 31 2c 20 33 29 3b 0a 20 20 20 20 20  ddr-1, 3);.     
14a4d 20 20 20 20 20 20 20 74 65 73 74 41 64 64 72 20         testAddr 
14a4e 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  = 0;.          }
14a4f 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45  ..          /* E
14a50 76 61 6c 75 61 74 65 20 74 68 65 20 65 78 70 72  valuate the expr
14a51 65 73 73 69 6f 6e 20 61 6e 64 20 69 6e 73 65 72  ession and inser
14a52 74 20 69 74 20 69 6e 74 6f 20 74 68 65 20 74 65  t it into the te
14a53 6d 70 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  mp table */.    
14a54 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
14a55 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45  rCode(pParse, pE
14a56 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  2);.          sq
14a57 6c 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20  lite3VdbeOp3(v, 
14a58 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 31  OP_MakeRecord, 1
14a59 2c 20 30 2c 20 26 61 66 66 69 6e 69 74 79 2c 20  , 0, &affinity, 
14a5a 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  1);.          sq
14a5b 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
14a5c 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20  , OP_IdxInsert, 
14a5d 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 30  pExpr->iTable, 0
14a5e 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
14a5f 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
14a60 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 76  e3VdbeChangeP3(v
14a61 2c 20 61 64 64 72 2c 20 28 76 6f 69 64 20 2a 29  , addr, (void *)
14a62 26 6b 65 79 49 6e 66 6f 2c 20 50 33 5f 4b 45 59  &keyInfo, P3_KEY
14a63 49 4e 46 4f 29 3b 0a 20 20 20 20 20 20 62 72 65  INFO);.      bre
14a64 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63  ak;.    }..    c
14a65 61 73 65 20 54 4b 5f 45 58 49 53 54 53 3a 0a 20  ase TK_EXISTS:. 
14a66 20 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43     case TK_SELEC
14a67 54 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68  T: {.      /* Th
14a68 69 73 20 68 61 73 20 74 6f 20 62 65 20 61 20 73  is has to be a s
14a69 63 61 6c 61 72 20 53 45 4c 45 43 54 2e 20 20 47  calar SELECT.  G
14a6a 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
14a6b 70 75 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a  put the.      **
14a6c 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 73   value of this s
14a6d 65 6c 65 63 74 20 69 6e 20 61 20 6d 65 6d 6f 72  elect in a memor
14a6e 79 20 63 65 6c 6c 20 61 6e 64 20 72 65 63 6f 72  y cell and recor
14a6f 64 20 74 68 65 20 6e 75 6d 62 65 72 0a 20 20 20  d the number.   
14a70 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 6d 65 6d     ** of the mem
14a71 6f 72 79 20 63 65 6c 6c 20 69 6e 20 69 43 6f 6c  ory cell in iCol
14a72 75 6d 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  umn..      */.  
14a73 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74      static const
14a74 20 54 6f 6b 65 6e 20 6f 6e 65 20 3d 20 7b 20 28   Token one = { (
14a75 75 38 2a 29 22 31 22 2c 20 30 2c 20 31 20 7d 3b  u8*)"1", 0, 1 };
14a76 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70  .      Select *p
14a77 53 65 6c 3b 0a 20 20 20 20 20 20 69 6e 74 20 69  Sel;.      int i
14a78 4d 65 6d 3b 0a 20 20 20 20 20 20 69 6e 74 20 73  Mem;.      int s
14a79 6f 70 3b 0a 0a 20 20 20 20 20 20 70 45 78 70 72  op;..      pExpr
14a7a 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 69 4d 65 6d  ->iColumn = iMem
14a7b 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b   = pParse->nMem+
14a7c 2b 3b 0a 20 20 20 20 20 20 70 53 65 6c 20 3d 20  +;.      pSel = 
14a7d 70 45 78 70 72 2d 3e 70 53 65 6c 65 63 74 3b 0a  pExpr->pSelect;.
14a7e 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d        if( pExpr-
14a7f 3e 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 29  >op==TK_SELECT )
14a80 7b 0a 20 20 20 20 20 20 20 20 73 6f 70 20 3d 20  {.        sop = 
14a81 53 52 54 5f 4d 65 6d 3b 0a 20 20 20 20 20 20 20  SRT_Mem;.       
14a82 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
14a83 70 28 76 2c 20 4f 50 5f 4d 65 6d 4e 75 6c 6c 2c  p(v, OP_MemNull,
14a84 20 69 4d 65 6d 2c 20 30 29 3b 0a 20 20 20 20 20   iMem, 0);.     
14a85 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
14a86 76 2c 20 22 23 20 49 6e 69 74 20 73 75 62 71 75  v, "# Init subqu
14a87 65 72 79 20 72 65 73 75 6c 74 22 29 29 3b 0a 20  ery result"));. 
14a88 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
14a89 20 20 20 20 73 6f 70 20 3d 20 53 52 54 5f 45 78      sop = SRT_Ex
14a8a 69 73 74 73 3b 0a 20 20 20 20 20 20 20 20 73 71  ists;.        sq
14a8b 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
14a8c 2c 20 4f 50 5f 4d 65 6d 49 6e 74 2c 20 30 2c 20  , OP_MemInt, 0, 
14a8d 69 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 56  iMem);.        V
14a8e 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
14a8f 23 20 49 6e 69 74 20 45 58 49 53 54 53 20 72 65  # Init EXISTS re
14a90 73 75 6c 74 22 29 29 3b 0a 20 20 20 20 20 20 7d  sult"));.      }
14a91 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
14a92 70 72 44 65 6c 65 74 65 28 70 53 65 6c 2d 3e 70  prDelete(pSel->p
14a93 4c 69 6d 69 74 29 3b 0a 20 20 20 20 20 20 70 53  Limit);.      pS
14a94 65 6c 2d 3e 70 4c 69 6d 69 74 20 3d 20 73 71 6c  el->pLimit = sql
14a95 69 74 65 33 45 78 70 72 28 54 4b 5f 49 4e 54 45  ite3Expr(TK_INTE
14a96 47 45 52 2c 20 30 2c 20 30 2c 20 26 6f 6e 65 29  GER, 0, 0, &one)
14a97 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  ;.      if( sqli
14a98 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65  te3Select(pParse
14a99 2c 20 70 53 65 6c 2c 20 73 6f 70 2c 20 69 4d 65  , pSel, sop, iMe
14a9a 6d 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 20 29  m, 0, 0, 0, 0) )
14a9b 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
14a9c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
14a9d 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
14a9e 0a 0a 20 20 69 66 28 20 74 65 73 74 41 64 64 72  ..  if( testAddr
14a9f 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
14aa0 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 74  dbeJumpHere(v, t
14aa1 65 73 74 41 64 64 72 29 3b 0a 20 20 7d 0a 0a 20  estAddr);.  }.. 
14aa2 20 72 65 74 75 72 6e 3b 0a 7d 0a 23 65 6e 64 69   return;.}.#endi
14aa3 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
14aa4 5f 53 55 42 51 55 45 52 59 20 2a 2f 0a 0a 2f 2a  _SUBQUERY */../*
14aa5 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 6e 20  .** Generate an 
14aa6 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 61 74  instruction that
14aa7 20 77 69 6c 6c 20 70 75 74 20 74 68 65 20 69 6e   will put the in
14aa8 74 65 67 65 72 20 64 65 73 63 72 69 62 65 20 62  teger describe b
14aa9 79 0a 2a 2a 20 74 65 78 74 20 7a 5b 30 2e 2e 6e  y.** text z[0..n
14aaa 2d 31 5d 20 6f 6e 20 74 68 65 20 73 74 61 63 6b  -1] on the stack
14aab 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
14aac 20 63 6f 64 65 49 6e 74 65 67 65 72 28 56 64 62   codeInteger(Vdb
14aad 65 20 2a 76 2c 20 63 6f 6e 73 74 20 63 68 61 72  e *v, const char
14aae 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 61   *z, int n){.  a
14aaf 73 73 65 72 74 28 20 7a 20 7c 7c 20 73 71 6c 69  ssert( z || sqli
14ab0 74 65 33 4d 61 6c 6c 6f 63 46 61 69 6c 65 64 28  te3MallocFailed(
14ab1 29 20 29 3b 0a 20 20 69 66 28 20 7a 20 29 7b 0a  ) );.  if( z ){.
14ab2 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 69      int i;.    i
14ab3 66 28 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74  f( sqlite3GetInt
14ab4 33 32 28 7a 2c 20 26 69 29 20 29 7b 0a 20 20 20  32(z, &i) ){.   
14ab5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
14ab6 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  dOp(v, OP_Intege
14ab7 72 2c 20 69 2c 20 30 29 3b 0a 20 20 20 20 7d 65  r, i, 0);.    }e
14ab8 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 46  lse if( sqlite3F
14ab9 69 74 73 49 6e 36 34 42 69 74 73 28 7a 29 20 29  itsIn64Bits(z) )
14aba 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
14abb 64 62 65 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 74  dbeOp3(v, OP_Int
14abc 36 34 2c 20 30 2c 20 30 2c 20 7a 2c 20 6e 29 3b  64, 0, 0, z, n);
14abd 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
14abe 20 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33    sqlite3VdbeOp3
14abf 28 76 2c 20 4f 50 5f 52 65 61 6c 2c 20 30 2c 20  (v, OP_Real, 0, 
14ac0 30 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a  0, z, n);.    }.
14ac1 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65    }.}.../*.** Ge
14ac2 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74  nerate code that
14ac3 20 77 69 6c 6c 20 65 78 74 72 61 63 74 20 74 68   will extract th
14ac4 65 20 69 43 6f 6c 75 6d 6e 2d 74 68 20 63 6f 6c  e iColumn-th col
14ac5 75 6d 6e 20 66 72 6f 6d 0a 2a 2a 20 74 61 62 6c  umn from.** tabl
14ac6 65 20 70 54 61 62 20 61 6e 64 20 70 75 73 68 20  e pTab and push 
14ac7 74 68 61 74 20 63 6f 6c 75 6d 6e 20 76 61 6c 75  that column valu
14ac8 65 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 2e 20  e on the stack. 
14ac9 20 54 68 65 72 65 0a 2a 2a 20 69 73 20 61 6e 20   There.** is an 
14aca 6f 70 65 6e 20 63 75 72 73 6f 72 20 74 6f 20 70  open cursor to p
14acb 54 61 62 20 69 6e 20 69 54 61 62 6c 65 2e 20 20  Tab in iTable.  
14acc 49 66 20 69 43 6f 6c 75 6d 6e 3c 30 20 74 68 65  If iColumn<0 the
14acd 6e 0a 2a 2a 20 63 6f 64 65 20 69 73 20 67 65 6e  n.** code is gen
14ace 65 72 61 74 65 64 20 74 68 61 74 20 65 78 74 72  erated that extr
14acf 61 63 74 73 20 74 68 65 20 72 6f 77 69 64 2e 0a  acts the rowid..
14ad0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
14ad1 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
14ad2 70 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28  prCodeGetColumn(
14ad3 56 64 62 65 20 2a 76 2c 20 54 61 62 6c 65 20 2a  Vdbe *v, Table *
14ad4 70 54 61 62 2c 20 69 6e 74 20 69 43 6f 6c 75 6d  pTab, int iColum
14ad5 6e 2c 20 69 6e 74 20 69 54 61 62 6c 65 29 7b 0a  n, int iTable){.
14ad6 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3c 30 20    if( iColumn<0 
14ad7 29 7b 0a 20 20 20 20 69 6e 74 20 6f 70 20 3d 20  ){.    int op = 
14ad8 28 70 54 61 62 20 26 26 20 49 73 56 69 72 74 75  (pTab && IsVirtu
14ad9 61 6c 28 70 54 61 62 29 29 20 3f 20 4f 50 5f 56  al(pTab)) ? OP_V
14ada 52 6f 77 69 64 20 3a 20 4f 50 5f 52 6f 77 69 64  Rowid : OP_Rowid
14adb 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
14adc 65 41 64 64 4f 70 28 76 2c 20 6f 70 2c 20 69 54  eAddOp(v, op, iT
14add 61 62 6c 65 2c 20 30 29 3b 0a 20 20 7d 65 6c 73  able, 0);.  }els
14ade 65 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29 7b  e if( pTab==0 ){
14adf 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
14ae0 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6f 6c 75  AddOp(v, OP_Colu
14ae1 6d 6e 2c 20 69 54 61 62 6c 65 2c 20 69 43 6f 6c  mn, iTable, iCol
14ae2 75 6d 6e 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  umn);.  }else{. 
14ae3 20 20 20 69 6e 74 20 6f 70 20 3d 20 49 73 56 69     int op = IsVi
14ae4 72 74 75 61 6c 28 70 54 61 62 29 20 3f 20 4f 50  rtual(pTab) ? OP
14ae5 5f 56 43 6f 6c 75 6d 6e 20 3a 20 4f 50 5f 43 6f  _VColumn : OP_Co
14ae6 6c 75 6d 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65  lumn;.    sqlite
14ae7 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 6f 70  3VdbeAddOp(v, op
14ae8 2c 20 69 54 61 62 6c 65 2c 20 69 43 6f 6c 75 6d  , iTable, iColum
14ae9 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43  n);.    sqlite3C
14aea 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 76 2c 20  olumnDefault(v, 
14aeb 70 54 61 62 2c 20 69 43 6f 6c 75 6d 6e 29 3b 0a  pTab, iColumn);.
14aec 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
14aed 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49  MIT_FLOATING_POI
14aee 4e 54 0a 20 20 20 20 69 66 28 20 70 54 61 62 2d  NT.    if( pTab-
14aef 3e 61 43 6f 6c 5b 69 43 6f 6c 75 6d 6e 5d 2e 61  >aCol[iColumn].a
14af0 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f  ffinity==SQLITE_
14af1 41 46 46 5f 52 45 41 4c 20 29 7b 0a 20 20 20 20  AFF_REAL ){.    
14af2 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
14af3 4f 70 28 76 2c 20 4f 50 5f 52 65 61 6c 41 66 66  Op(v, OP_RealAff
14af4 69 6e 69 74 79 2c 20 30 2c 20 30 29 3b 0a 20 20  inity, 0, 0);.  
14af5 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d    }.#endif.  }.}
14af6 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
14af7 20 63 6f 64 65 20 69 6e 74 6f 20 74 68 65 20 63   code into the c
14af8 75 72 72 65 6e 74 20 56 64 62 65 20 74 6f 20 65  urrent Vdbe to e
14af9 76 61 6c 75 61 74 65 20 74 68 65 20 67 69 76 65  valuate the give
14afa 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20  n.** expression 
14afb 61 6e 64 20 6c 65 61 76 65 20 74 68 65 20 72 65  and leave the re
14afc 73 75 6c 74 20 6f 6e 20 74 68 65 20 74 6f 70 20  sult on the top 
14afd 6f 66 20 73 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20  of stack..**.** 
14afe 54 68 69 73 20 63 6f 64 65 20 64 65 70 65 6e 64  This code depend
14aff 73 20 6f 6e 20 74 68 65 20 66 61 63 74 20 74 68  s on the fact th
14b00 61 74 20 63 65 72 74 61 69 6e 20 74 6f 6b 65 6e  at certain token
14b01 20 76 61 6c 75 65 73 20 28 65 78 3a 20 54 4b 5f   values (ex: TK_
14b02 45 51 29 0a 2a 2a 20 61 72 65 20 74 68 65 20 73  EQ).** are the s
14b03 61 6d 65 20 61 73 20 6f 70 63 6f 64 65 20 76 61  ame as opcode va
14b04 6c 75 65 73 20 28 65 78 3a 20 4f 50 5f 45 71 29  lues (ex: OP_Eq)
14b05 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20   that implement 
14b06 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
14b07 67 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2e 20  g.** operation. 
14b08 20 53 70 65 63 69 61 6c 20 63 6f 6d 6d 65 6e 74   Special comment
14b09 73 20 69 6e 20 76 64 62 65 2e 63 20 61 6e 64 20  s in vdbe.c and 
14b0a 74 68 65 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77  the mkopcodeh.aw
14b0b 6b 20 73 63 72 69 70 74 20 69 6e 0a 2a 2a 20 74  k script in.** t
14b0c 68 65 20 6d 61 6b 65 20 70 72 6f 63 65 73 73 20  he make process 
14b0d 63 61 75 73 65 20 74 68 65 73 65 20 76 61 6c 75  cause these valu
14b0e 65 73 20 74 6f 20 61 6c 69 67 6e 2e 20 20 41 73  es to align.  As
14b0f 73 65 72 74 28 29 73 20 69 6e 20 74 68 65 20 63  sert()s in the c
14b10 6f 64 65 0a 2a 2a 20 62 65 6c 6f 77 20 76 65 72  ode.** below ver
14b11 69 66 79 20 74 68 61 74 20 74 68 65 20 6e 75 6d  ify that the num
14b12 62 65 72 73 20 61 72 65 20 61 6c 69 67 6e 65 64  bers are aligned
14b13 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2f 0a 53   correctly..*/.S
14b14 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
14b15 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  id sqlite3ExprCo
14b16 64 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  de(Parse *pParse
14b17 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a  , Expr *pExpr){.
14b18 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
14b19 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74  se->pVdbe;.  int
14b1a 20 6f 70 3b 0a 20 20 69 6e 74 20 73 74 61 63 6b   op;.  int stack
14b1b 43 68 6e 67 20 3d 20 31 3b 20 20 20 20 2f 2a 20  Chng = 1;    /* 
14b1c 41 6d 6f 75 6e 74 20 6f 66 20 63 68 61 6e 67 65  Amount of change
14b1d 20 74 6f 20 73 74 61 63 6b 20 64 65 70 74 68 20   to stack depth 
14b1e 2a 2f 0a 0a 20 20 69 66 28 20 76 3d 3d 30 20 29  */..  if( v==0 )
14b1f 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70   return;.  if( p
14b20 45 78 70 72 3d 3d 30 20 29 7b 0a 20 20 20 20 73  Expr==0 ){.    s
14b21 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
14b22 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 30  v, OP_Null, 0, 0
14b23 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
14b24 20 7d 0a 20 20 6f 70 20 3d 20 70 45 78 70 72 2d   }.  op = pExpr-
14b25 3e 6f 70 3b 0a 20 20 73 77 69 74 63 68 28 20 6f  >op;.  switch( o
14b26 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b  p ){.    case TK
14b27 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20  _AGG_COLUMN: {. 
14b28 20 20 20 20 20 41 67 67 49 6e 66 6f 20 2a 70 41       AggInfo *pA
14b29 67 67 49 6e 66 6f 20 3d 20 70 45 78 70 72 2d 3e  ggInfo = pExpr->
14b2a 70 41 67 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20  pAggInfo;.      
14b2b 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 63  struct AggInfo_c
14b2c 6f 6c 20 2a 70 43 6f 6c 20 3d 20 26 70 41 67 67  ol *pCol = &pAgg
14b2d 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 70 45 78 70 72  Info->aCol[pExpr
14b2e 2d 3e 69 41 67 67 5d 3b 0a 20 20 20 20 20 20 69  ->iAgg];.      i
14b2f 66 28 20 21 70 41 67 67 49 6e 66 6f 2d 3e 64 69  f( !pAggInfo->di
14b30 72 65 63 74 4d 6f 64 65 20 29 7b 0a 20 20 20 20  rectMode ){.    
14b31 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
14b32 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 4c 6f  ddOp(v, OP_MemLo
14b33 61 64 2c 20 70 43 6f 6c 2d 3e 69 4d 65 6d 2c 20  ad, pCol->iMem, 
14b34 30 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  0);.        brea
14b35 6b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  k;.      }else i
14b36 66 28 20 70 41 67 67 49 6e 66 6f 2d 3e 75 73 65  f( pAggInfo->use
14b37 53 6f 72 74 69 6e 67 49 64 78 20 29 7b 0a 20 20  SortingIdx ){.  
14b38 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
14b39 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6f 6c  eAddOp(v, OP_Col
14b3a 75 6d 6e 2c 20 70 41 67 67 49 6e 66 6f 2d 3e 73  umn, pAggInfo->s
14b3b 6f 72 74 69 6e 67 49 64 78 2c 0a 20 20 20 20 20  ortingIdx,.     
14b3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14b3d 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69           pCol->i
14b3e 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 29 3b 0a 20  SorterColumn);. 
14b3f 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
14b40 20 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 4f      }.      /* O
14b41 74 68 65 72 77 69 73 65 2c 20 66 61 6c 6c 20 74  therwise, fall t
14b42 68 72 75 20 69 6e 74 6f 20 74 68 65 20 54 4b 5f  hru into the TK_
14b43 43 4f 4c 55 4d 4e 20 63 61 73 65 20 2a 2f 0a 20  COLUMN case */. 
14b44 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b     }.    case TK
14b45 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20 20  _COLUMN: {.     
14b46 20 69 66 28 20 70 45 78 70 72 2d 3e 69 54 61 62   if( pExpr->iTab
14b47 6c 65 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20  le<0 ){.        
14b48 2f 2a 20 54 68 69 73 20 6f 6e 6c 79 20 68 61 70  /* This only hap
14b49 70 65 6e 73 20 77 68 65 6e 20 63 6f 64 69 6e 67  pens when coding
14b4a 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e   check constrain
14b4b 74 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73  ts */.        as
14b4c 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 63 6b  sert( pParse->ck
14b4d 4f 66 66 73 65 74 3e 30 20 29 3b 0a 20 20 20 20  Offset>0 );.    
14b4e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
14b4f 64 64 4f 70 28 76 2c 20 4f 50 5f 44 75 70 2c 20  ddOp(v, OP_Dup, 
14b50 70 50 61 72 73 65 2d 3e 63 6b 4f 66 66 73 65 74  pParse->ckOffset
14b51 2d 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 2d  -pExpr->iColumn-
14b52 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d 65 6c  1, 1);.      }el
14b53 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  se{.        sqli
14b54 74 65 33 45 78 70 72 43 6f 64 65 47 65 74 43 6f  te3ExprCodeGetCo
14b55 6c 75 6d 6e 28 76 2c 20 70 45 78 70 72 2d 3e 70  lumn(v, pExpr->p
14b56 54 61 62 2c 20 70 45 78 70 72 2d 3e 69 43 6f 6c  Tab, pExpr->iCol
14b57 75 6d 6e 2c 20 70 45 78 70 72 2d 3e 69 54 61 62  umn, pExpr->iTab
14b58 6c 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  le);.      }.   
14b59 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
14b5a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 54 45      case TK_INTE
14b5b 47 45 52 3a 20 7b 0a 20 20 20 20 20 20 63 6f 64  GER: {.      cod
14b5c 65 49 6e 74 65 67 65 72 28 76 2c 20 28 63 68 61  eInteger(v, (cha
14b5d 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e  r*)pExpr->token.
14b5e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e  z, pExpr->token.
14b5f 6e 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  n);.      break;
14b60 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
14b61 54 4b 5f 46 4c 4f 41 54 3a 0a 20 20 20 20 63 61  TK_FLOAT:.    ca
14b62 73 65 20 54 4b 5f 53 54 52 49 4e 47 3a 20 7b 0a  se TK_STRING: {.
14b63 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
14b64 5f 46 4c 4f 41 54 3d 3d 4f 50 5f 52 65 61 6c 20  _FLOAT==OP_Real 
14b65 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
14b66 20 54 4b 5f 53 54 52 49 4e 47 3d 3d 4f 50 5f 53   TK_STRING==OP_S
14b67 74 72 69 6e 67 38 20 29 3b 0a 20 20 20 20 20 20  tring8 );.      
14b68 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 45 78  sqlite3DequoteEx
14b69 70 72 28 70 45 78 70 72 29 3b 0a 20 20 20 20 20  pr(pExpr);.     
14b6a 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28   sqlite3VdbeOp3(
14b6b 76 2c 20 6f 70 2c 20 30 2c 20 30 2c 20 28 63 68  v, op, 0, 0, (ch
14b6c 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e  ar*)pExpr->token
14b6d 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e  .z, pExpr->token
14b6e 2e 6e 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  .n);.      break
14b6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
14b70 20 54 4b 5f 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20   TK_NULL: {.    
14b71 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
14b72 4f 70 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30  Op(v, OP_Null, 0
14b73 2c 20 30 29 3b 0a 20 20 20 20 20 20 62 72 65 61  , 0);.      brea
14b74 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66  k;.    }.#ifndef
14b75 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f   SQLITE_OMIT_BLO
14b76 42 5f 4c 49 54 45 52 41 4c 0a 20 20 20 20 63 61  B_LITERAL.    ca
14b77 73 65 20 54 4b 5f 42 4c 4f 42 3a 20 7b 0a 20 20  se TK_BLOB: {.  
14b78 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 20      int n;.     
14b79 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b 0a   const char *z;.
14b7a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
14b7b 5f 42 4c 4f 42 3d 3d 4f 50 5f 48 65 78 42 6c 6f  _BLOB==OP_HexBlo
14b7c 62 20 29 3b 0a 20 20 20 20 20 20 6e 20 3d 20 70  b );.      n = p
14b7d 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 20 2d 20  Expr->token.n - 
14b7e 33 3b 0a 20 20 20 20 20 20 7a 20 3d 20 28 63 68  3;.      z = (ch
14b7f 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e  ar*)pExpr->token
14b80 2e 7a 20 2b 20 32 3b 0a 20 20 20 20 20 20 61 73  .z + 2;.      as
14b81 73 65 72 74 28 20 6e 3e 3d 30 20 29 3b 0a 20 20  sert( n>=0 );.  
14b82 20 20 20 20 69 66 28 20 6e 3d 3d 30 20 29 7b 0a      if( n==0 ){.
14b83 20 20 20 20 20 20 20 20 7a 20 3d 20 22 22 3b 0a          z = "";.
14b84 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
14b85 6c 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20  lite3VdbeOp3(v, 
14b86 6f 70 2c 20 30 2c 20 30 2c 20 7a 2c 20 6e 29 3b  op, 0, 0, z, n);
14b87 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
14b88 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63    }.#endif.    c
14b89 61 73 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a  ase TK_VARIABLE:
14b8a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   {.      sqlite3
14b8b 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
14b8c 56 61 72 69 61 62 6c 65 2c 20 70 45 78 70 72 2d  Variable, pExpr-
14b8d 3e 69 54 61 62 6c 65 2c 20 30 29 3b 0a 20 20 20  >iTable, 0);.   
14b8e 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 74 6f     if( pExpr->to
14b8f 6b 65 6e 2e 6e 3e 31 20 29 7b 0a 20 20 20 20 20  ken.n>1 ){.     
14b90 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
14b91 61 6e 67 65 50 33 28 76 2c 20 2d 31 2c 20 28 63  angeP3(v, -1, (c
14b92 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65  har*)pExpr->toke
14b93 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b 65  n.z, pExpr->toke
14b94 6e 2e 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  n.n);.      }.  
14b95 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
14b96 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 45 47  .    case TK_REG
14b97 49 53 54 45 52 3a 20 7b 0a 20 20 20 20 20 20 73  ISTER: {.      s
14b98 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
14b99 76 2c 20 4f 50 5f 4d 65 6d 4c 6f 61 64 2c 20 70  v, OP_MemLoad, p
14b9a 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 30 29  Expr->iTable, 0)
14b9b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
14b9c 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c     }.#ifndef SQL
14b9d 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 20 20  ITE_OMIT_CAST.  
14b9e 20 20 63 61 73 65 20 54 4b 5f 43 41 53 54 3a 20    case TK_CAST: 
14b9f 7b 0a 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65  {.      /* Expre
14ba0 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 66 6f  ssions of the fo
14ba1 72 6d 3a 20 20 20 43 41 53 54 28 70 4c 65 66 74  rm:   CAST(pLeft
14ba2 20 41 53 20 74 6f 6b 65 6e 29 20 2a 2f 0a 20 20   AS token) */.  
14ba3 20 20 20 20 69 6e 74 20 61 66 66 2c 20 74 6f 5f      int aff, to_
14ba4 6f 70 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  op;.      sqlite
14ba5 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
14ba6 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b  , pExpr->pLeft);
14ba7 0a 20 20 20 20 20 20 61 66 66 20 3d 20 73 71 6c  .      aff = sql
14ba8 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 70 65  ite3AffinityType
14ba9 28 26 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 29 3b  (&pExpr->token);
14baa 0a 20 20 20 20 20 20 74 6f 5f 6f 70 20 3d 20 61  .      to_op = a
14bab 66 66 20 2d 20 53 51 4c 49 54 45 5f 41 46 46 5f  ff - SQLITE_AFF_
14bac 54 45 58 54 20 2b 20 4f 50 5f 54 6f 54 65 78 74  TEXT + OP_ToText
14bad 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
14bae 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 54 65 78 74  to_op==OP_ToText
14baf 20 20 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49      || aff!=SQLI
14bb0 54 45 5f 41 46 46 5f 54 45 58 54 20 20 20 20 29  TE_AFF_TEXT    )
14bb1 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
14bb2 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 42 6c 6f 62  to_op==OP_ToBlob
14bb3 20 20 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49      || aff!=SQLI
14bb4 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 20 20 20 29  TE_AFF_NONE    )
14bb5 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
14bb6 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 4e 75 6d 65  to_op==OP_ToNume
14bb7 72 69 63 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49  ric || aff!=SQLI
14bb8 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29  TE_AFF_NUMERIC )
14bb9 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
14bba 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 49 6e 74 20  to_op==OP_ToInt 
14bbb 20 20 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49      || aff!=SQLI
14bbc 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 29  TE_AFF_INTEGER )
14bbd 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
14bbe 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 52 65 61 6c  to_op==OP_ToReal
14bbf 20 20 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49      || aff!=SQLI
14bc0 54 45 5f 41 46 46 5f 52 45 41 4c 20 20 20 20 29  TE_AFF_REAL    )
14bc1 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
14bc2 64 62 65 41 64 64 4f 70 28 76 2c 20 74 6f 5f 6f  dbeAddOp(v, to_o
14bc3 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20  p, 0, 0);.      
14bc4 73 74 61 63 6b 43 68 6e 67 20 3d 20 30 3b 0a 20  stackChng = 0;. 
14bc5 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
14bc6 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
14bc7 54 45 5f 4f 4d 49 54 5f 43 41 53 54 20 2a 2f 0a  TE_OMIT_CAST */.
14bc8 20 20 20 20 63 61 73 65 20 54 4b 5f 4c 54 3a 0a      case TK_LT:.
14bc9 20 20 20 20 63 61 73 65 20 54 4b 5f 4c 45 3a 0a      case TK_LE:.
14bca 20 20 20 20 63 61 73 65 20 54 4b 5f 47 54 3a 0a      case TK_GT:.
14bcb 20 20 20 20 63 61 73 65 20 54 4b 5f 47 45 3a 0a      case TK_GE:.
14bcc 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 45 3a 0a      case TK_NE:.
14bcd 20 20 20 20 63 61 73 65 20 54 4b 5f 45 51 3a 20      case TK_EQ: 
14bce 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
14bcf 54 4b 5f 4c 54 3d 3d 4f 50 5f 4c 74 20 29 3b 0a  TK_LT==OP_Lt );.
14bd0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
14bd1 5f 4c 45 3d 3d 4f 50 5f 4c 65 20 29 3b 0a 20 20  _LE==OP_Le );.  
14bd2 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 47      assert( TK_G
14bd3 54 3d 3d 4f 50 5f 47 74 20 29 3b 0a 20 20 20 20  T==OP_Gt );.    
14bd4 20 20 61 73 73 65 72 74 28 20 54 4b 5f 47 45 3d    assert( TK_GE=
14bd5 3d 4f 50 5f 47 65 20 29 3b 0a 20 20 20 20 20 20  =OP_Ge );.      
14bd6 61 73 73 65 72 74 28 20 54 4b 5f 45 51 3d 3d 4f  assert( TK_EQ==O
14bd7 50 5f 45 71 20 29 3b 0a 20 20 20 20 20 20 61 73  P_Eq );.      as
14bd8 73 65 72 74 28 20 54 4b 5f 4e 45 3d 3d 4f 50 5f  sert( TK_NE==OP_
14bd9 4e 65 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  Ne );.      sqli
14bda 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72  te3ExprCode(pPar
14bdb 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  se, pExpr->pLeft
14bdc 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
14bdd 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
14bde 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29 3b   pExpr->pRight);
14bdf 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61  .      codeCompa
14be0 72 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  re(pParse, pExpr
14be1 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e  ->pLeft, pExpr->
14be2 70 52 69 67 68 74 2c 20 6f 70 2c 20 30 2c 20 30  pRight, op, 0, 0
14be3 29 3b 0a 20 20 20 20 20 20 73 74 61 63 6b 43 68  );.      stackCh
14be4 6e 67 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 62  ng = -1;.      b
14be5 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
14be6 63 61 73 65 20 54 4b 5f 41 4e 44 3a 0a 20 20 20  case TK_AND:.   
14be7 20 63 61 73 65 20 54 4b 5f 4f 52 3a 0a 20 20 20   case TK_OR:.   
14be8 20 63 61 73 65 20 54 4b 5f 50 4c 55 53 3a 0a 20   case TK_PLUS:. 
14be9 20 20 20 63 61 73 65 20 54 4b 5f 53 54 41 52 3a     case TK_STAR:
14bea 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4d 49 4e  .    case TK_MIN
14beb 55 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  US:.    case TK_
14bec 52 45 4d 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  REM:.    case TK
14bed 5f 42 49 54 41 4e 44 3a 0a 20 20 20 20 63 61 73  _BITAND:.    cas
14bee 65 20 54 4b 5f 42 49 54 4f 52 3a 0a 20 20 20 20  e TK_BITOR:.    
14bef 63 61 73 65 20 54 4b 5f 53 4c 41 53 48 3a 0a 20  case TK_SLASH:. 
14bf0 20 20 20 63 61 73 65 20 54 4b 5f 4c 53 48 49 46     case TK_LSHIF
14bf1 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52  T:.    case TK_R
14bf2 53 48 49 46 54 3a 20 0a 20 20 20 20 63 61 73 65  SHIFT: .    case
14bf3 20 54 4b 5f 43 4f 4e 43 41 54 3a 20 7b 0a 20 20   TK_CONCAT: {.  
14bf4 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 41      assert( TK_A
14bf5 4e 44 3d 3d 4f 50 5f 41 6e 64 20 29 3b 0a 20 20  ND==OP_And );.  
14bf6 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4f      assert( TK_O
14bf7 52 3d 3d 4f 50 5f 4f 72 20 29 3b 0a 20 20 20 20  R==OP_Or );.    
14bf8 20 20 61 73 73 65 72 74 28 20 54 4b 5f 50 4c 55    assert( TK_PLU
14bf9 53 3d 3d 4f 50 5f 41 64 64 20 29 3b 0a 20 20 20  S==OP_Add );.   
14bfa 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4d 49     assert( TK_MI
14bfb 4e 55 53 3d 3d 4f 50 5f 53 75 62 74 72 61 63 74  NUS==OP_Subtract
14bfc 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
14bfd 28 20 54 4b 5f 52 45 4d 3d 3d 4f 50 5f 52 65 6d  ( TK_REM==OP_Rem
14bfe 61 69 6e 64 65 72 20 29 3b 0a 20 20 20 20 20 20  ainder );.      
14bff 61 73 73 65 72 74 28 20 54 4b 5f 42 49 54 41 4e  assert( TK_BITAN
14c00 44 3d 3d 4f 50 5f 42 69 74 41 6e 64 20 29 3b 0a  D==OP_BitAnd );.
14c01 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
14c02 5f 42 49 54 4f 52 3d 3d 4f 50 5f 42 69 74 4f 72  _BITOR==OP_BitOr
14c03 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
14c04 28 20 54 4b 5f 53 4c 41 53 48 3d 3d 4f 50 5f 44  ( TK_SLASH==OP_D
14c05 69 76 69 64 65 20 29 3b 0a 20 20 20 20 20 20 61  ivide );.      a
14c06 73 73 65 72 74 28 20 54 4b 5f 4c 53 48 49 46 54  ssert( TK_LSHIFT
14c07 3d 3d 4f 50 5f 53 68 69 66 74 4c 65 66 74 20 29  ==OP_ShiftLeft )
14c08 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
14c09 54 4b 5f 52 53 48 49 46 54 3d 3d 4f 50 5f 53 68  TK_RSHIFT==OP_Sh
14c0a 69 66 74 52 69 67 68 74 20 29 3b 0a 20 20 20 20  iftRight );.    
14c0b 20 20 61 73 73 65 72 74 28 20 54 4b 5f 43 4f 4e    assert( TK_CON
14c0c 43 41 54 3d 3d 4f 50 5f 43 6f 6e 63 61 74 20 29  CAT==OP_Concat )
14c0d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
14c0e 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20  xprCode(pParse, 
14c0f 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20  pExpr->pLeft);. 
14c10 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
14c11 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45 78  Code(pParse, pEx
14c12 70 72 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 20  pr->pRight);.   
14c13 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
14c14 64 4f 70 28 76 2c 20 6f 70 2c 20 30 2c 20 30 29  dOp(v, op, 0, 0)
14c15 3b 0a 20 20 20 20 20 20 73 74 61 63 6b 43 68 6e  ;.      stackChn
14c16 67 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 62 72  g = -1;.      br
14c17 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
14c18 61 73 65 20 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b  ase TK_UMINUS: {
14c19 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c 65  .      Expr *pLe
14c1a 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66  ft = pExpr->pLef
14c1b 74 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  t;.      assert(
14c1c 20 70 4c 65 66 74 20 29 3b 0a 20 20 20 20 20 20   pLeft );.      
14c1d 69 66 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54  if( pLeft->op==T
14c1e 4b 5f 46 4c 4f 41 54 20 7c 7c 20 70 4c 65 66 74  K_FLOAT || pLeft
14c1f 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52  ->op==TK_INTEGER
14c20 20 29 7b 0a 20 20 20 20 20 20 20 20 54 6f 6b 65   ){.        Toke
14c21 6e 20 2a 70 20 3d 20 26 70 4c 65 66 74 2d 3e 74  n *p = &pLeft->t
14c22 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 63 68  oken;.        ch
14c23 61 72 20 2a 7a 20 3d 20 73 71 6c 69 74 65 33 4d  ar *z = sqlite3M
14c24 50 72 69 6e 74 66 28 22 2d 25 2e 2a 73 22 2c 20  Printf("-%.*s", 
14c25 70 2d 3e 6e 2c 20 70 2d 3e 7a 29 3b 0a 20 20 20  p->n, p->z);.   
14c26 20 20 20 20 20 69 66 28 20 70 4c 65 66 74 2d 3e       if( pLeft->
14c27 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 7b 0a  op==TK_FLOAT ){.
14c28 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
14c29 33 56 64 62 65 4f 70 33 28 76 2c 20 4f 50 5f 52  3VdbeOp3(v, OP_R
14c2a 65 61 6c 2c 20 30 2c 20 30 2c 20 7a 2c 20 70 2d  eal, 0, 0, z, p-
14c2b 3e 6e 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 7d  >n+1);.        }
14c2c 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
14c2d 63 6f 64 65 49 6e 74 65 67 65 72 28 76 2c 20 7a  codeInteger(v, z
14c2e 2c 20 70 2d 3e 6e 2b 31 29 3b 0a 20 20 20 20 20  , p->n+1);.     
14c2f 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c     }.        sql
14c30 69 74 65 46 72 65 65 28 7a 29 3b 0a 20 20 20 20  iteFree(z);.    
14c31 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
14c32 20 7d 0a 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c   }.      /* Fall
14c33 20 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 54 4b   through into TK
14c34 5f 4e 4f 54 20 2a 2f 0a 20 20 20 20 7d 0a 20 20  _NOT */.    }.  
14c35 20 20 63 61 73 65 20 54 4b 5f 42 49 54 4e 4f 54    case TK_BITNOT
14c36 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f  :.    case TK_NO
14c37 54 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72  T: {.      asser
14c38 74 28 20 54 4b 5f 42 49 54 4e 4f 54 3d 3d 4f 50  t( TK_BITNOT==OP
14c39 5f 42 69 74 4e 6f 74 20 29 3b 0a 20 20 20 20 20  _BitNot );.     
14c3a 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 4f 54 3d   assert( TK_NOT=
14c3b 3d 4f 50 5f 4e 6f 74 20 29 3b 0a 20 20 20 20 20  =OP_Not );.     
14c3c 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
14c3d 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
14c3e 70 4c 65 66 74 29 3b 0a 20 20 20 20 20 20 73 71  pLeft);.      sq
14c3f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
14c40 2c 20 6f 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20  , op, 0, 0);.   
14c41 20 20 20 73 74 61 63 6b 43 68 6e 67 20 3d 20 30     stackChng = 0
14c42 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
14c43 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b     }.    case TK
14c44 5f 49 53 4e 55 4c 4c 3a 0a 20 20 20 20 63 61 73  _ISNULL:.    cas
14c45 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 7b 0a  e TK_NOTNULL: {.
14c46 20 20 20 20 20 20 69 6e 74 20 64 65 73 74 3b 0a        int dest;.
14c47 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
14c48 5f 49 53 4e 55 4c 4c 3d 3d 4f 50 5f 49 73 4e 75  _ISNULL==OP_IsNu
14c49 6c 6c 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  ll );.      asse
14c4a 72 74 28 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3d 3d  rt( TK_NOTNULL==
14c4b 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20 20  OP_NotNull );.  
14c4c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
14c4d 64 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 74 65 67  ddOp(v, OP_Integ
14c4e 65 72 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 20  er, 1, 0);.     
14c4f 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
14c50 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
14c51 70 4c 65 66 74 29 3b 0a 20 20 20 20 20 20 64 65  pLeft);.      de
14c52 73 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  st = sqlite3Vdbe
14c53 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 20 2b  CurrentAddr(v) +
14c54 20 32 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   2;.      sqlite
14c55 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 6f 70  3VdbeAddOp(v, op
14c56 2c 20 31 2c 20 64 65 73 74 29 3b 0a 20 20 20 20  , 1, dest);.    
14c57 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
14c58 4f 70 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c  Op(v, OP_AddImm,
14c59 20 2d 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 73   -1, 0);.      s
14c5a 74 61 63 6b 43 68 6e 67 20 3d 20 30 3b 0a 20 20  tackChng = 0;.  
14c5b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
14c5c 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47  .    case TK_AGG
14c5d 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a 20 20 20  _FUNCTION: {.   
14c5e 20 20 20 41 67 67 49 6e 66 6f 20 2a 70 49 6e 66     AggInfo *pInf
14c5f 6f 20 3d 20 70 45 78 70 72 2d 3e 70 41 67 67 49  o = pExpr->pAggI
14c60 6e 66 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 70  nfo;.      if( p
14c61 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Info==0 ){.     
14c62 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
14c63 73 67 28 70 50 61 72 73 65 2c 20 22 6d 69 73 75  sg(pParse, "misu
14c64 73 65 20 6f 66 20 61 67 67 72 65 67 61 74 65 3a  se of aggregate:
14c65 20 25 54 22 2c 0a 20 20 20 20 20 20 20 20 20 20   %T",.          
14c66 20 20 26 70 45 78 70 72 2d 3e 73 70 61 6e 29 3b    &pExpr->span);
14c67 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
14c68 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
14c69 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d  eAddOp(v, OP_Mem
14c6a 4c 6f 61 64 2c 20 70 49 6e 66 6f 2d 3e 61 46 75  Load, pInfo->aFu
14c6b 6e 63 5b 70 45 78 70 72 2d 3e 69 41 67 67 5d 2e  nc[pExpr->iAgg].
14c6c 69 4d 65 6d 2c 20 30 29 3b 0a 20 20 20 20 20 20  iMem, 0);.      
14c6d 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
14c6e 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b     }.    case TK
14c6f 5f 43 4f 4e 53 54 5f 46 55 4e 43 3a 0a 20 20 20  _CONST_FUNC:.   
14c70 20 63 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f   case TK_FUNCTIO
14c71 4e 3a 20 7b 0a 20 20 20 20 20 20 45 78 70 72 4c  N: {.      ExprL
14c72 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45 78  ist *pList = pEx
14c73 70 72 2d 3e 70 4c 69 73 74 3b 0a 20 20 20 20 20  pr->pList;.     
14c74 20 69 6e 74 20 6e 45 78 70 72 20 3d 20 70 4c 69   int nExpr = pLi
14c75 73 74 20 3f 20 70 4c 69 73 74 2d 3e 6e 45 78 70  st ? pList->nExp
14c76 72 20 3a 20 30 3b 0a 20 20 20 20 20 20 46 75 6e  r : 0;.      Fun
14c77 63 44 65 66 20 2a 70 44 65 66 3b 0a 20 20 20 20  cDef *pDef;.    
14c78 20 20 69 6e 74 20 6e 49 64 3b 0a 20 20 20 20 20    int nId;.     
14c79 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 64   const char *zId
14c7a 3b 0a 20 20 20 20 20 20 69 6e 74 20 63 6f 6e 73  ;.      int cons
14c7b 74 4d 61 73 6b 20 3d 20 30 3b 0a 20 20 20 20 20  tMask = 0;.     
14c7c 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 75 38   int i;.      u8
14c7d 20 65 6e 63 20 3d 20 45 4e 43 28 70 50 61 72 73   enc = ENC(pPars
14c7e 65 2d 3e 64 62 29 3b 0a 20 20 20 20 20 20 43 6f  e->db);.      Co
14c7f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 30  llSeq *pColl = 0
14c80 3b 0a 20 20 20 20 20 20 7a 49 64 20 3d 20 28 63  ;.      zId = (c
14c81 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65  har*)pExpr->toke
14c82 6e 2e 7a 3b 0a 20 20 20 20 20 20 6e 49 64 20 3d  n.z;.      nId =
14c83 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 3b   pExpr->token.n;
14c84 0a 20 20 20 20 20 20 70 44 65 66 20 3d 20 73 71  .      pDef = sq
14c85 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f  lite3FindFunctio
14c86 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 49  n(pParse->db, zI
14c87 64 2c 20 6e 49 64 2c 20 6e 45 78 70 72 2c 20 65  d, nId, nExpr, e
14c88 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 61 73  nc, 0);.      as
14c89 73 65 72 74 28 20 70 44 65 66 21 3d 30 20 29 3b  sert( pDef!=0 );
14c8a 0a 20 20 20 20 20 20 6e 45 78 70 72 20 3d 20 73  .      nExpr = s
14c8b 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 45 78  qlite3ExprCodeEx
14c8c 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70  prList(pParse, p
14c8d 4c 69 73 74 29 3b 0a 23 69 66 6e 64 65 66 20 53  List);.#ifndef S
14c8e 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
14c8f 41 4c 54 41 42 4c 45 0a 20 20 20 20 20 20 2f 2a  ALTABLE.      /*
14c90 20 50 6f 73 73 69 62 6c 79 20 6f 76 65 72 6c 6f   Possibly overlo
14c91 61 64 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  ad the function 
14c92 69 66 20 74 68 65 20 66 69 72 73 74 20 61 72 67  if the first arg
14c93 75 6d 65 6e 74 20 69 73 0a 20 20 20 20 20 20 2a  ument is.      *
14c94 2a 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  * a virtual tabl
14c95 65 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20 20 20  e column..      
14c96 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 46 6f 72 20  **.      ** For 
14c97 69 6e 66 69 78 20 66 75 6e 63 74 69 6f 6e 73 20  infix functions 
14c98 28 4c 49 4b 45 2c 20 47 4c 4f 42 2c 20 52 45 47  (LIKE, GLOB, REG
14c99 45 58 50 2c 20 61 6e 64 20 4d 41 54 43 48 29 20  EXP, and MATCH) 
14c9a 75 73 65 20 74 68 65 0a 20 20 20 20 20 20 2a 2a  use the.      **
14c9b 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
14c9c 2c 20 6e 6f 74 20 74 68 65 20 66 69 72 73 74 2c  , not the first,
14c9d 20 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e 74   as the argument
14c9e 20 74 6f 20 74 65 73 74 20 74 6f 0a 20 20 20 20   to test to.    
14c9f 20 20 2a 2a 20 73 65 65 20 69 66 20 69 74 20 69    ** see if it i
14ca0 73 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20  s a column in a 
14ca1 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20  virtual table.  
14ca2 54 68 69 73 20 69 73 20 64 6f 6e 65 20 62 65 63  This is done bec
14ca3 61 75 73 65 0a 20 20 20 20 20 20 2a 2a 20 74 68  ause.      ** th
14ca4 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 6f  e left operand o
14ca5 66 20 69 6e 66 69 78 20 66 75 6e 63 74 69 6f 6e  f infix function
14ca6 73 20 28 74 68 65 20 6f 70 65 72 61 6e 64 20 77  s (the operand w
14ca7 65 20 77 61 6e 74 20 74 6f 0a 20 20 20 20 20 20  e want to.      
14ca8 2a 2a 20 63 6f 6e 74 72 6f 6c 20 6f 76 65 72 6c  ** control overl
14ca9 6f 61 64 69 6e 67 29 20 65 6e 64 73 20 75 70 20  oading) ends up 
14caa 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  as the second ar
14cab 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 0a 20 20  gument to the.  
14cac 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e      ** function.
14cad 20 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e    The expression
14cae 20 22 41 20 67 6c 6f 62 20 42 22 20 69 73 20 65   "A glob B" is e
14caf 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 0a 20 20  quivalent to .  
14cb0 20 20 20 20 2a 2a 20 22 67 6c 6f 62 28 42 2c 41      ** "glob(B,A
14cb1 29 2e 20 20 57 65 20 77 61 6e 74 20 74 6f 20 75  ).  We want to u
14cb2 73 65 20 74 68 65 20 41 20 69 6e 20 22 41 20 67  se the A in "A g
14cb3 6c 6f 62 20 42 22 20 74 6f 20 74 65 73 74 0a 20  lob B" to test. 
14cb4 20 20 20 20 20 2a 2a 20 66 6f 72 20 66 75 6e 63       ** for func
14cb5 74 69 6f 6e 20 6f 76 65 72 6c 6f 61 64 69 6e 67  tion overloading
14cb6 2e 20 20 42 75 74 20 77 65 20 75 73 65 20 74 68  .  But we use th
14cb7 65 20 42 20 74 65 72 6d 20 69 6e 20 22 67 6c 6f  e B term in "glo
14cb8 62 28 42 2c 41 29 22 2e 0a 20 20 20 20 20 20 2a  b(B,A)"..      *
14cb9 2f 0a 20 20 20 20 20 20 69 66 28 20 6e 45 78 70  /.      if( nExp
14cba 72 3e 3d 32 20 26 26 20 28 70 45 78 70 72 2d 3e  r>=2 && (pExpr->
14cbb 66 6c 61 67 73 20 26 20 45 50 5f 49 6e 66 69 78  flags & EP_Infix
14cbc 46 75 6e 63 29 20 29 7b 0a 20 20 20 20 20 20 20  Func) ){.       
14cbd 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 56   pDef = sqlite3V
14cbe 74 61 62 4f 76 65 72 6c 6f 61 64 46 75 6e 63 74  tabOverloadFunct
14cbf 69 6f 6e 28 70 44 65 66 2c 20 6e 45 78 70 72 2c  ion(pDef, nExpr,
14cc0 20 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78   pList->a[1].pEx
14cc1 70 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  pr);.      }else
14cc2 20 69 66 28 20 6e 45 78 70 72 3e 30 20 29 7b 0a   if( nExpr>0 ){.
14cc3 20 20 20 20 20 20 20 20 70 44 65 66 20 3d 20 73          pDef = s
14cc4 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72 6c 6f  qlite3VtabOverlo
14cc5 61 64 46 75 6e 63 74 69 6f 6e 28 70 44 65 66 2c  adFunction(pDef,
14cc6 20 6e 45 78 70 72 2c 20 70 4c 69 73 74 2d 3e 61   nExpr, pList->a
14cc7 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20  [0].pExpr);.    
14cc8 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20    }.#endif.     
14cc9 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 45 78 70   for(i=0; i<nExp
14cca 72 20 26 26 20 69 3c 33 32 3b 20 69 2b 2b 29 7b  r && i<32; i++){
14ccb 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c  .        if( sql
14ccc 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61  ite3ExprIsConsta
14ccd 6e 74 28 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70  nt(pList->a[i].p
14cce 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 20  Expr) ){.       
14ccf 20 20 20 63 6f 6e 73 74 4d 61 73 6b 20 7c 3d 20     constMask |= 
14cd0 28 31 3c 3c 69 29 3b 0a 20 20 20 20 20 20 20 20  (1<<i);.        
14cd1 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 44  }.        if( pD
14cd2 65 66 2d 3e 6e 65 65 64 43 6f 6c 6c 53 65 71 20  ef->needCollSeq 
14cd3 26 26 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20  && !pColl ){.   
14cd4 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73         pColl = s
14cd5 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65  qlite3ExprCollSe
14cd6 71 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2d  q(pParse, pList-
14cd7 3e 61 5b 69 5d 2e 70 45 78 70 72 29 3b 0a 20 20  >a[i].pExpr);.  
14cd8 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
14cd9 20 20 20 20 20 20 69 66 28 20 70 44 65 66 2d 3e        if( pDef->
14cda 6e 65 65 64 43 6f 6c 6c 53 65 71 20 29 7b 0a 20  needCollSeq ){. 
14cdb 20 20 20 20 20 20 20 69 66 28 20 21 70 43 6f 6c         if( !pCol
14cdc 6c 20 29 20 70 43 6f 6c 6c 20 3d 20 70 50 61 72  l ) pColl = pPar
14cdd 73 65 2d 3e 64 62 2d 3e 70 44 66 6c 74 43 6f 6c  se->db->pDfltCol
14cde 6c 3b 20 0a 20 20 20 20 20 20 20 20 73 71 6c 69  l; .        sqli
14cdf 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20 4f 50  te3VdbeOp3(v, OP
14ce0 5f 43 6f 6c 6c 53 65 71 2c 20 30 2c 20 30 2c 20  _CollSeq, 0, 0, 
14ce1 28 63 68 61 72 20 2a 29 70 43 6f 6c 6c 2c 20 50  (char *)pColl, P
14ce2 33 5f 43 4f 4c 4c 53 45 51 29 3b 0a 20 20 20 20  3_COLLSEQ);.    
14ce3 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
14ce4 33 56 64 62 65 4f 70 33 28 76 2c 20 4f 50 5f 46  3VdbeOp3(v, OP_F
14ce5 75 6e 63 74 69 6f 6e 2c 20 63 6f 6e 73 74 4d 61  unction, constMa
14ce6 73 6b 2c 20 6e 45 78 70 72 2c 20 28 63 68 61 72  sk, nExpr, (char
14ce7 2a 29 70 44 65 66 2c 20 50 33 5f 46 55 4e 43 44  *)pDef, P3_FUNCD
14ce8 45 46 29 3b 0a 20 20 20 20 20 20 73 74 61 63 6b  EF);.      stack
14ce9 43 68 6e 67 20 3d 20 31 2d 6e 45 78 70 72 3b 0a  Chng = 1-nExpr;.
14cea 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
14ceb 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
14cec 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a  E_OMIT_SUBQUERY.
14ced 20 20 20 20 63 61 73 65 20 54 4b 5f 45 58 49 53      case TK_EXIS
14cee 54 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  TS:.    case TK_
14cef 53 45 4c 45 43 54 3a 20 7b 0a 20 20 20 20 20 20  SELECT: {.      
14cf0 69 66 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75  if( pExpr->iColu
14cf1 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  mn==0 ){.       
14cf2 20 73 71 6c 69 74 65 33 43 6f 64 65 53 75 62 73   sqlite3CodeSubs
14cf3 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 45  elect(pParse, pE
14cf4 78 70 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  xpr);.      }.  
14cf5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
14cf6 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 4c 6f  ddOp(v, OP_MemLo
14cf7 61 64 2c 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75  ad, pExpr->iColu
14cf8 6d 6e 2c 20 30 29 3b 0a 20 20 20 20 20 20 56 64  mn, 0);.      Vd
14cf9 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 23  beComment((v, "#
14cfa 20 6c 6f 61 64 20 73 75 62 71 75 65 72 79 20 72   load subquery r
14cfb 65 73 75 6c 74 22 29 29 3b 0a 20 20 20 20 20 20  esult"));.      
14cfc 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
14cfd 20 63 61 73 65 20 54 4b 5f 49 4e 3a 20 7b 0a 20   case TK_IN: {. 
14cfe 20 20 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20       int addr;. 
14cff 20 20 20 20 20 63 68 61 72 20 61 66 66 69 6e 69       char affini
14d00 74 79 3b 0a 20 20 20 20 20 20 69 6e 74 20 63 6b  ty;.      int ck
14d01 4f 66 66 73 65 74 20 3d 20 70 50 61 72 73 65 2d  Offset = pParse-
14d02 3e 63 6b 4f 66 66 73 65 74 3b 0a 20 20 20 20 20  >ckOffset;.     
14d03 20 73 71 6c 69 74 65 33 43 6f 64 65 53 75 62 73   sqlite3CodeSubs
14d04 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 45  elect(pParse, pE
14d05 78 70 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  xpr);..      /* 
14d06 46 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 61  Figure out the a
14d07 66 66 69 6e 69 74 79 20 74 6f 20 75 73 65 20 74  ffinity to use t
14d08 6f 20 63 72 65 61 74 65 20 61 20 6b 65 79 20 66  o create a key f
14d09 72 6f 6d 20 74 68 65 20 72 65 73 75 6c 74 73 0a  rom the results.
14d0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20        ** of the 
14d0b 65 78 70 72 65 73 73 69 6f 6e 2e 20 61 66 66 69  expression. affi
14d0c 6e 69 74 79 53 74 72 20 73 74 6f 72 65 73 20 61  nityStr stores a
14d0d 20 73 74 61 74 69 63 20 73 74 72 69 6e 67 20 73   static string s
14d0e 75 69 74 61 62 6c 65 20 66 6f 72 0a 20 20 20 20  uitable for.    
14d0f 20 20 2a 2a 20 50 33 20 6f 66 20 4f 50 5f 4d 61    ** P3 of OP_Ma
14d10 6b 65 52 65 63 6f 72 64 2e 0a 20 20 20 20 20 20  keRecord..      
14d11 2a 2f 0a 20 20 20 20 20 20 61 66 66 69 6e 69 74  */.      affinit
14d12 79 20 3d 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66  y = comparisonAf
14d13 66 69 6e 69 74 79 28 70 45 78 70 72 29 3b 0a 0a  finity(pExpr);..
14d14 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
14d15 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 74  eAddOp(v, OP_Int
14d16 65 67 65 72 2c 20 31 2c 20 30 29 3b 0a 20 20 20  eger, 1, 0);.   
14d17 20 20 20 70 50 61 72 73 65 2d 3e 63 6b 4f 66 66     pParse->ckOff
14d18 73 65 74 20 3d 20 28 63 6b 4f 66 66 73 65 74 20  set = (ckOffset 
14d19 3f 20 28 63 6b 4f 66 66 73 65 74 2b 31 29 20 3a  ? (ckOffset+1) :
14d1a 20 30 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43   0);..      /* C
14d1b 6f 64 65 20 74 68 65 20 3c 65 78 70 72 3e 20 66  ode the <expr> f
14d1c 72 6f 6d 20 22 3c 65 78 70 72 3e 20 49 4e 20 28  rom "<expr> IN (
14d1d 2e 2e 2e 29 22 2e 20 54 68 65 20 74 65 6d 70 6f  ...)". The tempo
14d1e 72 61 72 79 20 74 61 62 6c 65 0a 20 20 20 20 20  rary table.     
14d1f 20 2a 2a 20 70 45 78 70 72 2d 3e 69 54 61 62 6c   ** pExpr->iTabl
14d20 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 76  e contains the v
14d21 61 6c 75 65 73 20 74 68 61 74 20 6d 61 6b 65 20  alues that make 
14d22 75 70 20 74 68 65 20 28 2e 2e 2e 29 20 73 65 74  up the (...) set
14d23 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
14d24 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
14d25 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
14d26 70 4c 65 66 74 29 3b 0a 20 20 20 20 20 20 61 64  pLeft);.      ad
14d27 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
14d28 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a  CurrentAddr(v);.
14d29 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
14d2a 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4e 6f 74  eAddOp(v, OP_Not
14d2b 4e 75 6c 6c 2c 20 2d 31 2c 20 61 64 64 72 2b 34  Null, -1, addr+4
14d2c 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  );            /*
14d2d 20 61 64 64 72 20 2b 20 30 20 2a 2f 0a 20 20 20   addr + 0 */.   
14d2e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
14d2f 64 4f 70 28 76 2c 20 4f 50 5f 50 6f 70 2c 20 32  dOp(v, OP_Pop, 2
14d30 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 0);.      sqli
14d31 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
14d32 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 30 29 3b 0a  OP_Null, 0, 0);.
14d33 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
14d34 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 47 6f 74  eAddOp(v, OP_Got
14d35 6f 2c 20 30 2c 20 61 64 64 72 2b 37 29 3b 0a 20  o, 0, addr+7);. 
14d36 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
14d37 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65  Op3(v, OP_MakeRe
14d38 63 6f 72 64 2c 20 31 2c 20 30 2c 20 26 61 66 66  cord, 1, 0, &aff
14d39 69 6e 69 74 79 2c 20 31 29 3b 20 20 20 2f 2a 20  inity, 1);   /* 
14d3a 61 64 64 72 20 2b 20 34 20 2a 2f 0a 20 20 20 20  addr + 4 */.    
14d3b 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
14d3c 4f 70 28 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20  Op(v, OP_Found, 
14d3d 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 61  pExpr->iTable, a
14d3e 64 64 72 2b 37 29 3b 0a 20 20 20 20 20 20 73 71  ddr+7);.      sq
14d3f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
14d40 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 2d 31 2c  , OP_AddImm, -1,
14d41 20 30 29 3b 20 20 20 20 20 20 20 20 20 20 20 20   0);            
14d42 20 20 20 20 20 20 2f 2a 20 61 64 64 72 20 2b 20        /* addr + 
14d43 36 20 2a 2f 0a 0a 20 20 20 20 20 20 62 72 65 61  6 */..      brea
14d44 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  k;.    }.#endif.
14d45 20 20 20 20 63 61 73 65 20 54 4b 5f 42 45 54 57      case TK_BETW
14d46 45 45 4e 3a 20 7b 0a 20 20 20 20 20 20 45 78 70  EEN: {.      Exp
14d47 72 20 2a 70 4c 65 66 74 20 3d 20 70 45 78 70 72  r *pLeft = pExpr
14d48 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 73  ->pLeft;.      s
14d49 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
14d4a 74 65 6d 20 2a 70 4c 49 74 65 6d 20 3d 20 70 45  tem *pLItem = pE
14d4b 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 3b 0a 20  xpr->pList->a;. 
14d4c 20 20 20 20 20 45 78 70 72 20 2a 70 52 69 67 68       Expr *pRigh
14d4d 74 20 3d 20 70 4c 49 74 65 6d 2d 3e 70 45 78 70  t = pLItem->pExp
14d4e 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  r;.      sqlite3
14d4f 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
14d50 20 70 4c 65 66 74 29 3b 0a 20 20 20 20 20 20 73   pLeft);.      s
14d51 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
14d52 76 2c 20 4f 50 5f 44 75 70 2c 20 30 2c 20 30 29  v, OP_Dup, 0, 0)
14d53 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
14d54 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20  xprCode(pParse, 
14d55 70 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20 63  pRight);.      c
14d56 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61 72 73  odeCompare(pPars
14d57 65 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74  e, pLeft, pRight
14d58 2c 20 4f 50 5f 47 65 2c 20 30 2c 20 30 29 3b 0a  , OP_Ge, 0, 0);.
14d59 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
14d5a 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 50 75 6c  eAddOp(v, OP_Pul
14d5b 6c 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 20 20  l, 1, 0);.      
14d5c 70 4c 49 74 65 6d 2b 2b 3b 0a 20 20 20 20 20 20  pLItem++;.      
14d5d 70 52 69 67 68 74 20 3d 20 70 4c 49 74 65 6d 2d  pRight = pLItem-
14d5e 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 73 71  >pExpr;.      sq
14d5f 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
14d60 61 72 73 65 2c 20 70 52 69 67 68 74 29 3b 0a 20  arse, pRight);. 
14d61 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65       codeCompare
14d62 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 2c 20  (pParse, pLeft, 
14d63 70 52 69 67 68 74 2c 20 4f 50 5f 4c 65 2c 20 30  pRight, OP_Le, 0
14d64 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 0);.      sqli
14d65 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
14d66 4f 50 5f 41 6e 64 2c 20 30 2c 20 30 29 3b 0a 20  OP_And, 0, 0);. 
14d67 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
14d68 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55 50  }.    case TK_UP
14d69 4c 55 53 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c  LUS: {.      sql
14d6a 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
14d6b 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
14d6c 74 29 3b 0a 20 20 20 20 20 20 73 74 61 63 6b 43  t);.      stackC
14d6d 68 6e 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 62  hng = 0;.      b
14d6e 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
14d6f 63 61 73 65 20 54 4b 5f 43 41 53 45 3a 20 7b 0a  case TK_CASE: {.
14d70 20 20 20 20 20 20 69 6e 74 20 65 78 70 72 5f 65        int expr_e
14d71 6e 64 5f 6c 61 62 65 6c 3b 0a 20 20 20 20 20 20  nd_label;.      
14d72 69 6e 74 20 6a 75 6d 70 49 6e 73 74 3b 0a 20 20  int jumpInst;.  
14d73 20 20 20 20 69 6e 74 20 6e 45 78 70 72 3b 0a 20      int nExpr;. 
14d74 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20       int i;.    
14d75 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69    ExprList *pELi
14d76 73 74 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74  st;.      struct
14d77 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
14d78 61 4c 69 73 74 65 6c 65 6d 3b 0a 0a 20 20 20 20  aListelem;..    
14d79 20 20 61 73 73 65 72 74 28 70 45 78 70 72 2d 3e    assert(pExpr->
14d7a 70 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 61 73  pList);.      as
14d7b 73 65 72 74 28 28 70 45 78 70 72 2d 3e 70 4c 69  sert((pExpr->pLi
14d7c 73 74 2d 3e 6e 45 78 70 72 20 25 20 32 29 20 3d  st->nExpr % 2) =
14d7d 3d 20 30 29 3b 0a 20 20 20 20 20 20 61 73 73 65  = 0);.      asse
14d7e 72 74 28 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d  rt(pExpr->pList-
14d7f 3e 6e 45 78 70 72 20 3e 20 30 29 3b 0a 20 20 20  >nExpr > 0);.   
14d80 20 20 20 70 45 4c 69 73 74 20 3d 20 70 45 78 70     pEList = pExp
14d81 72 2d 3e 70 4c 69 73 74 3b 0a 20 20 20 20 20 20  r->pList;.      
14d82 61 4c 69 73 74 65 6c 65 6d 20 3d 20 70 45 4c 69  aListelem = pELi
14d83 73 74 2d 3e 61 3b 0a 20 20 20 20 20 20 6e 45 78  st->a;.      nEx
14d84 70 72 20 3d 20 70 45 4c 69 73 74 2d 3e 6e 45 78  pr = pEList->nEx
14d85 70 72 3b 0a 20 20 20 20 20 20 65 78 70 72 5f 65  pr;.      expr_e
14d86 6e 64 5f 6c 61 62 65 6c 20 3d 20 73 71 6c 69 74  nd_label = sqlit
14d87 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
14d88 76 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45  v);.      if( pE
14d89 78 70 72 2d 3e 70 4c 65 66 74 20 29 7b 0a 20 20  xpr->pLeft ){.  
14d8a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
14d8b 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45  rCode(pParse, pE
14d8c 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 20  xpr->pLeft);.   
14d8d 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 69     }.      for(i
14d8e 3d 30 3b 20 69 3c 6e 45 78 70 72 3b 20 69 3d 69  =0; i<nExpr; i=i
14d8f 2b 32 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  +2){.        sql
14d90 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
14d91 72 73 65 2c 20 61 4c 69 73 74 65 6c 65 6d 5b 69  rse, aListelem[i
14d92 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20  ].pExpr);.      
14d93 20 20 69 66 28 20 70 45 78 70 72 2d 3e 70 4c 65    if( pExpr->pLe
14d94 66 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ft ){.          
14d95 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
14d96 28 76 2c 20 4f 50 5f 44 75 70 2c 20 31 2c 20 31  (v, OP_Dup, 1, 1
14d97 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 75 6d  );.          jum
14d98 70 49 6e 73 74 20 3d 20 63 6f 64 65 43 6f 6d 70  pInst = codeComp
14d99 61 72 65 28 70 50 61 72 73 65 2c 20 70 45 78 70  are(pParse, pExp
14d9a 72 2d 3e 70 4c 65 66 74 2c 20 61 4c 69 73 74 65  r->pLeft, aListe
14d9b 6c 65 6d 5b 69 5d 2e 70 45 78 70 72 2c 0a 20 20  lem[i].pExpr,.  
14d9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14d9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f                 O
14d9e 50 5f 4e 65 2c 20 30 2c 20 31 29 3b 0a 20 20 20  P_Ne, 0, 1);.   
14d9f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
14da0 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 50 6f  beAddOp(v, OP_Po
14da1 70 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 20 20  p, 1, 0);.      
14da2 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
14da3 20 20 20 6a 75 6d 70 49 6e 73 74 20 3d 20 73 71     jumpInst = sq
14da4 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
14da5 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20 31 2c 20 30  , OP_IfNot, 1, 0
14da6 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
14da7 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
14da8 43 6f 64 65 28 70 50 61 72 73 65 2c 20 61 4c 69  Code(pParse, aLi
14da9 73 74 65 6c 65 6d 5b 69 2b 31 5d 2e 70 45 78 70  stelem[i+1].pExp
14daa 72 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  r);.        sqli
14dab 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
14dac 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 65 78 70 72  OP_Goto, 0, expr
14dad 5f 65 6e 64 5f 6c 61 62 65 6c 29 3b 0a 20 20 20  _end_label);.   
14dae 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
14daf 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 75 6d 70  JumpHere(v, jump
14db0 49 6e 73 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20  Inst);.      }. 
14db1 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e       if( pExpr->
14db2 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20 20  pLeft ){.       
14db3 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
14db4 70 28 76 2c 20 4f 50 5f 50 6f 70 2c 20 31 2c 20  p(v, OP_Pop, 1, 
14db5 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  0);.      }.    
14db6 20 20 69 66 28 20 70 45 78 70 72 2d 3e 70 52 69    if( pExpr->pRi
14db7 67 68 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ght ){.        s
14db8 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
14db9 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52  Parse, pExpr->pR
14dba 69 67 68 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c  ight);.      }el
14dbb 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  se{.        sqli
14dbc 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
14dbd 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 30 29 3b 0a  OP_Null, 0, 0);.
14dbe 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
14dbf 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
14dc0 4c 61 62 65 6c 28 76 2c 20 65 78 70 72 5f 65 6e  Label(v, expr_en
14dc1 64 5f 6c 61 62 65 6c 29 3b 0a 20 20 20 20 20 20  d_label);.      
14dc2 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66  break;.    }.#if
14dc3 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
14dc4 5f 54 52 49 47 47 45 52 0a 20 20 20 20 63 61 73  _TRIGGER.    cas
14dc5 65 20 54 4b 5f 52 41 49 53 45 3a 20 7b 0a 20 20  e TK_RAISE: {.  
14dc6 20 20 20 20 69 66 28 20 21 70 50 61 72 73 65 2d      if( !pParse-
14dc7 3e 74 72 69 67 53 74 61 63 6b 20 29 7b 0a 20 20  >trigStack ){.  
14dc8 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
14dc9 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 0a 20 20  orMsg(pParse,.  
14dca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14dcb 20 20 20 20 20 22 52 41 49 53 45 28 29 20 6d 61       "RAISE() ma
14dcc 79 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 77  y only be used w
14dcd 69 74 68 69 6e 20 61 20 74 72 69 67 67 65 72 2d  ithin a trigger-
14dce 70 72 6f 67 72 61 6d 22 29 3b 0a 09 72 65 74 75  program");..retu
14dcf 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
14dd0 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 43 6f    if( pExpr->iCo
14dd1 6c 75 6d 6e 21 3d 4f 45 5f 49 67 6e 6f 72 65 20  lumn!=OE_Ignore 
14dd2 29 7b 0a 20 20 20 20 20 20 20 20 20 61 73 73 65  ){.         asse
14dd3 72 74 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75  rt( pExpr->iColu
14dd4 6d 6e 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20  mn==OE_Rollback 
14dd5 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ||.             
14dd6 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75      pExpr->iColu
14dd7 6d 6e 20 3d 3d 20 4f 45 5f 41 62 6f 72 74 20 7c  mn == OE_Abort |
14dd8 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  |.              
14dd9 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d     pExpr->iColum
14dda 6e 20 3d 3d 20 4f 45 5f 46 61 69 6c 20 29 3b 0a  n == OE_Fail );.
14ddb 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
14ddc 44 65 71 75 6f 74 65 45 78 70 72 28 70 45 78 70  DequoteExpr(pExp
14ddd 72 29 3b 0a 20 20 20 20 20 20 20 20 20 73 71 6c  r);.         sql
14dde 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20 4f  ite3VdbeOp3(v, O
14ddf 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54 45 5f 43  P_Halt, SQLITE_C
14de0 4f 4e 53 54 52 41 49 4e 54 2c 20 70 45 78 70 72  ONSTRAINT, pExpr
14de1 2d 3e 69 43 6f 6c 75 6d 6e 2c 0a 20 20 20 20 20  ->iColumn,.     
14de2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14de3 20 20 20 28 63 68 61 72 2a 29 70 45 78 70 72 2d     (char*)pExpr-
14de4 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78 70 72 2d  >token.z, pExpr-
14de5 3e 74 6f 6b 65 6e 2e 6e 29 3b 0a 20 20 20 20 20  >token.n);.     
14de6 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20   } else {.      
14de7 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72     assert( pExpr
14de8 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 3d 20 4f 45 5f  ->iColumn == OE_
14de9 49 67 6e 6f 72 65 20 29 3b 0a 20 20 20 20 20 20  Ignore );.      
14dea 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
14deb 64 4f 70 28 76 2c 20 4f 50 5f 43 6f 6e 74 65 78  dOp(v, OP_Contex
14dec 74 50 6f 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20  tPop, 0, 0);.   
14ded 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
14dee 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 47 6f 74  eAddOp(v, OP_Got
14def 6f 2c 20 30 2c 20 70 50 61 72 73 65 2d 3e 74 72  o, 0, pParse->tr
14df0 69 67 53 74 61 63 6b 2d 3e 69 67 6e 6f 72 65 4a  igStack->ignoreJ
14df1 75 6d 70 29 3b 0a 20 20 20 20 20 20 20 20 20 56  ump);.         V
14df2 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
14df3 23 20 72 61 69 73 65 28 49 47 4e 4f 52 45 29 22  # raise(IGNORE)"
14df4 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ));.      }.    
14df5 20 20 73 74 61 63 6b 43 68 6e 67 20 3d 20 30 3b    stackChng = 0;
14df6 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
14df7 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a    }.#endif.  }..
14df8 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 63 6b    if( pParse->ck
14df9 4f 66 66 73 65 74 20 29 7b 0a 20 20 20 20 70 50  Offset ){.    pP
14dfa 61 72 73 65 2d 3e 63 6b 4f 66 66 73 65 74 20 2b  arse->ckOffset +
14dfb 3d 20 73 74 61 63 6b 43 68 6e 67 3b 0a 20 20 20  = stackChng;.   
14dfc 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d   assert( pParse-
14dfd 3e 63 6b 4f 66 66 73 65 74 20 29 3b 0a 20 20 7d  >ckOffset );.  }
14dfe 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
14dff 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a  TE_OMIT_TRIGGER.
14e00 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
14e01 6f 64 65 20 74 68 61 74 20 65 76 61 6c 75 74 65  ode that evalute
14e02 73 20 74 68 65 20 67 69 76 65 6e 20 65 78 70 72  s the given expr
14e03 65 73 73 69 6f 6e 20 61 6e 64 20 6c 65 61 76 65  ession and leave
14e04 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20  s the result.** 
14e05 6f 6e 20 74 68 65 20 73 74 61 63 6b 2e 20 20 53  on the stack.  S
14e06 65 65 20 61 6c 73 6f 20 73 71 6c 69 74 65 33 45  ee also sqlite3E
14e07 78 70 72 43 6f 64 65 28 29 2e 0a 2a 2a 0a 2a 2a  xprCode()..**.**
14e08 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 69   This routine mi
14e09 67 68 74 20 61 6c 73 6f 20 63 61 63 68 65 20 74  ght also cache t
14e0a 68 65 20 72 65 73 75 6c 74 20 61 6e 64 20 6d 6f  he result and mo
14e0b 64 69 66 79 20 74 68 65 20 70 45 78 70 72 20 74  dify the pExpr t
14e0c 72 65 65 0a 2a 2a 20 73 6f 20 74 68 61 74 20 69  ree.** so that i
14e0d 74 20 77 69 6c 6c 20 6d 61 6b 65 20 75 73 65 20  t will make use 
14e0e 6f 66 20 74 68 65 20 63 61 63 68 65 64 20 72 65  of the cached re
14e0f 73 75 6c 74 20 6f 6e 20 73 75 62 73 65 71 75 65  sult on subseque
14e10 6e 74 20 65 76 61 6c 75 61 74 69 6f 6e 73 0a 2a  nt evaluations.*
14e11 2a 20 72 61 74 68 65 72 20 74 68 61 6e 20 65 76  * rather than ev
14e12 61 6c 75 61 74 65 20 74 68 65 20 77 68 6f 6c 65  aluate the whole
14e13 20 65 78 70 72 65 73 73 69 6f 6e 20 61 67 61 69   expression agai
14e14 6e 2e 20 20 54 72 69 76 69 61 6c 20 65 78 70 72  n.  Trivial expr
14e15 65 73 73 69 6f 6e 73 20 61 72 65 0a 2a 2a 20 6e  essions are.** n
14e16 6f 74 20 63 61 63 68 65 64 2e 20 20 49 66 20 74  ot cached.  If t
14e17 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
14e18 20 63 61 63 68 65 64 2c 20 69 74 73 20 72 65 73   cached, its res
14e19 75 6c 74 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ult is stored in
14e1a 20 61 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 6c 6f   a .** memory lo
14e1b 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  cation..*/.SQLIT
14e1c 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
14e1d 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 41 6e  qlite3ExprCodeAn
14e1e 64 43 61 63 68 65 28 50 61 72 73 65 20 2a 70 50  dCache(Parse *pP
14e1f 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70  arse, Expr *pExp
14e20 72 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  r){.  Vdbe *v = 
14e21 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20  pParse->pVdbe;. 
14e22 20 69 6e 74 20 69 4d 65 6d 3b 0a 20 20 69 6e 74   int iMem;.  int
14e23 20 61 64 64 72 31 2c 20 61 64 64 72 32 3b 0a 20   addr1, addr2;. 
14e24 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75   if( v==0 ) retu
14e25 72 6e 3b 0a 20 20 61 64 64 72 31 20 3d 20 73 71  rn;.  addr1 = sq
14e26 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
14e27 41 64 64 72 28 76 29 3b 0a 20 20 73 71 6c 69 74  Addr(v);.  sqlit
14e28 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
14e29 65 2c 20 70 45 78 70 72 29 3b 0a 20 20 61 64 64  e, pExpr);.  add
14e2a 72 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  r2 = sqlite3Vdbe
14e2b 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a  CurrentAddr(v);.
14e2c 20 20 69 66 28 20 61 64 64 72 32 3e 61 64 64 72    if( addr2>addr
14e2d 31 2b 31 20 7c 7c 20 73 71 6c 69 74 65 33 56 64  1+1 || sqlite3Vd
14e2e 62 65 47 65 74 4f 70 28 76 2c 20 61 64 64 72 31  beGetOp(v, addr1
14e2f 29 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46 75  )->opcode==OP_Fu
14e30 6e 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 69 4d  nction ){.    iM
14e31 65 6d 20 3d 20 70 45 78 70 72 2d 3e 69 54 61 62  em = pExpr->iTab
14e32 6c 65 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65  le = pParse->nMe
14e33 6d 2b 2b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  m++;.    sqlite3
14e34 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
14e35 4d 65 6d 53 74 6f 72 65 2c 20 69 4d 65 6d 2c 20  MemStore, iMem, 
14e36 30 29 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f  0);.    pExpr->o
14e37 70 20 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b  p = TK_REGISTER;
14e38 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  .  }.}.#endif../
14e39 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
14e3a 64 65 20 74 68 61 74 20 70 75 73 68 65 73 20 74  de that pushes t
14e3b 68 65 20 76 61 6c 75 65 20 6f 66 20 65 76 65 72  he value of ever
14e3c 79 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65  y element of the
14e3d 20 67 69 76 65 6e 0a 2a 2a 20 65 78 70 72 65 73   given.** expres
14e3e 73 69 6f 6e 20 6c 69 73 74 20 6f 6e 74 6f 20 74  sion list onto t
14e3f 68 65 20 73 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20  he stack..**.** 
14e40 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
14e41 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 70 75  r of elements pu
14e42 73 68 65 64 20 6f 6e 74 6f 20 74 68 65 20 73 74  shed onto the st
14e43 61 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ack..*/.SQLITE_P
14e44 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
14e45 65 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69  e3ExprCodeExprLi
14e46 73 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  st(.  Parse *pPa
14e47 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73  rse,     /* Pars
14e48 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
14e49 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
14e4a 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65      /* The expre
14e4b 73 73 69 6f 6e 20 6c 69 73 74 20 74 6f 20 62 65  ssion list to be
14e4c 20 63 6f 64 65 64 20 2a 2f 0a 29 7b 0a 20 20 73   coded */.){.  s
14e4d 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
14e4e 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 6e  tem *pItem;.  in
14e4f 74 20 69 2c 20 6e 3b 0a 20 20 69 66 28 20 70 4c  t i, n;.  if( pL
14e50 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ist==0 ) return 
14e51 30 3b 0a 20 20 6e 20 3d 20 70 4c 69 73 74 2d 3e  0;.  n = pList->
14e52 6e 45 78 70 72 3b 0a 20 20 66 6f 72 28 70 49 74  nExpr;.  for(pIt
14e53 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20 69 3d 6e  em=pList->a, i=n
14e54 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65  ; i>0; i--, pIte
14e55 6d 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  m++){.    sqlite
14e56 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
14e57 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 3b  , pItem->pExpr);
14e58 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 3b  .  }.  return n;
14e59 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  .}../*.** Genera
14e5a 74 65 20 63 6f 64 65 20 66 6f 72 20 61 20 62 6f  te code for a bo
14e5b 6f 6c 65 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  olean expression
14e5c 20 73 75 63 68 20 74 68 61 74 20 61 20 6a 75 6d   such that a jum
14e5d 70 20 69 73 20 6d 61 64 65 0a 2a 2a 20 74 6f 20  p is made.** to 
14e5e 74 68 65 20 6c 61 62 65 6c 20 22 64 65 73 74 22  the label "dest"
14e5f 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69   if the expressi
14e60 6f 6e 20 69 73 20 74 72 75 65 20 62 75 74 20 65  on is true but e
14e61 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 63 6f 6e 74  xecution.** cont
14e62 69 6e 75 65 73 20 73 74 72 61 69 67 68 74 20 74  inues straight t
14e63 68 72 75 20 69 66 20 74 68 65 20 65 78 70 72 65  hru if the expre
14e64 73 73 69 6f 6e 20 69 73 20 66 61 6c 73 65 2e 0a  ssion is false..
14e65 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70  **.** If the exp
14e66 72 65 73 73 69 6f 6e 20 65 76 61 6c 75 61 74 65  ression evaluate
14e67 73 20 74 6f 20 4e 55 4c 4c 20 28 6e 65 69 74 68  s to NULL (neith
14e68 65 72 20 74 72 75 65 20 6e 6f 72 20 66 61 6c 73  er true nor fals
14e69 65 29 2c 20 74 68 65 6e 0a 2a 2a 20 74 61 6b 65  e), then.** take
14e6a 20 74 68 65 20 6a 75 6d 70 20 69 66 20 74 68 65   the jump if the
14e6b 20 6a 75 6d 70 49 66 4e 75 6c 6c 20 66 6c 61 67   jumpIfNull flag
14e6c 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20   is true..**.** 
14e6d 54 68 69 73 20 63 6f 64 65 20 64 65 70 65 6e 64  This code depend
14e6e 73 20 6f 6e 20 74 68 65 20 66 61 63 74 20 74 68  s on the fact th
14e6f 61 74 20 63 65 72 74 61 69 6e 20 74 6f 6b 65 6e  at certain token
14e70 20 76 61 6c 75 65 73 20 28 65 78 3a 20 54 4b 5f   values (ex: TK_
14e71 45 51 29 0a 2a 2a 20 61 72 65 20 74 68 65 20 73  EQ).** are the s
14e72 61 6d 65 20 61 73 20 6f 70 63 6f 64 65 20 76 61  ame as opcode va
14e73 6c 75 65 73 20 28 65 78 3a 20 4f 50 5f 45 71 29  lues (ex: OP_Eq)
14e74 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20   that implement 
14e75 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
14e76 67 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2e 20  g.** operation. 
14e77 20 53 70 65 63 69 61 6c 20 63 6f 6d 6d 65 6e 74   Special comment
14e78 73 20 69 6e 20 76 64 62 65 2e 63 20 61 6e 64 20  s in vdbe.c and 
14e79 74 68 65 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77  the mkopcodeh.aw
14e7a 6b 20 73 63 72 69 70 74 20 69 6e 0a 2a 2a 20 74  k script in.** t
14e7b 68 65 20 6d 61 6b 65 20 70 72 6f 63 65 73 73 20  he make process 
14e7c 63 61 75 73 65 20 74 68 65 73 65 20 76 61 6c 75  cause these valu
14e7d 65 73 20 74 6f 20 61 6c 69 67 6e 2e 20 20 41 73  es to align.  As
14e7e 73 65 72 74 28 29 73 20 69 6e 20 74 68 65 20 63  sert()s in the c
14e7f 6f 64 65 0a 2a 2a 20 62 65 6c 6f 77 20 76 65 72  ode.** below ver
14e80 69 66 79 20 74 68 61 74 20 74 68 65 20 6e 75 6d  ify that the num
14e81 62 65 72 73 20 61 72 65 20 61 6c 69 67 6e 65 64  bers are aligned
14e82 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2f 0a 53   correctly..*/.S
14e83 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
14e84 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 49 66  id sqlite3ExprIf
14e85 54 72 75 65 28 50 61 72 73 65 20 2a 70 50 61 72  True(Parse *pPar
14e86 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c  se, Expr *pExpr,
14e87 20 69 6e 74 20 64 65 73 74 2c 20 69 6e 74 20 6a   int dest, int j
14e88 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a 20 20 56 64  umpIfNull){.  Vd
14e89 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
14e8a 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 6f 70 20  pVdbe;.  int op 
14e8b 3d 20 30 3b 0a 20 20 69 6e 74 20 63 6b 4f 66 66  = 0;.  int ckOff
14e8c 73 65 74 20 3d 20 70 50 61 72 73 65 2d 3e 63 6b  set = pParse->ck
14e8d 4f 66 66 73 65 74 3b 0a 20 20 69 66 28 20 76 3d  Offset;.  if( v=
14e8e 3d 30 20 7c 7c 20 70 45 78 70 72 3d 3d 30 20 29  =0 || pExpr==0 )
14e8f 20 72 65 74 75 72 6e 3b 0a 20 20 6f 70 20 3d 20   return;.  op = 
14e90 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 20 73 77 69  pExpr->op;.  swi
14e91 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63  tch( op ){.    c
14e92 61 73 65 20 54 4b 5f 41 4e 44 3a 20 7b 0a 20 20  ase TK_AND: {.  
14e93 20 20 20 20 69 6e 74 20 64 32 20 3d 20 73 71 6c      int d2 = sql
14e94 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
14e95 6c 28 76 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  l(v);.      sqli
14e96 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70  te3ExprIfFalse(p
14e97 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c  Parse, pExpr->pL
14e98 65 66 74 2c 20 64 32 2c 20 21 6a 75 6d 70 49 66  eft, d2, !jumpIf
14e99 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c  Null);.      sql
14e9a 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 70  ite3ExprIfTrue(p
14e9b 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52  Parse, pExpr->pR
14e9c 69 67 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70  ight, dest, jump
14e9d 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 73  IfNull);.      s
14e9e 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
14e9f 65 4c 61 62 65 6c 28 76 2c 20 64 32 29 3b 0a 20  eLabel(v, d2);. 
14ea0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
14ea1 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4f 52  }.    case TK_OR
14ea2 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  : {.      sqlite
14ea3 33 45 78 70 72 49 66 54 72 75 65 28 70 50 61 72  3ExprIfTrue(pPar
14ea4 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  se, pExpr->pLeft
14ea5 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75  , dest, jumpIfNu
14ea6 6c 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ll);.      sqlit
14ea7 65 33 45 78 70 72 49 66 54 72 75 65 28 70 50 61  e3ExprIfTrue(pPa
14ea8 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67  rse, pExpr->pRig
14ea9 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66  ht, dest, jumpIf
14eaa 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65  Null);.      bre
14eab 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
14eac 73 65 20 54 4b 5f 4e 4f 54 3a 20 7b 0a 20 20 20  se TK_NOT: {.   
14ead 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66     sqlite3ExprIf
14eae 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45  False(pParse, pE
14eaf 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64 65 73 74  xpr->pLeft, dest
14eb0 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20  , jumpIfNull);. 
14eb1 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
14eb2 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c 54  }.    case TK_LT
14eb3 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c 45  :.    case TK_LE
14eb4 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47 54  :.    case TK_GT
14eb5 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47 45  :.    case TK_GE
14eb6 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 45  :.    case TK_NE
14eb7 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 51  :.    case TK_EQ
14eb8 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  : {.      assert
14eb9 28 20 54 4b 5f 4c 54 3d 3d 4f 50 5f 4c 74 20 29  ( TK_LT==OP_Lt )
14eba 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
14ebb 54 4b 5f 4c 45 3d 3d 4f 50 5f 4c 65 20 29 3b 0a  TK_LE==OP_Le );.
14ebc 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
14ebd 5f 47 54 3d 3d 4f 50 5f 47 74 20 29 3b 0a 20 20  _GT==OP_Gt );.  
14ebe 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 47      assert( TK_G
14ebf 45 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20 20 20 20  E==OP_Ge );.    
14ec0 20 20 61 73 73 65 72 74 28 20 54 4b 5f 45 51 3d    assert( TK_EQ=
14ec1 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 20 20 20 20  =OP_Eq );.      
14ec2 61 73 73 65 72 74 28 20 54 4b 5f 4e 45 3d 3d 4f  assert( TK_NE==O
14ec3 50 5f 4e 65 20 29 3b 0a 20 20 20 20 20 20 73 71  P_Ne );.      sq
14ec4 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
14ec5 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65  arse, pExpr->pLe
14ec6 66 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ft);.      sqlit
14ec7 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
14ec8 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  e, pExpr->pRight
14ec9 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d  );.      codeCom
14eca 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 45 78  pare(pParse, pEx
14ecb 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 72  pr->pLeft, pExpr
14ecc 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c 20 64 65  ->pRight, op, de
14ecd 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b  st, jumpIfNull);
14ece 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
14ecf 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
14ed0 49 53 4e 55 4c 4c 3a 0a 20 20 20 20 63 61 73 65  ISNULL:.    case
14ed1 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 7b 0a 20   TK_NOTNULL: {. 
14ed2 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
14ed3 49 53 4e 55 4c 4c 3d 3d 4f 50 5f 49 73 4e 75 6c  ISNULL==OP_IsNul
14ed4 6c 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  l );.      asser
14ed5 74 28 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3d 3d 4f  t( TK_NOTNULL==O
14ed6 50 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20 20 20  P_NotNull );.   
14ed7 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
14ed8 64 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  de(pParse, pExpr
14ed9 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 20 20 20 20  ->pLeft);.      
14eda 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
14edb 28 76 2c 20 6f 70 2c 20 31 2c 20 64 65 73 74 29  (v, op, 1, dest)
14edc 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
14edd 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b     }.    case TK
14ede 5f 42 45 54 57 45 45 4e 3a 20 7b 0a 20 20 20 20  _BETWEEN: {.    
14edf 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73    /* The express
14ee0 69 6f 6e 20 22 78 20 42 45 54 57 45 45 4e 20 79  ion "x BETWEEN y
14ee1 20 41 4e 44 20 7a 22 20 69 73 20 69 6d 70 6c 65   AND z" is imple
14ee2 6d 65 6e 74 65 64 20 61 73 3a 0a 20 20 20 20 20  mented as:.     
14ee3 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 31 20 49   **.      ** 1 I
14ee4 46 20 28 78 20 3c 20 79 29 20 47 4f 54 4f 20 33  F (x < y) GOTO 3
14ee5 0a 20 20 20 20 20 20 2a 2a 20 32 20 49 46 20 28  .      ** 2 IF (
14ee6 78 20 3c 3d 20 7a 29 20 47 4f 54 4f 20 3c 64 65  x <= z) GOTO <de
14ee7 73 74 3e 0a 20 20 20 20 20 20 2a 2a 20 33 20 2e  st>.      ** 3 .
14ee8 2e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  ...      */.    
14ee9 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 20 20    int addr;.    
14eea 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20    Expr *pLeft = 
14eeb 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20  pExpr->pLeft;.  
14eec 20 20 20 20 45 78 70 72 20 2a 70 52 69 67 68 74      Expr *pRight
14eed 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d   = pExpr->pList-
14eee 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20  >a[0].pExpr;.   
14eef 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
14ef0 64 65 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74  de(pParse, pLeft
14ef1 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
14ef2 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
14ef3 44 75 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  Dup, 0, 0);.    
14ef4 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
14ef5 65 28 70 50 61 72 73 65 2c 20 70 52 69 67 68 74  e(pParse, pRight
14ef6 29 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20  );.      addr = 
14ef7 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61 72  codeCompare(pPar
14ef8 73 65 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68  se, pLeft, pRigh
14ef9 74 2c 20 4f 50 5f 4c 74 2c 20 30 2c 20 21 6a 75  t, OP_Lt, 0, !ju
14efa 6d 70 49 66 4e 75 6c 6c 29 3b 0a 0a 20 20 20 20  mpIfNull);..    
14efb 20 20 70 52 69 67 68 74 20 3d 20 70 45 78 70 72    pRight = pExpr
14efc 2d 3e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45  ->pList->a[1].pE
14efd 78 70 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  xpr;.      sqlit
14efe 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
14eff 65 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 20 20  e, pRight);.    
14f00 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50    codeCompare(pP
14f01 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 70 52 69  arse, pLeft, pRi
14f02 67 68 74 2c 20 4f 50 5f 4c 65 2c 20 64 65 73 74  ght, OP_Le, dest
14f03 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 0a  , jumpIfNull);..
14f04 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
14f05 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 74  eAddOp(v, OP_Int
14f06 65 67 65 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20  eger, 0, 0);.   
14f07 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
14f08 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b  mpHere(v, addr);
14f09 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
14f0a 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 50 6f  beAddOp(v, OP_Po
14f0b 70 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 20 20  p, 1, 0);.      
14f0c 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
14f0d 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20   default: {.    
14f0e 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
14f0f 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 29  e(pParse, pExpr)
14f10 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
14f11 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49  dbeAddOp(v, OP_I
14f12 66 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 2c 20 64  f, jumpIfNull, d
14f13 65 73 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61  est);.      brea
14f14 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70  k;.    }.  }.  p
14f15 50 61 72 73 65 2d 3e 63 6b 4f 66 66 73 65 74 20  Parse->ckOffset 
14f16 3d 20 63 6b 4f 66 66 73 65 74 3b 0a 7d 0a 0a 2f  = ckOffset;.}../
14f17 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
14f18 64 65 20 66 6f 72 20 61 20 62 6f 6f 6c 65 61 6e  de for a boolean
14f19 20 65 78 70 72 65 73 73 69 6f 6e 20 73 75 63 68   expression such
14f1a 20 74 68 61 74 20 61 20 6a 75 6d 70 20 69 73 20   that a jump is 
14f1b 6d 61 64 65 0a 2a 2a 20 74 6f 20 74 68 65 20 6c  made.** to the l
14f1c 61 62 65 6c 20 22 64 65 73 74 22 20 69 66 20 74  abel "dest" if t
14f1d 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
14f1e 20 66 61 6c 73 65 20 62 75 74 20 65 78 65 63 75   false but execu
14f1f 74 69 6f 6e 0a 2a 2a 20 63 6f 6e 74 69 6e 75 65  tion.** continue
14f20 73 20 73 74 72 61 69 67 68 74 20 74 68 72 75 20  s straight thru 
14f21 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  if the expressio
14f22 6e 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a  n is true..**.**
14f23 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69   If the expressi
14f24 6f 6e 20 65 76 61 6c 75 61 74 65 73 20 74 6f 20  on evaluates to 
14f25 4e 55 4c 4c 20 28 6e 65 69 74 68 65 72 20 74 72  NULL (neither tr
14f26 75 65 20 6e 6f 72 20 66 61 6c 73 65 29 20 74 68  ue nor false) th
14f27 65 6e 0a 2a 2a 20 6a 75 6d 70 20 69 66 20 6a 75  en.** jump if ju
14f28 6d 70 49 66 4e 75 6c 6c 20 69 73 20 74 72 75 65  mpIfNull is true
14f29 20 6f 72 20 66 61 6c 6c 20 74 68 72 6f 75 67 68   or fall through
14f2a 20 69 66 20 6a 75 6d 70 49 66 4e 75 6c 6c 20 69   if jumpIfNull i
14f2b 73 20 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49  s false..*/.SQLI
14f2c 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
14f2d 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c  sqlite3ExprIfFal
14f2e 73 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  se(Parse *pParse
14f2f 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69  , Expr *pExpr, i
14f30 6e 74 20 64 65 73 74 2c 20 69 6e 74 20 6a 75 6d  nt dest, int jum
14f31 70 49 66 4e 75 6c 6c 29 7b 0a 20 20 56 64 62 65  pIfNull){.  Vdbe
14f32 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
14f33 64 62 65 3b 0a 20 20 69 6e 74 20 6f 70 20 3d 20  dbe;.  int op = 
14f34 30 3b 0a 20 20 69 6e 74 20 63 6b 4f 66 66 73 65  0;.  int ckOffse
14f35 74 20 3d 20 70 50 61 72 73 65 2d 3e 63 6b 4f 66  t = pParse->ckOf
14f36 66 73 65 74 3b 0a 20 20 69 66 28 20 76 3d 3d 30  fset;.  if( v==0
14f37 20 7c 7c 20 70 45 78 70 72 3d 3d 30 20 29 20 72   || pExpr==0 ) r
14f38 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 54 68 65  eturn;..  /* The
14f39 20 76 61 6c 75 65 20 6f 66 20 70 45 78 70 72 2d   value of pExpr-
14f3a 3e 6f 70 20 61 6e 64 20 6f 70 20 61 72 65 20 72  >op and op are r
14f3b 65 6c 61 74 65 64 20 61 73 20 66 6f 6c 6c 6f 77  elated as follow
14f3c 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  s:.  **.  **    
14f3d 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 20 20 20     pExpr->op    
14f3e 20 20 20 20 20 20 20 20 6f 70 0a 20 20 2a 2a 20          op.  ** 
14f3f 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 20        --------- 
14f40 20 20 20 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d           -------
14f41 2d 2d 2d 0a 20 20 2a 2a 20 20 20 20 20 20 20 54  ---.  **       T
14f42 4b 5f 49 53 4e 55 4c 4c 20 20 20 20 20 20 20 20  K_ISNULL        
14f43 20 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 0a 20 20 2a    OP_NotNull.  *
14f44 2a 20 20 20 20 20 20 20 54 4b 5f 4e 4f 54 4e 55  *       TK_NOTNU
14f45 4c 4c 20 20 20 20 20 20 20 20 20 4f 50 5f 49 73  LL         OP_Is
14f46 4e 75 6c 6c 0a 20 20 2a 2a 20 20 20 20 20 20 20  Null.  **       
14f47 54 4b 5f 4e 45 20 20 20 20 20 20 20 20 20 20 20  TK_NE           
14f48 20 20 20 4f 50 5f 45 71 0a 20 20 2a 2a 20 20 20     OP_Eq.  **   
14f49 20 20 20 20 54 4b 5f 45 51 20 20 20 20 20 20 20      TK_EQ       
14f4a 20 20 20 20 20 20 20 4f 50 5f 4e 65 0a 20 20 2a         OP_Ne.  *
14f4b 2a 20 20 20 20 20 20 20 54 4b 5f 47 54 20 20 20  *       TK_GT   
14f4c 20 20 20 20 20 20 20 20 20 20 20 4f 50 5f 4c 65             OP_Le
14f4d 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 4c  .  **       TK_L
14f4e 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f  E              O
14f4f 50 5f 47 74 0a 20 20 2a 2a 20 20 20 20 20 20 20  P_Gt.  **       
14f50 54 4b 5f 47 45 20 20 20 20 20 20 20 20 20 20 20  TK_GE           
14f51 20 20 20 4f 50 5f 4c 74 0a 20 20 2a 2a 20 20 20     OP_Lt.  **   
14f52 20 20 20 20 54 4b 5f 4c 54 20 20 20 20 20 20 20      TK_LT       
14f53 20 20 20 20 20 20 20 4f 50 5f 47 65 0a 20 20 2a         OP_Ge.  *
14f54 2a 0a 20 20 2a 2a 20 46 6f 72 20 6f 74 68 65 72  *.  ** For other
14f55 20 76 61 6c 75 65 73 20 6f 66 20 70 45 78 70 72   values of pExpr
14f56 2d 3e 6f 70 2c 20 6f 70 20 69 73 20 75 6e 64 65  ->op, op is unde
14f57 66 69 6e 65 64 20 61 6e 64 20 75 6e 75 73 65 64  fined and unused
14f58 2e 0a 20 20 2a 2a 20 54 68 65 20 76 61 6c 75 65  ..  ** The value
14f59 20 6f 66 20 54 4b 5f 20 61 6e 64 20 4f 50 5f 20   of TK_ and OP_ 
14f5a 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 61 72  constants are ar
14f5b 72 61 6e 67 65 64 20 73 75 63 68 20 74 68 61 74  ranged such that
14f5c 20 77 65 0a 20 20 2a 2a 20 63 61 6e 20 63 6f 6d   we.  ** can com
14f5d 70 75 74 65 20 74 68 65 20 6d 61 70 70 69 6e 67  pute the mapping
14f5e 20 61 62 6f 76 65 20 75 73 69 6e 67 20 74 68 65   above using the
14f5f 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 78 70 72 65   following expre
14f60 73 73 69 6f 6e 2e 0a 20 20 2a 2a 20 41 73 73 65  ssion..  ** Asse
14f61 72 74 28 29 73 20 76 65 72 69 66 79 20 74 68 61  rt()s verify tha
14f62 74 20 74 68 65 20 63 6f 6d 70 75 74 61 74 69 6f  t the computatio
14f63 6e 20 69 73 20 63 6f 72 72 65 63 74 2e 0a 20 20  n is correct..  
14f64 2a 2f 0a 20 20 6f 70 20 3d 20 28 28 70 45 78 70  */.  op = ((pExp
14f65 72 2d 3e 6f 70 2b 28 54 4b 5f 49 53 4e 55 4c 4c  r->op+(TK_ISNULL
14f66 26 31 29 29 5e 31 29 2d 28 54 4b 5f 49 53 4e 55  &1))^1)-(TK_ISNU
14f67 4c 4c 26 31 29 3b 0a 0a 20 20 2f 2a 20 56 65 72  LL&1);..  /* Ver
14f68 69 66 79 20 63 6f 72 72 65 63 74 20 61 6c 69 67  ify correct alig
14f69 6e 6d 65 6e 74 20 6f 66 20 54 4b 5f 20 61 6e 64  nment of TK_ and
14f6a 20 4f 50 5f 20 63 6f 6e 73 74 61 6e 74 73 0a 20   OP_ constants. 
14f6b 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 45   */.  assert( pE
14f6c 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 49 53 4e 55  xpr->op!=TK_ISNU
14f6d 4c 4c 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4e 6f 74  LL || op==OP_Not
14f6e 4e 75 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72 74  Null );.  assert
14f6f 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f  ( pExpr->op!=TK_
14f70 4e 4f 54 4e 55 4c 4c 20 7c 7c 20 6f 70 3d 3d 4f  NOTNULL || op==O
14f71 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a 20 20 61 73  P_IsNull );.  as
14f72 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21  sert( pExpr->op!
14f73 3d 54 4b 5f 4e 45 20 7c 7c 20 6f 70 3d 3d 4f 50  =TK_NE || op==OP
14f74 5f 45 71 20 29 3b 0a 20 20 61 73 73 65 72 74 28  _Eq );.  assert(
14f75 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 45   pExpr->op!=TK_E
14f76 51 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4e 65 20 29  Q || op==OP_Ne )
14f77 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 70  ;.  assert( pExp
14f78 72 2d 3e 6f 70 21 3d 54 4b 5f 4c 54 20 7c 7c 20  r->op!=TK_LT || 
14f79 6f 70 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20 20 61  op==OP_Ge );.  a
14f7a 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70  ssert( pExpr->op
14f7b 21 3d 54 4b 5f 4c 45 20 7c 7c 20 6f 70 3d 3d 4f  !=TK_LE || op==O
14f7c 50 5f 47 74 20 29 3b 0a 20 20 61 73 73 65 72 74  P_Gt );.  assert
14f7d 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f  ( pExpr->op!=TK_
14f7e 47 54 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4c 65 20  GT || op==OP_Le 
14f7f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 78  );.  assert( pEx
14f80 70 72 2d 3e 6f 70 21 3d 54 4b 5f 47 45 20 7c 7c  pr->op!=TK_GE ||
14f81 20 6f 70 3d 3d 4f 50 5f 4c 74 20 29 3b 0a 0a 20   op==OP_Lt );.. 
14f82 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e   switch( pExpr->
14f83 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54  op ){.    case T
14f84 4b 5f 41 4e 44 3a 20 7b 0a 20 20 20 20 20 20 73  K_AND: {.      s
14f85 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73  qlite3ExprIfFals
14f86 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  e(pParse, pExpr-
14f87 3e 70 4c 65 66 74 2c 20 64 65 73 74 2c 20 6a 75  >pLeft, dest, ju
14f88 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20  mpIfNull);.     
14f89 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61   sqlite3ExprIfFa
14f8a 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45 78 70  lse(pParse, pExp
14f8b 72 2d 3e 70 52 69 67 68 74 2c 20 64 65 73 74 2c  r->pRight, dest,
14f8c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20   jumpIfNull);.  
14f8d 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
14f8e 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4f 52 3a  .    case TK_OR:
14f8f 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 32 20   {.      int d2 
14f90 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
14f91 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20  eLabel(v);.     
14f92 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 54 72   sqlite3ExprIfTr
14f93 75 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  ue(pParse, pExpr
14f94 2d 3e 70 4c 65 66 74 2c 20 64 32 2c 20 21 6a 75  ->pLeft, d2, !ju
14f95 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20  mpIfNull);.     
14f96 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61   sqlite3ExprIfFa
14f97 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45 78 70  lse(pParse, pExp
14f98 72 2d 3e 70 52 69 67 68 74 2c 20 64 65 73 74 2c  r->pRight, dest,
14f99 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20   jumpIfNull);.  
14f9a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52      sqlite3VdbeR
14f9b 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 64  esolveLabel(v, d
14f9c 32 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  2);.      break;
14f9d 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
14f9e 54 4b 5f 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 20  TK_NOT: {.      
14f9f 73 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75  sqlite3ExprIfTru
14fa0 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  e(pParse, pExpr-
14fa1 3e 70 4c 65 66 74 2c 20 64 65 73 74 2c 20 6a 75  >pLeft, dest, ju
14fa2 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20  mpIfNull);.     
14fa3 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
14fa4 20 20 63 61 73 65 20 54 4b 5f 4c 54 3a 0a 20 20    case TK_LT:.  
14fa5 20 20 63 61 73 65 20 54 4b 5f 4c 45 3a 0a 20 20    case TK_LE:.  
14fa6 20 20 63 61 73 65 20 54 4b 5f 47 54 3a 0a 20 20    case TK_GT:.  
14fa7 20 20 63 61 73 65 20 54 4b 5f 47 45 3a 0a 20 20    case TK_GE:.  
14fa8 20 20 63 61 73 65 20 54 4b 5f 4e 45 3a 0a 20 20    case TK_NE:.  
14fa9 20 20 63 61 73 65 20 54 4b 5f 45 51 3a 20 7b 0a    case TK_EQ: {.
14faa 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
14fab 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45  rCode(pParse, pE
14fac 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 20  xpr->pLeft);.   
14fad 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
14fae 64 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  de(pParse, pExpr
14faf 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 20 20 20  ->pRight);.     
14fb0 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61   codeCompare(pPa
14fb1 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
14fb2 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  t, pExpr->pRight
14fb3 2c 20 6f 70 2c 20 64 65 73 74 2c 20 6a 75 6d 70  , op, dest, jump
14fb4 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62  IfNull);.      b
14fb5 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
14fb6 63 61 73 65 20 54 4b 5f 49 53 4e 55 4c 4c 3a 0a  case TK_ISNULL:.
14fb7 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54 4e      case TK_NOTN
14fb8 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c  ULL: {.      sql
14fb9 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
14fba 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
14fbb 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  t);.      sqlite
14fbc 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 6f 70  3VdbeAddOp(v, op
14fbd 2c 20 31 2c 20 64 65 73 74 29 3b 0a 20 20 20 20  , 1, dest);.    
14fbe 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
14fbf 20 20 20 63 61 73 65 20 54 4b 5f 42 45 54 57 45     case TK_BETWE
14fc0 45 4e 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54  EN: {.      /* T
14fc1 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
14fc2 20 22 78 20 42 45 54 57 45 45 4e 20 79 20 41 4e   "x BETWEEN y AN
14fc3 44 20 7a 22 2e 20 49 74 20 69 73 20 69 6d 70 6c  D z". It is impl
14fc4 65 6d 65 6e 74 65 64 20 61 73 3a 0a 20 20 20 20  emented as:.    
14fc5 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 31 20    **.      ** 1 
14fc6 49 46 20 28 78 20 3e 3d 20 79 29 20 47 4f 54 4f  IF (x >= y) GOTO
14fc7 20 33 0a 20 20 20 20 20 20 2a 2a 20 32 20 47 4f   3.      ** 2 GO
14fc8 54 4f 20 3c 64 65 73 74 3e 0a 20 20 20 20 20 20  TO <dest>.      
14fc9 2a 2a 20 33 20 49 46 20 28 78 20 3e 20 7a 29 20  ** 3 IF (x > z) 
14fca 47 4f 54 4f 20 3c 64 65 73 74 3e 0a 20 20 20 20  GOTO <dest>.    
14fcb 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61    */.      int a
14fcc 64 64 72 3b 0a 20 20 20 20 20 20 45 78 70 72 20  ddr;.      Expr 
14fcd 2a 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e  *pLeft = pExpr->
14fce 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 45 78 70  pLeft;.      Exp
14fcf 72 20 2a 70 52 69 67 68 74 20 3d 20 70 45 78 70  r *pRight = pExp
14fd0 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70  r->pList->a[0].p
14fd1 45 78 70 72 3b 0a 20 20 20 20 20 20 73 71 6c 69  Expr;.      sqli
14fd2 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72  te3ExprCode(pPar
14fd3 73 65 2c 20 70 4c 65 66 74 29 3b 0a 20 20 20 20  se, pLeft);.    
14fd4 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
14fd5 4f 70 28 76 2c 20 4f 50 5f 44 75 70 2c 20 30 2c  Op(v, OP_Dup, 0,
14fd6 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   0);.      sqlit
14fd7 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
14fd8 65 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 20 20  e, pRight);.    
14fd9 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33    addr = sqlite3
14fda 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
14fdb 76 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f  v);.      codeCo
14fdc 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 4c  mpare(pParse, pL
14fdd 65 66 74 2c 20 70 52 69 67 68 74 2c 20 4f 50 5f  eft, pRight, OP_
14fde 47 65 2c 20 61 64 64 72 2b 33 2c 20 21 6a 75 6d  Ge, addr+3, !jum
14fdf 70 49 66 4e 75 6c 6c 29 3b 0a 0a 20 20 20 20 20  pIfNull);..     
14fe0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
14fe1 70 28 76 2c 20 4f 50 5f 50 6f 70 2c 20 31 2c 20  p(v, OP_Pop, 1, 
14fe2 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  0);.      sqlite
14fe3 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
14fe4 5f 47 6f 74 6f 2c 20 30 2c 20 64 65 73 74 29 3b  _Goto, 0, dest);
14fe5 0a 20 20 20 20 20 20 70 52 69 67 68 74 20 3d 20  .      pRight = 
14fe6 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b  pExpr->pList->a[
14fe7 31 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  1].pExpr;.      
14fe8 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
14fe9 70 50 61 72 73 65 2c 20 70 52 69 67 68 74 29 3b  pParse, pRight);
14fea 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61  .      codeCompa
14feb 72 65 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74  re(pParse, pLeft
14fec 2c 20 70 52 69 67 68 74 2c 20 4f 50 5f 47 74 2c  , pRight, OP_Gt,
14fed 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c   dest, jumpIfNul
14fee 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  l);.      break;
14fef 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75  .    }.    defau
14ff0 6c 74 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69  lt: {.      sqli
14ff1 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72  te3ExprCode(pPar
14ff2 73 65 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20  se, pExpr);.    
14ff3 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
14ff4 4f 70 28 76 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20  Op(v, OP_IfNot, 
14ff5 6a 75 6d 70 49 66 4e 75 6c 6c 2c 20 64 65 73 74  jumpIfNull, dest
14ff6 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
14ff7 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 50 61 72      }.  }.  pPar
14ff8 73 65 2d 3e 63 6b 4f 66 66 73 65 74 20 3d 20 63  se->ckOffset = c
14ff9 6b 4f 66 66 73 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  kOffset;.}../*.*
14ffa 2a 20 44 6f 20 61 20 64 65 65 70 20 63 6f 6d 70  * Do a deep comp
14ffb 61 72 69 73 6f 6e 20 6f 66 20 74 77 6f 20 65 78  arison of two ex
14ffc 70 72 65 73 73 69 6f 6e 20 74 72 65 65 73 2e 20  pression trees. 
14ffd 20 52 65 74 75 72 6e 20 54 52 55 45 20 28 6e 6f   Return TRUE (no
14ffe 6e 2d 7a 65 72 6f 29 0a 2a 2a 20 69 66 20 74 68  n-zero).** if th
14fff 65 79 20 61 72 65 20 69 64 65 6e 74 69 63 61 6c  ey are identical
15000 20 61 6e 64 20 72 65 74 75 72 6e 20 46 41 4c 53   and return FALS
15001 45 20 69 66 20 74 68 65 79 20 64 69 66 66 65 72  E if they differ
15002 20 69 6e 20 61 6e 79 20 77 61 79 2e 0a 2a 2a 0a   in any way..**.
15003 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 74 68 69  ** Sometimes thi
15004 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72  s routine will r
15005 65 74 75 72 6e 20 46 41 4c 53 45 20 65 76 65 6e  eturn FALSE even
15006 20 69 66 20 74 68 65 20 74 77 6f 20 65 78 70 72   if the two expr
15007 65 73 73 69 6f 6e 73 0a 2a 2a 20 72 65 61 6c 6c  essions.** reall
15008 79 20 61 72 65 20 65 71 75 69 76 61 6c 65 6e 74  y are equivalent
15009 2e 20 20 49 66 20 77 65 20 63 61 6e 6e 6f 74 20  .  If we cannot 
1500a 70 72 6f 76 65 20 74 68 61 74 20 74 68 65 20 65  prove that the e
1500b 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65 0a 2a  xpressions are.*
1500c 2a 20 69 64 65 6e 74 69 63 61 6c 2c 20 77 65 20  * identical, we 
1500d 72 65 74 75 72 6e 20 46 41 4c 53 45 20 6a 75 73  return FALSE jus
1500e 74 20 74 6f 20 62 65 20 73 61 66 65 2e 20 20 53  t to be safe.  S
1500f 6f 20 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e  o if this routin
15010 65 0a 2a 2a 20 72 65 74 75 72 6e 73 20 66 61 6c  e.** returns fal
15011 73 65 2c 20 74 68 65 6e 20 79 6f 75 20 64 6f 20  se, then you do 
15012 6e 6f 74 20 72 65 61 6c 6c 79 20 6b 6e 6f 77 20  not really know 
15013 66 6f 72 20 63 65 72 74 61 69 6e 20 69 66 20 74  for certain if t
15014 68 65 20 74 77 6f 0a 2a 2a 20 65 78 70 72 65 73  he two.** expres
15015 73 69 6f 6e 73 20 61 72 65 20 74 68 65 20 73 61  sions are the sa
15016 6d 65 2e 20 20 42 75 74 20 69 66 20 79 6f 75 20  me.  But if you 
15017 67 65 74 20 61 20 54 52 55 45 20 72 65 74 75 72  get a TRUE retur
15018 6e 2c 20 74 68 65 6e 20 79 6f 75 0a 2a 2a 20 63  n, then you.** c
15019 61 6e 20 62 65 20 73 75 72 65 20 74 68 65 20 65  an be sure the e
1501a 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65 20 74  xpressions are t
1501b 68 65 20 73 61 6d 65 2e 20 20 49 6e 20 74 68 65  he same.  In the
1501c 20 70 6c 61 63 65 73 20 77 68 65 72 65 0a 2a 2a   places where.**
1501d 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
1501e 20 75 73 65 64 2c 20 69 74 20 64 6f 65 73 20 6e   used, it does n
1501f 6f 74 20 68 75 72 74 20 74 6f 20 67 65 74 20 61  ot hurt to get a
15020 6e 20 65 78 74 72 61 20 46 41 4c 53 45 20 2d 20  n extra FALSE - 
15021 74 68 61 74 0a 2a 2a 20 6a 75 73 74 20 6d 69 67  that.** just mig
15022 68 74 20 72 65 73 75 6c 74 20 69 6e 20 73 6f 6d  ht result in som
15023 65 20 73 6c 69 67 68 74 6c 79 20 73 6c 6f 77 65  e slightly slowe
15024 72 20 63 6f 64 65 2e 20 20 42 75 74 20 72 65 74  r code.  But ret
15025 75 72 6e 69 6e 67 0a 2a 2a 20 61 6e 20 69 6e 63  urning.** an inc
15026 6f 72 72 65 63 74 20 54 52 55 45 20 63 6f 75 6c  orrect TRUE coul
15027 64 20 6c 65 61 64 20 74 6f 20 61 20 6d 61 6c 66  d lead to a malf
15028 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49  unction..*/.SQLI
15029 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1502a 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72  qlite3ExprCompar
1502b 65 28 45 78 70 72 20 2a 70 41 2c 20 45 78 70 72  e(Expr *pA, Expr
1502c 20 2a 70 42 29 7b 0a 20 20 69 6e 74 20 69 3b 0a   *pB){.  int i;.
1502d 20 20 69 66 28 20 70 41 3d 3d 30 7c 7c 70 42 3d    if( pA==0||pB=
1502e 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
1502f 20 70 42 3d 3d 70 41 3b 0a 20 20 7d 0a 20 20 69   pB==pA;.  }.  i
15030 66 28 20 70 41 2d 3e 6f 70 21 3d 70 42 2d 3e 6f  f( pA->op!=pB->o
15031 70 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  p ) return 0;.  
15032 69 66 28 20 28 70 41 2d 3e 66 6c 61 67 73 20 26  if( (pA->flags &
15033 20 45 50 5f 44 69 73 74 69 6e 63 74 29 21 3d 28   EP_Distinct)!=(
15034 70 42 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 44  pB->flags & EP_D
15035 69 73 74 69 6e 63 74 29 20 29 20 72 65 74 75 72  istinct) ) retur
15036 6e 20 30 3b 0a 20 20 69 66 28 20 21 73 71 6c 69  n 0;.  if( !sqli
15037 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 70  te3ExprCompare(p
15038 41 2d 3e 70 4c 65 66 74 2c 20 70 42 2d 3e 70 4c  A->pLeft, pB->pL
15039 65 66 74 29 20 29 20 72 65 74 75 72 6e 20 30 3b  eft) ) return 0;
1503a 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 45  .  if( !sqlite3E
1503b 78 70 72 43 6f 6d 70 61 72 65 28 70 41 2d 3e 70  xprCompare(pA->p
1503c 52 69 67 68 74 2c 20 70 42 2d 3e 70 52 69 67 68  Right, pB->pRigh
1503d 74 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  t) ) return 0;. 
1503e 20 69 66 28 20 70 41 2d 3e 70 4c 69 73 74 20 29   if( pA->pList )
1503f 7b 0a 20 20 20 20 69 66 28 20 70 42 2d 3e 70 4c  {.    if( pB->pL
15040 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ist==0 ) return 
15041 30 3b 0a 20 20 20 20 69 66 28 20 70 41 2d 3e 70  0;.    if( pA->p
15042 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 70 42 2d  List->nExpr!=pB-
15043 3e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 20  >pList->nExpr ) 
15044 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 66 6f  return 0;.    fo
15045 72 28 69 3d 30 3b 20 69 3c 70 41 2d 3e 70 4c 69  r(i=0; i<pA->pLi
15046 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b  st->nExpr; i++){
15047 0a 20 20 20 20 20 20 69 66 28 20 21 73 71 6c 69  .      if( !sqli
15048 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 70  te3ExprCompare(p
15049 41 2d 3e 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70  A->pList->a[i].p
1504a 45 78 70 72 2c 20 70 42 2d 3e 70 4c 69 73 74 2d  Expr, pB->pList-
1504b 3e 61 5b 69 5d 2e 70 45 78 70 72 29 20 29 7b 0a  >a[i].pExpr) ){.
1504c 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30          return 0
1504d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1504e 20 20 7d 65 6c 73 65 20 69 66 28 20 70 42 2d 3e    }else if( pB->
1504f 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 72 65 74  pList ){.    ret
15050 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28  urn 0;.  }.  if(
15051 20 70 41 2d 3e 70 53 65 6c 65 63 74 20 7c 7c 20   pA->pSelect || 
15052 70 42 2d 3e 70 53 65 6c 65 63 74 20 29 20 72 65  pB->pSelect ) re
15053 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 41  turn 0;.  if( pA
15054 2d 3e 69 54 61 62 6c 65 21 3d 70 42 2d 3e 69 54  ->iTable!=pB->iT
15055 61 62 6c 65 20 7c 7c 20 70 41 2d 3e 69 43 6f 6c  able || pA->iCol
15056 75 6d 6e 21 3d 70 42 2d 3e 69 43 6f 6c 75 6d 6e  umn!=pB->iColumn
15057 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69   ) return 0;.  i
15058 66 28 20 70 41 2d 3e 6f 70 21 3d 54 4b 5f 43 4f  f( pA->op!=TK_CO
15059 4c 55 4d 4e 20 26 26 20 70 41 2d 3e 74 6f 6b 65  LUMN && pA->toke
1505a 6e 2e 7a 20 29 7b 0a 20 20 20 20 69 66 28 20 70  n.z ){.    if( p
1505b 42 2d 3e 74 6f 6b 65 6e 2e 7a 3d 3d 30 20 29 20  B->token.z==0 ) 
1505c 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 69 66  return 0;.    if
1505d 28 20 70 42 2d 3e 74 6f 6b 65 6e 2e 6e 21 3d 70  ( pB->token.n!=p
1505e 41 2d 3e 74 6f 6b 65 6e 2e 6e 20 29 20 72 65 74  A->token.n ) ret
1505f 75 72 6e 20 30 3b 0a 20 20 20 20 69 66 28 20 73  urn 0;.    if( s
15060 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 28  qlite3StrNICmp((
15061 63 68 61 72 2a 29 70 41 2d 3e 74 6f 6b 65 6e 2e  char*)pA->token.
15062 7a 2c 28 63 68 61 72 2a 29 70 42 2d 3e 74 6f 6b  z,(char*)pB->tok
15063 65 6e 2e 7a 2c 70 42 2d 3e 74 6f 6b 65 6e 2e 6e  en.z,pB->token.n
15064 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65  )!=0 ){.      re
15065 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20  turn 0;.    }.  
15066 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a  }.  return 1;.}.
15067 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65  ../*.** Add a ne
15068 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68 65  w element to the
15069 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b   pAggInfo->aCol[
1506a 5d 20 61 72 72 61 79 2e 20 20 52 65 74 75 72 6e  ] array.  Return
1506b 20 74 68 65 20 69 6e 64 65 78 20 6f 66 0a 2a 2a   the index of.**
1506c 20 74 68 65 20 6e 65 77 20 65 6c 65 6d 65 6e 74   the new element
1506d 2e 20 20 52 65 74 75 72 6e 20 61 20 6e 65 67 61  .  Return a nega
1506e 74 69 76 65 20 6e 75 6d 62 65 72 20 69 66 20 6d  tive number if m
1506f 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a  alloc fails..*/.
15070 73 74 61 74 69 63 20 69 6e 74 20 61 64 64 41 67  static int addAg
15071 67 49 6e 66 6f 43 6f 6c 75 6d 6e 28 41 67 67 49  gInfoColumn(AggI
15072 6e 66 6f 20 2a 70 49 6e 66 6f 29 7b 0a 20 20 69  nfo *pInfo){.  i
15073 6e 74 20 69 3b 0a 20 20 70 49 6e 66 6f 2d 3e 61  nt i;.  pInfo->a
15074 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 41 72 72  Col = sqlite3Arr
15075 61 79 41 6c 6c 6f 63 61 74 65 28 0a 20 20 20 20  ayAllocate(.    
15076 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6c 2c 0a     pInfo->aCol,.
15077 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 70 49         sizeof(pI
15078 6e 66 6f 2d 3e 61 43 6f 6c 5b 30 5d 29 2c 0a 20  nfo->aCol[0]),. 
15079 20 20 20 20 20 20 33 2c 0a 20 20 20 20 20 20 20        3,.       
1507a 26 70 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 2c  &pInfo->nColumn,
1507b 0a 20 20 20 20 20 20 20 26 70 49 6e 66 6f 2d 3e  .       &pInfo->
1507c 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f 63 2c 0a 20 20  nColumnAlloc,.  
1507d 20 20 20 20 20 26 69 0a 20 20 29 3b 0a 20 20 72       &i.  );.  r
1507e 65 74 75 72 6e 20 69 3b 0a 7d 20 20 20 20 0a 0a  eturn i;.}    ..
1507f 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 20  /*.** Add a new 
15080 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68 65 20 70  element to the p
15081 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 5d  AggInfo->aFunc[]
15082 20 61 72 72 61 79 2e 20 20 52 65 74 75 72 6e 20   array.  Return 
15083 74 68 65 20 69 6e 64 65 78 20 6f 66 0a 2a 2a 20  the index of.** 
15084 74 68 65 20 6e 65 77 20 65 6c 65 6d 65 6e 74 2e  the new element.
15085 20 20 52 65 74 75 72 6e 20 61 20 6e 65 67 61 74    Return a negat
15086 69 76 65 20 6e 75 6d 62 65 72 20 69 66 20 6d 61  ive number if ma
15087 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73  lloc fails..*/.s
15088 74 61 74 69 63 20 69 6e 74 20 61 64 64 41 67 67  tatic int addAgg
15089 49 6e 66 6f 46 75 6e 63 28 41 67 67 49 6e 66 6f  InfoFunc(AggInfo
1508a 20 2a 70 49 6e 66 6f 29 7b 0a 20 20 69 6e 74 20   *pInfo){.  int 
1508b 69 3b 0a 20 20 70 49 6e 66 6f 2d 3e 61 46 75 6e  i;.  pInfo->aFun
1508c 63 20 3d 20 73 71 6c 69 74 65 33 41 72 72 61 79  c = sqlite3Array
1508d 41 6c 6c 6f 63 61 74 65 28 0a 20 20 20 20 20 20  Allocate(.      
1508e 20 70 49 6e 66 6f 2d 3e 61 46 75 6e 63 2c 0a 20   pInfo->aFunc,. 
1508f 20 20 20 20 20 20 73 69 7a 65 6f 66 28 70 49 6e        sizeof(pIn
15090 66 6f 2d 3e 61 46 75 6e 63 5b 30 5d 29 2c 0a 20  fo->aFunc[0]),. 
15091 20 20 20 20 20 20 33 2c 0a 20 20 20 20 20 20 20        3,.       
15092 26 70 49 6e 66 6f 2d 3e 6e 46 75 6e 63 2c 0a 20  &pInfo->nFunc,. 
15093 20 20 20 20 20 20 26 70 49 6e 66 6f 2d 3e 6e 46        &pInfo->nF
15094 75 6e 63 41 6c 6c 6f 63 2c 0a 20 20 20 20 20 20  uncAlloc,.      
15095 20 26 69 0a 20 20 29 3b 0a 20 20 72 65 74 75 72   &i.  );.  retur
15096 6e 20 69 3b 0a 7d 20 20 20 20 0a 0a 2f 2a 0a 2a  n i;.}    ../*.*
15097 2a 20 54 68 69 73 20 69 73 20 61 6e 20 78 46 75  * This is an xFu
15098 6e 63 20 66 6f 72 20 77 61 6c 6b 45 78 70 72 54  nc for walkExprT
15099 72 65 65 28 29 20 75 73 65 64 20 74 6f 20 69 6d  ree() used to im
1509a 70 6c 65 6d 65 6e 74 20 0a 2a 2a 20 73 71 6c 69  plement .** sqli
1509b 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67  te3ExprAnalyzeAg
1509c 67 72 65 67 61 74 65 73 28 29 2e 20 20 53 65 65  gregates().  See
1509d 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c   sqlite3ExprAnal
1509e 79 7a 65 41 67 67 72 65 67 61 74 65 73 0a 2a 2a  yzeAggregates.**
1509f 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20   for additional 
150a0 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a  information..**.
150a1 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
150a2 61 6e 61 6c 79 7a 65 73 20 74 68 65 20 61 67 67  analyzes the agg
150a3 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20  regate function 
150a4 61 74 20 70 45 78 70 72 2e 0a 2a 2f 0a 73 74 61  at pExpr..*/.sta
150a5 74 69 63 20 69 6e 74 20 61 6e 61 6c 79 7a 65 41  tic int analyzeA
150a6 67 67 72 65 67 61 74 65 28 76 6f 69 64 20 2a 70  ggregate(void *p
150a7 41 72 67 2c 20 45 78 70 72 20 2a 70 45 78 70 72  Arg, Expr *pExpr
150a8 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4e 61  ){.  int i;.  Na
150a9 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 20 3d  meContext *pNC =
150aa 20 28 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 29   (NameContext *)
150ab 70 41 72 67 3b 0a 20 20 50 61 72 73 65 20 2a 70  pArg;.  Parse *p
150ac 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61  Parse = pNC->pPa
150ad 72 73 65 3b 0a 20 20 53 72 63 4c 69 73 74 20 2a  rse;.  SrcList *
150ae 70 53 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e  pSrcList = pNC->
150af 70 53 72 63 4c 69 73 74 3b 0a 20 20 41 67 67 49  pSrcList;.  AggI
150b0 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 20 3d 20  nfo *pAggInfo = 
150b1 70 4e 43 2d 3e 70 41 67 67 49 6e 66 6f 3b 0a 20  pNC->pAggInfo;. 
150b2 20 0a 0a 20 20 73 77 69 74 63 68 28 20 70 45 78   ..  switch( pEx
150b3 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61  pr->op ){.    ca
150b4 73 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e  se TK_AGG_COLUMN
150b5 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f  :.    case TK_CO
150b6 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20 20 20 2f 2a  LUMN: {.      /*
150b7 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   Check to see if
150b8 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 69   the column is i
150b9 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 74 61 62  n one of the tab
150ba 6c 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 0a  les in the FROM.
150bb 20 20 20 20 20 20 2a 2a 20 63 6c 61 75 73 65 20        ** clause 
150bc 6f 66 20 74 68 65 20 61 67 67 72 65 67 61 74 65  of the aggregate
150bd 20 71 75 65 72 79 20 2a 2f 0a 20 20 20 20 20 20   query */.      
150be 69 66 28 20 70 53 72 63 4c 69 73 74 20 29 7b 0a  if( pSrcList ){.
150bf 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20 53          struct S
150c0 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  rcList_item *pIt
150c1 65 6d 20 3d 20 70 53 72 63 4c 69 73 74 2d 3e 61  em = pSrcList->a
150c2 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d  ;.        for(i=
150c3 30 3b 20 69 3c 70 53 72 63 4c 69 73 74 2d 3e 6e  0; i<pSrcList->n
150c4 53 72 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b  Src; i++, pItem+
150c5 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 74  +){.          st
150c6 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 63 6f 6c  ruct AggInfo_col
150c7 20 2a 70 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20   *pCol;.        
150c8 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 54 61    if( pExpr->iTa
150c9 62 6c 65 3d 3d 70 49 74 65 6d 2d 3e 69 43 75 72  ble==pItem->iCur
150ca 73 6f 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20  sor ){.         
150cb 20 20 20 2f 2a 20 49 66 20 77 65 20 72 65 61 63     /* If we reac
150cc 68 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 69 74  h this point, it
150cd 20 6d 65 61 6e 73 20 74 68 61 74 20 70 45 78 70   means that pExp
150ce 72 20 72 65 66 65 72 73 20 74 6f 20 61 20 74 61  r refers to a ta
150cf 62 6c 65 0a 20 20 20 20 20 20 20 20 20 20 20 20  ble.            
150d0 2a 2a 20 74 68 61 74 20 69 73 20 69 6e 20 74 68  ** that is in th
150d1 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66  e FROM clause of
150d2 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 71   the aggregate q
150d3 75 65 72 79 2e 20 20 0a 20 20 20 20 20 20 20 20  uery.  .        
150d4 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 20      **.         
150d5 20 20 20 2a 2a 20 4d 61 6b 65 20 61 6e 20 65 6e     ** Make an en
150d6 74 72 79 20 66 6f 72 20 74 68 65 20 63 6f 6c 75  try for the colu
150d7 6d 6e 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e  mn in pAggInfo->
150d8 61 43 6f 6c 5b 5d 20 69 66 20 74 68 65 72 65 0a  aCol[] if there.
150d9 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69              ** i
150da 73 20 6e 6f 74 20 61 6e 20 65 6e 74 72 79 20 74  s not an entry t
150db 68 65 72 65 20 61 6c 72 65 61 64 79 2e 0a 20 20  here already..  
150dc 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20            */.   
150dd 20 20 20 20 20 20 20 20 20 69 6e 74 20 6b 3b 0a           int k;.
150de 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c              pCol
150df 20 3d 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f   = pAggInfo->aCo
150e0 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 66  l;.            f
150e1 6f 72 28 6b 3d 30 3b 20 6b 3c 70 41 67 67 49 6e  or(k=0; k<pAggIn
150e2 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6b 2b 2b  fo->nColumn; k++
150e3 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20  , pCol++){.     
150e4 20 20 20 20 20 20 20 20 20 69 66 28 20 70 43 6f           if( pCo
150e5 6c 2d 3e 69 54 61 62 6c 65 3d 3d 70 45 78 70 72  l->iTable==pExpr
150e6 2d 3e 69 54 61 62 6c 65 20 26 26 0a 20 20 20 20  ->iTable &&.    
150e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43                pC
150e8 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 70 45 78  ol->iColumn==pEx
150e9 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20  pr->iColumn ){. 
150ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
150eb 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
150ec 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
150ed 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
150ee 69 66 28 20 6b 3e 3d 70 41 67 67 49 6e 66 6f 2d  if( k>=pAggInfo-
150ef 3e 6e 43 6f 6c 75 6d 6e 20 26 26 20 28 6b 20 3d  >nColumn && (k =
150f0 20 61 64 64 41 67 67 49 6e 66 6f 43 6f 6c 75 6d   addAggInfoColum
150f1 6e 28 70 41 67 67 49 6e 66 6f 29 29 3e 3d 30 20  n(pAggInfo))>=0 
150f2 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
150f3 20 70 43 6f 6c 20 3d 20 26 70 41 67 67 49 6e 66   pCol = &pAggInf
150f4 6f 2d 3e 61 43 6f 6c 5b 6b 5d 3b 0a 20 20 20 20  o->aCol[k];.    
150f5 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e            pCol->
150f6 70 54 61 62 20 3d 20 70 45 78 70 72 2d 3e 70 54  pTab = pExpr->pT
150f7 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ab;.            
150f8 20 20 70 43 6f 6c 2d 3e 69 54 61 62 6c 65 20 3d    pCol->iTable =
150f9 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3b 0a   pExpr->iTable;.
150fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43                pC
150fb 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 70 45  ol->iColumn = pE
150fc 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20  xpr->iColumn;.  
150fd 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c              pCol
150fe 2d 3e 69 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d  ->iMem = pParse-
150ff 3e 6e 4d 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 20  >nMem++;.       
15100 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 53 6f         pCol->iSo
15101 72 74 65 72 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b  rterColumn = -1;
15102 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
15103 43 6f 6c 2d 3e 70 45 78 70 72 20 3d 20 70 45 78  Col->pExpr = pEx
15104 70 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  pr;.            
15105 20 20 69 66 28 20 70 41 67 67 49 6e 66 6f 2d 3e    if( pAggInfo->
15106 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 20  pGroupBy ){.    
15107 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20              int 
15108 6a 2c 20 6e 3b 0a 20 20 20 20 20 20 20 20 20 20  j, n;.          
15109 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a        ExprList *
1510a 70 47 42 20 3d 20 70 41 67 67 49 6e 66 6f 2d 3e  pGB = pAggInfo->
1510b 70 47 72 6f 75 70 42 79 3b 0a 20 20 20 20 20 20  pGroupBy;.      
1510c 20 20 20 20 20 20 20 20 20 20 73 74 72 75 63 74            struct
1510d 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
1510e 70 54 65 72 6d 20 3d 20 70 47 42 2d 3e 61 3b 0a  pTerm = pGB->a;.
1510f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15110 6e 20 3d 20 70 47 42 2d 3e 6e 45 78 70 72 3b 0a  n = pGB->nExpr;.
15111 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15112 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 3b 20 6a 2b  for(j=0; j<n; j+
15113 2b 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20  +, pTerm++){.   
15114 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45                 E
15115 78 70 72 20 2a 70 45 20 3d 20 70 54 65 72 6d 2d  xpr *pE = pTerm-
15116 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20  >pExpr;.        
15117 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 45            if( pE
15118 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20  ->op==TK_COLUMN 
15119 26 26 20 70 45 2d 3e 69 54 61 62 6c 65 3d 3d 70  && pE->iTable==p
1511a 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 26 26 0a  Expr->iTable &&.
1511b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1511c 20 20 20 20 20 20 70 45 2d 3e 69 43 6f 6c 75 6d        pE->iColum
1511d 6e 3d 3d 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d  n==pExpr->iColum
1511e 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  n ){.           
1511f 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69           pCol->i
15120 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 20 3d 20 6a  SorterColumn = j
15121 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
15122 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
15123 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d                 }
15124 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
15125 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   }.             
15126 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   }.             
15127 20 69 66 28 20 70 43 6f 6c 2d 3e 69 53 6f 72 74   if( pCol->iSort
15128 65 72 43 6f 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20  erColumn<0 ){.  
15129 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43                pC
1512a 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d  ol->iSorterColum
1512b 6e 20 3d 20 70 41 67 67 49 6e 66 6f 2d 3e 6e 53  n = pAggInfo->nS
1512c 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 2b 2b 3b 0a  ortingColumn++;.
1512d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a                }.
1512e 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
1512f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
15130 72 65 20 69 73 20 6e 6f 77 20 61 6e 20 65 6e 74  re is now an ent
15131 72 79 20 66 6f 72 20 70 45 78 70 72 20 69 6e 20  ry for pExpr in 
15132 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d  pAggInfo->aCol[]
15133 20 28 65 69 74 68 65 72 0a 20 20 20 20 20 20 20   (either.       
15134 20 20 20 20 20 2a 2a 20 62 65 63 61 75 73 65 20       ** because 
15135 69 74 20 77 61 73 20 74 68 65 72 65 20 62 65 66  it was there bef
15136 6f 72 65 20 6f 72 20 62 65 63 61 75 73 65 20 77  ore or because w
15137 65 20 6a 75 73 74 20 63 72 65 61 74 65 64 20 69  e just created i
15138 74 29 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20  t)..            
15139 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 70  ** Convert the p
1513a 45 78 70 72 20 74 6f 20 62 65 20 61 20 54 4b 5f  Expr to be a TK_
1513b 41 47 47 5f 43 4f 4c 55 4d 4e 20 72 65 66 65 72  AGG_COLUMN refer
1513c 72 69 6e 67 20 74 6f 20 74 68 61 74 0a 20 20 20  ring to that.   
1513d 20 20 20 20 20 20 20 20 20 2a 2a 20 70 41 67 67           ** pAgg
1513e 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 65 6e 74  Info->aCol[] ent
1513f 72 79 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20  ry..            
15140 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  */.            p
15141 45 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f 20 3d  Expr->pAggInfo =
15142 20 70 41 67 67 49 6e 66 6f 3b 0a 20 20 20 20 20   pAggInfo;.     
15143 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70         pExpr->op
15144 20 3d 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e   = TK_AGG_COLUMN
15145 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 45  ;.            pE
15146 78 70 72 2d 3e 69 41 67 67 20 3d 20 6b 3b 0a 20  xpr->iAgg = k;. 
15147 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
15148 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 20 2f 2a  ;.          } /*
15149 20 65 6e 64 69 66 20 70 45 78 70 72 2d 3e 69 54   endif pExpr->iT
1514a 61 62 6c 65 3d 3d 70 49 74 65 6d 2d 3e 69 43 75  able==pItem->iCu
1514b 72 73 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20  rsor */.        
1514c 7d 20 2f 2a 20 65 6e 64 20 6c 6f 6f 70 20 6f 76  } /* end loop ov
1514d 65 72 20 70 53 72 63 4c 69 73 74 20 2a 2f 0a 20  er pSrcList */. 
1514e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74       }.      ret
1514f 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
15150 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e   case TK_AGG_FUN
15151 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 2f  CTION: {.      /
15152 2a 20 54 68 65 20 70 4e 43 2d 3e 6e 44 65 70 74  * The pNC->nDept
15153 68 3d 3d 30 20 74 65 73 74 20 63 61 75 73 65 73  h==0 test causes
15154 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
15155 69 6f 6e 73 20 69 6e 20 73 75 62 71 75 65 72 69  ions in subqueri
15156 65 73 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 62  es.      ** to b
15157 65 20 69 67 6e 6f 72 65 64 20 2a 2f 0a 20 20 20  e ignored */.   
15158 20 20 20 69 66 28 20 70 4e 43 2d 3e 6e 44 65 70     if( pNC->nDep
15159 74 68 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  th==0 ){.       
1515a 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65   /* Check to see
1515b 20 69 66 20 70 45 78 70 72 20 69 73 20 61 20 64   if pExpr is a d
1515c 75 70 6c 69 63 61 74 65 20 6f 66 20 61 6e 6f 74  uplicate of anot
1515d 68 65 72 20 61 67 67 72 65 67 61 74 65 20 0a 20  her aggregate . 
1515e 20 20 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 69         ** functi
1515f 6f 6e 20 74 68 61 74 20 69 73 20 61 6c 72 65 61  on that is alrea
15160 64 79 20 69 6e 20 74 68 65 20 70 41 67 67 49 6e  dy in the pAggIn
15161 66 6f 20 73 74 72 75 63 74 75 72 65 0a 20 20 20  fo structure.   
15162 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
15163 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 66  struct AggInfo_f
15164 75 6e 63 20 2a 70 49 74 65 6d 20 3d 20 70 41 67  unc *pItem = pAg
15165 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 3b 0a 20 20  gInfo->aFunc;.  
15166 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
15167 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63  <pAggInfo->nFunc
15168 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b  ; i++, pItem++){
15169 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73  .          if( s
1516a 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72  qlite3ExprCompar
1516b 65 28 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20  e(pItem->pExpr, 
1516c 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20  pExpr) ){.      
1516d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1516e 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1516f 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 69   }.        if( i
15170 3e 3d 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e  >=pAggInfo->nFun
15171 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f  c ){.          /
15172 2a 20 70 45 78 70 72 20 69 73 20 6f 72 69 67 69  * pExpr is origi
15173 6e 61 6c 2e 20 20 4d 61 6b 65 20 61 20 6e 65 77  nal.  Make a new
15174 20 65 6e 74 72 79 20 69 6e 20 70 41 67 67 49 6e   entry in pAggIn
15175 66 6f 2d 3e 61 46 75 6e 63 5b 5d 0a 20 20 20 20  fo->aFunc[].    
15176 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
15177 20 20 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28     u8 enc = ENC(
15178 70 50 61 72 73 65 2d 3e 64 62 29 3b 0a 20 20 20  pParse->db);.   
15179 20 20 20 20 20 20 20 69 20 3d 20 61 64 64 41 67         i = addAg
1517a 67 49 6e 66 6f 46 75 6e 63 28 70 41 67 67 49 6e  gInfoFunc(pAggIn
1517b 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  fo);.          i
1517c 66 28 20 69 3e 3d 30 20 29 7b 0a 20 20 20 20 20  f( i>=0 ){.     
1517d 20 20 20 20 20 20 20 70 49 74 65 6d 20 3d 20 26         pItem = &
1517e 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b  pAggInfo->aFunc[
1517f 69 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  i];.            
15180 70 49 74 65 6d 2d 3e 70 45 78 70 72 20 3d 20 70  pItem->pExpr = p
15181 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 20 20  Expr;.          
15182 20 20 70 49 74 65 6d 2d 3e 69 4d 65 6d 20 3d 20    pItem->iMem = 
15183 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 2b 3b 0a  pParse->nMem++;.
15184 20 20 20 20 20 20 20 20 20 20 20 20 70 49 74 65              pIte
15185 6d 2d 3e 70 46 75 6e 63 20 3d 20 73 71 6c 69 74  m->pFunc = sqlit
15186 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 70  e3FindFunction(p
15187 50 61 72 73 65 2d 3e 64 62 2c 0a 20 20 20 20 20  Parse->db,.     
15188 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63                (c
15189 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65  har*)pExpr->toke
1518a 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b 65  n.z, pExpr->toke
1518b 6e 2e 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20  n.n,.           
1518c 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70          pExpr->p
1518d 4c 69 73 74 20 3f 20 70 45 78 70 72 2d 3e 70 4c  List ? pExpr->pL
1518e 69 73 74 2d 3e 6e 45 78 70 72 20 3a 20 30 2c 20  ist->nExpr : 0, 
1518f 65 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  enc, 0);.       
15190 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e       if( pExpr->
15191 66 6c 61 67 73 20 26 20 45 50 5f 44 69 73 74 69  flags & EP_Disti
15192 6e 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20  nct ){.         
15193 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 44 69 73       pItem->iDis
15194 74 69 6e 63 74 20 3d 20 70 50 61 72 73 65 2d 3e  tinct = pParse->
15195 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20 20  nTab++;.        
15196 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
15197 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e           pItem->
15198 69 44 69 73 74 69 6e 63 74 20 3d 20 2d 31 3b 0a  iDistinct = -1;.
15199 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
1519a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1519b 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 4d    }.        /* M
1519c 61 6b 65 20 70 45 78 70 72 20 70 6f 69 6e 74 20  ake pExpr point 
1519d 74 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  to the appropria
1519e 74 65 20 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75  te pAggInfo->aFu
1519f 6e 63 5b 5d 20 65 6e 74 72 79 0a 20 20 20 20 20  nc[] entry.     
151a0 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 45     */.        pE
151a1 78 70 72 2d 3e 69 41 67 67 20 3d 20 69 3b 0a 20  xpr->iAgg = i;. 
151a2 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 41         pExpr->pA
151a3 67 67 49 6e 66 6f 20 3d 20 70 41 67 67 49 6e 66  ggInfo = pAggInf
151a4 6f 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  o;.        retur
151a5 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 1;.      }.   
151a6 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 63   }.  }..  /* Rec
151a7 75 72 73 69 76 65 6c 79 20 77 61 6c 6b 20 73 75  ursively walk su
151a8 62 71 75 65 72 69 65 73 20 6c 6f 6f 6b 69 6e 67  bqueries looking
151a9 20 66 6f 72 20 54 4b 5f 43 4f 4c 55 4d 4e 20 6e   for TK_COLUMN n
151aa 6f 64 65 73 20 74 68 61 74 20 6e 65 65 64 0a 20  odes that need. 
151ab 20 2a 2a 20 74 6f 20 62 65 20 63 68 61 6e 67 65   ** to be change
151ac 64 20 74 6f 20 54 4b 5f 41 47 47 5f 43 4f 4c 55  d to TK_AGG_COLU
151ad 4d 4e 2e 20 20 42 75 74 20 69 6e 63 72 65 6d 65  MN.  But increme
151ae 6e 74 20 6e 44 65 70 74 68 20 73 6f 20 74 68 61  nt nDepth so tha
151af 74 0a 20 20 2a 2a 20 54 4b 5f 41 47 47 5f 46 55  t.  ** TK_AGG_FU
151b0 4e 43 54 49 4f 4e 20 6e 6f 64 65 73 20 69 6e 20  NCTION nodes in 
151b1 73 75 62 71 75 65 72 69 65 73 20 77 69 6c 6c 20  subqueries will 
151b2 62 65 20 75 6e 63 68 61 6e 67 65 64 2e 0a 20 20  be unchanged..  
151b3 2a 2f 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e  */.  if( pExpr->
151b4 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 70  pSelect ){.    p
151b5 4e 43 2d 3e 6e 44 65 70 74 68 2b 2b 3b 0a 20 20  NC->nDepth++;.  
151b6 20 20 77 61 6c 6b 53 65 6c 65 63 74 45 78 70 72    walkSelectExpr
151b7 28 70 45 78 70 72 2d 3e 70 53 65 6c 65 63 74 2c  (pExpr->pSelect,
151b8 20 61 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74   analyzeAggregat
151b9 65 2c 20 70 4e 43 29 3b 0a 20 20 20 20 70 4e 43  e, pNC);.    pNC
151ba 2d 3e 6e 44 65 70 74 68 2d 2d 3b 0a 20 20 7d 0a  ->nDepth--;.  }.
151bb 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
151bc 2a 0a 2a 2a 20 41 6e 61 6c 79 7a 65 20 74 68 65  *.** Analyze the
151bd 20 67 69 76 65 6e 20 65 78 70 72 65 73 73 69 6f   given expressio
151be 6e 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 67  n looking for ag
151bf 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
151c0 73 20 61 6e 64 0a 2a 2a 20 66 6f 72 20 76 61 72  s and.** for var
151c1 69 61 62 6c 65 73 20 74 68 61 74 20 6e 65 65 64  iables that need
151c2 20 74 6f 20 62 65 20 61 64 64 65 64 20 74 6f 20   to be added to 
151c3 74 68 65 20 70 50 61 72 73 65 2d 3e 61 41 67 67  the pParse->aAgg
151c4 5b 5d 20 61 72 72 61 79 2e 0a 2a 2a 20 4d 61 6b  [] array..** Mak
151c5 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 65 6e 74  e additional ent
151c6 72 69 65 73 20 74 6f 20 74 68 65 20 70 50 61 72  ries to the pPar
151c7 73 65 2d 3e 61 41 67 67 5b 5d 20 61 72 72 61 79  se->aAgg[] array
151c8 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a   as necessary..*
151c9 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
151ca 65 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 62 65  e should only be
151cb 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 74 68   called after th
151cc 65 20 65 78 70 72 65 73 73 69 6f 6e 20 68 61 73  e expression has
151cd 20 62 65 65 6e 0a 2a 2a 20 61 6e 61 6c 79 7a 65   been.** analyze
151ce 64 20 62 79 20 73 71 6c 69 74 65 33 45 78 70 72  d by sqlite3Expr
151cf 52 65 73 6f 6c 76 65 4e 61 6d 65 73 28 29 2e 0a  ResolveNames()..
151d0 2a 2a 0a 2a 2a 20 49 66 20 65 72 72 6f 72 73 20  **.** If errors 
151d1 61 72 65 20 73 65 65 6e 2c 20 6c 65 61 76 65 20  are seen, leave 
151d2 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
151d3 20 69 6e 20 7a 45 72 72 4d 73 67 20 61 6e 64 20   in zErrMsg and 
151d4 72 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 6e 75  return.** the nu
151d5 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 2e 0a  mber of errors..
151d6 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
151d7 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
151d8 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74  rAnalyzeAggregat
151d9 65 73 28 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a  es(NameContext *
151da 70 4e 43 2c 20 45 78 70 72 20 2a 70 45 78 70 72  pNC, Expr *pExpr
151db 29 7b 0a 20 20 69 6e 74 20 6e 45 72 72 20 3d 20  ){.  int nErr = 
151dc 70 4e 43 2d 3e 70 50 61 72 73 65 2d 3e 6e 45 72  pNC->pParse->nEr
151dd 72 3b 0a 20 20 77 61 6c 6b 45 78 70 72 54 72 65  r;.  walkExprTre
151de 65 28 70 45 78 70 72 2c 20 61 6e 61 6c 79 7a 65  e(pExpr, analyze
151df 41 67 67 72 65 67 61 74 65 2c 20 70 4e 43 29 3b  Aggregate, pNC);
151e0 0a 20 20 72 65 74 75 72 6e 20 70 4e 43 2d 3e 70  .  return pNC->p
151e1 50 61 72 73 65 2d 3e 6e 45 72 72 20 2d 20 6e 45  Parse->nErr - nE
151e2 72 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c  rr;.}../*.** Cal
151e3 6c 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61  l sqlite3ExprAna
151e4 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73 28 29  lyzeAggregates()
151e5 20 66 6f 72 20 65 76 65 72 79 20 65 78 70 72 65   for every expre
151e6 73 73 69 6f 6e 20 69 6e 20 61 6e 0a 2a 2a 20 65  ssion in an.** e
151e7 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 20  xpression list. 
151e8 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
151e9 65 72 20 6f 66 20 65 72 72 6f 72 73 2e 0a 2a 2a  er of errors..**
151ea 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20  .** If an error 
151eb 69 73 20 66 6f 75 6e 64 2c 20 74 68 65 20 61 6e  is found, the an
151ec 61 6c 79 73 69 73 20 69 73 20 63 75 74 20 73 68  alysis is cut sh
151ed 6f 72 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ort..*/.SQLITE_P
151ee 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
151ef 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67  e3ExprAnalyzeAgg
151f0 4c 69 73 74 28 4e 61 6d 65 43 6f 6e 74 65 78 74  List(NameContext
151f1 20 2a 70 4e 43 2c 20 45 78 70 72 4c 69 73 74 20   *pNC, ExprList 
151f2 2a 70 4c 69 73 74 29 7b 0a 20 20 73 74 72 75 63  *pList){.  struc
151f3 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
151f4 2a 70 49 74 65 6d 3b 0a 20 20 69 6e 74 20 69 3b  *pItem;.  int i;
151f5 0a 20 20 69 6e 74 20 6e 45 72 72 20 3d 20 30 3b  .  int nErr = 0;
151f6 0a 20 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a  .  if( pList ){.
151f7 20 20 20 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c      for(pItem=pL
151f8 69 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 6e 45 72  ist->a, i=0; nEr
151f9 72 3d 3d 30 20 26 26 20 69 3c 70 4c 69 73 74 2d  r==0 && i<pList-
151fa 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74  >nExpr; i++, pIt
151fb 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 6e 45 72  em++){.      nEr
151fc 72 20 2b 3d 20 73 71 6c 69 74 65 33 45 78 70 72  r += sqlite3Expr
151fd 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65  AnalyzeAggregate
151fe 73 28 70 4e 43 2c 20 70 49 74 65 6d 2d 3e 70 45  s(pNC, pItem->pE
151ff 78 70 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  xpr);.    }.  }.
15200 20 20 72 65 74 75 72 6e 20 6e 45 72 72 3b 0a 7d    return nErr;.}
15201 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
15202 2a 20 45 6e 64 20 6f 66 20 65 78 70 72 2e 63 20  * End of expr.c 
15203 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15204 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15205 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15206 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
15207 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 61 6c 74  * Begin file alt
15208 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  er.c ***********
15209 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1520a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1520b 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 46 65 62  /./*.** 2005 Feb
1520c 72 75 61 72 79 20 31 35 0a 2a 2a 0a 2a 2a 20 54  ruary 15.**.** T
1520d 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
1520e 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
1520f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
15210 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
15211 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
15212 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
15213 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
15214 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
15215 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
15216 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
15217 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
15218 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
15219 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
1521a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
1521b 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
1521c 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
1521d 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
1521e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1521f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15221 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15222 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
15223 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20  file contains C 
15224 63 6f 64 65 20 72 6f 75 74 69 6e 65 73 20 74 68  code routines th
15225 61 74 20 75 73 65 64 20 74 6f 20 67 65 6e 65 72  at used to gener
15226 61 74 65 20 56 44 42 45 20 63 6f 64 65 0a 2a 2a  ate VDBE code.**
15227 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73   that implements
15228 20 74 68 65 20 41 4c 54 45 52 20 54 41 42 4c 45   the ALTER TABLE
15229 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20   command..**.** 
1522a 24 49 64 3a 20 61 6c 74 65 72 2e 63 2c 76 20 31  $Id: alter.c,v 1
1522b 2e 32 37 20 32 30 30 37 2f 30 36 2f 32 37 20 31  .27 2007/06/27 1
1522c 37 3a 30 39 3a 32 34 20 64 61 6e 69 65 6c 6b 31  7:09:24 danielk1
1522d 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  977 Exp $.*/../*
1522e 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20  .** The code in 
1522f 74 68 69 73 20 66 69 6c 65 20 6f 6e 6c 79 20 65  this file only e
15230 78 69 73 74 73 20 69 66 20 77 65 20 61 72 65 20  xists if we are 
15231 6e 6f 74 20 6f 6d 69 74 74 69 6e 67 20 74 68 65  not omitting the
15232 0a 2a 2a 20 41 4c 54 45 52 20 54 41 42 4c 45 20  .** ALTER TABLE 
15233 6c 6f 67 69 63 20 66 72 6f 6d 20 74 68 65 20 62  logic from the b
15234 75 69 6c 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  uild..*/.#ifndef
15235 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54   SQLITE_OMIT_ALT
15236 45 52 54 41 42 4c 45 0a 0a 0a 2f 2a 0a 2a 2a 20  ERTABLE.../*.** 
15237 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
15238 20 75 73 65 64 20 62 79 20 53 51 4c 20 67 65 6e   used by SQL gen
15239 65 72 61 74 65 64 20 74 6f 20 69 6d 70 6c 65 6d  erated to implem
1523a 65 6e 74 20 74 68 65 20 0a 2a 2a 20 41 4c 54 45  ent the .** ALTE
1523b 52 20 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 2e  R TABLE command.
1523c 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   The first argum
1523d 65 6e 74 20 69 73 20 74 68 65 20 74 65 78 74 20  ent is the text 
1523e 6f 66 20 61 20 43 52 45 41 54 45 20 54 41 42 4c  of a CREATE TABL
1523f 45 20 6f 72 0a 2a 2a 20 43 52 45 41 54 45 20 49  E or.** CREATE I
15240 4e 44 45 58 20 63 6f 6d 6d 61 6e 64 2e 20 54 68  NDEX command. Th
15241 65 20 73 65 63 6f 6e 64 20 69 73 20 61 20 74 61  e second is a ta
15242 62 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 74 61  ble name. The ta
15243 62 6c 65 20 6e 61 6d 65 20 69 6e 20 0a 2a 2a 20  ble name in .** 
15244 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45  the CREATE TABLE
15245 20 6f 72 20 43 52 45 41 54 45 20 49 4e 44 45 58   or CREATE INDEX
15246 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 72 65   statement is re
15247 70 6c 61 63 65 64 20 77 69 74 68 20 74 68 65 20  placed with the 
15248 74 68 69 72 64 0a 2a 2a 20 61 72 67 75 6d 65 6e  third.** argumen
15249 74 20 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74  t and the result
1524a 20 72 65 74 75 72 6e 65 64 2e 20 45 78 61 6d 70   returned. Examp
1524b 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74  les:.**.** sqlit
1524c 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28 27  e_rename_table('
1524d 43 52 45 41 54 45 20 54 41 42 4c 45 20 61 62 63  CREATE TABLE abc
1524e 28 61 2c 20 62 2c 20 63 29 27 2c 20 27 64 65 66  (a, b, c)', 'def
1524f 27 29 0a 2a 2a 20 20 20 20 20 2d 3e 20 27 43 52  ').**     -> 'CR
15250 45 41 54 45 20 54 41 42 4c 45 20 64 65 66 28 61  EATE TABLE def(a
15251 2c 20 62 2c 20 63 29 27 0a 2a 2a 0a 2a 2a 20 73  , b, c)'.**.** s
15252 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62  qlite_rename_tab
15253 6c 65 28 27 43 52 45 41 54 45 20 49 4e 44 45 58  le('CREATE INDEX
15254 20 69 20 4f 4e 20 61 62 63 28 61 29 27 2c 20 27   i ON abc(a)', '
15255 64 65 66 27 29 0a 2a 2a 20 20 20 20 20 2d 3e 20  def').**     -> 
15256 27 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 20  'CREATE INDEX i 
15257 4f 4e 20 64 65 66 28 61 2c 20 62 2c 20 63 29 27  ON def(a, b, c)'
15258 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
15259 72 65 6e 61 6d 65 54 61 62 6c 65 46 75 6e 63 28  renameTableFunc(
1525a 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
1525b 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
1525c 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74  nt argc,.  sqlit
1525d 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
1525e 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  ){.  unsigned ch
1525f 61 72 20 63 6f 6e 73 74 20 2a 7a 53 71 6c 20 3d  ar const *zSql =
15260 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
15261 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ext(argv[0]);.  
15262 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f  unsigned char co
15263 6e 73 74 20 2a 7a 54 61 62 6c 65 4e 61 6d 65 20  nst *zTableName 
15264 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
15265 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 0a  text(argv[1]);..
15266 20 20 69 6e 74 20 74 6f 6b 65 6e 3b 0a 20 20 54    int token;.  T
15267 6f 6b 65 6e 20 74 6e 61 6d 65 3b 0a 20 20 75 6e  oken tname;.  un
15268 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73  signed char cons
15269 74 20 2a 7a 43 73 72 20 3d 20 7a 53 71 6c 3b 0a  t *zCsr = zSql;.
1526a 20 20 69 6e 74 20 6c 65 6e 20 3d 20 30 3b 0a 20    int len = 0;. 
1526b 20 63 68 61 72 20 2a 7a 52 65 74 3b 0a 0a 20 20   char *zRet;..  
1526c 2f 2a 20 54 68 65 20 70 72 69 6e 63 69 70 6c 65  /* The principle
1526d 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 65 20   used to locate 
1526e 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69  the table name i
1526f 6e 20 74 68 65 20 43 52 45 41 54 45 20 54 41 42  n the CREATE TAB
15270 4c 45 20 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65  LE .  ** stateme
15271 6e 74 20 69 73 20 74 68 61 74 20 74 68 65 20 74  nt is that the t
15272 61 62 6c 65 20 6e 61 6d 65 20 69 73 20 74 68 65  able name is the
15273 20 66 69 72 73 74 20 74 6f 6b 65 6e 20 74 68 61   first token tha
15274 74 20 69 73 20 69 6d 6d 65 64 69 61 74 65 64 6c  t is immediatedl
15275 79 0a 20 20 2a 2a 20 66 6f 6c 6c 6f 77 65 64 20  y.  ** followed 
15276 62 79 20 61 20 6c 65 66 74 20 70 61 72 65 6e 74  by a left parent
15277 68 65 73 69 73 20 2d 20 54 4b 5f 4c 50 20 2d 20  hesis - TK_LP - 
15278 6f 72 20 22 55 53 49 4e 47 22 20 54 4b 5f 55 53  or "USING" TK_US
15279 49 4e 47 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ING..  */.  if( 
1527a 7a 53 71 6c 20 29 7b 0a 20 20 20 20 64 6f 20 7b  zSql ){.    do {
1527b 0a 20 20 20 20 20 20 69 66 28 20 21 2a 7a 43 73  .      if( !*zCs
1527c 72 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  r ){.        /* 
1527d 52 61 6e 20 6f 75 74 20 6f 66 20 69 6e 70 75 74  Ran out of input
1527e 20 62 65 66 6f 72 65 20 66 69 6e 64 69 6e 67 20   before finding 
1527f 61 6e 20 6f 70 65 6e 69 6e 67 20 62 72 61 63 6b  an opening brack
15280 65 74 2e 20 52 65 74 75 72 6e 20 4e 55 4c 4c 2e  et. Return NULL.
15281 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75   */.        retu
15282 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  rn;.      }..   
15283 20 20 20 2f 2a 20 53 74 6f 72 65 20 74 68 65 20     /* Store the 
15284 74 6f 6b 65 6e 20 74 68 61 74 20 7a 43 73 72 20  token that zCsr 
15285 70 6f 69 6e 74 73 20 74 6f 20 69 6e 20 74 6e 61  points to in tna
15286 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20 74 6e 61  me. */.      tna
15287 6d 65 2e 7a 20 3d 20 7a 43 73 72 3b 0a 20 20 20  me.z = zCsr;.   
15288 20 20 20 74 6e 61 6d 65 2e 6e 20 3d 20 6c 65 6e     tname.n = len
15289 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 41 64 76 61  ;..      /* Adva
1528a 6e 63 65 20 7a 43 73 72 20 74 6f 20 74 68 65 20  nce zCsr to the 
1528b 6e 65 78 74 20 74 6f 6b 65 6e 2e 20 53 74 6f 72  next token. Stor
1528c 65 20 74 68 61 74 20 74 6f 6b 65 6e 20 74 79 70  e that token typ
1528d 65 20 69 6e 20 27 74 6f 6b 65 6e 27 2c 0a 20 20  e in 'token',.  
1528e 20 20 20 20 2a 2a 20 61 6e 64 20 69 74 27 73 20      ** and it's 
1528f 6c 65 6e 67 74 68 20 69 6e 20 27 6c 65 6e 27 20  length in 'len' 
15290 28 74 6f 20 62 65 20 75 73 65 64 20 6e 65 78 74  (to be used next
15291 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68   iteration of th
15292 69 73 20 6c 6f 6f 70 29 2e 0a 20 20 20 20 20 20  is loop)..      
15293 2a 2f 0a 20 20 20 20 20 20 64 6f 20 7b 0a 20 20  */.      do {.  
15294 20 20 20 20 20 20 7a 43 73 72 20 2b 3d 20 6c 65        zCsr += le
15295 6e 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e 20 3d  n;.        len =
15296 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e   sqlite3GetToken
15297 28 7a 43 73 72 2c 20 26 74 6f 6b 65 6e 29 3b 0a  (zCsr, &token);.
15298 20 20 20 20 20 20 7d 20 77 68 69 6c 65 28 20 74        } while( t
15299 6f 6b 65 6e 3d 3d 54 4b 5f 53 50 41 43 45 20 29  oken==TK_SPACE )
1529a 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1529b 6c 65 6e 3e 30 20 29 3b 0a 20 20 20 20 7d 20 77  len>0 );.    } w
1529c 68 69 6c 65 28 20 74 6f 6b 65 6e 21 3d 54 4b 5f  hile( token!=TK_
1529d 4c 50 20 26 26 20 74 6f 6b 65 6e 21 3d 54 4b 5f  LP && token!=TK_
1529e 55 53 49 4e 47 20 29 3b 0a 0a 20 20 20 20 7a 52  USING );..    zR
1529f 65 74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  et = sqlite3MPri
152a0 6e 74 66 28 22 25 2e 2a 73 25 51 25 73 22 2c 20  ntf("%.*s%Q%s", 
152a1 74 6e 61 6d 65 2e 7a 20 2d 20 7a 53 71 6c 2c 20  tname.z - zSql, 
152a2 7a 53 71 6c 2c 20 0a 20 20 20 20 20 20 20 7a 54  zSql, .       zT
152a3 61 62 6c 65 4e 61 6d 65 2c 20 74 6e 61 6d 65 2e  ableName, tname.
152a4 7a 2b 74 6e 61 6d 65 2e 6e 29 3b 0a 20 20 20 20  z+tname.n);.    
152a5 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
152a6 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 52 65  ext(context, zRe
152a7 74 2c 20 2d 31 2c 20 73 71 6c 69 74 65 33 46 72  t, -1, sqlite3Fr
152a8 65 65 58 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66  eeX);.  }.}..#if
152a9 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
152aa 5f 54 52 49 47 47 45 52 0a 2f 2a 20 54 68 69 73  _TRIGGER./* This
152ab 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
152ac 64 20 62 79 20 53 51 4c 20 67 65 6e 65 72 61 74  d by SQL generat
152ad 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  ed to implement 
152ae 74 68 65 0a 2a 2a 20 41 4c 54 45 52 20 54 41 42  the.** ALTER TAB
152af 4c 45 20 63 6f 6d 6d 61 6e 64 2e 20 54 68 65 20  LE command. The 
152b0 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69  first argument i
152b1 73 20 74 68 65 20 74 65 78 74 20 6f 66 20 61 20  s the text of a 
152b2 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20 0a  CREATE TRIGGER .
152b3 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68  ** statement. Th
152b4 65 20 73 65 63 6f 6e 64 20 69 73 20 61 20 74 61  e second is a ta
152b5 62 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 74 61  ble name. The ta
152b6 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20  ble name in the 
152b7 43 52 45 41 54 45 20 0a 2a 2a 20 54 52 49 47 47  CREATE .** TRIGG
152b8 45 52 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  ER statement is 
152b9 72 65 70 6c 61 63 65 64 20 77 69 74 68 20 74 68  replaced with th
152ba 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74  e third argument
152bb 20 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74 20   and the result 
152bc 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 54 68  .** returned. Th
152bd 69 73 20 69 73 20 61 6e 61 6c 61 67 6f 75 73 20  is is analagous 
152be 74 6f 20 72 65 6e 61 6d 65 54 61 62 6c 65 46 75  to renameTableFu
152bf 6e 63 28 29 20 61 62 6f 76 65 2c 20 65 78 63 65  nc() above, exce
152c0 70 74 20 66 6f 72 20 43 52 45 41 54 45 0a 2a 2a  pt for CREATE.**
152c1 20 54 52 49 47 47 45 52 2c 20 6e 6f 74 20 43 52   TRIGGER, not CR
152c2 45 41 54 45 20 49 4e 44 45 58 20 61 6e 64 20 43  EATE INDEX and C
152c3 52 45 41 54 45 20 54 41 42 4c 45 2e 0a 2a 2f 0a  REATE TABLE..*/.
152c4 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6e 61  static void rena
152c5 6d 65 54 72 69 67 67 65 72 46 75 6e 63 28 0a 20  meTriggerFunc(. 
152c6 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
152c7 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
152c8 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33   argc,.  sqlite3
152c9 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
152ca 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
152cb 20 63 6f 6e 73 74 20 2a 7a 53 71 6c 20 3d 20 73   const *zSql = s
152cc 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
152cd 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 75 6e  t(argv[0]);.  un
152ce 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73  signed char cons
152cf 74 20 2a 7a 54 61 62 6c 65 4e 61 6d 65 20 3d 20  t *zTableName = 
152d0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
152d1 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 0a 20 20  xt(argv[1]);..  
152d2 69 6e 74 20 74 6f 6b 65 6e 3b 0a 20 20 54 6f 6b  int token;.  Tok
152d3 65 6e 20 74 6e 61 6d 65 3b 0a 20 20 69 6e 74 20  en tname;.  int 
152d4 64 69 73 74 20 3d 20 33 3b 0a 20 20 75 6e 73 69  dist = 3;.  unsi
152d5 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20  gned char const 
152d6 2a 7a 43 73 72 20 3d 20 7a 53 71 6c 3b 0a 20 20  *zCsr = zSql;.  
152d7 69 6e 74 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 63  int len = 0;.  c
152d8 68 61 72 20 2a 7a 52 65 74 3b 0a 0a 20 20 2f 2a  har *zRet;..  /*
152d9 20 54 68 65 20 70 72 69 6e 63 69 70 6c 65 20 75   The principle u
152da 73 65 64 20 74 6f 20 6c 6f 63 61 74 65 20 74 68  sed to locate th
152db 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20  e table name in 
152dc 74 68 65 20 43 52 45 41 54 45 20 54 52 49 47 47  the CREATE TRIGG
152dd 45 52 20 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65  ER .  ** stateme
152de 6e 74 20 69 73 20 74 68 61 74 20 74 68 65 20 74  nt is that the t
152df 61 62 6c 65 20 6e 61 6d 65 20 69 73 20 74 68 65  able name is the
152e0 20 66 69 72 73 74 20 74 6f 6b 65 6e 20 74 68 61   first token tha
152e1 74 20 69 73 20 69 6d 6d 65 64 69 61 74 65 64 6c  t is immediatedl
152e2 79 0a 20 20 2a 2a 20 70 72 65 63 65 64 65 64 20  y.  ** preceded 
152e3 62 79 20 65 69 74 68 65 72 20 54 4b 5f 4f 4e 20  by either TK_ON 
152e4 6f 72 20 54 4b 5f 44 4f 54 20 61 6e 64 20 69 6d  or TK_DOT and im
152e5 6d 65 64 69 61 74 65 64 6c 79 20 66 6f 6c 6c 6f  mediatedly follo
152e6 77 65 64 20 62 79 20 6f 6e 65 0a 20 20 2a 2a 20  wed by one.  ** 
152e7 6f 66 20 54 4b 5f 57 48 45 4e 2c 20 54 4b 5f 42  of TK_WHEN, TK_B
152e8 45 47 49 4e 20 6f 72 20 54 4b 5f 46 4f 52 2e 0a  EGIN or TK_FOR..
152e9 20 20 2a 2f 0a 20 20 69 66 28 20 7a 53 71 6c 20    */.  if( zSql 
152ea 29 7b 0a 20 20 20 20 64 6f 20 7b 0a 0a 20 20 20  ){.    do {..   
152eb 20 20 20 69 66 28 20 21 2a 7a 43 73 72 20 29 7b     if( !*zCsr ){
152ec 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 61 6e 20  .        /* Ran 
152ed 6f 75 74 20 6f 66 20 69 6e 70 75 74 20 62 65 66  out of input bef
152ee 6f 72 65 20 66 69 6e 64 69 6e 67 20 74 68 65 20  ore finding the 
152ef 74 61 62 6c 65 20 6e 61 6d 65 2e 20 52 65 74 75  table name. Retu
152f0 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 20 20  rn NULL. */.    
152f1 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
152f2 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 53 74    }..      /* St
152f3 6f 72 65 20 74 68 65 20 74 6f 6b 65 6e 20 74 68  ore the token th
152f4 61 74 20 7a 43 73 72 20 70 6f 69 6e 74 73 20 74  at zCsr points t
152f5 6f 20 69 6e 20 74 6e 61 6d 65 2e 20 2a 2f 0a 20  o in tname. */. 
152f6 20 20 20 20 20 74 6e 61 6d 65 2e 7a 20 3d 20 7a       tname.z = z
152f7 43 73 72 3b 0a 20 20 20 20 20 20 74 6e 61 6d 65  Csr;.      tname
152f8 2e 6e 20 3d 20 6c 65 6e 3b 0a 0a 20 20 20 20 20  .n = len;..     
152f9 20 2f 2a 20 41 64 76 61 6e 63 65 20 7a 43 73 72   /* Advance zCsr
152fa 20 74 6f 20 74 68 65 20 6e 65 78 74 20 74 6f 6b   to the next tok
152fb 65 6e 2e 20 53 74 6f 72 65 20 74 68 61 74 20 74  en. Store that t
152fc 6f 6b 65 6e 20 74 79 70 65 20 69 6e 20 27 74 6f  oken type in 'to
152fd 6b 65 6e 27 2c 0a 20 20 20 20 20 20 2a 2a 20 61  ken',.      ** a
152fe 6e 64 20 69 74 27 73 20 6c 65 6e 67 74 68 20 69  nd it's length i
152ff 6e 20 27 6c 65 6e 27 20 28 74 6f 20 62 65 20 75  n 'len' (to be u
15300 73 65 64 20 6e 65 78 74 20 69 74 65 72 61 74 69  sed next iterati
15301 6f 6e 20 6f 66 20 74 68 69 73 20 6c 6f 6f 70 29  on of this loop)
15302 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
15303 20 64 6f 20 7b 0a 20 20 20 20 20 20 20 20 7a 43   do {.        zC
15304 73 72 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20 20  sr += len;.     
15305 20 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33     len = sqlite3
15306 47 65 74 54 6f 6b 65 6e 28 7a 43 73 72 2c 20 26  GetToken(zCsr, &
15307 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 7d 77  token);.      }w
15308 68 69 6c 65 28 20 74 6f 6b 65 6e 3d 3d 54 4b 5f  hile( token==TK_
15309 53 50 41 43 45 20 29 3b 0a 20 20 20 20 20 20 61  SPACE );.      a
1530a 73 73 65 72 74 28 20 6c 65 6e 3e 30 20 29 3b 0a  ssert( len>0 );.
1530b 0a 20 20 20 20 20 20 2f 2a 20 56 61 72 69 61 62  .      /* Variab
1530c 6c 65 20 27 64 69 73 74 27 20 73 74 6f 72 65 73  le 'dist' stores
1530d 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74   the number of t
1530e 6f 6b 65 6e 73 20 72 65 61 64 20 73 69 6e 63 65  okens read since
1530f 20 74 68 65 20 6d 6f 73 74 0a 20 20 20 20 20 20   the most.      
15310 2a 2a 20 72 65 63 65 6e 74 20 54 4b 5f 44 4f 54  ** recent TK_DOT
15311 20 6f 72 20 54 4b 5f 4f 4e 2e 20 54 68 69 73 20   or TK_ON. This 
15312 6d 65 61 6e 73 20 74 68 61 74 20 77 68 65 6e 20  means that when 
15313 61 20 57 48 45 4e 2c 20 46 4f 52 20 6f 72 20 42  a WHEN, FOR or B
15314 45 47 49 4e 20 0a 20 20 20 20 20 20 2a 2a 20 74  EGIN .      ** t
15315 6f 6b 65 6e 20 69 73 20 72 65 61 64 20 61 6e 64  oken is read and
15316 20 27 64 69 73 74 27 20 65 71 75 61 6c 73 20 32   'dist' equals 2
15317 2c 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20  , the condition 
15318 73 74 61 74 65 64 20 61 62 6f 76 65 0a 20 20 20  stated above.   
15319 20 20 20 2a 2a 20 74 6f 20 62 65 20 6d 65 74 2e     ** to be met.
1531a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
1531b 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 4f 4e 20  ** Note that ON 
1531c 63 61 6e 6e 6f 74 20 62 65 20 61 20 64 61 74 61  cannot be a data
1531d 62 61 73 65 2c 20 74 61 62 6c 65 20 6f 72 20 63  base, table or c
1531e 6f 6c 75 6d 6e 20 6e 61 6d 65 2c 20 73 6f 0a 20  olumn name, so. 
1531f 20 20 20 20 20 2a 2a 20 74 68 65 72 65 20 69 73       ** there is
15320 20 6e 6f 20 6e 65 65 64 20 74 6f 20 77 6f 72 72   no need to worr
15321 79 20 61 62 6f 75 74 20 73 79 6e 74 61 78 20 6c  y about syntax l
15322 69 6b 65 20 0a 20 20 20 20 20 20 2a 2a 20 22 43  ike .      ** "C
15323 52 45 41 54 45 20 54 52 49 47 47 45 52 20 2e 2e  REATE TRIGGER ..
15324 2e 20 4f 4e 20 4f 4e 2e 4f 4e 20 42 45 47 49 4e  . ON ON.ON BEGIN
15325 20 2e 2e 2e 22 20 65 74 63 2e 0a 20 20 20 20 20   ..." etc..     
15326 20 2a 2f 0a 20 20 20 20 20 20 64 69 73 74 2b 2b   */.      dist++
15327 3b 0a 20 20 20 20 20 20 69 66 28 20 74 6f 6b 65  ;.      if( toke
15328 6e 3d 3d 54 4b 5f 44 4f 54 20 7c 7c 20 74 6f 6b  n==TK_DOT || tok
15329 65 6e 3d 3d 54 4b 5f 4f 4e 20 29 7b 0a 20 20 20  en==TK_ON ){.   
1532a 20 20 20 20 20 64 69 73 74 20 3d 20 30 3b 0a 20       dist = 0;. 
1532b 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 77 68 69       }.    } whi
1532c 6c 65 28 20 64 69 73 74 21 3d 32 20 7c 7c 20 28  le( dist!=2 || (
1532d 74 6f 6b 65 6e 21 3d 54 4b 5f 57 48 45 4e 20 26  token!=TK_WHEN &
1532e 26 20 74 6f 6b 65 6e 21 3d 54 4b 5f 46 4f 52 20  & token!=TK_FOR 
1532f 26 26 20 74 6f 6b 65 6e 21 3d 54 4b 5f 42 45 47  && token!=TK_BEG
15330 49 4e 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 56  IN) );..    /* V
15331 61 72 69 61 62 6c 65 20 74 6e 61 6d 65 20 6e 6f  ariable tname no
15332 77 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74  w contains the t
15333 6f 6b 65 6e 20 74 68 61 74 20 69 73 20 74 68 65  oken that is the
15334 20 6f 6c 64 20 74 61 62 6c 65 2d 6e 61 6d 65 0a   old table-name.
15335 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 43 52      ** in the CR
15336 45 41 54 45 20 54 52 49 47 47 45 52 20 73 74 61  EATE TRIGGER sta
15337 74 65 6d 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20  tement..    */. 
15338 20 20 20 7a 52 65 74 20 3d 20 73 71 6c 69 74 65     zRet = sqlite
15339 33 4d 50 72 69 6e 74 66 28 22 25 2e 2a 73 25 51  3MPrintf("%.*s%Q
1533a 25 73 22 2c 20 74 6e 61 6d 65 2e 7a 20 2d 20 7a  %s", tname.z - z
1533b 53 71 6c 2c 20 7a 53 71 6c 2c 20 0a 20 20 20 20  Sql, zSql, .    
1533c 20 20 20 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 74     zTableName, t
1533d 6e 61 6d 65 2e 7a 2b 74 6e 61 6d 65 2e 6e 29 3b  name.z+tname.n);
1533e 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
1533f 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
15340 2c 20 7a 52 65 74 2c 20 2d 31 2c 20 73 71 6c 69  , zRet, -1, sqli
15341 74 65 33 46 72 65 65 58 29 3b 0a 20 20 7d 0a 7d  te3FreeX);.  }.}
15342 0a 23 65 6e 64 69 66 20 20 20 2f 2a 20 21 53 51  .#endif   /* !SQ
15343 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
15344 52 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69  R */../*.** Regi
15345 73 74 65 72 20 62 75 69 6c 74 2d 69 6e 20 66 75  ster built-in fu
15346 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20  nctions used to 
15347 68 65 6c 70 20 69 6d 70 6c 65 6d 65 6e 74 20 41  help implement A
15348 4c 54 45 52 20 54 41 42 4c 45 0a 2a 2f 0a 53 51  LTER TABLE.*/.SQ
15349 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1534a 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46 75  d sqlite3AlterFu
1534b 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 20  nctions(sqlite3 
1534c 2a 64 62 29 7b 0a 20 20 73 74 61 74 69 63 20 63  *db){.  static c
1534d 6f 6e 73 74 20 73 74 72 75 63 74 20 7b 0a 20 20  onst struct {.  
1534e 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a     char *zName;.
1534f 20 20 20 20 20 73 69 67 6e 65 64 20 63 68 61 72       signed char
15350 20 6e 41 72 67 3b 0a 20 20 20 20 20 76 6f 69 64   nArg;.     void
15351 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65   (*xFunc)(sqlite
15352 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73  3_context*,int,s
15353 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29  qlite3_value **)
15354 3b 0a 20 20 7d 20 61 46 75 6e 63 73 5b 5d 20 3d  ;.  } aFuncs[] =
15355 20 7b 0a 20 20 20 20 7b 20 22 73 71 6c 69 74 65   {.    { "sqlite
15356 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 22 2c 20  _rename_table", 
15357 20 20 20 32 2c 20 72 65 6e 61 6d 65 54 61 62 6c     2, renameTabl
15358 65 46 75 6e 63 7d 2c 0a 23 69 66 6e 64 65 66 20  eFunc},.#ifndef 
15359 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
1535a 47 45 52 0a 20 20 20 20 7b 20 22 73 71 6c 69 74  GER.    { "sqlit
1535b 65 5f 72 65 6e 61 6d 65 5f 74 72 69 67 67 65 72  e_rename_trigger
1535c 22 2c 20 20 32 2c 20 72 65 6e 61 6d 65 54 72 69  ",  2, renameTri
1535d 67 67 65 72 46 75 6e 63 7d 2c 0a 23 65 6e 64 69  ggerFunc},.#endi
1535e 66 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a  f.  };.  int i;.
1535f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 69  .  for(i=0; i<si
15360 7a 65 6f 66 28 61 46 75 6e 63 73 29 2f 73 69 7a  zeof(aFuncs)/siz
15361 65 6f 66 28 61 46 75 6e 63 73 5b 30 5d 29 3b 20  eof(aFuncs[0]); 
15362 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  i++){.    sqlite
15363 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20  3CreateFunc(db, 
15364 61 46 75 6e 63 73 5b 69 5d 2e 7a 4e 61 6d 65 2c  aFuncs[i].zName,
15365 20 61 46 75 6e 63 73 5b 69 5d 2e 6e 41 72 67 2c   aFuncs[i].nArg,
15366 0a 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  .        SQLITE_
15367 55 54 46 38 2c 20 30 2c 20 61 46 75 6e 63 73 5b  UTF8, 0, aFuncs[
15368 69 5d 2e 78 46 75 6e 63 2c 20 30 2c 20 30 29 3b  i].xFunc, 0, 0);
15369 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  .  }.}../*.** Ge
1536a 6e 65 72 61 74 65 20 74 68 65 20 74 65 78 74 20  nerate the text 
1536b 6f 66 20 61 20 57 48 45 52 45 20 65 78 70 72 65  of a WHERE expre
1536c 73 73 69 6f 6e 20 77 68 69 63 68 20 63 61 6e 20  ssion which can 
1536d 62 65 20 75 73 65 64 20 74 6f 20 73 65 6c 65 63  be used to selec
1536e 74 20 61 6c 6c 0a 2a 2a 20 74 65 6d 70 6f 72 61  t all.** tempora
1536f 72 79 20 74 72 69 67 67 65 72 73 20 6f 6e 20 74  ry triggers on t
15370 61 62 6c 65 20 70 54 61 62 20 66 72 6f 6d 20 74  able pTab from t
15371 68 65 20 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d  he sqlite_temp_m
15372 61 73 74 65 72 20 74 61 62 6c 65 2e 20 49 66 0a  aster table. If.
15373 2a 2a 20 74 61 62 6c 65 20 70 54 61 62 20 68 61  ** table pTab ha
15374 73 20 6e 6f 20 74 65 6d 70 6f 72 61 72 79 20 74  s no temporary t
15375 72 69 67 67 65 72 73 2c 20 6f 72 20 69 73 20 69  riggers, or is i
15376 74 73 65 6c 66 20 73 74 6f 72 65 64 20 69 6e 20  tself stored in 
15377 74 68 65 20 0a 2a 2a 20 74 65 6d 70 6f 72 61 72  the .** temporar
15378 79 20 64 61 74 61 62 61 73 65 2c 20 4e 55 4c 4c  y database, NULL
15379 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
1537a 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 77 68  .static char *wh
1537b 65 72 65 54 65 6d 70 54 72 69 67 67 65 72 73 28  ereTempTriggers(
1537c 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54  Parse *pParse, T
1537d 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 54  able *pTab){.  T
1537e 72 69 67 67 65 72 20 2a 70 54 72 69 67 3b 0a 20  rigger *pTrig;. 
1537f 20 63 68 61 72 20 2a 7a 57 68 65 72 65 20 3d 20   char *zWhere = 
15380 30 3b 0a 20 20 63 68 61 72 20 2a 74 6d 70 20 3d  0;.  char *tmp =
15381 20 30 3b 0a 20 20 63 6f 6e 73 74 20 53 63 68 65   0;.  const Sche
15382 6d 61 20 2a 70 54 65 6d 70 53 63 68 65 6d 61 20  ma *pTempSchema 
15383 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44  = pParse->db->aD
15384 62 5b 31 5d 2e 70 53 63 68 65 6d 61 3b 20 2f 2a  b[1].pSchema; /*
15385 20 54 65 6d 70 20 64 62 20 73 63 68 65 6d 61 20   Temp db schema 
15386 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  */..  /* If the 
15387 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 6c 6f 63  table is not loc
15388 61 74 65 64 20 69 6e 20 74 68 65 20 74 65 6d 70  ated in the temp
15389 2d 64 62 20 28 69 6e 20 77 68 69 63 68 20 63 61  -db (in which ca
1538a 73 65 20 4e 55 4c 4c 20 69 73 20 0a 20 20 2a 2a  se NULL is .  **
1538b 20 72 65 74 75 72 6e 65 64 2c 20 6c 6f 6f 70 20   returned, loop 
1538c 74 68 72 6f 75 67 68 20 74 68 65 20 74 61 62 6c  through the tabl
1538d 65 73 20 6c 69 73 74 20 6f 66 20 74 72 69 67 67  es list of trigg
1538e 65 72 73 2e 20 46 6f 72 20 65 61 63 68 20 74 72  ers. For each tr
1538f 69 67 67 65 72 0a 20 20 2a 2a 20 74 68 61 74 20  igger.  ** that 
15390 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74  is not part of t
15391 68 65 20 74 65 6d 70 2d 64 62 20 73 63 68 65 6d  he temp-db schem
15392 61 2c 20 61 64 64 20 61 20 63 6c 61 75 73 65 20  a, add a clause 
15393 74 6f 20 74 68 65 20 57 48 45 52 45 20 0a 20 20  to the WHERE .  
15394 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 62 65  ** expression be
15395 69 6e 67 20 62 75 69 6c 74 20 75 70 20 69 6e 20  ing built up in 
15396 7a 57 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 69  zWhere..  */.  i
15397 66 28 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  f( pTab->pSchema
15398 21 3d 70 54 65 6d 70 53 63 68 65 6d 61 20 29 7b  !=pTempSchema ){
15399 0a 20 20 20 20 66 6f 72 28 20 70 54 72 69 67 3d  .    for( pTrig=
1539a 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72 3b 20  pTab->pTrigger; 
1539b 70 54 72 69 67 3b 20 70 54 72 69 67 3d 70 54 72  pTrig; pTrig=pTr
1539c 69 67 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20  ig->pNext ){.   
1539d 20 20 20 69 66 28 20 70 54 72 69 67 2d 3e 70 53     if( pTrig->pS
1539e 63 68 65 6d 61 3d 3d 70 54 65 6d 70 53 63 68 65  chema==pTempSche
1539f 6d 61 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  ma ){.        if
153a0 28 20 21 7a 57 68 65 72 65 20 29 7b 0a 20 20 20  ( !zWhere ){.   
153a1 20 20 20 20 20 20 20 7a 57 68 65 72 65 20 3d 20         zWhere = 
153a2 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 22  sqlite3MPrintf("
153a3 6e 61 6d 65 3d 25 51 22 2c 20 70 54 72 69 67 2d  name=%Q", pTrig-
153a4 3e 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20  >name);.        
153a5 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
153a6 20 74 6d 70 20 3d 20 7a 57 68 65 72 65 3b 0a 20   tmp = zWhere;. 
153a7 20 20 20 20 20 20 20 20 20 7a 57 68 65 72 65 20           zWhere 
153a8 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
153a9 28 22 25 73 20 4f 52 20 6e 61 6d 65 3d 25 51 22  ("%s OR name=%Q"
153aa 2c 20 7a 57 68 65 72 65 2c 20 70 54 72 69 67 2d  , zWhere, pTrig-
153ab 3e 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20  >name);.        
153ac 20 20 73 71 6c 69 74 65 46 72 65 65 28 74 6d 70    sqliteFree(tmp
153ad 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
153ae 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
153af 20 72 65 74 75 72 6e 20 7a 57 68 65 72 65 3b 0a   return zWhere;.
153b0 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
153b1 65 20 63 6f 64 65 20 74 6f 20 64 72 6f 70 20 61  e code to drop a
153b2 6e 64 20 72 65 6c 6f 61 64 20 74 68 65 20 69 6e  nd reload the in
153b3 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74  ternal represent
153b4 61 74 69 6f 6e 20 6f 66 20 74 61 62 6c 65 0a 2a  ation of table.*
153b5 2a 20 70 54 61 62 20 66 72 6f 6d 20 74 68 65 20  * pTab from the 
153b6 64 61 74 61 62 61 73 65 2c 20 69 6e 63 6c 75 64  database, includ
153b7 69 6e 67 20 74 72 69 67 67 65 72 73 20 61 6e 64  ing triggers and
153b8 20 74 65 6d 70 6f 72 61 72 79 20 74 72 69 67 67   temporary trigg
153b9 65 72 73 2e 0a 2a 2a 20 41 72 67 75 6d 65 6e 74  ers..** Argument
153ba 20 7a 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61   zName is the na
153bb 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
153bc 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
153bd 73 63 68 65 6d 61 20 61 74 0a 2a 2a 20 74 68 65  schema at.** the
153be 20 74 69 6d 65 20 74 68 65 20 67 65 6e 65 72 61   time the genera
153bf 74 65 64 20 63 6f 64 65 20 69 73 20 65 78 65 63  ted code is exec
153c0 75 74 65 64 2e 20 54 68 69 73 20 63 61 6e 20 62  uted. This can b
153c1 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d  e different from
153c2 0a 2a 2a 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 20  .** pTab->zName 
153c3 69 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  if this function
153c4 20 69 73 20 62 65 69 6e 67 20 63 61 6c 6c 65 64   is being called
153c5 20 74 6f 20 63 6f 64 65 20 70 61 72 74 20 6f 66   to code part of
153c6 20 61 6e 20 0a 2a 2a 20 22 41 4c 54 45 52 20 54   an .** "ALTER T
153c7 41 42 4c 45 20 52 45 4e 41 4d 45 20 54 4f 22 20  ABLE RENAME TO" 
153c8 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74  statement..*/.st
153c9 61 74 69 63 20 76 6f 69 64 20 72 65 6c 6f 61 64  atic void reload
153ca 54 61 62 6c 65 53 63 68 65 6d 61 28 50 61 72 73  TableSchema(Pars
153cb 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65  e *pParse, Table
153cc 20 2a 70 54 61 62 2c 20 63 6f 6e 73 74 20 63 68   *pTab, const ch
153cd 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 56 64  ar *zName){.  Vd
153ce 62 65 20 2a 76 3b 0a 20 20 63 68 61 72 20 2a 7a  be *v;.  char *z
153cf 57 68 65 72 65 3b 0a 20 20 69 6e 74 20 69 44 62  Where;.  int iDb
153d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
153d1 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
153d2 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e  database contain
153d3 69 6e 67 20 70 54 61 62 20 2a 2f 0a 23 69 66 6e  ing pTab */.#ifn
153d4 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
153d5 54 52 49 47 47 45 52 0a 20 20 54 72 69 67 67 65  TRIGGER.  Trigge
153d6 72 20 2a 70 54 72 69 67 3b 0a 23 65 6e 64 69 66  r *pTrig;.#endif
153d7 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  ..  v = sqlite3G
153d8 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
153d9 20 20 69 66 28 20 21 76 20 29 20 72 65 74 75 72    if( !v ) retur
153da 6e 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74  n;.  iDb = sqlit
153db 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
153dc 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62  pParse->db, pTab
153dd 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 61 73  ->pSchema);.  as
153de 73 65 72 74 28 20 69 44 62 3e 3d 30 20 29 3b 0a  sert( iDb>=0 );.
153df 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
153e0 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 2f  OMIT_TRIGGER.  /
153e1 2a 20 44 72 6f 70 20 61 6e 79 20 74 61 62 6c 65  * Drop any table
153e2 20 74 72 69 67 67 65 72 73 20 66 72 6f 6d 20 74   triggers from t
153e3 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65  he internal sche
153e4 6d 61 2e 20 2a 2f 0a 20 20 66 6f 72 28 70 54 72  ma. */.  for(pTr
153e5 69 67 3d 70 54 61 62 2d 3e 70 54 72 69 67 67 65  ig=pTab->pTrigge
153e6 72 3b 20 70 54 72 69 67 3b 20 70 54 72 69 67 3d  r; pTrig; pTrig=
153e7 70 54 72 69 67 2d 3e 70 4e 65 78 74 29 7b 0a 20  pTrig->pNext){. 
153e8 20 20 20 69 6e 74 20 69 54 72 69 67 44 62 20 3d     int iTrigDb =
153e9 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
153ea 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62  Index(pParse->db
153eb 2c 20 70 54 72 69 67 2d 3e 70 53 63 68 65 6d 61  , pTrig->pSchema
153ec 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  );.    assert( i
153ed 54 72 69 67 44 62 3d 3d 69 44 62 20 7c 7c 20 69  TrigDb==iDb || i
153ee 54 72 69 67 44 62 3d 3d 31 20 29 3b 0a 20 20 20  TrigDb==1 );.   
153ef 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28   sqlite3VdbeOp3(
153f0 76 2c 20 4f 50 5f 44 72 6f 70 54 72 69 67 67 65  v, OP_DropTrigge
153f1 72 2c 20 69 54 72 69 67 44 62 2c 20 30 2c 20 70  r, iTrigDb, 0, p
153f2 54 72 69 67 2d 3e 6e 61 6d 65 2c 20 30 29 3b 0a  Trig->name, 0);.
153f3 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
153f4 20 44 72 6f 70 20 74 68 65 20 74 61 62 6c 65 20   Drop the table 
153f5 61 6e 64 20 69 6e 64 65 78 20 66 72 6f 6d 20 74  and index from t
153f6 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65  he internal sche
153f7 6d 61 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56  ma */.  sqlite3V
153f8 64 62 65 4f 70 33 28 76 2c 20 4f 50 5f 44 72 6f  dbeOp3(v, OP_Dro
153f9 70 54 61 62 6c 65 2c 20 69 44 62 2c 20 30 2c 20  pTable, iDb, 0, 
153fa 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b  pTab->zName, 0);
153fb 0a 0a 20 20 2f 2a 20 52 65 6c 6f 61 64 20 74 68  ..  /* Reload th
153fc 65 20 74 61 62 6c 65 2c 20 69 6e 64 65 78 20 61  e table, index a
153fd 6e 64 20 70 65 72 6d 61 6e 65 6e 74 20 74 72 69  nd permanent tri
153fe 67 67 65 72 20 73 63 68 65 6d 61 73 2e 20 2a 2f  gger schemas. */
153ff 0a 20 20 7a 57 68 65 72 65 20 3d 20 73 71 6c 69  .  zWhere = sqli
15400 74 65 33 4d 50 72 69 6e 74 66 28 22 74 62 6c 5f  te3MPrintf("tbl_
15401 6e 61 6d 65 3d 25 51 22 2c 20 7a 4e 61 6d 65 29  name=%Q", zName)
15402 3b 0a 20 20 69 66 28 20 21 7a 57 68 65 72 65 20  ;.  if( !zWhere 
15403 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69  ) return;.  sqli
15404 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20 4f 50  te3VdbeOp3(v, OP
15405 5f 50 61 72 73 65 53 63 68 65 6d 61 2c 20 69 44  _ParseSchema, iD
15406 62 2c 20 30 2c 20 7a 57 68 65 72 65 2c 20 50 33  b, 0, zWhere, P3
15407 5f 44 59 4e 41 4d 49 43 29 3b 0a 0a 23 69 66 6e  _DYNAMIC);..#ifn
15408 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
15409 54 52 49 47 47 45 52 0a 20 20 2f 2a 20 4e 6f 77  TRIGGER.  /* Now
1540a 2c 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 69  , if the table i
1540b 73 20 6e 6f 74 20 73 74 6f 72 65 64 20 69 6e 20  s not stored in 
1540c 74 68 65 20 74 65 6d 70 20 64 61 74 61 62 61 73  the temp databas
1540d 65 2c 20 72 65 6c 6f 61 64 20 61 6e 79 20 74 65  e, reload any te
1540e 6d 70 20 0a 20 20 2a 2a 20 74 72 69 67 67 65 72  mp .  ** trigger
1540f 73 2e 20 44 6f 6e 27 74 20 75 73 65 20 49 4e 28  s. Don't use IN(
15410 2e 2e 2e 29 20 69 6e 20 63 61 73 65 20 53 51 4c  ...) in case SQL
15411 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
15412 59 20 69 73 20 64 65 66 69 6e 65 64 2e 20 0a 20  Y is defined. . 
15413 20 2a 2f 0a 20 20 69 66 28 20 28 7a 57 68 65 72   */.  if( (zWher
15414 65 3d 77 68 65 72 65 54 65 6d 70 54 72 69 67 67  e=whereTempTrigg
15415 65 72 73 28 70 50 61 72 73 65 2c 20 70 54 61 62  ers(pParse, pTab
15416 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  ))!=0 ){.    sql
15417 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20 4f  ite3VdbeOp3(v, O
15418 50 5f 50 61 72 73 65 53 63 68 65 6d 61 2c 20 31  P_ParseSchema, 1
15419 2c 20 30 2c 20 7a 57 68 65 72 65 2c 20 50 33 5f  , 0, zWhere, P3_
1541a 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 23 65  DYNAMIC);.  }.#e
1541b 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  ndif.}../*.** Ge
1541c 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 69  nerate code to i
1541d 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 22 41 4c  mplement the "AL
1541e 54 45 52 20 54 41 42 4c 45 20 78 78 78 20 52 45  TER TABLE xxx RE
1541f 4e 41 4d 45 20 54 4f 20 79 79 79 22 20 0a 2a 2a  NAME TO yyy" .**
15420 20 63 6f 6d 6d 61 6e 64 2e 20 0a 2a 2f 0a 53 51   command. .*/.SQ
15421 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
15422 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 52 65  d sqlite3AlterRe
15423 6e 61 6d 65 54 61 62 6c 65 28 0a 20 20 50 61 72  nameTable(.  Par
15424 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
15425 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72         /* Parser
15426 20 63 6f 6e 74 65 78 74 2e 20 2a 2f 0a 20 20 53   context. */.  S
15427 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20  rcList *pSrc,   
15428 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
15429 74 61 62 6c 65 20 74 6f 20 72 65 6e 61 6d 65 2e  table to rename.
1542a 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61   */.  Token *pNa
1542b 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  me              
1542c 2f 2a 20 54 68 65 20 6e 65 77 20 74 61 62 6c 65  /* The new table
1542d 20 6e 61 6d 65 2e 20 2a 2f 0a 29 7b 0a 20 20 69   name. */.){.  i
1542e 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20  nt iDb;         
1542f 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
15430 62 61 73 65 20 74 68 61 74 20 63 6f 6e 74 61 69  base that contai
15431 6e 73 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  ns the table */.
15432 20 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20    char *zDb;    
15433 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
15434 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 20  ame of database 
15435 69 44 62 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  iDb */.  Table *
15436 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20  pTab;           
15437 20 20 20 2f 2a 20 54 61 62 6c 65 20 62 65 69 6e     /* Table bein
15438 67 20 72 65 6e 61 6d 65 64 20 2a 2f 0a 20 20 63  g renamed */.  c
15439 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 20  har *zName = 0; 
1543a 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 55 4c 4c           /* NULL
1543b 2d 74 65 72 6d 69 6e 61 74 65 64 20 76 65 72 73  -terminated vers
1543c 69 6f 6e 20 6f 66 20 70 4e 61 6d 65 20 2a 2f 20  ion of pName */ 
1543d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
1543e 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 2f 2a 20   pParse->db; /* 
1543f 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  Database connect
15440 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 61  ion */.  int nTa
15441 62 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20  bName;          
15442 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
15443 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 73  UTF-8 characters
15444 20 69 6e 20 7a 54 61 62 4e 61 6d 65 20 2a 2f 0a   in zTabName */.
15445 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
15446 61 62 4e 61 6d 65 3b 20 20 20 20 20 2f 2a 20 4f  abName;     /* O
15447 72 69 67 69 6e 61 6c 20 6e 61 6d 65 20 6f 66 20  riginal name of 
15448 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 56  the table */.  V
15449 64 62 65 20 2a 76 3b 0a 23 69 66 6e 64 65 66 20  dbe *v;.#ifndef 
1544a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
1544b 47 45 52 0a 20 20 63 68 61 72 20 2a 7a 57 68 65  GER.  char *zWhe
1544c 72 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  re = 0;         
1544d 2f 2a 20 57 68 65 72 65 20 63 6c 61 75 73 65 20  /* Where clause 
1544e 74 6f 20 6c 6f 63 61 74 65 20 74 65 6d 70 20 74  to locate temp t
1544f 72 69 67 67 65 72 73 20 2a 2f 0a 23 65 6e 64 69  riggers */.#endi
15450 66 0a 20 20 69 6e 74 20 69 73 56 69 72 74 75 61  f.  int isVirtua
15451 6c 52 65 6e 61 6d 65 20 3d 20 30 3b 20 20 2f 2a  lRename = 0;  /*
15452 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69 73   True if this is
15453 20 61 20 76 2d 74 61 62 6c 65 20 77 69 74 68 20   a v-table with 
15454 61 6e 20 78 52 65 6e 61 6d 65 28 29 20 2a 2f 0a  an xRename() */.
15455 20 20 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33    .  if( sqlite3
15456 4d 61 6c 6c 6f 63 46 61 69 6c 65 64 28 29 20 29  MallocFailed() )
15457 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d   goto exit_renam
15458 65 5f 74 61 62 6c 65 3b 0a 20 20 61 73 73 65 72  e_table;.  asser
15459 74 28 20 70 53 72 63 2d 3e 6e 53 72 63 3d 3d 31  t( pSrc->nSrc==1
1545a 20 29 3b 0a 0a 20 20 70 54 61 62 20 3d 20 73 71   );..  pTab = sq
1545b 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65  lite3LocateTable
1545c 28 70 50 61 72 73 65 2c 20 70 53 72 63 2d 3e 61  (pParse, pSrc->a
1545d 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70 53 72 63 2d  [0].zName, pSrc-
1545e 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61 73 65 29  >a[0].zDatabase)
1545f 3b 0a 20 20 69 66 28 20 21 70 54 61 62 20 29 20  ;.  if( !pTab ) 
15460 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65  goto exit_rename
15461 5f 74 61 62 6c 65 3b 0a 20 20 69 44 62 20 3d 20  _table;.  iDb = 
15462 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
15463 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c  ndex(pParse->db,
15464 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
15465 0a 20 20 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62  .  zDb = db->aDb
15466 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 0a 20 20  [iDb].zName;..  
15467 2f 2a 20 47 65 74 20 61 20 4e 55 4c 4c 20 74 65  /* Get a NULL te
15468 72 6d 69 6e 61 74 65 64 20 76 65 72 73 69 6f 6e  rminated version
15469 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c   of the new tabl
1546a 65 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 7a 4e 61  e name. */.  zNa
1546b 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65  me = sqlite3Name
1546c 46 72 6f 6d 54 6f 6b 65 6e 28 70 4e 61 6d 65 29  FromToken(pName)
1546d 3b 0a 20 20 69 66 28 20 21 7a 4e 61 6d 65 20 29  ;.  if( !zName )
1546e 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d   goto exit_renam
1546f 65 5f 74 61 62 6c 65 3b 0a 0a 20 20 2f 2a 20 43  e_table;..  /* C
15470 68 65 63 6b 20 74 68 61 74 20 61 20 74 61 62 6c  heck that a tabl
15471 65 20 6f 72 20 69 6e 64 65 78 20 6e 61 6d 65 64  e or index named
15472 20 27 7a 4e 61 6d 65 27 20 64 6f 65 73 20 6e 6f   'zName' does no
15473 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 0a  t already exist.
15474 20 20 2a 2a 20 69 6e 20 64 61 74 61 62 61 73 65    ** in database
15475 20 69 44 62 2e 20 49 66 20 73 6f 2c 20 74 68 69   iDb. If so, thi
15476 73 20 69 73 20 61 6e 20 65 72 72 6f 72 2e 0a 20  s is an error.. 
15477 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
15478 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a  3FindTable(db, z
15479 4e 61 6d 65 2c 20 7a 44 62 29 20 7c 7c 20 73 71  Name, zDb) || sq
1547a 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 64  lite3FindIndex(d
1547b 62 2c 20 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 29  b, zName, zDb) )
1547c 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
1547d 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20  orMsg(pParse, . 
1547e 20 20 20 20 20 20 20 22 74 68 65 72 65 20 69 73         "there is
1547f 20 61 6c 72 65 61 64 79 20 61 6e 6f 74 68 65 72   already another
15480 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20   table or index 
15481 77 69 74 68 20 74 68 69 73 20 6e 61 6d 65 3a 20  with this name: 
15482 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20  %s", zName);.   
15483 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d   goto exit_renam
15484 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 0a 20 20  e_table;.  }..  
15485 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 69 74 20  /* Make sure it 
15486 69 73 20 6e 6f 74 20 61 20 73 79 73 74 65 6d 20  is not a system 
15487 74 61 62 6c 65 20 62 65 69 6e 67 20 61 6c 74 65  table being alte
15488 72 65 64 2c 20 6f 72 20 61 20 72 65 73 65 72 76  red, or a reserv
15489 65 64 20 6e 61 6d 65 0a 20 20 2a 2a 20 74 68 61  ed name.  ** tha
1548a 74 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 62  t the table is b
1548b 65 69 6e 67 20 72 65 6e 61 6d 65 64 20 74 6f 2e  eing renamed to.
1548c 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 74 72 6c  .  */.  if( strl
1548d 65 6e 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3e  en(pTab->zName)>
1548e 36 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 53  6 && 0==sqlite3S
1548f 74 72 4e 49 43 6d 70 28 70 54 61 62 2d 3e 7a 4e  trNICmp(pTab->zN
15490 61 6d 65 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20  ame, "sqlite_", 
15491 37 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  7) ){.    sqlite
15492 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
15493 2c 20 22 74 61 62 6c 65 20 25 73 20 6d 61 79 20  , "table %s may 
15494 6e 6f 74 20 62 65 20 61 6c 74 65 72 65 64 22 2c  not be altered",
15495 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20   pTab->zName);. 
15496 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e     goto exit_ren
15497 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20  ame_table;.  }. 
15498 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
15499 73 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65  sqlite3CheckObje
1549a 63 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 7a  ctName(pParse, z
1549b 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 67 6f 74  Name) ){.    got
1549c 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61  o exit_rename_ta
1549d 62 6c 65 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65  ble;.  }..#ifnde
1549e 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
1549f 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a  THORIZATION.  /*
154a0 20 49 6e 76 6f 6b 65 20 74 68 65 20 61 75 74 68   Invoke the auth
154a1 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61  orization callba
154a2 63 6b 2e 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ck. */.  if( sql
154a3 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
154a4 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 41 4c 54  arse, SQLITE_ALT
154a5 45 52 5f 54 41 42 4c 45 2c 20 7a 44 62 2c 20 70  ER_TABLE, zDb, p
154a6 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 20 29  Tab->zName, 0) )
154a7 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f  {.    goto exit_
154a8 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20  rename_table;.  
154a9 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65  }.#endif..#ifnde
154aa 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
154ab 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28  RTUALTABLE.  if(
154ac 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43   sqlite3ViewGetC
154ad 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73  olumnNames(pPars
154ae 65 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20 20  e, pTab) ){.    
154af 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65  goto exit_rename
154b0 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 69 66  _table;.  }.  if
154b1 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  ( IsVirtual(pTab
154b2 29 20 26 26 20 70 54 61 62 2d 3e 70 4d 6f 64 2d  ) && pTab->pMod-
154b3 3e 70 4d 6f 64 75 6c 65 2d 3e 78 52 65 6e 61 6d  >pModule->xRenam
154b4 65 20 29 7b 0a 20 20 20 20 69 73 56 69 72 74 75  e ){.    isVirtu
154b5 61 6c 52 65 6e 61 6d 65 20 3d 20 31 3b 0a 20 20  alRename = 1;.  
154b6 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 42  }.#endif..  /* B
154b7 65 67 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69  egin a transacti
154b8 6f 6e 20 61 6e 64 20 63 6f 64 65 20 74 68 65 20  on and code the 
154b9 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 66 6f 72  VerifyCookie for
154ba 20 64 61 74 61 62 61 73 65 20 69 44 62 2e 20 0a   database iDb. .
154bb 20 20 2a 2a 20 54 68 65 6e 20 6d 6f 64 69 66 79    ** Then modify
154bc 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b   the schema cook
154bd 69 65 20 28 73 69 6e 63 65 20 74 68 65 20 41 4c  ie (since the AL
154be 54 45 52 20 54 41 42 4c 45 20 6d 6f 64 69 66 69  TER TABLE modifi
154bf 65 73 20 74 68 65 0a 20 20 2a 2a 20 73 63 68 65  es the.  ** sche
154c0 6d 61 29 2e 20 4f 70 65 6e 20 61 20 73 74 61 74  ma). Open a stat
154c1 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  ement transactio
154c2 6e 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 69  n if the table i
154c3 73 20 61 20 76 69 72 74 75 61 6c 0a 20 20 2a 2a  s a virtual.  **
154c4 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 76   table..  */.  v
154c5 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
154c6 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28  e(pParse);.  if(
154c7 20 76 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74   v==0 ){.    got
154c8 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61  o exit_rename_ta
154c9 62 6c 65 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  ble;.  }.  sqlit
154ca 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72  e3BeginWriteOper
154cb 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 69 73  ation(pParse, is
154cc 56 69 72 74 75 61 6c 52 65 6e 61 6d 65 2c 20 69  VirtualRename, i
154cd 44 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 43 68  Db);.  sqlite3Ch
154ce 61 6e 67 65 43 6f 6f 6b 69 65 28 64 62 2c 20 76  angeCookie(db, v
154cf 2c 20 69 44 62 29 3b 0a 0a 20 20 2f 2a 20 49 66  , iDb);..  /* If
154d0 20 74 68 69 73 20 69 73 20 61 20 76 69 72 74 75   this is a virtu
154d1 61 6c 20 74 61 62 6c 65 2c 20 69 6e 76 6f 6b 65  al table, invoke
154d2 20 74 68 65 20 78 52 65 6e 61 6d 65 28 29 20 66   the xRename() f
154d3 75 6e 63 74 69 6f 6e 20 69 66 0a 20 20 2a 2a 20  unction if.  ** 
154d4 6f 6e 65 20 69 73 20 64 65 66 69 6e 65 64 2e 20  one is defined. 
154d5 54 68 65 20 78 52 65 6e 61 6d 65 28 29 20 63 61  The xRename() ca
154d6 6c 6c 62 61 63 6b 20 77 69 6c 6c 20 6d 6f 64 69  llback will modi
154d7 66 79 20 74 68 65 20 6e 61 6d 65 73 0a 20 20 2a  fy the names.  *
154d8 2a 20 6f 66 20 61 6e 79 20 72 65 73 6f 75 72 63  * of any resourc
154d9 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20 76  es used by the v
154da 2d 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  -table implement
154db 61 74 69 6f 6e 20 28 69 6e 63 6c 75 64 69 6e 67  ation (including
154dc 20 6f 74 68 65 72 0a 20 20 2a 2a 20 53 51 4c 69   other.  ** SQLi
154dd 74 65 20 74 61 62 6c 65 73 29 20 74 68 61 74 20  te tables) that 
154de 61 72 65 20 69 64 65 6e 74 69 66 69 65 64 20 62  are identified b
154df 79 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  y the name of th
154e0 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  e virtual table.
154e1 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  .  */.#ifndef SQ
154e2 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
154e3 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 69 73 56  LTABLE.  if( isV
154e4 69 72 74 75 61 6c 52 65 6e 61 6d 65 20 29 7b 0a  irtualRename ){.
154e5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4f      sqlite3VdbeO
154e6 70 33 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p3(v, OP_String8
154e7 2c 20 30 2c 20 30 2c 20 7a 4e 61 6d 65 2c 20 30  , 0, 0, zName, 0
154e8 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
154e9 62 65 4f 70 33 28 76 2c 20 4f 50 5f 56 52 65 6e  beOp3(v, OP_VRen
154ea 61 6d 65 2c 20 30 2c 20 30 2c 20 28 63 6f 6e 73  ame, 0, 0, (cons
154eb 74 20 63 68 61 72 2a 29 70 54 61 62 2d 3e 70 56  t char*)pTab->pV
154ec 74 61 62 2c 20 50 33 5f 56 54 41 42 29 3b 0a 20  tab, P3_VTAB);. 
154ed 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20   }.#endif..  /* 
154ee 66 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d  figure out how m
154ef 61 6e 79 20 55 54 46 2d 38 20 63 68 61 72 61 63  any UTF-8 charac
154f0 74 65 72 73 20 61 72 65 20 69 6e 20 7a 4e 61 6d  ters are in zNam
154f1 65 20 2a 2f 0a 20 20 7a 54 61 62 4e 61 6d 65 20  e */.  zTabName 
154f2 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20  = pTab->zName;. 
154f3 20 6e 54 61 62 4e 61 6d 65 20 3d 20 73 71 6c 69   nTabName = sqli
154f4 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 7a  te3Utf8CharLen(z
154f5 54 61 62 4e 61 6d 65 2c 20 2d 31 29 3b 0a 0a 20  TabName, -1);.. 
154f6 20 2f 2a 20 4d 6f 64 69 66 79 20 74 68 65 20 73   /* Modify the s
154f7 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
154f8 6c 65 20 74 6f 20 75 73 65 20 74 68 65 20 6e 65  le to use the ne
154f9 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 2a 2f  w table name. */
154fa 0a 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64  .  sqlite3Nested
154fb 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20  Parse(pParse,.  
154fc 20 20 20 20 22 55 50 44 41 54 45 20 25 51 2e 25      "UPDATE %Q.%
154fd 73 20 53 45 54 20 22 0a 23 69 66 64 65 66 20 53  s SET ".#ifdef S
154fe 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
154ff 45 52 0a 20 20 20 20 20 20 20 20 20 20 22 73 71  ER.          "sq
15500 6c 20 3d 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d  l = sqlite_renam
15501 65 5f 74 61 62 6c 65 28 73 71 6c 2c 20 25 51 29  e_table(sql, %Q)
15502 2c 20 22 0a 23 65 6c 73 65 0a 20 20 20 20 20 20  , ".#else.      
15503 20 20 20 20 22 73 71 6c 20 3d 20 43 41 53 45 20      "sql = CASE 
15504 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 57  ".            "W
15505 48 45 4e 20 74 79 70 65 20 3d 20 27 74 72 69 67  HEN type = 'trig
15506 67 65 72 27 20 54 48 45 4e 20 73 71 6c 69 74 65  ger' THEN sqlite
15507 5f 72 65 6e 61 6d 65 5f 74 72 69 67 67 65 72 28  _rename_trigger(
15508 73 71 6c 2c 20 25 51 29 22 0a 20 20 20 20 20 20  sql, %Q)".      
15509 20 20 20 20 20 20 22 45 4c 53 45 20 73 71 6c 69        "ELSE sqli
1550a 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28  te_rename_table(
1550b 73 71 6c 2c 20 25 51 29 20 45 4e 44 2c 20 22 0a  sql, %Q) END, ".
1550c 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 20  #endif.         
1550d 20 22 74 62 6c 5f 6e 61 6d 65 20 3d 20 25 51 2c   "tbl_name = %Q,
1550e 20 22 0a 20 20 20 20 20 20 20 20 20 20 22 6e 61   ".          "na
1550f 6d 65 20 3d 20 43 41 53 45 20 22 0a 20 20 20 20  me = CASE ".    
15510 20 20 20 20 20 20 20 20 22 57 48 45 4e 20 74 79          "WHEN ty
15511 70 65 3d 27 74 61 62 6c 65 27 20 54 48 45 4e 20  pe='table' THEN 
15512 25 51 20 22 0a 20 20 20 20 20 20 20 20 20 20 20  %Q ".           
15513 20 22 57 48 45 4e 20 6e 61 6d 65 20 4c 49 4b 45   "WHEN name LIKE
15514 20 27 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e 64   'sqlite_autoind
15515 65 78 25 25 27 20 41 4e 44 20 74 79 70 65 3d 27  ex%%' AND type='
15516 69 6e 64 65 78 27 20 54 48 45 4e 20 22 0a 20 20  index' THEN ".  
15517 20 20 20 20 20 20 20 20 20 20 20 22 27 73 71 6c             "'sql
15518 69 74 65 5f 61 75 74 6f 69 6e 64 65 78 5f 27 20  ite_autoindex_' 
15519 7c 7c 20 25 51 20 7c 7c 20 73 75 62 73 74 72 28  || %Q || substr(
1551a 6e 61 6d 65 2c 25 64 2b 31 38 2c 31 30 29 20 22  name,%d+18,10) "
1551b 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 45 4c  .            "EL
1551c 53 45 20 6e 61 6d 65 20 45 4e 44 20 22 0a 20 20  SE name END ".  
1551d 20 20 20 20 22 57 48 45 52 45 20 74 62 6c 5f 6e      "WHERE tbl_n
1551e 61 6d 65 3d 25 51 20 41 4e 44 20 22 0a 20 20 20  ame=%Q AND ".   
1551f 20 20 20 20 20 20 20 22 28 74 79 70 65 3d 27 74         "(type='t
15520 61 62 6c 65 27 20 4f 52 20 74 79 70 65 3d 27 69  able' OR type='i
15521 6e 64 65 78 27 20 4f 52 20 74 79 70 65 3d 27 74  ndex' OR type='t
15522 72 69 67 67 65 72 27 29 3b 22 2c 20 0a 20 20 20  rigger');", .   
15523 20 20 20 7a 44 62 2c 20 53 43 48 45 4d 41 5f 54     zDb, SCHEMA_T
15524 41 42 4c 45 28 69 44 62 29 2c 20 7a 4e 61 6d 65  ABLE(iDb), zName
15525 2c 20 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20  , zName, zName, 
15526 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
15527 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20  OMIT_TRIGGER.   
15528 20 20 20 7a 4e 61 6d 65 2c 0a 23 65 6e 64 69 66     zName,.#endif
15529 0a 20 20 20 20 20 20 7a 4e 61 6d 65 2c 20 6e 54  .      zName, nT
1552a 61 62 4e 61 6d 65 2c 20 7a 54 61 62 4e 61 6d 65  abName, zTabName
1552b 0a 20 20 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53  .  );..#ifndef S
1552c 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49  QLITE_OMIT_AUTOI
1552d 4e 43 52 45 4d 45 4e 54 0a 20 20 2f 2a 20 49 66  NCREMENT.  /* If
1552e 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75   the sqlite_sequ
1552f 65 6e 63 65 20 74 61 62 6c 65 20 65 78 69 73 74  ence table exist
15530 73 20 69 6e 20 74 68 69 73 20 64 61 74 61 62 61  s in this databa
15531 73 65 2c 20 74 68 65 6e 20 75 70 64 61 74 65 20  se, then update 
15532 0a 20 20 2a 2a 20 69 74 20 77 69 74 68 20 74 68  .  ** it with th
15533 65 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65  e new table name
15534 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ..  */.  if( sql
15535 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62  ite3FindTable(db
15536 2c 20 22 73 71 6c 69 74 65 5f 73 65 71 75 65 6e  , "sqlite_sequen
15537 63 65 22 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20  ce", zDb) ){.   
15538 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61   sqlite3NestedPa
15539 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20  rse(pParse,.    
1553a 20 20 20 20 22 55 50 44 41 54 45 20 25 51 2e 73      "UPDATE %Q.s
1553b 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 73  qlite_sequence s
1553c 65 74 20 6e 61 6d 65 20 3d 20 25 51 20 57 48 45  et name = %Q WHE
1553d 52 45 20 6e 61 6d 65 20 3d 20 25 51 22 2c 0a 20  RE name = %Q",. 
1553e 20 20 20 20 20 20 20 7a 44 62 2c 20 7a 4e 61 6d         zDb, zNam
1553f 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b  e, pTab->zName);
15540 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66  .  }.#endif..#if
15541 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
15542 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a 20 49 66  _TRIGGER.  /* If
15543 20 74 68 65 72 65 20 61 72 65 20 54 45 4d 50 20   there are TEMP 
15544 74 72 69 67 67 65 72 73 20 6f 6e 20 74 68 69 73  triggers on this
15545 20 74 61 62 6c 65 2c 20 6d 6f 64 69 66 79 20 74   table, modify t
15546 68 65 20 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d  he sqlite_temp_m
15547 61 73 74 65 72 0a 20 20 2a 2a 20 74 61 62 6c 65  aster.  ** table
15548 2e 20 44 6f 6e 27 74 20 64 6f 20 74 68 69 73 20  . Don't do this 
15549 69 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 69  if the table bei
1554a 6e 67 20 41 4c 54 45 52 65 64 20 69 73 20 69 74  ng ALTERed is it
1554b 73 65 6c 66 20 6c 6f 63 61 74 65 64 20 69 6e 0a  self located in.
1554c 20 20 2a 2a 20 74 68 65 20 74 65 6d 70 20 64 61    ** the temp da
1554d 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 69  tabase..  */.  i
1554e 66 28 20 28 7a 57 68 65 72 65 3d 77 68 65 72 65  f( (zWhere=where
1554f 54 65 6d 70 54 72 69 67 67 65 72 73 28 70 50 61  TempTriggers(pPa
15550 72 73 65 2c 20 70 54 61 62 29 29 21 3d 30 20 29  rse, pTab))!=0 )
15551 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73  {.    sqlite3Nes
15552 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c  tedParse(pParse,
15553 20 0a 20 20 20 20 20 20 20 20 22 55 50 44 41 54   .        "UPDAT
15554 45 20 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61  E sqlite_temp_ma
15555 73 74 65 72 20 53 45 54 20 22 0a 20 20 20 20 20  ster SET ".     
15556 20 20 20 20 20 20 20 22 73 71 6c 20 3d 20 73 71         "sql = sq
15557 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 72 69 67  lite_rename_trig
15558 67 65 72 28 73 71 6c 2c 20 25 51 29 2c 20 22 0a  ger(sql, %Q), ".
15559 20 20 20 20 20 20 20 20 20 20 20 20 22 74 62 6c              "tbl
1555a 5f 6e 61 6d 65 20 3d 20 25 51 20 22 0a 20 20 20  _name = %Q ".   
1555b 20 20 20 20 20 20 20 20 20 22 57 48 45 52 45 20           "WHERE 
1555c 25 73 3b 22 2c 20 7a 4e 61 6d 65 2c 20 7a 4e 61  %s;", zName, zNa
1555d 6d 65 2c 20 7a 57 68 65 72 65 29 3b 0a 20 20 20  me, zWhere);.   
1555e 20 73 71 6c 69 74 65 46 72 65 65 28 7a 57 68 65   sqliteFree(zWhe
1555f 72 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  re);.  }.#endif.
15560 0a 20 20 2f 2a 20 44 72 6f 70 20 61 6e 64 20 72  .  /* Drop and r
15561 65 6c 6f 61 64 20 74 68 65 20 69 6e 74 65 72 6e  eload the intern
15562 61 6c 20 74 61 62 6c 65 20 73 63 68 65 6d 61 2e  al table schema.
15563 20 2a 2f 0a 20 20 72 65 6c 6f 61 64 54 61 62 6c   */.  reloadTabl
15564 65 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20  eSchema(pParse, 
15565 70 54 61 62 2c 20 7a 4e 61 6d 65 29 3b 0a 0a 65  pTab, zName);..e
15566 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65  xit_rename_table
15567 3a 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69  :.  sqlite3SrcLi
15568 73 74 44 65 6c 65 74 65 28 70 53 72 63 29 3b 0a  stDelete(pSrc);.
15569 20 20 73 71 6c 69 74 65 46 72 65 65 28 7a 4e 61    sqliteFree(zNa
1556a 6d 65 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54  me);.}.../*.** T
1556b 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
1556c 63 61 6c 6c 65 64 20 61 66 74 65 72 20 61 6e 20  called after an 
1556d 22 41 4c 54 45 52 20 54 41 42 4c 45 20 2e 2e 2e  "ALTER TABLE ...
1556e 20 41 44 44 22 20 73 74 61 74 65 6d 65 6e 74 0a   ADD" statement.
1556f 2a 2a 20 68 61 73 20 62 65 65 6e 20 70 61 72 73  ** has been pars
15570 65 64 2e 20 41 72 67 75 6d 65 6e 74 20 70 43 6f  ed. Argument pCo
15571 6c 44 65 66 20 63 6f 6e 74 61 69 6e 73 20 74 68  lDef contains th
15572 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 6e 65  e text of the ne
15573 77 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 64 65 66 69  w.** column defi
15574 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  nition..**.** Th
15575 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72  e Table structur
15576 65 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  e pParse->pNewTa
15577 62 6c 65 20 77 61 73 20 65 78 74 65 6e 64 65 64  ble was extended
15578 20 74 6f 20 69 6e 63 6c 75 64 65 0a 2a 2a 20 74   to include.** t
15579 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 64 75  he new column du
1557a 72 69 6e 67 20 70 61 72 73 69 6e 67 2e 0a 2a 2f  ring parsing..*/
1557b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1557c 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65  void sqlite3Alte
1557d 72 46 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e  rFinishAddColumn
1557e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
1557f 54 6f 6b 65 6e 20 2a 70 43 6f 6c 44 65 66 29 7b  Token *pColDef){
15580 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 77 3b 20  .  Table *pNew; 
15581 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
15582 43 6f 70 79 20 6f 66 20 70 50 61 72 73 65 2d 3e  Copy of pParse->
15583 70 4e 65 77 54 61 62 6c 65 20 2a 2f 0a 20 20 54  pNewTable */.  T
15584 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20  able *pTab;     
15585 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c           /* Tabl
15586 65 20 62 65 69 6e 67 20 61 6c 74 65 72 65 64 20  e being altered 
15587 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20  */.  int iDb;   
15588 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
15589 2a 20 44 61 74 61 62 61 73 65 20 6e 75 6d 62 65  * Database numbe
1558a 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  r */.  const cha
1558b 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 20 20 20  r *zDb;         
1558c 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 61 6d   /* Database nam
1558d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  e */.  const cha
1558e 72 20 2a 7a 54 61 62 3b 20 20 20 20 20 20 20 20  r *zTab;        
1558f 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20 2a   /* Table name *
15590 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 3b 20  /.  char *zCol; 
15591 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
15592 20 4e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 64   Null-terminated
15593 20 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69   column definiti
15594 6f 6e 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a  on */.  Column *
15595 70 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20  pCol;           
15596 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 63 6f 6c    /* The new col
15597 75 6d 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  umn */.  Expr *p
15598 44 66 6c 74 3b 20 20 20 20 20 20 20 20 20 20 20  Dflt;           
15599 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 76 61     /* Default va
1559a 6c 75 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20  lue for the new 
1559b 63 6f 6c 75 6d 6e 20 2a 2f 0a 0a 20 20 69 66 28  column */..  if(
1559c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 20   pParse->nErr ) 
1559d 72 65 74 75 72 6e 3b 0a 20 20 70 4e 65 77 20 3d  return;.  pNew =
1559e 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
1559f 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4e  le;.  assert( pN
155a0 65 77 20 29 3b 0a 0a 20 20 69 44 62 20 3d 20 73  ew );..  iDb = s
155a1 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
155a2 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  dex(pParse->db, 
155a3 70 4e 65 77 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  pNew->pSchema);.
155a4 20 20 7a 44 62 20 3d 20 70 50 61 72 73 65 2d 3e    zDb = pParse->
155a5 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
155a6 6d 65 3b 0a 20 20 7a 54 61 62 20 3d 20 70 4e 65  me;.  zTab = pNe
155a7 77 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 70 43 6f 6c  w->zName;.  pCol
155a8 20 3d 20 26 70 4e 65 77 2d 3e 61 43 6f 6c 5b 70   = &pNew->aCol[p
155a9 4e 65 77 2d 3e 6e 43 6f 6c 2d 31 5d 3b 0a 20 20  New->nCol-1];.  
155aa 70 44 66 6c 74 20 3d 20 70 43 6f 6c 2d 3e 70 44  pDflt = pCol->pD
155ab 66 6c 74 3b 0a 20 20 70 54 61 62 20 3d 20 73 71  flt;.  pTab = sq
155ac 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 70  lite3FindTable(p
155ad 50 61 72 73 65 2d 3e 64 62 2c 20 7a 54 61 62 2c  Parse->db, zTab,
155ae 20 7a 44 62 29 3b 0a 20 20 61 73 73 65 72 74 28   zDb);.  assert(
155af 20 70 54 61 62 20 29 3b 0a 0a 23 69 66 6e 64 65   pTab );..#ifnde
155b0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
155b1 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a  THORIZATION.  /*
155b2 20 49 6e 76 6f 6b 65 20 74 68 65 20 61 75 74 68   Invoke the auth
155b3 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61  orization callba
155b4 63 6b 2e 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ck. */.  if( sql
155b5 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
155b6 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 41 4c 54  arse, SQLITE_ALT
155b7 45 52 5f 54 41 42 4c 45 2c 20 7a 44 62 2c 20 70  ER_TABLE, zDb, p
155b8 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 20 29  Tab->zName, 0) )
155b9 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
155ba 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49  }.#endif..  /* I
155bb 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 76 61  f the default va
155bc 6c 75 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20  lue for the new 
155bd 63 6f 6c 75 6d 6e 20 77 61 73 20 73 70 65 63 69  column was speci
155be 66 69 65 64 20 77 69 74 68 20 61 20 0a 20 20 2a  fied with a .  *
155bf 2a 20 6c 69 74 65 72 61 6c 20 4e 55 4c 4c 2c 20  * literal NULL, 
155c0 74 68 65 6e 20 73 65 74 20 70 44 66 6c 74 20 74  then set pDflt t
155c1 6f 20 30 2e 20 54 68 69 73 20 73 69 6d 70 6c 69  o 0. This simpli
155c2 66 69 65 73 20 63 68 65 63 6b 69 6e 67 0a 20 20  fies checking.  
155c3 2a 2a 20 66 6f 72 20 61 6e 20 53 51 4c 20 4e 55  ** for an SQL NU
155c4 4c 4c 20 64 65 66 61 75 6c 74 20 62 65 6c 6f 77  LL default below
155c5 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 44 66  ..  */.  if( pDf
155c6 6c 74 20 26 26 20 70 44 66 6c 74 2d 3e 6f 70 3d  lt && pDflt->op=
155c7 3d 54 4b 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20  =TK_NULL ){.    
155c8 70 44 66 6c 74 20 3d 20 30 3b 0a 20 20 7d 0a 0a  pDflt = 0;.  }..
155c9 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20    /* Check that 
155ca 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 69  the new column i
155cb 73 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64 20  s not specified 
155cc 61 73 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f  as PRIMARY KEY o
155cd 72 20 55 4e 49 51 55 45 2e 0a 20 20 2a 2a 20 49  r UNIQUE..  ** I
155ce 66 20 74 68 65 72 65 20 69 73 20 61 20 4e 4f 54  f there is a NOT
155cf 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74   NULL constraint
155d0 2c 20 74 68 65 6e 20 74 68 65 20 64 65 66 61 75  , then the defau
155d1 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65  lt value for the
155d2 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6d 75 73  .  ** column mus
155d3 74 20 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e 0a 20  t not be NULL.. 
155d4 20 2a 2f 0a 20 20 69 66 28 20 70 43 6f 6c 2d 3e   */.  if( pCol->
155d5 69 73 50 72 69 6d 4b 65 79 20 29 7b 0a 20 20 20  isPrimKey ){.   
155d6 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
155d7 28 70 50 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74  (pParse, "Cannot
155d8 20 61 64 64 20 61 20 50 52 49 4d 41 52 59 20 4b   add a PRIMARY K
155d9 45 59 20 63 6f 6c 75 6d 6e 22 29 3b 0a 20 20 20  EY column");.   
155da 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69   return;.  }.  i
155db 66 28 20 70 4e 65 77 2d 3e 70 49 6e 64 65 78 20  f( pNew->pIndex 
155dc 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
155dd 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
155de 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 55 4e 49  Cannot add a UNI
155df 51 55 45 20 63 6f 6c 75 6d 6e 22 29 3b 0a 20 20  QUE column");.  
155e0 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
155e1 69 66 28 20 70 43 6f 6c 2d 3e 6e 6f 74 4e 75 6c  if( pCol->notNul
155e2 6c 20 26 26 20 21 70 44 66 6c 74 20 29 7b 0a 20  l && !pDflt ){. 
155e3 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
155e4 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20  sg(pParse, .    
155e5 20 20 20 20 22 43 61 6e 6e 6f 74 20 61 64 64 20      "Cannot add 
155e6 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6c 75 6d  a NOT NULL colum
155e7 6e 20 77 69 74 68 20 64 65 66 61 75 6c 74 20 76  n with default v
155e8 61 6c 75 65 20 4e 55 4c 4c 22 29 3b 0a 20 20 20  alue NULL");.   
155e9 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20   return;.  }..  
155ea 2f 2a 20 45 6e 73 75 72 65 20 74 68 65 20 64 65  /* Ensure the de
155eb 66 61 75 6c 74 20 65 78 70 72 65 73 73 69 6f 6e  fault expression
155ec 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 74 68   is something th
155ed 61 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46  at sqlite3ValueF
155ee 72 6f 6d 45 78 70 72 28 29 0a 20 20 2a 2a 20 63  romExpr().  ** c
155ef 61 6e 20 68 61 6e 64 6c 65 20 28 69 2e 65 2e 20  an handle (i.e. 
155f0 6e 6f 74 20 43 55 52 52 45 4e 54 5f 54 49 4d 45  not CURRENT_TIME
155f1 20 65 74 63 2e 29 0a 20 20 2a 2f 0a 20 20 69 66   etc.).  */.  if
155f2 28 20 70 44 66 6c 74 20 29 7b 0a 20 20 20 20 73  ( pDflt ){.    s
155f3 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56  qlite3_value *pV
155f4 61 6c 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  al;.    if( sqli
155f5 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72  te3ValueFromExpr
155f6 28 70 44 66 6c 74 2c 20 53 51 4c 49 54 45 5f 55  (pDflt, SQLITE_U
155f7 54 46 38 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f  TF8, SQLITE_AFF_
155f8 4e 4f 4e 45 2c 20 26 70 56 61 6c 29 20 29 7b 0a  NONE, &pVal) ){.
155f9 20 20 20 20 20 20 2f 2a 20 6d 61 6c 6c 6f 63 28        /* malloc(
155fa 29 20 68 61 73 20 66 61 69 6c 65 64 20 2a 2f 0a  ) has failed */.
155fb 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
155fc 20 20 7d 0a 20 20 20 20 69 66 28 20 21 70 56 61    }.    if( !pVa
155fd 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  l ){.      sqlit
155fe 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
155ff 65 2c 20 22 43 61 6e 6e 6f 74 20 61 64 64 20 61  e, "Cannot add a
15600 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20 6e 6f 6e   column with non
15601 2d 63 6f 6e 73 74 61 6e 74 20 64 65 66 61 75 6c  -constant defaul
15602 74 22 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  t");.      retur
15603 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  n;.    }.    sql
15604 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 70 56  ite3ValueFree(pV
15605 61 6c 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d  al);.  }..  /* M
15606 6f 64 69 66 79 20 74 68 65 20 43 52 45 41 54 45  odify the CREATE
15607 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74   TABLE statement
15608 2e 20 2a 2f 0a 20 20 7a 43 6f 6c 20 3d 20 73 71  . */.  zCol = sq
15609 6c 69 74 65 53 74 72 4e 44 75 70 28 28 63 68 61  liteStrNDup((cha
1560a 72 2a 29 70 43 6f 6c 44 65 66 2d 3e 7a 2c 20 70  r*)pColDef->z, p
1560b 43 6f 6c 44 65 66 2d 3e 6e 29 3b 0a 20 20 69 66  ColDef->n);.  if
1560c 28 20 7a 43 6f 6c 20 29 7b 0a 20 20 20 20 63 68  ( zCol ){.    ch
1560d 61 72 20 2a 7a 45 6e 64 20 3d 20 26 7a 43 6f 6c  ar *zEnd = &zCol
1560e 5b 70 43 6f 6c 44 65 66 2d 3e 6e 2d 31 5d 3b 0a  [pColDef->n-1];.
1560f 20 20 20 20 77 68 69 6c 65 28 20 28 7a 45 6e 64      while( (zEnd
15610 3e 7a 43 6f 6c 20 26 26 20 2a 7a 45 6e 64 3d 3d  >zCol && *zEnd==
15611 27 3b 27 29 20 7c 7c 20 69 73 73 70 61 63 65 28  ';') || isspace(
15612 2a 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  *(unsigned char 
15613 2a 29 7a 45 6e 64 29 20 29 7b 0a 20 20 20 20 20  *)zEnd) ){.     
15614 20 2a 7a 45 6e 64 2d 2d 20 3d 20 27 5c 30 27 3b   *zEnd-- = '\0';
15615 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
15616 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50  e3NestedParse(pP
15617 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22  arse, .        "
15618 55 50 44 41 54 45 20 25 51 2e 25 73 20 53 45 54  UPDATE %Q.%s SET
15619 20 22 0a 20 20 20 20 20 20 20 20 20 20 22 73 71   ".          "sq
1561a 6c 20 3d 20 73 75 62 73 74 72 28 73 71 6c 2c 31  l = substr(sql,1
1561b 2c 25 64 29 20 7c 7c 20 27 2c 20 27 20 7c 7c 20  ,%d) || ', ' || 
1561c 25 51 20 7c 7c 20 73 75 62 73 74 72 28 73 71 6c  %Q || substr(sql
1561d 2c 25 64 2c 6c 65 6e 67 74 68 28 73 71 6c 29 29  ,%d,length(sql))
1561e 20 22 0a 20 20 20 20 20 20 20 20 22 57 48 45 52   ".        "WHER
1561f 45 20 74 79 70 65 20 3d 20 27 74 61 62 6c 65 27  E type = 'table'
15620 20 41 4e 44 20 6e 61 6d 65 20 3d 20 25 51 22 2c   AND name = %Q",
15621 20 0a 20 20 20 20 20 20 7a 44 62 2c 20 53 43 48   .      zDb, SCH
15622 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20  EMA_TABLE(iDb), 
15623 70 4e 65 77 2d 3e 61 64 64 43 6f 6c 4f 66 66 73  pNew->addColOffs
15624 65 74 2c 20 7a 43 6f 6c 2c 20 70 4e 65 77 2d 3e  et, zCol, pNew->
15625 61 64 64 43 6f 6c 4f 66 66 73 65 74 2b 31 2c 0a  addColOffset+1,.
15626 20 20 20 20 20 20 7a 54 61 62 0a 20 20 20 20 29        zTab.    )
15627 3b 0a 20 20 20 20 73 71 6c 69 74 65 46 72 65 65  ;.    sqliteFree
15628 28 7a 43 6f 6c 29 3b 0a 20 20 7d 0a 0a 20 20 2f  (zCol);.  }..  /
15629 2a 20 49 66 20 74 68 65 20 64 65 66 61 75 6c 74  * If the default
1562a 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6e 65   value of the ne
1562b 77 20 63 6f 6c 75 6d 6e 20 69 73 20 4e 55 4c 4c  w column is NULL
1562c 2c 20 74 68 65 6e 20 73 65 74 20 74 68 65 20 66  , then set the f
1562d 69 6c 65 0a 20 20 2a 2a 20 66 6f 72 6d 61 74 20  ile.  ** format 
1562e 74 6f 20 32 2e 20 49 66 20 74 68 65 20 64 65 66  to 2. If the def
1562f 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 74 68  ault value of th
15630 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 69 73 20  e new column is 
15631 6e 6f 74 20 4e 55 4c 4c 2c 0a 20 20 2a 2a 20 74  not NULL,.  ** t
15632 68 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 62  he file format b
15633 65 63 6f 6d 65 73 20 33 2e 0a 20 20 2a 2f 0a 20  ecomes 3..  */. 
15634 20 73 71 6c 69 74 65 33 4d 69 6e 69 6d 75 6d 46   sqlite3MinimumF
15635 69 6c 65 46 6f 72 6d 61 74 28 70 50 61 72 73 65  ileFormat(pParse
15636 2c 20 69 44 62 2c 20 70 44 66 6c 74 20 3f 20 33  , iDb, pDflt ? 3
15637 20 3a 20 32 29 3b 0a 0a 20 20 2f 2a 20 52 65 6c   : 2);..  /* Rel
15638 6f 61 64 20 74 68 65 20 73 63 68 65 6d 61 20 6f  oad the schema o
15639 66 20 74 68 65 20 6d 6f 64 69 66 69 65 64 20 74  f the modified t
1563a 61 62 6c 65 2e 20 2a 2f 0a 20 20 72 65 6c 6f 61  able. */.  reloa
1563b 64 54 61 62 6c 65 53 63 68 65 6d 61 28 70 50 61  dTableSchema(pPa
1563c 72 73 65 2c 20 70 54 61 62 2c 20 70 54 61 62 2d  rse, pTab, pTab-
1563d 3e 7a 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  >zName);.}../*.*
1563e 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
1563f 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65  is called by the
15640 20 70 61 72 73 65 72 20 61 66 74 65 72 20 74 68   parser after th
15641 65 20 74 61 62 6c 65 2d 6e 61 6d 65 20 69 6e 0a  e table-name in.
15642 2a 2a 20 61 6e 20 22 41 4c 54 45 52 20 54 41 42  ** an "ALTER TAB
15643 4c 45 20 3c 74 61 62 6c 65 2d 6e 61 6d 65 3e 20  LE <table-name> 
15644 41 44 44 22 20 73 74 61 74 65 6d 65 6e 74 20 69  ADD" statement i
15645 73 20 70 61 72 73 65 64 2e 20 41 72 67 75 6d 65  s parsed. Argume
15646 6e 74 20 0a 2a 2a 20 70 53 72 63 20 69 73 20 74  nt .** pSrc is t
15647 68 65 20 66 75 6c 6c 2d 6e 61 6d 65 20 6f 66 20  he full-name of 
15648 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20  the table being 
15649 61 6c 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  altered..**.** T
1564a 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 61 6b 65  his routine make
1564b 73 20 61 20 28 70 61 72 74 69 61 6c 29 20 63 6f  s a (partial) co
1564c 70 79 20 6f 66 20 74 68 65 20 54 61 62 6c 65 20  py of the Table 
1564d 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 66 6f 72  structure.** for
1564e 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   the table being
1564f 20 61 6c 74 65 72 65 64 20 61 6e 64 20 73 65 74   altered and set
15650 73 20 50 61 72 73 65 2e 70 4e 65 77 54 61 62 6c  s Parse.pNewTabl
15651 65 20 74 6f 20 70 6f 69 6e 74 0a 2a 2a 20 74 6f  e to point.** to
15652 20 69 74 2e 20 52 6f 75 74 69 6e 65 73 20 63 61   it. Routines ca
15653 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73  lled by the pars
15654 65 72 20 61 73 20 74 68 65 20 63 6f 6c 75 6d 6e  er as the column
15655 20 64 65 66 69 6e 69 74 69 6f 6e 0a 2a 2a 20 69   definition.** i
15656 73 20 70 61 72 73 65 64 20 28 69 2e 65 2e 20 73  s parsed (i.e. s
15657 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 28  qlite3AddColumn(
15658 29 29 20 61 64 64 20 74 68 65 20 6e 65 77 20 43  )) add the new C
15659 6f 6c 75 6d 6e 20 64 61 74 61 20 74 6f 20 0a 2a  olumn data to .*
1565a 2a 20 74 68 65 20 63 6f 70 79 2e 20 54 68 65 20  * the copy. The 
1565b 63 6f 70 79 20 6f 66 20 74 68 65 20 54 61 62 6c  copy of the Tabl
1565c 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20 64  e structure is d
1565d 65 6c 65 74 65 64 20 62 79 20 74 6f 6b 65 6e 69  eleted by tokeni
1565e 7a 65 2e 63 20 0a 2a 2a 20 61 66 74 65 72 20 70  ze.c .** after p
1565f 61 72 73 69 6e 67 20 69 73 20 66 69 6e 69 73 68  arsing is finish
15660 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 6f 75 74 69 6e  ed..**.** Routin
15661 65 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46 69  e sqlite3AlterFi
15662 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28 29 20  nishAddColumn() 
15663 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 74  will be called t
15664 6f 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 63 6f  o complete.** co
15665 64 69 6e 67 20 74 68 65 20 22 41 4c 54 45 52 20  ding the "ALTER 
15666 54 41 42 4c 45 20 2e 2e 2e 20 41 44 44 22 20 73  TABLE ... ADD" s
15667 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c  tatement..*/.SQL
15668 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
15669 20 73 71 6c 69 74 65 33 41 6c 74 65 72 42 65 67   sqlite3AlterBeg
1566a 69 6e 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73  inAddColumn(Pars
1566b 65 20 2a 70 50 61 72 73 65 2c 20 53 72 63 4c 69  e *pParse, SrcLi
1566c 73 74 20 2a 70 53 72 63 29 7b 0a 20 20 54 61 62  st *pSrc){.  Tab
1566d 6c 65 20 2a 70 4e 65 77 3b 0a 20 20 54 61 62 6c  le *pNew;.  Tabl
1566e 65 20 2a 70 54 61 62 3b 0a 20 20 56 64 62 65 20  e *pTab;.  Vdbe 
1566f 2a 76 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20  *v;.  int iDb;. 
15670 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 41   int i;.  int nA
15671 6c 6c 6f 63 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 6b  lloc;..  /* Look
15672 20 75 70 20 74 68 65 20 74 61 62 6c 65 20 62 65   up the table be
15673 69 6e 67 20 61 6c 74 65 72 65 64 2e 20 2a 2f 0a  ing altered. */.
15674 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65    assert( pParse
15675 2d 3e 70 4e 65 77 54 61 62 6c 65 3d 3d 30 20 29  ->pNewTable==0 )
15676 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 4d  ;.  if( sqlite3M
15677 61 6c 6c 6f 63 46 61 69 6c 65 64 28 29 20 29 20  allocFailed() ) 
15678 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f  goto exit_begin_
15679 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 70 54  add_column;.  pT
1567a 61 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61  ab = sqlite3Loca
1567b 74 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  teTable(pParse, 
1567c 70 53 72 63 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65  pSrc->a[0].zName
1567d 2c 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 7a 44 61  , pSrc->a[0].zDa
1567e 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20 21  tabase);.  if( !
1567f 70 54 61 62 20 29 20 67 6f 74 6f 20 65 78 69 74  pTab ) goto exit
15680 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d  _begin_add_colum
15681 6e 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  n;..#ifndef SQLI
15682 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
15683 41 42 4c 45 0a 20 20 69 66 28 20 49 73 56 69 72  ABLE.  if( IsVir
15684 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20  tual(pTab) ){.  
15685 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
15686 67 28 70 50 61 72 73 65 2c 20 22 76 69 72 74 75  g(pParse, "virtu
15687 61 6c 20 74 61 62 6c 65 73 20 6d 61 79 20 6e 6f  al tables may no
15688 74 20 62 65 20 61 6c 74 65 72 65 64 22 29 3b 0a  t be altered");.
15689 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 62 65      goto exit_be
1568a 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a  gin_add_column;.
1568b 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
1568c 20 4d 61 6b 65 20 73 75 72 65 20 74 68 69 73 20   Make sure this 
1568d 69 73 20 6e 6f 74 20 61 6e 20 61 74 74 65 6d 70  is not an attemp
1568e 74 20 74 6f 20 41 4c 54 45 52 20 61 20 76 69 65  t to ALTER a vie
1568f 77 2e 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62  w. */.  if( pTab
15690 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20  ->pSelect ){.   
15691 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
15692 28 70 50 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74  (pParse, "Cannot
15693 20 61 64 64 20 61 20 63 6f 6c 75 6d 6e 20 74 6f   add a column to
15694 20 61 20 76 69 65 77 22 29 3b 0a 20 20 20 20 67   a view");.    g
15695 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61  oto exit_begin_a
15696 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 0a  dd_column;.  }..
15697 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e    assert( pTab->
15698 61 64 64 43 6f 6c 4f 66 66 73 65 74 3e 30 20 29  addColOffset>0 )
15699 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65  ;.  iDb = sqlite
1569a 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70  3SchemaToIndex(p
1569b 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d  Parse->db, pTab-
1569c 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 20 20 2f 2a  >pSchema);..  /*
1569d 20 50 75 74 20 61 20 63 6f 70 79 20 6f 66 20 74   Put a copy of t
1569e 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 20  he Table struct 
1569f 69 6e 20 50 61 72 73 65 2e 70 4e 65 77 54 61 62  in Parse.pNewTab
156a0 6c 65 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20  le for the.  ** 
156a1 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e  sqlite3AddColumn
156a2 28 29 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20  () function and 
156a3 66 72 69 65 6e 64 73 20 74 6f 20 6d 6f 64 69 66  friends to modif
156a4 79 2e 0a 20 20 2a 2f 0a 20 20 70 4e 65 77 20 3d  y..  */.  pNew =
156a5 20 28 54 61 62 6c 65 20 2a 29 73 71 6c 69 74 65   (Table *)sqlite
156a6 4d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 54 61  Malloc(sizeof(Ta
156a7 62 6c 65 29 29 3b 0a 20 20 69 66 28 20 21 70 4e  ble));.  if( !pN
156a8 65 77 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 62  ew ) goto exit_b
156a9 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b  egin_add_column;
156aa 0a 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  .  pParse->pNewT
156ab 61 62 6c 65 20 3d 20 70 4e 65 77 3b 0a 20 20 70  able = pNew;.  p
156ac 4e 65 77 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20  New->nRef = 1;. 
156ad 20 70 4e 65 77 2d 3e 6e 43 6f 6c 20 3d 20 70 54   pNew->nCol = pT
156ae 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 61 73 73 65  ab->nCol;.  asse
156af 72 74 28 20 70 4e 65 77 2d 3e 6e 43 6f 6c 3e 30  rt( pNew->nCol>0
156b0 20 29 3b 0a 20 20 6e 41 6c 6c 6f 63 20 3d 20 28   );.  nAlloc = (
156b1 28 28 70 4e 65 77 2d 3e 6e 43 6f 6c 2d 31 29 2f  ((pNew->nCol-1)/
156b2 38 29 2a 38 29 2b 38 3b 0a 20 20 61 73 73 65 72  8)*8)+8;.  asser
156b3 74 28 20 6e 41 6c 6c 6f 63 3e 3d 70 4e 65 77 2d  t( nAlloc>=pNew-
156b4 3e 6e 43 6f 6c 20 26 26 20 6e 41 6c 6c 6f 63 25  >nCol && nAlloc%
156b5 38 3d 3d 30 20 26 26 20 6e 41 6c 6c 6f 63 2d 70  8==0 && nAlloc-p
156b6 4e 65 77 2d 3e 6e 43 6f 6c 3c 38 20 29 3b 0a 20  New->nCol<8 );. 
156b7 20 70 4e 65 77 2d 3e 61 43 6f 6c 20 3d 20 28 43   pNew->aCol = (C
156b8 6f 6c 75 6d 6e 20 2a 29 73 71 6c 69 74 65 4d 61  olumn *)sqliteMa
156b9 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 43 6f 6c 75  lloc(sizeof(Colu
156ba 6d 6e 29 2a 6e 41 6c 6c 6f 63 29 3b 0a 20 20 70  mn)*nAlloc);.  p
156bb 4e 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c  New->zName = sql
156bc 69 74 65 53 74 72 44 75 70 28 70 54 61 62 2d 3e  iteStrDup(pTab->
156bd 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 21 70  zName);.  if( !p
156be 4e 65 77 2d 3e 61 43 6f 6c 20 7c 7c 20 21 70 4e  New->aCol || !pN
156bf 65 77 2d 3e 7a 4e 61 6d 65 20 29 7b 0a 20 20 20  ew->zName ){.   
156c0 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e   goto exit_begin
156c1 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 7d  _add_column;.  }
156c2 0a 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2d 3e  .  memcpy(pNew->
156c3 61 43 6f 6c 2c 20 70 54 61 62 2d 3e 61 43 6f 6c  aCol, pTab->aCol
156c4 2c 20 73 69 7a 65 6f 66 28 43 6f 6c 75 6d 6e 29  , sizeof(Column)
156c5 2a 70 4e 65 77 2d 3e 6e 43 6f 6c 29 3b 0a 20 20  *pNew->nCol);.  
156c6 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4e 65 77 2d  for(i=0; i<pNew-
156c7 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nCol; i++){.   
156c8 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20   Column *pCol = 
156c9 26 70 4e 65 77 2d 3e 61 43 6f 6c 5b 69 5d 3b 0a  &pNew->aCol[i];.
156ca 20 20 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 20      pCol->zName 
156cb 3d 20 73 71 6c 69 74 65 53 74 72 44 75 70 28 70  = sqliteStrDup(p
156cc 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  Col->zName);.   
156cd 20 70 43 6f 6c 2d 3e 7a 43 6f 6c 6c 20 3d 20 30   pCol->zColl = 0
156ce 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 54 79 70  ;.    pCol->zTyp
156cf 65 20 3d 20 30 3b 0a 20 20 20 20 70 43 6f 6c 2d  e = 0;.    pCol-
156d0 3e 70 44 66 6c 74 20 3d 20 30 3b 0a 20 20 7d 0a  >pDflt = 0;.  }.
156d1 20 20 70 4e 65 77 2d 3e 70 53 63 68 65 6d 61 20    pNew->pSchema 
156d2 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44  = pParse->db->aD
156d3 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b 0a  b[iDb].pSchema;.
156d4 20 20 70 4e 65 77 2d 3e 61 64 64 43 6f 6c 4f 66    pNew->addColOf
156d5 66 73 65 74 20 3d 20 70 54 61 62 2d 3e 61 64 64  fset = pTab->add
156d6 43 6f 6c 4f 66 66 73 65 74 3b 0a 20 20 70 4e 65  ColOffset;.  pNe
156d7 77 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 0a 20 20  w->nRef = 1;..  
156d8 2f 2a 20 42 65 67 69 6e 20 61 20 74 72 61 6e 73  /* Begin a trans
156d9 61 63 74 69 6f 6e 20 61 6e 64 20 69 6e 63 72 65  action and incre
156da 6d 65 6e 74 20 74 68 65 20 73 63 68 65 6d 61 20  ment the schema 
156db 63 6f 6f 6b 69 65 2e 20 20 2a 2f 0a 20 20 73 71  cookie.  */.  sq
156dc 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f  lite3BeginWriteO
156dd 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c  peration(pParse,
156de 20 30 2c 20 69 44 62 29 3b 0a 20 20 76 20 3d 20   0, iDb);.  v = 
156df 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
156e0 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 21 76  Parse);.  if( !v
156e1 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67   ) goto exit_beg
156e2 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20  in_add_column;. 
156e3 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f   sqlite3ChangeCo
156e4 6f 6b 69 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  okie(pParse->db,
156e5 20 76 2c 20 69 44 62 29 3b 0a 0a 65 78 69 74 5f   v, iDb);..exit_
156e6 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e  begin_add_column
156e7 3a 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69  :.  sqlite3SrcLi
156e8 73 74 44 65 6c 65 74 65 28 70 53 72 63 29 3b 0a  stDelete(pSrc);.
156e9 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 23 65 6e 64    return;.}.#end
156ea 69 66 20 20 2f 2a 20 53 51 4c 49 54 45 5f 41 4c  if  /* SQLITE_AL
156eb 54 45 52 5f 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a  TER_TABLE */../*
156ec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
156ed 64 20 6f 66 20 61 6c 74 65 72 2e 63 20 2a 2a 2a  d of alter.c ***
156ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
156ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
156f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
156f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
156f2 67 69 6e 20 66 69 6c 65 20 61 6e 61 6c 79 7a 65  gin file analyze
156f3 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
156f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
156f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
156f6 0a 2a 2a 20 32 30 30 35 20 4a 75 6c 79 20 38 0a  .** 2005 July 8.
156f7 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
156f8 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
156f9 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
156fa 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
156fb 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
156fc 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
156fd 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
156fe 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
156ff 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
15700 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
15701 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
15702 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
15703 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
15704 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
15705 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
15706 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
15707 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
15708 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
15709 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1570a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1570b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1570c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
1570d 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
1570e 61 69 6e 73 20 63 6f 64 65 20 61 73 73 6f 63 69  ains code associ
1570f 61 74 65 64 20 77 69 74 68 20 74 68 65 20 41 4e  ated with the AN
15710 41 4c 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a  ALYZE command..*
15711 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 61  *.** @(#) $Id: a
15712 6e 61 6c 79 7a 65 2e 63 2c 76 20 31 2e 31 39 20  nalyze.c,v 1.19 
15713 32 30 30 37 2f 30 36 2f 32 30 20 31 33 3a 33 37  2007/06/20 13:37
15714 3a 33 31 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :31 drh Exp $.*/
15715 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
15716 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 0a 0a 2f 2a  OMIT_ANALYZE../*
15717 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
15718 20 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20   generates code 
15719 74 68 61 74 20 6f 70 65 6e 73 20 74 68 65 20 73  that opens the s
1571a 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c  qlite_stat1 tabl
1571b 65 20 6f 6e 20 63 75 72 73 6f 72 0a 2a 2a 20 69  e on cursor.** i
1571c 53 74 61 74 43 75 72 2e 0a 2a 2a 0a 2a 2a 20 49  StatCur..**.** I
1571d 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61  f the sqlite_sta
1571e 74 31 20 74 61 62 6c 65 73 20 64 6f 65 73 20 6e  t1 tables does n
1571f 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20 65 78  ot previously ex
15720 69 73 74 2c 20 69 74 20 69 73 20 63 72 65 61 74  ist, it is creat
15721 65 64 2e 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65  ed..** If it doe
15722 73 20 70 72 65 76 69 6f 75 73 6c 79 20 65 78 69  s previously exi
15723 73 74 2c 20 61 6c 6c 20 65 6e 74 69 72 65 73 20  st, all entires 
15724 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
15725 74 61 62 6c 65 20 7a 57 68 65 72 65 0a 2a 2a 20  table zWhere.** 
15726 61 72 65 20 72 65 6d 6f 76 65 64 2e 20 20 49 66  are removed.  If
15727 20 7a 57 68 65 72 65 3d 3d 30 20 74 68 65 6e 20   zWhere==0 then 
15728 61 6c 6c 20 65 6e 74 72 69 65 73 20 61 72 65 20  all entries are 
15729 72 65 6d 6f 76 65 64 2e 0a 2a 2f 0a 73 74 61 74  removed..*/.stat
1572a 69 63 20 76 6f 69 64 20 6f 70 65 6e 53 74 61 74  ic void openStat
1572b 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a  Table(.  Parse *
1572c 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
1572d 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
1572e 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62  ext */.  int iDb
1572f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
15730 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
15731 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69 6e 67 20   we are looking 
15732 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74 61  in */.  int iSta
15733 74 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 20  tCur,           
15734 2f 2a 20 4f 70 65 6e 20 74 68 65 20 73 71 6c 69  /* Open the sqli
15735 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 6f  te_stat1 table o
15736 6e 20 74 68 69 73 20 63 75 72 73 6f 72 20 2a 2f  n this cursor */
15737 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
15738 57 68 65 72 65 20 20 20 20 20 20 2f 2a 20 44 65  Where      /* De
15739 6c 65 74 65 20 65 6e 74 72 69 65 73 20 61 73 73  lete entries ass
1573a 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69  ociated with thi
1573b 73 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20  s table */.){.  
1573c 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
1573d 61 72 73 65 2d 3e 64 62 3b 0a 20 20 44 62 20 2a  arse->db;.  Db *
1573e 70 44 62 3b 0a 20 20 69 6e 74 20 69 52 6f 6f 74  pDb;.  int iRoot
1573f 50 61 67 65 3b 0a 20 20 54 61 62 6c 65 20 2a 70  Page;.  Table *p
15740 53 74 61 74 3b 0a 20 20 56 64 62 65 20 2a 76 20  Stat;.  Vdbe *v 
15741 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
15742 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 69 66 28  (pParse);..  if(
15743 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a   v==0 ) return;.
15744 20 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62    pDb = &db->aDb
15745 5b 69 44 62 5d 3b 0a 20 20 69 66 28 20 28 70 53  [iDb];.  if( (pS
15746 74 61 74 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  tat = sqlite3Fin
15747 64 54 61 62 6c 65 28 64 62 2c 20 22 73 71 6c 69  dTable(db, "sqli
15748 74 65 5f 73 74 61 74 31 22 2c 20 70 44 62 2d 3e  te_stat1", pDb->
15749 7a 4e 61 6d 65 29 29 3d 3d 30 20 29 7b 0a 20 20  zName))==0 ){.  
1574a 20 20 2f 2a 20 54 68 65 20 73 71 6c 69 74 65 5f    /* The sqlite_
1574b 73 74 61 74 31 20 74 61 62 6c 65 73 20 64 6f 65  stat1 tables doe
1574c 73 20 6e 6f 74 20 65 78 69 73 74 2e 20 20 43 72  s not exist.  Cr
1574d 65 61 74 65 20 69 74 2e 20 20 0a 20 20 20 20 2a  eate it.  .    *
1574e 2a 20 4e 6f 74 65 20 74 68 61 74 20 61 20 73 69  * Note that a si
1574f 64 65 2d 65 66 66 65 63 74 20 6f 66 20 74 68 65  de-effect of the
15750 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74   CREATE TABLE st
15751 61 74 65 6d 65 6e 74 20 69 73 20 74 6f 20 6c 65  atement is to le
15752 61 76 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 72  ave.    ** the r
15753 6f 6f 74 70 61 67 65 20 6f 66 20 74 68 65 20 6e  ootpage of the n
15754 65 77 20 74 61 62 6c 65 20 6f 6e 20 74 68 65 20  ew table on the 
15755 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b  top of the stack
15756 2e 20 20 54 68 69 73 20 69 73 0a 20 20 20 20 2a  .  This is.    *
15757 2a 20 69 6d 70 6f 72 74 61 6e 74 20 62 65 63 61  * important beca
15758 75 73 65 20 74 68 65 20 4f 70 65 6e 57 72 69 74  use the OpenWrit
15759 65 20 6f 70 63 6f 64 65 20 62 65 6c 6f 77 20 77  e opcode below w
1575a 69 6c 6c 20 62 65 20 6e 65 65 64 69 6e 67 20 69  ill be needing i
1575b 74 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65  t. */.    sqlite
1575c 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61  3NestedParse(pPa
1575d 72 73 65 2c 0a 20 20 20 20 20 20 22 43 52 45 41  rse,.      "CREA
1575e 54 45 20 54 41 42 4c 45 20 25 51 2e 73 71 6c 69  TE TABLE %Q.sqli
1575f 74 65 5f 73 74 61 74 31 28 74 62 6c 2c 69 64 78  te_stat1(tbl,idx
15760 2c 73 74 61 74 29 22 2c 0a 20 20 20 20 20 20 70  ,stat)",.      p
15761 44 62 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 29 3b  Db->zName.    );
15762 0a 20 20 20 20 69 52 6f 6f 74 50 61 67 65 20 3d  .    iRootPage =
15763 20 30 3b 20 20 2f 2a 20 43 61 75 73 65 20 72 6f   0;  /* Cause ro
15764 6f 74 70 61 67 65 20 74 6f 20 62 65 20 74 61 6b  otpage to be tak
15765 65 6e 20 66 72 6f 6d 20 74 6f 70 20 6f 66 20 73  en from top of s
15766 74 61 63 6b 20 2a 2f 0a 20 20 7d 65 6c 73 65 20  tack */.  }else 
15767 69 66 28 20 7a 57 68 65 72 65 20 29 7b 0a 20 20  if( zWhere ){.  
15768 20 20 2f 2a 20 54 68 65 20 73 71 6c 69 74 65 5f    /* The sqlite_
15769 73 74 61 74 31 20 74 61 62 6c 65 20 65 78 69 73  stat1 table exis
1576a 74 73 2e 20 20 44 65 6c 65 74 65 20 61 6c 6c 20  ts.  Delete all 
1576b 65 6e 74 72 69 65 73 20 61 73 73 6f 63 69 61 74  entries associat
1576c 65 64 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 74  ed with.    ** t
1576d 68 65 20 74 61 62 6c 65 20 7a 57 68 65 72 65 2e  he table zWhere.
1576e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 4e   */.    sqlite3N
1576f 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73  estedParse(pPars
15770 65 2c 0a 20 20 20 20 20 20 20 22 44 45 4c 45 54  e,.       "DELET
15771 45 20 46 52 4f 4d 20 25 51 2e 73 71 6c 69 74 65  E FROM %Q.sqlite
15772 5f 73 74 61 74 31 20 57 48 45 52 45 20 74 62 6c  _stat1 WHERE tbl
15773 3d 25 51 22 2c 0a 20 20 20 20 20 20 20 70 44 62  =%Q",.       pDb
15774 2d 3e 7a 4e 61 6d 65 2c 20 7a 57 68 65 72 65 0a  ->zName, zWhere.
15775 20 20 20 20 29 3b 0a 20 20 20 20 69 52 6f 6f 74      );.    iRoot
15776 50 61 67 65 20 3d 20 70 53 74 61 74 2d 3e 74 6e  Page = pStat->tn
15777 75 6d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  um;.  }else{.   
15778 20 2f 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 73   /* The sqlite_s
15779 74 61 74 31 20 74 61 62 6c 65 20 61 6c 72 65 61  tat1 table alrea
1577a 64 79 20 65 78 69 73 74 73 2e 20 20 44 65 6c 65  dy exists.  Dele
1577b 74 65 20 61 6c 6c 20 72 6f 77 73 2e 20 2a 2f 0a  te all rows. */.
1577c 20 20 20 20 69 52 6f 6f 74 50 61 67 65 20 3d 20      iRootPage = 
1577d 70 53 74 61 74 2d 3e 74 6e 75 6d 3b 0a 20 20 20  pStat->tnum;.   
1577e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1577f 70 28 76 2c 20 4f 50 5f 43 6c 65 61 72 2c 20 70  p(v, OP_Clear, p
15780 53 74 61 74 2d 3e 74 6e 75 6d 2c 20 69 44 62 29  Stat->tnum, iDb)
15781 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e  ;.  }..  /* Open
15782 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
15783 31 20 74 61 62 6c 65 20 66 6f 72 20 77 72 69 74  1 table for writ
15784 69 6e 67 2e 20 55 6e 6c 65 73 73 20 69 74 20 77  ing. Unless it w
15785 61 73 20 63 72 65 61 74 65 64 0a 20 20 2a 2a 20  as created.  ** 
15786 62 79 20 74 68 69 73 20 76 64 62 65 20 70 72 6f  by this vdbe pro
15787 67 72 61 6d 2c 20 6c 6f 63 6b 20 69 74 20 66 6f  gram, lock it fo
15788 72 20 77 72 69 74 69 6e 67 20 61 74 20 74 68 65  r writing at the
15789 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6c 65   shared-cache le
1578a 76 65 6c 2e 20 0a 20 20 2a 2a 20 49 66 20 74 68  vel. .  ** If th
1578b 69 73 20 76 64 62 65 20 64 69 64 20 63 72 65 61  is vdbe did crea
1578c 74 65 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74  te the sqlite_st
1578d 61 74 31 20 74 61 62 6c 65 2c 20 74 68 65 6e 20  at1 table, then 
1578e 69 74 20 6d 75 73 74 20 68 61 76 65 20 0a 20 20  it must have .  
1578f 2a 2a 20 61 6c 72 65 61 64 79 20 6f 62 74 61 69  ** already obtai
15790 6e 65 64 20 61 20 73 63 68 65 6d 61 2d 6c 6f 63  ned a schema-loc
15791 6b 2c 20 6d 61 6b 69 6e 67 20 74 68 65 20 77 72  k, making the wr
15792 69 74 65 2d 6c 6f 63 6b 20 72 65 64 75 6e 64 61  ite-lock redunda
15793 6e 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69  nt..  */.  if( i
15794 52 6f 6f 74 50 61 67 65 3e 30 20 29 7b 0a 20 20  RootPage>0 ){.  
15795 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f    sqlite3TableLo
15796 63 6b 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20  ck(pParse, iDb, 
15797 69 52 6f 6f 74 50 61 67 65 2c 20 31 2c 20 22 73  iRootPage, 1, "s
15798 71 6c 69 74 65 5f 73 74 61 74 31 22 29 3b 0a 20  qlite_stat1");. 
15799 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65   }.  sqlite3Vdbe
1579a 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 74 65  AddOp(v, OP_Inte
1579b 67 65 72 2c 20 69 44 62 2c 20 30 29 3b 0a 20 20  ger, iDb, 0);.  
1579c 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1579d 28 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65  (v, OP_OpenWrite
1579e 2c 20 69 53 74 61 74 43 75 72 2c 20 69 52 6f 6f  , iStatCur, iRoo
1579f 74 50 61 67 65 29 3b 0a 20 20 73 71 6c 69 74 65  tPage);.  sqlite
157a0 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
157a1 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20  _SetNumColumns, 
157a2 69 53 74 61 74 43 75 72 2c 20 33 29 3b 0a 7d 0a  iStatCur, 3);.}.
157a3 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
157a4 63 6f 64 65 20 74 6f 20 64 6f 20 61 6e 20 61 6e  code to do an an
157a5 61 6c 79 73 69 73 20 6f 66 20 61 6c 6c 20 69 6e  alysis of all in
157a6 64 69 63 65 73 20 61 73 73 6f 63 69 61 74 65 64  dices associated
157a7 20 77 69 74 68 0a 2a 2a 20 61 20 73 69 6e 67 6c   with.** a singl
157a8 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74  e table..*/.stat
157a9 69 63 20 76 6f 69 64 20 61 6e 61 6c 79 7a 65 4f  ic void analyzeO
157aa 6e 65 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65  neTable(.  Parse
157ab 20 2a 70 50 61 72 73 65 2c 20 20 20 2f 2a 20 50   *pParse,   /* P
157ac 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
157ad 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20  .  Table *pTab, 
157ae 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 77 68 6f      /* Table who
157af 73 65 20 69 6e 64 69 63 65 73 20 61 72 65 20 74  se indices are t
157b0 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f  o be analyzed */
157b1 0a 20 20 69 6e 74 20 69 53 74 61 74 43 75 72 2c  .  int iStatCur,
157b2 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 74 68      /* Cursor th
157b3 61 74 20 77 72 69 74 65 73 20 74 6f 20 74 68 65  at writes to the
157b4 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61   sqlite_stat1 ta
157b5 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 4d 65  ble */.  int iMe
157b6 6d 20 20 20 20 20 20 20 20 20 2f 2a 20 41 76 61  m         /* Ava
157b7 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 79 20 6c 6f  ilable memory lo
157b8 63 61 74 69 6f 6e 73 20 62 65 67 69 6e 20 68 65  cations begin he
157b9 72 65 20 2a 2f 0a 29 7b 0a 20 20 49 6e 64 65 78  re */.){.  Index
157ba 20 2a 70 49 64 78 3b 20 20 20 20 20 2f 2a 20 41   *pIdx;     /* A
157bb 6e 20 69 6e 64 65 78 20 74 6f 20 62 65 69 6e 67  n index to being
157bc 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 69   analyzed */.  i
157bd 6e 74 20 69 49 64 78 43 75 72 3b 20 20 20 20 20  nt iIdxCur;     
157be 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72  /* Cursor number
157bf 20 66 6f 72 20 69 6e 64 65 78 20 62 65 69 6e 67   for index being
157c0 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 69   analyzed */.  i
157c1 6e 74 20 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20  nt nCol;        
157c2 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  /* Number of col
157c3 75 6d 6e 73 20 69 6e 20 74 68 65 20 69 6e 64 65  umns in the inde
157c4 78 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20  x */.  Vdbe *v; 
157c5 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76          /* The v
157c6 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 62  irtual machine b
157c7 65 69 6e 67 20 62 75 69 6c 74 20 75 70 20 2a 2f  eing built up */
157c8 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
157c9 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
157ca 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 70  ter */.  int top
157cb 4f 66 4c 6f 6f 70 3b 20 20 20 2f 2a 20 54 68 65  OfLoop;   /* The
157cc 20 74 6f 70 20 6f 66 20 74 68 65 20 6c 6f 6f 70   top of the loop
157cd 20 2a 2f 0a 20 20 69 6e 74 20 65 6e 64 4f 66 4c   */.  int endOfL
157ce 6f 6f 70 3b 20 20 20 2f 2a 20 54 68 65 20 65 6e  oop;   /* The en
157cf 64 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f  d of the loop */
157d0 0a 20 20 69 6e 74 20 61 64 64 72 3b 20 20 20 20  .  int addr;    
157d1 20 20 20 20 2f 2a 20 54 68 65 20 61 64 64 72 65      /* The addre
157d2 73 73 20 6f 66 20 61 6e 20 69 6e 73 74 72 75 63  ss of an instruc
157d3 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44  tion */.  int iD
157d4 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  b;         /* In
157d5 64 65 78 20 6f 66 20 64 61 74 61 62 61 73 65 20  dex of database 
157d6 63 6f 6e 74 61 69 6e 69 6e 67 20 70 54 61 62 20  containing pTab 
157d7 2a 2f 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65  */..  v = sqlite
157d8 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
157d9 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 7c 7c 20  ;.  if( v==0 || 
157da 70 54 61 62 3d 3d 30 20 7c 7c 20 70 54 61 62 2d  pTab==0 || pTab-
157db 3e 70 49 6e 64 65 78 3d 3d 30 20 29 7b 0a 20 20  >pIndex==0 ){.  
157dc 20 20 2f 2a 20 44 6f 20 6e 6f 20 61 6e 61 6c 79    /* Do no analy
157dd 73 69 73 20 66 6f 72 20 74 61 62 6c 65 73 20 74  sis for tables t
157de 68 61 74 20 68 61 76 65 20 6e 6f 20 69 6e 64 69  hat have no indi
157df 63 65 73 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  ces */.    retur
157e0 6e 3b 0a 20 20 7d 0a 0a 20 20 69 44 62 20 3d 20  n;.  }..  iDb = 
157e1 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
157e2 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c  ndex(pParse->db,
157e3 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
157e4 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d  .  assert( iDb>=
157e5 30 20 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  0 );.#ifndef SQL
157e6 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49  ITE_OMIT_AUTHORI
157e7 5a 41 54 49 4f 4e 0a 20 20 69 66 28 20 73 71 6c  ZATION.  if( sql
157e8 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
157e9 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 41 4e 41  arse, SQLITE_ANA
157ea 4c 59 5a 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  LYZE, pTab->zNam
157eb 65 2c 20 30 2c 0a 20 20 20 20 20 20 70 50 61 72  e, 0,.      pPar
157ec 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 62 5d  se->db->aDb[iDb]
157ed 2e 7a 4e 61 6d 65 20 29 20 29 7b 0a 20 20 20 20  .zName ) ){.    
157ee 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64  return;.  }.#end
157ef 69 66 0a 0a 20 20 2f 2a 20 45 73 74 61 62 6c 69  if..  /* Establi
157f0 73 68 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f  sh a read-lock o
157f1 6e 20 74 68 65 20 74 61 62 6c 65 20 61 74 20 74  n the table at t
157f2 68 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 20  he shared-cache 
157f3 6c 65 76 65 6c 2e 20 2a 2f 0a 20 20 73 71 6c 69  level. */.  sqli
157f4 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61  te3TableLock(pPa
157f5 72 73 65 2c 20 69 44 62 2c 20 70 54 61 62 2d 3e  rse, iDb, pTab->
157f6 74 6e 75 6d 2c 20 30 2c 20 70 54 61 62 2d 3e 7a  tnum, 0, pTab->z
157f7 4e 61 6d 65 29 3b 0a 0a 20 20 69 49 64 78 43 75  Name);..  iIdxCu
157f8 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  r = pParse->nTab
157f9 3b 0a 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61  ;.  for(pIdx=pTa
157fa 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b  b->pIndex; pIdx;
157fb 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
157fc 74 29 7b 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20  t){.    KeyInfo 
157fd 2a 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 49  *pKey = sqlite3I
157fe 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72  ndexKeyinfo(pPar
157ff 73 65 2c 20 70 49 64 78 29 3b 0a 0a 20 20 20 20  se, pIdx);..    
15800 2f 2a 20 4f 70 65 6e 20 61 20 63 75 72 73 6f 72  /* Open a cursor
15801 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 74 6f   to the index to
15802 20 62 65 20 61 6e 61 6c 79 7a 65 64 0a 20 20 20   be analyzed.   
15803 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
15804 69 44 62 3d 3d 73 71 6c 69 74 65 33 53 63 68 65  iDb==sqlite3Sche
15805 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65  maToIndex(pParse
15806 2d 3e 64 62 2c 20 70 49 64 78 2d 3e 70 53 63 68  ->db, pIdx->pSch
15807 65 6d 61 29 20 29 3b 0a 20 20 20 20 73 71 6c 69  ema) );.    sqli
15808 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
15809 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 44 62 2c  OP_Integer, iDb,
1580a 20 30 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d   0);.    VdbeCom
1580b 6d 65 6e 74 28 28 76 2c 20 22 23 20 25 73 22 2c  ment((v, "# %s",
1580c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a   pIdx->zName));.
1580d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4f      sqlite3VdbeO
1580e 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 61  p3(v, OP_OpenRea
1580f 64 2c 20 69 49 64 78 43 75 72 2c 20 70 49 64 78  d, iIdxCur, pIdx
15810 2d 3e 74 6e 75 6d 2c 0a 20 20 20 20 20 20 20 20  ->tnum,.        
15811 28 63 68 61 72 20 2a 29 70 4b 65 79 2c 20 50 33  (char *)pKey, P3
15812 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46  _KEYINFO_HANDOFF
15813 29 3b 0a 20 20 20 20 6e 43 6f 6c 20 3d 20 70 49  );.    nCol = pI
15814 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 20  dx->nColumn;.   
15815 20 69 66 28 20 69 4d 65 6d 2b 6e 43 6f 6c 2a 32   if( iMem+nCol*2
15816 3e 3d 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 29  >=pParse->nMem )
15817 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  {.      pParse->
15818 6e 4d 65 6d 20 3d 20 69 4d 65 6d 2b 6e 43 6f 6c  nMem = iMem+nCol
15819 2a 32 2b 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  *2+1;.    }.    
1581a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1581b 28 76 2c 20 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c  (v, OP_SetNumCol
1581c 75 6d 6e 73 2c 20 69 49 64 78 43 75 72 2c 20 6e  umns, iIdxCur, n
1581d 43 6f 6c 2b 31 29 3b 0a 0a 20 20 20 20 2f 2a 20  Col+1);..    /* 
1581e 4d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 61 72 65  Memory cells are
1581f 20 75 73 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73   used as follows
15820 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  :.    **.    ** 
15821 20 20 20 6d 65 6d 5b 69 4d 65 6d 5d 3a 20 20 20     mem[iMem]:   
15822 20 20 20 20 20 20 20 20 20 20 54 68 65 20 74 6f            The to
15823 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f  tal number of ro
15824 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e  ws in the table.
15825 0a 20 20 20 20 2a 2a 20 20 20 20 6d 65 6d 5b 69  .    **    mem[i
15826 4d 65 6d 2b 31 5d 3a 20 20 20 20 20 20 20 20 20  Mem+1]:         
15827 20 20 4e 75 6d 62 65 72 20 6f 66 20 64 69 73 74    Number of dist
15828 69 6e 63 74 20 76 61 6c 75 65 73 20 69 6e 20 63  inct values in c
15829 6f 6c 75 6d 6e 20 31 0a 20 20 20 20 2a 2a 20 20  olumn 1.    **  
1582a 20 20 2e 2e 2e 0a 20 20 20 20 2a 2a 20 20 20 20    ....    **    
1582b 6d 65 6d 5b 69 4d 65 6d 2b 6e 43 6f 6c 5d 3a 20  mem[iMem+nCol]: 
1582c 20 20 20 20 20 20 20 4e 75 6d 62 65 72 20 6f 66         Number of
1582d 20 64 69 73 74 69 6e 63 74 20 76 61 6c 75 65 73   distinct values
1582e 20 69 6e 20 63 6f 6c 75 6d 6e 20 4e 0a 20 20 20   in column N.   
1582f 20 2a 2a 20 20 20 20 6d 65 6d 5b 69 4d 65 6d 2b   **    mem[iMem+
15830 6e 43 6f 6c 2b 31 5d 20 20 20 20 20 20 20 4c 61  nCol+1]       La
15831 73 74 20 6f 62 73 65 72 76 65 64 20 76 61 6c 75  st observed valu
15832 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 31 0a 20 20  e of column 1.  
15833 20 20 2a 2a 20 20 20 20 2e 2e 2e 0a 20 20 20 20    **    ....    
15834 2a 2a 20 20 20 20 6d 65 6d 5b 69 4d 65 6d 2b 6e  **    mem[iMem+n
15835 43 6f 6c 2b 6e 43 6f 6c 5d 3a 20 20 20 4c 61 73  Col+nCol]:   Las
15836 74 20 6f 62 73 65 72 76 65 64 20 76 61 6c 75 65  t observed value
15837 20 6f 66 20 63 6f 6c 75 6d 6e 20 4e 0a 20 20 20   of column N.   
15838 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 65 6c 6c 73   **.    ** Cells
15839 20 69 4d 65 6d 20 74 68 72 6f 75 67 68 20 69 4d   iMem through iM
1583a 65 6d 2b 6e 43 6f 6c 20 61 72 65 20 69 6e 69 74  em+nCol are init
1583b 69 61 6c 69 7a 65 64 20 74 6f 20 30 2e 20 20 54  ialized to 0.  T
1583c 68 65 20 6f 74 68 65 72 73 0a 20 20 20 20 2a 2a  he others.    **
1583d 20 61 72 65 20 69 6e 69 74 69 61 6c 69 7a 65 64   are initialized
1583e 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f   to NULL..    */
1583f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
15840 3d 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  =nCol; i++){.   
15841 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
15842 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 49 6e 74  dOp(v, OP_MemInt
15843 2c 20 30 2c 20 69 4d 65 6d 2b 69 29 3b 0a 20 20  , 0, iMem+i);.  
15844 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b    }.    for(i=0;
15845 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20   i<nCol; i++){. 
15846 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
15847 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 4e  AddOp(v, OP_MemN
15848 75 6c 6c 2c 20 69 4d 65 6d 2b 6e 43 6f 6c 2b 69  ull, iMem+nCol+i
15849 2b 31 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 0a 20  +1, 0);.    }.. 
1584a 20 20 20 2f 2a 20 44 6f 20 74 68 65 20 61 6e 61     /* Do the ana
1584b 6c 79 73 69 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  lysis..    */.  
1584c 20 20 65 6e 64 4f 66 4c 6f 6f 70 20 3d 20 73 71    endOfLoop = sq
1584d 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
1584e 65 6c 28 76 29 3b 0a 20 20 20 20 73 71 6c 69 74  el(v);.    sqlit
1584f 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
15850 50 5f 52 65 77 69 6e 64 2c 20 69 49 64 78 43 75  P_Rewind, iIdxCu
15851 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20  r, endOfLoop);. 
15852 20 20 20 74 6f 70 4f 66 4c 6f 6f 70 20 3d 20 73     topOfLoop = s
15853 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
15854 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 73 71  tAddr(v);.    sq
15855 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
15856 2c 20 4f 50 5f 4d 65 6d 49 6e 63 72 2c 20 31 2c  , OP_MemIncr, 1,
15857 20 69 4d 65 6d 29 3b 0a 20 20 20 20 66 6f 72 28   iMem);.    for(
15858 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b  i=0; i<nCol; i++
15859 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1585a 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1585b 43 6f 6c 75 6d 6e 2c 20 69 49 64 78 43 75 72 2c  Column, iIdxCur,
1585c 20 69 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   i);.      sqlit
1585d 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1585e 50 5f 4d 65 6d 4c 6f 61 64 2c 20 69 4d 65 6d 2b  P_MemLoad, iMem+
1585f 6e 43 6f 6c 2b 69 2b 31 2c 20 30 29 3b 0a 20 20  nCol+i+1, 0);.  
15860 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
15861 64 64 4f 70 28 76 2c 20 4f 50 5f 4e 65 2c 20 30  ddOp(v, OP_Ne, 0
15862 78 31 30 30 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  x100, 0);.    }.
15863 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
15864 64 64 4f 70 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  ddOp(v, OP_Goto,
15865 20 30 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a   0, endOfLoop);.
15866 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
15867 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Col; i++){.     
15868 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
15869 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d  dbeAddOp(v, OP_M
1586a 65 6d 49 6e 63 72 2c 20 31 2c 20 69 4d 65 6d 2b  emIncr, 1, iMem+
1586b 69 2b 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  i+1);.      sqli
1586c 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28  te3VdbeChangeP2(
1586d 76 2c 20 74 6f 70 4f 66 4c 6f 6f 70 20 2b 20 33  v, topOfLoop + 3
1586e 2a 69 20 2b 20 33 2c 20 61 64 64 72 29 3b 0a 20  *i + 3, addr);. 
1586f 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
15870 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6f 6c 75  AddOp(v, OP_Colu
15871 6d 6e 2c 20 69 49 64 78 43 75 72 2c 20 69 29 3b  mn, iIdxCur, i);
15872 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
15873 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65  beAddOp(v, OP_Me
15874 6d 53 74 6f 72 65 2c 20 69 4d 65 6d 2b 6e 43 6f  mStore, iMem+nCo
15875 6c 2b 69 2b 31 2c 20 31 29 3b 0a 20 20 20 20 7d  l+i+1, 1);.    }
15876 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
15877 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
15878 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 20  endOfLoop);.    
15879 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1587a 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 69 49 64  (v, OP_Next, iId
1587b 78 43 75 72 2c 20 74 6f 70 4f 66 4c 6f 6f 70 29  xCur, topOfLoop)
1587c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
1587d 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f  eAddOp(v, OP_Clo
1587e 73 65 2c 20 69 49 64 78 43 75 72 2c 20 30 29 3b  se, iIdxCur, 0);
1587f 0a 0a 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74  ..    /* Store t
15880 68 65 20 72 65 73 75 6c 74 73 2e 20 20 0a 20 20  he results.  .  
15881 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20    **.    ** The 
15882 72 65 73 75 6c 74 20 69 73 20 61 20 73 69 6e 67  result is a sing
15883 6c 65 20 72 6f 77 20 6f 66 20 74 68 65 20 73 71  le row of the sq
15884 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65  lite_stat1 table
15885 2e 20 20 54 68 65 20 66 69 72 73 74 0a 20 20 20  .  The first.   
15886 20 2a 2a 20 74 77 6f 20 63 6f 6c 75 6d 6e 73 20   ** two columns 
15887 61 72 65 20 74 68 65 20 6e 61 6d 65 73 20 6f 66  are the names of
15888 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 69   the table and i
15889 6e 64 65 78 2e 20 20 54 68 65 20 74 68 69 72 64  ndex.  The third
1588a 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2a 20 69   column.    ** i
1588b 73 20 61 20 73 74 72 69 6e 67 20 63 6f 6d 70 6f  s a string compo
1588c 73 65 64 20 6f 66 20 61 20 6c 69 73 74 20 6f 66  sed of a list of
1588d 20 69 6e 74 65 67 65 72 20 73 74 61 74 69 73 74   integer statist
1588e 69 63 73 20 61 62 6f 75 74 20 74 68 65 0a 20 20  ics about the.  
1588f 20 20 2a 2a 20 69 6e 64 65 78 2e 20 20 54 68 65    ** index.  The
15890 20 66 69 72 73 74 20 69 6e 74 65 67 65 72 20 69   first integer i
15891 6e 20 74 68 65 20 6c 69 73 74 20 69 73 20 74 68  n the list is th
15892 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  e total number o
15893 66 20 65 6e 74 69 72 65 73 0a 20 20 20 20 2a 2a  f entires.    **
15894 20 69 6e 20 74 68 65 20 69 6e 64 65 78 2e 20 20   in the index.  
15895 54 68 65 72 65 20 69 73 20 6f 6e 65 20 61 64 64  There is one add
15896 69 74 69 6f 6e 61 6c 20 69 6e 74 65 67 65 72 20  itional integer 
15897 69 6e 20 74 68 65 20 6c 69 73 74 20 66 6f 72 20  in the list for 
15898 65 61 63 68 0a 20 20 20 20 2a 2a 20 63 6f 6c 75  each.    ** colu
15899 6d 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e  mn of the table.
1589a 20 20 54 68 69 73 20 61 64 64 69 74 69 6f 6e 61    This additiona
1589b 6c 20 69 6e 74 65 67 65 72 20 69 73 20 61 20 67  l integer is a g
1589c 75 65 73 73 20 6f 66 20 68 6f 77 20 6d 61 6e 79  uess of how many
1589d 0a 20 20 20 20 2a 2a 20 72 6f 77 73 20 6f 66 20  .    ** rows of 
1589e 74 68 65 20 74 61 62 6c 65 20 74 68 65 20 69 6e  the table the in
1589f 64 65 78 20 77 69 6c 6c 20 73 65 6c 65 63 74 2e  dex will select.
158a0 20 20 49 66 20 44 20 69 73 20 74 68 65 20 63 6f    If D is the co
158a1 75 6e 74 20 6f 66 20 64 69 73 74 69 6e 63 74 0a  unt of distinct.
158a2 20 20 20 20 2a 2a 20 76 61 6c 75 65 73 20 61 6e      ** values an
158a3 64 20 4b 20 69 73 20 74 68 65 20 74 6f 74 61 6c  d K is the total
158a4 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 2c   number of rows,
158a5 20 74 68 65 6e 20 74 68 65 20 69 6e 74 65 67 65   then the intege
158a6 72 20 69 73 20 63 6f 6d 70 75 74 65 64 0a 20 20  r is computed.  
158a7 20 20 2a 2a 20 61 73 3a 0a 20 20 20 20 2a 2a 0a    ** as:.    **.
158a8 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 49 20      **        I 
158a9 3d 20 28 4b 2b 44 2d 31 29 2f 44 0a 20 20 20 20  = (K+D-1)/D.    
158aa 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 4b 3d 3d  **.    ** If K==
158ab 30 20 74 68 65 6e 20 6e 6f 20 65 6e 74 72 79 20  0 then no entry 
158ac 69 73 20 6d 61 64 65 20 69 6e 74 6f 20 74 68 65  is made into the
158ad 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61   sqlite_stat1 ta
158ae 62 6c 65 2e 20 20 0a 20 20 20 20 2a 2a 20 49 66  ble.  .    ** If
158af 20 4b 3e 30 20 74 68 65 6e 20 69 74 20 69 73 20   K>0 then it is 
158b0 61 6c 77 61 79 73 20 74 68 65 20 63 61 73 65 20  always the case 
158b1 74 68 65 20 44 3e 30 20 73 6f 20 64 69 76 69 73  the D>0 so divis
158b2 69 6f 6e 20 62 79 20 7a 65 72 6f 0a 20 20 20 20  ion by zero.    
158b3 2a 2a 20 69 73 20 6e 65 76 65 72 20 70 6f 73 73  ** is never poss
158b4 69 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ible..    */.   
158b5 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
158b6 70 28 76 2c 20 4f 50 5f 4d 65 6d 4c 6f 61 64 2c  p(v, OP_MemLoad,
158b7 20 69 4d 65 6d 2c 20 30 29 3b 0a 20 20 20 20 61   iMem, 0);.    a
158b8 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
158b9 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 66 4e  eAddOp(v, OP_IfN
158ba 6f 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  ot, 0, 0);.    s
158bb 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
158bc 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20  v, OP_NewRowid, 
158bd 69 53 74 61 74 43 75 72 2c 20 30 29 3b 0a 20 20  iStatCur, 0);.  
158be 20 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33    sqlite3VdbeOp3
158bf 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20  (v, OP_String8, 
158c0 30 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  0, 0, pTab->zNam
158c1 65 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  e, 0);.    sqlit
158c2 65 33 56 64 62 65 4f 70 33 28 76 2c 20 4f 50 5f  e3VdbeOp3(v, OP_
158c3 53 74 72 69 6e 67 38 2c 20 30 2c 20 30 2c 20 70  String8, 0, 0, p
158c4 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a  Idx->zName, 0);.
158c5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
158c6 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 4c 6f  ddOp(v, OP_MemLo
158c7 61 64 2c 20 69 4d 65 6d 2c 20 30 29 3b 0a 20 20  ad, iMem, 0);.  
158c8 20 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33    sqlite3VdbeOp3
158c9 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20  (v, OP_String8, 
158ca 30 2c 20 30 2c 20 22 20 22 2c 20 30 29 3b 0a 20  0, 0, " ", 0);. 
158cb 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43     for(i=0; i<nC
158cc 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; i++){.      
158cd 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
158ce 28 76 2c 20 4f 50 5f 4d 65 6d 4c 6f 61 64 2c 20  (v, OP_MemLoad, 
158cf 69 4d 65 6d 2c 20 30 29 3b 0a 20 20 20 20 20 20  iMem, 0);.      
158d0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
158d1 28 76 2c 20 4f 50 5f 4d 65 6d 4c 6f 61 64 2c 20  (v, OP_MemLoad, 
158d2 69 4d 65 6d 2b 69 2b 31 2c 20 30 29 3b 0a 20 20  iMem+i+1, 0);.  
158d3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
158d4 64 64 4f 70 28 76 2c 20 4f 50 5f 41 64 64 2c 20  ddOp(v, OP_Add, 
158d5 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c  0, 0);.      sql
158d6 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
158d7 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 2d 31 2c 20   OP_AddImm, -1, 
158d8 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  0);.      sqlite
158d9 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
158da 5f 4d 65 6d 4c 6f 61 64 2c 20 69 4d 65 6d 2b 69  _MemLoad, iMem+i
158db 2b 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71  +1, 0);.      sq
158dc 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
158dd 2c 20 4f 50 5f 44 69 76 69 64 65 2c 20 30 2c 20  , OP_Divide, 0, 
158de 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  0);.      sqlite
158df 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
158e0 5f 54 6f 49 6e 74 2c 20 30 2c 20 30 29 3b 0a 20  _ToInt, 0, 0);. 
158e1 20 20 20 20 20 69 66 28 20 69 3d 3d 6e 43 6f 6c       if( i==nCol
158e2 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  -1 ){.        sq
158e3 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
158e4 2c 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 6e 43 6f  , OP_Concat, nCo
158e5 6c 2a 32 2d 31 2c 20 30 29 3b 0a 20 20 20 20 20  l*2-1, 0);.     
158e6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
158e7 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
158e8 28 76 2c 20 4f 50 5f 44 75 70 2c 20 31 2c 20 30  (v, OP_Dup, 1, 0
158e9 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
158ea 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
158eb 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65  Op3(v, OP_MakeRe
158ec 63 6f 72 64 2c 20 33 2c 20 30 2c 20 22 61 61 61  cord, 3, 0, "aaa
158ed 22 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  ", 0);.    sqlit
158ee 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
158ef 50 5f 49 6e 73 65 72 74 2c 20 69 53 74 61 74 43  P_Insert, iStatC
158f0 75 72 2c 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e  ur, OPFLAG_APPEN
158f1 44 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  D);.    sqlite3V
158f2 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
158f3 64 64 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ddr);.  }.}../*.
158f4 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
158f5 20 74 68 61 74 20 77 69 6c 6c 20 63 61 75 73 65   that will cause
158f6 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
158f7 20 69 6e 64 65 78 20 61 6e 61 6c 79 73 69 73 20   index analysis 
158f8 74 6f 0a 2a 2a 20 62 65 20 6c 61 6f 64 65 64 20  to.** be laoded 
158f9 69 6e 74 6f 20 69 6e 74 65 72 6e 61 6c 20 68 61  into internal ha
158fa 73 68 20 74 61 62 6c 65 73 20 77 68 65 72 65 20  sh tables where 
158fb 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 2e 0a  is can be used..
158fc 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c  */.static void l
158fd 6f 61 64 41 6e 61 6c 79 73 69 73 28 50 61 72 73  oadAnalysis(Pars
158fe 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69  e *pParse, int i
158ff 44 62 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d  Db){.  Vdbe *v =
15900 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
15901 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76  pParse);.  if( v
15902 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
15903 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4c  dbeAddOp(v, OP_L
15904 6f 61 64 41 6e 61 6c 79 73 69 73 2c 20 69 44 62  oadAnalysis, iDb
15905 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  , 0);.  }.}../*.
15906 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
15907 20 74 68 61 74 20 77 69 6c 6c 20 64 6f 20 61 6e   that will do an
15908 20 61 6e 61 6c 79 73 69 73 20 6f 66 20 61 6e 20   analysis of an 
15909 65 6e 74 69 72 65 20 64 61 74 61 62 61 73 65 0a  entire database.
1590a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61  */.static void a
1590b 6e 61 6c 79 7a 65 44 61 74 61 62 61 73 65 28 50  nalyzeDatabase(P
1590c 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e  arse *pParse, in
1590d 74 20 69 44 62 29 7b 0a 20 20 73 71 6c 69 74 65  t iDb){.  sqlite
1590e 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
1590f 64 62 3b 0a 20 20 53 63 68 65 6d 61 20 2a 70 53  db;.  Schema *pS
15910 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b  chema = db->aDb[
15911 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b 20 20 20  iDb].pSchema;   
15912 20 2f 2a 20 53 63 68 65 6d 61 20 6f 66 20 64 61   /* Schema of da
15913 74 61 62 61 73 65 20 69 44 62 20 2a 2f 0a 20 20  tabase iDb */.  
15914 48 61 73 68 45 6c 65 6d 20 2a 6b 3b 0a 20 20 69  HashElem *k;.  i
15915 6e 74 20 69 53 74 61 74 43 75 72 3b 0a 20 20 69  nt iStatCur;.  i
15916 6e 74 20 69 4d 65 6d 3b 0a 0a 20 20 73 71 6c 69  nt iMem;..  sqli
15917 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65  te3BeginWriteOpe
15918 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30  ration(pParse, 0
15919 2c 20 69 44 62 29 3b 0a 20 20 69 53 74 61 74 43  , iDb);.  iStatC
1591a 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  ur = pParse->nTa
1591b 62 2b 2b 3b 0a 20 20 6f 70 65 6e 53 74 61 74 54  b++;.  openStatT
1591c 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44 62  able(pParse, iDb
1591d 2c 20 69 53 74 61 74 43 75 72 2c 20 30 29 3b 0a  , iStatCur, 0);.
1591e 20 20 69 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d    iMem = pParse-
1591f 3e 6e 4d 65 6d 3b 0a 20 20 66 6f 72 28 6b 3d 73  >nMem;.  for(k=s
15920 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 26  qliteHashFirst(&
15921 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68  pSchema->tblHash
15922 29 3b 20 6b 3b 20 6b 3d 73 71 6c 69 74 65 48 61  ); k; k=sqliteHa
15923 73 68 4e 65 78 74 28 6b 29 29 7b 0a 20 20 20 20  shNext(k)){.    
15924 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 28 54  Table *pTab = (T
15925 61 62 6c 65 2a 29 73 71 6c 69 74 65 48 61 73 68  able*)sqliteHash
15926 44 61 74 61 28 6b 29 3b 0a 20 20 20 20 61 6e 61  Data(k);.    ana
15927 6c 79 7a 65 4f 6e 65 54 61 62 6c 65 28 70 50 61  lyzeOneTable(pPa
15928 72 73 65 2c 20 70 54 61 62 2c 20 69 53 74 61 74  rse, pTab, iStat
15929 43 75 72 2c 20 69 4d 65 6d 29 3b 0a 20 20 7d 0a  Cur, iMem);.  }.
1592a 20 20 6c 6f 61 64 41 6e 61 6c 79 73 69 73 28 70    loadAnalysis(p
1592b 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 7d 0a 0a  Parse, iDb);.}..
1592c 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
1592d 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 64 6f  ode that will do
1592e 20 61 6e 20 61 6e 61 6c 79 73 69 73 20 6f 66 20   an analysis of 
1592f 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 69  a single table i
15930 6e 0a 2a 2a 20 61 20 64 61 74 61 62 61 73 65 2e  n.** a database.
15931 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
15932 61 6e 61 6c 79 7a 65 54 61 62 6c 65 28 50 61 72  analyzeTable(Par
15933 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c  se *pParse, Tabl
15934 65 20 2a 70 54 61 62 29 7b 0a 20 20 69 6e 74 20  e *pTab){.  int 
15935 69 44 62 3b 0a 20 20 69 6e 74 20 69 53 74 61 74  iDb;.  int iStat
15936 43 75 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  Cur;..  assert( 
15937 70 54 61 62 21 3d 30 20 29 3b 0a 20 20 69 44 62  pTab!=0 );.  iDb
15938 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
15939 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e  ToIndex(pParse->
1593a 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
1593b 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 65 67  a);.  sqlite3Beg
1593c 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e  inWriteOperation
1593d 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29  (pParse, 0, iDb)
1593e 3b 0a 20 20 69 53 74 61 74 43 75 72 20 3d 20 70  ;.  iStatCur = p
1593f 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20  Parse->nTab++;. 
15940 20 6f 70 65 6e 53 74 61 74 54 61 62 6c 65 28 70   openStatTable(p
15941 50 61 72 73 65 2c 20 69 44 62 2c 20 69 53 74 61  Parse, iDb, iSta
15942 74 43 75 72 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  tCur, pTab->zNam
15943 65 29 3b 0a 20 20 61 6e 61 6c 79 7a 65 4f 6e 65  e);.  analyzeOne
15944 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54  Table(pParse, pT
15945 61 62 2c 20 69 53 74 61 74 43 75 72 2c 20 70 50  ab, iStatCur, pP
15946 61 72 73 65 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 6c  arse->nMem);.  l
15947 6f 61 64 41 6e 61 6c 79 73 69 73 28 70 50 61 72  oadAnalysis(pPar
15948 73 65 2c 20 69 44 62 29 3b 0a 7d 0a 0a 2f 2a 0a  se, iDb);.}../*.
15949 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
1594a 20 66 6f 72 20 74 68 65 20 41 4e 41 4c 59 5a 45   for the ANALYZE
1594b 20 63 6f 6d 6d 61 6e 64 2e 20 20 54 68 65 20 70   command.  The p
1594c 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73  arser calls this
1594d 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 77 68 65 6e   routine.** when
1594e 20 69 74 20 72 65 63 6f 67 6e 69 7a 65 73 20 61   it recognizes a
1594f 6e 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61 6e  n ANALYZE comman
15950 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  d..**.**        
15951 41 4e 41 4c 59 5a 45 20 20 20 20 20 20 20 20 20  ANALYZE         
15952 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15953 20 20 20 2d 2d 20 31 0a 2a 2a 20 20 20 20 20 20     -- 1.**      
15954 20 20 41 4e 41 4c 59 5a 45 20 20 3c 64 61 74 61    ANALYZE  <data
15955 62 61 73 65 3e 20 20 20 20 20 20 20 20 20 20 20  base>           
15956 20 20 20 20 20 2d 2d 20 32 0a 2a 2a 20 20 20 20       -- 2.**    
15957 20 20 20 20 41 4e 41 4c 59 5a 45 20 20 3f 3c 64      ANALYZE  ?<d
15958 61 74 61 62 61 73 65 3e 2e 3f 3c 74 61 62 6c 65  atabase>.?<table
15959 6e 61 6d 65 3e 20 20 2d 2d 20 33 0a 2a 2a 0a 2a  name>  -- 3.**.*
1595a 2a 20 46 6f 72 6d 20 31 20 63 61 75 73 65 73 20  * Form 1 causes 
1595b 61 6c 6c 20 69 6e 64 69 63 65 73 20 69 6e 20 61  all indices in a
1595c 6c 6c 20 61 74 74 61 63 68 65 64 20 64 61 74 61  ll attached data
1595d 62 61 73 65 73 20 74 6f 20 62 65 20 61 6e 61 6c  bases to be anal
1595e 79 7a 65 64 2e 0a 2a 2a 20 46 6f 72 6d 20 32 20  yzed..** Form 2 
1595f 61 6e 61 6c 79 7a 65 73 20 61 6c 6c 20 69 6e 64  analyzes all ind
15960 69 63 65 73 20 74 68 65 20 73 69 6e 67 6c 65 20  ices the single 
15961 64 61 74 61 62 61 73 65 20 6e 61 6d 65 64 2e 0a  database named..
15962 2a 2a 20 46 6f 72 6d 20 33 20 61 6e 61 6c 79 7a  ** Form 3 analyz
15963 65 73 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 61  es all indices a
15964 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
15965 68 65 20 6e 61 6d 65 64 20 74 61 62 6c 65 2e 0a  he named table..
15966 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
15967 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6e  E void sqlite3An
15968 61 6c 79 7a 65 28 50 61 72 73 65 20 2a 70 50 61  alyze(Parse *pPa
15969 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d  rse, Token *pNam
1596a 65 31 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  e1, Token *pName
1596b 32 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  2){.  sqlite3 *d
1596c 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
1596d 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 69 6e 74    int iDb;.  int
1596e 20 69 3b 0a 20 20 63 68 61 72 20 2a 7a 2c 20 2a   i;.  char *z, *
1596f 7a 44 62 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54  zDb;.  Table *pT
15970 61 62 3b 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61  ab;.  Token *pTa
15971 62 6c 65 4e 61 6d 65 3b 0a 0a 20 20 2f 2a 20 52  bleName;..  /* R
15972 65 61 64 20 74 68 65 20 64 61 74 61 62 61 73 65  ead the database
15973 20 73 63 68 65 6d 61 2e 20 49 66 20 61 6e 20 65   schema. If an e
15974 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 6c 65 61  rror occurs, lea
15975 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  ve an error mess
15976 61 67 65 0a 20 20 2a 2a 20 61 6e 64 20 63 6f 64  age.  ** and cod
15977 65 20 69 6e 20 70 50 61 72 73 65 20 61 6e 64 20  e in pParse and 
15978 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a  return NULL. */.
15979 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21    if( SQLITE_OK!
1597a 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65  =sqlite3ReadSche
1597b 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20  ma(pParse) ){.  
1597c 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20    return;.  }.. 
1597d 20 69 66 28 20 70 4e 61 6d 65 31 3d 3d 30 20 29   if( pName1==0 )
1597e 7b 0a 20 20 20 20 2f 2a 20 46 6f 72 6d 20 31 3a  {.    /* Form 1:
1597f 20 20 41 6e 61 6c 79 7a 65 20 65 76 65 72 79 74    Analyze everyt
15980 68 69 6e 67 20 2a 2f 0a 20 20 20 20 66 6f 72 28  hing */.    for(
15981 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  i=0; i<db->nDb; 
15982 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  i++){.      if( 
15983 69 3d 3d 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b  i==1 ) continue;
15984 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 61 6e 61 6c    /* Do not anal
15985 79 7a 65 20 74 68 65 20 54 45 4d 50 20 64 61 74  yze the TEMP dat
15986 61 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 61  abase */.      a
15987 6e 61 6c 79 7a 65 44 61 74 61 62 61 73 65 28 70  nalyzeDatabase(p
15988 50 61 72 73 65 2c 20 69 29 3b 0a 20 20 20 20 7d  Parse, i);.    }
15989 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4e 61  .  }else if( pNa
1598a 6d 65 32 3d 3d 30 20 7c 7c 20 70 4e 61 6d 65 32  me2==0 || pName2
1598b 2d 3e 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a  ->n==0 ){.    /*
1598c 20 46 6f 72 6d 20 32 3a 20 20 41 6e 61 6c 79 7a   Form 2:  Analyz
1598d 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6f  e the database o
1598e 72 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 2a 2f  r table named */
1598f 0a 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74  .    iDb = sqlit
15990 65 33 46 69 6e 64 44 62 28 64 62 2c 20 70 4e 61  e3FindDb(db, pNa
15991 6d 65 31 29 3b 0a 20 20 20 20 69 66 28 20 69 44  me1);.    if( iD
15992 62 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 6e  b>=0 ){.      an
15993 61 6c 79 7a 65 44 61 74 61 62 61 73 65 28 70 50  alyzeDatabase(pP
15994 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20  arse, iDb);.    
15995 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 20 3d  }else{.      z =
15996 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d   sqlite3NameFrom
15997 54 6f 6b 65 6e 28 70 4e 61 6d 65 31 29 3b 0a 20  Token(pName1);. 
15998 20 20 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69       pTab = sqli
15999 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70  te3LocateTable(p
1599a 50 61 72 73 65 2c 20 7a 2c 20 30 29 3b 0a 20 20  Parse, z, 0);.  
1599b 20 20 20 20 73 71 6c 69 74 65 46 72 65 65 28 7a      sqliteFree(z
1599c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 61  );.      if( pTa
1599d 62 20 29 7b 0a 20 20 20 20 20 20 20 20 61 6e 61  b ){.        ana
1599e 6c 79 7a 65 54 61 62 6c 65 28 70 50 61 72 73 65  lyzeTable(pParse
1599f 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 20 20 7d  , pTab);.      }
159a0 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
159a1 20 20 20 20 2f 2a 20 46 6f 72 6d 20 33 3a 20 41      /* Form 3: A
159a2 6e 61 6c 79 7a 65 20 74 68 65 20 66 75 6c 6c 79  nalyze the fully
159a3 20 71 75 61 6c 69 66 69 65 64 20 74 61 62 6c 65   qualified table
159a4 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 69 44 62   name */.    iDb
159a5 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72   = sqlite3TwoPar
159a6 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e  tName(pParse, pN
159a7 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70  ame1, pName2, &p
159a8 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 20 20  TableName);.    
159a9 69 66 28 20 69 44 62 3e 3d 30 20 29 7b 0a 20 20  if( iDb>=0 ){.  
159aa 20 20 20 20 7a 44 62 20 3d 20 64 62 2d 3e 61 44      zDb = db->aD
159ab 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  b[iDb].zName;.  
159ac 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e      z = sqlite3N
159ad 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 70 54 61  ameFromToken(pTa
159ae 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  bleName);.      
159af 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 20 20 20  if( z ){.       
159b0 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c   pTab = sqlite3L
159b1 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73  ocateTable(pPars
159b2 65 2c 20 7a 2c 20 7a 44 62 29 3b 0a 20 20 20 20  e, z, zDb);.    
159b3 20 20 20 20 73 71 6c 69 74 65 46 72 65 65 28 7a      sqliteFree(z
159b4 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
159b5 54 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Tab ){.         
159b6 20 61 6e 61 6c 79 7a 65 54 61 62 6c 65 28 70 50   analyzeTable(pP
159b7 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20  arse, pTab);.   
159b8 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
159b9 20 20 20 7d 20 20 20 0a 20 20 7d 0a 7d 0a 0a 2f     }   .  }.}../
159ba 2a 0a 2a 2a 20 55 73 65 64 20 74 6f 20 70 61 73  *.** Used to pas
159bb 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72  s information fr
159bc 6f 6d 20 74 68 65 20 61 6e 61 6c 79 7a 65 72 20  om the analyzer 
159bd 72 65 61 64 65 72 20 74 68 72 6f 75 67 68 20 74  reader through t
159be 6f 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 62 61 63  o the.** callbac
159bf 6b 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 74 79  k routine..*/.ty
159c0 70 65 64 65 66 20 73 74 72 75 63 74 20 61 6e 61  pedef struct ana
159c1 6c 79 73 69 73 49 6e 66 6f 20 61 6e 61 6c 79 73  lysisInfo analys
159c2 69 73 49 6e 66 6f 3b 0a 73 74 72 75 63 74 20 61  isInfo;.struct a
159c3 6e 61 6c 79 73 69 73 49 6e 66 6f 20 7b 0a 20 20  nalysisInfo {.  
159c4 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 63  sqlite3 *db;.  c
159c5 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74 61  onst char *zData
159c6 62 61 73 65 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  base;.};../*.** 
159c7 54 68 69 73 20 63 61 6c 6c 62 61 63 6b 20 69 73  This callback is
159c8 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66 6f   invoked once fo
159c9 72 20 65 61 63 68 20 69 6e 64 65 78 20 77 68 65  r each index whe
159ca 6e 20 72 65 61 64 69 6e 67 20 74 68 65 0a 2a 2a  n reading the.**
159cb 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61   sqlite_stat1 ta
159cc 62 6c 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 20 20 20  ble.  .**.**    
159cd 20 61 72 67 76 5b 30 5d 20 3d 20 6e 61 6d 65 20   argv[0] = name 
159ce 6f 66 20 74 68 65 20 69 6e 64 65 78 0a 2a 2a 20  of the index.** 
159cf 20 20 20 20 61 72 67 76 5b 31 5d 20 3d 20 72 65      argv[1] = re
159d0 73 75 6c 74 73 20 6f 66 20 61 6e 61 6c 79 73 69  sults of analysi
159d1 73 20 2d 20 6f 6e 20 69 6e 74 65 67 65 72 20 66  s - on integer f
159d2 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 0a 2a  or each column.*
159d3 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6e 61  /.static int ana
159d4 6c 79 73 69 73 4c 6f 61 64 65 72 28 76 6f 69 64  lysisLoader(void
159d5 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 61 72 67   *pData, int arg
159d6 63 2c 20 63 68 61 72 20 2a 2a 61 72 67 76 2c 20  c, char **argv, 
159d7 63 68 61 72 20 2a 2a 61 7a 4e 6f 74 55 73 65 64  char **azNotUsed
159d8 29 7b 0a 20 20 61 6e 61 6c 79 73 69 73 49 6e 66  ){.  analysisInf
159d9 6f 20 2a 70 49 6e 66 6f 20 3d 20 28 61 6e 61 6c  o *pInfo = (anal
159da 79 73 69 73 49 6e 66 6f 2a 29 70 44 61 74 61 3b  ysisInfo*)pData;
159db 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78  .  Index *pIndex
159dc 3b 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20  ;.  int i, c;.  
159dd 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 76 3b 0a  unsigned int v;.
159de 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b    const char *z;
159df 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63  ..  assert( argc
159e0 3d 3d 32 20 29 3b 0a 20 20 69 66 28 20 61 72 67  ==2 );.  if( arg
159e1 76 3d 3d 30 20 7c 7c 20 61 72 67 76 5b 30 5d 3d  v==0 || argv[0]=
159e2 3d 30 20 7c 7c 20 61 72 67 76 5b 31 5d 3d 3d 30  =0 || argv[1]==0
159e3 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
159e4 3b 0a 20 20 7d 0a 20 20 70 49 6e 64 65 78 20 3d  ;.  }.  pIndex =
159e5 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65   sqlite3FindInde
159e6 78 28 70 49 6e 66 6f 2d 3e 64 62 2c 20 61 72 67  x(pInfo->db, arg
159e7 76 5b 30 5d 2c 20 70 49 6e 66 6f 2d 3e 7a 44 61  v[0], pInfo->zDa
159e8 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20 70  tabase);.  if( p
159e9 49 6e 64 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20  Index==0 ){.    
159ea 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
159eb 7a 20 3d 20 61 72 67 76 5b 31 5d 3b 0a 20 20 66  z = argv[1];.  f
159ec 6f 72 28 69 3d 30 3b 20 2a 7a 20 26 26 20 69 3c  or(i=0; *z && i<
159ed 3d 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e  =pIndex->nColumn
159ee 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 76 20 3d 20  ; i++){.    v = 
159ef 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 63  0;.    while( (c
159f0 3d 7a 5b 30 5d 29 3e 3d 27 30 27 20 26 26 20 63  =z[0])>='0' && c
159f1 3c 3d 27 39 27 20 29 7b 0a 20 20 20 20 20 20 76  <='9' ){.      v
159f2 20 3d 20 76 2a 31 30 20 2b 20 63 20 2d 20 27 30   = v*10 + c - '0
159f3 27 3b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20  ';.      z++;.  
159f4 20 20 7d 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e    }.    pIndex->
159f5 61 69 52 6f 77 45 73 74 5b 69 5d 20 3d 20 76 3b  aiRowEst[i] = v;
159f6 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d 27 20 27  .    if( *z==' '
159f7 20 29 20 7a 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65   ) z++;.  }.  re
159f8 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
159f9 20 4c 6f 61 64 20 74 68 65 20 63 6f 6e 74 65 6e   Load the conten
159fa 74 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f  t of the sqlite_
159fb 73 74 61 74 31 20 74 61 62 6c 65 20 69 6e 74 6f  stat1 table into
159fc 20 74 68 65 20 69 6e 64 65 78 20 68 61 73 68 20   the index hash 
159fd 74 61 62 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54  tables..*/.SQLIT
159fe 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
159ff 6c 69 74 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61  lite3AnalysisLoa
15a00 64 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69  d(sqlite3 *db, i
15a01 6e 74 20 69 44 62 29 7b 0a 20 20 61 6e 61 6c 79  nt iDb){.  analy
15a02 73 69 73 49 6e 66 6f 20 73 49 6e 66 6f 3b 0a 20  sisInfo sInfo;. 
15a03 20 48 61 73 68 45 6c 65 6d 20 2a 69 3b 0a 20 20   HashElem *i;.  
15a04 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 69 6e  char *zSql;.  in
15a05 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 43 6c 65 61  t rc;..  /* Clea
15a06 72 20 61 6e 79 20 70 72 69 6f 72 20 73 74 61 74  r any prior stat
15a07 69 73 74 69 63 73 20 2a 2f 0a 20 20 66 6f 72 28  istics */.  for(
15a08 69 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73  i=sqliteHashFirs
15a09 74 28 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  t(&db->aDb[iDb].
15a0a 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68  pSchema->idxHash
15a0b 29 3b 69 3b 69 3d 73 71 6c 69 74 65 48 61 73 68  );i;i=sqliteHash
15a0c 4e 65 78 74 28 69 29 29 7b 0a 20 20 20 20 49 6e  Next(i)){.    In
15a0d 64 65 78 20 2a 70 49 64 78 20 3d 20 73 71 6c 69  dex *pIdx = sqli
15a0e 74 65 48 61 73 68 44 61 74 61 28 69 29 3b 0a 20  teHashData(i);. 
15a0f 20 20 20 73 71 6c 69 74 65 33 44 65 66 61 75 6c     sqlite3Defaul
15a10 74 52 6f 77 45 73 74 28 70 49 64 78 29 3b 0a 20  tRowEst(pIdx);. 
15a11 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74   }..  /* Check t
15a12 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20  o make sure the 
15a13 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62  sqlite_stat1 tab
15a14 6c 65 20 65 78 69 73 74 73 73 20 2a 2f 0a 20 20  le existss */.  
15a15 73 49 6e 66 6f 2e 64 62 20 3d 20 64 62 3b 0a 20  sInfo.db = db;. 
15a16 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61 73 65   sInfo.zDatabase
15a17 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e   = db->aDb[iDb].
15a18 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20 73 71 6c  zName;.  if( sql
15a19 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62  ite3FindTable(db
15a1a 2c 20 22 73 71 6c 69 74 65 5f 73 74 61 74 31 22  , "sqlite_stat1"
15a1b 2c 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61 73  , sInfo.zDatabas
15a1c 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 72 65  e)==0 ){.     re
15a1d 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
15a1e 52 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 4c 6f  R;.  }...  /* Lo
15a1f 61 64 20 6e 65 77 20 73 74 61 74 69 73 74 69 63  ad new statistic
15a20 73 20 6f 75 74 20 6f 66 20 74 68 65 20 73 71 6c  s out of the sql
15a21 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20  ite_stat1 table 
15a22 2a 2f 0a 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69  */.  zSql = sqli
15a23 74 65 33 4d 50 72 69 6e 74 66 28 22 53 45 4c 45  te3MPrintf("SELE
15a24 43 54 20 69 64 78 2c 20 73 74 61 74 20 46 52 4f  CT idx, stat FRO
15a25 4d 20 25 51 2e 73 71 6c 69 74 65 5f 73 74 61 74  M %Q.sqlite_stat
15a26 31 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  1",.            
15a27 20 20 20 20 20 20 20 20 20 20 20 20 73 49 6e 66              sInf
15a28 6f 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20  o.zDatabase);.  
15a29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
15a2a 28 64 62 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  (db);.  rc = sql
15a2b 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20 7a 53  ite3_exec(db, zS
15a2c 71 6c 2c 20 61 6e 61 6c 79 73 69 73 4c 6f 61 64  ql, analysisLoad
15a2d 65 72 2c 20 26 73 49 6e 66 6f 2c 20 30 29 3b 0a  er, &sInfo, 0);.
15a2e 20 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f    sqlite3SafetyO
15a2f 6e 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65 46  n(db);.  sqliteF
15a30 72 65 65 28 7a 53 71 6c 29 3b 0a 20 20 72 65 74  ree(zSql);.  ret
15a31 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 23 65 6e 64  urn rc;.}...#end
15a32 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
15a33 54 5f 41 4e 41 4c 59 5a 45 20 2a 2f 0a 0a 2f 2a  T_ANALYZE */../*
15a34 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
15a35 64 20 6f 66 20 61 6e 61 6c 79 7a 65 2e 63 20 2a  d of analyze.c *
15a36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15a37 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15a38 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
15a39 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
15a3a 67 69 6e 20 66 69 6c 65 20 61 74 74 61 63 68 2e  gin file attach.
15a3b 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
15a3c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15a3d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
15a3e 0a 2a 2a 20 32 30 30 33 20 41 70 72 69 6c 20 36  .** 2003 April 6
15a3f 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
15a40 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
15a41 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
15a42 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
15a43 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
15a44 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
15a45 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
15a46 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
15a47 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
15a48 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
15a49 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
15a4a 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
15a4b 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
15a4c 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
15a4d 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
15a4e 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
15a4f 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
15a50 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
15a51 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15a52 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15a53 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15a54 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
15a55 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
15a56 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20  tains code used 
15a57 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
15a58 20 41 54 54 41 43 48 20 61 6e 64 20 44 45 54 41   ATTACH and DETA
15a59 43 48 20 63 6f 6d 6d 61 6e 64 73 2e 0a 2a 2a 0a  CH commands..**.
15a5a 2a 2a 20 24 49 64 3a 20 61 74 74 61 63 68 2e 63  ** $Id: attach.c
15a5b 2c 76 20 31 2e 36 30 20 32 30 30 37 2f 30 35 2f  ,v 1.60 2007/05/
15a5c 30 39 20 32 30 3a 33 31 3a 33 30 20 64 72 68 20  09 20:31:30 drh 
15a5d 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65  Exp $.*/..#ifnde
15a5e 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54  f SQLITE_OMIT_AT
15a5f 54 41 43 48 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c  TACH./*.** Resol
15a60 76 65 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  ve an expression
15a61 20 74 68 61 74 20 77 61 73 20 70 61 72 74 20 6f   that was part o
15a62 66 20 61 6e 20 41 54 54 41 43 48 20 6f 72 20 44  f an ATTACH or D
15a63 45 54 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e  ETACH statement.
15a64 20 54 68 69 73 0a 2a 2a 20 69 73 20 73 6c 69 67   This.** is slig
15a65 68 74 6c 79 20 64 69 66 66 65 72 65 6e 74 20 66  htly different f
15a66 72 6f 6d 20 72 65 73 6f 6c 76 69 6e 67 20 61 20  rom resolving a 
15a67 6e 6f 72 6d 61 6c 20 53 51 4c 20 65 78 70 72 65  normal SQL expre
15a68 73 73 69 6f 6e 2c 20 62 65 63 61 75 73 65 20 73  ssion, because s
15a69 69 6d 70 6c 65 0a 2a 2a 20 69 64 65 6e 74 69 66  imple.** identif
15a6a 69 65 72 73 20 61 72 65 20 74 72 65 61 74 65 64  iers are treated
15a6b 20 61 73 20 73 74 72 69 6e 67 73 2c 20 6e 6f 74   as strings, not
15a6c 20 70 6f 73 73 69 62 6c 65 20 63 6f 6c 75 6d 6e   possible column
15a6d 20 6e 61 6d 65 73 20 6f 72 20 61 6c 69 61 73 65   names or aliase
15a6e 73 2e 0a 2a 2a 0a 2a 2a 20 69 2e 65 2e 20 69 66  s..**.** i.e. if
15a6f 20 74 68 65 20 70 61 72 73 65 72 20 73 65 65 73   the parser sees
15a70 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 41 54 54 41  :.**.**     ATTA
15a71 43 48 20 44 41 54 41 42 41 53 45 20 61 62 63 20  CH DATABASE abc 
15a72 41 53 20 64 65 66 0a 2a 2a 0a 2a 2a 20 69 74 20  AS def.**.** it 
15a73 74 72 65 61 74 73 20 74 68 65 20 74 77 6f 20 65  treats the two e
15a74 78 70 72 65 73 73 69 6f 6e 73 20 61 73 20 6c 69  xpressions as li
15a75 74 65 72 61 6c 20 73 74 72 69 6e 67 73 20 27 61  teral strings 'a
15a76 62 63 27 20 61 6e 64 20 27 64 65 66 27 20 69 6e  bc' and 'def' in
15a77 73 74 65 61 64 20 6f 66 0a 2a 2a 20 6c 6f 6f 6b  stead of.** look
15a78 69 6e 67 20 66 6f 72 20 63 6f 6c 75 6d 6e 73 20  ing for columns 
15a79 6f 66 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65  of the same name
15a7a 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 6e 6c  ..**.** This onl
15a7b 79 20 61 70 70 6c 69 65 73 20 74 6f 20 74 68 65  y applies to the
15a7c 20 72 6f 6f 74 20 6e 6f 64 65 20 6f 66 20 70 45   root node of pE
15a7d 78 70 72 2c 20 73 6f 20 74 68 65 20 73 74 61 74  xpr, so the stat
15a7e 65 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ement:.**.**    
15a7f 20 41 54 54 41 43 48 20 44 41 54 41 42 41 53 45   ATTACH DATABASE
15a80 20 61 62 63 7c 7c 64 65 66 20 41 53 20 27 64 62   abc||def AS 'db
15a81 32 27 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c 20 66 61  2'.**.** will fa
15a82 69 6c 20 62 65 63 61 75 73 65 20 6e 65 69 74 68  il because neith
15a83 65 72 20 61 62 63 20 6f 72 20 64 65 66 20 63 61  er abc or def ca
15a84 6e 20 62 65 20 72 65 73 6f 6c 76 65 64 2e 0a 2a  n be resolved..*
15a85 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73  /.static int res
15a86 6f 6c 76 65 41 74 74 61 63 68 45 78 70 72 28 4e  olveAttachExpr(N
15a87 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 61 6d  ameContext *pNam
15a88 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 0a  e, Expr *pExpr).
15a89 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
15a8a 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 45  ITE_OK;.  if( pE
15a8b 78 70 72 20 29 7b 0a 20 20 20 20 69 66 28 20 70  xpr ){.    if( p
15a8c 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 49 44 20  Expr->op!=TK_ID 
15a8d 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ){.      rc = sq
15a8e 6c 69 74 65 33 45 78 70 72 52 65 73 6f 6c 76 65  lite3ExprResolve
15a8f 4e 61 6d 65 73 28 70 4e 61 6d 65 2c 20 70 45 78  Names(pName, pEx
15a90 70 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  pr);.      if( r
15a91 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
15a92 21 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f  !sqlite3ExprIsCo
15a93 6e 73 74 61 6e 74 28 70 45 78 70 72 29 20 29 7b  nstant(pExpr) ){
15a94 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
15a95 45 72 72 6f 72 4d 73 67 28 70 4e 61 6d 65 2d 3e  ErrorMsg(pName->
15a96 70 50 61 72 73 65 2c 20 22 69 6e 76 61 6c 69 64  pParse, "invalid
15a97 20 6e 61 6d 65 3a 20 5c 22 25 54 5c 22 22 2c 20   name: \"%T\"", 
15a98 26 70 45 78 70 72 2d 3e 73 70 61 6e 29 3b 0a 20  &pExpr->span);. 
15a99 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51         return SQ
15a9a 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
15a9b 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
15a9c 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d       pExpr->op =
15a9d 20 54 4b 5f 53 54 52 49 4e 47 3b 0a 20 20 20 20   TK_STRING;.    
15a9e 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
15a9f 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53  c;.}../*.** An S
15aa0 51 4c 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e  QL user-function
15aa1 20 72 65 67 69 73 74 65 72 65 64 20 74 6f 20 64   registered to d
15aa2 6f 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 61 6e  o the work of an
15aa3 20 41 54 54 41 43 48 20 73 74 61 74 65 6d 65 6e   ATTACH statemen
15aa4 74 2e 20 54 68 65 0a 2a 2a 20 74 68 72 65 65 20  t. The.** three 
15aa5 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65  arguments to the
15aa6 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d 65 20 64   function come d
15aa7 69 72 65 63 74 6c 79 20 66 72 6f 6d 20 61 6e 20  irectly from an 
15aa8 61 74 74 61 63 68 20 73 74 61 74 65 6d 65 6e 74  attach statement
15aa9 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 41 54 54 41  :.**.**     ATTA
15aaa 43 48 20 44 41 54 41 42 41 53 45 20 78 20 41 53  CH DATABASE x AS
15aab 20 79 20 4b 45 59 20 7a 0a 2a 2a 0a 2a 2a 20 20   y KEY z.**.**  
15aac 20 20 20 53 45 4c 45 43 54 20 73 71 6c 69 74 65     SELECT sqlite
15aad 5f 61 74 74 61 63 68 28 78 2c 20 79 2c 20 7a 29  _attach(x, y, z)
15aae 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70  .**.** If the op
15aaf 74 69 6f 6e 61 6c 20 22 4b 45 59 20 7a 22 20 73  tional "KEY z" s
15ab0 79 6e 74 61 78 20 69 73 20 6f 6d 69 74 74 65 64  yntax is omitted
15ab1 2c 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 69 73  , an SQL NULL is
15ab2 20 70 61 73 73 65 64 20 61 73 20 74 68 65 0a 2a   passed as the.*
15ab3 2a 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74  * third argument
15ab4 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
15ab5 20 61 74 74 61 63 68 46 75 6e 63 28 0a 20 20 73   attachFunc(.  s
15ab6 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
15ab7 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
15ab8 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
15ab9 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
15aba 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 72 63   int i;.  int rc
15abb 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 20   = 0;.  sqlite3 
15abc 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 75 73  *db = sqlite3_us
15abd 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29  er_data(context)
15abe 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
15abf 7a 4e 61 6d 65 3b 0a 20 20 63 6f 6e 73 74 20 63  zName;.  const c
15ac0 68 61 72 20 2a 7a 46 69 6c 65 3b 0a 20 20 44 62  har *zFile;.  Db
15ac1 20 2a 61 4e 65 77 3b 0a 20 20 63 68 61 72 20 7a   *aNew;.  char z
15ac2 45 72 72 5b 31 32 38 5d 3b 0a 20 20 63 68 61 72  Err[128];.  char
15ac3 20 2a 7a 45 72 72 44 79 6e 20 3d 20 30 3b 0a 0a   *zErrDyn = 0;..
15ac4 20 20 7a 46 69 6c 65 20 3d 20 28 63 6f 6e 73 74    zFile = (const
15ac5 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f   char *)sqlite3_
15ac6 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
15ac7 30 5d 29 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 28  0]);.  zName = (
15ac8 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c  const char *)sql
15ac9 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
15aca 61 72 67 76 5b 31 5d 29 3b 0a 20 20 69 66 28 20  argv[1]);.  if( 
15acb 7a 46 69 6c 65 3d 3d 30 20 29 20 7a 46 69 6c 65  zFile==0 ) zFile
15acc 20 3d 20 22 22 3b 0a 20 20 69 66 28 20 7a 4e 61   = "";.  if( zNa
15acd 6d 65 3d 3d 30 20 29 20 7a 4e 61 6d 65 20 3d 20  me==0 ) zName = 
15ace 22 22 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20  "";..  /* Check 
15acf 66 6f 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  for the followin
15ad0 67 20 65 72 72 6f 72 73 3a 0a 20 20 2a 2a 0a 20  g errors:.  **. 
15ad1 20 2a 2a 20 20 20 20 20 2a 20 54 6f 6f 20 6d 61   **     * Too ma
15ad2 6e 79 20 61 74 74 61 63 68 65 64 20 64 61 74 61  ny attached data
15ad3 62 61 73 65 73 2c 0a 20 20 2a 2a 20 20 20 20 20  bases,.  **     
15ad4 2a 20 54 72 61 6e 73 61 63 74 69 6f 6e 20 63 75  * Transaction cu
15ad5 72 72 65 6e 74 6c 79 20 6f 70 65 6e 0a 20 20 2a  rrently open.  *
15ad6 2a 20 20 20 20 20 2a 20 53 70 65 63 69 66 69 65  *     * Specifie
15ad7 64 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  d database name 
15ad8 61 6c 72 65 61 64 79 20 62 65 69 6e 67 20 75 73  already being us
15ad9 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64  ed..  */.  if( d
15ada 62 2d 3e 6e 44 62 3e 3d 53 51 4c 49 54 45 5f 4d  b->nDb>=SQLITE_M
15adb 41 58 5f 41 54 54 41 43 48 45 44 2b 32 20 29 7b  AX_ATTACHED+2 ){
15adc 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
15add 72 69 6e 74 66 28 0a 20 20 20 20 20 20 73 69 7a  rintf(.      siz
15ade 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c  eof(zErr), zErr,
15adf 20 22 74 6f 6f 20 6d 61 6e 79 20 61 74 74 61 63   "too many attac
15ae0 68 65 64 20 64 61 74 61 62 61 73 65 73 20 2d 20  hed databases - 
15ae1 6d 61 78 20 25 64 22 2c 20 0a 20 20 20 20 20 20  max %d", .      
15ae2 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43  SQLITE_MAX_ATTAC
15ae3 48 45 44 0a 20 20 20 20 29 3b 0a 20 20 20 20 67  HED.    );.    g
15ae4 6f 74 6f 20 61 74 74 61 63 68 5f 65 72 72 6f 72  oto attach_error
15ae5 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 64 62 2d  ;.  }.  if( !db-
15ae6 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20  >autoCommit ){. 
15ae7 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
15ae8 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29  ntf(sizeof(zErr)
15ae9 2c 20 7a 45 72 72 2c 0a 20 20 20 20 20 20 20 20  , zErr,.        
15aea 20 20 20 20 20 20 20 20 20 20 20 20 20 22 63 61               "ca
15aeb 6e 6e 6f 74 20 41 54 54 41 43 48 20 64 61 74 61  nnot ATTACH data
15aec 62 61 73 65 20 77 69 74 68 69 6e 20 74 72 61 6e  base within tran
15aed 73 61 63 74 69 6f 6e 22 29 3b 0a 20 20 20 20 67  saction");.    g
15aee 6f 74 6f 20 61 74 74 61 63 68 5f 65 72 72 6f 72  oto attach_error
15aef 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b  ;.  }.  for(i=0;
15af0 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29   i<db->nDb; i++)
15af1 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 20 3d 20  {.    char *z = 
15af2 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65  db->aDb[i].zName
15af3 3b 0a 20 20 20 20 69 66 28 20 7a 20 26 26 20 7a  ;.    if( z && z
15af4 4e 61 6d 65 20 26 26 20 73 71 6c 69 74 65 33 53  Name && sqlite3S
15af5 74 72 49 43 6d 70 28 7a 2c 20 7a 4e 61 6d 65 29  trICmp(z, zName)
15af6 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ==0 ){.      sql
15af7 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69  ite3_snprintf(si
15af8 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72  zeof(zErr), zErr
15af9 2c 20 22 64 61 74 61 62 61 73 65 20 25 73 20 69  , "database %s i
15afa 73 20 61 6c 72 65 61 64 79 20 69 6e 20 75 73 65  s already in use
15afb 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  ", zName);.     
15afc 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 72 72   goto attach_err
15afd 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  or;.    }.  }.. 
15afe 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68 65   /* Allocate the
15aff 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 20 74 68   new entry in th
15b00 65 20 64 62 2d 3e 61 44 62 5b 5d 20 61 72 72 61  e db->aDb[] arra
15b01 79 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 73 65  y and initialise
15b02 20 74 68 65 20 73 63 68 65 6d 61 0a 20 20 2a 2a   the schema.  **
15b03 20 68 61 73 68 20 74 61 62 6c 65 73 2e 0a 20 20   hash tables..  
15b04 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 61 44 62  */.  if( db->aDb
15b05 3d 3d 64 62 2d 3e 61 44 62 53 74 61 74 69 63 20  ==db->aDbStatic 
15b06 29 7b 0a 20 20 20 20 61 4e 65 77 20 3d 20 73 71  ){.    aNew = sq
15b07 6c 69 74 65 4d 61 6c 6c 6f 63 28 20 73 69 7a 65  liteMalloc( size
15b08 6f 66 28 64 62 2d 3e 61 44 62 5b 30 5d 29 2a 33  of(db->aDb[0])*3
15b09 20 29 3b 0a 20 20 20 20 69 66 28 20 61 4e 65 77   );.    if( aNew
15b0a 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ==0 ){.      ret
15b0b 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d  urn;.    }.    m
15b0c 65 6d 63 70 79 28 61 4e 65 77 2c 20 64 62 2d 3e  emcpy(aNew, db->
15b0d 61 44 62 2c 20 73 69 7a 65 6f 66 28 64 62 2d 3e  aDb, sizeof(db->
15b0e 61 44 62 5b 30 5d 29 2a 32 29 3b 0a 20 20 7d 65  aDb[0])*2);.  }e
15b0f 6c 73 65 7b 0a 20 20 20 20 61 4e 65 77 20 3d 20  lse{.    aNew = 
15b10 73 71 6c 69 74 65 52 65 61 6c 6c 6f 63 28 64 62  sqliteRealloc(db
15b11 2d 3e 61 44 62 2c 20 73 69 7a 65 6f 66 28 64 62  ->aDb, sizeof(db
15b12 2d 3e 61 44 62 5b 30 5d 29 2a 28 64 62 2d 3e 6e  ->aDb[0])*(db->n
15b13 44 62 2b 31 29 20 29 3b 0a 20 20 20 20 69 66 28  Db+1) );.    if(
15b14 20 61 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20   aNew==0 ){.    
15b15 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 20    return;.    } 
15b16 0a 20 20 7d 0a 20 20 64 62 2d 3e 61 44 62 20 3d  .  }.  db->aDb =
15b17 20 61 4e 65 77 3b 0a 20 20 61 4e 65 77 20 3d 20   aNew;.  aNew = 
15b18 26 64 62 2d 3e 61 44 62 5b 64 62 2d 3e 6e 44 62  &db->aDb[db->nDb
15b19 2b 2b 5d 3b 0a 20 20 6d 65 6d 73 65 74 28 61 4e  ++];.  memset(aN
15b1a 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 61  ew, 0, sizeof(*a
15b1b 4e 65 77 29 29 3b 0a 0a 20 20 2f 2a 20 4f 70 65  New));..  /* Ope
15b1c 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
15b1d 69 6c 65 2e 20 49 66 20 74 68 65 20 62 74 72 65  ile. If the btre
15b1e 65 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 6c  e is successfull
15b1f 79 20 6f 70 65 6e 65 64 2c 20 75 73 65 0a 20 20  y opened, use.  
15b20 2a 2a 20 69 74 20 74 6f 20 6f 62 74 61 69 6e 20  ** it to obtain 
15b21 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68  the database sch
15b22 65 6d 61 2e 20 41 74 20 74 68 69 73 20 70 6f 69  ema. At this poi
15b23 6e 74 20 74 68 65 20 73 63 68 65 6d 61 20 6d 61  nt the schema ma
15b24 79 0a 20 20 2a 2a 20 6f 72 20 6d 61 79 20 6e 6f  y.  ** or may no
15b25 74 20 62 65 20 69 6e 69 74 69 61 6c 69 73 65 64  t be initialised
15b26 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71  ..  */.  rc = sq
15b27 6c 69 74 65 33 42 74 72 65 65 46 61 63 74 6f 72  lite3BtreeFactor
15b28 79 28 64 62 2c 20 7a 46 69 6c 65 2c 20 30 2c 20  y(db, zFile, 0, 
15b29 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43  SQLITE_DEFAULT_C
15b2a 41 43 48 45 5f 53 49 5a 45 2c 20 26 61 4e 65 77  ACHE_SIZE, &aNew
15b2b 2d 3e 70 42 74 29 3b 0a 20 20 69 66 28 20 72 63  ->pBt);.  if( rc
15b2c 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
15b2d 20 20 20 61 4e 65 77 2d 3e 70 53 63 68 65 6d 61     aNew->pSchema
15b2e 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
15b2f 47 65 74 28 61 4e 65 77 2d 3e 70 42 74 29 3b 0a  Get(aNew->pBt);.
15b30 20 20 20 20 69 66 28 20 21 61 4e 65 77 2d 3e 70      if( !aNew->p
15b31 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 20 20  Schema ){.      
15b32 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
15b33 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  M;.    }else if(
15b34 20 61 4e 65 77 2d 3e 70 53 63 68 65 6d 61 2d 3e   aNew->pSchema->
15b35 66 69 6c 65 5f 66 6f 72 6d 61 74 20 26 26 20 61  file_format && a
15b36 4e 65 77 2d 3e 70 53 63 68 65 6d 61 2d 3e 65 6e  New->pSchema->en
15b37 63 21 3d 45 4e 43 28 64 62 29 20 29 7b 0a 20 20  c!=ENC(db) ){.  
15b38 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
15b39 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72  intf(sizeof(zErr
15b3a 29 2c 20 7a 45 72 72 2c 20 0a 20 20 20 20 20 20  ), zErr, .      
15b3b 20 20 22 61 74 74 61 63 68 65 64 20 64 61 74 61    "attached data
15b3c 62 61 73 65 73 20 6d 75 73 74 20 75 73 65 20 74  bases must use t
15b3d 68 65 20 73 61 6d 65 20 74 65 78 74 20 65 6e 63  he same text enc
15b3e 6f 64 69 6e 67 20 61 73 20 6d 61 69 6e 20 64 61  oding as main da
15b3f 74 61 62 61 73 65 22 29 3b 0a 20 20 20 20 20 20  tabase");.      
15b40 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 72 72 6f  goto attach_erro
15b41 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  r;.    }.    sql
15b42 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67  ite3PagerLocking
15b43 4d 6f 64 65 28 73 71 6c 69 74 65 33 42 74 72 65  Mode(sqlite3Btre
15b44 65 50 61 67 65 72 28 61 4e 65 77 2d 3e 70 42 74  ePager(aNew->pBt
15b45 29 2c 20 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b 4d  ), db->dfltLockM
15b46 6f 64 65 29 3b 0a 20 20 7d 0a 20 20 61 4e 65 77  ode);.  }.  aNew
15b47 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65  ->zName = sqlite
15b48 53 74 72 44 75 70 28 7a 4e 61 6d 65 29 3b 0a 20  StrDup(zName);. 
15b49 20 61 4e 65 77 2d 3e 73 61 66 65 74 79 5f 6c 65   aNew->safety_le
15b4a 76 65 6c 20 3d 20 33 3b 0a 0a 23 69 66 20 53 51  vel = 3;..#if SQ
15b4b 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20  LITE_HAS_CODEC. 
15b4c 20 7b 0a 20 20 20 20 65 78 74 65 72 6e 20 69 6e   {.    extern in
15b4d 74 20 73 71 6c 69 74 65 33 43 6f 64 65 63 41 74  t sqlite3CodecAt
15b4e 74 61 63 68 28 73 71 6c 69 74 65 33 2a 2c 20 69  tach(sqlite3*, i
15b4f 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  nt, const void*,
15b50 20 69 6e 74 29 3b 0a 20 20 20 20 65 78 74 65 72   int);.    exter
15b51 6e 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f  n void sqlite3Co
15b52 64 65 63 47 65 74 4b 65 79 28 73 71 6c 69 74 65  decGetKey(sqlite
15b53 33 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 2a 2a 2c  3*, int, void**,
15b54 20 69 6e 74 2a 29 3b 0a 20 20 20 20 69 6e 74 20   int*);.    int 
15b55 6e 4b 65 79 3b 0a 20 20 20 20 63 68 61 72 20 2a  nKey;.    char *
15b56 7a 4b 65 79 3b 0a 20 20 20 20 69 6e 74 20 74 20  zKey;.    int t 
15b57 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
15b58 74 79 70 65 28 61 72 67 76 5b 32 5d 29 3b 0a 20  type(argv[2]);. 
15b59 20 20 20 73 77 69 74 63 68 28 20 74 20 29 7b 0a     switch( t ){.
15b5a 20 20 20 20 20 20 63 61 73 65 20 53 51 4c 49 54        case SQLIT
15b5b 45 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 20 20  E_INTEGER:.     
15b5c 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f   case SQLITE_FLO
15b5d 41 54 3a 0a 20 20 20 20 20 20 20 20 7a 45 72 72  AT:.        zErr
15b5e 44 79 6e 20 3d 20 73 71 6c 69 74 65 53 74 72 44  Dyn = sqliteStrD
15b5f 75 70 28 22 49 6e 76 61 6c 69 64 20 6b 65 79 20  up("Invalid key 
15b60 76 61 6c 75 65 22 29 3b 0a 20 20 20 20 20 20 20  value");.       
15b61 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
15b62 4f 52 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  OR;.        brea
15b63 6b 3b 0a 20 20 20 20 20 20 20 20 0a 20 20 20 20  k;.        .    
15b64 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45    case SQLITE_TE
15b65 58 54 3a 0a 20 20 20 20 20 20 63 61 73 65 20 53  XT:.      case S
15b66 51 4c 49 54 45 5f 42 4c 4f 42 3a 0a 20 20 20 20  QLITE_BLOB:.    
15b67 20 20 20 20 6e 4b 65 79 20 3d 20 73 71 6c 69 74      nKey = sqlit
15b68 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
15b69 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 20 20 20  rgv[2]);.       
15b6a 20 7a 4b 65 79 20 3d 20 28 63 68 61 72 20 2a 29   zKey = (char *)
15b6b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
15b6c 6f 62 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20  ob(argv[2]);.   
15b6d 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65       sqlite3Code
15b6e 63 41 74 74 61 63 68 28 64 62 2c 20 64 62 2d 3e  cAttach(db, db->
15b6f 6e 44 62 2d 31 2c 20 7a 4b 65 79 2c 20 6e 4b 65  nDb-1, zKey, nKe
15b70 79 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  y);.        brea
15b71 6b 3b 0a 0a 20 20 20 20 20 20 63 61 73 65 20 53  k;..      case S
15b72 51 4c 49 54 45 5f 4e 55 4c 4c 3a 0a 20 20 20 20  QLITE_NULL:.    
15b73 20 20 20 20 2f 2a 20 4e 6f 20 6b 65 79 20 73 70      /* No key sp
15b74 65 63 69 66 69 65 64 2e 20 20 55 73 65 20 74 68  ecified.  Use th
15b75 65 20 6b 65 79 20 66 72 6f 6d 20 74 68 65 20 6d  e key from the m
15b76 61 69 6e 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  ain database */.
15b77 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43          sqlite3C
15b78 6f 64 65 63 47 65 74 4b 65 79 28 64 62 2c 20 30  odecGetKey(db, 0
15b79 2c 20 28 76 6f 69 64 2a 2a 29 26 7a 4b 65 79 2c  , (void**)&zKey,
15b7a 20 26 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 20   &nKey);.       
15b7b 20 73 71 6c 69 74 65 33 43 6f 64 65 63 41 74 74   sqlite3CodecAtt
15b7c 61 63 68 28 64 62 2c 20 64 62 2d 3e 6e 44 62 2d  ach(db, db->nDb-
15b7d 31 2c 20 7a 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a  1, zKey, nKey);.
15b7e 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
15b7f 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a     }.  }.#endif.
15b80 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c  .  /* If the fil
15b81 65 20 77 61 73 20 6f 70 65 6e 65 64 20 73 75 63  e was opened suc
15b82 63 65 73 73 66 75 6c 6c 79 2c 20 72 65 61 64 20  cessfully, read 
15b83 74 68 65 20 73 63 68 65 6d 61 20 66 6f 72 20 74  the schema for t
15b84 68 65 20 6e 65 77 20 64 61 74 61 62 61 73 65 2e  he new database.
15b85 0a 20 20 2a 2a 20 49 66 20 74 68 69 73 20 66 61  .  ** If this fa
15b86 69 6c 73 2c 20 6f 72 20 69 66 20 6f 70 65 6e 69  ils, or if openi
15b87 6e 67 20 74 68 65 20 66 69 6c 65 20 66 61 69 6c  ng the file fail
15b88 65 64 2c 20 74 68 65 6e 20 63 6c 6f 73 65 20 74  ed, then close t
15b89 68 65 20 66 69 6c 65 20 61 6e 64 20 0a 20 20 2a  he file and .  *
15b8a 2a 20 72 65 6d 6f 76 65 20 74 68 65 20 65 6e 74  * remove the ent
15b8b 72 79 20 66 72 6f 6d 20 74 68 65 20 64 62 2d 3e  ry from the db->
15b8c 61 44 62 5b 5d 20 61 72 72 61 79 2e 20 69 2e 65  aDb[] array. i.e
15b8d 2e 20 70 75 74 20 65 76 65 72 79 74 68 69 6e 67  . put everything
15b8e 20 62 61 63 6b 20 74 68 65 20 77 61 79 0a 20 20   back the way.  
15b8f 2a 2a 20 77 65 20 66 6f 75 6e 64 20 69 74 2e 0a  ** we found it..
15b90 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53    */.  if( rc==S
15b91 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
15b92 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
15b93 64 62 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  db);.    rc = sq
15b94 6c 69 74 65 33 49 6e 69 74 28 64 62 2c 20 26 7a  lite3Init(db, &z
15b95 45 72 72 44 79 6e 29 3b 0a 20 20 20 20 73 71 6c  ErrDyn);.    sql
15b96 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62  ite3SafetyOff(db
15b97 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 20  );.  }.  if( rc 
15b98 29 7b 0a 20 20 20 20 69 6e 74 20 69 44 62 20 3d  ){.    int iDb =
15b99 20 64 62 2d 3e 6e 44 62 20 2d 20 31 3b 0a 20 20   db->nDb - 1;.  
15b9a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 32    assert( iDb>=2
15b9b 20 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e   );.    if( db->
15b9c 61 44 62 5b 69 44 62 5d 2e 70 42 74 20 29 7b 0a  aDb[iDb].pBt ){.
15b9d 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
15b9e 65 65 43 6c 6f 73 65 28 64 62 2d 3e 61 44 62 5b  eeClose(db->aDb[
15b9f 69 44 62 5d 2e 70 42 74 29 3b 0a 20 20 20 20 20  iDb].pBt);.     
15ba0 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42   db->aDb[iDb].pB
15ba1 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 64 62 2d  t = 0;.      db-
15ba2 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d  >aDb[iDb].pSchem
15ba3 61 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20  a = 0;.    }.   
15ba4 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74   sqlite3ResetInt
15ba5 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20  ernalSchema(db, 
15ba6 30 29 3b 0a 20 20 20 20 64 62 2d 3e 6e 44 62 20  0);.    db->nDb 
15ba7 3d 20 69 44 62 3b 0a 20 20 20 20 69 66 28 20 72  = iDb;.    if( r
15ba8 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
15ba9 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
15baa 46 61 69 6c 65 64 4d 61 6c 6c 6f 63 28 29 3b 0a  FailedMalloc();.
15bab 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
15bac 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45  printf(sizeof(zE
15bad 72 72 29 2c 7a 45 72 72 2c 20 22 6f 75 74 20 6f  rr),zErr, "out o
15bae 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 20 20 20  f memory");.    
15baf 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
15bb0 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69  ite3_snprintf(si
15bb1 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72 72 2c  zeof(zErr),zErr,
15bb2 20 22 75 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e   "unable to open
15bb3 20 64 61 74 61 62 61 73 65 3a 20 25 73 22 2c 20   database: %s", 
15bb4 7a 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20  zFile);.    }.  
15bb5 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 72    goto attach_er
15bb6 72 6f 72 3b 0a 20 20 7d 0a 20 20 0a 20 20 72 65  ror;.  }.  .  re
15bb7 74 75 72 6e 3b 0a 0a 61 74 74 61 63 68 5f 65 72  turn;..attach_er
15bb8 72 6f 72 3a 0a 20 20 2f 2a 20 52 65 74 75 72 6e  ror:.  /* Return
15bb9 20 61 6e 20 65 72 72 6f 72 20 69 66 20 77 65 20   an error if we 
15bba 67 65 74 20 68 65 72 65 20 2a 2f 0a 20 20 69 66  get here */.  if
15bbb 28 20 7a 45 72 72 44 79 6e 20 29 7b 0a 20 20 20  ( zErrDyn ){.   
15bbc 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
15bbd 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a  error(context, z
15bbe 45 72 72 44 79 6e 2c 20 2d 31 29 3b 0a 20 20 20  ErrDyn, -1);.   
15bbf 20 73 71 6c 69 74 65 46 72 65 65 28 7a 45 72 72   sqliteFree(zErr
15bc0 44 79 6e 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Dyn);.  }else{. 
15bc1 20 20 20 7a 45 72 72 5b 73 69 7a 65 6f 66 28 7a     zErr[sizeof(z
15bc2 45 72 72 29 2d 31 5d 20 3d 20 30 3b 0a 20 20 20  Err)-1] = 0;.   
15bc3 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
15bc4 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a  error(context, z
15bc5 45 72 72 2c 20 2d 31 29 3b 0a 20 20 7d 0a 7d 0a  Err, -1);.  }.}.
15bc6 0a 2f 2a 0a 2a 2a 20 41 6e 20 53 51 4c 20 75 73  ./*.** An SQL us
15bc7 65 72 2d 66 75 6e 63 74 69 6f 6e 20 72 65 67 69  er-function regi
15bc8 73 74 65 72 65 64 20 74 6f 20 64 6f 20 74 68 65  stered to do the
15bc9 20 77 6f 72 6b 20 6f 66 20 61 6e 20 44 45 54 41   work of an DETA
15bca 43 48 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68  CH statement. Th
15bcb 65 0a 2a 2a 20 74 68 72 65 65 20 61 72 67 75 6d  e.** three argum
15bcc 65 6e 74 73 20 74 6f 20 74 68 65 20 66 75 6e 63  ents to the func
15bcd 74 69 6f 6e 20 63 6f 6d 65 20 64 69 72 65 63 74  tion come direct
15bce 6c 79 20 66 72 6f 6d 20 61 20 64 65 74 61 63 68  ly from a detach
15bcf 20 73 74 61 74 65 6d 65 6e 74 3a 0a 2a 2a 0a 2a   statement:.**.*
15bd0 2a 20 20 20 20 20 44 45 54 41 43 48 20 44 41 54  *     DETACH DAT
15bd1 41 42 41 53 45 20 78 0a 2a 2a 0a 2a 2a 20 20 20  ABASE x.**.**   
15bd2 20 20 53 45 4c 45 43 54 20 73 71 6c 69 74 65 5f    SELECT sqlite_
15bd3 64 65 74 61 63 68 28 78 29 0a 2a 2f 0a 73 74 61  detach(x).*/.sta
15bd4 74 69 63 20 76 6f 69 64 20 64 65 74 61 63 68 46  tic void detachF
15bd5 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
15bd6 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
15bd7 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73  .  int argc,.  s
15bd8 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
15bd9 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63  rgv.){.  const c
15bda 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 28 63 6f  har *zName = (co
15bdb 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74  nst char *)sqlit
15bdc 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
15bdd 67 76 5b 30 5d 29 3b 0a 20 20 73 71 6c 69 74 65  gv[0]);.  sqlite
15bde 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f  3 *db = sqlite3_
15bdf 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78  user_data(contex
15be0 74 29 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 44  t);.  int i;.  D
15be1 62 20 2a 70 44 62 20 3d 20 30 3b 0a 20 20 63 68  b *pDb = 0;.  ch
15be2 61 72 20 7a 45 72 72 5b 31 32 38 5d 3b 0a 0a 20  ar zErr[128];.. 
15be3 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29 20   if( zName==0 ) 
15be4 7a 4e 61 6d 65 20 3d 20 22 22 3b 0a 20 20 66 6f  zName = "";.  fo
15be5 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62  r(i=0; i<db->nDb
15be6 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 44 62 20  ; i++){.    pDb 
15be7 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20  = &db->aDb[i];. 
15be8 20 20 20 69 66 28 20 70 44 62 2d 3e 70 42 74 3d     if( pDb->pBt=
15be9 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  =0 ) continue;. 
15bea 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74     if( sqlite3St
15beb 72 49 43 6d 70 28 70 44 62 2d 3e 7a 4e 61 6d 65  rICmp(pDb->zName
15bec 2c 20 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72  , zName)==0 ) br
15bed 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  eak;.  }..  if( 
15bee 69 3e 3d 64 62 2d 3e 6e 44 62 20 29 7b 0a 20 20  i>=db->nDb ){.  
15bef 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
15bf0 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c  tf(sizeof(zErr),
15bf1 7a 45 72 72 2c 20 22 6e 6f 20 73 75 63 68 20 64  zErr, "no such d
15bf2 61 74 61 62 61 73 65 3a 20 25 73 22 2c 20 7a 4e  atabase: %s", zN
15bf3 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 64  ame);.    goto d
15bf4 65 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d  etach_error;.  }
15bf5 0a 20 20 69 66 28 20 69 3c 32 20 29 7b 0a 20 20  .  if( i<2 ){.  
15bf6 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
15bf7 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c  tf(sizeof(zErr),
15bf8 7a 45 72 72 2c 20 22 63 61 6e 6e 6f 74 20 64 65  zErr, "cannot de
15bf9 74 61 63 68 20 64 61 74 61 62 61 73 65 20 25 73  tach database %s
15bfa 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67  ", zName);.    g
15bfb 6f 74 6f 20 64 65 74 61 63 68 5f 65 72 72 6f 72  oto detach_error
15bfc 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 64 62 2d  ;.  }.  if( !db-
15bfd 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20  >autoCommit ){. 
15bfe 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
15bff 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29  ntf(sizeof(zErr)
15c00 2c 20 7a 45 72 72 2c 0a 20 20 20 20 20 20 20 20  , zErr,.        
15c01 20 20 20 20 20 20 20 20 20 20 20 20 20 22 63 61               "ca
15c02 6e 6e 6f 74 20 44 45 54 41 43 48 20 64 61 74 61  nnot DETACH data
15c03 62 61 73 65 20 77 69 74 68 69 6e 20 74 72 61 6e  base within tran
15c04 73 61 63 74 69 6f 6e 22 29 3b 0a 20 20 20 20 67  saction");.    g
15c05 6f 74 6f 20 64 65 74 61 63 68 5f 65 72 72 6f 72  oto detach_error
15c06 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69  ;.  }.  if( sqli
15c07 74 65 33 42 74 72 65 65 49 73 49 6e 52 65 61 64  te3BtreeIsInRead
15c08 54 72 61 6e 73 28 70 44 62 2d 3e 70 42 74 29 20  Trans(pDb->pBt) 
15c09 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  ){.    sqlite3_s
15c0a 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a  nprintf(sizeof(z
15c0b 45 72 72 29 2c 7a 45 72 72 2c 20 22 64 61 74 61  Err),zErr, "data
15c0c 62 61 73 65 20 25 73 20 69 73 20 6c 6f 63 6b 65  base %s is locke
15c0d 64 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  d", zName);.    
15c0e 67 6f 74 6f 20 64 65 74 61 63 68 5f 65 72 72 6f  goto detach_erro
15c0f 72 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65  r;.  }..  sqlite
15c10 33 42 74 72 65 65 43 6c 6f 73 65 28 70 44 62 2d  3BtreeClose(pDb-
15c11 3e 70 42 74 29 3b 0a 20 20 70 44 62 2d 3e 70 42  >pBt);.  pDb->pB
15c12 74 20 3d 20 30 3b 0a 20 20 70 44 62 2d 3e 70 53  t = 0;.  pDb->pS
15c13 63 68 65 6d 61 20 3d 20 30 3b 0a 20 20 73 71 6c  chema = 0;.  sql
15c14 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61  ite3ResetInterna
15c15 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a  lSchema(db, 0);.
15c16 20 20 72 65 74 75 72 6e 3b 0a 0a 64 65 74 61 63    return;..detac
15c17 68 5f 65 72 72 6f 72 3a 0a 20 20 73 71 6c 69 74  h_error:.  sqlit
15c18 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
15c19 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d  context, zErr, -
15c1a 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  1);.}../*.** Thi
15c1b 73 20 70 72 6f 63 65 64 75 72 65 20 67 65 6e 65  s procedure gene
15c1c 72 61 74 65 73 20 56 44 42 45 20 63 6f 64 65 20  rates VDBE code 
15c1d 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 69 6e 76  for a single inv
15c1e 6f 63 61 74 69 6f 6e 20 6f 66 20 65 69 74 68 65  ocation of eithe
15c1f 72 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 5f  r the.** sqlite_
15c20 64 65 74 61 63 68 28 29 20 6f 72 20 73 71 6c 69  detach() or sqli
15c21 74 65 5f 61 74 74 61 63 68 28 29 20 53 51 4c 20  te_attach() SQL 
15c22 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 73 2e 0a  user functions..
15c23 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
15c24 6f 64 65 41 74 74 61 63 68 28 0a 20 20 50 61 72  odeAttach(.  Par
15c25 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
15c26 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20    /* The parser 
15c27 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74  context */.  int
15c28 20 74 79 70 65 2c 20 20 20 20 20 20 20 20 20 20   type,          
15c29 20 20 2f 2a 20 45 69 74 68 65 72 20 53 51 4c 49    /* Either SQLI
15c2a 54 45 5f 41 54 54 41 43 48 20 6f 72 20 53 51 4c  TE_ATTACH or SQL
15c2b 49 54 45 5f 44 45 54 41 43 48 20 2a 2f 0a 20 20  ITE_DETACH */.  
15c2c 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 75 6e  const char *zFun
15c2d 63 2c 20 20 20 2f 2a 20 45 69 74 68 65 72 20 22  c,   /* Either "
15c2e 73 71 6c 69 74 65 5f 61 74 74 61 63 68 22 20 6f  sqlite_attach" o
15c2f 72 20 22 73 71 6c 69 74 65 5f 64 65 74 61 63 68  r "sqlite_detach
15c30 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 75 6e 63 2c   */.  int nFunc,
15c31 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
15c32 6d 62 65 72 20 6f 66 20 61 72 67 73 20 74 6f 20  mber of args to 
15c33 70 61 73 73 20 74 6f 20 7a 46 75 6e 63 20 2a 2f  pass to zFunc */
15c34 0a 20 20 45 78 70 72 20 2a 70 41 75 74 68 41 72  .  Expr *pAuthAr
15c35 67 2c 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65  g,      /* Expre
15c36 73 73 69 6f 6e 20 74 6f 20 70 61 73 73 20 74 6f  ssion to pass to
15c37 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63   authorization c
15c38 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 45 78 70  allback */.  Exp
15c39 72 20 2a 70 46 69 6c 65 6e 61 6d 65 2c 20 20 20  r *pFilename,   
15c3a 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74    /* Name of dat
15c3b 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20  abase file */.  
15c3c 45 78 70 72 20 2a 70 44 62 6e 61 6d 65 2c 20 20  Expr *pDbname,  
15c3d 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
15c3e 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20  the database to 
15c3f 75 73 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20 2a  use internally *
15c40 2f 0a 20 20 45 78 70 72 20 2a 70 4b 65 79 20 20  /.  Expr *pKey  
15c41 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
15c42 62 61 73 65 20 6b 65 79 20 66 6f 72 20 65 6e 63  base key for enc
15c43 72 79 70 74 69 6f 6e 20 65 78 74 65 6e 73 69 6f  ryption extensio
15c44 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  n */.){.  int rc
15c45 3b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  ;.  NameContext 
15c46 73 4e 61 6d 65 3b 0a 20 20 56 64 62 65 20 2a 76  sName;.  Vdbe *v
15c47 3b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75  ;.  FuncDef *pFu
15c48 6e 63 3b 0a 20 20 73 71 6c 69 74 65 33 2a 20 64  nc;.  sqlite3* d
15c49 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
15c4a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
15c4b 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49  OMIT_AUTHORIZATI
15c4c 4f 4e 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ON.  assert( sql
15c4d 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69 6c 65 64  ite3MallocFailed
15c4e 28 29 20 7c 7c 20 70 41 75 74 68 41 72 67 20 29  () || pAuthArg )
15c4f 3b 0a 20 20 69 66 28 20 70 41 75 74 68 41 72 67  ;.  if( pAuthArg
15c50 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 41   ){.    char *zA
15c51 75 74 68 41 72 67 20 3d 20 73 71 6c 69 74 65 33  uthArg = sqlite3
15c52 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 26 70  NameFromToken(&p
15c53 41 75 74 68 41 72 67 2d 3e 73 70 61 6e 29 3b 0a  AuthArg->span);.
15c54 20 20 20 20 69 66 28 20 21 7a 41 75 74 68 41 72      if( !zAuthAr
15c55 67 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  g ){.      goto 
15c56 61 74 74 61 63 68 5f 65 6e 64 3b 0a 20 20 20 20  attach_end;.    
15c57 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  }.    rc = sqlit
15c58 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
15c59 73 65 2c 20 74 79 70 65 2c 20 7a 41 75 74 68 41  se, type, zAuthA
15c5a 72 67 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  rg, 0, 0);.    s
15c5b 71 6c 69 74 65 46 72 65 65 28 7a 41 75 74 68 41  qliteFree(zAuthA
15c5c 72 67 29 3b 0a 20 20 20 20 69 66 28 72 63 21 3d  rg);.    if(rc!=
15c5d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
15c5e 20 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65     goto attach_e
15c5f 6e 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  nd;.    }.  }.#e
15c60 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
15c61 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f  MIT_AUTHORIZATIO
15c62 4e 20 2a 2f 0a 0a 20 20 6d 65 6d 73 65 74 28 26  N */..  memset(&
15c63 73 4e 61 6d 65 2c 20 30 2c 20 73 69 7a 65 6f 66  sName, 0, sizeof
15c64 28 4e 61 6d 65 43 6f 6e 74 65 78 74 29 29 3b 0a  (NameContext));.
15c65 20 20 73 4e 61 6d 65 2e 70 50 61 72 73 65 20 3d    sName.pParse =
15c66 20 70 50 61 72 73 65 3b 0a 0a 20 20 69 66 28 20   pParse;..  if( 
15c67 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b  .      SQLITE_OK
15c68 21 3d 28 72 63 20 3d 20 72 65 73 6f 6c 76 65 41  !=(rc = resolveA
15c69 74 74 61 63 68 45 78 70 72 28 26 73 4e 61 6d 65  ttachExpr(&sName
15c6a 2c 20 70 46 69 6c 65 6e 61 6d 65 29 29 20 7c 7c  , pFilename)) ||
15c6b 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b  .      SQLITE_OK
15c6c 21 3d 28 72 63 20 3d 20 72 65 73 6f 6c 76 65 41  !=(rc = resolveA
15c6d 74 74 61 63 68 45 78 70 72 28 26 73 4e 61 6d 65  ttachExpr(&sName
15c6e 2c 20 70 44 62 6e 61 6d 65 29 29 20 7c 7c 0a 20  , pDbname)) ||. 
15c6f 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d       SQLITE_OK!=
15c70 28 72 63 20 3d 20 72 65 73 6f 6c 76 65 41 74 74  (rc = resolveAtt
15c71 61 63 68 45 78 70 72 28 26 73 4e 61 6d 65 2c 20  achExpr(&sName, 
15c72 70 4b 65 79 29 29 0a 20 20 29 7b 0a 20 20 20 20  pKey)).  ){.    
15c73 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a  pParse->nErr++;.
15c74 20 20 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f      goto attach_
15c75 65 6e 64 3b 0a 20 20 7d 0a 0a 20 20 76 20 3d 20  end;.  }..  v = 
15c76 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
15c77 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65  Parse);.  sqlite
15c78 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
15c79 2c 20 70 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20  , pFilename);.  
15c7a 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
15c7b 70 50 61 72 73 65 2c 20 70 44 62 6e 61 6d 65 29  pParse, pDbname)
15c7c 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43  ;.  sqlite3ExprC
15c7d 6f 64 65 28 70 50 61 72 73 65 2c 20 70 4b 65 79  ode(pParse, pKey
15c7e 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 76 20  );..  assert( v 
15c7f 7c 7c 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  || sqlite3Malloc
15c80 46 61 69 6c 65 64 28 29 20 29 3b 0a 20 20 69 66  Failed() );.  if
15c81 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ( v ){.    sqlit
15c82 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
15c83 50 5f 46 75 6e 63 74 69 6f 6e 2c 20 30 2c 20 6e  P_Function, 0, n
15c84 46 75 6e 63 29 3b 0a 20 20 20 20 70 46 75 6e 63  Func);.    pFunc
15c85 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75   = sqlite3FindFu
15c86 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 46 75 6e 63  nction(db, zFunc
15c87 2c 20 73 74 72 6c 65 6e 28 7a 46 75 6e 63 29 2c  , strlen(zFunc),
15c88 20 6e 46 75 6e 63 2c 20 53 51 4c 49 54 45 5f 55   nFunc, SQLITE_U
15c89 54 46 38 2c 30 29 3b 0a 20 20 20 20 73 71 6c 69  TF8,0);.    sqli
15c8a 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28  te3VdbeChangeP3(
15c8b 76 2c 20 2d 31 2c 20 28 63 68 61 72 20 2a 29 70  v, -1, (char *)p
15c8c 46 75 6e 63 2c 20 50 33 5f 46 55 4e 43 44 45 46  Func, P3_FUNCDEF
15c8d 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f 64 65 20  );..    /* Code 
15c8e 61 6e 20 4f 50 5f 45 78 70 69 72 65 2e 20 46 6f  an OP_Expire. Fo
15c8f 72 20 61 6e 20 41 54 54 41 43 48 20 73 74 61 74  r an ATTACH stat
15c90 65 6d 65 6e 74 2c 20 73 65 74 20 50 31 20 74 6f  ement, set P1 to
15c91 20 74 72 75 65 20 28 65 78 70 69 72 65 20 74 68   true (expire th
15c92 69 73 0a 20 20 20 20 2a 2a 20 73 74 61 74 65 6d  is.    ** statem
15c93 65 6e 74 20 6f 6e 6c 79 29 2e 20 46 6f 72 20 44  ent only). For D
15c94 45 54 41 43 48 2c 20 73 65 74 20 69 74 20 74 6f  ETACH, set it to
15c95 20 66 61 6c 73 65 20 28 65 78 70 69 72 65 20 61   false (expire a
15c96 6c 6c 20 65 78 69 73 74 69 6e 67 0a 20 20 20 20  ll existing.    
15c97 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 29 2e 0a  ** statements)..
15c98 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74      */.    sqlit
15c99 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
15c9a 50 5f 45 78 70 69 72 65 2c 20 28 74 79 70 65 3d  P_Expire, (type=
15c9b 3d 53 51 4c 49 54 45 5f 41 54 54 41 43 48 29 2c  =SQLITE_ATTACH),
15c9c 20 30 29 3b 0a 20 20 7d 0a 20 20 0a 61 74 74 61   0);.  }.  .atta
15c9d 63 68 5f 65 6e 64 3a 0a 20 20 73 71 6c 69 74 65  ch_end:.  sqlite
15c9e 33 45 78 70 72 44 65 6c 65 74 65 28 70 46 69 6c  3ExprDelete(pFil
15c9f 65 6e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65  ename);.  sqlite
15ca0 33 45 78 70 72 44 65 6c 65 74 65 28 70 44 62 6e  3ExprDelete(pDbn
15ca1 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  ame);.  sqlite3E
15ca2 78 70 72 44 65 6c 65 74 65 28 70 4b 65 79 29 3b  xprDelete(pKey);
15ca3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 65 64  .}../*.** Called
15ca4 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20 74   by the parser t
15ca5 6f 20 63 6f 6d 70 69 6c 65 20 61 20 44 45 54 41  o compile a DETA
15ca6 43 48 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a  CH statement..**
15ca7 0a 2a 2a 20 20 20 20 20 44 45 54 41 43 48 20 70  .**     DETACH p
15ca8 44 62 6e 61 6d 65 0a 2a 2f 0a 53 51 4c 49 54 45  Dbname.*/.SQLITE
15ca9 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
15caa 6c 69 74 65 33 44 65 74 61 63 68 28 50 61 72 73  lite3Detach(Pars
15cab 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20  e *pParse, Expr 
15cac 2a 70 44 62 6e 61 6d 65 29 7b 0a 20 20 63 6f 64  *pDbname){.  cod
15cad 65 41 74 74 61 63 68 28 70 50 61 72 73 65 2c 20  eAttach(pParse, 
15cae 53 51 4c 49 54 45 5f 44 45 54 41 43 48 2c 20 22  SQLITE_DETACH, "
15caf 73 71 6c 69 74 65 5f 64 65 74 61 63 68 22 2c 20  sqlite_detach", 
15cb0 31 2c 20 70 44 62 6e 61 6d 65 2c 20 30 2c 20 30  1, pDbname, 0, 0
15cb1 2c 20 70 44 62 6e 61 6d 65 29 3b 0a 7d 0a 0a 2f  , pDbname);.}../
15cb2 2a 0a 2a 2a 20 43 61 6c 6c 65 64 20 62 79 20 74  *.** Called by t
15cb3 68 65 20 70 61 72 73 65 72 20 74 6f 20 63 6f 6d  he parser to com
15cb4 70 69 6c 65 20 61 6e 20 41 54 54 41 43 48 20 73  pile an ATTACH s
15cb5 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  tatement..**.** 
15cb6 20 20 20 20 41 54 54 41 43 48 20 70 20 41 53 20      ATTACH p AS 
15cb7 70 44 62 6e 61 6d 65 20 4b 45 59 20 70 4b 65 79  pDbname KEY pKey
15cb8 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
15cb9 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
15cba 74 74 61 63 68 28 50 61 72 73 65 20 2a 70 50 61  ttach(Parse *pPa
15cbb 72 73 65 2c 20 45 78 70 72 20 2a 70 2c 20 45 78  rse, Expr *p, Ex
15cbc 70 72 20 2a 70 44 62 6e 61 6d 65 2c 20 45 78 70  pr *pDbname, Exp
15cbd 72 20 2a 70 4b 65 79 29 7b 0a 20 20 63 6f 64 65  r *pKey){.  code
15cbe 41 74 74 61 63 68 28 70 50 61 72 73 65 2c 20 53  Attach(pParse, S
15cbf 51 4c 49 54 45 5f 41 54 54 41 43 48 2c 20 22 73  QLITE_ATTACH, "s
15cc0 71 6c 69 74 65 5f 61 74 74 61 63 68 22 2c 20 33  qlite_attach", 3
15cc1 2c 20 70 2c 20 70 2c 20 70 44 62 6e 61 6d 65 2c  , p, p, pDbname,
15cc2 20 70 4b 65 79 29 3b 0a 7d 0a 23 65 6e 64 69 66   pKey);.}.#endif
15cc3 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
15cc4 41 54 54 41 43 48 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  ATTACH */../*.**
15cc5 20 52 65 67 69 73 74 65 72 20 74 68 65 20 66 75   Register the fu
15cc6 6e 63 74 69 6f 6e 73 20 73 71 6c 69 74 65 5f 61  nctions sqlite_a
15cc7 74 74 61 63 68 20 61 6e 64 20 73 71 6c 69 74 65  ttach and sqlite
15cc8 5f 64 65 74 61 63 68 2e 0a 2a 2f 0a 53 51 4c 49  _detach..*/.SQLI
15cc9 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
15cca 73 71 6c 69 74 65 33 41 74 74 61 63 68 46 75 6e  sqlite3AttachFun
15ccb 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a  ctions(sqlite3 *
15ccc 64 62 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c  db){.#ifndef SQL
15ccd 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48 0a  ITE_OMIT_ATTACH.
15cce 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69    static const i
15ccf 6e 74 20 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f  nt enc = SQLITE_
15cd0 55 54 46 38 3b 0a 20 20 73 71 6c 69 74 65 33 43  UTF8;.  sqlite3C
15cd1 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 22 73  reateFunc(db, "s
15cd2 71 6c 69 74 65 5f 61 74 74 61 63 68 22 2c 20 33  qlite_attach", 3
15cd3 2c 20 65 6e 63 2c 20 64 62 2c 20 61 74 74 61 63  , enc, db, attac
15cd4 68 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 20 20  hFunc, 0, 0);.  
15cd5 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e  sqlite3CreateFun
15cd6 63 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 64 65  c(db, "sqlite_de
15cd7 74 61 63 68 22 2c 20 31 2c 20 65 6e 63 2c 20 64  tach", 1, enc, d
15cd8 62 2c 20 64 65 74 61 63 68 46 75 6e 63 2c 20 30  b, detachFunc, 0
15cd9 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a  , 0);.#endif.}..
15cda 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65  /*.** Initialize
15cdb 20 61 20 44 62 46 69 78 65 72 20 73 74 72 75 63   a DbFixer struc
15cdc 74 75 72 65 2e 20 20 54 68 69 73 20 72 6f 75 74  ture.  This rout
15cdd 69 6e 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c  ine must be call
15cde 65 64 20 70 72 69 6f 72 0a 2a 2a 20 74 6f 20 70  ed prior.** to p
15cdf 61 73 73 69 6e 67 20 74 68 65 20 73 74 72 75 63  assing the struc
15ce0 74 75 72 65 20 74 6f 20 6f 6e 65 20 6f 66 20 74  ture to one of t
15ce1 68 65 20 73 71 6c 69 74 65 46 69 78 41 41 41 41  he sqliteFixAAAA
15ce2 28 29 20 72 6f 75 74 69 6e 65 73 20 62 65 6c 6f  () routines belo
15ce3 77 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74  w..**.** The ret
15ce4 75 72 6e 20 76 61 6c 75 65 20 69 6e 64 69 63 61  urn value indica
15ce5 74 65 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e  tes whether or n
15ce6 6f 74 20 66 69 78 61 74 69 6f 6e 20 69 73 20 72  ot fixation is r
15ce7 65 71 75 69 72 65 64 2e 20 20 54 52 55 45 0a 2a  equired.  TRUE.*
15ce8 2a 20 6d 65 61 6e 73 20 77 65 20 64 6f 20 6e 65  * means we do ne
15ce9 65 64 20 74 6f 20 66 69 78 20 74 68 65 20 64 61  ed to fix the da
15cea 74 61 62 61 73 65 20 72 65 66 65 72 65 6e 63 65  tabase reference
15ceb 73 2c 20 46 41 4c 53 45 20 6d 65 61 6e 73 20 77  s, FALSE means w
15cec 65 20 64 6f 20 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c  e do not..*/.SQL
15ced 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
15cee 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74 28 0a  sqlite3FixInit(.
15cef 20 20 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c    DbFixer *pFix,
15cf0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 78        /* The fix
15cf1 65 72 20 74 6f 20 62 65 20 69 6e 69 74 69 61 6c  er to be initial
15cf2 69 7a 65 64 20 2a 2f 0a 20 20 50 61 72 73 65 20  ized */.  Parse 
15cf3 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a  *pParse,      /*
15cf4 20 45 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20   Error messages 
15cf5 77 69 6c 6c 20 62 65 20 77 72 69 74 74 65 6e 20  will be written 
15cf6 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 44  here */.  int iD
15cf7 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  b,            /*
15cf8 20 54 68 69 73 20 69 73 20 74 68 65 20 64 61 74   This is the dat
15cf9 61 62 61 73 65 20 74 68 61 74 20 6d 75 73 74 20  abase that must 
15cfa 62 65 20 75 73 65 64 20 2a 2f 0a 20 20 63 6f 6e  be used */.  con
15cfb 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65 2c 20  st char *zType, 
15cfc 20 2f 2a 20 22 76 69 65 77 22 2c 20 22 74 72 69   /* "view", "tri
15cfd 67 67 65 72 22 2c 20 6f 72 20 22 69 6e 64 65 78  gger", or "index
15cfe 22 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b  " */.  const Tok
15cff 65 6e 20 2a 70 4e 61 6d 65 20 20 2f 2a 20 4e 61  en *pName  /* Na
15d00 6d 65 20 6f 66 20 74 68 65 20 76 69 65 77 2c 20  me of the view, 
15d01 74 72 69 67 67 65 72 2c 20 6f 72 20 69 6e 64 65  trigger, or inde
15d02 78 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65  x */.){.  sqlite
15d03 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28 20 69 44  3 *db;..  if( iD
15d04 62 3c 30 20 7c 7c 20 69 44 62 3d 3d 31 20 29 20  b<0 || iDb==1 ) 
15d05 72 65 74 75 72 6e 20 30 3b 0a 20 20 64 62 20 3d  return 0;.  db =
15d06 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61   pParse->db;.  a
15d07 73 73 65 72 74 28 20 64 62 2d 3e 6e 44 62 3e 69  ssert( db->nDb>i
15d08 44 62 20 29 3b 0a 20 20 70 46 69 78 2d 3e 70 50  Db );.  pFix->pP
15d09 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20  arse = pParse;. 
15d0a 20 70 46 69 78 2d 3e 7a 44 62 20 3d 20 64 62 2d   pFix->zDb = db-
15d0b 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b  >aDb[iDb].zName;
15d0c 0a 20 20 70 46 69 78 2d 3e 7a 54 79 70 65 20 3d  .  pFix->zType =
15d0d 20 7a 54 79 70 65 3b 0a 20 20 70 46 69 78 2d 3e   zType;.  pFix->
15d0e 70 4e 61 6d 65 20 3d 20 70 4e 61 6d 65 3b 0a 20  pName = pName;. 
15d0f 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a   return 1;.}../*
15d10 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
15d11 67 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65  g set of routine
15d12 73 20 77 61 6c 6b 20 74 68 72 6f 75 67 68 20 74  s walk through t
15d13 68 65 20 70 61 72 73 65 20 74 72 65 65 20 61 6e  he parse tree an
15d14 64 20 61 73 73 69 67 6e 0a 2a 2a 20 61 20 73 70  d assign.** a sp
15d15 65 63 69 66 69 63 20 64 61 74 61 62 61 73 65 20  ecific database 
15d16 74 6f 20 61 6c 6c 20 74 61 62 6c 65 20 72 65 66  to all table ref
15d17 65 72 65 6e 63 65 73 20 77 68 65 72 65 20 74 68  erences where th
15d18 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 0a  e database name.
15d19 2a 2a 20 77 61 73 20 6c 65 66 74 20 75 6e 73 70  ** was left unsp
15d1a 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 6f  ecified in the o
15d1b 72 69 67 69 6e 61 6c 20 53 51 4c 20 73 74 61 74  riginal SQL stat
15d1c 65 6d 65 6e 74 2e 20 20 54 68 65 20 70 46 69 78  ement.  The pFix
15d1d 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 6d 75   structure.** mu
15d1e 73 74 20 68 61 76 65 20 62 65 65 6e 20 69 6e 69  st have been ini
15d1f 74 69 61 6c 69 7a 65 64 20 62 79 20 61 20 70 72  tialized by a pr
15d20 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  ior call to sqli
15d21 74 65 33 46 69 78 49 6e 69 74 28 29 2e 0a 2a 2a  te3FixInit()..**
15d22 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e  .** These routin
15d23 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 6d  es are used to m
15d24 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 61 6e  ake sure that an
15d25 20 69 6e 64 65 78 2c 20 74 72 69 67 67 65 72 2c   index, trigger,
15d26 20 6f 72 0a 2a 2a 20 76 69 65 77 20 69 6e 20 6f   or.** view in o
15d27 6e 65 20 64 61 74 61 62 61 73 65 20 64 6f 65 73  ne database does
15d28 20 6e 6f 74 20 72 65 66 65 72 20 74 6f 20 6f 62   not refer to ob
15d29 6a 65 63 74 73 20 69 6e 20 61 20 64 69 66 66 65  jects in a diffe
15d2a 72 65 6e 74 20 64 61 74 61 62 61 73 65 2e 0a 2a  rent database..*
15d2b 2a 20 28 45 78 63 65 70 74 69 6f 6e 3a 20 69 6e  * (Exception: in
15d2c 64 69 63 65 73 2c 20 74 72 69 67 67 65 72 73 2c  dices, triggers,
15d2d 20 61 6e 64 20 76 69 65 77 73 20 69 6e 20 74 68   and views in th
15d2e 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 20  e TEMP database 
15d2f 61 72 65 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 74  are.** allowed t
15d30 6f 20 72 65 66 65 72 20 74 6f 20 61 6e 79 74 68  o refer to anyth
15d31 69 6e 67 2e 29 20 20 49 66 20 61 20 72 65 66 65  ing.)  If a refe
15d32 72 65 6e 63 65 20 69 73 20 65 78 70 6c 69 63 69  rence is explici
15d33 74 6c 79 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 61  tly made.** to a
15d34 6e 20 6f 62 6a 65 63 74 20 69 6e 20 61 20 64 69  n object in a di
15d35 66 66 65 72 65 6e 74 20 64 61 74 61 62 61 73 65  fferent database
15d36 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  , an error messa
15d37 67 65 20 69 73 20 61 64 64 65 64 20 74 6f 0a 2a  ge is added to.*
15d38 2a 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73  * pParse->zErrMs
15d39 67 20 61 6e 64 20 74 68 65 73 65 20 72 6f 75 74  g and these rout
15d3a 69 6e 65 73 20 72 65 74 75 72 6e 20 6e 6f 6e 2d  ines return non-
15d3b 7a 65 72 6f 2e 20 20 49 66 20 65 76 65 72 79 74  zero.  If everyt
15d3c 68 69 6e 67 0a 2a 2a 20 63 68 65 63 6b 73 20 6f  hing.** checks o
15d3d 75 74 2c 20 74 68 65 73 65 20 72 6f 75 74 69 6e  ut, these routin
15d3e 65 73 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a  es return 0..*/.
15d3f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
15d40 6e 74 20 73 71 6c 69 74 65 33 46 69 78 53 72 63  nt sqlite3FixSrc
15d41 4c 69 73 74 28 0a 20 20 44 62 46 69 78 65 72 20  List(.  DbFixer 
15d42 2a 70 46 69 78 2c 20 20 20 20 20 20 20 2f 2a 20  *pFix,       /* 
15d43 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 66  Context of the f
15d44 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 53 72 63  ixation */.  Src
15d45 4c 69 73 74 20 2a 70 4c 69 73 74 20 20 20 20 20  List *pList     
15d46 20 20 2f 2a 20 54 68 65 20 53 6f 75 72 63 65 20    /* The Source 
15d47 6c 69 73 74 20 74 6f 20 63 68 65 63 6b 20 61 6e  list to check an
15d48 64 20 6d 6f 64 69 66 79 20 2a 2f 0a 29 7b 0a 20  d modify */.){. 
15d49 20 69 6e 74 20 69 3b 0a 20 20 63 6f 6e 73 74 20   int i;.  const 
15d4a 63 68 61 72 20 2a 7a 44 62 3b 0a 20 20 73 74 72  char *zDb;.  str
15d4b 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
15d4c 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 69 66 28 20   *pItem;..  if( 
15d4d 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72  pList==0 ) retur
15d4e 6e 20 30 3b 0a 20 20 7a 44 62 20 3d 20 70 46 69  n 0;.  zDb = pFi
15d4f 78 2d 3e 7a 44 62 3b 0a 20 20 66 6f 72 28 69 3d  x->zDb;.  for(i=
15d50 30 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e  0, pItem=pList->
15d51 61 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 53 72 63  a; i<pList->nSrc
15d52 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b  ; i++, pItem++){
15d53 0a 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e  .    if( pItem->
15d54 7a 44 61 74 61 62 61 73 65 3d 3d 30 20 29 7b 0a  zDatabase==0 ){.
15d55 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 7a 44 61        pItem->zDa
15d56 74 61 62 61 73 65 20 3d 20 73 71 6c 69 74 65 53  tabase = sqliteS
15d57 74 72 44 75 70 28 7a 44 62 29 3b 0a 20 20 20 20  trDup(zDb);.    
15d58 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65  }else if( sqlite
15d59 33 53 74 72 49 43 6d 70 28 70 49 74 65 6d 2d 3e  3StrICmp(pItem->
15d5a 7a 44 61 74 61 62 61 73 65 2c 7a 44 62 29 21 3d  zDatabase,zDb)!=
15d5b 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
15d5c 65 33 45 72 72 6f 72 4d 73 67 28 70 46 69 78 2d  e3ErrorMsg(pFix-
15d5d 3e 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20  >pParse,.       
15d5e 20 20 22 25 73 20 25 54 20 63 61 6e 6e 6f 74 20    "%s %T cannot 
15d5f 72 65 66 65 72 65 6e 63 65 20 6f 62 6a 65 63 74  reference object
15d60 73 20 69 6e 20 64 61 74 61 62 61 73 65 20 25 73  s in database %s
15d61 22 2c 0a 20 20 20 20 20 20 20 20 20 70 46 69 78  ",.         pFix
15d62 2d 3e 7a 54 79 70 65 2c 20 70 46 69 78 2d 3e 70  ->zType, pFix->p
15d63 4e 61 6d 65 2c 20 70 49 74 65 6d 2d 3e 7a 44 61  Name, pItem->zDa
15d64 74 61 62 61 73 65 29 3b 0a 20 20 20 20 20 20 72  tabase);.      r
15d65 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 23  eturn 1;.    }.#
15d66 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
15d67 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c  TE_OMIT_VIEW) ||
15d68 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
15d69 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a 20  _OMIT_TRIGGER). 
15d6a 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69     if( sqlite3Fi
15d6b 78 53 65 6c 65 63 74 28 70 46 69 78 2c 20 70 49  xSelect(pFix, pI
15d6c 74 65 6d 2d 3e 70 53 65 6c 65 63 74 29 20 29 20  tem->pSelect) ) 
15d6d 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 69 66  return 1;.    if
15d6e 28 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 72  ( sqlite3FixExpr
15d6f 28 70 46 69 78 2c 20 70 49 74 65 6d 2d 3e 70 4f  (pFix, pItem->pO
15d70 6e 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 23  n) ) return 1;.#
15d71 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72 65 74 75  endif.  }.  retu
15d72 72 6e 20 30 3b 0a 7d 0a 23 69 66 20 21 64 65 66  rn 0;.}.#if !def
15d73 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
15d74 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e  _VIEW) || !defin
15d75 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ed(SQLITE_OMIT_T
15d76 52 49 47 47 45 52 29 0a 53 51 4c 49 54 45 5f 50  RIGGER).SQLITE_P
15d77 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
15d78 65 33 46 69 78 53 65 6c 65 63 74 28 0a 20 20 44  e3FixSelect(.  D
15d79 62 46 69 78 65 72 20 2a 70 46 69 78 2c 20 20 20  bFixer *pFix,   
15d7a 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 6f      /* Context o
15d7b 66 20 74 68 65 20 66 69 78 61 74 69 6f 6e 20 2a  f the fixation *
15d7c 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c  /.  Select *pSel
15d7d 65 63 74 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ect      /* The 
15d7e 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
15d7f 20 74 6f 20 62 65 20 66 69 78 65 64 20 74 6f 20   to be fixed to 
15d80 6f 6e 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  one database */.
15d81 29 7b 0a 20 20 77 68 69 6c 65 28 20 70 53 65 6c  ){.  while( pSel
15d82 65 63 74 20 29 7b 0a 20 20 20 20 69 66 28 20 73  ect ){.    if( s
15d83 71 6c 69 74 65 33 46 69 78 45 78 70 72 4c 69 73  qlite3FixExprLis
15d84 74 28 70 46 69 78 2c 20 70 53 65 6c 65 63 74 2d  t(pFix, pSelect-
15d85 3e 70 45 4c 69 73 74 29 20 29 7b 0a 20 20 20 20  >pEList) ){.    
15d86 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
15d87 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  }.    if( sqlite
15d88 33 46 69 78 53 72 63 4c 69 73 74 28 70 46 69 78  3FixSrcList(pFix
15d89 2c 20 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 29  , pSelect->pSrc)
15d8a 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
15d8b 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   1;.    }.    if
15d8c 28 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 72  ( sqlite3FixExpr
15d8d 28 70 46 69 78 2c 20 70 53 65 6c 65 63 74 2d 3e  (pFix, pSelect->
15d8e 70 57 68 65 72 65 29 20 29 7b 0a 20 20 20 20 20  pWhere) ){.     
15d8f 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
15d90 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
15d91 46 69 78 45 78 70 72 28 70 46 69 78 2c 20 70 53  FixExpr(pFix, pS
15d92 65 6c 65 63 74 2d 3e 70 48 61 76 69 6e 67 29 20  elect->pHaving) 
15d93 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
15d94 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 65  1;.    }.    pSe
15d95 6c 65 63 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e  lect = pSelect->
15d96 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 72 65  pPrior;.  }.  re
15d97 74 75 72 6e 20 30 3b 0a 7d 0a 53 51 4c 49 54 45  turn 0;.}.SQLITE
15d98 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
15d99 69 74 65 33 46 69 78 45 78 70 72 28 0a 20 20 44  ite3FixExpr(.  D
15d9a 62 46 69 78 65 72 20 2a 70 46 69 78 2c 20 20 20  bFixer *pFix,   
15d9b 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20    /* Context of 
15d9c 74 68 65 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0a  the fixation */.
15d9d 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 20 20    Expr *pExpr   
15d9e 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72       /* The expr
15d9f 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 66 69 78  ession to be fix
15da0 65 64 20 74 6f 20 6f 6e 65 20 64 61 74 61 62 61  ed to one databa
15da1 73 65 20 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c 65  se */.){.  while
15da2 28 20 70 45 78 70 72 20 29 7b 0a 20 20 20 20 69  ( pExpr ){.    i
15da3 66 28 20 73 71 6c 69 74 65 33 46 69 78 53 65 6c  f( sqlite3FixSel
15da4 65 63 74 28 70 46 69 78 2c 20 70 45 78 70 72 2d  ect(pFix, pExpr-
15da5 3e 70 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20  >pSelect) ){.   
15da6 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
15da7 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74   }.    if( sqlit
15da8 65 33 46 69 78 45 78 70 72 4c 69 73 74 28 70 46  e3FixExprList(pF
15da9 69 78 2c 20 70 45 78 70 72 2d 3e 70 4c 69 73 74  ix, pExpr->pList
15daa 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
15dab 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  n 1;.    }.    i
15dac 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78 70  f( sqlite3FixExp
15dad 72 28 70 46 69 78 2c 20 70 45 78 70 72 2d 3e 70  r(pFix, pExpr->p
15dae 52 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20  Right) ){.      
15daf 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
15db0 20 20 20 20 70 45 78 70 72 20 3d 20 70 45 78 70      pExpr = pExp
15db1 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 7d 0a 20 20  r->pLeft;.  }.  
15db2 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 53 51 4c 49  return 0;.}.SQLI
15db3 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
15db4 71 6c 69 74 65 33 46 69 78 45 78 70 72 4c 69 73  qlite3FixExprLis
15db5 74 28 0a 20 20 44 62 46 69 78 65 72 20 2a 70 46  t(.  DbFixer *pF
15db6 69 78 2c 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65  ix,     /* Conte
15db7 78 74 20 6f 66 20 74 68 65 20 66 69 78 61 74 69  xt of the fixati
15db8 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  on */.  ExprList
15db9 20 2a 70 4c 69 73 74 20 20 20 20 2f 2a 20 54 68   *pList    /* Th
15dba 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20  e expression to 
15dbb 62 65 20 66 69 78 65 64 20 74 6f 20 6f 6e 65 20  be fixed to one 
15dbc 64 61 74 61 62 61 73 65 20 2a 2f 0a 29 7b 0a 20  database */.){. 
15dbd 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74   int i;.  struct
15dbe 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
15dbf 70 49 74 65 6d 3b 0a 20 20 69 66 28 20 70 4c 69  pItem;.  if( pLi
15dc0 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  st==0 ) return 0
15dc1 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74  ;.  for(i=0, pIt
15dc2 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3c 70  em=pList->a; i<p
15dc3 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b  List->nExpr; i++
15dc4 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20  , pItem++){.    
15dc5 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78  if( sqlite3FixEx
15dc6 70 72 28 70 46 69 78 2c 20 70 49 74 65 6d 2d 3e  pr(pFix, pItem->
15dc7 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20  pExpr) ){.      
15dc8 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
15dc9 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
15dca 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65  }.#endif..#ifnde
15dcb 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
15dcc 49 47 47 45 52 0a 53 51 4c 49 54 45 5f 50 52 49  IGGER.SQLITE_PRI
15dcd 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
15dce 46 69 78 54 72 69 67 67 65 72 53 74 65 70 28 0a  FixTriggerStep(.
15dcf 20 20 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c    DbFixer *pFix,
15dd0 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20       /* Context 
15dd1 6f 66 20 74 68 65 20 66 69 78 61 74 69 6f 6e 20  of the fixation 
15dd2 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 65 70  */.  TriggerStep
15dd3 20 2a 70 53 74 65 70 20 2f 2a 20 54 68 65 20 74   *pStep /* The t
15dd4 72 69 67 67 65 72 20 73 74 65 70 20 62 65 20 66  rigger step be f
15dd5 69 78 65 64 20 74 6f 20 6f 6e 65 20 64 61 74 61  ixed to one data
15dd6 62 61 73 65 20 2a 2f 0a 29 7b 0a 20 20 77 68 69  base */.){.  whi
15dd7 6c 65 28 20 70 53 74 65 70 20 29 7b 0a 20 20 20  le( pStep ){.   
15dd8 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 53   if( sqlite3FixS
15dd9 65 6c 65 63 74 28 70 46 69 78 2c 20 70 53 74 65  elect(pFix, pSte
15dda 70 2d 3e 70 53 65 6c 65 63 74 29 20 29 7b 0a 20  p->pSelect) ){. 
15ddb 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
15ddc 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c     }.    if( sql
15ddd 69 74 65 33 46 69 78 45 78 70 72 28 70 46 69 78  ite3FixExpr(pFix
15dde 2c 20 70 53 74 65 70 2d 3e 70 57 68 65 72 65 29  , pStep->pWhere)
15ddf 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
15de0 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   1;.    }.    if
15de1 28 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 72  ( sqlite3FixExpr
15de2 4c 69 73 74 28 70 46 69 78 2c 20 70 53 74 65 70  List(pFix, pStep
15de3 2d 3e 70 45 78 70 72 4c 69 73 74 29 20 29 7b 0a  ->pExprList) ){.
15de4 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
15de5 20 20 20 20 7d 0a 20 20 20 20 70 53 74 65 70 20      }.    pStep 
15de6 3d 20 70 53 74 65 70 2d 3e 70 4e 65 78 74 3b 0a  = pStep->pNext;.
15de7 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
15de8 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a  }.#endif../*****
15de9 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
15dea 20 61 74 74 61 63 68 2e 63 20 2a 2a 2a 2a 2a 2a   attach.c ******
15deb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15dec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15ded 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
15dee 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
15def 66 69 6c 65 20 61 75 74 68 2e 63 20 2a 2a 2a 2a  file auth.c ****
15df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15df1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15df2 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
15df3 32 30 30 33 20 4a 61 6e 75 61 72 79 20 31 31 0a  2003 January 11.
15df4 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
15df5 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
15df6 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
15df7 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
15df8 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
15df9 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
15dfa 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
15dfb 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
15dfc 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
15dfd 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
15dfe 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
15dff 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
15e00 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
15e01 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
15e02 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
15e03 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
15e04 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
15e05 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
15e06 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15e07 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15e08 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15e09 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
15e0a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
15e0b 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74  ains code used t
15e0c 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  o implement the 
15e0d 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68  sqlite3_set_auth
15e0e 6f 72 69 7a 65 72 28 29 0a 2a 2a 20 41 50 49 2e  orizer().** API.
15e0f 20 20 54 68 69 73 20 66 61 63 69 6c 69 74 79 20    This facility 
15e10 69 73 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 66  is an optional f
15e11 65 61 74 75 72 65 20 6f 66 20 74 68 65 20 6c 69  eature of the li
15e12 62 72 61 72 79 2e 20 20 45 6d 62 65 64 64 65 64  brary.  Embedded
15e13 0a 2a 2a 20 73 79 73 74 65 6d 73 20 74 68 61 74  .** systems that
15e14 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 68 69   do not need thi
15e15 73 20 66 61 63 69 6c 69 74 79 20 6d 61 79 20 6f  s facility may o
15e16 6d 69 74 20 69 74 20 62 79 20 72 65 63 6f 6d 70  mit it by recomp
15e17 69 6c 69 6e 67 0a 2a 2a 20 74 68 65 20 6c 69 62  iling.** the lib
15e18 72 61 72 79 20 77 69 74 68 20 2d 44 53 51 4c 49  rary with -DSQLI
15e19 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
15e1a 41 54 49 4f 4e 3d 31 0a 2a 2a 0a 2a 2a 20 24 49  ATION=1.**.** $I
15e1b 64 3a 20 61 75 74 68 2e 63 2c 76 20 31 2e 32 36  d: auth.c,v 1.26
15e1c 20 32 30 30 37 2f 30 35 2f 31 34 20 31 31 3a 33   2007/05/14 11:3
15e1d 34 3a 34 37 20 64 72 68 20 45 78 70 20 24 0a 2a  4:47 drh Exp $.*
15e1e 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20  /../*.** All of 
15e1f 74 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73  the code in this
15e20 20 66 69 6c 65 20 6d 61 79 20 62 65 20 6f 6d 69   file may be omi
15e21 74 74 65 64 20 62 79 20 64 65 66 69 6e 69 6e 67  tted by defining
15e22 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 6d 61 63   a single.** mac
15e23 72 6f 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ro..*/.#ifndef S
15e24 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
15e25 52 49 5a 41 54 49 4f 4e 0a 0a 2f 2a 0a 2a 2a 20  RIZATION../*.** 
15e26 53 65 74 20 6f 72 20 63 6c 65 61 72 20 74 68 65  Set or clear the
15e27 20 61 63 63 65 73 73 20 61 75 74 68 6f 72 69 7a   access authoriz
15e28 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 2e 0a  ation function..
15e29 2a 2a 0a 2a 2a 20 54 68 65 20 61 63 63 65 73 73  **.** The access
15e2a 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66   authorization f
15e2b 75 6e 63 74 69 6f 6e 20 69 73 20 62 65 20 63 61  unction is be ca
15e2c 6c 6c 65 64 20 64 75 72 69 6e 67 20 74 68 65 20  lled during the 
15e2d 63 6f 6d 70 69 6c 61 74 69 6f 6e 0a 2a 2a 20 70  compilation.** p
15e2e 68 61 73 65 20 74 6f 20 76 65 72 69 66 79 20 74  hase to verify t
15e2f 68 61 74 20 74 68 65 20 75 73 65 72 20 68 61 73  hat the user has
15e30 20 72 65 61 64 20 61 6e 64 2f 6f 72 20 77 72 69   read and/or wri
15e31 74 65 20 61 63 63 65 73 73 20 70 65 72 6d 69 73  te access permis
15e32 73 69 6f 6e 20 6f 6e 0a 2a 2a 20 76 61 72 69 6f  sion on.** vario
15e33 75 73 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65  us fields of the
15e34 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 65 20   database.  The 
15e35 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74  first argument t
15e36 6f 20 74 68 65 20 61 75 74 68 20 66 75 6e 63 74  o the auth funct
15e37 69 6f 6e 0a 2a 2a 20 69 73 20 61 20 63 6f 70 79  ion.** is a copy
15e38 20 6f 66 20 74 68 65 20 33 72 64 20 61 72 67 75   of the 3rd argu
15e39 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 72 6f 75  ment to this rou
15e3a 74 69 6e 65 2e 20 20 54 68 65 20 73 65 63 6f 6e  tine.  The secon
15e3b 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f  d argument.** to
15e3c 20 74 68 65 20 61 75 74 68 20 66 75 6e 63 74 69   the auth functi
15e3d 6f 6e 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65  on is one of the
15e3e 73 65 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a  se constants:.**
15e3f 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
15e40 5f 43 52 45 41 54 45 5f 49 4e 44 45 58 0a 2a 2a  _CREATE_INDEX.**
15e41 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52         SQLITE_CR
15e42 45 41 54 45 5f 54 41 42 4c 45 0a 2a 2a 20 20 20  EATE_TABLE.**   
15e43 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54      SQLITE_CREAT
15e44 45 5f 54 45 4d 50 5f 49 4e 44 45 58 0a 2a 2a 20  E_TEMP_INDEX.** 
15e45 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45        SQLITE_CRE
15e46 41 54 45 5f 54 45 4d 50 5f 54 41 42 4c 45 0a 2a  ATE_TEMP_TABLE.*
15e47 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43  *       SQLITE_C
15e48 52 45 41 54 45 5f 54 45 4d 50 5f 54 52 49 47 47  REATE_TEMP_TRIGG
15e49 45 52 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49  ER.**       SQLI
15e4a 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 56  TE_CREATE_TEMP_V
15e4b 49 45 57 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  IEW.**       SQL
15e4c 49 54 45 5f 43 52 45 41 54 45 5f 54 52 49 47 47  ITE_CREATE_TRIGG
15e4d 45 52 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49  ER.**       SQLI
15e4e 54 45 5f 43 52 45 41 54 45 5f 56 49 45 57 0a 2a  TE_CREATE_VIEW.*
15e4f 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44  *       SQLITE_D
15e50 45 4c 45 54 45 0a 2a 2a 20 20 20 20 20 20 20 53  ELETE.**       S
15e51 51 4c 49 54 45 5f 44 52 4f 50 5f 49 4e 44 45 58  QLITE_DROP_INDEX
15e52 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
15e53 5f 44 52 4f 50 5f 54 41 42 4c 45 0a 2a 2a 20 20  _DROP_TABLE.**  
15e54 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f 50       SQLITE_DROP
15e55 5f 54 45 4d 50 5f 49 4e 44 45 58 0a 2a 2a 20 20  _TEMP_INDEX.**  
15e56 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f 50       SQLITE_DROP
15e57 5f 54 45 4d 50 5f 54 41 42 4c 45 0a 2a 2a 20 20  _TEMP_TABLE.**  
15e58 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f 50       SQLITE_DROP
15e59 5f 54 45 4d 50 5f 54 52 49 47 47 45 52 0a 2a 2a  _TEMP_TRIGGER.**
15e5a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52         SQLITE_DR
15e5b 4f 50 5f 54 45 4d 50 5f 56 49 45 57 0a 2a 2a 20  OP_TEMP_VIEW.** 
15e5c 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f        SQLITE_DRO
15e5d 50 5f 54 52 49 47 47 45 52 0a 2a 2a 20 20 20 20  P_TRIGGER.**    
15e5e 20 20 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56     SQLITE_DROP_V
15e5f 49 45 57 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  IEW.**       SQL
15e60 49 54 45 5f 49 4e 53 45 52 54 0a 2a 2a 20 20 20  ITE_INSERT.**   
15e61 20 20 20 20 53 51 4c 49 54 45 5f 50 52 41 47 4d      SQLITE_PRAGM
15e62 41 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54  A.**       SQLIT
15e63 45 5f 52 45 41 44 0a 2a 2a 20 20 20 20 20 20 20  E_READ.**       
15e64 53 51 4c 49 54 45 5f 53 45 4c 45 43 54 0a 2a 2a  SQLITE_SELECT.**
15e65 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 54 52         SQLITE_TR
15e66 41 4e 53 41 43 54 49 4f 4e 0a 2a 2a 20 20 20 20  ANSACTION.**    
15e67 20 20 20 53 51 4c 49 54 45 5f 55 50 44 41 54 45     SQLITE_UPDATE
15e68 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64  .**.** The third
15e69 20 61 6e 64 20 66 6f 75 72 74 68 20 61 72 67 75   and fourth argu
15e6a 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 61 75 74  ments to the aut
15e6b 68 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 74  h function are t
15e6c 68 65 20 6e 61 6d 65 20 6f 66 0a 2a 2a 20 74 68  he name of.** th
15e6d 65 20 74 61 62 6c 65 20 61 6e 64 20 74 68 65 20  e table and the 
15e6e 63 6f 6c 75 6d 6e 20 74 68 61 74 20 61 72 65 20  column that are 
15e6f 62 65 69 6e 67 20 61 63 63 65 73 73 65 64 2e 20  being accessed. 
15e70 20 54 68 65 20 61 75 74 68 20 66 75 6e 63 74 69   The auth functi
15e71 6f 6e 0a 2a 2a 20 73 68 6f 75 6c 64 20 72 65 74  on.** should ret
15e72 75 72 6e 20 65 69 74 68 65 72 20 53 51 4c 49 54  urn either SQLIT
15e73 45 5f 4f 4b 2c 20 53 51 4c 49 54 45 5f 44 45 4e  E_OK, SQLITE_DEN
15e74 59 2c 20 6f 72 20 53 51 4c 49 54 45 5f 49 47 4e  Y, or SQLITE_IGN
15e75 4f 52 45 2e 20 20 49 66 0a 2a 2a 20 53 51 4c 49  ORE.  If.** SQLI
15e76 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
15e77 64 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74  d, it means that
15e78 20 61 63 63 65 73 73 20 69 73 20 61 6c 6c 6f 77   access is allow
15e79 65 64 2e 20 20 53 51 4c 49 54 45 5f 44 45 4e 59  ed.  SQLITE_DENY
15e7a 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74 20 74  .** means that t
15e7b 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
15e7c 20 77 69 6c 6c 20 6e 65 76 65 72 2d 72 75 6e 20   will never-run 
15e7d 2d 20 74 68 65 20 73 71 6c 69 74 65 33 5f 65 78  - the sqlite3_ex
15e7e 65 63 28 29 20 63 61 6c 6c 0a 2a 2a 20 77 69 6c  ec() call.** wil
15e7f 6c 20 72 65 74 75 72 6e 20 77 69 74 68 20 61 6e  l return with an
15e80 20 65 72 72 6f 72 2e 20 20 53 51 4c 49 54 45 5f   error.  SQLITE_
15e81 49 47 4e 4f 52 45 20 6d 65 61 6e 73 20 74 68 61  IGNORE means tha
15e82 74 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d  t the SQL statem
15e83 65 6e 74 0a 2a 2a 20 73 68 6f 75 6c 64 20 72 75  ent.** should ru
15e84 6e 20 62 75 74 20 61 74 74 65 6d 70 74 73 20 74  n but attempts t
15e85 6f 20 72 65 61 64 20 74 68 65 20 73 70 65 63 69  o read the speci
15e86 66 69 65 64 20 63 6f 6c 75 6d 6e 20 77 69 6c 6c  fied column will
15e87 20 72 65 74 75 72 6e 20 4e 55 4c 4c 0a 2a 2a 20   return NULL.** 
15e88 61 6e 64 20 61 74 74 65 6d 70 74 73 20 74 6f 20  and attempts to 
15e89 77 72 69 74 65 20 74 68 65 20 63 6f 6c 75 6d 6e  write the column
15e8a 20 77 69 6c 6c 20 62 65 20 69 67 6e 6f 72 65 64   will be ignored
15e8b 2e 0a 2a 2a 0a 2a 2a 20 53 65 74 74 69 6e 67 20  ..**.** Setting 
15e8c 74 68 65 20 61 75 74 68 20 66 75 6e 63 74 69 6f  the auth functio
15e8d 6e 20 74 6f 20 4e 55 4c 4c 20 64 69 73 61 62 6c  n to NULL disabl
15e8e 65 73 20 74 68 69 73 20 68 6f 6f 6b 2e 20 20 54  es this hook.  T
15e8f 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 73 65  he default.** se
15e90 74 74 69 6e 67 20 6f 66 20 74 68 65 20 61 75 74  tting of the aut
15e91 68 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55  h function is NU
15e92 4c 4c 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74  LL..*/.int sqlit
15e93 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65  e3_set_authorize
15e94 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  r(.  sqlite3 *db
15e95 2c 0a 20 20 69 6e 74 20 28 2a 78 41 75 74 68 29  ,.  int (*xAuth)
15e96 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74  (void*,int,const
15e97 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61   char*,const cha
15e98 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63  r*,const char*,c
15e99 6f 6e 73 74 20 63 68 61 72 2a 29 2c 0a 20 20 76  onst char*),.  v
15e9a 6f 69 64 20 2a 70 41 72 67 0a 29 7b 0a 20 20 64  oid *pArg.){.  d
15e9b 62 2d 3e 78 41 75 74 68 20 3d 20 78 41 75 74 68  b->xAuth = xAuth
15e9c 3b 0a 20 20 64 62 2d 3e 70 41 75 74 68 41 72 67  ;.  db->pAuthArg
15e9d 20 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c 69 74   = pArg;.  sqlit
15e9e 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64  e3ExpirePrepared
15e9f 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a  Statements(db);.
15ea0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
15ea1 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69  OK;.}../*.** Wri
15ea2 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  te an error mess
15ea3 61 67 65 20 69 6e 74 6f 20 70 50 61 72 73 65 2d  age into pParse-
15ea4 3e 7a 45 72 72 4d 73 67 20 74 68 61 74 20 65 78  >zErrMsg that ex
15ea5 70 6c 61 69 6e 73 20 74 68 61 74 20 74 68 65 0a  plains that the.
15ea6 2a 2a 20 75 73 65 72 2d 73 75 70 70 6c 69 65 64  ** user-supplied
15ea7 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66   authorization f
15ea8 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 65 64  unction returned
15ea9 20 61 6e 20 69 6c 6c 65 67 61 6c 20 76 61 6c 75   an illegal valu
15eaa 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
15eab 64 20 73 71 6c 69 74 65 41 75 74 68 42 61 64 52  d sqliteAuthBadR
15eac 65 74 75 72 6e 43 6f 64 65 28 50 61 72 73 65 20  eturnCode(Parse 
15ead 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 72 63 29  *pParse, int rc)
15eae 7b 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72  {.  sqlite3Error
15eaf 4d 73 67 28 70 50 61 72 73 65 2c 20 22 69 6c 6c  Msg(pParse, "ill
15eb0 65 67 61 6c 20 72 65 74 75 72 6e 20 76 61 6c 75  egal return valu
15eb1 65 20 28 25 64 29 20 66 72 6f 6d 20 74 68 65 20  e (%d) from the 
15eb2 22 0a 20 20 20 20 22 61 75 74 68 6f 72 69 7a 61  ".    "authoriza
15eb3 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2d 20  tion function - 
15eb4 73 68 6f 75 6c 64 20 62 65 20 53 51 4c 49 54 45  should be SQLITE
15eb5 5f 4f 4b 2c 20 53 51 4c 49 54 45 5f 49 47 4e 4f  _OK, SQLITE_IGNO
15eb6 52 45 2c 20 22 0a 20 20 20 20 22 6f 72 20 53 51  RE, ".    "or SQ
15eb7 4c 49 54 45 5f 44 45 4e 59 22 2c 20 72 63 29 3b  LITE_DENY", rc);
15eb8 0a 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20  .  pParse->rc = 
15eb9 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a  SQLITE_ERROR;.}.
15eba 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 45 78 70 72  ./*.** The pExpr
15ebb 20 73 68 6f 75 6c 64 20 62 65 20 61 20 54 4b 5f   should be a TK_
15ebc 43 4f 4c 55 4d 4e 20 65 78 70 72 65 73 73 69 6f  COLUMN expressio
15ebd 6e 2e 20 20 54 68 65 20 74 61 62 6c 65 20 72 65  n.  The table re
15ebe 66 65 72 72 65 64 20 74 6f 0a 2a 2a 20 69 73 20  ferred to.** is 
15ebf 69 6e 20 70 54 61 62 4c 69 73 74 20 6f 72 20 65  in pTabList or e
15ec0 6c 73 65 20 69 74 20 69 73 20 74 68 65 20 4e 45  lse it is the NE
15ec1 57 20 6f 72 20 4f 4c 44 20 74 61 62 6c 65 20 6f  W or OLD table o
15ec2 66 20 61 20 74 72 69 67 67 65 72 2e 20 20 0a 2a  f a trigger.  .*
15ec3 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69  * Check to see i
15ec4 66 20 69 74 20 69 73 20 4f 4b 20 74 6f 20 72 65  f it is OK to re
15ec5 61 64 20 74 68 69 73 20 70 61 72 74 69 63 75 6c  ad this particul
15ec6 61 72 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a  ar column..**.**
15ec7 20 49 66 20 74 68 65 20 61 75 74 68 20 66 75 6e   If the auth fun
15ec8 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 53 51  ction returns SQ
15ec9 4c 49 54 45 5f 49 47 4e 4f 52 45 2c 20 63 68 61  LITE_IGNORE, cha
15eca 6e 67 65 20 74 68 65 20 54 4b 5f 43 4f 4c 55 4d  nge the TK_COLUM
15ecb 4e 20 0a 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f  N .** instructio
15ecc 6e 20 69 6e 74 6f 20 61 20 54 4b 5f 4e 55 4c 4c  n into a TK_NULL
15ecd 2e 20 20 49 66 20 74 68 65 20 61 75 74 68 20 66  .  If the auth f
15ece 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
15ecf 53 51 4c 49 54 45 5f 44 45 4e 59 2c 0a 2a 2a 20  SQLITE_DENY,.** 
15ed0 74 68 65 6e 20 67 65 6e 65 72 61 74 65 20 61 6e  then generate an
15ed1 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54   error..*/.SQLIT
15ed2 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
15ed3 71 6c 69 74 65 33 41 75 74 68 52 65 61 64 28 0a  qlite3AuthRead(.
15ed4 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
15ed5 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
15ed6 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
15ed7 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20  .  Expr *pExpr, 
15ed8 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
15ed9 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 63 68  expression to ch
15eda 65 63 6b 20 61 75 74 68 6f 72 69 7a 61 74 69 6f  eck authorizatio
15edb 6e 20 6f 6e 20 2a 2f 0a 20 20 53 72 63 4c 69 73  n on */.  SrcLis
15edc 74 20 2a 70 54 61 62 4c 69 73 74 20 20 20 20 20  t *pTabList     
15edd 2f 2a 20 41 6c 6c 20 74 61 62 6c 65 20 74 68 61  /* All table tha
15ede 74 20 70 45 78 70 72 20 6d 69 67 68 74 20 72 65  t pExpr might re
15edf 66 65 72 20 74 6f 20 2a 2f 0a 29 7b 0a 20 20 73  fer to */.){.  s
15ee0 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
15ee1 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 72  rse->db;.  int r
15ee2 63 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  c;.  Table *pTab
15ee3 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ;          /* Th
15ee4 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 72 65  e table being re
15ee5 61 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ad */.  const ch
15ee6 61 72 20 2a 7a 43 6f 6c 3b 20 20 20 20 20 2f 2a  ar *zCol;     /*
15ee7 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c   Name of the col
15ee8 75 6d 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65  umn of the table
15ee9 20 2a 2f 0a 20 20 69 6e 74 20 69 53 72 63 3b 20   */.  int iSrc; 
15eea 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
15eeb 6e 64 65 78 20 69 6e 20 70 54 61 62 4c 69 73 74  ndex in pTabList
15eec 2d 3e 61 5b 5d 20 6f 66 20 74 61 62 6c 65 20 62  ->a[] of table b
15eed 65 69 6e 67 20 72 65 61 64 20 2a 2f 0a 20 20 63  eing read */.  c
15eee 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 42 61 73  onst char *zDBas
15eef 65 3b 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20  e;   /* Name of 
15ef0 64 61 74 61 62 61 73 65 20 62 65 69 6e 67 20 61  database being a
15ef1 63 63 65 73 73 65 64 20 2a 2f 0a 20 20 54 72 69  ccessed */.  Tri
15ef2 67 67 65 72 53 74 61 63 6b 20 2a 70 53 74 61 63  ggerStack *pStac
15ef3 6b 3b 20 2f 2a 20 54 68 65 20 73 74 61 63 6b 20  k; /* The stack 
15ef4 6f 66 20 63 75 72 72 65 6e 74 20 74 72 69 67 67  of current trigg
15ef5 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62  ers */.  int iDb
15ef6 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
15ef7 2a 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20 74  * The index of t
15ef8 68 65 20 64 61 74 61 62 61 73 65 20 74 68 65 20  he database the 
15ef9 65 78 70 72 65 73 73 69 6f 6e 20 72 65 66 65 72  expression refer
15efa 73 20 74 6f 20 2a 2f 0a 0a 20 20 69 66 28 20 64  s to */..  if( d
15efb 62 2d 3e 78 41 75 74 68 3d 3d 30 20 29 20 72 65  b->xAuth==0 ) re
15efc 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 45 78 70  turn;.  if( pExp
15efd 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e  r->op!=TK_COLUMN
15efe 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 44 62   ) return;.  iDb
15eff 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
15f00 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e  ToIndex(pParse->
15f01 64 62 2c 20 70 45 78 70 72 2d 3e 70 53 63 68 65  db, pExpr->pSche
15f02 6d 61 29 3b 0a 20 20 69 66 28 20 69 44 62 3c 30  ma);.  if( iDb<0
15f03 20 29 7b 0a 20 20 20 20 2f 2a 20 41 6e 20 61 74   ){.    /* An at
15f04 74 65 6d 70 74 20 74 6f 20 72 65 61 64 20 61 20  tempt to read a 
15f05 63 6f 6c 75 6d 6e 20 6f 75 74 20 6f 66 20 61 20  column out of a 
15f06 73 75 62 71 75 65 72 79 20 6f 72 20 6f 74 68 65  subquery or othe
15f07 72 0a 20 20 20 20 2a 2a 20 74 65 6d 70 6f 72 61  r.    ** tempora
15f08 72 79 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 20  ry table. */.   
15f09 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66   return;.  }.  f
15f0a 6f 72 28 69 53 72 63 3d 30 3b 20 70 54 61 62 4c  or(iSrc=0; pTabL
15f0b 69 73 74 20 26 26 20 69 53 72 63 3c 70 54 61 62  ist && iSrc<pTab
15f0c 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 53 72 63  List->nSrc; iSrc
15f0d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 45 78  ++){.    if( pEx
15f0e 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 70 54 61 62  pr->iTable==pTab
15f0f 4c 69 73 74 2d 3e 61 5b 69 53 72 63 5d 2e 69 43  List->a[iSrc].iC
15f10 75 72 73 6f 72 20 29 20 62 72 65 61 6b 3b 0a 20  ursor ) break;. 
15f11 20 7d 0a 20 20 69 66 28 20 69 53 72 63 3e 3d 30   }.  if( iSrc>=0
15f12 20 26 26 20 70 54 61 62 4c 69 73 74 20 26 26 20   && pTabList && 
15f13 69 53 72 63 3c 70 54 61 62 4c 69 73 74 2d 3e 6e  iSrc<pTabList->n
15f14 53 72 63 20 29 7b 0a 20 20 20 20 70 54 61 62 20  Src ){.    pTab 
15f15 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 53  = pTabList->a[iS
15f16 72 63 5d 2e 70 54 61 62 3b 0a 20 20 7d 65 6c 73  rc].pTab;.  }els
15f17 65 20 69 66 28 20 28 70 53 74 61 63 6b 20 3d 20  e if( (pStack = 
15f18 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63  pParse->trigStac
15f19 6b 29 21 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20  k)!=0 ){.    /* 
15f1a 54 68 69 73 20 6d 75 73 74 20 62 65 20 61 6e 20  This must be an 
15f1b 61 74 74 65 6d 70 74 20 74 6f 20 72 65 61 64 20  attempt to read 
15f1c 74 68 65 20 4e 45 57 20 6f 72 20 4f 4c 44 20 70  the NEW or OLD p
15f1d 73 65 75 64 6f 2d 74 61 62 6c 65 73 0a 20 20 20  seudo-tables.   
15f1e 20 2a 2a 20 6f 66 20 61 20 74 72 69 67 67 65 72   ** of a trigger
15f1f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73  ..    */.    ass
15f20 65 72 74 28 20 70 45 78 70 72 2d 3e 69 54 61 62  ert( pExpr->iTab
15f21 6c 65 3d 3d 70 53 74 61 63 6b 2d 3e 6e 65 77 49  le==pStack->newI
15f22 64 78 20 7c 7c 20 70 45 78 70 72 2d 3e 69 54 61  dx || pExpr->iTa
15f23 62 6c 65 3d 3d 70 53 74 61 63 6b 2d 3e 6f 6c 64  ble==pStack->old
15f24 49 64 78 20 29 3b 0a 20 20 20 20 70 54 61 62 20  Idx );.    pTab 
15f25 3d 20 70 53 74 61 63 6b 2d 3e 70 54 61 62 3b 0a  = pStack->pTab;.
15f26 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
15f27 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  urn;.  }.  if( p
15f28 54 61 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  Tab==0 ) return;
15f29 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 43  .  if( pExpr->iC
15f2a 6f 6c 75 6d 6e 3e 3d 30 20 29 7b 0a 20 20 20 20  olumn>=0 ){.    
15f2b 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 69  assert( pExpr->i
15f2c 43 6f 6c 75 6d 6e 3c 70 54 61 62 2d 3e 6e 43 6f  Column<pTab->nCo
15f2d 6c 20 29 3b 0a 20 20 20 20 7a 43 6f 6c 20 3d 20  l );.    zCol = 
15f2e 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 45 78 70 72  pTab->aCol[pExpr
15f2f 2d 3e 69 43 6f 6c 75 6d 6e 5d 2e 7a 4e 61 6d 65  ->iColumn].zName
15f30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 54  ;.  }else if( pT
15f31 61 62 2d 3e 69 50 4b 65 79 3e 3d 30 20 29 7b 0a  ab->iPKey>=0 ){.
15f32 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62      assert( pTab
15f33 2d 3e 69 50 4b 65 79 3c 70 54 61 62 2d 3e 6e 43  ->iPKey<pTab->nC
15f34 6f 6c 20 29 3b 0a 20 20 20 20 7a 43 6f 6c 20 3d  ol );.    zCol =
15f35 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 54 61 62   pTab->aCol[pTab
15f36 2d 3e 69 50 4b 65 79 5d 2e 7a 4e 61 6d 65 3b 0a  ->iPKey].zName;.
15f37 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 43 6f    }else{.    zCo
15f38 6c 20 3d 20 22 52 4f 57 49 44 22 3b 0a 20 20 7d  l = "ROWID";.  }
15f39 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d  .  assert( iDb>=
15f3a 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62  0 && iDb<db->nDb
15f3b 20 29 3b 0a 20 20 7a 44 42 61 73 65 20 3d 20 64   );.  zDBase = d
15f3c 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
15f3d 65 3b 0a 20 20 72 63 20 3d 20 64 62 2d 3e 78 41  e;.  rc = db->xA
15f3e 75 74 68 28 64 62 2d 3e 70 41 75 74 68 41 72 67  uth(db->pAuthArg
15f3f 2c 20 53 51 4c 49 54 45 5f 52 45 41 44 2c 20 70  , SQLITE_READ, p
15f40 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c  Tab->zName, zCol
15f41 2c 20 7a 44 42 61 73 65 2c 20 0a 20 20 20 20 20  , zDBase, .     
15f42 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61 72              pPar
15f43 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74  se->zAuthContext
15f44 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
15f45 49 54 45 5f 49 47 4e 4f 52 45 20 29 7b 0a 20 20  ITE_IGNORE ){.  
15f46 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b    pExpr->op = TK
15f47 5f 4e 55 4c 4c 3b 0a 20 20 7d 65 6c 73 65 20 69  _NULL;.  }else i
15f48 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 45  f( rc==SQLITE_DE
15f49 4e 59 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62  NY ){.    if( db
15f4a 2d 3e 6e 44 62 3e 32 20 7c 7c 20 69 44 62 21 3d  ->nDb>2 || iDb!=
15f4b 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
15f4c 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
15f4d 65 2c 20 22 61 63 63 65 73 73 20 74 6f 20 25 73  e, "access to %s
15f4e 2e 25 73 2e 25 73 20 69 73 20 70 72 6f 68 69 62  .%s.%s is prohib
15f4f 69 74 65 64 22 2c 20 0a 20 20 20 20 20 20 20 20  ited", .        
15f50 20 7a 44 42 61 73 65 2c 20 70 54 61 62 2d 3e 7a   zDBase, pTab->z
15f51 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20  Name, zCol);.   
15f52 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71   }else{.      sq
15f53 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
15f54 61 72 73 65 2c 20 22 61 63 63 65 73 73 20 74 6f  arse, "access to
15f55 20 25 73 2e 25 73 20 69 73 20 70 72 6f 68 69 62   %s.%s is prohib
15f56 69 74 65 64 22 2c 70 54 61 62 2d 3e 7a 4e 61 6d  ited",pTab->zNam
15f57 65 2c 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d 0a 20  e,zCol);.    }. 
15f58 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20     pParse->rc = 
15f59 53 51 4c 49 54 45 5f 41 55 54 48 3b 0a 20 20 7d  SQLITE_AUTH;.  }
15f5a 65 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c  else if( rc!=SQL
15f5b 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71  ITE_OK ){.    sq
15f5c 6c 69 74 65 41 75 74 68 42 61 64 52 65 74 75 72  liteAuthBadRetur
15f5d 6e 43 6f 64 65 28 70 50 61 72 73 65 2c 20 72 63  nCode(pParse, rc
15f5e 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
15f5f 44 6f 20 61 6e 20 61 75 74 68 6f 72 69 7a 61 74  Do an authorizat
15f60 69 6f 6e 20 63 68 65 63 6b 20 75 73 69 6e 67 20  ion check using 
15f61 74 68 65 20 63 6f 64 65 20 61 6e 64 20 61 72 67  the code and arg
15f62 75 6d 65 6e 74 73 20 67 69 76 65 6e 2e 20 20 52  uments given.  R
15f63 65 74 75 72 6e 0a 2a 2a 20 65 69 74 68 65 72 20  eturn.** either 
15f64 53 51 4c 49 54 45 5f 4f 4b 20 28 7a 65 72 6f 29  SQLITE_OK (zero)
15f65 20 6f 72 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52   or SQLITE_IGNOR
15f66 45 20 6f 72 20 53 51 4c 49 54 45 5f 44 45 4e 59  E or SQLITE_DENY
15f67 2e 20 20 49 66 20 53 51 4c 49 54 45 5f 44 45 4e  .  If SQLITE_DEN
15f68 59 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64  Y.** is returned
15f69 2c 20 74 68 65 6e 20 74 68 65 20 65 72 72 6f 72  , then the error
15f6a 20 63 6f 75 6e 74 20 61 6e 64 20 65 72 72 6f 72   count and error
15f6b 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50 61 72   message in pPar
15f6c 73 65 20 61 72 65 0a 2a 2a 20 6d 6f 64 69 66 69  se are.** modifi
15f6d 65 64 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79  ed appropriately
15f6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
15f6f 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41  ATE int sqlite3A
15f70 75 74 68 43 68 65 63 6b 28 0a 20 20 50 61 72 73  uthCheck(.  Pars
15f71 65 20 2a 70 50 61 72 73 65 2c 0a 20 20 69 6e 74  e *pParse,.  int
15f72 20 63 6f 64 65 2c 0a 20 20 63 6f 6e 73 74 20 63   code,.  const c
15f73 68 61 72 20 2a 7a 41 72 67 31 2c 0a 20 20 63 6f  har *zArg1,.  co
15f74 6e 73 74 20 63 68 61 72 20 2a 7a 41 72 67 32 2c  nst char *zArg2,
15f75 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
15f76 41 72 67 33 0a 29 7b 0a 20 20 73 71 6c 69 74 65  Arg3.){.  sqlite
15f77 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
15f78 64 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20  db;.  int rc;.. 
15f79 20 2f 2a 20 44 6f 6e 27 74 20 64 6f 20 61 6e 79   /* Don't do any
15f7a 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63   authorization c
15f7b 68 65 63 6b 73 20 69 66 20 74 68 65 20 64 61 74  hecks if the dat
15f7c 61 62 61 73 65 20 69 73 20 69 6e 69 74 69 61 6c  abase is initial
15f7d 69 73 69 6e 67 0a 20 20 2a 2a 20 6f 72 20 69 66  ising.  ** or if
15f7e 20 74 68 65 20 70 61 72 73 65 72 20 69 73 20 62   the parser is b
15f7f 65 69 6e 67 20 69 6e 76 6f 6b 65 64 20 66 72 6f  eing invoked fro
15f80 6d 20 77 69 74 68 69 6e 20 73 71 6c 69 74 65 33  m within sqlite3
15f81 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 2e 0a 20  _declare_vtab.. 
15f82 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e   */.  if( db->in
15f83 69 74 2e 62 75 73 79 20 7c 7c 20 49 4e 5f 44 45  it.busy || IN_DE
15f84 43 4c 41 52 45 5f 56 54 41 42 20 29 7b 0a 20 20  CLARE_VTAB ){.  
15f85 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
15f86 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 64  OK;.  }..  if( d
15f87 62 2d 3e 78 41 75 74 68 3d 3d 30 20 29 7b 0a 20  b->xAuth==0 ){. 
15f88 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
15f89 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20  _OK;.  }.  rc = 
15f8a 64 62 2d 3e 78 41 75 74 68 28 64 62 2d 3e 70 41  db->xAuth(db->pA
15f8b 75 74 68 41 72 67 2c 20 63 6f 64 65 2c 20 7a 41  uthArg, code, zA
15f8c 72 67 31 2c 20 7a 41 72 67 32 2c 20 7a 41 72 67  rg1, zArg2, zArg
15f8d 33 2c 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68  3, pParse->zAuth
15f8e 43 6f 6e 74 65 78 74 29 3b 0a 20 20 69 66 28 20  Context);.  if( 
15f8f 72 63 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20  rc==SQLITE_DENY 
15f90 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
15f91 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
15f92 6e 6f 74 20 61 75 74 68 6f 72 69 7a 65 64 22 29  not authorized")
15f93 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63  ;.    pParse->rc
15f94 20 3d 20 53 51 4c 49 54 45 5f 41 55 54 48 3b 0a   = SQLITE_AUTH;.
15f95 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 21 3d    }else if( rc!=
15f96 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21  SQLITE_OK && rc!
15f97 3d 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 29  =SQLITE_IGNORE )
15f98 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
15f99 45 5f 44 45 4e 59 3b 0a 20 20 20 20 73 71 6c 69  E_DENY;.    sqli
15f9a 74 65 41 75 74 68 42 61 64 52 65 74 75 72 6e 43  teAuthBadReturnC
15f9b 6f 64 65 28 70 50 61 72 73 65 2c 20 72 63 29 3b  ode(pParse, rc);
15f9c 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
15f9d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 75 73 68 20  ;.}../*.** Push 
15f9e 61 6e 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  an authorization
15f9f 20 63 6f 6e 74 65 78 74 2e 20 20 41 66 74 65 72   context.  After
15fa0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
15fa1 20 63 61 6c 6c 65 64 2c 20 74 68 65 0a 2a 2a 20   called, the.** 
15fa2 7a 41 72 67 33 20 61 72 67 75 6d 65 6e 74 20 74  zArg3 argument t
15fa3 6f 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  o authorization 
15fa4 63 61 6c 6c 62 61 63 6b 73 20 77 69 6c 6c 20 62  callbacks will b
15fa5 65 20 7a 43 6f 6e 74 65 78 74 20 75 6e 74 69 6c  e zContext until
15fa6 0a 2a 2a 20 70 6f 70 70 65 64 2e 20 20 4f 72 20  .** popped.  Or 
15fa7 69 66 20 70 50 61 72 73 65 3d 3d 30 2c 20 74 68  if pParse==0, th
15fa8 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
15fa9 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45  no-op..*/.SQLITE
15faa 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
15fab 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74  lite3AuthContext
15fac 50 75 73 68 28 0a 20 20 50 61 72 73 65 20 2a 70  Push(.  Parse *p
15fad 50 61 72 73 65 2c 0a 20 20 41 75 74 68 43 6f 6e  Parse,.  AuthCon
15fae 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c 20  text *pContext, 
15faf 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
15fb0 43 6f 6e 74 65 78 74 0a 29 7b 0a 20 20 70 43 6f  Context.){.  pCo
15fb1 6e 74 65 78 74 2d 3e 70 50 61 72 73 65 20 3d 20  ntext->pParse = 
15fb2 70 50 61 72 73 65 3b 0a 20 20 69 66 28 20 70 50  pParse;.  if( pP
15fb3 61 72 73 65 20 29 7b 0a 20 20 20 20 70 43 6f 6e  arse ){.    pCon
15fb4 74 65 78 74 2d 3e 7a 41 75 74 68 43 6f 6e 74 65  text->zAuthConte
15fb5 78 74 20 3d 20 70 50 61 72 73 65 2d 3e 7a 41 75  xt = pParse->zAu
15fb6 74 68 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 70  thContext;.    p
15fb7 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74  Parse->zAuthCont
15fb8 65 78 74 20 3d 20 7a 43 6f 6e 74 65 78 74 3b 0a  ext = zContext;.
15fb9 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70    }.}../*.** Pop
15fba 20 61 6e 20 61 75 74 68 6f 72 69 7a 61 74 69 6f   an authorizatio
15fbb 6e 20 63 6f 6e 74 65 78 74 20 74 68 61 74 20 77  n context that w
15fbc 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 70 75  as previously pu
15fbd 73 68 65 64 0a 2a 2a 20 62 79 20 73 71 6c 69 74  shed.** by sqlit
15fbe 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75 73  e3AuthContextPus
15fbf 68 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  h.*/.SQLITE_PRIV
15fc0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
15fc1 41 75 74 68 43 6f 6e 74 65 78 74 50 6f 70 28 41  AuthContextPop(A
15fc2 75 74 68 43 6f 6e 74 65 78 74 20 2a 70 43 6f 6e  uthContext *pCon
15fc3 74 65 78 74 29 7b 0a 20 20 69 66 28 20 70 43 6f  text){.  if( pCo
15fc4 6e 74 65 78 74 2d 3e 70 50 61 72 73 65 20 29 7b  ntext->pParse ){
15fc5 0a 20 20 20 20 70 43 6f 6e 74 65 78 74 2d 3e 70  .    pContext->p
15fc6 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74  Parse->zAuthCont
15fc7 65 78 74 20 3d 20 70 43 6f 6e 74 65 78 74 2d 3e  ext = pContext->
15fc8 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a 20 20  zAuthContext;.  
15fc9 20 20 70 43 6f 6e 74 65 78 74 2d 3e 70 50 61 72    pContext->pPar
15fca 73 65 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 23  se = 0;.  }.}..#
15fcb 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
15fcc 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49  OMIT_AUTHORIZATI
15fcd 4f 4e 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ON */../********
15fce 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 61 75  ****** End of au
15fcf 74 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  th.c ***********
15fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15fd1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15fd2 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
15fd3 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
15fd4 65 20 62 75 69 6c 64 2e 63 20 2a 2a 2a 2a 2a 2a  e build.c ******
15fd5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15fd6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15fd7 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
15fd8 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a  1 September 15.*
15fd9 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
15fda 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
15fdb 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
15fdc 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
15fdd 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
15fde 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
15fdf 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
15fe0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
15fe1 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
15fe2 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
15fe3 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
15fe4 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
15fe5 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
15fe6 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
15fe7 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
15fe8 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
15fe9 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
15fea 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
15feb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15fec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15fed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15fee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
15fef 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
15ff0 69 6e 73 20 43 20 63 6f 64 65 20 72 6f 75 74 69  ins C code routi
15ff1 6e 65 73 20 74 68 61 74 20 61 72 65 20 63 61 6c  nes that are cal
15ff2 6c 65 64 20 62 79 20 74 68 65 20 53 51 4c 69 74  led by the SQLit
15ff3 65 20 70 61 72 73 65 72 0a 2a 2a 20 77 68 65 6e  e parser.** when
15ff4 20 73 79 6e 74 61 78 20 72 75 6c 65 73 20 61 72   syntax rules ar
15ff5 65 20 72 65 64 75 63 65 64 2e 20 20 54 68 65 20  e reduced.  The 
15ff6 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73  routines in this
15ff7 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 74 68 65   file handle the
15ff8 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 6b 69  .** following ki
15ff9 6e 64 73 20 6f 66 20 53 51 4c 20 73 79 6e 74 61  nds of SQL synta
15ffa 78 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43 52 45  x:.**.**     CRE
15ffb 41 54 45 20 54 41 42 4c 45 0a 2a 2a 20 20 20 20  ATE TABLE.**    
15ffc 20 44 52 4f 50 20 54 41 42 4c 45 0a 2a 2a 20 20   DROP TABLE.**  
15ffd 20 20 20 43 52 45 41 54 45 20 49 4e 44 45 58 0a     CREATE INDEX.
15ffe 2a 2a 20 20 20 20 20 44 52 4f 50 20 49 4e 44 45  **     DROP INDE
15fff 58 0a 2a 2a 20 20 20 20 20 63 72 65 61 74 69 6e  X.**     creatin
16000 67 20 49 44 20 6c 69 73 74 73 0a 2a 2a 20 20 20  g ID lists.**   
16001 20 20 42 45 47 49 4e 20 54 52 41 4e 53 41 43 54    BEGIN TRANSACT
16002 49 4f 4e 0a 2a 2a 20 20 20 20 20 43 4f 4d 4d 49  ION.**     COMMI
16003 54 0a 2a 2a 20 20 20 20 20 52 4f 4c 4c 42 41 43  T.**     ROLLBAC
16004 4b 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 62 75 69  K.**.** $Id: bui
16005 6c 64 2e 63 2c 76 20 31 2e 34 33 33 20 32 30 30  ld.c,v 1.433 200
16006 37 2f 30 37 2f 30 32 20 31 39 3a 33 31 3a 32 37  7/07/02 19:31:27
16007 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f   drh Exp $.*/../
16008 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
16009 65 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e  e is called when
1600a 20 61 20 6e 65 77 20 53 51 4c 20 73 74 61 74 65   a new SQL state
1600b 6d 65 6e 74 20 69 73 20 62 65 67 69 6e 6e 69 6e  ment is beginnin
1600c 67 20 74 6f 0a 2a 2a 20 62 65 20 70 61 72 73 65  g to.** be parse
1600d 64 2e 20 20 49 6e 69 74 69 61 6c 69 7a 65 20 74  d.  Initialize t
1600e 68 65 20 70 50 61 72 73 65 20 73 74 72 75 63 74  he pParse struct
1600f 75 72 65 20 61 73 20 6e 65 65 64 65 64 2e 0a 2a  ure as needed..*
16010 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
16011 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67   void sqlite3Beg
16012 69 6e 50 61 72 73 65 28 50 61 72 73 65 20 2a 70  inParse(Parse *p
16013 50 61 72 73 65 2c 20 69 6e 74 20 65 78 70 6c 61  Parse, int expla
16014 69 6e 46 6c 61 67 29 7b 0a 20 20 70 50 61 72 73  inFlag){.  pPars
16015 65 2d 3e 65 78 70 6c 61 69 6e 20 3d 20 65 78 70  e->explain = exp
16016 6c 61 69 6e 46 6c 61 67 3b 0a 20 20 70 50 61 72  lainFlag;.  pPar
16017 73 65 2d 3e 6e 56 61 72 20 3d 20 30 3b 0a 7d 0a  se->nVar = 0;.}.
16018 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
16019 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
1601a 45 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 61 62 6c  E./*.** The Tabl
1601b 65 4c 6f 63 6b 20 73 74 72 75 63 74 75 72 65 20  eLock structure 
1601c 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 62 79 20  is only used by 
1601d 74 68 65 20 73 71 6c 69 74 65 33 54 61 62 6c 65  the sqlite3Table
1601e 4c 6f 63 6b 28 29 20 61 6e 64 0a 2a 2a 20 63 6f  Lock() and.** co
1601f 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28 29 20 66  deTableLocks() f
16020 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 72  unctions..*/.str
16021 75 63 74 20 54 61 62 6c 65 4c 6f 63 6b 20 7b 0a  uct TableLock {.
16022 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20    int iDb;      
16023 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
16024 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e  tabase containin
16025 67 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62  g the table to b
16026 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 69 6e  e locked */.  in
16027 74 20 69 54 61 62 3b 20 20 20 20 20 20 20 20 20  t iTab;         
16028 20 20 20 2f 2a 20 54 68 65 20 72 6f 6f 74 20 70     /* The root p
16029 61 67 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  age of the table
1602a 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f   to be locked */
1602b 0a 20 20 75 38 20 69 73 57 72 69 74 65 4c 6f 63  .  u8 isWriteLoc
1602c 6b 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  k;      /* True 
1602d 66 6f 72 20 77 72 69 74 65 20 6c 6f 63 6b 2e 20  for write lock. 
1602e 20 46 61 6c 73 65 20 66 6f 72 20 61 20 72 65 61   False for a rea
1602f 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 63 6f 6e 73  d lock */.  cons
16030 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20  t char *zName;  
16031 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
16032 74 61 62 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  table */.};../*.
16033 2a 2a 20 52 65 63 6f 72 64 20 74 68 65 20 66 61  ** Record the fa
16034 63 74 20 74 68 61 74 20 77 65 20 77 61 6e 74 20  ct that we want 
16035 74 6f 20 6c 6f 63 6b 20 61 20 74 61 62 6c 65 20  to lock a table 
16036 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20 0a 2a  at run-time.  .*
16037 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 74  *.** The table t
16038 6f 20 62 65 20 6c 6f 63 6b 65 64 20 68 61 73 20  o be locked has 
16039 72 6f 6f 74 20 70 61 67 65 20 69 54 61 62 20 61  root page iTab a
1603a 6e 64 20 69 73 20 66 6f 75 6e 64 20 69 6e 20 64  nd is found in d
1603b 61 74 61 62 61 73 65 20 69 44 62 2e 0a 2a 2a 20  atabase iDb..** 
1603c 41 20 72 65 61 64 20 6f 72 20 61 20 77 72 69 74  A read or a writ
1603d 65 20 6c 6f 63 6b 20 63 61 6e 20 62 65 20 74 61  e lock can be ta
1603e 6b 65 6e 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e  ken depending on
1603f 20 69 73 57 72 69 74 65 6c 6f 63 6b 2e 0a 2a 2a   isWritelock..**
16040 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
16041 20 6a 75 73 74 20 72 65 63 6f 72 64 73 20 74 68   just records th
16042 65 20 66 61 63 74 20 74 68 61 74 20 74 68 65 20  e fact that the 
16043 6c 6f 63 6b 20 69 73 20 64 65 73 69 72 65 64 2e  lock is desired.
16044 20 20 54 68 65 0a 2a 2a 20 63 6f 64 65 20 74 6f    The.** code to
16045 20 6d 61 6b 65 20 74 68 65 20 6c 6f 63 6b 20 6f   make the lock o
16046 63 63 75 72 20 69 73 20 67 65 6e 65 72 61 74 65  ccur is generate
16047 64 20 62 79 20 61 20 6c 61 74 65 72 20 63 61 6c  d by a later cal
16048 6c 20 74 6f 0a 2a 2a 20 63 6f 64 65 54 61 62 6c  l to.** codeTabl
16049 65 4c 6f 63 6b 73 28 29 20 77 68 69 63 68 20 6f  eLocks() which o
1604a 63 63 75 72 73 20 64 75 72 69 6e 67 20 73 71 6c  ccurs during sql
1604b 69 74 65 33 46 69 6e 69 73 68 43 6f 64 69 6e 67  ite3FinishCoding
1604c 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ()..*/.SQLITE_PR
1604d 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1604e 65 33 54 61 62 6c 65 4c 6f 63 6b 28 0a 20 20 50  e3TableLock(.  P
1604f 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
16050 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
16051 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44  text */.  int iD
16052 62 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  b,           /* 
16053 49 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74  Index of the dat
16054 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67  abase containing
16055 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 6c 6f   the table to lo
16056 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62  ck */.  int iTab
16057 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f  ,          /* Ro
16058 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f  ot page number o
16059 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62  f the table to b
1605a 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 38  e locked */.  u8
1605b 20 69 73 57 72 69 74 65 4c 6f 63 6b 2c 20 20 20   isWriteLock,   
1605c 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 77   /* True for a w
1605d 72 69 74 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 63  rite lock */.  c
1605e 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
1605f 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
16060 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63   table to be loc
16061 6b 65 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ked */.){.  int 
16062 69 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 3b  i;.  int nBytes;
16063 0a 20 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 70 3b  .  TableLock *p;
16064 0a 0a 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74  ..  if( 0==sqlit
16065 65 33 54 68 72 65 61 64 44 61 74 61 52 65 61 64  e3ThreadDataRead
16066 4f 6e 6c 79 28 29 2d 3e 75 73 65 53 68 61 72 65  Only()->useShare
16067 64 44 61 74 61 20 7c 7c 20 69 44 62 3c 30 20 29  dData || iDb<0 )
16068 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
16069 7d 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  }..  for(i=0; i<
1606a 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f  pParse->nTableLo
1606b 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20  ck; i++){.    p 
1606c 3d 20 26 70 50 61 72 73 65 2d 3e 61 54 61 62 6c  = &pParse->aTabl
1606d 65 4c 6f 63 6b 5b 69 5d 3b 0a 20 20 20 20 69 66  eLock[i];.    if
1606e 28 20 70 2d 3e 69 44 62 3d 3d 69 44 62 20 26 26  ( p->iDb==iDb &&
1606f 20 70 2d 3e 69 54 61 62 3d 3d 69 54 61 62 20 29   p->iTab==iTab )
16070 7b 0a 20 20 20 20 20 20 70 2d 3e 69 73 57 72 69  {.      p->isWri
16071 74 65 4c 6f 63 6b 20 3d 20 28 70 2d 3e 69 73 57  teLock = (p->isW
16072 72 69 74 65 4c 6f 63 6b 20 7c 7c 20 69 73 57 72  riteLock || isWr
16073 69 74 65 4c 6f 63 6b 29 3b 0a 20 20 20 20 20 20  iteLock);.      
16074 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
16075 7d 0a 0a 20 20 6e 42 79 74 65 73 20 3d 20 73 69  }..  nBytes = si
16076 7a 65 6f 66 28 54 61 62 6c 65 4c 6f 63 6b 29 20  zeof(TableLock) 
16077 2a 20 28 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c  * (pParse->nTabl
16078 65 4c 6f 63 6b 2b 31 29 3b 0a 20 20 70 50 61 72  eLock+1);.  pPar
16079 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 20 3d  se->aTableLock =
1607a 20 73 71 6c 69 74 65 52 65 61 6c 6c 6f 63 4f 72   sqliteReallocOr
1607b 46 72 65 65 28 70 50 61 72 73 65 2d 3e 61 54 61  Free(pParse->aTa
1607c 62 6c 65 4c 6f 63 6b 2c 20 6e 42 79 74 65 73 29  bleLock, nBytes)
1607d 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ;.  if( pParse->
1607e 61 54 61 62 6c 65 4c 6f 63 6b 20 29 7b 0a 20 20  aTableLock ){.  
1607f 20 20 70 20 3d 20 26 70 50 61 72 73 65 2d 3e 61    p = &pParse->a
16080 54 61 62 6c 65 4c 6f 63 6b 5b 70 50 61 72 73 65  TableLock[pParse
16081 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 2b 2b 5d 3b  ->nTableLock++];
16082 0a 20 20 20 20 70 2d 3e 69 44 62 20 3d 20 69 44  .    p->iDb = iD
16083 62 3b 0a 20 20 20 20 70 2d 3e 69 54 61 62 20 3d  b;.    p->iTab =
16084 20 69 54 61 62 3b 0a 20 20 20 20 70 2d 3e 69 73   iTab;.    p->is
16085 57 72 69 74 65 4c 6f 63 6b 20 3d 20 69 73 57 72  WriteLock = isWr
16086 69 74 65 4c 6f 63 6b 3b 0a 20 20 20 20 70 2d 3e  iteLock;.    p->
16087 7a 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 0a 20  zName = zName;. 
16088 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 64 65   }.}../*.** Code
16089 20 61 6e 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b   an OP_TableLock
1608a 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 66 6f 72   instruction for
1608b 20 65 61 63 68 20 74 61 62 6c 65 20 6c 6f 63 6b   each table lock
1608c 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 73 74 61  ed by the.** sta
1608d 74 65 6d 65 6e 74 20 28 63 6f 6e 66 69 67 75 72  tement (configur
1608e 65 64 20 62 79 20 63 61 6c 6c 73 20 74 6f 20 73  ed by calls to s
1608f 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28  qlite3TableLock(
16090 29 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ))..*/.static vo
16091 69 64 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63 6b  id codeTableLock
16092 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29  s(Parse *pParse)
16093 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 56 64 62  {.  int i;.  Vdb
16094 65 20 2a 70 56 64 62 65 3b 20 0a 20 20 61 73 73  e *pVdbe; .  ass
16095 65 72 74 28 20 73 71 6c 69 74 65 33 54 68 72 65  ert( sqlite3Thre
16096 61 64 44 61 74 61 52 65 61 64 4f 6e 6c 79 28 29  adDataReadOnly()
16097 2d 3e 75 73 65 53 68 61 72 65 64 44 61 74 61 20  ->useSharedData 
16098 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c  || pParse->nTabl
16099 65 4c 6f 63 6b 3d 3d 30 20 29 3b 0a 0a 20 20 69  eLock==0 );..  i
1609a 66 28 20 30 3d 3d 28 70 56 64 62 65 20 3d 20 73  f( 0==(pVdbe = s
1609b 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
1609c 61 72 73 65 29 29 20 29 7b 0a 20 20 20 20 72 65  arse)) ){.    re
1609d 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72  turn;.  }..  for
1609e 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e  (i=0; i<pParse->
1609f 6e 54 61 62 6c 65 4c 6f 63 6b 3b 20 69 2b 2b 29  nTableLock; i++)
160a0 7b 0a 20 20 20 20 54 61 62 6c 65 4c 6f 63 6b 20  {.    TableLock 
160a1 2a 70 20 3d 20 26 70 50 61 72 73 65 2d 3e 61 54  *p = &pParse->aT
160a2 61 62 6c 65 4c 6f 63 6b 5b 69 5d 3b 0a 20 20 20  ableLock[i];.   
160a3 20 69 6e 74 20 70 31 20 3d 20 70 2d 3e 69 44 62   int p1 = p->iDb
160a4 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 73 57  ;.    if( p->isW
160a5 72 69 74 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 20  riteLock ){.    
160a6 20 20 70 31 20 3d 20 2d 31 2a 28 70 31 2b 31 29    p1 = -1*(p1+1)
160a7 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
160a8 74 65 33 56 64 62 65 4f 70 33 28 70 56 64 62 65  te3VdbeOp3(pVdbe
160a9 2c 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 2c 20  , OP_TableLock, 
160aa 70 31 2c 20 70 2d 3e 69 54 61 62 2c 20 70 2d 3e  p1, p->iTab, p->
160ab 7a 4e 61 6d 65 2c 20 50 33 5f 53 54 41 54 49 43  zName, P3_STATIC
160ac 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 20  );.  }.}.#else. 
160ad 20 23 64 65 66 69 6e 65 20 63 6f 64 65 54 61 62   #define codeTab
160ae 6c 65 4c 6f 63 6b 73 28 78 29 0a 23 65 6e 64 69  leLocks(x).#endi
160af 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  f../*.** This ro
160b0 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
160b1 61 66 74 65 72 20 61 20 73 69 6e 67 6c 65 20 53  after a single S
160b2 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73  QL statement has
160b3 20 62 65 65 6e 0a 2a 2a 20 70 61 72 73 65 64 20   been.** parsed 
160b4 61 6e 64 20 61 20 56 44 42 45 20 70 72 6f 67 72  and a VDBE progr
160b5 61 6d 20 74 6f 20 65 78 65 63 75 74 65 20 74 68  am to execute th
160b6 61 74 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73  at statement has
160b7 20 62 65 65 6e 0a 2a 2a 20 70 72 65 70 61 72 65   been.** prepare
160b8 64 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  d.  This routine
160b9 20 70 75 74 73 20 74 68 65 20 66 69 6e 69 73 68   puts the finish
160ba 69 6e 67 20 74 6f 75 63 68 65 73 20 6f 6e 20 74  ing touches on t
160bb 68 65 0a 2a 2a 20 56 44 42 45 20 70 72 6f 67 72  he.** VDBE progr
160bc 61 6d 20 61 6e 64 20 72 65 73 65 74 73 20 74 68  am and resets th
160bd 65 20 70 50 61 72 73 65 20 73 74 72 75 63 74 75  e pParse structu
160be 72 65 20 66 6f 72 20 74 68 65 20 6e 65 78 74 0a  re for the next.
160bf 2a 2a 20 70 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20  ** parse..**.** 
160c0 4e 6f 74 65 20 74 68 61 74 20 69 66 20 61 6e 20  Note that if an 
160c1 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 2c 20  error occurred, 
160c2 69 74 20 6d 69 67 68 74 20 62 65 20 74 68 65 20  it might be the 
160c3 63 61 73 65 20 74 68 61 74 0a 2a 2a 20 6e 6f 20  case that.** no 
160c4 56 44 42 45 20 63 6f 64 65 20 77 61 73 20 67 65  VDBE code was ge
160c5 6e 65 72 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49  nerated..*/.SQLI
160c6 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
160c7 73 71 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64  sqlite3FinishCod
160c8 69 6e 67 28 50 61 72 73 65 20 2a 70 50 61 72 73  ing(Parse *pPars
160c9 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  e){.  sqlite3 *d
160ca 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20  b;.  Vdbe *v;.. 
160cb 20 69 66 28 20 73 71 6c 69 74 65 33 4d 61 6c 6c   if( sqlite3Mall
160cc 6f 63 46 61 69 6c 65 64 28 29 20 29 20 72 65 74  ocFailed() ) ret
160cd 75 72 6e 3b 0a 20 20 69 66 28 20 70 50 61 72 73  urn;.  if( pPars
160ce 65 2d 3e 6e 65 73 74 65 64 20 29 20 72 65 74 75  e->nested ) retu
160cf 72 6e 3b 0a 20 20 69 66 28 20 21 70 50 61 72 73  rn;.  if( !pPars
160d0 65 2d 3e 70 56 64 62 65 20 29 7b 0a 20 20 20 20  e->pVdbe ){.    
160d1 69 66 28 20 70 50 61 72 73 65 2d 3e 72 63 3d 3d  if( pParse->rc==
160d2 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61  SQLITE_OK && pPa
160d3 72 73 65 2d 3e 6e 45 72 72 20 29 7b 0a 20 20 20  rse->nErr ){.   
160d4 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20     pParse->rc = 
160d5 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
160d6 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
160d7 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 65 67 69  }.  }..  /* Begi
160d8 6e 20 62 79 20 67 65 6e 65 72 61 74 69 6e 67 20  n by generating 
160d9 73 6f 6d 65 20 74 65 72 6d 69 6e 61 74 69 6f 6e  some termination
160da 20 63 6f 64 65 20 61 74 20 74 68 65 20 65 6e 64   code at the end
160db 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 76 64 62   of the.  ** vdb
160dc 65 20 70 72 6f 67 72 61 6d 0a 20 20 2a 2f 0a 20  e program.  */. 
160dd 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62   db = pParse->db
160de 3b 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  ;.  v = sqlite3G
160df 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
160e0 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 73    if( v ){.    s
160e1 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
160e2 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 30 2c 20 30  v, OP_Halt, 0, 0
160e3 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 63  );..    /* The c
160e4 6f 6f 6b 69 65 20 6d 61 73 6b 20 63 6f 6e 74 61  ookie mask conta
160e5 69 6e 73 20 6f 6e 65 20 62 69 74 20 66 6f 72 20  ins one bit for 
160e6 65 61 63 68 20 64 61 74 61 62 61 73 65 20 66 69  each database fi
160e7 6c 65 20 6f 70 65 6e 2e 0a 20 20 20 20 2a 2a 20  le open..    ** 
160e8 28 42 69 74 20 30 20 69 73 20 66 6f 72 20 6d 61  (Bit 0 is for ma
160e9 69 6e 2c 20 62 69 74 20 31 20 69 73 20 66 6f 72  in, bit 1 is for
160ea 20 74 65 6d 70 2c 20 61 6e 64 20 73 6f 20 66 6f   temp, and so fo
160eb 72 74 68 2e 29 20 20 42 69 74 73 20 61 72 65 0a  rth.)  Bits are.
160ec 20 20 20 20 2a 2a 20 73 65 74 20 66 6f 72 20 65      ** set for e
160ed 61 63 68 20 64 61 74 61 62 61 73 65 20 74 68 61  ach database tha
160ee 74 20 69 73 20 75 73 65 64 2e 20 20 47 65 6e 65  t is used.  Gene
160ef 72 61 74 65 20 63 6f 64 65 20 74 6f 20 73 74 61  rate code to sta
160f0 72 74 20 61 0a 20 20 20 20 2a 2a 20 74 72 61 6e  rt a.    ** tran
160f1 73 61 63 74 69 6f 6e 20 6f 6e 20 65 61 63 68 20  saction on each 
160f2 75 73 65 64 20 64 61 74 61 62 61 73 65 20 61 6e  used database an
160f3 64 20 74 6f 20 76 65 72 69 66 79 20 74 68 65 20  d to verify the 
160f4 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 0a 20 20  schema cookie.  
160f5 20 20 2a 2a 20 6f 6e 20 65 61 63 68 20 75 73 65    ** on each use
160f6 64 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20  d database..    
160f7 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61 72 73  */.    if( pPars
160f8 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 3e 30 20  e->cookieGoto>0 
160f9 29 7b 0a 20 20 20 20 20 20 75 33 32 20 6d 61 73  ){.      u32 mas
160fa 6b 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62  k;.      int iDb
160fb 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
160fc 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 70  dbeJumpHere(v, p
160fd 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74  Parse->cookieGot
160fe 6f 2d 31 29 3b 0a 20 20 20 20 20 20 66 6f 72 28  o-1);.      for(
160ff 69 44 62 3d 30 2c 20 6d 61 73 6b 3d 31 3b 20 69  iDb=0, mask=1; i
16100 44 62 3c 64 62 2d 3e 6e 44 62 3b 20 6d 61 73 6b  Db<db->nDb; mask
16101 3c 3c 3d 31 2c 20 69 44 62 2b 2b 29 7b 0a 20 20  <<=1, iDb++){.  
16102 20 20 20 20 20 20 69 66 28 20 28 6d 61 73 6b 20        if( (mask 
16103 26 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65  & pParse->cookie
16104 4d 61 73 6b 29 3d 3d 30 20 29 20 63 6f 6e 74 69  Mask)==0 ) conti
16105 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  nue;.        sql
16106 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
16107 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c   OP_Transaction,
16108 20 69 44 62 2c 20 28 6d 61 73 6b 20 26 20 70 50   iDb, (mask & pP
16109 61 72 73 65 2d 3e 77 72 69 74 65 4d 61 73 6b 29  arse->writeMask)
1610a 21 3d 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71  !=0);.        sq
1610b 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1610c 2c 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69  , OP_VerifyCooki
1610d 65 2c 20 69 44 62 2c 20 70 50 61 72 73 65 2d 3e  e, iDb, pParse->
1610e 63 6f 6f 6b 69 65 56 61 6c 75 65 5b 69 44 62 5d  cookieValue[iDb]
1610f 29 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e 64  );.      }.#ifnd
16110 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
16111 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20  IRTUALTABLE.    
16112 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 70 56    if( pParse->pV
16113 69 72 74 75 61 6c 4c 6f 63 6b 20 29 7b 0a 20 20  irtualLock ){.  
16114 20 20 20 20 20 20 63 68 61 72 20 2a 76 74 61 62        char *vtab
16115 20 3d 20 28 63 68 61 72 20 2a 29 70 50 61 72 73   = (char *)pPars
16116 65 2d 3e 70 56 69 72 74 75 61 6c 4c 6f 63 6b 2d  e->pVirtualLock-
16117 3e 70 56 74 61 62 3b 0a 20 20 20 20 20 20 20 20  >pVtab;.        
16118 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28 76  sqlite3VdbeOp3(v
16119 2c 20 4f 50 5f 56 42 65 67 69 6e 2c 20 30 2c 20  , OP_VBegin, 0, 
1611a 30 2c 20 76 74 61 62 2c 20 50 33 5f 56 54 41 42  0, vtab, P3_VTAB
1611b 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  );.      }.#endi
1611c 66 0a 0a 20 20 20 20 20 20 2f 2a 20 4f 6e 63 65  f..      /* Once
1611d 20 61 6c 6c 20 74 68 65 20 63 6f 6f 6b 69 65 73   all the cookies
1611e 20 68 61 76 65 20 62 65 65 6e 20 76 65 72 69 66   have been verif
1611f 69 65 64 20 61 6e 64 20 74 72 61 6e 73 61 63 74  ied and transact
16120 69 6f 6e 73 20 6f 70 65 6e 65 64 2c 20 0a 20 20  ions opened, .  
16121 20 20 20 20 2a 2a 20 6f 62 74 61 69 6e 20 74 68      ** obtain th
16122 65 20 72 65 71 75 69 72 65 64 20 74 61 62 6c 65  e required table
16123 2d 6c 6f 63 6b 73 2e 20 54 68 69 73 20 69 73 20  -locks. This is 
16124 61 20 6e 6f 2d 6f 70 20 75 6e 6c 65 73 73 20 74  a no-op unless t
16125 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 73 68 61  he .      ** sha
16126 72 65 64 2d 63 61 63 68 65 20 66 65 61 74 75 72  red-cache featur
16127 65 20 69 73 20 65 6e 61 62 6c 65 64 2e 0a 20 20  e is enabled..  
16128 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 6f 64      */.      cod
16129 65 54 61 62 6c 65 4c 6f 63 6b 73 28 70 50 61 72  eTableLocks(pPar
1612a 73 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  se);.      sqlit
1612b 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1612c 50 5f 47 6f 74 6f 2c 20 30 2c 20 70 50 61 72 73  P_Goto, 0, pPars
1612d 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 29 3b 0a  e->cookieGoto);.
1612e 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53      }..#ifndef S
1612f 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45  QLITE_OMIT_TRACE
16130 0a 20 20 20 20 2f 2a 20 41 64 64 20 61 20 4e 6f  .    /* Add a No
16131 2d 6f 70 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  -op that contain
16132 73 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74  s the complete t
16133 65 78 74 20 6f 66 20 74 68 65 20 63 6f 6d 70 69  ext of the compi
16134 6c 65 64 20 53 51 4c 0a 20 20 20 20 2a 2a 20 73  led SQL.    ** s
16135 74 61 74 65 6d 65 6e 74 20 61 73 20 69 74 73 20  tatement as its 
16136 50 33 20 61 72 67 75 6d 65 6e 74 2e 20 20 54 68  P3 argument.  Th
16137 69 73 20 64 6f 65 73 20 6e 6f 74 20 63 68 61 6e  is does not chan
16138 67 65 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 61  ge the functiona
16139 6c 69 74 79 0a 20 20 20 20 2a 2a 20 6f 66 20 74  lity.    ** of t
1613a 68 65 20 70 72 6f 67 72 61 6d 2e 20 0a 20 20 20  he program. .   
1613b 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 69 73 20   **.    ** This 
1613c 69 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  is used to imple
1613d 6d 65 6e 74 20 73 71 6c 69 74 65 33 5f 74 72 61  ment sqlite3_tra
1613e 63 65 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ce()..    */.   
1613f 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28   sqlite3VdbeOp3(
16140 76 2c 20 4f 50 5f 4e 6f 6f 70 2c 20 30 2c 20 30  v, OP_Noop, 0, 0
16141 2c 20 70 50 61 72 73 65 2d 3e 7a 53 71 6c 2c 20  , pParse->zSql, 
16142 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c 2d 70 50  pParse->zTail-pP
16143 61 72 73 65 2d 3e 7a 53 71 6c 29 3b 0a 23 65 6e  arse->zSql);.#en
16144 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
16145 49 54 5f 54 52 41 43 45 20 2a 2f 0a 20 20 7d 0a  IT_TRACE */.  }.
16146 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68 65 20 56  ..  /* Get the V
16147 44 42 45 20 70 72 6f 67 72 61 6d 20 72 65 61 64  DBE program read
16148 79 20 66 6f 72 20 65 78 65 63 75 74 69 6f 6e 0a  y for execution.
16149 20 20 2a 2f 0a 20 20 69 66 28 20 76 20 26 26 20    */.  if( v && 
1614a 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20  pParse->nErr==0 
1614b 26 26 20 21 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  && !sqlite3Mallo
1614c 63 46 61 69 6c 65 64 28 29 20 29 7b 0a 23 69 66  cFailed() ){.#if
1614d 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
1614e 0a 20 20 20 20 46 49 4c 45 20 2a 74 72 61 63 65  .    FILE *trace
1614f 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20   = (db->flags & 
16150 53 51 4c 49 54 45 5f 56 64 62 65 54 72 61 63 65  SQLITE_VdbeTrace
16151 29 21 3d 30 20 3f 20 73 74 64 6f 75 74 20 3a 20  )!=0 ? stdout : 
16152 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  0;.    sqlite3Vd
16153 62 65 54 72 61 63 65 28 76 2c 20 74 72 61 63 65  beTrace(v, trace
16154 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 73 71  );.#endif.    sq
16155 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61  lite3VdbeMakeRea
16156 64 79 28 76 2c 20 70 50 61 72 73 65 2d 3e 6e 56  dy(v, pParse->nV
16157 61 72 2c 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  ar, pParse->nMem
16158 2b 33 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  +3,.            
16159 20 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61               pPa
1615a 72 73 65 2d 3e 6e 54 61 62 2b 33 2c 20 70 50 61  rse->nTab+3, pPa
1615b 72 73 65 2d 3e 65 78 70 6c 61 69 6e 29 3b 0a 20  rse->explain);. 
1615c 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20     pParse->rc = 
1615d 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20  SQLITE_DONE;.   
1615e 20 70 50 61 72 73 65 2d 3e 63 6f 6c 4e 61 6d 65   pParse->colName
1615f 73 53 65 74 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  sSet = 0;.  }els
16160 65 20 69 66 28 20 70 50 61 72 73 65 2d 3e 72 63  e if( pParse->rc
16161 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
16162 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20     pParse->rc = 
16163 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
16164 7d 0a 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  }.  pParse->nTab
16165 20 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e   = 0;.  pParse->
16166 6e 4d 65 6d 20 3d 20 30 3b 0a 20 20 70 50 61 72  nMem = 0;.  pPar
16167 73 65 2d 3e 6e 53 65 74 20 3d 20 30 3b 0a 20 20  se->nSet = 0;.  
16168 70 50 61 72 73 65 2d 3e 6e 56 61 72 20 3d 20 30  pParse->nVar = 0
16169 3b 0a 20 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b  ;.  pParse->cook
1616a 69 65 4d 61 73 6b 20 3d 20 30 3b 0a 20 20 70 50  ieMask = 0;.  pP
1616b 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f  arse->cookieGoto
1616c 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52   = 0;.}../*.** R
1616d 75 6e 20 74 68 65 20 70 61 72 73 65 72 20 61 6e  un the parser an
1616e 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72  d code generator
1616f 20 72 65 63 75 72 73 69 76 65 6c 79 20 69 6e 20   recursively in 
16170 6f 72 64 65 72 20 74 6f 20 67 65 6e 65 72 61 74  order to generat
16171 65 0a 2a 2a 20 63 6f 64 65 20 66 6f 72 20 74 68  e.** code for th
16172 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  e SQL statement 
16173 67 69 76 65 6e 20 6f 6e 74 6f 20 74 68 65 20 65  given onto the e
16174 6e 64 20 6f 66 20 74 68 65 20 70 50 61 72 73 65  nd of the pParse
16175 20 63 6f 6e 74 65 78 74 0a 2a 2a 20 63 75 72 72   context.** curr
16176 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73  ently under cons
16177 74 72 75 63 74 69 6f 6e 2e 20 20 57 68 65 6e 20  truction.  When 
16178 74 68 65 20 70 61 72 73 65 72 20 69 73 20 72 75  the parser is ru
16179 6e 20 72 65 63 75 72 73 69 76 65 6c 79 0a 2a 2a  n recursively.**
1617a 20 74 68 69 73 20 77 61 79 2c 20 74 68 65 20 66   this way, the f
1617b 69 6e 61 6c 20 4f 50 5f 48 61 6c 74 20 69 73 20  inal OP_Halt is 
1617c 6e 6f 74 20 61 70 70 65 6e 64 65 64 20 61 6e 64  not appended and
1617d 20 6f 74 68 65 72 20 69 6e 69 74 69 61 6c 69 7a   other initializ
1617e 61 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 66 69 6e  ation.** and fin
1617f 61 6c 69 7a 61 74 69 6f 6e 20 73 74 65 70 73 20  alization steps 
16180 61 72 65 20 6f 6d 69 74 74 65 64 20 62 65 63 61  are omitted beca
16181 75 73 65 20 74 68 6f 73 65 20 61 72 65 20 68 61  use those are ha
16182 6e 64 6c 69 6e 67 20 62 79 20 74 68 65 0a 2a 2a  ndling by the.**
16183 20 6f 75 74 65 72 6d 6f 73 74 20 70 61 72 73 65   outermost parse
16184 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 20 65 76 65  r..**.** Not eve
16185 72 79 74 68 69 6e 67 20 69 73 20 6e 65 73 74 61  rything is nesta
16186 62 6c 65 2e 20 20 54 68 69 73 20 66 61 63 69 6c  ble.  This facil
16187 69 74 79 20 69 73 20 64 65 73 69 67 6e 65 64 20  ity is designed 
16188 74 6f 20 70 65 72 6d 69 74 0a 2a 2a 20 49 4e 53  to permit.** INS
16189 45 52 54 2c 20 55 50 44 41 54 45 2c 20 61 6e 64  ERT, UPDATE, and
1618a 20 44 45 4c 45 54 45 20 6f 70 65 72 61 74 69 6f   DELETE operatio
1618b 6e 73 20 61 67 61 69 6e 73 74 20 53 51 4c 49 54  ns against SQLIT
1618c 45 5f 4d 41 53 54 45 52 2e 20 20 55 73 65 0a 2a  E_MASTER.  Use.*
1618d 2a 20 63 61 72 65 20 69 66 20 79 6f 75 20 64 65  * care if you de
1618e 63 69 64 65 20 74 6f 20 74 72 79 20 74 6f 20 75  cide to try to u
1618f 73 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  se this routine 
16190 66 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 70  for some other p
16191 75 72 70 6f 73 65 73 2e 0a 2a 2f 0a 53 51 4c 49  urposes..*/.SQLI
16192 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
16193 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72  sqlite3NestedPar
16194 73 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  se(Parse *pParse
16195 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  , const char *zF
16196 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76  ormat, ...){.  v
16197 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61  a_list ap;.  cha
16198 72 20 2a 7a 53 71 6c 3b 0a 23 20 64 65 66 69 6e  r *zSql;.# defin
16199 65 20 53 41 56 45 5f 53 5a 20 20 28 73 69 7a 65  e SAVE_SZ  (size
1619a 6f 66 28 50 61 72 73 65 29 20 2d 20 6f 66 66 73  of(Parse) - offs
1619b 65 74 6f 66 28 50 61 72 73 65 2c 6e 56 61 72 29  etof(Parse,nVar)
1619c 29 0a 20 20 63 68 61 72 20 73 61 76 65 42 75 66  ).  char saveBuf
1619d 5b 53 41 56 45 5f 53 5a 5d 3b 0a 0a 20 20 69 66  [SAVE_SZ];..  if
1619e 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29  ( pParse->nErr )
1619f 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72   return;.  asser
161a0 74 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65  t( pParse->neste
161a1 64 3c 31 30 20 29 3b 20 20 2f 2a 20 4e 65 73 74  d<10 );  /* Nest
161a2 69 6e 67 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20  ing should only 
161a3 62 65 20 6f 66 20 6c 69 6d 69 74 65 64 20 64 65  be of limited de
161a4 70 74 68 20 2a 2f 0a 20 20 76 61 5f 73 74 61 72  pth */.  va_star
161a5 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a  t(ap, zFormat);.
161a6 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33    zSql = sqlite3
161a7 56 4d 50 72 69 6e 74 66 28 7a 46 6f 72 6d 61 74  VMPrintf(zFormat
161a8 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28  , ap);.  va_end(
161a9 61 70 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c 3d  ap);.  if( zSql=
161aa 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
161ab 3b 20 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63 20  ;   /* A malloc 
161ac 6d 75 73 74 20 68 61 76 65 20 66 61 69 6c 65 64  must have failed
161ad 20 2a 2f 0a 20 20 7d 0a 20 20 70 50 61 72 73 65   */.  }.  pParse
161ae 2d 3e 6e 65 73 74 65 64 2b 2b 3b 0a 20 20 6d 65  ->nested++;.  me
161af 6d 63 70 79 28 73 61 76 65 42 75 66 2c 20 26 70  mcpy(saveBuf, &p
161b0 50 61 72 73 65 2d 3e 6e 56 61 72 2c 20 53 41 56  Parse->nVar, SAV
161b1 45 5f 53 5a 29 3b 0a 20 20 6d 65 6d 73 65 74 28  E_SZ);.  memset(
161b2 26 70 50 61 72 73 65 2d 3e 6e 56 61 72 2c 20 30  &pParse->nVar, 0
161b3 2c 20 53 41 56 45 5f 53 5a 29 3b 0a 20 20 73 71  , SAVE_SZ);.  sq
161b4 6c 69 74 65 33 52 75 6e 50 61 72 73 65 72 28 70  lite3RunParser(p
161b5 50 61 72 73 65 2c 20 7a 53 71 6c 2c 20 30 29 3b  Parse, zSql, 0);
161b6 0a 20 20 73 71 6c 69 74 65 46 72 65 65 28 7a 53  .  sqliteFree(zS
161b7 71 6c 29 3b 0a 20 20 6d 65 6d 63 70 79 28 26 70  ql);.  memcpy(&p
161b8 50 61 72 73 65 2d 3e 6e 56 61 72 2c 20 73 61 76  Parse->nVar, sav
161b9 65 42 75 66 2c 20 53 41 56 45 5f 53 5a 29 3b 0a  eBuf, SAVE_SZ);.
161ba 20 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64    pParse->nested
161bb 2d 2d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63  --;.}../*.** Loc
161bc 61 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72  ate the in-memor
161bd 79 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  y structure that
161be 20 64 65 73 63 72 69 62 65 73 20 61 20 70 61 72   describes a par
161bf 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65  ticular database
161c0 0a 2a 2a 20 74 61 62 6c 65 20 67 69 76 65 6e 20  .** table given 
161c1 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 61 74  the name of that
161c2 20 74 61 62 6c 65 20 61 6e 64 20 28 6f 70 74 69   table and (opti
161c3 6f 6e 61 6c 6c 79 29 20 74 68 65 20 6e 61 6d 65  onally) the name
161c4 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62   of the.** datab
161c5 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ase containing t
161c6 68 65 20 74 61 62 6c 65 2e 20 20 52 65 74 75 72  he table.  Retur
161c7 6e 20 4e 55 4c 4c 20 69 66 20 6e 6f 74 20 66 6f  n NULL if not fo
161c8 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 44  und..**.** If zD
161c9 61 74 61 62 61 73 65 20 69 73 20 30 2c 20 61 6c  atabase is 0, al
161ca 6c 20 64 61 74 61 62 61 73 65 73 20 61 72 65 20  l databases are 
161cb 73 65 61 72 63 68 65 64 20 66 6f 72 20 74 68 65  searched for the
161cc 20 74 61 62 6c 65 20 61 6e 64 20 74 68 65 0a 2a   table and the.*
161cd 2a 20 66 69 72 73 74 20 6d 61 74 63 68 69 6e 67  * first matching
161ce 20 74 61 62 6c 65 20 69 73 20 72 65 74 75 72 6e   table is return
161cf 65 64 2e 20 20 28 4e 6f 20 63 68 65 63 6b 69 6e  ed.  (No checkin
161d0 67 20 66 6f 72 20 64 75 70 6c 69 63 61 74 65 20  g for duplicate 
161d1 74 61 62 6c 65 0a 2a 2a 20 6e 61 6d 65 73 20 69  table.** names i
161d2 73 20 64 6f 6e 65 2e 29 20 20 54 68 65 20 73 65  s done.)  The se
161d3 61 72 63 68 20 6f 72 64 65 72 20 69 73 20 54 45  arch order is TE
161d4 4d 50 20 66 69 72 73 74 2c 20 74 68 65 6e 20 4d  MP first, then M
161d5 41 49 4e 2c 20 74 68 65 6e 20 61 6e 79 0a 2a 2a  AIN, then any.**
161d6 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62   auxiliary datab
161d7 61 73 65 73 20 61 64 64 65 64 20 75 73 69 6e 67  ases added using
161d8 20 74 68 65 20 41 54 54 41 43 48 20 63 6f 6d 6d   the ATTACH comm
161d9 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  and..**.** See a
161da 6c 73 6f 20 73 71 6c 69 74 65 33 4c 6f 63 61 74  lso sqlite3Locat
161db 65 54 61 62 6c 65 28 29 2e 0a 2a 2f 0a 53 51 4c  eTable()..*/.SQL
161dc 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c  ITE_PRIVATE Tabl
161dd 65 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 54 61  e *sqlite3FindTa
161de 62 6c 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ble(sqlite3 *db,
161df 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
161e0 6d 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  me, const char *
161e1 7a 44 61 74 61 62 61 73 65 29 7b 0a 20 20 54 61  zDatabase){.  Ta
161e2 62 6c 65 20 2a 70 20 3d 20 30 3b 0a 20 20 69 6e  ble *p = 0;.  in
161e3 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 7a  t i;.  assert( z
161e4 4e 61 6d 65 21 3d 30 20 29 3b 0a 20 20 66 6f 72  Name!=0 );.  for
161e5 28 69 3d 4f 4d 49 54 5f 54 45 4d 50 44 42 3b 20  (i=OMIT_TEMPDB; 
161e6 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
161e7 0a 20 20 20 20 69 6e 74 20 6a 20 3d 20 28 69 3c  .    int j = (i<
161e8 32 29 20 3f 20 69 5e 31 20 3a 20 69 3b 20 20 20  2) ? i^1 : i;   
161e9 2f 2a 20 53 65 61 72 63 68 20 54 45 4d 50 20 62  /* Search TEMP b
161ea 65 66 6f 72 65 20 4d 41 49 4e 20 2a 2f 0a 20 20  efore MAIN */.  
161eb 20 20 69 66 28 20 7a 44 61 74 61 62 61 73 65 21    if( zDatabase!
161ec 3d 30 20 26 26 20 73 71 6c 69 74 65 33 53 74 72  =0 && sqlite3Str
161ed 49 43 6d 70 28 7a 44 61 74 61 62 61 73 65 2c 20  ICmp(zDatabase, 
161ee 64 62 2d 3e 61 44 62 5b 6a 5d 2e 7a 4e 61 6d 65  db->aDb[j].zName
161ef 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  ) ) continue;.  
161f0 20 20 70 20 3d 20 73 71 6c 69 74 65 33 48 61 73    p = sqlite3Has
161f1 68 46 69 6e 64 28 26 64 62 2d 3e 61 44 62 5b 6a  hFind(&db->aDb[j
161f2 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61  ].pSchema->tblHa
161f3 73 68 2c 20 7a 4e 61 6d 65 2c 20 73 74 72 6c 65  sh, zName, strle
161f4 6e 28 7a 4e 61 6d 65 29 2b 31 29 3b 0a 20 20 20  n(zName)+1);.   
161f5 20 69 66 28 20 70 20 29 20 62 72 65 61 6b 3b 0a   if( p ) break;.
161f6 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a    }.  return p;.
161f7 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20  }../*.** Locate 
161f8 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 74  the in-memory st
161f9 72 75 63 74 75 72 65 20 74 68 61 74 20 64 65 73  ructure that des
161fa 63 72 69 62 65 73 20 61 20 70 61 72 74 69 63 75  cribes a particu
161fb 6c 61 72 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  lar database.** 
161fc 74 61 62 6c 65 20 67 69 76 65 6e 20 74 68 65 20  table given the 
161fd 6e 61 6d 65 20 6f 66 20 74 68 61 74 20 74 61 62  name of that tab
161fe 6c 65 20 61 6e 64 20 28 6f 70 74 69 6f 6e 61 6c  le and (optional
161ff 6c 79 29 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  ly) the name of 
16200 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  the.** database 
16201 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74  containing the t
16202 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 4e 55  able.  Return NU
16203 4c 4c 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e  LL if not found.
16204 20 20 41 6c 73 6f 20 6c 65 61 76 65 20 61 6e 0a    Also leave an.
16205 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  ** error message
16206 20 69 6e 20 70 50 61 72 73 65 2d 3e 7a 45 72 72   in pParse->zErr
16207 4d 73 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64  Msg..**.** The d
16208 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65  ifference betwee
16209 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61  n this routine a
1620a 6e 64 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61  nd sqlite3FindTa
1620b 62 6c 65 28 29 20 69 73 20 74 68 61 74 20 74 68  ble() is that th
1620c 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 6c 65  is.** routine le
1620d 61 76 65 73 20 61 6e 20 65 72 72 6f 72 20 6d 65  aves an error me
1620e 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 2d  ssage in pParse-
1620f 3e 7a 45 72 72 4d 73 67 20 77 68 65 72 65 0a 2a  >zErrMsg where.*
16210 2a 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62  * sqlite3FindTab
16211 6c 65 28 29 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a  le() does not..*
16212 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
16213 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 4c   Table *sqlite3L
16214 6f 63 61 74 65 54 61 62 6c 65 28 50 61 72 73 65  ocateTable(Parse
16215 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20   *pParse, const 
16216 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 63 6f 6e  char *zName, con
16217 73 74 20 63 68 61 72 20 2a 7a 44 62 61 73 65 29  st char *zDbase)
16218 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 0a 20  {.  Table *p;.. 
16219 20 2f 2a 20 52 65 61 64 20 74 68 65 20 64 61 74   /* Read the dat
1621a 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20 49 66  abase schema. If
1621b 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
1621c 2c 20 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72  , leave an error
1621d 20 6d 65 73 73 61 67 65 0a 20 20 2a 2a 20 61 6e   message.  ** an
1621e 64 20 63 6f 64 65 20 69 6e 20 70 50 61 72 73 65  d code in pParse
1621f 20 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c   and return NULL
16220 2e 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54  . */.  if( SQLIT
16221 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61  E_OK!=sqlite3Rea
16222 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20  dSchema(pParse) 
16223 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
16224 0a 20 20 7d 0a 0a 20 20 70 20 3d 20 73 71 6c 69  .  }..  p = sqli
16225 74 65 33 46 69 6e 64 54 61 62 6c 65 28 70 50 61  te3FindTable(pPa
16226 72 73 65 2d 3e 64 62 2c 20 7a 4e 61 6d 65 2c 20  rse->db, zName, 
16227 7a 44 62 61 73 65 29 3b 0a 20 20 69 66 28 20 70  zDbase);.  if( p
16228 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 7a  ==0 ){.    if( z
16229 44 62 61 73 65 20 29 7b 0a 20 20 20 20 20 20 73  Dbase ){.      s
1622a 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
1622b 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20  Parse, "no such 
1622c 74 61 62 6c 65 3a 20 25 73 2e 25 73 22 2c 20 7a  table: %s.%s", z
1622d 44 62 61 73 65 2c 20 7a 4e 61 6d 65 29 3b 0a 20  Dbase, zName);. 
1622e 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1622f 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
16230 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68  pParse, "no such
16231 20 74 61 62 6c 65 3a 20 25 73 22 2c 20 7a 4e 61   table: %s", zNa
16232 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  me);.    }.    p
16233 50 61 72 73 65 2d 3e 63 68 65 63 6b 53 63 68 65  Parse->checkSche
16234 6d 61 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65  ma = 1;.  }.  re
16235 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn p;.}../*.**
16236 20 4c 6f 63 61 74 65 20 74 68 65 20 69 6e 2d 6d   Locate the in-m
16237 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65 20  emory structure 
16238 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 0a  that describes .
16239 2a 2a 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  ** a particular 
1623a 69 6e 64 65 78 20 67 69 76 65 6e 20 74 68 65 20  index given the 
1623b 6e 61 6d 65 20 6f 66 20 74 68 61 74 20 69 6e 64  name of that ind
1623c 65 78 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6e 61  ex.** and the na
1623d 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  me of the databa
1623e 73 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  se that contains
1623f 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a 2a 20 52   the index..** R
16240 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 6e 6f  eturn NULL if no
16241 74 20 66 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20 49  t found..**.** I
16242 66 20 7a 44 61 74 61 62 61 73 65 20 69 73 20 30  f zDatabase is 0
16243 2c 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73 20  , all databases 
16244 61 72 65 20 73 65 61 72 63 68 65 64 20 66 6f 72  are searched for
16245 20 74 68 65 0a 2a 2a 20 74 61 62 6c 65 20 61 6e   the.** table an
16246 64 20 74 68 65 20 66 69 72 73 74 20 6d 61 74 63  d the first matc
16247 68 69 6e 67 20 69 6e 64 65 78 20 69 73 20 72 65  hing index is re
16248 74 75 72 6e 65 64 2e 20 20 28 4e 6f 20 63 68 65  turned.  (No che
16249 63 6b 69 6e 67 0a 2a 2a 20 66 6f 72 20 64 75 70  cking.** for dup
1624a 6c 69 63 61 74 65 20 69 6e 64 65 78 20 6e 61 6d  licate index nam
1624b 65 73 20 69 73 20 64 6f 6e 65 2e 29 20 20 54 68  es is done.)  Th
1624c 65 20 73 65 61 72 63 68 20 6f 72 64 65 72 20 69  e search order i
1624d 73 0a 2a 2a 20 54 45 4d 50 20 66 69 72 73 74 2c  s.** TEMP first,
1624e 20 74 68 65 6e 20 4d 41 49 4e 2c 20 74 68 65 6e   then MAIN, then
1624f 20 61 6e 79 20 61 75 78 69 6c 69 61 72 79 20 64   any auxiliary d
16250 61 74 61 62 61 73 65 73 20 61 64 64 65 64 0a 2a  atabases added.*
16251 2a 20 75 73 69 6e 67 20 74 68 65 20 41 54 54 41  * using the ATTA
16252 43 48 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a 53  CH command..*/.S
16253 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 6e  QLITE_PRIVATE In
16254 64 65 78 20 2a 73 71 6c 69 74 65 33 46 69 6e 64  dex *sqlite3Find
16255 49 6e 64 65 78 28 73 71 6c 69 74 65 33 20 2a 64  Index(sqlite3 *d
16256 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
16257 4e 61 6d 65 2c 20 63 6f 6e 73 74 20 63 68 61 72  Name, const char
16258 20 2a 7a 44 62 29 7b 0a 20 20 49 6e 64 65 78 20   *zDb){.  Index 
16259 2a 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b  *p = 0;.  int i;
1625a 0a 20 20 66 6f 72 28 69 3d 4f 4d 49 54 5f 54 45  .  for(i=OMIT_TE
1625b 4d 50 44 42 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b  MPDB; i<db->nDb;
1625c 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6a   i++){.    int j
1625d 20 3d 20 28 69 3c 32 29 20 3f 20 69 5e 31 20 3a   = (i<2) ? i^1 :
1625e 20 69 3b 20 20 2f 2a 20 53 65 61 72 63 68 20 54   i;  /* Search T
1625f 45 4d 50 20 62 65 66 6f 72 65 20 4d 41 49 4e 20  EMP before MAIN 
16260 2a 2f 0a 20 20 20 20 53 63 68 65 6d 61 20 2a 70  */.    Schema *p
16261 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62  Schema = db->aDb
16262 5b 6a 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20 20  [j].pSchema;.   
16263 20 69 66 28 20 7a 44 62 20 26 26 20 73 71 6c 69   if( zDb && sqli
16264 74 65 33 53 74 72 49 43 6d 70 28 7a 44 62 2c 20  te3StrICmp(zDb, 
16265 64 62 2d 3e 61 44 62 5b 6a 5d 2e 7a 4e 61 6d 65  db->aDb[j].zName
16266 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  ) ) continue;.  
16267 20 20 61 73 73 65 72 74 28 20 70 53 63 68 65 6d    assert( pSchem
16268 61 20 7c 7c 20 28 6a 3d 3d 31 20 26 26 20 21 64  a || (j==1 && !d
16269 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 29 20 29  b->aDb[1].pBt) )
1626a 3b 0a 20 20 20 20 69 66 28 20 70 53 63 68 65 6d  ;.    if( pSchem
1626b 61 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 73  a ){.      p = s
1626c 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 26  qlite3HashFind(&
1626d 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68  pSchema->idxHash
1626e 2c 20 7a 4e 61 6d 65 2c 20 73 74 72 6c 65 6e 28  , zName, strlen(
1626f 7a 4e 61 6d 65 29 2b 31 29 3b 0a 20 20 20 20 7d  zName)+1);.    }
16270 0a 20 20 20 20 69 66 28 20 70 20 29 20 62 72 65  .    if( p ) bre
16271 61 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ak;.  }.  return
16272 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63   p;.}../*.** Rec
16273 6c 61 69 6d 20 74 68 65 20 6d 65 6d 6f 72 79 20  laim the memory 
16274 75 73 65 64 20 62 79 20 61 6e 20 69 6e 64 65 78  used by an index
16275 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
16276 66 72 65 65 49 6e 64 65 78 28 49 6e 64 65 78 20  freeIndex(Index 
16277 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 46 72 65  *p){.  sqliteFre
16278 65 28 70 2d 3e 7a 43 6f 6c 41 66 66 29 3b 0a 20  e(p->zColAff);. 
16279 20 73 71 6c 69 74 65 46 72 65 65 28 70 29 3b 0a   sqliteFree(p);.
1627a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20  }../*.** Remove 
1627b 74 68 65 20 67 69 76 65 6e 20 69 6e 64 65 78 20  the given index 
1627c 66 72 6f 6d 20 74 68 65 20 69 6e 64 65 78 20 68  from the index h
1627d 61 73 68 20 74 61 62 6c 65 2c 20 61 6e 64 20 66  ash table, and f
1627e 72 65 65 0a 2a 2a 20 69 74 73 20 6d 65 6d 6f 72  ree.** its memor
1627f 79 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2a  y structures..**
16280 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 69 73  .** The index is
16281 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68   removed from th
16282 65 20 64 61 74 61 62 61 73 65 20 68 61 73 68 20  e database hash 
16283 74 61 62 6c 65 73 20 62 75 74 0a 2a 2a 20 69 74  tables but.** it
16284 20 69 73 20 6e 6f 74 20 75 6e 6c 69 6e 6b 65 64   is not unlinked
16285 20 66 72 6f 6d 20 74 68 65 20 54 61 62 6c 65 20   from the Table 
16286 74 68 61 74 20 69 74 20 69 6e 64 65 78 65 73 2e  that it indexes.
16287 0a 2a 2a 20 55 6e 6c 69 6e 6b 69 6e 67 20 66 72  .** Unlinking fr
16288 6f 6d 20 74 68 65 20 54 61 62 6c 65 20 6d 75 73  om the Table mus
16289 74 20 62 65 20 64 6f 6e 65 20 62 79 20 74 68 65  t be done by the
1628a 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f   calling functio
1628b 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  n..*/.static voi
1628c 64 20 73 71 6c 69 74 65 44 65 6c 65 74 65 49 6e  d sqliteDeleteIn
1628d 64 65 78 28 49 6e 64 65 78 20 2a 70 29 7b 0a 20  dex(Index *p){. 
1628e 20 49 6e 64 65 78 20 2a 70 4f 6c 64 3b 0a 20 20   Index *pOld;.  
1628f 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
16290 65 20 3d 20 70 2d 3e 7a 4e 61 6d 65 3b 0a 0a 20  e = p->zName;.. 
16291 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74 65 33 48   pOld = sqlite3H
16292 61 73 68 49 6e 73 65 72 74 28 26 70 2d 3e 70 53  ashInsert(&p->pS
16293 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 2c 20  chema->idxHash, 
16294 7a 4e 61 6d 65 2c 20 73 74 72 6c 65 6e 28 20 7a  zName, strlen( z
16295 4e 61 6d 65 29 2b 31 2c 20 30 29 3b 0a 20 20 61  Name)+1, 0);.  a
16296 73 73 65 72 74 28 20 70 4f 6c 64 3d 3d 30 20 7c  ssert( pOld==0 |
16297 7c 20 70 4f 6c 64 3d 3d 70 20 29 3b 0a 20 20 66  | pOld==p );.  f
16298 72 65 65 49 6e 64 65 78 28 70 29 3b 0a 7d 0a 0a  reeIndex(p);.}..
16299 2f 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 69 6e  /*.** For the in
1629a 64 65 78 20 63 61 6c 6c 65 64 20 7a 49 64 78 4e  dex called zIdxN
1629b 61 6d 65 20 77 68 69 63 68 20 69 73 20 66 6f 75  ame which is fou
1629c 6e 64 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  nd in the databa
1629d 73 65 20 69 44 62 2c 0a 2a 2a 20 75 6e 6c 69 6b  se iDb,.** unlik
1629e 65 20 74 68 61 74 20 69 6e 64 65 78 20 66 72 6f  e that index fro
1629f 6d 20 69 74 73 20 54 61 62 6c 65 20 74 68 65 6e  m its Table then
162a0 20 72 65 6d 6f 76 65 20 74 68 65 20 69 6e 64 65   remove the inde
162a1 78 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 69 6e  x from.** the in
162a2 64 65 78 20 68 61 73 68 20 74 61 62 6c 65 20 61  dex hash table a
162a3 6e 64 20 66 72 65 65 20 61 6c 6c 20 6d 65 6d 6f  nd free all memo
162a4 72 79 20 73 74 72 75 63 74 75 72 65 73 20 61 73  ry structures as
162a5 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68  sociated.** with
162a6 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a 2f 0a 53   the index..*/.S
162a7 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
162a8 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b  id sqlite3Unlink
162a9 41 6e 64 44 65 6c 65 74 65 49 6e 64 65 78 28 73  AndDeleteIndex(s
162aa 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
162ab 69 44 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  iDb, const char 
162ac 2a 7a 49 64 78 4e 61 6d 65 29 7b 0a 20 20 49 6e  *zIdxName){.  In
162ad 64 65 78 20 2a 70 49 6e 64 65 78 3b 0a 20 20 69  dex *pIndex;.  i
162ae 6e 74 20 6c 65 6e 3b 0a 20 20 48 61 73 68 20 2a  nt len;.  Hash *
162af 70 48 61 73 68 20 3d 20 26 64 62 2d 3e 61 44 62  pHash = &db->aDb
162b0 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 69  [iDb].pSchema->i
162b1 64 78 48 61 73 68 3b 0a 0a 20 20 6c 65 6e 20 3d  dxHash;..  len =
162b2 20 73 74 72 6c 65 6e 28 7a 49 64 78 4e 61 6d 65   strlen(zIdxName
162b3 29 3b 0a 20 20 70 49 6e 64 65 78 20 3d 20 73 71  );.  pIndex = sq
162b4 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28  lite3HashInsert(
162b5 70 48 61 73 68 2c 20 7a 49 64 78 4e 61 6d 65 2c  pHash, zIdxName,
162b6 20 6c 65 6e 2b 31 2c 20 30 29 3b 0a 20 20 69 66   len+1, 0);.  if
162b7 28 20 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20  ( pIndex ){.    
162b8 69 66 28 20 70 49 6e 64 65 78 2d 3e 70 54 61 62  if( pIndex->pTab
162b9 6c 65 2d 3e 70 49 6e 64 65 78 3d 3d 70 49 6e 64  le->pIndex==pInd
162ba 65 78 20 29 7b 0a 20 20 20 20 20 20 70 49 6e 64  ex ){.      pInd
162bb 65 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e 64  ex->pTable->pInd
162bc 65 78 20 3d 20 70 49 6e 64 65 78 2d 3e 70 4e 65  ex = pIndex->pNe
162bd 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  xt;.    }else{. 
162be 20 20 20 20 20 49 6e 64 65 78 20 2a 70 3b 0a 20       Index *p;. 
162bf 20 20 20 20 20 66 6f 72 28 70 3d 70 49 6e 64 65       for(p=pInde
162c0 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65  x->pTable->pInde
162c1 78 3b 20 70 20 26 26 20 70 2d 3e 70 4e 65 78 74  x; p && p->pNext
162c2 21 3d 70 49 6e 64 65 78 3b 20 70 3d 70 2d 3e 70  !=pIndex; p=p->p
162c3 4e 65 78 74 29 7b 7d 0a 20 20 20 20 20 20 69 66  Next){}.      if
162c4 28 20 70 20 26 26 20 70 2d 3e 70 4e 65 78 74 3d  ( p && p->pNext=
162c5 3d 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 20  =pIndex ){.     
162c6 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 49     p->pNext = pI
162c7 6e 64 65 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  ndex->pNext;.   
162c8 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66     }.    }.    f
162c9 72 65 65 49 6e 64 65 78 28 70 49 6e 64 65 78 29  reeIndex(pIndex)
162ca 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 66 6c 61 67  ;.  }.  db->flag
162cb 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65  s |= SQLITE_Inte
162cc 72 6e 43 68 61 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a  rnChanges;.}../*
162cd 0a 2a 2a 20 45 72 61 73 65 20 61 6c 6c 20 73 63  .** Erase all sc
162ce 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  hema information
162cf 20 66 72 6f 6d 20 74 68 65 20 69 6e 2d 6d 65 6d   from the in-mem
162d0 6f 72 79 20 68 61 73 68 20 74 61 62 6c 65 73 20  ory hash tables 
162d1 6f 66 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 64  of.** a single d
162d2 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 72  atabase.  This r
162d3 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
162d4 20 74 6f 20 72 65 63 6c 61 69 6d 20 6d 65 6d 6f   to reclaim memo
162d5 72 79 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 65  ry.** before the
162d6 20 64 61 74 61 62 61 73 65 20 63 6c 6f 73 65 73   database closes
162d7 2e 20 20 49 74 20 69 73 20 61 6c 73 6f 20 63 61  .  It is also ca
162d8 6c 6c 65 64 20 64 75 72 69 6e 67 20 61 20 72 6f  lled during a ro
162d9 6c 6c 62 61 63 6b 0a 2a 2a 20 69 66 20 74 68 65  llback.** if the
162da 72 65 20 77 65 72 65 20 73 63 68 65 6d 61 20 63  re were schema c
162db 68 61 6e 67 65 73 20 64 75 72 69 6e 67 20 74 68  hanges during th
162dc 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 72  e transaction or
162dd 20 69 66 20 61 0a 2a 2a 20 73 63 68 65 6d 61 2d   if a.** schema-
162de 63 6f 6f 6b 69 65 20 6d 69 73 6d 61 74 63 68 20  cookie mismatch 
162df 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66  occurs..**.** If
162e0 20 69 44 62 3c 3d 30 20 74 68 65 6e 20 72 65 73   iDb<=0 then res
162e1 65 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  et the internal 
162e2 73 63 68 65 6d 61 20 74 61 62 6c 65 73 20 66 6f  schema tables fo
162e3 72 20 61 6c 6c 20 64 61 74 61 62 61 73 65 0a 2a  r all database.*
162e4 2a 20 66 69 6c 65 73 2e 20 20 49 66 20 69 44 62  * files.  If iDb
162e5 3e 3d 32 20 74 68 65 6e 20 72 65 73 65 74 20 74  >=2 then reset t
162e6 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65  he internal sche
162e7 6d 61 20 66 6f 72 20 6f 6e 6c 79 20 74 68 65 0a  ma for only the.
162e8 2a 2a 20 73 69 6e 67 6c 65 20 66 69 6c 65 20 69  ** single file i
162e9 6e 64 69 63 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c  ndicated..*/.SQL
162ea 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
162eb 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74   sqlite3ResetInt
162ec 65 72 6e 61 6c 53 63 68 65 6d 61 28 73 71 6c 69  ernalSchema(sqli
162ed 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62  te3 *db, int iDb
162ee 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 0a  ){.  int i, j;..
162ef 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30    assert( iDb>=0
162f0 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20   && iDb<db->nDb 
162f1 29 3b 0a 20 20 66 6f 72 28 69 3d 69 44 62 3b 20  );.  for(i=iDb; 
162f2 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
162f3 0a 20 20 20 20 44 62 20 2a 70 44 62 20 3d 20 26  .    Db *pDb = &
162f4 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20  db->aDb[i];.    
162f5 69 66 28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61  if( pDb->pSchema
162f6 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
162f7 33 53 63 68 65 6d 61 46 72 65 65 28 70 44 62 2d  3SchemaFree(pDb-
162f8 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 7d  >pSchema);.    }
162f9 0a 20 20 20 20 69 66 28 20 69 44 62 3e 30 20 29  .    if( iDb>0 )
162fa 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61   return;.  }.  a
162fb 73 73 65 72 74 28 20 69 44 62 3d 3d 30 20 29 3b  ssert( iDb==0 );
162fc 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 26 3d 20  .  db->flags &= 
162fd 7e 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68  ~SQLITE_InternCh
162fe 61 6e 67 65 73 3b 0a 0a 20 20 2f 2a 20 49 66 20  anges;..  /* If 
162ff 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74  one or more of t
16300 68 65 20 61 75 78 69 6c 69 61 72 79 20 64 61 74  he auxiliary dat
16301 61 62 61 73 65 20 66 69 6c 65 73 20 68 61 73 20  abase files has 
16302 62 65 65 6e 20 63 6c 6f 73 65 64 2c 0a 20 20 2a  been closed,.  *
16303 2a 20 74 68 65 6e 20 72 65 6d 6f 76 65 20 74 68  * then remove th
16304 65 6d 20 66 72 6f 6d 20 74 68 65 20 61 75 78 69  em from the auxi
16305 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20 6c  liary database l
16306 69 73 74 2e 20 20 57 65 20 74 61 6b 65 20 74 68  ist.  We take th
16307 65 0a 20 20 2a 2a 20 6f 70 70 6f 72 74 75 6e 69  e.  ** opportuni
16308 74 79 20 74 6f 20 64 6f 20 74 68 69 73 20 68 65  ty to do this he
16309 72 65 20 73 69 6e 63 65 20 77 65 20 68 61 76 65  re since we have
1630a 20 6a 75 73 74 20 64 65 6c 65 74 65 64 20 61 6c   just deleted al
1630b 6c 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 73 63  l of the.  ** sc
1630c 68 65 6d 61 20 68 61 73 68 20 74 61 62 6c 65 73  hema hash tables
1630d 20 61 6e 64 20 74 68 65 72 65 66 6f 72 65 20 64   and therefore d
1630e 6f 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 6d 61  o not have to ma
1630f 6b 65 20 61 6e 79 20 63 68 61 6e 67 65 73 0a 20  ke any changes. 
16310 20 2a 2a 20 74 6f 20 61 6e 79 20 6f 66 20 74 68   ** to any of th
16311 6f 73 65 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f  ose tables..  */
16312 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62  .  for(i=0; i<db
16313 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nDb; i++){.   
16314 20 73 74 72 75 63 74 20 44 62 20 2a 70 44 62 20   struct Db *pDb 
16315 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20  = &db->aDb[i];. 
16316 20 20 20 69 66 28 20 70 44 62 2d 3e 70 42 74 3d     if( pDb->pBt=
16317 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  =0 ){.      if( 
16318 70 44 62 2d 3e 70 41 75 78 20 26 26 20 70 44 62  pDb->pAux && pDb
16319 2d 3e 78 46 72 65 65 41 75 78 20 29 20 70 44 62  ->xFreeAux ) pDb
1631a 2d 3e 78 46 72 65 65 41 75 78 28 70 44 62 2d 3e  ->xFreeAux(pDb->
1631b 70 41 75 78 29 3b 0a 20 20 20 20 20 20 70 44 62  pAux);.      pDb
1631c 2d 3e 70 41 75 78 20 3d 20 30 3b 0a 20 20 20 20  ->pAux = 0;.    
1631d 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 6a 3d  }.  }.  for(i=j=
1631e 32 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  2; i<db->nDb; i+
1631f 2b 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 44  +){.    struct D
16320 62 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44  b *pDb = &db->aD
16321 62 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 44  b[i];.    if( pD
16322 62 2d 3e 70 42 74 3d 3d 30 20 29 7b 0a 20 20 20  b->pBt==0 ){.   
16323 20 20 20 73 71 6c 69 74 65 46 72 65 65 28 70 44     sqliteFree(pD
16324 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  b->zName);.     
16325 20 70 44 62 2d 3e 7a 4e 61 6d 65 20 3d 20 30 3b   pDb->zName = 0;
16326 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b  .      continue;
16327 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6a  .    }.    if( j
16328 3c 69 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e  <i ){.      db->
16329 61 44 62 5b 6a 5d 20 3d 20 64 62 2d 3e 61 44 62  aDb[j] = db->aDb
1632a 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6a  [i];.    }.    j
1632b 2b 2b 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74  ++;.  }.  memset
1632c 28 26 64 62 2d 3e 61 44 62 5b 6a 5d 2c 20 30 2c  (&db->aDb[j], 0,
1632d 20 28 64 62 2d 3e 6e 44 62 2d 6a 29 2a 73 69 7a   (db->nDb-j)*siz
1632e 65 6f 66 28 64 62 2d 3e 61 44 62 5b 6a 5d 29 29  eof(db->aDb[j]))
1632f 3b 0a 20 20 64 62 2d 3e 6e 44 62 20 3d 20 6a 3b  ;.  db->nDb = j;
16330 0a 20 20 69 66 28 20 64 62 2d 3e 6e 44 62 3c 3d  .  if( db->nDb<=
16331 32 20 26 26 20 64 62 2d 3e 61 44 62 21 3d 64 62  2 && db->aDb!=db
16332 2d 3e 61 44 62 53 74 61 74 69 63 20 29 7b 0a 20  ->aDbStatic ){. 
16333 20 20 20 6d 65 6d 63 70 79 28 64 62 2d 3e 61 44     memcpy(db->aD
16334 62 53 74 61 74 69 63 2c 20 64 62 2d 3e 61 44 62  bStatic, db->aDb
16335 2c 20 32 2a 73 69 7a 65 6f 66 28 64 62 2d 3e 61  , 2*sizeof(db->a
16336 44 62 5b 30 5d 29 29 3b 0a 20 20 20 20 73 71 6c  Db[0]));.    sql
16337 69 74 65 46 72 65 65 28 64 62 2d 3e 61 44 62 29  iteFree(db->aDb)
16338 3b 0a 20 20 20 20 64 62 2d 3e 61 44 62 20 3d 20  ;.    db->aDb = 
16339 64 62 2d 3e 61 44 62 53 74 61 74 69 63 3b 0a 20  db->aDbStatic;. 
1633a 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73   }.}../*.** This
1633b 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
1633c 65 64 20 77 68 65 6e 20 61 20 63 6f 6d 6d 69 74  ed when a commit
1633d 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53 51 4c 49   occurs..*/.SQLI
1633e 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1633f 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 49 6e 74  sqlite3CommitInt
16340 65 72 6e 61 6c 43 68 61 6e 67 65 73 28 73 71 6c  ernalChanges(sql
16341 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 64 62 2d  ite3 *db){.  db-
16342 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49 54  >flags &= ~SQLIT
16343 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b  E_InternChanges;
16344 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20  .}../*.** Clear 
16345 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  the column names
16346 20 66 72 6f 6d 20 61 20 74 61 62 6c 65 20 6f 72   from a table or
16347 20 76 69 65 77 2e 0a 2a 2f 0a 73 74 61 74 69 63   view..*/.static
16348 20 76 6f 69 64 20 73 71 6c 69 74 65 52 65 73 65   void sqliteRese
16349 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 54 61 62  tColumnNames(Tab
1634a 6c 65 20 2a 70 54 61 62 6c 65 29 7b 0a 20 20 69  le *pTable){.  i
1634b 6e 74 20 69 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a  nt i;.  Column *
1634c 70 43 6f 6c 3b 0a 20 20 61 73 73 65 72 74 28 20  pCol;.  assert( 
1634d 70 54 61 62 6c 65 21 3d 30 20 29 3b 0a 20 20 69  pTable!=0 );.  i
1634e 66 28 20 28 70 43 6f 6c 20 3d 20 70 54 61 62 6c  f( (pCol = pTabl
1634f 65 2d 3e 61 43 6f 6c 29 21 3d 30 20 29 7b 0a 20  e->aCol)!=0 ){. 
16350 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54     for(i=0; i<pT
16351 61 62 6c 65 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c  able->nCol; i++,
16352 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20   pCol++){.      
16353 73 71 6c 69 74 65 46 72 65 65 28 70 43 6f 6c 2d  sqliteFree(pCol-
16354 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 73  >zName);.      s
16355 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
16356 28 70 43 6f 6c 2d 3e 70 44 66 6c 74 29 3b 0a 20  (pCol->pDflt);. 
16357 20 20 20 20 20 73 71 6c 69 74 65 46 72 65 65 28       sqliteFree(
16358 70 43 6f 6c 2d 3e 7a 54 79 70 65 29 3b 0a 20 20  pCol->zType);.  
16359 20 20 20 20 73 71 6c 69 74 65 46 72 65 65 28 70      sqliteFree(p
1635a 43 6f 6c 2d 3e 7a 43 6f 6c 6c 29 3b 0a 20 20 20  Col->zColl);.   
1635b 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 46 72 65   }.    sqliteFre
1635c 65 28 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 29 3b  e(pTable->aCol);
1635d 0a 20 20 7d 0a 20 20 70 54 61 62 6c 65 2d 3e 61  .  }.  pTable->a
1635e 43 6f 6c 20 3d 20 30 3b 0a 20 20 70 54 61 62 6c  Col = 0;.  pTabl
1635f 65 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 7d 0a 0a  e->nCol = 0;.}..
16360 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65  /*.** Remove the
16361 20 6d 65 6d 6f 72 79 20 64 61 74 61 20 73 74 72   memory data str
16362 75 63 74 75 72 65 73 20 61 73 73 6f 63 69 61 74  uctures associat
16363 65 64 20 77 69 74 68 20 74 68 65 20 67 69 76 65  ed with the give
16364 6e 0a 2a 2a 20 54 61 62 6c 65 2e 20 20 4e 6f 20  n.** Table.  No 
16365 63 68 61 6e 67 65 73 20 61 72 65 20 6d 61 64 65  changes are made
16366 20 74 6f 20 64 69 73 6b 20 62 79 20 74 68 69 73   to disk by this
16367 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20   routine..**.** 
16368 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6a 75 73  This routine jus
16369 74 20 64 65 6c 65 74 65 73 20 74 68 65 20 64 61  t deletes the da
1636a 74 61 20 73 74 72 75 63 74 75 72 65 2e 20 20 49  ta structure.  I
1636b 74 20 64 6f 65 73 20 6e 6f 74 20 75 6e 6c 69 6e  t does not unlin
1636c 6b 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 64  k.** the table d
1636d 61 74 61 20 73 74 72 75 63 74 75 72 65 20 66 72  ata structure fr
1636e 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  om the hash tabl
1636f 65 2e 20 20 4e 6f 72 20 64 6f 65 73 20 69 74 20  e.  Nor does it 
16370 72 65 6d 6f 76 65 0a 2a 2a 20 66 6f 72 65 69 67  remove.** foreig
16371 6e 20 6b 65 79 73 20 66 72 6f 6d 20 74 68 65 20  n keys from the 
16372 73 71 6c 69 74 65 2e 61 46 4b 65 79 20 68 61 73  sqlite.aFKey has
16373 68 20 74 61 62 6c 65 2e 20 20 42 75 74 20 69 74  h table.  But it
16374 20 64 6f 65 73 20 64 65 73 74 72 6f 79 0a 2a 2a   does destroy.**
16375 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72   memory structur
16376 65 73 20 6f 66 20 74 68 65 20 69 6e 64 69 63 65  es of the indice
16377 73 20 61 6e 64 20 66 6f 72 65 69 67 6e 20 6b 65  s and foreign ke
16378 79 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ys associated wi
16379 74 68 20 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65  th .** the table
1637a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1637b 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1637c 44 65 6c 65 74 65 54 61 62 6c 65 28 54 61 62 6c  DeleteTable(Tabl
1637d 65 20 2a 70 54 61 62 6c 65 29 7b 0a 20 20 49 6e  e *pTable){.  In
1637e 64 65 78 20 2a 70 49 6e 64 65 78 2c 20 2a 70 4e  dex *pIndex, *pN
1637f 65 78 74 3b 0a 20 20 46 4b 65 79 20 2a 70 46 4b  ext;.  FKey *pFK
16380 65 79 2c 20 2a 70 4e 65 78 74 46 4b 65 79 3b 0a  ey, *pNextFKey;.
16381 0a 20 20 69 66 28 20 70 54 61 62 6c 65 3d 3d 30  .  if( pTable==0
16382 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a   ) return;..  /*
16383 20 44 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 74   Do not delete t
16384 68 65 20 74 61 62 6c 65 20 75 6e 74 69 6c 20 74  he table until t
16385 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75  he reference cou
16386 6e 74 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2e  nt reaches zero.
16387 20 2a 2f 0a 20 20 70 54 61 62 6c 65 2d 3e 6e 52   */.  pTable->nR
16388 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 54 61 62  ef--;.  if( pTab
16389 6c 65 2d 3e 6e 52 65 66 3e 30 20 29 7b 0a 20 20  le->nRef>0 ){.  
1638a 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
1638b 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 2d 3e  assert( pTable->
1638c 6e 52 65 66 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a  nRef==0 );..  /*
1638d 20 44 65 6c 65 74 65 20 61 6c 6c 20 69 6e 64 69   Delete all indi
1638e 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ces associated w
1638f 69 74 68 20 74 68 69 73 20 74 61 62 6c 65 0a 20  ith this table. 
16390 20 2a 2f 0a 20 20 66 6f 72 28 70 49 6e 64 65 78   */.  for(pIndex
16391 20 3d 20 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65   = pTable->pInde
16392 78 3b 20 70 49 6e 64 65 78 3b 20 70 49 6e 64 65  x; pIndex; pInde
16393 78 3d 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 4e  x=pNext){.    pN
16394 65 78 74 20 3d 20 70 49 6e 64 65 78 2d 3e 70 4e  ext = pIndex->pN
16395 65 78 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ext;.    assert(
16396 20 70 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61   pIndex->pSchema
16397 3d 3d 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d  ==pTable->pSchem
16398 61 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 44  a );.    sqliteD
16399 65 6c 65 74 65 49 6e 64 65 78 28 70 49 6e 64 65  eleteIndex(pInde
1639a 78 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66  x);.  }..#ifndef
1639b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52   SQLITE_OMIT_FOR
1639c 45 49 47 4e 5f 4b 45 59 0a 20 20 2f 2a 20 44 65  EIGN_KEY.  /* De
1639d 6c 65 74 65 20 61 6c 6c 20 66 6f 72 65 69 67 6e  lete all foreign
1639e 20 6b 65 79 73 20 61 73 73 6f 63 69 61 74 65 64   keys associated
1639f 20 77 69 74 68 20 74 68 69 73 20 74 61 62 6c 65   with this table
163a0 2e 20 20 54 68 65 20 6b 65 79 73 0a 20 20 2a 2a  .  The keys.  **
163a1 20 73 68 6f 75 6c 64 20 68 61 76 65 20 61 6c 72   should have alr
163a2 65 61 64 79 20 62 65 65 6e 20 75 6e 6c 69 6e 6b  eady been unlink
163a3 65 64 20 66 72 6f 6d 20 74 68 65 20 70 53 63 68  ed from the pSch
163a4 65 6d 61 2d 3e 61 46 4b 65 79 20 68 61 73 68 20  ema->aFKey hash 
163a5 74 61 62 6c 65 20 0a 20 20 2a 2f 0a 20 20 66 6f  table .  */.  fo
163a6 72 28 70 46 4b 65 79 3d 70 54 61 62 6c 65 2d 3e  r(pFKey=pTable->
163a7 70 46 4b 65 79 3b 20 70 46 4b 65 79 3b 20 70 46  pFKey; pFKey; pF
163a8 4b 65 79 3d 70 4e 65 78 74 46 4b 65 79 29 7b 0a  Key=pNextFKey){.
163a9 20 20 20 20 70 4e 65 78 74 46 4b 65 79 20 3d 20      pNextFKey = 
163aa 70 46 4b 65 79 2d 3e 70 4e 65 78 74 46 72 6f 6d  pFKey->pNextFrom
163ab 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71  ;.    assert( sq
163ac 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 26 70  lite3HashFind(&p
163ad 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61 2d 3e  Table->pSchema->
163ae 61 46 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20  aFKey,.         
163af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
163b0 20 20 70 46 4b 65 79 2d 3e 7a 54 6f 2c 20 73 74    pFKey->zTo, st
163b1 72 6c 65 6e 28 70 46 4b 65 79 2d 3e 7a 54 6f 29  rlen(pFKey->zTo)
163b2 2b 31 29 21 3d 70 46 4b 65 79 20 29 3b 0a 20 20  +1)!=pFKey );.  
163b3 20 20 73 71 6c 69 74 65 46 72 65 65 28 70 46 4b    sqliteFree(pFK
163b4 65 79 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  ey);.  }.#endif.
163b5 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65  .  /* Delete the
163b6 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65   Table structure
163b7 20 69 74 73 65 6c 66 2e 0a 20 20 2a 2f 0a 20 20   itself..  */.  
163b8 73 71 6c 69 74 65 52 65 73 65 74 43 6f 6c 75 6d  sqliteResetColum
163b9 6e 4e 61 6d 65 73 28 70 54 61 62 6c 65 29 3b 0a  nNames(pTable);.
163ba 20 20 73 71 6c 69 74 65 46 72 65 65 28 70 54 61    sqliteFree(pTa
163bb 62 6c 65 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 73  ble->zName);.  s
163bc 71 6c 69 74 65 46 72 65 65 28 70 54 61 62 6c 65  qliteFree(pTable
163bd 2d 3e 7a 43 6f 6c 41 66 66 29 3b 0a 20 20 73 71  ->zColAff);.  sq
163be 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74  lite3SelectDelet
163bf 65 28 70 54 61 62 6c 65 2d 3e 70 53 65 6c 65 63  e(pTable->pSelec
163c0 74 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  t);.#ifndef SQLI
163c1 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20  TE_OMIT_CHECK.  
163c2 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
163c3 65 28 70 54 61 62 6c 65 2d 3e 70 43 68 65 63 6b  e(pTable->pCheck
163c4 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69  );.#endif.  sqli
163c5 74 65 33 56 74 61 62 43 6c 65 61 72 28 70 54 61  te3VtabClear(pTa
163c6 62 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65 46 72  ble);.  sqliteFr
163c7 65 65 28 70 54 61 62 6c 65 29 3b 0a 7d 0a 0a 2f  ee(pTable);.}../
163c8 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20  *.** Unlink the 
163c9 67 69 76 65 6e 20 74 61 62 6c 65 20 66 72 6f 6d  given table from
163ca 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 73   the hash tables
163cb 20 61 6e 64 20 74 68 65 20 64 65 6c 65 74 65 20   and the delete 
163cc 74 68 65 0a 2a 2a 20 74 61 62 6c 65 20 73 74 72  the.** table str
163cd 75 63 74 75 72 65 20 77 69 74 68 20 61 6c 6c 20  ucture with all 
163ce 69 74 73 20 69 6e 64 69 63 65 73 20 61 6e 64 20  its indices and 
163cf 66 6f 72 65 69 67 6e 20 6b 65 79 73 2e 0a 2a 2f  foreign keys..*/
163d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
163d1 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69  void sqlite3Unli
163d2 6e 6b 41 6e 64 44 65 6c 65 74 65 54 61 62 6c 65  nkAndDeleteTable
163d3 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
163d4 74 20 69 44 62 2c 20 63 6f 6e 73 74 20 63 68 61  t iDb, const cha
163d5 72 20 2a 7a 54 61 62 4e 61 6d 65 29 7b 0a 20 20  r *zTabName){.  
163d6 54 61 62 6c 65 20 2a 70 3b 0a 20 20 46 4b 65 79  Table *p;.  FKey
163d7 20 2a 70 46 31 2c 20 2a 70 46 32 3b 0a 20 20 44   *pF1, *pF2;.  D
163d8 62 20 2a 70 44 62 3b 0a 0a 20 20 61 73 73 65 72  b *pDb;..  asser
163d9 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 61 73  t( db!=0 );.  as
163da 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20  sert( iDb>=0 && 
163db 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20  iDb<db->nDb );. 
163dc 20 61 73 73 65 72 74 28 20 7a 54 61 62 4e 61 6d   assert( zTabNam
163dd 65 20 26 26 20 7a 54 61 62 4e 61 6d 65 5b 30 5d  e && zTabName[0]
163de 20 29 3b 0a 20 20 70 44 62 20 3d 20 26 64 62 2d   );.  pDb = &db-
163df 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 70 20 3d  >aDb[iDb];.  p =
163e0 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65   sqlite3HashInse
163e1 72 74 28 26 70 44 62 2d 3e 70 53 63 68 65 6d 61  rt(&pDb->pSchema
163e2 2d 3e 74 62 6c 48 61 73 68 2c 20 7a 54 61 62 4e  ->tblHash, zTabN
163e3 61 6d 65 2c 20 73 74 72 6c 65 6e 28 7a 54 61 62  ame, strlen(zTab
163e4 4e 61 6d 65 29 2b 31 2c 30 29 3b 0a 20 20 69 66  Name)+1,0);.  if
163e5 28 20 70 20 29 7b 0a 23 69 66 6e 64 65 66 20 53  ( p ){.#ifndef S
163e6 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49  QLITE_OMIT_FOREI
163e7 47 4e 5f 4b 45 59 0a 20 20 20 20 66 6f 72 28 70  GN_KEY.    for(p
163e8 46 31 3d 70 2d 3e 70 46 4b 65 79 3b 20 70 46 31  F1=p->pFKey; pF1
163e9 3b 20 70 46 31 3d 70 46 31 2d 3e 70 4e 65 78 74  ; pF1=pF1->pNext
163ea 46 72 6f 6d 29 7b 0a 20 20 20 20 20 20 69 6e 74  From){.      int
163eb 20 6e 54 6f 20 3d 20 73 74 72 6c 65 6e 28 70 46   nTo = strlen(pF
163ec 31 2d 3e 7a 54 6f 29 20 2b 20 31 3b 0a 20 20 20  1->zTo) + 1;.   
163ed 20 20 20 70 46 32 20 3d 20 73 71 6c 69 74 65 33     pF2 = sqlite3
163ee 48 61 73 68 46 69 6e 64 28 26 70 44 62 2d 3e 70  HashFind(&pDb->p
163ef 53 63 68 65 6d 61 2d 3e 61 46 4b 65 79 2c 20 70  Schema->aFKey, p
163f0 46 31 2d 3e 7a 54 6f 2c 20 6e 54 6f 29 3b 0a 20  F1->zTo, nTo);. 
163f1 20 20 20 20 20 69 66 28 20 70 46 32 3d 3d 70 46       if( pF2==pF
163f2 31 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  1 ){.        sql
163f3 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 26  ite3HashInsert(&
163f4 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 61 46  pDb->pSchema->aF
163f5 4b 65 79 2c 20 70 46 31 2d 3e 7a 54 6f 2c 20 6e  Key, pF1->zTo, n
163f6 54 6f 2c 20 70 46 31 2d 3e 70 4e 65 78 74 54 6f  To, pF1->pNextTo
163f7 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
163f8 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70          while( p
163f9 46 32 20 26 26 20 70 46 32 2d 3e 70 4e 65 78 74  F2 && pF2->pNext
163fa 54 6f 21 3d 70 46 31 20 29 7b 20 70 46 32 3d 70  To!=pF1 ){ pF2=p
163fb 46 32 2d 3e 70 4e 65 78 74 54 6f 3b 20 7d 0a 20  F2->pNextTo; }. 
163fc 20 20 20 20 20 20 20 69 66 28 20 70 46 32 20 29         if( pF2 )
163fd 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46 32 2d  {.          pF2-
163fe 3e 70 4e 65 78 74 54 6f 20 3d 20 70 46 31 2d 3e  >pNextTo = pF1->
163ff 70 4e 65 78 74 54 6f 3b 0a 20 20 20 20 20 20 20  pNextTo;.       
16400 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
16401 0a 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c 69  .#endif.    sqli
16402 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70  te3DeleteTable(p
16403 29 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 66 6c 61  );.  }.  db->fla
16404 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74  gs |= SQLITE_Int
16405 65 72 6e 43 68 61 6e 67 65 73 3b 0a 7d 0a 0a 2f  ernChanges;.}../
16406 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 74 6f 6b  *.** Given a tok
16407 65 6e 2c 20 72 65 74 75 72 6e 20 61 20 73 74 72  en, return a str
16408 69 6e 67 20 74 68 61 74 20 63 6f 6e 73 69 73 74  ing that consist
16409 73 20 6f 66 20 74 68 65 20 74 65 78 74 20 6f 66  s of the text of
1640a 20 74 68 61 74 0a 2a 2a 20 74 6f 6b 65 6e 20 77   that.** token w
1640b 69 74 68 20 61 6e 79 20 71 75 6f 74 61 74 69 6f  ith any quotatio
1640c 6e 73 20 72 65 6d 6f 76 65 64 2e 20 20 53 70 61  ns removed.  Spa
1640d 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72  ce to hold the r
1640e 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 0a 2a  eturned string.*
1640f 2a 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72  * is obtained fr
16410 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28  om sqliteMalloc(
16411 29 20 61 6e 64 20 6d 75 73 74 20 62 65 20 66 72  ) and must be fr
16412 65 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69  eed by the calli
16413 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a  ng.** function..
16414 2a 2a 0a 2a 2a 20 54 6f 6b 65 6e 73 20 61 72 65  **.** Tokens are
16415 20 6f 66 74 65 6e 20 6a 75 73 74 20 70 6f 69 6e   often just poin
16416 74 65 72 73 20 69 6e 74 6f 20 74 68 65 20 6f 72  ters into the or
16417 69 67 69 6e 61 6c 20 53 51 4c 20 74 65 78 74 20  iginal SQL text 
16418 61 6e 64 20 73 6f 0a 2a 2a 20 61 72 65 20 6e 6f  and so.** are no
16419 74 20 5c 30 30 30 20 74 65 72 6d 69 6e 61 74 65  t \000 terminate
1641a 64 20 61 6e 64 20 61 72 65 20 6e 6f 74 20 70 65  d and are not pe
1641b 72 73 69 73 74 65 6e 74 2e 20 20 54 68 65 20 72  rsistent.  The r
1641c 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 0a 2a  eturned string.*
1641d 2a 20 69 73 20 5c 30 30 30 20 74 65 72 6d 69 6e  * is \000 termin
1641e 61 74 65 64 20 61 6e 64 20 69 73 20 70 65 72 73  ated and is pers
1641f 69 73 74 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54  istent..*/.SQLIT
16420 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a  E_PRIVATE char *
16421 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54  sqlite3NameFromT
16422 6f 6b 65 6e 28 54 6f 6b 65 6e 20 2a 70 4e 61 6d  oken(Token *pNam
16423 65 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d  e){.  char *zNam
16424 65 3b 0a 20 20 69 66 28 20 70 4e 61 6d 65 20 29  e;.  if( pName )
16425 7b 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71  {.    zName = sq
16426 6c 69 74 65 53 74 72 4e 44 75 70 28 28 63 68 61  liteStrNDup((cha
16427 72 2a 29 70 4e 61 6d 65 2d 3e 7a 2c 20 70 4e 61  r*)pName->z, pNa
16428 6d 65 2d 3e 6e 29 3b 0a 20 20 20 20 73 71 6c 69  me->n);.    sqli
16429 74 65 33 44 65 71 75 6f 74 65 28 7a 4e 61 6d 65  te3Dequote(zName
1642a 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
1642b 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a 20  zName = 0;.  }. 
1642c 20 72 65 74 75 72 6e 20 7a 4e 61 6d 65 3b 0a 7d   return zName;.}
1642d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65  ../*.** Open the
1642e 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74   sqlite_master t
1642f 61 62 6c 65 20 73 74 6f 72 65 64 20 69 6e 20 64  able stored in d
16430 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 69  atabase number i
16431 44 62 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e  Db for.** writin
16432 67 2e 20 54 68 65 20 74 61 62 6c 65 20 69 73 20  g. The table is 
16433 6f 70 65 6e 65 64 20 75 73 69 6e 67 20 63 75 72  opened using cur
16434 73 6f 72 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45  sor 0..*/.SQLITE
16435 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
16436 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 65 72 54  lite3OpenMasterT
16437 61 62 6c 65 28 50 61 72 73 65 20 2a 70 2c 20 69  able(Parse *p, i
16438 6e 74 20 69 44 62 29 7b 0a 20 20 56 64 62 65 20  nt iDb){.  Vdbe 
16439 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56  *v = sqlite3GetV
1643a 64 62 65 28 70 29 3b 0a 20 20 73 71 6c 69 74 65  dbe(p);.  sqlite
1643b 33 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 44  3TableLock(p, iD
1643c 62 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20  b, MASTER_ROOT, 
1643d 31 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28  1, SCHEMA_TABLE(
1643e 69 44 62 29 29 3b 0a 20 20 73 71 6c 69 74 65 33  iDb));.  sqlite3
1643f 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
16440 49 6e 74 65 67 65 72 2c 20 69 44 62 2c 20 30 29  Integer, iDb, 0)
16441 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
16442 64 64 4f 70 28 76 2c 20 4f 50 5f 4f 70 65 6e 57  ddOp(v, OP_OpenW
16443 72 69 74 65 2c 20 30 2c 20 4d 41 53 54 45 52 5f  rite, 0, MASTER_
16444 52 4f 4f 54 29 3b 0a 20 20 73 71 6c 69 74 65 33  ROOT);.  sqlite3
16445 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
16446 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30  SetNumColumns, 0
16447 2c 20 35 29 3b 20 2f 2a 20 73 71 6c 69 74 65 5f  , 5); /* sqlite_
16448 6d 61 73 74 65 72 20 68 61 73 20 35 20 63 6f 6c  master has 5 col
16449 75 6d 6e 73 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a  umns */.}../*.**
1644a 20 54 68 65 20 74 6f 6b 65 6e 20 2a 70 4e 61 6d   The token *pNam
1644b 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e  e contains the n
1644c 61 6d 65 20 6f 66 20 61 20 64 61 74 61 62 61 73  ame of a databas
1644d 65 20 28 65 69 74 68 65 72 20 22 6d 61 69 6e 22  e (either "main"
1644e 20 6f 72 0a 2a 2a 20 22 74 65 6d 70 22 20 6f 72   or.** "temp" or
1644f 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 6e 20   the name of an 
16450 61 74 74 61 63 68 65 64 20 64 62 29 2e 20 54 68  attached db). Th
16451 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  is routine retur
16452 6e 73 20 74 68 65 0a 2a 2a 20 69 6e 64 65 78 20  ns the.** index 
16453 6f 66 20 74 68 65 20 6e 61 6d 65 64 20 64 61 74  of the named dat
16454 61 62 61 73 65 20 69 6e 20 64 62 2d 3e 61 44 62  abase in db->aDb
16455 5b 5d 2c 20 6f 72 20 2d 31 20 69 66 20 74 68 65  [], or -1 if the
16456 20 6e 61 6d 65 64 20 64 62 20 0a 2a 2a 20 64 6f   named db .** do
16457 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 0a 2a 2f  es not exist..*/
16458 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
16459 69 6e 74 20 73 71 6c 69 74 65 33 46 69 6e 64 44  int sqlite3FindD
1645a 62 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54  b(sqlite3 *db, T
1645b 6f 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20 20  oken *pName){.  
1645c 69 6e 74 20 69 20 3d 20 2d 31 3b 20 20 20 20 2f  int i = -1;    /
1645d 2a 20 44 61 74 61 62 61 73 65 20 6e 75 6d 62 65  * Database numbe
1645e 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20  r */.  int n;   
1645f 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
16460 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 69 6e  of characters in
16461 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 44   the name */.  D
16462 62 20 2a 70 44 62 3b 20 20 20 20 20 20 20 2f 2a  b *pDb;       /*
16463 20 41 20 64 61 74 61 62 61 73 65 20 77 68 6f 73   A database whos
16464 65 20 6e 61 6d 65 20 73 70 61 63 65 20 69 73 20  e name space is 
16465 62 65 69 6e 67 20 73 65 61 72 63 68 65 64 20 2a  being searched *
16466 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b  /.  char *zName;
16467 20 20 20 2f 2a 20 4e 61 6d 65 20 77 65 20 61 72     /* Name we ar
16468 65 20 73 65 61 72 63 68 69 6e 67 20 66 6f 72 20  e searching for 
16469 2a 2f 0a 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71  */..  zName = sq
1646a 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
1646b 65 6e 28 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28  en(pName);.  if(
1646c 20 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 6e 20   zName ){.    n 
1646d 3d 20 73 74 72 6c 65 6e 28 7a 4e 61 6d 65 29 3b  = strlen(zName);
1646e 0a 20 20 20 20 66 6f 72 28 69 3d 28 64 62 2d 3e  .    for(i=(db->
1646f 6e 44 62 2d 31 29 2c 20 70 44 62 3d 26 64 62 2d  nDb-1), pDb=&db-
16470 3e 61 44 62 5b 69 5d 3b 20 69 3e 3d 30 3b 20 69  >aDb[i]; i>=0; i
16471 2d 2d 2c 20 70 44 62 2d 2d 29 7b 0a 20 20 20 20  --, pDb--){.    
16472 20 20 69 66 28 20 28 21 4f 4d 49 54 5f 54 45 4d    if( (!OMIT_TEM
16473 50 44 42 20 7c 7c 20 69 21 3d 31 20 29 20 26 26  PDB || i!=1 ) &&
16474 20 6e 3d 3d 73 74 72 6c 65 6e 28 70 44 62 2d 3e   n==strlen(pDb->
16475 7a 4e 61 6d 65 29 20 26 26 20 0a 20 20 20 20 20  zName) && .     
16476 20 20 20 20 20 30 3d 3d 73 71 6c 69 74 65 33 53       0==sqlite3S
16477 74 72 49 43 6d 70 28 70 44 62 2d 3e 7a 4e 61 6d  trICmp(pDb->zNam
16478 65 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20  e, zName) ){.   
16479 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1647a 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71    }.    }.    sq
1647b 6c 69 74 65 46 72 65 65 28 7a 4e 61 6d 65 29 3b  liteFree(zName);
1647c 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 3b  .  }.  return i;
1647d 0a 7d 0a 0a 2f 2a 20 54 68 65 20 74 61 62 6c 65  .}../* The table
1647e 20 6f 72 20 76 69 65 77 20 6f 72 20 74 72 69 67   or view or trig
1647f 67 65 72 20 6e 61 6d 65 20 69 73 20 70 61 73 73  ger name is pass
16480 65 64 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69  ed to this routi
16481 6e 65 20 76 69 61 20 74 6f 6b 65 6e 73 0a 2a 2a  ne via tokens.**
16482 20 70 4e 61 6d 65 31 20 61 6e 64 20 70 4e 61 6d   pName1 and pNam
16483 65 32 2e 20 49 66 20 74 68 65 20 74 61 62 6c 65  e2. If the table
16484 20 6e 61 6d 65 20 77 61 73 20 66 75 6c 6c 79 20   name was fully 
16485 71 75 61 6c 69 66 69 65 64 2c 20 66 6f 72 20 65  qualified, for e
16486 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 43 52  xample:.**.** CR
16487 45 41 54 45 20 54 41 42 4c 45 20 78 78 78 2e 79  EATE TABLE xxx.y
16488 79 79 20 28 2e 2e 2e 29 3b 0a 2a 2a 20 0a 2a 2a  yy (...);.** .**
16489 20 54 68 65 6e 20 70 4e 61 6d 65 31 20 69 73 20   Then pName1 is 
1648a 73 65 74 20 74 6f 20 22 78 78 78 22 20 61 6e 64  set to "xxx" and
1648b 20 70 4e 61 6d 65 32 20 22 79 79 79 22 2e 20 4f   pName2 "yyy". O
1648c 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64  n the other hand
1648d 20 69 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65   if.** the table
1648e 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20 66 75 6c   name is not ful
1648f 6c 79 20 71 75 61 6c 69 66 69 65 64 2c 20 69 2e  ly qualified, i.
16490 65 2e 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54 45  e.:.**.** CREATE
16491 20 54 41 42 4c 45 20 79 79 79 28 2e 2e 2e 29 3b   TABLE yyy(...);
16492 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 70 4e 61 6d  .**.** Then pNam
16493 65 31 20 69 73 20 73 65 74 20 74 6f 20 22 79 79  e1 is set to "yy
16494 79 22 20 61 6e 64 20 70 4e 61 6d 65 32 20 69 73  y" and pName2 is
16495 20 22 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20   ""..**.** This 
16496 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 65  routine sets the
16497 20 2a 70 70 55 6e 71 75 61 6c 20 70 6f 69 6e 74   *ppUnqual point
16498 65 72 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74  er to point at t
16499 68 65 20 74 6f 6b 65 6e 20 28 70 4e 61 6d 65 31  he token (pName1
1649a 20 6f 72 0a 2a 2a 20 70 4e 61 6d 65 32 29 20 74   or.** pName2) t
1649b 68 61 74 20 73 74 6f 72 65 73 20 74 68 65 20 75  hat stores the u
1649c 6e 71 75 61 6c 69 66 69 65 64 20 74 61 62 6c 65  nqualified table
1649d 20 6e 61 6d 65 2e 20 20 54 68 65 20 69 6e 64 65   name.  The inde
1649e 78 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61  x of the.** data
1649f 62 61 73 65 20 22 78 78 78 22 20 69 73 20 72 65  base "xxx" is re
164a0 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  turned..*/.SQLIT
164a1 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
164a2 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65  lite3TwoPartName
164a3 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
164a4 65 2c 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69  e,      /* Parsi
164a5 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65  ng and code gene
164a6 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  rating context *
164a7 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  /.  Token *pName
164a8 31 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22  1,      /* The "
164a9 78 78 78 22 20 69 6e 20 74 68 65 20 6e 61 6d 65  xxx" in the name
164aa 20 22 78 78 78 2e 79 79 79 22 20 6f 72 20 22 78   "xxx.yyy" or "x
164ab 78 78 22 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  xx" */.  Token *
164ac 70 4e 61 6d 65 32 2c 20 20 20 20 20 20 2f 2a 20  pName2,      /* 
164ad 54 68 65 20 22 79 79 79 22 20 69 6e 20 74 68 65  The "yyy" in the
164ae 20 6e 61 6d 65 20 22 78 78 78 2e 79 79 79 22 20   name "xxx.yyy" 
164af 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 2a 70 55 6e  */.  Token **pUn
164b0 71 75 61 6c 20 20 20 20 20 2f 2a 20 57 72 69 74  qual     /* Writ
164b1 65 20 74 68 65 20 75 6e 71 75 61 6c 69 66 69 65  e the unqualifie
164b2 64 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 68 65  d object name he
164b3 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  re */.){.  int i
164b4 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
164b5 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
164b6 73 65 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 6f  se holding the o
164b7 62 6a 65 63 74 20 2a 2f 0a 20 20 73 71 6c 69 74  bject */.  sqlit
164b8 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
164b9 3e 64 62 3b 0a 0a 20 20 69 66 28 20 70 4e 61 6d  >db;..  if( pNam
164ba 65 32 20 26 26 20 70 4e 61 6d 65 32 2d 3e 6e 3e  e2 && pName2->n>
164bb 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  0 ){.    assert(
164bc 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20   !db->init.busy 
164bd 29 3b 0a 20 20 20 20 2a 70 55 6e 71 75 61 6c 20  );.    *pUnqual 
164be 3d 20 70 4e 61 6d 65 32 3b 0a 20 20 20 20 69 44  = pName2;.    iD
164bf 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 44  b = sqlite3FindD
164c0 62 28 64 62 2c 20 70 4e 61 6d 65 31 29 3b 0a 20  b(db, pName1);. 
164c1 20 20 20 69 66 28 20 69 44 62 3c 30 20 29 7b 0a     if( iDb<0 ){.
164c2 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
164c3 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 75  orMsg(pParse, "u
164c4 6e 6b 6e 6f 77 6e 20 64 61 74 61 62 61 73 65 20  nknown database 
164c5 25 54 22 2c 20 70 4e 61 6d 65 31 29 3b 0a 20 20  %T", pName1);.  
164c6 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72      pParse->nErr
164c7 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  ++;.      return
164c8 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c   -1;.    }.  }el
164c9 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  se{.    assert( 
164ca 64 62 2d 3e 69 6e 69 74 2e 69 44 62 3d 3d 30 20  db->init.iDb==0 
164cb 7c 7c 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  || db->init.busy
164cc 20 29 3b 0a 20 20 20 20 69 44 62 20 3d 20 64 62   );.    iDb = db
164cd 2d 3e 69 6e 69 74 2e 69 44 62 3b 0a 20 20 20 20  ->init.iDb;.    
164ce 2a 70 55 6e 71 75 61 6c 20 3d 20 70 4e 61 6d 65  *pUnqual = pName
164cf 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  1;.  }.  return 
164d0 69 44 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  iDb;.}../*.** Th
164d1 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73  is routine is us
164d2 65 64 20 74 6f 20 63 68 65 63 6b 20 69 66 20 74  ed to check if t
164d3 68 65 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20  he UTF-8 string 
164d4 7a 4e 61 6d 65 20 69 73 20 61 20 6c 65 67 61 6c  zName is a legal
164d5 0a 2a 2a 20 75 6e 71 75 61 6c 69 66 69 65 64 20  .** unqualified 
164d6 6e 61 6d 65 20 66 6f 72 20 61 20 6e 65 77 20 73  name for a new s
164d7 63 68 65 6d 61 20 6f 62 6a 65 63 74 20 28 74 61  chema object (ta
164d8 62 6c 65 2c 20 69 6e 64 65 78 2c 20 76 69 65 77  ble, index, view
164d9 20 6f 72 0a 2a 2a 20 74 72 69 67 67 65 72 29 2e   or.** trigger).
164da 20 41 6c 6c 20 6e 61 6d 65 73 20 61 72 65 20 6c   All names are l
164db 65 67 61 6c 20 65 78 63 65 70 74 20 74 68 6f 73  egal except thos
164dc 65 20 74 68 61 74 20 62 65 67 69 6e 20 77 69 74  e that begin wit
164dd 68 20 74 68 65 20 73 74 72 69 6e 67 0a 2a 2a 20  h the string.** 
164de 22 73 71 6c 69 74 65 5f 22 20 28 69 6e 20 75 70  "sqlite_" (in up
164df 70 65 72 2c 20 6c 6f 77 65 72 20 6f 72 20 6d 69  per, lower or mi
164e0 78 65 64 20 63 61 73 65 29 2e 20 54 68 69 73 20  xed case). This 
164e1 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 6e  portion of the n
164e2 61 6d 65 73 70 61 63 65 0a 2a 2a 20 69 73 20 72  amespace.** is r
164e3 65 73 65 72 76 65 64 20 66 6f 72 20 69 6e 74 65  eserved for inte
164e4 72 6e 61 6c 20 75 73 65 2e 0a 2a 2f 0a 53 51 4c  rnal use..*/.SQL
164e5 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
164e6 73 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65  sqlite3CheckObje
164e7 63 74 4e 61 6d 65 28 50 61 72 73 65 20 2a 70 50  ctName(Parse *pP
164e8 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72  arse, const char
164e9 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 69 66 28 20   *zName){.  if( 
164ea 21 70 50 61 72 73 65 2d 3e 64 62 2d 3e 69 6e 69  !pParse->db->ini
164eb 74 2e 62 75 73 79 20 26 26 20 70 50 61 72 73 65  t.busy && pParse
164ec 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 0a 20 20 20  ->nested==0 .   
164ed 20 20 20 20 20 20 20 26 26 20 28 70 50 61 72 73         && (pPars
164ee 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53  e->db->flags & S
164ef 51 4c 49 54 45 5f 57 72 69 74 65 53 63 68 65 6d  QLITE_WriteSchem
164f0 61 29 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 20  a)==0.          
164f1 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72  && 0==sqlite3Str
164f2 4e 49 43 6d 70 28 7a 4e 61 6d 65 2c 20 22 73 71  NICmp(zName, "sq
164f3 6c 69 74 65 5f 22 2c 20 37 29 20 29 7b 0a 20 20  lite_", 7) ){.  
164f4 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
164f5 67 28 70 50 61 72 73 65 2c 20 22 6f 62 6a 65 63  g(pParse, "objec
164f6 74 20 6e 61 6d 65 20 72 65 73 65 72 76 65 64 20  t name reserved 
164f7 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 65  for internal use
164f8 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20  : %s", zName);. 
164f9 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
164fa 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 65  _ERROR;.  }.  re
164fb 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
164fc 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65 67 69 6e 20 63  }../*.** Begin c
164fd 6f 6e 73 74 72 75 63 74 69 6e 67 20 61 20 6e 65  onstructing a ne
164fe 77 20 74 61 62 6c 65 20 72 65 70 72 65 73 65 6e  w table represen
164ff 74 61 74 69 6f 6e 20 69 6e 20 6d 65 6d 6f 72 79  tation in memory
16500 2e 20 20 54 68 69 73 20 69 73 0a 2a 2a 20 74 68  .  This is.** th
16501 65 20 66 69 72 73 74 20 6f 66 20 73 65 76 65 72  e first of sever
16502 61 6c 20 61 63 74 69 6f 6e 20 72 6f 75 74 69 6e  al action routin
16503 65 73 20 74 68 61 74 20 67 65 74 20 63 61 6c 6c  es that get call
16504 65 64 20 69 6e 20 72 65 73 70 6f 6e 73 65 0a 2a  ed in response.*
16505 2a 20 74 6f 20 61 20 43 52 45 41 54 45 20 54 41  * to a CREATE TA
16506 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  BLE statement.  
16507 49 6e 20 70 61 72 74 69 63 75 6c 61 72 2c 20 74  In particular, t
16508 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
16509 61 6c 6c 65 64 0a 2a 2a 20 61 66 74 65 72 20 73  alled.** after s
1650a 65 65 69 6e 67 20 74 6f 6b 65 6e 73 20 22 43 52  eeing tokens "CR
1650b 45 41 54 45 22 20 61 6e 64 20 22 54 41 42 4c 45  EATE" and "TABLE
1650c 22 20 61 6e 64 20 74 68 65 20 74 61 62 6c 65 20  " and the table 
1650d 6e 61 6d 65 2e 20 54 68 65 20 69 73 54 65 6d 70  name. The isTemp
1650e 0a 2a 2a 20 66 6c 61 67 20 69 73 20 74 72 75 65  .** flag is true
1650f 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 73 68   if the table sh
16510 6f 75 6c 64 20 62 65 20 73 74 6f 72 65 64 20 69  ould be stored i
16511 6e 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20  n the auxiliary 
16512 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65  database.** file
16513 20 69 6e 73 74 65 61 64 20 6f 66 20 69 6e 20 74   instead of in t
16514 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
16515 20 66 69 6c 65 2e 20 20 54 68 69 73 20 69 73 20   file.  This is 
16516 6e 6f 72 6d 61 6c 6c 79 20 74 68 65 20 63 61 73  normally the cas
16517 65 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 22 54  e.** when the "T
16518 45 4d 50 22 20 6f 72 20 22 54 45 4d 50 4f 52 41  EMP" or "TEMPORA
16519 52 59 22 20 6b 65 79 77 6f 72 64 20 6f 63 63 75  RY" keyword occu
1651a 72 73 20 69 6e 20 62 65 74 77 65 65 6e 0a 2a 2a  rs in between.**
1651b 20 43 52 45 41 54 45 20 61 6e 64 20 54 41 42 4c   CREATE and TABL
1651c 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 77  E..**.** The new
1651d 20 74 61 62 6c 65 20 72 65 63 6f 72 64 20 69 73   table record is
1651e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 61 6e 64   initialized and
1651f 20 70 75 74 20 69 6e 20 70 50 61 72 73 65 2d 3e   put in pParse->
16520 70 4e 65 77 54 61 62 6c 65 2e 0a 2a 2a 20 41 73  pNewTable..** As
16521 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 43 52 45   more of the CRE
16522 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d  ATE TABLE statem
16523 65 6e 74 20 69 73 20 70 61 72 73 65 64 2c 20 61  ent is parsed, a
16524 64 64 69 74 69 6f 6e 61 6c 20 61 63 74 69 6f 6e  dditional action
16525 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 77 69 6c  .** routines wil
16526 6c 20 62 65 20 63 61 6c 6c 65 64 20 74 6f 20 61  l be called to a
16527 64 64 20 6d 6f 72 65 20 69 6e 66 6f 72 6d 61 74  dd more informat
16528 69 6f 6e 20 74 6f 20 74 68 69 73 20 72 65 63 6f  ion to this reco
16529 72 64 2e 0a 2a 2a 20 41 74 20 74 68 65 20 65 6e  rd..** At the en
1652a 64 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 20  d of the CREATE 
1652b 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2c  TABLE statement,
1652c 20 74 68 65 20 73 71 6c 69 74 65 33 45 6e 64 54   the sqlite3EndT
1652d 61 62 6c 65 28 29 20 72 6f 75 74 69 6e 65 0a 2a  able() routine.*
1652e 2a 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 63  * is called to c
1652f 6f 6d 70 6c 65 74 65 20 74 68 65 20 63 6f 6e 73  omplete the cons
16530 74 72 75 63 74 69 6f 6e 20 6f 66 20 74 68 65 20  truction of the 
16531 6e 65 77 20 74 61 62 6c 65 20 72 65 63 6f 72 64  new table record
16532 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
16533 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
16534 53 74 61 72 74 54 61 62 6c 65 28 0a 20 20 50 61  StartTable(.  Pa
16535 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 2f  rse *pParse,   /
16536 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74  * Parser context
16537 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61   */.  Token *pNa
16538 6d 65 31 2c 20 20 20 2f 2a 20 46 69 72 73 74 20  me1,   /* First 
16539 70 61 72 74 20 6f 66 20 74 68 65 20 6e 61 6d 65  part of the name
1653a 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72   of the table or
1653b 20 76 69 65 77 20 2a 2f 0a 20 20 54 6f 6b 65 6e   view */.  Token
1653c 20 2a 70 4e 61 6d 65 32 2c 20 20 20 2f 2a 20 53   *pName2,   /* S
1653d 65 63 6f 6e 64 20 70 61 72 74 20 6f 66 20 74 68  econd part of th
1653e 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  e name of the ta
1653f 62 6c 65 20 6f 72 20 76 69 65 77 20 2a 2f 0a 20  ble or view */. 
16540 20 69 6e 74 20 69 73 54 65 6d 70 2c 20 20 20 20   int isTemp,    
16541 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69    /* True if thi
16542 73 20 69 73 20 61 20 54 45 4d 50 20 74 61 62 6c  s is a TEMP tabl
16543 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 56 69 65  e */.  int isVie
16544 77 2c 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  w,      /* True 
16545 69 66 20 74 68 69 73 20 69 73 20 61 20 56 49 45  if this is a VIE
16546 57 20 2a 2f 0a 20 20 69 6e 74 20 69 73 56 69 72  W */.  int isVir
16547 74 75 61 6c 2c 20 20 20 2f 2a 20 54 72 75 65 20  tual,   /* True 
16548 69 66 20 74 68 69 73 20 69 73 20 61 20 56 49 52  if this is a VIR
16549 54 55 41 4c 20 74 61 62 6c 65 20 2a 2f 0a 20 20  TUAL table */.  
1654a 69 6e 74 20 6e 6f 45 72 72 20 20 20 20 20 20 20  int noErr       
1654b 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 20 69   /* Do nothing i
1654c 66 20 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20  f table already 
1654d 65 78 69 73 74 73 20 2a 2f 0a 29 7b 0a 20 20 54  exists */.){.  T
1654e 61 62 6c 65 20 2a 70 54 61 62 6c 65 3b 0a 20 20  able *pTable;.  
1654f 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b  char *zName = 0;
16550 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20   /* The name of 
16551 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 2a 2f  the new table */
16552 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
16553 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 56   pParse->db;.  V
16554 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 69 44  dbe *v;.  int iD
16555 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61  b;         /* Da
16556 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 74 6f  tabase number to
16557 20 63 72 65 61 74 65 20 74 68 65 20 74 61 62 6c   create the tabl
16558 65 20 69 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  e in */.  Token 
16559 2a 70 4e 61 6d 65 3b 20 20 20 20 2f 2a 20 55 6e  *pName;    /* Un
1655a 71 75 61 6c 69 66 69 65 64 20 6e 61 6d 65 20 6f  qualified name o
1655b 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 63  f the table to c
1655c 72 65 61 74 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54  reate */..  /* T
1655d 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77  he table or view
1655e 20 6e 61 6d 65 20 74 6f 20 63 72 65 61 74 65 20   name to create 
1655f 69 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 69  is passed to thi
16560 73 20 72 6f 75 74 69 6e 65 20 76 69 61 20 74 6f  s routine via to
16561 6b 65 6e 73 0a 20 20 2a 2a 20 70 4e 61 6d 65 31  kens.  ** pName1
16562 20 61 6e 64 20 70 4e 61 6d 65 32 2e 20 49 66 20   and pName2. If 
16563 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 77  the table name w
16564 61 73 20 66 75 6c 6c 79 20 71 75 61 6c 69 66 69  as fully qualifi
16565 65 64 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 3a  ed, for example:
16566 0a 20 20 2a 2a 0a 20 20 2a 2a 20 43 52 45 41 54  .  **.  ** CREAT
16567 45 20 54 41 42 4c 45 20 78 78 78 2e 79 79 79 20  E TABLE xxx.yyy 
16568 28 2e 2e 2e 29 3b 0a 20 20 2a 2a 20 0a 20 20 2a  (...);.  ** .  *
16569 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31 20 69 73  * Then pName1 is
1656a 20 73 65 74 20 74 6f 20 22 78 78 78 22 20 61 6e   set to "xxx" an
1656b 64 20 70 4e 61 6d 65 32 20 22 79 79 79 22 2e 20  d pName2 "yyy". 
1656c 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e  On the other han
1656d 64 20 69 66 0a 20 20 2a 2a 20 74 68 65 20 74 61  d if.  ** the ta
1656e 62 6c 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20  ble name is not 
1656f 66 75 6c 6c 79 20 71 75 61 6c 69 66 69 65 64 2c  fully qualified,
16570 20 69 2e 65 2e 3a 0a 20 20 2a 2a 0a 20 20 2a 2a   i.e.:.  **.  **
16571 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 79 79   CREATE TABLE yy
16572 79 28 2e 2e 2e 29 3b 0a 20 20 2a 2a 0a 20 20 2a  y(...);.  **.  *
16573 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31 20 69 73  * Then pName1 is
16574 20 73 65 74 20 74 6f 20 22 79 79 79 22 20 61 6e   set to "yyy" an
16575 64 20 70 4e 61 6d 65 32 20 69 73 20 22 22 2e 0a  d pName2 is ""..
16576 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 63 61    **.  ** The ca
16577 6c 6c 20 62 65 6c 6f 77 20 73 65 74 73 20 74 68  ll below sets th
16578 65 20 70 4e 61 6d 65 20 70 6f 69 6e 74 65 72 20  e pName pointer 
16579 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20  to point at the 
1657a 74 6f 6b 65 6e 20 28 70 4e 61 6d 65 31 20 6f 72  token (pName1 or
1657b 0a 20 20 2a 2a 20 70 4e 61 6d 65 32 29 20 74 68  .  ** pName2) th
1657c 61 74 20 73 74 6f 72 65 73 20 74 68 65 20 75 6e  at stores the un
1657d 71 75 61 6c 69 66 69 65 64 20 74 61 62 6c 65 20  qualified table 
1657e 6e 61 6d 65 2e 20 54 68 65 20 76 61 72 69 61 62  name. The variab
1657f 6c 65 20 69 44 62 20 69 73 0a 20 20 2a 2a 20 73  le iDb is.  ** s
16580 65 74 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20  et to the index 
16581 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
16582 74 68 61 74 20 74 68 65 20 74 61 62 6c 65 20 6f  that the table o
16583 72 20 76 69 65 77 20 69 73 20 74 6f 20 62 65 0a  r view is to be.
16584 20 20 2a 2a 20 63 72 65 61 74 65 64 20 69 6e 2e    ** created in.
16585 0a 20 20 2a 2f 0a 20 20 69 44 62 20 3d 20 73 71  .  */.  iDb = sq
16586 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65  lite3TwoPartName
16587 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c  (pParse, pName1,
16588 20 70 4e 61 6d 65 32 2c 20 26 70 4e 61 6d 65 29   pName2, &pName)
16589 3b 0a 20 20 69 66 28 20 69 44 62 3c 30 20 29 20  ;.  if( iDb<0 ) 
1658a 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 21 4f  return;.  if( !O
1658b 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 73  MIT_TEMPDB && is
1658c 54 65 6d 70 20 26 26 20 69 44 62 3e 31 20 29 7b  Temp && iDb>1 ){
1658d 0a 20 20 20 20 2f 2a 20 49 66 20 63 72 65 61 74  .    /* If creat
1658e 69 6e 67 20 61 20 74 65 6d 70 20 74 61 62 6c 65  ing a temp table
1658f 2c 20 74 68 65 20 6e 61 6d 65 20 6d 61 79 20 6e  , the name may n
16590 6f 74 20 62 65 20 71 75 61 6c 69 66 69 65 64 20  ot be qualified 
16591 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  */.    sqlite3Er
16592 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
16593 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20  temporary table 
16594 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 75 6e 71  name must be unq
16595 75 61 6c 69 66 69 65 64 22 29 3b 0a 20 20 20 20  ualified");.    
16596 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66  return;.  }.  if
16597 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26  ( !OMIT_TEMPDB &
16598 26 20 69 73 54 65 6d 70 20 29 20 69 44 62 20 3d  & isTemp ) iDb =
16599 20 31 3b 0a 0a 20 20 70 50 61 72 73 65 2d 3e 73   1;..  pParse->s
1659a 4e 61 6d 65 54 6f 6b 65 6e 20 3d 20 2a 70 4e 61  NameToken = *pNa
1659b 6d 65 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71  me;.  zName = sq
1659c 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
1659d 65 6e 28 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28  en(pName);.  if(
1659e 20 7a 4e 61 6d 65 3d 3d 30 20 29 20 72 65 74 75   zName==0 ) retu
1659f 72 6e 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45  rn;.  if( SQLITE
165a0 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 43 68 65 63  _OK!=sqlite3Chec
165a1 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 70 50 61 72  kObjectName(pPar
165a2 73 65 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a 20 20  se, zName) ){.  
165a3 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62    goto begin_tab
165a4 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20  le_error;.  }.  
165a5 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 69 44 62  if( db->init.iDb
165a6 3d 3d 31 20 29 20 69 73 54 65 6d 70 20 3d 20 31  ==1 ) isTemp = 1
165a7 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
165a8 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
165a9 49 4f 4e 0a 20 20 61 73 73 65 72 74 28 20 28 69  ION.  assert( (i
165aa 73 54 65 6d 70 20 26 20 31 29 3d 3d 69 73 54 65  sTemp & 1)==isTe
165ab 6d 70 20 29 3b 0a 20 20 7b 0a 20 20 20 20 69 6e  mp );.  {.    in
165ac 74 20 63 6f 64 65 3b 0a 20 20 20 20 63 68 61 72  t code;.    char
165ad 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b   *zDb = db->aDb[
165ae 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20  iDb].zName;.    
165af 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43  if( sqlite3AuthC
165b0 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c  heck(pParse, SQL
165b1 49 54 45 5f 49 4e 53 45 52 54 2c 20 53 43 48 45  ITE_INSERT, SCHE
165b2 4d 41 5f 54 41 42 4c 45 28 69 73 54 65 6d 70 29  MA_TABLE(isTemp)
165b3 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20  , 0, zDb) ){.   
165b4 20 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61     goto begin_ta
165b5 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d  ble_error;.    }
165b6 0a 20 20 20 20 69 66 28 20 69 73 56 69 65 77 20  .    if( isView 
165b7 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21 4f 4d  ){.      if( !OM
165b8 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 73 54  IT_TEMPDB && isT
165b9 65 6d 70 20 29 7b 0a 20 20 20 20 20 20 20 20 63  emp ){.        c
165ba 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 43 52 45  ode = SQLITE_CRE
165bb 41 54 45 5f 54 45 4d 50 5f 56 49 45 57 3b 0a 20  ATE_TEMP_VIEW;. 
165bc 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
165bd 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54      code = SQLIT
165be 45 5f 43 52 45 41 54 45 5f 56 49 45 57 3b 0a 20  E_CREATE_VIEW;. 
165bf 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
165c0 7b 0a 20 20 20 20 20 20 69 66 28 20 21 4f 4d 49  {.      if( !OMI
165c1 54 5f 54 45 4d 50 44 42 20 26 26 20 69 73 54 65  T_TEMPDB && isTe
165c2 6d 70 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f  mp ){.        co
165c3 64 65 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41  de = SQLITE_CREA
165c4 54 45 5f 54 45 4d 50 5f 54 41 42 4c 45 3b 0a 20  TE_TEMP_TABLE;. 
165c5 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
165c6 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54      code = SQLIT
165c7 45 5f 43 52 45 41 54 45 5f 54 41 42 4c 45 3b 0a  E_CREATE_TABLE;.
165c8 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
165c9 20 20 69 66 28 20 21 69 73 56 69 72 74 75 61 6c    if( !isVirtual
165ca 20 26 26 20 73 71 6c 69 74 65 33 41 75 74 68 43   && sqlite3AuthC
165cb 68 65 63 6b 28 70 50 61 72 73 65 2c 20 63 6f 64  heck(pParse, cod
165cc 65 2c 20 7a 4e 61 6d 65 2c 20 30 2c 20 7a 44 62  e, zName, 0, zDb
165cd 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  ) ){.      goto 
165ce 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f  begin_table_erro
165cf 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e  r;.    }.  }.#en
165d0 64 69 66 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73  dif..  /* Make s
165d1 75 72 65 20 74 68 65 20 6e 65 77 20 74 61 62 6c  ure the new tabl
165d2 65 20 6e 61 6d 65 20 64 6f 65 73 20 6e 6f 74 20  e name does not 
165d3 63 6f 6c 6c 69 64 65 20 77 69 74 68 20 61 6e 20  collide with an 
165d4 65 78 69 73 74 69 6e 67 0a 20 20 2a 2a 20 69 6e  existing.  ** in
165d5 64 65 78 20 6f 72 20 74 61 62 6c 65 20 6e 61 6d  dex or table nam
165d6 65 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 61  e in the same da
165d7 74 61 62 61 73 65 2e 20 20 49 73 73 75 65 20 61  tabase.  Issue a
165d8 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
165d9 69 66 0a 20 20 2a 2a 20 69 74 20 64 6f 65 73 2e  if.  ** it does.
165da 20 54 68 65 20 65 78 63 65 70 74 69 6f 6e 20 69   The exception i
165db 73 20 69 66 20 74 68 65 20 73 74 61 74 65 6d 65  s if the stateme
165dc 6e 74 20 62 65 69 6e 67 20 70 61 72 73 65 64 20  nt being parsed 
165dd 77 61 73 20 70 61 73 73 65 64 0a 20 20 2a 2a 20  was passed.  ** 
165de 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f 64 65  to an sqlite3_de
165df 63 6c 61 72 65 5f 76 74 61 62 28 29 20 63 61 6c  clare_vtab() cal
165e0 6c 2e 20 49 6e 20 74 68 61 74 20 63 61 73 65 20  l. In that case 
165e1 6f 6e 6c 79 20 74 68 65 20 63 6f 6c 75 6d 6e 20  only the column 
165e2 6e 61 6d 65 73 0a 20 20 2a 2a 20 61 6e 64 20 74  names.  ** and t
165e3 79 70 65 73 20 77 69 6c 6c 20 62 65 20 75 73 65  ypes will be use
165e4 64 2c 20 73 6f 20 74 68 65 72 65 20 69 73 20 6e  d, so there is n
165e5 6f 20 6e 65 65 64 20 74 6f 20 74 65 73 74 20 66  o need to test f
165e6 6f 72 20 6e 61 6d 65 73 70 61 63 65 0a 20 20 2a  or namespace.  *
165e7 2a 20 63 6f 6c 6c 69 73 69 6f 6e 73 2e 0a 20 20  * collisions..  
165e8 2a 2f 0a 20 20 69 66 28 20 21 49 4e 5f 44 45 43  */.  if( !IN_DEC
165e9 4c 41 52 45 5f 56 54 41 42 20 29 7b 0a 20 20 20  LARE_VTAB ){.   
165ea 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
165eb 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d  sqlite3ReadSchem
165ec 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20  a(pParse) ){.   
165ed 20 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61     goto begin_ta
165ee 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d  ble_error;.    }
165ef 0a 20 20 20 20 70 54 61 62 6c 65 20 3d 20 73 71  .    pTable = sq
165f0 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64  lite3FindTable(d
165f1 62 2c 20 7a 4e 61 6d 65 2c 20 64 62 2d 3e 61 44  b, zName, db->aD
165f2 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20  b[iDb].zName);. 
165f3 20 20 20 69 66 28 20 70 54 61 62 6c 65 20 29 7b     if( pTable ){
165f4 0a 20 20 20 20 20 20 69 66 28 20 21 6e 6f 45 72  .      if( !noEr
165f5 72 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  r ){.        sql
165f6 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
165f7 72 73 65 2c 20 22 74 61 62 6c 65 20 25 54 20 61  rse, "table %T a
165f8 6c 72 65 61 64 79 20 65 78 69 73 74 73 22 2c 20  lready exists", 
165f9 70 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a  pName);.      }.
165fa 20 20 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e        goto begin
165fb 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20  _table_error;.  
165fc 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69    }.    if( sqli
165fd 74 65 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c  te3FindIndex(db,
165fe 20 7a 4e 61 6d 65 2c 20 30 29 21 3d 30 20 26 26   zName, 0)!=0 &&
165ff 20 28 69 44 62 3d 3d 30 20 7c 7c 20 21 64 62 2d   (iDb==0 || !db-
16600 3e 69 6e 69 74 2e 62 75 73 79 29 20 29 7b 0a 20  >init.busy) ){. 
16601 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
16602 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 68  rMsg(pParse, "th
16603 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61  ere is already a
16604 6e 20 69 6e 64 65 78 20 6e 61 6d 65 64 20 25 73  n index named %s
16605 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  ", zName);.     
16606 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c   goto begin_tabl
16607 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20  e_error;.    }. 
16608 20 7d 0a 0a 20 20 70 54 61 62 6c 65 20 3d 20 73   }..  pTable = s
16609 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 20 73 69 7a  qliteMalloc( siz
1660a 65 6f 66 28 54 61 62 6c 65 29 20 29 3b 0a 20 20  eof(Table) );.  
1660b 69 66 28 20 70 54 61 62 6c 65 3d 3d 30 20 29 7b  if( pTable==0 ){
1660c 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20  .    pParse->rc 
1660d 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
1660e 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72      pParse->nErr
1660f 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f 20 62 65 67  ++;.    goto beg
16610 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a  in_table_error;.
16611 20 20 7d 0a 20 20 70 54 61 62 6c 65 2d 3e 7a 4e    }.  pTable->zN
16612 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 70  ame = zName;.  p
16613 54 61 62 6c 65 2d 3e 69 50 4b 65 79 20 3d 20 2d  Table->iPKey = -
16614 31 3b 0a 20 20 70 54 61 62 6c 65 2d 3e 70 53 63  1;.  pTable->pSc
16615 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69  hema = db->aDb[i
16616 44 62 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20 70  Db].pSchema;.  p
16617 54 61 62 6c 65 2d 3e 6e 52 65 66 20 3d 20 31 3b  Table->nRef = 1;
16618 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 70  .  if( pParse->p
16619 4e 65 77 54 61 62 6c 65 20 29 20 73 71 6c 69 74  NewTable ) sqlit
1661a 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70 50  e3DeleteTable(pP
1661b 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29  arse->pNewTable)
1661c 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  ;.  pParse->pNew
1661d 54 61 62 6c 65 20 3d 20 70 54 61 62 6c 65 3b 0a  Table = pTable;.
1661e 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73  .  /* If this is
1661f 20 74 68 65 20 6d 61 67 69 63 20 73 71 6c 69 74   the magic sqlit
16620 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65  e_sequence table
16621 20 75 73 65 64 20 62 79 20 61 75 74 6f 69 6e 63   used by autoinc
16622 72 65 6d 65 6e 74 2c 0a 20 20 2a 2a 20 74 68 65  rement,.  ** the
16623 6e 20 72 65 63 6f 72 64 20 61 20 70 6f 69 6e 74  n record a point
16624 65 72 20 74 6f 20 74 68 69 73 20 74 61 62 6c 65  er to this table
16625 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74   in the main dat
16626 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65 0a  abase structure.
16627 20 20 2a 2a 20 73 6f 20 74 68 61 74 20 49 4e 53    ** so that INS
16628 45 52 54 20 63 61 6e 20 66 69 6e 64 20 74 68 65  ERT can find the
16629 20 74 61 62 6c 65 20 65 61 73 69 6c 79 2e 0a 20   table easily.. 
1662a 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   */.#ifndef SQLI
1662b 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52  TE_OMIT_AUTOINCR
1662c 45 4d 45 4e 54 0a 20 20 69 66 28 20 21 70 50 61  EMENT.  if( !pPa
1662d 72 73 65 2d 3e 6e 65 73 74 65 64 20 26 26 20 73  rse->nested && s
1662e 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 20 22 73 71  trcmp(zName, "sq
1662f 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 22 29 3d  lite_sequence")=
16630 3d 30 20 29 7b 0a 20 20 20 20 70 54 61 62 6c 65  =0 ){.    pTable
16631 2d 3e 70 53 63 68 65 6d 61 2d 3e 70 53 65 71 54  ->pSchema->pSeqT
16632 61 62 20 3d 20 70 54 61 62 6c 65 3b 0a 20 20 7d  ab = pTable;.  }
16633 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 42 65  .#endif..  /* Be
16634 67 69 6e 20 67 65 6e 65 72 61 74 69 6e 67 20 74  gin generating t
16635 68 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c  he code that wil
16636 6c 20 69 6e 73 65 72 74 20 74 68 65 20 74 61 62  l insert the tab
16637 6c 65 20 72 65 63 6f 72 64 20 69 6e 74 6f 0a 20  le record into. 
16638 20 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f 4d   ** the SQLITE_M
16639 41 53 54 45 52 20 74 61 62 6c 65 2e 20 20 4e 6f  ASTER table.  No
1663a 74 65 20 69 6e 20 70 61 72 74 69 63 75 6c 61 72  te in particular
1663b 20 74 68 61 74 20 77 65 20 6d 75 73 74 20 67 6f   that we must go
1663c 20 61 68 65 61 64 0a 20 20 2a 2a 20 61 6e 64 20   ahead.  ** and 
1663d 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 72 65 63  allocate the rec
1663e 6f 72 64 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  ord number for t
1663f 68 65 20 74 61 62 6c 65 20 65 6e 74 72 79 20 6e  he table entry n
16640 6f 77 2e 20 20 42 65 66 6f 72 65 20 61 6e 79 0a  ow.  Before any.
16641 20 20 2a 2a 20 50 52 49 4d 41 52 59 20 4b 45 59    ** PRIMARY KEY
16642 20 6f 72 20 55 4e 49 51 55 45 20 6b 65 79 77 6f   or UNIQUE keywo
16643 72 64 73 20 61 72 65 20 70 61 72 73 65 64 2e 20  rds are parsed. 
16644 20 54 68 6f 73 65 20 6b 65 79 77 6f 72 64 73 20   Those keywords 
16645 77 69 6c 6c 20 63 61 75 73 65 0a 20 20 2a 2a 20  will cause.  ** 
16646 69 6e 64 69 63 65 73 20 74 6f 20 62 65 20 63 72  indices to be cr
16647 65 61 74 65 64 20 61 6e 64 20 74 68 65 20 74 61  eated and the ta
16648 62 6c 65 20 72 65 63 6f 72 64 20 6d 75 73 74 20  ble record must 
16649 63 6f 6d 65 20 62 65 66 6f 72 65 20 74 68 65 20  come before the 
1664a 0a 20 20 2a 2a 20 69 6e 64 69 63 65 73 2e 20 20  .  ** indices.  
1664b 48 65 6e 63 65 2c 20 74 68 65 20 72 65 63 6f 72  Hence, the recor
1664c 64 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65  d number for the
1664d 20 74 61 62 6c 65 20 6d 75 73 74 20 62 65 20 61   table must be a
1664e 6c 6c 6f 63 61 74 65 64 0a 20 20 2a 2a 20 6e 6f  llocated.  ** no
1664f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 64  w..  */.  if( !d
16650 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 20  b->init.busy && 
16651 28 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56  (v = sqlite3GetV
16652 64 62 65 28 70 50 61 72 73 65 29 29 21 3d 30 20  dbe(pParse))!=0 
16653 29 7b 0a 20 20 20 20 69 6e 74 20 6c 62 6c 3b 0a  ){.    int lbl;.
16654 20 20 20 20 69 6e 74 20 66 69 6c 65 46 6f 72 6d      int fileForm
16655 61 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42  at;.    sqlite3B
16656 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
16657 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44  on(pParse, 0, iD
16658 62 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  b);..#ifndef SQL
16659 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1665a 54 41 42 4c 45 0a 20 20 20 20 69 66 28 20 69 73  TABLE.    if( is
1665b 56 69 72 74 75 61 6c 20 29 7b 0a 20 20 20 20 20  Virtual ){.     
1665c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1665d 70 28 76 2c 20 4f 50 5f 56 42 65 67 69 6e 2c 20  p(v, OP_VBegin, 
1665e 30 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 23 65 6e  0, 0);.    }.#en
1665f 64 69 66 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74  dif..    /* If t
16660 68 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 61  he file format a
16661 6e 64 20 65 6e 63 6f 64 69 6e 67 20 69 6e 20 74  nd encoding in t
16662 68 65 20 64 61 74 61 62 61 73 65 20 68 61 76 65  he database have
16663 20 6e 6f 74 20 62 65 65 6e 20 73 65 74 2c 20 0a   not been set, .
16664 20 20 20 20 2a 2a 20 73 65 74 20 74 68 65 6d 20      ** set them 
16665 6e 6f 77 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  now..    */.    
16666 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
16667 28 76 2c 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69  (v, OP_ReadCooki
16668 65 2c 20 69 44 62 2c 20 31 29 3b 20 20 20 2f 2a  e, iDb, 1);   /*
16669 20 66 69 6c 65 5f 66 6f 72 6d 61 74 20 2a 2f 0a   file_format */.
1666a 20 20 20 20 6c 62 6c 20 3d 20 73 71 6c 69 74 65      lbl = sqlite
1666b 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76  3VdbeMakeLabel(v
1666c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1666d 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 66  beAddOp(v, OP_If
1666e 2c 20 30 2c 20 6c 62 6c 29 3b 0a 20 20 20 20 66  , 0, lbl);.    f
1666f 69 6c 65 46 6f 72 6d 61 74 20 3d 20 28 64 62 2d  ileFormat = (db-
16670 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
16671 4c 65 67 61 63 79 46 69 6c 65 46 6d 74 29 21 3d  LegacyFileFmt)!=
16672 30 20 3f 0a 20 20 20 20 20 20 20 20 20 20 20 20  0 ?.            
16673 20 20 20 20 20 20 31 20 3a 20 53 51 4c 49 54 45        1 : SQLITE
16674 5f 4d 41 58 5f 46 49 4c 45 5f 46 4f 52 4d 41 54  _MAX_FILE_FORMAT
16675 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
16676 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 74  eAddOp(v, OP_Int
16677 65 67 65 72 2c 20 66 69 6c 65 46 6f 72 6d 61 74  eger, fileFormat
16678 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
16679 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1667a 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c  _SetCookie, iDb,
1667b 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   1);.    sqlite3
1667c 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1667d 49 6e 74 65 67 65 72 2c 20 45 4e 43 28 64 62 29  Integer, ENC(db)
1667e 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
1667f 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
16680 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c  _SetCookie, iDb,
16681 20 34 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   4);.    sqlite3
16682 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
16683 28 76 2c 20 6c 62 6c 29 3b 0a 0a 20 20 20 20 2f  (v, lbl);..    /
16684 2a 20 54 68 69 73 20 6a 75 73 74 20 63 72 65 61  * This just crea
16685 74 65 73 20 61 20 70 6c 61 63 65 2d 68 6f 6c 64  tes a place-hold
16686 65 72 20 72 65 63 6f 72 64 20 69 6e 20 74 68 65  er record in the
16687 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74   sqlite_master t
16688 61 62 6c 65 2e 0a 20 20 20 20 2a 2a 20 54 68 65  able..    ** The
16689 20 72 65 63 6f 72 64 20 63 72 65 61 74 65 64 20   record created 
1668a 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e  does not contain
1668b 20 61 6e 79 74 68 69 6e 67 20 79 65 74 2e 20 20   anything yet.  
1668c 49 74 20 77 69 6c 6c 20 62 65 20 72 65 70 6c 61  It will be repla
1668d 63 65 64 0a 20 20 20 20 2a 2a 20 62 79 20 74 68  ced.    ** by th
1668e 65 20 72 65 61 6c 20 65 6e 74 72 79 20 69 6e 20  e real entry in 
1668f 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20 61  code generated a
16690 74 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c  t sqlite3EndTabl
16691 65 28 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  e()..    **.    
16692 2a 2a 20 54 68 65 20 72 6f 77 69 64 20 66 6f 72  ** The rowid for
16693 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20 69   the new entry i
16694 73 20 6c 65 66 74 20 6f 6e 20 74 68 65 20 74 6f  s left on the to
16695 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b 2e 0a  p of the stack..
16696 20 20 20 20 2a 2a 20 54 68 65 20 72 6f 77 69 64      ** The rowid
16697 20 76 61 6c 75 65 20 69 73 20 6e 65 65 64 65 64   value is needed
16698 20 62 79 20 74 68 65 20 63 6f 64 65 20 74 68 61   by the code tha
16699 74 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c  t sqlite3EndTabl
1669a 65 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 67 65  e will.    ** ge
1669b 6e 65 72 61 74 65 2e 0a 20 20 20 20 2a 2f 0a 23  nerate..    */.#
1669c 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
1669d 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c  TE_OMIT_VIEW) ||
1669e 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
1669f 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
166a0 4c 45 29 0a 20 20 20 20 69 66 28 20 69 73 56 69  LE).    if( isVi
166a1 65 77 20 7c 7c 20 69 73 56 69 72 74 75 61 6c 20  ew || isVirtual 
166a2 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
166a3 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
166a4 49 6e 74 65 67 65 72 2c 20 30 2c 20 30 29 3b 0a  Integer, 0, 0);.
166a5 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66      }else.#endif
166a6 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 73 71 6c  .    {.      sql
166a7 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
166a8 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 2c   OP_CreateTable,
166a9 20 69 44 62 2c 20 30 29 3b 0a 20 20 20 20 7d 0a   iDb, 0);.    }.
166aa 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 4d      sqlite3OpenM
166ab 61 73 74 65 72 54 61 62 6c 65 28 70 50 61 72 73  asterTable(pPars
166ac 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c  e, iDb);.    sql
166ad 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
166ae 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 30 2c   OP_NewRowid, 0,
166af 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
166b0 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
166b1 44 75 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  Dup, 0, 0);.    
166b2 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
166b3 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
166b4 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0);.    sqlite3V
166b5 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49  dbeAddOp(v, OP_I
166b6 6e 73 65 72 74 2c 20 30 2c 20 4f 50 46 4c 41 47  nsert, 0, OPFLAG
166b7 5f 41 50 50 45 4e 44 29 3b 0a 20 20 20 20 73 71  _APPEND);.    sq
166b8 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
166b9 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 30 2c 20 30  , OP_Close, 0, 0
166ba 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
166bb 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 50 75  beAddOp(v, OP_Pu
166bc 6c 6c 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 0a  ll, 1, 0);.  }..
166bd 20 20 2f 2a 20 4e 6f 72 6d 61 6c 20 28 6e 6f 6e    /* Normal (non
166be 2d 65 72 72 6f 72 29 20 72 65 74 75 72 6e 2e 20  -error) return. 
166bf 2a 2f 0a 20 20 72 65 74 75 72 6e 3b 0a 0a 20 20  */.  return;..  
166c0 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  /* If an error o
166c1 63 63 75 72 73 2c 20 77 65 20 6a 75 6d 70 20 68  ccurs, we jump h
166c2 65 72 65 20 2a 2f 0a 62 65 67 69 6e 5f 74 61 62  ere */.begin_tab
166c3 6c 65 5f 65 72 72 6f 72 3a 0a 20 20 73 71 6c 69  le_error:.  sqli
166c4 74 65 46 72 65 65 28 7a 4e 61 6d 65 29 3b 0a 20  teFree(zName);. 
166c5 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a   return;.}../*.*
166c6 2a 20 54 68 69 73 20 6d 61 63 72 6f 20 69 73 20  * This macro is 
166c7 75 73 65 64 20 74 6f 20 63 6f 6d 70 61 72 65 20  used to compare 
166c8 74 77 6f 20 73 74 72 69 6e 67 73 20 69 6e 20 61  two strings in a
166c9 20 63 61 73 65 2d 69 6e 73 65 6e 73 69 74 69 76   case-insensitiv
166ca 65 20 6d 61 6e 6e 65 72 2e 0a 2a 2a 20 49 74 20  e manner..** It 
166cb 69 73 20 73 6c 69 67 68 74 6c 79 20 66 61 73 74  is slightly fast
166cc 65 72 20 74 68 61 6e 20 63 61 6c 6c 69 6e 67 20  er than calling 
166cd 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 29  sqlite3StrICmp()
166ce 20 64 69 72 65 63 74 6c 79 2c 20 62 75 74 0a 2a   directly, but.*
166cf 2a 20 70 72 6f 64 75 63 65 73 20 6c 61 72 67 65  * produces large
166d0 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 57 41  r code..**.** WA
166d1 52 4e 49 4e 47 3a 20 54 68 69 73 20 6d 61 63 72  RNING: This macr
166d2 6f 20 69 73 20 6e 6f 74 20 63 6f 6d 70 61 74 69  o is not compati
166d3 62 6c 65 20 77 69 74 68 20 74 68 65 20 73 74 72  ble with the str
166d4 63 6d 70 28 29 20 66 61 6d 69 6c 79 2e 20 49 74  cmp() family. It
166d5 0a 2a 2a 20 72 65 74 75 72 6e 73 20 74 72 75 65  .** returns true
166d6 20 69 66 20 74 68 65 20 74 77 6f 20 73 74 72 69   if the two stri
166d7 6e 67 73 20 61 72 65 20 65 71 75 61 6c 2c 20 6f  ngs are equal, o
166d8 74 68 65 72 77 69 73 65 20 66 61 6c 73 65 2e 0a  therwise false..
166d9 2a 2f 0a 23 64 65 66 69 6e 65 20 53 54 52 49 43  */.#define STRIC
166da 4d 50 28 78 2c 20 79 29 20 28 5c 0a 73 71 6c 69  MP(x, y) (\.sqli
166db 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b  te3UpperToLower[
166dc 2a 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  *(unsigned char 
166dd 2a 29 28 78 29 5d 3d 3d 20 20 20 5c 0a 73 71 6c  *)(x)]==   \.sql
166de 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72  ite3UpperToLower
166df 5b 2a 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  [*(unsigned char
166e0 20 2a 29 28 79 29 5d 20 20 20 20 20 5c 0a 26 26   *)(y)]     \.&&
166e1 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
166e2 28 78 29 2b 31 2c 28 79 29 2b 31 29 3d 3d 30 20  (x)+1,(y)+1)==0 
166e3 29 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e  )../*.** Add a n
166e4 65 77 20 63 6f 6c 75 6d 6e 20 74 6f 20 74 68 65  ew column to the
166e5 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79   table currently
166e6 20 62 65 69 6e 67 20 63 6f 6e 73 74 72 75 63 74   being construct
166e7 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61  ed..**.** The pa
166e8 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20  rser calls this 
166e9 72 6f 75 74 69 6e 65 20 6f 6e 63 65 20 66 6f 72  routine once for
166ea 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 64 65 63   each column dec
166eb 6c 61 72 61 74 69 6f 6e 0a 2a 2a 20 69 6e 20 61  laration.** in a
166ec 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74   CREATE TABLE st
166ed 61 74 65 6d 65 6e 74 2e 20 20 73 71 6c 69 74 65  atement.  sqlite
166ee 33 53 74 61 72 74 54 61 62 6c 65 28 29 20 67 65  3StartTable() ge
166ef 74 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 66 69 72  ts called.** fir
166f0 73 74 20 74 6f 20 67 65 74 20 74 68 69 6e 67 73  st to get things
166f1 20 67 6f 69 6e 67 2e 20 20 54 68 65 6e 20 74 68   going.  Then th
166f2 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
166f3 6c 6c 65 64 20 66 6f 72 20 65 61 63 68 0a 2a 2a  lled for each.**
166f4 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49   column..*/.SQLI
166f5 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
166f6 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e  sqlite3AddColumn
166f7 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
166f8 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20  Token *pName){. 
166f9 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74   Table *p;.  int
166fa 20 69 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20   i;.  char *z;. 
166fb 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20   Column *pCol;. 
166fc 20 69 66 28 20 28 70 20 3d 20 70 50 61 72 73 65   if( (p = pParse
166fd 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3d 3d 30 20  ->pNewTable)==0 
166fe 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
166ff 70 2d 3e 6e 43 6f 6c 2b 31 3e 53 51 4c 49 54 45  p->nCol+1>SQLITE
16700 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20  _MAX_COLUMN ){. 
16701 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
16702 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20  sg(pParse, "too 
16703 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73 20 6f 6e 20  many columns on 
16704 25 73 22 2c 20 70 2d 3e 7a 4e 61 6d 65 29 3b 0a  %s", p->zName);.
16705 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
16706 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d    z = sqlite3Nam
16707 65 46 72 6f 6d 54 6f 6b 65 6e 28 70 4e 61 6d 65  eFromToken(pName
16708 29 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 20  );.  if( z==0 ) 
16709 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d  return;.  for(i=
1670a 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69 2b  0; i<p->nCol; i+
1670b 2b 29 7b 0a 20 20 20 20 69 66 28 20 53 54 52 49  +){.    if( STRI
1670c 43 4d 50 28 7a 2c 20 70 2d 3e 61 43 6f 6c 5b 69  CMP(z, p->aCol[i
1670d 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20  ].zName) ){.    
1670e 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
1670f 67 28 70 50 61 72 73 65 2c 20 22 64 75 70 6c 69  g(pParse, "dupli
16710 63 61 74 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65  cate column name
16711 3a 20 25 73 22 2c 20 7a 29 3b 0a 20 20 20 20 20  : %s", z);.     
16712 20 73 71 6c 69 74 65 46 72 65 65 28 7a 29 3b 0a   sqliteFree(z);.
16713 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
16714 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 28 70    }.  }.  if( (p
16715 2d 3e 6e 43 6f 6c 20 26 20 30 78 37 29 3d 3d 30  ->nCol & 0x7)==0
16716 20 29 7b 0a 20 20 20 20 43 6f 6c 75 6d 6e 20 2a   ){.    Column *
16717 61 4e 65 77 3b 0a 20 20 20 20 61 4e 65 77 20 3d  aNew;.    aNew =
16718 20 73 71 6c 69 74 65 52 65 61 6c 6c 6f 63 28 20   sqliteRealloc( 
16719 70 2d 3e 61 43 6f 6c 2c 20 28 70 2d 3e 6e 43 6f  p->aCol, (p->nCo
1671a 6c 2b 38 29 2a 73 69 7a 65 6f 66 28 70 2d 3e 61  l+8)*sizeof(p->a
1671b 43 6f 6c 5b 30 5d 29 29 3b 0a 20 20 20 20 69 66  Col[0]));.    if
1671c 28 20 61 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20  ( aNew==0 ){.   
1671d 20 20 20 73 71 6c 69 74 65 46 72 65 65 28 7a 29     sqliteFree(z)
1671e 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  ;.      return;.
1671f 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 61 43 6f      }.    p->aCo
16720 6c 20 3d 20 61 4e 65 77 3b 0a 20 20 7d 0a 20 20  l = aNew;.  }.  
16721 70 43 6f 6c 20 3d 20 26 70 2d 3e 61 43 6f 6c 5b  pCol = &p->aCol[
16722 70 2d 3e 6e 43 6f 6c 5d 3b 0a 20 20 6d 65 6d 73  p->nCol];.  mems
16723 65 74 28 70 43 6f 6c 2c 20 30 2c 20 73 69 7a 65  et(pCol, 0, size
16724 6f 66 28 70 2d 3e 61 43 6f 6c 5b 30 5d 29 29 3b  of(p->aCol[0]));
16725 0a 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d  .  pCol->zName =
16726 20 7a 3b 0a 20 0a 20 20 2f 2a 20 49 66 20 74 68   z;. .  /* If th
16727 65 72 65 20 69 73 20 6e 6f 20 74 79 70 65 20 73  ere is no type s
16728 70 65 63 69 66 69 65 64 2c 20 63 6f 6c 75 6d 6e  pecified, column
16729 73 20 68 61 76 65 20 74 68 65 20 64 65 66 61 75  s have the defau
1672a 6c 74 20 61 66 66 69 6e 69 74 79 0a 20 20 2a 2a  lt affinity.  **
1672b 20 27 4e 4f 4e 45 27 2e 20 49 66 20 74 68 65 72   'NONE'. If ther
1672c 65 20 69 73 20 61 20 74 79 70 65 20 73 70 65 63  e is a type spec
1672d 69 66 69 65 64 2c 20 74 68 65 6e 20 73 71 6c 69  ified, then sqli
1672e 74 65 33 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65  te3AddColumnType
1672f 28 29 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20  () will.  ** be 
16730 63 61 6c 6c 65 64 20 6e 65 78 74 20 74 6f 20 73  called next to s
16731 65 74 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74  et pCol->affinit
16732 79 20 63 6f 72 72 65 63 74 6c 79 2e 0a 20 20 2a  y correctly..  *
16733 2f 0a 20 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69  /.  pCol->affini
16734 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f  ty = SQLITE_AFF_
16735 4e 4f 4e 45 3b 0a 20 20 70 2d 3e 6e 43 6f 6c 2b  NONE;.  p->nCol+
16736 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  +;.}../*.** This
16737 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
16738 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72  ed by the parser
16739 20 77 68 69 6c 65 20 69 6e 20 74 68 65 20 6d 69   while in the mi
1673a 64 64 6c 65 20 6f 66 0a 2a 2a 20 70 61 72 73 69  ddle of.** parsi
1673b 6e 67 20 61 20 43 52 45 41 54 45 20 54 41 42 4c  ng a CREATE TABL
1673c 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 41 20  E statement.  A 
1673d 22 4e 4f 54 20 4e 55 4c 4c 22 20 63 6f 6e 73 74  "NOT NULL" const
1673e 72 61 69 6e 74 20 68 61 73 0a 2a 2a 20 62 65 65  raint has.** bee
1673f 6e 20 73 65 65 6e 20 6f 6e 20 61 20 63 6f 6c 75  n seen on a colu
16740 6d 6e 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  mn.  This routin
16741 65 20 73 65 74 73 20 74 68 65 20 6e 6f 74 4e 75  e sets the notNu
16742 6c 6c 20 66 6c 61 67 20 6f 6e 0a 2a 2a 20 74 68  ll flag on.** th
16743 65 20 63 6f 6c 75 6d 6e 20 63 75 72 72 65 6e 74  e column current
16744 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75  ly under constru
16745 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ction..*/.SQLITE
16746 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
16747 6c 69 74 65 33 41 64 64 4e 6f 74 4e 75 6c 6c 28  lite3AddNotNull(
16748 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69  Parse *pParse, i
16749 6e 74 20 6f 6e 45 72 72 6f 72 29 7b 0a 20 20 54  nt onError){.  T
1674a 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74 20 69  able *p;.  int i
1674b 3b 0a 20 20 69 66 28 20 28 70 20 3d 20 70 50 61  ;.  if( (p = pPa
1674c 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3d  rse->pNewTable)=
1674d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69  =0 ) return;.  i
1674e 20 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20 20   = p->nCol-1;.  
1674f 69 66 28 20 69 3e 3d 30 20 29 20 70 2d 3e 61 43  if( i>=0 ) p->aC
16750 6f 6c 5b 69 5d 2e 6e 6f 74 4e 75 6c 6c 20 3d 20  ol[i].notNull = 
16751 6f 6e 45 72 72 6f 72 3b 0a 7d 0a 0a 2f 2a 0a 2a  onError;.}../*.*
16752 2a 20 53 63 61 6e 20 74 68 65 20 63 6f 6c 75 6d  * Scan the colum
16753 6e 20 74 79 70 65 20 6e 61 6d 65 20 7a 54 79 70  n type name zTyp
16754 65 20 28 6c 65 6e 67 74 68 20 6e 54 79 70 65 29  e (length nType)
16755 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 0a   and return the.
16756 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 61 66  ** associated af
16757 66 69 6e 69 74 79 20 74 79 70 65 2e 0a 2a 2a 0a  finity type..**.
16758 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
16759 64 6f 65 73 20 61 20 63 61 73 65 2d 69 6e 64 65  does a case-inde
1675a 70 65 6e 64 65 6e 74 20 73 65 61 72 63 68 20 6f  pendent search o
1675b 66 20 7a 54 79 70 65 20 66 6f 72 20 74 68 65 20  f zType for the 
1675c 0a 2a 2a 20 73 75 62 73 74 72 69 6e 67 73 20 69  .** substrings i
1675d 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  n the following 
1675e 74 61 62 6c 65 2e 20 49 66 20 6f 6e 65 20 6f 66  table. If one of
1675f 20 74 68 65 20 73 75 62 73 74 72 69 6e 67 73 20   the substrings 
16760 69 73 0a 2a 2a 20 66 6f 75 6e 64 2c 20 74 68 65  is.** found, the
16761 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 61   corresponding a
16762 66 66 69 6e 69 74 79 20 69 73 20 72 65 74 75 72  ffinity is retur
16763 6e 65 64 2e 20 49 66 20 7a 54 79 70 65 20 63 6f  ned. If zType co
16764 6e 74 61 69 6e 73 0a 2a 2a 20 6d 6f 72 65 20 74  ntains.** more t
16765 68 61 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 73  han one of the s
16766 75 62 73 74 72 69 6e 67 73 2c 20 65 6e 74 72 69  ubstrings, entri
16767 65 73 20 74 6f 77 61 72 64 20 74 68 65 20 74 6f  es toward the to
16768 70 20 6f 66 20 0a 2a 2a 20 74 68 65 20 74 61 62  p of .** the tab
16769 6c 65 20 74 61 6b 65 20 70 72 69 6f 72 69 74 79  le take priority
1676a 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69  . For example, i
1676b 66 20 7a 54 79 70 65 20 69 73 20 27 42 4c 4f 42  f zType is 'BLOB
1676c 49 4e 54 27 2c 20 0a 2a 2a 20 53 51 4c 49 54 45  INT', .** SQLITE
1676d 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 69 73 20  _AFF_INTEGER is 
1676e 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
1676f 53 75 62 73 74 72 69 6e 67 20 20 20 20 20 7c 20  Substring     | 
16770 41 66 66 69 6e 69 74 79 0a 2a 2a 20 2d 2d 2d 2d  Affinity.** ----
16771 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
16772 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20  ------------.** 
16773 27 49 4e 54 27 20 20 20 20 20 20 20 20 20 7c 20  'INT'         | 
16774 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47  SQLITE_AFF_INTEG
16775 45 52 0a 2a 2a 20 27 43 48 41 52 27 20 20 20 20  ER.** 'CHAR'    
16776 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46      | SQLITE_AFF
16777 5f 54 45 58 54 0a 2a 2a 20 27 43 4c 4f 42 27 20  _TEXT.** 'CLOB' 
16778 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f         | SQLITE_
16779 41 46 46 5f 54 45 58 54 0a 2a 2a 20 27 54 45 58  AFF_TEXT.** 'TEX
1677a 54 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49  T'        | SQLI
1677b 54 45 5f 41 46 46 5f 54 45 58 54 0a 2a 2a 20 27  TE_AFF_TEXT.** '
1677c 42 4c 4f 42 27 20 20 20 20 20 20 20 20 7c 20 53  BLOB'        | S
1677d 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 0a 2a  QLITE_AFF_NONE.*
1677e 2a 20 27 52 45 41 4c 27 20 20 20 20 20 20 20 20  * 'REAL'        
1677f 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41  | SQLITE_AFF_REA
16780 4c 0a 2a 2a 20 27 46 4c 4f 41 27 20 20 20 20 20  L.** 'FLOA'     
16781 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f     | SQLITE_AFF_
16782 52 45 41 4c 0a 2a 2a 20 27 44 4f 55 42 27 20 20  REAL.** 'DOUB'  
16783 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41        | SQLITE_A
16784 46 46 5f 52 45 41 4c 0a 2a 2a 0a 2a 2a 20 49 66  FF_REAL.**.** If
16785 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 73 75 62   none of the sub
16786 73 74 72 69 6e 67 73 20 69 6e 20 74 68 65 20 61  strings in the a
16787 62 6f 76 65 20 74 61 62 6c 65 20 61 72 65 20 66  bove table are f
16788 6f 75 6e 64 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f  ound,.** SQLITE_
16789 41 46 46 5f 4e 55 4d 45 52 49 43 20 69 73 20 72  AFF_NUMERIC is r
1678a 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49  eturned..*/.SQLI
1678b 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20  TE_PRIVATE char 
1678c 73 71 6c 69 74 65 33 41 66 66 69 6e 69 74 79 54  sqlite3AffinityT
1678d 79 70 65 28 63 6f 6e 73 74 20 54 6f 6b 65 6e 20  ype(const Token 
1678e 2a 70 54 79 70 65 29 7b 0a 20 20 75 33 32 20 68  *pType){.  u32 h
1678f 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 61 66 66   = 0;.  char aff
16790 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55   = SQLITE_AFF_NU
16791 4d 45 52 49 43 3b 0a 20 20 63 6f 6e 73 74 20 75  MERIC;.  const u
16792 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 49  nsigned char *zI
16793 6e 20 3d 20 70 54 79 70 65 2d 3e 7a 3b 0a 20 20  n = pType->z;.  
16794 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
16795 68 61 72 20 2a 7a 45 6e 64 20 3d 20 26 70 54 79  har *zEnd = &pTy
16796 70 65 2d 3e 7a 5b 70 54 79 70 65 2d 3e 6e 5d 3b  pe->z[pType->n];
16797 0a 0a 20 20 77 68 69 6c 65 28 20 7a 49 6e 21 3d  ..  while( zIn!=
16798 7a 45 6e 64 20 29 7b 0a 20 20 20 20 68 20 3d 20  zEnd ){.    h = 
16799 28 68 3c 3c 38 29 20 2b 20 73 71 6c 69 74 65 33  (h<<8) + sqlite3
1679a 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 7a 49  UpperToLower[*zI
1679b 6e 5d 3b 0a 20 20 20 20 7a 49 6e 2b 2b 3b 0a 20  n];.    zIn++;. 
1679c 20 20 20 69 66 28 20 68 3d 3d 28 28 27 63 27 3c     if( h==(('c'<
1679d 3c 32 34 29 2b 28 27 68 27 3c 3c 31 36 29 2b 28  <24)+('h'<<16)+(
1679e 27 61 27 3c 3c 38 29 2b 27 72 27 29 20 29 7b 20  'a'<<8)+'r') ){ 
1679f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
167a0 48 41 52 20 2a 2f 0a 20 20 20 20 20 20 61 66 66  HAR */.      aff
167a1 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45   = SQLITE_AFF_TE
167a2 58 54 3b 20 0a 20 20 20 20 7d 65 6c 73 65 20 69  XT; .    }else i
167a3 66 28 20 68 3d 3d 28 28 27 63 27 3c 3c 32 34 29  f( h==(('c'<<24)
167a4 2b 28 27 6c 27 3c 3c 31 36 29 2b 28 27 6f 27 3c  +('l'<<16)+('o'<
167a5 3c 38 29 2b 27 62 27 29 20 29 7b 20 20 20 20 20  <8)+'b') ){     
167a6 20 20 2f 2a 20 43 4c 4f 42 20 2a 2f 0a 20 20 20    /* CLOB */.   
167a7 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f     aff = SQLITE_
167a8 41 46 46 5f 54 45 58 54 3b 0a 20 20 20 20 7d 65  AFF_TEXT;.    }e
167a9 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27 74 27  lse if( h==(('t'
167aa 3c 3c 32 34 29 2b 28 27 65 27 3c 3c 31 36 29 2b  <<24)+('e'<<16)+
167ab 28 27 78 27 3c 3c 38 29 2b 27 74 27 29 20 29 7b  ('x'<<8)+'t') ){
167ac 20 20 20 20 20 20 20 2f 2a 20 54 45 58 54 20 2a         /* TEXT *
167ad 2f 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 51  /.      aff = SQ
167ae 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3b 0a 20  LITE_AFF_TEXT;. 
167af 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d     }else if( h==
167b0 28 28 27 62 27 3c 3c 32 34 29 2b 28 27 6c 27 3c  (('b'<<24)+('l'<
167b1 3c 31 36 29 2b 28 27 6f 27 3c 3c 38 29 2b 27 62  <16)+('o'<<8)+'b
167b2 27 29 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42  ')          /* B
167b3 4c 4f 42 20 2a 2f 0a 20 20 20 20 20 20 20 20 26  LOB */.        &
167b4 26 20 28 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41  & (aff==SQLITE_A
167b5 46 46 5f 4e 55 4d 45 52 49 43 20 7c 7c 20 61 66  FF_NUMERIC || af
167b6 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52 45  f==SQLITE_AFF_RE
167b7 41 4c 29 20 29 7b 0a 20 20 20 20 20 20 61 66 66  AL) ){.      aff
167b8 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f   = SQLITE_AFF_NO
167b9 4e 45 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  NE;.#ifndef SQLI
167ba 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
167bb 5f 50 4f 49 4e 54 0a 20 20 20 20 7d 65 6c 73 65  _POINT.    }else
167bc 20 69 66 28 20 68 3d 3d 28 28 27 72 27 3c 3c 32   if( h==(('r'<<2
167bd 34 29 2b 28 27 65 27 3c 3c 31 36 29 2b 28 27 61  4)+('e'<<16)+('a
167be 27 3c 3c 38 29 2b 27 6c 27 29 20 20 20 20 20 20  '<<8)+'l')      
167bf 20 20 20 20 2f 2a 20 52 45 41 4c 20 2a 2f 0a 20      /* REAL */. 
167c0 20 20 20 20 20 20 20 26 26 20 61 66 66 3d 3d 53         && aff==S
167c1 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49  QLITE_AFF_NUMERI
167c2 43 20 29 7b 0a 20 20 20 20 20 20 61 66 66 20 3d  C ){.      aff =
167c3 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c   SQLITE_AFF_REAL
167c4 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
167c5 68 3d 3d 28 28 27 66 27 3c 3c 32 34 29 2b 28 27  h==(('f'<<24)+('
167c6 6c 27 3c 3c 31 36 29 2b 28 27 6f 27 3c 3c 38 29  l'<<16)+('o'<<8)
167c7 2b 27 61 27 29 20 20 20 20 20 20 20 20 20 20 2f  +'a')          /
167c8 2a 20 46 4c 4f 41 20 2a 2f 0a 20 20 20 20 20 20  * FLOA */.      
167c9 20 20 26 26 20 61 66 66 3d 3d 53 51 4c 49 54 45    && aff==SQLITE
167ca 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 7b 0a  _AFF_NUMERIC ){.
167cb 20 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49        aff = SQLI
167cc 54 45 5f 41 46 46 5f 52 45 41 4c 3b 0a 20 20 20  TE_AFF_REAL;.   
167cd 20 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 28   }else if( h==((
167ce 27 64 27 3c 3c 32 34 29 2b 28 27 6f 27 3c 3c 31  'd'<<24)+('o'<<1
167cf 36 29 2b 28 27 75 27 3c 3c 38 29 2b 27 62 27 29  6)+('u'<<8)+'b')
167d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 4f 55            /* DOU
167d1 42 20 2a 2f 0a 20 20 20 20 20 20 20 20 26 26 20  B */.        && 
167d2 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f  aff==SQLITE_AFF_
167d3 4e 55 4d 45 52 49 43 20 29 7b 0a 20 20 20 20 20  NUMERIC ){.     
167d4 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46   aff = SQLITE_AF
167d5 46 5f 52 45 41 4c 3b 0a 23 65 6e 64 69 66 0a 20  F_REAL;.#endif. 
167d6 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 68 26     }else if( (h&
167d7 30 78 30 30 46 46 46 46 46 46 29 3d 3d 28 28 27  0x00FFFFFF)==(('
167d8 69 27 3c 3c 31 36 29 2b 28 27 6e 27 3c 3c 38 29  i'<<16)+('n'<<8)
167d9 2b 27 74 27 29 20 29 7b 20 20 20 20 2f 2a 20 49  +'t') ){    /* I
167da 4e 54 20 2a 2f 0a 20 20 20 20 20 20 61 66 66 20  NT */.      aff 
167db 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54  = SQLITE_AFF_INT
167dc 45 47 45 52 3b 0a 20 20 20 20 20 20 62 72 65 61  EGER;.      brea
167dd 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  k;.    }.  }..  
167de 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a 2f  return aff;.}../
167df 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
167e0 65 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74  e is called by t
167e1 68 65 20 70 61 72 73 65 72 20 77 68 69 6c 65 20  he parser while 
167e2 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
167e3 0a 2a 2a 20 70 61 72 73 69 6e 67 20 61 20 43 52  .** parsing a CR
167e4 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65  EATE TABLE state
167e5 6d 65 6e 74 2e 20 20 54 68 65 20 70 46 69 72 73  ment.  The pFirs
167e6 74 20 74 6f 6b 65 6e 20 69 73 20 74 68 65 20 66  t token is the f
167e7 69 72 73 74 0a 2a 2a 20 74 6f 6b 65 6e 20 69 6e  irst.** token in
167e8 20 74 68 65 20 73 65 71 75 65 6e 63 65 20 6f 66   the sequence of
167e9 20 74 6f 6b 65 6e 73 20 74 68 61 74 20 64 65 73   tokens that des
167ea 63 72 69 62 65 20 74 68 65 20 74 79 70 65 20 6f  cribe the type o
167eb 66 20 74 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20  f the.** column 
167ec 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20  currently under 
167ed 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 20  construction.   
167ee 70 4c 61 73 74 20 69 73 20 74 68 65 20 6c 61 73  pLast is the las
167ef 74 20 74 6f 6b 65 6e 0a 2a 2a 20 69 6e 20 74 68  t token.** in th
167f0 65 20 73 65 71 75 65 6e 63 65 2e 20 20 55 73 65  e sequence.  Use
167f1 20 74 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f   this informatio
167f2 6e 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20 61  n to construct a
167f3 20 73 74 72 69 6e 67 0a 2a 2a 20 74 68 61 74 20   string.** that 
167f4 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 79 70  contains the typ
167f5 65 6e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c  ename of the col
167f6 75 6d 6e 20 61 6e 64 20 73 74 6f 72 65 20 74 68  umn and store th
167f7 61 74 20 73 74 72 69 6e 67 0a 2a 2a 20 69 6e 20  at string.** in 
167f8 7a 54 79 70 65 2e 0a 2a 2f 20 0a 53 51 4c 49 54  zType..*/ .SQLIT
167f9 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
167fa 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 54  qlite3AddColumnT
167fb 79 70 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  ype(Parse *pPars
167fc 65 2c 20 54 6f 6b 65 6e 20 2a 70 54 79 70 65 29  e, Token *pType)
167fd 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20  {.  Table *p;.  
167fe 69 6e 74 20 69 3b 0a 20 20 43 6f 6c 75 6d 6e 20  int i;.  Column 
167ff 2a 70 43 6f 6c 3b 0a 0a 20 20 69 66 28 20 28 70  *pCol;..  if( (p
16800 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54   = pParse->pNewT
16801 61 62 6c 65 29 3d 3d 30 20 29 20 72 65 74 75 72  able)==0 ) retur
16802 6e 3b 0a 20 20 69 20 3d 20 70 2d 3e 6e 43 6f 6c  n;.  i = p->nCol
16803 2d 31 3b 0a 20 20 69 66 28 20 69 3c 30 20 29 20  -1;.  if( i<0 ) 
16804 72 65 74 75 72 6e 3b 0a 20 20 70 43 6f 6c 20 3d  return;.  pCol =
16805 20 26 70 2d 3e 61 43 6f 6c 5b 69 5d 3b 0a 20 20   &p->aCol[i];.  
16806 73 71 6c 69 74 65 46 72 65 65 28 70 43 6f 6c 2d  sqliteFree(pCol-
16807 3e 7a 54 79 70 65 29 3b 0a 20 20 70 43 6f 6c 2d  >zType);.  pCol-
16808 3e 7a 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33  >zType = sqlite3
16809 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 70 54  NameFromToken(pT
1680a 79 70 65 29 3b 0a 20 20 70 43 6f 6c 2d 3e 61 66  ype);.  pCol->af
1680b 66 69 6e 69 74 79 20 3d 20 73 71 6c 69 74 65 33  finity = sqlite3
1680c 41 66 66 69 6e 69 74 79 54 79 70 65 28 70 54 79  AffinityType(pTy
1680d 70 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  pe);.}../*.** Th
1680e 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
1680f 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75  the default valu
16810 65 20 66 6f 72 20 74 68 65 20 6d 6f 73 74 20 72  e for the most r
16811 65 63 65 6e 74 6c 79 20 61 64 64 65 64 20 63 6f  ecently added co
16812 6c 75 6d 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 74  lumn.** of the t
16813 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 75  able currently u
16814 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f  nder constructio
16815 6e 2e 0a 2a 2a 0a 2a 2a 20 44 65 66 61 75 6c 74  n..**.** Default
16816 20 76 61 6c 75 65 20 65 78 70 72 65 73 73 69 6f   value expressio
16817 6e 73 20 6d 75 73 74 20 62 65 20 63 6f 6e 73 74  ns must be const
16818 61 6e 74 2e 20 20 52 61 69 73 65 20 61 6e 20 65  ant.  Raise an e
16819 78 63 65 70 74 69 6f 6e 20 69 66 20 74 68 69 73  xception if this
1681a 0a 2a 2a 20 69 73 20 6e 6f 74 20 74 68 65 20 63  .** is not the c
1681b 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ase..**.** This 
1681c 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
1681d 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20  d by the parser 
1681e 77 68 69 6c 65 20 69 6e 20 74 68 65 20 6d 69 64  while in the mid
1681f 64 6c 65 20 6f 66 0a 2a 2a 20 70 61 72 73 69 6e  dle of.** parsin
16820 67 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45  g a CREATE TABLE
16821 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53   statement..*/.S
16822 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
16823 69 64 20 73 71 6c 69 74 65 33 41 64 64 44 65 66  id sqlite3AddDef
16824 61 75 6c 74 56 61 6c 75 65 28 50 61 72 73 65 20  aultValue(Parse 
16825 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70  *pParse, Expr *p
16826 45 78 70 72 29 7b 0a 20 20 54 61 62 6c 65 20 2a  Expr){.  Table *
16827 70 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f  p;.  Column *pCo
16828 6c 3b 0a 20 20 69 66 28 20 28 70 20 3d 20 70 50  l;.  if( (p = pP
16829 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29  arse->pNewTable)
1682a 21 3d 30 20 29 7b 0a 20 20 20 20 70 43 6f 6c 20  !=0 ){.    pCol 
1682b 3d 20 26 28 70 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e  = &(p->aCol[p->n
1682c 43 6f 6c 2d 31 5d 29 3b 0a 20 20 20 20 69 66 28  Col-1]);.    if(
1682d 20 21 73 71 6c 69 74 65 33 45 78 70 72 49 73 43   !sqlite3ExprIsC
1682e 6f 6e 73 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f  onstantOrFunctio
1682f 6e 28 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20  n(pExpr) ){.    
16830 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
16831 67 28 70 50 61 72 73 65 2c 20 22 64 65 66 61 75  g(pParse, "defau
16832 6c 74 20 76 61 6c 75 65 20 6f 66 20 63 6f 6c 75  lt value of colu
16833 6d 6e 20 5b 25 73 5d 20 69 73 20 6e 6f 74 20 63  mn [%s] is not c
16834 6f 6e 73 74 61 6e 74 22 2c 0a 20 20 20 20 20 20  onstant",.      
16835 20 20 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29      pCol->zName)
16836 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
16837 20 20 20 45 78 70 72 20 2a 70 43 6f 70 79 3b 0a     Expr *pCopy;.
16838 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
16839 72 44 65 6c 65 74 65 28 70 43 6f 6c 2d 3e 70 44  rDelete(pCol->pD
1683a 66 6c 74 29 3b 0a 20 20 20 20 20 20 70 43 6f 6c  flt);.      pCol
1683b 2d 3e 70 44 66 6c 74 20 3d 20 70 43 6f 70 79 20  ->pDflt = pCopy 
1683c 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70  = sqlite3ExprDup
1683d 28 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 69  (pExpr);.      i
1683e 66 28 20 70 43 6f 70 79 20 29 7b 0a 20 20 20 20  f( pCopy ){.    
1683f 20 20 20 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e      sqlite3Token
16840 43 6f 70 79 28 26 70 43 6f 70 79 2d 3e 73 70 61  Copy(&pCopy->spa
16841 6e 2c 20 26 70 45 78 70 72 2d 3e 73 70 61 6e 29  n, &pExpr->span)
16842 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
16843 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 78 70    }.  sqlite3Exp
16844 72 44 65 6c 65 74 65 28 70 45 78 70 72 29 3b 0a  rDelete(pExpr);.
16845 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 69 67 6e 61  }../*.** Designa
16846 74 65 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b  te the PRIMARY K
16847 45 59 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65  EY for the table
16848 2e 20 20 70 4c 69 73 74 20 69 73 20 61 20 6c 69  .  pList is a li
16849 73 74 20 6f 66 20 6e 61 6d 65 73 20 0a 2a 2a 20  st of names .** 
1684a 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 68 61 74 20  of columns that 
1684b 66 6f 72 6d 20 74 68 65 20 70 72 69 6d 61 72 79  form the primary
1684c 20 6b 65 79 2e 20 20 49 66 20 70 4c 69 73 74 20   key.  If pList 
1684d 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68  is NULL, then th
1684e 65 0a 2a 2a 20 6d 6f 73 74 20 72 65 63 65 6e 74  e.** most recent
1684f 6c 79 20 61 64 64 65 64 20 63 6f 6c 75 6d 6e 20  ly added column 
16850 6f 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  of the table is 
16851 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 2e  the primary key.
16852 0a 2a 2a 0a 2a 2a 20 41 20 74 61 62 6c 65 20 63  .**.** A table c
16853 61 6e 20 68 61 76 65 20 61 74 20 6d 6f 73 74 20  an have at most 
16854 6f 6e 65 20 70 72 69 6d 61 72 79 20 6b 65 79 2e  one primary key.
16855 20 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 61    If the table a
16856 6c 72 65 61 64 79 20 68 61 73 0a 2a 2a 20 61 20  lready has.** a 
16857 70 72 69 6d 61 72 79 20 6b 65 79 20 28 61 6e 64  primary key (and
16858 20 74 68 69 73 20 69 73 20 74 68 65 20 73 65 63   this is the sec
16859 6f 6e 64 20 70 72 69 6d 61 72 79 20 6b 65 79 29  ond primary key)
1685a 20 74 68 65 6e 20 63 72 65 61 74 65 20 61 6e 0a   then create an.
1685b 2a 2a 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20  ** error..**.** 
1685c 49 66 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b  If the PRIMARY K
1685d 45 59 20 69 73 20 6f 6e 20 61 20 73 69 6e 67 6c  EY is on a singl
1685e 65 20 63 6f 6c 75 6d 6e 20 77 68 6f 73 65 20 64  e column whose d
1685f 61 74 61 74 79 70 65 20 69 73 20 49 4e 54 45 47  atatype is INTEG
16860 45 52 2c 0a 2a 2a 20 74 68 65 6e 20 77 65 20 77  ER,.** then we w
16861 69 6c 6c 20 74 72 79 20 74 6f 20 75 73 65 20 74  ill try to use t
16862 68 61 74 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68  hat column as th
16863 65 20 72 6f 77 69 64 2e 20 20 53 65 74 20 74 68  e rowid.  Set th
16864 65 20 54 61 62 6c 65 2e 69 50 4b 65 79 0a 2a 2a  e Table.iPKey.**
16865 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 74 61   field of the ta
16866 62 6c 65 20 75 6e 64 65 72 20 63 6f 6e 73 74 72  ble under constr
16867 75 63 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65  uction to be the
16868 20 69 6e 64 65 78 20 6f 66 20 74 68 65 0a 2a 2a   index of the.**
16869 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
1686a 20 4b 45 59 20 63 6f 6c 75 6d 6e 2e 20 20 54 61   KEY column.  Ta
1686b 62 6c 65 2e 69 50 4b 65 79 20 69 73 20 73 65 74  ble.iPKey is set
1686c 20 74 6f 20 2d 31 20 69 66 20 74 68 65 72 65 20   to -1 if there 
1686d 69 73 0a 2a 2a 20 6e 6f 20 49 4e 54 45 47 45 52  is.** no INTEGER
1686e 20 50 52 49 4d 41 52 59 20 4b 45 59 2e 0a 2a 2a   PRIMARY KEY..**
1686f 0a 2a 2a 20 49 66 20 74 68 65 20 6b 65 79 20 69  .** If the key i
16870 73 20 6e 6f 74 20 61 6e 20 49 4e 54 45 47 45 52  s not an INTEGER
16871 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 74 68   PRIMARY KEY, th
16872 65 6e 20 63 72 65 61 74 65 20 61 20 75 6e 69 71  en create a uniq
16873 75 65 0a 2a 2a 20 69 6e 64 65 78 20 66 6f 72 20  ue.** index for 
16874 74 68 65 20 6b 65 79 2e 20 20 4e 6f 20 69 6e 64  the key.  No ind
16875 65 78 20 69 73 20 63 72 65 61 74 65 64 20 66 6f  ex is created fo
16876 72 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  r INTEGER PRIMAR
16877 59 20 4b 45 59 73 2e 0a 2a 2f 0a 53 51 4c 49 54  Y KEYs..*/.SQLIT
16878 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
16879 71 6c 69 74 65 33 41 64 64 50 72 69 6d 61 72 79  qlite3AddPrimary
1687a 4b 65 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50  Key(.  Parse *pP
1687b 61 72 73 65 2c 20 20 20 20 2f 2a 20 50 61 72 73  arse,    /* Pars
1687c 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
1687d 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
1687e 2c 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 66 69  ,  /* List of fi
1687f 65 6c 64 20 6e 61 6d 65 73 20 74 6f 20 62 65 20  eld names to be 
16880 69 6e 64 65 78 65 64 20 2a 2f 0a 20 20 69 6e 74  indexed */.  int
16881 20 6f 6e 45 72 72 6f 72 2c 20 20 20 20 20 20 2f   onError,      /
16882 2a 20 57 68 61 74 20 74 6f 20 64 6f 20 77 69 74  * What to do wit
16883 68 20 61 20 75 6e 69 71 75 65 6e 65 73 73 20 63  h a uniqueness c
16884 6f 6e 66 6c 69 63 74 20 2a 2f 0a 20 20 69 6e 74  onflict */.  int
16885 20 61 75 74 6f 49 6e 63 2c 20 20 20 20 20 20 2f   autoInc,      /
16886 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 41 55  * True if the AU
16887 54 4f 49 4e 43 52 45 4d 45 4e 54 20 6b 65 79 77  TOINCREMENT keyw
16888 6f 72 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a  ord is present *
16889 2f 0a 20 20 69 6e 74 20 73 6f 72 74 4f 72 64 65  /.  int sortOrde
1688a 72 20 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  r     /* SQLITE_
1688b 53 4f 5f 41 53 43 20 6f 72 20 53 51 4c 49 54 45  SO_ASC or SQLITE
1688c 5f 53 4f 5f 44 45 53 43 20 2a 2f 0a 29 7b 0a 20  _SO_DESC */.){. 
1688d 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70   Table *pTab = p
1688e 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
1688f 3b 0a 20 20 63 68 61 72 20 2a 7a 54 79 70 65 20  ;.  char *zType 
16890 3d 20 30 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 20  = 0;.  int iCol 
16891 3d 20 2d 31 2c 20 69 3b 0a 20 20 69 66 28 20 70  = -1, i;.  if( p
16892 54 61 62 3d 3d 30 20 7c 7c 20 49 4e 5f 44 45 43  Tab==0 || IN_DEC
16893 4c 41 52 45 5f 56 54 41 42 20 29 20 67 6f 74 6f  LARE_VTAB ) goto
16894 20 70 72 69 6d 61 72 79 5f 6b 65 79 5f 65 78 69   primary_key_exi
16895 74 3b 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 68  t;.  if( pTab->h
16896 61 73 50 72 69 6d 4b 65 79 20 29 7b 0a 20 20 20  asPrimKey ){.   
16897 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
16898 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20  (pParse, .      
16899 22 74 61 62 6c 65 20 5c 22 25 73 5c 22 20 68 61  "table \"%s\" ha
1689a 73 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20  s more than one 
1689b 70 72 69 6d 61 72 79 20 6b 65 79 22 2c 20 70 54  primary key", pT
1689c 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ab->zName);.    
1689d 67 6f 74 6f 20 70 72 69 6d 61 72 79 5f 6b 65 79  goto primary_key
1689e 5f 65 78 69 74 3b 0a 20 20 7d 0a 20 20 70 54 61  _exit;.  }.  pTa
1689f 62 2d 3e 68 61 73 50 72 69 6d 4b 65 79 20 3d 20  b->hasPrimKey = 
168a0 31 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d  1;.  if( pList==
168a1 30 20 29 7b 0a 20 20 20 20 69 43 6f 6c 20 3d 20  0 ){.    iCol = 
168a2 70 54 61 62 2d 3e 6e 43 6f 6c 20 2d 20 31 3b 0a  pTab->nCol - 1;.
168a3 20 20 20 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69      pTab->aCol[i
168a4 43 6f 6c 5d 2e 69 73 50 72 69 6d 4b 65 79 20 3d  Col].isPrimKey =
168a5 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   1;.  }else{.   
168a6 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73   for(i=0; i<pLis
168a7 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a  t->nExpr; i++){.
168a8 20 20 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30        for(iCol=0
168a9 3b 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f  ; iCol<pTab->nCo
168aa 6c 3b 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20  l; iCol++){.    
168ab 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
168ac 74 72 49 43 6d 70 28 70 4c 69 73 74 2d 3e 61 5b  trICmp(pList->a[
168ad 69 5d 2e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e  i].zName, pTab->
168ae 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65  aCol[iCol].zName
168af 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
168b0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
168b1 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
168b2 20 69 66 28 20 69 43 6f 6c 3c 70 54 61 62 2d 3e   if( iCol<pTab->
168b3 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  nCol ){.        
168b4 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d  pTab->aCol[iCol]
168b5 2e 69 73 50 72 69 6d 4b 65 79 20 3d 20 31 3b 0a  .isPrimKey = 1;.
168b6 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
168b7 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 6e 45 78    if( pList->nEx
168b8 70 72 3e 31 20 29 20 69 43 6f 6c 20 3d 20 2d 31  pr>1 ) iCol = -1
168b9 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 43 6f 6c  ;.  }.  if( iCol
168ba 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70 54 61 62  >=0 && iCol<pTab
168bb 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 7a 54  ->nCol ){.    zT
168bc 79 70 65 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c  ype = pTab->aCol
168bd 5b 69 43 6f 6c 5d 2e 7a 54 79 70 65 3b 0a 20 20  [iCol].zType;.  
168be 7d 0a 20 20 69 66 28 20 7a 54 79 70 65 20 26 26  }.  if( zType &&
168bf 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
168c0 7a 54 79 70 65 2c 20 22 49 4e 54 45 47 45 52 22  zType, "INTEGER"
168c1 29 3d 3d 30 0a 20 20 20 20 20 20 20 20 26 26 20  )==0.        && 
168c2 73 6f 72 74 4f 72 64 65 72 3d 3d 53 51 4c 49 54  sortOrder==SQLIT
168c3 45 5f 53 4f 5f 41 53 43 20 29 7b 0a 20 20 20 20  E_SO_ASC ){.    
168c4 70 54 61 62 2d 3e 69 50 4b 65 79 20 3d 20 69 43  pTab->iPKey = iC
168c5 6f 6c 3b 0a 20 20 20 20 70 54 61 62 2d 3e 6b 65  ol;.    pTab->ke
168c6 79 43 6f 6e 66 20 3d 20 6f 6e 45 72 72 6f 72 3b  yConf = onError;
168c7 0a 20 20 20 20 70 54 61 62 2d 3e 61 75 74 6f 49  .    pTab->autoI
168c8 6e 63 20 3d 20 61 75 74 6f 49 6e 63 3b 0a 20 20  nc = autoInc;.  
168c9 7d 65 6c 73 65 20 69 66 28 20 61 75 74 6f 49 6e  }else if( autoIn
168ca 63 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c  c ){.#ifndef SQL
168cb 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43  ITE_OMIT_AUTOINC
168cc 52 45 4d 45 4e 54 0a 20 20 20 20 73 71 6c 69 74  REMENT.    sqlit
168cd 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
168ce 65 2c 20 22 41 55 54 4f 49 4e 43 52 45 4d 45 4e  e, "AUTOINCREMEN
168cf 54 20 69 73 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65  T is only allowe
168d0 64 20 6f 6e 20 61 6e 20 22 0a 20 20 20 20 20 20  d on an ".      
168d1 20 22 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52   "INTEGER PRIMAR
168d2 59 20 4b 45 59 22 29 3b 0a 23 65 6e 64 69 66 0a  Y KEY");.#endif.
168d3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
168d4 69 74 65 33 43 72 65 61 74 65 49 6e 64 65 78 28  ite3CreateIndex(
168d5 70 50 61 72 73 65 2c 20 30 2c 20 30 2c 20 30 2c  pParse, 0, 0, 0,
168d6 20 70 4c 69 73 74 2c 20 6f 6e 45 72 72 6f 72 2c   pList, onError,
168d7 20 30 2c 20 30 2c 20 73 6f 72 74 4f 72 64 65 72   0, 0, sortOrder
168d8 2c 20 30 29 3b 0a 20 20 20 20 70 4c 69 73 74 20  , 0);.    pList 
168d9 3d 20 30 3b 0a 20 20 7d 0a 0a 70 72 69 6d 61 72  = 0;.  }..primar
168da 79 5f 6b 65 79 5f 65 78 69 74 3a 0a 20 20 73 71  y_key_exit:.  sq
168db 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c  lite3ExprListDel
168dc 65 74 65 28 70 4c 69 73 74 29 3b 0a 20 20 72 65  ete(pList);.  re
168dd 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  turn;.}../*.** A
168de 64 64 20 61 20 6e 65 77 20 43 48 45 43 4b 20 63  dd a new CHECK c
168df 6f 6e 73 74 72 61 69 6e 74 20 74 6f 20 74 68 65  onstraint to the
168e0 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79   table currently
168e1 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74   under construct
168e2 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion..*/.SQLITE_P
168e3 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
168e4 74 65 33 41 64 64 43 68 65 63 6b 43 6f 6e 73 74  te3AddCheckConst
168e5 72 61 69 6e 74 28 0a 20 20 50 61 72 73 65 20 2a  raint(.  Parse *
168e6 70 50 61 72 73 65 2c 20 20 20 20 2f 2a 20 50 61  pParse,    /* Pa
168e7 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
168e8 0a 20 20 45 78 70 72 20 2a 70 43 68 65 63 6b 45  .  Expr *pCheckE
168e9 78 70 72 20 20 2f 2a 20 54 68 65 20 63 68 65 63  xpr  /* The chec
168ea 6b 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a  k expression */.
168eb 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ){.#ifndef SQLIT
168ec 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 54  E_OMIT_CHECK.  T
168ed 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 50 61  able *pTab = pPa
168ee 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a  rse->pNewTable;.
168ef 20 20 69 66 28 20 70 54 61 62 20 26 26 20 21 49    if( pTab && !I
168f0 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 29  N_DECLARE_VTAB )
168f1 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 43 48 45  {.    /* The CHE
168f2 43 4b 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 75  CK expression mu
168f3 73 74 20 62 65 20 64 75 70 6c 69 63 61 74 65 64  st be duplicated
168f4 20 73 6f 20 74 68 61 74 20 74 6f 6b 65 6e 73 20   so that tokens 
168f5 72 65 66 65 72 0a 20 20 20 20 2a 2a 20 74 6f 20  refer.    ** to 
168f6 6d 61 6c 6c 6f 63 65 64 20 73 70 61 63 65 20 61  malloced space a
168f7 6e 64 20 6e 6f 74 20 74 68 65 20 28 65 70 68 65  nd not the (ephe
168f8 6d 65 72 61 6c 29 20 74 65 78 74 20 6f 66 20 74  meral) text of t
168f9 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 0a  he CREATE TABLE.
168fa 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74      ** statement
168fb 20 2a 2f 0a 20 20 20 20 70 54 61 62 2d 3e 70 43   */.    pTab->pC
168fc 68 65 63 6b 20 3d 20 73 71 6c 69 74 65 33 45 78  heck = sqlite3Ex
168fd 70 72 41 6e 64 28 70 54 61 62 2d 3e 70 43 68 65  prAnd(pTab->pChe
168fe 63 6b 2c 20 73 71 6c 69 74 65 33 45 78 70 72 44  ck, sqlite3ExprD
168ff 75 70 28 70 43 68 65 63 6b 45 78 70 72 29 29 3b  up(pCheckExpr));
16900 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 73 71  .  }.#endif.  sq
16901 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
16902 70 43 68 65 63 6b 45 78 70 72 29 3b 0a 7d 0a 0a  pCheckExpr);.}..
16903 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 63 6f  /*.** Set the co
16904 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e  llation function
16905 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63   of the most rec
16906 65 6e 74 6c 79 20 70 61 72 73 65 64 20 74 61 62  ently parsed tab
16907 6c 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 74 6f 20  le column.** to 
16908 74 68 65 20 43 6f 6c 6c 53 65 71 20 67 69 76 65  the CollSeq give
16909 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  n..*/.SQLITE_PRI
1690a 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1690b 33 41 64 64 43 6f 6c 6c 61 74 65 54 79 70 65 28  3AddCollateType(
1690c 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63  Parse *pParse, c
1690d 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65  onst char *zType
1690e 2c 20 69 6e 74 20 6e 54 79 70 65 29 7b 0a 20 20  , int nType){.  
1690f 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74 20  Table *p;.  int 
16910 69 3b 0a 0a 20 20 69 66 28 20 28 70 20 3d 20 70  i;..  if( (p = p
16911 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
16912 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  )==0 ) return;. 
16913 20 69 20 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a   i = p->nCol-1;.
16914 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 4c 6f  .  if( sqlite3Lo
16915 63 61 74 65 43 6f 6c 6c 53 65 71 28 70 50 61 72  cateCollSeq(pPar
16916 73 65 2c 20 7a 54 79 70 65 2c 20 6e 54 79 70 65  se, zType, nType
16917 29 20 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a  ) ){.    Index *
16918 70 49 64 78 3b 0a 20 20 20 20 70 2d 3e 61 43 6f  pIdx;.    p->aCo
16919 6c 5b 69 5d 2e 7a 43 6f 6c 6c 20 3d 20 73 71 6c  l[i].zColl = sql
1691a 69 74 65 53 74 72 4e 44 75 70 28 7a 54 79 70 65  iteStrNDup(zType
1691b 2c 20 6e 54 79 70 65 29 3b 0a 20 20 0a 20 20 20  , nType);.  .   
1691c 20 2f 2a 20 49 66 20 74 68 65 20 63 6f 6c 75 6d   /* If the colum
1691d 6e 20 69 73 20 64 65 63 6c 61 72 65 64 20 61 73  n is declared as
1691e 20 22 3c 6e 61 6d 65 3e 20 50 52 49 4d 41 52 59   "<name> PRIMARY
1691f 20 4b 45 59 20 43 4f 4c 4c 41 54 45 20 3c 74 79   KEY COLLATE <ty
16920 70 65 3e 22 2c 0a 20 20 20 20 2a 2a 20 74 68 65  pe>",.    ** the
16921 6e 20 61 6e 20 69 6e 64 65 78 20 6d 61 79 20 68  n an index may h
16922 61 76 65 20 62 65 65 6e 20 63 72 65 61 74 65 64  ave been created
16923 20 6f 6e 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20   on this column 
16924 62 65 66 6f 72 65 20 74 68 65 0a 20 20 20 20 2a  before the.    *
16925 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 74 79 70 65  * collation type
16926 20 77 61 73 20 61 64 64 65 64 2e 20 43 6f 72 72   was added. Corr
16927 65 63 74 20 74 68 69 73 20 69 66 20 69 74 20 69  ect this if it i
16928 73 20 74 68 65 20 63 61 73 65 2e 0a 20 20 20 20  s the case..    
16929 2a 2f 0a 20 20 20 20 66 6f 72 28 70 49 64 78 3d  */.    for(pIdx=
1692a 70 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b  p->pIndex; pIdx;
1692b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
1692c 74 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  t){.      assert
1692d 28 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3d  ( pIdx->nColumn=
1692e 3d 31 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  =1 );.      if( 
1692f 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 30  pIdx->aiColumn[0
16930 5d 3d 3d 69 20 29 7b 0a 20 20 20 20 20 20 20 20  ]==i ){.        
16931 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 30 5d 20  pIdx->azColl[0] 
16932 3d 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f  = p->aCol[i].zCo
16933 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ll;.      }.    
16934 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  }.  }.}../*.** T
16935 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  his function ret
16936 75 72 6e 73 20 74 68 65 20 63 6f 6c 6c 61 74 69  urns the collati
16937 6f 6e 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20  on sequence for 
16938 64 61 74 61 62 61 73 65 20 6e 61 74 69 76 65 20  database native 
16939 74 65 78 74 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67  text.** encoding
1693a 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 74   identified by t
1693b 68 65 20 73 74 72 69 6e 67 20 7a 4e 61 6d 65 2c  he string zName,
1693c 20 6c 65 6e 67 74 68 20 6e 4e 61 6d 65 2e 0a 2a   length nName..*
1693d 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 71 75  *.** If the requ
1693e 65 73 74 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20  ested collation 
1693f 73 65 71 75 65 6e 63 65 20 69 73 20 6e 6f 74 20  sequence is not 
16940 61 76 61 69 6c 61 62 6c 65 2c 20 6f 72 20 6e 6f  available, or no
16941 74 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 69  t available.** i
16942 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e  n the database n
16943 61 74 69 76 65 20 65 6e 63 6f 64 69 6e 67 2c 20  ative encoding, 
16944 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61  the collation fa
16945 63 74 6f 72 79 20 69 73 20 69 6e 76 6f 6b 65 64  ctory is invoked
16946 20 74 6f 0a 2a 2a 20 72 65 71 75 65 73 74 20 69   to.** request i
16947 74 2e 20 49 66 20 74 68 65 20 63 6f 6c 6c 61 74  t. If the collat
16948 69 6f 6e 20 66 61 63 74 6f 72 79 20 64 6f 65 73  ion factory does
16949 20 6e 6f 74 20 73 75 70 70 6c 79 20 73 75 63 68   not supply such
1694a 20 61 20 73 65 71 75 65 6e 63 65 2c 0a 2a 2a 20   a sequence,.** 
1694b 61 6e 64 20 74 68 65 20 73 65 71 75 65 6e 63 65  and the sequence
1694c 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 69 6e   is available in
1694d 20 61 6e 6f 74 68 65 72 20 74 65 78 74 20 65 6e   another text en
1694e 63 6f 64 69 6e 67 2c 20 74 68 65 6e 20 74 68 61  coding, then tha
1694f 74 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64  t is.** returned
16950 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a 20   instead..**.** 
16951 49 66 20 6e 6f 20 76 65 72 73 69 6f 6e 73 20 6f  If no versions o
16952 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  f the requested 
16953 63 6f 6c 6c 61 74 69 6f 6e 73 20 73 65 71 75 65  collations seque
16954 6e 63 65 20 61 72 65 20 61 76 61 69 6c 61 62 6c  nce are availabl
16955 65 2c 20 6f 72 0a 2a 2a 20 61 6e 6f 74 68 65 72  e, or.** another
16956 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 4e   error occurs, N
16957 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20  ULL is returned 
16958 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  and an error mes
16959 73 61 67 65 20 77 72 69 74 74 65 6e 20 69 6e 74  sage written int
1695a 6f 0a 2a 2a 20 70 50 61 72 73 65 2e 0a 2a 2a 0a  o.** pParse..**.
1695b 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
1695c 69 73 20 61 20 77 72 61 70 70 65 72 20 61 72 6f  is a wrapper aro
1695d 75 6e 64 20 73 71 6c 69 74 65 33 46 69 6e 64 43  und sqlite3FindC
1695e 6f 6c 6c 53 65 71 28 29 2e 20 20 54 68 69 73 20  ollSeq().  This 
1695f 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 6e 76 6f 6b  routine.** invok
16960 65 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  es the collation
16961 20 66 61 63 74 6f 72 79 20 69 66 20 74 68 65 20   factory if the 
16962 6e 61 6d 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20  named collation 
16963 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 0a  cannot be found.
16964 2a 2a 20 61 6e 64 20 67 65 6e 65 72 61 74 65 73  ** and generates
16965 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
16966 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
16967 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71  VATE CollSeq *sq
16968 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53  lite3LocateCollS
16969 65 71 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  eq(Parse *pParse
1696a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  , const char *zN
1696b 61 6d 65 2c 20 69 6e 74 20 6e 4e 61 6d 65 29 7b  ame, int nName){
1696c 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
1696d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 75   pParse->db;.  u
1696e 38 20 65 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b  8 enc = ENC(db);
1696f 0a 20 20 75 38 20 69 6e 69 74 62 75 73 79 20 3d  .  u8 initbusy =
16970 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3b 0a   db->init.busy;.
16971 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
16972 3b 0a 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c  ;..  pColl = sql
16973 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28  ite3FindCollSeq(
16974 64 62 2c 20 65 6e 63 2c 20 7a 4e 61 6d 65 2c 20  db, enc, zName, 
16975 6e 4e 61 6d 65 2c 20 69 6e 69 74 62 75 73 79 29  nName, initbusy)
16976 3b 0a 20 20 69 66 28 20 21 69 6e 69 74 62 75 73  ;.  if( !initbus
16977 79 20 26 26 20 28 21 70 43 6f 6c 6c 20 7c 7c 20  y && (!pColl || 
16978 21 70 43 6f 6c 6c 2d 3e 78 43 6d 70 29 20 29 7b  !pColl->xCmp) ){
16979 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c  .    pColl = sql
1697a 69 74 65 33 47 65 74 43 6f 6c 6c 53 65 71 28 64  ite3GetCollSeq(d
1697b 62 2c 20 70 43 6f 6c 6c 2c 20 7a 4e 61 6d 65 2c  b, pColl, zName,
1697c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28   nName);.    if(
1697d 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20   !pColl ){.     
1697e 20 69 66 28 20 6e 4e 61 6d 65 3c 30 20 29 7b 0a   if( nName<0 ){.
1697f 20 20 20 20 20 20 20 20 6e 4e 61 6d 65 20 3d 20          nName = 
16980 73 74 72 6c 65 6e 28 7a 4e 61 6d 65 29 3b 0a 20  strlen(zName);. 
16981 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
16982 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
16983 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 63 6f  rse, "no such co
16984 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
16985 3a 20 25 2e 2a 73 22 2c 20 6e 4e 61 6d 65 2c 20  : %.*s", nName, 
16986 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 70 43  zName);.      pC
16987 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  oll = 0;.    }. 
16988 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 43 6f   }..  return pCo
16989 6c 6c 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65  ll;.}.../*.** Ge
1698a 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74  nerate code that
1698b 20 77 69 6c 6c 20 69 6e 63 72 65 6d 65 6e 74 20   will increment 
1698c 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69  the schema cooki
1698d 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 63 68  e..**.** The sch
1698e 65 6d 61 20 63 6f 6f 6b 69 65 20 69 73 20 75 73  ema cookie is us
1698f 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  ed to determine 
16990 77 68 65 6e 20 74 68 65 20 73 63 68 65 6d 61 20  when the schema 
16991 66 6f 72 20 74 68 65 0a 2a 2a 20 64 61 74 61 62  for the.** datab
16992 61 73 65 20 63 68 61 6e 67 65 73 2e 20 20 41 66  ase changes.  Af
16993 74 65 72 20 65 61 63 68 20 73 63 68 65 6d 61 20  ter each schema 
16994 63 68 61 6e 67 65 2c 20 74 68 65 20 63 6f 6f 6b  change, the cook
16995 69 65 20 76 61 6c 75 65 0a 2a 2a 20 63 68 61 6e  ie value.** chan
16996 67 65 73 2e 20 20 57 68 65 6e 20 61 20 70 72 6f  ges.  When a pro
16997 63 65 73 73 20 66 69 72 73 74 20 72 65 61 64 73  cess first reads
16998 20 74 68 65 20 73 63 68 65 6d 61 20 69 74 20 72   the schema it r
16999 65 63 6f 72 64 73 20 74 68 65 0a 2a 2a 20 63 6f  ecords the.** co
1699a 6f 6b 69 65 2e 20 20 54 68 65 72 65 61 66 74 65  okie.  Thereafte
1699b 72 2c 20 77 68 65 6e 65 76 65 72 20 69 74 20 67  r, whenever it g
1699c 6f 65 73 20 74 6f 20 61 63 63 65 73 73 20 74 68  oes to access th
1699d 65 20 64 61 74 61 62 61 73 65 2c 0a 2a 2a 20 69  e database,.** i
1699e 74 20 63 68 65 63 6b 73 20 74 68 65 20 63 6f 6f  t checks the coo
1699f 6b 69 65 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  kie to make sure
169a0 20 74 68 65 20 73 63 68 65 6d 61 20 68 61 73 20   the schema has 
169a1 6e 6f 74 20 63 68 61 6e 67 65 64 0a 2a 2a 20 73  not changed.** s
169a2 69 6e 63 65 20 69 74 20 77 61 73 20 6c 61 73 74  ince it was last
169a3 20 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69   read..**.** Thi
169a4 73 20 70 6c 61 6e 20 69 73 20 6e 6f 74 20 63 6f  s plan is not co
169a5 6d 70 6c 65 74 65 6c 79 20 62 75 6c 6c 65 74 2d  mpletely bullet-
169a6 70 72 6f 6f 66 2e 20 20 49 74 20 69 73 20 70 6f  proof.  It is po
169a7 73 73 69 62 6c 65 20 66 6f 72 0a 2a 2a 20 74 68  ssible for.** th
169a8 65 20 73 63 68 65 6d 61 20 74 6f 20 63 68 61 6e  e schema to chan
169a9 67 65 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65  ge multiple time
169aa 73 20 61 6e 64 20 66 6f 72 20 74 68 65 20 63 6f  s and for the co
169ab 6f 6b 69 65 20 74 6f 20 62 65 0a 2a 2a 20 73 65  okie to be.** se
169ac 74 20 62 61 63 6b 20 74 6f 20 70 72 69 6f 72 20  t back to prior 
169ad 76 61 6c 75 65 2e 20 20 42 75 74 20 73 63 68 65  value.  But sche
169ae 6d 61 20 63 68 61 6e 67 65 73 20 61 72 65 20 69  ma changes are i
169af 6e 66 72 65 71 75 65 6e 74 0a 2a 2a 20 61 6e 64  nfrequent.** and
169b0 20 74 68 65 20 70 72 6f 62 61 62 69 6c 69 74 79   the probability
169b1 20 6f 66 20 68 69 74 74 69 6e 67 20 74 68 65 20   of hitting the 
169b2 73 61 6d 65 20 63 6f 6f 6b 69 65 20 76 61 6c 75  same cookie valu
169b3 65 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 31 20 63  e is only.** 1 c
169b4 68 61 6e 63 65 20 69 6e 20 32 5e 33 32 2e 20 20  hance in 2^32.  
169b5 53 6f 20 77 65 27 72 65 20 73 61 66 65 20 65 6e  So we're safe en
169b6 6f 75 67 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ough..*/.SQLITE_
169b7 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
169b8 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65  ite3ChangeCookie
169b9 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 56 64  (sqlite3 *db, Vd
169ba 62 65 20 2a 76 2c 20 69 6e 74 20 69 44 62 29 7b  be *v, int iDb){
169bb 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
169bc 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  dOp(v, OP_Intege
169bd 72 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  r, db->aDb[iDb].
169be 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f  pSchema->schema_
169bf 63 6f 6f 6b 69 65 2b 31 2c 20 30 29 3b 0a 20 20  cookie+1, 0);.  
169c0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
169c1 28 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65  (v, OP_SetCookie
169c2 2c 20 69 44 62 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a  , iDb, 0);.}../*
169c3 0a 2a 2a 20 4d 65 61 73 75 72 65 20 74 68 65 20  .** Measure the 
169c4 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63  number of charac
169c5 74 65 72 73 20 6e 65 65 64 65 64 20 74 6f 20 6f  ters needed to o
169c6 75 74 70 75 74 20 74 68 65 20 67 69 76 65 6e 0a  utput the given.
169c7 2a 2a 20 69 64 65 6e 74 69 66 69 65 72 2e 20 20  ** identifier.  
169c8 54 68 65 20 6e 75 6d 62 65 72 20 72 65 74 75 72  The number retur
169c9 6e 65 64 20 69 6e 63 6c 75 64 65 73 20 61 6e 79  ned includes any
169ca 20 71 75 6f 74 65 73 20 75 73 65 64 0a 2a 2a 20   quotes used.** 
169cb 62 75 74 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63  but does not inc
169cc 6c 75 64 65 20 74 68 65 20 6e 75 6c 6c 20 74 65  lude the null te
169cd 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20  rminator..**.** 
169ce 54 68 65 20 65 73 74 69 6d 61 74 65 20 69 73 20  The estimate is 
169cf 63 6f 6e 73 65 72 76 61 74 69 76 65 2e 20 20 49  conservative.  I
169d0 74 20 6d 69 67 68 74 20 62 65 20 6c 61 72 67 65  t might be large
169d1 72 20 74 68 61 74 20 77 68 61 74 20 69 73 0a 2a  r that what is.*
169d2 2a 20 72 65 61 6c 6c 79 20 6e 65 65 64 65 64 2e  * really needed.
169d3 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .*/.static int i
169d4 64 65 6e 74 4c 65 6e 67 74 68 28 63 6f 6e 73 74  dentLength(const
169d5 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 6e 74   char *z){.  int
169d6 20 6e 3b 0a 20 20 66 6f 72 28 6e 3d 30 3b 20 2a   n;.  for(n=0; *
169d7 7a 3b 20 6e 2b 2b 2c 20 7a 2b 2b 29 7b 0a 20 20  z; n++, z++){.  
169d8 20 20 69 66 28 20 2a 7a 3d 3d 27 22 27 20 29 7b    if( *z=='"' ){
169d9 20 6e 2b 2b 3b 20 7d 0a 20 20 7d 0a 20 20 72 65   n++; }.  }.  re
169da 74 75 72 6e 20 6e 20 2b 20 32 3b 0a 7d 0a 0a 2f  turn n + 2;.}../
169db 2a 0a 2a 2a 20 57 72 69 74 65 20 61 6e 20 69 64  *.** Write an id
169dc 65 6e 74 69 66 69 65 72 20 6f 6e 74 6f 20 74 68  entifier onto th
169dd 65 20 65 6e 64 20 6f 66 20 74 68 65 20 67 69 76  e end of the giv
169de 65 6e 20 73 74 72 69 6e 67 2e 20 20 41 64 64 0a  en string.  Add.
169df 2a 2a 20 71 75 6f 74 65 20 63 68 61 72 61 63 74  ** quote charact
169e0 65 72 73 20 61 73 20 6e 65 65 64 65 64 2e 0a 2a  ers as needed..*
169e1 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 64  /.static void id
169e2 65 6e 74 50 75 74 28 63 68 61 72 20 2a 7a 2c 20  entPut(char *z, 
169e3 69 6e 74 20 2a 70 49 64 78 2c 20 63 68 61 72 20  int *pIdx, char 
169e4 2a 7a 53 69 67 6e 65 64 49 64 65 6e 74 29 7b 0a  *zSignedIdent){.
169e5 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
169e6 2a 7a 49 64 65 6e 74 20 3d 20 28 75 6e 73 69 67  *zIdent = (unsig
169e7 6e 65 64 20 63 68 61 72 2a 29 7a 53 69 67 6e 65  ned char*)zSigne
169e8 64 49 64 65 6e 74 3b 0a 20 20 69 6e 74 20 69 2c  dIdent;.  int i,
169e9 20 6a 2c 20 6e 65 65 64 51 75 6f 74 65 3b 0a 20   j, needQuote;. 
169ea 20 69 20 3d 20 2a 70 49 64 78 3b 0a 20 20 66 6f   i = *pIdx;.  fo
169eb 72 28 6a 3d 30 3b 20 7a 49 64 65 6e 74 5b 6a 5d  r(j=0; zIdent[j]
169ec 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; j++){.    if( 
169ed 21 69 73 61 6c 6e 75 6d 28 7a 49 64 65 6e 74 5b  !isalnum(zIdent[
169ee 6a 5d 29 20 26 26 20 7a 49 64 65 6e 74 5b 6a 5d  j]) && zIdent[j]
169ef 21 3d 27 5f 27 20 29 20 62 72 65 61 6b 3b 0a 20  !='_' ) break;. 
169f0 20 7d 0a 20 20 6e 65 65 64 51 75 6f 74 65 20 3d   }.  needQuote =
169f1 20 20 7a 49 64 65 6e 74 5b 6a 5d 21 3d 30 20 7c    zIdent[j]!=0 |
169f2 7c 20 69 73 64 69 67 69 74 28 7a 49 64 65 6e 74  | isdigit(zIdent
169f3 5b 30 5d 29 0a 20 20 20 20 20 20 20 20 20 20 20  [0]).           
169f4 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65         || sqlite
169f5 33 4b 65 79 77 6f 72 64 43 6f 64 65 28 7a 49 64  3KeywordCode(zId
169f6 65 6e 74 2c 20 6a 29 21 3d 54 4b 5f 49 44 3b 0a  ent, j)!=TK_ID;.
169f7 20 20 69 66 28 20 6e 65 65 64 51 75 6f 74 65 20    if( needQuote 
169f8 29 20 7a 5b 69 2b 2b 5d 20 3d 20 27 22 27 3b 0a  ) z[i++] = '"';.
169f9 20 20 66 6f 72 28 6a 3d 30 3b 20 7a 49 64 65 6e    for(j=0; zIden
169fa 74 5b 6a 5d 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20  t[j]; j++){.    
169fb 7a 5b 69 2b 2b 5d 20 3d 20 7a 49 64 65 6e 74 5b  z[i++] = zIdent[
169fc 6a 5d 3b 0a 20 20 20 20 69 66 28 20 7a 49 64 65  j];.    if( zIde
169fd 6e 74 5b 6a 5d 3d 3d 27 22 27 20 29 20 7a 5b 69  nt[j]=='"' ) z[i
169fe 2b 2b 5d 20 3d 20 27 22 27 3b 0a 20 20 7d 0a 20  ++] = '"';.  }. 
169ff 20 69 66 28 20 6e 65 65 64 51 75 6f 74 65 20 29   if( needQuote )
16a00 20 7a 5b 69 2b 2b 5d 20 3d 20 27 22 27 3b 0a 20   z[i++] = '"';. 
16a01 20 7a 5b 69 5d 20 3d 20 30 3b 0a 20 20 2a 70 49   z[i] = 0;.  *pI
16a02 64 78 20 3d 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  dx = i;.}../*.**
16a03 20 47 65 6e 65 72 61 74 65 20 61 20 43 52 45 41   Generate a CREA
16a04 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65  TE TABLE stateme
16a05 6e 74 20 61 70 70 72 6f 70 72 69 61 74 65 20 66  nt appropriate f
16a06 6f 72 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20  or the given.** 
16a07 74 61 62 6c 65 2e 20 20 4d 65 6d 6f 72 79 20 74  table.  Memory t
16a08 6f 20 68 6f 6c 64 20 74 68 65 20 74 65 78 74 20  o hold the text 
16a09 6f 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  of the statement
16a0a 20 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20   is obtained.** 
16a0b 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f  from sqliteMallo
16a0c 63 28 29 20 61 6e 64 20 6d 75 73 74 20 62 65 20  c() and must be 
16a0d 66 72 65 65 64 20 62 79 20 74 68 65 20 63 61 6c  freed by the cal
16a0e 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  ling function..*
16a0f 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 63  /.static char *c
16a10 72 65 61 74 65 54 61 62 6c 65 53 74 6d 74 28 54  reateTableStmt(T
16a11 61 62 6c 65 20 2a 70 2c 20 69 6e 74 20 69 73 54  able *p, int isT
16a12 65 6d 70 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6b  emp){.  int i, k
16a13 2c 20 6e 3b 0a 20 20 63 68 61 72 20 2a 7a 53 74  , n;.  char *zSt
16a14 6d 74 3b 0a 20 20 63 68 61 72 20 2a 7a 53 65 70  mt;.  char *zSep
16a15 2c 20 2a 7a 53 65 70 32 2c 20 2a 7a 45 6e 64 2c  , *zSep2, *zEnd,
16a16 20 2a 7a 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70   *z;.  Column *p
16a17 43 6f 6c 3b 0a 20 20 6e 20 3d 20 30 3b 0a 20 20  Col;.  n = 0;.  
16a18 66 6f 72 28 70 43 6f 6c 20 3d 20 70 2d 3e 61 43  for(pCol = p->aC
16a19 6f 6c 2c 20 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43  ol, i=0; i<p->nC
16a1a 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29  ol; i++, pCol++)
16a1b 7b 0a 20 20 20 20 6e 20 2b 3d 20 69 64 65 6e 74  {.    n += ident
16a1c 4c 65 6e 67 74 68 28 70 43 6f 6c 2d 3e 7a 4e 61  Length(pCol->zNa
16a1d 6d 65 29 3b 0a 20 20 20 20 7a 20 3d 20 70 43 6f  me);.    z = pCo
16a1e 6c 2d 3e 7a 54 79 70 65 3b 0a 20 20 20 20 69 66  l->zType;.    if
16a1f 28 20 7a 20 29 7b 0a 20 20 20 20 20 20 6e 20 2b  ( z ){.      n +
16a20 3d 20 28 73 74 72 6c 65 6e 28 7a 29 20 2b 20 31  = (strlen(z) + 1
16a21 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6e  );.    }.  }.  n
16a22 20 2b 3d 20 69 64 65 6e 74 4c 65 6e 67 74 68 28   += identLength(
16a23 70 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28  p->zName);.  if(
16a24 20 6e 3c 35 30 20 29 7b 0a 20 20 20 20 7a 53 65   n<50 ){.    zSe
16a25 70 20 3d 20 22 22 3b 0a 20 20 20 20 7a 53 65 70  p = "";.    zSep
16a26 32 20 3d 20 22 2c 22 3b 0a 20 20 20 20 7a 45 6e  2 = ",";.    zEn
16a27 64 20 3d 20 22 29 22 3b 0a 20 20 7d 65 6c 73 65  d = ")";.  }else
16a28 7b 0a 20 20 20 20 7a 53 65 70 20 3d 20 22 5c 6e  {.    zSep = "\n
16a29 20 20 22 3b 0a 20 20 20 20 7a 53 65 70 32 20 3d    ";.    zSep2 =
16a2a 20 22 2c 5c 6e 20 20 22 3b 0a 20 20 20 20 7a 45   ",\n  ";.    zE
16a2b 6e 64 20 3d 20 22 5c 6e 29 22 3b 0a 20 20 7d 0a  nd = "\n)";.  }.
16a2c 20 20 6e 20 2b 3d 20 33 35 20 2b 20 36 2a 70 2d    n += 35 + 6*p-
16a2d 3e 6e 43 6f 6c 3b 0a 20 20 7a 53 74 6d 74 20 3d  >nCol;.  zStmt =
16a2e 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 52 61 77   sqliteMallocRaw
16a2f 28 20 6e 20 29 3b 0a 20 20 69 66 28 20 7a 53 74  ( n );.  if( zSt
16a30 6d 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  mt==0 ) return 0
16a31 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72  ;.  sqlite3_snpr
16a32 69 6e 74 66 28 6e 2c 20 7a 53 74 6d 74 2c 0a 20  intf(n, zStmt,. 
16a33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a34 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 26 26 69   !OMIT_TEMPDB&&i
16a35 73 54 65 6d 70 20 3f 20 22 43 52 45 41 54 45 20  sTemp ? "CREATE 
16a36 54 45 4d 50 20 54 41 42 4c 45 20 22 3a 22 43 52  TEMP TABLE ":"CR
16a37 45 41 54 45 20 54 41 42 4c 45 20 22 29 3b 0a 20  EATE TABLE ");. 
16a38 20 6b 20 3d 20 73 74 72 6c 65 6e 28 7a 53 74 6d   k = strlen(zStm
16a39 74 29 3b 0a 20 20 69 64 65 6e 74 50 75 74 28 7a  t);.  identPut(z
16a3a 53 74 6d 74 2c 20 26 6b 2c 20 70 2d 3e 7a 4e 61  Stmt, &k, p->zNa
16a3b 6d 65 29 3b 0a 20 20 7a 53 74 6d 74 5b 6b 2b 2b  me);.  zStmt[k++
16a3c 5d 20 3d 20 27 28 27 3b 0a 20 20 66 6f 72 28 70  ] = '(';.  for(p
16a3d 43 6f 6c 3d 70 2d 3e 61 43 6f 6c 2c 20 69 3d 30  Col=p->aCol, i=0
16a3e 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b  ; i<p->nCol; i++
16a3f 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 73  , pCol++){.    s
16a40 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
16a41 6e 2d 6b 2c 20 26 7a 53 74 6d 74 5b 6b 5d 2c 20  n-k, &zStmt[k], 
16a42 7a 53 65 70 29 3b 0a 20 20 20 20 6b 20 2b 3d 20  zSep);.    k += 
16a43 73 74 72 6c 65 6e 28 26 7a 53 74 6d 74 5b 6b 5d  strlen(&zStmt[k]
16a44 29 3b 0a 20 20 20 20 7a 53 65 70 20 3d 20 7a 53  );.    zSep = zS
16a45 65 70 32 3b 0a 20 20 20 20 69 64 65 6e 74 50 75  ep2;.    identPu
16a46 74 28 7a 53 74 6d 74 2c 20 26 6b 2c 20 70 43 6f  t(zStmt, &k, pCo
16a47 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 69  l->zName);.    i
16a48 66 28 20 28 7a 20 3d 20 70 43 6f 6c 2d 3e 7a 54  f( (z = pCol->zT
16a49 79 70 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  ype)!=0 ){.     
16a4a 20 7a 53 74 6d 74 5b 6b 2b 2b 5d 20 3d 20 27 20   zStmt[k++] = ' 
16a4b 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ';.      assert(
16a4c 20 73 74 72 6c 65 6e 28 7a 29 2b 6b 2b 31 3c 3d   strlen(z)+k+1<=
16a4d 6e 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  n );.      sqlit
16a4e 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 2d 6b 2c  e3_snprintf(n-k,
16a4f 20 26 7a 53 74 6d 74 5b 6b 5d 2c 20 22 25 73 22   &zStmt[k], "%s"
16a50 2c 20 7a 29 3b 0a 20 20 20 20 20 20 6b 20 2b 3d  , z);.      k +=
16a51 20 73 74 72 6c 65 6e 28 7a 29 3b 0a 20 20 20 20   strlen(z);.    
16a52 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  }.  }.  sqlite3_
16a53 73 6e 70 72 69 6e 74 66 28 6e 2d 6b 2c 20 26 7a  snprintf(n-k, &z
16a54 53 74 6d 74 5b 6b 5d 2c 20 22 25 73 22 2c 20 7a  Stmt[k], "%s", z
16a55 45 6e 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a  End);.  return z
16a56 53 74 6d 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  Stmt;.}../*.** T
16a57 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
16a58 61 6c 6c 65 64 20 74 6f 20 72 65 70 6f 72 74 20  alled to report 
16a59 74 68 65 20 66 69 6e 61 6c 20 22 29 22 20 74 68  the final ")" th
16a5a 61 74 20 74 65 72 6d 69 6e 61 74 65 73 0a 2a 2a  at terminates.**
16a5b 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20   a CREATE TABLE 
16a5c 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a  statement..**.**
16a5d 20 54 68 65 20 74 61 62 6c 65 20 73 74 72 75 63   The table struc
16a5e 74 75 72 65 20 74 68 61 74 20 6f 74 68 65 72 20  ture that other 
16a5f 61 63 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20  action routines 
16a60 68 61 76 65 20 62 65 65 6e 20 62 75 69 6c 64 69  have been buildi
16a61 6e 67 0a 2a 2a 20 69 73 20 61 64 64 65 64 20 74  ng.** is added t
16a62 6f 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 68  o the internal h
16a63 61 73 68 20 74 61 62 6c 65 73 2c 20 61 73 73 75  ash tables, assu
16a64 6d 69 6e 67 20 6e 6f 20 65 72 72 6f 72 73 20 68  ming no errors h
16a65 61 76 65 0a 2a 2a 20 6f 63 63 75 72 72 65 64 2e  ave.** occurred.
16a66 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 6e 74 72 79 20  .**.** An entry 
16a67 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 69 73  for the table is
16a68 20 6d 61 64 65 20 69 6e 20 74 68 65 20 6d 61 73   made in the mas
16a69 74 65 72 20 74 61 62 6c 65 20 6f 6e 20 64 69 73  ter table on dis
16a6a 6b 2c 20 75 6e 6c 65 73 73 0a 2a 2a 20 74 68 69  k, unless.** thi
16a6b 73 20 69 73 20 61 20 74 65 6d 70 6f 72 61 72 79  s is a temporary
16a6c 20 74 61 62 6c 65 20 6f 72 20 64 62 2d 3e 69 6e   table or db->in
16a6d 69 74 2e 62 75 73 79 3d 3d 31 2e 20 20 57 68 65  it.busy==1.  Whe
16a6e 6e 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d  n db->init.busy=
16a6f 3d 31 0a 2a 2a 20 69 74 20 6d 65 61 6e 73 20 77  =1.** it means w
16a70 65 20 61 72 65 20 72 65 61 64 69 6e 67 20 74 68  e are reading th
16a71 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  e sqlite_master 
16a72 74 61 62 6c 65 20 62 65 63 61 75 73 65 20 77 65  table because we
16a73 20 6a 75 73 74 0a 2a 2a 20 63 6f 6e 6e 65 63 74   just.** connect
16a74 65 64 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ed to the databa
16a75 73 65 20 6f 72 20 62 65 63 61 75 73 65 20 74 68  se or because th
16a76 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  e sqlite_master 
16a77 74 61 62 6c 65 20 68 61 73 0a 2a 2a 20 72 65 63  table has.** rec
16a78 65 6e 74 6c 79 20 63 68 61 6e 67 65 64 2c 20 73  ently changed, s
16a79 6f 20 74 68 65 20 65 6e 74 72 79 20 66 6f 72 20  o the entry for 
16a7a 74 68 69 73 20 74 61 62 6c 65 20 61 6c 72 65 61  this table alrea
16a7b 64 79 20 65 78 69 73 74 73 20 69 6e 0a 2a 2a 20  dy exists in.** 
16a7c 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65  the sqlite_maste
16a7d 72 20 74 61 62 6c 65 2e 20 20 57 65 20 64 6f 20  r table.  We do 
16a7e 6e 6f 74 20 77 61 6e 74 20 74 6f 20 63 72 65 61  not want to crea
16a7f 74 65 20 69 74 20 61 67 61 69 6e 2e 0a 2a 2a 0a  te it again..**.
16a80 2a 2a 20 49 66 20 74 68 65 20 70 53 65 6c 65 63  ** If the pSelec
16a81 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f  t argument is no
16a82 74 20 4e 55 4c 4c 2c 20 69 74 20 6d 65 61 6e 73  t NULL, it means
16a83 20 74 68 61 74 20 74 68 69 73 20 72 6f 75 74 69   that this routi
16a84 6e 65 0a 2a 2a 20 77 61 73 20 63 61 6c 6c 65 64  ne.** was called
16a85 20 74 6f 20 63 72 65 61 74 65 20 61 20 74 61 62   to create a tab
16a86 6c 65 20 67 65 6e 65 72 61 74 65 64 20 66 72 6f  le generated fro
16a87 6d 20 61 20 0a 2a 2a 20 22 43 52 45 41 54 45 20  m a .** "CREATE 
16a88 54 41 42 4c 45 20 2e 2e 2e 20 41 53 20 53 45 4c  TABLE ... AS SEL
16a89 45 43 54 20 2e 2e 2e 22 20 73 74 61 74 65 6d 65  ECT ..." stateme
16a8a 6e 74 2e 20 20 54 68 65 20 63 6f 6c 75 6d 6e 20  nt.  The column 
16a8b 6e 61 6d 65 73 20 6f 66 0a 2a 2a 20 74 68 65 20  names of.** the 
16a8c 6e 65 77 20 74 61 62 6c 65 20 77 69 6c 6c 20 6d  new table will m
16a8d 61 74 63 68 20 74 68 65 20 72 65 73 75 6c 74 20  atch the result 
16a8e 73 65 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43  set of the SELEC
16a8f 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  T..*/.SQLITE_PRI
16a90 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
16a91 33 45 6e 64 54 61 62 6c 65 28 0a 20 20 50 61 72  3EndTable(.  Par
16a92 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
16a93 20 20 20 20 20 2f 2a 20 50 61 72 73 65 20 63 6f       /* Parse co
16a94 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e  ntext */.  Token
16a95 20 2a 70 43 6f 6e 73 2c 20 20 20 20 20 20 20 20   *pCons,        
16a96 20 20 20 2f 2a 20 54 68 65 20 27 2c 27 20 74 6f     /* The ',' to
16a97 6b 65 6e 20 61 66 74 65 72 20 74 68 65 20 6c 61  ken after the la
16a98 73 74 20 63 6f 6c 75 6d 6e 20 64 65 66 6e 2e 20  st column defn. 
16a99 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 45 6e 64  */.  Token *pEnd
16a9a 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
16a9b 54 68 65 20 66 69 6e 61 6c 20 27 29 27 20 74 6f  The final ')' to
16a9c 6b 65 6e 20 69 6e 20 74 68 65 20 43 52 45 41 54  ken in the CREAT
16a9d 45 20 54 41 42 4c 45 20 2a 2f 0a 20 20 53 65 6c  E TABLE */.  Sel
16a9e 65 63 74 20 2a 70 53 65 6c 65 63 74 20 20 20 20  ect *pSelect    
16a9f 20 20 20 20 20 2f 2a 20 53 65 6c 65 63 74 20 66       /* Select f
16aa0 72 6f 6d 20 61 20 22 43 52 45 41 54 45 20 2e 2e  rom a "CREATE ..
16aa1 2e 20 41 53 20 53 45 4c 45 43 54 22 20 2a 2f 0a  . AS SELECT" */.
16aa2 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20  ){.  Table *p;. 
16aa3 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
16aa4 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74  Parse->db;.  int
16aa5 20 69 44 62 3b 0a 0a 20 20 69 66 28 20 28 70 45   iDb;..  if( (pE
16aa6 6e 64 3d 3d 30 20 26 26 20 70 53 65 6c 65 63 74  nd==0 && pSelect
16aa7 3d 3d 30 29 20 7c 7c 20 70 50 61 72 73 65 2d 3e  ==0) || pParse->
16aa8 6e 45 72 72 20 7c 7c 20 73 71 6c 69 74 65 33 4d  nErr || sqlite3M
16aa9 61 6c 6c 6f 63 46 61 69 6c 65 64 28 29 20 29 20  allocFailed() ) 
16aaa 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
16aab 7d 0a 20 20 70 20 3d 20 70 50 61 72 73 65 2d 3e  }.  p = pParse->
16aac 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69 66 28  pNewTable;.  if(
16aad 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a   p==0 ) return;.
16aae 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e  .  assert( !db->
16aaf 69 6e 69 74 2e 62 75 73 79 20 7c 7c 20 21 70 53  init.busy || !pS
16ab0 65 6c 65 63 74 20 29 3b 0a 0a 20 20 69 44 62 20  elect );..  iDb 
16ab1 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
16ab2 6f 49 6e 64 65 78 28 64 62 2c 20 70 2d 3e 70 53  oIndex(db, p->pS
16ab3 63 68 65 6d 61 29 3b 0a 0a 23 69 66 6e 64 65 66  chema);..#ifndef
16ab4 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45   SQLITE_OMIT_CHE
16ab5 43 4b 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20  CK.  /* Resolve 
16ab6 6e 61 6d 65 73 20 69 6e 20 61 6c 6c 20 43 48 45  names in all CHE
16ab7 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 65 78  CK constraint ex
16ab8 70 72 65 73 73 69 6f 6e 73 2e 0a 20 20 2a 2f 0a  pressions..  */.
16ab9 20 20 69 66 28 20 70 2d 3e 70 43 68 65 63 6b 20    if( p->pCheck 
16aba 29 7b 0a 20 20 20 20 53 72 63 4c 69 73 74 20 73  ){.    SrcList s
16abb 53 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  Src;            
16abc 20 20 20 20 20 20 20 2f 2a 20 46 61 6b 65 20 53         /* Fake S
16abd 72 63 4c 69 73 74 20 66 6f 72 20 70 50 61 72 73  rcList for pPars
16abe 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 2a 2f 0a  e->pNewTable */.
16abf 20 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20      NameContext 
16ac0 73 4e 43 3b 20 20 20 20 20 20 20 20 20 20 20 20  sNC;            
16ac1 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74      /* Name cont
16ac2 65 78 74 20 66 6f 72 20 70 50 61 72 73 65 2d 3e  ext for pParse->
16ac3 70 4e 65 77 54 61 62 6c 65 20 2a 2f 0a 0a 20 20  pNewTable */..  
16ac4 20 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30    memset(&sNC, 0
16ac5 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a  , sizeof(sNC));.
16ac6 20 20 20 20 6d 65 6d 73 65 74 28 26 73 53 72 63      memset(&sSrc
16ac7 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 53 72 63  , 0, sizeof(sSrc
16ac8 29 29 3b 0a 20 20 20 20 73 53 72 63 2e 6e 53 72  ));.    sSrc.nSr
16ac9 63 20 3d 20 31 3b 0a 20 20 20 20 73 53 72 63 2e  c = 1;.    sSrc.
16aca 61 5b 30 5d 2e 7a 4e 61 6d 65 20 3d 20 70 2d 3e  a[0].zName = p->
16acb 7a 4e 61 6d 65 3b 0a 20 20 20 20 73 53 72 63 2e  zName;.    sSrc.
16acc 61 5b 30 5d 2e 70 54 61 62 20 3d 20 70 3b 0a 20  a[0].pTab = p;. 
16acd 20 20 20 73 53 72 63 2e 61 5b 30 5d 2e 69 43 75     sSrc.a[0].iCu
16ace 72 73 6f 72 20 3d 20 2d 31 3b 0a 20 20 20 20 73  rsor = -1;.    s
16acf 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72  NC.pParse = pPar
16ad0 73 65 3b 0a 20 20 20 20 73 4e 43 2e 70 53 72 63  se;.    sNC.pSrc
16ad1 4c 69 73 74 20 3d 20 26 73 53 72 63 3b 0a 20 20  List = &sSrc;.  
16ad2 20 20 73 4e 43 2e 69 73 43 68 65 63 6b 20 3d 20    sNC.isCheck = 
16ad3 31 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  1;.    if( sqlit
16ad4 65 33 45 78 70 72 52 65 73 6f 6c 76 65 4e 61 6d  e3ExprResolveNam
16ad5 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70 43 68 65  es(&sNC, p->pChe
16ad6 63 6b 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ck) ){.      ret
16ad7 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23  urn;.    }.  }.#
16ad8 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65  endif /* !define
16ad9 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48  d(SQLITE_OMIT_CH
16ada 45 43 4b 29 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66  ECK) */..  /* If
16adb 20 74 68 65 20 64 62 2d 3e 69 6e 69 74 2e 62 75   the db->init.bu
16adc 73 79 20 69 73 20 31 20 69 74 20 6d 65 61 6e 73  sy is 1 it means
16add 20 77 65 20 61 72 65 20 72 65 61 64 69 6e 67 20   we are reading 
16ade 74 68 65 20 53 51 4c 20 6f 66 66 20 74 68 65 0a  the SQL off the.
16adf 20 20 2a 2a 20 22 73 71 6c 69 74 65 5f 6d 61 73    ** "sqlite_mas
16ae0 74 65 72 22 20 6f 72 20 22 73 71 6c 69 74 65 5f  ter" or "sqlite_
16ae1 74 65 6d 70 5f 6d 61 73 74 65 72 22 20 74 61 62  temp_master" tab
16ae2 6c 65 20 6f 6e 20 74 68 65 20 64 69 73 6b 2e 0a  le on the disk..
16ae3 20 20 2a 2a 20 53 6f 20 64 6f 20 6e 6f 74 20 77    ** So do not w
16ae4 72 69 74 65 20 74 6f 20 74 68 65 20 64 69 73 6b  rite to the disk
16ae5 20 61 67 61 69 6e 2e 20 20 45 78 74 72 61 63 74   again.  Extract
16ae6 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e   the root page n
16ae7 75 6d 62 65 72 0a 20 20 2a 2a 20 66 6f 72 20 74  umber.  ** for t
16ae8 68 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 74 68  he table from th
16ae9 65 20 64 62 2d 3e 69 6e 69 74 2e 6e 65 77 54 6e  e db->init.newTn
16aea 75 6d 20 66 69 65 6c 64 2e 20 20 28 54 68 65 20  um field.  (The 
16aeb 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a 2a  page number.  **
16aec 20 73 68 6f 75 6c 64 20 68 61 76 65 20 62 65 65   should have bee
16aed 6e 20 70 75 74 20 74 68 65 72 65 20 62 79 20 74  n put there by t
16aee 68 65 20 73 71 6c 69 74 65 4f 70 65 6e 43 62 20  he sqliteOpenCb 
16aef 72 6f 75 74 69 6e 65 2e 29 0a 20 20 2a 2f 0a 20  routine.).  */. 
16af0 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75   if( db->init.bu
16af1 73 79 20 29 7b 0a 20 20 20 20 70 2d 3e 74 6e 75  sy ){.    p->tnu
16af2 6d 20 3d 20 64 62 2d 3e 69 6e 69 74 2e 6e 65 77  m = db->init.new
16af3 54 6e 75 6d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  Tnum;.  }..  /* 
16af4 49 66 20 6e 6f 74 20 69 6e 69 74 69 61 6c 69 7a  If not initializ
16af5 69 6e 67 2c 20 74 68 65 6e 20 63 72 65 61 74 65  ing, then create
16af6 20 61 20 72 65 63 6f 72 64 20 66 6f 72 20 74 68   a record for th
16af7 65 20 6e 65 77 20 74 61 62 6c 65 0a 20 20 2a 2a  e new table.  **
16af8 20 69 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 4d   in the SQLITE_M
16af9 41 53 54 45 52 20 74 61 62 6c 65 20 6f 66 20 74  ASTER table of t
16afa 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68  he database.  Th
16afb 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 0a  e record number.
16afc 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 6e 65 77    ** for the new
16afd 20 74 61 62 6c 65 20 65 6e 74 72 79 20 73 68 6f   table entry sho
16afe 75 6c 64 20 61 6c 72 65 61 64 79 20 62 65 20 6f  uld already be o
16aff 6e 20 74 68 65 20 73 74 61 63 6b 2e 0a 20 20 2a  n the stack..  *
16b00 2a 0a 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69  *.  ** If this i
16b01 73 20 61 20 54 45 4d 50 4f 52 41 52 59 20 74 61  s a TEMPORARY ta
16b02 62 6c 65 2c 20 77 72 69 74 65 20 74 68 65 20 65  ble, write the e
16b03 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 20 61 75  ntry into the au
16b04 78 69 6c 69 61 72 79 0a 20 20 2a 2a 20 66 69 6c  xiliary.  ** fil
16b05 65 20 69 6e 73 74 65 61 64 20 6f 66 20 69 6e 74  e instead of int
16b06 6f 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  o the main datab
16b07 61 73 65 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20  ase file..  */. 
16b08 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62   if( !db->init.b
16b09 75 73 79 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e  usy ){.    int n
16b0a 3b 0a 20 20 20 20 56 64 62 65 20 2a 76 3b 0a 20  ;.    Vdbe *v;. 
16b0b 20 20 20 63 68 61 72 20 2a 7a 54 79 70 65 3b 20     char *zType; 
16b0c 20 20 20 2f 2a 20 22 76 69 65 77 22 20 6f 72 20     /* "view" or 
16b0d 22 74 61 62 6c 65 22 20 2a 2f 0a 20 20 20 20 63  "table" */.    c
16b0e 68 61 72 20 2a 7a 54 79 70 65 32 3b 20 20 20 2f  har *zType2;   /
16b0f 2a 20 22 56 49 45 57 22 20 6f 72 20 22 54 41 42  * "VIEW" or "TAB
16b10 4c 45 22 20 2a 2f 0a 20 20 20 20 63 68 61 72 20  LE" */.    char 
16b11 2a 7a 53 74 6d 74 3b 20 20 20 20 2f 2a 20 54 65  *zStmt;    /* Te
16b12 78 74 20 6f 66 20 74 68 65 20 43 52 45 41 54 45  xt of the CREATE
16b13 20 54 41 42 4c 45 20 6f 72 20 43 52 45 41 54 45   TABLE or CREATE
16b14 20 56 49 45 57 20 73 74 61 74 65 6d 65 6e 74 20   VIEW statement 
16b15 2a 2f 0a 0a 20 20 20 20 76 20 3d 20 73 71 6c 69  */..    v = sqli
16b16 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
16b17 65 29 3b 0a 20 20 20 20 69 66 28 20 76 3d 3d 30  e);.    if( v==0
16b18 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 20 20   ) return;..    
16b19 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
16b1a 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 30 2c  (v, OP_Close, 0,
16b1b 20 30 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 72 65   0);..    /* Cre
16b1c 61 74 65 20 74 68 65 20 72 6f 6f 74 70 61 67 65  ate the rootpage
16b1d 20 66 6f 72 20 74 68 65 20 6e 65 77 20 74 61 62   for the new tab
16b1e 6c 65 20 61 6e 64 20 70 75 73 68 20 69 74 20 6f  le and push it o
16b1f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 2e 0a 20  nto the stack.. 
16b20 20 20 20 2a 2a 20 41 20 76 69 65 77 20 68 61 73     ** A view has
16b21 20 6e 6f 20 72 6f 6f 74 70 61 67 65 2c 20 73 6f   no rootpage, so
16b22 20 6a 75 73 74 20 70 75 73 68 20 61 20 7a 65 72   just push a zer
16b23 6f 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b  o onto the stack
16b24 20 66 6f 72 0a 20 20 20 20 2a 2a 20 76 69 65 77   for.    ** view
16b25 73 2e 20 20 49 6e 69 74 69 61 6c 69 7a 65 20 7a  s.  Initialize z
16b26 54 79 70 65 20 61 74 20 74 68 65 20 73 61 6d 65  Type at the same
16b27 20 74 69 6d 65 2e 0a 20 20 20 20 2a 2f 0a 20 20   time..    */.  
16b28 20 20 69 66 28 20 70 2d 3e 70 53 65 6c 65 63 74    if( p->pSelect
16b29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ==0 ){.      /* 
16b2a 41 20 72 65 67 75 6c 61 72 20 74 61 62 6c 65 20  A regular table 
16b2b 2a 2f 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d  */.      zType =
16b2c 20 22 74 61 62 6c 65 22 3b 0a 20 20 20 20 20 20   "table";.      
16b2d 7a 54 79 70 65 32 20 3d 20 22 54 41 42 4c 45 22  zType2 = "TABLE"
16b2e 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
16b2f 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 20 20 7d  _OMIT_VIEW.    }
16b30 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41  else{.      /* A
16b31 20 76 69 65 77 20 2a 2f 0a 20 20 20 20 20 20 7a   view */.      z
16b32 54 79 70 65 20 3d 20 22 76 69 65 77 22 3b 0a 20  Type = "view";. 
16b33 20 20 20 20 20 7a 54 79 70 65 32 20 3d 20 22 56       zType2 = "V
16b34 49 45 57 22 3b 0a 23 65 6e 64 69 66 0a 20 20 20  IEW";.#endif.   
16b35 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68   }..    /* If th
16b36 69 73 20 69 73 20 61 20 43 52 45 41 54 45 20 54  is is a CREATE T
16b37 41 42 4c 45 20 78 78 20 41 53 20 53 45 4c 45 43  ABLE xx AS SELEC
16b38 54 20 2e 2e 2e 2c 20 65 78 65 63 75 74 65 20 74  T ..., execute t
16b39 68 65 20 53 45 4c 45 43 54 0a 20 20 20 20 2a 2a  he SELECT.    **
16b3a 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 70 6f   statement to po
16b3b 70 75 6c 61 74 65 20 74 68 65 20 6e 65 77 20 74  pulate the new t
16b3c 61 62 6c 65 2e 20 54 68 65 20 72 6f 6f 74 2d 70  able. The root-p
16b3d 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  age number for t
16b3e 68 65 0a 20 20 20 20 2a 2a 20 6e 65 77 20 74 61  he.    ** new ta
16b3f 62 6c 65 20 69 73 20 6f 6e 20 74 68 65 20 74 6f  ble is on the to
16b40 70 20 6f 66 20 74 68 65 20 76 64 62 65 20 73 74  p of the vdbe st
16b41 61 63 6b 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ack..    **.    
16b42 2a 2a 20 4f 6e 63 65 20 74 68 65 20 53 45 4c 45  ** Once the SELE
16b43 43 54 20 68 61 73 20 62 65 65 6e 20 63 6f 64 65  CT has been code
16b44 64 20 62 79 20 73 71 6c 69 74 65 33 53 65 6c 65  d by sqlite3Sele
16b45 63 74 28 29 2c 20 69 74 20 69 73 20 69 6e 20 61  ct(), it is in a
16b46 0a 20 20 20 20 2a 2a 20 73 75 69 74 61 62 6c 65  .    ** suitable
16b47 20 73 74 61 74 65 20 74 6f 20 71 75 65 72 79 20   state to query 
16b48 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e  for the column n
16b49 61 6d 65 73 20 61 6e 64 20 74 79 70 65 73 20 74  ames and types t
16b4a 6f 20 62 65 20 75 73 65 64 0a 20 20 20 20 2a 2a  o be used.    **
16b4b 20 62 79 20 74 68 65 20 6e 65 77 20 74 61 62 6c   by the new tabl
16b4c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  e..    **.    **
16b4d 20 41 20 73 68 61 72 65 64 2d 63 61 63 68 65 20   A shared-cache 
16b4e 77 72 69 74 65 2d 6c 6f 63 6b 20 69 73 20 6e 6f  write-lock is no
16b4f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 77 72  t required to wr
16b50 69 74 65 20 74 6f 20 74 68 65 20 6e 65 77 20 74  ite to the new t
16b51 61 62 6c 65 2c 0a 20 20 20 20 2a 2a 20 61 73 20  able,.    ** as 
16b52 61 20 73 63 68 65 6d 61 2d 6c 6f 63 6b 20 6d 75  a schema-lock mu
16b53 73 74 20 68 61 76 65 20 61 6c 72 65 61 64 79 20  st have already 
16b54 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 74 6f  been obtained to
16b55 20 63 72 65 61 74 65 20 69 74 2e 20 53 69 6e 63   create it. Sinc
16b56 65 0a 20 20 20 20 2a 2a 20 61 20 73 63 68 65 6d  e.    ** a schem
16b57 61 2d 6c 6f 63 6b 20 65 78 63 6c 75 64 65 73 20  a-lock excludes 
16b58 61 6c 6c 20 6f 74 68 65 72 20 64 61 74 61 62 61  all other databa
16b59 73 65 20 75 73 65 72 73 2c 20 74 68 65 20 77 72  se users, the wr
16b5a 69 74 65 2d 6c 6f 63 6b 20 77 6f 75 6c 64 0a 20  ite-lock would. 
16b5b 20 20 20 2a 2a 20 62 65 20 72 65 64 75 6e 64 61     ** be redunda
16b5c 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  nt..    */.    i
16b5d 66 28 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20  f( pSelect ){.  
16b5e 20 20 20 20 54 61 62 6c 65 20 2a 70 53 65 6c 54      Table *pSelT
16b5f 61 62 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ab;.      sqlite
16b60 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
16b61 5f 44 75 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20  _Dup, 0, 0);.   
16b62 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
16b63 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  dOp(v, OP_Intege
16b64 72 2c 20 69 44 62 2c 20 30 29 3b 0a 20 20 20 20  r, iDb, 0);.    
16b65 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
16b66 4f 70 28 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69  Op(v, OP_OpenWri
16b67 74 65 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 20  te, 1, 0);.     
16b68 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 20 3d 20   pParse->nTab = 
16b69 32 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  2;.      sqlite3
16b6a 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
16b6b 53 65 6c 65 63 74 2c 20 53 52 54 5f 54 61 62 6c  Select, SRT_Tabl
16b6c 65 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30  e, 1, 0, 0, 0, 0
16b6d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
16b6e 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
16b6f 43 6c 6f 73 65 2c 20 31 2c 20 30 29 3b 0a 20 20  Close, 1, 0);.  
16b70 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e      if( pParse->
16b71 6e 45 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20  nErr==0 ){.     
16b72 20 20 20 70 53 65 6c 54 61 62 20 3d 20 73 71 6c     pSelTab = sql
16b73 69 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53  ite3ResultSetOfS
16b74 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 30 2c  elect(pParse, 0,
16b75 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20   pSelect);.     
16b76 20 20 20 69 66 28 20 70 53 65 6c 54 61 62 3d 3d     if( pSelTab==
16b77 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  0 ) return;.    
16b78 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61      assert( p->a
16b79 43 6f 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  Col==0 );.      
16b7a 20 20 70 2d 3e 6e 43 6f 6c 20 3d 20 70 53 65 6c    p->nCol = pSel
16b7b 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 20 20 20  Tab->nCol;.     
16b7c 20 20 20 70 2d 3e 61 43 6f 6c 20 3d 20 70 53 65     p->aCol = pSe
16b7d 6c 54 61 62 2d 3e 61 43 6f 6c 3b 0a 20 20 20 20  lTab->aCol;.    
16b7e 20 20 20 20 70 53 65 6c 54 61 62 2d 3e 6e 43 6f      pSelTab->nCo
16b7f 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70  l = 0;.        p
16b80 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 20 3d 20 30  SelTab->aCol = 0
16b81 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
16b82 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70 53 65  3DeleteTable(pSe
16b83 6c 54 61 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20  lTab);.      }. 
16b84 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 6d     }..    /* Com
16b85 70 75 74 65 20 74 68 65 20 63 6f 6d 70 6c 65 74  pute the complet
16b86 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 43 52  e text of the CR
16b87 45 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 2a  EATE statement *
16b88 2f 0a 20 20 20 20 69 66 28 20 70 53 65 6c 65 63  /.    if( pSelec
16b89 74 20 29 7b 0a 20 20 20 20 20 20 7a 53 74 6d 74  t ){.      zStmt
16b8a 20 3d 20 63 72 65 61 74 65 54 61 62 6c 65 53 74   = createTableSt
16b8b 6d 74 28 70 2c 20 70 2d 3e 70 53 63 68 65 6d 61  mt(p, p->pSchema
16b8c 3d 3d 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44  ==pParse->db->aD
16b8d 62 5b 31 5d 2e 70 53 63 68 65 6d 61 29 3b 0a 20  b[1].pSchema);. 
16b8e 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
16b8f 6e 20 3d 20 70 45 6e 64 2d 3e 7a 20 2d 20 70 50  n = pEnd->z - pP
16b90 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e  arse->sNameToken
16b91 2e 7a 20 2b 20 31 3b 0a 20 20 20 20 20 20 7a 53  .z + 1;.      zS
16b92 74 6d 74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  tmt = sqlite3MPr
16b93 69 6e 74 66 28 22 43 52 45 41 54 45 20 25 73 20  intf("CREATE %s 
16b94 25 2e 2a 73 22 2c 20 7a 54 79 70 65 32 2c 20 6e  %.*s", zType2, n
16b95 2c 20 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54  , pParse->sNameT
16b96 6f 6b 65 6e 2e 7a 29 3b 0a 20 20 20 20 7d 0a 0a  oken.z);.    }..
16b97 20 20 20 20 2f 2a 20 41 20 73 6c 6f 74 20 66 6f      /* A slot fo
16b98 72 20 74 68 65 20 72 65 63 6f 72 64 20 68 61 73  r the record has
16b99 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 61 6c   already been al
16b9a 6c 6f 63 61 74 65 64 20 69 6e 20 74 68 65 20 0a  located in the .
16b9b 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4d 41      ** SQLITE_MA
16b9c 53 54 45 52 20 74 61 62 6c 65 2e 20 20 57 65 20  STER table.  We 
16b9d 6a 75 73 74 20 6e 65 65 64 20 74 6f 20 75 70 64  just need to upd
16b9e 61 74 65 20 74 68 61 74 20 73 6c 6f 74 20 77 69  ate that slot wi
16b9f 74 68 20 61 6c 6c 0a 20 20 20 20 2a 2a 20 74 68  th all.    ** th
16ba0 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 65  e information we
16ba1 27 76 65 20 63 6f 6c 6c 65 63 74 65 64 2e 20 20  've collected.  
16ba2 54 68 65 20 72 6f 77 69 64 20 66 6f 72 20 74 68  The rowid for th
16ba3 65 20 70 72 65 61 6c 6c 6f 63 61 74 65 64 0a 20  e preallocated. 
16ba4 20 20 20 2a 2a 20 73 6c 6f 74 20 69 73 20 74 68     ** slot is th
16ba5 65 20 32 6e 64 20 69 74 65 6d 20 6f 6e 20 74 68  e 2nd item on th
16ba6 65 20 73 74 61 63 6b 2e 20 20 54 68 65 20 74 6f  e stack.  The to
16ba7 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20 69  p of the stack i
16ba8 73 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 6f 6f  s the.    ** roo
16ba9 74 20 70 61 67 65 20 66 6f 72 20 74 68 65 20 6e  t page for the n
16baa 65 77 20 74 61 62 6c 65 20 28 6f 72 20 61 20 30  ew table (or a 0
16bab 20 69 66 20 74 68 69 73 20 69 73 20 61 20 76 69   if this is a vi
16bac 65 77 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ew)..    */.    
16bad 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72  sqlite3NestedPar
16bae 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20  se(pParse,.     
16baf 20 22 55 50 44 41 54 45 20 25 51 2e 25 73 20 22   "UPDATE %Q.%s "
16bb0 0a 20 20 20 20 20 20 20 20 20 22 53 45 54 20 74  .         "SET t
16bb1 79 70 65 3d 27 25 73 27 2c 20 6e 61 6d 65 3d 25  ype='%s', name=%
16bb2 51 2c 20 74 62 6c 5f 6e 61 6d 65 3d 25 51 2c 20  Q, tbl_name=%Q, 
16bb3 72 6f 6f 74 70 61 67 65 3d 23 30 2c 20 73 71 6c  rootpage=#0, sql
16bb4 3d 25 51 20 22 0a 20 20 20 20 20 20 20 22 57 48  =%Q ".       "WH
16bb5 45 52 45 20 72 6f 77 69 64 3d 23 31 22 2c 0a 20  ERE rowid=#1",. 
16bb6 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62       db->aDb[iDb
16bb7 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f  ].zName, SCHEMA_
16bb8 54 41 42 4c 45 28 69 44 62 29 2c 0a 20 20 20 20  TABLE(iDb),.    
16bb9 20 20 7a 54 79 70 65 2c 0a 20 20 20 20 20 20 70    zType,.      p
16bba 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 70  ->zName,.      p
16bbb 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 7a  ->zName,.      z
16bbc 53 74 6d 74 0a 20 20 20 20 29 3b 0a 20 20 20 20  Stmt.    );.    
16bbd 73 71 6c 69 74 65 46 72 65 65 28 7a 53 74 6d 74  sqliteFree(zStmt
16bbe 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68  );.    sqlite3Ch
16bbf 61 6e 67 65 43 6f 6f 6b 69 65 28 64 62 2c 20 76  angeCookie(db, v
16bc0 2c 20 69 44 62 29 3b 0a 0a 23 69 66 6e 64 65 66  , iDb);..#ifndef
16bc1 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
16bc2 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 20 20 2f  OINCREMENT.    /
16bc3 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69  * Check to see i
16bc4 66 20 77 65 20 6e 65 65 64 20 74 6f 20 63 72 65  f we need to cre
16bc5 61 74 65 20 61 6e 20 73 71 6c 69 74 65 5f 73 65  ate an sqlite_se
16bc6 71 75 65 6e 63 65 20 74 61 62 6c 65 20 66 6f 72  quence table for
16bc7 0a 20 20 20 20 2a 2a 20 6b 65 65 70 69 6e 67 20  .    ** keeping 
16bc8 74 72 61 63 6b 20 6f 66 20 61 75 74 6f 69 6e 63  track of autoinc
16bc9 72 65 6d 65 6e 74 20 6b 65 79 73 2e 0a 20 20 20  rement keys..   
16bca 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 61   */.    if( p->a
16bcb 75 74 6f 49 6e 63 20 29 7b 0a 20 20 20 20 20 20  utoInc ){.      
16bcc 44 62 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61  Db *pDb = &db->a
16bcd 44 62 5b 69 44 62 5d 3b 0a 20 20 20 20 20 20 69  Db[iDb];.      i
16bce 66 28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d  f( pDb->pSchema-
16bcf 3e 70 53 65 71 54 61 62 3d 3d 30 20 29 7b 0a 20  >pSeqTab==0 ){. 
16bd0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4e 65         sqlite3Ne
16bd1 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65  stedParse(pParse
16bd2 2c 0a 20 20 20 20 20 20 20 20 20 20 22 43 52 45  ,.          "CRE
16bd3 41 54 45 20 54 41 42 4c 45 20 25 51 2e 73 71 6c  ATE TABLE %Q.sql
16bd4 69 74 65 5f 73 65 71 75 65 6e 63 65 28 6e 61 6d  ite_sequence(nam
16bd5 65 2c 73 65 71 29 22 2c 0a 20 20 20 20 20 20 20  e,seq)",.       
16bd6 20 20 20 70 44 62 2d 3e 7a 4e 61 6d 65 0a 20 20     pDb->zName.  
16bd7 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 7d        );.      }
16bd8 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  .    }.#endif.. 
16bd9 20 20 20 2f 2a 20 52 65 70 61 72 73 65 20 65 76     /* Reparse ev
16bda 65 72 79 74 68 69 6e 67 20 74 6f 20 75 70 64 61  erything to upda
16bdb 74 65 20 6f 75 72 20 69 6e 74 65 72 6e 61 6c 20  te our internal 
16bdc 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20  data structures 
16bdd 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  */.    sqlite3Vd
16bde 62 65 4f 70 33 28 76 2c 20 4f 50 5f 50 61 72 73  beOp3(v, OP_Pars
16bdf 65 53 63 68 65 6d 61 2c 20 69 44 62 2c 20 30 2c  eSchema, iDb, 0,
16be0 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
16be1 4d 50 72 69 6e 74 66 28 22 74 62 6c 5f 6e 61 6d  MPrintf("tbl_nam
16be2 65 3d 27 25 71 27 22 2c 70 2d 3e 7a 4e 61 6d 65  e='%q'",p->zName
16be3 29 2c 20 50 33 5f 44 59 4e 41 4d 49 43 29 3b 0a  ), P3_DYNAMIC);.
16be4 20 20 7d 0a 0a 0a 20 20 2f 2a 20 41 64 64 20 74    }...  /* Add t
16be5 68 65 20 74 61 62 6c 65 20 74 6f 20 74 68 65 20  he table to the 
16be6 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65 73  in-memory repres
16be7 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
16be8 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a 20  database..  */. 
16be9 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75   if( db->init.bu
16bea 73 79 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 45  sy && pParse->nE
16beb 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20 54 61 62  rr==0 ){.    Tab
16bec 6c 65 20 2a 70 4f 6c 64 3b 0a 20 20 20 20 46 4b  le *pOld;.    FK
16bed 65 79 20 2a 70 46 4b 65 79 3b 20 0a 20 20 20 20  ey *pFKey; .    
16bee 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20  Schema *pSchema 
16bef 3d 20 70 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20  = p->pSchema;.  
16bf0 20 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74 65 33    pOld = sqlite3
16bf1 48 61 73 68 49 6e 73 65 72 74 28 26 70 53 63 68  HashInsert(&pSch
16bf2 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 70 2d  ema->tblHash, p-
16bf3 3e 7a 4e 61 6d 65 2c 20 73 74 72 6c 65 6e 28 70  >zName, strlen(p
16bf4 2d 3e 7a 4e 61 6d 65 29 2b 31 2c 70 29 3b 0a 20  ->zName)+1,p);. 
16bf5 20 20 20 69 66 28 20 70 4f 6c 64 20 29 7b 0a 20     if( pOld ){. 
16bf6 20 20 20 20 20 61 73 73 65 72 74 28 20 70 3d 3d       assert( p==
16bf7 70 4f 6c 64 20 29 3b 20 20 2f 2a 20 4d 61 6c 6c  pOld );  /* Mall
16bf8 6f 63 20 6d 75 73 74 20 68 61 76 65 20 66 61 69  oc must have fai
16bf9 6c 65 64 20 69 6e 73 69 64 65 20 48 61 73 68 49  led inside HashI
16bfa 6e 73 65 72 74 28 29 20 2a 2f 0a 20 20 20 20 20  nsert() */.     
16bfb 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 23   return;.    }.#
16bfc 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
16bfd 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20  IT_FOREIGN_KEY. 
16bfe 20 20 20 66 6f 72 28 70 46 4b 65 79 3d 70 2d 3e     for(pFKey=p->
16bff 70 46 4b 65 79 3b 20 70 46 4b 65 79 3b 20 70 46  pFKey; pFKey; pF
16c00 4b 65 79 3d 70 46 4b 65 79 2d 3e 70 4e 65 78 74  Key=pFKey->pNext
16c01 46 72 6f 6d 29 7b 0a 20 20 20 20 20 20 69 6e 74  From){.      int
16c02 20 6e 54 6f 20 3d 20 73 74 72 6c 65 6e 28 70 46   nTo = strlen(pF
16c03 4b 65 79 2d 3e 7a 54 6f 29 20 2b 20 31 3b 0a 20  Key->zTo) + 1;. 
16c04 20 20 20 20 20 70 46 4b 65 79 2d 3e 70 4e 65 78       pFKey->pNex
16c05 74 54 6f 20 3d 20 73 71 6c 69 74 65 33 48 61 73  tTo = sqlite3Has
16c06 68 46 69 6e 64 28 26 70 53 63 68 65 6d 61 2d 3e  hFind(&pSchema->
16c07 61 46 4b 65 79 2c 20 70 46 4b 65 79 2d 3e 7a 54  aFKey, pFKey->zT
16c08 6f 2c 20 6e 54 6f 29 3b 0a 20 20 20 20 20 20 73  o, nTo);.      s
16c09 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74  qlite3HashInsert
16c0a 28 26 70 53 63 68 65 6d 61 2d 3e 61 46 4b 65 79  (&pSchema->aFKey
16c0b 2c 20 70 46 4b 65 79 2d 3e 7a 54 6f 2c 20 6e 54  , pFKey->zTo, nT
16c0c 6f 2c 20 70 46 4b 65 79 29 3b 0a 20 20 20 20 7d  o, pFKey);.    }
16c0d 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 50 61 72  .#endif.    pPar
16c0e 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 3d 20  se->pNewTable = 
16c0f 30 3b 0a 20 20 20 20 64 62 2d 3e 6e 54 61 62 6c  0;.    db->nTabl
16c10 65 2b 2b 3b 0a 20 20 20 20 64 62 2d 3e 66 6c 61  e++;.    db->fla
16c11 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74  gs |= SQLITE_Int
16c12 65 72 6e 43 68 61 6e 67 65 73 3b 0a 0a 23 69 66  ernChanges;..#if
16c13 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
16c14 5f 41 4c 54 45 52 54 41 42 4c 45 0a 20 20 20 20  _ALTERTABLE.    
16c15 69 66 28 20 21 70 2d 3e 70 53 65 6c 65 63 74 20  if( !p->pSelect 
16c16 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63  ){.      const c
16c17 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 28 63 6f  har *zName = (co
16c18 6e 73 74 20 63 68 61 72 20 2a 29 70 50 61 72 73  nst char *)pPars
16c19 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 7a 3b  e->sNameToken.z;
16c1a 0a 20 20 20 20 20 20 69 6e 74 20 6e 4e 61 6d 65  .      int nName
16c1b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
16c1c 21 70 53 65 6c 65 63 74 20 26 26 20 70 43 6f 6e  !pSelect && pCon
16c1d 73 20 26 26 20 70 45 6e 64 20 29 3b 0a 20 20 20  s && pEnd );.   
16c1e 20 20 20 69 66 28 20 70 43 6f 6e 73 2d 3e 7a 3d     if( pCons->z=
16c1f 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43  =0 ){.        pC
16c20 6f 6e 73 20 3d 20 70 45 6e 64 3b 0a 20 20 20 20  ons = pEnd;.    
16c21 20 20 7d 0a 20 20 20 20 20 20 6e 4e 61 6d 65 20    }.      nName 
16c22 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
16c23 70 43 6f 6e 73 2d 3e 7a 20 2d 20 7a 4e 61 6d 65  pCons->z - zName
16c24 3b 0a 20 20 20 20 20 20 70 2d 3e 61 64 64 43 6f  ;.      p->addCo
16c25 6c 4f 66 66 73 65 74 20 3d 20 31 33 20 2b 20 73  lOffset = 13 + s
16c26 71 6c 69 74 65 33 55 74 66 38 43 68 61 72 4c 65  qlite3Utf8CharLe
16c27 6e 28 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b  n(zName, nName);
16c28 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
16c29 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  }.}..#ifndef SQL
16c2a 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 2f 2a  ITE_OMIT_VIEW./*
16c2b 0a 2a 2a 20 54 68 65 20 70 61 72 73 65 72 20 63  .** The parser c
16c2c 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e  alls this routin
16c2d 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 72  e in order to cr
16c2e 65 61 74 65 20 61 20 6e 65 77 20 56 49 45 57 0a  eate a new VIEW.
16c2f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
16c30 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72  E void sqlite3Cr
16c31 65 61 74 65 56 69 65 77 28 0a 20 20 50 61 72 73  eateView(.  Pars
16c32 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f  e *pParse,     /
16c33 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f  * The parsing co
16c34 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e  ntext */.  Token
16c35 20 2a 70 42 65 67 69 6e 2c 20 20 20 20 20 2f 2a   *pBegin,     /*
16c36 20 54 68 65 20 43 52 45 41 54 45 20 74 6f 6b 65   The CREATE toke
16c37 6e 20 74 68 61 74 20 62 65 67 69 6e 73 20 74 68  n that begins th
16c38 65 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  e statement */. 
16c39 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20   Token *pName1, 
16c3a 20 20 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e      /* The token
16c3b 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20   that holds the 
16c3c 6e 61 6d 65 20 6f 66 20 74 68 65 20 76 69 65 77  name of the view
16c3d 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61   */.  Token *pNa
16c3e 6d 65 32 2c 20 20 20 20 20 2f 2a 20 54 68 65 20  me2,     /* The 
16c3f 74 6f 6b 65 6e 20 74 68 61 74 20 68 6f 6c 64 73  token that holds
16c40 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
16c41 20 76 69 65 77 20 2a 2f 0a 20 20 53 65 6c 65 63   view */.  Selec
16c42 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 2f 2a  t *pSelect,   /*
16c43 20 41 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   A SELECT statem
16c44 65 6e 74 20 74 68 61 74 20 77 69 6c 6c 20 62 65  ent that will be
16c45 63 6f 6d 65 20 74 68 65 20 6e 65 77 20 76 69 65  come the new vie
16c46 77 20 2a 2f 0a 20 20 69 6e 74 20 69 73 54 65 6d  w */.  int isTem
16c47 70 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 52 55  p,        /* TRU
16c48 45 20 66 6f 72 20 61 20 54 45 4d 50 4f 52 41 52  E for a TEMPORAR
16c49 59 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20  Y view */.  int 
16c4a 6e 6f 45 72 72 20 20 20 20 20 20 20 20 20 20 2f  noErr          /
16c4b 2a 20 53 75 70 70 72 65 73 73 20 65 72 72 6f 72  * Suppress error
16c4c 20 6d 65 73 73 61 67 65 73 20 69 66 20 56 49 45   messages if VIE
16c4d 57 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73  W already exists
16c4e 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a   */.){.  Table *
16c4f 70 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 63 6f  p;.  int n;.  co
16c50 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
16c51 72 20 2a 7a 3b 0a 20 20 54 6f 6b 65 6e 20 73 45  r *z;.  Token sE
16c52 6e 64 3b 0a 20 20 44 62 46 69 78 65 72 20 73 46  nd;.  DbFixer sF
16c53 69 78 3b 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61  ix;.  Token *pNa
16c54 6d 65 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 0a  me;.  int iDb;..
16c55 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 56    if( pParse->nV
16c56 61 72 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  ar>0 ){.    sqli
16c57 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
16c58 73 65 2c 20 22 70 61 72 61 6d 65 74 65 72 73 20  se, "parameters 
16c59 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20  are not allowed 
16c5a 69 6e 20 76 69 65 77 73 22 29 3b 0a 20 20 20 20  in views");.    
16c5b 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c  sqlite3SelectDel
16c5c 65 74 65 28 70 53 65 6c 65 63 74 29 3b 0a 20 20  ete(pSelect);.  
16c5d 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
16c5e 73 71 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c  sqlite3StartTabl
16c5f 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 31  e(pParse, pName1
16c60 2c 20 70 4e 61 6d 65 32 2c 20 69 73 54 65 6d 70  , pName2, isTemp
16c61 2c 20 31 2c 20 30 2c 20 6e 6f 45 72 72 29 3b 0a  , 1, 0, noErr);.
16c62 20 20 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e    p = pParse->pN
16c63 65 77 54 61 62 6c 65 3b 0a 20 20 69 66 28 20 70  ewTable;.  if( p
16c64 3d 3d 30 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e  ==0 || pParse->n
16c65 45 72 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Err ){.    sqlit
16c66 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70  e3SelectDelete(p
16c67 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 72 65 74  Select);.    ret
16c68 75 72 6e 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  urn;.  }.  sqlit
16c69 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 70 50  e3TwoPartName(pP
16c6a 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e  arse, pName1, pN
16c6b 61 6d 65 32 2c 20 26 70 4e 61 6d 65 29 3b 0a 20  ame2, &pName);. 
16c6c 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
16c6d 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72  hemaToIndex(pPar
16c6e 73 65 2d 3e 64 62 2c 20 70 2d 3e 70 53 63 68 65  se->db, p->pSche
16c6f 6d 61 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  ma);.  if( sqlit
16c70 65 33 46 69 78 49 6e 69 74 28 26 73 46 69 78 2c  e3FixInit(&sFix,
16c71 20 70 50 61 72 73 65 2c 20 69 44 62 2c 20 22 76   pParse, iDb, "v
16c72 69 65 77 22 2c 20 70 4e 61 6d 65 29 0a 20 20 20  iew", pName).   
16c73 20 26 26 20 73 71 6c 69 74 65 33 46 69 78 53 65   && sqlite3FixSe
16c74 6c 65 63 74 28 26 73 46 69 78 2c 20 70 53 65 6c  lect(&sFix, pSel
16c75 65 63 74 29 0a 20 20 29 7b 0a 20 20 20 20 73 71  ect).  ){.    sq
16c76 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74  lite3SelectDelet
16c77 65 28 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20  e(pSelect);.    
16c78 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f  return;.  }..  /
16c79 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66  * Make a copy of
16c7a 20 74 68 65 20 65 6e 74 69 72 65 20 53 45 4c 45   the entire SELE
16c7b 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61  CT statement tha
16c7c 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 76 69  t defines the vi
16c7d 65 77 2e 0a 20 20 2a 2a 20 54 68 69 73 20 77 69  ew..  ** This wi
16c7e 6c 6c 20 66 6f 72 63 65 20 61 6c 6c 20 74 68 65  ll force all the
16c7f 20 45 78 70 72 2e 74 6f 6b 65 6e 2e 7a 20 76 61   Expr.token.z va
16c80 6c 75 65 73 20 74 6f 20 62 65 20 64 79 6e 61 6d  lues to be dynam
16c81 69 63 61 6c 6c 79 0a 20 20 2a 2a 20 61 6c 6c 6f  ically.  ** allo
16c82 63 61 74 65 64 20 72 61 74 68 65 72 20 74 68 61  cated rather tha
16c83 6e 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 69  n point to the i
16c84 6e 70 75 74 20 73 74 72 69 6e 67 20 2d 20 77 68  nput string - wh
16c85 69 63 68 20 6d 65 61 6e 73 20 74 68 61 74 0a 20  ich means that. 
16c86 20 2a 2a 20 74 68 65 79 20 77 69 6c 6c 20 70 65   ** they will pe
16c87 72 73 69 73 74 20 61 66 74 65 72 20 74 68 65 20  rsist after the 
16c88 63 75 72 72 65 6e 74 20 73 71 6c 69 74 65 33 5f  current sqlite3_
16c89 65 78 65 63 28 29 20 63 61 6c 6c 20 72 65 74 75  exec() call retu
16c8a 72 6e 73 2e 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70  rns..  */.  p->p
16c8b 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33  Select = sqlite3
16c8c 53 65 6c 65 63 74 44 75 70 28 70 53 65 6c 65 63  SelectDup(pSelec
16c8d 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c  t);.  sqlite3Sel
16c8e 65 63 74 44 65 6c 65 74 65 28 70 53 65 6c 65 63  ectDelete(pSelec
16c8f 74 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  t);.  if( sqlite
16c90 33 4d 61 6c 6c 6f 63 46 61 69 6c 65 64 28 29 20  3MallocFailed() 
16c91 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  ){.    return;. 
16c92 20 7d 0a 20 20 69 66 28 20 21 70 50 61 72 73 65   }.  if( !pParse
16c93 2d 3e 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20  ->db->init.busy 
16c94 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 69  ){.    sqlite3Vi
16c95 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73  ewGetColumnNames
16c96 28 70 50 61 72 73 65 2c 20 70 29 3b 0a 20 20 7d  (pParse, p);.  }
16c97 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68  ..  /* Locate th
16c98 65 20 65 6e 64 20 6f 66 20 74 68 65 20 43 52 45  e end of the CRE
16c99 41 54 45 20 56 49 45 57 20 73 74 61 74 65 6d 65  ATE VIEW stateme
16c9a 6e 74 2e 20 20 4d 61 6b 65 20 73 45 6e 64 20 70  nt.  Make sEnd p
16c9b 6f 69 6e 74 20 74 6f 0a 20 20 2a 2a 20 74 68 65  oint to.  ** the
16c9c 20 65 6e 64 2e 0a 20 20 2a 2f 0a 20 20 73 45 6e   end..  */.  sEn
16c9d 64 20 3d 20 70 50 61 72 73 65 2d 3e 73 4c 61 73  d = pParse->sLas
16c9e 74 54 6f 6b 65 6e 3b 0a 20 20 69 66 28 20 73 45  tToken;.  if( sE
16c9f 6e 64 2e 7a 5b 30 5d 21 3d 30 20 26 26 20 73 45  nd.z[0]!=0 && sE
16ca0 6e 64 2e 7a 5b 30 5d 21 3d 27 3b 27 20 29 7b 0a  nd.z[0]!=';' ){.
16ca1 20 20 20 20 73 45 6e 64 2e 7a 20 2b 3d 20 73 45      sEnd.z += sE
16ca2 6e 64 2e 6e 3b 0a 20 20 7d 0a 20 20 73 45 6e 64  nd.n;.  }.  sEnd
16ca3 2e 6e 20 3d 20 30 3b 0a 20 20 6e 20 3d 20 73 45  .n = 0;.  n = sE
16ca4 6e 64 2e 7a 20 2d 20 70 42 65 67 69 6e 2d 3e 7a  nd.z - pBegin->z
16ca5 3b 0a 20 20 7a 20 3d 20 28 63 6f 6e 73 74 20 75  ;.  z = (const u
16ca6 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 70 42  nsigned char*)pB
16ca7 65 67 69 6e 2d 3e 7a 3b 0a 20 20 77 68 69 6c 65  egin->z;.  while
16ca8 28 20 6e 3e 30 20 26 26 20 28 7a 5b 6e 2d 31 5d  ( n>0 && (z[n-1]
16ca9 3d 3d 27 3b 27 20 7c 7c 20 69 73 73 70 61 63 65  ==';' || isspace
16caa 28 7a 5b 6e 2d 31 5d 29 29 20 29 7b 20 6e 2d 2d  (z[n-1])) ){ n--
16cab 3b 20 7d 0a 20 20 73 45 6e 64 2e 7a 20 3d 20 26  ; }.  sEnd.z = &
16cac 7a 5b 6e 2d 31 5d 3b 0a 20 20 73 45 6e 64 2e 6e  z[n-1];.  sEnd.n
16cad 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 55 73 65 20   = 1;..  /* Use 
16cae 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 28  sqlite3EndTable(
16caf 29 20 74 6f 20 61 64 64 20 74 68 65 20 76 69 65  ) to add the vie
16cb0 77 20 74 6f 20 74 68 65 20 53 51 4c 49 54 45 5f  w to the SQLITE_
16cb1 4d 41 53 54 45 52 20 74 61 62 6c 65 20 2a 2f 0a  MASTER table */.
16cb2 20 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c    sqlite3EndTabl
16cb3 65 28 70 50 61 72 73 65 2c 20 30 2c 20 26 73 45  e(pParse, 0, &sE
16cb4 6e 64 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e  nd, 0);.  return
16cb5 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
16cb6 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 20 2a  LITE_OMIT_VIEW *
16cb7 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  /..#if !defined(
16cb8 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
16cb9 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51  ) || !defined(SQ
16cba 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
16cbb 4c 54 41 42 4c 45 29 0a 2f 2a 0a 2a 2a 20 54 68  LTABLE)./*.** Th
16cbc 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72  e Table structur
16cbd 65 20 70 54 61 62 6c 65 20 69 73 20 72 65 61 6c  e pTable is real
16cbe 6c 79 20 61 20 56 49 45 57 2e 20 20 46 69 6c 6c  ly a VIEW.  Fill
16cbf 20 69 6e 20 74 68 65 20 6e 61 6d 65 73 20 6f 66   in the names of
16cc0 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20  .** the columns 
16cc1 6f 66 20 74 68 65 20 76 69 65 77 20 69 6e 20 74  of the view in t
16cc2 68 65 20 70 54 61 62 6c 65 20 73 74 72 75 63 74  he pTable struct
16cc3 75 72 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65  ure.  Return the
16cc4 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 65 72   number.** of er
16cc5 72 6f 72 73 2e 20 20 49 66 20 61 6e 20 65 72 72  rors.  If an err
16cc6 6f 72 20 69 73 20 73 65 65 6e 20 6c 65 61 76 65  or is seen leave
16cc7 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
16cc8 65 20 69 6e 20 70 50 61 72 73 65 2d 3e 7a 45 72  e in pParse->zEr
16cc9 72 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rMsg..*/.SQLITE_
16cca 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
16ccb 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e  te3ViewGetColumn
16ccc 4e 61 6d 65 73 28 50 61 72 73 65 20 2a 70 50 61  Names(Parse *pPa
16ccd 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62  rse, Table *pTab
16cce 6c 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 53  le){.  Table *pS
16ccf 65 6c 54 61 62 3b 20 20 20 2f 2a 20 41 20 66 61  elTab;   /* A fa
16cd0 6b 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 77 68  ke table from wh
16cd1 69 63 68 20 77 65 20 67 65 74 20 74 68 65 20 72  ich we get the r
16cd2 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 53  esult set */.  S
16cd3 65 6c 65 63 74 20 2a 70 53 65 6c 3b 20 20 20 20  elect *pSel;    
16cd4 20 2f 2a 20 43 6f 70 79 20 6f 66 20 74 68 65 20   /* Copy of the 
16cd5 53 45 4c 45 43 54 20 74 68 61 74 20 69 6d 70 6c  SELECT that impl
16cd6 65 6d 65 6e 74 73 20 74 68 65 20 76 69 65 77 20  ements the view 
16cd7 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 20 3d 20  */.  int nErr = 
16cd8 30 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  0;     /* Number
16cd9 20 6f 66 20 65 72 72 6f 72 73 20 65 6e 63 6f 75   of errors encou
16cda 6e 74 65 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20  ntered */.  int 
16cdb 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  n;            /*
16cdc 20 54 65 6d 70 6f 72 61 72 69 6c 79 20 68 6f 6c   Temporarily hol
16cdd 64 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ds the number of
16cde 20 63 75 72 73 6f 72 73 20 61 73 73 69 67 6e 65   cursors assigne
16cdf 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  d */..  assert( 
16ce0 70 54 61 62 6c 65 20 29 3b 0a 0a 23 69 66 6e 64  pTable );..#ifnd
16ce1 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
16ce2 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66  IRTUALTABLE.  if
16ce3 28 20 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c  ( sqlite3VtabCal
16ce4 6c 43 6f 6e 6e 65 63 74 28 70 50 61 72 73 65 2c  lConnect(pParse,
16ce5 20 70 54 61 62 6c 65 29 20 29 7b 0a 20 20 20 20   pTable) ){.    
16ce6 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
16ce7 52 4f 52 3b 0a 20 20 7d 0a 20 20 69 66 28 20 49  ROR;.  }.  if( I
16ce8 73 56 69 72 74 75 61 6c 28 70 54 61 62 6c 65 29  sVirtual(pTable)
16ce9 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e   ) return 0;.#en
16cea 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
16ceb 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20  ITE_OMIT_VIEW.  
16cec 2f 2a 20 41 20 70 6f 73 69 74 69 76 65 20 6e 43  /* A positive nC
16ced 6f 6c 20 6d 65 61 6e 73 20 74 68 65 20 63 6f 6c  ol means the col
16cee 75 6d 6e 73 20 6e 61 6d 65 73 20 66 6f 72 20 74  umns names for t
16cef 68 69 73 20 76 69 65 77 20 61 72 65 0a 20 20 2a  his view are.  *
16cf0 2a 20 61 6c 72 65 61 64 79 20 6b 6e 6f 77 6e 2e  * already known.
16cf1 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62  .  */.  if( pTab
16cf2 6c 65 2d 3e 6e 43 6f 6c 3e 30 20 29 20 72 65 74  le->nCol>0 ) ret
16cf3 75 72 6e 20 30 3b 0a 0a 20 20 2f 2a 20 41 20 6e  urn 0;..  /* A n
16cf4 65 67 61 74 69 76 65 20 6e 43 6f 6c 20 69 73 20  egative nCol is 
16cf5 61 20 73 70 65 63 69 61 6c 20 6d 61 72 6b 65 72  a special marker
16cf6 20 6d 65 61 6e 69 6e 67 20 74 68 61 74 20 77 65   meaning that we
16cf7 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 0a 20   are currently. 
16cf8 20 2a 2a 20 74 72 79 69 6e 67 20 74 6f 20 63 6f   ** trying to co
16cf9 6d 70 75 74 65 20 74 68 65 20 63 6f 6c 75 6d 6e  mpute the column
16cfa 20 6e 61 6d 65 73 2e 20 20 49 66 20 77 65 20 65   names.  If we e
16cfb 6e 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e  nter this routin
16cfc 65 20 77 69 74 68 0a 20 20 2a 2a 20 61 20 6e 65  e with.  ** a ne
16cfd 67 61 74 69 76 65 20 6e 43 6f 6c 2c 20 69 74 20  gative nCol, it 
16cfe 6d 65 61 6e 73 20 74 77 6f 20 6f 72 20 6d 6f 72  means two or mor
16cff 65 20 76 69 65 77 73 20 66 6f 72 6d 20 61 20 6c  e views form a l
16d00 6f 6f 70 2c 20 6c 69 6b 65 20 74 68 69 73 3a 0a  oop, like this:.
16d01 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 43 52    **.  **     CR
16d02 45 41 54 45 20 56 49 45 57 20 6f 6e 65 20 41 53  EATE VIEW one AS
16d03 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74   SELECT * FROM t
16d04 77 6f 3b 0a 20 20 2a 2a 20 20 20 20 20 43 52 45  wo;.  **     CRE
16d05 41 54 45 20 56 49 45 57 20 74 77 6f 20 41 53 20  ATE VIEW two AS 
16d06 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 6f 6e  SELECT * FROM on
16d07 65 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 63 74  e;.  **.  ** Act
16d08 75 61 6c 6c 79 2c 20 74 68 69 73 20 65 72 72 6f  ually, this erro
16d09 72 20 69 73 20 63 61 75 67 68 74 20 70 72 65 76  r is caught prev
16d0a 69 6f 75 73 6c 79 20 61 6e 64 20 73 6f 20 74 68  iously and so th
16d0b 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 65 73 74  e following test
16d0c 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 61 6c 77  .  ** should alw
16d0d 61 79 73 20 66 61 69 6c 2e 20 20 42 75 74 20 77  ays fail.  But w
16d0e 65 20 77 69 6c 6c 20 6c 65 61 76 65 20 69 74 20  e will leave it 
16d0f 69 6e 20 70 6c 61 63 65 20 6a 75 73 74 20 74 6f  in place just to
16d10 20 62 65 20 73 61 66 65 2e 0a 20 20 2a 2f 0a 20   be safe..  */. 
16d11 20 69 66 28 20 70 54 61 62 6c 65 2d 3e 6e 43 6f   if( pTable->nCo
16d12 6c 3c 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  l<0 ){.    sqlit
16d13 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
16d14 65 2c 20 22 76 69 65 77 20 25 73 20 69 73 20 63  e, "view %s is c
16d15 69 72 63 75 6c 61 72 6c 79 20 64 65 66 69 6e 65  ircularly define
16d16 64 22 2c 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d  d", pTable->zNam
16d17 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  e);.    return 1
16d18 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
16d19 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3e 3d 30 20  pTable->nCol>=0 
16d1a 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 67  );..  /* If we g
16d1b 65 74 20 74 68 69 73 20 66 61 72 2c 20 69 74 20  et this far, it 
16d1c 6d 65 61 6e 73 20 77 65 20 6e 65 65 64 20 74 6f  means we need to
16d1d 20 63 6f 6d 70 75 74 65 20 74 68 65 20 74 61 62   compute the tab
16d1e 6c 65 20 6e 61 6d 65 73 2e 0a 20 20 2a 2a 20 4e  le names..  ** N
16d1f 6f 74 65 20 74 68 61 74 20 74 68 65 20 63 61 6c  ote that the cal
16d20 6c 20 74 6f 20 73 71 6c 69 74 65 33 52 65 73 75  l to sqlite3Resu
16d21 6c 74 53 65 74 4f 66 53 65 6c 65 63 74 28 29 20  ltSetOfSelect() 
16d22 77 69 6c 6c 20 65 78 70 61 6e 64 20 61 6e 79 0a  will expand any.
16d23 20 20 2a 2a 20 22 2a 22 20 65 6c 65 6d 65 6e 74    ** "*" element
16d24 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 73  s in the results
16d25 20 73 65 74 20 6f 66 20 74 68 65 20 76 69 65 77   set of the view
16d26 20 61 6e 64 20 77 69 6c 6c 20 61 73 73 69 67 6e   and will assign
16d27 20 63 75 72 73 6f 72 73 0a 20 20 2a 2a 20 74 6f   cursors.  ** to
16d28 20 74 68 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66   the elements of
16d29 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
16d2a 2e 20 20 42 75 74 20 77 65 20 64 6f 20 6e 6f 74  .  But we do not
16d2b 20 77 61 6e 74 20 74 68 65 73 65 20 63 68 61 6e   want these chan
16d2c 67 65 73 0a 20 20 2a 2a 20 74 6f 20 62 65 20 70  ges.  ** to be p
16d2d 65 72 6d 61 6e 65 6e 74 2e 20 20 53 6f 20 74 68  ermanent.  So th
16d2e 65 20 63 6f 6d 70 75 74 61 74 69 6f 6e 20 69 73  e computation is
16d2f 20 64 6f 6e 65 20 6f 6e 20 61 20 63 6f 70 79 20   done on a copy 
16d30 6f 66 20 74 68 65 20 53 45 4c 45 43 54 0a 20 20  of the SELECT.  
16d31 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61  ** statement tha
16d32 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 76 69  t defines the vi
16d33 65 77 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  ew..  */.  asser
16d34 74 28 20 70 54 61 62 6c 65 2d 3e 70 53 65 6c 65  t( pTable->pSele
16d35 63 74 20 29 3b 0a 20 20 70 53 65 6c 20 3d 20 73  ct );.  pSel = s
16d36 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28  qlite3SelectDup(
16d37 70 54 61 62 6c 65 2d 3e 70 53 65 6c 65 63 74 29  pTable->pSelect)
16d38 3b 0a 20 20 69 66 28 20 70 53 65 6c 20 29 7b 0a  ;.  if( pSel ){.
16d39 20 20 20 20 6e 20 3d 20 70 50 61 72 73 65 2d 3e      n = pParse->
16d3a 6e 54 61 62 3b 0a 20 20 20 20 73 71 6c 69 74 65  nTab;.    sqlite
16d3b 33 53 72 63 4c 69 73 74 41 73 73 69 67 6e 43 75  3SrcListAssignCu
16d3c 72 73 6f 72 73 28 70 50 61 72 73 65 2c 20 70 53  rsors(pParse, pS
16d3d 65 6c 2d 3e 70 53 72 63 29 3b 0a 20 20 20 20 70  el->pSrc);.    p
16d3e 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 2d 31  Table->nCol = -1
16d3f 3b 0a 20 20 20 20 70 53 65 6c 54 61 62 20 3d 20  ;.    pSelTab = 
16d40 73 71 6c 69 74 65 33 52 65 73 75 6c 74 53 65 74  sqlite3ResultSet
16d41 4f 66 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c  OfSelect(pParse,
16d42 20 30 2c 20 70 53 65 6c 29 3b 0a 20 20 20 20 70   0, pSel);.    p
16d43 50 61 72 73 65 2d 3e 6e 54 61 62 20 3d 20 6e 3b  Parse->nTab = n;
16d44 0a 20 20 20 20 69 66 28 20 70 53 65 6c 54 61 62  .    if( pSelTab
16d45 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
16d46 28 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 3d 3d  ( pTable->aCol==
16d47 30 20 29 3b 0a 20 20 20 20 20 20 70 54 61 62 6c  0 );.      pTabl
16d48 65 2d 3e 6e 43 6f 6c 20 3d 20 70 53 65 6c 54 61  e->nCol = pSelTa
16d49 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 70  b->nCol;.      p
16d4a 54 61 62 6c 65 2d 3e 61 43 6f 6c 20 3d 20 70 53  Table->aCol = pS
16d4b 65 6c 54 61 62 2d 3e 61 43 6f 6c 3b 0a 20 20 20  elTab->aCol;.   
16d4c 20 20 20 70 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c     pSelTab->nCol
16d4d 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 53 65 6c   = 0;.      pSel
16d4e 54 61 62 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20  Tab->aCol = 0;. 
16d4f 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65       sqlite3Dele
16d50 74 65 54 61 62 6c 65 28 70 53 65 6c 54 61 62 29  teTable(pSelTab)
16d51 3b 0a 20 20 20 20 20 20 70 54 61 62 6c 65 2d 3e  ;.      pTable->
16d52 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 20 7c  pSchema->flags |
16d53 3d 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77  = DB_UnresetView
16d54 73 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  s;.    }else{.  
16d55 20 20 20 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c      pTable->nCol
16d56 20 3d 20 30 3b 0a 20 20 20 20 20 20 6e 45 72 72   = 0;.      nErr
16d57 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  ++;.    }.    sq
16d58 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74  lite3SelectDelet
16d59 65 28 70 53 65 6c 29 3b 0a 20 20 7d 20 65 6c 73  e(pSel);.  } els
16d5a 65 20 7b 0a 20 20 20 20 6e 45 72 72 2b 2b 3b 0a  e {.    nErr++;.
16d5b 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51    }.#endif /* SQ
16d5c 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 20 2a  LITE_OMIT_VIEW *
16d5d 2f 0a 20 20 72 65 74 75 72 6e 20 6e 45 72 72 3b  /.  return nErr;
16d5e 20 20 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21    .}.#endif /* !
16d5f 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
16d60 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65  MIT_VIEW) || !de
16d61 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
16d62 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29 20  T_VIRTUALTABLE) 
16d63 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
16d64 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 2f 2a 0a  TE_OMIT_VIEW./*.
16d65 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 63 6f 6c  ** Clear the col
16d66 75 6d 6e 20 6e 61 6d 65 73 20 66 72 6f 6d 20 65  umn names from e
16d67 76 65 72 79 20 56 49 45 57 20 69 6e 20 64 61 74  very VIEW in dat
16d68 61 62 61 73 65 20 69 64 78 2e 0a 2a 2f 0a 73 74  abase idx..*/.st
16d69 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65  atic void sqlite
16d6a 56 69 65 77 52 65 73 65 74 41 6c 6c 28 73 71 6c  ViewResetAll(sql
16d6b 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 64  ite3 *db, int id
16d6c 78 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a  x){.  HashElem *
16d6d 69 3b 0a 20 20 69 66 28 20 21 44 62 48 61 73 50  i;.  if( !DbHasP
16d6e 72 6f 70 65 72 74 79 28 64 62 2c 20 69 64 78 2c  roperty(db, idx,
16d6f 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73   DB_UnresetViews
16d70 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f  ) ) return;.  fo
16d71 72 28 69 3d 73 71 6c 69 74 65 48 61 73 68 46 69  r(i=sqliteHashFi
16d72 72 73 74 28 26 64 62 2d 3e 61 44 62 5b 69 64 78  rst(&db->aDb[idx
16d73 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61  ].pSchema->tblHa
16d74 73 68 29 3b 20 69 3b 69 3d 73 71 6c 69 74 65 48  sh); i;i=sqliteH
16d75 61 73 68 4e 65 78 74 28 69 29 29 7b 0a 20 20 20  ashNext(i)){.   
16d76 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 73   Table *pTab = s
16d77 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 69 29  qliteHashData(i)
16d78 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e  ;.    if( pTab->
16d79 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20  pSelect ){.     
16d7a 20 73 71 6c 69 74 65 52 65 73 65 74 43 6f 6c 75   sqliteResetColu
16d7b 6d 6e 4e 61 6d 65 73 28 70 54 61 62 29 3b 0a 20  mnNames(pTab);. 
16d7c 20 20 20 7d 0a 20 20 7d 0a 20 20 44 62 43 6c 65     }.  }.  DbCle
16d7d 61 72 50 72 6f 70 65 72 74 79 28 64 62 2c 20 69  arProperty(db, i
16d7e 64 78 2c 20 44 42 5f 55 6e 72 65 73 65 74 56 69  dx, DB_UnresetVi
16d7f 65 77 73 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23 20  ews);.}.#else.# 
16d80 64 65 66 69 6e 65 20 73 71 6c 69 74 65 56 69 65  define sqliteVie
16d81 77 52 65 73 65 74 41 6c 6c 28 41 2c 42 29 0a 23  wResetAll(A,B).#
16d82 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
16d83 4f 4d 49 54 5f 56 49 45 57 20 2a 2f 0a 0a 2f 2a  OMIT_VIEW */../*
16d84 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
16d85 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74  n is called by t
16d86 68 65 20 56 44 42 45 20 74 6f 20 61 64 6a 75 73  he VDBE to adjus
16d87 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73  t the internal s
16d88 63 68 65 6d 61 0a 2a 2a 20 75 73 65 64 20 62 79  chema.** used by
16d89 20 53 51 4c 69 74 65 20 77 68 65 6e 20 74 68 65   SQLite when the
16d8a 20 62 74 72 65 65 20 6c 61 79 65 72 20 6d 6f 76   btree layer mov
16d8b 65 73 20 61 20 74 61 62 6c 65 20 72 6f 6f 74 20  es a table root 
16d8c 70 61 67 65 2e 20 54 68 65 0a 2a 2a 20 72 6f 6f  page. The.** roo
16d8d 74 2d 70 61 67 65 20 6f 66 20 61 20 74 61 62 6c  t-page of a tabl
16d8e 65 20 6f 72 20 69 6e 64 65 78 20 69 6e 20 64 61  e or index in da
16d8f 74 61 62 61 73 65 20 69 44 62 20 68 61 73 20 63  tabase iDb has c
16d90 68 61 6e 67 65 64 20 66 72 6f 6d 20 69 46 72 6f  hanged from iFro
16d91 6d 0a 2a 2a 20 74 6f 20 69 54 6f 2e 0a 2a 2a 0a  m.** to iTo..**.
16d92 2a 2a 20 54 69 63 6b 65 74 20 23 31 37 32 38 3a  ** Ticket #1728:
16d93 20 20 54 68 65 20 73 79 6d 62 6f 6c 20 74 61 62    The symbol tab
16d94 6c 65 20 6d 69 67 68 74 20 73 74 69 6c 6c 20 63  le might still c
16d95 6f 6e 74 61 69 6e 20 69 6e 66 6f 72 6d 61 74 69  ontain informati
16d96 6f 6e 0a 2a 2a 20 6f 6e 20 74 61 62 6c 65 73 20  on.** on tables 
16d97 61 6e 64 2f 6f 72 20 69 6e 64 69 63 65 73 20 74  and/or indices t
16d98 68 61 74 20 61 72 65 20 74 68 65 20 70 72 6f 63  hat are the proc
16d99 65 73 73 20 6f 66 20 62 65 69 6e 67 20 64 65 6c  ess of being del
16d9a 65 74 65 64 2e 0a 2a 2a 20 49 66 20 79 6f 75 20  eted..** If you 
16d9b 61 72 65 20 75 6e 6c 75 63 6b 79 2c 20 6f 6e 65  are unlucky, one
16d9c 20 6f 66 20 74 68 6f 73 65 20 64 65 6c 65 74 65   of those delete
16d9d 64 20 69 6e 64 69 63 65 73 20 6f 72 20 74 61 62  d indices or tab
16d9e 6c 65 73 20 6d 69 67 68 74 0a 2a 2a 20 68 61 76  les might.** hav
16d9f 65 20 74 68 65 20 73 61 6d 65 20 72 6f 6f 74 70  e the same rootp
16da0 61 67 65 20 6e 75 6d 62 65 72 20 61 73 20 74 68  age number as th
16da1 65 20 72 65 61 6c 20 74 61 62 6c 65 20 6f 72 20  e real table or 
16da2 69 6e 64 65 78 20 74 68 61 74 20 69 73 0a 2a 2a  index that is.**
16da3 20 62 65 69 6e 67 20 6d 6f 76 65 64 2e 20 20 53   being moved.  S
16da4 6f 20 77 65 20 63 61 6e 6e 6f 74 20 73 74 6f 70  o we cannot stop
16da5 20 73 65 61 72 63 68 69 6e 67 20 61 66 74 65 72   searching after
16da6 20 74 68 65 20 66 69 72 73 74 20 6d 61 74 63 68   the first match
16da7 20 0a 2a 2a 20 62 65 63 61 75 73 65 20 74 68 65   .** because the
16da8 20 66 69 72 73 74 20 6d 61 74 63 68 20 6d 69 67   first match mig
16da9 68 74 20 62 65 20 66 6f 72 20 6f 6e 65 20 6f 66  ht be for one of
16daa 20 74 68 65 20 64 65 6c 65 74 65 64 20 69 6e 64   the deleted ind
16dab 69 63 65 73 0a 2a 2a 20 6f 72 20 74 61 62 6c 65  ices.** or table
16dac 73 20 61 6e 64 20 6e 6f 74 20 74 68 65 20 74 61  s and not the ta
16dad 62 6c 65 2f 69 6e 64 65 78 20 74 68 61 74 20 69  ble/index that i
16dae 73 20 61 63 74 75 61 6c 6c 79 20 62 65 69 6e 67  s actually being
16daf 20 6d 6f 76 65 64 2e 0a 2a 2a 20 57 65 20 6d 75   moved..** We mu
16db0 73 74 20 63 6f 6e 74 69 6e 75 65 20 6c 6f 6f 70  st continue loop
16db1 69 6e 67 20 75 6e 74 69 6c 20 61 6c 6c 20 74 61  ing until all ta
16db2 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63 65 73  bles and indices
16db3 20 77 69 74 68 0a 2a 2a 20 72 6f 6f 74 70 61 67   with.** rootpag
16db4 65 3d 3d 69 46 72 6f 6d 20 68 61 76 65 20 62 65  e==iFrom have be
16db5 65 6e 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 20  en converted to 
16db6 68 61 76 65 20 61 20 72 6f 6f 74 70 61 67 65 20  have a rootpage 
16db7 6f 66 20 69 54 6f 0a 2a 2a 20 69 6e 20 6f 72 64  of iTo.** in ord
16db8 65 72 20 74 6f 20 62 65 20 63 65 72 74 61 69 6e  er to be certain
16db9 20 74 68 61 74 20 77 65 20 67 6f 74 20 74 68 65   that we got the
16dba 20 72 69 67 68 74 20 6f 6e 65 2e 0a 2a 2f 0a 23   right one..*/.#
16dbb 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
16dbc 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 53 51  IT_AUTOVACUUM.SQ
16dbd 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
16dbe 64 20 73 71 6c 69 74 65 33 52 6f 6f 74 50 61 67  d sqlite3RootPag
16dbf 65 4d 6f 76 65 64 28 44 62 20 2a 70 44 62 2c 20  eMoved(Db *pDb, 
16dc0 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74 20 69  int iFrom, int i
16dc1 54 6f 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20  To){.  HashElem 
16dc2 2a 70 45 6c 65 6d 3b 0a 20 20 48 61 73 68 20 2a  *pElem;.  Hash *
16dc3 70 48 61 73 68 3b 0a 0a 20 20 70 48 61 73 68 20  pHash;..  pHash 
16dc4 3d 20 26 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d  = &pDb->pSchema-
16dc5 3e 74 62 6c 48 61 73 68 3b 0a 20 20 66 6f 72 28  >tblHash;.  for(
16dc6 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 68  pElem=sqliteHash
16dc7 46 69 72 73 74 28 70 48 61 73 68 29 3b 20 70 45  First(pHash); pE
16dc8 6c 65 6d 3b 20 70 45 6c 65 6d 3d 73 71 6c 69 74  lem; pElem=sqlit
16dc9 65 48 61 73 68 4e 65 78 74 28 70 45 6c 65 6d 29  eHashNext(pElem)
16dca 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54  ){.    Table *pT
16dcb 61 62 20 3d 20 73 71 6c 69 74 65 48 61 73 68 44  ab = sqliteHashD
16dcc 61 74 61 28 70 45 6c 65 6d 29 3b 0a 20 20 20 20  ata(pElem);.    
16dcd 69 66 28 20 70 54 61 62 2d 3e 74 6e 75 6d 3d 3d  if( pTab->tnum==
16dce 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20 70  iFrom ){.      p
16dcf 54 61 62 2d 3e 74 6e 75 6d 20 3d 20 69 54 6f 3b  Tab->tnum = iTo;
16dd0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 48 61  .    }.  }.  pHa
16dd1 73 68 20 3d 20 26 70 44 62 2d 3e 70 53 63 68 65  sh = &pDb->pSche
16dd2 6d 61 2d 3e 69 64 78 48 61 73 68 3b 0a 20 20 66  ma->idxHash;.  f
16dd3 6f 72 28 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48  or(pElem=sqliteH
16dd4 61 73 68 46 69 72 73 74 28 70 48 61 73 68 29 3b  ashFirst(pHash);
16dd5 20 70 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d 73 71   pElem; pElem=sq
16dd6 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70 45 6c  liteHashNext(pEl
16dd7 65 6d 29 29 7b 0a 20 20 20 20 49 6e 64 65 78 20  em)){.    Index 
16dd8 2a 70 49 64 78 20 3d 20 73 71 6c 69 74 65 48 61  *pIdx = sqliteHa
16dd9 73 68 44 61 74 61 28 70 45 6c 65 6d 29 3b 0a 20  shData(pElem);. 
16dda 20 20 20 69 66 28 20 70 49 64 78 2d 3e 74 6e 75     if( pIdx->tnu
16ddb 6d 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20  m==iFrom ){.    
16ddc 20 20 70 49 64 78 2d 3e 74 6e 75 6d 20 3d 20 69    pIdx->tnum = i
16ddd 54 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  To;.    }.  }.}.
16dde 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 72  #endif../*.** Wr
16ddf 69 74 65 20 63 6f 64 65 20 74 6f 20 65 72 61 73  ite code to eras
16de0 65 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 68  e the table with
16de1 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62 6c   root-page iTabl
16de2 65 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65 20  e from database 
16de3 69 44 62 2e 0a 2a 2a 20 41 6c 73 6f 20 77 72 69  iDb..** Also wri
16de4 74 65 20 63 6f 64 65 20 74 6f 20 6d 6f 64 69 66  te code to modif
16de5 79 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73  y the sqlite_mas
16de6 74 65 72 20 74 61 62 6c 65 20 61 6e 64 20 69 6e  ter table and in
16de7 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 0a 2a 2a  ternal schema.**
16de8 20 69 66 20 61 20 72 6f 6f 74 2d 70 61 67 65 20   if a root-page 
16de9 6f 66 20 61 6e 6f 74 68 65 72 20 74 61 62 6c 65  of another table
16dea 20 69 73 20 6d 6f 76 65 64 20 62 79 20 74 68 65   is moved by the
16deb 20 62 74 72 65 65 2d 6c 61 79 65 72 20 77 68 69   btree-layer whi
16dec 6c 73 74 0a 2a 2a 20 65 72 61 73 69 6e 67 20 69  lst.** erasing i
16ded 54 61 62 6c 65 20 28 74 68 69 73 20 63 61 6e 20  Table (this can 
16dee 68 61 70 70 65 6e 20 77 69 74 68 20 61 6e 20 61  happen with an a
16def 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62  uto-vacuum datab
16df0 61 73 65 29 2e 0a 2a 2f 20 0a 73 74 61 74 69 63  ase)..*/ .static
16df1 20 76 6f 69 64 20 64 65 73 74 72 6f 79 52 6f 6f   void destroyRoo
16df2 74 50 61 67 65 28 50 61 72 73 65 20 2a 70 50 61  tPage(Parse *pPa
16df3 72 73 65 2c 20 69 6e 74 20 69 54 61 62 6c 65 2c  rse, int iTable,
16df4 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 56 64 62   int iDb){.  Vdb
16df5 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65  e *v = sqlite3Ge
16df6 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
16df7 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
16df8 70 28 76 2c 20 4f 50 5f 44 65 73 74 72 6f 79 2c  p(v, OP_Destroy,
16df9 20 69 54 61 62 6c 65 2c 20 69 44 62 29 3b 0a 23   iTable, iDb);.#
16dfa 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
16dfb 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
16dfc 2f 2a 20 4f 50 5f 44 65 73 74 72 6f 79 20 70 75  /* OP_Destroy pu
16dfd 73 68 65 73 20 61 6e 20 69 6e 74 65 67 65 72 20  shes an integer 
16dfe 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 2e 20  onto the stack. 
16dff 49 66 20 74 68 69 73 20 69 6e 74 65 67 65 72 0a  If this integer.
16e00 20 20 2a 2a 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f    ** is non-zero
16e01 2c 20 74 68 65 6e 20 69 74 20 69 73 20 74 68 65  , then it is the
16e02 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65   root page numbe
16e03 72 20 6f 66 20 61 20 74 61 62 6c 65 20 6d 6f 76  r of a table mov
16e04 65 64 20 74 6f 0a 20 20 2a 2a 20 6c 6f 63 61 74  ed to.  ** locat
16e05 69 6f 6e 20 69 54 61 62 6c 65 2e 20 54 68 65 20  ion iTable. The 
16e06 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 6d  following code m
16e07 6f 64 69 66 69 65 73 20 74 68 65 20 73 71 6c 69  odifies the sqli
16e08 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20  te_master table 
16e09 74 6f 0a 20 20 2a 2a 20 72 65 66 6c 65 63 74 20  to.  ** reflect 
16e0a 74 68 69 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  this..  **.  ** 
16e0b 54 68 65 20 22 23 30 22 20 69 6e 20 74 68 65 20  The "#0" in the 
16e0c 53 51 4c 20 69 73 20 61 20 73 70 65 63 69 61 6c  SQL is a special
16e0d 20 63 6f 6e 73 74 61 6e 74 20 74 68 61 74 20 6d   constant that m
16e0e 65 61 6e 73 20 77 68 61 74 65 76 65 72 20 76 61  eans whatever va
16e0f 6c 75 65 0a 20 20 2a 2a 20 69 73 20 6f 6e 20 74  lue.  ** is on t
16e10 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74  he top of the st
16e11 61 63 6b 2e 20 20 53 65 65 20 73 71 6c 69 74 65  ack.  See sqlite
16e12 33 52 65 67 69 73 74 65 72 45 78 70 72 28 29 2e  3RegisterExpr().
16e13 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 4e  .  */.  sqlite3N
16e14 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73  estedParse(pPars
16e15 65 2c 20 0a 20 20 20 20 20 22 55 50 44 41 54 45  e, .     "UPDATE
16e16 20 25 51 2e 25 73 20 53 45 54 20 72 6f 6f 74 70   %Q.%s SET rootp
16e17 61 67 65 3d 25 64 20 57 48 45 52 45 20 23 30 20  age=%d WHERE #0 
16e18 41 4e 44 20 72 6f 6f 74 70 61 67 65 3d 23 30 22  AND rootpage=#0"
16e19 2c 0a 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64  ,.     pParse->d
16e1a 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
16e1b 65 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28  e, SCHEMA_TABLE(
16e1c 69 44 62 29 2c 20 69 54 61 62 6c 65 29 3b 0a 23  iDb), iTable);.#
16e1d 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57  endif.}../*.** W
16e1e 72 69 74 65 20 56 44 42 45 20 63 6f 64 65 20 74  rite VDBE code t
16e1f 6f 20 65 72 61 73 65 20 74 61 62 6c 65 20 70 54  o erase table pT
16e20 61 62 20 61 6e 64 20 61 6c 6c 20 61 73 73 6f 63  ab and all assoc
16e21 69 61 74 65 64 20 69 6e 64 69 63 65 73 20 6f 6e  iated indices on
16e22 20 64 69 73 6b 2e 0a 2a 2a 20 43 6f 64 65 20 74   disk..** Code t
16e23 6f 20 75 70 64 61 74 65 20 74 68 65 20 73 71 6c  o update the sql
16e24 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65  ite_master table
16e25 73 20 61 6e 64 20 69 6e 74 65 72 6e 61 6c 20 73  s and internal s
16e26 63 68 65 6d 61 20 64 65 66 69 6e 69 74 69 6f 6e  chema definition
16e27 73 0a 2a 2a 20 69 6e 20 63 61 73 65 20 61 20 72  s.** in case a r
16e28 6f 6f 74 2d 70 61 67 65 20 62 65 6c 6f 6e 67 69  oot-page belongi
16e29 6e 67 20 74 6f 20 61 6e 6f 74 68 65 72 20 74 61  ng to another ta
16e2a 62 6c 65 20 69 73 20 6d 6f 76 65 64 20 62 79 20  ble is moved by 
16e2b 74 68 65 20 62 74 72 65 65 20 6c 61 79 65 72 0a  the btree layer.
16e2c 2a 2a 20 69 73 20 61 6c 73 6f 20 61 64 64 65 64  ** is also added
16e2d 20 28 74 68 69 73 20 63 61 6e 20 68 61 70 70 65   (this can happe
16e2e 6e 20 77 69 74 68 20 61 6e 20 61 75 74 6f 2d 76  n with an auto-v
16e2f 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 29 2e  acuum database).
16e30 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
16e31 64 65 73 74 72 6f 79 54 61 62 6c 65 28 50 61 72  destroyTable(Par
16e32 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c  se *pParse, Tabl
16e33 65 20 2a 70 54 61 62 29 7b 0a 23 69 66 64 65 66  e *pTab){.#ifdef
16e34 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
16e35 4f 56 41 43 55 55 4d 0a 20 20 49 6e 64 65 78 20  OVACUUM.  Index 
16e36 2a 70 49 64 78 3b 0a 20 20 69 6e 74 20 69 44 62  *pIdx;.  int iDb
16e37 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
16e38 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e  ToIndex(pParse->
16e39 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
16e3a 61 29 3b 0a 20 20 64 65 73 74 72 6f 79 52 6f 6f  a);.  destroyRoo
16e3b 74 50 61 67 65 28 70 50 61 72 73 65 2c 20 70 54  tPage(pParse, pT
16e3c 61 62 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 3b 0a  ab->tnum, iDb);.
16e3d 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d    for(pIdx=pTab-
16e3e 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70  >pIndex; pIdx; p
16e3f 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29  Idx=pIdx->pNext)
16e40 7b 0a 20 20 20 20 64 65 73 74 72 6f 79 52 6f 6f  {.    destroyRoo
16e41 74 50 61 67 65 28 70 50 61 72 73 65 2c 20 70 49  tPage(pParse, pI
16e42 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 3b 0a  dx->tnum, iDb);.
16e43 20 20 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 49    }.#else.  /* I
16e44 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6d  f the database m
16e45 61 79 20 62 65 20 61 75 74 6f 2d 76 61 63 75 75  ay be auto-vacuu
16e46 6d 20 63 61 70 61 62 6c 65 20 28 69 66 20 53 51  m capable (if SQ
16e47 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
16e48 43 55 55 4d 0a 20 20 2a 2a 20 69 73 20 6e 6f 74  CUUM.  ** is not
16e49 20 64 65 66 69 6e 65 64 29 2c 20 74 68 65 6e 20   defined), then 
16e4a 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20  it is important 
16e4b 74 6f 20 63 61 6c 6c 20 4f 50 5f 44 65 73 74 72  to call OP_Destr
16e4c 6f 79 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 74  oy on the.  ** t
16e4d 61 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 20 72  able and index r
16e4e 6f 6f 74 2d 70 61 67 65 73 20 69 6e 20 6f 72 64  oot-pages in ord
16e4f 65 72 2c 20 73 74 61 72 74 69 6e 67 20 77 69 74  er, starting wit
16e50 68 20 74 68 65 20 6e 75 6d 65 72 69 63 61 6c 6c  h the numericall
16e51 79 20 0a 20 20 2a 2a 20 6c 61 72 67 65 73 74 20  y .  ** largest 
16e52 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72  root-page number
16e53 2e 20 54 68 69 73 20 67 75 61 72 61 6e 74 65 65  . This guarantee
16e54 73 20 74 68 61 74 20 6e 6f 6e 65 20 6f 66 20 74  s that none of t
16e55 68 65 20 72 6f 6f 74 2d 70 61 67 65 73 0a 20 20  he root-pages.  
16e56 2a 2a 20 74 6f 20 62 65 20 64 65 73 74 72 6f 79  ** to be destroy
16e57 65 64 20 69 73 20 72 65 6c 6f 63 61 74 65 64 20  ed is relocated 
16e58 62 79 20 61 6e 20 65 61 72 6c 69 65 72 20 4f 50  by an earlier OP
16e59 5f 44 65 73 74 72 6f 79 2e 20 69 2e 65 2e 20 69  _Destroy. i.e. i
16e5a 66 20 74 68 65 0a 20 20 2a 2a 20 66 6f 6c 6c 6f  f the.  ** follo
16e5b 77 69 6e 67 20 77 65 72 65 20 63 6f 64 65 64 3a  wing were coded:
16e5c 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4f 50 5f 44 65  .  **.  ** OP_De
16e5d 73 74 72 6f 79 20 34 20 30 0a 20 20 2a 2a 20 2e  stroy 4 0.  ** .
16e5e 2e 2e 0a 20 20 2a 2a 20 4f 50 5f 44 65 73 74 72  ...  ** OP_Destr
16e5f 6f 79 20 35 20 30 0a 20 20 2a 2a 0a 20 20 2a 2a  oy 5 0.  **.  **
16e60 20 61 6e 64 20 72 6f 6f 74 20 70 61 67 65 20 35   and root page 5
16e61 20 68 61 70 70 65 6e 65 64 20 74 6f 20 62 65 20   happened to be 
16e62 74 68 65 20 6c 61 72 67 65 73 74 20 72 6f 6f 74  the largest root
16e63 2d 70 61 67 65 20 6e 75 6d 62 65 72 20 69 6e 20  -page number in 
16e64 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73  the.  ** databas
16e65 65 2c 20 74 68 65 6e 20 72 6f 6f 74 20 70 61 67  e, then root pag
16e66 65 20 35 20 77 6f 75 6c 64 20 62 65 20 6d 6f 76  e 5 would be mov
16e67 65 64 20 74 6f 20 70 61 67 65 20 34 20 62 79 20  ed to page 4 by 
16e68 74 68 65 20 0a 20 20 2a 2a 20 22 4f 50 5f 44 65  the .  ** "OP_De
16e69 73 74 72 6f 79 20 34 20 30 22 20 6f 70 63 6f 64  stroy 4 0" opcod
16e6a 65 2e 20 54 68 65 20 73 75 62 73 65 71 75 65 6e  e. The subsequen
16e6b 74 20 22 4f 50 5f 44 65 73 74 72 6f 79 20 35 20  t "OP_Destroy 5 
16e6c 30 22 20 77 6f 75 6c 64 20 68 69 74 0a 20 20 2a  0" would hit.  *
16e6d 2a 20 61 20 66 72 65 65 2d 6c 69 73 74 20 70 61  * a free-list pa
16e6e 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 69  ge..  */.  int i
16e6f 54 61 62 20 3d 20 70 54 61 62 2d 3e 74 6e 75 6d  Tab = pTab->tnum
16e70 3b 0a 20 20 69 6e 74 20 69 44 65 73 74 72 6f 79  ;.  int iDestroy
16e71 65 64 20 3d 20 30 3b 0a 0a 20 20 77 68 69 6c 65  ed = 0;..  while
16e72 28 20 31 20 29 7b 0a 20 20 20 20 49 6e 64 65 78  ( 1 ){.    Index
16e73 20 2a 70 49 64 78 3b 0a 20 20 20 20 69 6e 74 20   *pIdx;.    int 
16e74 69 4c 61 72 67 65 73 74 20 3d 20 30 3b 0a 0a 20  iLargest = 0;.. 
16e75 20 20 20 69 66 28 20 69 44 65 73 74 72 6f 79 65     if( iDestroye
16e76 64 3d 3d 30 20 7c 7c 20 69 54 61 62 3c 69 44 65  d==0 || iTab<iDe
16e77 73 74 72 6f 79 65 64 20 29 7b 0a 20 20 20 20 20  stroyed ){.     
16e78 20 69 4c 61 72 67 65 73 74 20 3d 20 69 54 61 62   iLargest = iTab
16e79 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28  ;.    }.    for(
16e7a 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65  pIdx=pTab->pInde
16e7b 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49  x; pIdx; pIdx=pI
16e7c 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  dx->pNext){.    
16e7d 20 20 69 6e 74 20 69 49 64 78 20 3d 20 70 49 64    int iIdx = pId
16e7e 78 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20 20 20 61  x->tnum;.      a
16e7f 73 73 65 72 74 28 20 70 49 64 78 2d 3e 70 53 63  ssert( pIdx->pSc
16e80 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68  hema==pTab->pSch
16e81 65 6d 61 20 29 3b 0a 20 20 20 20 20 20 69 66 28  ema );.      if(
16e82 20 28 69 44 65 73 74 72 6f 79 65 64 3d 3d 30 20   (iDestroyed==0 
16e83 7c 7c 20 28 69 49 64 78 3c 69 44 65 73 74 72 6f  || (iIdx<iDestro
16e84 79 65 64 29 29 20 26 26 20 69 49 64 78 3e 69 4c  yed)) && iIdx>iL
16e85 61 72 67 65 73 74 20 29 7b 0a 20 20 20 20 20 20  argest ){.      
16e86 20 20 69 4c 61 72 67 65 73 74 20 3d 20 69 49 64    iLargest = iId
16e87 78 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  x;.      }.    }
16e88 0a 20 20 20 20 69 66 28 20 69 4c 61 72 67 65 73  .    if( iLarges
16e89 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65  t==0 ){.      re
16e8a 74 75 72 6e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  turn;.    }else{
16e8b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 20 3d  .      int iDb =
16e8c 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
16e8d 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62  Index(pParse->db
16e8e 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29  , pTab->pSchema)
16e8f 3b 0a 20 20 20 20 20 20 64 65 73 74 72 6f 79 52  ;.      destroyR
16e90 6f 6f 74 50 61 67 65 28 70 50 61 72 73 65 2c 20  ootPage(pParse, 
16e91 69 4c 61 72 67 65 73 74 2c 20 69 44 62 29 3b 0a  iLargest, iDb);.
16e92 20 20 20 20 20 20 69 44 65 73 74 72 6f 79 65 64        iDestroyed
16e93 20 3d 20 69 4c 61 72 67 65 73 74 3b 0a 20 20 20   = iLargest;.   
16e94 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a   }.  }.#endif.}.
16e95 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
16e96 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f  ine is called to
16e97 20 64 6f 20 74 68 65 20 77 6f 72 6b 20 6f 66 20   do the work of 
16e98 61 20 44 52 4f 50 20 54 41 42 4c 45 20 73 74 61  a DROP TABLE sta
16e99 74 65 6d 65 6e 74 2e 0a 2a 2a 20 70 4e 61 6d 65  tement..** pName
16e9a 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   is the name of 
16e9b 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20  the table to be 
16e9c 64 72 6f 70 70 65 64 2e 0a 2a 2f 0a 53 51 4c 49  dropped..*/.SQLI
16e9d 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
16e9e 73 71 6c 69 74 65 33 44 72 6f 70 54 61 62 6c 65  sqlite3DropTable
16e9f 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
16ea0 53 72 63 4c 69 73 74 20 2a 70 4e 61 6d 65 2c 20  SrcList *pName, 
16ea1 69 6e 74 20 69 73 56 69 65 77 2c 20 69 6e 74 20  int isView, int 
16ea2 6e 6f 45 72 72 29 7b 0a 20 20 54 61 62 6c 65 20  noErr){.  Table 
16ea3 2a 70 54 61 62 3b 0a 20 20 56 64 62 65 20 2a 76  *pTab;.  Vdbe *v
16ea4 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
16ea5 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
16ea6 69 6e 74 20 69 44 62 3b 0a 0a 20 20 69 66 28 20  int iDb;..  if( 
16ea7 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20  pParse->nErr || 
16ea8 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69  sqlite3MallocFai
16ea9 6c 65 64 28 29 20 29 7b 0a 20 20 20 20 67 6f 74  led() ){.    got
16eaa 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c  o exit_drop_tabl
16eab 65 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  e;.  }.  assert(
16eac 20 70 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20   pName->nSrc==1 
16ead 29 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69  );.  pTab = sqli
16eae 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70  te3LocateTable(p
16eaf 50 61 72 73 65 2c 20 70 4e 61 6d 65 2d 3e 61 5b  Parse, pName->a[
16eb0 30 5d 2e 7a 4e 61 6d 65 2c 20 70 4e 61 6d 65 2d  0].zName, pName-
16eb1 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61 73 65 29  >a[0].zDatabase)
16eb2 3b 0a 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30  ;..  if( pTab==0
16eb3 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 6f 45 72   ){.    if( noEr
16eb4 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  r ){.      sqlit
16eb5 65 33 45 72 72 6f 72 43 6c 65 61 72 28 70 50 61  e3ErrorClear(pPa
16eb6 72 73 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rse);.    }.    
16eb7 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74  goto exit_drop_t
16eb8 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 69 44 62 20  able;.  }.  iDb 
16eb9 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
16eba 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d  oIndex(db, pTab-
16ebb 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 61 73 73  >pSchema);.  ass
16ebc 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69  ert( iDb>=0 && i
16ebd 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 23 69  Db<db->nDb );.#i
16ebe 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
16ebf 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a  T_AUTHORIZATION.
16ec0 20 20 7b 0a 20 20 20 20 69 6e 74 20 63 6f 64 65    {.    int code
16ec1 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ;.    const char
16ec2 20 2a 7a 54 61 62 20 3d 20 53 43 48 45 4d 41 5f   *zTab = SCHEMA_
16ec3 54 41 42 4c 45 28 69 44 62 29 3b 0a 20 20 20 20  TABLE(iDb);.    
16ec4 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20  const char *zDb 
16ec5 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  = db->aDb[iDb].z
16ec6 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20  Name;.    const 
16ec7 63 68 61 72 20 2a 7a 41 72 67 32 20 3d 20 30 3b  char *zArg2 = 0;
16ec8 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
16ec9 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
16eca 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c  , SQLITE_DELETE,
16ecb 20 7a 54 61 62 2c 20 30 2c 20 7a 44 62 29 29 7b   zTab, 0, zDb)){
16ecc 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74  .      goto exit
16ecd 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 20  _drop_table;.   
16ece 20 7d 0a 20 20 20 20 69 66 28 20 69 73 56 69 65   }.    if( isVie
16ecf 77 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21  w ){.      if( !
16ed0 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69  OMIT_TEMPDB && i
16ed1 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20  Db==1 ){.       
16ed2 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44   code = SQLITE_D
16ed3 52 4f 50 5f 54 45 4d 50 5f 56 49 45 57 3b 0a 20  ROP_TEMP_VIEW;. 
16ed4 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
16ed5 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54      code = SQLIT
16ed6 45 5f 44 52 4f 50 5f 56 49 45 57 3b 0a 20 20 20  E_DROP_VIEW;.   
16ed7 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c     }.#ifndef SQL
16ed8 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
16ed9 54 41 42 4c 45 0a 20 20 20 20 7d 65 6c 73 65 20  TABLE.    }else 
16eda 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54  if( IsVirtual(pT
16edb 61 62 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ab) ){.      if(
16edc 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43   sqlite3ViewGetC
16edd 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73  olumnNames(pPars
16ede 65 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20 20  e, pTab) ){.    
16edf 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72      goto exit_dr
16ee0 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 20 20 20 20  op_table;.      
16ee1 7d 0a 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53  }.      code = S
16ee2 51 4c 49 54 45 5f 44 52 4f 50 5f 56 54 41 42 4c  QLITE_DROP_VTABL
16ee3 45 3b 0a 20 20 20 20 20 20 7a 41 72 67 32 20 3d  E;.      zArg2 =
16ee4 20 70 54 61 62 2d 3e 70 4d 6f 64 2d 3e 7a 4e 61   pTab->pMod->zNa
16ee5 6d 65 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d  me;.#endif.    }
16ee6 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20  else{.      if( 
16ee7 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20  !OMIT_TEMPDB && 
16ee8 69 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20  iDb==1 ){.      
16ee9 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f    code = SQLITE_
16eea 44 52 4f 50 5f 54 45 4d 50 5f 54 41 42 4c 45 3b  DROP_TEMP_TABLE;
16eeb 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
16eec 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c        code = SQL
16eed 49 54 45 5f 44 52 4f 50 5f 54 41 42 4c 45 3b 0a  ITE_DROP_TABLE;.
16eee 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
16eef 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74    if( sqlite3Aut
16ef0 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 63  hCheck(pParse, c
16ef1 6f 64 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ode, pTab->zName
16ef2 2c 20 7a 41 72 67 32 2c 20 7a 44 62 29 20 29 7b  , zArg2, zDb) ){
16ef3 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74  .      goto exit
16ef4 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 20  _drop_table;.   
16ef5 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74   }.    if( sqlit
16ef6 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
16ef7 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54  se, SQLITE_DELET
16ef8 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20  E, pTab->zName, 
16ef9 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20  0, zDb) ){.     
16efa 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f   goto exit_drop_
16efb 74 61 62 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 7d  table;.    }.  }
16efc 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70 54  .#endif.  if( pT
16efd 61 62 2d 3e 72 65 61 64 4f 6e 6c 79 20 7c 7c 20  ab->readOnly || 
16efe 70 54 61 62 3d 3d 64 62 2d 3e 61 44 62 5b 69 44  pTab==db->aDb[iD
16eff 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 70 53 65 71  b].pSchema->pSeq
16f00 54 61 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Tab ){.    sqlit
16f01 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
16f02 65 2c 20 22 74 61 62 6c 65 20 25 73 20 6d 61 79  e, "table %s may
16f03 20 6e 6f 74 20 62 65 20 64 72 6f 70 70 65 64 22   not be dropped"
16f04 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
16f05 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72      goto exit_dr
16f06 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 0a 23  op_table;.  }..#
16f07 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
16f08 49 54 5f 56 49 45 57 0a 20 20 2f 2a 20 45 6e 73  IT_VIEW.  /* Ens
16f09 75 72 65 20 44 52 4f 50 20 54 41 42 4c 45 20 69  ure DROP TABLE i
16f0a 73 20 6e 6f 74 20 75 73 65 64 20 6f 6e 20 61 20  s not used on a 
16f0b 76 69 65 77 2c 20 61 6e 64 20 44 52 4f 50 20 56  view, and DROP V
16f0c 49 45 57 20 69 73 20 6e 6f 74 20 75 73 65 64 0a  IEW is not used.
16f0d 20 20 2a 2a 20 6f 6e 20 61 20 74 61 62 6c 65 2e    ** on a table.
16f0e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 56 69  .  */.  if( isVi
16f0f 65 77 20 26 26 20 70 54 61 62 2d 3e 70 53 65 6c  ew && pTab->pSel
16f10 65 63 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  ect==0 ){.    sq
16f11 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
16f12 61 72 73 65 2c 20 22 75 73 65 20 44 52 4f 50 20  arse, "use DROP 
16f13 54 41 42 4c 45 20 74 6f 20 64 65 6c 65 74 65 20  TABLE to delete 
16f14 74 61 62 6c 65 20 25 73 22 2c 20 70 54 61 62 2d  table %s", pTab-
16f15 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74  >zName);.    got
16f16 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c  o exit_drop_tabl
16f17 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 69 73  e;.  }.  if( !is
16f18 56 69 65 77 20 26 26 20 70 54 61 62 2d 3e 70 53  View && pTab->pS
16f19 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c  elect ){.    sql
16f1a 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
16f1b 72 73 65 2c 20 22 75 73 65 20 44 52 4f 50 20 56  rse, "use DROP V
16f1c 49 45 57 20 74 6f 20 64 65 6c 65 74 65 20 76 69  IEW to delete vi
16f1d 65 77 20 25 73 22 2c 20 70 54 61 62 2d 3e 7a 4e  ew %s", pTab->zN
16f1e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65  ame);.    goto e
16f1f 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a  xit_drop_table;.
16f20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
16f21 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
16f22 6f 20 72 65 6d 6f 76 65 20 74 68 65 20 74 61 62  o remove the tab
16f23 6c 65 20 66 72 6f 6d 20 74 68 65 20 6d 61 73 74  le from the mast
16f24 65 72 20 74 61 62 6c 65 0a 20 20 2a 2a 20 6f 6e  er table.  ** on
16f25 20 64 69 73 6b 2e 0a 20 20 2a 2f 0a 20 20 76 20   disk..  */.  v 
16f26 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
16f27 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20  (pParse);.  if( 
16f28 76 20 29 7b 0a 20 20 20 20 54 72 69 67 67 65 72  v ){.    Trigger
16f29 20 2a 70 54 72 69 67 67 65 72 3b 0a 20 20 20 20   *pTrigger;.    
16f2a 44 62 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61  Db *pDb = &db->a
16f2b 44 62 5b 69 44 62 5d 3b 0a 20 20 20 20 73 71 6c  Db[iDb];.    sql
16f2c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70  ite3BeginWriteOp
16f2d 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20  eration(pParse, 
16f2e 30 2c 20 69 44 62 29 3b 0a 0a 23 69 66 6e 64 65  0, iDb);..#ifnde
16f2f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
16f30 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 69  RTUALTABLE.    i
16f31 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  f( IsVirtual(pTa
16f32 62 29 20 29 7b 0a 20 20 20 20 20 20 56 64 62 65  b) ){.      Vdbe
16f33 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74   *v = sqlite3Get
16f34 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
16f35 20 20 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20      if( v ){.   
16f36 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
16f37 41 64 64 4f 70 28 76 2c 20 4f 50 5f 56 42 65 67  AddOp(v, OP_VBeg
16f38 69 6e 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20  in, 0, 0);.     
16f39 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a   }.    }.#endif.
16f3a 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 61 6c 6c  .    /* Drop all
16f3b 20 74 72 69 67 67 65 72 73 20 61 73 73 6f 63 69   triggers associ
16f3c 61 74 65 64 20 77 69 74 68 20 74 68 65 20 74 61  ated with the ta
16f3d 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70 65  ble being droppe
16f3e 64 2e 20 43 6f 64 65 0a 20 20 20 20 2a 2a 20 69  d. Code.    ** i
16f3f 73 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20 72  s generated to r
16f40 65 6d 6f 76 65 20 65 6e 74 72 69 65 73 20 66 72  emove entries fr
16f41 6f 6d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  om sqlite_master
16f42 20 61 6e 64 2f 6f 72 0a 20 20 20 20 2a 2a 20 73   and/or.    ** s
16f43 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65  qlite_temp_maste
16f44 72 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a 20  r if required.. 
16f45 20 20 20 2a 2f 0a 20 20 20 20 70 54 72 69 67 67     */.    pTrigg
16f46 65 72 20 3d 20 70 54 61 62 2d 3e 70 54 72 69 67  er = pTab->pTrig
16f47 67 65 72 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  ger;.    while( 
16f48 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20  pTrigger ){.    
16f49 20 20 61 73 73 65 72 74 28 20 70 54 72 69 67 67    assert( pTrigg
16f4a 65 72 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61  er->pSchema==pTa
16f4b 62 2d 3e 70 53 63 68 65 6d 61 20 7c 7c 20 0a 20  b->pSchema || . 
16f4c 20 20 20 20 20 20 20 20 20 70 54 72 69 67 67 65           pTrigge
16f4d 72 2d 3e 70 53 63 68 65 6d 61 3d 3d 64 62 2d 3e  r->pSchema==db->
16f4e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29  aDb[1].pSchema )
16f4f 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  ;.      sqlite3D
16f50 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 70 50  ropTriggerPtr(pP
16f51 61 72 73 65 2c 20 70 54 72 69 67 67 65 72 29 3b  arse, pTrigger);
16f52 0a 20 20 20 20 20 20 70 54 72 69 67 67 65 72 20  .      pTrigger 
16f53 3d 20 70 54 72 69 67 67 65 72 2d 3e 70 4e 65 78  = pTrigger->pNex
16f54 74 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65  t;.    }..#ifnde
16f55 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
16f56 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 20 20  TOINCREMENT.    
16f57 2f 2a 20 52 65 6d 6f 76 65 20 61 6e 79 20 65 6e  /* Remove any en
16f58 74 72 69 65 73 20 6f 66 20 74 68 65 20 73 71 6c  tries of the sql
16f59 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62  ite_sequence tab
16f5a 6c 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  le associated wi
16f5b 74 68 0a 20 20 20 20 2a 2a 20 74 68 65 20 74 61  th.    ** the ta
16f5c 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70 65  ble being droppe
16f5d 64 2e 20 54 68 69 73 20 69 73 20 64 6f 6e 65 20  d. This is done 
16f5e 62 65 66 6f 72 65 20 74 68 65 20 74 61 62 6c 65  before the table
16f5f 20 69 73 20 64 72 6f 70 70 65 64 0a 20 20 20 20   is dropped.    
16f60 2a 2a 20 61 74 20 74 68 65 20 62 74 72 65 65 20  ** at the btree 
16f61 6c 65 76 65 6c 2c 20 69 6e 20 63 61 73 65 20 74  level, in case t
16f62 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e  he sqlite_sequen
16f63 63 65 20 74 61 62 6c 65 20 6e 65 65 64 73 20 74  ce table needs t
16f64 6f 0a 20 20 20 20 2a 2a 20 6d 6f 76 65 20 61 73  o.    ** move as
16f65 20 61 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65   a result of the
16f66 20 64 72 6f 70 20 28 63 61 6e 20 68 61 70 70 65   drop (can happe
16f67 6e 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d  n in auto-vacuum
16f68 20 6d 6f 64 65 29 2e 0a 20 20 20 20 2a 2f 0a 20   mode)..    */. 
16f69 20 20 20 69 66 28 20 70 54 61 62 2d 3e 61 75 74     if( pTab->aut
16f6a 6f 49 6e 63 20 29 7b 0a 20 20 20 20 20 20 73 71  oInc ){.      sq
16f6b 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
16f6c 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20  (pParse,.       
16f6d 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 73   "DELETE FROM %s
16f6e 2e 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65  .sqlite_sequence
16f6f 20 57 48 45 52 45 20 6e 61 6d 65 3d 25 51 22 2c   WHERE name=%Q",
16f70 0a 20 20 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e  .        pDb->zN
16f71 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ame, pTab->zName
16f72 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a  .      );.    }.
16f73 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 44  #endif..    /* D
16f74 72 6f 70 20 61 6c 6c 20 53 51 4c 49 54 45 5f 4d  rop all SQLITE_M
16f75 41 53 54 45 52 20 74 61 62 6c 65 20 61 6e 64 20  ASTER table and 
16f76 69 6e 64 65 78 20 65 6e 74 72 69 65 73 20 74 68  index entries th
16f77 61 74 20 72 65 66 65 72 20 74 6f 20 74 68 65 0a  at refer to the.
16f78 20 20 20 20 2a 2a 20 74 61 62 6c 65 2e 20 54 68      ** table. Th
16f79 65 20 70 72 6f 67 72 61 6d 20 6e 61 6d 65 20 6c  e program name l
16f7a 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 74 68 65  oops through the
16f7b 20 6d 61 73 74 65 72 20 74 61 62 6c 65 20 61 6e   master table an
16f7c 64 20 64 65 6c 65 74 65 73 0a 20 20 20 20 2a 2a  d deletes.    **
16f7d 20 65 76 65 72 79 20 72 6f 77 20 74 68 61 74 20   every row that 
16f7e 72 65 66 65 72 73 20 74 6f 20 61 20 74 61 62 6c  refers to a tabl
16f7f 65 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6e 61  e of the same na
16f80 6d 65 20 61 73 20 74 68 65 20 6f 6e 65 20 62 65  me as the one be
16f81 69 6e 67 0a 20 20 20 20 2a 2a 20 64 72 6f 70 70  ing.    ** dropp
16f82 65 64 2e 20 54 72 69 67 67 65 72 73 20 61 72 65  ed. Triggers are
16f83 20 68 61 6e 64 6c 65 64 20 73 65 70 65 72 61 74   handled seperat
16f84 65 6c 79 20 62 65 63 61 75 73 65 20 61 20 74 72  ely because a tr
16f85 69 67 67 65 72 20 63 61 6e 20 62 65 0a 20 20 20  igger can be.   
16f86 20 2a 2a 20 63 72 65 61 74 65 64 20 69 6e 20 74   ** created in t
16f87 68 65 20 74 65 6d 70 20 64 61 74 61 62 61 73 65  he temp database
16f88 20 74 68 61 74 20 72 65 66 65 72 73 20 74 6f 20   that refers to 
16f89 61 20 74 61 62 6c 65 20 69 6e 20 61 6e 6f 74 68  a table in anoth
16f8a 65 72 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61  er.    ** databa
16f8b 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73  se..    */.    s
16f8c 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73  qlite3NestedPars
16f8d 65 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20  e(pParse, .     
16f8e 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20     "DELETE FROM 
16f8f 25 51 2e 25 73 20 57 48 45 52 45 20 74 62 6c 5f  %Q.%s WHERE tbl_
16f90 6e 61 6d 65 3d 25 51 20 61 6e 64 20 74 79 70 65  name=%Q and type
16f91 21 3d 27 74 72 69 67 67 65 72 27 22 2c 0a 20 20  !='trigger'",.  
16f92 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d 65        pDb->zName
16f93 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69  , SCHEMA_TABLE(i
16f94 44 62 29 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  Db), pTab->zName
16f95 29 3b 0a 20 20 20 20 69 66 28 20 21 69 73 56 69  );.    if( !isVi
16f96 65 77 20 26 26 20 21 49 73 56 69 72 74 75 61 6c  ew && !IsVirtual
16f97 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20  (pTab) ){.      
16f98 64 65 73 74 72 6f 79 54 61 62 6c 65 28 70 50 61  destroyTable(pPa
16f99 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 20  rse, pTab);.    
16f9a 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65  }..    /* Remove
16f9b 20 74 68 65 20 74 61 62 6c 65 20 65 6e 74 72 79   the table entry
16f9c 20 66 72 6f 6d 20 53 51 4c 69 74 65 27 73 20 69   from SQLite's i
16f9d 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 20 61  nternal schema a
16f9e 6e 64 20 6d 6f 64 69 66 79 0a 20 20 20 20 2a 2a  nd modify.    **
16f9f 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b   the schema cook
16fa0 69 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ie..    */.    i
16fa1 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  f( IsVirtual(pTa
16fa2 62 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  b) ){.      sqli
16fa3 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20 4f 50  te3VdbeOp3(v, OP
16fa4 5f 56 44 65 73 74 72 6f 79 2c 20 69 44 62 2c 20  _VDestroy, iDb, 
16fa5 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20  0, pTab->zName, 
16fa6 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  0);.    }.    sq
16fa7 6c 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20  lite3VdbeOp3(v, 
16fa8 4f 50 5f 44 72 6f 70 54 61 62 6c 65 2c 20 69 44  OP_DropTable, iD
16fa9 62 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  b, 0, pTab->zNam
16faa 65 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  e, 0);.    sqlit
16fab 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 64  e3ChangeCookie(d
16fac 62 2c 20 76 2c 20 69 44 62 29 3b 0a 20 20 7d 0a  b, v, iDb);.  }.
16fad 20 20 73 71 6c 69 74 65 56 69 65 77 52 65 73 65    sqliteViewRese
16fae 74 41 6c 6c 28 64 62 2c 20 69 44 62 29 3b 0a 0a  tAll(db, iDb);..
16faf 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3a  exit_drop_table:
16fb0 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  .  sqlite3SrcLis
16fb1 74 44 65 6c 65 74 65 28 70 4e 61 6d 65 29 3b 0a  tDelete(pName);.
16fb2 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
16fb3 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
16fb4 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 20  to create a new 
16fb5 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20 74  foreign key on t
16fb6 68 65 20 74 61 62 6c 65 0a 2a 2a 20 63 75 72 72  he table.** curr
16fb7 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73  ently under cons
16fb8 74 72 75 63 74 69 6f 6e 2e 20 20 70 46 72 6f 6d  truction.  pFrom
16fb9 43 6f 6c 20 64 65 74 65 72 6d 69 6e 65 73 20 77  Col determines w
16fba 68 69 63 68 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20  hich columns.** 
16fbb 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  in the current t
16fbc 61 62 6c 65 20 70 6f 69 6e 74 20 74 6f 20 74 68  able point to th
16fbd 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 2e 20 20  e foreign key.  
16fbe 49 66 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20 74  If pFromCol==0 t
16fbf 68 65 6e 0a 2a 2a 20 63 6f 6e 6e 65 63 74 20 74  hen.** connect t
16fc0 68 65 20 6b 65 79 20 74 6f 20 74 68 65 20 6c 61  he key to the la
16fc1 73 74 20 63 6f 6c 75 6d 6e 20 69 6e 73 65 72 74  st column insert
16fc2 65 64 2e 20 20 70 54 6f 20 69 73 20 74 68 65 20  ed.  pTo is the 
16fc3 6e 61 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20 74  name of.** the t
16fc4 61 62 6c 65 20 72 65 66 65 72 72 65 64 20 74 6f  able referred to
16fc5 2e 20 20 70 54 6f 43 6f 6c 20 69 73 20 61 20 6c  .  pToCol is a l
16fc6 69 73 74 20 6f 66 20 74 61 62 6c 65 73 20 69 6e  ist of tables in
16fc7 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 70 54   the other.** pT
16fc8 6f 20 74 61 62 6c 65 20 74 68 61 74 20 74 68 65  o table that the
16fc9 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 70 6f 69   foreign key poi
16fca 6e 74 73 20 74 6f 2e 20 20 66 6c 61 67 73 20 63  nts to.  flags c
16fcb 6f 6e 74 61 69 6e 73 20 61 6c 6c 0a 2a 2a 20 69  ontains all.** i
16fcc 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
16fcd 20 74 68 65 20 63 6f 6e 66 6c 69 63 74 20 72 65   the conflict re
16fce 73 6f 6c 75 74 69 6f 6e 20 61 6c 67 6f 72 69 74  solution algorit
16fcf 68 6d 73 20 73 70 65 63 69 66 69 65 64 0a 2a 2a  hms specified.**
16fd0 20 69 6e 20 74 68 65 20 4f 4e 20 44 45 4c 45 54   in the ON DELET
16fd1 45 2c 20 4f 4e 20 55 50 44 41 54 45 20 61 6e 64  E, ON UPDATE and
16fd2 20 4f 4e 20 49 4e 53 45 52 54 20 63 6c 61 75 73   ON INSERT claus
16fd3 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 46 4b 65  es..**.** An FKe
16fd4 79 20 73 74 72 75 63 74 75 72 65 20 69 73 20 63  y structure is c
16fd5 72 65 61 74 65 64 20 61 6e 64 20 61 64 64 65 64  reated and added
16fd6 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 63 75   to the table cu
16fd7 72 72 65 6e 74 6c 79 0a 2a 2a 20 75 6e 64 65 72  rrently.** under
16fd8 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e   construction in
16fd9 20 74 68 65 20 70 50 61 72 73 65 2d 3e 70 4e 65   the pParse->pNe
16fda 77 54 61 62 6c 65 20 66 69 65 6c 64 2e 20 20 54  wTable field.  T
16fdb 68 65 20 6e 65 77 20 46 4b 65 79 0a 2a 2a 20 69  he new FKey.** i
16fdc 73 20 6e 6f 74 20 6c 69 6e 6b 65 64 20 69 6e 74  s not linked int
16fdd 6f 20 64 62 2d 3e 61 46 4b 65 79 20 61 74 20 74  o db->aFKey at t
16fde 68 69 73 20 70 6f 69 6e 74 20 2d 20 74 68 61 74  his point - that
16fdf 20 64 6f 65 73 20 6e 6f 74 20 68 61 70 70 65 6e   does not happen
16fe0 0a 2a 2a 20 75 6e 74 69 6c 20 73 71 6c 69 74 65  .** until sqlite
16fe1 33 45 6e 64 54 61 62 6c 65 28 29 2e 0a 2a 2a 0a  3EndTable()..**.
16fe2 2a 2a 20 54 68 65 20 66 6f 72 65 69 67 6e 20 6b  ** The foreign k
16fe3 65 79 20 69 73 20 73 65 74 20 66 6f 72 20 49 4d  ey is set for IM
16fe4 4d 45 44 49 41 54 45 20 70 72 6f 63 65 73 73 69  MEDIATE processi
16fe5 6e 67 2e 20 20 41 20 73 75 62 73 65 71 75 65 6e  ng.  A subsequen
16fe6 74 20 63 61 6c 6c 0a 2a 2a 20 74 6f 20 73 71 6c  t call.** to sql
16fe7 69 74 65 33 44 65 66 65 72 46 6f 72 65 69 67 6e  ite3DeferForeign
16fe8 4b 65 79 28 29 20 6d 69 67 68 74 20 63 68 61 6e  Key() might chan
16fe9 67 65 20 74 68 69 73 20 74 6f 20 44 45 46 45 52  ge this to DEFER
16fea 52 45 44 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  RED..*/.SQLITE_P
16feb 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
16fec 74 65 33 43 72 65 61 74 65 46 6f 72 65 69 67 6e  te3CreateForeign
16fed 4b 65 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50  Key(.  Parse *pP
16fee 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 50  arse,       /* P
16fef 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
16ff0 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 46  /.  ExprList *pF
16ff1 72 6f 6d 43 6f 6c 2c 20 20 2f 2a 20 43 6f 6c 75  romCol,  /* Colu
16ff2 6d 6e 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c  mns in this tabl
16ff3 65 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20  e that point to 
16ff4 6f 74 68 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20  other table */. 
16ff5 20 54 6f 6b 65 6e 20 2a 70 54 6f 2c 20 20 20 20   Token *pTo,    
16ff6 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
16ff7 20 74 68 65 20 6f 74 68 65 72 20 74 61 62 6c 65   the other table
16ff8 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
16ff9 70 54 6f 43 6f 6c 2c 20 20 20 20 2f 2a 20 43 6f  pToCol,    /* Co
16ffa 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 6f 74 68  lumns in the oth
16ffb 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  er table */.  in
16ffc 74 20 66 6c 61 67 73 20 20 20 20 20 20 20 20 20  t flags         
16ffd 20 20 20 2f 2a 20 43 6f 6e 66 6c 69 63 74 20 72     /* Conflict r
16ffe 65 73 6f 6c 75 74 69 6f 6e 20 61 6c 67 6f 72 69  esolution algori
16fff 74 68 6d 73 2e 20 2a 2f 0a 29 7b 0a 23 69 66 6e  thms. */.){.#ifn
17000 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
17001 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 46 4b  FOREIGN_KEY.  FK
17002 65 79 20 2a 70 46 4b 65 79 20 3d 20 30 3b 0a 20  ey *pFKey = 0;. 
17003 20 54 61 62 6c 65 20 2a 70 20 3d 20 70 50 61 72   Table *p = pPar
17004 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20  se->pNewTable;. 
17005 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 69 6e   int nByte;.  in
17006 74 20 69 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b  t i;.  int nCol;
17007 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 61  .  char *z;..  a
17008 73 73 65 72 74 28 20 70 54 6f 21 3d 30 20 29 3b  ssert( pTo!=0 );
17009 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 70  .  if( p==0 || p
1700a 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 49  Parse->nErr || I
1700b 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 29  N_DECLARE_VTAB )
1700c 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20   goto fk_end;.  
1700d 69 66 28 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20  if( pFromCol==0 
1700e 29 7b 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 20  ){.    int iCol 
1700f 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 20  = p->nCol-1;.   
17010 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 20 67 6f   if( iCol<0 ) go
17011 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 20 20 69  to fk_end;.    i
17012 66 28 20 70 54 6f 43 6f 6c 20 26 26 20 70 54 6f  f( pToCol && pTo
17013 43 6f 6c 2d 3e 6e 45 78 70 72 21 3d 31 20 29 7b  Col->nExpr!=1 ){
17014 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
17015 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
17016 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20 25  foreign key on %
17017 73 22 0a 20 20 20 20 20 20 20 20 20 22 20 73 68  s".         " sh
17018 6f 75 6c 64 20 72 65 66 65 72 65 6e 63 65 20 6f  ould reference o
17019 6e 6c 79 20 6f 6e 65 20 63 6f 6c 75 6d 6e 20 6f  nly one column o
1701a 66 20 74 61 62 6c 65 20 25 54 22 2c 0a 20 20 20  f table %T",.   
1701b 20 20 20 20 20 20 70 2d 3e 61 43 6f 6c 5b 69 43        p->aCol[iC
1701c 6f 6c 5d 2e 7a 4e 61 6d 65 2c 20 70 54 6f 29 3b  ol].zName, pTo);
1701d 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65  .      goto fk_e
1701e 6e 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 43  nd;.    }.    nC
1701f 6f 6c 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 20  ol = 1;.  }else 
17020 69 66 28 20 70 54 6f 43 6f 6c 20 26 26 20 70 54  if( pToCol && pT
17021 6f 43 6f 6c 2d 3e 6e 45 78 70 72 21 3d 70 46 72  oCol->nExpr!=pFr
17022 6f 6d 43 6f 6c 2d 3e 6e 45 78 70 72 20 29 7b 0a  omCol->nExpr ){.
17023 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
17024 4d 73 67 28 70 50 61 72 73 65 2c 0a 20 20 20 20  Msg(pParse,.    
17025 20 20 20 20 22 6e 75 6d 62 65 72 20 6f 66 20 63      "number of c
17026 6f 6c 75 6d 6e 73 20 69 6e 20 66 6f 72 65 69 67  olumns in foreig
17027 6e 20 6b 65 79 20 64 6f 65 73 20 6e 6f 74 20 6d  n key does not m
17028 61 74 63 68 20 74 68 65 20 6e 75 6d 62 65 72 20  atch the number 
17029 6f 66 20 22 0a 20 20 20 20 20 20 20 20 22 63 6f  of ".        "co
1702a 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 66  lumns in the ref
1702b 65 72 65 6e 63 65 64 20 74 61 62 6c 65 22 29 3b  erenced table");
1702c 0a 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64  .    goto fk_end
1702d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e  ;.  }else{.    n
1702e 43 6f 6c 20 3d 20 70 46 72 6f 6d 43 6f 6c 2d 3e  Col = pFromCol->
1702f 6e 45 78 70 72 3b 0a 20 20 7d 0a 20 20 6e 42 79  nExpr;.  }.  nBy
17030 74 65 20 3d 20 73 69 7a 65 6f 66 28 2a 70 46 4b  te = sizeof(*pFK
17031 65 79 29 20 2b 20 6e 43 6f 6c 2a 73 69 7a 65 6f  ey) + nCol*sizeo
17032 66 28 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 30 5d  f(pFKey->aCol[0]
17033 29 20 2b 20 70 54 6f 2d 3e 6e 20 2b 20 31 3b 0a  ) + pTo->n + 1;.
17034 20 20 69 66 28 20 70 54 6f 43 6f 6c 20 29 7b 0a    if( pToCol ){.
17035 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
17036 54 6f 43 6f 6c 2d 3e 6e 45 78 70 72 3b 20 69 2b  ToCol->nExpr; i+
17037 2b 29 7b 0a 20 20 20 20 20 20 6e 42 79 74 65 20  +){.      nByte 
17038 2b 3d 20 73 74 72 6c 65 6e 28 70 54 6f 43 6f 6c  += strlen(pToCol
17039 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 20 2b 20  ->a[i].zName) + 
1703a 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70  1;.    }.  }.  p
1703b 46 4b 65 79 20 3d 20 73 71 6c 69 74 65 4d 61 6c  FKey = sqliteMal
1703c 6c 6f 63 28 20 6e 42 79 74 65 20 29 3b 0a 20 20  loc( nByte );.  
1703d 69 66 28 20 70 46 4b 65 79 3d 3d 30 20 29 20 67  if( pFKey==0 ) g
1703e 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 70 46  oto fk_end;.  pF
1703f 4b 65 79 2d 3e 70 46 72 6f 6d 20 3d 20 70 3b 0a  Key->pFrom = p;.
17040 20 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74 46 72    pFKey->pNextFr
17041 6f 6d 20 3d 20 70 2d 3e 70 46 4b 65 79 3b 0a 20  om = p->pFKey;. 
17042 20 7a 20 3d 20 28 63 68 61 72 2a 29 26 70 46 4b   z = (char*)&pFK
17043 65 79 5b 31 5d 3b 0a 20 20 70 46 4b 65 79 2d 3e  ey[1];.  pFKey->
17044 61 43 6f 6c 20 3d 20 28 73 74 72 75 63 74 20 73  aCol = (struct s
17045 43 6f 6c 4d 61 70 2a 29 7a 3b 0a 20 20 7a 20 2b  ColMap*)z;.  z +
17046 3d 20 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20  = sizeof(struct 
17047 73 43 6f 6c 4d 61 70 29 2a 6e 43 6f 6c 3b 0a 20  sColMap)*nCol;. 
17048 20 70 46 4b 65 79 2d 3e 7a 54 6f 20 3d 20 7a 3b   pFKey->zTo = z;
17049 0a 20 20 6d 65 6d 63 70 79 28 7a 2c 20 70 54 6f  .  memcpy(z, pTo
1704a 2d 3e 7a 2c 20 70 54 6f 2d 3e 6e 29 3b 0a 20 20  ->z, pTo->n);.  
1704b 7a 5b 70 54 6f 2d 3e 6e 5d 20 3d 20 30 3b 0a 20  z[pTo->n] = 0;. 
1704c 20 7a 20 2b 3d 20 70 54 6f 2d 3e 6e 2b 31 3b 0a   z += pTo->n+1;.
1704d 20 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74 54 6f    pFKey->pNextTo
1704e 20 3d 20 30 3b 0a 20 20 70 46 4b 65 79 2d 3e 6e   = 0;.  pFKey->n
1704f 43 6f 6c 20 3d 20 6e 43 6f 6c 3b 0a 20 20 69 66  Col = nCol;.  if
17050 28 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20 29 7b  ( pFromCol==0 ){
17051 0a 20 20 20 20 70 46 4b 65 79 2d 3e 61 43 6f 6c  .    pFKey->aCol
17052 5b 30 5d 2e 69 46 72 6f 6d 20 3d 20 70 2d 3e 6e  [0].iFrom = p->n
17053 43 6f 6c 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Col-1;.  }else{.
17054 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
17055 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Col; i++){.     
17056 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 66 6f   int j;.      fo
17057 72 28 6a 3d 30 3b 20 6a 3c 70 2d 3e 6e 43 6f 6c  r(j=0; j<p->nCol
17058 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; j++){.        
17059 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
1705a 6d 70 28 70 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e  mp(p->aCol[j].zN
1705b 61 6d 65 2c 20 70 46 72 6f 6d 43 6f 6c 2d 3e 61  ame, pFromCol->a
1705c 5b 69 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b  [i].zName)==0 ){
1705d 0a 20 20 20 20 20 20 20 20 20 20 70 46 4b 65 79  .          pFKey
1705e 2d 3e 61 43 6f 6c 5b 69 5d 2e 69 46 72 6f 6d 20  ->aCol[i].iFrom 
1705f 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 62  = j;.          b
17060 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
17061 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
17062 28 20 6a 3e 3d 70 2d 3e 6e 43 6f 6c 20 29 7b 0a  ( j>=p->nCol ){.
17063 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
17064 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
17065 0a 20 20 20 20 20 20 20 20 20 20 22 75 6e 6b 6e  .          "unkn
17066 6f 77 6e 20 63 6f 6c 75 6d 6e 20 5c 22 25 73 5c  own column \"%s\
17067 22 20 69 6e 20 66 6f 72 65 69 67 6e 20 6b 65 79  " in foreign key
17068 20 64 65 66 69 6e 69 74 69 6f 6e 22 2c 20 0a 20   definition", . 
17069 20 20 20 20 20 20 20 20 20 70 46 72 6f 6d 43 6f           pFromCo
1706a 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a  l->a[i].zName);.
1706b 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 6b 5f          goto fk_
1706c 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  end;.      }.   
1706d 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 54 6f   }.  }.  if( pTo
1706e 43 6f 6c 20 29 7b 0a 20 20 20 20 66 6f 72 28 69  Col ){.    for(i
1706f 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29  =0; i<nCol; i++)
17070 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20  {.      int n = 
17071 73 74 72 6c 65 6e 28 70 54 6f 43 6f 6c 2d 3e 61  strlen(pToCol->a
17072 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  [i].zName);.    
17073 20 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 69 5d    pFKey->aCol[i]
17074 2e 7a 43 6f 6c 20 3d 20 7a 3b 0a 20 20 20 20 20  .zCol = z;.     
17075 20 6d 65 6d 63 70 79 28 7a 2c 20 70 54 6f 43 6f   memcpy(z, pToCo
17076 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 6e  l->a[i].zName, n
17077 29 3b 0a 20 20 20 20 20 20 7a 5b 6e 5d 20 3d 20  );.      z[n] = 
17078 30 3b 0a 20 20 20 20 20 20 7a 20 2b 3d 20 6e 2b  0;.      z += n+
17079 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70  1;.    }.  }.  p
1707a 46 4b 65 79 2d 3e 69 73 44 65 66 65 72 72 65 64  FKey->isDeferred
1707b 20 3d 20 30 3b 0a 20 20 70 46 4b 65 79 2d 3e 64   = 0;.  pFKey->d
1707c 65 6c 65 74 65 43 6f 6e 66 20 3d 20 66 6c 61 67  eleteConf = flag
1707d 73 20 26 20 30 78 66 66 3b 0a 20 20 70 46 4b 65  s & 0xff;.  pFKe
1707e 79 2d 3e 75 70 64 61 74 65 43 6f 6e 66 20 3d 20  y->updateConf = 
1707f 28 66 6c 61 67 73 20 3e 3e 20 38 20 29 20 26 20  (flags >> 8 ) & 
17080 30 78 66 66 3b 0a 20 20 70 46 4b 65 79 2d 3e 69  0xff;.  pFKey->i
17081 6e 73 65 72 74 43 6f 6e 66 20 3d 20 28 66 6c 61  nsertConf = (fla
17082 67 73 20 3e 3e 20 31 36 20 29 20 26 20 30 78 66  gs >> 16 ) & 0xf
17083 66 3b 0a 0a 20 20 2f 2a 20 4c 69 6e 6b 20 74 68  f;..  /* Link th
17084 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 74 6f  e foreign key to
17085 20 74 68 65 20 74 61 62 6c 65 20 61 73 20 74 68   the table as th
17086 65 20 6c 61 73 74 20 73 74 65 70 2e 0a 20 20 2a  e last step..  *
17087 2f 0a 20 20 70 2d 3e 70 46 4b 65 79 20 3d 20 70  /.  p->pFKey = p
17088 46 4b 65 79 3b 0a 20 20 70 46 4b 65 79 20 3d 20  FKey;.  pFKey = 
17089 30 3b 0a 0a 66 6b 5f 65 6e 64 3a 0a 20 20 73 71  0;..fk_end:.  sq
1708a 6c 69 74 65 46 72 65 65 28 70 46 4b 65 79 29 3b  liteFree(pFKey);
1708b 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69  .#endif /* !defi
1708c 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
1708d 46 4f 52 45 49 47 4e 5f 4b 45 59 29 20 2a 2f 0a  FOREIGN_KEY) */.
1708e 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
1708f 74 44 65 6c 65 74 65 28 70 46 72 6f 6d 43 6f 6c  tDelete(pFromCol
17090 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
17091 4c 69 73 74 44 65 6c 65 74 65 28 70 54 6f 43 6f  ListDelete(pToCo
17092 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  l);.}../*.** Thi
17093 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
17094 6c 65 64 20 77 68 65 6e 20 61 6e 20 49 4e 49 54  led when an INIT
17095 49 41 4c 4c 59 20 49 4d 4d 45 44 49 41 54 45 20  IALLY IMMEDIATE 
17096 6f 72 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46  or INITIALLY DEF
17097 45 52 52 45 44 0a 2a 2a 20 63 6c 61 75 73 65 20  ERRED.** clause 
17098 69 73 20 73 65 65 6e 20 61 73 20 70 61 72 74 20  is seen as part 
17099 6f 66 20 61 20 66 6f 72 65 69 67 6e 20 6b 65 79  of a foreign key
1709a 20 64 65 66 69 6e 69 74 69 6f 6e 2e 20 20 54 68   definition.  Th
1709b 65 20 69 73 44 65 66 65 72 72 65 64 0a 2a 2a 20  e isDeferred.** 
1709c 70 61 72 61 6d 65 74 65 72 20 69 73 20 31 20 66  parameter is 1 f
1709d 6f 72 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46  or INITIALLY DEF
1709e 45 52 52 45 44 20 61 6e 64 20 30 20 66 6f 72 20  ERRED and 0 for 
1709f 49 4e 49 54 49 41 4c 4c 59 20 49 4d 4d 45 44 49  INITIALLY IMMEDI
170a0 41 54 45 2e 0a 2a 2a 20 54 68 65 20 62 65 68 61  ATE..** The beha
170a1 76 69 6f 72 20 6f 66 20 74 68 65 20 6d 6f 73 74  vior of the most
170a2 20 72 65 63 65 6e 74 6c 79 20 63 72 65 61 74 65   recently create
170a3 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 73  d foreign key is
170a4 20 61 64 6a 75 73 74 65 64 0a 2a 2a 20 61 63 63   adjusted.** acc
170a5 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2f 0a 53 51 4c  ordingly..*/.SQL
170a6 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
170a7 20 73 71 6c 69 74 65 33 44 65 66 65 72 46 6f 72   sqlite3DeferFor
170a8 65 69 67 6e 4b 65 79 28 50 61 72 73 65 20 2a 70  eignKey(Parse *p
170a9 50 61 72 73 65 2c 20 69 6e 74 20 69 73 44 65 66  Parse, int isDef
170aa 65 72 72 65 64 29 7b 0a 23 69 66 6e 64 65 66 20  erred){.#ifndef 
170ab 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45  SQLITE_OMIT_FORE
170ac 49 47 4e 5f 4b 45 59 0a 20 20 54 61 62 6c 65 20  IGN_KEY.  Table 
170ad 2a 70 54 61 62 3b 0a 20 20 46 4b 65 79 20 2a 70  *pTab;.  FKey *p
170ae 46 4b 65 79 3b 0a 20 20 69 66 28 20 28 70 54 61  FKey;.  if( (pTa
170af 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  b = pParse->pNew
170b0 54 61 62 6c 65 29 3d 3d 30 20 7c 7c 20 28 70 46  Table)==0 || (pF
170b1 4b 65 79 20 3d 20 70 54 61 62 2d 3e 70 46 4b 65  Key = pTab->pFKe
170b2 79 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  y)==0 ) return;.
170b3 20 20 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72    pFKey->isDefer
170b4 72 65 64 20 3d 20 69 73 44 65 66 65 72 72 65 64  red = isDeferred
170b5 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a  ;.#endif.}../*.*
170b6 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
170b7 74 68 61 74 20 77 69 6c 6c 20 65 72 61 73 65 20  that will erase 
170b8 61 6e 64 20 72 65 66 69 6c 6c 20 69 6e 64 65 78  and refill index
170b9 20 2a 70 49 64 78 2e 20 20 54 68 69 73 20 69 73   *pIdx.  This is
170ba 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69 6e 69 74  .** used to init
170bb 69 61 6c 69 7a 65 20 61 20 6e 65 77 6c 79 20 63  ialize a newly c
170bc 72 65 61 74 65 64 20 69 6e 64 65 78 20 6f 72 20  reated index or 
170bd 74 6f 20 72 65 63 6f 6d 70 75 74 65 20 74 68 65  to recompute the
170be 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61  .** content of a
170bf 6e 20 69 6e 64 65 78 20 69 6e 20 72 65 73 70 6f  n index in respo
170c0 6e 73 65 20 74 6f 20 61 20 52 45 49 4e 44 45 58  nse to a REINDEX
170c1 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20   command..**.** 
170c2 69 66 20 6d 65 6d 52 6f 6f 74 50 61 67 65 20 69  if memRootPage i
170c3 73 20 6e 6f 74 20 6e 65 67 61 74 69 76 65 2c 20  s not negative, 
170c4 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  it means that th
170c5 65 20 69 6e 64 65 78 20 69 73 20 6e 65 77 6c 79  e index is newly
170c6 0a 2a 2a 20 63 72 65 61 74 65 64 2e 20 20 54 68  .** created.  Th
170c7 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 73 70  e memory cell sp
170c8 65 63 69 66 69 65 64 20 62 79 20 6d 65 6d 52 6f  ecified by memRo
170c9 6f 74 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 20  otPage contains 
170ca 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65  the.** root page
170cb 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 69   number of the i
170cc 6e 64 65 78 2e 20 20 49 66 20 6d 65 6d 52 6f 6f  ndex.  If memRoo
170cd 74 50 61 67 65 20 69 73 20 6e 65 67 61 74 69 76  tPage is negativ
170ce 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 69  e, then.** the i
170cf 6e 64 65 78 20 61 6c 72 65 61 64 79 20 65 78 69  ndex already exi
170d0 73 74 73 20 61 6e 64 20 6d 75 73 74 20 62 65 20  sts and must be 
170d1 63 6c 65 61 72 65 64 20 62 65 66 6f 72 65 20 62  cleared before b
170d2 65 69 6e 67 20 72 65 66 69 6c 6c 65 64 20 61 6e  eing refilled an
170d3 64 0a 2a 2a 20 74 68 65 20 72 6f 6f 74 20 70 61  d.** the root pa
170d4 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  ge number of the
170d5 20 69 6e 64 65 78 20 69 73 20 74 61 6b 65 6e 20   index is taken 
170d6 66 72 6f 6d 20 70 49 6e 64 65 78 2d 3e 74 6e 75  from pIndex->tnu
170d7 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  m..*/.static voi
170d8 64 20 73 71 6c 69 74 65 33 52 65 66 69 6c 6c 49  d sqlite3RefillI
170d9 6e 64 65 78 28 50 61 72 73 65 20 2a 70 50 61 72  ndex(Parse *pPar
170da 73 65 2c 20 49 6e 64 65 78 20 2a 70 49 6e 64 65  se, Index *pInde
170db 78 2c 20 69 6e 74 20 6d 65 6d 52 6f 6f 74 50 61  x, int memRootPa
170dc 67 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54  ge){.  Table *pT
170dd 61 62 20 3d 20 70 49 6e 64 65 78 2d 3e 70 54 61  ab = pIndex->pTa
170de 62 6c 65 3b 20 20 2f 2a 20 54 68 65 20 74 61 62  ble;  /* The tab
170df 6c 65 20 74 68 61 74 20 69 73 20 69 6e 64 65 78  le that is index
170e0 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62  ed */.  int iTab
170e1 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 3b   = pParse->nTab;
170e2 20 20 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20         /* Btree 
170e3 63 75 72 73 6f 72 20 75 73 65 64 20 66 6f 72 20  cursor used for 
170e4 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20 69 49  pTab */.  int iI
170e5 64 78 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  dx = pParse->nTa
170e6 62 2b 31 3b 20 20 20 20 20 2f 2a 20 42 74 72 65  b+1;     /* Btre
170e7 65 20 63 75 72 73 6f 72 20 75 73 65 64 20 66 6f  e cursor used fo
170e8 72 20 70 49 6e 64 65 78 20 2a 2f 0a 20 20 69 6e  r pIndex */.  in
170e9 74 20 61 64 64 72 31 3b 20 20 20 20 20 20 20 20  t addr1;        
170ea 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
170eb 41 64 64 72 65 73 73 20 6f 66 20 74 6f 70 20 6f  Address of top o
170ec 66 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20  f loop */.  int 
170ed 74 6e 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20  tnum;           
170ee 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f             /* Ro
170ef 6f 74 20 70 61 67 65 20 6f 66 20 69 6e 64 65 78  ot page of index
170f0 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20   */.  Vdbe *v;  
170f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
170f2 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65       /* Generate
170f3 20 63 6f 64 65 20 69 6e 74 6f 20 74 68 69 73 20   code into this 
170f4 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20  virtual machine 
170f5 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b  */.  KeyInfo *pK
170f6 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ey;             
170f7 20 20 20 20 2f 2a 20 4b 65 79 49 6e 66 6f 20 66      /* KeyInfo f
170f8 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e  or index */.  in
170f9 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53  t iDb = sqlite3S
170fa 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61  chemaToIndex(pPa
170fb 72 73 65 2d 3e 64 62 2c 20 70 49 6e 64 65 78 2d  rse->db, pIndex-
170fc 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 23 69 66 6e  >pSchema);..#ifn
170fd 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
170fe 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20  AUTHORIZATION.  
170ff 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43  if( sqlite3AuthC
17100 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c  heck(pParse, SQL
17101 49 54 45 5f 52 45 49 4e 44 45 58 2c 20 70 49 6e  ITE_REINDEX, pIn
17102 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 0a 20  dex->zName, 0,. 
17103 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64 62 2d       pParse->db-
17104 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 20  >aDb[iDb].zName 
17105 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b  ) ){.    return;
17106 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
17107 2a 20 52 65 71 75 69 72 65 20 61 20 77 72 69 74  * Require a writ
17108 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61  e-lock on the ta
17109 62 6c 65 20 74 6f 20 70 65 72 66 6f 72 6d 20 74  ble to perform t
1710a 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f  his operation */
1710b 0a 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c  .  sqlite3TableL
1710c 6f 63 6b 28 70 50 61 72 73 65 2c 20 69 44 62 2c  ock(pParse, iDb,
1710d 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 31 2c 20   pTab->tnum, 1, 
1710e 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20  pTab->zName);.. 
1710f 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   v = sqlite3GetV
17110 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69  dbe(pParse);.  i
17111 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( v==0 ) return
17112 3b 0a 20 20 69 66 28 20 6d 65 6d 52 6f 6f 74 50  ;.  if( memRootP
17113 61 67 65 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71  age>=0 ){.    sq
17114 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
17115 2c 20 4f 50 5f 4d 65 6d 4c 6f 61 64 2c 20 6d 65  , OP_MemLoad, me
17116 6d 52 6f 6f 74 50 61 67 65 2c 20 30 29 3b 0a 20  mRootPage, 0);. 
17117 20 20 20 74 6e 75 6d 20 3d 20 30 3b 0a 20 20 7d     tnum = 0;.  }
17118 65 6c 73 65 7b 0a 20 20 20 20 74 6e 75 6d 20 3d  else{.    tnum =
17119 20 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 3b 0a 20   pIndex->tnum;. 
1711a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1711b 64 4f 70 28 76 2c 20 4f 50 5f 43 6c 65 61 72 2c  dOp(v, OP_Clear,
1711c 20 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20 7d   tnum, iDb);.  }
1711d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
1711e 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  dOp(v, OP_Intege
1711f 72 2c 20 69 44 62 2c 20 30 29 3b 0a 20 20 70 4b  r, iDb, 0);.  pK
17120 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65  ey = sqlite3Inde
17121 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c  xKeyinfo(pParse,
17122 20 70 49 6e 64 65 78 29 3b 0a 20 20 73 71 6c 69   pIndex);.  sqli
17123 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20 4f 50  te3VdbeOp3(v, OP
17124 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69 49 64 78  _OpenWrite, iIdx
17125 2c 20 74 6e 75 6d 2c 20 28 63 68 61 72 20 2a 29  , tnum, (char *)
17126 70 4b 65 79 2c 20 50 33 5f 4b 45 59 49 4e 46 4f  pKey, P3_KEYINFO
17127 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 73 71 6c  _HANDOFF);.  sql
17128 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50  ite3OpenTable(pP
17129 61 72 73 65 2c 20 69 54 61 62 2c 20 69 44 62 2c  arse, iTab, iDb,
1712a 20 70 54 61 62 2c 20 4f 50 5f 4f 70 65 6e 52 65   pTab, OP_OpenRe
1712b 61 64 29 3b 0a 20 20 61 64 64 72 31 20 3d 20 73  ad);.  addr1 = s
1712c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1712d 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69 54  v, OP_Rewind, iT
1712e 61 62 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65  ab, 0);.  sqlite
1712f 33 47 65 6e 65 72 61 74 65 49 6e 64 65 78 4b 65  3GenerateIndexKe
17130 79 28 76 2c 20 70 49 6e 64 65 78 2c 20 69 54 61  y(v, pIndex, iTa
17131 62 29 3b 0a 20 20 69 66 28 20 70 49 6e 64 65 78  b);.  if( pIndex
17132 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f  ->onError!=OE_No
17133 6e 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 63 75  ne ){.    int cu
17134 72 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56  raddr = sqlite3V
17135 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
17136 29 3b 0a 20 20 20 20 69 6e 74 20 61 64 64 72 32  );.    int addr2
17137 20 3d 20 63 75 72 61 64 64 72 2b 34 3b 0a 20 20   = curaddr+4;.  
17138 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
17139 6e 67 65 50 32 28 76 2c 20 63 75 72 61 64 64 72  ngeP2(v, curaddr
1713a 2d 31 2c 20 61 64 64 72 32 29 3b 0a 20 20 20 20  -1, addr2);.    
1713b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1713c 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69 54  (v, OP_Rowid, iT
1713d 61 62 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69  ab, 0);.    sqli
1713e 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1713f 4f 50 5f 41 64 64 49 6d 6d 2c 20 31 2c 20 30 29  OP_AddImm, 1, 0)
17140 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
17141 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 73 55  eAddOp(v, OP_IsU
17142 6e 69 71 75 65 2c 20 69 49 64 78 2c 20 61 64 64  nique, iIdx, add
17143 72 32 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  r2);.    sqlite3
17144 56 64 62 65 4f 70 33 28 76 2c 20 4f 50 5f 48 61  VdbeOp3(v, OP_Ha
17145 6c 74 2c 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54  lt, SQLITE_CONST
17146 52 41 49 4e 54 2c 20 4f 45 5f 41 62 6f 72 74 2c  RAINT, OE_Abort,
17147 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
17148 20 20 20 20 20 22 69 6e 64 65 78 65 64 20 63 6f       "indexed co
17149 6c 75 6d 6e 73 20 61 72 65 20 6e 6f 74 20 75 6e  lumns are not un
1714a 69 71 75 65 22 2c 20 50 33 5f 53 54 41 54 49 43  ique", P3_STATIC
1714b 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  );.    assert( s
1714c 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69 6c  qlite3MallocFail
1714d 65 64 28 29 20 7c 7c 20 61 64 64 72 32 3d 3d 73  ed() || addr2==s
1714e 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
1714f 74 41 64 64 72 28 76 29 20 29 3b 0a 20 20 7d 0a  tAddr(v) );.  }.
17150 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
17151 4f 70 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65  Op(v, OP_IdxInse
17152 72 74 2c 20 69 49 64 78 2c 20 30 29 3b 0a 20 20  rt, iIdx, 0);.  
17153 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
17154 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 69 54 61  (v, OP_Next, iTa
17155 62 2c 20 61 64 64 72 31 2b 31 29 3b 0a 20 20 73  b, addr1+1);.  s
17156 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
17157 72 65 28 76 2c 20 61 64 64 72 31 29 3b 0a 20 20  re(v, addr1);.  
17158 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
17159 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 54  (v, OP_Close, iT
1715a 61 62 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65  ab, 0);.  sqlite
1715b 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1715c 5f 43 6c 6f 73 65 2c 20 69 49 64 78 2c 20 30 29  _Close, iIdx, 0)
1715d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74  ;.}../*.** Creat
1715e 65 20 61 20 6e 65 77 20 69 6e 64 65 78 20 66 6f  e a new index fo
1715f 72 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 2e 20  r an SQL table. 
17160 20 70 4e 61 6d 65 31 2e 70 4e 61 6d 65 32 20 69   pName1.pName2 i
17161 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  s the name of th
17162 65 20 69 6e 64 65 78 20 0a 2a 2a 20 61 6e 64 20  e index .** and 
17163 70 54 62 6c 4c 69 73 74 20 69 73 20 74 68 65 20  pTblList is the 
17164 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c  name of the tabl
17165 65 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 20  e that is to be 
17166 69 6e 64 65 78 65 64 2e 20 20 42 6f 74 68 20 77  indexed.  Both w
17167 69 6c 6c 20 0a 2a 2a 20 62 65 20 4e 55 4c 4c 20  ill .** be NULL 
17168 66 6f 72 20 61 20 70 72 69 6d 61 72 79 20 6b 65  for a primary ke
17169 79 20 6f 72 20 61 6e 20 69 6e 64 65 78 20 74 68  y or an index th
1716a 61 74 20 69 73 20 63 72 65 61 74 65 64 20 74 6f  at is created to
1716b 20 73 61 74 69 73 66 79 20 61 0a 2a 2a 20 55 4e   satisfy a.** UN
1716c 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e 74 2e  IQUE constraint.
1716d 20 20 49 66 20 70 54 61 62 6c 65 20 61 6e 64 20    If pTable and 
1716e 70 49 6e 64 65 78 20 61 72 65 20 4e 55 4c 4c 2c  pIndex are NULL,
1716f 20 75 73 65 20 70 50 61 72 73 65 2d 3e 70 4e 65   use pParse->pNe
17170 77 54 61 62 6c 65 0a 2a 2a 20 61 73 20 74 68 65  wTable.** as the
17171 20 74 61 62 6c 65 20 74 6f 20 62 65 20 69 6e 64   table to be ind
17172 65 78 65 64 2e 20 20 70 50 61 72 73 65 2d 3e 70  exed.  pParse->p
17173 4e 65 77 54 61 62 6c 65 20 69 73 20 61 20 74 61  NewTable is a ta
17174 62 6c 65 20 74 68 61 74 20 69 73 0a 2a 2a 20 63  ble that is.** c
17175 75 72 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 63  urrently being c
17176 6f 6e 73 74 72 75 63 74 65 64 20 62 79 20 61 20  onstructed by a 
17177 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61  CREATE TABLE sta
17178 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 70 4c  tement..**.** pL
17179 69 73 74 20 69 73 20 61 20 6c 69 73 74 20 6f 66  ist is a list of
1717a 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 62 65 20 69   columns to be i
1717b 6e 64 65 78 65 64 2e 20 20 70 4c 69 73 74 20 77  ndexed.  pList w
1717c 69 6c 6c 20 62 65 20 4e 55 4c 4c 20 69 66 20 74  ill be NULL if t
1717d 68 69 73 0a 2a 2a 20 69 73 20 61 20 70 72 69 6d  his.** is a prim
1717e 61 72 79 20 6b 65 79 20 6f 72 20 75 6e 69 71 75  ary key or uniqu
1717f 65 2d 63 6f 6e 73 74 72 61 69 6e 74 20 6f 6e 20  e-constraint on 
17180 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
17181 63 6f 6c 75 6d 6e 20 61 64 64 65 64 0a 2a 2a 20  column added.** 
17182 74 6f 20 74 68 65 20 74 61 62 6c 65 20 63 75 72  to the table cur
17183 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e  rently under con
17184 73 74 72 75 63 74 69 6f 6e 2e 20 20 0a 2a 2f 0a  struction.  .*/.
17185 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
17186 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65 61 74  oid sqlite3Creat
17187 65 49 6e 64 65 78 28 0a 20 20 50 61 72 73 65 20  eIndex(.  Parse 
17188 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20  *pParse,     /* 
17189 41 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  All information 
1718a 61 62 6f 75 74 20 74 68 69 73 20 70 61 72 73 65  about this parse
1718b 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61   */.  Token *pNa
1718c 6d 65 31 2c 20 20 20 20 20 2f 2a 20 46 69 72 73  me1,     /* Firs
1718d 74 20 70 61 72 74 20 6f 66 20 69 6e 64 65 78 20  t part of index 
1718e 6e 61 6d 65 2e 20 4d 61 79 20 62 65 20 4e 55 4c  name. May be NUL
1718f 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e  L */.  Token *pN
17190 61 6d 65 32 2c 20 20 20 20 20 2f 2a 20 53 65 63  ame2,     /* Sec
17191 6f 6e 64 20 70 61 72 74 20 6f 66 20 69 6e 64 65  ond part of inde
17192 78 20 6e 61 6d 65 2e 20 4d 61 79 20 62 65 20 4e  x name. May be N
17193 55 4c 4c 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74  ULL */.  SrcList
17194 20 2a 70 54 62 6c 4e 61 6d 65 2c 20 2f 2a 20 54   *pTblName, /* T
17195 61 62 6c 65 20 74 6f 20 69 6e 64 65 78 2e 20 55  able to index. U
17196 73 65 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  se pParse->pNewT
17197 61 62 6c 65 20 69 66 20 30 20 2a 2f 0a 20 20 45  able if 0 */.  E
17198 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20  xprList *pList, 
17199 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f 66 20 63    /* A list of c
1719a 6f 6c 75 6d 6e 73 20 74 6f 20 62 65 20 69 6e 64  olumns to be ind
1719b 65 78 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e  exed */.  int on
1719c 45 72 72 6f 72 2c 20 20 20 20 20 20 20 2f 2a 20  Error,       /* 
1719d 4f 45 5f 41 62 6f 72 74 2c 20 4f 45 5f 49 67 6e  OE_Abort, OE_Ign
1719e 6f 72 65 2c 20 4f 45 5f 52 65 70 6c 61 63 65 2c  ore, OE_Replace,
1719f 20 6f 72 20 4f 45 5f 4e 6f 6e 65 20 2a 2f 0a 20   or OE_None */. 
171a0 20 54 6f 6b 65 6e 20 2a 70 53 74 61 72 74 2c 20   Token *pStart, 
171a1 20 20 20 20 2f 2a 20 54 68 65 20 43 52 45 41 54      /* The CREAT
171a2 45 20 74 6f 6b 65 6e 20 74 68 61 74 20 62 65 67  E token that beg
171a3 69 6e 73 20 74 68 69 73 20 73 74 61 74 65 6d 65  ins this stateme
171a4 6e 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  nt */.  Token *p
171a5 45 6e 64 2c 20 20 20 20 20 20 20 2f 2a 20 54 68  End,       /* Th
171a6 65 20 22 29 22 20 74 68 61 74 20 63 6c 6f 73 65  e ")" that close
171a7 73 20 74 68 65 20 43 52 45 41 54 45 20 49 4e 44  s the CREATE IND
171a8 45 58 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  EX statement */.
171a9 20 20 69 6e 74 20 73 6f 72 74 4f 72 64 65 72 2c    int sortOrder,
171aa 20 20 20 20 20 2f 2a 20 53 6f 72 74 20 6f 72 64       /* Sort ord
171ab 65 72 20 6f 66 20 70 72 69 6d 61 72 79 20 6b 65  er of primary ke
171ac 79 20 77 68 65 6e 20 70 4c 69 73 74 3d 3d 4e 55  y when pList==NU
171ad 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 69 66 4e 6f  LL */.  int ifNo
171ae 74 45 78 69 73 74 20 20 20 20 20 2f 2a 20 4f 6d  tExist     /* Om
171af 69 74 20 65 72 72 6f 72 20 69 66 20 69 6e 64 65  it error if inde
171b0 78 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73  x already exists
171b1 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a   */.){.  Table *
171b2 70 54 61 62 20 3d 20 30 3b 20 20 20 20 20 2f 2a  pTab = 0;     /*
171b3 20 54 61 62 6c 65 20 74 6f 20 62 65 20 69 6e 64   Table to be ind
171b4 65 78 65 64 20 2a 2f 0a 20 20 49 6e 64 65 78 20  exed */.  Index 
171b5 2a 70 49 6e 64 65 78 20 3d 20 30 3b 20 20 20 2f  *pIndex = 0;   /
171b6 2a 20 54 68 65 20 69 6e 64 65 78 20 74 6f 20 62  * The index to b
171b7 65 20 63 72 65 61 74 65 64 20 2a 2f 0a 20 20 63  e created */.  c
171b8 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 20  har *zName = 0; 
171b9 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
171ba 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e  he index */.  in
171bb 74 20 6e 4e 61 6d 65 3b 20 20 20 20 20 20 20 20  t nName;        
171bc 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
171bd 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 7a 4e  characters in zN
171be 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20  ame */.  int i, 
171bf 6a 3b 0a 20 20 54 6f 6b 65 6e 20 6e 75 6c 6c 49  j;.  Token nullI
171c0 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6b  d;        /* Fak
171c1 65 20 74 6f 6b 65 6e 20 66 6f 72 20 61 6e 20 65  e token for an e
171c2 6d 70 74 79 20 49 44 20 6c 69 73 74 20 2a 2f 0a  mpty ID list */.
171c3 20 20 44 62 46 69 78 65 72 20 73 46 69 78 3b 20    DbFixer sFix; 
171c4 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 61 73         /* For as
171c5 73 69 67 6e 69 6e 67 20 64 61 74 61 62 61 73 65  signing database
171c6 20 6e 61 6d 65 73 20 74 6f 20 70 54 61 62 6c 65   names to pTable
171c7 20 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 4f 72   */.  int sortOr
171c8 64 65 72 4d 61 73 6b 3b 20 20 20 2f 2a 20 31 20  derMask;   /* 1 
171c9 74 6f 20 68 6f 6e 6f 72 20 44 45 53 43 20 69 6e  to honor DESC in
171ca 20 69 6e 64 65 78 2e 20 20 30 20 74 6f 20 69 67   index.  0 to ig
171cb 6e 6f 72 65 2e 20 2a 2f 0a 20 20 73 71 6c 69 74  nore. */.  sqlit
171cc 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
171cd 3e 64 62 3b 0a 20 20 44 62 20 2a 70 44 62 3b 20  >db;.  Db *pDb; 
171ce 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
171cf 68 65 20 73 70 65 63 69 66 69 63 20 74 61 62 6c  he specific tabl
171d0 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  e containing the
171d1 20 69 6e 64 65 78 65 64 20 64 61 74 61 62 61 73   indexed databas
171d2 65 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20  e */.  int iDb; 
171d3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
171d4 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61  ndex of the data
171d5 62 61 73 65 20 74 68 61 74 20 69 73 20 62 65 69  base that is bei
171d6 6e 67 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20  ng written */.  
171d7 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 3d 20 30  Token *pName = 0
171d8 3b 20 20 20 20 2f 2a 20 55 6e 71 75 61 6c 69 66  ;    /* Unqualif
171d9 69 65 64 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  ied name of the 
171da 69 6e 64 65 78 20 74 6f 20 63 72 65 61 74 65 20  index to create 
171db 2a 2f 0a 20 20 73 74 72 75 63 74 20 45 78 70 72  */.  struct Expr
171dc 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4c 69 73 74  List_item *pList
171dd 49 74 65 6d 3b 20 2f 2a 20 46 6f 72 20 6c 6f 6f  Item; /* For loo
171de 70 69 6e 67 20 6f 76 65 72 20 70 4c 69 73 74 20  ping over pList 
171df 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a 20  */.  int nCol;. 
171e0 20 69 6e 74 20 6e 45 78 74 72 61 20 3d 20 30 3b   int nExtra = 0;
171e1 0a 20 20 63 68 61 72 20 2a 7a 45 78 74 72 61 3b  .  char *zExtra;
171e2 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ..  if( pParse->
171e3 6e 45 72 72 20 7c 7c 20 73 71 6c 69 74 65 33 4d  nErr || sqlite3M
171e4 61 6c 6c 6f 63 46 61 69 6c 65 64 28 29 20 7c 7c  allocFailed() ||
171e5 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42   IN_DECLARE_VTAB
171e6 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69   ){.    goto exi
171e7 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a  t_create_index;.
171e8 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46    }..  /*.  ** F
171e9 69 6e 64 20 74 68 65 20 74 61 62 6c 65 20 74 68  ind the table th
171ea 61 74 20 69 73 20 74 6f 20 62 65 20 69 6e 64 65  at is to be inde
171eb 78 65 64 2e 20 20 52 65 74 75 72 6e 20 65 61 72  xed.  Return ear
171ec 6c 79 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e  ly if not found.
171ed 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 62 6c  .  */.  if( pTbl
171ee 4e 61 6d 65 21 3d 30 20 29 7b 0a 0a 20 20 20 20  Name!=0 ){..    
171ef 2f 2a 20 55 73 65 20 74 68 65 20 74 77 6f 2d 70  /* Use the two-p
171f0 61 72 74 20 69 6e 64 65 78 20 6e 61 6d 65 20 74  art index name t
171f1 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20  o determine the 
171f2 64 61 74 61 62 61 73 65 20 0a 20 20 20 20 2a 2a  database .    **
171f3 20 74 6f 20 73 65 61 72 63 68 20 66 6f 72 20 74   to search for t
171f4 68 65 20 74 61 62 6c 65 2e 20 27 46 69 78 27 20  he table. 'Fix' 
171f5 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 74  the table name t
171f6 6f 20 74 68 69 73 20 64 62 0a 20 20 20 20 2a 2a  o this db.    **
171f7 20 62 65 66 6f 72 65 20 6c 6f 6f 6b 69 6e 67 20   before looking 
171f8 75 70 20 74 68 65 20 74 61 62 6c 65 2e 0a 20 20  up the table..  
171f9 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28    */.    assert(
171fa 20 70 4e 61 6d 65 31 20 26 26 20 70 4e 61 6d 65   pName1 && pName
171fb 32 20 29 3b 0a 20 20 20 20 69 44 62 20 3d 20 73  2 );.    iDb = s
171fc 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d  qlite3TwoPartNam
171fd 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 31  e(pParse, pName1
171fe 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4e 61 6d 65  , pName2, &pName
171ff 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3c 30  );.    if( iDb<0
17200 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65   ) goto exit_cre
17201 61 74 65 5f 69 6e 64 65 78 3b 0a 0a 23 69 66 6e  ate_index;..#ifn
17202 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
17203 54 45 4d 50 44 42 0a 20 20 20 20 2f 2a 20 49 66  TEMPDB.    /* If
17204 20 74 68 65 20 69 6e 64 65 78 20 6e 61 6d 65 20   the index name 
17205 77 61 73 20 75 6e 71 75 61 6c 69 66 69 65 64 2c  was unqualified,
17206 20 63 68 65 63 6b 20 69 66 20 74 68 65 20 74 68   check if the th
17207 65 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20 69  e table.    ** i
17208 73 20 61 20 74 65 6d 70 20 74 61 62 6c 65 2e 20  s a temp table. 
17209 49 66 20 73 6f 2c 20 73 65 74 20 74 68 65 20 64  If so, set the d
1720a 61 74 61 62 61 73 65 20 74 6f 20 31 2e 0a 20 20  atabase to 1..  
1720b 20 20 2a 2f 0a 20 20 20 20 70 54 61 62 20 3d 20    */.    pTab = 
1720c 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 4c 6f  sqlite3SrcListLo
1720d 6f 6b 75 70 28 70 50 61 72 73 65 2c 20 70 54 62  okup(pParse, pTb
1720e 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20  lName);.    if( 
1720f 70 4e 61 6d 65 32 20 26 26 20 70 4e 61 6d 65 32  pName2 && pName2
17210 2d 3e 6e 3d 3d 30 20 26 26 20 70 54 61 62 20 26  ->n==0 && pTab &
17211 26 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3d  & pTab->pSchema=
17212 3d 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68  =db->aDb[1].pSch
17213 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 69 44 62  ema ){.      iDb
17214 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 23 65 6e 64   = 1;.    }.#end
17215 69 66 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69  if..    if( sqli
17216 74 65 33 46 69 78 49 6e 69 74 28 26 73 46 69 78  te3FixInit(&sFix
17217 2c 20 70 50 61 72 73 65 2c 20 69 44 62 2c 20 22  , pParse, iDb, "
17218 69 6e 64 65 78 22 2c 20 70 4e 61 6d 65 29 20 26  index", pName) &
17219 26 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  &.        sqlite
1721a 33 46 69 78 53 72 63 4c 69 73 74 28 26 73 46 69  3FixSrcList(&sFi
1721b 78 2c 20 70 54 62 6c 4e 61 6d 65 29 0a 20 20 20  x, pTblName).   
1721c 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 42 65 63   ){.      /* Bec
1721d 61 75 73 65 20 74 68 65 20 70 61 72 73 65 72 20  ause the parser 
1721e 63 6f 6e 73 74 72 75 63 74 73 20 70 54 62 6c 4e  constructs pTblN
1721f 61 6d 65 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c  ame from a singl
17220 65 20 69 64 65 6e 74 69 66 69 65 72 2c 0a 20 20  e identifier,.  
17221 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 46 69      ** sqlite3Fi
17222 78 53 72 63 4c 69 73 74 20 63 61 6e 20 6e 65 76  xSrcList can nev
17223 65 72 20 66 61 69 6c 2e 20 2a 2f 0a 20 20 20 20  er fail. */.    
17224 20 20 61 73 73 65 72 74 28 30 29 3b 0a 20 20 20    assert(0);.   
17225 20 7d 0a 20 20 20 20 70 54 61 62 20 3d 20 73 71   }.    pTab = sq
17226 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65  lite3LocateTable
17227 28 70 50 61 72 73 65 2c 20 70 54 62 6c 4e 61 6d  (pParse, pTblNam
17228 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 0a  e->a[0].zName, .
17229 20 20 20 20 20 20 20 20 70 54 62 6c 4e 61 6d 65          pTblName
1722a 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61 73 65  ->a[0].zDatabase
1722b 29 3b 0a 20 20 20 20 69 66 28 20 21 70 54 61 62  );.    if( !pTab
1722c 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65   ) goto exit_cre
1722d 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 61  ate_index;.    a
1722e 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62 5b 69  ssert( db->aDb[i
1722f 44 62 5d 2e 70 53 63 68 65 6d 61 3d 3d 70 54 61  Db].pSchema==pTa
17230 62 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0a 20 20  b->pSchema );.  
17231 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72  }else{.    asser
17232 74 28 20 70 4e 61 6d 65 3d 3d 30 20 29 3b 0a 20  t( pName==0 );. 
17233 20 20 20 70 54 61 62 20 3d 20 70 50 61 72 73 65     pTab = pParse
17234 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 20  ->pNewTable;.   
17235 20 69 66 28 20 21 70 54 61 62 20 29 20 67 6f 74   if( !pTab ) got
17236 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
17237 64 65 78 3b 0a 20 20 20 20 69 44 62 20 3d 20 73  dex;.    iDb = s
17238 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
17239 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53  dex(db, pTab->pS
1723a 63 68 65 6d 61 29 3b 0a 20 20 7d 0a 20 20 70 44  chema);.  }.  pD
1723b 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62  b = &db->aDb[iDb
1723c 5d 3b 0a 0a 20 20 69 66 28 20 70 54 61 62 3d 3d  ];..  if( pTab==
1723d 30 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72  0 || pParse->nEr
1723e 72 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 72  r ) goto exit_cr
1723f 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 69 66  eate_index;.  if
17240 28 20 70 54 61 62 2d 3e 72 65 61 64 4f 6e 6c 79  ( pTab->readOnly
17241 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
17242 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
17243 22 74 61 62 6c 65 20 25 73 20 6d 61 79 20 6e 6f  "table %s may no
17244 74 20 62 65 20 69 6e 64 65 78 65 64 22 2c 20 70  t be indexed", p
17245 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  Tab->zName);.   
17246 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74   goto exit_creat
17247 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 23 69 66  e_index;.  }.#if
17248 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
17249 5f 56 49 45 57 0a 20 20 69 66 28 20 70 54 61 62  _VIEW.  if( pTab
1724a 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20  ->pSelect ){.   
1724b 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
1724c 28 70 50 61 72 73 65 2c 20 22 76 69 65 77 73 20  (pParse, "views 
1724d 6d 61 79 20 6e 6f 74 20 62 65 20 69 6e 64 65 78  may not be index
1724e 65 64 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65  ed");.    goto e
1724f 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78  xit_create_index
17250 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66  ;.  }.#endif.#if
17251 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
17252 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
17253 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54  if( IsVirtual(pT
17254 61 62 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ab) ){.    sqlit
17255 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
17256 65 2c 20 22 76 69 72 74 75 61 6c 20 74 61 62 6c  e, "virtual tabl
17257 65 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 69 6e  es may not be in
17258 64 65 78 65 64 22 29 3b 0a 20 20 20 20 67 6f 74  dexed");.    got
17259 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
1725a 64 65 78 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  dex;.  }.#endif.
1725b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46 69 6e 64 20  .  /*.  ** Find 
1725c 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
1725d 69 6e 64 65 78 2e 20 20 4d 61 6b 65 20 73 75 72  index.  Make sur
1725e 65 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20 61  e there is not a
1725f 6c 72 65 61 64 79 20 61 6e 6f 74 68 65 72 0a 20  lready another. 
17260 20 2a 2a 20 69 6e 64 65 78 20 6f 72 20 74 61 62   ** index or tab
17261 6c 65 20 77 69 74 68 20 74 68 65 20 73 61 6d 65  le with the same
17262 20 6e 61 6d 65 2e 20 20 0a 20 20 2a 2a 0a 20 20   name.  .  **.  
17263 2a 2a 20 45 78 63 65 70 74 69 6f 6e 3a 20 20 49  ** Exception:  I
17264 66 20 77 65 20 61 72 65 20 72 65 61 64 69 6e 67  f we are reading
17265 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 70 65   the names of pe
17266 72 6d 61 6e 65 6e 74 20 69 6e 64 69 63 65 73 20  rmanent indices 
17267 66 72 6f 6d 20 74 68 65 0a 20 20 2a 2a 20 73 71  from the.  ** sq
17268 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c  lite_master tabl
17269 65 20 28 62 65 63 61 75 73 65 20 73 6f 6d 65 20  e (because some 
1726a 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 63 68  other process ch
1726b 61 6e 67 65 64 20 74 68 65 20 73 63 68 65 6d 61  anged the schema
1726c 29 20 61 6e 64 0a 20 20 2a 2a 20 6f 6e 65 20 6f  ) and.  ** one o
1726d 66 20 74 68 65 20 69 6e 64 65 78 20 6e 61 6d 65  f the index name
1726e 73 20 63 6f 6c 6c 69 64 65 73 20 77 69 74 68 20  s collides with 
1726f 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 74 65  the name of a te
17270 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 6f 72  mporary table or
17271 0a 20 20 2a 2a 20 69 6e 64 65 78 2c 20 74 68 65  .  ** index, the
17272 6e 20 77 65 20 77 69 6c 6c 20 63 6f 6e 74 69 6e  n we will contin
17273 75 65 20 74 6f 20 70 72 6f 63 65 73 73 20 74 68  ue to process th
17274 69 73 20 69 6e 64 65 78 2e 0a 20 20 2a 2a 0a 20  is index..  **. 
17275 20 2a 2a 20 49 66 20 70 4e 61 6d 65 3d 3d 30 20   ** If pName==0 
17276 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65  it means that we
17277 20 61 72 65 0a 20 20 2a 2a 20 64 65 61 6c 69 6e   are.  ** dealin
17278 67 20 77 69 74 68 20 61 20 70 72 69 6d 61 72 79  g with a primary
17279 20 6b 65 79 20 6f 72 20 55 4e 49 51 55 45 20 63   key or UNIQUE c
1727a 6f 6e 73 74 72 61 69 6e 74 2e 20 20 57 65 20 68  onstraint.  We h
1727b 61 76 65 20 74 6f 20 69 6e 76 65 6e 74 20 6f 75  ave to invent ou
1727c 72 0a 20 20 2a 2a 20 6f 77 6e 20 6e 61 6d 65 2e  r.  ** own name.
1727d 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4e 61 6d  .  */.  if( pNam
1727e 65 20 29 7b 0a 20 20 20 20 7a 4e 61 6d 65 20 3d  e ){.    zName =
1727f 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d   sqlite3NameFrom
17280 54 6f 6b 65 6e 28 70 4e 61 6d 65 29 3b 0a 20 20  Token(pName);.  
17281 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21    if( SQLITE_OK!
17282 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65  =sqlite3ReadSche
17283 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74  ma(pParse) ) got
17284 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
17285 64 65 78 3b 0a 20 20 20 20 69 66 28 20 7a 4e 61  dex;.    if( zNa
17286 6d 65 3d 3d 30 20 29 20 67 6f 74 6f 20 65 78 69  me==0 ) goto exi
17287 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a  t_create_index;.
17288 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f      if( SQLITE_O
17289 4b 21 3d 73 71 6c 69 74 65 33 43 68 65 63 6b 4f  K!=sqlite3CheckO
1728a 62 6a 65 63 74 4e 61 6d 65 28 70 50 61 72 73 65  bjectName(pParse
1728b 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20  , zName) ){.    
1728c 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61    goto exit_crea
1728d 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a  te_index;.    }.
1728e 20 20 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69      if( !db->ini
1728f 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 20 20  t.busy ){.      
17290 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73  if( SQLITE_OK!=s
17291 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61  qlite3ReadSchema
17292 28 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20  (pParse) ) goto 
17293 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65  exit_create_inde
17294 78 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  x;.      if( sql
17295 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62  ite3FindTable(db
17296 2c 20 7a 4e 61 6d 65 2c 20 30 29 21 3d 30 20 29  , zName, 0)!=0 )
17297 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
17298 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
17299 2c 20 22 74 68 65 72 65 20 69 73 20 61 6c 72 65  , "there is alre
1729a 61 64 79 20 61 20 74 61 62 6c 65 20 6e 61 6d 65  ady a table name
1729b 64 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20  d %s", zName);. 
1729c 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74         goto exit
1729d 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
1729e 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
1729f 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64   if( sqlite3Find
172a0 49 6e 64 65 78 28 64 62 2c 20 7a 4e 61 6d 65 2c  Index(db, zName,
172a1 20 70 44 62 2d 3e 7a 4e 61 6d 65 29 21 3d 30 20   pDb->zName)!=0 
172a2 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21 69 66  ){.      if( !if
172a3 4e 6f 74 45 78 69 73 74 20 29 7b 0a 20 20 20 20  NotExist ){.    
172a4 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
172a5 4d 73 67 28 70 50 61 72 73 65 2c 20 22 69 6e 64  Msg(pParse, "ind
172a6 65 78 20 25 73 20 61 6c 72 65 61 64 79 20 65 78  ex %s already ex
172a7 69 73 74 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20  ists", zName);. 
172a8 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74       }.      got
172a9 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
172aa 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  dex;.    }.  }el
172ab 73 65 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75  se{.    char zBu
172ac 66 5b 33 30 5d 3b 0a 20 20 20 20 69 6e 74 20 6e  f[30];.    int n
172ad 3b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 4c 6f  ;.    Index *pLo
172ae 6f 70 3b 0a 20 20 20 20 66 6f 72 28 70 4c 6f 6f  op;.    for(pLoo
172af 70 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 2c 20  p=pTab->pIndex, 
172b0 6e 3d 31 3b 20 70 4c 6f 6f 70 3b 20 70 4c 6f 6f  n=1; pLoop; pLoo
172b1 70 3d 70 4c 6f 6f 70 2d 3e 70 4e 65 78 74 2c 20  p=pLoop->pNext, 
172b2 6e 2b 2b 29 7b 7d 0a 20 20 20 20 73 71 6c 69 74  n++){}.    sqlit
172b3 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65  e3_snprintf(size
172b4 6f 66 28 7a 42 75 66 29 2c 7a 42 75 66 2c 22 5f  of(zBuf),zBuf,"_
172b5 25 64 22 2c 6e 29 3b 0a 20 20 20 20 7a 4e 61 6d  %d",n);.    zNam
172b6 65 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74  e = 0;.    sqlit
172b7 65 33 53 65 74 53 74 72 69 6e 67 28 26 7a 4e 61  e3SetString(&zNa
172b8 6d 65 2c 20 22 73 71 6c 69 74 65 5f 61 75 74 6f  me, "sqlite_auto
172b9 69 6e 64 65 78 5f 22 2c 20 70 54 61 62 2d 3e 7a  index_", pTab->z
172ba 4e 61 6d 65 2c 20 7a 42 75 66 2c 20 28 63 68 61  Name, zBuf, (cha
172bb 72 2a 29 30 29 3b 0a 20 20 20 20 69 66 28 20 7a  r*)0);.    if( z
172bc 4e 61 6d 65 3d 3d 30 20 29 20 67 6f 74 6f 20 65  Name==0 ) goto e
172bd 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78  xit_create_index
172be 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63  ;.  }..  /* Chec
172bf 6b 20 66 6f 72 20 61 75 74 68 6f 72 69 7a 61 74  k for authorizat
172c0 69 6f 6e 20 74 6f 20 63 72 65 61 74 65 20 61 6e  ion to create an
172c1 20 69 6e 64 65 78 2e 0a 20 20 2a 2f 0a 23 69 66   index..  */.#if
172c2 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
172c3 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20  _AUTHORIZATION. 
172c4 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61   {.    const cha
172c5 72 20 2a 7a 44 62 20 3d 20 70 44 62 2d 3e 7a 4e  r *zDb = pDb->zN
172c6 61 6d 65 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  ame;.    if( sql
172c7 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
172c8 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 49 4e 53  arse, SQLITE_INS
172c9 45 52 54 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c  ERT, SCHEMA_TABL
172ca 45 28 69 44 62 29 2c 20 30 2c 20 7a 44 62 29 20  E(iDb), 0, zDb) 
172cb 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78  ){.      goto ex
172cc 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b  it_create_index;
172cd 0a 20 20 20 20 7d 0a 20 20 20 20 69 20 3d 20 53  .    }.    i = S
172ce 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 49 4e 44  QLITE_CREATE_IND
172cf 45 58 3b 0a 20 20 20 20 69 66 28 20 21 4f 4d 49  EX;.    if( !OMI
172d0 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44 62 3d  T_TEMPDB && iDb=
172d1 3d 31 20 29 20 69 20 3d 20 53 51 4c 49 54 45 5f  =1 ) i = SQLITE_
172d2 43 52 45 41 54 45 5f 54 45 4d 50 5f 49 4e 44 45  CREATE_TEMP_INDE
172d3 58 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  X;.    if( sqlit
172d4 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
172d5 73 65 2c 20 69 2c 20 7a 4e 61 6d 65 2c 20 70 54  se, i, zName, pT
172d6 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 44 62 29 20  ab->zName, zDb) 
172d7 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78  ){.      goto ex
172d8 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b  it_create_index;
172d9 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
172da 66 0a 0a 20 20 2f 2a 20 49 66 20 70 4c 69 73 74  f..  /* If pList
172db 3d 3d 30 2c 20 69 74 20 6d 65 61 6e 73 20 74 68  ==0, it means th
172dc 69 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20 63  is routine was c
172dd 61 6c 6c 65 64 20 74 6f 20 6d 61 6b 65 20 61 20  alled to make a 
172de 70 72 69 6d 61 72 79 0a 20 20 2a 2a 20 6b 65 79  primary.  ** key
172df 20 6f 75 74 20 6f 66 20 74 68 65 20 6c 61 73 74   out of the last
172e0 20 63 6f 6c 75 6d 6e 20 61 64 64 65 64 20 74 6f   column added to
172e1 20 74 68 65 20 74 61 62 6c 65 20 75 6e 64 65 72   the table under
172e2 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 20   construction.. 
172e3 20 2a 2a 20 53 6f 20 63 72 65 61 74 65 20 61 20   ** So create a 
172e4 66 61 6b 65 20 6c 69 73 74 20 74 6f 20 73 69 6d  fake list to sim
172e5 75 6c 61 74 65 20 74 68 69 73 2e 0a 20 20 2a 2f  ulate this..  */
172e6 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20  .  if( pList==0 
172e7 29 7b 0a 20 20 20 20 6e 75 6c 6c 49 64 2e 7a 20  ){.    nullId.z 
172e8 3d 20 28 75 38 2a 29 70 54 61 62 2d 3e 61 43 6f  = (u8*)pTab->aCo
172e9 6c 5b 70 54 61 62 2d 3e 6e 43 6f 6c 2d 31 5d 2e  l[pTab->nCol-1].
172ea 7a 4e 61 6d 65 3b 0a 20 20 20 20 6e 75 6c 6c 49  zName;.    nullI
172eb 64 2e 6e 20 3d 20 73 74 72 6c 65 6e 28 28 63 68  d.n = strlen((ch
172ec 61 72 2a 29 6e 75 6c 6c 49 64 2e 7a 29 3b 0a 20  ar*)nullId.z);. 
172ed 20 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74     pList = sqlit
172ee 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
172ef 28 30 2c 20 30 2c 20 26 6e 75 6c 6c 49 64 29 3b  (0, 0, &nullId);
172f0 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 3d 3d  .    if( pList==
172f1 30 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 72  0 ) goto exit_cr
172f2 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20  eate_index;.    
172f3 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 73 6f 72 74  pList->a[0].sort
172f4 4f 72 64 65 72 20 3d 20 73 6f 72 74 4f 72 64 65  Order = sortOrde
172f5 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 67  r;.  }..  /* Fig
172f6 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79  ure out how many
172f7 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20   bytes of space 
172f8 61 72 65 20 72 65 71 75 69 72 65 64 20 74 6f 20  are required to 
172f9 73 74 6f 72 65 20 65 78 70 6c 69 63 69 74 6c 79  store explicitly
172fa 0a 20 20 2a 2a 20 73 70 65 63 69 66 69 65 64 20  .  ** specified 
172fb 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
172fc 63 65 20 6e 61 6d 65 73 2e 0a 20 20 2a 2f 0a 20  ce names..  */. 
172fd 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73   for(i=0; i<pLis
172fe 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a  t->nExpr; i++){.
172ff 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 20      Expr *pExpr 
17300 3d 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45  = pList->a[i].pE
17301 78 70 72 3b 0a 20 20 20 20 69 66 28 20 70 45 78  xpr;.    if( pEx
17302 70 72 20 29 7b 0a 20 20 20 20 20 20 6e 45 78 74  pr ){.      nExt
17303 72 61 20 2b 3d 20 28 31 20 2b 20 73 74 72 6c 65  ra += (1 + strle
17304 6e 28 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 2d 3e  n(pExpr->pColl->
17305 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 7d 0a 20  zName));.    }. 
17306 20 7d 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 41   }..  /* .  ** A
17307 6c 6c 6f 63 61 74 65 20 74 68 65 20 69 6e 64 65  llocate the inde
17308 78 20 73 74 72 75 63 74 75 72 65 2e 20 0a 20 20  x structure. .  
17309 2a 2f 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 74 72  */.  nName = str
1730a 6c 65 6e 28 7a 4e 61 6d 65 29 3b 0a 20 20 6e 43  len(zName);.  nC
1730b 6f 6c 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70  ol = pList->nExp
1730c 72 3b 0a 20 20 70 49 6e 64 65 78 20 3d 20 73 71  r;.  pIndex = sq
1730d 6c 69 74 65 4d 61 6c 6c 6f 63 28 20 0a 20 20 20  liteMalloc( .   
1730e 20 20 20 73 69 7a 65 6f 66 28 49 6e 64 65 78 29     sizeof(Index)
1730f 20 2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20   +              
17310 2f 2a 20 49 6e 64 65 78 20 73 74 72 75 63 74 75  /* Index structu
17311 72 65 20 20 2a 2f 0a 20 20 20 20 20 20 73 69 7a  re  */.      siz
17312 65 6f 66 28 69 6e 74 29 2a 6e 43 6f 6c 20 2b 20  eof(int)*nCol + 
17313 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
17314 65 78 2e 61 69 43 6f 6c 75 6d 6e 20 20 20 2a 2f  ex.aiColumn   */
17315 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 69 6e  .      sizeof(in
17316 74 29 2a 28 6e 43 6f 6c 2b 31 29 20 2b 20 20 20  t)*(nCol+1) +   
17317 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e 61 69 52      /* Index.aiR
17318 6f 77 45 73 74 20 20 20 2a 2f 0a 20 20 20 20 20  owEst   */.     
17319 20 73 69 7a 65 6f 66 28 63 68 61 72 20 2a 29 2a   sizeof(char *)*
1731a 6e 43 6f 6c 20 2b 20 20 20 20 20 20 20 20 2f 2a  nCol +        /*
1731b 20 49 6e 64 65 78 2e 61 7a 43 6f 6c 6c 20 20 20   Index.azColl   
1731c 20 20 2a 2f 0a 20 20 20 20 20 20 73 69 7a 65 6f    */.      sizeo
1731d 66 28 75 38 29 2a 6e 43 6f 6c 20 2b 20 20 20 20  f(u8)*nCol +    
1731e 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
1731f 2e 61 53 6f 72 74 4f 72 64 65 72 20 2a 2f 0a 20  .aSortOrder */. 
17320 20 20 20 20 20 6e 4e 61 6d 65 20 2b 20 31 20 2b       nName + 1 +
17321 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17322 20 20 2f 2a 20 49 6e 64 65 78 2e 7a 4e 61 6d 65    /* Index.zName
17323 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 6e        */.      n
17324 45 78 74 72 61 20 20 20 20 20 20 20 20 20 20 20  Extra           
17325 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
17326 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
17327 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 29 3b 0a  e names */.  );.
17328 20 20 69 66 28 20 73 71 6c 69 74 65 33 4d 61 6c    if( sqlite3Mal
17329 6c 6f 63 46 61 69 6c 65 64 28 29 20 29 20 67 6f  locFailed() ) go
1732a 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69  to exit_create_i
1732b 6e 64 65 78 3b 0a 20 20 70 49 6e 64 65 78 2d 3e  ndex;.  pIndex->
1732c 61 7a 43 6f 6c 6c 20 3d 20 28 63 68 61 72 2a 2a  azColl = (char**
1732d 29 28 26 70 49 6e 64 65 78 5b 31 5d 29 3b 0a 20  )(&pIndex[1]);. 
1732e 20 70 49 6e 64 65 78 2d 3e 61 69 43 6f 6c 75 6d   pIndex->aiColum
1732f 6e 20 3d 20 28 69 6e 74 20 2a 29 28 26 70 49 6e  n = (int *)(&pIn
17330 64 65 78 2d 3e 61 7a 43 6f 6c 6c 5b 6e 43 6f 6c  dex->azColl[nCol
17331 5d 29 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 61 69  ]);.  pIndex->ai
17332 52 6f 77 45 73 74 20 3d 20 28 75 6e 73 69 67 6e  RowEst = (unsign
17333 65 64 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e 61  ed *)(&pIndex->a
17334 69 43 6f 6c 75 6d 6e 5b 6e 43 6f 6c 5d 29 3b 0a  iColumn[nCol]);.
17335 20 20 70 49 6e 64 65 78 2d 3e 61 53 6f 72 74 4f    pIndex->aSortO
17336 72 64 65 72 20 3d 20 28 75 38 20 2a 29 28 26 70  rder = (u8 *)(&p
17337 49 6e 64 65 78 2d 3e 61 69 52 6f 77 45 73 74 5b  Index->aiRowEst[
17338 6e 43 6f 6c 2b 31 5d 29 3b 0a 20 20 70 49 6e 64  nCol+1]);.  pInd
17339 65 78 2d 3e 7a 4e 61 6d 65 20 3d 20 28 63 68 61  ex->zName = (cha
1733a 72 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e 61 53  r *)(&pIndex->aS
1733b 6f 72 74 4f 72 64 65 72 5b 6e 43 6f 6c 5d 29 3b  ortOrder[nCol]);
1733c 0a 20 20 7a 45 78 74 72 61 20 3d 20 28 63 68 61  .  zExtra = (cha
1733d 72 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e 7a 4e  r *)(&pIndex->zN
1733e 61 6d 65 5b 6e 4e 61 6d 65 2b 31 5d 29 3b 0a 20  ame[nName+1]);. 
1733f 20 6d 65 6d 63 70 79 28 70 49 6e 64 65 78 2d 3e   memcpy(pIndex->
17340 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 6e 4e  zName, zName, nN
17341 61 6d 65 2b 31 29 3b 0a 20 20 70 49 6e 64 65 78  ame+1);.  pIndex
17342 2d 3e 70 54 61 62 6c 65 20 3d 20 70 54 61 62 3b  ->pTable = pTab;
17343 0a 20 20 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75  .  pIndex->nColu
17344 6d 6e 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70  mn = pList->nExp
17345 72 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 6f 6e 45  r;.  pIndex->onE
17346 72 72 6f 72 20 3d 20 6f 6e 45 72 72 6f 72 3b 0a  rror = onError;.
17347 20 20 70 49 6e 64 65 78 2d 3e 61 75 74 6f 49 6e    pIndex->autoIn
17348 64 65 78 20 3d 20 70 4e 61 6d 65 3d 3d 30 3b 0a  dex = pName==0;.
17349 20 20 70 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d    pIndex->pSchem
1734a 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  a = db->aDb[iDb]
1734b 2e 70 53 63 68 65 6d 61 3b 0a 0a 20 20 2f 2a 20  .pSchema;..  /* 
1734c 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  Check to see if 
1734d 77 65 20 73 68 6f 75 6c 64 20 68 6f 6e 6f 72 20  we should honor 
1734e 44 45 53 43 20 72 65 71 75 65 73 74 73 20 6f 6e  DESC requests on
1734f 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e 73 0a 20   index columns. 
17350 20 2a 2f 0a 20 20 69 66 28 20 70 44 62 2d 3e 70   */.  if( pDb->p
17351 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72  Schema->file_for
17352 6d 61 74 3e 3d 34 20 29 7b 0a 20 20 20 20 73 6f  mat>=4 ){.    so
17353 72 74 4f 72 64 65 72 4d 61 73 6b 20 3d 20 2d 31  rtOrderMask = -1
17354 3b 20 20 20 2f 2a 20 48 6f 6e 6f 72 20 44 45 53  ;   /* Honor DES
17355 43 20 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  C */.  }else{.  
17356 20 20 73 6f 72 74 4f 72 64 65 72 4d 61 73 6b 20    sortOrderMask 
17357 3d 20 30 3b 20 20 20 20 2f 2a 20 49 67 6e 6f 72  = 0;    /* Ignor
17358 65 20 44 45 53 43 20 2a 2f 0a 20 20 7d 0a 0a 20  e DESC */.  }.. 
17359 20 2f 2a 20 53 63 61 6e 20 74 68 65 20 6e 61 6d   /* Scan the nam
1735a 65 73 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  es of the column
1735b 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74  s of the table t
1735c 6f 20 62 65 20 69 6e 64 65 78 65 64 20 61 6e 64  o be indexed and
1735d 0a 20 20 2a 2a 20 6c 6f 61 64 20 74 68 65 20 63  .  ** load the c
1735e 6f 6c 75 6d 6e 20 69 6e 64 69 63 65 73 20 69 6e  olumn indices in
1735f 74 6f 20 74 68 65 20 49 6e 64 65 78 20 73 74 72  to the Index str
17360 75 63 74 75 72 65 2e 20 20 52 65 70 6f 72 74 20  ucture.  Report 
17361 61 6e 20 65 72 72 6f 72 0a 20 20 2a 2a 20 69 66  an error.  ** if
17362 20 61 6e 79 20 63 6f 6c 75 6d 6e 20 69 73 20 6e   any column is n
17363 6f 74 20 66 6f 75 6e 64 2e 0a 20 20 2a 2f 0a 20  ot found..  */. 
17364 20 66 6f 72 28 69 3d 30 2c 20 70 4c 69 73 74 49   for(i=0, pListI
17365 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3c  tem=pList->a; i<
17366 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b  pList->nExpr; i+
17367 2b 2c 20 70 4c 69 73 74 49 74 65 6d 2b 2b 29 7b  +, pListItem++){
17368 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
17369 2a 7a 43 6f 6c 4e 61 6d 65 20 3d 20 70 4c 69 73  *zColName = pLis
1736a 74 49 74 65 6d 2d 3e 7a 4e 61 6d 65 3b 0a 20 20  tItem->zName;.  
1736b 20 20 43 6f 6c 75 6d 6e 20 2a 70 54 61 62 43 6f    Column *pTabCo
1736c 6c 3b 0a 20 20 20 20 69 6e 74 20 72 65 71 75 65  l;.    int reque
1736d 73 74 65 64 53 6f 72 74 4f 72 64 65 72 3b 0a 20  stedSortOrder;. 
1736e 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 20     char *zColl; 
1736f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17370 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73    /* Collation s
17371 65 71 75 65 6e 63 65 20 6e 61 6d 65 20 2a 2f 0a  equence name */.
17372 0a 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 54  .    for(j=0, pT
17373 61 62 43 6f 6c 3d 70 54 61 62 2d 3e 61 43 6f 6c  abCol=pTab->aCol
17374 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20  ; j<pTab->nCol; 
17375 6a 2b 2b 2c 20 70 54 61 62 43 6f 6c 2b 2b 29 7b  j++, pTabCol++){
17376 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
17377 65 33 53 74 72 49 43 6d 70 28 7a 43 6f 6c 4e 61  e3StrICmp(zColNa
17378 6d 65 2c 20 70 54 61 62 43 6f 6c 2d 3e 7a 4e 61  me, pTabCol->zNa
17379 6d 65 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a  me)==0 ) break;.
1737a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6a 3e      }.    if( j>
1737b 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20  =pTab->nCol ){. 
1737c 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
1737d 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 61  rMsg(pParse, "ta
1737e 62 6c 65 20 25 73 20 68 61 73 20 6e 6f 20 63 6f  ble %s has no co
1737f 6c 75 6d 6e 20 6e 61 6d 65 64 20 25 73 22 2c 0a  lumn named %s",.
17380 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e 7a 4e          pTab->zN
17381 61 6d 65 2c 20 7a 43 6f 6c 4e 61 6d 65 29 3b 0a  ame, zColName);.
17382 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f        goto exit_
17383 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20  create_index;.  
17384 20 20 7d 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 3a    }.    /* TODO:
17385 20 20 41 64 64 20 61 20 74 65 73 74 20 74 6f 20    Add a test to 
17386 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74  make sure that t
17387 68 65 20 73 61 6d 65 20 63 6f 6c 75 6d 6e 20 69  he same column i
17388 73 20 6e 6f 74 20 6e 61 6d 65 64 0a 20 20 20 20  s not named.    
17389 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63  ** more than onc
1738a 65 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d  e within the sam
1738b 65 20 69 6e 64 65 78 2e 20 20 4f 6e 6c 79 20 74  e index.  Only t
1738c 68 65 20 66 69 72 73 74 20 69 6e 73 74 61 6e 63  he first instanc
1738d 65 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 20  e of.    ** the 
1738e 63 6f 6c 75 6d 6e 20 77 69 6c 6c 20 65 76 65 72  column will ever
1738f 20 62 65 20 75 73 65 64 20 62 79 20 74 68 65 20   be used by the 
17390 6f 70 74 69 6d 69 7a 65 72 2e 20 20 4e 6f 74 65  optimizer.  Note
17391 20 74 68 61 74 20 75 73 69 6e 67 20 74 68 65 0a   that using the.
17392 20 20 20 20 2a 2a 20 73 61 6d 65 20 63 6f 6c 75      ** same colu
17393 6d 6e 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63  mn more than onc
17394 65 20 63 61 6e 6e 6f 74 20 62 65 20 61 6e 20 65  e cannot be an e
17395 72 72 6f 72 20 62 65 63 61 75 73 65 20 74 68 61  rror because tha
17396 74 20 77 6f 75 6c 64 20 0a 20 20 20 20 2a 2a 20  t would .    ** 
17397 62 72 65 61 6b 20 62 61 63 6b 77 61 72 64 73 20  break backwards 
17398 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 2d 20  compatibility - 
17399 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 61  it needs to be a
1739a 20 77 61 72 6e 69 6e 67 2e 0a 20 20 20 20 2a 2f   warning..    */
1739b 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 69 43  .    pIndex->aiC
1739c 6f 6c 75 6d 6e 5b 69 5d 20 3d 20 6a 3b 0a 20 20  olumn[i] = j;.  
1739d 20 20 69 66 28 20 70 4c 69 73 74 49 74 65 6d 2d    if( pListItem-
1739e 3e 70 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20  >pExpr ){.      
1739f 61 73 73 65 72 74 28 20 70 4c 69 73 74 49 74 65  assert( pListIte
173a0 6d 2d 3e 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 20  m->pExpr->pColl 
173a1 29 3b 0a 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d  );.      zColl =
173a2 20 7a 45 78 74 72 61 3b 0a 20 20 20 20 20 20 73   zExtra;.      s
173a3 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
173a4 6e 45 78 74 72 61 2c 20 7a 45 78 74 72 61 2c 20  nExtra, zExtra, 
173a5 22 25 73 22 2c 20 70 4c 69 73 74 49 74 65 6d 2d  "%s", pListItem-
173a6 3e 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 2d 3e 7a  >pExpr->pColl->z
173a7 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7a 45 78  Name);.      zEx
173a8 74 72 61 20 2b 3d 20 28 73 74 72 6c 65 6e 28 7a  tra += (strlen(z
173a9 43 6f 6c 6c 29 20 2b 20 31 29 3b 0a 20 20 20 20  Coll) + 1);.    
173aa 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 43 6f  }else{.      zCo
173ab 6c 6c 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b  ll = pTab->aCol[
173ac 6a 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 20  j].zColl;.      
173ad 69 66 28 20 21 7a 43 6f 6c 6c 20 29 7b 0a 20 20  if( !zColl ){.  
173ae 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 64 62        zColl = db
173af 2d 3e 70 44 66 6c 74 43 6f 6c 6c 2d 3e 7a 4e 61  ->pDfltColl->zNa
173b0 6d 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  me;.      }.    
173b1 7d 0a 20 20 20 20 69 66 28 20 21 64 62 2d 3e 69  }.    if( !db->i
173b2 6e 69 74 2e 62 75 73 79 20 26 26 20 21 73 71 6c  nit.busy && !sql
173b3 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65  ite3LocateCollSe
173b4 71 28 70 50 61 72 73 65 2c 20 7a 43 6f 6c 6c 2c  q(pParse, zColl,
173b5 20 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 67 6f   -1) ){.      go
173b6 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69  to exit_create_i
173b7 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ndex;.    }.    
173b8 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 5b 69  pIndex->azColl[i
173b9 5d 20 3d 20 7a 43 6f 6c 6c 3b 0a 20 20 20 20 72  ] = zColl;.    r
173ba 65 71 75 65 73 74 65 64 53 6f 72 74 4f 72 64 65  equestedSortOrde
173bb 72 20 3d 20 70 4c 69 73 74 49 74 65 6d 2d 3e 73  r = pListItem->s
173bc 6f 72 74 4f 72 64 65 72 20 26 20 73 6f 72 74 4f  ortOrder & sortO
173bd 72 64 65 72 4d 61 73 6b 3b 0a 20 20 20 20 70 49  rderMask;.    pI
173be 6e 64 65 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72  ndex->aSortOrder
173bf 5b 69 5d 20 3d 20 72 65 71 75 65 73 74 65 64 53  [i] = requestedS
173c0 6f 72 74 4f 72 64 65 72 3b 0a 20 20 7d 0a 20 20  ortOrder;.  }.  
173c1 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 52 6f  sqlite3DefaultRo
173c2 77 45 73 74 28 70 49 6e 64 65 78 29 3b 0a 0a 20  wEst(pIndex);.. 
173c3 20 69 66 28 20 70 54 61 62 3d 3d 70 50 61 72 73   if( pTab==pPars
173c4 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 29 7b 0a  e->pNewTable ){.
173c5 20 20 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74      /* This rout
173c6 69 6e 65 20 68 61 73 20 62 65 65 6e 20 63 61 6c  ine has been cal
173c7 6c 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 6e  led to create an
173c8 20 61 75 74 6f 6d 61 74 69 63 20 69 6e 64 65 78   automatic index
173c9 20 61 73 20 61 0a 20 20 20 20 2a 2a 20 72 65 73   as a.    ** res
173ca 75 6c 74 20 6f 66 20 61 20 50 52 49 4d 41 52 59  ult of a PRIMARY
173cb 20 4b 45 59 20 6f 72 20 55 4e 49 51 55 45 20 63   KEY or UNIQUE c
173cc 6c 61 75 73 65 20 6f 6e 20 61 20 63 6f 6c 75 6d  lause on a colum
173cd 6e 20 64 65 66 69 6e 69 74 69 6f 6e 2c 20 6f 72  n definition, or
173ce 0a 20 20 20 20 2a 2a 20 61 20 50 52 49 4d 41 52  .    ** a PRIMAR
173cf 59 20 4b 45 59 20 6f 72 20 55 4e 49 51 55 45 20  Y KEY or UNIQUE 
173d0 63 6c 61 75 73 65 20 66 6f 6c 6c 6f 77 69 6e 67  clause following
173d1 20 74 68 65 20 63 6f 6c 75 6d 6e 20 64 65 66 69   the column defi
173d2 6e 69 74 69 6f 6e 73 2e 0a 20 20 20 20 2a 2a 20  nitions..    ** 
173d3 69 2e 65 2e 20 6f 6e 65 20 6f 66 3a 0a 20 20 20  i.e. one of:.   
173d4 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 52 45 41 54   **.    ** CREAT
173d5 45 20 54 41 42 4c 45 20 74 28 78 20 50 52 49 4d  E TABLE t(x PRIM
173d6 41 52 59 20 4b 45 59 2c 20 79 29 3b 0a 20 20 20  ARY KEY, y);.   
173d7 20 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45   ** CREATE TABLE
173d8 20 74 28 78 2c 20 79 2c 20 55 4e 49 51 55 45 28   t(x, y, UNIQUE(
173d9 78 2c 20 79 29 29 3b 0a 20 20 20 20 2a 2a 0a 20  x, y));.    **. 
173da 20 20 20 2a 2a 20 45 69 74 68 65 72 20 77 61 79     ** Either way
173db 2c 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69  , check to see i
173dc 66 20 74 68 65 20 74 61 62 6c 65 20 61 6c 72 65  f the table alre
173dd 61 64 79 20 68 61 73 20 73 75 63 68 20 61 6e 20  ady has such an 
173de 69 6e 64 65 78 2e 20 49 66 0a 20 20 20 20 2a 2a  index. If.    **
173df 20 73 6f 2c 20 64 6f 6e 27 74 20 62 6f 74 68 65   so, don't bothe
173e0 72 20 63 72 65 61 74 69 6e 67 20 74 68 69 73 20  r creating this 
173e1 6f 6e 65 2e 20 54 68 69 73 20 6f 6e 6c 79 20 61  one. This only a
173e2 70 70 6c 69 65 73 20 74 6f 0a 20 20 20 20 2a 2a  pplies to.    **
173e3 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63   automatically c
173e4 72 65 61 74 65 64 20 69 6e 64 69 63 65 73 2e 20  reated indices. 
173e5 55 73 65 72 73 20 63 61 6e 20 64 6f 20 61 73 20  Users can do as 
173e6 74 68 65 79 20 77 69 73 68 20 77 69 74 68 0a 20  they wish with. 
173e7 20 20 20 2a 2a 20 65 78 70 6c 69 63 69 74 20 69     ** explicit i
173e8 6e 64 69 63 65 73 2e 0a 20 20 20 20 2a 2f 0a 20  ndices..    */. 
173e9 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a     Index *pIdx;.
173ea 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61      for(pIdx=pTa
173eb 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b  b->pIndex; pIdx;
173ec 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
173ed 74 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6b 3b  t){.      int k;
173ee 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
173ef 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45  Idx->onError!=OE
173f0 5f 4e 6f 6e 65 20 29 3b 0a 20 20 20 20 20 20 61  _None );.      a
173f1 73 73 65 72 74 28 20 70 49 64 78 2d 3e 61 75 74  ssert( pIdx->aut
173f2 6f 49 6e 64 65 78 20 29 3b 0a 20 20 20 20 20 20  oIndex );.      
173f3 61 73 73 65 72 74 28 20 70 49 6e 64 65 78 2d 3e  assert( pIndex->
173f4 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65  onError!=OE_None
173f5 20 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 70   );..      if( p
173f6 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 21 3d 70 49  Idx->nColumn!=pI
173f7 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 20  ndex->nColumn ) 
173f8 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
173f9 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 49 64 78 2d  for(k=0; k<pIdx-
173fa 3e 6e 43 6f 6c 75 6d 6e 3b 20 6b 2b 2b 29 7b 0a  >nColumn; k++){.
173fb 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
173fc 61 72 20 2a 7a 31 20 3d 20 70 49 64 78 2d 3e 61  ar *z1 = pIdx->a
173fd 7a 43 6f 6c 6c 5b 6b 5d 3b 0a 20 20 20 20 20 20  zColl[k];.      
173fe 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32    const char *z2
173ff 20 3d 20 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c   = pIndex->azCol
17400 6c 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 20 69 66  l[k];.        if
17401 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e  ( pIdx->aiColumn
17402 5b 6b 5d 21 3d 70 49 6e 64 65 78 2d 3e 61 69 43  [k]!=pIndex->aiC
17403 6f 6c 75 6d 6e 5b 6b 5d 20 29 20 62 72 65 61 6b  olumn[k] ) break
17404 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 49  ;.        if( pI
17405 64 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 6b  dx->aSortOrder[k
17406 5d 21 3d 70 49 6e 64 65 78 2d 3e 61 53 6f 72 74  ]!=pIndex->aSort
17407 4f 72 64 65 72 5b 6b 5d 20 29 20 62 72 65 61 6b  Order[k] ) break
17408 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 31  ;.        if( z1
17409 21 3d 7a 32 20 26 26 20 73 71 6c 69 74 65 33 53  !=z2 && sqlite3S
1740a 74 72 49 43 6d 70 28 7a 31 2c 20 7a 32 29 20 29  trICmp(z1, z2) )
1740b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
1740c 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 70 49 64        if( k==pId
1740d 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20  x->nColumn ){.  
1740e 20 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e        if( pIdx->
1740f 6f 6e 45 72 72 6f 72 21 3d 70 49 6e 64 65 78 2d  onError!=pIndex-
17410 3e 6f 6e 45 72 72 6f 72 20 29 7b 0a 20 20 20 20  >onError ){.    
17411 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63 6f        /* This co
17412 6e 73 74 72 61 69 6e 74 20 63 72 65 61 74 65 73  nstraint creates
17413 20 74 68 65 20 73 61 6d 65 20 69 6e 64 65 78 20   the same index 
17414 61 73 20 61 20 70 72 65 76 69 6f 75 73 0a 20 20  as a previous.  
17415 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 73 74          ** const
17416 72 61 69 6e 74 20 73 70 65 63 69 66 69 65 64 20  raint specified 
17417 73 6f 6d 65 77 68 65 72 65 20 69 6e 20 74 68 65  somewhere in the
17418 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74   CREATE TABLE st
17419 61 74 65 6d 65 6e 74 2e 0a 20 20 20 20 20 20 20  atement..       
1741a 20 20 20 2a 2a 20 48 6f 77 65 76 65 72 20 74 68     ** However th
1741b 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c  e ON CONFLICT cl
1741c 61 75 73 65 73 20 61 72 65 20 64 69 66 66 65 72  auses are differ
1741d 65 6e 74 2e 20 49 66 20 62 6f 74 68 20 74 68 69  ent. If both thi
1741e 73 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  s .          ** 
1741f 63 6f 6e 73 74 72 61 69 6e 74 20 61 6e 64 20 74  constraint and t
17420 68 65 20 70 72 65 76 69 6f 75 73 20 65 71 75 69  he previous equi
17421 76 61 6c 65 6e 74 20 63 6f 6e 73 74 72 61 69 6e  valent constrain
17422 74 20 68 61 76 65 20 65 78 70 6c 69 63 69 74 0a  t have explicit.
17423 20 20 20 20 20 20 20 20 20 20 2a 2a 20 4f 4e 20            ** ON 
17424 43 4f 4e 46 4c 49 43 54 20 63 6c 61 75 73 65 73  CONFLICT clauses
17425 20 74 68 69 73 20 69 73 20 61 6e 20 65 72 72 6f   this is an erro
17426 72 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 75 73  r. Otherwise, us
17427 65 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20  e the.          
17428 2a 2a 20 65 78 70 6c 69 63 69 74 6c 79 20 73 70  ** explicitly sp
17429 65 63 69 66 69 65 64 20 62 65 68 61 76 69 6f 75  ecified behaviou
1742a 72 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78 2e  r for the index.
1742b 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  .          */.  
1742c 20 20 20 20 20 20 20 20 69 66 28 20 21 28 70 49          if( !(pI
1742d 64 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f  dx->onError==OE_
1742e 44 65 66 61 75 6c 74 20 7c 7c 20 70 49 6e 64 65  Default || pInde
1742f 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44  x->onError==OE_D
17430 65 66 61 75 6c 74 29 20 29 7b 0a 20 20 20 20 20  efault) ){.     
17431 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
17432 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a  rorMsg(pParse, .
17433 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17434 22 63 6f 6e 66 6c 69 63 74 69 6e 67 20 4f 4e 20  "conflicting ON 
17435 43 4f 4e 46 4c 49 43 54 20 63 6c 61 75 73 65 73  CONFLICT clauses
17436 20 73 70 65 63 69 66 69 65 64 22 2c 20 30 29 3b   specified", 0);
17437 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
17438 20 20 20 20 20 20 20 69 66 28 20 70 49 64 78 2d         if( pIdx-
17439 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66  >onError==OE_Def
1743a 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20 20 20  ault ){.        
1743b 20 20 20 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f      pIdx->onErro
1743c 72 20 3d 20 70 49 6e 64 65 78 2d 3e 6f 6e 45 72  r = pIndex->onEr
1743d 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  ror;.          }
1743e 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1743f 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65     goto exit_cre
17440 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 20  ate_index;.     
17441 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
17442 2f 2a 20 4c 69 6e 6b 20 74 68 65 20 6e 65 77 20  /* Link the new 
17443 49 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 20  Index structure 
17444 74 6f 20 69 74 73 20 74 61 62 6c 65 20 61 6e 64  to its table and
17445 20 74 6f 20 74 68 65 20 6f 74 68 65 72 0a 20 20   to the other.  
17446 2a 2a 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74  ** in-memory dat
17447 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65 73  abase structures
17448 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62  . .  */.  if( db
17449 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20  ->init.busy ){. 
1744a 20 20 20 49 6e 64 65 78 20 2a 70 3b 0a 20 20 20     Index *p;.   
1744b 20 70 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68   p = sqlite3Hash
1744c 49 6e 73 65 72 74 28 26 70 49 6e 64 65 78 2d 3e  Insert(&pIndex->
1744d 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68  pSchema->idxHash
1744e 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
1744f 20 20 20 20 20 20 20 20 20 20 20 20 70 49 6e 64              pInd
17450 65 78 2d 3e 7a 4e 61 6d 65 2c 20 73 74 72 6c 65  ex->zName, strle
17451 6e 28 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 29  n(pIndex->zName)
17452 2b 31 2c 20 70 49 6e 64 65 78 29 3b 0a 20 20 20  +1, pIndex);.   
17453 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20   if( p ){.      
17454 61 73 73 65 72 74 28 20 70 3d 3d 70 49 6e 64 65  assert( p==pInde
17455 78 20 29 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20  x );  /* Malloc 
17456 6d 75 73 74 20 68 61 76 65 20 66 61 69 6c 65 64  must have failed
17457 20 2a 2f 0a 20 20 20 20 20 20 67 6f 74 6f 20 65   */.      goto e
17458 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78  xit_create_index
17459 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e  ;.    }.    db->
1745a 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f  flags |= SQLITE_
1745b 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 20  InternChanges;. 
1745c 20 20 20 69 66 28 20 70 54 62 6c 4e 61 6d 65 21     if( pTblName!
1745d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 49 6e 64  =0 ){.      pInd
1745e 65 78 2d 3e 74 6e 75 6d 20 3d 20 64 62 2d 3e 69  ex->tnum = db->i
1745f 6e 69 74 2e 6e 65 77 54 6e 75 6d 3b 0a 20 20 20  nit.newTnum;.   
17460 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20   }.  }..  /* If 
17461 74 68 65 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73  the db->init.bus
17462 79 20 69 73 20 30 20 74 68 65 6e 20 63 72 65 61  y is 0 then crea
17463 74 65 20 74 68 65 20 69 6e 64 65 78 20 6f 6e 20  te the index on 
17464 64 69 73 6b 2e 20 20 54 68 69 73 0a 20 20 2a 2a  disk.  This.  **
17465 20 69 6e 76 6f 6c 76 65 73 20 77 72 69 74 69 6e   involves writin
17466 67 20 74 68 65 20 69 6e 64 65 78 20 69 6e 74 6f  g the index into
17467 20 74 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c   the master tabl
17468 65 20 61 6e 64 20 66 69 6c 6c 69 6e 67 20 69 6e  e and filling in
17469 20 74 68 65 0a 20 20 2a 2a 20 69 6e 64 65 78 20   the.  ** index 
1746a 77 69 74 68 20 74 68 65 20 63 75 72 72 65 6e 74  with the current
1746b 20 74 61 62 6c 65 20 63 6f 6e 74 65 6e 74 73 2e   table contents.
1746c 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 64  .  **.  ** The d
1746d 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 69 73 20  b->init.busy is 
1746e 30 20 77 68 65 6e 20 74 68 65 20 75 73 65 72 20  0 when the user 
1746f 66 69 72 73 74 20 65 6e 74 65 72 73 20 61 20 43  first enters a C
17470 52 45 41 54 45 20 49 4e 44 45 58 20 0a 20 20 2a  REATE INDEX .  *
17471 2a 20 63 6f 6d 6d 61 6e 64 2e 20 20 64 62 2d 3e  * command.  db->
17472 69 6e 69 74 2e 62 75 73 79 20 69 73 20 31 20 77  init.busy is 1 w
17473 68 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 69  hen a database i
17474 73 20 6f 70 65 6e 65 64 20 61 6e 64 20 0a 20 20  s opened and .  
17475 2a 2a 20 43 52 45 41 54 45 20 49 4e 44 45 58 20  ** CREATE INDEX 
17476 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 72  statements are r
17477 65 61 64 20 6f 75 74 20 6f 66 20 74 68 65 20 6d  ead out of the m
17478 61 73 74 65 72 20 74 61 62 6c 65 2e 20 20 49 6e  aster table.  In
17479 0a 20 20 2a 2a 20 74 68 65 20 6c 61 74 74 65 72  .  ** the latter
1747a 20 63 61 73 65 20 74 68 65 20 69 6e 64 65 78 20   case the index 
1747b 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 6f  already exists o
1747c 6e 20 64 69 73 6b 2c 20 77 68 69 63 68 20 69 73  n disk, which is
1747d 20 77 68 79 0a 20 20 2a 2a 20 77 65 20 64 6f 6e   why.  ** we don
1747e 27 74 20 77 61 6e 74 20 74 6f 20 72 65 63 72 65  't want to recre
1747f 61 74 65 20 69 74 2e 0a 20 20 2a 2a 0a 20 20 2a  ate it..  **.  *
17480 2a 20 49 66 20 70 54 62 6c 4e 61 6d 65 3d 3d 30  * If pTblName==0
17481 20 69 74 20 6d 65 61 6e 73 20 74 68 69 73 20 69   it means this i
17482 6e 64 65 78 20 69 73 20 67 65 6e 65 72 61 74 65  ndex is generate
17483 64 20 61 73 20 61 20 70 72 69 6d 61 72 79 20 6b  d as a primary k
17484 65 79 0a 20 20 2a 2a 20 6f 72 20 55 4e 49 51 55  ey.  ** or UNIQU
17485 45 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f 66 20  E constraint of 
17486 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73  a CREATE TABLE s
17487 74 61 74 65 6d 65 6e 74 2e 20 20 53 69 6e 63 65  tatement.  Since
17488 20 74 68 65 20 74 61 62 6c 65 0a 20 20 2a 2a 20   the table.  ** 
17489 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 63 72  has just been cr
1748a 65 61 74 65 64 2c 20 69 74 20 63 6f 6e 74 61 69  eated, it contai
1748b 6e 73 20 6e 6f 20 64 61 74 61 20 61 6e 64 20 74  ns no data and t
1748c 68 65 20 69 6e 64 65 78 20 69 6e 69 74 69 61 6c  he index initial
1748d 69 7a 61 74 69 6f 6e 0a 20 20 2a 2a 20 73 74 65  ization.  ** ste
1748e 70 20 63 61 6e 20 62 65 20 73 6b 69 70 70 65 64  p can be skipped
1748f 2e 0a 20 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66  ..  */.  else if
17490 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d  ( db->init.busy=
17491 3d 30 20 29 7b 0a 20 20 20 20 56 64 62 65 20 2a  =0 ){.    Vdbe *
17492 76 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 74  v;.    char *zSt
17493 6d 74 3b 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d  mt;.    int iMem
17494 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b   = pParse->nMem+
17495 2b 3b 0a 0a 20 20 20 20 76 20 3d 20 73 71 6c 69  +;..    v = sqli
17496 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
17497 65 29 3b 0a 20 20 20 20 69 66 28 20 76 3d 3d 30  e);.    if( v==0
17498 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65   ) goto exit_cre
17499 61 74 65 5f 69 6e 64 65 78 3b 0a 0a 0a 20 20 20  ate_index;...   
1749a 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65 20 72   /* Create the r
1749b 6f 6f 74 70 61 67 65 20 66 6f 72 20 74 68 65 20  ootpage for the 
1749c 69 6e 64 65 78 0a 20 20 20 20 2a 2f 0a 20 20 20  index.    */.   
1749d 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69   sqlite3BeginWri
1749e 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72  teOperation(pPar
1749f 73 65 2c 20 31 2c 20 69 44 62 29 3b 0a 20 20 20  se, 1, iDb);.   
174a0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
174a1 70 28 76 2c 20 4f 50 5f 43 72 65 61 74 65 49 6e  p(v, OP_CreateIn
174a2 64 65 78 2c 20 69 44 62 2c 20 30 29 3b 0a 20 20  dex, iDb, 0);.  
174a3 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
174a4 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 53 74 6f 72  Op(v, OP_MemStor
174a5 65 2c 20 69 4d 65 6d 2c 20 30 29 3b 0a 0a 20 20  e, iMem, 0);..  
174a6 20 20 2f 2a 20 47 61 74 68 65 72 20 74 68 65 20    /* Gather the 
174a7 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66  complete text of
174a8 20 74 68 65 20 43 52 45 41 54 45 20 49 4e 44 45   the CREATE INDE
174a9 58 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 74 6f  X statement into
174aa 0a 20 20 20 20 2a 2a 20 74 68 65 20 7a 53 74 6d  .    ** the zStm
174ab 74 20 76 61 72 69 61 62 6c 65 0a 20 20 20 20 2a  t variable.    *
174ac 2f 0a 20 20 20 20 69 66 28 20 70 53 74 61 72 74  /.    if( pStart
174ad 20 26 26 20 70 45 6e 64 20 29 7b 0a 20 20 20 20   && pEnd ){.    
174ae 20 20 2f 2a 20 41 20 6e 61 6d 65 64 20 69 6e 64    /* A named ind
174af 65 78 20 77 69 74 68 20 61 6e 20 65 78 70 6c 69  ex with an expli
174b0 63 69 74 20 43 52 45 41 54 45 20 49 4e 44 45 58  cit CREATE INDEX
174b1 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
174b2 20 20 20 20 7a 53 74 6d 74 20 3d 20 73 71 6c 69      zStmt = sqli
174b3 74 65 33 4d 50 72 69 6e 74 66 28 22 43 52 45 41  te3MPrintf("CREA
174b4 54 45 25 73 20 49 4e 44 45 58 20 25 2e 2a 73 22  TE%s INDEX %.*s"
174b5 2c 0a 20 20 20 20 20 20 20 20 6f 6e 45 72 72 6f  ,.        onErro
174b6 72 3d 3d 4f 45 5f 4e 6f 6e 65 20 3f 20 22 22 20  r==OE_None ? "" 
174b7 3a 20 22 20 55 4e 49 51 55 45 22 2c 0a 20 20 20  : " UNIQUE",.   
174b8 20 20 20 20 20 70 45 6e 64 2d 3e 7a 20 2d 20 70       pEnd->z - p
174b9 4e 61 6d 65 2d 3e 7a 20 2b 20 31 2c 0a 20 20 20  Name->z + 1,.   
174ba 20 20 20 20 20 70 4e 61 6d 65 2d 3e 7a 29 3b 0a       pName->z);.
174bb 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
174bc 20 2f 2a 20 41 6e 20 61 75 74 6f 6d 61 74 69 63   /* An automatic
174bd 20 69 6e 64 65 78 20 63 72 65 61 74 65 64 20 62   index created b
174be 79 20 61 20 50 52 49 4d 41 52 59 20 4b 45 59 20  y a PRIMARY KEY 
174bf 6f 72 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72  or UNIQUE constr
174c0 61 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 2f 2a  aint */.      /*
174c1 20 7a 53 74 6d 74 20 3d 20 73 71 6c 69 74 65 33   zStmt = sqlite3
174c2 4d 50 72 69 6e 74 66 28 22 22 29 3b 20 2a 2f 0a  MPrintf(""); */.
174c3 20 20 20 20 20 20 7a 53 74 6d 74 20 3d 20 30 3b        zStmt = 0;
174c4 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41  .    }..    /* A
174c5 64 64 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 73  dd an entry in s
174c6 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 66 6f 72  qlite_master for
174c7 20 74 68 69 73 20 69 6e 64 65 78 0a 20 20 20 20   this index.    
174c8 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65  */.    sqlite3Ne
174c9 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65  stedParse(pParse
174ca 2c 20 0a 20 20 20 20 20 20 20 20 22 49 4e 53 45  , .        "INSE
174cb 52 54 20 49 4e 54 4f 20 25 51 2e 25 73 20 56 41  RT INTO %Q.%s VA
174cc 4c 55 45 53 28 27 69 6e 64 65 78 27 2c 25 51 2c  LUES('index',%Q,
174cd 25 51 2c 23 30 2c 25 51 29 3b 22 2c 0a 20 20 20  %Q,#0,%Q);",.   
174ce 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62       db->aDb[iDb
174cf 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f  ].zName, SCHEMA_
174d0 54 41 42 4c 45 28 69 44 62 29 2c 0a 20 20 20 20  TABLE(iDb),.    
174d1 20 20 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d      pIndex->zNam
174d2 65 2c 0a 20 20 20 20 20 20 20 20 70 54 61 62 2d  e,.        pTab-
174d3 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20  >zName,.        
174d4 7a 53 74 6d 74 0a 20 20 20 20 29 3b 0a 20 20 20  zStmt.    );.   
174d5 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
174d6 70 28 76 2c 20 4f 50 5f 50 6f 70 2c 20 31 2c 20  p(v, OP_Pop, 1, 
174d7 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 46 72  0);.    sqliteFr
174d8 65 65 28 7a 53 74 6d 74 29 3b 0a 0a 20 20 20 20  ee(zStmt);..    
174d9 2f 2a 20 46 69 6c 6c 20 74 68 65 20 69 6e 64 65  /* Fill the inde
174da 78 20 77 69 74 68 20 64 61 74 61 20 61 6e 64 20  x with data and 
174db 72 65 70 61 72 73 65 20 74 68 65 20 73 63 68 65  reparse the sche
174dc 6d 61 2e 20 43 6f 64 65 20 61 6e 20 4f 50 5f 45  ma. Code an OP_E
174dd 78 70 69 72 65 0a 20 20 20 20 2a 2a 20 74 6f 20  xpire.    ** to 
174de 69 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 70  invalidate all p
174df 72 65 2d 63 6f 6d 70 69 6c 65 64 20 73 74 61 74  re-compiled stat
174e0 65 6d 65 6e 74 73 2e 0a 20 20 20 20 2a 2f 0a 20  ements..    */. 
174e1 20 20 20 69 66 28 20 70 54 62 6c 4e 61 6d 65 20     if( pTblName 
174e2 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
174e3 52 65 66 69 6c 6c 49 6e 64 65 78 28 70 50 61 72  RefillIndex(pPar
174e4 73 65 2c 20 70 49 6e 64 65 78 2c 20 69 4d 65 6d  se, pIndex, iMem
174e5 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
174e6 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 64 62 2c  ChangeCookie(db,
174e7 20 76 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20   v, iDb);.      
174e8 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28 76  sqlite3VdbeOp3(v
174e9 2c 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61  , OP_ParseSchema
174ea 2c 20 69 44 62 2c 20 30 2c 0a 20 20 20 20 20 20  , iDb, 0,.      
174eb 20 20 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74     sqlite3MPrint
174ec 66 28 22 6e 61 6d 65 3d 27 25 71 27 22 2c 20 70  f("name='%q'", p
174ed 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 29 2c 20 50  Index->zName), P
174ee 33 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20  3_DYNAMIC);.    
174ef 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
174f0 4f 70 28 76 2c 20 4f 50 5f 45 78 70 69 72 65 2c  Op(v, OP_Expire,
174f1 20 30 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20   0, 0);.    }.  
174f2 7d 0a 0a 20 20 2f 2a 20 57 68 65 6e 20 61 64 64  }..  /* When add
174f3 69 6e 67 20 61 6e 20 69 6e 64 65 78 20 74 6f 20  ing an index to 
174f4 74 68 65 20 6c 69 73 74 20 6f 66 20 69 6e 64 69  the list of indi
174f5 63 65 73 20 66 6f 72 20 61 20 74 61 62 6c 65 2c  ces for a table,
174f6 20 6d 61 6b 65 0a 20 20 2a 2a 20 73 75 72 65 20   make.  ** sure 
174f7 61 6c 6c 20 69 6e 64 69 63 65 73 20 6c 61 62 65  all indices labe
174f8 6c 65 64 20 4f 45 5f 52 65 70 6c 61 63 65 20 63  led OE_Replace c
174f9 6f 6d 65 20 61 66 74 65 72 20 61 6c 6c 20 74 68  ome after all th
174fa 6f 73 65 20 6c 61 62 65 6c 65 64 0a 20 20 2a 2a  ose labeled.  **
174fb 20 4f 45 5f 49 67 6e 6f 72 65 2e 20 20 54 68 69   OE_Ignore.  Thi
174fc 73 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 66  s is necessary f
174fd 6f 72 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f  or the correct o
174fe 70 65 72 61 74 69 6f 6e 20 6f 66 20 55 50 44 41  peration of UPDA
174ff 54 45 0a 20 20 2a 2a 20 61 6e 64 20 49 4e 53 45  TE.  ** and INSE
17500 52 54 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64  RT..  */.  if( d
17501 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 7c 7c 20  b->init.busy || 
17502 70 54 62 6c 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20  pTblName==0 ){. 
17503 20 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 21 3d     if( onError!=
17504 4f 45 5f 52 65 70 6c 61 63 65 20 7c 7c 20 70 54  OE_Replace || pT
17505 61 62 2d 3e 70 49 6e 64 65 78 3d 3d 30 0a 20 20  ab->pIndex==0.  
17506 20 20 20 20 20 20 20 7c 7c 20 70 54 61 62 2d 3e         || pTab->
17507 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 3d  pIndex->onError=
17508 3d 4f 45 5f 52 65 70 6c 61 63 65 29 7b 0a 20 20  =OE_Replace){.  
17509 20 20 20 20 70 49 6e 64 65 78 2d 3e 70 4e 65 78      pIndex->pNex
1750a 74 20 3d 20 70 54 61 62 2d 3e 70 49 6e 64 65 78  t = pTab->pIndex
1750b 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 70 49  ;.      pTab->pI
1750c 6e 64 65 78 20 3d 20 70 49 6e 64 65 78 3b 0a 20  ndex = pIndex;. 
1750d 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1750e 49 6e 64 65 78 20 2a 70 4f 74 68 65 72 20 3d 20  Index *pOther = 
1750f 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 0a 20 20  pTab->pIndex;.  
17510 20 20 20 20 77 68 69 6c 65 28 20 70 4f 74 68 65      while( pOthe
17511 72 2d 3e 70 4e 65 78 74 20 26 26 20 70 4f 74 68  r->pNext && pOth
17512 65 72 2d 3e 70 4e 65 78 74 2d 3e 6f 6e 45 72 72  er->pNext->onErr
17513 6f 72 21 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29  or!=OE_Replace )
17514 7b 0a 20 20 20 20 20 20 20 20 70 4f 74 68 65 72  {.        pOther
17515 20 3d 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74   = pOther->pNext
17516 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
17517 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 20 3d 20  pIndex->pNext = 
17518 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 3b 0a 20  pOther->pNext;. 
17519 20 20 20 20 20 70 4f 74 68 65 72 2d 3e 70 4e 65       pOther->pNe
1751a 78 74 20 3d 20 70 49 6e 64 65 78 3b 0a 20 20 20  xt = pIndex;.   
1751b 20 7d 0a 20 20 20 20 70 49 6e 64 65 78 20 3d 20   }.    pIndex = 
1751c 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65  0;.  }..  /* Cle
1751d 61 6e 20 75 70 20 62 65 66 6f 72 65 20 65 78 69  an up before exi
1751e 74 69 6e 67 20 2a 2f 0a 65 78 69 74 5f 63 72 65  ting */.exit_cre
1751f 61 74 65 5f 69 6e 64 65 78 3a 0a 20 20 69 66 28  ate_index:.  if(
17520 20 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 66   pIndex ){.    f
17521 72 65 65 49 6e 64 65 78 28 70 49 6e 64 65 78 29  reeIndex(pIndex)
17522 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45  ;.  }.  sqlite3E
17523 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 4c  xprListDelete(pL
17524 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 53  ist);.  sqlite3S
17525 72 63 4c 69 73 74 44 65 6c 65 74 65 28 70 54 62  rcListDelete(pTb
17526 6c 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65  lName);.  sqlite
17527 46 72 65 65 28 7a 4e 61 6d 65 29 3b 0a 20 20 72  Free(zName);.  r
17528 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  eturn;.}../*.** 
17529 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
1752a 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 66   make sure the f
1752b 69 6c 65 20 66 6f 72 6d 61 74 20 6e 75 6d 62 65  ile format numbe
1752c 72 20 69 73 20 61 74 20 6c 65 61 73 74 20 6d 69  r is at least mi
1752d 6e 46 6f 72 6d 61 74 2e 0a 2a 2a 20 54 68 65 20  nFormat..** The 
1752e 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65 20 77  generated code w
1752f 69 6c 6c 20 69 6e 63 72 65 61 73 65 20 74 68 65  ill increase the
17530 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 6e 75 6d   file format num
17531 62 65 72 20 69 66 20 6e 65 63 65 73 73 61 72 79  ber if necessary
17532 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
17533 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
17534 4d 69 6e 69 6d 75 6d 46 69 6c 65 46 6f 72 6d 61  MinimumFileForma
17535 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  t(Parse *pParse,
17536 20 69 6e 74 20 69 44 62 2c 20 69 6e 74 20 6d 69   int iDb, int mi
17537 6e 46 6f 72 6d 61 74 29 7b 0a 20 20 56 64 62 65  nFormat){.  Vdbe
17538 20 2a 76 3b 0a 20 20 76 20 3d 20 73 71 6c 69 74   *v;.  v = sqlit
17539 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
1753a 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20  );.  if( v ){.  
1753b 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1753c 4f 70 28 76 2c 20 4f 50 5f 52 65 61 64 43 6f 6f  Op(v, OP_ReadCoo
1753d 6b 69 65 2c 20 69 44 62 2c 20 31 29 3b 0a 20 20  kie, iDb, 1);.  
1753e 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1753f 4f 70 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  Op(v, OP_Integer
17540 2c 20 6d 69 6e 46 6f 72 6d 61 74 2c 20 30 29 3b  , minFormat, 0);
17541 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
17542 41 64 64 4f 70 28 76 2c 20 4f 50 5f 47 65 2c 20  AddOp(v, OP_Ge, 
17543 30 2c 20 73 71 6c 69 74 65 33 56 64 62 65 43 75  0, sqlite3VdbeCu
17544 72 72 65 6e 74 41 64 64 72 28 76 29 2b 33 29 3b  rrentAddr(v)+3);
17545 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
17546 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 74 65  AddOp(v, OP_Inte
17547 67 65 72 2c 20 6d 69 6e 46 6f 72 6d 61 74 2c 20  ger, minFormat, 
17548 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0);.    sqlite3V
17549 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 53  dbeAddOp(v, OP_S
1754a 65 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 31  etCookie, iDb, 1
1754b 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
1754c 46 69 6c 6c 20 74 68 65 20 49 6e 64 65 78 2e 61  Fill the Index.a
1754d 69 52 6f 77 45 73 74 5b 5d 20 61 72 72 61 79 20  iRowEst[] array 
1754e 77 69 74 68 20 64 65 66 61 75 6c 74 20 69 6e 66  with default inf
1754f 6f 72 6d 61 74 69 6f 6e 20 2d 20 69 6e 66 6f 72  ormation - infor
17550 6d 61 74 69 6f 6e 0a 2a 2a 20 74 6f 20 62 65 20  mation.** to be 
17551 75 73 65 64 20 77 68 65 6e 20 77 65 20 68 61 76  used when we hav
17552 65 20 6e 6f 74 20 72 75 6e 20 74 68 65 20 41 4e  e not run the AN
17553 41 4c 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a  ALYZE command..*
17554 2a 0a 2a 2a 20 61 69 52 6f 77 45 73 74 5b 30 5d  *.** aiRowEst[0]
17555 20 69 73 20 73 75 70 70 6f 73 65 20 74 6f 20 63   is suppose to c
17556 6f 6e 74 61 69 6e 20 74 68 65 20 6e 75 6d 62 65  ontain the numbe
17557 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e  r of elements in
17558 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a 2a 20 53   the index..** S
17559 69 6e 63 65 20 77 65 20 64 6f 20 6e 6f 74 20 6b  ince we do not k
1755a 6e 6f 77 2c 20 67 75 65 73 73 20 31 20 6d 69 6c  now, guess 1 mil
1755b 6c 69 6f 6e 2e 20 20 61 69 52 6f 77 45 73 74 5b  lion.  aiRowEst[
1755c 31 5d 20 69 73 20 61 6e 20 65 73 74 69 6d 61 74  1] is an estimat
1755d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 6e 75 6d 62  e of the.** numb
1755e 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68  er of rows in th
1755f 65 20 74 61 62 6c 65 20 74 68 61 74 20 6d 61 74  e table that mat
17560 63 68 20 61 6e 79 20 70 61 72 74 69 63 75 6c 61  ch any particula
17561 72 20 76 61 6c 75 65 20 6f 66 20 74 68 65 0a 2a  r value of the.*
17562 2a 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e 20 6f  * first column o
17563 66 20 74 68 65 20 69 6e 64 65 78 2e 20 20 61 69  f the index.  ai
17564 52 6f 77 45 73 74 5b 32 5d 20 69 73 20 61 6e 20  RowEst[2] is an 
17565 65 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65 20  estimate of the 
17566 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 72 6f 77  number.** of row
17567 73 20 74 68 61 74 20 6d 61 74 63 68 20 61 6e 79  s that match any
17568 20 70 61 72 74 69 63 75 6c 61 72 20 63 6f 6d 62   particular comb
17569 69 6e 69 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  iniation of the 
1756a 66 69 72 73 74 20 32 20 63 6f 6c 75 6d 6e 73 0a  first 2 columns.
1756b 2a 2a 20 6f 66 20 74 68 65 20 69 6e 64 65 78 2e  ** of the index.
1756c 20 20 41 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20    And so forth. 
1756d 20 49 74 20 6d 75 73 74 20 61 6c 77 61 79 73 20   It must always 
1756e 62 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74  be the case that
1756f 0a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  .*.**           
17570 61 69 52 6f 77 45 73 74 5b 4e 5d 3c 3d 61 69 52  aiRowEst[N]<=aiR
17571 6f 77 45 73 74 5b 4e 2d 31 5d 0a 2a 2a 20 20 20  owEst[N-1].**   
17572 20 20 20 20 20 20 20 20 61 69 52 6f 77 45 73 74          aiRowEst
17573 5b 4e 5d 3e 3d 31 0a 2a 2a 0a 2a 2a 20 41 70 61  [N]>=1.**.** Apa
17574 72 74 20 66 72 6f 6d 20 74 68 61 74 2c 20 77 65  rt from that, we
17575 20 68 61 76 65 20 6c 69 74 74 6c 65 20 74 6f 20   have little to 
17576 67 6f 20 6f 6e 20 62 65 73 69 64 65 73 20 69 6e  go on besides in
17577 74 75 69 74 69 6f 6e 20 61 73 20 74 6f 0a 2a 2a  tuition as to.**
17578 20 68 6f 77 20 61 69 52 6f 77 45 73 74 5b 5d 20   how aiRowEst[] 
17579 73 68 6f 75 6c 64 20 62 65 20 69 6e 69 74 69 61  should be initia
1757a 6c 69 7a 65 64 2e 20 20 54 68 65 20 6e 75 6d 62  lized.  The numb
1757b 65 72 73 20 67 65 6e 65 72 61 74 65 64 20 68 65  ers generated he
1757c 72 65 0a 2a 2a 20 61 72 65 20 62 61 73 65 64 20  re.** are based 
1757d 6f 6e 20 74 79 70 69 63 61 6c 20 76 61 6c 75 65  on typical value
1757e 73 20 66 6f 75 6e 64 20 69 6e 20 61 63 74 75 61  s found in actua
1757f 6c 20 69 6e 64 69 63 65 73 2e 0a 2a 2f 0a 53 51  l indices..*/.SQ
17580 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
17581 64 20 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74  d sqlite3Default
17582 52 6f 77 45 73 74 28 49 6e 64 65 78 20 2a 70 49  RowEst(Index *pI
17583 64 78 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20  dx){.  unsigned 
17584 2a 61 20 3d 20 70 49 64 78 2d 3e 61 69 52 6f 77  *a = pIdx->aiRow
17585 45 73 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  Est;.  int i;.  
17586 61 73 73 65 72 74 28 20 61 21 3d 30 20 29 3b 0a  assert( a!=0 );.
17587 20 20 61 5b 30 5d 20 3d 20 31 30 30 30 30 30 30    a[0] = 1000000
17588 3b 0a 20 20 66 6f 72 28 69 3d 70 49 64 78 2d 3e  ;.  for(i=pIdx->
17589 6e 43 6f 6c 75 6d 6e 3b 20 69 3e 3d 35 3b 20 69  nColumn; i>=5; i
1758a 2d 2d 29 7b 0a 20 20 20 20 61 5b 69 5d 20 3d 20  --){.    a[i] = 
1758b 35 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20  5;.  }.  while( 
1758c 69 3e 3d 31 20 29 7b 0a 20 20 20 20 61 5b 69 5d  i>=1 ){.    a[i]
1758d 20 3d 20 31 31 20 2d 20 69 3b 0a 20 20 20 20 69   = 11 - i;.    i
1758e 2d 2d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 49  --;.  }.  if( pI
1758f 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f  dx->onError!=OE_
17590 4e 6f 6e 65 20 29 7b 0a 20 20 20 20 61 5b 70 49  None ){.    a[pI
17591 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 5d 20 3d 20 31  dx->nColumn] = 1
17592 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
17593 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c  his routine will
17594 20 64 72 6f 70 20 61 6e 20 65 78 69 73 74 69 6e   drop an existin
17595 67 20 6e 61 6d 65 64 20 69 6e 64 65 78 2e 20 20  g named index.  
17596 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  This routine.** 
17597 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 44  implements the D
17598 52 4f 50 20 49 4e 44 45 58 20 73 74 61 74 65 6d  ROP INDEX statem
17599 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ent..*/.SQLITE_P
1759a 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1759b 74 65 33 44 72 6f 70 49 6e 64 65 78 28 50 61 72  te3DropIndex(Par
1759c 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72 63 4c  se *pParse, SrcL
1759d 69 73 74 20 2a 70 4e 61 6d 65 2c 20 69 6e 74 20  ist *pName, int 
1759e 69 66 45 78 69 73 74 73 29 7b 0a 20 20 49 6e 64  ifExists){.  Ind
1759f 65 78 20 2a 70 49 6e 64 65 78 3b 0a 20 20 56 64  ex *pIndex;.  Vd
175a0 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33  be *v;.  sqlite3
175a1 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
175a2 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 0a 20  b;.  int iDb;.. 
175a3 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72   if( pParse->nEr
175a4 72 20 7c 7c 20 73 71 6c 69 74 65 33 4d 61 6c 6c  r || sqlite3Mall
175a5 6f 63 46 61 69 6c 65 64 28 29 20 29 7b 0a 20 20  ocFailed() ){.  
175a6 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70    goto exit_drop
175a7 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 61 73  _index;.  }.  as
175a8 73 65 72 74 28 20 70 4e 61 6d 65 2d 3e 6e 53 72  sert( pName->nSr
175a9 63 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 53 51  c==1 );.  if( SQ
175aa 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33  LITE_OK!=sqlite3
175ab 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73  ReadSchema(pPars
175ac 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65  e) ){.    goto e
175ad 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b 0a  xit_drop_index;.
175ae 20 20 7d 0a 20 20 70 49 6e 64 65 78 20 3d 20 73    }.  pIndex = s
175af 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28  qlite3FindIndex(
175b0 64 62 2c 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e  db, pName->a[0].
175b1 7a 4e 61 6d 65 2c 20 70 4e 61 6d 65 2d 3e 61 5b  zName, pName->a[
175b2 30 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20  0].zDatabase);. 
175b3 20 69 66 28 20 70 49 6e 64 65 78 3d 3d 30 20 29   if( pIndex==0 )
175b4 7b 0a 20 20 20 20 69 66 28 20 21 69 66 45 78 69  {.    if( !ifExi
175b5 73 74 73 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  sts ){.      sql
175b6 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
175b7 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 69 6e  rse, "no such in
175b8 64 65 78 3a 20 25 53 22 2c 20 70 4e 61 6d 65 2c  dex: %S", pName,
175b9 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70   0);.    }.    p
175ba 50 61 72 73 65 2d 3e 63 68 65 63 6b 53 63 68 65  Parse->checkSche
175bb 6d 61 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74 6f  ma = 1;.    goto
175bc 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78   exit_drop_index
175bd 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 6e 64  ;.  }.  if( pInd
175be 65 78 2d 3e 61 75 74 6f 49 6e 64 65 78 20 29 7b  ex->autoIndex ){
175bf 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
175c0 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 69 6e  rMsg(pParse, "in
175c1 64 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77  dex associated w
175c2 69 74 68 20 55 4e 49 51 55 45 20 22 0a 20 20 20  ith UNIQUE ".   
175c3 20 20 20 22 6f 72 20 50 52 49 4d 41 52 59 20 4b     "or PRIMARY K
175c4 45 59 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 61  EY constraint ca
175c5 6e 6e 6f 74 20 62 65 20 64 72 6f 70 70 65 64 22  nnot be dropped"
175c6 2c 20 30 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65  , 0);.    goto e
175c7 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b 0a  xit_drop_index;.
175c8 20 20 7d 0a 20 20 69 44 62 20 3d 20 73 71 6c 69    }.  iDb = sqli
175c9 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
175ca 28 64 62 2c 20 70 49 6e 64 65 78 2d 3e 70 53 63  (db, pIndex->pSc
175cb 68 65 6d 61 29 3b 0a 23 69 66 6e 64 65 66 20 53  hema);.#ifndef S
175cc 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
175cd 52 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20  RIZATION.  {.   
175ce 20 69 6e 74 20 63 6f 64 65 20 3d 20 53 51 4c 49   int code = SQLI
175cf 54 45 5f 44 52 4f 50 5f 49 4e 44 45 58 3b 0a 20  TE_DROP_INDEX;. 
175d0 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d     Table *pTab =
175d1 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 3b   pIndex->pTable;
175d2 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
175d3 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69  *zDb = db->aDb[i
175d4 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63  Db].zName;.    c
175d5 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 20  onst char *zTab 
175d6 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69  = SCHEMA_TABLE(i
175d7 44 62 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  Db);.    if( sql
175d8 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
175d9 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c  arse, SQLITE_DEL
175da 45 54 45 2c 20 7a 54 61 62 2c 20 30 2c 20 7a 44  ETE, zTab, 0, zD
175db 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  b) ){.      goto
175dc 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78   exit_drop_index
175dd 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
175de 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20  !OMIT_TEMPDB && 
175df 69 44 62 20 29 20 63 6f 64 65 20 3d 20 53 51 4c  iDb ) code = SQL
175e0 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 49 4e  ITE_DROP_TEMP_IN
175e1 44 45 58 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  DEX;.    if( sql
175e2 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
175e3 61 72 73 65 2c 20 63 6f 64 65 2c 20 70 49 6e 64  arse, code, pInd
175e4 65 78 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d  ex->zName, pTab-
175e5 3e 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 29 7b 0a  >zName, zDb) ){.
175e6 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f        goto exit_
175e7 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 20 20  drop_index;.    
175e8 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  }.  }.#endif..  
175e9 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
175ea 20 74 6f 20 72 65 6d 6f 76 65 20 74 68 65 20 69   to remove the i
175eb 6e 64 65 78 20 61 6e 64 20 66 72 6f 6d 20 74 68  ndex and from th
175ec 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 20 2a  e master table *
175ed 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  /.  v = sqlite3G
175ee 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
175ef 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 73    if( v ){.    s
175f0 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73  qlite3NestedPars
175f1 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20  e(pParse,.      
175f2 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 51   "DELETE FROM %Q
175f3 2e 25 73 20 57 48 45 52 45 20 6e 61 6d 65 3d 25  .%s WHERE name=%
175f4 51 22 2c 0a 20 20 20 20 20 20 20 64 62 2d 3e 61  Q",.       db->a
175f5 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53  Db[iDb].zName, S
175f6 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29  CHEMA_TABLE(iDb)
175f7 2c 0a 20 20 20 20 20 20 20 70 49 6e 64 65 78 2d  ,.       pIndex-
175f8 3e 7a 4e 61 6d 65 0a 20 20 20 20 29 3b 0a 20 20  >zName.    );.  
175f9 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43    sqlite3ChangeC
175fa 6f 6f 6b 69 65 28 64 62 2c 20 76 2c 20 69 44 62  ookie(db, v, iDb
175fb 29 3b 0a 20 20 20 20 64 65 73 74 72 6f 79 52 6f  );.    destroyRo
175fc 6f 74 50 61 67 65 28 70 50 61 72 73 65 2c 20 70  otPage(pParse, p
175fd 49 6e 64 65 78 2d 3e 74 6e 75 6d 2c 20 69 44 62  Index->tnum, iDb
175fe 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
175ff 62 65 4f 70 33 28 76 2c 20 4f 50 5f 44 72 6f 70  beOp3(v, OP_Drop
17600 49 6e 64 65 78 2c 20 69 44 62 2c 20 30 2c 20 70  Index, iDb, 0, p
17601 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 30 29  Index->zName, 0)
17602 3b 0a 20 20 7d 0a 0a 65 78 69 74 5f 64 72 6f 70  ;.  }..exit_drop
17603 5f 69 6e 64 65 78 3a 0a 20 20 73 71 6c 69 74 65  _index:.  sqlite
17604 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 70  3SrcListDelete(p
17605 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Name);.}../*.** 
17606 70 41 72 72 61 79 20 69 73 20 61 20 70 6f 69 6e  pArray is a poin
17607 74 65 72 20 74 6f 20 61 6e 20 61 72 72 61 79 20  ter to an array 
17608 6f 66 20 6f 62 6a 65 63 74 73 2e 20 20 45 61 63  of objects.  Eac
17609 68 20 6f 62 6a 65 63 74 20 69 6e 20 74 68 65 0a  h object in the.
1760a 2a 2a 20 61 72 72 61 79 20 69 73 20 73 7a 45 6e  ** array is szEn
1760b 74 72 79 20 62 79 74 65 73 20 69 6e 20 73 69 7a  try bytes in siz
1760c 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  e.  This routine
1760d 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 77   allocates a new
1760e 0a 2a 2a 20 6f 62 6a 65 63 74 20 6f 6e 20 74 68  .** object on th
1760f 65 20 65 6e 64 20 6f 66 20 74 68 65 20 61 72 72  e end of the arr
17610 61 79 2e 0a 2a 2a 0a 2a 2a 20 2a 70 6e 45 6e 74  ay..**.** *pnEnt
17611 72 79 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  ry is the number
17612 20 6f 66 20 65 6e 74 72 69 65 73 20 61 6c 72 65   of entries alre
17613 61 64 79 20 69 6e 20 75 73 65 2e 20 20 2a 70 6e  ady in use.  *pn
17614 41 6c 6c 6f 63 20 69 73 0a 2a 2a 20 74 68 65 20  Alloc is.** the 
17615 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63  previously alloc
17616 61 74 65 64 20 73 69 7a 65 20 6f 66 20 74 68 65  ated size of the
17617 20 61 72 72 61 79 2e 20 20 69 6e 69 74 53 69 7a   array.  initSiz
17618 65 20 69 73 20 74 68 65 0a 2a 2a 20 73 75 67 67  e is the.** sugg
17619 65 73 74 65 64 20 69 6e 69 74 69 61 6c 20 61 72  ested initial ar
1761a 72 61 79 20 73 69 7a 65 20 61 6c 6c 6f 63 61 74  ray size allocat
1761b 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69  ion..**.** The i
1761c 6e 64 65 78 20 6f 66 20 74 68 65 20 6e 65 77 20  ndex of the new 
1761d 65 6e 74 72 79 20 69 73 20 72 65 74 75 72 6e 65  entry is returne
1761e 64 20 69 6e 20 2a 70 49 64 78 2e 0a 2a 2a 0a 2a  d in *pIdx..**.*
1761f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72  * This routine r
17620 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
17621 20 74 6f 20 74 68 65 20 61 72 72 61 79 20 6f 66   to the array of
17622 20 6f 62 6a 65 63 74 73 2e 20 20 54 68 69 73 0a   objects.  This.
17623 2a 2a 20 6d 69 67 68 74 20 62 65 20 74 68 65 20  ** might be the 
17624 73 61 6d 65 20 61 73 20 74 68 65 20 70 41 72 72  same as the pArr
17625 61 79 20 70 61 72 61 6d 65 74 65 72 20 6f 72 20  ay parameter or 
17626 69 74 20 6d 69 67 68 74 20 62 65 20 61 20 64 69  it might be a di
17627 66 66 65 72 65 6e 74 0a 2a 2a 20 70 6f 69 6e 74  fferent.** point
17628 65 72 20 69 66 20 74 68 65 20 61 72 72 61 79 20  er if the array 
17629 77 61 73 20 72 65 73 69 7a 65 64 2e 0a 2a 2f 0a  was resized..*/.
1762a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1762b 6f 69 64 20 2a 73 71 6c 69 74 65 33 41 72 72 61  oid *sqlite3Arra
1762c 79 41 6c 6c 6f 63 61 74 65 28 0a 20 20 76 6f 69  yAllocate(.  voi
1762d 64 20 2a 70 41 72 72 61 79 2c 20 20 20 20 20 2f  d *pArray,     /
1762e 2a 20 41 72 72 61 79 20 6f 66 20 6f 62 6a 65 63  * Array of objec
1762f 74 73 2e 20 20 4d 69 67 68 74 20 62 65 20 72 65  ts.  Might be re
17630 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 69  allocated */.  i
17631 6e 74 20 73 7a 45 6e 74 72 79 2c 20 20 20 20 20  nt szEntry,     
17632 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68   /* Size of each
17633 20 6f 62 6a 65 63 74 20 69 6e 20 74 68 65 20 61   object in the a
17634 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 69 6e  rray */.  int in
17635 69 74 53 69 7a 65 2c 20 20 20 20 20 2f 2a 20 53  itSize,     /* S
17636 75 67 67 65 73 74 65 64 20 69 6e 69 74 69 61 6c  uggested initial
17637 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e 20   allocation, in 
17638 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 69 6e  elements */.  in
17639 74 20 2a 70 6e 45 6e 74 72 79 2c 20 20 20 20 20  t *pnEntry,     
1763a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 62 6a  /* Number of obj
1763b 65 63 74 73 20 63 75 72 72 65 6e 74 6c 79 20 69  ects currently i
1763c 6e 20 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 2a  n use */.  int *
1763d 70 6e 41 6c 6c 6f 63 2c 20 20 20 20 20 2f 2a 20  pnAlloc,     /* 
1763e 43 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20  Current size of 
1763f 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20  the allocation, 
17640 69 6e 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20  in elements */. 
17641 20 69 6e 74 20 2a 70 49 64 78 20 20 20 20 20 20   int *pIdx      
17642 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
17643 69 6e 64 65 78 20 6f 66 20 61 20 6e 65 77 20 73  index of a new s
17644 6c 6f 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  lot here */.){. 
17645 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69 66 28 20   char *z;.  if( 
17646 2a 70 6e 45 6e 74 72 79 20 3e 3d 20 2a 70 6e 41  *pnEntry >= *pnA
17647 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 76 6f 69 64  lloc ){.    void
17648 20 2a 70 4e 65 77 3b 0a 20 20 20 20 69 6e 74 20   *pNew;.    int 
17649 6e 65 77 53 69 7a 65 3b 0a 20 20 20 20 6e 65 77  newSize;.    new
1764a 53 69 7a 65 20 3d 20 28 2a 70 6e 41 6c 6c 6f 63  Size = (*pnAlloc
1764b 29 2a 32 20 2b 20 69 6e 69 74 53 69 7a 65 3b 0a  )*2 + initSize;.
1764c 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74      pNew = sqlit
1764d 65 52 65 61 6c 6c 6f 63 28 70 41 72 72 61 79 2c  eRealloc(pArray,
1764e 20 6e 65 77 53 69 7a 65 2a 73 7a 45 6e 74 72 79   newSize*szEntry
1764f 29 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77 3d  );.    if( pNew=
17650 3d 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 49 64  =0 ){.      *pId
17651 78 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 72 65  x = -1;.      re
17652 74 75 72 6e 20 70 41 72 72 61 79 3b 0a 20 20 20  turn pArray;.   
17653 20 7d 0a 20 20 20 20 2a 70 6e 41 6c 6c 6f 63 20   }.    *pnAlloc 
17654 3d 20 6e 65 77 53 69 7a 65 3b 0a 20 20 20 20 70  = newSize;.    p
17655 41 72 72 61 79 20 3d 20 70 4e 65 77 3b 0a 20 20  Array = pNew;.  
17656 7d 0a 20 20 7a 20 3d 20 28 63 68 61 72 2a 29 70  }.  z = (char*)p
17657 41 72 72 61 79 3b 0a 20 20 6d 65 6d 73 65 74 28  Array;.  memset(
17658 26 7a 5b 2a 70 6e 45 6e 74 72 79 20 2a 20 73 7a  &z[*pnEntry * sz
17659 45 6e 74 72 79 5d 2c 20 30 2c 20 73 7a 45 6e 74  Entry], 0, szEnt
1765a 72 79 29 3b 0a 20 20 2a 70 49 64 78 20 3d 20 2a  ry);.  *pIdx = *
1765b 70 6e 45 6e 74 72 79 3b 0a 20 20 2b 2b 2a 70 6e  pnEntry;.  ++*pn
1765c 45 6e 74 72 79 3b 0a 20 20 72 65 74 75 72 6e 20  Entry;.  return 
1765d 70 41 72 72 61 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pArray;.}../*.**
1765e 20 41 70 70 65 6e 64 20 61 20 6e 65 77 20 65 6c   Append a new el
1765f 65 6d 65 6e 74 20 74 6f 20 74 68 65 20 67 69 76  ement to the giv
17660 65 6e 20 49 64 4c 69 73 74 2e 20 20 43 72 65 61  en IdList.  Crea
17661 74 65 20 61 20 6e 65 77 20 49 64 4c 69 73 74 20  te a new IdList 
17662 69 66 0a 2a 2a 20 6e 65 65 64 20 62 65 2e 0a 2a  if.** need be..*
17663 2a 0a 2a 2a 20 41 20 6e 65 77 20 49 64 4c 69 73  *.** A new IdLis
17664 74 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 6f  t is returned, o
17665 72 20 4e 55 4c 4c 20 69 66 20 6d 61 6c 6c 6f 63  r NULL if malloc
17666 28 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c  () fails..*/.SQL
17667 49 54 45 5f 50 52 49 56 41 54 45 20 49 64 4c 69  ITE_PRIVATE IdLi
17668 73 74 20 2a 73 71 6c 69 74 65 33 49 64 4c 69 73  st *sqlite3IdLis
17669 74 41 70 70 65 6e 64 28 49 64 4c 69 73 74 20 2a  tAppend(IdList *
1766a 70 4c 69 73 74 2c 20 54 6f 6b 65 6e 20 2a 70 54  pList, Token *pT
1766b 6f 6b 65 6e 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  oken){.  int i;.
1766c 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29    if( pList==0 )
1766d 7b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 73 71  {.    pList = sq
1766e 6c 69 74 65 4d 61 6c 6c 6f 63 28 20 73 69 7a 65  liteMalloc( size
1766f 6f 66 28 49 64 4c 69 73 74 29 20 29 3b 0a 20 20  of(IdList) );.  
17670 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29    if( pList==0 )
17671 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 70   return 0;.    p
17672 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 30  List->nAlloc = 0
17673 3b 0a 20 20 7d 0a 20 20 70 4c 69 73 74 2d 3e 61  ;.  }.  pList->a
17674 20 3d 20 73 71 6c 69 74 65 33 41 72 72 61 79 41   = sqlite3ArrayA
17675 6c 6c 6f 63 61 74 65 28 0a 20 20 20 20 20 20 70  llocate(.      p
17676 4c 69 73 74 2d 3e 61 2c 0a 20 20 20 20 20 20 73  List->a,.      s
17677 69 7a 65 6f 66 28 70 4c 69 73 74 2d 3e 61 5b 30  izeof(pList->a[0
17678 5d 29 2c 0a 20 20 20 20 20 20 35 2c 0a 20 20 20  ]),.      5,.   
17679 20 20 20 26 70 4c 69 73 74 2d 3e 6e 49 64 2c 0a     &pList->nId,.
1767a 20 20 20 20 20 20 26 70 4c 69 73 74 2d 3e 6e 41        &pList->nA
1767b 6c 6c 6f 63 2c 0a 20 20 20 20 20 20 26 69 0a 20  lloc,.      &i. 
1767c 20 29 3b 0a 20 20 69 66 28 20 69 3c 30 20 29 7b   );.  if( i<0 ){
1767d 0a 20 20 20 20 73 71 6c 69 74 65 33 49 64 4c 69  .    sqlite3IdLi
1767e 73 74 44 65 6c 65 74 65 28 70 4c 69 73 74 29 3b  stDelete(pList);
1767f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
17680 20 7d 0a 20 20 70 4c 69 73 74 2d 3e 61 5b 69 5d   }.  pList->a[i]
17681 2e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33  .zName = sqlite3
17682 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 70 54  NameFromToken(pT
17683 6f 6b 65 6e 29 3b 0a 20 20 72 65 74 75 72 6e 20  oken);.  return 
17684 70 4c 69 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  pList;.}../*.** 
17685 44 65 6c 65 74 65 20 61 6e 20 49 64 4c 69 73 74  Delete an IdList
17686 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
17687 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
17688 49 64 4c 69 73 74 44 65 6c 65 74 65 28 49 64 4c  IdListDelete(IdL
17689 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69  ist *pList){.  i
1768a 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 4c 69 73  nt i;.  if( pLis
1768b 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  t==0 ) return;. 
1768c 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73   for(i=0; i<pLis
1768d 74 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 20  t->nId; i++){.  
1768e 20 20 73 71 6c 69 74 65 46 72 65 65 28 70 4c 69    sqliteFree(pLi
1768f 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b  st->a[i].zName);
17690 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 46 72 65  .  }.  sqliteFre
17691 65 28 70 4c 69 73 74 2d 3e 61 29 3b 0a 20 20 73  e(pList->a);.  s
17692 71 6c 69 74 65 46 72 65 65 28 70 4c 69 73 74 29  qliteFree(pList)
17693 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
17694 6e 20 74 68 65 20 69 6e 64 65 78 20 69 6e 20 70  n the index in p
17695 4c 69 73 74 20 6f 66 20 74 68 65 20 69 64 65 6e  List of the iden
17696 74 69 66 69 65 72 20 6e 61 6d 65 64 20 7a 49 64  tifier named zId
17697 2e 20 20 52 65 74 75 72 6e 20 2d 31 0a 2a 2a 20  .  Return -1.** 
17698 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2f  if not found..*/
17699 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1769a 69 6e 74 20 73 71 6c 69 74 65 33 49 64 4c 69 73  int sqlite3IdLis
1769b 74 49 6e 64 65 78 28 49 64 4c 69 73 74 20 2a 70  tIndex(IdList *p
1769c 4c 69 73 74 2c 20 63 6f 6e 73 74 20 63 68 61 72  List, const char
1769d 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 69 6e 74 20   *zName){.  int 
1769e 69 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d  i;.  if( pList==
1769f 30 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20  0 ) return -1;. 
176a0 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73   for(i=0; i<pLis
176a1 74 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 20  t->nId; i++){.  
176a2 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
176a3 49 43 6d 70 28 70 4c 69 73 74 2d 3e 61 5b 69 5d  ICmp(pList->a[i]
176a4 2e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 3d 3d  .zName, zName)==
176a5 30 20 29 20 72 65 74 75 72 6e 20 69 3b 0a 20 20  0 ) return i;.  
176a6 7d 0a 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 7d  }.  return -1;.}
176a7 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61  ../*.** Append a
176a8 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 20   new table name 
176a9 74 6f 20 74 68 65 20 67 69 76 65 6e 20 53 72 63  to the given Src
176aa 4c 69 73 74 2e 20 20 43 72 65 61 74 65 20 61 20  List.  Create a 
176ab 6e 65 77 20 53 72 63 4c 69 73 74 20 69 66 0a 2a  new SrcList if.*
176ac 2a 20 6e 65 65 64 20 62 65 2e 20 20 41 20 6e 65  * need be.  A ne
176ad 77 20 65 6e 74 72 79 20 69 73 20 63 72 65 61 74  w entry is creat
176ae 65 64 20 69 6e 20 74 68 65 20 53 72 63 4c 69 73  ed in the SrcLis
176af 74 20 65 76 65 6e 20 69 66 20 70 54 6f 6b 65 6e  t even if pToken
176b0 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20   is NULL..**.** 
176b1 41 20 6e 65 77 20 53 72 63 4c 69 73 74 20 69 73  A new SrcList is
176b2 20 72 65 74 75 72 6e 65 64 2c 20 6f 72 20 4e 55   returned, or NU
176b3 4c 4c 20 69 66 20 6d 61 6c 6c 6f 63 28 29 20 66  LL if malloc() f
176b4 61 69 6c 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70  ails..**.** If p
176b5 44 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 20  Database is not 
176b6 6e 75 6c 6c 2c 20 69 74 20 6d 65 61 6e 73 20 74  null, it means t
176b7 68 61 74 20 74 68 65 20 74 61 62 6c 65 20 68 61  hat the table ha
176b8 73 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 0a 2a 2a  s an optional.**
176b9 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 70   database name p
176ba 72 65 66 69 78 2e 20 20 4c 69 6b 65 20 74 68 69  refix.  Like thi
176bb 73 3a 20 20 22 64 61 74 61 62 61 73 65 2e 74 61  s:  "database.ta
176bc 62 6c 65 22 2e 20 20 54 68 65 20 70 44 61 74 61  ble".  The pData
176bd 62 61 73 65 0a 2a 2a 20 70 6f 69 6e 74 73 20 74  base.** points t
176be 6f 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65  o the table name
176bf 20 61 6e 64 20 74 68 65 20 70 54 61 62 6c 65 20   and the pTable 
176c0 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 64 61  points to the da
176c1 74 61 62 61 73 65 20 6e 61 6d 65 2e 0a 2a 2a 20  tabase name..** 
176c2 54 68 65 20 53 72 63 4c 69 73 74 2e 61 5b 5d 2e  The SrcList.a[].
176c3 7a 4e 61 6d 65 20 66 69 65 6c 64 20 69 73 20 66  zName field is f
176c4 69 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 74  illed with the t
176c5 61 62 6c 65 20 6e 61 6d 65 20 77 68 69 63 68 20  able name which 
176c6 6d 69 67 68 74 0a 2a 2a 20 63 6f 6d 65 20 66 72  might.** come fr
176c7 6f 6d 20 70 54 61 62 6c 65 20 28 69 66 20 70 44  om pTable (if pD
176c8 61 74 61 62 61 73 65 20 69 73 20 4e 55 4c 4c 29  atabase is NULL)
176c9 20 6f 72 20 66 72 6f 6d 20 70 44 61 74 61 62 61   or from pDataba
176ca 73 65 2e 20 20 0a 2a 2a 20 53 72 63 4c 69 73 74  se.  .** SrcList
176cb 2e 61 5b 5d 2e 7a 44 61 74 61 62 61 73 65 20 69  .a[].zDatabase i
176cc 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20 74 68  s filled with th
176cd 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  e database name 
176ce 66 72 6f 6d 20 70 54 61 62 6c 65 2c 0a 2a 2a 20  from pTable,.** 
176cf 6f 72 20 77 69 74 68 20 4e 55 4c 4c 20 69 66 20  or with NULL if 
176d0 6e 6f 20 64 61 74 61 62 61 73 65 20 69 73 20 73  no database is s
176d1 70 65 63 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20  pecified..**.** 
176d2 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
176d3 69 66 20 63 61 6c 6c 20 6c 69 6b 65 20 74 68 69  if call like thi
176d4 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  s:.**.**        
176d5 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41   sqlite3SrcListA
176d6 70 70 65 6e 64 28 41 2c 42 2c 30 29 3b 0a 2a 2a  ppend(A,B,0);.**
176d7 0a 2a 2a 20 54 68 65 6e 20 42 20 69 73 20 61 20  .** Then B is a 
176d8 74 61 62 6c 65 20 6e 61 6d 65 20 61 6e 64 20 74  table name and t
176d9 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  he database name
176da 20 69 73 20 75 6e 73 70 65 63 69 66 69 65 64 2e   is unspecified.
176db 20 20 49 66 20 63 61 6c 6c 65 64 0a 2a 2a 20 6c    If called.** l
176dc 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20  ike this:.**.** 
176dd 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
176de 72 63 4c 69 73 74 41 70 70 65 6e 64 28 41 2c 42  rcListAppend(A,B
176df 2c 43 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20  ,C);.**.** Then 
176e0 43 20 69 73 20 74 68 65 20 74 61 62 6c 65 20 6e  C is the table n
176e1 61 6d 65 20 61 6e 64 20 42 20 69 73 20 74 68 65  ame and B is the
176e2 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 2e 0a   database name..
176e3 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
176e4 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74  E SrcList *sqlit
176e5 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28  e3SrcListAppend(
176e6 53 72 63 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20  SrcList *pList, 
176e7 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 2c 20 54  Token *pTable, T
176e8 6f 6b 65 6e 20 2a 70 44 61 74 61 62 61 73 65 29  oken *pDatabase)
176e9 7b 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69  {.  struct SrcLi
176ea 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a  st_item *pItem;.
176eb 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29    if( pList==0 )
176ec 7b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 73 71  {.    pList = sq
176ed 6c 69 74 65 4d 61 6c 6c 6f 63 28 20 73 69 7a 65  liteMalloc( size
176ee 6f 66 28 53 72 63 4c 69 73 74 29 20 29 3b 0a 20  of(SrcList) );. 
176ef 20 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20     if( pList==0 
176f0 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20  ) return 0;.    
176f1 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20 3d 20  pList->nAlloc = 
176f2 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4c 69  1;.  }.  if( pLi
176f3 73 74 2d 3e 6e 53 72 63 3e 3d 70 4c 69 73 74 2d  st->nSrc>=pList-
176f4 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 53  >nAlloc ){.    S
176f5 72 63 4c 69 73 74 20 2a 70 4e 65 77 3b 0a 20 20  rcList *pNew;.  
176f6 20 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20    pList->nAlloc 
176f7 2a 3d 20 32 3b 0a 20 20 20 20 70 4e 65 77 20 3d  *= 2;.    pNew =
176f8 20 73 71 6c 69 74 65 52 65 61 6c 6c 6f 63 28 70   sqliteRealloc(p
176f9 4c 69 73 74 2c 0a 20 20 20 20 20 20 20 20 20 20  List,.          
176fa 20 20 20 20 20 73 69 7a 65 6f 66 28 2a 70 4c 69       sizeof(*pLi
176fb 73 74 29 20 2b 20 28 70 4c 69 73 74 2d 3e 6e 41  st) + (pList->nA
176fc 6c 6c 6f 63 2d 31 29 2a 73 69 7a 65 6f 66 28 70  lloc-1)*sizeof(p
176fd 4c 69 73 74 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20  List->a[0]) );. 
176fe 20 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29     if( pNew==0 )
176ff 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53  {.      sqlite3S
17700 72 63 4c 69 73 74 44 65 6c 65 74 65 28 70 4c 69  rcListDelete(pLi
17701 73 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  st);.      retur
17702 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  n 0;.    }.    p
17703 4c 69 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 7d  List = pNew;.  }
17704 0a 20 20 70 49 74 65 6d 20 3d 20 26 70 4c 69 73  .  pItem = &pLis
17705 74 2d 3e 61 5b 70 4c 69 73 74 2d 3e 6e 53 72 63  t->a[pList->nSrc
17706 5d 3b 0a 20 20 6d 65 6d 73 65 74 28 70 49 74 65  ];.  memset(pIte
17707 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 4c 69  m, 0, sizeof(pLi
17708 73 74 2d 3e 61 5b 30 5d 29 29 3b 0a 20 20 69 66  st->a[0]));.  if
17709 28 20 70 44 61 74 61 62 61 73 65 20 26 26 20 70  ( pDatabase && p
1770a 44 61 74 61 62 61 73 65 2d 3e 7a 3d 3d 30 20 29  Database->z==0 )
1770b 7b 0a 20 20 20 20 70 44 61 74 61 62 61 73 65 20  {.    pDatabase 
1770c 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  = 0;.  }.  if( p
1770d 44 61 74 61 62 61 73 65 20 26 26 20 70 54 61 62  Database && pTab
1770e 6c 65 20 29 7b 0a 20 20 20 20 54 6f 6b 65 6e 20  le ){.    Token 
1770f 2a 70 54 65 6d 70 20 3d 20 70 44 61 74 61 62 61  *pTemp = pDataba
17710 73 65 3b 0a 20 20 20 20 70 44 61 74 61 62 61 73  se;.    pDatabas
17711 65 20 3d 20 70 54 61 62 6c 65 3b 0a 20 20 20 20  e = pTable;.    
17712 70 54 61 62 6c 65 20 3d 20 70 54 65 6d 70 3b 0a  pTable = pTemp;.
17713 20 20 7d 0a 20 20 70 49 74 65 6d 2d 3e 7a 4e 61    }.  pItem->zNa
17714 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65  me = sqlite3Name
17715 46 72 6f 6d 54 6f 6b 65 6e 28 70 54 61 62 6c 65  FromToken(pTable
17716 29 3b 0a 20 20 70 49 74 65 6d 2d 3e 7a 44 61 74  );.  pItem->zDat
17717 61 62 61 73 65 20 3d 20 73 71 6c 69 74 65 33 4e  abase = sqlite3N
17718 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 70 44 61  ameFromToken(pDa
17719 74 61 62 61 73 65 29 3b 0a 20 20 70 49 74 65 6d  tabase);.  pItem
1771a 2d 3e 69 43 75 72 73 6f 72 20 3d 20 2d 31 3b 0a  ->iCursor = -1;.
1771b 20 20 70 49 74 65 6d 2d 3e 69 73 50 6f 70 75 6c    pItem->isPopul
1771c 61 74 65 64 20 3d 20 30 3b 0a 20 20 70 4c 69 73  ated = 0;.  pLis
1771d 74 2d 3e 6e 53 72 63 2b 2b 3b 0a 20 20 72 65 74  t->nSrc++;.  ret
1771e 75 72 6e 20 70 4c 69 73 74 3b 0a 7d 0a 0a 2f 2a  urn pList;.}../*
1771f 0a 2a 2a 20 41 73 73 69 67 6e 20 63 75 72 73 6f  .** Assign curso
17720 72 73 20 74 6f 20 61 6c 6c 20 74 61 62 6c 65 73  rs to all tables
17721 20 69 6e 20 61 20 53 72 63 4c 69 73 74 0a 2a 2f   in a SrcList.*/
17722 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
17723 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c  void sqlite3SrcL
17724 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f 72 73  istAssignCursors
17725 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
17726 53 72 63 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b  SrcList *pList){
17727 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75  .  int i;.  stru
17728 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
17729 2a 70 49 74 65 6d 3b 0a 20 20 61 73 73 65 72 74  *pItem;.  assert
1772a 28 70 4c 69 73 74 20 7c 7c 20 73 71 6c 69 74 65  (pList || sqlite
1772b 33 4d 61 6c 6c 6f 63 46 61 69 6c 65 64 28 29 20  3MallocFailed() 
1772c 29 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 20 29  );.  if( pList )
1772d 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70  {.    for(i=0, p
1772e 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69  Item=pList->a; i
1772f 3c 70 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b  <pList->nSrc; i+
17730 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20  +, pItem++){.   
17731 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69 43     if( pItem->iC
17732 75 72 73 6f 72 3e 3d 30 20 29 20 62 72 65 61 6b  ursor>=0 ) break
17733 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69  ;.      pItem->i
17734 43 75 72 73 6f 72 20 3d 20 70 50 61 72 73 65 2d  Cursor = pParse-
17735 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 69  >nTab++;.      i
17736 66 28 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63  f( pItem->pSelec
17737 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  t ){.        sql
17738 69 74 65 33 53 72 63 4c 69 73 74 41 73 73 69 67  ite3SrcListAssig
17739 6e 43 75 72 73 6f 72 73 28 70 50 61 72 73 65 2c  nCursors(pParse,
1773a 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 2d   pItem->pSelect-
1773b 3e 70 53 72 63 29 3b 0a 20 20 20 20 20 20 7d 0a  >pSrc);.      }.
1773c 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
1773d 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65 6e 74  ** Delete an ent
1773e 69 72 65 20 53 72 63 4c 69 73 74 20 69 6e 63 6c  ire SrcList incl
1773f 75 64 69 6e 67 20 61 6c 6c 20 69 74 73 20 73 75  uding all its su
17740 62 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53  bstructure..*/.S
17741 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
17742 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  id sqlite3SrcLis
17743 74 44 65 6c 65 74 65 28 53 72 63 4c 69 73 74 20  tDelete(SrcList 
17744 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69  *pList){.  int i
17745 3b 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69  ;.  struct SrcLi
17746 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a  st_item *pItem;.
17747 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29    if( pList==0 )
17748 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 70   return;.  for(p
17749 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20 69  Item=pList->a, i
1774a 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 53 72  =0; i<pList->nSr
1774b 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29  c; i++, pItem++)
1774c 7b 0a 20 20 20 20 73 71 6c 69 74 65 46 72 65 65  {.    sqliteFree
1774d 28 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73  (pItem->zDatabas
1774e 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 46 72  e);.    sqliteFr
1774f 65 65 28 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29  ee(pItem->zName)
17750 3b 0a 20 20 20 20 73 71 6c 69 74 65 46 72 65 65  ;.    sqliteFree
17751 28 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b  (pItem->zAlias);
17752 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65  .    sqlite3Dele
17753 74 65 54 61 62 6c 65 28 70 49 74 65 6d 2d 3e 70  teTable(pItem->p
17754 54 61 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Tab);.    sqlite
17755 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70 49  3SelectDelete(pI
17756 74 65 6d 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20  tem->pSelect);. 
17757 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65     sqlite3ExprDe
17758 6c 65 74 65 28 70 49 74 65 6d 2d 3e 70 4f 6e 29  lete(pItem->pOn)
17759 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 49 64 4c  ;.    sqlite3IdL
1775a 69 73 74 44 65 6c 65 74 65 28 70 49 74 65 6d 2d  istDelete(pItem-
1775b 3e 70 55 73 69 6e 67 29 3b 0a 20 20 7d 0a 20 20  >pUsing);.  }.  
1775c 73 71 6c 69 74 65 46 72 65 65 28 70 4c 69 73 74  sqliteFree(pList
1775d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  );.}../*.** This
1775e 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
1775f 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72  ed by the parser
17760 20 74 6f 20 61 64 64 20 61 20 6e 65 77 20 74 65   to add a new te
17761 72 6d 20 74 6f 20 74 68 65 0a 2a 2a 20 65 6e 64  rm to the.** end
17762 20 6f 66 20 61 20 67 72 6f 77 69 6e 67 20 46 52   of a growing FR
17763 4f 4d 20 63 6c 61 75 73 65 2e 20 20 54 68 65 20  OM clause.  The 
17764 22 70 22 20 70 61 72 61 6d 65 74 65 72 20 69 73  "p" parameter is
17765 20 74 68 65 20 70 61 72 74 20 6f 66 0a 2a 2a 20   the part of.** 
17766 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
17767 74 68 61 74 20 68 61 73 20 61 6c 72 65 61 64 79  that has already
17768 20 62 65 65 6e 20 63 6f 6e 73 74 72 75 63 74 65   been constructe
17769 64 2e 20 20 22 70 22 20 69 73 20 4e 55 4c 4c 0a  d.  "p" is NULL.
1776a 2a 2a 20 69 66 20 74 68 69 73 20 69 73 20 74 68  ** if this is th
1776b 65 20 66 69 72 73 74 20 74 65 72 6d 20 6f 66 20  e first term of 
1776c 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e  the FROM clause.
1776d 20 20 70 54 61 62 6c 65 20 61 6e 64 20 70 44 61    pTable and pDa
1776e 74 61 62 61 73 65 0a 2a 2a 20 61 72 65 20 74 68  tabase.** are th
1776f 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  e name of the ta
17770 62 6c 65 20 61 6e 64 20 64 61 74 61 62 61 73 65  ble and database
17771 20 6e 61 6d 65 64 20 69 6e 20 74 68 65 20 46 52   named in the FR
17772 4f 4d 20 63 6c 61 75 73 65 20 74 65 72 6d 2e 0a  OM clause term..
17773 2a 2a 20 70 44 61 74 61 62 61 73 65 20 69 73 20  ** pDatabase is 
17774 4e 55 4c 4c 20 69 66 20 74 68 65 20 64 61 74 61  NULL if the data
17775 62 61 73 65 20 6e 61 6d 65 20 71 75 61 6c 69 66  base name qualif
17776 69 65 72 20 69 73 20 6d 69 73 73 69 6e 67 20 2d  ier is missing -
17777 20 74 68 65 0a 2a 2a 20 75 73 75 61 6c 20 63 61   the.** usual ca
17778 73 65 2e 20 20 49 66 20 74 68 65 20 74 65 72 6d  se.  If the term
17779 20 68 61 73 20 61 20 61 6c 69 61 73 2c 20 74 68   has a alias, th
1777a 65 6e 20 70 41 6c 69 61 73 20 70 6f 69 6e 74 73  en pAlias points
1777b 20 74 6f 20 74 68 65 0a 2a 2a 20 61 6c 69 61 73   to the.** alias
1777c 20 74 6f 6b 65 6e 2e 20 20 49 66 20 74 68 65 20   token.  If the 
1777d 74 65 72 6d 20 69 73 20 61 20 73 75 62 71 75 65  term is a subque
1777e 72 79 2c 20 74 68 65 6e 20 70 53 75 62 71 75 65  ry, then pSubque
1777f 72 79 20 69 73 20 74 68 65 0a 2a 2a 20 53 45 4c  ry is the.** SEL
17780 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 68  ECT statement th
17781 61 74 20 74 68 65 20 73 75 62 71 75 65 72 79 20  at the subquery 
17782 65 6e 63 6f 64 65 73 2e 20 20 54 68 65 20 70 54  encodes.  The pT
17783 61 62 6c 65 20 61 6e 64 0a 2a 2a 20 70 44 61 74  able and.** pDat
17784 61 62 61 73 65 20 70 61 72 61 6d 65 74 65 72 73  abase parameters
17785 20 61 72 65 20 4e 55 4c 4c 20 66 6f 72 20 73 75   are NULL for su
17786 62 71 75 65 72 69 65 73 2e 20 20 54 68 65 20 70  bqueries.  The p
17787 4f 6e 20 61 6e 64 20 70 55 73 69 6e 67 0a 2a 2a  On and pUsing.**
17788 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20   parameters are 
17789 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74  the content of t
1778a 68 65 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20  he ON and USING 
1778b 63 6c 61 75 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 52  clauses..**.** R
1778c 65 74 75 72 6e 20 61 20 6e 65 77 20 53 72 63 4c  eturn a new SrcL
1778d 69 73 74 20 77 68 69 63 68 20 65 6e 63 6f 64 65  ist which encode
1778e 73 20 69 73 20 74 68 65 20 46 52 4f 4d 20 77 69  s is the FROM wi
1778f 74 68 20 74 68 65 20 6e 65 77 0a 2a 2a 20 74 65  th the new.** te
17790 72 6d 20 61 64 64 65 64 2e 0a 2a 2f 0a 53 51 4c  rm added..*/.SQL
17791 49 54 45 5f 50 52 49 56 41 54 45 20 53 72 63 4c  ITE_PRIVATE SrcL
17792 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c  ist *sqlite3SrcL
17793 69 73 74 41 70 70 65 6e 64 46 72 6f 6d 54 65 72  istAppendFromTer
17794 6d 28 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 2c  m(.  SrcList *p,
17795 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
17796 54 68 65 20 6c 65 66 74 20 70 61 72 74 20 6f 66  The left part of
17797 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
17798 20 61 6c 72 65 61 64 79 20 73 65 65 6e 20 2a 2f   already seen */
17799 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65  .  Token *pTable
1779a 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61  ,          /* Na
1779b 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
1779c 74 6f 20 61 64 64 20 74 6f 20 74 68 65 20 46 52  to add to the FR
1779d 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 54  OM clause */.  T
1779e 6f 6b 65 6e 20 2a 70 44 61 74 61 62 61 73 65 2c  oken *pDatabase,
1779f 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
177a0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  f the database c
177a1 6f 6e 74 61 69 6e 69 6e 67 20 70 54 61 62 6c 65  ontaining pTable
177a2 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 41 6c   */.  Token *pAl
177a3 69 61 73 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  ias,          /*
177a4 20 54 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20   The right-hand 
177a5 73 69 64 65 20 6f 66 20 74 68 65 20 41 53 20 73  side of the AS s
177a6 75 62 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a  ubexpression */.
177a7 20 20 53 65 6c 65 63 74 20 2a 70 53 75 62 71 75    Select *pSubqu
177a8 65 72 79 2c 20 20 20 20 20 20 2f 2a 20 41 20 73  ery,      /* A s
177a9 75 62 71 75 65 72 79 20 75 73 65 64 20 69 6e 20  ubquery used in 
177aa 70 6c 61 63 65 20 6f 66 20 61 20 74 61 62 6c 65  place of a table
177ab 20 6e 61 6d 65 20 2a 2f 0a 20 20 45 78 70 72 20   name */.  Expr 
177ac 2a 70 4f 6e 2c 20 20 20 20 20 20 20 20 20 20 20  *pOn,           
177ad 20 20 20 2f 2a 20 54 68 65 20 4f 4e 20 63 6c 61     /* The ON cla
177ae 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f  use of a join */
177af 0a 20 20 49 64 4c 69 73 74 20 2a 70 55 73 69 6e  .  IdList *pUsin
177b0 67 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  g          /* Th
177b1 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20 6f  e USING clause o
177b2 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 29 7b 0a 20  f a join */.){. 
177b3 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
177b4 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 70  item *pItem;.  p
177b5 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73   = sqlite3SrcLis
177b6 74 41 70 70 65 6e 64 28 70 2c 20 70 54 61 62 6c  tAppend(p, pTabl
177b7 65 2c 20 70 44 61 74 61 62 61 73 65 29 3b 0a 20  e, pDatabase);. 
177b8 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e   if( p==0 || p->
177b9 6e 53 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 73  nSrc==0 ){.    s
177ba 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
177bb 28 70 4f 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74  (pOn);.    sqlit
177bc 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 70  e3IdListDelete(p
177bd 55 73 69 6e 67 29 3b 0a 20 20 20 20 73 71 6c 69  Using);.    sqli
177be 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
177bf 70 53 75 62 71 75 65 72 79 29 3b 0a 20 20 20 20  pSubquery);.    
177c0 72 65 74 75 72 6e 20 70 3b 0a 20 20 7d 0a 20 20  return p;.  }.  
177c1 70 49 74 65 6d 20 3d 20 26 70 2d 3e 61 5b 70 2d  pItem = &p->a[p-
177c2 3e 6e 53 72 63 2d 31 5d 3b 0a 20 20 69 66 28 20  >nSrc-1];.  if( 
177c3 70 41 6c 69 61 73 20 26 26 20 70 41 6c 69 61 73  pAlias && pAlias
177c4 2d 3e 6e 20 29 7b 0a 20 20 20 20 70 49 74 65 6d  ->n ){.    pItem
177c5 2d 3e 7a 41 6c 69 61 73 20 3d 20 73 71 6c 69 74  ->zAlias = sqlit
177c6 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
177c7 70 41 6c 69 61 73 29 3b 0a 20 20 7d 0a 20 20 70  pAlias);.  }.  p
177c8 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 3d 20  Item->pSelect = 
177c9 70 53 75 62 71 75 65 72 79 3b 0a 20 20 70 49 74  pSubquery;.  pIt
177ca 65 6d 2d 3e 70 4f 6e 20 3d 20 70 4f 6e 3b 0a 20  em->pOn = pOn;. 
177cb 20 70 49 74 65 6d 2d 3e 70 55 73 69 6e 67 20 3d   pItem->pUsing =
177cc 20 70 55 73 69 6e 67 3b 0a 20 20 72 65 74 75 72   pUsing;.  retur
177cd 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 68  n p;.}../*.** Wh
177ce 65 6e 20 62 75 69 6c 64 69 6e 67 20 75 70 20 61  en building up a
177cf 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 6e 20   FROM clause in 
177d0 74 68 65 20 70 61 72 73 65 72 2c 20 74 68 65 20  the parser, the 
177d1 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72 0a 2a 2a  join operator.**
177d2 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 61 74   is initially at
177d3 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 6c 65  tached to the le
177d4 66 74 20 6f 70 65 72 61 6e 64 2e 20 20 42 75 74  ft operand.  But
177d5 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61   the code genera
177d6 74 6f 72 0a 2a 2a 20 65 78 70 65 63 74 73 20 74  tor.** expects t
177d7 68 65 20 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72  he join operator
177d8 20 74 6f 20 62 65 20 6f 6e 20 74 68 65 20 72 69   to be on the ri
177d9 67 68 74 20 6f 70 65 72 61 6e 64 2e 20 20 54 68  ght operand.  Th
177da 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 53 68  is routine.** Sh
177db 69 66 74 73 20 61 6c 6c 20 6a 6f 69 6e 20 6f 70  ifts all join op
177dc 65 72 61 74 6f 72 73 20 66 72 6f 6d 20 6c 65 66  erators from lef
177dd 74 20 74 6f 20 72 69 67 68 74 20 66 6f 72 20 61  t to right for a
177de 6e 20 65 6e 74 69 72 65 20 46 52 4f 4d 0a 2a 2a  n entire FROM.**
177df 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 45   clause..**.** E
177e0 78 61 6d 70 6c 65 3a 20 53 75 70 70 6f 73 65 20  xample: Suppose 
177e1 74 68 65 20 6a 6f 69 6e 20 69 73 20 6c 69 6b 65  the join is like
177e2 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   this:.**.**    
177e3 20 20 20 20 20 20 20 41 20 6e 61 74 75 72 61 6c         A natural
177e4 20 63 72 6f 73 73 20 6a 6f 69 6e 20 42 0a 2a 2a   cross join B.**
177e5 0a 2a 2a 20 54 68 65 20 6f 70 65 72 61 74 6f 72  .** The operator
177e6 20 69 73 20 22 6e 61 74 75 72 61 6c 20 63 72 6f   is "natural cro
177e7 73 73 20 6a 6f 69 6e 22 2e 20 20 54 68 65 20 41  ss join".  The A
177e8 20 61 6e 64 20 42 20 6f 70 65 72 61 6e 64 73 20   and B operands 
177e9 61 72 65 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e  are stored.** in
177ea 20 70 2d 3e 61 5b 30 5d 20 61 6e 64 20 70 2d 3e   p->a[0] and p->
177eb 61 5b 31 5d 2c 20 72 65 73 70 65 63 74 69 76 65  a[1], respective
177ec 6c 79 2e 20 20 54 68 65 20 70 61 72 73 65 72 20  ly.  The parser 
177ed 69 6e 69 74 69 61 6c 6c 79 20 73 74 6f 72 65 73  initially stores
177ee 20 74 68 65 0a 2a 2a 20 6f 70 65 72 61 74 6f 72   the.** operator
177ef 20 77 69 74 68 20 41 2e 20 20 54 68 69 73 20 72   with A.  This r
177f0 6f 75 74 69 6e 65 20 73 68 69 66 74 73 20 74 68  outine shifts th
177f1 61 74 20 6f 70 65 72 61 74 6f 72 20 6f 76 65 72  at operator over
177f2 20 74 6f 20 42 2e 0a 2a 2f 0a 53 51 4c 49 54 45   to B..*/.SQLITE
177f3 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
177f4 6c 69 74 65 33 53 72 63 4c 69 73 74 53 68 69 66  lite3SrcListShif
177f5 74 4a 6f 69 6e 54 79 70 65 28 53 72 63 4c 69 73  tJoinType(SrcLis
177f6 74 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 26  t *p){.  if( p &
177f7 26 20 70 2d 3e 61 20 29 7b 0a 20 20 20 20 69 6e  & p->a ){.    in
177f8 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 70  t i;.    for(i=p
177f9 2d 3e 6e 53 72 63 2d 31 3b 20 69 3e 30 3b 20 69  ->nSrc-1; i>0; i
177fa 2d 2d 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61 5b  --){.      p->a[
177fb 69 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d 20 70 2d  i].jointype = p-
177fc 3e 61 5b 69 2d 31 5d 2e 6a 6f 69 6e 74 79 70 65  >a[i-1].jointype
177fd 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 61  ;.    }.    p->a
177fe 5b 30 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d 20 30  [0].jointype = 0
177ff 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42  ;.  }.}../*.** B
17800 65 67 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69  egin a transacti
17801 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  on.*/.SQLITE_PRI
17802 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
17803 33 42 65 67 69 6e 54 72 61 6e 73 61 63 74 69 6f  3BeginTransactio
17804 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  n(Parse *pParse,
17805 20 69 6e 74 20 74 79 70 65 29 7b 0a 20 20 73 71   int type){.  sq
17806 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 56 64 62  lite3 *db;.  Vdb
17807 65 20 2a 76 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a  e *v;.  int i;..
17808 20 20 69 66 28 20 70 50 61 72 73 65 3d 3d 30 20    if( pParse==0 
17809 7c 7c 20 28 64 62 3d 70 50 61 72 73 65 2d 3e 64  || (db=pParse->d
1780a 62 29 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61 44 62  b)==0 || db->aDb
1780b 5b 30 5d 2e 70 42 74 3d 3d 30 20 29 20 72 65 74  [0].pBt==0 ) ret
1780c 75 72 6e 3b 0a 20 20 69 66 28 20 70 50 61 72 73  urn;.  if( pPars
1780d 65 2d 3e 6e 45 72 72 20 7c 7c 20 73 71 6c 69 74  e->nErr || sqlit
1780e 65 33 4d 61 6c 6c 6f 63 46 61 69 6c 65 64 28 29  e3MallocFailed()
1780f 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28   ) return;.  if(
17810 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
17811 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
17812 5f 54 52 41 4e 53 41 43 54 49 4f 4e 2c 20 22 42  _TRANSACTION, "B
17813 45 47 49 4e 22 2c 20 30 2c 20 30 29 20 29 20 72  EGIN", 0, 0) ) r
17814 65 74 75 72 6e 3b 0a 0a 20 20 76 20 3d 20 73 71  eturn;..  v = sq
17815 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
17816 72 73 65 29 3b 0a 20 20 69 66 28 20 21 76 20 29  rse);.  if( !v )
17817 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 74   return;.  if( t
17818 79 70 65 21 3d 54 4b 5f 44 45 46 45 52 52 45 44  ype!=TK_DEFERRED
17819 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   ){.    for(i=0;
1781a 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29   i<db->nDb; i++)
1781b 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
1781c 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 54  dbeAddOp(v, OP_T
1781d 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 2c 20 28  ransaction, i, (
1781e 74 79 70 65 3d 3d 54 4b 5f 45 58 43 4c 55 53 49  type==TK_EXCLUSI
1781f 56 45 29 2b 31 29 3b 0a 20 20 20 20 7d 0a 20 20  VE)+1);.    }.  
17820 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  }.  sqlite3VdbeA
17821 64 64 4f 70 28 76 2c 20 4f 50 5f 41 75 74 6f 43  ddOp(v, OP_AutoC
17822 6f 6d 6d 69 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a  ommit, 0, 0);.}.
17823 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d 69 74 20 61 20  ./*.** Commit a 
17824 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2f 0a 53  transaction.*/.S
17825 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
17826 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74  id sqlite3Commit
17827 54 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73  Transaction(Pars
17828 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 73 71  e *pParse){.  sq
17829 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 56 64 62  lite3 *db;.  Vdb
1782a 65 20 2a 76 3b 0a 0a 20 20 69 66 28 20 70 50 61  e *v;..  if( pPa
1782b 72 73 65 3d 3d 30 20 7c 7c 20 28 64 62 3d 70 50  rse==0 || (db=pP
1782c 61 72 73 65 2d 3e 64 62 29 3d 3d 30 20 7c 7c 20  arse->db)==0 || 
1782d 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 3d 3d  db->aDb[0].pBt==
1782e 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  0 ) return;.  if
1782f 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c  ( pParse->nErr |
17830 7c 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 46  | sqlite3MallocF
17831 61 69 6c 65 64 28 29 20 29 20 72 65 74 75 72 6e  ailed() ) return
17832 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 41  ;.  if( sqlite3A
17833 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
17834 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 41 43 54   SQLITE_TRANSACT
17835 49 4f 4e 2c 20 22 43 4f 4d 4d 49 54 22 2c 20 30  ION, "COMMIT", 0
17836 2c 20 30 29 20 29 20 72 65 74 75 72 6e 3b 0a 0a  , 0) ) return;..
17837 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
17838 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
17839 69 66 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c  if( v ){.    sql
1783a 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1783b 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 2c 20   OP_AutoCommit, 
1783c 31 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  1, 0);.  }.}../*
1783d 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 20 74  .** Rollback a t
1783e 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2f 0a 53 51  ransaction.*/.SQ
1783f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
17840 64 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63  d sqlite3Rollbac
17841 6b 54 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 72  kTransaction(Par
17842 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 73  se *pParse){.  s
17843 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 56 64  qlite3 *db;.  Vd
17844 62 65 20 2a 76 3b 0a 0a 20 20 69 66 28 20 70 50  be *v;..  if( pP
17845 61 72 73 65 3d 3d 30 20 7c 7c 20 28 64 62 3d 70  arse==0 || (db=p
17846 50 61 72 73 65 2d 3e 64 62 29 3d 3d 30 20 7c 7c  Parse->db)==0 ||
17847 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 3d   db->aDb[0].pBt=
17848 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69  =0 ) return;.  i
17849 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20  f( pParse->nErr 
1784a 7c 7c 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  || sqlite3Malloc
1784b 46 61 69 6c 65 64 28 29 20 29 20 72 65 74 75 72  Failed() ) retur
1784c 6e 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  n;.  if( sqlite3
1784d 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
1784e 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 41 43  , SQLITE_TRANSAC
1784f 54 49 4f 4e 2c 20 22 52 4f 4c 4c 42 41 43 4b 22  TION, "ROLLBACK"
17850 2c 20 30 2c 20 30 29 20 29 20 72 65 74 75 72 6e  , 0, 0) ) return
17851 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33  ;..  v = sqlite3
17852 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
17853 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20  .  if( v ){.    
17854 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
17855 28 76 2c 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69  (v, OP_AutoCommi
17856 74 2c 20 31 2c 20 31 29 3b 0a 20 20 7d 0a 7d 0a  t, 1, 1);.  }.}.
17857 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65  ./*.** Make sure
17858 20 74 68 65 20 54 45 4d 50 20 64 61 74 61 62 61   the TEMP databa
17859 73 65 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 61  se is open and a
1785a 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 75 73 65  vailable for use
1785b 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65  .  Return.** the
1785c 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72   number of error
1785d 73 2e 20 20 4c 65 61 76 65 20 61 6e 79 20 65 72  s.  Leave any er
1785e 72 6f 72 20 6d 65 73 73 61 67 65 73 20 69 6e 20  ror messages in 
1785f 74 68 65 20 70 50 61 72 73 65 20 73 74 72 75 63  the pParse struc
17860 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ture..*/.SQLITE_
17861 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
17862 74 65 33 4f 70 65 6e 54 65 6d 70 44 61 74 61 62  te3OpenTempDatab
17863 61 73 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  ase(Parse *pPars
17864 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  e){.  sqlite3 *d
17865 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
17866 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 31 5d    if( db->aDb[1]
17867 2e 70 42 74 3d 3d 30 20 26 26 20 21 70 50 61 72  .pBt==0 && !pPar
17868 73 65 2d 3e 65 78 70 6c 61 69 6e 20 29 7b 0a 20  se->explain ){. 
17869 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69     int rc = sqli
1786a 74 65 33 42 74 72 65 65 46 61 63 74 6f 72 79 28  te3BtreeFactory(
1786b 64 62 2c 20 30 2c 20 30 2c 20 53 51 4c 49 54 45  db, 0, 0, SQLITE
1786c 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53  _DEFAULT_CACHE_S
1786d 49 5a 45 2c 0a 20 20 20 20 20 20 20 20 20 20 20  IZE,.           
1786e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1786f 20 20 20 20 20 20 26 64 62 2d 3e 61 44 62 5b 31        &db->aDb[1
17870 5d 2e 70 42 74 29 3b 0a 20 20 20 20 69 66 28 20  ].pBt);.    if( 
17871 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
17872 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
17873 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
17874 75 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 61  unable to open a
17875 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62   temporary datab
17876 61 73 65 20 22 0a 20 20 20 20 20 20 20 20 22 66  ase ".        "f
17877 69 6c 65 20 66 6f 72 20 73 74 6f 72 69 6e 67 20  ile for storing 
17878 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73  temporary tables
17879 22 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65  ");.      pParse
1787a 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 20  ->rc = rc;.     
1787b 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
1787c 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 66 6c 61  .    if( db->fla
1787d 67 73 20 26 20 21 64 62 2d 3e 61 75 74 6f 43 6f  gs & !db->autoCo
1787e 6d 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 72 63  mmit ){.      rc
1787f 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 42   = sqlite3BtreeB
17880 65 67 69 6e 54 72 61 6e 73 28 64 62 2d 3e 61 44  eginTrans(db->aD
17881 62 5b 31 5d 2e 70 42 74 2c 20 31 29 3b 0a 20 20  b[1].pBt, 1);.  
17882 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
17883 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
17884 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
17885 28 70 50 61 72 73 65 2c 20 22 75 6e 61 62 6c 65  (pParse, "unable
17886 20 74 6f 20 67 65 74 20 61 20 77 72 69 74 65 20   to get a write 
17887 6c 6f 63 6b 20 6f 6e 20 22 0a 20 20 20 20 20 20  lock on ".      
17888 20 20 20 20 22 74 68 65 20 74 65 6d 70 6f 72 61      "the tempora
17889 72 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ry database file
1788a 22 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72  ");.        pPar
1788b 73 65 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20  se->rc = rc;.   
1788c 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
1788d 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
1788e 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62   assert( db->aDb
1788f 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29 3b 0a 20  [1].pSchema );. 
17890 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
17891 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
17892 20 56 44 42 45 20 63 6f 64 65 20 74 68 61 74 20   VDBE code that 
17893 77 69 6c 6c 20 76 65 72 69 66 79 20 74 68 65 20  will verify the 
17894 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 61 6e  schema cookie an
17895 64 20 73 74 61 72 74 0a 2a 2a 20 61 20 72 65 61  d start.** a rea
17896 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 66 6f  d-transaction fo
17897 72 20 61 6c 6c 20 6e 61 6d 65 64 20 64 61 74 61  r all named data
17898 62 61 73 65 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a  base files..**.*
17899 2a 20 49 74 20 69 73 20 69 6d 70 6f 72 74 61 6e  * It is importan
1789a 74 20 74 68 61 74 20 61 6c 6c 20 73 63 68 65 6d  t that all schem
1789b 61 20 63 6f 6f 6b 69 65 73 20 62 65 20 76 65 72  a cookies be ver
1789c 69 66 69 65 64 20 61 6e 64 20 61 6c 6c 0a 2a 2a  ified and all.**
1789d 20 72 65 61 64 20 74 72 61 6e 73 61 63 74 69 6f   read transactio
1789e 6e 73 20 62 65 20 73 74 61 72 74 65 64 20 62 65  ns be started be
1789f 66 6f 72 65 20 61 6e 79 74 68 69 6e 67 20 65 6c  fore anything el
178a0 73 65 20 68 61 70 70 65 6e 73 20 69 6e 0a 2a 2a  se happens in.**
178a1 20 74 68 65 20 56 44 42 45 20 70 72 6f 67 72 61   the VDBE progra
178a2 6d 2e 20 20 42 75 74 20 74 68 69 73 20 72 6f 75  m.  But this rou
178a3 74 69 6e 65 20 63 61 6e 20 62 65 20 63 61 6c 6c  tine can be call
178a4 65 64 20 61 66 74 65 72 20 6d 75 63 68 20 6f 74  ed after much ot
178a5 68 65 72 0a 2a 2a 20 63 6f 64 65 20 68 61 73 20  her.** code has 
178a6 62 65 65 6e 20 67 65 6e 65 72 61 74 65 64 2e 20  been generated. 
178a7 20 53 6f 20 68 65 72 65 20 69 73 20 77 68 61 74   So here is what
178a8 20 77 65 20 64 6f 3a 0a 2a 2a 0a 2a 2a 20 54 68   we do:.**.** Th
178a9 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 69  e first time thi
178aa 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
178ab 6c 65 64 2c 20 77 65 20 63 6f 64 65 20 61 6e 20  led, we code an 
178ac 4f 50 5f 47 6f 74 6f 20 74 68 61 74 0a 2a 2a 20  OP_Goto that.** 
178ad 77 69 6c 6c 20 6a 75 6d 70 20 74 6f 20 61 20 73  will jump to a s
178ae 75 62 72 6f 75 74 69 6e 65 20 61 74 20 74 68 65  ubroutine at the
178af 20 65 6e 64 20 6f 66 20 74 68 65 20 70 72 6f 67   end of the prog
178b0 72 61 6d 2e 20 20 54 68 65 6e 20 77 65 0a 2a 2a  ram.  Then we.**
178b1 20 72 65 63 6f 72 64 20 65 76 65 72 79 20 64 61   record every da
178b2 74 61 62 61 73 65 20 74 68 61 74 20 6e 65 65 64  tabase that need
178b3 73 20 69 74 73 20 73 63 68 65 6d 61 20 76 65 72  s its schema ver
178b4 69 66 69 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20  ified in the.** 
178b5 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61  pParse->cookieMa
178b6 73 6b 20 66 69 65 6c 64 2e 20 20 4c 61 74 65 72  sk field.  Later
178b7 2c 20 61 66 74 65 72 20 61 6c 6c 20 6f 74 68 65  , after all othe
178b8 72 20 63 6f 64 65 20 68 61 73 20 62 65 65 6e 0a  r code has been.
178b9 2a 2a 20 67 65 6e 65 72 61 74 65 64 2c 20 74 68  ** generated, th
178ba 65 20 73 75 62 72 6f 75 74 69 6e 65 20 74 68 61  e subroutine tha
178bb 74 20 64 6f 65 73 20 74 68 65 20 63 6f 6f 6b 69  t does the cooki
178bc 65 20 76 65 72 69 66 69 63 61 74 69 6f 6e 73 20  e verifications 
178bd 61 6e 64 0a 2a 2a 20 73 74 61 72 74 73 20 74 68  and.** starts th
178be 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 77  e transactions w
178bf 69 6c 6c 20 62 65 20 63 6f 64 65 64 20 61 6e 64  ill be coded and
178c0 20 74 68 65 20 4f 50 5f 47 6f 74 6f 20 50 32 20   the OP_Goto P2 
178c1 76 61 6c 75 65 0a 2a 2a 20 77 69 6c 6c 20 62 65  value.** will be
178c2 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74   made to point t
178c3 6f 20 74 68 61 74 20 73 75 62 72 6f 75 74 69 6e  o that subroutin
178c4 65 2e 20 20 54 68 65 20 67 65 6e 65 72 61 74 69  e.  The generati
178c5 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6f  on of the.** coo
178c6 6b 69 65 20 76 65 72 69 66 69 63 61 74 69 6f 6e  kie verification
178c7 20 73 75 62 72 6f 75 74 69 6e 65 20 63 6f 64 65   subroutine code
178c8 20 68 61 70 70 65 6e 73 20 69 6e 20 73 71 6c 69   happens in sqli
178c9 74 65 33 46 69 6e 69 73 68 43 6f 64 69 6e 67 28  te3FinishCoding(
178ca 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 44 62 3c  )..**.** If iDb<
178cb 30 20 74 68 65 6e 20 63 6f 64 65 20 74 68 65 20  0 then code the 
178cc 4f 50 5f 47 6f 74 6f 20 6f 6e 6c 79 20 2d 20 64  OP_Goto only - d
178cd 6f 6e 27 74 20 73 65 74 20 66 6c 61 67 20 74 6f  on't set flag to
178ce 20 76 65 72 69 66 79 20 74 68 65 0a 2a 2a 20 73   verify the.** s
178cf 63 68 65 6d 61 20 6f 6e 20 61 6e 79 20 64 61 74  chema on any dat
178d0 61 62 61 73 65 73 2e 20 20 54 68 69 73 20 63 61  abases.  This ca
178d1 6e 20 62 65 20 75 73 65 64 20 74 6f 20 70 6f 73  n be used to pos
178d2 69 74 69 6f 6e 20 74 68 65 20 4f 50 5f 47 6f 74  ition the OP_Got
178d3 6f 0a 2a 2a 20 65 61 72 6c 79 20 69 6e 20 74 68  o.** early in th
178d4 65 20 63 6f 64 65 2c 20 62 65 66 6f 72 65 20 77  e code, before w
178d5 65 20 6b 6e 6f 77 20 69 66 20 61 6e 79 20 64 61  e know if any da
178d6 74 61 62 61 73 65 20 74 61 62 6c 65 73 20 77 69  tabase tables wi
178d7 6c 6c 20 62 65 20 75 73 65 64 2e 0a 2a 2f 0a 53  ll be used..*/.S
178d8 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
178d9 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65  id sqlite3CodeVe
178da 72 69 66 79 53 63 68 65 6d 61 28 50 61 72 73 65  rifySchema(Parse
178db 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 44   *pParse, int iD
178dc 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  b){.  sqlite3 *d
178dd 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20  b;.  Vdbe *v;.  
178de 69 6e 74 20 6d 61 73 6b 3b 0a 0a 20 20 76 20 3d  int mask;..  v =
178df 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
178e0 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76  pParse);.  if( v
178e1 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 20 20 2f  ==0 ) return;  /
178e2 2a 20 54 68 69 73 20 6f 6e 6c 79 20 68 61 70 70  * This only happ
178e3 65 6e 73 20 69 66 20 74 68 65 72 65 20 77 61 73  ens if there was
178e4 20 61 20 70 72 69 6f 72 20 65 72 72 6f 72 20 2a   a prior error *
178e5 2f 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d  /.  db = pParse-
178e6 3e 64 62 3b 0a 20 20 69 66 28 20 70 50 61 72 73  >db;.  if( pPars
178e7 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 3d 3d 30  e->cookieGoto==0
178e8 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e   ){.    pParse->
178e9 63 6f 6f 6b 69 65 47 6f 74 6f 20 3d 20 73 71 6c  cookieGoto = sql
178ea 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
178eb 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 30 29 2b   OP_Goto, 0, 0)+
178ec 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 44 62  1;.  }.  if( iDb
178ed 3e 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72  >=0 ){.    asser
178ee 74 28 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29  t( iDb<db->nDb )
178ef 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 62  ;.    assert( db
178f0 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 21 3d  ->aDb[iDb].pBt!=
178f1 30 20 7c 7c 20 69 44 62 3d 3d 31 20 29 3b 0a 20  0 || iDb==1 );. 
178f2 20 20 20 61 73 73 65 72 74 28 20 69 44 62 3c 53     assert( iDb<S
178f3 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48  QLITE_MAX_ATTACH
178f4 45 44 2b 32 20 29 3b 0a 20 20 20 20 6d 61 73 6b  ED+2 );.    mask
178f5 20 3d 20 31 3c 3c 69 44 62 3b 0a 20 20 20 20 69   = 1<<iDb;.    i
178f6 66 28 20 28 70 50 61 72 73 65 2d 3e 63 6f 6f 6b  f( (pParse->cook
178f7 69 65 4d 61 73 6b 20 26 20 6d 61 73 6b 29 3d 3d  ieMask & mask)==
178f8 30 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73  0 ){.      pPars
178f9 65 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20 7c 3d  e->cookieMask |=
178fa 20 6d 61 73 6b 3b 0a 20 20 20 20 20 20 70 50 61   mask;.      pPa
178fb 72 73 65 2d 3e 63 6f 6f 6b 69 65 56 61 6c 75 65  rse->cookieValue
178fc 5b 69 44 62 5d 20 3d 20 64 62 2d 3e 61 44 62 5b  [iDb] = db->aDb[
178fd 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 73 63  iDb].pSchema->sc
178fe 68 65 6d 61 5f 63 6f 6f 6b 69 65 3b 0a 20 20 20  hema_cookie;.   
178ff 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d     if( !OMIT_TEM
17900 50 44 42 20 26 26 20 69 44 62 3d 3d 31 20 29 7b  PDB && iDb==1 ){
17901 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
17902 4f 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73 65  OpenTempDatabase
17903 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
17904 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  }.    }.  }.}../
17905 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 56 44  *.** Generate VD
17906 42 45 20 63 6f 64 65 20 74 68 61 74 20 70 72 65  BE code that pre
17907 70 61 72 65 73 20 66 6f 72 20 64 6f 69 6e 67 20  pares for doing 
17908 61 6e 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 61  an operation tha
17909 74 0a 2a 2a 20 6d 69 67 68 74 20 63 68 61 6e 67  t.** might chang
1790a 65 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a  e the database..
1790b 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
1790c 6e 65 20 73 74 61 72 74 73 20 61 20 6e 65 77 20  ne starts a new 
1790d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 66 20 77  transaction if w
1790e 65 20 61 72 65 20 6e 6f 74 20 61 6c 72 65 61 64  e are not alread
1790f 79 20 77 69 74 68 69 6e 0a 2a 2a 20 61 20 74 72  y within.** a tr
17910 61 6e 73 61 63 74 69 6f 6e 2e 20 20 49 66 20 77  ansaction.  If w
17911 65 20 61 72 65 20 61 6c 72 65 61 64 79 20 77 69  e are already wi
17912 74 68 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69  thin a transacti
17913 6f 6e 2c 20 74 68 65 6e 20 61 20 63 68 65 63 6b  on, then a check
17914 70 6f 69 6e 74 0a 2a 2a 20 69 73 20 73 65 74 20  point.** is set 
17915 69 66 20 74 68 65 20 73 65 74 53 74 61 74 65 6d  if the setStatem
17916 65 6e 74 20 70 61 72 61 6d 65 74 65 72 20 69 73  ent parameter is
17917 20 74 72 75 65 2e 20 20 41 20 63 68 65 63 6b 70   true.  A checkp
17918 6f 69 6e 74 20 73 68 6f 75 6c 64 0a 2a 2a 20 62  oint should.** b
17919 65 20 73 65 74 20 66 6f 72 20 6f 70 65 72 61 74  e set for operat
1791a 69 6f 6e 73 20 74 68 61 74 20 6d 69 67 68 74 20  ions that might 
1791b 66 61 69 6c 20 28 64 75 65 20 74 6f 20 61 20 63  fail (due to a c
1791c 6f 6e 73 74 72 61 69 6e 74 29 20 70 61 72 74 20  onstraint) part 
1791d 6f 66 0a 2a 2a 20 74 68 65 20 77 61 79 20 74 68  of.** the way th
1791e 72 6f 75 67 68 20 61 6e 64 20 77 68 69 63 68 20  rough and which 
1791f 77 69 6c 6c 20 6e 65 65 64 20 74 6f 20 75 6e 64  will need to und
17920 6f 20 73 6f 6d 65 20 77 72 69 74 65 73 20 77 69  o some writes wi
17921 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 0a  thout having to.
17922 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20  ** rollback the 
17923 77 68 6f 6c 65 20 74 72 61 6e 73 61 63 74 69 6f  whole transactio
17924 6e 2e 20 20 46 6f 72 20 6f 70 65 72 61 74 69 6f  n.  For operatio
17925 6e 73 20 77 68 65 72 65 20 61 6c 6c 20 63 6f 6e  ns where all con
17926 73 74 72 61 69 6e 74 73 0a 2a 2a 20 63 61 6e 20  straints.** can 
17927 62 65 20 63 68 65 63 6b 65 64 20 62 65 66 6f 72  be checked befor
17928 65 20 61 6e 79 20 63 68 61 6e 67 65 73 20 61 72  e any changes ar
17929 65 20 6d 61 64 65 20 74 6f 20 74 68 65 20 64 61  e made to the da
1792a 74 61 62 61 73 65 2c 20 69 74 20 69 73 20 6e 65  tabase, it is ne
1792b 76 65 72 0a 2a 2a 20 6e 65 63 65 73 73 61 72 79  ver.** necessary
1792c 20 74 6f 20 75 6e 64 6f 20 61 20 77 72 69 74 65   to undo a write
1792d 20 61 6e 64 20 74 68 65 20 63 68 65 63 6b 70 6f   and the checkpo
1792e 69 6e 74 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62  int should not b
1792f 65 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c  e set..**.** Onl
17930 79 20 64 61 74 61 62 61 73 65 20 69 44 62 20 61  y database iDb a
17931 6e 64 20 74 68 65 20 74 65 6d 70 20 64 61 74 61  nd the temp data
17932 62 61 73 65 20 61 72 65 20 6d 61 64 65 20 77 72  base are made wr
17933 69 74 61 62 6c 65 20 62 79 20 74 68 69 73 20 63  itable by this c
17934 61 6c 6c 2e 0a 2a 2a 20 49 66 20 69 44 62 3d 3d  all..** If iDb==
17935 30 2c 20 74 68 65 6e 20 74 68 65 20 6d 61 69 6e  0, then the main
17936 20 61 6e 64 20 74 65 6d 70 20 64 61 74 61 62 61   and temp databa
17937 73 65 73 20 61 72 65 20 6d 61 64 65 20 77 72 69  ses are made wri
17938 74 61 62 6c 65 2e 20 20 20 49 66 0a 2a 2a 20 69  table.   If.** i
17939 44 62 3d 3d 31 20 74 68 65 6e 20 6f 6e 6c 79 20  Db==1 then only 
1793a 74 68 65 20 74 65 6d 70 20 64 61 74 61 62 61 73  the temp databas
1793b 65 20 69 73 20 6d 61 64 65 20 77 72 69 74 61 62  e is made writab
1793c 6c 65 2e 20 20 49 66 20 69 44 62 3e 31 20 74 68  le.  If iDb>1 th
1793d 65 6e 20 74 68 65 0a 2a 2a 20 73 70 65 63 69 66  en the.** specif
1793e 69 65 64 20 61 75 78 69 6c 69 61 72 79 20 64 61  ied auxiliary da
1793f 74 61 62 61 73 65 20 61 6e 64 20 74 68 65 20 74  tabase and the t
17940 65 6d 70 20 64 61 74 61 62 61 73 65 20 61 72 65  emp database are
17941 20 6d 61 64 65 20 77 72 69 74 61 62 6c 65 2e 0a   made writable..
17942 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
17943 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65  E void sqlite3Be
17944 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f  ginWriteOperatio
17945 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  n(Parse *pParse,
17946 20 69 6e 74 20 73 65 74 53 74 61 74 65 6d 65 6e   int setStatemen
17947 74 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 56  t, int iDb){.  V
17948 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33  dbe *v = sqlite3
17949 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
1794a 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65  .  if( v==0 ) re
1794b 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 43  turn;.  sqlite3C
1794c 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 28  odeVerifySchema(
1794d 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20  pParse, iDb);.  
1794e 70 50 61 72 73 65 2d 3e 77 72 69 74 65 4d 61 73  pParse->writeMas
1794f 6b 20 7c 3d 20 31 3c 3c 69 44 62 3b 0a 20 20 69  k |= 1<<iDb;.  i
17950 66 28 20 73 65 74 53 74 61 74 65 6d 65 6e 74 20  f( setStatement 
17951 26 26 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65  && pParse->neste
17952 64 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  d==0 ){.    sqli
17953 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
17954 4f 50 5f 53 74 61 74 65 6d 65 6e 74 2c 20 69 44  OP_Statement, iD
17955 62 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28  b, 0);.  }.  if(
17956 20 28 4f 4d 49 54 5f 54 45 4d 50 44 42 20 7c 7c   (OMIT_TEMPDB ||
17957 20 69 44 62 21 3d 31 29 20 26 26 20 70 50 61 72   iDb!=1) && pPar
17958 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 31 5d 2e 70  se->db->aDb[1].p
17959 42 74 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  Bt!=0 ){.    sql
1795a 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70  ite3BeginWriteOp
1795b 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20  eration(pParse, 
1795c 73 65 74 53 74 61 74 65 6d 65 6e 74 2c 20 31 29  setStatement, 1)
1795d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  ;.  }.}../*.** C
1795e 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 70  heck to see if p
1795f 49 6e 64 65 78 20 75 73 65 73 20 74 68 65 20 63  Index uses the c
17960 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
17961 65 20 70 43 6f 6c 6c 2e 20 20 52 65 74 75 72 6e  e pColl.  Return
17962 0a 2a 2a 20 74 72 75 65 20 69 66 20 69 74 20 64  .** true if it d
17963 6f 65 73 20 61 6e 64 20 66 61 6c 73 65 20 69 66  oes and false if
17964 20 69 74 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2f   it does not..*/
17965 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
17966 4f 4d 49 54 5f 52 45 49 4e 44 45 58 0a 73 74 61  OMIT_REINDEX.sta
17967 74 69 63 20 69 6e 74 20 63 6f 6c 6c 61 74 69 6f  tic int collatio
17968 6e 4d 61 74 63 68 28 63 6f 6e 73 74 20 63 68 61  nMatch(const cha
17969 72 20 2a 7a 43 6f 6c 6c 2c 20 49 6e 64 65 78 20  r *zColl, Index 
1796a 2a 70 49 6e 64 65 78 29 7b 0a 20 20 69 6e 74 20  *pIndex){.  int 
1796b 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  i;.  for(i=0; i<
1796c 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b  pIndex->nColumn;
1796d 20 69 2b 2b 29 7b 0a 20 20 20 20 63 6f 6e 73 74   i++){.    const
1796e 20 63 68 61 72 20 2a 7a 20 3d 20 70 49 6e 64 65   char *z = pInde
1796f 78 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 3b 0a 20 20  x->azColl[i];.  
17970 20 20 69 66 28 20 7a 3d 3d 7a 43 6f 6c 6c 20 7c    if( z==zColl |
17971 7c 20 28 7a 20 26 26 20 7a 43 6f 6c 6c 20 26 26  | (z && zColl &&
17972 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43   0==sqlite3StrIC
17973 6d 70 28 7a 2c 20 7a 43 6f 6c 6c 29 29 20 29 7b  mp(z, zColl)) ){
17974 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
17975 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
17976 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a  urn 0;.}.#endif.
17977 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 6d 70 75 74 65  ./*.** Recompute
17978 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66 20   all indices of 
17979 70 54 61 62 20 74 68 61 74 20 75 73 65 20 74 68  pTab that use th
1797a 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  e collating sequ
1797b 65 6e 63 65 20 70 43 6f 6c 6c 2e 0a 2a 2a 20 49  ence pColl..** I
1797c 66 20 70 43 6f 6c 6c 3d 3d 30 20 74 68 65 6e 20  f pColl==0 then 
1797d 72 65 63 6f 6d 70 75 74 65 20 61 6c 6c 20 69 6e  recompute all in
1797e 64 69 63 65 73 20 6f 66 20 70 54 61 62 2e 0a 2a  dices of pTab..*
1797f 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
17980 5f 4f 4d 49 54 5f 52 45 49 4e 44 45 58 0a 73 74  _OMIT_REINDEX.st
17981 61 74 69 63 20 76 6f 69 64 20 72 65 69 6e 64 65  atic void reinde
17982 78 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70 50  xTable(Parse *pP
17983 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61  arse, Table *pTa
17984 62 2c 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a  b, char const *z
17985 43 6f 6c 6c 29 7b 0a 20 20 49 6e 64 65 78 20 2a  Coll){.  Index *
17986 70 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20 20  pIndex;         
17987 20 20 20 20 20 2f 2a 20 41 6e 20 69 6e 64 65 78       /* An index
17988 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
17989 20 70 54 61 62 20 2a 2f 0a 0a 20 20 66 6f 72 28   pTab */..  for(
1798a 70 49 6e 64 65 78 3d 70 54 61 62 2d 3e 70 49 6e  pIndex=pTab->pIn
1798b 64 65 78 3b 20 70 49 6e 64 65 78 3b 20 70 49 6e  dex; pIndex; pIn
1798c 64 65 78 3d 70 49 6e 64 65 78 2d 3e 70 4e 65 78  dex=pIndex->pNex
1798d 74 29 7b 0a 20 20 20 20 69 66 28 20 7a 43 6f 6c  t){.    if( zCol
1798e 6c 3d 3d 30 20 7c 7c 20 63 6f 6c 6c 61 74 69 6f  l==0 || collatio
1798f 6e 4d 61 74 63 68 28 7a 43 6f 6c 6c 2c 20 70 49  nMatch(zColl, pI
17990 6e 64 65 78 29 20 29 7b 0a 20 20 20 20 20 20 69  ndex) ){.      i
17991 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33  nt iDb = sqlite3
17992 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50  SchemaToIndex(pP
17993 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e  arse->db, pTab->
17994 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 20  pSchema);.      
17995 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74  sqlite3BeginWrit
17996 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73  eOperation(pPars
17997 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 20 20  e, 0, iDb);.    
17998 20 20 73 71 6c 69 74 65 33 52 65 66 69 6c 6c 49    sqlite3RefillI
17999 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70 49 6e  ndex(pParse, pIn
1799a 64 65 78 2c 20 2d 31 29 3b 0a 20 20 20 20 7d 0a  dex, -1);.    }.
1799b 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a    }.}.#endif../*
1799c 0a 2a 2a 20 52 65 63 6f 6d 70 75 74 65 20 61 6c  .** Recompute al
1799d 6c 20 69 6e 64 69 63 65 73 20 6f 66 20 61 6c 6c  l indices of all
1799e 20 74 61 62 6c 65 73 20 69 6e 20 61 6c 6c 20 64   tables in all d
1799f 61 74 61 62 61 73 65 73 20 77 68 65 72 65 20 74  atabases where t
179a0 68 65 0a 2a 2a 20 69 6e 64 69 63 65 73 20 75 73  he.** indices us
179a1 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20  e the collating 
179a2 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c 2e 20  sequence pColl. 
179a3 20 49 66 20 70 43 6f 6c 6c 3d 3d 30 20 74 68 65   If pColl==0 the
179a4 6e 20 72 65 63 6f 6d 70 75 74 65 0a 2a 2a 20 61  n recompute.** a
179a5 6c 6c 20 69 6e 64 69 63 65 73 20 65 76 65 72 79  ll indices every
179a6 77 68 65 72 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65  where..*/.#ifnde
179a7 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 52 45  f SQLITE_OMIT_RE
179a8 49 4e 44 45 58 0a 73 74 61 74 69 63 20 76 6f 69  INDEX.static voi
179a9 64 20 72 65 69 6e 64 65 78 44 61 74 61 62 61 73  d reindexDatabas
179aa 65 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  es(Parse *pParse
179ab 2c 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 43  , char const *zC
179ac 6f 6c 6c 29 7b 0a 20 20 44 62 20 2a 70 44 62 3b  oll){.  Db *pDb;
179ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
179ae 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20      /* A single 
179af 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e  database */.  in
179b0 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20  t iDb;          
179b1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
179b2 20 64 61 74 61 62 61 73 65 20 69 6e 64 65 78 20   database index 
179b3 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 73 71 6c 69  number */.  sqli
179b4 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
179b5 2d 3e 64 62 3b 20 20 20 2f 2a 20 54 68 65 20 64  ->db;   /* The d
179b6 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
179b7 6f 6e 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d  on */.  HashElem
179b8 20 2a 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20   *k;            
179b9 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69      /* For loopi
179ba 6e 67 20 6f 76 65 72 20 74 61 62 6c 65 73 20 69  ng over tables i
179bb 6e 20 70 44 62 20 2a 2f 0a 20 20 54 61 62 6c 65  n pDb */.  Table
179bc 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20   *pTab;         
179bd 20 20 20 20 20 20 20 2f 2a 20 41 20 74 61 62 6c         /* A tabl
179be 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  e in the databas
179bf 65 20 2a 2f 0a 0a 20 20 66 6f 72 28 69 44 62 3d  e */..  for(iDb=
179c0 30 2c 20 70 44 62 3d 64 62 2d 3e 61 44 62 3b 20  0, pDb=db->aDb; 
179c1 69 44 62 3c 64 62 2d 3e 6e 44 62 3b 20 69 44 62  iDb<db->nDb; iDb
179c2 2b 2b 2c 20 70 44 62 2b 2b 29 7b 0a 20 20 20 20  ++, pDb++){.    
179c3 61 73 73 65 72 74 28 20 70 44 62 21 3d 30 20 29  assert( pDb!=0 )
179c4 3b 0a 20 20 20 20 66 6f 72 28 6b 3d 73 71 6c 69  ;.    for(k=sqli
179c5 74 65 48 61 73 68 46 69 72 73 74 28 26 70 44 62  teHashFirst(&pDb
179c6 2d 3e 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61  ->pSchema->tblHa
179c7 73 68 29 3b 20 20 6b 3b 20 6b 3d 73 71 6c 69 74  sh);  k; k=sqlit
179c8 65 48 61 73 68 4e 65 78 74 28 6b 29 29 7b 0a 20  eHashNext(k)){. 
179c9 20 20 20 20 20 70 54 61 62 20 3d 20 28 54 61 62       pTab = (Tab
179ca 6c 65 2a 29 73 71 6c 69 74 65 48 61 73 68 44 61  le*)sqliteHashDa
179cb 74 61 28 6b 29 3b 0a 20 20 20 20 20 20 72 65 69  ta(k);.      rei
179cc 6e 64 65 78 54 61 62 6c 65 28 70 50 61 72 73 65  ndexTable(pParse
179cd 2c 20 70 54 61 62 2c 20 7a 43 6f 6c 6c 29 3b 0a  , pTab, zColl);.
179ce 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64      }.  }.}.#end
179cf 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  if../*.** Genera
179d0 74 65 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20  te code for the 
179d1 52 45 49 4e 44 45 58 20 63 6f 6d 6d 61 6e 64 2e  REINDEX command.
179d2 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 52 45  .**.**        RE
179d3 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 20 20  INDEX           
179d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
179d5 20 2d 2d 20 31 0a 2a 2a 20 20 20 20 20 20 20 20   -- 1.**        
179d6 52 45 49 4e 44 45 58 20 20 3c 63 6f 6c 6c 61 74  REINDEX  <collat
179d7 69 6f 6e 3e 20 20 20 20 20 20 20 20 20 20 20 20  ion>            
179d8 20 20 20 2d 2d 20 32 0a 2a 2a 20 20 20 20 20 20     -- 2.**      
179d9 20 20 52 45 49 4e 44 45 58 20 20 3f 3c 64 61 74    REINDEX  ?<dat
179da 61 62 61 73 65 3e 2e 3f 3c 74 61 62 6c 65 6e 61  abase>.?<tablena
179db 6d 65 3e 20 20 2d 2d 20 33 0a 2a 2a 20 20 20 20  me>  -- 3.**    
179dc 20 20 20 20 52 45 49 4e 44 45 58 20 20 3f 3c 64      REINDEX  ?<d
179dd 61 74 61 62 61 73 65 3e 2e 3f 3c 69 6e 64 65 78  atabase>.?<index
179de 6e 61 6d 65 3e 20 20 2d 2d 20 34 0a 2a 2a 0a 2a  name>  -- 4.**.*
179df 2a 20 46 6f 72 6d 20 31 20 63 61 75 73 65 73 20  * Form 1 causes 
179e0 61 6c 6c 20 69 6e 64 69 63 65 73 20 69 6e 20 61  all indices in a
179e1 6c 6c 20 61 74 74 61 63 68 65 64 20 64 61 74 61  ll attached data
179e2 62 61 73 65 73 20 74 6f 20 62 65 20 72 65 62 75  bases to be rebu
179e3 69 6c 74 2e 0a 2a 2a 20 46 6f 72 6d 20 32 20 72  ilt..** Form 2 r
179e4 65 62 75 69 6c 64 73 20 61 6c 6c 20 69 6e 64 69  ebuilds all indi
179e5 63 65 73 20 69 6e 20 61 6c 6c 20 64 61 74 61 62  ces in all datab
179e6 61 73 65 73 20 74 68 61 74 20 75 73 65 20 74 68  ases that use th
179e7 65 20 6e 61 6d 65 64 0a 2a 2a 20 63 6f 6c 6c 61  e named.** colla
179e8 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 2e 20 20  ting function.  
179e9 46 6f 72 6d 73 20 33 20 61 6e 64 20 34 20 72 65  Forms 3 and 4 re
179ea 62 75 69 6c 64 20 74 68 65 20 6e 61 6d 65 64 20  build the named 
179eb 69 6e 64 65 78 20 6f 72 20 61 6c 6c 0a 2a 2a 20  index or all.** 
179ec 69 6e 64 69 63 65 73 20 61 73 73 6f 63 69 61 74  indices associat
179ed 65 64 20 77 69 74 68 20 74 68 65 20 6e 61 6d 65  ed with the name
179ee 64 20 74 61 62 6c 65 2e 0a 2a 2f 0a 23 69 66 6e  d table..*/.#ifn
179ef 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
179f0 52 45 49 4e 44 45 58 0a 53 51 4c 49 54 45 5f 50  REINDEX.SQLITE_P
179f1 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
179f2 74 65 33 52 65 69 6e 64 65 78 28 50 61 72 73 65  te3Reindex(Parse
179f3 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20   *pParse, Token 
179f4 2a 70 4e 61 6d 65 31 2c 20 54 6f 6b 65 6e 20 2a  *pName1, Token *
179f5 70 4e 61 6d 65 32 29 7b 0a 20 20 43 6f 6c 6c 53  pName2){.  CollS
179f6 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20  eq *pColl;      
179f7 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74         /* Collat
179f8 69 6e 67 20 73 65 71 75 65 6e 63 65 20 74 6f 20  ing sequence to 
179f9 62 65 20 72 65 69 6e 64 65 78 65 64 2c 20 6f 72  be reindexed, or
179fa 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 68 61 72 20   NULL */.  char 
179fb 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  *z;             
179fc 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
179fd 66 20 61 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  f a table or ind
179fe 65 78 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ex */.  const ch
179ff 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 20 20  ar *zDb;        
17a00 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
17a01 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  he database */. 
17a02 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20   Table *pTab;   
17a03 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
17a04 41 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 64  A table in the d
17a05 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 49 6e 64  atabase */.  Ind
17a06 65 78 20 2a 70 49 6e 64 65 78 3b 20 20 20 20 20  ex *pIndex;     
17a07 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 69           /* An i
17a08 6e 64 65 78 20 61 73 73 6f 63 69 61 74 65 64 20  ndex associated 
17a09 77 69 74 68 20 70 54 61 62 20 2a 2f 0a 20 20 69  with pTab */.  i
17a0a 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20  nt iDb;         
17a0b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
17a0c 65 20 64 61 74 61 62 61 73 65 20 69 6e 64 65 78  e database index
17a0d 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 73 71 6c   number */.  sql
17a0e 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
17a0f 65 2d 3e 64 62 3b 20 20 20 2f 2a 20 54 68 65 20  e->db;   /* The 
17a10 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
17a11 69 6f 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  ion */.  Token *
17a12 70 4f 62 6a 4e 61 6d 65 3b 20 20 20 20 20 20 20  pObjName;       
17a13 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
17a14 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  the table or ind
17a15 65 78 20 74 6f 20 62 65 20 72 65 69 6e 64 65 78  ex to be reindex
17a16 65 64 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 61 64  ed */..  /* Read
17a17 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
17a18 68 65 6d 61 2e 20 49 66 20 61 6e 20 65 72 72 6f  hema. If an erro
17a19 72 20 6f 63 63 75 72 73 2c 20 6c 65 61 76 65 20  r occurs, leave 
17a1a 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
17a1b 0a 20 20 2a 2a 20 61 6e 64 20 63 6f 64 65 20 69  .  ** and code i
17a1c 6e 20 70 50 61 72 73 65 20 61 6e 64 20 72 65 74  n pParse and ret
17a1d 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 69  urn NULL. */.  i
17a1e 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71  f( SQLITE_OK!=sq
17a1f 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28  lite3ReadSchema(
17a20 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20 72  pParse) ){.    r
17a21 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 69 66  eturn;.  }..  if
17a22 28 20 70 4e 61 6d 65 31 3d 3d 30 20 7c 7c 20 70  ( pName1==0 || p
17a23 4e 61 6d 65 31 2d 3e 7a 3d 3d 30 20 29 7b 0a 20  Name1->z==0 ){. 
17a24 20 20 20 72 65 69 6e 64 65 78 44 61 74 61 62 61     reindexDataba
17a25 73 65 73 28 70 50 61 72 73 65 2c 20 30 29 3b 0a  ses(pParse, 0);.
17a26 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 65      return;.  }e
17a27 6c 73 65 20 69 66 28 20 70 4e 61 6d 65 32 3d 3d  lse if( pName2==
17a28 30 20 7c 7c 20 70 4e 61 6d 65 32 2d 3e 7a 3d 3d  0 || pName2->z==
17a29 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  0 ){.    assert(
17a2a 20 70 4e 61 6d 65 31 2d 3e 7a 20 29 3b 0a 20 20   pName1->z );.  
17a2b 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    pColl = sqlite
17a2c 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c  3FindCollSeq(db,
17a2d 20 45 4e 43 28 64 62 29 2c 20 28 63 68 61 72 2a   ENC(db), (char*
17a2e 29 70 4e 61 6d 65 31 2d 3e 7a 2c 20 70 4e 61 6d  )pName1->z, pNam
17a2f 65 31 2d 3e 6e 2c 20 30 29 3b 0a 20 20 20 20 69  e1->n, 0);.    i
17a30 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  f( pColl ){.    
17a31 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 20 3d 20    char *zColl = 
17a32 73 71 6c 69 74 65 53 74 72 4e 44 75 70 28 28 63  sqliteStrNDup((c
17a33 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 4e 61 6d  onst char *)pNam
17a34 65 31 2d 3e 7a 2c 20 70 4e 61 6d 65 31 2d 3e 6e  e1->z, pName1->n
17a35 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 43 6f  );.      if( zCo
17a36 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  ll ){.        re
17a37 69 6e 64 65 78 44 61 74 61 62 61 73 65 73 28 70  indexDatabases(p
17a38 50 61 72 73 65 2c 20 7a 43 6f 6c 6c 29 3b 0a 20  Parse, zColl);. 
17a39 20 20 20 20 20 20 20 73 71 6c 69 74 65 46 72 65         sqliteFre
17a3a 65 28 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 20 20  e(zColl);.      
17a3b 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  }.      return;.
17a3c 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 44 62 20      }.  }.  iDb 
17a3d 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74  = sqlite3TwoPart
17a3e 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61  Name(pParse, pNa
17a3f 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4f  me1, pName2, &pO
17a40 62 6a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 69  bjName);.  if( i
17a41 44 62 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  Db<0 ) return;. 
17a42 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65   z = sqlite3Name
17a43 46 72 6f 6d 54 6f 6b 65 6e 28 70 4f 62 6a 4e 61  FromToken(pObjNa
17a44 6d 65 29 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20  me);.  if( z==0 
17a45 29 20 72 65 74 75 72 6e 3b 0a 20 20 7a 44 62 20  ) return;.  zDb 
17a46 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  = db->aDb[iDb].z
17a47 4e 61 6d 65 3b 0a 20 20 70 54 61 62 20 3d 20 73  Name;.  pTab = s
17a48 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28  qlite3FindTable(
17a49 64 62 2c 20 7a 2c 20 7a 44 62 29 3b 0a 20 20 69  db, z, zDb);.  i
17a4a 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20 72  f( pTab ){.    r
17a4b 65 69 6e 64 65 78 54 61 62 6c 65 28 70 50 61 72  eindexTable(pPar
17a4c 73 65 2c 20 70 54 61 62 2c 20 30 29 3b 0a 20 20  se, pTab, 0);.  
17a4d 20 20 73 71 6c 69 74 65 46 72 65 65 28 7a 29 3b    sqliteFree(z);
17a4e 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
17a4f 0a 20 20 70 49 6e 64 65 78 20 3d 20 73 71 6c 69  .  pIndex = sqli
17a50 74 65 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c  te3FindIndex(db,
17a51 20 7a 2c 20 7a 44 62 29 3b 0a 20 20 73 71 6c 69   z, zDb);.  sqli
17a52 74 65 46 72 65 65 28 7a 29 3b 0a 20 20 69 66 28  teFree(z);.  if(
17a53 20 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 73   pIndex ){.    s
17a54 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65  qlite3BeginWrite
17a55 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65  Operation(pParse
17a56 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 20 20 73  , 0, iDb);.    s
17a57 71 6c 69 74 65 33 52 65 66 69 6c 6c 49 6e 64 65  qlite3RefillInde
17a58 78 28 70 50 61 72 73 65 2c 20 70 49 6e 64 65 78  x(pParse, pIndex
17a59 2c 20 2d 31 29 3b 0a 20 20 20 20 72 65 74 75 72  , -1);.    retur
17a5a 6e 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  n;.  }.  sqlite3
17a5b 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
17a5c 20 22 75 6e 61 62 6c 65 20 74 6f 20 69 64 65 6e   "unable to iden
17a5d 74 69 66 79 20 74 68 65 20 6f 62 6a 65 63 74 20  tify the object 
17a5e 74 6f 20 62 65 20 72 65 69 6e 64 65 78 65 64 22  to be reindexed"
17a5f 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  );.}.#endif../*.
17a60 2a 2a 20 52 65 74 75 72 6e 20 61 20 64 79 6e 61  ** Return a dyna
17a61 6d 69 63 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20  micly allocated 
17a62 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72  KeyInfo structur
17a63 65 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 73  e that can be us
17a64 65 64 0a 2a 2a 20 77 69 74 68 20 4f 50 5f 4f 70  ed.** with OP_Op
17a65 65 6e 52 65 61 64 20 6f 72 20 4f 50 5f 4f 70 65  enRead or OP_Ope
17a66 6e 57 72 69 74 65 20 74 6f 20 61 63 63 65 73 73  nWrite to access
17a67 20 64 61 74 61 62 61 73 65 20 69 6e 64 65 78 20   database index 
17a68 70 49 64 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73  pIdx..**.** If s
17a69 75 63 63 65 73 73 66 75 6c 2c 20 61 20 70 6f 69  uccessful, a poi
17a6a 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20  nter to the new 
17a6b 73 74 72 75 63 74 75 72 65 20 69 73 20 72 65 74  structure is ret
17a6c 75 72 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 63  urned. In this c
17a6d 61 73 65 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65  ase.** the calle
17a6e 72 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65  r is responsible
17a6f 20 66 6f 72 20 63 61 6c 6c 69 6e 67 20 73 71 6c   for calling sql
17a70 69 74 65 46 72 65 65 28 29 20 6f 6e 20 74 68 65  iteFree() on the
17a71 20 72 65 74 75 72 6e 65 64 20 0a 2a 2a 20 70 6f   returned .** po
17a72 69 6e 74 65 72 2e 20 49 66 20 61 6e 20 65 72 72  inter. If an err
17a73 6f 72 20 6f 63 63 75 72 73 20 28 6f 75 74 20 6f  or occurs (out o
17a74 66 20 6d 65 6d 6f 72 79 20 6f 72 20 6d 69 73 73  f memory or miss
17a75 69 6e 67 20 63 6f 6c 6c 61 74 69 6f 6e 20 0a 2a  ing collation .*
17a76 2a 20 73 65 71 75 65 6e 63 65 29 2c 20 4e 55 4c  * sequence), NUL
17a77 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  L is returned an
17a78 64 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 70  d the state of p
17a79 50 61 72 73 65 20 75 70 64 61 74 65 64 20 74 6f  Parse updated to
17a7a 20 72 65 66 6c 65 63 74 0a 2a 2a 20 74 68 65 20   reflect.** the 
17a7b 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  error..*/.SQLITE
17a7c 5f 50 52 49 56 41 54 45 20 4b 65 79 49 6e 66 6f  _PRIVATE KeyInfo
17a7d 20 2a 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65   *sqlite3IndexKe
17a7e 79 69 6e 66 6f 28 50 61 72 73 65 20 2a 70 50 61  yinfo(Parse *pPa
17a7f 72 73 65 2c 20 49 6e 64 65 78 20 2a 70 49 64 78  rse, Index *pIdx
17a80 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  ){.  int i;.  in
17a81 74 20 6e 43 6f 6c 20 3d 20 70 49 64 78 2d 3e 6e  t nCol = pIdx->n
17a82 43 6f 6c 75 6d 6e 3b 0a 20 20 69 6e 74 20 6e 42  Column;.  int nB
17a83 79 74 65 73 20 3d 20 73 69 7a 65 6f 66 28 4b 65  ytes = sizeof(Ke
17a84 79 49 6e 66 6f 29 20 2b 20 28 6e 43 6f 6c 2d 31  yInfo) + (nCol-1
17a85 29 2a 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71  )*sizeof(CollSeq
17a86 2a 29 20 2b 20 6e 43 6f 6c 3b 0a 20 20 4b 65 79  *) + nCol;.  Key
17a87 49 6e 66 6f 20 2a 70 4b 65 79 20 3d 20 28 4b 65  Info *pKey = (Ke
17a88 79 49 6e 66 6f 20 2a 29 73 71 6c 69 74 65 4d 61  yInfo *)sqliteMa
17a89 6c 6c 6f 63 28 6e 42 79 74 65 73 29 3b 0a 0a 20  lloc(nBytes);.. 
17a8a 20 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 20 20   if( pKey ){.   
17a8b 20 70 4b 65 79 2d 3e 61 53 6f 72 74 4f 72 64 65   pKey->aSortOrde
17a8c 72 20 3d 20 28 75 38 20 2a 29 26 28 70 4b 65 79  r = (u8 *)&(pKey
17a8d 2d 3e 61 43 6f 6c 6c 5b 6e 43 6f 6c 5d 29 3b 0a  ->aColl[nCol]);.
17a8e 20 20 20 20 61 73 73 65 72 74 28 20 26 70 4b 65      assert( &pKe
17a8f 79 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 6e 43  y->aSortOrder[nC
17a90 6f 6c 5d 3d 3d 26 28 28 28 75 38 20 2a 29 70 4b  ol]==&(((u8 *)pK
17a91 65 79 29 5b 6e 42 79 74 65 73 5d 29 20 29 3b 0a  ey)[nBytes]) );.
17a92 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
17a93 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Col; i++){.     
17a94 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 20 3d 20 70   char *zColl = p
17a95 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 3b 0a  Idx->azColl[i];.
17a96 20 20 20 20 20 20 61 73 73 65 72 74 28 20 7a 43        assert( zC
17a97 6f 6c 6c 20 29 3b 0a 20 20 20 20 20 20 70 4b 65  oll );.      pKe
17a98 79 2d 3e 61 43 6f 6c 6c 5b 69 5d 20 3d 20 73 71  y->aColl[i] = sq
17a99 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53  lite3LocateCollS
17a9a 65 71 28 70 50 61 72 73 65 2c 20 7a 43 6f 6c 6c  eq(pParse, zColl
17a9b 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 70 4b 65  , -1);.      pKe
17a9c 79 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d  y->aSortOrder[i]
17a9d 20 3d 20 70 49 64 78 2d 3e 61 53 6f 72 74 4f 72   = pIdx->aSortOr
17a9e 64 65 72 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20  der[i];.    }.  
17a9f 20 20 70 4b 65 79 2d 3e 6e 46 69 65 6c 64 20 3d    pKey->nField =
17aa0 20 6e 43 6f 6c 3b 0a 20 20 7d 0a 0a 20 20 69 66   nCol;.  }..  if
17aa1 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29  ( pParse->nErr )
17aa2 7b 0a 20 20 20 20 73 71 6c 69 74 65 46 72 65 65  {.    sqliteFree
17aa3 28 70 4b 65 79 29 3b 0a 20 20 20 20 70 4b 65 79  (pKey);.    pKey
17aa4 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 0;.  }.  retu
17aa5 72 6e 20 70 4b 65 79 3b 0a 7d 0a 0a 2f 2a 2a 2a  rn pKey;.}../***
17aa6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
17aa7 6f 66 20 62 75 69 6c 64 2e 63 20 2a 2a 2a 2a 2a  of build.c *****
17aa8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17aa9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17aaa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
17aab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
17aac 6e 20 66 69 6c 65 20 63 61 6c 6c 62 61 63 6b 2e  n file callback.
17aad 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
17aae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17aaf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
17ab0 2a 20 32 30 30 35 20 4d 61 79 20 32 33 20 0a 2a  * 2005 May 23 .*
17ab1 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
17ab2 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
17ab3 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
17ab4 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
17ab5 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
17ab6 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
17ab7 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
17ab8 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
17ab9 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
17aba 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
17abb 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
17abc 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
17abd 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
17abe 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
17abf 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
17ac0 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
17ac1 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
17ac2 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
17ac3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17ac4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17ac5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17ac6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
17ac7 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
17ac8 6e 74 61 69 6e 73 20 66 75 6e 63 74 69 6f 6e 73  ntains functions
17ac9 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20   used to access 
17aca 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 68 61 73  the internal has
17acb 68 20 74 61 62 6c 65 73 0a 2a 2a 20 6f 66 20 75  h tables.** of u
17acc 73 65 72 20 64 65 66 69 6e 65 64 20 66 75 6e 63  ser defined func
17acd 74 69 6f 6e 73 20 61 6e 64 20 63 6f 6c 6c 61 74  tions and collat
17ace 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 2e 0a 2a  ion sequences..*
17acf 2a 0a 2a 2a 20 24 49 64 3a 20 63 61 6c 6c 62 61  *.** $Id: callba
17ad0 63 6b 2e 63 2c 76 20 31 2e 31 38 20 32 30 30 37  ck.c,v 1.18 2007
17ad1 2f 30 35 2f 30 37 20 30 39 3a 33 32 3a 34 35 20  /05/07 09:32:45 
17ad2 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20  danielk1977 Exp 
17ad3 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76  $.*/.../*.** Inv
17ad4 6f 6b 65 20 74 68 65 20 27 63 6f 6c 6c 61 74 69  oke the 'collati
17ad5 6f 6e 20 6e 65 65 64 65 64 27 20 63 61 6c 6c 62  on needed' callb
17ad6 61 63 6b 20 74 6f 20 72 65 71 75 65 73 74 20 61  ack to request a
17ad7 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
17ad8 6e 63 65 0a 2a 2a 20 69 6e 20 74 68 65 20 64 61  nce.** in the da
17ad9 74 61 62 61 73 65 20 74 65 78 74 20 65 6e 63 6f  tabase text enco
17ada 64 69 6e 67 20 6f 66 20 6e 61 6d 65 20 7a 4e 61  ding of name zNa
17adb 6d 65 2c 20 6c 65 6e 67 74 68 20 6e 4e 61 6d 65  me, length nName
17adc 2e 0a 2a 2a 20 49 66 20 74 68 65 20 63 6f 6c 6c  ..** If the coll
17add 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 0a 2a  ation sequence.*
17ade 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 61  /.static void ca
17adf 6c 6c 43 6f 6c 6c 4e 65 65 64 65 64 28 73 71 6c  llCollNeeded(sql
17ae0 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20  ite3 *db, const 
17ae1 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74  char *zName, int
17ae2 20 6e 4e 61 6d 65 29 7b 0a 20 20 61 73 73 65 72   nName){.  asser
17ae3 74 28 20 21 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65  t( !db->xCollNee
17ae4 64 65 64 20 7c 7c 20 21 64 62 2d 3e 78 43 6f 6c  ded || !db->xCol
17ae5 6c 4e 65 65 64 65 64 31 36 20 29 3b 0a 20 20 69  lNeeded16 );.  i
17ae6 66 28 20 6e 4e 61 6d 65 3c 30 20 29 20 6e 4e 61  f( nName<0 ) nNa
17ae7 6d 65 20 3d 20 73 74 72 6c 65 6e 28 7a 4e 61 6d  me = strlen(zNam
17ae8 65 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 78 43  e);.  if( db->xC
17ae9 6f 6c 6c 4e 65 65 64 65 64 20 29 7b 0a 20 20 20  ollNeeded ){.   
17aea 20 63 68 61 72 20 2a 7a 45 78 74 65 72 6e 61 6c   char *zExternal
17aeb 20 3d 20 73 71 6c 69 74 65 53 74 72 4e 44 75 70   = sqliteStrNDup
17aec 28 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a  (zName, nName);.
17aed 20 20 20 20 69 66 28 20 21 7a 45 78 74 65 72 6e      if( !zExtern
17aee 61 6c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20  al ) return;.   
17aef 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64   db->xCollNeeded
17af0 28 64 62 2d 3e 70 43 6f 6c 6c 4e 65 65 64 65 64  (db->pCollNeeded
17af1 41 72 67 2c 20 64 62 2c 20 28 69 6e 74 29 45 4e  Arg, db, (int)EN
17af2 43 28 64 62 29 2c 20 7a 45 78 74 65 72 6e 61 6c  C(db), zExternal
17af3 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 46 72 65  );.    sqliteFre
17af4 65 28 7a 45 78 74 65 72 6e 61 6c 29 3b 0a 20 20  e(zExternal);.  
17af5 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
17af6 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20 69 66  _OMIT_UTF16.  if
17af7 28 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65  ( db->xCollNeede
17af8 64 31 36 20 29 7b 0a 20 20 20 20 63 68 61 72 20  d16 ){.    char 
17af9 63 6f 6e 73 74 20 2a 7a 45 78 74 65 72 6e 61 6c  const *zExternal
17afa 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61  ;.    sqlite3_va
17afb 6c 75 65 20 2a 70 54 6d 70 20 3d 20 73 71 6c 69  lue *pTmp = sqli
17afc 74 65 33 56 61 6c 75 65 4e 65 77 28 29 3b 0a 20  te3ValueNew();. 
17afd 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53     sqlite3ValueS
17afe 65 74 53 74 72 28 70 54 6d 70 2c 20 6e 4e 61 6d  etStr(pTmp, nNam
17aff 65 2c 20 7a 4e 61 6d 65 2c 20 53 51 4c 49 54 45  e, zName, SQLITE
17b00 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 54  _UTF8, SQLITE_ST
17b01 41 54 49 43 29 3b 0a 20 20 20 20 7a 45 78 74 65  ATIC);.    zExte
17b02 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61  rnal = sqlite3Va
17b03 6c 75 65 54 65 78 74 28 70 54 6d 70 2c 20 53 51  lueText(pTmp, SQ
17b04 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45  LITE_UTF16NATIVE
17b05 29 3b 0a 20 20 20 20 69 66 28 20 7a 45 78 74 65  );.    if( zExte
17b06 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 64 62  rnal ){.      db
17b07 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 28  ->xCollNeeded16(
17b08 64 62 2d 3e 70 43 6f 6c 6c 4e 65 65 64 65 64 41  db->pCollNeededA
17b09 72 67 2c 20 64 62 2c 20 28 69 6e 74 29 45 4e 43  rg, db, (int)ENC
17b0a 28 64 62 29 2c 20 7a 45 78 74 65 72 6e 61 6c 29  (db), zExternal)
17b0b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
17b0c 74 65 33 56 61 6c 75 65 46 72 65 65 28 70 54 6d  te3ValueFree(pTm
17b0d 70 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d  p);.  }.#endif.}
17b0e 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
17b0f 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 69  tine is called i
17b10 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  f the collation 
17b11 66 61 63 74 6f 72 79 20 66 61 69 6c 73 20 74 6f  factory fails to
17b12 20 64 65 6c 69 76 65 72 20 61 0a 2a 2a 20 63 6f   deliver a.** co
17b13 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e  llation function
17b14 20 69 6e 20 74 68 65 20 62 65 73 74 20 65 6e 63   in the best enc
17b15 6f 64 69 6e 67 20 62 75 74 20 74 68 65 72 65 20  oding but there 
17b16 6d 61 79 20 62 65 20 6f 74 68 65 72 20 76 65 72  may be other ver
17b17 73 69 6f 6e 73 0a 2a 2a 20 6f 66 20 74 68 69 73  sions.** of this
17b18 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74   collation funct
17b19 69 6f 6e 20 28 66 6f 72 20 6f 74 68 65 72 20 74  ion (for other t
17b1a 65 78 74 20 65 6e 63 6f 64 69 6e 67 73 29 20 61  ext encodings) a
17b1b 76 61 69 6c 61 62 6c 65 2e 20 55 73 65 20 6f 6e  vailable. Use on
17b1c 65 0a 2a 2a 20 6f 66 20 74 68 65 73 65 20 69 6e  e.** of these in
17b1d 73 74 65 61 64 20 69 66 20 74 68 65 79 20 65 78  stead if they ex
17b1e 69 73 74 2e 20 41 76 6f 69 64 20 61 20 55 54 46  ist. Avoid a UTF
17b1f 2d 38 20 3c 2d 3e 20 55 54 46 2d 31 36 20 63 6f  -8 <-> UTF-16 co
17b20 6e 76 65 72 73 69 6f 6e 20 69 66 0a 2a 2a 20 70  nversion if.** p
17b21 6f 73 73 69 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74  ossible..*/.stat
17b22 69 63 20 69 6e 74 20 73 79 6e 74 68 43 6f 6c 6c  ic int synthColl
17b23 53 65 71 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  Seq(sqlite3 *db,
17b24 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 29   CollSeq *pColl)
17b25 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  {.  CollSeq *pCo
17b26 6c 6c 32 3b 0a 20 20 63 68 61 72 20 2a 7a 20 3d  ll2;.  char *z =
17b27 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 3b 0a 20   pColl->zName;. 
17b28 20 69 6e 74 20 6e 20 3d 20 73 74 72 6c 65 6e 28   int n = strlen(
17b29 7a 29 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73  z);.  int i;.  s
17b2a 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20 61  tatic const u8 a
17b2b 45 6e 63 5b 5d 20 3d 20 7b 20 53 51 4c 49 54 45  Enc[] = { SQLITE
17b2c 5f 55 54 46 31 36 42 45 2c 20 53 51 4c 49 54 45  _UTF16BE, SQLITE
17b2d 5f 55 54 46 31 36 4c 45 2c 20 53 51 4c 49 54 45  _UTF16LE, SQLITE
17b2e 5f 55 54 46 38 20 7d 3b 0a 20 20 66 6f 72 28 69  _UTF8 };.  for(i
17b2f 3d 30 3b 20 69 3c 33 3b 20 69 2b 2b 29 7b 0a 20  =0; i<3; i++){. 
17b30 20 20 20 70 43 6f 6c 6c 32 20 3d 20 73 71 6c 69     pColl2 = sqli
17b31 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64  te3FindCollSeq(d
17b32 62 2c 20 61 45 6e 63 5b 69 5d 2c 20 7a 2c 20 6e  b, aEnc[i], z, n
17b33 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 43  , 0);.    if( pC
17b34 6f 6c 6c 32 2d 3e 78 43 6d 70 21 3d 30 20 29 7b  oll2->xCmp!=0 ){
17b35 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 43  .      memcpy(pC
17b36 6f 6c 6c 2c 20 70 43 6f 6c 6c 32 2c 20 73 69 7a  oll, pColl2, siz
17b37 65 6f 66 28 43 6f 6c 6c 53 65 71 29 29 3b 0a 20  eof(CollSeq));. 
17b38 20 20 20 20 20 70 43 6f 6c 6c 2d 3e 78 44 65 6c       pColl->xDel
17b39 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a   = 0;         /*
17b3a 20 44 6f 20 6e 6f 74 20 63 6f 70 79 20 74 68 65   Do not copy the
17b3b 20 64 65 73 74 72 75 63 74 6f 72 20 2a 2f 0a 20   destructor */. 
17b3c 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
17b3d 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d  TE_OK;.    }.  }
17b3e 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
17b3f 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  _ERROR;.}../*.**
17b40 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
17b41 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f  s responsible fo
17b42 72 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 63  r invoking the c
17b43 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79  ollation factory
17b44 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 6f 72 20   callback.** or 
17b45 73 75 62 73 74 69 74 75 74 69 6e 67 20 61 20 63  substituting a c
17b46 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
17b47 65 20 6f 66 20 61 20 64 69 66 66 65 72 65 6e 74  e of a different
17b48 20 65 6e 63 6f 64 69 6e 67 20 77 68 65 6e 20 74   encoding when t
17b49 68 65 0a 2a 2a 20 72 65 71 75 65 73 74 65 64 20  he.** requested 
17b4a 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
17b4b 63 65 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61  ce is not availa
17b4c 62 6c 65 20 69 6e 20 74 68 65 20 64 61 74 61 62  ble in the datab
17b4d 61 73 65 20 6e 61 74 69 76 65 0a 2a 2a 20 65 6e  ase native.** en
17b4e 63 6f 64 69 6e 67 2e 0a 2a 2a 20 0a 2a 2a 20 49  coding..** .** I
17b4f 66 20 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  f it is not NULL
17b50 2c 20 74 68 65 6e 20 70 43 6f 6c 6c 20 6d 75 73  , then pColl mus
17b51 74 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 64  t point to the d
17b52 61 74 61 62 61 73 65 20 6e 61 74 69 76 65 20 65  atabase native e
17b53 6e 63 6f 64 69 6e 67 20 0a 2a 2a 20 63 6f 6c 6c  ncoding .** coll
17b54 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77  ation sequence w
17b55 69 74 68 20 6e 61 6d 65 20 7a 4e 61 6d 65 2c 20  ith name zName, 
17b56 6c 65 6e 67 74 68 20 6e 4e 61 6d 65 2e 0a 2a 2a  length nName..**
17b57 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76  .** The return v
17b58 61 6c 75 65 20 69 73 20 65 69 74 68 65 72 20 74  alue is either t
17b59 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  he collation seq
17b5a 75 65 6e 63 65 20 74 6f 20 62 65 20 75 73 65 64  uence to be used
17b5b 20 69 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20   in database.** 
17b5c 64 62 20 66 6f 72 20 63 6f 6c 6c 61 74 69 6f 6e  db for collation
17b5d 20 74 79 70 65 20 6e 61 6d 65 20 7a 4e 61 6d 65   type name zName
17b5e 2c 20 6c 65 6e 67 74 68 20 6e 4e 61 6d 65 2c 20  , length nName, 
17b5f 6f 72 20 4e 55 4c 4c 2c 20 69 66 20 6e 6f 20 63  or NULL, if no c
17b60 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65 71 75  ollation.** sequ
17b61 65 6e 63 65 20 63 61 6e 20 62 65 20 66 6f 75 6e  ence can be foun
17b62 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
17b63 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71  VATE CollSeq *sq
17b64 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53 65 71 28  lite3GetCollSeq(
17b65 0a 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 20  .  sqlite3* db, 
17b66 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c  .  CollSeq *pCol
17b67 6c 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  l, .  const char
17b68 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20 69 6e 74 20   *zName, .  int 
17b69 6e 4e 61 6d 65 0a 29 7b 0a 20 20 43 6f 6c 6c 53  nName.){.  CollS
17b6a 65 71 20 2a 70 3b 0a 0a 20 20 70 20 3d 20 70 43  eq *p;..  p = pC
17b6b 6f 6c 6c 3b 0a 20 20 69 66 28 20 21 70 20 29 7b  oll;.  if( !p ){
17b6c 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33  .    p = sqlite3
17b6d 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20  FindCollSeq(db, 
17b6e 45 4e 43 28 64 62 29 2c 20 7a 4e 61 6d 65 2c 20  ENC(db), zName, 
17b6f 6e 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a 20  nName, 0);.  }. 
17b70 20 69 66 28 20 21 70 20 7c 7c 20 21 70 2d 3e 78   if( !p || !p->x
17b71 43 6d 70 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f  Cmp ){.    /* No
17b72 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
17b73 6e 63 65 20 6f 66 20 74 68 69 73 20 74 79 70 65  nce of this type
17b74 20 66 6f 72 20 74 68 69 73 20 65 6e 63 6f 64 69   for this encodi
17b75 6e 67 20 69 73 20 72 65 67 69 73 74 65 72 65 64  ng is registered
17b76 2e 0a 20 20 20 20 2a 2a 20 43 61 6c 6c 20 74 68  ..    ** Call th
17b77 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74  e collation fact
17b78 6f 72 79 20 74 6f 20 73 65 65 20 69 66 20 69 74  ory to see if it
17b79 20 63 61 6e 20 73 75 70 70 6c 79 20 75 73 20 77   can supply us w
17b7a 69 74 68 20 6f 6e 65 2e 0a 20 20 20 20 2a 2f 0a  ith one..    */.
17b7b 20 20 20 20 63 61 6c 6c 43 6f 6c 6c 4e 65 65 64      callCollNeed
17b7c 65 64 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 6e 4e  ed(db, zName, nN
17b7d 61 6d 65 29 3b 0a 20 20 20 20 70 20 3d 20 73 71  ame);.    p = sq
17b7e 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71  lite3FindCollSeq
17b7f 28 64 62 2c 20 45 4e 43 28 64 62 29 2c 20 7a 4e  (db, ENC(db), zN
17b80 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 30 29 3b 0a  ame, nName, 0);.
17b81 20 20 7d 0a 20 20 69 66 28 20 70 20 26 26 20 21    }.  if( p && !
17b82 70 2d 3e 78 43 6d 70 20 26 26 20 73 79 6e 74 68  p->xCmp && synth
17b83 43 6f 6c 6c 53 65 71 28 64 62 2c 20 70 29 20 29  CollSeq(db, p) )
17b84 7b 0a 20 20 20 20 70 20 3d 20 30 3b 0a 20 20 7d  {.    p = 0;.  }
17b85 0a 20 20 61 73 73 65 72 74 28 20 21 70 20 7c 7c  .  assert( !p ||
17b86 20 70 2d 3e 78 43 6d 70 20 29 3b 0a 20 20 72 65   p->xCmp );.  re
17b87 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn p;.}../*.**
17b88 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
17b89 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 63 6f 6c   called on a col
17b8a 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
17b8b 62 65 66 6f 72 65 20 69 74 20 69 73 20 75 73 65  before it is use
17b8c 64 20 74 6f 0a 2a 2a 20 63 68 65 63 6b 20 74 68  d to.** check th
17b8d 61 74 20 69 74 20 69 73 20 64 65 66 69 6e 65 64  at it is defined
17b8e 2e 20 41 6e 20 75 6e 64 65 66 69 6e 65 64 20 63  . An undefined c
17b8f 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
17b90 65 20 65 78 69 73 74 73 20 77 68 65 6e 0a 2a 2a  e exists when.**
17b91 20 61 20 64 61 74 61 62 61 73 65 20 69 73 20 6c   a database is l
17b92 6f 61 64 65 64 20 74 68 61 74 20 63 6f 6e 74 61  oaded that conta
17b93 69 6e 73 20 72 65 66 65 72 65 6e 63 65 73 20 74  ins references t
17b94 6f 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  o collation sequ
17b95 65 6e 63 65 73 0a 2a 2a 20 74 68 61 74 20 68 61  ences.** that ha
17b96 76 65 20 6e 6f 74 20 62 65 65 6e 20 64 65 66 69  ve not been defi
17b97 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63  ned by sqlite3_c
17b98 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28  reate_collation(
17b99 29 20 65 74 63 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ) etc..**.** If 
17b9a 72 65 71 75 69 72 65 64 2c 20 74 68 69 73 20 72  required, this r
17b9b 6f 75 74 69 6e 65 20 63 61 6c 6c 73 20 74 68 65  outine calls the
17b9c 20 27 63 6f 6c 6c 61 74 69 6f 6e 20 6e 65 65 64   'collation need
17b9d 65 64 27 20 63 61 6c 6c 62 61 63 6b 20 74 6f 0a  ed' callback to.
17b9e 2a 2a 20 72 65 71 75 65 73 74 20 61 20 64 65 66  ** request a def
17b9f 69 6e 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 63  inition of the c
17ba0 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
17ba1 65 2e 20 49 66 20 74 68 69 73 20 64 6f 65 73 6e  e. If this doesn
17ba2 27 74 20 77 6f 72 6b 2c 20 0a 2a 2a 20 61 6e 20  't work, .** an 
17ba3 65 71 75 69 76 61 6c 65 6e 74 20 63 6f 6c 6c 61  equivalent colla
17ba4 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 74 68  ting sequence th
17ba5 61 74 20 75 73 65 73 20 61 20 74 65 78 74 20 65  at uses a text e
17ba6 6e 63 6f 64 69 6e 67 20 64 69 66 66 65 72 65 6e  ncoding differen
17ba7 74 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 6d 61  t.** from the ma
17ba8 69 6e 20 64 61 74 61 62 61 73 65 20 69 73 20 73  in database is s
17ba9 75 62 73 74 69 74 75 74 65 64 2c 20 69 66 20 6f  ubstituted, if o
17baa 6e 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e  ne is available.
17bab 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
17bac 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 68  TE int sqlite3Ch
17bad 65 63 6b 43 6f 6c 6c 53 65 71 28 50 61 72 73 65  eckCollSeq(Parse
17bae 20 2a 70 50 61 72 73 65 2c 20 43 6f 6c 6c 53 65   *pParse, CollSe
17baf 71 20 2a 70 43 6f 6c 6c 29 7b 0a 20 20 69 66 28  q *pColl){.  if(
17bb0 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 63 6f   pColl ){.    co
17bb1 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20  nst char *zName 
17bb2 3d 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 3b 0a  = pColl->zName;.
17bb3 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 20 3d      CollSeq *p =
17bb4 20 73 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53   sqlite3GetCollS
17bb5 65 71 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  eq(pParse->db, p
17bb6 43 6f 6c 6c 2c 20 7a 4e 61 6d 65 2c 20 2d 31 29  Coll, zName, -1)
17bb7 3b 0a 20 20 20 20 69 66 28 20 21 70 20 29 7b 0a  ;.    if( !p ){.
17bb8 20 20 20 20 20 20 69 66 28 20 70 50 61 72 73 65        if( pParse
17bb9 2d 3e 6e 45 72 72 3d 3d 30 20 29 7b 0a 20 20 20  ->nErr==0 ){.   
17bba 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
17bbb 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f  rMsg(pParse, "no
17bbc 20 73 75 63 68 20 63 6f 6c 6c 61 74 69 6f 6e 20   such collation 
17bbd 73 65 71 75 65 6e 63 65 3a 20 25 73 22 2c 20 7a  sequence: %s", z
17bbe 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20  Name);.      }. 
17bbf 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72       pParse->nEr
17bc0 72 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74 75 72  r++;.      retur
17bc1 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
17bc2 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
17bc3 28 20 70 3d 3d 70 43 6f 6c 6c 20 29 3b 0a 20 20  ( p==pColl );.  
17bc4 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
17bc5 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a  E_OK;.}..../*.**
17bc6 20 4c 6f 63 61 74 65 20 61 6e 64 20 72 65 74 75   Locate and retu
17bc7 72 6e 20 61 6e 20 65 6e 74 72 79 20 66 72 6f 6d  rn an entry from
17bc8 20 74 68 65 20 64 62 2e 61 43 6f 6c 6c 53 65 71   the db.aCollSeq
17bc9 20 68 61 73 68 20 74 61 62 6c 65 2e 20 49 66 20   hash table. If 
17bca 74 68 65 20 65 6e 74 72 79 0a 2a 2a 20 73 70 65  the entry.** spe
17bcb 63 69 66 69 65 64 20 62 79 20 7a 4e 61 6d 65 20  cified by zName 
17bcc 61 6e 64 20 6e 4e 61 6d 65 20 69 73 20 6e 6f 74  and nName is not
17bcd 20 66 6f 75 6e 64 20 61 6e 64 20 70 61 72 61 6d   found and param
17bce 65 74 65 72 20 27 63 72 65 61 74 65 27 20 69 73  eter 'create' is
17bcf 0a 2a 2a 20 74 72 75 65 2c 20 74 68 65 6e 20 63  .** true, then c
17bd0 72 65 61 74 65 20 61 20 6e 65 77 20 65 6e 74 72  reate a new entr
17bd1 79 2e 20 4f 74 68 65 72 77 69 73 65 20 72 65 74  y. Otherwise ret
17bd2 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20  urn NULL..**.** 
17bd3 45 61 63 68 20 70 6f 69 6e 74 65 72 20 73 74 6f  Each pointer sto
17bd4 72 65 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74  red in the sqlit
17bd5 65 33 2e 61 43 6f 6c 6c 53 65 71 20 68 61 73 68  e3.aCollSeq hash
17bd6 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 73 20   table contains 
17bd7 61 6e 0a 2a 2a 20 61 72 72 61 79 20 6f 66 20 74  an.** array of t
17bd8 68 72 65 65 20 43 6f 6c 6c 53 65 71 20 73 74 72  hree CollSeq str
17bd9 75 63 74 75 72 65 73 2e 20 54 68 65 20 66 69 72  uctures. The fir
17bda 73 74 20 69 73 20 74 68 65 20 63 6f 6c 6c 61 74  st is the collat
17bdb 69 6f 6e 20 73 65 71 75 65 6e 63 65 0a 2a 2a 20  ion sequence.** 
17bdc 70 72 65 66 66 65 72 72 65 64 20 66 6f 72 20 55  prefferred for U
17bdd 54 46 2d 38 2c 20 74 68 65 20 73 65 63 6f 6e 64  TF-8, the second
17bde 20 55 54 46 2d 31 36 6c 65 2c 20 61 6e 64 20 74   UTF-16le, and t
17bdf 68 65 20 74 68 69 72 64 20 55 54 46 2d 31 36 62  he third UTF-16b
17be0 65 2e 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65 64 20  e..**.** Stored 
17be1 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65  immediately afte
17be2 72 20 74 68 65 20 74 68 72 65 65 20 63 6f 6c 6c  r the three coll
17be3 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 20  ation sequences 
17be4 69 73 20 61 20 63 6f 70 79 20 6f 66 0a 2a 2a 20  is a copy of.** 
17be5 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  the collation se
17be6 71 75 65 6e 63 65 20 6e 61 6d 65 2e 20 41 20 70  quence name. A p
17be7 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73  ointer to this s
17be8 74 72 69 6e 67 20 69 73 20 73 74 6f 72 65 64 20  tring is stored 
17be9 69 6e 0a 2a 2a 20 65 61 63 68 20 63 6f 6c 6c 61  in.** each colla
17bea 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 73 74  tion sequence st
17beb 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 61 74  ructure..*/.stat
17bec 69 63 20 43 6f 6c 6c 53 65 71 20 2a 66 69 6e 64  ic CollSeq *find
17bed 43 6f 6c 6c 53 65 71 45 6e 74 72 79 28 0a 20 20  CollSeqEntry(.  
17bee 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63  sqlite3 *db,.  c
17bef 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
17bf0 2c 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 2c 0a 20  ,.  int nName,. 
17bf1 20 69 6e 74 20 63 72 65 61 74 65 0a 29 7b 0a 20   int create.){. 
17bf2 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b   CollSeq *pColl;
17bf3 0a 20 20 69 66 28 20 6e 4e 61 6d 65 3c 30 20 29  .  if( nName<0 )
17bf4 20 6e 4e 61 6d 65 20 3d 20 73 74 72 6c 65 6e 28   nName = strlen(
17bf5 7a 4e 61 6d 65 29 3b 0a 20 20 70 43 6f 6c 6c 20  zName);.  pColl 
17bf6 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e  = sqlite3HashFin
17bf7 64 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 2c  d(&db->aCollSeq,
17bf8 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a   zName, nName);.
17bf9 0a 20 20 69 66 28 20 30 3d 3d 70 43 6f 6c 6c 20  .  if( 0==pColl 
17bfa 26 26 20 63 72 65 61 74 65 20 29 7b 0a 20 20 20  && create ){.   
17bfb 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 4d   pColl = sqliteM
17bfc 61 6c 6c 6f 63 28 20 33 2a 73 69 7a 65 6f 66 28  alloc( 3*sizeof(
17bfd 2a 70 43 6f 6c 6c 29 20 2b 20 6e 4e 61 6d 65 20  *pColl) + nName 
17bfe 2b 20 31 20 29 3b 0a 20 20 20 20 69 66 28 20 70  + 1 );.    if( p
17bff 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 43 6f  Coll ){.      Co
17c00 6c 6c 53 65 71 20 2a 70 44 65 6c 20 3d 20 30 3b  llSeq *pDel = 0;
17c01 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 30 5d 2e  .      pColl[0].
17c02 7a 4e 61 6d 65 20 3d 20 28 63 68 61 72 2a 29 26  zName = (char*)&
17c03 70 43 6f 6c 6c 5b 33 5d 3b 0a 20 20 20 20 20 20  pColl[3];.      
17c04 70 43 6f 6c 6c 5b 30 5d 2e 65 6e 63 20 3d 20 53  pColl[0].enc = S
17c05 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 20  QLITE_UTF8;.    
17c06 20 20 70 43 6f 6c 6c 5b 31 5d 2e 7a 4e 61 6d 65    pColl[1].zName
17c07 20 3d 20 28 63 68 61 72 2a 29 26 70 43 6f 6c 6c   = (char*)&pColl
17c08 5b 33 5d 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c  [3];.      pColl
17c09 5b 31 5d 2e 65 6e 63 20 3d 20 53 51 4c 49 54 45  [1].enc = SQLITE
17c0a 5f 55 54 46 31 36 4c 45 3b 0a 20 20 20 20 20 20  _UTF16LE;.      
17c0b 70 43 6f 6c 6c 5b 32 5d 2e 7a 4e 61 6d 65 20 3d  pColl[2].zName =
17c0c 20 28 63 68 61 72 2a 29 26 70 43 6f 6c 6c 5b 33   (char*)&pColl[3
17c0d 5d 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 32  ];.      pColl[2
17c0e 5d 2e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55  ].enc = SQLITE_U
17c0f 54 46 31 36 42 45 3b 0a 20 20 20 20 20 20 6d 65  TF16BE;.      me
17c10 6d 63 70 79 28 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e  mcpy(pColl[0].zN
17c11 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  ame, zName, nNam
17c12 65 29 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b  e);.      pColl[
17c13 30 5d 2e 7a 4e 61 6d 65 5b 6e 4e 61 6d 65 5d 20  0].zName[nName] 
17c14 3d 20 30 3b 0a 20 20 20 20 20 20 70 44 65 6c 20  = 0;.      pDel 
17c15 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73  = sqlite3HashIns
17c16 65 72 74 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65  ert(&db->aCollSe
17c17 71 2c 20 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d  q, pColl[0].zNam
17c18 65 2c 20 6e 4e 61 6d 65 2c 20 70 43 6f 6c 6c 29  e, nName, pColl)
17c19 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 61  ;..      /* If a
17c1a 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72   malloc() failur
17c1b 65 20 6f 63 63 75 72 65 64 20 69 6e 20 73 71 6c  e occured in sql
17c1c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 29  ite3HashInsert()
17c1d 2c 20 69 74 20 77 69 6c 6c 20 0a 20 20 20 20 20  , it will .     
17c1e 20 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20 70   ** return the p
17c1f 43 6f 6c 6c 20 70 6f 69 6e 74 65 72 20 74 6f 20  Coll pointer to 
17c20 62 65 20 64 65 6c 65 74 65 64 20 28 62 65 63 61  be deleted (beca
17c21 75 73 65 20 69 74 20 77 61 73 6e 27 74 20 61 64  use it wasn't ad
17c22 64 65 64 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20  ded.      ** to 
17c23 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 29 2e  the hash table).
17c24 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
17c25 61 73 73 65 72 74 28 20 21 70 44 65 6c 20 7c 7c  assert( !pDel ||
17c26 20 28 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 46   (sqlite3MallocF
17c27 61 69 6c 65 64 28 29 20 26 26 20 70 44 65 6c 3d  ailed() && pDel=
17c28 3d 70 43 6f 6c 6c 29 20 29 3b 0a 20 20 20 20 20  =pColl) );.     
17c29 20 69 66 28 20 70 44 65 6c 20 29 7b 0a 20 20 20   if( pDel ){.   
17c2a 20 20 20 20 20 73 71 6c 69 74 65 46 72 65 65 28       sqliteFree(
17c2b 70 44 65 6c 29 3b 0a 20 20 20 20 20 20 20 20 70  pDel);.        p
17c2c 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20  Coll = 0;.      
17c2d 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  }.    }.  }.  re
17c2e 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f  turn pColl;.}../
17c2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 7a  *.** Parameter z
17c30 4e 61 6d 65 20 70 6f 69 6e 74 73 20 74 6f 20 61  Name points to a
17c31 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 73   UTF-8 encoded s
17c32 74 72 69 6e 67 20 6e 4e 61 6d 65 20 62 79 74 65  tring nName byte
17c33 73 20 6c 6f 6e 67 2e 0a 2a 2a 20 52 65 74 75 72  s long..** Retur
17c34 6e 20 74 68 65 20 43 6f 6c 6c 53 65 71 2a 20 70  n the CollSeq* p
17c35 6f 69 6e 74 65 72 20 66 6f 72 20 74 68 65 20 63  ointer for the c
17c36 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
17c37 65 20 6e 61 6d 65 64 20 7a 4e 61 6d 65 0a 2a 2a  e named zName.**
17c38 20 66 6f 72 20 74 68 65 20 65 6e 63 6f 64 69 6e   for the encodin
17c39 67 20 27 65 6e 63 27 20 66 72 6f 6d 20 74 68 65  g 'enc' from the
17c3a 20 64 61 74 61 62 61 73 65 20 27 64 62 27 2e 0a   database 'db'..
17c3b 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 6e 74  **.** If the ent
17c3c 72 79 20 73 70 65 63 69 66 69 65 64 20 69 73 20  ry specified is 
17c3d 6e 6f 74 20 66 6f 75 6e 64 20 61 6e 64 20 27 63  not found and 'c
17c3e 72 65 61 74 65 27 20 69 73 20 74 72 75 65 2c 20  reate' is true, 
17c3f 74 68 65 6e 20 63 72 65 61 74 65 20 61 0a 2a 2a  then create a.**
17c40 20 6e 65 77 20 65 6e 74 72 79 2e 20 20 4f 74 68   new entry.  Oth
17c41 65 72 77 69 73 65 20 72 65 74 75 72 6e 20 4e 55  erwise return NU
17c42 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 65 70 61  LL..**.** A sepa
17c43 72 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 73 71  rate function sq
17c44 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53  lite3LocateCollS
17c45 65 71 28 29 20 69 73 20 61 20 77 72 61 70 70 65  eq() is a wrappe
17c46 72 20 61 72 6f 75 6e 64 0a 2a 2a 20 74 68 69 73  r around.** this
17c47 20 72 6f 75 74 69 6e 65 2e 20 20 73 71 6c 69 74   routine.  sqlit
17c48 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28  e3LocateCollSeq(
17c49 29 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 63 6f  ) invokes the co
17c4a 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79 0a  llation factory.
17c4b 2a 2a 20 69 66 20 6e 65 63 65 73 73 61 72 79 20  ** if necessary 
17c4c 61 6e 64 20 67 65 6e 65 72 61 74 65 73 20 61 6e  and generates an
17c4d 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69   error message i
17c4e 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20  f the collating 
17c4f 73 65 71 75 65 6e 63 65 0a 2a 2a 20 63 61 6e 6e  sequence.** cann
17c50 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a  ot be found..*/.
17c51 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43  SQLITE_PRIVATE C
17c52 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 46  ollSeq *sqlite3F
17c53 69 6e 64 43 6f 6c 6c 53 65 71 28 0a 20 20 73 71  indCollSeq(.  sq
17c54 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 75 38 20  lite3 *db,.  u8 
17c55 65 6e 63 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61  enc,.  const cha
17c56 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 69 6e 74 20  r *zName,.  int 
17c57 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 63 72 65  nName,.  int cre
17c58 61 74 65 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71  ate.){.  CollSeq
17c59 20 2a 70 43 6f 6c 6c 3b 0a 20 20 69 66 28 20 7a   *pColl;.  if( z
17c5a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 70 43 6f 6c  Name ){.    pCol
17c5b 6c 20 3d 20 66 69 6e 64 43 6f 6c 6c 53 65 71 45  l = findCollSeqE
17c5c 6e 74 72 79 28 64 62 2c 20 7a 4e 61 6d 65 2c 20  ntry(db, zName, 
17c5d 6e 4e 61 6d 65 2c 20 63 72 65 61 74 65 29 3b 0a  nName, create);.
17c5e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43 6f    }else{.    pCo
17c5f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f  ll = db->pDfltCo
17c60 6c 6c 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  ll;.  }.  assert
17c61 28 20 53 51 4c 49 54 45 5f 55 54 46 38 3d 3d 31  ( SQLITE_UTF8==1
17c62 20 26 26 20 53 51 4c 49 54 45 5f 55 54 46 31 36   && SQLITE_UTF16
17c63 4c 45 3d 3d 32 20 26 26 20 53 51 4c 49 54 45 5f  LE==2 && SQLITE_
17c64 55 54 46 31 36 42 45 3d 3d 33 20 29 3b 0a 20 20  UTF16BE==3 );.  
17c65 61 73 73 65 72 74 28 20 65 6e 63 3e 3d 53 51 4c  assert( enc>=SQL
17c66 49 54 45 5f 55 54 46 38 20 26 26 20 65 6e 63 3c  ITE_UTF8 && enc<
17c67 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20  =SQLITE_UTF16BE 
17c68 29 3b 0a 20 20 69 66 28 20 70 43 6f 6c 6c 20 29  );.  if( pColl )
17c69 20 70 43 6f 6c 6c 20 2b 3d 20 65 6e 63 2d 31 3b   pColl += enc-1;
17c6a 0a 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 3b  .  return pColl;
17c6b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65  .}../*.** Locate
17c6c 20 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e   a user function
17c6d 20 67 69 76 65 6e 20 61 20 6e 61 6d 65 2c 20 61   given a name, a
17c6e 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d   number of argum
17c6f 65 6e 74 73 20 61 6e 64 20 61 20 66 6c 61 67 0a  ents and a flag.
17c70 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 77 68  ** indicating wh
17c71 65 74 68 65 72 20 74 68 65 20 66 75 6e 63 74 69  ether the functi
17c72 6f 6e 20 70 72 65 66 65 72 73 20 55 54 46 2d 31  on prefers UTF-1
17c73 36 20 6f 76 65 72 20 55 54 46 2d 38 2e 20 20 52  6 over UTF-8.  R
17c74 65 74 75 72 6e 20 61 0a 2a 2a 20 70 6f 69 6e 74  eturn a.** point
17c75 65 72 20 74 6f 20 74 68 65 20 46 75 6e 63 44 65  er to the FuncDe
17c76 66 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  f structure that
17c77 20 64 65 66 69 6e 65 73 20 74 68 61 74 20 66 75   defines that fu
17c78 6e 63 74 69 6f 6e 2c 20 6f 72 20 72 65 74 75 72  nction, or retur
17c79 6e 0a 2a 2a 20 4e 55 4c 4c 20 69 66 20 74 68 65  n.** NULL if the
17c7a 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e   function does n
17c7b 6f 74 20 65 78 69 73 74 2e 0a 2a 2a 0a 2a 2a 20  ot exist..**.** 
17c7c 49 66 20 74 68 65 20 63 72 65 61 74 65 46 6c 61  If the createFla
17c7d 67 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 72  g argument is tr
17c7e 75 65 2c 20 74 68 65 6e 20 61 20 6e 65 77 20 28  ue, then a new (
17c7f 62 6c 61 6e 6b 29 20 46 75 6e 63 44 65 66 0a 2a  blank) FuncDef.*
17c80 2a 20 73 74 72 75 63 74 75 72 65 20 69 73 20 63  * structure is c
17c81 72 65 61 74 65 64 20 61 6e 64 20 6c 69 6b 65 64  reated and liked
17c82 20 69 6e 74 6f 20 74 68 65 20 22 64 62 22 20 73   into the "db" s
17c83 74 72 75 63 74 75 72 65 20 69 66 20 61 0a 2a 2a  tructure if a.**
17c84 20 6e 6f 20 6d 61 74 63 68 69 6e 67 20 66 75 6e   no matching fun
17c85 63 74 69 6f 6e 20 70 72 65 76 69 6f 75 73 6c 79  ction previously
17c86 20 65 78 69 73 74 65 64 2e 20 20 57 68 65 6e 20   existed.  When 
17c87 63 72 65 61 74 65 46 6c 61 67 20 69 73 20 74 72  createFlag is tr
17c88 75 65 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6e 41  ue.** and the nA
17c89 72 67 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  rg parameter is 
17c8a 2d 31 2c 20 74 68 65 6e 20 6f 6e 6c 79 20 61 20  -1, then only a 
17c8b 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 61 63  function that ac
17c8c 63 65 70 74 73 0a 2a 2a 20 61 6e 79 20 6e 75 6d  cepts.** any num
17c8d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73  ber of arguments
17c8e 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65   will be returne
17c8f 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 63 72 65 61  d..**.** If crea
17c90 74 65 46 6c 61 67 20 69 73 20 66 61 6c 73 65 20  teFlag is false 
17c91 61 6e 64 20 6e 41 72 67 20 69 73 20 2d 31 2c 20  and nArg is -1, 
17c92 74 68 65 6e 20 74 68 65 20 66 69 72 73 74 20 76  then the first v
17c93 61 6c 69 64 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  alid.** function
17c94 20 66 6f 75 6e 64 20 69 73 20 72 65 74 75 72 6e   found is return
17c95 65 64 2e 20 20 41 20 66 75 6e 63 74 69 6f 6e 20  ed.  A function 
17c96 69 73 20 76 61 6c 69 64 20 69 66 20 65 69 74 68  is valid if eith
17c97 65 72 20 78 46 75 6e 63 0a 2a 2a 20 6f 72 20 78  er xFunc.** or x
17c98 53 74 65 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  Step is non-zero
17c99 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 63 72 65 61 74  ..**.** If creat
17c9a 65 46 6c 61 67 20 69 73 20 66 61 6c 73 65 2c 20  eFlag is false, 
17c9b 74 68 65 6e 20 61 20 66 75 6e 63 74 69 6f 6e 20  then a function 
17c9c 77 69 74 68 20 74 68 65 20 72 65 71 75 69 72 65  with the require
17c9d 64 20 6e 61 6d 65 20 61 6e 64 0a 2a 2a 20 6e 75  d name and.** nu
17c9e 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
17c9f 73 20 6d 61 79 20 62 65 20 72 65 74 75 72 6e 65  s may be returne
17ca0 64 20 65 76 65 6e 20 69 66 20 74 68 65 20 65 54  d even if the eT
17ca1 65 78 74 52 65 70 20 66 6c 61 67 20 64 6f 65 73  extRep flag does
17ca2 20 6e 6f 74 0a 2a 2a 20 6d 61 74 63 68 20 74 68   not.** match th
17ca3 61 74 20 72 65 71 75 65 73 74 65 64 2e 0a 2a 2f  at requested..*/
17ca4 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
17ca5 46 75 6e 63 44 65 66 20 2a 73 71 6c 69 74 65 33  FuncDef *sqlite3
17ca6 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 0a 20 20  FindFunction(.  
17ca7 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
17ca8 20 20 20 2f 2a 20 41 6e 20 6f 70 65 6e 20 64 61     /* An open da
17ca9 74 61 62 61 73 65 20 2a 2f 0a 20 20 63 6f 6e 73  tabase */.  cons
17caa 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 2f  t char *zName, /
17cab 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 75  * Name of the fu
17cac 6e 63 74 69 6f 6e 2e 20 20 4e 6f 74 20 6e 75 6c  nction.  Not nul
17cad 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 2a 2f 0a  l-terminated */.
17cae 20 20 69 6e 74 20 6e 4e 61 6d 65 2c 20 20 20 20    int nName,    
17caf 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
17cb0 66 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20  f characters in 
17cb1 74 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e  the name */.  in
17cb2 74 20 6e 41 72 67 2c 20 20 20 20 20 20 20 20 20  t nArg,         
17cb3 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72   /* Number of ar
17cb4 67 75 6d 65 6e 74 73 2e 20 20 2d 31 20 6d 65 61  guments.  -1 mea
17cb5 6e 73 20 61 6e 79 20 6e 75 6d 62 65 72 20 2a 2f  ns any number */
17cb6 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20 20  .  u8 enc,      
17cb7 20 20 20 20 20 20 2f 2a 20 50 72 65 66 65 72 72        /* Preferr
17cb8 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67  ed text encoding
17cb9 20 2a 2f 0a 20 20 69 6e 74 20 63 72 65 61 74 65   */.  int create
17cba 46 6c 61 67 20 20 20 20 20 2f 2a 20 43 72 65 61  Flag     /* Crea
17cbb 74 65 20 6e 65 77 20 65 6e 74 72 79 20 69 66 20  te new entry if 
17cbc 74 72 75 65 20 61 6e 64 20 64 6f 65 73 20 6e 6f  true and does no
17cbd 74 20 6f 74 68 65 72 77 69 73 65 20 65 78 69 73  t otherwise exis
17cbe 74 20 2a 2f 0a 29 7b 0a 20 20 46 75 6e 63 44 65  t */.){.  FuncDe
17cbf 66 20 2a 70 3b 20 20 20 20 20 20 20 20 20 2f 2a  f *p;         /*
17cc0 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62   Iterator variab
17cc1 6c 65 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66 20  le */.  FuncDef 
17cc2 2a 70 46 69 72 73 74 3b 20 20 20 20 2f 2a 20 46  *pFirst;    /* F
17cc3 69 72 73 74 20 66 75 6e 63 74 69 6f 6e 20 77 69  irst function wi
17cc4 74 68 20 74 68 69 73 20 6e 61 6d 65 20 2a 2f 0a  th this name */.
17cc5 20 20 46 75 6e 63 44 65 66 20 2a 70 42 65 73 74    FuncDef *pBest
17cc6 20 3d 20 30 3b 20 2f 2a 20 42 65 73 74 20 6d 61   = 0; /* Best ma
17cc7 74 63 68 20 66 6f 75 6e 64 20 73 6f 20 66 61 72  tch found so far
17cc8 20 2a 2f 0a 20 20 69 6e 74 20 62 65 73 74 6d 61   */.  int bestma
17cc9 74 63 68 20 3d 20 30 3b 20 20 0a 0a 0a 20 20 61  tch = 0;  ...  a
17cca 73 73 65 72 74 28 20 65 6e 63 3d 3d 53 51 4c 49  ssert( enc==SQLI
17ccb 54 45 5f 55 54 46 38 20 7c 7c 20 65 6e 63 3d 3d  TE_UTF8 || enc==
17ccc 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 7c  SQLITE_UTF16LE |
17ccd 7c 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  | enc==SQLITE_UT
17cce 46 31 36 42 45 20 29 3b 0a 20 20 69 66 28 20 6e  F16BE );.  if( n
17ccf 41 72 67 3c 2d 31 20 29 20 6e 41 72 67 20 3d 20  Arg<-1 ) nArg = 
17cd0 2d 31 3b 0a 0a 20 20 70 46 69 72 73 74 20 3d 20  -1;..  pFirst = 
17cd1 28 46 75 6e 63 44 65 66 2a 29 73 71 6c 69 74 65  (FuncDef*)sqlite
17cd2 33 48 61 73 68 46 69 6e 64 28 26 64 62 2d 3e 61  3HashFind(&db->a
17cd3 46 75 6e 63 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61  Func, zName, nNa
17cd4 6d 65 29 3b 0a 20 20 66 6f 72 28 70 3d 70 46 69  me);.  for(p=pFi
17cd5 72 73 74 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65  rst; p; p=p->pNe
17cd6 78 74 29 7b 0a 20 20 20 20 2f 2a 20 44 75 72 69  xt){.    /* Duri
17cd7 6e 67 20 74 68 65 20 73 65 61 72 63 68 20 66 6f  ng the search fo
17cd8 72 20 74 68 65 20 62 65 73 74 20 66 75 6e 63 74  r the best funct
17cd9 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 2c 20  ion definition, 
17cda 62 65 73 74 6d 61 74 63 68 20 69 73 20 73 65 74  bestmatch is set
17cdb 0a 20 20 20 20 2a 2a 20 61 73 20 66 6f 6c 6c 6f  .    ** as follo
17cdc 77 73 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74  ws to indicate t
17cdd 68 65 20 71 75 61 6c 69 74 79 20 6f 66 20 74 68  he quality of th
17cde 65 20 6d 61 74 63 68 20 77 69 74 68 20 74 68 65  e match with the
17cdf 20 64 65 66 69 6e 69 74 69 6f 6e 0a 20 20 20 20   definition.    
17ce0 2a 2a 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  ** pointed to by
17ce1 20 70 42 65 73 74 3a 0a 20 20 20 20 2a 2a 0a 20   pBest:.    **. 
17ce2 20 20 20 2a 2a 20 30 3a 20 70 42 65 73 74 20 69     ** 0: pBest i
17ce3 73 20 4e 55 4c 4c 2e 20 4e 6f 20 6d 61 74 63 68  s NULL. No match
17ce4 20 68 61 73 20 62 65 65 6e 20 66 6f 75 6e 64 2e   has been found.
17ce5 0a 20 20 20 20 2a 2a 20 31 3a 20 41 20 76 61 72  .    ** 1: A var
17ce6 69 61 62 6c 65 20 61 72 67 75 6d 65 6e 74 73 20  iable arguments 
17ce7 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 70 72  function that pr
17ce8 65 66 65 72 73 20 55 54 46 2d 38 20 77 68 65 6e  efers UTF-8 when
17ce9 20 61 20 55 54 46 2d 31 36 0a 20 20 20 20 2a 2a   a UTF-16.    **
17cea 20 20 20 20 65 6e 63 6f 64 69 6e 67 20 69 73 20      encoding is 
17ceb 72 65 71 75 65 73 74 65 64 2c 20 6f 72 20 76 69  requested, or vi
17cec 63 65 20 76 65 72 73 61 2e 0a 20 20 20 20 2a 2a  ce versa..    **
17ced 20 32 3a 20 41 20 76 61 72 69 61 62 6c 65 20 61   2: A variable a
17cee 72 67 75 6d 65 6e 74 73 20 66 75 6e 63 74 69 6f  rguments functio
17cef 6e 20 74 68 61 74 20 75 73 65 73 20 55 54 46 2d  n that uses UTF-
17cf0 31 36 42 45 20 77 68 65 6e 20 55 54 46 2d 31 36  16BE when UTF-16
17cf1 4c 45 20 69 73 0a 20 20 20 20 2a 2a 20 20 20 20  LE is.    **    
17cf2 72 65 71 75 65 73 74 65 64 2c 20 6f 72 20 76 69  requested, or vi
17cf3 63 65 20 76 65 72 73 61 2e 0a 20 20 20 20 2a 2a  ce versa..    **
17cf4 20 33 3a 20 41 20 76 61 72 69 61 62 6c 65 20 61   3: A variable a
17cf5 72 67 75 6d 65 6e 74 73 20 66 75 6e 63 74 69 6f  rguments functio
17cf6 6e 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65  n using the same
17cf7 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 2e 0a   text encoding..
17cf8 20 20 20 20 2a 2a 20 34 3a 20 41 20 66 75 6e 63      ** 4: A func
17cf9 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20 65 78  tion with the ex
17cfa 61 63 74 20 6e 75 6d 62 65 72 20 6f 66 20 61 72  act number of ar
17cfb 67 75 6d 65 6e 74 73 20 72 65 71 75 65 73 74 65  guments requeste
17cfc 64 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 20 20  d that.    **   
17cfd 20 70 72 65 66 65 72 73 20 55 54 46 2d 38 20 77   prefers UTF-8 w
17cfe 68 65 6e 20 61 20 55 54 46 2d 31 36 20 65 6e 63  hen a UTF-16 enc
17cff 6f 64 69 6e 67 20 69 73 20 72 65 71 75 65 73 74  oding is request
17d00 65 64 2c 20 6f 72 20 76 69 63 65 20 76 65 72 73  ed, or vice vers
17d01 61 2e 0a 20 20 20 20 2a 2a 20 35 3a 20 41 20 66  a..    ** 5: A f
17d02 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65  unction with the
17d03 20 65 78 61 63 74 20 6e 75 6d 62 65 72 20 6f 66   exact number of
17d04 20 61 72 67 75 6d 65 6e 74 73 20 72 65 71 75 65   arguments reque
17d05 73 74 65 64 20 74 68 61 74 0a 20 20 20 20 2a 2a  sted that.    **
17d06 20 20 20 20 70 72 65 66 65 72 73 20 55 54 46 2d      prefers UTF-
17d07 31 36 4c 45 20 77 68 65 6e 20 55 54 46 2d 31 36  16LE when UTF-16
17d08 42 45 20 69 73 20 72 65 71 75 65 73 74 65 64 2c  BE is requested,
17d09 20 6f 72 20 76 69 63 65 20 76 65 72 73 61 2e 0a   or vice versa..
17d0a 20 20 20 20 2a 2a 20 36 3a 20 41 6e 20 65 78 61      ** 6: An exa
17d0b 63 74 20 6d 61 74 63 68 2e 0a 20 20 20 20 2a 2a  ct match..    **
17d0c 0a 20 20 20 20 2a 2a 20 41 20 6c 61 72 67 65 72  .    ** A larger
17d0d 20 76 61 6c 75 65 20 6f 66 20 27 6d 61 74 63 68   value of 'match
17d0e 71 75 61 6c 27 20 69 6e 64 69 63 61 74 65 73 20  qual' indicates 
17d0f 61 20 6d 6f 72 65 20 64 65 73 69 72 61 62 6c 65  a more desirable
17d10 20 6d 61 74 63 68 2e 0a 20 20 20 20 2a 2f 0a 20   match..    */. 
17d11 20 20 20 69 66 28 20 70 2d 3e 6e 41 72 67 3d 3d     if( p->nArg==
17d12 2d 31 20 7c 7c 20 70 2d 3e 6e 41 72 67 3d 3d 6e  -1 || p->nArg==n
17d13 41 72 67 20 7c 7c 20 6e 41 72 67 3d 3d 2d 31 20  Arg || nArg==-1 
17d14 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6d 61 74  ){.      int mat
17d15 63 68 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20  ch = 1;         
17d16 20 2f 2a 20 51 75 61 6c 69 74 79 20 6f 66 20 74   /* Quality of t
17d17 68 69 73 20 6d 61 74 63 68 20 2a 2f 0a 20 20 20  his match */.   
17d18 20 20 20 69 66 28 20 70 2d 3e 6e 41 72 67 3d 3d     if( p->nArg==
17d19 6e 41 72 67 20 7c 7c 20 6e 41 72 67 3d 3d 2d 31  nArg || nArg==-1
17d1a 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 61 74 63   ){.        matc
17d1b 68 20 3d 20 34 3b 0a 20 20 20 20 20 20 7d 0a 20  h = 4;.      }. 
17d1c 20 20 20 20 20 69 66 28 20 65 6e 63 3d 3d 70 2d       if( enc==p-
17d1d 3e 69 50 72 65 66 45 6e 63 20 29 7b 0a 20 20 20  >iPrefEnc ){.   
17d1e 20 20 20 20 20 6d 61 74 63 68 20 2b 3d 20 32 3b       match += 2;
17d1f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 65  .      }.      e
17d20 6c 73 65 20 69 66 28 20 28 65 6e 63 3d 3d 53 51  lse if( (enc==SQ
17d21 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 26 26 20  LITE_UTF16LE && 
17d22 70 2d 3e 69 50 72 65 66 45 6e 63 3d 3d 53 51 4c  p->iPrefEnc==SQL
17d23 49 54 45 5f 55 54 46 31 36 42 45 29 20 7c 7c 0a  ITE_UTF16BE) ||.
17d24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
17d25 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31  enc==SQLITE_UTF1
17d26 36 42 45 20 26 26 20 70 2d 3e 69 50 72 65 66 45  6BE && p->iPrefE
17d27 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36  nc==SQLITE_UTF16
17d28 4c 45 29 20 29 7b 0a 20 20 20 20 20 20 20 20 6d  LE) ){.        m
17d29 61 74 63 68 20 2b 3d 20 31 3b 0a 20 20 20 20 20  atch += 1;.     
17d2a 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 6d 61   }..      if( ma
17d2b 74 63 68 3e 62 65 73 74 6d 61 74 63 68 20 29 7b  tch>bestmatch ){
17d2c 0a 20 20 20 20 20 20 20 20 70 42 65 73 74 20 3d  .        pBest =
17d2d 20 70 3b 0a 20 20 20 20 20 20 20 20 62 65 73 74   p;.        best
17d2e 6d 61 74 63 68 20 3d 20 6d 61 74 63 68 3b 0a 20  match = match;. 
17d2f 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
17d30 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 72  ..  /* If the cr
17d31 65 61 74 65 46 6c 61 67 20 70 61 72 61 6d 65 74  eateFlag paramet
17d32 65 72 20 69 73 20 74 72 75 65 2c 20 61 6e 64 20  er is true, and 
17d33 74 68 65 20 73 65 61 63 68 20 64 69 64 20 6e 6f  the seach did no
17d34 74 20 72 65 76 65 61 6c 20 61 6e 0a 20 20 2a 2a  t reveal an.  **
17d35 20 65 78 61 63 74 20 6d 61 74 63 68 20 66 6f 72   exact match for
17d36 20 74 68 65 20 6e 61 6d 65 2c 20 6e 75 6d 62 65   the name, numbe
17d37 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 61  r of arguments a
17d38 6e 64 20 65 6e 63 6f 64 69 6e 67 2c 20 74 68 65  nd encoding, the
17d39 6e 20 61 64 64 20 61 0a 20 20 2a 2a 20 6e 65 77  n add a.  ** new
17d3a 20 65 6e 74 72 79 20 74 6f 20 74 68 65 20 68 61   entry to the ha
17d3b 73 68 20 74 61 62 6c 65 20 61 6e 64 20 72 65 74  sh table and ret
17d3c 75 72 6e 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69  urn it..  */.  i
17d3d 66 28 20 63 72 65 61 74 65 46 6c 61 67 20 26 26  f( createFlag &&
17d3e 20 62 65 73 74 6d 61 74 63 68 3c 36 20 26 26 20   bestmatch<6 && 
17d3f 0a 20 20 20 20 20 20 28 70 42 65 73 74 20 3d 20  .      (pBest = 
17d40 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 73 69 7a  sqliteMalloc(siz
17d41 65 6f 66 28 2a 70 42 65 73 74 29 2b 6e 4e 61 6d  eof(*pBest)+nNam
17d42 65 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 70 42  e))!=0 ){.    pB
17d43 65 73 74 2d 3e 6e 41 72 67 20 3d 20 6e 41 72 67  est->nArg = nArg
17d44 3b 0a 20 20 20 20 70 42 65 73 74 2d 3e 70 4e 65  ;.    pBest->pNe
17d45 78 74 20 3d 20 70 46 69 72 73 74 3b 0a 20 20 20  xt = pFirst;.   
17d46 20 70 42 65 73 74 2d 3e 69 50 72 65 66 45 6e 63   pBest->iPrefEnc
17d47 20 3d 20 65 6e 63 3b 0a 20 20 20 20 6d 65 6d 63   = enc;.    memc
17d48 70 79 28 70 42 65 73 74 2d 3e 7a 4e 61 6d 65 2c  py(pBest->zName,
17d49 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a   zName, nName);.
17d4a 20 20 20 20 70 42 65 73 74 2d 3e 7a 4e 61 6d 65      pBest->zName
17d4b 5b 6e 4e 61 6d 65 5d 20 3d 20 30 3b 0a 20 20 20  [nName] = 0;.   
17d4c 20 69 66 28 20 70 42 65 73 74 3d 3d 73 71 6c 69   if( pBest==sqli
17d4d 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 64  te3HashInsert(&d
17d4e 62 2d 3e 61 46 75 6e 63 2c 70 42 65 73 74 2d 3e  b->aFunc,pBest->
17d4f 7a 4e 61 6d 65 2c 6e 4e 61 6d 65 2c 28 76 6f 69  zName,nName,(voi
17d50 64 2a 29 70 42 65 73 74 29 20 29 7b 0a 20 20 20  d*)pBest) ){.   
17d51 20 20 20 73 71 6c 69 74 65 46 72 65 65 28 70 42     sqliteFree(pB
17d52 65 73 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75  est);.      retu
17d53 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  rn 0;.    }.  }.
17d54 0a 20 20 69 66 28 20 70 42 65 73 74 20 26 26 20  .  if( pBest && 
17d55 28 70 42 65 73 74 2d 3e 78 53 74 65 70 20 7c 7c  (pBest->xStep ||
17d56 20 70 42 65 73 74 2d 3e 78 46 75 6e 63 20 7c 7c   pBest->xFunc ||
17d57 20 63 72 65 61 74 65 46 6c 61 67 29 20 29 7b 0a   createFlag) ){.
17d58 20 20 20 20 72 65 74 75 72 6e 20 70 42 65 73 74      return pBest
17d59 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  ;.  }.  return 0
17d5a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20  ;.}../*.** Free 
17d5b 61 6c 6c 20 72 65 73 6f 75 72 63 65 73 20 68 65  all resources he
17d5c 6c 64 20 62 79 20 74 68 65 20 73 63 68 65 6d 61  ld by the schema
17d5d 20 73 74 72 75 63 74 75 72 65 2e 20 54 68 65 20   structure. The 
17d5e 76 6f 69 64 2a 20 61 72 67 75 6d 65 6e 74 20 70  void* argument p
17d5f 6f 69 6e 74 73 0a 2a 2a 20 61 74 20 61 20 53 63  oints.** at a Sc
17d60 68 65 6d 61 20 73 74 72 75 63 74 2e 20 54 68 69  hema struct. Thi
17d61 73 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20  s function does 
17d62 6e 6f 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 46  not call sqliteF
17d63 72 65 65 28 29 20 6f 6e 20 74 68 65 20 0a 2a 2a  ree() on the .**
17d64 20 70 6f 69 6e 74 65 72 20 69 74 73 65 6c 66 2c   pointer itself,
17d65 20 69 74 20 6a 75 73 74 20 63 6c 65 61 6e 73 20   it just cleans 
17d66 75 70 20 73 75 62 73 69 64 75 61 72 79 20 72 65  up subsiduary re
17d67 73 6f 75 72 63 65 73 20 28 69 2e 65 2e 20 74 68  sources (i.e. th
17d68 65 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 6f 66  e contents.** of
17d69 20 74 68 65 20 73 63 68 65 6d 61 20 68 61 73 68   the schema hash
17d6a 20 74 61 62 6c 65 73 29 2e 0a 2a 2f 0a 53 51 4c   tables)..*/.SQL
17d6b 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
17d6c 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 46 72   sqlite3SchemaFr
17d6d 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 48  ee(void *p){.  H
17d6e 61 73 68 20 74 65 6d 70 31 3b 0a 20 20 48 61 73  ash temp1;.  Has
17d6f 68 20 74 65 6d 70 32 3b 0a 20 20 48 61 73 68 45  h temp2;.  HashE
17d70 6c 65 6d 20 2a 70 45 6c 65 6d 3b 0a 20 20 53 63  lem *pElem;.  Sc
17d71 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d 20  hema *pSchema = 
17d72 28 53 63 68 65 6d 61 20 2a 29 70 3b 0a 0a 20 20  (Schema *)p;..  
17d73 74 65 6d 70 31 20 3d 20 70 53 63 68 65 6d 61 2d  temp1 = pSchema-
17d74 3e 74 62 6c 48 61 73 68 3b 0a 20 20 74 65 6d 70  >tblHash;.  temp
17d75 32 20 3d 20 70 53 63 68 65 6d 61 2d 3e 74 72 69  2 = pSchema->tri
17d76 67 48 61 73 68 3b 0a 20 20 73 71 6c 69 74 65 33  gHash;.  sqlite3
17d77 48 61 73 68 49 6e 69 74 28 26 70 53 63 68 65 6d  HashInit(&pSchem
17d78 61 2d 3e 74 72 69 67 48 61 73 68 2c 20 53 51 4c  a->trigHash, SQL
17d79 49 54 45 5f 48 41 53 48 5f 53 54 52 49 4e 47 2c  ITE_HASH_STRING,
17d7a 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 48 61   0);.  sqlite3Ha
17d7b 73 68 43 6c 65 61 72 28 26 70 53 63 68 65 6d 61  shClear(&pSchema
17d7c 2d 3e 61 46 4b 65 79 29 3b 0a 20 20 73 71 6c 69  ->aFKey);.  sqli
17d7d 74 65 33 48 61 73 68 43 6c 65 61 72 28 26 70 53  te3HashClear(&pS
17d7e 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 29 3b  chema->idxHash);
17d7f 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c  .  for(pElem=sql
17d80 69 74 65 48 61 73 68 46 69 72 73 74 28 26 74 65  iteHashFirst(&te
17d81 6d 70 32 29 3b 20 70 45 6c 65 6d 3b 20 70 45 6c  mp2); pElem; pEl
17d82 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78  em=sqliteHashNex
17d83 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20 20 73  t(pElem)){.    s
17d84 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67  qlite3DeleteTrig
17d85 67 65 72 28 28 54 72 69 67 67 65 72 2a 29 73 71  ger((Trigger*)sq
17d86 6c 69 74 65 48 61 73 68 44 61 74 61 28 70 45 6c  liteHashData(pEl
17d87 65 6d 29 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  em));.  }.  sqli
17d88 74 65 33 48 61 73 68 43 6c 65 61 72 28 26 74 65  te3HashClear(&te
17d89 6d 70 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 48  mp2);.  sqlite3H
17d8a 61 73 68 49 6e 69 74 28 26 70 53 63 68 65 6d 61  ashInit(&pSchema
17d8b 2d 3e 74 62 6c 48 61 73 68 2c 20 53 51 4c 49 54  ->tblHash, SQLIT
17d8c 45 5f 48 41 53 48 5f 53 54 52 49 4e 47 2c 20 30  E_HASH_STRING, 0
17d8d 29 3b 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d 73  );.  for(pElem=s
17d8e 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 26  qliteHashFirst(&
17d8f 74 65 6d 70 31 29 3b 20 70 45 6c 65 6d 3b 20 70  temp1); pElem; p
17d90 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e  Elem=sqliteHashN
17d91 65 78 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20  ext(pElem)){.   
17d92 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 73   Table *pTab = s
17d93 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 70 45  qliteHashData(pE
17d94 6c 65 6d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  lem);.    sqlite
17d95 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70 54 61  3DeleteTable(pTa
17d96 62 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  b);.  }.  sqlite
17d97 33 48 61 73 68 43 6c 65 61 72 28 26 74 65 6d 70  3HashClear(&temp
17d98 31 29 3b 0a 20 20 70 53 63 68 65 6d 61 2d 3e 70  1);.  pSchema->p
17d99 53 65 71 54 61 62 20 3d 20 30 3b 0a 20 20 70 53  SeqTab = 0;.  pS
17d9a 63 68 65 6d 61 2d 3e 66 6c 61 67 73 20 26 3d 20  chema->flags &= 
17d9b 7e 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64  ~DB_SchemaLoaded
17d9c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20  ;.}../*.** Find 
17d9d 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 73  and return the s
17d9e 63 68 65 6d 61 20 61 73 73 6f 63 69 61 74 65 64  chema associated
17d9f 20 77 69 74 68 20 61 20 42 54 72 65 65 2e 20 20   with a BTree.  
17da0 43 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20  Create.** a new 
17da1 6f 6e 65 20 69 66 20 6e 65 63 65 73 73 61 72 79  one if necessary
17da2 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
17da3 41 54 45 20 53 63 68 65 6d 61 20 2a 73 71 6c 69  ATE Schema *sqli
17da4 74 65 33 53 63 68 65 6d 61 47 65 74 28 42 74 72  te3SchemaGet(Btr
17da5 65 65 20 2a 70 42 74 29 7b 0a 20 20 53 63 68 65  ee *pBt){.  Sche
17da6 6d 61 20 2a 20 70 3b 0a 20 20 69 66 28 20 70 42  ma * p;.  if( pB
17da7 74 20 29 7b 0a 20 20 20 20 70 20 3d 20 28 53 63  t ){.    p = (Sc
17da8 68 65 6d 61 20 2a 29 73 71 6c 69 74 65 33 42 74  hema *)sqlite3Bt
17da9 72 65 65 53 63 68 65 6d 61 28 70 42 74 2c 73 69  reeSchema(pBt,si
17daa 7a 65 6f 66 28 53 63 68 65 6d 61 29 2c 73 71 6c  zeof(Schema),sql
17dab 69 74 65 33 53 63 68 65 6d 61 46 72 65 65 29 3b  ite3SchemaFree);
17dac 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20  .  }else{.    p 
17dad 3d 20 28 53 63 68 65 6d 61 20 2a 29 73 71 6c 69  = (Schema *)sqli
17dae 74 65 4d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28  teMalloc(sizeof(
17daf 53 63 68 65 6d 61 29 29 3b 0a 20 20 7d 0a 20 20  Schema));.  }.  
17db0 69 66 28 20 70 20 26 26 20 30 3d 3d 70 2d 3e 66  if( p && 0==p->f
17db1 69 6c 65 5f 66 6f 72 6d 61 74 20 29 7b 0a 20 20  ile_format ){.  
17db2 20 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69    sqlite3HashIni
17db3 74 28 26 70 2d 3e 74 62 6c 48 61 73 68 2c 20 53  t(&p->tblHash, S
17db4 51 4c 49 54 45 5f 48 41 53 48 5f 53 54 52 49 4e  QLITE_HASH_STRIN
17db5 47 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  G, 0);.    sqlit
17db6 65 33 48 61 73 68 49 6e 69 74 28 26 70 2d 3e 69  e3HashInit(&p->i
17db7 64 78 48 61 73 68 2c 20 53 51 4c 49 54 45 5f 48  dxHash, SQLITE_H
17db8 41 53 48 5f 53 54 52 49 4e 47 2c 20 30 29 3b 0a  ASH_STRING, 0);.
17db9 20 20 20 20 73 71 6c 69 74 65 33 48 61 73 68 49      sqlite3HashI
17dba 6e 69 74 28 26 70 2d 3e 74 72 69 67 48 61 73 68  nit(&p->trigHash
17dbb 2c 20 53 51 4c 49 54 45 5f 48 41 53 48 5f 53 54  , SQLITE_HASH_ST
17dbc 52 49 4e 47 2c 20 30 29 3b 0a 20 20 20 20 73 71  RING, 0);.    sq
17dbd 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 26 70  lite3HashInit(&p
17dbe 2d 3e 61 46 4b 65 79 2c 20 53 51 4c 49 54 45 5f  ->aFKey, SQLITE_
17dbf 48 41 53 48 5f 53 54 52 49 4e 47 2c 20 31 29 3b  HASH_STRING, 1);
17dc0 0a 20 20 20 20 70 2d 3e 65 6e 63 20 3d 20 53 51  .    p->enc = SQ
17dc1 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 7d 0a 20  LITE_UTF8;.  }. 
17dc2 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a   return p;.}../*
17dc3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
17dc4 64 20 6f 66 20 63 61 6c 6c 62 61 63 6b 2e 63 20  d of callback.c 
17dc5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17dc6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17dc7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
17dc8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
17dc9 67 69 6e 20 66 69 6c 65 20 63 6f 6d 70 6c 65 74  gin file complet
17dca 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
17dcb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17dcc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
17dcd 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
17dce 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 15.**.** The 
17dcf 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
17dd0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
17dd1 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
17dd2 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
17dd3 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
17dd4 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
17dd5 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
17dd6 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
17dd7 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
17dd8 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
17dd9 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
17dda 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
17ddb 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
17ddc 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
17ddd 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
17dde 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
17ddf 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
17de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17de1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17de2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17de3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17de4 2a 2a 2a 2a 0a 2a 2a 20 41 6e 20 74 6f 6b 65 6e  ****.** An token
17de5 69 7a 65 72 20 66 6f 72 20 53 51 4c 0a 2a 2a 0a  izer for SQL.**.
17de6 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
17de7 74 61 69 6e 73 20 43 20 63 6f 64 65 20 74 68 61  tains C code tha
17de8 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  t implements the
17de9 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74   sqlite3_complet
17dea 65 28 29 20 41 50 49 2e 0a 2a 2a 20 54 68 69 73  e() API..** This
17deb 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20 62 65   code used to be
17dec 20 70 61 72 74 20 6f 66 20 74 68 65 20 74 6f 6b   part of the tok
17ded 65 6e 69 7a 65 72 2e 63 20 73 6f 75 72 63 65 20  enizer.c source 
17dee 66 69 6c 65 2e 20 20 42 75 74 20 62 79 0a 2a 2a  file.  But by.**
17def 20 73 65 70 61 72 61 74 69 6e 67 20 69 74 20 6f   separating it o
17df0 75 74 2c 20 74 68 65 20 63 6f 64 65 20 77 69 6c  ut, the code wil
17df1 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c  l be automatical
17df2 6c 79 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 0a  ly omitted from.
17df3 2a 2a 20 73 74 61 74 69 63 20 6c 69 6e 6b 73 20  ** static links 
17df4 74 68 61 74 20 64 6f 20 6e 6f 74 20 75 73 65 20  that do not use 
17df5 69 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 63  it..**.** $Id: c
17df6 6f 6d 70 6c 65 74 65 2e 63 2c 76 20 31 2e 33 20  omplete.c,v 1.3 
17df7 32 30 30 36 2f 30 31 2f 31 38 20 31 35 3a 32 35  2006/01/18 15:25
17df8 3a 31 37 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :17 danielk1977 
17df9 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66  Exp $.*/.#ifndef
17dfa 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d   SQLITE_OMIT_COM
17dfb 50 4c 45 54 45 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  PLETE../*.** Thi
17dfc 73 20 69 73 20 64 65 66 69 6e 65 64 20 69 6e 20  s is defined in 
17dfd 74 6f 6b 65 6e 69 7a 65 2e 63 2e 20 20 57 65 20  tokenize.c.  We 
17dfe 6a 75 73 74 20 68 61 76 65 20 74 6f 20 69 6d 70  just have to imp
17dff 6f 72 74 20 74 68 65 20 64 65 66 69 6e 69 74 69  ort the definiti
17e00 6f 6e 2e 0a 2a 2f 0a 65 78 74 65 72 6e 20 63 6f  on..*/.extern co
17e01 6e 73 74 20 63 68 61 72 20 73 71 6c 69 74 65 33  nst char sqlite3
17e02 49 73 49 64 43 68 61 72 5b 5d 3b 0a 23 64 65 66  IsIdChar[];.#def
17e03 69 6e 65 20 49 64 43 68 61 72 28 43 29 20 20 28  ine IdChar(C)  (
17e04 28 28 63 3d 43 29 26 30 78 38 30 29 21 3d 30 20  ((c=C)&0x80)!=0 
17e05 7c 7c 20 28 63 3e 30 78 31 66 20 26 26 20 73 71  || (c>0x1f && sq
17e06 6c 69 74 65 33 49 73 49 64 43 68 61 72 5b 63 2d  lite3IsIdChar[c-
17e07 30 78 32 30 5d 29 29 0a 0a 0a 2f 2a 0a 2a 2a 20  0x20])).../*.** 
17e08 54 6f 6b 65 6e 20 74 79 70 65 73 20 75 73 65 64  Token types used
17e09 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f   by the sqlite3_
17e0a 63 6f 6d 70 6c 65 74 65 28 29 20 72 6f 75 74 69  complete() routi
17e0b 6e 65 2e 20 20 53 65 65 20 74 68 65 20 68 65 61  ne.  See the hea
17e0c 64 65 72 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 73 20  der.** comments 
17e0d 6f 6e 20 74 68 61 74 20 70 72 6f 63 65 64 75 72  on that procedur
17e0e 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  e for additional
17e0f 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f   information..*/
17e10 0a 23 64 65 66 69 6e 65 20 74 6b 53 45 4d 49 20  .#define tkSEMI 
17e11 20 20 20 30 0a 23 64 65 66 69 6e 65 20 74 6b 57     0.#define tkW
17e12 53 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65  S      1.#define
17e13 20 74 6b 4f 54 48 45 52 20 20 20 32 0a 23 64 65   tkOTHER   2.#de
17e14 66 69 6e 65 20 74 6b 45 58 50 4c 41 49 4e 20 33  fine tkEXPLAIN 3
17e15 0a 23 64 65 66 69 6e 65 20 74 6b 43 52 45 41 54  .#define tkCREAT
17e16 45 20 20 34 0a 23 64 65 66 69 6e 65 20 74 6b 54  E  4.#define tkT
17e17 45 4d 50 20 20 20 20 35 0a 23 64 65 66 69 6e 65  EMP    5.#define
17e18 20 74 6b 54 52 49 47 47 45 52 20 36 0a 23 64 65   tkTRIGGER 6.#de
17e19 66 69 6e 65 20 74 6b 45 4e 44 20 20 20 20 20 37  fine tkEND     7
17e1a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54  ../*.** Return T
17e1b 52 55 45 20 69 66 20 74 68 65 20 67 69 76 65 6e  RUE if the given
17e1c 20 53 51 4c 20 73 74 72 69 6e 67 20 65 6e 64 73   SQL string ends
17e1d 20 69 6e 20 61 20 73 65 6d 69 63 6f 6c 6f 6e 2e   in a semicolon.
17e1e 0a 2a 2a 0a 2a 2a 20 53 70 65 63 69 61 6c 20 68  .**.** Special h
17e1f 61 6e 64 6c 69 6e 67 20 69 73 20 72 65 71 75 69  andling is requi
17e20 72 65 20 66 6f 72 20 43 52 45 41 54 45 20 54 52  re for CREATE TR
17e21 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e 74 73  IGGER statements
17e22 2e 0a 2a 2a 20 57 68 65 6e 65 76 65 72 20 74 68  ..** Whenever th
17e23 65 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52  e CREATE TRIGGER
17e24 20 6b 65 79 77 6f 72 64 73 20 61 72 65 20 73 65   keywords are se
17e25 65 6e 2c 20 74 68 65 20 73 74 61 74 65 6d 65 6e  en, the statemen
17e26 74 0a 2a 2a 20 6d 75 73 74 20 65 6e 64 20 77 69  t.** must end wi
17e27 74 68 20 22 3b 45 4e 44 3b 22 2e 0a 2a 2a 0a 2a  th ";END;"..**.*
17e28 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74  * This implement
17e29 61 74 69 6f 6e 20 75 73 65 73 20 61 20 73 74 61  ation uses a sta
17e2a 74 65 20 6d 61 63 68 69 6e 65 20 77 69 74 68 20  te machine with 
17e2b 37 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20  7 states:.**.** 
17e2c 20 20 28 30 29 20 53 54 41 52 54 20 20 20 20 20    (0) START     
17e2d 41 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  At the beginning
17e2e 20 6f 72 20 65 6e 64 20 6f 66 20 61 6e 20 53 51   or end of an SQ
17e2f 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68  L statement.  Th
17e30 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 20 20  is routine.**   
17e31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65                re
17e32 74 75 72 6e 73 20 31 20 69 66 20 69 74 20 65 6e  turns 1 if it en
17e33 64 73 20 69 6e 20 74 68 65 20 53 54 41 52 54 20  ds in the START 
17e34 73 74 61 74 65 20 61 6e 64 20 30 20 69 66 20 69  state and 0 if i
17e35 74 20 65 6e 64 73 0a 2a 2a 20 20 20 20 20 20 20  t ends.**       
17e36 20 20 20 20 20 20 20 20 20 20 69 6e 20 61 6e 79            in any
17e37 20 6f 74 68 65 72 20 73 74 61 74 65 2e 0a 2a 2a   other state..**
17e38 0a 2a 2a 20 20 20 28 31 29 20 4e 4f 52 4d 41 4c  .**   (1) NORMAL
17e39 20 20 20 20 57 65 20 61 72 65 20 69 6e 20 74 68      We are in th
17e3a 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 74 61 74  e middle of stat
17e3b 65 6d 65 6e 74 20 77 68 69 63 68 20 65 6e 64 73  ement which ends
17e3c 20 77 69 74 68 20 61 20 73 69 6e 67 6c 65 0a 2a   with a single.*
17e3d 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
17e3e 20 20 73 65 6d 69 63 6f 6c 6f 6e 2e 0a 2a 2a 0a    semicolon..**.
17e3f 2a 2a 20 20 20 28 32 29 20 45 58 50 4c 41 49 4e  **   (2) EXPLAIN
17e40 20 20 20 54 68 65 20 6b 65 79 77 6f 72 64 20 45     The keyword E
17e41 58 50 4c 41 49 4e 20 68 61 73 20 62 65 65 6e 20  XPLAIN has been 
17e42 73 65 65 6e 20 61 74 20 74 68 65 20 62 65 67 69  seen at the begi
17e43 6e 6e 69 6e 67 20 6f 66 20 0a 2a 2a 20 20 20 20  nning of .**    
17e44 20 20 20 20 20 20 20 20 20 20 20 20 20 61 20 73               a s
17e45 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  tatement..**.** 
17e46 20 20 28 33 29 20 43 52 45 41 54 45 20 20 20 20    (3) CREATE    
17e47 54 68 65 20 6b 65 79 77 6f 72 64 20 43 52 45 41  The keyword CREA
17e48 54 45 20 68 61 73 20 62 65 65 6e 20 73 65 65 6e  TE has been seen
17e49 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e   at the beginnin
17e4a 67 20 6f 66 20 61 0a 2a 2a 20 20 20 20 20 20 20  g of a.**       
17e4b 20 20 20 20 20 20 20 20 20 20 73 74 61 74 65 6d            statem
17e4c 65 6e 74 2c 20 70 6f 73 73 69 62 6c 79 20 70 72  ent, possibly pr
17e4d 65 63 65 65 64 65 64 20 62 79 20 45 58 50 4c 41  eceeded by EXPLA
17e4e 49 4e 20 61 6e 64 2f 6f 72 20 66 6f 6c 6c 6f 77  IN and/or follow
17e4f 65 64 20 62 79 0a 2a 2a 20 20 20 20 20 20 20 20  ed by.**        
17e50 20 20 20 20 20 20 20 20 20 54 45 4d 50 20 6f 72           TEMP or
17e51 20 54 45 4d 50 4f 52 41 52 59 0a 2a 2a 0a 2a 2a   TEMPORARY.**.**
17e52 20 20 20 28 34 29 20 54 52 49 47 47 45 52 20 20     (4) TRIGGER  
17e53 20 57 65 20 61 72 65 20 69 6e 20 74 68 65 20 6d   We are in the m
17e54 69 64 64 6c 65 20 6f 66 20 61 20 74 72 69 67 67  iddle of a trigg
17e55 65 72 20 64 65 66 69 6e 69 74 69 6f 6e 20 74 68  er definition th
17e56 61 74 20 6d 75 73 74 20 62 65 0a 2a 2a 20 20 20  at must be.**   
17e57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 65 6e                en
17e58 64 65 64 20 62 79 20 61 20 73 65 6d 69 63 6f 6c  ded by a semicol
17e59 6f 6e 2c 20 74 68 65 20 6b 65 79 77 6f 72 64 20  on, the keyword 
17e5a 45 4e 44 2c 20 61 6e 64 20 61 6e 6f 74 68 65 72  END, and another
17e5b 20 73 65 6d 69 63 6f 6c 6f 6e 2e 0a 2a 2a 0a 2a   semicolon..**.*
17e5c 2a 20 20 20 28 35 29 20 53 45 4d 49 20 20 20 20  *   (5) SEMI    
17e5d 20 20 57 65 27 76 65 20 73 65 65 6e 20 74 68 65    We've seen the
17e5e 20 66 69 72 73 74 20 73 65 6d 69 63 6f 6c 6f 6e   first semicolon
17e5f 20 69 6e 20 74 68 65 20 22 3b 45 4e 44 3b 22 20   in the ";END;" 
17e60 74 68 61 74 20 6f 63 63 75 72 73 20 61 74 0a 2a  that occurs at.*
17e61 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
17e62 20 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20 74    the end of a t
17e63 72 69 67 67 65 72 20 64 65 66 69 6e 69 74 69 6f  rigger definitio
17e64 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 36 29 20 45  n..**.**   (6) E
17e65 4e 44 20 20 20 20 20 20 20 57 65 27 76 65 20 73  ND       We've s
17e66 65 65 6e 20 74 68 65 20 22 3b 45 4e 44 22 20 6f  een the ";END" o
17e67 66 20 74 68 65 20 22 3b 45 4e 44 3b 22 20 74 68  f the ";END;" th
17e68 61 74 20 6f 63 63 75 72 73 20 61 74 20 74 68 65  at occurs at the
17e69 20 65 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20   end.**         
17e6a 20 20 20 20 20 20 20 20 6f 66 20 61 20 74 72 69          of a tri
17e6b 67 67 65 72 20 64 69 66 69 6e 69 74 69 6f 6e 2e  gger difinition.
17e6c 0a 2a 2a 0a 2a 2a 20 54 72 61 6e 73 69 74 69 6f  .**.** Transitio
17e6d 6e 73 20 62 65 74 77 65 65 6e 20 73 74 61 74 65  ns between state
17e6e 73 20 61 62 6f 76 65 20 61 72 65 20 64 65 74 65  s above are dete
17e6f 72 6d 69 6e 65 64 20 62 79 20 74 6f 6b 65 6e 73  rmined by tokens
17e70 20 65 78 74 72 61 63 74 65 64 0a 2a 2a 20 66 72   extracted.** fr
17e71 6f 6d 20 74 68 65 20 69 6e 70 75 74 2e 20 20 54  om the input.  T
17e72 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 6f 6b  he following tok
17e73 65 6e 73 20 61 72 65 20 73 69 67 6e 69 66 69 63  ens are signific
17e74 61 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 30 29  ant:.**.**   (0)
17e75 20 74 6b 53 45 4d 49 20 20 20 20 20 20 41 20 73   tkSEMI      A s
17e76 65 6d 69 63 6f 6c 6f 6e 2e 0a 2a 2a 20 20 20 28  emicolon..**   (
17e77 31 29 20 74 6b 57 53 20 20 20 20 20 20 20 20 57  1) tkWS        W
17e78 68 69 74 65 73 70 61 63 65 0a 2a 2a 20 20 20 28  hitespace.**   (
17e79 32 29 20 74 6b 4f 54 48 45 52 20 20 20 20 20 41  2) tkOTHER     A
17e7a 6e 79 20 6f 74 68 65 72 20 53 51 4c 20 74 6f 6b  ny other SQL tok
17e7b 65 6e 2e 0a 2a 2a 20 20 20 28 33 29 20 74 6b 45  en..**   (3) tkE
17e7c 58 50 4c 41 49 4e 20 20 20 54 68 65 20 22 65 78  XPLAIN   The "ex
17e7d 70 6c 61 69 6e 22 20 6b 65 79 77 6f 72 64 2e 0a  plain" keyword..
17e7e 2a 2a 20 20 20 28 34 29 20 74 6b 43 52 45 41 54  **   (4) tkCREAT
17e7f 45 20 20 20 20 54 68 65 20 22 63 72 65 61 74 65  E    The "create
17e80 22 20 6b 65 79 77 6f 72 64 2e 0a 2a 2a 20 20 20  " keyword..**   
17e81 28 35 29 20 74 6b 54 45 4d 50 20 20 20 20 20 20  (5) tkTEMP      
17e82 54 68 65 20 22 74 65 6d 70 22 20 6f 72 20 22 74  The "temp" or "t
17e83 65 6d 70 6f 72 61 72 79 22 20 6b 65 79 77 6f 72  emporary" keywor
17e84 64 2e 0a 2a 2a 20 20 20 28 36 29 20 74 6b 54 52  d..**   (6) tkTR
17e85 49 47 47 45 52 20 20 20 54 68 65 20 22 74 72 69  IGGER   The "tri
17e86 67 67 65 72 22 20 6b 65 79 77 6f 72 64 2e 0a 2a  gger" keyword..*
17e87 2a 20 20 20 28 37 29 20 74 6b 45 4e 44 20 20 20  *   (7) tkEND   
17e88 20 20 20 20 54 68 65 20 22 65 6e 64 22 20 6b 65      The "end" ke
17e89 79 77 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 69  yword..**.** Whi
17e8a 74 65 73 70 61 63 65 20 6e 65 76 65 72 20 63 61  tespace never ca
17e8b 75 73 65 73 20 61 20 73 74 61 74 65 20 74 72 61  uses a state tra
17e8c 6e 73 69 74 69 6f 6e 20 61 6e 64 20 69 73 20 61  nsition and is a
17e8d 6c 77 61 79 73 20 69 67 6e 6f 72 65 64 2e 0a 2a  lways ignored..*
17e8e 2a 0a 2a 2a 20 49 66 20 77 65 20 63 6f 6d 70 69  *.** If we compi
17e8f 6c 65 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4f  le with SQLITE_O
17e90 4d 49 54 5f 54 52 49 47 47 45 52 2c 20 61 6c 6c  MIT_TRIGGER, all
17e91 20 6f 66 20 74 68 65 20 63 6f 6d 70 75 74 61 74   of the computat
17e92 69 6f 6e 20 6e 65 65 64 65 64 0a 2a 2a 20 74 6f  ion needed.** to
17e93 20 72 65 63 6f 67 6e 69 7a 65 20 74 68 65 20 65   recognize the e
17e94 6e 64 20 6f 66 20 61 20 74 72 69 67 67 65 72 20  nd of a trigger 
17e95 63 61 6e 20 62 65 20 6f 6d 69 74 74 65 64 2e 20  can be omitted. 
17e96 20 41 6c 6c 20 77 65 20 68 61 76 65 20 74 6f 20   All we have to 
17e97 64 6f 0a 2a 2a 20 69 73 20 6c 6f 6f 6b 20 66 6f  do.** is look fo
17e98 72 20 61 20 73 65 6d 69 63 6f 6c 6f 6e 20 74 68  r a semicolon th
17e99 61 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f  at is not part o
17e9a 66 20 61 6e 20 73 74 72 69 6e 67 20 6f 72 20 63  f an string or c
17e9b 6f 6d 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54  omment..*/.SQLIT
17e9c 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
17e9d 33 5f 63 6f 6d 70 6c 65 74 65 28 63 6f 6e 73 74  3_complete(const
17e9e 20 63 68 61 72 20 2a 7a 53 71 6c 29 7b 0a 20 20   char *zSql){.  
17e9f 75 38 20 73 74 61 74 65 20 3d 20 30 3b 20 20 20  u8 state = 0;   
17ea0 2f 2a 20 43 75 72 72 65 6e 74 20 73 74 61 74 65  /* Current state
17ea1 2c 20 75 73 69 6e 67 20 6e 75 6d 62 65 72 73 20  , using numbers 
17ea2 64 65 66 69 6e 65 64 20 69 6e 20 68 65 61 64 65  defined in heade
17ea3 72 20 63 6f 6d 6d 65 6e 74 20 2a 2f 0a 20 20 75  r comment */.  u
17ea4 38 20 74 6f 6b 65 6e 3b 20 20 20 20 20 20 20 2f  8 token;       /
17ea5 2a 20 56 61 6c 75 65 20 6f 66 20 74 68 65 20 6e  * Value of the n
17ea6 65 78 74 20 74 6f 6b 65 6e 20 2a 2f 0a 0a 23 69  ext token */..#i
17ea7 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
17ea8 54 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a 20 41  T_TRIGGER.  /* A
17ea9 20 63 6f 6d 70 6c 65 78 20 73 74 61 74 65 6d 65   complex stateme
17eaa 6e 74 20 6d 61 63 68 69 6e 65 20 75 73 65 64 20  nt machine used 
17eab 74 6f 20 64 65 74 65 63 74 20 74 68 65 20 65 6e  to detect the en
17eac 64 20 6f 66 20 61 20 43 52 45 41 54 45 20 54 52  d of a CREATE TR
17ead 49 47 47 45 52 0a 20 20 2a 2a 20 73 74 61 74 65  IGGER.  ** state
17eae 6d 65 6e 74 2e 20 20 54 68 69 73 20 69 73 20 74  ment.  This is t
17eaf 68 65 20 6e 6f 72 6d 61 6c 20 63 61 73 65 2e 0a  he normal case..
17eb0 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f    */.  static co
17eb1 6e 73 74 20 75 38 20 74 72 61 6e 73 5b 37 5d 5b  nst u8 trans[7][
17eb2 38 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 20  8] = {.         
17eb3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
17eb4 6f 6b 65 6e 3a 20 20 20 20 20 20 20 20 20 20 20  oken:           
17eb5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17eb6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17eb7 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 2f 2a 20       */.     /* 
17eb8 53 74 61 74 65 3a 20 20 20 20 20 20 20 2a 2a 20  State:       ** 
17eb9 20 53 45 4d 49 20 20 57 53 20 20 4f 54 48 45 52   SEMI  WS  OTHER
17eba 20 45 58 50 4c 41 49 4e 20 20 43 52 45 41 54 45   EXPLAIN  CREATE
17ebb 20 20 54 45 4d 50 20 20 54 52 49 47 47 45 52 20    TEMP  TRIGGER 
17ebc 20 45 4e 44 20 20 2a 2f 0a 20 20 20 20 20 2f 2a   END  */.     /*
17ebd 20 30 20 20 20 53 54 41 52 54 3a 20 2a 2f 20 7b   0   START: */ {
17ebe 20 20 20 20 30 2c 20 20 30 2c 20 20 20 20 20 31      0,  0,     1
17ebf 2c 20 20 20 20 20 20 32 2c 20 20 20 20 20 20 33  ,      2,      3
17ec0 2c 20 20 20 20 31 2c 20 20 20 20 20 20 20 31 2c  ,    1,       1,
17ec1 20 20 20 31 2c 20 20 7d 2c 0a 20 20 20 20 20 2f     1,  },.     /
17ec2 2a 20 31 20 20 4e 4f 52 4d 41 4c 3a 20 2a 2f 20  * 1  NORMAL: */ 
17ec3 7b 20 20 20 20 30 2c 20 20 31 2c 20 20 20 20 20  {    0,  1,     
17ec4 31 2c 20 20 20 20 20 20 31 2c 20 20 20 20 20 20  1,      1,      
17ec5 31 2c 20 20 20 20 31 2c 20 20 20 20 20 20 20 31  1,    1,       1
17ec6 2c 20 20 20 31 2c 20 20 7d 2c 0a 20 20 20 20 20  ,   1,  },.     
17ec7 2f 2a 20 32 20 45 58 50 4c 41 49 4e 3a 20 2a 2f  /* 2 EXPLAIN: */
17ec8 20 7b 20 20 20 20 30 2c 20 20 32 2c 20 20 20 20   {    0,  2,    
17ec9 20 31 2c 20 20 20 20 20 20 31 2c 20 20 20 20 20   1,      1,     
17eca 20 33 2c 20 20 20 20 31 2c 20 20 20 20 20 20 20   3,    1,       
17ecb 31 2c 20 20 20 31 2c 20 20 7d 2c 0a 20 20 20 20  1,   1,  },.    
17ecc 20 2f 2a 20 33 20 20 43 52 45 41 54 45 3a 20 2a   /* 3  CREATE: *
17ecd 2f 20 7b 20 20 20 20 30 2c 20 20 33 2c 20 20 20  / {    0,  3,   
17ece 20 20 31 2c 20 20 20 20 20 20 31 2c 20 20 20 20    1,      1,    
17ecf 20 20 31 2c 20 20 20 20 33 2c 20 20 20 20 20 20    1,    3,      
17ed0 20 34 2c 20 20 20 31 2c 20 20 7d 2c 0a 20 20 20   4,   1,  },.   
17ed1 20 20 2f 2a 20 34 20 54 52 49 47 47 45 52 3a 20    /* 4 TRIGGER: 
17ed2 2a 2f 20 7b 20 20 20 20 35 2c 20 20 34 2c 20 20  */ {    5,  4,  
17ed3 20 20 20 34 2c 20 20 20 20 20 20 34 2c 20 20 20     4,      4,   
17ed4 20 20 20 34 2c 20 20 20 20 34 2c 20 20 20 20 20     4,    4,     
17ed5 20 20 34 2c 20 20 20 34 2c 20 20 7d 2c 0a 20 20    4,   4,  },.  
17ed6 20 20 20 2f 2a 20 35 20 20 20 20 53 45 4d 49 3a     /* 5    SEMI:
17ed7 20 2a 2f 20 7b 20 20 20 20 35 2c 20 20 35 2c 20   */ {    5,  5, 
17ed8 20 20 20 20 34 2c 20 20 20 20 20 20 34 2c 20 20      4,      4,  
17ed9 20 20 20 20 34 2c 20 20 20 20 34 2c 20 20 20 20      4,    4,    
17eda 20 20 20 34 2c 20 20 20 36 2c 20 20 7d 2c 0a 20     4,   6,  },. 
17edb 20 20 20 20 2f 2a 20 36 20 20 20 20 20 45 4e 44      /* 6     END
17edc 3a 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20 36 2c  : */ {    0,  6,
17edd 20 20 20 20 20 34 2c 20 20 20 20 20 20 34 2c 20       4,      4, 
17ede 20 20 20 20 20 34 2c 20 20 20 20 34 2c 20 20 20       4,    4,   
17edf 20 20 20 20 34 2c 20 20 20 34 2c 20 20 7d 2c 0a      4,   4,  },.
17ee0 20 20 7d 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a 20    };.#else.  /* 
17ee1 49 66 20 74 72 69 67 67 65 72 73 20 61 72 65 20  If triggers are 
17ee2 6e 6f 74 20 73 75 70 70 6f 72 65 64 20 62 79 20  not suppored by 
17ee3 74 68 69 73 20 63 6f 6d 70 69 6c 65 20 74 68 65  this compile the
17ee4 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  n the statement 
17ee5 6d 61 63 68 69 6e 65 0a 20 20 2a 2a 20 75 73 65  machine.  ** use
17ee6 64 20 74 6f 20 64 65 74 65 63 74 20 74 68 65 20  d to detect the 
17ee7 65 6e 64 20 6f 66 20 61 20 73 74 61 74 65 6d 65  end of a stateme
17ee8 6e 74 20 69 73 20 6d 75 63 68 20 73 69 6d 70 6c  nt is much simpl
17ee9 69 65 72 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69  ier.  */.  stati
17eea 63 20 63 6f 6e 73 74 20 75 38 20 74 72 61 6e 73  c const u8 trans
17eeb 5b 32 5d 5b 33 5d 20 3d 20 7b 0a 20 20 20 20 20  [2][3] = {.     
17eec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17eed 2f 2a 20 54 6f 6b 65 6e 3a 20 20 20 20 20 20 20  /* Token:       
17eee 20 20 20 20 2a 2f 0a 20 20 20 20 20 2f 2a 20 53      */.     /* S
17eef 74 61 74 65 3a 20 20 20 20 20 20 20 2a 2a 20 20  tate:       **  
17ef0 53 45 4d 49 20 20 57 53 20 20 4f 54 48 45 52 20  SEMI  WS  OTHER 
17ef1 2a 2f 0a 20 20 20 20 20 2f 2a 20 30 20 20 20 53  */.     /* 0   S
17ef2 54 41 52 54 3a 20 2a 2f 20 7b 20 20 20 20 30 2c  TART: */ {    0,
17ef3 20 20 30 2c 20 20 20 20 20 31 2c 20 7d 2c 0a 20    0,     1, },. 
17ef4 20 20 20 20 2f 2a 20 31 20 20 4e 4f 52 4d 41 4c      /* 1  NORMAL
17ef5 3a 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20 31 2c  : */ {    0,  1,
17ef6 20 20 20 20 20 31 2c 20 7d 2c 0a 20 20 7d 3b 0a       1, },.  };.
17ef7 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
17ef8 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 2a 2f  _OMIT_TRIGGER */
17ef9 0a 0a 20 20 77 68 69 6c 65 28 20 2a 7a 53 71 6c  ..  while( *zSql
17efa 20 29 7b 0a 20 20 20 20 73 77 69 74 63 68 28 20   ){.    switch( 
17efb 2a 7a 53 71 6c 20 29 7b 0a 20 20 20 20 20 20 63  *zSql ){.      c
17efc 61 73 65 20 27 3b 27 3a 20 7b 20 20 2f 2a 20 41  ase ';': {  /* A
17efd 20 73 65 6d 69 63 6f 6c 6f 6e 20 2a 2f 0a 20 20   semicolon */.  
17efe 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b        token = tk
17eff 53 45 4d 49 3b 0a 20 20 20 20 20 20 20 20 62 72  SEMI;.        br
17f00 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
17f01 20 20 20 63 61 73 65 20 27 20 27 3a 0a 20 20 20     case ' ':.   
17f02 20 20 20 63 61 73 65 20 27 5c 72 27 3a 0a 20 20     case '\r':.  
17f03 20 20 20 20 63 61 73 65 20 27 5c 74 27 3a 0a 20      case '\t':. 
17f04 20 20 20 20 20 63 61 73 65 20 27 5c 6e 27 3a 0a       case '\n':.
17f05 20 20 20 20 20 20 63 61 73 65 20 27 5c 66 27 3a        case '\f':
17f06 20 7b 20 20 2f 2a 20 57 68 69 74 65 20 73 70 61   {  /* White spa
17f07 63 65 20 69 73 20 69 67 6e 6f 72 65 64 20 2a 2f  ce is ignored */
17f08 0a 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d  .        token =
17f09 20 74 6b 57 53 3b 0a 20 20 20 20 20 20 20 20 62   tkWS;.        b
17f0a 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
17f0b 20 20 20 20 63 61 73 65 20 27 2f 27 3a 20 7b 20      case '/': { 
17f0c 20 20 2f 2a 20 43 2d 73 74 79 6c 65 20 63 6f 6d    /* C-style com
17f0d 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 20  ments */.       
17f0e 20 69 66 28 20 7a 53 71 6c 5b 31 5d 21 3d 27 2a   if( zSql[1]!='*
17f0f 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 74  ' ){.          t
17f10 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a  oken = tkOTHER;.
17f11 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
17f12 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
17f13 20 20 20 7a 53 71 6c 20 2b 3d 20 32 3b 0a 20 20     zSql += 2;.  
17f14 20 20 20 20 20 20 77 68 69 6c 65 28 20 7a 53 71        while( zSq
17f15 6c 5b 30 5d 20 26 26 20 28 7a 53 71 6c 5b 30 5d  l[0] && (zSql[0]
17f16 21 3d 27 2a 27 20 7c 7c 20 7a 53 71 6c 5b 31 5d  !='*' || zSql[1]
17f17 21 3d 27 2f 27 29 20 29 7b 20 7a 53 71 6c 2b 2b  !='/') ){ zSql++
17f18 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20  ; }.        if( 
17f19 7a 53 71 6c 5b 30 5d 3d 3d 30 20 29 20 72 65 74  zSql[0]==0 ) ret
17f1a 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 7a  urn 0;.        z
17f1b 53 71 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20 74  Sql++;.        t
17f1c 6f 6b 65 6e 20 3d 20 74 6b 57 53 3b 0a 20 20 20  oken = tkWS;.   
17f1d 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
17f1e 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 27    }.      case '
17f1f 2d 27 3a 20 7b 20 20 20 2f 2a 20 53 51 4c 2d 73  -': {   /* SQL-s
17f20 74 79 6c 65 20 63 6f 6d 6d 65 6e 74 73 20 66 72  tyle comments fr
17f21 6f 6d 20 22 2d 2d 22 20 74 6f 20 65 6e 64 20 6f  om "--" to end o
17f22 66 20 6c 69 6e 65 20 2a 2f 0a 20 20 20 20 20 20  f line */.      
17f23 20 20 69 66 28 20 7a 53 71 6c 5b 31 5d 21 3d 27    if( zSql[1]!='
17f24 2d 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  -' ){.          
17f25 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b  token = tkOTHER;
17f26 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
17f27 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
17f28 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 53 71 6c      while( *zSql
17f29 20 26 26 20 2a 7a 53 71 6c 21 3d 27 5c 6e 27 20   && *zSql!='\n' 
17f2a 29 7b 20 7a 53 71 6c 2b 2b 3b 20 7d 0a 20 20 20  ){ zSql++; }.   
17f2b 20 20 20 20 20 69 66 28 20 2a 7a 53 71 6c 3d 3d       if( *zSql==
17f2c 30 20 29 20 72 65 74 75 72 6e 20 73 74 61 74 65  0 ) return state
17f2d 3d 3d 30 3b 0a 20 20 20 20 20 20 20 20 74 6f 6b  ==0;.        tok
17f2e 65 6e 20 3d 20 74 6b 57 53 3b 0a 20 20 20 20 20  en = tkWS;.     
17f2f 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
17f30 7d 0a 20 20 20 20 20 20 63 61 73 65 20 27 5b 27  }.      case '['
17f31 3a 20 7b 20 20 20 2f 2a 20 4d 69 63 72 6f 73 6f  : {   /* Microso
17f32 66 74 2d 73 74 79 6c 65 20 69 64 65 6e 74 69 66  ft-style identif
17f33 69 65 72 73 20 69 6e 20 5b 2e 2e 2e 5d 20 2a 2f  iers in [...] */
17f34 0a 20 20 20 20 20 20 20 20 7a 53 71 6c 2b 2b 3b  .        zSql++;
17f35 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
17f36 2a 7a 53 71 6c 20 26 26 20 2a 7a 53 71 6c 21 3d  *zSql && *zSql!=
17f37 27 5d 27 20 29 7b 20 7a 53 71 6c 2b 2b 3b 20 7d  ']' ){ zSql++; }
17f38 0a 20 20 20 20 20 20 20 20 69 66 28 20 2a 7a 53  .        if( *zS
17f39 71 6c 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  ql==0 ) return 0
17f3a 3b 0a 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20  ;.        token 
17f3b 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20  = tkOTHER;.     
17f3c 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
17f3d 7d 0a 20 20 20 20 20 20 63 61 73 65 20 27 60 27  }.      case '`'
17f3e 3a 20 20 20 20 20 2f 2a 20 47 72 61 76 65 2d 61  :     /* Grave-a
17f3f 63 63 65 6e 74 20 71 75 6f 74 65 64 20 73 79 6d  ccent quoted sym
17f40 62 6f 6c 73 20 75 73 65 64 20 62 79 20 4d 79 53  bols used by MyS
17f41 51 4c 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  QL */.      case
17f42 20 27 22 27 3a 20 20 20 20 20 2f 2a 20 73 69 6e   '"':     /* sin
17f43 67 6c 65 2d 20 61 6e 64 20 64 6f 75 62 6c 65 2d  gle- and double-
17f44 71 75 6f 74 65 64 20 73 74 72 69 6e 67 73 20 2a  quoted strings *
17f45 2f 0a 20 20 20 20 20 20 63 61 73 65 20 27 5c 27  /.      case '\'
17f46 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  ': {.        int
17f47 20 63 20 3d 20 2a 7a 53 71 6c 3b 0a 20 20 20 20   c = *zSql;.    
17f48 20 20 20 20 7a 53 71 6c 2b 2b 3b 0a 20 20 20 20      zSql++;.    
17f49 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 53 71 6c      while( *zSql
17f4a 20 26 26 20 2a 7a 53 71 6c 21 3d 63 20 29 7b 20   && *zSql!=c ){ 
17f4b 7a 53 71 6c 2b 2b 3b 20 7d 0a 20 20 20 20 20 20  zSql++; }.      
17f4c 20 20 69 66 28 20 2a 7a 53 71 6c 3d 3d 30 20 29    if( *zSql==0 )
17f4d 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20   return 0;.     
17f4e 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48     token = tkOTH
17f4f 45 52 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  ER;.        brea
17f50 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
17f51 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20   default: {.    
17f52 20 20 20 20 69 6e 74 20 63 3b 0a 20 20 20 20 20      int c;.     
17f53 20 20 20 69 66 28 20 49 64 43 68 61 72 28 28 75     if( IdChar((u
17f54 38 29 2a 7a 53 71 6c 29 20 29 7b 0a 20 20 20 20  8)*zSql) ){.    
17f55 20 20 20 20 20 20 2f 2a 20 4b 65 79 77 6f 72 64        /* Keyword
17f56 73 20 61 6e 64 20 75 6e 71 75 6f 74 65 64 20 69  s and unquoted i
17f57 64 65 6e 74 69 66 69 65 72 73 20 2a 2f 0a 20 20  dentifiers */.  
17f58 20 20 20 20 20 20 20 20 69 6e 74 20 6e 49 64 3b          int nId;
17f59 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6e  .          for(n
17f5a 49 64 3d 31 3b 20 49 64 43 68 61 72 28 7a 53 71  Id=1; IdChar(zSq
17f5b 6c 5b 6e 49 64 5d 29 3b 20 6e 49 64 2b 2b 29 7b  l[nId]); nId++){
17f5c 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  }.#ifdef SQLITE_
17f5d 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20  OMIT_TRIGGER.   
17f5e 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74         token = t
17f5f 6b 4f 54 48 45 52 3b 0a 23 65 6c 73 65 0a 20 20  kOTHER;.#else.  
17f60 20 20 20 20 20 20 20 20 73 77 69 74 63 68 28 20          switch( 
17f61 2a 7a 53 71 6c 20 29 7b 0a 20 20 20 20 20 20 20  *zSql ){.       
17f62 20 20 20 20 20 63 61 73 65 20 27 63 27 3a 20 63       case 'c': c
17f63 61 73 65 20 27 43 27 3a 20 7b 0a 20 20 20 20 20  ase 'C': {.     
17f64 20 20 20 20 20 20 20 20 20 69 66 28 20 6e 49 64           if( nId
17f65 3d 3d 36 20 26 26 20 73 71 6c 69 74 65 33 53 74  ==6 && sqlite3St
17f66 72 4e 49 43 6d 70 28 7a 53 71 6c 2c 20 22 63 72  rNICmp(zSql, "cr
17f67 65 61 74 65 22 2c 20 36 29 3d 3d 30 20 29 7b 0a  eate", 6)==0 ){.
17f68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17f69 74 6f 6b 65 6e 20 3d 20 74 6b 43 52 45 41 54 45  token = tkCREATE
17f6a 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
17f6b 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
17f6c 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74         token = t
17f6d 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20  kOTHER;.        
17f6e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
17f6f 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
17f70 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
17f71 20 20 20 20 20 20 20 63 61 73 65 20 27 74 27 3a         case 't':
17f72 20 63 61 73 65 20 27 54 27 3a 20 7b 0a 20 20 20   case 'T': {.   
17f73 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6e             if( n
17f74 49 64 3d 3d 37 20 26 26 20 73 71 6c 69 74 65 33  Id==7 && sqlite3
17f75 53 74 72 4e 49 43 6d 70 28 7a 53 71 6c 2c 20 22  StrNICmp(zSql, "
17f76 74 72 69 67 67 65 72 22 2c 20 37 29 3d 3d 30 20  trigger", 7)==0 
17f77 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
17f78 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 54 52 49     token = tkTRI
17f79 47 47 45 52 3b 0a 20 20 20 20 20 20 20 20 20 20  GGER;.          
17f7a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 49      }else if( nI
17f7b 64 3d 3d 34 20 26 26 20 73 71 6c 69 74 65 33 53  d==4 && sqlite3S
17f7c 74 72 4e 49 43 6d 70 28 7a 53 71 6c 2c 20 22 74  trNICmp(zSql, "t
17f7d 65 6d 70 22 2c 20 34 29 3d 3d 30 20 29 7b 0a 20  emp", 4)==0 ){. 
17f7e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
17f7f 6f 6b 65 6e 20 3d 20 74 6b 54 45 4d 50 3b 0a 20  oken = tkTEMP;. 
17f80 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c               }el
17f81 73 65 20 69 66 28 20 6e 49 64 3d 3d 39 20 26 26  se if( nId==9 &&
17f82 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70   sqlite3StrNICmp
17f83 28 7a 53 71 6c 2c 20 22 74 65 6d 70 6f 72 61 72  (zSql, "temporar
17f84 79 22 2c 20 39 29 3d 3d 30 20 29 7b 0a 20 20 20  y", 9)==0 ){.   
17f85 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b               tok
17f86 65 6e 20 3d 20 74 6b 54 45 4d 50 3b 0a 20 20 20  en = tkTEMP;.   
17f87 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65             }else
17f88 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
17f89 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45    token = tkOTHE
17f8a 52 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  R;.             
17f8b 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   }.             
17f8c 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
17f8d 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
17f8e 20 20 63 61 73 65 20 27 65 27 3a 20 20 63 61 73    case 'e':  cas
17f8f 65 20 27 45 27 3a 20 7b 0a 20 20 20 20 20 20 20  e 'E': {.       
17f90 20 20 20 20 20 20 20 69 66 28 20 6e 49 64 3d 3d         if( nId==
17f91 33 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e  3 && sqlite3StrN
17f92 49 43 6d 70 28 7a 53 71 6c 2c 20 22 65 6e 64 22  ICmp(zSql, "end"
17f93 2c 20 33 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  , 3)==0 ){.     
17f94 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e             token
17f95 20 3d 20 74 6b 45 4e 44 3b 0a 20 20 20 20 20 20   = tkEND;.      
17f96 20 20 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 69          }else.#i
17f97 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
17f98 54 5f 45 58 50 4c 41 49 4e 0a 20 20 20 20 20 20  T_EXPLAIN.      
17f99 20 20 20 20 20 20 20 20 69 66 28 20 6e 49 64 3d          if( nId=
17f9a 3d 37 20 26 26 20 73 71 6c 69 74 65 33 53 74 72  =7 && sqlite3Str
17f9b 4e 49 43 6d 70 28 7a 53 71 6c 2c 20 22 65 78 70  NICmp(zSql, "exp
17f9c 6c 61 69 6e 22 2c 20 37 29 3d 3d 30 20 29 7b 0a  lain", 7)==0 ){.
17f9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17f9e 74 6f 6b 65 6e 20 3d 20 74 6b 45 58 50 4c 41 49  token = tkEXPLAI
17f9f 4e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  N;.             
17fa0 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20   }else.#endif.  
17fa1 20 20 20 20 20 20 20 20 20 20 20 20 7b 0a 20 20              {.  
17fa2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f                to
17fa3 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20  ken = tkOTHER;. 
17fa4 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20               }. 
17fa5 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65               bre
17fa6 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ak;.            
17fa7 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 65  }.            de
17fa8 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20  fault: {.       
17fa9 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74         token = t
17faa 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20  kOTHER;.        
17fab 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
17fac 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
17fad 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a       }.#endif /*
17fae 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49   SQLITE_OMIT_TRI
17faf 47 47 45 52 20 2a 2f 0a 20 20 20 20 20 20 20 20  GGER */.        
17fb0 20 20 7a 53 71 6c 20 2b 3d 20 6e 49 64 2d 31 3b    zSql += nId-1;
17fb1 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
17fb2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65            /* Ope
17fb3 72 61 74 6f 72 73 20 61 6e 64 20 73 70 65 63 69  rators and speci
17fb4 61 6c 20 73 79 6d 62 6f 6c 73 20 2a 2f 0a 20 20  al symbols */.  
17fb5 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20          token = 
17fb6 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20  tkOTHER;.       
17fb7 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
17fb8 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
17fb9 20 20 20 20 73 74 61 74 65 20 3d 20 74 72 61 6e      state = tran
17fba 73 5b 73 74 61 74 65 5d 5b 74 6f 6b 65 6e 5d 3b  s[state][token];
17fbb 0a 20 20 20 20 7a 53 71 6c 2b 2b 3b 0a 20 20 7d  .    zSql++;.  }
17fbc 0a 20 20 72 65 74 75 72 6e 20 73 74 61 74 65 3d  .  return state=
17fbd 3d 30 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53  =0;.}..#ifndef S
17fbe 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
17fbf 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
17fc0 69 6e 65 20 69 73 20 74 68 65 20 73 61 6d 65 20  ine is the same 
17fc1 61 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63  as the sqlite3_c
17fc2 6f 6d 70 6c 65 74 65 28 29 20 72 6f 75 74 69 6e  omplete() routin
17fc3 65 20 64 65 73 63 72 69 62 65 64 0a 2a 2a 20 61  e described.** a
17fc4 62 6f 76 65 2c 20 65 78 63 65 70 74 20 74 68 61  bove, except tha
17fc5 74 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20  t the parameter 
17fc6 69 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 62  is required to b
17fc7 65 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64  e UTF-16 encoded
17fc8 2c 20 6e 6f 74 0a 2a 2a 20 55 54 46 2d 38 2e 0a  , not.** UTF-8..
17fc9 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
17fca 74 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65  t sqlite3_comple
17fcb 74 65 31 36 28 63 6f 6e 73 74 20 76 6f 69 64 20  te16(const void 
17fcc 2a 7a 53 71 6c 29 7b 0a 20 20 73 71 6c 69 74 65  *zSql){.  sqlite
17fcd 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 3b 0a 20  3_value *pVal;. 
17fce 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 53 71   char const *zSq
17fcf 6c 38 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 30  l8;.  int rc = 0
17fd0 3b 0a 0a 20 20 70 56 61 6c 20 3d 20 73 71 6c 69  ;..  pVal = sqli
17fd1 74 65 33 56 61 6c 75 65 4e 65 77 28 29 3b 0a 20  te3ValueNew();. 
17fd2 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74   sqlite3ValueSet
17fd3 53 74 72 28 70 56 61 6c 2c 20 2d 31 2c 20 7a 53  Str(pVal, -1, zS
17fd4 71 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36  ql, SQLITE_UTF16
17fd5 4e 41 54 49 56 45 2c 20 53 51 4c 49 54 45 5f 53  NATIVE, SQLITE_S
17fd6 54 41 54 49 43 29 3b 0a 20 20 7a 53 71 6c 38 20  TATIC);.  zSql8 
17fd7 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65  = sqlite3ValueTe
17fd8 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f  xt(pVal, SQLITE_
17fd9 55 54 46 38 29 3b 0a 20 20 69 66 28 20 7a 53 71  UTF8);.  if( zSq
17fda 6c 38 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  l8 ){.    rc = s
17fdb 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28  qlite3_complete(
17fdc 7a 53 71 6c 38 29 3b 0a 20 20 7d 0a 20 20 73 71  zSql8);.  }.  sq
17fdd 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 70  lite3ValueFree(p
17fde 56 61 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 73  Val);.  return s
17fdf 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 30 2c  qlite3ApiExit(0,
17fe0 20 72 63 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f   rc);.}.#endif /
17fe1 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  * SQLITE_OMIT_UT
17fe2 46 31 36 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a  F16 */.#endif /*
17fe3 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d   SQLITE_OMIT_COM
17fe4 50 4c 45 54 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  PLETE */../*****
17fe5 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
17fe6 20 63 6f 6d 70 6c 65 74 65 2e 63 20 2a 2a 2a 2a   complete.c ****
17fe7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17fe8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17fe9 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
17fea 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
17feb 66 69 6c 65 20 64 65 6c 65 74 65 2e 63 20 2a 2a  file delete.c **
17fec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17fed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17fee 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
17fef 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
17ff0 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
17ff1 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
17ff2 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
17ff3 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
17ff4 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
17ff5 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
17ff6 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
17ff7 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
17ff8 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
17ff9 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
17ffa 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
17ffb 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
17ffc 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
17ffd 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
17ffe 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
17fff 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
18000 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
18001 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
18002 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18003 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18004 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18005 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18006 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
18007 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20 72 6f  ntains C code ro
18008 75 74 69 6e 65 73 20 74 68 61 74 20 61 72 65 20  utines that are 
18009 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61  called by the pa
1800a 72 73 65 72 0a 2a 2a 20 69 6e 20 6f 72 64 65 72  rser.** in order
1800b 20 74 6f 20 67 65 6e 65 72 61 74 65 20 63 6f 64   to generate cod
1800c 65 20 66 6f 72 20 44 45 4c 45 54 45 20 46 52 4f  e for DELETE FRO
1800d 4d 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a  M statements..**
1800e 0a 2a 2a 20 24 49 64 3a 20 64 65 6c 65 74 65 2e  .** $Id: delete.
1800f 63 2c 76 20 31 2e 31 32 39 20 32 30 30 37 2f 30  c,v 1.129 2007/0
18010 34 2f 31 36 20 31 35 3a 30 36 3a 32 35 20 64 61  4/16 15:06:25 da
18011 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a  nielk1977 Exp $.
18012 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 6b 20 75  */../*.** Look u
18013 70 20 65 76 65 72 79 20 74 61 62 6c 65 20 74 68  p every table th
18014 61 74 20 69 73 20 6e 61 6d 65 64 20 69 6e 20 70  at is named in p
18015 53 72 63 2e 20 20 49 66 20 61 6e 79 20 74 61 62  Src.  If any tab
18016 6c 65 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2c  le is not found,
18017 0a 2a 2a 20 61 64 64 20 61 6e 20 65 72 72 6f 72  .** add an error
18018 20 6d 65 73 73 61 67 65 20 74 6f 20 70 50 61 72   message to pPar
18019 73 65 2d 3e 7a 45 72 72 4d 73 67 20 61 6e 64 20  se->zErrMsg and 
1801a 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 20 49 66  return NULL.  If
1801b 20 61 6c 6c 20 74 61 62 6c 65 73 0a 2a 2a 20 61   all tables.** a
1801c 72 65 20 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e  re found, return
1801d 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
1801e 65 20 6c 61 73 74 20 74 61 62 6c 65 2e 0a 2a 2f  e last table..*/
1801f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
18020 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 53 72  Table *sqlite3Sr
18021 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 50 61 72 73  cListLookup(Pars
18022 65 20 2a 70 50 61 72 73 65 2c 20 53 72 63 4c 69  e *pParse, SrcLi
18023 73 74 20 2a 70 53 72 63 29 7b 0a 20 20 54 61 62  st *pSrc){.  Tab
18024 6c 65 20 2a 70 54 61 62 20 3d 20 30 3b 0a 20 20  le *pTab = 0;.  
18025 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20  int i;.  struct 
18026 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  SrcList_item *pI
18027 74 65 6d 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20  tem;.  for(i=0, 
18028 70 49 74 65 6d 3d 70 53 72 63 2d 3e 61 3b 20 69  pItem=pSrc->a; i
18029 3c 70 53 72 63 2d 3e 6e 53 72 63 3b 20 69 2b 2b  <pSrc->nSrc; i++
1802a 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20  , pItem++){.    
1802b 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f  pTab = sqlite3Lo
1802c 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73 65  cateTable(pParse
1802d 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 2c 20  , pItem->zName, 
1802e 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65  pItem->zDatabase
1802f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  );.    sqlite3De
18030 6c 65 74 65 54 61 62 6c 65 28 70 49 74 65 6d 2d  leteTable(pItem-
18031 3e 70 54 61 62 29 3b 0a 20 20 20 20 70 49 74 65  >pTab);.    pIte
18032 6d 2d 3e 70 54 61 62 20 3d 20 70 54 61 62 3b 0a  m->pTab = pTab;.
18033 20 20 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a      if( pTab ){.
18034 20 20 20 20 20 20 70 54 61 62 2d 3e 6e 52 65 66        pTab->nRef
18035 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ++;.    }.  }.  
18036 72 65 74 75 72 6e 20 70 54 61 62 3b 0a 7d 0a 0a  return pTab;.}..
18037 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 6d  /*.** Check to m
18038 61 6b 65 20 73 75 72 65 20 74 68 65 20 67 69 76  ake sure the giv
18039 65 6e 20 74 61 62 6c 65 20 69 73 20 77 72 69 74  en table is writ
1803a 61 62 6c 65 2e 20 20 49 66 20 69 74 20 69 73 20  able.  If it is 
1803b 6e 6f 74 0a 2a 2a 20 77 72 69 74 61 62 6c 65 2c  not.** writable,
1803c 20 67 65 6e 65 72 61 74 65 20 61 6e 20 65 72 72   generate an err
1803d 6f 72 20 6d 65 73 73 61 67 65 20 61 6e 64 20 72  or message and r
1803e 65 74 75 72 6e 20 31 2e 20 20 49 66 20 69 74 20  eturn 1.  If it 
1803f 69 73 0a 2a 2a 20 77 72 69 74 61 62 6c 65 20 72  is.** writable r
18040 65 74 75 72 6e 20 30 3b 0a 2a 2f 0a 53 51 4c 49  eturn 0;.*/.SQLI
18041 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
18042 71 6c 69 74 65 33 49 73 52 65 61 64 4f 6e 6c 79  qlite3IsReadOnly
18043 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
18044 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 69 6e 74  Table *pTab, int
18045 20 76 69 65 77 4f 6b 29 7b 0a 20 20 69 66 28 20   viewOk){.  if( 
18046 28 70 54 61 62 2d 3e 72 65 61 64 4f 6e 6c 79 20  (pTab->readOnly 
18047 26 26 20 28 70 50 61 72 73 65 2d 3e 64 62 2d 3e  && (pParse->db->
18048 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 57  flags & SQLITE_W
18049 72 69 74 65 53 63 68 65 6d 61 29 3d 3d 30 0a 20  riteSchema)==0. 
1804a 20 20 20 20 20 20 20 26 26 20 70 50 61 72 73 65         && pParse
1804b 2d 3e 6e 65 73 74 65 64 3d 3d 30 29 20 0a 23 69  ->nested==0) .#i
1804c 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1804d 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
1804e 20 20 20 20 20 7c 7c 20 28 70 54 61 62 2d 3e 70       || (pTab->p
1804f 4d 6f 64 20 26 26 20 70 54 61 62 2d 3e 70 4d 6f  Mod && pTab->pMo
18050 64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 55 70 64  d->pModule->xUpd
18051 61 74 65 3d 3d 30 29 0a 23 65 6e 64 69 66 0a 20  ate==0).#endif. 
18052 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
18053 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
18054 22 74 61 62 6c 65 20 25 73 20 6d 61 79 20 6e 6f  "table %s may no
18055 74 20 62 65 20 6d 6f 64 69 66 69 65 64 22 2c 20  t be modified", 
18056 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  pTab->zName);.  
18057 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
18058 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
18059 4d 49 54 5f 56 49 45 57 0a 20 20 69 66 28 20 21  MIT_VIEW.  if( !
1805a 76 69 65 77 4f 6b 20 26 26 20 70 54 61 62 2d 3e  viewOk && pTab->
1805b 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73  pSelect ){.    s
1805c 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
1805d 50 61 72 73 65 2c 22 63 61 6e 6e 6f 74 20 6d 6f  Parse,"cannot mo
1805e 64 69 66 79 20 25 73 20 62 65 63 61 75 73 65 20  dify %s because 
1805f 69 74 20 69 73 20 61 20 76 69 65 77 22 2c 70 54  it is a view",pT
18060 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ab->zName);.    
18061 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65  return 1;.  }.#e
18062 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 30 3b  ndif.  return 0;
18063 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  .}../*.** Genera
18064 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c  te code that wil
18065 6c 20 6f 70 65 6e 20 61 20 74 61 62 6c 65 20 66  l open a table f
18066 6f 72 20 72 65 61 64 69 6e 67 2e 0a 2a 2f 0a 53  or reading..*/.S
18067 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
18068 69 64 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61  id sqlite3OpenTa
18069 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 2c  ble(.  Parse *p,
1806a 20 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61         /* Genera
1806b 74 65 20 63 6f 64 65 20 69 6e 74 6f 20 74 68 69  te code into thi
1806c 73 20 56 44 42 45 20 2a 2f 0a 20 20 69 6e 74 20  s VDBE */.  int 
1806d 69 43 75 72 2c 20 20 20 20 20 20 20 2f 2a 20 54  iCur,       /* T
1806e 68 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  he cursor number
1806f 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f   of the table */
18070 0a 20 20 69 6e 74 20 69 44 62 2c 20 20 20 20 20  .  int iDb,     
18071 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
18072 73 65 20 69 6e 64 65 78 20 69 6e 20 73 71 6c 69  se index in sqli
18073 74 65 33 2e 61 44 62 5b 5d 20 2a 2f 0a 20 20 54  te3.aDb[] */.  T
18074 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 2f  able *pTab,    /
18075 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20 62  * The table to b
18076 65 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 69 6e  e opened */.  in
18077 74 20 6f 70 63 6f 64 65 20 20 20 20 20 20 2f 2a  t opcode      /*
18078 20 4f 50 5f 4f 70 65 6e 52 65 61 64 20 6f 72 20   OP_OpenRead or 
18079 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 2a 2f 0a  OP_OpenWrite */.
1807a 29 7b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20  ){.  Vdbe *v;.  
1807b 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54  if( IsVirtual(pT
1807c 61 62 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ab) ) return;.  
1807d 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
1807e 62 65 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28  be(p);.  assert(
1807f 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e   opcode==OP_Open
18080 57 72 69 74 65 20 7c 7c 20 6f 70 63 6f 64 65 3d  Write || opcode=
18081 3d 4f 50 5f 4f 70 65 6e 52 65 61 64 20 29 3b 0a  =OP_OpenRead );.
18082 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f    sqlite3TableLo
18083 63 6b 28 70 2c 20 69 44 62 2c 20 70 54 61 62 2d  ck(p, iDb, pTab-
18084 3e 74 6e 75 6d 2c 20 28 6f 70 63 6f 64 65 3d 3d  >tnum, (opcode==
18085 4f 50 5f 4f 70 65 6e 57 72 69 74 65 29 2c 20 70  OP_OpenWrite), p
18086 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 73  Tab->zName);.  s
18087 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
18088 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69  v, OP_Integer, i
18089 44 62 2c 20 30 29 3b 0a 20 20 56 64 62 65 43 6f  Db, 0);.  VdbeCo
1808a 6d 6d 65 6e 74 28 28 76 2c 20 22 23 20 25 73 22  mment((v, "# %s"
1808b 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 29 3b  , pTab->zName));
1808c 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
1808d 64 4f 70 28 76 2c 20 6f 70 63 6f 64 65 2c 20 69  dOp(v, opcode, i
1808e 43 75 72 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 29  Cur, pTab->tnum)
1808f 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
18090 64 64 4f 70 28 76 2c 20 4f 50 5f 53 65 74 4e 75  ddOp(v, OP_SetNu
18091 6d 43 6f 6c 75 6d 6e 73 2c 20 69 43 75 72 2c 20  mColumns, iCur, 
18092 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 7d 0a 0a  pTab->nCol);.}..
18093 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
18094 63 6f 64 65 20 66 6f 72 20 61 20 44 45 4c 45 54  code for a DELET
18095 45 20 46 52 4f 4d 20 73 74 61 74 65 6d 65 6e 74  E FROM statement
18096 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 45 4c 45  ..**.**     DELE
18097 54 45 20 46 52 4f 4d 20 74 61 62 6c 65 5f 77 78  TE FROM table_wx
18098 79 7a 20 57 48 45 52 45 20 61 3c 35 20 41 4e 44  yz WHERE a<5 AND
18099 20 62 20 4e 4f 54 20 4e 55 4c 4c 3b 0a 2a 2a 20   b NOT NULL;.** 
1809a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1809b 5c 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 20  \________/      
1809c 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f   \______________
1809d 5f 5f 2f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  __/.**          
1809e 20 20 20 20 20 20 20 20 70 54 61 62 4c 69 73 74          pTabList
1809f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 57                pW
180a0 68 65 72 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  here.*/.SQLITE_P
180a1 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
180a2 74 65 33 44 65 6c 65 74 65 46 72 6f 6d 28 0a 20  te3DeleteFrom(. 
180a3 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
180a4 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
180a5 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
180a6 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62  .  SrcList *pTab
180a7 4c 69 73 74 2c 20 20 20 20 20 2f 2a 20 54 68 65  List,     /* The
180a8 20 74 61 62 6c 65 20 66 72 6f 6d 20 77 68 69 63   table from whic
180a9 68 20 77 65 20 73 68 6f 75 6c 64 20 64 65 6c 65  h we should dele
180aa 74 65 20 74 68 69 6e 67 73 20 2a 2f 0a 20 20 45  te things */.  E
180ab 78 70 72 20 2a 70 57 68 65 72 65 20 20 20 20 20  xpr *pWhere     
180ac 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45        /* The WHE
180ad 52 45 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20  RE clause.  May 
180ae 62 65 20 6e 75 6c 6c 20 2a 2f 0a 29 7b 0a 20 20  be null */.){.  
180af 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20  Vdbe *v;        
180b0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69         /* The vi
180b1 72 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 65  rtual database e
180b2 6e 67 69 6e 65 20 2a 2f 0a 20 20 54 61 62 6c 65  ngine */.  Table
180b3 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20   *pTab;         
180b4 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 66    /* The table f
180b5 72 6f 6d 20 77 68 69 63 68 20 72 65 63 6f 72 64  rom which record
180b6 73 20 77 69 6c 6c 20 62 65 20 64 65 6c 65 74 65  s will be delete
180b7 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  d */.  const cha
180b8 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 20 2f 2a  r *zDb;       /*
180b9 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73   Name of databas
180ba 65 20 68 6f 6c 64 69 6e 67 20 70 54 61 62 20 2a  e holding pTab *
180bb 2f 0a 20 20 69 6e 74 20 65 6e 64 2c 20 61 64 64  /.  int end, add
180bc 72 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 41 20  r = 0;     /* A 
180bd 63 6f 75 70 6c 65 20 61 64 64 72 65 73 73 65 73  couple addresses
180be 20 6f 66 20 67 65 6e 65 72 61 74 65 64 20 63 6f   of generated co
180bf 64 65 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20  de */.  int i;  
180c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
180c1 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
180c2 2f 0a 20 20 57 68 65 72 65 49 6e 66 6f 20 2a 70  /.  WhereInfo *p
180c3 57 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20 49 6e  WInfo;     /* In
180c4 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
180c5 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
180c6 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64   */.  Index *pId
180c7 78 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  x;           /* 
180c8 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72  For looping over
180c9 20 69 6e 64 69 63 65 73 20 6f 66 20 74 68 65 20   indices of the 
180ca 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69  table */.  int i
180cb 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  Cur;            
180cc 20 20 2f 2a 20 56 44 42 45 20 43 75 72 73 6f 72    /* VDBE Cursor
180cd 20 6e 75 6d 62 65 72 20 66 6f 72 20 70 54 61 62   number for pTab
180ce 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
180cf 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  b;           /* 
180d0 4d 61 69 6e 20 64 61 74 61 62 61 73 65 20 73 74  Main database st
180d1 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 41 75 74  ructure */.  Aut
180d2 68 43 6f 6e 74 65 78 74 20 73 43 6f 6e 74 65 78  hContext sContex
180d3 74 3b 20 20 2f 2a 20 41 75 74 68 6f 72 69 7a 61  t;  /* Authoriza
180d4 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  tion context */.
180d5 20 20 69 6e 74 20 6f 6c 64 49 64 78 20 3d 20 2d    int oldIdx = -
180d6 31 3b 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73  1;       /* Curs
180d7 6f 72 20 66 6f 72 20 74 68 65 20 4f 4c 44 20 74  or for the OLD t
180d8 61 62 6c 65 20 6f 66 20 41 46 54 45 52 20 74 72  able of AFTER tr
180d9 69 67 67 65 72 73 20 2a 2f 0a 20 20 4e 61 6d 65  iggers */.  Name
180da 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20 20 20  Context sNC;    
180db 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65     /* Name conte
180dc 78 74 20 74 6f 20 72 65 73 6f 6c 76 65 20 65 78  xt to resolve ex
180dd 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 2a 2f 0a  pressions in */.
180de 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20    int iDb;      
180df 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
180e0 62 61 73 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20  base number */. 
180e1 20 69 6e 74 20 6d 65 6d 43 6e 74 20 3d 20 30 3b   int memCnt = 0;
180e2 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72          /* Memor
180e3 79 20 63 65 6c 6c 20 75 73 65 64 20 66 6f 72 20  y cell used for 
180e4 63 68 61 6e 67 65 20 63 6f 75 6e 74 69 6e 67 20  change counting 
180e5 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
180e6 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a  TE_OMIT_TRIGGER.
180e7 20 20 69 6e 74 20 69 73 56 69 65 77 3b 20 20 20    int isView;   
180e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
180e9 2a 20 54 72 75 65 20 69 66 20 61 74 74 65 6d 70  * True if attemp
180ea 74 69 6e 67 20 74 6f 20 64 65 6c 65 74 65 20 66  ting to delete f
180eb 72 6f 6d 20 61 20 76 69 65 77 20 2a 2f 0a 20 20  rom a view */.  
180ec 69 6e 74 20 74 72 69 67 67 65 72 73 5f 65 78 69  int triggers_exi
180ed 73 74 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20  st = 0;      /* 
180ee 54 72 75 65 20 69 66 20 61 6e 79 20 74 72 69 67  True if any trig
180ef 67 65 72 73 20 65 78 69 73 74 20 2a 2f 0a 23 65  gers exist */.#e
180f0 6e 64 69 66 0a 0a 20 20 73 43 6f 6e 74 65 78 74  ndif..  sContext
180f1 2e 70 50 61 72 73 65 20 3d 20 30 3b 0a 20 20 69  .pParse = 0;.  i
180f2 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20  f( pParse->nErr 
180f3 7c 7c 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  || sqlite3Malloc
180f4 46 61 69 6c 65 64 28 29 20 29 7b 0a 20 20 20 20  Failed() ){.    
180f5 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d  goto delete_from
180f6 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20  _cleanup;.  }.  
180f7 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
180f8 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62 4c  .  assert( pTabL
180f9 69 73 74 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a  ist->nSrc==1 );.
180fa 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65  .  /* Locate the
180fb 20 74 61 62 6c 65 20 77 68 69 63 68 20 77 65 20   table which we 
180fc 77 61 6e 74 20 74 6f 20 64 65 6c 65 74 65 2e 20  want to delete. 
180fd 20 54 68 69 73 20 74 61 62 6c 65 20 68 61 73 20   This table has 
180fe 74 6f 20 62 65 0a 20 20 2a 2a 20 70 75 74 20 69  to be.  ** put i
180ff 6e 20 61 6e 20 53 72 63 4c 69 73 74 20 73 74 72  n an SrcList str
18100 75 63 74 75 72 65 20 62 65 63 61 75 73 65 20 73  ucture because s
18101 6f 6d 65 20 6f 66 20 74 68 65 20 73 75 62 72 6f  ome of the subro
18102 75 74 69 6e 65 73 20 77 65 0a 20 20 2a 2a 20 77  utines we.  ** w
18103 69 6c 6c 20 62 65 20 63 61 6c 6c 69 6e 67 20 61  ill be calling a
18104 72 65 20 64 65 73 69 67 6e 65 64 20 74 6f 20 77  re designed to w
18105 6f 72 6b 20 77 69 74 68 20 6d 75 6c 74 69 70 6c  ork with multipl
18106 65 20 74 61 62 6c 65 73 20 61 6e 64 20 65 78 70  e tables and exp
18107 65 63 74 0a 20 20 2a 2a 20 61 6e 20 53 72 63 4c  ect.  ** an SrcL
18108 69 73 74 2a 20 70 61 72 61 6d 65 74 65 72 20 69  ist* parameter i
18109 6e 73 74 65 61 64 20 6f 66 20 6a 75 73 74 20 61  nstead of just a
1810a 20 54 61 62 6c 65 2a 20 70 61 72 61 6d 65 74 65   Table* paramete
1810b 72 2e 0a 20 20 2a 2f 0a 20 20 70 54 61 62 20 3d  r..  */.  pTab =
1810c 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 4c   sqlite3SrcListL
1810d 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c 20 70 54  ookup(pParse, pT
1810e 61 62 4c 69 73 74 29 3b 0a 20 20 69 66 28 20 70  abList);.  if( p
1810f 54 61 62 3d 3d 30 20 29 20 20 67 6f 74 6f 20 64  Tab==0 )  goto d
18110 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e  elete_from_clean
18111 75 70 3b 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65  up;..  /* Figure
18112 20 6f 75 74 20 69 66 20 77 65 20 68 61 76 65 20   out if we have 
18113 61 6e 79 20 74 72 69 67 67 65 72 73 20 61 6e 64  any triggers and
18114 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 62 65   if the table be
18115 69 6e 67 0a 20 20 2a 2a 20 64 65 6c 65 74 65 64  ing.  ** deleted
18116 20 66 72 6f 6d 20 69 73 20 61 20 76 69 65 77 0a   from is a view.
18117 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c    */.#ifndef SQL
18118 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
18119 0a 20 20 74 72 69 67 67 65 72 73 5f 65 78 69 73  .  triggers_exis
1811a 74 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67  t = sqlite3Trigg
1811b 65 72 73 45 78 69 73 74 28 70 50 61 72 73 65 2c  ersExist(pParse,
1811c 20 70 54 61 62 2c 20 54 4b 5f 44 45 4c 45 54 45   pTab, TK_DELETE
1811d 2c 20 30 29 3b 0a 20 20 69 73 56 69 65 77 20 3d  , 0);.  isView =
1811e 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 21 3d   pTab->pSelect!=
1811f 30 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  0;.#else.# defin
18120 65 20 74 72 69 67 67 65 72 73 5f 65 78 69 73 74  e triggers_exist
18121 20 30 0a 23 20 64 65 66 69 6e 65 20 69 73 56 69   0.# define isVi
18122 65 77 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 64  ew 0.#endif.#ifd
18123 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
18124 49 45 57 0a 23 20 75 6e 64 65 66 20 69 73 56 69  IEW.# undef isVi
18125 65 77 0a 23 20 64 65 66 69 6e 65 20 69 73 56 69  ew.# define isVi
18126 65 77 20 30 0a 23 65 6e 64 69 66 0a 0a 20 20 69  ew 0.#endif..  i
18127 66 28 20 73 71 6c 69 74 65 33 49 73 52 65 61 64  f( sqlite3IsRead
18128 4f 6e 6c 79 28 70 50 61 72 73 65 2c 20 70 54 61  Only(pParse, pTa
18129 62 2c 20 74 72 69 67 67 65 72 73 5f 65 78 69 73  b, triggers_exis
1812a 74 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64  t) ){.    goto d
1812b 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e  elete_from_clean
1812c 75 70 3b 0a 20 20 7d 0a 20 20 69 44 62 20 3d 20  up;.  }.  iDb = 
1812d 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
1812e 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70  ndex(db, pTab->p
1812f 53 63 68 65 6d 61 29 3b 0a 20 20 61 73 73 65 72  Schema);.  asser
18130 74 28 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29  t( iDb<db->nDb )
18131 3b 0a 20 20 7a 44 62 20 3d 20 64 62 2d 3e 61 44  ;.  zDb = db->aD
18132 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  b[iDb].zName;.  
18133 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43  if( sqlite3AuthC
18134 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c  heck(pParse, SQL
18135 49 54 45 5f 44 45 4c 45 54 45 2c 20 70 54 61 62  ITE_DELETE, pTab
18136 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 20 7a 44 62 29  ->zName, 0, zDb)
18137 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c   ){.    goto del
18138 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70  ete_from_cleanup
18139 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 70  ;.  }..  /* If p
1813a 54 61 62 20 69 73 20 72 65 61 6c 6c 79 20 61 20  Tab is really a 
1813b 76 69 65 77 2c 20 6d 61 6b 65 20 73 75 72 65 20  view, make sure 
1813c 69 74 20 68 61 73 20 62 65 65 6e 20 69 6e 69 74  it has been init
1813d 69 61 6c 69 7a 65 64 2e 0a 20 20 2a 2f 0a 20 20  ialized..  */.  
1813e 69 66 28 20 73 71 6c 69 74 65 33 56 69 65 77 47  if( sqlite3ViewG
1813f 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50  etColumnNames(pP
18140 61 72 73 65 2c 20 70 54 61 62 29 20 29 7b 0a 20  arse, pTab) ){. 
18141 20 20 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66     goto delete_f
18142 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  rom_cleanup;.  }
18143 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20  ..  /* Allocate 
18144 61 20 63 75 72 73 6f 72 20 75 73 65 64 20 74 6f  a cursor used to
18145 20 73 74 6f 72 65 20 74 68 65 20 6f 6c 64 2e 2a   store the old.*
18146 20 64 61 74 61 20 66 6f 72 20 61 20 74 72 69 67   data for a trig
18147 67 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ger..  */.  if( 
18148 74 72 69 67 67 65 72 73 5f 65 78 69 73 74 20 29  triggers_exist )
18149 7b 20 0a 20 20 20 20 6f 6c 64 49 64 78 20 3d 20  { .    oldIdx = 
1814a 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a  pParse->nTab++;.
1814b 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76    }..  /* Resolv
1814c 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d  e the column nam
1814d 65 73 20 69 6e 20 74 68 65 20 57 48 45 52 45 20  es in the WHERE 
1814e 63 6c 61 75 73 65 2e 0a 20 20 2a 2f 0a 20 20 61  clause..  */.  a
1814f 73 73 65 72 74 28 20 70 54 61 62 4c 69 73 74 2d  ssert( pTabList-
18150 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 69 43  >nSrc==1 );.  iC
18151 75 72 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61  ur = pTabList->a
18152 5b 30 5d 2e 69 43 75 72 73 6f 72 20 3d 20 70 50  [0].iCursor = pP
18153 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20  arse->nTab++;.  
18154 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c 20  memset(&sNC, 0, 
18155 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20  sizeof(sNC));.  
18156 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 50 61  sNC.pParse = pPa
18157 72 73 65 3b 0a 20 20 73 4e 43 2e 70 53 72 63 4c  rse;.  sNC.pSrcL
18158 69 73 74 20 3d 20 70 54 61 62 4c 69 73 74 3b 0a  ist = pTabList;.
18159 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 78 70    if( sqlite3Exp
1815a 72 52 65 73 6f 6c 76 65 4e 61 6d 65 73 28 26 73  rResolveNames(&s
1815b 4e 43 2c 20 70 57 68 65 72 65 29 20 29 7b 0a 20  NC, pWhere) ){. 
1815c 20 20 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66     goto delete_f
1815d 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  rom_cleanup;.  }
1815e 0a 0a 20 20 2f 2a 20 53 74 61 72 74 20 74 68 65  ..  /* Start the
1815f 20 76 69 65 77 20 63 6f 6e 74 65 78 74 0a 20 20   view context.  
18160 2a 2f 0a 20 20 69 66 28 20 69 73 56 69 65 77 20  */.  if( isView 
18161 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 75  ){.    sqlite3Au
18162 74 68 43 6f 6e 74 65 78 74 50 75 73 68 28 70 50  thContextPush(pP
18163 61 72 73 65 2c 20 26 73 43 6f 6e 74 65 78 74 2c  arse, &sContext,
18164 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20   pTab->zName);. 
18165 20 7d 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 67   }..  /* Begin g
18166 65 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 2e 0a  enerating code..
18167 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74    */.  v = sqlit
18168 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
18169 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 7b  );.  if( v==0 ){
1816a 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c 65 74 65  .    goto delete
1816b 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20  _from_cleanup;. 
1816c 20 7d 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d   }.  if( pParse-
1816d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 20 73 71 6c  >nested==0 ) sql
1816e 69 74 65 33 56 64 62 65 43 6f 75 6e 74 43 68 61  ite3VdbeCountCha
1816f 6e 67 65 73 28 76 29 3b 0a 20 20 73 71 6c 69 74  nges(v);.  sqlit
18170 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72  e3BeginWriteOper
18171 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 74 72  ation(pParse, tr
18172 69 67 67 65 72 73 5f 65 78 69 73 74 2c 20 69 44  iggers_exist, iD
18173 62 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20  b);..  /* If we 
18174 61 72 65 20 74 72 79 69 6e 67 20 74 6f 20 64 65  are trying to de
18175 6c 65 74 65 20 66 72 6f 6d 20 61 20 76 69 65 77  lete from a view
18176 2c 20 72 65 61 6c 69 7a 65 20 74 68 61 74 20 76  , realize that v
18177 69 65 77 20 69 6e 74 6f 0a 20 20 2a 2a 20 61 20  iew into.  ** a 
18178 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 2e  ephemeral table.
18179 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 56 69  .  */.  if( isVi
1817a 65 77 20 29 7b 0a 20 20 20 20 53 65 6c 65 63 74  ew ){.    Select
1817b 20 2a 70 56 69 65 77 20 3d 20 73 71 6c 69 74 65   *pView = sqlite
1817c 33 53 65 6c 65 63 74 44 75 70 28 70 54 61 62 2d  3SelectDup(pTab-
1817d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 73  >pSelect);.    s
1817e 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61  qlite3Select(pPa
1817f 72 73 65 2c 20 70 56 69 65 77 2c 20 53 52 54 5f  rse, pView, SRT_
18180 45 70 68 65 6d 54 61 62 2c 20 69 43 75 72 2c 20  EphemTab, iCur, 
18181 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20  0, 0, 0, 0);.   
18182 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
18183 6c 65 74 65 28 70 56 69 65 77 29 3b 0a 20 20 7d  lete(pView);.  }
18184 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a  ..  /* Initializ
18185 65 20 74 68 65 20 63 6f 75 6e 74 65 72 20 6f 66  e the counter of
18186 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72   the number of r
18187 6f 77 73 20 64 65 6c 65 74 65 64 2c 20 69 66 0a  ows deleted, if.
18188 20 20 2a 2a 20 77 65 20 61 72 65 20 63 6f 75 6e    ** we are coun
18189 74 69 6e 67 20 72 6f 77 73 2e 0a 20 20 2a 2f 0a  ting rows..  */.
1818a 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 20    if( db->flags 
1818b 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f  & SQLITE_CountRo
1818c 77 73 20 29 7b 0a 20 20 20 20 6d 65 6d 43 6e 74  ws ){.    memCnt
1818d 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b   = pParse->nMem+
1818e 2b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  +;.    sqlite3Vd
1818f 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65  beAddOp(v, OP_Me
18190 6d 49 6e 74 2c 20 30 2c 20 6d 65 6d 43 6e 74 29  mInt, 0, memCnt)
18191 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 70 65 63  ;.  }..  /* Spec
18192 69 61 6c 20 63 61 73 65 3a 20 41 20 44 45 4c 45  ial case: A DELE
18193 54 45 20 77 69 74 68 6f 75 74 20 61 20 57 48 45  TE without a WHE
18194 52 45 20 63 6c 61 75 73 65 20 64 65 6c 65 74 65  RE clause delete
18195 73 20 65 76 65 72 79 74 68 69 6e 67 2e 0a 20 20  s everything..  
18196 2a 2a 20 49 74 20 69 73 20 65 61 73 69 65 72 20  ** It is easier 
18197 6a 75 73 74 20 74 6f 20 65 72 61 73 65 20 74 68  just to erase th
18198 65 20 77 68 6f 6c 65 20 74 61 62 6c 65 2e 20 20  e whole table.  
18199 4e 6f 74 65 2c 20 68 6f 77 65 76 65 72 2c 20 74  Note, however, t
1819a 68 61 74 0a 20 20 2a 2a 20 74 68 69 73 20 6d 65  hat.  ** this me
1819b 61 6e 73 20 74 68 61 74 20 74 68 65 20 72 6f 77  ans that the row
1819c 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 20 77 69   change count wi
1819d 6c 6c 20 62 65 20 69 6e 63 6f 72 72 65 63 74 2e  ll be incorrect.
1819e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 57 68 65  .  */.  if( pWhe
1819f 72 65 3d 3d 30 20 26 26 20 21 74 72 69 67 67 65  re==0 && !trigge
181a0 72 73 5f 65 78 69 73 74 20 26 26 20 21 49 73 56  rs_exist && !IsV
181a1 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a  irtual(pTab) ){.
181a2 20 20 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67      if( db->flag
181a3 73 20 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74  s & SQLITE_Count
181a4 52 6f 77 73 20 29 7b 0a 20 20 20 20 20 20 2f 2a  Rows ){.      /*
181a5 20 49 66 20 63 6f 75 6e 74 69 6e 67 20 72 6f 77   If counting row
181a6 73 20 64 65 6c 65 74 65 64 2c 20 6a 75 73 74 20  s deleted, just 
181a7 63 6f 75 6e 74 20 74 68 65 20 74 6f 74 61 6c 20  count the total 
181a8 6e 75 6d 62 65 72 20 6f 66 0a 20 20 20 20 20 20  number of.      
181a9 2a 2a 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68  ** entries in th
181aa 65 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20  e table. */.    
181ab 20 20 69 6e 74 20 65 6e 64 4f 66 4c 6f 6f 70 20    int endOfLoop 
181ac 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
181ad 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20  eLabel(v);.     
181ae 20 69 6e 74 20 61 64 64 72 32 3b 0a 20 20 20 20   int addr2;.    
181af 20 20 69 66 28 20 21 69 73 56 69 65 77 20 29 7b    if( !isView ){
181b0 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
181b1 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65  OpenTable(pParse
181b2 2c 20 69 43 75 72 2c 20 69 44 62 2c 20 70 54 61  , iCur, iDb, pTa
181b3 62 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29 3b  b, OP_OpenRead);
181b4 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
181b5 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
181b6 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69 43  v, OP_Rewind, iC
181b7 75 72 2c 20 73 71 6c 69 74 65 33 56 64 62 65 43  ur, sqlite3VdbeC
181b8 75 72 72 65 6e 74 41 64 64 72 28 76 29 2b 32 29  urrentAddr(v)+2)
181b9 3b 0a 20 20 20 20 20 20 61 64 64 72 32 20 3d 20  ;.      addr2 = 
181ba 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
181bb 28 76 2c 20 4f 50 5f 4d 65 6d 49 6e 63 72 2c 20  (v, OP_MemIncr, 
181bc 31 2c 20 6d 65 6d 43 6e 74 29 3b 0a 20 20 20 20  1, memCnt);.    
181bd 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
181be 4f 70 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 69  Op(v, OP_Next, i
181bf 43 75 72 2c 20 61 64 64 72 32 29 3b 0a 20 20 20  Cur, addr2);.   
181c0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65     sqlite3VdbeRe
181c1 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 65 6e  solveLabel(v, en
181c2 64 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 20 20 20  dOfLoop);.      
181c3 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
181c4 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43  (v, OP_Close, iC
181c5 75 72 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20  ur, 0);.    }.  
181c6 20 20 69 66 28 20 21 69 73 56 69 65 77 20 29 7b    if( !isView ){
181c7 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
181c8 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6c  beAddOp(v, OP_Cl
181c9 65 61 72 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c  ear, pTab->tnum,
181ca 20 69 44 62 29 3b 0a 20 20 20 20 20 20 69 66 28   iDb);.      if(
181cb 20 21 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64   !pParse->nested
181cc 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
181cd 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28  te3VdbeChangeP3(
181ce 76 2c 20 2d 31 2c 20 70 54 61 62 2d 3e 7a 4e 61  v, -1, pTab->zNa
181cf 6d 65 2c 20 50 33 5f 53 54 41 54 49 43 29 3b 0a  me, P3_STATIC);.
181d0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f        }.      fo
181d1 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  r(pIdx=pTab->pIn
181d2 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
181d3 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  pIdx->pNext){.  
181d4 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49        assert( pI
181d5 64 78 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61  dx->pSchema==pTa
181d6 62 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0a 20 20  b->pSchema );.  
181d7 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
181d8 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6c 65  eAddOp(v, OP_Cle
181d9 61 72 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20  ar, pIdx->tnum, 
181da 69 44 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  iDb);.      }.  
181db 20 20 7d 0a 20 20 7d 20 0a 20 20 2f 2a 20 54 68    }.  } .  /* Th
181dc 65 20 75 73 75 61 6c 20 63 61 73 65 3a 20 54 68  e usual case: Th
181dd 65 72 65 20 69 73 20 61 20 57 48 45 52 45 20 63  ere is a WHERE c
181de 6c 61 75 73 65 20 73 6f 20 77 65 20 68 61 76 65  lause so we have
181df 20 74 6f 20 73 63 61 6e 20 74 68 72 6f 75 67 68   to scan through
181e0 0a 20 20 2a 2a 20 74 68 65 20 74 61 62 6c 65 20  .  ** the table 
181e1 61 6e 64 20 70 69 63 6b 20 77 68 69 63 68 20 72  and pick which r
181e2 65 63 6f 72 64 73 20 74 6f 20 64 65 6c 65 74 65  ecords to delete
181e3 2e 0a 20 20 2a 2f 0a 20 20 65 6c 73 65 7b 0a 20  ..  */.  else{. 
181e4 20 20 20 2f 2a 20 42 65 67 69 6e 20 74 68 65 20     /* Begin the 
181e5 64 61 74 61 62 61 73 65 20 73 63 61 6e 0a 20 20  database scan.  
181e6 20 20 2a 2f 0a 20 20 20 20 70 57 49 6e 66 6f 20    */.    pWInfo 
181e7 3d 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65  = sqlite3WhereBe
181e8 67 69 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62  gin(pParse, pTab
181e9 4c 69 73 74 2c 20 70 57 68 65 72 65 2c 20 30 29  List, pWhere, 0)
181ea 3b 0a 20 20 20 20 69 66 28 20 70 57 49 6e 66 6f  ;.    if( pWInfo
181eb 3d 3d 30 20 29 20 67 6f 74 6f 20 64 65 6c 65 74  ==0 ) goto delet
181ec 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a  e_from_cleanup;.
181ed 0a 20 20 20 20 2f 2a 20 52 65 6d 65 6d 62 65 72  .    /* Remember
181ee 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 65 76   the rowid of ev
181ef 65 72 79 20 69 74 65 6d 20 74 6f 20 62 65 20 64  ery item to be d
181f0 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20  eleted..    */. 
181f1 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
181f2 64 4f 70 28 76 2c 20 49 73 56 69 72 74 75 61 6c  dOp(v, IsVirtual
181f3 28 70 54 61 62 29 20 3f 20 4f 50 5f 56 52 6f 77  (pTab) ? OP_VRow
181f4 69 64 20 3a 20 4f 50 5f 52 6f 77 69 64 2c 20 69  id : OP_Rowid, i
181f5 43 75 72 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  Cur, 0);.    sql
181f6 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
181f7 20 4f 50 5f 46 69 66 6f 57 72 69 74 65 2c 20 30   OP_FifoWrite, 0
181f8 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 64 62  , 0);.    if( db
181f9 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
181fa 5f 43 6f 75 6e 74 52 6f 77 73 20 29 7b 0a 20 20  _CountRows ){.  
181fb 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
181fc 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 49 6e  ddOp(v, OP_MemIn
181fd 63 72 2c 20 31 2c 20 6d 65 6d 43 6e 74 29 3b 0a  cr, 1, memCnt);.
181fe 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 45 6e      }..    /* En
181ff 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  d the database s
18200 63 61 6e 20 6c 6f 6f 70 2e 0a 20 20 20 20 2a 2f  can loop..    */
18201 0a 20 20 20 20 73 71 6c 69 74 65 33 57 68 65 72  .    sqlite3Wher
18202 65 45 6e 64 28 70 57 49 6e 66 6f 29 3b 0a 0a 20  eEnd(pWInfo);.. 
18203 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 70     /* Open the p
18204 73 65 75 64 6f 2d 74 61 62 6c 65 20 75 73 65 64  seudo-table used
18205 20 74 6f 20 73 74 6f 72 65 20 4f 4c 44 20 69 66   to store OLD if
18206 20 74 68 65 72 65 20 61 72 65 20 74 72 69 67 67   there are trigg
18207 65 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ers..    */.    
18208 69 66 28 20 74 72 69 67 67 65 72 73 5f 65 78 69  if( triggers_exi
18209 73 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  st ){.      sqli
1820a 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1820b 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f 2c 20 6f  OP_OpenPseudo, o
1820c 6c 64 49 64 78 2c 20 30 29 3b 0a 20 20 20 20 20  ldIdx, 0);.     
1820d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1820e 70 28 76 2c 20 4f 50 5f 53 65 74 4e 75 6d 43 6f  p(v, OP_SetNumCo
1820f 6c 75 6d 6e 73 2c 20 6f 6c 64 49 64 78 2c 20 70  lumns, oldIdx, p
18210 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20  Tab->nCol);.    
18211 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65 74 65  }..    /* Delete
18212 20 65 76 65 72 79 20 69 74 65 6d 20 77 68 6f 73   every item whos
18213 65 20 6b 65 79 20 77 61 73 20 77 72 69 74 74 65  e key was writte
18214 6e 20 74 6f 20 74 68 65 20 6c 69 73 74 20 64 75  n to the list du
18215 72 69 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a 20  ring the.    ** 
18216 64 61 74 61 62 61 73 65 20 73 63 61 6e 2e 20 20  database scan.  
18217 57 65 20 68 61 76 65 20 74 6f 20 64 65 6c 65 74  We have to delet
18218 65 20 69 74 65 6d 73 20 61 66 74 65 72 20 74 68  e items after th
18219 65 20 73 63 61 6e 20 69 73 20 63 6f 6d 70 6c 65  e scan is comple
1821a 74 65 0a 20 20 20 20 2a 2a 20 62 65 63 61 75 73  te.    ** becaus
1821b 65 20 64 65 6c 65 74 69 6e 67 20 61 6e 20 69 74  e deleting an it
1821c 65 6d 20 63 61 6e 20 63 68 61 6e 67 65 20 74 68  em can change th
1821d 65 20 73 63 61 6e 20 6f 72 64 65 72 2e 0a 20 20  e scan order..  
1821e 20 20 2a 2f 0a 20 20 20 20 65 6e 64 20 3d 20 73    */.    end = s
1821f 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
18220 62 65 6c 28 76 29 3b 0a 0a 20 20 20 20 2f 2a 20  bel(v);..    /* 
18221 54 68 69 73 20 69 73 20 74 68 65 20 62 65 67 69  This is the begi
18222 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 64 65 6c  nning of the del
18223 65 74 65 20 6c 6f 6f 70 20 77 68 65 6e 20 74 68  ete loop when th
18224 65 72 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 72  ere are.    ** r
18225 6f 77 20 74 72 69 67 67 65 72 73 2e 0a 20 20 20  ow triggers..   
18226 20 2a 2f 0a 20 20 20 20 69 66 28 20 74 72 69 67   */.    if( trig
18227 67 65 72 73 5f 65 78 69 73 74 20 29 7b 0a 20 20  gers_exist ){.  
18228 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74      addr = sqlit
18229 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1822a 50 5f 46 69 66 6f 52 65 61 64 2c 20 30 2c 20 65  P_FifoRead, 0, e
1822b 6e 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21  nd);.      if( !
1822c 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 20 20  isView ){.      
1822d 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1822e 4f 70 28 76 2c 20 4f 50 5f 44 75 70 2c 20 30 2c  Op(v, OP_Dup, 0,
1822f 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c   0);.        sql
18230 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50  ite3OpenTable(pP
18231 61 72 73 65 2c 20 69 43 75 72 2c 20 69 44 62 2c  arse, iCur, iDb,
18232 20 70 54 61 62 2c 20 4f 50 5f 4f 70 65 6e 52 65   pTab, OP_OpenRe
18233 61 64 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ad);.      }.   
18234 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
18235 64 4f 70 28 76 2c 20 4f 50 5f 4d 6f 76 65 47 65  dOp(v, OP_MoveGe
18236 2c 20 69 43 75 72 2c 20 30 29 3b 0a 20 20 20 20  , iCur, 0);.    
18237 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
18238 4f 70 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20  Op(v, OP_Rowid, 
18239 69 43 75 72 2c 20 30 29 3b 0a 20 20 20 20 20 20  iCur, 0);.      
1823a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1823b 28 76 2c 20 4f 50 5f 52 6f 77 44 61 74 61 2c 20  (v, OP_RowData, 
1823c 69 43 75 72 2c 20 30 29 3b 0a 20 20 20 20 20 20  iCur, 0);.      
1823d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1823e 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 6f  (v, OP_Insert, o
1823f 6c 64 49 64 78 2c 20 30 29 3b 0a 20 20 20 20 20  ldIdx, 0);.     
18240 20 69 66 28 20 21 69 73 56 69 65 77 20 29 7b 0a   if( !isView ){.
18241 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
18242 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43  dbeAddOp(v, OP_C
18243 6c 6f 73 65 2c 20 69 43 75 72 2c 20 30 29 3b 0a  lose, iCur, 0);.
18244 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 28        }..      (
18245 76 6f 69 64 29 73 71 6c 69 74 65 33 43 6f 64 65  void)sqlite3Code
18246 52 6f 77 54 72 69 67 67 65 72 28 70 50 61 72 73  RowTrigger(pPars
18247 65 2c 20 54 4b 5f 44 45 4c 45 54 45 2c 20 30 2c  e, TK_DELETE, 0,
18248 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 2c   TRIGGER_BEFORE,
18249 20 70 54 61 62 2c 0a 20 20 20 20 20 20 20 20 20   pTab,.         
1824a 20 2d 31 2c 20 6f 6c 64 49 64 78 2c 20 28 70 50   -1, oldIdx, (pP
1824b 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 29  arse->trigStack)
1824c 3f 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61  ?pParse->trigSta
1824d 63 6b 2d 3e 6f 72 63 6f 6e 66 3a 4f 45 5f 44 65  ck->orconf:OE_De
1824e 66 61 75 6c 74 2c 0a 20 20 20 20 20 20 20 20 20  fault,.         
1824f 20 61 64 64 72 29 3b 0a 20 20 20 20 7d 0a 0a 20   addr);.    }.. 
18250 20 20 20 69 66 28 20 21 69 73 56 69 65 77 20 29     if( !isView )
18251 7b 0a 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20  {.      /* Open 
18252 63 75 72 73 6f 72 73 20 66 6f 72 20 74 68 65 20  cursors for the 
18253 74 61 62 6c 65 20 77 65 20 61 72 65 20 64 65 6c  table we are del
18254 65 74 69 6e 67 20 66 72 6f 6d 20 61 6e 64 20 61  eting from and a
18255 6c 6c 20 69 74 73 0a 20 20 20 20 20 20 2a 2a 20  ll its.      ** 
18256 69 6e 64 69 63 65 73 2e 20 20 49 66 20 74 68 65  indices.  If the
18257 72 65 20 61 72 65 20 72 6f 77 20 74 72 69 67 67  re are row trigg
18258 65 72 73 2c 20 74 68 69 73 20 68 61 70 70 65 6e  ers, this happen
18259 73 20 69 6e 73 69 64 65 20 74 68 65 0a 20 20 20  s inside the.   
1825a 20 20 20 2a 2a 20 4f 50 5f 46 69 66 6f 52 65 61     ** OP_FifoRea
1825b 64 20 6c 6f 6f 70 20 62 65 63 61 75 73 65 20 74  d loop because t
1825c 68 65 20 63 75 72 73 6f 72 20 68 61 76 65 20 74  he cursor have t
1825d 6f 20 61 6c 6c 20 62 65 20 63 6c 6f 73 65 64 0a  o all be closed.
1825e 20 20 20 20 20 20 2a 2a 20 62 65 66 6f 72 65 20        ** before 
1825f 74 68 65 20 74 72 69 67 67 65 72 20 66 69 72 65  the trigger fire
18260 73 2e 20 20 49 66 20 74 68 65 72 65 20 61 72 65  s.  If there are
18261 20 6e 6f 20 72 6f 77 20 74 72 69 67 67 65 72 73   no row triggers
18262 2c 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 63  , the.      ** c
18263 75 72 73 6f 72 73 20 61 72 65 20 6f 70 65 6e 65  ursors are opene
18264 64 20 6f 6e 6c 79 20 6f 6e 63 65 20 6f 6e 20 74  d only once on t
18265 68 65 20 6f 75 74 73 69 64 65 20 74 68 65 20 6c  he outside the l
18266 6f 6f 70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  oop..      */.  
18267 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54      sqlite3OpenT
18268 61 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 28 70  ableAndIndices(p
18269 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69 43 75  Parse, pTab, iCu
1826a 72 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 29  r, OP_OpenWrite)
1826b 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73  ;..      /* This
1826c 20 69 73 20 74 68 65 20 62 65 67 69 6e 6e 69 6e   is the beginnin
1826d 67 20 6f 66 20 74 68 65 20 64 65 6c 65 74 65 20  g of the delete 
1826e 6c 6f 6f 70 20 77 68 65 6e 20 74 68 65 72 65 20  loop when there 
1826f 61 72 65 20 6e 6f 0a 20 20 20 20 20 20 2a 2a 20  are no.      ** 
18270 72 6f 77 20 74 72 69 67 67 65 72 73 20 2a 2f 0a  row triggers */.
18271 20 20 20 20 20 20 69 66 28 20 21 74 72 69 67 67        if( !trigg
18272 65 72 73 5f 65 78 69 73 74 20 29 7b 20 0a 20 20  ers_exist ){ .  
18273 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c        addr = sql
18274 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
18275 20 4f 50 5f 46 69 66 6f 52 65 61 64 2c 20 30 2c   OP_FifoRead, 0,
18276 20 65 6e 64 29 3b 0a 20 20 20 20 20 20 7d 0a 0a   end);.      }..
18277 20 20 20 20 20 20 2f 2a 20 44 65 6c 65 74 65 20        /* Delete 
18278 74 68 65 20 72 6f 77 20 2a 2f 0a 23 69 66 6e 64  the row */.#ifnd
18279 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
1827a 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20  IRTUALTABLE.    
1827b 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28    if( IsVirtual(
1827c 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 20  pTab) ){.       
1827d 20 70 50 61 72 73 65 2d 3e 70 56 69 72 74 75 61   pParse->pVirtua
1827e 6c 4c 6f 63 6b 20 3d 20 70 54 61 62 3b 0a 20 20  lLock = pTab;.  
1827f 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
18280 65 4f 70 33 28 76 2c 20 4f 50 5f 56 55 70 64 61  eOp3(v, OP_VUpda
18281 74 65 2c 20 30 2c 20 31 2c 20 28 63 6f 6e 73 74  te, 0, 1, (const
18282 20 63 68 61 72 2a 29 70 54 61 62 2d 3e 70 56 74   char*)pTab->pVt
18283 61 62 2c 20 50 33 5f 56 54 41 42 29 3b 0a 20 20  ab, P3_VTAB);.  
18284 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66      }else.#endif
18285 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20  .      {.       
18286 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65   sqlite3Generate
18287 52 6f 77 44 65 6c 65 74 65 28 64 62 2c 20 76 2c  RowDelete(db, v,
18288 20 70 54 61 62 2c 20 69 43 75 72 2c 20 70 50 61   pTab, iCur, pPa
18289 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 29 3b  rse->nested==0);
1828a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
1828b 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20      /* If there 
1828c 61 72 65 20 72 6f 77 20 74 72 69 67 67 65 72 73  are row triggers
1828d 2c 20 63 6c 6f 73 65 20 61 6c 6c 20 63 75 72 73  , close all curs
1828e 6f 72 73 20 74 68 65 6e 20 69 6e 76 6f 6b 65 0a  ors then invoke.
1828f 20 20 20 20 2a 2a 20 74 68 65 20 41 46 54 45 52      ** the AFTER
18290 20 74 72 69 67 67 65 72 73 0a 20 20 20 20 2a 2f   triggers.    */
18291 0a 20 20 20 20 69 66 28 20 74 72 69 67 67 65 72  .    if( trigger
18292 73 5f 65 78 69 73 74 20 29 7b 0a 20 20 20 20 20  s_exist ){.     
18293 20 69 66 28 20 21 69 73 56 69 65 77 20 29 7b 0a   if( !isView ){.
18294 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 31 2c          for(i=1,
18295 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64   pIdx=pTab->pInd
18296 65 78 3b 20 70 49 64 78 3b 20 69 2b 2b 2c 20 70  ex; pIdx; i++, p
18297 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29  Idx=pIdx->pNext)
18298 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
18299 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1829a 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72 20 2b  OP_Close, iCur +
1829b 20 69 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 29 3b   i, pIdx->tnum);
1829c 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1829d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1829e 64 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  dOp(v, OP_Close,
1829f 20 69 43 75 72 2c 20 30 29 3b 0a 20 20 20 20 20   iCur, 0);.     
182a0 20 7d 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73   }.      (void)s
182a1 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69  qlite3CodeRowTri
182a2 67 67 65 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  gger(pParse, TK_
182a3 44 45 4c 45 54 45 2c 20 30 2c 20 54 52 49 47 47  DELETE, 0, TRIGG
182a4 45 52 5f 41 46 54 45 52 2c 20 70 54 61 62 2c 20  ER_AFTER, pTab, 
182a5 2d 31 2c 0a 20 20 20 20 20 20 20 20 20 20 6f 6c  -1,.          ol
182a6 64 49 64 78 2c 20 28 70 50 61 72 73 65 2d 3e 74  dIdx, (pParse->t
182a7 72 69 67 53 74 61 63 6b 29 3f 70 50 61 72 73 65  rigStack)?pParse
182a8 2d 3e 74 72 69 67 53 74 61 63 6b 2d 3e 6f 72 63  ->trigStack->orc
182a9 6f 6e 66 3a 4f 45 5f 44 65 66 61 75 6c 74 2c 0a  onf:OE_Default,.
182aa 20 20 20 20 20 20 20 20 20 20 61 64 64 72 29 3b            addr);
182ab 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 45  .    }..    /* E
182ac 6e 64 20 6f 66 20 74 68 65 20 64 65 6c 65 74 65  nd of the delete
182ad 20 6c 6f 6f 70 20 2a 2f 0a 20 20 20 20 73 71 6c   loop */.    sql
182ae 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
182af 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64   OP_Goto, 0, add
182b0 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  r);.    sqlite3V
182b1 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
182b2 76 2c 20 65 6e 64 29 3b 0a 0a 20 20 20 20 2f 2a  v, end);..    /*
182b3 20 43 6c 6f 73 65 20 74 68 65 20 63 75 72 73 6f   Close the curso
182b4 72 73 20 61 66 74 65 72 20 74 68 65 20 6c 6f 6f  rs after the loo
182b5 70 20 69 66 20 74 68 65 72 65 20 61 72 65 20 6e  p if there are n
182b6 6f 20 72 6f 77 20 74 72 69 67 67 65 72 73 20 2a  o row triggers *
182b7 2f 0a 20 20 20 20 69 66 28 20 21 74 72 69 67 67  /.    if( !trigg
182b8 65 72 73 5f 65 78 69 73 74 20 26 26 20 21 49 73  ers_exist && !Is
182b9 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b  Virtual(pTab) ){
182ba 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31 2c 20  .      for(i=1, 
182bb 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65  pIdx=pTab->pInde
182bc 78 3b 20 70 49 64 78 3b 20 69 2b 2b 2c 20 70 49  x; pIdx; i++, pI
182bd 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b  dx=pIdx->pNext){
182be 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
182bf 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
182c0 43 6c 6f 73 65 2c 20 69 43 75 72 20 2b 20 69 2c  Close, iCur + i,
182c1 20 70 49 64 78 2d 3e 74 6e 75 6d 29 3b 0a 20 20   pIdx->tnum);.  
182c2 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
182c3 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
182c4 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72 2c 20  OP_Close, iCur, 
182c5 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  0);.    }.  }.. 
182c6 20 2f 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20   /*.  ** Return 
182c7 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f  the number of ro
182c8 77 73 20 74 68 61 74 20 77 65 72 65 20 64 65 6c  ws that were del
182c9 65 74 65 64 2e 20 49 66 20 74 68 69 73 20 72 6f  eted. If this ro
182ca 75 74 69 6e 65 20 69 73 20 0a 20 20 2a 2a 20 67  utine is .  ** g
182cb 65 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 20 62  enerating code b
182cc 65 63 61 75 73 65 20 6f 66 20 61 20 63 61 6c 6c  ecause of a call
182cd 20 74 6f 20 73 71 6c 69 74 65 33 4e 65 73 74 65   to sqlite3Neste
182ce 64 50 61 72 73 65 28 29 2c 20 64 6f 20 6e 6f 74  dParse(), do not
182cf 0a 20 20 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65  .  ** invoke the
182d0 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69   callback functi
182d1 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64  on..  */.  if( d
182d2 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54  b->flags & SQLIT
182d3 45 5f 43 6f 75 6e 74 52 6f 77 73 20 26 26 20 70  E_CountRows && p
182d4 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30  Parse->nested==0
182d5 20 26 26 20 21 70 50 61 72 73 65 2d 3e 74 72 69   && !pParse->tri
182d6 67 53 74 61 63 6b 20 29 7b 0a 20 20 20 20 73 71  gStack ){.    sq
182d7 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
182d8 2c 20 4f 50 5f 4d 65 6d 4c 6f 61 64 2c 20 6d 65  , OP_MemLoad, me
182d9 6d 43 6e 74 2c 20 30 29 3b 0a 20 20 20 20 73 71  mCnt, 0);.    sq
182da 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
182db 2c 20 4f 50 5f 43 61 6c 6c 62 61 63 6b 2c 20 31  , OP_Callback, 1
182dc 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
182dd 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28  3VdbeSetNumCols(
182de 76 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  v, 1);.    sqlit
182df 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
182e0 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 0, COLNAME_N
182e1 41 4d 45 2c 20 22 72 6f 77 73 20 64 65 6c 65 74  AME, "rows delet
182e2 65 64 22 2c 20 50 33 5f 53 54 41 54 49 43 29 3b  ed", P3_STATIC);
182e3 0a 20 20 7d 0a 0a 64 65 6c 65 74 65 5f 66 72 6f  .  }..delete_fro
182e4 6d 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71 6c  m_cleanup:.  sql
182e5 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50  ite3AuthContextP
182e6 6f 70 28 26 73 43 6f 6e 74 65 78 74 29 3b 0a 20  op(&sContext);. 
182e7 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44   sqlite3SrcListD
182e8 65 6c 65 74 65 28 70 54 61 62 4c 69 73 74 29 3b  elete(pTabList);
182e9 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  .  sqlite3ExprDe
182ea 6c 65 74 65 28 70 57 68 65 72 65 29 3b 0a 20 20  lete(pWhere);.  
182eb 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  return;.}../*.**
182ec 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65   This routine ge
182ed 6e 65 72 61 74 65 73 20 56 44 42 45 20 63 6f 64  nerates VDBE cod
182ee 65 20 74 68 61 74 20 63 61 75 73 65 73 20 61 20  e that causes a 
182ef 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 61 0a  single row of a.
182f0 2a 2a 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20  ** single table 
182f1 74 6f 20 62 65 20 64 65 6c 65 74 65 64 2e 0a 2a  to be deleted..*
182f2 2a 0a 2a 2a 20 54 68 65 20 56 44 42 45 20 6d 75  *.** The VDBE mu
182f3 73 74 20 62 65 20 69 6e 20 61 20 70 61 72 74 69  st be in a parti
182f4 63 75 6c 61 72 20 73 74 61 74 65 20 77 68 65 6e  cular state when
182f5 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
182f6 20 63 61 6c 6c 65 64 2e 0a 2a 2a 20 54 68 65 73   called..** Thes
182f7 65 20 61 72 65 20 74 68 65 20 72 65 71 75 69 72  e are the requir
182f8 65 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  ements:.**.**   
182f9 31 2e 20 20 41 20 72 65 61 64 2f 77 72 69 74 65  1.  A read/write
182fa 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67   cursor pointing
182fb 20 74 6f 20 70 54 61 62 2c 20 74 68 65 20 74 61   to pTab, the ta
182fc 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ble containing t
182fd 68 65 20 72 6f 77 0a 2a 2a 20 20 20 20 20 20 20  he row.**       
182fe 74 6f 20 62 65 20 64 65 6c 65 74 65 64 2c 20 6d  to be deleted, m
182ff 75 73 74 20 62 65 20 6f 70 65 6e 65 64 20 61 73  ust be opened as
18300 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 22   cursor number "
18301 62 61 73 65 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 32  base"..**.**   2
18302 2e 20 20 52 65 61 64 2f 77 72 69 74 65 20 63 75  .  Read/write cu
18303 72 73 6f 72 73 20 66 6f 72 20 61 6c 6c 20 69 6e  rsors for all in
18304 64 69 63 65 73 20 6f 66 20 70 54 61 62 20 6d 75  dices of pTab mu
18305 73 74 20 62 65 20 6f 70 65 6e 20 61 73 0a 2a 2a  st be open as.**
18306 20 20 20 20 20 20 20 63 75 72 73 6f 72 20 6e 75         cursor nu
18307 6d 62 65 72 20 62 61 73 65 2b 69 20 66 6f 72 20  mber base+i for 
18308 74 68 65 20 69 2d 74 68 20 69 6e 64 65 78 2e 0a  the i-th index..
18309 2a 2a 0a 2a 2a 20 20 20 33 2e 20 20 54 68 65 20  **.**   3.  The 
1830a 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 6f 66  record number of
1830b 20 74 68 65 20 72 6f 77 20 74 6f 20 62 65 20 64   the row to be d
1830c 65 6c 65 74 65 64 20 6d 75 73 74 20 62 65 20 6f  eleted must be o
1830d 6e 20 74 68 65 20 74 6f 70 0a 2a 2a 20 20 20 20  n the top.**    
1830e 20 20 20 6f 66 20 74 68 65 20 73 74 61 63 6b 2e     of the stack.
1830f 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
18310 69 6e 65 20 70 6f 70 73 20 74 68 65 20 74 6f 70  ine pops the top
18311 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20 74 6f   of the stack to
18312 20 72 65 6d 6f 76 65 20 74 68 65 20 72 65 63 6f   remove the reco
18313 72 64 20 6e 75 6d 62 65 72 0a 2a 2a 20 61 6e 64  rd number.** and
18314 20 74 68 65 6e 20 67 65 6e 65 72 61 74 65 73 20   then generates 
18315 63 6f 64 65 20 74 6f 20 72 65 6d 6f 76 65 20 62  code to remove b
18316 6f 74 68 20 74 68 65 20 74 61 62 6c 65 20 72 65  oth the table re
18317 63 6f 72 64 20 61 6e 64 20 61 6c 6c 20 69 6e 64  cord and all ind
18318 65 78 0a 2a 2a 20 65 6e 74 72 69 65 73 20 74 68  ex.** entries th
18319 61 74 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 74  at point to that
1831a 20 72 65 63 6f 72 64 2e 0a 2a 2f 0a 53 51 4c 49   record..*/.SQLI
1831b 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1831c 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52  sqlite3GenerateR
1831d 6f 77 44 65 6c 65 74 65 28 0a 20 20 73 71 6c 69  owDelete(.  sqli
1831e 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 2f  te3 *db,       /
1831f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  * The database c
18320 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 69 6e  ontaining the in
18321 64 65 78 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76  dex */.  Vdbe *v
18322 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47  ,           /* G
18323 65 6e 65 72 61 74 65 20 63 6f 64 65 20 69 6e 74  enerate code int
18324 6f 20 74 68 69 73 20 56 44 42 45 20 2a 2f 0a 20  o this VDBE */. 
18325 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20   Table *pTab,   
18326 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 63 6f 6e      /* Table con
18327 74 61 69 6e 69 6e 67 20 74 68 65 20 72 6f 77 20  taining the row 
18328 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20 2a 2f  to be deleted */
18329 0a 20 20 69 6e 74 20 69 43 75 72 2c 20 20 20 20  .  int iCur,    
1832a 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20        /* Cursor 
1832b 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 74  number for the t
1832c 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 63 6f  able */.  int co
1832d 75 6e 74 20 20 20 20 20 20 20 20 20 20 2f 2a 20  unt          /* 
1832e 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 72 6f  Increment the ro
1832f 77 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72  w change counter
18330 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61 64 64   */.){.  int add
18331 72 3b 0a 20 20 61 64 64 72 20 3d 20 73 71 6c 69  r;.  addr = sqli
18332 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
18333 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20 69 43  OP_NotExists, iC
18334 75 72 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65  ur, 0);.  sqlite
18335 33 47 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65  3GenerateRowInde
18336 78 44 65 6c 65 74 65 28 76 2c 20 70 54 61 62 2c  xDelete(v, pTab,
18337 20 69 43 75 72 2c 20 30 29 3b 0a 20 20 73 71 6c   iCur, 0);.  sql
18338 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
18339 20 4f 50 5f 44 65 6c 65 74 65 2c 20 69 43 75 72   OP_Delete, iCur
1833a 2c 20 28 63 6f 75 6e 74 3f 4f 50 46 4c 41 47 5f  , (count?OPFLAG_
1833b 4e 43 48 41 4e 47 45 3a 30 29 29 3b 0a 20 20 69  NCHANGE:0));.  i
1833c 66 28 20 63 6f 75 6e 74 20 29 7b 0a 20 20 20 20  f( count ){.    
1833d 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1833e 65 50 33 28 76 2c 20 2d 31 2c 20 70 54 61 62 2d  eP3(v, -1, pTab-
1833f 3e 7a 4e 61 6d 65 2c 20 50 33 5f 53 54 41 54 49  >zName, P3_STATI
18340 43 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  C);.  }.  sqlite
18341 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
18342 20 61 64 64 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   addr);.}../*.**
18343 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65   This routine ge
18344 6e 65 72 61 74 65 73 20 56 44 42 45 20 63 6f 64  nerates VDBE cod
18345 65 20 74 68 61 74 20 63 61 75 73 65 73 20 74 68  e that causes th
18346 65 20 64 65 6c 65 74 69 6f 6e 20 6f 66 20 61 6c  e deletion of al
18347 6c 0a 2a 2a 20 69 6e 64 65 78 20 65 6e 74 72 69  l.** index entri
18348 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  es associated wi
18349 74 68 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20  th a single row 
1834a 6f 66 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c  of a single tabl
1834b 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 44 42  e..**.** The VDB
1834c 45 20 6d 75 73 74 20 62 65 20 69 6e 20 61 20 70  E must be in a p
1834d 61 72 74 69 63 75 6c 61 72 20 73 74 61 74 65 20  articular state 
1834e 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  when this routin
1834f 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 20  e is called..** 
18350 54 68 65 73 65 20 61 72 65 20 74 68 65 20 72 65  These are the re
18351 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a  quirements:.**.*
18352 2a 20 20 20 31 2e 20 20 41 20 72 65 61 64 2f 77  *   1.  A read/w
18353 72 69 74 65 20 63 75 72 73 6f 72 20 70 6f 69 6e  rite cursor poin
18354 74 69 6e 67 20 74 6f 20 70 54 61 62 2c 20 74 68  ting to pTab, th
18355 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69  e table containi
18356 6e 67 20 74 68 65 20 72 6f 77 0a 2a 2a 20 20 20  ng the row.**   
18357 20 20 20 20 74 6f 20 62 65 20 64 65 6c 65 74 65      to be delete
18358 64 2c 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 65  d, must be opene
18359 64 20 61 73 20 63 75 72 73 6f 72 20 6e 75 6d 62  d as cursor numb
1835a 65 72 20 22 69 43 75 72 22 2e 0a 2a 2a 0a 2a 2a  er "iCur"..**.**
1835b 20 20 20 32 2e 20 20 52 65 61 64 2f 77 72 69 74     2.  Read/writ
1835c 65 20 63 75 72 73 6f 72 73 20 66 6f 72 20 61 6c  e cursors for al
1835d 6c 20 69 6e 64 69 63 65 73 20 6f 66 20 70 54 61  l indices of pTa
1835e 62 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 20 61  b must be open a
1835f 73 0a 2a 2a 20 20 20 20 20 20 20 63 75 72 73 6f  s.**       curso
18360 72 20 6e 75 6d 62 65 72 20 69 43 75 72 2b 69 20  r number iCur+i 
18361 66 6f 72 20 74 68 65 20 69 2d 74 68 20 69 6e 64  for the i-th ind
18362 65 78 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 2e 20 20  ex..**.**   3.  
18363 54 68 65 20 22 69 43 75 72 22 20 63 75 72 73 6f  The "iCur" curso
18364 72 20 6d 75 73 74 20 62 65 20 70 6f 69 6e 74 69  r must be pointi
18365 6e 67 20 74 6f 20 74 68 65 20 72 6f 77 20 74 68  ng to the row th
18366 61 74 20 69 73 20 74 6f 20 62 65 0a 2a 2a 20 20  at is to be.**  
18367 20 20 20 20 20 64 65 6c 65 74 65 64 2e 0a 2a 2f       deleted..*/
18368 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
18369 76 6f 69 64 20 73 71 6c 69 74 65 33 47 65 6e 65  void sqlite3Gene
1836a 72 61 74 65 52 6f 77 49 6e 64 65 78 44 65 6c 65  rateRowIndexDele
1836b 74 65 28 0a 20 20 56 64 62 65 20 2a 76 2c 20 20  te(.  Vdbe *v,  
1836c 20 20 20 20 20 20 20 20 20 2f 2a 20 47 65 6e 65           /* Gene
1836d 72 61 74 65 20 63 6f 64 65 20 69 6e 74 6f 20 74  rate code into t
1836e 68 69 73 20 56 44 42 45 20 2a 2f 0a 20 20 54 61  his VDBE */.  Ta
1836f 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20  ble *pTab,      
18370 20 2f 2a 20 54 61 62 6c 65 20 63 6f 6e 74 61 69   /* Table contai
18371 6e 69 6e 67 20 74 68 65 20 72 6f 77 20 74 6f 20  ning the row to 
18372 62 65 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20  be deleted */.  
18373 69 6e 74 20 69 43 75 72 2c 20 20 20 20 20 20 20  int iCur,       
18374 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d     /* Cursor num
18375 62 65 72 20 66 6f 72 20 74 68 65 20 74 61 62 6c  ber for the tabl
18376 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 61 49 64  e */.  char *aId
18377 78 55 73 65 64 20 20 20 20 20 2f 2a 20 4f 6e 6c  xUsed     /* Onl
18378 79 20 64 65 6c 65 74 65 20 69 66 20 61 49 64 78  y delete if aIdx
18379 55 73 65 64 21 3d 30 20 26 26 20 61 49 64 78 55  Used!=0 && aIdxU
1837a 73 65 64 5b 69 5d 21 3d 30 20 2a 2f 0a 29 7b 0a  sed[i]!=0 */.){.
1837b 20 20 69 6e 74 20 69 3b 0a 20 20 49 6e 64 65 78    int i;.  Index
1837c 20 2a 70 49 64 78 3b 0a 0a 20 20 66 6f 72 28 69   *pIdx;..  for(i
1837d 3d 31 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70  =1, pIdx=pTab->p
1837e 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 69 2b 2b  Index; pIdx; i++
1837f 2c 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65  , pIdx=pIdx->pNe
18380 78 74 29 7b 0a 20 20 20 20 69 66 28 20 61 49 64  xt){.    if( aId
18381 78 55 73 65 64 21 3d 30 20 26 26 20 61 49 64 78  xUsed!=0 && aIdx
18382 55 73 65 64 5b 69 2d 31 5d 3d 3d 30 20 29 20 63  Used[i-1]==0 ) c
18383 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 73 71 6c  ontinue;.    sql
18384 69 74 65 33 47 65 6e 65 72 61 74 65 49 6e 64 65  ite3GenerateInde
18385 78 4b 65 79 28 76 2c 20 70 49 64 78 2c 20 69 43  xKey(v, pIdx, iC
18386 75 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ur);.    sqlite3
18387 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
18388 49 64 78 44 65 6c 65 74 65 2c 20 69 43 75 72 2b  IdxDelete, iCur+
18389 69 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  i, 0);.  }.}../*
1838a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
1838b 65 20 74 68 61 74 20 77 69 6c 6c 20 61 73 73 65  e that will asse
1838c 6d 62 6c 65 20 61 6e 20 69 6e 64 65 78 20 6b 65  mble an index ke
1838d 79 20 61 6e 64 20 70 75 74 20 69 74 20 6f 6e 20  y and put it on 
1838e 74 68 65 20 74 6f 70 0a 2a 2a 20 6f 66 20 74 68  the top.** of th
1838f 65 20 74 61 63 6b 2e 20 20 54 68 65 20 6b 65 79  e tack.  The key
18390 20 77 69 74 68 20 62 65 20 66 6f 72 20 69 6e 64   with be for ind
18391 65 78 20 70 49 64 78 20 77 68 69 63 68 20 69 73  ex pIdx which is
18392 20 61 6e 20 69 6e 64 65 78 20 6f 6e 20 70 54 61   an index on pTa
18393 62 2e 0a 2a 2a 20 69 43 75 72 20 69 73 20 74 68  b..** iCur is th
18394 65 20 69 6e 64 65 78 20 6f 66 20 61 20 63 75 72  e index of a cur
18395 73 6f 72 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20  sor open on the 
18396 70 54 61 62 20 74 61 62 6c 65 20 61 6e 64 20 70  pTab table and p
18397 6f 69 6e 74 69 6e 67 20 74 6f 0a 2a 2a 20 74 68  ointing to.** th
18398 65 20 65 6e 74 72 79 20 74 68 61 74 20 6e 65 65  e entry that nee
18399 64 73 20 69 6e 64 65 78 69 6e 67 2e 0a 2a 2f 0a  ds indexing..*/.
1839a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1839b 6f 69 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72  oid sqlite3Gener
1839c 61 74 65 49 6e 64 65 78 4b 65 79 28 0a 20 20 56  ateIndexKey(.  V
1839d 64 62 65 20 2a 76 2c 20 20 20 20 20 20 20 20 20  dbe *v,         
1839e 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f    /* Generate co
1839f 64 65 20 69 6e 74 6f 20 74 68 69 73 20 56 44 42  de into this VDB
183a0 45 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49  E */.  Index *pI
183a1 64 78 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65  dx,       /* The
183a2 20 69 6e 64 65 78 20 66 6f 72 20 77 68 69 63 68   index for which
183a3 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20 6b   to generate a k
183a4 65 79 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72  ey */.  int iCur
183a5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75             /* Cu
183a6 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20  rsor number for 
183a7 74 68 65 20 70 49 64 78 2d 3e 70 54 61 62 6c 65  the pIdx->pTable
183a8 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69   table */.){.  i
183a9 6e 74 20 6a 3b 0a 20 20 54 61 62 6c 65 20 2a 70  nt j;.  Table *p
183aa 54 61 62 20 3d 20 70 49 64 78 2d 3e 70 54 61 62  Tab = pIdx->pTab
183ab 6c 65 3b 0a 0a 20 20 73 71 6c 69 74 65 33 56 64  le;..  sqlite3Vd
183ac 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 52 6f  beAddOp(v, OP_Ro
183ad 77 69 64 2c 20 69 43 75 72 2c 20 30 29 3b 0a 20  wid, iCur, 0);. 
183ae 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 49 64 78   for(j=0; j<pIdx
183af 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b  ->nColumn; j++){
183b0 0a 20 20 20 20 69 6e 74 20 69 64 78 20 3d 20 70  .    int idx = p
183b1 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d  Idx->aiColumn[j]
183b2 3b 0a 20 20 20 20 69 66 28 20 69 64 78 3d 3d 70  ;.    if( idx==p
183b3 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20  Tab->iPKey ){.  
183b4 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
183b5 64 64 4f 70 28 76 2c 20 4f 50 5f 44 75 70 2c 20  ddOp(v, OP_Dup, 
183b6 6a 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65  j, 0);.    }else
183b7 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
183b8 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43  dbeAddOp(v, OP_C
183b9 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 69 64 78  olumn, iCur, idx
183ba 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
183bb 43 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 76 2c  ColumnDefault(v,
183bc 20 70 54 61 62 2c 20 69 64 78 29 3b 0a 20 20 20   pTab, idx);.   
183bd 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33   }.  }.  sqlite3
183be 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
183bf 4d 61 6b 65 49 64 78 52 65 63 2c 20 70 49 64 78  MakeIdxRec, pIdx
183c0 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 30 29 3b 0a 20  ->nColumn, 0);. 
183c1 20 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66   sqlite3IndexAff
183c2 69 6e 69 74 79 53 74 72 28 76 2c 20 70 49 64 78  inityStr(v, pIdx
183c3 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  );.}../*********
183c4 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 64 65 6c  ***** End of del
183c5 65 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ete.c **********
183c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
183c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
183c8 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
183c9 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
183ca 20 66 75 6e 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   func.c ********
183cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
183cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
183cd 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 32  ****/./*.** 2002
183ce 20 46 65 62 72 75 61 72 79 20 32 33 0a 2a 2a 0a   February 23.**.
183cf 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
183d0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
183d1 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
183d2 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
183d3 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
183d4 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
183d5 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
183d6 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
183d7 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
183d8 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
183d9 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
183da 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
183db 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
183dc 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
183dd 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
183de 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
183df 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
183e0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
183e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
183e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
183e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
183e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
183e5 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
183e6 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e  s the C function
183e7 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  s that implement
183e8 20 76 61 72 69 6f 75 73 20 53 51 4c 0a 2a 2a 20   various SQL.** 
183e9 66 75 6e 63 74 69 6f 6e 73 20 6f 66 20 53 51 4c  functions of SQL
183ea 69 74 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65  ite.  .**.** The
183eb 72 65 20 69 73 20 6f 6e 6c 79 20 6f 6e 65 20 65  re is only one e
183ec 78 70 6f 72 74 65 64 20 73 79 6d 62 6f 6c 20 69  xported symbol i
183ed 6e 20 74 68 69 73 20 66 69 6c 65 20 2d 20 74 68  n this file - th
183ee 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73 71  e function.** sq
183ef 6c 69 74 65 52 65 67 69 73 74 65 72 42 75 69 6c  liteRegisterBuil
183f0 64 69 6e 46 75 6e 63 74 69 6f 6e 73 28 29 20 66  dinFunctions() f
183f1 6f 75 6e 64 20 61 74 20 74 68 65 20 62 6f 74 74  ound at the bott
183f2 6f 6d 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a  om of the file..
183f3 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20 63 6f 64  ** All other cod
183f4 65 20 68 61 73 20 66 69 6c 65 20 73 63 6f 70 65  e has file scope
183f5 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 66 75 6e  ..**.** $Id: fun
183f6 63 2e 63 2c 76 20 31 2e 31 36 32 20 32 30 30 37  c.c,v 1.162 2007
183f7 2f 30 37 2f 32 33 20 31 39 3a 31 32 3a 34 32 20  /07/23 19:12:42 
183f8 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 2f 2a 20  drh Exp $.*/./* 
183f9 23 69 6e 63 6c 75 64 65 20 3c 6d 61 74 68 2e 68  #include <math.h
183fa 3e 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  > */.../*.** Ret
183fb 75 72 6e 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e  urn the collatin
183fc 67 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 6f 63  g function assoc
183fd 69 61 74 65 64 20 77 69 74 68 20 61 20 66 75 6e  iated with a fun
183fe 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ction..*/.static
183ff 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65   CollSeq *sqlite
18400 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28  3GetFuncCollSeq(
18401 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
18402 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 72 65 74  *context){.  ret
18403 75 72 6e 20 63 6f 6e 74 65 78 74 2d 3e 70 43 6f  urn context->pCo
18404 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70  ll;.}../*.** Imp
18405 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
18406 68 65 20 6e 6f 6e 2d 61 67 67 72 65 67 61 74 65  he non-aggregate
18407 20 6d 69 6e 28 29 20 61 6e 64 20 6d 61 78 28 29   min() and max()
18408 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2f 0a 73 74   functions.*/.st
18409 61 74 69 63 20 76 6f 69 64 20 6d 69 6e 6d 61 78  atic void minmax
1840a 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
1840b 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
1840c 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
1840d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
1840e 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 69 3b  argv.){.  int i;
1840f 0a 20 20 69 6e 74 20 6d 61 73 6b 3b 20 20 20 20  .  int mask;    
18410 2f 2a 20 30 20 66 6f 72 20 6d 69 6e 28 29 20 6f  /* 0 for min() o
18411 72 20 30 78 66 66 66 66 66 66 66 66 20 66 6f 72  r 0xffffffff for
18412 20 6d 61 78 28 29 20 2a 2f 0a 20 20 69 6e 74 20   max() */.  int 
18413 69 42 65 73 74 3b 0a 20 20 43 6f 6c 6c 53 65 71  iBest;.  CollSeq
18414 20 2a 70 43 6f 6c 6c 3b 0a 0a 20 20 69 66 28 20   *pColl;..  if( 
18415 61 72 67 63 3d 3d 30 20 29 20 72 65 74 75 72 6e  argc==0 ) return
18416 3b 0a 20 20 6d 61 73 6b 20 3d 20 73 71 6c 69 74  ;.  mask = sqlit
18417 65 33 5f 75 73 65 72 5f 64 61 74 61 28 63 6f 6e  e3_user_data(con
18418 74 65 78 74 29 3d 3d 30 20 3f 20 30 20 3a 20 2d  text)==0 ? 0 : -
18419 31 3b 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c  1;.  pColl = sql
1841a 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53  ite3GetFuncCollS
1841b 65 71 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 61  eq(context);.  a
1841c 73 73 65 72 74 28 20 70 43 6f 6c 6c 20 29 3b 0a  ssert( pColl );.
1841d 20 20 61 73 73 65 72 74 28 20 6d 61 73 6b 3d 3d    assert( mask==
1841e 2d 31 20 7c 7c 20 6d 61 73 6b 3d 3d 30 20 29 3b  -1 || mask==0 );
1841f 0a 20 20 69 42 65 73 74 20 3d 20 30 3b 0a 20 20  .  iBest = 0;.  
18420 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  if( sqlite3_valu
18421 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3d  e_type(argv[0])=
18422 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72  =SQLITE_NULL ) r
18423 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 31  eturn;.  for(i=1
18424 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0a  ; i<argc; i++){.
18425 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f      if( sqlite3_
18426 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b  value_type(argv[
18427 69 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c  i])==SQLITE_NULL
18428 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 69   ) return;.    i
18429 66 28 20 28 73 71 6c 69 74 65 33 4d 65 6d 43 6f  f( (sqlite3MemCo
1842a 6d 70 61 72 65 28 61 72 67 76 5b 69 42 65 73 74  mpare(argv[iBest
1842b 5d 2c 20 61 72 67 76 5b 69 5d 2c 20 70 43 6f 6c  ], argv[i], pCol
1842c 6c 29 5e 6d 61 73 6b 29 3e 3d 30 20 29 7b 0a 20  l)^mask)>=0 ){. 
1842d 20 20 20 20 20 69 42 65 73 74 20 3d 20 69 3b 0a       iBest = i;.
1842e 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69      }.  }.  sqli
1842f 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65  te3_result_value
18430 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 76 5b 69  (context, argv[i
18431 42 65 73 74 5d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Best]);.}../*.**
18432 20 52 65 74 75 72 6e 20 74 68 65 20 74 79 70 65   Return the type
18433 20 6f 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74   of the argument
18434 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
18435 20 74 79 70 65 6f 66 46 75 6e 63 28 0a 20 20 73   typeofFunc(.  s
18436 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
18437 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
18438 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
18439 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
1843a 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d   const char *z =
1843b 20 30 3b 0a 20 20 73 77 69 74 63 68 28 20 73 71   0;.  switch( sq
1843c 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
1843d 28 61 72 67 76 5b 30 5d 29 20 29 7b 0a 20 20 20  (argv[0]) ){.   
1843e 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4e 55 4c   case SQLITE_NUL
1843f 4c 3a 20 20 20 20 7a 20 3d 20 22 6e 75 6c 6c 22  L:    z = "null"
18440 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20  ;    break;.    
18441 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 45  case SQLITE_INTE
18442 47 45 52 3a 20 7a 20 3d 20 22 69 6e 74 65 67 65  GER: z = "intege
18443 72 22 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 63  r"; break;.    c
18444 61 73 65 20 53 51 4c 49 54 45 5f 54 45 58 54 3a  ase SQLITE_TEXT:
18445 20 20 20 20 7a 20 3d 20 22 74 65 78 74 22 3b 20      z = "text"; 
18446 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
18447 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a  se SQLITE_FLOAT:
18448 20 20 20 7a 20 3d 20 22 72 65 61 6c 22 3b 20 20     z = "real";  
18449 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
1844a 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a 20 20  e SQLITE_BLOB:  
1844b 20 20 7a 20 3d 20 22 62 6c 6f 62 22 3b 20 20 20    z = "blob";   
1844c 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 73 71   break;.  }.  sq
1844d 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
1844e 74 28 63 6f 6e 74 65 78 74 2c 20 7a 2c 20 2d 31  t(context, z, -1
1844f 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
18450 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c  ;.}.../*.** Impl
18451 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
18452 65 20 6c 65 6e 67 74 68 28 29 20 66 75 6e 63 74  e length() funct
18453 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ion.*/.static vo
18454 69 64 20 6c 65 6e 67 74 68 46 75 6e 63 28 0a 20  id lengthFunc(. 
18455 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
18456 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
18457 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33   argc,.  sqlite3
18458 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
18459 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 0a 20 20 61  .  int len;..  a
1845a 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29  ssert( argc==1 )
1845b 3b 0a 20 20 73 77 69 74 63 68 28 20 73 71 6c 69  ;.  switch( sqli
1845c 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
1845d 72 67 76 5b 30 5d 29 20 29 7b 0a 20 20 20 20 63  rgv[0]) ){.    c
1845e 61 73 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a  ase SQLITE_BLOB:
1845f 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
18460 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 20 63 61  _INTEGER:.    ca
18461 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a  se SQLITE_FLOAT:
18462 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   {.      sqlite3
18463 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74  _result_int(cont
18464 65 78 74 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  ext, sqlite3_val
18465 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d  ue_bytes(argv[0]
18466 29 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ));.      break;
18467 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
18468 53 51 4c 49 54 45 5f 54 45 58 54 3a 20 7b 0a 20  SQLITE_TEXT: {. 
18469 20 20 20 20 20 63 6f 6e 73 74 20 75 6e 73 69 67       const unsig
1846a 6e 65 64 20 63 68 61 72 20 2a 7a 20 3d 20 73 71  ned char *z = sq
1846b 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
1846c 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20  (argv[0]);.     
1846d 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75   if( z==0 ) retu
1846e 72 6e 3b 0a 20 20 20 20 20 20 6c 65 6e 20 3d 20  rn;.      len = 
1846f 30 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  0;.      while( 
18470 2a 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 6c 65  *z ){.        le
18471 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 53 51 4c  n++;.        SQL
18472 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 29  ITE_SKIP_UTF8(z)
18473 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
18474 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
18475 6e 74 28 63 6f 6e 74 65 78 74 2c 20 6c 65 6e 29  nt(context, len)
18476 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
18477 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74     }.    default
18478 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  : {.      sqlite
18479 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 63 6f  3_result_null(co
1847a 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 62 72  ntext);.      br
1847b 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  eak;.    }.  }.}
1847c 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
1847d 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 61 62  tation of the ab
1847e 73 28 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2f 0a  s() function.*/.
1847f 73 74 61 74 69 63 20 76 6f 69 64 20 61 62 73 46  static void absF
18480 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  unc(sqlite3_cont
18481 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e  ext *context, in
18482 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
18483 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20  value **argv){. 
18484 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31   assert( argc==1
18485 20 29 3b 0a 20 20 73 77 69 74 63 68 28 20 73 71   );.  switch( sq
18486 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
18487 28 61 72 67 76 5b 30 5d 29 20 29 7b 0a 20 20 20  (argv[0]) ){.   
18488 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 54   case SQLITE_INT
18489 45 47 45 52 3a 20 7b 0a 20 20 20 20 20 20 69 36  EGER: {.      i6
1848a 34 20 69 56 61 6c 20 3d 20 73 71 6c 69 74 65 33  4 iVal = sqlite3
1848b 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 72 67  _value_int64(arg
1848c 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 69 66 28  v[0]);.      if(
1848d 20 69 56 61 6c 3c 30 20 29 7b 0a 20 20 20 20 20   iVal<0 ){.     
1848e 20 20 20 69 66 28 20 28 69 56 61 6c 3c 3c 31 29     if( (iVal<<1)
1848f 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
18490 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
18491 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22  error(context, "
18492 69 6e 74 65 67 65 72 20 6f 76 65 72 66 6c 6f 77  integer overflow
18493 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20  ", -1);.        
18494 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
18495 20 20 7d 0a 20 20 20 20 20 20 20 20 69 56 61 6c    }.        iVal
18496 20 3d 20 2d 69 56 61 6c 3b 0a 20 20 20 20 20 20   = -iVal;.      
18497 7d 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  } .      sqlite3
18498 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f  _result_int64(co
18499 6e 74 65 78 74 2c 20 69 56 61 6c 29 3b 0a 20 20  ntext, iVal);.  
1849a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1849b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
1849c 5f 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 73  _NULL: {.      s
1849d 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
1849e 6c 6c 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  ll(context);.   
1849f 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
184a0 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
184a1 20 20 20 20 20 64 6f 75 62 6c 65 20 72 56 61 6c       double rVal
184a2 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
184a3 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d 29  _double(argv[0])
184a4 3b 0a 20 20 20 20 20 20 69 66 28 20 72 56 61 6c  ;.      if( rVal
184a5 3c 30 20 29 20 72 56 61 6c 20 3d 20 2d 72 56 61  <0 ) rVal = -rVa
184a6 6c 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  l;.      sqlite3
184a7 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63  _result_double(c
184a8 6f 6e 74 65 78 74 2c 20 72 56 61 6c 29 3b 0a 20  ontext, rVal);. 
184a9 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
184aa 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  }.  }.}../*.** I
184ab 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
184ac 20 74 68 65 20 73 75 62 73 74 72 28 29 20 66 75   the substr() fu
184ad 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 73 75  nction..**.** su
184ae 62 73 74 72 28 78 2c 70 31 2c 70 32 29 20 20 72  bstr(x,p1,p2)  r
184af 65 74 75 72 6e 73 20 70 32 20 63 68 61 72 61 63  eturns p2 charac
184b0 74 65 72 73 20 6f 66 20 78 5b 5d 20 62 65 67 69  ters of x[] begi
184b1 6e 6e 69 6e 67 20 77 69 74 68 20 70 31 2e 0a 2a  nning with p1..*
184b2 2a 20 70 31 20 69 73 20 31 2d 69 6e 64 65 78 65  * p1 is 1-indexe
184b3 64 2e 20 20 53 6f 20 73 75 62 73 74 72 28 78 2c  d.  So substr(x,
184b4 31 2c 31 29 20 72 65 74 75 72 6e 73 20 74 68 65  1,1) returns the
184b5 20 66 69 72 73 74 20 63 68 61 72 61 63 74 65 72   first character
184b6 0a 2a 2a 20 6f 66 20 78 2e 20 20 49 66 20 78 20  .** of x.  If x 
184b7 69 73 20 74 65 78 74 2c 20 74 68 65 6e 20 77 65  is text, then we
184b8 20 61 63 74 75 61 6c 6c 79 20 63 6f 75 6e 74 20   actually count 
184b9 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 73  UTF-8 characters
184ba 2e 0a 2a 2a 20 49 66 20 78 20 69 73 20 61 20 62  ..** If x is a b
184bb 6c 6f 62 2c 20 74 68 65 6e 20 77 65 20 63 6f 75  lob, then we cou
184bc 6e 74 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20  nt bytes..**.** 
184bd 49 66 20 70 31 20 69 73 20 6e 65 67 61 74 69 76  If p1 is negativ
184be 65 2c 20 74 68 65 6e 20 77 65 20 62 65 67 69 6e  e, then we begin
184bf 20 61 62 73 28 70 31 29 20 66 72 6f 6d 20 74 68   abs(p1) from th
184c0 65 20 65 6e 64 20 6f 66 20 78 5b 5d 2e 0a 2a 2f  e end of x[]..*/
184c1 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 75 62  .static void sub
184c2 73 74 72 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  strFunc(.  sqlit
184c3 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
184c4 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c  ext,.  int argc,
184c5 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
184c6 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e   **argv.){.  con
184c7 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
184c8 20 2a 7a 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73   *z;.  const uns
184c9 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 32 3b 0a  igned char *z2;.
184ca 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 69 6e 74    int len;.  int
184cb 20 70 30 74 79 70 65 3b 0a 20 20 69 36 34 20 70   p0type;.  i64 p
184cc 31 2c 20 70 32 3b 0a 0a 20 20 61 73 73 65 72 74  1, p2;..  assert
184cd 28 20 61 72 67 63 3d 3d 33 20 29 3b 0a 20 20 70  ( argc==3 );.  p
184ce 30 74 79 70 65 20 3d 20 73 71 6c 69 74 65 33 5f  0type = sqlite3_
184cf 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b  value_type(argv[
184d0 30 5d 29 3b 0a 20 20 69 66 28 20 70 30 74 79 70  0]);.  if( p0typ
184d1 65 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29  e==SQLITE_BLOB )
184d2 7b 0a 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c 69  {.    len = sqli
184d3 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
184d4 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 7a 20  argv[0]);.    z 
184d5 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
184d6 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 3b 0a 20  blob(argv[0]);. 
184d7 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65     if( z==0 ) re
184d8 74 75 72 6e 3b 0a 20 20 20 20 61 73 73 65 72 74  turn;.    assert
184d9 28 20 6c 65 6e 3d 3d 73 71 6c 69 74 65 33 5f 76  ( len==sqlite3_v
184da 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b  alue_bytes(argv[
184db 30 5d 29 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  0]) );.  }else{.
184dc 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f      z = sqlite3_
184dd 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
184de 30 5d 29 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d  0]);.    if( z==
184df 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  0 ) return;.    
184e0 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72  len = 0;.    for
184e1 28 7a 32 3d 7a 3b 20 2a 7a 32 3b 20 6c 65 6e 2b  (z2=z; *z2; len+
184e2 2b 29 7b 0a 20 20 20 20 20 20 53 51 4c 49 54 45  +){.      SQLITE
184e3 5f 53 4b 49 50 5f 55 54 46 38 28 7a 32 29 3b 0a  _SKIP_UTF8(z2);.
184e4 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 31 20 3d      }.  }.  p1 =
184e5 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
184e6 6e 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 70  nt(argv[1]);.  p
184e7 32 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  2 = sqlite3_valu
184e8 65 5f 69 6e 74 28 61 72 67 76 5b 32 5d 29 3b 0a  e_int(argv[2]);.
184e9 20 20 69 66 28 20 70 31 3c 30 20 29 7b 0a 20 20    if( p1<0 ){.  
184ea 20 20 70 31 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20    p1 += len;.   
184eb 20 69 66 28 20 70 31 3c 30 20 29 7b 0a 20 20 20   if( p1<0 ){.   
184ec 20 20 20 70 32 20 2b 3d 20 70 31 3b 0a 20 20 20     p2 += p1;.   
184ed 20 20 20 70 31 20 3d 20 30 3b 0a 20 20 20 20 7d     p1 = 0;.    }
184ee 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 31 3e  .  }else if( p1>
184ef 30 20 29 7b 0a 20 20 20 20 70 31 2d 2d 3b 0a 20  0 ){.    p1--;. 
184f0 20 7d 0a 20 20 69 66 28 20 70 31 2b 70 32 3e 6c   }.  if( p1+p2>l
184f1 65 6e 20 29 7b 0a 20 20 20 20 70 32 20 3d 20 6c  en ){.    p2 = l
184f2 65 6e 2d 70 31 3b 0a 20 20 7d 0a 20 20 69 66 28  en-p1;.  }.  if(
184f3 20 70 30 74 79 70 65 21 3d 53 51 4c 49 54 45 5f   p0type!=SQLITE_
184f4 42 4c 4f 42 20 29 7b 0a 20 20 20 20 77 68 69 6c  BLOB ){.    whil
184f5 65 28 20 2a 7a 20 26 26 20 70 31 20 29 7b 0a 20  e( *z && p1 ){. 
184f6 20 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50       SQLITE_SKIP
184f7 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 20 20  _UTF8(z);.      
184f8 70 31 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  p1--;.    }.    
184f9 66 6f 72 28 7a 32 3d 7a 3b 20 2a 7a 32 20 26 26  for(z2=z; *z2 &&
184fa 20 70 32 3b 20 70 32 2d 2d 29 7b 0a 20 20 20 20   p2; p2--){.    
184fb 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54    SQLITE_SKIP_UT
184fc 46 38 28 7a 32 29 3b 0a 20 20 20 20 7d 0a 20 20  F8(z2);.    }.  
184fd 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
184fe 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 28  _text(context, (
184ff 63 68 61 72 2a 29 7a 2c 20 7a 32 2d 7a 2c 20 53  char*)z, z2-z, S
18500 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
18501 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  ;.  }else{.    i
18502 66 28 20 70 32 3c 30 20 29 20 70 32 20 3d 20 30  f( p2<0 ) p2 = 0
18503 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ;.    sqlite3_re
18504 73 75 6c 74 5f 62 6c 6f 62 28 63 6f 6e 74 65 78  sult_blob(contex
18505 74 2c 20 28 63 68 61 72 2a 29 26 7a 5b 70 31 5d  t, (char*)&z[p1]
18506 2c 20 70 32 2c 20 53 51 4c 49 54 45 5f 54 52 41  , p2, SQLITE_TRA
18507 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a  NSIENT);.  }.}..
18508 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
18509 74 69 6f 6e 20 6f 66 20 74 68 65 20 72 6f 75 6e  tion of the roun
1850a 64 28 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2f 0a  d() function.*/.
1850b 73 74 61 74 69 63 20 76 6f 69 64 20 72 6f 75 6e  static void roun
1850c 64 46 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f  dFunc(sqlite3_co
1850d 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20  ntext *context, 
1850e 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
1850f 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b  3_value **argv){
18510 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20  .  int n = 0;.  
18511 64 6f 75 62 6c 65 20 72 3b 0a 20 20 63 68 61 72  double r;.  char
18512 20 7a 42 75 66 5b 35 30 30 5d 3b 20 20 2f 2a 20   zBuf[500];  /* 
18513 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20  larger than the 
18514 25 66 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  %f representatio
18515 6e 20 6f 66 20 74 68 65 20 6c 61 72 67 65 73 74  n of the largest
18516 20 64 6f 75 62 6c 65 20 2a 2f 0a 20 20 61 73 73   double */.  ass
18517 65 72 74 28 20 61 72 67 63 3d 3d 31 20 7c 7c 20  ert( argc==1 || 
18518 61 72 67 63 3d 3d 32 20 29 3b 0a 20 20 69 66 28  argc==2 );.  if(
18519 20 61 72 67 63 3d 3d 32 20 29 7b 0a 20 20 20 20   argc==2 ){.    
1851a 69 66 28 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3d  if( SQLITE_NULL=
1851b 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  =sqlite3_value_t
1851c 79 70 65 28 61 72 67 76 5b 31 5d 29 20 29 20 72  ype(argv[1]) ) r
1851d 65 74 75 72 6e 3b 0a 20 20 20 20 6e 20 3d 20 73  eturn;.    n = s
1851e 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
1851f 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 69  (argv[1]);.    i
18520 66 28 20 6e 3e 33 30 20 29 20 6e 20 3d 20 33 30  f( n>30 ) n = 30
18521 3b 0a 20 20 20 20 69 66 28 20 6e 3c 30 20 29 20  ;.    if( n<0 ) 
18522 6e 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28  n = 0;.  }.  if(
18523 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
18524 79 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51  ype(argv[0])==SQ
18525 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75  LITE_NULL ) retu
18526 72 6e 3b 0a 20 20 72 20 3d 20 73 71 6c 69 74 65  rn;.  r = sqlite
18527 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61  3_value_double(a
18528 72 67 76 5b 30 5d 29 3b 0a 20 20 73 71 6c 69 74  rgv[0]);.  sqlit
18529 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65  e3_snprintf(size
1852a 6f 66 28 7a 42 75 66 29 2c 7a 42 75 66 2c 22 25  of(zBuf),zBuf,"%
1852b 2e 2a 66 22 2c 6e 2c 72 29 3b 0a 20 20 73 71 6c  .*f",n,r);.  sql
1852c 69 74 65 33 41 74 6f 46 28 7a 42 75 66 2c 20 26  ite3AtoF(zBuf, &
1852d 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  r);.  sqlite3_re
1852e 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74  sult_double(cont
1852f 65 78 74 2c 20 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  ext, r);.}../*.*
18530 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
18531 20 6f 66 20 74 68 65 20 75 70 70 65 72 28 29 20   of the upper() 
18532 61 6e 64 20 6c 6f 77 65 72 28 29 20 53 51 4c 20  and lower() SQL 
18533 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74  functions..*/.st
18534 61 74 69 63 20 76 6f 69 64 20 75 70 70 65 72 46  atic void upperF
18535 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  unc(sqlite3_cont
18536 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e  ext *context, in
18537 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
18538 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20  value **argv){. 
18539 20 63 68 61 72 20 2a 7a 31 3b 0a 20 20 63 6f 6e   char *z1;.  con
1853a 73 74 20 63 68 61 72 20 2a 7a 32 3b 0a 20 20 69  st char *z2;.  i
1853b 6e 74 20 69 2c 20 6e 3b 0a 20 20 69 66 28 20 61  nt i, n;.  if( a
1853c 72 67 63 3c 31 20 7c 7c 20 53 51 4c 49 54 45 5f  rgc<1 || SQLITE_
1853d 4e 55 4c 4c 3d 3d 73 71 6c 69 74 65 33 5f 76 61  NULL==sqlite3_va
1853e 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d  lue_type(argv[0]
1853f 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7a 32  ) ) return;.  z2
18540 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65   = (char*)sqlite
18541 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
18542 76 5b 30 5d 29 3b 0a 20 20 6e 20 3d 20 73 71 6c  v[0]);.  n = sql
18543 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
18544 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 2f 2a 20  (argv[0]);.  /* 
18545 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20  Verify that the 
18546 63 61 6c 6c 20 74 6f 20 5f 62 79 74 65 73 28 29  call to _bytes()
18547 20 64 6f 65 73 20 6e 6f 74 20 69 6e 76 61 6c 69   does not invali
18548 64 61 74 65 20 74 68 65 20 5f 74 65 78 74 28 29  date the _text()
18549 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 61 73   pointer */.  as
1854a 73 65 72 74 28 20 7a 32 3d 3d 28 63 68 61 72 2a  sert( z2==(char*
1854b 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  )sqlite3_value_t
1854c 65 78 74 28 61 72 67 76 5b 30 5d 29 20 29 3b 0a  ext(argv[0]) );.
1854d 20 20 69 66 28 20 7a 32 20 29 7b 0a 20 20 20 20    if( z2 ){.    
1854e 7a 31 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  z1 = sqlite3_mal
1854f 6c 6f 63 28 6e 2b 31 29 3b 0a 20 20 20 20 69 66  loc(n+1);.    if
18550 28 20 7a 31 20 29 7b 0a 20 20 20 20 20 20 6d 65  ( z1 ){.      me
18551 6d 63 70 79 28 7a 31 2c 20 7a 32 2c 20 6e 2b 31  mcpy(z1, z2, n+1
18552 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  );.      for(i=0
18553 3b 20 7a 31 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20  ; z1[i]; i++){. 
18554 20 20 20 20 20 20 20 7a 31 5b 69 5d 20 3d 20 74         z1[i] = t
18555 6f 75 70 70 65 72 28 7a 31 5b 69 5d 29 3b 0a 20  oupper(z1[i]);. 
18556 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
18557 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
18558 28 63 6f 6e 74 65 78 74 2c 20 7a 31 2c 20 2d 31  (context, z1, -1
18559 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b  , sqlite3_free);
1855a 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61  .    }.  }.}.sta
1855b 74 69 63 20 76 6f 69 64 20 6c 6f 77 65 72 46 75  tic void lowerFu
1855c 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  nc(sqlite3_conte
1855d 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74  xt *context, int
1855e 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
1855f 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20  alue **argv){.  
18560 63 68 61 72 20 2a 7a 31 3b 0a 20 20 63 6f 6e 73  char *z1;.  cons
18561 74 20 63 68 61 72 20 2a 7a 32 3b 0a 20 20 69 6e  t char *z2;.  in
18562 74 20 69 2c 20 6e 3b 0a 20 20 69 66 28 20 61 72  t i, n;.  if( ar
18563 67 63 3c 31 20 7c 7c 20 53 51 4c 49 54 45 5f 4e  gc<1 || SQLITE_N
18564 55 4c 4c 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c  ULL==sqlite3_val
18565 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29  ue_type(argv[0])
18566 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7a 32 20   ) return;.  z2 
18567 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33  = (char*)sqlite3
18568 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
18569 5b 30 5d 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69  [0]);.  n = sqli
1856a 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
1856b 61 72 67 76 5b 30 5d 29 3b 0a 20 20 2f 2a 20 56  argv[0]);.  /* V
1856c 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 63  erify that the c
1856d 61 6c 6c 20 74 6f 20 5f 62 79 74 65 73 28 29 20  all to _bytes() 
1856e 64 6f 65 73 20 6e 6f 74 20 69 6e 76 61 6c 69 64  does not invalid
1856f 61 74 65 20 74 68 65 20 5f 74 65 78 74 28 29 20  ate the _text() 
18570 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 61 73 73  pointer */.  ass
18571 65 72 74 28 20 7a 32 3d 3d 28 63 68 61 72 2a 29  ert( z2==(char*)
18572 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
18573 78 74 28 61 72 67 76 5b 30 5d 29 20 29 3b 0a 20  xt(argv[0]) );. 
18574 20 69 66 28 20 7a 32 20 29 7b 0a 20 20 20 20 7a   if( z2 ){.    z
18575 31 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  1 = sqlite3_mall
18576 6f 63 28 6e 2b 31 29 3b 0a 20 20 20 20 69 66 28  oc(n+1);.    if(
18577 20 7a 31 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d   z1 ){.      mem
18578 63 70 79 28 7a 31 2c 20 7a 32 2c 20 6e 2b 31 29  cpy(z1, z2, n+1)
18579 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b  ;.      for(i=0;
1857a 20 7a 31 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20   z1[i]; i++){.  
1857b 20 20 20 20 20 20 7a 31 5b 69 5d 20 3d 20 74 6f        z1[i] = to
1857c 6c 6f 77 65 72 28 7a 31 5b 69 5d 29 3b 0a 20 20  lower(z1[i]);.  
1857d 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
1857e 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
1857f 63 6f 6e 74 65 78 74 2c 20 7a 31 2c 20 2d 31 2c  context, z1, -1,
18580 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a   sqlite3_free);.
18581 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
18582 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
18583 6e 20 6f 66 20 74 68 65 20 49 46 4e 55 4c 4c 28  n of the IFNULL(
18584 29 2c 20 4e 56 4c 28 29 2c 20 61 6e 64 20 43 4f  ), NVL(), and CO
18585 41 4c 45 53 43 45 28 29 20 66 75 6e 63 74 69 6f  ALESCE() functio
18586 6e 73 2e 20 20 0a 2a 2a 20 41 6c 6c 20 74 68 72  ns.  .** All thr
18587 65 65 20 64 6f 20 74 68 65 20 73 61 6d 65 20 74  ee do the same t
18588 68 69 6e 67 2e 20 20 54 68 65 79 20 72 65 74 75  hing.  They retu
18589 72 6e 20 74 68 65 20 66 69 72 73 74 20 6e 6f 6e  rn the first non
1858a 2d 4e 55 4c 4c 0a 2a 2a 20 61 72 67 75 6d 65 6e  -NULL.** argumen
1858b 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  t..*/.static voi
1858c 64 20 69 66 6e 75 6c 6c 46 75 6e 63 28 0a 20 20  d ifnullFunc(.  
1858d 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1858e 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
1858f 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f  argc,.  sqlite3_
18590 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
18591 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69    int i;.  for(i
18592 3d 30 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29  =0; i<argc; i++)
18593 7b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45  {.    if( SQLITE
18594 5f 4e 55 4c 4c 21 3d 73 71 6c 69 74 65 33 5f 76  _NULL!=sqlite3_v
18595 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 69  alue_type(argv[i
18596 5d 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ]) ){.      sqli
18597 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65  te3_result_value
18598 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 76 5b 69  (context, argv[i
18599 5d 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ]);.      break;
1859a 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
1859b 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
1859c 6f 6e 20 6f 66 20 72 61 6e 64 6f 6d 28 29 2e 20  on of random(). 
1859d 20 52 65 74 75 72 6e 20 61 20 72 61 6e 64 6f 6d   Return a random
1859e 20 69 6e 74 65 67 65 72 2e 20 20 0a 2a 2f 0a 73   integer.  .*/.s
1859f 74 61 74 69 63 20 76 6f 69 64 20 72 61 6e 64 6f  tatic void rando
185a0 6d 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  mFunc(.  sqlite3
185a1 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
185a2 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20  t,.  int argc,. 
185a3 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
185a4 2a 61 72 67 76 0a 29 7b 0a 20 20 73 71 6c 69 74  *argv.){.  sqlit
185a5 65 5f 69 6e 74 36 34 20 72 3b 0a 20 20 73 71 6c  e_int64 r;.  sql
185a6 69 74 65 33 52 61 6e 64 6f 6d 6e 65 73 73 28 73  ite3Randomness(s
185a7 69 7a 65 6f 66 28 72 29 2c 20 26 72 29 3b 0a 20  izeof(r), &r);. 
185a8 20 69 66 28 20 28 72 3c 3c 31 29 3d 3d 30 20 29   if( (r<<1)==0 )
185a9 20 72 20 3d 20 30 3b 20 20 2f 2a 20 50 72 65 76   r = 0;  /* Prev
185aa 65 6e 74 20 30 78 38 30 30 30 2e 2e 2e 2e 20 61  ent 0x8000.... a
185ab 73 20 74 68 65 20 72 65 73 75 6c 74 20 73 6f 20  s the result so 
185ac 74 68 61 74 20 77 65 20 2a 2f 0a 20 20 20 20 20  that we */.     
185ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
185ae 20 20 20 20 20 2f 2a 20 63 61 6e 20 61 6c 77 61       /* can alwa
185af 79 73 20 64 6f 20 61 62 73 28 29 20 6f 66 20 74  ys do abs() of t
185b0 68 65 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20 73  he result */.  s
185b1 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
185b2 74 36 34 28 63 6f 6e 74 65 78 74 2c 20 72 29 3b  t64(context, r);
185b3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
185b4 65 6e 74 61 74 69 6f 6e 20 6f 66 20 72 61 6e 64  entation of rand
185b5 6f 6d 62 6c 6f 62 28 4e 29 2e 20 20 52 65 74 75  omblob(N).  Retu
185b6 72 6e 20 61 20 72 61 6e 64 6f 6d 20 62 6c 6f 62  rn a random blob
185b7 0a 2a 2a 20 74 68 61 74 20 69 73 20 4e 20 62 79  .** that is N by
185b8 74 65 73 20 6c 6f 6e 67 2e 0a 2a 2f 0a 73 74 61  tes long..*/.sta
185b9 74 69 63 20 76 6f 69 64 20 72 61 6e 64 6f 6d 42  tic void randomB
185ba 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  lob(.  sqlite3_c
185bb 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
185bc 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73  .  int argc,.  s
185bd 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
185be 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a  rgv.){.  int n;.
185bf 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
185c0 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72  *p;.  assert( ar
185c1 67 63 3d 3d 31 20 29 3b 0a 20 20 6e 20 3d 20 73  gc==1 );.  n = s
185c2 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
185c3 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28  (argv[0]);.  if(
185c4 20 6e 3c 31 20 29 7b 0a 20 20 20 20 6e 20 3d 20   n<1 ){.    n = 
185c5 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 3e 53  1;.  }.  if( n>S
185c6 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48  QLITE_MAX_LENGTH
185c7 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
185c8 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f  result_error_too
185c9 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  big(context);.  
185ca 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
185cb 70 20 3d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63  p = sqliteMalloc
185cc 28 6e 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a  (n);.  if( p ){.
185cd 20 20 20 20 73 71 6c 69 74 65 33 52 61 6e 64 6f      sqlite3Rando
185ce 6d 6e 65 73 73 28 6e 2c 20 70 29 3b 0a 20 20 20  mness(n, p);.   
185cf 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
185d0 62 6c 6f 62 28 63 6f 6e 74 65 78 74 2c 20 28 63  blob(context, (c
185d1 68 61 72 2a 29 70 2c 20 6e 2c 20 73 71 6c 69 74  har*)p, n, sqlit
185d2 65 33 46 72 65 65 58 29 3b 0a 20 20 7d 0a 7d 0a  e3FreeX);.  }.}.
185d3 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
185d4 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 61 73  ation of the las
185d5 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29  t_insert_rowid()
185d6 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20   SQL function.  
185d7 54 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61  The return.** va
185d8 6c 75 65 20 69 73 20 74 68 65 20 73 61 6d 65 20  lue is the same 
185d9 61 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6c  as the sqlite3_l
185da 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64  ast_insert_rowid
185db 28 29 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e  () API function.
185dc 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
185dd 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69  last_insert_rowi
185de 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  d(.  sqlite3_con
185df 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a  text *context, .
185e0 20 20 69 6e 74 20 61 72 67 2c 20 0a 20 20 73 71    int arg, .  sq
185e1 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
185e2 67 76 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  gv.){.  sqlite3 
185e3 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 75 73  *db = sqlite3_us
185e4 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29  er_data(context)
185e5 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ;.  sqlite3_resu
185e6 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78 74  lt_int64(context
185e7 2c 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69  , sqlite3_last_i
185e8 6e 73 65 72 74 5f 72 6f 77 69 64 28 64 62 29 29  nsert_rowid(db))
185e9 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  ;.}../*.** Imple
185ea 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
185eb 20 63 68 61 6e 67 65 73 28 29 20 53 51 4c 20 66   changes() SQL f
185ec 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 72 65  unction.  The re
185ed 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68  turn value is th
185ee 65 0a 2a 2a 20 73 61 6d 65 20 61 73 20 74 68 65  e.** same as the
185ef 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73   sqlite3_changes
185f0 28 29 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e  () API function.
185f1 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
185f2 63 68 61 6e 67 65 73 28 0a 20 20 73 71 6c 69 74  changes(.  sqlit
185f3 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
185f4 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 2c 0a  ext,.  int arg,.
185f5 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
185f6 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 73 71 6c 69  **argv.){.  sqli
185f7 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65  te3 *db = sqlite
185f8 33 5f 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74  3_user_data(cont
185f9 65 78 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  ext);.  sqlite3_
185fa 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65  result_int(conte
185fb 78 74 2c 20 73 71 6c 69 74 65 33 5f 63 68 61 6e  xt, sqlite3_chan
185fc 67 65 73 28 64 62 29 29 3b 0a 7d 0a 0a 2f 2a 0a  ges(db));.}../*.
185fd 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
185fe 6e 20 6f 66 20 74 68 65 20 74 6f 74 61 6c 5f 63  n of the total_c
185ff 68 61 6e 67 65 73 28 29 20 53 51 4c 20 66 75 6e  hanges() SQL fun
18600 63 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 74 75  ction.  The retu
18601 72 6e 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 74  rn value is.** t
18602 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 73  he same as the s
18603 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61  qlite3_total_cha
18604 6e 67 65 73 28 29 20 41 50 49 20 66 75 6e 63 74  nges() API funct
18605 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ion..*/.static v
18606 6f 69 64 20 74 6f 74 61 6c 5f 63 68 61 6e 67 65  oid total_change
18607 73 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  s(.  sqlite3_con
18608 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
18609 20 69 6e 74 20 61 72 67 2c 0a 20 20 73 71 6c 69   int arg,.  sqli
1860a 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
1860b 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  .){.  sqlite3 *d
1860c 62 20 3d 20 73 71 6c 69 74 65 33 5f 75 73 65 72  b = sqlite3_user
1860d 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 3b 0a  _data(context);.
1860e 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
1860f 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 73 71  _int(context, sq
18610 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e  lite3_total_chan
18611 67 65 73 28 64 62 29 29 3b 0a 7d 0a 0a 2f 2a 0a  ges(db));.}../*.
18612 2a 2a 20 41 20 73 74 72 75 63 74 75 72 65 20 64  ** A structure d
18613 65 66 69 6e 69 6e 67 20 68 6f 77 20 74 6f 20 64  efining how to d
18614 6f 20 47 4c 4f 42 2d 73 74 79 6c 65 20 63 6f 6d  o GLOB-style com
18615 70 61 72 69 73 6f 6e 73 2e 0a 2a 2f 0a 73 74 72  parisons..*/.str
18616 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20  uct compareInfo 
18617 7b 0a 20 20 75 38 20 6d 61 74 63 68 41 6c 6c 3b  {.  u8 matchAll;
18618 0a 20 20 75 38 20 6d 61 74 63 68 4f 6e 65 3b 0a  .  u8 matchOne;.
18619 20 20 75 38 20 6d 61 74 63 68 53 65 74 3b 0a 20    u8 matchSet;. 
1861a 20 75 38 20 6e 6f 43 61 73 65 3b 0a 7d 3b 0a 0a   u8 noCase;.};..
1861b 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72  static const str
1861c 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20  uct compareInfo 
1861d 67 6c 6f 62 49 6e 66 6f 20 3d 20 7b 20 27 2a 27  globInfo = { '*'
1861e 2c 20 27 3f 27 2c 20 27 5b 27 2c 20 30 20 7d 3b  , '?', '[', 0 };
1861f 0a 2f 2a 20 54 68 65 20 63 6f 72 72 65 63 74 20  ./* The correct 
18620 53 51 4c 2d 39 32 20 62 65 68 61 76 69 6f 72 20  SQL-92 behavior 
18621 69 73 20 66 6f 72 20 74 68 65 20 4c 49 4b 45 20  is for the LIKE 
18622 6f 70 65 72 61 74 6f 72 20 74 6f 20 69 67 6e 6f  operator to igno
18623 72 65 0a 2a 2a 20 63 61 73 65 2e 20 20 54 68 75  re.** case.  Thu
18624 73 20 20 27 61 27 20 4c 49 4b 45 20 27 41 27 20  s  'a' LIKE 'A' 
18625 77 6f 75 6c 64 20 62 65 20 74 72 75 65 2e 20 2a  would be true. *
18626 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73  /.static const s
18627 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66  truct compareInf
18628 6f 20 6c 69 6b 65 49 6e 66 6f 4e 6f 72 6d 20 3d  o likeInfoNorm =
18629 20 7b 20 27 25 27 2c 20 27 5f 27 2c 20 20 20 30   { '%', '_',   0
1862a 2c 20 31 20 7d 3b 0a 2f 2a 20 49 66 20 53 51 4c  , 1 };./* If SQL
1862b 49 54 45 5f 43 41 53 45 5f 53 45 4e 53 49 54 49  ITE_CASE_SENSITI
1862c 56 45 5f 4c 49 4b 45 20 69 73 20 64 65 66 69 6e  VE_LIKE is defin
1862d 65 64 2c 20 74 68 65 6e 20 74 68 65 20 4c 49 4b  ed, then the LIK
1862e 45 20 6f 70 65 72 61 74 6f 72 0a 2a 2a 20 69 73  E operator.** is
1862f 20 63 61 73 65 20 73 65 6e 73 69 74 69 76 65 20   case sensitive 
18630 63 61 75 73 69 6e 67 20 27 61 27 20 4c 49 4b 45  causing 'a' LIKE
18631 20 27 41 27 20 74 6f 20 62 65 20 66 61 6c 73 65   'A' to be false
18632 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74   */.static const
18633 20 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49   struct compareI
18634 6e 66 6f 20 6c 69 6b 65 49 6e 66 6f 41 6c 74 20  nfo likeInfoAlt 
18635 3d 20 7b 20 27 25 27 2c 20 27 5f 27 2c 20 20 20  = { '%', '_',   
18636 30 2c 20 30 20 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43  0, 0 };../*.** C
18637 6f 6d 70 61 72 65 20 74 77 6f 20 55 54 46 2d 38  ompare two UTF-8
18638 20 73 74 72 69 6e 67 73 20 66 6f 72 20 65 71 75   strings for equ
18639 61 6c 69 74 79 20 77 68 65 72 65 20 74 68 65 20  ality where the 
1863a 66 69 72 73 74 20 73 74 72 69 6e 67 20 63 61 6e  first string can
1863b 0a 2a 2a 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20  .** potentially 
1863c 62 65 20 61 20 22 67 6c 6f 62 22 20 65 78 70 72  be a "glob" expr
1863d 65 73 73 69 6f 6e 2e 20 20 52 65 74 75 72 6e 20  ession.  Return 
1863e 74 72 75 65 20 28 31 29 20 69 66 20 74 68 65 79  true (1) if they
1863f 0a 2a 2a 20 61 72 65 20 74 68 65 20 73 61 6d 65  .** are the same
18640 20 61 6e 64 20 66 61 6c 73 65 20 28 30 29 20 69   and false (0) i
18641 66 20 74 68 65 79 20 61 72 65 20 64 69 66 66 65  f they are diffe
18642 72 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 47 6c 6f 62  rent..**.** Glob
18643 62 69 6e 67 20 72 75 6c 65 73 3a 0a 2a 2a 0a 2a  bing rules:.**.*
18644 2a 20 20 20 20 20 20 27 2a 27 20 20 20 20 20 20  *      '*'      
18645 20 4d 61 74 63 68 65 73 20 61 6e 79 20 73 65 71   Matches any seq
18646 75 65 6e 63 65 20 6f 66 20 7a 65 72 6f 20 6f 72  uence of zero or
18647 20 6d 6f 72 65 20 63 68 61 72 61 63 74 65 72 73   more characters
18648 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 27 3f 27  ..**.**      '?'
18649 20 20 20 20 20 20 20 4d 61 74 63 68 65 73 20 65         Matches e
1864a 78 61 63 74 6c 79 20 6f 6e 65 20 63 68 61 72 61  xactly one chara
1864b 63 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  cter..**.**     
1864c 5b 2e 2e 2e 5d 20 20 20 20 20 20 4d 61 74 63 68  [...]      Match
1864d 65 73 20 6f 6e 65 20 63 68 61 72 61 63 74 65 72  es one character
1864e 20 66 72 6f 6d 20 74 68 65 20 65 6e 63 6c 6f 73   from the enclos
1864f 65 64 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 20 20  ed list of.**   
18650 20 20 20 20 20 20 20 20 20 20 20 20 20 63 68 61               cha
18651 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 20  racters..**.**  
18652 20 20 20 5b 5e 2e 2e 2e 5d 20 20 20 20 20 4d 61     [^...]     Ma
18653 74 63 68 65 73 20 6f 6e 65 20 63 68 61 72 61 63  tches one charac
18654 74 65 72 20 6e 6f 74 20 69 6e 20 74 68 65 20 65  ter not in the e
18655 6e 63 6c 6f 73 65 64 20 6c 69 73 74 2e 0a 2a 2a  nclosed list..**
18656 0a 2a 2a 20 57 69 74 68 20 74 68 65 20 5b 2e 2e  .** With the [..
18657 2e 5d 20 61 6e 64 20 5b 5e 2e 2e 2e 5d 20 6d 61  .] and [^...] ma
18658 74 63 68 69 6e 67 2c 20 61 20 27 5d 27 20 63 68  tching, a ']' ch
18659 61 72 61 63 74 65 72 20 63 61 6e 20 62 65 20 69  aracter can be i
1865a 6e 63 6c 75 64 65 64 0a 2a 2a 20 69 6e 20 74 68  ncluded.** in th
1865b 65 20 6c 69 73 74 20 62 79 20 6d 61 6b 69 6e 67  e list by making
1865c 20 69 74 20 74 68 65 20 66 69 72 73 74 20 63 68   it the first ch
1865d 61 72 61 63 74 65 72 20 61 66 74 65 72 20 27 5b  aracter after '[
1865e 27 20 6f 72 20 27 5e 27 2e 20 20 41 0a 2a 2a 20  ' or '^'.  A.** 
1865f 72 61 6e 67 65 20 6f 66 20 63 68 61 72 61 63 74  range of charact
18660 65 72 73 20 63 61 6e 20 62 65 20 73 70 65 63 69  ers can be speci
18661 66 69 65 64 20 75 73 69 6e 67 20 27 2d 27 2e 20  fied using '-'. 
18662 20 45 78 61 6d 70 6c 65 3a 0a 2a 2a 20 22 5b 61   Example:.** "[a
18663 2d 7a 5d 22 20 6d 61 74 63 68 65 73 20 61 6e 79  -z]" matches any
18664 20 73 69 6e 67 6c 65 20 6c 6f 77 65 72 2d 63 61   single lower-ca
18665 73 65 20 6c 65 74 74 65 72 2e 20 20 54 6f 20 6d  se letter.  To m
18666 61 74 63 68 20 61 20 27 2d 27 2c 20 6d 61 6b 65  atch a '-', make
18667 0a 2a 2a 20 69 74 20 74 68 65 20 6c 61 73 74 20  .** it the last 
18668 63 68 61 72 61 63 74 65 72 20 69 6e 20 74 68 65  character in the
18669 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69   list..**.** Thi
1866a 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 75  s routine is usu
1866b 61 6c 6c 79 20 71 75 69 63 6b 2c 20 62 75 74 20  ally quick, but 
1866c 63 61 6e 20 62 65 20 4e 2a 2a 32 20 69 6e 20 74  can be N**2 in t
1866d 68 65 20 77 6f 72 73 74 20 63 61 73 65 2e 0a 2a  he worst case..*
1866e 2a 0a 2a 2a 20 48 69 6e 74 73 3a 20 74 6f 20 6d  *.** Hints: to m
1866f 61 74 63 68 20 27 2a 27 20 6f 72 20 27 3f 27 2c  atch '*' or '?',
18670 20 70 75 74 20 74 68 65 6d 20 69 6e 20 22 5b 5d   put them in "[]
18671 22 2e 20 20 4c 69 6b 65 20 74 68 69 73 3a 0a 2a  ".  Like this:.*
18672 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 61 62 63  *.**         abc
18673 5b 2a 5d 78 79 7a 20 20 20 20 20 20 20 20 4d 61  [*]xyz        Ma
18674 74 63 68 65 73 20 22 61 62 63 2a 78 79 7a 22 20  tches "abc*xyz" 
18675 6f 6e 6c 79 0a 2a 2f 0a 73 74 61 74 69 63 20 69  only.*/.static i
18676 6e 74 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 72  nt patternCompar
18677 65 28 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a  e(.  const u8 *z
18678 50 61 74 74 65 72 6e 2c 20 20 20 20 20 20 20 20  Pattern,        
18679 20 20 20 20 20 20 2f 2a 20 54 68 65 20 67 6c 6f        /* The glo
1867a 62 20 70 61 74 74 65 72 6e 20 2a 2f 0a 20 20 63  b pattern */.  c
1867b 6f 6e 73 74 20 75 38 20 2a 7a 53 74 72 69 6e 67  onst u8 *zString
1867c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1867d 2f 2a 20 54 68 65 20 73 74 72 69 6e 67 20 74 6f  /* The string to
1867e 20 63 6f 6d 70 61 72 65 20 61 67 61 69 6e 73 74   compare against
1867f 20 74 68 65 20 67 6c 6f 62 20 2a 2f 0a 20 20 63   the glob */.  c
18680 6f 6e 73 74 20 73 74 72 75 63 74 20 63 6f 6d 70  onst struct comp
18681 61 72 65 49 6e 66 6f 20 2a 70 49 6e 66 6f 2c 20  areInfo *pInfo, 
18682 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  /* Information a
18683 62 6f 75 74 20 68 6f 77 20 74 6f 20 64 6f 20 74  bout how to do t
18684 68 65 20 63 6f 6d 70 61 72 65 20 2a 2f 0a 20 20  he compare */.  
18685 63 6f 6e 73 74 20 69 6e 74 20 65 73 63 20 20 20  const int esc   
18686 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18687 20 2f 2a 20 54 68 65 20 65 73 63 61 70 65 20 63   /* The escape c
18688 68 61 72 61 63 74 65 72 20 2a 2f 0a 29 7b 0a 20  haracter */.){. 
18689 20 69 6e 74 20 63 2c 20 63 32 3b 0a 20 20 69 6e   int c, c2;.  in
1868a 74 20 69 6e 76 65 72 74 3b 0a 20 20 69 6e 74 20  t invert;.  int 
1868b 73 65 65 6e 3b 0a 20 20 75 38 20 6d 61 74 63 68  seen;.  u8 match
1868c 4f 6e 65 20 3d 20 70 49 6e 66 6f 2d 3e 6d 61 74  One = pInfo->mat
1868d 63 68 4f 6e 65 3b 0a 20 20 75 38 20 6d 61 74 63  chOne;.  u8 matc
1868e 68 41 6c 6c 20 3d 20 70 49 6e 66 6f 2d 3e 6d 61  hAll = pInfo->ma
1868f 74 63 68 41 6c 6c 3b 0a 20 20 75 38 20 6d 61 74  tchAll;.  u8 mat
18690 63 68 53 65 74 20 3d 20 70 49 6e 66 6f 2d 3e 6d  chSet = pInfo->m
18691 61 74 63 68 53 65 74 3b 0a 20 20 75 38 20 6e 6f  atchSet;.  u8 no
18692 43 61 73 65 20 3d 20 70 49 6e 66 6f 2d 3e 6e 6f  Case = pInfo->no
18693 43 61 73 65 3b 20 0a 20 20 69 6e 74 20 70 72 65  Case; .  int pre
18694 76 45 73 63 61 70 65 20 3d 20 30 3b 20 20 20 20  vEscape = 0;    
18695 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20   /* True if the 
18696 70 72 65 76 69 6f 75 73 20 63 68 61 72 61 63 74  previous charact
18697 65 72 20 77 61 73 20 27 65 73 63 61 70 65 27 20  er was 'escape' 
18698 2a 2f 0a 0a 20 20 77 68 69 6c 65 28 20 28 63 20  */..  while( (c 
18699 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  = sqlite3Utf8Rea
1869a 64 28 7a 50 61 74 74 65 72 6e 2c 30 2c 26 7a 50  d(zPattern,0,&zP
1869b 61 74 74 65 72 6e 29 29 21 3d 30 20 29 7b 0a 20  attern))!=0 ){. 
1869c 20 20 20 69 66 28 20 21 70 72 65 76 45 73 63 61     if( !prevEsca
1869d 70 65 20 26 26 20 63 3d 3d 6d 61 74 63 68 41 6c  pe && c==matchAl
1869e 6c 20 29 7b 0a 20 20 20 20 20 20 77 68 69 6c 65  l ){.      while
1869f 28 20 28 63 3d 73 71 6c 69 74 65 33 55 74 66 38  ( (c=sqlite3Utf8
186a0 52 65 61 64 28 7a 50 61 74 74 65 72 6e 2c 30 2c  Read(zPattern,0,
186a1 26 7a 50 61 74 74 65 72 6e 29 29 20 3d 3d 20 6d  &zPattern)) == m
186a2 61 74 63 68 41 6c 6c 0a 20 20 20 20 20 20 20 20  atchAll.        
186a3 20 20 20 20 20 20 20 7c 7c 20 63 20 3d 3d 20 6d         || c == m
186a4 61 74 63 68 4f 6e 65 20 29 7b 0a 20 20 20 20 20  atchOne ){.     
186a5 20 20 20 69 66 28 20 63 3d 3d 6d 61 74 63 68 4f     if( c==matchO
186a6 6e 65 20 26 26 20 73 71 6c 69 74 65 33 55 74 66  ne && sqlite3Utf
186a7 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 20 30  8Read(zString, 0
186a8 2c 20 26 7a 53 74 72 69 6e 67 29 3d 3d 30 20 29  , &zString)==0 )
186a9 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  {.          retu
186aa 72 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a  rn 0;.        }.
186ab 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
186ac 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ( c==0 ){.      
186ad 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
186ae 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 65    }else if( c==e
186af 73 63 20 29 7b 0a 20 20 20 20 20 20 20 20 63 20  sc ){.        c 
186b0 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  = sqlite3Utf8Rea
186b1 64 28 7a 50 61 74 74 65 72 6e 2c 20 30 2c 20 26  d(zPattern, 0, &
186b2 7a 50 61 74 74 65 72 6e 29 3b 0a 20 20 20 20 20  zPattern);.     
186b3 20 20 20 69 66 28 20 63 3d 3d 30 20 29 7b 0a 20     if( c==0 ){. 
186b4 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
186b5 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  0;.        }.   
186b6 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d     }else if( c==
186b7 6d 61 74 63 68 53 65 74 20 29 7b 0a 20 20 20 20  matchSet ){.    
186b8 20 20 20 20 61 73 73 65 72 74 28 20 65 73 63 3d      assert( esc=
186b9 3d 30 20 29 3b 20 20 20 20 20 20 20 20 20 2f 2a  =0 );         /*
186ba 20 54 68 69 73 20 69 73 20 47 4c 4f 42 2c 20 6e   This is GLOB, n
186bb 6f 74 20 4c 49 4b 45 20 2a 2f 0a 20 20 20 20 20  ot LIKE */.     
186bc 20 20 20 61 73 73 65 72 74 28 20 6d 61 74 63 68     assert( match
186bd 53 65 74 3c 30 78 38 30 20 29 3b 20 20 2f 2a 20  Set<0x80 );  /* 
186be 27 5b 27 20 69 73 20 61 20 73 69 6e 67 6c 65 2d  '[' is a single-
186bf 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 2a  byte character *
186c0 2f 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28  /.        while(
186c1 20 2a 7a 53 74 72 69 6e 67 20 26 26 20 70 61 74   *zString && pat
186c2 74 65 72 6e 43 6f 6d 70 61 72 65 28 26 7a 50 61  ternCompare(&zPa
186c3 74 74 65 72 6e 5b 2d 31 5d 2c 7a 53 74 72 69 6e  ttern[-1],zStrin
186c4 67 2c 70 49 6e 66 6f 2c 65 73 63 29 3d 3d 30 20  g,pInfo,esc)==0 
186c5 29 7b 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c  ){.          SQL
186c6 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 53  ITE_SKIP_UTF8(zS
186c7 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 20 20  tring);.        
186c8 7d 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  }.        return
186c9 20 2a 7a 53 74 72 69 6e 67 21 3d 30 3b 0a 20 20   *zString!=0;.  
186ca 20 20 20 20 7d 0a 20 20 20 20 20 20 77 68 69 6c      }.      whil
186cb 65 28 20 28 63 32 20 3d 20 73 71 6c 69 74 65 33  e( (c2 = sqlite3
186cc 55 74 66 38 52 65 61 64 28 7a 53 74 72 69 6e 67  Utf8Read(zString
186cd 2c 30 2c 26 7a 53 74 72 69 6e 67 29 29 21 3d 30  ,0,&zString))!=0
186ce 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
186cf 6e 6f 43 61 73 65 20 29 7b 0a 20 20 20 20 20 20  noCase ){.      
186d0 20 20 20 20 63 32 20 3d 20 63 32 3c 30 78 38 30      c2 = c2<0x80
186d1 20 3f 20 73 71 6c 69 74 65 33 55 70 70 65 72 54   ? sqlite3UpperT
186d2 6f 4c 6f 77 65 72 5b 63 32 5d 20 3a 20 63 32 3b  oLower[c2] : c2;
186d3 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 63  .          c = c
186d4 3c 30 78 38 30 20 3f 20 73 71 6c 69 74 65 33 55  <0x80 ? sqlite3U
186d5 70 70 65 72 54 6f 4c 6f 77 65 72 5b 63 5d 20 3a  pperToLower[c] :
186d6 20 63 3b 0a 20 20 20 20 20 20 20 20 20 20 77 68   c;.          wh
186d7 69 6c 65 28 20 63 32 20 21 3d 20 30 20 26 26 20  ile( c2 != 0 && 
186d8 63 32 20 21 3d 20 63 20 29 7b 0a 20 20 20 20 20  c2 != c ){.     
186d9 20 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69         c2 = sqli
186da 74 65 33 55 74 66 38 52 65 61 64 28 7a 53 74 72  te3Utf8Read(zStr
186db 69 6e 67 2c 20 30 2c 20 26 7a 53 74 72 69 6e 67  ing, 0, &zString
186dc 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  );.            i
186dd 66 28 20 63 32 3c 30 78 38 30 20 29 20 63 32 20  f( c2<0x80 ) c2 
186de 3d 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f  = sqlite3UpperTo
186df 4c 6f 77 65 72 5b 63 32 5d 3b 0a 20 20 20 20 20  Lower[c2];.     
186e0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
186e1 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
186e2 77 68 69 6c 65 28 20 63 32 20 21 3d 20 30 20 26  while( c2 != 0 &
186e3 26 20 63 32 20 21 3d 20 63 20 29 7b 0a 20 20 20  & c2 != c ){.   
186e4 20 20 20 20 20 20 20 20 20 63 32 20 3d 20 73 71           c2 = sq
186e5 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 53  lite3Utf8Read(zS
186e6 74 72 69 6e 67 2c 20 30 2c 20 26 7a 53 74 72 69  tring, 0, &zStri
186e7 6e 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  ng);.          }
186e8 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
186e9 20 20 20 69 66 28 20 63 32 3d 3d 30 20 29 20 72     if( c2==0 ) r
186ea 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 20  eturn 0;.       
186eb 20 69 66 28 20 70 61 74 74 65 72 6e 43 6f 6d 70   if( patternComp
186ec 61 72 65 28 7a 50 61 74 74 65 72 6e 2c 7a 53 74  are(zPattern,zSt
186ed 72 69 6e 67 2c 70 49 6e 66 6f 2c 65 73 63 29 20  ring,pInfo,esc) 
186ee 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20  ) return 1;.    
186ef 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e    }.      return
186f0 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66   0;.    }else if
186f1 28 20 21 70 72 65 76 45 73 63 61 70 65 20 26 26  ( !prevEscape &&
186f2 20 63 3d 3d 6d 61 74 63 68 4f 6e 65 20 29 7b 0a   c==matchOne ){.
186f3 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
186f4 33 55 74 66 38 52 65 61 64 28 7a 53 74 72 69 6e  3Utf8Read(zStrin
186f5 67 2c 20 30 2c 20 26 7a 53 74 72 69 6e 67 29 3d  g, 0, &zString)=
186f6 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  =0 ){.        re
186f7 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a  turn 0;.      }.
186f8 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d      }else if( c=
186f9 3d 6d 61 74 63 68 53 65 74 20 29 7b 0a 20 20 20  =matchSet ){.   
186fa 20 20 20 69 6e 74 20 70 72 69 6f 72 5f 63 20 3d     int prior_c =
186fb 20 30 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   0;.      assert
186fc 28 20 65 73 63 3d 3d 30 20 29 3b 20 20 20 20 2f  ( esc==0 );    /
186fd 2a 20 54 68 69 73 20 6f 6e 6c 79 20 6f 63 63 75  * This only occu
186fe 72 73 20 66 6f 72 20 47 4c 4f 42 2c 20 6e 6f 74  rs for GLOB, not
186ff 20 4c 49 4b 45 20 2a 2f 0a 20 20 20 20 20 20 73   LIKE */.      s
18700 65 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 69  een = 0;.      i
18701 6e 76 65 72 74 20 3d 20 30 3b 0a 20 20 20 20 20  nvert = 0;.     
18702 20 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38   c = sqlite3Utf8
18703 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 20 30 2c  Read(zString, 0,
18704 20 26 7a 53 74 72 69 6e 67 29 3b 0a 20 20 20 20   &zString);.    
18705 20 20 69 66 28 20 63 3d 3d 30 20 29 20 72 65 74    if( c==0 ) ret
18706 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 63 32 20  urn 0;.      c2 
18707 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  = sqlite3Utf8Rea
18708 64 28 7a 50 61 74 74 65 72 6e 2c 20 30 2c 20 26  d(zPattern, 0, &
18709 7a 50 61 74 74 65 72 6e 29 3b 0a 20 20 20 20 20  zPattern);.     
1870a 20 69 66 28 20 63 32 3d 3d 27 5e 27 20 29 7b 0a   if( c2=='^' ){.
1870b 20 20 20 20 20 20 20 20 69 6e 76 65 72 74 20 3d          invert =
1870c 20 31 3b 0a 20 20 20 20 20 20 20 20 63 32 20 3d   1;.        c2 =
1870d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64   sqlite3Utf8Read
1870e 28 7a 50 61 74 74 65 72 6e 2c 20 30 2c 20 26 7a  (zPattern, 0, &z
1870f 50 61 74 74 65 72 6e 29 3b 0a 20 20 20 20 20 20  Pattern);.      
18710 7d 0a 20 20 20 20 20 20 69 66 28 20 63 32 3d 3d  }.      if( c2==
18711 27 5d 27 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ']' ){.        i
18712 66 28 20 63 3d 3d 27 5d 27 20 29 20 73 65 65 6e  f( c==']' ) seen
18713 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 63 32   = 1;.        c2
18714 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65   = sqlite3Utf8Re
18715 61 64 28 7a 50 61 74 74 65 72 6e 2c 20 30 2c 20  ad(zPattern, 0, 
18716 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20 20 20 20  &zPattern);.    
18717 20 20 7d 0a 20 20 20 20 20 20 77 68 69 6c 65 28    }.      while(
18718 20 63 32 20 26 26 20 63 32 21 3d 27 5d 27 20 29   c2 && c2!=']' )
18719 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 32  {.        if( c2
1871a 3d 3d 27 2d 27 20 26 26 20 7a 50 61 74 74 65 72  =='-' && zPatter
1871b 6e 5b 30 5d 21 3d 27 5d 27 20 26 26 20 7a 50 61  n[0]!=']' && zPa
1871c 74 74 65 72 6e 5b 30 5d 21 3d 30 20 26 26 20 70  ttern[0]!=0 && p
1871d 72 69 6f 72 5f 63 3e 30 20 29 7b 0a 20 20 20 20  rior_c>0 ){.    
1871e 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74        c2 = sqlit
1871f 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 74 74  e3Utf8Read(zPatt
18720 65 72 6e 2c 20 30 2c 20 26 7a 50 61 74 74 65 72  ern, 0, &zPatter
18721 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  n);.          if
18722 28 20 63 3e 3d 70 72 69 6f 72 5f 63 20 26 26 20  ( c>=prior_c && 
18723 63 3c 3d 63 32 20 29 20 73 65 65 6e 20 3d 20 31  c<=c2 ) seen = 1
18724 3b 0a 20 20 20 20 20 20 20 20 20 20 70 72 69 6f  ;.          prio
18725 72 5f 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  r_c = 0;.       
18726 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
18727 20 20 69 66 28 20 63 3d 3d 63 32 20 29 7b 0a 20    if( c==c2 ){. 
18728 20 20 20 20 20 20 20 20 20 20 20 73 65 65 6e 20             seen 
18729 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  = 1;.          }
1872a 0a 20 20 20 20 20 20 20 20 20 20 70 72 69 6f 72  .          prior
1872b 5f 63 20 3d 20 63 32 3b 0a 20 20 20 20 20 20 20  _c = c2;.       
1872c 20 7d 0a 20 20 20 20 20 20 20 20 63 32 20 3d 20   }.        c2 = 
1872d 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28  sqlite3Utf8Read(
1872e 7a 50 61 74 74 65 72 6e 2c 20 30 2c 20 26 7a 50  zPattern, 0, &zP
1872f 61 74 74 65 72 6e 29 3b 0a 20 20 20 20 20 20 7d  attern);.      }
18730 0a 20 20 20 20 20 20 69 66 28 20 63 32 3d 3d 30  .      if( c2==0
18731 20 7c 7c 20 28 73 65 65 6e 20 5e 20 69 6e 76 65   || (seen ^ inve
18732 72 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  rt)==0 ){.      
18733 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
18734 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66    }.    }else if
18735 28 20 65 73 63 3d 3d 63 20 26 26 20 21 70 72 65  ( esc==c && !pre
18736 76 45 73 63 61 70 65 20 29 7b 0a 20 20 20 20 20  vEscape ){.     
18737 20 70 72 65 76 45 73 63 61 70 65 20 3d 20 31 3b   prevEscape = 1;
18738 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
18739 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74    c2 = sqlite3Ut
1873a 66 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 20  f8Read(zString, 
1873b 30 2c 20 26 7a 53 74 72 69 6e 67 29 3b 0a 20 20  0, &zString);.  
1873c 20 20 20 20 69 66 28 20 6e 6f 43 61 73 65 20 29      if( noCase )
1873d 7b 0a 20 20 20 20 20 20 20 20 63 20 3d 20 63 3c  {.        c = c<
1873e 30 78 38 30 20 3f 20 73 71 6c 69 74 65 33 55 70  0x80 ? sqlite3Up
1873f 70 65 72 54 6f 4c 6f 77 65 72 5b 63 5d 20 3a 20  perToLower[c] : 
18740 63 3b 0a 20 20 20 20 20 20 20 20 63 32 20 3d 20  c;.        c2 = 
18741 63 32 3c 30 78 38 30 20 3f 20 73 71 6c 69 74 65  c2<0x80 ? sqlite
18742 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 63 32  3UpperToLower[c2
18743 5d 20 3a 20 63 32 3b 0a 20 20 20 20 20 20 7d 0a  ] : c2;.      }.
18744 20 20 20 20 20 20 69 66 28 20 63 21 3d 63 32 20        if( c!=c2 
18745 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
18746 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 0;.      }.   
18747 20 20 20 70 72 65 76 45 73 63 61 70 65 20 3d 20     prevEscape = 
18748 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  0;.    }.  }.  r
18749 65 74 75 72 6e 20 2a 7a 53 74 72 69 6e 67 3d 3d  eturn *zString==
1874a 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e  0;.}../*.** Coun
1874b 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  t the number of 
1874c 74 69 6d 65 73 20 74 68 61 74 20 74 68 65 20 4c  times that the L
1874d 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 28 6f 72  IKE operator (or
1874e 20 47 4c 4f 42 20 77 68 69 63 68 20 69 73 0a 2a   GLOB which is.*
1874f 2a 20 6a 75 73 74 20 61 20 76 61 72 69 61 74 69  * just a variati
18750 6f 6e 20 6f 66 20 4c 49 4b 45 29 20 67 65 74 73  on of LIKE) gets
18751 20 63 61 6c 6c 65 64 2e 20 20 54 68 69 73 20 69   called.  This i
18752 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69  s used for testi
18753 6e 67 0a 2a 2a 20 6f 6e 6c 79 2e 0a 2a 2f 0a 23  ng.** only..*/.#
18754 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
18755 54 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c 69  T.int sqlite3_li
18756 6b 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65  ke_count = 0;.#e
18757 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70  ndif.../*.** Imp
18758 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
18759 68 65 20 6c 69 6b 65 28 29 20 53 51 4c 20 66 75  he like() SQL fu
1875a 6e 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 66 75  nction.  This fu
1875b 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74  nction implement
1875c 73 0a 2a 2a 20 74 68 65 20 62 75 69 6c 64 2d 69  s.** the build-i
1875d 6e 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 2e  n LIKE operator.
1875e 20 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75    The first argu
1875f 6d 65 6e 74 20 74 6f 20 74 68 65 20 66 75 6e 63  ment to the func
18760 74 69 6f 6e 20 69 73 20 74 68 65 0a 2a 2a 20 70  tion is the.** p
18761 61 74 74 65 72 6e 20 61 6e 64 20 74 68 65 20 73  attern and the s
18762 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69  econd argument i
18763 73 20 74 68 65 20 73 74 72 69 6e 67 2e 20 20 53  s the string.  S
18764 6f 2c 20 74 68 65 20 53 51 4c 20 73 74 61 74 65  o, the SQL state
18765 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ments:.**.**    
18766 20 20 20 41 20 4c 49 4b 45 20 42 0a 2a 2a 0a 2a     A LIKE B.**.*
18767 2a 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64  * is implemented
18768 20 61 73 20 6c 69 6b 65 28 42 2c 41 29 2e 0a 2a   as like(B,A)..*
18769 2a 0a 2a 2a 20 54 68 69 73 20 73 61 6d 65 20 66  *.** This same f
1876a 75 6e 63 74 69 6f 6e 20 28 77 69 74 68 20 61 20  unction (with a 
1876b 64 69 66 66 65 72 65 6e 74 20 63 6f 6d 70 61 72  different compar
1876c 65 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 29  eInfo structure)
1876d 20 63 6f 6d 70 75 74 65 73 0a 2a 2a 20 74 68 65   computes.** the
1876e 20 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72 2e 0a   GLOB operator..
1876f 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c  */.static void l
18770 69 6b 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  ikeFunc(.  sqlit
18771 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
18772 65 78 74 2c 20 0a 20 20 69 6e 74 20 61 72 67 63  ext, .  int argc
18773 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  , .  sqlite3_val
18774 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63  ue **argv.){.  c
18775 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
18776 61 72 20 2a 7a 41 2c 20 2a 7a 42 3b 0a 20 20 69  ar *zA, *zB;.  i
18777 6e 74 20 65 73 63 61 70 65 20 3d 20 30 3b 0a 0a  nt escape = 0;..
18778 20 20 7a 42 20 3d 20 73 71 6c 69 74 65 33 5f 76    zB = sqlite3_v
18779 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
1877a 5d 29 3b 0a 20 20 7a 41 20 3d 20 73 71 6c 69 74  ]);.  zA = sqlit
1877b 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
1877c 67 76 5b 31 5d 29 3b 0a 0a 20 20 2f 2a 20 4c 69  gv[1]);..  /* Li
1877d 6d 69 74 20 74 68 65 20 6c 65 6e 67 74 68 20 6f  mit the length o
1877e 66 20 74 68 65 20 4c 49 4b 45 20 6f 72 20 47 4c  f the LIKE or GL
1877f 4f 42 20 70 61 74 74 65 72 6e 20 74 6f 20 61 76  OB pattern to av
18780 6f 69 64 20 70 72 6f 62 6c 65 6d 73 0a 20 20 2a  oid problems.  *
18781 2a 20 6f 66 20 64 65 65 70 20 72 65 63 75 72 73  * of deep recurs
18782 69 6f 6e 20 61 6e 64 20 4e 2a 4e 20 62 65 68 61  ion and N*N beha
18783 76 69 6f 72 20 69 6e 20 70 61 74 74 65 72 6e 43  vior in patternC
18784 6f 6d 70 61 72 65 28 29 2e 0a 20 20 2a 2f 0a 20  ompare()..  */. 
18785 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c   if( sqlite3_val
18786 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d  ue_bytes(argv[0]
18787 29 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b  )>SQLITE_MAX_LIK
18788 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48  E_PATTERN_LENGTH
18789 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
1878a 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e  result_error(con
1878b 74 65 78 74 2c 20 22 4c 49 4b 45 20 6f 72 20 47  text, "LIKE or G
1878c 4c 4f 42 20 70 61 74 74 65 72 6e 20 74 6f 6f 20  LOB pattern too 
1878d 63 6f 6d 70 6c 65 78 22 2c 20 2d 31 29 3b 0a 20  complex", -1);. 
1878e 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
1878f 20 61 73 73 65 72 74 28 20 7a 42 3d 3d 73 71 6c   assert( zB==sql
18790 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
18791 61 72 67 76 5b 30 5d 29 20 29 3b 20 20 2f 2a 20  argv[0]) );  /* 
18792 45 6e 63 6f 64 69 6e 67 20 64 69 64 20 6e 6f 74  Encoding did not
18793 20 63 68 61 6e 67 65 20 2a 2f 0a 0a 20 20 69 66   change */..  if
18794 28 20 61 72 67 63 3d 3d 33 20 29 7b 0a 20 20 20  ( argc==3 ){.   
18795 20 2f 2a 20 54 68 65 20 65 73 63 61 70 65 20 63   /* The escape c
18796 68 61 72 61 63 74 65 72 20 73 74 72 69 6e 67 20  haracter string 
18797 6d 75 73 74 20 63 6f 6e 73 69 73 74 20 6f 66 20  must consist of 
18798 61 20 73 69 6e 67 6c 65 20 55 54 46 2d 38 20 63  a single UTF-8 c
18799 68 61 72 61 63 74 65 72 2e 0a 20 20 20 20 2a 2a  haracter..    **
1879a 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 75   Otherwise, retu
1879b 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 20  rn an error..   
1879c 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 75 6e   */.    const un
1879d 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 45 73  signed char *zEs
1879e 63 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  c = sqlite3_valu
1879f 65 5f 74 65 78 74 28 61 72 67 76 5b 32 5d 29 3b  e_text(argv[2]);
187a0 0a 20 20 20 20 69 66 28 20 7a 45 73 63 3d 3d 30  .    if( zEsc==0
187a1 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 69   ) return;.    i
187a2 66 28 20 73 71 6c 69 74 65 33 55 74 66 38 43 68  f( sqlite3Utf8Ch
187a3 61 72 4c 65 6e 28 28 63 68 61 72 2a 29 7a 45 73  arLen((char*)zEs
187a4 63 2c 20 2d 31 29 21 3d 31 20 29 7b 0a 20 20 20  c, -1)!=1 ){.   
187a5 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
187a6 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c  t_error(context,
187a7 20 0a 20 20 20 20 20 20 20 20 20 20 22 45 53 43   .          "ESC
187a8 41 50 45 20 65 78 70 72 65 73 73 69 6f 6e 20 6d  APE expression m
187a9 75 73 74 20 62 65 20 61 20 73 69 6e 67 6c 65 20  ust be a single 
187aa 63 68 61 72 61 63 74 65 72 22 2c 20 2d 31 29 3b  character", -1);
187ab 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
187ac 20 20 20 7d 0a 20 20 20 20 65 73 63 61 70 65 20     }.    escape 
187ad 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  = sqlite3Utf8Rea
187ae 64 28 7a 45 73 63 2c 20 30 2c 20 26 7a 45 73 63  d(zEsc, 0, &zEsc
187af 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 41 20  );.  }.  if( zA 
187b0 26 26 20 7a 42 20 29 7b 0a 20 20 20 20 73 74 72  && zB ){.    str
187b1 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20  uct compareInfo 
187b2 2a 70 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33  *pInfo = sqlite3
187b3 5f 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65  _user_data(conte
187b4 78 74 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  xt);.#ifdef SQLI
187b5 54 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c 69  TE_TEST.    sqli
187b6 74 65 33 5f 6c 69 6b 65 5f 63 6f 75 6e 74 2b 2b  te3_like_count++
187b7 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 0a 20 20  ;.#endif.    .  
187b8 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
187b9 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 70 61  _int(context, pa
187ba 74 74 65 72 6e 43 6f 6d 70 61 72 65 28 7a 42 2c  tternCompare(zB,
187bb 20 7a 41 2c 20 70 49 6e 66 6f 2c 20 65 73 63 61   zA, pInfo, esca
187bc 70 65 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  pe));.  }.}../*.
187bd 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
187be 6e 20 6f 66 20 74 68 65 20 4e 55 4c 4c 49 46 28  n of the NULLIF(
187bf 78 2c 79 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20  x,y) function.  
187c0 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 74 68  The result is th
187c1 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d  e first.** argum
187c2 65 6e 74 20 69 66 20 74 68 65 20 61 72 67 75 6d  ent if the argum
187c3 65 6e 74 73 20 61 72 65 20 64 69 66 66 65 72 65  ents are differe
187c4 6e 74 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20  nt.  The result 
187c5 69 73 20 4e 55 4c 4c 20 69 66 20 74 68 65 0a 2a  is NULL if the.*
187c6 2a 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20  * arguments are 
187c7 65 71 75 61 6c 20 74 6f 20 65 61 63 68 20 6f 74  equal to each ot
187c8 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  her..*/.static v
187c9 6f 69 64 20 6e 75 6c 6c 69 66 46 75 6e 63 28 0a  oid nullifFunc(.
187ca 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
187cb 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
187cc 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65  t argc,.  sqlite
187cd 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
187ce 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  {.  CollSeq *pCo
187cf 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 65 74 46  ll = sqlite3GetF
187d0 75 6e 63 43 6f 6c 6c 53 65 71 28 63 6f 6e 74 65  uncCollSeq(conte
187d1 78 74 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  xt);.  if( sqlit
187d2 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 61 72 67  e3MemCompare(arg
187d3 76 5b 30 5d 2c 20 61 72 67 76 5b 31 5d 2c 20 70  v[0], argv[1], p
187d4 43 6f 6c 6c 29 21 3d 30 20 29 7b 0a 20 20 20 20  Coll)!=0 ){.    
187d5 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76  sqlite3_result_v
187d6 61 6c 75 65 28 63 6f 6e 74 65 78 74 2c 20 61 72  alue(context, ar
187d7 67 76 5b 30 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  gv[0]);.  }.}../
187d8 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
187d9 69 6f 6e 20 6f 66 20 74 68 65 20 56 45 52 53 49  ion of the VERSI
187da 4f 4e 28 2a 29 20 66 75 6e 63 74 69 6f 6e 2e 20  ON(*) function. 
187db 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 74   The result is t
187dc 68 65 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 6f 66  he version.** of
187dd 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72   the SQLite libr
187de 61 72 79 20 74 68 61 74 20 69 73 20 72 75 6e 6e  ary that is runn
187df 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ing..*/.static v
187e0 6f 69 64 20 76 65 72 73 69 6f 6e 46 75 6e 63 28  oid versionFunc(
187e1 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
187e2 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
187e3 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74  nt argc,.  sqlit
187e4 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
187e5 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  ){.  sqlite3_res
187e6 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
187e7 2c 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f  , sqlite3_versio
187e8 6e 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54  n, -1, SQLITE_ST
187e9 41 54 49 43 29 3b 0a 7d 0a 0a 2f 2a 20 41 72 72  ATIC);.}../* Arr
187ea 61 79 20 66 6f 72 20 63 6f 6e 76 65 72 74 69 6e  ay for convertin
187eb 67 20 66 72 6f 6d 20 68 61 6c 66 2d 62 79 74 65  g from half-byte
187ec 73 20 28 6e 79 62 62 6c 65 73 29 20 69 6e 74 6f  s (nybbles) into
187ed 20 41 53 43 49 49 20 68 65 78 0a 2a 2a 20 64 69   ASCII hex.** di
187ee 67 69 74 73 2e 20 2a 2f 0a 73 74 61 74 69 63 20  gits. */.static 
187ef 63 6f 6e 73 74 20 63 68 61 72 20 68 65 78 64 69  const char hexdi
187f0 67 69 74 73 5b 5d 20 3d 20 7b 0a 20 20 27 30 27  gits[] = {.  '0'
187f1 2c 20 27 31 27 2c 20 27 32 27 2c 20 27 33 27 2c  , '1', '2', '3',
187f2 20 27 34 27 2c 20 27 35 27 2c 20 27 36 27 2c 20   '4', '5', '6', 
187f3 27 37 27 2c 0a 20 20 27 38 27 2c 20 27 39 27 2c  '7',.  '8', '9',
187f4 20 27 41 27 2c 20 27 42 27 2c 20 27 43 27 2c 20   'A', 'B', 'C', 
187f5 27 44 27 2c 20 27 45 27 2c 20 27 46 27 20 0a 7d  'D', 'E', 'F' .}
187f6 3b 0a 0a 2f 2a 0a 2a 2a 20 45 58 50 45 52 49 4d  ;../*.** EXPERIM
187f7 45 4e 54 41 4c 20 2d 20 54 68 69 73 20 69 73 20  ENTAL - This is 
187f8 6e 6f 74 20 61 6e 20 6f 66 66 69 63 69 61 6c 20  not an official 
187f9 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 69  function.  The i
187fa 6e 74 65 72 66 61 63 65 20 6d 61 79 0a 2a 2a 20  nterface may.** 
187fb 63 68 61 6e 67 65 2e 20 20 54 68 69 73 20 66 75  change.  This fu
187fc 6e 63 74 69 6f 6e 20 6d 61 79 20 64 69 73 61 70  nction may disap
187fd 70 65 61 72 2e 20 20 44 6f 20 6e 6f 74 20 77 72  pear.  Do not wr
187fe 69 74 65 20 63 6f 64 65 20 74 68 61 74 20 64 65  ite code that de
187ff 70 65 6e 64 73 0a 2a 2a 20 6f 6e 20 74 68 69 73  pends.** on this
18800 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   function..**.**
18801 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
18802 6f 66 20 74 68 65 20 51 55 4f 54 45 28 29 20 66  of the QUOTE() f
18803 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 66  unction.  This f
18804 75 6e 63 74 69 6f 6e 20 74 61 6b 65 73 20 61 20  unction takes a 
18805 73 69 6e 67 6c 65 0a 2a 2a 20 61 72 67 75 6d 65  single.** argume
18806 6e 74 2e 20 20 49 66 20 74 68 65 20 61 72 67 75  nt.  If the argu
18807 6d 65 6e 74 20 69 73 20 6e 75 6d 65 72 69 63 2c  ment is numeric,
18808 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
18809 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73  e is the same as
1880a 0a 2a 2a 20 74 68 65 20 61 72 67 75 6d 65 6e 74  .** the argument
1880b 2e 20 20 49 66 20 74 68 65 20 61 72 67 75 6d 65  .  If the argume
1880c 6e 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20  nt is NULL, the 
1880d 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20  return value is 
1880e 74 68 65 20 73 74 72 69 6e 67 0a 2a 2a 20 22 4e  the string.** "N
1880f 55 4c 4c 22 2e 20 20 4f 74 68 65 72 77 69 73 65  ULL".  Otherwise
18810 2c 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69  , the argument i
18811 73 20 65 6e 63 6c 6f 73 65 64 20 69 6e 20 73 69  s enclosed in si
18812 6e 67 6c 65 20 71 75 6f 74 65 73 20 77 69 74 68  ngle quotes with
18813 0a 2a 2a 20 73 69 6e 67 6c 65 2d 71 75 6f 74 65  .** single-quote
18814 20 65 73 63 61 70 65 73 2e 0a 2a 2f 0a 73 74 61   escapes..*/.sta
18815 74 69 63 20 76 6f 69 64 20 71 75 6f 74 65 46 75  tic void quoteFu
18816 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  nc(sqlite3_conte
18817 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74  xt *context, int
18818 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
18819 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20  alue **argv){.  
1881a 69 66 28 20 61 72 67 63 3c 31 20 29 20 72 65 74  if( argc<1 ) ret
1881b 75 72 6e 3b 0a 20 20 73 77 69 74 63 68 28 20 73  urn;.  switch( s
1881c 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
1881d 65 28 61 72 67 76 5b 30 5d 29 20 29 7b 0a 20 20  e(argv[0]) ){.  
1881e 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4e 55    case SQLITE_NU
1881f 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69  LL: {.      sqli
18820 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
18821 63 6f 6e 74 65 78 74 2c 20 22 4e 55 4c 4c 22 2c  context, "NULL",
18822 20 34 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49   4, SQLITE_STATI
18823 43 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  C);.      break;
18824 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
18825 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 0a  SQLITE_INTEGER:.
18826 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
18827 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 73  FLOAT: {.      s
18828 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61  qlite3_result_va
18829 6c 75 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67  lue(context, arg
1882a 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 62 72 65  v[0]);.      bre
1882b 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
1882c 73 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a 20  se SQLITE_BLOB: 
1882d 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54  {.      char *zT
1882e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 63  ext = 0;.      c
1882f 68 61 72 20 63 6f 6e 73 74 20 2a 7a 42 6c 6f 62  har const *zBlob
18830 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
18831 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 3b 0a  _blob(argv[0]);.
18832 20 20 20 20 20 20 69 6e 74 20 6e 42 6c 6f 62 20        int nBlob 
18833 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
18834 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a  bytes(argv[0]);.
18835 20 20 20 20 20 20 61 73 73 65 72 74 28 20 7a 42        assert( zB
18836 6c 6f 62 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c  lob==sqlite3_val
18837 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29  ue_blob(argv[0])
18838 20 29 3b 20 2f 2a 20 4e 6f 20 65 6e 63 6f 64 69   ); /* No encodi
18839 6e 67 20 63 68 61 6e 67 65 20 2a 2f 0a 0a 20 20  ng change */..  
1883a 20 20 20 20 69 66 28 20 32 2a 6e 42 6c 6f 62 2b      if( 2*nBlob+
1883b 34 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e  4>SQLITE_MAX_LEN
1883c 47 54 48 20 29 7b 0a 20 20 20 20 20 20 20 20 73  GTH ){.        s
1883d 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
1883e 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65  ror_toobig(conte
1883f 78 74 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74  xt);.        ret
18840 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
18841 20 20 20 7a 54 65 78 74 20 3d 20 28 63 68 61 72     zText = (char
18842 20 2a 29 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28   *)sqliteMalloc(
18843 28 32 2a 6e 42 6c 6f 62 29 2b 34 29 3b 20 0a 20  (2*nBlob)+4); . 
18844 20 20 20 20 20 69 66 28 20 21 7a 54 65 78 74 20       if( !zText 
18845 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
18846 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
18847 63 6f 6e 74 65 78 74 2c 20 22 6f 75 74 20 6f 66  context, "out of
18848 20 6d 65 6d 6f 72 79 22 2c 20 2d 31 29 3b 0a 20   memory", -1);. 
18849 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1884a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20      int i;.     
1884b 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 42     for(i=0; i<nB
1884c 6c 6f 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  lob; i++){.     
1884d 20 20 20 20 20 7a 54 65 78 74 5b 28 69 2a 32 29       zText[(i*2)
1884e 2b 32 5d 20 3d 20 68 65 78 64 69 67 69 74 73 5b  +2] = hexdigits[
1884f 28 7a 42 6c 6f 62 5b 69 5d 3e 3e 34 29 26 30 78  (zBlob[i]>>4)&0x
18850 30 46 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 7a  0F];.          z
18851 54 65 78 74 5b 28 69 2a 32 29 2b 33 5d 20 3d 20  Text[(i*2)+3] = 
18852 68 65 78 64 69 67 69 74 73 5b 28 7a 42 6c 6f 62  hexdigits[(zBlob
18853 5b 69 5d 29 26 30 78 30 46 5d 3b 0a 20 20 20 20  [i])&0x0F];.    
18854 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7a 54      }.        zT
18855 65 78 74 5b 28 6e 42 6c 6f 62 2a 32 29 2b 32 5d  ext[(nBlob*2)+2]
18856 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20 20 20   = '\'';.       
18857 20 7a 54 65 78 74 5b 28 6e 42 6c 6f 62 2a 32 29   zText[(nBlob*2)
18858 2b 33 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20  +3] = '\0';.    
18859 20 20 20 20 7a 54 65 78 74 5b 30 5d 20 3d 20 27      zText[0] = '
1885a 58 27 3b 0a 20 20 20 20 20 20 20 20 7a 54 65 78  X';.        zTex
1885b 74 5b 31 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20  t[1] = '\'';.   
1885c 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
1885d 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
1885e 2c 20 7a 54 65 78 74 2c 20 2d 31 2c 20 53 51 4c  , zText, -1, SQL
1885f 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a  ITE_TRANSIENT);.
18860 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 46 72          sqliteFr
18861 65 65 28 7a 54 65 78 74 29 3b 0a 20 20 20 20 20  ee(zText);.     
18862 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
18863 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53      }.    case S
18864 51 4c 49 54 45 5f 54 45 58 54 3a 20 7b 0a 20 20  QLITE_TEXT: {.  
18865 20 20 20 20 69 6e 74 20 69 2c 6a 3b 0a 20 20 20      int i,j;.   
18866 20 20 20 75 36 34 20 6e 3b 0a 20 20 20 20 20 20     u64 n;.      
18867 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
18868 68 61 72 20 2a 7a 41 72 67 20 3d 20 73 71 6c 69  har *zArg = sqli
18869 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
1886a 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 63  rgv[0]);.      c
1886b 68 61 72 20 2a 7a 3b 0a 0a 20 20 20 20 20 20 69  har *z;..      i
1886c 66 28 20 7a 41 72 67 3d 3d 30 20 29 20 72 65 74  f( zArg==0 ) ret
1886d 75 72 6e 3b 0a 20 20 20 20 20 20 66 6f 72 28 69  urn;.      for(i
1886e 3d 30 2c 20 6e 3d 30 3b 20 7a 41 72 67 5b 69 5d  =0, n=0; zArg[i]
1886f 3b 20 69 2b 2b 29 7b 20 69 66 28 20 7a 41 72 67  ; i++){ if( zArg
18870 5b 69 5d 3d 3d 27 5c 27 27 20 29 20 6e 2b 2b 3b  [i]=='\'' ) n++;
18871 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 2b 6e   }.      if( i+n
18872 2b 33 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45  +3>SQLITE_MAX_LE
18873 4e 47 54 48 20 29 7b 0a 20 20 20 20 20 20 20 20  NGTH ){.        
18874 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
18875 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74  rror_toobig(cont
18876 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20 72 65  ext);.        re
18877 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
18878 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 4d 61      z = sqliteMa
18879 6c 6c 6f 63 28 20 69 2b 6e 2b 33 20 29 3b 0a 20  lloc( i+n+3 );. 
1887a 20 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 20       if( z==0 ) 
1887b 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7a 5b  return;.      z[
1887c 30 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20  0] = '\'';.     
1887d 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 31 3b 20 7a   for(i=0, j=1; z
1887e 41 72 67 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20  Arg[i]; i++){.  
1887f 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a        z[j++] = z
18880 41 72 67 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20  Arg[i];.        
18881 69 66 28 20 7a 41 72 67 5b 69 5d 3d 3d 27 5c 27  if( zArg[i]=='\'
18882 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a  ' ){.          z
18883 5b 6a 2b 2b 5d 20 3d 20 27 5c 27 27 3b 0a 20 20  [j++] = '\'';.  
18884 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
18885 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27        z[j++] = '
18886 5c 27 27 3b 0a 20 20 20 20 20 20 7a 5b 6a 5d 20  \'';.      z[j] 
18887 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  = 0;.      sqlit
18888 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63  e3_result_text(c
18889 6f 6e 74 65 78 74 2c 20 7a 2c 20 6a 2c 20 53 51  ontext, z, j, SQ
1888a 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b  LITE_TRANSIENT);
1888b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 46 72 65  .      sqliteFre
1888c 65 28 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  e(z);.    }.  }.
1888d 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 78  }../*.** The hex
1888e 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 49 6e  () function.  In
1888f 74 65 72 70 72 65 74 20 74 68 65 20 61 72 67 75  terpret the argu
18890 6d 65 6e 74 20 61 73 20 61 20 62 6c 6f 62 2e 20  ment as a blob. 
18891 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20 68 65 78   Return.** a hex
18892 61 64 65 63 69 6d 61 6c 20 72 65 6e 64 65 72 69  adecimal renderi
18893 6e 67 20 61 73 20 74 65 78 74 2e 0a 2a 2f 0a 73  ng as text..*/.s
18894 74 61 74 69 63 20 76 6f 69 64 20 68 65 78 46 75  tatic void hexFu
18895 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
18896 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
18897 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
18898 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
18899 67 76 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6e  gv.){.  int i, n
1889a 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  ;.  const unsign
1889b 65 64 20 63 68 61 72 20 2a 70 42 6c 6f 62 3b 0a  ed char *pBlob;.
1889c 20 20 63 68 61 72 20 2a 7a 48 65 78 2c 20 2a 7a    char *zHex, *z
1889d 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63  ;.  assert( argc
1889e 3d 3d 31 20 29 3b 0a 20 20 70 42 6c 6f 62 20 3d  ==1 );.  pBlob =
1889f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
188a0 6c 6f 62 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  lob(argv[0]);.  
188a1 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  n = sqlite3_valu
188a2 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29  e_bytes(argv[0])
188a3 3b 0a 20 20 69 66 28 20 6e 2a 32 2b 31 3e 53 51  ;.  if( n*2+1>SQ
188a4 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20  LITE_MAX_LENGTH 
188a5 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  ){.    sqlite3_r
188a6 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62  esult_error_toob
188a7 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  ig(context);.   
188a8 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61   return;.  }.  a
188a9 73 73 65 72 74 28 20 70 42 6c 6f 62 3d 3d 73 71  ssert( pBlob==sq
188aa 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
188ab 28 61 72 67 76 5b 30 5d 29 20 29 3b 20 20 2f 2a  (argv[0]) );  /*
188ac 20 4e 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68 61   No encoding cha
188ad 6e 67 65 20 2a 2f 0a 20 20 7a 20 3d 20 7a 48 65  nge */.  z = zHe
188ae 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  x = sqlite3_mall
188af 6f 63 28 6e 2a 32 20 2b 20 31 29 3b 0a 20 20 69  oc(n*2 + 1);.  i
188b0 66 28 20 7a 48 65 78 3d 3d 30 20 29 20 72 65 74  f( zHex==0 ) ret
188b1 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  urn;.  for(i=0; 
188b2 69 3c 6e 3b 20 69 2b 2b 2c 20 70 42 6c 6f 62 2b  i<n; i++, pBlob+
188b3 2b 29 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64  +){.    unsigned
188b4 20 63 68 61 72 20 63 20 3d 20 2a 70 42 6c 6f 62   char c = *pBlob
188b5 3b 0a 20 20 20 20 2a 28 7a 2b 2b 29 20 3d 20 68  ;.    *(z++) = h
188b6 65 78 64 69 67 69 74 73 5b 28 63 3e 3e 34 29 26  exdigits[(c>>4)&
188b7 30 78 66 5d 3b 0a 20 20 20 20 2a 28 7a 2b 2b 29  0xf];.    *(z++)
188b8 20 3d 20 68 65 78 64 69 67 69 74 73 5b 63 26 30   = hexdigits[c&0
188b9 78 66 5d 3b 0a 20 20 7d 0a 20 20 2a 7a 20 3d 20  xf];.  }.  *z = 
188ba 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  0;.  sqlite3_res
188bb 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
188bc 2c 20 7a 48 65 78 2c 20 6e 2a 32 2c 20 73 71 6c  , zHex, n*2, sql
188bd 69 74 65 33 5f 66 72 65 65 29 3b 0a 7d 0a 0a 2f  ite3_free);.}../
188be 2a 0a 2a 2a 20 54 68 65 20 7a 65 72 6f 62 6c 6f  *.** The zeroblo
188bf 62 28 4e 29 20 66 75 6e 63 74 69 6f 6e 20 72 65  b(N) function re
188c0 74 75 72 6e 73 20 61 20 7a 65 72 6f 2d 66 69 6c  turns a zero-fil
188c1 6c 65 64 20 62 6c 6f 62 20 6f 66 20 73 69 7a 65  led blob of size
188c2 20 4e 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61   N bytes..*/.sta
188c3 74 69 63 20 76 6f 69 64 20 7a 65 72 6f 62 6c 6f  tic void zeroblo
188c4 62 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  bFunc(.  sqlite3
188c5 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
188c6 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20  t,.  int argc,. 
188c7 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
188c8 2a 61 72 67 76 0a 29 7b 0a 20 20 69 36 34 20 6e  *argv.){.  i64 n
188c9 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63  ;.  assert( argc
188ca 3d 3d 31 20 29 3b 0a 20 20 6e 20 3d 20 73 71 6c  ==1 );.  n = sql
188cb 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34  ite3_value_int64
188cc 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28  (argv[0]);.  if(
188cd 20 6e 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45   n>SQLITE_MAX_LE
188ce 4e 47 54 48 20 29 7b 0a 20 20 20 20 73 71 6c 69  NGTH ){.    sqli
188cf 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
188d0 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78 74 29  _toobig(context)
188d1 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
188d2 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65  qlite3_result_ze
188d3 72 6f 62 6c 6f 62 28 63 6f 6e 74 65 78 74 2c 20  roblob(context, 
188d4 6e 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  n);.  }.}../*.**
188d5 20 54 68 65 20 72 65 70 6c 61 63 65 28 29 20 66   The replace() f
188d6 75 6e 63 74 69 6f 6e 2e 20 20 54 68 72 65 65 20  unction.  Three 
188d7 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20 61 6c  arguments are al
188d8 6c 20 73 74 72 69 6e 67 73 3a 20 63 61 6c 6c 0a  l strings: call.
188d9 2a 2a 20 74 68 65 6d 20 41 2c 20 42 2c 20 61 6e  ** them A, B, an
188da 64 20 43 2e 20 54 68 65 20 72 65 73 75 6c 74 20  d C. The result 
188db 69 73 20 61 6c 73 6f 20 61 20 73 74 72 69 6e 67  is also a string
188dc 20 77 68 69 63 68 20 69 73 20 64 65 72 69 76 65   which is derive
188dd 64 0a 2a 2a 20 66 72 6f 6d 20 41 20 62 79 20 72  d.** from A by r
188de 65 70 6c 61 63 69 6e 67 20 65 76 65 72 79 20 6f  eplacing every o
188df 63 63 75 72 61 6e 63 65 20 6f 66 20 42 20 77 69  ccurance of B wi
188e0 74 68 20 43 2e 20 20 54 68 65 20 6d 61 74 63 68  th C.  The match
188e1 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 78 61 63  .** must be exac
188e2 74 2e 20 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65  t.  Collating se
188e3 71 75 65 6e 63 65 73 20 61 72 65 20 6e 6f 74 20  quences are not 
188e4 75 73 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  used..*/.static 
188e5 76 6f 69 64 20 72 65 70 6c 61 63 65 46 75 6e 63  void replaceFunc
188e6 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
188e7 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
188e8 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69  int argc,.  sqli
188e9 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
188ea 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69  .){.  const unsi
188eb 67 6e 65 64 20 63 68 61 72 20 2a 7a 53 74 72 3b  gned char *zStr;
188ec 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69          /* The i
188ed 6e 70 75 74 20 73 74 72 69 6e 67 20 41 20 2a 2f  nput string A */
188ee 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  .  const unsigne
188ef 64 20 63 68 61 72 20 2a 7a 50 61 74 74 65 72 6e  d char *zPattern
188f0 3b 20 20 20 20 2f 2a 20 54 68 65 20 70 61 74 74  ;    /* The patt
188f1 65 72 6e 20 73 74 72 69 6e 67 20 42 20 2a 2f 0a  ern string B */.
188f2 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
188f3 20 63 68 61 72 20 2a 7a 52 65 70 3b 20 20 20 20   char *zRep;    
188f4 20 20 20 20 2f 2a 20 54 68 65 20 72 65 70 6c 61      /* The repla
188f5 63 65 6d 65 6e 74 20 73 74 72 69 6e 67 20 43 20  cement string C 
188f6 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  */.  unsigned ch
188f7 61 72 20 2a 7a 4f 75 74 3b 20 20 20 20 20 20 20  ar *zOut;       
188f8 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 75         /* The ou
188f9 74 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 53  tput */.  int nS
188fa 74 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  tr;             
188fb 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 53     /* Size of zS
188fc 74 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 74  tr */.  int nPat
188fd 74 65 72 6e 3b 20 20 20 20 20 20 20 20 20 20 20  tern;           
188fe 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 50 61 74   /* Size of zPat
188ff 74 65 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 52  tern */.  int nR
18900 65 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ep;             
18901 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 52     /* Size of zR
18902 65 70 20 2a 2f 0a 20 20 69 36 34 20 6e 4f 75 74  ep */.  i64 nOut
18903 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
18904 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65   /* Maximum size
18905 20 6f 66 20 7a 4f 75 74 20 2a 2f 0a 20 20 69 6e   of zOut */.  in
18906 74 20 6c 6f 6f 70 4c 69 6d 69 74 3b 20 20 20 20  t loopLimit;    
18907 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 7a         /* Last z
18908 53 74 72 5b 5d 20 74 68 61 74 20 6d 69 67 68 74  Str[] that might
18909 20 6d 61 74 63 68 20 7a 50 61 74 74 65 72 6e 5b   match zPattern[
1890a 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 6a 3b  ] */.  int i, j;
1890b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1890c 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73  /* Loop counters
1890d 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 61   */..  assert( a
1890e 72 67 63 3d 3d 33 20 29 3b 0a 20 20 7a 53 74 72  rgc==3 );.  zStr
1890f 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
18910 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a  _text(argv[0]);.
18911 20 20 69 66 28 20 7a 53 74 72 3d 3d 30 20 29 20    if( zStr==0 ) 
18912 72 65 74 75 72 6e 3b 0a 20 20 6e 53 74 72 20 3d  return;.  nStr =
18913 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
18914 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20  ytes(argv[0]);. 
18915 20 61 73 73 65 72 74 28 20 7a 53 74 72 3d 3d 73   assert( zStr==s
18916 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
18917 74 28 61 72 67 76 5b 30 5d 29 20 29 3b 20 20 2f  t(argv[0]) );  /
18918 2a 20 4e 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68  * No encoding ch
18919 61 6e 67 65 20 2a 2f 0a 20 20 7a 50 61 74 74 65  ange */.  zPatte
1891a 72 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  rn = sqlite3_val
1891b 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29  ue_text(argv[1])
1891c 3b 0a 20 20 69 66 28 20 7a 50 61 74 74 65 72 6e  ;.  if( zPattern
1891d 3d 3d 30 20 7c 7c 20 7a 50 61 74 74 65 72 6e 5b  ==0 || zPattern[
1891e 30 5d 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  0]==0 ) return;.
1891f 20 20 6e 50 61 74 74 65 72 6e 20 3d 20 73 71 6c    nPattern = sql
18920 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
18921 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 61 73 73  (argv[1]);.  ass
18922 65 72 74 28 20 7a 50 61 74 74 65 72 6e 3d 3d 73  ert( zPattern==s
18923 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
18924 74 28 61 72 67 76 5b 31 5d 29 20 29 3b 20 20 2f  t(argv[1]) );  /
18925 2a 20 4e 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68  * No encoding ch
18926 61 6e 67 65 20 2a 2f 0a 20 20 7a 52 65 70 20 3d  ange */.  zRep =
18927 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
18928 65 78 74 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20  ext(argv[2]);.  
18929 69 66 28 20 7a 52 65 70 3d 3d 30 20 29 20 72 65  if( zRep==0 ) re
1892a 74 75 72 6e 3b 0a 20 20 6e 52 65 70 20 3d 20 73  turn;.  nRep = s
1892b 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
1892c 65 73 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 61  es(argv[2]);.  a
1892d 73 73 65 72 74 28 20 7a 52 65 70 3d 3d 73 71 6c  ssert( zRep==sql
1892e 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
1892f 61 72 67 76 5b 32 5d 29 20 29 3b 0a 20 20 6e 4f  argv[2]) );.  nO
18930 75 74 20 3d 20 6e 53 74 72 20 2b 20 31 3b 0a 20  ut = nStr + 1;. 
18931 20 61 73 73 65 72 74 28 20 6e 4f 75 74 3c 53 51   assert( nOut<SQ
18932 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20  LITE_MAX_LENGTH 
18933 29 3b 0a 20 20 7a 4f 75 74 20 3d 20 73 71 6c 69  );.  zOut = sqli
18934 74 65 33 5f 6d 61 6c 6c 6f 63 28 28 69 6e 74 29  te3_malloc((int)
18935 6e 4f 75 74 29 3b 0a 20 20 69 66 28 20 7a 4f 75  nOut);.  if( zOu
18936 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  t==0 ){.    retu
18937 72 6e 3b 0a 20 20 7d 0a 20 20 6c 6f 6f 70 4c 69  rn;.  }.  loopLi
18938 6d 69 74 20 3d 20 6e 53 74 72 20 2d 20 6e 50 61  mit = nStr - nPa
18939 74 74 65 72 6e 3b 20 20 0a 20 20 66 6f 72 28 69  ttern;  .  for(i
1893a 3d 6a 3d 30 3b 20 69 3c 3d 6c 6f 6f 70 4c 69 6d  =j=0; i<=loopLim
1893b 69 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  it; i++){.    if
1893c 28 20 7a 53 74 72 5b 69 5d 21 3d 7a 50 61 74 74  ( zStr[i]!=zPatt
1893d 65 72 6e 5b 30 5d 20 7c 7c 20 6d 65 6d 63 6d 70  ern[0] || memcmp
1893e 28 26 7a 53 74 72 5b 69 5d 2c 20 7a 50 61 74 74  (&zStr[i], zPatt
1893f 65 72 6e 2c 20 6e 50 61 74 74 65 72 6e 29 20 29  ern, nPattern) )
18940 7b 0a 20 20 20 20 20 20 7a 4f 75 74 5b 6a 2b 2b  {.      zOut[j++
18941 5d 20 3d 20 7a 53 74 72 5b 69 5d 3b 0a 20 20 20  ] = zStr[i];.   
18942 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 4f   }else{.      nO
18943 75 74 20 2b 3d 20 6e 52 65 70 20 2d 20 6e 50 61  ut += nRep - nPa
18944 74 74 65 72 6e 3b 0a 20 20 20 20 20 20 69 66 28  ttern;.      if(
18945 20 6e 4f 75 74 3e 3d 53 51 4c 49 54 45 5f 4d 41   nOut>=SQLITE_MA
18946 58 5f 4c 45 4e 47 54 48 20 29 7b 0a 20 20 20 20  X_LENGTH ){.    
18947 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
18948 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28  lt_error_toobig(
18949 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20  context);.      
1894a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
1894b 4f 75 74 29 3b 0a 20 20 20 20 20 20 20 20 72 65  Out);.        re
1894c 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
1894d 20 20 20 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74      zOut = sqlit
1894e 65 33 5f 72 65 61 6c 6c 6f 63 28 7a 4f 75 74 2c  e3_realloc(zOut,
1894f 20 28 69 6e 74 29 6e 4f 75 74 29 3b 0a 20 20 20   (int)nOut);.   
18950 20 20 20 69 66 28 20 7a 4f 75 74 3d 3d 30 20 29     if( zOut==0 )
18951 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
18952 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
18953 6d 65 6d 63 70 79 28 26 7a 4f 75 74 5b 6a 5d 2c  memcpy(&zOut[j],
18954 20 7a 52 65 70 2c 20 6e 52 65 70 29 3b 0a 20 20   zRep, nRep);.  
18955 20 20 20 20 6a 20 2b 3d 20 6e 52 65 70 3b 0a 20      j += nRep;. 
18956 20 20 20 20 20 69 20 2b 3d 20 6e 50 61 74 74 65       i += nPatte
18957 72 6e 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  rn-1;.    }.  }.
18958 20 20 61 73 73 65 72 74 28 20 6a 2b 6e 53 74 72    assert( j+nStr
18959 2d 69 2b 31 3d 3d 6e 4f 75 74 20 29 3b 0a 20 20  -i+1==nOut );.  
1895a 6d 65 6d 63 70 79 28 26 7a 4f 75 74 5b 6a 5d 2c  memcpy(&zOut[j],
1895b 20 26 7a 53 74 72 5b 69 5d 2c 20 6e 53 74 72 2d   &zStr[i], nStr-
1895c 69 29 3b 0a 20 20 6a 20 2b 3d 20 6e 53 74 72 20  i);.  j += nStr 
1895d 2d 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 6a  - i;.  assert( j
1895e 3c 3d 6e 4f 75 74 20 29 3b 0a 20 20 7a 4f 75 74  <=nOut );.  zOut
1895f 5b 6a 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74  [j] = 0;.  sqlit
18960 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63  e3_result_text(c
18961 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a 29 7a  ontext, (char*)z
18962 4f 75 74 2c 20 6a 2c 20 73 71 6c 69 74 65 33 5f  Out, j, sqlite3_
18963 66 72 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  free);.}../*.** 
18964 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
18965 66 20 74 68 65 20 54 52 49 4d 28 29 2c 20 4c 54  f the TRIM(), LT
18966 52 49 4d 28 29 2c 20 61 6e 64 20 52 54 52 49 4d  RIM(), and RTRIM
18967 28 29 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a  () functions..**
18968 20 54 68 65 20 75 73 65 72 64 61 74 61 20 69 73   The userdata is
18969 20 30 78 31 20 66 6f 72 20 6c 65 66 74 20 74 72   0x1 for left tr
1896a 69 6d 2c 20 30 78 32 20 66 6f 72 20 72 69 67 68  im, 0x2 for righ
1896b 74 20 74 72 69 6d 2c 20 30 78 33 20 66 6f 72 20  t trim, 0x3 for 
1896c 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  both..*/.static 
1896d 76 6f 69 64 20 74 72 69 6d 46 75 6e 63 28 0a 20  void trimFunc(. 
1896e 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
1896f 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
18970 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33   argc,.  sqlite3
18971 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
18972 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  .  const unsigne
18973 64 20 63 68 61 72 20 2a 7a 49 6e 3b 20 20 20 20  d char *zIn;    
18974 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 73 74       /* Input st
18975 72 69 6e 67 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ring */.  const 
18976 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
18977 43 68 61 72 53 65 74 3b 20 20 20 20 2f 2a 20 53  CharSet;    /* S
18978 65 74 20 6f 66 20 63 68 61 72 61 63 74 65 72 73  et of characters
18979 20 74 6f 20 74 72 69 6d 20 2a 2f 0a 20 20 69 6e   to trim */.  in
1897a 74 20 6e 49 6e 3b 20 20 20 20 20 20 20 20 20 20  t nIn;          
1897b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1897c 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
1897d 65 73 20 69 6e 20 69 6e 70 75 74 20 2a 2f 0a 20  es in input */. 
1897e 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20   int flags;     
1897f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18980 20 20 20 2f 2a 20 31 3a 20 74 72 69 6d 6c 65 66     /* 1: trimlef
18981 74 20 20 32 3a 20 74 72 69 6d 72 69 67 68 74 20  t  2: trimright 
18982 20 33 3a 20 74 72 69 6d 20 2a 2f 0a 20 20 69 6e   3: trim */.  in
18983 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
18984 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18985 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
18986 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  */.  unsigned ch
18987 61 72 20 2a 61 4c 65 6e 3b 20 20 20 20 20 20 20  ar *aLen;       
18988 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68         /* Length
18989 20 6f 66 20 65 61 63 68 20 63 68 61 72 61 63 74   of each charact
1898a 65 72 20 69 6e 20 7a 43 68 61 72 53 65 74 20 2a  er in zCharSet *
1898b 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  /.  const unsign
1898c 65 64 20 63 68 61 72 20 2a 2a 61 7a 43 68 61 72  ed char **azChar
1898d 3b 20 20 20 20 20 2f 2a 20 49 6e 64 69 76 69 64  ;     /* Individ
1898e 75 61 6c 20 63 68 61 72 61 63 74 65 72 73 20 69  ual characters i
1898f 6e 20 7a 43 68 61 72 53 65 74 20 2a 2f 0a 20 20  n zCharSet */.  
18990 69 6e 74 20 6e 43 68 61 72 3b 20 20 20 20 20 20  int nChar;      
18991 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18992 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
18993 68 61 72 61 63 74 65 72 73 20 69 6e 20 7a 43 68  haracters in zCh
18994 61 72 53 65 74 20 2a 2f 0a 0a 20 20 69 66 28 20  arSet */..  if( 
18995 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
18996 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c  pe(argv[0])==SQL
18997 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20  ITE_NULL ){.    
18998 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 7a 49  return;.  }.  zI
18999 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  n = sqlite3_valu
1899a 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b  e_text(argv[0]);
1899b 0a 20 20 69 66 28 20 7a 49 6e 3d 3d 30 20 29 20  .  if( zIn==0 ) 
1899c 72 65 74 75 72 6e 3b 0a 20 20 6e 49 6e 20 3d 20  return;.  nIn = 
1899d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
1899e 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  tes(argv[0]);.  
1899f 61 73 73 65 72 74 28 20 7a 49 6e 3d 3d 73 71 6c  assert( zIn==sql
189a0 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
189a1 61 72 67 76 5b 30 5d 29 20 29 3b 0a 20 20 69 66  argv[0]) );.  if
189a2 28 20 61 72 67 63 3d 3d 31 20 29 7b 0a 20 20 20  ( argc==1 ){.   
189a3 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e   static const un
189a4 73 69 67 6e 65 64 20 63 68 61 72 20 6c 65 6e 4f  signed char lenO
189a5 6e 65 5b 5d 20 3d 20 7b 20 31 20 7d 3b 0a 20 20  ne[] = { 1 };.  
189a6 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
189a7 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 7a  nsigned char *az
189a8 4f 6e 65 5b 5d 20 3d 20 7b 20 28 75 38 2a 29 22  One[] = { (u8*)"
189a9 20 22 20 7d 3b 0a 20 20 20 20 6e 43 68 61 72 20   " };.    nChar 
189aa 3d 20 31 3b 0a 20 20 20 20 61 4c 65 6e 20 3d 20  = 1;.    aLen = 
189ab 28 75 38 2a 29 6c 65 6e 4f 6e 65 3b 0a 20 20 20  (u8*)lenOne;.   
189ac 20 61 7a 43 68 61 72 20 3d 20 61 7a 4f 6e 65 3b   azChar = azOne;
189ad 0a 20 20 20 20 7a 43 68 61 72 53 65 74 20 3d 20  .    zCharSet = 
189ae 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 28  0;.  }else if( (
189af 7a 43 68 61 72 53 65 74 20 3d 20 73 71 6c 69 74  zCharSet = sqlit
189b0 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
189b1 67 76 5b 31 5d 29 29 3d 3d 30 20 29 7b 0a 20 20  gv[1]))==0 ){.  
189b2 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73    return;.  }els
189b3 65 7b 0a 20 20 20 20 63 6f 6e 73 74 20 75 6e 73  e{.    const uns
189b4 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a 20  igned char *z;. 
189b5 20 20 20 66 6f 72 28 7a 3d 7a 43 68 61 72 53 65     for(z=zCharSe
189b6 74 2c 20 6e 43 68 61 72 3d 30 3b 20 2a 7a 3b 20  t, nChar=0; *z; 
189b7 6e 43 68 61 72 2b 2b 29 7b 0a 20 20 20 20 20 20  nChar++){.      
189b8 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38  SQLITE_SKIP_UTF8
189b9 28 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  (z);.    }.    i
189ba 66 28 20 6e 43 68 61 72 3e 30 20 29 7b 0a 20 20  f( nChar>0 ){.  
189bb 20 20 20 20 61 7a 43 68 61 72 20 3d 20 73 71 6c      azChar = sql
189bc 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 6e 43 68  ite3_malloc( nCh
189bd 61 72 2a 28 73 69 7a 65 6f 66 28 63 68 61 72 2a  ar*(sizeof(char*
189be 29 2b 31 29 20 29 3b 0a 20 20 20 20 20 20 69 66  )+1) );.      if
189bf 28 20 61 7a 43 68 61 72 3d 3d 30 20 29 7b 0a 20  ( azChar==0 ){. 
189c0 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20         return;. 
189c1 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 4c 65       }.      aLe
189c2 6e 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68  n = (unsigned ch
189c3 61 72 2a 29 26 61 7a 43 68 61 72 5b 6e 43 68 61  ar*)&azChar[nCha
189c4 72 5d 3b 0a 20 20 20 20 20 20 66 6f 72 28 7a 3d  r];.      for(z=
189c5 7a 43 68 61 72 53 65 74 2c 20 6e 43 68 61 72 3d  zCharSet, nChar=
189c6 30 3b 20 2a 7a 3b 20 6e 43 68 61 72 2b 2b 29 7b  0; *z; nChar++){
189c7 0a 20 20 20 20 20 20 20 20 61 7a 43 68 61 72 5b  .        azChar[
189c8 6e 43 68 61 72 5d 20 3d 20 7a 3b 0a 20 20 20 20  nChar] = z;.    
189c9 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f      SQLITE_SKIP_
189ca 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 20 20 20  UTF8(z);.       
189cb 20 61 4c 65 6e 5b 6e 43 68 61 72 5d 20 3d 20 7a   aLen[nChar] = z
189cc 20 2d 20 61 7a 43 68 61 72 5b 6e 43 68 61 72 5d   - azChar[nChar]
189cd 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
189ce 20 20 7d 0a 20 20 69 66 28 20 6e 43 68 61 72 3e    }.  if( nChar>
189cf 30 20 29 7b 0a 20 20 20 20 66 6c 61 67 73 20 3d  0 ){.    flags =
189d0 20 28 69 6e 74 29 73 71 6c 69 74 65 33 5f 75 73   (int)sqlite3_us
189d1 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29  er_data(context)
189d2 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 20  ;.    if( flags 
189d3 26 20 31 20 29 7b 0a 20 20 20 20 20 20 77 68 69  & 1 ){.      whi
189d4 6c 65 28 20 6e 49 6e 3e 30 20 29 7b 0a 20 20 20  le( nIn>0 ){.   
189d5 20 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20       int len;.  
189d6 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
189d7 3c 6e 43 68 61 72 3b 20 69 2b 2b 29 7b 0a 20 20  <nChar; i++){.  
189d8 20 20 20 20 20 20 20 20 6c 65 6e 20 3d 20 61 4c          len = aL
189d9 65 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 20  en[i];.         
189da 20 69 66 28 20 6d 65 6d 63 6d 70 28 7a 49 6e 2c   if( memcmp(zIn,
189db 20 61 7a 43 68 61 72 5b 69 5d 2c 20 6c 65 6e 29   azChar[i], len)
189dc 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  ==0 ) break;.   
189dd 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
189de 66 28 20 69 3e 3d 6e 43 68 61 72 20 29 20 62 72  f( i>=nChar ) br
189df 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7a 49 6e  eak;.        zIn
189e0 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 20   += len;.       
189e1 20 6e 49 6e 20 2d 3d 20 6c 65 6e 3b 0a 20 20 20   nIn -= len;.   
189e2 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
189e3 66 28 20 66 6c 61 67 73 20 26 20 32 20 29 7b 0a  f( flags & 2 ){.
189e4 20 20 20 20 20 20 77 68 69 6c 65 28 20 6e 49 6e        while( nIn
189e5 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  >0 ){.        in
189e6 74 20 6c 65 6e 3b 0a 20 20 20 20 20 20 20 20 66  t len;.        f
189e7 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 68 61 72 3b  or(i=0; i<nChar;
189e8 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   i++){.         
189e9 20 6c 65 6e 20 3d 20 61 4c 65 6e 5b 69 5d 3b 0a   len = aLen[i];.
189ea 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6c 65            if( le
189eb 6e 3c 3d 6e 49 6e 20 26 26 20 6d 65 6d 63 6d 70  n<=nIn && memcmp
189ec 28 26 7a 49 6e 5b 6e 49 6e 2d 6c 65 6e 5d 2c 61  (&zIn[nIn-len],a
189ed 7a 43 68 61 72 5b 69 5d 2c 6c 65 6e 29 3d 3d 30  zChar[i],len)==0
189ee 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
189ef 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
189f0 69 3e 3d 6e 43 68 61 72 20 29 20 62 72 65 61 6b  i>=nChar ) break
189f1 3b 0a 20 20 20 20 20 20 20 20 6e 49 6e 20 2d 3d  ;.        nIn -=
189f2 20 6c 65 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20   len;.      }.  
189f3 20 20 7d 0a 20 20 20 20 69 66 28 20 7a 43 68 61    }.    if( zCha
189f4 72 53 65 74 20 29 7b 0a 20 20 20 20 20 20 73 71  rSet ){.      sq
189f5 6c 69 74 65 33 5f 66 72 65 65 28 61 7a 43 68 61  lite3_free(azCha
189f6 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  r);.    }.  }.  
189f7 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
189f8 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 28 63 68  ext(context, (ch
189f9 61 72 2a 29 7a 49 6e 2c 20 6e 49 6e 2c 20 53 51  ar*)zIn, nIn, SQ
189fa 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b  LITE_TRANSIENT);
189fb 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .}..#ifdef SQLIT
189fc 45 5f 53 4f 55 4e 44 45 58 0a 2f 2a 0a 2a 2a 20  E_SOUNDEX./*.** 
189fd 43 6f 6d 70 75 74 65 20 74 68 65 20 73 6f 75 6e  Compute the soun
189fe 64 65 78 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20  dex encoding of 
189ff 61 20 77 6f 72 64 2e 0a 2a 2f 0a 73 74 61 74 69  a word..*/.stati
18a00 63 20 76 6f 69 64 20 73 6f 75 6e 64 65 78 46 75  c void soundexFu
18a01 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
18a02 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
18a03 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
18a04 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
18a05 67 76 0a 29 7b 0a 20 20 63 68 61 72 20 7a 52 65  gv.){.  char zRe
18a06 73 75 6c 74 5b 38 5d 3b 0a 20 20 63 6f 6e 73 74  sult[8];.  const
18a07 20 75 38 20 2a 7a 49 6e 3b 0a 20 20 69 6e 74 20   u8 *zIn;.  int 
18a08 69 2c 20 6a 3b 0a 20 20 73 74 61 74 69 63 20 63  i, j;.  static c
18a09 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
18a0a 61 72 20 69 43 6f 64 65 5b 5d 20 3d 20 7b 0a 20  ar iCode[] = {. 
18a0b 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30     0, 0, 0, 0, 0
18a0c 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
18a0d 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
18a0e 30 2c 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c 20  0,.    0, 0, 0, 
18a0f 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
18a10 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
18a11 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30 2c   0, 0,.    0, 0,
18a12 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
18a13 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
18a14 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30  , 0, 0, 0,.    0
18a15 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
18a16 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
18a17 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20  0, 0, 0, 0, 0,. 
18a18 20 20 20 30 2c 20 30 2c 20 31 2c 20 32 2c 20 33     0, 0, 1, 2, 3
18a19 2c 20 30 2c 20 31 2c 20 32 2c 20 30 2c 20 30 2c  , 0, 1, 2, 0, 0,
18a1a 20 32 2c 20 32 2c 20 34 2c 20 35 2c 20 35 2c 20   2, 2, 4, 5, 5, 
18a1b 30 2c 0a 20 20 20 20 31 2c 20 32 2c 20 36 2c 20  0,.    1, 2, 6, 
18a1c 32 2c 20 33 2c 20 30 2c 20 31 2c 20 30 2c 20 32  2, 3, 0, 1, 0, 2
18a1d 2c 20 30 2c 20 32 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 2, 0, 0, 0,
18a1e 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30 2c   0, 0,.    0, 0,
18a1f 20 31 2c 20 32 2c 20 33 2c 20 30 2c 20 31 2c 20   1, 2, 3, 0, 1, 
18a20 32 2c 20 30 2c 20 30 2c 20 32 2c 20 32 2c 20 34  2, 0, 0, 2, 2, 4
18a21 2c 20 35 2c 20 35 2c 20 30 2c 0a 20 20 20 20 31  , 5, 5, 0,.    1
18a22 2c 20 32 2c 20 36 2c 20 32 2c 20 33 2c 20 30 2c  , 2, 6, 2, 3, 0,
18a23 20 31 2c 20 30 2c 20 32 2c 20 30 2c 20 32 2c 20   1, 0, 2, 0, 2, 
18a24 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20  0, 0, 0, 0, 0,. 
18a25 20 7d 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72   };.  assert( ar
18a26 67 63 3d 3d 31 20 29 3b 0a 20 20 7a 49 6e 20 3d  gc==1 );.  zIn =
18a27 20 28 75 38 2a 29 73 71 6c 69 74 65 33 5f 76 61   (u8*)sqlite3_va
18a28 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
18a29 29 3b 0a 20 20 69 66 28 20 7a 49 6e 3d 3d 30 20  );.  if( zIn==0 
18a2a 29 20 7a 49 6e 20 3d 20 28 75 38 2a 29 22 22 3b  ) zIn = (u8*)"";
18a2b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 7a 49 6e 5b  .  for(i=0; zIn[
18a2c 69 5d 20 26 26 20 21 69 73 61 6c 70 68 61 28 7a  i] && !isalpha(z
18a2d 49 6e 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20  In[i]); i++){}. 
18a2e 20 69 66 28 20 7a 49 6e 5b 69 5d 20 29 7b 0a 20   if( zIn[i] ){. 
18a2f 20 20 20 75 38 20 70 72 65 76 63 6f 64 65 20 3d     u8 prevcode =
18a30 20 69 43 6f 64 65 5b 7a 49 6e 5b 69 5d 26 30 78   iCode[zIn[i]&0x
18a31 37 66 5d 3b 0a 20 20 20 20 7a 52 65 73 75 6c 74  7f];.    zResult
18a32 5b 30 5d 20 3d 20 74 6f 75 70 70 65 72 28 7a 49  [0] = toupper(zI
18a33 6e 5b 69 5d 29 3b 0a 20 20 20 20 66 6f 72 28 6a  n[i]);.    for(j
18a34 3d 31 3b 20 6a 3c 34 20 26 26 20 7a 49 6e 5b 69  =1; j<4 && zIn[i
18a35 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  ]; i++){.      i
18a36 6e 74 20 63 6f 64 65 20 3d 20 69 43 6f 64 65 5b  nt code = iCode[
18a37 7a 49 6e 5b 69 5d 26 30 78 37 66 5d 3b 0a 20 20  zIn[i]&0x7f];.  
18a38 20 20 20 20 69 66 28 20 63 6f 64 65 3e 30 20 29      if( code>0 )
18a39 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 6f  {.        if( co
18a3a 64 65 21 3d 70 72 65 76 63 6f 64 65 20 29 7b 0a  de!=prevcode ){.
18a3b 20 20 20 20 20 20 20 20 20 20 70 72 65 76 63 6f            prevco
18a3c 64 65 20 3d 20 63 6f 64 65 3b 0a 20 20 20 20 20  de = code;.     
18a3d 20 20 20 20 20 7a 52 65 73 75 6c 74 5b 6a 2b 2b       zResult[j++
18a3e 5d 20 3d 20 63 6f 64 65 20 2b 20 27 30 27 3b 0a  ] = code + '0';.
18a3f 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
18a40 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70  }else{.        p
18a41 72 65 76 63 6f 64 65 20 3d 20 30 3b 0a 20 20 20  revcode = 0;.   
18a42 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 77     }.    }.    w
18a43 68 69 6c 65 28 20 6a 3c 34 20 29 7b 0a 20 20 20  hile( j<4 ){.   
18a44 20 20 20 7a 52 65 73 75 6c 74 5b 6a 2b 2b 5d 20     zResult[j++] 
18a45 3d 20 27 30 27 3b 0a 20 20 20 20 7d 0a 20 20 20  = '0';.    }.   
18a46 20 7a 52 65 73 75 6c 74 5b 6a 5d 20 3d 20 30 3b   zResult[j] = 0;
18a47 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
18a48 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
18a49 2c 20 7a 52 65 73 75 6c 74 2c 20 34 2c 20 53 51  , zResult, 4, SQ
18a4a 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b  LITE_TRANSIENT);
18a4b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
18a4c 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
18a4d 74 28 63 6f 6e 74 65 78 74 2c 20 22 3f 30 30 30  t(context, "?000
18a4e 22 2c 20 34 2c 20 53 51 4c 49 54 45 5f 53 54 41  ", 4, SQLITE_STA
18a4f 54 49 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64  TIC);.  }.}.#end
18a50 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
18a51 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54  TE_OMIT_LOAD_EXT
18a52 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 41 20 66  ENSION./*.** A f
18a53 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 6c 6f 61  unction that loa
18a54 64 73 20 61 20 73 68 61 72 65 64 2d 6c 69 62 72  ds a shared-libr
18a55 61 72 79 20 65 78 74 65 6e 73 69 6f 6e 20 74 68  ary extension th
18a56 65 6e 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2e  en returns NULL.
18a57 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
18a58 6c 6f 61 64 45 78 74 28 73 71 6c 69 74 65 33 5f  loadExt(sqlite3_
18a59 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
18a5a 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69  , int argc, sqli
18a5b 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
18a5c 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ){.  const char 
18a5d 2a 7a 46 69 6c 65 20 3d 20 28 63 6f 6e 73 74 20  *zFile = (const 
18a5e 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76  char *)sqlite3_v
18a5f 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
18a60 5d 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  ]);.  const char
18a61 20 2a 7a 50 72 6f 63 3b 0a 20 20 73 71 6c 69 74   *zProc;.  sqlit
18a62 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33  e3 *db = sqlite3
18a63 5f 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65  _user_data(conte
18a64 78 74 29 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72  xt);.  char *zEr
18a65 72 4d 73 67 20 3d 20 30 3b 0a 0a 20 20 69 66 28  rMsg = 0;..  if(
18a66 20 61 72 67 63 3d 3d 32 20 29 7b 0a 20 20 20 20   argc==2 ){.    
18a67 7a 50 72 6f 63 20 3d 20 28 63 6f 6e 73 74 20 63  zProc = (const c
18a68 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61  har *)sqlite3_va
18a69 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d  lue_text(argv[1]
18a6a 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
18a6b 7a 50 72 6f 63 20 3d 20 30 3b 0a 20 20 7d 0a 20  zProc = 0;.  }. 
18a6c 20 69 66 28 20 7a 46 69 6c 65 20 26 26 20 73 71   if( zFile && sq
18a6d 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e  lite3_load_exten
18a6e 73 69 6f 6e 28 64 62 2c 20 7a 46 69 6c 65 2c 20  sion(db, zFile, 
18a6f 7a 50 72 6f 63 2c 20 26 7a 45 72 72 4d 73 67 29  zProc, &zErrMsg)
18a70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
18a71 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e  result_error(con
18a72 74 65 78 74 2c 20 7a 45 72 72 4d 73 67 2c 20 2d  text, zErrMsg, -
18a73 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  1);.    sqlite3_
18a74 66 72 65 65 28 7a 45 72 72 4d 73 67 29 3b 0a 20  free(zErrMsg);. 
18a75 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66   }.}.#endif..#if
18a76 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
18a77 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
18a78 69 6f 6e 20 67 65 6e 65 72 61 74 65 73 20 61 20  ion generates a 
18a79 73 74 72 69 6e 67 20 6f 66 20 72 61 6e 64 6f 6d  string of random
18a7a 20 63 68 61 72 61 63 74 65 72 73 2e 20 20 55 73   characters.  Us
18a7b 65 64 20 66 6f 72 0a 2a 2a 20 67 65 6e 65 72 61  ed for.** genera
18a7c 74 69 6e 67 20 74 65 73 74 20 64 61 74 61 2e 0a  ting test data..
18a7d 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72  */.static void r
18a7e 61 6e 64 53 74 72 28 73 71 6c 69 74 65 33 5f 63  andStr(sqlite3_c
18a7f 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
18a80 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
18a81 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29  e3_value **argv)
18a82 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  {.  static const
18a83 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 7a   unsigned char z
18a84 53 72 63 5b 5d 20 3d 20 0a 20 20 20 20 20 22 61  Src[] = .     "a
18a85 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71  bcdefghijklmnopq
18a86 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20 20 20  rstuvwxyz".     
18a87 22 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  "ABCDEFGHIJKLMNO
18a88 50 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20 20  PQRSTUVWXYZ".   
18a89 20 20 22 30 31 32 33 34 35 36 37 38 39 22 0a 20    "0123456789". 
18a8a 20 20 20 20 22 2e 2d 21 2c 3a 2a 5e 2b 3d 5f 7c      ".-!,:*^+=_|
18a8b 3f 2f 3c 3e 20 22 3b 0a 20 20 69 6e 74 20 69 4d  ?/<> ";.  int iM
18a8c 69 6e 2c 20 69 4d 61 78 2c 20 6e 2c 20 72 2c 20  in, iMax, n, r, 
18a8d 69 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  i;.  unsigned ch
18a8e 61 72 20 7a 42 75 66 5b 31 30 30 30 5d 3b 0a 20  ar zBuf[1000];. 
18a8f 20 69 66 28 20 61 72 67 63 3e 3d 31 20 29 7b 0a   if( argc>=1 ){.
18a90 20 20 20 20 69 4d 69 6e 20 3d 20 73 71 6c 69 74      iMin = sqlit
18a91 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67  e3_value_int(arg
18a92 76 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28 20 69  v[0]);.    if( i
18a93 4d 69 6e 3c 30 20 29 20 69 4d 69 6e 20 3d 20 30  Min<0 ) iMin = 0
18a94 3b 0a 20 20 20 20 69 66 28 20 69 4d 69 6e 3e 3d  ;.    if( iMin>=
18a95 73 69 7a 65 6f 66 28 7a 42 75 66 29 20 29 20 69  sizeof(zBuf) ) i
18a96 4d 69 6e 20 3d 20 73 69 7a 65 6f 66 28 7a 42 75  Min = sizeof(zBu
18a97 66 29 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  f)-1;.  }else{. 
18a98 20 20 20 69 4d 69 6e 20 3d 20 31 3b 0a 20 20 7d     iMin = 1;.  }
18a99 0a 20 20 69 66 28 20 61 72 67 63 3e 3d 32 20 29  .  if( argc>=2 )
18a9a 7b 0a 20 20 20 20 69 4d 61 78 20 3d 20 73 71 6c  {.    iMax = sql
18a9b 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61  ite3_value_int(a
18a9c 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 69 66 28  rgv[1]);.    if(
18a9d 20 69 4d 61 78 3c 69 4d 69 6e 20 29 20 69 4d 61   iMax<iMin ) iMa
18a9e 78 20 3d 20 69 4d 69 6e 3b 0a 20 20 20 20 69 66  x = iMin;.    if
18a9f 28 20 69 4d 61 78 3e 3d 73 69 7a 65 6f 66 28 7a  ( iMax>=sizeof(z
18aa0 42 75 66 29 20 29 20 69 4d 61 78 20 3d 20 73 69  Buf) ) iMax = si
18aa1 7a 65 6f 66 28 7a 42 75 66 29 2d 31 3b 0a 20 20  zeof(zBuf)-1;.  
18aa2 7d 65 6c 73 65 7b 0a 20 20 20 20 69 4d 61 78 20  }else{.    iMax 
18aa3 3d 20 35 30 3b 0a 20 20 7d 0a 20 20 6e 20 3d 20  = 50;.  }.  n = 
18aa4 69 4d 69 6e 3b 0a 20 20 69 66 28 20 69 4d 61 78  iMin;.  if( iMax
18aa5 3e 69 4d 69 6e 20 29 7b 0a 20 20 20 20 73 71 6c  >iMin ){.    sql
18aa6 69 74 65 33 52 61 6e 64 6f 6d 6e 65 73 73 28 73  ite3Randomness(s
18aa7 69 7a 65 6f 66 28 72 29 2c 20 26 72 29 3b 0a 20  izeof(r), &r);. 
18aa8 20 20 20 72 20 26 3d 20 30 78 37 66 66 66 66 66     r &= 0x7fffff
18aa9 66 66 3b 0a 20 20 20 20 6e 20 2b 3d 20 72 25 28  ff;.    n += r%(
18aaa 69 4d 61 78 20 2b 20 31 20 2d 20 69 4d 69 6e 29  iMax + 1 - iMin)
18aab 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
18aac 6e 3c 73 69 7a 65 6f 66 28 7a 42 75 66 29 20 29  n<sizeof(zBuf) )
18aad 3b 0a 20 20 73 71 6c 69 74 65 33 52 61 6e 64 6f  ;.  sqlite3Rando
18aae 6d 6e 65 73 73 28 6e 2c 20 7a 42 75 66 29 3b 0a  mness(n, zBuf);.
18aaf 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20    for(i=0; i<n; 
18ab0 69 2b 2b 29 7b 0a 20 20 20 20 7a 42 75 66 5b 69  i++){.    zBuf[i
18ab1 5d 20 3d 20 7a 53 72 63 5b 7a 42 75 66 5b 69 5d  ] = zSrc[zBuf[i]
18ab2 25 28 73 69 7a 65 6f 66 28 7a 53 72 63 29 2d 31  %(sizeof(zSrc)-1
18ab3 29 5d 3b 0a 20 20 7d 0a 20 20 7a 42 75 66 5b 6e  )];.  }.  zBuf[n
18ab4 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33  ] = 0;.  sqlite3
18ab5 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e  _result_text(con
18ab6 74 65 78 74 2c 20 28 63 68 61 72 2a 29 7a 42 75  text, (char*)zBu
18ab7 66 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 54 52 41  f, n, SQLITE_TRA
18ab8 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 65 6e 64 69  NSIENT);.}.#endi
18ab9 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 45 53 54  f /* SQLITE_TEST
18aba 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49   */..#ifdef SQLI
18abb 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54 68  TE_TEST./*.** Th
18abc 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20  e following two 
18abd 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61 72  SQL functions ar
18abe 65 20 75 73 65 64 20 74 6f 20 74 65 73 74 20 72  e used to test r
18abf 65 74 75 72 6e 69 6e 67 20 61 20 74 65 78 74 0a  eturning a text.
18ac0 2a 2a 20 72 65 73 75 6c 74 20 77 69 74 68 20 61  ** result with a
18ac1 20 64 65 73 74 72 75 63 74 6f 72 2e 20 46 75 6e   destructor. Fun
18ac2 63 74 69 6f 6e 20 27 74 65 73 74 5f 64 65 73 74  ction 'test_dest
18ac3 72 75 63 74 6f 72 27 20 74 61 6b 65 73 20 6f 6e  ructor' takes on
18ac4 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 61 6e  e argument.** an
18ac5 64 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 61  d returns the sa
18ac6 6d 65 20 61 72 67 75 6d 65 6e 74 20 69 6e 74 65  me argument inte
18ac7 72 70 72 65 74 65 64 20 61 73 20 54 45 58 54 2e  rpreted as TEXT.
18ac8 20 41 20 64 65 73 74 72 75 63 74 6f 72 20 69 73   A destructor is
18ac9 0a 2a 2a 20 70 61 73 73 65 64 20 77 69 74 68 20  .** passed with 
18aca 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  the sqlite3_resu
18acb 6c 74 5f 74 65 78 74 28 29 20 63 61 6c 6c 2e 0a  lt_text() call..
18acc 2a 2a 0a 2a 2a 20 53 51 4c 20 66 75 6e 63 74 69  **.** SQL functi
18acd 6f 6e 20 27 74 65 73 74 5f 64 65 73 74 72 75 63  on 'test_destruc
18ace 74 6f 72 5f 63 6f 75 6e 74 27 20 72 65 74 75 72  tor_count' retur
18acf 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ns the number of
18ad0 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 0a 2a 2a   outstanding .**
18ad1 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 6d 61 64   allocations mad
18ad2 65 20 62 79 20 27 74 65 73 74 5f 64 65 73 74 72  e by 'test_destr
18ad3 75 63 74 6f 72 27 3b 0a 2a 2a 0a 2a 2a 20 57 41  uctor';.**.** WA
18ad4 52 4e 49 4e 47 3a 20 4e 6f 74 20 74 68 72 65 61  RNING: Not threa
18ad5 64 73 61 66 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  dsafe..*/.static
18ad6 20 69 6e 74 20 74 65 73 74 5f 64 65 73 74 72 75   int test_destru
18ad7 63 74 6f 72 5f 63 6f 75 6e 74 5f 76 61 72 20 3d  ctor_count_var =
18ad8 20 30 3b 0a 73 74 61 74 69 63 20 76 6f 69 64 20   0;.static void 
18ad9 64 65 73 74 72 75 63 74 6f 72 28 76 6f 69 64 20  destructor(void 
18ada 2a 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 56 61  *p){.  char *zVa
18adb 6c 20 3d 20 28 63 68 61 72 20 2a 29 70 3b 0a 20  l = (char *)p;. 
18adc 20 61 73 73 65 72 74 28 7a 56 61 6c 29 3b 0a 20   assert(zVal);. 
18add 20 7a 56 61 6c 2d 2d 3b 0a 20 20 73 71 6c 69 74   zVal--;.  sqlit
18ade 65 46 72 65 65 28 7a 56 61 6c 29 3b 0a 20 20 74  eFree(zVal);.  t
18adf 65 73 74 5f 64 65 73 74 72 75 63 74 6f 72 5f 63  est_destructor_c
18ae0 6f 75 6e 74 5f 76 61 72 2d 2d 3b 0a 7d 0a 73 74  ount_var--;.}.st
18ae1 61 74 69 63 20 76 6f 69 64 20 74 65 73 74 5f 64  atic void test_d
18ae2 65 73 74 72 75 63 74 6f 72 28 0a 20 20 73 71 6c  estructor(.  sql
18ae3 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43  ite3_context *pC
18ae4 74 78 2c 20 0a 20 20 69 6e 74 20 6e 41 72 67 2c  tx, .  int nArg,
18ae5 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
18ae6 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 68 61   **argv.){.  cha
18ae7 72 20 2a 7a 56 61 6c 3b 0a 20 20 69 6e 74 20 6c  r *zVal;.  int l
18ae8 65 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  en;.  sqlite3 *d
18ae9 62 20 3d 20 73 71 6c 69 74 65 33 5f 75 73 65 72  b = sqlite3_user
18aea 5f 64 61 74 61 28 70 43 74 78 29 3b 0a 20 0a 20  _data(pCtx);. . 
18aeb 20 74 65 73 74 5f 64 65 73 74 72 75 63 74 6f 72   test_destructor
18aec 5f 63 6f 75 6e 74 5f 76 61 72 2b 2b 3b 0a 20 20  _count_var++;.  
18aed 61 73 73 65 72 74 28 20 6e 41 72 67 3d 3d 31 20  assert( nArg==1 
18aee 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  );.  if( sqlite3
18aef 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76  _value_type(argv
18af0 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c  [0])==SQLITE_NUL
18af1 4c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6c 65  L ) return;.  le
18af2 6e 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65  n = sqlite3Value
18af3 42 79 74 65 73 28 61 72 67 76 5b 30 5d 2c 20 45  Bytes(argv[0], E
18af4 4e 43 28 64 62 29 29 3b 20 0a 20 20 7a 56 61 6c  NC(db)); .  zVal
18af5 20 3d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28   = sqliteMalloc(
18af6 6c 65 6e 2b 33 29 3b 0a 20 20 7a 56 61 6c 5b 6c  len+3);.  zVal[l
18af7 65 6e 5d 20 3d 20 30 3b 0a 20 20 7a 56 61 6c 5b  en] = 0;.  zVal[
18af8 6c 65 6e 2d 31 5d 20 3d 20 30 3b 0a 20 20 61 73  len-1] = 0;.  as
18af9 73 65 72 74 28 20 7a 56 61 6c 20 29 3b 0a 20 20  sert( zVal );.  
18afa 7a 56 61 6c 2b 2b 3b 0a 20 20 6d 65 6d 63 70 79  zVal++;.  memcpy
18afb 28 7a 56 61 6c 2c 20 73 71 6c 69 74 65 33 56 61  (zVal, sqlite3Va
18afc 6c 75 65 54 65 78 74 28 61 72 67 76 5b 30 5d 2c  lueText(argv[0],
18afd 20 45 4e 43 28 64 62 29 29 2c 20 6c 65 6e 29 3b   ENC(db)), len);
18afe 0a 20 20 69 66 28 20 45 4e 43 28 64 62 29 3d 3d  .  if( ENC(db)==
18aff 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20  SQLITE_UTF8 ){. 
18b00 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
18b01 74 5f 74 65 78 74 28 70 43 74 78 2c 20 7a 56 61  t_text(pCtx, zVa
18b02 6c 2c 20 2d 31 2c 20 64 65 73 74 72 75 63 74 6f  l, -1, destructo
18b03 72 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  r);.#ifndef SQLI
18b04 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20  TE_OMIT_UTF16.  
18b05 7d 65 6c 73 65 20 69 66 28 20 45 4e 43 28 64 62  }else if( ENC(db
18b06 29 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  )==SQLITE_UTF16L
18b07 45 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  E ){.    sqlite3
18b08 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65  _result_text16le
18b09 28 70 43 74 78 2c 20 7a 56 61 6c 2c 20 2d 31 2c  (pCtx, zVal, -1,
18b0a 20 64 65 73 74 72 75 63 74 6f 72 29 3b 0a 20 20   destructor);.  
18b0b 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
18b0c 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36  e3_result_text16
18b0d 62 65 28 70 43 74 78 2c 20 7a 56 61 6c 2c 20 2d  be(pCtx, zVal, -
18b0e 31 2c 20 64 65 73 74 72 75 63 74 6f 72 29 3b 0a  1, destructor);.
18b0f 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
18b10 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 20  _OMIT_UTF16 */. 
18b11 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64   }.}.static void
18b12 20 74 65 73 74 5f 64 65 73 74 72 75 63 74 6f 72   test_destructor
18b13 5f 63 6f 75 6e 74 28 0a 20 20 73 71 6c 69 74 65  _count(.  sqlite
18b14 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c  3_context *pCtx,
18b15 20 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20   .  int nArg,.  
18b16 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
18b17 61 72 67 76 0a 29 7b 0a 20 20 73 71 6c 69 74 65  argv.){.  sqlite
18b18 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 70 43 74  3_result_int(pCt
18b19 78 2c 20 74 65 73 74 5f 64 65 73 74 72 75 63 74  x, test_destruct
18b1a 6f 72 5f 63 6f 75 6e 74 5f 76 61 72 29 3b 0a 7d  or_count_var);.}
18b1b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
18b1c 45 5f 54 45 53 54 20 2a 2f 0a 0a 23 69 66 64 65  E_TEST */..#ifde
18b1d 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a  f SQLITE_TEST./*
18b1e 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20 66 6f 72  .** Routines for
18b1f 20 74 65 73 74 69 6e 67 20 74 68 65 20 73 71 6c   testing the sql
18b20 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61  ite3_get_auxdata
18b21 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 73  () and sqlite3_s
18b22 65 74 5f 61 75 78 64 61 74 61 28 29 0a 2a 2a 20  et_auxdata().** 
18b23 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a  interface..**.**
18b24 20 54 68 65 20 74 65 73 74 5f 61 75 78 64 61 74   The test_auxdat
18b25 61 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  a() SQL function
18b26 20 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 67   attempts to reg
18b27 69 73 74 65 72 20 65 61 63 68 20 6f 66 20 69 74  ister each of it
18b28 73 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 61  s arguments.** a
18b29 73 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61  s auxiliary data
18b2a 2e 20 20 49 66 20 74 68 65 72 65 20 61 72 65 20  .  If there are 
18b2b 6e 6f 20 70 72 69 6f 72 20 72 65 67 69 73 74 72  no prior registr
18b2c 61 74 69 6f 6e 73 20 6f 66 20 61 75 78 20 64 61  ations of aux da
18b2d 74 61 20 66 6f 72 0a 2a 2a 20 74 68 61 74 20 61  ta for.** that a
18b2e 72 67 75 6d 65 6e 74 20 28 6d 65 61 6e 69 6e 67  rgument (meaning
18b2f 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73   the argument is
18b30 20 6e 6f 74 20 61 20 63 6f 6e 73 74 61 6e 74 20   not a constant 
18b31 6f 72 20 74 68 69 73 20 69 73 20 69 74 73 20 66  or this is its f
18b32 69 72 73 74 0a 2a 2a 20 63 61 6c 6c 29 20 74 68  irst.** call) th
18b33 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 66 6f  en the result fo
18b34 72 20 74 68 61 74 20 61 72 67 75 6d 65 6e 74 20  r that argument 
18b35 69 73 20 30 2e 20 20 49 66 20 74 68 65 72 65 20  is 0.  If there 
18b36 69 73 20 61 20 70 72 69 6f 72 0a 2a 2a 20 72 65  is a prior.** re
18b37 67 69 73 74 72 61 74 69 6f 6e 2c 20 74 68 65 20  gistration, the 
18b38 72 65 73 75 6c 74 20 66 6f 72 20 74 68 61 74 20  result for that 
18b39 61 72 67 75 6d 65 6e 74 20 69 73 20 31 2e 20 20  argument is 1.  
18b3a 54 68 65 20 6f 76 65 72 61 6c 6c 20 72 65 73 75  The overall resu
18b3b 6c 74 0a 2a 2a 20 69 73 20 74 68 65 20 69 6e 64  lt.** is the ind
18b3c 69 76 69 64 75 61 6c 20 61 72 67 75 6d 65 6e 74  ividual argument
18b3d 20 72 65 73 75 6c 74 73 20 73 65 70 61 72 61 74   results separat
18b3e 65 64 20 62 79 20 73 70 61 63 65 73 2e 0a 2a 2f  ed by spaces..*/
18b3f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65  .static void fre
18b40 65 5f 74 65 73 74 5f 61 75 78 64 61 74 61 28 76  e_test_auxdata(v
18b41 6f 69 64 20 2a 70 29 20 7b 73 71 6c 69 74 65 46  oid *p) {sqliteF
18b42 72 65 65 28 70 29 3b 7d 0a 73 74 61 74 69 63 20  ree(p);}.static 
18b43 76 6f 69 64 20 74 65 73 74 5f 61 75 78 64 61 74  void test_auxdat
18b44 61 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  a(.  sqlite3_con
18b45 74 65 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 69  text *pCtx, .  i
18b46 6e 74 20 6e 41 72 67 2c 0a 20 20 73 71 6c 69 74  nt nArg,.  sqlit
18b47 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
18b48 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 68  ){.  int i;.  ch
18b49 61 72 20 2a 7a 52 65 74 20 3d 20 73 71 6c 69 74  ar *zRet = sqlit
18b4a 65 4d 61 6c 6c 6f 63 28 6e 41 72 67 2a 32 29 3b  eMalloc(nArg*2);
18b4b 0a 20 20 69 66 28 20 21 7a 52 65 74 20 29 20 72  .  if( !zRet ) r
18b4c 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 30  eturn;.  for(i=0
18b4d 3b 20 69 3c 6e 41 72 67 3b 20 69 2b 2b 29 7b 0a  ; i<nArg; i++){.
18b4e 20 20 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a      char const *
18b4f 7a 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74  z = (char*)sqlit
18b50 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
18b51 67 76 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28 20  gv[i]);.    if( 
18b52 7a 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20  z ){.      char 
18b53 2a 7a 41 75 78 20 3d 20 73 71 6c 69 74 65 33 5f  *zAux = sqlite3_
18b54 67 65 74 5f 61 75 78 64 61 74 61 28 70 43 74 78  get_auxdata(pCtx
18b55 2c 20 69 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , i);.      if( 
18b56 7a 41 75 78 20 29 7b 0a 20 20 20 20 20 20 20 20  zAux ){.        
18b57 7a 52 65 74 5b 69 2a 32 5d 20 3d 20 27 31 27 3b  zRet[i*2] = '1';
18b58 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 74 72  .        if( str
18b59 63 6d 70 28 7a 41 75 78 2c 20 7a 29 20 29 7b 0a  cmp(zAux, z) ){.
18b5a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
18b5b 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 70  3_result_error(p
18b5c 43 74 78 2c 20 22 41 75 78 69 6c 61 72 79 20 64  Ctx, "Auxilary d
18b5d 61 74 61 20 63 6f 72 72 75 70 74 69 6f 6e 22 2c  ata corruption",
18b5e 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20   -1);.          
18b5f 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20  return;.        
18b60 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  }.      }else{. 
18b61 20 20 20 20 20 20 20 7a 52 65 74 5b 69 2a 32 5d         zRet[i*2]
18b62 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20   = '0';.        
18b63 7a 41 75 78 20 3d 20 73 71 6c 69 74 65 53 74 72  zAux = sqliteStr
18b64 44 75 70 28 7a 29 3b 0a 20 20 20 20 20 20 20 20  Dup(z);.        
18b65 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64  sqlite3_set_auxd
18b66 61 74 61 28 70 43 74 78 2c 20 69 2c 20 7a 41 75  ata(pCtx, i, zAu
18b67 78 2c 20 66 72 65 65 5f 74 65 73 74 5f 61 75 78  x, free_test_aux
18b68 64 61 74 61 29 3b 0a 20 20 20 20 20 20 7d 0a 20  data);.      }. 
18b69 20 20 20 20 20 7a 52 65 74 5b 69 2a 32 2b 31 5d       zRet[i*2+1]
18b6a 20 3d 20 27 20 27 3b 0a 20 20 20 20 7d 0a 20 20   = ' ';.    }.  
18b6b 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  }.  sqlite3_resu
18b6c 6c 74 5f 74 65 78 74 28 70 43 74 78 2c 20 7a 52  lt_text(pCtx, zR
18b6d 65 74 2c 20 32 2a 6e 41 72 67 2d 31 2c 20 66 72  et, 2*nArg-1, fr
18b6e 65 65 5f 74 65 73 74 5f 61 75 78 64 61 74 61 29  ee_test_auxdata)
18b6f 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
18b70 4c 49 54 45 5f 54 45 53 54 20 2a 2f 0a 0a 23 69  LITE_TEST */..#i
18b71 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
18b72 0a 2f 2a 0a 2a 2a 20 41 20 66 75 6e 63 74 69 6f  ./*.** A functio
18b73 6e 20 74 6f 20 74 65 73 74 20 65 72 72 6f 72 20  n to test error 
18b74 72 65 70 6f 72 74 69 6e 67 20 66 72 6f 6d 20 75  reporting from u
18b75 73 65 72 20 66 75 6e 63 74 69 6f 6e 73 2e 20 54  ser functions. T
18b76 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  his function.** 
18b77 72 65 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f  returns a copy o
18b78 66 20 69 74 27 73 20 66 69 72 73 74 20 61 72 67  f it's first arg
18b79 75 6d 65 6e 74 20 61 73 20 61 6e 20 65 72 72 6f  ument as an erro
18b7a 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  r..*/.static voi
18b7b 64 20 74 65 73 74 5f 65 72 72 6f 72 28 0a 20 20  d test_error(.  
18b7c 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
18b7d 2a 70 43 74 78 2c 20 0a 20 20 69 6e 74 20 6e 41  *pCtx, .  int nA
18b7e 72 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  rg,.  sqlite3_va
18b7f 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
18b80 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
18b81 72 72 6f 72 28 70 43 74 78 2c 20 28 63 68 61 72  rror(pCtx, (char
18b82 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
18b83 74 65 78 74 28 61 72 67 76 5b 30 5d 29 2c 20 30  text(argv[0]), 0
18b84 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  );.}.#endif /* S
18b85 51 4c 49 54 45 5f 54 45 53 54 20 2a 2f 0a 0a 2f  QLITE_TEST */../
18b86 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
18b87 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
18b88 67 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64  g structure hold
18b89 73 20 74 68 65 20 63 6f 6e 74 65 78 74 20 6f 66  s the context of
18b8a 20 61 0a 2a 2a 20 73 75 6d 28 29 20 6f 72 20 61   a.** sum() or a
18b8b 76 67 28 29 20 61 67 67 72 65 67 61 74 65 20 63  vg() aggregate c
18b8c 6f 6d 70 75 74 61 74 69 6f 6e 2e 0a 2a 2f 0a 74  omputation..*/.t
18b8d 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 75  ypedef struct Su
18b8e 6d 43 74 78 20 53 75 6d 43 74 78 3b 0a 73 74 72  mCtx SumCtx;.str
18b8f 75 63 74 20 53 75 6d 43 74 78 20 7b 0a 20 20 64  uct SumCtx {.  d
18b90 6f 75 62 6c 65 20 72 53 75 6d 3b 20 20 20 20 20  ouble rSum;     
18b91 20 2f 2a 20 46 6c 6f 61 74 69 6e 67 20 70 6f 69   /* Floating poi
18b92 6e 74 20 73 75 6d 20 2a 2f 0a 20 20 69 36 34 20  nt sum */.  i64 
18b93 69 53 75 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a  iSum;         /*
18b94 20 49 6e 74 65 67 65 72 20 73 75 6d 20 2a 2f 20   Integer sum */ 
18b95 20 20 0a 20 20 69 36 34 20 63 6e 74 3b 20 20 20    .  i64 cnt;   
18b96 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
18b97 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 73 75 6d   of elements sum
18b98 6d 65 64 20 2a 2f 0a 20 20 75 38 20 6f 76 65 72  med */.  u8 over
18b99 66 6c 6f 77 3b 20 20 20 20 20 20 2f 2a 20 54 72  flow;      /* Tr
18b9a 75 65 20 69 66 20 69 6e 74 65 67 65 72 20 6f 76  ue if integer ov
18b9b 65 72 66 6c 6f 77 20 73 65 65 6e 20 2a 2f 0a 20  erflow seen */. 
18b9c 20 75 38 20 61 70 70 72 6f 78 3b 20 20 20 20 20   u8 approx;     
18b9d 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6e 6f     /* True if no
18b9e 6e 2d 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20  n-integer value 
18b9f 77 61 73 20 69 6e 70 75 74 20 74 6f 20 74 68 65  was input to the
18ba0 20 73 75 6d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a   sum */.};../*.*
18ba1 2a 20 52 6f 75 74 69 6e 65 73 20 75 73 65 64 20  * Routines used 
18ba2 74 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 20 73  to compute the s
18ba3 75 6d 2c 20 61 76 65 72 61 67 65 2c 20 61 6e 64  um, average, and
18ba4 20 74 6f 74 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68   total..**.** Th
18ba5 65 20 53 55 4d 28 29 20 66 75 6e 63 74 69 6f 6e  e SUM() function
18ba6 20 66 6f 6c 6c 6f 77 73 20 74 68 65 20 28 62 72   follows the (br
18ba7 6f 6b 65 6e 29 20 53 51 4c 20 73 74 61 6e 64 61  oken) SQL standa
18ba8 72 64 20 77 68 69 63 68 20 6d 65 61 6e 73 0a 2a  rd which means.*
18ba9 2a 20 74 68 61 74 20 69 74 20 72 65 74 75 72 6e  * that it return
18baa 73 20 4e 55 4c 4c 20 69 66 20 69 74 20 73 75 6d  s NULL if it sum
18bab 73 20 6f 76 65 72 20 6e 6f 20 69 6e 70 75 74 73  s over no inputs
18bac 2e 20 20 54 4f 54 41 4c 20 72 65 74 75 72 6e 73  .  TOTAL returns
18bad 0a 2a 2a 20 30 2e 30 20 69 6e 20 74 68 61 74 20  .** 0.0 in that 
18bae 63 61 73 65 2e 20 20 49 6e 20 61 64 64 69 74 69  case.  In additi
18baf 6f 6e 2c 20 54 4f 54 41 4c 20 61 6c 77 61 79 73  on, TOTAL always
18bb0 20 72 65 74 75 72 6e 73 20 61 20 66 6c 6f 61 74   returns a float
18bb1 20 77 68 65 72 65 0a 2a 2a 20 53 55 4d 20 6d 69   where.** SUM mi
18bb2 67 68 74 20 72 65 74 75 72 6e 20 61 6e 20 69 6e  ght return an in
18bb3 74 65 67 65 72 20 69 66 20 69 74 20 6e 65 76 65  teger if it neve
18bb4 72 20 65 6e 63 6f 75 6e 74 65 72 73 20 61 20 66  r encounters a f
18bb5 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a  loating point.**
18bb6 20 76 61 6c 75 65 2e 20 20 54 4f 54 41 4c 20 6e   value.  TOTAL n
18bb7 65 76 65 72 20 66 61 69 6c 73 2c 20 62 75 74 20  ever fails, but 
18bb8 53 55 4d 20 6d 69 67 68 74 20 74 68 72 6f 75 67  SUM might throug
18bb9 68 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 69  h an exception i
18bba 66 0a 2a 2a 20 69 74 20 6f 76 65 72 66 6c 6f 77  f.** it overflow
18bbb 73 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f  s an integer..*/
18bbc 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 75 6d  .static void sum
18bbd 53 74 65 70 28 73 71 6c 69 74 65 33 5f 63 6f 6e  Step(sqlite3_con
18bbe 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69  text *context, i
18bbf 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33  nt argc, sqlite3
18bc0 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a  _value **argv){.
18bc1 20 20 53 75 6d 43 74 78 20 2a 70 3b 0a 20 20 69    SumCtx *p;.  i
18bc2 6e 74 20 74 79 70 65 3b 0a 20 20 61 73 73 65 72  nt type;.  asser
18bc3 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20  t( argc==1 );.  
18bc4 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72  p = sqlite3_aggr
18bc5 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f  egate_context(co
18bc6 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70  ntext, sizeof(*p
18bc7 29 29 3b 0a 20 20 74 79 70 65 20 3d 20 73 71 6c  ));.  type = sql
18bc8 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72  ite3_value_numer
18bc9 69 63 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29  ic_type(argv[0])
18bca 3b 0a 20 20 69 66 28 20 70 20 26 26 20 74 79 70  ;.  if( p && typ
18bcb 65 21 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29  e!=SQLITE_NULL )
18bcc 7b 0a 20 20 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a  {.    p->cnt++;.
18bcd 20 20 20 20 69 66 28 20 74 79 70 65 3d 3d 53 51      if( type==SQ
18bce 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 29 7b 0a  LITE_INTEGER ){.
18bcf 20 20 20 20 20 20 69 36 34 20 76 20 3d 20 73 71        i64 v = sq
18bd0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36  lite3_value_int6
18bd1 34 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20  4(argv[0]);.    
18bd2 20 20 70 2d 3e 72 53 75 6d 20 2b 3d 20 76 3b 0a    p->rSum += v;.
18bd3 20 20 20 20 20 20 69 66 28 20 28 70 2d 3e 61 70        if( (p->ap
18bd4 70 72 6f 78 7c 70 2d 3e 6f 76 65 72 66 6c 6f 77  prox|p->overflow
18bd5 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
18bd6 69 36 34 20 69 4e 65 77 53 75 6d 20 3d 20 70 2d  i64 iNewSum = p-
18bd7 3e 69 53 75 6d 20 2b 20 76 3b 0a 20 20 20 20 20  >iSum + v;.     
18bd8 20 20 20 69 6e 74 20 73 31 20 3d 20 70 2d 3e 69     int s1 = p->i
18bd9 53 75 6d 20 3e 3e 20 28 73 69 7a 65 6f 66 28 69  Sum >> (sizeof(i
18bda 36 34 29 2a 38 2d 31 29 3b 0a 20 20 20 20 20 20  64)*8-1);.      
18bdb 20 20 69 6e 74 20 73 32 20 3d 20 76 20 20 20 20    int s2 = v    
18bdc 20 20 20 3e 3e 20 28 73 69 7a 65 6f 66 28 69 36     >> (sizeof(i6
18bdd 34 29 2a 38 2d 31 29 3b 0a 20 20 20 20 20 20 20  4)*8-1);.       
18bde 20 69 6e 74 20 73 33 20 3d 20 69 4e 65 77 53 75   int s3 = iNewSu
18bdf 6d 20 3e 3e 20 28 73 69 7a 65 6f 66 28 69 36 34  m >> (sizeof(i64
18be0 29 2a 38 2d 31 29 3b 0a 20 20 20 20 20 20 20 20  )*8-1);.        
18be1 70 2d 3e 6f 76 65 72 66 6c 6f 77 20 3d 20 28 73  p->overflow = (s
18be2 31 26 73 32 26 7e 73 33 29 20 7c 20 28 7e 73 31  1&s2&~s3) | (~s1
18be3 26 7e 73 32 26 73 33 29 3b 0a 20 20 20 20 20 20  &~s2&s3);.      
18be4 20 20 70 2d 3e 69 53 75 6d 20 3d 20 69 4e 65 77    p->iSum = iNew
18be5 53 75 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  Sum;.      }.   
18be6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 2d   }else{.      p-
18be7 3e 72 53 75 6d 20 2b 3d 20 73 71 6c 69 74 65 33  >rSum += sqlite3
18be8 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72  _value_double(ar
18be9 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 70 2d  gv[0]);.      p-
18bea 3e 61 70 70 72 6f 78 20 3d 20 31 3b 0a 20 20 20  >approx = 1;.   
18beb 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20   }.  }.}.static 
18bec 76 6f 69 64 20 73 75 6d 46 69 6e 61 6c 69 7a 65  void sumFinalize
18bed 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
18bee 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 53 75   *context){.  Su
18bef 6d 43 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73  mCtx *p;.  p = s
18bf0 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65  qlite3_aggregate
18bf1 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74  _context(context
18bf2 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 20 26 26  , 0);.  if( p &&
18bf3 20 70 2d 3e 63 6e 74 3e 30 20 29 7b 0a 20 20 20   p->cnt>0 ){.   
18bf4 20 69 66 28 20 70 2d 3e 6f 76 65 72 66 6c 6f 77   if( p->overflow
18bf5 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
18bf6 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63  3_result_error(c
18bf7 6f 6e 74 65 78 74 2c 22 69 6e 74 65 67 65 72 20  ontext,"integer 
18bf8 6f 76 65 72 66 6c 6f 77 22 2c 2d 31 29 3b 0a 20  overflow",-1);. 
18bf9 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e     }else if( p->
18bfa 61 70 70 72 6f 78 20 29 7b 0a 20 20 20 20 20 20  approx ){.      
18bfb 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64  sqlite3_result_d
18bfc 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 70  ouble(context, p
18bfd 2d 3e 72 53 75 6d 29 3b 0a 20 20 20 20 7d 65 6c  ->rSum);.    }el
18bfe 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
18bff 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63  3_result_int64(c
18c00 6f 6e 74 65 78 74 2c 20 70 2d 3e 69 53 75 6d 29  ontext, p->iSum)
18c01 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74  ;.    }.  }.}.st
18c02 61 74 69 63 20 76 6f 69 64 20 61 76 67 46 69 6e  atic void avgFin
18c03 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f  alize(sqlite3_co
18c04 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b  ntext *context){
18c05 0a 20 20 53 75 6d 43 74 78 20 2a 70 3b 0a 20 20  .  SumCtx *p;.  
18c06 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72  p = sqlite3_aggr
18c07 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f  egate_context(co
18c08 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 69 66 28  ntext, 0);.  if(
18c09 20 70 20 26 26 20 70 2d 3e 63 6e 74 3e 30 20 29   p && p->cnt>0 )
18c0a 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
18c0b 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74  sult_double(cont
18c0c 65 78 74 2c 20 70 2d 3e 72 53 75 6d 2f 28 64 6f  ext, p->rSum/(do
18c0d 75 62 6c 65 29 70 2d 3e 63 6e 74 29 3b 0a 20 20  uble)p->cnt);.  
18c0e 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  }.}.static void 
18c0f 74 6f 74 61 6c 46 69 6e 61 6c 69 7a 65 28 73 71  totalFinalize(sq
18c10 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
18c11 6f 6e 74 65 78 74 29 7b 0a 20 20 53 75 6d 43 74  ontext){.  SumCt
18c12 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69  x *p;.  p = sqli
18c13 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
18c14 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 30  ntext(context, 0
18c15 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  );.  sqlite3_res
18c16 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65  ult_double(conte
18c17 78 74 2c 20 70 20 3f 20 70 2d 3e 72 53 75 6d 20  xt, p ? p->rSum 
18c18 3a 20 30 2e 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  : 0.0);.}../*.**
18c19 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   The following s
18c1a 74 72 75 63 74 75 72 65 20 6b 65 65 70 73 20 74  tructure keeps t
18c1b 72 61 63 6b 20 6f 66 20 73 74 61 74 65 20 69 6e  rack of state in
18c1c 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68  formation for th
18c1d 65 0a 2a 2a 20 63 6f 75 6e 74 28 29 20 61 67 67  e.** count() agg
18c1e 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 2e  regate function.
18c1f 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
18c20 63 74 20 43 6f 75 6e 74 43 74 78 20 43 6f 75 6e  ct CountCtx Coun
18c21 74 43 74 78 3b 0a 73 74 72 75 63 74 20 43 6f 75  tCtx;.struct Cou
18c22 6e 74 43 74 78 20 7b 0a 20 20 69 36 34 20 6e 3b  ntCtx {.  i64 n;
18c23 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69  .};../*.** Routi
18c24 6e 65 73 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  nes to implement
18c25 20 74 68 65 20 63 6f 75 6e 74 28 29 20 61 67 67   the count() agg
18c26 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 2e  regate function.
18c27 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
18c28 63 6f 75 6e 74 53 74 65 70 28 73 71 6c 69 74 65  countStep(sqlite
18c29 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
18c2a 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71  xt, int argc, sq
18c2b 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
18c2c 67 76 29 7b 0a 20 20 43 6f 75 6e 74 43 74 78 20  gv){.  CountCtx 
18c2d 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65  *p;.  p = sqlite
18c2e 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74  3_aggregate_cont
18c2f 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 73 69 7a  ext(context, siz
18c30 65 6f 66 28 2a 70 29 29 3b 0a 20 20 69 66 28 20  eof(*p));.  if( 
18c31 28 61 72 67 63 3d 3d 30 20 7c 7c 20 53 51 4c 49  (argc==0 || SQLI
18c32 54 45 5f 4e 55 4c 4c 21 3d 73 71 6c 69 74 65 33  TE_NULL!=sqlite3
18c33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76  _value_type(argv
18c34 5b 30 5d 29 29 20 26 26 20 70 20 29 7b 0a 20 20  [0])) && p ){.  
18c35 20 20 70 2d 3e 6e 2b 2b 3b 0a 20 20 7d 0a 7d 20    p->n++;.  }.} 
18c36 20 20 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63    .static void c
18c37 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 28 73 71 6c  ountFinalize(sql
18c38 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
18c39 6e 74 65 78 74 29 7b 0a 20 20 43 6f 75 6e 74 43  ntext){.  CountC
18c3a 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c  tx *p;.  p = sql
18c3b 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63  ite3_aggregate_c
18c3c 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  ontext(context, 
18c3d 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  0);.  sqlite3_re
18c3e 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65  sult_int64(conte
18c3f 78 74 2c 20 70 20 3f 20 70 2d 3e 6e 20 3a 20 30  xt, p ? p->n : 0
18c40 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74  );.}../*.** Rout
18c41 69 6e 65 73 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  ines to implemen
18c42 74 20 6d 69 6e 28 29 20 61 6e 64 20 6d 61 78 28  t min() and max(
18c43 29 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  ) aggregate func
18c44 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  tions..*/.static
18c45 20 76 6f 69 64 20 6d 69 6e 6d 61 78 53 74 65 70   void minmaxStep
18c46 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
18c47 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61   *context, int a
18c48 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
18c49 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 4d 65  ue **argv){.  Me
18c4a 6d 20 2a 70 41 72 67 20 20 3d 20 28 4d 65 6d 20  m *pArg  = (Mem 
18c4b 2a 29 61 72 67 76 5b 30 5d 3b 0a 20 20 4d 65 6d  *)argv[0];.  Mem
18c4c 20 2a 70 42 65 73 74 3b 0a 0a 20 20 69 66 28 20   *pBest;..  if( 
18c4d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
18c4e 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c  pe(argv[0])==SQL
18c4f 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72  ITE_NULL ) retur
18c50 6e 3b 0a 20 20 70 42 65 73 74 20 3d 20 28 4d 65  n;.  pBest = (Me
18c51 6d 20 2a 29 73 71 6c 69 74 65 33 5f 61 67 67 72  m *)sqlite3_aggr
18c52 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f  egate_context(co
18c53 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70  ntext, sizeof(*p
18c54 42 65 73 74 29 29 3b 0a 20 20 69 66 28 20 21 70  Best));.  if( !p
18c55 42 65 73 74 20 29 20 72 65 74 75 72 6e 3b 0a 0a  Best ) return;..
18c56 20 20 69 66 28 20 70 42 65 73 74 2d 3e 66 6c 61    if( pBest->fla
18c57 67 73 20 29 7b 0a 20 20 20 20 69 6e 74 20 6d 61  gs ){.    int ma
18c58 78 3b 0a 20 20 20 20 69 6e 74 20 63 6d 70 3b 0a  x;.    int cmp;.
18c59 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f      CollSeq *pCo
18c5a 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 65 74 46  ll = sqlite3GetF
18c5b 75 6e 63 43 6f 6c 6c 53 65 71 28 63 6f 6e 74 65  uncCollSeq(conte
18c5c 78 74 29 3b 0a 20 20 20 20 2f 2a 20 54 68 69 73  xt);.    /* This
18c5d 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 69   step function i
18c5e 73 20 75 73 65 64 20 66 6f 72 20 62 6f 74 68 20  s used for both 
18c5f 74 68 65 20 6d 69 6e 28 29 20 61 6e 64 20 6d 61  the min() and ma
18c60 78 28 29 20 61 67 67 72 65 67 61 74 65 73 2c 0a  x() aggregates,.
18c61 20 20 20 20 2a 2a 20 74 68 65 20 6f 6e 6c 79 20      ** the only 
18c62 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65  difference betwe
18c63 65 6e 20 74 68 65 20 74 77 6f 20 62 65 69 6e 67  en the two being
18c64 20 74 68 61 74 20 74 68 65 20 73 65 6e 73 65 20   that the sense 
18c65 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f  of the.    ** co
18c66 6d 70 61 72 69 73 6f 6e 20 69 73 20 69 6e 76 65  mparison is inve
18c67 72 74 65 64 2e 20 46 6f 72 20 74 68 65 20 6d 61  rted. For the ma
18c68 78 28 29 20 61 67 67 72 65 67 61 74 65 2c 20 74  x() aggregate, t
18c69 68 65 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65  he.    ** sqlite
18c6a 33 5f 75 73 65 72 5f 64 61 74 61 28 29 20 66 75  3_user_data() fu
18c6b 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 28  nction returns (
18c6c 76 6f 69 64 20 2a 29 2d 31 2e 20 46 6f 72 20 6d  void *)-1. For m
18c6d 69 6e 28 29 20 69 74 0a 20 20 20 20 2a 2a 20 72  in() it.    ** r
18c6e 65 74 75 72 6e 73 20 28 76 6f 69 64 20 2a 29 64  eturns (void *)d
18c6f 62 2c 20 77 68 65 72 65 20 64 62 20 69 73 20 74  b, where db is t
18c70 68 65 20 73 71 6c 69 74 65 33 2a 20 64 61 74 61  he sqlite3* data
18c71 62 61 73 65 20 70 6f 69 6e 74 65 72 2e 0a 20 20  base pointer..  
18c72 20 20 2a 2a 20 54 68 65 72 65 66 6f 72 65 20 74    ** Therefore t
18c73 68 65 20 6e 65 78 74 20 73 74 61 74 65 6d 65 6e  he next statemen
18c74 74 20 73 65 74 73 20 76 61 72 69 61 62 6c 65 20  t sets variable 
18c75 27 6d 61 78 27 20 74 6f 20 31 20 66 6f 72 20 74  'max' to 1 for t
18c76 68 65 20 6d 61 78 28 29 0a 20 20 20 20 2a 2a 20  he max().    ** 
18c77 61 67 67 72 65 67 61 74 65 2c 20 6f 72 20 30 20  aggregate, or 0 
18c78 66 6f 72 20 6d 69 6e 28 29 2e 0a 20 20 20 20 2a  for min()..    *
18c79 2f 0a 20 20 20 20 6d 61 78 20 3d 20 73 71 6c 69  /.    max = sqli
18c7a 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 63 6f  te3_user_data(co
18c7b 6e 74 65 78 74 29 21 3d 30 3b 0a 20 20 20 20 63  ntext)!=0;.    c
18c7c 6d 70 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 43  mp = sqlite3MemC
18c7d 6f 6d 70 61 72 65 28 70 42 65 73 74 2c 20 70 41  ompare(pBest, pA
18c7e 72 67 2c 20 70 43 6f 6c 6c 29 3b 0a 20 20 20 20  rg, pColl);.    
18c7f 69 66 28 20 28 6d 61 78 20 26 26 20 63 6d 70 3c  if( (max && cmp<
18c80 30 29 20 7c 7c 20 28 21 6d 61 78 20 26 26 20 63  0) || (!max && c
18c81 6d 70 3e 30 29 20 29 7b 0a 20 20 20 20 20 20 73  mp>0) ){.      s
18c82 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70  qlite3VdbeMemCop
18c83 79 28 70 42 65 73 74 2c 20 70 41 72 67 29 3b 0a  y(pBest, pArg);.
18c84 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
18c85 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
18c86 6d 43 6f 70 79 28 70 42 65 73 74 2c 20 70 41 72  mCopy(pBest, pAr
18c87 67 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63  g);.  }.}.static
18c88 20 76 6f 69 64 20 6d 69 6e 4d 61 78 46 69 6e 61   void minMaxFina
18c89 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e  lize(sqlite3_con
18c8a 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a  text *context){.
18c8b 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
18c8c 2a 70 52 65 73 3b 0a 20 20 70 52 65 73 20 3d 20  *pRes;.  pRes = 
18c8d 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a  (sqlite3_value *
18c8e 29 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61  )sqlite3_aggrega
18c8f 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65  te_context(conte
18c90 78 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 52  xt, 0);.  if( pR
18c91 65 73 20 29 7b 0a 20 20 20 20 69 66 28 20 70 52  es ){.    if( pR
18c92 65 73 2d 3e 66 6c 61 67 73 20 29 7b 0a 20 20 20  es->flags ){.   
18c93 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
18c94 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78 74 2c  t_value(context,
18c95 20 70 52 65 73 29 3b 0a 20 20 20 20 7d 0a 20 20   pRes);.    }.  
18c96 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
18c97 52 65 6c 65 61 73 65 28 70 52 65 73 29 3b 0a 20  Release(pRes);. 
18c98 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69   }.}.../*.** Thi
18c99 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73  s function regis
18c9a 74 65 72 65 64 20 61 6c 6c 20 6f 66 20 74 68 65  tered all of the
18c9b 20 61 62 6f 76 65 20 43 20 66 75 6e 63 74 69 6f   above C functio
18c9c 6e 73 20 61 73 20 53 51 4c 0a 2a 2a 20 66 75 6e  ns as SQL.** fun
18c9d 63 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 73 68  ctions.  This sh
18c9e 6f 75 6c 64 20 62 65 20 74 68 65 20 6f 6e 6c 79  ould be the only
18c9f 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73   routine in this
18ca0 20 66 69 6c 65 20 77 69 74 68 0a 2a 2a 20 65 78   file with.** ex
18ca1 74 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a  ternal linkage..
18ca2 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
18ca3 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65  E void sqlite3Re
18ca4 67 69 73 74 65 72 42 75 69 6c 74 69 6e 46 75 6e  gisterBuiltinFun
18ca5 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a  ctions(sqlite3 *
18ca6 64 62 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f  db){.  static co
18ca7 6e 73 74 20 73 74 72 75 63 74 20 7b 0a 20 20 20  nst struct {.   
18ca8 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20    char *zName;. 
18ca9 20 20 20 20 73 69 67 6e 65 64 20 63 68 61 72 20      signed char 
18caa 6e 41 72 67 3b 0a 20 20 20 20 20 75 38 20 61 72  nArg;.     u8 ar
18cab 67 54 79 70 65 3b 20 20 20 20 20 20 20 20 20 20  gType;          
18cac 20 2f 2a 20 66 66 3a 20 64 62 20 20 20 31 3a 20   /* ff: db   1: 
18cad 30 2c 20 32 3a 20 31 2c 20 33 3a 20 32 2c 2e 2e  0, 2: 1, 3: 2,..
18cae 2e 20 20 4e 3a 20 20 4e 2d 31 2e 20 2a 2f 0a 20  .  N:  N-1. */. 
18caf 20 20 20 20 75 38 20 65 54 65 78 74 52 65 70 3b      u8 eTextRep;
18cb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 3a 20            /* 1: 
18cb1 55 54 46 2d 31 36 2e 20 20 30 3a 20 55 54 46 2d  UTF-16.  0: UTF-
18cb2 38 20 2a 2f 0a 20 20 20 20 20 75 38 20 6e 65 65  8 */.     u8 nee
18cb3 64 43 6f 6c 6c 53 65 71 3b 0a 20 20 20 20 20 76  dCollSeq;.     v
18cb4 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c  oid (*xFunc)(sql
18cb5 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
18cb6 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  t,sqlite3_value 
18cb7 2a 2a 29 3b 0a 20 20 7d 20 61 46 75 6e 63 73 5b  **);.  } aFuncs[
18cb8 5d 20 3d 20 7b 0a 20 20 20 20 7b 20 22 6d 69 6e  ] = {.    { "min
18cb9 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ",              
18cba 20 2d 31 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55   -1, 0, SQLITE_U
18cbb 54 46 38 2c 20 20 20 20 31 2c 20 6d 69 6e 6d 61  TF8,    1, minma
18cbc 78 46 75 6e 63 20 7d 2c 0a 20 20 20 20 7b 20 22  xFunc },.    { "
18cbd 6d 69 6e 22 2c 20 20 20 20 20 20 20 20 20 20 20  min",           
18cbe 20 20 20 20 20 30 2c 20 30 2c 20 53 51 4c 49 54       0, 0, SQLIT
18cbf 45 5f 55 54 46 38 2c 20 20 20 20 31 2c 20 30 20  E_UTF8,    1, 0 
18cc0 20 20 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 20           },.    
18cc1 7b 20 22 6d 61 78 22 2c 20 20 20 20 20 20 20 20  { "max",        
18cc2 20 20 20 20 20 20 20 2d 31 2c 20 31 2c 20 53 51         -1, 1, SQ
18cc3 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20 31 2c  LITE_UTF8,    1,
18cc4 20 6d 69 6e 6d 61 78 46 75 6e 63 20 7d 2c 0a 20   minmaxFunc },. 
18cc5 20 20 20 7b 20 22 6d 61 78 22 2c 20 20 20 20 20     { "max",     
18cc6 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 31 2c             0, 1,
18cc7 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20   SQLITE_UTF8,   
18cc8 20 31 2c 20 30 20 20 20 20 20 20 20 20 20 20 7d   1, 0          }
18cc9 2c 0a 20 20 20 20 7b 20 22 74 79 70 65 6f 66 22  ,.    { "typeof"
18cca 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c  ,             1,
18ccb 20 30 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   0, SQLITE_UTF8,
18ccc 20 20 20 20 30 2c 20 74 79 70 65 6f 66 46 75 6e      0, typeofFun
18ccd 63 20 7d 2c 0a 20 20 20 20 7b 20 22 6c 65 6e 67  c },.    { "leng
18cce 74 68 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  th",            
18ccf 20 31 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55 54   1, 0, SQLITE_UT
18cd0 46 38 2c 20 20 20 20 30 2c 20 6c 65 6e 67 74 68  F8,    0, length
18cd1 46 75 6e 63 20 7d 2c 0a 20 20 20 20 7b 20 22 73  Func },.    { "s
18cd2 75 62 73 74 72 22 2c 20 20 20 20 20 20 20 20 20  ubstr",         
18cd3 20 20 20 20 33 2c 20 30 2c 20 53 51 4c 49 54 45      3, 0, SQLITE
18cd4 5f 55 54 46 38 2c 20 20 20 20 30 2c 20 73 75 62  _UTF8,    0, sub
18cd5 73 74 72 46 75 6e 63 20 7d 2c 0a 20 20 20 20 7b  strFunc },.    {
18cd6 20 22 61 62 73 22 2c 20 20 20 20 20 20 20 20 20   "abs",         
18cd7 20 20 20 20 20 20 20 31 2c 20 30 2c 20 53 51 4c         1, 0, SQL
18cd8 49 54 45 5f 55 54 46 38 2c 20 20 20 20 30 2c 20  ITE_UTF8,    0, 
18cd9 61 62 73 46 75 6e 63 20 20 20 20 7d 2c 0a 20 20  absFunc    },.  
18cda 20 20 7b 20 22 72 6f 75 6e 64 22 2c 20 20 20 20    { "round",    
18cdb 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20            1, 0, 
18cdc 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20  SQLITE_UTF8,    
18cdd 30 2c 20 72 6f 75 6e 64 46 75 6e 63 20 20 7d 2c  0, roundFunc  },
18cde 0a 20 20 20 20 7b 20 22 72 6f 75 6e 64 22 2c 20  .    { "round", 
18cdf 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2c 20               2, 
18ce0 30 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  0, SQLITE_UTF8, 
18ce1 20 20 20 30 2c 20 72 6f 75 6e 64 46 75 6e 63 20     0, roundFunc 
18ce2 20 7d 2c 0a 20 20 20 20 7b 20 22 75 70 70 65 72   },.    { "upper
18ce3 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ",              
18ce4 31 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55 54 46  1, 0, SQLITE_UTF
18ce5 38 2c 20 20 20 20 30 2c 20 75 70 70 65 72 46 75  8,    0, upperFu
18ce6 6e 63 20 20 7d 2c 0a 20 20 20 20 7b 20 22 6c 6f  nc  },.    { "lo
18ce7 77 65 72 22 2c 20 20 20 20 20 20 20 20 20 20 20  wer",           
18ce8 20 20 20 31 2c 20 30 2c 20 53 51 4c 49 54 45 5f     1, 0, SQLITE_
18ce9 55 54 46 38 2c 20 20 20 20 30 2c 20 6c 6f 77 65  UTF8,    0, lowe
18cea 72 46 75 6e 63 20 20 7d 2c 0a 20 20 20 20 7b 20  rFunc  },.    { 
18ceb 22 63 6f 61 6c 65 73 63 65 22 2c 20 20 20 20 20  "coalesce",     
18cec 20 20 20 20 20 2d 31 2c 20 30 2c 20 53 51 4c 49       -1, 0, SQLI
18ced 54 45 5f 55 54 46 38 2c 20 20 20 20 30 2c 20 69  TE_UTF8,    0, i
18cee 66 6e 75 6c 6c 46 75 6e 63 20 7d 2c 0a 20 20 20  fnullFunc },.   
18cef 20 7b 20 22 63 6f 61 6c 65 73 63 65 22 2c 20 20   { "coalesce",  
18cf0 20 20 20 20 20 20 20 20 20 30 2c 20 30 2c 20 53           0, 0, S
18cf1 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20 30  QLITE_UTF8,    0
18cf2 2c 20 30 20 20 20 20 20 20 20 20 20 20 7d 2c 0a  , 0          },.
18cf3 20 20 20 20 7b 20 22 63 6f 61 6c 65 73 63 65 22      { "coalesce"
18cf4 2c 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30  ,           1, 0
18cf5 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20  , SQLITE_UTF8,  
18cf6 20 20 30 2c 20 30 20 20 20 20 20 20 20 20 20 20    0, 0          
18cf7 7d 2c 0a 20 20 20 20 7b 20 22 68 65 78 22 2c 20  },.    { "hex", 
18cf8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
18cf9 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  , 0, SQLITE_UTF8
18cfa 2c 20 20 20 20 30 2c 20 68 65 78 46 75 6e 63 20  ,    0, hexFunc 
18cfb 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 69 66 6e     },.    { "ifn
18cfc 75 6c 6c 22 2c 20 20 20 20 20 20 20 20 20 20 20  ull",           
18cfd 20 20 32 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55    2, 0, SQLITE_U
18cfe 54 46 38 2c 20 20 20 20 31 2c 20 69 66 6e 75 6c  TF8,    1, ifnul
18cff 6c 46 75 6e 63 20 7d 2c 0a 20 20 20 20 7b 20 22  lFunc },.    { "
18d00 72 61 6e 64 6f 6d 22 2c 20 20 20 20 20 20 20 20  random",        
18d01 20 20 20 20 2d 31 2c 20 30 2c 20 53 51 4c 49 54      -1, 0, SQLIT
18d02 45 5f 55 54 46 38 2c 20 20 20 20 30 2c 20 72 61  E_UTF8,    0, ra
18d03 6e 64 6f 6d 46 75 6e 63 20 7d 2c 0a 20 20 20 20  ndomFunc },.    
18d04 7b 20 22 72 61 6e 64 6f 6d 62 6c 6f 62 22 2c 20  { "randomblob", 
18d05 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 53 51          1, 0, SQ
18d06 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20 30 2c  LITE_UTF8,    0,
18d07 20 72 61 6e 64 6f 6d 42 6c 6f 62 20 7d 2c 0a 20   randomBlob },. 
18d08 20 20 20 7b 20 22 6e 75 6c 6c 69 66 22 2c 20 20     { "nullif",  
18d09 20 20 20 20 20 20 20 20 20 20 20 32 2c 20 30 2c             2, 0,
18d0a 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20   SQLITE_UTF8,   
18d0b 20 31 2c 20 6e 75 6c 6c 69 66 46 75 6e 63 20 7d   1, nullifFunc }
18d0c 2c 0a 20 20 20 20 7b 20 22 73 71 6c 69 74 65 5f  ,.    { "sqlite_
18d0d 76 65 72 73 69 6f 6e 22 2c 20 20 20 20 20 30 2c  version",     0,
18d0e 20 30 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   0, SQLITE_UTF8,
18d0f 20 20 20 20 30 2c 20 76 65 72 73 69 6f 6e 46 75      0, versionFu
18d10 6e 63 7d 2c 0a 20 20 20 20 7b 20 22 71 75 6f 74  nc},.    { "quot
18d11 65 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  e",             
18d12 20 31 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55 54   1, 0, SQLITE_UT
18d13 46 38 2c 20 20 20 20 30 2c 20 71 75 6f 74 65 46  F8,    0, quoteF
18d14 75 6e 63 20 20 7d 2c 0a 20 20 20 20 7b 20 22 6c  unc  },.    { "l
18d15 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64  ast_insert_rowid
18d16 22 2c 20 20 30 2c 20 30 78 66 66 2c 20 53 51 4c  ",  0, 0xff, SQL
18d17 49 54 45 5f 55 54 46 38 2c 20 30 2c 20 6c 61 73  ITE_UTF8, 0, las
18d18 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 20 7d  t_insert_rowid }
18d19 2c 0a 20 20 20 20 7b 20 22 63 68 61 6e 67 65 73  ,.    { "changes
18d1a 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 30 2c  ",            0,
18d1b 20 30 78 66 66 2c 20 53 51 4c 49 54 45 5f 55 54   0xff, SQLITE_UT
18d1c 46 38 2c 20 30 2c 20 63 68 61 6e 67 65 73 20 20  F8, 0, changes  
18d1d 20 20 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 20           },.    
18d1e 7b 20 22 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  { "total_changes
18d1f 22 2c 20 20 20 20 20 20 30 2c 20 30 78 66 66 2c  ",      0, 0xff,
18d20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c   SQLITE_UTF8, 0,
18d21 20 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 20 20   total_changes  
18d22 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 72 65 70     },.    { "rep
18d23 6c 61 63 65 22 2c 20 20 20 20 20 20 20 20 20 20  lace",          
18d24 20 20 33 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55    3, 0, SQLITE_U
18d25 54 46 38 2c 20 20 20 20 30 2c 20 72 65 70 6c 61  TF8,    0, repla
18d26 63 65 46 75 6e 63 20 20 20 20 20 20 20 7d 2c 0a  ceFunc       },.
18d27 20 20 20 20 7b 20 22 6c 74 72 69 6d 22 2c 20 20      { "ltrim",  
18d28 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 31              1, 1
18d29 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20  , SQLITE_UTF8,  
18d2a 20 20 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 20    0, trimFunc   
18d2b 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20         },.    { 
18d2c 22 6c 74 72 69 6d 22 2c 20 20 20 20 20 20 20 20  "ltrim",        
18d2d 20 20 20 20 20 20 32 2c 20 31 2c 20 53 51 4c 49        2, 1, SQLI
18d2e 54 45 5f 55 54 46 38 2c 20 20 20 20 30 2c 20 74  TE_UTF8,    0, t
18d2f 72 69 6d 46 75 6e 63 20 20 20 20 20 20 20 20 20  rimFunc         
18d30 20 7d 2c 0a 20 20 20 20 7b 20 22 72 74 72 69 6d   },.    { "rtrim
18d31 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ",              
18d32 31 2c 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46  1, 2, SQLITE_UTF
18d33 38 2c 20 20 20 20 30 2c 20 74 72 69 6d 46 75 6e  8,    0, trimFun
18d34 63 20 20 20 20 20 20 20 20 20 20 7d 2c 0a 20 20  c          },.  
18d35 20 20 7b 20 22 72 74 72 69 6d 22 2c 20 20 20 20    { "rtrim",    
18d36 20 20 20 20 20 20 20 20 20 20 32 2c 20 32 2c 20            2, 2, 
18d37 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20  SQLITE_UTF8,    
18d38 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 20 20 20  0, trimFunc     
18d39 20 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 74       },.    { "t
18d3a 72 69 6d 22 2c 20 20 20 20 20 20 20 20 20 20 20  rim",           
18d3b 20 20 20 20 31 2c 20 33 2c 20 53 51 4c 49 54 45      1, 3, SQLITE
18d3c 5f 55 54 46 38 2c 20 20 20 20 30 2c 20 74 72 69  _UTF8,    0, tri
18d3d 6d 46 75 6e 63 20 20 20 20 20 20 20 20 20 20 7d  mFunc          }
18d3e 2c 0a 20 20 20 20 7b 20 22 74 72 69 6d 22 2c 20  ,.    { "trim", 
18d3f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2c                2,
18d40 20 33 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   3, SQLITE_UTF8,
18d41 20 20 20 20 30 2c 20 74 72 69 6d 46 75 6e 63 20      0, trimFunc 
18d42 20 20 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 20           },.    
18d43 7b 20 22 7a 65 72 6f 62 6c 6f 62 22 2c 20 20 20  { "zeroblob",   
18d44 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 53 51          1, 0, SQ
18d45 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20 30 2c  LITE_UTF8,    0,
18d46 20 7a 65 72 6f 62 6c 6f 62 46 75 6e 63 20 20 20   zeroblobFunc   
18d47 20 20 20 7d 2c 0a 23 69 66 64 65 66 20 53 51 4c     },.#ifdef SQL
18d48 49 54 45 5f 53 4f 55 4e 44 45 58 0a 20 20 20 20  ITE_SOUNDEX.    
18d49 7b 20 22 73 6f 75 6e 64 65 78 22 2c 20 20 20 20  { "soundex",    
18d4a 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 53 51          1, 0, SQ
18d4b 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20 30 2c  LITE_UTF8,    0,
18d4c 20 73 6f 75 6e 64 65 78 46 75 6e 63 7d 2c 0a 23   soundexFunc},.#
18d4d 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51  endif.#ifndef SQ
18d4e 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45  LITE_OMIT_LOAD_E
18d4f 58 54 45 4e 53 49 4f 4e 0a 20 20 20 20 7b 20 22  XTENSION.    { "
18d50 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 22 2c  load_extension",
18d51 20 20 20 20 20 31 2c 20 30 78 66 66 2c 20 53 51       1, 0xff, SQ
18d52 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c 20 6c 6f  LITE_UTF8, 0, lo
18d53 61 64 45 78 74 20 7d 2c 0a 20 20 20 20 7b 20 22  adExt },.    { "
18d54 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 22 2c  load_extension",
18d55 20 20 20 20 20 32 2c 20 30 78 66 66 2c 20 53 51       2, 0xff, SQ
18d56 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c 20 6c 6f  LITE_UTF8, 0, lo
18d57 61 64 45 78 74 20 7d 2c 0a 23 65 6e 64 69 66 0a  adExt },.#endif.
18d58 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
18d59 53 54 0a 20 20 20 20 7b 20 22 72 61 6e 64 73 74  ST.    { "randst
18d5a 72 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  r",             
18d5b 20 20 32 2c 20 30 2c 20 20 20 20 53 51 4c 49 54    2, 0,    SQLIT
18d5c 45 5f 55 54 46 38 2c 20 30 2c 20 72 61 6e 64 53  E_UTF8, 0, randS
18d5d 74 72 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22  tr    },.    { "
18d5e 74 65 73 74 5f 64 65 73 74 72 75 63 74 6f 72 22  test_destructor"
18d5f 2c 20 20 20 20 20 20 20 31 2c 20 30 78 66 66 2c  ,       1, 0xff,
18d60 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c   SQLITE_UTF8, 0,
18d61 20 74 65 73 74 5f 64 65 73 74 72 75 63 74 6f 72   test_destructor
18d62 7d 2c 0a 20 20 20 20 7b 20 22 74 65 73 74 5f 64  },.    { "test_d
18d63 65 73 74 72 75 63 74 6f 72 5f 63 6f 75 6e 74 22  estructor_count"
18d64 2c 20 30 2c 20 30 2c 20 20 20 20 53 51 4c 49 54  , 0, 0,    SQLIT
18d65 45 5f 55 54 46 38 2c 20 30 2c 20 74 65 73 74 5f  E_UTF8, 0, test_
18d66 64 65 73 74 72 75 63 74 6f 72 5f 63 6f 75 6e 74  destructor_count
18d67 7d 2c 0a 20 20 20 20 7b 20 22 74 65 73 74 5f 61  },.    { "test_a
18d68 75 78 64 61 74 61 22 2c 20 20 20 20 20 20 20 20  uxdata",        
18d69 20 2d 31 2c 20 30 2c 20 20 20 20 53 51 4c 49 54   -1, 0,    SQLIT
18d6a 45 5f 55 54 46 38 2c 20 30 2c 20 74 65 73 74 5f  E_UTF8, 0, test_
18d6b 61 75 78 64 61 74 61 7d 2c 0a 20 20 20 20 7b 20  auxdata},.    { 
18d6c 22 74 65 73 74 5f 65 72 72 6f 72 22 2c 20 20 20  "test_error",   
18d6d 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 20           1, 0,  
18d6e 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30    SQLITE_UTF8, 0
18d6f 2c 20 74 65 73 74 5f 65 72 72 6f 72 7d 2c 0a 23  , test_error},.#
18d70 65 6e 64 69 66 0a 20 20 7d 3b 0a 20 20 73 74 61  endif.  };.  sta
18d71 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74  tic const struct
18d72 20 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e 61   {.    char *zNa
18d73 6d 65 3b 0a 20 20 20 20 73 69 67 6e 65 64 20 63  me;.    signed c
18d74 68 61 72 20 6e 41 72 67 3b 0a 20 20 20 20 75 38  har nArg;.    u8
18d75 20 61 72 67 54 79 70 65 3b 0a 20 20 20 20 75 38   argType;.    u8
18d76 20 6e 65 65 64 43 6f 6c 6c 53 65 71 3b 0a 20 20   needCollSeq;.  
18d77 20 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28    void (*xStep)(
18d78 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
18d79 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
18d7a 75 65 2a 2a 29 3b 0a 20 20 20 20 76 6f 69 64 20  ue**);.    void 
18d7b 28 2a 78 46 69 6e 61 6c 69 7a 65 29 28 73 71 6c  (*xFinalize)(sql
18d7c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a  ite3_context*);.
18d7d 20 20 7d 20 61 41 67 67 73 5b 5d 20 3d 20 7b 0a    } aAggs[] = {.
18d7e 20 20 20 20 7b 20 22 6d 69 6e 22 2c 20 20 20 20      { "min",    
18d7f 31 2c 20 30 2c 20 31 2c 20 6d 69 6e 6d 61 78 53  1, 0, 1, minmaxS
18d80 74 65 70 2c 20 20 20 6d 69 6e 4d 61 78 46 69 6e  tep,   minMaxFin
18d81 61 6c 69 7a 65 20 7d 2c 0a 20 20 20 20 7b 20 22  alize },.    { "
18d82 6d 61 78 22 2c 20 20 20 20 31 2c 20 31 2c 20 31  max",    1, 1, 1
18d83 2c 20 6d 69 6e 6d 61 78 53 74 65 70 2c 20 20 20  , minmaxStep,   
18d84 6d 69 6e 4d 61 78 46 69 6e 61 6c 69 7a 65 20 7d  minMaxFinalize }
18d85 2c 0a 20 20 20 20 7b 20 22 73 75 6d 22 2c 20 20  ,.    { "sum",  
18d86 20 20 31 2c 20 30 2c 20 30 2c 20 73 75 6d 53 74    1, 0, 0, sumSt
18d87 65 70 2c 20 20 20 20 20 20 73 75 6d 46 69 6e 61  ep,      sumFina
18d88 6c 69 7a 65 20 20 20 20 7d 2c 0a 20 20 20 20 7b  lize    },.    {
18d89 20 22 74 6f 74 61 6c 22 2c 20 20 31 2c 20 30 2c   "total",  1, 0,
18d8a 20 30 2c 20 73 75 6d 53 74 65 70 2c 20 20 20 20   0, sumStep,    
18d8b 20 20 74 6f 74 61 6c 46 69 6e 61 6c 69 7a 65 20    totalFinalize 
18d8c 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 61 76 67     },.    { "avg
18d8d 22 2c 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 73  ",    1, 0, 0, s
18d8e 75 6d 53 74 65 70 2c 20 20 20 20 20 20 61 76 67  umStep,      avg
18d8f 46 69 6e 61 6c 69 7a 65 20 20 20 20 7d 2c 0a 20  Finalize    },. 
18d90 20 20 20 7b 20 22 63 6f 75 6e 74 22 2c 20 20 30     { "count",  0
18d91 2c 20 30 2c 20 30 2c 20 63 6f 75 6e 74 53 74 65  , 0, 0, countSte
18d92 70 2c 20 20 20 20 63 6f 75 6e 74 46 69 6e 61 6c  p,    countFinal
18d93 69 7a 65 20 20 7d 2c 0a 20 20 20 20 7b 20 22 63  ize  },.    { "c
18d94 6f 75 6e 74 22 2c 20 20 31 2c 20 30 2c 20 30 2c  ount",  1, 0, 0,
18d95 20 63 6f 75 6e 74 53 74 65 70 2c 20 20 20 20 63   countStep,    c
18d96 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 20 20 7d 2c  ountFinalize  },
18d97 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a  .  };.  int i;..
18d98 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a    for(i=0; i<siz
18d99 65 6f 66 28 61 46 75 6e 63 73 29 2f 73 69 7a 65  eof(aFuncs)/size
18d9a 6f 66 28 61 46 75 6e 63 73 5b 30 5d 29 3b 20 69  of(aFuncs[0]); i
18d9b 2b 2b 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 70  ++){.    void *p
18d9c 41 72 67 3b 0a 20 20 20 20 75 38 20 61 72 67 54  Arg;.    u8 argT
18d9d 79 70 65 20 3d 20 61 46 75 6e 63 73 5b 69 5d 2e  ype = aFuncs[i].
18d9e 61 72 67 54 79 70 65 3b 0a 20 20 20 20 69 66 28  argType;.    if(
18d9f 20 61 72 67 54 79 70 65 3d 3d 30 78 66 66 20 29   argType==0xff )
18da0 7b 0a 20 20 20 20 20 20 70 41 72 67 20 3d 20 64  {.      pArg = d
18da1 62 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  b;.    }else{.  
18da2 20 20 20 20 70 41 72 67 20 3d 20 28 76 6f 69 64      pArg = (void
18da3 2a 29 28 69 6e 74 29 61 72 67 54 79 70 65 3b 0a  *)(int)argType;.
18da4 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
18da5 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20  3CreateFunc(db, 
18da6 61 46 75 6e 63 73 5b 69 5d 2e 7a 4e 61 6d 65 2c  aFuncs[i].zName,
18da7 20 61 46 75 6e 63 73 5b 69 5d 2e 6e 41 72 67 2c   aFuncs[i].nArg,
18da8 0a 20 20 20 20 20 20 20 20 61 46 75 6e 63 73 5b  .        aFuncs[
18da9 69 5d 2e 65 54 65 78 74 52 65 70 2c 20 70 41 72  i].eTextRep, pAr
18daa 67 2c 20 61 46 75 6e 63 73 5b 69 5d 2e 78 46 75  g, aFuncs[i].xFu
18dab 6e 63 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69  nc, 0, 0);.    i
18dac 66 28 20 61 46 75 6e 63 73 5b 69 5d 2e 6e 65 65  f( aFuncs[i].nee
18dad 64 43 6f 6c 6c 53 65 71 20 29 7b 0a 20 20 20 20  dCollSeq ){.    
18dae 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e 63    FuncDef *pFunc
18daf 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75   = sqlite3FindFu
18db0 6e 63 74 69 6f 6e 28 64 62 2c 20 61 46 75 6e 63  nction(db, aFunc
18db1 73 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 0a 20 20 20  s[i].zName, .   
18db2 20 20 20 20 20 20 20 73 74 72 6c 65 6e 28 61 46         strlen(aF
18db3 75 6e 63 73 5b 69 5d 2e 7a 4e 61 6d 65 29 2c 20  uncs[i].zName), 
18db4 61 46 75 6e 63 73 5b 69 5d 2e 6e 41 72 67 2c 20  aFuncs[i].nArg, 
18db5 61 46 75 6e 63 73 5b 69 5d 2e 65 54 65 78 74 52  aFuncs[i].eTextR
18db6 65 70 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66  ep, 0);.      if
18db7 28 20 70 46 75 6e 63 20 26 26 20 61 46 75 6e 63  ( pFunc && aFunc
18db8 73 5b 69 5d 2e 6e 65 65 64 43 6f 6c 6c 53 65 71  s[i].needCollSeq
18db9 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 75 6e   ){.        pFun
18dba 63 2d 3e 6e 65 65 64 43 6f 6c 6c 53 65 71 20 3d  c->needCollSeq =
18dbb 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   1;.      }.    
18dbc 7d 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51  }.  }.#ifndef SQ
18dbd 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54  LITE_OMIT_ALTERT
18dbe 41 42 4c 45 0a 20 20 73 71 6c 69 74 65 33 41 6c  ABLE.  sqlite3Al
18dbf 74 65 72 46 75 6e 63 74 69 6f 6e 73 28 64 62 29  terFunctions(db)
18dc0 3b 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66  ;.#endif.#ifndef
18dc1 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 52   SQLITE_OMIT_PAR
18dc2 53 45 52 0a 20 20 73 71 6c 69 74 65 33 41 74 74  SER.  sqlite3Att
18dc3 61 63 68 46 75 6e 63 74 69 6f 6e 73 28 64 62 29  achFunctions(db)
18dc4 3b 0a 23 65 6e 64 69 66 0a 20 20 66 6f 72 28 69  ;.#endif.  for(i
18dc5 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 41 67  =0; i<sizeof(aAg
18dc6 67 73 29 2f 73 69 7a 65 6f 66 28 61 41 67 67 73  gs)/sizeof(aAggs
18dc7 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  [0]); i++){.    
18dc8 76 6f 69 64 20 2a 70 41 72 67 20 3d 20 28 76 6f  void *pArg = (vo
18dc9 69 64 2a 29 28 69 6e 74 29 61 41 67 67 73 5b 69  id*)(int)aAggs[i
18dca 5d 2e 61 72 67 54 79 70 65 3b 0a 20 20 20 20 73  ].argType;.    s
18dcb 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63  qlite3CreateFunc
18dcc 28 64 62 2c 20 61 41 67 67 73 5b 69 5d 2e 7a 4e  (db, aAggs[i].zN
18dcd 61 6d 65 2c 20 61 41 67 67 73 5b 69 5d 2e 6e 41  ame, aAggs[i].nA
18dce 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c  rg, SQLITE_UTF8,
18dcf 20 0a 20 20 20 20 20 20 20 20 70 41 72 67 2c 20   .        pArg, 
18dd0 30 2c 20 61 41 67 67 73 5b 69 5d 2e 78 53 74 65  0, aAggs[i].xSte
18dd1 70 2c 20 61 41 67 67 73 5b 69 5d 2e 78 46 69 6e  p, aAggs[i].xFin
18dd2 61 6c 69 7a 65 29 3b 0a 20 20 20 20 69 66 28 20  alize);.    if( 
18dd3 61 41 67 67 73 5b 69 5d 2e 6e 65 65 64 43 6f 6c  aAggs[i].needCol
18dd4 6c 53 65 71 20 29 7b 0a 20 20 20 20 20 20 46 75  lSeq ){.      Fu
18dd5 6e 63 44 65 66 20 2a 70 46 75 6e 63 20 3d 20 73  ncDef *pFunc = s
18dd6 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69  qlite3FindFuncti
18dd7 6f 6e 28 20 64 62 2c 20 61 41 67 67 73 5b 69 5d  on( db, aAggs[i]
18dd8 2e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20  .zName,.        
18dd9 20 20 73 74 72 6c 65 6e 28 61 41 67 67 73 5b 69    strlen(aAggs[i
18dda 5d 2e 7a 4e 61 6d 65 29 2c 20 61 41 67 67 73 5b  ].zName), aAggs[
18ddb 69 5d 2e 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f  i].nArg, SQLITE_
18ddc 55 54 46 38 2c 20 30 29 3b 0a 20 20 20 20 20 20  UTF8, 0);.      
18ddd 69 66 28 20 70 46 75 6e 63 20 26 26 20 61 41 67  if( pFunc && aAg
18dde 67 73 5b 69 5d 2e 6e 65 65 64 43 6f 6c 6c 53 65  gs[i].needCollSe
18ddf 71 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 75  q ){.        pFu
18de0 6e 63 2d 3e 6e 65 65 64 43 6f 6c 6c 53 65 71 20  nc->needCollSeq 
18de1 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 1;.      }.   
18de2 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33   }.  }.  sqlite3
18de3 52 65 67 69 73 74 65 72 44 61 74 65 54 69 6d 65  RegisterDateTime
18de4 46 75 6e 63 74 69 6f 6e 73 28 64 62 29 3b 0a 20  Functions(db);. 
18de5 20 69 66 28 20 21 73 71 6c 69 74 65 33 4d 61 6c   if( !sqlite3Mal
18de6 6c 6f 63 46 61 69 6c 65 64 28 29 20 29 7b 0a 20  locFailed() ){. 
18de7 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69     int rc = sqli
18de8 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e  te3_overload_fun
18de9 63 74 69 6f 6e 28 64 62 2c 20 22 4d 41 54 43 48  ction(db, "MATCH
18dea 22 2c 20 32 29 3b 0a 20 20 20 20 61 73 73 65 72  ", 2);.    asser
18deb 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  t( rc==SQLITE_NO
18dec 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54  MEM || rc==SQLIT
18ded 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 69 66 28 20  E_OK );.    if( 
18dee 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
18def 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
18df0 33 46 61 69 6c 65 64 4d 61 6c 6c 6f 63 28 29 3b  3FailedMalloc();
18df1 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 69 66 64 65  .    }.  }.#ifde
18df2 66 20 53 51 4c 49 54 45 5f 53 53 45 0a 20 20 28  f SQLITE_SSE.  (
18df3 76 6f 69 64 29 73 71 6c 69 74 65 33 53 73 65 46  void)sqlite3SseF
18df4 75 6e 63 74 69 6f 6e 73 28 64 62 29 3b 0a 23 65  unctions(db);.#e
18df5 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49  ndif.#ifdef SQLI
18df6 54 45 5f 43 41 53 45 5f 53 45 4e 53 49 54 49 56  TE_CASE_SENSITIV
18df7 45 5f 4c 49 4b 45 0a 20 20 73 71 6c 69 74 65 33  E_LIKE.  sqlite3
18df8 52 65 67 69 73 74 65 72 4c 69 6b 65 46 75 6e 63  RegisterLikeFunc
18df9 74 69 6f 6e 73 28 64 62 2c 20 31 29 3b 0a 23 65  tions(db, 1);.#e
18dfa 6c 73 65 0a 20 20 73 71 6c 69 74 65 33 52 65 67  lse.  sqlite3Reg
18dfb 69 73 74 65 72 4c 69 6b 65 46 75 6e 63 74 69 6f  isterLikeFunctio
18dfc 6e 73 28 64 62 2c 20 30 29 3b 0a 23 65 6e 64 69  ns(db, 0);.#endi
18dfd 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74  f.}../*.** Set t
18dfe 68 65 20 4c 49 4b 45 4f 50 54 20 66 6c 61 67 20  he LIKEOPT flag 
18dff 6f 6e 20 74 68 65 20 32 2d 61 72 67 75 6d 65 6e  on the 2-argumen
18e00 74 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20  t function with 
18e01 74 68 65 20 67 69 76 65 6e 20 6e 61 6d 65 2e 0a  the given name..
18e02 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
18e03 65 74 4c 69 6b 65 4f 70 74 46 6c 61 67 28 73 71  etLikeOptFlag(sq
18e04 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74  lite3 *db, const
18e05 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e   char *zName, in
18e06 74 20 66 6c 61 67 56 61 6c 29 7b 0a 20 20 46 75  t flagVal){.  Fu
18e07 6e 63 44 65 66 20 2a 70 44 65 66 3b 0a 20 20 70  ncDef *pDef;.  p
18e08 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  Def = sqlite3Fin
18e09 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 4e  dFunction(db, zN
18e0a 61 6d 65 2c 20 73 74 72 6c 65 6e 28 7a 4e 61 6d  ame, strlen(zNam
18e0b 65 29 2c 20 32 2c 20 53 51 4c 49 54 45 5f 55 54  e), 2, SQLITE_UT
18e0c 46 38 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 44  F8, 0);.  if( pD
18e0d 65 66 20 29 7b 0a 20 20 20 20 70 44 65 66 2d 3e  ef ){.    pDef->
18e0e 66 6c 61 67 73 20 3d 20 66 6c 61 67 56 61 6c 3b  flags = flagVal;
18e0f 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
18e10 67 69 73 74 65 72 20 74 68 65 20 62 75 69 6c 74  gister the built
18e11 2d 69 6e 20 4c 49 4b 45 20 61 6e 64 20 47 4c 4f  -in LIKE and GLO
18e12 42 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68  B functions.  Th
18e13 65 20 63 61 73 65 53 65 6e 73 69 74 69 76 65 0a  e caseSensitive.
18e14 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 64 65 74  ** parameter det
18e15 65 72 6d 69 6e 65 73 20 77 68 65 74 68 65 72 20  ermines whether 
18e16 6f 72 20 6e 6f 74 20 74 68 65 20 4c 49 4b 45 20  or not the LIKE 
18e17 6f 70 65 72 61 74 6f 72 20 69 73 20 63 61 73 65  operator is case
18e18 0a 2a 2a 20 73 65 6e 73 69 74 69 76 65 2e 20 20  .** sensitive.  
18e19 47 4c 4f 42 20 69 73 20 61 6c 77 61 79 73 20 63  GLOB is always c
18e1a 61 73 65 20 73 65 6e 73 69 74 69 76 65 2e 0a 2a  ase sensitive..*
18e1b 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
18e1c 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67   void sqlite3Reg
18e1d 69 73 74 65 72 4c 69 6b 65 46 75 6e 63 74 69 6f  isterLikeFunctio
18e1e 6e 73 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ns(sqlite3 *db, 
18e1f 69 6e 74 20 63 61 73 65 53 65 6e 73 69 74 69 76  int caseSensitiv
18e20 65 29 7b 0a 20 20 73 74 72 75 63 74 20 63 6f 6d  e){.  struct com
18e21 70 61 72 65 49 6e 66 6f 20 2a 70 49 6e 66 6f 3b  pareInfo *pInfo;
18e22 0a 20 20 69 66 28 20 63 61 73 65 53 65 6e 73 69  .  if( caseSensi
18e23 74 69 76 65 20 29 7b 0a 20 20 20 20 70 49 6e 66  tive ){.    pInf
18e24 6f 20 3d 20 28 73 74 72 75 63 74 20 63 6f 6d 70  o = (struct comp
18e25 61 72 65 49 6e 66 6f 2a 29 26 6c 69 6b 65 49 6e  areInfo*)&likeIn
18e26 66 6f 41 6c 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a  foAlt;.  }else{.
18e27 20 20 20 20 70 49 6e 66 6f 20 3d 20 28 73 74 72      pInfo = (str
18e28 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 2a  uct compareInfo*
18e29 29 26 6c 69 6b 65 49 6e 66 6f 4e 6f 72 6d 3b 0a  )&likeInfoNorm;.
18e2a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 43 72 65    }.  sqlite3Cre
18e2b 61 74 65 46 75 6e 63 28 64 62 2c 20 22 6c 69 6b  ateFunc(db, "lik
18e2c 65 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 55 54  e", 2, SQLITE_UT
18e2d 46 38 2c 20 70 49 6e 66 6f 2c 20 6c 69 6b 65 46  F8, pInfo, likeF
18e2e 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71  unc, 0, 0);.  sq
18e2f 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 28  lite3CreateFunc(
18e30 64 62 2c 20 22 6c 69 6b 65 22 2c 20 33 2c 20 53  db, "like", 3, S
18e31 51 4c 49 54 45 5f 55 54 46 38 2c 20 70 49 6e 66  QLITE_UTF8, pInf
18e32 6f 2c 20 6c 69 6b 65 46 75 6e 63 2c 20 30 2c 20  o, likeFunc, 0, 
18e33 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 43 72 65  0);.  sqlite3Cre
18e34 61 74 65 46 75 6e 63 28 64 62 2c 20 22 67 6c 6f  ateFunc(db, "glo
18e35 62 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 55 54  b", 2, SQLITE_UT
18e36 46 38 2c 20 0a 20 20 20 20 20 20 28 73 74 72 75  F8, .      (stru
18e37 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 2a 29  ct compareInfo*)
18e38 26 67 6c 6f 62 49 6e 66 6f 2c 20 6c 69 6b 65 46  &globInfo, likeF
18e39 75 6e 63 2c 20 30 2c 30 29 3b 0a 20 20 73 65 74  unc, 0,0);.  set
18e3a 4c 69 6b 65 4f 70 74 46 6c 61 67 28 64 62 2c 20  LikeOptFlag(db, 
18e3b 22 67 6c 6f 62 22 2c 20 53 51 4c 49 54 45 5f 46  "glob", SQLITE_F
18e3c 55 4e 43 5f 4c 49 4b 45 20 7c 20 53 51 4c 49 54  UNC_LIKE | SQLIT
18e3d 45 5f 46 55 4e 43 5f 43 41 53 45 29 3b 0a 20 20  E_FUNC_CASE);.  
18e3e 73 65 74 4c 69 6b 65 4f 70 74 46 6c 61 67 28 64  setLikeOptFlag(d
18e3f 62 2c 20 22 6c 69 6b 65 22 2c 20 0a 20 20 20 20  b, "like", .    
18e40 20 20 63 61 73 65 53 65 6e 73 69 74 69 76 65 20    caseSensitive 
18e41 3f 20 28 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c  ? (SQLITE_FUNC_L
18e42 49 4b 45 20 7c 20 53 51 4c 49 54 45 5f 46 55 4e  IKE | SQLITE_FUN
18e43 43 5f 43 41 53 45 29 20 3a 20 53 51 4c 49 54 45  C_CASE) : SQLITE
18e44 5f 46 55 4e 43 5f 4c 49 4b 45 29 3b 0a 7d 0a 0a  _FUNC_LIKE);.}..
18e45 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 70 6f 69 6e  /*.** pExpr poin
18e46 74 73 20 74 6f 20 61 6e 20 65 78 70 72 65 73 73  ts to an express
18e47 69 6f 6e 20 77 68 69 63 68 20 69 6d 70 6c 65 6d  ion which implem
18e48 65 6e 74 73 20 61 20 66 75 6e 63 74 69 6f 6e 2e  ents a function.
18e49 20 20 49 66 0a 2a 2a 20 69 74 20 69 73 20 61 70    If.** it is ap
18e4a 70 72 6f 70 72 69 61 74 65 20 74 6f 20 61 70 70  propriate to app
18e4b 6c 79 20 74 68 65 20 4c 49 4b 45 20 6f 70 74 69  ly the LIKE opti
18e4c 6d 69 7a 61 74 69 6f 6e 20 74 6f 20 74 68 61 74  mization to that
18e4d 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 74 68 65   function.** the
18e4e 6e 20 73 65 74 20 61 57 63 5b 30 5d 20 74 68 72  n set aWc[0] thr
18e4f 6f 75 67 68 20 61 57 63 5b 32 5d 20 74 6f 20 74  ough aWc[2] to t
18e50 68 65 20 77 69 6c 64 63 61 72 64 20 63 68 61 72  he wildcard char
18e51 61 63 74 65 72 73 20 61 6e 64 0a 2a 2a 20 72 65  acters and.** re
18e52 74 75 72 6e 20 54 52 55 45 2e 20 20 49 66 20 74  turn TRUE.  If t
18e53 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e  he function is n
18e54 6f 74 20 61 20 4c 49 4b 45 2d 73 74 79 6c 65 20  ot a LIKE-style 
18e55 66 75 6e 63 74 69 6f 6e 20 74 68 65 6e 0a 2a 2a  function then.**
18e56 20 72 65 74 75 72 6e 20 46 41 4c 53 45 2e 0a 2a   return FALSE..*
18e57 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
18e58 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4c 69   int sqlite3IsLi
18e59 6b 65 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74  keFunction(sqlit
18e5a 65 33 20 2a 64 62 2c 20 45 78 70 72 20 2a 70 45  e3 *db, Expr *pE
18e5b 78 70 72 2c 20 69 6e 74 20 2a 70 49 73 4e 6f 63  xpr, int *pIsNoc
18e5c 61 73 65 2c 20 63 68 61 72 20 2a 61 57 63 29 7b  ase, char *aWc){
18e5d 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66  .  FuncDef *pDef
18e5e 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f  ;.  if( pExpr->o
18e5f 70 21 3d 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 7c  p!=TK_FUNCTION |
18e60 7c 20 21 70 45 78 70 72 2d 3e 70 4c 69 73 74 20  | !pExpr->pList 
18e61 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
18e62 0a 20 20 7d 0a 20 20 69 66 28 20 70 45 78 70 72  .  }.  if( pExpr
18e63 2d 3e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d  ->pList->nExpr!=
18e64 32 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  2 ){.    return 
18e65 30 3b 0a 20 20 7d 0a 20 20 70 44 65 66 20 3d 20  0;.  }.  pDef = 
18e66 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74  sqlite3FindFunct
18e67 69 6f 6e 28 64 62 2c 20 28 63 68 61 72 2a 29 70  ion(db, (char*)p
18e68 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70  Expr->token.z, p
18e69 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 2c 20 32  Expr->token.n, 2
18e6a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
18e6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
18e6c 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 29 3b 0a  QLITE_UTF8, 0);.
18e6d 20 20 69 66 28 20 70 44 65 66 3d 3d 30 20 7c 7c    if( pDef==0 ||
18e6e 20 28 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20   (pDef->flags & 
18e6f 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45  SQLITE_FUNC_LIKE
18e70 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  )==0 ){.    retu
18e71 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  rn 0;.  }..  /* 
18e72 54 68 65 20 6d 65 6d 63 70 79 28 29 20 73 74 61  The memcpy() sta
18e73 74 65 6d 65 6e 74 20 61 73 73 75 6d 65 73 20 74  tement assumes t
18e74 68 61 74 20 74 68 65 20 77 69 6c 64 63 61 72 64  hat the wildcard
18e75 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65 0a   characters are.
18e76 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20 74    ** the first t
18e77 68 72 65 65 20 73 74 61 74 65 6d 65 6e 74 73 20  hree statements 
18e78 69 6e 20 74 68 65 20 63 6f 6d 70 61 72 65 49 6e  in the compareIn
18e79 66 6f 20 73 74 72 75 63 74 75 72 65 2e 20 20 54  fo structure.  T
18e7a 68 65 0a 20 20 2a 2a 20 61 73 73 65 72 74 73 28  he.  ** asserts(
18e7b 29 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 76 65  ) that follow ve
18e7c 72 69 66 79 20 74 68 61 74 20 61 73 73 75 6d 70  rify that assump
18e7d 74 69 6f 6e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 63  tion.  */.  memc
18e7e 70 79 28 61 57 63 2c 20 70 44 65 66 2d 3e 70 55  py(aWc, pDef->pU
18e7f 73 65 72 44 61 74 61 2c 20 33 29 3b 0a 20 20 61  serData, 3);.  a
18e80 73 73 65 72 74 28 20 28 63 68 61 72 2a 29 26 6c  ssert( (char*)&l
18e81 69 6b 65 49 6e 66 6f 41 6c 74 20 3d 3d 20 28 63  ikeInfoAlt == (c
18e82 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c  har*)&likeInfoAl
18e83 74 2e 6d 61 74 63 68 41 6c 6c 20 29 3b 0a 20 20  t.matchAll );.  
18e84 61 73 73 65 72 74 28 20 26 28 28 63 68 61 72 2a  assert( &((char*
18e85 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 29 5b 31  )&likeInfoAlt)[1
18e86 5d 20 3d 3d 20 28 63 68 61 72 2a 29 26 6c 69 6b  ] == (char*)&lik
18e87 65 49 6e 66 6f 41 6c 74 2e 6d 61 74 63 68 4f 6e  eInfoAlt.matchOn
18e88 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 26  e );.  assert( &
18e89 28 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66  ((char*)&likeInf
18e8a 6f 41 6c 74 29 5b 32 5d 20 3d 3d 20 28 63 68 61  oAlt)[2] == (cha
18e8b 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 2e  r*)&likeInfoAlt.
18e8c 6d 61 74 63 68 53 65 74 20 29 3b 0a 20 20 2a 70  matchSet );.  *p
18e8d 49 73 4e 6f 63 61 73 65 20 3d 20 28 70 44 65 66  IsNocase = (pDef
18e8e 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
18e8f 5f 46 55 4e 43 5f 43 41 53 45 29 3d 3d 30 3b 0a  _FUNC_CASE)==0;.
18e90 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f    return 1;.}../
18e91 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
18e92 6e 64 20 6f 66 20 66 75 6e 63 2e 63 20 2a 2a 2a  nd of func.c ***
18e93 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18e94 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18e95 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
18e96 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
18e97 65 67 69 6e 20 66 69 6c 65 20 69 6e 73 65 72 74  egin file insert
18e98 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
18e99 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18e9a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
18e9b 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d  *.** 2001 Septem
18e9c 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65  ber 15.**.** The
18e9d 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
18e9e 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
18e9f 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
18ea0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
18ea1 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
18ea2 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
18ea3 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
18ea4 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
18ea5 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
18ea6 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
18ea7 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
18ea8 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
18ea9 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
18eaa 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
18eab 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
18eac 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
18ead 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
18eae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18eaf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18eb1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18eb2 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
18eb3 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f  le contains C co
18eb4 64 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74  de routines that
18eb5 20 61 72 65 20 63 61 6c 6c 65 64 20 62 79 20 74   are called by t
18eb6 68 65 20 70 61 72 73 65 72 0a 2a 2a 20 74 6f 20  he parser.** to 
18eb7 68 61 6e 64 6c 65 20 49 4e 53 45 52 54 20 73 74  handle INSERT st
18eb8 61 74 65 6d 65 6e 74 73 20 69 6e 20 53 51 4c 69  atements in SQLi
18eb9 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 69  te..**.** $Id: i
18eba 6e 73 65 72 74 2e 63 2c 76 20 31 2e 31 38 38 20  nsert.c,v 1.188 
18ebb 32 30 30 37 2f 30 37 2f 32 33 20 31 39 3a 33 39  2007/07/23 19:39
18ebc 3a 34 37 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :47 drh Exp $.*/
18ebd 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 50 33 20 6f  ../*.** Set P3 o
18ebe 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  f the most recen
18ebf 74 6c 79 20 69 6e 73 65 72 74 65 64 20 6f 70 63  tly inserted opc
18ec0 6f 64 65 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20  ode to a column 
18ec1 61 66 66 69 6e 69 74 79 0a 2a 2a 20 73 74 72 69  affinity.** stri
18ec2 6e 67 20 66 6f 72 20 69 6e 64 65 78 20 70 49 64  ng for index pId
18ec3 78 2e 20 41 20 63 6f 6c 75 6d 6e 20 61 66 66 69  x. A column affi
18ec4 6e 69 74 79 20 73 74 72 69 6e 67 20 68 61 73 20  nity string has 
18ec5 6f 6e 65 20 63 68 61 72 61 63 74 65 72 0a 2a 2a  one character.**
18ec6 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e   for each column
18ec7 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2c 20 61   in the table, a
18ec8 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20  ccording to the 
18ec9 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65 20  affinity of the 
18eca 63 6f 6c 75 6d 6e 3a 0a 2a 2a 0a 2a 2a 20 20 43  column:.**.**  C
18ecb 68 61 72 61 63 74 65 72 20 20 20 20 20 20 43 6f  haracter      Co
18ecc 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 0a 2a 2a  lumn affinity.**
18ecd 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    --------------
18ece 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
18ecf 0a 2a 2a 20 20 27 61 27 20 20 20 20 20 20 20 20  .**  'a'        
18ed0 20 20 20 20 54 45 58 54 0a 2a 2a 20 20 27 62 27      TEXT.**  'b'
18ed1 20 20 20 20 20 20 20 20 20 20 20 20 4e 4f 4e 45              NONE
18ed2 0a 2a 2a 20 20 27 63 27 20 20 20 20 20 20 20 20  .**  'c'        
18ed3 20 20 20 20 4e 55 4d 45 52 49 43 0a 2a 2a 20 20      NUMERIC.**  
18ed4 27 64 27 20 20 20 20 20 20 20 20 20 20 20 20 49  'd'            I
18ed5 4e 54 45 47 45 52 0a 2a 2a 20 20 27 65 27 20 20  NTEGER.**  'e'  
18ed6 20 20 20 20 20 20 20 20 20 20 52 45 41 4c 0a 2a            REAL.*
18ed7 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
18ed8 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 6e 64   void sqlite3Ind
18ed9 65 78 41 66 66 69 6e 69 74 79 53 74 72 28 56 64  exAffinityStr(Vd
18eda 62 65 20 2a 76 2c 20 49 6e 64 65 78 20 2a 70 49  be *v, Index *pI
18edb 64 78 29 7b 0a 20 20 69 66 28 20 21 70 49 64 78  dx){.  if( !pIdx
18edc 2d 3e 7a 43 6f 6c 41 66 66 20 29 7b 0a 20 20 20  ->zColAff ){.   
18edd 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 74 69   /* The first ti
18ede 6d 65 20 61 20 63 6f 6c 75 6d 6e 20 61 66 66 69  me a column affi
18edf 6e 69 74 79 20 73 74 72 69 6e 67 20 66 6f 72 20  nity string for 
18ee0 61 20 70 61 72 74 69 63 75 6c 61 72 20 69 6e 64  a particular ind
18ee1 65 78 20 69 73 0a 20 20 20 20 2a 2a 20 72 65 71  ex is.    ** req
18ee2 75 69 72 65 64 2c 20 69 74 20 69 73 20 61 6c 6c  uired, it is all
18ee3 6f 63 61 74 65 64 20 61 6e 64 20 70 6f 70 75 6c  ocated and popul
18ee4 61 74 65 64 20 68 65 72 65 2e 20 49 74 20 69 73  ated here. It is
18ee5 20 74 68 65 6e 20 73 74 6f 72 65 64 20 61 73 0a   then stored as.
18ee6 20 20 20 20 2a 2a 20 61 20 6d 65 6d 62 65 72 20      ** a member 
18ee7 6f 66 20 74 68 65 20 49 6e 64 65 78 20 73 74 72  of the Index str
18ee8 75 63 74 75 72 65 20 66 6f 72 20 73 75 62 73 65  ucture for subse
18ee9 71 75 65 6e 74 20 75 73 65 2e 0a 20 20 20 20 2a  quent use..    *
18eea 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 63 6f 6c  *.    ** The col
18eeb 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72  umn affinity str
18eec 69 6e 67 20 77 69 6c 6c 20 65 76 65 6e 74 75 61  ing will eventua
18eed 6c 6c 79 20 62 65 20 64 65 6c 65 74 65 64 20 62  lly be deleted b
18eee 79 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 44  y.    ** sqliteD
18eef 65 6c 65 74 65 49 6e 64 65 78 28 29 20 77 68 65  eleteIndex() whe
18ef0 6e 20 74 68 65 20 49 6e 64 65 78 20 73 74 72 75  n the Index stru
18ef1 63 74 75 72 65 20 69 74 73 65 6c 66 20 69 73 20  cture itself is 
18ef2 63 6c 65 61 6e 65 64 0a 20 20 20 20 2a 2a 20 75  cleaned.    ** u
18ef3 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e  p..    */.    in
18ef4 74 20 6e 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a  t n;.    Table *
18ef5 70 54 61 62 20 3d 20 70 49 64 78 2d 3e 70 54 61  pTab = pIdx->pTa
18ef6 62 6c 65 3b 0a 20 20 20 20 70 49 64 78 2d 3e 7a  ble;.    pIdx->z
18ef7 43 6f 6c 41 66 66 20 3d 20 28 63 68 61 72 20 2a  ColAff = (char *
18ef8 29 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 70 49  )sqliteMalloc(pI
18ef9 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b 0a  dx->nColumn+1);.
18efa 20 20 20 20 69 66 28 20 21 70 49 64 78 2d 3e 7a      if( !pIdx->z
18efb 43 6f 6c 41 66 66 20 29 7b 0a 20 20 20 20 20 20  ColAff ){.      
18efc 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
18efd 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c 70 49 64    for(n=0; n<pId
18efe 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6e 2b 2b 29  x->nColumn; n++)
18eff 7b 0a 20 20 20 20 20 20 70 49 64 78 2d 3e 7a 43  {.      pIdx->zC
18f00 6f 6c 41 66 66 5b 6e 5d 20 3d 20 70 54 61 62 2d  olAff[n] = pTab-
18f01 3e 61 43 6f 6c 5b 70 49 64 78 2d 3e 61 69 43 6f  >aCol[pIdx->aiCo
18f02 6c 75 6d 6e 5b 6e 5d 5d 2e 61 66 66 69 6e 69 74  lumn[n]].affinit
18f03 79 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 49 64  y;.    }.    pId
18f04 78 2d 3e 7a 43 6f 6c 41 66 66 5b 70 49 64 78 2d  x->zColAff[pIdx-
18f05 3e 6e 43 6f 6c 75 6d 6e 5d 20 3d 20 27 5c 30 27  >nColumn] = '\0'
18f06 3b 0a 20 20 7d 0a 20 0a 20 20 73 71 6c 69 74 65  ;.  }. .  sqlite
18f07 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 76 2c  3VdbeChangeP3(v,
18f08 20 2d 31 2c 20 70 49 64 78 2d 3e 7a 43 6f 6c 41   -1, pIdx->zColA
18f09 66 66 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ff, 0);.}../*.**
18f0a 20 53 65 74 20 50 33 20 6f 66 20 74 68 65 20 6d   Set P3 of the m
18f0b 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 69 6e 73  ost recently ins
18f0c 65 72 74 65 64 20 6f 70 63 6f 64 65 20 74 6f 20  erted opcode to 
18f0d 61 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74  a column affinit
18f0e 79 0a 2a 2a 20 73 74 72 69 6e 67 20 66 6f 72 20  y.** string for 
18f0f 74 61 62 6c 65 20 70 54 61 62 2e 20 41 20 63 6f  table pTab. A co
18f10 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74  lumn affinity st
18f11 72 69 6e 67 20 68 61 73 20 6f 6e 65 20 63 68 61  ring has one cha
18f12 72 61 63 74 65 72 0a 2a 2a 20 66 6f 72 20 65 61  racter.** for ea
18f13 63 68 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 65  ch column indexe
18f14 64 20 62 79 20 74 68 65 20 69 6e 64 65 78 2c 20  d by the index, 
18f15 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65  according to the
18f16 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65   affinity of the
18f17 0a 2a 2a 20 63 6f 6c 75 6d 6e 3a 0a 2a 2a 0a 2a  .** column:.**.*
18f18 2a 20 20 43 68 61 72 61 63 74 65 72 20 20 20 20  *  Character    
18f19 20 20 43 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74    Column affinit
18f1a 79 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  y.**  ----------
18f1b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
18f1c 2d 2d 2d 2d 0a 2a 2a 20 20 27 61 27 20 20 20 20  ----.**  'a'    
18f1d 20 20 20 20 20 20 20 20 54 45 58 54 0a 2a 2a 20          TEXT.** 
18f1e 20 27 62 27 20 20 20 20 20 20 20 20 20 20 20 20   'b'            
18f1f 4e 4f 4e 45 0a 2a 2a 20 20 27 63 27 20 20 20 20  NONE.**  'c'    
18f20 20 20 20 20 20 20 20 20 4e 55 4d 45 52 49 43 0a          NUMERIC.
18f21 2a 2a 20 20 27 64 27 20 20 20 20 20 20 20 20 20  **  'd'         
18f22 20 20 20 49 4e 54 45 47 45 52 0a 2a 2a 20 20 27     INTEGER.**  '
18f23 65 27 20 20 20 20 20 20 20 20 20 20 20 20 52 45  e'            RE
18f24 41 4c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  AL.*/.SQLITE_PRI
18f25 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
18f26 33 54 61 62 6c 65 41 66 66 69 6e 69 74 79 53 74  3TableAffinitySt
18f27 72 28 56 64 62 65 20 2a 76 2c 20 54 61 62 6c 65  r(Vdbe *v, Table
18f28 20 2a 70 54 61 62 29 7b 0a 20 20 2f 2a 20 54 68   *pTab){.  /* Th
18f29 65 20 66 69 72 73 74 20 74 69 6d 65 20 61 20 63  e first time a c
18f2a 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73  olumn affinity s
18f2b 74 72 69 6e 67 20 66 6f 72 20 61 20 70 61 72 74  tring for a part
18f2c 69 63 75 6c 61 72 20 74 61 62 6c 65 0a 20 20 2a  icular table.  *
18f2d 2a 20 69 73 20 72 65 71 75 69 72 65 64 2c 20 69  * is required, i
18f2e 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61  t is allocated a
18f2f 6e 64 20 70 6f 70 75 6c 61 74 65 64 20 68 65 72  nd populated her
18f30 65 2e 20 49 74 20 69 73 20 74 68 65 6e 20 0a 20  e. It is then . 
18f31 20 2a 2a 20 73 74 6f 72 65 64 20 61 73 20 61 20   ** stored as a 
18f32 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20 54 61  member of the Ta
18f33 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 66 6f  ble structure fo
18f34 72 20 73 75 62 73 65 71 75 65 6e 74 20 75 73 65  r subsequent use
18f35 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  ..  **.  ** The 
18f36 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20  column affinity 
18f37 73 74 72 69 6e 67 20 77 69 6c 6c 20 65 76 65 6e  string will even
18f38 74 75 61 6c 6c 79 20 62 65 20 64 65 6c 65 74 65  tually be delete
18f39 64 20 62 79 0a 20 20 2a 2a 20 73 71 6c 69 74 65  d by.  ** sqlite
18f3a 33 44 65 6c 65 74 65 54 61 62 6c 65 28 29 20 77  3DeleteTable() w
18f3b 68 65 6e 20 74 68 65 20 54 61 62 6c 65 20 73 74  hen the Table st
18f3c 72 75 63 74 75 72 65 20 69 74 73 65 6c 66 20 69  ructure itself i
18f3d 73 20 63 6c 65 61 6e 65 64 20 75 70 2e 0a 20 20  s cleaned up..  
18f3e 2a 2f 0a 20 20 69 66 28 20 21 70 54 61 62 2d 3e  */.  if( !pTab->
18f3f 7a 43 6f 6c 41 66 66 20 29 7b 0a 20 20 20 20 63  zColAff ){.    c
18f40 68 61 72 20 2a 7a 43 6f 6c 41 66 66 3b 0a 20 20  har *zColAff;.  
18f41 20 20 69 6e 74 20 69 3b 0a 0a 20 20 20 20 7a 43    int i;..    zC
18f42 6f 6c 41 66 66 20 3d 20 28 63 68 61 72 20 2a 29  olAff = (char *)
18f43 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 70 54 61  sqliteMalloc(pTa
18f44 62 2d 3e 6e 43 6f 6c 2b 31 29 3b 0a 20 20 20 20  b->nCol+1);.    
18f45 69 66 28 20 21 7a 43 6f 6c 41 66 66 20 29 7b 0a  if( !zColAff ){.
18f46 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
18f47 20 20 7d 0a 0a 20 20 20 20 66 6f 72 28 69 3d 30    }..    for(i=0
18f48 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20  ; i<pTab->nCol; 
18f49 69 2b 2b 29 7b 0a 20 20 20 20 20 20 7a 43 6f 6c  i++){.      zCol
18f4a 41 66 66 5b 69 5d 20 3d 20 70 54 61 62 2d 3e 61  Aff[i] = pTab->a
18f4b 43 6f 6c 5b 69 5d 2e 61 66 66 69 6e 69 74 79 3b  Col[i].affinity;
18f4c 0a 20 20 20 20 7d 0a 20 20 20 20 7a 43 6f 6c 41  .    }.    zColA
18f4d 66 66 5b 70 54 61 62 2d 3e 6e 43 6f 6c 5d 20 3d  ff[pTab->nCol] =
18f4e 20 27 5c 30 27 3b 0a 0a 20 20 20 20 70 54 61 62   '\0';..    pTab
18f4f 2d 3e 7a 43 6f 6c 41 66 66 20 3d 20 7a 43 6f 6c  ->zColAff = zCol
18f50 41 66 66 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69  Aff;.  }..  sqli
18f51 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28  te3VdbeChangeP3(
18f52 76 2c 20 2d 31 2c 20 70 54 61 62 2d 3e 7a 43 6f  v, -1, pTab->zCo
18f53 6c 41 66 66 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a  lAff, 0);.}../*.
18f54 2a 2a 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65  ** Return non-ze
18f55 72 6f 20 69 66 20 53 45 4c 45 43 54 20 73 74 61  ro if SELECT sta
18f56 74 65 6d 65 6e 74 20 70 20 6f 70 65 6e 73 20 74  tement p opens t
18f57 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f  he table with ro
18f58 6f 74 70 61 67 65 0a 2a 2a 20 69 54 61 62 20 69  otpage.** iTab i
18f59 6e 20 64 61 74 61 62 61 73 65 20 69 44 62 2e 20  n database iDb. 
18f5a 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74 6f   This is used to
18f5b 20 73 65 65 20 69 66 20 61 20 73 74 61 74 65 6d   see if a statem
18f5c 65 6e 74 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  ent of the form 
18f5d 0a 2a 2a 20 22 49 4e 53 45 52 54 20 49 4e 54 4f  .** "INSERT INTO
18f5e 20 3c 69 44 62 2c 20 69 54 61 62 3e 20 53 45 4c   <iDb, iTab> SEL
18f5f 45 43 54 20 2e 2e 2e 22 20 63 61 6e 20 72 75 6e  ECT ..." can run
18f60 20 77 69 74 68 6f 75 74 20 75 73 69 6e 67 20 74   without using t
18f61 65 6d 70 6f 72 61 72 79 0a 2a 2a 20 74 61 62 6c  emporary.** tabl
18f62 65 20 66 6f 72 20 74 68 65 20 72 65 73 75 6c 74  e for the result
18f63 73 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 2e  s of the SELECT.
18f64 20 0a 2a 2a 0a 2a 2a 20 4e 6f 20 63 68 65 63 6b   .**.** No check
18f65 69 6e 67 20 69 73 20 64 6f 6e 65 20 66 6f 72 20  ing is done for 
18f66 73 75 62 2d 73 65 6c 65 63 74 73 20 74 68 61 74  sub-selects that
18f67 20 61 72 65 20 70 61 72 74 20 6f 66 20 65 78 70   are part of exp
18f68 72 65 73 73 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61  ressions..*/.sta
18f69 74 69 63 20 69 6e 74 20 73 65 6c 65 63 74 52 65  tic int selectRe
18f6a 61 64 73 54 61 62 6c 65 28 53 65 6c 65 63 74 20  adsTable(Select 
18f6b 2a 70 2c 20 53 63 68 65 6d 61 20 2a 70 53 63 68  *p, Schema *pSch
18f6c 65 6d 61 2c 20 69 6e 74 20 69 54 61 62 29 7b 0a  ema, int iTab){.
18f6d 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63    int i;.  struc
18f6e 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
18f6f 70 49 74 65 6d 3b 0a 20 20 69 66 28 20 70 2d 3e  pItem;.  if( p->
18f70 70 53 72 63 3d 3d 30 20 29 20 72 65 74 75 72 6e  pSrc==0 ) return
18f71 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70   0;.  for(i=0, p
18f72 49 74 65 6d 3d 70 2d 3e 70 53 72 63 2d 3e 61 3b  Item=p->pSrc->a;
18f73 20 69 3c 70 2d 3e 70 53 72 63 2d 3e 6e 53 72 63   i<p->pSrc->nSrc
18f74 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b  ; i++, pItem++){
18f75 0a 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e  .    if( pItem->
18f76 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20  pSelect ){.     
18f77 20 69 66 28 20 73 65 6c 65 63 74 52 65 61 64 73   if( selectReads
18f78 54 61 62 6c 65 28 70 49 74 65 6d 2d 3e 70 53 65  Table(pItem->pSe
18f79 6c 65 63 74 2c 20 70 53 63 68 65 6d 61 2c 20 69  lect, pSchema, i
18f7a 54 61 62 29 20 29 20 72 65 74 75 72 6e 20 31 3b  Tab) ) return 1;
18f7b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
18f7c 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 70 54 61    if( pItem->pTa
18f7d 62 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 53 63 68  b->pSchema==pSch
18f7e 65 6d 61 20 26 26 20 70 49 74 65 6d 2d 3e 70 54  ema && pItem->pT
18f7f 61 62 2d 3e 74 6e 75 6d 3d 3d 69 54 61 62 20 29  ab->tnum==iTab )
18f80 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
18f81 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
18f82 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
18f83 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52  TE_OMIT_AUTOINCR
18f84 45 4d 45 4e 54 0a 2f 2a 0a 2a 2a 20 57 72 69 74  EMENT./*.** Writ
18f85 65 20 6f 75 74 20 63 6f 64 65 20 74 6f 20 69 6e  e out code to in
18f86 69 74 69 61 6c 69 7a 65 20 74 68 65 20 61 75 74  itialize the aut
18f87 6f 69 6e 63 72 65 6d 65 6e 74 20 6c 6f 67 69 63  oincrement logic
18f88 2e 20 20 54 68 69 73 20 63 6f 64 65 0a 2a 2a 20  .  This code.** 
18f89 6c 6f 6f 6b 73 20 75 70 20 74 68 65 20 63 75 72  looks up the cur
18f8a 72 65 6e 74 20 61 75 74 6f 69 6e 63 72 65 6d 65  rent autoincreme
18f8b 6e 74 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20  nt value in the 
18f8c 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 0a  sqlite_sequence.
18f8d 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 73 74 6f  ** table and sto
18f8e 72 65 73 20 74 68 61 74 20 76 61 6c 75 65 20 69  res that value i
18f8f 6e 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e  n a memory cell.
18f90 20 20 43 6f 64 65 20 67 65 6e 65 72 61 74 65 64    Code generated
18f91 20 62 79 0a 2a 2a 20 61 75 74 6f 49 6e 63 53 74   by.** autoIncSt
18f92 65 70 28 29 20 77 69 6c 6c 20 6b 65 65 70 20 74  ep() will keep t
18f93 68 61 74 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20  hat memory cell 
18f94 68 6f 6c 64 69 6e 67 20 74 68 65 20 6c 61 72 67  holding the larg
18f95 65 73 74 0a 2a 2a 20 72 6f 77 69 64 20 76 61 6c  est.** rowid val
18f96 75 65 2e 20 20 43 6f 64 65 20 67 65 6e 65 72 61  ue.  Code genera
18f97 74 65 64 20 62 79 20 61 75 74 6f 49 6e 63 45 6e  ted by autoIncEn
18f98 64 28 29 20 77 69 6c 6c 20 77 72 69 74 65 20 74  d() will write t
18f99 68 65 20 6e 65 77 0a 2a 2a 20 6c 61 72 67 65 73  he new.** larges
18f9a 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63  t value of the c
18f9b 6f 75 6e 74 65 72 20 62 61 63 6b 20 69 6e 74 6f  ounter back into
18f9c 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75   the sqlite_sequ
18f9d 65 6e 63 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a  ence table..**.*
18f9e 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72  * This routine r
18f9f 65 74 75 72 6e 73 20 74 68 65 20 69 6e 64 65 78  eturns the index
18fa0 20 6f 66 20 74 68 65 20 6d 65 6d 5b 5d 20 63 65   of the mem[] ce
18fa1 6c 6c 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  ll that contains
18fa2 0a 2a 2a 20 74 68 65 20 6d 61 78 69 6d 75 6d 20  .** the maximum 
18fa3 72 6f 77 69 64 20 63 6f 75 6e 74 65 72 2e 0a 2a  rowid counter..*
18fa4 2a 0a 2a 2a 20 54 77 6f 20 6d 65 6d 6f 72 79 20  *.** Two memory 
18fa5 63 65 6c 6c 73 20 61 72 65 20 61 6c 6c 6f 63 61  cells are alloca
18fa6 74 65 64 2e 20 20 54 68 65 20 6e 65 78 74 20 6d  ted.  The next m
18fa7 65 6d 6f 72 79 20 63 65 6c 6c 20 61 66 74 65 72  emory cell after
18fa8 20 74 68 65 0a 2a 2a 20 6f 6e 65 20 72 65 74 75   the.** one retu
18fa9 72 6e 65 64 20 68 6f 6c 64 73 20 74 68 65 20 72  rned holds the r
18faa 6f 77 69 64 20 69 6e 20 73 71 6c 69 74 65 5f 73  owid in sqlite_s
18fab 65 71 75 65 6e 63 65 20 77 68 65 72 65 20 77 65  equence where we
18fac 20 77 69 6c 6c 0a 2a 2a 20 77 72 69 74 65 20 62   will.** write b
18fad 61 63 6b 20 74 68 65 20 72 65 76 69 73 65 64 20  ack the revised 
18fae 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64 2e 0a 2a  maximum rowid..*
18faf 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 75 74  /.static int aut
18fb0 6f 49 6e 63 42 65 67 69 6e 28 0a 20 20 50 61 72  oIncBegin(.  Par
18fb1 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
18fb2 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
18fb3 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62  ext */.  int iDb
18fb4 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
18fb5 49 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74  Index of the dat
18fb6 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 70 54  abase holding pT
18fb7 61 62 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  ab */.  Table *p
18fb8 54 61 62 20 20 20 20 20 20 20 20 20 2f 2a 20 54  Tab         /* T
18fb9 68 65 20 74 61 62 6c 65 20 77 65 20 61 72 65 20  he table we are 
18fba 77 72 69 74 69 6e 67 20 74 6f 20 2a 2f 0a 29 7b  writing to */.){
18fbb 0a 20 20 69 6e 74 20 6d 65 6d 49 64 20 3d 20 30  .  int memId = 0
18fbc 3b 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 61 75  ;.  if( pTab->au
18fbd 74 6f 49 6e 63 20 29 7b 0a 20 20 20 20 56 64 62  toInc ){.    Vdb
18fbe 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70  e *v = pParse->p
18fbf 56 64 62 65 3b 0a 20 20 20 20 44 62 20 2a 70 44  Vdbe;.    Db *pD
18fc0 62 20 3d 20 26 70 50 61 72 73 65 2d 3e 64 62 2d  b = &pParse->db-
18fc1 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 20 20 69  >aDb[iDb];.    i
18fc2 6e 74 20 69 43 75 72 20 3d 20 70 50 61 72 73 65  nt iCur = pParse
18fc3 2d 3e 6e 54 61 62 3b 0a 20 20 20 20 69 6e 74 20  ->nTab;.    int 
18fc4 61 64 64 72 3b 0a 20 20 20 20 61 73 73 65 72 74  addr;.    assert
18fc5 28 20 76 20 29 3b 0a 20 20 20 20 61 64 64 72 20  ( v );.    addr 
18fc6 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
18fc7 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20  rentAddr(v);.   
18fc8 20 6d 65 6d 49 64 20 3d 20 70 50 61 72 73 65 2d   memId = pParse-
18fc9 3e 6e 4d 65 6d 2b 31 3b 0a 20 20 20 20 70 50 61  >nMem+1;.    pPa
18fca 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 32 3b 0a  rse->nMem += 2;.
18fcb 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54      sqlite3OpenT
18fcc 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 43 75  able(pParse, iCu
18fcd 72 2c 20 69 44 62 2c 20 70 44 62 2d 3e 70 53 63  r, iDb, pDb->pSc
18fce 68 65 6d 61 2d 3e 70 53 65 71 54 61 62 2c 20 4f  hema->pSeqTab, O
18fcf 50 5f 4f 70 65 6e 52 65 61 64 29 3b 0a 20 20 20  P_OpenRead);.   
18fd0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
18fd1 70 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20  p(v, OP_Rewind, 
18fd2 69 43 75 72 2c 20 61 64 64 72 2b 31 33 29 3b 0a  iCur, addr+13);.
18fd3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
18fd4 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d  ddOp(v, OP_Colum
18fd5 6e 2c 20 69 43 75 72 2c 20 30 29 3b 0a 20 20 20  n, iCur, 0);.   
18fd6 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28   sqlite3VdbeOp3(
18fd7 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
18fd8 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  , 0, pTab->zName
18fd9 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
18fda 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
18fdb 5f 4e 65 2c 20 30 78 31 30 30 2c 20 61 64 64 72  _Ne, 0x100, addr
18fdc 2b 31 32 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  +12);.    sqlite
18fdd 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
18fde 5f 52 6f 77 69 64 2c 20 69 43 75 72 2c 20 30 29  _Rowid, iCur, 0)
18fdf 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
18fe0 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d  eAddOp(v, OP_Mem
18fe1 53 74 6f 72 65 2c 20 6d 65 6d 49 64 2d 31 2c 20  Store, memId-1, 
18fe2 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
18fe3 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43  dbeAddOp(v, OP_C
18fe4 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 31 29 3b  olumn, iCur, 1);
18fe5 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
18fe6 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 53  AddOp(v, OP_MemS
18fe7 74 6f 72 65 2c 20 6d 65 6d 49 64 2c 20 31 29 3b  tore, memId, 1);
18fe8 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
18fe9 41 64 64 4f 70 28 76 2c 20 4f 50 5f 47 6f 74 6f  AddOp(v, OP_Goto
18fea 2c 20 30 2c 20 61 64 64 72 2b 31 33 29 3b 0a 20  , 0, addr+13);. 
18feb 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
18fec 64 4f 70 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20  dOp(v, OP_Next, 
18fed 69 43 75 72 2c 20 61 64 64 72 2b 34 29 3b 0a 20  iCur, addr+4);. 
18fee 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
18fef 64 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  dOp(v, OP_Close,
18ff0 20 69 43 75 72 2c 20 30 29 3b 0a 20 20 7d 0a 20   iCur, 0);.  }. 
18ff1 20 72 65 74 75 72 6e 20 6d 65 6d 49 64 3b 0a 7d   return memId;.}
18ff2 0a 0a 2f 2a 0a 2a 2a 20 55 70 64 61 74 65 20 74  ../*.** Update t
18ff3 68 65 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64  he maximum rowid
18ff4 20 66 6f 72 20 61 6e 20 61 75 74 6f 69 6e 63 72   for an autoincr
18ff5 65 6d 65 6e 74 20 63 61 6c 63 75 6c 61 74 69 6f  ement calculatio
18ff6 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  n..**.** This ro
18ff7 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 62 65 20  utine should be 
18ff8 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 20  called when the 
18ff9 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b  top of the stack
18ffa 20 68 6f 6c 64 73 20 61 0a 2a 2a 20 6e 65 77 20   holds a.** new 
18ffb 72 6f 77 69 64 20 74 68 61 74 20 69 73 20 61 62  rowid that is ab
18ffc 6f 75 74 20 74 6f 20 62 65 20 69 6e 73 65 72 74  out to be insert
18ffd 65 64 2e 20 20 49 66 20 74 68 61 74 20 6e 65 77  ed.  If that new
18ffe 20 72 6f 77 69 64 20 69 73 0a 2a 2a 20 6c 61 72   rowid is.** lar
18fff 67 65 72 20 74 68 61 6e 20 74 68 65 20 6d 61 78  ger than the max
19000 69 6d 75 6d 20 72 6f 77 69 64 20 69 6e 20 74 68  imum rowid in th
19001 65 20 6d 65 6d 49 64 20 6d 65 6d 6f 72 79 20 63  e memId memory c
19002 65 6c 6c 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a  ell, then the.**
19003 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 73 20   memory cell is 
19004 75 70 64 61 74 65 64 2e 20 20 54 68 65 20 73 74  updated.  The st
19005 61 63 6b 20 69 73 20 75 6e 63 68 61 6e 67 65 64  ack is unchanged
19006 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
19007 20 61 75 74 6f 49 6e 63 53 74 65 70 28 50 61 72   autoIncStep(Par
19008 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
19009 6d 65 6d 49 64 29 7b 0a 20 20 69 66 28 20 6d 65  memId){.  if( me
1900a 6d 49 64 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c  mId>0 ){.    sql
1900b 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 70 50  ite3VdbeAddOp(pP
1900c 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f  arse->pVdbe, OP_
1900d 4d 65 6d 4d 61 78 2c 20 6d 65 6d 49 64 2c 20 30  MemMax, memId, 0
1900e 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
1900f 41 66 74 65 72 20 64 6f 69 6e 67 20 6f 6e 65 20  After doing one 
19010 6f 72 20 6d 6f 72 65 20 69 6e 73 65 72 74 73 2c  or more inserts,
19011 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 72 6f 77   the maximum row
19012 69 64 20 69 73 20 73 74 6f 72 65 64 0a 2a 2a 20  id is stored.** 
19013 69 6e 20 6d 65 6d 5b 6d 65 6d 49 64 5d 2e 20 20  in mem[memId].  
19014 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
19015 20 77 72 69 74 65 20 74 68 69 73 20 76 61 6c 75   write this valu
19016 65 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 0a  e back into the.
19017 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65  ** the sqlite_se
19018 71 75 65 6e 63 65 20 74 61 62 6c 65 2e 0a 2a 2f  quence table..*/
19019 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 75 74  .static void aut
1901a 6f 49 6e 63 45 6e 64 28 0a 20 20 50 61 72 73 65  oIncEnd(.  Parse
1901b 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a   *pParse,     /*
1901c 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e   The parsing con
1901d 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44  text */.  int iD
1901e 62 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  b,           /* 
1901f 49 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74  Index of the dat
19020 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 70 54  abase holding pT
19021 61 62 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  ab */.  Table *p
19022 54 61 62 2c 20 20 20 20 20 20 20 2f 2a 20 54 61  Tab,       /* Ta
19023 62 6c 65 20 77 65 20 61 72 65 20 69 6e 73 65 72  ble we are inser
19024 74 69 6e 67 20 69 6e 74 6f 20 2a 2f 0a 20 20 69  ting into */.  i
19025 6e 74 20 6d 65 6d 49 64 20 20 20 20 20 20 20 20  nt memId        
19026 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c    /* Memory cell
19027 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 6d 61 78   holding the max
19028 69 6d 75 6d 20 72 6f 77 69 64 20 2a 2f 0a 29 7b  imum rowid */.){
19029 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 61 75 74  .  if( pTab->aut
1902a 6f 49 6e 63 20 29 7b 0a 20 20 20 20 69 6e 74 20  oInc ){.    int 
1902b 69 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e  iCur = pParse->n
1902c 54 61 62 3b 0a 20 20 20 20 56 64 62 65 20 2a 76  Tab;.    Vdbe *v
1902d 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
1902e 3b 0a 20 20 20 20 44 62 20 2a 70 44 62 20 3d 20  ;.    Db *pDb = 
1902f 26 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62  &pParse->db->aDb
19030 5b 69 44 62 5d 3b 0a 20 20 20 20 69 6e 74 20 61  [iDb];.    int a
19031 64 64 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ddr;.    assert(
19032 20 76 20 29 3b 0a 20 20 20 20 61 64 64 72 20 3d   v );.    addr =
19033 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
19034 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20  entAddr(v);.    
19035 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65  sqlite3OpenTable
19036 28 70 50 61 72 73 65 2c 20 69 43 75 72 2c 20 69  (pParse, iCur, i
19037 44 62 2c 20 70 44 62 2d 3e 70 53 63 68 65 6d 61  Db, pDb->pSchema
19038 2d 3e 70 53 65 71 54 61 62 2c 20 4f 50 5f 4f 70  ->pSeqTab, OP_Op
19039 65 6e 57 72 69 74 65 29 3b 0a 20 20 20 20 73 71  enWrite);.    sq
1903a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1903b 2c 20 4f 50 5f 4d 65 6d 4c 6f 61 64 2c 20 6d 65  , OP_MemLoad, me
1903c 6d 49 64 2d 31 2c 20 30 29 3b 0a 20 20 20 20 73  mId-1, 0);.    s
1903d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1903e 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 2d  v, OP_NotNull, -
1903f 31 2c 20 61 64 64 72 2b 37 29 3b 0a 20 20 20 20  1, addr+7);.    
19040 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
19041 28 76 2c 20 4f 50 5f 50 6f 70 2c 20 31 2c 20 30  (v, OP_Pop, 1, 0
19042 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
19043 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4e 65  beAddOp(v, OP_Ne
19044 77 52 6f 77 69 64 2c 20 69 43 75 72 2c 20 30 29  wRowid, iCur, 0)
19045 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
19046 65 4f 70 33 28 76 2c 20 4f 50 5f 53 74 72 69 6e  eOp3(v, OP_Strin
19047 67 38 2c 20 30 2c 20 30 2c 20 70 54 61 62 2d 3e  g8, 0, 0, pTab->
19048 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 73  zName, 0);.    s
19049 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1904a 76 2c 20 4f 50 5f 4d 65 6d 4c 6f 61 64 2c 20 6d  v, OP_MemLoad, m
1904b 65 6d 49 64 2c 20 30 29 3b 0a 20 20 20 20 73 71  emId, 0);.    sq
1904c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1904d 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c  , OP_MakeRecord,
1904e 20 32 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   2, 0);.    sqli
1904f 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
19050 4f 50 5f 49 6e 73 65 72 74 2c 20 69 43 75 72 2c  OP_Insert, iCur,
19051 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b   OPFLAG_APPEND);
19052 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
19053 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f 73  AddOp(v, OP_Clos
19054 65 2c 20 69 43 75 72 2c 20 30 29 3b 0a 20 20 7d  e, iCur, 0);.  }
19055 0a 7d 0a 23 65 6c 73 65 0a 2f 2a 0a 2a 2a 20 49  .}.#else./*.** I
19056 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
19057 54 4f 49 4e 43 52 45 4d 45 4e 54 20 69 73 20 64  TOINCREMENT is d
19058 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 74 68 65  efined, then the
19059 20 74 68 72 65 65 20 72 6f 75 74 69 6e 65 73 0a   three routines.
1905a 2a 2a 20 61 62 6f 76 65 20 61 72 65 20 61 6c 6c  ** above are all
1905b 20 6e 6f 2d 6f 70 73 0a 2a 2f 0a 23 20 64 65 66   no-ops.*/.# def
1905c 69 6e 65 20 61 75 74 6f 49 6e 63 42 65 67 69 6e  ine autoIncBegin
1905d 28 41 2c 42 2c 43 29 20 28 30 29 0a 23 20 64 65  (A,B,C) (0).# de
1905e 66 69 6e 65 20 61 75 74 6f 49 6e 63 53 74 65 70  fine autoIncStep
1905f 28 41 2c 42 29 0a 23 20 64 65 66 69 6e 65 20 61  (A,B).# define a
19060 75 74 6f 49 6e 63 45 6e 64 28 41 2c 42 2c 43 2c  utoIncEnd(A,B,C,
19061 44 29 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  D).#endif /* SQL
19062 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43  ITE_OMIT_AUTOINC
19063 52 45 4d 45 4e 54 20 2a 2f 0a 0a 0a 2f 2a 20 46  REMENT */.../* F
19064 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69  orward declarati
19065 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  on */.static int
19066 20 78 66 65 72 4f 70 74 69 6d 69 7a 61 74 69 6f   xferOptimizatio
19067 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  n(.  Parse *pPar
19068 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61  se,        /* Pa
19069 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  rser context */.
1906a 20 20 54 61 62 6c 65 20 2a 70 44 65 73 74 2c 20    Table *pDest, 
1906b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
1906c 61 62 6c 65 20 77 65 20 61 72 65 20 69 6e 73 65  able we are inse
1906d 72 74 69 6e 67 20 69 6e 74 6f 20 2a 2f 0a 20 20  rting into */.  
1906e 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c  Select *pSelect,
1906f 20 20 20 20 20 20 2f 2a 20 41 20 53 45 4c 45 43        /* A SELEC
19070 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 75  T statement to u
19071 73 65 20 61 73 20 74 68 65 20 64 61 74 61 20 73  se as the data s
19072 6f 75 72 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6f  ource */.  int o
19073 6e 45 72 72 6f 72 2c 20 20 20 20 20 20 20 20 20  nError,         
19074 20 2f 2a 20 48 6f 77 20 74 6f 20 68 61 6e 64 6c   /* How to handl
19075 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 65 72 72  e constraint err
19076 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62  ors */.  int iDb
19077 44 65 73 74 20 20 20 20 20 20 20 20 20 20 20 2f  Dest           /
19078 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 6f  * The database o
19079 66 20 70 44 65 73 74 20 2a 2f 0a 29 3b 0a 0a 2f  f pDest */.);../
1907a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1907b 65 20 69 73 20 63 61 6c 6c 20 74 6f 20 68 61 6e  e is call to han
1907c 64 6c 65 20 53 51 4c 20 6f 66 20 74 68 65 20 66  dle SQL of the f
1907d 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 73 3a 0a  ollowing forms:.
1907e 2a 2a 0a 2a 2a 20 20 20 20 69 6e 73 65 72 74 20  **.**    insert 
1907f 69 6e 74 6f 20 54 41 42 4c 45 20 28 49 44 4c 49  into TABLE (IDLI
19080 53 54 29 20 76 61 6c 75 65 73 28 45 58 50 52 4c  ST) values(EXPRL
19081 49 53 54 29 0a 2a 2a 20 20 20 20 69 6e 73 65 72  IST).**    inser
19082 74 20 69 6e 74 6f 20 54 41 42 4c 45 20 28 49 44  t into TABLE (ID
19083 4c 49 53 54 29 20 73 65 6c 65 63 74 0a 2a 2a 0a  LIST) select.**.
19084 2a 2a 20 54 68 65 20 49 44 4c 49 53 54 20 66 6f  ** The IDLIST fo
19085 6c 6c 6f 77 69 6e 67 20 74 68 65 20 74 61 62 6c  llowing the tabl
19086 65 20 6e 61 6d 65 20 69 73 20 61 6c 77 61 79 73  e name is always
19087 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 49 66 20 6f   optional.  If o
19088 6d 69 74 74 65 64 2c 0a 2a 2a 20 74 68 65 6e 20  mitted,.** then 
19089 61 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 63 6f  a list of all co
1908a 6c 75 6d 6e 73 20 66 6f 72 20 74 68 65 20 74 61  lumns for the ta
1908b 62 6c 65 20 69 73 20 73 75 62 73 74 69 74 75 74  ble is substitut
1908c 65 64 2e 20 20 54 68 65 20 49 44 4c 49 53 54 0a  ed.  The IDLIST.
1908d 2a 2a 20 61 70 70 65 61 72 73 20 69 6e 20 74 68  ** appears in th
1908e 65 20 70 43 6f 6c 75 6d 6e 20 70 61 72 61 6d 65  e pColumn parame
1908f 74 65 72 2e 20 20 70 43 6f 6c 75 6d 6e 20 69 73  ter.  pColumn is
19090 20 4e 55 4c 4c 20 69 66 20 49 44 4c 49 53 54 20   NULL if IDLIST 
19091 69 73 20 6f 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a  is omitted..**.*
19092 2a 20 54 68 65 20 70 4c 69 73 74 20 70 61 72 61  * The pList para
19093 6d 65 74 65 72 20 68 6f 6c 64 73 20 45 58 50 52  meter holds EXPR
19094 4c 49 53 54 20 69 6e 20 74 68 65 20 66 69 72 73  LIST in the firs
19095 74 20 66 6f 72 6d 20 6f 66 20 74 68 65 20 49 4e  t form of the IN
19096 53 45 52 54 0a 2a 2a 20 73 74 61 74 65 6d 65 6e  SERT.** statemen
19097 74 20 61 62 6f 76 65 2c 20 61 6e 64 20 70 53 65  t above, and pSe
19098 6c 65 63 74 20 69 73 20 4e 55 4c 4c 2e 20 20 46  lect is NULL.  F
19099 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 66 6f  or the second fo
1909a 72 6d 2c 20 70 4c 69 73 74 20 69 73 0a 2a 2a 20  rm, pList is.** 
1909b 4e 55 4c 4c 20 61 6e 64 20 70 53 65 6c 65 63 74  NULL and pSelect
1909c 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
1909d 20 74 68 65 20 73 65 6c 65 63 74 20 73 74 61 74   the select stat
1909e 65 6d 65 6e 74 20 75 73 65 64 20 74 6f 20 67 65  ement used to ge
1909f 6e 65 72 61 74 65 0a 2a 2a 20 64 61 74 61 20 66  nerate.** data f
190a0 6f 72 20 74 68 65 20 69 6e 73 65 72 74 2e 0a 2a  or the insert..*
190a1 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 67 65  *.** The code ge
190a2 6e 65 72 61 74 65 64 20 66 6f 6c 6c 6f 77 73 20  nerated follows 
190a3 6f 6e 65 20 6f 66 20 66 6f 75 72 20 74 65 6d 70  one of four temp
190a4 6c 61 74 65 73 2e 20 20 46 6f 72 20 61 20 73 69  lates.  For a si
190a5 6d 70 6c 65 0a 2a 2a 20 73 65 6c 65 63 74 20 77  mple.** select w
190a6 69 74 68 20 64 61 74 61 20 63 6f 6d 69 6e 67 20  ith data coming 
190a7 66 72 6f 6d 20 61 20 56 41 4c 55 45 53 20 63 6c  from a VALUES cl
190a8 61 75 73 65 2c 20 74 68 65 20 63 6f 64 65 20 65  ause, the code e
190a9 78 65 63 75 74 65 73 0a 2a 2a 20 6f 6e 63 65 20  xecutes.** once 
190aa 73 74 72 61 69 67 68 74 20 64 6f 77 6e 20 74 68  straight down th
190ab 72 6f 75 67 68 2e 20 20 54 68 65 20 74 65 6d 70  rough.  The temp
190ac 6c 61 74 65 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20  late looks like 
190ad 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  this:.**.**     
190ae 20 20 20 20 6f 70 65 6e 20 77 72 69 74 65 20 63      open write c
190af 75 72 73 6f 72 20 74 6f 20 3c 74 61 62 6c 65 3e  ursor to <table>
190b0 20 61 6e 64 20 69 74 73 20 69 6e 64 69 63 65 73   and its indices
190b1 0a 2a 2a 20 20 20 20 20 20 20 20 20 70 75 74 73  .**         puts
190b2 20 56 41 4c 55 45 53 20 63 6c 61 75 73 65 20 65   VALUES clause e
190b3 78 70 72 65 73 73 69 6f 6e 73 20 6f 6e 74 6f 20  xpressions onto 
190b4 74 68 65 20 73 74 61 63 6b 0a 2a 2a 20 20 20 20  the stack.**    
190b5 20 20 20 20 20 77 72 69 74 65 20 74 68 65 20 72       write the r
190b6 65 73 75 6c 74 69 6e 67 20 72 65 63 6f 72 64 20  esulting record 
190b7 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20  into <table>.** 
190b8 20 20 20 20 20 20 20 20 63 6c 65 61 6e 75 70 0a          cleanup.
190b9 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 72 65 65 20  **.** The three 
190ba 72 65 6d 61 69 6e 69 6e 67 20 74 65 6d 70 6c 61  remaining templa
190bb 74 65 73 20 61 73 73 75 6d 65 20 74 68 65 20 73  tes assume the s
190bc 74 61 74 65 6d 65 6e 74 20 69 73 20 6f 66 20 74  tatement is of t
190bd 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20  he form.**.**   
190be 49 4e 53 45 52 54 20 49 4e 54 4f 20 3c 74 61 62  INSERT INTO <tab
190bf 6c 65 3e 20 53 45 4c 45 43 54 20 2e 2e 2e 0a 2a  le> SELECT ....*
190c0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 45 4c 45  *.** If the SELE
190c1 43 54 20 63 6c 61 75 73 65 20 69 73 20 6f 66 20  CT clause is of 
190c2 74 68 65 20 72 65 73 74 72 69 63 74 65 64 20 66  the restricted f
190c3 6f 72 6d 20 22 53 45 4c 45 43 54 20 2a 20 46 52  orm "SELECT * FR
190c4 4f 4d 20 3c 74 61 62 6c 65 32 3e 22 20 2d 0a 2a  OM <table2>" -.*
190c5 2a 20 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73  * in other words
190c6 20 69 66 20 74 68 65 20 53 45 4c 45 43 54 20 70   if the SELECT p
190c7 75 6c 6c 73 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73  ulls all columns
190c8 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20 74   from a single t
190c9 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 65 72  able.** and ther
190ca 65 20 69 73 20 6e 6f 20 57 48 45 52 45 20 6f 72  e is no WHERE or
190cb 20 4c 49 4d 49 54 20 6f 72 20 47 52 4f 55 50 20   LIMIT or GROUP 
190cc 42 59 20 6f 72 20 4f 52 44 45 52 20 42 59 20 63  BY or ORDER BY c
190cd 6c 61 75 73 65 73 2c 20 61 6e 64 0a 2a 2a 20 69  lauses, and.** i
190ce 66 20 3c 74 61 62 6c 65 32 3e 20 61 6e 64 20 3c  f <table2> and <
190cf 74 61 62 6c 65 31 3e 20 61 72 65 20 64 69 73 74  table1> are dist
190d0 69 6e 63 74 20 74 61 62 6c 65 73 20 62 75 74 20  inct tables but 
190d1 68 61 76 65 20 69 64 65 6e 74 69 63 61 6c 0a 2a  have identical.*
190d2 2a 20 73 63 68 65 6d 61 73 2c 20 69 6e 63 6c 75  * schemas, inclu
190d3 64 69 6e 67 20 61 6c 6c 20 74 68 65 20 73 61 6d  ding all the sam
190d4 65 20 69 6e 64 69 63 65 73 2c 20 74 68 65 6e 20  e indices, then 
190d5 61 20 73 70 65 63 69 61 6c 20 6f 70 74 69 6d 69  a special optimi
190d6 7a 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e 76  zation.** is inv
190d7 6f 6b 65 64 20 74 68 61 74 20 63 6f 70 69 65 73  oked that copies
190d8 20 72 61 77 20 72 65 63 6f 72 64 73 20 66 72 6f   raw records fro
190d9 6d 20 3c 74 61 62 6c 65 32 3e 20 6f 76 65 72 20  m <table2> over 
190da 74 6f 20 3c 74 61 62 6c 65 31 3e 2e 0a 2a 2a 20  to <table1>..** 
190db 53 65 65 20 74 68 65 20 78 66 65 72 4f 70 74 69  See the xferOpti
190dc 6d 69 7a 61 74 69 6f 6e 28 29 20 66 75 6e 63 74  mization() funct
190dd 69 6f 6e 20 66 6f 72 20 74 68 65 20 69 6d 70 6c  ion for the impl
190de 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
190df 69 73 0a 2a 2a 20 74 65 6d 70 6c 61 74 65 2e 20  is.** template. 
190e0 20 54 68 69 73 20 69 73 20 74 68 65 20 73 65 63   This is the sec
190e1 6f 6e 64 20 74 65 6d 70 6c 61 74 65 2e 0a 2a 2a  ond template..**
190e2 0a 2a 2a 20 20 20 20 20 20 20 20 20 6f 70 65 6e  .**         open
190e3 20 61 20 77 72 69 74 65 20 63 75 72 73 6f 72 20   a write cursor 
190e4 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20 20  to <table>.**   
190e5 20 20 20 20 20 20 6f 70 65 6e 20 72 65 61 64 20        open read 
190e6 63 75 72 73 6f 72 20 6f 6e 20 3c 74 61 62 6c 65  cursor on <table
190e7 32 3e 0a 2a 2a 20 20 20 20 20 20 20 20 20 74 72  2>.**         tr
190e8 61 6e 73 66 65 72 20 61 6c 6c 20 72 65 63 6f 72  ansfer all recor
190e9 64 73 20 69 6e 20 3c 74 61 62 6c 65 32 3e 20 6f  ds in <table2> o
190ea 76 65 72 20 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a  ver to <table>.*
190eb 2a 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 20  *         close 
190ec 63 75 72 73 6f 72 73 0a 2a 2a 20 20 20 20 20 20  cursors.**      
190ed 20 20 20 66 6f 72 65 61 63 68 20 69 6e 64 65 78     foreach index
190ee 20 6f 6e 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20   on <table>.**  
190ef 20 20 20 20 20 20 20 20 20 6f 70 65 6e 20 61 20           open a 
190f0 77 72 69 74 65 20 63 75 72 73 6f 72 20 6f 6e 20  write cursor on 
190f1 74 68 65 20 3c 74 61 62 6c 65 3e 20 69 6e 64 65  the <table> inde
190f2 78 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6f  x.**           o
190f3 70 65 6e 20 61 20 72 65 61 64 20 63 75 72 73 6f  pen a read curso
190f4 72 20 6f 6e 20 74 68 65 20 63 6f 72 72 65 73 70  r on the corresp
190f5 6f 6e 64 69 6e 67 20 3c 74 61 62 6c 65 32 3e 20  onding <table2> 
190f6 69 6e 64 65 78 0a 2a 2a 20 20 20 20 20 20 20 20  index.**        
190f7 20 20 20 74 72 61 6e 73 66 65 72 20 61 6c 6c 20     transfer all 
190f8 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 74 68 65  records from the
190f9 20 72 65 61 64 20 74 6f 20 74 68 65 20 77 72 69   read to the wri
190fa 74 65 20 63 75 72 73 6f 72 73 0a 2a 2a 20 20 20  te cursors.**   
190fb 20 20 20 20 20 20 20 20 63 6c 6f 73 65 20 63 75          close cu
190fc 72 73 6f 72 73 0a 2a 2a 20 20 20 20 20 20 20 20  rsors.**        
190fd 20 65 6e 64 20 66 6f 72 65 61 63 68 0a 2a 2a 0a   end foreach.**.
190fe 2a 2a 20 54 68 65 20 74 68 69 72 64 20 74 65 6d  ** The third tem
190ff 70 6c 61 74 65 20 69 73 20 66 6f 72 20 77 68 65  plate is for whe
19100 6e 20 74 68 65 20 73 65 63 6f 6e 64 20 74 65 6d  n the second tem
19101 70 6c 61 74 65 20 64 6f 65 73 20 6e 6f 74 20 61  plate does not a
19102 70 70 6c 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20  pply.** and the 
19103 53 45 4c 45 43 54 20 63 6c 61 75 73 65 20 64 6f  SELECT clause do
19104 65 73 20 6e 6f 74 20 72 65 61 64 20 66 72 6f 6d  es not read from
19105 20 3c 74 61 62 6c 65 3e 20 61 74 20 61 6e 79 20   <table> at any 
19106 74 69 6d 65 2e 0a 2a 2a 20 54 68 65 20 67 65 6e  time..** The gen
19107 65 72 61 74 65 64 20 63 6f 64 65 20 66 6f 6c 6c  erated code foll
19108 6f 77 73 20 74 68 69 73 20 74 65 6d 70 6c 61 74  ows this templat
19109 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  e:.**.**        
1910a 20 67 6f 74 6f 20 42 0a 2a 2a 20 20 20 20 20 20   goto B.**      
1910b 41 3a 20 73 65 74 75 70 20 66 6f 72 20 74 68 65  A: setup for the
1910c 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20   SELECT.**      
1910d 20 20 20 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65     loop over the
1910e 20 72 6f 77 73 20 69 6e 20 74 68 65 20 53 45 4c   rows in the SEL
1910f 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ECT.**          
19110 20 67 6f 73 75 62 20 43 0a 2a 2a 20 20 20 20 20   gosub C.**     
19111 20 20 20 20 65 6e 64 20 6c 6f 6f 70 0a 2a 2a 20      end loop.** 
19112 20 20 20 20 20 20 20 20 63 6c 65 61 6e 75 70 20          cleanup 
19113 61 66 74 65 72 20 74 68 65 20 53 45 4c 45 43 54  after the SELECT
19114 0a 2a 2a 20 20 20 20 20 20 20 20 20 67 6f 74 6f  .**         goto
19115 20 44 0a 2a 2a 20 20 20 20 20 20 42 3a 20 6f 70   D.**      B: op
19116 65 6e 20 77 72 69 74 65 20 63 75 72 73 6f 72 20  en write cursor 
19117 74 6f 20 3c 74 61 62 6c 65 3e 20 61 6e 64 20 69  to <table> and i
19118 74 73 20 69 6e 64 69 63 65 73 0a 2a 2a 20 20 20  ts indices.**   
19119 20 20 20 20 20 20 67 6f 74 6f 20 41 0a 2a 2a 20        goto A.** 
1911a 20 20 20 20 20 43 3a 20 69 6e 73 65 72 74 20 74       C: insert t
1911b 68 65 20 73 65 6c 65 63 74 20 72 65 73 75 6c 74  he select result
1911c 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a   into <table>.**
1911d 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 0a           return.
1911e 2a 2a 20 20 20 20 20 20 44 3a 20 63 6c 65 61 6e  **      D: clean
1911f 75 70 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 75  up.**.** The fou
19120 72 74 68 20 74 65 6d 70 6c 61 74 65 20 69 73 20  rth template is 
19121 75 73 65 64 20 69 66 20 74 68 65 20 69 6e 73 65  used if the inse
19122 72 74 20 73 74 61 74 65 6d 65 6e 74 20 74 61 6b  rt statement tak
19123 65 73 20 69 74 73 0a 2a 2a 20 76 61 6c 75 65 73  es its.** values
19124 20 66 72 6f 6d 20 61 20 53 45 4c 45 43 54 20 62   from a SELECT b
19125 75 74 20 74 68 65 20 64 61 74 61 20 69 73 20 62  ut the data is b
19126 65 69 6e 67 20 69 6e 73 65 72 74 65 64 20 69 6e  eing inserted in
19127 74 6f 20 61 20 74 61 62 6c 65 0a 2a 2a 20 74 68  to a table.** th
19128 61 74 20 69 73 20 61 6c 73 6f 20 72 65 61 64 20  at is also read 
19129 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 53  as part of the S
1912a 45 4c 45 43 54 2e 20 20 49 6e 20 74 68 65 20 74  ELECT.  In the t
1912b 68 69 72 64 20 66 6f 72 6d 2c 0a 2a 2a 20 77 65  hird form,.** we
1912c 20 68 61 76 65 20 74 6f 20 75 73 65 20 61 20 69   have to use a i
1912d 6e 74 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c  ntermediate tabl
1912e 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 72  e to store the r
1912f 65 73 75 6c 74 73 20 6f 66 0a 2a 2a 20 74 68 65  esults of.** the
19130 20 73 65 6c 65 63 74 2e 20 20 54 68 65 20 74 65   select.  The te
19131 6d 70 6c 61 74 65 20 69 73 20 6c 69 6b 65 20 74  mplate is like t
19132 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  his:.**.**      
19133 20 20 20 67 6f 74 6f 20 42 0a 2a 2a 20 20 20 20     goto B.**    
19134 20 20 41 3a 20 73 65 74 75 70 20 66 6f 72 20 74    A: setup for t
19135 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20  he SELECT.**    
19136 20 20 20 20 20 6c 6f 6f 70 20 6f 76 65 72 20 74       loop over t
19137 68 65 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65  he tables in the
19138 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20   SELECT.**      
19139 20 20 20 20 20 67 6f 73 75 62 20 43 0a 2a 2a 20       gosub C.** 
1913a 20 20 20 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70          end loop
1913b 0a 2a 2a 20 20 20 20 20 20 20 20 20 63 6c 65 61  .**         clea
1913c 6e 75 70 20 61 66 74 65 72 20 74 68 65 20 53 45  nup after the SE
1913d 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20  LECT.**         
1913e 67 6f 74 6f 20 44 0a 2a 2a 20 20 20 20 20 20 43  goto D.**      C
1913f 3a 20 69 6e 73 65 72 74 20 74 68 65 20 73 65 6c  : insert the sel
19140 65 63 74 20 72 65 73 75 6c 74 20 69 6e 74 6f 20  ect result into 
19141 74 68 65 20 69 6e 74 65 72 6d 65 64 69 61 74 65  the intermediate
19142 20 74 61 62 6c 65 0a 2a 2a 20 20 20 20 20 20 20   table.**       
19143 20 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20 20    return.**     
19144 20 42 3a 20 6f 70 65 6e 20 61 20 63 75 72 73 6f   B: open a curso
19145 72 20 74 6f 20 61 6e 20 69 6e 74 65 72 6d 65 64  r to an intermed
19146 69 61 74 65 20 74 61 62 6c 65 0a 2a 2a 20 20 20  iate table.**   
19147 20 20 20 20 20 20 67 6f 74 6f 20 41 0a 2a 2a 20        goto A.** 
19148 20 20 20 20 20 44 3a 20 6f 70 65 6e 20 77 72 69       D: open wri
19149 74 65 20 63 75 72 73 6f 72 20 74 6f 20 3c 74 61  te cursor to <ta
1914a 62 6c 65 3e 20 61 6e 64 20 69 74 73 20 69 6e 64  ble> and its ind
1914b 69 63 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  ices.**         
1914c 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 69 6e  loop over the in
1914d 74 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c 65  termediate table
1914e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 72  .**           tr
1914f 61 6e 73 66 65 72 20 76 61 6c 75 65 73 20 66 6f  ansfer values fo
19150 72 6d 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20  rm intermediate 
19151 74 61 62 6c 65 20 69 6e 74 6f 20 3c 74 61 62 6c  table into <tabl
19152 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20 20 65 6e  e>.**         en
19153 64 20 74 68 65 20 6c 6f 6f 70 0a 2a 2a 20 20 20  d the loop.**   
19154 20 20 20 20 20 20 63 6c 65 61 6e 75 70 0a 2a 2f        cleanup.*/
19155 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
19156 76 6f 69 64 20 73 71 6c 69 74 65 33 49 6e 73 65  void sqlite3Inse
19157 72 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  rt(.  Parse *pPa
19158 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50  rse,        /* P
19159 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
1915a 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62  .  SrcList *pTab
1915b 4c 69 73 74 2c 20 20 20 20 2f 2a 20 4e 61 6d 65  List,    /* Name
1915c 20 6f 66 20 74 61 62 6c 65 20 69 6e 74 6f 20 77   of table into w
1915d 68 69 63 68 20 77 65 20 61 72 65 20 69 6e 73 65  hich we are inse
1915e 72 74 69 6e 67 20 2a 2f 0a 20 20 45 78 70 72 4c  rting */.  ExprL
1915f 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20  ist *pList,     
19160 20 2f 2a 20 4c 69 73 74 20 6f 66 20 76 61 6c 75   /* List of valu
19161 65 73 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65  es to be inserte
19162 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  d */.  Select *p
19163 53 65 6c 65 63 74 2c 20 20 20 20 20 20 2f 2a 20  Select,      /* 
19164 41 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  A SELECT stateme
19165 6e 74 20 74 6f 20 75 73 65 20 61 73 20 74 68 65  nt to use as the
19166 20 64 61 74 61 20 73 6f 75 72 63 65 20 2a 2f 0a   data source */.
19167 20 20 49 64 4c 69 73 74 20 2a 70 43 6f 6c 75 6d    IdList *pColum
19168 6e 2c 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d  n,      /* Colum
19169 6e 20 6e 61 6d 65 73 20 63 6f 72 72 65 73 70 6f  n names correspo
1916a 6e 64 69 6e 67 20 74 6f 20 49 44 4c 49 53 54 2e  nding to IDLIST.
1916b 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f   */.  int onErro
1916c 72 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48  r           /* H
1916d 6f 77 20 74 6f 20 68 61 6e 64 6c 65 20 63 6f 6e  ow to handle con
1916e 73 74 72 61 69 6e 74 20 65 72 72 6f 72 73 20 2a  straint errors *
1916f 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54  /.){.  Table *pT
19170 61 62 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ab;          /* 
19171 54 68 65 20 74 61 62 6c 65 20 74 6f 20 69 6e 73  The table to ins
19172 65 72 74 20 69 6e 74 6f 20 2a 2f 0a 20 20 63 68  ert into */.  ch
19173 61 72 20 2a 7a 54 61 62 3b 20 20 20 20 20 20 20  ar *zTab;       
19174 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
19175 68 65 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68  he table into wh
19176 69 63 68 20 77 65 20 61 72 65 20 69 6e 73 65 72  ich we are inser
19177 74 69 6e 67 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ting */.  const 
19178 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20  char *zDb;      
19179 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64  /* Name of the d
1917a 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20  atabase holding 
1917b 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20  this table */.  
1917c 69 6e 74 20 69 2c 20 6a 2c 20 69 64 78 3b 20 20  int i, j, idx;  
1917d 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
1917e 75 6e 74 65 72 73 20 2a 2f 0a 20 20 56 64 62 65  unters */.  Vdbe
1917f 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20   *v;            
19180 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f    /* Generate co
19181 64 65 20 69 6e 74 6f 20 74 68 69 73 20 76 69 72  de into this vir
19182 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 2a 2f 0a  tual machine */.
19183 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20    Index *pIdx;  
19184 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c          /* For l
19185 6f 6f 70 69 6e 67 20 6f 76 65 72 20 69 6e 64 69  ooping over indi
19186 63 65 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ces of the table
19187 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d   */.  int nColum
19188 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  n;          /* N
19189 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
1918a 20 69 6e 20 74 68 65 20 64 61 74 61 20 2a 2f 0a   in the data */.
1918b 20 20 69 6e 74 20 62 61 73 65 20 3d 20 30 3b 20    int base = 0; 
1918c 20 20 20 20 20 20 20 20 2f 2a 20 56 44 42 45 20          /* VDBE 
1918d 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f  Cursor number fo
1918e 72 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20  r pTab */.  int 
1918f 69 43 6f 6e 74 3d 30 2c 69 42 72 65 61 6b 3d 30  iCont=0,iBreak=0
19190 3b 20 2f 2a 20 42 65 67 69 6e 6e 69 6e 67 20 61  ; /* Beginning a
19191 6e 64 20 65 6e 64 20 6f 66 20 74 68 65 20 6c 6f  nd end of the lo
19192 6f 70 20 6f 76 65 72 20 73 72 63 54 61 62 20 2a  op over srcTab *
19193 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  /.  sqlite3 *db;
19194 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
19195 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 73   main database s
19196 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e  tructure */.  in
19197 74 20 6b 65 79 43 6f 6c 75 6d 6e 20 3d 20 2d 31  t keyColumn = -1
19198 3b 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 74 68  ;   /* Column th
19199 61 74 20 69 73 20 74 68 65 20 49 4e 54 45 47 45  at is the INTEGE
1919a 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 2a 2f  R PRIMARY KEY */
1919b 0a 20 20 69 6e 74 20 65 6e 64 4f 66 4c 6f 6f 70  .  int endOfLoop
1919c 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 62 65  ;        /* Labe
1919d 6c 20 66 6f 72 20 74 68 65 20 65 6e 64 20 6f 66  l for the end of
1919e 20 74 68 65 20 69 6e 73 65 72 74 69 6f 6e 20 6c   the insertion l
1919f 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 75 73 65  oop */.  int use
191a0 54 65 6d 70 54 61 62 6c 65 20 3d 20 30 3b 20 2f  TempTable = 0; /
191a1 2a 20 53 74 6f 72 65 20 53 45 4c 45 43 54 20 72  * Store SELECT r
191a2 65 73 75 6c 74 73 20 69 6e 20 69 6e 74 65 72 6d  esults in interm
191a3 65 64 69 61 74 65 20 74 61 62 6c 65 20 2a 2f 0a  ediate table */.
191a4 20 20 69 6e 74 20 73 72 63 54 61 62 20 3d 20 30    int srcTab = 0
191a5 3b 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20  ;       /* Data 
191a6 63 6f 6d 65 73 20 66 72 6f 6d 20 74 68 69 73 20  comes from this 
191a7 74 65 6d 70 6f 72 61 72 79 20 63 75 72 73 6f 72  temporary cursor
191a8 20 69 66 20 3e 3d 30 20 2a 2f 0a 20 20 69 6e 74   if >=0 */.  int
191a9 20 69 53 65 6c 65 63 74 4c 6f 6f 70 20 3d 20 30   iSelectLoop = 0
191aa 3b 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66  ;  /* Address of
191ab 20 63 6f 64 65 20 74 68 61 74 20 69 6d 70 6c 65   code that imple
191ac 6d 65 6e 74 73 20 74 68 65 20 53 45 4c 45 43 54  ments the SELECT
191ad 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6c 65 61 6e   */.  int iClean
191ae 75 70 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 41  up = 0;     /* A
191af 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 63 6c  ddress of the cl
191b0 65 61 6e 75 70 20 63 6f 64 65 20 2a 2f 0a 20 20  eanup code */.  
191b1 69 6e 74 20 69 49 6e 73 65 72 74 42 6c 6f 63 6b  int iInsertBlock
191b2 20 3d 20 30 3b 20 2f 2a 20 41 64 64 72 65 73 73   = 0; /* Address
191b3 20 6f 66 20 74 68 65 20 73 75 62 72 6f 75 74 69   of the subrouti
191b4 6e 65 20 75 73 65 64 20 74 6f 20 69 6e 73 65 72  ne used to inser
191b5 74 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20  t data */.  int 
191b6 69 43 6e 74 4d 65 6d 20 3d 20 30 3b 20 20 20 20  iCntMem = 0;    
191b7 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c    /* Memory cell
191b8 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 72 6f   used for the ro
191b9 77 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69  w counter */.  i
191ba 6e 74 20 6e 65 77 49 64 78 20 3d 20 2d 31 3b 20  nt newIdx = -1; 
191bb 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 66       /* Cursor f
191bc 6f 72 20 74 68 65 20 4e 45 57 20 74 61 62 6c 65  or the NEW table
191bd 20 2a 2f 0a 20 20 44 62 20 2a 70 44 62 3b 20 20   */.  Db *pDb;  
191be 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
191bf 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74  he database cont
191c0 61 69 6e 69 6e 67 20 74 61 62 6c 65 20 62 65 69  aining table bei
191c1 6e 67 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f  ng inserted into
191c2 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 75 6e 74 65   */.  int counte
191c3 72 4d 65 6d 20 3d 20 30 3b 20 20 20 2f 2a 20 4d  rMem = 0;   /* M
191c4 65 6d 6f 72 79 20 63 65 6c 6c 20 68 6f 6c 64 69  emory cell holdi
191c5 6e 67 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54  ng AUTOINCREMENT
191c6 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e   counter */.  in
191c7 74 20 61 70 70 65 6e 64 46 6c 61 67 20 3d 20 30  t appendFlag = 0
191c8 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74  ;   /* True if t
191c9 68 65 20 69 6e 73 65 72 74 20 69 73 20 6c 69 6b  he insert is lik
191ca 65 6c 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70  ely to be an app
191cb 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62  end */.  int iDb
191cc 3b 0a 0a 20 20 69 6e 74 20 6e 48 69 64 64 65 6e  ;..  int nHidden
191cd 20 3d 20 30 3b 0a 0a 23 69 66 6e 64 65 66 20 53   = 0;..#ifndef S
191ce 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
191cf 45 52 0a 20 20 69 6e 74 20 69 73 56 69 65 77 3b  ER.  int isView;
191d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
191d1 20 2f 2a 20 54 72 75 65 20 69 66 20 61 74 74 65   /* True if atte
191d2 6d 70 74 69 6e 67 20 74 6f 20 69 6e 73 65 72 74  mpting to insert
191d3 20 69 6e 74 6f 20 61 20 76 69 65 77 20 2a 2f 0a   into a view */.
191d4 20 20 69 6e 74 20 74 72 69 67 67 65 72 73 5f 65    int triggers_e
191d5 78 69 73 74 20 3d 20 30 3b 20 20 20 20 20 2f 2a  xist = 0;     /*
191d6 20 54 72 75 65 20 69 66 20 74 68 65 72 65 20 61   True if there a
191d7 72 65 20 46 4f 52 20 45 41 43 48 20 52 4f 57 20  re FOR EACH ROW 
191d8 74 72 69 67 67 65 72 73 20 2a 2f 0a 23 65 6e 64  triggers */.#end
191d9 69 66 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65  if..  if( pParse
191da 2d 3e 6e 45 72 72 20 7c 7c 20 73 71 6c 69 74 65  ->nErr || sqlite
191db 33 4d 61 6c 6c 6f 63 46 61 69 6c 65 64 28 29 20  3MallocFailed() 
191dc 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65  ){.    goto inse
191dd 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a  rt_cleanup;.  }.
191de 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64    db = pParse->d
191df 62 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20  b;..  /* Locate 
191e0 74 68 65 20 74 61 62 6c 65 20 69 6e 74 6f 20 77  the table into w
191e1 68 69 63 68 20 77 65 20 77 69 6c 6c 20 62 65 20  hich we will be 
191e2 69 6e 73 65 72 74 69 6e 67 20 6e 65 77 20 69 6e  inserting new in
191e3 66 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a  formation..  */.
191e4 20 20 61 73 73 65 72 74 28 20 70 54 61 62 4c 69    assert( pTabLi
191e5 73 74 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20  st->nSrc==1 );. 
191e6 20 7a 54 61 62 20 3d 20 70 54 61 62 4c 69 73 74   zTab = pTabList
191e7 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  ->a[0].zName;.  
191e8 69 66 28 20 7a 54 61 62 3d 3d 30 20 29 20 67 6f  if( zTab==0 ) go
191e9 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75  to insert_cleanu
191ea 70 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69  p;.  pTab = sqli
191eb 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70  te3SrcListLookup
191ec 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73  (pParse, pTabLis
191ed 74 29 3b 0a 20 20 69 66 28 20 70 54 61 62 3d 3d  t);.  if( pTab==
191ee 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69 6e  0 ){.    goto in
191ef 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  sert_cleanup;.  
191f0 7d 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65  }.  iDb = sqlite
191f1 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64  3SchemaToIndex(d
191f2 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  b, pTab->pSchema
191f3 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62  );.  assert( iDb
191f4 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 70 44  <db->nDb );.  pD
191f5 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62  b = &db->aDb[iDb
191f6 5d 3b 0a 20 20 7a 44 62 20 3d 20 70 44 62 2d 3e  ];.  zDb = pDb->
191f7 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20 73 71 6c  zName;.  if( sql
191f8 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
191f9 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 49 4e 53  arse, SQLITE_INS
191fa 45 52 54 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ERT, pTab->zName
191fb 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20  , 0, zDb) ){.   
191fc 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65   goto insert_cle
191fd 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  anup;.  }..  /* 
191fe 46 69 67 75 72 65 20 6f 75 74 20 69 66 20 77 65  Figure out if we
191ff 20 68 61 76 65 20 61 6e 79 20 74 72 69 67 67 65   have any trigge
19200 72 73 20 61 6e 64 20 69 66 20 74 68 65 20 74 61  rs and if the ta
19201 62 6c 65 20 62 65 69 6e 67 0a 20 20 2a 2a 20 69  ble being.  ** i
19202 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 69 73 20  nserted into is 
19203 61 20 76 69 65 77 0a 20 20 2a 2f 0a 23 69 66 6e  a view.  */.#ifn
19204 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
19205 54 52 49 47 47 45 52 0a 20 20 74 72 69 67 67 65  TRIGGER.  trigge
19206 72 73 5f 65 78 69 73 74 20 3d 20 73 71 6c 69 74  rs_exist = sqlit
19207 65 33 54 72 69 67 67 65 72 73 45 78 69 73 74 28  e3TriggersExist(
19208 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 54 4b  pParse, pTab, TK
19209 5f 49 4e 53 45 52 54 2c 20 30 29 3b 0a 20 20 69  _INSERT, 0);.  i
1920a 73 56 69 65 77 20 3d 20 70 54 61 62 2d 3e 70 53  sView = pTab->pS
1920b 65 6c 65 63 74 21 3d 30 3b 0a 23 65 6c 73 65 0a  elect!=0;.#else.
1920c 23 20 64 65 66 69 6e 65 20 74 72 69 67 67 65 72  # define trigger
1920d 73 5f 65 78 69 73 74 20 30 0a 23 20 64 65 66 69  s_exist 0.# defi
1920e 6e 65 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64  ne isView 0.#end
1920f 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  if.#ifdef SQLITE
19210 5f 4f 4d 49 54 5f 56 49 45 57 0a 23 20 75 6e 64  _OMIT_VIEW.# und
19211 65 66 20 69 73 56 69 65 77 0a 23 20 64 65 66 69  ef isView.# defi
19212 6e 65 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64  ne isView 0.#end
19213 69 66 0a 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20  if..  /* Ensure 
19214 74 68 61 74 3a 0a 20 20 2a 20 20 28 61 29 20 74  that:.  *  (a) t
19215 68 65 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20  he table is not 
19216 72 65 61 64 2d 6f 6e 6c 79 2c 20 0a 20 20 2a 20  read-only, .  * 
19217 20 28 62 29 20 74 68 61 74 20 69 66 20 69 74 20   (b) that if it 
19218 69 73 20 61 20 76 69 65 77 20 74 68 65 6e 20 4f  is a view then O
19219 4e 20 49 4e 53 45 52 54 20 74 72 69 67 67 65 72  N INSERT trigger
1921a 73 20 65 78 69 73 74 0a 20 20 2a 2f 0a 20 20 69  s exist.  */.  i
1921b 66 28 20 73 71 6c 69 74 65 33 49 73 52 65 61 64  f( sqlite3IsRead
1921c 4f 6e 6c 79 28 70 50 61 72 73 65 2c 20 70 54 61  Only(pParse, pTa
1921d 62 2c 20 74 72 69 67 67 65 72 73 5f 65 78 69 73  b, triggers_exis
1921e 74 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69  t) ){.    goto i
1921f 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20  nsert_cleanup;. 
19220 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 54 61   }.  assert( pTa
19221 62 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66  b!=0 );..  /* If
19222 20 70 54 61 62 20 69 73 20 72 65 61 6c 6c 79 20   pTab is really 
19223 61 20 76 69 65 77 2c 20 6d 61 6b 65 20 73 75 72  a view, make sur
19224 65 20 69 74 20 68 61 73 20 62 65 65 6e 20 69 6e  e it has been in
19225 69 74 69 61 6c 69 7a 65 64 2e 0a 20 20 2a 2a 20  itialized..  ** 
19226 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d  ViewGetColumnNam
19227 65 73 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70 20  es() is a no-op 
19228 69 66 20 70 54 61 62 20 69 73 20 6e 6f 74 20 61  if pTab is not a
19229 20 76 69 65 77 20 28 6f 72 20 76 69 72 74 75 61   view (or virtua
1922a 6c 20 0a 20 20 2a 2a 20 6d 6f 64 75 6c 65 20 74  l .  ** module t
1922b 61 62 6c 65 29 2e 0a 20 20 2a 2f 0a 20 20 69 66  able)..  */.  if
1922c 28 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74  ( sqlite3ViewGet
1922d 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72  ColumnNames(pPar
1922e 73 65 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20  se, pTab) ){.   
1922f 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65   goto insert_cle
19230 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  anup;.  }..  /* 
19231 41 6c 6c 6f 63 61 74 65 20 61 20 56 44 42 45 0a  Allocate a VDBE.
19232 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74    */.  v = sqlit
19233 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
19234 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20  );.  if( v==0 ) 
19235 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61  goto insert_clea
19236 6e 75 70 3b 0a 20 20 69 66 28 20 70 50 61 72 73  nup;.  if( pPars
19237 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 20 73  e->nested==0 ) s
19238 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e 74 43  qlite3VdbeCountC
19239 68 61 6e 67 65 73 28 76 29 3b 0a 20 20 73 71 6c  hanges(v);.  sql
1923a 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70  ite3BeginWriteOp
1923b 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20  eration(pParse, 
1923c 70 53 65 6c 65 63 74 20 7c 7c 20 74 72 69 67 67  pSelect || trigg
1923d 65 72 73 5f 65 78 69 73 74 2c 20 69 44 62 29 3b  ers_exist, iDb);
1923e 0a 0a 20 20 2f 2a 20 69 66 20 74 68 65 72 65 20  ..  /* if there 
1923f 61 72 65 20 72 6f 77 20 74 72 69 67 67 65 72 73  are row triggers
19240 2c 20 61 6c 6c 6f 63 61 74 65 20 61 20 74 65 6d  , allocate a tem
19241 70 20 74 61 62 6c 65 20 66 6f 72 20 6e 65 77 2e  p table for new.
19242 2a 20 72 65 66 65 72 65 6e 63 65 73 2e 20 2a 2f  * references. */
19243 0a 20 20 69 66 28 20 74 72 69 67 67 65 72 73 5f  .  if( triggers_
19244 65 78 69 73 74 20 29 7b 0a 20 20 20 20 6e 65 77  exist ){.    new
19245 49 64 78 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  Idx = pParse->nT
19246 61 62 2b 2b 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64  ab++;.  }..#ifnd
19247 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 58  ef SQLITE_OMIT_X
19248 46 45 52 5f 4f 50 54 0a 20 20 2f 2a 20 49 66 20  FER_OPT.  /* If 
19249 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73  the statement is
1924a 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 20 20 2a   of the form.  *
1924b 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 49 4e 53  *.  **       INS
1924c 45 52 54 20 49 4e 54 4f 20 3c 74 61 62 6c 65 31  ERT INTO <table1
1924d 3e 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20  > SELECT * FROM 
1924e 3c 74 61 62 6c 65 32 3e 3b 0a 20 20 2a 2a 0a 20  <table2>;.  **. 
1924f 20 2a 2a 20 54 68 65 6e 20 73 70 65 63 69 61 6c   ** Then special
19250 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 63   optimizations c
19251 61 6e 20 62 65 20 61 70 70 6c 69 65 64 20 74 68  an be applied th
19252 61 74 20 6d 61 6b 65 20 74 68 65 20 74 72 61 6e  at make the tran
19253 73 66 65 72 0a 20 20 2a 2a 20 76 65 72 79 20 66  sfer.  ** very f
19254 61 73 74 20 61 6e 64 20 77 68 69 63 68 20 72 65  ast and which re
19255 64 75 63 65 20 66 72 61 67 6d 65 6e 74 61 74 69  duce fragmentati
19256 6f 6e 20 6f 66 20 69 6e 64 69 63 65 73 2e 0a 20  on of indices.. 
19257 20 2a 2f 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d   */.  if( pColum
19258 6e 3d 3d 30 20 26 26 20 78 66 65 72 4f 70 74 69  n==0 && xferOpti
19259 6d 69 7a 61 74 69 6f 6e 28 70 50 61 72 73 65 2c  mization(pParse,
1925a 20 70 54 61 62 2c 20 70 53 65 6c 65 63 74 2c 20   pTab, pSelect, 
1925b 6f 6e 45 72 72 6f 72 2c 20 69 44 62 29 20 29 7b  onError, iDb) ){
1925c 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 74 72  .    assert( !tr
1925d 69 67 67 65 72 73 5f 65 78 69 73 74 20 29 3b 0a  iggers_exist );.
1925e 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 69 73      assert( pLis
1925f 74 3d 3d 30 20 29 3b 0a 20 20 20 20 67 6f 74 6f  t==0 );.    goto
19260 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b   insert_cleanup;
19261 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  .  }.#endif /* S
19262 51 4c 49 54 45 5f 4f 4d 49 54 5f 58 46 45 52 5f  QLITE_OMIT_XFER_
19263 4f 50 54 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20  OPT */..  /* If 
19264 74 68 69 73 20 69 73 20 61 6e 20 41 55 54 4f 49  this is an AUTOI
19265 4e 43 52 45 4d 45 4e 54 20 74 61 62 6c 65 2c 20  NCREMENT table, 
19266 6c 6f 6f 6b 20 75 70 20 74 68 65 20 73 65 71 75  look up the sequ
19267 65 6e 63 65 20 6e 75 6d 62 65 72 20 69 6e 20 74  ence number in t
19268 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f 73  he.  ** sqlite_s
19269 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 61 6e  equence table an
1926a 64 20 73 74 6f 72 65 20 69 74 20 69 6e 20 6d 65  d store it in me
1926b 6d 6f 72 79 20 63 65 6c 6c 20 63 6f 75 6e 74 65  mory cell counte
1926c 72 4d 65 6d 2e 20 20 41 6c 73 6f 0a 20 20 2a 2a  rMem.  Also.  **
1926d 20 72 65 6d 65 6d 62 65 72 20 74 68 65 20 72 6f   remember the ro
1926e 77 69 64 20 6f 66 20 74 68 65 20 73 71 6c 69 74  wid of the sqlit
1926f 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65  e_sequence table
19270 20 65 6e 74 72 79 20 69 6e 20 6d 65 6d 6f 72 79   entry in memory
19271 20 63 65 6c 6c 0a 20 20 2a 2a 20 63 6f 75 6e 74   cell.  ** count
19272 65 72 52 6f 77 69 64 2e 0a 20 20 2a 2f 0a 20 20  erRowid..  */.  
19273 63 6f 75 6e 74 65 72 4d 65 6d 20 3d 20 61 75 74  counterMem = aut
19274 6f 49 6e 63 42 65 67 69 6e 28 70 50 61 72 73 65  oIncBegin(pParse
19275 2c 20 69 44 62 2c 20 70 54 61 62 29 3b 0a 0a 20  , iDb, pTab);.. 
19276 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68   /* Figure out h
19277 6f 77 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73 20  ow many columns 
19278 6f 66 20 64 61 74 61 20 61 72 65 20 73 75 70 70  of data are supp
19279 6c 69 65 64 2e 20 20 49 66 20 74 68 65 20 64 61  lied.  If the da
1927a 74 61 0a 20 20 2a 2a 20 69 73 20 63 6f 6d 69 6e  ta.  ** is comin
1927b 67 20 66 72 6f 6d 20 61 20 53 45 4c 45 43 54 20  g from a SELECT 
1927c 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20  statement, then 
1927d 74 68 69 73 20 73 74 65 70 20 61 6c 73 6f 20 67  this step also g
1927e 65 6e 65 72 61 74 65 73 0a 20 20 2a 2a 20 61 6c  enerates.  ** al
1927f 6c 20 74 68 65 20 63 6f 64 65 20 74 6f 20 69 6d  l the code to im
19280 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 45 4c 45  plement the SELE
19281 43 54 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64  CT statement and
19282 20 69 6e 76 6f 6b 65 20 61 20 73 75 62 72 6f 75   invoke a subrou
19283 74 69 6e 65 0a 20 20 2a 2a 20 74 6f 20 70 72 6f  tine.  ** to pro
19284 63 65 73 73 20 65 61 63 68 20 72 6f 77 20 6f 66  cess each row of
19285 20 74 68 65 20 72 65 73 75 6c 74 2e 20 28 54 65   the result. (Te
19286 6d 70 6c 61 74 65 20 32 2e 29 20 49 66 20 74 68  mplate 2.) If th
19287 65 20 53 45 4c 45 43 54 0a 20 20 2a 2a 20 73 74  e SELECT.  ** st
19288 61 74 65 6d 65 6e 74 20 75 73 65 73 20 74 68 65  atement uses the
19289 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20   the table that 
1928a 69 73 20 62 65 69 6e 67 20 69 6e 73 65 72 74 65  is being inserte
1928b 64 20 69 6e 74 6f 2c 20 74 68 65 6e 20 74 68 65  d into, then the
1928c 0a 20 20 2a 2a 20 73 75 62 72 6f 75 74 69 6e 65  .  ** subroutine
1928d 20 69 73 20 61 6c 73 6f 20 63 6f 64 65 64 20 68   is also coded h
1928e 65 72 65 2e 20 20 54 68 61 74 20 73 75 62 72 6f  ere.  That subro
1928f 75 74 69 6e 65 20 73 74 6f 72 65 73 20 74 68 65  utine stores the
19290 20 53 45 4c 45 43 54 0a 20 20 2a 2a 20 72 65 73   SELECT.  ** res
19291 75 6c 74 73 20 69 6e 20 61 20 74 65 6d 70 6f 72  ults in a tempor
19292 61 72 79 20 74 61 62 6c 65 2e 20 28 54 65 6d 70  ary table. (Temp
19293 6c 61 74 65 20 33 2e 29 0a 20 20 2a 2f 0a 20 20  late 3.).  */.  
19294 69 66 28 20 70 53 65 6c 65 63 74 20 29 7b 0a 20  if( pSelect ){. 
19295 20 20 20 2f 2a 20 44 61 74 61 20 69 73 20 63 6f     /* Data is co
19296 6d 69 6e 67 20 66 72 6f 6d 20 61 20 53 45 4c 45  ming from a SELE
19297 43 54 2e 20 20 47 65 6e 65 72 61 74 65 20 63 6f  CT.  Generate co
19298 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  de to implement 
19299 74 68 61 74 20 53 45 4c 45 43 54 0a 20 20 20 20  that SELECT.    
1929a 2a 2f 0a 20 20 20 20 69 6e 74 20 72 63 2c 20 69  */.    int rc, i
1929b 49 6e 69 74 43 6f 64 65 3b 0a 20 20 20 20 69 49  InitCode;.    iI
1929c 6e 69 74 43 6f 64 65 20 3d 20 73 71 6c 69 74 65  nitCode = sqlite
1929d 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1929e 5f 47 6f 74 6f 2c 20 30 2c 20 30 29 3b 0a 20 20  _Goto, 0, 0);.  
1929f 20 20 69 53 65 6c 65 63 74 4c 6f 6f 70 20 3d 20    iSelectLoop = 
192a0 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
192a1 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 69  ntAddr(v);.    i
192a2 49 6e 73 65 72 74 42 6c 6f 63 6b 20 3d 20 73 71  InsertBlock = sq
192a3 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
192a4 65 6c 28 76 29 3b 0a 0a 20 20 20 20 2f 2a 20 52  el(v);..    /* R
192a5 65 73 6f 6c 76 65 20 74 68 65 20 65 78 70 72 65  esolve the expre
192a6 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 53 45  ssions in the SE
192a7 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 61  LECT statement a
192a8 6e 64 20 65 78 65 63 75 74 65 20 69 74 2e 20 2a  nd execute it. *
192a9 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  /.    rc = sqlit
192aa 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c  e3Select(pParse,
192ab 20 70 53 65 6c 65 63 74 2c 20 53 52 54 5f 53 75   pSelect, SRT_Su
192ac 62 72 6f 75 74 69 6e 65 2c 20 69 49 6e 73 65 72  broutine, iInser
192ad 74 42 6c 6f 63 6b 2c 30 2c 30 2c 30 2c 30 29 3b  tBlock,0,0,0,0);
192ae 0a 20 20 20 20 69 66 28 20 72 63 20 7c 7c 20 70  .    if( rc || p
192af 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 73  Parse->nErr || s
192b0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69 6c  qlite3MallocFail
192b1 65 64 28 29 20 29 7b 0a 20 20 20 20 20 20 67 6f  ed() ){.      go
192b2 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75  to insert_cleanu
192b3 70 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 43  p;.    }..    iC
192b4 6c 65 61 6e 75 70 20 3d 20 73 71 6c 69 74 65 33  leanup = sqlite3
192b5 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29  VdbeMakeLabel(v)
192b6 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
192b7 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 47 6f 74  eAddOp(v, OP_Got
192b8 6f 2c 20 30 2c 20 69 43 6c 65 61 6e 75 70 29 3b  o, 0, iCleanup);
192b9 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 53 65  .    assert( pSe
192ba 6c 65 63 74 2d 3e 70 45 4c 69 73 74 20 29 3b 0a  lect->pEList );.
192bb 20 20 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20 70 53      nColumn = pS
192bc 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 6e  elect->pEList->n
192bd 45 78 70 72 3b 0a 0a 20 20 20 20 2f 2a 20 53 65  Expr;..    /* Se
192be 74 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20 74  t useTempTable t
192bf 6f 20 54 52 55 45 20 69 66 20 74 68 65 20 72 65  o TRUE if the re
192c0 73 75 6c 74 20 6f 66 20 74 68 65 20 53 45 4c 45  sult of the SELE
192c1 43 54 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 20  CT statement.   
192c2 20 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 77 72   ** should be wr
192c3 69 74 74 65 6e 20 69 6e 74 6f 20 61 20 74 65 6d  itten into a tem
192c4 70 6f 72 61 72 79 20 74 61 62 6c 65 2e 20 20 53  porary table.  S
192c5 65 74 20 74 6f 20 46 41 4c 53 45 20 69 66 20 65  et to FALSE if e
192c6 61 63 68 0a 20 20 20 20 2a 2a 20 72 6f 77 20 6f  ach.    ** row o
192c7 66 20 74 68 65 20 53 45 4c 45 43 54 20 63 61 6e  f the SELECT can
192c8 20 62 65 20 77 72 69 74 74 65 6e 20 64 69 72 65   be written dire
192c9 63 74 6c 79 20 69 6e 74 6f 20 74 68 65 20 72 65  ctly into the re
192ca 73 75 6c 74 20 74 61 62 6c 65 2e 0a 20 20 20 20  sult table..    
192cb 2a 2a 0a 20 20 20 20 2a 2a 20 41 20 74 65 6d 70  **.    ** A temp
192cc 20 74 61 62 6c 65 20 6d 75 73 74 20 62 65 20 75   table must be u
192cd 73 65 64 20 69 66 20 74 68 65 20 74 61 62 6c 65  sed if the table
192ce 20 62 65 69 6e 67 20 75 70 64 61 74 65 64 20 69   being updated i
192cf 73 20 61 6c 73 6f 20 6f 6e 65 0a 20 20 20 20 2a  s also one.    *
192d0 2a 20 6f 66 20 74 68 65 20 74 61 62 6c 65 73 20  * of the tables 
192d1 62 65 69 6e 67 20 72 65 61 64 20 62 79 20 74 68  being read by th
192d2 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
192d3 6e 74 2e 20 20 41 6c 73 6f 20 75 73 65 20 61 20  nt.  Also use a 
192d4 0a 20 20 20 20 2a 2a 20 74 65 6d 70 20 74 61 62  .    ** temp tab
192d5 6c 65 20 69 6e 20 74 68 65 20 63 61 73 65 20 6f  le in the case o
192d6 66 20 72 6f 77 20 74 72 69 67 67 65 72 73 2e 0a  f row triggers..
192d7 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 74      */.    if( t
192d8 72 69 67 67 65 72 73 5f 65 78 69 73 74 20 7c 7c  riggers_exist ||
192d9 20 73 65 6c 65 63 74 52 65 61 64 73 54 61 62 6c   selectReadsTabl
192da 65 28 70 53 65 6c 65 63 74 2c 70 54 61 62 2d 3e  e(pSelect,pTab->
192db 70 53 63 68 65 6d 61 2c 70 54 61 62 2d 3e 74 6e  pSchema,pTab->tn
192dc 75 6d 29 20 29 7b 0a 20 20 20 20 20 20 75 73 65  um) ){.      use
192dd 54 65 6d 70 54 61 62 6c 65 20 3d 20 31 3b 0a 20  TempTable = 1;. 
192de 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 75 73     }..    if( us
192df 65 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20  eTempTable ){.  
192e0 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20      /* Generate 
192e1 74 68 65 20 73 75 62 72 6f 75 74 69 6e 65 20 74  the subroutine t
192e2 68 61 74 20 53 45 4c 45 43 54 20 63 61 6c 6c 73  hat SELECT calls
192e3 20 74 6f 20 70 72 6f 63 65 73 73 20 65 61 63 68   to process each
192e4 20 72 6f 77 20 6f 66 0a 20 20 20 20 20 20 2a 2a   row of.      **
192e5 20 74 68 65 20 72 65 73 75 6c 74 2e 20 20 53 74   the result.  St
192e6 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69  ore the result i
192e7 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61  n a temporary ta
192e8 62 6c 65 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ble.      */.   
192e9 20 20 20 73 72 63 54 61 62 20 3d 20 70 50 61 72     srcTab = pPar
192ea 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20  se->nTab++;.    
192eb 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
192ec 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 49 6e  olveLabel(v, iIn
192ed 73 65 72 74 42 6c 6f 63 6b 29 3b 0a 20 20 20 20  sertBlock);.    
192ee 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
192ef 4f 70 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63  Op(v, OP_MakeRec
192f0 6f 72 64 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 30 29  ord, nColumn, 0)
192f1 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
192f2 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4e  dbeAddOp(v, OP_N
192f3 65 77 52 6f 77 69 64 2c 20 73 72 63 54 61 62 2c  ewRowid, srcTab,
192f4 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   0);.      sqlit
192f5 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
192f6 50 5f 50 75 6c 6c 2c 20 31 2c 20 30 29 3b 0a 20  P_Pull, 1, 0);. 
192f7 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
192f8 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 73 65  AddOp(v, OP_Inse
192f9 72 74 2c 20 73 72 63 54 61 62 2c 20 4f 50 46 4c  rt, srcTab, OPFL
192fa 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20 20 20  AG_APPEND);.    
192fb 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
192fc 4f 70 28 76 2c 20 4f 50 5f 52 65 74 75 72 6e 2c  Op(v, OP_Return,
192fd 20 30 2c 20 30 29 3b 0a 0a 20 20 20 20 20 20 2f   0, 0);..      /
192fe 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
192ff 63 6f 64 65 20 72 75 6e 73 20 66 69 72 73 74 20  code runs first 
19300 62 65 63 61 75 73 65 20 74 68 65 20 47 4f 54 4f  because the GOTO
19301 20 61 74 20 74 68 65 20 76 65 72 79 20 74 6f 70   at the very top
19302 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65  .      ** of the
19303 20 70 72 6f 67 72 61 6d 20 6a 75 6d 70 73 20 74   program jumps t
19304 6f 20 69 74 2e 20 20 43 72 65 61 74 65 20 74 68  o it.  Create th
19305 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  e temporary tabl
19306 65 2c 20 74 68 65 6e 20 6a 75 6d 70 0a 20 20 20  e, then jump.   
19307 20 20 20 2a 2a 20 62 61 63 6b 20 75 70 20 61 6e     ** back up an
19308 64 20 65 78 65 63 75 74 65 20 74 68 65 20 53 45  d execute the SE
19309 4c 45 43 54 20 63 6f 64 65 20 61 62 6f 76 65 2e  LECT code above.
1930a 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
1930b 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
1930c 65 72 65 28 76 2c 20 69 49 6e 69 74 43 6f 64 65  ere(v, iInitCode
1930d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1930e 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1930f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 73  OpenEphemeral, s
19310 72 63 54 61 62 2c 20 30 29 3b 0a 20 20 20 20 20  rcTab, 0);.     
19311 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
19312 70 28 76 2c 20 4f 50 5f 53 65 74 4e 75 6d 43 6f  p(v, OP_SetNumCo
19313 6c 75 6d 6e 73 2c 20 73 72 63 54 61 62 2c 20 6e  lumns, srcTab, n
19314 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 73  Column);.      s
19315 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
19316 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69  v, OP_Goto, 0, i
19317 53 65 6c 65 63 74 4c 6f 6f 70 29 3b 0a 20 20 20  SelectLoop);.   
19318 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65     sqlite3VdbeRe
19319 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 43  solveLabel(v, iC
1931a 6c 65 61 6e 75 70 29 3b 0a 20 20 20 20 7d 65 6c  leanup);.    }el
1931b 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
1931c 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
1931d 20 69 49 6e 69 74 43 6f 64 65 29 3b 0a 20 20 20   iInitCode);.   
1931e 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
1931f 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 63  /* This is the c
19320 61 73 65 20 69 66 20 74 68 65 20 64 61 74 61 20  ase if the data 
19321 66 6f 72 20 74 68 65 20 49 4e 53 45 52 54 20 69  for the INSERT i
19322 73 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 61 20  s coming from a 
19323 56 41 4c 55 45 53 0a 20 20 20 20 2a 2a 20 63 6c  VALUES.    ** cl
19324 61 75 73 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ause.    */.    
19325 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b  NameContext sNC;
19326 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 73 4e 43  .    memset(&sNC
19327 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29  , 0, sizeof(sNC)
19328 29 3b 0a 20 20 20 20 73 4e 43 2e 70 50 61 72 73  );.    sNC.pPars
19329 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 20 20  e = pParse;.    
1932a 73 72 63 54 61 62 20 3d 20 2d 31 3b 0a 20 20 20  srcTab = -1;.   
1932b 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20 3d 20   useTempTable = 
1932c 30 3b 0a 20 20 20 20 6e 43 6f 6c 75 6d 6e 20 3d  0;.    nColumn =
1932d 20 70 4c 69 73 74 20 3f 20 70 4c 69 73 74 2d 3e   pList ? pList->
1932e 6e 45 78 70 72 20 3a 20 30 3b 0a 20 20 20 20 66  nExpr : 0;.    f
1932f 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 75 6d  or(i=0; i<nColum
19330 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  n; i++){.      i
19331 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 52 65  f( sqlite3ExprRe
19332 73 6f 6c 76 65 4e 61 6d 65 73 28 26 73 4e 43 2c  solveNames(&sNC,
19333 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78   pList->a[i].pEx
19334 70 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 67  pr) ){.        g
19335 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e  oto insert_clean
19336 75 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  up;.      }.    
19337 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65  }.  }..  /* Make
19338 20 73 75 72 65 20 74 68 65 20 6e 75 6d 62 65 72   sure the number
19339 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74   of columns in t
1933a 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 20 6d  he source data m
1933b 61 74 63 68 65 73 20 74 68 65 20 6e 75 6d 62 65  atches the numbe
1933c 72 0a 20 20 2a 2a 20 6f 66 20 63 6f 6c 75 6d 6e  r.  ** of column
1933d 73 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64  s to be inserted
1933e 20 69 6e 74 6f 20 74 68 65 20 74 61 62 6c 65 2e   into the table.
1933f 0a 20 20 2a 2f 0a 20 20 69 66 28 20 49 73 56 69  .  */.  if( IsVi
19340 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20  rtual(pTab) ){. 
19341 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54     for(i=0; i<pT
19342 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a  ab->nCol; i++){.
19343 20 20 20 20 20 20 6e 48 69 64 64 65 6e 20 2b 3d        nHidden +=
19344 20 28 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d 6e   (IsHiddenColumn
19345 28 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 29  (&pTab->aCol[i])
19346 20 3f 20 31 20 3a 20 30 29 3b 0a 20 20 20 20 7d   ? 1 : 0);.    }
19347 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 6f 6c 75  .  }.  if( pColu
19348 6d 6e 3d 3d 30 20 26 26 20 6e 43 6f 6c 75 6d 6e  mn==0 && nColumn
19349 20 26 26 20 6e 43 6f 6c 75 6d 6e 21 3d 28 70 54   && nColumn!=(pT
1934a 61 62 2d 3e 6e 43 6f 6c 2d 6e 48 69 64 64 65 6e  ab->nCol-nHidden
1934b 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) ){.    sqlite3
1934c 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
1934d 20 0a 20 20 20 20 20 20 20 22 74 61 62 6c 65 20   .       "table 
1934e 25 53 20 68 61 73 20 25 64 20 63 6f 6c 75 6d 6e  %S has %d column
1934f 73 20 62 75 74 20 25 64 20 76 61 6c 75 65 73 20  s but %d values 
19350 77 65 72 65 20 73 75 70 70 6c 69 65 64 22 2c 0a  were supplied",.
19351 20 20 20 20 20 20 20 70 54 61 62 4c 69 73 74 2c         pTabList,
19352 20 30 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20   0, pTab->nCol, 
19353 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 67 6f  nColumn);.    go
19354 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75  to insert_cleanu
19355 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 6f  p;.  }.  if( pCo
19356 6c 75 6d 6e 21 3d 30 20 26 26 20 6e 43 6f 6c 75  lumn!=0 && nColu
19357 6d 6e 21 3d 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64  mn!=pColumn->nId
19358 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
19359 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
1935a 22 25 64 20 76 61 6c 75 65 73 20 66 6f 72 20 25  "%d values for %
1935b 64 20 63 6f 6c 75 6d 6e 73 22 2c 20 6e 43 6f 6c  d columns", nCol
1935c 75 6d 6e 2c 20 70 43 6f 6c 75 6d 6e 2d 3e 6e 49  umn, pColumn->nI
1935d 64 29 3b 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73  d);.    goto ins
1935e 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  ert_cleanup;.  }
1935f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 49 4e  ..  /* If the IN
19360 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 20 69  SERT statement i
19361 6e 63 6c 75 64 65 64 20 61 6e 20 49 44 4c 49 53  ncluded an IDLIS
19362 54 20 74 65 72 6d 2c 20 74 68 65 6e 20 6d 61 6b  T term, then mak
19363 65 20 73 75 72 65 0a 20 20 2a 2a 20 61 6c 6c 20  e sure.  ** all 
19364 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20  elements of the 
19365 49 44 4c 49 53 54 20 72 65 61 6c 6c 79 20 61 72  IDLIST really ar
19366 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65  e columns of the
19367 20 74 61 62 6c 65 20 61 6e 64 20 0a 20 20 2a 2a   table and .  **
19368 20 72 65 6d 65 6d 62 65 72 20 74 68 65 20 63 6f   remember the co
19369 6c 75 6d 6e 20 69 6e 64 69 63 65 73 2e 0a 20 20  lumn indices..  
1936a 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 74  **.  ** If the t
1936b 61 62 6c 65 20 68 61 73 20 61 6e 20 49 4e 54 45  able has an INTE
1936c 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20  GER PRIMARY KEY 
1936d 63 6f 6c 75 6d 6e 20 61 6e 64 20 74 68 61 74 20  column and that 
1936e 63 6f 6c 75 6d 6e 0a 20 20 2a 2a 20 69 73 20 6e  column.  ** is n
1936f 61 6d 65 64 20 69 6e 20 74 68 65 20 49 44 4c 49  amed in the IDLI
19370 53 54 2c 20 74 68 65 6e 20 72 65 63 6f 72 64 20  ST, then record 
19371 69 6e 20 74 68 65 20 6b 65 79 43 6f 6c 75 6d 6e  in the keyColumn
19372 20 76 61 72 69 61 62 6c 65 0a 20 20 2a 2a 20 74   variable.  ** t
19373 68 65 20 69 6e 64 65 78 20 69 6e 74 6f 20 49 44  he index into ID
19374 4c 49 53 54 20 6f 66 20 74 68 65 20 70 72 69 6d  LIST of the prim
19375 61 72 79 20 6b 65 79 20 63 6f 6c 75 6d 6e 2e 20  ary key column. 
19376 20 6b 65 79 43 6f 6c 75 6d 6e 20 69 73 0a 20 20   keyColumn is.  
19377 2a 2a 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  ** the index of 
19378 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20  the primary key 
19379 61 73 20 69 74 20 61 70 70 65 61 72 73 20 69 6e  as it appears in
1937a 20 49 44 4c 49 53 54 2c 20 6e 6f 74 20 61 73 0a   IDLIST, not as.
1937b 20 20 2a 2a 20 69 73 20 61 70 70 65 61 72 73 20    ** is appears 
1937c 69 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  in the original 
1937d 74 61 62 6c 65 2e 20 20 28 54 68 65 20 69 6e 64  table.  (The ind
1937e 65 78 20 6f 66 20 74 68 65 20 70 72 69 6d 61 72  ex of the primar
1937f 79 0a 20 20 2a 2a 20 6b 65 79 20 69 6e 20 74 68  y.  ** key in th
19380 65 20 6f 72 69 67 69 6e 61 6c 20 74 61 62 6c 65  e original table
19381 20 69 73 20 70 54 61 62 2d 3e 69 50 4b 65 79 2e   is pTab->iPKey.
19382 29 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 43 6f  ).  */.  if( pCo
19383 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 66 6f 72 28  lumn ){.    for(
19384 69 3d 30 3b 20 69 3c 70 43 6f 6c 75 6d 6e 2d 3e  i=0; i<pColumn->
19385 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  nId; i++){.     
19386 20 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 69   pColumn->a[i].i
19387 64 78 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20  dx = -1;.    }. 
19388 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43     for(i=0; i<pC
19389 6f 6c 75 6d 6e 2d 3e 6e 49 64 3b 20 69 2b 2b 29  olumn->nId; i++)
1938a 7b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b  {.      for(j=0;
1938b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a   j<pTab->nCol; j
1938c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ++){.        if(
1938d 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
1938e 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 7a 4e  pColumn->a[i].zN
1938f 61 6d 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b  ame, pTab->aCol[
19390 6a 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a  j].zName)==0 ){.
19391 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 75 6d            pColum
19392 6e 2d 3e 61 5b 69 5d 2e 69 64 78 20 3d 20 6a 3b  n->a[i].idx = j;
19393 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6a  .          if( j
19394 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b  ==pTab->iPKey ){
19395 0a 20 20 20 20 20 20 20 20 20 20 20 20 6b 65 79  .            key
19396 43 6f 6c 75 6d 6e 20 3d 20 69 3b 0a 20 20 20 20  Column = i;.    
19397 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
19398 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
19399 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
1939a 20 69 66 28 20 6a 3e 3d 70 54 61 62 2d 3e 6e 43   if( j>=pTab->nC
1939b 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  ol ){.        if
1939c 28 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64  ( sqlite3IsRowid
1939d 28 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 7a  (pColumn->a[i].z
1939e 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 20  Name) ){.       
1939f 20 20 20 6b 65 79 43 6f 6c 75 6d 6e 20 3d 20 69     keyColumn = i
193a0 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
193a1 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
193a2 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
193a3 65 2c 20 22 74 61 62 6c 65 20 25 53 20 68 61 73  e, "table %S has
193a4 20 6e 6f 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64   no column named
193a5 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20 20   %s",.          
193a6 20 20 20 20 70 54 61 62 4c 69 73 74 2c 20 30 2c      pTabList, 0,
193a7 20 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 7a   pColumn->a[i].z
193a8 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20  Name);.         
193a9 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b   pParse->nErr++;
193aa 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
193ab 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a  insert_cleanup;.
193ac 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
193ad 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  }.    }.  }..  /
193ae 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f  * If there is no
193af 20 49 44 4c 49 53 54 20 74 65 72 6d 20 62 75 74   IDLIST term but
193b0 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 61   the table has a
193b1 6e 20 69 6e 74 65 67 65 72 20 70 72 69 6d 61 72  n integer primar
193b2 79 0a 20 20 2a 2a 20 6b 65 79 2c 20 74 68 65 20  y.  ** key, the 
193b3 73 65 74 20 74 68 65 20 6b 65 79 43 6f 6c 75 6d  set the keyColum
193b4 6e 20 76 61 72 69 61 62 6c 65 20 74 6f 20 74 68  n variable to th
193b5 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 63 6f  e primary key co
193b6 6c 75 6d 6e 20 69 6e 64 65 78 0a 20 20 2a 2a 20  lumn index.  ** 
193b7 69 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  in the original 
193b8 74 61 62 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e  table definition
193b9 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 43 6f  ..  */.  if( pCo
193ba 6c 75 6d 6e 3d 3d 30 20 26 26 20 6e 43 6f 6c 75  lumn==0 && nColu
193bb 6d 6e 3e 30 20 29 7b 0a 20 20 20 20 6b 65 79 43  mn>0 ){.    keyC
193bc 6f 6c 75 6d 6e 20 3d 20 70 54 61 62 2d 3e 69 50  olumn = pTab->iP
193bd 4b 65 79 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f  Key;.  }..  /* O
193be 70 65 6e 20 74 68 65 20 74 65 6d 70 20 74 61 62  pen the temp tab
193bf 6c 65 20 66 6f 72 20 46 4f 52 20 45 41 43 48 20  le for FOR EACH 
193c0 52 4f 57 20 74 72 69 67 67 65 72 73 0a 20 20 2a  ROW triggers.  *
193c1 2f 0a 20 20 69 66 28 20 74 72 69 67 67 65 72 73  /.  if( triggers
193c2 5f 65 78 69 73 74 20 29 7b 0a 20 20 20 20 73 71  _exist ){.    sq
193c3 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
193c4 2c 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f 2c  , OP_OpenPseudo,
193c5 20 6e 65 77 49 64 78 2c 20 30 29 3b 0a 20 20 20   newIdx, 0);.   
193c6 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
193c7 70 28 76 2c 20 4f 50 5f 53 65 74 4e 75 6d 43 6f  p(v, OP_SetNumCo
193c8 6c 75 6d 6e 73 2c 20 6e 65 77 49 64 78 2c 20 70  lumns, newIdx, p
193c9 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 7d 0a  Tab->nCol);.  }.
193ca 20 20 20 20 0a 20 20 2f 2a 20 49 6e 69 74 69 61      .  /* Initia
193cb 6c 69 7a 65 20 74 68 65 20 63 6f 75 6e 74 20 6f  lize the count o
193cc 66 20 72 6f 77 73 20 74 6f 20 62 65 20 69 6e 73  f rows to be ins
193cd 65 72 74 65 64 0a 20 20 2a 2f 0a 20 20 69 66 28  erted.  */.  if(
193ce 20 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c   db->flags & SQL
193cf 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 29 7b  ITE_CountRows ){
193d0 0a 20 20 20 20 69 43 6e 74 4d 65 6d 20 3d 20 70  .    iCntMem = p
193d1 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 2b 3b 0a 20  Parse->nMem++;. 
193d2 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
193d3 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 49 6e 74  dOp(v, OP_MemInt
193d4 2c 20 30 2c 20 69 43 6e 74 4d 65 6d 29 3b 0a 20  , 0, iCntMem);. 
193d5 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 61   }..  /* Open ta
193d6 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63 65 73  bles and indices
193d7 20 69 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f   if there are no
193d8 20 72 6f 77 20 74 72 69 67 67 65 72 73 20 2a 2f   row triggers */
193d9 0a 20 20 69 66 28 20 21 74 72 69 67 67 65 72 73  .  if( !triggers
193da 5f 65 78 69 73 74 20 29 7b 0a 20 20 20 20 62 61  _exist ){.    ba
193db 73 65 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  se = pParse->nTa
193dc 62 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 70  b;.    sqlite3Op
193dd 65 6e 54 61 62 6c 65 41 6e 64 49 6e 64 69 63 65  enTableAndIndice
193de 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  s(pParse, pTab, 
193df 62 61 73 65 2c 20 4f 50 5f 4f 70 65 6e 57 72 69  base, OP_OpenWri
193e0 74 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  te);.  }..  /* I
193e1 66 20 74 68 65 20 64 61 74 61 20 73 6f 75 72 63  f the data sourc
193e2 65 20 69 73 20 61 20 74 65 6d 70 6f 72 61 72 79  e is a temporary
193e3 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 77 65 20   table, then we 
193e4 68 61 76 65 20 74 6f 20 63 72 65 61 74 65 0a 20  have to create. 
193e5 20 2a 2a 20 61 20 6c 6f 6f 70 20 62 65 63 61 75   ** a loop becau
193e6 73 65 20 74 68 65 72 65 20 6d 69 67 68 74 20 62  se there might b
193e7 65 20 6d 75 6c 74 69 70 6c 65 20 72 6f 77 73 20  e multiple rows 
193e8 6f 66 20 64 61 74 61 2e 20 20 49 66 20 74 68 65  of data.  If the
193e9 20 64 61 74 61 0a 20 20 2a 2a 20 73 6f 75 72 63   data.  ** sourc
193ea 65 20 69 73 20 61 20 73 75 62 72 6f 75 74 69 6e  e is a subroutin
193eb 65 20 63 61 6c 6c 20 66 72 6f 6d 20 74 68 65 20  e call from the 
193ec 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
193ed 2c 20 74 68 65 6e 20 77 65 20 6e 65 65 64 0a 20  , then we need. 
193ee 20 2a 2a 20 74 6f 20 6c 61 75 6e 63 68 20 74 68   ** to launch th
193ef 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
193f0 6e 74 20 70 72 6f 63 65 73 73 69 6e 67 2e 0a 20  nt processing.. 
193f1 20 2a 2f 0a 20 20 69 66 28 20 75 73 65 54 65 6d   */.  if( useTem
193f2 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 69 42  pTable ){.    iB
193f3 72 65 61 6b 20 3d 20 73 71 6c 69 74 65 33 56 64  reak = sqlite3Vd
193f4 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
193f5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
193f6 64 64 4f 70 28 76 2c 20 4f 50 5f 52 65 77 69 6e  ddOp(v, OP_Rewin
193f7 64 2c 20 73 72 63 54 61 62 2c 20 69 42 72 65 61  d, srcTab, iBrea
193f8 6b 29 3b 0a 20 20 20 20 69 43 6f 6e 74 20 3d 20  k);.    iCont = 
193f9 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
193fa 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 7d 65 6c  ntAddr(v);.  }el
193fb 73 65 20 69 66 28 20 70 53 65 6c 65 63 74 20 29  se if( pSelect )
193fc 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
193fd 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 47 6f 74  eAddOp(v, OP_Got
193fe 6f 2c 20 30 2c 20 69 53 65 6c 65 63 74 4c 6f 6f  o, 0, iSelectLoo
193ff 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  p);.    sqlite3V
19400 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
19401 76 2c 20 69 49 6e 73 65 72 74 42 6c 6f 63 6b 29  v, iInsertBlock)
19402 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 75 6e 20  ;.  }..  /* Run 
19403 74 68 65 20 42 45 46 4f 52 45 20 61 6e 64 20 49  the BEFORE and I
19404 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67 67 65  NSTEAD OF trigge
19405 72 73 2c 20 69 66 20 74 68 65 72 65 20 61 72 65  rs, if there are
19406 20 61 6e 79 0a 20 20 2a 2f 0a 20 20 65 6e 64 4f   any.  */.  endO
19407 66 4c 6f 6f 70 20 3d 20 73 71 6c 69 74 65 33 56  fLoop = sqlite3V
19408 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
19409 0a 20 20 69 66 28 20 74 72 69 67 67 65 72 73 5f  .  if( triggers_
1940a 65 78 69 73 74 20 26 20 54 52 49 47 47 45 52 5f  exist & TRIGGER_
1940b 42 45 46 4f 52 45 20 29 7b 0a 0a 20 20 20 20 2f  BEFORE ){..    /
1940c 2a 20 62 75 69 6c 64 20 74 68 65 20 4e 45 57 2e  * build the NEW.
1940d 2a 20 72 65 66 65 72 65 6e 63 65 20 72 6f 77 2e  * reference row.
1940e 20 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 74    Note that if t
1940f 68 65 72 65 20 69 73 20 61 6e 20 49 4e 54 45 47  here is an INTEG
19410 45 52 0a 20 20 20 20 2a 2a 20 50 52 49 4d 41 52  ER.    ** PRIMAR
19411 59 20 4b 45 59 20 69 6e 74 6f 20 77 68 69 63 68  Y KEY into which
19412 20 61 20 4e 55 4c 4c 20 69 73 20 62 65 69 6e 67   a NULL is being
19413 20 69 6e 73 65 72 74 65 64 2c 20 74 68 61 74 20   inserted, that 
19414 4e 55 4c 4c 20 77 69 6c 6c 20 62 65 0a 20 20 20  NULL will be.   
19415 20 2a 2a 20 74 72 61 6e 73 6c 61 74 65 64 20 69   ** translated i
19416 6e 74 6f 20 61 20 75 6e 69 71 75 65 20 49 44 20  nto a unique ID 
19417 66 6f 72 20 74 68 65 20 72 6f 77 2e 20 20 42 75  for the row.  Bu
19418 74 20 6f 6e 20 61 20 42 45 46 4f 52 45 20 74 72  t on a BEFORE tr
19419 69 67 67 65 72 2c 0a 20 20 20 20 2a 2a 20 77 65  igger,.    ** we
1941a 20 64 6f 20 6e 6f 74 20 6b 6e 6f 77 20 77 68 61   do not know wha
1941b 74 20 74 68 65 20 75 6e 69 71 75 65 20 49 44 20  t the unique ID 
1941c 77 69 6c 6c 20 62 65 20 28 62 65 63 61 75 73 65  will be (because
1941d 20 74 68 65 20 69 6e 73 65 72 74 20 68 61 73 0a   the insert has.
1941e 20 20 20 20 2a 2a 20 6e 6f 74 20 68 61 70 70 65      ** not happe
1941f 6e 65 64 20 79 65 74 29 20 73 6f 20 77 65 20 73  ned yet) so we s
19420 75 62 73 74 69 74 75 74 65 20 61 20 72 6f 77 69  ubstitute a rowi
19421 64 20 6f 66 20 2d 31 0a 20 20 20 20 2a 2f 0a 20  d of -1.    */. 
19422 20 20 20 69 66 28 20 6b 65 79 43 6f 6c 75 6d 6e     if( keyColumn
19423 3c 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  <0 ){.      sqli
19424 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
19425 4f 50 5f 49 6e 74 65 67 65 72 2c 20 2d 31 2c 20  OP_Integer, -1, 
19426 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  0);.    }else if
19427 28 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20 29  ( useTempTable )
19428 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
19429 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43  dbeAddOp(v, OP_C
1942a 6f 6c 75 6d 6e 2c 20 73 72 63 54 61 62 2c 20 6b  olumn, srcTab, k
1942b 65 79 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 7d  eyColumn);.    }
1942c 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65  else{.      asse
1942d 72 74 28 20 70 53 65 6c 65 63 74 3d 3d 30 20 29  rt( pSelect==0 )
1942e 3b 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20  ;  /* Otherwise 
1942f 75 73 65 54 65 6d 70 54 61 62 6c 65 20 69 73 20  useTempTable is 
19430 74 72 75 65 20 2a 2f 0a 20 20 20 20 20 20 73 71  true */.      sq
19431 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
19432 61 72 73 65 2c 20 70 4c 69 73 74 2d 3e 61 5b 6b  arse, pList->a[k
19433 65 79 43 6f 6c 75 6d 6e 5d 2e 70 45 78 70 72 29  eyColumn].pExpr)
19434 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
19435 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4e  dbeAddOp(v, OP_N
19436 6f 74 4e 75 6c 6c 2c 20 2d 31 2c 20 73 71 6c 69  otNull, -1, sqli
19437 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
19438 64 72 28 76 29 2b 33 29 3b 0a 20 20 20 20 20 20  dr(v)+3);.      
19439 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1943a 28 76 2c 20 4f 50 5f 50 6f 70 2c 20 31 2c 20 30  (v, OP_Pop, 1, 0
1943b 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1943c 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1943d 49 6e 74 65 67 65 72 2c 20 2d 31 2c 20 30 29 3b  Integer, -1, 0);
1943e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1943f 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 75  beAddOp(v, OP_Mu
19440 73 74 42 65 49 6e 74 2c 20 30 2c 20 30 29 3b 0a  stBeInt, 0, 0);.
19441 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 61      }..    /* Ca
19442 6e 6e 6f 74 20 68 61 76 65 20 74 72 69 67 67 65  nnot have trigge
19443 72 73 20 6f 6e 20 61 20 76 69 72 74 75 61 6c 20  rs on a virtual 
19444 74 61 62 6c 65 2e 20 49 66 20 69 74 20 77 65 72  table. If it wer
19445 65 20 70 6f 73 73 69 62 6c 65 2c 0a 20 20 20 20  e possible,.    
19446 2a 2a 20 74 68 69 73 20 62 6c 6f 63 6b 20 77 6f  ** this block wo
19447 75 6c 64 20 68 61 76 65 20 74 6f 20 61 63 63 6f  uld have to acco
19448 75 6e 74 20 66 6f 72 20 68 69 64 64 65 6e 20 63  unt for hidden c
19449 6f 6c 75 6d 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20  olumn..    */.  
1944a 20 20 61 73 73 65 72 74 28 21 49 73 56 69 72 74    assert(!IsVirt
1944b 75 61 6c 28 70 54 61 62 29 29 3b 0a 0a 20 20 20  ual(pTab));..   
1944c 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65 20 6e   /* Create the n
1944d 65 77 20 63 6f 6c 75 6d 6e 20 64 61 74 61 0a 20  ew column data. 
1944e 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d     */.    for(i=
1944f 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b  0; i<pTab->nCol;
19450 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   i++){.      if(
19451 20 70 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20   pColumn==0 ){. 
19452 20 20 20 20 20 20 20 6a 20 3d 20 69 3b 0a 20 20         j = i;.  
19453 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
19454 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 43     for(j=0; j<pC
19455 6f 6c 75 6d 6e 2d 3e 6e 49 64 3b 20 6a 2b 2b 29  olumn->nId; j++)
19456 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
19457 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 6a 5d 2e 69 64  pColumn->a[j].id
19458 78 3d 3d 69 20 29 20 62 72 65 61 6b 3b 0a 20 20  x==i ) break;.  
19459 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1945a 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 75 6d        if( pColum
1945b 6e 20 26 26 20 6a 3e 3d 70 43 6f 6c 75 6d 6e 2d  n && j>=pColumn-
1945c 3e 6e 49 64 20 29 7b 0a 20 20 20 20 20 20 20 20  >nId ){.        
1945d 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
1945e 70 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 61 43  pParse, pTab->aC
1945f 6f 6c 5b 69 5d 2e 70 44 66 6c 74 29 3b 0a 20 20  ol[i].pDflt);.  
19460 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 75 73      }else if( us
19461 65 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20  eTempTable ){.  
19462 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
19463 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6f 6c  eAddOp(v, OP_Col
19464 75 6d 6e 2c 20 73 72 63 54 61 62 2c 20 6a 29 3b  umn, srcTab, j);
19465 20 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20   .      }else{. 
19466 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
19467 53 65 6c 65 63 74 3d 3d 30 20 29 3b 20 2f 2a 20  Select==0 ); /* 
19468 4f 74 68 65 72 77 69 73 65 20 75 73 65 54 65 6d  Otherwise useTem
19469 70 54 61 62 6c 65 20 69 73 20 74 72 75 65 20 2a  pTable is true *
1946a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  /.        sqlite
1946b 33 45 78 70 72 43 6f 64 65 41 6e 64 43 61 63 68  3ExprCodeAndCach
1946c 65 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2d  e(pParse, pList-
1946d 3e 61 5b 6a 5d 2e 70 45 78 70 72 29 3b 0a 20 20  >a[j].pExpr);.  
1946e 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
1946f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
19470 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72  (v, OP_MakeRecor
19471 64 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20 30  d, pTab->nCol, 0
19472 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  );..    /* If th
19473 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54 20  is is an INSERT 
19474 6f 6e 20 61 20 76 69 65 77 20 77 69 74 68 20 61  on a view with a
19475 6e 20 49 4e 53 54 45 41 44 20 4f 46 20 49 4e 53  n INSTEAD OF INS
19476 45 52 54 20 74 72 69 67 67 65 72 2c 0a 20 20 20  ERT trigger,.   
19477 20 2a 2a 20 64 6f 20 6e 6f 74 20 61 74 74 65 6d   ** do not attem
19478 70 74 20 61 6e 79 20 63 6f 6e 76 65 72 73 69 6f  pt any conversio
19479 6e 73 20 62 65 66 6f 72 65 20 61 73 73 65 6d 62  ns before assemb
1947a 6c 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64 2e  ling the record.
1947b 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20  .    ** If this 
1947c 69 73 20 61 20 72 65 61 6c 20 74 61 62 6c 65 2c  is a real table,
1947d 20 61 74 74 65 6d 70 74 20 63 6f 6e 76 65 72 73   attempt convers
1947e 69 6f 6e 73 20 61 73 20 72 65 71 75 69 72 65 64  ions as required
1947f 20 62 79 20 74 68 65 0a 20 20 20 20 2a 2a 20 74   by the.    ** t
19480 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69  able column affi
19481 6e 69 74 69 65 73 2e 0a 20 20 20 20 2a 2f 0a 20  nities..    */. 
19482 20 20 20 69 66 28 20 21 69 73 56 69 65 77 20 29     if( !isView )
19483 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 54  {.      sqlite3T
19484 61 62 6c 65 41 66 66 69 6e 69 74 79 53 74 72 28  ableAffinityStr(
19485 76 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 7d 0a  v, pTab);.    }.
19486 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
19487 64 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 73 65 72  ddOp(v, OP_Inser
19488 74 2c 20 6e 65 77 49 64 78 2c 20 30 29 3b 0a 0a  t, newIdx, 0);..
19489 20 20 20 20 2f 2a 20 46 69 72 65 20 42 45 46 4f      /* Fire BEFO
1948a 52 45 20 6f 72 20 49 4e 53 54 45 41 44 20 4f 46  RE or INSTEAD OF
1948b 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 20   triggers */.   
1948c 20 69 66 28 20 73 71 6c 69 74 65 33 43 6f 64 65   if( sqlite3Code
1948d 52 6f 77 54 72 69 67 67 65 72 28 70 50 61 72 73  RowTrigger(pPars
1948e 65 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20 30 2c  e, TK_INSERT, 0,
1948f 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 2c   TRIGGER_BEFORE,
19490 20 70 54 61 62 2c 20 0a 20 20 20 20 20 20 20 20   pTab, .        
19491 6e 65 77 49 64 78 2c 20 2d 31 2c 20 6f 6e 45 72  newIdx, -1, onEr
19492 72 6f 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29 20  ror, endOfLoop) 
19493 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 69 6e  ){.      goto in
19494 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  sert_cleanup;.  
19495 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66    }.  }..  /* If
19496 20 61 6e 79 20 74 72 69 67 67 65 72 73 20 65 78   any triggers ex
19497 69 73 74 73 2c 20 74 68 65 20 6f 70 65 6e 69 6e  ists, the openin
19498 67 20 6f 66 20 74 61 62 6c 65 73 20 61 6e 64 20  g of tables and 
19499 69 6e 64 69 63 65 73 20 69 73 20 64 65 66 65 72  indices is defer
1949a 72 65 64 0a 20 20 2a 2a 20 75 6e 74 69 6c 20 6e  red.  ** until n
1949b 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 74  ow..  */.  if( t
1949c 72 69 67 67 65 72 73 5f 65 78 69 73 74 20 26 26  riggers_exist &&
1949d 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20   !isView ){.    
1949e 62 61 73 65 20 3d 20 70 50 61 72 73 65 2d 3e 6e  base = pParse->n
1949f 54 61 62 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Tab;.    sqlite3
194a0 4f 70 65 6e 54 61 62 6c 65 41 6e 64 49 6e 64 69  OpenTableAndIndi
194a1 63 65 73 28 70 50 61 72 73 65 2c 20 70 54 61 62  ces(pParse, pTab
194a2 2c 20 62 61 73 65 2c 20 4f 50 5f 4f 70 65 6e 57  , base, OP_OpenW
194a3 72 69 74 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  rite);.  }..  /*
194a4 20 50 75 73 68 20 74 68 65 20 72 65 63 6f 72 64   Push the record
194a5 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20   number for the 
194a6 6e 65 77 20 65 6e 74 72 79 20 6f 6e 74 6f 20 74  new entry onto t
194a7 68 65 20 73 74 61 63 6b 2e 20 20 54 68 65 0a 20  he stack.  The. 
194a8 20 2a 2a 20 72 65 63 6f 72 64 20 6e 75 6d 62 65   ** record numbe
194a9 72 20 69 73 20 61 20 72 61 6e 64 6f 6d 6c 79 20  r is a randomly 
194aa 67 65 6e 65 72 61 74 65 20 69 6e 74 65 67 65 72  generate integer
194ab 20 63 72 65 61 74 65 64 20 62 79 20 4e 65 77 52   created by NewR
194ac 6f 77 69 64 0a 20 20 2a 2a 20 65 78 63 65 70 74  owid.  ** except
194ad 20 77 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20   when the table 
194ae 68 61 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50  has an INTEGER P
194af 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d  RIMARY KEY colum
194b0 6e 2c 20 69 6e 20 77 68 69 63 68 0a 20 20 2a 2a  n, in which.  **
194b1 20 63 61 73 65 20 74 68 65 20 72 65 63 6f 72 64   case the record
194b2 20 6e 75 6d 62 65 72 20 69 73 20 74 68 65 20 73   number is the s
194b3 61 6d 65 20 61 73 20 74 68 61 74 20 63 6f 6c 75  ame as that colu
194b4 6d 6e 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20  mn. .  */.  if( 
194b5 21 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 69  !isView ){.    i
194b6 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  f( IsVirtual(pTa
194b7 62 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54  b) ){.      /* T
194b8 68 65 20 72 6f 77 20 74 68 61 74 20 74 68 65 20  he row that the 
194b9 56 55 70 64 61 74 65 20 6f 70 63 6f 64 65 20 77  VUpdate opcode w
194ba 69 6c 6c 20 64 65 6c 65 74 65 3a 20 20 6e 6f 6e  ill delete:  non
194bb 65 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74  e */.      sqlit
194bc 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
194bd 50 5f 4e 75 6c 6c 2c 20 30 2c 20 30 29 3b 0a 20  P_Null, 0, 0);. 
194be 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6b 65 79     }.    if( key
194bf 43 6f 6c 75 6d 6e 3e 3d 30 20 29 7b 0a 20 20 20  Column>=0 ){.   
194c0 20 20 20 69 66 28 20 75 73 65 54 65 6d 70 54 61     if( useTempTa
194c1 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ble ){.        s
194c2 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
194c3 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73 72  v, OP_Column, sr
194c4 63 54 61 62 2c 20 6b 65 79 43 6f 6c 75 6d 6e 29  cTab, keyColumn)
194c5 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
194c6 28 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20  ( pSelect ){.   
194c7 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
194c8 41 64 64 4f 70 28 76 2c 20 4f 50 5f 44 75 70 2c  AddOp(v, OP_Dup,
194c9 20 6e 43 6f 6c 75 6d 6e 20 2d 20 6b 65 79 43 6f   nColumn - keyCo
194ca 6c 75 6d 6e 20 2d 20 31 2c 20 31 29 3b 0a 20 20  lumn - 1, 1);.  
194cb 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
194cc 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a     VdbeOp *pOp;.
194cd 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
194ce 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20  xprCode(pParse, 
194cf 70 4c 69 73 74 2d 3e 61 5b 6b 65 79 43 6f 6c 75  pList->a[keyColu
194d0 6d 6e 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20  mn].pExpr);.    
194d1 20 20 20 20 70 4f 70 20 3d 20 73 71 6c 69 74 65      pOp = sqlite
194d2 33 56 64 62 65 47 65 74 4f 70 28 76 2c 20 73 71  3VdbeGetOp(v, sq
194d3 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
194d4 41 64 64 72 28 76 29 20 2d 20 31 29 3b 0a 20 20  Addr(v) - 1);.  
194d5 20 20 20 20 20 20 69 66 28 20 70 4f 70 20 26 26        if( pOp &&
194d6 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
194d7 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20  _Null ){.       
194d8 20 20 20 61 70 70 65 6e 64 46 6c 61 67 20 3d 20     appendFlag = 
194d9 31 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 70  1;.          pOp
194da 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 4e 65  ->opcode = OP_Ne
194db 77 52 6f 77 69 64 3b 0a 20 20 20 20 20 20 20 20  wRowid;.        
194dc 20 20 70 4f 70 2d 3e 70 31 20 3d 20 62 61 73 65    pOp->p1 = base
194dd 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d  ;.          pOp-
194de 3e 70 32 20 3d 20 63 6f 75 6e 74 65 72 4d 65 6d  >p2 = counterMem
194df 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
194e0 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20    }.      /* If 
194e1 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20  the PRIMARY KEY 
194e2 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 4e 55  expression is NU
194e3 4c 4c 2c 20 74 68 65 6e 20 75 73 65 20 4f 50 5f  LL, then use OP_
194e4 4e 65 77 52 6f 77 69 64 0a 20 20 20 20 20 20 2a  NewRowid.      *
194e5 2a 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20  * to generate a 
194e6 75 6e 69 71 75 65 20 70 72 69 6d 61 72 79 20 6b  unique primary k
194e7 65 79 20 76 61 6c 75 65 2e 0a 20 20 20 20 20 20  ey value..      
194e8 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 21 61 70  */.      if( !ap
194e9 70 65 6e 64 46 6c 61 67 20 29 7b 0a 20 20 20 20  pendFlag ){.    
194ea 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
194eb 64 64 4f 70 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75  ddOp(v, OP_NotNu
194ec 6c 6c 2c 20 2d 31 2c 20 73 71 6c 69 74 65 33 56  ll, -1, sqlite3V
194ed 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
194ee 29 2b 33 29 3b 0a 20 20 20 20 20 20 20 20 73 71  )+3);.        sq
194ef 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
194f0 2c 20 4f 50 5f 50 6f 70 2c 20 31 2c 20 30 29 3b  , OP_Pop, 1, 0);
194f1 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
194f2 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
194f3 4e 65 77 52 6f 77 69 64 2c 20 62 61 73 65 2c 20  NewRowid, base, 
194f4 63 6f 75 6e 74 65 72 4d 65 6d 29 3b 0a 20 20 20  counterMem);.   
194f5 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
194f6 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 75 73 74  AddOp(v, OP_Must
194f7 42 65 49 6e 74 2c 20 30 2c 20 30 29 3b 0a 20 20  BeInt, 0, 0);.  
194f8 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20      }.    }else 
194f9 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54  if( IsVirtual(pT
194fa 61 62 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ab) ){.      sql
194fb 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
194fc 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 30 29 3b   OP_Null, 0, 0);
194fd 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
194fe 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
194ff 4f 70 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69  Op(v, OP_NewRowi
19500 64 2c 20 62 61 73 65 2c 20 63 6f 75 6e 74 65 72  d, base, counter
19501 4d 65 6d 29 3b 0a 20 20 20 20 20 20 61 70 70 65  Mem);.      appe
19502 6e 64 46 6c 61 67 20 3d 20 31 3b 0a 20 20 20 20  ndFlag = 1;.    
19503 7d 0a 20 20 20 20 61 75 74 6f 49 6e 63 53 74 65  }.    autoIncSte
19504 70 28 70 50 61 72 73 65 2c 20 63 6f 75 6e 74 65  p(pParse, counte
19505 72 4d 65 6d 29 3b 0a 0a 20 20 20 20 2f 2a 20 50  rMem);..    /* P
19506 75 73 68 20 6f 6e 74 6f 20 74 68 65 20 73 74 61  ush onto the sta
19507 63 6b 2c 20 64 61 74 61 20 66 6f 72 20 61 6c 6c  ck, data for all
19508 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20   columns of the 
19509 6e 65 77 20 65 6e 74 72 79 2c 20 62 65 67 69 6e  new entry, begin
1950a 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 77 69 74 68  ning.    ** with
1950b 20 74 68 65 20 66 69 72 73 74 20 63 6f 6c 75 6d   the first colum
1950c 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6e 48  n..    */.    nH
1950d 69 64 64 65 6e 20 3d 20 30 3b 0a 20 20 20 20 66  idden = 0;.    f
1950e 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e  or(i=0; i<pTab->
1950f 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
19510 20 20 69 66 28 20 69 3d 3d 70 54 61 62 2d 3e 69    if( i==pTab->i
19511 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20  PKey ){.        
19512 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20  /* The value of 
19513 74 68 65 20 49 4e 54 45 47 45 52 20 50 52 49 4d  the INTEGER PRIM
19514 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e 20 69  ARY KEY column i
19515 73 20 61 6c 77 61 79 73 20 61 20 4e 55 4c 4c 2e  s always a NULL.
19516 0a 20 20 20 20 20 20 20 20 2a 2a 20 57 68 65 6e  .        ** When
19517 65 76 65 72 20 74 68 69 73 20 63 6f 6c 75 6d 6e  ever this column
19518 20 69 73 20 72 65 61 64 2c 20 74 68 65 20 72 65   is read, the re
19519 63 6f 72 64 20 6e 75 6d 62 65 72 20 77 69 6c 6c  cord number will
1951a 20 62 65 20 73 75 62 73 74 69 74 75 74 65 64 0a   be substituted.
1951b 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 20 69 74          ** in it
1951c 73 20 70 6c 61 63 65 2e 20 20 53 6f 20 77 69 6c  s place.  So wil
1951d 6c 20 66 69 6c 6c 20 74 68 69 73 20 63 6f 6c 75  l fill this colu
1951e 6d 6e 20 77 69 74 68 20 61 20 4e 55 4c 4c 20 74  mn with a NULL t
1951f 6f 20 61 76 6f 69 64 0a 20 20 20 20 20 20 20 20  o avoid.        
19520 2a 2a 20 74 61 6b 69 6e 67 20 75 70 20 64 61 74  ** taking up dat
19521 61 20 73 70 61 63 65 20 77 69 74 68 20 69 6e 66  a space with inf
19522 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74 20 77 69  ormation that wi
19523 6c 6c 20 6e 65 76 65 72 20 62 65 20 75 73 65 64  ll never be used
19524 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c  . */.        sql
19525 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
19526 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 30 29 3b   OP_Null, 0, 0);
19527 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75  .        continu
19528 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  e;.      }.     
19529 20 69 66 28 20 70 43 6f 6c 75 6d 6e 3d 3d 30 20   if( pColumn==0 
1952a 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 49  ){.        if( I
1952b 73 48 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 26 70  sHiddenColumn(&p
1952c 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 29 20 29 7b  Tab->aCol[i]) ){
1952d 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
1952e 74 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  t( IsVirtual(pTa
1952f 62 29 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  b) );.          
19530 6a 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20  j = -1;.        
19531 20 20 6e 48 69 64 64 65 6e 2b 2b 3b 0a 20 20 20    nHidden++;.   
19532 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
19533 20 20 20 20 20 20 6a 20 3d 20 69 20 2d 20 6e 48        j = i - nH
19534 69 64 64 65 6e 3b 0a 20 20 20 20 20 20 20 20 7d  idden;.        }
19535 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
19536 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
19537 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 3b 20 6a  <pColumn->nId; j
19538 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  ++){.          i
19539 66 28 20 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 6a 5d  f( pColumn->a[j]
1953a 2e 69 64 78 3d 3d 69 20 29 20 62 72 65 61 6b 3b  .idx==i ) break;
1953b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1953c 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6a 3c 30   }.      if( j<0
1953d 20 7c 7c 20 6e 43 6f 6c 75 6d 6e 3d 3d 30 20 7c   || nColumn==0 |
1953e 7c 20 28 70 43 6f 6c 75 6d 6e 20 26 26 20 6a 3e  | (pColumn && j>
1953f 3d 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 29 20 29  =pColumn->nId) )
19540 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
19541 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
19542 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e  , pTab->aCol[i].
19543 70 44 66 6c 74 29 3b 0a 20 20 20 20 20 20 7d 65  pDflt);.      }e
19544 6c 73 65 20 69 66 28 20 75 73 65 54 65 6d 70 54  lse if( useTempT
19545 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20  able ){.        
19546 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
19547 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73  (v, OP_Column, s
19548 72 63 54 61 62 2c 20 6a 29 3b 20 0a 20 20 20 20  rcTab, j); .    
19549 20 20 7d 65 6c 73 65 20 69 66 28 20 70 53 65 6c    }else if( pSel
1954a 65 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ect ){.        s
1954b 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1954c 76 2c 20 4f 50 5f 44 75 70 2c 20 69 2b 6e 43 6f  v, OP_Dup, i+nCo
1954d 6c 75 6d 6e 2d 6a 2b 49 73 56 69 72 74 75 61 6c  lumn-j+IsVirtual
1954e 28 70 54 61 62 29 2c 20 31 29 3b 0a 20 20 20 20  (pTab), 1);.    
1954f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
19550 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
19551 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2d 3e  (pParse, pList->
19552 61 5b 6a 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20  a[j].pExpr);.   
19553 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
19554 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
19555 20 74 6f 20 63 68 65 63 6b 20 63 6f 6e 73 74 72   to check constr
19556 61 69 6e 74 73 20 61 6e 64 20 67 65 6e 65 72 61  aints and genera
19557 74 65 20 69 6e 64 65 78 20 6b 65 79 73 20 61 6e  te index keys an
19558 64 0a 20 20 20 20 2a 2a 20 64 6f 20 74 68 65 20  d.    ** do the 
19559 69 6e 73 65 72 74 69 6f 6e 2e 0a 20 20 20 20 2a  insertion..    *
1955a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
1955b 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
1955c 4c 45 0a 20 20 20 20 69 66 28 20 49 73 56 69 72  LE.    if( IsVir
1955d 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20  tual(pTab) ){.  
1955e 20 20 20 20 70 50 61 72 73 65 2d 3e 70 56 69 72      pParse->pVir
1955f 74 75 61 6c 4c 6f 63 6b 20 3d 20 70 54 61 62 3b  tualLock = pTab;
19560 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
19561 62 65 4f 70 33 28 76 2c 20 4f 50 5f 56 55 70 64  beOp3(v, OP_VUpd
19562 61 74 65 2c 20 31 2c 20 70 54 61 62 2d 3e 6e 43  ate, 1, pTab->nC
19563 6f 6c 2b 32 2c 0a 20 20 20 20 20 20 20 20 20 20  ol+2,.          
19564 20 20 20 20 20 20 20 20 20 20 20 28 63 6f 6e 73             (cons
19565 74 20 63 68 61 72 2a 29 70 54 61 62 2d 3e 70 56  t char*)pTab->pV
19566 74 61 62 2c 20 50 33 5f 56 54 41 42 29 3b 0a 20  tab, P3_VTAB);. 
19567 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a     }else.#endif.
19568 20 20 20 20 7b 0a 20 20 20 20 20 20 73 71 6c 69      {.      sqli
19569 74 65 33 47 65 6e 65 72 61 74 65 43 6f 6e 73 74  te3GenerateConst
1956a 72 61 69 6e 74 43 68 65 63 6b 73 28 70 50 61 72  raintChecks(pPar
1956b 73 65 2c 20 70 54 61 62 2c 20 62 61 73 65 2c 20  se, pTab, base, 
1956c 30 2c 20 6b 65 79 43 6f 6c 75 6d 6e 3e 3d 30 2c  0, keyColumn>=0,
1956d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1956e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1956f 20 20 20 20 20 20 30 2c 20 6f 6e 45 72 72 6f 72        0, onError
19570 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20 20  , endOfLoop);.  
19571 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6d 70 6c      sqlite3Compl
19572 65 74 65 49 6e 73 65 72 74 69 6f 6e 28 70 50 61  eteInsertion(pPa
19573 72 73 65 2c 20 70 54 61 62 2c 20 62 61 73 65 2c  rse, pTab, base,
19574 20 30 2c 30 2c 30 2c 0a 20 20 20 20 20 20 20 20   0,0,0,.        
19575 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19576 20 20 20 20 28 74 72 69 67 67 65 72 73 5f 65 78      (triggers_ex
19577 69 73 74 20 26 20 54 52 49 47 47 45 52 5f 41 46  ist & TRIGGER_AF
19578 54 45 52 29 21 3d 30 20 3f 20 6e 65 77 49 64 78  TER)!=0 ? newIdx
19579 20 3a 20 2d 31 2c 0a 20 20 20 20 20 20 20 20 20   : -1,.         
1957a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1957b 20 20 20 61 70 70 65 6e 64 46 6c 61 67 29 3b 0a     appendFlag);.
1957c 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
1957d 55 70 64 61 74 65 20 74 68 65 20 63 6f 75 6e 74  Update the count
1957e 20 6f 66 20 72 6f 77 73 20 74 68 61 74 20 61 72   of rows that ar
1957f 65 20 69 6e 73 65 72 74 65 64 0a 20 20 2a 2f 0a  e inserted.  */.
19580 20 20 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73    if( (db->flags
19581 20 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52   & SQLITE_CountR
19582 6f 77 73 29 21 3d 30 20 29 7b 0a 20 20 20 20 73  ows)!=0 ){.    s
19583 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
19584 76 2c 20 4f 50 5f 4d 65 6d 49 6e 63 72 2c 20 31  v, OP_MemIncr, 1
19585 2c 20 69 43 6e 74 4d 65 6d 29 3b 0a 20 20 7d 0a  , iCntMem);.  }.
19586 0a 20 20 69 66 28 20 74 72 69 67 67 65 72 73 5f  .  if( triggers_
19587 65 78 69 73 74 20 29 7b 0a 20 20 20 20 2f 2a 20  exist ){.    /* 
19588 43 6c 6f 73 65 20 61 6c 6c 20 74 61 62 6c 65 73  Close all tables
19589 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 20 20 69   opened */.    i
1958a 66 28 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20  f( !isView ){.  
1958b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1958c 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f 73 65  ddOp(v, OP_Close
1958d 2c 20 62 61 73 65 2c 20 30 29 3b 0a 20 20 20 20  , base, 0);.    
1958e 20 20 66 6f 72 28 69 64 78 3d 31 2c 20 70 49 64    for(idx=1, pId
1958f 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
19590 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d  pIdx; pIdx=pIdx-
19591 3e 70 4e 65 78 74 2c 20 69 64 78 2b 2b 29 7b 0a  >pNext, idx++){.
19592 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
19593 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43  dbeAddOp(v, OP_C
19594 6c 6f 73 65 2c 20 69 64 78 2b 62 61 73 65 2c 20  lose, idx+base, 
19595 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  0);.      }.    
19596 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 64 65 20 41  }..    /* Code A
19597 46 54 45 52 20 74 72 69 67 67 65 72 73 20 2a 2f  FTER triggers */
19598 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
19599 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 70  CodeRowTrigger(p
1959a 50 61 72 73 65 2c 20 54 4b 5f 49 4e 53 45 52 54  Parse, TK_INSERT
1959b 2c 20 30 2c 20 54 52 49 47 47 45 52 5f 41 46 54  , 0, TRIGGER_AFT
1959c 45 52 2c 20 70 54 61 62 2c 0a 20 20 20 20 20 20  ER, pTab,.      
1959d 20 20 20 20 6e 65 77 49 64 78 2c 20 2d 31 2c 20      newIdx, -1, 
1959e 6f 6e 45 72 72 6f 72 2c 20 65 6e 64 4f 66 4c 6f  onError, endOfLo
1959f 6f 70 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  op) ){.      got
195a0 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70  o insert_cleanup
195a1 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
195a2 2a 20 54 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20  * The bottom of 
195a3 74 68 65 20 6c 6f 6f 70 2c 20 69 66 20 74 68 65  the loop, if the
195a4 20 64 61 74 61 20 73 6f 75 72 63 65 20 69 73 20   data source is 
195a5 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  a SELECT stateme
195a6 6e 74 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  nt.  */.  sqlite
195a7 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
195a8 6c 28 76 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b  l(v, endOfLoop);
195a9 0a 20 20 69 66 28 20 75 73 65 54 65 6d 70 54 61  .  if( useTempTa
195aa 62 6c 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ble ){.    sqlit
195ab 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
195ac 50 5f 4e 65 78 74 2c 20 73 72 63 54 61 62 2c 20  P_Next, srcTab, 
195ad 69 43 6f 6e 74 29 3b 0a 20 20 20 20 73 71 6c 69  iCont);.    sqli
195ae 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
195af 62 65 6c 28 76 2c 20 69 42 72 65 61 6b 29 3b 0a  bel(v, iBreak);.
195b0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
195b1 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f 73 65  ddOp(v, OP_Close
195b2 2c 20 73 72 63 54 61 62 2c 20 30 29 3b 0a 20 20  , srcTab, 0);.  
195b3 7d 65 6c 73 65 20 69 66 28 20 70 53 65 6c 65 63  }else if( pSelec
195b4 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
195b5 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
195b6 50 6f 70 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 30 29  Pop, nColumn, 0)
195b7 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
195b8 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 52 65 74  eAddOp(v, OP_Ret
195b9 75 72 6e 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  urn, 0, 0);.    
195ba 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c  sqlite3VdbeResol
195bb 76 65 4c 61 62 65 6c 28 76 2c 20 69 43 6c 65 61  veLabel(v, iClea
195bc 6e 75 70 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  nup);.  }..  if(
195bd 20 21 74 72 69 67 67 65 72 73 5f 65 78 69 73 74   !triggers_exist
195be 20 26 26 20 21 49 73 56 69 72 74 75 61 6c 28 70   && !IsVirtual(p
195bf 54 61 62 29 20 29 7b 0a 20 20 20 20 2f 2a 20 43  Tab) ){.    /* C
195c0 6c 6f 73 65 20 61 6c 6c 20 74 61 62 6c 65 73 20  lose all tables 
195c1 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 20 20 73 71  opened */.    sq
195c2 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
195c3 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 62 61 73 65  , OP_Close, base
195c4 2c 20 30 29 3b 0a 20 20 20 20 66 6f 72 28 69 64  , 0);.    for(id
195c5 78 3d 31 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e  x=1, pIdx=pTab->
195c6 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49  pIndex; pIdx; pI
195c7 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20  dx=pIdx->pNext, 
195c8 69 64 78 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71  idx++){.      sq
195c9 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
195ca 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 64 78 2b  , OP_Close, idx+
195cb 62 61 73 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  base, 0);.    }.
195cc 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65    }..  /* Update
195cd 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75   the sqlite_sequ
195ce 65 6e 63 65 20 74 61 62 6c 65 20 62 79 20 73 74  ence table by st
195cf 6f 72 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e  oring the conten
195d0 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 63 6f  t of the.  ** co
195d1 75 6e 74 65 72 20 76 61 6c 75 65 20 69 6e 20 6d  unter value in m
195d2 65 6d 6f 72 79 20 63 6f 75 6e 74 65 72 4d 65 6d  emory counterMem
195d3 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 73   back into the s
195d4 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 0a 20  qlite_sequence. 
195d5 20 2a 2a 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a   ** table..  */.
195d6 20 20 61 75 74 6f 49 6e 63 45 6e 64 28 70 50 61    autoIncEnd(pPa
195d7 72 73 65 2c 20 69 44 62 2c 20 70 54 61 62 2c 20  rse, iDb, pTab, 
195d8 63 6f 75 6e 74 65 72 4d 65 6d 29 3b 0a 0a 20 20  counterMem);..  
195d9 2f 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 74  /*.  ** Return t
195da 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  he number of row
195db 73 20 69 6e 73 65 72 74 65 64 2e 20 49 66 20 74  s inserted. If t
195dc 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 0a  his routine is .
195dd 20 20 2a 2a 20 67 65 6e 65 72 61 74 69 6e 67 20    ** generating 
195de 63 6f 64 65 20 62 65 63 61 75 73 65 20 6f 66 20  code because of 
195df 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  a call to sqlite
195e0 33 4e 65 73 74 65 64 50 61 72 73 65 28 29 2c 20  3NestedParse(), 
195e1 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20 69 6e 76 6f  do not.  ** invo
195e2 6b 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  ke the callback 
195e3 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20  function..  */. 
195e4 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 20 26   if( db->flags &
195e5 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77   SQLITE_CountRow
195e6 73 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 65 73  s && pParse->nes
195e7 74 65 64 3d 3d 30 20 26 26 20 21 70 50 61 72 73  ted==0 && !pPars
195e8 65 2d 3e 74 72 69 67 53 74 61 63 6b 20 29 7b 0a  e->trigStack ){.
195e9 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
195ea 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 4c 6f  ddOp(v, OP_MemLo
195eb 61 64 2c 20 69 43 6e 74 4d 65 6d 2c 20 30 29 3b  ad, iCntMem, 0);
195ec 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
195ed 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 61 6c 6c  AddOp(v, OP_Call
195ee 62 61 63 6b 2c 20 31 2c 20 30 29 3b 0a 20 20 20  back, 1, 0);.   
195ef 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e   sqlite3VdbeSetN
195f0 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20  umCols(v, 1);.  
195f1 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
195f2 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f  ColName(v, 0, CO
195f3 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 72 6f 77  LNAME_NAME, "row
195f4 73 20 69 6e 73 65 72 74 65 64 22 2c 20 50 33 5f  s inserted", P3_
195f5 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 0a 69 6e  STATIC);.  }..in
195f6 73 65 72 74 5f 63 6c 65 61 6e 75 70 3a 0a 20 20  sert_cleanup:.  
195f7 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65  sqlite3SrcListDe
195f8 6c 65 74 65 28 70 54 61 62 4c 69 73 74 29 3b 0a  lete(pTabList);.
195f9 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
195fa 74 44 65 6c 65 74 65 28 70 4c 69 73 74 29 3b 0a  tDelete(pList);.
195fb 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
195fc 65 6c 65 74 65 28 70 53 65 6c 65 63 74 29 3b 0a  elete(pSelect);.
195fd 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44    sqlite3IdListD
195fe 65 6c 65 74 65 28 70 43 6f 6c 75 6d 6e 29 3b 0a  elete(pColumn);.
195ff 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
19600 65 20 63 6f 64 65 20 74 6f 20 64 6f 20 61 20 63  e code to do a c
19601 6f 6e 73 74 72 61 69 6e 74 20 63 68 65 63 6b 20  onstraint check 
19602 70 72 69 6f 72 20 74 6f 20 61 6e 20 49 4e 53 45  prior to an INSE
19603 52 54 20 6f 72 20 61 6e 20 55 50 44 41 54 45 2e  RT or an UPDATE.
19604 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73  .**.** When this
19605 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
19606 65 64 2c 20 74 68 65 20 73 74 61 63 6b 20 63 6f  ed, the stack co
19607 6e 74 61 69 6e 73 20 28 66 72 6f 6d 20 62 6f 74  ntains (from bot
19608 74 6f 6d 20 74 6f 20 74 6f 70 29 0a 2a 2a 20 74  tom to top).** t
19609 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c  he following val
1960a 75 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 31 2e  ues:.**.**    1.
1960b 20 20 54 68 65 20 72 6f 77 69 64 20 6f 66 20 74    The rowid of t
1960c 68 65 20 72 6f 77 20 74 6f 20 62 65 20 75 70 64  he row to be upd
1960d 61 74 65 64 20 62 65 66 6f 72 65 20 74 68 65 20  ated before the 
1960e 75 70 64 61 74 65 2e 20 20 54 68 69 73 0a 2a 2a  update.  This.**
1960f 20 20 20 20 20 20 20 20 76 61 6c 75 65 20 69 73          value is
19610 20 6f 6d 69 74 74 65 64 20 75 6e 6c 65 73 73 20   omitted unless 
19611 77 65 20 61 72 65 20 64 6f 69 6e 67 20 61 6e 20  we are doing an 
19612 55 50 44 41 54 45 20 74 68 61 74 20 69 6e 76 6f  UPDATE that invo
19613 6c 76 65 73 20 61 0a 2a 2a 20 20 20 20 20 20 20  lves a.**       
19614 20 63 68 61 6e 67 65 20 74 6f 20 74 68 65 20 72   change to the r
19615 65 63 6f 72 64 20 6e 75 6d 62 65 72 2e 0a 2a 2a  ecord number..**
19616 0a 2a 2a 20 20 20 20 32 2e 20 20 54 68 65 20 72  .**    2.  The r
19617 6f 77 69 64 20 6f 66 20 74 68 65 20 72 6f 77 20  owid of the row 
19618 61 66 74 65 72 20 74 68 65 20 75 70 64 61 74 65  after the update
19619 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 33 2e 20 20 54  ..**.**    3.  T
1961a 68 65 20 64 61 74 61 20 69 6e 20 74 68 65 20 66  he data in the f
1961b 69 72 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74  irst column of t
1961c 68 65 20 65 6e 74 72 79 20 61 66 74 65 72 20 74  he entry after t
1961d 68 65 20 75 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a  he update..**.**
1961e 20 20 20 20 69 2e 20 20 44 61 74 61 20 66 72 6f      i.  Data fro
1961f 6d 20 6d 69 64 64 6c 65 20 63 6f 6c 75 6d 6e 73  m middle columns
19620 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 4e 2e 20  ....**.**    N. 
19621 20 54 68 65 20 64 61 74 61 20 69 6e 20 74 68 65   The data in the
19622 20 6c 61 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20   last column of 
19623 74 68 65 20 65 6e 74 72 79 20 61 66 74 65 72 20  the entry after 
19624 74 68 65 20 75 70 64 61 74 65 2e 0a 2a 2a 0a 2a  the update..**.*
19625 2a 20 54 68 65 20 6f 6c 64 20 72 6f 77 69 64 20  * The old rowid 
19626 73 68 6f 77 6e 20 61 73 20 65 6e 74 72 79 20 28  shown as entry (
19627 31 29 20 61 62 6f 76 65 20 69 73 20 6f 6d 69 74  1) above is omit
19628 74 65 64 20 75 6e 6c 65 73 73 20 62 6f 74 68 20  ted unless both 
19629 69 73 55 70 64 61 74 65 0a 2a 2a 20 61 6e 64 20  isUpdate.** and 
1962a 72 6f 77 69 64 43 68 6e 67 20 61 72 65 20 31 2e  rowidChng are 1.
1962b 20 20 69 73 55 70 64 61 74 65 20 69 73 20 74 72    isUpdate is tr
1962c 75 65 20 66 6f 72 20 55 50 44 41 54 45 73 20 61  ue for UPDATEs a
1962d 6e 64 20 66 61 6c 73 65 20 66 6f 72 0a 2a 2a 20  nd false for.** 
1962e 49 4e 53 45 52 54 73 20 61 6e 64 20 72 6f 77 69  INSERTs and rowi
1962f 64 43 68 6e 67 20 69 73 20 74 72 75 65 20 69 66  dChng is true if
19630 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62   the record numb
19631 65 72 20 69 73 20 62 65 69 6e 67 20 63 68 61 6e  er is being chan
19632 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  ged..**.** The c
19633 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20 62 79  ode generated by
19634 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 70 75   this routine pu
19635 73 68 65 73 20 61 64 64 69 74 69 6f 6e 61 6c 20  shes additional 
19636 65 6e 74 72 69 65 73 20 6f 6e 74 6f 0a 2a 2a 20  entries onto.** 
19637 74 68 65 20 73 74 61 63 6b 20 77 68 69 63 68 20  the stack which 
19638 61 72 65 20 74 68 65 20 6b 65 79 73 20 66 6f 72  are the keys for
19639 20 6e 65 77 20 69 6e 64 65 78 20 65 6e 74 72 69   new index entri
1963a 65 73 20 66 6f 72 20 74 68 65 20 6e 65 77 20 72  es for the new r
1963b 65 63 6f 72 64 2e 0a 2a 2a 20 54 68 65 20 6f 72  ecord..** The or
1963c 64 65 72 20 6f 66 20 69 6e 64 65 78 20 6b 65 79  der of index key
1963d 73 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73  s is the same as
1963e 20 74 68 65 20 6f 72 64 65 72 20 6f 66 20 74 68   the order of th
1963f 65 20 69 6e 64 69 63 65 73 20 6f 6e 0a 2a 2a 20  e indices on.** 
19640 74 68 65 20 70 54 61 62 6c 65 2d 3e 70 49 6e 64  the pTable->pInd
19641 65 78 20 6c 69 73 74 2e 20 20 41 20 6b 65 79 20  ex list.  A key 
19642 69 73 20 6f 6e 6c 79 20 63 72 65 61 74 65 64 20  is only created 
19643 66 6f 72 20 69 6e 64 65 78 20 69 20 69 66 20 0a  for index i if .
19644 2a 2a 20 61 49 64 78 55 73 65 64 21 3d 30 20 61  ** aIdxUsed!=0 a
19645 6e 64 20 61 49 64 78 55 73 65 64 5b 69 5d 21 3d  nd aIdxUsed[i]!=
19646 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  0..**.** This ro
19647 75 74 69 6e 65 20 61 6c 73 6f 20 67 65 6e 65 72  utine also gener
19648 61 74 65 73 20 63 6f 64 65 20 74 6f 20 63 68 65  ates code to che
19649 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20  ck constraints. 
1964a 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 2a 2a 20 43 48   NOT NULL,.** CH
1964b 45 43 4b 2c 20 61 6e 64 20 55 4e 49 51 55 45 20  ECK, and UNIQUE 
1964c 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 72 65 20  constraints are 
1964d 61 6c 6c 20 63 68 65 63 6b 65 64 2e 20 20 49 66  all checked.  If
1964e 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61   a constraint fa
1964f 69 6c 73 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65  ils,.** then the
19650 20 61 70 70 72 6f 70 72 69 61 74 65 20 61 63 74   appropriate act
19651 69 6f 6e 20 69 73 20 70 65 72 66 6f 72 6d 65 64  ion is performed
19652 2e 20 20 54 68 65 72 65 20 61 72 65 20 66 69 76  .  There are fiv
19653 65 20 70 6f 73 73 69 62 6c 65 0a 2a 2a 20 61 63  e possible.** ac
19654 74 69 6f 6e 73 3a 20 52 4f 4c 4c 42 41 43 4b 2c  tions: ROLLBACK,
19655 20 41 42 4f 52 54 2c 20 46 41 49 4c 2c 20 52 45   ABORT, FAIL, RE
19656 50 4c 41 43 45 2c 20 61 6e 64 20 49 47 4e 4f 52  PLACE, and IGNOR
19657 45 2e 0a 2a 2a 0a 2a 2a 20 20 43 6f 6e 73 74 72  E..**.**  Constr
19658 61 69 6e 74 20 74 79 70 65 20 20 41 63 74 69 6f  aint type  Actio
19659 6e 20 20 20 20 20 20 20 57 68 61 74 20 48 61 70  n       What Hap
1965a 70 65 6e 73 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d  pens.**  -------
1965b 2d 2d 2d 2d 2d 2d 2d 2d 20 20 2d 2d 2d 2d 2d 2d  --------  ------
1965c 2d 2d 2d 2d 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----   ---------
1965d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1965e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
1965f 2a 2a 20 20 61 6e 79 20 20 20 20 20 20 20 20 20  **  any         
19660 20 20 20 20 20 52 4f 4c 4c 42 41 43 4b 20 20 20       ROLLBACK   
19661 20 20 54 68 65 20 63 75 72 72 65 6e 74 20 74 72    The current tr
19662 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c  ansaction is rol
19663 6c 65 64 20 62 61 63 6b 20 61 6e 64 0a 2a 2a 20  led back and.** 
19664 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19665 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
19666 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 72 65  qlite3_exec() re
19667 74 75 72 6e 73 20 69 6d 6d 65 64 69 61 74 65 6c  turns immediatel
19668 79 20 77 69 74 68 20 61 0a 2a 2a 20 20 20 20 20  y with a.**     
19669 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1966a 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72             retur
1966b 6e 20 63 6f 64 65 20 6f 66 20 53 51 4c 49 54 45  n code of SQLITE
1966c 5f 43 4f 4e 53 54 52 41 49 4e 54 2e 0a 2a 2a 0a  _CONSTRAINT..**.
1966d 2a 2a 20 20 61 6e 79 20 20 20 20 20 20 20 20 20  **  any         
1966e 20 20 20 20 20 41 42 4f 52 54 20 20 20 20 20 20       ABORT      
1966f 20 20 42 61 63 6b 20 6f 75 74 20 63 68 61 6e 67    Back out chang
19670 65 73 20 66 72 6f 6d 20 74 68 65 20 63 75 72 72  es from the curr
19671 65 6e 74 20 63 6f 6d 6d 61 6e 64 0a 2a 2a 20 20  ent command.**  
19672 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19673 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 6e                on
19674 6c 79 20 28 64 6f 20 6e 6f 74 20 64 6f 20 61 20  ly (do not do a 
19675 63 6f 6d 70 6c 65 74 65 20 72 6f 6c 6c 62 61 63  complete rollbac
19676 6b 29 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20  k) then.**      
19677 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19678 20 20 20 20 20 20 20 20 20 20 63 61 75 73 65 20            cause 
19679 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 74  sqlite3_exec() t
1967a 6f 20 72 65 74 75 72 6e 20 69 6d 6d 65 64 69 61  o return immedia
1967b 74 65 6c 79 0a 2a 2a 20 20 20 20 20 20 20 20 20  tely.**         
1967c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1967d 20 20 20 20 20 20 20 77 69 74 68 20 53 51 4c 49         with SQLI
1967e 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2e 0a 2a  TE_CONSTRAINT..*
1967f 2a 0a 2a 2a 20 20 61 6e 79 20 20 20 20 20 20 20  *.**  any       
19680 20 20 20 20 20 20 20 46 41 49 4c 20 20 20 20 20         FAIL     
19681 20 20 20 20 53 71 6c 69 74 65 5f 65 78 65 63 28      Sqlite_exec(
19682 29 20 72 65 74 75 72 6e 73 20 69 6d 6d 65 64 69  ) returns immedi
19683 61 74 65 6c 79 20 77 69 74 68 20 61 0a 2a 2a 20  ately with a.** 
19684 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19685 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72                 r
19686 65 74 75 72 6e 20 63 6f 64 65 20 6f 66 20 53 51  eturn code of SQ
19687 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2e  LITE_CONSTRAINT.
19688 20 20 54 68 65 0a 2a 2a 20 20 20 20 20 20 20 20    The.**        
19689 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1968a 20 20 20 20 20 20 20 20 74 72 61 6e 73 61 63 74          transact
1968b 69 6f 6e 20 69 73 20 6e 6f 74 20 72 6f 6c 6c 65  ion is not rolle
1968c 64 20 62 61 63 6b 20 61 6e 64 20 61 6e 79 0a 2a  d back and any.*
1968d 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
1968e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1968f 20 70 72 69 6f 72 20 63 68 61 6e 67 65 73 20 61   prior changes a
19690 72 65 20 72 65 74 61 69 6e 65 64 2e 0a 2a 2a 0a  re retained..**.
19691 2a 2a 20 20 61 6e 79 20 20 20 20 20 20 20 20 20  **  any         
19692 20 20 20 20 20 49 47 4e 4f 52 45 20 20 20 20 20       IGNORE     
19693 20 20 54 68 65 20 72 65 63 6f 72 64 20 6e 75 6d    The record num
19694 62 65 72 20 61 6e 64 20 64 61 74 61 20 69 73 20  ber and data is 
19695 70 6f 70 70 65 64 20 66 72 6f 6d 0a 2a 2a 20 20  popped from.**  
19696 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19697 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68                th
19698 65 20 73 74 61 63 6b 20 61 6e 64 20 74 68 65 72  e stack and ther
19699 65 20 69 73 20 61 6e 20 69 6d 6d 65 64 69 61 74  e is an immediat
1969a 65 20 6a 75 6d 70 0a 2a 2a 20 20 20 20 20 20 20  e jump.**       
1969b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1969c 20 20 20 20 20 20 20 20 20 74 6f 20 6c 61 62 65           to labe
1969d 6c 20 69 67 6e 6f 72 65 44 65 73 74 2e 0a 2a 2a  l ignoreDest..**
1969e 0a 2a 2a 20 20 4e 4f 54 20 4e 55 4c 4c 20 20 20  .**  NOT NULL   
1969f 20 20 20 20 20 20 52 45 50 4c 41 43 45 20 20 20        REPLACE   
196a0 20 20 20 54 68 65 20 4e 55 4c 4c 20 76 61 6c 75     The NULL valu
196a1 65 20 69 73 20 72 65 70 6c 61 63 65 20 62 79 20  e is replace by 
196a2 74 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 20  the default.**  
196a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
196a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 76 61                va
196a5 6c 75 65 20 66 6f 72 20 74 68 61 74 20 63 6f 6c  lue for that col
196a6 75 6d 6e 2e 20 20 49 66 20 74 68 65 20 64 65 66  umn.  If the def
196a7 61 75 6c 74 20 76 61 6c 75 65 0a 2a 2a 20 20 20  ault value.**   
196a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
196a9 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73 20               is 
196aa 4e 55 4c 4c 2c 20 74 68 65 20 61 63 74 69 6f 6e  NULL, the action
196ab 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20   is the same as 
196ac 41 42 4f 52 54 2e 0a 2a 2a 0a 2a 2a 20 20 55 4e  ABORT..**.**  UN
196ad 49 51 55 45 20 20 20 20 20 20 20 20 20 20 20 52  IQUE           R
196ae 45 50 4c 41 43 45 20 20 20 20 20 20 54 68 65 20  EPLACE      The 
196af 6f 74 68 65 72 20 72 6f 77 20 74 68 61 74 20 63  other row that c
196b0 6f 6e 66 6c 69 63 74 73 20 77 69 74 68 20 74 68  onflicts with th
196b1 65 20 72 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20  e row.**        
196b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
196b3 20 20 20 20 20 20 20 20 62 65 69 6e 67 20 69 6e          being in
196b4 73 65 72 74 65 64 20 69 73 20 72 65 6d 6f 76 65  serted is remove
196b5 64 2e 0a 2a 2a 0a 2a 2a 20 20 43 48 45 43 4b 20  d..**.**  CHECK 
196b6 20 20 20 20 20 20 20 20 20 20 20 52 45 50 4c 41             REPLA
196b7 43 45 20 20 20 20 20 20 49 6c 6c 65 67 61 6c 2e  CE      Illegal.
196b8 20 20 54 68 65 20 72 65 73 75 6c 74 73 20 69 6e    The results in
196b9 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 2e 0a 2a   an exception..*
196ba 2a 0a 2a 2a 20 57 68 69 63 68 20 61 63 74 69 6f  *.** Which actio
196bb 6e 20 74 6f 20 74 61 6b 65 20 69 73 20 64 65 74  n to take is det
196bc 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 6f  ermined by the o
196bd 76 65 72 72 69 64 65 45 72 72 6f 72 20 70 61 72  verrideError par
196be 61 6d 65 74 65 72 2e 0a 2a 2a 20 4f 72 20 69 66  ameter..** Or if
196bf 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72 3d 3d   overrideError==
196c0 4f 45 5f 44 65 66 61 75 6c 74 2c 20 74 68 65 6e  OE_Default, then
196c1 20 74 68 65 20 70 50 61 72 73 65 2d 3e 6f 6e 45   the pParse->onE
196c2 72 72 6f 72 20 70 61 72 61 6d 65 74 65 72 0a 2a  rror parameter.*
196c3 2a 20 69 73 20 75 73 65 64 2e 20 20 4f 72 20 69  * is used.  Or i
196c4 66 20 70 50 61 72 73 65 2d 3e 6f 6e 45 72 72 6f  f pParse->onErro
196c5 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20 74 68  r==OE_Default th
196c6 65 6e 20 74 68 65 20 6f 6e 45 72 72 6f 72 20 76  en the onError v
196c7 61 6c 75 65 0a 2a 2a 20 66 6f 72 20 74 68 65 20  alue.** for the 
196c8 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 75 73  constraint is us
196c9 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61  ed..**.** The ca
196ca 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 20 6d 75  lling routine mu
196cb 73 74 20 6f 70 65 6e 20 61 20 72 65 61 64 2f 77  st open a read/w
196cc 72 69 74 65 20 63 75 72 73 6f 72 20 66 6f 72 20  rite cursor for 
196cd 70 54 61 62 20 77 69 74 68 0a 2a 2a 20 63 75 72  pTab with.** cur
196ce 73 6f 72 20 6e 75 6d 62 65 72 20 22 62 61 73 65  sor number "base
196cf 22 2e 20 20 41 6c 6c 20 69 6e 64 69 63 65 73 20  ".  All indices 
196d0 6f 66 20 70 54 61 62 20 6d 75 73 74 20 61 6c 73  of pTab must als
196d1 6f 20 68 61 76 65 20 6f 70 65 6e 0a 2a 2a 20 72  o have open.** r
196d2 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72  ead/write cursor
196d3 73 20 77 69 74 68 20 63 75 72 73 6f 72 20 6e 75  s with cursor nu
196d4 6d 62 65 72 20 62 61 73 65 2b 69 20 66 6f 72 20  mber base+i for 
196d5 74 68 65 20 69 2d 74 68 20 63 75 72 73 6f 72 2e  the i-th cursor.
196d6 0a 2a 2a 20 45 78 63 65 70 74 2c 20 69 66 20 74  .** Except, if t
196d7 68 65 72 65 20 69 73 20 6e 6f 20 70 6f 73 73 69  here is no possi
196d8 62 69 6c 69 74 79 20 6f 66 20 61 20 52 45 50 4c  bility of a REPL
196d9 41 43 45 20 61 63 74 69 6f 6e 20 74 68 65 6e 0a  ACE action then.
196da 2a 2a 20 63 75 72 73 6f 72 73 20 64 6f 20 6e 6f  ** cursors do no
196db 74 20 6e 65 65 64 20 74 6f 20 62 65 20 6f 70 65  t need to be ope
196dc 6e 20 66 6f 72 20 69 6e 64 69 63 65 73 20 77 68  n for indices wh
196dd 65 72 65 20 61 49 64 78 55 73 65 64 5b 69 5d 3d  ere aIdxUsed[i]=
196de 3d 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  =0..**.** If the
196df 20 69 73 55 70 64 61 74 65 20 66 6c 61 67 20 69   isUpdate flag i
196e0 73 20 74 72 75 65 2c 20 69 74 20 6d 65 61 6e 73  s true, it means
196e1 20 74 68 61 74 20 74 68 65 20 22 62 61 73 65 22   that the "base"
196e2 20 63 75 72 73 6f 72 20 69 73 0a 2a 2a 20 69 6e   cursor is.** in
196e3 69 74 69 61 6c 6c 79 20 70 6f 69 6e 74 69 6e 67  itially pointing
196e4 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 74 68 61   to an entry tha
196e5 74 20 69 73 20 62 65 69 6e 67 20 75 70 64 61 74  t is being updat
196e6 65 64 2e 20 20 54 68 65 20 69 73 55 70 64 61 74  ed.  The isUpdat
196e7 65 0a 2a 2a 20 66 6c 61 67 20 63 61 75 73 65 73  e.** flag causes
196e8 20 65 78 74 72 61 20 63 6f 64 65 20 74 6f 20 62   extra code to b
196e9 65 20 67 65 6e 65 72 61 74 65 64 20 73 6f 20 74  e generated so t
196ea 68 61 74 20 74 68 65 20 22 62 61 73 65 22 20 63  hat the "base" c
196eb 75 72 73 6f 72 0a 2a 2a 20 69 73 20 73 74 69 6c  ursor.** is stil
196ec 6c 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 74 68  l pointing at th
196ed 65 20 73 61 6d 65 20 65 6e 74 72 79 20 61 66 74  e same entry aft
196ee 65 72 20 74 68 65 20 72 6f 75 74 69 6e 65 20 72  er the routine r
196ef 65 74 75 72 6e 73 2e 0a 2a 2a 20 57 69 74 68 6f  eturns..** Witho
196f0 75 74 20 74 68 65 20 69 73 55 70 64 61 74 65 20  ut the isUpdate 
196f1 66 6c 61 67 2c 20 74 68 65 20 22 62 61 73 65 22  flag, the "base"
196f2 20 63 75 72 73 6f 72 20 6d 69 67 68 74 20 62 65   cursor might be
196f3 20 6d 6f 76 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54   moved..*/.SQLIT
196f4 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
196f5 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 43 6f  qlite3GenerateCo
196f6 6e 73 74 72 61 69 6e 74 43 68 65 63 6b 73 28 0a  nstraintChecks(.
196f7 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
196f8 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
196f9 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ser context */. 
196fa 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20   Table *pTab,   
196fb 20 20 20 20 20 2f 2a 20 74 68 65 20 74 61 62 6c       /* the tabl
196fc 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77 65 20  e into which we 
196fd 61 72 65 20 69 6e 73 65 72 74 69 6e 67 20 2a 2f  are inserting */
196fe 0a 20 20 69 6e 74 20 62 61 73 65 2c 20 20 20 20  .  int base,    
196ff 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
19700 6f 66 20 61 20 72 65 61 64 2f 77 72 69 74 65 20  of a read/write 
19701 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20  cursor pointing 
19702 61 74 20 70 54 61 62 20 2a 2f 0a 20 20 63 68 61  at pTab */.  cha
19703 72 20 2a 61 49 64 78 55 73 65 64 2c 20 20 20 20  r *aIdxUsed,    
19704 20 2f 2a 20 57 68 69 63 68 20 69 6e 64 69 63 65   /* Which indice
19705 73 20 61 72 65 20 75 73 65 64 2e 20 20 4e 55 4c  s are used.  NUL
19706 4c 20 6d 65 61 6e 73 20 61 6c 6c 20 61 72 65 20  L means all are 
19707 75 73 65 64 20 2a 2f 0a 20 20 69 6e 74 20 72 6f  used */.  int ro
19708 77 69 64 43 68 6e 67 2c 20 20 20 20 20 20 2f 2a  widChng,      /*
19709 20 54 72 75 65 20 69 66 20 74 68 65 20 72 65 63   True if the rec
1970a 6f 72 64 20 6e 75 6d 62 65 72 20 77 69 6c 6c 20  ord number will 
1970b 63 68 61 6e 67 65 20 2a 2f 0a 20 20 69 6e 74 20  change */.  int 
1970c 69 73 55 70 64 61 74 65 2c 20 20 20 20 20 20 20  isUpdate,       
1970d 2f 2a 20 54 72 75 65 20 66 6f 72 20 55 50 44 41  /* True for UPDA
1970e 54 45 2c 20 46 61 6c 73 65 20 66 6f 72 20 49 4e  TE, False for IN
1970f 53 45 52 54 20 2a 2f 0a 20 20 69 6e 74 20 6f 76  SERT */.  int ov
19710 65 72 72 69 64 65 45 72 72 6f 72 2c 20 20 2f 2a  errideError,  /*
19711 20 4f 76 65 72 72 69 64 65 20 6f 6e 45 72 72 6f   Override onErro
19712 72 20 74 6f 20 74 68 69 73 20 69 66 20 6e 6f 74  r to this if not
19713 20 4f 45 5f 44 65 66 61 75 6c 74 20 2a 2f 0a 20   OE_Default */. 
19714 20 69 6e 74 20 69 67 6e 6f 72 65 44 65 73 74 20   int ignoreDest 
19715 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20       /* Jump to 
19716 74 68 69 73 20 6c 61 62 65 6c 20 6f 6e 20 61 6e  this label on an
19717 20 4f 45 5f 49 67 6e 6f 72 65 20 72 65 73 6f 6c   OE_Ignore resol
19718 75 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e  ution */.){.  in
19719 74 20 69 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a  t i;.  Vdbe *v;.
1971a 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a 20 20 69 6e    int nCol;.  in
1971b 74 20 6f 6e 45 72 72 6f 72 3b 0a 20 20 69 6e 74  t onError;.  int
1971c 20 61 64 64 72 3b 0a 20 20 69 6e 74 20 65 78 74   addr;.  int ext
1971d 72 61 3b 0a 20 20 69 6e 74 20 69 43 75 72 3b 0a  ra;.  int iCur;.
1971e 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20    Index *pIdx;. 
1971f 20 69 6e 74 20 73 65 65 6e 52 65 70 6c 61 63 65   int seenReplace
19720 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6a 75 6d 70   = 0;.  int jump
19721 49 6e 73 74 31 3d 30 2c 20 6a 75 6d 70 49 6e 73  Inst1=0, jumpIns
19722 74 32 3b 0a 20 20 69 6e 74 20 68 61 73 54 77 6f  t2;.  int hasTwo
19723 52 6f 77 69 64 73 20 3d 20 28 69 73 55 70 64 61  Rowids = (isUpda
19724 74 65 20 26 26 20 72 6f 77 69 64 43 68 6e 67 29  te && rowidChng)
19725 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33  ;..  v = sqlite3
19726 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
19727 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d 30 20  .  assert( v!=0 
19728 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 61  );.  assert( pTa
19729 62 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b  b->pSelect==0 );
1972a 20 20 2f 2a 20 54 68 69 73 20 74 61 62 6c 65 20    /* This table 
1972b 69 73 20 6e 6f 74 20 61 20 56 49 45 57 20 2a 2f  is not a VIEW */
1972c 0a 20 20 6e 43 6f 6c 20 3d 20 70 54 61 62 2d 3e  .  nCol = pTab->
1972d 6e 43 6f 6c 3b 0a 0a 20 20 2f 2a 20 54 65 73 74  nCol;..  /* Test
1972e 20 61 6c 6c 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f   all NOT NULL co
1972f 6e 73 74 72 61 69 6e 74 73 2e 0a 20 20 2a 2f 0a  nstraints..  */.
19730 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f    for(i=0; i<nCo
19731 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  l; i++){.    if(
19732 20 69 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20   i==pTab->iPKey 
19733 29 7b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75  ){.      continu
19734 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6f 6e 45  e;.    }.    onE
19735 72 72 6f 72 20 3d 20 70 54 61 62 2d 3e 61 43 6f  rror = pTab->aCo
19736 6c 5b 69 5d 2e 6e 6f 74 4e 75 6c 6c 3b 0a 20 20  l[i].notNull;.  
19737 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f    if( onError==O
19738 45 5f 4e 6f 6e 65 20 29 20 63 6f 6e 74 69 6e 75  E_None ) continu
19739 65 3b 0a 20 20 20 20 69 66 28 20 6f 76 65 72 72  e;.    if( overr
1973a 69 64 65 45 72 72 6f 72 21 3d 4f 45 5f 44 65 66  ideError!=OE_Def
1973b 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20 6f 6e  ault ){.      on
1973c 45 72 72 6f 72 20 3d 20 6f 76 65 72 72 69 64 65  Error = override
1973d 45 72 72 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65  Error;.    }else
1973e 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45   if( onError==OE
1973f 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20  _Default ){.    
19740 20 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41    onError = OE_A
19741 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  bort;.    }.    
19742 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f  if( onError==OE_
19743 52 65 70 6c 61 63 65 20 26 26 20 70 54 61 62 2d  Replace && pTab-
19744 3e 61 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74 3d 3d  >aCol[i].pDflt==
19745 30 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72  0 ){.      onErr
19746 6f 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20  or = OE_Abort;. 
19747 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
19748 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
19749 44 75 70 2c 20 6e 43 6f 6c 2d 31 2d 69 2c 20 31  Dup, nCol-1-i, 1
1974a 29 3b 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71  );.    addr = sq
1974b 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1974c 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 31 2c  , OP_NotNull, 1,
1974d 20 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28   0);.    assert(
1974e 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 6f 6c   onError==OE_Rol
1974f 6c 62 61 63 6b 20 7c 7c 20 6f 6e 45 72 72 6f 72  lback || onError
19750 3d 3d 4f 45 5f 41 62 6f 72 74 20 7c 7c 20 6f 6e  ==OE_Abort || on
19751 45 72 72 6f 72 3d 3d 4f 45 5f 46 61 69 6c 0a 20  Error==OE_Fail. 
19752 20 20 20 20 20 20 20 7c 7c 20 6f 6e 45 72 72 6f         || onErro
19753 72 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20 7c 7c 20  r==OE_Ignore || 
19754 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c  onError==OE_Repl
19755 61 63 65 20 29 3b 0a 20 20 20 20 73 77 69 74 63  ace );.    switc
19756 68 28 20 6f 6e 45 72 72 6f 72 20 29 7b 0a 20 20  h( onError ){.  
19757 20 20 20 20 63 61 73 65 20 4f 45 5f 52 6f 6c 6c      case OE_Roll
19758 62 61 63 6b 3a 0a 20 20 20 20 20 20 63 61 73 65  back:.      case
19759 20 4f 45 5f 41 62 6f 72 74 3a 0a 20 20 20 20 20   OE_Abort:.     
1975a 20 63 61 73 65 20 4f 45 5f 46 61 69 6c 3a 20 7b   case OE_Fail: {
1975b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a  .        char *z
1975c 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  Msg = 0;.       
1975d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1975e 70 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51  p(v, OP_Halt, SQ
1975f 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c  LITE_CONSTRAINT,
19760 20 6f 6e 45 72 72 6f 72 29 3b 0a 20 20 20 20 20   onError);.     
19761 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72     sqlite3SetStr
19762 69 6e 67 28 26 7a 4d 73 67 2c 20 70 54 61 62 2d  ing(&zMsg, pTab-
19763 3e 7a 4e 61 6d 65 2c 20 22 2e 22 2c 20 70 54 61  >zName, ".", pTa
19764 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65  b->aCol[i].zName
19765 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
19766 20 20 20 20 20 20 20 20 20 20 22 20 6d 61 79 20            " may 
19767 6e 6f 74 20 62 65 20 4e 55 4c 4c 22 2c 20 28 63  not be NULL", (c
19768 68 61 72 2a 29 30 29 3b 0a 20 20 20 20 20 20 20  har*)0);.       
19769 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
1976a 67 65 50 33 28 76 2c 20 2d 31 2c 20 7a 4d 73 67  geP3(v, -1, zMsg
1976b 2c 20 50 33 5f 44 59 4e 41 4d 49 43 29 3b 0a 20  , P3_DYNAMIC);. 
1976c 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1976d 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65      }.      case
1976e 20 4f 45 5f 49 67 6e 6f 72 65 3a 20 7b 0a 20 20   OE_Ignore: {.  
1976f 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
19770 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 50 6f 70  eAddOp(v, OP_Pop
19771 2c 20 6e 43 6f 6c 2b 31 2b 68 61 73 54 77 6f 52  , nCol+1+hasTwoR
19772 6f 77 69 64 73 2c 20 30 29 3b 0a 20 20 20 20 20  owids, 0);.     
19773 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
19774 64 4f 70 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  dOp(v, OP_Goto, 
19775 30 2c 20 69 67 6e 6f 72 65 44 65 73 74 29 3b 0a  0, ignoreDest);.
19776 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
19777 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73       }.      cas
19778 65 20 4f 45 5f 52 65 70 6c 61 63 65 3a 20 7b 0a  e OE_Replace: {.
19779 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
1977a 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20  xprCode(pParse, 
1977b 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 70 44  pTab->aCol[i].pD
1977c 66 6c 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71  flt);.        sq
1977d 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1977e 2c 20 4f 50 5f 50 75 73 68 2c 20 6e 43 6f 6c 2d  , OP_Push, nCol-
1977f 69 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 62  i, 0);.        b
19780 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
19781 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
19782 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
19783 64 64 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ddr);.  }..  /* 
19784 54 65 73 74 20 61 6c 6c 20 43 48 45 43 4b 20 63  Test all CHECK c
19785 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20 2a 2f 0a  onstraints.  */.
19786 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
19787 4d 49 54 5f 43 48 45 43 4b 0a 20 20 69 66 28 20  MIT_CHECK.  if( 
19788 70 54 61 62 2d 3e 70 43 68 65 63 6b 20 26 26 20  pTab->pCheck && 
19789 28 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61  (pParse->db->fla
1978a 67 73 20 26 20 53 51 4c 49 54 45 5f 49 67 6e 6f  gs & SQLITE_Igno
1978b 72 65 43 68 65 63 6b 73 29 3d 3d 30 20 29 7b 0a  reChecks)==0 ){.
1978c 20 20 20 20 69 6e 74 20 61 6c 6c 4f 6b 20 3d 20      int allOk = 
1978d 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
1978e 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 61 73 73  abel(v);.    ass
1978f 65 72 74 28 20 70 50 61 72 73 65 2d 3e 63 6b 4f  ert( pParse->ckO
19790 66 66 73 65 74 3d 3d 30 20 29 3b 0a 20 20 20 20  ffset==0 );.    
19791 70 50 61 72 73 65 2d 3e 63 6b 4f 66 66 73 65 74  pParse->ckOffset
19792 20 3d 20 6e 43 6f 6c 3b 0a 20 20 20 20 73 71 6c   = nCol;.    sql
19793 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 70  ite3ExprIfTrue(p
19794 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 70 43 68  Parse, pTab->pCh
19795 65 63 6b 2c 20 61 6c 6c 4f 6b 2c 20 31 29 3b 0a  eck, allOk, 1);.
19796 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72      assert( pPar
19797 73 65 2d 3e 63 6b 4f 66 66 73 65 74 3d 3d 6e 43  se->ckOffset==nC
19798 6f 6c 20 29 3b 0a 20 20 20 20 70 50 61 72 73 65  ol );.    pParse
19799 2d 3e 63 6b 4f 66 66 73 65 74 20 3d 20 30 3b 0a  ->ckOffset = 0;.
1979a 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 6f 76      onError = ov
1979b 65 72 72 69 64 65 45 72 72 6f 72 21 3d 4f 45 5f  errideError!=OE_
1979c 44 65 66 61 75 6c 74 20 3f 20 6f 76 65 72 72 69  Default ? overri
1979d 64 65 45 72 72 6f 72 20 3a 20 4f 45 5f 41 62 6f  deError : OE_Abo
1979e 72 74 3b 0a 20 20 20 20 69 66 28 20 6f 6e 45 72  rt;.    if( onEr
1979f 72 6f 72 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20 29  ror==OE_Ignore )
197a0 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
197a1 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 50  dbeAddOp(v, OP_P
197a2 6f 70 2c 20 6e 43 6f 6c 2b 31 2b 68 61 73 54 77  op, nCol+1+hasTw
197a3 6f 52 6f 77 69 64 73 2c 20 30 29 3b 0a 20 20 20  oRowids, 0);.   
197a4 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
197a5 64 4f 70 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  dOp(v, OP_Goto, 
197a6 30 2c 20 69 67 6e 6f 72 65 44 65 73 74 29 3b 0a  0, ignoreDest);.
197a7 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
197a8 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
197a9 70 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51  p(v, OP_Halt, SQ
197aa 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c  LITE_CONSTRAINT,
197ab 20 6f 6e 45 72 72 6f 72 29 3b 0a 20 20 20 20 7d   onError);.    }
197ac 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
197ad 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
197ae 61 6c 6c 4f 6b 29 3b 0a 20 20 7d 0a 23 65 6e 64  allOk);.  }.#end
197af 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53  if /* !defined(S
197b0 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b  QLITE_OMIT_CHECK
197b1 29 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 77 65  ) */..  /* If we
197b2 20 68 61 76 65 20 61 6e 20 49 4e 54 45 47 45 52   have an INTEGER
197b3 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 6d 61   PRIMARY KEY, ma
197b4 6b 65 20 73 75 72 65 20 74 68 65 20 70 72 69 6d  ke sure the prim
197b5 61 72 79 20 6b 65 79 0a 20 20 2a 2a 20 6f 66 20  ary key.  ** of 
197b6 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 64  the new record d
197b7 6f 65 73 20 6e 6f 74 20 70 72 65 76 69 6f 75 73  oes not previous
197b8 6c 79 20 65 78 69 73 74 2e 20 20 45 78 63 65 70  ly exist.  Excep
197b9 74 2c 20 69 66 20 74 68 69 73 0a 20 20 2a 2a 20  t, if this.  ** 
197ba 69 73 20 61 6e 20 55 50 44 41 54 45 20 61 6e 64  is an UPDATE and
197bb 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79   the primary key
197bc 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 69 6e 67   is not changing
197bd 2c 20 74 68 61 74 20 69 73 20 4f 4b 2e 0a 20 20  , that is OK..  
197be 2a 2f 0a 20 20 69 66 28 20 72 6f 77 69 64 43 68  */.  if( rowidCh
197bf 6e 67 20 29 7b 0a 20 20 20 20 6f 6e 45 72 72 6f  ng ){.    onErro
197c0 72 20 3d 20 70 54 61 62 2d 3e 6b 65 79 43 6f 6e  r = pTab->keyCon
197c1 66 3b 0a 20 20 20 20 69 66 28 20 6f 76 65 72 72  f;.    if( overr
197c2 69 64 65 45 72 72 6f 72 21 3d 4f 45 5f 44 65 66  ideError!=OE_Def
197c3 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20 6f 6e  ault ){.      on
197c4 45 72 72 6f 72 20 3d 20 6f 76 65 72 72 69 64 65  Error = override
197c5 45 72 72 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65  Error;.    }else
197c6 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45   if( onError==OE
197c7 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20  _Default ){.    
197c8 20 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41    onError = OE_A
197c9 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  bort;.    }.    
197ca 0a 20 20 20 20 69 66 28 20 69 73 55 70 64 61 74  .    if( isUpdat
197cb 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  e ){.      sqlit
197cc 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
197cd 50 5f 44 75 70 2c 20 6e 43 6f 6c 2b 31 2c 20 31  P_Dup, nCol+1, 1
197ce 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
197cf 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
197d0 44 75 70 2c 20 6e 43 6f 6c 2b 31 2c 20 31 29 3b  Dup, nCol+1, 1);
197d1 0a 20 20 20 20 20 20 6a 75 6d 70 49 6e 73 74 31  .      jumpInst1
197d2 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
197d3 64 4f 70 28 76 2c 20 4f 50 5f 45 71 2c 20 30 2c  dOp(v, OP_Eq, 0,
197d4 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73   0);.    }.    s
197d5 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
197d6 76 2c 20 4f 50 5f 44 75 70 2c 20 6e 43 6f 6c 2c  v, OP_Dup, nCol,
197d7 20 31 29 3b 0a 20 20 20 20 6a 75 6d 70 49 6e 73   1);.    jumpIns
197d8 74 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  t2 = sqlite3Vdbe
197d9 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4e 6f 74 45  AddOp(v, OP_NotE
197da 78 69 73 74 73 2c 20 62 61 73 65 2c 20 30 29 3b  xists, base, 0);
197db 0a 20 20 20 20 73 77 69 74 63 68 28 20 6f 6e 45  .    switch( onE
197dc 72 72 6f 72 20 29 7b 0a 20 20 20 20 20 20 64 65  rror ){.      de
197dd 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20  fault: {.       
197de 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62   onError = OE_Ab
197df 6f 72 74 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20  ort;.        /* 
197e0 46 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20 74  Fall thru into t
197e1 68 65 20 6e 65 78 74 20 63 61 73 65 20 2a 2f 0a  he next case */.
197e2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61        }.      ca
197e3 73 65 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 3a 0a  se OE_Rollback:.
197e4 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 41 62        case OE_Ab
197e5 6f 72 74 3a 0a 20 20 20 20 20 20 63 61 73 65 20  ort:.      case 
197e6 4f 45 5f 46 61 69 6c 3a 20 7b 0a 20 20 20 20 20  OE_Fail: {.     
197e7 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70     sqlite3VdbeOp
197e8 33 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51  3(v, OP_Halt, SQ
197e9 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c  LITE_CONSTRAINT,
197ea 20 6f 6e 45 72 72 6f 72 2c 0a 20 20 20 20 20 20   onError,.      
197eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
197ec 20 20 20 22 50 52 49 4d 41 52 59 20 4b 45 59 20     "PRIMARY KEY 
197ed 6d 75 73 74 20 62 65 20 75 6e 69 71 75 65 22 2c  must be unique",
197ee 20 50 33 5f 53 54 41 54 49 43 29 3b 0a 20 20 20   P3_STATIC);.   
197ef 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
197f0 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 4f    }.      case O
197f1 45 5f 52 65 70 6c 61 63 65 3a 20 7b 0a 20 20 20  E_Replace: {.   
197f2 20 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65       sqlite3Gene
197f3 72 61 74 65 52 6f 77 49 6e 64 65 78 44 65 6c 65  rateRowIndexDele
197f4 74 65 28 76 2c 20 70 54 61 62 2c 20 62 61 73 65  te(v, pTab, base
197f5 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66  , 0);.        if
197f6 28 20 69 73 55 70 64 61 74 65 20 29 7b 0a 20 20  ( isUpdate ){.  
197f7 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
197f8 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 44  dbeAddOp(v, OP_D
197f9 75 70 2c 20 6e 43 6f 6c 2b 68 61 73 54 77 6f 52  up, nCol+hasTwoR
197fa 6f 77 69 64 73 2c 20 31 29 3b 0a 20 20 20 20 20  owids, 1);.     
197fb 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
197fc 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 6f 76 65  AddOp(v, OP_Move
197fd 47 65 2c 20 62 61 73 65 2c 20 30 29 3b 0a 20 20  Ge, base, 0);.  
197fe 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
197ff 73 65 65 6e 52 65 70 6c 61 63 65 20 3d 20 31 3b  seenReplace = 1;
19800 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
19801 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61        }.      ca
19802 73 65 20 4f 45 5f 49 67 6e 6f 72 65 3a 20 7b 0a  se OE_Ignore: {.
19803 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
19804 73 65 65 6e 52 65 70 6c 61 63 65 3d 3d 30 20 29  seenReplace==0 )
19805 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
19806 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
19807 5f 50 6f 70 2c 20 6e 43 6f 6c 2b 31 2b 68 61 73  _Pop, nCol+1+has
19808 54 77 6f 52 6f 77 69 64 73 2c 20 30 29 3b 0a 20  TwoRowids, 0);. 
19809 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1980a 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 47 6f  beAddOp(v, OP_Go
1980b 74 6f 2c 20 30 2c 20 69 67 6e 6f 72 65 44 65 73  to, 0, ignoreDes
1980c 74 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  t);.        brea
1980d 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
1980e 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1980f 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 75 6d 70  JumpHere(v, jump
19810 49 6e 73 74 32 29 3b 0a 20 20 20 20 69 66 28 20  Inst2);.    if( 
19811 69 73 55 70 64 61 74 65 20 29 7b 0a 20 20 20 20  isUpdate ){.    
19812 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
19813 70 48 65 72 65 28 76 2c 20 6a 75 6d 70 49 6e 73  pHere(v, jumpIns
19814 74 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  t1);.      sqlit
19815 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
19816 50 5f 44 75 70 2c 20 6e 43 6f 6c 2b 31 2c 20 31  P_Dup, nCol+1, 1
19817 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
19818 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
19819 4d 6f 76 65 47 65 2c 20 62 61 73 65 2c 20 30 29  MoveGe, base, 0)
1981a 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
1981b 2a 20 54 65 73 74 20 61 6c 6c 20 55 4e 49 51 55  * Test all UNIQU
1981c 45 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 62 79  E constraints by
1981d 20 63 72 65 61 74 69 6e 67 20 65 6e 74 72 69 65   creating entrie
1981e 73 20 66 6f 72 20 65 61 63 68 20 55 4e 49 51 55  s for each UNIQU
1981f 45 0a 20 20 2a 2a 20 69 6e 64 65 78 20 61 6e 64  E.  ** index and
19820 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 74 68 61   making sure tha
19821 74 20 64 75 70 6c 69 63 61 74 65 20 65 6e 74 72  t duplicate entr
19822 69 65 73 20 64 6f 20 6e 6f 74 20 61 6c 72 65 61  ies do not alrea
19823 64 79 20 65 78 69 73 74 2e 0a 20 20 2a 2a 20 41  dy exist..  ** A
19824 64 64 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72  dd the new recor
19825 64 73 20 74 6f 20 74 68 65 20 69 6e 64 69 63 65  ds to the indice
19826 73 20 61 73 20 77 65 20 67 6f 2e 0a 20 20 2a 2f  s as we go..  */
19827 0a 20 20 65 78 74 72 61 20 3d 20 2d 31 3b 0a 20  .  extra = -1;. 
19828 20 66 6f 72 28 69 43 75 72 3d 30 2c 20 70 49 64   for(iCur=0, pId
19829 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
1982a 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d  pIdx; pIdx=pIdx-
1982b 3e 70 4e 65 78 74 2c 20 69 43 75 72 2b 2b 29 7b  >pNext, iCur++){
1982c 0a 20 20 20 20 69 66 28 20 61 49 64 78 55 73 65  .    if( aIdxUse
1982d 64 20 26 26 20 61 49 64 78 55 73 65 64 5b 69 43  d && aIdxUsed[iC
1982e 75 72 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75  ur]==0 ) continu
1982f 65 3b 20 20 2f 2a 20 53 6b 69 70 20 75 6e 75 73  e;  /* Skip unus
19830 65 64 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20  ed indices */.  
19831 20 20 65 78 74 72 61 2b 2b 3b 0a 0a 20 20 20 20    extra++;..    
19832 2f 2a 20 43 72 65 61 74 65 20 61 20 6b 65 79 20  /* Create a key 
19833 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20 74 68  for accessing th
19834 65 20 69 6e 64 65 78 20 65 6e 74 72 79 20 2a 2f  e index entry */
19835 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
19836 41 64 64 4f 70 28 76 2c 20 4f 50 5f 44 75 70 2c  AddOp(v, OP_Dup,
19837 20 6e 43 6f 6c 2b 65 78 74 72 61 2c 20 31 29 3b   nCol+extra, 1);
19838 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
19839 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69  pIdx->nColumn; i
1983a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  ++){.      int i
1983b 64 78 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c  dx = pIdx->aiCol
1983c 75 6d 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66  umn[i];.      if
1983d 28 20 69 64 78 3d 3d 70 54 61 62 2d 3e 69 50 4b  ( idx==pTab->iPK
1983e 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ey ){.        sq
1983f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
19840 2c 20 4f 50 5f 44 75 70 2c 20 69 2b 65 78 74 72  , OP_Dup, i+extr
19841 61 2b 6e 43 6f 6c 2b 31 2c 20 31 29 3b 0a 20 20  a+nCol+1, 1);.  
19842 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
19843 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
19844 64 4f 70 28 76 2c 20 4f 50 5f 44 75 70 2c 20 69  dOp(v, OP_Dup, i
19845 2b 65 78 74 72 61 2b 6e 43 6f 6c 2d 69 64 78 2c  +extra+nCol-idx,
19846 20 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   1);.      }.   
19847 20 7d 0a 20 20 20 20 6a 75 6d 70 49 6e 73 74 31   }.    jumpInst1
19848 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
19849 64 4f 70 28 76 2c 20 4f 50 5f 4d 61 6b 65 49 64  dOp(v, OP_MakeId
1984a 78 52 65 63 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c  xRec, pIdx->nCol
1984b 75 6d 6e 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  umn, 0);.    sql
1984c 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74  ite3IndexAffinit
1984d 79 53 74 72 28 76 2c 20 70 49 64 78 29 3b 0a 0a  yStr(v, pIdx);..
1984e 20 20 20 20 2f 2a 20 46 69 6e 64 20 6f 75 74 20      /* Find out 
1984f 77 68 61 74 20 61 63 74 69 6f 6e 20 74 6f 20 74  what action to t
19850 61 6b 65 20 69 6e 20 63 61 73 65 20 74 68 65 72  ake in case ther
19851 65 20 69 73 20 61 6e 20 69 6e 64 65 78 69 6e 67  e is an indexing
19852 20 63 6f 6e 66 6c 69 63 74 20 2a 2f 0a 20 20 20   conflict */.   
19853 20 6f 6e 45 72 72 6f 72 20 3d 20 70 49 64 78 2d   onError = pIdx-
19854 3e 6f 6e 45 72 72 6f 72 3b 0a 20 20 20 20 69 66  >onError;.    if
19855 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 4e 6f  ( onError==OE_No
19856 6e 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b 20 20  ne ) continue;  
19857 2f 2a 20 70 49 64 78 20 69 73 20 6e 6f 74 20 61  /* pIdx is not a
19858 20 55 4e 49 51 55 45 20 69 6e 64 65 78 20 2a 2f   UNIQUE index */
19859 0a 20 20 20 20 69 66 28 20 6f 76 65 72 72 69 64  .    if( overrid
1985a 65 45 72 72 6f 72 21 3d 4f 45 5f 44 65 66 61 75  eError!=OE_Defau
1985b 6c 74 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72  lt ){.      onEr
1985c 72 6f 72 20 3d 20 6f 76 65 72 72 69 64 65 45 72  ror = overrideEr
1985d 72 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  ror;.    }else i
1985e 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44  f( onError==OE_D
1985f 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20  efault ){.      
19860 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f  onError = OE_Abo
19861 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  rt;.    }.    if
19862 28 20 73 65 65 6e 52 65 70 6c 61 63 65 20 29 7b  ( seenReplace ){
19863 0a 20 20 20 20 20 20 69 66 28 20 6f 6e 45 72 72  .      if( onErr
19864 6f 72 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20 29 20  or==OE_Ignore ) 
19865 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 52 65 70  onError = OE_Rep
19866 6c 61 63 65 3b 0a 20 20 20 20 20 20 65 6c 73 65  lace;.      else
19867 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45   if( onError==OE
19868 5f 46 61 69 6c 20 29 20 6f 6e 45 72 72 6f 72 20  _Fail ) onError 
19869 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20  = OE_Abort;.    
1986a 7d 0a 20 20 20 20 0a 0a 20 20 20 20 2f 2a 20 43  }.    ..    /* C
1986b 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74  heck to see if t
1986c 68 65 20 6e 65 77 20 69 6e 64 65 78 20 65 6e 74  he new index ent
1986d 72 79 20 77 69 6c 6c 20 62 65 20 75 6e 69 71 75  ry will be uniqu
1986e 65 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  e */.    sqlite3
1986f 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
19870 44 75 70 2c 20 65 78 74 72 61 2b 6e 43 6f 6c 2b  Dup, extra+nCol+
19871 31 2b 68 61 73 54 77 6f 52 6f 77 69 64 73 2c 20  1+hasTwoRowids, 
19872 31 29 3b 0a 20 20 20 20 6a 75 6d 70 49 6e 73 74  1);.    jumpInst
19873 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  2 = sqlite3VdbeA
19874 64 64 4f 70 28 76 2c 20 4f 50 5f 49 73 55 6e 69  ddOp(v, OP_IsUni
19875 71 75 65 2c 20 62 61 73 65 2b 69 43 75 72 2b 31  que, base+iCur+1
19876 2c 20 30 29 3b 0a 0a 20 20 20 20 2f 2a 20 47 65  , 0);..    /* Ge
19877 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74  nerate code that
19878 20 65 78 65 63 75 74 65 73 20 69 66 20 74 68 65   executes if the
19879 20 6e 65 77 20 69 6e 64 65 78 20 65 6e 74 72 79   new index entry
1987a 20 69 73 20 6e 6f 74 20 75 6e 69 71 75 65 20 2a   is not unique *
1987b 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 6f 6e  /.    assert( on
1987c 45 72 72 6f 72 3d 3d 4f 45 5f 52 6f 6c 6c 62 61  Error==OE_Rollba
1987d 63 6b 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f  ck || onError==O
1987e 45 5f 41 62 6f 72 74 20 7c 7c 20 6f 6e 45 72 72  E_Abort || onErr
1987f 6f 72 3d 3d 4f 45 5f 46 61 69 6c 0a 20 20 20 20  or==OE_Fail.    
19880 20 20 20 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d      || onError==
19881 4f 45 5f 49 67 6e 6f 72 65 20 7c 7c 20 6f 6e 45  OE_Ignore || onE
19882 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65  rror==OE_Replace
19883 20 29 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20   );.    switch( 
19884 6f 6e 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 20  onError ){.     
19885 20 63 61 73 65 20 4f 45 5f 52 6f 6c 6c 62 61 63   case OE_Rollbac
19886 6b 3a 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45  k:.      case OE
19887 5f 41 62 6f 72 74 3a 0a 20 20 20 20 20 20 63 61  _Abort:.      ca
19888 73 65 20 4f 45 5f 46 61 69 6c 3a 20 7b 0a 20 20  se OE_Fail: {.  
19889 20 20 20 20 20 20 69 6e 74 20 6a 2c 20 6e 31 2c        int j, n1,
1988a 20 6e 32 3b 0a 20 20 20 20 20 20 20 20 63 68 61   n2;.        cha
1988b 72 20 7a 45 72 72 4d 73 67 5b 32 30 30 5d 3b 0a  r zErrMsg[200];.
1988c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
1988d 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28  snprintf(sizeof(
1988e 7a 45 72 72 4d 73 67 29 2c 20 7a 45 72 72 4d 73  zErrMsg), zErrMs
1988f 67 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  g,.             
19890 20 20 20 20 20 20 20 20 20 20 20 20 70 49 64 78              pIdx
19891 2d 3e 6e 43 6f 6c 75 6d 6e 3e 31 20 3f 20 22 63  ->nColumn>1 ? "c
19892 6f 6c 75 6d 6e 73 20 22 20 3a 20 22 63 6f 6c 75  olumns " : "colu
19893 6d 6e 20 22 29 3b 0a 20 20 20 20 20 20 20 20 6e  mn ");.        n
19894 31 20 3d 20 73 74 72 6c 65 6e 28 7a 45 72 72 4d  1 = strlen(zErrM
19895 73 67 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  sg);.        for
19896 28 6a 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e 6e 43  (j=0; j<pIdx->nC
19897 6f 6c 75 6d 6e 20 26 26 20 6e 31 3c 73 69 7a 65  olumn && n1<size
19898 6f 66 28 7a 45 72 72 4d 73 67 29 2d 33 30 3b 20  of(zErrMsg)-30; 
19899 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  j++){.          
1989a 63 68 61 72 20 2a 7a 43 6f 6c 20 3d 20 70 54 61  char *zCol = pTa
1989b 62 2d 3e 61 43 6f 6c 5b 70 49 64 78 2d 3e 61 69  b->aCol[pIdx->ai
1989c 43 6f 6c 75 6d 6e 5b 6a 5d 5d 2e 7a 4e 61 6d 65  Column[j]].zName
1989d 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 32 20 3d  ;.          n2 =
1989e 20 73 74 72 6c 65 6e 28 7a 43 6f 6c 29 3b 0a 20   strlen(zCol);. 
1989f 20 20 20 20 20 20 20 20 20 69 66 28 20 6a 3e 30           if( j>0
198a0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
198a1 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
198a2 28 73 69 7a 65 6f 66 28 7a 45 72 72 4d 73 67 29  (sizeof(zErrMsg)
198a3 2d 6e 31 2c 20 26 7a 45 72 72 4d 73 67 5b 6e 31  -n1, &zErrMsg[n1
198a4 5d 2c 20 22 2c 20 22 29 3b 0a 20 20 20 20 20 20  ], ", ");.      
198a5 20 20 20 20 20 20 6e 31 20 2b 3d 20 32 3b 0a 20        n1 += 2;. 
198a6 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
198a7 20 20 20 20 20 69 66 28 20 6e 31 2b 6e 32 3e 73       if( n1+n2>s
198a8 69 7a 65 6f 66 28 7a 45 72 72 4d 73 67 29 2d 33  izeof(zErrMsg)-3
198a9 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
198aa 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
198ab 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 4d 73 67  f(sizeof(zErrMsg
198ac 29 2d 6e 31 2c 20 26 7a 45 72 72 4d 73 67 5b 6e  )-n1, &zErrMsg[n
198ad 31 5d 2c 20 22 2e 2e 2e 22 29 3b 0a 20 20 20 20  1], "...");.    
198ae 20 20 20 20 20 20 20 20 6e 31 20 2b 3d 20 33 3b          n1 += 3;
198af 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65  .            bre
198b0 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65  ak;.          }e
198b1 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
198b2 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
198b3 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 4d 73 67  f(sizeof(zErrMsg
198b4 29 2d 6e 31 2c 20 26 7a 45 72 72 4d 73 67 5b 6e  )-n1, &zErrMsg[n
198b5 31 5d 2c 20 22 25 73 22 2c 20 7a 43 6f 6c 29 3b  1], "%s", zCol);
198b6 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 31 20  .            n1 
198b7 2b 3d 20 6e 32 3b 0a 20 20 20 20 20 20 20 20 20  += n2;.         
198b8 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
198b9 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
198ba 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72  rintf(sizeof(zEr
198bb 72 4d 73 67 29 2d 6e 31 2c 20 26 7a 45 72 72 4d  rMsg)-n1, &zErrM
198bc 73 67 5b 6e 31 5d 2c 20 0a 20 20 20 20 20 20 20  sg[n1], .       
198bd 20 20 20 20 20 70 49 64 78 2d 3e 6e 43 6f 6c 75       pIdx->nColu
198be 6d 6e 3e 31 20 3f 20 22 20 61 72 65 20 6e 6f 74  mn>1 ? " are not
198bf 20 75 6e 69 71 75 65 22 20 3a 20 22 20 69 73 20   unique" : " is 
198c0 6e 6f 74 20 75 6e 69 71 75 65 22 29 3b 0a 20 20  not unique");.  
198c1 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
198c2 65 4f 70 33 28 76 2c 20 4f 50 5f 48 61 6c 74 2c  eOp3(v, OP_Halt,
198c3 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49   SQLITE_CONSTRAI
198c4 4e 54 2c 20 6f 6e 45 72 72 6f 72 2c 20 7a 45 72  NT, onError, zEr
198c5 72 4d 73 67 2c 20 30 29 3b 0a 20 20 20 20 20 20  rMsg, 0);.      
198c6 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
198c7 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 49  .      case OE_I
198c8 67 6e 6f 72 65 3a 20 7b 0a 20 20 20 20 20 20 20  gnore: {.       
198c9 20 61 73 73 65 72 74 28 20 73 65 65 6e 52 65 70   assert( seenRep
198ca 6c 61 63 65 3d 3d 30 20 29 3b 0a 20 20 20 20 20  lace==0 );.     
198cb 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
198cc 64 4f 70 28 76 2c 20 4f 50 5f 50 6f 70 2c 20 6e  dOp(v, OP_Pop, n
198cd 43 6f 6c 2b 65 78 74 72 61 2b 33 2b 68 61 73 54  Col+extra+3+hasT
198ce 77 6f 52 6f 77 69 64 73 2c 20 30 29 3b 0a 20 20  woRowids, 0);.  
198cf 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
198d0 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 47 6f 74  eAddOp(v, OP_Got
198d1 6f 2c 20 30 2c 20 69 67 6e 6f 72 65 44 65 73 74  o, 0, ignoreDest
198d2 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  );.        break
198d3 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
198d4 63 61 73 65 20 4f 45 5f 52 65 70 6c 61 63 65 3a  case OE_Replace:
198d5 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   {.        sqlit
198d6 65 33 47 65 6e 65 72 61 74 65 52 6f 77 44 65 6c  e3GenerateRowDel
198d7 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ete(pParse->db, 
198d8 76 2c 20 70 54 61 62 2c 20 62 61 73 65 2c 20 30  v, pTab, base, 0
198d9 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  );.        if( i
198da 73 55 70 64 61 74 65 20 29 7b 0a 20 20 20 20 20  sUpdate ){.     
198db 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
198dc 41 64 64 4f 70 28 76 2c 20 4f 50 5f 44 75 70 2c  AddOp(v, OP_Dup,
198dd 20 6e 43 6f 6c 2b 65 78 74 72 61 2b 31 2b 68 61   nCol+extra+1+ha
198de 73 54 77 6f 52 6f 77 69 64 73 2c 20 31 29 3b 0a  sTwoRowids, 1);.
198df 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
198e0 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
198e1 5f 4d 6f 76 65 47 65 2c 20 62 61 73 65 2c 20 30  _MoveGe, base, 0
198e2 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
198e3 20 20 20 20 20 73 65 65 6e 52 65 70 6c 61 63 65       seenReplace
198e4 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 62 72   = 1;.        br
198e5 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
198e6 20 7d 0a 23 69 66 20 4e 55 4c 4c 5f 44 49 53 54   }.#if NULL_DIST
198e7 49 4e 43 54 5f 46 4f 52 5f 55 4e 49 51 55 45 0a  INCT_FOR_UNIQUE.
198e8 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
198e9 75 6d 70 48 65 72 65 28 76 2c 20 6a 75 6d 70 49  umpHere(v, jumpI
198ea 6e 73 74 31 29 3b 0a 23 65 6e 64 69 66 0a 20 20  nst1);.#endif.  
198eb 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
198ec 70 48 65 72 65 28 76 2c 20 6a 75 6d 70 49 6e 73  pHere(v, jumpIns
198ed 74 32 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  t2);.  }.}../*.*
198ee 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67  * This routine g
198ef 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20 74 6f  enerates code to
198f0 20 66 69 6e 69 73 68 20 74 68 65 20 49 4e 53 45   finish the INSE
198f1 52 54 20 6f 72 20 55 50 44 41 54 45 20 6f 70 65  RT or UPDATE ope
198f2 72 61 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 77  ration.** that w
198f3 61 73 20 73 74 61 72 74 65 64 20 62 79 20 61 20  as started by a 
198f4 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71  prior call to sq
198f5 6c 69 74 65 33 47 65 6e 65 72 61 74 65 43 6f 6e  lite3GenerateCon
198f6 73 74 72 61 69 6e 74 43 68 65 63 6b 73 2e 0a 2a  straintChecks..*
198f7 2a 20 54 68 65 20 73 74 61 63 6b 20 6d 75 73 74  * The stack must
198f8 20 63 6f 6e 74 61 69 6e 20 6b 65 79 73 20 66 6f   contain keys fo
198f9 72 20 61 6c 6c 20 61 63 74 69 76 65 20 69 6e 64  r all active ind
198fa 69 63 65 73 20 66 6f 6c 6c 6f 77 65 64 20 62 79  ices followed by
198fb 20 64 61 74 61 0a 2a 2a 20 61 6e 64 20 74 68 65   data.** and the
198fc 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20 6e   rowid for the n
198fd 65 77 20 65 6e 74 72 79 2e 20 20 54 68 69 73 20  ew entry.  This 
198fe 72 6f 75 74 69 6e 65 20 63 72 65 61 74 65 73 20  routine creates 
198ff 74 68 65 20 6e 65 77 0a 2a 2a 20 65 6e 74 72 69  the new.** entri
19900 65 73 20 69 6e 20 61 6c 6c 20 69 6e 64 69 63 65  es in all indice
19901 73 20 61 6e 64 20 69 6e 20 74 68 65 20 6d 61 69  s and in the mai
19902 6e 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54  n table..**.** T
19903 68 65 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  he arguments to 
19904 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f  this routine sho
19905 75 6c 64 20 62 65 20 74 68 65 20 73 61 6d 65 20  uld be the same 
19906 61 73 20 74 68 65 20 66 69 72 73 74 20 73 69 78  as the first six
19907 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20 74 6f  .** arguments to
19908 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65   sqlite3Generate
19909 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 63 6b 73  ConstraintChecks
1990a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1990b 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1990c 43 6f 6d 70 6c 65 74 65 49 6e 73 65 72 74 69 6f  CompleteInsertio
1990d 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  n(.  Parse *pPar
1990e 73 65 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20  se,      /* The 
1990f 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a  parser context *
19910 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c  /.  Table *pTab,
19911 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 74          /* the t
19912 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20  able into which 
19913 77 65 20 61 72 65 20 69 6e 73 65 72 74 69 6e 67  we are inserting
19914 20 2a 2f 0a 20 20 69 6e 74 20 62 61 73 65 2c 20   */.  int base, 
19915 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
19916 65 78 20 6f 66 20 61 20 72 65 61 64 2f 77 72 69  ex of a read/wri
19917 74 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 69  te cursor pointi
19918 6e 67 20 61 74 20 70 54 61 62 20 2a 2f 0a 20 20  ng at pTab */.  
19919 63 68 61 72 20 2a 61 49 64 78 55 73 65 64 2c 20  char *aIdxUsed, 
1991a 20 20 20 20 2f 2a 20 57 68 69 63 68 20 69 6e 64      /* Which ind
1991b 69 63 65 73 20 61 72 65 20 75 73 65 64 2e 20 20  ices are used.  
1991c 4e 55 4c 4c 20 6d 65 61 6e 73 20 61 6c 6c 20 61  NULL means all a
1991d 72 65 20 75 73 65 64 20 2a 2f 0a 20 20 69 6e 74  re used */.  int
1991e 20 72 6f 77 69 64 43 68 6e 67 2c 20 20 20 20 20   rowidChng,     
1991f 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20   /* True if the 
19920 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 77 69  record number wi
19921 6c 6c 20 63 68 61 6e 67 65 20 2a 2f 0a 20 20 69  ll change */.  i
19922 6e 74 20 69 73 55 70 64 61 74 65 2c 20 20 20 20  nt isUpdate,    
19923 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 55     /* True for U
19924 50 44 41 54 45 2c 20 46 61 6c 73 65 20 66 6f 72  PDATE, False for
19925 20 49 4e 53 45 52 54 20 2a 2f 0a 20 20 69 6e 74   INSERT */.  int
19926 20 6e 65 77 49 64 78 2c 20 20 20 20 20 20 20 20   newIdx,        
19927 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 4e 45 57   /* Index of NEW
19928 20 74 61 62 6c 65 20 66 6f 72 20 74 72 69 67 67   table for trigg
19929 65 72 73 2e 20 20 2d 31 20 69 66 20 6e 6f 6e 65  ers.  -1 if none
1992a 20 2a 2f 0a 20 20 69 6e 74 20 61 70 70 65 6e 64   */.  int append
1992b 42 69 61 73 20 20 20 20 20 20 2f 2a 20 54 72 75  Bias      /* Tru
1992c 65 20 69 66 20 74 68 69 73 20 69 73 20 6c 69 6b  e if this is lik
1992d 65 6c 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70  ely to be an app
1992e 65 6e 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  end */.){.  int 
1992f 69 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20  i;.  Vdbe *v;.  
19930 69 6e 74 20 6e 49 64 78 3b 0a 20 20 49 6e 64 65  int nIdx;.  Inde
19931 78 20 2a 70 49 64 78 3b 0a 20 20 69 6e 74 20 70  x *pIdx;.  int p
19932 69 6b 5f 66 6c 61 67 73 3b 0a 0a 20 20 76 20 3d  ik_flags;..  v =
19933 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
19934 70 50 61 72 73 65 29 3b 0a 20 20 61 73 73 65 72  pParse);.  asser
19935 74 28 20 76 21 3d 30 20 29 3b 0a 20 20 61 73 73  t( v!=0 );.  ass
19936 65 72 74 28 20 70 54 61 62 2d 3e 70 53 65 6c 65  ert( pTab->pSele
19937 63 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 54 68 69  ct==0 );  /* Thi
19938 73 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 61  s table is not a
19939 20 56 49 45 57 20 2a 2f 0a 20 20 66 6f 72 28 6e   VIEW */.  for(n
1993a 49 64 78 3d 30 2c 20 70 49 64 78 3d 70 54 61 62  Idx=0, pIdx=pTab
1993b 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
1993c 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
1993d 2c 20 6e 49 64 78 2b 2b 29 7b 7d 0a 20 20 66 6f  , nIdx++){}.  fo
1993e 72 28 69 3d 6e 49 64 78 2d 31 3b 20 69 3e 3d 30  r(i=nIdx-1; i>=0
1993f 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 69 66 28 20  ; i--){.    if( 
19940 61 49 64 78 55 73 65 64 20 26 26 20 61 49 64 78  aIdxUsed && aIdx
19941 55 73 65 64 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e  Used[i]==0 ) con
19942 74 69 6e 75 65 3b 0a 20 20 20 20 73 71 6c 69 74  tinue;.    sqlit
19943 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
19944 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 62 61 73  P_IdxInsert, bas
19945 65 2b 69 2b 31 2c 20 30 29 3b 0a 20 20 7d 0a 20  e+i+1, 0);.  }. 
19946 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
19947 70 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f  p(v, OP_MakeReco
19948 72 64 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20  rd, pTab->nCol, 
19949 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 54 61 62  0);.  sqlite3Tab
1994a 6c 65 41 66 66 69 6e 69 74 79 53 74 72 28 76 2c  leAffinityStr(v,
1994b 20 70 54 61 62 29 3b 0a 23 69 66 6e 64 65 66 20   pTab);.#ifndef 
1994c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
1994d 47 45 52 0a 20 20 69 66 28 20 6e 65 77 49 64 78  GER.  if( newIdx
1994e 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  >=0 ){.    sqlit
1994f 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
19950 50 5f 44 75 70 2c 20 31 2c 20 30 29 3b 0a 20 20  P_Dup, 1, 0);.  
19951 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
19952 4f 70 28 76 2c 20 4f 50 5f 44 75 70 2c 20 31 2c  Op(v, OP_Dup, 1,
19953 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
19954 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
19955 49 6e 73 65 72 74 2c 20 6e 65 77 49 64 78 2c 20  Insert, newIdx, 
19956 30 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20  0);.  }.#endif. 
19957 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73   if( pParse->nes
19958 74 65 64 20 29 7b 0a 20 20 20 20 70 69 6b 5f 66  ted ){.    pik_f
19959 6c 61 67 73 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  lags = 0;.  }els
1995a 65 7b 0a 20 20 20 20 70 69 6b 5f 66 6c 61 67 73  e{.    pik_flags
1995b 20 3d 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47   = OPFLAG_NCHANG
1995c 45 3b 0a 20 20 20 20 70 69 6b 5f 66 6c 61 67 73  E;.    pik_flags
1995d 20 7c 3d 20 28 69 73 55 70 64 61 74 65 3f 4f 50   |= (isUpdate?OP
1995e 46 4c 41 47 5f 49 53 55 50 44 41 54 45 3a 4f 50  FLAG_ISUPDATE:OP
1995f 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44 29 3b  FLAG_LASTROWID);
19960 0a 20 20 7d 0a 20 20 69 66 28 20 61 70 70 65 6e  .  }.  if( appen
19961 64 42 69 61 73 20 29 7b 0a 20 20 20 20 70 69 6b  dBias ){.    pik
19962 5f 66 6c 61 67 73 20 7c 3d 20 4f 50 46 4c 41 47  _flags |= OPFLAG
19963 5f 41 50 50 45 4e 44 3b 0a 20 20 7d 0a 20 20 73  _APPEND;.  }.  s
19964 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
19965 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 62 61  v, OP_Insert, ba
19966 73 65 2c 20 70 69 6b 5f 66 6c 61 67 73 29 3b 0a  se, pik_flags);.
19967 20 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e 6e    if( !pParse->n
19968 65 73 74 65 64 20 29 7b 0a 20 20 20 20 73 71 6c  ested ){.    sql
19969 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33  ite3VdbeChangeP3
1996a 28 76 2c 20 2d 31 2c 20 70 54 61 62 2d 3e 7a 4e  (v, -1, pTab->zN
1996b 61 6d 65 2c 20 50 33 5f 53 54 41 54 49 43 29 3b  ame, P3_STATIC);
1996c 0a 20 20 7d 0a 20 20 0a 20 20 69 66 28 20 69 73  .  }.  .  if( is
1996d 55 70 64 61 74 65 20 26 26 20 72 6f 77 69 64 43  Update && rowidC
1996e 68 6e 67 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  hng ){.    sqlit
1996f 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
19970 50 5f 50 6f 70 2c 20 31 2c 20 30 29 3b 0a 20 20  P_Pop, 1, 0);.  
19971 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  }.}../*.** Gener
19972 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69  ate code that wi
19973 6c 6c 20 6f 70 65 6e 20 63 75 72 73 6f 72 73 20  ll open cursors 
19974 66 6f 72 20 61 20 74 61 62 6c 65 20 61 6e 64 20  for a table and 
19975 66 6f 72 20 61 6c 6c 0a 2a 2a 20 69 6e 64 69 63  for all.** indic
19976 65 73 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65  es of that table
19977 2e 20 20 54 68 65 20 22 62 61 73 65 22 20 70 61  .  The "base" pa
19978 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 63  rameter is the c
19979 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 75 73 65  ursor number use
1997a 64 0a 2a 2a 20 66 6f 72 20 74 68 65 20 74 61 62  d.** for the tab
1997b 6c 65 2e 20 20 49 6e 64 69 63 65 73 20 61 72 65  le.  Indices are
1997c 20 6f 70 65 6e 65 64 20 6f 6e 20 73 75 62 73 65   opened on subse
1997d 71 75 65 6e 74 20 63 75 72 73 6f 72 73 2e 0a 2a  quent cursors..*
1997e 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1997f 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 70 65   void sqlite3Ope
19980 6e 54 61 62 6c 65 41 6e 64 49 6e 64 69 63 65 73  nTableAndIndices
19981 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
19982 65 2c 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20  e,   /* Parsing 
19983 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62  context */.  Tab
19984 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 2f 2a  le *pTab,     /*
19985 20 54 61 62 6c 65 20 74 6f 20 62 65 20 6f 70 65   Table to be ope
19986 6e 65 64 20 2a 2f 0a 20 20 69 6e 74 20 62 61 73  ned */.  int bas
19987 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72  e,        /* Cur
19988 73 6f 72 20 6e 75 6d 62 65 72 20 61 73 73 69 67  sor number assig
19989 6e 65 64 20 74 6f 20 74 68 65 20 74 61 62 6c 65  ned to the table
1998a 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 20 20 20 20   */.  int op    
1998b 20 20 20 20 20 20 20 2f 2a 20 4f 50 5f 4f 70 65         /* OP_Ope
1998c 6e 52 65 61 64 20 6f 72 20 4f 50 5f 4f 70 65 6e  nRead or OP_Open
1998d 57 72 69 74 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e  Write */.){.  in
1998e 74 20 69 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a  t i;.  int iDb;.
1998f 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20    Index *pIdx;. 
19990 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20 69 66 28   Vdbe *v;..  if(
19991 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
19992 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 44 62   ) return;.  iDb
19993 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
19994 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e  ToIndex(pParse->
19995 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
19996 61 29 3b 0a 20 20 76 20 3d 20 73 71 6c 69 74 65  a);.  v = sqlite
19997 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
19998 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d 30  ;.  assert( v!=0
19999 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 4f 70 65   );.  sqlite3Ope
1999a 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 62  nTable(pParse, b
1999b 61 73 65 2c 20 69 44 62 2c 20 70 54 61 62 2c 20  ase, iDb, pTab, 
1999c 6f 70 29 3b 0a 20 20 66 6f 72 28 69 3d 31 2c 20  op);.  for(i=1, 
1999d 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65  pIdx=pTab->pInde
1999e 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49  x; pIdx; pIdx=pI
1999f 64 78 2d 3e 70 4e 65 78 74 2c 20 69 2b 2b 29 7b  dx->pNext, i++){
199a0 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b  .    KeyInfo *pK
199a1 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65  ey = sqlite3Inde
199a2 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c  xKeyinfo(pParse,
199a3 20 70 49 64 78 29 3b 0a 20 20 20 20 61 73 73 65   pIdx);.    asse
199a4 72 74 28 20 70 49 64 78 2d 3e 70 53 63 68 65 6d  rt( pIdx->pSchem
199a5 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  a==pTab->pSchema
199a6 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   );.    sqlite3V
199a7 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49  dbeAddOp(v, OP_I
199a8 6e 74 65 67 65 72 2c 20 69 44 62 2c 20 30 29 3b  nteger, iDb, 0);
199a9 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74  .    VdbeComment
199aa 28 28 76 2c 20 22 23 20 25 73 22 2c 20 70 49 64  ((v, "# %s", pId
199ab 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20  x->zName));.    
199ac 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28 76  sqlite3VdbeOp3(v
199ad 2c 20 6f 70 2c 20 69 2b 62 61 73 65 2c 20 70 49  , op, i+base, pI
199ae 64 78 2d 3e 74 6e 75 6d 2c 20 28 63 68 61 72 2a  dx->tnum, (char*
199af 29 70 4b 65 79 2c 20 50 33 5f 4b 45 59 49 4e 46  )pKey, P3_KEYINF
199b0 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 7d 0a  O_HANDOFF);.  }.
199b1 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 54    if( pParse->nT
199b2 61 62 3c 3d 62 61 73 65 2b 69 20 29 7b 0a 20 20  ab<=base+i ){.  
199b3 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 20 3d    pParse->nTab =
199b4 20 62 61 73 65 2b 69 3b 0a 20 20 7d 0a 7d 0a 0a   base+i;.  }.}..
199b5 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
199b6 45 53 54 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  EST./*.** The fo
199b7 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76  llowing global v
199b8 61 72 69 61 62 6c 65 20 69 73 20 69 6e 63 72 65  ariable is incre
199b9 6d 65 6e 74 65 64 20 77 68 65 6e 65 76 65 72 20  mented whenever 
199ba 74 68 65 0a 2a 2a 20 74 72 61 6e 73 66 65 72 20  the.** transfer 
199bb 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20  optimization is 
199bc 75 73 65 64 2e 20 20 54 68 69 73 20 69 73 20 75  used.  This is u
199bd 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 0a  sed for testing.
199be 2a 2a 20 70 75 72 70 6f 73 65 73 20 6f 6e 6c 79  ** purposes only
199bf 20 2d 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20   - to make sure 
199c0 74 68 65 20 74 72 61 6e 73 66 65 72 20 6f 70 74  the transfer opt
199c1 69 6d 69 7a 61 74 69 6f 6e 20 72 65 61 6c 6c 79  imization really
199c2 0a 2a 2a 20 69 73 20 68 61 70 70 65 6e 69 6e 67  .** is happening
199c3 20 77 68 65 6e 20 69 74 20 69 73 20 73 75 70 70   when it is supp
199c4 6f 73 65 20 74 6f 2e 0a 2a 2f 0a 69 6e 74 20 73  ose to..*/.int s
199c5 71 6c 69 74 65 33 5f 78 66 65 72 6f 70 74 5f 63  qlite3_xferopt_c
199c6 6f 75 6e 74 3b 0a 23 65 6e 64 69 66 20 2f 2a 20  ount;.#endif /* 
199c7 53 51 4c 49 54 45 5f 54 45 53 54 20 2a 2f 0a 0a  SQLITE_TEST */..
199c8 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
199c9 4f 4d 49 54 5f 58 46 45 52 5f 4f 50 54 0a 2f 2a  OMIT_XFER_OPT./*
199ca 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 63 6f 6c  .** Check to col
199cb 6c 61 74 69 6f 6e 20 6e 61 6d 65 73 20 74 6f 20  lation names to 
199cc 73 65 65 20 69 66 20 74 68 65 79 20 61 72 65 20  see if they are 
199cd 63 6f 6d 70 61 74 69 62 6c 65 2e 0a 2a 2f 0a 73  compatible..*/.s
199ce 74 61 74 69 63 20 69 6e 74 20 78 66 65 72 43 6f  tatic int xferCo
199cf 6d 70 61 74 69 62 6c 65 43 6f 6c 6c 61 74 69 6f  mpatibleCollatio
199d0 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 31  n(const char *z1
199d1 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32  , const char *z2
199d2 29 7b 0a 20 20 69 66 28 20 7a 31 3d 3d 30 20 29  ){.  if( z1==0 )
199d3 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 7a 32 3d  {.    return z2=
199d4 3d 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 32  =0;.  }.  if( z2
199d5 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
199d6 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  n 0;.  }.  retur
199d7 6e 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  n sqlite3StrICmp
199d8 28 7a 31 2c 20 7a 32 29 3d 3d 30 3b 0a 7d 0a 0a  (z1, z2)==0;.}..
199d9 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20  ./*.** Check to 
199da 73 65 65 20 69 66 20 69 6e 64 65 78 20 70 53 72  see if index pSr
199db 63 20 69 73 20 63 6f 6d 70 61 74 69 62 6c 65 20  c is compatible 
199dc 61 73 20 61 20 73 6f 75 72 63 65 20 6f 66 20 64  as a source of d
199dd 61 74 61 0a 2a 2a 20 66 6f 72 20 69 6e 64 65 78  ata.** for index
199de 20 70 44 65 73 74 20 69 6e 20 61 6e 20 69 6e 73   pDest in an ins
199df 65 72 74 20 74 72 61 6e 73 66 65 72 20 6f 70 74  ert transfer opt
199e0 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 54 68 65 20  imization.  The 
199e1 72 75 6c 65 73 0a 2a 2a 20 66 6f 72 20 61 20 63  rules.** for a c
199e2 6f 6d 70 61 74 69 62 6c 65 20 69 6e 64 65 78 3a  ompatible index:
199e3 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 20 54 68  .**.**    *   Th
199e4 65 20 69 6e 64 65 78 20 69 73 20 6f 76 65 72 20  e index is over 
199e5 74 68 65 20 73 61 6d 65 20 73 65 74 20 6f 66 20  the same set of 
199e6 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 20 20 20 2a 20  columns.**    * 
199e7 20 20 54 68 65 20 73 61 6d 65 20 44 45 53 43 20    The same DESC 
199e8 61 6e 64 20 41 53 43 20 6d 61 72 6b 69 6e 67 73  and ASC markings
199e9 20 6f 63 63 75 72 73 20 6f 6e 20 61 6c 6c 20 63   occurs on all c
199ea 6f 6c 75 6d 6e 73 0a 2a 2a 20 20 20 20 2a 20 20  olumns.**    *  
199eb 20 54 68 65 20 73 61 6d 65 20 6f 6e 45 72 72 6f   The same onErro
199ec 72 20 70 72 6f 63 65 73 73 69 6e 67 20 28 4f 45  r processing (OE
199ed 5f 41 62 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f 72  _Abort, OE_Ignor
199ee 65 2c 20 65 74 63 29 0a 2a 2a 20 20 20 20 2a 20  e, etc).**    * 
199ef 20 20 54 68 65 20 73 61 6d 65 20 63 6f 6c 6c 61    The same colla
199f0 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6f 6e  ting sequence on
199f1 20 65 61 63 68 20 63 6f 6c 75 6d 6e 0a 2a 2f 0a   each column.*/.
199f2 73 74 61 74 69 63 20 69 6e 74 20 78 66 65 72 43  static int xferC
199f3 6f 6d 70 61 74 69 62 6c 65 49 6e 64 65 78 28 49  ompatibleIndex(I
199f4 6e 64 65 78 20 2a 70 44 65 73 74 2c 20 49 6e 64  ndex *pDest, Ind
199f5 65 78 20 2a 70 53 72 63 29 7b 0a 20 20 69 6e 74  ex *pSrc){.  int
199f6 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44   i;.  assert( pD
199f7 65 73 74 20 26 26 20 70 53 72 63 20 29 3b 0a 20  est && pSrc );. 
199f8 20 61 73 73 65 72 74 28 20 70 44 65 73 74 2d 3e   assert( pDest->
199f9 70 54 61 62 6c 65 21 3d 70 53 72 63 2d 3e 70 54  pTable!=pSrc->pT
199fa 61 62 6c 65 20 29 3b 0a 20 20 69 66 28 20 70 44  able );.  if( pD
199fb 65 73 74 2d 3e 6e 43 6f 6c 75 6d 6e 21 3d 70 53  est->nColumn!=pS
199fc 72 63 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20  rc->nColumn ){. 
199fd 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
199fe 2a 20 44 69 66 66 65 72 65 6e 74 20 6e 75 6d 62  * Different numb
199ff 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 2a 2f  er of columns */
19a00 0a 20 20 7d 0a 20 20 69 66 28 20 70 44 65 73 74  .  }.  if( pDest
19a01 2d 3e 6f 6e 45 72 72 6f 72 21 3d 70 53 72 63 2d  ->onError!=pSrc-
19a02 3e 6f 6e 45 72 72 6f 72 20 29 7b 0a 20 20 20 20  >onError ){.    
19a03 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 44  return 0;   /* D
19a04 69 66 66 65 72 65 6e 74 20 63 6f 6e 66 6c 69 63  ifferent conflic
19a05 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 73 74 72  t resolution str
19a06 61 74 65 67 69 65 73 20 2a 2f 0a 20 20 7d 0a 20  ategies */.  }. 
19a07 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 53 72 63   for(i=0; i<pSrc
19a08 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b  ->nColumn; i++){
19a09 0a 20 20 20 20 69 66 28 20 70 53 72 63 2d 3e 61  .    if( pSrc->a
19a0a 69 43 6f 6c 75 6d 6e 5b 69 5d 21 3d 70 44 65 73  iColumn[i]!=pDes
19a0b 74 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 20 29  t->aiColumn[i] )
19a0c 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  {.      return 0
19a0d 3b 20 20 20 2f 2a 20 44 69 66 66 65 72 65 6e 74  ;   /* Different
19a0e 20 63 6f 6c 75 6d 6e 73 20 69 6e 64 65 78 65 64   columns indexed
19a0f 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   */.    }.    if
19a10 28 20 70 53 72 63 2d 3e 61 53 6f 72 74 4f 72 64  ( pSrc->aSortOrd
19a11 65 72 5b 69 5d 21 3d 70 44 65 73 74 2d 3e 61 53  er[i]!=pDest->aS
19a12 6f 72 74 4f 72 64 65 72 5b 69 5d 20 29 7b 0a 20  ortOrder[i] ){. 
19a13 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20       return 0;  
19a14 20 2f 2a 20 44 69 66 66 65 72 65 6e 74 20 73 6f   /* Different so
19a15 72 74 20 6f 72 64 65 72 73 20 2a 2f 0a 20 20 20  rt orders */.   
19a16 20 7d 0a 20 20 20 20 69 66 28 20 70 53 72 63 2d   }.    if( pSrc-
19a17 3e 61 7a 43 6f 6c 6c 5b 69 5d 21 3d 70 44 65 73  >azColl[i]!=pDes
19a18 74 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 20 29 7b 0a  t->azColl[i] ){.
19a19 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20        return 0; 
19a1a 20 20 2f 2a 20 44 69 66 66 65 72 65 6e 74 20 73    /* Different s
19a1b 6f 72 74 20 6f 72 64 65 72 73 20 2a 2f 0a 20 20  ort orders */.  
19a1c 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66    }.  }..  /* If
19a1d 20 6e 6f 20 74 65 73 74 20 61 62 6f 76 65 20 66   no test above f
19a1e 61 69 6c 73 20 74 68 65 6e 20 74 68 65 20 69 6e  ails then the in
19a1f 64 69 63 65 73 20 6d 75 73 74 20 62 65 20 63 6f  dices must be co
19a20 6d 70 61 74 69 62 6c 65 20 2a 2f 0a 20 20 72 65  mpatible */.  re
19a21 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 1;.}../*.**
19a22 20 41 74 74 65 6d 70 74 20 74 68 65 20 74 72 61   Attempt the tra
19a23 6e 73 66 65 72 20 6f 70 74 69 6d 69 7a 61 74 69  nsfer optimizati
19a24 6f 6e 20 6f 6e 20 49 4e 53 45 52 54 73 20 6f 66  on on INSERTs of
19a25 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20   the form.**.** 
19a26 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
19a27 74 61 62 31 20 53 45 4c 45 43 54 20 2a 20 46 52  tab1 SELECT * FR
19a28 4f 4d 20 74 61 62 32 3b 0a 2a 2a 0a 2a 2a 20 54  OM tab2;.**.** T
19a29 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  his optimization
19a2a 20 69 73 20 6f 6e 6c 79 20 61 74 74 65 6d 70 74   is only attempt
19a2b 65 64 20 69 66 0a 2a 2a 0a 2a 2a 20 20 20 20 28  ed if.**.**    (
19a2c 31 29 20 20 74 61 62 31 20 61 6e 64 20 74 61 62  1)  tab1 and tab
19a2d 32 20 68 61 76 65 20 69 64 65 6e 74 69 63 61 6c  2 have identical
19a2e 20 73 63 68 65 6d 61 73 20 69 6e 63 6c 75 64 69   schemas includi
19a2f 6e 67 20 61 6c 6c 20 74 68 65 0a 2a 2a 20 20 20  ng all the.**   
19a30 20 20 20 20 20 20 73 61 6d 65 20 69 6e 64 69 63        same indic
19a31 65 73 20 61 6e 64 20 63 6f 6e 73 74 72 61 69 6e  es and constrain
19a32 74 73 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20  ts.**.**    (2) 
19a33 20 74 61 62 31 20 61 6e 64 20 74 61 62 32 20 61   tab1 and tab2 a
19a34 72 65 20 64 69 66 66 65 72 65 6e 74 20 74 61 62  re different tab
19a35 6c 65 73 0a 2a 2a 0a 2a 2a 20 20 20 20 28 33 29  les.**.**    (3)
19a36 20 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 20    There must be 
19a37 6e 6f 20 74 72 69 67 67 65 72 73 20 6f 6e 20 74  no triggers on t
19a38 61 62 31 0a 2a 2a 0a 2a 2a 20 20 20 20 28 34 29  ab1.**.**    (4)
19a39 20 20 54 68 65 20 72 65 73 75 6c 74 20 73 65 74    The result set
19a3a 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 73   of the SELECT s
19a3b 74 61 74 65 6d 65 6e 74 20 69 73 20 22 2a 22 0a  tatement is "*".
19a3c 2a 2a 0a 2a 2a 20 20 20 20 28 35 29 20 20 54 68  **.**    (5)  Th
19a3d 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
19a3e 6e 74 20 68 61 73 20 6e 6f 20 57 48 45 52 45 2c  nt has no WHERE,
19a3f 20 48 41 56 49 4e 47 2c 20 4f 52 44 45 52 20 42   HAVING, ORDER B
19a40 59 2c 20 47 52 4f 55 50 20 42 59 2c 0a 2a 2a 20  Y, GROUP BY,.** 
19a41 20 20 20 20 20 20 20 20 6f 72 20 4c 49 4d 49 54          or LIMIT
19a42 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20   clause..**.**  
19a43 20 20 28 36 29 20 20 54 68 65 20 53 45 4c 45 43    (6)  The SELEC
19a44 54 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61  T statement is a
19a45 20 73 69 6d 70 6c 65 20 28 6e 6f 74 20 61 20 63   simple (not a c
19a46 6f 6d 70 6f 75 6e 64 29 20 73 65 6c 65 63 74 20  ompound) select 
19a47 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  that.**         
19a48 63 6f 6e 74 61 69 6e 73 20 6f 6e 6c 79 20 74 61  contains only ta
19a49 62 32 20 69 6e 20 69 74 73 20 46 52 4f 4d 20 63  b2 in its FROM c
19a4a 6c 61 75 73 65 0a 2a 2a 0a 2a 2a 20 54 68 69 73  lause.**.** This
19a4b 20 6d 65 74 68 6f 64 20 66 6f 72 20 69 6d 70 6c   method for impl
19a4c 65 6d 65 6e 74 69 6e 67 20 74 68 65 20 49 4e 53  ementing the INS
19a4d 45 52 54 20 74 72 61 6e 73 66 65 72 73 20 72 61  ERT transfers ra
19a4e 77 20 72 65 63 6f 72 64 73 20 66 72 6f 6d 0a 2a  w records from.*
19a4f 2a 20 74 61 62 32 20 6f 76 65 72 20 74 6f 20 74  * tab2 over to t
19a50 61 62 31 2e 20 20 54 68 65 20 63 6f 6c 75 6d 6e  ab1.  The column
19a51 73 20 61 72 65 20 6e 6f 74 20 64 65 63 6f 64 65  s are not decode
19a52 64 2e 20 20 52 61 77 20 72 65 63 6f 72 64 73 20  d.  Raw records 
19a53 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 69 6e 64 69  from.** the indi
19a54 63 65 73 20 6f 66 20 74 61 62 32 20 61 72 65 20  ces of tab2 are 
19a55 74 72 61 6e 73 66 65 72 65 64 20 74 6f 20 74 61  transfered to ta
19a56 62 31 20 61 73 20 77 65 6c 6c 2e 20 20 49 6e 20  b1 as well.  In 
19a57 73 6f 20 64 6f 69 6e 67 2c 0a 2a 2a 20 74 68 65  so doing,.** the
19a58 20 72 65 73 75 6c 74 69 6e 67 20 74 61 62 31 20   resulting tab1 
19a59 68 61 73 20 6d 75 63 68 20 6c 65 73 73 20 66 72  has much less fr
19a5a 61 67 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a  agmentation..**.
19a5b 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
19a5c 72 65 74 75 72 6e 73 20 54 52 55 45 20 69 66 20  returns TRUE if 
19a5d 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  the optimization
19a5e 20 69 73 20 61 74 74 65 6d 70 74 65 64 2e 20 20   is attempted.  
19a5f 49 66 20 61 6e 79 0a 2a 2a 20 6f 66 20 74 68 65  If any.** of the
19a60 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 62 6f 76   conditions abov
19a61 65 20 66 61 69 6c 20 73 6f 20 74 68 61 74 20 74  e fail so that t
19a62 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20  he optimization 
19a63 73 68 6f 75 6c 64 20 6e 6f 74 0a 2a 2a 20 62 65  should not.** be
19a64 20 61 74 74 65 6d 70 74 65 64 2c 20 74 68 65 6e   attempted, then
19a65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   this routine re
19a66 74 75 72 6e 73 20 46 41 4c 53 45 2e 0a 2a 2f 0a  turns FALSE..*/.
19a67 73 74 61 74 69 63 20 69 6e 74 20 78 66 65 72 4f  static int xferO
19a68 70 74 69 6d 69 7a 61 74 69 6f 6e 28 0a 20 20 50  ptimization(.  P
19a69 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
19a6a 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63       /* Parser c
19a6b 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c  ontext */.  Tabl
19a6c 65 20 2a 70 44 65 73 74 2c 20 20 20 20 20 20 20  e *pDest,       
19a6d 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 77    /* The table w
19a6e 65 20 61 72 65 20 69 6e 73 65 72 74 69 6e 67 20  e are inserting 
19a6f 69 6e 74 6f 20 2a 2f 0a 20 20 53 65 6c 65 63 74  into */.  Select
19a70 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 20 20 20   *pSelect,      
19a71 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74 61 74  /* A SELECT stat
19a72 65 6d 65 6e 74 20 74 6f 20 75 73 65 20 61 73 20  ement to use as 
19a73 74 68 65 20 64 61 74 61 20 73 6f 75 72 63 65 20  the data source 
19a74 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72  */.  int onError
19a75 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f  ,          /* Ho
19a76 77 20 74 6f 20 68 61 6e 64 6c 65 20 63 6f 6e 73  w to handle cons
19a77 74 72 61 69 6e 74 20 65 72 72 6f 72 73 20 2a 2f  traint errors */
19a78 0a 20 20 69 6e 74 20 69 44 62 44 65 73 74 20 20  .  int iDbDest  
19a79 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
19a7a 64 61 74 61 62 61 73 65 20 6f 66 20 70 44 65 73  database of pDes
19a7b 74 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 4c 69  t */.){.  ExprLi
19a7c 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 20 20  st *pEList;     
19a7d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
19a7e 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20  e result set of 
19a7f 74 68 65 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20  the SELECT */.  
19a80 54 61 62 6c 65 20 2a 70 53 72 63 3b 20 20 20 20  Table *pSrc;    
19a81 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19a82 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 69 6e   /* The table in
19a83 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
19a84 20 6f 66 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20   of SELECT */.  
19a85 49 6e 64 65 78 20 2a 70 53 72 63 49 64 78 2c 20  Index *pSrcIdx, 
19a86 2a 70 44 65 73 74 49 64 78 3b 20 20 20 20 20 20  *pDestIdx;      
19a87 20 2f 2a 20 53 6f 75 72 63 65 20 61 6e 64 20 64   /* Source and d
19a88 65 73 74 69 6e 61 74 69 6f 6e 20 69 6e 64 69 63  estination indic
19a89 65 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53  es */.  struct S
19a8a 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  rcList_item *pIt
19a8b 65 6d 3b 20 20 20 20 20 20 2f 2a 20 41 6e 20 65  em;      /* An e
19a8c 6c 65 6d 65 6e 74 20 6f 66 20 70 53 65 6c 65 63  lement of pSelec
19a8d 74 2d 3e 70 53 72 63 20 2a 2f 0a 20 20 69 6e 74  t->pSrc */.  int
19a8e 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
19a8f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
19a90 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
19a91 0a 20 20 69 6e 74 20 69 44 62 53 72 63 3b 20 20  .  int iDbSrc;  
19a92 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19a93 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
19a94 61 73 65 20 6f 66 20 70 53 72 63 20 2a 2f 0a 20  ase of pSrc */. 
19a95 20 69 6e 74 20 69 53 72 63 2c 20 69 44 65 73 74   int iSrc, iDest
19a96 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
19a97 20 20 2f 2a 20 43 75 72 73 6f 72 73 20 66 72 6f    /* Cursors fro
19a98 6d 20 73 6f 75 72 63 65 20 61 6e 64 20 64 65 73  m source and des
19a99 74 69 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  tination */.  in
19a9a 74 20 61 64 64 72 31 2c 20 61 64 64 72 32 3b 20  t addr1, addr2; 
19a9b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
19a9c 2a 20 4c 6f 6f 70 20 61 64 64 72 65 73 73 65 73  * Loop addresses
19a9d 20 2a 2f 0a 20 20 69 6e 74 20 65 6d 70 74 79 44   */.  int emptyD
19a9e 65 73 74 54 65 73 74 3b 20 20 20 20 20 20 20 20  estTest;        
19a9f 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
19aa0 73 20 6f 66 20 74 65 73 74 20 66 6f 72 20 65 6d  s of test for em
19aa1 70 74 79 20 70 44 65 73 74 20 2a 2f 0a 20 20 69  pty pDest */.  i
19aa2 6e 74 20 65 6d 70 74 79 53 72 63 54 65 73 74 3b  nt emptySrcTest;
19aa3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19aa4 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74 65  /* Address of te
19aa5 73 74 20 66 6f 72 20 65 6d 70 74 79 20 70 53 72  st for empty pSr
19aa6 63 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20  c */.  Vdbe *v; 
19aa7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19aa8 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56          /* The V
19aa9 44 42 45 20 77 65 20 61 72 65 20 62 75 69 6c 64  DBE we are build
19aaa 69 6e 67 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f  ing */.  KeyInfo
19aab 20 2a 70 4b 65 79 3b 20 20 20 20 20 20 20 20 20   *pKey;         
19aac 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4b 65 79            /* Key
19aad 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72   information for
19aae 20 61 6e 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69   an index */.  i
19aaf 6e 74 20 63 6f 75 6e 74 65 72 4d 65 6d 3b 20 20  nt counterMem;  
19ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19ab1 2f 2a 20 4d 65 6d 6f 72 79 20 72 65 67 69 73 74  /* Memory regist
19ab2 65 72 20 75 73 65 64 20 62 79 20 41 55 54 4f 49  er used by AUTOI
19ab3 4e 43 20 2a 2f 0a 20 20 69 6e 74 20 64 65 73 74  NC */.  int dest
19ab4 48 61 73 55 6e 69 71 75 65 49 64 78 20 3d 20 30  HasUniqueIdx = 0
19ab5 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65  ;        /* True
19ab6 20 69 66 20 70 44 65 73 74 20 68 61 73 20 61 20   if pDest has a 
19ab7 55 4e 49 51 55 45 20 69 6e 64 65 78 20 2a 2f 0a  UNIQUE index */.
19ab8 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 3d 3d  .  if( pSelect==
19ab9 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
19aba 30 3b 20 20 20 2f 2a 20 4d 75 73 74 20 62 65 20  0;   /* Must be 
19abb 6f 66 20 74 68 65 20 66 6f 72 6d 20 20 49 4e 53  of the form  INS
19abc 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 53 45 4c  ERT INTO ... SEL
19abd 45 43 54 20 2e 2e 2e 20 2a 2f 0a 20 20 7d 0a 20  ECT ... */.  }. 
19abe 20 69 66 28 20 70 44 65 73 74 2d 3e 70 54 72 69   if( pDest->pTri
19abf 67 67 65 72 20 29 7b 0a 20 20 20 20 72 65 74 75  gger ){.    retu
19ac0 72 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62 31 20  rn 0;   /* tab1 
19ac1 6d 75 73 74 20 6e 6f 74 20 68 61 76 65 20 74 72  must not have tr
19ac2 69 67 67 65 72 73 20 2a 2f 0a 20 20 7d 0a 23 69  iggers */.  }.#i
19ac3 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
19ac4 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
19ac5 20 69 66 28 20 70 44 65 73 74 2d 3e 69 73 56 69   if( pDest->isVi
19ac6 72 74 75 61 6c 20 29 7b 0a 20 20 20 20 72 65 74  rtual ){.    ret
19ac7 75 72 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62 31  urn 0;   /* tab1
19ac8 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 61 20 76   must not be a v
19ac9 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a  irtual table */.
19aca 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28    }.#endif.  if(
19acb 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66   onError==OE_Def
19acc 61 75 6c 74 20 29 7b 0a 20 20 20 20 6f 6e 45 72  ault ){.    onEr
19acd 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a  ror = OE_Abort;.
19ace 20 20 7d 0a 20 20 69 66 28 20 6f 6e 45 72 72 6f    }.  if( onErro
19acf 72 21 3d 4f 45 5f 41 62 6f 72 74 20 26 26 20 6f  r!=OE_Abort && o
19ad0 6e 45 72 72 6f 72 21 3d 4f 45 5f 52 6f 6c 6c 62  nError!=OE_Rollb
19ad1 61 63 6b 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ack ){.    retur
19ad2 6e 20 30 3b 20 20 20 2f 2a 20 43 61 6e 6e 6f 74  n 0;   /* Cannot
19ad3 20 64 6f 20 4f 52 20 52 45 50 4c 41 43 45 20 6f   do OR REPLACE o
19ad4 72 20 4f 52 20 49 47 4e 4f 52 45 20 6f 72 20 4f  r OR IGNORE or O
19ad5 52 20 46 41 49 4c 20 2a 2f 0a 20 20 7d 0a 20 20  R FAIL */.  }.  
19ad6 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 70 53 72  if( pSelect->pSr
19ad7 63 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  c==0 ){.    retu
19ad8 72 6e 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43  rn 0;   /* SELEC
19ad9 54 20 6d 75 73 74 20 68 61 76 65 20 61 20 46 52  T must have a FR
19ada 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 7d  OM clause */.  }
19adb 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e  .  if( pSelect->
19adc 70 53 72 63 2d 3e 6e 53 72 63 21 3d 31 20 29 7b  pSrc->nSrc!=1 ){
19add 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20  .    return 0;  
19ade 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20   /* FROM clause 
19adf 6d 75 73 74 20 68 61 76 65 20 65 78 61 63 74 6c  must have exactl
19ae0 79 20 6f 6e 65 20 74 65 72 6d 20 2a 2f 0a 20 20  y one term */.  
19ae1 7d 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d  }.  if( pSelect-
19ae2 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 53 65 6c  >pSrc->a[0].pSel
19ae3 65 63 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ect ){.    retur
19ae4 6e 20 30 3b 20 20 20 2f 2a 20 46 52 4f 4d 20 63  n 0;   /* FROM c
19ae5 6c 61 75 73 65 20 63 61 6e 6e 6f 74 20 63 6f 6e  lause cannot con
19ae6 74 61 69 6e 20 61 20 73 75 62 71 75 65 72 79 20  tain a subquery 
19ae7 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 65  */.  }.  if( pSe
19ae8 6c 65 63 74 2d 3e 70 57 68 65 72 65 20 29 7b 0a  lect->pWhere ){.
19ae9 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
19aea 2f 2a 20 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f  /* SELECT may no
19aeb 74 20 68 61 76 65 20 61 20 57 48 45 52 45 20 63  t have a WHERE c
19aec 6c 61 75 73 65 20 2a 2f 0a 20 20 7d 0a 20 20 69  lause */.  }.  i
19aed 66 28 20 70 53 65 6c 65 63 74 2d 3e 70 4f 72 64  f( pSelect->pOrd
19aee 65 72 42 79 20 29 7b 0a 20 20 20 20 72 65 74 75  erBy ){.    retu
19aef 72 6e 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43  rn 0;   /* SELEC
19af0 54 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 61  T may not have a
19af1 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  n ORDER BY claus
19af2 65 20 2a 2f 0a 20 20 7d 0a 20 20 2f 2a 20 44 6f  e */.  }.  /* Do
19af3 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 74 65 73   not need to tes
19af4 74 20 66 6f 72 20 61 20 48 41 56 49 4e 47 20 63  t for a HAVING c
19af5 6c 61 75 73 65 2e 20 20 49 66 20 48 41 56 49 4e  lause.  If HAVIN
19af6 47 20 69 73 20 70 72 65 73 65 6e 74 20 62 75 74  G is present but
19af7 0a 20 20 2a 2a 20 74 68 65 72 65 20 69 73 20 6e  .  ** there is n
19af8 6f 20 4f 52 44 45 52 20 42 59 2c 20 77 65 20 77  o ORDER BY, we w
19af9 69 6c 6c 20 67 65 74 20 61 6e 20 65 72 72 6f 72  ill get an error
19afa 2e 20 2a 2f 0a 20 20 69 66 28 20 70 53 65 6c 65  . */.  if( pSele
19afb 63 74 2d 3e 70 47 72 6f 75 70 42 79 20 29 7b 0a  ct->pGroupBy ){.
19afc 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
19afd 2f 2a 20 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f  /* SELECT may no
19afe 74 20 68 61 76 65 20 61 20 47 52 4f 55 50 20 42  t have a GROUP B
19aff 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 7d 0a  Y clause */.  }.
19b00 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 70    if( pSelect->p
19b01 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 72 65 74  Limit ){.    ret
19b02 75 72 6e 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45  urn 0;   /* SELE
19b03 43 54 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20  CT may not have 
19b04 61 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 2a  a LIMIT clause *
19b05 2f 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  /.  }.  assert( 
19b06 70 53 65 6c 65 63 74 2d 3e 70 4f 66 66 73 65 74  pSelect->pOffset
19b07 3d 3d 30 20 29 3b 20 20 2f 2a 20 4d 75 73 74 20  ==0 );  /* Must 
19b08 62 65 20 73 6f 20 69 66 20 70 4c 69 6d 69 74 3d  be so if pLimit=
19b09 3d 30 20 2a 2f 0a 20 20 69 66 28 20 70 53 65 6c  =0 */.  if( pSel
19b0a 65 63 74 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20  ect->pPrior ){. 
19b0b 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
19b0c 2a 20 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74  * SELECT may not
19b0d 20 62 65 20 61 20 63 6f 6d 70 6f 75 6e 64 20 71   be a compound q
19b0e 75 65 72 79 20 2a 2f 0a 20 20 7d 0a 20 20 69 66  uery */.  }.  if
19b0f 28 20 70 53 65 6c 65 63 74 2d 3e 69 73 44 69 73  ( pSelect->isDis
19b10 74 69 6e 63 74 20 29 7b 0a 20 20 20 20 72 65 74  tinct ){.    ret
19b11 75 72 6e 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45  urn 0;   /* SELE
19b12 43 54 20 6d 61 79 20 6e 6f 74 20 62 65 20 44 49  CT may not be DI
19b13 53 54 49 4e 43 54 20 2a 2f 0a 20 20 7d 0a 20 20  STINCT */.  }.  
19b14 70 45 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 74  pEList = pSelect
19b15 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 61 73 73 65  ->pEList;.  asse
19b16 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b  rt( pEList!=0 );
19b17 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e 6e  .  if( pEList->n
19b18 45 78 70 72 21 3d 31 20 29 7b 0a 20 20 20 20 72  Expr!=1 ){.    r
19b19 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 54 68  eturn 0;   /* Th
19b1a 65 20 72 65 73 75 6c 74 20 73 65 74 20 6d 75 73  e result set mus
19b1b 74 20 68 61 76 65 20 65 78 61 63 74 6c 79 20 6f  t have exactly o
19b1c 6e 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 7d  ne column */.  }
19b1d 0a 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 73  .  assert( pELis
19b1e 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 20 29 3b  t->a[0].pExpr );
19b1f 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e 61  .  if( pEList->a
19b20 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f 70 21 3d 54  [0].pExpr->op!=T
19b21 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 72 65 74  K_ALL ){.    ret
19b22 75 72 6e 20 30 3b 20 20 20 2f 2a 20 54 68 65 20  urn 0;   /* The 
19b23 72 65 73 75 6c 74 20 73 65 74 20 6d 75 73 74 20  result set must 
19b24 62 65 20 74 68 65 20 73 70 65 63 69 61 6c 20 6f  be the special o
19b25 70 65 72 61 74 6f 72 20 22 2a 22 20 2a 2f 0a 20  perator "*" */. 
19b26 20 7d 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73   }..  /* At this
19b27 20 70 6f 69 6e 74 20 77 65 20 68 61 76 65 20 65   point we have e
19b28 73 74 61 62 6c 69 73 68 65 64 20 74 68 61 74 20  stablished that 
19b29 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73  the statement is
19b2a 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 63 6f 72   of the.  ** cor
19b2b 72 65 63 74 20 73 79 6e 74 61 63 74 69 63 20 66  rect syntactic f
19b2c 6f 72 6d 20 74 6f 20 70 61 72 74 69 63 69 70 61  orm to participa
19b2d 74 65 20 69 6e 20 74 68 69 73 20 6f 70 74 69 6d  te in this optim
19b2e 69 7a 61 74 69 6f 6e 2e 20 20 4e 6f 77 0a 20 20  ization.  Now.  
19b2f 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 63 68  ** we have to ch
19b30 65 63 6b 20 74 68 65 20 73 65 6d 61 6e 74 69 63  eck the semantic
19b31 73 2e 0a 20 20 2a 2f 0a 20 20 70 49 74 65 6d 20  s..  */.  pItem 
19b32 3d 20 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 2d  = pSelect->pSrc-
19b33 3e 61 3b 0a 20 20 70 53 72 63 20 3d 20 73 71 6c  >a;.  pSrc = sql
19b34 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28  ite3LocateTable(
19b35 70 50 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e 7a  pParse, pItem->z
19b36 4e 61 6d 65 2c 20 70 49 74 65 6d 2d 3e 7a 44 61  Name, pItem->zDa
19b37 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20 70  tabase);.  if( p
19b38 53 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  Src==0 ){.    re
19b39 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 46 52 4f  turn 0;   /* FRO
19b3a 4d 20 63 6c 61 75 73 65 20 64 6f 65 73 20 6e 6f  M clause does no
19b3b 74 20 63 6f 6e 74 61 69 6e 20 61 20 72 65 61 6c  t contain a real
19b3c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 7d 0a 20 20   table */.  }.  
19b3d 69 66 28 20 70 53 72 63 3d 3d 70 44 65 73 74 20  if( pSrc==pDest 
19b3e 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
19b3f 20 20 20 2f 2a 20 74 61 62 31 20 61 6e 64 20 74     /* tab1 and t
19b40 61 62 32 20 6d 61 79 20 6e 6f 74 20 62 65 20 74  ab2 may not be t
19b41 68 65 20 73 61 6d 65 20 74 61 62 6c 65 20 2a 2f  he same table */
19b42 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c  .  }.#ifndef SQL
19b43 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
19b44 54 41 42 4c 45 0a 20 20 69 66 28 20 70 53 72 63  TABLE.  if( pSrc
19b45 2d 3e 69 73 56 69 72 74 75 61 6c 20 29 7b 0a 20  ->isVirtual ){. 
19b46 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
19b47 2a 20 74 61 62 32 20 6d 75 73 74 20 6e 6f 74 20  * tab2 must not 
19b48 62 65 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  be a virtual tab
19b49 6c 65 20 2a 2f 0a 20 20 7d 0a 23 65 6e 64 69 66  le */.  }.#endif
19b4a 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 70 53 65  .  if( pSrc->pSe
19b4b 6c 65 63 74 20 29 7b 0a 20 20 20 20 72 65 74 75  lect ){.    retu
19b4c 72 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62 32 20  rn 0;   /* tab2 
19b4d 6d 61 79 20 6e 6f 74 20 62 65 20 61 20 76 69 65  may not be a vie
19b4e 77 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70  w */.  }.  if( p
19b4f 44 65 73 74 2d 3e 6e 43 6f 6c 21 3d 70 53 72 63  Dest->nCol!=pSrc
19b50 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 72 65  ->nCol ){.    re
19b51 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 4e 75 6d  turn 0;   /* Num
19b52 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6d  ber of columns m
19b53 75 73 74 20 62 65 20 74 68 65 20 73 61 6d 65 20  ust be the same 
19b54 69 6e 20 74 61 62 31 20 61 6e 64 20 74 61 62 32  in tab1 and tab2
19b55 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 44   */.  }.  if( pD
19b56 65 73 74 2d 3e 69 50 4b 65 79 21 3d 70 53 72 63  est->iPKey!=pSrc
19b57 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 72  ->iPKey ){.    r
19b58 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 42 6f  eturn 0;   /* Bo
19b59 74 68 20 74 61 62 6c 65 73 20 6d 75 73 74 20 68  th tables must h
19b5a 61 76 65 20 74 68 65 20 73 61 6d 65 20 49 4e 54  ave the same INT
19b5b 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
19b5c 20 2a 2f 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d   */.  }.  for(i=
19b5d 30 3b 20 69 3c 70 44 65 73 74 2d 3e 6e 43 6f 6c  0; i<pDest->nCol
19b5e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
19b5f 70 44 65 73 74 2d 3e 61 43 6f 6c 5b 69 5d 2e 61  pDest->aCol[i].a
19b60 66 66 69 6e 69 74 79 21 3d 70 53 72 63 2d 3e 61  ffinity!=pSrc->a
19b61 43 6f 6c 5b 69 5d 2e 61 66 66 69 6e 69 74 79 20  Col[i].affinity 
19b62 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
19b63 30 3b 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 74  0;    /* Affinit
19b64 79 20 6d 75 73 74 20 62 65 20 74 68 65 20 73 61  y must be the sa
19b65 6d 65 20 6f 6e 20 61 6c 6c 20 63 6f 6c 75 6d 6e  me on all column
19b66 73 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69  s */.    }.    i
19b67 66 28 20 21 78 66 65 72 43 6f 6d 70 61 74 69 62  f( !xferCompatib
19b68 6c 65 43 6f 6c 6c 61 74 69 6f 6e 28 70 44 65 73  leCollation(pDes
19b69 74 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c  t->aCol[i].zColl
19b6a 2c 20 70 53 72 63 2d 3e 61 43 6f 6c 5b 69 5d 2e  , pSrc->aCol[i].
19b6b 7a 43 6f 6c 6c 29 20 29 7b 0a 20 20 20 20 20 20  zColl) ){.      
19b6c 72 65 74 75 72 6e 20 30 3b 20 20 20 20 2f 2a 20  return 0;    /* 
19b6d 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  Collating sequen
19b6e 63 65 20 6d 75 73 74 20 62 65 20 74 68 65 20 73  ce must be the s
19b6f 61 6d 65 20 6f 6e 20 61 6c 6c 20 63 6f 6c 75 6d  ame on all colum
19b70 6e 73 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20  ns */.    }.    
19b71 69 66 28 20 70 44 65 73 74 2d 3e 61 43 6f 6c 5b  if( pDest->aCol[
19b72 69 5d 2e 6e 6f 74 4e 75 6c 6c 20 26 26 20 21 70  i].notNull && !p
19b73 53 72 63 2d 3e 61 43 6f 6c 5b 69 5d 2e 6e 6f 74  Src->aCol[i].not
19b74 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 72 65  Null ){.      re
19b75 74 75 72 6e 20 30 3b 20 20 20 20 2f 2a 20 74 61  turn 0;    /* ta
19b76 62 32 20 6d 75 73 74 20 62 65 20 4e 4f 54 20 4e  b2 must be NOT N
19b77 55 4c 4c 20 69 66 20 74 61 62 31 20 69 73 20 2a  ULL if tab1 is *
19b78 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f  /.    }.  }.  fo
19b79 72 28 70 44 65 73 74 49 64 78 3d 70 44 65 73 74  r(pDestIdx=pDest
19b7a 2d 3e 70 49 6e 64 65 78 3b 20 70 44 65 73 74 49  ->pIndex; pDestI
19b7b 64 78 3b 20 70 44 65 73 74 49 64 78 3d 70 44 65  dx; pDestIdx=pDe
19b7c 73 74 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20  stIdx->pNext){. 
19b7d 20 20 20 69 66 28 20 70 44 65 73 74 49 64 78 2d     if( pDestIdx-
19b7e 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e  >onError!=OE_Non
19b7f 65 20 29 7b 0a 20 20 20 20 20 20 64 65 73 74 48  e ){.      destH
19b80 61 73 55 6e 69 71 75 65 49 64 78 20 3d 20 31 3b  asUniqueIdx = 1;
19b81 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 70  .    }.    for(p
19b82 53 72 63 49 64 78 3d 70 53 72 63 2d 3e 70 49 6e  SrcIdx=pSrc->pIn
19b83 64 65 78 3b 20 70 53 72 63 49 64 78 3b 20 70 53  dex; pSrcIdx; pS
19b84 72 63 49 64 78 3d 70 53 72 63 49 64 78 2d 3e 70  rcIdx=pSrcIdx->p
19b85 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 66 28  Next){.      if(
19b86 20 78 66 65 72 43 6f 6d 70 61 74 69 62 6c 65 49   xferCompatibleI
19b87 6e 64 65 78 28 70 44 65 73 74 49 64 78 2c 20 70  ndex(pDestIdx, p
19b88 53 72 63 49 64 78 29 20 29 20 62 72 65 61 6b 3b  SrcIdx) ) break;
19b89 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
19b8a 53 72 63 49 64 78 3d 3d 30 20 29 7b 0a 20 20 20  SrcIdx==0 ){.   
19b8b 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20     return 0;    
19b8c 2f 2a 20 70 44 65 73 74 49 64 78 20 68 61 73 20  /* pDestIdx has 
19b8d 6e 6f 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  no corresponding
19b8e 20 69 6e 64 65 78 20 69 6e 20 70 53 72 63 20 2a   index in pSrc *
19b8f 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 69 66 6e  /.    }.  }.#ifn
19b90 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
19b91 43 48 45 43 4b 0a 20 20 69 66 28 20 70 44 65 73  CHECK.  if( pDes
19b92 74 2d 3e 70 43 68 65 63 6b 20 26 26 20 21 73 71  t->pCheck && !sq
19b93 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65  lite3ExprCompare
19b94 28 70 53 72 63 2d 3e 70 43 68 65 63 6b 2c 20 70  (pSrc->pCheck, p
19b95 44 65 73 74 2d 3e 70 43 68 65 63 6b 29 20 29 7b  Dest->pCheck) ){
19b96 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20  .    return 0;  
19b97 20 2f 2a 20 54 61 62 6c 65 73 20 68 61 76 65 20   /* Tables have 
19b98 64 69 66 66 65 72 65 6e 74 20 43 48 45 43 4b 20  different CHECK 
19b99 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 20 54 69  constraints.  Ti
19b9a 63 6b 65 74 20 23 32 32 35 32 20 2a 2f 0a 20 20  cket #2252 */.  
19b9b 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49  }.#endif..  /* I
19b9c 66 20 77 65 20 67 65 74 20 74 68 69 73 20 66 61  f we get this fa
19b9d 72 2c 20 69 74 20 6d 65 61 6e 73 20 65 69 74 68  r, it means eith
19b9e 65 72 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  er:.  **.  **   
19b9f 20 2a 20 20 20 57 65 20 63 61 6e 20 61 6c 77 61   *   We can alwa
19ba0 79 73 20 64 6f 20 74 68 65 20 74 72 61 6e 73 66  ys do the transf
19ba1 65 72 20 69 66 20 74 68 65 20 74 61 62 6c 65 20  er if the table 
19ba2 63 6f 6e 74 61 69 6e 73 20 61 6e 0a 20 20 2a 2a  contains an.  **
19ba3 20 20 20 20 20 20 20 20 61 6e 20 69 6e 74 65 67          an integ
19ba4 65 72 20 70 72 69 6d 61 72 79 20 6b 65 79 0a 20  er primary key. 
19ba5 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 2a 20 20 20   **.  **    *   
19ba6 57 65 20 63 61 6e 20 63 6f 6e 64 69 74 69 6f 6e  We can condition
19ba7 61 6c 6c 79 20 64 6f 20 74 68 65 20 74 72 61 6e  ally do the tran
19ba8 73 66 65 72 20 69 66 20 74 68 65 20 64 65 73 74  sfer if the dest
19ba9 69 6e 61 74 69 6f 6e 0a 20 20 2a 2a 20 20 20 20  ination.  **    
19baa 20 20 20 20 74 61 62 6c 65 20 69 73 20 65 6d 70      table is emp
19bab 74 79 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20  ty..  */.#ifdef 
19bac 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 73 71  SQLITE_TEST.  sq
19bad 6c 69 74 65 33 5f 78 66 65 72 6f 70 74 5f 63 6f  lite3_xferopt_co
19bae 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20  unt++;.#endif.  
19baf 69 44 62 53 72 63 20 3d 20 73 71 6c 69 74 65 33  iDbSrc = sqlite3
19bb0 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50  SchemaToIndex(pP
19bb1 61 72 73 65 2d 3e 64 62 2c 20 70 53 72 63 2d 3e  arse->db, pSrc->
19bb2 70 53 63 68 65 6d 61 29 3b 0a 20 20 76 20 3d 20  pSchema);.  v = 
19bb3 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
19bb4 50 61 72 73 65 29 3b 0a 20 20 69 53 72 63 20 3d  Parse);.  iSrc =
19bb5 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b   pParse->nTab++;
19bb6 0a 20 20 69 44 65 73 74 20 3d 20 70 50 61 72 73  .  iDest = pPars
19bb7 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 63 6f 75  e->nTab++;.  cou
19bb8 6e 74 65 72 4d 65 6d 20 3d 20 61 75 74 6f 49 6e  nterMem = autoIn
19bb9 63 42 65 67 69 6e 28 70 50 61 72 73 65 2c 20 69  cBegin(pParse, i
19bba 44 62 44 65 73 74 2c 20 70 44 65 73 74 29 3b 0a  DbDest, pDest);.
19bbb 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62    sqlite3OpenTab
19bbc 6c 65 28 70 50 61 72 73 65 2c 20 69 44 65 73 74  le(pParse, iDest
19bbd 2c 20 69 44 62 44 65 73 74 2c 20 70 44 65 73 74  , iDbDest, pDest
19bbe 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 29 3b  , OP_OpenWrite);
19bbf 0a 20 20 69 66 28 20 28 70 44 65 73 74 2d 3e 69  .  if( (pDest->i
19bc0 50 4b 65 79 3c 30 20 26 26 20 70 44 65 73 74 2d  PKey<0 && pDest-
19bc1 3e 70 49 6e 64 65 78 21 3d 30 29 20 7c 7c 20 64  >pIndex!=0) || d
19bc2 65 73 74 48 61 73 55 6e 69 71 75 65 49 64 78 20  estHasUniqueIdx 
19bc3 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 61 62  ){.    /* If tab
19bc4 6c 65 73 20 64 6f 20 6e 6f 74 20 68 61 76 65 20  les do not have 
19bc5 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  an INTEGER PRIMA
19bc6 52 59 20 4b 45 59 20 61 6e 64 20 74 68 65 72 65  RY KEY and there
19bc7 0a 20 20 20 20 2a 2a 20 61 72 65 20 69 6e 64 69  .    ** are indi
19bc8 63 65 73 20 74 6f 20 62 65 20 63 6f 70 69 65 64  ces to be copied
19bc9 20 61 6e 64 20 74 68 65 20 64 65 73 74 69 6e 61   and the destina
19bca 74 69 6f 6e 20 69 73 20 6e 6f 74 20 65 6d 70 74  tion is not empt
19bcb 79 2c 0a 20 20 20 20 2a 2a 20 77 65 20 68 61 76  y,.    ** we hav
19bcc 65 20 74 6f 20 64 69 73 61 6c 6c 6f 77 20 74 68  e to disallow th
19bcd 65 20 74 72 61 6e 73 66 65 72 20 6f 70 74 69 6d  e transfer optim
19bce 69 7a 61 74 69 6f 6e 20 62 65 63 61 75 73 65 20  ization because 
19bcf 74 68 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 72  the.    ** the r
19bd0 6f 77 69 64 73 20 6d 69 67 68 74 20 63 68 61 6e  owids might chan
19bd1 67 65 20 77 68 69 63 68 20 77 69 6c 6c 20 6d 65  ge which will me
19bd2 73 73 20 75 70 20 69 6e 64 65 78 69 6e 67 2e 0a  ss up indexing..
19bd3 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4f 72      **.    ** Or
19bd4 20 69 66 20 74 68 65 20 64 65 73 74 69 6e 61 74   if the destinat
19bd5 69 6f 6e 20 68 61 73 20 61 20 55 4e 49 51 55 45  ion has a UNIQUE
19bd6 20 69 6e 64 65 78 20 61 6e 64 20 69 73 20 6e 6f   index and is no
19bd7 74 20 65 6d 70 74 79 2c 0a 20 20 20 20 2a 2a 20  t empty,.    ** 
19bd8 77 65 20 61 6c 73 6f 20 64 69 73 61 6c 6c 6f 77  we also disallow
19bd9 20 74 68 65 20 74 72 61 6e 73 66 65 72 20 6f 70   the transfer op
19bda 74 69 6d 69 7a 61 74 69 6f 6e 20 62 65 63 61 75  timization becau
19bdb 73 65 20 77 65 20 63 61 6e 6e 6f 74 0a 20 20 20  se we cannot.   
19bdc 20 2a 2a 20 69 6e 73 75 72 65 20 74 68 61 74 20   ** insure that 
19bdd 61 6c 6c 20 65 6e 74 72 69 65 73 20 69 6e 20 74  all entries in t
19bde 68 65 20 75 6e 69 6f 6e 20 6f 66 20 44 45 53 54  he union of DEST
19bdf 20 61 6e 64 20 53 52 43 20 77 69 6c 6c 20 62 65   and SRC will be
19be0 0a 20 20 20 20 2a 2a 20 75 6e 69 71 75 65 2e 0a  .    ** unique..
19be1 20 20 20 20 2a 2f 0a 20 20 20 20 61 64 64 72 31      */.    addr1
19be2 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
19be3 64 4f 70 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64  dOp(v, OP_Rewind
19be4 2c 20 69 44 65 73 74 2c 20 30 29 3b 0a 20 20 20  , iDest, 0);.   
19be5 20 65 6d 70 74 79 44 65 73 74 54 65 73 74 20 3d   emptyDestTest =
19be6 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
19be7 70 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  p(v, OP_Goto, 0,
19be8 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
19be9 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
19bea 61 64 64 72 31 29 3b 0a 20 20 7d 65 6c 73 65 7b  addr1);.  }else{
19beb 0a 20 20 20 20 65 6d 70 74 79 44 65 73 74 54 65  .    emptyDestTe
19bec 73 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71  st = 0;.  }.  sq
19bed 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70  lite3OpenTable(p
19bee 50 61 72 73 65 2c 20 69 53 72 63 2c 20 69 44 62  Parse, iSrc, iDb
19bef 53 72 63 2c 20 70 53 72 63 2c 20 4f 50 5f 4f 70  Src, pSrc, OP_Op
19bf0 65 6e 52 65 61 64 29 3b 0a 20 20 65 6d 70 74 79  enRead);.  empty
19bf1 53 72 63 54 65 73 74 20 3d 20 73 71 6c 69 74 65  SrcTest = sqlite
19bf2 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
19bf3 5f 52 65 77 69 6e 64 2c 20 69 53 72 63 2c 20 30  _Rewind, iSrc, 0
19bf4 29 3b 0a 20 20 69 66 28 20 70 44 65 73 74 2d 3e  );.  if( pDest->
19bf5 69 50 4b 65 79 3e 3d 30 20 29 7b 0a 20 20 20 20  iPKey>=0 ){.    
19bf6 61 64 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56  addr1 = sqlite3V
19bf7 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 52  dbeAddOp(v, OP_R
19bf8 6f 77 69 64 2c 20 69 53 72 63 2c 20 30 29 3b 0a  owid, iSrc, 0);.
19bf9 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
19bfa 64 64 4f 70 28 76 2c 20 4f 50 5f 44 75 70 2c 20  ddOp(v, OP_Dup, 
19bfb 30 2c 20 30 29 3b 0a 20 20 20 20 61 64 64 72 32  0, 0);.    addr2
19bfc 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
19bfd 64 4f 70 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69  dOp(v, OP_NotExi
19bfe 73 74 73 2c 20 69 44 65 73 74 2c 20 30 29 3b 0a  sts, iDest, 0);.
19bff 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4f      sqlite3VdbeO
19c00 70 33 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53  p3(v, OP_Halt, S
19c01 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
19c02 2c 20 6f 6e 45 72 72 6f 72 2c 20 0a 20 20 20 20  , onError, .    
19c03 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19c04 20 20 22 50 52 49 4d 41 52 59 20 4b 45 59 20 6d    "PRIMARY KEY m
19c05 75 73 74 20 62 65 20 75 6e 69 71 75 65 22 2c 20  ust be unique", 
19c06 50 33 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  P3_STATIC);.    
19c07 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
19c08 65 72 65 28 76 2c 20 61 64 64 72 32 29 3b 0a 20  ere(v, addr2);. 
19c09 20 20 20 61 75 74 6f 49 6e 63 53 74 65 70 28 70     autoIncStep(p
19c0a 50 61 72 73 65 2c 20 63 6f 75 6e 74 65 72 4d 65  Parse, counterMe
19c0b 6d 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  m);.  }else if( 
19c0c 70 44 65 73 74 2d 3e 70 49 6e 64 65 78 3d 3d 30  pDest->pIndex==0
19c0d 20 29 7b 0a 20 20 20 20 61 64 64 72 31 20 3d 20   ){.    addr1 = 
19c0e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
19c0f 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c  (v, OP_NewRowid,
19c10 20 69 44 65 73 74 2c 20 30 29 3b 0a 20 20 7d 65   iDest, 0);.  }e
19c11 6c 73 65 7b 0a 20 20 20 20 61 64 64 72 31 20 3d  lse{.    addr1 =
19c12 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
19c13 70 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69  p(v, OP_Rowid, i
19c14 53 72 63 2c 20 30 29 3b 0a 20 20 20 20 61 73 73  Src, 0);.    ass
19c15 65 72 74 28 20 70 44 65 73 74 2d 3e 61 75 74 6f  ert( pDest->auto
19c16 49 6e 63 3d 3d 30 20 29 3b 0a 20 20 7d 0a 20 20  Inc==0 );.  }.  
19c17 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
19c18 28 76 2c 20 4f 50 5f 52 6f 77 44 61 74 61 2c 20  (v, OP_RowData, 
19c19 69 53 72 63 2c 20 30 29 3b 0a 20 20 73 71 6c 69  iSrc, 0);.  sqli
19c1a 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20 4f 50  te3VdbeOp3(v, OP
19c1b 5f 49 6e 73 65 72 74 2c 20 69 44 65 73 74 2c 0a  _Insert, iDest,.
19c1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19c1d 20 20 20 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e      OPFLAG_NCHAN
19c1e 47 45 7c 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f  GE|OPFLAG_LASTRO
19c1f 57 49 44 7c 4f 50 46 4c 41 47 5f 41 50 50 45 4e  WID|OPFLAG_APPEN
19c20 44 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  D,.             
19c21 20 20 20 20 20 20 20 70 44 65 73 74 2d 3e 7a 4e         pDest->zN
19c22 61 6d 65 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74  ame, 0);.  sqlit
19c23 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
19c24 50 5f 4e 65 78 74 2c 20 69 53 72 63 2c 20 61 64  P_Next, iSrc, ad
19c25 64 72 31 29 3b 0a 20 20 61 75 74 6f 49 6e 63 45  dr1);.  autoIncE
19c26 6e 64 28 70 50 61 72 73 65 2c 20 69 44 62 44 65  nd(pParse, iDbDe
19c27 73 74 2c 20 70 44 65 73 74 2c 20 63 6f 75 6e 74  st, pDest, count
19c28 65 72 4d 65 6d 29 3b 0a 20 20 66 6f 72 28 70 44  erMem);.  for(pD
19c29 65 73 74 49 64 78 3d 70 44 65 73 74 2d 3e 70 49  estIdx=pDest->pI
19c2a 6e 64 65 78 3b 20 70 44 65 73 74 49 64 78 3b 20  ndex; pDestIdx; 
19c2b 70 44 65 73 74 49 64 78 3d 70 44 65 73 74 49 64  pDestIdx=pDestId
19c2c 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 66  x->pNext){.    f
19c2d 6f 72 28 70 53 72 63 49 64 78 3d 70 53 72 63 2d  or(pSrcIdx=pSrc-
19c2e 3e 70 49 6e 64 65 78 3b 20 70 53 72 63 49 64 78  >pIndex; pSrcIdx
19c2f 3b 20 70 53 72 63 49 64 78 3d 70 53 72 63 49 64  ; pSrcIdx=pSrcId
19c30 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20  x->pNext){.     
19c31 20 69 66 28 20 78 66 65 72 43 6f 6d 70 61 74 69   if( xferCompati
19c32 62 6c 65 49 6e 64 65 78 28 70 44 65 73 74 49 64  bleIndex(pDestId
19c33 78 2c 20 70 53 72 63 49 64 78 29 20 29 20 62 72  x, pSrcIdx) ) br
19c34 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  eak;.    }.    a
19c35 73 73 65 72 74 28 20 70 53 72 63 49 64 78 20 29  ssert( pSrcIdx )
19c36 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
19c37 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f  eAddOp(v, OP_Clo
19c38 73 65 2c 20 69 53 72 63 2c 20 30 29 3b 0a 20 20  se, iSrc, 0);.  
19c39 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
19c3a 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20  Op(v, OP_Close, 
19c3b 69 44 65 73 74 2c 20 30 29 3b 0a 20 20 20 20 73  iDest, 0);.    s
19c3c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
19c3d 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69  v, OP_Integer, i
19c3e 44 62 53 72 63 2c 20 30 29 3b 0a 20 20 20 20 70  DbSrc, 0);.    p
19c3f 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64  Key = sqlite3Ind
19c40 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65  exKeyinfo(pParse
19c41 2c 20 70 53 72 63 49 64 78 29 3b 0a 20 20 20 20  , pSrcIdx);.    
19c42 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
19c43 22 23 20 25 73 22 2c 20 70 53 72 63 49 64 78 2d  "# %s", pSrcIdx-
19c44 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 73 71  >zName));.    sq
19c45 6c 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20  lite3VdbeOp3(v, 
19c46 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20 69 53 72  OP_OpenRead, iSr
19c47 63 2c 20 70 53 72 63 49 64 78 2d 3e 74 6e 75 6d  c, pSrcIdx->tnum
19c48 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
19c49 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65        (char*)pKe
19c4a 79 2c 20 50 33 5f 4b 45 59 49 4e 46 4f 5f 48 41  y, P3_KEYINFO_HA
19c4b 4e 44 4f 46 46 29 3b 0a 20 20 20 20 73 71 6c 69  NDOFF);.    sqli
19c4c 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
19c4d 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 44 62 44  OP_Integer, iDbD
19c4e 65 73 74 2c 20 30 29 3b 0a 20 20 20 20 70 4b 65  est, 0);.    pKe
19c4f 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78  y = sqlite3Index
19c50 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20  Keyinfo(pParse, 
19c51 70 44 65 73 74 49 64 78 29 3b 0a 20 20 20 20 56  pDestIdx);.    V
19c52 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
19c53 23 20 25 73 22 2c 20 70 44 65 73 74 49 64 78 2d  # %s", pDestIdx-
19c54 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 73 71  >zName));.    sq
19c55 6c 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20  lite3VdbeOp3(v, 
19c56 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69 44  OP_OpenWrite, iD
19c57 65 73 74 2c 20 70 44 65 73 74 49 64 78 2d 3e 74  est, pDestIdx->t
19c58 6e 75 6d 2c 20 0a 20 20 20 20 20 20 20 20 20 20  num, .          
19c59 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29           (char*)
19c5a 70 4b 65 79 2c 20 50 33 5f 4b 45 59 49 4e 46 4f  pKey, P3_KEYINFO
19c5b 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 61  _HANDOFF);.    a
19c5c 64 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64  ddr1 = sqlite3Vd
19c5d 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 52 65  beAddOp(v, OP_Re
19c5e 77 69 6e 64 2c 20 69 53 72 63 2c 20 30 29 3b 0a  wind, iSrc, 0);.
19c5f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
19c60 64 64 4f 70 28 76 2c 20 4f 50 5f 52 6f 77 4b 65  ddOp(v, OP_RowKe
19c61 79 2c 20 69 53 72 63 2c 20 30 29 3b 0a 20 20 20  y, iSrc, 0);.   
19c62 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
19c63 70 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72  p(v, OP_IdxInser
19c64 74 2c 20 69 44 65 73 74 2c 20 31 29 3b 0a 20 20  t, iDest, 1);.  
19c65 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
19c66 4f 70 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 69  Op(v, OP_Next, i
19c67 53 72 63 2c 20 61 64 64 72 31 2b 31 29 3b 0a 20  Src, addr1+1);. 
19c68 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
19c69 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 31 29  mpHere(v, addr1)
19c6a 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56  ;.  }.  sqlite3V
19c6b 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 65  dbeJumpHere(v, e
19c6c 6d 70 74 79 53 72 63 54 65 73 74 29 3b 0a 20 20  mptySrcTest);.  
19c6d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
19c6e 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 53  (v, OP_Close, iS
19c6f 72 63 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65  rc, 0);.  sqlite
19c70 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
19c71 5f 43 6c 6f 73 65 2c 20 69 44 65 73 74 2c 20 30  _Close, iDest, 0
19c72 29 3b 0a 20 20 69 66 28 20 65 6d 70 74 79 44 65  );.  if( emptyDe
19c73 73 74 54 65 73 74 20 29 7b 0a 20 20 20 20 73 71  stTest ){.    sq
19c74 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
19c75 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54  , OP_Halt, SQLIT
19c76 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 20 20 73 71  E_OK, 0);.    sq
19c77 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
19c78 65 28 76 2c 20 65 6d 70 74 79 44 65 73 74 54 65  e(v, emptyDestTe
19c79 73 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  st);.    sqlite3
19c7a 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
19c7b 43 6c 6f 73 65 2c 20 69 44 65 73 74 2c 20 30 29  Close, iDest, 0)
19c7c 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  ;.    return 0;.
19c7d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
19c7e 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65 6e  urn 1;.  }.}.#en
19c7f 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
19c80 49 54 5f 58 46 45 52 5f 4f 50 54 20 2a 2f 0a 0a  IT_XFER_OPT */..
19c81 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
19c82 45 6e 64 20 6f 66 20 69 6e 73 65 72 74 2e 63 20  End of insert.c 
19c83 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19c84 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19c85 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
19c86 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
19c87 42 65 67 69 6e 20 66 69 6c 65 20 6c 65 67 61 63  Begin file legac
19c88 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  y.c ************
19c89 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19c8a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
19c8b 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
19c8c 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68  mber 15.**.** Th
19c8d 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
19c8e 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
19c8f 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
19c90 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
19c91 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
19c92 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
19c93 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
19c94 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
19c95 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
19c96 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
19c97 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
19c98 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
19c99 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
19c9a 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
19c9b 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
19c9c 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
19c9d 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
19c9e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19c9f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19ca1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19ca2 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 4d 61 69 6e 20 66  ******.** Main f
19ca3 69 6c 65 20 66 6f 72 20 74 68 65 20 53 51 4c 69  ile for the SQLi
19ca4 74 65 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65  te library.  The
19ca5 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69   routines in thi
19ca6 73 20 66 69 6c 65 0a 2a 2a 20 69 6d 70 6c 65 6d  s file.** implem
19ca7 65 6e 74 20 74 68 65 20 70 72 6f 67 72 61 6d 6d  ent the programm
19ca8 65 72 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20  er interface to 
19ca9 74 68 65 20 6c 69 62 72 61 72 79 2e 20 20 52 6f  the library.  Ro
19caa 75 74 69 6e 65 73 20 69 6e 0a 2a 2a 20 6f 74 68  utines in.** oth
19cab 65 72 20 66 69 6c 65 73 20 61 72 65 20 66 6f 72  er files are for
19cac 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 20 62 79   internal use by
19cad 20 53 51 4c 69 74 65 20 61 6e 64 20 73 68 6f 75   SQLite and shou
19cae 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a 20 61 63 63  ld not be.** acc
19caf 65 73 73 65 64 20 62 79 20 75 73 65 72 73 20 6f  essed by users o
19cb0 66 20 74 68 65 20 6c 69 62 72 61 72 79 2e 0a 2a  f the library..*
19cb1 2a 0a 2a 2a 20 24 49 64 3a 20 6c 65 67 61 63 79  *.** $Id: legacy
19cb2 2e 63 2c 76 20 31 2e 31 38 20 32 30 30 37 2f 30  .c,v 1.18 2007/0
19cb3 35 2f 30 34 20 31 33 3a 31 35 3a 35 36 20 64 72  5/04 13:15:56 dr
19cb4 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a  h Exp $.*/.../*.
19cb5 2a 2a 20 45 78 65 63 75 74 65 20 53 51 4c 20 63  ** Execute SQL c
19cb6 6f 64 65 2e 20 20 52 65 74 75 72 6e 20 6f 6e 65  ode.  Return one
19cb7 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f 20   of the SQLITE_ 
19cb8 73 75 63 63 65 73 73 2f 66 61 69 6c 75 72 65 0a  success/failure.
19cb9 2a 2a 20 63 6f 64 65 73 2e 20 20 41 6c 73 6f 20  ** codes.  Also 
19cba 77 72 69 74 65 20 61 6e 20 65 72 72 6f 72 20 6d  write an error m
19cbb 65 73 73 61 67 65 20 69 6e 74 6f 20 6d 65 6d 6f  essage into memo
19cbc 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ry obtained from
19cbd 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 61 6e 64  .** malloc() and
19cbe 20 6d 61 6b 65 20 2a 70 7a 45 72 72 4d 73 67 20   make *pzErrMsg 
19cbf 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 20 6d 65  point to that me
19cc0 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ssage..**.** If 
19cc1 74 68 65 20 53 51 4c 20 69 73 20 61 20 71 75 65  the SQL is a que
19cc2 72 79 2c 20 74 68 65 6e 20 66 6f 72 20 65 61 63  ry, then for eac
19cc3 68 20 72 6f 77 20 69 6e 20 74 68 65 20 71 75 65  h row in the que
19cc4 72 79 20 72 65 73 75 6c 74 0a 2a 2a 20 74 68 65  ry result.** the
19cc5 20 78 43 61 6c 6c 62 61 63 6b 28 29 20 66 75 6e   xCallback() fun
19cc6 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e  ction is called.
19cc7 20 20 70 41 72 67 20 62 65 63 6f 6d 65 73 20 74    pArg becomes t
19cc8 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75  he first.** argu
19cc9 6d 65 6e 74 20 74 6f 20 78 43 61 6c 6c 62 61 63  ment to xCallbac
19cca 6b 28 29 2e 20 20 49 66 20 78 43 61 6c 6c 62 61  k().  If xCallba
19ccb 63 6b 3d 4e 55 4c 4c 20 74 68 65 6e 20 6e 6f 20  ck=NULL then no 
19ccc 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 20 69  callback.** is i
19ccd 6e 76 6f 6b 65 64 2c 20 65 76 65 6e 20 66 6f 72  nvoked, even for
19cce 20 71 75 65 72 69 65 73 2e 0a 2a 2f 0a 53 51 4c   queries..*/.SQL
19ccf 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
19cd0 74 65 33 5f 65 78 65 63 28 0a 20 20 73 71 6c 69  te3_exec(.  sqli
19cd1 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
19cd2 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
19cd3 61 74 61 62 61 73 65 20 6f 6e 20 77 68 69 63 68  atabase on which
19cd4 20 74 68 65 20 53 51 4c 20 65 78 65 63 75 74 65   the SQL execute
19cd5 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  s */.  const cha
19cd6 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20  r *zSql,        
19cd7 20 20 20 2f 2a 20 54 68 65 20 53 51 4c 20 74 6f     /* The SQL to
19cd8 20 62 65 20 65 78 65 63 75 74 65 64 20 2a 2f 0a   be executed */.
19cd9 20 20 73 71 6c 69 74 65 33 5f 63 61 6c 6c 62 61    sqlite3_callba
19cda 63 6b 20 78 43 61 6c 6c 62 61 63 6b 2c 20 2f 2a  ck xCallback, /*
19cdb 20 49 6e 76 6f 6b 65 20 74 68 69 73 20 63 61 6c   Invoke this cal
19cdc 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 20 2a 2f  lback routine */
19cdd 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 2c 20 20  .  void *pArg,  
19cde 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
19cdf 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74  * First argument
19ce0 20 74 6f 20 78 43 61 6c 6c 62 61 63 6b 28 29 20   to xCallback() 
19ce1 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72  */.  char **pzEr
19ce2 72 4d 73 67 20 20 20 20 20 20 20 20 20 20 20 20  rMsg            
19ce3 20 2f 2a 20 57 72 69 74 65 20 65 72 72 6f 72 20   /* Write error 
19ce4 6d 65 73 73 61 67 65 73 20 68 65 72 65 20 2a 2f  messages here */
19ce5 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  .){.  int rc = S
19ce6 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63 6f 6e 73  QLITE_OK;.  cons
19ce7 74 20 63 68 61 72 20 2a 7a 4c 65 66 74 6f 76 65  t char *zLeftove
19ce8 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  r;.  sqlite3_stm
19ce9 74 20 2a 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20  t *pStmt = 0;.  
19cea 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 73 20 3d 20  char **azCols = 
19ceb 30 3b 0a 0a 20 20 69 6e 74 20 6e 52 65 74 72 79  0;..  int nRetry
19cec 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 43 61 6c   = 0;.  int nCal
19ced 6c 62 61 63 6b 3b 0a 0a 20 20 69 66 28 20 7a 53  lback;..  if( zS
19cee 71 6c 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53  ql==0 ) return S
19cef 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 77 68 69 6c  QLITE_OK;.  whil
19cf0 65 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  e( (rc==SQLITE_O
19cf1 4b 20 7c 7c 20 28 72 63 3d 3d 53 51 4c 49 54 45  K || (rc==SQLITE
19cf2 5f 53 43 48 45 4d 41 20 26 26 20 28 2b 2b 6e 52  _SCHEMA && (++nR
19cf3 65 74 72 79 29 3c 32 29 29 20 26 26 20 7a 53 71  etry)<2)) && zSq
19cf4 6c 5b 30 5d 20 29 7b 0a 20 20 20 20 69 6e 74 20  l[0] ){.    int 
19cf5 6e 43 6f 6c 3b 0a 20 20 20 20 63 68 61 72 20 2a  nCol;.    char *
19cf6 2a 61 7a 56 61 6c 73 20 3d 20 30 3b 0a 0a 20 20  *azVals = 0;..  
19cf7 20 20 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20    pStmt = 0;.   
19cf8 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72   rc = sqlite3_pr
19cf9 65 70 61 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20  epare(db, zSql, 
19cfa 2d 31 2c 20 26 70 53 74 6d 74 2c 20 26 7a 4c 65  -1, &pStmt, &zLe
19cfb 66 74 6f 76 65 72 29 3b 0a 20 20 20 20 61 73 73  ftover);.    ass
19cfc 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
19cfd 4f 4b 20 7c 7c 20 70 53 74 6d 74 3d 3d 30 20 29  OK || pStmt==0 )
19cfe 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
19cff 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
19d00 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d   continue;.    }
19d01 0a 20 20 20 20 69 66 28 20 21 70 53 74 6d 74 20  .    if( !pStmt 
19d02 29 7b 0a 20 20 20 20 20 20 2f 2a 20 74 68 69 73  ){.      /* this
19d03 20 68 61 70 70 65 6e 73 20 66 6f 72 20 61 20 63   happens for a c
19d04 6f 6d 6d 65 6e 74 20 6f 72 20 77 68 69 74 65 2d  omment or white-
19d05 73 70 61 63 65 20 2a 2f 0a 20 20 20 20 20 20 7a  space */.      z
19d06 53 71 6c 20 3d 20 7a 4c 65 66 74 6f 76 65 72 3b  Sql = zLeftover;
19d07 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b  .      continue;
19d08 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e 43 61 6c  .    }..    nCal
19d09 6c 62 61 63 6b 20 3d 20 30 3b 0a 0a 20 20 20 20  lback = 0;..    
19d0a 6e 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 5f 63  nCol = sqlite3_c
19d0b 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 70 53 74 6d  olumn_count(pStm
19d0c 74 29 3b 0a 20 20 20 20 61 7a 43 6f 6c 73 20 3d  t);.    azCols =
19d0d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 32 2a   sqliteMalloc(2*
19d0e 6e 43 6f 6c 2a 73 69 7a 65 6f 66 28 63 6f 6e 73  nCol*sizeof(cons
19d0f 74 20 63 68 61 72 20 2a 29 20 2b 20 31 29 3b 0a  t char *) + 1);.
19d10 20 20 20 20 69 66 28 20 61 7a 43 6f 6c 73 3d 3d      if( azCols==
19d11 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  0 ){.      goto 
19d12 65 78 65 63 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a  exec_out;.    }.
19d13 0a 20 20 20 20 77 68 69 6c 65 28 20 31 20 29 7b  .    while( 1 ){
19d14 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  .      int i;.  
19d15 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
19d16 5f 73 74 65 70 28 70 53 74 6d 74 29 3b 0a 0a 20  _step(pStmt);.. 
19d17 20 20 20 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74       /* Invoke t
19d18 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63  he callback func
19d19 74 69 6f 6e 20 69 66 20 72 65 71 75 69 72 65 64  tion if required
19d1a 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 78 43   */.      if( xC
19d1b 61 6c 6c 62 61 63 6b 20 26 26 20 28 53 51 4c 49  allback && (SQLI
19d1c 54 45 5f 52 4f 57 3d 3d 72 63 20 7c 7c 20 0a 20  TE_ROW==rc || . 
19d1d 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45           (SQLITE
19d1e 5f 44 4f 4e 45 3d 3d 72 63 20 26 26 20 21 6e 43  _DONE==rc && !nC
19d1f 61 6c 6c 62 61 63 6b 20 26 26 20 64 62 2d 3e 66  allback && db->f
19d20 6c 61 67 73 26 53 51 4c 49 54 45 5f 4e 75 6c 6c  lags&SQLITE_Null
19d21 43 61 6c 6c 62 61 63 6b 29 29 20 29 7b 0a 20 20  Callback)) ){.  
19d22 20 20 20 20 20 20 69 66 28 20 30 3d 3d 6e 43 61        if( 0==nCa
19d23 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 20 20  llback ){.      
19d24 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
19d25 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Col; i++){.     
19d26 20 20 20 20 20 20 20 61 7a 43 6f 6c 73 5b 69 5d         azCols[i]
19d27 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74   = (char *)sqlit
19d28 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 70  e3_column_name(p
19d29 53 74 6d 74 2c 20 69 29 3b 0a 20 20 20 20 20 20  Stmt, i);.      
19d2a 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
19d2b 6e 43 61 6c 6c 62 61 63 6b 2b 2b 3b 0a 20 20 20  nCallback++;.   
19d2c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
19d2d 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f  f( rc==SQLITE_RO
19d2e 57 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61  W ){.          a
19d2f 7a 56 61 6c 73 20 3d 20 26 61 7a 43 6f 6c 73 5b  zVals = &azCols[
19d30 6e 43 6f 6c 5d 3b 0a 20 20 20 20 20 20 20 20 20  nCol];.         
19d31 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c   for(i=0; i<nCol
19d32 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
19d33 20 20 20 20 61 7a 56 61 6c 73 5b 69 5d 20 3d 20      azVals[i] = 
19d34 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f  (char *)sqlite3_
19d35 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 70 53 74 6d  column_text(pStm
19d36 74 2c 20 69 29 3b 0a 20 20 20 20 20 20 20 20 20  t, i);.         
19d37 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
19d38 20 20 20 20 20 69 66 28 20 78 43 61 6c 6c 62 61       if( xCallba
19d39 63 6b 28 70 41 72 67 2c 20 6e 43 6f 6c 2c 20 61  ck(pArg, nCol, a
19d3a 7a 56 61 6c 73 2c 20 61 7a 43 6f 6c 73 29 20 29  zVals, azCols) )
19d3b 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  {.          rc =
19d3c 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20   SQLITE_ABORT;. 
19d3d 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78           goto ex
19d3e 65 63 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20  ec_out;.        
19d3f 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  }.      }..     
19d40 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
19d41 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ROW ){.        r
19d42 63 20 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  c = sqlite3_fina
19d43 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 20 20 20  lize(pStmt);.   
19d44 20 20 20 20 20 70 53 74 6d 74 20 3d 20 30 3b 0a       pStmt = 0;.
19d45 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
19d46 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 29 7b  SQLITE_SCHEMA ){
19d47 0a 20 20 20 20 20 20 20 20 20 20 6e 52 65 74 72  .          nRetr
19d48 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  y = 0;.         
19d49 20 7a 53 71 6c 20 3d 20 7a 4c 65 66 74 6f 76 65   zSql = zLeftove
19d4a 72 3b 0a 20 20 20 20 20 20 20 20 20 20 77 68 69  r;.          whi
19d4b 6c 65 28 20 69 73 73 70 61 63 65 28 28 75 6e 73  le( isspace((uns
19d4c 69 67 6e 65 64 20 63 68 61 72 29 7a 53 71 6c 5b  igned char)zSql[
19d4d 30 5d 29 20 29 20 7a 53 71 6c 2b 2b 3b 0a 20 20  0]) ) zSql++;.  
19d4e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
19d4f 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
19d50 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65     }..    sqlite
19d51 46 72 65 65 28 61 7a 43 6f 6c 73 29 3b 0a 20 20  Free(azCols);.  
19d52 20 20 61 7a 43 6f 6c 73 20 3d 20 30 3b 0a 20 20    azCols = 0;.  
19d53 7d 0a 0a 65 78 65 63 5f 6f 75 74 3a 0a 20 20 69  }..exec_out:.  i
19d54 66 28 20 70 53 74 6d 74 20 29 20 73 71 6c 69 74  f( pStmt ) sqlit
19d55 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d  e3_finalize(pStm
19d56 74 29 3b 0a 20 20 69 66 28 20 61 7a 43 6f 6c 73  t);.  if( azCols
19d57 20 29 20 73 71 6c 69 74 65 46 72 65 65 28 61 7a   ) sqliteFree(az
19d58 43 6f 6c 73 29 3b 0a 0a 20 20 72 63 20 3d 20 73  Cols);..  rc = s
19d59 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 30 2c  qlite3ApiExit(0,
19d5a 20 72 63 29 3b 0a 20 20 69 66 28 20 72 63 21 3d   rc);.  if( rc!=
19d5b 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 3d  SQLITE_OK && rc=
19d5c 3d 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65  =sqlite3_errcode
19d5d 28 64 62 29 20 26 26 20 70 7a 45 72 72 4d 73 67  (db) && pzErrMsg
19d5e 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 45 72 72   ){.    int nErr
19d5f 4d 73 67 20 3d 20 31 20 2b 20 73 74 72 6c 65 6e  Msg = 1 + strlen
19d60 28 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28  (sqlite3_errmsg(
19d61 64 62 29 29 3b 0a 20 20 20 20 2a 70 7a 45 72 72  db));.    *pzErr
19d62 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  Msg = sqlite3_ma
19d63 6c 6c 6f 63 28 6e 45 72 72 4d 73 67 29 3b 0a 20  lloc(nErrMsg);. 
19d64 20 20 20 69 66 28 20 2a 70 7a 45 72 72 4d 73 67     if( *pzErrMsg
19d65 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79   ){.      memcpy
19d66 28 2a 70 7a 45 72 72 4d 73 67 2c 20 73 71 6c 69  (*pzErrMsg, sqli
19d67 74 65 33 5f 65 72 72 6d 73 67 28 64 62 29 2c 20  te3_errmsg(db), 
19d68 6e 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 7d 0a  nErrMsg);.    }.
19d69 20 20 7d 65 6c 73 65 20 69 66 28 20 70 7a 45 72    }else if( pzEr
19d6a 72 4d 73 67 20 29 7b 0a 20 20 20 20 2a 70 7a 45  rMsg ){.    *pzE
19d6b 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 7d 0a 0a  rrMsg = 0;.  }..
19d6c 20 20 61 73 73 65 72 74 28 20 28 72 63 26 64 62    assert( (rc&db
19d6d 2d 3e 65 72 72 4d 61 73 6b 29 3d 3d 72 63 20 29  ->errMask)==rc )
19d6e 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
19d6f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
19d70 2a 20 45 6e 64 20 6f 66 20 6c 65 67 61 63 79 2e  * End of legacy.
19d71 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
19d72 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19d73 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19d74 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
19d75 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6c 6f 61  * Begin file loa
19d76 64 65 78 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  dext.c *********
19d77 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19d78 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19d79 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 4a 75 6e  /./*.** 2006 Jun
19d7a 65 20 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75  e 7.**.** The au
19d7b 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
19d7c 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
19d7d 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
19d7e 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
19d7f 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
19d80 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
19d81 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
19d82 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
19d83 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
19d84 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
19d85 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
19d86 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
19d87 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
19d88 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
19d89 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
19d8a 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
19d8b 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
19d8c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19d8d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19d8e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19d8f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19d90 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
19d91 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73  contains code us
19d92 65 64 20 74 6f 20 64 79 6e 61 6d 69 63 61 6c 6c  ed to dynamicall
19d93 79 20 6c 6f 61 64 20 65 78 74 65 6e 73 69 6f 6e  y load extension
19d94 73 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 53 51  s into.** the SQ
19d95 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2f  Lite library..*/
19d96 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
19d97 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53  OMIT_LOAD_EXTENS
19d98 49 4f 4e 0a 0a 23 64 65 66 69 6e 65 20 53 51 4c  ION..#define SQL
19d99 49 54 45 5f 43 4f 52 45 20 31 20 20 2f 2a 20 44  ITE_CORE 1  /* D
19d9a 69 73 61 62 6c 65 20 74 68 65 20 41 50 49 20 72  isable the API r
19d9b 65 64 65 66 69 6e 69 74 69 6f 6e 20 69 6e 20 73  edefinition in s
19d9c 71 6c 69 74 65 33 65 78 74 2e 68 20 2a 2f 0a 2f  qlite3ext.h */./
19d9d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49  ************** I
19d9e 6e 63 6c 75 64 65 20 73 71 6c 69 74 65 33 65 78  nclude sqlite3ex
19d9f 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  t.h in the middl
19da0 65 20 6f 66 20 6c 6f 61 64 65 78 74 2e 63 20 2a  e of loadext.c *
19da1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
19da2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
19da3 65 67 69 6e 20 66 69 6c 65 20 73 71 6c 69 74 65  egin file sqlite
19da4 33 65 78 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  3ext.h *********
19da5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19da6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
19da7 2a 0a 2a 2a 20 32 30 30 36 20 4a 75 6e 65 20 37  *.** 2006 June 7
19da8 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
19da9 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
19daa 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
19dab 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
19dac 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
19dad 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
19dae 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
19daf 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
19db0 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
19db1 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
19db2 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
19db3 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
19db4 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
19db5 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
19db6 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
19db7 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
19db8 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
19db9 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
19dba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19dbb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19dbc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19dbd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
19dbe 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66  ** This header f
19dbf 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65 20  ile defines the 
19dc0 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65  SQLite interface
19dc1 20 66 6f 72 20 75 73 65 20 62 79 0a 2a 2a 20 73   for use by.** s
19dc2 68 61 72 65 64 20 6c 69 62 72 61 72 69 65 73 20  hared libraries 
19dc3 74 68 61 74 20 77 61 6e 74 20 74 6f 20 62 65 20  that want to be 
19dc4 69 6d 70 6f 72 74 65 64 20 61 73 20 65 78 74 65  imported as exte
19dc5 6e 73 69 6f 6e 73 20 69 6e 74 6f 0a 2a 2a 20 61  nsions into.** a
19dc6 6e 20 53 51 4c 69 74 65 20 69 6e 73 74 61 6e 63  n SQLite instanc
19dc7 65 2e 20 20 53 68 61 72 65 64 20 6c 69 62 72 61  e.  Shared libra
19dc8 72 69 65 73 20 74 68 61 74 20 69 6e 74 65 6e 64  ries that intend
19dc9 20 74 6f 20 62 65 20 6c 6f 61 64 65 64 0a 2a 2a   to be loaded.**
19dca 20 61 73 20 65 78 74 65 6e 73 69 6f 6e 73 20 62   as extensions b
19dcb 79 20 53 51 4c 69 74 65 20 73 68 6f 75 6c 64 20  y SQLite should 
19dcc 23 69 6e 63 6c 75 64 65 20 74 68 69 73 20 66 69  #include this fi
19dcd 6c 65 20 69 6e 73 74 65 61 64 20 6f 66 20 0a 2a  le instead of .*
19dce 2a 20 73 71 6c 69 74 65 33 2e 68 2e 0a 2a 2a 0a  * sqlite3.h..**.
19dcf 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 73 71 6c  ** @(#) $Id: sql
19dd0 69 74 65 33 65 78 74 2e 68 2c 76 20 31 2e 31 32  ite3ext.h,v 1.12
19dd1 20 32 30 30 37 2f 30 37 2f 32 30 20 31 30 3a 34   2007/07/20 10:4
19dd2 38 3a 33 36 20 64 72 68 20 45 78 70 20 24 0a 2a  8:36 drh Exp $.*
19dd3 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54  /.#ifndef _SQLIT
19dd4 45 33 45 58 54 5f 48 5f 0a 23 64 65 66 69 6e 65  E3EXT_H_.#define
19dd5 20 5f 53 51 4c 49 54 45 33 45 58 54 5f 48 5f 0a   _SQLITE3EXT_H_.
19dd6 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
19dd7 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74  sqlite3_api_rout
19dd8 69 6e 65 73 20 73 71 6c 69 74 65 33 5f 61 70 69  ines sqlite3_api
19dd9 5f 72 6f 75 74 69 6e 65 73 3b 0a 0a 2f 2a 0a 2a  _routines;../*.*
19dda 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
19ddb 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 20 70  structure hold p
19ddc 6f 69 6e 74 65 72 73 20 74 6f 20 61 6c 6c 20 6f  ointers to all o
19ddd 66 20 74 68 65 20 53 51 4c 69 74 65 20 41 50 49  f the SQLite API
19dde 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a  .** routines..**
19ddf 0a 2a 2a 20 57 41 52 4e 49 4e 47 3a 20 20 49 6e  .** WARNING:  In
19de0 20 6f 72 64 65 72 20 74 6f 20 6d 61 69 6e 74 61   order to mainta
19de1 69 6e 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d  in backwards com
19de2 70 61 74 69 62 69 6c 69 74 79 2c 20 61 64 64 20  patibility, add 
19de3 6e 65 77 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  new.** interface
19de4 73 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20  s to the end of 
19de5 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 6f  this structure o
19de6 6e 6c 79 2e 20 20 49 66 20 79 6f 75 20 69 6e 73  nly.  If you ins
19de7 65 72 74 20 6e 65 77 0a 2a 2a 20 69 6e 74 65 72  ert new.** inter
19de8 66 61 63 65 73 20 69 6e 20 74 68 65 20 6d 69 64  faces in the mid
19de9 64 6c 65 20 6f 66 20 74 68 69 73 20 73 74 72 75  dle of this stru
19dea 63 74 75 72 65 2c 20 74 68 65 6e 20 6f 6c 64 65  cture, then olde
19deb 72 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 76  r different.** v
19dec 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74  ersions of SQLit
19ded 65 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62  e will not be ab
19dee 6c 65 20 74 6f 20 6c 6f 61 64 20 65 61 63 68 20  le to load each 
19def 6f 74 68 65 72 73 20 73 68 61 72 65 64 0a 2a 2a  others shared.**
19df0 20 6c 69 62 72 61 72 69 65 73 21 0a 2a 2f 0a 73   libraries!.*/.s
19df1 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 61 70  truct sqlite3_ap
19df2 69 5f 72 6f 75 74 69 6e 65 73 20 7b 0a 20 20 76  i_routines {.  v
19df3 6f 69 64 20 2a 20 28 2a 61 67 67 72 65 67 61 74  oid * (*aggregat
19df4 65 5f 63 6f 6e 74 65 78 74 29 28 73 71 6c 69 74  e_context)(sqlit
19df5 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 20  e3_context*,int 
19df6 6e 42 79 74 65 73 29 3b 0a 20 20 69 6e 74 20 20  nBytes);.  int  
19df7 28 2a 61 67 67 72 65 67 61 74 65 5f 63 6f 75 6e  (*aggregate_coun
19df8 74 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  t)(sqlite3_conte
19df9 78 74 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62  xt*);.  int  (*b
19dfa 69 6e 64 5f 62 6c 6f 62 29 28 73 71 6c 69 74 65  ind_blob)(sqlite
19dfb 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c 63 6f 6e 73  3_stmt*,int,cons
19dfc 74 20 76 6f 69 64 2a 2c 69 6e 74 20 6e 2c 76 6f  t void*,int n,vo
19dfd 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20  id(*)(void*));. 
19dfe 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 64 6f 75   int  (*bind_dou
19dff 62 6c 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  ble)(sqlite3_stm
19e00 74 2a 2c 69 6e 74 2c 64 6f 75 62 6c 65 29 3b 0a  t*,int,double);.
19e01 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 69 6e    int  (*bind_in
19e02 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  t)(sqlite3_stmt*
19e03 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20 20 69 6e 74  ,int,int);.  int
19e04 20 20 28 2a 62 69 6e 64 5f 69 6e 74 36 34 29 28    (*bind_int64)(
19e05 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
19e06 74 2c 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 3b  t,sqlite_int64);
19e07 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 6e  .  int  (*bind_n
19e08 75 6c 6c 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  ull)(sqlite3_stm
19e09 74 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 20  t*,int);.  int  
19e0a 28 2a 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72  (*bind_parameter
19e0b 5f 63 6f 75 6e 74 29 28 73 71 6c 69 74 65 33 5f  _count)(sqlite3_
19e0c 73 74 6d 74 2a 29 3b 0a 20 20 69 6e 74 20 20 28  stmt*);.  int  (
19e0d 2a 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  *bind_parameter_
19e0e 69 6e 64 65 78 29 28 73 71 6c 69 74 65 33 5f 73  index)(sqlite3_s
19e0f 74 6d 74 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  tmt*,const char*
19e10 7a 4e 61 6d 65 29 3b 0a 20 20 63 6f 6e 73 74 20  zName);.  const 
19e11 63 68 61 72 20 2a 20 28 2a 62 69 6e 64 5f 70 61  char * (*bind_pa
19e12 72 61 6d 65 74 65 72 5f 6e 61 6d 65 29 28 73 71  rameter_name)(sq
19e13 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29  lite3_stmt*,int)
19e14 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f  ;.  int  (*bind_
19e15 74 65 78 74 29 28 73 71 6c 69 74 65 33 5f 73 74  text)(sqlite3_st
19e16 6d 74 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68  mt*,int,const ch
19e17 61 72 2a 2c 69 6e 74 20 6e 2c 76 6f 69 64 28 2a  ar*,int n,void(*
19e18 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 69 6e 74  )(void*));.  int
19e19 20 20 28 2a 62 69 6e 64 5f 74 65 78 74 31 36 29    (*bind_text16)
19e1a 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
19e1b 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69  nt,const void*,i
19e1c 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a  nt,void(*)(void*
19e1d 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e  ));.  int  (*bin
19e1e 64 5f 76 61 6c 75 65 29 28 73 71 6c 69 74 65 33  d_value)(sqlite3
19e1f 5f 73 74 6d 74 2a 2c 69 6e 74 2c 63 6f 6e 73 74  _stmt*,int,const
19e20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29   sqlite3_value*)
19e21 3b 0a 20 20 69 6e 74 20 20 28 2a 62 75 73 79 5f  ;.  int  (*busy_
19e22 68 61 6e 64 6c 65 72 29 28 73 71 6c 69 74 65 33  handler)(sqlite3
19e23 2a 2c 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69  *,int(*)(void*,i
19e24 6e 74 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 69 6e  nt),void*);.  in
19e25 74 20 20 28 2a 62 75 73 79 5f 74 69 6d 65 6f 75  t  (*busy_timeou
19e26 74 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20  t)(sqlite3*,int 
19e27 6d 73 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 68  ms);.  int  (*ch
19e28 61 6e 67 65 73 29 28 73 71 6c 69 74 65 33 2a 29  anges)(sqlite3*)
19e29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6c 6f 73 65  ;.  int  (*close
19e2a 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 69  )(sqlite3*);.  i
19e2b 6e 74 20 20 28 2a 63 6f 6c 6c 61 74 69 6f 6e 5f  nt  (*collation_
19e2c 6e 65 65 64 65 64 29 28 73 71 6c 69 74 65 33 2a  needed)(sqlite3*
19e2d 2c 76 6f 69 64 2a 2c 76 6f 69 64 28 2a 29 28 76  ,void*,void(*)(v
19e2e 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e  oid*,sqlite3*,in
19e2f 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74  t eTextRep,const
19e30 20 63 68 61 72 2a 29 29 3b 0a 20 20 69 6e 74 20   char*));.  int 
19e31 20 28 2a 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65   (*collation_nee
19e32 64 65 64 31 36 29 28 73 71 6c 69 74 65 33 2a 2c  ded16)(sqlite3*,
19e33 76 6f 69 64 2a 2c 76 6f 69 64 28 2a 29 28 76 6f  void*,void(*)(vo
19e34 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74  id*,sqlite3*,int
19e35 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20   eTextRep,const 
19e36 76 6f 69 64 2a 29 29 3b 0a 20 20 63 6f 6e 73 74  void*));.  const
19e37 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e   void * (*column
19e38 5f 62 6c 6f 62 29 28 73 71 6c 69 74 65 33 5f 73  _blob)(sqlite3_s
19e39 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a  tmt*,int iCol);.
19e3a 20 20 69 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f    int  (*column_
19e3b 62 79 74 65 73 29 28 73 71 6c 69 74 65 33 5f 73  bytes)(sqlite3_s
19e3c 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a  tmt*,int iCol);.
19e3d 20 20 69 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f    int  (*column_
19e3e 62 79 74 65 73 31 36 29 28 73 71 6c 69 74 65 33  bytes16)(sqlite3
19e3f 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29  _stmt*,int iCol)
19e40 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6c 75 6d  ;.  int  (*colum
19e41 6e 5f 63 6f 75 6e 74 29 28 73 71 6c 69 74 65 33  n_count)(sqlite3
19e42 5f 73 74 6d 74 2a 70 53 74 6d 74 29 3b 0a 20 20  _stmt*pStmt);.  
19e43 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 28 2a 63  const char * (*c
19e44 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e  olumn_database_n
19e45 61 6d 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  ame)(sqlite3_stm
19e46 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74  t*,int);.  const
19e47 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e   void * (*column
19e48 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36  _database_name16
19e49 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
19e4a 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68  int);.  const ch
19e4b 61 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 64 65  ar * (*column_de
19e4c 63 6c 74 79 70 65 29 28 73 71 6c 69 74 65 33 5f  cltype)(sqlite3_
19e4d 73 74 6d 74 2a 2c 69 6e 74 20 69 29 3b 0a 20 20  stmt*,int i);.  
19e4e 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 63  const void * (*c
19e4f 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36  olumn_decltype16
19e50 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
19e51 69 6e 74 29 3b 0a 20 20 64 6f 75 62 6c 65 20 20  int);.  double  
19e52 28 2a 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 29  (*column_double)
19e53 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
19e54 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 69 6e 74 20  nt iCol);.  int 
19e55 20 28 2a 63 6f 6c 75 6d 6e 5f 69 6e 74 29 28 73   (*column_int)(s
19e56 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
19e57 20 69 43 6f 6c 29 3b 0a 20 20 73 71 6c 69 74 65   iCol);.  sqlite
19e58 5f 69 6e 74 36 34 20 20 28 2a 63 6f 6c 75 6d 6e  _int64  (*column
19e59 5f 69 6e 74 36 34 29 28 73 71 6c 69 74 65 33 5f  _int64)(sqlite3_
19e5a 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b  stmt*,int iCol);
19e5b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20  .  const char * 
19e5c 28 2a 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 29 28 73  (*column_name)(s
19e5d 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
19e5e 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  );.  const void 
19e5f 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31  * (*column_name1
19e60 36 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  6)(sqlite3_stmt*
19e61 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20 63  ,int);.  const c
19e62 68 61 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 6f  har * (*column_o
19e63 72 69 67 69 6e 5f 6e 61 6d 65 29 28 73 71 6c 69  rigin_name)(sqli
19e64 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a  te3_stmt*,int);.
19e65 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28    const void * (
19e66 2a 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e  *column_origin_n
19e67 61 6d 65 31 36 29 28 73 71 6c 69 74 65 33 5f 73  ame16)(sqlite3_s
19e68 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e  tmt*,int);.  con
19e69 73 74 20 63 68 61 72 20 2a 20 28 2a 63 6f 6c 75  st char * (*colu
19e6a 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 29 28 73  mn_table_name)(s
19e6b 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
19e6c 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  );.  const void 
19e6d 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65  * (*column_table
19e6e 5f 6e 61 6d 65 31 36 29 28 73 71 6c 69 74 65 33  _name16)(sqlite3
19e6f 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63  _stmt*,int);.  c
19e70 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
19e71 61 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 74 65  ar * (*column_te
19e72 78 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  xt)(sqlite3_stmt
19e73 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 63  *,int iCol);.  c
19e74 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f  onst void * (*co
19e75 6c 75 6d 6e 5f 74 65 78 74 31 36 29 28 73 71 6c  lumn_text16)(sql
19e76 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69  ite3_stmt*,int i
19e77 43 6f 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63  Col);.  int  (*c
19e78 6f 6c 75 6d 6e 5f 74 79 70 65 29 28 73 71 6c 69  olumn_type)(sqli
19e79 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43  te3_stmt*,int iC
19e7a 6f 6c 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76  ol);.  sqlite3_v
19e7b 61 6c 75 65 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 76  alue* (*column_v
19e7c 61 6c 75 65 29 28 73 71 6c 69 74 65 33 5f 73 74  alue)(sqlite3_st
19e7d 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20  mt*,int iCol);. 
19e7e 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6d 6d 69 74   void * (*commit
19e7f 5f 68 6f 6f 6b 29 28 73 71 6c 69 74 65 33 2a 2c  _hook)(sqlite3*,
19e80 69 6e 74 28 2a 29 28 76 6f 69 64 2a 29 2c 76 6f  int(*)(void*),vo
19e81 69 64 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63  id*);.  int  (*c
19e82 6f 6d 70 6c 65 74 65 29 28 63 6f 6e 73 74 20 63  omplete)(const c
19e83 68 61 72 2a 73 71 6c 29 3b 0a 20 20 69 6e 74 20  har*sql);.  int 
19e84 20 28 2a 63 6f 6d 70 6c 65 74 65 31 36 29 28 63   (*complete16)(c
19e85 6f 6e 73 74 20 76 6f 69 64 2a 73 71 6c 29 3b 0a  onst void*sql);.
19e86 20 20 69 6e 74 20 20 28 2a 63 72 65 61 74 65 5f    int  (*create_
19e87 63 6f 6c 6c 61 74 69 6f 6e 29 28 73 71 6c 69 74  collation)(sqlit
19e88 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  e3*,const char*,
19e89 69 6e 74 2c 76 6f 69 64 2a 2c 69 6e 74 28 2a 29  int,void*,int(*)
19e8a 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74  (void*,int,const
19e8b 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74   void*,int,const
19e8c 20 76 6f 69 64 2a 29 29 3b 0a 20 20 69 6e 74 20   void*));.  int 
19e8d 20 28 2a 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74   (*create_collat
19e8e 69 6f 6e 31 36 29 28 73 71 6c 69 74 65 33 2a 2c  ion16)(sqlite3*,
19e8f 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c  const char*,int,
19e90 76 6f 69 64 2a 2c 69 6e 74 28 2a 29 28 76 6f 69  void*,int(*)(voi
19e91 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69  d*,int,const voi
19e92 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69  d*,int,const voi
19e93 64 2a 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63  d*));.  int  (*c
19e94 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 29 28  reate_function)(
19e95 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63  sqlite3*,const c
19e96 68 61 72 2a 2c 69 6e 74 2c 69 6e 74 2c 76 6f 69  har*,int,int,voi
19e97 64 2a 2c 76 6f 69 64 20 28 2a 78 46 75 6e 63 29  d*,void (*xFunc)
19e98 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
19e99 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61  *,int,sqlite3_va
19e9a 6c 75 65 2a 2a 29 2c 76 6f 69 64 20 28 2a 78 53  lue**),void (*xS
19e9b 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  tep)(sqlite3_con
19e9c 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  text*,int,sqlite
19e9d 33 5f 76 61 6c 75 65 2a 2a 29 2c 76 6f 69 64 20  3_value**),void 
19e9e 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69 74 65  (*xFinal)(sqlite
19e9f 33 5f 63 6f 6e 74 65 78 74 2a 29 29 3b 0a 20 20  3_context*));.  
19ea0 69 6e 74 20 20 28 2a 63 72 65 61 74 65 5f 66 75  int  (*create_fu
19ea1 6e 63 74 69 6f 6e 31 36 29 28 73 71 6c 69 74 65  nction16)(sqlite
19ea2 33 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69  3*,const void*,i
19ea3 6e 74 2c 69 6e 74 2c 76 6f 69 64 2a 2c 76 6f 69  nt,int,void*,voi
19ea4 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74  d (*xFunc)(sqlit
19ea5 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c  e3_context*,int,
19ea6 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29  sqlite3_value**)
19ea7 2c 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73  ,void (*xStep)(s
19ea8 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
19ea9 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
19eaa 65 2a 2a 29 2c 76 6f 69 64 20 28 2a 78 46 69 6e  e**),void (*xFin
19eab 61 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  al)(sqlite3_cont
19eac 65 78 74 2a 29 29 3b 0a 20 20 69 6e 74 20 28 2a  ext*));.  int (*
19ead 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 29 28 73  create_module)(s
19eae 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68  qlite3*,const ch
19eaf 61 72 2a 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65  ar*,const sqlite
19eb0 33 5f 6d 6f 64 75 6c 65 2a 2c 76 6f 69 64 2a 29  3_module*,void*)
19eb1 3b 0a 20 20 69 6e 74 20 20 28 2a 64 61 74 61 5f  ;.  int  (*data_
19eb2 63 6f 75 6e 74 29 28 73 71 6c 69 74 65 33 5f 73  count)(sqlite3_s
19eb3 74 6d 74 2a 70 53 74 6d 74 29 3b 0a 20 20 73 71  tmt*pStmt);.  sq
19eb4 6c 69 74 65 33 20 2a 20 28 2a 64 62 5f 68 61 6e  lite3 * (*db_han
19eb5 64 6c 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  dle)(sqlite3_stm
19eb6 74 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 64 65 63  t*);.  int (*dec
19eb7 6c 61 72 65 5f 76 74 61 62 29 28 73 71 6c 69 74  lare_vtab)(sqlit
19eb8 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29  e3*,const char*)
19eb9 3b 0a 20 20 69 6e 74 20 20 28 2a 65 6e 61 62 6c  ;.  int  (*enabl
19eba 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65 29 28  e_shared_cache)(
19ebb 69 6e 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a 65  int);.  int  (*e
19ebc 72 72 63 6f 64 65 29 28 73 71 6c 69 74 65 33 2a  rrcode)(sqlite3*
19ebd 64 62 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  db);.  const cha
19ebe 72 20 2a 20 28 2a 65 72 72 6d 73 67 29 28 73 71  r * (*errmsg)(sq
19ebf 6c 69 74 65 33 2a 29 3b 0a 20 20 63 6f 6e 73 74  lite3*);.  const
19ec0 20 76 6f 69 64 20 2a 20 28 2a 65 72 72 6d 73 67   void * (*errmsg
19ec1 31 36 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20  16)(sqlite3*);. 
19ec2 20 69 6e 74 20 20 28 2a 65 78 65 63 29 28 73 71   int  (*exec)(sq
19ec3 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61  lite3*,const cha
19ec4 72 2a 2c 73 71 6c 69 74 65 33 5f 63 61 6c 6c 62  r*,sqlite3_callb
19ec5 61 63 6b 2c 76 6f 69 64 2a 2c 63 68 61 72 2a 2a  ack,void*,char**
19ec6 29 3b 0a 20 20 69 6e 74 20 20 28 2a 65 78 70 69  );.  int  (*expi
19ec7 72 65 64 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  red)(sqlite3_stm
19ec8 74 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 66 69  t*);.  int  (*fi
19ec9 6e 61 6c 69 7a 65 29 28 73 71 6c 69 74 65 33 5f  nalize)(sqlite3_
19eca 73 74 6d 74 2a 70 53 74 6d 74 29 3b 0a 20 20 76  stmt*pStmt);.  v
19ecb 6f 69 64 20 20 28 2a 66 72 65 65 29 28 76 6f 69  oid  (*free)(voi
19ecc 64 2a 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 66  d*);.  void  (*f
19ecd 72 65 65 5f 74 61 62 6c 65 29 28 63 68 61 72 2a  ree_table)(char*
19ece 2a 72 65 73 75 6c 74 29 3b 0a 20 20 69 6e 74 20  *result);.  int 
19ecf 20 28 2a 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69   (*get_autocommi
19ed0 74 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20  t)(sqlite3*);.  
19ed1 76 6f 69 64 20 2a 20 28 2a 67 65 74 5f 61 75 78  void * (*get_aux
19ed2 64 61 74 61 29 28 73 71 6c 69 74 65 33 5f 63 6f  data)(sqlite3_co
19ed3 6e 74 65 78 74 2a 2c 69 6e 74 29 3b 0a 20 20 69  ntext*,int);.  i
19ed4 6e 74 20 20 28 2a 67 65 74 5f 74 61 62 6c 65 29  nt  (*get_table)
19ed5 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
19ed6 63 68 61 72 2a 2c 63 68 61 72 2a 2a 2a 2c 69 6e  char*,char***,in
19ed7 74 2a 2c 69 6e 74 2a 2c 63 68 61 72 2a 2a 29 3b  t*,int*,char**);
19ed8 0a 20 20 69 6e 74 20 20 28 2a 67 6c 6f 62 61 6c  .  int  (*global
19ed9 5f 72 65 63 6f 76 65 72 29 28 76 6f 69 64 29 3b  _recover)(void);
19eda 0a 20 20 76 6f 69 64 20 20 28 2a 69 6e 74 65 72  .  void  (*inter
19edb 72 75 70 74 78 29 28 73 71 6c 69 74 65 33 2a 29  ruptx)(sqlite3*)
19edc 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  ;.  sqlite_int64
19edd 20 20 28 2a 6c 61 73 74 5f 69 6e 73 65 72 74 5f    (*last_insert_
19ede 72 6f 77 69 64 29 28 73 71 6c 69 74 65 33 2a 29  rowid)(sqlite3*)
19edf 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
19ee0 20 28 2a 6c 69 62 76 65 72 73 69 6f 6e 29 28 76   (*libversion)(v
19ee1 6f 69 64 29 3b 0a 20 20 69 6e 74 20 20 28 2a 6c  oid);.  int  (*l
19ee2 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72  ibversion_number
19ee3 29 28 76 6f 69 64 29 3b 0a 20 20 76 6f 69 64 20  )(void);.  void 
19ee4 2a 28 2a 6d 61 6c 6c 6f 63 29 28 69 6e 74 29 3b  *(*malloc)(int);
19ee5 0a 20 20 63 68 61 72 20 2a 20 28 2a 6d 70 72 69  .  char * (*mpri
19ee6 6e 74 66 29 28 63 6f 6e 73 74 20 63 68 61 72 2a  ntf)(const char*
19ee7 2c 2e 2e 2e 29 3b 0a 20 20 69 6e 74 20 20 28 2a  ,...);.  int  (*
19ee8 6f 70 65 6e 29 28 63 6f 6e 73 74 20 63 68 61 72  open)(const char
19ee9 2a 2c 73 71 6c 69 74 65 33 2a 2a 29 3b 0a 20 20  *,sqlite3**);.  
19eea 69 6e 74 20 20 28 2a 6f 70 65 6e 31 36 29 28 63  int  (*open16)(c
19eeb 6f 6e 73 74 20 76 6f 69 64 2a 2c 73 71 6c 69 74  onst void*,sqlit
19eec 65 33 2a 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a  e3**);.  int  (*
19eed 70 72 65 70 61 72 65 29 28 73 71 6c 69 74 65 33  prepare)(sqlite3
19eee 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e  *,const char*,in
19eef 74 2c 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2a  t,sqlite3_stmt**
19ef0 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2a 29 3b 0a  ,const char**);.
19ef1 20 20 69 6e 74 20 20 28 2a 70 72 65 70 61 72 65    int  (*prepare
19ef2 31 36 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e  16)(sqlite3*,con
19ef3 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 73 71 6c  st void*,int,sql
19ef4 69 74 65 33 5f 73 74 6d 74 2a 2a 2c 63 6f 6e 73  ite3_stmt**,cons
19ef5 74 20 76 6f 69 64 2a 2a 29 3b 0a 20 20 76 6f 69  t void**);.  voi
19ef6 64 20 2a 20 28 2a 70 72 6f 66 69 6c 65 29 28 73  d * (*profile)(s
19ef7 71 6c 69 74 65 33 2a 2c 76 6f 69 64 28 2a 29 28  qlite3*,void(*)(
19ef8 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72  void*,const char
19ef9 2a 2c 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29  *,sqlite_uint64)
19efa 2c 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20  ,void*);.  void 
19efb 20 28 2a 70 72 6f 67 72 65 73 73 5f 68 61 6e 64   (*progress_hand
19efc 6c 65 72 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e  ler)(sqlite3*,in
19efd 74 2c 69 6e 74 28 2a 29 28 76 6f 69 64 2a 29 2c  t,int(*)(void*),
19efe 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 2a  void*);.  void *
19eff 28 2a 72 65 61 6c 6c 6f 63 29 28 76 6f 69 64 2a  (*realloc)(void*
19f00 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a  ,int);.  int  (*
19f01 72 65 73 65 74 29 28 73 71 6c 69 74 65 33 5f 73  reset)(sqlite3_s
19f02 74 6d 74 2a 70 53 74 6d 74 29 3b 0a 20 20 76 6f  tmt*pStmt);.  vo
19f03 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 62 6c 6f  id  (*result_blo
19f04 62 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  b)(sqlite3_conte
19f05 78 74 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  xt*,const void*,
19f06 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64  int,void(*)(void
19f07 2a 29 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72  *));.  void  (*r
19f08 65 73 75 6c 74 5f 64 6f 75 62 6c 65 29 28 73 71  esult_double)(sq
19f09 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 64  lite3_context*,d
19f0a 6f 75 62 6c 65 29 3b 0a 20 20 76 6f 69 64 20 20  ouble);.  void  
19f0b 28 2a 72 65 73 75 6c 74 5f 65 72 72 6f 72 29 28  (*result_error)(
19f0c 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
19f0d 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74  ,const char*,int
19f0e 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73  );.  void  (*res
19f0f 75 6c 74 5f 65 72 72 6f 72 31 36 29 28 73 71 6c  ult_error16)(sql
19f10 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f  ite3_context*,co
19f11 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 0a  nst void*,int);.
19f12 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74    void  (*result
19f13 5f 69 6e 74 29 28 73 71 6c 69 74 65 33 5f 63 6f  _int)(sqlite3_co
19f14 6e 74 65 78 74 2a 2c 69 6e 74 29 3b 0a 20 20 76  ntext*,int);.  v
19f15 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 69 6e  oid  (*result_in
19f16 74 36 34 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  t64)(sqlite3_con
19f17 74 65 78 74 2a 2c 73 71 6c 69 74 65 5f 69 6e 74  text*,sqlite_int
19f18 36 34 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72  64);.  void  (*r
19f19 65 73 75 6c 74 5f 6e 75 6c 6c 29 28 73 71 6c 69  esult_null)(sqli
19f1a 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 20  te3_context*);. 
19f1b 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f   void  (*result_
19f1c 74 65 78 74 29 28 73 71 6c 69 74 65 33 5f 63 6f  text)(sqlite3_co
19f1d 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20 63 68 61  ntext*,const cha
19f1e 72 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76  r*,int,void(*)(v
19f1f 6f 69 64 2a 29 29 3b 0a 20 20 76 6f 69 64 20 20  oid*));.  void  
19f20 28 2a 72 65 73 75 6c 74 5f 74 65 78 74 31 36 29  (*result_text16)
19f21 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
19f22 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  *,const void*,in
19f23 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29  t,void(*)(void*)
19f24 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73  );.  void  (*res
19f25 75 6c 74 5f 74 65 78 74 31 36 62 65 29 28 73 71  ult_text16be)(sq
19f26 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63  lite3_context*,c
19f27 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 76  onst void*,int,v
19f28 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
19f29 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74    void  (*result
19f2a 5f 74 65 78 74 31 36 6c 65 29 28 73 71 6c 69 74  _text16le)(sqlit
19f2b 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73  e3_context*,cons
19f2c 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69 64  t void*,int,void
19f2d 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 76  (*)(void*));.  v
19f2e 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 76 61  oid  (*result_va
19f2f 6c 75 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  lue)(sqlite3_con
19f30 74 65 78 74 2a 2c 73 71 6c 69 74 65 33 5f 76 61  text*,sqlite3_va
19f31 6c 75 65 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 20  lue*);.  void * 
19f32 28 2a 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 29  (*rollback_hook)
19f33 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64 28 2a  (sqlite3*,void(*
19f34 29 28 76 6f 69 64 2a 29 2c 76 6f 69 64 2a 29 3b  )(void*),void*);
19f35 0a 20 20 69 6e 74 20 20 28 2a 73 65 74 5f 61 75  .  int  (*set_au
19f36 74 68 6f 72 69 7a 65 72 29 28 73 71 6c 69 74 65  thorizer)(sqlite
19f37 33 2a 2c 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c  3*,int(*)(void*,
19f38 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  int,const char*,
19f39 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73  const char*,cons
19f3a 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68  t char*,const ch
19f3b 61 72 2a 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 76  ar*),void*);.  v
19f3c 6f 69 64 20 20 28 2a 73 65 74 5f 61 75 78 64 61  oid  (*set_auxda
19f3d 74 61 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ta)(sqlite3_cont
19f3e 65 78 74 2a 2c 69 6e 74 2c 76 6f 69 64 2a 2c 76  ext*,int,void*,v
19f3f 6f 69 64 20 28 2a 29 28 76 6f 69 64 2a 29 29 3b  oid (*)(void*));
19f40 0a 20 20 63 68 61 72 20 2a 20 28 2a 73 6e 70 72  .  char * (*snpr
19f41 69 6e 74 66 29 28 69 6e 74 2c 63 68 61 72 2a 2c  intf)(int,char*,
19f42 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29  const char*,...)
19f43 3b 0a 20 20 69 6e 74 20 20 28 2a 73 74 65 70 29  ;.  int  (*step)
19f44 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b  (sqlite3_stmt*);
19f45 0a 20 20 69 6e 74 20 20 28 2a 74 61 62 6c 65 5f  .  int  (*table_
19f46 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61 29  column_metadata)
19f47 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
19f48 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72  char*,const char
19f49 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 68  *,const char*,ch
19f4a 61 72 20 63 6f 6e 73 74 2a 2a 2c 63 68 61 72 20  ar const**,char 
19f4b 63 6f 6e 73 74 2a 2a 2c 69 6e 74 2a 2c 69 6e 74  const**,int*,int
19f4c 2a 2c 69 6e 74 2a 29 3b 0a 20 20 76 6f 69 64 20  *,int*);.  void 
19f4d 20 28 2a 74 68 72 65 61 64 5f 63 6c 65 61 6e 75   (*thread_cleanu
19f4e 70 29 28 76 6f 69 64 29 3b 0a 20 20 69 6e 74 20  p)(void);.  int 
19f4f 20 28 2a 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73   (*total_changes
19f50 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 76  )(sqlite3*);.  v
19f51 6f 69 64 20 2a 20 28 2a 74 72 61 63 65 29 28 73  oid * (*trace)(s
19f52 71 6c 69 74 65 33 2a 2c 76 6f 69 64 28 2a 78 54  qlite3*,void(*xT
19f53 72 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73  race)(void*,cons
19f54 74 20 63 68 61 72 2a 29 2c 76 6f 69 64 2a 29 3b  t char*),void*);
19f55 0a 20 20 69 6e 74 20 20 28 2a 74 72 61 6e 73 66  .  int  (*transf
19f56 65 72 5f 62 69 6e 64 69 6e 67 73 29 28 73 71 6c  er_bindings)(sql
19f57 69 74 65 33 5f 73 74 6d 74 2a 2c 73 71 6c 69 74  ite3_stmt*,sqlit
19f58 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20 76 6f 69  e3_stmt*);.  voi
19f59 64 20 2a 20 28 2a 75 70 64 61 74 65 5f 68 6f 6f  d * (*update_hoo
19f5a 6b 29 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64  k)(sqlite3*,void
19f5b 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 20 2c 63  (*)(void*,int ,c
19f5c 68 61 72 20 63 6f 6e 73 74 2a 2c 63 68 61 72 20  har const*,char 
19f5d 63 6f 6e 73 74 2a 2c 73 71 6c 69 74 65 5f 69 6e  const*,sqlite_in
19f5e 74 36 34 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 76  t64),void*);.  v
19f5f 6f 69 64 20 2a 20 28 2a 75 73 65 72 5f 64 61 74  oid * (*user_dat
19f60 61 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  a)(sqlite3_conte
19f61 78 74 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f  xt*);.  const vo
19f62 69 64 20 2a 20 28 2a 76 61 6c 75 65 5f 62 6c 6f  id * (*value_blo
19f63 62 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  b)(sqlite3_value
19f64 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 76 61 6c  *);.  int  (*val
19f65 75 65 5f 62 79 74 65 73 29 28 73 71 6c 69 74 65  ue_bytes)(sqlite
19f66 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 69 6e 74  3_value*);.  int
19f67 20 20 28 2a 76 61 6c 75 65 5f 62 79 74 65 73 31    (*value_bytes1
19f68 36 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  6)(sqlite3_value
19f69 2a 29 3b 0a 20 20 64 6f 75 62 6c 65 20 20 28 2a  *);.  double  (*
19f6a 76 61 6c 75 65 5f 64 6f 75 62 6c 65 29 28 73 71  value_double)(sq
19f6b 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20  lite3_value*);. 
19f6c 20 69 6e 74 20 20 28 2a 76 61 6c 75 65 5f 69 6e   int  (*value_in
19f6d 74 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  t)(sqlite3_value
19f6e 2a 29 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74  *);.  sqlite_int
19f6f 36 34 20 20 28 2a 76 61 6c 75 65 5f 69 6e 74 36  64  (*value_int6
19f70 34 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  4)(sqlite3_value
19f71 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 76 61 6c  *);.  int  (*val
19f72 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 29  ue_numeric_type)
19f73 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
19f74 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  ;.  const unsign
19f75 65 64 20 63 68 61 72 20 2a 20 28 2a 76 61 6c 75  ed char * (*valu
19f76 65 5f 74 65 78 74 29 28 73 71 6c 69 74 65 33 5f  e_text)(sqlite3_
19f77 76 61 6c 75 65 2a 29 3b 0a 20 20 63 6f 6e 73 74  value*);.  const
19f78 20 76 6f 69 64 20 2a 20 28 2a 76 61 6c 75 65 5f   void * (*value_
19f79 74 65 78 74 31 36 29 28 73 71 6c 69 74 65 33 5f  text16)(sqlite3_
19f7a 76 61 6c 75 65 2a 29 3b 0a 20 20 63 6f 6e 73 74  value*);.  const
19f7b 20 76 6f 69 64 20 2a 20 28 2a 76 61 6c 75 65 5f   void * (*value_
19f7c 74 65 78 74 31 36 62 65 29 28 73 71 6c 69 74 65  text16be)(sqlite
19f7d 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 63 6f 6e  3_value*);.  con
19f7e 73 74 20 76 6f 69 64 20 2a 20 28 2a 76 61 6c 75  st void * (*valu
19f7f 65 5f 74 65 78 74 31 36 6c 65 29 28 73 71 6c 69  e_text16le)(sqli
19f80 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 69  te3_value*);.  i
19f81 6e 74 20 20 28 2a 76 61 6c 75 65 5f 74 79 70 65  nt  (*value_type
19f82 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  )(sqlite3_value*
19f83 29 3b 0a 20 20 63 68 61 72 20 2a 28 2a 76 6d 70  );.  char *(*vmp
19f84 72 69 6e 74 66 29 28 63 6f 6e 73 74 20 63 68 61  rintf)(const cha
19f85 72 2a 2c 76 61 5f 6c 69 73 74 29 3b 0a 20 20 69  r*,va_list);.  i
19f86 6e 74 20 28 2a 6f 76 65 72 6c 6f 61 64 5f 66 75  nt (*overload_fu
19f87 6e 63 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 2a  nction)(sqlite3*
19f88 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  , const char *zF
19f89 75 6e 63 4e 61 6d 65 2c 20 69 6e 74 20 6e 41 72  uncName, int nAr
19f8a 67 29 3b 0a 20 20 69 6e 74 20 28 2a 70 72 65 70  g);.  int (*prep
19f8b 61 72 65 5f 76 32 29 28 73 71 6c 69 74 65 33 2a  are_v2)(sqlite3*
19f8c 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74  ,const char*,int
19f8d 2c 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2a 2c  ,sqlite3_stmt**,
19f8e 63 6f 6e 73 74 20 63 68 61 72 2a 2a 29 3b 0a 20  const char**);. 
19f8f 20 69 6e 74 20 28 2a 70 72 65 70 61 72 65 31 36   int (*prepare16
19f90 5f 76 32 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f  _v2)(sqlite3*,co
19f91 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 73 71  nst void*,int,sq
19f92 6c 69 74 65 33 5f 73 74 6d 74 2a 2a 2c 63 6f 6e  lite3_stmt**,con
19f93 73 74 20 76 6f 69 64 2a 2a 29 3b 0a 20 20 69 6e  st void**);.  in
19f94 74 20 28 2a 63 6c 65 61 72 5f 62 69 6e 64 69 6e  t (*clear_bindin
19f95 67 73 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  gs)(sqlite3_stmt
19f96 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 63 72 65 61  *);.  int (*crea
19f97 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 29 28 73 71  te_module_v2)(sq
19f98 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61  lite3*,const cha
19f99 72 2a 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  r*,const sqlite3
19f9a 5f 6d 6f 64 75 6c 65 2a 2c 76 6f 69 64 2a 2c 76  _module*,void*,v
19f9b 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79 29 28  oid (*xDestroy)(
19f9c 76 6f 69 64 20 2a 29 29 3b 0a 7d 3b 0a 0a 2f 2a  void *));.};../*
19f9d 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
19f9e 67 20 6d 61 63 72 6f 73 20 72 65 64 65 66 69 6e  g macros redefin
19f9f 65 20 74 68 65 20 41 50 49 20 72 6f 75 74 69 6e  e the API routin
19fa0 65 73 20 73 6f 20 74 68 61 74 20 74 68 65 79 20  es so that they 
19fa1 61 72 65 0a 2a 2a 20 72 65 64 69 72 65 63 74 65  are.** redirecte
19fa2 64 20 74 68 72 6f 75 67 68 74 20 74 68 65 20 67  d throught the g
19fa3 6c 6f 62 61 6c 20 73 71 6c 69 74 65 33 5f 61 70  lobal sqlite3_ap
19fa4 69 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a  i structure..**.
19fa5 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66  ** This header f
19fa6 69 6c 65 20 69 73 20 61 6c 73 6f 20 75 73 65 64  ile is also used
19fa7 20 62 79 20 74 68 65 20 6c 6f 61 64 65 78 74 2e   by the loadext.
19fa8 63 20 73 6f 75 72 63 65 20 66 69 6c 65 0a 2a 2a  c source file.**
19fa9 20 28 70 61 72 74 20 6f 66 20 74 68 65 20 6d 61   (part of the ma
19faa 69 6e 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72  in SQLite librar
19fab 79 20 2d 20 6e 6f 74 20 61 6e 20 65 78 74 65 6e  y - not an exten
19fac 73 69 6f 6e 29 20 73 6f 20 74 68 61 74 0a 2a 2a  sion) so that.**
19fad 20 69 74 20 63 61 6e 20 67 65 74 20 61 63 63 65   it can get acce
19fae 73 73 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65  ss to the sqlite
19faf 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20 73  3_api_routines s
19fb0 74 72 75 63 74 75 72 65 0a 2a 2a 20 64 65 66 69  tructure.** defi
19fb1 6e 69 74 69 6f 6e 2e 20 20 42 75 74 20 74 68 65  nition.  But the
19fb2 20 6d 61 69 6e 20 6c 69 62 72 61 72 79 20 64 6f   main library do
19fb3 65 73 20 6e 6f 74 20 77 61 6e 74 20 74 6f 20 72  es not want to r
19fb4 65 64 65 66 69 6e 65 0a 2a 2a 20 74 68 65 20 41  edefine.** the A
19fb5 50 49 2e 20 20 53 6f 20 74 68 65 20 72 65 64 65  PI.  So the rede
19fb6 66 69 6e 69 74 69 6f 6e 20 6d 61 63 72 6f 73 20  finition macros 
19fb7 61 72 65 20 6f 6e 6c 79 20 76 61 6c 69 64 20 69  are only valid i
19fb8 66 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f  f the.** SQLITE_
19fb9 43 4f 52 45 20 6d 61 63 72 6f 73 20 69 73 20 75  CORE macros is u
19fba 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66  ndefined..*/.#if
19fbb 6e 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 52 45  ndef SQLITE_CORE
19fbc 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
19fbd 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65  _aggregate_conte
19fbe 78 74 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  xt      sqlite3_
19fbf 61 70 69 2d 3e 61 67 67 72 65 67 61 74 65 5f 63  api->aggregate_c
19fc0 6f 6e 74 65 78 74 0a 23 64 65 66 69 6e 65 20 73  ontext.#define s
19fc1 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65  qlite3_aggregate
19fc2 5f 63 6f 75 6e 74 20 20 20 20 20 20 20 20 73 71  _count        sq
19fc3 6c 69 74 65 33 5f 61 70 69 2d 3e 61 67 67 72 65  lite3_api->aggre
19fc4 67 61 74 65 5f 63 6f 75 6e 74 0a 23 64 65 66 69  gate_count.#defi
19fc5 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  ne sqlite3_bind_
19fc6 62 6c 6f 62 20 20 20 20 20 20 20 20 20 20 20 20  blob            
19fc7 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
19fc8 69 6e 64 5f 62 6c 6f 62 0a 23 64 65 66 69 6e 65  ind_blob.#define
19fc9 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64 6f   sqlite3_bind_do
19fca 75 62 6c 65 20 20 20 20 20 20 20 20 20 20 20 20  uble            
19fcb 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e  sqlite3_api->bin
19fcc 64 5f 64 6f 75 62 6c 65 0a 23 64 65 66 69 6e 65  d_double.#define
19fcd 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
19fce 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
19fcf 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e  sqlite3_api->bin
19fd0 64 5f 69 6e 74 0a 23 64 65 66 69 6e 65 20 73 71  d_int.#define sq
19fd1 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
19fd2 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
19fd3 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 69  ite3_api->bind_i
19fd4 6e 74 36 34 0a 23 64 65 66 69 6e 65 20 73 71 6c  nt64.#define sql
19fd5 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 20 20  ite3_bind_null  
19fd6 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
19fd7 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 6e 75  te3_api->bind_nu
19fd8 6c 6c 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ll.#define sqlit
19fd9 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65  e3_bind_paramete
19fda 72 5f 63 6f 75 6e 74 20 20 20 73 71 6c 69 74 65  r_count   sqlite
19fdb 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 70 61 72 61  3_api->bind_para
19fdc 6d 65 74 65 72 5f 63 6f 75 6e 74 0a 23 64 65 66  meter_count.#def
19fdd 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ine sqlite3_bind
19fde 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78  _parameter_index
19fdf 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
19fe0 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69  bind_parameter_i
19fe1 6e 64 65 78 0a 23 64 65 66 69 6e 65 20 73 71 6c  ndex.#define sql
19fe2 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
19fe3 74 65 72 5f 6e 61 6d 65 20 20 20 20 73 71 6c 69  ter_name    sqli
19fe4 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 70 61  te3_api->bind_pa
19fe5 72 61 6d 65 74 65 72 5f 6e 61 6d 65 0a 23 64 65  rameter_name.#de
19fe6 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e  fine sqlite3_bin
19fe7 64 5f 74 65 78 74 20 20 20 20 20 20 20 20 20 20  d_text          
19fe8 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
19fe9 3e 62 69 6e 64 5f 74 65 78 74 0a 23 64 65 66 69  >bind_text.#defi
19fea 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  ne sqlite3_bind_
19feb 74 65 78 74 31 36 20 20 20 20 20 20 20 20 20 20  text16          
19fec 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
19fed 69 6e 64 5f 74 65 78 74 31 36 0a 23 64 65 66 69  ind_text16.#defi
19fee 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  ne sqlite3_bind_
19fef 76 61 6c 75 65 20 20 20 20 20 20 20 20 20 20 20  value           
19ff0 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
19ff1 69 6e 64 5f 76 61 6c 75 65 0a 23 64 65 66 69 6e  ind_value.#defin
19ff2 65 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68  e sqlite3_busy_h
19ff3 61 6e 64 6c 65 72 20 20 20 20 20 20 20 20 20 20  andler          
19ff4 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 75   sqlite3_api->bu
19ff5 73 79 5f 68 61 6e 64 6c 65 72 0a 23 64 65 66 69  sy_handler.#defi
19ff6 6e 65 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f  ne sqlite3_busy_
19ff7 74 69 6d 65 6f 75 74 20 20 20 20 20 20 20 20 20  timeout         
19ff8 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
19ff9 75 73 79 5f 74 69 6d 65 6f 75 74 0a 23 64 65 66  usy_timeout.#def
19ffa 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 68 61 6e  ine sqlite3_chan
19ffb 67 65 73 20 20 20 20 20 20 20 20 20 20 20 20 20  ges             
19ffc 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
19ffd 63 68 61 6e 67 65 73 0a 23 64 65 66 69 6e 65 20  changes.#define 
19ffe 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 20 20 20  sqlite3_close   
19fff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
1a000 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6c 6f 73  qlite3_api->clos
1a001 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
1a002 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64  3_collation_need
1a003 65 64 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  ed       sqlite3
1a004 5f 61 70 69 2d 3e 63 6f 6c 6c 61 74 69 6f 6e 5f  _api->collation_
1a005 6e 65 65 64 65 64 0a 23 64 65 66 69 6e 65 20 73  needed.#define s
1a006 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e  qlite3_collation
1a007 5f 6e 65 65 64 65 64 31 36 20 20 20 20 20 73 71  _needed16     sq
1a008 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 6c 61  lite3_api->colla
1a009 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 0a 23 64  tion_needed16.#d
1a00a 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
1a00b 6c 75 6d 6e 5f 62 6c 6f 62 20 20 20 20 20 20 20  lumn_blob       
1a00c 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
1a00d 2d 3e 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 0a 23 64  ->column_blob.#d
1a00e 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
1a00f 6c 75 6d 6e 5f 62 79 74 65 73 20 20 20 20 20 20  lumn_bytes      
1a010 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
1a011 2d 3e 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 0a 23  ->column_bytes.#
1a012 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
1a013 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 20 20 20  olumn_bytes16   
1a014 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
1a015 69 2d 3e 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31  i->column_bytes1
1a016 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  6.#define sqlite
1a017 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 20 20  3_column_count  
1a018 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1a019 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 63 6f 75  _api->column_cou
1a01a 6e 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  nt.#define sqlit
1a01b 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61  e3_column_databa
1a01c 73 65 5f 6e 61 6d 65 20 20 20 73 71 6c 69 74 65  se_name   sqlite
1a01d 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64 61  3_api->column_da
1a01e 74 61 62 61 73 65 5f 6e 61 6d 65 0a 23 64 65 66  tabase_name.#def
1a01f 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
1a020 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65  mn_database_name
1a021 31 36 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e  16 sqlite3_api->
1a022 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f  column_database_
1a023 6e 61 6d 65 31 36 0a 23 64 65 66 69 6e 65 20 73  name16.#define s
1a024 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65  qlite3_column_de
1a025 63 6c 74 79 70 65 20 20 20 20 20 20 20 20 73 71  cltype        sq
1a026 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d  lite3_api->colum
1a027 6e 5f 64 65 63 6c 74 79 70 65 0a 23 64 65 66 69  n_decltype.#defi
1a028 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
1a029 6e 5f 64 65 63 6c 74 79 70 65 31 36 20 20 20 20  n_decltype16    
1a02a 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
1a02b 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36  olumn_decltype16
1a02c 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
1a02d 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 20 20  _column_double  
1a02e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
1a02f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64 6f 75 62  api->column_doub
1a030 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  le.#define sqlit
1a031 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 20 20 20  e3_column_int   
1a032 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1a033 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 69 6e  3_api->column_in
1a034 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  t.#define sqlite
1a035 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 20 20  3_column_int64  
1a036 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1a037 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 69 6e 74  _api->column_int
1a038 36 34 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  64.#define sqlit
1a039 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 20 20  e3_column_name  
1a03a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1a03b 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 6e 61  3_api->column_na
1a03c 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  me.#define sqlit
1a03d 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36  e3_column_name16
1a03e 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1a03f 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 6e 61  3_api->column_na
1a040 6d 65 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c  me16.#define sql
1a041 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67  ite3_column_orig
1a042 69 6e 5f 6e 61 6d 65 20 20 20 20 20 73 71 6c 69  in_name     sqli
1a043 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f  te3_api->column_
1a044 6f 72 69 67 69 6e 5f 6e 61 6d 65 0a 23 64 65 66  origin_name.#def
1a045 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
1a046 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36  mn_origin_name16
1a047 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1a048 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61  column_origin_na
1a049 6d 65 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c  me16.#define sql
1a04a 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c  ite3_column_tabl
1a04b 65 5f 6e 61 6d 65 20 20 20 20 20 20 73 71 6c 69  e_name      sqli
1a04c 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f  te3_api->column_
1a04d 74 61 62 6c 65 5f 6e 61 6d 65 0a 23 64 65 66 69  table_name.#defi
1a04e 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
1a04f 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 20 20  n_table_name16  
1a050 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
1a051 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65  olumn_table_name
1a052 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  16.#define sqlit
1a053 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 20 20  e3_column_text  
1a054 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1a055 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 74 65  3_api->column_te
1a056 78 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  xt.#define sqlit
1a057 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36  e3_column_text16
1a058 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1a059 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 74 65  3_api->column_te
1a05a 78 74 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c  xt16.#define sql
1a05b 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65  ite3_column_type
1a05c 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
1a05d 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f  te3_api->column_
1a05e 74 79 70 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  type.#define sql
1a05f 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75  ite3_column_valu
1a060 65 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  e           sqli
1a061 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f  te3_api->column_
1a062 76 61 6c 75 65 0a 23 64 65 66 69 6e 65 20 73 71  value.#define sq
1a063 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f  lite3_commit_hoo
1a064 6b 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  k            sql
1a065 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6d 6d 69 74  ite3_api->commit
1a066 5f 68 6f 6f 6b 0a 23 64 65 66 69 6e 65 20 73 71  _hook.#define sq
1a067 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 20 20  lite3_complete  
1a068 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
1a069 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6d 70 6c 65  ite3_api->comple
1a06a 74 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  te.#define sqlit
1a06b 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 20 20 20  e3_complete16   
1a06c 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1a06d 33 5f 61 70 69 2d 3e 63 6f 6d 70 6c 65 74 65 31  3_api->complete1
1a06e 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  6.#define sqlite
1a06f 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69  3_create_collati
1a070 6f 6e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  on       sqlite3
1a071 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f 63 6f 6c  _api->create_col
1a072 6c 61 74 69 6f 6e 0a 23 64 65 66 69 6e 65 20 73  lation.#define s
1a073 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
1a074 6c 6c 61 74 69 6f 6e 31 36 20 20 20 20 20 73 71  llation16     sq
1a075 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72 65 61 74  lite3_api->creat
1a076 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 0a 23 64  e_collation16.#d
1a077 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 72  efine sqlite3_cr
1a078 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 20 20  eate_function   
1a079 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
1a07a 2d 3e 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  ->create_functio
1a07b 6e 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  n.#define sqlite
1a07c 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
1a07d 6e 31 36 20 20 20 20 20 20 73 71 6c 69 74 65 33  n16      sqlite3
1a07e 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f 66 75 6e  _api->create_fun
1a07f 63 74 69 6f 6e 31 36 0a 23 64 65 66 69 6e 65 20  ction16.#define 
1a080 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d  sqlite3_create_m
1a081 6f 64 75 6c 65 20 20 20 20 20 20 20 20 20 20 73  odule          s
1a082 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72 65 61  qlite3_api->crea
1a083 74 65 5f 6d 6f 64 75 6c 65 0a 23 64 65 66 69 6e  te_module.#defin
1a084 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  e sqlite3_create
1a085 5f 6d 6f 64 75 6c 65 5f 76 32 20 20 20 20 20 20  _module_v2      
1a086 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72   sqlite3_api->cr
1a087 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 0a 23  eate_module_v2.#
1a088 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 64  define sqlite3_d
1a089 61 74 61 5f 63 6f 75 6e 74 20 20 20 20 20 20 20  ata_count       
1a08a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
1a08b 69 2d 3e 64 61 74 61 5f 63 6f 75 6e 74 0a 23 64  i->data_count.#d
1a08c 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 64 62  efine sqlite3_db
1a08d 5f 68 61 6e 64 6c 65 20 20 20 20 20 20 20 20 20  _handle         
1a08e 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
1a08f 2d 3e 64 62 5f 68 61 6e 64 6c 65 0a 23 64 65 66  ->db_handle.#def
1a090 69 6e 65 20 73 71 6c 69 74 65 33 5f 64 65 63 6c  ine sqlite3_decl
1a091 61 72 65 5f 76 74 61 62 20 20 20 20 20 20 20 20  are_vtab        
1a092 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1a093 64 65 63 6c 61 72 65 5f 76 74 61 62 0a 23 64 65  declare_vtab.#de
1a094 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 6e 61  fine sqlite3_ena
1a095 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65  ble_shared_cache
1a096 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
1a097 3e 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63  >enable_shared_c
1a098 61 63 68 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  ache.#define sql
1a099 69 74 65 33 5f 65 72 72 63 6f 64 65 20 20 20 20  ite3_errcode    
1a09a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
1a09b 74 65 33 5f 61 70 69 2d 3e 65 72 72 63 6f 64 65  te3_api->errcode
1a09c 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
1a09d 5f 65 72 72 6d 73 67 20 20 20 20 20 20 20 20 20  _errmsg         
1a09e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
1a09f 61 70 69 2d 3e 65 72 72 6d 73 67 0a 23 64 65 66  api->errmsg.#def
1a0a0 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 72 72 6d  ine sqlite3_errm
1a0a1 73 67 31 36 20 20 20 20 20 20 20 20 20 20 20 20  sg16            
1a0a2 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1a0a3 65 72 72 6d 73 67 31 36 0a 23 64 65 66 69 6e 65  errmsg16.#define
1a0a4 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20 20 20   sqlite3_exec   
1a0a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a0a6 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65 78 65  sqlite3_api->exe
1a0a7 63 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  c.#define sqlite
1a0a8 33 5f 65 78 70 69 72 65 64 20 20 20 20 20 20 20  3_expired       
1a0a9 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1a0aa 5f 61 70 69 2d 3e 65 78 70 69 72 65 64 0a 23 64  _api->expired.#d
1a0ab 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 66 69  efine sqlite3_fi
1a0ac 6e 61 6c 69 7a 65 20 20 20 20 20 20 20 20 20 20  nalize          
1a0ad 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
1a0ae 2d 3e 66 69 6e 61 6c 69 7a 65 0a 23 64 65 66 69  ->finalize.#defi
1a0af 6e 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65 20  ne sqlite3_free 
1a0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a0b1 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 66    sqlite3_api->f
1a0b2 72 65 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ree.#define sqli
1a0b3 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 20 20  te3_free_table  
1a0b4 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1a0b5 65 33 5f 61 70 69 2d 3e 66 72 65 65 5f 74 61 62  e3_api->free_tab
1a0b6 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  le.#define sqlit
1a0b7 65 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69  e3_get_autocommi
1a0b8 74 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  t         sqlite
1a0b9 33 5f 61 70 69 2d 3e 67 65 74 5f 61 75 74 6f 63  3_api->get_autoc
1a0ba 6f 6d 6d 69 74 0a 23 64 65 66 69 6e 65 20 73 71  ommit.#define sq
1a0bb 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74  lite3_get_auxdat
1a0bc 61 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  a            sql
1a0bd 69 74 65 33 5f 61 70 69 2d 3e 67 65 74 5f 61 75  ite3_api->get_au
1a0be 78 64 61 74 61 0a 23 64 65 66 69 6e 65 20 73 71  xdata.#define sq
1a0bf 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 20  lite3_get_table 
1a0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
1a0c1 69 74 65 33 5f 61 70 69 2d 3e 67 65 74 5f 74 61  ite3_api->get_ta
1a0c2 62 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ble.#define sqli
1a0c3 74 65 33 5f 67 6c 6f 62 61 6c 5f 72 65 63 6f 76  te3_global_recov
1a0c4 65 72 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  er         sqlit
1a0c5 65 33 5f 61 70 69 2d 3e 67 6c 6f 62 61 6c 5f 72  e3_api->global_r
1a0c6 65 63 6f 76 65 72 0a 23 64 65 66 69 6e 65 20 73  ecover.#define s
1a0c7 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
1a0c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
1a0c9 6c 69 74 65 33 5f 61 70 69 2d 3e 69 6e 74 65 72  lite3_api->inter
1a0ca 72 75 70 74 78 0a 23 64 65 66 69 6e 65 20 73 71  ruptx.#define sq
1a0cb 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72  lite3_last_inser
1a0cc 74 5f 72 6f 77 69 64 20 20 20 20 20 20 73 71 6c  t_rowid      sql
1a0cd 69 74 65 33 5f 61 70 69 2d 3e 6c 61 73 74 5f 69  ite3_api->last_i
1a0ce 6e 73 65 72 74 5f 72 6f 77 69 64 0a 23 64 65 66  nsert_rowid.#def
1a0cf 69 6e 65 20 73 71 6c 69 74 65 33 5f 6c 69 62 76  ine sqlite3_libv
1a0d0 65 72 73 69 6f 6e 20 20 20 20 20 20 20 20 20 20  ersion          
1a0d1 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1a0d2 6c 69 62 76 65 72 73 69 6f 6e 0a 23 64 65 66 69  libversion.#defi
1a0d3 6e 65 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65  ne sqlite3_libve
1a0d4 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 20 20 20 20  rsion_number    
1a0d5 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6c    sqlite3_api->l
1a0d6 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72  ibversion_number
1a0d7 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
1a0d8 5f 6d 61 6c 6c 6f 63 20 20 20 20 20 20 20 20 20  _malloc         
1a0d9 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
1a0da 61 70 69 2d 3e 6d 61 6c 6c 6f 63 0a 23 64 65 66  api->malloc.#def
1a0db 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  ine sqlite3_mpri
1a0dc 6e 74 66 20 20 20 20 20 20 20 20 20 20 20 20 20  ntf             
1a0dd 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1a0de 6d 70 72 69 6e 74 66 0a 23 64 65 66 69 6e 65 20  mprintf.#define 
1a0df 73 71 6c 69 74 65 33 5f 6f 70 65 6e 20 20 20 20  sqlite3_open    
1a0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
1a0e1 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6f 70 65 6e  qlite3_api->open
1a0e2 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
1a0e3 5f 6f 70 65 6e 31 36 20 20 20 20 20 20 20 20 20  _open16         
1a0e4 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
1a0e5 61 70 69 2d 3e 6f 70 65 6e 31 36 0a 23 64 65 66  api->open16.#def
1a0e6 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70  ine sqlite3_prep
1a0e7 61 72 65 20 20 20 20 20 20 20 20 20 20 20 20 20  are             
1a0e8 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1a0e9 70 72 65 70 61 72 65 0a 23 64 65 66 69 6e 65 20  prepare.#define 
1a0ea 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31  sqlite3_prepare1
1a0eb 36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73  6              s
1a0ec 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72 65 70  qlite3_api->prep
1a0ed 61 72 65 31 36 0a 23 64 65 66 69 6e 65 20 73 71  are16.#define sq
1a0ee 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
1a0ef 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
1a0f0 69 74 65 33 5f 61 70 69 2d 3e 70 72 65 70 61 72  ite3_api->prepar
1a0f1 65 5f 76 32 0a 23 64 65 66 69 6e 65 20 73 71 6c  e_v2.#define sql
1a0f2 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76  ite3_prepare16_v
1a0f3 32 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  2           sqli
1a0f4 74 65 33 5f 61 70 69 2d 3e 70 72 65 70 61 72 65  te3_api->prepare
1a0f5 31 36 5f 76 32 0a 23 64 65 66 69 6e 65 20 73 71  16_v2.#define sq
1a0f6 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65 20 20 20  lite3_profile   
1a0f7 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
1a0f8 69 74 65 33 5f 61 70 69 2d 3e 70 72 6f 66 69 6c  ite3_api->profil
1a0f9 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
1a0fa 33 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c  3_progress_handl
1a0fb 65 72 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  er       sqlite3
1a0fc 5f 61 70 69 2d 3e 70 72 6f 67 72 65 73 73 5f 68  _api->progress_h
1a0fd 61 6e 64 6c 65 72 0a 23 64 65 66 69 6e 65 20 73  andler.#define s
1a0fe 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 20 20  qlite3_realloc  
1a0ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
1a100 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 61 6c 6c  lite3_api->reall
1a101 6f 63 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  oc.#define sqlit
1a102 65 33 5f 72 65 73 65 74 20 20 20 20 20 20 20 20  e3_reset        
1a103 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1a104 33 5f 61 70 69 2d 3e 72 65 73 65 74 0a 23 64 65  3_api->reset.#de
1a105 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73  fine sqlite3_res
1a106 75 6c 74 5f 62 6c 6f 62 20 20 20 20 20 20 20 20  ult_blob        
1a107 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
1a108 3e 72 65 73 75 6c 74 5f 62 6c 6f 62 0a 23 64 65  >result_blob.#de
1a109 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73  fine sqlite3_res
1a10a 75 6c 74 5f 64 6f 75 62 6c 65 20 20 20 20 20 20  ult_double      
1a10b 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
1a10c 3e 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 0a 23  >result_double.#
1a10d 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72  define sqlite3_r
1a10e 65 73 75 6c 74 5f 65 72 72 6f 72 20 20 20 20 20  esult_error     
1a10f 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
1a110 69 2d 3e 72 65 73 75 6c 74 5f 65 72 72 6f 72 0a  i->result_error.
1a111 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
1a112 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 20 20  result_error16  
1a113 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
1a114 70 69 2d 3e 72 65 73 75 6c 74 5f 65 72 72 6f 72  pi->result_error
1a115 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  16.#define sqlit
1a116 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 20 20  e3_result_int   
1a117 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1a118 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 69 6e  3_api->result_in
1a119 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  t.#define sqlite
1a11a 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 20 20  3_result_int64  
1a11b 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1a11c 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 69 6e 74  _api->result_int
1a11d 36 34 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  64.#define sqlit
1a11e 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 20  e3_result_null  
1a11f 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1a120 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 6e 75  3_api->result_nu
1a121 6c 6c 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ll.#define sqlit
1a122 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 20 20  e3_result_text  
1a123 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1a124 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 74 65  3_api->result_te
1a125 78 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  xt.#define sqlit
1a126 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36  e3_result_text16
1a127 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1a128 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 74 65  3_api->result_te
1a129 78 74 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c  xt16.#define sql
1a12a 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
1a12b 31 36 62 65 20 20 20 20 20 20 20 20 73 71 6c 69  16be        sqli
1a12c 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f  te3_api->result_
1a12d 74 65 78 74 31 36 62 65 0a 23 64 65 66 69 6e 65  text16be.#define
1a12e 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
1a12f 74 65 78 74 31 36 6c 65 20 20 20 20 20 20 20 20  text16le        
1a130 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73  sqlite3_api->res
1a131 75 6c 74 5f 74 65 78 74 31 36 6c 65 0a 23 64 65  ult_text16le.#de
1a132 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73  fine sqlite3_res
1a133 75 6c 74 5f 76 61 6c 75 65 20 20 20 20 20 20 20  ult_value       
1a134 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
1a135 3e 72 65 73 75 6c 74 5f 76 61 6c 75 65 0a 23 64  >result_value.#d
1a136 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 6f  efine sqlite3_ro
1a137 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 20 20 20 20 20  llback_hook     
1a138 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
1a139 2d 3e 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 0a  ->rollback_hook.
1a13a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
1a13b 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 20 20  set_authorizer  
1a13c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
1a13d 70 69 2d 3e 73 65 74 5f 61 75 74 68 6f 72 69 7a  pi->set_authoriz
1a13e 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  er.#define sqlit
1a13f 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 20 20  e3_set_auxdata  
1a140 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1a141 33 5f 61 70 69 2d 3e 73 65 74 5f 61 75 78 64 61  3_api->set_auxda
1a142 74 61 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ta.#define sqlit
1a143 65 33 5f 73 6e 70 72 69 6e 74 66 20 20 20 20 20  e3_snprintf     
1a144 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1a145 33 5f 61 70 69 2d 3e 73 6e 70 72 69 6e 74 66 0a  3_api->snprintf.
1a146 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
1a147 73 74 65 70 20 20 20 20 20 20 20 20 20 20 20 20  step            
1a148 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
1a149 70 69 2d 3e 73 74 65 70 0a 23 64 65 66 69 6e 65  pi->step.#define
1a14a 20 73 71 6c 69 74 65 33 5f 74 61 62 6c 65 5f 63   sqlite3_table_c
1a14b 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61 20 20  olumn_metadata  
1a14c 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 74 61 62  sqlite3_api->tab
1a14d 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61  le_column_metada
1a14e 74 61 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ta.#define sqlit
1a14f 65 33 5f 74 68 72 65 61 64 5f 63 6c 65 61 6e 75  e3_thread_cleanu
1a150 70 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  p         sqlite
1a151 33 5f 61 70 69 2d 3e 74 68 72 65 61 64 5f 63 6c  3_api->thread_cl
1a152 65 61 6e 75 70 0a 23 64 65 66 69 6e 65 20 73 71  eanup.#define sq
1a153 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e  lite3_total_chan
1a154 67 65 73 20 20 20 20 20 20 20 20 20 20 73 71 6c  ges          sql
1a155 69 74 65 33 5f 61 70 69 2d 3e 74 6f 74 61 6c 5f  ite3_api->total_
1a156 63 68 61 6e 67 65 73 0a 23 64 65 66 69 6e 65 20  changes.#define 
1a157 73 71 6c 69 74 65 33 5f 74 72 61 63 65 20 20 20  sqlite3_trace   
1a158 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
1a159 71 6c 69 74 65 33 5f 61 70 69 2d 3e 74 72 61 63  qlite3_api->trac
1a15a 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
1a15b 33 5f 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69  3_transfer_bindi
1a15c 6e 67 73 20 20 20 20 20 20 73 71 6c 69 74 65 33  ngs      sqlite3
1a15d 5f 61 70 69 2d 3e 74 72 61 6e 73 66 65 72 5f 62  _api->transfer_b
1a15e 69 6e 64 69 6e 67 73 0a 23 64 65 66 69 6e 65 20  indings.#define 
1a15f 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68  sqlite3_update_h
1a160 6f 6f 6b 20 20 20 20 20 20 20 20 20 20 20 20 73  ook            s
1a161 71 6c 69 74 65 33 5f 61 70 69 2d 3e 75 70 64 61  qlite3_api->upda
1a162 74 65 5f 68 6f 6f 6b 0a 23 64 65 66 69 6e 65 20  te_hook.#define 
1a163 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74  sqlite3_user_dat
1a164 61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73  a              s
1a165 71 6c 69 74 65 33 5f 61 70 69 2d 3e 75 73 65 72  qlite3_api->user
1a166 5f 64 61 74 61 0a 23 64 65 66 69 6e 65 20 73 71  _data.#define sq
1a167 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
1a168 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
1a169 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f  ite3_api->value_
1a16a 62 6c 6f 62 0a 23 64 65 66 69 6e 65 20 73 71 6c  blob.#define sql
1a16b 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
1a16c 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
1a16d 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 62  te3_api->value_b
1a16e 79 74 65 73 0a 23 64 65 66 69 6e 65 20 73 71 6c  ytes.#define sql
1a16f 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
1a170 31 36 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  16          sqli
1a171 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 62  te3_api->value_b
1a172 79 74 65 73 31 36 0a 23 64 65 66 69 6e 65 20 73  ytes16.#define s
1a173 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75  qlite3_value_dou
1a174 62 6c 65 20 20 20 20 20 20 20 20 20 20 20 73 71  ble           sq
1a175 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65  lite3_api->value
1a176 5f 64 6f 75 62 6c 65 0a 23 64 65 66 69 6e 65 20  _double.#define 
1a177 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
1a178 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73  t              s
1a179 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75  qlite3_api->valu
1a17a 65 5f 69 6e 74 0a 23 64 65 66 69 6e 65 20 73 71  e_int.#define sq
1a17b 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36  lite3_value_int6
1a17c 34 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  4            sql
1a17d 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f  ite3_api->value_
1a17e 69 6e 74 36 34 0a 23 64 65 66 69 6e 65 20 73 71  int64.#define sq
1a17f 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65  lite3_value_nume
1a180 72 69 63 5f 74 79 70 65 20 20 20 20 20 73 71 6c  ric_type     sql
1a181 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f  ite3_api->value_
1a182 6e 75 6d 65 72 69 63 5f 74 79 70 65 0a 23 64 65  numeric_type.#de
1a183 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c  fine sqlite3_val
1a184 75 65 5f 74 65 78 74 20 20 20 20 20 20 20 20 20  ue_text         
1a185 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
1a186 3e 76 61 6c 75 65 5f 74 65 78 74 0a 23 64 65 66  >value_text.#def
1a187 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ine sqlite3_valu
1a188 65 5f 74 65 78 74 31 36 20 20 20 20 20 20 20 20  e_text16        
1a189 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1a18a 76 61 6c 75 65 5f 74 65 78 74 31 36 0a 23 64 65  value_text16.#de
1a18b 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c  fine sqlite3_val
1a18c 75 65 5f 74 65 78 74 31 36 62 65 20 20 20 20 20  ue_text16be     
1a18d 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
1a18e 3e 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65 0a  >value_text16be.
1a18f 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
1a190 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 20 20  value_text16le  
1a191 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
1a192 70 69 2d 3e 76 61 6c 75 65 5f 74 65 78 74 31 36  pi->value_text16
1a193 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  le.#define sqlit
1a194 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 20 20  e3_value_type   
1a195 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1a196 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 74 79 70  3_api->value_typ
1a197 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
1a198 33 5f 76 6d 70 72 69 6e 74 66 20 20 20 20 20 20  3_vmprintf      
1a199 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1a19a 5f 61 70 69 2d 3e 76 6d 70 72 69 6e 74 66 0a 23  _api->vmprintf.#
1a19b 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6f  define sqlite3_o
1a19c 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e  verload_function
1a19d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
1a19e 69 2d 3e 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63  i->overload_func
1a19f 74 69 6f 6e 0a 23 64 65 66 69 6e 65 20 73 71 6c  tion.#define sql
1a1a0 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 20  ite3_prepare_v2 
1a1a1 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
1a1a2 74 65 33 5f 61 70 69 2d 3e 70 72 65 70 61 72 65  te3_api->prepare
1a1a3 5f 76 32 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  _v2.#define sqli
1a1a4 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32  te3_prepare16_v2
1a1a5 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1a1a6 65 33 5f 61 70 69 2d 3e 70 72 65 70 61 72 65 31  e3_api->prepare1
1a1a7 36 5f 76 32 0a 23 64 65 66 69 6e 65 20 73 71 6c  6_v2.#define sql
1a1a8 69 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e 64 69  ite3_clear_bindi
1a1a9 6e 67 73 20 20 20 20 20 20 20 20 20 73 71 6c 69  ngs         sqli
1a1aa 74 65 33 5f 61 70 69 2d 3e 63 6c 65 61 72 5f 62  te3_api->clear_b
1a1ab 69 6e 64 69 6e 67 73 0a 23 65 6e 64 69 66 20 2f  indings.#endif /
1a1ac 2a 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 2a 2f  * SQLITE_CORE */
1a1ad 0a 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ..#define SQLITE
1a1ae 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 31  _EXTENSION_INIT1
1a1af 20 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74       const sqlit
1a1b0 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20  e3_api_routines 
1a1b1 2a 73 71 6c 69 74 65 33 5f 61 70 69 3b 0a 23 64  *sqlite3_api;.#d
1a1b2 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 58 54  efine SQLITE_EXT
1a1b3 45 4e 53 49 4f 4e 5f 49 4e 49 54 32 28 76 29 20  ENSION_INIT2(v) 
1a1b4 20 73 71 6c 69 74 65 33 5f 61 70 69 20 3d 20 76   sqlite3_api = v
1a1b5 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 53 51  ;..#endif /* _SQ
1a1b6 4c 49 54 45 33 45 58 54 5f 48 5f 20 2a 2f 0a 0a  LITE3EXT_H_ */..
1a1b7 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1a1b8 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 65 78  End of sqlite3ex
1a1b9 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
1a1ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a1bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1a1bc 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1a1bd 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
1a1be 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
1a1bf 6c 6f 61 64 65 78 74 2e 63 20 2a 2a 2a 2a 2a 2a  loadext.c ******
1a1c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1a1c1 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 41 50 49 20  ./*.** Some API 
1a1c2 72 6f 75 74 69 6e 65 73 20 61 72 65 20 6f 6d 69  routines are omi
1a1c3 74 74 65 64 20 77 68 65 6e 20 76 61 72 69 6f 75  tted when variou
1a1c4 73 20 66 65 61 74 75 72 65 73 20 61 72 65 0a 2a  s features are.*
1a1c5 2a 20 65 78 63 6c 75 64 65 64 20 66 72 6f 6d 20  * excluded from 
1a1c6 61 20 62 75 69 6c 64 20 6f 66 20 53 51 4c 69 74  a build of SQLit
1a1c7 65 2e 20 20 53 75 62 73 74 69 74 75 74 65 20 61  e.  Substitute a
1a1c8 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a   NULL pointer.**
1a1c9 20 66 6f 72 20 61 6e 79 20 6d 69 73 73 69 6e 67   for any missing
1a1ca 20 41 50 49 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65   APIs..*/.#ifnde
1a1cb 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
1a1cc 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a  COLUMN_METADATA.
1a1cd 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
1a1ce 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65  _column_database
1a1cf 5f 6e 61 6d 65 20 20 20 30 0a 23 20 64 65 66 69  _name   0.# defi
1a1d0 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
1a1d1 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31  n_database_name1
1a1d2 36 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  6 0.# define sql
1a1d3 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c  ite3_column_tabl
1a1d4 65 5f 6e 61 6d 65 20 20 20 20 20 20 30 0a 23 20  e_name      0.# 
1a1d5 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
1a1d6 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65  olumn_table_name
1a1d7 31 36 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65  16    0.# define
1a1d8 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
1a1d9 6f 72 69 67 69 6e 5f 6e 61 6d 65 20 20 20 20 20  origin_name     
1a1da 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
1a1db 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e  e3_column_origin
1a1dc 5f 6e 61 6d 65 31 36 20 20 20 30 0a 23 20 64 65  _name16   0.# de
1a1dd 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 61 62  fine sqlite3_tab
1a1de 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61  le_column_metada
1a1df 74 61 20 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69  ta  0.#endif..#i
1a1e0 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
1a1e1 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 23  _AUTHORIZATION.#
1a1e2 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
1a1e3 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 20 20  set_authorizer  
1a1e4 20 20 20 20 20 20 20 30 0a 23 65 6e 64 69 66 0a         0.#endif.
1a1e5 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
1a1e6 4d 49 54 5f 55 54 46 31 36 0a 23 20 64 65 66 69  MIT_UTF16.# defi
1a1e7 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  ne sqlite3_bind_
1a1e8 74 65 78 74 31 36 20 20 20 20 20 20 20 20 20 20  text16          
1a1e9 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
1a1ea 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e  ite3_collation_n
1a1eb 65 65 64 65 64 31 36 20 20 20 20 20 30 0a 23 20  eeded16     0.# 
1a1ec 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
1a1ed 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36  olumn_decltype16
1a1ee 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65        0.# define
1a1ef 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
1a1f0 6e 61 6d 65 31 36 20 20 20 20 20 20 20 20 20 20  name16          
1a1f1 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
1a1f2 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36  e3_column_text16
1a1f3 20 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65            0.# de
1a1f4 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d  fine sqlite3_com
1a1f5 70 6c 65 74 65 31 36 20 20 20 20 20 20 20 20 20  plete16         
1a1f6 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
1a1f7 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
1a1f8 6c 6c 61 74 69 6f 6e 31 36 20 20 20 20 20 30 0a  llation16     0.
1a1f9 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
1a1fa 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
1a1fb 31 36 20 20 20 20 20 20 30 0a 23 20 64 65 66 69  16      0.# defi
1a1fc 6e 65 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  ne sqlite3_errms
1a1fd 67 31 36 20 20 20 20 20 20 20 20 20 20 20 20 20  g16             
1a1fe 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
1a1ff 69 74 65 33 5f 6f 70 65 6e 31 36 20 20 20 20 20  ite3_open16     
1a200 20 20 20 20 20 20 20 20 20 20 20 20 30 0a 23 20              0.# 
1a201 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70  define sqlite3_p
1a202 72 65 70 61 72 65 31 36 20 20 20 20 20 20 20 20  repare16        
1a203 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65        0.# define
1a204 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
1a205 31 36 5f 76 32 20 20 20 20 20 20 20 20 20 20 20  16_v2           
1a206 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
1a207 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31  e3_result_error1
1a208 36 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65  6         0.# de
1a209 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73  fine sqlite3_res
1a20a 75 6c 74 5f 74 65 78 74 31 36 20 20 20 20 20 20  ult_text16      
1a20b 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
1a20c 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
1a20d 78 74 31 36 62 65 20 20 20 20 20 20 20 20 30 0a  xt16be        0.
1a20e 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
1a20f 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65  _result_text16le
1a210 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69          0.# defi
1a211 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ne sqlite3_value
1a212 5f 74 65 78 74 31 36 20 20 20 20 20 20 20 20 20  _text16         
1a213 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
1a214 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
1a215 36 62 65 20 20 20 20 20 20 20 20 20 30 0a 23 20  6be         0.# 
1a216 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76  define sqlite3_v
1a217 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 20 20 20  alue_text16le   
1a218 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65        0.# define
1a219 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
1a21a 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 20  database_name16 
1a21b 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
1a21c 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f  e3_column_table_
1a21d 6e 61 6d 65 31 36 20 20 20 20 30 0a 23 20 64 65  name16    0.# de
1a21e 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
1a21f 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31  umn_origin_name1
1a220 36 20 20 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69  6   0.#endif..#i
1a221 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
1a222 5f 43 4f 4d 50 4c 45 54 45 0a 23 20 64 65 66 69  _COMPLETE.# defi
1a223 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c  ne sqlite3_compl
1a224 65 74 65 20 30 0a 23 20 64 65 66 69 6e 65 20 73  ete 0.# define s
1a225 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31  qlite3_complete1
1a226 36 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64  6 0.#endif..#ifd
1a227 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50  ef SQLITE_OMIT_P
1a228 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 4b  ROGRESS_CALLBACK
1a229 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
1a22a 33 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c  3_progress_handl
1a22b 65 72 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66  er 0.#endif..#if
1a22c 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1a22d 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 23 20 64  VIRTUALTABLE.# d
1a22e 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 72  efine sqlite3_cr
1a22f 65 61 74 65 5f 6d 6f 64 75 6c 65 20 30 0a 23 20  eate_module 0.# 
1a230 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
1a231 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 20  reate_module_v2 
1a232 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
1a233 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 20  e3_declare_vtab 
1a234 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  0.#endif..#ifdef
1a235 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
1a236 52 45 44 5f 43 41 43 48 45 0a 23 20 64 65 66 69  RED_CACHE.# defi
1a237 6e 65 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c  ne sqlite3_enabl
1a238 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65 20 30  e_shared_cache 0
1a239 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
1a23a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43  SQLITE_OMIT_TRAC
1a23b 45 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  E.# define sqlit
1a23c 65 33 5f 70 72 6f 66 69 6c 65 20 20 20 20 20 20  e3_profile      
1a23d 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69   0.# define sqli
1a23e 74 65 33 5f 74 72 61 63 65 20 20 20 20 20 20 20  te3_trace       
1a23f 20 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64    0.#endif..#ifd
1a240 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 47  ef SQLITE_OMIT_G
1a241 45 54 5f 54 41 42 4c 45 0a 23 20 64 65 66 69 6e  ET_TABLE.# defin
1a242 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74  e sqlite3_free_t
1a243 61 62 6c 65 20 20 20 20 30 0a 23 20 64 65 66 69  able    0.# defi
1a244 6e 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74  ne sqlite3_get_t
1a245 61 62 6c 65 20 20 20 20 20 30 0a 23 65 6e 64 69  able     0.#endi
1a246 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  f../*.** The fol
1a247 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
1a248 20 63 6f 6e 74 61 69 6e 73 20 70 6f 69 6e 74 65   contains pointe
1a249 72 73 20 74 6f 20 61 6c 6c 20 53 51 4c 69 74 65  rs to all SQLite
1a24a 20 41 50 49 20 72 6f 75 74 69 6e 65 73 2e 0a 2a   API routines..*
1a24b 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  * A pointer to t
1a24c 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73  his structure is
1a24d 20 70 61 73 73 65 64 20 69 6e 74 6f 20 65 78 74   passed into ext
1a24e 65 6e 73 69 6f 6e 73 20 77 68 65 6e 20 74 68 65  ensions when the
1a24f 79 20 61 72 65 0a 2a 2a 20 6c 6f 61 64 65 64 20  y are.** loaded 
1a250 73 6f 20 74 68 61 74 20 74 68 65 20 65 78 74 65  so that the exte
1a251 6e 73 69 6f 6e 20 63 61 6e 20 6d 61 6b 65 20 63  nsion can make c
1a252 61 6c 6c 73 20 62 61 63 6b 20 69 6e 74 6f 20 74  alls back into t
1a253 68 65 20 53 51 4c 69 74 65 0a 2a 2a 20 6c 69 62  he SQLite.** lib
1a254 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  rary..**.** When
1a255 20 61 64 64 69 6e 67 20 6e 65 77 20 41 50 49 73   adding new APIs
1a256 2c 20 61 64 64 20 74 68 65 6d 20 74 6f 20 74 68  , add them to th
1a257 65 20 62 6f 74 74 6f 6d 20 6f 66 20 74 68 69 73  e bottom of this
1a258 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 6e   structure.** in
1a259 20 6f 72 64 65 72 20 74 6f 20 70 72 65 73 65 72   order to preser
1a25a 76 65 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d  ve backwards com
1a25b 70 61 74 69 62 69 6c 69 74 79 2e 0a 2a 2a 0a 2a  patibility..**.*
1a25c 2a 20 45 78 74 65 6e 73 69 6f 6e 73 20 74 68 61  * Extensions tha
1a25d 74 20 75 73 65 20 6e 65 77 65 72 20 41 50 49 73  t use newer APIs
1a25e 20 73 68 6f 75 6c 64 20 66 69 72 73 74 20 63 61   should first ca
1a25f 6c 6c 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65  ll the.** sqlite
1a260 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d  3_libversion_num
1a261 62 65 72 28 29 20 74 6f 20 6d 61 6b 65 20 73 75  ber() to make su
1a262 72 65 20 74 68 61 74 20 74 68 65 20 41 50 49 20  re that the API 
1a263 74 68 65 79 0a 2a 2a 20 69 6e 74 65 6e 64 20 74  they.** intend t
1a264 6f 20 75 73 65 20 69 73 20 73 75 70 70 6f 72 74  o use is support
1a265 65 64 20 62 79 20 74 68 65 20 6c 69 62 72 61 72  ed by the librar
1a266 79 2e 20 20 45 78 74 65 6e 73 69 6f 6e 73 20 73  y.  Extensions s
1a267 68 6f 75 6c 64 0a 2a 2a 20 61 6c 73 6f 20 63 68  hould.** also ch
1a268 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  eck to make sure
1a269 20 74 68 61 74 20 74 68 65 20 70 6f 69 6e 74 65   that the pointe
1a26a 72 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f  r to the functio
1a26b 6e 20 69 73 0a 2a 2a 20 6e 6f 74 20 4e 55 4c 4c  n is.** not NULL
1a26c 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20   before calling 
1a26d 69 74 2e 0a 2a 2f 0a 63 6f 6e 73 74 20 73 71 6c  it..*/.const sql
1a26e 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65  ite3_api_routine
1a26f 73 20 73 71 6c 69 74 65 33 5f 61 70 69 73 20 3d  s sqlite3_apis =
1a270 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 61 67 67   {.  sqlite3_agg
1a271 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 2c 0a  regate_context,.
1a272 20 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67    sqlite3_aggreg
1a273 61 74 65 5f 63 6f 75 6e 74 2c 0a 20 20 73 71 6c  ate_count,.  sql
1a274 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 2c 0a  ite3_bind_blob,.
1a275 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64    sqlite3_bind_d
1a276 6f 75 62 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33  ouble,.  sqlite3
1a277 5f 62 69 6e 64 5f 69 6e 74 2c 0a 20 20 73 71 6c  _bind_int,.  sql
1a278 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 2c  ite3_bind_int64,
1a279 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
1a27a 6e 75 6c 6c 2c 0a 20 20 73 71 6c 69 74 65 33 5f  null,.  sqlite3_
1a27b 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63  bind_parameter_c
1a27c 6f 75 6e 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f  ount,.  sqlite3_
1a27d 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69  bind_parameter_i
1a27e 6e 64 65 78 2c 0a 20 20 73 71 6c 69 74 65 33 5f  ndex,.  sqlite3_
1a27f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e  bind_parameter_n
1a280 61 6d 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62  ame,.  sqlite3_b
1a281 69 6e 64 5f 74 65 78 74 2c 0a 20 20 73 71 6c 69  ind_text,.  sqli
1a282 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 2c  te3_bind_text16,
1a283 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
1a284 76 61 6c 75 65 2c 0a 20 20 73 71 6c 69 74 65 33  value,.  sqlite3
1a285 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 2c 0a 20  _busy_handler,. 
1a286 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69   sqlite3_busy_ti
1a287 6d 65 6f 75 74 2c 0a 20 20 73 71 6c 69 74 65 33  meout,.  sqlite3
1a288 5f 63 68 61 6e 67 65 73 2c 0a 20 20 73 71 6c 69  _changes,.  sqli
1a289 74 65 33 5f 63 6c 6f 73 65 2c 0a 20 20 73 71 6c  te3_close,.  sql
1a28a 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e  ite3_collation_n
1a28b 65 65 64 65 64 2c 0a 20 20 73 71 6c 69 74 65 33  eeded,.  sqlite3
1a28c 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65  _collation_neede
1a28d 64 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  d16,.  sqlite3_c
1a28e 6f 6c 75 6d 6e 5f 62 6c 6f 62 2c 0a 20 20 73 71  olumn_blob,.  sq
1a28f 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
1a290 65 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  es,.  sqlite3_co
1a291 6c 75 6d 6e 5f 62 79 74 65 73 31 36 2c 0a 20 20  lumn_bytes16,.  
1a292 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63  sqlite3_column_c
1a293 6f 75 6e 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f  ount,.  sqlite3_
1a294 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f  column_database_
1a295 6e 61 6d 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  name,.  sqlite3_
1a296 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f  column_database_
1a297 6e 61 6d 65 31 36 2c 0a 20 20 73 71 6c 69 74 65  name16,.  sqlite
1a298 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70  3_column_decltyp
1a299 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c  e,.  sqlite3_col
1a29a 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 2c 0a  umn_decltype16,.
1a29b 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
1a29c 5f 64 6f 75 62 6c 65 2c 0a 20 20 73 71 6c 69 74  _double,.  sqlit
1a29d 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 2c 0a 20  e3_column_int,. 
1a29e 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
1a29f 69 6e 74 36 34 2c 0a 20 20 73 71 6c 69 74 65 33  int64,.  sqlite3
1a2a0 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 2c 0a 20 20  _column_name,.  
1a2a1 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e  sqlite3_column_n
1a2a2 61 6d 65 31 36 2c 0a 20 20 73 71 6c 69 74 65 33  ame16,.  sqlite3
1a2a3 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e  _column_origin_n
1a2a4 61 6d 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  ame,.  sqlite3_c
1a2a5 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d  olumn_origin_nam
1a2a6 65 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  e16,.  sqlite3_c
1a2a7 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65  olumn_table_name
1a2a8 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ,.  sqlite3_colu
1a2a9 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 2c  mn_table_name16,
1a2aa 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  .  sqlite3_colum
1a2ab 6e 5f 74 65 78 74 2c 0a 20 20 73 71 6c 69 74 65  n_text,.  sqlite
1a2ac 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 2c  3_column_text16,
1a2ad 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  .  sqlite3_colum
1a2ae 6e 5f 74 79 70 65 2c 0a 20 20 73 71 6c 69 74 65  n_type,.  sqlite
1a2af 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 2c 0a  3_column_value,.
1a2b0 20 20 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74    sqlite3_commit
1a2b1 5f 68 6f 6f 6b 2c 0a 20 20 73 71 6c 69 74 65 33  _hook,.  sqlite3
1a2b2 5f 63 6f 6d 70 6c 65 74 65 2c 0a 20 20 73 71 6c  _complete,.  sql
1a2b3 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 2c  ite3_complete16,
1a2b4 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  .  sqlite3_creat
1a2b5 65 5f 63 6f 6c 6c 61 74 69 6f 6e 2c 0a 20 20 73  e_collation,.  s
1a2b6 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
1a2b7 6c 6c 61 74 69 6f 6e 31 36 2c 0a 20 20 73 71 6c  llation16,.  sql
1a2b8 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
1a2b9 74 69 6f 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f  tion,.  sqlite3_
1a2ba 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31  create_function1
1a2bb 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65  6,.  sqlite3_cre
1a2bc 61 74 65 5f 6d 6f 64 75 6c 65 2c 0a 20 20 73 71  ate_module,.  sq
1a2bd 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74  lite3_data_count
1a2be 2c 0a 20 20 73 71 6c 69 74 65 33 5f 64 62 5f 68  ,.  sqlite3_db_h
1a2bf 61 6e 64 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33  andle,.  sqlite3
1a2c0 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 2c 0a 20  _declare_vtab,. 
1a2c1 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f   sqlite3_enable_
1a2c2 73 68 61 72 65 64 5f 63 61 63 68 65 2c 0a 20 20  shared_cache,.  
1a2c3 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 2c  sqlite3_errcode,
1a2c4 0a 20 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  .  sqlite3_errms
1a2c5 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f 65 72 72  g,.  sqlite3_err
1a2c6 6d 73 67 31 36 2c 0a 20 20 73 71 6c 69 74 65 33  msg16,.  sqlite3
1a2c7 5f 65 78 65 63 2c 0a 20 20 73 71 6c 69 74 65 33  _exec,.  sqlite3
1a2c8 5f 65 78 70 69 72 65 64 2c 0a 20 20 73 71 6c 69  _expired,.  sqli
1a2c9 74 65 33 5f 66 69 6e 61 6c 69 7a 65 2c 0a 20 20  te3_finalize,.  
1a2ca 73 71 6c 69 74 65 33 5f 66 72 65 65 2c 0a 20 20  sqlite3_free,.  
1a2cb 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62  sqlite3_free_tab
1a2cc 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 67 65  le,.  sqlite3_ge
1a2cd 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 2c 0a 20 20  t_autocommit,.  
1a2ce 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64  sqlite3_get_auxd
1a2cf 61 74 61 2c 0a 20 20 73 71 6c 69 74 65 33 5f 67  ata,.  sqlite3_g
1a2d0 65 74 5f 74 61 62 6c 65 2c 0a 20 20 30 2c 20 20  et_table,.  0,  
1a2d1 20 20 20 2f 2a 20 57 61 73 20 73 71 6c 69 74 65     /* Was sqlite
1a2d2 33 5f 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72  3_global_recover
1a2d3 28 29 2c 20 62 75 74 20 74 68 61 74 20 66 75 6e  (), but that fun
1a2d4 63 74 69 6f 6e 20 69 73 20 64 65 70 72 65 63 61  ction is depreca
1a2d5 74 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ted */.  sqlite3
1a2d6 5f 69 6e 74 65 72 72 75 70 74 2c 0a 20 20 73 71  _interrupt,.  sq
1a2d7 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72  lite3_last_inser
1a2d8 74 5f 72 6f 77 69 64 2c 0a 20 20 73 71 6c 69 74  t_rowid,.  sqlit
1a2d9 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 2c 0a 20  e3_libversion,. 
1a2da 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73   sqlite3_libvers
1a2db 69 6f 6e 5f 6e 75 6d 62 65 72 2c 0a 20 20 73 71  ion_number,.  sq
1a2dc 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 2c 0a 20 20  lite3_malloc,.  
1a2dd 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 2c  sqlite3_mprintf,
1a2de 0a 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 2c  .  sqlite3_open,
1a2df 0a 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31  .  sqlite3_open1
1a2e0 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 70 72 65  6,.  sqlite3_pre
1a2e1 70 61 72 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  pare,.  sqlite3_
1a2e2 70 72 65 70 61 72 65 31 36 2c 0a 20 20 73 71 6c  prepare16,.  sql
1a2e3 69 74 65 33 5f 70 72 6f 66 69 6c 65 2c 0a 20 20  ite3_profile,.  
1a2e4 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73  sqlite3_progress
1a2e5 5f 68 61 6e 64 6c 65 72 2c 0a 20 20 73 71 6c 69  _handler,.  sqli
1a2e6 74 65 33 5f 72 65 61 6c 6c 6f 63 2c 0a 20 20 73  te3_realloc,.  s
1a2e7 71 6c 69 74 65 33 5f 72 65 73 65 74 2c 0a 20 20  qlite3_reset,.  
1a2e8 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62  sqlite3_result_b
1a2e9 6c 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72  lob,.  sqlite3_r
1a2ea 65 73 75 6c 74 5f 64 6f 75 62 6c 65 2c 0a 20 20  esult_double,.  
1a2eb 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
1a2ec 72 72 6f 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f  rror,.  sqlite3_
1a2ed 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 2c 0a  result_error16,.
1a2ee 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
1a2ef 5f 69 6e 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f  _int,.  sqlite3_
1a2f0 72 65 73 75 6c 74 5f 69 6e 74 36 34 2c 0a 20 20  result_int64,.  
1a2f1 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
1a2f2 75 6c 6c 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72  ull,.  sqlite3_r
1a2f3 65 73 75 6c 74 5f 74 65 78 74 2c 0a 20 20 73 71  esult_text,.  sq
1a2f4 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
1a2f5 74 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72  t16,.  sqlite3_r
1a2f6 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 2c 0a  esult_text16be,.
1a2f7 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
1a2f8 5f 74 65 78 74 31 36 6c 65 2c 0a 20 20 73 71 6c  _text16le,.  sql
1a2f9 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75  ite3_result_valu
1a2fa 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 6f 6c  e,.  sqlite3_rol
1a2fb 6c 62 61 63 6b 5f 68 6f 6f 6b 2c 0a 20 20 73 71  lback_hook,.  sq
1a2fc 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72  lite3_set_author
1a2fd 69 7a 65 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f  izer,.  sqlite3_
1a2fe 73 65 74 5f 61 75 78 64 61 74 61 2c 0a 20 20 73  set_auxdata,.  s
1a2ff 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 2c  qlite3_snprintf,
1a300 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 2c  .  sqlite3_step,
1a301 0a 20 20 73 71 6c 69 74 65 33 5f 74 61 62 6c 65  .  sqlite3_table
1a302 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61  _column_metadata
1a303 2c 0a 20 20 73 71 6c 69 74 65 33 5f 74 68 72 65  ,.  sqlite3_thre
1a304 61 64 5f 63 6c 65 61 6e 75 70 2c 0a 20 20 73 71  ad_cleanup,.  sq
1a305 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e  lite3_total_chan
1a306 67 65 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 74  ges,.  sqlite3_t
1a307 72 61 63 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  race,.  sqlite3_
1a308 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69 6e 67  transfer_binding
1a309 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 75 70 64  s,.  sqlite3_upd
1a30a 61 74 65 5f 68 6f 6f 6b 2c 0a 20 20 73 71 6c 69  ate_hook,.  sqli
1a30b 74 65 33 5f 75 73 65 72 5f 64 61 74 61 2c 0a 20  te3_user_data,. 
1a30c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
1a30d 6c 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  lob,.  sqlite3_v
1a30e 61 6c 75 65 5f 62 79 74 65 73 2c 0a 20 20 73 71  alue_bytes,.  sq
1a30f 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
1a310 73 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  s16,.  sqlite3_v
1a311 61 6c 75 65 5f 64 6f 75 62 6c 65 2c 0a 20 20 73  alue_double,.  s
1a312 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
1a313 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
1a314 65 5f 69 6e 74 36 34 2c 0a 20 20 73 71 6c 69 74  e_int64,.  sqlit
1a315 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63  e3_value_numeric
1a316 5f 74 79 70 65 2c 0a 20 20 73 71 6c 69 74 65 33  _type,.  sqlite3
1a317 5f 76 61 6c 75 65 5f 74 65 78 74 2c 0a 20 20 73  _value_text,.  s
1a318 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
1a319 74 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  t16,.  sqlite3_v
1a31a 61 6c 75 65 5f 74 65 78 74 31 36 62 65 2c 0a 20  alue_text16be,. 
1a31b 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
1a31c 65 78 74 31 36 6c 65 2c 0a 20 20 73 71 6c 69 74  ext16le,.  sqlit
1a31d 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 2c 0a 20  e3_value_type,. 
1a31e 20 73 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74   sqlite3_vmprint
1a31f 66 2c 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65  f,.  /*.  ** The
1a320 20 6f 72 69 67 69 6e 61 6c 20 41 50 49 20 73 65   original API se
1a321 74 20 65 6e 64 73 20 68 65 72 65 2e 20 20 41 6c  t ends here.  Al
1a322 6c 20 65 78 74 65 6e 73 69 6f 6e 73 20 63 61 6e  l extensions can
1a323 20 63 61 6c 6c 20 61 6e 79 0a 20 20 2a 2a 20 6f   call any.  ** o
1a324 66 20 74 68 65 20 41 50 49 73 20 61 62 6f 76 65  f the APIs above
1a325 20 70 72 6f 76 69 64 65 64 20 74 68 61 74 20 74   provided that t
1a326 68 65 20 70 6f 69 6e 74 65 72 20 69 73 20 6e 6f  he pointer is no
1a327 74 20 4e 55 4c 4c 2e 20 20 42 75 74 0a 20 20 2a  t NULL.  But.  *
1a328 2a 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67  * before calling
1a329 20 41 50 49 73 20 74 68 61 74 20 66 6f 6c 6c 6f   APIs that follo
1a32a 77 2c 20 65 78 74 65 6e 73 69 6f 6e 20 73 68 6f  w, extension sho
1a32b 75 6c 64 20 63 68 65 63 6b 20 74 68 65 0a 20 20  uld check the.  
1a32c 2a 2a 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65  ** sqlite3_libve
1a32d 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 29 20 74  rsion_number() t
1a32e 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 79  o make sure they
1a32f 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74   are dealing wit
1a330 68 0a 20 20 2a 2a 20 61 20 6c 69 62 72 61 72 79  h.  ** a library
1a331 20 74 68 61 74 20 69 73 20 6e 65 77 20 65 6e 6f   that is new eno
1a332 75 67 68 20 74 6f 20 73 75 70 70 6f 72 74 20 74  ugh to support t
1a333 68 61 74 20 41 50 49 2e 0a 20 20 2a 2a 2a 2a 2a  hat API..  *****
1a334 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a335 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a336 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a337 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a338 2a 2a 2a 2a 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  ****.  */.  sqli
1a339 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e  te3_overload_fun
1a33a 63 74 69 6f 6e 2c 0a 0a 20 20 2f 2a 0a 20 20 2a  ction,..  /*.  *
1a33b 2a 20 41 64 64 65 64 20 61 66 74 65 72 20 33 2e  * Added after 3.
1a33c 33 2e 31 33 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  3.13.  */.  sqli
1a33d 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 2c 0a  te3_prepare_v2,.
1a33e 20 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72    sqlite3_prepar
1a33f 65 31 36 5f 76 32 2c 0a 20 20 73 71 6c 69 74 65  e16_v2,.  sqlite
1a340 33 5f 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73  3_clear_bindings
1a341 2c 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 64 64  ,..  /*.  ** Add
1a342 65 64 20 66 6f 72 20 33 2e 34 2e 31 0a 20 20 2a  ed for 3.4.1.  *
1a343 2f 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61  /.  sqlite3_crea
1a344 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 2c 0a 0a 7d  te_module_v2,..}
1a345 3b 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74  ;../*.** Attempt
1a346 20 74 6f 20 6c 6f 61 64 20 61 6e 20 53 51 4c 69   to load an SQLi
1a347 74 65 20 65 78 74 65 6e 73 69 6f 6e 20 6c 69 62  te extension lib
1a348 72 61 72 79 20 63 6f 6e 74 61 69 6e 65 64 20 69  rary contained i
1a349 6e 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20 7a 46  n the file.** zF
1a34a 69 6c 65 2e 20 20 54 68 65 20 65 6e 74 72 79 20  ile.  The entry 
1a34b 70 6f 69 6e 74 20 69 73 20 7a 50 72 6f 63 2e 20  point is zProc. 
1a34c 20 7a 50 72 6f 63 20 6d 61 79 20 62 65 20 30 20   zProc may be 0 
1a34d 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 61 0a  in which case a.
1a34e 2a 2a 20 64 65 66 61 75 6c 74 20 65 6e 74 72 79  ** default entry
1a34f 20 70 6f 69 6e 74 20 6e 61 6d 65 20 28 73 71 6c   point name (sql
1a350 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69  ite3_extension_i
1a351 6e 69 74 29 20 69 73 20 75 73 65 64 2e 20 20 55  nit) is used.  U
1a352 73 65 0a 2a 2a 20 6f 66 20 74 68 65 20 64 65 66  se.** of the def
1a353 61 75 6c 74 20 6e 61 6d 65 20 69 73 20 72 65 63  ault name is rec
1a354 6f 6d 6d 65 6e 64 65 64 2e 0a 2a 2a 0a 2a 2a 20  ommended..**.** 
1a355 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
1a356 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20   on success and 
1a357 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 66 20  SQLITE_ERROR if 
1a358 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77  something goes w
1a359 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  rong..**.** If a
1a35a 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61  n error occurs a
1a35b 6e 64 20 70 7a 45 72 72 4d 73 67 20 69 73 20 6e  nd pzErrMsg is n
1a35c 6f 74 20 30 2c 20 74 68 65 6e 20 66 69 6c 6c 20  ot 0, then fill 
1a35d 2a 70 7a 45 72 72 4d 73 67 20 77 69 74 68 20 0a  *pzErrMsg with .
1a35e 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  ** error message
1a35f 20 74 65 78 74 2e 20 20 54 68 65 20 63 61 6c 6c   text.  The call
1a360 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f  ing function sho
1a361 75 6c 64 20 66 72 65 65 20 74 68 69 73 20 6d 65  uld free this me
1a362 6d 6f 72 79 0a 2a 2a 20 62 79 20 63 61 6c 6c 69  mory.** by calli
1a363 6e 67 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  ng sqlite3_free(
1a364 29 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65  )..*/.int sqlite
1a365 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e  3_load_extension
1a366 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
1a367 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 61            /* Loa
1a368 64 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e 20  d the extension 
1a369 69 6e 74 6f 20 74 68 69 73 20 64 61 74 61 62 61  into this databa
1a36a 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
1a36b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
1a36c 46 69 6c 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65  File,    /* Name
1a36d 20 6f 66 20 74 68 65 20 73 68 61 72 65 64 20 6c   of the shared l
1a36e 69 62 72 61 72 79 20 63 6f 6e 74 61 69 6e 69 6e  ibrary containin
1a36f 67 20 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 20  g extension */. 
1a370 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 72   const char *zPr
1a371 6f 63 2c 20 20 20 20 2f 2a 20 45 6e 74 72 79 20  oc,    /* Entry 
1a372 70 6f 69 6e 74 2e 20 20 55 73 65 20 22 73 71 6c  point.  Use "sql
1a373 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69  ite3_extension_i
1a374 6e 69 74 22 20 69 66 20 30 20 2a 2f 0a 20 20 63  nit" if 0 */.  c
1a375 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20  har **pzErrMsg  
1a376 20 20 20 20 20 2f 2a 20 50 75 74 20 65 72 72 6f       /* Put erro
1a377 72 20 6d 65 73 73 61 67 65 20 68 65 72 65 20 69  r message here i
1a378 66 20 6e 6f 74 20 30 20 2a 2f 0a 29 7b 0a 20 20  f not 0 */.){.  
1a379 76 6f 69 64 20 2a 68 61 6e 64 6c 65 3b 0a 20 20  void *handle;.  
1a37a 69 6e 74 20 28 2a 78 49 6e 69 74 29 28 73 71 6c  int (*xInit)(sql
1a37b 69 74 65 33 2a 2c 63 68 61 72 2a 2a 2c 63 6f 6e  ite3*,char**,con
1a37c 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72  st sqlite3_api_r
1a37d 6f 75 74 69 6e 65 73 2a 29 3b 0a 20 20 63 68 61  outines*);.  cha
1a37e 72 20 2a 7a 45 72 72 6d 73 67 20 3d 20 30 3b 0a  r *zErrmsg = 0;.
1a37f 20 20 76 6f 69 64 20 2a 2a 61 48 61 6e 64 6c 65    void **aHandle
1a380 3b 0a 0a 20 20 2f 2a 20 54 69 63 6b 65 74 20 23  ;..  /* Ticket #
1a381 31 38 36 33 2e 20 20 54 6f 20 61 76 6f 69 64 20  1863.  To avoid 
1a382 61 20 63 72 65 61 74 69 6e 67 20 73 65 63 75 72  a creating secur
1a383 69 74 79 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72  ity problems for
1a384 20 6f 6c 64 65 72 0a 20 20 2a 2a 20 61 70 70 6c   older.  ** appl
1a385 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 72 65  ications that re
1a386 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20 6e 65 77  link against new
1a387 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53  er versions of S
1a388 51 4c 69 74 65 2c 20 74 68 65 0a 20 20 2a 2a 20  QLite, the.  ** 
1a389 61 62 69 6c 69 74 79 20 74 6f 20 72 75 6e 20 6c  ability to run l
1a38a 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 20 69 73  oad_extension is
1a38b 20 74 75 72 6e 65 64 20 6f 66 66 20 62 79 20 64   turned off by d
1a38c 65 66 61 75 6c 74 2e 20 20 4f 6e 65 0a 20 20 2a  efault.  One.  *
1a38d 2a 20 6d 75 73 74 20 63 61 6c 6c 20 73 71 6c 69  * must call sqli
1a38e 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f  te3_enable_load_
1a38f 65 78 74 65 6e 73 69 6f 6e 28 29 20 74 6f 20 74  extension() to t
1a390 75 72 6e 20 6f 6e 20 65 78 74 65 6e 73 69 6f 6e  urn on extension
1a391 0a 20 20 2a 2a 20 6c 6f 61 64 69 6e 67 2e 20 20  .  ** loading.  
1a392 4f 74 68 65 72 77 69 73 65 20 79 6f 75 20 67 65  Otherwise you ge
1a393 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  t the following 
1a394 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66  error..  */.  if
1a395 28 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53  ( (db->flags & S
1a396 51 4c 49 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73  QLITE_LoadExtens
1a397 69 6f 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69  ion)==0 ){.    i
1a398 66 28 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20  f( pzErrMsg ){. 
1a399 20 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d       *pzErrMsg =
1a39a 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
1a39b 28 22 6e 6f 74 20 61 75 74 68 6f 72 69 7a 65 64  ("not authorized
1a39c 22 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65  ");.    }.    re
1a39d 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
1a39e 52 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 7a 50  R;.  }..  if( zP
1a39f 72 6f 63 3d 3d 30 20 29 7b 0a 20 20 20 20 7a 50  roc==0 ){.    zP
1a3a0 72 6f 63 20 3d 20 22 73 71 6c 69 74 65 33 5f 65  roc = "sqlite3_e
1a3a1 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 22 3b 0a  xtension_init";.
1a3a2 20 20 7d 0a 0a 20 20 68 61 6e 64 6c 65 20 3d 20    }..  handle = 
1a3a3 73 71 6c 69 74 65 33 4f 73 44 6c 6f 70 65 6e 28  sqlite3OsDlopen(
1a3a4 7a 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 68 61  zFile);.  if( ha
1a3a5 6e 64 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 20 69  ndle==0 ){.    i
1a3a6 66 28 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20  f( pzErrMsg ){. 
1a3a7 20 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d       *pzErrMsg =
1a3a8 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
1a3a9 28 22 75 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e  ("unable to open
1a3aa 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20   shared library 
1a3ab 5b 25 73 5d 22 2c 20 7a 46 69 6c 65 29 3b 0a 20  [%s]", zFile);. 
1a3ac 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20     }.    return 
1a3ad 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
1a3ae 7d 0a 20 20 78 49 6e 69 74 20 3d 20 28 69 6e 74  }.  xInit = (int
1a3af 28 2a 29 28 73 71 6c 69 74 65 33 2a 2c 63 68 61  (*)(sqlite3*,cha
1a3b0 72 2a 2a 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65  r**,const sqlite
1a3b1 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 2a 29  3_api_routines*)
1a3b2 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ).              
1a3b3 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44 6c       sqlite3OsDl
1a3b4 73 79 6d 28 68 61 6e 64 6c 65 2c 20 7a 50 72 6f  sym(handle, zPro
1a3b5 63 29 3b 0a 20 20 69 66 28 20 78 49 6e 69 74 3d  c);.  if( xInit=
1a3b6 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 7a  =0 ){.    if( pz
1a3b7 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20  ErrMsg ){.      
1a3b8 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c   *pzErrMsg = sql
1a3b9 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 6e 6f  ite3_mprintf("no
1a3ba 20 65 6e 74 72 79 20 70 6f 69 6e 74 20 5b 25 73   entry point [%s
1a3bb 5d 20 69 6e 20 73 68 61 72 65 64 20 6c 69 62 72  ] in shared libr
1a3bc 61 72 79 20 5b 25 73 5d 22 2c 0a 20 20 20 20 20  ary [%s]",.     
1a3bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a3be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 50                zP
1a3bf 72 6f 63 2c 20 7a 46 69 6c 65 29 3b 0a 20 20 20  roc, zFile);.   
1a3c0 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73   }.    sqlite3Os
1a3c1 44 6c 63 6c 6f 73 65 28 68 61 6e 64 6c 65 29 3b  Dlclose(handle);
1a3c2 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
1a3c3 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 65 6c 73  TE_ERROR;.  }els
1a3c4 65 20 69 66 28 20 78 49 6e 69 74 28 64 62 2c 20  e if( xInit(db, 
1a3c5 26 7a 45 72 72 6d 73 67 2c 20 26 73 71 6c 69 74  &zErrmsg, &sqlit
1a3c6 65 33 5f 61 70 69 73 29 20 29 7b 0a 20 20 20 20  e3_apis) ){.    
1a3c7 69 66 28 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a  if( pzErrMsg ){.
1a3c8 20 20 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20        *pzErrMsg 
1a3c9 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
1a3ca 66 28 22 65 72 72 6f 72 20 64 75 72 69 6e 67 20  f("error during 
1a3cb 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 3a 20  initialization: 
1a3cc 25 73 22 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20  %s", zErrmsg);. 
1a3cd 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
1a3ce 5f 66 72 65 65 28 7a 45 72 72 6d 73 67 29 3b 0a  _free(zErrmsg);.
1a3cf 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 63      sqlite3OsDlc
1a3d0 6c 6f 73 65 28 68 61 6e 64 6c 65 29 3b 0a 20 20  lose(handle);.  
1a3d1 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1a3d2 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  ERROR;.  }..  /*
1a3d3 20 41 70 70 65 6e 64 20 74 68 65 20 6e 65 77 20   Append the new 
1a3d4 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20 68  shared library h
1a3d5 61 6e 64 6c 65 20 74 6f 20 74 68 65 20 64 62 2d  andle to the db-
1a3d6 3e 61 45 78 74 65 6e 73 69 6f 6e 20 61 72 72 61  >aExtension arra
1a3d7 79 2e 20 2a 2f 0a 20 20 64 62 2d 3e 6e 45 78 74  y. */.  db->nExt
1a3d8 65 6e 73 69 6f 6e 2b 2b 3b 0a 20 20 61 48 61 6e  ension++;.  aHan
1a3d9 64 6c 65 20 3d 20 73 71 6c 69 74 65 4d 61 6c 6c  dle = sqliteMall
1a3da 6f 63 28 73 69 7a 65 6f 66 28 68 61 6e 64 6c 65  oc(sizeof(handle
1a3db 29 2a 64 62 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e  )*db->nExtension
1a3dc 29 3b 0a 20 20 69 66 28 20 61 48 61 6e 64 6c 65  );.  if( aHandle
1a3dd 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
1a3de 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
1a3df 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6e 45    }.  if( db->nE
1a3e0 78 74 65 6e 73 69 6f 6e 3e 30 20 29 7b 0a 20 20  xtension>0 ){.  
1a3e1 20 20 6d 65 6d 63 70 79 28 61 48 61 6e 64 6c 65    memcpy(aHandle
1a3e2 2c 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e  , db->aExtension
1a3e3 2c 20 73 69 7a 65 6f 66 28 68 61 6e 64 6c 65 29  , sizeof(handle)
1a3e4 2a 28 64 62 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e  *(db->nExtension
1a3e5 2d 31 29 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  -1));.  }.  sqli
1a3e6 74 65 46 72 65 65 28 64 62 2d 3e 61 45 78 74 65  teFree(db->aExte
1a3e7 6e 73 69 6f 6e 29 3b 0a 20 20 64 62 2d 3e 61 45  nsion);.  db->aE
1a3e8 78 74 65 6e 73 69 6f 6e 20 3d 20 61 48 61 6e 64  xtension = aHand
1a3e9 6c 65 3b 0a 0a 20 20 64 62 2d 3e 61 45 78 74 65  le;..  db->aExte
1a3ea 6e 73 69 6f 6e 5b 64 62 2d 3e 6e 45 78 74 65 6e  nsion[db->nExten
1a3eb 73 69 6f 6e 2d 31 5d 20 3d 20 68 61 6e 64 6c 65  sion-1] = handle
1a3ec 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
1a3ed 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  E_OK;.}../*.** C
1a3ee 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  all this routine
1a3ef 20 77 68 65 6e 20 74 68 65 20 64 61 74 61 62 61   when the databa
1a3f0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73  se connection is
1a3f1 20 63 6c 6f 73 69 6e 67 20 69 6e 20 6f 72 64 65   closing in orde
1a3f2 72 0a 2a 2a 20 74 6f 20 63 6c 65 61 6e 20 75 70  r.** to clean up
1a3f3 20 6c 6f 61 64 65 64 20 65 78 74 65 6e 73 69 6f   loaded extensio
1a3f4 6e 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  ns.*/.SQLITE_PRI
1a3f5 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1a3f6 33 43 6c 6f 73 65 45 78 74 65 6e 73 69 6f 6e 73  3CloseExtensions
1a3f7 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
1a3f8 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d   int i;.  for(i=
1a3f9 30 3b 20 69 3c 64 62 2d 3e 6e 45 78 74 65 6e 73  0; i<db->nExtens
1a3fa 69 6f 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  ion; i++){.    s
1a3fb 71 6c 69 74 65 33 4f 73 44 6c 63 6c 6f 73 65 28  qlite3OsDlclose(
1a3fc 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e 5b 69  db->aExtension[i
1a3fd 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  ]);.  }.  sqlite
1a3fe 46 72 65 65 28 64 62 2d 3e 61 45 78 74 65 6e 73  Free(db->aExtens
1a3ff 69 6f 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45  ion);.}../*.** E
1a400 6e 61 62 6c 65 20 6f 72 20 64 69 73 61 62 6c 65  nable or disable
1a401 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69   extension loadi
1a402 6e 67 2e 20 20 45 78 74 65 6e 73 69 6f 6e 20 6c  ng.  Extension l
1a403 6f 61 64 69 6e 67 20 69 73 20 64 69 73 61 62 6c  oading is disabl
1a404 65 64 20 62 79 0a 2a 2a 20 64 65 66 61 75 6c 74  ed by.** default
1a405 20 73 6f 20 61 73 20 6e 6f 74 20 74 6f 20 6f 70   so as not to op
1a406 65 6e 20 73 65 63 75 72 69 74 79 20 68 6f 6c 65  en security hole
1a407 73 20 69 6e 20 6f 6c 64 65 72 20 61 70 70 6c 69  s in older appli
1a408 63 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 69 6e 74 20  cations..*/.int 
1a409 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 6c  sqlite3_enable_l
1a40a 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 73 71  oad_extension(sq
1a40b 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6f  lite3 *db, int o
1a40c 6e 6f 66 66 29 7b 0a 20 20 69 66 28 20 6f 6e 6f  noff){.  if( ono
1a40d 66 66 20 29 7b 0a 20 20 20 20 64 62 2d 3e 66 6c  ff ){.    db->fl
1a40e 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 4c 6f  ags |= SQLITE_Lo
1a40f 61 64 45 78 74 65 6e 73 69 6f 6e 3b 0a 20 20 7d  adExtension;.  }
1a410 65 6c 73 65 7b 0a 20 20 20 20 64 62 2d 3e 66 6c  else{.    db->fl
1a411 61 67 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f 4c  ags &= ~SQLITE_L
1a412 6f 61 64 45 78 74 65 6e 73 69 6f 6e 3b 0a 20 20  oadExtension;.  
1a413 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
1a414 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  E_OK;.}../*.** A
1a415 20 6c 69 73 74 20 6f 66 20 61 75 74 6f 6d 61 74   list of automat
1a416 69 63 61 6c 6c 79 20 6c 6f 61 64 65 64 20 65 78  ically loaded ex
1a417 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20  tensions..**.** 
1a418 54 68 69 73 20 6c 69 73 74 20 69 73 20 73 68 61  This list is sha
1a419 72 65 64 20 61 63 72 6f 73 73 20 74 68 72 65 61  red across threa
1a41a 64 73 2c 20 73 6f 20 62 65 20 73 75 72 65 20 74  ds, so be sure t
1a41b 6f 20 68 6f 6c 64 20 74 68 65 0a 2a 2a 20 6d 75  o hold the.** mu
1a41c 74 65 78 20 77 68 69 6c 65 20 61 63 63 65 73 73  tex while access
1a41d 69 6e 67 20 6f 72 20 63 68 61 6e 67 69 6e 67 20  ing or changing 
1a41e 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  it..*/.static in
1a41f 74 20 6e 41 75 74 6f 45 78 74 65 6e 73 69 6f 6e  t nAutoExtension
1a420 20 3d 20 30 3b 0a 73 74 61 74 69 63 20 76 6f 69   = 0;.static voi
1a421 64 20 2a 2a 61 41 75 74 6f 45 78 74 65 6e 73 69  d **aAutoExtensi
1a422 6f 6e 20 3d 20 30 3b 0a 0a 0a 2f 2a 0a 2a 2a 20  on = 0;.../*.** 
1a423 52 65 67 69 73 74 65 72 20 61 20 73 74 61 74 69  Register a stati
1a424 63 61 6c 6c 79 20 6c 69 6e 6b 65 64 20 65 78 74  cally linked ext
1a425 65 6e 73 69 6f 6e 20 74 68 61 74 20 69 73 20 61  ension that is a
1a426 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20  utomatically.** 
1a427 6c 6f 61 64 65 64 20 62 79 20 65 76 65 72 79 20  loaded by every 
1a428 6e 65 77 20 64 61 74 61 62 61 73 65 20 63 6f 6e  new database con
1a429 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 69 6e 74 20  nection..*/.int 
1a42a 73 71 6c 69 74 65 33 5f 61 75 74 6f 5f 65 78 74  sqlite3_auto_ext
1a42b 65 6e 73 69 6f 6e 28 76 6f 69 64 20 2a 78 49 6e  ension(void *xIn
1a42c 69 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  it){.  int i;.  
1a42d 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
1a42e 4f 4b 3b 0a 20 20 73 71 6c 69 74 65 33 4f 73 45  OK;.  sqlite3OsE
1a42f 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 66  nterMutex();.  f
1a430 6f 72 28 69 3d 30 3b 20 69 3c 6e 41 75 74 6f 45  or(i=0; i<nAutoE
1a431 78 74 65 6e 73 69 6f 6e 3b 20 69 2b 2b 29 7b 0a  xtension; i++){.
1a432 20 20 20 20 69 66 28 20 61 41 75 74 6f 45 78 74      if( aAutoExt
1a433 65 6e 73 69 6f 6e 5b 69 5d 3d 3d 78 49 6e 69 74  ension[i]==xInit
1a434 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20   ) break;.  }.  
1a435 69 66 28 20 69 3d 3d 6e 41 75 74 6f 45 78 74 65  if( i==nAutoExte
1a436 6e 73 69 6f 6e 20 29 7b 0a 20 20 20 20 6e 41 75  nsion ){.    nAu
1a437 74 6f 45 78 74 65 6e 73 69 6f 6e 2b 2b 3b 0a 20  toExtension++;. 
1a438 20 20 20 61 41 75 74 6f 45 78 74 65 6e 73 69 6f     aAutoExtensio
1a439 6e 20 3d 20 73 71 6c 69 74 65 33 52 65 61 6c 6c  n = sqlite3Reall
1a43a 6f 63 28 20 61 41 75 74 6f 45 78 74 65 6e 73 69  oc( aAutoExtensi
1a43b 6f 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  on,.            
1a43c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a43d 20 20 20 20 20 20 20 20 20 6e 41 75 74 6f 45 78           nAutoEx
1a43e 74 65 6e 73 69 6f 6e 2a 73 69 7a 65 6f 66 28 61  tension*sizeof(a
1a43f 41 75 74 6f 45 78 74 65 6e 73 69 6f 6e 5b 30 5d  AutoExtension[0]
1a440 29 20 29 3b 0a 20 20 20 20 69 66 28 20 61 41 75  ) );.    if( aAu
1a441 74 6f 45 78 74 65 6e 73 69 6f 6e 3d 3d 30 20 29  toExtension==0 )
1a442 7b 0a 20 20 20 20 20 20 6e 41 75 74 6f 45 78 74  {.      nAutoExt
1a443 65 6e 73 69 6f 6e 20 3d 20 30 3b 0a 20 20 20 20  ension = 0;.    
1a444 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
1a445 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  MEM;.    }else{.
1a446 20 20 20 20 20 20 61 41 75 74 6f 45 78 74 65 6e        aAutoExten
1a447 73 69 6f 6e 5b 6e 41 75 74 6f 45 78 74 65 6e 73  sion[nAutoExtens
1a448 69 6f 6e 2d 31 5d 20 3d 20 78 49 6e 69 74 3b 0a  ion-1] = xInit;.
1a449 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69      }.  }.  sqli
1a44a 74 65 33 4f 73 4c 65 61 76 65 4d 75 74 65 78 28  te3OsLeaveMutex(
1a44b 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 72 63  );.  assert( (rc
1a44c 26 30 78 66 66 29 3d 3d 72 63 20 29 3b 0a 20 20  &0xff)==rc );.  
1a44d 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1a44e 0a 2a 2a 20 52 65 73 65 74 20 74 68 65 20 61 75  .** Reset the au
1a44f 74 6f 6d 61 74 69 63 20 65 78 74 65 6e 73 69 6f  tomatic extensio
1a450 6e 20 6c 6f 61 64 69 6e 67 20 6d 65 63 68 61 6e  n loading mechan
1a451 69 73 6d 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c  ism..*/.void sql
1a452 69 74 65 33 5f 72 65 73 65 74 5f 61 75 74 6f 5f  ite3_reset_auto_
1a453 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64 29 7b  extension(void){
1a454 0a 20 20 73 71 6c 69 74 65 33 4f 73 45 6e 74 65  .  sqlite3OsEnte
1a455 72 4d 75 74 65 78 28 29 3b 0a 20 20 73 71 6c 69  rMutex();.  sqli
1a456 74 65 46 72 65 65 28 61 41 75 74 6f 45 78 74 65  teFree(aAutoExte
1a457 6e 73 69 6f 6e 29 3b 0a 20 20 61 41 75 74 6f 45  nsion);.  aAutoE
1a458 78 74 65 6e 73 69 6f 6e 20 3d 20 30 3b 0a 20 20  xtension = 0;.  
1a459 6e 41 75 74 6f 45 78 74 65 6e 73 69 6f 6e 20 3d  nAutoExtension =
1a45a 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 4f 73 4c   0;.  sqlite3OsL
1a45b 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a  eaveMutex();.}..
1a45c 2f 2a 0a 2a 2a 20 4c 6f 61 64 20 61 6c 6c 20 61  /*.** Load all a
1a45d 75 74 6f 6d 61 74 69 63 20 65 78 74 65 6e 73 69  utomatic extensi
1a45e 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ons..*/.SQLITE_P
1a45f 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1a460 65 33 41 75 74 6f 4c 6f 61 64 45 78 74 65 6e 73  e3AutoLoadExtens
1a461 69 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a 64 62  ions(sqlite3 *db
1a462 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  ){.  int i;.  in
1a463 74 20 67 6f 20 3d 20 31 3b 0a 20 20 69 6e 74 20  t go = 1;.  int 
1a464 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1a465 20 20 69 6e 74 20 28 2a 78 49 6e 69 74 29 28 73    int (*xInit)(s
1a466 71 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2a 2c 63  qlite3*,char**,c
1a467 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69  onst sqlite3_api
1a468 5f 72 6f 75 74 69 6e 65 73 2a 29 3b 0a 0a 20 20  _routines*);..  
1a469 69 66 28 20 6e 41 75 74 6f 45 78 74 65 6e 73 69  if( nAutoExtensi
1a46a 6f 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20  on==0 ){.    /* 
1a46b 43 6f 6d 6d 6f 6e 20 63 61 73 65 3a 20 65 61 72  Common case: ear
1a46c 6c 79 20 6f 75 74 20 77 69 74 68 6f 75 74 20 65  ly out without e
1a46d 76 65 72 79 20 68 61 76 69 6e 67 20 74 6f 20 61  very having to a
1a46e 63 71 75 69 72 65 20 61 20 6d 75 74 65 78 20 2a  cquire a mutex *
1a46f 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  /.    return SQL
1a470 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 66 6f  ITE_OK;.  }.  fo
1a471 72 28 69 3d 30 3b 20 67 6f 3b 20 69 2b 2b 29 7b  r(i=0; go; i++){
1a472 0a 20 20 20 20 63 68 61 72 20 2a 7a 45 72 72 6d  .    char *zErrm
1a473 73 67 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69  sg = 0;.    sqli
1a474 74 65 33 4f 73 45 6e 74 65 72 4d 75 74 65 78 28  te3OsEnterMutex(
1a475 29 3b 0a 20 20 20 20 69 66 28 20 69 3e 3d 6e 41  );.    if( i>=nA
1a476 75 74 6f 45 78 74 65 6e 73 69 6f 6e 20 29 7b 0a  utoExtension ){.
1a477 20 20 20 20 20 20 78 49 6e 69 74 20 3d 20 30 3b        xInit = 0;
1a478 0a 20 20 20 20 20 20 67 6f 20 3d 20 30 3b 0a 20  .      go = 0;. 
1a479 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1a47a 78 49 6e 69 74 20 3d 20 28 69 6e 74 28 2a 29 28  xInit = (int(*)(
1a47b 73 71 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2a 2c  sqlite3*,char**,
1a47c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70  const sqlite3_ap
1a47d 69 5f 72 6f 75 74 69 6e 65 73 2a 29 29 0a 20 20  i_routines*)).  
1a47e 20 20 20 20 20 20 20 20 20 20 20 20 61 41 75 74              aAut
1a47f 6f 45 78 74 65 6e 73 69 6f 6e 5b 69 5d 3b 0a 20  oExtension[i];. 
1a480 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
1a481 4f 73 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a  OsLeaveMutex();.
1a482 20 20 20 20 69 66 28 20 78 49 6e 69 74 20 26 26      if( xInit &&
1a483 20 78 49 6e 69 74 28 64 62 2c 20 26 7a 45 72 72   xInit(db, &zErr
1a484 6d 73 67 2c 20 26 73 71 6c 69 74 65 33 5f 61 70  msg, &sqlite3_ap
1a485 69 73 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  is) ){.      sql
1a486 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51  ite3Error(db, SQ
1a487 4c 49 54 45 5f 45 52 52 4f 52 2c 0a 20 20 20 20  LITE_ERROR,.    
1a488 20 20 20 20 20 20 20 20 22 61 75 74 6f 6d 61 74          "automat
1a489 69 63 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61  ic extension loa
1a48a 64 69 6e 67 20 66 61 69 6c 65 64 3a 20 25 73 22  ding failed: %s"
1a48b 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20 20  , zErrmsg);.    
1a48c 20 20 67 6f 20 3d 20 30 3b 0a 20 20 20 20 20 20    go = 0;.      
1a48d 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
1a48e 52 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  R;.    }.  }.  r
1a48f 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6e  eturn rc;.}..#en
1a490 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
1a491 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f  IT_LOAD_EXTENSIO
1a492 4e 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  N */../*********
1a493 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6c 6f 61  ***** End of loa
1a494 64 65 78 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  dext.c *********
1a495 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a496 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a497 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
1a498 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
1a499 20 70 72 61 67 6d 61 2e 63 20 2a 2a 2a 2a 2a 2a   pragma.c ******
1a49a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a49b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a49c 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33  ****/./*.** 2003
1a49d 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54   April 6.**.** T
1a49e 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
1a49f 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
1a4a0 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
1a4a1 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
1a4a2 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
1a4a3 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
1a4a4 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
1a4a5 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
1a4a6 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
1a4a7 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
1a4a8 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
1a4a9 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
1a4aa 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
1a4ab 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
1a4ac 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
1a4ad 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
1a4ae 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
1a4af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a4b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a4b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a4b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a4b3 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
1a4b4 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f  file contains co
1a4b5 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  de used to imple
1a4b6 6d 65 6e 74 20 74 68 65 20 50 52 41 47 4d 41 20  ment the PRAGMA 
1a4b7 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 24  command..**.** $
1a4b8 49 64 3a 20 70 72 61 67 6d 61 2e 63 2c 76 20 31  Id: pragma.c,v 1
1a4b9 2e 31 34 32 20 32 30 30 37 2f 30 36 2f 32 36 20  .142 2007/06/26 
1a4ba 31 30 3a 33 38 3a 35 35 20 64 61 6e 69 65 6c 6b  10:38:55 danielk
1a4bb 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f  1977 Exp $.*/../
1a4bc 2a 20 49 67 6e 6f 72 65 20 74 68 69 73 20 77 68  * Ignore this wh
1a4bd 6f 6c 65 20 66 69 6c 65 20 69 66 20 70 72 61 67  ole file if prag
1a4be 6d 61 73 20 61 72 65 20 64 69 73 61 62 6c 65 64  mas are disabled
1a4bf 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64  .*/.#if !defined
1a4c0 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 41  (SQLITE_OMIT_PRA
1a4c1 47 4d 41 29 20 26 26 20 21 64 65 66 69 6e 65 64  GMA) && !defined
1a4c2 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 52  (SQLITE_OMIT_PAR
1a4c3 53 45 52 29 0a 0a 23 69 66 20 64 65 66 69 6e 65  SER)..#if define
1a4c4 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20  d(SQLITE_DEBUG) 
1a4c5 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  || defined(SQLIT
1a4c6 45 5f 54 45 53 54 29 0a 23 65 6e 64 69 66 0a 0a  E_TEST).#endif..
1a4c7 2f 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20  /*.** Interpret 
1a4c8 74 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e 67  the given string
1a4c9 20 61 73 20 61 20 73 61 66 65 74 79 20 6c 65 76   as a safety lev
1a4ca 65 6c 2e 20 20 52 65 74 75 72 6e 20 30 20 66 6f  el.  Return 0 fo
1a4cb 72 20 4f 46 46 2c 0a 2a 2a 20 31 20 66 6f 72 20  r OFF,.** 1 for 
1a4cc 4f 4e 20 6f 72 20 4e 4f 52 4d 41 4c 20 61 6e 64  ON or NORMAL and
1a4cd 20 32 20 66 6f 72 20 46 55 4c 4c 2e 20 20 52 65   2 for FULL.  Re
1a4ce 74 75 72 6e 20 31 20 66 6f 72 20 61 6e 20 65 6d  turn 1 for an em
1a4cf 70 74 79 20 6f 72 20 0a 2a 2a 20 75 6e 72 65 63  pty or .** unrec
1a4d0 6f 67 6e 69 7a 65 64 20 73 74 72 69 6e 67 20 61  ognized string a
1a4d1 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4e  rgument..**.** N
1a4d2 6f 74 65 20 74 68 61 74 20 74 68 65 20 76 61 6c  ote that the val
1a4d3 75 65 73 20 72 65 74 75 72 6e 65 64 20 61 72 65  ues returned are
1a4d4 20 6f 6e 65 20 6c 65 73 73 20 74 68 61 74 20 74   one less that t
1a4d5 68 65 20 76 61 6c 75 65 73 20 74 68 61 74 0a 2a  he values that.*
1a4d6 2a 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73  * should be pass
1a4d7 65 64 20 69 6e 74 6f 20 73 71 6c 69 74 65 33 42  ed into sqlite3B
1a4d8 74 72 65 65 53 65 74 53 61 66 65 74 79 4c 65 76  treeSetSafetyLev
1a4d9 65 6c 28 29 2e 20 20 54 68 65 20 69 73 20 64 6f  el().  The is do
1a4da 6e 65 0a 2a 2a 20 74 6f 20 73 75 70 70 6f 72 74  ne.** to support
1a4db 20 6c 65 67 61 63 79 20 53 51 4c 20 63 6f 64 65   legacy SQL code
1a4dc 2e 20 20 54 68 65 20 73 61 66 65 74 79 20 6c 65  .  The safety le
1a4dd 76 65 6c 20 75 73 65 64 20 74 6f 20 62 65 20 62  vel used to be b
1a4de 6f 6f 6c 65 61 6e 0a 2a 2a 20 61 6e 64 20 6f 6c  oolean.** and ol
1a4df 64 65 72 20 73 63 72 69 70 74 73 20 6d 61 79 20  der scripts may 
1a4e0 68 61 76 65 20 75 73 65 64 20 6e 75 6d 62 65 72  have used number
1a4e1 73 20 30 20 66 6f 72 20 4f 46 46 20 61 6e 64 20  s 0 for OFF and 
1a4e2 31 20 66 6f 72 20 4f 4e 2e 0a 2a 2f 0a 73 74 61  1 for ON..*/.sta
1a4e3 74 69 63 20 69 6e 74 20 67 65 74 53 61 66 65 74  tic int getSafet
1a4e4 79 4c 65 76 65 6c 28 63 6f 6e 73 74 20 63 68 61  yLevel(const cha
1a4e5 72 20 2a 7a 29 7b 0a 20 20 20 20 20 20 20 20 20  r *z){.         
1a4e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a4e7 20 20 20 20 2f 2a 20 31 32 33 34 35 36 37 38 39      /* 123456789
1a4e8 20 31 32 33 34 35 36 37 38 39 20 2a 2f 0a 20 20   123456789 */.  
1a4e9 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
1a4ea 72 20 7a 54 65 78 74 5b 5d 20 3d 20 22 6f 6e 6f  r zText[] = "ono
1a4eb 66 66 61 6c 73 65 79 65 73 74 72 75 65 66 75 6c  ffalseyestrueful
1a4ec 6c 22 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e  l";.  static con
1a4ed 73 74 20 75 38 20 69 4f 66 66 73 65 74 5b 5d 20  st u8 iOffset[] 
1a4ee 3d 20 7b 30 2c 20 31 2c 20 32 2c 20 34 2c 20 39  = {0, 1, 2, 4, 9
1a4ef 2c 20 31 32 2c 20 31 36 7d 3b 0a 20 20 73 74 61  , 12, 16};.  sta
1a4f0 74 69 63 20 63 6f 6e 73 74 20 75 38 20 69 4c 65  tic const u8 iLe
1a4f1 6e 67 74 68 5b 5d 20 3d 20 7b 32 2c 20 32 2c 20  ngth[] = {2, 2, 
1a4f2 33 2c 20 35 2c 20 33 2c 20 34 2c 20 34 7d 3b 0a  3, 5, 3, 4, 4};.
1a4f3 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
1a4f4 38 20 69 56 61 6c 75 65 5b 5d 20 3d 20 20 7b 31  8 iValue[] =  {1
1a4f5 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20 31 2c  , 0, 0, 0, 1, 1,
1a4f6 20 32 7d 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b   2};.  int i, n;
1a4f7 0a 20 20 69 66 28 20 69 73 64 69 67 69 74 28 2a  .  if( isdigit(*
1a4f8 7a 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  z) ){.    return
1a4f9 20 61 74 6f 69 28 7a 29 3b 0a 20 20 7d 0a 20 20   atoi(z);.  }.  
1a4fa 6e 20 3d 20 73 74 72 6c 65 6e 28 7a 29 3b 0a 20  n = strlen(z);. 
1a4fb 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65   for(i=0; i<size
1a4fc 6f 66 28 69 4c 65 6e 67 74 68 29 3b 20 69 2b 2b  of(iLength); i++
1a4fd 29 7b 0a 20 20 20 20 69 66 28 20 69 4c 65 6e 67  ){.    if( iLeng
1a4fe 74 68 5b 69 5d 3d 3d 6e 20 26 26 20 73 71 6c 69  th[i]==n && sqli
1a4ff 74 65 33 53 74 72 4e 49 43 6d 70 28 26 7a 54 65  te3StrNICmp(&zTe
1a500 78 74 5b 69 4f 66 66 73 65 74 5b 69 5d 5d 2c 7a  xt[iOffset[i]],z
1a501 2c 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ,n)==0 ){.      
1a502 72 65 74 75 72 6e 20 69 56 61 6c 75 65 5b 69 5d  return iValue[i]
1a503 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
1a504 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 1;.}../*.**
1a505 20 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 67   Interpret the g
1a506 69 76 65 6e 20 73 74 72 69 6e 67 20 61 73 20 61  iven string as a
1a507 20 62 6f 6f 6c 65 61 6e 20 76 61 6c 75 65 2e 0a   boolean value..
1a508 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65  */.static int ge
1a509 74 42 6f 6f 6c 65 61 6e 28 63 6f 6e 73 74 20 63  tBoolean(const c
1a50a 68 61 72 20 2a 7a 29 7b 0a 20 20 72 65 74 75 72  har *z){.  retur
1a50b 6e 20 67 65 74 53 61 66 65 74 79 4c 65 76 65 6c  n getSafetyLevel
1a50c 28 7a 29 26 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  (z)&1;.}../*.** 
1a50d 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 67 69  Interpret the gi
1a50e 76 65 6e 20 73 74 72 69 6e 67 20 61 73 20 61 20  ven string as a 
1a50f 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 76 61 6c  locking mode val
1a510 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ue..*/.static in
1a511 74 20 67 65 74 4c 6f 63 6b 69 6e 67 4d 6f 64 65  t getLockingMode
1a512 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b  (const char *z){
1a513 0a 20 20 69 66 28 20 7a 20 29 7b 0a 20 20 20 20  .  if( z ){.    
1a514 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 53 74  if( 0==sqlite3St
1a515 72 49 43 6d 70 28 7a 2c 20 22 65 78 63 6c 75 73  rICmp(z, "exclus
1a516 69 76 65 22 29 20 29 20 72 65 74 75 72 6e 20 50  ive") ) return P
1a517 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
1a518 5f 45 58 43 4c 55 53 49 56 45 3b 0a 20 20 20 20  _EXCLUSIVE;.    
1a519 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 53 74  if( 0==sqlite3St
1a51a 72 49 43 6d 70 28 7a 2c 20 22 6e 6f 72 6d 61 6c  rICmp(z, "normal
1a51b 22 29 20 29 20 72 65 74 75 72 6e 20 50 41 47 45  ") ) return PAGE
1a51c 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f  R_LOCKINGMODE_NO
1a51d 52 4d 41 4c 3b 0a 20 20 7d 0a 20 20 72 65 74 75  RMAL;.  }.  retu
1a51e 72 6e 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  rn PAGER_LOCKING
1a51f 4d 4f 44 45 5f 51 55 45 52 59 3b 0a 7d 0a 0a 23  MODE_QUERY;.}..#
1a520 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1a521 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a  IT_AUTOVACUUM./*
1a522 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74 68  .** Interpret th
1a523 65 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20 61  e given string a
1a524 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d  s an auto-vacuum
1a525 20 6d 6f 64 65 20 76 61 6c 75 65 2e 0a 2a 2a 0a   mode value..**.
1a526 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
1a527 20 73 74 72 69 6e 67 73 2c 20 22 6e 6f 6e 65 22   strings, "none"
1a528 2c 20 22 66 75 6c 6c 22 20 61 6e 64 20 22 69 6e  , "full" and "in
1a529 63 72 65 6d 65 6e 74 61 6c 22 20 61 72 65 20 0a  cremental" are .
1a52a 2a 2a 20 61 63 63 65 70 74 61 62 6c 65 2c 20 61  ** acceptable, a
1a52b 73 20 61 72 65 20 74 68 65 69 72 20 6e 75 6d 65  s are their nume
1a52c 72 69 63 20 65 71 75 69 76 61 6c 65 6e 74 73 3a  ric equivalents:
1a52d 20 30 2c 20 31 20 61 6e 64 20 32 20 72 65 73 70   0, 1 and 2 resp
1a52e 65 63 74 69 76 65 6c 79 2e 0a 2a 2f 0a 73 74 61  ectively..*/.sta
1a52f 74 69 63 20 69 6e 74 20 67 65 74 41 75 74 6f 56  tic int getAutoV
1a530 61 63 75 75 6d 28 63 6f 6e 73 74 20 63 68 61 72  acuum(const char
1a531 20 2a 7a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20   *z){.  int i;. 
1a532 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 53   if( 0==sqlite3S
1a533 74 72 49 43 6d 70 28 7a 2c 20 22 6e 6f 6e 65 22  trICmp(z, "none"
1a534 29 20 29 20 72 65 74 75 72 6e 20 42 54 52 45 45  ) ) return BTREE
1a535 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e 45  _AUTOVACUUM_NONE
1a536 3b 0a 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74  ;.  if( 0==sqlit
1a537 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 66 75  e3StrICmp(z, "fu
1a538 6c 6c 22 29 20 29 20 72 65 74 75 72 6e 20 42 54  ll") ) return BT
1a539 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 46  REE_AUTOVACUUM_F
1a53a 55 4c 4c 3b 0a 20 20 69 66 28 20 30 3d 3d 73 71  ULL;.  if( 0==sq
1a53b 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20  lite3StrICmp(z, 
1a53c 22 69 6e 63 72 65 6d 65 6e 74 61 6c 22 29 20 29  "incremental") )
1a53d 20 72 65 74 75 72 6e 20 42 54 52 45 45 5f 41 55   return BTREE_AU
1a53e 54 4f 56 41 43 55 55 4d 5f 49 4e 43 52 3b 0a 20  TOVACUUM_INCR;. 
1a53f 20 69 20 3d 20 61 74 6f 69 28 7a 29 3b 0a 20 20   i = atoi(z);.  
1a540 72 65 74 75 72 6e 20 28 28 69 3e 3d 30 26 26 69  return ((i>=0&&i
1a541 3c 3d 32 29 3f 69 3a 30 29 3b 0a 7d 0a 23 65 6e  <=2)?i:0);.}.#en
1a542 64 69 66 20 2f 2a 20 69 66 6e 64 65 66 20 53 51  dif /* ifndef SQ
1a543 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
1a544 43 55 55 4d 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  CUUM */..#ifndef
1a545 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47   SQLITE_OMIT_PAG
1a546 45 52 5f 50 52 41 47 4d 41 53 0a 2f 2a 0a 2a 2a  ER_PRAGMAS./*.**
1a547 20 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 67   Interpret the g
1a548 69 76 65 6e 20 73 74 72 69 6e 67 20 61 73 20 61  iven string as a
1a549 20 74 65 6d 70 20 64 62 20 6c 6f 63 61 74 69 6f   temp db locatio
1a54a 6e 2e 20 52 65 74 75 72 6e 20 31 20 66 6f 72 20  n. Return 1 for 
1a54b 66 69 6c 65 0a 2a 2a 20 62 61 63 6b 65 64 20 74  file.** backed t
1a54c 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73  emporary databas
1a54d 65 73 2c 20 32 20 66 6f 72 20 74 68 65 20 52 65  es, 2 for the Re
1a54e 64 2d 42 6c 61 63 6b 20 74 72 65 65 20 69 6e 20  d-Black tree in 
1a54f 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 0a  memory database.
1a550 2a 2a 20 61 6e 64 20 30 20 74 6f 20 75 73 65 20  ** and 0 to use 
1a551 74 68 65 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65  the compile-time
1a552 20 64 65 66 61 75 6c 74 2e 0a 2a 2f 0a 73 74 61   default..*/.sta
1a553 74 69 63 20 69 6e 74 20 67 65 74 54 65 6d 70 53  tic int getTempS
1a554 74 6f 72 65 28 63 6f 6e 73 74 20 63 68 61 72 20  tore(const char 
1a555 2a 7a 29 7b 0a 20 20 69 66 28 20 7a 5b 30 5d 3e  *z){.  if( z[0]>
1a556 3d 27 30 27 20 26 26 20 7a 5b 30 5d 3c 3d 27 32  ='0' && z[0]<='2
1a557 27 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ' ){.    return 
1a558 7a 5b 30 5d 20 2d 20 27 30 27 3b 0a 20 20 7d 65  z[0] - '0';.  }e
1a559 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 53  lse if( sqlite3S
1a55a 74 72 49 43 6d 70 28 7a 2c 20 22 66 69 6c 65 22  trICmp(z, "file"
1a55b 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  )==0 ){.    retu
1a55c 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66  rn 1;.  }else if
1a55d 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
1a55e 28 7a 2c 20 22 6d 65 6d 6f 72 79 22 29 3d 3d 30  (z, "memory")==0
1a55f 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 32   ){.    return 2
1a560 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
1a561 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 23  eturn 0;.  }.}.#
1a562 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
1a563 50 41 47 45 52 5f 50 52 41 47 4d 41 53 20 2a 2f  PAGER_PRAGMAS */
1a564 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1a565 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47  _OMIT_PAGER_PRAG
1a566 4d 41 53 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69  MAS./*.** Invali
1a567 64 61 74 65 20 74 65 6d 70 20 73 74 6f 72 61 67  date temp storag
1a568 65 2c 20 65 69 74 68 65 72 20 77 68 65 6e 20 74  e, either when t
1a569 68 65 20 74 65 6d 70 20 73 74 6f 72 61 67 65 20  he temp storage 
1a56a 69 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 66 72  is changed.** fr
1a56b 6f 6d 20 64 65 66 61 75 6c 74 2c 20 6f 72 20 77  om default, or w
1a56c 68 65 6e 20 27 66 69 6c 65 27 20 61 6e 64 20 74  hen 'file' and t
1a56d 68 65 20 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69  he temp_store_di
1a56e 72 65 63 74 6f 72 79 20 68 61 73 20 63 68 61 6e  rectory has chan
1a56f 67 65 64 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ged.*/.static in
1a570 74 20 69 6e 76 61 6c 69 64 61 74 65 54 65 6d 70  t invalidateTemp
1a571 53 74 6f 72 61 67 65 28 50 61 72 73 65 20 2a 70  Storage(Parse *p
1a572 50 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65  Parse){.  sqlite
1a573 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
1a574 64 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e 61 44  db;.  if( db->aD
1a575 62 5b 31 5d 2e 70 42 74 21 3d 30 20 29 7b 0a 20  b[1].pBt!=0 ){. 
1a576 20 20 20 69 66 28 20 21 64 62 2d 3e 61 75 74 6f     if( !db->auto
1a577 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 20 20  Commit ){.      
1a578 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1a579 70 50 61 72 73 65 2c 20 22 74 65 6d 70 6f 72 61  pParse, "tempora
1a57a 72 79 20 73 74 6f 72 61 67 65 20 63 61 6e 6e 6f  ry storage canno
1a57b 74 20 62 65 20 63 68 61 6e 67 65 64 20 22 0a 20  t be changed ". 
1a57c 20 20 20 20 20 20 20 22 66 72 6f 6d 20 77 69 74         "from wit
1a57d 68 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f  hin a transactio
1a57e 6e 22 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  n");.      retur
1a57f 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
1a580 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
1a581 33 42 74 72 65 65 43 6c 6f 73 65 28 64 62 2d 3e  3BtreeClose(db->
1a582 61 44 62 5b 31 5d 2e 70 42 74 29 3b 0a 20 20 20  aDb[1].pBt);.   
1a583 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 20   db->aDb[1].pBt 
1a584 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  = 0;.    sqlite3
1a585 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68  ResetInternalSch
1a586 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 7d 0a  ema(db, 0);.  }.
1a587 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1a588 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  OK;.}.#endif /* 
1a589 53 51 4c 49 54 45 5f 50 41 47 45 52 5f 50 52 41  SQLITE_PAGER_PRA
1a58a 47 4d 41 53 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  GMAS */..#ifndef
1a58b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47   SQLITE_OMIT_PAG
1a58c 45 52 5f 50 52 41 47 4d 41 53 0a 2f 2a 0a 2a 2a  ER_PRAGMAS./*.**
1a58d 20 49 66 20 74 68 65 20 54 45 4d 50 20 64 61 74   If the TEMP dat
1a58e 61 62 61 73 65 20 69 73 20 6f 70 65 6e 2c 20 63  abase is open, c
1a58f 6c 6f 73 65 20 69 74 20 61 6e 64 20 6d 61 72 6b  lose it and mark
1a590 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
1a591 68 65 6d 61 0a 2a 2a 20 61 73 20 6e 65 65 64 69  hema.** as needi
1a592 6e 67 20 72 65 6c 6f 61 64 69 6e 67 2e 20 20 54  ng reloading.  T
1a593 68 69 73 20 6d 75 73 74 20 62 65 20 64 6f 6e 65  his must be done
1a594 20 77 68 65 6e 20 75 73 69 6e 67 20 74 68 65 20   when using the 
1a595 54 45 4d 50 5f 53 54 4f 52 45 0a 2a 2a 20 6f 72  TEMP_STORE.** or
1a596 20 44 45 46 41 55 4c 54 5f 54 45 4d 50 5f 53 54   DEFAULT_TEMP_ST
1a597 4f 52 45 20 70 72 61 67 6d 61 73 2e 0a 2a 2f 0a  ORE pragmas..*/.
1a598 73 74 61 74 69 63 20 69 6e 74 20 63 68 61 6e 67  static int chang
1a599 65 54 65 6d 70 53 74 6f 72 61 67 65 28 50 61 72  eTempStorage(Par
1a59a 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73  se *pParse, cons
1a59b 74 20 63 68 61 72 20 2a 7a 53 74 6f 72 61 67 65  t char *zStorage
1a59c 54 79 70 65 29 7b 0a 20 20 69 6e 74 20 74 73 20  Type){.  int ts 
1a59d 3d 20 67 65 74 54 65 6d 70 53 74 6f 72 65 28 7a  = getTempStore(z
1a59e 53 74 6f 72 61 67 65 54 79 70 65 29 3b 0a 20 20  StorageType);.  
1a59f 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
1a5a0 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20  arse->db;.  if( 
1a5a1 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 3d 3d  db->temp_store==
1a5a2 74 73 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  ts ) return SQLI
1a5a3 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 6e 76  TE_OK;.  if( inv
1a5a4 61 6c 69 64 61 74 65 54 65 6d 70 53 74 6f 72 61  alidateTempStora
1a5a5 67 65 28 20 70 50 61 72 73 65 20 29 20 21 3d 20  ge( pParse ) != 
1a5a6 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1a5a7 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
1a5a8 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e  RROR;.  }.  db->
1a5a9 74 65 6d 70 5f 73 74 6f 72 65 20 3d 20 74 73 3b  temp_store = ts;
1a5aa 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
1a5ab 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  _OK;.}.#endif /*
1a5ac 20 53 51 4c 49 54 45 5f 50 41 47 45 52 5f 50 52   SQLITE_PAGER_PR
1a5ad 41 47 4d 41 53 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  AGMAS */../*.** 
1a5ae 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
1a5af 20 72 65 74 75 72 6e 20 61 20 73 69 6e 67 6c 65   return a single
1a5b0 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 2e 0a   integer value..
1a5b1 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72  */.static void r
1a5b2 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 50  eturnSingleInt(P
1a5b3 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f  arse *pParse, co
1a5b4 6e 73 74 20 63 68 61 72 20 2a 7a 4c 61 62 65 6c  nst char *zLabel
1a5b5 2c 20 69 6e 74 20 76 61 6c 75 65 29 7b 0a 20 20  , int value){.  
1a5b6 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65  Vdbe *v = sqlite
1a5b7 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
1a5b8 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
1a5b9 64 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 74 65 67  ddOp(v, OP_Integ
1a5ba 65 72 2c 20 76 61 6c 75 65 2c 20 30 29 3b 0a 20  er, value, 0);. 
1a5bb 20 69 66 28 20 70 50 61 72 73 65 2d 3e 65 78 70   if( pParse->exp
1a5bc 6c 61 69 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 73  lain==0 ){.    s
1a5bd 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
1a5be 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20  Cols(v, 1);.    
1a5bf 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
1a5c0 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
1a5c1 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4c 61 62 65 6c  AME_NAME, zLabel
1a5c2 2c 20 50 33 5f 53 54 41 54 49 43 29 3b 0a 20 20  , P3_STATIC);.  
1a5c3 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  }.  sqlite3VdbeA
1a5c4 64 64 4f 70 28 76 2c 20 4f 50 5f 43 61 6c 6c 62  ddOp(v, OP_Callb
1a5c5 61 63 6b 2c 20 31 2c 20 30 29 3b 0a 7d 0a 0a 23  ack, 1, 0);.}..#
1a5c6 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1a5c7 49 54 5f 46 4c 41 47 5f 50 52 41 47 4d 41 53 0a  IT_FLAG_PRAGMAS.
1a5c8 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73  /*.** Check to s
1a5c9 65 65 20 69 66 20 7a 52 69 67 68 74 20 61 6e 64  ee if zRight and
1a5ca 20 7a 4c 65 66 74 20 72 65 66 65 72 20 74 6f 20   zLeft refer to 
1a5cb 61 20 70 72 61 67 6d 61 20 74 68 61 74 20 71 75  a pragma that qu
1a5cc 65 72 69 65 73 0a 2a 2a 20 6f 72 20 63 68 61 6e  eries.** or chan
1a5cd 67 65 73 20 6f 6e 65 20 6f 66 20 74 68 65 20 66  ges one of the f
1a5ce 6c 61 67 73 20 69 6e 20 64 62 2d 3e 66 6c 61 67  lags in db->flag
1a5cf 73 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66 20  s.  Return 1 if 
1a5d0 73 6f 20 61 6e 64 20 30 20 69 66 20 6e 6f 74 2e  so and 0 if not.
1a5d1 0a 2a 2a 20 41 6c 73 6f 2c 20 69 6d 70 6c 65 6d  .** Also, implem
1a5d2 65 6e 74 20 74 68 65 20 70 72 61 67 6d 61 2e 0a  ent the pragma..
1a5d3 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c  */.static int fl
1a5d4 61 67 50 72 61 67 6d 61 28 50 61 72 73 65 20 2a  agPragma(Parse *
1a5d5 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68  pParse, const ch
1a5d6 61 72 20 2a 7a 4c 65 66 74 2c 20 63 6f 6e 73 74  ar *zLeft, const
1a5d7 20 63 68 61 72 20 2a 7a 52 69 67 68 74 29 7b 0a   char *zRight){.
1a5d8 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73    static const s
1a5d9 74 72 75 63 74 20 73 50 72 61 67 6d 61 54 79 70  truct sPragmaTyp
1a5da 65 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  e {.    const ch
1a5db 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 2f 2a 20 4e  ar *zName;  /* N
1a5dc 61 6d 65 20 6f 66 20 74 68 65 20 70 72 61 67 6d  ame of the pragm
1a5dd 61 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6d 61 73  a */.    int mas
1a5de 6b 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  k;           /* 
1a5df 4d 61 73 6b 20 66 6f 72 20 74 68 65 20 64 62 2d  Mask for the db-
1a5e0 3e 66 6c 61 67 73 20 76 61 6c 75 65 20 2a 2f 0a  >flags value */.
1a5e1 20 20 7d 20 61 50 72 61 67 6d 61 5b 5d 20 3d 20    } aPragma[] = 
1a5e2 7b 0a 20 20 20 20 7b 20 22 66 75 6c 6c 5f 63 6f  {.    { "full_co
1a5e3 6c 75 6d 6e 5f 6e 61 6d 65 73 22 2c 20 20 20 20  lumn_names",    
1a5e4 20 20 20 20 53 51 4c 49 54 45 5f 46 75 6c 6c 43      SQLITE_FullC
1a5e5 6f 6c 4e 61 6d 65 73 20 20 7d 2c 0a 20 20 20 20  olNames  },.    
1a5e6 7b 20 22 73 68 6f 72 74 5f 63 6f 6c 75 6d 6e 5f  { "short_column_
1a5e7 6e 61 6d 65 73 22 2c 20 20 20 20 20 20 20 53 51  names",       SQ
1a5e8 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d  LITE_ShortColNam
1a5e9 65 73 20 7d 2c 0a 20 20 20 20 7b 20 22 63 6f 75  es },.    { "cou
1a5ea 6e 74 5f 63 68 61 6e 67 65 73 22 2c 20 20 20 20  nt_changes",    
1a5eb 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43          SQLITE_C
1a5ec 6f 75 6e 74 52 6f 77 73 20 20 20 20 20 7d 2c 0a  ountRows     },.
1a5ed 20 20 20 20 7b 20 22 65 6d 70 74 79 5f 72 65 73      { "empty_res
1a5ee 75 6c 74 5f 63 61 6c 6c 62 61 63 6b 73 22 2c 20  ult_callbacks", 
1a5ef 20 20 53 51 4c 49 54 45 5f 4e 75 6c 6c 43 61 6c    SQLITE_NullCal
1a5f0 6c 62 61 63 6b 20 20 7d 2c 0a 20 20 20 20 7b 20  lback  },.    { 
1a5f1 22 6c 65 67 61 63 79 5f 66 69 6c 65 5f 66 6f 72  "legacy_file_for
1a5f2 6d 61 74 22 2c 20 20 20 20 20 20 20 53 51 4c 49  mat",       SQLI
1a5f3 54 45 5f 4c 65 67 61 63 79 46 69 6c 65 46 6d 74  TE_LegacyFileFmt
1a5f4 20 7d 2c 0a 20 20 20 20 7b 20 22 66 75 6c 6c 66   },.    { "fullf
1a5f5 73 79 6e 63 22 2c 20 20 20 20 20 20 20 20 20 20  sync",          
1a5f6 20 20 20 20 20 20 53 51 4c 49 54 45 5f 46 75 6c        SQLITE_Ful
1a5f7 6c 46 53 79 6e 63 20 20 20 20 20 7d 2c 0a 23 69  lFSync     },.#i
1a5f8 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
1a5f9 47 0a 20 20 20 20 7b 20 22 73 71 6c 5f 74 72 61  G.    { "sql_tra
1a5fa 63 65 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  ce",            
1a5fb 20 20 20 20 53 51 4c 49 54 45 5f 53 71 6c 54 72      SQLITE_SqlTr
1a5fc 61 63 65 20 20 20 20 20 20 7d 2c 0a 20 20 20 20  ace      },.    
1a5fd 7b 20 22 76 64 62 65 5f 6c 69 73 74 69 6e 67 22  { "vdbe_listing"
1a5fe 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51  ,             SQ
1a5ff 4c 49 54 45 5f 56 64 62 65 4c 69 73 74 69 6e 67  LITE_VdbeListing
1a600 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 76 64 62     },.    { "vdb
1a601 65 5f 74 72 61 63 65 22 2c 20 20 20 20 20 20 20  e_trace",       
1a602 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 56          SQLITE_V
1a603 64 62 65 54 72 61 63 65 20 20 20 20 20 7d 2c 0a  dbeTrace     },.
1a604 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53  #endif.#ifndef S
1a605 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b  QLITE_OMIT_CHECK
1a606 0a 20 20 20 20 7b 20 22 69 67 6e 6f 72 65 5f 63  .    { "ignore_c
1a607 68 65 63 6b 5f 63 6f 6e 73 74 72 61 69 6e 74 73  heck_constraints
1a608 22 2c 20 53 51 4c 49 54 45 5f 49 67 6e 6f 72 65  ", SQLITE_Ignore
1a609 43 68 65 63 6b 73 20 20 7d 2c 0a 23 65 6e 64 69  Checks  },.#endi
1a60a 66 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c  f.    /* The fol
1a60b 6c 6f 77 69 6e 67 20 69 73 20 56 45 52 59 20 65  lowing is VERY e
1a60c 78 70 65 72 69 6d 65 6e 74 61 6c 20 2a 2f 0a 20  xperimental */. 
1a60d 20 20 20 7b 20 22 77 72 69 74 61 62 6c 65 5f 73     { "writable_s
1a60e 63 68 65 6d 61 22 2c 20 20 20 20 20 20 20 20 20  chema",         
1a60f 20 53 51 4c 49 54 45 5f 57 72 69 74 65 53 63 68   SQLITE_WriteSch
1a610 65 6d 61 7c 53 51 4c 49 54 45 5f 52 65 63 6f 76  ema|SQLITE_Recov
1a611 65 72 79 4d 6f 64 65 20 7d 2c 0a 20 20 20 20 7b  eryMode },.    {
1a612 20 22 6f 6d 69 74 5f 72 65 61 64 6c 6f 63 6b 22   "omit_readlock"
1a613 2c 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c  ,            SQL
1a614 49 54 45 5f 4e 6f 52 65 61 64 6c 6f 63 6b 20 20  ITE_NoReadlock  
1a615 20 20 7d 2c 0a 0a 20 20 20 20 2f 2a 20 54 4f 44    },..    /* TOD
1a616 4f 3a 20 4d 61 79 62 65 20 69 74 20 73 68 6f 75  O: Maybe it shou
1a617 6c 64 6e 27 74 20 62 65 20 70 6f 73 73 69 62 6c  ldn't be possibl
1a618 65 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 20  e to change the 
1a619 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 0a  ReadUncommitted.
1a61a 20 20 20 20 2a 2a 20 66 6c 61 67 20 69 66 20 74      ** flag if t
1a61b 68 65 72 65 20 61 72 65 20 61 6e 79 20 61 63 74  here are any act
1a61c 69 76 65 20 73 74 61 74 65 6d 65 6e 74 73 2e 20  ive statements. 
1a61d 2a 2f 0a 20 20 20 20 7b 20 22 72 65 61 64 5f 75  */.    { "read_u
1a61e 6e 63 6f 6d 6d 69 74 74 65 64 22 2c 20 20 20 20  ncommitted",    
1a61f 20 20 20 20 20 53 51 4c 49 54 45 5f 52 65 61 64       SQLITE_Read
1a620 55 6e 63 6f 6d 6d 69 74 74 65 64 20 7d 2c 0a 20  Uncommitted },. 
1a621 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63   };.  int i;.  c
1a622 6f 6e 73 74 20 73 74 72 75 63 74 20 73 50 72 61  onst struct sPra
1a623 67 6d 61 54 79 70 65 20 2a 70 3b 0a 20 20 66 6f  gmaType *p;.  fo
1a624 72 28 69 3d 30 2c 20 70 3d 61 50 72 61 67 6d 61  r(i=0, p=aPragma
1a625 3b 20 69 3c 73 69 7a 65 6f 66 28 61 50 72 61 67  ; i<sizeof(aPrag
1a626 6d 61 29 2f 73 69 7a 65 6f 66 28 61 50 72 61 67  ma)/sizeof(aPrag
1a627 6d 61 5b 30 5d 29 3b 20 69 2b 2b 2c 20 70 2b 2b  ma[0]); i++, p++
1a628 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ){.    if( sqlit
1a629 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
1a62a 20 70 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b   p->zName)==0 ){
1a62b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 20 2a  .      sqlite3 *
1a62c 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
1a62d 0a 20 20 20 20 20 20 56 64 62 65 20 2a 76 3b 0a  .      Vdbe *v;.
1a62e 20 20 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65        v = sqlite
1a62f 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
1a630 3b 0a 20 20 20 20 20 20 69 66 28 20 76 20 29 7b  ;.      if( v ){
1a631 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 52 69  .        if( zRi
1a632 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ght==0 ){.      
1a633 20 20 20 20 72 65 74 75 72 6e 53 69 6e 67 6c 65      returnSingle
1a634 49 6e 74 28 70 50 61 72 73 65 2c 20 70 2d 3e 7a  Int(pParse, p->z
1a635 4e 61 6d 65 2c 20 28 64 62 2d 3e 66 6c 61 67 73  Name, (db->flags
1a636 20 26 20 70 2d 3e 6d 61 73 6b 29 21 3d 30 20 29   & p->mask)!=0 )
1a637 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
1a638 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 67  .          if( g
1a639 65 74 42 6f 6f 6c 65 61 6e 28 7a 52 69 67 68 74  etBoolean(zRight
1a63a 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ) ){.           
1a63b 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 70 2d   db->flags |= p-
1a63c 3e 6d 61 73 6b 3b 0a 20 20 20 20 20 20 20 20 20  >mask;.         
1a63d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1a63e 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 26 3d      db->flags &=
1a63f 20 7e 70 2d 3e 6d 61 73 6b 3b 0a 20 20 20 20 20   ~p->mask;.     
1a640 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
1a641 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
1a642 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
1a643 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
1a644 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1a645 45 5f 4f 4d 49 54 5f 46 4c 41 47 5f 50 52 41 47  E_OMIT_FLAG_PRAG
1a646 4d 41 53 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72  MAS */../*.** Pr
1a647 6f 63 65 73 73 20 61 20 70 72 61 67 6d 61 20 73  ocess a pragma s
1a648 74 61 74 65 6d 65 6e 74 2e 20 20 0a 2a 2a 0a 2a  tatement.  .**.*
1a649 2a 20 50 72 61 67 6d 61 73 20 61 72 65 20 6f 66  * Pragmas are of
1a64a 20 74 68 69 73 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a   this form:.**.*
1a64b 2a 20 20 20 20 20 20 50 52 41 47 4d 41 20 5b 64  *      PRAGMA [d
1a64c 61 74 61 62 61 73 65 2e 5d 69 64 20 5b 3d 20 76  atabase.]id [= v
1a64d 61 6c 75 65 5d 0a 2a 2a 0a 2a 2a 20 54 68 65 20  alue].**.** The 
1a64e 69 64 65 6e 74 69 66 69 65 72 20 6d 69 67 68 74  identifier might
1a64f 20 61 6c 73 6f 20 62 65 20 61 20 73 74 72 69 6e   also be a strin
1a650 67 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73  g.  The value is
1a651 20 61 20 73 74 72 69 6e 67 2c 20 61 6e 64 0a 2a   a string, and.*
1a652 2a 20 69 64 65 6e 74 69 66 69 65 72 2c 20 6f 72  * identifier, or
1a653 20 61 20 6e 75 6d 62 65 72 2e 20 20 49 66 20 6d   a number.  If m
1a654 69 6e 75 73 46 6c 61 67 20 69 73 20 74 72 75 65  inusFlag is true
1a655 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65  , then the value
1a656 20 69 73 0a 2a 2a 20 61 20 6e 75 6d 62 65 72 20   is.** a number 
1a657 74 68 61 74 20 77 61 73 20 70 72 65 63 65 64 65  that was precede
1a658 64 20 62 79 20 61 20 6d 69 6e 75 73 20 73 69 67  d by a minus sig
1a659 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  n..**.** If the 
1a65a 6c 65 66 74 20 73 69 64 65 20 69 73 20 22 64 61  left side is "da
1a65b 74 61 62 61 73 65 2e 69 64 22 20 74 68 65 6e 20  tabase.id" then 
1a65c 70 49 64 31 20 69 73 20 74 68 65 20 64 61 74 61  pId1 is the data
1a65d 62 61 73 65 20 6e 61 6d 65 0a 2a 2a 20 61 6e 64  base name.** and
1a65e 20 70 49 64 32 20 69 73 20 74 68 65 20 69 64 2e   pId2 is the id.
1a65f 20 20 49 66 20 74 68 65 20 6c 65 66 74 20 73 69    If the left si
1a660 64 65 20 69 73 20 6a 75 73 74 20 22 69 64 22 20  de is just "id" 
1a661 74 68 65 6e 20 70 49 64 31 20 69 73 20 74 68 65  then pId1 is the
1a662 0a 2a 2a 20 69 64 20 61 6e 64 20 70 49 64 32 20  .** id and pId2 
1a663 69 73 20 61 6e 79 20 65 6d 70 74 79 20 73 74 72  is any empty str
1a664 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ing..*/.SQLITE_P
1a665 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1a666 74 65 33 50 72 61 67 6d 61 28 0a 20 20 50 61 72  te3Pragma(.  Par
1a667 73 65 20 2a 70 50 61 72 73 65 2c 20 0a 20 20 54  se *pParse, .  T
1a668 6f 6b 65 6e 20 2a 70 49 64 31 2c 20 20 20 20 20  oken *pId1,     
1a669 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61 72 74     /* First part
1a66a 20 6f 66 20 5b 64 61 74 61 62 61 73 65 2e 5d 69   of [database.]i
1a66b 64 20 66 69 65 6c 64 20 2a 2f 0a 20 20 54 6f 6b  d field */.  Tok
1a66c 65 6e 20 2a 70 49 64 32 2c 20 20 20 20 20 20 20  en *pId2,       
1a66d 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 74 20   /* Second part 
1a66e 6f 66 20 5b 64 61 74 61 62 61 73 65 2e 5d 69 64  of [database.]id
1a66f 20 66 69 65 6c 64 2c 20 6f 72 20 4e 55 4c 4c 20   field, or NULL 
1a670 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 56 61 6c  */.  Token *pVal
1a671 75 65 2c 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65  ue,      /* Toke
1a672 6e 20 66 6f 72 20 3c 76 61 6c 75 65 3e 2c 20 6f  n for <value>, o
1a673 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20  r NULL */.  int 
1a674 6d 69 6e 75 73 46 6c 61 67 20 20 20 20 20 20 20  minusFlag       
1a675 2f 2a 20 54 72 75 65 20 69 66 20 61 20 27 2d 27  /* True if a '-'
1a676 20 73 69 67 6e 20 70 72 65 63 65 64 65 64 20 3c   sign preceded <
1a677 76 61 6c 75 65 3e 20 2a 2f 0a 29 7b 0a 20 20 63  value> */.){.  c
1a678 68 61 72 20 2a 7a 4c 65 66 74 20 3d 20 30 3b 20  har *zLeft = 0; 
1a679 20 20 20 20 20 20 2f 2a 20 4e 75 6c 2d 74 65 72        /* Nul-ter
1a67a 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74  minated UTF-8 st
1a67b 72 69 6e 67 20 3c 69 64 3e 20 2a 2f 0a 20 20 63  ring <id> */.  c
1a67c 68 61 72 20 2a 7a 52 69 67 68 74 20 3d 20 30 3b  har *zRight = 0;
1a67d 20 20 20 20 20 20 2f 2a 20 4e 75 6c 2d 74 65 72        /* Nul-ter
1a67e 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74  minated UTF-8 st
1a67f 72 69 6e 67 20 3c 76 61 6c 75 65 3e 2c 20 6f 72  ring <value>, or
1a680 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74   NULL */.  const
1a681 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 30 3b 20   char *zDb = 0; 
1a682 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
1a683 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 54 6f 6b 65  e name */.  Toke
1a684 6e 20 2a 70 49 64 3b 20 20 20 20 20 20 20 20 20  n *pId;         
1a685 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
1a686 20 3c 69 64 3e 20 74 6f 6b 65 6e 20 2a 2f 0a 20   <id> token */. 
1a687 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20   int iDb;       
1a688 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
1a689 61 73 65 20 69 6e 64 65 78 20 66 6f 72 20 3c 64  ase index for <d
1a68a 61 74 61 62 61 73 65 3e 20 2a 2f 0a 20 20 73 71  atabase> */.  sq
1a68b 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
1a68c 73 65 2d 3e 64 62 3b 0a 20 20 44 62 20 2a 70 44  se->db;.  Db *pD
1a68d 62 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73  b;.  Vdbe *v = s
1a68e 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
1a68f 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d  arse);.  if( v==
1a690 30 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f  0 ) return;..  /
1a691 2a 20 49 6e 74 65 72 70 72 65 74 20 74 68 65 20  * Interpret the 
1a692 5b 64 61 74 61 62 61 73 65 2e 5d 20 70 61 72 74  [database.] part
1a693 20 6f 66 20 74 68 65 20 70 72 61 67 6d 61 20 73   of the pragma s
1a694 74 61 74 65 6d 65 6e 74 2e 20 69 44 62 20 69 73  tatement. iDb is
1a695 20 74 68 65 0a 20 20 2a 2a 20 69 6e 64 65 78 20   the.  ** index 
1a696 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
1a697 74 68 69 73 20 70 72 61 67 6d 61 20 69 73 20 62  this pragma is b
1a698 65 69 6e 67 20 61 70 70 6c 69 65 64 20 74 6f 20  eing applied to 
1a699 69 6e 20 64 62 2e 61 44 62 5b 5d 2e 20 2a 2f 0a  in db.aDb[]. */.
1a69a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 54    iDb = sqlite3T
1a69b 77 6f 50 61 72 74 4e 61 6d 65 28 70 50 61 72 73  woPartName(pPars
1a69c 65 2c 20 70 49 64 31 2c 20 70 49 64 32 2c 20 26  e, pId1, pId2, &
1a69d 70 49 64 29 3b 0a 20 20 69 66 28 20 69 44 62 3c  pId);.  if( iDb<
1a69e 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 44  0 ) return;.  pD
1a69f 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62  b = &db->aDb[iDb
1a6a0 5d 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  ];..  /* If the 
1a6a1 74 65 6d 70 20 64 61 74 61 62 61 73 65 20 68 61  temp database ha
1a6a2 73 20 62 65 65 6e 20 65 78 70 6c 69 63 69 74 6c  s been explicitl
1a6a3 79 20 6e 61 6d 65 64 20 61 73 20 70 61 72 74 20  y named as part 
1a6a4 6f 66 20 74 68 65 20 0a 20 20 2a 2a 20 70 72 61  of the .  ** pra
1a6a5 67 6d 61 2c 20 6d 61 6b 65 20 73 75 72 65 20 69  gma, make sure i
1a6a6 74 20 69 73 20 6f 70 65 6e 2e 20 0a 20 20 2a 2f  t is open. .  */
1a6a7 0a 20 20 69 66 28 20 69 44 62 3d 3d 31 20 26 26  .  if( iDb==1 &&
1a6a8 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 65 6d 70   sqlite3OpenTemp
1a6a9 44 61 74 61 62 61 73 65 28 70 50 61 72 73 65 29  Database(pParse)
1a6aa 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
1a6ab 20 20 7d 0a 0a 20 20 7a 4c 65 66 74 20 3d 20 73    }..  zLeft = s
1a6ac 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
1a6ad 6b 65 6e 28 70 49 64 29 3b 0a 20 20 69 66 28 20  ken(pId);.  if( 
1a6ae 21 7a 4c 65 66 74 20 29 20 72 65 74 75 72 6e 3b  !zLeft ) return;
1a6af 0a 20 20 69 66 28 20 6d 69 6e 75 73 46 6c 61 67  .  if( minusFlag
1a6b0 20 29 7b 0a 20 20 20 20 7a 52 69 67 68 74 20 3d   ){.    zRight =
1a6b1 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
1a6b2 22 2d 25 54 22 2c 20 70 56 61 6c 75 65 29 3b 0a  "-%T", pValue);.
1a6b3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 52 69    }else{.    zRi
1a6b4 67 68 74 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d  ght = sqlite3Nam
1a6b5 65 46 72 6f 6d 54 6f 6b 65 6e 28 70 56 61 6c 75  eFromToken(pValu
1a6b6 65 29 3b 0a 20 20 7d 0a 0a 20 20 7a 44 62 20 3d  e);.  }..  zDb =
1a6b7 20 28 28 69 44 62 3e 30 29 3f 70 44 62 2d 3e 7a   ((iDb>0)?pDb->z
1a6b8 4e 61 6d 65 3a 30 29 3b 0a 20 20 69 66 28 20 73  Name:0);.  if( s
1a6b9 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
1a6ba 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 50  pParse, SQLITE_P
1a6bb 52 41 47 4d 41 2c 20 7a 4c 65 66 74 2c 20 7a 52  RAGMA, zLeft, zR
1a6bc 69 67 68 74 2c 20 7a 44 62 29 20 29 7b 0a 20 20  ight, zDb) ){.  
1a6bd 20 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75    goto pragma_ou
1a6be 74 3b 0a 20 20 7d 0a 20 0a 23 69 66 6e 64 65 66  t;.  }. .#ifndef
1a6bf 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47   SQLITE_OMIT_PAG
1a6c0 45 52 5f 50 52 41 47 4d 41 53 0a 20 20 2f 2a 0a  ER_PRAGMAS.  /*.
1a6c1 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61    **  PRAGMA [da
1a6c2 74 61 62 61 73 65 2e 5d 64 65 66 61 75 6c 74 5f  tabase.]default_
1a6c3 63 61 63 68 65 5f 73 69 7a 65 0a 20 20 2a 2a 20  cache_size.  ** 
1a6c4 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
1a6c5 65 2e 5d 64 65 66 61 75 6c 74 5f 63 61 63 68 65  e.]default_cache
1a6c6 5f 73 69 7a 65 3d 4e 0a 20 20 2a 2a 0a 20 20 2a  _size=N.  **.  *
1a6c7 2a 20 54 68 65 20 66 69 72 73 74 20 66 6f 72 6d  * The first form
1a6c8 20 72 65 70 6f 72 74 73 20 74 68 65 20 63 75 72   reports the cur
1a6c9 72 65 6e 74 20 70 65 72 73 69 73 74 65 6e 74 20  rent persistent 
1a6ca 73 65 74 74 69 6e 67 20 66 6f 72 20 74 68 65 0a  setting for the.
1a6cb 20 20 2a 2a 20 70 61 67 65 20 63 61 63 68 65 20    ** page cache 
1a6cc 73 69 7a 65 2e 20 20 54 68 65 20 76 61 6c 75 65  size.  The value
1a6cd 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 65   returned is the
1a6ce 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   maximum number 
1a6cf 6f 66 0a 20 20 2a 2a 20 70 61 67 65 73 20 69 6e  of.  ** pages in
1a6d0 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 2e   the page cache.
1a6d1 20 20 54 68 65 20 73 65 63 6f 6e 64 20 66 6f 72    The second for
1a6d2 6d 20 73 65 74 73 20 62 6f 74 68 20 74 68 65 20  m sets both the 
1a6d3 63 75 72 72 65 6e 74 0a 20 20 2a 2a 20 70 61 67  current.  ** pag
1a6d4 65 20 63 61 63 68 65 20 73 69 7a 65 20 76 61 6c  e cache size val
1a6d5 75 65 20 61 6e 64 20 74 68 65 20 70 65 72 73 69  ue and the persi
1a6d6 73 74 65 6e 74 20 70 61 67 65 20 63 61 63 68 65  stent page cache
1a6d7 20 73 69 7a 65 20 76 61 6c 75 65 0a 20 20 2a 2a   size value.  **
1a6d8 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 64   stored in the d
1a6d9 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20  atabase file..  
1a6da 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 64 65 66 61  **.  ** The defa
1a6db 75 6c 74 20 63 61 63 68 65 20 73 69 7a 65 20 69  ult cache size i
1a6dc 73 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 74 61  s stored in meta
1a6dd 2d 76 61 6c 75 65 20 32 20 6f 66 20 70 61 67 65  -value 2 of page
1a6de 20 31 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 64   1 of the.  ** d
1a6df 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54  atabase file.  T
1a6e0 68 65 20 63 61 63 68 65 20 73 69 7a 65 20 69 73  he cache size is
1a6e1 20 61 63 74 75 61 6c 6c 79 20 74 68 65 20 61 62   actually the ab
1a6e2 73 6f 6c 75 74 65 20 76 61 6c 75 65 20 6f 66 0a  solute value of.
1a6e3 20 20 2a 2a 20 74 68 69 73 20 6d 65 6d 6f 72 79    ** this memory
1a6e4 20 6c 6f 63 61 74 69 6f 6e 2e 20 20 54 68 65 20   location.  The 
1a6e5 73 69 67 6e 20 6f 66 20 6d 65 74 61 2d 76 61 6c  sign of meta-val
1a6e6 75 65 20 32 20 64 65 74 65 72 6d 69 6e 65 73 20  ue 2 determines 
1a6e7 74 68 65 0a 20 20 2a 2a 20 73 79 6e 63 68 72 6f  the.  ** synchro
1a6e8 6e 6f 75 73 20 73 65 74 74 69 6e 67 2e 20 20 41  nous setting.  A
1a6e9 20 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65 20   negative value 
1a6ea 6d 65 61 6e 73 20 73 79 6e 63 68 72 6f 6e 6f 75  means synchronou
1a6eb 73 20 69 73 20 6f 66 66 0a 20 20 2a 2a 20 61 6e  s is off.  ** an
1a6ec 64 20 61 20 70 6f 73 69 74 69 76 65 20 76 61 6c  d a positive val
1a6ed 75 65 20 6d 65 61 6e 73 20 73 79 6e 63 68 72 6f  ue means synchro
1a6ee 6e 6f 75 73 20 69 73 20 6f 6e 2e 0a 20 20 2a 2f  nous is on..  */
1a6ef 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
1a6f0 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 64 65 66  rICmp(zLeft,"def
1a6f1 61 75 6c 74 5f 63 61 63 68 65 5f 73 69 7a 65 22  ault_cache_size"
1a6f2 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74 61 74  )==0 ){.    stat
1a6f3 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c  ic const VdbeOpL
1a6f4 69 73 74 20 67 65 74 43 61 63 68 65 53 69 7a 65  ist getCacheSize
1a6f5 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 7b 20 4f  [] = {.      { O
1a6f6 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 20 30  P_ReadCookie,  0
1a6f7 2c 20 32 2c 20 20 20 20 20 20 20 20 30 7d 2c 20  , 2,        0}, 
1a6f8 20 2f 2a 20 30 20 2a 2f 0a 20 20 20 20 20 20 7b   /* 0 */.      {
1a6f9 20 4f 50 5f 41 62 73 56 61 6c 75 65 2c 20 20 20   OP_AbsValue,   
1a6fa 20 30 2c 20 30 2c 20 20 20 20 20 20 20 20 30 7d   0, 0,        0}
1a6fb 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 44 75 70  ,.      { OP_Dup
1a6fc 2c 20 20 20 20 20 20 20 20 20 30 2c 20 30 2c 20  ,         0, 0, 
1a6fd 20 20 20 20 20 20 20 30 7d 2c 0a 20 20 20 20 20         0},.     
1a6fe 20 7b 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 20   { OP_Integer,  
1a6ff 20 20 20 30 2c 20 30 2c 20 20 20 20 20 20 20 20     0, 0,        
1a700 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 4e  0},.      { OP_N
1a701 65 2c 20 20 20 20 20 20 20 20 20 20 30 2c 20 36  e,          0, 6
1a702 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a 20 20 20  ,        0},.   
1a703 20 20 20 7b 20 4f 50 5f 49 6e 74 65 67 65 72 2c     { OP_Integer,
1a704 20 20 20 20 20 30 2c 20 30 2c 20 20 20 20 20 20       0, 0,      
1a705 20 20 30 7d 2c 20 20 2f 2a 20 35 20 2a 2f 0a 20    0},  /* 5 */. 
1a706 20 20 20 20 20 7b 20 4f 50 5f 43 61 6c 6c 62 61       { OP_Callba
1a707 63 6b 2c 20 20 20 20 31 2c 20 30 2c 20 20 20 20  ck,    1, 0,    
1a708 20 20 20 20 30 7d 2c 0a 20 20 20 20 7d 3b 0a 20      0},.    };. 
1a709 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 20     int addr;.   
1a70a 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 61 64   if( sqlite3Read
1a70b 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29  Schema(pParse) )
1a70c 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74   goto pragma_out
1a70d 3b 0a 20 20 20 20 69 66 28 20 21 7a 52 69 67 68  ;.    if( !zRigh
1a70e 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  t ){.      sqlit
1a70f 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
1a710 28 76 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71  (v, 1);.      sq
1a711 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
1a712 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d  ame(v, 0, COLNAM
1a713 45 5f 4e 41 4d 45 2c 20 22 63 61 63 68 65 5f 73  E_NAME, "cache_s
1a714 69 7a 65 22 2c 20 50 33 5f 53 54 41 54 49 43 29  ize", P3_STATIC)
1a715 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73  ;.      addr = s
1a716 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c  qlite3VdbeAddOpL
1a717 69 73 74 28 76 2c 20 41 72 72 61 79 53 69 7a 65  ist(v, ArraySize
1a718 28 67 65 74 43 61 63 68 65 53 69 7a 65 29 2c 20  (getCacheSize), 
1a719 67 65 74 43 61 63 68 65 53 69 7a 65 29 3b 0a 20  getCacheSize);. 
1a71a 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1a71b 43 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72  ChangeP1(v, addr
1a71c 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71  , iDb);.      sq
1a71d 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
1a71e 31 28 76 2c 20 61 64 64 72 2b 35 2c 20 53 51 4c  1(v, addr+5, SQL
1a71f 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48  ITE_DEFAULT_CACH
1a720 45 5f 53 49 5a 45 29 3b 0a 20 20 20 20 7d 65 6c  E_SIZE);.    }el
1a721 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 69  se{.      int si
1a722 7a 65 20 3d 20 61 74 6f 69 28 7a 52 69 67 68 74  ze = atoi(zRight
1a723 29 3b 0a 20 20 20 20 20 20 69 66 28 20 73 69 7a  );.      if( siz
1a724 65 3c 30 20 29 20 73 69 7a 65 20 3d 20 2d 73 69  e<0 ) size = -si
1a725 7a 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ze;.      sqlite
1a726 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61  3BeginWriteOpera
1a727 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20  tion(pParse, 0, 
1a728 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  iDb);.      sqli
1a729 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1a72a 4f 50 5f 49 6e 74 65 67 65 72 2c 20 73 69 7a 65  OP_Integer, size
1a72b 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 0);.      sqli
1a72c 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1a72d 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 69  OP_ReadCookie, i
1a72e 44 62 2c 20 32 29 3b 0a 20 20 20 20 20 20 61 64  Db, 2);.      ad
1a72f 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
1a730 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 74 65  AddOp(v, OP_Inte
1a731 67 65 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  ger, 0, 0);.    
1a732 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1a733 4f 70 28 76 2c 20 4f 50 5f 47 65 2c 20 30 2c 20  Op(v, OP_Ge, 0, 
1a734 61 64 64 72 2b 33 29 3b 0a 20 20 20 20 20 20 73  addr+3);.      s
1a735 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1a736 76 2c 20 4f 50 5f 4e 65 67 61 74 69 76 65 2c 20  v, OP_Negative, 
1a737 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c  0, 0);.      sql
1a738 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1a739 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69   OP_SetCookie, i
1a73a 44 62 2c 20 32 29 3b 0a 20 20 20 20 20 20 70 44  Db, 2);.      pD
1a73b 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63 68  b->pSchema->cach
1a73c 65 5f 73 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20  e_size = size;. 
1a73d 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
1a73e 65 53 65 74 43 61 63 68 65 53 69 7a 65 28 70 44  eSetCacheSize(pD
1a73f 62 2d 3e 70 42 74 2c 20 70 44 62 2d 3e 70 53 63  b->pBt, pDb->pSc
1a740 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65  hema->cache_size
1a741 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
1a742 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41  ..  /*.  **  PRA
1a743 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 70  GMA [database.]p
1a744 61 67 65 5f 73 69 7a 65 0a 20 20 2a 2a 20 20 50  age_size.  **  P
1a745 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
1a746 5d 70 61 67 65 5f 73 69 7a 65 3d 4e 0a 20 20 2a  ]page_size=N.  *
1a747 2a 0a 20 20 2a 2a 20 54 68 65 20 66 69 72 73 74  *.  ** The first
1a748 20 66 6f 72 6d 20 72 65 70 6f 72 74 73 20 74 68   form reports th
1a749 65 20 63 75 72 72 65 6e 74 20 73 65 74 74 69 6e  e current settin
1a74a 67 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 64  g for the.  ** d
1a74b 61 74 61 62 61 73 65 20 70 61 67 65 20 73 69 7a  atabase page siz
1a74c 65 20 69 6e 20 62 79 74 65 73 2e 20 20 54 68 65  e in bytes.  The
1a74d 20 73 65 63 6f 6e 64 20 66 6f 72 6d 20 73 65 74   second form set
1a74e 73 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62  s the.  ** datab
1a74f 61 73 65 20 70 61 67 65 20 73 69 7a 65 20 76 61  ase page size va
1a750 6c 75 65 2e 20 20 54 68 65 20 76 61 6c 75 65 20  lue.  The value 
1a751 63 61 6e 20 6f 6e 6c 79 20 62 65 20 73 65 74 20  can only be set 
1a752 69 66 0a 20 20 2a 2a 20 74 68 65 20 64 61 74 61  if.  ** the data
1a753 62 61 73 65 20 68 61 73 20 6e 6f 74 20 79 65 74  base has not yet
1a754 20 62 65 65 6e 20 63 72 65 61 74 65 64 2e 0a 20   been created.. 
1a755 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
1a756 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22  3StrICmp(zLeft,"
1a757 70 61 67 65 5f 73 69 7a 65 22 29 3d 3d 30 20 29  page_size")==0 )
1a758 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 42 74  {.    Btree *pBt
1a759 20 3d 20 70 44 62 2d 3e 70 42 74 3b 0a 20 20 20   = pDb->pBt;.   
1a75a 20 69 66 28 20 21 7a 52 69 67 68 74 20 29 7b 0a   if( !zRight ){.
1a75b 20 20 20 20 20 20 69 6e 74 20 73 69 7a 65 20 3d        int size =
1a75c 20 70 42 74 20 3f 20 73 71 6c 69 74 65 33 42 74   pBt ? sqlite3Bt
1a75d 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 70  reeGetPageSize(p
1a75e 42 74 29 20 3a 20 30 3b 0a 20 20 20 20 20 20 72  Bt) : 0;.      r
1a75f 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70  eturnSingleInt(p
1a760 50 61 72 73 65 2c 20 22 70 61 67 65 5f 73 69 7a  Parse, "page_siz
1a761 65 22 2c 20 73 69 7a 65 29 3b 0a 20 20 20 20 7d  e", size);.    }
1a762 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
1a763 74 65 33 42 74 72 65 65 53 65 74 50 61 67 65 53  te3BtreeSetPageS
1a764 69 7a 65 28 70 42 74 2c 20 61 74 6f 69 28 7a 52  ize(pBt, atoi(zR
1a765 69 67 68 74 29 2c 20 2d 31 29 3b 0a 20 20 20 20  ight), -1);.    
1a766 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a  }.  }else..  /*.
1a767 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61    **  PRAGMA [da
1a768 74 61 62 61 73 65 2e 5d 6d 61 78 5f 70 61 67 65  tabase.]max_page
1a769 5f 63 6f 75 6e 74 0a 20 20 2a 2a 20 20 50 52 41  _count.  **  PRA
1a76a 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6d  GMA [database.]m
1a76b 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 3d 4e 0a  ax_page_count=N.
1a76c 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 66 69    **.  ** The fi
1a76d 72 73 74 20 66 6f 72 6d 20 72 65 70 6f 72 74 73  rst form reports
1a76e 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 65 74   the current set
1a76f 74 69 6e 67 20 66 6f 72 20 74 68 65 0a 20 20 2a  ting for the.  *
1a770 2a 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  * maximum number
1a771 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65   of pages in the
1a772 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
1a773 20 54 68 65 20 0a 20 20 2a 2a 20 73 65 63 6f 6e   The .  ** secon
1a774 64 20 66 6f 72 6d 20 61 74 74 65 6d 70 74 73 20  d form attempts 
1a775 74 6f 20 63 68 61 6e 67 65 20 74 68 69 73 20 73  to change this s
1a776 65 74 74 69 6e 67 2e 20 20 42 6f 74 68 0a 20 20  etting.  Both.  
1a777 2a 2a 20 66 6f 72 6d 73 20 72 65 74 75 72 6e 20  ** forms return 
1a778 74 68 65 20 63 75 72 72 65 6e 74 20 73 65 74 74  the current sett
1a779 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ing..  */.  if( 
1a77a 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
1a77b 4c 65 66 74 2c 22 6d 61 78 5f 70 61 67 65 5f 63  Left,"max_page_c
1a77c 6f 75 6e 74 22 29 3d 3d 30 20 29 7b 0a 20 20 20  ount")==0 ){.   
1a77d 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 70 44   Btree *pBt = pD
1a77e 62 2d 3e 70 42 74 3b 0a 20 20 20 20 69 6e 74 20  b->pBt;.    int 
1a77f 6e 65 77 4d 61 78 20 3d 20 30 3b 0a 20 20 20 20  newMax = 0;.    
1a780 69 66 28 20 7a 52 69 67 68 74 20 29 7b 0a 20 20  if( zRight ){.  
1a781 20 20 20 20 6e 65 77 4d 61 78 20 3d 20 61 74 6f      newMax = ato
1a782 69 28 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 7d  i(zRight);.    }
1a783 0a 20 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a  .    if( pBt ){.
1a784 20 20 20 20 20 20 6e 65 77 4d 61 78 20 3d 20 73        newMax = s
1a785 71 6c 69 74 65 33 42 74 72 65 65 4d 61 78 50 61  qlite3BtreeMaxPa
1a786 67 65 43 6f 75 6e 74 28 70 42 74 2c 20 6e 65 77  geCount(pBt, new
1a787 4d 61 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Max);.    }.    
1a788 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28  returnSingleInt(
1a789 70 50 61 72 73 65 2c 20 22 6d 61 78 5f 70 61 67  pParse, "max_pag
1a78a 65 5f 63 6f 75 6e 74 22 2c 20 6e 65 77 4d 61 78  e_count", newMax
1a78b 29 3b 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a  );.  }else..  /*
1a78c 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64  .  **  PRAGMA [d
1a78d 61 74 61 62 61 73 65 2e 5d 6c 6f 63 6b 69 6e 67  atabase.]locking
1a78e 5f 6d 6f 64 65 0a 20 20 2a 2a 20 20 50 52 41 47  _mode.  **  PRAG
1a78f 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f  MA [database.]lo
1a790 63 6b 69 6e 67 5f 6d 6f 64 65 20 3d 20 28 6e 6f  cking_mode = (no
1a791 72 6d 61 6c 7c 65 78 63 6c 75 73 69 76 65 29 0a  rmal|exclusive).
1a792 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
1a793 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
1a794 22 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 22 29 3d  "locking_mode")=
1a795 3d 30 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20  =0 ){.    const 
1a796 63 68 61 72 20 2a 7a 52 65 74 20 3d 20 22 6e 6f  char *zRet = "no
1a797 72 6d 61 6c 22 3b 0a 20 20 20 20 69 6e 74 20 65  rmal";.    int e
1a798 4d 6f 64 65 20 3d 20 67 65 74 4c 6f 63 6b 69 6e  Mode = getLockin
1a799 67 4d 6f 64 65 28 7a 52 69 67 68 74 29 3b 0a 0a  gMode(zRight);..
1a79a 20 20 20 20 69 66 28 20 70 49 64 32 2d 3e 6e 3d      if( pId2->n=
1a79b 3d 30 20 26 26 20 65 4d 6f 64 65 3d 3d 50 41 47  =0 && eMode==PAG
1a79c 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51  ER_LOCKINGMODE_Q
1a79d 55 45 52 59 20 29 7b 0a 20 20 20 20 20 20 2f 2a  UERY ){.      /*
1a79e 20 53 69 6d 70 6c 65 20 22 50 52 41 47 4d 41 20   Simple "PRAGMA 
1a79f 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3b 22 20 73  locking_mode;" s
1a7a0 74 61 74 65 6d 65 6e 74 2e 20 54 68 69 73 20 69  tatement. This i
1a7a1 73 20 61 20 71 75 65 72 79 20 66 6f 72 0a 20 20  s a query for.  
1a7a2 20 20 20 20 2a 2a 20 74 68 65 20 63 75 72 72 65      ** the curre
1a7a3 6e 74 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69  nt default locki
1a7a4 6e 67 20 6d 6f 64 65 20 28 77 68 69 63 68 20 6d  ng mode (which m
1a7a5 61 79 20 62 65 20 64 69 66 66 65 72 65 6e 74 20  ay be different 
1a7a6 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  to.      ** the 
1a7a7 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 6f 66 20  locking-mode of 
1a7a8 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
1a7a9 65 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  e)..      */.   
1a7aa 20 20 20 65 4d 6f 64 65 20 3d 20 64 62 2d 3e 64     eMode = db->d
1a7ab 66 6c 74 4c 6f 63 6b 4d 6f 64 65 3b 0a 20 20 20  fltLockMode;.   
1a7ac 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 50 61   }else{.      Pa
1a7ad 67 65 72 20 2a 70 50 61 67 65 72 3b 0a 20 20 20  ger *pPager;.   
1a7ae 20 20 20 69 66 28 20 70 49 64 32 2d 3e 6e 3d 3d     if( pId2->n==
1a7af 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  0 ){.        /* 
1a7b0 54 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 74  This indicates t
1a7b1 68 61 74 20 6e 6f 20 64 61 74 61 62 61 73 65 20  hat no database 
1a7b2 6e 61 6d 65 20 77 61 73 20 73 70 65 63 69 66 69  name was specifi
1a7b3 65 64 20 61 73 20 70 61 72 74 0a 20 20 20 20 20  ed as part.     
1a7b4 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 50 52 41     ** of the PRA
1a7b5 47 4d 41 20 63 6f 6d 6d 61 6e 64 2e 20 49 6e 20  GMA command. In 
1a7b6 74 68 69 73 20 63 61 73 65 20 74 68 65 20 6c 6f  this case the lo
1a7b7 63 6b 69 6e 67 2d 6d 6f 64 65 20 6d 75 73 74 20  cking-mode must 
1a7b8 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 65  be.        ** se
1a7b9 74 20 6f 6e 20 61 6c 6c 20 61 74 74 61 63 68 65  t on all attache
1a7ba 64 20 64 61 74 61 62 61 73 65 73 2c 20 61 73 20  d databases, as 
1a7bb 77 65 6c 6c 20 61 73 20 74 68 65 20 6d 61 69 6e  well as the main
1a7bc 20 64 62 20 66 69 6c 65 2e 0a 20 20 20 20 20 20   db file..      
1a7bd 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20    **.        ** 
1a7be 41 6c 73 6f 2c 20 74 68 65 20 73 71 6c 69 74 65  Also, the sqlite
1a7bf 33 2e 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65 20 76  3.dfltLockMode v
1a7c0 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 73  ariable is set s
1a7c1 6f 20 74 68 61 74 0a 20 20 20 20 20 20 20 20 2a  o that.        *
1a7c2 2a 20 61 6e 79 20 73 75 62 73 65 71 75 65 6e 74  * any subsequent
1a7c3 6c 79 20 61 74 74 61 63 68 65 64 20 64 61 74 61  ly attached data
1a7c4 62 61 73 65 73 20 61 6c 73 6f 20 75 73 65 20 74  bases also use t
1a7c5 68 65 20 73 70 65 63 69 66 69 65 64 0a 20 20 20  he specified.   
1a7c6 20 20 20 20 20 2a 2a 20 6c 6f 63 6b 69 6e 67 20       ** locking 
1a7c7 6d 6f 64 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f  mode..        */
1a7c8 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 69 3b  .        int ii;
1a7c9 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1a7ca 70 44 62 3d 3d 26 64 62 2d 3e 61 44 62 5b 30 5d  pDb==&db->aDb[0]
1a7cb 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  );.        for(i
1a7cc 69 3d 32 3b 20 69 69 3c 64 62 2d 3e 6e 44 62 3b  i=2; ii<db->nDb;
1a7cd 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20   ii++){.        
1a7ce 20 20 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74    pPager = sqlit
1a7cf 65 33 42 74 72 65 65 50 61 67 65 72 28 64 62 2d  e3BtreePager(db-
1a7d0 3e 61 44 62 5b 69 69 5d 2e 70 42 74 29 3b 0a 20  >aDb[ii].pBt);. 
1a7d1 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1a7d2 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65  PagerLockingMode
1a7d3 28 70 50 61 67 65 72 2c 20 65 4d 6f 64 65 29 3b  (pPager, eMode);
1a7d4 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1a7d5 20 20 20 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b 4d     db->dfltLockM
1a7d6 6f 64 65 20 3d 20 65 4d 6f 64 65 3b 0a 20 20 20  ode = eMode;.   
1a7d7 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67 65     }.      pPage
1a7d8 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  r = sqlite3Btree
1a7d9 50 61 67 65 72 28 70 44 62 2d 3e 70 42 74 29 3b  Pager(pDb->pBt);
1a7da 0a 20 20 20 20 20 20 65 4d 6f 64 65 20 3d 20 73  .      eMode = s
1a7db 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69  qlite3PagerLocki
1a7dc 6e 67 4d 6f 64 65 28 70 50 61 67 65 72 2c 20 65  ngMode(pPager, e
1a7dd 4d 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  Mode);.    }..  
1a7de 20 20 61 73 73 65 72 74 28 65 4d 6f 64 65 3d 3d    assert(eMode==
1a7df 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44  PAGER_LOCKINGMOD
1a7e0 45 5f 4e 4f 52 4d 41 4c 7c 7c 65 4d 6f 64 65 3d  E_NORMAL||eMode=
1a7e1 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f  =PAGER_LOCKINGMO
1a7e2 44 45 5f 45 58 43 4c 55 53 49 56 45 29 3b 0a 20  DE_EXCLUSIVE);. 
1a7e3 20 20 20 69 66 28 20 65 4d 6f 64 65 3d 3d 50 41     if( eMode==PA
1a7e4 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
1a7e5 45 58 43 4c 55 53 49 56 45 20 29 7b 0a 20 20 20  EXCLUSIVE ){.   
1a7e6 20 20 20 7a 52 65 74 20 3d 20 22 65 78 63 6c 75     zRet = "exclu
1a7e7 73 69 76 65 22 3b 0a 20 20 20 20 7d 0a 20 20 20  sive";.    }.   
1a7e8 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e   sqlite3VdbeSetN
1a7e9 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20  umCols(v, 1);.  
1a7ea 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
1a7eb 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f  ColName(v, 0, CO
1a7ec 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6c 6f 63  LNAME_NAME, "loc
1a7ed 6b 69 6e 67 5f 6d 6f 64 65 22 2c 20 50 33 5f 53  king_mode", P3_S
1a7ee 54 41 54 49 43 29 3b 0a 20 20 20 20 73 71 6c 69  TATIC);.    sqli
1a7ef 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20 4f 50  te3VdbeOp3(v, OP
1a7f0 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 30 2c 20  _String8, 0, 0, 
1a7f1 7a 52 65 74 2c 20 30 29 3b 0a 20 20 20 20 73 71  zRet, 0);.    sq
1a7f2 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1a7f3 2c 20 4f 50 5f 43 61 6c 6c 62 61 63 6b 2c 20 31  , OP_Callback, 1
1a7f4 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65  , 0);.  }else.#e
1a7f5 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
1a7f6 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41  MIT_PAGER_PRAGMA
1a7f7 53 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  S */..  /*.  ** 
1a7f8 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
1a7f9 65 2e 5d 61 75 74 6f 5f 76 61 63 75 75 6d 0a 20  e.]auto_vacuum. 
1a7fa 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74   **  PRAGMA [dat
1a7fb 61 62 61 73 65 2e 5d 61 75 74 6f 5f 76 61 63 75  abase.]auto_vacu
1a7fc 75 6d 3d 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 47  um=N.  **.  ** G
1a7fd 65 74 20 6f 72 20 73 65 74 20 74 68 65 20 28 62  et or set the (b
1a7fe 6f 6f 6c 65 61 6e 29 20 76 61 6c 75 65 20 6f 66  oolean) value of
1a7ff 20 74 68 65 20 64 61 74 61 62 61 73 65 20 27 61   the database 'a
1a800 75 74 6f 2d 76 61 63 75 75 6d 27 20 70 61 72 61  uto-vacuum' para
1a801 6d 65 74 65 72 2e 0a 20 20 2a 2f 0a 23 69 66 6e  meter..  */.#ifn
1a802 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1a803 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 69 66 28  AUTOVACUUM.  if(
1a804 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
1a805 7a 4c 65 66 74 2c 22 61 75 74 6f 5f 76 61 63 75  zLeft,"auto_vacu
1a806 75 6d 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 42  um")==0 ){.    B
1a807 74 72 65 65 20 2a 70 42 74 20 3d 20 70 44 62 2d  tree *pBt = pDb-
1a808 3e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 73 71  >pBt;.    if( sq
1a809 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28  lite3ReadSchema(
1a80a 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20 20  pParse) ){.     
1a80b 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74   goto pragma_out
1a80c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1a80d 21 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20  !zRight ){.     
1a80e 20 69 6e 74 20 61 75 74 6f 5f 76 61 63 75 75 6d   int auto_vacuum
1a80f 20 3d 20 0a 20 20 20 20 20 20 20 20 20 20 70 42   = .          pB
1a810 74 20 3f 20 73 71 6c 69 74 65 33 42 74 72 65 65  t ? sqlite3Btree
1a811 47 65 74 41 75 74 6f 56 61 63 75 75 6d 28 70 42  GetAutoVacuum(pB
1a812 74 29 20 3a 20 53 51 4c 49 54 45 5f 44 45 46 41  t) : SQLITE_DEFA
1a813 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 3b 0a  ULT_AUTOVACUUM;.
1a814 20 20 20 20 20 20 72 65 74 75 72 6e 53 69 6e 67        returnSing
1a815 6c 65 49 6e 74 28 70 50 61 72 73 65 2c 20 22 61  leInt(pParse, "a
1a816 75 74 6f 5f 76 61 63 75 75 6d 22 2c 20 61 75 74  uto_vacuum", aut
1a817 6f 5f 76 61 63 75 75 6d 29 3b 0a 20 20 20 20 7d  o_vacuum);.    }
1a818 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20  else{.      int 
1a819 65 41 75 74 6f 20 3d 20 67 65 74 41 75 74 6f 56  eAuto = getAutoV
1a81a 61 63 75 75 6d 28 7a 52 69 67 68 74 29 3b 0a 20  acuum(zRight);. 
1a81b 20 20 20 20 20 69 66 28 20 65 41 75 74 6f 3e 3d       if( eAuto>=
1a81c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  0 ){.        /* 
1a81d 43 61 6c 6c 20 53 65 74 41 75 74 6f 56 61 63 75  Call SetAutoVacu
1a81e 75 6d 28 29 20 74 6f 20 73 65 74 20 69 6e 69 74  um() to set init
1a81f 69 61 6c 69 7a 65 20 74 68 65 20 69 6e 74 65 72  ialize the inter
1a820 6e 61 6c 20 61 75 74 6f 20 61 6e 64 0a 20 20 20  nal auto and.   
1a821 20 20 20 20 20 2a 2a 20 69 6e 63 72 2d 76 61 63       ** incr-vac
1a822 75 75 6d 20 66 6c 61 67 73 2e 20 54 68 69 73 20  uum flags. This 
1a823 69 73 20 72 65 71 75 69 72 65 64 20 69 6e 20 63  is required in c
1a824 61 73 65 20 74 68 69 73 20 63 6f 6e 6e 65 63 74  ase this connect
1a825 69 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 63  ion.        ** c
1a826 72 65 61 74 65 73 20 74 68 65 20 64 61 74 61 62  reates the datab
1a827 61 73 65 20 66 69 6c 65 2e 20 49 74 20 69 73 20  ase file. It is 
1a828 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20 69  important that i
1a829 74 20 69 73 20 63 72 65 61 74 65 64 0a 20 20 20  t is created.   
1a82a 20 20 20 20 20 2a 2a 20 61 73 20 61 6e 20 61 75       ** as an au
1a82b 74 6f 2d 76 61 63 75 75 6d 20 63 61 70 61 62 6c  to-vacuum capabl
1a82c 65 20 64 62 2e 0a 20 20 20 20 20 20 20 20 2a 2f  e db..        */
1a82d 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 63 20  .        int rc 
1a82e 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65  = sqlite3BtreeSe
1a82f 74 41 75 74 6f 56 61 63 75 75 6d 28 70 42 74 2c  tAutoVacuum(pBt,
1a830 20 65 41 75 74 6f 29 3b 0a 20 20 20 20 20 20 20   eAuto);.       
1a831 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1a832 4f 4b 20 26 26 20 28 65 41 75 74 6f 3d 3d 31 20  OK && (eAuto==1 
1a833 7c 7c 20 65 41 75 74 6f 3d 3d 32 29 20 29 7b 0a  || eAuto==2) ){.
1a834 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65            /* Whe
1a835 6e 20 73 65 74 74 69 6e 67 20 74 68 65 20 61 75  n setting the au
1a836 74 6f 5f 76 61 63 75 75 6d 20 6d 6f 64 65 20 74  to_vacuum mode t
1a837 6f 20 65 69 74 68 65 72 20 22 66 75 6c 6c 22 20  o either "full" 
1a838 6f 72 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  or .          **
1a839 20 22 69 6e 63 72 65 6d 65 6e 74 61 6c 22 2c 20   "incremental", 
1a83a 77 72 69 74 65 20 74 68 65 20 76 61 6c 75 65 20  write the value 
1a83b 6f 66 20 6d 65 74 61 5b 36 5d 20 69 6e 20 74 68  of meta[6] in th
1a83c 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 20  e database.     
1a83d 20 20 20 20 20 2a 2a 20 66 69 6c 65 2e 20 42 65       ** file. Be
1a83e 66 6f 72 65 20 77 72 69 74 69 6e 67 20 74 6f 20  fore writing to 
1a83f 6d 65 74 61 5b 36 5d 2c 20 63 68 65 63 6b 20 74  meta[6], check t
1a840 68 61 74 20 6d 65 74 61 5b 33 5d 20 69 6e 64 69  hat meta[3] indi
1a841 63 61 74 65 73 0a 20 20 20 20 20 20 20 20 20 20  cates.          
1a842 2a 2a 20 74 68 61 74 20 74 68 69 73 20 72 65 61  ** that this rea
1a843 6c 6c 79 20 69 73 20 61 6e 20 61 75 74 6f 2d 76  lly is an auto-v
1a844 61 63 75 75 6d 20 63 61 70 61 62 6c 65 20 64 61  acuum capable da
1a845 74 61 62 61 73 65 2e 0a 20 20 20 20 20 20 20 20  tabase..        
1a846 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73    */.          s
1a847 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65  tatic const Vdbe
1a848 4f 70 4c 69 73 74 20 73 65 74 4d 65 74 61 36 5b  OpList setMeta6[
1a849 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 20 20  ] = {.          
1a84a 20 20 7b 20 4f 50 5f 54 72 61 6e 73 61 63 74 69    { OP_Transacti
1a84b 6f 6e 2c 20 20 20 20 30 2c 20 20 20 20 20 20 20  on,    0,       
1a84c 20 20 20 20 20 20 20 20 31 2c 20 20 20 20 20 20          1,      
1a84d 20 20 30 7d 2c 20 20 20 20 2f 2a 20 30 20 2a 2f    0},    /* 0 */
1a84e 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f  .            { O
1a84f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 20 20  P_ReadCookie,   
1a850 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
1a851 20 20 20 33 2c 20 20 20 20 20 20 20 20 30 7d 2c     3,        0},
1a852 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20      /* 1 */.    
1a853 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 49 66 2c          { OP_If,
1a854 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20               0, 
1a855 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c                0,
1a856 20 20 20 20 20 20 20 20 30 7d 2c 20 20 20 20 2f          0},    /
1a857 2a 20 32 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  * 2 */.         
1a858 20 20 20 7b 20 4f 50 5f 48 61 6c 74 2c 20 20 20     { OP_Halt,   
1a859 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f          SQLITE_O
1a85a 4b 2c 20 20 20 20 20 20 20 4f 45 5f 41 62 6f 72  K,       OE_Abor
1a85b 74 2c 20 30 7d 2c 20 20 20 20 2f 2a 20 33 20 2a  t, 0},    /* 3 *
1a85c 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  /.            { 
1a85d 4f 50 5f 49 6e 74 65 67 65 72 2c 20 20 20 20 20  OP_Integer,     
1a85e 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20     0,           
1a85f 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 30 7d      0,        0}
1a860 2c 20 20 20 20 2f 2a 20 34 20 2a 2f 0a 20 20 20  ,    /* 4 */.   
1a861 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53 65           { OP_Se
1a862 74 43 6f 6f 6b 69 65 2c 20 20 20 20 20 20 30 2c  tCookie,      0,
1a863 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36                 6
1a864 2c 20 20 20 20 20 20 20 20 30 7d 2c 20 20 20 20  ,        0},    
1a865 2f 2a 20 35 20 2a 2f 0a 20 20 20 20 20 20 20 20  /* 5 */.        
1a866 20 20 7d 3b 0a 20 20 20 20 20 20 20 20 20 20 69    };.          i
1a867 6e 74 20 69 41 64 64 72 3b 0a 20 20 20 20 20 20  nt iAddr;.      
1a868 20 20 20 20 69 41 64 64 72 20 3d 20 73 71 6c 69      iAddr = sqli
1a869 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74  te3VdbeAddOpList
1a86a 28 76 2c 20 41 72 72 61 79 53 69 7a 65 28 73 65  (v, ArraySize(se
1a86b 74 4d 65 74 61 36 29 2c 20 73 65 74 4d 65 74 61  tMeta6), setMeta
1a86c 36 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  6);.          sq
1a86d 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
1a86e 31 28 76 2c 20 69 41 64 64 72 2c 20 69 44 62 29  1(v, iAddr, iDb)
1a86f 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
1a870 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28  te3VdbeChangeP1(
1a871 76 2c 20 69 41 64 64 72 2b 31 2c 20 69 44 62 29  v, iAddr+1, iDb)
1a872 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
1a873 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28  te3VdbeChangeP2(
1a874 76 2c 20 69 41 64 64 72 2b 32 2c 20 69 41 64 64  v, iAddr+2, iAdd
1a875 72 2b 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20  r+4);.          
1a876 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1a877 65 50 31 28 76 2c 20 69 41 64 64 72 2b 34 2c 20  eP1(v, iAddr+4, 
1a878 65 41 75 74 6f 2d 31 29 3b 0a 20 20 20 20 20 20  eAuto-1);.      
1a879 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
1a87a 68 61 6e 67 65 50 31 28 76 2c 20 69 41 64 64 72  hangeP1(v, iAddr
1a87b 2b 35 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20  +5, iDb);.      
1a87c 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
1a87d 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66  }.  }else.#endif
1a87e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41  ..  /*.  **  PRA
1a87f 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 69  GMA [database.]i
1a880 6e 63 72 65 6d 65 6e 74 61 6c 5f 76 61 63 75 75  ncremental_vacuu
1a881 6d 28 4e 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 44  m(N).  **.  ** D
1a882 6f 20 4e 20 73 74 65 70 73 20 6f 66 20 69 6e 63  o N steps of inc
1a883 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 69  remental vacuumi
1a884 6e 67 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65  ng on a database
1a885 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ..  */.#ifndef S
1a886 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
1a887 41 43 55 55 4d 0a 20 20 69 66 28 20 73 71 6c 69  ACUUM.  if( sqli
1a888 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
1a889 2c 22 69 6e 63 72 65 6d 65 6e 74 61 6c 5f 76 61  ,"incremental_va
1a88a 63 75 75 6d 22 29 3d 3d 30 20 29 7b 0a 20 20 20  cuum")==0 ){.   
1a88b 20 69 6e 74 20 69 4c 69 6d 69 74 2c 20 61 64 64   int iLimit, add
1a88c 72 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  r;.    if( sqlit
1a88d 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61  e3ReadSchema(pPa
1a88e 72 73 65 29 20 29 7b 0a 20 20 20 20 20 20 67 6f  rse) ){.      go
1a88f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20  to pragma_out;. 
1a890 20 20 20 7d 0a 20 20 20 20 69 66 28 20 7a 52 69     }.    if( zRi
1a891 67 68 74 3d 3d 30 20 7c 7c 20 21 73 71 6c 69 74  ght==0 || !sqlit
1a892 65 33 47 65 74 49 6e 74 33 32 28 7a 52 69 67 68  e3GetInt32(zRigh
1a893 74 2c 20 26 69 4c 69 6d 69 74 29 20 7c 7c 20 69  t, &iLimit) || i
1a894 4c 69 6d 69 74 3c 3d 30 20 29 7b 0a 20 20 20 20  Limit<=0 ){.    
1a895 20 20 69 4c 69 6d 69 74 20 3d 20 30 78 37 66 66    iLimit = 0x7ff
1a896 66 66 66 66 66 3b 0a 20 20 20 20 7d 0a 20 20 20  fffff;.    }.   
1a897 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69   sqlite3BeginWri
1a898 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72  teOperation(pPar
1a899 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 20  se, 0, iDb);.   
1a89a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1a89b 70 28 76 2c 20 4f 50 5f 4d 65 6d 49 6e 74 2c 20  p(v, OP_MemInt, 
1a89c 69 4c 69 6d 69 74 2c 20 30 29 3b 0a 20 20 20 20  iLimit, 0);.    
1a89d 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
1a89e 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 6e  beAddOp(v, OP_In
1a89f 63 72 56 61 63 75 75 6d 2c 20 69 44 62 2c 20 30  crVacuum, iDb, 0
1a8a0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1a8a1 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 61  beAddOp(v, OP_Ca
1a8a2 6c 6c 62 61 63 6b 2c 20 30 2c 20 30 29 3b 0a 20  llback, 0, 0);. 
1a8a3 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1a8a4 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 49 6e 63  dOp(v, OP_MemInc
1a8a5 72 2c 20 2d 31 2c 20 30 29 3b 0a 20 20 20 20 73  r, -1, 0);.    s
1a8a6 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1a8a7 76 2c 20 4f 50 5f 49 66 4d 65 6d 50 6f 73 2c 20  v, OP_IfMemPos, 
1a8a8 30 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 73 71  0, addr);.    sq
1a8a9 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
1a8aa 65 28 76 2c 20 61 64 64 72 29 3b 0a 20 20 7d 65  e(v, addr);.  }e
1a8ab 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e  lse.#endif..#ifn
1a8ac 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1a8ad 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 20 20  PAGER_PRAGMAS.  
1a8ae 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20  /*.  **  PRAGMA 
1a8af 5b 64 61 74 61 62 61 73 65 2e 5d 63 61 63 68 65  [database.]cache
1a8b0 5f 73 69 7a 65 0a 20 20 2a 2a 20 20 50 52 41 47  _size.  **  PRAG
1a8b1 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 63 61  MA [database.]ca
1a8b2 63 68 65 5f 73 69 7a 65 3d 4e 0a 20 20 2a 2a 0a  che_size=N.  **.
1a8b3 20 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20 66    ** The first f
1a8b4 6f 72 6d 20 72 65 70 6f 72 74 73 20 74 68 65 20  orm reports the 
1a8b5 63 75 72 72 65 6e 74 20 6c 6f 63 61 6c 20 73 65  current local se
1a8b6 74 74 69 6e 67 20 66 6f 72 20 74 68 65 0a 20 20  tting for the.  
1a8b7 2a 2a 20 70 61 67 65 20 63 61 63 68 65 20 73 69  ** page cache si
1a8b8 7a 65 2e 20 20 54 68 65 20 6c 6f 63 61 6c 20 73  ze.  The local s
1a8b9 65 74 74 69 6e 67 20 63 61 6e 20 62 65 20 64 69  etting can be di
1a8ba 66 66 65 72 65 6e 74 20 66 72 6f 6d 0a 20 20 2a  fferent from.  *
1a8bb 2a 20 74 68 65 20 70 65 72 73 69 73 74 65 6e 74  * the persistent
1a8bc 20 63 61 63 68 65 20 73 69 7a 65 20 76 61 6c 75   cache size valu
1a8bd 65 20 74 68 61 74 20 69 73 20 73 74 6f 72 65 64  e that is stored
1a8be 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
1a8bf 0a 20 20 2a 2a 20 66 69 6c 65 20 69 74 73 65 6c  .  ** file itsel
1a8c0 66 2e 20 20 54 68 65 20 76 61 6c 75 65 20 72 65  f.  The value re
1a8c1 74 75 72 6e 65 64 20 69 73 20 74 68 65 20 6d 61  turned is the ma
1a8c2 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 0a  ximum number of.
1a8c3 20 20 2a 2a 20 70 61 67 65 73 20 69 6e 20 74 68    ** pages in th
1a8c4 65 20 70 61 67 65 20 63 61 63 68 65 2e 20 20 54  e page cache.  T
1a8c5 68 65 20 73 65 63 6f 6e 64 20 66 6f 72 6d 20 73  he second form s
1a8c6 65 74 73 20 74 68 65 20 6c 6f 63 61 6c 0a 20 20  ets the local.  
1a8c7 2a 2a 20 70 61 67 65 20 63 61 63 68 65 20 73 69  ** page cache si
1a8c8 7a 65 20 76 61 6c 75 65 2e 20 20 49 74 20 64 6f  ze value.  It do
1a8c9 65 73 20 6e 6f 74 20 63 68 61 6e 67 65 20 74 68  es not change th
1a8ca 65 20 70 65 72 73 69 73 74 65 6e 74 0a 20 20 2a  e persistent.  *
1a8cb 2a 20 63 61 63 68 65 20 73 69 7a 65 20 73 74 6f  * cache size sto
1a8cc 72 65 64 20 6f 6e 20 74 68 65 20 64 69 73 6b 20  red on the disk 
1a8cd 73 6f 20 74 68 65 20 63 61 63 68 65 20 73 69 7a  so the cache siz
1a8ce 65 20 77 69 6c 6c 20 72 65 76 65 72 74 0a 20 20  e will revert.  
1a8cf 2a 2a 20 74 6f 20 69 74 73 20 64 65 66 61 75 6c  ** to its defaul
1a8d0 74 20 76 61 6c 75 65 20 77 68 65 6e 20 74 68 65  t value when the
1a8d1 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6c 6f   database is clo
1a8d2 73 65 64 20 61 6e 64 20 72 65 6f 70 65 6e 65 64  sed and reopened
1a8d3 2e 0a 20 20 2a 2a 20 4e 20 73 68 6f 75 6c 64 20  ..  ** N should 
1a8d4 62 65 20 61 20 70 6f 73 69 74 69 76 65 20 69 6e  be a positive in
1a8d5 74 65 67 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66  teger..  */.  if
1a8d6 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
1a8d7 28 7a 4c 65 66 74 2c 22 63 61 63 68 65 5f 73 69  (zLeft,"cache_si
1a8d8 7a 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69  ze")==0 ){.    i
1a8d9 66 28 20 73 71 6c 69 74 65 33 52 65 61 64 53 63  f( sqlite3ReadSc
1a8da 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 20 67  hema(pParse) ) g
1a8db 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a  oto pragma_out;.
1a8dc 20 20 20 20 69 66 28 20 21 7a 52 69 67 68 74 20      if( !zRight 
1a8dd 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 53  ){.      returnS
1a8de 69 6e 67 6c 65 49 6e 74 28 70 50 61 72 73 65 2c  ingleInt(pParse,
1a8df 20 22 63 61 63 68 65 5f 73 69 7a 65 22 2c 20 70   "cache_size", p
1a8e0 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63  Db->pSchema->cac
1a8e1 68 65 5f 73 69 7a 65 29 3b 0a 20 20 20 20 7d 65  he_size);.    }e
1a8e2 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 73  lse{.      int s
1a8e3 69 7a 65 20 3d 20 61 74 6f 69 28 7a 52 69 67 68  ize = atoi(zRigh
1a8e4 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 73 69  t);.      if( si
1a8e5 7a 65 3c 30 20 29 20 73 69 7a 65 20 3d 20 2d 73  ze<0 ) size = -s
1a8e6 69 7a 65 3b 0a 20 20 20 20 20 20 70 44 62 2d 3e  ize;.      pDb->
1a8e7 70 53 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73  pSchema->cache_s
1a8e8 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 20 20  ize = size;.    
1a8e9 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65    sqlite3BtreeSe
1a8ea 74 43 61 63 68 65 53 69 7a 65 28 70 44 62 2d 3e  tCacheSize(pDb->
1a8eb 70 42 74 2c 20 70 44 62 2d 3e 70 53 63 68 65 6d  pBt, pDb->pSchem
1a8ec 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a  a->cache_size);.
1a8ed 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20      }.  }else.. 
1a8ee 20 2f 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d   /*.  **   PRAGM
1a8ef 41 20 74 65 6d 70 5f 73 74 6f 72 65 0a 20 20 2a  A temp_store.  *
1a8f0 2a 20 20 20 50 52 41 47 4d 41 20 74 65 6d 70 5f  *   PRAGMA temp_
1a8f1 73 74 6f 72 65 20 3d 20 22 64 65 66 61 75 6c 74  store = "default
1a8f2 22 7c 22 6d 65 6d 6f 72 79 22 7c 22 66 69 6c 65  "|"memory"|"file
1a8f3 22 0a 20 20 2a 2a 0a 20 20 2a 2a 20 52 65 74 75  ".  **.  ** Retu
1a8f4 72 6e 20 6f 72 20 73 65 74 20 74 68 65 20 6c 6f  rn or set the lo
1a8f5 63 61 6c 20 76 61 6c 75 65 20 6f 66 20 74 68 65  cal value of the
1a8f6 20 74 65 6d 70 5f 73 74 6f 72 65 20 66 6c 61 67   temp_store flag
1a8f7 2e 20 20 43 68 61 6e 67 69 6e 67 0a 20 20 2a 2a  .  Changing.  **
1a8f8 20 74 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65   the local value
1a8f9 20 64 6f 65 73 20 6e 6f 74 20 6d 61 6b 65 20 63   does not make c
1a8fa 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 64 69  hanges to the di
1a8fb 73 6b 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20  sk file and the 
1a8fc 64 65 66 61 75 6c 74 0a 20 20 2a 2a 20 76 61 6c  default.  ** val
1a8fd 75 65 20 77 69 6c 6c 20 62 65 20 72 65 73 74 6f  ue will be resto
1a8fe 72 65 64 20 74 68 65 20 6e 65 78 74 20 74 69 6d  red the next tim
1a8ff 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  e the database i
1a900 73 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2a 0a 20  s opened..  **. 
1a901 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 74   ** Note that it
1a902 20 69 73 20 70 6f 73 73 69 62 6c 65 20 66 6f 72   is possible for
1a903 20 74 68 65 20 6c 69 62 72 61 72 79 20 63 6f 6d   the library com
1a904 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e  pile-time option
1a905 73 20 74 6f 0a 20 20 2a 2a 20 6f 76 65 72 72 69  s to.  ** overri
1a906 64 65 20 74 68 69 73 20 73 65 74 74 69 6e 67 0a  de this setting.
1a907 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
1a908 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
1a909 20 22 74 65 6d 70 5f 73 74 6f 72 65 22 29 3d 3d   "temp_store")==
1a90a 30 20 29 7b 0a 20 20 20 20 69 66 28 20 21 7a 52  0 ){.    if( !zR
1a90b 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 72 65  ight ){.      re
1a90c 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50  turnSingleInt(pP
1a90d 61 72 73 65 2c 20 22 74 65 6d 70 5f 73 74 6f 72  arse, "temp_stor
1a90e 65 22 2c 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f  e", db->temp_sto
1a90f 72 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  re);.    }else{.
1a910 20 20 20 20 20 20 63 68 61 6e 67 65 54 65 6d 70        changeTemp
1a911 53 74 6f 72 61 67 65 28 70 50 61 72 73 65 2c 20  Storage(pParse, 
1a912 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20  zRight);.    }. 
1a913 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a   }else..  /*.  *
1a914 2a 20 20 20 50 52 41 47 4d 41 20 74 65 6d 70 5f  *   PRAGMA temp_
1a915 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 0a  store_directory.
1a916 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 74 65    **   PRAGMA te
1a917 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63 74 6f  mp_store_directo
1a918 72 79 20 3d 20 22 22 7c 22 64 69 72 65 63 74 6f  ry = ""|"directo
1a919 72 79 5f 6e 61 6d 65 22 0a 20 20 2a 2a 0a 20 20  ry_name".  **.  
1a91a 2a 2a 20 52 65 74 75 72 6e 20 6f 72 20 73 65 74  ** Return or set
1a91b 20 74 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65   the local value
1a91c 20 6f 66 20 74 68 65 20 74 65 6d 70 5f 73 74 6f   of the temp_sto
1a91d 72 65 5f 64 69 72 65 63 74 6f 72 79 20 66 6c 61  re_directory fla
1a91e 67 2e 20 20 43 68 61 6e 67 69 6e 67 0a 20 20 2a  g.  Changing.  *
1a91f 2a 20 74 68 65 20 76 61 6c 75 65 20 73 65 74 73  * the value sets
1a920 20 61 20 73 70 65 63 69 66 69 63 20 64 69 72 65   a specific dire
1a921 63 74 6f 72 79 20 74 6f 20 62 65 20 75 73 65 64  ctory to be used
1a922 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 66   for temporary f
1a923 69 6c 65 73 2e 0a 20 20 2a 2a 20 53 65 74 74 69  iles..  ** Setti
1a924 6e 67 20 74 6f 20 61 20 6e 75 6c 6c 20 73 74 72  ng to a null str
1a925 69 6e 67 20 72 65 76 65 72 74 73 20 74 6f 20 74  ing reverts to t
1a926 68 65 20 64 65 66 61 75 6c 74 20 74 65 6d 70 6f  he default tempo
1a927 72 61 72 79 20 64 69 72 65 63 74 6f 72 79 20 73  rary directory s
1a928 65 61 72 63 68 2e 0a 20 20 2a 2a 20 49 66 20 74  earch..  ** If t
1a929 65 6d 70 6f 72 61 72 79 20 64 69 72 65 63 74 6f  emporary directo
1a92a 72 79 20 69 73 20 63 68 61 6e 67 65 64 2c 20 74  ry is changed, t
1a92b 68 65 6e 20 69 6e 76 61 6c 69 64 61 74 65 54 65  hen invalidateTe
1a92c 6d 70 53 74 6f 72 61 67 65 2e 0a 20 20 2a 2a 0a  mpStorage..  **.
1a92d 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
1a92e 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
1a92f 20 22 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72   "temp_store_dir
1a930 65 63 74 6f 72 79 22 29 3d 3d 30 20 29 7b 0a 20  ectory")==0 ){. 
1a931 20 20 20 69 66 28 20 21 7a 52 69 67 68 74 20 29     if( !zRight )
1a932 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  {.      if( sqli
1a933 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f  te3_temp_directo
1a934 72 79 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ry ){.        sq
1a935 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43  lite3VdbeSetNumC
1a936 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 20  ols(v, 1);.     
1a937 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
1a938 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43  tColName(v, 0, C
1a939 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 0a 20 20  OLNAME_NAME, .  
1a93a 20 20 20 20 20 20 20 20 20 20 22 74 65 6d 70 5f            "temp_
1a93b 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 22  store_directory"
1a93c 2c 20 50 33 5f 53 54 41 54 49 43 29 3b 0a 20 20  , P3_STATIC);.  
1a93d 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1a93e 65 4f 70 33 28 76 2c 20 4f 50 5f 53 74 72 69 6e  eOp3(v, OP_Strin
1a93f 67 38 2c 20 30 2c 20 30 2c 20 73 71 6c 69 74 65  g8, 0, 0, sqlite
1a940 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79  3_temp_directory
1a941 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71  , 0);.        sq
1a942 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1a943 2c 20 4f 50 5f 43 61 6c 6c 62 61 63 6b 2c 20 31  , OP_Callback, 1
1a944 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  , 0);.      }.  
1a945 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
1a946 66 28 20 7a 52 69 67 68 74 5b 30 5d 20 26 26 20  f( zRight[0] && 
1a947 21 73 71 6c 69 74 65 33 4f 73 49 73 44 69 72 57  !sqlite3OsIsDirW
1a948 72 69 74 61 62 6c 65 28 7a 52 69 67 68 74 29 20  ritable(zRight) 
1a949 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1a94a 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
1a94b 65 2c 20 22 6e 6f 74 20 61 20 77 72 69 74 61 62  e, "not a writab
1a94c 6c 65 20 64 69 72 65 63 74 6f 72 79 22 29 3b 0a  le directory");.
1a94d 20 20 20 20 20 20 20 20 67 6f 74 6f 20 70 72 61          goto pra
1a94e 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d  gma_out;.      }
1a94f 0a 20 20 20 20 20 20 69 66 28 20 54 45 4d 50 5f  .      if( TEMP_
1a950 53 54 4f 52 45 3d 3d 30 0a 20 20 20 20 20 20 20  STORE==0.       
1a951 7c 7c 20 28 54 45 4d 50 5f 53 54 4f 52 45 3d 3d  || (TEMP_STORE==
1a952 31 20 26 26 20 64 62 2d 3e 74 65 6d 70 5f 73 74  1 && db->temp_st
1a953 6f 72 65 3c 3d 31 29 0a 20 20 20 20 20 20 20 7c  ore<=1).       |
1a954 7c 20 28 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 32  | (TEMP_STORE==2
1a955 20 26 26 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f   && db->temp_sto
1a956 72 65 3d 3d 31 29 0a 20 20 20 20 20 20 29 7b 0a  re==1).      ){.
1a957 20 20 20 20 20 20 20 20 69 6e 76 61 6c 69 64 61          invalida
1a958 74 65 54 65 6d 70 53 74 6f 72 61 67 65 28 70 50  teTempStorage(pP
1a959 61 72 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20  arse);.      }. 
1a95a 20 20 20 20 20 73 71 6c 69 74 65 46 72 65 65 28       sqliteFree(
1a95b 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72  sqlite3_temp_dir
1a95c 65 63 74 6f 72 79 29 3b 0a 20 20 20 20 20 20 69  ectory);.      i
1a95d 66 28 20 7a 52 69 67 68 74 5b 30 5d 20 29 7b 0a  f( zRight[0] ){.
1a95e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
1a95f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 3d  temp_directory =
1a960 20 7a 52 69 67 68 74 3b 0a 20 20 20 20 20 20 20   zRight;.       
1a961 20 7a 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 20   zRight = 0;.   
1a962 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1a963 20 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64    sqlite3_temp_d
1a964 69 72 65 63 74 6f 72 79 20 3d 20 30 3b 0a 20 20  irectory = 0;.  
1a965 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65      }.    }.  }e
1a966 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20  lse..  /*.  **  
1a967 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
1a968 65 2e 5d 73 79 6e 63 68 72 6f 6e 6f 75 73 0a 20  e.]synchronous. 
1a969 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b 64 61   **   PRAGMA [da
1a96a 74 61 62 61 73 65 2e 5d 73 79 6e 63 68 72 6f 6e  tabase.]synchron
1a96b 6f 75 73 3d 4f 46 46 7c 4f 4e 7c 4e 4f 52 4d 41  ous=OFF|ON|NORMA
1a96c 4c 7c 46 55 4c 4c 0a 20 20 2a 2a 0a 20 20 2a 2a  L|FULL.  **.  **
1a96d 20 52 65 74 75 72 6e 20 6f 72 20 73 65 74 20 74   Return or set t
1a96e 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65 20 6f  he local value o
1a96f 66 20 74 68 65 20 73 79 6e 63 68 72 6f 6e 6f 75  f the synchronou
1a970 73 20 66 6c 61 67 2e 20 20 43 68 61 6e 67 69 6e  s flag.  Changin
1a971 67 0a 20 20 2a 2a 20 74 68 65 20 6c 6f 63 61 6c  g.  ** the local
1a972 20 76 61 6c 75 65 20 64 6f 65 73 20 6e 6f 74 20   value does not 
1a973 6d 61 6b 65 20 63 68 61 6e 67 65 73 20 74 6f 20  make changes to 
1a974 74 68 65 20 64 69 73 6b 20 66 69 6c 65 20 61 6e  the disk file an
1a975 64 20 74 68 65 0a 20 20 2a 2a 20 64 65 66 61 75  d the.  ** defau
1a976 6c 74 20 76 61 6c 75 65 20 77 69 6c 6c 20 62 65  lt value will be
1a977 20 72 65 73 74 6f 72 65 64 20 74 68 65 20 6e 65   restored the ne
1a978 78 74 20 74 69 6d 65 20 74 68 65 20 64 61 74 61  xt time the data
1a979 62 61 73 65 20 69 73 0a 20 20 2a 2a 20 6f 70 65  base is.  ** ope
1a97a 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ned..  */.  if( 
1a97b 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
1a97c 4c 65 66 74 2c 22 73 79 6e 63 68 72 6f 6e 6f 75  Left,"synchronou
1a97d 73 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66  s")==0 ){.    if
1a97e 28 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68  ( sqlite3ReadSch
1a97f 65 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f  ema(pParse) ) go
1a980 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20  to pragma_out;. 
1a981 20 20 20 69 66 28 20 21 7a 52 69 67 68 74 20 29     if( !zRight )
1a982 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 53 69  {.      returnSi
1a983 6e 67 6c 65 49 6e 74 28 70 50 61 72 73 65 2c 20  ngleInt(pParse, 
1a984 22 73 79 6e 63 68 72 6f 6e 6f 75 73 22 2c 20 70  "synchronous", p
1a985 44 62 2d 3e 73 61 66 65 74 79 5f 6c 65 76 65 6c  Db->safety_level
1a986 2d 31 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  -1);.    }else{.
1a987 20 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e 61        if( !db->a
1a988 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20  utoCommit ){.   
1a989 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
1a98a 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20  rMsg(pParse, .  
1a98b 20 20 20 20 20 20 20 20 20 20 22 53 61 66 65 74            "Safet
1a98c 79 20 6c 65 76 65 6c 20 6d 61 79 20 6e 6f 74 20  y level may not 
1a98d 62 65 20 63 68 61 6e 67 65 64 20 69 6e 73 69 64  be changed insid
1a98e 65 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 22  e a transaction"
1a98f 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
1a990 20 20 20 20 20 20 20 20 70 44 62 2d 3e 73 61 66          pDb->saf
1a991 65 74 79 5f 6c 65 76 65 6c 20 3d 20 67 65 74 53  ety_level = getS
1a992 61 66 65 74 79 4c 65 76 65 6c 28 7a 52 69 67 68  afetyLevel(zRigh
1a993 74 29 2b 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20  t)+1;.      }.  
1a994 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64    }.  }else.#end
1a995 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
1a996 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 20  T_PAGER_PRAGMAS 
1a997 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
1a998 54 45 5f 4f 4d 49 54 5f 46 4c 41 47 5f 50 52 41  TE_OMIT_FLAG_PRA
1a999 47 4d 41 53 0a 20 20 69 66 28 20 66 6c 61 67 50  GMAS.  if( flagP
1a99a 72 61 67 6d 61 28 70 50 61 72 73 65 2c 20 7a 4c  ragma(pParse, zL
1a99b 65 66 74 2c 20 7a 52 69 67 68 74 29 20 29 7b 0a  eft, zRight) ){.
1a99c 20 20 20 20 2f 2a 20 54 68 65 20 66 6c 61 67 50      /* The flagP
1a99d 72 61 67 6d 61 28 29 20 73 75 62 72 6f 75 74 69  ragma() subrouti
1a99e 6e 65 20 61 6c 73 6f 20 67 65 6e 65 72 61 74 65  ne also generate
1a99f 73 20 61 6e 79 20 6e 65 63 65 73 73 61 72 79 20  s any necessary 
1a9a0 63 6f 64 65 0a 20 20 20 20 2a 2a 20 74 68 65 72  code.    ** ther
1a9a1 65 20 69 73 20 6e 6f 74 68 69 6e 67 20 6d 6f 72  e is nothing mor
1a9a2 65 20 74 6f 20 64 6f 20 68 65 72 65 20 2a 2f 0a  e to do here */.
1a9a3 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f    }else.#endif /
1a9a4 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  * SQLITE_OMIT_FL
1a9a5 41 47 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 23  AG_PRAGMAS */..#
1a9a6 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1a9a7 49 54 5f 53 43 48 45 4d 41 5f 50 52 41 47 4d 41  IT_SCHEMA_PRAGMA
1a9a8 53 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 20 50 52  S.  /*.  **   PR
1a9a9 41 47 4d 41 20 74 61 62 6c 65 5f 69 6e 66 6f 28  AGMA table_info(
1a9aa 3c 74 61 62 6c 65 3e 29 0a 20 20 2a 2a 0a 20 20  <table>).  **.  
1a9ab 2a 2a 20 52 65 74 75 72 6e 20 61 20 73 69 6e 67  ** Return a sing
1a9ac 6c 65 20 72 6f 77 20 66 6f 72 20 65 61 63 68 20  le row for each 
1a9ad 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 6e 61  column of the na
1a9ae 6d 65 64 20 74 61 62 6c 65 2e 20 54 68 65 20 63  med table. The c
1a9af 6f 6c 75 6d 6e 73 20 6f 66 0a 20 20 2a 2a 20 74  olumns of.  ** t
1a9b0 68 65 20 72 65 74 75 72 6e 65 64 20 64 61 74 61  he returned data
1a9b1 20 73 65 74 20 61 72 65 3a 0a 20 20 2a 2a 0a 20   set are:.  **. 
1a9b2 20 2a 2a 20 63 69 64 3a 20 20 20 20 20 20 20 20   ** cid:        
1a9b3 43 6f 6c 75 6d 6e 20 69 64 20 28 6e 75 6d 62 65  Column id (numbe
1a9b4 72 65 64 20 66 72 6f 6d 20 6c 65 66 74 20 74 6f  red from left to
1a9b5 20 72 69 67 68 74 2c 20 73 74 61 72 74 69 6e 67   right, starting
1a9b6 20 61 74 20 30 29 0a 20 20 2a 2a 20 6e 61 6d 65   at 0).  ** name
1a9b7 3a 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 6e  :       Column n
1a9b8 61 6d 65 0a 20 20 2a 2a 20 74 79 70 65 3a 20 20  ame.  ** type:  
1a9b9 20 20 20 20 20 43 6f 6c 75 6d 6e 20 64 65 63 6c       Column decl
1a9ba 61 72 61 74 69 6f 6e 20 74 79 70 65 2e 0a 20 20  aration type..  
1a9bb 2a 2a 20 6e 6f 74 6e 75 6c 6c 3a 20 20 20 20 54  ** notnull:    T
1a9bc 72 75 65 20 69 66 20 27 4e 4f 54 20 4e 55 4c 4c  rue if 'NOT NULL
1a9bd 27 20 69 73 20 70 61 72 74 20 6f 66 20 63 6f 6c  ' is part of col
1a9be 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 0a  umn declaration.
1a9bf 20 20 2a 2a 20 64 66 6c 74 5f 76 61 6c 75 65 3a    ** dflt_value:
1a9c0 20 54 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c   The default val
1a9c1 75 65 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d  ue for the colum
1a9c2 6e 2c 20 69 66 20 61 6e 79 2e 0a 20 20 2a 2f 0a  n, if any..  */.
1a9c3 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
1a9c4 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 74 61 62  ICmp(zLeft, "tab
1a9c5 6c 65 5f 69 6e 66 6f 22 29 3d 3d 30 20 26 26 20  le_info")==0 && 
1a9c6 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 54 61  zRight ){.    Ta
1a9c7 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 69  ble *pTab;.    i
1a9c8 66 28 20 73 71 6c 69 74 65 33 52 65 61 64 53 63  f( sqlite3ReadSc
1a9c9 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 20 67  hema(pParse) ) g
1a9ca 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a  oto pragma_out;.
1a9cb 20 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74      pTab = sqlit
1a9cc 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20  e3FindTable(db, 
1a9cd 7a 52 69 67 68 74 2c 20 7a 44 62 29 3b 0a 20 20  zRight, zDb);.  
1a9ce 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20    if( pTab ){.  
1a9cf 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20      int i;.     
1a9d0 20 69 6e 74 20 6e 48 69 64 64 65 6e 20 3d 20 30   int nHidden = 0
1a9d1 3b 0a 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 2a  ;.      Column *
1a9d2 70 43 6f 6c 3b 0a 20 20 20 20 20 20 73 71 6c 69  pCol;.      sqli
1a9d3 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c  te3VdbeSetNumCol
1a9d4 73 28 76 2c 20 36 29 3b 0a 20 20 20 20 20 20 73  s(v, 6);.      s
1a9d5 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
1a9d6 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41  Name(v, 0, COLNA
1a9d7 4d 45 5f 4e 41 4d 45 2c 20 22 63 69 64 22 2c 20  ME_NAME, "cid", 
1a9d8 50 33 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  P3_STATIC);.    
1a9d9 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
1a9da 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c 20 43 4f  ColName(v, 1, CO
1a9db 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d  LNAME_NAME, "nam
1a9dc 65 22 2c 20 50 33 5f 53 54 41 54 49 43 29 3b 0a  e", P3_STATIC);.
1a9dd 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1a9de 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 32  eSetColName(v, 2
1a9df 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
1a9e0 22 74 79 70 65 22 2c 20 50 33 5f 53 54 41 54 49  "type", P3_STATI
1a9e1 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  C);.      sqlite
1a9e2 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
1a9e3 76 2c 20 33 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 3, COLNAME_NA
1a9e4 4d 45 2c 20 22 6e 6f 74 6e 75 6c 6c 22 2c 20 50  ME, "notnull", P
1a9e5 33 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  3_STATIC);.     
1a9e6 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
1a9e7 6f 6c 4e 61 6d 65 28 76 2c 20 34 2c 20 43 4f 4c  olName(v, 4, COL
1a9e8 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 64 66 6c 74  NAME_NAME, "dflt
1a9e9 5f 76 61 6c 75 65 22 2c 20 50 33 5f 53 54 41 54  _value", P3_STAT
1a9ea 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  IC);.      sqlit
1a9eb 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
1a9ec 28 76 2c 20 35 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 5, COLNAME_N
1a9ed 41 4d 45 2c 20 22 70 6b 22 2c 20 50 33 5f 53 54  AME, "pk", P3_ST
1a9ee 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c  ATIC);.      sql
1a9ef 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d  ite3ViewGetColum
1a9f0 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70  nNames(pParse, p
1a9f1 54 61 62 29 3b 0a 20 20 20 20 20 20 66 6f 72 28  Tab);.      for(
1a9f2 69 3d 30 2c 20 70 43 6f 6c 3d 70 54 61 62 2d 3e  i=0, pCol=pTab->
1a9f3 61 43 6f 6c 3b 20 69 3c 70 54 61 62 2d 3e 6e 43  aCol; i<pTab->nC
1a9f4 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29  ol; i++, pCol++)
1a9f5 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20  {.        const 
1a9f6 54 6f 6b 65 6e 20 2a 70 44 66 6c 74 3b 0a 20 20  Token *pDflt;.  
1a9f7 20 20 20 20 20 20 69 66 28 20 49 73 48 69 64 64        if( IsHidd
1a9f8 65 6e 43 6f 6c 75 6d 6e 28 70 43 6f 6c 29 20 29  enColumn(pCol) )
1a9f9 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 48 69 64  {.          nHid
1a9fa 64 65 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20  den++;.         
1a9fb 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
1a9fc 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c     }.        sql
1a9fd 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1a9fe 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2d 6e   OP_Integer, i-n
1a9ff 48 69 64 64 65 6e 2c 20 30 29 3b 0a 20 20 20 20  Hidden, 0);.    
1aa00 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4f      sqlite3VdbeO
1aa01 70 33 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p3(v, OP_String8
1aa02 2c 20 30 2c 20 30 2c 20 70 43 6f 6c 2d 3e 7a 4e  , 0, 0, pCol->zN
1aa03 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  ame, 0);.       
1aa04 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28   sqlite3VdbeOp3(
1aa05 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
1aa06 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20  , 0,.           
1aa07 70 43 6f 6c 2d 3e 7a 54 79 70 65 20 3f 20 70 43  pCol->zType ? pC
1aa08 6f 6c 2d 3e 7a 54 79 70 65 20 3a 20 22 22 2c 20  ol->zType : "", 
1aa09 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  0);.        sqli
1aa0a 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1aa0b 4f 50 5f 49 6e 74 65 67 65 72 2c 20 70 43 6f 6c  OP_Integer, pCol
1aa0c 2d 3e 6e 6f 74 4e 75 6c 6c 2c 20 30 29 3b 0a 20  ->notNull, 0);. 
1aa0d 20 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 2d         if( pCol-
1aa0e 3e 70 44 66 6c 74 20 26 26 20 28 70 44 66 6c 74  >pDflt && (pDflt
1aa0f 20 3d 20 26 70 43 6f 6c 2d 3e 70 44 66 6c 74 2d   = &pCol->pDflt-
1aa10 3e 73 70 61 6e 29 2d 3e 7a 20 29 7b 0a 20 20 20  >span)->z ){.   
1aa11 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1aa12 62 65 4f 70 33 28 76 2c 20 4f 50 5f 53 74 72 69  beOp3(v, OP_Stri
1aa13 6e 67 38 2c 20 30 2c 20 30 2c 20 28 63 68 61 72  ng8, 0, 0, (char
1aa14 2a 29 70 44 66 6c 74 2d 3e 7a 2c 20 70 44 66 6c  *)pDflt->z, pDfl
1aa15 74 2d 3e 6e 29 3b 0a 20 20 20 20 20 20 20 20 7d  t->n);.        }
1aa16 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
1aa17 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1aa18 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
1aa19 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  0);.        }.  
1aa1a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1aa1b 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 74  eAddOp(v, OP_Int
1aa1c 65 67 65 72 2c 20 70 43 6f 6c 2d 3e 69 73 50 72  eger, pCol->isPr
1aa1d 69 6d 4b 65 79 2c 20 30 29 3b 0a 20 20 20 20 20  imKey, 0);.     
1aa1e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1aa1f 64 4f 70 28 76 2c 20 4f 50 5f 43 61 6c 6c 62 61  dOp(v, OP_Callba
1aa20 63 6b 2c 20 36 2c 20 30 29 3b 0a 20 20 20 20 20  ck, 6, 0);.     
1aa21 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65   }.    }.  }else
1aa22 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  ..  if( sqlite3S
1aa23 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 69  trICmp(zLeft, "i
1aa24 6e 64 65 78 5f 69 6e 66 6f 22 29 3d 3d 30 20 26  ndex_info")==0 &
1aa25 26 20 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20  & zRight ){.    
1aa26 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20  Index *pIdx;.   
1aa27 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20   Table *pTab;.  
1aa28 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 61    if( sqlite3Rea
1aa29 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20  dSchema(pParse) 
1aa2a 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75  ) goto pragma_ou
1aa2b 74 3b 0a 20 20 20 20 70 49 64 78 20 3d 20 73 71  t;.    pIdx = sq
1aa2c 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 64  lite3FindIndex(d
1aa2d 62 2c 20 7a 52 69 67 68 74 2c 20 7a 44 62 29 3b  b, zRight, zDb);
1aa2e 0a 20 20 20 20 69 66 28 20 70 49 64 78 20 29 7b  .    if( pIdx ){
1aa2f 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  .      int i;.  
1aa30 20 20 20 20 70 54 61 62 20 3d 20 70 49 64 78 2d      pTab = pIdx-
1aa31 3e 70 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 73  >pTable;.      s
1aa32 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
1aa33 43 6f 6c 73 28 76 2c 20 33 29 3b 0a 20 20 20 20  Cols(v, 3);.    
1aa34 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
1aa35 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f  ColName(v, 0, CO
1aa36 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71  LNAME_NAME, "seq
1aa37 6e 6f 22 2c 20 50 33 5f 53 54 41 54 49 43 29 3b  no", P3_STATIC);
1aa38 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1aa39 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
1aa3a 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  1, COLNAME_NAME,
1aa3b 20 22 63 69 64 22 2c 20 50 33 5f 53 54 41 54 49   "cid", P3_STATI
1aa3c 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  C);.      sqlite
1aa3d 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
1aa3e 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 2, COLNAME_NA
1aa3f 4d 45 2c 20 22 6e 61 6d 65 22 2c 20 50 33 5f 53  ME, "name", P3_S
1aa40 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 66 6f  TATIC);.      fo
1aa41 72 28 69 3d 30 3b 20 69 3c 70 49 64 78 2d 3e 6e  r(i=0; i<pIdx->n
1aa42 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Column; i++){.  
1aa43 20 20 20 20 20 20 69 6e 74 20 63 6e 75 6d 20 3d        int cnum =
1aa44 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b   pIdx->aiColumn[
1aa45 69 5d 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  i];.        sqli
1aa46 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1aa47 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20 30  OP_Integer, i, 0
1aa48 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
1aa49 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1aa4a 50 5f 49 6e 74 65 67 65 72 2c 20 63 6e 75 6d 2c  P_Integer, cnum,
1aa4b 20 30 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73   0);.        ass
1aa4c 65 72 74 28 20 70 54 61 62 2d 3e 6e 43 6f 6c 3e  ert( pTab->nCol>
1aa4d 63 6e 75 6d 20 29 3b 0a 20 20 20 20 20 20 20 20  cnum );.        
1aa4e 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28 76  sqlite3VdbeOp3(v
1aa4f 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c  , OP_String8, 0,
1aa50 20 30 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 63   0, pTab->aCol[c
1aa51 6e 75 6d 5d 2e 7a 4e 61 6d 65 2c 20 30 29 3b 0a  num].zName, 0);.
1aa52 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1aa53 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43  dbeAddOp(v, OP_C
1aa54 61 6c 6c 62 61 63 6b 2c 20 33 2c 20 30 29 3b 0a  allback, 3, 0);.
1aa55 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1aa56 7d 65 6c 73 65 0a 0a 20 20 69 66 28 20 73 71 6c  }else..  if( sql
1aa57 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
1aa58 74 2c 20 22 69 6e 64 65 78 5f 6c 69 73 74 22 29  t, "index_list")
1aa59 3d 3d 30 20 26 26 20 7a 52 69 67 68 74 20 29 7b  ==0 && zRight ){
1aa5a 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78  .    Index *pIdx
1aa5b 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61  ;.    Table *pTa
1aa5c 62 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  b;.    if( sqlit
1aa5d 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61  e3ReadSchema(pPa
1aa5e 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67  rse) ) goto prag
1aa5f 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 70 54 61 62  ma_out;.    pTab
1aa60 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61   = sqlite3FindTa
1aa61 62 6c 65 28 64 62 2c 20 7a 52 69 67 68 74 2c 20  ble(db, zRight, 
1aa62 7a 44 62 29 3b 0a 20 20 20 20 69 66 28 20 70 54  zDb);.    if( pT
1aa63 61 62 20 29 7b 0a 20 20 20 20 20 20 76 20 3d 20  ab ){.      v = 
1aa64 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
1aa65 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 70 49  Parse);.      pI
1aa66 64 78 20 3d 20 70 54 61 62 2d 3e 70 49 6e 64 65  dx = pTab->pInde
1aa67 78 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 64  x;.      if( pId
1aa68 78 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  x ){.        int
1aa69 20 69 20 3d 20 30 3b 20 0a 20 20 20 20 20 20 20   i = 0; .       
1aa6a 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e   sqlite3VdbeSetN
1aa6b 75 6d 43 6f 6c 73 28 76 2c 20 33 29 3b 0a 20 20  umCols(v, 3);.  
1aa6c 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1aa6d 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30  eSetColName(v, 0
1aa6e 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
1aa6f 22 73 65 71 22 2c 20 50 33 5f 53 54 41 54 49 43  "seq", P3_STATIC
1aa70 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
1aa71 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
1aa72 28 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 1, COLNAME_N
1aa73 41 4d 45 2c 20 22 6e 61 6d 65 22 2c 20 50 33 5f  AME, "name", P3_
1aa74 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20  STATIC);.       
1aa75 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
1aa76 6f 6c 4e 61 6d 65 28 76 2c 20 32 2c 20 43 4f 4c  olName(v, 2, COL
1aa77 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 75 6e 69 71  NAME_NAME, "uniq
1aa78 75 65 22 2c 20 50 33 5f 53 54 41 54 49 43 29 3b  ue", P3_STATIC);
1aa79 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 70  .        while(p
1aa7a 49 64 78 29 7b 0a 20 20 20 20 20 20 20 20 20 20  Idx){.          
1aa7b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1aa7c 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
1aa7d 69 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20  i, 0);.         
1aa7e 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28   sqlite3VdbeOp3(
1aa7f 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
1aa80 2c 20 30 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65  , 0, pIdx->zName
1aa81 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 0);.          
1aa82 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1aa83 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
1aa84 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f  pIdx->onError!=O
1aa85 45 5f 4e 6f 6e 65 2c 20 30 29 3b 0a 20 20 20 20  E_None, 0);.    
1aa86 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1aa87 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 61 6c  eAddOp(v, OP_Cal
1aa88 6c 62 61 63 6b 2c 20 33 2c 20 30 29 3b 0a 20 20  lback, 3, 0);.  
1aa89 20 20 20 20 20 20 20 20 2b 2b 69 3b 0a 20 20 20          ++i;.   
1aa8a 20 20 20 20 20 20 20 70 49 64 78 20 3d 20 70 49         pIdx = pI
1aa8b 64 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20  dx->pNext;.     
1aa8c 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
1aa8d 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 69 66   }.  }else..  if
1aa8e 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
1aa8f 28 7a 4c 65 66 74 2c 20 22 64 61 74 61 62 61 73  (zLeft, "databas
1aa90 65 5f 6c 69 73 74 22 29 3d 3d 30 20 29 7b 0a 20  e_list")==0 ){. 
1aa91 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 69 66     int i;.    if
1aa92 28 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68  ( sqlite3ReadSch
1aa93 65 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f  ema(pParse) ) go
1aa94 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20  to pragma_out;. 
1aa95 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
1aa96 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 33 29 3b 0a  tNumCols(v, 3);.
1aa97 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
1aa98 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20  etColName(v, 0, 
1aa99 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73  COLNAME_NAME, "s
1aa9a 65 71 22 2c 20 50 33 5f 53 54 41 54 49 43 29 3b  eq", P3_STATIC);
1aa9b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1aa9c 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c  SetColName(v, 1,
1aa9d 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
1aa9e 6e 61 6d 65 22 2c 20 50 33 5f 53 54 41 54 49 43  name", P3_STATIC
1aa9f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1aaa0 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
1aaa1 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  2, COLNAME_NAME,
1aaa2 20 22 66 69 6c 65 22 2c 20 50 33 5f 53 54 41 54   "file", P3_STAT
1aaa3 49 43 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  IC);.    for(i=0
1aaa4 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
1aaa5 29 7b 0a 20 20 20 20 20 20 69 66 28 20 64 62 2d  ){.      if( db-
1aaa6 3e 61 44 62 5b 69 5d 2e 70 42 74 3d 3d 30 20 29  >aDb[i].pBt==0 )
1aaa7 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
1aaa8 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62   assert( db->aDb
1aaa9 5b 69 5d 2e 7a 4e 61 6d 65 21 3d 30 20 29 3b 0a  [i].zName!=0 );.
1aaaa 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1aaab 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 74  eAddOp(v, OP_Int
1aaac 65 67 65 72 2c 20 69 2c 20 30 29 3b 0a 20 20 20  eger, i, 0);.   
1aaad 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70     sqlite3VdbeOp
1aaae 33 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  3(v, OP_String8,
1aaaf 20 30 2c 20 30 2c 20 64 62 2d 3e 61 44 62 5b 69   0, 0, db->aDb[i
1aab0 5d 2e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20  ].zName, 0);.   
1aab1 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70     sqlite3VdbeOp
1aab2 33 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  3(v, OP_String8,
1aab3 20 30 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20   0, 0,.         
1aab4 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65    sqlite3BtreeGe
1aab5 74 46 69 6c 65 6e 61 6d 65 28 64 62 2d 3e 61 44  tFilename(db->aD
1aab6 62 5b 69 5d 2e 70 42 74 29 2c 20 30 29 3b 0a 20  b[i].pBt), 0);. 
1aab7 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1aab8 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 61 6c 6c  AddOp(v, OP_Call
1aab9 62 61 63 6b 2c 20 33 2c 20 30 29 3b 0a 20 20 20  back, 3, 0);.   
1aaba 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 69 66   }.  }else..  if
1aabb 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
1aabc 28 7a 4c 65 66 74 2c 20 22 63 6f 6c 6c 61 74 69  (zLeft, "collati
1aabd 6f 6e 5f 6c 69 73 74 22 29 3d 3d 30 20 29 7b 0a  on_list")==0 ){.
1aabe 20 20 20 20 69 6e 74 20 69 20 3d 20 30 3b 0a 20      int i = 0;. 
1aabf 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 3b 0a     HashElem *p;.
1aac0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
1aac1 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 32 29 3b  etNumCols(v, 2);
1aac2 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1aac3 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c  SetColName(v, 0,
1aac4 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
1aac5 73 65 71 22 2c 20 50 33 5f 53 54 41 54 49 43 29  seq", P3_STATIC)
1aac6 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
1aac7 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31  eSetColName(v, 1
1aac8 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
1aac9 22 6e 61 6d 65 22 2c 20 50 33 5f 53 54 41 54 49  "name", P3_STATI
1aaca 43 29 3b 0a 20 20 20 20 66 6f 72 28 70 3d 73 71  C);.    for(p=sq
1aacb 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 26 64  liteHashFirst(&d
1aacc 62 2d 3e 61 43 6f 6c 6c 53 65 71 29 3b 20 70 3b  b->aCollSeq); p;
1aacd 20 70 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78   p=sqliteHashNex
1aace 74 28 70 29 29 7b 0a 20 20 20 20 20 20 43 6f 6c  t(p)){.      Col
1aacf 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 28 43  lSeq *pColl = (C
1aad0 6f 6c 6c 53 65 71 20 2a 29 73 71 6c 69 74 65 48  ollSeq *)sqliteH
1aad1 61 73 68 44 61 74 61 28 70 29 3b 0a 20 20 20 20  ashData(p);.    
1aad2 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1aad3 4f 70 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  Op(v, OP_Integer
1aad4 2c 20 69 2b 2b 2c 20 30 29 3b 0a 20 20 20 20 20  , i++, 0);.     
1aad5 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28   sqlite3VdbeOp3(
1aad6 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
1aad7 2c 20 30 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d  , 0, pColl->zNam
1aad8 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c  e, 0);.      sql
1aad9 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1aada 20 4f 50 5f 43 61 6c 6c 62 61 63 6b 2c 20 32 2c   OP_Callback, 2,
1aadb 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c   0);.    }.  }el
1aadc 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  se.#endif /* SQL
1aadd 49 54 45 5f 4f 4d 49 54 5f 53 43 48 45 4d 41 5f  ITE_OMIT_SCHEMA_
1aade 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 23 69 66 6e  PRAGMAS */..#ifn
1aadf 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1aae0 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 69 66  FOREIGN_KEY.  if
1aae1 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
1aae2 28 7a 4c 65 66 74 2c 20 22 66 6f 72 65 69 67 6e  (zLeft, "foreign
1aae3 5f 6b 65 79 5f 6c 69 73 74 22 29 3d 3d 30 20 26  _key_list")==0 &
1aae4 26 20 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20  & zRight ){.    
1aae5 46 4b 65 79 20 2a 70 46 4b 3b 0a 20 20 20 20 54  FKey *pFK;.    T
1aae6 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20  able *pTab;.    
1aae7 69 66 28 20 73 71 6c 69 74 65 33 52 65 61 64 53  if( sqlite3ReadS
1aae8 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 20  chema(pParse) ) 
1aae9 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b  goto pragma_out;
1aaea 0a 20 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69  .    pTab = sqli
1aaeb 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c  te3FindTable(db,
1aaec 20 7a 52 69 67 68 74 2c 20 7a 44 62 29 3b 0a 20   zRight, zDb);. 
1aaed 20 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20     if( pTab ){. 
1aaee 20 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33       v = sqlite3
1aaef 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
1aaf0 0a 20 20 20 20 20 20 70 46 4b 20 3d 20 70 54 61  .      pFK = pTa
1aaf1 62 2d 3e 70 46 4b 65 79 3b 0a 20 20 20 20 20 20  b->pFKey;.      
1aaf2 69 66 28 20 70 46 4b 20 29 7b 0a 20 20 20 20 20  if( pFK ){.     
1aaf3 20 20 20 69 6e 74 20 69 20 3d 20 30 3b 20 0a 20     int i = 0; . 
1aaf4 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1aaf5 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20  beSetNumCols(v, 
1aaf6 35 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  5);.        sqli
1aaf7 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
1aaf8 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 0, COLNAME_
1aaf9 4e 41 4d 45 2c 20 22 69 64 22 2c 20 50 33 5f 53  NAME, "id", P3_S
1aafa 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20  TATIC);.        
1aafb 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
1aafc 6c 4e 61 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e  lName(v, 1, COLN
1aafd 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71 22 2c  AME_NAME, "seq",
1aafe 20 50 33 5f 53 54 41 54 49 43 29 3b 0a 20 20 20   P3_STATIC);.   
1aaff 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1ab00 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 32 2c  SetColName(v, 2,
1ab01 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
1ab02 74 61 62 6c 65 22 2c 20 50 33 5f 53 54 41 54 49  table", P3_STATI
1ab03 43 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  C);.        sqli
1ab04 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
1ab05 65 28 76 2c 20 33 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 3, COLNAME_
1ab06 4e 41 4d 45 2c 20 22 66 72 6f 6d 22 2c 20 50 33  NAME, "from", P3
1ab07 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
1ab08 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
1ab09 43 6f 6c 4e 61 6d 65 28 76 2c 20 34 2c 20 43 4f  ColName(v, 4, CO
1ab0a 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 74 6f 22  LNAME_NAME, "to"
1ab0b 2c 20 50 33 5f 53 54 41 54 49 43 29 3b 0a 20 20  , P3_STATIC);.  
1ab0c 20 20 20 20 20 20 77 68 69 6c 65 28 70 46 4b 29        while(pFK)
1ab0d 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20  {.          int 
1ab0e 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72  j;.          for
1ab0f 28 6a 3d 30 3b 20 6a 3c 70 46 4b 2d 3e 6e 43 6f  (j=0; j<pFK->nCo
1ab10 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  l; j++){.       
1ab11 20 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20       char *zCol 
1ab12 3d 20 70 46 4b 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a  = pFK->aCol[j].z
1ab13 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 20  Col;.           
1ab14 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1ab15 70 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  p(v, OP_Integer,
1ab16 20 69 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20   i, 0);.        
1ab17 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1ab18 64 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 74 65 67  ddOp(v, OP_Integ
1ab19 65 72 2c 20 6a 2c 20 30 29 3b 0a 20 20 20 20 20  er, j, 0);.     
1ab1a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1ab1b 62 65 4f 70 33 28 76 2c 20 4f 50 5f 53 74 72 69  beOp3(v, OP_Stri
1ab1c 6e 67 38 2c 20 30 2c 20 30 2c 20 70 46 4b 2d 3e  ng8, 0, 0, pFK->
1ab1d 7a 54 6f 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  zTo, 0);.       
1ab1e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1ab1f 4f 70 33 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op3(v, OP_String
1ab20 38 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20 20 20  8, 0, 0,.       
1ab21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ab22 20 20 20 20 20 20 70 54 61 62 2d 3e 61 43 6f 6c        pTab->aCol
1ab23 5b 70 46 4b 2d 3e 61 43 6f 6c 5b 6a 5d 2e 69 46  [pFK->aCol[j].iF
1ab24 72 6f 6d 5d 2e 7a 4e 61 6d 65 2c 20 30 29 3b 0a  rom].zName, 0);.
1ab25 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
1ab26 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20 7a 43  te3VdbeOp3(v, zC
1ab27 6f 6c 20 3f 20 4f 50 5f 53 74 72 69 6e 67 38 20  ol ? OP_String8 
1ab28 3a 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 30 2c  : OP_Null, 0, 0,
1ab29 20 7a 43 6f 6c 2c 20 30 29 3b 0a 20 20 20 20 20   zCol, 0);.     
1ab2a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1ab2b 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 61  beAddOp(v, OP_Ca
1ab2c 6c 6c 62 61 63 6b 2c 20 35 2c 20 30 29 3b 0a 20  llback, 5, 0);. 
1ab2d 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1ab2e 20 20 20 20 20 2b 2b 69 3b 0a 20 20 20 20 20 20       ++i;.      
1ab2f 20 20 20 20 70 46 4b 20 3d 20 70 46 4b 2d 3e 70      pFK = pFK->p
1ab30 4e 65 78 74 46 72 6f 6d 3b 0a 20 20 20 20 20 20  NextFrom;.      
1ab31 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
1ab32 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66  }.  }else.#endif
1ab33 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c   /* !defined(SQL
1ab34 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e  ITE_OMIT_FOREIGN
1ab35 5f 4b 45 59 29 20 2a 2f 0a 0a 23 69 66 6e 64 65  _KEY) */..#ifnde
1ab36 66 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20 73  f NDEBUG.  if( s
1ab37 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
1ab38 65 66 74 2c 20 22 70 61 72 73 65 72 5f 74 72 61  eft, "parser_tra
1ab39 63 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69  ce")==0 ){.    i
1ab3a 66 28 20 7a 52 69 67 68 74 20 29 7b 0a 20 20 20  f( zRight ){.   
1ab3b 20 20 20 69 66 28 20 67 65 74 42 6f 6f 6c 65 61     if( getBoolea
1ab3c 6e 28 7a 52 69 67 68 74 29 20 29 7b 0a 20 20 20  n(zRight) ){.   
1ab3d 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 72 73       sqlite3Pars
1ab3e 65 72 54 72 61 63 65 28 73 74 64 65 72 72 2c 20  erTrace(stderr, 
1ab3f 22 70 61 72 73 65 72 3a 20 22 29 3b 0a 20 20 20  "parser: ");.   
1ab40 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1ab41 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 54    sqlite3ParserT
1ab42 72 61 63 65 28 30 2c 20 30 29 3b 0a 20 20 20 20  race(0, 0);.    
1ab43 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73    }.    }.  }els
1ab44 65 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52  e.#endif..  /* R
1ab45 65 69 6e 73 74 61 6c 6c 20 74 68 65 20 4c 49 4b  einstall the LIK
1ab46 45 20 61 6e 64 20 47 4c 4f 42 20 66 75 6e 63 74  E and GLOB funct
1ab47 69 6f 6e 73 2e 20 20 54 68 65 20 76 61 72 69 61  ions.  The varia
1ab48 6e 74 20 6f 66 20 4c 49 4b 45 0a 20 20 2a 2a 20  nt of LIKE.  ** 
1ab49 75 73 65 64 20 77 69 6c 6c 20 62 65 20 63 61 73  used will be cas
1ab4a 65 20 73 65 6e 73 69 74 69 76 65 20 6f 72 20 6e  e sensitive or n
1ab4b 6f 74 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20  ot depending on 
1ab4c 74 68 65 20 52 48 53 2e 0a 20 20 2a 2f 0a 20 20  the RHS..  */.  
1ab4d 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
1ab4e 6d 70 28 7a 4c 65 66 74 2c 20 22 63 61 73 65 5f  mp(zLeft, "case_
1ab4f 73 65 6e 73 69 74 69 76 65 5f 6c 69 6b 65 22 29  sensitive_like")
1ab50 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 7a  ==0 ){.    if( z
1ab51 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 73  Right ){.      s
1ab52 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 4c 69  qlite3RegisterLi
1ab53 6b 65 46 75 6e 63 74 69 6f 6e 73 28 64 62 2c 20  keFunctions(db, 
1ab54 67 65 74 42 6f 6f 6c 65 61 6e 28 7a 52 69 67 68  getBoolean(zRigh
1ab55 74 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  t));.    }.  }el
1ab56 73 65 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  se..#ifndef SQLI
1ab57 54 45 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45  TE_INTEGRITY_CHE
1ab58 43 4b 5f 45 52 52 4f 52 5f 4d 41 58 0a 23 20 64  CK_ERROR_MAX.# d
1ab59 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54  efine SQLITE_INT
1ab5a 45 47 52 49 54 59 5f 43 48 45 43 4b 5f 45 52 52  EGRITY_CHECK_ERR
1ab5b 4f 52 5f 4d 41 58 20 31 30 30 0a 23 65 6e 64 69  OR_MAX 100.#endi
1ab5c 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  f..#ifndef SQLIT
1ab5d 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59  E_OMIT_INTEGRITY
1ab5e 5f 43 48 45 43 4b 0a 20 20 69 66 28 20 73 71 6c  _CHECK.  if( sql
1ab5f 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
1ab60 74 2c 20 22 69 6e 74 65 67 72 69 74 79 5f 63 68  t, "integrity_ch
1ab61 65 63 6b 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  eck")==0 ){.    
1ab62 69 6e 74 20 69 2c 20 6a 2c 20 61 64 64 72 2c 20  int i, j, addr, 
1ab63 6d 78 45 72 72 3b 0a 0a 20 20 20 20 2f 2a 20 43  mxErr;..    /* C
1ab64 6f 64 65 20 74 68 61 74 20 61 70 70 65 61 72 73  ode that appears
1ab65 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74   at the end of t
1ab66 68 65 20 69 6e 74 65 67 72 69 74 79 20 63 68 65  he integrity che
1ab67 63 6b 2e 20 20 49 66 20 6e 6f 20 65 72 72 6f 72  ck.  If no error
1ab68 0a 20 20 20 20 2a 2a 20 6d 65 73 73 61 67 65 73  .    ** messages
1ab69 20 68 61 76 65 20 62 65 65 6e 20 67 65 6e 65 72   have been gener
1ab6a 61 74 65 64 2c 20 6f 75 74 70 75 74 20 4f 4b 2e  ated, output OK.
1ab6b 20 20 4f 74 68 65 72 77 69 73 65 20 6f 75 74 70    Otherwise outp
1ab6c 75 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 65 72  ut the.    ** er
1ab6d 72 6f 72 20 6d 65 73 73 61 67 65 0a 20 20 20 20  ror message.    
1ab6e 2a 2f 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f  */.    static co
1ab6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20 65  nst VdbeOpList e
1ab70 6e 64 43 6f 64 65 5b 5d 20 3d 20 7b 0a 20 20 20  ndCode[] = {.   
1ab71 20 20 20 7b 20 4f 50 5f 4d 65 6d 4c 6f 61 64 2c     { OP_MemLoad,
1ab72 20 20 20 20 20 30 2c 20 30 2c 20 20 20 20 20 20       0, 0,      
1ab73 20 20 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50    0},.      { OP
1ab74 5f 49 6e 74 65 67 65 72 2c 20 20 20 20 20 30 2c  _Integer,     0,
1ab75 20 30 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a 20   0,        0},. 
1ab76 20 20 20 20 20 7b 20 4f 50 5f 4e 65 2c 20 20 20       { OP_Ne,   
1ab77 20 20 20 20 20 20 20 30 2c 20 30 2c 20 20 20 20         0, 0,    
1ab78 20 20 20 20 30 7d 2c 20 20 20 20 2f 2a 20 32 20      0},    /* 2 
1ab79 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f 53 74  */.      { OP_St
1ab7a 72 69 6e 67 38 2c 20 20 20 20 20 30 2c 20 30 2c  ring8,     0, 0,
1ab7b 20 20 20 20 20 20 20 20 22 6f 6b 22 7d 2c 0a 20          "ok"},. 
1ab7c 20 20 20 20 20 7b 20 4f 50 5f 43 61 6c 6c 62 61       { OP_Callba
1ab7d 63 6b 2c 20 20 20 20 31 2c 20 30 2c 20 20 20 20  ck,    1, 0,    
1ab7e 20 20 20 20 30 7d 2c 0a 20 20 20 20 7d 3b 0a 0a      0},.    };..
1ab7f 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a      /* Initializ
1ab80 65 20 74 68 65 20 56 44 42 45 20 70 72 6f 67 72  e the VDBE progr
1ab81 61 6d 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 71  am */.    if( sq
1ab82 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28  lite3ReadSchema(
1ab83 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70  pParse) ) goto p
1ab84 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 73  ragma_out;.    s
1ab85 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
1ab86 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20  Cols(v, 1);.    
1ab87 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
1ab88 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
1ab89 41 4d 45 5f 4e 41 4d 45 2c 20 22 69 6e 74 65 67  AME_NAME, "integ
1ab8a 72 69 74 79 5f 63 68 65 63 6b 22 2c 20 50 33 5f  rity_check", P3_
1ab8b 53 54 41 54 49 43 29 3b 0a 0a 20 20 20 20 2f 2a  STATIC);..    /*
1ab8c 20 53 65 74 20 74 68 65 20 6d 61 78 69 6d 75 6d   Set the maximum
1ab8d 20 65 72 72 6f 72 20 63 6f 75 6e 74 20 2a 2f 0a   error count */.
1ab8e 20 20 20 20 6d 78 45 72 72 20 3d 20 53 51 4c 49      mxErr = SQLI
1ab8f 54 45 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45  TE_INTEGRITY_CHE
1ab90 43 4b 5f 45 52 52 4f 52 5f 4d 41 58 3b 0a 20 20  CK_ERROR_MAX;.  
1ab91 20 20 69 66 28 20 7a 52 69 67 68 74 20 29 7b 0a    if( zRight ){.
1ab92 20 20 20 20 20 20 6d 78 45 72 72 20 3d 20 61 74        mxErr = at
1ab93 6f 69 28 7a 52 69 67 68 74 29 3b 0a 20 20 20 20  oi(zRight);.    
1ab94 20 20 69 66 28 20 6d 78 45 72 72 3c 3d 30 20 29    if( mxErr<=0 )
1ab95 7b 0a 20 20 20 20 20 20 20 20 6d 78 45 72 72 20  {.        mxErr 
1ab96 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 52 49  = SQLITE_INTEGRI
1ab97 54 59 5f 43 48 45 43 4b 5f 45 52 52 4f 52 5f 4d  TY_CHECK_ERROR_M
1ab98 41 58 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  AX;.      }.    
1ab99 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  }.    sqlite3Vdb
1ab9a 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d  eAddOp(v, OP_Mem
1ab9b 49 6e 74 2c 20 6d 78 45 72 72 2c 20 30 29 3b 0a  Int, mxErr, 0);.
1ab9c 0a 20 20 20 20 2f 2a 20 44 6f 20 61 6e 20 69 6e  .    /* Do an in
1ab9d 74 65 67 72 69 74 79 20 63 68 65 63 6b 20 6f 6e  tegrity check on
1ab9e 20 65 61 63 68 20 64 61 74 61 62 61 73 65 20 66   each database f
1ab9f 69 6c 65 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69  ile */.    for(i
1aba0 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69  =0; i<db->nDb; i
1aba1 2b 2b 29 7b 0a 20 20 20 20 20 20 48 61 73 68 45  ++){.      HashE
1aba2 6c 65 6d 20 2a 78 3b 0a 20 20 20 20 20 20 48 61  lem *x;.      Ha
1aba3 73 68 20 2a 70 54 62 6c 73 3b 0a 20 20 20 20 20  sh *pTbls;.     
1aba4 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 0a 20   int cnt = 0;.. 
1aba5 20 20 20 20 20 69 66 28 20 4f 4d 49 54 5f 54 45       if( OMIT_TE
1aba6 4d 50 44 42 20 26 26 20 69 3d 3d 31 20 29 20 63  MPDB && i==1 ) c
1aba7 6f 6e 74 69 6e 75 65 3b 0a 0a 20 20 20 20 20 20  ontinue;..      
1aba8 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66  sqlite3CodeVerif
1aba9 79 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20  ySchema(pParse, 
1abaa 69 29 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d  i);.      addr =
1abab 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1abac 70 28 76 2c 20 4f 50 5f 49 66 4d 65 6d 50 6f 73  p(v, OP_IfMemPos
1abad 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 73  , 0, 0);.      s
1abae 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1abaf 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 30 2c 20 30  v, OP_Halt, 0, 0
1abb0 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1abb1 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
1abb2 61 64 64 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  addr);..      /*
1abb3 20 44 6f 20 61 6e 20 69 6e 74 65 67 72 69 74 79   Do an integrity
1abb4 20 63 68 65 63 6b 20 6f 66 20 74 68 65 20 42 2d   check of the B-
1abb5 54 72 65 65 0a 20 20 20 20 20 20 2a 2f 0a 20 20  Tree.      */.  
1abb6 20 20 20 20 70 54 62 6c 73 20 3d 20 26 64 62 2d      pTbls = &db-
1abb7 3e 61 44 62 5b 69 5d 2e 70 53 63 68 65 6d 61 2d  >aDb[i].pSchema-
1abb8 3e 74 62 6c 48 61 73 68 3b 0a 20 20 20 20 20 20  >tblHash;.      
1abb9 66 6f 72 28 78 3d 73 71 6c 69 74 65 48 61 73 68  for(x=sqliteHash
1abba 46 69 72 73 74 28 70 54 62 6c 73 29 3b 20 78 3b  First(pTbls); x;
1abbb 20 78 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78   x=sqliteHashNex
1abbc 74 28 78 29 29 7b 0a 20 20 20 20 20 20 20 20 54  t(x)){.        T
1abbd 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 73 71 6c  able *pTab = sql
1abbe 69 74 65 48 61 73 68 44 61 74 61 28 78 29 3b 0a  iteHashData(x);.
1abbf 20 20 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70          Index *p
1abc0 49 64 78 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  Idx;.        sql
1abc1 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1abc2 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 70 54 61   OP_Integer, pTa
1abc3 62 2d 3e 74 6e 75 6d 2c 20 30 29 3b 0a 20 20 20  b->tnum, 0);.   
1abc4 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20       cnt++;.    
1abc5 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61      for(pIdx=pTa
1abc6 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b  b->pIndex; pIdx;
1abc7 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
1abc8 74 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71  t){.          sq
1abc9 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1abca 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 70 49  , OP_Integer, pI
1abcb 64 78 2d 3e 74 6e 75 6d 2c 20 30 29 3b 0a 20 20  dx->tnum, 0);.  
1abcc 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20          cnt++;. 
1abcd 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
1abce 0a 20 20 20 20 20 20 69 66 28 20 63 6e 74 3d 3d  .      if( cnt==
1abcf 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  0 ) continue;.  
1abd0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1abd1 64 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 74 65 67  ddOp(v, OP_Integ
1abd2 72 69 74 79 43 6b 2c 20 30 2c 20 69 29 3b 0a 20  rityCk, 0, i);. 
1abd3 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69       addr = sqli
1abd4 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1abd5 4f 50 5f 49 73 4e 75 6c 6c 2c 20 2d 31 2c 20 30  OP_IsNull, -1, 0
1abd6 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1abd7 56 64 62 65 4f 70 33 28 76 2c 20 4f 50 5f 53 74  VdbeOp3(v, OP_St
1abd8 72 69 6e 67 38 2c 20 30 2c 20 30 2c 0a 20 20 20  ring8, 0, 0,.   
1abd9 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 50 72        sqlite3MPr
1abda 69 6e 74 66 28 22 2a 2a 2a 20 69 6e 20 64 61 74  intf("*** in dat
1abdb 61 62 61 73 65 20 25 73 20 2a 2a 2a 5c 6e 22 2c  abase %s ***\n",
1abdc 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d   db->aDb[i].zNam
1abdd 65 29 2c 0a 20 20 20 20 20 20 20 20 20 50 33 5f  e),.         P3_
1abde 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20  DYNAMIC);.      
1abdf 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1abe0 28 76 2c 20 4f 50 5f 50 75 6c 6c 2c 20 31 2c 20  (v, OP_Pull, 1, 
1abe1 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  0);.      sqlite
1abe2 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1abe3 5f 43 6f 6e 63 61 74 2c 20 30 2c 20 30 29 3b 0a  _Concat, 0, 0);.
1abe4 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1abe5 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 61 6c  eAddOp(v, OP_Cal
1abe6 6c 62 61 63 6b 2c 20 31 2c 20 30 29 3b 0a 20 20  lback, 1, 0);.  
1abe7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
1abe8 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 29  umpHere(v, addr)
1abe9 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65  ;..      /* Make
1abea 20 73 75 72 65 20 61 6c 6c 20 74 68 65 20 69 6e   sure all the in
1abeb 64 69 63 65 73 20 61 72 65 20 63 6f 6e 73 74 72  dices are constr
1abec 75 63 74 65 64 20 63 6f 72 72 65 63 74 6c 79 2e  ucted correctly.
1abed 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
1abee 66 6f 72 28 78 3d 73 71 6c 69 74 65 48 61 73 68  for(x=sqliteHash
1abef 46 69 72 73 74 28 70 54 62 6c 73 29 3b 20 78 3b  First(pTbls); x;
1abf0 20 78 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78   x=sqliteHashNex
1abf1 74 28 78 29 29 7b 0a 20 20 20 20 20 20 20 20 54  t(x)){.        T
1abf2 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 73 71 6c  able *pTab = sql
1abf3 69 74 65 48 61 73 68 44 61 74 61 28 78 29 3b 0a  iteHashData(x);.
1abf4 20 20 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70          Index *p
1abf5 49 64 78 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  Idx;.        int
1abf6 20 6c 6f 6f 70 54 6f 70 3b 0a 0a 20 20 20 20 20   loopTop;..     
1abf7 20 20 20 69 66 28 20 70 54 61 62 2d 3e 70 49 6e     if( pTab->pIn
1abf8 64 65 78 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75  dex==0 ) continu
1abf9 65 3b 0a 20 20 20 20 20 20 20 20 61 64 64 72 20  e;.        addr 
1abfa 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
1abfb 4f 70 28 76 2c 20 4f 50 5f 49 66 4d 65 6d 50 6f  Op(v, OP_IfMemPo
1abfc 73 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20  s, 0, 0);.      
1abfd 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1abfe 4f 70 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 30  Op(v, OP_Halt, 0
1abff 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71  , 0);.        sq
1ac00 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
1ac01 65 28 76 2c 20 61 64 64 72 29 3b 0a 20 20 20 20  e(v, addr);.    
1ac02 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54      sqlite3OpenT
1ac03 61 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 28 70  ableAndIndices(p
1ac04 50 61 72 73 65 2c 20 70 54 61 62 2c 20 31 2c 20  Parse, pTab, 1, 
1ac05 4f 50 5f 4f 70 65 6e 52 65 61 64 29 3b 0a 20 20  OP_OpenRead);.  
1ac06 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1ac07 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d  eAddOp(v, OP_Mem
1ac08 49 6e 74 2c 20 30 2c 20 31 29 3b 0a 20 20 20 20  Int, 0, 1);.    
1ac09 20 20 20 20 6c 6f 6f 70 54 6f 70 20 3d 20 73 71      loopTop = sq
1ac0a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1ac0b 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 31 2c 20  , OP_Rewind, 1, 
1ac0c 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  0);.        sqli
1ac0d 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1ac0e 4f 50 5f 4d 65 6d 49 6e 63 72 2c 20 31 2c 20 31  OP_MemIncr, 1, 1
1ac0f 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a  );.        for(j
1ac10 3d 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70  =0, pIdx=pTab->p
1ac11 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64  Index; pIdx; pId
1ac12 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 6a  x=pIdx->pNext, j
1ac13 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  ++){.          i
1ac14 6e 74 20 6a 6d 70 32 3b 0a 20 20 20 20 20 20 20  nt jmp2;.       
1ac15 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
1ac16 56 64 62 65 4f 70 4c 69 73 74 20 69 64 78 45 72  VdbeOpList idxEr
1ac17 72 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20  r[] = {.        
1ac18 20 20 20 20 7b 20 4f 50 5f 4d 65 6d 49 6e 63 72      { OP_MemIncr
1ac19 2c 20 20 20 20 2d 31 2c 20 20 30 2c 20 20 30 7d  ,    -1,  0,  0}
1ac1a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  ,.            { 
1ac1b 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20 20 20 20  OP_String8,     
1ac1c 30 2c 20 20 30 2c 20 20 22 72 6f 77 69 64 20 22  0,  0,  "rowid "
1ac1d 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b  },.            {
1ac1e 20 4f 50 5f 52 6f 77 69 64 2c 20 20 20 20 20 20   OP_Rowid,      
1ac1f 20 31 2c 20 20 30 2c 20 20 30 7d 2c 0a 20 20 20   1,  0,  0},.   
1ac20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53 74           { OP_St
1ac21 72 69 6e 67 38 2c 20 20 20 20 20 30 2c 20 20 30  ring8,     0,  0
1ac22 2c 20 20 22 20 6d 69 73 73 69 6e 67 20 66 72 6f  ,  " missing fro
1ac23 6d 20 69 6e 64 65 78 20 22 7d 2c 0a 20 20 20 20  m index "},.    
1ac24 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72          { OP_Str
1ac25 69 6e 67 38 2c 20 20 20 20 20 30 2c 20 20 30 2c  ing8,     0,  0,
1ac26 20 20 30 7d 2c 20 20 20 20 2f 2a 20 34 20 2a 2f    0},    /* 4 */
1ac27 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f  .            { O
1ac28 50 5f 43 6f 6e 63 61 74 2c 20 20 20 20 20 20 32  P_Concat,      2
1ac29 2c 20 20 30 2c 20 20 30 7d 2c 0a 20 20 20 20 20  ,  0,  0},.     
1ac2a 20 20 20 20 20 20 20 7b 20 4f 50 5f 43 61 6c 6c         { OP_Call
1ac2b 62 61 63 6b 2c 20 20 20 20 31 2c 20 20 30 2c 20  back,    1,  0, 
1ac2c 20 30 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 7d   0},.          }
1ac2d 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
1ac2e 74 65 33 47 65 6e 65 72 61 74 65 49 6e 64 65 78  te3GenerateIndex
1ac2f 4b 65 79 28 76 2c 20 70 49 64 78 2c 20 31 29 3b  Key(v, pIdx, 1);
1ac30 0a 20 20 20 20 20 20 20 20 20 20 6a 6d 70 32 20  .          jmp2 
1ac31 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
1ac32 4f 70 28 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20  Op(v, OP_Found, 
1ac33 6a 2b 32 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  j+2, 0);.       
1ac34 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
1ac35 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76  3VdbeAddOpList(v
1ac36 2c 20 41 72 72 61 79 53 69 7a 65 28 69 64 78 45  , ArraySize(idxE
1ac37 72 72 29 2c 20 69 64 78 45 72 72 29 3b 0a 20 20  rr), idxErr);.  
1ac38 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1ac39 64 62 65 43 68 61 6e 67 65 50 33 28 76 2c 20 61  dbeChangeP3(v, a
1ac3a 64 64 72 2b 34 2c 20 70 49 64 78 2d 3e 7a 4e 61  ddr+4, pIdx->zNa
1ac3b 6d 65 2c 20 50 33 5f 53 54 41 54 49 43 29 3b 0a  me, P3_STATIC);.
1ac3c 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1ac3d 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
1ac3e 20 6a 6d 70 32 29 3b 0a 20 20 20 20 20 20 20 20   jmp2);.        
1ac3f 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  }.        sqlite
1ac40 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1ac41 5f 4e 65 78 74 2c 20 31 2c 20 6c 6f 6f 70 54 6f  _Next, 1, loopTo
1ac42 70 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71  p+1);.        sq
1ac43 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
1ac44 65 28 76 2c 20 6c 6f 6f 70 54 6f 70 29 3b 0a 20  e(v, loopTop);. 
1ac45 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20         for(j=0, 
1ac46 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65  pIdx=pTab->pInde
1ac47 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49  x; pIdx; pIdx=pI
1ac48 64 78 2d 3e 70 4e 65 78 74 2c 20 6a 2b 2b 29 7b  dx->pNext, j++){
1ac49 0a 20 20 20 20 20 20 20 20 20 20 73 74 61 74 69  .          stati
1ac4a 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69  c const VdbeOpLi
1ac4b 73 74 20 63 6e 74 49 64 78 5b 5d 20 3d 20 7b 0a  st cntIdx[] = {.
1ac4c 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f               { O
1ac4d 50 5f 4d 65 6d 49 6e 74 2c 20 20 20 20 20 20 20  P_MemInt,       
1ac4e 30 2c 20 20 32 2c 20 20 30 7d 2c 0a 20 20 20 20  0,  2,  0},.    
1ac4f 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 52 65           { OP_Re
1ac50 77 69 6e 64 2c 20 20 20 20 20 20 20 30 2c 20 20  wind,       0,  
1ac51 30 2c 20 20 30 7d 2c 20 20 2f 2a 20 31 20 2a 2f  0,  0},  /* 1 */
1ac52 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  .             { 
1ac53 4f 50 5f 4d 65 6d 49 6e 63 72 2c 20 20 20 20 20  OP_MemIncr,     
1ac54 20 31 2c 20 20 32 2c 20 20 30 7d 2c 0a 20 20 20   1,  2,  0},.   
1ac55 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 4e            { OP_N
1ac56 65 78 74 2c 20 20 20 20 20 20 20 20 20 30 2c 20  ext,         0, 
1ac57 20 30 2c 20 20 30 7d 2c 20 20 2f 2a 20 33 20 2a   0,  0},  /* 3 *
1ac58 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 7b  /.             {
1ac59 20 4f 50 5f 4d 65 6d 4c 6f 61 64 2c 20 20 20 20   OP_MemLoad,    
1ac5a 20 20 31 2c 20 20 30 2c 20 20 30 7d 2c 0a 20 20    1,  0,  0},.  
1ac5b 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
1ac5c 4d 65 6d 4c 6f 61 64 2c 20 20 20 20 20 20 32 2c  MemLoad,      2,
1ac5d 20 20 30 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20    0,  0},.      
1ac5e 20 20 20 20 20 20 20 7b 20 4f 50 5f 45 71 2c 20         { OP_Eq, 
1ac5f 20 20 20 20 20 20 20 20 20 20 30 2c 20 20 30 2c            0,  0,
1ac60 20 20 30 7d 2c 20 20 2f 2a 20 36 20 2a 2f 0a 20    0},  /* 6 */. 
1ac61 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
1ac62 5f 4d 65 6d 49 6e 63 72 2c 20 20 20 20 20 2d 31  _MemIncr,     -1
1ac63 2c 20 20 30 2c 20 20 30 7d 2c 0a 20 20 20 20 20  ,  0,  0},.     
1ac64 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72          { OP_Str
1ac65 69 6e 67 38 2c 20 20 20 20 20 20 30 2c 20 20 30  ing8,      0,  0
1ac66 2c 20 20 22 77 72 6f 6e 67 20 23 20 6f 66 20 65  ,  "wrong # of e
1ac67 6e 74 72 69 65 73 20 69 6e 20 69 6e 64 65 78 20  ntries in index 
1ac68 22 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  "},.            
1ac69 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20   { OP_String8,  
1ac6a 20 20 20 20 30 2c 20 20 30 2c 20 20 30 7d 2c 20      0,  0,  0}, 
1ac6b 20 2f 2a 20 39 20 2a 2f 0a 20 20 20 20 20 20 20   /* 9 */.       
1ac6c 20 20 20 20 20 20 7b 20 4f 50 5f 43 6f 6e 63 61        { OP_Conca
1ac6d 74 2c 20 20 20 20 20 20 20 30 2c 20 20 30 2c 20  t,       0,  0, 
1ac6e 20 30 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20   0},.           
1ac6f 20 20 7b 20 4f 50 5f 43 61 6c 6c 62 61 63 6b 2c    { OP_Callback,
1ac70 20 20 20 20 20 31 2c 20 20 30 2c 20 20 30 7d 2c       1,  0,  0},
1ac71 0a 20 20 20 20 20 20 20 20 20 20 7d 3b 0a 20 20  .          };.  
1ac72 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64 78          if( pIdx
1ac73 2d 3e 74 6e 75 6d 3d 3d 30 20 29 20 63 6f 6e 74  ->tnum==0 ) cont
1ac74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20  inue;.          
1ac75 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
1ac76 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 66  beAddOp(v, OP_If
1ac77 4d 65 6d 50 6f 73 2c 20 30 2c 20 30 29 3b 0a 20  MemPos, 0, 0);. 
1ac78 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1ac79 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1ac7a 48 61 6c 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20  Halt, 0, 0);.   
1ac7b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1ac7c 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
1ac7d 64 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61  dr);.          a
1ac7e 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
1ac7f 65 41 64 64 4f 70 4c 69 73 74 28 76 2c 20 41 72  eAddOpList(v, Ar
1ac80 72 61 79 53 69 7a 65 28 63 6e 74 49 64 78 29 2c  raySize(cntIdx),
1ac81 20 63 6e 74 49 64 78 29 3b 0a 20 20 20 20 20 20   cntIdx);.      
1ac82 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
1ac83 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2b  hangeP1(v, addr+
1ac84 31 2c 20 6a 2b 32 29 3b 0a 20 20 20 20 20 20 20  1, j+2);.       
1ac85 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
1ac86 61 6e 67 65 50 32 28 76 2c 20 61 64 64 72 2b 31  angeP2(v, addr+1
1ac87 2c 20 61 64 64 72 2b 34 29 3b 0a 20 20 20 20 20  , addr+4);.     
1ac88 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1ac89 43 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72  ChangeP1(v, addr
1ac8a 2b 33 2c 20 6a 2b 32 29 3b 0a 20 20 20 20 20 20  +3, j+2);.      
1ac8b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
1ac8c 68 61 6e 67 65 50 32 28 76 2c 20 61 64 64 72 2b  hangeP2(v, addr+
1ac8d 33 2c 20 61 64 64 72 2b 32 29 3b 0a 20 20 20 20  3, addr+2);.    
1ac8e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1ac8f 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
1ac90 72 2b 36 29 3b 0a 20 20 20 20 20 20 20 20 20 20  r+6);.          
1ac91 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1ac92 65 50 33 28 76 2c 20 61 64 64 72 2b 39 2c 20 70  eP3(v, addr+9, p
1ac93 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 50 33 5f 53  Idx->zName, P3_S
1ac94 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20  TATIC);.        
1ac95 7d 0a 20 20 20 20 20 20 7d 20 0a 20 20 20 20 7d  }.      } .    }
1ac96 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69  .    addr = sqli
1ac97 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74  te3VdbeAddOpList
1ac98 28 76 2c 20 41 72 72 61 79 53 69 7a 65 28 65 6e  (v, ArraySize(en
1ac99 64 43 6f 64 65 29 2c 20 65 6e 64 43 6f 64 65 29  dCode), endCode)
1ac9a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
1ac9b 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64  eChangeP1(v, add
1ac9c 72 2b 31 2c 20 6d 78 45 72 72 29 3b 0a 20 20 20  r+1, mxErr);.   
1ac9d 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
1ac9e 48 65 72 65 28 76 2c 20 61 64 64 72 2b 32 29 3b  Here(v, addr+2);
1ac9f 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20  .  }else.#endif 
1aca0 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49  /* SQLITE_OMIT_I
1aca1 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 20 2a  NTEGRITY_CHECK *
1aca2 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
1aca3 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20 2f  E_OMIT_UTF16.  /
1aca4 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20  *.  **   PRAGMA 
1aca5 65 6e 63 6f 64 69 6e 67 0a 20 20 2a 2a 20 20 20  encoding.  **   
1aca6 50 52 41 47 4d 41 20 65 6e 63 6f 64 69 6e 67 20  PRAGMA encoding 
1aca7 3d 20 22 75 74 66 2d 38 22 7c 22 75 74 66 2d 31  = "utf-8"|"utf-1
1aca8 36 22 7c 22 75 74 66 2d 31 36 6c 65 22 7c 22 75  6"|"utf-16le"|"u
1aca9 74 66 2d 31 36 62 65 22 0a 20 20 2a 2a 0a 20 20  tf-16be".  **.  
1acaa 2a 2a 20 49 6e 20 69 74 27 73 20 66 69 72 73 74  ** In it's first
1acab 20 66 6f 72 6d 2c 20 74 68 69 73 20 70 72 61 67   form, this prag
1acac 6d 61 20 72 65 74 75 72 6e 73 20 74 68 65 20 65  ma returns the e
1acad 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 6d  ncoding of the m
1acae 61 69 6e 0a 20 20 2a 2a 20 64 61 74 61 62 61 73  ain.  ** databas
1acaf 65 2e 20 49 66 20 74 68 65 20 64 61 74 61 62 61  e. If the databa
1acb0 73 65 20 69 73 20 6e 6f 74 20 69 6e 69 74 69 61  se is not initia
1acb1 6c 69 7a 65 64 2c 20 69 74 20 69 73 20 69 6e 69  lized, it is ini
1acb2 74 69 61 6c 69 7a 65 64 20 6e 6f 77 2e 0a 20 20  tialized now..  
1acb3 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 65 63 6f  **.  ** The seco
1acb4 6e 64 20 66 6f 72 6d 20 6f 66 20 74 68 69 73 20  nd form of this 
1acb5 70 72 61 67 6d 61 20 69 73 20 61 20 6e 6f 2d 6f  pragma is a no-o
1acb6 70 20 69 66 20 74 68 65 20 6d 61 69 6e 20 64 61  p if the main da
1acb7 74 61 62 61 73 65 20 66 69 6c 65 0a 20 20 2a 2a  tabase file.  **
1acb8 20 68 61 73 20 6e 6f 74 20 61 6c 72 65 61 64 79   has not already
1acb9 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65   been initialize
1acba 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  d. In this case 
1acbb 69 74 20 73 65 74 73 20 74 68 65 20 64 65 66 61  it sets the defa
1acbc 75 6c 74 0a 20 20 2a 2a 20 65 6e 63 6f 64 69 6e  ult.  ** encodin
1acbd 67 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 75  g that will be u
1acbe 73 65 64 20 66 6f 72 20 74 68 65 20 6d 61 69 6e  sed for the main
1acbf 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
1acc0 66 20 61 20 6e 65 77 20 66 69 6c 65 0a 20 20 2a  f a new file.  *
1acc1 2a 20 69 73 20 63 72 65 61 74 65 64 2e 20 49 66  * is created. If
1acc2 20 61 6e 20 65 78 69 73 74 69 6e 67 20 6d 61 69   an existing mai
1acc3 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  n database file 
1acc4 69 73 20 6f 70 65 6e 65 64 2c 20 74 68 65 6e 20  is opened, then 
1acc5 74 68 65 0a 20 20 2a 2a 20 64 65 66 61 75 6c 74  the.  ** default
1acc6 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 66   text encoding f
1acc7 6f 72 20 74 68 65 20 65 78 69 73 74 69 6e 67 20  or the existing 
1acc8 64 61 74 61 62 61 73 65 20 69 73 20 75 73 65 64  database is used
1acc9 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 49 6e 20  ..  ** .  ** In 
1acca 61 6c 6c 20 63 61 73 65 73 20 6e 65 77 20 64 61  all cases new da
1accb 74 61 62 61 73 65 73 20 63 72 65 61 74 65 64 20  tabases created 
1accc 75 73 69 6e 67 20 74 68 65 20 41 54 54 41 43 48  using the ATTACH
1accd 20 63 6f 6d 6d 61 6e 64 20 61 72 65 0a 20 20 2a   command are.  *
1acce 2a 20 63 72 65 61 74 65 64 20 74 6f 20 75 73 65  * created to use
1accf 20 74 68 65 20 73 61 6d 65 20 64 65 66 61 75 6c   the same defaul
1acd0 74 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20  t text encoding 
1acd1 61 73 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  as the main data
1acd2 62 61 73 65 2e 20 49 66 0a 20 20 2a 2a 20 74 68  base. If.  ** th
1acd3 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
1acd4 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 69 6e 69  has not been ini
1acd5 74 69 61 6c 69 7a 65 64 20 61 6e 64 2f 6f 72 20  tialized and/or 
1acd6 63 72 65 61 74 65 64 20 77 68 65 6e 20 41 54 54  created when ATT
1acd7 41 43 48 0a 20 20 2a 2a 20 69 73 20 65 78 65 63  ACH.  ** is exec
1acd8 75 74 65 64 2c 20 74 68 69 73 20 69 73 20 64 6f  uted, this is do
1acd9 6e 65 20 62 65 66 6f 72 65 20 74 68 65 20 41 54  ne before the AT
1acda 54 41 43 48 20 6f 70 65 72 61 74 69 6f 6e 2e 0a  TACH operation..
1acdb 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 74 68 65    **.  ** In the
1acdc 20 73 65 63 6f 6e 64 20 66 6f 72 6d 20 74 68 69   second form thi
1acdd 73 20 70 72 61 67 6d 61 20 73 65 74 73 20 74 68  s pragma sets th
1acde 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20  e text encoding 
1acdf 74 6f 20 62 65 20 75 73 65 64 20 69 6e 0a 20 20  to be used in.  
1ace0 2a 2a 20 6e 65 77 20 64 61 74 61 62 61 73 65 20  ** new database 
1ace1 66 69 6c 65 73 20 63 72 65 61 74 65 64 20 75 73  files created us
1ace2 69 6e 67 20 74 68 69 73 20 64 61 74 61 62 61 73  ing this databas
1ace3 65 20 68 61 6e 64 6c 65 2e 20 49 74 20 69 73 20  e handle. It is 
1ace4 6f 6e 6c 79 0a 20 20 2a 2a 20 75 73 65 66 75 6c  only.  ** useful
1ace5 20 69 66 20 69 6e 76 6f 6b 65 64 20 69 6d 6d 65   if invoked imme
1ace6 64 69 61 74 65 6c 79 20 61 66 74 65 72 20 74 68  diately after th
1ace7 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
1ace8 69 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  i.  */.  if( sql
1ace9 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
1acea 74 2c 20 22 65 6e 63 6f 64 69 6e 67 22 29 3d 3d  t, "encoding")==
1aceb 30 20 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20  0 ){.    static 
1acec 63 6f 6e 73 74 20 73 74 72 75 63 74 20 45 6e 63  const struct Enc
1aced 4e 61 6d 65 20 7b 0a 20 20 20 20 20 20 63 68 61  Name {.      cha
1acee 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20  r *zName;.      
1acef 75 38 20 65 6e 63 3b 0a 20 20 20 20 7d 20 65 6e  u8 enc;.    } en
1acf0 63 6e 61 6d 65 73 5b 5d 20 3d 20 7b 0a 20 20 20  cnames[] = {.   
1acf1 20 20 20 7b 20 22 55 54 46 2d 38 22 2c 20 20 20     { "UTF-8",   
1acf2 20 53 51 4c 49 54 45 5f 55 54 46 38 20 20 20 20   SQLITE_UTF8    
1acf3 20 20 20 20 7d 2c 0a 20 20 20 20 20 20 7b 20 22      },.      { "
1acf4 55 54 46 38 22 2c 20 20 20 20 20 53 51 4c 49 54  UTF8",     SQLIT
1acf5 45 5f 55 54 46 38 20 20 20 20 20 20 20 20 7d 2c  E_UTF8        },
1acf6 0a 20 20 20 20 20 20 7b 20 22 55 54 46 2d 31 36  .      { "UTF-16
1acf7 6c 65 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  le", SQLITE_UTF1
1acf8 36 4c 45 20 20 20 20 20 7d 2c 0a 20 20 20 20 20  6LE     },.     
1acf9 20 7b 20 22 55 54 46 31 36 6c 65 22 2c 20 20 53   { "UTF16le",  S
1acfa 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 20 20  QLITE_UTF16LE   
1acfb 20 20 7d 2c 0a 20 20 20 20 20 20 7b 20 22 55 54    },.      { "UT
1acfc 46 2d 31 36 62 65 22 2c 20 53 51 4c 49 54 45 5f  F-16be", SQLITE_
1acfd 55 54 46 31 36 42 45 20 20 20 20 20 7d 2c 0a 20  UTF16BE     },. 
1acfe 20 20 20 20 20 7b 20 22 55 54 46 31 36 62 65 22       { "UTF16be"
1acff 2c 20 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42  ,  SQLITE_UTF16B
1ad00 45 20 20 20 20 20 7d 2c 0a 20 20 20 20 20 20 7b  E     },.      {
1ad01 20 22 55 54 46 2d 31 36 22 2c 20 20 20 30 20 20   "UTF-16",   0  
1ad02 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ad03 7d 2c 20 2f 2a 20 53 51 4c 49 54 45 5f 55 54 46  }, /* SQLITE_UTF
1ad04 31 36 4e 41 54 49 56 45 20 2a 2f 0a 20 20 20 20  16NATIVE */.    
1ad05 20 20 7b 20 22 55 54 46 31 36 22 2c 20 20 20 20    { "UTF16",    
1ad06 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0               
1ad07 20 20 20 7d 2c 20 2f 2a 20 53 51 4c 49 54 45 5f     }, /* SQLITE_
1ad08 55 54 46 31 36 4e 41 54 49 56 45 20 2a 2f 0a 20  UTF16NATIVE */. 
1ad09 20 20 20 20 20 7b 20 30 2c 20 30 20 7d 0a 20 20       { 0, 0 }.  
1ad0a 20 20 7d 3b 0a 20 20 20 20 63 6f 6e 73 74 20 73    };.    const s
1ad0b 74 72 75 63 74 20 45 6e 63 4e 61 6d 65 20 2a 70  truct EncName *p
1ad0c 45 6e 63 3b 0a 20 20 20 20 69 66 28 20 21 7a 52  Enc;.    if( !zR
1ad0d 69 67 68 74 20 29 7b 20 20 20 20 2f 2a 20 22 50  ight ){    /* "P
1ad0e 52 41 47 4d 41 20 65 6e 63 6f 64 69 6e 67 22 20  RAGMA encoding" 
1ad0f 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  */.      if( sql
1ad10 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70  ite3ReadSchema(p
1ad11 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72  Parse) ) goto pr
1ad12 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 20 20  agma_out;.      
1ad13 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75  sqlite3VdbeSetNu
1ad14 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20  mCols(v, 1);.   
1ad15 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
1ad16 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43  tColName(v, 0, C
1ad17 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 65 6e  OLNAME_NAME, "en
1ad18 63 6f 64 69 6e 67 22 2c 20 50 33 5f 53 54 41 54  coding", P3_STAT
1ad19 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  IC);.      sqlit
1ad1a 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1ad1b 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 30 29  P_String8, 0, 0)
1ad1c 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 45 6e 63  ;.      for(pEnc
1ad1d 3d 26 65 6e 63 6e 61 6d 65 73 5b 30 5d 3b 20 70  =&encnames[0]; p
1ad1e 45 6e 63 2d 3e 7a 4e 61 6d 65 3b 20 70 45 6e 63  Enc->zName; pEnc
1ad1f 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ++){.        if(
1ad20 20 70 45 6e 63 2d 3e 65 6e 63 3d 3d 45 4e 43 28   pEnc->enc==ENC(
1ad21 70 50 61 72 73 65 2d 3e 64 62 29 20 29 7b 0a 20  pParse->db) ){. 
1ad22 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1ad23 56 64 62 65 43 68 61 6e 67 65 50 33 28 76 2c 20  VdbeChangeP3(v, 
1ad24 2d 31 2c 20 70 45 6e 63 2d 3e 7a 4e 61 6d 65 2c  -1, pEnc->zName,
1ad25 20 50 33 5f 53 54 41 54 49 43 29 3b 0a 20 20 20   P3_STATIC);.   
1ad26 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1ad27 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1ad28 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1ad29 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 61 6c  eAddOp(v, OP_Cal
1ad2a 6c 62 61 63 6b 2c 20 31 2c 20 30 29 3b 0a 20 20  lback, 1, 0);.  
1ad2b 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20 20    }else{        
1ad2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ad2d 2f 2a 20 22 50 52 41 47 4d 41 20 65 6e 63 6f 64  /* "PRAGMA encod
1ad2e 69 6e 67 20 3d 20 58 58 58 22 20 2a 2f 0a 20 20  ing = XXX" */.  
1ad2f 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 63 68 61 6e      /* Only chan
1ad30 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ge the value of 
1ad31 73 71 6c 69 74 65 2e 65 6e 63 20 69 66 20 74 68  sqlite.enc if th
1ad32 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  e database handl
1ad33 65 20 69 73 20 6e 6f 74 0a 20 20 20 20 20 20 2a  e is not.      *
1ad34 2a 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 49  * initialized. I
1ad35 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  f the main datab
1ad36 61 73 65 20 65 78 69 73 74 73 2c 20 74 68 65 20  ase exists, the 
1ad37 6e 65 77 20 73 71 6c 69 74 65 2e 65 6e 63 20 76  new sqlite.enc v
1ad38 61 6c 75 65 0a 20 20 20 20 20 20 2a 2a 20 77 69  alue.      ** wi
1ad39 6c 6c 20 62 65 20 6f 76 65 72 77 72 69 74 74 65  ll be overwritte
1ad3a 6e 20 77 68 65 6e 20 74 68 65 20 73 63 68 65 6d  n when the schem
1ad3b 61 20 69 73 20 6e 65 78 74 20 6c 6f 61 64 65 64  a is next loaded
1ad3c 2e 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74  . If it does not
1ad3d 0a 20 20 20 20 20 20 2a 2a 20 61 6c 72 65 61 64  .      ** alread
1ad3e 79 20 65 78 69 73 74 73 2c 20 69 74 20 77 69 6c  y exists, it wil
1ad3f 6c 20 62 65 20 63 72 65 61 74 65 64 20 74 6f 20  l be created to 
1ad40 75 73 65 20 74 68 65 20 6e 65 77 20 65 6e 63 6f  use the new enco
1ad41 64 69 6e 67 20 76 61 6c 75 65 2e 0a 20 20 20 20  ding value..    
1ad42 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 0a    */.      if( .
1ad43 20 20 20 20 20 20 20 20 21 28 44 62 48 61 73 50          !(DbHasP
1ad44 72 6f 70 65 72 74 79 28 64 62 2c 20 30 2c 20 44  roperty(db, 0, D
1ad45 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 29  B_SchemaLoaded))
1ad46 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 44 62 48   || .        DbH
1ad47 61 73 50 72 6f 70 65 72 74 79 28 64 62 2c 20 30  asProperty(db, 0
1ad48 2c 20 44 42 5f 45 6d 70 74 79 29 20 0a 20 20 20  , DB_Empty) .   
1ad49 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6f     ){.        fo
1ad4a 72 28 70 45 6e 63 3d 26 65 6e 63 6e 61 6d 65 73  r(pEnc=&encnames
1ad4b 5b 30 5d 3b 20 70 45 6e 63 2d 3e 7a 4e 61 6d 65  [0]; pEnc->zName
1ad4c 3b 20 70 45 6e 63 2b 2b 29 7b 0a 20 20 20 20 20  ; pEnc++){.     
1ad4d 20 20 20 20 20 69 66 28 20 30 3d 3d 73 71 6c 69       if( 0==sqli
1ad4e 74 65 33 53 74 72 49 43 6d 70 28 7a 52 69 67 68  te3StrICmp(zRigh
1ad4f 74 2c 20 70 45 6e 63 2d 3e 7a 4e 61 6d 65 29 20  t, pEnc->zName) 
1ad50 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 45  ){.            E
1ad51 4e 43 28 70 50 61 72 73 65 2d 3e 64 62 29 20 3d  NC(pParse->db) =
1ad52 20 70 45 6e 63 2d 3e 65 6e 63 20 3f 20 70 45 6e   pEnc->enc ? pEn
1ad53 63 2d 3e 65 6e 63 20 3a 20 53 51 4c 49 54 45 5f  c->enc : SQLITE_
1ad54 55 54 46 31 36 4e 41 54 49 56 45 3b 0a 20 20 20  UTF16NATIVE;.   
1ad55 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
1ad56 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
1ad57 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
1ad58 28 20 21 70 45 6e 63 2d 3e 7a 4e 61 6d 65 20 29  ( !pEnc->zName )
1ad59 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
1ad5a 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1ad5b 73 65 2c 20 22 75 6e 73 75 70 70 6f 72 74 65 64  se, "unsupported
1ad5c 20 65 6e 63 6f 64 69 6e 67 3a 20 25 73 22 2c 20   encoding: %s", 
1ad5d 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20 20  zRight);.       
1ad5e 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
1ad5f 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20  .  }else.#endif 
1ad60 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  /* SQLITE_OMIT_U
1ad61 54 46 31 36 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  TF16 */..#ifndef
1ad62 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 43 48   SQLITE_OMIT_SCH
1ad63 45 4d 41 5f 56 45 52 53 49 4f 4e 5f 50 52 41 47  EMA_VERSION_PRAG
1ad64 4d 41 53 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 20  MAS.  /*.  **   
1ad65 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65  PRAGMA [database
1ad66 2e 5d 73 63 68 65 6d 61 5f 76 65 72 73 69 6f 6e  .]schema_version
1ad67 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b  .  **   PRAGMA [
1ad68 64 61 74 61 62 61 73 65 2e 5d 73 63 68 65 6d 61  database.]schema
1ad69 5f 76 65 72 73 69 6f 6e 20 3d 20 3c 69 6e 74 65  _version = <inte
1ad6a 67 65 72 3e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20  ger>.  **.  **  
1ad6b 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
1ad6c 65 2e 5d 75 73 65 72 5f 76 65 72 73 69 6f 6e 0a  e.]user_version.
1ad6d 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b 64    **   PRAGMA [d
1ad6e 61 74 61 62 61 73 65 2e 5d 75 73 65 72 5f 76 65  atabase.]user_ve
1ad6f 72 73 69 6f 6e 20 3d 20 3c 69 6e 74 65 67 65 72  rsion = <integer
1ad70 3e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  >.  **.  ** The 
1ad71 70 72 61 67 6d 61 27 73 20 73 63 68 65 6d 61 5f  pragma's schema_
1ad72 76 65 72 73 69 6f 6e 20 61 6e 64 20 75 73 65 72  version and user
1ad73 5f 76 65 72 73 69 6f 6e 20 61 72 65 20 75 73 65  _version are use
1ad74 64 20 74 6f 20 73 65 74 20 6f 72 20 67 65 74 0a  d to set or get.
1ad75 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f    ** the value o
1ad76 66 20 74 68 65 20 73 63 68 65 6d 61 2d 76 65 72  f the schema-ver
1ad77 73 69 6f 6e 20 61 6e 64 20 75 73 65 72 2d 76 65  sion and user-ve
1ad78 72 73 69 6f 6e 2c 20 72 65 73 70 65 63 74 69 76  rsion, respectiv
1ad79 65 6c 79 2e 20 42 6f 74 68 0a 20 20 2a 2a 20 74  ely. Both.  ** t
1ad7a 68 65 20 73 63 68 65 6d 61 2d 76 65 72 73 69 6f  he schema-versio
1ad7b 6e 20 61 6e 64 20 74 68 65 20 75 73 65 72 2d 76  n and the user-v
1ad7c 65 72 73 69 6f 6e 20 61 72 65 20 33 32 2d 62 69  ersion are 32-bi
1ad7d 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  t signed integer
1ad7e 73 0a 20 20 2a 2a 20 73 74 6f 72 65 64 20 69 6e  s.  ** stored in
1ad7f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 65   the database he
1ad80 61 64 65 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ader..  **.  ** 
1ad81 54 68 65 20 73 63 68 65 6d 61 2d 63 6f 6f 6b 69  The schema-cooki
1ad82 65 20 69 73 20 75 73 75 61 6c 6c 79 20 6f 6e 6c  e is usually onl
1ad83 79 20 6d 61 6e 69 70 75 6c 61 74 65 64 20 69 6e  y manipulated in
1ad84 74 65 72 6e 61 6c 6c 79 20 62 79 20 53 51 4c 69  ternally by SQLi
1ad85 74 65 2e 20 49 74 0a 20 20 2a 2a 20 69 73 20 69  te. It.  ** is i
1ad86 6e 63 72 65 6d 65 6e 74 65 64 20 62 79 20 53 51  ncremented by SQ
1ad87 4c 69 74 65 20 77 68 65 6e 65 76 65 72 20 74 68  Lite whenever th
1ad88 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  e database schem
1ad89 61 20 69 73 20 6d 6f 64 69 66 69 65 64 20 28 62  a is modified (b
1ad8a 79 0a 20 20 2a 2a 20 63 72 65 61 74 69 6e 67 20  y.  ** creating 
1ad8b 6f 72 20 64 72 6f 70 70 69 6e 67 20 61 20 74 61  or dropping a ta
1ad8c 62 6c 65 20 6f 72 20 69 6e 64 65 78 29 2e 20 54  ble or index). T
1ad8d 68 65 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f  he schema versio
1ad8e 6e 20 69 73 20 75 73 65 64 20 62 79 0a 20 20 2a  n is used by.  *
1ad8f 2a 20 53 51 4c 69 74 65 20 65 61 63 68 20 74 69  * SQLite each ti
1ad90 6d 65 20 61 20 71 75 65 72 79 20 69 73 20 65 78  me a query is ex
1ad91 65 63 75 74 65 64 20 74 6f 20 65 6e 73 75 72 65  ecuted to ensure
1ad92 20 74 68 61 74 20 74 68 65 20 69 6e 74 65 72 6e   that the intern
1ad93 61 6c 20 63 61 63 68 65 0a 20 20 2a 2a 20 6f 66  al cache.  ** of
1ad94 20 74 68 65 20 73 63 68 65 6d 61 20 75 73 65 64   the schema used
1ad95 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20   when compiling 
1ad96 74 68 65 20 53 51 4c 20 71 75 65 72 79 20 6d 61  the SQL query ma
1ad97 74 63 68 65 73 20 74 68 65 20 73 63 68 65 6d 61  tches the schema
1ad98 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 64 61 74   of.  ** the dat
1ad99 61 62 61 73 65 20 61 67 61 69 6e 73 74 20 77 68  abase against wh
1ad9a 69 63 68 20 74 68 65 20 63 6f 6d 70 69 6c 65 64  ich the compiled
1ad9b 20 71 75 65 72 79 20 69 73 20 61 63 74 75 61 6c   query is actual
1ad9c 6c 79 20 65 78 65 63 75 74 65 64 2e 0a 20 20 2a  ly executed..  *
1ad9d 2a 20 53 75 62 76 65 72 74 69 6e 67 20 74 68 69  * Subverting thi
1ad9e 73 20 6d 65 63 68 61 6e 69 73 6d 20 62 79 20 75  s mechanism by u
1ad9f 73 69 6e 67 20 22 50 52 41 47 4d 41 20 73 63 68  sing "PRAGMA sch
1ada0 65 6d 61 5f 76 65 72 73 69 6f 6e 22 20 74 6f 20  ema_version" to 
1ada1 6d 6f 64 69 66 79 0a 20 20 2a 2a 20 74 68 65 20  modify.  ** the 
1ada2 73 63 68 65 6d 61 2d 76 65 72 73 69 6f 6e 20 69  schema-version i
1ada3 73 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20 64 61  s potentially da
1ada4 6e 67 65 72 6f 75 73 20 61 6e 64 20 6d 61 79 20  ngerous and may 
1ada5 6c 65 61 64 20 74 6f 20 70 72 6f 67 72 61 6d 0a  lead to program.
1ada6 20 20 2a 2a 20 63 72 61 73 68 65 73 20 6f 72 20    ** crashes or 
1ada7 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74  database corrupt
1ada8 69 6f 6e 2e 20 55 73 65 20 77 69 74 68 20 63 61  ion. Use with ca
1ada9 75 74 69 6f 6e 21 0a 20 20 2a 2a 0a 20 20 2a 2a  ution!.  **.  **
1adaa 20 54 68 65 20 75 73 65 72 2d 76 65 72 73 69 6f   The user-versio
1adab 6e 20 69 73 20 6e 6f 74 20 75 73 65 64 20 69 6e  n is not used in
1adac 74 65 72 6e 61 6c 6c 79 20 62 79 20 53 51 4c 69  ternally by SQLi
1adad 74 65 2e 20 49 74 20 6d 61 79 20 62 65 20 75 73  te. It may be us
1adae 65 64 20 62 79 0a 20 20 2a 2a 20 61 70 70 6c 69  ed by.  ** appli
1adaf 63 61 74 69 6f 6e 73 20 66 6f 72 20 61 6e 79 20  cations for any 
1adb0 70 75 72 70 6f 73 65 2e 0a 20 20 2a 2f 0a 20 20  purpose..  */.  
1adb1 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
1adb2 6d 70 28 7a 4c 65 66 74 2c 20 22 73 63 68 65 6d  mp(zLeft, "schem
1adb3 61 5f 76 65 72 73 69 6f 6e 22 29 3d 3d 30 20 0a  a_version")==0 .
1adb4 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 53 74 72     || sqlite3Str
1adb5 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 75 73 65  ICmp(zLeft, "use
1adb6 72 5f 76 65 72 73 69 6f 6e 22 29 3d 3d 30 20 0a  r_version")==0 .
1adb7 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 53 74 72     || sqlite3Str
1adb8 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 66 72 65  ICmp(zLeft, "fre
1adb9 65 6c 69 73 74 5f 63 6f 75 6e 74 22 29 3d 3d 30  elist_count")==0
1adba 20 0a 20 20 29 7b 0a 0a 20 20 20 20 69 6e 74 20   .  ){..    int 
1adbb 69 43 6f 6f 6b 69 65 3b 20 20 20 2f 2a 20 43 6f  iCookie;   /* Co
1adbc 6f 6b 69 65 20 69 6e 64 65 78 2e 20 30 20 66 6f  okie index. 0 fo
1adbd 72 20 73 63 68 65 6d 61 2d 63 6f 6f 6b 69 65 2c  r schema-cookie,
1adbe 20 36 20 66 6f 72 20 75 73 65 72 2d 63 6f 6f 6b   6 for user-cook
1adbf 69 65 2e 20 2a 2f 0a 20 20 20 20 73 77 69 74 63  ie. */.    switc
1adc0 68 28 20 7a 4c 65 66 74 5b 30 5d 20 29 7b 0a 20  h( zLeft[0] ){. 
1adc1 20 20 20 20 20 63 61 73 65 20 27 73 27 3a 20 63       case 's': c
1adc2 61 73 65 20 27 53 27 3a 0a 20 20 20 20 20 20 20  ase 'S':.       
1adc3 20 69 43 6f 6f 6b 69 65 20 3d 20 30 3b 0a 20 20   iCookie = 0;.  
1adc4 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1adc5 20 20 20 63 61 73 65 20 27 66 27 3a 20 63 61 73     case 'f': cas
1adc6 65 20 27 46 27 3a 0a 20 20 20 20 20 20 20 20 69  e 'F':.        i
1adc7 43 6f 6f 6b 69 65 20 3d 20 31 3b 0a 20 20 20 20  Cookie = 1;.    
1adc8 20 20 20 20 69 44 62 20 3d 20 28 2d 31 2a 28 69      iDb = (-1*(i
1adc9 44 62 2b 31 29 29 3b 0a 20 20 20 20 20 20 20 20  Db+1));.        
1adca 61 73 73 65 72 74 28 69 44 62 3c 3d 30 29 3b 0a  assert(iDb<=0);.
1adcb 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
1adcc 20 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20       default:.  
1adcd 20 20 20 20 20 20 69 43 6f 6f 6b 69 65 20 3d 20        iCookie = 
1adce 35 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  5;.        break
1adcf 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  ;.    }..    if(
1add0 20 7a 52 69 67 68 74 20 26 26 20 69 44 62 3e 3d   zRight && iDb>=
1add1 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 57 72  0 ){.      /* Wr
1add2 69 74 65 20 74 68 65 20 73 70 65 63 69 66 69 65  ite the specifie
1add3 64 20 63 6f 6f 6b 69 65 20 76 61 6c 75 65 20 2a  d cookie value *
1add4 2f 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 63  /.      static c
1add5 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20  onst VdbeOpList 
1add6 73 65 74 43 6f 6f 6b 69 65 5b 5d 20 3d 20 7b 0a  setCookie[] = {.
1add7 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 54 72 61          { OP_Tra
1add8 6e 73 61 63 74 69 6f 6e 2c 20 20 20 20 30 2c 20  nsaction,    0, 
1add9 20 31 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 30   1,  0},    /* 0
1adda 20 2a 2f 0a 20 20 20 20 20 20 20 20 7b 20 4f 50   */.        { OP
1addb 5f 49 6e 74 65 67 65 72 2c 20 20 20 20 20 20 20  _Integer,       
1addc 20 30 2c 20 20 30 2c 20 20 30 7d 2c 20 20 20 20   0,  0,  0},    
1addd 2f 2a 20 31 20 2a 2f 0a 20 20 20 20 20 20 20 20  /* 1 */.        
1adde 7b 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20  { OP_SetCookie, 
1addf 20 20 20 20 20 30 2c 20 20 30 2c 20 20 30 7d 2c       0,  0,  0},
1ade0 20 20 20 20 2f 2a 20 32 20 2a 2f 0a 20 20 20 20      /* 2 */.    
1ade1 20 20 7d 3b 0a 20 20 20 20 20 20 69 6e 74 20 61    };.      int a
1ade2 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
1ade3 65 41 64 64 4f 70 4c 69 73 74 28 76 2c 20 41 72  eAddOpList(v, Ar
1ade4 72 61 79 53 69 7a 65 28 73 65 74 43 6f 6f 6b 69  raySize(setCooki
1ade5 65 29 2c 20 73 65 74 43 6f 6f 6b 69 65 29 3b 0a  e), setCookie);.
1ade6 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1ade7 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64  eChangeP1(v, add
1ade8 72 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73  r, iDb);.      s
1ade9 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1adea 50 31 28 76 2c 20 61 64 64 72 2b 31 2c 20 61 74  P1(v, addr+1, at
1adeb 6f 69 28 7a 52 69 67 68 74 29 29 3b 0a 20 20 20  oi(zRight));.   
1adec 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
1aded 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2b 32  angeP1(v, addr+2
1adee 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71  , iDb);.      sq
1adef 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
1adf0 32 28 76 2c 20 61 64 64 72 2b 32 2c 20 69 43 6f  2(v, addr+2, iCo
1adf1 6f 6b 69 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65  okie);.    }else
1adf2 7b 0a 20 20 20 20 20 20 2f 2a 20 52 65 61 64 20  {.      /* Read 
1adf3 74 68 65 20 73 70 65 63 69 66 69 65 64 20 63 6f  the specified co
1adf4 6f 6b 69 65 20 76 61 6c 75 65 20 2a 2f 0a 20 20  okie value */.  
1adf5 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74      static const
1adf6 20 56 64 62 65 4f 70 4c 69 73 74 20 72 65 61 64   VdbeOpList read
1adf7 43 6f 6f 6b 69 65 5b 5d 20 3d 20 7b 0a 20 20 20  Cookie[] = {.   
1adf8 20 20 20 20 20 7b 20 4f 50 5f 52 65 61 64 43 6f       { OP_ReadCo
1adf9 6f 6b 69 65 2c 20 20 20 20 20 20 30 2c 20 20 30  okie,      0,  0
1adfa 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 30 20 2a  ,  0},    /* 0 *
1adfb 2f 0a 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 43  /.        { OP_C
1adfc 61 6c 6c 62 61 63 6b 2c 20 20 20 20 20 20 20 20  allback,        
1adfd 31 2c 20 20 30 2c 20 20 30 7d 0a 20 20 20 20 20  1,  0,  0}.     
1adfe 20 7d 3b 0a 20 20 20 20 20 20 69 6e 74 20 61 64   };.      int ad
1adff 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
1ae00 41 64 64 4f 70 4c 69 73 74 28 76 2c 20 41 72 72  AddOpList(v, Arr
1ae01 61 79 53 69 7a 65 28 72 65 61 64 43 6f 6f 6b 69  aySize(readCooki
1ae02 65 29 2c 20 72 65 61 64 43 6f 6f 6b 69 65 29 3b  e), readCookie);
1ae03 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1ae04 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61 64  beChangeP1(v, ad
1ae05 64 72 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20  dr, iDb);.      
1ae06 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1ae07 65 50 32 28 76 2c 20 61 64 64 72 2c 20 69 43 6f  eP2(v, addr, iCo
1ae08 6f 6b 69 65 29 3b 0a 20 20 20 20 20 20 73 71 6c  okie);.      sql
1ae09 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f  ite3VdbeSetNumCo
1ae0a 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 20 20  ls(v, 1);.      
1ae0b 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
1ae0c 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
1ae0d 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4c 65 66 74 2c  AME_NAME, zLeft,
1ae0e 20 50 33 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a   P3_TRANSIENT);.
1ae0f 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65      }.  }else.#e
1ae10 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
1ae11 4d 49 54 5f 53 43 48 45 4d 41 5f 56 45 52 53 49  MIT_SCHEMA_VERSI
1ae12 4f 4e 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 23  ON_PRAGMAS */..#
1ae13 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
1ae14 45 5f 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69  E_DEBUG) || defi
1ae15 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29  ned(SQLITE_TEST)
1ae16 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 70 6f 72  .  /*.  ** Repor
1ae17 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 74  t the current st
1ae18 61 74 65 20 6f 66 20 66 69 6c 65 20 6c 6f 67 73  ate of file logs
1ae19 20 66 6f 72 20 61 6c 6c 20 64 61 74 61 62 61 73   for all databas
1ae1a 65 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71  es.  */.  if( sq
1ae1b 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
1ae1c 66 74 2c 20 22 6c 6f 63 6b 5f 73 74 61 74 75 73  ft, "lock_status
1ae1d 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74 61  ")==0 ){.    sta
1ae1e 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
1ae1f 63 6f 6e 73 74 20 61 7a 4c 6f 63 6b 4e 61 6d 65  const azLockName
1ae20 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 22 75 6e  [] = {.      "un
1ae21 6c 6f 63 6b 65 64 22 2c 20 22 73 68 61 72 65 64  locked", "shared
1ae22 22 2c 20 22 72 65 73 65 72 76 65 64 22 2c 20 22  ", "reserved", "
1ae23 70 65 6e 64 69 6e 67 22 2c 20 22 65 78 63 6c 75  pending", "exclu
1ae24 73 69 76 65 22 0a 20 20 20 20 7d 3b 0a 20 20 20  sive".    };.   
1ae25 20 69 6e 74 20 69 3b 0a 20 20 20 20 56 64 62 65   int i;.    Vdbe
1ae26 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74   *v = sqlite3Get
1ae27 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
1ae28 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
1ae29 4e 75 6d 43 6f 6c 73 28 76 2c 20 32 29 3b 0a 20  NumCols(v, 2);. 
1ae2a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
1ae2b 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43  tColName(v, 0, C
1ae2c 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 64 61  OLNAME_NAME, "da
1ae2d 74 61 62 61 73 65 22 2c 20 50 33 5f 53 54 41 54  tabase", P3_STAT
1ae2e 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  IC);.    sqlite3
1ae2f 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
1ae30 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 1, COLNAME_NAM
1ae31 45 2c 20 22 73 74 61 74 75 73 22 2c 20 50 33 5f  E, "status", P3_
1ae32 53 54 41 54 49 43 29 3b 0a 20 20 20 20 66 6f 72  STATIC);.    for
1ae33 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b  (i=0; i<db->nDb;
1ae34 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 42 74 72   i++){.      Btr
1ae35 65 65 20 2a 70 42 74 3b 0a 20 20 20 20 20 20 50  ee *pBt;.      P
1ae36 61 67 65 72 20 2a 70 50 61 67 65 72 3b 0a 20 20  ager *pPager;.  
1ae37 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b      if( db->aDb[
1ae38 69 5d 2e 7a 4e 61 6d 65 3d 3d 30 20 29 20 63 6f  i].zName==0 ) co
1ae39 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 73 71  ntinue;.      sq
1ae3a 6c 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20  lite3VdbeOp3(v, 
1ae3b 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 30  OP_String8, 0, 0
1ae3c 2c 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61  , db->aDb[i].zNa
1ae3d 6d 65 2c 20 50 33 5f 53 54 41 54 49 43 29 3b 0a  me, P3_STATIC);.
1ae3e 20 20 20 20 20 20 70 42 74 20 3d 20 64 62 2d 3e        pBt = db->
1ae3f 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20  aDb[i].pBt;.    
1ae40 20 20 69 66 28 20 70 42 74 3d 3d 30 20 7c 7c 20    if( pBt==0 || 
1ae41 28 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65  (pPager = sqlite
1ae42 33 42 74 72 65 65 50 61 67 65 72 28 70 42 74 29  3BtreePager(pBt)
1ae43 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
1ae44 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28 76  sqlite3VdbeOp3(v
1ae45 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c  , OP_String8, 0,
1ae46 20 30 2c 20 22 63 6c 6f 73 65 64 22 2c 20 50 33   0, "closed", P3
1ae47 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
1ae48 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69  }else{.        i
1ae49 6e 74 20 6a 20 3d 20 73 71 6c 69 74 65 33 50 61  nt j = sqlite3Pa
1ae4a 67 65 72 4c 6f 63 6b 73 74 61 74 65 28 70 50 61  gerLockstate(pPa
1ae4b 67 65 72 29 3b 0a 20 20 20 20 20 20 20 20 73 71  ger);.        sq
1ae4c 6c 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20  lite3VdbeOp3(v, 
1ae4d 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 30  OP_String8, 0, 0
1ae4e 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 28  , .            (
1ae4f 6a 3e 3d 30 20 26 26 20 6a 3c 3d 34 29 20 3f 20  j>=0 && j<=4) ? 
1ae50 61 7a 4c 6f 63 6b 4e 61 6d 65 5b 6a 5d 20 3a 20  azLockName[j] : 
1ae51 22 75 6e 6b 6e 6f 77 6e 22 2c 20 50 33 5f 53 54  "unknown", P3_ST
1ae52 41 54 49 43 29 3b 0a 20 20 20 20 20 20 7d 0a 20  ATIC);.      }. 
1ae53 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1ae54 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 61 6c 6c  AddOp(v, OP_Call
1ae55 62 61 63 6b 2c 20 32 2c 20 30 29 3b 0a 20 20 20  back, 2, 0);.   
1ae56 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69   }.  }else.#endi
1ae57 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
1ae58 5f 53 53 45 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43  _SSE.  /*.  ** C
1ae59 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74  heck to see if t
1ae5a 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 65 6d  he sqlite_statem
1ae5b 65 6e 74 73 20 74 61 62 6c 65 20 65 78 69 73 74  ents table exist
1ae5c 73 2e 20 20 43 72 65 61 74 65 20 69 74 0a 20 20  s.  Create it.  
1ae5d 2a 2a 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f  ** if it does no
1ae5e 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71  t..  */.  if( sq
1ae5f 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
1ae60 66 74 2c 20 22 63 72 65 61 74 65 5f 73 71 6c 69  ft, "create_sqli
1ae61 74 65 5f 73 74 61 74 65 6d 65 6e 74 5f 74 61 62  te_statement_tab
1ae62 6c 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 65  le")==0 ){.    e
1ae63 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65  xtern int sqlite
1ae64 33 43 72 65 61 74 65 53 74 61 74 65 6d 65 6e 74  3CreateStatement
1ae65 73 54 61 62 6c 65 28 50 61 72 73 65 2a 29 3b 0a  sTable(Parse*);.
1ae66 20 20 20 20 73 71 6c 69 74 65 33 43 72 65 61 74      sqlite3Creat
1ae67 65 53 74 61 74 65 6d 65 6e 74 73 54 61 62 6c 65  eStatementsTable
1ae68 28 70 50 61 72 73 65 29 3b 0a 20 20 7d 65 6c 73  (pParse);.  }els
1ae69 65 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 53 51  e.#endif..#if SQ
1ae6a 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20  LITE_HAS_CODEC. 
1ae6b 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
1ae6c 43 6d 70 28 7a 4c 65 66 74 2c 20 22 6b 65 79 22  Cmp(zLeft, "key"
1ae6d 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  )==0 ){.    sqli
1ae6e 74 65 33 5f 6b 65 79 28 64 62 2c 20 7a 52 69 67  te3_key(db, zRig
1ae6f 68 74 2c 20 73 74 72 6c 65 6e 28 7a 52 69 67 68  ht, strlen(zRigh
1ae70 74 29 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e  t));.  }else.#en
1ae71 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 48  dif.#if SQLITE_H
1ae72 41 53 5f 43 4f 44 45 43 20 7c 7c 20 64 65 66 69  AS_CODEC || defi
1ae73 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
1ae74 45 5f 43 45 52 4f 44 29 0a 20 20 69 66 28 20 73  E_CEROD).  if( s
1ae75 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
1ae76 65 66 74 2c 20 22 61 63 74 69 76 61 74 65 5f 65  eft, "activate_e
1ae77 78 74 65 6e 73 69 6f 6e 73 22 29 3d 3d 30 20 29  xtensions")==0 )
1ae78 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 48 41 53  {.#if SQLITE_HAS
1ae79 5f 43 4f 44 45 43 0a 20 20 20 20 69 66 28 20 73  _CODEC.    if( s
1ae7a 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a  qlite3StrNICmp(z
1ae7b 52 69 67 68 74 2c 20 22 73 65 65 2d 22 2c 20 34  Right, "see-", 4
1ae7c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 65 78  )==0 ){.      ex
1ae7d 74 65 72 6e 20 76 6f 69 64 20 73 71 6c 69 74 65  tern void sqlite
1ae7e 33 5f 61 63 74 69 76 61 74 65 5f 73 65 65 28 63  3_activate_see(c
1ae7f 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 20  onst char*);.   
1ae80 20 20 20 73 71 6c 69 74 65 33 5f 61 63 74 69 76     sqlite3_activ
1ae81 61 74 65 5f 73 65 65 28 26 7a 52 69 67 68 74 5b  ate_see(&zRight[
1ae82 34 5d 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  4]);.    }.#endi
1ae83 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
1ae84 45 4e 41 42 4c 45 5f 43 45 52 4f 44 0a 20 20 20  ENABLE_CEROD.   
1ae85 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e   if( sqlite3StrN
1ae86 49 43 6d 70 28 7a 52 69 67 68 74 2c 20 22 63 65  ICmp(zRight, "ce
1ae87 72 6f 64 2d 22 2c 20 36 29 3d 3d 30 20 29 7b 0a  rod-", 6)==0 ){.
1ae88 20 20 20 20 20 20 65 78 74 65 72 6e 20 76 6f 69        extern voi
1ae89 64 20 73 71 6c 69 74 65 33 5f 61 63 74 69 76 61  d sqlite3_activa
1ae8a 74 65 5f 63 65 72 6f 64 28 63 6f 6e 73 74 20 63  te_cerod(const c
1ae8b 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 73 71 6c  har*);.      sql
1ae8c 69 74 65 33 5f 61 63 74 69 76 61 74 65 5f 63 65  ite3_activate_ce
1ae8d 72 6f 64 28 26 7a 52 69 67 68 74 5b 36 5d 29 3b  rod(&zRight[6]);
1ae8e 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
1ae8f 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 7b 7d 0a 0a  }.#endif..  {}..
1ae90 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 2f    if( v ){.    /
1ae91 2a 20 43 6f 64 65 20 61 6e 20 4f 50 5f 45 78 70  * Code an OP_Exp
1ae92 69 72 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f  ire at the end o
1ae93 66 20 65 61 63 68 20 50 52 41 47 4d 41 20 70 72  f each PRAGMA pr
1ae94 6f 67 72 61 6d 20 74 6f 20 63 61 75 73 65 0a 20  ogram to cause. 
1ae95 20 20 20 2a 2a 20 74 68 65 20 56 44 42 45 20 69     ** the VDBE i
1ae96 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 74 68 65 20  mplementing the 
1ae97 70 72 61 67 6d 61 20 74 6f 20 65 78 70 69 72 65  pragma to expire
1ae98 2e 20 4d 6f 73 74 20 28 61 6c 6c 3f 29 20 70 72  . Most (all?) pr
1ae99 61 67 6d 61 73 0a 20 20 20 20 2a 2a 20 61 72 65  agmas.    ** are
1ae9a 20 6f 6e 6c 79 20 76 61 6c 69 64 20 66 6f 72 20   only valid for 
1ae9b 61 20 73 69 6e 67 6c 65 20 65 78 65 63 75 74 69  a single executi
1ae9c 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73  on..    */.    s
1ae9d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1ae9e 76 2c 20 4f 50 5f 45 78 70 69 72 65 2c 20 31 2c  v, OP_Expire, 1,
1ae9f 20 30 29 3b 0a 0a 20 20 20 20 2f 2a 0a 20 20 20   0);..    /*.   
1aea0 20 2a 2a 20 52 65 73 65 74 20 74 68 65 20 73 61   ** Reset the sa
1aea1 66 65 74 79 20 6c 65 76 65 6c 2c 20 69 6e 20 63  fety level, in c
1aea2 61 73 65 20 74 68 65 20 66 75 6c 6c 66 73 79 6e  ase the fullfsyn
1aea3 63 20 66 6c 61 67 20 6f 72 20 73 79 6e 63 68 72  c flag or synchr
1aea4 6f 6e 6f 75 73 0a 20 20 20 20 2a 2a 20 73 65 74  onous.    ** set
1aea5 74 69 6e 67 20 63 68 61 6e 67 65 64 2e 0a 20 20  ting changed..  
1aea6 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c    */.#ifndef SQL
1aea7 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50  ITE_OMIT_PAGER_P
1aea8 52 41 47 4d 41 53 0a 20 20 20 20 69 66 28 20 64  RAGMAS.    if( d
1aea9 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b  b->autoCommit ){
1aeaa 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74  .      sqlite3Bt
1aeab 72 65 65 53 65 74 53 61 66 65 74 79 4c 65 76 65  reeSetSafetyLeve
1aeac 6c 28 70 44 62 2d 3e 70 42 74 2c 20 70 44 62 2d  l(pDb->pBt, pDb-
1aead 3e 73 61 66 65 74 79 5f 6c 65 76 65 6c 2c 0a 20  >safety_level,. 
1aeae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1aeaf 28 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54  (db->flags&SQLIT
1aeb0 45 5f 46 75 6c 6c 46 53 79 6e 63 29 21 3d 30 29  E_FullFSync)!=0)
1aeb1 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
1aeb2 20 7d 0a 70 72 61 67 6d 61 5f 6f 75 74 3a 0a 20   }.pragma_out:. 
1aeb3 20 73 71 6c 69 74 65 46 72 65 65 28 7a 4c 65 66   sqliteFree(zLef
1aeb4 74 29 3b 0a 20 20 73 71 6c 69 74 65 46 72 65 65  t);.  sqliteFree
1aeb5 28 7a 52 69 67 68 74 29 3b 0a 7d 0a 0a 23 65 6e  (zRight);.}..#en
1aeb6 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
1aeb7 49 54 5f 50 52 41 47 4d 41 20 7c 7c 20 53 51 4c  IT_PRAGMA || SQL
1aeb8 49 54 45 5f 4f 4d 49 54 5f 50 41 52 53 45 52 20  ITE_OMIT_PARSER 
1aeb9 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
1aeba 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 72 61 67 6d  *** End of pragm
1aebb 61 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  a.c ************
1aebc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1aebd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1aebe 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
1aebf 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70  *** Begin file p
1aec0 72 65 70 61 72 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  repare.c *******
1aec1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1aec2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1aec3 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 4d  **/./*.** 2005 M
1aec4 61 79 20 32 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ay 25.**.** The 
1aec5 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
1aec6 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
1aec7 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
1aec8 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
1aec9 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
1aeca 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
1aecb 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
1aecc 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
1aecd 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
1aece 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
1aecf 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
1aed0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
1aed1 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
1aed2 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
1aed3 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
1aed4 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
1aed5 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
1aed6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1aed7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1aed8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1aed9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1aeda 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
1aedb 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 69  e contains the i
1aedc 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
1aedd 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65   the sqlite3_pre
1aede 70 61 72 65 28 29 0a 2a 2a 20 69 6e 74 65 72 66  pare().** interf
1aedf 61 63 65 2c 20 61 6e 64 20 72 6f 75 74 69 6e 65  ace, and routine
1aee0 73 20 74 68 61 74 20 63 6f 6e 74 72 69 62 75 74  s that contribut
1aee1 65 20 74 6f 20 6c 6f 61 64 69 6e 67 20 74 68 65  e to loading the
1aee2 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
1aee3 0a 2a 2a 20 66 72 6f 6d 20 64 69 73 6b 2e 0a 2a  .** from disk..*
1aee4 2a 0a 2a 2a 20 24 49 64 3a 20 70 72 65 70 61 72  *.** $Id: prepar
1aee5 65 2e 63 2c 76 20 31 2e 35 31 20 32 30 30 37 2f  e.c,v 1.51 2007/
1aee6 30 36 2f 32 34 20 31 30 3a 31 34 3a 30 30 20 64  06/24 10:14:00 d
1aee7 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24  anielk1977 Exp $
1aee8 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46 69 6c 6c 20  .*/../*.** Fill 
1aee9 74 68 65 20 49 6e 69 74 44 61 74 61 20 73 74 72  the InitData str
1aeea 75 63 74 75 72 65 20 77 69 74 68 20 61 6e 20 65  ucture with an e
1aeeb 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 68 61  rror message tha
1aeec 74 20 69 6e 64 69 63 61 74 65 73 0a 2a 2a 20 74  t indicates.** t
1aeed 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65  hat the database
1aeee 20 69 73 20 63 6f 72 72 75 70 74 2e 0a 2a 2f 0a   is corrupt..*/.
1aeef 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 72 72  static void corr
1aef0 75 70 74 53 63 68 65 6d 61 28 49 6e 69 74 44 61  uptSchema(InitDa
1aef1 74 61 20 2a 70 44 61 74 61 2c 20 63 6f 6e 73 74  ta *pData, const
1aef2 20 63 68 61 72 20 2a 7a 45 78 74 72 61 29 7b 0a   char *zExtra){.
1aef3 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 4d 61    if( !sqlite3Ma
1aef4 6c 6c 6f 63 46 61 69 6c 65 64 28 29 20 29 7b 0a  llocFailed() ){.
1aef5 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74      sqlite3SetSt
1aef6 72 69 6e 67 28 70 44 61 74 61 2d 3e 70 7a 45 72  ring(pData->pzEr
1aef7 72 4d 73 67 2c 20 22 6d 61 6c 66 6f 72 6d 65 64  rMsg, "malformed
1aef8 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
1aef9 22 2c 0a 20 20 20 20 20 20 20 7a 45 78 74 72 61  ",.       zExtra
1aefa 21 3d 30 20 26 26 20 7a 45 78 74 72 61 5b 30 5d  !=0 && zExtra[0]
1aefb 21 3d 30 20 3f 20 22 20 2d 20 22 20 3a 20 28 63  !=0 ? " - " : (c
1aefc 68 61 72 2a 29 30 2c 20 7a 45 78 74 72 61 2c 20  har*)0, zExtra, 
1aefd 28 63 68 61 72 2a 29 30 29 3b 0a 20 20 7d 0a 20  (char*)0);.  }. 
1aefe 20 70 44 61 74 61 2d 3e 72 63 20 3d 20 53 51 4c   pData->rc = SQL
1aeff 49 54 45 5f 43 4f 52 52 55 50 54 3b 0a 7d 0a 0a  ITE_CORRUPT;.}..
1af00 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68  /*.** This is th
1af01 65 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69  e callback routi
1af02 6e 65 20 66 6f 72 20 74 68 65 20 63 6f 64 65 20  ne for the code 
1af03 74 68 61 74 20 69 6e 69 74 69 61 6c 69 7a 65 73  that initializes
1af04 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65   the.** database
1af05 2e 20 20 53 65 65 20 73 71 6c 69 74 65 33 49 6e  .  See sqlite3In
1af06 69 74 28 29 20 62 65 6c 6f 77 20 66 6f 72 20 61  it() below for a
1af07 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
1af08 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 72  ation..** This r
1af09 6f 75 74 69 6e 65 20 69 73 20 61 6c 73 6f 20 63  outine is also c
1af0a 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 4f  alled from the O
1af0b 50 5f 50 61 72 73 65 53 63 68 65 6d 61 20 6f 70  P_ParseSchema op
1af0c 63 6f 64 65 20 6f 66 20 74 68 65 20 56 44 42 45  code of the VDBE
1af0d 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 63 61 6c  ..**.** Each cal
1af0e 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e 73 20 74  lback contains t
1af0f 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 66  he following inf
1af10 6f 72 6d 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20  ormation:.**.** 
1af11 20 20 20 20 61 72 67 76 5b 30 5d 20 3d 20 6e 61      argv[0] = na
1af12 6d 65 20 6f 66 20 74 68 69 6e 67 20 62 65 69 6e  me of thing bein
1af13 67 20 63 72 65 61 74 65 64 0a 2a 2a 20 20 20 20  g created.**    
1af14 20 61 72 67 76 5b 31 5d 20 3d 20 72 6f 6f 74 20   argv[1] = root 
1af15 70 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20  page number for 
1af16 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 20  table or index. 
1af17 30 20 66 6f 72 20 74 72 69 67 67 65 72 20 6f 72  0 for trigger or
1af18 20 76 69 65 77 2e 0a 2a 2a 20 20 20 20 20 61 72   view..**     ar
1af19 67 76 5b 32 5d 20 3d 20 53 51 4c 20 74 65 78 74  gv[2] = SQL text
1af1a 20 66 6f 72 20 74 68 65 20 43 52 45 41 54 45 20   for the CREATE 
1af1b 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2f  statement..**.*/
1af1c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1af1d 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74 43  int sqlite3InitC
1af1e 61 6c 6c 62 61 63 6b 28 76 6f 69 64 20 2a 70 49  allback(void *pI
1af1f 6e 69 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 63  nit, int argc, c
1af20 68 61 72 20 2a 2a 61 72 67 76 2c 20 63 68 61 72  har **argv, char
1af21 20 2a 2a 61 7a 43 6f 6c 4e 61 6d 65 29 7b 0a 20   **azColName){. 
1af22 20 49 6e 69 74 44 61 74 61 20 2a 70 44 61 74 61   InitData *pData
1af23 20 3d 20 28 49 6e 69 74 44 61 74 61 2a 29 70 49   = (InitData*)pI
1af24 6e 69 74 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  nit;.  sqlite3 *
1af25 64 62 20 3d 20 70 44 61 74 61 2d 3e 64 62 3b 0a  db = pData->db;.
1af26 20 20 69 6e 74 20 69 44 62 20 3d 20 70 44 61 74    int iDb = pDat
1af27 61 2d 3e 69 44 62 3b 0a 0a 20 20 70 44 61 74 61  a->iDb;..  pData
1af28 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  ->rc = SQLITE_OK
1af29 3b 0a 20 20 44 62 43 6c 65 61 72 50 72 6f 70 65  ;.  DbClearPrope
1af2a 72 74 79 28 64 62 2c 20 69 44 62 2c 20 44 42 5f  rty(db, iDb, DB_
1af2b 45 6d 70 74 79 29 3b 0a 20 20 69 66 28 20 73 71  Empty);.  if( sq
1af2c 6c 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69 6c 65  lite3MallocFaile
1af2d 64 28 29 20 29 7b 0a 20 20 20 20 63 6f 72 72 75  d() ){.    corru
1af2e 70 74 53 63 68 65 6d 61 28 70 44 61 74 61 2c 20  ptSchema(pData, 
1af2f 30 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  0);.    return S
1af30 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
1af31 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63  ..  assert( argc
1af32 3d 3d 33 20 29 3b 0a 20 20 69 66 28 20 61 72 67  ==3 );.  if( arg
1af33 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  v==0 ) return 0;
1af34 20 20 20 2f 2a 20 4d 69 67 68 74 20 68 61 70 70     /* Might happ
1af35 65 6e 20 69 66 20 45 4d 50 54 59 5f 52 45 53 55  en if EMPTY_RESU
1af36 4c 54 5f 43 41 4c 4c 42 41 43 4b 53 20 61 72 65  LT_CALLBACKS are
1af37 20 6f 6e 20 2a 2f 0a 20 20 69 66 28 20 61 72 67   on */.  if( arg
1af38 76 5b 31 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 63  v[1]==0 ){.    c
1af39 6f 72 72 75 70 74 53 63 68 65 6d 61 28 70 44 61  orruptSchema(pDa
1af3a 74 61 2c 20 30 29 3b 0a 20 20 20 20 72 65 74 75  ta, 0);.    retu
1af3b 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 61 73 73 65  rn 1;.  }.  asse
1af3c 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44  rt( iDb>=0 && iD
1af3d 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 69  b<db->nDb );.  i
1af3e 66 28 20 61 72 67 76 5b 32 5d 20 26 26 20 61 72  f( argv[2] && ar
1af3f 67 76 5b 32 5d 5b 30 5d 20 29 7b 0a 20 20 20 20  gv[2][0] ){.    
1af40 2f 2a 20 43 61 6c 6c 20 74 68 65 20 70 61 72 73  /* Call the pars
1af41 65 72 20 74 6f 20 70 72 6f 63 65 73 73 20 61 20  er to process a 
1af42 43 52 45 41 54 45 20 54 41 42 4c 45 2c 20 49 4e  CREATE TABLE, IN
1af43 44 45 58 20 6f 72 20 56 49 45 57 2e 0a 20 20 20  DEX or VIEW..   
1af44 20 2a 2a 20 42 75 74 20 62 65 63 61 75 73 65 20   ** But because 
1af45 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 69 73  db->init.busy is
1af46 20 73 65 74 20 74 6f 20 31 2c 20 6e 6f 20 56 44   set to 1, no VD
1af47 42 45 20 63 6f 64 65 20 69 73 20 67 65 6e 65 72  BE code is gener
1af48 61 74 65 64 0a 20 20 20 20 2a 2a 20 6f 72 20 65  ated.    ** or e
1af49 78 65 63 75 74 65 64 2e 20 20 41 6c 6c 20 74 68  xecuted.  All th
1af4a 65 20 70 61 72 73 65 72 20 64 6f 65 73 20 69 73  e parser does is
1af4b 20 62 75 69 6c 64 20 74 68 65 20 69 6e 74 65 72   build the inter
1af4c 6e 61 6c 20 64 61 74 61 0a 20 20 20 20 2a 2a 20  nal data.    ** 
1af4d 73 74 72 75 63 74 75 72 65 73 20 74 68 61 74 20  structures that 
1af4e 64 65 73 63 72 69 62 65 20 74 68 65 20 74 61 62  describe the tab
1af4f 6c 65 2c 20 69 6e 64 65 78 2c 20 6f 72 20 76 69  le, index, or vi
1af50 65 77 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  ew..    */.    c
1af51 68 61 72 20 2a 7a 45 72 72 3b 0a 20 20 20 20 69  har *zErr;.    i
1af52 6e 74 20 72 63 3b 0a 20 20 20 20 61 73 73 65 72  nt rc;.    asser
1af53 74 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  t( db->init.busy
1af54 20 29 3b 0a 20 20 20 20 64 62 2d 3e 69 6e 69 74   );.    db->init
1af55 2e 69 44 62 20 3d 20 69 44 62 3b 0a 20 20 20 20  .iDb = iDb;.    
1af56 64 62 2d 3e 69 6e 69 74 2e 6e 65 77 54 6e 75 6d  db->init.newTnum
1af57 20 3d 20 61 74 6f 69 28 61 72 67 76 5b 31 5d 29   = atoi(argv[1])
1af58 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
1af59 65 33 5f 65 78 65 63 28 64 62 2c 20 61 72 67 76  e3_exec(db, argv
1af5a 5b 32 5d 2c 20 30 2c 20 30 2c 20 26 7a 45 72 72  [2], 0, 0, &zErr
1af5b 29 3b 0a 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e  );.    db->init.
1af5c 69 44 62 20 3d 20 30 3b 0a 20 20 20 20 61 73 73  iDb = 0;.    ass
1af5d 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f  ert( rc!=SQLITE_
1af5e 4f 4b 20 7c 7c 20 7a 45 72 72 3d 3d 30 20 29 3b  OK || zErr==0 );
1af5f 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f  .    if( SQLITE_
1af60 4f 4b 21 3d 72 63 20 29 7b 0a 20 20 20 20 20 20  OK!=rc ){.      
1af61 70 44 61 74 61 2d 3e 72 63 20 3d 20 72 63 3b 0a  pData->rc = rc;.
1af62 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
1af63 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20  LITE_NOMEM ){.  
1af64 20 20 20 20 20 20 73 71 6c 69 74 65 33 46 61 69        sqlite3Fai
1af65 6c 65 64 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20  ledMalloc();.   
1af66 20 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 21     }else if( rc!
1af67 3d 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50  =SQLITE_INTERRUP
1af68 54 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 72  T ){.        cor
1af69 72 75 70 74 53 63 68 65 6d 61 28 70 44 61 74 61  ruptSchema(pData
1af6a 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 7d  , zErr);.      }
1af6b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
1af6c 72 65 65 28 7a 45 72 72 29 3b 0a 20 20 20 20 20  ree(zErr);.     
1af6d 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
1af6e 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
1af6f 20 49 66 20 74 68 65 20 53 51 4c 20 63 6f 6c 75   If the SQL colu
1af70 6d 6e 20 69 73 20 62 6c 61 6e 6b 20 69 74 20 6d  mn is blank it m
1af71 65 61 6e 73 20 74 68 69 73 20 69 73 20 61 6e 20  eans this is an 
1af72 69 6e 64 65 78 20 74 68 61 74 0a 20 20 20 20 2a  index that.    *
1af73 2a 20 77 61 73 20 63 72 65 61 74 65 64 20 74 6f  * was created to
1af74 20 62 65 20 74 68 65 20 50 52 49 4d 41 52 59 20   be the PRIMARY 
1af75 4b 45 59 20 6f 72 20 74 6f 20 66 75 6c 66 69 6c  KEY or to fulfil
1af76 6c 20 61 20 55 4e 49 51 55 45 0a 20 20 20 20 2a  l a UNIQUE.    *
1af77 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 6f 72  * constraint for
1af78 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 2e   a CREATE TABLE.
1af79 20 20 54 68 65 20 69 6e 64 65 78 20 73 68 6f 75    The index shou
1af7a 6c 64 20 68 61 76 65 20 61 6c 72 65 61 64 79 0a  ld have already.
1af7b 20 20 20 20 2a 2a 20 62 65 65 6e 20 63 72 65 61      ** been crea
1af7c 74 65 64 20 77 68 65 6e 20 77 65 20 70 72 6f 63  ted when we proc
1af7d 65 73 73 65 64 20 74 68 65 20 43 52 45 41 54 45  essed the CREATE
1af7e 20 54 41 42 4c 45 2e 20 20 41 6c 6c 20 77 65 20   TABLE.  All we 
1af7f 68 61 76 65 0a 20 20 20 20 2a 2a 20 74 6f 20 64  have.    ** to d
1af80 6f 20 68 65 72 65 20 69 73 20 72 65 63 6f 72 64  o here is record
1af81 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e   the root page n
1af82 75 6d 62 65 72 20 66 6f 72 20 74 68 61 74 20 69  umber for that i
1af83 6e 64 65 78 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ndex..    */.   
1af84 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 0a   Index *pIndex;.
1af85 20 20 20 20 70 49 6e 64 65 78 20 3d 20 73 71 6c      pIndex = sql
1af86 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 64 62  ite3FindIndex(db
1af87 2c 20 61 72 67 76 5b 30 5d 2c 20 64 62 2d 3e 61  , argv[0], db->a
1af88 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a  Db[iDb].zName);.
1af89 20 20 20 20 69 66 28 20 70 49 6e 64 65 78 3d 3d      if( pIndex==
1af8a 30 20 7c 7c 20 70 49 6e 64 65 78 2d 3e 74 6e 75  0 || pIndex->tnu
1af8b 6d 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a  m!=0 ){.      /*
1af8c 20 54 68 69 73 20 63 61 6e 20 6f 63 63 75 72 20   This can occur 
1af8d 69 66 20 74 68 65 72 65 20 65 78 69 73 74 73 20  if there exists 
1af8e 61 6e 20 69 6e 64 65 78 20 6f 6e 20 61 20 54 45  an index on a TE
1af8f 4d 50 20 74 61 62 6c 65 20 77 68 69 63 68 0a 20  MP table which. 
1af90 20 20 20 20 20 2a 2a 20 68 61 73 20 74 68 65 20       ** has the 
1af91 73 61 6d 65 20 6e 61 6d 65 20 61 73 20 61 6e 6f  same name as ano
1af92 74 68 65 72 20 69 6e 64 65 78 20 6f 6e 20 61 20  ther index on a 
1af93 70 65 72 6d 61 6e 65 6e 74 20 69 6e 64 65 78 2e  permanent index.
1af94 20 20 53 69 6e 63 65 0a 20 20 20 20 20 20 2a 2a    Since.      **
1af95 20 74 68 65 20 70 65 72 6d 61 6e 65 6e 74 20 74   the permanent t
1af96 61 62 6c 65 20 69 73 20 68 69 64 64 65 6e 20 62  able is hidden b
1af97 79 20 74 68 65 20 54 45 4d 50 20 74 61 62 6c 65  y the TEMP table
1af98 2c 20 77 65 20 63 61 6e 20 61 6c 73 6f 0a 20 20  , we can also.  
1af99 20 20 20 20 2a 2a 20 73 61 66 65 6c 79 20 69 67      ** safely ig
1af9a 6e 6f 72 65 20 74 68 65 20 69 6e 64 65 78 20 6f  nore the index o
1af9b 6e 20 74 68 65 20 70 65 72 6d 61 6e 65 6e 74 20  n the permanent 
1af9c 74 61 62 6c 65 2e 0a 20 20 20 20 20 20 2a 2f 0a  table..      */.
1af9d 20 20 20 20 20 20 2f 2a 20 44 6f 20 4e 6f 74 68        /* Do Noth
1af9e 69 6e 67 20 2a 2f 3b 0a 20 20 20 20 7d 65 6c 73  ing */;.    }els
1af9f 65 7b 0a 20 20 20 20 20 20 70 49 6e 64 65 78 2d  e{.      pIndex-
1afa0 3e 74 6e 75 6d 20 3d 20 61 74 6f 69 28 61 72 67  >tnum = atoi(arg
1afa1 76 5b 31 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  v[1]);.    }.  }
1afa2 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
1afa3 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f  /*.** Attempt to
1afa4 20 72 65 61 64 20 74 68 65 20 64 61 74 61 62 61   read the databa
1afa5 73 65 20 73 63 68 65 6d 61 20 61 6e 64 20 69 6e  se schema and in
1afa6 69 74 69 61 6c 69 7a 65 20 69 6e 74 65 72 6e 61  itialize interna
1afa7 6c 0a 2a 2a 20 64 61 74 61 20 73 74 72 75 63 74  l.** data struct
1afa8 75 72 65 73 20 66 6f 72 20 61 20 73 69 6e 67 6c  ures for a singl
1afa9 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
1afaa 20 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20 74    The index of t
1afab 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66  he.** database f
1afac 69 6c 65 20 69 73 20 67 69 76 65 6e 20 62 79 20  ile is given by 
1afad 69 44 62 2e 20 20 69 44 62 3d 3d 30 20 69 73 20  iDb.  iDb==0 is 
1afae 75 73 65 64 20 66 6f 72 20 74 68 65 20 6d 61 69  used for the mai
1afaf 6e 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20  n.** database.  
1afb0 69 44 62 3d 3d 31 20 73 68 6f 75 6c 64 20 6e 65  iDb==1 should ne
1afb1 76 65 72 20 62 65 20 75 73 65 64 2e 20 20 69 44  ver be used.  iD
1afb2 62 3e 3d 32 20 69 73 20 75 73 65 64 20 66 6f 72  b>=2 is used for
1afb3 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79 20 64 61  .** auxiliary da
1afb4 74 61 62 61 73 65 73 2e 20 20 52 65 74 75 72 6e  tabases.  Return
1afb5 20 6f 6e 65 20 6f 66 20 74 68 65 20 53 51 4c 49   one of the SQLI
1afb6 54 45 5f 20 65 72 72 6f 72 20 63 6f 64 65 73 20  TE_ error codes 
1afb7 74 6f 0a 2a 2a 20 69 6e 64 69 63 61 74 65 20 73  to.** indicate s
1afb8 75 63 63 65 73 73 20 6f 72 20 66 61 69 6c 75 72  uccess or failur
1afb9 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
1afba 20 73 71 6c 69 74 65 33 49 6e 69 74 4f 6e 65 28   sqlite3InitOne(
1afbb 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74  sqlite3 *db, int
1afbc 20 69 44 62 2c 20 63 68 61 72 20 2a 2a 70 7a 45   iDb, char **pzE
1afbd 72 72 4d 73 67 29 7b 0a 20 20 69 6e 74 20 72 63  rrMsg){.  int rc
1afbe 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 63 75  ;.  BtCursor *cu
1afbf 72 4d 61 69 6e 3b 0a 20 20 69 6e 74 20 73 69 7a  rMain;.  int siz
1afc0 65 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  e;.  Table *pTab
1afc1 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 63  ;.  Db *pDb;.  c
1afc2 68 61 72 20 63 6f 6e 73 74 20 2a 61 7a 41 72 67  har const *azArg
1afc3 5b 34 5d 3b 0a 20 20 69 6e 74 20 6d 65 74 61 5b  [4];.  int meta[
1afc4 31 30 5d 3b 0a 20 20 49 6e 69 74 44 61 74 61 20  10];.  InitData 
1afc5 69 6e 69 74 44 61 74 61 3b 0a 20 20 63 68 61 72  initData;.  char
1afc6 20 63 6f 6e 73 74 20 2a 7a 4d 61 73 74 65 72 53   const *zMasterS
1afc7 63 68 65 6d 61 3b 0a 20 20 63 68 61 72 20 63 6f  chema;.  char co
1afc8 6e 73 74 20 2a 7a 4d 61 73 74 65 72 4e 61 6d 65  nst *zMasterName
1afc9 20 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28   = SCHEMA_TABLE(
1afca 69 44 62 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  iDb);..  /*.  **
1afcb 20 54 68 65 20 6d 61 73 74 65 72 20 64 61 74 61   The master data
1afcc 62 61 73 65 20 74 61 62 6c 65 20 68 61 73 20 61  base table has a
1afcd 20 73 74 72 75 63 74 75 72 65 20 6c 69 6b 65 20   structure like 
1afce 74 68 69 73 0a 20 20 2a 2f 0a 20 20 73 74 61 74  this.  */.  stat
1afcf 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 6d 61  ic const char ma
1afd0 73 74 65 72 5f 73 63 68 65 6d 61 5b 5d 20 3d 20  ster_schema[] = 
1afd1 0a 20 20 20 20 20 22 43 52 45 41 54 45 20 54 41  .     "CREATE TA
1afd2 42 4c 45 20 73 71 6c 69 74 65 5f 6d 61 73 74 65  BLE sqlite_maste
1afd3 72 28 5c 6e 22 0a 20 20 20 20 20 22 20 20 74 79  r(\n".     "  ty
1afd4 70 65 20 74 65 78 74 2c 5c 6e 22 0a 20 20 20 20  pe text,\n".    
1afd5 20 22 20 20 6e 61 6d 65 20 74 65 78 74 2c 5c 6e   "  name text,\n
1afd6 22 0a 20 20 20 20 20 22 20 20 74 62 6c 5f 6e 61  ".     "  tbl_na
1afd7 6d 65 20 74 65 78 74 2c 5c 6e 22 0a 20 20 20 20  me text,\n".    
1afd8 20 22 20 20 72 6f 6f 74 70 61 67 65 20 69 6e 74   "  rootpage int
1afd9 65 67 65 72 2c 5c 6e 22 0a 20 20 20 20 20 22 20  eger,\n".     " 
1afda 20 73 71 6c 20 74 65 78 74 5c 6e 22 0a 20 20 20   sql text\n".   
1afdb 20 20 22 29 22 0a 20 20 3b 0a 23 69 66 6e 64 65    ")".  ;.#ifnde
1afdc 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45  f SQLITE_OMIT_TE
1afdd 4d 50 44 42 0a 20 20 73 74 61 74 69 63 20 63 6f  MPDB.  static co
1afde 6e 73 74 20 63 68 61 72 20 74 65 6d 70 5f 6d 61  nst char temp_ma
1afdf 73 74 65 72 5f 73 63 68 65 6d 61 5b 5d 20 3d 20  ster_schema[] = 
1afe0 0a 20 20 20 20 20 22 43 52 45 41 54 45 20 54 45  .     "CREATE TE
1afe1 4d 50 20 54 41 42 4c 45 20 73 71 6c 69 74 65 5f  MP TABLE sqlite_
1afe2 74 65 6d 70 5f 6d 61 73 74 65 72 28 5c 6e 22 0a  temp_master(\n".
1afe3 20 20 20 20 20 22 20 20 74 79 70 65 20 74 65 78       "  type tex
1afe4 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20 6e 61  t,\n".     "  na
1afe5 6d 65 20 74 65 78 74 2c 5c 6e 22 0a 20 20 20 20  me text,\n".    
1afe6 20 22 20 20 74 62 6c 5f 6e 61 6d 65 20 74 65 78   "  tbl_name tex
1afe7 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20 72 6f  t,\n".     "  ro
1afe8 6f 74 70 61 67 65 20 69 6e 74 65 67 65 72 2c 5c  otpage integer,\
1afe9 6e 22 0a 20 20 20 20 20 22 20 20 73 71 6c 20 74  n".     "  sql t
1afea 65 78 74 5c 6e 22 0a 20 20 20 20 20 22 29 22 0a  ext\n".     ")".
1afeb 20 20 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66    ;.#else.  #def
1afec 69 6e 65 20 74 65 6d 70 5f 6d 61 73 74 65 72 5f  ine temp_master_
1afed 73 63 68 65 6d 61 20 30 0a 23 65 6e 64 69 66 0a  schema 0.#endif.
1afee 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d  .  assert( iDb>=
1afef 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62  0 && iDb<db->nDb
1aff0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62   );.  assert( db
1aff1 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65  ->aDb[iDb].pSche
1aff2 6d 61 20 29 3b 0a 0a 20 20 2f 2a 20 7a 4d 61 73  ma );..  /* zMas
1aff3 74 65 72 53 63 68 65 6d 61 20 61 6e 64 20 7a 49  terSchema and zI
1aff4 6e 69 74 53 63 72 69 70 74 20 61 72 65 20 73 65  nitScript are se
1aff5 74 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68  t to point at th
1aff6 65 20 6d 61 73 74 65 72 20 73 63 68 65 6d 61 0a  e master schema.
1aff7 20 20 2a 2a 20 61 6e 64 20 69 6e 69 74 69 61 6c    ** and initial
1aff8 69 73 61 74 69 6f 6e 20 73 63 72 69 70 74 20 61  isation script a
1aff9 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 74  ppropriate for t
1affa 68 65 20 64 61 74 61 62 61 73 65 20 62 65 69 6e  he database bein
1affb 67 0a 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69 73  g.  ** initialis
1affc 65 64 2e 20 7a 4d 61 73 74 65 72 4e 61 6d 65 20  ed. zMasterName 
1affd 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  is the name of t
1affe 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 2e  he master table.
1afff 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 4f 4d 49  .  */.  if( !OMI
1b000 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44 62 3d  T_TEMPDB && iDb=
1b001 3d 31 20 29 7b 0a 20 20 20 20 7a 4d 61 73 74 65  =1 ){.    zMaste
1b002 72 53 63 68 65 6d 61 20 3d 20 74 65 6d 70 5f 6d  rSchema = temp_m
1b003 61 73 74 65 72 5f 73 63 68 65 6d 61 3b 0a 20 20  aster_schema;.  
1b004 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 4d 61 73 74  }else{.    zMast
1b005 65 72 53 63 68 65 6d 61 20 3d 20 6d 61 73 74 65  erSchema = maste
1b006 72 5f 73 63 68 65 6d 61 3b 0a 20 20 7d 0a 20 20  r_schema;.  }.  
1b007 7a 4d 61 73 74 65 72 4e 61 6d 65 20 3d 20 53 43  zMasterName = SC
1b008 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 3b  HEMA_TABLE(iDb);
1b009 0a 0a 20 20 2f 2a 20 43 6f 6e 73 74 72 75 63 74  ..  /* Construct
1b00a 20 74 68 65 20 73 63 68 65 6d 61 20 74 61 62 6c   the schema tabl
1b00b 65 73 2e 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  es.  */.  sqlite
1b00c 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a  3SafetyOff(db);.
1b00d 20 20 61 7a 41 72 67 5b 30 5d 20 3d 20 7a 4d 61    azArg[0] = zMa
1b00e 73 74 65 72 4e 61 6d 65 3b 0a 20 20 61 7a 41 72  sterName;.  azAr
1b00f 67 5b 31 5d 20 3d 20 22 31 22 3b 0a 20 20 61 7a  g[1] = "1";.  az
1b010 41 72 67 5b 32 5d 20 3d 20 7a 4d 61 73 74 65 72  Arg[2] = zMaster
1b011 53 63 68 65 6d 61 3b 0a 20 20 61 7a 41 72 67 5b  Schema;.  azArg[
1b012 33 5d 20 3d 20 30 3b 0a 20 20 69 6e 69 74 44 61  3] = 0;.  initDa
1b013 74 61 2e 64 62 20 3d 20 64 62 3b 0a 20 20 69 6e  ta.db = db;.  in
1b014 69 74 44 61 74 61 2e 69 44 62 20 3d 20 69 44 62  itData.iDb = iDb
1b015 3b 0a 20 20 69 6e 69 74 44 61 74 61 2e 70 7a 45  ;.  initData.pzE
1b016 72 72 4d 73 67 20 3d 20 70 7a 45 72 72 4d 73 67  rrMsg = pzErrMsg
1b017 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
1b018 49 6e 69 74 43 61 6c 6c 62 61 63 6b 28 26 69 6e  InitCallback(&in
1b019 69 74 44 61 74 61 2c 20 33 2c 20 28 63 68 61 72  itData, 3, (char
1b01a 20 2a 2a 29 61 7a 41 72 67 2c 20 30 29 3b 0a 20   **)azArg, 0);. 
1b01b 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 73   if( rc ){.    s
1b01c 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64  qlite3SafetyOn(d
1b01d 62 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 69  b);.    return i
1b01e 6e 69 74 44 61 74 61 2e 72 63 3b 0a 20 20 7d 0a  nitData.rc;.  }.
1b01f 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33    pTab = sqlite3
1b020 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 4d  FindTable(db, zM
1b021 61 73 74 65 72 4e 61 6d 65 2c 20 64 62 2d 3e 61  asterName, db->a
1b022 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a  Db[iDb].zName);.
1b023 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20    if( pTab ){.  
1b024 20 20 70 54 61 62 2d 3e 72 65 61 64 4f 6e 6c 79    pTab->readOnly
1b025 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 73 71 6c 69   = 1;.  }.  sqli
1b026 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b  te3SafetyOn(db);
1b027 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65 20 61 20  ..  /* Create a 
1b028 63 75 72 73 6f 72 20 74 6f 20 68 6f 6c 64 20 74  cursor to hold t
1b029 68 65 20 64 61 74 61 62 61 73 65 20 6f 70 65 6e  he database open
1b02a 0a 20 20 2a 2f 0a 20 20 70 44 62 20 3d 20 26 64  .  */.  pDb = &d
1b02b 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 69  b->aDb[iDb];.  i
1b02c 66 28 20 70 44 62 2d 3e 70 42 74 3d 3d 30 20 29  f( pDb->pBt==0 )
1b02d 7b 0a 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f  {.    if( !OMIT_
1b02e 54 45 4d 50 44 42 20 26 26 20 69 44 62 3d 3d 31  TEMPDB && iDb==1
1b02f 20 29 7b 0a 20 20 20 20 20 20 44 62 53 65 74 50   ){.      DbSetP
1b030 72 6f 70 65 72 74 79 28 64 62 2c 20 31 2c 20 44  roperty(db, 1, D
1b031 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 3b  B_SchemaLoaded);
1b032 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
1b033 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
1b034 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42  .  rc = sqlite3B
1b035 74 72 65 65 43 75 72 73 6f 72 28 70 44 62 2d 3e  treeCursor(pDb->
1b036 70 42 74 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54  pBt, MASTER_ROOT
1b037 2c 20 30 2c 20 30 2c 20 30 2c 20 26 63 75 72 4d  , 0, 0, 0, &curM
1b038 61 69 6e 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  ain);.  if( rc!=
1b039 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21  SQLITE_OK && rc!
1b03a 3d 53 51 4c 49 54 45 5f 45 4d 50 54 59 20 29 7b  =SQLITE_EMPTY ){
1b03b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53  .    sqlite3SetS
1b03c 74 72 69 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20  tring(pzErrMsg, 
1b03d 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 72 63  sqlite3ErrStr(rc
1b03e 29 2c 20 28 63 68 61 72 2a 29 30 29 3b 0a 20 20  ), (char*)0);.  
1b03f 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
1b040 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68 65 20 64  ..  /* Get the d
1b041 61 74 61 62 61 73 65 20 6d 65 74 61 20 69 6e 66  atabase meta inf
1b042 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20 2a 2a 0a 20  ormation..  **. 
1b043 20 2a 2a 20 4d 65 74 61 20 76 61 6c 75 65 73 20   ** Meta values 
1b044 61 72 65 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  are as follows:.
1b045 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 30 5d 20    **    meta[0] 
1b046 20 20 53 63 68 65 6d 61 20 63 6f 6f 6b 69 65 2e    Schema cookie.
1b047 20 20 43 68 61 6e 67 65 73 20 77 69 74 68 20 65    Changes with e
1b048 61 63 68 20 73 63 68 65 6d 61 20 63 68 61 6e 67  ach schema chang
1b049 65 2e 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b  e..  **    meta[
1b04a 31 5d 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74  1]   File format
1b04b 20 6f 66 20 73 63 68 65 6d 61 20 6c 61 79 65 72   of schema layer
1b04c 2e 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 32  ..  **    meta[2
1b04d 5d 20 20 20 53 69 7a 65 20 6f 66 20 74 68 65 20  ]   Size of the 
1b04e 70 61 67 65 20 63 61 63 68 65 2e 0a 20 20 2a 2a  page cache..  **
1b04f 20 20 20 20 6d 65 74 61 5b 33 5d 20 20 20 55 73      meta[3]   Us
1b050 65 20 66 72 65 65 6c 69 73 74 20 69 66 20 30 2e  e freelist if 0.
1b051 20 20 41 75 74 6f 76 61 63 75 75 6d 20 69 66 20    Autovacuum if 
1b052 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72  greater than zer
1b053 6f 2e 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b  o..  **    meta[
1b054 34 5d 20 20 20 44 62 20 74 65 78 74 20 65 6e 63  4]   Db text enc
1b055 6f 64 69 6e 67 2e 20 31 3a 55 54 46 2d 38 20 32  oding. 1:UTF-8 2
1b056 3a 55 54 46 2d 31 36 4c 45 20 33 3a 55 54 46 2d  :UTF-16LE 3:UTF-
1b057 31 36 42 45 0a 20 20 2a 2a 20 20 20 20 6d 65 74  16BE.  **    met
1b058 61 5b 35 5d 20 20 20 54 68 65 20 75 73 65 72 20  a[5]   The user 
1b059 63 6f 6f 6b 69 65 2e 20 55 73 65 64 20 62 79 20  cookie. Used by 
1b05a 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e  the application.
1b05b 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 36 5d  .  **    meta[6]
1b05c 20 20 20 49 6e 63 72 65 6d 65 6e 74 61 6c 2d 76     Incremental-v
1b05d 61 63 75 75 6d 20 66 6c 61 67 2e 0a 20 20 2a 2a  acuum flag..  **
1b05e 20 20 20 20 6d 65 74 61 5b 37 5d 0a 20 20 2a 2a      meta[7].  **
1b05f 20 20 20 20 6d 65 74 61 5b 38 5d 0a 20 20 2a 2a      meta[8].  **
1b060 20 20 20 20 6d 65 74 61 5b 39 5d 0a 20 20 2a 2a      meta[9].  **
1b061 0a 20 20 2a 2a 20 4e 6f 74 65 3a 20 54 68 65 20  .  ** Note: The 
1b062 23 64 65 66 69 6e 65 64 20 53 51 4c 49 54 45 5f  #defined SQLITE_
1b063 55 54 46 2a 20 73 79 6d 62 6f 6c 73 20 69 6e 20  UTF* symbols in 
1b064 73 71 6c 69 74 65 49 6e 74 2e 68 20 63 6f 72 72  sqliteInt.h corr
1b065 65 73 70 6f 6e 64 20 74 6f 0a 20 20 2a 2a 20 74  espond to.  ** t
1b066 68 65 20 70 6f 73 73 69 62 6c 65 20 76 61 6c 75  he possible valu
1b067 65 73 20 6f 66 20 6d 65 74 61 5b 34 5d 2e 0a 20  es of meta[4].. 
1b068 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   */.  if( rc==SQ
1b069 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69  LITE_OK ){.    i
1b06a 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d  nt i;.    for(i=
1b06b 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  0; rc==SQLITE_OK
1b06c 20 26 26 20 69 3c 73 69 7a 65 6f 66 28 6d 65 74   && i<sizeof(met
1b06d 61 29 2f 73 69 7a 65 6f 66 28 6d 65 74 61 5b 30  a)/sizeof(meta[0
1b06e 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ]); i++){.      
1b06f 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1b070 65 47 65 74 4d 65 74 61 28 70 44 62 2d 3e 70 42  eGetMeta(pDb->pB
1b071 74 2c 20 69 2b 31 2c 20 28 75 33 32 20 2a 29 26  t, i+1, (u32 *)&
1b072 6d 65 74 61 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a  meta[i]);.    }.
1b073 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20      if( rc ){.  
1b074 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74      sqlite3SetSt
1b075 72 69 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20 73  ring(pzErrMsg, s
1b076 71 6c 69 74 65 33 45 72 72 53 74 72 28 72 63 29  qlite3ErrStr(rc)
1b077 2c 20 28 63 68 61 72 2a 29 30 29 3b 0a 20 20 20  , (char*)0);.   
1b078 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43     sqlite3BtreeC
1b079 6c 6f 73 65 43 75 72 73 6f 72 28 63 75 72 4d 61  loseCursor(curMa
1b07a 69 6e 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  in);.      retur
1b07b 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  n rc;.    }.  }e
1b07c 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28  lse{.    memset(
1b07d 6d 65 74 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28  meta, 0, sizeof(
1b07e 6d 65 74 61 29 29 3b 0a 20 20 7d 0a 20 20 70 44  meta));.  }.  pD
1b07f 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65  b->pSchema->sche
1b080 6d 61 5f 63 6f 6f 6b 69 65 20 3d 20 6d 65 74 61  ma_cookie = meta
1b081 5b 30 5d 3b 0a 0a 20 20 2f 2a 20 49 66 20 6f 70  [0];..  /* If op
1b082 65 6e 69 6e 67 20 61 20 6e 6f 6e 2d 65 6d 70 74  ening a non-empt
1b083 79 20 64 61 74 61 62 61 73 65 2c 20 63 68 65 63  y database, chec
1b084 6b 20 74 68 65 20 74 65 78 74 20 65 6e 63 6f 64  k the text encod
1b085 69 6e 67 2e 20 46 6f 72 20 74 68 65 0a 20 20 2a  ing. For the.  *
1b086 2a 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2c  * main database,
1b087 20 73 65 74 20 73 71 6c 69 74 65 33 2e 65 6e 63   set sqlite3.enc
1b088 20 74 6f 20 74 68 65 20 65 6e 63 6f 64 69 6e 67   to the encoding
1b089 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74   of the main dat
1b08a 61 62 61 73 65 2e 0a 20 20 2a 2a 20 46 6f 72 20  abase..  ** For 
1b08b 61 6e 20 61 74 74 61 63 68 65 64 20 64 62 2c 20  an attached db, 
1b08c 69 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 69  it is an error i
1b08d 66 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 69  f the encoding i
1b08e 73 20 6e 6f 74 20 74 68 65 20 73 61 6d 65 0a 20  s not the same. 
1b08f 20 2a 2a 20 61 73 20 73 71 6c 69 74 65 33 2e 65   ** as sqlite3.e
1b090 6e 63 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6d  nc..  */.  if( m
1b091 65 74 61 5b 34 5d 20 29 7b 20 20 2f 2a 20 74 65  eta[4] ){  /* te
1b092 78 74 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20  xt encoding */. 
1b093 20 20 20 69 66 28 20 69 44 62 3d 3d 30 20 29 7b     if( iDb==0 ){
1b094 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 6f 70 65  .      /* If ope
1b095 6e 69 6e 67 20 74 68 65 20 6d 61 69 6e 20 64 61  ning the main da
1b096 74 61 62 61 73 65 2c 20 73 65 74 20 45 4e 43 28  tabase, set ENC(
1b097 64 62 29 2e 20 2a 2f 0a 20 20 20 20 20 20 45 4e  db). */.      EN
1b098 43 28 64 62 29 20 3d 20 28 75 38 29 6d 65 74 61  C(db) = (u8)meta
1b099 5b 34 5d 3b 0a 20 20 20 20 20 20 64 62 2d 3e 70  [4];.      db->p
1b09a 44 66 6c 74 43 6f 6c 6c 20 3d 20 73 71 6c 69 74  DfltColl = sqlit
1b09b 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62  e3FindCollSeq(db
1b09c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 22  , SQLITE_UTF8, "
1b09d 42 49 4e 41 52 59 22 2c 20 36 2c 20 30 29 3b 0a  BINARY", 6, 0);.
1b09e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1b09f 20 2f 2a 20 49 66 20 6f 70 65 6e 69 6e 67 20 61   /* If opening a
1b0a0 6e 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62  n attached datab
1b0a1 61 73 65 2c 20 74 68 65 20 65 6e 63 6f 64 69 6e  ase, the encodin
1b0a2 67 20 6d 75 63 68 20 6d 61 74 63 68 20 45 4e 43  g much match ENC
1b0a3 28 64 62 29 20 2a 2f 0a 20 20 20 20 20 20 69 66  (db) */.      if
1b0a4 28 20 6d 65 74 61 5b 34 5d 21 3d 45 4e 43 28 64  ( meta[4]!=ENC(d
1b0a5 62 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  b) ){.        sq
1b0a6 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43  lite3BtreeCloseC
1b0a7 75 72 73 6f 72 28 63 75 72 4d 61 69 6e 29 3b 0a  ursor(curMain);.
1b0a8 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
1b0a9 65 74 53 74 72 69 6e 67 28 70 7a 45 72 72 4d 73  etString(pzErrMs
1b0aa 67 2c 20 22 61 74 74 61 63 68 65 64 20 64 61 74  g, "attached dat
1b0ab 61 62 61 73 65 73 20 6d 75 73 74 20 75 73 65 20  abases must use 
1b0ac 74 68 65 20 73 61 6d 65 22 0a 20 20 20 20 20 20  the same".      
1b0ad 20 20 20 20 20 20 22 20 74 65 78 74 20 65 6e 63        " text enc
1b0ae 6f 64 69 6e 67 20 61 73 20 6d 61 69 6e 20 64 61  oding as main da
1b0af 74 61 62 61 73 65 22 2c 20 28 63 68 61 72 2a 29  tabase", (char*)
1b0b0 30 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  0);.        retu
1b0b1 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
1b0b2 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1b0b3 20 7d 65 6c 73 65 7b 0a 20 20 20 20 44 62 53 65   }else{.    DbSe
1b0b4 74 50 72 6f 70 65 72 74 79 28 64 62 2c 20 69 44  tProperty(db, iD
1b0b5 62 2c 20 44 42 5f 45 6d 70 74 79 29 3b 0a 20 20  b, DB_Empty);.  
1b0b6 7d 0a 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61  }.  pDb->pSchema
1b0b7 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b  ->enc = ENC(db);
1b0b8 0a 0a 20 20 73 69 7a 65 20 3d 20 6d 65 74 61 5b  ..  size = meta[
1b0b9 32 5d 3b 0a 20 20 69 66 28 20 73 69 7a 65 3d 3d  2];.  if( size==
1b0ba 30 20 29 7b 20 73 69 7a 65 20 3d 20 53 51 4c 49  0 ){ size = SQLI
1b0bb 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45  TE_DEFAULT_CACHE
1b0bc 5f 53 49 5a 45 3b 20 7d 0a 20 20 70 44 62 2d 3e  _SIZE; }.  pDb->
1b0bd 70 53 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73  pSchema->cache_s
1b0be 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 73 71  ize = size;.  sq
1b0bf 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63  lite3BtreeSetCac
1b0c0 68 65 53 69 7a 65 28 70 44 62 2d 3e 70 42 74 2c  heSize(pDb->pBt,
1b0c1 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63   pDb->pSchema->c
1b0c2 61 63 68 65 5f 73 69 7a 65 29 3b 0a 0a 20 20 2f  ache_size);..  /
1b0c3 2a 0a 20 20 2a 2a 20 66 69 6c 65 5f 66 6f 72 6d  *.  ** file_form
1b0c4 61 74 3d 3d 31 20 20 20 20 56 65 72 73 69 6f 6e  at==1    Version
1b0c5 20 33 2e 30 2e 30 2e 0a 20 20 2a 2a 20 66 69 6c   3.0.0..  ** fil
1b0c6 65 5f 66 6f 72 6d 61 74 3d 3d 32 20 20 20 20 56  e_format==2    V
1b0c7 65 72 73 69 6f 6e 20 33 2e 31 2e 33 2e 20 20 2f  ersion 3.1.3.  /
1b0c8 2f 20 41 4c 54 45 52 20 54 41 42 4c 45 20 41 44  / ALTER TABLE AD
1b0c9 44 20 43 4f 4c 55 4d 4e 0a 20 20 2a 2a 20 66 69  D COLUMN.  ** fi
1b0ca 6c 65 5f 66 6f 72 6d 61 74 3d 3d 33 20 20 20 20  le_format==3    
1b0cb 56 65 72 73 69 6f 6e 20 33 2e 31 2e 34 2e 20 20  Version 3.1.4.  
1b0cc 2f 2f 20 64 69 74 74 6f 20 62 75 74 20 77 69 74  // ditto but wit
1b0cd 68 20 6e 6f 6e 2d 4e 55 4c 4c 20 64 65 66 61 75  h non-NULL defau
1b0ce 6c 74 73 0a 20 20 2a 2a 20 66 69 6c 65 5f 66 6f  lts.  ** file_fo
1b0cf 72 6d 61 74 3d 3d 34 20 20 20 20 56 65 72 73 69  rmat==4    Versi
1b0d0 6f 6e 20 33 2e 33 2e 30 2e 20 20 2f 2f 20 44 45  on 3.3.0.  // DE
1b0d1 53 43 20 69 6e 64 69 63 65 73 2e 20 20 42 6f 6f  SC indices.  Boo
1b0d2 6c 65 61 6e 20 63 6f 6e 73 74 61 6e 74 73 0a 20  lean constants. 
1b0d3 20 2a 2f 0a 20 20 70 44 62 2d 3e 70 53 63 68 65   */.  pDb->pSche
1b0d4 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 20  ma->file_format 
1b0d5 3d 20 6d 65 74 61 5b 31 5d 3b 0a 20 20 69 66 28  = meta[1];.  if(
1b0d6 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66   pDb->pSchema->f
1b0d7 69 6c 65 5f 66 6f 72 6d 61 74 3d 3d 30 20 29 7b  ile_format==0 ){
1b0d8 0a 20 20 20 20 70 44 62 2d 3e 70 53 63 68 65 6d  .    pDb->pSchem
1b0d9 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 20 3d  a->file_format =
1b0da 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 44   1;.  }.  if( pD
1b0db 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65  b->pSchema->file
1b0dc 5f 66 6f 72 6d 61 74 3e 53 51 4c 49 54 45 5f 4d  _format>SQLITE_M
1b0dd 41 58 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 20 29  AX_FILE_FORMAT )
1b0de 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  {.    sqlite3Btr
1b0df 65 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 63 75  eeCloseCursor(cu
1b0e0 72 4d 61 69 6e 29 3b 0a 20 20 20 20 73 71 6c 69  rMain);.    sqli
1b0e1 74 65 33 53 65 74 53 74 72 69 6e 67 28 70 7a 45  te3SetString(pzE
1b0e2 72 72 4d 73 67 2c 20 22 75 6e 73 75 70 70 6f 72  rrMsg, "unsuppor
1b0e3 74 65 64 20 66 69 6c 65 20 66 6f 72 6d 61 74 22  ted file format"
1b0e4 2c 20 28 63 68 61 72 2a 29 30 29 3b 0a 20 20 20  , (char*)0);.   
1b0e5 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
1b0e6 52 52 4f 52 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a  RROR;.  }...  /*
1b0e7 20 52 65 61 64 20 74 68 65 20 73 63 68 65 6d 61   Read the schema
1b0e8 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 75 74   information out
1b0e9 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61 20 74   of the schema t
1b0ea 61 62 6c 65 73 0a 20 20 2a 2f 0a 20 20 61 73 73  ables.  */.  ass
1b0eb 65 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75  ert( db->init.bu
1b0ec 73 79 20 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  sy );.  if( rc==
1b0ed 53 51 4c 49 54 45 5f 45 4d 50 54 59 20 29 7b 0a  SQLITE_EMPTY ){.
1b0ee 20 20 20 20 2f 2a 20 46 6f 72 20 61 6e 20 65 6d      /* For an em
1b0ef 70 74 79 20 64 61 74 61 62 61 73 65 2c 20 74 68  pty database, th
1b0f0 65 72 65 20 69 73 20 6e 6f 74 68 69 6e 67 20 74  ere is nothing t
1b0f1 6f 20 72 65 61 64 20 2a 2f 0a 20 20 20 20 72 63  o read */.    rc
1b0f2 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
1b0f3 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 20  }else{.    char 
1b0f4 2a 7a 53 71 6c 3b 0a 20 20 20 20 7a 53 71 6c 20  *zSql;.    zSql 
1b0f5 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
1b0f6 28 0a 20 20 20 20 20 20 20 20 22 53 45 4c 45 43  (.        "SELEC
1b0f7 54 20 6e 61 6d 65 2c 20 72 6f 6f 74 70 61 67 65  T name, rootpage
1b0f8 2c 20 73 71 6c 20 46 52 4f 4d 20 27 25 71 27 2e  , sql FROM '%q'.
1b0f9 25 73 22 2c 0a 20 20 20 20 20 20 20 20 64 62 2d  %s",.        db-
1b0fa 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c  >aDb[iDb].zName,
1b0fb 20 7a 4d 61 73 74 65 72 4e 61 6d 65 29 3b 0a 20   zMasterName);. 
1b0fc 20 20 20 73 71 6c 69 74 65 33 53 61 66 65 74 79     sqlite3Safety
1b0fd 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 72 63 20  Off(db);.    rc 
1b0fe 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64  = sqlite3_exec(d
1b0ff 62 2c 20 7a 53 71 6c 2c 20 73 71 6c 69 74 65 33  b, zSql, sqlite3
1b100 49 6e 69 74 43 61 6c 6c 62 61 63 6b 2c 20 26 69  InitCallback, &i
1b101 6e 69 74 44 61 74 61 2c 20 30 29 3b 0a 20 20 20  nitData, 0);.   
1b102 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1b103 41 42 4f 52 54 20 29 20 72 63 20 3d 20 69 6e 69  ABORT ) rc = ini
1b104 74 44 61 74 61 2e 72 63 3b 0a 20 20 20 20 73 71  tData.rc;.    sq
1b105 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62  lite3SafetyOn(db
1b106 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 46 72 65  );.    sqliteFre
1b107 65 28 7a 53 71 6c 29 3b 0a 23 69 66 6e 64 65 66  e(zSql);.#ifndef
1b108 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41   SQLITE_OMIT_ANA
1b109 4c 59 5a 45 0a 20 20 20 20 69 66 28 20 72 63 3d  LYZE.    if( rc=
1b10a 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1b10b 20 20 20 20 73 71 6c 69 74 65 33 41 6e 61 6c 79      sqlite3Analy
1b10c 73 69 73 4c 6f 61 64 28 64 62 2c 20 69 44 62 29  sisLoad(db, iDb)
1b10d 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
1b10e 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43     sqlite3BtreeC
1b10f 6c 6f 73 65 43 75 72 73 6f 72 28 63 75 72 4d 61  loseCursor(curMa
1b110 69 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73  in);.  }.  if( s
1b111 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69 6c  qlite3MallocFail
1b112 65 64 28 29 20 29 7b 0a 20 20 20 20 2f 2a 20 73  ed() ){.    /* s
1b113 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
1b114 70 7a 45 72 72 4d 73 67 2c 20 22 6f 75 74 20 6f  pzErrMsg, "out o
1b115 66 20 6d 65 6d 6f 72 79 22 2c 20 28 63 68 61 72  f memory", (char
1b116 2a 29 30 29 3b 20 2a 2f 0a 20 20 20 20 72 63 20  *)0); */.    rc 
1b117 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
1b118 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 65 74      sqlite3Reset
1b119 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64  InternalSchema(d
1b11a 62 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28  b, 0);.  }.  if(
1b11b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc==SQLITE_OK |
1b11c 7c 20 28 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c  | (db->flags&SQL
1b11d 49 54 45 5f 52 65 63 6f 76 65 72 79 4d 6f 64 65  ITE_RecoveryMode
1b11e 29 29 7b 0a 20 20 20 20 2f 2a 20 42 6c 61 63 6b  )){.    /* Black
1b11f 20 6d 61 67 69 63 3a 20 49 66 20 74 68 65 20 53   magic: If the S
1b120 51 4c 49 54 45 5f 52 65 63 6f 76 65 72 79 4d 6f  QLITE_RecoveryMo
1b121 64 65 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20  de flag is set, 
1b122 74 68 65 6e 20 63 6f 6e 73 69 64 65 72 0a 20 20  then consider.  
1b123 20 20 2a 2a 20 74 68 65 20 73 63 68 65 6d 61 20    ** the schema 
1b124 6c 6f 61 64 65 64 2c 20 65 76 65 6e 20 69 66 20  loaded, even if 
1b125 65 72 72 6f 72 73 20 6f 63 63 75 72 65 64 2e 20  errors occured. 
1b126 49 6e 20 74 68 69 73 20 73 69 74 75 61 74 69 6f  In this situatio
1b127 6e 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 63 75  n the .    ** cu
1b128 72 72 65 6e 74 20 73 71 6c 69 74 65 33 5f 70 72  rrent sqlite3_pr
1b129 65 70 61 72 65 28 29 20 6f 70 65 72 61 74 69 6f  epare() operatio
1b12a 6e 20 77 69 6c 6c 20 66 61 69 6c 2c 20 62 75 74  n will fail, but
1b12b 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f   the following o
1b12c 6e 65 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 61  ne.    ** will a
1b12d 74 74 65 6d 70 74 20 74 6f 20 63 6f 6d 70 69 6c  ttempt to compil
1b12e 65 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 73  e the supplied s
1b12f 74 61 74 65 6d 65 6e 74 20 61 67 61 69 6e 73 74  tatement against
1b130 20 77 68 61 74 65 76 65 72 20 73 75 62 73 65 74   whatever subset
1b131 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 73  .    ** of the s
1b132 63 68 65 6d 61 20 77 61 73 20 6c 6f 61 64 65 64  chema was loaded
1b133 20 62 65 66 6f 72 65 20 74 68 65 20 65 72 72 6f   before the erro
1b134 72 20 6f 63 63 75 72 65 64 2e 20 54 68 65 20 70  r occured. The p
1b135 72 69 6d 61 72 79 0a 20 20 20 20 2a 2a 20 70 75  rimary.    ** pu
1b136 72 70 6f 73 65 20 6f 66 20 74 68 69 73 20 69 73  rpose of this is
1b137 20 74 6f 20 61 6c 6c 6f 77 20 61 63 63 65 73 73   to allow access
1b138 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 5f 6d   to the sqlite_m
1b139 61 73 74 65 72 20 74 61 62 6c 65 0a 20 20 20 20  aster table.    
1b13a 2a 2a 20 65 76 65 6e 20 77 68 65 6e 20 69 74 27  ** even when it'
1b13b 73 20 63 6f 6e 74 65 6e 74 73 20 68 61 76 65 20  s contents have 
1b13c 62 65 65 6e 20 63 6f 72 72 75 70 74 65 64 2e 0a  been corrupted..
1b13d 20 20 20 20 2a 2f 0a 20 20 20 20 44 62 53 65 74      */.    DbSet
1b13e 50 72 6f 70 65 72 74 79 28 64 62 2c 20 69 44 62  Property(db, iDb
1b13f 2c 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65  , DB_SchemaLoade
1b140 64 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  d);.    rc = SQL
1b141 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65  ITE_OK;.  }.  re
1b142 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
1b143 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6c 6c  * Initialize all
1b144 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20   database files 
1b145 2d 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  - the main datab
1b146 61 73 65 20 66 69 6c 65 2c 20 74 68 65 20 66 69  ase file, the fi
1b147 6c 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 73 74  le.** used to st
1b148 6f 72 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61  ore temporary ta
1b149 62 6c 65 73 2c 20 61 6e 64 20 61 6e 79 20 61 64  bles, and any ad
1b14a 64 69 74 69 6f 6e 61 6c 20 64 61 74 61 62 61 73  ditional databas
1b14b 65 20 66 69 6c 65 73 0a 2a 2a 20 63 72 65 61 74  e files.** creat
1b14c 65 64 20 75 73 69 6e 67 20 41 54 54 41 43 48 20  ed using ATTACH 
1b14d 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 52 65 74  statements.  Ret
1b14e 75 72 6e 20 61 20 73 75 63 63 65 73 73 20 63 6f  urn a success co
1b14f 64 65 2e 20 20 49 66 20 61 6e 0a 2a 2a 20 65 72  de.  If an.** er
1b150 72 6f 72 20 6f 63 63 75 72 73 2c 20 77 72 69 74  ror occurs, writ
1b151 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  e an error messa
1b152 67 65 20 69 6e 74 6f 20 2a 70 7a 45 72 72 4d 73  ge into *pzErrMs
1b153 67 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 61  g..**.** After a
1b154 20 64 61 74 61 62 61 73 65 20 69 73 20 69 6e 69   database is ini
1b155 74 69 61 6c 69 7a 65 64 2c 20 74 68 65 20 44 42  tialized, the DB
1b156 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 20 62 69  _SchemaLoaded bi
1b157 74 20 69 73 20 73 65 74 0a 2a 2a 20 62 69 74 20  t is set.** bit 
1b158 69 73 20 73 65 74 20 69 6e 20 74 68 65 20 66 6c  is set in the fl
1b159 61 67 73 20 66 69 65 6c 64 20 6f 66 20 74 68 65  ags field of the
1b15a 20 44 62 20 73 74 72 75 63 74 75 72 65 2e 20 49   Db structure. I
1b15b 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a  f the database.*
1b15c 2a 20 66 69 6c 65 20 77 61 73 20 6f 66 20 7a 65  * file was of ze
1b15d 72 6f 2d 6c 65 6e 67 74 68 2c 20 74 68 65 6e 20  ro-length, then 
1b15e 74 68 65 20 44 42 5f 45 6d 70 74 79 20 66 6c 61  the DB_Empty fla
1b15f 67 20 69 73 20 61 6c 73 6f 20 73 65 74 2e 0a 2a  g is also set..*
1b160 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1b161 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74   int sqlite3Init
1b162 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 68  (sqlite3 *db, ch
1b163 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 29 7b 0a  ar **pzErrMsg){.
1b164 20 20 69 6e 74 20 69 2c 20 72 63 3b 0a 20 20 69    int i, rc;.  i
1b165 6e 74 20 63 61 6c 6c 65 64 5f 69 6e 69 74 6f 6e  nt called_initon
1b166 65 20 3d 20 30 3b 0a 20 20 0a 20 20 69 66 28 20  e = 0;.  .  if( 
1b167 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 20  db->init.busy ) 
1b168 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1b169 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  ;.  rc = SQLITE_
1b16a 4f 4b 3b 0a 20 20 64 62 2d 3e 69 6e 69 74 2e 62  OK;.  db->init.b
1b16b 75 73 79 20 3d 20 31 3b 0a 20 20 66 6f 72 28 69  usy = 1;.  for(i
1b16c 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  =0; rc==SQLITE_O
1b16d 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  K && i<db->nDb; 
1b16e 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 44 62  i++){.    if( Db
1b16f 48 61 73 50 72 6f 70 65 72 74 79 28 64 62 2c 20  HasProperty(db, 
1b170 69 2c 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64  i, DB_SchemaLoad
1b171 65 64 29 20 7c 7c 20 69 3d 3d 31 20 29 20 63 6f  ed) || i==1 ) co
1b172 6e 74 69 6e 75 65 3b 0a 20 20 20 20 72 63 20 3d  ntinue;.    rc =
1b173 20 73 71 6c 69 74 65 33 49 6e 69 74 4f 6e 65 28   sqlite3InitOne(
1b174 64 62 2c 20 69 2c 20 70 7a 45 72 72 4d 73 67 29  db, i, pzErrMsg)
1b175 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a  ;.    if( rc ){.
1b176 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 73        sqlite3Res
1b177 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61  etInternalSchema
1b178 28 64 62 2c 20 69 29 3b 0a 20 20 20 20 7d 0a 20  (db, i);.    }. 
1b179 20 20 20 63 61 6c 6c 65 64 5f 69 6e 69 74 6f 6e     called_initon
1b17a 65 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  e = 1;.  }..  /*
1b17b 20 4f 6e 63 65 20 61 6c 6c 20 74 68 65 20 6f 74   Once all the ot
1b17c 68 65 72 20 64 61 74 61 62 61 73 65 73 20 68 61  her databases ha
1b17d 76 65 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69  ve been initiali
1b17e 73 65 64 2c 20 6c 6f 61 64 20 74 68 65 20 73 63  sed, load the sc
1b17f 68 65 6d 61 0a 20 20 2a 2a 20 66 6f 72 20 74 68  hema.  ** for th
1b180 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 2e  e TEMP database.
1b181 20 54 68 69 73 20 69 73 20 6c 6f 61 64 65 64 20   This is loaded 
1b182 6c 61 73 74 2c 20 61 73 20 74 68 65 20 54 45 4d  last, as the TEM
1b183 50 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20  P database.  ** 
1b184 73 63 68 65 6d 61 20 6d 61 79 20 63 6f 6e 74 61  schema may conta
1b185 69 6e 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f  in references to
1b186 20 6f 62 6a 65 63 74 73 20 69 6e 20 6f 74 68 65   objects in othe
1b187 72 20 64 61 74 61 62 61 73 65 73 2e 0a 20 20 2a  r databases..  *
1b188 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
1b189 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 20 20 69  _OMIT_TEMPDB.  i
1b18a 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1b18b 20 26 26 20 64 62 2d 3e 6e 44 62 3e 31 20 26 26   && db->nDb>1 &&
1b18c 20 21 44 62 48 61 73 50 72 6f 70 65 72 74 79 28   !DbHasProperty(
1b18d 64 62 2c 20 31 2c 20 44 42 5f 53 63 68 65 6d 61  db, 1, DB_Schema
1b18e 4c 6f 61 64 65 64 29 20 29 7b 0a 20 20 20 20 72  Loaded) ){.    r
1b18f 63 20 3d 20 73 71 6c 69 74 65 33 49 6e 69 74 4f  c = sqlite3InitO
1b190 6e 65 28 64 62 2c 20 31 2c 20 70 7a 45 72 72 4d  ne(db, 1, pzErrM
1b191 73 67 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20  sg);.    if( rc 
1b192 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1b193 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68  ResetInternalSch
1b194 65 6d 61 28 64 62 2c 20 31 29 3b 0a 20 20 20 20  ema(db, 1);.    
1b195 7d 0a 20 20 20 20 63 61 6c 6c 65 64 5f 69 6e 69  }.    called_ini
1b196 74 6f 6e 65 20 3d 20 31 3b 0a 20 20 7d 0a 23 65  tone = 1;.  }.#e
1b197 6e 64 69 66 0a 0a 20 20 64 62 2d 3e 69 6e 69 74  ndif..  db->init
1b198 2e 62 75 73 79 20 3d 20 30 3b 0a 20 20 69 66 28  .busy = 0;.  if(
1b199 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
1b19a 26 20 63 61 6c 6c 65 64 5f 69 6e 69 74 6f 6e 65  & called_initone
1b19b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43   ){.    sqlite3C
1b19c 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43 68 61  ommitInternalCha
1b19d 6e 67 65 73 28 64 62 29 3b 0a 20 20 7d 0a 0a 20  nges(db);.  }.. 
1b19e 20 72 65 74 75 72 6e 20 72 63 3b 20 0a 7d 0a 0a   return rc; .}..
1b19f 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
1b1a0 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66  ne is a no-op if
1b1a1 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
1b1a2 68 65 6d 61 20 69 73 20 61 6c 72 65 61 64 79 20  hema is already 
1b1a3 69 6e 69 74 69 61 6c 69 73 65 64 2e 0a 2a 2a 20  initialised..** 
1b1a4 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 73  Otherwise, the s
1b1a5 63 68 65 6d 61 20 69 73 20 6c 6f 61 64 65 64 2e  chema is loaded.
1b1a6 20 41 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69   An error code i
1b1a7 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53  s returned..*/.S
1b1a8 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1b1a9 74 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68  t sqlite3ReadSch
1b1aa 65 6d 61 28 50 61 72 73 65 20 2a 70 50 61 72 73  ema(Parse *pPars
1b1ab 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  e){.  int rc = S
1b1ac 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c 69  QLITE_OK;.  sqli
1b1ad 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
1b1ae 2d 3e 64 62 3b 0a 20 20 69 66 28 20 21 64 62 2d  ->db;.  if( !db-
1b1af 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20  >init.busy ){.  
1b1b0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 49 6e    rc = sqlite3In
1b1b1 69 74 28 64 62 2c 20 26 70 50 61 72 73 65 2d 3e  it(db, &pParse->
1b1b2 7a 45 72 72 4d 73 67 29 3b 0a 20 20 7d 0a 20 20  zErrMsg);.  }.  
1b1b3 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1b1b4 4b 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d  K ){.    pParse-
1b1b5 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 70 50  >rc = rc;.    pP
1b1b6 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20  arse->nErr++;.  
1b1b7 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
1b1b8 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 73  .../*.** Check s
1b1b9 63 68 65 6d 61 20 63 6f 6f 6b 69 65 73 20 69 6e  chema cookies in
1b1ba 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73 2e 20   all databases. 
1b1bb 20 49 66 20 61 6e 79 20 63 6f 6f 6b 69 65 20 69   If any cookie i
1b1bc 73 20 6f 75 74 0a 2a 2a 20 6f 66 20 64 61 74 65  s out.** of date
1b1bd 2c 20 72 65 74 75 72 6e 20 30 2e 20 20 49 66 20  , return 0.  If 
1b1be 61 6c 6c 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69  all schema cooki
1b1bf 65 73 20 61 72 65 20 63 75 72 72 65 6e 74 2c 20  es are current, 
1b1c0 72 65 74 75 72 6e 20 31 2e 0a 2a 2f 0a 73 74 61  return 1..*/.sta
1b1c1 74 69 63 20 69 6e 74 20 73 63 68 65 6d 61 49 73  tic int schemaIs
1b1c2 56 61 6c 69 64 28 73 71 6c 69 74 65 33 20 2a 64  Valid(sqlite3 *d
1b1c3 62 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20  b){.  int iDb;. 
1b1c4 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 43 75 72   int rc;.  BtCur
1b1c5 73 6f 72 20 2a 63 75 72 54 65 6d 70 3b 0a 20 20  sor *curTemp;.  
1b1c6 69 6e 74 20 63 6f 6f 6b 69 65 3b 0a 20 20 69 6e  int cookie;.  in
1b1c7 74 20 61 6c 6c 4f 6b 20 3d 20 31 3b 0a 0a 20 20  t allOk = 1;..  
1b1c8 66 6f 72 28 69 44 62 3d 30 3b 20 61 6c 6c 4f 6b  for(iDb=0; allOk
1b1c9 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 3b   && iDb<db->nDb;
1b1ca 20 69 44 62 2b 2b 29 7b 0a 20 20 20 20 42 74 72   iDb++){.    Btr
1b1cb 65 65 20 2a 70 42 74 3b 0a 20 20 20 20 70 42 74  ee *pBt;.    pBt
1b1cc 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e   = db->aDb[iDb].
1b1cd 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70 42 74  pBt;.    if( pBt
1b1ce 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ==0 ) continue;.
1b1cf 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1b1d0 42 74 72 65 65 43 75 72 73 6f 72 28 70 42 74 2c  BtreeCursor(pBt,
1b1d1 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 30 2c   MASTER_ROOT, 0,
1b1d2 20 30 2c 20 30 2c 20 26 63 75 72 54 65 6d 70 29   0, 0, &curTemp)
1b1d3 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
1b1d4 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1b1d5 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1b1d6 65 65 47 65 74 4d 65 74 61 28 70 42 74 2c 20 31  eeGetMeta(pBt, 1
1b1d7 2c 20 28 75 33 32 20 2a 29 26 63 6f 6f 6b 69 65  , (u32 *)&cookie
1b1d8 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
1b1d9 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 63 6f  =SQLITE_OK && co
1b1da 6f 6b 69 65 21 3d 64 62 2d 3e 61 44 62 5b 69 44  okie!=db->aDb[iD
1b1db 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65  b].pSchema->sche
1b1dc 6d 61 5f 63 6f 6f 6b 69 65 20 29 7b 0a 20 20 20  ma_cookie ){.   
1b1dd 20 20 20 20 20 61 6c 6c 4f 6b 20 3d 20 30 3b 0a       allOk = 0;.
1b1de 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
1b1df 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43  lite3BtreeCloseC
1b1e0 75 72 73 6f 72 28 63 75 72 54 65 6d 70 29 3b 0a  ursor(curTemp);.
1b1e1 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
1b1e2 72 6e 20 61 6c 6c 4f 6b 3b 0a 7d 0a 0a 2f 2a 0a  rn allOk;.}../*.
1b1e3 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 73 63 68  ** Convert a sch
1b1e4 65 6d 61 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f  ema pointer into
1b1e5 20 74 68 65 20 69 44 62 20 69 6e 64 65 78 20 74   the iDb index t
1b1e6 68 61 74 20 69 6e 64 69 63 61 74 65 73 0a 2a 2a  hat indicates.**
1b1e7 20 77 68 69 63 68 20 64 61 74 61 62 61 73 65 20   which database 
1b1e8 66 69 6c 65 20 69 6e 20 64 62 2d 3e 61 44 62 5b  file in db->aDb[
1b1e9 5d 20 74 68 65 20 73 63 68 65 6d 61 20 72 65 66  ] the schema ref
1b1ea 65 72 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66  ers to..**.** If
1b1eb 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61   the same databa
1b1ec 73 65 20 69 73 20 61 74 74 61 63 68 65 64 20 6d  se is attached m
1b1ed 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74  ore than once, t
1b1ee 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 74 74 61  he first.** atta
1b1ef 63 68 65 64 20 64 61 74 61 62 61 73 65 20 69 73  ched database is
1b1f0 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51   returned..*/.SQ
1b1f1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1b1f2 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
1b1f3 49 6e 64 65 78 28 73 71 6c 69 74 65 33 20 2a 64  Index(sqlite3 *d
1b1f4 62 2c 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65  b, Schema *pSche
1b1f5 6d 61 29 7b 0a 20 20 69 6e 74 20 69 20 3d 20 2d  ma){.  int i = -
1b1f6 31 30 30 30 30 30 30 3b 0a 0a 20 20 2f 2a 20 49  1000000;..  /* I
1b1f7 66 20 70 53 63 68 65 6d 61 20 69 73 20 4e 55 4c  f pSchema is NUL
1b1f8 4c 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20 2d  L, then return -
1b1f9 31 30 30 30 30 30 30 2e 20 54 68 69 73 20 68 61  1000000. This ha
1b1fa 70 70 65 6e 73 20 77 68 65 6e 20 63 6f 64 65 20  ppens when code 
1b1fb 69 6e 20 0a 20 20 2a 2a 20 65 78 70 72 2e 63 20  in .  ** expr.c 
1b1fc 69 73 20 74 72 79 69 6e 67 20 74 6f 20 72 65 73  is trying to res
1b1fd 6f 6c 76 65 20 61 20 72 65 66 65 72 65 6e 63 65  olve a reference
1b1fe 20 74 6f 20 61 20 74 72 61 6e 73 69 65 6e 74 20   to a transient 
1b1ff 74 61 62 6c 65 20 28 69 2e 65 2e 20 6f 6e 65 0a  table (i.e. one.
1b200 20 20 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20    ** created by 
1b201 61 20 73 75 62 2d 73 65 6c 65 63 74 29 2e 20 49  a sub-select). I
1b202 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20  n this case the 
1b203 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20  return value of 
1b204 74 68 69 73 20 0a 20 20 2a 2a 20 66 75 6e 63 74  this .  ** funct
1b205 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65 76 65 72  ion should never
1b206 20 62 65 20 75 73 65 64 2e 0a 20 20 2a 2a 0a 20   be used..  **. 
1b207 20 2a 2a 20 57 65 20 72 65 74 75 72 6e 20 2d 31   ** We return -1
1b208 30 30 30 30 30 30 20 69 6e 73 74 65 61 64 20 6f  000000 instead o
1b209 66 20 74 68 65 20 6d 6f 72 65 20 75 73 75 61 6c  f the more usual
1b20a 20 2d 31 20 73 69 6d 70 6c 79 20 62 65 63 61 75   -1 simply becau
1b20b 73 65 20 75 73 69 6e 67 0a 20 20 2a 2a 20 2d 31  se using.  ** -1
1b20c 30 30 30 30 30 30 20 61 73 20 69 6e 63 6f 72 72  000000 as incorr
1b20d 65 63 74 6c 79 20 75 73 69 6e 67 20 2d 31 30 30  ectly using -100
1b20e 30 30 30 30 20 69 6e 64 65 78 20 69 6e 74 6f 20  0000 index into 
1b20f 64 62 2d 3e 61 44 62 5b 5d 20 69 73 20 6d 75 63  db->aDb[] is muc
1b210 68 20 0a 20 20 2a 2a 20 6d 6f 72 65 20 6c 69 6b  h .  ** more lik
1b211 65 6c 79 20 74 6f 20 63 61 75 73 65 20 61 20 73  ely to cause a s
1b212 65 67 66 61 75 6c 74 20 74 68 61 6e 20 2d 31 20  egfault than -1 
1b213 28 6f 66 20 63 6f 75 72 73 65 20 74 68 65 72 65  (of course there
1b214 20 61 72 65 20 61 73 73 65 72 74 28 29 0a 20 20   are assert().  
1b215 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f  ** statements to
1b216 6f 2c 20 62 75 74 20 69 74 20 6e 65 76 65 72 20  o, but it never 
1b217 68 75 72 74 73 20 74 6f 20 70 6c 61 79 20 74 68  hurts to play th
1b218 65 20 6f 64 64 73 29 2e 0a 20 20 2a 2f 0a 20 20  e odds)..  */.  
1b219 69 66 28 20 70 53 63 68 65 6d 61 20 29 7b 0a 20  if( pSchema ){. 
1b21a 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62     for(i=0; i<db
1b21b 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nDb; i++){.   
1b21c 20 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 69     if( db->aDb[i
1b21d 5d 2e 70 53 63 68 65 6d 61 3d 3d 70 53 63 68 65  ].pSchema==pSche
1b21e 6d 61 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72  ma ){.        br
1b21f 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
1b220 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 69   }.    assert( i
1b221 3e 3d 30 20 26 26 69 3e 3d 30 20 26 26 20 20 69  >=0 &&i>=0 &&  i
1b222 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 7d 0a  <db->nDb );.  }.
1b223 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a 2f    return i;.}../
1b224 2a 0a 2a 2a 20 43 6f 6d 70 69 6c 65 20 74 68 65  *.** Compile the
1b225 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 53   UTF-8 encoded S
1b226 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 7a 53 71  QL statement zSq
1b227 6c 20 69 6e 74 6f 20 61 20 73 74 61 74 65 6d 65  l into a stateme
1b228 6e 74 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51  nt handle..*/.SQ
1b229 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1b22a 20 73 71 6c 69 74 65 33 50 72 65 70 61 72 65 28   sqlite3Prepare(
1b22b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
1b22c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1b22d 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e  Database handle.
1b22e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
1b22f 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 20   *zSql,         
1b230 2f 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64  /* UTF-8 encoded
1b231 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20   SQL statement. 
1b232 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 2c  */.  int nBytes,
1b233 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1b234 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c  * Length of zSql
1b235 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20   in bytes. */.  
1b236 69 6e 74 20 73 61 76 65 53 71 6c 46 6c 61 67 2c  int saveSqlFlag,
1b237 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
1b238 65 20 74 6f 20 63 6f 70 79 20 53 51 4c 20 74 65  e to copy SQL te
1b239 78 74 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69  xt into the sqli
1b23a 74 65 33 5f 73 74 6d 74 20 2a 2f 0a 20 20 73 71  te3_stmt */.  sq
1b23b 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53  lite3_stmt **ppS
1b23c 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20  tmt,    /* OUT: 
1b23d 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  A pointer to the
1b23e 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
1b23f 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ent */.  const c
1b240 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20  har **pzTail    
1b241 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f     /* OUT: End o
1b242 66 20 70 61 72 73 65 64 20 73 74 72 69 6e 67 20  f parsed string 
1b243 2a 2f 0a 29 7b 0a 20 20 50 61 72 73 65 20 73 50  */.){.  Parse sP
1b244 61 72 73 65 3b 0a 20 20 63 68 61 72 20 2a 7a 45  arse;.  char *zE
1b245 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 69 6e 74  rrMsg = 0;.  int
1b246 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
1b247 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 2f 2a 20  .  int i;..  /* 
1b248 41 73 73 65 72 74 20 74 68 61 74 20 6d 61 6c 6c  Assert that mall
1b249 6f 63 28 29 20 68 61 73 20 6e 6f 74 20 66 61 69  oc() has not fai
1b24a 6c 65 64 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  led */.  assert(
1b24b 20 21 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 46   !sqlite3MallocF
1b24c 61 69 6c 65 64 28 29 20 29 3b 0a 0a 20 20 61 73  ailed() );..  as
1b24d 73 65 72 74 28 20 70 70 53 74 6d 74 20 29 3b 0a  sert( ppStmt );.
1b24e 20 20 2a 70 70 53 74 6d 74 20 3d 20 30 3b 0a 20    *ppStmt = 0;. 
1b24f 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65   if( sqlite3Safe
1b250 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20  tyOn(db) ){.    
1b251 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
1b252 53 55 53 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  SUSE;.  }..  /* 
1b253 49 66 20 61 6e 79 20 61 74 74 61 63 68 65 64 20  If any attached 
1b254 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 73  database schemas
1b255 20 61 72 65 20 6c 6f 63 6b 65 64 2c 20 64 6f 20   are locked, do 
1b256 6e 6f 74 20 70 72 6f 63 65 65 64 20 77 69 74 68  not proceed with
1b257 0a 20 20 2a 2a 20 63 6f 6d 70 69 6c 61 74 69 6f  .  ** compilatio
1b258 6e 2e 20 49 6e 73 74 65 61 64 20 72 65 74 75 72  n. Instead retur
1b259 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20  n SQLITE_LOCKED 
1b25a 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 20 20 2a  immediately..  *
1b25b 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64  /.  for(i=0; i<d
1b25c 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 20 7b 0a 20  b->nDb; i++) {. 
1b25d 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20     Btree *pBt = 
1b25e 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a  db->aDb[i].pBt;.
1b25f 20 20 20 20 69 66 28 20 70 42 74 20 26 26 20 73      if( pBt && s
1b260 71 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 6d  qlite3BtreeSchem
1b261 61 4c 6f 63 6b 65 64 28 70 42 74 29 20 29 7b 0a  aLocked(pBt) ){.
1b262 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
1b263 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b   *zDb = db->aDb[
1b264 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20  i].zName;.      
1b265 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
1b266 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2c 20   SQLITE_LOCKED, 
1b267 22 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61  "database schema
1b268 20 69 73 20 6c 6f 63 6b 65 64 3a 20 25 73 22 2c   is locked: %s",
1b269 20 7a 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c   zDb);.      sql
1b26a 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62  ite3SafetyOff(db
1b26b 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
1b26c 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20  SQLITE_LOCKED;. 
1b26d 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 6d 65     }.  }.  .  me
1b26e 6d 73 65 74 28 26 73 50 61 72 73 65 2c 20 30 2c  mset(&sParse, 0,
1b26f 20 73 69 7a 65 6f 66 28 73 50 61 72 73 65 29 29   sizeof(sParse))
1b270 3b 0a 20 20 73 50 61 72 73 65 2e 64 62 20 3d 20  ;.  sParse.db = 
1b271 64 62 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73  db;.  if( nBytes
1b272 3e 3d 30 20 26 26 20 7a 53 71 6c 5b 6e 42 79 74  >=0 && zSql[nByt
1b273 65 73 5d 21 3d 30 20 29 7b 0a 20 20 20 20 63 68  es]!=0 ){.    ch
1b274 61 72 20 2a 7a 53 71 6c 43 6f 70 79 3b 0a 20 20  ar *zSqlCopy;.  
1b275 20 20 69 66 28 20 6e 42 79 74 65 73 3e 53 51 4c    if( nBytes>SQL
1b276 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47  ITE_MAX_SQL_LENG
1b277 54 48 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  TH ){.      retu
1b278 72 6e 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47  rn SQLITE_TOOBIG
1b279 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 53 71 6c  ;.    }.    zSql
1b27a 43 6f 70 79 20 3d 20 73 71 6c 69 74 65 33 53 74  Copy = sqlite3St
1b27b 72 4e 44 75 70 28 7a 53 71 6c 2c 20 6e 42 79 74  rNDup(zSql, nByt
1b27c 65 73 29 3b 0a 20 20 20 20 69 66 28 20 7a 53 71  es);.    if( zSq
1b27d 6c 43 6f 70 79 20 29 7b 0a 20 20 20 20 20 20 73  lCopy ){.      s
1b27e 71 6c 69 74 65 33 52 75 6e 50 61 72 73 65 72 28  qlite3RunParser(
1b27f 26 73 50 61 72 73 65 2c 20 7a 53 71 6c 43 6f 70  &sParse, zSqlCop
1b280 79 2c 20 26 7a 45 72 72 4d 73 67 29 3b 0a 20 20  y, &zErrMsg);.  
1b281 20 20 20 20 73 71 6c 69 74 65 46 72 65 65 28 7a      sqliteFree(z
1b282 53 71 6c 43 6f 70 79 29 3b 0a 20 20 20 20 7d 0a  SqlCopy);.    }.
1b283 20 20 20 20 73 50 61 72 73 65 2e 7a 54 61 69 6c      sParse.zTail
1b284 20 3d 20 26 7a 53 71 6c 5b 6e 42 79 74 65 73 5d   = &zSql[nBytes]
1b285 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
1b286 71 6c 69 74 65 33 52 75 6e 50 61 72 73 65 72 28  qlite3RunParser(
1b287 26 73 50 61 72 73 65 2c 20 7a 53 71 6c 2c 20 26  &sParse, zSql, &
1b288 7a 45 72 72 4d 73 67 29 3b 0a 20 20 7d 0a 0a 20  zErrMsg);.  }.. 
1b289 20 69 66 28 20 73 71 6c 69 74 65 33 4d 61 6c 6c   if( sqlite3Mall
1b28a 6f 63 46 61 69 6c 65 64 28 29 20 29 7b 0a 20 20  ocFailed() ){.  
1b28b 20 20 73 50 61 72 73 65 2e 72 63 20 3d 20 53 51    sParse.rc = SQ
1b28c 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
1b28d 20 20 69 66 28 20 73 50 61 72 73 65 2e 72 63 3d    if( sParse.rc=
1b28e 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20 73  =SQLITE_DONE ) s
1b28f 50 61 72 73 65 2e 72 63 20 3d 20 53 51 4c 49 54  Parse.rc = SQLIT
1b290 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 73 50 61 72  E_OK;.  if( sPar
1b291 73 65 2e 63 68 65 63 6b 53 63 68 65 6d 61 20 26  se.checkSchema &
1b292 26 20 21 73 63 68 65 6d 61 49 73 56 61 6c 69 64  & !schemaIsValid
1b293 28 64 62 29 20 29 7b 0a 20 20 20 20 73 50 61 72  (db) ){.    sPar
1b294 73 65 2e 72 63 20 3d 20 53 51 4c 49 54 45 5f 53  se.rc = SQLITE_S
1b295 43 48 45 4d 41 3b 0a 20 20 7d 0a 20 20 69 66 28  CHEMA;.  }.  if(
1b296 20 73 50 61 72 73 65 2e 72 63 3d 3d 53 51 4c 49   sParse.rc==SQLI
1b297 54 45 5f 53 43 48 45 4d 41 20 29 7b 0a 20 20 20  TE_SCHEMA ){.   
1b298 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74   sqlite3ResetInt
1b299 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20  ernalSchema(db, 
1b29a 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71  0);.  }.  if( sq
1b29b 6c 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69 6c 65  lite3MallocFaile
1b29c 64 28 29 20 29 7b 0a 20 20 20 20 73 50 61 72 73  d() ){.    sPars
1b29d 65 2e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f  e.rc = SQLITE_NO
1b29e 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  MEM;.  }.  if( p
1b29f 7a 54 61 69 6c 20 29 7b 0a 20 20 20 20 2a 70 7a  zTail ){.    *pz
1b2a0 54 61 69 6c 20 3d 20 73 50 61 72 73 65 2e 7a 54  Tail = sParse.zT
1b2a1 61 69 6c 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20  ail;.  }.  rc = 
1b2a2 73 50 61 72 73 65 2e 72 63 3b 0a 0a 23 69 66 6e  sParse.rc;..#ifn
1b2a3 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1b2a4 45 58 50 4c 41 49 4e 0a 20 20 69 66 28 20 72 63  EXPLAIN.  if( rc
1b2a5 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 73  ==SQLITE_OK && s
1b2a6 50 61 72 73 65 2e 70 56 64 62 65 20 26 26 20 73  Parse.pVdbe && s
1b2a7 50 61 72 73 65 2e 65 78 70 6c 61 69 6e 20 29 7b  Parse.explain ){
1b2a8 0a 20 20 20 20 69 66 28 20 73 50 61 72 73 65 2e  .    if( sParse.
1b2a9 65 78 70 6c 61 69 6e 3d 3d 32 20 29 7b 0a 20 20  explain==2 ){.  
1b2aa 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
1b2ab 65 74 4e 75 6d 43 6f 6c 73 28 73 50 61 72 73 65  etNumCols(sParse
1b2ac 2e 70 56 64 62 65 2c 20 33 29 3b 0a 20 20 20 20  .pVdbe, 3);.    
1b2ad 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
1b2ae 43 6f 6c 4e 61 6d 65 28 73 50 61 72 73 65 2e 70  ColName(sParse.p
1b2af 56 64 62 65 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45  Vdbe, 0, COLNAME
1b2b0 5f 4e 41 4d 45 2c 20 22 6f 72 64 65 72 22 2c 20  _NAME, "order", 
1b2b1 50 33 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  P3_STATIC);.    
1b2b2 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
1b2b3 43 6f 6c 4e 61 6d 65 28 73 50 61 72 73 65 2e 70  ColName(sParse.p
1b2b4 56 64 62 65 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45  Vdbe, 1, COLNAME
1b2b5 5f 4e 41 4d 45 2c 20 22 66 72 6f 6d 22 2c 20 50  _NAME, "from", P
1b2b6 33 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  3_STATIC);.     
1b2b7 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
1b2b8 6f 6c 4e 61 6d 65 28 73 50 61 72 73 65 2e 70 56  olName(sParse.pV
1b2b9 64 62 65 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f  dbe, 2, COLNAME_
1b2ba 4e 41 4d 45 2c 20 22 64 65 74 61 69 6c 22 2c 20  NAME, "detail", 
1b2bb 50 33 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  P3_STATIC);.    
1b2bc 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
1b2bd 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f  ite3VdbeSetNumCo
1b2be 6c 73 28 73 50 61 72 73 65 2e 70 56 64 62 65 2c  ls(sParse.pVdbe,
1b2bf 20 35 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   5);.      sqlit
1b2c0 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
1b2c1 28 73 50 61 72 73 65 2e 70 56 64 62 65 2c 20 30  (sParse.pVdbe, 0
1b2c2 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
1b2c3 22 61 64 64 72 22 2c 20 50 33 5f 53 54 41 54 49  "addr", P3_STATI
1b2c4 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  C);.      sqlite
1b2c5 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
1b2c6 73 50 61 72 73 65 2e 70 56 64 62 65 2c 20 31 2c  sParse.pVdbe, 1,
1b2c7 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
1b2c8 6f 70 63 6f 64 65 22 2c 20 50 33 5f 53 54 41 54  opcode", P3_STAT
1b2c9 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  IC);.      sqlit
1b2ca 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
1b2cb 28 73 50 61 72 73 65 2e 70 56 64 62 65 2c 20 32  (sParse.pVdbe, 2
1b2cc 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
1b2cd 22 70 31 22 2c 20 50 33 5f 53 54 41 54 49 43 29  "p1", P3_STATIC)
1b2ce 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1b2cf 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 73 50  dbeSetColName(sP
1b2d0 61 72 73 65 2e 70 56 64 62 65 2c 20 33 2c 20 43  arse.pVdbe, 3, C
1b2d1 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 70 32  OLNAME_NAME, "p2
1b2d2 22 2c 20 50 33 5f 53 54 41 54 49 43 29 3b 0a 20  ", P3_STATIC);. 
1b2d3 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1b2d4 53 65 74 43 6f 6c 4e 61 6d 65 28 73 50 61 72 73  SetColName(sPars
1b2d5 65 2e 70 56 64 62 65 2c 20 34 2c 20 43 4f 4c 4e  e.pVdbe, 4, COLN
1b2d6 41 4d 45 5f 4e 41 4d 45 2c 20 22 70 33 22 2c 20  AME_NAME, "p3", 
1b2d7 50 33 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  P3_STATIC);.    
1b2d8 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  }.  }.#endif..  
1b2d9 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74  if( sqlite3Safet
1b2da 79 4f 66 66 28 64 62 29 20 29 7b 0a 20 20 20 20  yOff(db) ){.    
1b2db 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55  rc = SQLITE_MISU
1b2dc 53 45 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73  SE;.  }..  if( s
1b2dd 61 76 65 53 71 6c 46 6c 61 67 20 29 7b 0a 20 20  aveSqlFlag ){.  
1b2de 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
1b2df 53 71 6c 28 73 50 61 72 73 65 2e 70 56 64 62 65  Sql(sParse.pVdbe
1b2e0 2c 20 7a 53 71 6c 2c 20 73 50 61 72 73 65 2e 7a  , zSql, sParse.z
1b2e1 54 61 69 6c 20 2d 20 7a 53 71 6c 29 3b 0a 20 20  Tail - zSql);.  
1b2e2 7d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  }.  if( rc!=SQLI
1b2e3 54 45 5f 4f 4b 20 7c 7c 20 73 71 6c 69 74 65 33  TE_OK || sqlite3
1b2e4 4d 61 6c 6c 6f 63 46 61 69 6c 65 64 28 29 20 29  MallocFailed() )
1b2e5 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69  {.    sqlite3_fi
1b2e6 6e 61 6c 69 7a 65 28 28 73 71 6c 69 74 65 33 5f  nalize((sqlite3_
1b2e7 73 74 6d 74 2a 29 73 50 61 72 73 65 2e 70 56 64  stmt*)sParse.pVd
1b2e8 62 65 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  be);.    assert(
1b2e9 21 28 2a 70 70 53 74 6d 74 29 29 3b 0a 20 20 7d  !(*ppStmt));.  }
1b2ea 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 53 74 6d  else{.    *ppStm
1b2eb 74 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 6d  t = (sqlite3_stm
1b2ec 74 2a 29 73 50 61 72 73 65 2e 70 56 64 62 65 3b  t*)sParse.pVdbe;
1b2ed 0a 20 20 7d 0a 0a 20 20 69 66 28 20 7a 45 72 72  .  }..  if( zErr
1b2ee 4d 73 67 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Msg ){.    sqlit
1b2ef 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20  e3Error(db, rc, 
1b2f0 22 25 73 22 2c 20 7a 45 72 72 4d 73 67 29 3b 0a  "%s", zErrMsg);.
1b2f1 20 20 20 20 73 71 6c 69 74 65 46 72 65 65 28 7a      sqliteFree(z
1b2f2 45 72 72 4d 73 67 29 3b 0a 20 20 7d 65 6c 73 65  ErrMsg);.  }else
1b2f3 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
1b2f4 6f 72 28 64 62 2c 20 72 63 2c 20 30 29 3b 0a 20  or(db, rc, 0);. 
1b2f5 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   }..  rc = sqlit
1b2f6 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63  e3ApiExit(db, rc
1b2f7 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65  );.  sqlite3Rele
1b2f8 61 73 65 54 68 72 65 61 64 44 61 74 61 28 29 3b  aseThreadData();
1b2f9 0a 20 20 61 73 73 65 72 74 28 20 28 72 63 26 64  .  assert( (rc&d
1b2fa 62 2d 3e 65 72 72 4d 61 73 6b 29 3d 3d 72 63 20  b->errMask)==rc 
1b2fb 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
1b2fc 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 72 75 6e 20 74  }../*.** Rerun t
1b2fd 68 65 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 6f  he compilation o
1b2fe 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20 61 66  f a statement af
1b2ff 74 65 72 20 61 20 73 63 68 65 6d 61 20 63 68 61  ter a schema cha
1b300 6e 67 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74  nge..** Return t
1b301 72 75 65 20 69 66 20 74 68 65 20 73 74 61 74 65  rue if the state
1b302 6d 65 6e 74 20 77 61 73 20 72 65 63 6f 6d 70 69  ment was recompi
1b303 6c 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79  led successfully
1b304 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 66 61 6c 73  ..** Return fals
1b305 65 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e  e if there is an
1b306 20 65 72 72 6f 72 20 6f 66 20 73 6f 6d 65 20 6b   error of some k
1b307 69 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ind..*/.SQLITE_P
1b308 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1b309 65 33 52 65 70 72 65 70 61 72 65 28 56 64 62 65  e3Reprepare(Vdbe
1b30a 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a   *p){.  int rc;.
1b30b 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
1b30c 70 4e 65 77 3b 0a 20 20 63 6f 6e 73 74 20 63 68  pNew;.  const ch
1b30d 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 73 71 6c 69  ar *zSql;.  sqli
1b30e 74 65 33 20 2a 64 62 3b 0a 20 20 0a 20 20 7a 53  te3 *db;.  .  zS
1b30f 71 6c 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  ql = sqlite3Vdbe
1b310 47 65 74 53 71 6c 28 70 29 3b 0a 20 20 69 66 28  GetSql(p);.  if(
1b311 20 7a 53 71 6c 3d 3d 30 20 29 7b 0a 20 20 20 20   zSql==0 ){.    
1b312 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
1b313 64 62 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  db = sqlite3Vdbe
1b314 44 62 28 70 29 3b 0a 20 20 72 63 20 3d 20 73 71  Db(p);.  rc = sq
1b315 6c 69 74 65 33 50 72 65 70 61 72 65 28 64 62 2c  lite3Prepare(db,
1b316 20 7a 53 71 6c 2c 20 2d 31 2c 20 30 2c 20 26 70   zSql, -1, 0, &p
1b317 4e 65 77 2c 20 30 29 3b 0a 20 20 69 66 28 20 72  New, 0);.  if( r
1b318 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  c ){.    assert(
1b319 20 70 4e 65 77 3d 3d 30 20 29 3b 0a 20 20 20 20   pNew==0 );.    
1b31a 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73  return 0;.  }els
1b31b 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  e{.    assert( p
1b31c 4e 65 77 21 3d 30 20 29 3b 0a 20 20 7d 0a 20 20  New!=0 );.  }.  
1b31d 73 71 6c 69 74 65 33 56 64 62 65 53 77 61 70 28  sqlite3VdbeSwap(
1b31e 28 56 64 62 65 2a 29 70 4e 65 77 2c 20 70 29 3b  (Vdbe*)pNew, p);
1b31f 0a 20 20 73 71 6c 69 74 65 33 5f 74 72 61 6e 73  .  sqlite3_trans
1b320 66 65 72 5f 62 69 6e 64 69 6e 67 73 28 70 4e 65  fer_bindings(pNe
1b321 77 2c 20 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  w, (sqlite3_stmt
1b322 2a 29 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  *)p);.  sqlite3V
1b323 64 62 65 52 65 73 65 74 53 74 65 70 52 65 73 75  dbeResetStepResu
1b324 6c 74 28 28 56 64 62 65 2a 29 70 4e 65 77 29 3b  lt((Vdbe*)pNew);
1b325 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 69  .  sqlite3VdbeFi
1b326 6e 61 6c 69 7a 65 28 28 56 64 62 65 2a 29 70 4e  nalize((Vdbe*)pN
1b327 65 77 29 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b  ew);.  return 1;
1b328 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 77 6f 20 76  .}.../*.** Two v
1b329 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 6f  ersions of the o
1b32a 66 66 69 63 69 61 6c 20 41 50 49 2e 20 20 4c 65  fficial API.  Le
1b32b 67 61 63 79 20 61 6e 64 20 6e 65 77 20 75 73 65  gacy and new use
1b32c 2e 20 20 49 6e 20 74 68 65 20 6c 65 67 61 63 79  .  In the legacy
1b32d 0a 2a 2a 20 76 65 72 73 69 6f 6e 2c 20 74 68 65  .** version, the
1b32e 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 74 65   original SQL te
1b32f 78 74 20 69 73 20 6e 6f 74 20 73 61 76 65 64 20  xt is not saved 
1b330 69 6e 20 74 68 65 20 70 72 65 70 61 72 65 64 20  in the prepared 
1b331 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 61 6e 64  statement.** and
1b332 20 73 6f 20 69 66 20 61 20 73 63 68 65 6d 61 20   so if a schema 
1b333 63 68 61 6e 67 65 20 6f 63 63 75 72 73 2c 20 53  change occurs, S
1b334 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 69 73 20  QLITE_SCHEMA is 
1b335 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73  returned by.** s
1b336 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20 20  qlite3_step().  
1b337 49 6e 20 74 68 65 20 6e 65 77 20 76 65 72 73 69  In the new versi
1b338 6f 6e 2c 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  on, the original
1b339 20 53 51 4c 20 74 65 78 74 20 69 73 20 72 65 74   SQL text is ret
1b33a 61 69 6e 65 64 0a 2a 2a 20 61 6e 64 20 74 68 65  ained.** and the
1b33b 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 75   statement is au
1b33c 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 65 63 6f  tomatically reco
1b33d 6d 70 69 6c 65 64 20 69 66 20 61 6e 20 73 63 68  mpiled if an sch
1b33e 65 6d 61 20 63 68 61 6e 67 65 0a 2a 2a 20 6f 63  ema change.** oc
1b33f 63 75 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  curs..*/.SQLITE_
1b340 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1b341 70 72 65 70 61 72 65 28 0a 20 20 73 71 6c 69 74  prepare(.  sqlit
1b342 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
1b343 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
1b344 20 68 61 6e 64 6c 65 2e 20 2a 2f 0a 20 20 63 6f   handle. */.  co
1b345 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20  nst char *zSql, 
1b346 20 20 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38          /* UTF-8
1b347 20 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61   encoded SQL sta
1b348 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74  tement. */.  int
1b349 20 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20 20   nBytes,        
1b34a 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68         /* Length
1b34b 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65   of zSql in byte
1b34c 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  s. */.  sqlite3_
1b34d 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20  stmt **ppStmt,  
1b34e 20 20 2f 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e    /* OUT: A poin
1b34f 74 65 72 20 74 6f 20 74 68 65 20 70 72 65 70 61  ter to the prepa
1b350 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  red statement */
1b351 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a  .  const char **
1b352 70 7a 54 61 69 6c 20 20 20 20 20 20 20 2f 2a 20  pzTail       /* 
1b353 4f 55 54 3a 20 45 6e 64 20 6f 66 20 70 61 72 73  OUT: End of pars
1b354 65 64 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a  ed string */.){.
1b355 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
1b356 50 72 65 70 61 72 65 28 64 62 2c 7a 53 71 6c 2c  Prepare(db,zSql,
1b357 6e 42 79 74 65 73 2c 30 2c 70 70 53 74 6d 74 2c  nBytes,0,ppStmt,
1b358 70 7a 54 61 69 6c 29 3b 0a 7d 0a 69 6e 74 20 73  pzTail);.}.int s
1b359 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
1b35a 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  2(.  sqlite3 *db
1b35b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
1b35c 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c  * Database handl
1b35d 65 2e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  e. */.  const ch
1b35e 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20  ar *zSql,       
1b35f 20 20 2f 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64    /* UTF-8 encod
1b360 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ed SQL statement
1b361 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65  . */.  int nByte
1b362 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s,              
1b363 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53   /* Length of zS
1b364 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a  ql in bytes. */.
1b365 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
1b366 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a 20 4f  *ppStmt,    /* O
1b367 55 54 3a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f  UT: A pointer to
1b368 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73 74   the prepared st
1b369 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e  atement */.  con
1b36a 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c  st char **pzTail
1b36b 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45         /* OUT: E
1b36c 6e 64 20 6f 66 20 70 61 72 73 65 64 20 73 74 72  nd of parsed str
1b36d 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 72 65 74 75  ing */.){.  retu
1b36e 72 6e 20 73 71 6c 69 74 65 33 50 72 65 70 61 72  rn sqlite3Prepar
1b36f 65 28 64 62 2c 7a 53 71 6c 2c 6e 42 79 74 65 73  e(db,zSql,nBytes
1b370 2c 31 2c 70 70 53 74 6d 74 2c 70 7a 54 61 69 6c  ,1,ppStmt,pzTail
1b371 29 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53  );.}...#ifndef S
1b372 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
1b373 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 69 6c 65 20 74  ./*.** Compile t
1b374 68 65 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65  he UTF-16 encode
1b375 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  d SQL statement 
1b376 7a 53 71 6c 20 69 6e 74 6f 20 61 20 73 74 61 74  zSql into a stat
1b377 65 6d 65 6e 74 20 68 61 6e 64 6c 65 2e 0a 2a 2f  ement handle..*/
1b378 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69  .static int sqli
1b379 74 65 33 50 72 65 70 61 72 65 31 36 28 0a 20 20  te3Prepare16(.  
1b37a 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
1b37b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
1b37c 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f  abase handle. */
1b37d 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a   .  const void *
1b37e 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a  zSql,         /*
1b37f 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 53   UTF-8 encoded S
1b380 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f  QL statement. */
1b381 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20  .  int nBytes,  
1b382 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1b383 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69  Length of zSql i
1b384 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 69 6e  n bytes. */.  in
1b385 74 20 73 61 76 65 53 71 6c 46 6c 61 67 2c 20 20  t saveSqlFlag,  
1b386 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
1b387 74 6f 20 73 61 76 65 20 53 51 4c 20 74 65 78 74  to save SQL text
1b388 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65   into the sqlite
1b389 33 5f 73 74 6d 74 20 2a 2f 0a 20 20 73 71 6c 69  3_stmt */.  sqli
1b38a 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d  te3_stmt **ppStm
1b38b 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41 20  t,    /* OUT: A 
1b38c 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70  pointer to the p
1b38d 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
1b38e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69  t */.  const voi
1b38f 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 20  d **pzTail      
1b390 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f 66 20   /* OUT: End of 
1b391 70 61 72 73 65 64 20 73 74 72 69 6e 67 20 2a 2f  parsed string */
1b392 0a 29 7b 0a 20 20 2f 2a 20 54 68 69 73 20 66 75  .){.  /* This fu
1b393 6e 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79  nction currently
1b394 20 77 6f 72 6b 73 20 62 79 20 66 69 72 73 74 20   works by first 
1b395 74 72 61 6e 73 66 6f 72 6d 69 6e 67 20 74 68 65  transforming the
1b396 20 55 54 46 2d 31 36 0a 20 20 2a 2a 20 65 6e 63   UTF-16.  ** enc
1b397 6f 64 65 64 20 73 74 72 69 6e 67 20 74 6f 20 55  oded string to U
1b398 54 46 2d 38 2c 20 74 68 65 6e 20 69 6e 76 6f 6b  TF-8, then invok
1b399 69 6e 67 20 73 71 6c 69 74 65 33 5f 70 72 65 70  ing sqlite3_prep
1b39a 61 72 65 28 29 2e 20 54 68 65 0a 20 20 2a 2a 20  are(). The.  ** 
1b39b 74 72 69 63 6b 79 20 62 69 74 20 69 73 20 66 69  tricky bit is fi
1b39c 67 75 72 69 6e 67 20 6f 75 74 20 74 68 65 20 70  guring out the p
1b39d 6f 69 6e 74 65 72 20 74 6f 20 72 65 74 75 72 6e  ointer to return
1b39e 20 69 6e 20 2a 70 7a 54 61 69 6c 2e 0a 20 20 2a   in *pzTail..  *
1b39f 2f 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 38 3b  /.  char *zSql8;
1b3a0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
1b3a1 54 61 69 6c 38 20 3d 20 30 3b 0a 20 20 69 6e 74  Tail8 = 0;.  int
1b3a2 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
1b3a3 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  ..  if( sqlite3S
1b3a4 61 66 65 74 79 43 68 65 63 6b 28 64 62 29 20 29  afetyCheck(db) )
1b3a5 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
1b3a6 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a  ITE_MISUSE;.  }.
1b3a7 20 20 7a 53 71 6c 38 20 3d 20 73 71 6c 69 74 65    zSql8 = sqlite
1b3a8 33 55 74 66 31 36 74 6f 38 28 7a 53 71 6c 2c 20  3Utf16to8(zSql, 
1b3a9 6e 42 79 74 65 73 29 3b 0a 20 20 69 66 28 20 7a  nBytes);.  if( z
1b3aa 53 71 6c 38 20 29 7b 0a 20 20 20 20 72 63 20 3d  Sql8 ){.    rc =
1b3ab 20 73 71 6c 69 74 65 33 50 72 65 70 61 72 65 28   sqlite3Prepare(
1b3ac 64 62 2c 20 7a 53 71 6c 38 2c 20 2d 31 2c 20 73  db, zSql8, -1, s
1b3ad 61 76 65 53 71 6c 46 6c 61 67 2c 20 70 70 53 74  aveSqlFlag, ppSt
1b3ae 6d 74 2c 20 26 7a 54 61 69 6c 38 29 3b 0a 20 20  mt, &zTail8);.  
1b3af 7d 0a 0a 20 20 69 66 28 20 7a 54 61 69 6c 38 20  }..  if( zTail8 
1b3b0 26 26 20 70 7a 54 61 69 6c 20 29 7b 0a 20 20 20  && pzTail ){.   
1b3b1 20 2f 2a 20 49 66 20 73 71 6c 69 74 65 33 5f 70   /* If sqlite3_p
1b3b2 72 65 70 61 72 65 20 72 65 74 75 72 6e 73 20 61  repare returns a
1b3b3 20 74 61 69 6c 20 70 6f 69 6e 74 65 72 2c 20 77   tail pointer, w
1b3b4 65 20 63 61 6c 63 75 6c 61 74 65 20 74 68 65 0a  e calculate the.
1b3b5 20 20 20 20 2a 2a 20 65 71 75 69 76 61 6c 65 6e      ** equivalen
1b3b6 74 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f 20 74  t pointer into t
1b3b7 68 65 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67  he UTF-16 string
1b3b8 20 62 79 20 63 6f 75 6e 74 69 6e 67 20 74 68 65   by counting the
1b3b9 20 75 6e 69 63 6f 64 65 0a 20 20 20 20 2a 2a 20   unicode.    ** 
1b3ba 63 68 61 72 61 63 74 65 72 73 20 62 65 74 77 65  characters betwe
1b3bb 65 6e 20 7a 53 71 6c 38 20 61 6e 64 20 7a 54 61  en zSql8 and zTa
1b3bc 69 6c 38 2c 20 61 6e 64 20 74 68 65 6e 20 72 65  il8, and then re
1b3bd 74 75 72 6e 69 6e 67 20 61 20 70 6f 69 6e 74 65  turning a pointe
1b3be 72 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 61 6d  r.    ** the sam
1b3bf 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72  e number of char
1b3c0 61 63 74 65 72 73 20 69 6e 74 6f 20 74 68 65 20  acters into the 
1b3c1 55 54 46 2d 31 36 20 73 74 72 69 6e 67 2e 0a 20  UTF-16 string.. 
1b3c2 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 63 68     */.    int ch
1b3c3 61 72 73 5f 70 61 72 73 65 64 20 3d 20 73 71 6c  ars_parsed = sql
1b3c4 69 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28  ite3Utf8CharLen(
1b3c5 7a 53 71 6c 38 2c 20 7a 54 61 69 6c 38 2d 7a 53  zSql8, zTail8-zS
1b3c6 71 6c 38 29 3b 0a 20 20 20 20 2a 70 7a 54 61 69  ql8);.    *pzTai
1b3c7 6c 20 3d 20 28 75 38 20 2a 29 7a 53 71 6c 20 2b  l = (u8 *)zSql +
1b3c8 20 73 71 6c 69 74 65 33 55 74 66 31 36 42 79 74   sqlite3Utf16Byt
1b3c9 65 4c 65 6e 28 7a 53 71 6c 2c 20 63 68 61 72 73  eLen(zSql, chars
1b3ca 5f 70 61 72 73 65 64 29 3b 0a 20 20 7d 0a 20 20  _parsed);.  }.  
1b3cb 73 71 6c 69 74 65 46 72 65 65 28 7a 53 71 6c 38  sqliteFree(zSql8
1b3cc 29 3b 20 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  ); .  return sql
1b3cd 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20  ite3ApiExit(db, 
1b3ce 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 77  rc);.}../*.** Tw
1b3cf 6f 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68  o versions of th
1b3d0 65 20 6f 66 66 69 63 69 61 6c 20 41 50 49 2e 20  e official API. 
1b3d1 20 4c 65 67 61 63 79 20 61 6e 64 20 6e 65 77 20   Legacy and new 
1b3d2 75 73 65 2e 20 20 49 6e 20 74 68 65 20 6c 65 67  use.  In the leg
1b3d3 61 63 79 0a 2a 2a 20 76 65 72 73 69 6f 6e 2c 20  acy.** version, 
1b3d4 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c  the original SQL
1b3d5 20 74 65 78 74 20 69 73 20 6e 6f 74 20 73 61 76   text is not sav
1b3d6 65 64 20 69 6e 20 74 68 65 20 70 72 65 70 61 72  ed in the prepar
1b3d7 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20  ed statement.** 
1b3d8 61 6e 64 20 73 6f 20 69 66 20 61 20 73 63 68 65  and so if a sche
1b3d9 6d 61 20 63 68 61 6e 67 65 20 6f 63 63 75 72 73  ma change occurs
1b3da 2c 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20  , SQLITE_SCHEMA 
1b3db 69 73 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a  is returned by.*
1b3dc 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  * sqlite3_step()
1b3dd 2e 20 20 49 6e 20 74 68 65 20 6e 65 77 20 76 65  .  In the new ve
1b3de 72 73 69 6f 6e 2c 20 74 68 65 20 6f 72 69 67 69  rsion, the origi
1b3df 6e 61 6c 20 53 51 4c 20 74 65 78 74 20 69 73 20  nal SQL text is 
1b3e0 72 65 74 61 69 6e 65 64 0a 2a 2a 20 61 6e 64 20  retained.** and 
1b3e1 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73  the statement is
1b3e2 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72   automatically r
1b3e3 65 63 6f 6d 70 69 6c 65 64 20 69 66 20 61 6e 20  ecompiled if an 
1b3e4 73 63 68 65 6d 61 20 63 68 61 6e 67 65 0a 2a 2a  schema change.**
1b3e5 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53 51 4c 49   occurs..*/.SQLI
1b3e6 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1b3e7 65 33 5f 70 72 65 70 61 72 65 31 36 28 0a 20 20  e3_prepare16(.  
1b3e8 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
1b3e9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
1b3ea 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f  abase handle. */
1b3eb 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a   .  const void *
1b3ec 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a  zSql,         /*
1b3ed 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 53   UTF-8 encoded S
1b3ee 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f  QL statement. */
1b3ef 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20  .  int nBytes,  
1b3f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1b3f1 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69  Length of zSql i
1b3f2 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71  n bytes. */.  sq
1b3f3 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53  lite3_stmt **ppS
1b3f4 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20  tmt,    /* OUT: 
1b3f5 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  A pointer to the
1b3f6 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
1b3f7 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  ent */.  const v
1b3f8 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20  oid **pzTail    
1b3f9 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f     /* OUT: End o
1b3fa 66 20 70 61 72 73 65 64 20 73 74 72 69 6e 67 20  f parsed string 
1b3fb 2a 2f 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 73  */.){.  return s
1b3fc 71 6c 69 74 65 33 50 72 65 70 61 72 65 31 36 28  qlite3Prepare16(
1b3fd 64 62 2c 7a 53 71 6c 2c 6e 42 79 74 65 73 2c 30  db,zSql,nBytes,0
1b3fe 2c 70 70 53 74 6d 74 2c 70 7a 54 61 69 6c 29 3b  ,ppStmt,pzTail);
1b3ff 0a 7d 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 70  .}.int sqlite3_p
1b400 72 65 70 61 72 65 31 36 5f 76 32 28 0a 20 20 73  repare16_v2(.  s
1b401 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
1b402 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
1b403 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 20  base handle. */ 
1b404 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  .  const void *z
1b405 53 71 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  Sql,         /* 
1b406 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 53 51  UTF-8 encoded SQ
1b407 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a  L statement. */.
1b408 20 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20    int nBytes,   
1b409 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
1b40a 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e  ength of zSql in
1b40b 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c   bytes. */.  sql
1b40c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74  ite3_stmt **ppSt
1b40d 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41  mt,    /* OUT: A
1b40e 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
1b40f 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
1b410 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f  nt */.  const vo
1b411 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20  id **pzTail     
1b412 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f 66    /* OUT: End of
1b413 20 70 61 72 73 65 64 20 73 74 72 69 6e 67 20 2a   parsed string *
1b414 2f 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71  /.){.  return sq
1b415 6c 69 74 65 33 50 72 65 70 61 72 65 31 36 28 64  lite3Prepare16(d
1b416 62 2c 7a 53 71 6c 2c 6e 42 79 74 65 73 2c 31 2c  b,zSql,nBytes,1,
1b417 70 70 53 74 6d 74 2c 70 7a 54 61 69 6c 29 3b 0a  ppStmt,pzTail);.
1b418 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  }..#endif /* SQL
1b419 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a  ITE_OMIT_UTF16 *
1b41a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
1b41b 2a 2a 20 45 6e 64 20 6f 66 20 70 72 65 70 61 72  ** End of prepar
1b41c 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
1b41d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b41e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b41f 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
1b420 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73 65  ** Begin file se
1b421 6c 65 63 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  lect.c *********
1b422 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b423 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b424 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65  */./*.** 2001 Se
1b425 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a  ptember 15.**.**
1b426 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
1b427 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
1b428 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
1b429 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
1b42a 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
1b42b 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
1b42c 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
1b42d 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
1b42e 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
1b42f 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
1b430 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
1b431 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
1b432 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
1b433 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
1b434 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
1b435 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
1b436 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
1b437 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b438 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b439 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b43a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b43b 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
1b43c 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
1b43d 43 20 63 6f 64 65 20 72 6f 75 74 69 6e 65 73 20  C code routines 
1b43e 74 68 61 74 20 61 72 65 20 63 61 6c 6c 65 64 20  that are called 
1b43f 62 79 20 74 68 65 20 70 61 72 73 65 72 0a 2a 2a  by the parser.**
1b440 20 74 6f 20 68 61 6e 64 6c 65 20 53 45 4c 45 43   to handle SELEC
1b441 54 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20  T statements in 
1b442 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49  SQLite..**.** $I
1b443 64 3a 20 73 65 6c 65 63 74 2e 63 2c 76 20 31 2e  d: select.c,v 1.
1b444 33 35 34 20 32 30 30 37 2f 30 37 2f 31 38 20 31  354 2007/07/18 1
1b445 38 3a 31 37 3a 31 32 20 64 72 68 20 45 78 70 20  8:17:12 drh Exp 
1b446 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c  $.*/.../*.** Del
1b447 65 74 65 20 61 6c 6c 20 74 68 65 20 63 6f 6e 74  ete all the cont
1b448 65 6e 74 20 6f 66 20 61 20 53 65 6c 65 63 74 20  ent of a Select 
1b449 73 74 72 75 63 74 75 72 65 20 62 75 74 20 64 6f  structure but do
1b44a 20 6e 6f 74 20 64 65 61 6c 6c 6f 63 61 74 65 0a   not deallocate.
1b44b 2a 2a 20 74 68 65 20 73 65 6c 65 63 74 20 73 74  ** the select st
1b44c 72 75 63 74 75 72 65 20 69 74 73 65 6c 66 2e 0a  ructure itself..
1b44d 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
1b44e 6c 65 61 72 53 65 6c 65 63 74 28 53 65 6c 65 63  learSelect(Selec
1b44f 74 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33  t *p){.  sqlite3
1b450 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70  ExprListDelete(p
1b451 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 73 71 6c  ->pEList);.  sql
1b452 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74  ite3SrcListDelet
1b453 65 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 73 71  e(p->pSrc);.  sq
1b454 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
1b455 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20 73 71  p->pWhere);.  sq
1b456 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c  lite3ExprListDel
1b457 65 74 65 28 70 2d 3e 70 47 72 6f 75 70 42 79 29  ete(p->pGroupBy)
1b458 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44  ;.  sqlite3ExprD
1b459 65 6c 65 74 65 28 70 2d 3e 70 48 61 76 69 6e 67  elete(p->pHaving
1b45a 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
1b45b 4c 69 73 74 44 65 6c 65 74 65 28 70 2d 3e 70 4f  ListDelete(p->pO
1b45c 72 64 65 72 42 79 29 3b 0a 20 20 73 71 6c 69 74  rderBy);.  sqlit
1b45d 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70  e3SelectDelete(p
1b45e 2d 3e 70 50 72 69 6f 72 29 3b 0a 20 20 73 71 6c  ->pPrior);.  sql
1b45f 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 70  ite3ExprDelete(p
1b460 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20 20 73 71 6c  ->pLimit);.  sql
1b461 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 70  ite3ExprDelete(p
1b462 2d 3e 70 4f 66 66 73 65 74 29 3b 0a 7d 0a 0a 0a  ->pOffset);.}...
1b463 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61  /*.** Allocate a
1b464 20 6e 65 77 20 53 65 6c 65 63 74 20 73 74 72 75   new Select stru
1b465 63 74 75 72 65 20 61 6e 64 20 72 65 74 75 72 6e  cture and return
1b466 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
1b467 61 74 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e  at.** structure.
1b468 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1b469 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74  TE Select *sqlit
1b46a 65 33 53 65 6c 65 63 74 4e 65 77 28 0a 20 20 45  e3SelectNew(.  E
1b46b 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c  xprList *pEList,
1b46c 20 20 20 20 20 2f 2a 20 77 68 69 63 68 20 63 6f       /* which co
1b46d 6c 75 6d 6e 73 20 74 6f 20 69 6e 63 6c 75 64 65  lumns to include
1b46e 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 2a   in the result *
1b46f 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  /.  SrcList *pSr
1b470 63 2c 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65  c,        /* the
1b471 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2d 2d 20   FROM clause -- 
1b472 77 68 69 63 68 20 74 61 62 6c 65 73 20 74 6f 20  which tables to 
1b473 73 63 61 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a  scan */.  Expr *
1b474 70 57 68 65 72 65 2c 20 20 20 20 20 20 20 20 20  pWhere,         
1b475 2f 2a 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  /* the WHERE cla
1b476 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  use */.  ExprLis
1b477 74 20 2a 70 47 72 6f 75 70 42 79 2c 20 20 20 2f  t *pGroupBy,   /
1b478 2a 20 74 68 65 20 47 52 4f 55 50 20 42 59 20 63  * the GROUP BY c
1b479 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 20  lause */.  Expr 
1b47a 2a 70 48 61 76 69 6e 67 2c 20 20 20 20 20 20 20  *pHaving,       
1b47b 20 2f 2a 20 74 68 65 20 48 41 56 49 4e 47 20 63   /* the HAVING c
1b47c 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c  lause */.  ExprL
1b47d 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20  ist *pOrderBy,  
1b47e 20 2f 2a 20 74 68 65 20 4f 52 44 45 52 20 42 59   /* the ORDER BY
1b47f 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74   clause */.  int
1b480 20 69 73 44 69 73 74 69 6e 63 74 2c 20 20 20 20   isDistinct,    
1b481 20 20 20 2f 2a 20 74 72 75 65 20 69 66 20 74 68     /* true if th
1b482 65 20 44 49 53 54 49 4e 43 54 20 6b 65 79 77 6f  e DISTINCT keywo
1b483 72 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f  rd is present */
1b484 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 2c  .  Expr *pLimit,
1b485 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 49 4d 49           /* LIMI
1b486 54 20 76 61 6c 75 65 2e 20 20 4e 55 4c 4c 20 6d  T value.  NULL m
1b487 65 61 6e 73 20 6e 6f 74 20 75 73 65 64 20 2a 2f  eans not used */
1b488 0a 20 20 45 78 70 72 20 2a 70 4f 66 66 73 65 74  .  Expr *pOffset
1b489 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 46 46 53           /* OFFS
1b48a 45 54 20 76 61 6c 75 65 2e 20 20 4e 55 4c 4c 20  ET value.  NULL 
1b48b 6d 65 61 6e 73 20 6e 6f 20 6f 66 66 73 65 74 20  means no offset 
1b48c 2a 2f 0a 29 7b 0a 20 20 53 65 6c 65 63 74 20 2a  */.){.  Select *
1b48d 70 4e 65 77 3b 0a 20 20 53 65 6c 65 63 74 20 73  pNew;.  Select s
1b48e 74 61 6e 64 69 6e 3b 0a 20 20 70 4e 65 77 20 3d  tandin;.  pNew =
1b48f 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 20 73   sqliteMalloc( s
1b490 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20 29 3b 0a  izeof(*pNew) );.
1b491 20 20 61 73 73 65 72 74 28 20 21 70 4f 66 66 73    assert( !pOffs
1b492 65 74 20 7c 7c 20 70 4c 69 6d 69 74 20 29 3b 20  et || pLimit ); 
1b493 20 20 2f 2a 20 43 61 6e 27 74 20 68 61 76 65 20    /* Can't have 
1b494 4f 46 46 53 45 54 20 77 69 74 68 6f 75 74 20 4c  OFFSET without L
1b495 49 4d 49 54 2e 20 2a 2f 0a 20 20 69 66 28 20 70  IMIT. */.  if( p
1b496 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4e  New==0 ){.    pN
1b497 65 77 20 3d 20 26 73 74 61 6e 64 69 6e 3b 0a 20  ew = &standin;. 
1b498 20 20 20 6d 65 6d 73 65 74 28 70 4e 65 77 2c 20     memset(pNew, 
1b499 30 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29  0, sizeof(*pNew)
1b49a 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 45 4c  );.  }.  if( pEL
1b49b 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 45  ist==0 ){.    pE
1b49c 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78  List = sqlite3Ex
1b49d 70 72 4c 69 73 74 41 70 70 65 6e 64 28 30 2c 20  prListAppend(0, 
1b49e 73 71 6c 69 74 65 33 45 78 70 72 28 54 4b 5f 41  sqlite3Expr(TK_A
1b49f 4c 4c 2c 30 2c 30 2c 30 29 2c 20 30 29 3b 0a 20  LL,0,0,0), 0);. 
1b4a0 20 7d 0a 20 20 70 4e 65 77 2d 3e 70 45 4c 69 73   }.  pNew->pELis
1b4a1 74 20 3d 20 70 45 4c 69 73 74 3b 0a 20 20 70 4e  t = pEList;.  pN
1b4a2 65 77 2d 3e 70 53 72 63 20 3d 20 70 53 72 63 3b  ew->pSrc = pSrc;
1b4a3 0a 20 20 70 4e 65 77 2d 3e 70 57 68 65 72 65 20  .  pNew->pWhere 
1b4a4 3d 20 70 57 68 65 72 65 3b 0a 20 20 70 4e 65 77  = pWhere;.  pNew
1b4a5 2d 3e 70 47 72 6f 75 70 42 79 20 3d 20 70 47 72  ->pGroupBy = pGr
1b4a6 6f 75 70 42 79 3b 0a 20 20 70 4e 65 77 2d 3e 70  oupBy;.  pNew->p
1b4a7 48 61 76 69 6e 67 20 3d 20 70 48 61 76 69 6e 67  Having = pHaving
1b4a8 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f 72 64 65 72  ;.  pNew->pOrder
1b4a9 42 79 20 3d 20 70 4f 72 64 65 72 42 79 3b 0a 20  By = pOrderBy;. 
1b4aa 20 70 4e 65 77 2d 3e 69 73 44 69 73 74 69 6e 63   pNew->isDistinc
1b4ab 74 20 3d 20 69 73 44 69 73 74 69 6e 63 74 3b 0a  t = isDistinct;.
1b4ac 20 20 70 4e 65 77 2d 3e 6f 70 20 3d 20 54 4b 5f    pNew->op = TK_
1b4ad 53 45 4c 45 43 54 3b 0a 20 20 61 73 73 65 72 74  SELECT;.  assert
1b4ae 28 20 70 4f 66 66 73 65 74 3d 3d 30 20 7c 7c 20  ( pOffset==0 || 
1b4af 70 4c 69 6d 69 74 21 3d 30 20 29 3b 0a 20 20 70  pLimit!=0 );.  p
1b4b0 4e 65 77 2d 3e 70 4c 69 6d 69 74 20 3d 20 70 4c  New->pLimit = pL
1b4b1 69 6d 69 74 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f  imit;.  pNew->pO
1b4b2 66 66 73 65 74 20 3d 20 70 4f 66 66 73 65 74 3b  ffset = pOffset;
1b4b3 0a 20 20 70 4e 65 77 2d 3e 69 4c 69 6d 69 74 20  .  pNew->iLimit 
1b4b4 3d 20 2d 31 3b 0a 20 20 70 4e 65 77 2d 3e 69 4f  = -1;.  pNew->iO
1b4b5 66 66 73 65 74 20 3d 20 2d 31 3b 0a 20 20 70 4e  ffset = -1;.  pN
1b4b6 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d  ew->addrOpenEphm
1b4b7 5b 30 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e 65 77  [0] = -1;.  pNew
1b4b8 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 31  ->addrOpenEphm[1
1b4b9 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e 65 77 2d 3e  ] = -1;.  pNew->
1b4ba 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 32 5d 20  addrOpenEphm[2] 
1b4bb 3d 20 2d 31 3b 0a 20 20 69 66 28 20 70 4e 65 77  = -1;.  if( pNew
1b4bc 3d 3d 26 73 74 61 6e 64 69 6e 29 20 7b 0a 20 20  ==&standin) {.  
1b4bd 20 20 63 6c 65 61 72 53 65 6c 65 63 74 28 70 4e    clearSelect(pN
1b4be 65 77 29 3b 0a 20 20 20 20 70 4e 65 77 20 3d 20  ew);.    pNew = 
1b4bf 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
1b4c0 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  pNew;.}../*.** D
1b4c1 65 6c 65 74 65 20 74 68 65 20 67 69 76 65 6e 20  elete the given 
1b4c2 53 65 6c 65 63 74 20 73 74 72 75 63 74 75 72 65  Select structure
1b4c3 20 61 6e 64 20 61 6c 6c 20 6f 66 20 69 74 73 20   and all of its 
1b4c4 73 75 62 73 74 72 75 63 74 75 72 65 73 2e 0a 2a  substructures..*
1b4c5 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1b4c6 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c   void sqlite3Sel
1b4c7 65 63 74 44 65 6c 65 74 65 28 53 65 6c 65 63 74  ectDelete(Select
1b4c8 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b   *p){.  if( p ){
1b4c9 0a 20 20 20 20 63 6c 65 61 72 53 65 6c 65 63 74  .    clearSelect
1b4ca 28 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 46  (p);.    sqliteF
1b4cb 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ree(p);.  }.}../
1b4cc 2a 0a 2a 2a 20 47 69 76 65 6e 20 31 20 74 6f 20  *.** Given 1 to 
1b4cd 33 20 69 64 65 6e 74 69 66 69 65 72 73 20 70 72  3 identifiers pr
1b4ce 65 63 65 65 64 69 6e 67 20 74 68 65 20 4a 4f 49  eceeding the JOI
1b4cf 4e 20 6b 65 79 77 6f 72 64 2c 20 64 65 74 65 72  N keyword, deter
1b4d0 6d 69 6e 65 20 74 68 65 0a 2a 2a 20 74 79 70 65  mine the.** type
1b4d1 20 6f 66 20 6a 6f 69 6e 2e 20 20 52 65 74 75 72   of join.  Retur
1b4d2 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 6e  n an integer con
1b4d3 73 74 61 6e 74 20 74 68 61 74 20 65 78 70 72 65  stant that expre
1b4d4 73 73 65 73 20 74 68 61 74 20 74 79 70 65 0a 2a  sses that type.*
1b4d5 2a 20 69 6e 20 74 65 72 6d 73 20 6f 66 20 74 68  * in terms of th
1b4d6 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 69 74 20  e following bit 
1b4d7 76 61 6c 75 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  values:.**.**   
1b4d8 20 20 4a 54 5f 49 4e 4e 45 52 0a 2a 2a 20 20 20    JT_INNER.**   
1b4d9 20 20 4a 54 5f 43 52 4f 53 53 0a 2a 2a 20 20 20    JT_CROSS.**   
1b4da 20 20 4a 54 5f 4f 55 54 45 52 0a 2a 2a 20 20 20    JT_OUTER.**   
1b4db 20 20 4a 54 5f 4e 41 54 55 52 41 4c 0a 2a 2a 20    JT_NATURAL.** 
1b4dc 20 20 20 20 4a 54 5f 4c 45 46 54 0a 2a 2a 20 20      JT_LEFT.**  
1b4dd 20 20 20 4a 54 5f 52 49 47 48 54 0a 2a 2a 0a 2a     JT_RIGHT.**.*
1b4de 2a 20 41 20 66 75 6c 6c 20 6f 75 74 65 72 20 6a  * A full outer j
1b4df 6f 69 6e 20 69 73 20 74 68 65 20 63 6f 6d 62 69  oin is the combi
1b4e0 6e 61 74 69 6f 6e 20 6f 66 20 4a 54 5f 4c 45 46  nation of JT_LEF
1b4e1 54 20 61 6e 64 20 4a 54 5f 52 49 47 48 54 2e 0a  T and JT_RIGHT..
1b4e2 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 69 6c 6c 65  **.** If an ille
1b4e3 67 61 6c 20 6f 72 20 75 6e 73 75 70 70 6f 72 74  gal or unsupport
1b4e4 65 64 20 6a 6f 69 6e 20 74 79 70 65 20 69 73 20  ed join type is 
1b4e5 73 65 65 6e 2c 20 74 68 65 6e 20 73 74 69 6c 6c  seen, then still
1b4e6 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 6a 6f 69   return.** a joi
1b4e7 6e 20 74 79 70 65 2c 20 62 75 74 20 70 75 74 20  n type, but put 
1b4e8 61 6e 20 65 72 72 6f 72 20 69 6e 20 74 68 65 20  an error in the 
1b4e9 70 50 61 72 73 65 20 73 74 72 75 63 74 75 72 65  pParse structure
1b4ea 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1b4eb 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a  ATE int sqlite3J
1b4ec 6f 69 6e 54 79 70 65 28 50 61 72 73 65 20 2a 70  oinType(Parse *p
1b4ed 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 41  Parse, Token *pA
1b4ee 2c 20 54 6f 6b 65 6e 20 2a 70 42 2c 20 54 6f 6b  , Token *pB, Tok
1b4ef 65 6e 20 2a 70 43 29 7b 0a 20 20 69 6e 74 20 6a  en *pC){.  int j
1b4f0 6f 69 6e 74 79 70 65 20 3d 20 30 3b 0a 20 20 54  ointype = 0;.  T
1b4f1 6f 6b 65 6e 20 2a 61 70 41 6c 6c 5b 33 5d 3b 0a  oken *apAll[3];.
1b4f2 20 20 54 6f 6b 65 6e 20 2a 70 3b 0a 20 20 73 74    Token *p;.  st
1b4f3 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63  atic const struc
1b4f4 74 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  t {.    const ch
1b4f5 61 72 20 7a 4b 65 79 77 6f 72 64 5b 38 5d 3b 0a  ar zKeyword[8];.
1b4f6 20 20 20 20 75 38 20 6e 43 68 61 72 3b 0a 20 20      u8 nChar;.  
1b4f7 20 20 75 38 20 63 6f 64 65 3b 0a 20 20 7d 20 6b    u8 code;.  } k
1b4f8 65 79 77 6f 72 64 73 5b 5d 20 3d 20 7b 0a 20 20  eywords[] = {.  
1b4f9 20 20 7b 20 22 6e 61 74 75 72 61 6c 22 2c 20 37    { "natural", 7
1b4fa 2c 20 4a 54 5f 4e 41 54 55 52 41 4c 20 7d 2c 0a  , JT_NATURAL },.
1b4fb 20 20 20 20 7b 20 22 6c 65 66 74 22 2c 20 20 20      { "left",   
1b4fc 20 34 2c 20 4a 54 5f 4c 45 46 54 7c 4a 54 5f 4f   4, JT_LEFT|JT_O
1b4fd 55 54 45 52 20 7d 2c 0a 20 20 20 20 7b 20 22 72  UTER },.    { "r
1b4fe 69 67 68 74 22 2c 20 20 20 35 2c 20 4a 54 5f 52  ight",   5, JT_R
1b4ff 49 47 48 54 7c 4a 54 5f 4f 55 54 45 52 20 7d 2c  IGHT|JT_OUTER },
1b500 0a 20 20 20 20 7b 20 22 66 75 6c 6c 22 2c 20 20  .    { "full",  
1b501 20 20 34 2c 20 4a 54 5f 4c 45 46 54 7c 4a 54 5f    4, JT_LEFT|JT_
1b502 52 49 47 48 54 7c 4a 54 5f 4f 55 54 45 52 20 7d  RIGHT|JT_OUTER }
1b503 2c 0a 20 20 20 20 7b 20 22 6f 75 74 65 72 22 2c  ,.    { "outer",
1b504 20 20 20 35 2c 20 4a 54 5f 4f 55 54 45 52 20 7d     5, JT_OUTER }
1b505 2c 0a 20 20 20 20 7b 20 22 69 6e 6e 65 72 22 2c  ,.    { "inner",
1b506 20 20 20 35 2c 20 4a 54 5f 49 4e 4e 45 52 20 7d     5, JT_INNER }
1b507 2c 0a 20 20 20 20 7b 20 22 63 72 6f 73 73 22 2c  ,.    { "cross",
1b508 20 20 20 35 2c 20 4a 54 5f 49 4e 4e 45 52 7c 4a     5, JT_INNER|J
1b509 54 5f 43 52 4f 53 53 20 7d 2c 0a 20 20 7d 3b 0a  T_CROSS },.  };.
1b50a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 61 70    int i, j;.  ap
1b50b 41 6c 6c 5b 30 5d 20 3d 20 70 41 3b 0a 20 20 61  All[0] = pA;.  a
1b50c 70 41 6c 6c 5b 31 5d 20 3d 20 70 42 3b 0a 20 20  pAll[1] = pB;.  
1b50d 61 70 41 6c 6c 5b 32 5d 20 3d 20 70 43 3b 0a 20  apAll[2] = pC;. 
1b50e 20 66 6f 72 28 69 3d 30 3b 20 69 3c 33 20 26 26   for(i=0; i<3 &&
1b50f 20 61 70 41 6c 6c 5b 69 5d 3b 20 69 2b 2b 29 7b   apAll[i]; i++){
1b510 0a 20 20 20 20 70 20 3d 20 61 70 41 6c 6c 5b 69  .    p = apAll[i
1b511 5d 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20  ];.    for(j=0; 
1b512 6a 3c 73 69 7a 65 6f 66 28 6b 65 79 77 6f 72 64  j<sizeof(keyword
1b513 73 29 2f 73 69 7a 65 6f 66 28 6b 65 79 77 6f 72  s)/sizeof(keywor
1b514 64 73 5b 30 5d 29 3b 20 6a 2b 2b 29 7b 0a 20 20  ds[0]); j++){.  
1b515 20 20 20 20 69 66 28 20 70 2d 3e 6e 3d 3d 6b 65      if( p->n==ke
1b516 79 77 6f 72 64 73 5b 6a 5d 2e 6e 43 68 61 72 20  ywords[j].nChar 
1b517 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 73 71  .          && sq
1b518 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 28 63  lite3StrNICmp((c
1b519 68 61 72 2a 29 70 2d 3e 7a 2c 20 6b 65 79 77 6f  har*)p->z, keywo
1b51a 72 64 73 5b 6a 5d 2e 7a 4b 65 79 77 6f 72 64 2c  rds[j].zKeyword,
1b51b 20 70 2d 3e 6e 29 3d 3d 30 20 29 7b 0a 20 20 20   p->n)==0 ){.   
1b51c 20 20 20 20 20 6a 6f 69 6e 74 79 70 65 20 7c 3d       jointype |=
1b51d 20 6b 65 79 77 6f 72 64 73 5b 6a 5d 2e 63 6f 64   keywords[j].cod
1b51e 65 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  e;.        break
1b51f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1b520 20 20 20 20 69 66 28 20 6a 3e 3d 73 69 7a 65 6f      if( j>=sizeo
1b521 66 28 6b 65 79 77 6f 72 64 73 29 2f 73 69 7a 65  f(keywords)/size
1b522 6f 66 28 6b 65 79 77 6f 72 64 73 5b 30 5d 29 20  of(keywords[0]) 
1b523 29 7b 0a 20 20 20 20 20 20 6a 6f 69 6e 74 79 70  ){.      jointyp
1b524 65 20 7c 3d 20 4a 54 5f 45 52 52 4f 52 3b 0a 20  e |= JT_ERROR;. 
1b525 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1b526 7d 0a 20 20 7d 0a 20 20 69 66 28 0a 20 20 20 20  }.  }.  if(.    
1b527 20 28 6a 6f 69 6e 74 79 70 65 20 26 20 28 4a 54   (jointype & (JT
1b528 5f 49 4e 4e 45 52 7c 4a 54 5f 4f 55 54 45 52 29  _INNER|JT_OUTER)
1b529 29 3d 3d 28 4a 54 5f 49 4e 4e 45 52 7c 4a 54 5f  )==(JT_INNER|JT_
1b52a 4f 55 54 45 52 29 20 7c 7c 0a 20 20 20 20 20 28  OUTER) ||.     (
1b52b 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 45 52  jointype & JT_ER
1b52c 52 4f 52 29 21 3d 30 0a 20 20 29 7b 0a 20 20 20  ROR)!=0.  ){.   
1b52d 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 70   const char *zSp
1b52e 31 20 3d 20 22 20 22 3b 0a 20 20 20 20 63 6f 6e  1 = " ";.    con
1b52f 73 74 20 63 68 61 72 20 2a 7a 53 70 32 20 3d 20  st char *zSp2 = 
1b530 22 20 22 3b 0a 20 20 20 20 69 66 28 20 70 42 3d  " ";.    if( pB=
1b531 3d 30 20 29 7b 20 7a 53 70 31 2b 2b 3b 20 7d 0a  =0 ){ zSp1++; }.
1b532 20 20 20 20 69 66 28 20 70 43 3d 3d 30 20 29 7b      if( pC==0 ){
1b533 20 7a 53 70 32 2b 2b 3b 20 7d 0a 20 20 20 20 73   zSp2++; }.    s
1b534 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
1b535 50 61 72 73 65 2c 20 22 75 6e 6b 6e 6f 77 6e 20  Parse, "unknown 
1b536 6f 72 20 75 6e 73 75 70 70 6f 72 74 65 64 20 6a  or unsupported j
1b537 6f 69 6e 20 74 79 70 65 3a 20 22 0a 20 20 20 20  oin type: ".    
1b538 20 20 20 22 25 54 25 73 25 54 25 73 25 54 22 2c     "%T%s%T%s%T",
1b539 20 70 41 2c 20 7a 53 70 31 2c 20 70 42 2c 20 7a   pA, zSp1, pB, z
1b53a 53 70 32 2c 20 70 43 29 3b 0a 20 20 20 20 6a 6f  Sp2, pC);.    jo
1b53b 69 6e 74 79 70 65 20 3d 20 4a 54 5f 49 4e 4e 45  intype = JT_INNE
1b53c 52 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6a  R;.  }else if( j
1b53d 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 52 49 47  ointype & JT_RIG
1b53e 48 54 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  HT ){.    sqlite
1b53f 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
1b540 2c 20 0a 20 20 20 20 20 20 22 52 49 47 48 54 20  , .      "RIGHT 
1b541 61 6e 64 20 46 55 4c 4c 20 4f 55 54 45 52 20 4a  and FULL OUTER J
1b542 4f 49 4e 73 20 61 72 65 20 6e 6f 74 20 63 75 72  OINs are not cur
1b543 72 65 6e 74 6c 79 20 73 75 70 70 6f 72 74 65 64  rently supported
1b544 22 29 3b 0a 20 20 20 20 6a 6f 69 6e 74 79 70 65  ");.    jointype
1b545 20 3d 20 4a 54 5f 49 4e 4e 45 52 3b 0a 20 20 7d   = JT_INNER;.  }
1b546 0a 20 20 72 65 74 75 72 6e 20 6a 6f 69 6e 74 79  .  return jointy
1b547 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  pe;.}../*.** Ret
1b548 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66  urn the index of
1b549 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 74   a column in a t
1b54a 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 2d 31  able.  Return -1
1b54b 20 69 66 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 2a   if the column.*
1b54c 2a 20 69 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e  * is not contain
1b54d 65 64 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e  ed in the table.
1b54e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
1b54f 6f 6c 75 6d 6e 49 6e 64 65 78 28 54 61 62 6c 65  olumnIndex(Table
1b550 20 2a 70 54 61 62 2c 20 63 6f 6e 73 74 20 63 68   *pTab, const ch
1b551 61 72 20 2a 7a 43 6f 6c 29 7b 0a 20 20 69 6e 74  ar *zCol){.  int
1b552 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   i;.  for(i=0; i
1b553 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b  <pTab->nCol; i++
1b554 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ){.    if( sqlit
1b555 65 33 53 74 72 49 43 6d 70 28 70 54 61 62 2d 3e  e3StrICmp(pTab->
1b556 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 7a  aCol[i].zName, z
1b557 43 6f 6c 29 3d 3d 30 20 29 20 72 65 74 75 72 6e  Col)==0 ) return
1b558 20 69 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   i;.  }.  return
1b559 20 2d 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65   -1;.}../*.** Se
1b55a 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61  t the value of a
1b55b 20 74 6f 6b 65 6e 20 74 6f 20 61 20 27 5c 30 30   token to a '\00
1b55c 30 27 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74  0'-terminated st
1b55d 72 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ring..*/.static 
1b55e 76 6f 69 64 20 73 65 74 54 6f 6b 65 6e 28 54 6f  void setToken(To
1b55f 6b 65 6e 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68  ken *p, const ch
1b560 61 72 20 2a 7a 29 7b 0a 20 20 70 2d 3e 7a 20 3d  ar *z){.  p->z =
1b561 20 28 75 38 2a 29 7a 3b 0a 20 20 70 2d 3e 6e 20   (u8*)z;.  p->n 
1b562 3d 20 7a 20 3f 20 73 74 72 6c 65 6e 28 7a 29 20  = z ? strlen(z) 
1b563 3a 20 30 3b 0a 20 20 70 2d 3e 64 79 6e 20 3d 20  : 0;.  p->dyn = 
1b564 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20  0;.}../*.** Set 
1b565 74 68 65 20 74 6f 6b 65 6e 20 74 6f 20 74 68 65  the token to the
1b566 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20 61   double-quoted a
1b567 6e 64 20 65 73 63 61 70 65 64 20 76 65 72 73 69  nd escaped versi
1b568 6f 6e 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67  on of the string
1b569 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62   pointed.** to b
1b56a 79 20 7a 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65  y z. For example
1b56b 3b 0a 2a 2a 0a 2a 2a 20 20 20 20 7b 61 22 62 63  ;.**.**    {a"bc
1b56c 7d 20 20 2d 3e 20 20 7b 22 61 22 22 62 63 22 7d  }  ->  {"a""bc"}
1b56d 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1b56e 73 65 74 51 75 6f 74 65 64 54 6f 6b 65 6e 28 54  setQuotedToken(T
1b56f 6f 6b 65 6e 20 2a 70 2c 20 63 6f 6e 73 74 20 63  oken *p, const c
1b570 68 61 72 20 2a 7a 29 7b 0a 20 20 70 2d 3e 7a 20  har *z){.  p->z 
1b571 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 4d  = (u8 *)sqlite3M
1b572 50 72 69 6e 74 66 28 22 5c 22 25 77 5c 22 22 2c  Printf("\"%w\"",
1b573 20 7a 29 3b 0a 20 20 70 2d 3e 64 79 6e 20 3d 20   z);.  p->dyn = 
1b574 31 3b 0a 20 20 69 66 28 20 70 2d 3e 7a 20 29 7b  1;.  if( p->z ){
1b575 0a 20 20 20 20 70 2d 3e 6e 20 3d 20 73 74 72 6c  .    p->n = strl
1b576 65 6e 28 28 63 68 61 72 20 2a 29 70 2d 3e 7a 29  en((char *)p->z)
1b577 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  ;.  }.}../*.** C
1b578 72 65 61 74 65 20 61 6e 20 65 78 70 72 65 73 73  reate an express
1b579 69 6f 6e 20 6e 6f 64 65 20 66 6f 72 20 61 6e 20  ion node for an 
1b57a 69 64 65 6e 74 69 66 69 65 72 20 77 69 74 68 20  identifier with 
1b57b 74 68 65 20 6e 61 6d 65 20 6f 66 20 7a 4e 61 6d  the name of zNam
1b57c 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  e.*/.SQLITE_PRIV
1b57d 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65  ATE Expr *sqlite
1b57e 33 43 72 65 61 74 65 49 64 45 78 70 72 28 63 6f  3CreateIdExpr(co
1b57f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29  nst char *zName)
1b580 7b 0a 20 20 54 6f 6b 65 6e 20 64 75 6d 6d 79 3b  {.  Token dummy;
1b581 0a 20 20 73 65 74 54 6f 6b 65 6e 28 26 64 75 6d  .  setToken(&dum
1b582 6d 79 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 72 65  my, zName);.  re
1b583 74 75 72 6e 20 73 71 6c 69 74 65 33 45 78 70 72  turn sqlite3Expr
1b584 28 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 64  (TK_ID, 0, 0, &d
1b585 75 6d 6d 79 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  ummy);.}.../*.**
1b586 20 41 64 64 20 61 20 74 65 72 6d 20 74 6f 20 74   Add a term to t
1b587 68 65 20 57 48 45 52 45 20 65 78 70 72 65 73 73  he WHERE express
1b588 69 6f 6e 20 69 6e 20 2a 70 70 45 78 70 72 20 74  ion in *ppExpr t
1b589 68 61 74 20 72 65 71 75 69 72 65 73 20 74 68 65  hat requires the
1b58a 0a 2a 2a 20 7a 43 6f 6c 20 63 6f 6c 75 6d 6e 20  .** zCol column 
1b58b 74 6f 20 62 65 20 65 71 75 61 6c 20 69 6e 20 74  to be equal in t
1b58c 68 65 20 74 77 6f 20 74 61 62 6c 65 73 20 70 54  he two tables pT
1b58d 61 62 31 20 61 6e 64 20 70 54 61 62 32 2e 0a 2a  ab1 and pTab2..*
1b58e 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 64  /.static void ad
1b58f 64 57 68 65 72 65 54 65 72 6d 28 0a 20 20 63 6f  dWhereTerm(.  co
1b590 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 2c 20  nst char *zCol, 
1b591 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
1b592 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a  f the column */.
1b593 20 20 63 6f 6e 73 74 20 54 61 62 6c 65 20 2a 70    const Table *p
1b594 54 61 62 31 2c 20 20 20 20 20 20 2f 2a 20 46 69  Tab1,      /* Fi
1b595 72 73 74 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63  rst table */.  c
1b596 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 6c 69 61  onst char *zAlia
1b597 73 31 2c 20 20 20 20 20 2f 2a 20 41 6c 69 61 73  s1,     /* Alias
1b598 20 66 6f 72 20 66 69 72 73 74 20 74 61 62 6c 65   for first table
1b599 2e 20 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a  .  May be NULL *
1b59a 2f 0a 20 20 63 6f 6e 73 74 20 54 61 62 6c 65 20  /.  const Table 
1b59b 2a 70 54 61 62 32 2c 20 20 20 20 20 20 2f 2a 20  *pTab2,      /* 
1b59c 53 65 63 6f 6e 64 20 74 61 62 6c 65 20 2a 2f 0a  Second table */.
1b59d 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41    const char *zA
1b59e 6c 69 61 73 32 2c 20 20 20 20 20 2f 2a 20 41 6c  lias2,     /* Al
1b59f 69 61 73 20 66 6f 72 20 73 65 63 6f 6e 64 20 74  ias for second t
1b5a0 61 62 6c 65 2e 20 20 4d 61 79 20 62 65 20 4e 55  able.  May be NU
1b5a1 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 69 52 69 67  LL */.  int iRig
1b5a2 68 74 4a 6f 69 6e 54 61 62 6c 65 2c 20 20 20 20  htJoinTable,    
1b5a3 20 2f 2a 20 56 44 42 45 20 63 75 72 73 6f 72 20   /* VDBE cursor 
1b5a4 66 6f 72 20 74 68 65 20 72 69 67 68 74 20 74 61  for the right ta
1b5a5 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 2a  ble */.  Expr **
1b5a6 70 70 45 78 70 72 20 20 20 20 20 20 20 20 20 20  ppExpr          
1b5a7 20 20 2f 2a 20 41 64 64 20 74 68 65 20 65 71 75    /* Add the equ
1b5a8 61 6c 69 74 79 20 74 65 72 6d 20 74 6f 20 74 68  ality term to th
1b5a9 69 73 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f  is expression */
1b5aa 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 45 31 61  .){.  Expr *pE1a
1b5ab 2c 20 2a 70 45 31 62 2c 20 2a 70 45 31 63 3b 0a  , *pE1b, *pE1c;.
1b5ac 20 20 45 78 70 72 20 2a 70 45 32 61 2c 20 2a 70    Expr *pE2a, *p
1b5ad 45 32 62 2c 20 2a 70 45 32 63 3b 0a 20 20 45 78  E2b, *pE2c;.  Ex
1b5ae 70 72 20 2a 70 45 3b 0a 0a 20 20 70 45 31 61 20  pr *pE;..  pE1a 
1b5af 3d 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 49  = sqlite3CreateI
1b5b0 64 45 78 70 72 28 7a 43 6f 6c 29 3b 0a 20 20 70  dExpr(zCol);.  p
1b5b1 45 32 61 20 3d 20 73 71 6c 69 74 65 33 43 72 65  E2a = sqlite3Cre
1b5b2 61 74 65 49 64 45 78 70 72 28 7a 43 6f 6c 29 3b  ateIdExpr(zCol);
1b5b3 0a 20 20 69 66 28 20 7a 41 6c 69 61 73 31 3d 3d  .  if( zAlias1==
1b5b4 30 20 29 7b 0a 20 20 20 20 7a 41 6c 69 61 73 31  0 ){.    zAlias1
1b5b5 20 3d 20 70 54 61 62 31 2d 3e 7a 4e 61 6d 65 3b   = pTab1->zName;
1b5b6 0a 20 20 7d 0a 20 20 70 45 31 62 20 3d 20 73 71  .  }.  pE1b = sq
1b5b7 6c 69 74 65 33 43 72 65 61 74 65 49 64 45 78 70  lite3CreateIdExp
1b5b8 72 28 7a 41 6c 69 61 73 31 29 3b 0a 20 20 69 66  r(zAlias1);.  if
1b5b9 28 20 7a 41 6c 69 61 73 32 3d 3d 30 20 29 7b 0a  ( zAlias2==0 ){.
1b5ba 20 20 20 20 7a 41 6c 69 61 73 32 20 3d 20 70 54      zAlias2 = pT
1b5bb 61 62 32 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 7d 0a  ab2->zName;.  }.
1b5bc 20 20 70 45 32 62 20 3d 20 73 71 6c 69 74 65 33    pE2b = sqlite3
1b5bd 43 72 65 61 74 65 49 64 45 78 70 72 28 7a 41 6c  CreateIdExpr(zAl
1b5be 69 61 73 32 29 3b 0a 20 20 70 45 31 63 20 3d 20  ias2);.  pE1c = 
1b5bf 73 71 6c 69 74 65 33 45 78 70 72 4f 72 46 72 65  sqlite3ExprOrFre
1b5c0 65 28 54 4b 5f 44 4f 54 2c 20 70 45 31 62 2c 20  e(TK_DOT, pE1b, 
1b5c1 70 45 31 61 2c 20 30 29 3b 0a 20 20 70 45 32 63  pE1a, 0);.  pE2c
1b5c2 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4f 72   = sqlite3ExprOr
1b5c3 46 72 65 65 28 54 4b 5f 44 4f 54 2c 20 70 45 32  Free(TK_DOT, pE2
1b5c4 62 2c 20 70 45 32 61 2c 20 30 29 3b 0a 20 20 70  b, pE2a, 0);.  p
1b5c5 45 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4f  E = sqlite3ExprO
1b5c6 72 46 72 65 65 28 54 4b 5f 45 51 2c 20 70 45 31  rFree(TK_EQ, pE1
1b5c7 63 2c 20 70 45 32 63 2c 20 30 29 3b 0a 20 20 69  c, pE2c, 0);.  i
1b5c8 66 28 20 70 45 20 29 7b 0a 20 20 20 20 45 78 70  f( pE ){.    Exp
1b5c9 72 53 65 74 50 72 6f 70 65 72 74 79 28 70 45 2c  rSetProperty(pE,
1b5ca 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 3b 0a 20   EP_FromJoin);. 
1b5cb 20 20 20 70 45 2d 3e 69 52 69 67 68 74 4a 6f 69     pE->iRightJoi
1b5cc 6e 54 61 62 6c 65 20 3d 20 69 52 69 67 68 74 4a  nTable = iRightJ
1b5cd 6f 69 6e 54 61 62 6c 65 3b 0a 20 20 7d 0a 20 20  oinTable;.  }.  
1b5ce 70 45 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  pE = sqlite3Expr
1b5cf 41 6e 64 28 2a 70 70 45 78 70 72 2c 20 70 45 29  And(*ppExpr, pE)
1b5d0 3b 0a 20 20 69 66 28 20 70 45 20 29 7b 0a 20 20  ;.  if( pE ){.  
1b5d1 20 20 2a 70 70 45 78 70 72 20 3d 20 70 45 3b 0a    *ppExpr = pE;.
1b5d2 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74    }.}../*.** Set
1b5d3 20 74 68 65 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e   the EP_FromJoin
1b5d4 20 70 72 6f 70 65 72 74 79 20 6f 6e 20 61 6c 6c   property on all
1b5d5 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 67 69   terms of the gi
1b5d6 76 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a  ven expression..
1b5d7 2a 2a 20 41 6e 64 20 73 65 74 20 74 68 65 20 45  ** And set the E
1b5d8 78 70 72 2e 69 52 69 67 68 74 4a 6f 69 6e 54 61  xpr.iRightJoinTa
1b5d9 62 6c 65 20 74 6f 20 69 54 61 62 6c 65 20 66 6f  ble to iTable fo
1b5da 72 20 65 76 65 72 79 20 74 65 72 6d 20 69 6e 20  r every term in 
1b5db 74 68 65 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f  the.** expressio
1b5dc 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 45 50 5f  n..**.** The EP_
1b5dd 46 72 6f 6d 4a 6f 69 6e 20 70 72 6f 70 65 72 74  FromJoin propert
1b5de 79 20 69 73 20 75 73 65 64 20 6f 6e 20 74 65 72  y is used on ter
1b5df 6d 73 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73  ms of an express
1b5e0 69 6f 6e 20 74 6f 20 74 65 6c 6c 0a 2a 2a 20 74  ion to tell.** t
1b5e1 68 65 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f  he LEFT OUTER JO
1b5e2 49 4e 20 70 72 6f 63 65 73 73 69 6e 67 20 6c 6f  IN processing lo
1b5e3 67 69 63 20 74 68 61 74 20 74 68 69 73 20 74 65  gic that this te
1b5e4 72 6d 20 69 73 20 70 61 72 74 20 6f 66 20 74 68  rm is part of th
1b5e5 65 0a 2a 2a 20 6a 6f 69 6e 20 72 65 73 74 72 69  e.** join restri
1b5e6 63 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64 20  ction specified 
1b5e7 69 6e 20 74 68 65 20 4f 4e 20 6f 72 20 55 53 49  in the ON or USI
1b5e8 4e 47 20 63 6c 61 75 73 65 20 61 6e 64 20 6e 6f  NG clause and no
1b5e9 74 20 61 20 70 61 72 74 0a 2a 2a 20 6f 66 20 74  t a part.** of t
1b5ea 68 65 20 6d 6f 72 65 20 67 65 6e 65 72 61 6c 20  he more general 
1b5eb 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20 54  WHERE clause.  T
1b5ec 68 65 73 65 20 74 65 72 6d 73 20 61 72 65 20 6d  hese terms are m
1b5ed 6f 76 65 64 20 6f 76 65 72 20 74 6f 20 74 68 65  oved over to the
1b5ee 0a 2a 2a 20 57 48 45 52 45 20 63 6c 61 75 73 65  .** WHERE clause
1b5ef 20 64 75 72 69 6e 67 20 6a 6f 69 6e 20 70 72 6f   during join pro
1b5f0 63 65 73 73 69 6e 67 20 62 75 74 20 77 65 20 6e  cessing but we n
1b5f1 65 65 64 20 74 6f 20 72 65 6d 65 6d 62 65 72 20  eed to remember 
1b5f2 74 68 61 74 20 74 68 65 79 0a 2a 2a 20 6f 72 69  that they.** ori
1b5f3 67 69 6e 61 74 65 64 20 69 6e 20 74 68 65 20 4f  ginated in the O
1b5f4 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 73  N or USING claus
1b5f5 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 45 78 70  e..**.** The Exp
1b5f6 72 2e 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c  r.iRightJoinTabl
1b5f7 65 20 74 65 6c 6c 73 20 74 68 65 20 57 48 45 52  e tells the WHER
1b5f8 45 20 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73  E clause process
1b5f9 69 6e 67 20 74 68 61 74 20 74 68 65 0a 2a 2a 20  ing that the.** 
1b5fa 65 78 70 72 65 73 73 69 6f 6e 20 64 65 70 65 6e  expression depen
1b5fb 64 73 20 6f 6e 20 74 61 62 6c 65 20 69 52 69 67  ds on table iRig
1b5fc 68 74 4a 6f 69 6e 54 61 62 6c 65 20 65 76 65 6e  htJoinTable even
1b5fd 20 69 66 20 74 68 61 74 20 74 61 62 6c 65 20 69   if that table i
1b5fe 73 20 6e 6f 74 0a 2a 2a 20 65 78 70 6c 69 63 69  s not.** explici
1b5ff 74 6c 79 20 6d 65 6e 74 69 6f 6e 65 64 20 69 6e  tly mentioned in
1b600 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 2e   the expression.
1b601 20 20 54 68 61 74 20 69 6e 66 6f 72 6d 61 74 69    That informati
1b602 6f 6e 20 69 73 20 6e 65 65 64 65 64 0a 2a 2a 20  on is needed.** 
1b603 66 6f 72 20 63 61 73 65 73 20 6c 69 6b 65 20 74  for cases like t
1b604 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 53 45  his:.**.**    SE
1b605 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 4c  LECT * FROM t1 L
1b606 45 46 54 20 4a 4f 49 4e 20 74 32 20 4f 4e 20 74  EFT JOIN t2 ON t
1b607 31 2e 61 3d 74 32 2e 62 20 41 4e 44 20 74 31 2e  1.a=t2.b AND t1.
1b608 78 3d 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 77 68  x=5.**.** The wh
1b609 65 72 65 20 63 6c 61 75 73 65 20 6e 65 65 64 73  ere clause needs
1b60a 20 74 6f 20 64 65 66 65 72 20 74 68 65 20 68 61   to defer the ha
1b60b 6e 64 6c 69 6e 67 20 6f 66 20 74 68 65 20 74 31  ndling of the t1
1b60c 2e 78 3d 35 0a 2a 2a 20 74 65 72 6d 20 75 6e 74  .x=5.** term unt
1b60d 69 6c 20 61 66 74 65 72 20 74 68 65 20 74 32 20  il after the t2 
1b60e 6c 6f 6f 70 20 6f 66 20 74 68 65 20 6a 6f 69 6e  loop of the join
1b60f 2e 20 20 49 6e 20 74 68 61 74 20 77 61 79 2c 20  .  In that way, 
1b610 61 0a 2a 2a 20 4e 55 4c 4c 20 74 32 20 72 6f 77  a.** NULL t2 row
1b611 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 72 74 65   will be inserte
1b612 64 20 77 68 65 6e 65 76 65 72 20 74 31 2e 78 21  d whenever t1.x!
1b613 3d 35 2e 20 20 49 66 20 77 65 20 64 6f 20 6e 6f  =5.  If we do no
1b614 74 0a 2a 2a 20 64 65 66 65 72 20 74 68 65 20 68  t.** defer the h
1b615 61 6e 64 6c 69 6e 67 20 6f 66 20 74 31 2e 78 3d  andling of t1.x=
1b616 35 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 70 72  5, it will be pr
1b617 6f 63 65 73 73 65 64 20 69 6d 6d 65 64 69 61 74  ocessed immediat
1b618 65 6c 79 0a 2a 2a 20 61 66 74 65 72 20 74 68 65  ely.** after the
1b619 20 74 31 20 6c 6f 6f 70 20 61 6e 64 20 72 6f 77   t1 loop and row
1b61a 73 20 77 69 74 68 20 74 31 2e 78 21 3d 35 20 77  s with t1.x!=5 w
1b61b 69 6c 6c 20 6e 65 76 65 72 20 61 70 70 65 61 72  ill never appear
1b61c 20 69 6e 0a 2a 2a 20 74 68 65 20 6f 75 74 70 75   in.** the outpu
1b61d 74 2c 20 77 68 69 63 68 20 69 73 20 69 6e 63 6f  t, which is inco
1b61e 72 72 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  rrect..*/.static
1b61f 20 76 6f 69 64 20 73 65 74 4a 6f 69 6e 45 78 70   void setJoinExp
1b620 72 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 69  r(Expr *p, int i
1b621 54 61 62 6c 65 29 7b 0a 20 20 77 68 69 6c 65 28  Table){.  while(
1b622 20 70 20 29 7b 0a 20 20 20 20 45 78 70 72 53 65   p ){.    ExprSe
1b623 74 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f  tProperty(p, EP_
1b624 46 72 6f 6d 4a 6f 69 6e 29 3b 0a 20 20 20 20 70  FromJoin);.    p
1b625 2d 3e 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c  ->iRightJoinTabl
1b626 65 20 3d 20 69 54 61 62 6c 65 3b 0a 20 20 20 20  e = iTable;.    
1b627 73 65 74 4a 6f 69 6e 45 78 70 72 28 70 2d 3e 70  setJoinExpr(p->p
1b628 4c 65 66 74 2c 20 69 54 61 62 6c 65 29 3b 0a 20  Left, iTable);. 
1b629 20 20 20 70 20 3d 20 70 2d 3e 70 52 69 67 68 74     p = p->pRight
1b62a 3b 0a 20 20 7d 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ;.  } .}../*.** 
1b62b 54 68 69 73 20 72 6f 75 74 69 6e 65 20 70 72 6f  This routine pro
1b62c 63 65 73 73 65 73 20 74 68 65 20 6a 6f 69 6e 20  cesses the join 
1b62d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20  information for 
1b62e 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  a SELECT stateme
1b62f 6e 74 2e 0a 2a 2a 20 4f 4e 20 61 6e 64 20 55 53  nt..** ON and US
1b630 49 4e 47 20 63 6c 61 75 73 65 73 20 61 72 65 20  ING clauses are 
1b631 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 65  converted into e
1b632 78 74 72 61 20 74 65 72 6d 73 20 6f 66 20 74 68  xtra terms of th
1b633 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 0a  e WHERE clause..
1b634 2a 2a 20 4e 41 54 55 52 41 4c 20 6a 6f 69 6e 73  ** NATURAL joins
1b635 20 61 6c 73 6f 20 63 72 65 61 74 65 20 65 78 74   also create ext
1b636 72 61 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  ra WHERE clause 
1b637 74 65 72 6d 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  terms..**.** The
1b638 20 74 65 72 6d 73 20 6f 66 20 61 20 46 52 4f 4d   terms of a FROM
1b639 20 63 6c 61 75 73 65 20 61 72 65 20 63 6f 6e 74   clause are cont
1b63a 61 69 6e 65 64 20 69 6e 20 74 68 65 20 53 65 6c  ained in the Sel
1b63b 65 63 74 2e 70 53 72 63 20 73 74 72 75 63 74 75  ect.pSrc structu
1b63c 72 65 2e 0a 2a 2a 20 54 68 65 20 6c 65 66 74 20  re..** The left 
1b63d 6d 6f 73 74 20 74 61 62 6c 65 20 69 73 20 74 68  most table is th
1b63e 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 69 6e  e first entry in
1b63f 20 53 65 6c 65 63 74 2e 70 53 72 63 2e 20 20 54   Select.pSrc.  T
1b640 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 0a 2a 2a  he right-most.**
1b641 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 6c 61   table is the la
1b642 73 74 20 65 6e 74 72 79 2e 20 20 54 68 65 20 6a  st entry.  The j
1b643 6f 69 6e 20 6f 70 65 72 61 74 6f 72 20 69 73 20  oin operator is 
1b644 68 65 6c 64 20 69 6e 20 74 68 65 20 65 6e 74 72  held in the entr
1b645 79 20 74 6f 0a 2a 2a 20 74 68 65 20 6c 65 66 74  y to.** the left
1b646 2e 20 20 54 68 75 73 20 65 6e 74 72 79 20 30 20  .  Thus entry 0 
1b647 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6a 6f 69  contains the joi
1b648 6e 20 6f 70 65 72 61 74 6f 72 20 66 6f 72 20 74  n operator for t
1b649 68 65 20 6a 6f 69 6e 20 62 65 74 77 65 65 6e 0a  he join between.
1b64a 2a 2a 20 65 6e 74 72 69 65 73 20 30 20 61 6e 64  ** entries 0 and
1b64b 20 31 2e 20 20 41 6e 79 20 4f 4e 20 6f 72 20 55   1.  Any ON or U
1b64c 53 49 4e 47 20 63 6c 61 75 73 65 73 20 61 73 73  SING clauses ass
1b64d 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
1b64e 20 6a 6f 69 6e 20 61 72 65 0a 2a 2a 20 61 6c 73   join are.** als
1b64f 6f 20 61 74 74 61 63 68 65 64 20 74 6f 20 74 68  o attached to th
1b650 65 20 6c 65 66 74 20 65 6e 74 72 79 2e 0a 2a 2a  e left entry..**
1b651 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
1b652 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d   returns the num
1b653 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20 65 6e  ber of errors en
1b654 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2f 0a 73 74  countered..*/.st
1b655 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 50  atic int sqliteP
1b656 72 6f 63 65 73 73 4a 6f 69 6e 28 50 61 72 73 65  rocessJoin(Parse
1b657 20 2a 70 50 61 72 73 65 2c 20 53 65 6c 65 63 74   *pParse, Select
1b658 20 2a 70 29 7b 0a 20 20 53 72 63 4c 69 73 74 20   *p){.  SrcList 
1b659 2a 70 53 72 63 3b 20 20 20 20 20 20 20 20 20 20  *pSrc;          
1b65a 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 74          /* All t
1b65b 61 62 6c 65 73 20 69 6e 20 74 68 65 20 46 52 4f  ables in the FRO
1b65c 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e  M clause */.  in
1b65d 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20  t i, j;         
1b65e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1b65f 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a   Loop counters *
1b660 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69  /.  struct SrcLi
1b661 73 74 5f 69 74 65 6d 20 2a 70 4c 65 66 74 3b 20  st_item *pLeft; 
1b662 20 20 20 20 2f 2a 20 4c 65 66 74 20 74 61 62 6c      /* Left tabl
1b663 65 20 62 65 69 6e 67 20 6a 6f 69 6e 65 64 20 2a  e being joined *
1b664 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69  /.  struct SrcLi
1b665 73 74 5f 69 74 65 6d 20 2a 70 52 69 67 68 74 3b  st_item *pRight;
1b666 20 20 20 20 2f 2a 20 52 69 67 68 74 20 74 61 62      /* Right tab
1b667 6c 65 20 62 65 69 6e 67 20 6a 6f 69 6e 65 64 20  le being joined 
1b668 2a 2f 0a 0a 20 20 70 53 72 63 20 3d 20 70 2d 3e  */..  pSrc = p->
1b669 70 53 72 63 3b 0a 20 20 70 4c 65 66 74 20 3d 20  pSrc;.  pLeft = 
1b66a 26 70 53 72 63 2d 3e 61 5b 30 5d 3b 0a 20 20 70  &pSrc->a[0];.  p
1b66b 52 69 67 68 74 20 3d 20 26 70 4c 65 66 74 5b 31  Right = &pLeft[1
1b66c 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  ];.  for(i=0; i<
1b66d 70 53 72 63 2d 3e 6e 53 72 63 2d 31 3b 20 69 2b  pSrc->nSrc-1; i+
1b66e 2b 2c 20 70 52 69 67 68 74 2b 2b 2c 20 70 4c 65  +, pRight++, pLe
1b66f 66 74 2b 2b 29 7b 0a 20 20 20 20 54 61 62 6c 65  ft++){.    Table
1b670 20 2a 70 4c 65 66 74 54 61 62 20 3d 20 70 4c 65   *pLeftTab = pLe
1b671 66 74 2d 3e 70 54 61 62 3b 0a 20 20 20 20 54 61  ft->pTab;.    Ta
1b672 62 6c 65 20 2a 70 52 69 67 68 74 54 61 62 20 3d  ble *pRightTab =
1b673 20 70 52 69 67 68 74 2d 3e 70 54 61 62 3b 0a 0a   pRight->pTab;..
1b674 20 20 20 20 69 66 28 20 70 4c 65 66 74 54 61 62      if( pLeftTab
1b675 3d 3d 30 20 7c 7c 20 70 52 69 67 68 74 54 61 62  ==0 || pRightTab
1b676 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ==0 ) continue;.
1b677 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 74 68 65  .    /* When the
1b678 20 4e 41 54 55 52 41 4c 20 6b 65 79 77 6f 72 64   NATURAL keyword
1b679 20 69 73 20 70 72 65 73 65 6e 74 2c 20 61 64 64   is present, add
1b67a 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65   WHERE clause te
1b67b 72 6d 73 20 66 6f 72 0a 20 20 20 20 2a 2a 20 65  rms for.    ** e
1b67c 76 65 72 79 20 63 6f 6c 75 6d 6e 20 74 68 61 74  very column that
1b67d 20 74 68 65 20 74 77 6f 20 74 61 62 6c 65 73 20   the two tables 
1b67e 68 61 76 65 20 69 6e 20 63 6f 6d 6d 6f 6e 2e 0a  have in common..
1b67f 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
1b680 52 69 67 68 74 2d 3e 6a 6f 69 6e 74 79 70 65 20  Right->jointype 
1b681 26 20 4a 54 5f 4e 41 54 55 52 41 4c 20 29 7b 0a  & JT_NATURAL ){.
1b682 20 20 20 20 20 20 69 66 28 20 70 52 69 67 68 74        if( pRight
1b683 2d 3e 70 4f 6e 20 7c 7c 20 70 52 69 67 68 74 2d  ->pOn || pRight-
1b684 3e 70 55 73 69 6e 67 20 29 7b 0a 20 20 20 20 20  >pUsing ){.     
1b685 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1b686 73 67 28 70 50 61 72 73 65 2c 20 22 61 20 4e 41  sg(pParse, "a NA
1b687 54 55 52 41 4c 20 6a 6f 69 6e 20 6d 61 79 20 6e  TURAL join may n
1b688 6f 74 20 68 61 76 65 20 22 0a 20 20 20 20 20 20  ot have ".      
1b689 20 20 20 20 20 22 61 6e 20 4f 4e 20 6f 72 20 55       "an ON or U
1b68a 53 49 4e 47 20 63 6c 61 75 73 65 22 2c 20 30 29  SING clause", 0)
1b68b 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
1b68c 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   1;.      }.    
1b68d 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 4c 65    for(j=0; j<pLe
1b68e 66 74 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b  ftTab->nCol; j++
1b68f 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20  ){.        char 
1b690 2a 7a 4e 61 6d 65 20 3d 20 70 4c 65 66 74 54 61  *zName = pLeftTa
1b691 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65  b->aCol[j].zName
1b692 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 6f  ;.        if( co
1b693 6c 75 6d 6e 49 6e 64 65 78 28 70 52 69 67 68 74  lumnIndex(pRight
1b694 54 61 62 2c 20 7a 4e 61 6d 65 29 3e 3d 30 20 29  Tab, zName)>=0 )
1b695 7b 0a 20 20 20 20 20 20 20 20 20 20 61 64 64 57  {.          addW
1b696 68 65 72 65 54 65 72 6d 28 7a 4e 61 6d 65 2c 20  hereTerm(zName, 
1b697 70 4c 65 66 74 54 61 62 2c 20 70 4c 65 66 74 2d  pLeftTab, pLeft-
1b698 3e 7a 41 6c 69 61 73 2c 20 0a 20 20 20 20 20 20  >zAlias, .      
1b699 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b69a 20 20 20 20 20 20 20 20 70 52 69 67 68 74 54 61          pRightTa
1b69b 62 2c 20 70 52 69 67 68 74 2d 3e 7a 41 6c 69 61  b, pRight->zAlia
1b69c 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  s,.             
1b69d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b69e 20 70 52 69 67 68 74 2d 3e 69 43 75 72 73 6f 72   pRight->iCursor
1b69f 2c 20 26 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20  , &p->pWhere);. 
1b6a0 20 20 20 20 20 20 20 20 20 0a 20 20 20 20 20 20           .      
1b6a1 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
1b6a2 7d 0a 0a 20 20 20 20 2f 2a 20 44 69 73 61 6c 6c  }..    /* Disall
1b6a3 6f 77 20 62 6f 74 68 20 4f 4e 20 61 6e 64 20 55  ow both ON and U
1b6a4 53 49 4e 47 20 63 6c 61 75 73 65 73 20 69 6e 20  SING clauses in 
1b6a5 74 68 65 20 73 61 6d 65 20 6a 6f 69 6e 0a 20 20  the same join.  
1b6a6 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 52 69    */.    if( pRi
1b6a7 67 68 74 2d 3e 70 4f 6e 20 26 26 20 70 52 69 67  ght->pOn && pRig
1b6a8 68 74 2d 3e 70 55 73 69 6e 67 20 29 7b 0a 20 20  ht->pUsing ){.  
1b6a9 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1b6aa 4d 73 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e  Msg(pParse, "can
1b6ab 6e 6f 74 20 68 61 76 65 20 62 6f 74 68 20 4f 4e  not have both ON
1b6ac 20 61 6e 64 20 55 53 49 4e 47 20 22 0a 20 20 20   and USING ".   
1b6ad 20 20 20 20 20 22 63 6c 61 75 73 65 73 20 69 6e       "clauses in
1b6ae 20 74 68 65 20 73 61 6d 65 20 6a 6f 69 6e 22 29   the same join")
1b6af 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  ;.      return 1
1b6b0 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
1b6b1 41 64 64 20 74 68 65 20 4f 4e 20 63 6c 61 75 73  Add the ON claus
1b6b2 65 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20  e to the end of 
1b6b3 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
1b6b4 2c 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 0a 20  , connected by. 
1b6b5 20 20 20 2a 2a 20 61 6e 20 41 4e 44 20 6f 70 65     ** an AND ope
1b6b6 72 61 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20  rator..    */.  
1b6b7 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 70 4f    if( pRight->pO
1b6b8 6e 20 29 7b 0a 20 20 20 20 20 20 73 65 74 4a 6f  n ){.      setJo
1b6b9 69 6e 45 78 70 72 28 70 52 69 67 68 74 2d 3e 70  inExpr(pRight->p
1b6ba 4f 6e 2c 20 70 52 69 67 68 74 2d 3e 69 43 75 72  On, pRight->iCur
1b6bb 73 6f 72 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70  sor);.      p->p
1b6bc 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45  Where = sqlite3E
1b6bd 78 70 72 41 6e 64 28 70 2d 3e 70 57 68 65 72 65  xprAnd(p->pWhere
1b6be 2c 20 70 52 69 67 68 74 2d 3e 70 4f 6e 29 3b 0a  , pRight->pOn);.
1b6bf 20 20 20 20 20 20 70 52 69 67 68 74 2d 3e 70 4f        pRight->pO
1b6c0 6e 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20  n = 0;.    }..  
1b6c1 20 20 2f 2a 20 43 72 65 61 74 65 20 65 78 74 72    /* Create extr
1b6c2 61 20 74 65 72 6d 73 20 6f 6e 20 74 68 65 20 57  a terms on the W
1b6c3 48 45 52 45 20 63 6c 61 75 73 65 20 66 6f 72 20  HERE clause for 
1b6c4 65 61 63 68 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65  each column name
1b6c5 64 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20  d.    ** in the 
1b6c6 55 53 49 4e 47 20 63 6c 61 75 73 65 2e 20 20 45  USING clause.  E
1b6c7 78 61 6d 70 6c 65 3a 20 49 66 20 74 68 65 20 74  xample: If the t
1b6c8 77 6f 20 74 61 62 6c 65 73 20 74 6f 20 62 65 20  wo tables to be 
1b6c9 6a 6f 69 6e 65 64 20 61 72 65 20 0a 20 20 20 20  joined are .    
1b6ca 2a 2a 20 41 20 61 6e 64 20 42 20 61 6e 64 20 74  ** A and B and t
1b6cb 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20  he USING clause 
1b6cc 6e 61 6d 65 73 20 58 2c 20 59 2c 20 61 6e 64 20  names X, Y, and 
1b6cd 5a 2c 20 74 68 65 6e 20 61 64 64 20 74 68 69 73  Z, then add this
1b6ce 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 57  .    ** to the W
1b6cf 48 45 52 45 20 63 6c 61 75 73 65 3a 20 20 20 20  HERE clause:    
1b6d0 41 2e 58 3d 42 2e 58 20 41 4e 44 20 41 2e 59 3d  A.X=B.X AND A.Y=
1b6d1 42 2e 59 20 41 4e 44 20 41 2e 5a 3d 42 2e 5a 0a  B.Y AND A.Z=B.Z.
1b6d2 20 20 20 20 2a 2a 20 52 65 70 6f 72 74 20 61 6e      ** Report an
1b6d3 20 65 72 72 6f 72 20 69 66 20 61 6e 79 20 63 6f   error if any co
1b6d4 6c 75 6d 6e 20 6d 65 6e 74 69 6f 6e 65 64 20 69  lumn mentioned i
1b6d5 6e 20 74 68 65 20 55 53 49 4e 47 20 63 6c 61 75  n the USING clau
1b6d6 73 65 20 69 73 0a 20 20 20 20 2a 2a 20 6e 6f 74  se is.    ** not
1b6d7 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 62 6f   contained in bo
1b6d8 74 68 20 74 61 62 6c 65 73 20 74 6f 20 62 65 20  th tables to be 
1b6d9 6a 6f 69 6e 65 64 2e 0a 20 20 20 20 2a 2f 0a 20  joined..    */. 
1b6da 20 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 70     if( pRight->p
1b6db 55 73 69 6e 67 20 29 7b 0a 20 20 20 20 20 20 49  Using ){.      I
1b6dc 64 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70  dList *pList = p
1b6dd 52 69 67 68 74 2d 3e 70 55 73 69 6e 67 3b 0a 20  Right->pUsing;. 
1b6de 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
1b6df 70 4c 69 73 74 2d 3e 6e 49 64 3b 20 6a 2b 2b 29  pList->nId; j++)
1b6e0 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a  {.        char *
1b6e1 7a 4e 61 6d 65 20 3d 20 70 4c 69 73 74 2d 3e 61  zName = pList->a
1b6e2 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20  [j].zName;.     
1b6e3 20 20 20 69 66 28 20 63 6f 6c 75 6d 6e 49 6e 64     if( columnInd
1b6e4 65 78 28 70 4c 65 66 74 54 61 62 2c 20 7a 4e 61  ex(pLeftTab, zNa
1b6e5 6d 65 29 3c 30 20 7c 7c 20 63 6f 6c 75 6d 6e 49  me)<0 || columnI
1b6e6 6e 64 65 78 28 70 52 69 67 68 74 54 61 62 2c 20  ndex(pRightTab, 
1b6e7 7a 4e 61 6d 65 29 3c 30 20 29 7b 0a 20 20 20 20  zName)<0 ){.    
1b6e8 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
1b6e9 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 63  orMsg(pParse, "c
1b6ea 61 6e 6e 6f 74 20 6a 6f 69 6e 20 75 73 69 6e 67  annot join using
1b6eb 20 63 6f 6c 75 6d 6e 20 25 73 20 2d 20 63 6f 6c   column %s - col
1b6ec 75 6d 6e 20 22 0a 20 20 20 20 20 20 20 20 20 20  umn ".          
1b6ed 20 20 22 6e 6f 74 20 70 72 65 73 65 6e 74 20 69    "not present i
1b6ee 6e 20 62 6f 74 68 20 74 61 62 6c 65 73 22 2c 20  n both tables", 
1b6ef 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20  zName);.        
1b6f0 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
1b6f1 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 64      }.        ad
1b6f2 64 57 68 65 72 65 54 65 72 6d 28 7a 4e 61 6d 65  dWhereTerm(zName
1b6f3 2c 20 70 4c 65 66 74 54 61 62 2c 20 70 4c 65 66  , pLeftTab, pLef
1b6f4 74 2d 3e 7a 41 6c 69 61 73 2c 20 0a 20 20 20 20  t->zAlias, .    
1b6f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b6f6 20 20 20 20 20 20 20 20 70 52 69 67 68 74 54 61          pRightTa
1b6f7 62 2c 20 70 52 69 67 68 74 2d 3e 7a 41 6c 69 61  b, pRight->zAlia
1b6f8 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  s,.             
1b6f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
1b6fa 52 69 67 68 74 2d 3e 69 43 75 72 73 6f 72 2c 20  Right->iCursor, 
1b6fb 26 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20 20  &p->pWhere);.   
1b6fc 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
1b6fd 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
1b6fe 0a 2a 2a 20 49 6e 73 65 72 74 20 63 6f 64 65 20  .** Insert code 
1b6ff 69 6e 74 6f 20 22 76 22 20 74 68 61 74 20 77 69  into "v" that wi
1b700 6c 6c 20 70 75 73 68 20 74 68 65 20 72 65 63 6f  ll push the reco
1b701 72 64 20 6f 6e 20 74 68 65 20 74 6f 70 20 6f 66  rd on the top of
1b702 20 74 68 65 0a 2a 2a 20 73 74 61 63 6b 20 69 6e   the.** stack in
1b703 74 6f 20 74 68 65 20 73 6f 72 74 65 72 2e 0a 2a  to the sorter..*
1b704 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 75  /.static void pu
1b705 73 68 4f 6e 74 6f 53 6f 72 74 65 72 28 0a 20 20  shOntoSorter(.  
1b706 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
1b707 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72         /* Parser
1b708 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78   context */.  Ex
1b709 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79  prList *pOrderBy
1b70a 2c 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45  ,    /* The ORDE
1b70b 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20  R BY clause */. 
1b70c 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
1b70d 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 77          /* The w
1b70e 68 6f 6c 65 20 53 45 4c 45 43 54 20 73 74 61 74  hole SELECT stat
1b70f 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 56 64  ement */.){.  Vd
1b710 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
1b711 70 56 64 62 65 3b 0a 20 20 73 71 6c 69 74 65 33  pVdbe;.  sqlite3
1b712 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74  ExprCodeExprList
1b713 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65 72 42  (pParse, pOrderB
1b714 79 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  y);.  sqlite3Vdb
1b715 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 53 65 71  eAddOp(v, OP_Seq
1b716 75 65 6e 63 65 2c 20 70 4f 72 64 65 72 42 79 2d  uence, pOrderBy-
1b717 3e 69 45 43 75 72 73 6f 72 2c 20 30 29 3b 0a 20  >iECursor, 0);. 
1b718 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1b719 70 28 76 2c 20 4f 50 5f 50 75 6c 6c 2c 20 70 4f  p(v, OP_Pull, pO
1b71a 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 20 2b 20  rderBy->nExpr + 
1b71b 31 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  1, 0);.  sqlite3
1b71c 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1b71d 4d 61 6b 65 52 65 63 6f 72 64 2c 20 70 4f 72 64  MakeRecord, pOrd
1b71e 65 72 42 79 2d 3e 6e 45 78 70 72 20 2b 20 32 2c  erBy->nExpr + 2,
1b71f 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64   0);.  sqlite3Vd
1b720 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 64  beAddOp(v, OP_Id
1b721 78 49 6e 73 65 72 74 2c 20 70 4f 72 64 65 72 42  xInsert, pOrderB
1b722 79 2d 3e 69 45 43 75 72 73 6f 72 2c 20 30 29 3b  y->iECursor, 0);
1b723 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e  .  if( pSelect->
1b724 69 4c 69 6d 69 74 3e 3d 30 20 29 7b 0a 20 20 20  iLimit>=0 ){.   
1b725 20 69 6e 74 20 61 64 64 72 31 2c 20 61 64 64 72   int addr1, addr
1b726 32 3b 0a 20 20 20 20 61 64 64 72 31 20 3d 20 73  2;.    addr1 = s
1b727 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1b728 76 2c 20 4f 50 5f 49 66 4d 65 6d 5a 65 72 6f 2c  v, OP_IfMemZero,
1b729 20 70 53 65 6c 65 63 74 2d 3e 69 4c 69 6d 69 74   pSelect->iLimit
1b72a 2b 31 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69  +1, 0);.    sqli
1b72b 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1b72c 4f 50 5f 4d 65 6d 49 6e 63 72 2c 20 2d 31 2c 20  OP_MemIncr, -1, 
1b72d 70 53 65 6c 65 63 74 2d 3e 69 4c 69 6d 69 74 2b  pSelect->iLimit+
1b72e 31 29 3b 0a 20 20 20 20 61 64 64 72 32 20 3d 20  1);.    addr2 = 
1b72f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1b730 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
1b731 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0);.    sqlite3V
1b732 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
1b733 64 64 72 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  ddr1);.    sqlit
1b734 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1b735 50 5f 4c 61 73 74 2c 20 70 4f 72 64 65 72 42 79  P_Last, pOrderBy
1b736 2d 3e 69 45 43 75 72 73 6f 72 2c 20 30 29 3b 0a  ->iECursor, 0);.
1b737 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1b738 64 64 4f 70 28 76 2c 20 4f 50 5f 44 65 6c 65 74  ddOp(v, OP_Delet
1b739 65 2c 20 70 4f 72 64 65 72 42 79 2d 3e 69 45 43  e, pOrderBy->iEC
1b73a 75 72 73 6f 72 2c 20 30 29 3b 0a 20 20 20 20 73  ursor, 0);.    s
1b73b 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
1b73c 72 65 28 76 2c 20 61 64 64 72 32 29 3b 0a 20 20  re(v, addr2);.  
1b73d 20 20 70 53 65 6c 65 63 74 2d 3e 69 4c 69 6d 69    pSelect->iLimi
1b73e 74 20 3d 20 2d 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f  t = -1;.  }.}../
1b73f 2a 0a 2a 2a 20 41 64 64 20 63 6f 64 65 20 74 6f  *.** Add code to
1b740 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 4f   implement the O
1b741 46 46 53 45 54 0a 2a 2f 0a 73 74 61 74 69 63 20  FFSET.*/.static 
1b742 76 6f 69 64 20 63 6f 64 65 4f 66 66 73 65 74 28  void codeOffset(
1b743 0a 20 20 56 64 62 65 20 2a 76 2c 20 20 20 20 20  .  Vdbe *v,     
1b744 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65       /* Generate
1b745 20 63 6f 64 65 20 69 6e 74 6f 20 74 68 69 73 20   code into this 
1b746 56 4d 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  VM */.  Select *
1b747 70 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  p,        /* The
1b748 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
1b749 74 20 62 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f  t being coded */
1b74a 0a 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75 65  .  int iContinue
1b74b 2c 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72  ,    /* Jump her
1b74c 65 20 74 6f 20 73 6b 69 70 20 74 68 65 20 63 75  e to skip the cu
1b74d 72 72 65 6e 74 20 72 65 63 6f 72 64 20 2a 2f 0a  rrent record */.
1b74e 20 20 69 6e 74 20 6e 50 6f 70 20 20 20 20 20 20    int nPop      
1b74f 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1b750 20 74 69 6d 65 73 20 74 6f 20 70 6f 70 20 73 74   times to pop st
1b751 61 63 6b 20 77 68 65 6e 20 6a 75 6d 70 69 6e 67  ack when jumping
1b752 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 70 2d 3e   */.){.  if( p->
1b753 69 4f 66 66 73 65 74 3e 3d 30 20 26 26 20 69 43  iOffset>=0 && iC
1b754 6f 6e 74 69 6e 75 65 21 3d 30 20 29 7b 0a 20 20  ontinue!=0 ){.  
1b755 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 20 20    int addr;.    
1b756 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1b757 28 76 2c 20 4f 50 5f 4d 65 6d 49 6e 63 72 2c 20  (v, OP_MemIncr, 
1b758 2d 31 2c 20 70 2d 3e 69 4f 66 66 73 65 74 29 3b  -1, p->iOffset);
1b759 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69  .    addr = sqli
1b75a 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1b75b 4f 50 5f 49 66 4d 65 6d 4e 65 67 2c 20 70 2d 3e  OP_IfMemNeg, p->
1b75c 69 4f 66 66 73 65 74 2c 20 30 29 3b 0a 20 20 20  iOffset, 0);.   
1b75d 20 69 66 28 20 6e 50 6f 70 3e 30 20 29 7b 0a 20   if( nPop>0 ){. 
1b75e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1b75f 41 64 64 4f 70 28 76 2c 20 4f 50 5f 50 6f 70 2c  AddOp(v, OP_Pop,
1b760 20 6e 50 6f 70 2c 20 30 29 3b 0a 20 20 20 20 7d   nPop, 0);.    }
1b761 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1b762 41 64 64 4f 70 28 76 2c 20 4f 50 5f 47 6f 74 6f  AddOp(v, OP_Goto
1b763 2c 20 30 2c 20 69 43 6f 6e 74 69 6e 75 65 29 3b  , 0, iContinue);
1b764 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74  .    VdbeComment
1b765 28 28 76 2c 20 22 23 20 73 6b 69 70 20 4f 46 46  ((v, "# skip OFF
1b766 53 45 54 20 72 65 63 6f 72 64 73 22 29 29 3b 0a  SET records"));.
1b767 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
1b768 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 29  umpHere(v, addr)
1b769 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  ;.  }.}../*.** A
1b76a 64 64 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c  dd code that wil
1b76b 6c 20 63 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20  l check to make 
1b76c 73 75 72 65 20 74 68 65 20 74 6f 70 20 4e 20 65  sure the top N e
1b76d 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 0a 2a  lements of the.*
1b76e 2a 20 73 74 61 63 6b 20 61 72 65 20 64 69 73 74  * stack are dist
1b76f 69 6e 63 74 2e 20 20 69 54 61 62 20 69 73 20 61  inct.  iTab is a
1b770 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 74   sorting index t
1b771 68 61 74 20 68 6f 6c 64 73 20 70 72 65 76 69 6f  hat holds previo
1b772 75 73 6c 79 0a 2a 2a 20 73 65 65 6e 20 63 6f 6d  usly.** seen com
1b773 62 69 6e 61 74 69 6f 6e 73 20 6f 66 20 74 68 65  binations of the
1b774 20 4e 20 76 61 6c 75 65 73 2e 20 20 41 20 6e 65   N values.  A ne
1b775 77 20 65 6e 74 72 79 20 69 73 20 6d 61 64 65 20  w entry is made 
1b776 69 6e 20 69 54 61 62 0a 2a 2a 20 69 66 20 74 68  in iTab.** if th
1b777 65 20 63 75 72 72 65 6e 74 20 4e 20 76 61 6c 75  e current N valu
1b778 65 73 20 61 72 65 20 6e 65 77 2e 0a 2a 2a 0a 2a  es are new..**.*
1b779 2a 20 41 20 6a 75 6d 70 20 74 6f 20 61 64 64 72  * A jump to addr
1b77a 52 65 70 65 61 74 20 69 73 20 6d 61 64 65 20 61  Repeat is made a
1b77b 6e 64 20 74 68 65 20 4e 2b 31 20 76 61 6c 75 65  nd the N+1 value
1b77c 73 20 61 72 65 20 70 6f 70 70 65 64 20 66 72 6f  s are popped fro
1b77d 6d 20 74 68 65 0a 2a 2a 20 73 74 61 63 6b 20 69  m the.** stack i
1b77e 66 20 74 68 65 20 74 6f 70 20 4e 20 65 6c 65 6d  f the top N elem
1b77f 65 6e 74 73 20 61 72 65 20 6e 6f 74 20 64 69 73  ents are not dis
1b780 74 69 6e 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  tinct..*/.static
1b781 20 76 6f 69 64 20 63 6f 64 65 44 69 73 74 69 6e   void codeDistin
1b782 63 74 28 0a 20 20 56 64 62 65 20 2a 76 2c 20 20  ct(.  Vdbe *v,  
1b783 20 20 20 20 20 20 20 20 20 2f 2a 20 47 65 6e 65           /* Gene
1b784 72 61 74 65 20 63 6f 64 65 20 69 6e 74 6f 20 74  rate code into t
1b785 68 69 73 20 56 4d 20 2a 2f 0a 20 20 69 6e 74 20  his VM */.  int 
1b786 69 54 61 62 2c 20 20 20 20 20 20 20 20 20 20 2f  iTab,          /
1b787 2a 20 41 20 73 6f 72 74 69 6e 67 20 69 6e 64 65  * A sorting inde
1b788 78 20 75 73 65 64 20 74 6f 20 74 65 73 74 20 66  x used to test f
1b789 6f 72 20 64 69 73 74 69 6e 63 74 6e 65 73 73 20  or distinctness 
1b78a 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 52 65 70  */.  int addrRep
1b78b 65 61 74 2c 20 20 20 20 2f 2a 20 4a 75 6d 70 20  eat,    /* Jump 
1b78c 74 6f 20 68 65 72 65 20 69 66 20 6e 6f 74 20 64  to here if not d
1b78d 69 73 74 69 6e 63 74 20 2a 2f 0a 20 20 69 6e 74  istinct */.  int
1b78e 20 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20   N              
1b78f 2f 2a 20 54 68 65 20 74 6f 70 20 4e 20 65 6c 65  /* The top N ele
1b790 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 73 74 61  ments of the sta
1b791 63 6b 20 6d 75 73 74 20 62 65 20 64 69 73 74 69  ck must be disti
1b792 6e 63 74 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69  nct */.){.  sqli
1b793 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1b794 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 2d  OP_MakeRecord, -
1b795 4e 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  N, 0);.  sqlite3
1b796 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1b797 44 69 73 74 69 6e 63 74 2c 20 69 54 61 62 2c 20  Distinct, iTab, 
1b798 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
1b799 6e 74 41 64 64 72 28 76 29 2b 33 29 3b 0a 20 20  ntAddr(v)+3);.  
1b79a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1b79b 28 76 2c 20 4f 50 5f 50 6f 70 2c 20 4e 2b 31 2c  (v, OP_Pop, N+1,
1b79c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64   0);.  sqlite3Vd
1b79d 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 47 6f  beAddOp(v, OP_Go
1b79e 74 6f 2c 20 30 2c 20 61 64 64 72 52 65 70 65 61  to, 0, addrRepea
1b79f 74 29 3b 0a 20 20 56 64 62 65 43 6f 6d 6d 65 6e  t);.  VdbeCommen
1b7a0 74 28 28 76 2c 20 22 23 20 73 6b 69 70 20 69 6e  t((v, "# skip in
1b7a1 64 69 73 74 69 6e 63 74 20 72 65 63 6f 72 64 73  distinct records
1b7a2 22 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  "));.  sqlite3Vd
1b7a3 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 64  beAddOp(v, OP_Id
1b7a4 78 49 6e 73 65 72 74 2c 20 69 54 61 62 2c 20 30  xInsert, iTab, 0
1b7a5 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  );.}../*.** Gene
1b7a6 72 61 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65  rate an error me
1b7a7 73 73 61 67 65 20 77 68 65 6e 20 61 20 53 45 4c  ssage when a SEL
1b7a8 45 43 54 20 69 73 20 75 73 65 64 20 77 69 74 68  ECT is used with
1b7a9 69 6e 20 61 20 73 75 62 65 78 70 72 65 73 73 69  in a subexpressi
1b7aa 6f 6e 0a 2a 2a 20 28 65 78 61 6d 70 6c 65 3a 20  on.** (example: 
1b7ab 20 22 61 20 49 4e 20 28 53 45 4c 45 43 54 20 2a   "a IN (SELECT *
1b7ac 20 46 52 4f 4d 20 74 61 62 6c 65 29 22 29 20 62   FROM table)") b
1b7ad 75 74 20 69 74 20 68 61 73 20 6d 6f 72 65 20 74  ut it has more t
1b7ae 68 61 6e 20 31 20 72 65 73 75 6c 74 0a 2a 2a 20  han 1 result.** 
1b7af 63 6f 6c 75 6d 6e 2e 20 20 57 65 20 64 6f 20 74  column.  We do t
1b7b0 68 69 73 20 69 6e 20 61 20 73 75 62 72 6f 75 74  his in a subrout
1b7b1 69 6e 65 20 62 65 63 61 75 73 65 20 74 68 65 20  ine because the 
1b7b2 65 72 72 6f 72 20 6f 63 63 75 72 73 20 69 6e 20  error occurs in 
1b7b3 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 70 6c 61 63  multiple.** plac
1b7b4 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  es..*/.static in
1b7b5 74 20 63 68 65 63 6b 46 6f 72 4d 75 6c 74 69 43  t checkForMultiC
1b7b6 6f 6c 75 6d 6e 53 65 6c 65 63 74 45 72 72 6f 72  olumnSelectError
1b7b7 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
1b7b8 69 6e 74 20 65 44 65 73 74 2c 20 69 6e 74 20 6e  int eDest, int n
1b7b9 45 78 70 72 29 7b 0a 20 20 69 66 28 20 6e 45 78  Expr){.  if( nEx
1b7ba 70 72 3e 31 20 26 26 20 28 65 44 65 73 74 3d 3d  pr>1 && (eDest==
1b7bb 53 52 54 5f 4d 65 6d 20 7c 7c 20 65 44 65 73 74  SRT_Mem || eDest
1b7bc 3d 3d 53 52 54 5f 53 65 74 29 20 29 7b 0a 20 20  ==SRT_Set) ){.  
1b7bd 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
1b7be 67 28 70 50 61 72 73 65 2c 20 22 6f 6e 6c 79 20  g(pParse, "only 
1b7bf 61 20 73 69 6e 67 6c 65 20 72 65 73 75 6c 74 20  a single result 
1b7c0 61 6c 6c 6f 77 65 64 20 66 6f 72 20 22 0a 20 20  allowed for ".  
1b7c1 20 20 20 20 20 22 61 20 53 45 4c 45 43 54 20 74       "a SELECT t
1b7c2 68 61 74 20 69 73 20 70 61 72 74 20 6f 66 20 61  hat is part of a
1b7c3 6e 20 65 78 70 72 65 73 73 69 6f 6e 22 29 3b 0a  n expression");.
1b7c4 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
1b7c5 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72  }else{.    retur
1b7c6 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  n 0;.  }.}../*.*
1b7c7 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67  * This routine g
1b7c8 65 6e 65 72 61 74 65 73 20 74 68 65 20 63 6f 64  enerates the cod
1b7c9 65 20 66 6f 72 20 74 68 65 20 69 6e 73 69 64 65  e for the inside
1b7ca 20 6f 66 20 74 68 65 20 69 6e 6e 65 72 20 6c 6f   of the inner lo
1b7cb 6f 70 0a 2a 2a 20 6f 66 20 61 20 53 45 4c 45 43  op.** of a SELEC
1b7cc 54 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 72 63 54  T..**.** If srcT
1b7cd 61 62 20 61 6e 64 20 6e 43 6f 6c 75 6d 6e 20 61  ab and nColumn a
1b7ce 72 65 20 62 6f 74 68 20 7a 65 72 6f 2c 20 74 68  re both zero, th
1b7cf 65 6e 20 74 68 65 20 70 45 4c 69 73 74 20 65 78  en the pEList ex
1b7d0 70 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 61 72 65  pressions.** are
1b7d1 20 65 76 61 6c 75 61 74 65 64 20 69 6e 20 6f 72   evaluated in or
1b7d2 64 65 72 20 74 6f 20 67 65 74 20 74 68 65 20 64  der to get the d
1b7d3 61 74 61 20 66 6f 72 20 74 68 69 73 20 72 6f 77  ata for this row
1b7d4 2e 20 20 49 66 20 6e 43 6f 6c 75 6d 6e 3e 30 0a  .  If nColumn>0.
1b7d5 2a 2a 20 74 68 65 6e 20 64 61 74 61 20 69 73 20  ** then data is 
1b7d6 70 75 6c 6c 65 64 20 66 72 6f 6d 20 73 72 63 54  pulled from srcT
1b7d7 61 62 20 61 6e 64 20 70 45 4c 69 73 74 20 69 73  ab and pEList is
1b7d8 20 75 73 65 64 20 6f 6e 6c 79 20 74 6f 20 67 65   used only to ge
1b7d9 74 20 74 68 65 0a 2a 2a 20 64 61 74 61 74 79 70  t the.** datatyp
1b7da 65 73 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75  es for each colu
1b7db 6d 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  mn..*/.static in
1b7dc 74 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f  t selectInnerLoo
1b7dd 70 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  p(.  Parse *pPar
1b7de 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  se,          /* 
1b7df 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65  The parser conte
1b7e0 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  xt */.  Select *
1b7e1 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
1b7e2 2f 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20  /* The complete 
1b7e3 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74  select statement
1b7e4 20 62 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a   being coded */.
1b7e5 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69    ExprList *pELi
1b7e6 73 74 2c 20 20 20 20 20 20 20 2f 2a 20 4c 69 73  st,       /* Lis
1b7e7 74 20 6f 66 20 76 61 6c 75 65 73 20 62 65 69 6e  t of values bein
1b7e8 67 20 65 78 74 72 61 63 74 65 64 20 2a 2f 0a 20  g extracted */. 
1b7e9 20 69 6e 74 20 73 72 63 54 61 62 2c 20 20 20 20   int srcTab,    
1b7ea 20 20 20 20 20 20 20 20 20 2f 2a 20 50 75 6c 6c           /* Pull
1b7eb 20 64 61 74 61 20 66 72 6f 6d 20 74 68 69 73 20   data from this 
1b7ec 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  table */.  int n
1b7ed 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 20  Column,         
1b7ee 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1b7ef 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 73  columns in the s
1b7f0 6f 75 72 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20  ource table */. 
1b7f1 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65   ExprList *pOrde
1b7f2 72 42 79 2c 20 20 20 20 20 2f 2a 20 49 66 20 6e  rBy,     /* If n
1b7f3 6f 74 20 4e 55 4c 4c 2c 20 73 6f 72 74 20 72 65  ot NULL, sort re
1b7f4 73 75 6c 74 73 20 75 73 69 6e 67 20 74 68 69 73  sults using this
1b7f5 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 64 69   key */.  int di
1b7f6 73 74 69 6e 63 74 2c 20 20 20 20 20 20 20 20 20  stinct,         
1b7f7 20 20 2f 2a 20 49 66 20 3e 3d 30 2c 20 6d 61 6b    /* If >=0, mak
1b7f8 65 20 73 75 72 65 20 72 65 73 75 6c 74 73 20 61  e sure results a
1b7f9 72 65 20 64 69 73 74 69 6e 63 74 20 2a 2f 0a 20  re distinct */. 
1b7fa 20 69 6e 74 20 65 44 65 73 74 2c 20 20 20 20 20   int eDest,     
1b7fb 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 77 20           /* How 
1b7fc 74 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 74 68  to dispose of th
1b7fd 65 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20 69  e results */.  i
1b7fe 6e 74 20 69 50 61 72 6d 2c 20 20 20 20 20 20 20  nt iParm,       
1b7ff 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 61 72 67         /* An arg
1b800 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 64 69 73  ument to the dis
1b801 70 6f 73 61 6c 20 6d 65 74 68 6f 64 20 2a 2f 0a  posal method */.
1b802 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75 65 2c    int iContinue,
1b803 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d            /* Jum
1b804 70 20 68 65 72 65 20 74 6f 20 63 6f 6e 74 69 6e  p here to contin
1b805 75 65 20 77 69 74 68 20 6e 65 78 74 20 72 6f 77  ue with next row
1b806 20 2a 2f 0a 20 20 69 6e 74 20 69 42 72 65 61 6b   */.  int iBreak
1b807 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
1b808 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 62 72   Jump here to br
1b809 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 69  eak out of the i
1b80a 6e 6e 65 72 20 6c 6f 6f 70 20 2a 2f 0a 20 20 63  nner loop */.  c
1b80b 68 61 72 20 2a 61 66 66 20 20 20 20 20 20 20 20  har *aff        
1b80c 20 20 20 20 20 20 20 2f 2a 20 61 66 66 69 6e 69         /* affini
1b80d 74 79 20 73 74 72 69 6e 67 20 69 66 20 65 44 65  ty string if eDe
1b80e 73 74 20 69 73 20 53 52 54 5f 55 6e 69 6f 6e 20  st is SRT_Union 
1b80f 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20  */.){.  Vdbe *v 
1b810 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  = pParse->pVdbe;
1b811 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
1b812 68 61 73 44 69 73 74 69 6e 63 74 3b 20 20 20 20  hasDistinct;    
1b813 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
1b814 68 65 20 44 49 53 54 49 4e 43 54 20 6b 65 79 77  he DISTINCT keyw
1b815 6f 72 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a  ord is present *
1b816 2f 0a 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20  /..  if( v==0 ) 
1b817 72 65 74 75 72 6e 20 30 3b 0a 20 20 61 73 73 65  return 0;.  asse
1b818 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b  rt( pEList!=0 );
1b819 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20  ..  /* If there 
1b81a 77 61 73 20 61 20 4c 49 4d 49 54 20 63 6c 61 75  was a LIMIT clau
1b81b 73 65 20 6f 6e 20 74 68 65 20 53 45 4c 45 43 54  se on the SELECT
1b81c 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e   statement, then
1b81d 20 64 6f 20 74 68 65 20 63 68 65 63 6b 0a 20 20   do the check.  
1b81e 2a 2a 20 74 6f 20 73 65 65 20 69 66 20 74 68 69  ** to see if thi
1b81f 73 20 72 6f 77 20 73 68 6f 75 6c 64 20 62 65 20  s row should be 
1b820 6f 75 74 70 75 74 2e 0a 20 20 2a 2f 0a 20 20 68  output..  */.  h
1b821 61 73 44 69 73 74 69 6e 63 74 20 3d 20 64 69 73  asDistinct = dis
1b822 74 69 6e 63 74 3e 3d 30 20 26 26 20 70 45 4c 69  tinct>=0 && pELi
1b823 73 74 2d 3e 6e 45 78 70 72 3e 30 3b 0a 20 20 69  st->nExpr>0;.  i
1b824 66 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 26  f( pOrderBy==0 &
1b825 26 20 21 68 61 73 44 69 73 74 69 6e 63 74 20 29  & !hasDistinct )
1b826 7b 0a 20 20 20 20 63 6f 64 65 4f 66 66 73 65 74  {.    codeOffset
1b827 28 76 2c 20 70 2c 20 69 43 6f 6e 74 69 6e 75 65  (v, p, iContinue
1b828 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  , 0);.  }..  /* 
1b829 50 75 6c 6c 20 74 68 65 20 72 65 71 75 65 73 74  Pull the request
1b82a 65 64 20 63 6f 6c 75 6d 6e 73 2e 0a 20 20 2a 2f  ed columns..  */
1b82b 0a 20 20 69 66 28 20 6e 43 6f 6c 75 6d 6e 3e 30  .  if( nColumn>0
1b82c 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   ){.    for(i=0;
1b82d 20 69 3c 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29   i<nColumn; i++)
1b82e 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
1b82f 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43  dbeAddOp(v, OP_C
1b830 6f 6c 75 6d 6e 2c 20 73 72 63 54 61 62 2c 20 69  olumn, srcTab, i
1b831 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
1b832 7b 0a 20 20 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20  {.    nColumn = 
1b833 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20  pEList->nExpr;. 
1b834 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
1b835 64 65 45 78 70 72 4c 69 73 74 28 70 50 61 72 73  deExprList(pPars
1b836 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 7d 0a  e, pEList);.  }.
1b837 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 44 49 53  .  /* If the DIS
1b838 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 77 61  TINCT keyword wa
1b839 73 20 70 72 65 73 65 6e 74 20 6f 6e 20 74 68 65  s present on the
1b83a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
1b83b 74 0a 20 20 2a 2a 20 61 6e 64 20 74 68 69 73 20  t.  ** and this 
1b83c 72 6f 77 20 68 61 73 20 62 65 65 6e 20 73 65 65  row has been see
1b83d 6e 20 62 65 66 6f 72 65 2c 20 74 68 65 6e 20 64  n before, then d
1b83e 6f 20 6e 6f 74 20 6d 61 6b 65 20 74 68 69 73 20  o not make this 
1b83f 72 6f 77 0a 20 20 2a 2a 20 70 61 72 74 20 6f 66  row.  ** part of
1b840 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 20 20 2a   the result..  *
1b841 2f 0a 20 20 69 66 28 20 68 61 73 44 69 73 74 69  /.  if( hasDisti
1b842 6e 63 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72  nct ){.    asser
1b843 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b 0a  t( pEList!=0 );.
1b844 20 20 20 20 61 73 73 65 72 74 28 20 70 45 4c 69      assert( pELi
1b845 73 74 2d 3e 6e 45 78 70 72 3d 3d 6e 43 6f 6c 75  st->nExpr==nColu
1b846 6d 6e 20 29 3b 0a 20 20 20 20 63 6f 64 65 44 69  mn );.    codeDi
1b847 73 74 69 6e 63 74 28 76 2c 20 64 69 73 74 69 6e  stinct(v, distin
1b848 63 74 2c 20 69 43 6f 6e 74 69 6e 75 65 2c 20 6e  ct, iContinue, n
1b849 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 69 66 28  Column);.    if(
1b84a 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 7b 0a   pOrderBy==0 ){.
1b84b 20 20 20 20 20 20 63 6f 64 65 4f 66 66 73 65 74        codeOffset
1b84c 28 76 2c 20 70 2c 20 69 43 6f 6e 74 69 6e 75 65  (v, p, iContinue
1b84d 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20  , nColumn);.    
1b84e 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 63 68 65  }.  }..  if( che
1b84f 63 6b 46 6f 72 4d 75 6c 74 69 43 6f 6c 75 6d 6e  ckForMultiColumn
1b850 53 65 6c 65 63 74 45 72 72 6f 72 28 70 50 61 72  SelectError(pPar
1b851 73 65 2c 20 65 44 65 73 74 2c 20 70 45 4c 69 73  se, eDest, pELis
1b852 74 2d 3e 6e 45 78 70 72 29 20 29 7b 0a 20 20 20  t->nExpr) ){.   
1b853 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a   return 0;.  }..
1b854 20 20 73 77 69 74 63 68 28 20 65 44 65 73 74 20    switch( eDest 
1b855 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 20 74 68 69  ){.    /* In thi
1b856 73 20 6d 6f 64 65 2c 20 77 72 69 74 65 20 65 61  s mode, write ea
1b857 63 68 20 71 75 65 72 79 20 72 65 73 75 6c 74 20  ch query result 
1b858 74 6f 20 74 68 65 20 6b 65 79 20 6f 66 20 74 68  to the key of th
1b859 65 20 74 65 6d 70 6f 72 61 72 79 0a 20 20 20 20  e temporary.    
1b85a 2a 2a 20 74 61 62 6c 65 20 69 50 61 72 6d 2e 0a  ** table iParm..
1b85b 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53      */.#ifndef S
1b85c 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f  QLITE_OMIT_COMPO
1b85d 55 4e 44 5f 53 45 4c 45 43 54 0a 20 20 20 20 63  UND_SELECT.    c
1b85e 61 73 65 20 53 52 54 5f 55 6e 69 6f 6e 3a 20 7b  ase SRT_Union: {
1b85f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1b860 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 61  beAddOp(v, OP_Ma
1b861 6b 65 52 65 63 6f 72 64 2c 20 6e 43 6f 6c 75 6d  keRecord, nColum
1b862 6e 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28  n, 0);.      if(
1b863 20 61 66 66 20 29 7b 0a 20 20 20 20 20 20 20 20   aff ){.        
1b864 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1b865 65 50 33 28 76 2c 20 2d 31 2c 20 61 66 66 2c 20  eP3(v, -1, aff, 
1b866 50 33 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  P3_STATIC);.    
1b867 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
1b868 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1b869 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 50 61 72  _IdxInsert, iPar
1b86a 6d 2c 20 30 29 3b 0a 20 20 20 20 20 20 62 72 65  m, 0);.      bre
1b86b 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  ak;.    }..    /
1b86c 2a 20 43 6f 6e 73 74 72 75 63 74 20 61 20 72 65  * Construct a re
1b86d 63 6f 72 64 20 66 72 6f 6d 20 74 68 65 20 71 75  cord from the qu
1b86e 65 72 79 20 72 65 73 75 6c 74 2c 20 62 75 74 20  ery result, but 
1b86f 69 6e 73 74 65 61 64 20 6f 66 0a 20 20 20 20 2a  instead of.    *
1b870 2a 20 73 61 76 69 6e 67 20 74 68 61 74 20 72 65  * saving that re
1b871 63 6f 72 64 2c 20 75 73 65 20 69 74 20 61 73 20  cord, use it as 
1b872 61 20 6b 65 79 20 74 6f 20 64 65 6c 65 74 65 20  a key to delete 
1b873 65 6c 65 6d 65 6e 74 73 20 66 72 6f 6d 0a 20 20  elements from.  
1b874 20 20 2a 2a 20 74 68 65 20 74 65 6d 70 6f 72 61    ** the tempora
1b875 72 79 20 74 61 62 6c 65 20 69 50 61 72 6d 2e 0a  ry table iParm..
1b876 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20      */.    case 
1b877 53 52 54 5f 45 78 63 65 70 74 3a 20 7b 0a 20 20  SRT_Except: {.  
1b878 20 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20      int addr;.  
1b879 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74      addr = sqlit
1b87a 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1b87b 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 6e 43  P_MakeRecord, nC
1b87c 6f 6c 75 6d 6e 2c 20 30 29 3b 0a 20 20 20 20 20  olumn, 0);.     
1b87d 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
1b87e 67 65 50 33 28 76 2c 20 2d 31 2c 20 61 66 66 2c  geP3(v, -1, aff,
1b87f 20 50 33 5f 53 54 41 54 49 43 29 3b 0a 20 20 20   P3_STATIC);.   
1b880 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1b881 64 4f 70 28 76 2c 20 4f 50 5f 4e 6f 74 46 6f 75  dOp(v, OP_NotFou
1b882 6e 64 2c 20 69 50 61 72 6d 2c 20 61 64 64 72 2b  nd, iParm, addr+
1b883 33 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  3);.      sqlite
1b884 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1b885 5f 44 65 6c 65 74 65 2c 20 69 50 61 72 6d 2c 20  _Delete, iParm, 
1b886 30 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  0);.      break;
1b887 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  .    }.#endif.. 
1b888 20 20 20 2f 2a 20 53 74 6f 72 65 20 74 68 65 20     /* Store the 
1b889 72 65 73 75 6c 74 20 61 73 20 64 61 74 61 20 75  result as data u
1b88a 73 69 6e 67 20 61 20 75 6e 69 71 75 65 20 6b 65  sing a unique ke
1b88b 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61  y..    */.    ca
1b88c 73 65 20 53 52 54 5f 54 61 62 6c 65 3a 0a 20 20  se SRT_Table:.  
1b88d 20 20 63 61 73 65 20 53 52 54 5f 45 70 68 65 6d    case SRT_Ephem
1b88e 54 61 62 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c  Tab: {.      sql
1b88f 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1b890 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20   OP_MakeRecord, 
1b891 6e 43 6f 6c 75 6d 6e 2c 20 30 29 3b 0a 20 20 20  nColumn, 0);.   
1b892 20 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 20     if( pOrderBy 
1b893 29 7b 0a 20 20 20 20 20 20 20 20 70 75 73 68 4f  ){.        pushO
1b894 6e 74 6f 53 6f 72 74 65 72 28 70 50 61 72 73 65  ntoSorter(pParse
1b895 2c 20 70 4f 72 64 65 72 42 79 2c 20 70 29 3b 0a  , pOrderBy, p);.
1b896 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1b897 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1b898 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4e 65 77 52  AddOp(v, OP_NewR
1b899 6f 77 69 64 2c 20 69 50 61 72 6d 2c 20 30 29 3b  owid, iParm, 0);
1b89a 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1b89b 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1b89c 50 75 6c 6c 2c 20 31 2c 20 30 29 3b 0a 20 20 20  Pull, 1, 0);.   
1b89d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1b89e 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 73 65  AddOp(v, OP_Inse
1b89f 72 74 2c 20 69 50 61 72 6d 2c 20 4f 50 46 4c 41  rt, iParm, OPFLA
1b8a0 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20 20 20 20  G_APPEND);.     
1b8a1 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
1b8a2 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53      }..#ifndef S
1b8a3 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
1b8a4 45 52 59 0a 20 20 20 20 2f 2a 20 49 66 20 77 65  ERY.    /* If we
1b8a5 20 61 72 65 20 63 72 65 61 74 69 6e 67 20 61 20   are creating a 
1b8a6 73 65 74 20 66 6f 72 20 61 6e 20 22 65 78 70 72  set for an "expr
1b8a7 20 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29   IN (SELECT ...)
1b8a8 22 20 63 6f 6e 73 74 72 75 63 74 2c 0a 20 20 20  " construct,.   
1b8a9 20 2a 2a 20 74 68 65 6e 20 74 68 65 72 65 20 73   ** then there s
1b8aa 68 6f 75 6c 64 20 62 65 20 61 20 73 69 6e 67 6c  hould be a singl
1b8ab 65 20 69 74 65 6d 20 6f 6e 20 74 68 65 20 73 74  e item on the st
1b8ac 61 63 6b 2e 20 20 57 72 69 74 65 20 74 68 69 73  ack.  Write this
1b8ad 0a 20 20 20 20 2a 2a 20 69 74 65 6d 20 69 6e 74  .    ** item int
1b8ae 6f 20 74 68 65 20 73 65 74 20 74 61 62 6c 65 20  o the set table 
1b8af 77 69 74 68 20 62 6f 67 75 73 20 64 61 74 61 2e  with bogus data.
1b8b0 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65  .    */.    case
1b8b1 20 53 52 54 5f 53 65 74 3a 20 7b 0a 20 20 20 20   SRT_Set: {.    
1b8b2 20 20 69 6e 74 20 61 64 64 72 31 20 3d 20 73 71    int addr1 = sq
1b8b3 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
1b8b4 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20 69  Addr(v);.      i
1b8b5 6e 74 20 61 64 64 72 32 3b 0a 0a 20 20 20 20 20  nt addr2;..     
1b8b6 20 61 73 73 65 72 74 28 20 6e 43 6f 6c 75 6d 6e   assert( nColumn
1b8b7 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ==1 );.      sql
1b8b8 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1b8b9 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 2d 31 2c   OP_NotNull, -1,
1b8ba 20 61 64 64 72 31 2b 33 29 3b 0a 20 20 20 20 20   addr1+3);.     
1b8bb 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1b8bc 70 28 76 2c 20 4f 50 5f 50 6f 70 2c 20 31 2c 20  p(v, OP_Pop, 1, 
1b8bd 30 29 3b 0a 20 20 20 20 20 20 61 64 64 72 32 20  0);.      addr2 
1b8be 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
1b8bf 4f 70 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  Op(v, OP_Goto, 0
1b8c0 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61  , 0);.      p->a
1b8c1 66 66 69 6e 69 74 79 20 3d 20 73 71 6c 69 74 65  ffinity = sqlite
1b8c2 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79  3CompareAffinity
1b8c3 28 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45  (pEList->a[0].pE
1b8c4 78 70 72 2c 28 69 50 61 72 6d 3e 3e 31 36 29 26  xpr,(iParm>>16)&
1b8c5 30 78 66 66 29 3b 0a 20 20 20 20 20 20 69 66 28  0xff);.      if(
1b8c6 20 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20   pOrderBy ){.   
1b8c7 20 20 20 20 20 2f 2a 20 41 74 20 66 69 72 73 74       /* At first
1b8c8 20 67 6c 61 6e 63 65 20 79 6f 75 20 77 6f 75 6c   glance you woul
1b8c9 64 20 74 68 69 6e 6b 20 77 65 20 63 6f 75 6c 64  d think we could
1b8ca 20 6f 70 74 69 6d 69 7a 65 20 6f 75 74 20 74 68   optimize out th
1b8cb 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f 52 44  e.        ** ORD
1b8cc 45 52 20 42 59 20 69 6e 20 74 68 69 73 20 63 61  ER BY in this ca
1b8cd 73 65 20 73 69 6e 63 65 20 74 68 65 20 6f 72 64  se since the ord
1b8ce 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e  er of entries in
1b8cf 20 74 68 65 20 73 65 74 0a 20 20 20 20 20 20 20   the set.       
1b8d0 20 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74   ** does not mat
1b8d1 74 65 72 2e 20 20 42 75 74 20 74 68 65 72 65 20  ter.  But there 
1b8d2 6d 69 67 68 74 20 62 65 20 61 20 4c 49 4d 49 54  might be a LIMIT
1b8d3 20 63 6c 61 75 73 65 2c 20 69 6e 20 77 68 69 63   clause, in whic
1b8d4 68 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 61 73  h.        ** cas
1b8d5 65 20 74 68 65 20 6f 72 64 65 72 20 64 6f 65 73  e the order does
1b8d6 20 6d 61 74 74 65 72 20 2a 2f 0a 20 20 20 20 20   matter */.     
1b8d7 20 20 20 70 75 73 68 4f 6e 74 6f 53 6f 72 74 65     pushOntoSorte
1b8d8 72 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65 72  r(pParse, pOrder
1b8d9 42 79 2c 20 70 29 3b 0a 20 20 20 20 20 20 7d 65  By, p);.      }e
1b8da 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  lse{.        sql
1b8db 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20 4f  ite3VdbeOp3(v, O
1b8dc 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 31 2c  P_MakeRecord, 1,
1b8dd 20 30 2c 20 26 70 2d 3e 61 66 66 69 6e 69 74 79   0, &p->affinity
1b8de 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71  , 1);.        sq
1b8df 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1b8e0 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20  , OP_IdxInsert, 
1b8e1 28 69 50 61 72 6d 26 30 78 30 30 30 30 46 46 46  (iParm&0x0000FFF
1b8e2 46 29 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a  F), 0);.      }.
1b8e3 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1b8e4 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
1b8e5 72 32 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  r2);.      break
1b8e6 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
1b8e7 49 66 20 61 6e 79 20 72 6f 77 20 65 78 69 73 74  If any row exist
1b8e8 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
1b8e9 65 74 2c 20 72 65 63 6f 72 64 20 74 68 61 74 20  et, record that 
1b8ea 66 61 63 74 20 61 6e 64 20 61 62 6f 72 74 2e 0a  fact and abort..
1b8eb 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20      */.    case 
1b8ec 53 52 54 5f 45 78 69 73 74 73 3a 20 7b 0a 20 20  SRT_Exists: {.  
1b8ed 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1b8ee 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 49 6e  ddOp(v, OP_MemIn
1b8ef 74 2c 20 31 2c 20 69 50 61 72 6d 29 3b 0a 20 20  t, 1, iParm);.  
1b8f0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1b8f1 64 64 4f 70 28 76 2c 20 4f 50 5f 50 6f 70 2c 20  ddOp(v, OP_Pop, 
1b8f2 6e 43 6f 6c 75 6d 6e 2c 20 30 29 3b 0a 20 20 20  nColumn, 0);.   
1b8f3 20 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20     /* The LIMIT 
1b8f4 63 6c 61 75 73 65 20 77 69 6c 6c 20 74 65 72 6d  clause will term
1b8f5 69 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70 20 66  inate the loop f
1b8f6 6f 72 20 75 73 20 2a 2f 0a 20 20 20 20 20 20 62  or us */.      b
1b8f7 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  reak;.    }..   
1b8f8 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61   /* If this is a
1b8f9 20 73 63 61 6c 61 72 20 73 65 6c 65 63 74 20 74   scalar select t
1b8fa 68 61 74 20 69 73 20 70 61 72 74 20 6f 66 20 61  hat is part of a
1b8fb 6e 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 74 68  n expression, th
1b8fc 65 6e 0a 20 20 20 20 2a 2a 20 73 74 6f 72 65 20  en.    ** store 
1b8fd 74 68 65 20 72 65 73 75 6c 74 73 20 69 6e 20 74  the results in t
1b8fe 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 6d  he appropriate m
1b8ff 65 6d 6f 72 79 20 63 65 6c 6c 20 61 6e 64 20 62  emory cell and b
1b900 72 65 61 6b 20 6f 75 74 0a 20 20 20 20 2a 2a 20  reak out.    ** 
1b901 6f 66 20 74 68 65 20 73 63 61 6e 20 6c 6f 6f 70  of the scan loop
1b902 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73  ..    */.    cas
1b903 65 20 53 52 54 5f 4d 65 6d 3a 20 7b 0a 20 20 20  e SRT_Mem: {.   
1b904 20 20 20 61 73 73 65 72 74 28 20 6e 43 6f 6c 75     assert( nColu
1b905 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 69  mn==1 );.      i
1b906 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b 0a 20  f( pOrderBy ){. 
1b907 20 20 20 20 20 20 20 70 75 73 68 4f 6e 74 6f 53         pushOntoS
1b908 6f 72 74 65 72 28 70 50 61 72 73 65 2c 20 70 4f  orter(pParse, pO
1b909 72 64 65 72 42 79 2c 20 70 29 3b 0a 20 20 20 20  rderBy, p);.    
1b90a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1b90b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1b90c 70 28 76 2c 20 4f 50 5f 4d 65 6d 53 74 6f 72 65  p(v, OP_MemStore
1b90d 2c 20 69 50 61 72 6d 2c 20 31 29 3b 0a 20 20 20  , iParm, 1);.   
1b90e 20 20 20 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49       /* The LIMI
1b90f 54 20 63 6c 61 75 73 65 20 77 69 6c 6c 20 6a 75  T clause will ju
1b910 6d 70 20 6f 75 74 20 6f 66 20 74 68 65 20 6c 6f  mp out of the lo
1b911 6f 70 20 66 6f 72 20 75 73 20 2a 2f 0a 20 20 20  op for us */.   
1b912 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
1b913 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f  ;.    }.#endif /
1b914 2a 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  * #ifndef SQLITE
1b915 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a  _OMIT_SUBQUERY *
1b916 2f 0a 0a 20 20 20 20 2f 2a 20 53 65 6e 64 20 74  /..    /* Send t
1b917 68 65 20 64 61 74 61 20 74 6f 20 74 68 65 20 63  he data to the c
1b918 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
1b919 20 6f 72 20 74 6f 20 61 20 73 75 62 72 6f 75 74   or to a subrout
1b91a 69 6e 65 2e 20 20 49 6e 20 74 68 65 0a 20 20 20  ine.  In the.   
1b91b 20 2a 2a 20 63 61 73 65 20 6f 66 20 61 20 73 75   ** case of a su
1b91c 62 72 6f 75 74 69 6e 65 2c 20 74 68 65 20 73 75  broutine, the su
1b91d 62 72 6f 75 74 69 6e 65 20 69 74 73 65 6c 66 20  broutine itself 
1b91e 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66  is responsible f
1b91f 6f 72 0a 20 20 20 20 2a 2a 20 70 6f 70 70 69 6e  or.    ** poppin
1b920 67 20 74 68 65 20 64 61 74 61 20 66 72 6f 6d 20  g the data from 
1b921 74 68 65 20 73 74 61 63 6b 2e 0a 20 20 20 20 2a  the stack..    *
1b922 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 53  /.    case SRT_S
1b923 75 62 72 6f 75 74 69 6e 65 3a 0a 20 20 20 20 63  ubroutine:.    c
1b924 61 73 65 20 53 52 54 5f 43 61 6c 6c 62 61 63 6b  ase SRT_Callback
1b925 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f  : {.      if( pO
1b926 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 20 20  rderBy ){.      
1b927 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1b928 4f 70 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63  Op(v, OP_MakeRec
1b929 6f 72 64 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 30 29  ord, nColumn, 0)
1b92a 3b 0a 20 20 20 20 20 20 20 20 70 75 73 68 4f 6e  ;.        pushOn
1b92b 74 6f 53 6f 72 74 65 72 28 70 50 61 72 73 65 2c  toSorter(pParse,
1b92c 20 70 4f 72 64 65 72 42 79 2c 20 70 29 3b 0a 20   pOrderBy, p);. 
1b92d 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 65       }else if( e
1b92e 44 65 73 74 3d 3d 53 52 54 5f 53 75 62 72 6f 75  Dest==SRT_Subrou
1b92f 74 69 6e 65 20 29 7b 0a 20 20 20 20 20 20 20 20  tine ){.        
1b930 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1b931 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 30 2c  (v, OP_Gosub, 0,
1b932 20 69 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 7d   iParm);.      }
1b933 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71  else{.        sq
1b934 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1b935 2c 20 4f 50 5f 43 61 6c 6c 62 61 63 6b 2c 20 6e  , OP_Callback, n
1b936 43 6f 6c 75 6d 6e 2c 20 30 29 3b 0a 20 20 20 20  Column, 0);.    
1b937 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b    }.      break;
1b938 0a 20 20 20 20 7d 0a 0a 23 69 66 20 21 64 65 66  .    }..#if !def
1b939 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
1b93a 5f 54 52 49 47 47 45 52 29 0a 20 20 20 20 2f 2a  _TRIGGER).    /*
1b93b 20 44 69 73 63 61 72 64 20 74 68 65 20 72 65 73   Discard the res
1b93c 75 6c 74 73 2e 20 20 54 68 69 73 20 69 73 20 75  ults.  This is u
1b93d 73 65 64 20 66 6f 72 20 53 45 4c 45 43 54 20 73  sed for SELECT s
1b93e 74 61 74 65 6d 65 6e 74 73 20 69 6e 73 69 64 65  tatements inside
1b93f 0a 20 20 20 20 2a 2a 20 74 68 65 20 62 6f 64 79  .    ** the body
1b940 20 6f 66 20 61 20 54 52 49 47 47 45 52 2e 20 20   of a TRIGGER.  
1b941 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66 20 73  The purpose of s
1b942 75 63 68 20 73 65 6c 65 63 74 73 20 69 73 20 74  uch selects is t
1b943 6f 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 75 73  o call.    ** us
1b944 65 72 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74  er-defined funct
1b945 69 6f 6e 73 20 74 68 61 74 20 68 61 76 65 20 73  ions that have s
1b946 69 64 65 20 65 66 66 65 63 74 73 2e 20 20 57 65  ide effects.  We
1b947 20 64 6f 20 6e 6f 74 20 63 61 72 65 0a 20 20 20   do not care.   
1b948 20 2a 2a 20 61 62 6f 75 74 20 74 68 65 20 61 63   ** about the ac
1b949 74 75 61 6c 20 72 65 73 75 6c 74 73 20 6f 66 20  tual results of 
1b94a 74 68 65 20 73 65 6c 65 63 74 2e 0a 20 20 20 20  the select..    
1b94b 2a 2f 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20  */.    default: 
1b94c 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
1b94d 65 44 65 73 74 3d 3d 53 52 54 5f 44 69 73 63 61  eDest==SRT_Disca
1b94e 72 64 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  rd );.      sqli
1b94f 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1b950 4f 50 5f 50 6f 70 2c 20 6e 43 6f 6c 75 6d 6e 2c  OP_Pop, nColumn,
1b951 20 30 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b   0);.      break
1b952 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
1b953 20 7d 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f   }..  /* Jump to
1b954 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
1b955 6c 6f 6f 70 20 69 66 20 74 68 65 20 4c 49 4d 49  loop if the LIMI
1b956 54 20 69 73 20 72 65 61 63 68 65 64 2e 0a 20 20  T is reached..  
1b957 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 4c 69 6d  */.  if( p->iLim
1b958 69 74 3e 3d 30 20 26 26 20 70 4f 72 64 65 72 42  it>=0 && pOrderB
1b959 79 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  y==0 ){.    sqli
1b95a 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1b95b 4f 50 5f 4d 65 6d 49 6e 63 72 2c 20 2d 31 2c 20  OP_MemIncr, -1, 
1b95c 70 2d 3e 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20  p->iLimit);.    
1b95d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1b95e 28 76 2c 20 4f 50 5f 49 66 4d 65 6d 5a 65 72 6f  (v, OP_IfMemZero
1b95f 2c 20 70 2d 3e 69 4c 69 6d 69 74 2c 20 69 42 72  , p->iLimit, iBr
1b960 65 61 6b 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  eak);.  }.  retu
1b961 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  rn 0;.}../*.** G
1b962 69 76 65 6e 20 61 6e 20 65 78 70 72 65 73 73 69  iven an expressi
1b963 6f 6e 20 6c 69 73 74 2c 20 67 65 6e 65 72 61 74  on list, generat
1b964 65 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75  e a KeyInfo stru
1b965 63 74 75 72 65 20 74 68 61 74 20 72 65 63 6f 72  cture that recor
1b966 64 73 0a 2a 2a 20 74 68 65 20 63 6f 6c 6c 61 74  ds.** the collat
1b967 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72  ing sequence for
1b968 20 65 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e   each expression
1b969 20 69 6e 20 74 68 61 74 20 65 78 70 72 65 73 73   in that express
1b96a 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20  ion list..**.** 
1b96b 49 66 20 74 68 65 20 45 78 70 72 4c 69 73 74 20  If the ExprList 
1b96c 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 20 6f  is an ORDER BY o
1b96d 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73  r GROUP BY claus
1b96e 65 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c  e then the resul
1b96f 74 69 6e 67 0a 2a 2a 20 4b 65 79 49 6e 66 6f 20  ting.** KeyInfo 
1b970 73 74 72 75 63 74 75 72 65 20 69 73 20 61 70 70  structure is app
1b971 72 6f 70 72 69 61 74 65 20 66 6f 72 20 69 6e 69  ropriate for ini
1b972 74 69 61 6c 69 7a 69 6e 67 20 61 20 76 69 72 74  tializing a virt
1b973 75 61 6c 20 69 6e 64 65 78 20 74 6f 0a 2a 2a 20  ual index to.** 
1b974 69 6d 70 6c 65 6d 65 6e 74 20 74 68 61 74 20 63  implement that c
1b975 6c 61 75 73 65 2e 20 20 49 66 20 74 68 65 20 45  lause.  If the E
1b976 78 70 72 4c 69 73 74 20 69 73 20 74 68 65 20 72  xprList is the r
1b977 65 73 75 6c 74 20 73 65 74 20 6f 66 20 61 20 53  esult set of a S
1b978 45 4c 45 43 54 0a 2a 2a 20 74 68 65 6e 20 74 68  ELECT.** then th
1b979 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74  e KeyInfo struct
1b97a 75 72 65 20 69 73 20 61 70 70 72 6f 70 72 69 61  ure is appropria
1b97b 74 65 20 66 6f 72 20 69 6e 69 74 69 61 6c 69 7a  te for initializ
1b97c 69 6e 67 20 61 20 76 69 72 74 75 61 6c 0a 2a 2a  ing a virtual.**
1b97d 20 69 6e 64 65 78 20 74 6f 20 69 6d 70 6c 65 6d   index to implem
1b97e 65 6e 74 20 61 20 44 49 53 54 49 4e 43 54 20 74  ent a DISTINCT t
1b97f 65 73 74 2e 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65  est..**.** Space
1b980 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 4b 65 79   to hold the Key
1b981 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 69  Info structure i
1b982 73 20 6f 62 74 61 69 6e 20 66 72 6f 6d 20 6d 61  s obtain from ma
1b983 6c 6c 6f 63 2e 20 20 54 68 65 20 63 61 6c 6c 69  lloc.  The calli
1b984 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69  ng.** function i
1b985 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f  s responsible fo
1b986 72 20 73 65 65 69 6e 67 20 74 68 61 74 20 74 68  r seeing that th
1b987 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20  is structure is 
1b988 65 76 65 6e 74 75 61 6c 6c 79 0a 2a 2a 20 66 72  eventually.** fr
1b989 65 65 64 2e 20 20 41 64 64 20 74 68 65 20 4b 65  eed.  Add the Ke
1b98a 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  yInfo structure 
1b98b 74 6f 20 74 68 65 20 50 33 20 66 69 65 6c 64 20  to the P3 field 
1b98c 6f 66 20 61 6e 20 6f 70 63 6f 64 65 20 75 73 69  of an opcode usi
1b98d 6e 67 0a 2a 2a 20 50 33 5f 4b 45 59 49 4e 46 4f  ng.** P3_KEYINFO
1b98e 5f 48 41 4e 44 4f 46 46 20 69 73 20 74 68 65 20  _HANDOFF is the 
1b98f 75 73 75 61 6c 20 77 61 79 20 6f 66 20 64 65 61  usual way of dea
1b990 6c 69 6e 67 20 77 69 74 68 20 74 68 69 73 2e 0a  ling with this..
1b991 2a 2f 0a 73 74 61 74 69 63 20 4b 65 79 49 6e 66  */.static KeyInf
1b992 6f 20 2a 6b 65 79 49 6e 66 6f 46 72 6f 6d 45 78  o *keyInfoFromEx
1b993 70 72 4c 69 73 74 28 50 61 72 73 65 20 2a 70 50  prList(Parse *pP
1b994 61 72 73 65 2c 20 45 78 70 72 4c 69 73 74 20 2a  arse, ExprList *
1b995 70 4c 69 73 74 29 7b 0a 20 20 73 71 6c 69 74 65  pList){.  sqlite
1b996 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
1b997 64 62 3b 0a 20 20 69 6e 74 20 6e 45 78 70 72 3b  db;.  int nExpr;
1b998 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 49 6e 66  .  KeyInfo *pInf
1b999 6f 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72  o;.  struct Expr
1b99a 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
1b99b 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 6e 45  ;.  int i;..  nE
1b99c 78 70 72 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78  xpr = pList->nEx
1b99d 70 72 3b 0a 20 20 70 49 6e 66 6f 20 3d 20 73 71  pr;.  pInfo = sq
1b99e 6c 69 74 65 4d 61 6c 6c 6f 63 28 20 73 69 7a 65  liteMalloc( size
1b99f 6f 66 28 2a 70 49 6e 66 6f 29 20 2b 20 6e 45 78  of(*pInfo) + nEx
1b9a0 70 72 2a 28 73 69 7a 65 6f 66 28 43 6f 6c 6c 53  pr*(sizeof(CollS
1b9a1 65 71 2a 29 2b 31 29 20 29 3b 0a 20 20 69 66 28  eq*)+1) );.  if(
1b9a2 20 70 49 6e 66 6f 20 29 7b 0a 20 20 20 20 70 49   pInfo ){.    pI
1b9a3 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20  nfo->aSortOrder 
1b9a4 3d 20 28 75 38 2a 29 26 70 49 6e 66 6f 2d 3e 61  = (u8*)&pInfo->a
1b9a5 43 6f 6c 6c 5b 6e 45 78 70 72 5d 3b 0a 20 20 20  Coll[nExpr];.   
1b9a6 20 70 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 3d   pInfo->nField =
1b9a7 20 6e 45 78 70 72 3b 0a 20 20 20 20 70 49 6e 66   nExpr;.    pInf
1b9a8 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 64 62 29  o->enc = ENC(db)
1b9a9 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70  ;.    for(i=0, p
1b9aa 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69  Item=pList->a; i
1b9ab 3c 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74  <nExpr; i++, pIt
1b9ac 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 43 6f 6c  em++){.      Col
1b9ad 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 20  lSeq *pColl;.   
1b9ae 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74     pColl = sqlit
1b9af 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50  e3ExprCollSeq(pP
1b9b0 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e 70 45 78  arse, pItem->pEx
1b9b1 70 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21  pr);.      if( !
1b9b2 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20  pColl ){.       
1b9b3 20 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66   pColl = db->pDf
1b9b4 6c 74 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a  ltColl;.      }.
1b9b5 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f        pInfo->aCo
1b9b6 6c 6c 5b 69 5d 20 3d 20 70 43 6f 6c 6c 3b 0a 20  ll[i] = pColl;. 
1b9b7 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61 53 6f 72       pInfo->aSor
1b9b8 74 4f 72 64 65 72 5b 69 5d 20 3d 20 70 49 74 65  tOrder[i] = pIte
1b9b9 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20  m->sortOrder;.  
1b9ba 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
1b9bb 20 70 49 6e 66 6f 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a   pInfo;.}.../*.*
1b9bc 2a 20 49 66 20 74 68 65 20 69 6e 6e 65 72 20 6c  * If the inner l
1b9bd 6f 6f 70 20 77 61 73 20 67 65 6e 65 72 61 74 65  oop was generate
1b9be 64 20 75 73 69 6e 67 20 61 20 6e 6f 6e 2d 6e 75  d using a non-nu
1b9bf 6c 6c 20 70 4f 72 64 65 72 42 79 20 61 72 67 75  ll pOrderBy argu
1b9c0 6d 65 6e 74 2c 0a 2a 2a 20 74 68 65 6e 20 74 68  ment,.** then th
1b9c1 65 20 72 65 73 75 6c 74 73 20 77 65 72 65 20 70  e results were p
1b9c2 6c 61 63 65 64 20 69 6e 20 61 20 73 6f 72 74 65  laced in a sorte
1b9c3 72 2e 20 20 41 66 74 65 72 20 74 68 65 20 6c 6f  r.  After the lo
1b9c4 6f 70 20 69 73 20 74 65 72 6d 69 6e 61 74 65 64  op is terminated
1b9c5 0a 2a 2a 20 77 65 20 6e 65 65 64 20 74 6f 20 72  .** we need to r
1b9c6 75 6e 20 74 68 65 20 73 6f 72 74 65 72 20 61 6e  un the sorter an
1b9c7 64 20 6f 75 74 70 75 74 20 74 68 65 20 72 65 73  d output the res
1b9c8 75 6c 74 73 2e 20 20 54 68 65 20 66 6f 6c 6c 6f  ults.  The follo
1b9c9 77 69 6e 67 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  wing.** routine 
1b9ca 67 65 6e 65 72 61 74 65 73 20 74 68 65 20 63 6f  generates the co
1b9cb 64 65 20 6e 65 65 64 65 64 20 74 6f 20 64 6f 20  de needed to do 
1b9cc 74 68 61 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  that..*/.static 
1b9cd 76 6f 69 64 20 67 65 6e 65 72 61 74 65 53 6f 72  void generateSor
1b9ce 74 54 61 69 6c 28 0a 20 20 50 61 72 73 65 20 2a  tTail(.  Parse *
1b9cf 70 50 61 72 73 65 2c 20 20 20 2f 2a 20 50 61 72  pParse,   /* Par
1b9d0 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
1b9d1 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20    Select *p,    
1b9d2 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54     /* The SELECT
1b9d3 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
1b9d4 56 64 62 65 20 2a 76 2c 20 20 20 20 20 20 20 20  Vdbe *v,        
1b9d5 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64   /* Generate cod
1b9d6 65 20 69 6e 74 6f 20 74 68 69 73 20 56 44 42 45  e into this VDBE
1b9d7 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d   */.  int nColum
1b9d8 6e 2c 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  n,     /* Number
1b9d9 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 64   of columns of d
1b9da 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 65 44 65  ata */.  int eDe
1b9db 73 74 2c 20 20 20 20 20 20 20 2f 2a 20 57 72 69  st,       /* Wri
1b9dc 74 65 20 74 68 65 20 73 6f 72 74 65 64 20 72 65  te the sorted re
1b9dd 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20  sults here */.  
1b9de 69 6e 74 20 69 50 61 72 6d 20 20 20 20 20 20 20  int iParm       
1b9df 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 70 61 72   /* Optional par
1b9e0 61 6d 65 74 65 72 20 61 73 73 6f 63 69 61 74 65  ameter associate
1b9e1 64 20 77 69 74 68 20 65 44 65 73 74 20 2a 2f 0a  d with eDest */.
1b9e2 29 7b 0a 20 20 69 6e 74 20 62 72 6b 20 3d 20 73  ){.  int brk = s
1b9e3 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
1b9e4 62 65 6c 28 76 29 3b 0a 20 20 69 6e 74 20 63 6f  bel(v);.  int co
1b9e5 6e 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  nt = sqlite3Vdbe
1b9e6 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20  MakeLabel(v);.  
1b9e7 69 6e 74 20 61 64 64 72 3b 0a 20 20 69 6e 74 20  int addr;.  int 
1b9e8 69 54 61 62 3b 0a 20 20 69 6e 74 20 70 73 65 75  iTab;.  int pseu
1b9e9 64 6f 54 61 62 20 3d 20 30 3b 0a 20 20 45 78 70  doTab = 0;.  Exp
1b9ea 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 20  rList *pOrderBy 
1b9eb 3d 20 70 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 0a  = p->pOrderBy;..
1b9ec 20 20 69 54 61 62 20 3d 20 70 4f 72 64 65 72 42    iTab = pOrderB
1b9ed 79 2d 3e 69 45 43 75 72 73 6f 72 3b 0a 20 20 69  y->iECursor;.  i
1b9ee 66 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 43 61  f( eDest==SRT_Ca
1b9ef 6c 6c 62 61 63 6b 20 7c 7c 20 65 44 65 73 74 3d  llback || eDest=
1b9f0 3d 53 52 54 5f 53 75 62 72 6f 75 74 69 6e 65 20  =SRT_Subroutine 
1b9f1 29 7b 0a 20 20 20 20 70 73 65 75 64 6f 54 61 62  ){.    pseudoTab
1b9f2 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b   = pParse->nTab+
1b9f3 2b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  +;.    sqlite3Vd
1b9f4 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4f 70  beAddOp(v, OP_Op
1b9f5 65 6e 50 73 65 75 64 6f 2c 20 70 73 65 75 64 6f  enPseudo, pseudo
1b9f6 54 61 62 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  Tab, 0);.    sql
1b9f7 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1b9f8 20 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e   OP_SetNumColumn
1b9f9 73 2c 20 70 73 65 75 64 6f 54 61 62 2c 20 6e 43  s, pseudoTab, nC
1b9fa 6f 6c 75 6d 6e 29 3b 0a 20 20 7d 0a 20 20 61 64  olumn);.  }.  ad
1b9fb 64 72 20 3d 20 31 20 2b 20 73 71 6c 69 74 65 33  dr = 1 + sqlite3
1b9fc 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1b9fd 53 6f 72 74 2c 20 69 54 61 62 2c 20 62 72 6b 29  Sort, iTab, brk)
1b9fe 3b 0a 20 20 63 6f 64 65 4f 66 66 73 65 74 28 76  ;.  codeOffset(v
1b9ff 2c 20 70 2c 20 63 6f 6e 74 2c 20 30 29 3b 0a 20  , p, cont, 0);. 
1ba00 20 69 66 28 20 65 44 65 73 74 3d 3d 53 52 54 5f   if( eDest==SRT_
1ba01 43 61 6c 6c 62 61 63 6b 20 7c 7c 20 65 44 65 73  Callback || eDes
1ba02 74 3d 3d 53 52 54 5f 53 75 62 72 6f 75 74 69 6e  t==SRT_Subroutin
1ba03 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  e ){.    sqlite3
1ba04 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1ba05 49 6e 74 65 67 65 72 2c 20 31 2c 20 30 29 3b 0a  Integer, 1, 0);.
1ba06 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62    }.  sqlite3Vdb
1ba07 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6f 6c  eAddOp(v, OP_Col
1ba08 75 6d 6e 2c 20 69 54 61 62 2c 20 70 4f 72 64 65  umn, iTab, pOrde
1ba09 72 42 79 2d 3e 6e 45 78 70 72 20 2b 20 31 29 3b  rBy->nExpr + 1);
1ba0a 0a 20 20 73 77 69 74 63 68 28 20 65 44 65 73 74  .  switch( eDest
1ba0b 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 52 54   ){.    case SRT
1ba0c 5f 54 61 62 6c 65 3a 0a 20 20 20 20 63 61 73 65  _Table:.    case
1ba0d 20 53 52 54 5f 45 70 68 65 6d 54 61 62 3a 20 7b   SRT_EphemTab: {
1ba0e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1ba0f 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4e 65  beAddOp(v, OP_Ne
1ba10 77 52 6f 77 69 64 2c 20 69 50 61 72 6d 2c 20 30  wRowid, iParm, 0
1ba11 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1ba12 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1ba13 50 75 6c 6c 2c 20 31 2c 20 30 29 3b 0a 20 20 20  Pull, 1, 0);.   
1ba14 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1ba15 64 4f 70 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74  dOp(v, OP_Insert
1ba16 2c 20 69 50 61 72 6d 2c 20 4f 50 46 4c 41 47 5f  , iParm, OPFLAG_
1ba17 41 50 50 45 4e 44 29 3b 0a 20 20 20 20 20 20 62  APPEND);.      b
1ba18 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e  reak;.    }.#ifn
1ba19 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1ba1a 53 55 42 51 55 45 52 59 0a 20 20 20 20 63 61 73  SUBQUERY.    cas
1ba1b 65 20 53 52 54 5f 53 65 74 3a 20 7b 0a 20 20 20  e SRT_Set: {.   
1ba1c 20 20 20 61 73 73 65 72 74 28 20 6e 43 6f 6c 75     assert( nColu
1ba1d 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 73  mn==1 );.      s
1ba1e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1ba1f 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 2d  v, OP_NotNull, -
1ba20 31 2c 20 73 71 6c 69 74 65 33 56 64 62 65 43 75  1, sqlite3VdbeCu
1ba21 72 72 65 6e 74 41 64 64 72 28 76 29 2b 33 29 3b  rrentAddr(v)+3);
1ba22 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1ba23 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 50 6f  beAddOp(v, OP_Po
1ba24 70 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 20 20  p, 1, 0);.      
1ba25 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1ba26 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
1ba27 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
1ba28 6e 74 41 64 64 72 28 76 29 2b 33 29 3b 0a 20 20  ntAddr(v)+3);.  
1ba29 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4f      sqlite3VdbeO
1ba2a 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63  p3(v, OP_MakeRec
1ba2b 6f 72 64 2c 20 31 2c 20 30 2c 20 26 70 2d 3e 61  ord, 1, 0, &p->a
1ba2c 66 66 69 6e 69 74 79 2c 20 31 29 3b 0a 20 20 20  ffinity, 1);.   
1ba2d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1ba2e 64 4f 70 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73  dOp(v, OP_IdxIns
1ba2f 65 72 74 2c 20 28 69 50 61 72 6d 26 30 78 30 30  ert, (iParm&0x00
1ba30 30 30 46 46 46 46 29 2c 20 30 29 3b 0a 20 20 20  00FFFF), 0);.   
1ba31 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1ba32 20 20 20 20 63 61 73 65 20 53 52 54 5f 4d 65 6d      case SRT_Mem
1ba33 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  : {.      assert
1ba34 28 20 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a  ( nColumn==1 );.
1ba35 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1ba36 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d  eAddOp(v, OP_Mem
1ba37 53 74 6f 72 65 2c 20 69 50 61 72 6d 2c 20 31 29  Store, iParm, 1)
1ba38 3b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4c  ;.      /* The L
1ba39 49 4d 49 54 20 63 6c 61 75 73 65 20 77 69 6c 6c  IMIT clause will
1ba3a 20 74 65 72 6d 69 6e 61 74 65 20 74 68 65 20 6c   terminate the l
1ba3b 6f 6f 70 20 66 6f 72 20 75 73 20 2a 2f 0a 20 20  oop for us */.  
1ba3c 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1ba3d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65  .#endif.    case
1ba3e 20 53 52 54 5f 43 61 6c 6c 62 61 63 6b 3a 0a 20   SRT_Callback:. 
1ba3f 20 20 20 63 61 73 65 20 53 52 54 5f 53 75 62 72     case SRT_Subr
1ba40 6f 75 74 69 6e 65 3a 20 7b 0a 20 20 20 20 20 20  outine: {.      
1ba41 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 73 71 6c  int i;.      sql
1ba42 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1ba43 20 4f 50 5f 49 6e 73 65 72 74 2c 20 70 73 65 75   OP_Insert, pseu
1ba44 64 6f 54 61 62 2c 20 30 29 3b 0a 20 20 20 20 20  doTab, 0);.     
1ba45 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c   for(i=0; i<nCol
1ba46 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  umn; i++){.     
1ba47 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1ba48 64 4f 70 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  dOp(v, OP_Column
1ba49 2c 20 70 73 65 75 64 6f 54 61 62 2c 20 69 29 3b  , pseudoTab, i);
1ba4a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
1ba4b 66 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 43 61  f( eDest==SRT_Ca
1ba4c 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 20 20  llback ){.      
1ba4d 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1ba4e 4f 70 28 76 2c 20 4f 50 5f 43 61 6c 6c 62 61 63  Op(v, OP_Callbac
1ba4f 6b 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 30 29 3b 0a  k, nColumn, 0);.
1ba50 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1ba51 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1ba52 41 64 64 4f 70 28 76 2c 20 4f 50 5f 47 6f 73 75  AddOp(v, OP_Gosu
1ba53 62 2c 20 30 2c 20 69 50 61 72 6d 29 3b 0a 20 20  b, 0, iParm);.  
1ba54 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
1ba55 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66  k;.    }.    def
1ba56 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 2f 2a  ault: {.      /*
1ba57 20 44 6f 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   Do nothing */. 
1ba58 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1ba59 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4a 75 6d 70  }.  }..  /* Jump
1ba5a 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74   to the end of t
1ba5b 68 65 20 6c 6f 6f 70 20 77 68 65 6e 20 74 68 65  he loop when the
1ba5c 20 4c 49 4d 49 54 20 69 73 20 72 65 61 63 68 65   LIMIT is reache
1ba5d 64 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e  d.  */.  if( p->
1ba5e 69 4c 69 6d 69 74 3e 3d 30 20 29 7b 0a 20 20 20  iLimit>=0 ){.   
1ba5f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1ba60 70 28 76 2c 20 4f 50 5f 4d 65 6d 49 6e 63 72 2c  p(v, OP_MemIncr,
1ba61 20 2d 31 2c 20 70 2d 3e 69 4c 69 6d 69 74 29 3b   -1, p->iLimit);
1ba62 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1ba63 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 66 4d 65  AddOp(v, OP_IfMe
1ba64 6d 5a 65 72 6f 2c 20 70 2d 3e 69 4c 69 6d 69 74  mZero, p->iLimit
1ba65 2c 20 62 72 6b 29 3b 0a 20 20 7d 0a 0a 20 20 2f  , brk);.  }..  /
1ba66 2a 20 54 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20  * The bottom of 
1ba67 74 68 65 20 6c 6f 6f 70 0a 20 20 2a 2f 0a 20 20  the loop.  */.  
1ba68 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c  sqlite3VdbeResol
1ba69 76 65 4c 61 62 65 6c 28 76 2c 20 63 6f 6e 74 29  veLabel(v, cont)
1ba6a 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
1ba6b 64 64 4f 70 28 76 2c 20 4f 50 5f 4e 65 78 74 2c  ddOp(v, OP_Next,
1ba6c 20 69 54 61 62 2c 20 61 64 64 72 29 3b 0a 20 20   iTab, addr);.  
1ba6d 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c  sqlite3VdbeResol
1ba6e 76 65 4c 61 62 65 6c 28 76 2c 20 62 72 6b 29 3b  veLabel(v, brk);
1ba6f 0a 20 20 69 66 28 20 65 44 65 73 74 3d 3d 53 52  .  if( eDest==SR
1ba70 54 5f 43 61 6c 6c 62 61 63 6b 20 7c 7c 20 65 44  T_Callback || eD
1ba71 65 73 74 3d 3d 53 52 54 5f 53 75 62 72 6f 75 74  est==SRT_Subrout
1ba72 69 6e 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ine ){.    sqlit
1ba73 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1ba74 50 5f 43 6c 6f 73 65 2c 20 70 73 65 75 64 6f 54  P_Close, pseudoT
1ba75 61 62 2c 20 30 29 3b 0a 20 20 7d 0a 0a 7d 0a 0a  ab, 0);.  }..}..
1ba76 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70  /*.** Return a p
1ba77 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 72 69  ointer to a stri
1ba78 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  ng containing th
1ba79 65 20 27 64 65 63 6c 61 72 61 74 69 6f 6e 20 74  e 'declaration t
1ba7a 79 70 65 27 20 6f 66 20 74 68 65 0a 2a 2a 20 65  ype' of the.** e
1ba7b 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 2e  xpression pExpr.
1ba7c 20 54 68 65 20 73 74 72 69 6e 67 20 6d 61 79 20   The string may 
1ba7d 62 65 20 74 72 65 61 74 65 64 20 61 73 20 73 74  be treated as st
1ba7e 61 74 69 63 20 62 79 20 74 68 65 20 63 61 6c 6c  atic by the call
1ba7f 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65  er..**.** The de
1ba80 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20 69  claration type i
1ba81 73 20 74 68 65 20 65 78 61 63 74 20 64 61 74 61  s the exact data
1ba82 74 79 70 65 20 64 65 66 69 6e 69 74 69 6f 6e 20  type definition 
1ba83 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20 74  extracted from t
1ba84 68 65 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 43  he.** original C
1ba85 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74  REATE TABLE stat
1ba86 65 6d 65 6e 74 20 69 66 20 74 68 65 20 65 78 70  ement if the exp
1ba87 72 65 73 73 69 6f 6e 20 69 73 20 61 20 63 6f 6c  ression is a col
1ba88 75 6d 6e 2e 20 54 68 65 0a 2a 2a 20 64 65 63 6c  umn. The.** decl
1ba89 61 72 61 74 69 6f 6e 20 74 79 70 65 20 66 6f 72  aration type for
1ba8a 20 61 20 52 4f 57 49 44 20 66 69 65 6c 64 20 69   a ROWID field i
1ba8b 73 20 49 4e 54 45 47 45 52 2e 20 45 78 61 63 74  s INTEGER. Exact
1ba8c 6c 79 20 77 68 65 6e 20 61 6e 20 65 78 70 72 65  ly when an expre
1ba8d 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 63 6f 6e 73  ssion.** is cons
1ba8e 69 64 65 72 65 64 20 61 20 63 6f 6c 75 6d 6e 20  idered a column 
1ba8f 63 61 6e 20 62 65 20 63 6f 6d 70 6c 65 78 20 69  can be complex i
1ba90 6e 20 74 68 65 20 70 72 65 73 65 6e 63 65 20 6f  n the presence o
1ba91 66 20 73 75 62 71 75 65 72 69 65 73 2e 20 54 68  f subqueries. Th
1ba92 65 0a 2a 2a 20 72 65 73 75 6c 74 2d 73 65 74 20  e.** result-set 
1ba93 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 61 6c  expression in al
1ba94 6c 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  l of the followi
1ba95 6e 67 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  ng SELECT statem
1ba96 65 6e 74 73 20 69 73 20 0a 2a 2a 20 63 6f 6e 73  ents is .** cons
1ba97 69 64 65 72 65 64 20 61 20 63 6f 6c 75 6d 6e 20  idered a column 
1ba98 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  by this function
1ba99 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54  ..**.**   SELECT
1ba9a 20 63 6f 6c 20 46 52 4f 4d 20 74 62 6c 3b 0a 2a   col FROM tbl;.*
1ba9b 2a 20 20 20 53 45 4c 45 43 54 20 28 53 45 4c 45  *   SELECT (SELE
1ba9c 43 54 20 63 6f 6c 20 46 52 4f 4d 20 74 62 6c 3b  CT col FROM tbl;
1ba9d 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 28 53 45  .**   SELECT (SE
1ba9e 4c 45 43 54 20 63 6f 6c 20 46 52 4f 4d 20 74 62  LECT col FROM tb
1ba9f 6c 29 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20  l);.**   SELECT 
1baa0 61 62 63 20 46 52 4f 4d 20 28 53 45 4c 45 43 54  abc FROM (SELECT
1baa1 20 63 6f 6c 20 41 53 20 61 62 63 20 46 52 4f 4d   col AS abc FROM
1baa2 20 74 62 6c 29 3b 0a 2a 2a 20 0a 2a 2a 20 54 68   tbl);.** .** Th
1baa3 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79  e declaration ty
1baa4 70 65 20 66 6f 72 20 61 6e 79 20 65 78 70 72 65  pe for any expre
1baa5 73 73 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e  ssion other than
1baa6 20 61 20 63 6f 6c 75 6d 6e 20 69 73 20 4e 55 4c   a column is NUL
1baa7 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  L..*/.static con
1baa8 73 74 20 63 68 61 72 20 2a 63 6f 6c 75 6d 6e 54  st char *columnT
1baa9 79 70 65 28 0a 20 20 4e 61 6d 65 43 6f 6e 74 65  ype(.  NameConte
1baaa 78 74 20 2a 70 4e 43 2c 20 0a 20 20 45 78 70 72  xt *pNC, .  Expr
1baab 20 2a 70 45 78 70 72 2c 0a 20 20 63 6f 6e 73 74   *pExpr,.  const
1baac 20 63 68 61 72 20 2a 2a 70 7a 4f 72 69 67 69 6e   char **pzOrigin
1baad 44 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  Db,.  const char
1baae 20 2a 2a 70 7a 4f 72 69 67 69 6e 54 61 62 2c 0a   **pzOriginTab,.
1baaf 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70    const char **p
1bab0 7a 4f 72 69 67 69 6e 43 6f 6c 0a 29 7b 0a 20 20  zOriginCol.){.  
1bab1 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54 79 70  char const *zTyp
1bab2 65 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 63 6f  e = 0;.  char co
1bab3 6e 73 74 20 2a 7a 4f 72 69 67 69 6e 44 62 20 3d  nst *zOriginDb =
1bab4 20 30 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74   0;.  char const
1bab5 20 2a 7a 4f 72 69 67 69 6e 54 61 62 20 3d 20 30   *zOriginTab = 0
1bab6 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  ;.  char const *
1bab7 7a 4f 72 69 67 69 6e 43 6f 6c 20 3d 20 30 3b 0a  zOriginCol = 0;.
1bab8 20 20 69 6e 74 20 6a 3b 0a 20 20 69 66 28 20 70    int j;.  if( p
1bab9 45 78 70 72 3d 3d 30 20 7c 7c 20 70 4e 43 2d 3e  Expr==0 || pNC->
1baba 70 53 72 63 4c 69 73 74 3d 3d 30 20 29 20 72 65  pSrcList==0 ) re
1babb 74 75 72 6e 20 30 3b 0a 0a 20 20 73 77 69 74 63  turn 0;..  switc
1babc 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a  h( pExpr->op ){.
1babd 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f      case TK_AGG_
1babe 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 63 61 73 65  COLUMN:.    case
1babf 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20   TK_COLUMN: {.  
1bac0 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65      /* The expre
1bac1 73 73 69 6f 6e 20 69 73 20 61 20 63 6f 6c 75 6d  ssion is a colum
1bac2 6e 2e 20 4c 6f 63 61 74 65 20 74 68 65 20 74 61  n. Locate the ta
1bac3 62 6c 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69  ble the column i
1bac4 73 20 62 65 69 6e 67 0a 20 20 20 20 20 20 2a 2a  s being.      **
1bac5 20 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20   extracted from 
1bac6 69 6e 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2e 70  in NameContext.p
1bac7 53 72 63 4c 69 73 74 2e 20 54 68 69 73 20 74 61  SrcList. This ta
1bac8 62 6c 65 20 6d 61 79 20 62 65 20 72 65 61 6c 0a  ble may be real.
1bac9 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73        ** databas
1baca 65 20 74 61 62 6c 65 20 6f 72 20 61 20 73 75 62  e table or a sub
1bacb 71 75 65 72 79 2e 0a 20 20 20 20 20 20 2a 2f 0a  query..      */.
1bacc 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61        Table *pTa
1bacd 62 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  b = 0;          
1bace 20 20 2f 2a 20 54 61 62 6c 65 20 73 74 72 75 63    /* Table struc
1bacf 74 75 72 65 20 63 6f 6c 75 6d 6e 20 69 73 20 65  ture column is e
1bad0 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20 2a 2f  xtracted from */
1bad1 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70  .      Select *p
1bad2 53 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  S = 0;          
1bad3 20 20 20 2f 2a 20 53 65 6c 65 63 74 20 74 68 65     /* Select the
1bad4 20 63 6f 6c 75 6d 6e 20 69 73 20 65 78 74 72 61   column is extra
1bad5 63 74 65 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 20  cted from */.   
1bad6 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 45     int iCol = pE
1bad7 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 20 20 2f  xpr->iColumn;  /
1bad8 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d  * Index of colum
1bad9 6e 20 69 6e 20 70 54 61 62 20 2a 2f 0a 20 20 20  n in pTab */.   
1bada 20 20 20 77 68 69 6c 65 28 20 70 4e 43 20 26 26     while( pNC &&
1badb 20 21 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20   !pTab ){.      
1badc 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c    SrcList *pTabL
1badd 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c  ist = pNC->pSrcL
1bade 69 73 74 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  ist;.        for
1badf 28 6a 3d 30 3b 6a 3c 70 54 61 62 4c 69 73 74 2d  (j=0;j<pTabList-
1bae0 3e 6e 53 72 63 20 26 26 20 70 54 61 62 4c 69 73  >nSrc && pTabLis
1bae1 74 2d 3e 61 5b 6a 5d 2e 69 43 75 72 73 6f 72 21  t->a[j].iCursor!
1bae2 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3b 6a  =pExpr->iTable;j
1bae3 2b 2b 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ++);.        if(
1bae4 20 6a 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72   j<pTabList->nSr
1bae5 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  c ){.          p
1bae6 54 61 62 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e  Tab = pTabList->
1bae7 61 5b 6a 5d 2e 70 54 61 62 3b 0a 20 20 20 20 20  a[j].pTab;.     
1bae8 20 20 20 20 20 70 53 20 3d 20 70 54 61 62 4c 69       pS = pTabLi
1bae9 73 74 2d 3e 61 5b 6a 5d 2e 70 53 65 6c 65 63 74  st->a[j].pSelect
1baea 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
1baeb 0a 20 20 20 20 20 20 20 20 20 20 70 4e 43 20 3d  .          pNC =
1baec 20 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20   pNC->pNext;.   
1baed 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a       }.      }..
1baee 20 20 20 20 20 20 69 66 28 20 70 54 61 62 3d 3d        if( pTab==
1baef 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  0 ){.        /* 
1baf0 46 49 58 20 4d 45 3a 0a 20 20 20 20 20 20 20 20  FIX ME:.        
1baf1 2a 2a 20 54 68 69 73 20 63 61 6e 20 6f 63 63 75  ** This can occu
1baf2 72 73 20 69 66 20 79 6f 75 20 68 61 76 65 20 73  rs if you have s
1baf3 6f 6d 65 74 68 69 6e 67 20 6c 69 6b 65 20 22 53  omething like "S
1baf4 45 4c 45 43 54 20 6e 65 77 2e 78 3b 22 20 69 6e  ELECT new.x;" in
1baf5 73 69 64 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  side.        ** 
1baf6 61 20 74 72 69 67 67 65 72 2e 20 20 49 6e 20 6f  a trigger.  In o
1baf7 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 79  ther words, if y
1baf8 6f 75 20 72 65 66 65 72 65 6e 63 65 20 74 68 65  ou reference the
1baf9 20 73 70 65 63 69 61 6c 20 22 6e 65 77 22 0a 20   special "new". 
1bafa 20 20 20 20 20 20 20 2a 2a 20 74 61 62 6c 65 20         ** table 
1bafb 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  in the result se
1bafc 74 20 6f 66 20 61 20 73 65 6c 65 63 74 2e 20 20  t of a select.  
1bafd 57 65 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 61  We do not have a
1bafe 20 67 6f 6f 64 20 77 61 79 0a 20 20 20 20 20 20   good way.      
1baff 20 20 2a 2a 20 74 6f 20 66 69 6e 64 20 74 68 65    ** to find the
1bb00 20 61 63 74 75 61 6c 20 74 61 62 6c 65 20 74 79   actual table ty
1bb01 70 65 2c 20 73 6f 20 63 61 6c 6c 20 69 74 20 22  pe, so call it "
1bb02 54 45 58 54 22 2e 20 20 54 68 69 73 20 69 73 20  TEXT".  This is 
1bb03 72 65 61 6c 6c 79 0a 20 20 20 20 20 20 20 20 2a  really.        *
1bb04 2a 20 73 6f 6d 65 74 68 69 6e 67 20 6f 66 20 61  * something of a
1bb05 20 62 75 67 2c 20 62 75 74 20 49 20 64 6f 20 6e   bug, but I do n
1bb06 6f 74 20 6b 6e 6f 77 20 68 6f 77 20 74 6f 20 66  ot know how to f
1bb07 69 78 20 69 74 2e 0a 20 20 20 20 20 20 20 20 2a  ix it..        *
1bb08 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54 68 69  *.        ** Thi
1bb09 73 20 63 6f 64 65 20 64 6f 65 73 20 6e 6f 74 20  s code does not 
1bb0a 70 72 6f 64 75 63 65 20 74 68 65 20 63 6f 72 72  produce the corr
1bb0b 65 63 74 20 61 6e 73 77 65 72 20 2d 20 69 74 20  ect answer - it 
1bb0c 6a 75 73 74 20 70 72 65 76 65 6e 74 73 0a 20 20  just prevents.  
1bb0d 20 20 20 20 20 20 2a 2a 20 61 20 73 65 67 66 61        ** a segfa
1bb0e 75 6c 74 2e 20 20 53 65 65 20 74 69 63 6b 65 74  ult.  See ticket
1bb0f 20 23 31 32 32 39 2e 0a 20 20 20 20 20 20 20 20   #1229..        
1bb10 2a 2f 0a 20 20 20 20 20 20 20 20 7a 54 79 70 65  */.        zType
1bb11 20 3d 20 22 54 45 58 54 22 3b 0a 20 20 20 20 20   = "TEXT";.     
1bb12 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
1bb13 7d 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  }..      assert(
1bb14 20 70 54 61 62 20 29 3b 0a 20 20 20 20 20 20 69   pTab );.      i
1bb15 66 28 20 70 53 20 29 7b 0a 20 20 20 20 20 20 20  f( pS ){.       
1bb16 20 2f 2a 20 54 68 65 20 22 74 61 62 6c 65 22 20   /* The "table" 
1bb17 69 73 20 61 63 74 75 61 6c 6c 79 20 61 20 73 75  is actually a su
1bb18 62 2d 73 65 6c 65 63 74 20 6f 72 20 61 20 76 69  b-select or a vi
1bb19 65 77 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  ew in the FROM c
1bb1a 6c 61 75 73 65 0a 20 20 20 20 20 20 20 20 2a 2a  lause.        **
1bb1b 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 73   of the SELECT s
1bb1c 74 61 74 65 6d 65 6e 74 2e 20 52 65 74 75 72 6e  tatement. Return
1bb1d 20 74 68 65 20 64 65 63 6c 61 72 61 74 69 6f 6e   the declaration
1bb1e 20 74 79 70 65 20 61 6e 64 20 6f 72 69 67 69 6e   type and origin
1bb1f 0a 20 20 20 20 20 20 20 20 2a 2a 20 64 61 74 61  .        ** data
1bb20 20 66 6f 72 20 74 68 65 20 72 65 73 75 6c 74 2d   for the result-
1bb21 73 65 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  set column of th
1bb22 65 20 73 75 62 2d 73 65 6c 65 63 74 2e 0a 20 20  e sub-select..  
1bb23 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
1bb24 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20 26 26 20   if( iCol>=0 && 
1bb25 69 43 6f 6c 3c 70 53 2d 3e 70 45 4c 69 73 74 2d  iCol<pS->pEList-
1bb26 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20  >nExpr ){.      
1bb27 20 20 20 20 2f 2a 20 49 66 20 69 43 6f 6c 20 69      /* If iCol i
1bb28 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f  s less than zero
1bb29 2c 20 74 68 65 6e 20 74 68 65 20 65 78 70 72 65  , then the expre
1bb2a 73 73 69 6f 6e 20 72 65 71 75 65 73 74 73 20 74  ssion requests t
1bb2b 68 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  he.          ** 
1bb2c 72 6f 77 69 64 20 6f 66 20 74 68 65 20 73 75 62  rowid of the sub
1bb2d 2d 73 65 6c 65 63 74 20 6f 72 20 76 69 65 77 2e  -select or view.
1bb2e 20 54 68 69 73 20 65 78 70 72 65 73 73 69 6f 6e   This expression
1bb2f 20 69 73 20 6c 65 67 61 6c 20 28 73 65 65 20 0a   is legal (see .
1bb30 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 65 73            ** tes
1bb31 74 20 63 61 73 65 20 6d 69 73 63 32 2e 32 2e 32  t case misc2.2.2
1bb32 29 20 2d 20 69 74 20 61 6c 77 61 79 73 20 65 76  ) - it always ev
1bb33 61 6c 75 61 74 65 73 20 74 6f 20 4e 55 4c 4c 2e  aluates to NULL.
1bb34 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  .          */.  
1bb35 20 20 20 20 20 20 20 20 4e 61 6d 65 43 6f 6e 74          NameCont
1bb36 65 78 74 20 73 4e 43 3b 0a 20 20 20 20 20 20 20  ext sNC;.       
1bb37 20 20 20 45 78 70 72 20 2a 70 20 3d 20 70 53 2d     Expr *p = pS-
1bb38 3e 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d  >pEList->a[iCol]
1bb39 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20  .pExpr;.        
1bb3a 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d    sNC.pSrcList =
1bb3b 20 70 53 2d 3e 70 53 72 63 3b 0a 20 20 20 20 20   pS->pSrc;.     
1bb3c 20 20 20 20 20 73 4e 43 2e 70 4e 65 78 74 20 3d       sNC.pNext =
1bb3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 73 4e   0;.          sN
1bb3e 43 2e 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e  C.pParse = pNC->
1bb3f 70 50 61 72 73 65 3b 0a 20 20 20 20 20 20 20 20  pParse;.        
1bb40 20 20 7a 54 79 70 65 20 3d 20 63 6f 6c 75 6d 6e    zType = column
1bb41 54 79 70 65 28 26 73 4e 43 2c 20 70 2c 20 26 7a  Type(&sNC, p, &z
1bb42 4f 72 69 67 69 6e 44 62 2c 20 26 7a 4f 72 69 67  OriginDb, &zOrig
1bb43 69 6e 54 61 62 2c 20 26 7a 4f 72 69 67 69 6e 43  inTab, &zOriginC
1bb44 6f 6c 29 3b 20 0a 20 20 20 20 20 20 20 20 7d 0a  ol); .        }.
1bb45 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
1bb46 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29 7b  pTab->pSchema ){
1bb47 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 20 72 65  .        /* A re
1bb48 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  al table */.    
1bb49 20 20 20 20 61 73 73 65 72 74 28 20 21 70 53 20      assert( !pS 
1bb4a 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  );.        if( i
1bb4b 43 6f 6c 3c 30 20 29 20 69 43 6f 6c 20 3d 20 70  Col<0 ) iCol = p
1bb4c 54 61 62 2d 3e 69 50 4b 65 79 3b 0a 20 20 20 20  Tab->iPKey;.    
1bb4d 20 20 20 20 61 73 73 65 72 74 28 20 69 43 6f 6c      assert( iCol
1bb4e 3d 3d 2d 31 20 7c 7c 20 28 69 43 6f 6c 3e 3d 30  ==-1 || (iCol>=0
1bb4f 20 26 26 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e   && iCol<pTab->n
1bb50 43 6f 6c 29 20 29 3b 0a 20 20 20 20 20 20 20 20  Col) );.        
1bb51 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b 0a 20 20  if( iCol<0 ){.  
1bb52 20 20 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20          zType = 
1bb53 22 49 4e 54 45 47 45 52 22 3b 0a 20 20 20 20 20  "INTEGER";.     
1bb54 20 20 20 20 20 7a 4f 72 69 67 69 6e 43 6f 6c 20       zOriginCol 
1bb55 3d 20 22 72 6f 77 69 64 22 3b 0a 20 20 20 20 20  = "rowid";.     
1bb56 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1bb57 20 20 20 20 7a 54 79 70 65 20 3d 20 70 54 61 62      zType = pTab
1bb58 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 54 79  ->aCol[iCol].zTy
1bb59 70 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 4f  pe;.          zO
1bb5a 72 69 67 69 6e 43 6f 6c 20 3d 20 70 54 61 62 2d  riginCol = pTab-
1bb5b 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d  >aCol[iCol].zNam
1bb5c 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  e;.        }.   
1bb5d 20 20 20 20 20 7a 4f 72 69 67 69 6e 54 61 62 20       zOriginTab 
1bb5e 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20  = pTab->zName;. 
1bb5f 20 20 20 20 20 20 20 69 66 28 20 70 4e 43 2d 3e         if( pNC->
1bb60 70 50 61 72 73 65 20 29 7b 0a 20 20 20 20 20 20  pParse ){.      
1bb61 20 20 20 20 69 6e 74 20 69 44 62 20 3d 20 73 71      int iDb = sq
1bb62 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64  lite3SchemaToInd
1bb63 65 78 28 70 4e 43 2d 3e 70 50 61 72 73 65 2d 3e  ex(pNC->pParse->
1bb64 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
1bb65 61 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 4f  a);.          zO
1bb66 72 69 67 69 6e 44 62 20 3d 20 70 4e 43 2d 3e 70  riginDb = pNC->p
1bb67 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 69  Parse->db->aDb[i
1bb68 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20  Db].zName;.     
1bb69 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
1bb6a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1bb6b 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1bb6c 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20 20  MIT_SUBQUERY.   
1bb6d 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 54 3a   case TK_SELECT:
1bb6e 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20   {.      /* The 
1bb6f 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20  expression is a 
1bb70 73 75 62 2d 73 65 6c 65 63 74 2e 20 52 65 74 75  sub-select. Retu
1bb71 72 6e 20 74 68 65 20 64 65 63 6c 61 72 61 74 69  rn the declarati
1bb72 6f 6e 20 74 79 70 65 20 61 6e 64 0a 20 20 20 20  on type and.    
1bb73 20 20 2a 2a 20 6f 72 69 67 69 6e 20 69 6e 66 6f    ** origin info
1bb74 20 66 6f 72 20 74 68 65 20 73 69 6e 67 6c 65 20   for the single 
1bb75 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72 65  column in the re
1bb76 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65 20  sult set of the 
1bb77 53 45 4c 45 43 54 0a 20 20 20 20 20 20 2a 2a 20  SELECT.      ** 
1bb78 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20 20 20  statement..     
1bb79 20 2a 2f 0a 20 20 20 20 20 20 4e 61 6d 65 43 6f   */.      NameCo
1bb7a 6e 74 65 78 74 20 73 4e 43 3b 0a 20 20 20 20 20  ntext sNC;.     
1bb7b 20 53 65 6c 65 63 74 20 2a 70 53 20 3d 20 70 45   Select *pS = pE
1bb7c 78 70 72 2d 3e 70 53 65 6c 65 63 74 3b 0a 20 20  xpr->pSelect;.  
1bb7d 20 20 20 20 45 78 70 72 20 2a 70 20 3d 20 70 53      Expr *p = pS
1bb7e 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70  ->pEList->a[0].p
1bb7f 45 78 70 72 3b 0a 20 20 20 20 20 20 73 4e 43 2e  Expr;.      sNC.
1bb80 70 53 72 63 4c 69 73 74 20 3d 20 70 53 2d 3e 70  pSrcList = pS->p
1bb81 53 72 63 3b 0a 20 20 20 20 20 20 73 4e 43 2e 70  Src;.      sNC.p
1bb82 4e 65 78 74 20 3d 20 70 4e 43 3b 0a 20 20 20 20  Next = pNC;.    
1bb83 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70    sNC.pParse = p
1bb84 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 20 20  NC->pParse;.    
1bb85 20 20 7a 54 79 70 65 20 3d 20 63 6f 6c 75 6d 6e    zType = column
1bb86 54 79 70 65 28 26 73 4e 43 2c 20 70 2c 20 26 7a  Type(&sNC, p, &z
1bb87 4f 72 69 67 69 6e 44 62 2c 20 26 7a 4f 72 69 67  OriginDb, &zOrig
1bb88 69 6e 54 61 62 2c 20 26 7a 4f 72 69 67 69 6e 43  inTab, &zOriginC
1bb89 6f 6c 29 3b 20 0a 20 20 20 20 20 20 62 72 65 61  ol); .      brea
1bb8a 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  k;.    }.#endif.
1bb8b 20 20 7d 0a 20 20 0a 20 20 69 66 28 20 70 7a 4f    }.  .  if( pzO
1bb8c 72 69 67 69 6e 44 62 20 29 7b 0a 20 20 20 20 61  riginDb ){.    a
1bb8d 73 73 65 72 74 28 20 70 7a 4f 72 69 67 69 6e 54  ssert( pzOriginT
1bb8e 61 62 20 26 26 20 70 7a 4f 72 69 67 69 6e 43 6f  ab && pzOriginCo
1bb8f 6c 20 29 3b 0a 20 20 20 20 2a 70 7a 4f 72 69 67  l );.    *pzOrig
1bb90 69 6e 44 62 20 3d 20 7a 4f 72 69 67 69 6e 44 62  inDb = zOriginDb
1bb91 3b 0a 20 20 20 20 2a 70 7a 4f 72 69 67 69 6e 54  ;.    *pzOriginT
1bb92 61 62 20 3d 20 7a 4f 72 69 67 69 6e 54 61 62 3b  ab = zOriginTab;
1bb93 0a 20 20 20 20 2a 70 7a 4f 72 69 67 69 6e 43 6f  .    *pzOriginCo
1bb94 6c 20 3d 20 7a 4f 72 69 67 69 6e 43 6f 6c 3b 0a  l = zOriginCol;.
1bb95 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 54 79    }.  return zTy
1bb96 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e  pe;.}../*.** Gen
1bb97 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20  erate code that 
1bb98 77 69 6c 6c 20 74 65 6c 6c 20 74 68 65 20 56 44  will tell the VD
1bb99 42 45 20 74 68 65 20 64 65 63 6c 61 72 61 74 69  BE the declarati
1bb9a 6f 6e 20 74 79 70 65 73 20 6f 66 20 63 6f 6c 75  on types of colu
1bb9b 6d 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65  mns.** in the re
1bb9c 73 75 6c 74 20 73 65 74 2e 0a 2a 2f 0a 73 74 61  sult set..*/.sta
1bb9d 74 69 63 20 76 6f 69 64 20 67 65 6e 65 72 61 74  tic void generat
1bb9e 65 43 6f 6c 75 6d 6e 54 79 70 65 73 28 0a 20 20  eColumnTypes(.  
1bb9f 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
1bba0 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f      /* Parser co
1bba1 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69  ntext */.  SrcLi
1bba2 73 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 20 2f  st *pTabList,  /
1bba3 2a 20 4c 69 73 74 20 6f 66 20 74 61 62 6c 65 73  * List of tables
1bba4 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
1bba5 70 45 4c 69 73 74 20 20 20 20 2f 2a 20 45 78 70  pEList    /* Exp
1bba6 72 65 73 73 69 6f 6e 73 20 64 65 66 69 6e 69 6e  ressions definin
1bba7 67 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  g the result set
1bba8 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76   */.){.  Vdbe *v
1bba9 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
1bbaa 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4e 61 6d  ;.  int i;.  Nam
1bbab 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a 20 20  eContext sNC;.  
1bbac 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70  sNC.pSrcList = p
1bbad 54 61 62 4c 69 73 74 3b 0a 20 20 73 4e 43 2e 70  TabList;.  sNC.p
1bbae 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a  Parse = pParse;.
1bbaf 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c    for(i=0; i<pEL
1bbb0 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29  ist->nExpr; i++)
1bbb1 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 20 3d 20  {.    Expr *p = 
1bbb2 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78  pEList->a[i].pEx
1bbb3 70 72 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  pr;.    const ch
1bbb4 61 72 20 2a 7a 4f 72 69 67 44 62 20 3d 20 30 3b  ar *zOrigDb = 0;
1bbb5 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
1bbb6 2a 7a 4f 72 69 67 54 61 62 20 3d 20 30 3b 0a 20  *zOrigTab = 0;. 
1bbb7 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
1bbb8 4f 72 69 67 43 6f 6c 20 3d 20 30 3b 0a 20 20 20  OrigCol = 0;.   
1bbb9 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79   const char *zTy
1bbba 70 65 20 3d 20 63 6f 6c 75 6d 6e 54 79 70 65 28  pe = columnType(
1bbbb 26 73 4e 43 2c 20 70 2c 20 26 7a 4f 72 69 67 44  &sNC, p, &zOrigD
1bbbc 62 2c 20 26 7a 4f 72 69 67 54 61 62 2c 20 26 7a  b, &zOrigTab, &z
1bbbd 4f 72 69 67 43 6f 6c 29 3b 0a 0a 20 20 20 20 2f  OrigCol);..    /
1bbbe 2a 20 54 68 65 20 76 64 62 65 20 6d 75 73 74 20  * The vdbe must 
1bbbf 6d 61 6b 65 20 69 74 27 73 20 6f 77 6e 20 63 6f  make it's own co
1bbc0 70 79 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  py of the column
1bbc1 2d 74 79 70 65 20 61 6e 64 20 6f 74 68 65 72 20  -type and other 
1bbc2 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 73  .    ** column s
1bbc3 70 65 63 69 66 69 63 20 73 74 72 69 6e 67 73 2c  pecific strings,
1bbc4 20 69 6e 20 63 61 73 65 20 74 68 65 20 73 63 68   in case the sch
1bbc5 65 6d 61 20 69 73 20 72 65 73 65 74 20 62 65 66  ema is reset bef
1bbc6 6f 72 65 20 74 68 69 73 0a 20 20 20 20 2a 2a 20  ore this.    ** 
1bbc7 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20  virtual machine 
1bbc8 69 73 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 20  is deleted..    
1bbc9 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  */.    sqlite3Vd
1bbca 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
1bbcb 69 2c 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c 54  i, COLNAME_DECLT
1bbcc 59 50 45 2c 20 7a 54 79 70 65 2c 20 50 33 5f 54  YPE, zType, P3_T
1bbcd 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 73  RANSIENT);.    s
1bbce 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
1bbcf 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41  Name(v, i, COLNA
1bbd0 4d 45 5f 44 41 54 41 42 41 53 45 2c 20 7a 4f 72  ME_DATABASE, zOr
1bbd1 69 67 44 62 2c 20 50 33 5f 54 52 41 4e 53 49 45  igDb, P3_TRANSIE
1bbd2 4e 54 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  NT);.    sqlite3
1bbd3 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
1bbd4 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 54 41 42  , i, COLNAME_TAB
1bbd5 4c 45 2c 20 7a 4f 72 69 67 54 61 62 2c 20 50 33  LE, zOrigTab, P3
1bbd6 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20  _TRANSIENT);.   
1bbd7 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
1bbd8 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c  olName(v, i, COL
1bbd9 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 2c 20 7a 4f 72  NAME_COLUMN, zOr
1bbda 69 67 43 6f 6c 2c 20 50 33 5f 54 52 41 4e 53 49  igCol, P3_TRANSI
1bbdb 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ENT);.  }.}../*.
1bbdc 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
1bbdd 20 74 68 61 74 20 77 69 6c 6c 20 74 65 6c 6c 20   that will tell 
1bbde 74 68 65 20 56 44 42 45 20 74 68 65 20 6e 61 6d  the VDBE the nam
1bbdf 65 73 20 6f 66 20 63 6f 6c 75 6d 6e 73 0a 2a 2a  es of columns.**
1bbe0 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
1bbe1 65 74 2e 20 20 54 68 69 73 20 69 6e 66 6f 72 6d  et.  This inform
1bbe2 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f  ation is used to
1bbe3 20 70 72 6f 76 69 64 65 20 74 68 65 0a 2a 2a 20   provide the.** 
1bbe4 61 7a 43 6f 6c 5b 5d 20 76 61 6c 75 65 73 20 69  azCol[] values i
1bbe5 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a  n the callback..
1bbe6 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 67  */.static void g
1bbe7 65 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 4e 61 6d  enerateColumnNam
1bbe8 65 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  es(.  Parse *pPa
1bbe9 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 50 61 72  rse,      /* Par
1bbea 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ser context */. 
1bbeb 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69   SrcList *pTabLi
1bbec 73 74 2c 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20  st,  /* List of 
1bbed 74 61 62 6c 65 73 20 2a 2f 0a 20 20 45 78 70 72  tables */.  Expr
1bbee 4c 69 73 74 20 2a 70 45 4c 69 73 74 20 20 20 20  List *pEList    
1bbef 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 73 20 64  /* Expressions d
1bbf0 65 66 69 6e 69 6e 67 20 74 68 65 20 72 65 73 75  efining the resu
1bbf1 6c 74 20 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 56  lt set */.){.  V
1bbf2 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d  dbe *v = pParse-
1bbf3 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 2c  >pVdbe;.  int i,
1bbf4 20 6a 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   j;.  sqlite3 *d
1bbf5 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
1bbf6 20 20 69 6e 74 20 66 75 6c 6c 4e 61 6d 65 73 2c    int fullNames,
1bbf7 20 73 68 6f 72 74 4e 61 6d 65 73 3b 0a 0a 23 69   shortNames;..#i
1bbf8 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1bbf9 54 5f 45 58 50 4c 41 49 4e 0a 20 20 2f 2a 20 49  T_EXPLAIN.  /* I
1bbfa 66 20 74 68 69 73 20 69 73 20 61 6e 20 45 58 50  f this is an EXP
1bbfb 4c 41 49 4e 2c 20 73 6b 69 70 20 74 68 69 73 20  LAIN, skip this 
1bbfc 73 74 65 70 20 2a 2f 0a 20 20 69 66 28 20 70 50  step */.  if( pP
1bbfd 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e 20 29 7b  arse->explain ){
1bbfe 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
1bbff 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72  .#endif..  asser
1bc00 74 28 20 76 21 3d 30 20 29 3b 0a 20 20 69 66 28  t( v!=0 );.  if(
1bc01 20 70 50 61 72 73 65 2d 3e 63 6f 6c 4e 61 6d 65   pParse->colName
1bc02 73 53 65 74 20 7c 7c 20 76 3d 3d 30 20 7c 7c 20  sSet || v==0 || 
1bc03 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69  sqlite3MallocFai
1bc04 6c 65 64 28 29 20 29 20 72 65 74 75 72 6e 3b 0a  led() ) return;.
1bc05 20 20 70 50 61 72 73 65 2d 3e 63 6f 6c 4e 61 6d    pParse->colNam
1bc06 65 73 53 65 74 20 3d 20 31 3b 0a 20 20 66 75 6c  esSet = 1;.  ful
1bc07 6c 4e 61 6d 65 73 20 3d 20 28 64 62 2d 3e 66 6c  lNames = (db->fl
1bc08 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 75 6c  ags & SQLITE_Ful
1bc09 6c 43 6f 6c 4e 61 6d 65 73 29 21 3d 30 3b 0a 20  lColNames)!=0;. 
1bc0a 20 73 68 6f 72 74 4e 61 6d 65 73 20 3d 20 28 64   shortNames = (d
1bc0b 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54  b->flags & SQLIT
1bc0c 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65 73 29  E_ShortColNames)
1bc0d 21 3d 30 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  !=0;.  sqlite3Vd
1bc0e 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20  beSetNumCols(v, 
1bc0f 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 3b 0a  pEList->nExpr);.
1bc10 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c    for(i=0; i<pEL
1bc11 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29  ist->nExpr; i++)
1bc12 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 3b 0a 20  {.    Expr *p;. 
1bc13 20 20 20 70 20 3d 20 70 45 4c 69 73 74 2d 3e 61     p = pEList->a
1bc14 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 69  [i].pExpr;.    i
1bc15 66 28 20 70 3d 3d 30 20 29 20 63 6f 6e 74 69 6e  f( p==0 ) contin
1bc16 75 65 3b 0a 20 20 20 20 69 66 28 20 70 45 4c 69  ue;.    if( pELi
1bc17 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 20 29  st->a[i].zName )
1bc18 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e  {.      char *zN
1bc19 61 6d 65 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b  ame = pEList->a[
1bc1a 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20  i].zName;.      
1bc1b 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
1bc1c 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e  lName(v, i, COLN
1bc1d 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4e 61 6d 65 2c  AME_NAME, zName,
1bc1e 20 73 74 72 6c 65 6e 28 7a 4e 61 6d 65 29 29 3b   strlen(zName));
1bc1f 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b  .      continue;
1bc20 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
1bc21 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20  ->op==TK_COLUMN 
1bc22 26 26 20 70 54 61 62 4c 69 73 74 20 29 7b 0a 20  && pTabList ){. 
1bc23 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62       Table *pTab
1bc24 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 43  ;.      char *zC
1bc25 6f 6c 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 43  ol;.      int iC
1bc26 6f 6c 20 3d 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3b  ol = p->iColumn;
1bc27 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20  .      for(j=0; 
1bc28 6a 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  j<pTabList->nSrc
1bc29 20 26 26 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b   && pTabList->a[
1bc2a 6a 5d 2e 69 43 75 72 73 6f 72 21 3d 70 2d 3e 69  j].iCursor!=p->i
1bc2b 54 61 62 6c 65 3b 20 6a 2b 2b 29 7b 7d 0a 20 20  Table; j++){}.  
1bc2c 20 20 20 20 61 73 73 65 72 74 28 20 6a 3c 70 54      assert( j<pT
1bc2d 61 62 4c 69 73 74 2d 3e 6e 53 72 63 20 29 3b 0a  abList->nSrc );.
1bc2e 20 20 20 20 20 20 70 54 61 62 20 3d 20 70 54 61        pTab = pTa
1bc2f 62 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 54 61 62  bList->a[j].pTab
1bc30 3b 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c  ;.      if( iCol
1bc31 3c 30 20 29 20 69 43 6f 6c 20 3d 20 70 54 61 62  <0 ) iCol = pTab
1bc32 2d 3e 69 50 4b 65 79 3b 0a 20 20 20 20 20 20 61  ->iPKey;.      a
1bc33 73 73 65 72 74 28 20 69 43 6f 6c 3d 3d 2d 31 20  ssert( iCol==-1 
1bc34 7c 7c 20 28 69 43 6f 6c 3e 3d 30 20 26 26 20 69  || (iCol>=0 && i
1bc35 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 29 20  Col<pTab->nCol) 
1bc36 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f  );.      if( iCo
1bc37 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 7a  l<0 ){.        z
1bc38 43 6f 6c 20 3d 20 22 72 6f 77 69 64 22 3b 0a 20  Col = "rowid";. 
1bc39 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1bc3a 20 20 20 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d      zCol = pTab-
1bc3b 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d  >aCol[iCol].zNam
1bc3c 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  e;.      }.     
1bc3d 20 69 66 28 20 21 73 68 6f 72 74 4e 61 6d 65 73   if( !shortNames
1bc3e 20 26 26 20 21 66 75 6c 6c 4e 61 6d 65 73 20 26   && !fullNames &
1bc3f 26 20 70 2d 3e 73 70 61 6e 2e 7a 20 26 26 20 70  & p->span.z && p
1bc40 2d 3e 73 70 61 6e 2e 7a 5b 30 5d 20 29 7b 0a 20  ->span.z[0] ){. 
1bc41 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1bc42 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
1bc43 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  i, COLNAME_NAME,
1bc44 20 28 63 68 61 72 2a 29 70 2d 3e 73 70 61 6e 2e   (char*)p->span.
1bc45 7a 2c 20 70 2d 3e 73 70 61 6e 2e 6e 29 3b 0a 20  z, p->span.n);. 
1bc46 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66       }else if( f
1bc47 75 6c 6c 4e 61 6d 65 73 20 7c 7c 20 28 21 73 68  ullNames || (!sh
1bc48 6f 72 74 4e 61 6d 65 73 20 26 26 20 70 54 61 62  ortNames && pTab
1bc49 4c 69 73 74 2d 3e 6e 53 72 63 3e 31 29 20 29 7b  List->nSrc>1) ){
1bc4a 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a  .        char *z
1bc4b 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 20 20 20 20  Name = 0;.      
1bc4c 20 20 63 68 61 72 20 2a 7a 54 61 62 3b 0a 20 0a    char *zTab;. .
1bc4d 20 20 20 20 20 20 20 20 7a 54 61 62 20 3d 20 70          zTab = p
1bc4e 54 61 62 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 7a 41  TabList->a[j].zA
1bc4f 6c 69 61 73 3b 0a 20 20 20 20 20 20 20 20 69 66  lias;.        if
1bc50 28 20 66 75 6c 6c 4e 61 6d 65 73 20 7c 7c 20 7a  ( fullNames || z
1bc51 54 61 62 3d 3d 30 20 29 20 7a 54 61 62 20 3d 20  Tab==0 ) zTab = 
1bc52 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20  pTab->zName;.   
1bc53 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53       sqlite3SetS
1bc54 74 72 69 6e 67 28 26 7a 4e 61 6d 65 2c 20 7a 54  tring(&zName, zT
1bc55 61 62 2c 20 22 2e 22 2c 20 7a 43 6f 6c 2c 20 28  ab, ".", zCol, (
1bc56 63 68 61 72 2a 29 30 29 3b 0a 20 20 20 20 20 20  char*)0);.      
1bc57 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
1bc58 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f  ColName(v, i, CO
1bc59 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4e 61 6d  LNAME_NAME, zNam
1bc5a 65 2c 20 50 33 5f 44 59 4e 41 4d 49 43 29 3b 0a  e, P3_DYNAMIC);.
1bc5b 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1bc5c 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1bc5d 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c  SetColName(v, i,
1bc5e 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a   COLNAME_NAME, z
1bc5f 43 6f 6c 2c 20 73 74 72 6c 65 6e 28 7a 43 6f 6c  Col, strlen(zCol
1bc60 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ));.      }.    
1bc61 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 73 70 61  }else if( p->spa
1bc62 6e 2e 7a 20 26 26 20 70 2d 3e 73 70 61 6e 2e 7a  n.z && p->span.z
1bc63 5b 30 5d 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  [0] ){.      sql
1bc64 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
1bc65 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45  me(v, i, COLNAME
1bc66 5f 4e 41 4d 45 2c 20 28 63 68 61 72 2a 29 70 2d  _NAME, (char*)p-
1bc67 3e 73 70 61 6e 2e 7a 2c 20 70 2d 3e 73 70 61 6e  >span.z, p->span
1bc68 2e 6e 29 3b 0a 20 20 20 20 20 20 2f 2a 20 73 71  .n);.      /* sq
1bc69 6c 69 74 65 33 56 64 62 65 43 6f 6d 70 72 65 73  lite3VdbeCompres
1bc6a 73 53 70 61 63 65 28 76 2c 20 61 64 64 72 29 3b  sSpace(v, addr);
1bc6b 20 2a 2f 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   */.    }else{. 
1bc6c 20 20 20 20 20 63 68 61 72 20 7a 4e 61 6d 65 5b       char zName[
1bc6d 33 30 5d 3b 0a 20 20 20 20 20 20 61 73 73 65 72  30];.      asser
1bc6e 74 28 20 70 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c  t( p->op!=TK_COL
1bc6f 55 4d 4e 20 7c 7c 20 70 54 61 62 4c 69 73 74 3d  UMN || pTabList=
1bc70 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  =0 );.      sqli
1bc71 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
1bc72 65 6f 66 28 7a 4e 61 6d 65 29 2c 20 7a 4e 61 6d  eof(zName), zNam
1bc73 65 2c 20 22 63 6f 6c 75 6d 6e 25 64 22 2c 20 69  e, "column%d", i
1bc74 2b 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  +1);.      sqlit
1bc75 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
1bc76 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, i, COLNAME_N
1bc77 41 4d 45 2c 20 7a 4e 61 6d 65 2c 20 30 29 3b 0a  AME, zName, 0);.
1bc78 20 20 20 20 7d 0a 20 20 7d 0a 20 20 67 65 6e 65      }.  }.  gene
1bc79 72 61 74 65 43 6f 6c 75 6d 6e 54 79 70 65 73 28  rateColumnTypes(
1bc7a 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74  pParse, pTabList
1bc7b 2c 20 70 45 4c 69 73 74 29 3b 0a 7d 0a 0a 23 69  , pEList);.}..#i
1bc7c 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1bc7d 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43  T_COMPOUND_SELEC
1bc7e 54 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 20 6f 66 20  T./*.** Name of 
1bc7f 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f  the connection o
1bc80 70 65 72 61 74 6f 72 2c 20 75 73 65 64 20 66 6f  perator, used fo
1bc81 72 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73  r error messages
1bc82 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  ..*/.static cons
1bc83 74 20 63 68 61 72 20 2a 73 65 6c 65 63 74 4f 70  t char *selectOp
1bc84 4e 61 6d 65 28 69 6e 74 20 69 64 29 7b 0a 20 20  Name(int id){.  
1bc85 63 68 61 72 20 2a 7a 3b 0a 20 20 73 77 69 74 63  char *z;.  switc
1bc86 68 28 20 69 64 20 29 7b 0a 20 20 20 20 63 61 73  h( id ){.    cas
1bc87 65 20 54 4b 5f 41 4c 4c 3a 20 20 20 20 20 20 20  e TK_ALL:       
1bc88 7a 20 3d 20 22 55 4e 49 4f 4e 20 41 4c 4c 22 3b  z = "UNION ALL";
1bc89 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
1bc8a 73 65 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 3a  se TK_INTERSECT:
1bc8b 20 7a 20 3d 20 22 49 4e 54 45 52 53 45 43 54 22   z = "INTERSECT"
1bc8c 3b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63  ;   break;.    c
1bc8d 61 73 65 20 54 4b 5f 45 58 43 45 50 54 3a 20 20  ase TK_EXCEPT:  
1bc8e 20 20 7a 20 3d 20 22 45 58 43 45 50 54 22 3b 20    z = "EXCEPT"; 
1bc8f 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1bc90 64 65 66 61 75 6c 74 3a 20 20 20 20 20 20 20 20  default:        
1bc91 20 20 20 7a 20 3d 20 22 55 4e 49 4f 4e 22 3b 20     z = "UNION"; 
1bc92 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d        break;.  }
1bc93 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 23  .  return z;.}.#
1bc94 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
1bc95 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45  OMIT_COMPOUND_SE
1bc96 4c 45 43 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46  LECT */../*.** F
1bc97 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69  orward declarati
1bc98 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  on.*/.static int
1bc99 20 70 72 65 70 53 65 6c 65 63 74 53 74 6d 74 28   prepSelectStmt(
1bc9a 50 61 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a 29  Parse*, Select*)
1bc9b 3b 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61  ;../*.** Given a
1bc9c 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
1bc9d 74 2c 20 67 65 6e 65 72 61 74 65 20 61 20 54 61  t, generate a Ta
1bc9e 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 74 68  ble structure th
1bc9f 61 74 20 64 65 73 63 72 69 62 65 73 0a 2a 2a 20  at describes.** 
1bca0 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f  the result set o
1bca1 66 20 74 68 61 74 20 53 45 4c 45 43 54 2e 0a 2a  f that SELECT..*
1bca2 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1bca3 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 52   Table *sqlite3R
1bca4 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 63 74  esultSetOfSelect
1bca5 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
1bca6 63 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65 2c 20  char *zTabName, 
1bca7 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 29  Select *pSelect)
1bca8 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b  {.  Table *pTab;
1bca9 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 45  .  int i, j;.  E
1bcaa 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b  xprList *pEList;
1bcab 0a 20 20 43 6f 6c 75 6d 6e 20 2a 61 43 6f 6c 2c  .  Column *aCol,
1bcac 20 2a 70 43 6f 6c 3b 0a 0a 20 20 77 68 69 6c 65   *pCol;..  while
1bcad 28 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f  ( pSelect->pPrio
1bcae 72 20 29 20 70 53 65 6c 65 63 74 20 3d 20 70 53  r ) pSelect = pS
1bcaf 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 3b 0a 20  elect->pPrior;. 
1bcb0 20 69 66 28 20 70 72 65 70 53 65 6c 65 63 74 53   if( prepSelectS
1bcb1 74 6d 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c  tmt(pParse, pSel
1bcb2 65 63 74 29 20 29 7b 0a 20 20 20 20 72 65 74 75  ect) ){.    retu
1bcb3 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20  rn 0;.  }.  if( 
1bcb4 73 71 6c 69 74 65 33 53 65 6c 65 63 74 52 65 73  sqlite3SelectRes
1bcb5 6f 6c 76 65 28 70 50 61 72 73 65 2c 20 70 53 65  olve(pParse, pSe
1bcb6 6c 65 63 74 2c 20 30 29 20 29 7b 0a 20 20 20 20  lect, 0) ){.    
1bcb7 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
1bcb8 70 54 61 62 20 3d 20 73 71 6c 69 74 65 4d 61 6c  pTab = sqliteMal
1bcb9 6c 6f 63 28 20 73 69 7a 65 6f 66 28 54 61 62 6c  loc( sizeof(Tabl
1bcba 65 29 20 29 3b 0a 20 20 69 66 28 20 70 54 61 62  e) );.  if( pTab
1bcbb 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
1bcbc 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 54 61 62 2d  n 0;.  }.  pTab-
1bcbd 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 70 54 61  >nRef = 1;.  pTa
1bcbe 62 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 54 61 62 4e  b->zName = zTabN
1bcbf 61 6d 65 20 3f 20 73 71 6c 69 74 65 53 74 72 44  ame ? sqliteStrD
1bcc0 75 70 28 7a 54 61 62 4e 61 6d 65 29 20 3a 20 30  up(zTabName) : 0
1bcc1 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 53 65  ;.  pEList = pSe
1bcc2 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a 20 20  lect->pEList;.  
1bcc3 70 54 61 62 2d 3e 6e 43 6f 6c 20 3d 20 70 45 4c  pTab->nCol = pEL
1bcc4 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 61 73  ist->nExpr;.  as
1bcc5 73 65 72 74 28 20 70 54 61 62 2d 3e 6e 43 6f 6c  sert( pTab->nCol
1bcc6 3e 30 20 29 3b 0a 20 20 70 54 61 62 2d 3e 61 43  >0 );.  pTab->aC
1bcc7 6f 6c 20 3d 20 61 43 6f 6c 20 3d 20 73 71 6c 69  ol = aCol = sqli
1bcc8 74 65 4d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66  teMalloc( sizeof
1bcc9 28 70 54 61 62 2d 3e 61 43 6f 6c 5b 30 5d 29 2a  (pTab->aCol[0])*
1bcca 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20  pTab->nCol );.  
1bccb 66 6f 72 28 69 3d 30 2c 20 70 43 6f 6c 3d 61 43  for(i=0, pCol=aC
1bccc 6f 6c 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c  ol; i<pTab->nCol
1bccd 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a  ; i++, pCol++){.
1bcce 20 20 20 20 45 78 70 72 20 2a 70 2c 20 2a 70 52      Expr *p, *pR
1bccf 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 79 70  ;.    char *zTyp
1bcd0 65 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e 61  e;.    char *zNa
1bcd1 6d 65 3b 0a 20 20 20 20 69 6e 74 20 6e 4e 61 6d  me;.    int nNam
1bcd2 65 3b 0a 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a  e;.    CollSeq *
1bcd3 70 43 6f 6c 6c 3b 0a 20 20 20 20 69 6e 74 20 63  pColl;.    int c
1bcd4 6e 74 3b 0a 20 20 20 20 4e 61 6d 65 43 6f 6e 74  nt;.    NameCont
1bcd5 65 78 74 20 73 4e 43 3b 0a 20 20 20 20 0a 20 20  ext sNC;.    .  
1bcd6 20 20 2f 2a 20 47 65 74 20 61 6e 20 61 70 70 72    /* Get an appr
1bcd7 6f 70 72 69 61 74 65 20 6e 61 6d 65 20 66 6f 72  opriate name for
1bcd8 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 20 20 20 20   the column.    
1bcd9 2a 2f 0a 20 20 20 20 70 20 3d 20 70 45 4c 69 73  */.    p = pELis
1bcda 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20  t->a[i].pExpr;. 
1bcdb 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 52     assert( p->pR
1bcdc 69 67 68 74 3d 3d 30 20 7c 7c 20 70 2d 3e 70 52  ight==0 || p->pR
1bcdd 69 67 68 74 2d 3e 74 6f 6b 65 6e 2e 7a 3d 3d 30  ight->token.z==0
1bcde 20 7c 7c 20 70 2d 3e 70 52 69 67 68 74 2d 3e 74   || p->pRight->t
1bcdf 6f 6b 65 6e 2e 7a 5b 30 5d 21 3d 30 20 29 3b 0a  oken.z[0]!=0 );.
1bce0 20 20 20 20 69 66 28 20 28 7a 4e 61 6d 65 20 3d      if( (zName =
1bce1 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e   pEList->a[i].zN
1bce2 61 6d 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  ame)!=0 ){.     
1bce3 20 2f 2a 20 49 66 20 74 68 65 20 63 6f 6c 75 6d   /* If the colum
1bce4 6e 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 22 41  n contains an "A
1bce5 53 20 3c 6e 61 6d 65 3e 22 20 70 68 72 61 73 65  S <name>" phrase
1bce6 2c 20 75 73 65 20 3c 6e 61 6d 65 3e 20 61 73 20  , use <name> as 
1bce7 74 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20  the name */.    
1bce8 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65    zName = sqlite
1bce9 53 74 72 44 75 70 28 7a 4e 61 6d 65 29 3b 0a 20  StrDup(zName);. 
1bcea 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e     }else if( p->
1bceb 6f 70 3d 3d 54 4b 5f 44 4f 54 20 0a 20 20 20 20  op==TK_DOT .    
1bcec 20 20 20 20 20 20 20 20 20 20 26 26 20 28 70 52            && (pR
1bced 3d 70 2d 3e 70 52 69 67 68 74 29 21 3d 30 20 26  =p->pRight)!=0 &
1bcee 26 20 70 52 2d 3e 74 6f 6b 65 6e 2e 7a 20 26 26  & pR->token.z &&
1bcef 20 70 52 2d 3e 74 6f 6b 65 6e 2e 7a 5b 30 5d 20   pR->token.z[0] 
1bcf0 29 7b 0a 20 20 20 20 20 20 2f 2a 20 46 6f 72 20  ){.      /* For 
1bcf1 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 66  columns of the f
1bcf2 72 6f 6d 20 41 2e 42 20 75 73 65 20 42 20 61 73  rom A.B use B as
1bcf3 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20   the name */.   
1bcf4 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74     zName = sqlit
1bcf5 65 33 4d 50 72 69 6e 74 66 28 22 25 54 22 2c 20  e3MPrintf("%T", 
1bcf6 26 70 52 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 20 20  &pR->token);.   
1bcf7 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 73 70   }else if( p->sp
1bcf8 61 6e 2e 7a 20 26 26 20 70 2d 3e 73 70 61 6e 2e  an.z && p->span.
1bcf9 7a 5b 30 5d 20 29 7b 0a 20 20 20 20 20 20 2f 2a  z[0] ){.      /*
1bcfa 20 55 73 65 20 74 68 65 20 6f 72 69 67 69 6e 61   Use the origina
1bcfb 6c 20 74 65 78 74 20 6f 66 20 74 68 65 20 63 6f  l text of the co
1bcfc 6c 75 6d 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  lumn expression 
1bcfd 61 73 20 69 74 73 20 6e 61 6d 65 20 2a 2f 0a 20  as its name */. 
1bcfe 20 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c       zName = sql
1bcff 69 74 65 33 4d 50 72 69 6e 74 66 28 22 25 54 22  ite3MPrintf("%T"
1bd00 2c 20 26 70 2d 3e 73 70 61 6e 29 3b 0a 20 20 20  , &p->span);.   
1bd01 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a   }else{.      /*
1bd02 20 49 66 20 61 6c 6c 20 65 6c 73 65 20 66 61 69   If all else fai
1bd03 6c 73 2c 20 6d 61 6b 65 20 75 70 20 61 20 6e 61  ls, make up a na
1bd04 6d 65 20 2a 2f 0a 20 20 20 20 20 20 7a 4e 61 6d  me */.      zNam
1bd05 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  e = sqlite3MPrin
1bd06 74 66 28 22 63 6f 6c 75 6d 6e 25 64 22 2c 20 69  tf("column%d", i
1bd07 2b 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  +1);.    }.    s
1bd08 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 7a 4e  qlite3Dequote(zN
1bd09 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 73 71  ame);.    if( sq
1bd0a 6c 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69 6c 65  lite3MallocFaile
1bd0b 64 28 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  d() ){.      sql
1bd0c 69 74 65 46 72 65 65 28 7a 4e 61 6d 65 29 3b 0a  iteFree(zName);.
1bd0d 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c        sqlite3Del
1bd0e 65 74 65 54 61 62 6c 65 28 70 54 61 62 29 3b 0a  eteTable(pTab);.
1bd0f 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
1bd10 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4d 61      }..    /* Ma
1bd11 6b 65 20 73 75 72 65 20 74 68 65 20 63 6f 6c 75  ke sure the colu
1bd12 6d 6e 20 6e 61 6d 65 20 69 73 20 75 6e 69 71 75  mn name is uniqu
1bd13 65 2e 20 20 49 66 20 74 68 65 20 6e 61 6d 65 20  e.  If the name 
1bd14 69 73 20 6e 6f 74 20 75 6e 69 71 75 65 2c 0a 20  is not unique,. 
1bd15 20 20 20 2a 2a 20 61 70 70 65 6e 64 20 61 20 69     ** append a i
1bd16 6e 74 65 67 65 72 20 74 6f 20 74 68 65 20 6e 61  nteger to the na
1bd17 6d 65 20 73 6f 20 74 68 61 74 20 69 74 20 62 65  me so that it be
1bd18 63 6f 6d 65 73 20 75 6e 69 71 75 65 2e 0a 20 20  comes unique..  
1bd19 20 20 2a 2f 0a 20 20 20 20 6e 4e 61 6d 65 20 3d    */.    nName =
1bd1a 20 73 74 72 6c 65 6e 28 7a 4e 61 6d 65 29 3b 0a   strlen(zName);.
1bd1b 20 20 20 20 66 6f 72 28 6a 3d 63 6e 74 3d 30 3b      for(j=cnt=0;
1bd1c 20 6a 3c 69 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20   j<i; j++){.    
1bd1d 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
1bd1e 49 43 6d 70 28 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61  ICmp(aCol[j].zNa
1bd1f 6d 65 2c 20 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b  me, zName)==0 ){
1bd20 0a 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 5b 6e  .        zName[n
1bd21 4e 61 6d 65 5d 20 3d 20 30 3b 0a 20 20 20 20 20  Name] = 0;.     
1bd22 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74     zName = sqlit
1bd23 65 33 4d 50 72 69 6e 74 66 28 22 25 7a 3a 25 64  e3MPrintf("%z:%d
1bd24 22 2c 20 7a 4e 61 6d 65 2c 20 2b 2b 63 6e 74 29  ", zName, ++cnt)
1bd25 3b 0a 20 20 20 20 20 20 20 20 6a 20 3d 20 2d 31  ;.        j = -1
1bd26 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 4e  ;.        if( zN
1bd27 61 6d 65 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a  ame==0 ) break;.
1bd28 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1bd29 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d 20    pCol->zName = 
1bd2a 7a 4e 61 6d 65 3b 0a 0a 20 20 20 20 2f 2a 20 47  zName;..    /* G
1bd2b 65 74 20 74 68 65 20 74 79 70 65 6e 61 6d 65 2c  et the typename,
1bd2c 20 74 79 70 65 20 61 66 66 69 6e 69 74 79 2c 20   type affinity, 
1bd2d 61 6e 64 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  and collating se
1bd2e 71 75 65 6e 63 65 20 66 6f 72 20 74 68 65 0a 20  quence for the. 
1bd2f 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 2e 0a 20 20     ** column..  
1bd30 20 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28    */.    memset(
1bd31 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28  &sNC, 0, sizeof(
1bd32 73 4e 43 29 29 3b 0a 20 20 20 20 73 4e 43 2e 70  sNC));.    sNC.p
1bd33 53 72 63 4c 69 73 74 20 3d 20 70 53 65 6c 65 63  SrcList = pSelec
1bd34 74 2d 3e 70 53 72 63 3b 0a 20 20 20 20 7a 54 79  t->pSrc;.    zTy
1bd35 70 65 20 3d 20 73 71 6c 69 74 65 53 74 72 44 75  pe = sqliteStrDu
1bd36 70 28 63 6f 6c 75 6d 6e 54 79 70 65 28 26 73 4e  p(columnType(&sN
1bd37 43 2c 20 70 2c 20 30 2c 20 30 2c 20 30 29 29 3b  C, p, 0, 0, 0));
1bd38 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 54 79 70 65  .    pCol->zType
1bd39 20 3d 20 7a 54 79 70 65 3b 0a 20 20 20 20 70 43   = zType;.    pC
1bd3a 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 73  ol->affinity = s
1bd3b 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69  qlite3ExprAffini
1bd3c 74 79 28 70 29 3b 0a 20 20 20 20 70 43 6f 6c 6c  ty(p);.    pColl
1bd3d 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
1bd3e 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 29  llSeq(pParse, p)
1bd3f 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20  ;.    if( pColl 
1bd40 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c 2d 3e 7a  ){.      pCol->z
1bd41 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 53 74 72  Coll = sqliteStr
1bd42 44 75 70 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65  Dup(pColl->zName
1bd43 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70  );.    }.  }.  p
1bd44 54 61 62 2d 3e 69 50 4b 65 79 20 3d 20 2d 31 3b  Tab->iPKey = -1;
1bd45 0a 20 20 72 65 74 75 72 6e 20 70 54 61 62 3b 0a  .  return pTab;.
1bd46 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61 72 65  }../*.** Prepare
1bd47 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
1bd48 65 6e 74 20 66 6f 72 20 70 72 6f 63 65 73 73 69  ent for processi
1bd49 6e 67 20 62 79 20 64 6f 69 6e 67 20 74 68 65 20  ng by doing the 
1bd4a 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 74 68 69  following.** thi
1bd4b 6e 67 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 28 31  ngs:.**.**    (1
1bd4c 29 20 20 4d 61 6b 65 20 73 75 72 65 20 56 44 42  )  Make sure VDB
1bd4d 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73  E cursor numbers
1bd4e 20 68 61 76 65 20 62 65 65 6e 20 61 73 73 69 67   have been assig
1bd4f 6e 65 64 20 74 6f 20 65 76 65 72 79 0a 2a 2a 20  ned to every.** 
1bd50 20 20 20 20 20 20 20 20 65 6c 65 6d 65 6e 74 20          element 
1bd51 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  of the FROM clau
1bd52 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29  se..**.**    (2)
1bd53 20 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 70 54    Fill in the pT
1bd54 61 62 4c 69 73 74 2d 3e 61 5b 5d 2e 70 54 61 62  abList->a[].pTab
1bd55 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 53   fields in the S
1bd56 72 63 4c 69 73 74 20 74 68 61 74 20 0a 2a 2a 20  rcList that .** 
1bd57 20 20 20 20 20 20 20 20 64 65 66 69 6e 65 73 20          defines 
1bd58 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 57 68  FROM clause.  Wh
1bd59 65 6e 20 76 69 65 77 73 20 61 70 70 65 61 72 20  en views appear 
1bd5a 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  in the FROM clau
1bd5b 73 65 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 66  se,.**         f
1bd5c 69 6c 6c 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b  ill pTabList->a[
1bd5d 5d 2e 70 53 65 6c 65 63 74 20 77 69 74 68 20 61  ].pSelect with a
1bd5e 20 63 6f 70 79 20 6f 66 20 74 68 65 20 53 45 4c   copy of the SEL
1bd5f 45 43 54 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a  ECT statement.**
1bd60 20 20 20 20 20 20 20 20 20 74 68 61 74 20 69 6d           that im
1bd61 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 76 69 65  plements the vie
1bd62 77 2e 20 20 41 20 63 6f 70 79 20 69 73 20 6d 61  w.  A copy is ma
1bd63 64 65 20 6f 66 20 74 68 65 20 76 69 65 77 27 73  de of the view's
1bd64 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20   SELECT.**      
1bd65 20 20 20 73 74 61 74 65 6d 65 6e 74 20 73 6f 20     statement so 
1bd66 74 68 61 74 20 77 65 20 63 61 6e 20 66 72 65 65  that we can free
1bd67 6c 79 20 6d 6f 64 69 66 79 20 6f 72 20 64 65 6c  ly modify or del
1bd68 65 74 65 20 74 68 61 74 20 73 74 61 74 65 6d 65  ete that stateme
1bd69 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 77 69  nt.**         wi
1bd6a 74 68 6f 75 74 20 77 6f 72 72 79 69 6e 67 20 61  thout worrying a
1bd6b 62 6f 75 74 20 6d 65 73 73 69 6e 67 20 75 70 20  bout messing up 
1bd6c 74 68 65 20 70 72 65 73 69 73 74 65 6e 74 20 72  the presistent r
1bd6d 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a  epresentation.**
1bd6e 20 20 20 20 20 20 20 20 20 6f 66 20 74 68 65 20           of the 
1bd6f 76 69 65 77 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28  view..**.**    (
1bd70 33 29 20 20 41 64 64 20 74 65 72 6d 73 20 74 6f  3)  Add terms to
1bd71 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
1bd72 65 20 74 6f 20 61 63 63 6f 6d 6f 64 61 74 65 20  e to accomodate 
1bd73 74 68 65 20 4e 41 54 55 52 41 4c 20 6b 65 79 77  the NATURAL keyw
1bd74 6f 72 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 6f  ord.**         o
1bd75 6e 20 6a 6f 69 6e 73 20 61 6e 64 20 74 68 65 20  n joins and the 
1bd76 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20 63 6c 61  ON and USING cla
1bd77 75 73 65 20 6f 66 20 6a 6f 69 6e 73 2e 0a 2a 2a  use of joins..**
1bd78 0a 2a 2a 20 20 20 20 28 34 29 20 20 53 63 61 6e  .**    (4)  Scan
1bd79 20 74 68 65 20 6c 69 73 74 20 6f 66 20 63 6f 6c   the list of col
1bd7a 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75  umns in the resu
1bd7b 6c 74 20 73 65 74 20 28 70 45 4c 69 73 74 29 20  lt set (pEList) 
1bd7c 6c 6f 6f 6b 69 6e 67 0a 2a 2a 20 20 20 20 20 20  looking.**      
1bd7d 20 20 20 66 6f 72 20 69 6e 73 74 61 6e 63 65 73     for instances
1bd7e 20 6f 66 20 74 68 65 20 22 2a 22 20 6f 70 65 72   of the "*" oper
1bd7f 61 74 6f 72 20 6f 72 20 74 68 65 20 54 41 42 4c  ator or the TABL
1bd80 45 2e 2a 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2a  E.* operator..**
1bd81 20 20 20 20 20 20 20 20 20 49 66 20 66 6f 75 6e           If foun
1bd82 64 2c 20 65 78 70 61 6e 64 20 65 61 63 68 20 22  d, expand each "
1bd83 2a 22 20 74 6f 20 62 65 20 65 76 65 72 79 20 63  *" to be every c
1bd84 6f 6c 75 6d 6e 20 69 6e 20 65 76 65 72 79 20 74  olumn in every t
1bd85 61 62 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  able.**         
1bd86 61 6e 64 20 54 41 42 4c 45 2e 2a 20 74 6f 20 62  and TABLE.* to b
1bd87 65 20 65 76 65 72 79 20 63 6f 6c 75 6d 6e 20 69  e every column i
1bd88 6e 20 54 41 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 52  n TABLE..**.** R
1bd89 65 74 75 72 6e 20 30 20 6f 6e 20 73 75 63 63 65  eturn 0 on succe
1bd8a 73 73 2e 20 20 49 66 20 74 68 65 72 65 20 61 72  ss.  If there ar
1bd8b 65 20 70 72 6f 62 6c 65 6d 73 2c 20 6c 65 61 76  e problems, leav
1bd8c 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  e an error messa
1bd8d 67 65 0a 2a 2a 20 69 6e 20 70 50 61 72 73 65 20  ge.** in pParse 
1bd8e 61 6e 64 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a  and return non-z
1bd8f 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ero..*/.static i
1bd90 6e 74 20 70 72 65 70 53 65 6c 65 63 74 53 74 6d  nt prepSelectStm
1bd91 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  t(Parse *pParse,
1bd92 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 69   Select *p){.  i
1bd93 6e 74 20 69 2c 20 6a 2c 20 6b 2c 20 72 63 3b 0a  nt i, j, k, rc;.
1bd94 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c    SrcList *pTabL
1bd95 69 73 74 3b 0a 20 20 45 78 70 72 4c 69 73 74 20  ist;.  ExprList 
1bd96 2a 70 45 4c 69 73 74 3b 0a 20 20 73 74 72 75 63  *pEList;.  struc
1bd97 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
1bd98 70 46 72 6f 6d 3b 0a 0a 20 20 69 66 28 20 70 3d  pFrom;..  if( p=
1bd99 3d 30 20 7c 7c 20 70 2d 3e 70 53 72 63 3d 3d 30  =0 || p->pSrc==0
1bd9a 20 7c 7c 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f   || sqlite3Mallo
1bd9b 63 46 61 69 6c 65 64 28 29 20 29 7b 0a 20 20 20  cFailed() ){.   
1bd9c 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20   return 1;.  }. 
1bd9d 20 70 54 61 62 4c 69 73 74 20 3d 20 70 2d 3e 70   pTabList = p->p
1bd9e 53 72 63 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20  Src;.  pEList = 
1bd9f 70 2d 3e 70 45 4c 69 73 74 3b 0a 0a 20 20 2f 2a  p->pEList;..  /*
1bda0 20 4d 61 6b 65 20 73 75 72 65 20 63 75 72 73 6f   Make sure curso
1bda1 72 20 6e 75 6d 62 65 72 73 20 68 61 76 65 20 62  r numbers have b
1bda2 65 65 6e 20 61 73 73 69 67 6e 65 64 20 74 6f 20  een assigned to 
1bda3 61 6c 6c 20 65 6e 74 72 69 65 73 20 69 6e 0a 20  all entries in. 
1bda4 20 2a 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   ** the FROM cla
1bda5 75 73 65 20 6f 66 20 74 68 65 20 53 45 4c 45 43  use of the SELEC
1bda6 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 2a  T statement..  *
1bda7 2f 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69  /.  sqlite3SrcLi
1bda8 73 74 41 73 73 69 67 6e 43 75 72 73 6f 72 73 28  stAssignCursors(
1bda9 70 50 61 72 73 65 2c 20 70 2d 3e 70 53 72 63 29  pParse, p->pSrc)
1bdaa 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 6b 20 75 70 20  ;..  /* Look up 
1bdab 65 76 65 72 79 20 74 61 62 6c 65 20 6e 61 6d 65  every table name
1bdac 64 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  d in the FROM cl
1bdad 61 75 73 65 20 6f 66 20 74 68 65 20 73 65 6c 65  ause of the sele
1bdae 63 74 2e 20 20 49 66 0a 20 20 2a 2a 20 61 6e 20  ct.  If.  ** an 
1bdaf 65 6e 74 72 79 20 6f 66 20 74 68 65 20 46 52 4f  entry of the FRO
1bdb0 4d 20 63 6c 61 75 73 65 20 69 73 20 61 20 73 75  M clause is a su
1bdb1 62 71 75 65 72 79 20 69 6e 73 74 65 61 64 20 6f  bquery instead o
1bdb2 66 20 61 20 74 61 62 6c 65 20 6f 72 20 76 69 65  f a table or vie
1bdb3 77 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 63 72 65  w,.  ** then cre
1bdb4 61 74 65 20 61 20 74 72 61 6e 73 69 65 6e 74 20  ate a transient 
1bdb5 74 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20  table structure 
1bdb6 74 6f 20 64 65 73 63 72 69 62 65 20 74 68 65 20  to describe the 
1bdb7 73 75 62 71 75 65 72 79 2e 0a 20 20 2a 2f 0a 20  subquery..  */. 
1bdb8 20 66 6f 72 28 69 3d 30 2c 20 70 46 72 6f 6d 3d   for(i=0, pFrom=
1bdb9 70 54 61 62 4c 69 73 74 2d 3e 61 3b 20 69 3c 70  pTabList->a; i<p
1bdba 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69  TabList->nSrc; i
1bdbb 2b 2b 2c 20 70 46 72 6f 6d 2b 2b 29 7b 0a 20 20  ++, pFrom++){.  
1bdbc 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20    Table *pTab;. 
1bdbd 20 20 20 69 66 28 20 70 46 72 6f 6d 2d 3e 70 54     if( pFrom->pT
1bdbe 61 62 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f  ab!=0 ){.      /
1bdbf 2a 20 54 68 69 73 20 73 74 61 74 65 6d 65 6e 74  * This statement
1bdc0 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65   has already bee
1bdc1 6e 20 70 72 65 70 61 72 65 64 2e 20 20 54 68 65  n prepared.  The
1bdc2 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20  re is no need.  
1bdc3 20 20 20 20 2a 2a 20 74 6f 20 67 6f 20 66 75 72      ** to go fur
1bdc4 74 68 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 61  ther. */.      a
1bdc5 73 73 65 72 74 28 20 69 3d 3d 30 20 29 3b 0a 20  ssert( i==0 );. 
1bdc6 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
1bdc7 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 46 72     }.    if( pFr
1bdc8 6f 6d 2d 3e 7a 4e 61 6d 65 3d 3d 30 20 29 7b 0a  om->zName==0 ){.
1bdc9 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1bdca 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20 20  MIT_SUBQUERY.   
1bdcb 20 20 20 2f 2a 20 41 20 73 75 62 2d 71 75 65 72     /* A sub-quer
1bdcc 79 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  y in the FROM cl
1bdcd 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43 54  ause of a SELECT
1bdce 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74   */.      assert
1bdcf 28 20 70 46 72 6f 6d 2d 3e 70 53 65 6c 65 63 74  ( pFrom->pSelect
1bdd0 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28  !=0 );.      if(
1bdd1 20 70 46 72 6f 6d 2d 3e 7a 41 6c 69 61 73 3d 3d   pFrom->zAlias==
1bdd2 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 72  0 ){.        pFr
1bdd3 6f 6d 2d 3e 7a 41 6c 69 61 73 20 3d 0a 20 20 20  om->zAlias =.   
1bdd4 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 50         sqlite3MP
1bdd5 72 69 6e 74 66 28 22 73 71 6c 69 74 65 5f 73 75  rintf("sqlite_su
1bdd6 62 71 75 65 72 79 5f 25 70 5f 22 2c 20 28 76 6f  bquery_%p_", (vo
1bdd7 69 64 2a 29 70 46 72 6f 6d 2d 3e 70 53 65 6c 65  id*)pFrom->pSele
1bdd8 63 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ct);.      }.   
1bdd9 20 20 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d     assert( pFrom
1bdda 2d 3e 70 54 61 62 3d 3d 30 20 29 3b 0a 20 20 20  ->pTab==0 );.   
1bddb 20 20 20 70 46 72 6f 6d 2d 3e 70 54 61 62 20 3d     pFrom->pTab =
1bddc 20 70 54 61 62 20 3d 20 0a 20 20 20 20 20 20 20   pTab = .       
1bddd 20 73 71 6c 69 74 65 33 52 65 73 75 6c 74 53 65   sqlite3ResultSe
1bdde 74 4f 66 53 65 6c 65 63 74 28 70 50 61 72 73 65  tOfSelect(pParse
1bddf 2c 20 70 46 72 6f 6d 2d 3e 7a 41 6c 69 61 73 2c  , pFrom->zAlias,
1bde0 20 70 46 72 6f 6d 2d 3e 70 53 65 6c 65 63 74 29   pFrom->pSelect)
1bde1 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 61 62  ;.      if( pTab
1bde2 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ==0 ){.        r
1bde3 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d  eturn 1;.      }
1bde4 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 73  .      /* The is
1bde5 45 70 68 65 6d 20 66 6c 61 67 20 69 6e 64 69 63  Ephem flag indic
1bde6 61 74 65 73 20 74 68 61 74 20 74 68 65 20 54 61  ates that the Ta
1bde7 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 68 61  ble structure ha
1bde8 73 20 62 65 65 6e 0a 20 20 20 20 20 20 2a 2a 20  s been.      ** 
1bde9 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f  dynamically allo
1bdea 63 61 74 65 64 20 61 6e 64 20 6d 61 79 20 62 65  cated and may be
1bdeb 20 66 72 65 65 64 20 61 74 20 61 6e 79 20 74 69   freed at any ti
1bdec 6d 65 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f  me.  In other wo
1bded 72 64 73 2c 0a 20 20 20 20 20 20 2a 2a 20 70 54  rds,.      ** pT
1bdee 61 62 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69  ab is not pointi
1bdef 6e 67 20 74 6f 20 61 20 70 65 72 73 69 73 74 65  ng to a persiste
1bdf0 6e 74 20 74 61 62 6c 65 20 73 74 72 75 63 74 75  nt table structu
1bdf1 72 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 0a  re that defines.
1bdf2 20 20 20 20 20 20 2a 2a 20 70 61 72 74 20 6f 66        ** part of
1bdf3 20 74 68 65 20 73 63 68 65 6d 61 2e 20 2a 2f 0a   the schema. */.
1bdf4 20 20 20 20 20 20 70 54 61 62 2d 3e 69 73 45 70        pTab->isEp
1bdf5 68 65 6d 20 3d 20 31 3b 0a 23 65 6e 64 69 66 0a  hem = 1;.#endif.
1bdf6 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1bdf7 20 2f 2a 20 41 6e 20 6f 72 64 69 6e 61 72 79 20   /* An ordinary 
1bdf8 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 6e 61  table or view na
1bdf9 6d 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  me in the FROM c
1bdfa 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 20 20 61  lause */.      a
1bdfb 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e 70 54  ssert( pFrom->pT
1bdfc 61 62 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70  ab==0 );.      p
1bdfd 46 72 6f 6d 2d 3e 70 54 61 62 20 3d 20 70 54 61  From->pTab = pTa
1bdfe 62 20 3d 20 0a 20 20 20 20 20 20 20 20 73 71 6c  b = .        sql
1bdff 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28  ite3LocateTable(
1be00 70 50 61 72 73 65 2c 70 46 72 6f 6d 2d 3e 7a 4e  pParse,pFrom->zN
1be01 61 6d 65 2c 70 46 72 6f 6d 2d 3e 7a 44 61 74 61  ame,pFrom->zData
1be02 62 61 73 65 29 3b 0a 20 20 20 20 20 20 69 66 28  base);.      if(
1be03 20 70 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20   pTab==0 ){.    
1be04 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
1be05 20 20 20 20 7d 0a 20 20 20 20 20 20 70 54 61 62      }.      pTab
1be06 2d 3e 6e 52 65 66 2b 2b 3b 0a 23 69 66 20 21 64  ->nRef++;.#if !d
1be07 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
1be08 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66  IT_VIEW) || !def
1be09 69 6e 65 64 20 28 53 51 4c 49 54 45 5f 4f 4d 49  ined (SQLITE_OMI
1be0a 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29 0a  T_VIRTUALTABLE).
1be0b 20 20 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e        if( pTab->
1be0c 70 53 65 6c 65 63 74 20 7c 7c 20 49 73 56 69 72  pSelect || IsVir
1be0d 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20  tual(pTab) ){.  
1be0e 20 20 20 20 20 20 2f 2a 20 57 65 20 72 65 61 63        /* We reac
1be0f 68 20 68 65 72 65 20 69 66 20 74 68 65 20 6e 61  h here if the na
1be10 6d 65 64 20 74 61 62 6c 65 20 69 73 20 61 20 72  med table is a r
1be11 65 61 6c 6c 79 20 61 20 76 69 65 77 20 2a 2f 0a  eally a view */.
1be12 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69          if( sqli
1be13 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e  te3ViewGetColumn
1be14 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70 54  Names(pParse, pT
1be15 61 62 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ab) ){.         
1be16 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
1be17 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20     }.        /* 
1be18 49 66 20 70 46 72 6f 6d 2d 3e 70 53 65 6c 65 63  If pFrom->pSelec
1be19 74 21 3d 30 20 69 74 20 6d 65 61 6e 73 20 77 65  t!=0 it means we
1be1a 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74   are dealing wit
1be1b 68 20 61 0a 20 20 20 20 20 20 20 20 2a 2a 20 76  h a.        ** v
1be1c 69 65 77 20 77 69 74 68 69 6e 20 61 20 76 69 65  iew within a vie
1be1d 77 2e 20 20 54 68 65 20 53 45 4c 45 43 54 20 73  w.  The SELECT s
1be1e 74 72 75 63 74 75 72 65 20 68 61 73 20 61 6c 72  tructure has alr
1be1f 65 61 64 79 20 62 65 65 6e 0a 20 20 20 20 20 20  eady been.      
1be20 20 20 2a 2a 20 63 6f 70 69 65 64 20 62 79 20 74    ** copied by t
1be21 68 65 20 6f 75 74 65 72 20 76 69 65 77 20 73 6f  he outer view so
1be22 20 77 65 20 63 61 6e 20 73 6b 69 70 20 74 68 65   we can skip the
1be23 20 63 6f 70 79 20 73 74 65 70 20 68 65 72 65 0a   copy step here.
1be24 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 20 74 68          ** in th
1be25 65 20 69 6e 6e 65 72 20 76 69 65 77 2e 0a 20 20  e inner view..  
1be26 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
1be27 20 69 66 28 20 70 46 72 6f 6d 2d 3e 70 53 65 6c   if( pFrom->pSel
1be28 65 63 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ect==0 ){.      
1be29 20 20 20 20 70 46 72 6f 6d 2d 3e 70 53 65 6c 65      pFrom->pSele
1be2a 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  ct = sqlite3Sele
1be2b 63 74 44 75 70 28 70 54 61 62 2d 3e 70 53 65 6c  ctDup(pTab->pSel
1be2c 65 63 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ect);.        }.
1be2d 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20        }.#endif. 
1be2e 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 50     }.  }..  /* P
1be2f 72 6f 63 65 73 73 20 4e 41 54 55 52 41 4c 20 6b  rocess NATURAL k
1be30 65 79 77 6f 72 64 73 2c 20 61 6e 64 20 4f 4e 20  eywords, and ON 
1be31 61 6e 64 20 55 53 49 4e 47 20 63 6c 61 75 73 65  and USING clause
1be32 73 20 6f 66 20 6a 6f 69 6e 73 2e 0a 20 20 2a 2f  s of joins..  */
1be33 0a 20 20 69 66 28 20 73 71 6c 69 74 65 50 72 6f  .  if( sqlitePro
1be34 63 65 73 73 4a 6f 69 6e 28 70 50 61 72 73 65 2c  cessJoin(pParse,
1be35 20 70 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a   p) ) return 1;.
1be36 0a 20 20 2f 2a 20 46 6f 72 20 65 76 65 72 79 20  .  /* For every 
1be37 22 2a 22 20 74 68 61 74 20 6f 63 63 75 72 73 20  "*" that occurs 
1be38 69 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6c 69  in the column li
1be39 73 74 2c 20 69 6e 73 65 72 74 20 74 68 65 20 6e  st, insert the n
1be3a 61 6d 65 73 20 6f 66 0a 20 20 2a 2a 20 61 6c 6c  ames of.  ** all
1be3b 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 6c 6c 20   columns in all 
1be3c 74 61 62 6c 65 73 2e 20 20 41 6e 64 20 66 6f 72  tables.  And for
1be3d 20 65 76 65 72 79 20 54 41 42 4c 45 2e 2a 20 69   every TABLE.* i
1be3e 6e 73 65 72 74 20 74 68 65 20 6e 61 6d 65 73 0a  nsert the names.
1be3f 20 20 2a 2a 20 6f 66 20 61 6c 6c 20 63 6f 6c 75    ** of all colu
1be40 6d 6e 73 20 69 6e 20 54 41 42 4c 45 2e 20 20 54  mns in TABLE.  T
1be41 68 65 20 70 61 72 73 65 72 20 69 6e 73 65 72 74  he parser insert
1be42 65 64 20 61 20 73 70 65 63 69 61 6c 20 65 78 70  ed a special exp
1be43 72 65 73 73 69 6f 6e 0a 20 20 2a 2a 20 77 69 74  ression.  ** wit
1be44 68 20 74 68 65 20 54 4b 5f 41 4c 4c 20 6f 70 65  h the TK_ALL ope
1be45 72 61 74 6f 72 20 66 6f 72 20 65 61 63 68 20 22  rator for each "
1be46 2a 22 20 74 68 61 74 20 69 74 20 66 6f 75 6e 64  *" that it found
1be47 20 69 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6c   in the column l
1be48 69 73 74 2e 0a 20 20 2a 2a 20 54 68 65 20 66 6f  ist..  ** The fo
1be49 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 6a 75 73  llowing code jus
1be4a 74 20 68 61 73 20 74 6f 20 6c 6f 63 61 74 65 20  t has to locate 
1be4b 74 68 65 20 54 4b 5f 41 4c 4c 20 65 78 70 72 65  the TK_ALL expre
1be4c 73 73 69 6f 6e 73 20 61 6e 64 20 65 78 70 61 6e  ssions and expan
1be4d 64 0a 20 20 2a 2a 20 65 61 63 68 20 6f 6e 65 20  d.  ** each one 
1be4e 74 6f 20 74 68 65 20 6c 69 73 74 20 6f 66 20 61  to the list of a
1be4f 6c 6c 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 6c  ll columns in al
1be50 6c 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2a 0a 20  l tables..  **. 
1be51 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20 6c 6f   ** The first lo
1be52 6f 70 20 6a 75 73 74 20 63 68 65 63 6b 73 20 74  op just checks t
1be53 6f 20 73 65 65 20 69 66 20 74 68 65 72 65 20 61  o see if there a
1be54 72 65 20 61 6e 79 20 22 2a 22 20 6f 70 65 72 61  re any "*" opera
1be55 74 6f 72 73 0a 20 20 2a 2a 20 74 68 61 74 20 6e  tors.  ** that n
1be56 65 65 64 20 65 78 70 61 6e 64 69 6e 67 2e 0a 20  eed expanding.. 
1be57 20 2a 2f 0a 20 20 66 6f 72 28 6b 3d 30 3b 20 6b   */.  for(k=0; k
1be58 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20  <pEList->nExpr; 
1be59 6b 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a  k++){.    Expr *
1be5a 70 45 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 6b  pE = pEList->a[k
1be5b 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 69 66 28  ].pExpr;.    if(
1be5c 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 41 4c 4c 20   pE->op==TK_ALL 
1be5d 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 69 66 28  ) break;.    if(
1be5e 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20   pE->op==TK_DOT 
1be5f 26 26 20 70 45 2d 3e 70 52 69 67 68 74 20 26 26  && pE->pRight &&
1be60 20 70 45 2d 3e 70 52 69 67 68 74 2d 3e 6f 70 3d   pE->pRight->op=
1be61 3d 54 4b 5f 41 4c 4c 0a 20 20 20 20 20 20 20 20  =TK_ALL.        
1be62 20 26 26 20 70 45 2d 3e 70 4c 65 66 74 20 26 26   && pE->pLeft &&
1be63 20 70 45 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d 3d   pE->pLeft->op==
1be64 54 4b 5f 49 44 20 29 20 62 72 65 61 6b 3b 0a 20  TK_ID ) break;. 
1be65 20 7d 0a 20 20 72 63 20 3d 20 30 3b 0a 20 20 69   }.  rc = 0;.  i
1be66 66 28 20 6b 3c 70 45 4c 69 73 74 2d 3e 6e 45 78  f( k<pEList->nEx
1be67 70 72 20 29 7b 0a 20 20 20 20 2f 2a 0a 20 20 20  pr ){.    /*.   
1be68 20 2a 2a 20 49 66 20 77 65 20 67 65 74 20 68 65   ** If we get he
1be69 72 65 20 69 74 20 6d 65 61 6e 73 20 74 68 65 20  re it means the 
1be6a 72 65 73 75 6c 74 20 73 65 74 20 63 6f 6e 74 61  result set conta
1be6b 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  ins one or more 
1be6c 22 2a 22 0a 20 20 20 20 2a 2a 20 6f 70 65 72 61  "*".    ** opera
1be6d 74 6f 72 73 20 74 68 61 74 20 6e 65 65 64 20 74  tors that need t
1be6e 6f 20 62 65 20 65 78 70 61 6e 64 65 64 2e 20 20  o be expanded.  
1be6f 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 65 61 63  Loop through eac
1be70 68 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 20  h expression.   
1be71 20 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 75 6c   ** in the resul
1be72 74 20 73 65 74 20 61 6e 64 20 65 78 70 61 6e 64  t set and expand
1be73 20 74 68 65 6d 20 6f 6e 65 20 62 79 20 6f 6e 65   them one by one
1be74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 74 72  ..    */.    str
1be75 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
1be76 6d 20 2a 61 20 3d 20 70 45 4c 69 73 74 2d 3e 61  m *a = pEList->a
1be77 3b 0a 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a  ;.    ExprList *
1be78 70 4e 65 77 20 3d 20 30 3b 0a 20 20 20 20 69 6e  pNew = 0;.    in
1be79 74 20 66 6c 61 67 73 20 3d 20 70 50 61 72 73 65  t flags = pParse
1be7a 2d 3e 64 62 2d 3e 66 6c 61 67 73 3b 0a 20 20 20  ->db->flags;.   
1be7b 20 69 6e 74 20 6c 6f 6e 67 4e 61 6d 65 73 20 3d   int longNames =
1be7c 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45   (flags & SQLITE
1be7d 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73 29 21 3d  _FullColNames)!=
1be7e 30 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20  0 &&.           
1be7f 20 20 20 20 20 20 20 20 20 20 20 28 66 6c 61 67             (flag
1be80 73 20 26 20 53 51 4c 49 54 45 5f 53 68 6f 72 74  s & SQLITE_Short
1be81 43 6f 6c 4e 61 6d 65 73 29 3d 3d 30 3b 0a 0a 20  ColNames)==0;.. 
1be82 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 45     for(k=0; k<pE
1be83 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 6b 2b 2b  List->nExpr; k++
1be84 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70  ){.      Expr *p
1be85 45 20 3d 20 61 5b 6b 5d 2e 70 45 78 70 72 3b 0a  E = a[k].pExpr;.
1be86 20 20 20 20 20 20 69 66 28 20 70 45 2d 3e 6f 70        if( pE->op
1be87 21 3d 54 4b 5f 41 4c 4c 20 26 26 0a 20 20 20 20  !=TK_ALL &&.    
1be88 20 20 20 20 20 20 20 28 70 45 2d 3e 6f 70 21 3d         (pE->op!=
1be89 54 4b 5f 44 4f 54 20 7c 7c 20 70 45 2d 3e 70 52  TK_DOT || pE->pR
1be8a 69 67 68 74 3d 3d 30 20 7c 7c 20 70 45 2d 3e 70  ight==0 || pE->p
1be8b 52 69 67 68 74 2d 3e 6f 70 21 3d 54 4b 5f 41 4c  Right->op!=TK_AL
1be8c 4c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  L) ){.        /*
1be8d 20 54 68 69 73 20 70 61 72 74 69 63 75 6c 61 72   This particular
1be8e 20 65 78 70 72 65 73 73 69 6f 6e 20 64 6f 65 73   expression does
1be8f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20   not need to be 
1be90 65 78 70 61 6e 64 65 64 2e 0a 20 20 20 20 20 20  expanded..      
1be91 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 4e 65    */.        pNe
1be92 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  w = sqlite3ExprL
1be93 69 73 74 41 70 70 65 6e 64 28 70 4e 65 77 2c 20  istAppend(pNew, 
1be94 61 5b 6b 5d 2e 70 45 78 70 72 2c 20 30 29 3b 0a  a[k].pExpr, 0);.
1be95 20 20 20 20 20 20 20 20 69 66 28 20 70 4e 65 77          if( pNew
1be96 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4e   ){.          pN
1be97 65 77 2d 3e 61 5b 70 4e 65 77 2d 3e 6e 45 78 70  ew->a[pNew->nExp
1be98 72 2d 31 5d 2e 7a 4e 61 6d 65 20 3d 20 61 5b 6b  r-1].zName = a[k
1be99 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20  ].zName;.       
1be9a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1be9b 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 20 20    rc = 1;.      
1be9c 20 20 7d 0a 20 20 20 20 20 20 20 20 61 5b 6b 5d    }.        a[k]
1be9d 2e 70 45 78 70 72 20 3d 20 30 3b 0a 20 20 20 20  .pExpr = 0;.    
1be9e 20 20 20 20 61 5b 6b 5d 2e 7a 4e 61 6d 65 20 3d      a[k].zName =
1be9f 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b   0;.      }else{
1bea0 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73  .        /* This
1bea1 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61   expression is a
1bea2 20 22 2a 22 20 6f 72 20 61 20 22 54 41 42 4c 45   "*" or a "TABLE
1bea3 2e 2a 22 20 61 6e 64 20 6e 65 65 64 73 20 74 6f  .*" and needs to
1bea4 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 65   be.        ** e
1bea5 78 70 61 6e 64 65 64 2e 20 2a 2f 0a 20 20 20 20  xpanded. */.    
1bea6 20 20 20 20 69 6e 74 20 74 61 62 6c 65 53 65 65      int tableSee
1bea7 6e 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 53  n = 0;      /* S
1bea8 65 74 20 74 6f 20 31 20 77 68 65 6e 20 54 41 42  et to 1 when TAB
1bea9 4c 45 20 6d 61 74 63 68 65 73 20 2a 2f 0a 20 20  LE matches */.  
1beaa 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 4e 61        char *zTNa
1beab 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  me;            /
1beac 2a 20 74 65 78 74 20 6f 66 20 6e 61 6d 65 20 6f  * text of name o
1bead 66 20 54 41 42 4c 45 20 2a 2f 0a 20 20 20 20 20  f TABLE */.     
1beae 20 20 20 69 66 28 20 70 45 2d 3e 6f 70 3d 3d 54     if( pE->op==T
1beaf 4b 5f 44 4f 54 20 26 26 20 70 45 2d 3e 70 4c 65  K_DOT && pE->pLe
1beb0 66 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ft ){.          
1beb1 7a 54 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33  zTName = sqlite3
1beb2 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 26 70  NameFromToken(&p
1beb3 45 2d 3e 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e 29  E->pLeft->token)
1beb4 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
1beb5 0a 20 20 20 20 20 20 20 20 20 20 7a 54 4e 61 6d  .          zTNam
1beb6 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d  e = 0;.        }
1beb7 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30  .        for(i=0
1beb8 2c 20 70 46 72 6f 6d 3d 70 54 61 62 4c 69 73 74  , pFrom=pTabList
1beb9 2d 3e 61 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d  ->a; i<pTabList-
1beba 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 46 72 6f  >nSrc; i++, pFro
1bebb 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  m++){.          
1bebc 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 46  Table *pTab = pF
1bebd 72 6f 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20  rom->pTab;.     
1bebe 20 20 20 20 20 63 68 61 72 20 2a 7a 54 61 62 4e       char *zTabN
1bebf 61 6d 65 20 3d 20 70 46 72 6f 6d 2d 3e 7a 41 6c  ame = pFrom->zAl
1bec0 69 61 73 3b 0a 20 20 20 20 20 20 20 20 20 20 69  ias;.          i
1bec1 66 28 20 7a 54 61 62 4e 61 6d 65 3d 3d 30 20 7c  f( zTabName==0 |
1bec2 7c 20 7a 54 61 62 4e 61 6d 65 5b 30 5d 3d 3d 30  | zTabName[0]==0
1bec3 20 29 7b 20 0a 20 20 20 20 20 20 20 20 20 20 20   ){ .           
1bec4 20 7a 54 61 62 4e 61 6d 65 20 3d 20 70 54 61 62   zTabName = pTab
1bec5 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20  ->zName;.       
1bec6 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69     }.          i
1bec7 66 28 20 7a 54 4e 61 6d 65 20 26 26 20 28 7a 54  f( zTName && (zT
1bec8 61 62 4e 61 6d 65 3d 3d 30 20 7c 7c 20 7a 54 61  abName==0 || zTa
1bec9 62 4e 61 6d 65 5b 30 5d 3d 3d 30 20 7c 7c 20 0a  bName[0]==0 || .
1beca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1becb 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
1becc 7a 54 4e 61 6d 65 2c 20 7a 54 61 62 4e 61 6d 65  zTName, zTabName
1becd 29 21 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 20  )!=0) ){.       
1bece 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
1becf 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1bed0 20 20 20 20 20 74 61 62 6c 65 53 65 65 6e 20 3d       tableSeen =
1bed1 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f   1;.          fo
1bed2 72 28 6a 3d 30 3b 20 6a 3c 70 54 61 62 2d 3e 6e  r(j=0; j<pTab->n
1bed3 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  Col; j++){.     
1bed4 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 78         Expr *pEx
1bed5 70 72 2c 20 2a 70 52 69 67 68 74 3b 0a 20 20 20  pr, *pRight;.   
1bed6 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a           char *z
1bed7 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e 61 43 6f  Name = pTab->aCo
1bed8 6c 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 0a 20 20 20  l[j].zName;..   
1bed9 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61           /* If a
1beda 20 63 6f 6c 75 6d 6e 20 69 73 20 6d 61 72 6b 65   column is marke
1bedb 64 20 61 73 20 27 68 69 64 64 65 6e 27 20 28 63  d as 'hidden' (c
1bedc 75 72 72 65 6e 74 6c 79 20 6f 6e 6c 79 20 70 6f  urrently only po
1bedd 73 73 69 62 6c 65 0a 20 20 20 20 20 20 20 20 20  ssible.         
1bede 20 20 20 2a 2a 20 66 6f 72 20 76 69 72 74 75 61     ** for virtua
1bedf 6c 20 74 61 62 6c 65 73 29 2c 20 64 6f 20 6e 6f  l tables), do no
1bee0 74 20 69 6e 63 6c 75 64 65 20 69 74 20 69 6e 20  t include it in 
1bee1 74 68 65 20 65 78 70 61 6e 64 65 64 0a 20 20 20  the expanded.   
1bee2 20 20 20 20 20 20 20 20 20 2a 2a 20 72 65 73 75           ** resu
1bee3 6c 74 2d 73 65 74 20 6c 69 73 74 2e 0a 20 20 20  lt-set list..   
1bee4 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20           */.    
1bee5 20 20 20 20 20 20 20 20 69 66 28 20 49 73 48 69          if( IsHi
1bee6 64 64 65 6e 43 6f 6c 75 6d 6e 28 26 70 54 61 62  ddenColumn(&pTab
1bee7 2d 3e 61 43 6f 6c 5b 6a 5d 29 20 29 7b 0a 20 20  ->aCol[j]) ){.  
1bee8 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65              asse
1bee9 72 74 28 49 73 56 69 72 74 75 61 6c 28 70 54 61  rt(IsVirtual(pTa
1beea 62 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  b));.           
1beeb 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
1beec 20 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20           }..    
1beed 20 20 20 20 20 20 20 20 69 66 28 20 69 3e 30 20          if( i>0 
1beee 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
1beef 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
1bef0 69 74 65 6d 20 2a 70 4c 65 66 74 20 3d 20 26 70  item *pLeft = &p
1bef1 54 61 62 4c 69 73 74 2d 3e 61 5b 69 2d 31 5d 3b  TabList->a[i-1];
1bef2 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69  .              i
1bef3 66 28 20 28 70 4c 65 66 74 5b 31 5d 2e 6a 6f 69  f( (pLeft[1].joi
1bef4 6e 74 79 70 65 20 26 20 4a 54 5f 4e 41 54 55 52  ntype & JT_NATUR
1bef5 41 4c 29 21 3d 30 20 26 26 0a 20 20 20 20 20 20  AL)!=0 &&.      
1bef6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bef7 20 20 63 6f 6c 75 6d 6e 49 6e 64 65 78 28 70 4c    columnIndex(pL
1bef8 65 66 74 2d 3e 70 54 61 62 2c 20 7a 4e 61 6d 65  eft->pTab, zName
1bef9 29 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )>=0 ){.        
1befa 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 61 20          /* In a 
1befb 4e 41 54 55 52 41 4c 20 6a 6f 69 6e 2c 20 6f 6d  NATURAL join, om
1befc 69 74 20 74 68 65 20 6a 6f 69 6e 20 63 6f 6c 75  it the join colu
1befd 6d 6e 73 20 66 72 6f 6d 20 74 68 65 20 0a 20 20  mns from the .  
1befe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a                **
1beff 20 74 61 62 6c 65 20 6f 6e 20 74 68 65 20 72 69   table on the ri
1bf00 67 68 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  ght */.         
1bf01 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b         continue;
1bf02 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d  .              }
1bf03 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69  .              i
1bf04 66 28 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74  f( sqlite3IdList
1bf05 49 6e 64 65 78 28 70 4c 65 66 74 5b 31 5d 2e 70  Index(pLeft[1].p
1bf06 55 73 69 6e 67 2c 20 7a 4e 61 6d 65 29 3e 3d 30  Using, zName)>=0
1bf07 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
1bf08 20 20 20 20 2f 2a 20 49 6e 20 61 20 6a 6f 69 6e      /* In a join
1bf09 20 77 69 74 68 20 61 20 55 53 49 4e 47 20 63 6c   with a USING cl
1bf0a 61 75 73 65 2c 20 6f 6d 69 74 20 63 6f 6c 75 6d  ause, omit colum
1bf0b 6e 73 20 69 6e 20 74 68 65 0a 20 20 20 20 20 20  ns in the.      
1bf0c 20 20 20 20 20 20 20 20 20 20 2a 2a 20 75 73 69            ** usi
1bf0d 6e 67 20 63 6c 61 75 73 65 20 66 72 6f 6d 20 74  ng clause from t
1bf0e 68 65 20 74 61 62 6c 65 20 6f 6e 20 74 68 65 20  he table on the 
1bf0f 72 69 67 68 74 2e 20 2a 2f 0a 20 20 20 20 20 20  right. */.      
1bf10 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e            contin
1bf11 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ue;.            
1bf12 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
1bf13 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 52  }.            pR
1bf14 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33 45 78  ight = sqlite3Ex
1bf15 70 72 28 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20  pr(TK_ID, 0, 0, 
1bf16 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  0);.            
1bf17 69 66 28 20 70 52 69 67 68 74 3d 3d 30 20 29 20  if( pRight==0 ) 
1bf18 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
1bf19 20 20 20 73 65 74 51 75 6f 74 65 64 54 6f 6b 65     setQuotedToke
1bf1a 6e 28 26 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e  n(&pRight->token
1bf1b 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  , zName);.      
1bf1c 20 20 20 20 20 20 69 66 28 20 7a 54 61 62 4e 61        if( zTabNa
1bf1d 6d 65 20 26 26 20 28 6c 6f 6e 67 4e 61 6d 65 73  me && (longNames
1bf1e 20 7c 7c 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53   || pTabList->nS
1bf1f 72 63 3e 31 29 20 29 7b 0a 20 20 20 20 20 20 20  rc>1) ){.       
1bf20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c 65         Expr *pLe
1bf21 66 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ft = sqlite3Expr
1bf22 28 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 30 29  (TK_ID, 0, 0, 0)
1bf23 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
1bf24 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 45  pExpr = sqlite3E
1bf25 78 70 72 28 54 4b 5f 44 4f 54 2c 20 70 4c 65 66  xpr(TK_DOT, pLef
1bf26 74 2c 20 70 52 69 67 68 74 2c 20 30 29 3b 0a 20  t, pRight, 0);. 
1bf27 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28               if(
1bf28 20 70 45 78 70 72 3d 3d 30 20 29 20 62 72 65 61   pExpr==0 ) brea
1bf29 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  k;.             
1bf2a 20 73 65 74 51 75 6f 74 65 64 54 6f 6b 65 6e 28   setQuotedToken(
1bf2b 26 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e 2c 20 7a  &pLeft->token, z
1bf2c 54 61 62 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  TabName);.      
1bf2d 20 20 20 20 20 20 20 20 73 65 74 54 6f 6b 65 6e          setToken
1bf2e 28 26 70 45 78 70 72 2d 3e 73 70 61 6e 2c 20 73  (&pExpr->span, s
1bf2f 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 22 25  qlite3MPrintf("%
1bf30 73 2e 25 73 22 2c 20 7a 54 61 62 4e 61 6d 65 2c  s.%s", zTabName,
1bf31 20 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 20 20   zName));.      
1bf32 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 73          pExpr->s
1bf33 70 61 6e 2e 64 79 6e 20 3d 20 31 3b 0a 20 20 20  pan.dyn = 1;.   
1bf34 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72             pExpr
1bf35 2d 3e 74 6f 6b 65 6e 2e 7a 20 3d 20 30 3b 0a 20  ->token.z = 0;. 
1bf36 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78               pEx
1bf37 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 20 3d 20 30 3b  pr->token.n = 0;
1bf38 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
1bf39 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 64 79 6e 20  Expr->token.dyn 
1bf3a 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20  = 0;.           
1bf3b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1bf3c 20 20 20 20 20 20 70 45 78 70 72 20 3d 20 70 52        pExpr = pR
1bf3d 69 67 68 74 3b 0a 20 20 20 20 20 20 20 20 20 20  ight;.          
1bf3e 20 20 20 20 70 45 78 70 72 2d 3e 73 70 61 6e 20      pExpr->span 
1bf3f 3d 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 3b 0a  = pExpr->token;.
1bf40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45                pE
1bf41 78 70 72 2d 3e 73 70 61 6e 2e 64 79 6e 20 3d 20  xpr->span.dyn = 
1bf42 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  0;.            }
1bf43 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
1bf44 20 6c 6f 6e 67 4e 61 6d 65 73 20 29 7b 0a 20 20   longNames ){.  
1bf45 20 20 20 20 20 20 20 20 20 20 20 20 70 4e 65 77              pNew
1bf46 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
1bf47 73 74 41 70 70 65 6e 64 28 70 4e 65 77 2c 20 70  stAppend(pNew, p
1bf48 45 78 70 72 2c 20 26 70 45 78 70 72 2d 3e 73 70  Expr, &pExpr->sp
1bf49 61 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  an);.           
1bf4a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1bf4b 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c        pNew = sql
1bf4c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
1bf4d 6e 64 28 70 4e 65 77 2c 20 70 45 78 70 72 2c 20  nd(pNew, pExpr, 
1bf4e 26 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e 29 3b  &pRight->token);
1bf4f 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
1bf50 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1bf51 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
1bf52 20 21 74 61 62 6c 65 53 65 65 6e 20 29 7b 0a 20   !tableSeen ){. 
1bf53 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 54 4e           if( zTN
1bf54 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ame ){.         
1bf55 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1bf56 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73  sg(pParse, "no s
1bf57 75 63 68 20 74 61 62 6c 65 3a 20 25 73 22 2c 20  uch table: %s", 
1bf58 7a 54 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20  zTName);.       
1bf59 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1bf5a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
1bf5b 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e  orMsg(pParse, "n
1bf5c 6f 20 74 61 62 6c 65 73 20 73 70 65 63 69 66 69  o tables specifi
1bf5d 65 64 22 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ed");.          
1bf5e 7d 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  }.          rc =
1bf5f 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   1;.        }.  
1bf60 20 20 20 20 20 20 73 71 6c 69 74 65 46 72 65 65        sqliteFree
1bf61 28 7a 54 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  (zTName);.      
1bf62 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  }.    }.    sqli
1bf63 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
1bf64 65 28 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 70  e(pEList);.    p
1bf65 2d 3e 70 45 4c 69 73 74 20 3d 20 70 4e 65 77 3b  ->pEList = pNew;
1bf66 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 45  .  }.  if( p->pE
1bf67 4c 69 73 74 20 26 26 20 70 2d 3e 70 45 4c 69 73  List && p->pELis
1bf68 74 2d 3e 6e 45 78 70 72 3e 53 51 4c 49 54 45 5f  t->nExpr>SQLITE_
1bf69 4d 41 58 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20  MAX_COLUMN ){.  
1bf6a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
1bf6b 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d  g(pParse, "too m
1bf6c 61 6e 79 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 72  any columns in r
1bf6d 65 73 75 6c 74 20 73 65 74 22 29 3b 0a 20 20 20  esult set");.   
1bf6e 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
1bf6f 4f 52 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71  OR;.  }.  if( sq
1bf70 6c 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69 6c 65  lite3MallocFaile
1bf71 64 28 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  d() ){.    rc = 
1bf72 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
1bf73 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
1bf74 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1bf75 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53  _OMIT_COMPOUND_S
1bf76 45 4c 45 43 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ELECT./*.** This
1bf77 20 72 6f 75 74 69 6e 65 20 61 73 73 6f 63 69 61   routine associa
1bf78 74 65 73 20 65 6e 74 72 69 65 73 20 69 6e 20 61  tes entries in a
1bf79 6e 20 4f 52 44 45 52 20 42 59 20 65 78 70 72 65  n ORDER BY expre
1bf7a 73 73 69 6f 6e 20 6c 69 73 74 20 77 69 74 68 0a  ssion list with.
1bf7b 2a 2a 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 20  ** columns in a 
1bf7c 72 65 73 75 6c 74 2e 20 20 46 6f 72 20 65 61 63  result.  For eac
1bf7d 68 20 4f 52 44 45 52 20 42 59 20 65 78 70 72 65  h ORDER BY expre
1bf7e 73 73 69 6f 6e 2c 20 74 68 65 20 6f 70 63 6f 64  ssion, the opcod
1bf7f 65 20 6f 66 0a 2a 2a 20 74 68 65 20 74 6f 70 2d  e of.** the top-
1bf80 6c 65 76 65 6c 20 6e 6f 64 65 20 69 73 20 63 68  level node is ch
1bf81 61 6e 67 65 64 20 74 6f 20 54 4b 5f 43 4f 4c 55  anged to TK_COLU
1bf82 4d 4e 20 61 6e 64 20 74 68 65 20 69 43 6f 6c 75  MN and the iColu
1bf83 6d 6e 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 74  mn value of.** t
1bf84 68 65 20 74 6f 70 2d 6c 65 76 65 6c 20 6e 6f 64  he top-level nod
1bf85 65 20 69 73 20 66 69 6c 6c 65 64 20 69 6e 20 77  e is filled in w
1bf86 69 74 68 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65  ith column numbe
1bf87 72 20 61 6e 64 20 74 68 65 20 69 54 61 62 6c 65  r and the iTable
1bf88 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 74 68 65  .** value of the
1bf89 20 74 6f 70 2d 6c 65 76 65 6c 20 6e 6f 64 65 20   top-level node 
1bf8a 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20 69  is filled with i
1bf8b 54 61 62 6c 65 20 70 61 72 61 6d 65 74 65 72 2e  Table parameter.
1bf8c 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20  .**.** If there 
1bf8d 61 72 65 20 70 72 69 6f 72 20 53 45 4c 45 43 54  are prior SELECT
1bf8e 20 63 6c 61 75 73 65 73 2c 20 74 68 65 79 20 61   clauses, they a
1bf8f 72 65 20 70 72 6f 63 65 73 73 65 64 20 66 69 72  re processed fir
1bf90 73 74 2e 20 20 41 20 6d 61 74 63 68 0a 2a 2a 20  st.  A match.** 
1bf91 69 6e 20 61 6e 20 65 61 72 6c 69 65 72 20 53 45  in an earlier SE
1bf92 4c 45 43 54 20 74 61 6b 65 73 20 70 72 65 63 65  LECT takes prece
1bf93 64 65 6e 63 65 20 6f 76 65 72 20 61 20 6c 61 74  dence over a lat
1bf94 65 72 20 53 45 4c 45 43 54 2e 0a 2a 2a 0a 2a 2a  er SELECT..**.**
1bf95 20 41 6e 79 20 65 6e 74 72 79 20 74 68 61 74 20   Any entry that 
1bf96 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 69  does not match i
1bf97 73 20 66 6c 61 67 67 65 64 20 61 73 20 61 6e 20  s flagged as an 
1bf98 65 72 72 6f 72 2e 20 20 54 68 65 20 6e 75 6d 62  error.  The numb
1bf99 65 72 0a 2a 2a 20 6f 66 20 65 72 72 6f 72 73 20  er.** of errors 
1bf9a 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a  is returned..*/.
1bf9b 73 74 61 74 69 63 20 69 6e 74 20 6d 61 74 63 68  static int match
1bf9c 4f 72 64 65 72 62 79 54 6f 43 6f 6c 75 6d 6e 28  OrderbyToColumn(
1bf9d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
1bf9e 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20  ,          /* A 
1bf9f 70 6c 61 63 65 20 74 6f 20 6c 65 61 76 65 20 65  place to leave e
1bfa0 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f  rror messages */
1bfa1 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65  .  Select *pSele
1bfa2 63 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 4d 61  ct,        /* Ma
1bfa3 74 63 68 20 74 6f 20 72 65 73 75 6c 74 20 63 6f  tch to result co
1bfa4 6c 75 6d 6e 73 20 6f 66 20 74 68 69 73 20 53 45  lumns of this SE
1bfa5 4c 45 43 54 20 2a 2f 0a 20 20 45 78 70 72 4c 69  LECT */.  ExprLi
1bfa6 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20  st *pOrderBy,   
1bfa7 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42    /* The ORDER B
1bfa8 59 20 76 61 6c 75 65 73 20 74 6f 20 6d 61 74 63  Y values to matc
1bfa9 68 20 61 67 61 69 6e 73 74 20 63 6f 6c 75 6d 6e  h against column
1bfaa 73 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c  s */.  int iTabl
1bfab 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  e,             /
1bfac 2a 20 49 6e 73 65 72 74 20 74 68 69 73 20 76 61  * Insert this va
1bfad 6c 75 65 20 69 6e 20 69 54 61 62 6c 65 20 2a 2f  lue in iTable */
1bfae 0a 20 20 69 6e 74 20 6d 75 73 74 43 6f 6d 70 6c  .  int mustCompl
1bfaf 65 74 65 20 20 20 20 20 20 20 20 2f 2a 20 49 66  ete        /* If
1bfb0 20 54 52 55 45 20 61 6c 6c 20 4f 52 44 45 52 20   TRUE all ORDER 
1bfb1 42 59 73 20 6d 75 73 74 20 6d 61 74 63 68 20 2a  BYs must match *
1bfb2 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 45 72 72 20  /.){.  int nErr 
1bfb3 3d 20 30 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b  = 0;.  int i, j;
1bfb4 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c  .  ExprList *pEL
1bfb5 69 73 74 3b 0a 0a 20 20 69 66 28 20 70 53 65 6c  ist;..  if( pSel
1bfb6 65 63 74 3d 3d 30 20 7c 7c 20 70 4f 72 64 65 72  ect==0 || pOrder
1bfb7 42 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31  By==0 ) return 1
1bfb8 3b 0a 20 20 69 66 28 20 6d 75 73 74 43 6f 6d 70  ;.  if( mustComp
1bfb9 6c 65 74 65 20 29 7b 0a 20 20 20 20 66 6f 72 28  lete ){.    for(
1bfba 69 3d 30 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d  i=0; i<pOrderBy-
1bfbb 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 20 70 4f  >nExpr; i++){ pO
1bfbc 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 64 6f 6e  rderBy->a[i].don
1bfbd 65 20 3d 20 30 3b 20 7d 0a 20 20 7d 0a 20 20 69  e = 0; }.  }.  i
1bfbe 66 28 20 70 72 65 70 53 65 6c 65 63 74 53 74 6d  f( prepSelectStm
1bfbf 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63  t(pParse, pSelec
1bfc0 74 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  t) ){.    return
1bfc1 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 53   1;.  }.  if( pS
1bfc2 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 20 29 7b  elect->pPrior ){
1bfc3 0a 20 20 20 20 69 66 28 20 6d 61 74 63 68 4f 72  .    if( matchOr
1bfc4 64 65 72 62 79 54 6f 43 6f 6c 75 6d 6e 28 70 50  derbyToColumn(pP
1bfc5 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2d 3e 70  arse, pSelect->p
1bfc6 50 72 69 6f 72 2c 20 70 4f 72 64 65 72 42 79 2c  Prior, pOrderBy,
1bfc7 20 69 54 61 62 6c 65 2c 20 30 29 20 29 7b 0a 20   iTable, 0) ){. 
1bfc8 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
1bfc9 20 20 20 7d 0a 20 20 7d 0a 20 20 70 45 4c 69 73     }.  }.  pELis
1bfca 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c  t = pSelect->pEL
1bfcb 69 73 74 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  ist;.  for(i=0; 
1bfcc 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70  i<pOrderBy->nExp
1bfcd 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72  r; i++){.    str
1bfce 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
1bfcf 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 45 78  m *pItem;.    Ex
1bfd0 70 72 20 2a 70 45 20 3d 20 70 4f 72 64 65 72 42  pr *pE = pOrderB
1bfd1 79 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20  y->a[i].pExpr;. 
1bfd2 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 2d 31     int iCol = -1
1bfd3 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4c 61 62  ;.    char *zLab
1bfd4 65 6c 3b 0a 0a 20 20 20 20 69 66 28 20 70 4f 72  el;..    if( pOr
1bfd5 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 64 6f 6e 65  derBy->a[i].done
1bfd6 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
1bfd7 20 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 72   if( sqlite3Expr
1bfd8 49 73 49 6e 74 65 67 65 72 28 70 45 2c 20 26 69  IsInteger(pE, &i
1bfd9 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 69 66  Col) ){.      if
1bfda 28 20 69 43 6f 6c 3c 3d 30 20 7c 7c 20 69 43 6f  ( iCol<=0 || iCo
1bfdb 6c 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20  l>pEList->nExpr 
1bfdc 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1bfdd 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
1bfde 65 2c 0a 20 20 20 20 20 20 20 20 20 20 22 4f 52  e,.          "OR
1bfdf 44 45 52 20 42 59 20 70 6f 73 69 74 69 6f 6e 20  DER BY position 
1bfe0 25 64 20 73 68 6f 75 6c 64 20 62 65 20 62 65 74  %d should be bet
1bfe1 77 65 65 6e 20 31 20 61 6e 64 20 25 64 22 2c 0a  ween 1 and %d",.
1bfe2 20 20 20 20 20 20 20 20 20 20 69 43 6f 6c 2c 20            iCol, 
1bfe3 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 3b 0a  pEList->nExpr);.
1bfe4 20 20 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a          nErr++;.
1bfe5 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
1bfe6 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
1bfe7 20 21 6d 75 73 74 43 6f 6d 70 6c 65 74 65 20 29   !mustComplete )
1bfe8 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
1bfe9 20 69 43 6f 6c 2d 2d 3b 0a 20 20 20 20 7d 0a 20   iCol--;.    }. 
1bfea 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 26 26     if( iCol<0 &&
1bfeb 20 28 7a 4c 61 62 65 6c 20 3d 20 73 71 6c 69 74   (zLabel = sqlit
1bfec 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
1bfed 26 70 45 2d 3e 74 6f 6b 65 6e 29 29 21 3d 30 20  &pE->token))!=0 
1bfee 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30  ){.      for(j=0
1bfef 2c 20 70 49 74 65 6d 3d 70 45 4c 69 73 74 2d 3e  , pItem=pEList->
1bff0 61 3b 20 6a 3c 70 45 4c 69 73 74 2d 3e 6e 45 78  a; j<pEList->nEx
1bff1 70 72 3b 20 6a 2b 2b 2c 20 70 49 74 65 6d 2b 2b  pr; j++, pItem++
1bff2 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20  ){.        char 
1bff3 2a 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20  *zName;.        
1bff4 69 6e 74 20 69 73 4d 61 74 63 68 3b 0a 20 20 20  int isMatch;.   
1bff5 20 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e       if( pItem->
1bff6 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 20  zName ){.       
1bff7 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74     zName = sqlit
1bff8 65 33 53 74 72 44 75 70 28 70 49 74 65 6d 2d 3e  e3StrDup(pItem->
1bff9 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20  zName);.        
1bffa 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
1bffb 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   zName = sqlite3
1bffc 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 26 70  NameFromToken(&p
1bffd 49 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 74 6f 6b  Item->pExpr->tok
1bffe 65 6e 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  en);.        }. 
1bfff 20 20 20 20 20 20 20 69 73 4d 61 74 63 68 20 3d         isMatch =
1c000 20 7a 4e 61 6d 65 20 26 26 20 73 71 6c 69 74 65   zName && sqlite
1c001 33 53 74 72 49 43 6d 70 28 7a 4e 61 6d 65 2c 20  3StrICmp(zName, 
1c002 7a 4c 61 62 65 6c 29 3d 3d 30 3b 0a 20 20 20 20  zLabel)==0;.    
1c003 20 20 20 20 73 71 6c 69 74 65 46 72 65 65 28 7a      sqliteFree(z
1c004 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 69  Name);.        i
1c005 66 28 20 69 73 4d 61 74 63 68 20 29 7b 0a 20 20  f( isMatch ){.  
1c006 20 20 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 6a          iCol = j
1c007 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  ;.          brea
1c008 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
1c009 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
1c00a 65 46 72 65 65 28 7a 4c 61 62 65 6c 29 3b 0a 20  eFree(zLabel);. 
1c00b 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 43 6f     }.    if( iCo
1c00c 6c 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 45  l>=0 ){.      pE
1c00d 2d 3e 6f 70 20 3d 20 54 4b 5f 43 4f 4c 55 4d 4e  ->op = TK_COLUMN
1c00e 3b 0a 20 20 20 20 20 20 70 45 2d 3e 69 43 6f 6c  ;.      pE->iCol
1c00f 75 6d 6e 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20  umn = iCol;.    
1c010 20 20 70 45 2d 3e 69 54 61 62 6c 65 20 3d 20 69    pE->iTable = i
1c011 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 70 45 2d  Table;.      pE-
1c012 3e 69 41 67 67 20 3d 20 2d 31 3b 0a 20 20 20 20  >iAgg = -1;.    
1c013 20 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d    pOrderBy->a[i]
1c014 2e 64 6f 6e 65 20 3d 20 31 3b 0a 20 20 20 20 7d  .done = 1;.    }
1c015 65 6c 73 65 20 69 66 28 20 6d 75 73 74 43 6f 6d  else if( mustCom
1c016 70 6c 65 74 65 20 29 7b 0a 20 20 20 20 20 20 73  plete ){.      s
1c017 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
1c018 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 22  Parse,.        "
1c019 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 6e 75  ORDER BY term nu
1c01a 6d 62 65 72 20 25 64 20 64 6f 65 73 20 6e 6f 74  mber %d does not
1c01b 20 6d 61 74 63 68 20 61 6e 79 20 72 65 73 75 6c   match any resul
1c01c 74 20 63 6f 6c 75 6d 6e 22 2c 20 69 2b 31 29 3b  t column", i+1);
1c01d 0a 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20  .      nErr++;. 
1c01e 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1c01f 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e  }.  }.  return n
1c020 45 72 72 3b 20 20 0a 7d 0a 23 65 6e 64 69 66 20  Err;  .}.#endif 
1c021 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /* #ifndef SQLIT
1c022 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f  E_OMIT_COMPOUND_
1c023 53 45 4c 45 43 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  SELECT */../*.**
1c024 20 47 65 74 20 61 20 56 44 42 45 20 66 6f 72 20   Get a VDBE for 
1c025 74 68 65 20 67 69 76 65 6e 20 70 61 72 73 65 72  the given parser
1c026 20 63 6f 6e 74 65 78 74 2e 20 20 43 72 65 61 74   context.  Creat
1c027 65 20 61 20 6e 65 77 20 6f 6e 65 20 69 66 20 6e  e a new one if n
1c028 65 63 65 73 73 61 72 79 2e 0a 2a 2a 20 49 66 20  ecessary..** If 
1c029 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
1c02a 20 72 65 74 75 72 6e 20 4e 55 4c 4c 20 61 6e 64   return NULL and
1c02b 20 6c 65 61 76 65 20 61 20 6d 65 73 73 61 67 65   leave a message
1c02c 20 69 6e 20 70 50 61 72 73 65 2e 0a 2a 2f 0a 53   in pParse..*/.S
1c02d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64  QLITE_PRIVATE Vd
1c02e 62 65 20 2a 73 71 6c 69 74 65 33 47 65 74 56 64  be *sqlite3GetVd
1c02f 62 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  be(Parse *pParse
1c030 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70  ){.  Vdbe *v = p
1c031 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20  Parse->pVdbe;.  
1c032 69 66 28 20 76 3d 3d 30 20 29 7b 0a 20 20 20 20  if( v==0 ){.    
1c033 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
1c034 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  e = sqlite3VdbeC
1c035 72 65 61 74 65 28 70 50 61 72 73 65 2d 3e 64 62  reate(pParse->db
1c036 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
1c037 76 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d  v;.}.../*.** Com
1c038 70 75 74 65 20 74 68 65 20 69 4c 69 6d 69 74 20  pute the iLimit 
1c039 61 6e 64 20 69 4f 66 66 73 65 74 20 66 69 65 6c  and iOffset fiel
1c03a 64 73 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  ds of the SELECT
1c03b 20 62 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a   based on the.**
1c03c 20 70 4c 69 6d 69 74 20 61 6e 64 20 70 4f 66 66   pLimit and pOff
1c03d 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 73 2e  set expressions.
1c03e 20 20 70 4c 69 6d 69 74 20 61 6e 64 20 70 4f 66    pLimit and pOf
1c03f 66 73 65 74 20 68 6f 6c 64 20 74 68 65 20 65 78  fset hold the ex
1c040 70 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 74 68 61  pressions.** tha
1c041 74 20 61 70 70 65 61 72 20 69 6e 20 74 68 65 20  t appear in the 
1c042 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 73 74 61  original SQL sta
1c043 74 65 6d 65 6e 74 20 61 66 74 65 72 20 74 68 65  tement after the
1c044 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46 53 45   LIMIT and OFFSE
1c045 54 0a 2a 2a 20 6b 65 79 77 6f 72 64 73 2e 20 20  T.** keywords.  
1c046 4f 72 20 4e 55 4c 4c 20 69 66 20 74 68 6f 73 65  Or NULL if those
1c047 20 6b 65 79 77 6f 72 64 73 20 61 72 65 20 6f 6d   keywords are om
1c048 69 74 74 65 64 2e 20 69 4c 69 6d 69 74 20 61 6e  itted. iLimit an
1c049 64 20 69 4f 66 66 73 65 74 20 0a 2a 2a 20 61 72  d iOffset .** ar
1c04a 65 20 74 68 65 20 69 6e 74 65 67 65 72 20 6d 65  e the integer me
1c04b 6d 6f 72 79 20 72 65 67 69 73 74 65 72 20 6e 75  mory register nu
1c04c 6d 62 65 72 73 20 66 6f 72 20 63 6f 75 6e 74 65  mbers for counte
1c04d 72 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 75  rs used to compu
1c04e 74 65 20 0a 2a 2a 20 74 68 65 20 6c 69 6d 69 74  te .** the limit
1c04f 20 61 6e 64 20 6f 66 66 73 65 74 2e 20 20 49 66   and offset.  If
1c050 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6c 69 6d   there is no lim
1c051 69 74 20 61 6e 64 2f 6f 72 20 6f 66 66 73 65 74  it and/or offset
1c052 2c 20 74 68 65 6e 20 0a 2a 2a 20 69 4c 69 6d 69  , then .** iLimi
1c053 74 20 61 6e 64 20 69 4f 66 66 73 65 74 20 61 72  t and iOffset ar
1c054 65 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 0a 2a  e negative..**.*
1c055 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
1c056 68 61 6e 67 65 73 20 74 68 65 20 76 61 6c 75 65  hanges the value
1c057 73 20 6f 66 20 69 4c 69 6d 69 74 20 61 6e 64 20  s of iLimit and 
1c058 69 4f 66 66 73 65 74 20 6f 6e 6c 79 20 69 66 0a  iOffset only if.
1c059 2a 2a 20 61 20 6c 69 6d 69 74 20 6f 72 20 6f 66  ** a limit or of
1c05a 66 73 65 74 20 69 73 20 64 65 66 69 6e 65 64 20  fset is defined 
1c05b 62 79 20 70 4c 69 6d 69 74 20 61 6e 64 20 70 4f  by pLimit and pO
1c05c 66 66 73 65 74 2e 20 20 69 4c 69 6d 69 74 20 61  ffset.  iLimit a
1c05d 6e 64 0a 2a 2a 20 69 4f 66 66 73 65 74 20 73 68  nd.** iOffset sh
1c05e 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e 20 70  ould have been p
1c05f 72 65 73 65 74 20 74 6f 20 61 70 70 72 6f 70 72  reset to appropr
1c060 69 61 74 65 20 64 65 66 61 75 6c 74 20 76 61 6c  iate default val
1c061 75 65 73 0a 2a 2a 20 28 75 73 75 61 6c 6c 79 20  ues.** (usually 
1c062 62 75 74 20 6e 6f 74 20 61 6c 77 61 79 73 20 2d  but not always -
1c063 31 29 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c  1) prior to call
1c064 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ing this routine
1c065 2e 0a 2a 2a 20 4f 6e 6c 79 20 69 66 20 70 4c 69  ..** Only if pLi
1c066 6d 69 74 21 3d 30 20 6f 72 20 70 4f 66 66 73 65  mit!=0 or pOffse
1c067 74 21 3d 30 20 64 6f 20 74 68 65 20 6c 69 6d 69  t!=0 do the limi
1c068 74 20 72 65 67 69 73 74 65 72 73 20 67 65 74 0a  t registers get.
1c069 2a 2a 20 72 65 64 65 66 69 6e 65 64 2e 20 20 54  ** redefined.  T
1c06a 68 65 20 55 4e 49 4f 4e 20 41 4c 4c 20 6f 70 65  he UNION ALL ope
1c06b 72 61 74 6f 72 20 75 73 65 73 20 74 68 69 73 20  rator uses this 
1c06c 70 72 6f 70 65 72 74 79 20 74 6f 20 66 6f 72 63  property to forc
1c06d 65 0a 2a 2a 20 74 68 65 20 72 65 75 73 65 20 6f  e.** the reuse o
1c06e 66 20 74 68 65 20 73 61 6d 65 20 6c 69 6d 69 74  f the same limit
1c06f 20 61 6e 64 20 6f 66 66 73 65 74 20 72 65 67 69   and offset regi
1c070 73 74 65 72 73 20 61 63 72 6f 73 73 20 6d 75 6c  sters across mul
1c071 74 69 70 6c 65 0a 2a 2a 20 53 45 4c 45 43 54 20  tiple.** SELECT 
1c072 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73  statements..*/.s
1c073 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75  tatic void compu
1c074 74 65 4c 69 6d 69 74 52 65 67 69 73 74 65 72 73  teLimitRegisters
1c075 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
1c076 53 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20 69  Select *p, int i
1c077 42 72 65 61 6b 29 7b 0a 20 20 56 64 62 65 20 2a  Break){.  Vdbe *
1c078 76 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 4c 69  v = 0;.  int iLi
1c079 6d 69 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69  mit = 0;.  int i
1c07a 4f 66 66 73 65 74 3b 0a 20 20 69 6e 74 20 61 64  Offset;.  int ad
1c07b 64 72 31 2c 20 61 64 64 72 32 3b 0a 0a 20 20 2f  dr1, addr2;..  /
1c07c 2a 20 0a 20 20 2a 2a 20 22 4c 49 4d 49 54 20 2d  * .  ** "LIMIT -
1c07d 31 22 20 61 6c 77 61 79 73 20 73 68 6f 77 73 20  1" always shows 
1c07e 61 6c 6c 20 72 6f 77 73 2e 20 20 54 68 65 72 65  all rows.  There
1c07f 20 69 73 20 73 6f 6d 65 0a 20 20 2a 2a 20 63 6f   is some.  ** co
1c080 6e 74 72 61 76 65 72 73 79 20 61 62 6f 75 74 20  ntraversy about 
1c081 77 68 61 74 20 74 68 65 20 63 6f 72 72 65 63 74  what the correct
1c082 20 62 65 68 61 76 69 6f 72 20 73 68 6f 75 6c 64   behavior should
1c083 20 62 65 2e 0a 20 20 2a 2a 20 54 68 65 20 63 75   be..  ** The cu
1c084 72 72 65 6e 74 20 69 6d 70 6c 65 6d 65 6e 74 61  rrent implementa
1c085 74 69 6f 6e 20 69 6e 74 65 72 70 72 65 74 73 20  tion interprets 
1c086 22 4c 49 4d 49 54 20 30 22 20 74 6f 20 6d 65 61  "LIMIT 0" to mea
1c087 6e 0a 20 20 2a 2a 20 6e 6f 20 72 6f 77 73 2e 0a  n.  ** no rows..
1c088 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 4c    */.  if( p->pL
1c089 69 6d 69 74 20 29 7b 0a 20 20 20 20 70 2d 3e 69  imit ){.    p->i
1c08a 4c 69 6d 69 74 20 3d 20 69 4c 69 6d 69 74 20 3d  Limit = iLimit =
1c08b 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20   pParse->nMem;. 
1c08c 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20     pParse->nMem 
1c08d 2b 3d 20 32 3b 0a 20 20 20 20 76 20 3d 20 73 71  += 2;.    v = sq
1c08e 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
1c08f 72 73 65 29 3b 0a 20 20 20 20 69 66 28 20 76 3d  rse);.    if( v=
1c090 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20  =0 ) return;.   
1c091 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
1c092 28 70 50 61 72 73 65 2c 20 70 2d 3e 70 4c 69 6d  (pParse, p->pLim
1c093 69 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  it);.    sqlite3
1c094 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1c095 4d 75 73 74 42 65 49 6e 74 2c 20 30 2c 20 30 29  MustBeInt, 0, 0)
1c096 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
1c097 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d  eAddOp(v, OP_Mem
1c098 53 74 6f 72 65 2c 20 69 4c 69 6d 69 74 2c 20 31  Store, iLimit, 1
1c099 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  );.    VdbeComme
1c09a 6e 74 28 28 76 2c 20 22 23 20 4c 49 4d 49 54 20  nt((v, "# LIMIT 
1c09b 63 6f 75 6e 74 65 72 22 29 29 3b 0a 20 20 20 20  counter"));.    
1c09c 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1c09d 28 76 2c 20 4f 50 5f 49 66 4d 65 6d 5a 65 72 6f  (v, OP_IfMemZero
1c09e 2c 20 69 4c 69 6d 69 74 2c 20 69 42 72 65 61 6b  , iLimit, iBreak
1c09f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1c0a0 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65  beAddOp(v, OP_Me
1c0a1 6d 4c 6f 61 64 2c 20 69 4c 69 6d 69 74 2c 20 30  mLoad, iLimit, 0
1c0a2 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e  );.  }.  if( p->
1c0a3 70 4f 66 66 73 65 74 20 29 7b 0a 20 20 20 20 70  pOffset ){.    p
1c0a4 2d 3e 69 4f 66 66 73 65 74 20 3d 20 69 4f 66 66  ->iOffset = iOff
1c0a5 73 65 74 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d  set = pParse->nM
1c0a6 65 6d 2b 2b 3b 0a 20 20 20 20 76 20 3d 20 73 71  em++;.    v = sq
1c0a7 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
1c0a8 72 73 65 29 3b 0a 20 20 20 20 69 66 28 20 76 3d  rse);.    if( v=
1c0a9 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20  =0 ) return;.   
1c0aa 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
1c0ab 28 70 50 61 72 73 65 2c 20 70 2d 3e 70 4f 66 66  (pParse, p->pOff
1c0ac 73 65 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  set);.    sqlite
1c0ad 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1c0ae 5f 4d 75 73 74 42 65 49 6e 74 2c 20 30 2c 20 30  _MustBeInt, 0, 0
1c0af 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1c0b0 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65  beAddOp(v, OP_Me
1c0b1 6d 53 74 6f 72 65 2c 20 69 4f 66 66 73 65 74 2c  mStore, iOffset,
1c0b2 20 70 2d 3e 70 4c 69 6d 69 74 3d 3d 30 29 3b 0a   p->pLimit==0);.
1c0b3 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
1c0b4 28 76 2c 20 22 23 20 4f 46 46 53 45 54 20 63 6f  (v, "# OFFSET co
1c0b5 75 6e 74 65 72 22 29 29 3b 0a 20 20 20 20 61 64  unter"));.    ad
1c0b6 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62  dr1 = sqlite3Vdb
1c0b7 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 66 4d  eAddOp(v, OP_IfM
1c0b8 65 6d 50 6f 73 2c 20 69 4f 66 66 73 65 74 2c 20  emPos, iOffset, 
1c0b9 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0);.    sqlite3V
1c0ba 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 50  dbeAddOp(v, OP_P
1c0bb 6f 70 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 73  op, 1, 0);.    s
1c0bc 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1c0bd 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30  v, OP_Integer, 0
1c0be 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
1c0bf 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
1c0c0 20 61 64 64 72 31 29 3b 0a 20 20 20 20 69 66 28   addr1);.    if(
1c0c1 20 70 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a 20 20   p->pLimit ){.  
1c0c2 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1c0c3 64 64 4f 70 28 76 2c 20 4f 50 5f 41 64 64 2c 20  ddOp(v, OP_Add, 
1c0c4 30 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  0, 0);.    }.  }
1c0c5 0a 20 20 69 66 28 20 70 2d 3e 70 4c 69 6d 69 74  .  if( p->pLimit
1c0c6 20 29 7b 0a 20 20 20 20 61 64 64 72 31 20 3d 20   ){.    addr1 = 
1c0c7 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1c0c8 28 76 2c 20 4f 50 5f 49 66 4d 65 6d 50 6f 73 2c  (v, OP_IfMemPos,
1c0c9 20 69 4c 69 6d 69 74 2c 20 30 29 3b 0a 20 20 20   iLimit, 0);.   
1c0ca 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1c0cb 70 28 76 2c 20 4f 50 5f 50 6f 70 2c 20 31 2c 20  p(v, OP_Pop, 1, 
1c0cc 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0);.    sqlite3V
1c0cd 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d  dbeAddOp(v, OP_M
1c0ce 65 6d 49 6e 74 2c 20 2d 31 2c 20 69 4c 69 6d 69  emInt, -1, iLimi
1c0cf 74 2b 31 29 3b 0a 20 20 20 20 61 64 64 72 32 20  t+1);.    addr2 
1c0d0 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
1c0d1 4f 70 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  Op(v, OP_Goto, 0
1c0d2 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
1c0d3 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
1c0d4 20 61 64 64 72 31 29 3b 0a 20 20 20 20 73 71 6c   addr1);.    sql
1c0d5 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1c0d6 20 4f 50 5f 4d 65 6d 53 74 6f 72 65 2c 20 69 4c   OP_MemStore, iL
1c0d7 69 6d 69 74 2b 31 2c 20 31 29 3b 0a 20 20 20 20  imit+1, 1);.    
1c0d8 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
1c0d9 22 23 20 4c 49 4d 49 54 2b 4f 46 46 53 45 54 22  "# LIMIT+OFFSET"
1c0da 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  ));.    sqlite3V
1c0db 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
1c0dc 64 64 72 32 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ddr2);.  }.}../*
1c0dd 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 76  .** Allocate a v
1c0de 69 72 74 75 61 6c 20 69 6e 64 65 78 20 74 6f 20  irtual index to 
1c0df 75 73 65 20 66 6f 72 20 73 6f 72 74 69 6e 67 2e  use for sorting.
1c0e0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1c0e1 63 72 65 61 74 65 53 6f 72 74 69 6e 67 49 6e 64  createSortingInd
1c0e2 65 78 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ex(Parse *pParse
1c0e3 2c 20 53 65 6c 65 63 74 20 2a 70 2c 20 45 78 70  , Select *p, Exp
1c0e4 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 29  rList *pOrderBy)
1c0e5 7b 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79  {.  if( pOrderBy
1c0e6 20 29 7b 0a 20 20 20 20 69 6e 74 20 61 64 64 72   ){.    int addr
1c0e7 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f  ;.    assert( pO
1c0e8 72 64 65 72 42 79 2d 3e 69 45 43 75 72 73 6f 72  rderBy->iECursor
1c0e9 3d 3d 30 20 29 3b 0a 20 20 20 20 70 4f 72 64 65  ==0 );.    pOrde
1c0ea 72 42 79 2d 3e 69 45 43 75 72 73 6f 72 20 3d 20  rBy->iECursor = 
1c0eb 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a  pParse->nTab++;.
1c0ec 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74      addr = sqlit
1c0ed 65 33 56 64 62 65 41 64 64 4f 70 28 70 50 61 72  e3VdbeAddOp(pPar
1c0ee 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 4f 70  se->pVdbe, OP_Op
1c0ef 65 6e 45 70 68 65 6d 65 72 61 6c 2c 0a 20 20 20  enEphemeral,.   
1c0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c0f1 20 20 20 20 20 20 20 20 20 70 4f 72 64 65 72 42           pOrderB
1c0f2 79 2d 3e 69 45 43 75 72 73 6f 72 2c 20 70 4f 72  y->iECursor, pOr
1c0f3 64 65 72 42 79 2d 3e 6e 45 78 70 72 2b 31 29 3b  derBy->nExpr+1);
1c0f4 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e  .    assert( p->
1c0f5 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 32 5d 20  addrOpenEphm[2] 
1c0f6 3d 3d 20 2d 31 20 29 3b 0a 20 20 20 20 70 2d 3e  == -1 );.    p->
1c0f7 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 32 5d 20  addrOpenEphm[2] 
1c0f8 3d 20 61 64 64 72 3b 0a 20 20 7d 0a 7d 0a 0a 23  = addr;.  }.}..#
1c0f9 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1c0fa 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45  IT_COMPOUND_SELE
1c0fb 43 54 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  CT./*.** Return 
1c0fc 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  the appropriate 
1c0fd 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
1c0fe 63 65 20 66 6f 72 20 74 68 65 20 69 43 6f 6c 2d  ce for the iCol-
1c0ff 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 0a 2a 2a 20  th column of.** 
1c100 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 66  the result set f
1c101 6f 72 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 2d  or the compound-
1c102 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74  select statement
1c103 20 22 70 22 2e 20 20 52 65 74 75 72 6e 20 4e 55   "p".  Return NU
1c104 4c 4c 20 69 66 0a 2a 2a 20 74 68 65 20 63 6f 6c  LL if.** the col
1c105 75 6d 6e 20 68 61 73 20 6e 6f 20 64 65 66 61 75  umn has no defau
1c106 6c 74 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  lt collating seq
1c107 75 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  uence..**.** The
1c108 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
1c109 6e 63 65 20 66 6f 72 20 74 68 65 20 63 6f 6d 70  nce for the comp
1c10a 6f 75 6e 64 20 73 65 6c 65 63 74 20 69 73 20 74  ound select is t
1c10b 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65 0a 2a 2a  aken from the.**
1c10c 20 6c 65 66 74 2d 6d 6f 73 74 20 74 65 72 6d 20   left-most term 
1c10d 6f 66 20 74 68 65 20 73 65 6c 65 63 74 20 74 68  of the select th
1c10e 61 74 20 68 61 73 20 61 20 63 6f 6c 6c 61 74 69  at has a collati
1c10f 6e 67 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2f 0a  ng sequence..*/.
1c110 73 74 61 74 69 63 20 43 6f 6c 6c 53 65 71 20 2a  static CollSeq *
1c111 6d 75 6c 74 69 53 65 6c 65 63 74 43 6f 6c 6c 53  multiSelectCollS
1c112 65 71 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  eq(Parse *pParse
1c113 2c 20 53 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74  , Select *p, int
1c114 20 69 43 6f 6c 29 7b 0a 20 20 43 6f 6c 6c 53 65   iCol){.  CollSe
1c115 71 20 2a 70 52 65 74 3b 0a 20 20 69 66 28 20 70  q *pRet;.  if( p
1c116 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20 20 20  ->pPrior ){.    
1c117 70 52 65 74 20 3d 20 6d 75 6c 74 69 53 65 6c 65  pRet = multiSele
1c118 63 74 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65  ctCollSeq(pParse
1c119 2c 20 70 2d 3e 70 50 72 69 6f 72 2c 20 69 43 6f  , p->pPrior, iCo
1c11a 6c 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  l);.  }else{.   
1c11b 20 70 52 65 74 20 3d 20 30 3b 0a 20 20 7d 0a 20   pRet = 0;.  }. 
1c11c 20 69 66 28 20 70 52 65 74 3d 3d 30 20 29 7b 0a   if( pRet==0 ){.
1c11d 20 20 20 20 70 52 65 74 20 3d 20 73 71 6c 69 74      pRet = sqlit
1c11e 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50  e3ExprCollSeq(pP
1c11f 61 72 73 65 2c 20 70 2d 3e 70 45 4c 69 73 74 2d  arse, p->pEList-
1c120 3e 61 5b 69 43 6f 6c 5d 2e 70 45 78 70 72 29 3b  >a[iCol].pExpr);
1c121 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 52  .  }.  return pR
1c122 65 74 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  et;.}.#endif /* 
1c123 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50  SQLITE_OMIT_COMP
1c124 4f 55 4e 44 5f 53 45 4c 45 43 54 20 2a 2f 0a 0a  OUND_SELECT */..
1c125 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1c126 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c  MIT_COMPOUND_SEL
1c127 45 43 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  ECT./*.** This r
1c128 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
1c129 20 74 6f 20 70 72 6f 63 65 73 73 20 61 20 71 75   to process a qu
1c12a 65 72 79 20 74 68 61 74 20 69 73 20 72 65 61 6c  ery that is real
1c12b 6c 79 20 74 68 65 20 75 6e 69 6f 6e 0a 2a 2a 20  ly the union.** 
1c12c 6f 72 20 69 6e 74 65 72 73 65 63 74 69 6f 6e 20  or intersection 
1c12d 6f 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 73  of two or more s
1c12e 65 70 61 72 61 74 65 20 71 75 65 72 69 65 73 2e  eparate queries.
1c12f 0a 2a 2a 0a 2a 2a 20 22 70 22 20 70 6f 69 6e 74  .**.** "p" point
1c130 73 20 74 6f 20 74 68 65 20 72 69 67 68 74 2d 6d  s to the right-m
1c131 6f 73 74 20 6f 66 20 74 68 65 20 74 77 6f 20 71  ost of the two q
1c132 75 65 72 69 65 73 2e 20 20 74 68 65 20 71 75 65  ueries.  the que
1c133 72 79 20 6f 6e 20 74 68 65 0a 2a 2a 20 6c 65 66  ry on the.** lef
1c134 74 20 69 73 20 70 2d 3e 70 50 72 69 6f 72 2e 20  t is p->pPrior. 
1c135 20 54 68 65 20 6c 65 66 74 20 71 75 65 72 79 20   The left query 
1c136 63 6f 75 6c 64 20 61 6c 73 6f 20 62 65 20 61 20  could also be a 
1c137 63 6f 6d 70 6f 75 6e 64 20 71 75 65 72 79 0a 2a  compound query.*
1c138 2a 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20  * in which case 
1c139 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c  this routine wil
1c13a 6c 20 62 65 20 63 61 6c 6c 65 64 20 72 65 63 75  l be called recu
1c13b 72 73 69 76 65 6c 79 2e 20 0a 2a 2a 0a 2a 2a 20  rsively. .**.** 
1c13c 54 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74  The results of t
1c13d 68 65 20 74 6f 74 61 6c 20 71 75 65 72 79 20 61  he total query a
1c13e 72 65 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e  re to be written
1c13f 20 69 6e 74 6f 20 61 20 64 65 73 74 69 6e 61 74   into a destinat
1c140 69 6f 6e 0a 2a 2a 20 6f 66 20 74 79 70 65 20 65  ion.** of type e
1c141 44 65 73 74 20 77 69 74 68 20 70 61 72 61 6d 65  Dest with parame
1c142 74 65 72 20 69 50 61 72 6d 2e 0a 2a 2a 0a 2a 2a  ter iParm..**.**
1c143 20 45 78 61 6d 70 6c 65 20 31 3a 20 20 43 6f 6e   Example 1:  Con
1c144 73 69 64 65 72 20 61 20 74 68 72 65 65 2d 77 61  sider a three-wa
1c145 79 20 63 6f 6d 70 6f 75 6e 64 20 53 51 4c 20 73  y compound SQL s
1c146 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  tatement..**.** 
1c147 20 20 20 20 53 45 4c 45 43 54 20 61 20 46 52 4f      SELECT a FRO
1c148 4d 20 74 31 20 55 4e 49 4f 4e 20 53 45 4c 45 43  M t1 UNION SELEC
1c149 54 20 62 20 46 52 4f 4d 20 74 32 20 55 4e 49 4f  T b FROM t2 UNIO
1c14a 4e 20 53 45 4c 45 43 54 20 63 20 46 52 4f 4d 20  N SELECT c FROM 
1c14b 74 33 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74  t3.**.** This st
1c14c 61 74 65 6d 65 6e 74 20 69 73 20 70 61 72 73 65  atement is parse
1c14d 64 20 75 70 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  d up as follows:
1c14e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43  .**.**     SELEC
1c14f 54 20 63 20 46 52 4f 4d 20 74 33 0a 2a 2a 20 20  T c FROM t3.**  
1c150 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20 20 60 2d      |.**      `-
1c151 2d 2d 2d 2d 3e 20 20 53 45 4c 45 43 54 20 62 20  ---->  SELECT b 
1c152 46 52 4f 4d 20 74 32 0a 2a 2a 20 20 20 20 20 20  FROM t2.**      
1c153 20 20 20 20 20 20 20 20 20 20 7c 0a 2a 2a 20 20            |.**  
1c154 20 20 20 20 20 20 20 20 20 20 20 20 20 20 60 2d                `-
1c155 2d 2d 2d 2d 2d 3e 20 20 53 45 4c 45 43 54 20 61  ----->  SELECT a
1c156 20 46 52 4f 4d 20 74 31 0a 2a 2a 0a 2a 2a 20 54   FROM t1.**.** T
1c157 68 65 20 61 72 72 6f 77 73 20 69 6e 20 74 68 65  he arrows in the
1c158 20 64 69 61 67 72 61 6d 20 61 62 6f 76 65 20 72   diagram above r
1c159 65 70 72 65 73 65 6e 74 20 74 68 65 20 53 65 6c  epresent the Sel
1c15a 65 63 74 2e 70 50 72 69 6f 72 20 70 6f 69 6e 74  ect.pPrior point
1c15b 65 72 2e 0a 2a 2a 20 53 6f 20 69 66 20 74 68 69  er..** So if thi
1c15c 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
1c15d 6c 65 64 20 77 69 74 68 20 70 20 65 71 75 61 6c  led with p equal
1c15e 20 74 6f 20 74 68 65 20 74 33 20 71 75 65 72 79   to the t3 query
1c15f 2c 20 74 68 65 6e 0a 2a 2a 20 70 50 72 69 6f 72  , then.** pPrior
1c160 20 77 69 6c 6c 20 62 65 20 74 68 65 20 74 32 20   will be the t2 
1c161 71 75 65 72 79 2e 20 20 70 2d 3e 6f 70 20 77 69  query.  p->op wi
1c162 6c 6c 20 62 65 20 54 4b 5f 55 4e 49 4f 4e 20 69  ll be TK_UNION i
1c163 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a  n this case..**.
1c164 2a 2a 20 4e 6f 74 69 63 65 20 74 68 61 74 20 62  ** Notice that b
1c165 65 63 61 75 73 65 20 6f 66 20 74 68 65 20 77 61  ecause of the wa
1c166 79 20 53 51 4c 69 74 65 20 70 61 72 73 65 73 20  y SQLite parses 
1c167 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 73  compound SELECTs
1c168 2c 20 74 68 65 0a 2a 2a 20 69 6e 64 69 76 69 64  , the.** individ
1c169 75 61 6c 20 73 65 6c 65 63 74 73 20 61 6c 77 61  ual selects alwa
1c16a 79 73 20 67 72 6f 75 70 20 66 72 6f 6d 20 6c 65  ys group from le
1c16b 66 74 20 74 6f 20 72 69 67 68 74 2e 0a 2a 2f 0a  ft to right..*/.
1c16c 73 74 61 74 69 63 20 69 6e 74 20 6d 75 6c 74 69  static int multi
1c16d 53 65 6c 65 63 74 28 0a 20 20 50 61 72 73 65 20  Select(.  Parse 
1c16e 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
1c16f 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
1c170 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  xt */.  Select *
1c171 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  p,            /*
1c172 20 54 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20   The right-most 
1c173 6f 66 20 53 45 4c 45 43 54 73 20 74 6f 20 62 65  of SELECTs to be
1c174 20 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20   coded */.  int 
1c175 65 44 65 73 74 2c 20 20 20 20 20 20 20 20 20 20  eDest,          
1c176 20 20 2f 2a 20 5c 5f 5f 5f 20 20 53 74 6f 72 65    /* \___  Store
1c177 20 71 75 65 72 79 20 72 65 73 75 6c 74 73 20 61   query results a
1c178 73 20 73 70 65 63 69 66 69 65 64 20 2a 2f 0a 20  s specified */. 
1c179 20 69 6e 74 20 69 50 61 72 6d 2c 20 20 20 20 20   int iParm,     
1c17a 20 20 20 20 20 20 20 2f 2a 20 2f 20 20 20 20 20         /* /     
1c17b 62 79 20 74 68 65 73 65 20 74 77 6f 20 70 61 72  by these two par
1c17c 61 6d 65 74 65 72 73 2e 20 20 20 20 20 20 20 20  ameters.        
1c17d 20 2a 2f 0a 20 20 63 68 61 72 20 2a 61 66 66 20   */.  char *aff 
1c17e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
1c17f 66 20 65 44 65 73 74 20 69 73 20 53 52 54 5f 55  f eDest is SRT_U
1c180 6e 69 6f 6e 2c 20 74 68 65 20 61 66 66 69 6e 69  nion, the affini
1c181 74 79 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a  ty string */.){.
1c182 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
1c183 45 5f 4f 4b 3b 20 20 20 2f 2a 20 53 75 63 63 65  E_OK;   /* Succe
1c184 73 73 20 63 6f 64 65 20 66 72 6f 6d 20 61 20 73  ss code from a s
1c185 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 53  ubroutine */.  S
1c186 65 6c 65 63 74 20 2a 70 50 72 69 6f 72 3b 20 20  elect *pPrior;  
1c187 20 20 20 20 20 2f 2a 20 41 6e 6f 74 68 65 72 20       /* Another 
1c188 53 45 4c 45 43 54 20 69 6d 6d 65 64 69 61 74 65  SELECT immediate
1c189 6c 79 20 74 6f 20 6f 75 72 20 6c 65 66 74 20 2a  ly to our left *
1c18a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20  /.  Vdbe *v;    
1c18b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47 65 6e            /* Gen
1c18c 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 74 68  erate code to th
1c18d 69 73 20 56 44 42 45 20 2a 2f 0a 20 20 69 6e 74  is VDBE */.  int
1c18e 20 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20   nCol;          
1c18f 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1c190 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72  columns in the r
1c191 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 45  esult set */.  E
1c192 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42  xprList *pOrderB
1c193 79 3b 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45  y;   /* The ORDE
1c194 52 20 42 59 20 63 6c 61 75 73 65 20 6f 6e 20 70  R BY clause on p
1c195 20 2a 2f 0a 20 20 69 6e 74 20 61 53 65 74 50 32   */.  int aSetP2
1c196 5b 32 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 53  [2];        /* S
1c197 65 74 20 50 32 20 76 61 6c 75 65 20 6f 66 20 74  et P2 value of t
1c198 68 65 73 65 20 6f 70 20 74 6f 20 6e 75 6d 62 65  hese op to numbe
1c199 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a  r of columns */.
1c19a 20 20 69 6e 74 20 6e 53 65 74 50 32 20 3d 20 30    int nSetP2 = 0
1c19b 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  ;       /* Numbe
1c19c 72 20 6f 66 20 73 6c 6f 74 73 20 69 6e 20 61 53  r of slots in aS
1c19d 65 74 50 32 5b 5d 20 75 73 65 64 20 2a 2f 0a 0a  etP2[] used */..
1c19e 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74    /* Make sure t
1c19f 68 65 72 65 20 69 73 20 6e 6f 20 4f 52 44 45 52  here is no ORDER
1c1a0 20 42 59 20 6f 72 20 4c 49 4d 49 54 20 63 6c 61   BY or LIMIT cla
1c1a1 75 73 65 20 6f 6e 20 70 72 69 6f 72 20 53 45 4c  use on prior SEL
1c1a2 45 43 54 73 2e 20 20 4f 6e 6c 79 0a 20 20 2a 2a  ECTs.  Only.  **
1c1a3 20 74 68 65 20 6c 61 73 74 20 28 72 69 67 68 74   the last (right
1c1a4 2d 6d 6f 73 74 29 20 53 45 4c 45 43 54 20 69 6e  -most) SELECT in
1c1a5 20 74 68 65 20 73 65 72 69 65 73 20 6d 61 79 20   the series may 
1c1a6 68 61 76 65 20 61 6e 20 4f 52 44 45 52 20 42 59  have an ORDER BY
1c1a7 20 6f 72 20 4c 49 4d 49 54 2e 0a 20 20 2a 2f 0a   or LIMIT..  */.
1c1a8 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 70 2d    if( p==0 || p-
1c1a9 3e 70 50 72 69 6f 72 3d 3d 30 20 29 7b 0a 20 20  >pPrior==0 ){.  
1c1aa 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 67 6f    rc = 1;.    go
1c1ab 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f  to multi_select_
1c1ac 65 6e 64 3b 0a 20 20 7d 0a 20 20 70 50 72 69 6f  end;.  }.  pPrio
1c1ad 72 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20  r = p->pPrior;. 
1c1ae 20 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 2d   assert( pPrior-
1c1af 3e 70 52 69 67 68 74 6d 6f 73 74 21 3d 70 50 72  >pRightmost!=pPr
1c1b0 69 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ior );.  assert(
1c1b1 20 70 50 72 69 6f 72 2d 3e 70 52 69 67 68 74 6d   pPrior->pRightm
1c1b2 6f 73 74 3d 3d 70 2d 3e 70 52 69 67 68 74 6d 6f  ost==p->pRightmo
1c1b3 73 74 20 29 3b 0a 20 20 69 66 28 20 70 50 72 69  st );.  if( pPri
1c1b4 6f 72 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a  or->pOrderBy ){.
1c1b5 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1c1b6 4d 73 67 28 70 50 61 72 73 65 2c 22 4f 52 44 45  Msg(pParse,"ORDE
1c1b7 52 20 42 59 20 63 6c 61 75 73 65 20 73 68 6f 75  R BY clause shou
1c1b8 6c 64 20 63 6f 6d 65 20 61 66 74 65 72 20 25 73  ld come after %s
1c1b9 20 6e 6f 74 20 62 65 66 6f 72 65 22 2c 0a 20 20   not before",.  
1c1ba 20 20 20 20 73 65 6c 65 63 74 4f 70 4e 61 6d 65      selectOpName
1c1bb 28 70 2d 3e 6f 70 29 29 3b 0a 20 20 20 20 72 63  (p->op));.    rc
1c1bc 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74 6f 20 6d   = 1;.    goto m
1c1bd 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b  ulti_select_end;
1c1be 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 72 69 6f  .  }.  if( pPrio
1c1bf 72 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a 20 20 20  r->pLimit ){.   
1c1c0 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
1c1c1 28 70 50 61 72 73 65 2c 22 4c 49 4d 49 54 20 63  (pParse,"LIMIT c
1c1c2 6c 61 75 73 65 20 73 68 6f 75 6c 64 20 63 6f 6d  lause should com
1c1c3 65 20 61 66 74 65 72 20 25 73 20 6e 6f 74 20 62  e after %s not b
1c1c4 65 66 6f 72 65 22 2c 0a 20 20 20 20 20 20 73 65  efore",.      se
1c1c5 6c 65 63 74 4f 70 4e 61 6d 65 28 70 2d 3e 6f 70  lectOpName(p->op
1c1c6 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a  ));.    rc = 1;.
1c1c7 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73      goto multi_s
1c1c8 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a 0a  elect_end;.  }..
1c1c9 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 77    /* Make sure w
1c1ca 65 20 68 61 76 65 20 61 20 76 61 6c 69 64 20 71  e have a valid q
1c1cb 75 65 72 79 20 65 6e 67 69 6e 65 2e 20 20 49 66  uery engine.  If
1c1cc 20 6e 6f 74 2c 20 63 72 65 61 74 65 20 61 20 6e   not, create a n
1c1cd 65 77 20 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 76  ew one..  */.  v
1c1ce 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
1c1cf 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28  e(pParse);.  if(
1c1d0 20 76 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20   v==0 ){.    rc 
1c1d1 3d 20 31 3b 0a 20 20 20 20 67 6f 74 6f 20 6d 75  = 1;.    goto mu
1c1d2 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a  lti_select_end;.
1c1d3 20 20 7d 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65    }..  /* Create
1c1d4 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
1c1d5 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
1c1d6 20 69 66 20 6e 65 63 65 73 73 61 72 79 0a 20 20   if necessary.  
1c1d7 2a 2f 0a 20 20 69 66 28 20 65 44 65 73 74 3d 3d  */.  if( eDest==
1c1d8 53 52 54 5f 45 70 68 65 6d 54 61 62 20 29 7b 0a  SRT_EphemTab ){.
1c1d9 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70      assert( p->p
1c1da 45 4c 69 73 74 20 29 3b 0a 20 20 20 20 61 73 73  EList );.    ass
1c1db 65 72 74 28 20 6e 53 65 74 50 32 3c 73 69 7a 65  ert( nSetP2<size
1c1dc 6f 66 28 61 53 65 74 50 32 29 2f 73 69 7a 65 6f  of(aSetP2)/sizeo
1c1dd 66 28 61 53 65 74 50 32 5b 30 5d 29 20 29 3b 0a  f(aSetP2[0]) );.
1c1de 20 20 20 20 61 53 65 74 50 32 5b 6e 53 65 74 50      aSetP2[nSetP
1c1df 32 2b 2b 5d 20 3d 20 73 71 6c 69 74 65 33 56 64  2++] = sqlite3Vd
1c1e0 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4f 70  beAddOp(v, OP_Op
1c1e1 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 69 50 61  enEphemeral, iPa
1c1e2 72 6d 2c 20 30 29 3b 0a 20 20 20 20 65 44 65 73  rm, 0);.    eDes
1c1e3 74 20 3d 20 53 52 54 5f 54 61 62 6c 65 3b 0a 20  t = SRT_Table;. 
1c1e4 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74   }..  /* Generat
1c1e5 65 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 6c  e code for the l
1c1e6 65 66 74 20 61 6e 64 20 72 69 67 68 74 20 53 45  eft and right SE
1c1e7 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 2e  LECT statements.
1c1e8 0a 20 20 2a 2f 0a 20 20 70 4f 72 64 65 72 42 79  .  */.  pOrderBy
1c1e9 20 3d 20 70 2d 3e 70 4f 72 64 65 72 42 79 3b 0a   = p->pOrderBy;.
1c1ea 20 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70 20    switch( p->op 
1c1eb 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41  ){.    case TK_A
1c1ec 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20  LL: {.      if( 
1c1ed 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 7b 0a 20  pOrderBy==0 ){. 
1c1ee 20 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 20         int addr 
1c1ef 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 61 73 73  = 0;.        ass
1c1f0 65 72 74 28 20 21 70 50 72 69 6f 72 2d 3e 70 4c  ert( !pPrior->pL
1c1f1 69 6d 69 74 20 29 3b 0a 20 20 20 20 20 20 20 20  imit );.        
1c1f2 70 50 72 69 6f 72 2d 3e 70 4c 69 6d 69 74 20 3d  pPrior->pLimit =
1c1f3 20 70 2d 3e 70 4c 69 6d 69 74 3b 0a 20 20 20 20   p->pLimit;.    
1c1f4 20 20 20 20 70 50 72 69 6f 72 2d 3e 70 4f 66 66      pPrior->pOff
1c1f5 73 65 74 20 3d 20 70 2d 3e 70 4f 66 66 73 65 74  set = p->pOffset
1c1f6 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  ;.        rc = s
1c1f7 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61  qlite3Select(pPa
1c1f8 72 73 65 2c 20 70 50 72 69 6f 72 2c 20 65 44 65  rse, pPrior, eDe
1c1f9 73 74 2c 20 69 50 61 72 6d 2c 20 30 2c 20 30 2c  st, iParm, 0, 0,
1c1fa 20 30 2c 20 61 66 66 29 3b 0a 20 20 20 20 20 20   0, aff);.      
1c1fb 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 30 3b    p->pLimit = 0;
1c1fc 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 4f 66 66  .        p->pOff
1c1fd 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  set = 0;.       
1c1fe 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20   if( rc ){.     
1c1ff 20 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f       goto multi_
1c200 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20  select_end;.    
1c201 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 2d      }.        p-
1c202 3e 70 50 72 69 6f 72 20 3d 20 30 3b 0a 20 20 20  >pPrior = 0;.   
1c203 20 20 20 20 20 70 2d 3e 69 4c 69 6d 69 74 20 3d       p->iLimit =
1c204 20 70 50 72 69 6f 72 2d 3e 69 4c 69 6d 69 74 3b   pPrior->iLimit;
1c205 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4f 66 66  .        p->iOff
1c206 73 65 74 20 3d 20 70 50 72 69 6f 72 2d 3e 69 4f  set = pPrior->iO
1c207 66 66 73 65 74 3b 0a 20 20 20 20 20 20 20 20 69  ffset;.        i
1c208 66 28 20 70 2d 3e 69 4c 69 6d 69 74 3e 3d 30 20  f( p->iLimit>=0 
1c209 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 64 64  ){.          add
1c20a 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
1c20b 64 64 4f 70 28 76 2c 20 4f 50 5f 49 66 4d 65 6d  ddOp(v, OP_IfMem
1c20c 5a 65 72 6f 2c 20 70 2d 3e 69 4c 69 6d 69 74 2c  Zero, p->iLimit,
1c20d 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 56   0);.          V
1c20e 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
1c20f 23 20 4a 75 6d 70 20 61 68 65 61 64 20 69 66 20  # Jump ahead if 
1c210 4c 49 4d 49 54 20 72 65 61 63 68 65 64 22 29 29  LIMIT reached"))
1c211 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1c212 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1c213 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
1c214 2c 20 65 44 65 73 74 2c 20 69 50 61 72 6d 2c 20  , eDest, iParm, 
1c215 30 2c 20 30 2c 20 30 2c 20 61 66 66 29 3b 0a 20  0, 0, 0, aff);. 
1c216 20 20 20 20 20 20 20 70 2d 3e 70 50 72 69 6f 72         p->pPrior
1c217 20 3d 20 70 50 72 69 6f 72 3b 0a 20 20 20 20 20   = pPrior;.     
1c218 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20     if( rc ){.   
1c219 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74         goto mult
1c21a 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20  i_select_end;.  
1c21b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1c21c 69 66 28 20 61 64 64 72 20 29 7b 0a 20 20 20 20  if( addr ){.    
1c21d 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1c21e 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
1c21f 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  r);.        }.  
1c220 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1c221 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 46 6f     }.      /* Fo
1c222 72 20 55 4e 49 4f 4e 20 41 4c 4c 20 2e 2e 2e 20  r UNION ALL ... 
1c223 4f 52 44 45 52 20 42 59 20 66 61 6c 6c 20 74 68  ORDER BY fall th
1c224 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78  rough to the nex
1c225 74 20 63 61 73 65 20 2a 2f 0a 20 20 20 20 7d 0a  t case */.    }.
1c226 20 20 20 20 63 61 73 65 20 54 4b 5f 45 58 43 45      case TK_EXCE
1c227 50 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  PT:.    case TK_
1c228 55 4e 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 69  UNION: {.      i
1c229 6e 74 20 75 6e 69 6f 6e 54 61 62 3b 20 20 20 20  nt unionTab;    
1c22a 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72  /* Cursor number
1c22b 20 6f 66 20 74 68 65 20 74 65 6d 70 6f 72 61 72   of the temporar
1c22c 79 20 74 61 62 6c 65 20 68 6f 6c 64 69 6e 67 20  y table holding 
1c22d 72 65 73 75 6c 74 20 2a 2f 0a 20 20 20 20 20 20  result */.      
1c22e 69 6e 74 20 6f 70 20 3d 20 30 3b 20 20 20 20 20  int op = 0;     
1c22f 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 53   /* One of the S
1c230 52 54 5f 20 6f 70 65 72 61 74 69 6f 6e 73 20 74  RT_ operations t
1c231 6f 20 61 70 70 6c 79 20 74 6f 20 73 65 6c 66 20  o apply to self 
1c232 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 70 72 69  */.      int pri
1c233 6f 72 4f 70 3b 20 20 20 20 20 2f 2a 20 54 68 65  orOp;     /* The
1c234 20 53 52 54 5f 20 6f 70 65 72 61 74 69 6f 6e 20   SRT_ operation 
1c235 74 6f 20 61 70 70 6c 79 20 74 6f 20 70 72 69 6f  to apply to prio
1c236 72 20 73 65 6c 65 63 74 73 20 2a 2f 0a 20 20 20  r selects */.   
1c237 20 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 2c     Expr *pLimit,
1c238 20 2a 70 4f 66 66 73 65 74 3b 20 2f 2a 20 53 61   *pOffset; /* Sa
1c239 76 65 64 20 76 61 6c 75 65 73 20 6f 66 20 70 2d  ved values of p-
1c23a 3e 6e 4c 69 6d 69 74 20 61 6e 64 20 70 2d 3e 6e  >nLimit and p->n
1c23b 4f 66 66 73 65 74 20 2a 2f 0a 20 20 20 20 20 20  Offset */.      
1c23c 69 6e 74 20 61 64 64 72 3b 0a 0a 20 20 20 20 20  int addr;..     
1c23d 20 70 72 69 6f 72 4f 70 20 3d 20 70 2d 3e 6f 70   priorOp = p->op
1c23e 3d 3d 54 4b 5f 41 4c 4c 20 3f 20 53 52 54 5f 54  ==TK_ALL ? SRT_T
1c23f 61 62 6c 65 20 3a 20 53 52 54 5f 55 6e 69 6f 6e  able : SRT_Union
1c240 3b 0a 20 20 20 20 20 20 69 66 28 20 65 44 65 73  ;.      if( eDes
1c241 74 3d 3d 70 72 69 6f 72 4f 70 20 26 26 20 70 4f  t==priorOp && pO
1c242 72 64 65 72 42 79 3d 3d 30 20 26 26 20 21 70 2d  rderBy==0 && !p-
1c243 3e 70 4c 69 6d 69 74 20 26 26 20 21 70 2d 3e 70  >pLimit && !p->p
1c244 4f 66 66 73 65 74 20 29 7b 0a 20 20 20 20 20 20  Offset ){.      
1c245 20 20 2f 2a 20 57 65 20 63 61 6e 20 72 65 75 73    /* We can reus
1c246 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61  e a temporary ta
1c247 62 6c 65 20 67 65 6e 65 72 61 74 65 64 20 62 79  ble generated by
1c248 20 61 20 53 45 4c 45 43 54 20 74 6f 20 6f 75 72   a SELECT to our
1c249 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 69 67 68  .        ** righ
1c24a 74 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  t..        */.  
1c24b 20 20 20 20 20 20 75 6e 69 6f 6e 54 61 62 20 3d        unionTab =
1c24c 20 69 50 61 72 6d 3b 0a 20 20 20 20 20 20 7d 65   iParm;.      }e
1c24d 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  lse{.        /* 
1c24e 57 65 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f 20  We will need to 
1c24f 63 72 65 61 74 65 20 6f 75 72 20 6f 77 6e 20 74  create our own t
1c250 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 74  emporary table t
1c251 6f 20 68 6f 6c 64 20 74 68 65 0a 20 20 20 20 20  o hold the.     
1c252 20 20 20 2a 2a 20 69 6e 74 65 72 6d 65 64 69 61     ** intermedia
1c253 74 65 20 72 65 73 75 6c 74 73 2e 0a 20 20 20 20  te results..    
1c254 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 75      */.        u
1c255 6e 69 6f 6e 54 61 62 20 3d 20 70 50 61 72 73 65  nionTab = pParse
1c256 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20  ->nTab++;.      
1c257 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 20 26    if( pOrderBy &
1c258 26 20 6d 61 74 63 68 4f 72 64 65 72 62 79 54 6f  & matchOrderbyTo
1c259 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20 70  Column(pParse, p
1c25a 2c 20 70 4f 72 64 65 72 42 79 2c 20 75 6e 69 6f  , pOrderBy, unio
1c25b 6e 54 61 62 2c 31 29 20 29 7b 0a 20 20 20 20 20  nTab,1) ){.     
1c25c 20 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20       rc = 1;.   
1c25d 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74         goto mult
1c25e 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20  i_select_end;.  
1c25f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1c260 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
1c261 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4f 70  beAddOp(v, OP_Op
1c262 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 75 6e 69  enEphemeral, uni
1c263 6f 6e 54 61 62 2c 20 30 29 3b 0a 20 20 20 20 20  onTab, 0);.     
1c264 20 20 20 69 66 28 20 70 72 69 6f 72 4f 70 3d 3d     if( priorOp==
1c265 53 52 54 5f 54 61 62 6c 65 20 29 7b 0a 20 20 20  SRT_Table ){.   
1c266 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e         assert( n
1c267 53 65 74 50 32 3c 73 69 7a 65 6f 66 28 61 53 65  SetP2<sizeof(aSe
1c268 74 50 32 29 2f 73 69 7a 65 6f 66 28 61 53 65 74  tP2)/sizeof(aSet
1c269 50 32 5b 30 5d 29 20 29 3b 0a 20 20 20 20 20 20  P2[0]) );.      
1c26a 20 20 20 20 61 53 65 74 50 32 5b 6e 53 65 74 50      aSetP2[nSetP
1c26b 32 2b 2b 5d 20 3d 20 61 64 64 72 3b 0a 20 20 20  2++] = addr;.   
1c26c 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1c26d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
1c26e 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d  >addrOpenEphm[0]
1c26f 20 3d 3d 20 2d 31 20 29 3b 0a 20 20 20 20 20 20   == -1 );.      
1c270 20 20 20 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45      p->addrOpenE
1c271 70 68 6d 5b 30 5d 20 3d 20 61 64 64 72 3b 0a 20  phm[0] = addr;. 
1c272 20 20 20 20 20 20 20 20 20 70 2d 3e 70 52 69 67           p->pRig
1c273 68 74 6d 6f 73 74 2d 3e 75 73 65 73 45 70 68 6d  htmost->usesEphm
1c274 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = 1;.        }.
1c275 20 20 20 20 20 20 20 20 63 72 65 61 74 65 53 6f          createSo
1c276 72 74 69 6e 67 49 6e 64 65 78 28 70 50 61 72 73  rtingIndex(pPars
1c277 65 2c 20 70 2c 20 70 4f 72 64 65 72 42 79 29 3b  e, p, pOrderBy);
1c278 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1c279 20 70 2d 3e 70 45 4c 69 73 74 20 29 3b 0a 20 20   p->pEList );.  
1c27a 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20      }..      /* 
1c27b 43 6f 64 65 20 74 68 65 20 53 45 4c 45 43 54 20  Code the SELECT 
1c27c 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 6f 75  statements to ou
1c27d 72 20 6c 65 66 74 0a 20 20 20 20 20 20 2a 2f 0a  r left.      */.
1c27e 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70        assert( !p
1c27f 50 72 69 6f 72 2d 3e 70 4f 72 64 65 72 42 79 20  Prior->pOrderBy 
1c280 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  );.      rc = sq
1c281 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72  lite3Select(pPar
1c282 73 65 2c 20 70 50 72 69 6f 72 2c 20 70 72 69 6f  se, pPrior, prio
1c283 72 4f 70 2c 20 75 6e 69 6f 6e 54 61 62 2c 20 30  rOp, unionTab, 0
1c284 2c 20 30 2c 20 30 2c 20 61 66 66 29 3b 0a 20 20  , 0, 0, aff);.  
1c285 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20      if( rc ){.  
1c286 20 20 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74 69        goto multi
1c287 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20  _select_end;.   
1c288 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 43     }..      /* C
1c289 6f 64 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ode the current 
1c28a 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
1c28b 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
1c28c 73 77 69 74 63 68 28 20 70 2d 3e 6f 70 20 29 7b  switch( p->op ){
1c28d 0a 20 20 20 20 20 20 20 20 20 63 61 73 65 20 54  .         case T
1c28e 4b 5f 45 58 43 45 50 54 3a 20 20 6f 70 20 3d 20  K_EXCEPT:  op = 
1c28f 53 52 54 5f 45 78 63 65 70 74 3b 20 20 20 62 72  SRT_Except;   br
1c290 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 63 61  eak;.         ca
1c291 73 65 20 54 4b 5f 55 4e 49 4f 4e 3a 20 20 20 6f  se TK_UNION:   o
1c292 70 20 3d 20 53 52 54 5f 55 6e 69 6f 6e 3b 20 20  p = SRT_Union;  
1c293 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
1c294 20 20 63 61 73 65 20 54 4b 5f 41 4c 4c 3a 20 20    case TK_ALL:  
1c295 20 20 20 6f 70 20 3d 20 53 52 54 5f 54 61 62 6c     op = SRT_Tabl
1c296 65 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  e;    break;.   
1c297 20 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e 70 50     }.      p->pP
1c298 72 69 6f 72 20 3d 20 30 3b 0a 20 20 20 20 20 20  rior = 0;.      
1c299 70 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 30 3b  p->pOrderBy = 0;
1c29a 0a 20 20 20 20 20 20 70 2d 3e 64 69 73 61 6c 6c  .      p->disall
1c29b 6f 77 4f 72 64 65 72 42 79 20 3d 20 70 4f 72 64  owOrderBy = pOrd
1c29c 65 72 42 79 21 3d 30 3b 0a 20 20 20 20 20 20 70  erBy!=0;.      p
1c29d 4c 69 6d 69 74 20 3d 20 70 2d 3e 70 4c 69 6d 69  Limit = p->pLimi
1c29e 74 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d  t;.      p->pLim
1c29f 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4f  it = 0;.      pO
1c2a0 66 66 73 65 74 20 3d 20 70 2d 3e 70 4f 66 66 73  ffset = p->pOffs
1c2a1 65 74 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4f 66  et;.      p->pOf
1c2a2 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  fset = 0;.      
1c2a3 72 63 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  rc = sqlite3Sele
1c2a4 63 74 28 70 50 61 72 73 65 2c 20 70 2c 20 6f 70  ct(pParse, p, op
1c2a5 2c 20 75 6e 69 6f 6e 54 61 62 2c 20 30 2c 20 30  , unionTab, 0, 0
1c2a6 2c 20 30 2c 20 61 66 66 29 3b 0a 20 20 20 20 20  , 0, aff);.     
1c2a7 20 2f 2a 20 51 75 65 72 79 20 66 6c 61 74 74 65   /* Query flatte
1c2a8 6e 69 6e 67 20 69 6e 20 73 71 6c 69 74 65 33 53  ning in sqlite3S
1c2a9 65 6c 65 63 74 28 29 20 6d 69 67 68 74 20 72 65  elect() might re
1c2aa 66 69 6c 6c 20 70 2d 3e 70 4f 72 64 65 72 42 79  fill p->pOrderBy
1c2ab 2e 0a 20 20 20 20 20 20 2a 2a 20 42 65 20 73 75  ..      ** Be su
1c2ac 72 65 20 74 6f 20 64 65 6c 65 74 65 20 70 2d 3e  re to delete p->
1c2ad 70 4f 72 64 65 72 42 79 2c 20 74 68 65 72 65 66  pOrderBy, theref
1c2ae 6f 72 65 2c 20 74 6f 20 61 76 6f 69 64 20 61 20  ore, to avoid a 
1c2af 6d 65 6d 6f 72 79 20 6c 65 61 6b 2e 20 2a 2f 0a  memory leak. */.
1c2b0 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
1c2b1 72 4c 69 73 74 44 65 6c 65 74 65 28 70 2d 3e 70  rListDelete(p->p
1c2b2 4f 72 64 65 72 42 79 29 3b 0a 20 20 20 20 20 20  OrderBy);.      
1c2b3 70 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50 72 69  p->pPrior = pPri
1c2b4 6f 72 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4f 72  or;.      p->pOr
1c2b5 64 65 72 42 79 20 3d 20 70 4f 72 64 65 72 42 79  derBy = pOrderBy
1c2b6 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
1c2b7 78 70 72 44 65 6c 65 74 65 28 70 2d 3e 70 4c 69  xprDelete(p->pLi
1c2b8 6d 69 74 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70  mit);.      p->p
1c2b9 4c 69 6d 69 74 20 3d 20 70 4c 69 6d 69 74 3b 0a  Limit = pLimit;.
1c2ba 20 20 20 20 20 20 70 2d 3e 70 4f 66 66 73 65 74        p->pOffset
1c2bb 20 3d 20 70 4f 66 66 73 65 74 3b 0a 20 20 20 20   = pOffset;.    
1c2bc 20 20 70 2d 3e 69 4c 69 6d 69 74 20 3d 20 2d 31    p->iLimit = -1
1c2bd 3b 0a 20 20 20 20 20 20 70 2d 3e 69 4f 66 66 73  ;.      p->iOffs
1c2be 65 74 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 69  et = -1;.      i
1c2bf 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20  f( rc ){.       
1c2c0 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65   goto multi_sele
1c2c1 63 74 5f 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a  ct_end;.      }.
1c2c2 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65  ..      /* Conve
1c2c3 72 74 20 74 68 65 20 64 61 74 61 20 69 6e 20 74  rt the data in t
1c2c4 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  he temporary tab
1c2c5 6c 65 20 69 6e 74 6f 20 77 68 61 74 65 76 65 72  le into whatever
1c2c6 20 66 6f 72 6d 0a 20 20 20 20 20 20 2a 2a 20 69   form.      ** i
1c2c7 74 20 69 73 20 74 68 61 74 20 77 65 20 63 75 72  t is that we cur
1c2c8 72 65 6e 74 6c 79 20 6e 65 65 64 2e 0a 20 20 20  rently need..   
1c2c9 20 20 20 2a 2f 20 20 20 20 20 20 0a 20 20 20 20     */      .    
1c2ca 20 20 69 66 28 20 65 44 65 73 74 21 3d 70 72 69    if( eDest!=pri
1c2cb 6f 72 4f 70 20 7c 7c 20 75 6e 69 6f 6e 54 61 62  orOp || unionTab
1c2cc 21 3d 69 50 61 72 6d 20 29 7b 0a 20 20 20 20 20  !=iParm ){.     
1c2cd 20 20 20 69 6e 74 20 69 43 6f 6e 74 2c 20 69 42     int iCont, iB
1c2ce 72 65 61 6b 2c 20 69 53 74 61 72 74 3b 0a 20 20  reak, iStart;.  
1c2cf 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
1c2d0 3e 70 45 4c 69 73 74 20 29 3b 0a 20 20 20 20 20  >pEList );.     
1c2d1 20 20 20 69 66 28 20 65 44 65 73 74 3d 3d 53 52     if( eDest==SR
1c2d2 54 5f 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 20  T_Callback ){.  
1c2d3 20 20 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a          Select *
1c2d4 70 46 69 72 73 74 20 3d 20 70 3b 0a 20 20 20 20  pFirst = p;.    
1c2d5 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 46 69        while( pFi
1c2d6 72 73 74 2d 3e 70 50 72 69 6f 72 20 29 20 70 46  rst->pPrior ) pF
1c2d7 69 72 73 74 20 3d 20 70 46 69 72 73 74 2d 3e 70  irst = pFirst->p
1c2d8 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 20 20 20  Prior;.         
1c2d9 20 67 65 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 4e   generateColumnN
1c2da 61 6d 65 73 28 70 50 61 72 73 65 2c 20 30 2c 20  ames(pParse, 0, 
1c2db 70 46 69 72 73 74 2d 3e 70 45 4c 69 73 74 29 3b  pFirst->pEList);
1c2dc 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1c2dd 20 20 20 69 42 72 65 61 6b 20 3d 20 73 71 6c 69     iBreak = sqli
1c2de 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
1c2df 28 76 29 3b 0a 20 20 20 20 20 20 20 20 69 43 6f  (v);.        iCo
1c2e0 6e 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  nt = sqlite3Vdbe
1c2e1 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20  MakeLabel(v);.  
1c2e2 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4c 69 6d        computeLim
1c2e3 69 74 52 65 67 69 73 74 65 72 73 28 70 50 61 72  itRegisters(pPar
1c2e4 73 65 2c 20 70 2c 20 69 42 72 65 61 6b 29 3b 0a  se, p, iBreak);.
1c2e5 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1c2e6 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 52  dbeAddOp(v, OP_R
1c2e7 65 77 69 6e 64 2c 20 75 6e 69 6f 6e 54 61 62 2c  ewind, unionTab,
1c2e8 20 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 20 20   iBreak);.      
1c2e9 20 20 69 53 74 61 72 74 20 3d 20 73 71 6c 69 74    iStart = sqlit
1c2ea 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
1c2eb 72 28 76 29 3b 0a 20 20 20 20 20 20 20 20 72 63  r(v);.        rc
1c2ec 20 3d 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f   = selectInnerLo
1c2ed 6f 70 28 70 50 61 72 73 65 2c 20 70 2c 20 70 2d  op(pParse, p, p-
1c2ee 3e 70 45 4c 69 73 74 2c 20 75 6e 69 6f 6e 54 61  >pEList, unionTa
1c2ef 62 2c 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45  b, p->pEList->nE
1c2f0 78 70 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20  xpr,.           
1c2f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c2f2 20 20 70 4f 72 64 65 72 42 79 2c 20 2d 31 2c 20    pOrderBy, -1, 
1c2f3 65 44 65 73 74 2c 20 69 50 61 72 6d 2c 20 0a 20  eDest, iParm, . 
1c2f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c2f5 20 20 20 20 20 20 20 20 20 20 20 20 69 43 6f 6e              iCon
1c2f6 74 2c 20 69 42 72 65 61 6b 2c 20 30 29 3b 0a 20  t, iBreak, 0);. 
1c2f7 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b         if( rc ){
1c2f8 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
1c2f9 31 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  1;.          got
1c2fa 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65  o multi_select_e
1c2fb 6e 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  nd;.        }.  
1c2fc 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1c2fd 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
1c2fe 20 69 43 6f 6e 74 29 3b 0a 20 20 20 20 20 20 20   iCont);.       
1c2ff 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1c300 70 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 75 6e  p(v, OP_Next, un
1c301 69 6f 6e 54 61 62 2c 20 69 53 74 61 72 74 29 3b  ionTab, iStart);
1c302 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1c303 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
1c304 28 76 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 20  (v, iBreak);.   
1c305 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1c306 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f 73  AddOp(v, OP_Clos
1c307 65 2c 20 75 6e 69 6f 6e 54 61 62 2c 20 30 29 3b  e, unionTab, 0);
1c308 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
1c309 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1c30a 63 61 73 65 20 54 4b 5f 49 4e 54 45 52 53 45 43  case TK_INTERSEC
1c30b 54 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 74  T: {.      int t
1c30c 61 62 31 2c 20 74 61 62 32 3b 0a 20 20 20 20 20  ab1, tab2;.     
1c30d 20 69 6e 74 20 69 43 6f 6e 74 2c 20 69 42 72 65   int iCont, iBre
1c30e 61 6b 2c 20 69 53 74 61 72 74 3b 0a 20 20 20 20  ak, iStart;.    
1c30f 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 2c 20    Expr *pLimit, 
1c310 2a 70 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20  *pOffset;.      
1c311 69 6e 74 20 61 64 64 72 3b 0a 0a 20 20 20 20 20  int addr;..     
1c312 20 2f 2a 20 49 4e 54 45 52 53 45 43 54 20 69 73   /* INTERSECT is
1c313 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20   different from 
1c314 74 68 65 20 6f 74 68 65 72 73 20 73 69 6e 63 65  the others since
1c315 20 69 74 20 72 65 71 75 69 72 65 73 0a 20 20 20   it requires.   
1c316 20 20 20 2a 2a 20 74 77 6f 20 74 65 6d 70 6f 72     ** two tempor
1c317 61 72 79 20 74 61 62 6c 65 73 2e 20 20 48 65 6e  ary tables.  Hen
1c318 63 65 20 69 74 20 68 61 73 20 69 74 73 20 6f 77  ce it has its ow
1c319 6e 20 63 61 73 65 2e 20 20 42 65 67 69 6e 0a 20  n case.  Begin. 
1c31a 20 20 20 20 20 2a 2a 20 62 79 20 61 6c 6c 6f 63       ** by alloc
1c31b 61 74 69 6e 67 20 74 68 65 20 74 61 62 6c 65 73  ating the tables
1c31c 20 77 65 20 77 69 6c 6c 20 6e 65 65 64 2e 0a 20   we will need.. 
1c31d 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 74 61       */.      ta
1c31e 62 31 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  b1 = pParse->nTa
1c31f 62 2b 2b 3b 0a 20 20 20 20 20 20 74 61 62 32 20  b++;.      tab2 
1c320 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b  = pParse->nTab++
1c321 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 72 64  ;.      if( pOrd
1c322 65 72 42 79 20 26 26 20 6d 61 74 63 68 4f 72 64  erBy && matchOrd
1c323 65 72 62 79 54 6f 43 6f 6c 75 6d 6e 28 70 50 61  erbyToColumn(pPa
1c324 72 73 65 2c 70 2c 70 4f 72 64 65 72 42 79 2c 74  rse,p,pOrderBy,t
1c325 61 62 31 2c 31 29 20 29 7b 0a 20 20 20 20 20 20  ab1,1) ){.      
1c326 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 20 20    rc = 1;.      
1c327 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c    goto multi_sel
1c328 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20 20 20 7d  ect_end;.      }
1c329 0a 20 20 20 20 20 20 63 72 65 61 74 65 53 6f 72  .      createSor
1c32a 74 69 6e 67 49 6e 64 65 78 28 70 50 61 72 73 65  tingIndex(pParse
1c32b 2c 20 70 2c 20 70 4f 72 64 65 72 42 79 29 3b 0a  , p, pOrderBy);.
1c32c 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71  .      addr = sq
1c32d 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1c32e 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72  , OP_OpenEphemer
1c32f 61 6c 2c 20 74 61 62 31 2c 20 30 29 3b 0a 20 20  al, tab1, 0);.  
1c330 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61      assert( p->a
1c331 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d  ddrOpenEphm[0] =
1c332 3d 20 2d 31 20 29 3b 0a 20 20 20 20 20 20 70 2d  = -1 );.      p-
1c333 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d  >addrOpenEphm[0]
1c334 20 3d 20 61 64 64 72 3b 0a 20 20 20 20 20 20 70   = addr;.      p
1c335 2d 3e 70 52 69 67 68 74 6d 6f 73 74 2d 3e 75 73  ->pRightmost->us
1c336 65 73 45 70 68 6d 20 3d 20 31 3b 0a 20 20 20 20  esEphm = 1;.    
1c337 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 45 4c    assert( p->pEL
1c338 69 73 74 20 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  ist );..      /*
1c339 20 43 6f 64 65 20 74 68 65 20 53 45 4c 45 43 54   Code the SELECT
1c33a 73 20 74 6f 20 6f 75 72 20 6c 65 66 74 20 69 6e  s to our left in
1c33b 74 6f 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  to temporary tab
1c33c 6c 65 20 22 74 61 62 31 22 2e 0a 20 20 20 20 20  le "tab1"..     
1c33d 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 73   */.      rc = s
1c33e 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61  qlite3Select(pPa
1c33f 72 73 65 2c 20 70 50 72 69 6f 72 2c 20 53 52 54  rse, pPrior, SRT
1c340 5f 55 6e 69 6f 6e 2c 20 74 61 62 31 2c 20 30 2c  _Union, tab1, 0,
1c341 20 30 2c 20 30 2c 20 61 66 66 29 3b 0a 20 20 20   0, 0, aff);.   
1c342 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20     if( rc ){.   
1c343 20 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f       goto multi_
1c344 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20  select_end;.    
1c345 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f    }..      /* Co
1c346 64 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 53  de the current S
1c347 45 4c 45 43 54 20 69 6e 74 6f 20 74 65 6d 70 6f  ELECT into tempo
1c348 72 61 72 79 20 74 61 62 6c 65 20 22 74 61 62 32  rary table "tab2
1c349 22 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ".      */.     
1c34a 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
1c34b 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4f  dbeAddOp(v, OP_O
1c34c 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 74 61  penEphemeral, ta
1c34d 62 32 2c 20 30 29 3b 0a 20 20 20 20 20 20 61 73  b2, 0);.      as
1c34e 73 65 72 74 28 20 70 2d 3e 61 64 64 72 4f 70 65  sert( p->addrOpe
1c34f 6e 45 70 68 6d 5b 31 5d 20 3d 3d 20 2d 31 20 29  nEphm[1] == -1 )
1c350 3b 0a 20 20 20 20 20 20 70 2d 3e 61 64 64 72 4f  ;.      p->addrO
1c351 70 65 6e 45 70 68 6d 5b 31 5d 20 3d 20 61 64 64  penEphm[1] = add
1c352 72 3b 0a 20 20 20 20 20 20 70 2d 3e 70 50 72 69  r;.      p->pPri
1c353 6f 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4c  or = 0;.      pL
1c354 69 6d 69 74 20 3d 20 70 2d 3e 70 4c 69 6d 69 74  imit = p->pLimit
1c355 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d 69  ;.      p->pLimi
1c356 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4f 66  t = 0;.      pOf
1c357 66 73 65 74 20 3d 20 70 2d 3e 70 4f 66 66 73 65  fset = p->pOffse
1c358 74 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4f 66 66  t;.      p->pOff
1c359 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 72  set = 0;.      r
1c35a 63 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63  c = sqlite3Selec
1c35b 74 28 70 50 61 72 73 65 2c 20 70 2c 20 53 52 54  t(pParse, p, SRT
1c35c 5f 55 6e 69 6f 6e 2c 20 74 61 62 32 2c 20 30 2c  _Union, tab2, 0,
1c35d 20 30 2c 20 30 2c 20 61 66 66 29 3b 0a 20 20 20   0, 0, aff);.   
1c35e 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20 70     p->pPrior = p
1c35f 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 73 71 6c  Prior;.      sql
1c360 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 70  ite3ExprDelete(p
1c361 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20 20 20 20 20  ->pLimit);.     
1c362 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 70 4c 69   p->pLimit = pLi
1c363 6d 69 74 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4f  mit;.      p->pO
1c364 66 66 73 65 74 20 3d 20 70 4f 66 66 73 65 74 3b  ffset = pOffset;
1c365 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b  .      if( rc ){
1c366 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 75  .        goto mu
1c367 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a  lti_select_end;.
1c368 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
1c369 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
1c36a 74 6f 20 74 61 6b 65 20 74 68 65 20 69 6e 74 65  to take the inte
1c36b 72 73 65 63 74 69 6f 6e 20 6f 66 20 74 68 65 20  rsection of the 
1c36c 74 77 6f 20 74 65 6d 70 6f 72 61 72 79 0a 20 20  two temporary.  
1c36d 20 20 20 20 2a 2a 20 74 61 62 6c 65 73 2e 0a 20      ** tables.. 
1c36e 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 73       */.      as
1c36f 73 65 72 74 28 20 70 2d 3e 70 45 4c 69 73 74 20  sert( p->pEList 
1c370 29 3b 0a 20 20 20 20 20 20 69 66 28 20 65 44 65  );.      if( eDe
1c371 73 74 3d 3d 53 52 54 5f 43 61 6c 6c 62 61 63 6b  st==SRT_Callback
1c372 20 29 7b 0a 20 20 20 20 20 20 20 20 53 65 6c 65   ){.        Sele
1c373 63 74 20 2a 70 46 69 72 73 74 20 3d 20 70 3b 0a  ct *pFirst = p;.
1c374 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70          while( p
1c375 46 69 72 73 74 2d 3e 70 50 72 69 6f 72 20 29 20  First->pPrior ) 
1c376 70 46 69 72 73 74 20 3d 20 70 46 69 72 73 74 2d  pFirst = pFirst-
1c377 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 20  >pPrior;.       
1c378 20 67 65 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 4e   generateColumnN
1c379 61 6d 65 73 28 70 50 61 72 73 65 2c 20 30 2c 20  ames(pParse, 0, 
1c37a 70 46 69 72 73 74 2d 3e 70 45 4c 69 73 74 29 3b  pFirst->pEList);
1c37b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
1c37c 42 72 65 61 6b 20 3d 20 73 71 6c 69 74 65 33 56  Break = sqlite3V
1c37d 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
1c37e 0a 20 20 20 20 20 20 69 43 6f 6e 74 20 3d 20 73  .      iCont = s
1c37f 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
1c380 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 63 6f  bel(v);.      co
1c381 6d 70 75 74 65 4c 69 6d 69 74 52 65 67 69 73 74  mputeLimitRegist
1c382 65 72 73 28 70 50 61 72 73 65 2c 20 70 2c 20 69  ers(pParse, p, i
1c383 42 72 65 61 6b 29 3b 0a 20 20 20 20 20 20 73 71  Break);.      sq
1c384 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1c385 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 74 61 62  , OP_Rewind, tab
1c386 31 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 20 20  1, iBreak);.    
1c387 20 20 69 53 74 61 72 74 20 3d 20 73 71 6c 69 74    iStart = sqlit
1c388 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1c389 50 5f 52 6f 77 4b 65 79 2c 20 74 61 62 31 2c 20  P_RowKey, tab1, 
1c38a 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  0);.      sqlite
1c38b 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1c38c 5f 4e 6f 74 46 6f 75 6e 64 2c 20 74 61 62 32 2c  _NotFound, tab2,
1c38d 20 69 43 6f 6e 74 29 3b 0a 20 20 20 20 20 20 72   iCont);.      r
1c38e 63 20 3d 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c  c = selectInnerL
1c38f 6f 6f 70 28 70 50 61 72 73 65 2c 20 70 2c 20 70  oop(pParse, p, p
1c390 2d 3e 70 45 4c 69 73 74 2c 20 74 61 62 31 2c 20  ->pEList, tab1, 
1c391 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  p->pEList->nExpr
1c392 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1c393 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
1c394 4f 72 64 65 72 42 79 2c 20 2d 31 2c 20 65 44 65  OrderBy, -1, eDe
1c395 73 74 2c 20 69 50 61 72 6d 2c 20 0a 20 20 20 20  st, iParm, .    
1c396 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c397 20 20 20 20 20 20 20 20 20 69 43 6f 6e 74 2c 20           iCont, 
1c398 69 42 72 65 61 6b 2c 20 30 29 3b 0a 20 20 20 20  iBreak, 0);.    
1c399 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
1c39a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20      rc = 1;.    
1c39b 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73      goto multi_s
1c39c 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20 20  elect_end;.     
1c39d 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
1c39e 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
1c39f 28 76 2c 20 69 43 6f 6e 74 29 3b 0a 20 20 20 20  (v, iCont);.    
1c3a0 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1c3a1 4f 70 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 74  Op(v, OP_Next, t
1c3a2 61 62 31 2c 20 69 53 74 61 72 74 29 3b 0a 20 20  ab1, iStart);.  
1c3a3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52      sqlite3VdbeR
1c3a4 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69  esolveLabel(v, i
1c3a5 42 72 65 61 6b 29 3b 0a 20 20 20 20 20 20 73 71  Break);.      sq
1c3a6 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1c3a7 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 74 61 62 32  , OP_Close, tab2
1c3a8 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 0);.      sqli
1c3a9 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1c3aa 4f 50 5f 43 6c 6f 73 65 2c 20 74 61 62 31 2c 20  OP_Close, tab1, 
1c3ab 30 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  0);.      break;
1c3ac 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
1c3ad 20 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 53   Make sure all S
1c3ae 45 4c 45 43 54 73 20 69 6e 20 74 68 65 20 73 74  ELECTs in the st
1c3af 61 74 65 6d 65 6e 74 20 68 61 76 65 20 74 68 65  atement have the
1c3b0 20 73 61 6d 65 20 6e 75 6d 62 65 72 20 6f 66 20   same number of 
1c3b1 65 6c 65 6d 65 6e 74 73 0a 20 20 2a 2a 20 69 6e  elements.  ** in
1c3b2 20 74 68 65 69 72 20 72 65 73 75 6c 74 20 73 65   their result se
1c3b3 74 73 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  ts..  */.  asser
1c3b4 74 28 20 70 2d 3e 70 45 4c 69 73 74 20 26 26 20  t( p->pEList && 
1c3b5 70 50 72 69 6f 72 2d 3e 70 45 4c 69 73 74 20 29  pPrior->pEList )
1c3b6 3b 0a 20 20 69 66 28 20 70 2d 3e 70 45 4c 69 73  ;.  if( p->pELis
1c3b7 74 2d 3e 6e 45 78 70 72 21 3d 70 50 72 69 6f 72  t->nExpr!=pPrior
1c3b8 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20  ->pEList->nExpr 
1c3b9 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
1c3ba 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
1c3bb 53 45 4c 45 43 54 73 20 74 6f 20 74 68 65 20 6c  SELECTs to the l
1c3bc 65 66 74 20 61 6e 64 20 72 69 67 68 74 20 6f 66  eft and right of
1c3bd 20 25 73 22 0a 20 20 20 20 20 20 22 20 64 6f 20   %s".      " do 
1c3be 6e 6f 74 20 68 61 76 65 20 74 68 65 20 73 61 6d  not have the sam
1c3bf 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 75  e number of resu
1c3c0 6c 74 20 63 6f 6c 75 6d 6e 73 22 2c 20 73 65 6c  lt columns", sel
1c3c1 65 63 74 4f 70 4e 61 6d 65 28 70 2d 3e 6f 70 29  ectOpName(p->op)
1c3c2 29 3b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20  );.    rc = 1;. 
1c3c3 20 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65     goto multi_se
1c3c4 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a 0a 20  lect_end;.  }.. 
1c3c5 20 2f 2a 20 53 65 74 20 74 68 65 20 6e 75 6d 62   /* Set the numb
1c3c6 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
1c3c7 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
1c3c8 73 0a 20 20 2a 2f 0a 20 20 6e 43 6f 6c 20 3d 20  s.  */.  nCol = 
1c3c9 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  p->pEList->nExpr
1c3ca 3b 0a 20 20 77 68 69 6c 65 28 20 6e 53 65 74 50  ;.  while( nSetP
1c3cb 32 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  2 ){.    sqlite3
1c3cc 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20  VdbeChangeP2(v, 
1c3cd 61 53 65 74 50 32 5b 2d 2d 6e 53 65 74 50 32 5d  aSetP2[--nSetP2]
1c3ce 2c 20 6e 43 6f 6c 29 3b 0a 20 20 7d 0a 0a 20 20  , nCol);.  }..  
1c3cf 2f 2a 20 43 6f 6d 70 75 74 65 20 63 6f 6c 6c 61  /* Compute colla
1c3d0 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20 75  ting sequences u
1c3d1 73 65 64 20 62 79 20 65 69 74 68 65 72 20 74 68  sed by either th
1c3d2 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
1c3d3 65 20 6f 72 0a 20 20 2a 2a 20 62 79 20 61 6e 79  e or.  ** by any
1c3d4 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
1c3d5 73 20 6e 65 65 64 65 64 20 74 6f 20 69 6d 70 6c  s needed to impl
1c3d6 65 6d 65 6e 74 20 74 68 65 20 63 6f 6d 70 6f 75  ement the compou
1c3d7 6e 64 20 73 65 6c 65 63 74 2e 0a 20 20 2a 2a 20  nd select..  ** 
1c3d8 41 74 74 61 63 68 20 74 68 65 20 4b 65 79 49 6e  Attach the KeyIn
1c3d9 66 6f 20 73 74 72 75 63 74 75 72 65 20 74 6f 20  fo structure to 
1c3da 61 6c 6c 20 74 65 6d 70 6f 72 61 72 79 20 74 61  all temporary ta
1c3db 62 6c 65 73 2e 20 20 49 6e 76 6f 6b 65 20 74 68  bles.  Invoke th
1c3dc 65 0a 20 20 2a 2a 20 4f 52 44 45 52 20 42 59 20  e.  ** ORDER BY 
1c3dd 70 72 6f 63 65 73 73 69 6e 67 20 69 66 20 74 68  processing if th
1c3de 65 72 65 20 69 73 20 61 6e 20 4f 52 44 45 52 20  ere is an ORDER 
1c3df 42 59 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2a 0a  BY clause..  **.
1c3e0 20 20 2a 2a 20 54 68 69 73 20 73 65 63 74 69 6f    ** This sectio
1c3e1 6e 20 69 73 20 72 75 6e 20 62 79 20 74 68 65 20  n is run by the 
1c3e2 72 69 67 68 74 2d 6d 6f 73 74 20 53 45 4c 45 43  right-most SELEC
1c3e3 54 20 73 74 61 74 65 6d 65 6e 74 20 6f 6e 6c 79  T statement only
1c3e4 2e 0a 20 20 2a 2a 20 53 45 4c 45 43 54 20 73 74  ..  ** SELECT st
1c3e5 61 74 65 6d 65 6e 74 73 20 74 6f 20 74 68 65 20  atements to the 
1c3e6 6c 65 66 74 20 61 6c 77 61 79 73 20 73 6b 69 70  left always skip
1c3e7 20 74 68 69 73 20 70 61 72 74 2e 20 20 54 68 65   this part.  The
1c3e8 20 72 69 67 68 74 2d 6d 6f 73 74 0a 20 20 2a 2a   right-most.  **
1c3e9 20 53 45 4c 45 43 54 20 6d 69 67 68 74 20 61 6c   SELECT might al
1c3ea 73 6f 20 73 6b 69 70 20 74 68 69 73 20 70 61 72  so skip this par
1c3eb 74 20 69 66 20 69 74 20 68 61 73 20 6e 6f 20 4f  t if it has no O
1c3ec 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 61  RDER BY clause a
1c3ed 6e 64 0a 20 20 2a 2a 20 6e 6f 20 74 65 6d 70 20  nd.  ** no temp 
1c3ee 74 61 62 6c 65 73 20 61 72 65 20 72 65 71 75 69  tables are requi
1c3ef 72 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  red..  */.  if( 
1c3f0 70 4f 72 64 65 72 42 79 20 7c 7c 20 70 2d 3e 75  pOrderBy || p->u
1c3f1 73 65 73 45 70 68 6d 20 29 7b 0a 20 20 20 20 69  sesEphm ){.    i
1c3f2 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
1c3f3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1c3f4 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  Loop counter */.
1c3f5 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65      KeyInfo *pKe
1c3f6 79 49 6e 66 6f 3b 20 20 20 20 20 20 20 20 20 20  yInfo;          
1c3f7 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73    /* Collating s
1c3f8 65 71 75 65 6e 63 65 20 66 6f 72 20 74 68 65 20  equence for the 
1c3f9 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20  result set */.  
1c3fa 20 20 53 65 6c 65 63 74 20 2a 70 4c 6f 6f 70 3b    Select *pLoop;
1c3fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c3fc 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 74  /* For looping t
1c3fd 68 72 6f 75 67 68 20 53 45 4c 45 43 54 20 73 74  hrough SELECT st
1c3fe 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20  atements */.    
1c3ff 69 6e 74 20 6e 4b 65 79 43 6f 6c 3b 20 20 20 20  int nKeyCol;    
1c400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1c401 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
1c402 65 73 20 69 6e 20 70 4b 65 79 49 6e 66 6f 2d 3e  es in pKeyInfo->
1c403 61 43 6f 6c 5b 5d 20 2a 2f 0a 20 20 20 20 43 6f  aCol[] */.    Co
1c404 6c 6c 53 65 71 20 2a 2a 61 70 43 6f 6c 6c 3b 20  llSeq **apColl; 
1c405 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
1c406 6f 72 20 6c 6f 6f 70 69 6e 67 20 74 68 72 6f 75  or looping throu
1c407 67 68 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f  gh pKeyInfo->aCo
1c408 6c 6c 5b 5d 20 2a 2f 0a 20 20 20 20 43 6f 6c 6c  ll[] */.    Coll
1c409 53 65 71 20 2a 2a 61 43 6f 70 79 3b 20 20 20 20  Seq **aCopy;    
1c40a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 63            /* A c
1c40b 6f 70 79 20 6f 66 20 70 4b 65 79 49 6e 66 6f 2d  opy of pKeyInfo-
1c40c 3e 61 43 6f 6c 6c 5b 5d 20 2a 2f 0a 0a 20 20 20  >aColl[] */..   
1c40d 20 61 73 73 65 72 74 28 20 70 2d 3e 70 52 69 67   assert( p->pRig
1c40e 68 74 6d 6f 73 74 3d 3d 70 20 29 3b 0a 20 20 20  htmost==p );.   
1c40f 20 6e 4b 65 79 43 6f 6c 20 3d 20 6e 43 6f 6c 20   nKeyCol = nCol 
1c410 2b 20 28 70 4f 72 64 65 72 42 79 20 3f 20 70 4f  + (pOrderBy ? pO
1c411 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 20 3a 20  rderBy->nExpr : 
1c412 30 29 3b 0a 20 20 20 20 70 4b 65 79 49 6e 66 6f  0);.    pKeyInfo
1c413 20 3d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28   = sqliteMalloc(
1c414 73 69 7a 65 6f 66 28 2a 70 4b 65 79 49 6e 66 6f  sizeof(*pKeyInfo
1c415 29 2b 6e 4b 65 79 43 6f 6c 2a 28 73 69 7a 65 6f  )+nKeyCol*(sizeo
1c416 66 28 43 6f 6c 6c 53 65 71 2a 29 20 2b 20 31 29  f(CollSeq*) + 1)
1c417 29 3b 0a 20 20 20 20 69 66 28 20 21 70 4b 65 79  );.    if( !pKey
1c418 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 72 63  Info ){.      rc
1c419 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
1c41a 0a 20 20 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74  .      goto mult
1c41b 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20  i_select_end;.  
1c41c 20 20 7d 0a 0a 20 20 20 20 70 4b 65 79 49 6e 66    }..    pKeyInf
1c41d 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 70 50 61  o->enc = ENC(pPa
1c41e 72 73 65 2d 3e 64 62 29 3b 0a 20 20 20 20 70 4b  rse->db);.    pK
1c41f 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 3d  eyInfo->nField =
1c420 20 6e 43 6f 6c 3b 0a 0a 20 20 20 20 66 6f 72 28   nCol;..    for(
1c421 69 3d 30 2c 20 61 70 43 6f 6c 6c 3d 70 4b 65 79  i=0, apColl=pKey
1c422 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 3b 20 69 3c 6e  Info->aColl; i<n
1c423 43 6f 6c 3b 20 69 2b 2b 2c 20 61 70 43 6f 6c 6c  Col; i++, apColl
1c424 2b 2b 29 7b 0a 20 20 20 20 20 20 2a 61 70 43 6f  ++){.      *apCo
1c425 6c 6c 20 3d 20 6d 75 6c 74 69 53 65 6c 65 63 74  ll = multiSelect
1c426 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20  CollSeq(pParse, 
1c427 70 2c 20 69 29 3b 0a 20 20 20 20 20 20 69 66 28  p, i);.      if(
1c428 20 30 3d 3d 2a 61 70 43 6f 6c 6c 20 29 7b 0a 20   0==*apColl ){. 
1c429 20 20 20 20 20 20 20 2a 61 70 43 6f 6c 6c 20 3d         *apColl =
1c42a 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 70 44 66   pParse->db->pDf
1c42b 6c 74 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a  ltColl;.      }.
1c42c 20 20 20 20 7d 0a 0a 20 20 20 20 66 6f 72 28 70      }..    for(p
1c42d 4c 6f 6f 70 3d 70 3b 20 70 4c 6f 6f 70 3b 20 70  Loop=p; pLoop; p
1c42e 4c 6f 6f 70 3d 70 4c 6f 6f 70 2d 3e 70 50 72 69  Loop=pLoop->pPri
1c42f 6f 72 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 69  or){.      for(i
1c430 3d 30 3b 20 69 3c 32 3b 20 69 2b 2b 29 7b 0a 20  =0; i<2; i++){. 
1c431 20 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 20         int addr 
1c432 3d 20 70 4c 6f 6f 70 2d 3e 61 64 64 72 4f 70 65  = pLoop->addrOpe
1c433 6e 45 70 68 6d 5b 69 5d 3b 0a 20 20 20 20 20 20  nEphm[i];.      
1c434 20 20 69 66 28 20 61 64 64 72 3c 30 20 29 7b 0a    if( addr<0 ){.
1c435 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20            /* If 
1c436 5b 30 5d 20 69 73 20 75 6e 75 73 65 64 20 74 68  [0] is unused th
1c437 65 6e 20 5b 31 5d 20 69 73 20 61 6c 73 6f 20 75  en [1] is also u
1c438 6e 75 73 65 64 2e 20 20 53 6f 20 77 65 20 63 61  nused.  So we ca
1c439 6e 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61  n.          ** a
1c43a 6c 77 61 79 73 20 73 61 66 65 6c 79 20 61 62 6f  lways safely abo
1c43b 72 74 20 61 73 20 73 6f 6f 6e 20 61 73 20 74 68  rt as soon as th
1c43c 65 20 66 69 72 73 74 20 75 6e 75 73 65 64 20 73  e first unused s
1c43d 6c 6f 74 20 69 73 20 66 6f 75 6e 64 20 2a 2f 0a  lot is found */.
1c43e 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
1c43f 28 20 70 4c 6f 6f 70 2d 3e 61 64 64 72 4f 70 65  ( pLoop->addrOpe
1c440 6e 45 70 68 6d 5b 31 5d 3c 30 20 29 3b 0a 20 20  nEphm[1]<0 );.  
1c441 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
1c442 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1c443 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
1c444 67 65 50 32 28 76 2c 20 61 64 64 72 2c 20 6e 43  geP2(v, addr, nC
1c445 6f 6c 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ol);.        sql
1c446 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33  ite3VdbeChangeP3
1c447 28 76 2c 20 61 64 64 72 2c 20 28 63 68 61 72 2a  (v, addr, (char*
1c448 29 70 4b 65 79 49 6e 66 6f 2c 20 50 33 5f 4b 45  )pKeyInfo, P3_KE
1c449 59 49 4e 46 4f 29 3b 0a 20 20 20 20 20 20 20 20  YINFO);.        
1c44a 70 4c 6f 6f 70 2d 3e 61 64 64 72 4f 70 65 6e 45  pLoop->addrOpenE
1c44b 70 68 6d 5b 69 5d 20 3d 20 2d 31 3b 0a 20 20 20  phm[i] = -1;.   
1c44c 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
1c44d 69 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b 0a  if( pOrderBy ){.
1c44e 20 20 20 20 20 20 73 74 72 75 63 74 20 45 78 70        struct Exp
1c44f 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4f 54 65  rList_item *pOTe
1c450 72 6d 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61  rm = pOrderBy->a
1c451 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 4f 72 64  ;.      int nOrd
1c452 65 72 42 79 45 78 70 72 20 3d 20 70 4f 72 64 65  erByExpr = pOrde
1c453 72 42 79 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20  rBy->nExpr;.    
1c454 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 20 20    int addr;.    
1c455 20 20 75 38 20 2a 70 53 6f 72 74 4f 72 64 65 72    u8 *pSortOrder
1c456 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 52 65 75 73  ;..      /* Reus
1c457 65 20 74 68 65 20 73 61 6d 65 20 70 4b 65 79 49  e the same pKeyI
1c458 6e 66 6f 20 66 6f 72 20 74 68 65 20 4f 52 44 45  nfo for the ORDE
1c459 52 20 42 59 20 61 73 20 77 61 73 20 75 73 65 64  R BY as was used
1c45a 20 61 62 6f 76 65 20 66 6f 72 0a 20 20 20 20 20   above for.     
1c45b 20 2a 2a 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64   ** the compound
1c45c 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e   select statemen
1c45d 74 73 2e 20 20 45 78 63 65 70 74 20 77 65 20 68  ts.  Except we h
1c45e 61 76 65 20 74 6f 20 63 68 61 6e 67 65 20 6f 75  ave to change ou
1c45f 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 70  t the.      ** p
1c460 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 5d  KeyInfo->aColl[]
1c461 20 76 61 6c 75 65 73 2e 20 20 53 6f 6d 65 20 6f   values.  Some o
1c462 66 20 74 68 65 20 61 43 6f 6c 6c 5b 5d 20 76 61  f the aColl[] va
1c463 6c 75 65 73 20 77 69 6c 6c 20 62 65 0a 20 20 20  lues will be.   
1c464 20 20 20 2a 2a 20 72 65 75 73 65 64 20 77 68 65     ** reused whe
1c465 6e 20 63 6f 6e 73 74 72 75 63 74 69 6e 67 20 74  n constructing t
1c466 68 65 20 70 4b 65 79 49 6e 66 6f 20 66 6f 72 20  he pKeyInfo for 
1c467 74 68 65 20 4f 52 44 45 52 20 42 59 2c 20 73 6f  the ORDER BY, so
1c468 20 6d 61 6b 65 0a 20 20 20 20 20 20 2a 2a 20 61   make.      ** a
1c469 20 63 6f 70 79 2e 20 20 53 75 66 66 69 63 69 65   copy.  Sufficie
1c46a 6e 74 20 73 70 61 63 65 20 74 6f 20 68 6f 6c 64  nt space to hold
1c46b 20 62 6f 74 68 20 74 68 65 20 6e 43 6f 6c 20 65   both the nCol e
1c46c 6e 74 72 69 65 73 20 66 6f 72 0a 20 20 20 20 20  ntries for.     
1c46d 20 2a 2a 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64   ** the compound
1c46e 20 73 65 6c 65 63 74 20 61 6e 64 20 74 68 65 20   select and the 
1c46f 6e 4f 72 64 65 72 62 79 45 78 70 72 20 65 6e 74  nOrderbyExpr ent
1c470 72 69 65 73 20 66 6f 72 20 74 68 65 20 4f 52 44  ries for the ORD
1c471 45 52 20 42 59 0a 20 20 20 20 20 20 2a 2a 20 77  ER BY.      ** w
1c472 61 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 62 6f  as allocated abo
1c473 76 65 2e 20 20 42 75 74 20 77 65 20 6e 65 65 64  ve.  But we need
1c474 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 63 6f 6d   to move the com
1c475 70 6f 75 6e 64 20 73 65 6c 65 63 74 0a 20 20 20  pound select.   
1c476 20 20 20 2a 2a 20 65 6e 74 72 69 65 73 20 6f 75     ** entries ou
1c477 74 20 6f 66 20 74 68 65 20 77 61 79 20 62 65 66  t of the way bef
1c478 6f 72 65 20 63 6f 6e 73 74 72 75 63 74 69 6e 67  ore constructing
1c479 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 65 6e   the ORDER BY en
1c47a 74 72 69 65 73 2e 0a 20 20 20 20 20 20 2a 2a 20  tries..      ** 
1c47b 4d 6f 76 65 20 74 68 65 20 63 6f 6d 70 6f 75 6e  Move the compoun
1c47c 64 20 73 65 6c 65 63 74 20 65 6e 74 72 69 65 73  d select entries
1c47d 20 69 6e 74 6f 20 61 43 6f 70 79 5b 5d 20 77 68   into aCopy[] wh
1c47e 65 72 65 20 74 68 65 79 20 63 61 6e 20 62 65 0a  ere they can be.
1c47f 20 20 20 20 20 20 2a 2a 20 61 63 63 65 73 73 65        ** accesse
1c480 64 20 61 6e 64 20 72 65 75 73 65 64 20 77 68 65  d and reused whe
1c481 6e 20 63 6f 6e 73 74 72 75 63 74 69 6e 67 20 74  n constructing t
1c482 68 65 20 4f 52 44 45 52 20 42 59 20 65 6e 74 72  he ORDER BY entr
1c483 69 65 73 2e 0a 20 20 20 20 20 20 2a 2a 20 42 65  ies..      ** Be
1c484 63 61 75 73 65 20 6e 43 6f 6c 20 6d 69 67 68 74  cause nCol might
1c485 20 62 65 20 67 72 65 61 74 65 72 20 74 68 61 6e   be greater than
1c486 20 6f 72 20 6c 65 73 73 20 74 68 61 6e 20 6e 4f   or less than nO
1c487 72 64 65 72 42 79 45 78 70 72 0a 20 20 20 20 20  rderByExpr.     
1c488 20 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 75   ** we have to u
1c489 73 65 20 6d 65 6d 6d 6f 76 65 28 29 20 77 68 65  se memmove() whe
1c48a 6e 20 64 6f 69 6e 67 20 74 68 65 20 63 6f 70 79  n doing the copy
1c48b 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
1c48c 20 61 43 6f 70 79 20 3d 20 26 70 4b 65 79 49 6e   aCopy = &pKeyIn
1c48d 66 6f 2d 3e 61 43 6f 6c 6c 5b 6e 4f 72 64 65 72  fo->aColl[nOrder
1c48e 42 79 45 78 70 72 5d 3b 0a 20 20 20 20 20 20 70  ByExpr];.      p
1c48f 53 6f 72 74 4f 72 64 65 72 20 3d 20 70 4b 65 79  SortOrder = pKey
1c490 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72  Info->aSortOrder
1c491 20 3d 20 28 75 38 2a 29 26 61 43 6f 70 79 5b 6e   = (u8*)&aCopy[n
1c492 43 6f 6c 5d 3b 0a 20 20 20 20 20 20 6d 65 6d 6d  Col];.      memm
1c493 6f 76 65 28 61 43 6f 70 79 2c 20 70 4b 65 79 49  ove(aCopy, pKeyI
1c494 6e 66 6f 2d 3e 61 43 6f 6c 6c 2c 20 6e 43 6f 6c  nfo->aColl, nCol
1c495 2a 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71 2a  *sizeof(CollSeq*
1c496 29 29 3b 0a 0a 20 20 20 20 20 20 61 70 43 6f 6c  ));..      apCol
1c497 6c 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43  l = pKeyInfo->aC
1c498 6f 6c 6c 3b 0a 20 20 20 20 20 20 66 6f 72 28 69  oll;.      for(i
1c499 3d 30 3b 20 69 3c 6e 4f 72 64 65 72 42 79 45 78  =0; i<nOrderByEx
1c49a 70 72 3b 20 69 2b 2b 2c 20 70 4f 54 65 72 6d 2b  pr; i++, pOTerm+
1c49b 2b 2c 20 61 70 43 6f 6c 6c 2b 2b 2c 20 70 53 6f  +, apColl++, pSo
1c49c 72 74 4f 72 64 65 72 2b 2b 29 7b 0a 20 20 20 20  rtOrder++){.    
1c49d 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 20      Expr *pExpr 
1c49e 3d 20 70 4f 54 65 72 6d 2d 3e 70 45 78 70 72 3b  = pOTerm->pExpr;
1c49f 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 70 45  .        if( (pE
1c4a0 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f  xpr->flags & EP_
1c4a1 45 78 70 43 6f 6c 6c 61 74 65 29 20 29 7b 0a 20  ExpCollate) ){. 
1c4a2 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
1c4a3 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 21 3d 30   pExpr->pColl!=0
1c4a4 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 2a 61   );.          *a
1c4a5 70 43 6f 6c 6c 20 3d 20 70 45 78 70 72 2d 3e 70  pColl = pExpr->p
1c4a6 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 7d 65  Coll;.        }e
1c4a7 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2a  lse{.          *
1c4a8 61 70 43 6f 6c 6c 20 3d 20 61 43 6f 70 79 5b 70  apColl = aCopy[p
1c4a9 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 5d 3b 0a  Expr->iColumn];.
1c4aa 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1c4ab 20 20 2a 70 53 6f 72 74 4f 72 64 65 72 20 3d 20    *pSortOrder = 
1c4ac 70 4f 54 65 72 6d 2d 3e 73 6f 72 74 4f 72 64 65  pOTerm->sortOrde
1c4ad 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  r;.      }.     
1c4ae 20 61 73 73 65 72 74 28 20 70 2d 3e 70 52 69 67   assert( p->pRig
1c4af 68 74 6d 6f 73 74 3d 3d 70 20 29 3b 0a 20 20 20  htmost==p );.   
1c4b0 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 64     assert( p->ad
1c4b1 64 72 4f 70 65 6e 45 70 68 6d 5b 32 5d 3e 3d 30  drOpenEphm[2]>=0
1c4b2 20 29 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d   );.      addr =
1c4b3 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d   p->addrOpenEphm
1c4b4 5b 32 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  [2];.      sqlit
1c4b5 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 76  e3VdbeChangeP2(v
1c4b6 2c 20 61 64 64 72 2c 20 70 2d 3e 70 4f 72 64 65  , addr, p->pOrde
1c4b7 72 42 79 2d 3e 6e 45 78 70 72 2b 32 29 3b 0a 20  rBy->nExpr+2);. 
1c4b8 20 20 20 20 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e       pKeyInfo->n
1c4b9 46 69 65 6c 64 20 3d 20 6e 4f 72 64 65 72 42 79  Field = nOrderBy
1c4ba 45 78 70 72 3b 0a 20 20 20 20 20 20 73 71 6c 69  Expr;.      sqli
1c4bb 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28  te3VdbeChangeP3(
1c4bc 76 2c 20 61 64 64 72 2c 20 28 63 68 61 72 2a 29  v, addr, (char*)
1c4bd 70 4b 65 79 49 6e 66 6f 2c 20 50 33 5f 4b 45 59  pKeyInfo, P3_KEY
1c4be 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20  INFO_HANDOFF);. 
1c4bf 20 20 20 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20       pKeyInfo = 
1c4c0 30 3b 0a 20 20 20 20 20 20 67 65 6e 65 72 61 74  0;.      generat
1c4c1 65 53 6f 72 74 54 61 69 6c 28 70 50 61 72 73 65  eSortTail(pParse
1c4c2 2c 20 70 2c 20 76 2c 20 70 2d 3e 70 45 4c 69 73  , p, v, p->pELis
1c4c3 74 2d 3e 6e 45 78 70 72 2c 20 65 44 65 73 74 2c  t->nExpr, eDest,
1c4c4 20 69 50 61 72 6d 29 3b 0a 20 20 20 20 7d 0a 0a   iParm);.    }..
1c4c5 20 20 20 20 73 71 6c 69 74 65 46 72 65 65 28 70      sqliteFree(p
1c4c6 4b 65 79 49 6e 66 6f 29 3b 0a 20 20 7d 0a 0a 6d  KeyInfo);.  }..m
1c4c7 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3a  ulti_select_end:
1c4c8 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1c4c9 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1c4ca 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53  _OMIT_COMPOUND_S
1c4cb 45 4c 45 43 54 20 2a 2f 0a 0a 23 69 66 6e 64 65  ELECT */..#ifnde
1c4cc 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
1c4cd 45 57 0a 2f 2a 0a 2a 2a 20 53 63 61 6e 20 74 68  EW./*.** Scan th
1c4ce 72 6f 75 67 68 20 74 68 65 20 65 78 70 72 65 73  rough the expres
1c4cf 73 69 6f 6e 20 70 45 78 70 72 2e 20 20 52 65 70  sion pExpr.  Rep
1c4d0 6c 61 63 65 20 65 76 65 72 79 20 72 65 66 65 72  lace every refer
1c4d1 65 6e 63 65 20 74 6f 0a 2a 2a 20 61 20 63 6f 6c  ence to.** a col
1c4d2 75 6d 6e 20 69 6e 20 74 61 62 6c 65 20 6e 75 6d  umn in table num
1c4d3 62 65 72 20 69 54 61 62 6c 65 20 77 69 74 68 20  ber iTable with 
1c4d4 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 69 43  a copy of the iC
1c4d5 6f 6c 75 6d 6e 2d 74 68 0a 2a 2a 20 65 6e 74 72  olumn-th.** entr
1c4d6 79 20 69 6e 20 70 45 4c 69 73 74 2e 20 20 28 42  y in pEList.  (B
1c4d7 75 74 20 6c 65 61 76 65 20 72 65 66 65 72 65 6e  ut leave referen
1c4d8 63 65 73 20 74 6f 20 74 68 65 20 52 4f 57 49 44  ces to the ROWID
1c4d9 20 63 6f 6c 75 6d 6e 20 0a 2a 2a 20 75 6e 63 68   column .** unch
1c4da 61 6e 67 65 64 2e 29 0a 2a 2a 0a 2a 2a 20 54 68  anged.).**.** Th
1c4db 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 70 61  is routine is pa
1c4dc 72 74 20 6f 66 20 74 68 65 20 66 6c 61 74 74 65  rt of the flatte
1c4dd 6e 69 6e 67 20 70 72 6f 63 65 64 75 72 65 2e 20  ning procedure. 
1c4de 20 41 20 73 75 62 71 75 65 72 79 0a 2a 2a 20 77   A subquery.** w
1c4df 68 6f 73 65 20 72 65 73 75 6c 74 20 73 65 74 20  hose result set 
1c4e0 69 73 20 64 65 66 69 6e 65 64 20 62 79 20 70 45  is defined by pE
1c4e1 4c 69 73 74 20 61 70 70 65 61 72 73 20 61 73 20  List appears as 
1c4e2 65 6e 74 72 79 20 69 6e 20 74 68 65 0a 2a 2a 20  entry in the.** 
1c4e3 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 61  FROM clause of a
1c4e4 20 53 45 4c 45 43 54 20 73 75 63 68 20 74 68 61   SELECT such tha
1c4e5 74 20 74 68 65 20 56 44 42 45 20 63 75 72 73 6f  t the VDBE curso
1c4e6 72 20 61 73 73 69 67 6e 65 64 20 74 6f 20 74 68  r assigned to th
1c4e7 61 74 0a 2a 2a 20 46 4f 52 4d 20 63 6c 61 75 73  at.** FORM claus
1c4e8 65 20 65 6e 74 72 79 20 69 73 20 69 54 61 62 6c  e entry is iTabl
1c4e9 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  e.  This routine
1c4ea 20 6d 61 6b 65 20 74 68 65 20 6e 65 63 65 73 73   make the necess
1c4eb 61 72 79 20 0a 2a 2a 20 63 68 61 6e 67 65 73 20  ary .** changes 
1c4ec 74 6f 20 70 45 78 70 72 20 73 6f 20 74 68 61 74  to pExpr so that
1c4ed 20 69 74 20 72 65 66 65 72 73 20 64 69 72 65 63   it refers direc
1c4ee 74 6c 79 20 74 6f 20 74 68 65 20 73 6f 75 72 63  tly to the sourc
1c4ef 65 20 74 61 62 6c 65 0a 2a 2a 20 6f 66 20 74 68  e table.** of th
1c4f0 65 20 73 75 62 71 75 65 72 79 20 72 61 74 68 65  e subquery rathe
1c4f1 72 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  r the result set
1c4f2 20 6f 66 20 74 68 65 20 73 75 62 71 75 65 72 79   of the subquery
1c4f3 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
1c4f4 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 28 45   substExprList(E
1c4f5 78 70 72 4c 69 73 74 2a 2c 69 6e 74 2c 45 78 70  xprList*,int,Exp
1c4f6 72 4c 69 73 74 2a 29 3b 20 20 2f 2a 20 46 6f 72  rList*);  /* For
1c4f7 77 61 72 64 20 44 65 63 6c 20 2a 2f 0a 73 74 61  ward Decl */.sta
1c4f8 74 69 63 20 76 6f 69 64 20 73 75 62 73 74 53 65  tic void substSe
1c4f9 6c 65 63 74 28 53 65 6c 65 63 74 20 2a 2c 20 69  lect(Select *, i
1c4fa 6e 74 2c 20 45 78 70 72 4c 69 73 74 20 2a 29 3b  nt, ExprList *);
1c4fb 20 20 2f 2a 20 46 6f 72 77 61 72 64 20 44 65 63    /* Forward Dec
1c4fc 6c 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  l */.static void
1c4fd 20 73 75 62 73 74 45 78 70 72 28 45 78 70 72 20   substExpr(Expr 
1c4fe 2a 70 45 78 70 72 2c 20 69 6e 74 20 69 54 61 62  *pExpr, int iTab
1c4ff 6c 65 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 45  le, ExprList *pE
1c500 4c 69 73 74 29 7b 0a 20 20 69 66 28 20 70 45 78  List){.  if( pEx
1c501 70 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  pr==0 ) return;.
1c502 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d    if( pExpr->op=
1c503 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 70 45  =TK_COLUMN && pE
1c504 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61  xpr->iTable==iTa
1c505 62 6c 65 20 29 7b 0a 20 20 20 20 69 66 28 20 70  ble ){.    if( p
1c506 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3c 30 20  Expr->iColumn<0 
1c507 29 7b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e  ){.      pExpr->
1c508 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a 20 20  op = TK_NULL;.  
1c509 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 45    }else{.      E
1c50a 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20 20 20 20  xpr *pNew;.     
1c50b 20 61 73 73 65 72 74 28 20 70 45 4c 69 73 74 21   assert( pEList!
1c50c 3d 30 20 26 26 20 70 45 78 70 72 2d 3e 69 43 6f  =0 && pExpr->iCo
1c50d 6c 75 6d 6e 3c 70 45 4c 69 73 74 2d 3e 6e 45 78  lumn<pEList->nEx
1c50e 70 72 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  pr );.      asse
1c50f 72 74 28 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  rt( pExpr->pLeft
1c510 3d 3d 30 20 26 26 20 70 45 78 70 72 2d 3e 70 52  ==0 && pExpr->pR
1c511 69 67 68 74 3d 3d 30 20 26 26 20 70 45 78 70 72  ight==0 && pExpr
1c512 2d 3e 70 4c 69 73 74 3d 3d 30 20 29 3b 0a 20 20  ->pList==0 );.  
1c513 20 20 20 20 70 4e 65 77 20 3d 20 70 45 4c 69 73      pNew = pELis
1c514 74 2d 3e 61 5b 70 45 78 70 72 2d 3e 69 43 6f 6c  t->a[pExpr->iCol
1c515 75 6d 6e 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20  umn].pExpr;.    
1c516 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 21 3d    assert( pNew!=
1c517 30 20 29 3b 0a 20 20 20 20 20 20 70 45 78 70 72  0 );.      pExpr
1c518 2d 3e 6f 70 20 3d 20 70 4e 65 77 2d 3e 6f 70 3b  ->op = pNew->op;
1c519 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1c51a 45 78 70 72 2d 3e 70 4c 65 66 74 3d 3d 30 20 29  Expr->pLeft==0 )
1c51b 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70  ;.      pExpr->p
1c51c 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 45 78  Left = sqlite3Ex
1c51d 70 72 44 75 70 28 70 4e 65 77 2d 3e 70 4c 65 66  prDup(pNew->pLef
1c51e 74 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  t);.      assert
1c51f 28 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3d  ( pExpr->pRight=
1c520 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 45 78 70  =0 );.      pExp
1c521 72 2d 3e 70 52 69 67 68 74 20 3d 20 73 71 6c 69  r->pRight = sqli
1c522 74 65 33 45 78 70 72 44 75 70 28 70 4e 65 77 2d  te3ExprDup(pNew-
1c523 3e 70 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20  >pRight);.      
1c524 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70  assert( pExpr->p
1c525 4c 69 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20  List==0 );.     
1c526 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 20 3d 20   pExpr->pList = 
1c527 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
1c528 75 70 28 70 4e 65 77 2d 3e 70 4c 69 73 74 29 3b  up(pNew->pList);
1c529 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54  .      pExpr->iT
1c52a 61 62 6c 65 20 3d 20 70 4e 65 77 2d 3e 69 54 61  able = pNew->iTa
1c52b 62 6c 65 3b 0a 20 20 20 20 20 20 70 45 78 70 72  ble;.      pExpr
1c52c 2d 3e 70 54 61 62 20 3d 20 70 4e 65 77 2d 3e 70  ->pTab = pNew->p
1c52d 54 61 62 3b 0a 20 20 20 20 20 20 70 45 78 70 72  Tab;.      pExpr
1c52e 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 70 4e 65 77  ->iColumn = pNew
1c52f 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20  ->iColumn;.     
1c530 20 70 45 78 70 72 2d 3e 69 41 67 67 20 3d 20 70   pExpr->iAgg = p
1c531 4e 65 77 2d 3e 69 41 67 67 3b 0a 20 20 20 20 20  New->iAgg;.     
1c532 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e 43 6f 70   sqlite3TokenCop
1c533 79 28 26 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2c  y(&pExpr->token,
1c534 20 26 70 4e 65 77 2d 3e 74 6f 6b 65 6e 29 3b 0a   &pNew->token);.
1c535 20 20 20 20 20 20 73 71 6c 69 74 65 33 54 6f 6b        sqlite3Tok
1c536 65 6e 43 6f 70 79 28 26 70 45 78 70 72 2d 3e 73  enCopy(&pExpr->s
1c537 70 61 6e 2c 20 26 70 4e 65 77 2d 3e 73 70 61 6e  pan, &pNew->span
1c538 29 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e  );.      pExpr->
1c539 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65  pSelect = sqlite
1c53a 33 53 65 6c 65 63 74 44 75 70 28 70 4e 65 77 2d  3SelectDup(pNew-
1c53b 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20  >pSelect);.     
1c53c 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 3d 20   pExpr->flags = 
1c53d 70 4e 65 77 2d 3e 66 6c 61 67 73 3b 0a 20 20 20  pNew->flags;.   
1c53e 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
1c53f 73 75 62 73 74 45 78 70 72 28 70 45 78 70 72 2d  substExpr(pExpr-
1c540 3e 70 4c 65 66 74 2c 20 69 54 61 62 6c 65 2c 20  >pLeft, iTable, 
1c541 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 73 75 62  pEList);.    sub
1c542 73 74 45 78 70 72 28 70 45 78 70 72 2d 3e 70 52  stExpr(pExpr->pR
1c543 69 67 68 74 2c 20 69 54 61 62 6c 65 2c 20 70 45  ight, iTable, pE
1c544 4c 69 73 74 29 3b 0a 20 20 20 20 73 75 62 73 74  List);.    subst
1c545 53 65 6c 65 63 74 28 70 45 78 70 72 2d 3e 70 53  Select(pExpr->pS
1c546 65 6c 65 63 74 2c 20 69 54 61 62 6c 65 2c 20 70  elect, iTable, p
1c547 45 4c 69 73 74 29 3b 0a 20 20 20 20 73 75 62 73  EList);.    subs
1c548 74 45 78 70 72 4c 69 73 74 28 70 45 78 70 72 2d  tExprList(pExpr-
1c549 3e 70 4c 69 73 74 2c 20 69 54 61 62 6c 65 2c 20  >pList, iTable, 
1c54a 70 45 4c 69 73 74 29 3b 0a 20 20 7d 0a 7d 0a 73  pEList);.  }.}.s
1c54b 74 61 74 69 63 20 76 6f 69 64 20 73 75 62 73 74  tatic void subst
1c54c 45 78 70 72 4c 69 73 74 28 45 78 70 72 4c 69 73  ExprList(ExprLis
1c54d 74 20 2a 70 4c 69 73 74 2c 20 69 6e 74 20 69 54  t *pList, int iT
1c54e 61 62 6c 65 2c 20 45 78 70 72 4c 69 73 74 20 2a  able, ExprList *
1c54f 70 45 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69  pEList){.  int i
1c550 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30  ;.  if( pList==0
1c551 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72   ) return;.  for
1c552 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e  (i=0; i<pList->n
1c553 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Expr; i++){.    
1c554 73 75 62 73 74 45 78 70 72 28 70 4c 69 73 74 2d  substExpr(pList-
1c555 3e 61 5b 69 5d 2e 70 45 78 70 72 2c 20 69 54 61  >a[i].pExpr, iTa
1c556 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20  ble, pEList);.  
1c557 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  }.}.static void 
1c558 73 75 62 73 74 53 65 6c 65 63 74 28 53 65 6c 65  substSelect(Sele
1c559 63 74 20 2a 70 2c 20 69 6e 74 20 69 54 61 62 6c  ct *p, int iTabl
1c55a 65 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c  e, ExprList *pEL
1c55b 69 73 74 29 7b 0a 20 20 69 66 28 20 21 70 20 29  ist){.  if( !p )
1c55c 20 72 65 74 75 72 6e 3b 0a 20 20 73 75 62 73 74   return;.  subst
1c55d 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 45 4c 69  ExprList(p->pELi
1c55e 73 74 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69  st, iTable, pELi
1c55f 73 74 29 3b 0a 20 20 73 75 62 73 74 45 78 70 72  st);.  substExpr
1c560 4c 69 73 74 28 70 2d 3e 70 47 72 6f 75 70 42 79  List(p->pGroupBy
1c561 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74  , iTable, pEList
1c562 29 3b 0a 20 20 73 75 62 73 74 45 78 70 72 4c 69  );.  substExprLi
1c563 73 74 28 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20  st(p->pOrderBy, 
1c564 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b  iTable, pEList);
1c565 0a 20 20 73 75 62 73 74 45 78 70 72 28 70 2d 3e  .  substExpr(p->
1c566 70 48 61 76 69 6e 67 2c 20 69 54 61 62 6c 65 2c  pHaving, iTable,
1c567 20 70 45 4c 69 73 74 29 3b 0a 20 20 73 75 62 73   pEList);.  subs
1c568 74 45 78 70 72 28 70 2d 3e 70 57 68 65 72 65 2c  tExpr(p->pWhere,
1c569 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29   iTable, pEList)
1c56a 3b 0a 20 20 73 75 62 73 74 53 65 6c 65 63 74 28  ;.  substSelect(
1c56b 70 2d 3e 70 50 72 69 6f 72 2c 20 69 54 61 62 6c  p->pPrior, iTabl
1c56c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 7d 0a 23 65  e, pEList);.}.#e
1c56d 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
1c56e 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45  (SQLITE_OMIT_VIE
1c56f 57 29 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  W) */..#ifndef S
1c570 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a  QLITE_OMIT_VIEW.
1c571 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
1c572 6e 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 66  ne attempts to f
1c573 6c 61 74 74 65 6e 20 73 75 62 71 75 65 72 69 65  latten subquerie
1c574 73 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 70  s in order to sp
1c575 65 65 64 0a 2a 2a 20 65 78 65 63 75 74 69 6f 6e  eed.** execution
1c576 2e 20 20 49 74 20 72 65 74 75 72 6e 73 20 31 20  .  It returns 1 
1c577 69 66 20 69 74 20 6d 61 6b 65 73 20 63 68 61 6e  if it makes chan
1c578 67 65 73 20 61 6e 64 20 30 20 69 66 20 6e 6f 20  ges and 0 if no 
1c579 66 6c 61 74 74 65 6e 69 6e 67 0a 2a 2a 20 6f 63  flattening.** oc
1c57a 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 75  curs..**.** To u
1c57b 6e 64 65 72 73 74 61 6e 64 20 74 68 65 20 63 6f  nderstand the co
1c57c 6e 63 65 70 74 20 6f 66 20 66 6c 61 74 74 65 6e  ncept of flatten
1c57d 69 6e 67 2c 20 63 6f 6e 73 69 64 65 72 20 74 68  ing, consider th
1c57e 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 71  e following.** q
1c57f 75 65 72 79 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  uery:.**.**     
1c580 53 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 28 53  SELECT a FROM (S
1c581 45 4c 45 43 54 20 78 2b 79 20 41 53 20 61 20 46  ELECT x+y AS a F
1c582 52 4f 4d 20 74 31 20 57 48 45 52 45 20 7a 3c 31  ROM t1 WHERE z<1
1c583 30 30 29 20 57 48 45 52 45 20 61 3e 35 0a 2a 2a  00) WHERE a>5.**
1c584 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20  .** The default 
1c585 77 61 79 20 6f 66 20 69 6d 70 6c 65 6d 65 6e 74  way of implement
1c586 69 6e 67 20 74 68 69 73 20 71 75 65 72 79 20 69  ing this query i
1c587 73 20 74 6f 20 65 78 65 63 75 74 65 20 74 68 65  s to execute the
1c588 0a 2a 2a 20 73 75 62 71 75 65 72 79 20 66 69 72  .** subquery fir
1c589 73 74 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65  st and store the
1c58a 20 72 65 73 75 6c 74 73 20 69 6e 20 61 20 74 65   results in a te
1c58b 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 2c 20 74  mporary table, t
1c58c 68 65 6e 0a 2a 2a 20 72 75 6e 20 74 68 65 20 6f  hen.** run the o
1c58d 75 74 65 72 20 71 75 65 72 79 20 6f 6e 20 74 68  uter query on th
1c58e 61 74 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  at temporary tab
1c58f 6c 65 2e 20 20 54 68 69 73 20 72 65 71 75 69 72  le.  This requir
1c590 65 73 20 74 77 6f 0a 2a 2a 20 70 61 73 73 65 73  es two.** passes
1c591 20 6f 76 65 72 20 74 68 65 20 64 61 74 61 2e 20   over the data. 
1c592 20 46 75 72 74 68 65 72 6d 6f 72 65 2c 20 62 65   Furthermore, be
1c593 63 61 75 73 65 20 74 68 65 20 74 65 6d 70 6f 72  cause the tempor
1c594 61 72 79 20 74 61 62 6c 65 0a 2a 2a 20 68 61 73  ary table.** has
1c595 20 6e 6f 20 69 6e 64 69 63 65 73 2c 20 74 68 65   no indices, the
1c596 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 6f 6e   WHERE clause on
1c597 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79   the outer query
1c598 20 63 61 6e 6e 6f 74 20 62 65 0a 2a 2a 20 6f 70   cannot be.** op
1c599 74 69 6d 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  timized..**.** T
1c59a 68 69 73 20 72 6f 75 74 69 6e 65 20 61 74 74 65  his routine atte
1c59b 6d 70 74 73 20 74 6f 20 72 65 77 72 69 74 65 20  mpts to rewrite 
1c59c 71 75 65 72 69 65 73 20 73 75 63 68 20 61 73 20  queries such as 
1c59d 74 68 65 20 61 62 6f 76 65 20 69 6e 74 6f 0a 2a  the above into.*
1c59e 2a 20 61 20 73 69 6e 67 6c 65 20 66 6c 61 74 20  * a single flat 
1c59f 73 65 6c 65 63 74 2c 20 6c 69 6b 65 20 74 68 69  select, like thi
1c5a0 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c  s:.**.**     SEL
1c5a1 45 43 54 20 78 2b 79 20 41 53 20 61 20 46 52 4f  ECT x+y AS a FRO
1c5a2 4d 20 74 31 20 57 48 45 52 45 20 7a 3c 31 30 30  M t1 WHERE z<100
1c5a3 20 41 4e 44 20 61 3e 35 0a 2a 2a 0a 2a 2a 20 54   AND a>5.**.** T
1c5a4 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65  he code generate
1c5a5 64 20 66 6f 72 20 74 68 69 73 20 73 69 6d 70 69  d for this simpi
1c5a6 66 69 63 61 74 69 6f 6e 20 67 69 76 65 73 20 74  fication gives t
1c5a7 68 65 20 73 61 6d 65 20 72 65 73 75 6c 74 0a 2a  he same result.*
1c5a8 2a 20 62 75 74 20 6f 6e 6c 79 20 68 61 73 20 74  * but only has t
1c5a9 6f 20 73 63 61 6e 20 74 68 65 20 64 61 74 61 20  o scan the data 
1c5aa 6f 6e 63 65 2e 20 20 41 6e 64 20 62 65 63 61 75  once.  And becau
1c5ab 73 65 20 69 6e 64 69 63 65 73 20 6d 69 67 68 74  se indices might
1c5ac 20 0a 2a 2a 20 65 78 69 73 74 20 6f 6e 20 74 68   .** exist on th
1c5ad 65 20 74 61 62 6c 65 20 74 31 2c 20 61 20 63 6f  e table t1, a co
1c5ae 6d 70 6c 65 74 65 20 73 63 61 6e 20 6f 66 20 74  mplete scan of t
1c5af 68 65 20 64 61 74 61 20 6d 69 67 68 74 20 62 65  he data might be
1c5b0 0a 2a 2a 20 61 76 6f 69 64 65 64 2e 0a 2a 2a 0a  .** avoided..**.
1c5b1 2a 2a 20 46 6c 61 74 74 65 6e 69 6e 67 20 69 73  ** Flattening is
1c5b2 20 6f 6e 6c 79 20 61 74 74 65 6d 70 74 65 64 20   only attempted 
1c5b3 69 66 20 61 6c 6c 20 6f 66 20 74 68 65 20 66 6f  if all of the fo
1c5b4 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 72 75 65  llowing are true
1c5b5 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 31 29 20 20 54  :.**.**   (1)  T
1c5b6 68 65 20 73 75 62 71 75 65 72 79 20 61 6e 64 20  he subquery and 
1c5b7 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20  the outer query 
1c5b8 64 6f 20 6e 6f 74 20 62 6f 74 68 20 75 73 65 20  do not both use 
1c5b9 61 67 67 72 65 67 61 74 65 73 2e 0a 2a 2a 0a 2a  aggregates..**.*
1c5ba 2a 20 20 20 28 32 29 20 20 54 68 65 20 73 75 62  *   (2)  The sub
1c5bb 71 75 65 72 79 20 69 73 20 6e 6f 74 20 61 6e 20  query is not an 
1c5bc 61 67 67 72 65 67 61 74 65 20 6f 72 20 74 68 65  aggregate or the
1c5bd 20 6f 75 74 65 72 20 71 75 65 72 79 20 69 73 20   outer query is 
1c5be 6e 6f 74 20 61 20 6a 6f 69 6e 2e 0a 2a 2a 0a 2a  not a join..**.*
1c5bf 2a 20 20 20 28 33 29 20 20 54 68 65 20 73 75 62  *   (3)  The sub
1c5c0 71 75 65 72 79 20 69 73 20 6e 6f 74 20 74 68 65  query is not the
1c5c1 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20 6f   right operand o
1c5c2 66 20 61 20 6c 65 66 74 20 6f 75 74 65 72 20 6a  f a left outer j
1c5c3 6f 69 6e 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20  oin, or.**      
1c5c4 20 20 74 68 65 20 73 75 62 71 75 65 72 79 20 69    the subquery i
1c5c5 73 20 6e 6f 74 20 69 74 73 65 6c 66 20 61 20 6a  s not itself a j
1c5c6 6f 69 6e 2e 20 20 28 54 69 63 6b 65 74 20 23 33  oin.  (Ticket #3
1c5c7 30 36 29 0a 2a 2a 0a 2a 2a 20 20 20 28 34 29 20  06).**.**   (4) 
1c5c8 20 54 68 65 20 73 75 62 71 75 65 72 79 20 69 73   The subquery is
1c5c9 20 6e 6f 74 20 44 49 53 54 49 4e 43 54 20 6f 72   not DISTINCT or
1c5ca 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79   the outer query
1c5cb 20 69 73 20 6e 6f 74 20 61 20 6a 6f 69 6e 2e 0a   is not a join..
1c5cc 2a 2a 0a 2a 2a 20 20 20 28 35 29 20 20 54 68 65  **.**   (5)  The
1c5cd 20 73 75 62 71 75 65 72 79 20 69 73 20 6e 6f 74   subquery is not
1c5ce 20 44 49 53 54 49 4e 43 54 20 6f 72 20 74 68 65   DISTINCT or the
1c5cf 20 6f 75 74 65 72 20 71 75 65 72 79 20 64 6f 65   outer query doe
1c5d0 73 20 6e 6f 74 20 75 73 65 0a 2a 2a 20 20 20 20  s not use.**    
1c5d1 20 20 20 20 61 67 67 72 65 67 61 74 65 73 2e 0a      aggregates..
1c5d2 2a 2a 0a 2a 2a 20 20 20 28 36 29 20 20 54 68 65  **.**   (6)  The
1c5d3 20 73 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e   subquery does n
1c5d4 6f 74 20 75 73 65 20 61 67 67 72 65 67 61 74 65  ot use aggregate
1c5d5 73 20 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71  s or the outer q
1c5d6 75 65 72 79 20 69 73 20 6e 6f 74 0a 2a 2a 20 20  uery is not.**  
1c5d7 20 20 20 20 20 20 44 49 53 54 49 4e 43 54 2e 0a        DISTINCT..
1c5d8 2a 2a 0a 2a 2a 20 20 20 28 37 29 20 20 54 68 65  **.**   (7)  The
1c5d9 20 73 75 62 71 75 65 72 79 20 68 61 73 20 61 20   subquery has a 
1c5da 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a  FROM clause..**.
1c5db 2a 2a 20 20 20 28 38 29 20 20 54 68 65 20 73 75  **   (8)  The su
1c5dc 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20  bquery does not 
1c5dd 75 73 65 20 4c 49 4d 49 54 20 6f 72 20 74 68 65  use LIMIT or the
1c5de 20 6f 75 74 65 72 20 71 75 65 72 79 20 69 73 20   outer query is 
1c5df 6e 6f 74 20 61 20 6a 6f 69 6e 2e 0a 2a 2a 0a 2a  not a join..**.*
1c5e0 2a 20 20 20 28 39 29 20 20 54 68 65 20 73 75 62  *   (9)  The sub
1c5e1 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20 75  query does not u
1c5e2 73 65 20 4c 49 4d 49 54 20 6f 72 20 74 68 65 20  se LIMIT or the 
1c5e3 6f 75 74 65 72 20 71 75 65 72 79 20 64 6f 65 73  outer query does
1c5e4 20 6e 6f 74 20 75 73 65 0a 2a 2a 20 20 20 20 20   not use.**     
1c5e5 20 20 20 61 67 67 72 65 67 61 74 65 73 2e 0a 2a     aggregates..*
1c5e6 2a 0a 2a 2a 20 20 28 31 30 29 20 20 54 68 65 20  *.**  (10)  The 
1c5e7 73 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f  subquery does no
1c5e8 74 20 75 73 65 20 61 67 67 72 65 67 61 74 65 73  t use aggregates
1c5e9 20 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71 75   or the outer qu
1c5ea 65 72 79 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20  ery does not.** 
1c5eb 20 20 20 20 20 20 20 75 73 65 20 4c 49 4d 49 54         use LIMIT
1c5ec 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 31 29 20 20 54  ..**.**  (11)  T
1c5ed 68 65 20 73 75 62 71 75 65 72 79 20 61 6e 64 20  he subquery and 
1c5ee 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20  the outer query 
1c5ef 64 6f 20 6e 6f 74 20 62 6f 74 68 20 68 61 76 65  do not both have
1c5f0 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
1c5f1 73 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 32 29 20 20  s..**.**  (12)  
1c5f2 54 68 65 20 73 75 62 71 75 65 72 79 20 69 73 20  The subquery is 
1c5f3 6e 6f 74 20 74 68 65 20 72 69 67 68 74 20 74 65  not the right te
1c5f4 72 6d 20 6f 66 20 61 20 4c 45 46 54 20 4f 55 54  rm of a LEFT OUT
1c5f5 45 52 20 4a 4f 49 4e 20 6f 72 20 74 68 65 0a 2a  ER JOIN or the.*
1c5f6 2a 20 20 20 20 20 20 20 20 73 75 62 71 75 65 72  *        subquer
1c5f7 79 20 68 61 73 20 6e 6f 20 57 48 45 52 45 20 63  y has no WHERE c
1c5f8 6c 61 75 73 65 2e 20 20 28 61 64 64 65 64 20 62  lause.  (added b
1c5f9 79 20 74 69 63 6b 65 74 20 23 33 35 30 29 0a 2a  y ticket #350).*
1c5fa 2a 0a 2a 2a 20 20 28 31 33 29 20 20 54 68 65 20  *.**  (13)  The 
1c5fb 73 75 62 71 75 65 72 79 20 61 6e 64 20 6f 75 74  subquery and out
1c5fc 65 72 20 71 75 65 72 79 20 64 6f 20 6e 6f 74 20  er query do not 
1c5fd 62 6f 74 68 20 75 73 65 20 4c 49 4d 49 54 0a 2a  both use LIMIT.*
1c5fe 2a 0a 2a 2a 20 20 28 31 34 29 20 20 54 68 65 20  *.**  (14)  The 
1c5ff 73 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f  subquery does no
1c600 74 20 75 73 65 20 4f 46 46 53 45 54 0a 2a 2a 0a  t use OFFSET.**.
1c601 2a 2a 20 20 28 31 35 29 20 20 54 68 65 20 6f 75  **  (15)  The ou
1c602 74 65 72 20 71 75 65 72 79 20 69 73 20 6e 6f 74  ter query is not
1c603 20 70 61 72 74 20 6f 66 20 61 20 63 6f 6d 70 6f   part of a compo
1c604 75 6e 64 20 73 65 6c 65 63 74 20 6f 72 20 74 68  und select or th
1c605 65 0a 2a 2a 20 20 20 20 20 20 20 20 73 75 62 71  e.**        subq
1c606 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20 68 61  uery does not ha
1c607 76 65 20 62 6f 74 68 20 61 6e 20 4f 52 44 45 52  ve both an ORDER
1c608 20 42 59 20 61 6e 64 20 61 20 4c 49 4d 49 54 20   BY and a LIMIT 
1c609 63 6c 61 75 73 65 2e 0a 2a 2a 20 20 20 20 20 20  clause..**      
1c60a 20 20 28 53 65 65 20 74 69 63 6b 65 74 20 23 32    (See ticket #2
1c60b 33 33 39 29 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68  339).**.** In th
1c60c 69 73 20 72 6f 75 74 69 6e 65 2c 20 74 68 65 20  is routine, the 
1c60d 22 70 22 20 70 61 72 61 6d 65 74 65 72 20 69 73  "p" parameter is
1c60e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
1c60f 65 20 6f 75 74 65 72 20 71 75 65 72 79 2e 0a 2a  e outer query..*
1c610 2a 20 54 68 65 20 73 75 62 71 75 65 72 79 20 69  * The subquery i
1c611 73 20 70 2d 3e 70 53 72 63 2d 3e 61 5b 69 46 72  s p->pSrc->a[iFr
1c612 6f 6d 5d 2e 20 20 69 73 41 67 67 20 69 73 20 74  om].  isAgg is t
1c613 72 75 65 20 69 66 20 74 68 65 20 6f 75 74 65 72  rue if the outer
1c614 20 71 75 65 72 79 0a 2a 2a 20 75 73 65 73 20 61   query.** uses a
1c615 67 67 72 65 67 61 74 65 73 20 61 6e 64 20 73 75  ggregates and su
1c616 62 71 75 65 72 79 49 73 41 67 67 20 69 73 20 74  bqueryIsAgg is t
1c617 72 75 65 20 69 66 20 74 68 65 20 73 75 62 71 75  rue if the subqu
1c618 65 72 79 20 75 73 65 73 20 61 67 67 72 65 67 61  ery uses aggrega
1c619 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 66 6c  tes..**.** If fl
1c61a 61 74 74 65 6e 69 6e 67 20 69 73 20 6e 6f 74 20  attening is not 
1c61b 61 74 74 65 6d 70 74 65 64 2c 20 74 68 69 73 20  attempted, this 
1c61c 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d  routine is a no-
1c61d 6f 70 20 61 6e 64 20 72 65 74 75 72 6e 73 20 30  op and returns 0
1c61e 2e 0a 2a 2a 20 49 66 20 66 6c 61 74 74 65 6e 69  ..** If flatteni
1c61f 6e 67 20 69 73 20 61 74 74 65 6d 70 74 65 64 20  ng is attempted 
1c620 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  this routine ret
1c621 75 72 6e 73 20 31 2e 0a 2a 2a 0a 2a 2a 20 41 6c  urns 1..**.** Al
1c622 6c 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73  l of the express
1c623 69 6f 6e 20 61 6e 61 6c 79 73 69 73 20 6d 75 73  ion analysis mus
1c624 74 20 6f 63 63 75 72 20 6f 6e 20 62 6f 74 68 20  t occur on both 
1c625 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20  the outer query 
1c626 61 6e 64 0a 2a 2a 20 74 68 65 20 73 75 62 71 75  and.** the subqu
1c627 65 72 79 20 62 65 66 6f 72 65 20 74 68 69 73 20  ery before this 
1c628 72 6f 75 74 69 6e 65 20 72 75 6e 73 2e 0a 2a 2f  routine runs..*/
1c629 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c 61 74  .static int flat
1c62a 74 65 6e 53 75 62 71 75 65 72 79 28 0a 20 20 53  tenSubquery(.  S
1c62b 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20  elect *p,       
1c62c 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 65 6e      /* The paren
1c62d 74 20 6f 72 20 6f 75 74 65 72 20 53 45 4c 45 43  t or outer SELEC
1c62e 54 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  T statement */. 
1c62f 20 69 6e 74 20 69 46 72 6f 6d 2c 20 20 20 20 20   int iFrom,     
1c630 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69        /* Index i
1c631 6e 20 70 2d 3e 70 53 72 63 2d 3e 61 5b 5d 20 6f  n p->pSrc->a[] o
1c632 66 20 74 68 65 20 69 6e 6e 65 72 20 73 75 62 71  f the inner subq
1c633 75 65 72 79 20 2a 2f 0a 20 20 69 6e 74 20 69 73  uery */.  int is
1c634 41 67 67 2c 20 20 20 20 20 20 20 20 20 20 20 2f  Agg,           /
1c635 2a 20 54 72 75 65 20 69 66 20 6f 75 74 65 72 20  * True if outer 
1c636 53 45 4c 45 43 54 20 75 73 65 73 20 61 67 67 72  SELECT uses aggr
1c637 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20  egate functions 
1c638 2a 2f 0a 20 20 69 6e 74 20 73 75 62 71 75 65 72  */.  int subquer
1c639 79 49 73 41 67 67 20 20 20 20 2f 2a 20 54 72 75  yIsAgg    /* Tru
1c63a 65 20 69 66 20 74 68 65 20 73 75 62 71 75 65 72  e if the subquer
1c63b 79 20 75 73 65 73 20 61 67 67 72 65 67 61 74 65  y uses aggregate
1c63c 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 29 7b   functions */.){
1c63d 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 75 62 3b  .  Select *pSub;
1c63e 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e         /* The in
1c63f 6e 65 72 20 71 75 65 72 79 20 6f 72 20 22 73 75  ner query or "su
1c640 62 71 75 65 72 79 22 20 2a 2f 0a 20 20 53 72 63  bquery" */.  Src
1c641 4c 69 73 74 20 2a 70 53 72 63 3b 20 20 20 20 20  List *pSrc;     
1c642 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c 61   /* The FROM cla
1c643 75 73 65 20 6f 66 20 74 68 65 20 6f 75 74 65 72  use of the outer
1c644 20 71 75 65 72 79 20 2a 2f 0a 20 20 53 72 63 4c   query */.  SrcL
1c645 69 73 74 20 2a 70 53 75 62 53 72 63 3b 20 20 20  ist *pSubSrc;   
1c646 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c 61 75  /* The FROM clau
1c647 73 65 20 6f 66 20 74 68 65 20 73 75 62 71 75 65  se of the subque
1c648 72 79 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  ry */.  ExprList
1c649 20 2a 70 4c 69 73 74 3b 20 20 20 20 2f 2a 20 54   *pList;    /* T
1c64a 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66  he result set of
1c64b 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79   the outer query
1c64c 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 72 65 6e   */.  int iParen
1c64d 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 56 44 42  t;        /* VDB
1c64e 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  E cursor number 
1c64f 6f 66 20 74 68 65 20 70 53 75 62 20 72 65 73 75  of the pSub resu
1c650 6c 74 20 73 65 74 20 74 65 6d 70 20 74 61 62 6c  lt set temp tabl
1c651 65 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20  e */.  int i;   
1c652 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
1c653 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
1c654 45 78 70 72 20 2a 70 57 68 65 72 65 3b 20 20 20  Expr *pWhere;   
1c655 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c656 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c   /* The WHERE cl
1c657 61 75 73 65 20 2a 2f 0a 20 20 73 74 72 75 63 74  ause */.  struct
1c658 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
1c659 53 75 62 69 74 65 6d 3b 20 20 20 2f 2a 20 54 68  Subitem;   /* Th
1c65a 65 20 73 75 62 71 75 65 72 79 20 2a 2f 0a 0a 20  e subquery */.. 
1c65b 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65   /* Check to see
1c65c 20 69 66 20 66 6c 61 74 74 65 6e 69 6e 67 20 69   if flattening i
1c65d 73 20 70 65 72 6d 69 74 74 65 64 2e 20 20 52 65  s permitted.  Re
1c65e 74 75 72 6e 20 30 20 69 66 20 6e 6f 74 2e 0a 20  turn 0 if not.. 
1c65f 20 2a 2f 0a 20 20 69 66 28 20 70 3d 3d 30 20 29   */.  if( p==0 )
1c660 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 53 72   return 0;.  pSr
1c661 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 61  c = p->pSrc;.  a
1c662 73 73 65 72 74 28 20 70 53 72 63 20 26 26 20 69  ssert( pSrc && i
1c663 46 72 6f 6d 3e 3d 30 20 26 26 20 69 46 72 6f 6d  From>=0 && iFrom
1c664 3c 70 53 72 63 2d 3e 6e 53 72 63 20 29 3b 0a 20  <pSrc->nSrc );. 
1c665 20 70 53 75 62 69 74 65 6d 20 3d 20 26 70 53 72   pSubitem = &pSr
1c666 63 2d 3e 61 5b 69 46 72 6f 6d 5d 3b 0a 20 20 70  c->a[iFrom];.  p
1c667 53 75 62 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e  Sub = pSubitem->
1c668 70 53 65 6c 65 63 74 3b 0a 20 20 61 73 73 65 72  pSelect;.  asser
1c669 74 28 20 70 53 75 62 21 3d 30 20 29 3b 0a 20 20  t( pSub!=0 );.  
1c66a 69 66 28 20 69 73 41 67 67 20 26 26 20 73 75 62  if( isAgg && sub
1c66b 71 75 65 72 79 49 73 41 67 67 20 29 20 72 65 74  queryIsAgg ) ret
1c66c 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20  urn 0;          
1c66d 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69         /* Restri
1c66e 63 74 69 6f 6e 20 28 31 29 20 20 2a 2f 0a 20 20  ction (1)  */.  
1c66f 69 66 28 20 73 75 62 71 75 65 72 79 49 73 41 67  if( subqueryIsAg
1c670 67 20 26 26 20 70 53 72 63 2d 3e 6e 53 72 63 3e  g && pSrc->nSrc>
1c671 31 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20  1 ) return 0;   
1c672 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69         /* Restri
1c673 63 74 69 6f 6e 20 28 32 29 20 20 2a 2f 0a 20 20  ction (2)  */.  
1c674 70 53 75 62 53 72 63 20 3d 20 70 53 75 62 2d 3e  pSubSrc = pSub->
1c675 70 53 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20  pSrc;.  assert( 
1c676 70 53 75 62 53 72 63 20 29 3b 0a 20 20 2f 2a 20  pSubSrc );.  /* 
1c677 50 72 69 6f 72 20 74 6f 20 76 65 72 73 69 6f 6e  Prior to version
1c678 20 33 2e 31 2e 32 2c 20 77 68 65 6e 20 4c 49 4d   3.1.2, when LIM
1c679 49 54 20 61 6e 64 20 4f 46 46 53 45 54 20 68 61  IT and OFFSET ha
1c67a 64 20 74 6f 20 62 65 20 73 69 6d 70 6c 65 20 63  d to be simple c
1c67b 6f 6e 73 74 61 6e 74 73 2c 0a 20 20 2a 2a 20 6e  onstants,.  ** n
1c67c 6f 74 20 61 72 62 69 74 72 61 72 79 20 65 78 70  ot arbitrary exp
1c67d 72 65 73 73 73 69 6f 6e 73 2c 20 77 65 20 61 6c  resssions, we al
1c67e 6c 6f 77 65 64 20 73 6f 6d 65 20 63 6f 6d 62 69  lowed some combi
1c67f 6e 69 6e 67 20 6f 66 20 4c 49 4d 49 54 20 61 6e  ning of LIMIT an
1c680 64 20 4f 46 46 53 45 54 0a 20 20 2a 2a 20 62 65  d OFFSET.  ** be
1c681 63 61 75 73 65 20 74 68 65 79 20 63 6f 75 6c 64  cause they could
1c682 20 62 65 20 63 6f 6d 70 75 74 65 64 20 61 74 20   be computed at 
1c683 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 20 20 42  compile-time.  B
1c684 75 74 20 77 68 65 6e 20 4c 49 4d 49 54 20 61 6e  ut when LIMIT an
1c685 64 20 4f 46 46 53 45 54 0a 20 20 2a 2a 20 62 65  d OFFSET.  ** be
1c686 63 61 6d 65 20 61 72 62 69 74 72 61 72 79 20 65  came arbitrary e
1c687 78 70 72 65 73 73 69 6f 6e 73 2c 20 77 65 20 77  xpressions, we w
1c688 65 72 65 20 66 6f 72 63 65 64 20 74 6f 20 61 64  ere forced to ad
1c689 64 20 72 65 73 74 72 69 63 74 69 6f 6e 73 20 28  d restrictions (
1c68a 31 33 29 0a 20 20 2a 2a 20 61 6e 64 20 28 31 34  13).  ** and (14
1c68b 29 2e 20 2a 2f 0a 20 20 69 66 28 20 70 53 75 62  ). */.  if( pSub
1c68c 2d 3e 70 4c 69 6d 69 74 20 26 26 20 70 2d 3e 70  ->pLimit && p->p
1c68d 4c 69 6d 69 74 20 29 20 72 65 74 75 72 6e 20 30  Limit ) return 0
1c68e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
1c68f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31  * Restriction (1
1c690 33 29 20 2a 2f 0a 20 20 69 66 28 20 70 53 75 62  3) */.  if( pSub
1c691 2d 3e 70 4f 66 66 73 65 74 20 29 20 72 65 74 75  ->pOffset ) retu
1c692 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20  rn 0;           
1c693 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1c694 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31  * Restriction (1
1c695 34 29 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70  4) */.  if( p->p
1c696 52 69 67 68 74 6d 6f 73 74 20 26 26 20 70 53 75  Rightmost && pSu
1c697 62 2d 3e 70 4c 69 6d 69 74 20 26 26 20 70 53 75  b->pLimit && pSu
1c698 62 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20  b->pOrderBy ){. 
1c699 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20     return 0;    
1c69a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c69b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c69c 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72          /* Restr
1c69d 69 63 74 69 6f 6e 20 28 31 35 29 20 2a 2f 0a 20  iction (15) */. 
1c69e 20 7d 0a 20 20 69 66 28 20 70 53 75 62 53 72 63   }.  if( pSubSrc
1c69f 2d 3e 6e 53 72 63 3d 3d 30 20 29 20 72 65 74 75  ->nSrc==0 ) retu
1c6a0 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20  rn 0;           
1c6a1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
1c6a2 65 73 74 72 69 63 74 69 6f 6e 20 28 37 29 20 20  estriction (7)  
1c6a3 2a 2f 0a 20 20 69 66 28 20 28 70 53 75 62 2d 3e  */.  if( (pSub->
1c6a4 69 73 44 69 73 74 69 6e 63 74 20 7c 7c 20 70 53  isDistinct || pS
1c6a5 75 62 2d 3e 70 4c 69 6d 69 74 29 20 0a 20 20 20  ub->pLimit) .   
1c6a6 20 20 20 20 20 20 26 26 20 28 70 53 72 63 2d 3e        && (pSrc->
1c6a7 6e 53 72 63 3e 31 20 7c 7c 20 69 73 41 67 67 29  nSrc>1 || isAgg)
1c6a8 20 29 7b 20 20 20 20 20 20 20 20 20 20 2f 2a 20   ){          /* 
1c6a9 52 65 73 74 72 69 63 74 69 6f 6e 73 20 28 34 29  Restrictions (4)
1c6aa 28 35 29 28 38 29 28 39 29 20 2a 2f 0a 20 20 20  (5)(8)(9) */.   
1c6ab 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20    return 0;     
1c6ac 20 20 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e    .  }.  if( p->
1c6ad 69 73 44 69 73 74 69 6e 63 74 20 26 26 20 73 75  isDistinct && su
1c6ae 62 71 75 65 72 79 49 73 41 67 67 20 29 20 72 65  bqueryIsAgg ) re
1c6af 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20  turn 0;         
1c6b0 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28  /* Restriction (
1c6b1 36 29 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 2d  6)  */.  if( (p-
1c6b2 3e 64 69 73 61 6c 6c 6f 77 4f 72 64 65 72 42 79  >disallowOrderBy
1c6b3 20 7c 7c 20 70 2d 3e 70 4f 72 64 65 72 42 79 29   || p->pOrderBy)
1c6b4 20 26 26 20 70 53 75 62 2d 3e 70 4f 72 64 65 72   && pSub->pOrder
1c6b5 42 79 20 29 7b 0a 20 20 20 20 20 72 65 74 75 72  By ){.     retur
1c6b6 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  n 0;            
1c6b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c6b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1c6b9 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31  * Restriction (1
1c6ba 31 29 20 2a 2f 0a 20 20 7d 0a 0a 20 20 2f 2a 20  1) */.  }..  /* 
1c6bb 52 65 73 74 72 69 63 74 69 6f 6e 20 33 3a 20 20  Restriction 3:  
1c6bc 49 66 20 74 68 65 20 73 75 62 71 75 65 72 79 20  If the subquery 
1c6bd 69 73 20 61 20 6a 6f 69 6e 2c 20 6d 61 6b 65 20  is a join, make 
1c6be 73 75 72 65 20 74 68 65 20 73 75 62 71 75 65 72  sure the subquer
1c6bf 79 20 69 73 20 0a 20 20 2a 2a 20 6e 6f 74 20 75  y is .  ** not u
1c6c0 73 65 64 20 61 73 20 74 68 65 20 72 69 67 68 74  sed as the right
1c6c1 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 6e 20 6f   operand of an o
1c6c2 75 74 65 72 20 6a 6f 69 6e 2e 20 20 45 78 61 6d  uter join.  Exam
1c6c3 70 6c 65 73 20 6f 66 20 77 68 79 20 74 68 69 73  ples of why this
1c6c4 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20 61 6c 6c  .  ** is not all
1c6c5 6f 77 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  owed:.  **.  ** 
1c6c6 20 20 20 20 20 20 20 20 74 31 20 4c 45 46 54 20          t1 LEFT 
1c6c7 4f 55 54 45 52 20 4a 4f 49 4e 20 28 74 32 20 4a  OUTER JOIN (t2 J
1c6c8 4f 49 4e 20 74 33 29 0a 20 20 2a 2a 0a 20 20 2a  OIN t3).  **.  *
1c6c9 2a 20 49 66 20 77 65 20 66 6c 61 74 74 65 6e 20  * If we flatten 
1c6ca 74 68 65 20 61 62 6f 76 65 2c 20 77 65 20 77 6f  the above, we wo
1c6cb 75 6c 64 20 67 65 74 0a 20 20 2a 2a 0a 20 20 2a  uld get.  **.  *
1c6cc 2a 20 20 20 20 20 20 20 20 20 28 74 31 20 4c 45  *         (t1 LE
1c6cd 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20 74 32  FT OUTER JOIN t2
1c6ce 29 20 4a 4f 49 4e 20 74 33 0a 20 20 2a 2a 0a 20  ) JOIN t3.  **. 
1c6cf 20 2a 2a 20 77 68 69 63 68 20 69 73 20 6e 6f 74   ** which is not
1c6d0 20 61 74 20 61 6c 6c 20 74 68 65 20 73 61 6d 65   at all the same
1c6d1 20 74 68 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69   thing..  */.  i
1c6d2 66 28 20 70 53 75 62 53 72 63 2d 3e 6e 53 72 63  f( pSubSrc->nSrc
1c6d3 3e 31 20 26 26 20 28 70 53 75 62 69 74 65 6d 2d  >1 && (pSubitem-
1c6d4 3e 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 4f  >jointype & JT_O
1c6d5 55 54 45 52 29 21 3d 30 20 29 7b 0a 20 20 20 20  UTER)!=0 ){.    
1c6d6 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20  return 0;.  }.. 
1c6d7 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20   /* Restriction 
1c6d8 31 32 3a 20 20 49 66 20 74 68 65 20 73 75 62 71  12:  If the subq
1c6d9 75 65 72 79 20 69 73 20 74 68 65 20 72 69 67 68  uery is the righ
1c6da 74 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 20 6c  t operand of a l
1c6db 65 66 74 20 6f 75 74 65 72 0a 20 20 2a 2a 20 6a  eft outer.  ** j
1c6dc 6f 69 6e 2c 20 6d 61 6b 65 20 73 75 72 65 20 74  oin, make sure t
1c6dd 68 65 20 73 75 62 71 75 65 72 79 20 68 61 73 20  he subquery has 
1c6de 6e 6f 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e  no WHERE clause.
1c6df 0a 20 20 2a 2a 20 41 6e 20 65 78 61 6d 70 6c 65  .  ** An example
1c6e0 73 20 6f 66 20 77 68 79 20 74 68 69 73 20 69 73  s of why this is
1c6e1 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 3a 0a 20 20   not allowed:.  
1c6e2 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20  **.  **         
1c6e3 74 31 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f  t1 LEFT OUTER JO
1c6e4 49 4e 20 28 53 45 4c 45 43 54 20 2a 20 46 52 4f  IN (SELECT * FRO
1c6e5 4d 20 74 32 20 57 48 45 52 45 20 74 32 2e 78 3e  M t2 WHERE t2.x>
1c6e6 30 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20  0).  **.  ** If 
1c6e7 77 65 20 66 6c 61 74 74 65 6e 20 74 68 65 20 61  we flatten the a
1c6e8 62 6f 76 65 2c 20 77 65 20 77 6f 75 6c 64 20 67  bove, we would g
1c6e9 65 74 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  et.  **.  **    
1c6ea 20 20 20 20 20 28 74 31 20 4c 45 46 54 20 4f 55       (t1 LEFT OU
1c6eb 54 45 52 20 4a 4f 49 4e 20 74 32 29 20 57 48 45  TER JOIN t2) WHE
1c6ec 52 45 20 74 32 2e 78 3e 30 0a 20 20 2a 2a 0a 20  RE t2.x>0.  **. 
1c6ed 20 2a 2a 20 42 75 74 20 74 68 65 20 74 32 2e 78   ** But the t2.x
1c6ee 3e 30 20 74 65 73 74 20 77 69 6c 6c 20 61 6c 77  >0 test will alw
1c6ef 61 79 73 20 66 61 69 6c 20 6f 6e 20 61 20 4e 55  ays fail on a NU
1c6f0 4c 4c 20 72 6f 77 20 6f 66 20 74 32 2c 20 77 68  LL row of t2, wh
1c6f1 69 63 68 0a 20 20 2a 2a 20 65 66 66 65 63 74 69  ich.  ** effecti
1c6f2 76 65 6c 79 20 63 6f 6e 76 65 72 74 73 20 74 68  vely converts th
1c6f3 65 20 4f 55 54 45 52 20 4a 4f 49 4e 20 69 6e 74  e OUTER JOIN int
1c6f4 6f 20 61 6e 20 49 4e 4e 45 52 20 4a 4f 49 4e 2e  o an INNER JOIN.
1c6f5 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 53 75  .  */.  if( (pSu
1c6f6 62 69 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65 20  bitem->jointype 
1c6f7 26 20 4a 54 5f 4f 55 54 45 52 29 21 3d 30 20 26  & JT_OUTER)!=0 &
1c6f8 26 20 70 53 75 62 2d 3e 70 57 68 65 72 65 21 3d  & pSub->pWhere!=
1c6f9 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
1c6fa 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  0;.  }..  /* If 
1c6fb 77 65 20 72 65 61 63 68 20 74 68 69 73 20 70 6f  we reach this po
1c6fc 69 6e 74 2c 20 69 74 20 6d 65 61 6e 73 20 66 6c  int, it means fl
1c6fd 61 74 74 65 6e 69 6e 67 20 69 73 20 70 65 72 6d  attening is perm
1c6fe 69 74 74 65 64 20 66 6f 72 20 74 68 65 0a 20 20  itted for the.  
1c6ff 2a 2a 20 69 46 72 6f 6d 2d 74 68 20 65 6e 74 72  ** iFrom-th entr
1c700 79 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c  y of the FROM cl
1c701 61 75 73 65 20 69 6e 20 74 68 65 20 6f 75 74 65  ause in the oute
1c702 72 20 71 75 65 72 79 2e 0a 20 20 2a 2f 0a 0a 20  r query..  */.. 
1c703 20 2f 2a 20 4d 6f 76 65 20 61 6c 6c 20 6f 66 20   /* Move all of 
1c704 74 68 65 20 46 52 4f 4d 20 65 6c 65 6d 65 6e 74  the FROM element
1c705 73 20 6f 66 20 74 68 65 20 73 75 62 71 75 65 72  s of the subquer
1c706 79 20 69 6e 74 6f 20 74 68 65 0a 20 20 2a 2a 20  y into the.  ** 
1c707 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
1c708 6f 66 20 74 68 65 20 6f 75 74 65 72 20 71 75 65  of the outer que
1c709 72 79 2e 20 20 42 65 66 6f 72 65 20 64 6f 69 6e  ry.  Before doin
1c70a 67 20 74 68 69 73 2c 20 72 65 6d 65 6d 62 65 72  g this, remember
1c70b 0a 20 20 2a 2a 20 74 68 65 20 63 75 72 73 6f 72  .  ** the cursor
1c70c 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20   number for the 
1c70d 6f 72 69 67 69 6e 61 6c 20 6f 75 74 65 72 20 71  original outer q
1c70e 75 65 72 79 20 46 52 4f 4d 20 65 6c 65 6d 65 6e  uery FROM elemen
1c70f 74 20 69 6e 0a 20 20 2a 2a 20 69 50 61 72 65 6e  t in.  ** iParen
1c710 74 2e 20 20 54 68 65 20 69 50 61 72 65 6e 74 20  t.  The iParent 
1c711 63 75 72 73 6f 72 20 77 69 6c 6c 20 6e 65 76 65  cursor will neve
1c712 72 20 62 65 20 75 73 65 64 2e 20 20 53 75 62 73  r be used.  Subs
1c713 65 71 75 65 6e 74 20 63 6f 64 65 0a 20 20 2a 2a  equent code.  **
1c714 20 77 69 6c 6c 20 73 63 61 6e 20 65 78 70 72 65   will scan expre
1c715 73 73 69 6f 6e 73 20 6c 6f 6f 6b 69 6e 67 20 66  ssions looking f
1c716 6f 72 20 69 50 61 72 65 6e 74 20 72 65 66 65 72  or iParent refer
1c717 65 6e 63 65 73 20 61 6e 64 20 72 65 70 6c 61 63  ences and replac
1c718 65 0a 20 20 2a 2a 20 74 68 6f 73 65 20 72 65 66  e.  ** those ref
1c719 65 72 65 6e 63 65 73 20 77 69 74 68 20 65 78 70  erences with exp
1c71a 72 65 73 73 69 6f 6e 73 20 74 68 61 74 20 72 65  ressions that re
1c71b 73 6f 6c 76 65 20 74 6f 20 74 68 65 20 73 75 62  solve to the sub
1c71c 71 75 65 72 79 20 46 52 4f 4d 0a 20 20 2a 2a 20  query FROM.  ** 
1c71d 65 6c 65 6d 65 6e 74 73 20 77 65 20 61 72 65 20  elements we are 
1c71e 6e 6f 77 20 63 6f 70 79 69 6e 67 20 69 6e 2e 0a  now copying in..
1c71f 20 20 2a 2f 0a 20 20 69 50 61 72 65 6e 74 20 3d    */.  iParent =
1c720 20 70 53 75 62 69 74 65 6d 2d 3e 69 43 75 72 73   pSubitem->iCurs
1c721 6f 72 3b 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20  or;.  {.    int 
1c722 6e 53 75 62 53 72 63 20 3d 20 70 53 75 62 53 72  nSubSrc = pSubSr
1c723 63 2d 3e 6e 53 72 63 3b 0a 20 20 20 20 69 6e 74  c->nSrc;.    int
1c724 20 6a 6f 69 6e 74 79 70 65 20 3d 20 70 53 75 62   jointype = pSub
1c725 69 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65 3b 0a  item->jointype;.
1c726 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65  .    sqlite3Dele
1c727 74 65 54 61 62 6c 65 28 70 53 75 62 69 74 65 6d  teTable(pSubitem
1c728 2d 3e 70 54 61 62 29 3b 0a 20 20 20 20 73 71 6c  ->pTab);.    sql
1c729 69 74 65 46 72 65 65 28 70 53 75 62 69 74 65 6d  iteFree(pSubitem
1c72a 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20  ->zDatabase);.  
1c72b 20 20 73 71 6c 69 74 65 46 72 65 65 28 70 53 75    sqliteFree(pSu
1c72c 62 69 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  bitem->zName);. 
1c72d 20 20 20 73 71 6c 69 74 65 46 72 65 65 28 70 53     sqliteFree(pS
1c72e 75 62 69 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b  ubitem->zAlias);
1c72f 0a 20 20 20 20 69 66 28 20 6e 53 75 62 53 72 63  .    if( nSubSrc
1c730 3e 31 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  >1 ){.      int 
1c731 65 78 74 72 61 20 3d 20 6e 53 75 62 53 72 63 20  extra = nSubSrc 
1c732 2d 20 31 3b 0a 20 20 20 20 20 20 66 6f 72 28 69  - 1;.      for(i
1c733 3d 31 3b 20 69 3c 6e 53 75 62 53 72 63 3b 20 69  =1; i<nSubSrc; i
1c734 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 53 72  ++){.        pSr
1c735 63 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69  c = sqlite3SrcLi
1c736 73 74 41 70 70 65 6e 64 28 70 53 72 63 2c 20 30  stAppend(pSrc, 0
1c737 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  , 0);.      }.  
1c738 20 20 20 20 70 2d 3e 70 53 72 63 20 3d 20 70 53      p->pSrc = pS
1c739 72 63 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d  rc;.      for(i=
1c73a 70 53 72 63 2d 3e 6e 53 72 63 2d 31 3b 20 69 2d  pSrc->nSrc-1; i-
1c73b 65 78 74 72 61 3e 3d 69 46 72 6f 6d 3b 20 69 2d  extra>=iFrom; i-
1c73c 2d 29 7b 0a 20 20 20 20 20 20 20 20 70 53 72 63  -){.        pSrc
1c73d 2d 3e 61 5b 69 5d 20 3d 20 70 53 72 63 2d 3e 61  ->a[i] = pSrc->a
1c73e 5b 69 2d 65 78 74 72 61 5d 3b 0a 20 20 20 20 20  [i-extra];.     
1c73f 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72   }.    }.    for
1c740 28 69 3d 30 3b 20 69 3c 6e 53 75 62 53 72 63 3b  (i=0; i<nSubSrc;
1c741 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 53 72   i++){.      pSr
1c742 63 2d 3e 61 5b 69 2b 69 46 72 6f 6d 5d 20 3d 20  c->a[i+iFrom] = 
1c743 70 53 75 62 53 72 63 2d 3e 61 5b 69 5d 3b 0a 20  pSubSrc->a[i];. 
1c744 20 20 20 20 20 6d 65 6d 73 65 74 28 26 70 53 75       memset(&pSu
1c745 62 53 72 63 2d 3e 61 5b 69 5d 2c 20 30 2c 20 73  bSrc->a[i], 0, s
1c746 69 7a 65 6f 66 28 70 53 75 62 53 72 63 2d 3e 61  izeof(pSubSrc->a
1c747 5b 69 5d 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20  [i]));.    }.   
1c748 20 70 53 72 63 2d 3e 61 5b 69 46 72 6f 6d 5d 2e   pSrc->a[iFrom].
1c749 6a 6f 69 6e 74 79 70 65 20 3d 20 6a 6f 69 6e 74  jointype = joint
1c74a 79 70 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e  ype;.  }..  /* N
1c74b 6f 77 20 62 65 67 69 6e 20 73 75 62 73 74 69 74  ow begin substit
1c74c 75 74 69 6e 67 20 73 75 62 71 75 65 72 79 20 72  uting subquery r
1c74d 65 73 75 6c 74 20 73 65 74 20 65 78 70 72 65 73  esult set expres
1c74e 73 69 6f 6e 73 20 66 6f 72 20 0a 20 20 2a 2a 20  sions for .  ** 
1c74f 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68  references to th
1c750 65 20 69 50 61 72 65 6e 74 20 69 6e 20 74 68 65  e iParent in the
1c751 20 6f 75 74 65 72 20 71 75 65 72 79 2e 0a 20 20   outer query..  
1c752 2a 2a 20 0a 20 20 2a 2a 20 45 78 61 6d 70 6c 65  ** .  ** Example
1c753 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 53 45  :.  **.  **   SE
1c754 4c 45 43 54 20 61 2b 35 2c 20 62 2a 31 30 20 46  LECT a+5, b*10 F
1c755 52 4f 4d 20 28 53 45 4c 45 43 54 20 78 2a 33 20  ROM (SELECT x*3 
1c756 41 53 20 61 2c 20 79 2b 31 30 20 41 53 20 62 20  AS a, y+10 AS b 
1c757 46 52 4f 4d 20 74 31 29 20 57 48 45 52 45 20 61  FROM t1) WHERE a
1c758 3e 62 3b 0a 20 20 2a 2a 20 20 20 5c 20 20 20 20  >b;.  **   \    
1c759 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c75a 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 20   \_____________ 
1c75b 73 75 62 71 75 65 72 79 20 5f 5f 5f 5f 5f 5f 5f  subquery _______
1c75c 5f 5f 5f 2f 20 20 20 20 20 20 20 20 20 20 2f 0a  ___/          /.
1c75d 20 20 2a 2a 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f    **    \_______
1c75e 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 20 6f  ______________ o
1c75f 75 74 65 72 20 71 75 65 72 79 20 5f 5f 5f 5f 5f  uter query _____
1c760 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
1c761 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 20 20 2a 2a 0a  _________/.  **.
1c762 20 20 2a 2a 20 57 65 20 6c 6f 6f 6b 20 61 74 20    ** We look at 
1c763 65 76 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e  every expression
1c764 20 69 6e 20 74 68 65 20 6f 75 74 65 72 20 71 75   in the outer qu
1c765 65 72 79 20 61 6e 64 20 65 76 65 72 79 20 70 6c  ery and every pl
1c766 61 63 65 20 77 65 20 73 65 65 0a 20 20 2a 2a 20  ace we see.  ** 
1c767 22 61 22 20 77 65 20 73 75 62 73 74 69 74 75 74  "a" we substitut
1c768 65 20 22 78 2a 33 22 20 61 6e 64 20 65 76 65 72  e "x*3" and ever
1c769 79 20 70 6c 61 63 65 20 77 65 20 73 65 65 20 22  y place we see "
1c76a 62 22 20 77 65 20 73 75 62 73 74 69 74 75 74 65  b" we substitute
1c76b 20 22 79 2b 31 30 22 2e 0a 20 20 2a 2f 0a 20 20   "y+10"..  */.  
1c76c 70 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73  pList = p->pELis
1c76d 74 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  t;.  for(i=0; i<
1c76e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b  pList->nExpr; i+
1c76f 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45  +){.    Expr *pE
1c770 78 70 72 3b 0a 20 20 20 20 69 66 28 20 70 4c 69  xpr;.    if( pLi
1c771 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 3d 3d  st->a[i].zName==
1c772 30 20 26 26 20 28 70 45 78 70 72 20 3d 20 70 4c  0 && (pExpr = pL
1c773 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29  ist->a[i].pExpr)
1c774 2d 3e 73 70 61 6e 2e 7a 21 3d 30 20 29 7b 0a 20  ->span.z!=0 ){. 
1c775 20 20 20 20 20 70 4c 69 73 74 2d 3e 61 5b 69 5d       pList->a[i]
1c776 2e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 53  .zName = sqliteS
1c777 74 72 4e 44 75 70 28 28 63 68 61 72 2a 29 70 45  trNDup((char*)pE
1c778 78 70 72 2d 3e 73 70 61 6e 2e 7a 2c 20 70 45 78  xpr->span.z, pEx
1c779 70 72 2d 3e 73 70 61 6e 2e 6e 29 3b 0a 20 20 20  pr->span.n);.   
1c77a 20 7d 0a 20 20 7d 0a 20 20 73 75 62 73 74 45 78   }.  }.  substEx
1c77b 70 72 4c 69 73 74 28 70 2d 3e 70 45 4c 69 73 74  prList(p->pEList
1c77c 2c 20 69 50 61 72 65 6e 74 2c 20 70 53 75 62 2d  , iParent, pSub-
1c77d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 69 66 28 20  >pEList);.  if( 
1c77e 69 73 41 67 67 20 29 7b 0a 20 20 20 20 73 75 62  isAgg ){.    sub
1c77f 73 74 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 47  stExprList(p->pG
1c780 72 6f 75 70 42 79 2c 20 69 50 61 72 65 6e 74 2c  roupBy, iParent,
1c781 20 70 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a   pSub->pEList);.
1c782 20 20 20 20 73 75 62 73 74 45 78 70 72 28 70 2d      substExpr(p-
1c783 3e 70 48 61 76 69 6e 67 2c 20 69 50 61 72 65 6e  >pHaving, iParen
1c784 74 2c 20 70 53 75 62 2d 3e 70 45 4c 69 73 74 29  t, pSub->pEList)
1c785 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 75 62  ;.  }.  if( pSub
1c786 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20  ->pOrderBy ){.  
1c787 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4f 72    assert( p->pOr
1c788 64 65 72 42 79 3d 3d 30 20 29 3b 0a 20 20 20 20  derBy==0 );.    
1c789 70 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 70 53  p->pOrderBy = pS
1c78a 75 62 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20  ub->pOrderBy;.  
1c78b 20 20 70 53 75 62 2d 3e 70 4f 72 64 65 72 42 79    pSub->pOrderBy
1c78c 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66   = 0;.  }else if
1c78d 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b  ( p->pOrderBy ){
1c78e 0a 20 20 20 20 73 75 62 73 74 45 78 70 72 4c 69  .    substExprLi
1c78f 73 74 28 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20  st(p->pOrderBy, 
1c790 69 50 61 72 65 6e 74 2c 20 70 53 75 62 2d 3e 70  iParent, pSub->p
1c791 45 4c 69 73 74 29 3b 0a 20 20 7d 0a 20 20 69 66  EList);.  }.  if
1c792 28 20 70 53 75 62 2d 3e 70 57 68 65 72 65 20 29  ( pSub->pWhere )
1c793 7b 0a 20 20 20 20 70 57 68 65 72 65 20 3d 20 73  {.    pWhere = s
1c794 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 70 53  qlite3ExprDup(pS
1c795 75 62 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20 7d  ub->pWhere);.  }
1c796 65 6c 73 65 7b 0a 20 20 20 20 70 57 68 65 72 65  else{.    pWhere
1c797 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20   = 0;.  }.  if( 
1c798 73 75 62 71 75 65 72 79 49 73 41 67 67 20 29 7b  subqueryIsAgg ){
1c799 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e  .    assert( p->
1c79a 70 48 61 76 69 6e 67 3d 3d 30 20 29 3b 0a 20 20  pHaving==0 );.  
1c79b 20 20 70 2d 3e 70 48 61 76 69 6e 67 20 3d 20 70    p->pHaving = p
1c79c 2d 3e 70 57 68 65 72 65 3b 0a 20 20 20 20 70 2d  ->pWhere;.    p-
1c79d 3e 70 57 68 65 72 65 20 3d 20 70 57 68 65 72 65  >pWhere = pWhere
1c79e 3b 0a 20 20 20 20 73 75 62 73 74 45 78 70 72 28  ;.    substExpr(
1c79f 70 2d 3e 70 48 61 76 69 6e 67 2c 20 69 50 61 72  p->pHaving, iPar
1c7a0 65 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c 69 73  ent, pSub->pELis
1c7a1 74 29 3b 0a 20 20 20 20 70 2d 3e 70 48 61 76 69  t);.    p->pHavi
1c7a2 6e 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ng = sqlite3Expr
1c7a3 41 6e 64 28 70 2d 3e 70 48 61 76 69 6e 67 2c 20  And(p->pHaving, 
1c7a4 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 70  sqlite3ExprDup(p
1c7a5 53 75 62 2d 3e 70 48 61 76 69 6e 67 29 29 3b 0a  Sub->pHaving));.
1c7a6 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70      assert( p->p
1c7a7 47 72 6f 75 70 42 79 3d 3d 30 20 29 3b 0a 20 20  GroupBy==0 );.  
1c7a8 20 20 70 2d 3e 70 47 72 6f 75 70 42 79 20 3d 20    p->pGroupBy = 
1c7a9 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
1c7aa 75 70 28 70 53 75 62 2d 3e 70 47 72 6f 75 70 42  up(pSub->pGroupB
1c7ab 79 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  y);.  }else{.   
1c7ac 20 73 75 62 73 74 45 78 70 72 28 70 2d 3e 70 57   substExpr(p->pW
1c7ad 68 65 72 65 2c 20 69 50 61 72 65 6e 74 2c 20 70  here, iParent, p
1c7ae 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20  Sub->pEList);.  
1c7af 20 20 70 2d 3e 70 57 68 65 72 65 20 3d 20 73 71    p->pWhere = sq
1c7b0 6c 69 74 65 33 45 78 70 72 41 6e 64 28 70 2d 3e  lite3ExprAnd(p->
1c7b1 70 57 68 65 72 65 2c 20 70 57 68 65 72 65 29 3b  pWhere, pWhere);
1c7b2 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 66  .  }..  /* The f
1c7b3 6c 61 74 74 65 6e 65 64 20 71 75 65 72 79 20 69  lattened query i
1c7b4 73 20 64 69 73 74 69 6e 63 74 20 69 66 20 65 69  s distinct if ei
1c7b5 74 68 65 72 20 74 68 65 20 69 6e 6e 65 72 20 6f  ther the inner o
1c7b6 72 20 74 68 65 0a 20 20 2a 2a 20 6f 75 74 65 72  r the.  ** outer
1c7b7 20 71 75 65 72 79 20 69 73 20 64 69 73 74 69 6e   query is distin
1c7b8 63 74 2e 20 0a 20 20 2a 2f 0a 20 20 70 2d 3e 69  ct. .  */.  p->i
1c7b9 73 44 69 73 74 69 6e 63 74 20 3d 20 70 2d 3e 69  sDistinct = p->i
1c7ba 73 44 69 73 74 69 6e 63 74 20 7c 7c 20 70 53 75  sDistinct || pSu
1c7bb 62 2d 3e 69 73 44 69 73 74 69 6e 63 74 3b 0a 0a  b->isDistinct;..
1c7bc 20 20 2f 2a 0a 20 20 2a 2a 20 53 45 4c 45 43 54    /*.  ** SELECT
1c7bd 20 2e 2e 2e 20 46 52 4f 4d 20 28 53 45 4c 45 43   ... FROM (SELEC
1c7be 54 20 2e 2e 2e 20 4c 49 4d 49 54 20 61 20 4f 46  T ... LIMIT a OF
1c7bf 46 53 45 54 20 62 29 20 4c 49 4d 49 54 20 78 20  FSET b) LIMIT x 
1c7c0 4f 46 46 53 45 54 20 79 3b 0a 20 20 2a 2a 0a 20  OFFSET y;.  **. 
1c7c1 20 2a 2a 20 4f 6e 65 20 69 73 20 74 65 6d 70 74   ** One is tempt
1c7c2 65 64 20 74 6f 20 74 72 79 20 74 6f 20 61 64 64  ed to try to add
1c7c3 20 61 20 61 6e 64 20 62 20 74 6f 20 63 6f 6d 62   a and b to comb
1c7c4 69 6e 65 20 74 68 65 20 6c 69 6d 69 74 73 2e 20  ine the limits. 
1c7c5 20 42 75 74 20 74 68 69 73 0a 20 20 2a 2a 20 64   But this.  ** d
1c7c6 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 69 66 20  oes not work if 
1c7c7 65 69 74 68 65 72 20 6c 69 6d 69 74 20 69 73 20  either limit is 
1c7c8 6e 65 67 61 74 69 76 65 2e 0a 20 20 2a 2f 0a 20  negative..  */. 
1c7c9 20 69 66 28 20 70 53 75 62 2d 3e 70 4c 69 6d 69   if( pSub->pLimi
1c7ca 74 20 29 7b 0a 20 20 20 20 70 2d 3e 70 4c 69 6d  t ){.    p->pLim
1c7cb 69 74 20 3d 20 70 53 75 62 2d 3e 70 4c 69 6d 69  it = pSub->pLimi
1c7cc 74 3b 0a 20 20 20 20 70 53 75 62 2d 3e 70 4c 69  t;.    pSub->pLi
1c7cd 6d 69 74 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20  mit = 0;.  }..  
1c7ce 2f 2a 20 46 69 6e 69 61 6c 6c 79 2c 20 64 65 6c  /* Finially, del
1c7cf 65 74 65 20 77 68 61 74 20 69 73 20 6c 65 66 74  ete what is left
1c7d0 20 6f 66 20 74 68 65 20 73 75 62 71 75 65 72 79   of the subquery
1c7d1 20 61 6e 64 20 72 65 74 75 72 6e 0a 20 20 2a 2a   and return.  **
1c7d2 20 73 75 63 63 65 73 73 2e 0a 20 20 2a 2f 0a 20   success..  */. 
1c7d3 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
1c7d4 6c 65 74 65 28 70 53 75 62 29 3b 0a 20 20 72 65  lete(pSub);.  re
1c7d5 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66  turn 1;.}.#endif
1c7d6 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1c7d7 56 49 45 57 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41  VIEW */../*.** A
1c7d8 6e 61 6c 79 7a 65 20 74 68 65 20 53 45 4c 45 43  nalyze the SELEC
1c7d9 54 20 73 74 61 74 65 6d 65 6e 74 20 70 61 73 73  T statement pass
1c7da 65 64 20 69 6e 20 61 73 20 61 6e 20 61 72 67 75  ed in as an argu
1c7db 6d 65 6e 74 20 74 6f 20 73 65 65 20 69 66 20 69  ment to see if i
1c7dc 74 0a 2a 2a 20 69 73 20 61 20 73 69 6d 70 6c 65  t.** is a simple
1c7dd 20 6d 69 6e 28 29 20 6f 72 20 6d 61 78 28 29 20   min() or max() 
1c7de 71 75 65 72 79 2e 20 20 49 66 20 69 74 20 69 73  query.  If it is
1c7df 20 61 6e 64 20 74 68 69 73 20 71 75 65 72 79 20   and this query 
1c7e0 63 61 6e 20 62 65 0a 2a 2a 20 73 61 74 69 73 66  can be.** satisf
1c7e1 69 65 64 20 75 73 69 6e 67 20 61 20 73 69 6e 67  ied using a sing
1c7e2 6c 65 20 73 65 65 6b 20 74 6f 20 74 68 65 20 62  le seek to the b
1c7e3 65 67 69 6e 6e 69 6e 67 20 6f 72 20 65 6e 64 20  eginning or end 
1c7e4 6f 66 20 61 6e 20 69 6e 64 65 78 2c 0a 2a 2a 20  of an index,.** 
1c7e5 74 68 65 6e 20 67 65 6e 65 72 61 74 65 20 74 68  then generate th
1c7e6 65 20 63 6f 64 65 20 66 6f 72 20 74 68 69 73 20  e code for this 
1c7e7 53 45 4c 45 43 54 20 61 6e 64 20 72 65 74 75 72  SELECT and retur
1c7e8 6e 20 31 2e 20 20 49 66 20 74 68 69 73 20 69 73  n 1.  If this is
1c7e9 20 6e 6f 74 20 61 20 0a 2a 2a 20 73 69 6d 70 6c   not a .** simpl
1c7ea 65 20 6d 69 6e 28 29 20 6f 72 20 6d 61 78 28 29  e min() or max()
1c7eb 20 71 75 65 72 79 2c 20 74 68 65 6e 20 72 65 74   query, then ret
1c7ec 75 72 6e 20 30 3b 0a 2a 2a 0a 2a 2a 20 41 20 73  urn 0;.**.** A s
1c7ed 69 6d 70 6c 79 20 6d 69 6e 28 29 20 6f 72 20 6d  imply min() or m
1c7ee 61 78 28 29 20 71 75 65 72 79 20 6c 6f 6f 6b 73  ax() query looks
1c7ef 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a   like this:.**.*
1c7f0 2a 20 20 20 20 53 45 4c 45 43 54 20 6d 69 6e 28  *    SELECT min(
1c7f1 61 29 20 46 52 4f 4d 20 74 61 62 6c 65 3b 0a 2a  a) FROM table;.*
1c7f2 2a 20 20 20 20 53 45 4c 45 43 54 20 6d 61 78 28  *    SELECT max(
1c7f3 61 29 20 46 52 4f 4d 20 74 61 62 6c 65 3b 0a 2a  a) FROM table;.*
1c7f4 2a 0a 2a 2a 20 54 68 65 20 71 75 65 72 79 20 6d  *.** The query m
1c7f5 61 79 20 68 61 76 65 20 6f 6e 6c 79 20 61 20 73  ay have only a s
1c7f6 69 6e 67 6c 65 20 74 61 62 6c 65 20 69 6e 20 69  ingle table in i
1c7f7 74 73 20 46 52 4f 4d 20 61 72 67 75 6d 65 6e 74  ts FROM argument
1c7f8 2e 20 20 54 68 65 72 65 0a 2a 2a 20 63 61 6e 20  .  There.** can 
1c7f9 62 65 20 6e 6f 20 47 52 4f 55 50 20 42 59 20 6f  be no GROUP BY o
1c7fa 72 20 48 41 56 49 4e 47 20 6f 72 20 57 48 45 52  r HAVING or WHER
1c7fb 45 20 63 6c 61 75 73 65 73 2e 20 20 54 68 65 20  E clauses.  The 
1c7fc 72 65 73 75 6c 74 20 73 65 74 20 6d 75 73 74 0a  result set must.
1c7fd 2a 2a 20 62 65 20 74 68 65 20 6d 69 6e 28 29 20  ** be the min() 
1c7fe 6f 72 20 6d 61 78 28 29 20 6f 66 20 61 20 73 69  or max() of a si
1c7ff 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 6f 66 20 74  ngle column of t
1c800 68 65 20 74 61 62 6c 65 2e 20 20 54 68 65 20 63  he table.  The c
1c801 6f 6c 75 6d 6e 0a 2a 2a 20 69 6e 20 74 68 65 20  olumn.** in the 
1c802 6d 69 6e 28 29 20 6f 72 20 6d 61 78 28 29 20 66  min() or max() f
1c803 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20  unction must be 
1c804 69 6e 64 65 78 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  indexed..**.** T
1c805 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f  he parameters to
1c806 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 72   this routine ar
1c807 65 20 74 68 65 20 73 61 6d 65 20 61 73 20 66 6f  e the same as fo
1c808 72 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28  r sqlite3Select(
1c809 29 2e 0a 2a 2a 20 53 65 65 20 74 68 65 20 68 65  )..** See the he
1c80a 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20  ader comment on 
1c80b 74 68 61 74 20 72 6f 75 74 69 6e 65 20 66 6f 72  that routine for
1c80c 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f   additional info
1c80d 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  rmation..*/.stat
1c80e 69 63 20 69 6e 74 20 73 69 6d 70 6c 65 4d 69 6e  ic int simpleMin
1c80f 4d 61 78 51 75 65 72 79 28 50 61 72 73 65 20 2a  MaxQuery(Parse *
1c810 70 50 61 72 73 65 2c 20 53 65 6c 65 63 74 20 2a  pParse, Select *
1c811 70 2c 20 69 6e 74 20 65 44 65 73 74 2c 20 69 6e  p, int eDest, in
1c812 74 20 69 50 61 72 6d 29 7b 0a 20 20 45 78 70 72  t iParm){.  Expr
1c813 20 2a 70 45 78 70 72 3b 0a 20 20 69 6e 74 20 69   *pExpr;.  int i
1c814 43 6f 6c 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54  Col;.  Table *pT
1c815 61 62 3b 0a 20 20 49 6e 64 65 78 20 2a 70 49 64  ab;.  Index *pId
1c816 78 3b 0a 20 20 69 6e 74 20 62 61 73 65 3b 0a 20  x;.  int base;. 
1c817 20 56 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20   Vdbe *v;.  int 
1c818 73 65 65 6b 4f 70 3b 0a 20 20 45 78 70 72 4c 69  seekOp;.  ExprLi
1c819 73 74 20 2a 70 45 4c 69 73 74 2c 20 2a 70 4c 69  st *pEList, *pLi
1c81a 73 74 2c 20 65 4c 69 73 74 3b 0a 20 20 73 74 72  st, eList;.  str
1c81b 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
1c81c 6d 20 65 4c 69 73 74 49 74 65 6d 3b 0a 20 20 53  m eListItem;.  S
1c81d 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 0a 20 20  rcList *pSrc;.  
1c81e 69 6e 74 20 62 72 6b 3b 0a 20 20 69 6e 74 20 69  int brk;.  int i
1c81f 44 62 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20  Db;..  /* Check 
1c820 74 6f 20 73 65 65 20 69 66 20 74 68 69 73 20 71  to see if this q
1c821 75 65 72 79 20 69 73 20 61 20 73 69 6d 70 6c 65  uery is a simple
1c822 20 6d 69 6e 28 29 20 6f 72 20 6d 61 78 28 29 20   min() or max() 
1c823 71 75 65 72 79 2e 20 20 52 65 74 75 72 6e 0a 20  query.  Return. 
1c824 20 2a 2a 20 7a 65 72 6f 20 69 66 20 69 74 20 69   ** zero if it i
1c825 73 20 20 6e 6f 74 2e 0a 20 20 2a 2f 0a 20 20 69  s  not..  */.  i
1c826 66 28 20 70 2d 3e 70 47 72 6f 75 70 42 79 20 7c  f( p->pGroupBy |
1c827 7c 20 70 2d 3e 70 48 61 76 69 6e 67 20 7c 7c 20  | p->pHaving || 
1c828 70 2d 3e 70 57 68 65 72 65 20 29 20 72 65 74 75  p->pWhere ) retu
1c829 72 6e 20 30 3b 0a 20 20 70 53 72 63 20 3d 20 70  rn 0;.  pSrc = p
1c82a 2d 3e 70 53 72 63 3b 0a 20 20 69 66 28 20 70 53  ->pSrc;.  if( pS
1c82b 72 63 2d 3e 6e 53 72 63 21 3d 31 20 29 20 72 65  rc->nSrc!=1 ) re
1c82c 74 75 72 6e 20 30 3b 0a 20 20 70 45 4c 69 73 74  turn 0;.  pEList
1c82d 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20   = p->pEList;.  
1c82e 69 66 28 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70  if( pEList->nExp
1c82f 72 21 3d 31 20 29 20 72 65 74 75 72 6e 20 30 3b  r!=1 ) return 0;
1c830 0a 20 20 70 45 78 70 72 20 3d 20 70 45 4c 69 73  .  pExpr = pELis
1c831 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20  t->a[0].pExpr;. 
1c832 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d   if( pExpr->op!=
1c833 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20  TK_AGG_FUNCTION 
1c834 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4c  ) return 0;.  pL
1c835 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69  ist = pExpr->pLi
1c836 73 74 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d  st;.  if( pList=
1c837 3d 30 20 7c 7c 20 70 4c 69 73 74 2d 3e 6e 45 78  =0 || pList->nEx
1c838 70 72 21 3d 31 20 29 20 72 65 74 75 72 6e 20 30  pr!=1 ) return 0
1c839 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 74  ;.  if( pExpr->t
1c83a 6f 6b 65 6e 2e 6e 21 3d 33 20 29 20 72 65 74 75  oken.n!=3 ) retu
1c83b 72 6e 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69  rn 0;.  if( sqli
1c83c 74 65 33 53 74 72 4e 49 43 6d 70 28 28 63 68 61  te3StrNICmp((cha
1c83d 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e  r*)pExpr->token.
1c83e 7a 2c 22 6d 69 6e 22 2c 33 29 3d 3d 30 20 29 7b  z,"min",3)==0 ){
1c83f 0a 20 20 20 20 73 65 65 6b 4f 70 20 3d 20 4f 50  .    seekOp = OP
1c840 5f 52 65 77 69 6e 64 3b 0a 20 20 7d 65 6c 73 65  _Rewind;.  }else
1c841 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e   if( sqlite3StrN
1c842 49 43 6d 70 28 28 63 68 61 72 2a 29 70 45 78 70  ICmp((char*)pExp
1c843 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 22 6d 61 78 22  r->token.z,"max"
1c844 2c 33 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 65  ,3)==0 ){.    se
1c845 65 6b 4f 70 20 3d 20 4f 50 5f 4c 61 73 74 3b 0a  ekOp = OP_Last;.
1c846 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
1c847 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 45 78  urn 0;.  }.  pEx
1c848 70 72 20 3d 20 70 4c 69 73 74 2d 3e 61 5b 30 5d  pr = pList->a[0]
1c849 2e 70 45 78 70 72 3b 0a 20 20 69 66 28 20 70 45  .pExpr;.  if( pE
1c84a 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55  xpr->op!=TK_COLU
1c84b 4d 4e 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  MN ) return 0;. 
1c84c 20 69 43 6f 6c 20 3d 20 70 45 78 70 72 2d 3e 69   iCol = pExpr->i
1c84d 43 6f 6c 75 6d 6e 3b 0a 20 20 70 54 61 62 20 3d  Column;.  pTab =
1c84e 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 61 62   pSrc->a[0].pTab
1c84f 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6f 70 74  ;..  /* This opt
1c850 69 6d 69 7a 61 74 69 6f 6e 20 63 61 6e 6e 6f 74  imization cannot
1c851 20 62 65 20 75 73 65 64 20 77 69 74 68 20 76 69   be used with vi
1c852 72 74 75 61 6c 20 74 61 62 6c 65 73 2e 20 2a 2f  rtual tables. */
1c853 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c  .  if( IsVirtual
1c854 28 70 54 61 62 29 20 29 20 72 65 74 75 72 6e 20  (pTab) ) return 
1c855 30 3b 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 67  0;..  /* If we g
1c856 65 74 20 74 6f 20 68 65 72 65 2c 20 69 74 20 6d  et to here, it m
1c857 65 61 6e 73 20 74 68 65 20 71 75 65 72 79 20 69  eans the query i
1c858 73 20 6f 66 20 74 68 65 20 63 6f 72 72 65 63 74  s of the correct
1c859 20 66 6f 72 6d 2e 0a 20 20 2a 2a 20 43 68 65 63   form..  ** Chec
1c85a 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 77  k to make sure w
1c85b 65 20 68 61 76 65 20 61 6e 20 69 6e 64 65 78 20  e have an index 
1c85c 61 6e 64 20 6d 61 6b 65 20 70 49 64 78 20 70 6f  and make pIdx po
1c85d 69 6e 74 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20  int to the.  ** 
1c85e 61 70 70 72 6f 70 72 69 61 74 65 20 69 6e 64 65  appropriate inde
1c85f 78 2e 20 20 49 66 20 74 68 65 20 6d 69 6e 28 29  x.  If the min()
1c860 20 6f 72 20 6d 61 78 28 29 20 69 73 20 6f 6e 20   or max() is on 
1c861 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  an INTEGER PRIMA
1c862 52 59 0a 20 20 2a 2a 20 6b 65 79 20 63 6f 6c 75  RY.  ** key colu
1c863 6d 6e 2c 20 6e 6f 20 69 6e 64 65 78 20 69 73 20  mn, no index is 
1c864 6e 65 63 65 73 73 61 72 79 20 73 6f 20 73 65 74  necessary so set
1c865 20 70 49 64 78 20 74 6f 20 4e 55 4c 4c 2e 20 20   pIdx to NULL.  
1c866 49 66 20 6e 6f 0a 20 20 2a 2a 20 75 73 61 62 6c  If no.  ** usabl
1c867 65 20 69 6e 64 65 78 20 69 73 20 66 6f 75 6e 64  e index is found
1c868 2c 20 72 65 74 75 72 6e 20 30 2e 0a 20 20 2a 2f  , return 0..  */
1c869 0a 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b  .  if( iCol<0 ){
1c86a 0a 20 20 20 20 70 49 64 78 20 3d 20 30 3b 0a 20  .    pIdx = 0;. 
1c86b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 43 6f 6c 6c   }else{.    Coll
1c86c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 73 71 6c  Seq *pColl = sql
1c86d 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28  ite3ExprCollSeq(
1c86e 70 50 61 72 73 65 2c 20 70 45 78 70 72 29 3b 0a  pParse, pExpr);.
1c86f 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 3d 3d 30      if( pColl==0
1c870 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20   ) return 0;.   
1c871 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e   for(pIdx=pTab->
1c872 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49  pIndex; pIdx; pI
1c873 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b  dx=pIdx->pNext){
1c874 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1c875 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 3d 31 20  Idx->nColumn>=1 
1c876 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 64  );.      if( pId
1c877 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d  x->aiColumn[0]==
1c878 69 43 6f 6c 20 26 26 20 0a 20 20 20 20 20 20 20  iCol && .       
1c879 20 20 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72     0==sqlite3Str
1c87a 49 43 6d 70 28 70 49 64 78 2d 3e 61 7a 43 6f 6c  ICmp(pIdx->azCol
1c87b 6c 5b 30 5d 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61  l[0], pColl->zNa
1c87c 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 62  me) ){.        b
1c87d 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
1c87e 20 20 7d 0a 20 20 20 20 69 66 28 20 70 49 64 78    }.    if( pIdx
1c87f 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
1c880 20 20 7d 0a 0a 20 20 2f 2a 20 49 64 65 6e 74 69    }..  /* Identi
1c881 66 79 20 63 6f 6c 75 6d 6e 20 74 79 70 65 73 20  fy column types 
1c882 69 66 20 77 65 20 77 69 6c 6c 20 62 65 20 75 73  if we will be us
1c883 69 6e 67 20 74 68 65 20 63 61 6c 6c 62 61 63 6b  ing the callback
1c884 2e 20 20 54 68 69 73 0a 20 20 2a 2a 20 73 74 65  .  This.  ** ste
1c885 70 20 69 73 20 73 6b 69 70 70 65 64 20 69 66 20  p is skipped if 
1c886 74 68 65 20 6f 75 74 70 75 74 20 69 73 20 67 6f  the output is go
1c887 69 6e 67 20 74 6f 20 61 20 74 61 62 6c 65 20 6f  ing to a table o
1c888 72 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e  r a memory cell.
1c889 0a 20 20 2a 2a 20 54 68 65 20 63 6f 6c 75 6d 6e  .  ** The column
1c88a 20 6e 61 6d 65 73 20 68 61 76 65 20 61 6c 72 65   names have alre
1c88b 61 64 79 20 62 65 65 6e 20 67 65 6e 65 72 61 74  ady been generat
1c88c 65 64 20 69 6e 20 74 68 65 20 63 61 6c 6c 69 6e  ed in the callin
1c88d 67 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2f  g function..  */
1c88e 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65  .  v = sqlite3Ge
1c88f 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
1c890 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75   if( v==0 ) retu
1c891 72 6e 20 30 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  rn 0;..  /* If t
1c892 68 65 20 6f 75 74 70 75 74 20 69 73 20 64 65 73  he output is des
1c893 74 69 6e 65 64 20 66 6f 72 20 61 20 74 65 6d 70  tined for a temp
1c894 6f 72 61 72 79 20 74 61 62 6c 65 2c 20 6f 70 65  orary table, ope
1c895 6e 20 74 68 61 74 20 74 61 62 6c 65 2e 0a 20 20  n that table..  
1c896 2a 2f 0a 20 20 69 66 28 20 65 44 65 73 74 3d 3d  */.  if( eDest==
1c897 53 52 54 5f 45 70 68 65 6d 54 61 62 20 29 7b 0a  SRT_EphemTab ){.
1c898 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1c899 64 64 4f 70 28 76 2c 20 4f 50 5f 4f 70 65 6e 45  ddOp(v, OP_OpenE
1c89a 70 68 65 6d 65 72 61 6c 2c 20 69 50 61 72 6d 2c  phemeral, iParm,
1c89b 20 31 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47   1);.  }..  /* G
1c89c 65 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 20 74  enerating code t
1c89d 6f 20 66 69 6e 64 20 74 68 65 20 6d 69 6e 20 6f  o find the min o
1c89e 72 20 74 68 65 20 6d 61 78 2e 20 20 42 61 73 69  r the max.  Basi
1c89f 63 61 6c 6c 79 20 61 6c 6c 20 77 65 20 68 61 76  cally all we hav
1c8a0 65 0a 20 20 2a 2a 20 74 6f 20 64 6f 20 69 73 20  e.  ** to do is 
1c8a1 66 69 6e 64 20 74 68 65 20 66 69 72 73 74 20 6f  find the first o
1c8a2 72 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79  r the last entry
1c8a3 20 69 6e 20 74 68 65 20 63 68 6f 73 65 6e 20 69   in the chosen i
1c8a4 6e 64 65 78 2e 20 20 49 66 0a 20 20 2a 2a 20 74  ndex.  If.  ** t
1c8a5 68 65 20 6d 69 6e 28 29 20 6f 72 20 6d 61 78 28  he min() or max(
1c8a6 29 20 69 73 20 6f 6e 20 74 68 65 20 49 4e 54 45  ) is on the INTE
1c8a7 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c  GER PRIMARY KEY,
1c8a8 20 74 68 65 6e 20 66 69 6e 64 20 74 68 65 20 66   then find the f
1c8a9 69 72 73 74 0a 20 20 2a 2a 20 6f 72 20 6c 61 73  irst.  ** or las
1c8aa 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 6d  t entry in the m
1c8ab 61 69 6e 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a  ain table..  */.
1c8ac 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
1c8ad 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61  chemaToIndex(pPa
1c8ae 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70  rse->db, pTab->p
1c8af 53 63 68 65 6d 61 29 3b 0a 20 20 61 73 73 65 72  Schema);.  asser
1c8b0 74 28 20 69 44 62 3e 3d 30 20 7c 7c 20 70 54 61  t( iDb>=0 || pTa
1c8b1 62 2d 3e 69 73 45 70 68 65 6d 20 29 3b 0a 20 20  b->isEphem );.  
1c8b2 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66  sqlite3CodeVerif
1c8b3 79 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20  ySchema(pParse, 
1c8b4 69 44 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 54  iDb);.  sqlite3T
1c8b5 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c  ableLock(pParse,
1c8b6 20 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d   iDb, pTab->tnum
1c8b7 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  , 0, pTab->zName
1c8b8 29 3b 0a 20 20 62 61 73 65 20 3d 20 70 53 72 63  );.  base = pSrc
1c8b9 2d 3e 61 5b 30 5d 2e 69 43 75 72 73 6f 72 3b 0a  ->a[0].iCursor;.
1c8ba 20 20 62 72 6b 20 3d 20 73 71 6c 69 74 65 33 56    brk = sqlite3V
1c8bb 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
1c8bc 0a 20 20 63 6f 6d 70 75 74 65 4c 69 6d 69 74 52  .  computeLimitR
1c8bd 65 67 69 73 74 65 72 73 28 70 50 61 72 73 65 2c  egisters(pParse,
1c8be 20 70 2c 20 62 72 6b 29 3b 0a 20 20 69 66 28 20   p, brk);.  if( 
1c8bf 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 53 65 6c 65  pSrc->a[0].pSele
1c8c0 63 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  ct==0 ){.    sql
1c8c1 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50  ite3OpenTable(pP
1c8c2 61 72 73 65 2c 20 62 61 73 65 2c 20 69 44 62 2c  arse, base, iDb,
1c8c3 20 70 54 61 62 2c 20 4f 50 5f 4f 70 65 6e 52 65   pTab, OP_OpenRe
1c8c4 61 64 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  ad);.  }.  if( p
1c8c5 49 64 78 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  Idx==0 ){.    sq
1c8c6 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1c8c7 2c 20 73 65 65 6b 4f 70 2c 20 62 61 73 65 2c 20  , seekOp, base, 
1c8c8 30 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  0);.  }else{.   
1c8c9 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67 68 20   /* Even though 
1c8ca 74 68 65 20 63 75 72 73 6f 72 20 75 73 65 64 20  the cursor used 
1c8cb 74 6f 20 6f 70 65 6e 20 74 68 65 20 69 6e 64 65  to open the inde
1c8cc 78 20 68 65 72 65 20 69 73 20 63 6c 6f 73 65 64  x here is closed
1c8cd 0a 20 20 20 20 2a 2a 20 61 73 20 73 6f 6f 6e 20  .    ** as soon 
1c8ce 61 73 20 61 20 73 69 6e 67 6c 65 20 76 61 6c 75  as a single valu
1c8cf 65 20 68 61 73 20 62 65 65 6e 20 72 65 61 64 20  e has been read 
1c8d0 66 72 6f 6d 20 69 74 2c 20 61 6c 6c 6f 63 61 74  from it, allocat
1c8d1 65 20 69 74 0a 20 20 20 20 2a 2a 20 75 73 69 6e  e it.    ** usin
1c8d2 67 20 28 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b  g (pParse->nTab+
1c8d3 2b 29 20 74 6f 20 70 72 65 76 65 6e 74 20 74 68  +) to prevent th
1c8d4 65 20 63 75 72 73 6f 72 20 69 64 20 66 72 6f 6d  e cursor id from
1c8d5 20 62 65 69 6e 67 20 0a 20 20 20 20 2a 2a 20 72   being .    ** r
1c8d6 65 75 73 65 64 2e 20 54 68 69 73 20 69 73 20 69  eused. This is i
1c8d7 6d 70 6f 72 74 61 6e 74 20 66 6f 72 20 73 74 61  mportant for sta
1c8d8 74 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 66  tements of the f
1c8d9 6f 72 6d 20 0a 20 20 20 20 2a 2a 20 22 49 4e 53  orm .    ** "INS
1c8da 45 52 54 20 49 4e 54 4f 20 78 20 53 45 4c 45 43  ERT INTO x SELEC
1c8db 54 20 6d 61 78 28 29 20 46 52 4f 4d 20 78 22 2e  T max() FROM x".
1c8dc 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20  .    */.    int 
1c8dd 69 49 64 78 3b 0a 20 20 20 20 4b 65 79 49 6e 66  iIdx;.    KeyInf
1c8de 6f 20 2a 70 4b 65 79 20 3d 20 73 71 6c 69 74 65  o *pKey = sqlite
1c8df 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50  3IndexKeyinfo(pP
1c8e0 61 72 73 65 2c 20 70 49 64 78 29 3b 0a 20 20 20  arse, pIdx);.   
1c8e1 20 69 49 64 78 20 3d 20 70 50 61 72 73 65 2d 3e   iIdx = pParse->
1c8e2 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 61 73 73 65  nTab++;.    asse
1c8e3 72 74 28 20 70 49 64 78 2d 3e 70 53 63 68 65 6d  rt( pIdx->pSchem
1c8e4 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  a==pTab->pSchema
1c8e5 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   );.    sqlite3V
1c8e6 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49  dbeAddOp(v, OP_I
1c8e7 6e 74 65 67 65 72 2c 20 69 44 62 2c 20 30 29 3b  nteger, iDb, 0);
1c8e8 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1c8e9 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 65  Op3(v, OP_OpenRe
1c8ea 61 64 2c 20 69 49 64 78 2c 20 70 49 64 78 2d 3e  ad, iIdx, pIdx->
1c8eb 74 6e 75 6d 2c 20 0a 20 20 20 20 20 20 20 20 28  tnum, .        (
1c8ec 63 68 61 72 2a 29 70 4b 65 79 2c 20 50 33 5f 4b  char*)pKey, P3_K
1c8ed 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b  EYINFO_HANDOFF);
1c8ee 0a 20 20 20 20 69 66 28 20 73 65 65 6b 4f 70 3d  .    if( seekOp=
1c8ef 3d 4f 50 5f 52 65 77 69 6e 64 20 29 7b 0a 20 20  =OP_Rewind ){.  
1c8f0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1c8f1 64 64 4f 70 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  ddOp(v, OP_Null,
1c8f2 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71   0, 0);.      sq
1c8f3 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1c8f4 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c  , OP_MakeRecord,
1c8f5 20 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 65   1, 0);.      se
1c8f6 65 6b 4f 70 20 3d 20 4f 50 5f 4d 6f 76 65 47 74  ekOp = OP_MoveGt
1c8f7 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1c8f8 70 49 64 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72  pIdx->aSortOrder
1c8f9 5b 30 5d 3d 3d 53 51 4c 49 54 45 5f 53 4f 5f 44  [0]==SQLITE_SO_D
1c8fa 45 53 43 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ESC ){.      /* 
1c8fb 54 69 63 6b 65 74 20 23 32 35 31 34 3a 20 69 6e  Ticket #2514: in
1c8fc 76 65 72 74 20 74 68 65 20 73 65 65 6b 20 6f 70  vert the seek op
1c8fd 65 72 61 74 6f 72 20 69 66 20 77 65 20 61 72 65  erator if we are
1c8fe 20 75 73 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20   using.      ** 
1c8ff 61 20 64 65 73 63 65 6e 64 69 6e 67 20 69 6e 64  a descending ind
1c900 65 78 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  ex. */.      if(
1c901 20 73 65 65 6b 4f 70 3d 3d 4f 50 5f 4c 61 73 74   seekOp==OP_Last
1c902 20 29 7b 0a 20 20 20 20 20 20 20 20 73 65 65 6b   ){.        seek
1c903 4f 70 20 3d 20 4f 50 5f 52 65 77 69 6e 64 3b 0a  Op = OP_Rewind;.
1c904 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1c905 20 20 20 20 20 61 73 73 65 72 74 28 20 73 65 65       assert( see
1c906 6b 4f 70 3d 3d 4f 50 5f 4d 6f 76 65 47 74 20 29  kOp==OP_MoveGt )
1c907 3b 0a 20 20 20 20 20 20 20 20 73 65 65 6b 4f 70  ;.        seekOp
1c908 20 3d 20 4f 50 5f 4d 6f 76 65 4c 74 3b 0a 20 20   = OP_MoveLt;.  
1c909 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
1c90a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1c90b 28 76 2c 20 73 65 65 6b 4f 70 2c 20 69 49 64 78  (v, seekOp, iIdx
1c90c 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
1c90d 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1c90e 5f 49 64 78 52 6f 77 69 64 2c 20 69 49 64 78 2c  _IdxRowid, iIdx,
1c90f 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
1c910 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1c911 43 6c 6f 73 65 2c 20 69 49 64 78 2c 20 30 29 3b  Close, iIdx, 0);
1c912 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1c913 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 6f 76 65  AddOp(v, OP_Move
1c914 47 65 2c 20 62 61 73 65 2c 20 30 29 3b 0a 20 20  Ge, base, 0);.  
1c915 7d 0a 20 20 65 4c 69 73 74 2e 6e 45 78 70 72 20  }.  eList.nExpr 
1c916 3d 20 31 3b 0a 20 20 6d 65 6d 73 65 74 28 26 65  = 1;.  memset(&e
1c917 4c 69 73 74 49 74 65 6d 2c 20 30 2c 20 73 69 7a  ListItem, 0, siz
1c918 65 6f 66 28 65 4c 69 73 74 49 74 65 6d 29 29 3b  eof(eListItem));
1c919 0a 20 20 65 4c 69 73 74 2e 61 20 3d 20 26 65 4c  .  eList.a = &eL
1c91a 69 73 74 49 74 65 6d 3b 0a 20 20 65 4c 69 73 74  istItem;.  eList
1c91b 2e 61 5b 30 5d 2e 70 45 78 70 72 20 3d 20 70 45  .a[0].pExpr = pE
1c91c 78 70 72 3b 0a 20 20 73 65 6c 65 63 74 49 6e 6e  xpr;.  selectInn
1c91d 65 72 4c 6f 6f 70 28 70 50 61 72 73 65 2c 20 70  erLoop(pParse, p
1c91e 2c 20 26 65 4c 69 73 74 2c 20 30 2c 20 30 2c 20  , &eList, 0, 0, 
1c91f 30 2c 20 2d 31 2c 20 65 44 65 73 74 2c 20 69 50  0, -1, eDest, iP
1c920 61 72 6d 2c 20 62 72 6b 2c 20 62 72 6b 2c 20 30  arm, brk, brk, 0
1c921 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
1c922 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
1c923 62 72 6b 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  brk);.  sqlite3V
1c924 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43  dbeAddOp(v, OP_C
1c925 6c 6f 73 65 2c 20 62 61 73 65 2c 20 30 29 3b 0a  lose, base, 0);.
1c926 20 20 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d    .  return 1;.}
1c927 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c 79 7a 65 20  ../*.** Analyze 
1c928 61 6e 64 20 4f 52 44 45 52 20 42 59 20 6f 72 20  and ORDER BY or 
1c929 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20  GROUP BY clause 
1c92a 69 6e 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  in a SELECT stat
1c92b 65 6d 65 6e 74 2e 20 20 52 65 74 75 72 6e 0a 2a  ement.  Return.*
1c92c 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * the number of 
1c92d 65 72 72 6f 72 73 20 73 65 65 6e 2e 0a 2a 2a 0a  errors seen..**.
1c92e 2a 2a 20 41 6e 20 4f 52 44 45 52 20 42 59 20 6f  ** An ORDER BY o
1c92f 72 20 47 52 4f 55 50 20 42 59 20 69 73 20 61 20  r GROUP BY is a 
1c930 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 69  list of expressi
1c931 6f 6e 73 2e 20 20 49 66 20 61 6e 79 20 65 78 70  ons.  If any exp
1c932 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 61 6e  ression.** is an
1c933 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e   integer constan
1c934 74 2c 20 74 68 65 6e 20 74 68 61 74 20 65 78 70  t, then that exp
1c935 72 65 73 73 69 6f 6e 20 69 73 20 72 65 70 6c 61  ression is repla
1c936 63 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 63 6f  ced by the.** co
1c937 72 72 65 73 70 6f 6e 64 69 6e 67 20 65 6e 74 72  rresponding entr
1c938 79 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  y in the result 
1c939 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  set..*/.static i
1c93a 6e 74 20 70 72 6f 63 65 73 73 4f 72 64 65 72 47  nt processOrderG
1c93b 72 6f 75 70 42 79 28 0a 20 20 4e 61 6d 65 43 6f  roupBy(.  NameCo
1c93c 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20 20 20 20  ntext *pNC,     
1c93d 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20  /* Name context 
1c93e 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 73 74  of the SELECT st
1c93f 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 45 78  atement. */.  Ex
1c940 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79  prList *pOrderBy
1c941 2c 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52  ,   /* The ORDER
1c942 20 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20   BY or GROUP BY 
1c943 63 6c 61 75 73 65 20 74 6f 20 62 65 20 70 72 6f  clause to be pro
1c944 63 65 73 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73  cessed */.  cons
1c945 74 20 63 68 61 72 20 2a 7a 54 79 70 65 20 20 20  t char *zType   
1c946 20 20 2f 2a 20 45 69 74 68 65 72 20 22 4f 52 44    /* Either "ORD
1c947 45 52 22 20 6f 72 20 22 47 52 4f 55 50 22 2c 20  ER" or "GROUP", 
1c948 61 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 2a  as appropriate *
1c949 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  /.){.  int i;.  
1c94a 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
1c94b 20 3d 20 70 4e 43 2d 3e 70 45 4c 69 73 74 3b 20   = pNC->pEList; 
1c94c 20 20 20 20 2f 2a 20 54 68 65 20 72 65 73 75 6c      /* The resul
1c94d 74 20 73 65 74 20 6f 66 20 74 68 65 20 53 45 4c  t set of the SEL
1c94e 45 43 54 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a  ECT */.  Parse *
1c94f 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50  pParse = pNC->pP
1c950 61 72 73 65 3b 20 20 20 20 20 2f 2a 20 54 68 65  arse;     /* The
1c951 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74   result set of t
1c952 68 65 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 61  he SELECT */.  a
1c953 73 73 65 72 74 28 20 70 45 4c 69 73 74 20 29 3b  ssert( pEList );
1c954 0a 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79  ..  if( pOrderBy
1c955 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
1c956 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 2d 3e    if( pOrderBy->
1c957 6e 45 78 70 72 3e 53 51 4c 49 54 45 5f 4d 41 58  nExpr>SQLITE_MAX
1c958 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 73  _COLUMN ){.    s
1c959 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
1c95a 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79  Parse, "too many
1c95b 20 74 65 72 6d 73 20 69 6e 20 25 73 20 42 59 20   terms in %s BY 
1c95c 63 6c 61 75 73 65 22 2c 20 7a 54 79 70 65 29 3b  clause", zType);
1c95d 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
1c95e 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c   }.  for(i=0; i<
1c95f 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b  pOrderBy->nExpr;
1c960 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 69   i++){.    int i
1c961 43 6f 6c 3b 0a 20 20 20 20 45 78 70 72 20 2a 70  Col;.    Expr *p
1c962 45 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b  E = pOrderBy->a[
1c963 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 69 66  i].pExpr;.    if
1c964 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 49  ( sqlite3ExprIsI
1c965 6e 74 65 67 65 72 28 70 45 2c 20 26 69 43 6f 6c  nteger(pE, &iCol
1c966 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69  ) ){.      if( i
1c967 43 6f 6c 3e 30 20 26 26 20 69 43 6f 6c 3c 3d 70  Col>0 && iCol<=p
1c968 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a  EList->nExpr ){.
1c969 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20          CollSeq 
1c96a 2a 70 43 6f 6c 6c 20 3d 20 70 45 2d 3e 70 43 6f  *pColl = pE->pCo
1c96b 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  ll;.        int 
1c96c 66 6c 61 67 73 20 3d 20 70 45 2d 3e 66 6c 61 67  flags = pE->flag
1c96d 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74  s & EP_ExpCollat
1c96e 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  e;.        sqlit
1c96f 65 33 45 78 70 72 44 65 6c 65 74 65 28 70 45 29  e3ExprDelete(pE)
1c970 3b 0a 20 20 20 20 20 20 20 20 70 45 20 3d 20 70  ;.        pE = p
1c971 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 70 45  OrderBy->a[i].pE
1c972 78 70 72 20 3d 20 73 71 6c 69 74 65 33 45 78 70  xpr = sqlite3Exp
1c973 72 44 75 70 28 70 45 4c 69 73 74 2d 3e 61 5b 69  rDup(pEList->a[i
1c974 43 6f 6c 2d 31 5d 2e 70 45 78 70 72 29 3b 0a 20  Col-1].pExpr);. 
1c975 20 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 6c         if( pColl
1c976 20 26 26 20 66 6c 61 67 73 20 29 7b 0a 20 20 20   && flags ){.   
1c977 20 20 20 20 20 20 20 70 45 2d 3e 70 43 6f 6c 6c         pE->pColl
1c978 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20   = pColl;.      
1c979 20 20 20 20 70 45 2d 3e 66 6c 61 67 73 20 7c 3d      pE->flags |=
1c97a 20 66 6c 61 67 73 3b 0a 20 20 20 20 20 20 20 20   flags;.        
1c97b 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  }.      }else{. 
1c97c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
1c97d 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a  rorMsg(pParse, .
1c97e 20 20 20 20 20 20 20 20 20 20 20 22 25 73 20 42             "%s B
1c97f 59 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20  Y column number 
1c980 25 64 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20  %d out of range 
1c981 2d 20 73 68 6f 75 6c 64 20 62 65 20 22 0a 20 20  - should be ".  
1c982 20 20 20 20 20 20 20 20 20 22 62 65 74 77 65 65           "betwee
1c983 6e 20 31 20 61 6e 64 20 25 64 22 2c 20 7a 54 79  n 1 and %d", zTy
1c984 70 65 2c 20 69 43 6f 6c 2c 20 70 45 4c 69 73 74  pe, iCol, pEList
1c985 2d 3e 6e 45 78 70 72 29 3b 0a 20 20 20 20 20 20  ->nExpr);.      
1c986 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
1c987 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
1c988 28 20 73 71 6c 69 74 65 33 45 78 70 72 52 65 73  ( sqlite3ExprRes
1c989 6f 6c 76 65 4e 61 6d 65 73 28 70 4e 43 2c 20 70  olveNames(pNC, p
1c98a 45 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  E) ){.      retu
1c98b 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  rn 1;.    }.  }.
1c98c 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
1c98d 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1c98e 65 20 72 65 73 6f 6c 76 65 73 20 61 6e 79 20 6e  e resolves any n
1c98f 61 6d 65 73 20 75 73 65 64 20 69 6e 20 74 68 65  ames used in the
1c990 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74   result set of t
1c991 68 65 0a 2a 2a 20 73 75 70 70 6c 69 65 64 20 53  he.** supplied S
1c992 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
1c993 20 49 66 20 74 68 65 20 53 45 4c 45 43 54 20 73   If the SELECT s
1c994 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 72  tatement being r
1c995 65 73 6f 6c 76 65 64 0a 2a 2a 20 69 73 20 61 20  esolved.** is a 
1c996 73 75 62 2d 73 65 6c 65 63 74 2c 20 74 68 65 6e  sub-select, then
1c997 20 70 4f 75 74 65 72 4e 43 20 69 73 20 61 20 70   pOuterNC is a p
1c998 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 4e 61  ointer to the Na
1c999 6d 65 43 6f 6e 74 65 78 74 20 0a 2a 2a 20 6f 66  meContext .** of
1c99a 20 74 68 65 20 70 61 72 65 6e 74 20 53 45 4c 45   the parent SELE
1c99b 43 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  CT..*/.SQLITE_PR
1c99c 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1c99d 33 53 65 6c 65 63 74 52 65 73 6f 6c 76 65 28 0a  3SelectResolve(.
1c99e 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
1c99f 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1c9a0 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a  parser context *
1c9a1 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20  /.  Select *p,  
1c9a2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1c9a3 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
1c9a4 6e 74 20 62 65 69 6e 67 20 63 6f 64 65 64 2e 20  nt being coded. 
1c9a5 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74  */.  NameContext
1c9a6 20 2a 70 4f 75 74 65 72 4e 43 20 20 2f 2a 20 54   *pOuterNC  /* T
1c9a7 68 65 20 6f 75 74 65 72 20 6e 61 6d 65 20 63 6f  he outer name co
1c9a8 6e 74 65 78 74 2e 20 4d 61 79 20 62 65 20 4e 55  ntext. May be NU
1c9a9 4c 4c 2e 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72  LL. */.){.  Expr
1c9aa 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 20  List *pEList;   
1c9ab 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74         /* Result
1c9ac 20 73 65 74 2e 20 2a 2f 0a 20 20 69 6e 74 20 69   set. */.  int i
1c9ad 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1c9ae 20 20 20 20 20 20 2f 2a 20 46 6f 72 2d 6c 6f 6f        /* For-loo
1c9af 70 20 76 61 72 69 61 62 6c 65 20 75 73 65 64 20  p variable used 
1c9b0 69 6e 20 6d 75 6c 74 69 70 6c 65 20 70 6c 61 63  in multiple plac
1c9b1 65 73 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74  es */.  NameCont
1c9b2 65 78 74 20 73 4e 43 3b 20 20 20 20 20 20 20 20  ext sNC;        
1c9b3 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 6e 61 6d 65     /* Local name
1c9b4 2d 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78  -context */.  Ex
1c9b5 70 72 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79  prList *pGroupBy
1c9b6 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ;        /* The 
1c9b7 67 72 6f 75 70 20 62 79 20 63 6c 61 75 73 65 20  group by clause 
1c9b8 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73  */..  /* If this
1c9b9 20 72 6f 75 74 69 6e 65 20 68 61 73 20 72 75 6e   routine has run
1c9ba 20 62 65 66 6f 72 65 2c 20 72 65 74 75 72 6e 20   before, return 
1c9bb 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 2a 2f 0a  immediately. */.
1c9bc 20 20 69 66 28 20 70 2d 3e 69 73 52 65 73 6f 6c    if( p->isResol
1c9bd 76 65 64 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ved ){.    asser
1c9be 74 28 20 21 70 4f 75 74 65 72 4e 43 20 29 3b 0a  t( !pOuterNC );.
1c9bf 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1c9c0 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 2d 3e 69  E_OK;.  }.  p->i
1c9c1 73 52 65 73 6f 6c 76 65 64 20 3d 20 31 3b 0a 0a  sResolved = 1;..
1c9c2 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 68 61    /* If there ha
1c9c3 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  ve already been 
1c9c4 65 72 72 6f 72 73 2c 20 64 6f 20 6e 6f 74 68 69  errors, do nothi
1c9c5 6e 67 2e 20 2a 2f 0a 20 20 69 66 28 20 70 50 61  ng. */.  if( pPa
1c9c6 72 73 65 2d 3e 6e 45 72 72 3e 30 20 29 7b 0a 20  rse->nErr>0 ){. 
1c9c7 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1c9c8 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f  _ERROR;.  }..  /
1c9c9 2a 20 50 72 65 70 61 72 65 20 74 68 65 20 73 65  * Prepare the se
1c9ca 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 2e 20  lect statement. 
1c9cb 54 68 69 73 20 63 61 6c 6c 20 77 69 6c 6c 20 61  This call will a
1c9cc 6c 6c 6f 63 61 74 65 20 61 6c 6c 20 63 75 72 73  llocate all curs
1c9cd 6f 72 73 0a 20 20 2a 2a 20 72 65 71 75 69 72 65  ors.  ** require
1c9ce 64 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 65 20  d to handle the 
1c9cf 74 61 62 6c 65 73 20 61 6e 64 20 73 75 62 71 75  tables and subqu
1c9d0 65 72 69 65 73 20 69 6e 20 74 68 65 20 46 52 4f  eries in the FRO
1c9d1 4d 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2f 0a 20  M clause..  */. 
1c9d2 20 69 66 28 20 70 72 65 70 53 65 6c 65 63 74 53   if( prepSelectS
1c9d3 74 6d 74 28 70 50 61 72 73 65 2c 20 70 29 20 29  tmt(pParse, p) )
1c9d4 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
1c9d5 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a  ITE_ERROR;.  }..
1c9d6 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65    /* Resolve the
1c9d7 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20   expressions in 
1c9d8 74 68 65 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46  the LIMIT and OF
1c9d9 46 53 45 54 20 63 6c 61 75 73 65 73 2e 20 54 68  FSET clauses. Th
1c9da 65 73 65 0a 20 20 2a 2a 20 61 72 65 20 6e 6f 74  ese.  ** are not
1c9db 20 61 6c 6c 6f 77 65 64 20 74 6f 20 72 65 66 65   allowed to refe
1c9dc 72 20 74 6f 20 61 6e 79 20 6e 61 6d 65 73 2c 20  r to any names, 
1c9dd 73 6f 20 70 61 73 73 20 61 6e 20 65 6d 70 74 79  so pass an empty
1c9de 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2e 0a 20 20   NameContext..  
1c9df 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 73 4e 43  */.  memset(&sNC
1c9e0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29  , 0, sizeof(sNC)
1c9e1 29 3b 0a 20 20 73 4e 43 2e 70 50 61 72 73 65 20  );.  sNC.pParse 
1c9e2 3d 20 70 50 61 72 73 65 3b 0a 20 20 69 66 28 20  = pParse;.  if( 
1c9e3 73 71 6c 69 74 65 33 45 78 70 72 52 65 73 6f 6c  sqlite3ExprResol
1c9e4 76 65 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d  veNames(&sNC, p-
1c9e5 3e 70 4c 69 6d 69 74 29 20 7c 7c 0a 20 20 20 20  >pLimit) ||.    
1c9e6 20 20 73 71 6c 69 74 65 33 45 78 70 72 52 65 73    sqlite3ExprRes
1c9e7 6f 6c 76 65 4e 61 6d 65 73 28 26 73 4e 43 2c 20  olveNames(&sNC, 
1c9e8 70 2d 3e 70 4f 66 66 73 65 74 29 20 29 7b 0a 20  p->pOffset) ){. 
1c9e9 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1c9ea 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f  _ERROR;.  }..  /
1c9eb 2a 20 53 65 74 20 75 70 20 74 68 65 20 6c 6f 63  * Set up the loc
1c9ec 61 6c 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20  al name-context 
1c9ed 74 6f 20 70 61 73 73 20 74 6f 20 45 78 70 72 52  to pass to ExprR
1c9ee 65 73 6f 6c 76 65 4e 61 6d 65 73 28 29 20 74 6f  esolveNames() to
1c9ef 0a 20 20 2a 2a 20 72 65 73 6f 6c 76 65 20 74 68  .  ** resolve th
1c9f0 65 20 65 78 70 72 65 73 73 69 6f 6e 2d 6c 69 73  e expression-lis
1c9f1 74 2e 0a 20 20 2a 2f 0a 20 20 73 4e 43 2e 61 6c  t..  */.  sNC.al
1c9f2 6c 6f 77 41 67 67 20 3d 20 31 3b 0a 20 20 73 4e  lowAgg = 1;.  sN
1c9f3 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 2d 3e  C.pSrcList = p->
1c9f4 70 53 72 63 3b 0a 20 20 73 4e 43 2e 70 4e 65 78  pSrc;.  sNC.pNex
1c9f5 74 20 3d 20 70 4f 75 74 65 72 4e 43 3b 0a 0a 20  t = pOuterNC;.. 
1c9f6 20 2f 2a 20 52 65 73 6f 6c 76 65 20 6e 61 6d 65   /* Resolve name
1c9f7 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  s in the result 
1c9f8 73 65 74 2e 20 2a 2f 0a 20 20 70 45 4c 69 73 74  set. */.  pEList
1c9f9 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20   = p->pEList;.  
1c9fa 69 66 28 20 21 70 45 4c 69 73 74 20 29 20 72 65  if( !pEList ) re
1c9fb 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
1c9fc 52 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  R;.  for(i=0; i<
1c9fd 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69  pEList->nExpr; i
1c9fe 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70  ++){.    Expr *p
1c9ff 58 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d  X = pEList->a[i]
1ca00 2e 70 45 78 70 72 3b 0a 20 20 20 20 69 66 28 20  .pExpr;.    if( 
1ca01 73 71 6c 69 74 65 33 45 78 70 72 52 65 73 6f 6c  sqlite3ExprResol
1ca02 76 65 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 58  veNames(&sNC, pX
1ca03 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
1ca04 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
1ca05 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
1ca06 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20  If there are no 
1ca07 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
1ca08 6f 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c  ons in the resul
1ca09 74 2d 73 65 74 2c 20 61 6e 64 20 6e 6f 20 47 52  t-set, and no GR
1ca0a 4f 55 50 20 42 59 20 0a 20 20 2a 2a 20 65 78 70  OUP BY .  ** exp
1ca0b 72 65 73 73 69 6f 6e 2c 20 64 6f 20 6e 6f 74 20  ression, do not 
1ca0c 61 6c 6c 6f 77 20 61 67 67 72 65 67 61 74 65 73  allow aggregates
1ca0d 20 69 6e 20 61 6e 79 20 6f 66 20 74 68 65 20 6f   in any of the o
1ca0e 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 73  ther expressions
1ca0f 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
1ca10 20 21 70 2d 3e 69 73 41 67 67 20 29 3b 0a 20 20   !p->isAgg );.  
1ca11 70 47 72 6f 75 70 42 79 20 3d 20 70 2d 3e 70 47  pGroupBy = p->pG
1ca12 72 6f 75 70 42 79 3b 0a 20 20 69 66 28 20 70 47  roupBy;.  if( pG
1ca13 72 6f 75 70 42 79 20 7c 7c 20 73 4e 43 2e 68 61  roupBy || sNC.ha
1ca14 73 41 67 67 20 29 7b 0a 20 20 20 20 70 2d 3e 69  sAgg ){.    p->i
1ca15 73 41 67 67 20 3d 20 31 3b 0a 20 20 7d 65 6c 73  sAgg = 1;.  }els
1ca16 65 7b 0a 20 20 20 20 73 4e 43 2e 61 6c 6c 6f 77  e{.    sNC.allow
1ca17 41 67 67 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20  Agg = 0;.  }..  
1ca18 2f 2a 20 49 66 20 61 20 48 41 56 49 4e 47 20 63  /* If a HAVING c
1ca19 6c 61 75 73 65 20 69 73 20 70 72 65 73 65 6e 74  lause is present
1ca1a 2c 20 74 68 65 6e 20 74 68 65 72 65 20 6d 75 73  , then there mus
1ca1b 74 20 62 65 20 61 20 47 52 4f 55 50 20 42 59 20  t be a GROUP BY 
1ca1c 63 6c 61 75 73 65 2e 0a 20 20 2a 2f 0a 20 20 69  clause..  */.  i
1ca1d 66 28 20 70 2d 3e 70 48 61 76 69 6e 67 20 26 26  f( p->pHaving &&
1ca1e 20 21 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 20   !pGroupBy ){.  
1ca1f 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
1ca20 67 28 70 50 61 72 73 65 2c 20 22 61 20 47 52 4f  g(pParse, "a GRO
1ca21 55 50 20 42 59 20 63 6c 61 75 73 65 20 69 73 20  UP BY clause is 
1ca22 72 65 71 75 69 72 65 64 20 62 65 66 6f 72 65 20  required before 
1ca23 48 41 56 49 4e 47 22 29 3b 0a 20 20 20 20 72 65  HAVING");.    re
1ca24 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
1ca25 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 64 64  R;.  }..  /* Add
1ca26 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
1ca27 6c 69 73 74 20 74 6f 20 74 68 65 20 6e 61 6d 65  list to the name
1ca28 2d 63 6f 6e 74 65 78 74 20 62 65 66 6f 72 65 20  -context before 
1ca29 70 61 72 73 69 6e 67 20 74 68 65 0a 20 20 2a 2a  parsing the.  **
1ca2a 20 6f 74 68 65 72 20 65 78 70 72 65 73 73 69 6f   other expressio
1ca2b 6e 73 20 69 6e 20 74 68 65 20 53 45 4c 45 43 54  ns in the SELECT
1ca2c 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 69 73   statement. This
1ca2d 20 69 73 20 73 6f 20 74 68 61 74 0a 20 20 2a 2a   is so that.  **
1ca2e 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20   expressions in 
1ca2f 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
1ca30 20 28 65 74 63 2e 29 20 63 61 6e 20 72 65 66 65   (etc.) can refe
1ca31 72 20 74 6f 20 65 78 70 72 65 73 73 69 6f 6e 73  r to expressions
1ca32 20 62 79 0a 20 20 2a 2a 20 61 6c 69 61 73 65 73   by.  ** aliases
1ca33 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
1ca34 65 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4d 69  et..  **.  ** Mi
1ca35 6e 6f 72 20 70 6f 69 6e 74 3a 20 49 66 20 74 68  nor point: If th
1ca36 69 73 20 69 73 20 74 68 65 20 63 61 73 65 2c 20  is is the case, 
1ca37 74 68 65 6e 20 74 68 65 20 65 78 70 72 65 73 73  then the express
1ca38 69 6f 6e 20 77 69 6c 6c 20 62 65 0a 20 20 2a 2a  ion will be.  **
1ca39 20 72 65 2d 65 76 61 6c 75 61 74 65 64 20 66 6f   re-evaluated fo
1ca3a 72 20 65 61 63 68 20 72 65 66 65 72 65 6e 63 65  r each reference
1ca3b 20 74 6f 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 73   to it..  */.  s
1ca3c 4e 43 2e 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70  NC.pEList = p->p
1ca3d 45 4c 69 73 74 3b 0a 20 20 69 66 28 20 73 71 6c  EList;.  if( sql
1ca3e 69 74 65 33 45 78 70 72 52 65 73 6f 6c 76 65 4e  ite3ExprResolveN
1ca3f 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70 57  ames(&sNC, p->pW
1ca40 68 65 72 65 29 20 7c 7c 0a 20 20 20 20 20 73 71  here) ||.     sq
1ca41 6c 69 74 65 33 45 78 70 72 52 65 73 6f 6c 76 65  lite3ExprResolve
1ca42 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70  Names(&sNC, p->p
1ca43 48 61 76 69 6e 67 29 20 29 7b 0a 20 20 20 20 72  Having) ){.    r
1ca44 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
1ca45 4f 52 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d  OR;.  }.  if( p-
1ca46 3e 70 50 72 69 6f 72 3d 3d 30 20 29 7b 0a 20 20  >pPrior==0 ){.  
1ca47 20 20 69 66 28 20 70 72 6f 63 65 73 73 4f 72 64    if( processOrd
1ca48 65 72 47 72 6f 75 70 42 79 28 26 73 4e 43 2c 20  erGroupBy(&sNC, 
1ca49 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 22 4f 52  p->pOrderBy, "OR
1ca4a 44 45 52 22 29 20 7c 7c 0a 20 20 20 20 20 20 20  DER") ||.       
1ca4b 20 70 72 6f 63 65 73 73 4f 72 64 65 72 47 72 6f   processOrderGro
1ca4c 75 70 42 79 28 26 73 4e 43 2c 20 70 47 72 6f 75  upBy(&sNC, pGrou
1ca4d 70 42 79 2c 20 22 47 52 4f 55 50 22 29 20 29 7b  pBy, "GROUP") ){
1ca4e 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
1ca4f 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
1ca50 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73 71 6c  }.  }..  if( sql
1ca51 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69 6c 65 64  ite3MallocFailed
1ca52 28 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  () ){.    return
1ca53 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
1ca54 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75   }..  /* Make su
1ca55 72 65 20 74 68 65 20 47 52 4f 55 50 20 42 59 20  re the GROUP BY 
1ca56 63 6c 61 75 73 65 20 64 6f 65 73 20 6e 6f 74 20  clause does not 
1ca57 63 6f 6e 74 61 69 6e 20 61 67 67 72 65 67 61 74  contain aggregat
1ca58 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a  e functions..  *
1ca59 2f 0a 20 20 69 66 28 20 70 47 72 6f 75 70 42 79  /.  if( pGroupBy
1ca5a 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 45   ){.    struct E
1ca5b 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  xprList_item *pI
1ca5c 74 65 6d 3b 0a 20 20 0a 20 20 20 20 66 6f 72 28  tem;.  .    for(
1ca5d 69 3d 30 2c 20 70 49 74 65 6d 3d 70 47 72 6f 75  i=0, pItem=pGrou
1ca5e 70 42 79 2d 3e 61 3b 20 69 3c 70 47 72 6f 75 70  pBy->a; i<pGroup
1ca5f 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20  By->nExpr; i++, 
1ca60 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20  pItem++){.      
1ca61 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65  if( ExprHasPrope
1ca62 72 74 79 28 70 49 74 65 6d 2d 3e 70 45 78 70 72  rty(pItem->pExpr
1ca63 2c 20 45 50 5f 41 67 67 29 20 29 7b 0a 20 20 20  , EP_Agg) ){.   
1ca64 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
1ca65 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61 67  rMsg(pParse, "ag
1ca66 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
1ca67 73 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65  s are not allowe
1ca68 64 20 69 6e 20 22 0a 20 20 20 20 20 20 20 20 20  d in ".         
1ca69 20 20 20 22 74 68 65 20 47 52 4f 55 50 20 42 59     "the GROUP BY
1ca6a 20 63 6c 61 75 73 65 22 29 3b 0a 20 20 20 20 20   clause");.     
1ca6b 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1ca6c 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 7d 0a  _ERROR;.      }.
1ca6d 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
1ca6e 49 66 20 74 68 69 73 20 69 73 20 6f 6e 65 20 53  If this is one S
1ca6f 45 4c 45 43 54 20 6f 66 20 61 20 63 6f 6d 70 6f  ELECT of a compo
1ca70 75 6e 64 2c 20 62 65 20 73 75 72 65 20 74 6f 20  und, be sure to 
1ca71 72 65 73 6f 6c 76 65 20 6e 61 6d 65 73 0a 20 20  resolve names.  
1ca72 2a 2a 20 69 6e 20 74 68 65 20 6f 74 68 65 72 20  ** in the other 
1ca73 53 45 4c 45 43 54 73 2e 0a 20 20 2a 2f 0a 20 20  SELECTs..  */.  
1ca74 69 66 28 20 70 2d 3e 70 50 72 69 6f 72 20 29 7b  if( p->pPrior ){
1ca75 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69  .    return sqli
1ca76 74 65 33 53 65 6c 65 63 74 52 65 73 6f 6c 76 65  te3SelectResolve
1ca77 28 70 50 61 72 73 65 2c 20 70 2d 3e 70 50 72 69  (pParse, p->pPri
1ca78 6f 72 2c 20 70 4f 75 74 65 72 4e 43 29 3b 0a 20  or, pOuterNC);. 
1ca79 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75   }else{.    retu
1ca7a 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
1ca7b 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74  }.}../*.** Reset
1ca7c 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 61   the aggregate a
1ca7d 63 63 75 6d 75 6c 61 74 6f 72 2e 0a 2a 2a 0a 2a  ccumulator..**.*
1ca7e 2a 20 54 68 65 20 61 67 67 72 65 67 61 74 65 20  * The aggregate 
1ca7f 61 63 63 75 6d 75 6c 61 74 6f 72 20 69 73 20 61  accumulator is a
1ca80 20 73 65 74 20 6f 66 20 6d 65 6d 6f 72 79 20 63   set of memory c
1ca81 65 6c 6c 73 20 74 68 61 74 20 68 6f 6c 64 0a 2a  ells that hold.*
1ca82 2a 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 72  * intermediate r
1ca83 65 73 75 6c 74 73 20 77 68 69 6c 65 20 63 61 6c  esults while cal
1ca84 63 75 6c 61 74 69 6e 67 20 61 6e 20 61 67 67 72  culating an aggr
1ca85 65 67 61 74 65 2e 20 20 54 68 69 73 0a 2a 2a 20  egate.  This.** 
1ca86 72 6f 75 74 69 6e 65 20 73 69 6d 70 6c 79 20 73  routine simply s
1ca87 74 6f 72 65 73 20 4e 55 4c 4c 73 20 69 6e 20 61  tores NULLs in a
1ca88 6c 6c 20 6f 66 20 74 68 6f 73 65 20 6d 65 6d 6f  ll of those memo
1ca89 72 79 20 63 65 6c 6c 73 2e 0a 2a 2f 0a 73 74 61  ry cells..*/.sta
1ca8a 74 69 63 20 76 6f 69 64 20 72 65 73 65 74 41 63  tic void resetAc
1ca8b 63 75 6d 75 6c 61 74 6f 72 28 50 61 72 73 65 20  cumulator(Parse 
1ca8c 2a 70 50 61 72 73 65 2c 20 41 67 67 49 6e 66 6f  *pParse, AggInfo
1ca8d 20 2a 70 41 67 67 49 6e 66 6f 29 7b 0a 20 20 56   *pAggInfo){.  V
1ca8e 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d  dbe *v = pParse-
1ca8f 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b  >pVdbe;.  int i;
1ca90 0a 20 20 73 74 72 75 63 74 20 41 67 67 49 6e 66  .  struct AggInf
1ca91 6f 5f 66 75 6e 63 20 2a 70 46 75 6e 63 3b 0a 20  o_func *pFunc;. 
1ca92 20 69 66 28 20 70 41 67 67 49 6e 66 6f 2d 3e 6e   if( pAggInfo->n
1ca93 46 75 6e 63 2b 70 41 67 67 49 6e 66 6f 2d 3e 6e  Func+pAggInfo->n
1ca94 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20  Column==0 ){.   
1ca95 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66   return;.  }.  f
1ca96 6f 72 28 69 3d 30 3b 20 69 3c 70 41 67 67 49 6e  or(i=0; i<pAggIn
1ca97 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b  fo->nColumn; i++
1ca98 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
1ca99 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65  beAddOp(v, OP_Me
1ca9a 6d 4e 75 6c 6c 2c 20 70 41 67 67 49 6e 66 6f 2d  mNull, pAggInfo-
1ca9b 3e 61 43 6f 6c 5b 69 5d 2e 69 4d 65 6d 2c 20 30  >aCol[i].iMem, 0
1ca9c 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 70 46 75  );.  }.  for(pFu
1ca9d 6e 63 3d 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75  nc=pAggInfo->aFu
1ca9e 6e 63 2c 20 69 3d 30 3b 20 69 3c 70 41 67 67 49  nc, i=0; i<pAggI
1ca9f 6e 66 6f 2d 3e 6e 46 75 6e 63 3b 20 69 2b 2b 2c  nfo->nFunc; i++,
1caa0 20 70 46 75 6e 63 2b 2b 29 7b 0a 20 20 20 20 73   pFunc++){.    s
1caa1 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1caa2 76 2c 20 4f 50 5f 4d 65 6d 4e 75 6c 6c 2c 20 70  v, OP_MemNull, p
1caa3 46 75 6e 63 2d 3e 69 4d 65 6d 2c 20 30 29 3b 0a  Func->iMem, 0);.
1caa4 20 20 20 20 69 66 28 20 70 46 75 6e 63 2d 3e 69      if( pFunc->i
1caa5 44 69 73 74 69 6e 63 74 3e 3d 30 20 29 7b 0a 20  Distinct>=0 ){. 
1caa6 20 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d 20       Expr *pE = 
1caa7 70 46 75 6e 63 2d 3e 70 45 78 70 72 3b 0a 20 20  pFunc->pExpr;.  
1caa8 20 20 20 20 69 66 28 20 70 45 2d 3e 70 4c 69 73      if( pE->pLis
1caa9 74 3d 3d 30 20 7c 7c 20 70 45 2d 3e 70 4c 69 73  t==0 || pE->pLis
1caaa 74 2d 3e 6e 45 78 70 72 21 3d 31 20 29 7b 0a 20  t->nExpr!=1 ){. 
1caab 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
1caac 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
1caad 44 49 53 54 49 4e 43 54 20 69 6e 20 61 67 67 72  DISTINCT in aggr
1caae 65 67 61 74 65 20 6d 75 73 74 20 62 65 20 66 6f  egate must be fo
1caaf 6c 6c 6f 77 65 64 20 22 0a 20 20 20 20 20 20 20  llowed ".       
1cab0 20 20 20 20 22 62 79 20 61 6e 20 65 78 70 72 65      "by an expre
1cab1 73 73 69 6f 6e 22 29 3b 0a 20 20 20 20 20 20 20  ssion");.       
1cab2 20 70 46 75 6e 63 2d 3e 69 44 69 73 74 69 6e 63   pFunc->iDistinc
1cab3 74 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7d 65  t = -1;.      }e
1cab4 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 4b 65 79  lse{.        Key
1cab5 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 20 3d  Info *pKeyInfo =
1cab6 20 6b 65 79 49 6e 66 6f 46 72 6f 6d 45 78 70 72   keyInfoFromExpr
1cab7 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 45 2d  List(pParse, pE-
1cab8 3e 70 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 20  >pList);.       
1cab9 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28   sqlite3VdbeOp3(
1caba 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  v, OP_OpenEpheme
1cabb 72 61 6c 2c 20 70 46 75 6e 63 2d 3e 69 44 69 73  ral, pFunc->iDis
1cabc 74 69 6e 63 74 2c 20 30 2c 20 0a 20 20 20 20 20  tinct, 0, .     
1cabd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cabe 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79       (char*)pKey
1cabf 49 6e 66 6f 2c 20 50 33 5f 4b 45 59 49 4e 46 4f  Info, P3_KEYINFO
1cac0 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 20  _HANDOFF);.     
1cac1 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a   }.    }.  }.}..
1cac2 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65  /*.** Invoke the
1cac3 20 4f 50 5f 41 67 67 46 69 6e 61 6c 69 7a 65 20   OP_AggFinalize 
1cac4 6f 70 63 6f 64 65 20 66 6f 72 20 65 76 65 72 79  opcode for every
1cac5 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
1cac6 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 41 67  ion.** in the Ag
1cac7 67 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e  gInfo structure.
1cac8 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1cac9 66 69 6e 61 6c 69 7a 65 41 67 67 46 75 6e 63 74  finalizeAggFunct
1caca 69 6f 6e 73 28 50 61 72 73 65 20 2a 70 50 61 72  ions(Parse *pPar
1cacb 73 65 2c 20 41 67 67 49 6e 66 6f 20 2a 70 41 67  se, AggInfo *pAg
1cacc 67 49 6e 66 6f 29 7b 0a 20 20 56 64 62 65 20 2a  gInfo){.  Vdbe *
1cacd 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
1cace 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74  e;.  int i;.  st
1cacf 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e  ruct AggInfo_fun
1cad0 63 20 2a 70 46 3b 0a 20 20 66 6f 72 28 69 3d 30  c *pF;.  for(i=0
1cad1 2c 20 70 46 3d 70 41 67 67 49 6e 66 6f 2d 3e 61  , pF=pAggInfo->a
1cad2 46 75 6e 63 3b 20 69 3c 70 41 67 67 49 6e 66 6f  Func; i<pAggInfo
1cad3 2d 3e 6e 46 75 6e 63 3b 20 69 2b 2b 2c 20 70 46  ->nFunc; i++, pF
1cad4 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 4c 69 73  ++){.    ExprLis
1cad5 74 20 2a 70 4c 69 73 74 20 3d 20 70 46 2d 3e 70  t *pList = pF->p
1cad6 45 78 70 72 2d 3e 70 4c 69 73 74 3b 0a 20 20 20  Expr->pList;.   
1cad7 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28   sqlite3VdbeOp3(
1cad8 76 2c 20 4f 50 5f 41 67 67 46 69 6e 61 6c 2c 20  v, OP_AggFinal, 
1cad9 70 46 2d 3e 69 4d 65 6d 2c 20 70 4c 69 73 74 20  pF->iMem, pList 
1cada 3f 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3a  ? pList->nExpr :
1cadb 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20   0,.            
1cadc 20 20 20 20 20 20 20 20 20 20 28 76 6f 69 64 2a            (void*
1cadd 29 70 46 2d 3e 70 46 75 6e 63 2c 20 50 33 5f 46  )pF->pFunc, P3_F
1cade 55 4e 43 44 45 46 29 3b 0a 20 20 7d 0a 7d 0a 0a  UNCDEF);.  }.}..
1cadf 2f 2a 0a 2a 2a 20 55 70 64 61 74 65 20 74 68 65  /*.** Update the
1cae0 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 6d 65 6d   accumulator mem
1cae1 6f 72 79 20 63 65 6c 6c 73 20 66 6f 72 20 61 6e  ory cells for an
1cae2 20 61 67 67 72 65 67 61 74 65 20 62 61 73 65 64   aggregate based
1cae3 20 6f 6e 0a 2a 2a 20 74 68 65 20 63 75 72 72 65   on.** the curre
1cae4 6e 74 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69  nt cursor positi
1cae5 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  on..*/.static vo
1cae6 69 64 20 75 70 64 61 74 65 41 63 63 75 6d 75 6c  id updateAccumul
1cae7 61 74 6f 72 28 50 61 72 73 65 20 2a 70 50 61 72  ator(Parse *pPar
1cae8 73 65 2c 20 41 67 67 49 6e 66 6f 20 2a 70 41 67  se, AggInfo *pAg
1cae9 67 49 6e 66 6f 29 7b 0a 20 20 56 64 62 65 20 2a  gInfo){.  Vdbe *
1caea 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
1caeb 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74  e;.  int i;.  st
1caec 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e  ruct AggInfo_fun
1caed 63 20 2a 70 46 3b 0a 20 20 73 74 72 75 63 74 20  c *pF;.  struct 
1caee 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70 43 3b  AggInfo_col *pC;
1caef 0a 0a 20 20 70 41 67 67 49 6e 66 6f 2d 3e 64 69  ..  pAggInfo->di
1caf0 72 65 63 74 4d 6f 64 65 20 3d 20 31 3b 0a 20 20  rectMode = 1;.  
1caf1 66 6f 72 28 69 3d 30 2c 20 70 46 3d 70 41 67 67  for(i=0, pF=pAgg
1caf2 49 6e 66 6f 2d 3e 61 46 75 6e 63 3b 20 69 3c 70  Info->aFunc; i<p
1caf3 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 3b 20  AggInfo->nFunc; 
1caf4 69 2b 2b 2c 20 70 46 2b 2b 29 7b 0a 20 20 20 20  i++, pF++){.    
1caf5 69 6e 74 20 6e 41 72 67 3b 0a 20 20 20 20 69 6e  int nArg;.    in
1caf6 74 20 61 64 64 72 4e 65 78 74 20 3d 20 30 3b 0a  t addrNext = 0;.
1caf7 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c      ExprList *pL
1caf8 69 73 74 20 3d 20 70 46 2d 3e 70 45 78 70 72 2d  ist = pF->pExpr-
1caf9 3e 70 4c 69 73 74 3b 0a 20 20 20 20 69 66 28 20  >pList;.    if( 
1cafa 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 6e  pList ){.      n
1cafb 41 72 67 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78  Arg = pList->nEx
1cafc 70 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  pr;.      sqlite
1cafd 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73  3ExprCodeExprLis
1cafe 74 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 29  t(pParse, pList)
1caff 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
1cb00 20 20 20 6e 41 72 67 20 3d 20 30 3b 0a 20 20 20     nArg = 0;.   
1cb01 20 7d 0a 20 20 20 20 69 66 28 20 70 46 2d 3e 69   }.    if( pF->i
1cb02 44 69 73 74 69 6e 63 74 3e 3d 30 20 29 7b 0a 20  Distinct>=0 ){. 
1cb03 20 20 20 20 20 61 64 64 72 4e 65 78 74 20 3d 20       addrNext = 
1cb04 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
1cb05 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 61  abel(v);.      a
1cb06 73 73 65 72 74 28 20 6e 41 72 67 3d 3d 31 20 29  ssert( nArg==1 )
1cb07 3b 0a 20 20 20 20 20 20 63 6f 64 65 44 69 73 74  ;.      codeDist
1cb08 69 6e 63 74 28 76 2c 20 70 46 2d 3e 69 44 69 73  inct(v, pF->iDis
1cb09 74 69 6e 63 74 2c 20 61 64 64 72 4e 65 78 74 2c  tinct, addrNext,
1cb0a 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69   1);.    }.    i
1cb0b 66 28 20 70 46 2d 3e 70 46 75 6e 63 2d 3e 6e 65  f( pF->pFunc->ne
1cb0c 65 64 43 6f 6c 6c 53 65 71 20 29 7b 0a 20 20 20  edCollSeq ){.   
1cb0d 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c     CollSeq *pCol
1cb0e 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 74 72  l = 0;.      str
1cb0f 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
1cb10 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 20 20  m *pItem;.      
1cb11 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 61 73 73  int j;.      ass
1cb12 65 72 74 28 20 70 4c 69 73 74 21 3d 30 20 29 3b  ert( pList!=0 );
1cb13 20 20 2f 2a 20 70 4c 69 73 74 21 3d 30 20 69 66    /* pList!=0 if
1cb14 20 70 46 2d 3e 70 46 75 6e 63 2d 3e 6e 65 65 64   pF->pFunc->need
1cb15 43 6f 6c 6c 53 65 71 20 69 73 20 74 72 75 65 20  CollSeq is true 
1cb16 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30  */.      for(j=0
1cb17 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61  , pItem=pList->a
1cb18 3b 20 21 70 43 6f 6c 6c 20 26 26 20 6a 3c 6e 41  ; !pColl && j<nA
1cb19 72 67 3b 20 6a 2b 2b 2c 20 70 49 74 65 6d 2b 2b  rg; j++, pItem++
1cb1a 29 7b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c  ){.        pColl
1cb1b 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
1cb1c 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 49  llSeq(pParse, pI
1cb1d 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20  tem->pExpr);.   
1cb1e 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21     }.      if( !
1cb1f 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20  pColl ){.       
1cb20 20 70 43 6f 6c 6c 20 3d 20 70 50 61 72 73 65 2d   pColl = pParse-
1cb21 3e 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a  >db->pDfltColl;.
1cb22 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
1cb23 6c 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20  lite3VdbeOp3(v, 
1cb24 4f 50 5f 43 6f 6c 6c 53 65 71 2c 20 30 2c 20 30  OP_CollSeq, 0, 0
1cb25 2c 20 28 63 68 61 72 20 2a 29 70 43 6f 6c 6c 2c  , (char *)pColl,
1cb26 20 50 33 5f 43 4f 4c 4c 53 45 51 29 3b 0a 20 20   P3_COLLSEQ);.  
1cb27 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
1cb28 64 62 65 4f 70 33 28 76 2c 20 4f 50 5f 41 67 67  dbeOp3(v, OP_Agg
1cb29 53 74 65 70 2c 20 70 46 2d 3e 69 4d 65 6d 2c 20  Step, pF->iMem, 
1cb2a 6e 41 72 67 2c 20 28 76 6f 69 64 2a 29 70 46 2d  nArg, (void*)pF-
1cb2b 3e 70 46 75 6e 63 2c 20 50 33 5f 46 55 4e 43 44  >pFunc, P3_FUNCD
1cb2c 45 46 29 3b 0a 20 20 20 20 69 66 28 20 61 64 64  EF);.    if( add
1cb2d 72 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 73  rNext ){.      s
1cb2e 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
1cb2f 65 4c 61 62 65 6c 28 76 2c 20 61 64 64 72 4e 65  eLabel(v, addrNe
1cb30 78 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  xt);.    }.  }. 
1cb31 20 66 6f 72 28 69 3d 30 2c 20 70 43 3d 70 41 67   for(i=0, pC=pAg
1cb32 67 49 6e 66 6f 2d 3e 61 43 6f 6c 3b 20 69 3c 70  gInfo->aCol; i<p
1cb33 41 67 67 49 6e 66 6f 2d 3e 6e 41 63 63 75 6d 75  AggInfo->nAccumu
1cb34 6c 61 74 6f 72 3b 20 69 2b 2b 2c 20 70 43 2b 2b  lator; i++, pC++
1cb35 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  ){.    sqlite3Ex
1cb36 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70  prCode(pParse, p
1cb37 43 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20 20 73  C->pExpr);.    s
1cb38 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1cb39 76 2c 20 4f 50 5f 4d 65 6d 53 74 6f 72 65 2c 20  v, OP_MemStore, 
1cb3a 70 43 2d 3e 69 4d 65 6d 2c 20 31 29 3b 0a 20 20  pC->iMem, 1);.  
1cb3b 7d 0a 20 20 70 41 67 67 49 6e 66 6f 2d 3e 64 69  }.  pAggInfo->di
1cb3c 72 65 63 74 4d 6f 64 65 20 3d 20 30 3b 0a 7d 0a  rectMode = 0;.}.
1cb3d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
1cb3e 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 67 69   code for the gi
1cb3f 76 65 6e 20 53 45 4c 45 43 54 20 73 74 61 74 65  ven SELECT state
1cb40 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ment..**.** The 
1cb41 72 65 73 75 6c 74 73 20 61 72 65 20 64 69 73 74  results are dist
1cb42 72 69 62 75 74 65 64 20 69 6e 20 76 61 72 69 6f  ributed in vario
1cb43 75 73 20 77 61 79 73 20 64 65 70 65 6e 64 69 6e  us ways dependin
1cb44 67 20 6f 6e 20 74 68 65 0a 2a 2a 20 76 61 6c 75  g on the.** valu
1cb45 65 20 6f 66 20 65 44 65 73 74 20 61 6e 64 20 69  e of eDest and i
1cb46 50 61 72 6d 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  Parm..**.**     
1cb47 65 44 65 73 74 20 56 61 6c 75 65 20 20 20 20 20  eDest Value     
1cb48 20 20 52 65 73 75 6c 74 0a 2a 2a 20 20 20 20 20    Result.**     
1cb49 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20  ------------    
1cb4a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1cb4b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1cb4c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20  -----------.**  
1cb4d 20 20 20 53 52 54 5f 43 61 6c 6c 62 61 63 6b 20     SRT_Callback 
1cb4e 20 20 20 49 6e 76 6f 6b 65 20 74 68 65 20 63 61     Invoke the ca
1cb4f 6c 6c 62 61 63 6b 20 66 6f 72 20 65 61 63 68 20  llback for each 
1cb50 72 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c  row of the resul
1cb51 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54  t..**.**     SRT
1cb52 5f 4d 65 6d 20 20 20 20 20 20 20 20 20 53 74 6f  _Mem         Sto
1cb53 72 65 20 66 69 72 73 74 20 72 65 73 75 6c 74 20  re first result 
1cb54 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69  in memory cell i
1cb55 50 61 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53  Parm.**.**     S
1cb56 52 54 5f 53 65 74 20 20 20 20 20 20 20 20 20 53  RT_Set         S
1cb57 74 6f 72 65 20 72 65 73 75 6c 74 73 20 61 73 20  tore results as 
1cb58 6b 65 79 73 20 6f 66 20 74 61 62 6c 65 20 69 50  keys of table iP
1cb59 61 72 6d 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53  arm..**.**     S
1cb5a 52 54 5f 55 6e 69 6f 6e 20 20 20 20 20 20 20 53  RT_Union       S
1cb5b 74 6f 72 65 20 72 65 73 75 6c 74 73 20 61 73 20  tore results as 
1cb5c 61 20 6b 65 79 20 69 6e 20 61 20 74 65 6d 70 6f  a key in a tempo
1cb5d 72 61 72 79 20 74 61 62 6c 65 20 69 50 61 72 6d  rary table iParm
1cb5e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f 45  .**.**     SRT_E
1cb5f 78 63 65 70 74 20 20 20 20 20 20 52 65 6d 6f 76  xcept      Remov
1cb60 65 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 20 74  e results from t
1cb61 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  he temporary tab
1cb62 6c 65 20 69 50 61 72 6d 2e 0a 2a 2a 0a 2a 2a 20  le iParm..**.** 
1cb63 20 20 20 20 53 52 54 5f 54 61 62 6c 65 20 20 20      SRT_Table   
1cb64 20 20 20 20 53 74 6f 72 65 20 72 65 73 75 6c 74      Store result
1cb65 73 20 69 6e 20 74 65 6d 70 6f 72 61 72 79 20 74  s in temporary t
1cb66 61 62 6c 65 20 69 50 61 72 6d 0a 2a 2a 0a 2a 2a  able iParm.**.**
1cb67 20 54 68 65 20 74 61 62 6c 65 20 61 62 6f 76 65   The table above
1cb68 20 69 73 20 69 6e 63 6f 6d 70 6c 65 74 65 2e 20   is incomplete. 
1cb69 20 41 64 64 69 74 69 6f 6e 61 6c 20 65 44 69 73   Additional eDis
1cb6a 74 20 76 61 6c 75 65 20 68 61 76 65 20 62 65 20  t value have be 
1cb6b 61 64 64 65 64 0a 2a 2a 20 73 69 6e 63 65 20 74  added.** since t
1cb6c 68 69 73 20 63 6f 6d 6d 65 6e 74 20 77 61 73 20  his comment was 
1cb6d 77 72 69 74 74 65 6e 2e 20 20 53 65 65 20 74 68  written.  See th
1cb6e 65 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f  e selectInnerLoo
1cb6f 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72  p() function for
1cb70 0a 2a 2a 20 61 20 63 6f 6d 70 6c 65 74 65 20 6c  .** a complete l
1cb71 69 73 74 69 6e 67 20 6f 66 20 74 68 65 20 61 6c  isting of the al
1cb72 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 6f 66 20  lowed values of 
1cb73 65 44 65 73 74 20 61 6e 64 20 74 68 65 69 72 20  eDest and their 
1cb74 6d 65 61 6e 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20  meanings..**.** 
1cb75 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  This routine ret
1cb76 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20  urns the number 
1cb77 6f 66 20 65 72 72 6f 72 73 2e 20 20 49 66 20 61  of errors.  If a
1cb78 6e 79 20 65 72 72 6f 72 73 20 61 72 65 0a 2a 2a  ny errors are.**
1cb79 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 74 68   encountered, th
1cb7a 65 6e 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74  en an appropriat
1cb7b 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  e error message 
1cb7c 69 73 20 6c 65 66 74 20 69 6e 0a 2a 2a 20 70 50  is left in.** pP
1cb7d 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2e 0a 2a  arse->zErrMsg..*
1cb7e 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1cb7f 65 20 64 6f 65 73 20 4e 4f 54 20 66 72 65 65 20  e does NOT free 
1cb80 74 68 65 20 53 65 6c 65 63 74 20 73 74 72 75 63  the Select struc
1cb81 74 75 72 65 20 70 61 73 73 65 64 20 69 6e 2e 20  ture passed in. 
1cb82 20 54 68 65 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20   The.** calling 
1cb83 66 75 6e 63 74 69 6f 6e 20 6e 65 65 64 73 20 74  function needs t
1cb84 6f 20 64 6f 20 74 68 61 74 2e 0a 2a 2a 0a 2a 2a  o do that..**.**
1cb85 20 54 68 65 20 70 50 61 72 65 6e 74 2c 20 70 61   The pParent, pa
1cb86 72 65 6e 74 54 61 62 2c 20 61 6e 64 20 2a 70 50  rentTab, and *pP
1cb87 61 72 65 6e 74 41 67 67 20 66 69 65 6c 64 73 20  arentAgg fields 
1cb88 61 72 65 20 66 69 6c 6c 65 64 20 69 6e 20 69 66  are filled in if
1cb89 20 74 68 69 73 0a 2a 2a 20 53 45 4c 45 43 54 20   this.** SELECT 
1cb8a 69 73 20 61 20 73 75 62 71 75 65 72 79 2e 20 20  is a subquery.  
1cb8b 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 61 79  This routine may
1cb8c 20 74 72 79 20 74 6f 20 63 6f 6d 62 69 6e 65 20   try to combine 
1cb8d 74 68 69 73 20 53 45 4c 45 43 54 0a 2a 2a 20 77  this SELECT.** w
1cb8e 69 74 68 20 69 74 73 20 70 61 72 65 6e 74 20 74  ith its parent t
1cb8f 6f 20 66 6f 72 6d 20 61 20 73 69 6e 67 6c 65 20  o form a single 
1cb90 66 6c 61 74 20 71 75 65 72 79 2e 20 20 49 6e 20  flat query.  In 
1cb91 73 6f 20 64 6f 69 6e 67 2c 20 69 74 20 6d 69 67  so doing, it mig
1cb92 68 74 0a 2a 2a 20 63 68 61 6e 67 65 20 74 68 65  ht.** change the
1cb93 20 70 61 72 65 6e 74 20 71 75 65 72 79 20 66 72   parent query fr
1cb94 6f 6d 20 61 20 6e 6f 6e 2d 61 67 67 72 65 67 61  om a non-aggrega
1cb95 74 65 20 74 6f 20 61 6e 20 61 67 67 72 65 67 61  te to an aggrega
1cb96 74 65 20 71 75 65 72 79 2e 0a 2a 2a 20 46 6f 72  te query..** For
1cb97 20 74 68 61 74 20 72 65 61 73 6f 6e 2c 20 74 68   that reason, th
1cb98 65 20 70 50 61 72 65 6e 74 41 67 67 20 66 6c 61  e pParentAgg fla
1cb99 67 20 69 73 20 70 61 73 73 65 64 20 61 73 20 61  g is passed as a
1cb9a 20 70 6f 69 6e 74 65 72 2c 20 73 6f 20 69 74 0a   pointer, so it.
1cb9b 2a 2a 20 63 61 6e 20 62 65 20 63 68 61 6e 67 65  ** can be change
1cb9c 64 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65  d..**.** Example
1cb9d 20 31 3a 20 20 20 54 68 65 20 6d 65 61 6e 69 6e   1:   The meanin
1cb9e 67 20 6f 66 20 74 68 65 20 70 50 61 72 65 6e 74  g of the pParent
1cb9f 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a   parameter..**.*
1cba0 2a 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52  *    SELECT * FR
1cba1 4f 4d 20 74 31 20 4a 4f 49 4e 20 28 53 45 4c 45  OM t1 JOIN (SELE
1cba2 43 54 20 78 2c 20 63 6f 75 6e 74 28 2a 29 20 46  CT x, count(*) F
1cba3 52 4f 4d 20 74 32 29 20 4a 4f 49 4e 20 74 33 3b  ROM t2) JOIN t3;
1cba4 0a 2a 2a 20 20 20 20 5c 20 20 20 20 20 20 20 20  .**    \        
1cba5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 5f                \_
1cba6 5f 5f 5f 5f 5f 5f 20 73 75 62 71 75 65 72 79 20  ______ subquery 
1cba7 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 20 20 20  _______/        
1cba8 2f 0a 2a 2a 20 20 20 20 20 5c 20 20 20 20 20 20  /.**     \      
1cba9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cbaa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cbab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cbac 2f 0a 2a 2a 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f  /.**      \_____
1cbad 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 20  _______________ 
1cbae 6f 75 74 65 72 20 71 75 65 72 79 20 5f 5f 5f 5f  outer query ____
1cbaf 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f  _______________/
1cbb0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
1cbb1 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 66 6f  ine is called fo
1cbb2 72 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72  r the outer quer
1cbb3 79 20 66 69 72 73 74 2e 20 20 20 46 6f 72 20 74  y first.   For t
1cbb4 68 61 74 20 63 61 6c 6c 2c 0a 2a 2a 20 70 50 61  hat call,.** pPa
1cbb5 72 65 6e 74 20 77 69 6c 6c 20 62 65 20 4e 55 4c  rent will be NUL
1cbb6 4c 2e 20 20 44 75 72 69 6e 67 20 74 68 65 20 70  L.  During the p
1cbb7 72 6f 63 65 73 73 69 6e 67 20 6f 66 20 74 68 65  rocessing of the
1cbb8 20 6f 75 74 65 72 20 71 75 65 72 79 2c 20 74 68   outer query, th
1cbb9 69 73 20 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69  is .** routine i
1cbba 73 20 63 61 6c 6c 65 64 20 72 65 63 75 72 73 69  s called recursi
1cbbb 76 65 6c 79 20 74 6f 20 68 61 6e 64 6c 65 20 74  vely to handle t
1cbbc 68 65 20 73 75 62 71 75 65 72 79 2e 20 20 46 6f  he subquery.  Fo
1cbbd 72 20 74 68 65 20 72 65 63 75 72 73 69 76 65 0a  r the recursive.
1cbbe 2a 2a 20 63 61 6c 6c 2c 20 70 50 61 72 65 6e 74  ** call, pParent
1cbbf 20 77 69 6c 6c 20 70 6f 69 6e 74 20 74 6f 20 74   will point to t
1cbc0 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 2e 20  he outer query. 
1cbc1 20 42 65 63 61 75 73 65 20 74 68 65 20 73 75 62   Because the sub
1cbc2 71 75 65 72 79 20 69 73 0a 2a 2a 20 74 68 65 20  query is.** the 
1cbc3 73 65 63 6f 6e 64 20 65 6c 65 6d 65 6e 74 20 69  second element i
1cbc4 6e 20 61 20 74 68 72 65 65 2d 77 61 79 20 6a 6f  n a three-way jo
1cbc5 69 6e 2c 20 74 68 65 20 70 61 72 65 6e 74 54 61  in, the parentTa
1cbc6 62 20 70 61 72 61 6d 65 74 65 72 20 77 69 6c 6c  b parameter will
1cbc7 0a 2a 2a 20 62 65 20 31 20 28 74 68 65 20 32 6e  .** be 1 (the 2n
1cbc8 64 20 76 61 6c 75 65 20 6f 66 20 61 20 30 2d 69  d value of a 0-i
1cbc9 6e 64 65 78 65 64 20 61 72 72 61 79 2e 29 0a 2a  ndexed array.).*
1cbca 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1cbcb 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65 6c 65   int sqlite3Sele
1cbcc 63 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  ct(.  Parse *pPa
1cbcd 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  rse,         /* 
1cbce 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65  The parser conte
1cbcf 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  xt */.  Select *
1cbd0 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  p,             /
1cbd1 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61  * The SELECT sta
1cbd2 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f 64  tement being cod
1cbd3 65 64 2e 20 2a 2f 0a 20 20 69 6e 74 20 65 44 65  ed. */.  int eDe
1cbd4 73 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  st,             
1cbd5 2f 2a 20 48 6f 77 20 74 6f 20 64 69 73 70 6f 73  /* How to dispos
1cbd6 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 73  e of the results
1cbd7 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 72 6d 2c   */.  int iParm,
1cbd8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1cbd9 41 20 70 61 72 61 6d 65 74 65 72 20 75 73 65 64  A parameter used
1cbda 20 62 79 20 74 68 65 20 65 44 65 73 74 20 64 69   by the eDest di
1cbdb 73 70 6f 73 61 6c 20 6d 65 74 68 6f 64 20 2a 2f  sposal method */
1cbdc 0a 20 20 53 65 6c 65 63 74 20 2a 70 50 61 72 65  .  Select *pPare
1cbdd 6e 74 2c 20 20 20 20 20 20 20 2f 2a 20 41 6e 6f  nt,       /* Ano
1cbde 74 68 65 72 20 53 45 4c 45 43 54 20 66 6f 72 20  ther SELECT for 
1cbdf 77 68 69 63 68 20 74 68 69 73 20 69 73 20 61 20  which this is a 
1cbe0 73 75 62 2d 71 75 65 72 79 20 2a 2f 0a 20 20 69  sub-query */.  i
1cbe1 6e 74 20 70 61 72 65 6e 74 54 61 62 2c 20 20 20  nt parentTab,   
1cbe2 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69        /* Index i
1cbe3 6e 20 70 50 61 72 65 6e 74 2d 3e 70 53 72 63 20  n pParent->pSrc 
1cbe4 6f 66 20 74 68 69 73 20 71 75 65 72 79 20 2a 2f  of this query */
1cbe5 0a 20 20 69 6e 74 20 2a 70 50 61 72 65 6e 74 41  .  int *pParentA
1cbe6 67 67 2c 20 20 20 20 20 20 20 2f 2a 20 54 72 75  gg,       /* Tru
1cbe7 65 20 69 66 20 70 50 61 72 65 6e 74 20 75 73 65  e if pParent use
1cbe8 73 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  s aggregate func
1cbe9 74 69 6f 6e 73 20 2a 2f 0a 20 20 63 68 61 72 20  tions */.  char 
1cbea 2a 61 66 66 20 20 20 20 20 20 20 20 20 20 20 20  *aff            
1cbeb 20 20 2f 2a 20 49 66 20 65 44 65 73 74 20 69 73    /* If eDest is
1cbec 20 53 52 54 5f 55 6e 69 6f 6e 2c 20 74 68 65 20   SRT_Union, the 
1cbed 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20  affinity string 
1cbee 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a  */.){.  int i, j
1cbef 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
1cbf0 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20  * Loop counters 
1cbf1 2a 2f 0a 20 20 57 68 65 72 65 49 6e 66 6f 20 2a  */.  WhereInfo *
1cbf2 70 57 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20 52  pWInfo;     /* R
1cbf3 65 74 75 72 6e 20 66 72 6f 6d 20 73 71 6c 69 74  eturn from sqlit
1cbf4 65 33 57 68 65 72 65 42 65 67 69 6e 28 29 20 2a  e3WhereBegin() *
1cbf5 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20  /.  Vdbe *v;    
1cbf6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1cbf7 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e  e virtual machin
1cbf8 65 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63  e under construc
1cbf9 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 73  tion */.  int is
1cbfa 41 67 67 3b 20 20 20 20 20 20 20 20 20 20 20 20  Agg;            
1cbfb 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 73 65 6c   /* True for sel
1cbfc 65 63 74 20 6c 69 73 74 73 20 6c 69 6b 65 20 22  ect lists like "
1cbfd 63 6f 75 6e 74 28 2a 29 22 20 2a 2f 0a 20 20 45  count(*)" */.  E
1cbfe 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b  xprList *pEList;
1cbff 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66        /* List of
1cc00 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 65 78 74 72   columns to extr
1cc01 61 63 74 2e 20 2a 2f 0a 20 20 53 72 63 4c 69 73  act. */.  SrcLis
1cc02 74 20 2a 70 54 61 62 4c 69 73 74 3b 20 20 20 20  t *pTabList;    
1cc03 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 61 62 6c   /* List of tabl
1cc04 65 73 20 74 6f 20 73 65 6c 65 63 74 20 66 72 6f  es to select fro
1cc05 6d 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68  m */.  Expr *pWh
1cc06 65 72 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ere;          /*
1cc07 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   The WHERE claus
1cc08 65 2e 20 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20  e.  May be NULL 
1cc09 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
1cc0a 4f 72 64 65 72 42 79 3b 20 20 20 20 2f 2a 20 54  OrderBy;    /* T
1cc0b 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
1cc0c 73 65 2e 20 20 4d 61 79 20 62 65 20 4e 55 4c 4c  se.  May be NULL
1cc0d 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
1cc0e 70 47 72 6f 75 70 42 79 3b 20 20 20 20 2f 2a 20  pGroupBy;    /* 
1cc0f 54 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61  The GROUP BY cla
1cc10 75 73 65 2e 20 20 4d 61 79 20 62 65 20 4e 55 4c  use.  May be NUL
1cc11 4c 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 48 61  L */.  Expr *pHa
1cc12 76 69 6e 67 3b 20 20 20 20 20 20 20 20 20 2f 2a  ving;         /*
1cc13 20 54 68 65 20 48 41 56 49 4e 47 20 63 6c 61 75   The HAVING clau
1cc14 73 65 2e 20 20 4d 61 79 20 62 65 20 4e 55 4c 4c  se.  May be NULL
1cc15 20 2a 2f 0a 20 20 69 6e 74 20 69 73 44 69 73 74   */.  int isDist
1cc16 69 6e 63 74 3b 20 20 20 20 20 20 20 20 2f 2a 20  inct;        /* 
1cc17 54 72 75 65 20 69 66 20 74 68 65 20 44 49 53 54  True if the DIST
1cc18 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 69 73 20  INCT keyword is 
1cc19 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74  present */.  int
1cc1a 20 64 69 73 74 69 6e 63 74 3b 20 20 20 20 20 20   distinct;      
1cc1b 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20      /* Table to 
1cc1c 75 73 65 20 66 6f 72 20 74 68 65 20 64 69 73 74  use for the dist
1cc1d 69 6e 63 74 20 73 65 74 20 2a 2f 0a 20 20 69 6e  inct set */.  in
1cc1e 74 20 72 63 20 3d 20 31 3b 20 20 20 20 20 20 20  t rc = 1;       
1cc1f 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f       /* Value to
1cc20 20 72 65 74 75 72 6e 20 66 72 6f 6d 20 74 68 69   return from thi
1cc21 73 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20  s function */.  
1cc22 69 6e 74 20 61 64 64 72 53 6f 72 74 49 6e 64 65  int addrSortInde
1cc23 78 3b 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73  x;     /* Addres
1cc24 73 20 6f 66 20 61 6e 20 4f 50 5f 4f 70 65 6e 45  s of an OP_OpenE
1cc25 70 68 65 6d 65 72 61 6c 20 69 6e 73 74 72 75 63  phemeral instruc
1cc26 74 69 6f 6e 20 2a 2f 0a 20 20 41 67 67 49 6e 66  tion */.  AggInf
1cc27 6f 20 73 41 67 67 49 6e 66 6f 3b 20 20 20 20 20  o sAggInfo;     
1cc28 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20   /* Information 
1cc29 75 73 65 64 20 62 79 20 61 67 67 72 65 67 61 74  used by aggregat
1cc2a 65 20 71 75 65 72 69 65 73 20 2a 2f 0a 20 20 69  e queries */.  i
1cc2b 6e 74 20 69 45 6e 64 3b 20 20 20 20 20 20 20 20  nt iEnd;        
1cc2c 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73        /* Address
1cc2d 20 6f 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74   of the end of t
1cc2e 68 65 20 71 75 65 72 79 20 2a 2f 0a 0a 20 20 69  he query */..  i
1cc2f 66 28 20 70 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  f( p==0 || sqlit
1cc30 65 33 4d 61 6c 6c 6f 63 46 61 69 6c 65 64 28 29  e3MallocFailed()
1cc31 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72   || pParse->nErr
1cc32 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31   ){.    return 1
1cc33 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69  ;.  }.  if( sqli
1cc34 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
1cc35 72 73 65 2c 20 53 51 4c 49 54 45 5f 53 45 4c 45  rse, SQLITE_SELE
1cc36 43 54 2c 20 30 2c 20 30 2c 20 30 29 20 29 20 72  CT, 0, 0, 0) ) r
1cc37 65 74 75 72 6e 20 31 3b 0a 20 20 6d 65 6d 73 65  eturn 1;.  memse
1cc38 74 28 26 73 41 67 67 49 6e 66 6f 2c 20 30 2c 20  t(&sAggInfo, 0, 
1cc39 73 69 7a 65 6f 66 28 73 41 67 67 49 6e 66 6f 29  sizeof(sAggInfo)
1cc3a 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  );..#ifndef SQLI
1cc3b 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44  TE_OMIT_COMPOUND
1cc3c 5f 53 45 4c 45 43 54 0a 20 20 2f 2a 20 49 66 20  _SELECT.  /* If 
1cc3d 74 68 65 72 65 20 69 73 20 61 72 65 20 61 20 73  there is are a s
1cc3e 65 71 75 65 6e 63 65 20 6f 66 20 71 75 65 72 69  equence of queri
1cc3f 65 73 2c 20 64 6f 20 74 68 65 20 65 61 72 6c 69  es, do the earli
1cc40 65 72 20 6f 6e 65 73 20 66 69 72 73 74 2e 0a 20  er ones first.. 
1cc41 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 50 72   */.  if( p->pPr
1cc42 69 6f 72 20 29 7b 0a 20 20 20 20 69 66 28 20 70  ior ){.    if( p
1cc43 2d 3e 70 52 69 67 68 74 6d 6f 73 74 3d 3d 30 20  ->pRightmost==0 
1cc44 29 7b 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20  ){.      Select 
1cc45 2a 70 4c 6f 6f 70 3b 0a 20 20 20 20 20 20 69 6e  *pLoop;.      in
1cc46 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20  t cnt = 0;.     
1cc47 20 66 6f 72 28 70 4c 6f 6f 70 3d 70 3b 20 70 4c   for(pLoop=p; pL
1cc48 6f 6f 70 3b 20 70 4c 6f 6f 70 3d 70 4c 6f 6f 70  oop; pLoop=pLoop
1cc49 2d 3e 70 50 72 69 6f 72 2c 20 63 6e 74 2b 2b 29  ->pPrior, cnt++)
1cc4a 7b 0a 20 20 20 20 20 20 20 20 70 4c 6f 6f 70 2d  {.        pLoop-
1cc4b 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d 20 70 3b  >pRightmost = p;
1cc4c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
1cc4d 66 28 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f  f( SQLITE_MAX_CO
1cc4e 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 3e 30 20  MPOUND_SELECT>0 
1cc4f 26 26 20 63 6e 74 3e 53 51 4c 49 54 45 5f 4d 41  && cnt>SQLITE_MA
1cc50 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43  X_COMPOUND_SELEC
1cc51 54 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  T ){.        sql
1cc52 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
1cc53 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 74  rse, "too many t
1cc54 65 72 6d 73 20 69 6e 20 63 6f 6d 70 6f 75 6e 64  erms in compound
1cc55 20 53 45 4c 45 43 54 22 29 3b 0a 20 20 20 20 20   SELECT");.     
1cc56 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
1cc57 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 72     }.    }.    r
1cc58 65 74 75 72 6e 20 6d 75 6c 74 69 53 65 6c 65 63  eturn multiSelec
1cc59 74 28 70 50 61 72 73 65 2c 20 70 2c 20 65 44 65  t(pParse, p, eDe
1cc5a 73 74 2c 20 69 50 61 72 6d 2c 20 61 66 66 29 3b  st, iParm, aff);
1cc5b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 70  .  }.#endif..  p
1cc5c 4f 72 64 65 72 42 79 20 3d 20 70 2d 3e 70 4f 72  OrderBy = p->pOr
1cc5d 64 65 72 42 79 3b 0a 20 20 69 66 28 20 49 67 6e  derBy;.  if( Ign
1cc5e 6f 72 61 62 6c 65 4f 72 64 65 72 62 79 28 65 44  orableOrderby(eD
1cc5f 65 73 74 29 20 29 7b 0a 20 20 20 20 70 2d 3e 70  est) ){.    p->p
1cc60 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20 7d  OrderBy = 0;.  }
1cc61 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 65  .  if( sqlite3Se
1cc62 6c 65 63 74 52 65 73 6f 6c 76 65 28 70 50 61 72  lectResolve(pPar
1cc63 73 65 2c 20 70 2c 20 30 29 20 29 7b 0a 20 20 20  se, p, 0) ){.   
1cc64 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64   goto select_end
1cc65 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4f 72 64 65  ;.  }.  p->pOrde
1cc66 72 42 79 20 3d 20 70 4f 72 64 65 72 42 79 3b 0a  rBy = pOrderBy;.
1cc67 0a 20 20 2f 2a 20 4d 61 6b 65 20 6c 6f 63 61 6c  .  /* Make local
1cc68 20 63 6f 70 69 65 73 20 6f 66 20 74 68 65 20 70   copies of the p
1cc69 61 72 61 6d 65 74 65 72 73 20 66 6f 72 20 74 68  arameters for th
1cc6a 69 73 20 71 75 65 72 79 2e 0a 20 20 2a 2f 0a 20  is query..  */. 
1cc6b 20 70 54 61 62 4c 69 73 74 20 3d 20 70 2d 3e 70   pTabList = p->p
1cc6c 53 72 63 3b 0a 20 20 70 57 68 65 72 65 20 3d 20  Src;.  pWhere = 
1cc6d 70 2d 3e 70 57 68 65 72 65 3b 0a 20 20 70 47 72  p->pWhere;.  pGr
1cc6e 6f 75 70 42 79 20 3d 20 70 2d 3e 70 47 72 6f 75  oupBy = p->pGrou
1cc6f 70 42 79 3b 0a 20 20 70 48 61 76 69 6e 67 20 3d  pBy;.  pHaving =
1cc70 20 70 2d 3e 70 48 61 76 69 6e 67 3b 0a 20 20 69   p->pHaving;.  i
1cc71 73 41 67 67 20 3d 20 70 2d 3e 69 73 41 67 67 3b  sAgg = p->isAgg;
1cc72 0a 20 20 69 73 44 69 73 74 69 6e 63 74 20 3d 20  .  isDistinct = 
1cc73 70 2d 3e 69 73 44 69 73 74 69 6e 63 74 3b 0a 20  p->isDistinct;. 
1cc74 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c   pEList = p->pEL
1cc75 69 73 74 3b 0a 20 20 69 66 28 20 70 45 4c 69 73  ist;.  if( pELis
1cc76 74 3d 3d 30 20 29 20 67 6f 74 6f 20 73 65 6c 65  t==0 ) goto sele
1cc77 63 74 5f 65 6e 64 3b 0a 0a 20 20 2f 2a 20 0a 20  ct_end;..  /* . 
1cc78 20 2a 2a 20 44 6f 20 6e 6f 74 20 65 76 65 6e 20   ** Do not even 
1cc79 61 74 74 65 6d 70 74 20 74 6f 20 67 65 6e 65 72  attempt to gener
1cc7a 61 74 65 20 61 6e 79 20 63 6f 64 65 20 69 66 20  ate any code if 
1cc7b 77 65 20 68 61 76 65 20 61 6c 72 65 61 64 79 20  we have already 
1cc7c 73 65 65 6e 0a 20 20 2a 2a 20 65 72 72 6f 72 73  seen.  ** errors
1cc7d 20 62 65 66 6f 72 65 20 74 68 69 73 20 72 6f 75   before this rou
1cc7e 74 69 6e 65 20 73 74 61 72 74 73 2e 0a 20 20 2a  tine starts..  *
1cc7f 2f 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  /.  if( pParse->
1cc80 6e 45 72 72 3e 30 20 29 20 67 6f 74 6f 20 73 65  nErr>0 ) goto se
1cc81 6c 65 63 74 5f 65 6e 64 3b 0a 0a 20 20 2f 2a 20  lect_end;..  /* 
1cc82 49 66 20 77 72 69 74 69 6e 67 20 74 6f 20 6d 65  If writing to me
1cc83 6d 6f 72 79 20 6f 72 20 67 65 6e 65 72 61 74 69  mory or generati
1cc84 6e 67 20 61 20 73 65 74 0a 20 20 2a 2a 20 6f 6e  ng a set.  ** on
1cc85 6c 79 20 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75  ly a single colu
1cc86 6d 6e 20 6d 61 79 20 62 65 20 6f 75 74 70 75 74  mn may be output
1cc87 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ..  */.#ifndef S
1cc88 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
1cc89 45 52 59 0a 20 20 69 66 28 20 63 68 65 63 6b 46  ERY.  if( checkF
1cc8a 6f 72 4d 75 6c 74 69 43 6f 6c 75 6d 6e 53 65 6c  orMultiColumnSel
1cc8b 65 63 74 45 72 72 6f 72 28 70 50 61 72 73 65 2c  ectError(pParse,
1cc8c 20 65 44 65 73 74 2c 20 70 45 4c 69 73 74 2d 3e   eDest, pEList->
1cc8d 6e 45 78 70 72 29 20 29 7b 0a 20 20 20 20 67 6f  nExpr) ){.    go
1cc8e 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20  to select_end;. 
1cc8f 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20   }.#endif..  /* 
1cc90 4f 52 44 45 52 20 42 59 20 69 73 20 69 67 6e 6f  ORDER BY is igno
1cc91 72 65 64 20 66 6f 72 20 73 6f 6d 65 20 64 65 73  red for some des
1cc92 74 69 6e 61 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a  tinations..  */.
1cc93 20 20 69 66 28 20 49 67 6e 6f 72 61 62 6c 65 4f    if( IgnorableO
1cc94 72 64 65 72 62 79 28 65 44 65 73 74 29 20 29 7b  rderby(eDest) ){
1cc95 0a 20 20 20 20 70 4f 72 64 65 72 42 79 20 3d 20  .    pOrderBy = 
1cc96 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 65 67  0;.  }..  /* Beg
1cc97 69 6e 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f  in generating co
1cc98 64 65 2e 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 73  de..  */.  v = s
1cc99 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
1cc9a 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d  arse);.  if( v==
1cc9b 30 20 29 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f  0 ) goto select_
1cc9c 65 6e 64 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72  end;..  /* Gener
1cc9d 61 74 65 20 63 6f 64 65 20 66 6f 72 20 61 6c 6c  ate code for all
1cc9e 20 73 75 62 2d 71 75 65 72 69 65 73 20 69 6e 20   sub-queries in 
1cc9f 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 0a  the FROM clause.
1cca0 20 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65    */.#if !define
1cca1 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  d(SQLITE_OMIT_SU
1cca2 42 51 55 45 52 59 29 20 7c 7c 20 21 64 65 66 69  BQUERY) || !defi
1cca3 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
1cca4 56 49 45 57 29 0a 20 20 66 6f 72 28 69 3d 30 3b  VIEW).  for(i=0;
1cca5 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72   i<pTabList->nSr
1cca6 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 6f 6e  c; i++){.    con
1cca7 73 74 20 63 68 61 72 20 2a 7a 53 61 76 65 64 41  st char *zSavedA
1cca8 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 30 3b 0a  uthContext = 0;.
1cca9 20 20 20 20 69 6e 74 20 6e 65 65 64 52 65 73 74      int needRest
1ccaa 6f 72 65 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20  oreContext;.    
1ccab 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
1ccac 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 54  tem *pItem = &pT
1ccad 61 62 4c 69 73 74 2d 3e 61 5b 69 5d 3b 0a 0a 20  abList->a[i];.. 
1ccae 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 70 53     if( pItem->pS
1ccaf 65 6c 65 63 74 3d 3d 30 20 7c 7c 20 70 49 74 65  elect==0 || pIte
1ccb0 6d 2d 3e 69 73 50 6f 70 75 6c 61 74 65 64 20 29  m->isPopulated )
1ccb1 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69   continue;.    i
1ccb2 66 28 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 21  f( pItem->zName!
1ccb3 3d 30 20 29 7b 0a 20 20 20 20 20 20 7a 53 61 76  =0 ){.      zSav
1ccb4 65 64 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20  edAuthContext = 
1ccb5 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e  pParse->zAuthCon
1ccb6 74 65 78 74 3b 0a 20 20 20 20 20 20 70 50 61 72  text;.      pPar
1ccb7 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74  se->zAuthContext
1ccb8 20 3d 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 3b   = pItem->zName;
1ccb9 0a 20 20 20 20 20 20 6e 65 65 64 52 65 73 74 6f  .      needResto
1ccba 72 65 43 6f 6e 74 65 78 74 20 3d 20 31 3b 0a 20  reContext = 1;. 
1ccbb 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1ccbc 6e 65 65 64 52 65 73 74 6f 72 65 43 6f 6e 74 65  needRestoreConte
1ccbd 78 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 23 69  xt = 0;.    }.#i
1ccbe 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50  f SQLITE_MAX_EXP
1ccbf 52 5f 44 45 50 54 48 3e 30 0a 20 20 20 20 2f 2a  R_DEPTH>0.    /*
1ccc0 20 49 6e 63 72 65 6d 65 6e 74 20 50 61 72 73 65   Increment Parse
1ccc1 2e 6e 48 65 69 67 68 74 20 62 79 20 74 68 65 20  .nHeight by the 
1ccc2 68 65 69 67 68 74 20 6f 66 20 74 68 65 20 6c 61  height of the la
1ccc3 72 67 65 73 74 20 65 78 70 72 65 73 73 69 6f 6e  rgest expression
1ccc4 0a 20 20 20 20 2a 2a 20 74 72 65 65 20 72 65 66  .    ** tree ref
1ccc5 65 72 65 64 20 74 6f 20 62 79 20 74 68 69 73 2c  ered to by this,
1ccc6 20 74 68 65 20 70 61 72 65 6e 74 20 73 65 6c 65   the parent sele
1ccc7 63 74 2e 20 54 68 65 20 63 68 69 6c 64 20 73 65  ct. The child se
1ccc8 6c 65 63 74 0a 20 20 20 20 2a 2a 20 6d 61 79 20  lect.    ** may 
1ccc9 63 6f 6e 74 61 69 6e 20 65 78 70 72 65 73 73 69  contain expressi
1ccca 6f 6e 20 74 72 65 65 73 20 6f 66 20 61 74 20 6d  on trees of at m
1cccb 6f 73 74 0a 20 20 20 20 2a 2a 20 28 53 51 4c 49  ost.    ** (SQLI
1cccc 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54  TE_MAX_EXPR_DEPT
1cccd 48 2d 50 61 72 73 65 2e 6e 48 65 69 67 68 74 29  H-Parse.nHeight)
1ccce 20 68 65 69 67 68 74 2e 20 54 68 69 73 20 69 73   height. This is
1cccf 20 61 20 62 69 74 0a 20 20 20 20 2a 2a 20 6d 6f   a bit.    ** mo
1ccd0 72 65 20 63 6f 6e 73 65 72 76 61 74 69 76 65 20  re conservative 
1ccd1 74 68 61 6e 20 6e 65 63 65 73 73 61 72 79 2c 20  than necessary, 
1ccd2 62 75 74 20 6d 75 63 68 20 65 61 73 69 65 72 20  but much easier 
1ccd3 74 68 61 6e 20 65 6e 66 6f 72 63 69 6e 67 0a 20  than enforcing. 
1ccd4 20 20 20 2a 2a 20 61 6e 20 65 78 61 63 74 20 6c     ** an exact l
1ccd5 69 6d 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  imit..    */.   
1ccd6 20 70 50 61 72 73 65 2d 3e 6e 48 65 69 67 68 74   pParse->nHeight
1ccd7 20 2b 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63   += sqlite3Selec
1ccd8 74 45 78 70 72 48 65 69 67 68 74 28 70 29 3b 0a  tExprHeight(p);.
1ccd9 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c 69 74  #endif.    sqlit
1ccda 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c  e3Select(pParse,
1ccdb 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 2c   pItem->pSelect,
1ccdc 20 53 52 54 5f 45 70 68 65 6d 54 61 62 2c 20 0a   SRT_EphemTab, .
1ccdd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ccde 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 2c   pItem->iCursor,
1ccdf 20 70 2c 20 69 2c 20 26 69 73 41 67 67 2c 20 30   p, i, &isAgg, 0
1cce0 29 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41  );.#if SQLITE_MA
1cce1 58 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 20  X_EXPR_DEPTH>0. 
1cce2 20 20 20 70 50 61 72 73 65 2d 3e 6e 48 65 69 67     pParse->nHeig
1cce3 68 74 20 2d 3d 20 73 71 6c 69 74 65 33 53 65 6c  ht -= sqlite3Sel
1cce4 65 63 74 45 78 70 72 48 65 69 67 68 74 28 70 29  ectExprHeight(p)
1cce5 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28  ;.#endif.    if(
1cce6 20 6e 65 65 64 52 65 73 74 6f 72 65 43 6f 6e 74   needRestoreCont
1cce7 65 78 74 20 29 7b 0a 20 20 20 20 20 20 70 50 61  ext ){.      pPa
1cce8 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78  rse->zAuthContex
1cce9 74 20 3d 20 7a 53 61 76 65 64 41 75 74 68 43 6f  t = zSavedAuthCo
1ccea 6e 74 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20  ntext;.    }.   
1cceb 20 70 54 61 62 4c 69 73 74 20 3d 20 70 2d 3e 70   pTabList = p->p
1ccec 53 72 63 3b 0a 20 20 20 20 70 57 68 65 72 65 20  Src;.    pWhere 
1cced 3d 20 70 2d 3e 70 57 68 65 72 65 3b 0a 20 20 20  = p->pWhere;.   
1ccee 20 69 66 28 20 21 49 67 6e 6f 72 61 62 6c 65 4f   if( !IgnorableO
1ccef 72 64 65 72 62 79 28 65 44 65 73 74 29 20 29 7b  rderby(eDest) ){
1ccf0 0a 20 20 20 20 20 20 70 4f 72 64 65 72 42 79 20  .      pOrderBy 
1ccf1 3d 20 70 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20  = p->pOrderBy;. 
1ccf2 20 20 20 7d 0a 20 20 20 20 70 47 72 6f 75 70 42     }.    pGroupB
1ccf3 79 20 3d 20 70 2d 3e 70 47 72 6f 75 70 42 79 3b  y = p->pGroupBy;
1ccf4 0a 20 20 20 20 70 48 61 76 69 6e 67 20 3d 20 70  .    pHaving = p
1ccf5 2d 3e 70 48 61 76 69 6e 67 3b 0a 20 20 20 20 69  ->pHaving;.    i
1ccf6 73 44 69 73 74 69 6e 63 74 20 3d 20 70 2d 3e 69  sDistinct = p->i
1ccf7 73 44 69 73 74 69 6e 63 74 3b 0a 20 20 7d 0a 23  sDistinct;.  }.#
1ccf8 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 43 68 65 63  endif..  /* Chec
1ccf9 6b 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 61  k for the specia
1ccfa 6c 20 63 61 73 65 20 6f 66 20 61 20 6d 69 6e 28  l case of a min(
1ccfb 29 20 6f 72 20 6d 61 78 28 29 20 66 75 6e 63 74  ) or max() funct
1ccfc 69 6f 6e 20 62 79 20 69 74 73 65 6c 66 0a 20 20  ion by itself.  
1ccfd 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  ** in the result
1ccfe 20 73 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28   set..  */.  if(
1ccff 20 73 69 6d 70 6c 65 4d 69 6e 4d 61 78 51 75 65   simpleMinMaxQue
1cd00 72 79 28 70 50 61 72 73 65 2c 20 70 2c 20 65 44  ry(pParse, p, eD
1cd01 65 73 74 2c 20 69 50 61 72 6d 29 20 29 7b 0a 20  est, iParm) ){. 
1cd02 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 67     rc = 0;.    g
1cd03 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a  oto select_end;.
1cd04 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20    }..  /* Check 
1cd05 74 6f 20 73 65 65 20 69 66 20 74 68 69 73 20 69  to see if this i
1cd06 73 20 61 20 73 75 62 71 75 65 72 79 20 74 68 61  s a subquery tha
1cd07 74 20 63 61 6e 20 62 65 20 22 66 6c 61 74 74 65  t can be "flatte
1cd08 6e 65 64 22 20 69 6e 74 6f 20 69 74 73 20 70 61  ned" into its pa
1cd09 72 65 6e 74 2e 0a 20 20 2a 2a 20 49 66 20 66 6c  rent..  ** If fl
1cd0a 61 74 74 65 6e 69 6e 67 20 69 73 20 61 20 70 6f  attening is a po
1cd0b 73 73 69 62 6c 69 74 79 2c 20 64 6f 20 73 6f 20  ssiblity, do so 
1cd0c 61 6e 64 20 72 65 74 75 72 6e 20 69 6d 6d 65 64  and return immed
1cd0d 69 61 74 65 6c 79 2e 20 20 0a 20 20 2a 2f 0a 23  iately.  .  */.#
1cd0e 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1cd0f 49 54 5f 56 49 45 57 0a 20 20 69 66 28 20 70 50  IT_VIEW.  if( pP
1cd10 61 72 65 6e 74 20 26 26 20 70 50 61 72 65 6e 74  arent && pParent
1cd11 41 67 67 20 26 26 0a 20 20 20 20 20 20 66 6c 61  Agg &&.      fla
1cd12 74 74 65 6e 53 75 62 71 75 65 72 79 28 70 50 61  ttenSubquery(pPa
1cd13 72 65 6e 74 2c 20 70 61 72 65 6e 74 54 61 62 2c  rent, parentTab,
1cd14 20 2a 70 50 61 72 65 6e 74 41 67 67 2c 20 69 73   *pParentAgg, is
1cd15 41 67 67 29 20 29 7b 0a 20 20 20 20 69 66 28 20  Agg) ){.    if( 
1cd16 69 73 41 67 67 20 29 20 2a 70 50 61 72 65 6e 74  isAgg ) *pParent
1cd17 41 67 67 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74  Agg = 1;.    got
1cd18 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20  o select_end;.  
1cd19 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49  }.#endif..  /* I
1cd1a 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 4f 52  f there is an OR
1cd1b 44 45 52 20 42 59 20 63 6c 61 75 73 65 2c 20 74  DER BY clause, t
1cd1c 68 65 6e 20 74 68 69 73 20 73 6f 72 74 69 6e 67  hen this sorting
1cd1d 0a 20 20 2a 2a 20 69 6e 64 65 78 20 6d 69 67 68  .  ** index migh
1cd1e 74 20 65 6e 64 20 75 70 20 62 65 69 6e 67 20 75  t end up being u
1cd1f 6e 75 73 65 64 20 69 66 20 74 68 65 20 64 61 74  nused if the dat
1cd20 61 20 63 61 6e 20 62 65 20 0a 20 20 2a 2a 20 65  a can be .  ** e
1cd21 78 74 72 61 63 74 65 64 20 69 6e 20 70 72 65 2d  xtracted in pre-
1cd22 73 6f 72 74 65 64 20 6f 72 64 65 72 2e 20 20 49  sorted order.  I
1cd23 66 20 74 68 61 74 20 69 73 20 74 68 65 20 63 61  f that is the ca
1cd24 73 65 2c 20 74 68 65 6e 20 74 68 65 0a 20 20 2a  se, then the.  *
1cd25 2a 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72  * OP_OpenEphemer
1cd26 61 6c 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 77  al instruction w
1cd27 69 6c 6c 20 62 65 20 63 68 61 6e 67 65 64 20 74  ill be changed t
1cd28 6f 20 61 6e 20 4f 50 5f 4e 6f 6f 70 20 6f 6e 63  o an OP_Noop onc
1cd29 65 0a 20 20 2a 2a 20 77 65 20 66 69 67 75 72 65  e.  ** we figure
1cd2a 20 6f 75 74 20 74 68 61 74 20 74 68 65 20 73 6f   out that the so
1cd2b 72 74 69 6e 67 20 69 6e 64 65 78 20 69 73 20 6e  rting index is n
1cd2c 6f 74 20 6e 65 65 64 65 64 2e 20 20 54 68 65 20  ot needed.  The 
1cd2d 61 64 64 72 53 6f 72 74 49 6e 64 65 78 0a 20 20  addrSortIndex.  
1cd2e 2a 2a 20 76 61 72 69 61 62 6c 65 20 69 73 20 75  ** variable is u
1cd2f 73 65 64 20 74 6f 20 66 61 63 69 6c 69 74 61 74  sed to facilitat
1cd30 65 20 74 68 61 74 20 63 68 61 6e 67 65 2e 0a 20  e that change.. 
1cd31 20 2a 2f 0a 20 20 69 66 28 20 70 4f 72 64 65 72   */.  if( pOrder
1cd32 42 79 20 29 7b 0a 20 20 20 20 4b 65 79 49 6e 66  By ){.    KeyInf
1cd33 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20  o *pKeyInfo;.   
1cd34 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72   if( pParse->nEr
1cd35 72 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  r ){.      goto 
1cd36 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20  select_end;.    
1cd37 7d 0a 20 20 20 20 70 4b 65 79 49 6e 66 6f 20 3d  }.    pKeyInfo =
1cd38 20 6b 65 79 49 6e 66 6f 46 72 6f 6d 45 78 70 72   keyInfoFromExpr
1cd39 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 4f 72  List(pParse, pOr
1cd3a 64 65 72 42 79 29 3b 0a 20 20 20 20 70 4f 72 64  derBy);.    pOrd
1cd3b 65 72 42 79 2d 3e 69 45 43 75 72 73 6f 72 20 3d  erBy->iECursor =
1cd3c 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b   pParse->nTab++;
1cd3d 0a 20 20 20 20 70 2d 3e 61 64 64 72 4f 70 65 6e  .    p->addrOpen
1cd3e 45 70 68 6d 5b 32 5d 20 3d 20 61 64 64 72 53 6f  Ephm[2] = addrSo
1cd3f 72 74 49 6e 64 65 78 20 3d 0a 20 20 20 20 20 20  rtIndex =.      
1cd40 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28 76  sqlite3VdbeOp3(v
1cd41 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72  , OP_OpenEphemer
1cd42 61 6c 2c 20 70 4f 72 64 65 72 42 79 2d 3e 69 45  al, pOrderBy->iE
1cd43 43 75 72 73 6f 72 2c 20 70 4f 72 64 65 72 42 79  Cursor, pOrderBy
1cd44 2d 3e 6e 45 78 70 72 2b 32 2c 20 20 20 20 20 20  ->nExpr+2,      
1cd45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
1cd46 63 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c 20  char*)pKeyInfo, 
1cd47 50 33 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f  P3_KEYINFO_HANDO
1cd48 46 46 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  FF);.  }else{.  
1cd49 20 20 61 64 64 72 53 6f 72 74 49 6e 64 65 78 20    addrSortIndex 
1cd4a 3d 20 2d 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  = -1;.  }..  /* 
1cd4b 49 66 20 74 68 65 20 6f 75 74 70 75 74 20 69 73  If the output is
1cd4c 20 64 65 73 74 69 6e 65 64 20 66 6f 72 20 61 20   destined for a 
1cd4d 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 2c  temporary table,
1cd4e 20 6f 70 65 6e 20 74 68 61 74 20 74 61 62 6c 65   open that table
1cd4f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 65 44 65  ..  */.  if( eDe
1cd50 73 74 3d 3d 53 52 54 5f 45 70 68 65 6d 54 61 62  st==SRT_EphemTab
1cd51 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
1cd52 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4f  dbeAddOp(v, OP_O
1cd53 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 69 50  penEphemeral, iP
1cd54 61 72 6d 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78  arm, pEList->nEx
1cd55 70 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53  pr);.  }..  /* S
1cd56 65 74 20 74 68 65 20 6c 69 6d 69 74 65 72 2e 0a  et the limiter..
1cd57 20 20 2a 2f 0a 20 20 69 45 6e 64 20 3d 20 73 71    */.  iEnd = sq
1cd58 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
1cd59 65 6c 28 76 29 3b 0a 20 20 63 6f 6d 70 75 74 65  el(v);.  compute
1cd5a 4c 69 6d 69 74 52 65 67 69 73 74 65 72 73 28 70  LimitRegisters(p
1cd5b 50 61 72 73 65 2c 20 70 2c 20 69 45 6e 64 29 3b  Parse, p, iEnd);
1cd5c 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 61 20 76 69  ..  /* Open a vi
1cd5d 72 74 75 61 6c 20 69 6e 64 65 78 20 74 6f 20 75  rtual index to u
1cd5e 73 65 20 66 6f 72 20 74 68 65 20 64 69 73 74 69  se for the disti
1cd5f 6e 63 74 20 73 65 74 2e 0a 20 20 2a 2f 0a 20 20  nct set..  */.  
1cd60 69 66 28 20 69 73 44 69 73 74 69 6e 63 74 20 29  if( isDistinct )
1cd61 7b 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70  {.    KeyInfo *p
1cd62 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 64 69 73  KeyInfo;.    dis
1cd63 74 69 6e 63 74 20 3d 20 70 50 61 72 73 65 2d 3e  tinct = pParse->
1cd64 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 70 4b 65 79  nTab++;.    pKey
1cd65 49 6e 66 6f 20 3d 20 6b 65 79 49 6e 66 6f 46 72  Info = keyInfoFr
1cd66 6f 6d 45 78 70 72 4c 69 73 74 28 70 50 61 72 73  omExprList(pPars
1cd67 65 2c 20 70 2d 3e 70 45 4c 69 73 74 29 3b 0a 20  e, p->pEList);. 
1cd68 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70     sqlite3VdbeOp
1cd69 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65  3(v, OP_OpenEphe
1cd6a 6d 65 72 61 6c 2c 20 64 69 73 74 69 6e 63 74 2c  meral, distinct,
1cd6b 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20   0, .           
1cd6c 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63 68               (ch
1cd6d 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c 20 50 33  ar*)pKeyInfo, P3
1cd6e 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46  _KEYINFO_HANDOFF
1cd6f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
1cd70 64 69 73 74 69 6e 63 74 20 3d 20 2d 31 3b 0a 20  distinct = -1;. 
1cd71 20 7d 0a 0a 20 20 2f 2a 20 41 67 67 72 65 67 61   }..  /* Aggrega
1cd72 74 65 20 61 6e 64 20 6e 6f 6e 2d 61 67 67 72 65  te and non-aggre
1cd73 67 61 74 65 20 71 75 65 72 69 65 73 20 61 72 65  gate queries are
1cd74 20 68 61 6e 64 6c 65 64 20 64 69 66 66 65 72 65   handled differe
1cd75 6e 74 6c 79 20 2a 2f 0a 20 20 69 66 28 20 21 69  ntly */.  if( !i
1cd76 73 41 67 67 20 26 26 20 70 47 72 6f 75 70 42 79  sAgg && pGroupBy
1cd77 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  ==0 ){.    /* Th
1cd78 69 73 20 63 61 73 65 20 69 73 20 66 6f 72 20 6e  is case is for n
1cd79 6f 6e 2d 61 67 67 72 65 67 61 74 65 20 71 75 65  on-aggregate que
1cd7a 72 69 65 73 0a 20 20 20 20 2a 2a 20 42 65 67 69  ries.    ** Begi
1cd7b 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  n the database s
1cd7c 63 61 6e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70  can.    */.    p
1cd7d 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 57  WInfo = sqlite3W
1cd7e 68 65 72 65 42 65 67 69 6e 28 70 50 61 72 73 65  hereBegin(pParse
1cd7f 2c 20 70 54 61 62 4c 69 73 74 2c 20 70 57 68 65  , pTabList, pWhe
1cd80 72 65 2c 20 26 70 4f 72 64 65 72 42 79 29 3b 0a  re, &pOrderBy);.
1cd81 20 20 20 20 69 66 28 20 70 57 49 6e 66 6f 3d 3d      if( pWInfo==
1cd82 30 20 29 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f  0 ) goto select_
1cd83 65 6e 64 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20  end;..    /* If 
1cd84 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 74 68  sorting index th
1cd85 61 74 20 77 61 73 20 63 72 65 61 74 65 64 20 62  at was created b
1cd86 79 20 61 20 70 72 69 6f 72 20 4f 50 5f 4f 70 65  y a prior OP_Ope
1cd87 6e 45 70 68 65 6d 65 72 61 6c 20 0a 20 20 20 20  nEphemeral .    
1cd88 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 65  ** instruction e
1cd89 6e 64 65 64 20 75 70 20 6e 6f 74 20 62 65 69 6e  nded up not bein
1cd8a 67 20 6e 65 65 64 65 64 2c 20 74 68 65 6e 20 63  g needed, then c
1cd8b 68 61 6e 67 65 20 74 68 65 20 4f 50 5f 4f 70 65  hange the OP_Ope
1cd8c 6e 45 70 68 65 6d 65 72 61 6c 0a 20 20 20 20 2a  nEphemeral.    *
1cd8d 2a 20 69 6e 74 6f 20 61 6e 20 4f 50 5f 4e 6f 6f  * into an OP_Noo
1cd8e 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  p..    */.    if
1cd8f 28 20 61 64 64 72 53 6f 72 74 49 6e 64 65 78 3e  ( addrSortIndex>
1cd90 3d 30 20 26 26 20 70 4f 72 64 65 72 42 79 3d 3d  =0 && pOrderBy==
1cd91 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
1cd92 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f  e3VdbeChangeToNo
1cd93 6f 70 28 76 2c 20 61 64 64 72 53 6f 72 74 49 6e  op(v, addrSortIn
1cd94 64 65 78 2c 20 31 29 3b 0a 20 20 20 20 20 20 70  dex, 1);.      p
1cd95 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 32  ->addrOpenEphm[2
1cd96 5d 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 0a 20  ] = -1;.    }.. 
1cd97 20 20 20 2f 2a 20 55 73 65 20 74 68 65 20 73 74     /* Use the st
1cd98 61 6e 64 61 72 64 20 69 6e 6e 65 72 20 6c 6f 6f  andard inner loo
1cd99 70 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  p.    */.    if(
1cd9a 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70   selectInnerLoop
1cd9b 28 70 50 61 72 73 65 2c 20 70 2c 20 70 45 4c 69  (pParse, p, pELi
1cd9c 73 74 2c 20 30 2c 20 30 2c 20 70 4f 72 64 65 72  st, 0, 0, pOrder
1cd9d 42 79 2c 20 64 69 73 74 69 6e 63 74 2c 20 65 44  By, distinct, eD
1cd9e 65 73 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20  est,.           
1cd9f 20 20 20 20 20 20 20 20 20 69 50 61 72 6d 2c 20           iParm, 
1cda0 70 57 49 6e 66 6f 2d 3e 69 43 6f 6e 74 69 6e 75  pWInfo->iContinu
1cda1 65 2c 20 70 57 49 6e 66 6f 2d 3e 69 42 72 65 61  e, pWInfo->iBrea
1cda2 6b 2c 20 61 66 66 29 20 29 7b 0a 20 20 20 20 20  k, aff) ){.     
1cda3 20 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e    goto select_en
1cda4 64 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  d;.    }..    /*
1cda5 20 45 6e 64 20 74 68 65 20 64 61 74 61 62 61 73   End the databas
1cda6 65 20 73 63 61 6e 20 6c 6f 6f 70 2e 0a 20 20 20  e scan loop..   
1cda7 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 57   */.    sqlite3W
1cda8 68 65 72 65 45 6e 64 28 70 57 49 6e 66 6f 29 3b  hereEnd(pWInfo);
1cda9 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
1cdaa 20 54 68 69 73 20 69 73 20 74 68 65 20 70 72 6f   This is the pro
1cdab 63 65 73 73 69 6e 67 20 66 6f 72 20 61 67 67 72  cessing for aggr
1cdac 65 67 61 74 65 20 71 75 65 72 69 65 73 20 2a 2f  egate queries */
1cdad 0a 20 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74  .    NameContext
1cdae 20 73 4e 43 3b 20 20 20 20 2f 2a 20 4e 61 6d 65   sNC;    /* Name
1cdaf 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 70 72 6f   context for pro
1cdb0 63 65 73 73 69 6e 67 20 61 67 67 72 65 67 61 74  cessing aggregat
1cdb1 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f  e information */
1cdb2 0a 20 20 20 20 69 6e 74 20 69 41 4d 65 6d 3b 20  .    int iAMem; 
1cdb3 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73           /* Firs
1cdb4 74 20 4d 65 6d 20 61 64 64 72 65 73 73 20 66 6f  t Mem address fo
1cdb5 72 20 73 74 6f 72 69 6e 67 20 63 75 72 72 65 6e  r storing curren
1cdb6 74 20 47 52 4f 55 50 20 42 59 20 2a 2f 0a 20 20  t GROUP BY */.  
1cdb7 20 20 69 6e 74 20 69 42 4d 65 6d 3b 20 20 20 20    int iBMem;    
1cdb8 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 4d        /* First M
1cdb9 65 6d 20 61 64 64 72 65 73 73 20 66 6f 72 20 70  em address for p
1cdba 72 65 76 69 6f 75 73 20 47 52 4f 55 50 20 42 59  revious GROUP BY
1cdbb 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 55 73 65   */.    int iUse
1cdbc 46 6c 61 67 3b 20 20 20 20 20 20 20 2f 2a 20 4d  Flag;       /* M
1cdbd 65 6d 20 61 64 64 72 65 73 73 20 68 6f 6c 64 69  em address holdi
1cdbe 6e 67 20 66 6c 61 67 20 69 6e 64 69 63 61 74 69  ng flag indicati
1cdbf 6e 67 20 74 68 61 74 20 61 74 20 6c 65 61 73 74  ng that at least
1cdc0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1cdc1 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e 65 20           ** one 
1cdc2 72 6f 77 20 6f 66 20 74 68 65 20 69 6e 70 75 74  row of the input
1cdc3 20 74 6f 20 74 68 65 20 61 67 67 72 65 67 61 74   to the aggregat
1cdc4 6f 72 20 68 61 73 20 62 65 65 6e 0a 20 20 20 20  or has been.    
1cdc5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cdc6 20 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 65 64      ** processed
1cdc7 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 41 62 6f   */.    int iAbo
1cdc8 72 74 46 6c 61 67 3b 20 20 20 20 20 2f 2a 20 4d  rtFlag;     /* M
1cdc9 65 6d 20 61 64 64 72 65 73 73 20 77 68 69 63 68  em address which
1cdca 20 63 61 75 73 65 73 20 71 75 65 72 79 20 61 62   causes query ab
1cdcb 6f 72 74 20 69 66 20 70 6f 73 69 74 69 76 65 20  ort if positive 
1cdcc 2a 2f 0a 20 20 20 20 69 6e 74 20 67 72 6f 75 70  */.    int group
1cdcd 42 79 53 6f 72 74 3b 20 20 20 20 2f 2a 20 52 6f  BySort;    /* Ro
1cdce 77 73 20 63 6f 6d 65 20 66 72 6f 6d 20 73 6f 75  ws come from sou
1cdcf 72 63 65 20 69 6e 20 47 52 4f 55 50 20 42 59 20  rce in GROUP BY 
1cdd0 6f 72 64 65 72 20 2a 2f 0a 0a 0a 20 20 20 20 2f  order */...    /
1cdd1 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
1cdd2 76 61 72 69 61 62 6c 65 73 20 68 6f 6c 64 20 61  variables hold a
1cdd3 64 64 72 65 73 73 65 73 20 6f 72 20 6c 61 62 65  ddresses or labe
1cdd4 6c 73 20 66 6f 72 20 70 61 72 74 73 20 6f 66 20  ls for parts of 
1cdd5 74 68 65 0a 20 20 20 20 2a 2a 20 76 69 72 74 75  the.    ** virtu
1cdd6 61 6c 20 6d 61 63 68 69 6e 65 20 70 72 6f 67 72  al machine progr
1cdd7 61 6d 20 77 65 20 61 72 65 20 70 75 74 74 69 6e  am we are puttin
1cdd8 67 20 74 6f 67 65 74 68 65 72 20 2a 2f 0a 20 20  g together */.  
1cdd9 20 20 69 6e 74 20 61 64 64 72 4f 75 74 70 75 74    int addrOutput
1cdda 52 6f 77 3b 20 20 20 20 20 20 2f 2a 20 53 74 61  Row;      /* Sta
1cddb 72 74 20 6f 66 20 73 75 62 72 6f 75 74 69 6e 65  rt of subroutine
1cddc 20 74 68 61 74 20 6f 75 74 70 75 74 73 20 61 20   that outputs a 
1cddd 72 65 73 75 6c 74 20 72 6f 77 20 2a 2f 0a 20 20  result row */.  
1cdde 20 20 69 6e 74 20 61 64 64 72 53 65 74 41 62 6f    int addrSetAbo
1cddf 72 74 3b 20 20 20 20 20 20 20 2f 2a 20 53 65 74  rt;       /* Set
1cde0 20 74 68 65 20 61 62 6f 72 74 20 66 6c 61 67 20   the abort flag 
1cde1 61 6e 64 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20  and return */.  
1cde2 20 20 69 6e 74 20 61 64 64 72 49 6e 69 74 69 61    int addrInitia
1cde3 6c 69 7a 65 4c 6f 6f 70 3b 20 2f 2a 20 53 74 61  lizeLoop; /* Sta
1cde4 72 74 20 6f 66 20 63 6f 64 65 20 74 68 61 74 20  rt of code that 
1cde5 69 6e 69 74 69 61 6c 69 7a 65 73 20 74 68 65 20  initializes the 
1cde6 69 6e 70 75 74 20 6c 6f 6f 70 20 2a 2f 0a 20 20  input loop */.  
1cde7 20 20 69 6e 74 20 61 64 64 72 54 6f 70 4f 66 4c    int addrTopOfL
1cde8 6f 6f 70 3b 20 20 20 20 20 20 2f 2a 20 54 6f 70  oop;      /* Top
1cde9 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 6c 6f   of the input lo
1cdea 6f 70 20 2a 2f 0a 20 20 20 20 69 6e 74 20 61 64  op */.    int ad
1cdeb 64 72 47 72 6f 75 70 42 79 43 68 61 6e 67 65 3b  drGroupByChange;
1cdec 20 20 2f 2a 20 43 6f 64 65 20 74 68 61 74 20 72    /* Code that r
1cded 75 6e 73 20 77 68 65 6e 20 61 6e 79 20 47 52 4f  uns when any GRO
1cdee 55 50 20 42 59 20 74 65 72 6d 20 63 68 61 6e 67  UP BY term chang
1cdef 65 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20 61 64  es */.    int ad
1cdf0 64 72 50 72 6f 63 65 73 73 52 6f 77 3b 20 20 20  drProcessRow;   
1cdf1 20 20 2f 2a 20 43 6f 64 65 20 74 6f 20 70 72 6f    /* Code to pro
1cdf2 63 65 73 73 20 61 20 73 69 6e 67 6c 65 20 69 6e  cess a single in
1cdf3 70 75 74 20 72 6f 77 20 2a 2f 0a 20 20 20 20 69  put row */.    i
1cdf4 6e 74 20 61 64 64 72 45 6e 64 3b 20 20 20 20 20  nt addrEnd;     
1cdf5 20 20 20 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66         /* End of
1cdf6 20 61 6c 6c 20 70 72 6f 63 65 73 73 69 6e 67 20   all processing 
1cdf7 2a 2f 0a 20 20 20 20 69 6e 74 20 61 64 64 72 53  */.    int addrS
1cdf8 6f 72 74 69 6e 67 49 64 78 3b 20 20 20 20 20 2f  ortingIdx;     /
1cdf9 2a 20 54 68 65 20 4f 50 5f 4f 70 65 6e 45 70 68  * The OP_OpenEph
1cdfa 65 6d 65 72 61 6c 20 66 6f 72 20 74 68 65 20 73  emeral for the s
1cdfb 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 2a 2f 0a  orting index */.
1cdfc 20 20 20 20 69 6e 74 20 61 64 64 72 52 65 73 65      int addrRese
1cdfd 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53  t;          /* S
1cdfe 75 62 72 6f 75 74 69 6e 65 20 66 6f 72 20 72 65  ubroutine for re
1cdff 73 65 74 74 69 6e 67 20 74 68 65 20 61 63 63 75  setting the accu
1ce00 6d 75 6c 61 74 6f 72 20 2a 2f 0a 0a 20 20 20 20  mulator */..    
1ce01 61 64 64 72 45 6e 64 20 3d 20 73 71 6c 69 74 65  addrEnd = sqlite
1ce02 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76  3VdbeMakeLabel(v
1ce03 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f 6e 76 65  );..    /* Conve
1ce04 72 74 20 54 4b 5f 43 4f 4c 55 4d 4e 20 6e 6f 64  rt TK_COLUMN nod
1ce05 65 73 20 69 6e 74 6f 20 54 4b 5f 41 47 47 5f 43  es into TK_AGG_C
1ce06 4f 4c 55 4d 4e 20 61 6e 64 20 6d 61 6b 65 20 65  OLUMN and make e
1ce07 6e 74 72 69 65 73 20 69 6e 0a 20 20 20 20 2a 2a  ntries in.    **
1ce08 20 73 41 67 67 49 6e 66 6f 20 66 6f 72 20 61 6c   sAggInfo for al
1ce09 6c 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f  l TK_AGG_FUNCTIO
1ce0a 4e 20 6e 6f 64 65 73 20 69 6e 20 65 78 70 72 65  N nodes in expre
1ce0b 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 0a 20 20  ssions of the.  
1ce0c 20 20 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74    ** SELECT stat
1ce0d 65 6d 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20 20  ement..    */.  
1ce0e 20 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30    memset(&sNC, 0
1ce0f 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a  , sizeof(sNC));.
1ce10 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d      sNC.pParse =
1ce11 20 70 50 61 72 73 65 3b 0a 20 20 20 20 73 4e 43   pParse;.    sNC
1ce12 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 54 61 62  .pSrcList = pTab
1ce13 4c 69 73 74 3b 0a 20 20 20 20 73 4e 43 2e 70 41  List;.    sNC.pA
1ce14 67 67 49 6e 66 6f 20 3d 20 26 73 41 67 67 49 6e  ggInfo = &sAggIn
1ce15 66 6f 3b 0a 20 20 20 20 73 41 67 67 49 6e 66 6f  fo;.    sAggInfo
1ce16 2e 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 20  .nSortingColumn 
1ce17 3d 20 70 47 72 6f 75 70 42 79 20 3f 20 70 47 72  = pGroupBy ? pGr
1ce18 6f 75 70 42 79 2d 3e 6e 45 78 70 72 2b 31 20 3a  oupBy->nExpr+1 :
1ce19 20 30 3b 0a 20 20 20 20 73 41 67 67 49 6e 66 6f   0;.    sAggInfo
1ce1a 2e 70 47 72 6f 75 70 42 79 20 3d 20 70 47 72 6f  .pGroupBy = pGro
1ce1b 75 70 42 79 3b 0a 20 20 20 20 69 66 28 20 73 71  upBy;.    if( sq
1ce1c 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65  lite3ExprAnalyze
1ce1d 41 67 67 4c 69 73 74 28 26 73 4e 43 2c 20 70 45  AggList(&sNC, pE
1ce1e 4c 69 73 74 29 20 29 7b 0a 20 20 20 20 20 20 67  List) ){.      g
1ce1f 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a  oto select_end;.
1ce20 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71      }.    if( sq
1ce21 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65  lite3ExprAnalyze
1ce22 41 67 67 4c 69 73 74 28 26 73 4e 43 2c 20 70 4f  AggList(&sNC, pO
1ce23 72 64 65 72 42 79 29 20 29 7b 0a 20 20 20 20 20  rderBy) ){.     
1ce24 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64   goto select_end
1ce25 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1ce26 70 48 61 76 69 6e 67 20 26 26 20 73 71 6c 69 74  pHaving && sqlit
1ce27 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67  e3ExprAnalyzeAgg
1ce28 72 65 67 61 74 65 73 28 26 73 4e 43 2c 20 70 48  regates(&sNC, pH
1ce29 61 76 69 6e 67 29 20 29 7b 0a 20 20 20 20 20 20  aving) ){.      
1ce2a 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b  goto select_end;
1ce2b 0a 20 20 20 20 7d 0a 20 20 20 20 73 41 67 67 49  .    }.    sAggI
1ce2c 6e 66 6f 2e 6e 41 63 63 75 6d 75 6c 61 74 6f 72  nfo.nAccumulator
1ce2d 20 3d 20 73 41 67 67 49 6e 66 6f 2e 6e 43 6f 6c   = sAggInfo.nCol
1ce2e 75 6d 6e 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  umn;.    for(i=0
1ce2f 3b 20 69 3c 73 41 67 67 49 6e 66 6f 2e 6e 46 75  ; i<sAggInfo.nFu
1ce30 6e 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  nc; i++){.      
1ce31 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 41  if( sqlite3ExprA
1ce32 6e 61 6c 79 7a 65 41 67 67 4c 69 73 74 28 26 73  nalyzeAggList(&s
1ce33 4e 43 2c 20 73 41 67 67 49 6e 66 6f 2e 61 46 75  NC, sAggInfo.aFu
1ce34 6e 63 5b 69 5d 2e 70 45 78 70 72 2d 3e 70 4c 69  nc[i].pExpr->pLi
1ce35 73 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 67  st) ){.        g
1ce36 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a  oto select_end;.
1ce37 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1ce38 20 20 69 66 28 20 73 71 6c 69 74 65 33 4d 61 6c    if( sqlite3Mal
1ce39 6c 6f 63 46 61 69 6c 65 64 28 29 20 29 20 67 6f  locFailed() ) go
1ce3a 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 0a  to select_end;..
1ce3b 20 20 20 20 2f 2a 20 50 72 6f 63 65 73 73 69 6e      /* Processin
1ce3c 67 20 66 6f 72 20 61 67 67 72 65 67 61 74 65 73  g for aggregates
1ce3d 20 77 69 74 68 20 47 52 4f 55 50 20 42 59 20 69   with GROUP BY i
1ce3e 73 20 76 65 72 79 20 64 69 66 66 65 72 65 6e 74  s very different
1ce3f 20 61 6e 64 0a 20 20 20 20 2a 2a 20 6d 75 63 68   and.    ** much
1ce40 20 6d 6f 72 65 20 63 6f 6d 70 6c 65 78 20 74 68   more complex th
1ce41 61 20 61 67 67 72 65 67 61 74 65 73 20 77 69 74  a aggregates wit
1ce42 68 6f 75 74 20 61 20 47 52 4f 55 50 20 42 59 2e  hout a GROUP BY.
1ce43 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
1ce44 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 20  pGroupBy ){.    
1ce45 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49    KeyInfo *pKeyI
1ce46 6e 66 6f 3b 20 20 2f 2a 20 4b 65 79 69 6e 67 20  nfo;  /* Keying 
1ce47 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20  information for 
1ce48 74 68 65 20 67 72 6f 75 70 20 62 79 20 63 6c 61  the group by cla
1ce49 75 73 65 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a  use */..      /*
1ce4a 20 43 72 65 61 74 65 20 6c 61 62 65 6c 73 20 74   Create labels t
1ce4b 68 61 74 20 77 65 20 77 69 6c 6c 20 62 65 20 6e  hat we will be n
1ce4c 65 65 64 69 6e 67 0a 20 20 20 20 20 20 2a 2f 0a  eeding.      */.
1ce4d 20 20 20 20 20 0a 20 20 20 20 20 20 61 64 64 72       .      addr
1ce4e 49 6e 69 74 69 61 6c 69 7a 65 4c 6f 6f 70 20 3d  InitializeLoop =
1ce4f 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
1ce50 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20  Label(v);.      
1ce51 61 64 64 72 47 72 6f 75 70 42 79 43 68 61 6e 67  addrGroupByChang
1ce52 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  e = sqlite3VdbeM
1ce53 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20  akeLabel(v);.   
1ce54 20 20 20 61 64 64 72 50 72 6f 63 65 73 73 52 6f     addrProcessRo
1ce55 77 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  w = sqlite3VdbeM
1ce56 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 0a 20 20  akeLabel(v);..  
1ce57 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20      /* If there 
1ce58 69 73 20 61 20 47 52 4f 55 50 20 42 59 20 63 6c  is a GROUP BY cl
1ce59 61 75 73 65 20 77 65 20 6d 69 67 68 74 20 6e 65  ause we might ne
1ce5a 65 64 20 61 20 73 6f 72 74 69 6e 67 20 69 6e 64  ed a sorting ind
1ce5b 65 78 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 69  ex to.      ** i
1ce5c 6d 70 6c 65 6d 65 6e 74 20 69 74 2e 20 20 41 6c  mplement it.  Al
1ce5d 6c 6f 63 61 74 65 20 74 68 61 74 20 73 6f 72 74  locate that sort
1ce5e 69 6e 67 20 69 6e 64 65 78 20 6e 6f 77 2e 20 20  ing index now.  
1ce5f 49 66 20 69 74 20 74 75 72 6e 73 20 6f 75 74 0a  If it turns out.
1ce60 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 77 65        ** that we
1ce61 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 69 74 20   do not need it 
1ce62 61 66 74 65 72 20 61 6c 6c 2c 20 74 68 65 20 4f  after all, the O
1ce63 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 69 6e 73  penEphemeral ins
1ce64 74 72 75 63 74 69 6f 6e 0a 20 20 20 20 20 20 2a  truction.      *
1ce65 2a 20 77 69 6c 6c 20 62 65 20 63 6f 6e 76 65 72  * will be conver
1ce66 74 65 64 20 69 6e 74 6f 20 61 20 4e 6f 6f 70 2e  ted into a Noop.
1ce67 20 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20    .      */.    
1ce68 20 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69    sAggInfo.sorti
1ce69 6e 67 49 64 78 20 3d 20 70 50 61 72 73 65 2d 3e  ngIdx = pParse->
1ce6a 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 70 4b  nTab++;.      pK
1ce6b 65 79 49 6e 66 6f 20 3d 20 6b 65 79 49 6e 66 6f  eyInfo = keyInfo
1ce6c 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 70 50 61  FromExprList(pPa
1ce6d 72 73 65 2c 20 70 47 72 6f 75 70 42 79 29 3b 0a  rse, pGroupBy);.
1ce6e 20 20 20 20 20 20 61 64 64 72 53 6f 72 74 69 6e        addrSortin
1ce6f 67 49 64 78 20 3d 0a 20 20 20 20 20 20 20 20 20  gIdx =.         
1ce70 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28   sqlite3VdbeOp3(
1ce71 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  v, OP_OpenEpheme
1ce72 72 61 6c 2c 20 73 41 67 67 49 6e 66 6f 2e 73 6f  ral, sAggInfo.so
1ce73 72 74 69 6e 67 49 64 78 2c 0a 20 20 20 20 20 20  rtingIdx,.      
1ce74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ce75 20 20 20 73 41 67 67 49 6e 66 6f 2e 6e 53 6f 72     sAggInfo.nSor
1ce76 74 69 6e 67 43 6f 6c 75 6d 6e 2c 0a 20 20 20 20  tingColumn,.    
1ce77 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ce78 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79       (char*)pKey
1ce79 49 6e 66 6f 2c 20 50 33 5f 4b 45 59 49 4e 46 4f  Info, P3_KEYINFO
1ce7a 5f 48 41 4e 44 4f 46 46 29 3b 0a 0a 20 20 20 20  _HANDOFF);..    
1ce7b 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20    /* Initialize 
1ce7c 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73  memory locations
1ce7d 20 75 73 65 64 20 62 79 20 47 52 4f 55 50 20 42   used by GROUP B
1ce7e 59 20 61 67 67 72 65 67 61 74 65 20 70 72 6f 63  Y aggregate proc
1ce7f 65 73 73 69 6e 67 0a 20 20 20 20 20 20 2a 2f 0a  essing.      */.
1ce80 20 20 20 20 20 20 69 55 73 65 46 6c 61 67 20 3d        iUseFlag =
1ce81 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 2b 3b   pParse->nMem++;
1ce82 0a 20 20 20 20 20 20 69 41 62 6f 72 74 46 6c 61  .      iAbortFla
1ce83 67 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  g = pParse->nMem
1ce84 2b 2b 3b 0a 20 20 20 20 20 20 69 41 4d 65 6d 20  ++;.      iAMem 
1ce85 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  = pParse->nMem;.
1ce86 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d        pParse->nM
1ce87 65 6d 20 2b 3d 20 70 47 72 6f 75 70 42 79 2d 3e  em += pGroupBy->
1ce88 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 69 42 4d  nExpr;.      iBM
1ce89 65 6d 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65  em = pParse->nMe
1ce8a 6d 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  m;.      pParse-
1ce8b 3e 6e 4d 65 6d 20 2b 3d 20 70 47 72 6f 75 70 42  >nMem += pGroupB
1ce8c 79 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20  y->nExpr;.      
1ce8d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1ce8e 28 76 2c 20 4f 50 5f 4d 65 6d 49 6e 74 2c 20 30  (v, OP_MemInt, 0
1ce8f 2c 20 69 41 62 6f 72 74 46 6c 61 67 29 3b 0a 20  , iAbortFlag);. 
1ce90 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74       VdbeComment
1ce91 28 28 76 2c 20 22 23 20 63 6c 65 61 72 20 61 62  ((v, "# clear ab
1ce92 6f 72 74 20 66 6c 61 67 22 29 29 3b 0a 20 20 20  ort flag"));.   
1ce93 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1ce94 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 49 6e 74  dOp(v, OP_MemInt
1ce95 2c 20 30 2c 20 69 55 73 65 46 6c 61 67 29 3b 0a  , 0, iUseFlag);.
1ce96 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e        VdbeCommen
1ce97 74 28 28 76 2c 20 22 23 20 69 6e 64 69 63 61 74  t((v, "# indicat
1ce98 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 65 6d  e accumulator em
1ce99 70 74 79 22 29 29 3b 0a 20 20 20 20 20 20 73 71  pty"));.      sq
1ce9a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1ce9b 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61 64  , OP_Goto, 0, ad
1ce9c 64 72 49 6e 69 74 69 61 6c 69 7a 65 4c 6f 6f 70  drInitializeLoop
1ce9d 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 47 65 6e  );..      /* Gen
1ce9e 65 72 61 74 65 20 61 20 73 75 62 72 6f 75 74 69  erate a subrouti
1ce9f 6e 65 20 74 68 61 74 20 6f 75 74 70 75 74 73 20  ne that outputs 
1cea0 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20  a single row of 
1cea1 74 68 65 20 72 65 73 75 6c 74 0a 20 20 20 20 20  the result.     
1cea2 20 2a 2a 20 73 65 74 2e 20 20 54 68 69 73 20 73   ** set.  This s
1cea3 75 62 72 6f 75 74 69 6e 65 20 66 69 72 73 74 20  ubroutine first 
1cea4 6c 6f 6f 6b 73 20 61 74 20 74 68 65 20 69 55 73  looks at the iUs
1cea5 65 46 6c 61 67 2e 20 20 49 66 20 69 55 73 65 46  eFlag.  If iUseF
1cea6 6c 61 67 0a 20 20 20 20 20 20 2a 2a 20 69 73 20  lag.      ** is 
1cea7 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75  less than or equ
1cea8 61 6c 20 74 6f 20 7a 65 72 6f 2c 20 74 68 65 20  al to zero, the 
1cea9 73 75 62 72 6f 75 74 69 6e 65 20 69 73 20 61 20  subroutine is a 
1ceaa 6e 6f 2d 6f 70 2e 20 20 49 66 0a 20 20 20 20 20  no-op.  If.     
1ceab 20 2a 2a 20 74 68 65 20 70 72 6f 63 65 73 73 69   ** the processi
1ceac 6e 67 20 63 61 6c 6c 73 20 66 6f 72 20 74 68 65  ng calls for the
1cead 20 71 75 65 72 79 20 74 6f 20 61 62 6f 72 74 2c   query to abort,
1ceae 20 74 68 69 73 20 73 75 62 72 6f 75 74 69 6e 65   this subroutine
1ceaf 0a 20 20 20 20 20 20 2a 2a 20 69 6e 63 72 65 6d  .      ** increm
1ceb0 65 6e 74 73 20 74 68 65 20 69 41 62 6f 72 74 46  ents the iAbortF
1ceb1 6c 61 67 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74  lag memory locat
1ceb2 69 6f 6e 20 62 65 66 6f 72 65 20 72 65 74 75 72  ion before retur
1ceb3 6e 69 6e 67 20 69 6e 0a 20 20 20 20 20 20 2a 2a  ning in.      **
1ceb4 20 6f 72 64 65 72 20 74 6f 20 73 69 67 6e 61 6c   order to signal
1ceb5 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 61   the caller to a
1ceb6 62 6f 72 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  bort..      */. 
1ceb7 20 20 20 20 20 61 64 64 72 53 65 74 41 62 6f 72       addrSetAbor
1ceb8 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  t = sqlite3VdbeC
1ceb9 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20  urrentAddr(v);. 
1ceba 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1cebb 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 49  AddOp(v, OP_MemI
1cebc 6e 74 2c 20 31 2c 20 69 41 62 6f 72 74 46 6c 61  nt, 1, iAbortFla
1cebd 67 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f  g);.      VdbeCo
1cebe 6d 6d 65 6e 74 28 28 76 2c 20 22 23 20 73 65 74  mment((v, "# set
1cebf 20 61 62 6f 72 74 20 66 6c 61 67 22 29 29 3b 0a   abort flag"));.
1cec0 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1cec1 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 52 65 74  eAddOp(v, OP_Ret
1cec2 75 72 6e 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  urn, 0, 0);.    
1cec3 20 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77 20    addrOutputRow 
1cec4 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
1cec5 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20  rentAddr(v);.   
1cec6 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1cec7 64 4f 70 28 76 2c 20 4f 50 5f 49 66 4d 65 6d 50  dOp(v, OP_IfMemP
1cec8 6f 73 2c 20 69 55 73 65 46 6c 61 67 2c 20 61 64  os, iUseFlag, ad
1cec9 64 72 4f 75 74 70 75 74 52 6f 77 2b 32 29 3b 0a  drOutputRow+2);.
1ceca 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e        VdbeCommen
1cecb 74 28 28 76 2c 20 22 23 20 47 72 6f 75 70 62 79  t((v, "# Groupby
1cecc 20 72 65 73 75 6c 74 20 67 65 6e 65 72 61 74 6f   result generato
1cecd 72 20 65 6e 74 72 79 20 70 6f 69 6e 74 22 29 29  r entry point"))
1cece 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1cecf 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 52  dbeAddOp(v, OP_R
1ced0 65 74 75 72 6e 2c 20 30 2c 20 30 29 3b 0a 20 20  eturn, 0, 0);.  
1ced1 20 20 20 20 66 69 6e 61 6c 69 7a 65 41 67 67 46      finalizeAggF
1ced2 75 6e 63 74 69 6f 6e 73 28 70 50 61 72 73 65 2c  unctions(pParse,
1ced3 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a 20 20 20   &sAggInfo);.   
1ced4 20 20 20 69 66 28 20 70 48 61 76 69 6e 67 20 29     if( pHaving )
1ced5 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1ced6 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50 61  3ExprIfFalse(pPa
1ced7 72 73 65 2c 20 70 48 61 76 69 6e 67 2c 20 61 64  rse, pHaving, ad
1ced8 64 72 4f 75 74 70 75 74 52 6f 77 2b 31 2c 20 31  drOutputRow+1, 1
1ced9 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1ceda 20 72 63 20 3d 20 73 65 6c 65 63 74 49 6e 6e 65   rc = selectInne
1cedb 72 4c 6f 6f 70 28 70 50 61 72 73 65 2c 20 70 2c  rLoop(pParse, p,
1cedc 20 70 2d 3e 70 45 4c 69 73 74 2c 20 30 2c 20 30   p->pEList, 0, 0
1cedd 2c 20 70 4f 72 64 65 72 42 79 2c 0a 20 20 20 20  , pOrderBy,.    
1cede 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cedf 20 20 20 20 20 20 20 64 69 73 74 69 6e 63 74 2c         distinct,
1cee0 20 65 44 65 73 74 2c 20 69 50 61 72 6d 2c 20 0a   eDest, iParm, .
1cee1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cee2 20 20 20 20 20 20 20 20 20 20 20 61 64 64 72 4f             addrO
1cee3 75 74 70 75 74 52 6f 77 2b 31 2c 20 61 64 64 72  utputRow+1, addr
1cee4 53 65 74 41 62 6f 72 74 2c 20 61 66 66 29 3b 0a  SetAbort, aff);.
1cee5 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a        if( rc ){.
1cee6 20 20 20 20 20 20 20 20 67 6f 74 6f 20 73 65 6c          goto sel
1cee7 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20 20 20 7d  ect_end;.      }
1cee8 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1cee9 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 52 65  beAddOp(v, OP_Re
1ceea 74 75 72 6e 2c 20 30 2c 20 30 29 3b 0a 20 20 20  turn, 0, 0);.   
1ceeb 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
1ceec 76 2c 20 22 23 20 65 6e 64 20 67 72 6f 75 70 62  v, "# end groupb
1ceed 79 20 72 65 73 75 6c 74 20 67 65 6e 65 72 61 74  y result generat
1ceee 6f 72 22 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  or"));..      /*
1ceef 20 47 65 6e 65 72 61 74 65 20 61 20 73 75 62 72   Generate a subr
1cef0 6f 75 74 69 6e 65 20 74 68 61 74 20 77 69 6c 6c  outine that will
1cef1 20 72 65 73 65 74 20 74 68 65 20 67 72 6f 75 70   reset the group
1cef2 2d 62 79 20 61 63 63 75 6d 75 6c 61 74 6f 72 0a  -by accumulator.
1cef3 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61        */.      a
1cef4 64 64 72 52 65 73 65 74 20 3d 20 73 71 6c 69 74  ddrReset = sqlit
1cef5 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
1cef6 72 28 76 29 3b 0a 20 20 20 20 20 20 72 65 73 65  r(v);.      rese
1cef7 74 41 63 63 75 6d 75 6c 61 74 6f 72 28 70 50 61  tAccumulator(pPa
1cef8 72 73 65 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b  rse, &sAggInfo);
1cef9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1cefa 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 52 65  beAddOp(v, OP_Re
1cefb 74 75 72 6e 2c 20 30 2c 20 30 29 3b 0a 0a 20 20  turn, 0, 0);..  
1cefc 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 61 20 6c      /* Begin a l
1cefd 6f 6f 70 20 74 68 61 74 20 77 69 6c 6c 20 65 78  oop that will ex
1cefe 74 72 61 63 74 20 61 6c 6c 20 73 6f 75 72 63 65  tract all source
1ceff 20 72 6f 77 73 20 69 6e 20 47 52 4f 55 50 20 42   rows in GROUP B
1cf00 59 20 6f 72 64 65 72 2e 0a 20 20 20 20 20 20 2a  Y order..      *
1cf01 2a 20 54 68 69 73 20 6d 69 67 68 74 20 69 6e 76  * This might inv
1cf02 6f 6c 76 65 20 74 77 6f 20 73 65 70 61 72 61 74  olve two separat
1cf03 65 20 6c 6f 6f 70 73 20 77 69 74 68 20 61 6e 20  e loops with an 
1cf04 4f 50 5f 53 6f 72 74 20 69 6e 20 62 65 74 77 65  OP_Sort in betwe
1cf05 65 6e 2c 20 6f 72 0a 20 20 20 20 20 20 2a 2a 20  en, or.      ** 
1cf06 69 74 20 6d 69 67 68 74 20 62 65 20 61 20 73 69  it might be a si
1cf07 6e 67 6c 65 20 6c 6f 6f 70 20 74 68 61 74 20 75  ngle loop that u
1cf08 73 65 73 20 61 6e 20 69 6e 64 65 78 20 74 6f 20  ses an index to 
1cf09 65 78 74 72 61 63 74 20 69 6e 66 6f 72 6d 61 74  extract informat
1cf0a 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 69 6e 20  ion.      ** in 
1cf0b 74 68 65 20 72 69 67 68 74 20 6f 72 64 65 72 20  the right order 
1cf0c 74 6f 20 62 65 67 69 6e 20 77 69 74 68 2e 0a 20  to begin with.. 
1cf0d 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71       */.      sq
1cf0e 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
1cf0f 4c 61 62 65 6c 28 76 2c 20 61 64 64 72 49 6e 69  Label(v, addrIni
1cf10 74 69 61 6c 69 7a 65 4c 6f 6f 70 29 3b 0a 20 20  tializeLoop);.  
1cf11 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1cf12 64 64 4f 70 28 76 2c 20 4f 50 5f 47 6f 73 75 62  ddOp(v, OP_Gosub
1cf13 2c 20 30 2c 20 61 64 64 72 52 65 73 65 74 29 3b  , 0, addrReset);
1cf14 0a 20 20 20 20 20 20 70 57 49 6e 66 6f 20 3d 20  .      pWInfo = 
1cf15 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69  sqlite3WhereBegi
1cf16 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69  n(pParse, pTabLi
1cf17 73 74 2c 20 70 57 68 65 72 65 2c 20 26 70 47 72  st, pWhere, &pGr
1cf18 6f 75 70 42 79 29 3b 0a 20 20 20 20 20 20 69 66  oupBy);.      if
1cf19 28 20 70 57 49 6e 66 6f 3d 3d 30 20 29 20 67 6f  ( pWInfo==0 ) go
1cf1a 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20  to select_end;. 
1cf1b 20 20 20 20 20 69 66 28 20 70 47 72 6f 75 70 42       if( pGroupB
1cf1c 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  y==0 ){.        
1cf1d 2f 2a 20 54 68 65 20 6f 70 74 69 6d 69 7a 65 72  /* The optimizer
1cf1e 20 69 73 20 61 62 6c 65 20 74 6f 20 64 65 6c 69   is able to deli
1cf1f 76 65 72 20 72 6f 77 73 20 69 6e 20 67 72 6f 75  ver rows in grou
1cf20 70 20 62 79 20 6f 72 64 65 72 20 73 6f 0a 20 20  p by order so.  
1cf21 20 20 20 20 20 20 2a 2a 20 77 65 20 64 6f 20 6e        ** we do n
1cf22 6f 74 20 68 61 76 65 20 74 6f 20 73 6f 72 74 2e  ot have to sort.
1cf23 20 20 54 68 65 20 4f 50 5f 4f 70 65 6e 45 70 68    The OP_OpenEph
1cf24 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 77 69 6c  emeral table wil
1cf25 6c 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  l be.        ** 
1cf26 63 61 6e 63 65 6c 6c 65 64 20 6c 61 74 65 72 20  cancelled later 
1cf27 62 65 63 61 75 73 65 20 77 65 20 73 74 69 6c 6c  because we still
1cf28 20 6e 65 65 64 20 74 6f 20 75 73 65 20 74 68 65   need to use the
1cf29 20 70 4b 65 79 49 6e 66 6f 0a 20 20 20 20 20 20   pKeyInfo.      
1cf2a 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 47 72    */.        pGr
1cf2b 6f 75 70 42 79 20 3d 20 70 2d 3e 70 47 72 6f 75  oupBy = p->pGrou
1cf2c 70 42 79 3b 0a 20 20 20 20 20 20 20 20 67 72 6f  pBy;.        gro
1cf2d 75 70 42 79 53 6f 72 74 20 3d 20 30 3b 0a 20 20  upBySort = 0;.  
1cf2e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1cf2f 20 20 20 2f 2a 20 52 6f 77 73 20 61 72 65 20 63     /* Rows are c
1cf30 6f 6d 69 6e 67 20 6f 75 74 20 69 6e 20 75 6e 64  oming out in und
1cf31 65 74 65 72 6d 69 6e 65 64 20 6f 72 64 65 72 2e  etermined order.
1cf32 20 20 57 65 20 68 61 76 65 20 74 6f 20 70 75 73    We have to pus
1cf33 68 0a 20 20 20 20 20 20 20 20 2a 2a 20 65 61 63  h.        ** eac
1cf34 68 20 72 6f 77 20 69 6e 74 6f 20 61 20 73 6f 72  h row into a sor
1cf35 74 69 6e 67 20 69 6e 64 65 78 2c 20 74 65 72 6d  ting index, term
1cf36 69 6e 61 74 65 20 74 68 65 20 66 69 72 73 74 20  inate the first 
1cf37 6c 6f 6f 70 2c 0a 20 20 20 20 20 20 20 20 2a 2a  loop,.        **
1cf38 20 74 68 65 6e 20 6c 6f 6f 70 20 6f 76 65 72 20   then loop over 
1cf39 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65  the sorting inde
1cf3a 78 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 67 65  x in order to ge
1cf3b 74 20 74 68 65 20 6f 75 74 70 75 74 0a 20 20 20  t the output.   
1cf3c 20 20 20 20 20 2a 2a 20 69 6e 20 73 6f 72 74 65       ** in sorte
1cf3d 64 20 6f 72 64 65 72 0a 20 20 20 20 20 20 20 20  d order.        
1cf3e 2a 2f 0a 20 20 20 20 20 20 20 20 67 72 6f 75 70  */.        group
1cf3f 42 79 53 6f 72 74 20 3d 20 31 3b 0a 20 20 20 20  BySort = 1;.    
1cf40 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
1cf41 6f 64 65 45 78 70 72 4c 69 73 74 28 70 50 61 72  odeExprList(pPar
1cf42 73 65 2c 20 70 47 72 6f 75 70 42 79 29 3b 0a 20  se, pGroupBy);. 
1cf43 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1cf44 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 53 65  beAddOp(v, OP_Se
1cf45 71 75 65 6e 63 65 2c 20 73 41 67 67 49 6e 66 6f  quence, sAggInfo
1cf46 2e 73 6f 72 74 69 6e 67 49 64 78 2c 20 30 29 3b  .sortingIdx, 0);
1cf47 0a 20 20 20 20 20 20 20 20 6a 20 3d 20 70 47 72  .        j = pGr
1cf48 6f 75 70 42 79 2d 3e 6e 45 78 70 72 2b 31 3b 0a  oupBy->nExpr+1;.
1cf49 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b          for(i=0;
1cf4a 20 69 3c 73 41 67 67 49 6e 66 6f 2e 6e 43 6f 6c   i<sAggInfo.nCol
1cf4b 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  umn; i++){.     
1cf4c 20 20 20 20 20 73 74 72 75 63 74 20 41 67 67 49       struct AggI
1cf4d 6e 66 6f 5f 63 6f 6c 20 2a 70 43 6f 6c 20 3d 20  nfo_col *pCol = 
1cf4e 26 73 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 5b 69  &sAggInfo.aCol[i
1cf4f 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ];.          if(
1cf50 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f   pCol->iSorterCo
1cf51 6c 75 6d 6e 3c 6a 20 29 20 63 6f 6e 74 69 6e 75  lumn<j ) continu
1cf52 65 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  e;.          sql
1cf53 69 74 65 33 45 78 70 72 43 6f 64 65 47 65 74 43  ite3ExprCodeGetC
1cf54 6f 6c 75 6d 6e 28 76 2c 20 70 43 6f 6c 2d 3e 70  olumn(v, pCol->p
1cf55 54 61 62 2c 20 70 43 6f 6c 2d 3e 69 43 6f 6c 75  Tab, pCol->iColu
1cf56 6d 6e 2c 20 70 43 6f 6c 2d 3e 69 54 61 62 6c 65  mn, pCol->iTable
1cf57 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 2b 2b  );.          j++
1cf58 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1cf59 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1cf5a 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 61 6b 65 52  ddOp(v, OP_MakeR
1cf5b 65 63 6f 72 64 2c 20 6a 2c 20 30 29 3b 0a 20 20  ecord, j, 0);.  
1cf5c 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1cf5d 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 64 78  eAddOp(v, OP_Idx
1cf5e 49 6e 73 65 72 74 2c 20 73 41 67 67 49 6e 66 6f  Insert, sAggInfo
1cf5f 2e 73 6f 72 74 69 6e 67 49 64 78 2c 20 30 29 3b  .sortingIdx, 0);
1cf60 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1cf61 57 68 65 72 65 45 6e 64 28 70 57 49 6e 66 6f 29  WhereEnd(pWInfo)
1cf62 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
1cf63 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1cf64 5f 53 6f 72 74 2c 20 73 41 67 67 49 6e 66 6f 2e  _Sort, sAggInfo.
1cf65 73 6f 72 74 69 6e 67 49 64 78 2c 20 61 64 64 72  sortingIdx, addr
1cf66 45 6e 64 29 3b 0a 20 20 20 20 20 20 20 20 56 64  End);.        Vd
1cf67 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 23  beComment((v, "#
1cf68 20 47 52 4f 55 50 20 42 59 20 73 6f 72 74 22 29   GROUP BY sort")
1cf69 29 3b 0a 20 20 20 20 20 20 20 20 73 41 67 67 49  );.        sAggI
1cf6a 6e 66 6f 2e 75 73 65 53 6f 72 74 69 6e 67 49 64  nfo.useSortingId
1cf6b 78 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 0a  x = 1;.      }..
1cf6c 20 20 20 20 20 20 2f 2a 20 45 76 61 6c 75 61 74        /* Evaluat
1cf6d 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 47 52  e the current GR
1cf6e 4f 55 50 20 42 59 20 74 65 72 6d 73 20 61 6e 64  OUP BY terms and
1cf6f 20 73 74 6f 72 65 20 69 6e 20 62 30 2c 20 62 31   store in b0, b1
1cf70 2c 20 62 32 2e 2e 2e 0a 20 20 20 20 20 20 2a 2a  , b2....      **
1cf71 20 28 62 30 20 69 73 20 6d 65 6d 6f 72 79 20 6c   (b0 is memory l
1cf72 6f 63 61 74 69 6f 6e 20 69 42 4d 65 6d 2b 30 2c  ocation iBMem+0,
1cf73 20 62 31 20 69 73 20 69 42 4d 65 6d 2b 31 2c 20   b1 is iBMem+1, 
1cf74 61 6e 64 20 73 6f 20 66 6f 72 74 68 29 0a 20 20  and so forth).  
1cf75 20 20 20 20 2a 2a 20 54 68 65 6e 20 63 6f 6d 70      ** Then comp
1cf76 61 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  are the current 
1cf77 47 52 4f 55 50 20 42 59 20 74 65 72 6d 73 20 61  GROUP BY terms a
1cf78 67 61 69 6e 73 74 20 74 68 65 20 47 52 4f 55 50  gainst the GROUP
1cf79 20 42 59 20 74 65 72 6d 73 0a 20 20 20 20 20 20   BY terms.      
1cf7a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 70 72 65 76  ** from the prev
1cf7b 69 6f 75 73 20 72 6f 77 20 63 75 72 72 65 6e 74  ious row current
1cf7c 6c 79 20 73 74 6f 72 65 64 20 69 6e 20 61 30 2c  ly stored in a0,
1cf7d 20 61 31 2c 20 61 32 2e 2e 2e 0a 20 20 20 20 20   a1, a2....     
1cf7e 20 2a 2f 0a 20 20 20 20 20 20 61 64 64 72 54 6f   */.      addrTo
1cf7f 70 4f 66 4c 6f 6f 70 20 3d 20 73 71 6c 69 74 65  pOfLoop = sqlite
1cf80 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
1cf81 28 76 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a  (v);.      for(j
1cf82 3d 30 3b 20 6a 3c 70 47 72 6f 75 70 42 79 2d 3e  =0; j<pGroupBy->
1cf83 6e 45 78 70 72 3b 20 6a 2b 2b 29 7b 0a 20 20 20  nExpr; j++){.   
1cf84 20 20 20 20 20 69 66 28 20 67 72 6f 75 70 42 79       if( groupBy
1cf85 53 6f 72 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Sort ){.        
1cf86 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1cf87 4f 70 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c  Op(v, OP_Column,
1cf88 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e   sAggInfo.sortin
1cf89 67 49 64 78 2c 20 6a 29 3b 0a 20 20 20 20 20 20  gIdx, j);.      
1cf8a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1cf8b 20 20 20 73 41 67 67 49 6e 66 6f 2e 64 69 72 65     sAggInfo.dire
1cf8c 63 74 4d 6f 64 65 20 3d 20 31 3b 0a 20 20 20 20  ctMode = 1;.    
1cf8d 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
1cf8e 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 47  rCode(pParse, pG
1cf8f 72 6f 75 70 42 79 2d 3e 61 5b 6a 5d 2e 70 45 78  roupBy->a[j].pEx
1cf90 70 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  pr);.        }. 
1cf91 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1cf92 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65  beAddOp(v, OP_Me
1cf93 6d 53 74 6f 72 65 2c 20 69 42 4d 65 6d 2b 6a 2c  mStore, iBMem+j,
1cf94 20 6a 3c 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78   j<pGroupBy->nEx
1cf95 70 72 2d 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20  pr-1);.      }. 
1cf96 20 20 20 20 20 66 6f 72 28 6a 3d 70 47 72 6f 75       for(j=pGrou
1cf97 70 42 79 2d 3e 6e 45 78 70 72 2d 31 3b 20 6a 3e  pBy->nExpr-1; j>
1cf98 3d 30 3b 20 6a 2d 2d 29 7b 0a 20 20 20 20 20 20  =0; j--){.      
1cf99 20 20 69 66 28 20 6a 3c 70 47 72 6f 75 70 42 79    if( j<pGroupBy
1cf9a 2d 3e 6e 45 78 70 72 2d 31 20 29 7b 0a 20 20 20  ->nExpr-1 ){.   
1cf9b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1cf9c 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65  beAddOp(v, OP_Me
1cf9d 6d 4c 6f 61 64 2c 20 69 42 4d 65 6d 2b 6a 2c 20  mLoad, iBMem+j, 
1cf9e 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  0);.        }.  
1cf9f 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1cfa0 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d  eAddOp(v, OP_Mem
1cfa1 4c 6f 61 64 2c 20 69 41 4d 65 6d 2b 6a 2c 20 30  Load, iAMem+j, 0
1cfa2 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6a  );.        if( j
1cfa3 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
1cfa4 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1cfa5 70 28 76 2c 20 4f 50 5f 45 71 2c 20 30 78 32 30  p(v, OP_Eq, 0x20
1cfa6 30 2c 20 61 64 64 72 50 72 6f 63 65 73 73 52 6f  0, addrProcessRo
1cfa7 77 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  w);.        }els
1cfa8 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  e{.          sql
1cfa9 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1cfaa 20 4f 50 5f 4e 65 2c 20 30 78 32 30 30 2c 20 61   OP_Ne, 0x200, a
1cfab 64 64 72 47 72 6f 75 70 42 79 43 68 61 6e 67 65  ddrGroupByChange
1cfac 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
1cfad 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1cfae 43 68 61 6e 67 65 50 33 28 76 2c 20 2d 31 2c 20  ChangeP3(v, -1, 
1cfaf 28 76 6f 69 64 2a 29 70 4b 65 79 49 6e 66 6f 2d  (void*)pKeyInfo-
1cfb0 3e 61 43 6f 6c 6c 5b 6a 5d 2c 20 50 33 5f 43 4f  >aColl[j], P3_CO
1cfb1 4c 4c 53 45 51 29 3b 0a 20 20 20 20 20 20 7d 0a  LLSEQ);.      }.
1cfb2 0a 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61  .      /* Genera
1cfb3 74 65 20 63 6f 64 65 20 74 68 61 74 20 72 75 6e  te code that run
1cfb4 73 20 77 68 65 6e 65 76 65 72 20 74 68 65 20 47  s whenever the G
1cfb5 52 4f 55 50 20 42 59 20 63 68 61 6e 67 65 73 2e  ROUP BY changes.
1cfb6 0a 20 20 20 20 20 20 2a 2a 20 43 68 61 6e 67 65  .      ** Change
1cfb7 20 69 6e 20 74 68 65 20 47 52 4f 55 50 20 42 59   in the GROUP BY
1cfb8 20 61 72 65 20 64 65 74 65 63 74 65 64 20 62 79   are detected by
1cfb9 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 6f   the previous co
1cfba 64 65 0a 20 20 20 20 20 20 2a 2a 20 62 6c 6f 63  de.      ** bloc
1cfbb 6b 2e 20 20 49 66 20 74 68 65 72 65 20 77 65 72  k.  If there wer
1cfbc 65 20 6e 6f 20 63 68 61 6e 67 65 73 2c 20 74 68  e no changes, th
1cfbd 69 73 20 62 6c 6f 63 6b 20 69 73 20 73 6b 69 70  is block is skip
1cfbe 70 65 64 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20  ped..      **.  
1cfbf 20 20 20 20 2a 2a 20 54 68 69 73 20 63 6f 64 65      ** This code
1cfc0 20 63 6f 70 69 65 73 20 63 75 72 72 65 6e 74 20   copies current 
1cfc1 67 72 6f 75 70 20 62 79 20 74 65 72 6d 73 20 69  group by terms i
1cfc2 6e 20 62 30 2c 62 31 2c 62 32 2c 2e 2e 2e 0a 20  n b0,b1,b2,.... 
1cfc3 20 20 20 20 20 2a 2a 20 6f 76 65 72 20 74 6f 20       ** over to 
1cfc4 61 30 2c 61 31 2c 61 32 2e 20 20 49 74 20 74 68  a0,a1,a2.  It th
1cfc5 65 6e 20 63 61 6c 6c 73 20 74 68 65 20 6f 75 74  en calls the out
1cfc6 70 75 74 20 73 75 62 72 6f 75 74 69 6e 65 0a 20  put subroutine. 
1cfc7 20 20 20 20 20 2a 2a 20 61 6e 64 20 72 65 73 65       ** and rese
1cfc8 74 73 20 74 68 65 20 61 67 67 72 65 67 61 74 65  ts the aggregate
1cfc9 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 72 65 67   accumulator reg
1cfca 69 73 74 65 72 73 20 69 6e 20 70 72 65 70 61 72  isters in prepar
1cfcb 61 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 66  ation.      ** f
1cfcc 6f 72 20 74 68 65 20 6e 65 78 74 20 47 52 4f 55  or the next GROU
1cfcd 50 20 42 59 20 62 61 74 63 68 2e 0a 20 20 20 20  P BY batch..    
1cfce 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74    */.      sqlit
1cfcf 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
1cfd0 65 6c 28 76 2c 20 61 64 64 72 47 72 6f 75 70 42  el(v, addrGroupB
1cfd1 79 43 68 61 6e 67 65 29 3b 0a 20 20 20 20 20 20  yChange);.      
1cfd2 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 47 72 6f 75  for(j=0; j<pGrou
1cfd3 70 42 79 2d 3e 6e 45 78 70 72 3b 20 6a 2b 2b 29  pBy->nExpr; j++)
1cfd4 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1cfd5 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1cfd6 5f 4d 65 6d 4d 6f 76 65 2c 20 69 41 4d 65 6d 2b  _MemMove, iAMem+
1cfd7 6a 2c 20 69 42 4d 65 6d 2b 6a 29 3b 0a 20 20 20  j, iBMem+j);.   
1cfd8 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
1cfd9 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1cfda 50 5f 47 6f 73 75 62 2c 20 30 2c 20 61 64 64 72  P_Gosub, 0, addr
1cfdb 4f 75 74 70 75 74 52 6f 77 29 3b 0a 20 20 20 20  OutputRow);.    
1cfdc 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
1cfdd 2c 20 22 23 20 6f 75 74 70 75 74 20 6f 6e 65 20  , "# output one 
1cfde 72 6f 77 22 29 29 3b 0a 20 20 20 20 20 20 73 71  row"));.      sq
1cfdf 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1cfe0 2c 20 4f 50 5f 49 66 4d 65 6d 50 6f 73 2c 20 69  , OP_IfMemPos, i
1cfe1 41 62 6f 72 74 46 6c 61 67 2c 20 61 64 64 72 45  AbortFlag, addrE
1cfe2 6e 64 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43  nd);.      VdbeC
1cfe3 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 23 20 63 68  omment((v, "# ch
1cfe4 65 63 6b 20 61 62 6f 72 74 20 66 6c 61 67 22 29  eck abort flag")
1cfe5 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1cfe6 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1cfe7 47 6f 73 75 62 2c 20 30 2c 20 61 64 64 72 52 65  Gosub, 0, addrRe
1cfe8 73 65 74 29 3b 0a 20 20 20 20 20 20 56 64 62 65  set);.      Vdbe
1cfe9 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 23 20 72  Comment((v, "# r
1cfea 65 73 65 74 20 61 63 63 75 6d 75 6c 61 74 6f 72  eset accumulator
1cfeb 22 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 55  "));..      /* U
1cfec 70 64 61 74 65 20 74 68 65 20 61 67 67 72 65 67  pdate the aggreg
1cfed 61 74 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 73  ate accumulators
1cfee 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 63 6f   based on the co
1cfef 6e 74 65 6e 74 20 6f 66 0a 20 20 20 20 20 20 2a  ntent of.      *
1cff0 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f  * the current ro
1cff1 77 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  w.      */.     
1cff2 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
1cff3 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61 64 64 72  lveLabel(v, addr
1cff4 50 72 6f 63 65 73 73 52 6f 77 29 3b 0a 20 20 20  ProcessRow);.   
1cff5 20 20 20 75 70 64 61 74 65 41 63 63 75 6d 75 6c     updateAccumul
1cff6 61 74 6f 72 28 70 50 61 72 73 65 2c 20 26 73 41  ator(pParse, &sA
1cff7 67 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 73  ggInfo);.      s
1cff8 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1cff9 76 2c 20 4f 50 5f 4d 65 6d 49 6e 74 2c 20 31 2c  v, OP_MemInt, 1,
1cffa 20 69 55 73 65 46 6c 61 67 29 3b 0a 20 20 20 20   iUseFlag);.    
1cffb 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
1cffc 2c 20 22 23 20 69 6e 64 69 63 61 74 65 20 64 61  , "# indicate da
1cffd 74 61 20 69 6e 20 61 63 63 75 6d 75 6c 61 74 6f  ta in accumulato
1cffe 72 22 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  r"));..      /* 
1cfff 45 6e 64 20 6f 66 20 74 68 65 20 6c 6f 6f 70 0a  End of the loop.
1d000 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
1d001 66 28 20 67 72 6f 75 70 42 79 53 6f 72 74 20 29  f( groupBySort )
1d002 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1d003 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1d004 5f 4e 65 78 74 2c 20 73 41 67 67 49 6e 66 6f 2e  _Next, sAggInfo.
1d005 73 6f 72 74 69 6e 67 49 64 78 2c 20 61 64 64 72  sortingIdx, addr
1d006 54 6f 70 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 20  TopOfLoop);.    
1d007 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1d008 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64   sqlite3WhereEnd
1d009 28 70 57 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20  (pWInfo);.      
1d00a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
1d00b 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c 20 61 64 64  ngeToNoop(v, add
1d00c 72 53 6f 72 74 69 6e 67 49 64 78 2c 20 31 29 3b  rSortingIdx, 1);
1d00d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
1d00e 2f 2a 20 4f 75 74 70 75 74 20 74 68 65 20 66 69  /* Output the fi
1d00f 6e 61 6c 20 72 6f 77 20 6f 66 20 72 65 73 75 6c  nal row of resul
1d010 74 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  t.      */.     
1d011 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1d012 70 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 30  p(v, OP_Gosub, 0
1d013 2c 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77 29  , addrOutputRow)
1d014 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d  ;.      VdbeComm
1d015 65 6e 74 28 28 76 2c 20 22 23 20 6f 75 74 70 75  ent((v, "# outpu
1d016 74 20 66 69 6e 61 6c 20 72 6f 77 22 29 29 3b 0a  t final row"));.
1d017 20 20 20 20 20 20 0a 20 20 20 20 7d 20 2f 2a 20        .    } /* 
1d018 65 6e 64 69 66 20 70 47 72 6f 75 70 42 79 20 2a  endif pGroupBy *
1d019 2f 0a 20 20 20 20 65 6c 73 65 20 7b 0a 20 20 20  /.    else {.   
1d01a 20 20 20 2f 2a 20 54 68 69 73 20 63 61 73 65 20     /* This case 
1d01b 72 75 6e 73 20 69 66 20 74 68 65 20 61 67 67 72  runs if the aggr
1d01c 65 67 61 74 65 20 68 61 73 20 6e 6f 20 47 52 4f  egate has no GRO
1d01d 55 50 20 42 59 20 63 6c 61 75 73 65 2e 20 20 54  UP BY clause.  T
1d01e 68 65 0a 20 20 20 20 20 20 2a 2a 20 70 72 6f 63  he.      ** proc
1d01f 65 73 73 69 6e 67 20 69 73 20 6d 75 63 68 20 73  essing is much s
1d020 69 6d 70 6c 65 72 20 73 69 6e 63 65 20 74 68 65  impler since the
1d021 72 65 20 69 73 20 6f 6e 6c 79 20 61 20 73 69 6e  re is only a sin
1d022 67 6c 65 20 72 6f 77 0a 20 20 20 20 20 20 2a 2a  gle row.      **
1d023 20 6f 66 20 6f 75 74 70 75 74 2e 0a 20 20 20 20   of output..    
1d024 20 20 2a 2f 0a 20 20 20 20 20 20 72 65 73 65 74    */.      reset
1d025 41 63 63 75 6d 75 6c 61 74 6f 72 28 70 50 61 72  Accumulator(pPar
1d026 73 65 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a  se, &sAggInfo);.
1d027 20 20 20 20 20 20 70 57 49 6e 66 6f 20 3d 20 73        pWInfo = s
1d028 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e  qlite3WhereBegin
1d029 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73  (pParse, pTabLis
1d02a 74 2c 20 70 57 68 65 72 65 2c 20 30 29 3b 0a 20  t, pWhere, 0);. 
1d02b 20 20 20 20 20 69 66 28 20 70 57 49 6e 66 6f 3d       if( pWInfo=
1d02c 3d 30 20 29 20 67 6f 74 6f 20 73 65 6c 65 63 74  =0 ) goto select
1d02d 5f 65 6e 64 3b 0a 20 20 20 20 20 20 75 70 64 61  _end;.      upda
1d02e 74 65 41 63 63 75 6d 75 6c 61 74 6f 72 28 70 50  teAccumulator(pP
1d02f 61 72 73 65 2c 20 26 73 41 67 67 49 6e 66 6f 29  arse, &sAggInfo)
1d030 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 57  ;.      sqlite3W
1d031 68 65 72 65 45 6e 64 28 70 57 49 6e 66 6f 29 3b  hereEnd(pWInfo);
1d032 0a 20 20 20 20 20 20 66 69 6e 61 6c 69 7a 65 41  .      finalizeA
1d033 67 67 46 75 6e 63 74 69 6f 6e 73 28 70 50 61 72  ggFunctions(pPar
1d034 73 65 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a  se, &sAggInfo);.
1d035 20 20 20 20 20 20 70 4f 72 64 65 72 42 79 20 3d        pOrderBy =
1d036 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 70 48   0;.      if( pH
1d037 61 76 69 6e 67 20 29 7b 0a 20 20 20 20 20 20 20  aving ){.       
1d038 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61   sqlite3ExprIfFa
1d039 6c 73 65 28 70 50 61 72 73 65 2c 20 70 48 61 76  lse(pParse, pHav
1d03a 69 6e 67 2c 20 61 64 64 72 45 6e 64 2c 20 31 29  ing, addrEnd, 1)
1d03b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1d03c 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70 28  selectInnerLoop(
1d03d 70 50 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70 45  pParse, p, p->pE
1d03e 4c 69 73 74 2c 20 30 2c 20 30 2c 20 30 2c 20 2d  List, 0, 0, 0, -
1d03f 31 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  1, .            
1d040 20 20 20 20 20 20 20 20 20 20 65 44 65 73 74 2c            eDest,
1d041 20 69 50 61 72 6d 2c 20 61 64 64 72 45 6e 64 2c   iParm, addrEnd,
1d042 20 61 64 64 72 45 6e 64 2c 20 61 66 66 29 3b 0a   addrEnd, aff);.
1d043 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
1d044 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
1d045 6c 28 76 2c 20 61 64 64 72 45 6e 64 29 3b 0a 20  l(v, addrEnd);. 
1d046 20 20 20 0a 20 20 7d 20 2f 2a 20 65 6e 64 69 66     .  } /* endif
1d047 20 61 67 67 72 65 67 61 74 65 20 71 75 65 72 79   aggregate query
1d048 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65   */..  /* If the
1d049 72 65 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42  re is an ORDER B
1d04a 59 20 63 6c 61 75 73 65 2c 20 74 68 65 6e 20 77  Y clause, then w
1d04b 65 20 6e 65 65 64 20 74 6f 20 73 6f 72 74 20 74  e need to sort t
1d04c 68 65 20 72 65 73 75 6c 74 73 0a 20 20 2a 2a 20  he results.  ** 
1d04d 61 6e 64 20 73 65 6e 64 20 74 68 65 6d 20 74 6f  and send them to
1d04e 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e   the callback on
1d04f 65 20 62 79 20 6f 6e 65 2e 0a 20 20 2a 2f 0a 20  e by one..  */. 
1d050 20 69 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b   if( pOrderBy ){
1d051 0a 20 20 20 20 67 65 6e 65 72 61 74 65 53 6f 72  .    generateSor
1d052 74 54 61 69 6c 28 70 50 61 72 73 65 2c 20 70 2c  tTail(pParse, p,
1d053 20 76 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70   v, pEList->nExp
1d054 72 2c 20 65 44 65 73 74 2c 20 69 50 61 72 6d 29  r, eDest, iParm)
1d055 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53  ;.  }..#ifndef S
1d056 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
1d057 45 52 59 0a 20 20 2f 2a 20 49 66 20 74 68 69 73  ERY.  /* If this
1d058 20 77 61 73 20 61 20 73 75 62 71 75 65 72 79 2c   was a subquery,
1d059 20 77 65 20 68 61 76 65 20 6e 6f 77 20 63 6f 6e   we have now con
1d05a 76 65 72 74 65 64 20 74 68 65 20 73 75 62 71 75  verted the subqu
1d05b 65 72 79 20 69 6e 74 6f 20 61 0a 20 20 2a 2a 20  ery into a.  ** 
1d05c 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 2e  temporary table.
1d05d 20 20 53 6f 20 73 65 74 20 74 68 65 20 53 72 63    So set the Src
1d05e 4c 69 73 74 5f 69 74 65 6d 2e 69 73 50 6f 70 75  List_item.isPopu
1d05f 6c 61 74 65 64 20 66 6c 61 67 20 74 6f 20 70 72  lated flag to pr
1d060 65 76 65 6e 74 0a 20 20 2a 2a 20 74 68 69 73 20  event.  ** this 
1d061 73 75 62 71 75 65 72 79 20 66 72 6f 6d 20 62 65  subquery from be
1d062 69 6e 67 20 65 76 61 6c 75 61 74 65 64 20 61 67  ing evaluated ag
1d063 61 69 6e 20 61 6e 64 20 74 6f 20 66 6f 72 63 65  ain and to force
1d064 20 74 68 65 20 75 73 65 20 6f 66 0a 20 20 2a 2a   the use of.  **
1d065 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74   the temporary t
1d066 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  able..  */.  if(
1d067 20 70 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20   pParent ){.    
1d068 61 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 2d  assert( pParent-
1d069 3e 70 53 72 63 2d 3e 6e 53 72 63 3e 70 61 72 65  >pSrc->nSrc>pare
1d06a 6e 74 54 61 62 20 29 3b 0a 20 20 20 20 61 73 73  ntTab );.    ass
1d06b 65 72 74 28 20 70 50 61 72 65 6e 74 2d 3e 70 53  ert( pParent->pS
1d06c 72 63 2d 3e 61 5b 70 61 72 65 6e 74 54 61 62 5d  rc->a[parentTab]
1d06d 2e 70 53 65 6c 65 63 74 3d 3d 70 20 29 3b 0a 20  .pSelect==p );. 
1d06e 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 53 72 63     pParent->pSrc
1d06f 2d 3e 61 5b 70 61 72 65 6e 74 54 61 62 5d 2e 69  ->a[parentTab].i
1d070 73 50 6f 70 75 6c 61 74 65 64 20 3d 20 31 3b 0a  sPopulated = 1;.
1d071 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
1d072 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 73 6b   Jump here to sk
1d073 69 70 20 74 68 69 73 20 71 75 65 72 79 0a 20 20  ip this query.  
1d074 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  */.  sqlite3Vdbe
1d075 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
1d076 69 45 6e 64 29 3b 0a 0a 20 20 2f 2a 20 54 68 65  iEnd);..  /* The
1d077 20 53 45 4c 45 43 54 20 77 61 73 20 73 75 63 63   SELECT was succ
1d078 65 73 73 66 75 6c 6c 79 20 63 6f 64 65 64 2e 20  essfully coded. 
1d079 20 20 53 65 74 20 74 68 65 20 72 65 74 75 72 6e    Set the return
1d07a 20 63 6f 64 65 20 74 6f 20 30 0a 20 20 2a 2a 20   code to 0.  ** 
1d07b 74 6f 20 69 6e 64 69 63 61 74 65 20 6e 6f 20 65  to indicate no e
1d07c 72 72 6f 72 73 2e 0a 20 20 2a 2f 0a 20 20 72 63  rrors..  */.  rc
1d07d 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 43 6f 6e 74   = 0;..  /* Cont
1d07e 72 6f 6c 20 6a 75 6d 70 73 20 74 6f 20 68 65 72  rol jumps to her
1d07f 65 20 69 66 20 61 6e 20 65 72 72 6f 72 20 69 73  e if an error is
1d080 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 61 62 6f   encountered abo
1d081 76 65 2c 20 6f 72 20 75 70 6f 6e 0a 20 20 2a 2a  ve, or upon.  **
1d082 20 73 75 63 63 65 73 73 66 75 6c 20 63 6f 64 69   successful codi
1d083 6e 67 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  ng of the SELECT
1d084 2e 0a 20 20 2a 2f 0a 73 65 6c 65 63 74 5f 65 6e  ..  */.select_en
1d085 64 3a 0a 0a 20 20 2f 2a 20 49 64 65 6e 74 69 66  d:..  /* Identif
1d086 79 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 69  y column names i
1d087 66 20 77 65 20 77 69 6c 6c 20 62 65 20 75 73 69  f we will be usi
1d088 6e 67 20 74 68 65 6d 20 69 6e 20 61 20 63 61 6c  ng them in a cal
1d089 6c 62 61 63 6b 2e 20 20 54 68 69 73 0a 20 20 2a  lback.  This.  *
1d08a 2a 20 73 74 65 70 20 69 73 20 73 6b 69 70 70 65  * step is skippe
1d08b 64 20 69 66 20 74 68 65 20 6f 75 74 70 75 74 20  d if the output 
1d08c 69 73 20 67 6f 69 6e 67 20 74 6f 20 73 6f 6d 65  is going to some
1d08d 20 6f 74 68 65 72 20 64 65 73 74 69 6e 61 74 69   other destinati
1d08e 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72  on..  */.  if( r
1d08f 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
1d090 65 44 65 73 74 3d 3d 53 52 54 5f 43 61 6c 6c 62  eDest==SRT_Callb
1d091 61 63 6b 20 29 7b 0a 20 20 20 20 67 65 6e 65 72  ack ){.    gener
1d092 61 74 65 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70  ateColumnNames(p
1d093 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c  Parse, pTabList,
1d094 20 70 45 4c 69 73 74 29 3b 0a 20 20 7d 0a 0a 20   pEList);.  }.. 
1d095 20 73 71 6c 69 74 65 46 72 65 65 28 73 41 67 67   sqliteFree(sAgg
1d096 49 6e 66 6f 2e 61 43 6f 6c 29 3b 0a 20 20 73 71  Info.aCol);.  sq
1d097 6c 69 74 65 46 72 65 65 28 73 41 67 67 49 6e 66  liteFree(sAggInf
1d098 6f 2e 61 46 75 6e 63 29 3b 0a 20 20 72 65 74 75  o.aFunc);.  retu
1d099 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 64 65  rn rc;.}..#if de
1d09a 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42  fined(SQLITE_DEB
1d09b 55 47 29 0a 2f 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  UG)./*.*********
1d09c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d09d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d09e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d09f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d0a0 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f  ******.** The fo
1d0a1 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 69 73 20  llowing code is 
1d0a2 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67  used for testing
1d0a3 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20 6f   and debugging o
1d0a4 6e 6c 79 2e 20 20 54 68 65 20 63 6f 64 65 0a 2a  nly.  The code.*
1d0a5 2a 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20 64  * that follows d
1d0a6 6f 65 73 20 6e 6f 74 20 61 70 70 65 61 72 20 69  oes not appear i
1d0a7 6e 20 6e 6f 72 6d 61 6c 20 62 75 69 6c 64 73 2e  n normal builds.
1d0a8 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75  .**.** These rou
1d0a9 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 74  tines are used t
1d0aa 6f 20 70 72 69 6e 74 20 6f 75 74 20 74 68 65 20  o print out the 
1d0ab 63 6f 6e 74 65 6e 74 20 6f 66 20 61 6c 6c 20 6f  content of all o
1d0ac 72 20 70 61 72 74 20 6f 66 20 61 20 0a 2a 2a 20  r part of a .** 
1d0ad 70 61 72 73 65 20 73 74 72 75 63 74 75 72 65 73  parse structures
1d0ae 20 73 75 63 68 20 61 73 20 53 65 6c 65 63 74 20   such as Select 
1d0af 6f 72 20 45 78 70 72 2e 20 20 53 75 63 68 20 70  or Expr.  Such p
1d0b0 72 69 6e 74 6f 75 74 73 20 61 72 65 20 75 73 65  rintouts are use
1d0b1 66 75 6c 0a 2a 2a 20 66 6f 72 20 68 65 6c 70 69  ful.** for helpi
1d0b2 6e 67 20 74 6f 20 75 6e 64 65 72 73 74 61 6e 64  ng to understand
1d0b3 20 77 68 61 74 20 69 73 20 68 61 70 70 65 6e 69   what is happeni
1d0b4 6e 67 20 69 6e 73 69 64 65 20 74 68 65 20 63 6f  ng inside the co
1d0b5 64 65 20 67 65 6e 65 72 61 74 6f 72 0a 2a 2a 20  de generator.** 
1d0b6 64 75 72 69 6e 67 20 74 68 65 20 65 78 65 63 75  during the execu
1d0b7 74 69 6f 6e 20 6f 66 20 63 6f 6d 70 6c 65 78 20  tion of complex 
1d0b8 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
1d0b9 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72  s..**.** These r
1d0ba 6f 75 74 69 6e 65 20 61 72 65 20 6e 6f 74 20 63  outine are not c
1d0bb 61 6c 6c 65 64 20 61 6e 79 77 68 65 72 65 20 66  alled anywhere f
1d0bc 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20 6e  rom within the n
1d0bd 6f 72 6d 61 6c 0a 2a 2a 20 63 6f 64 65 20 62 61  ormal.** code ba
1d0be 73 65 2e 20 20 54 68 65 6e 20 61 72 65 20 69 6e  se.  Then are in
1d0bf 74 65 6e 64 65 64 20 74 6f 20 62 65 20 63 61 6c  tended to be cal
1d0c0 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20  led from within 
1d0c1 74 68 65 20 64 65 62 75 67 67 65 72 0a 2a 2a 20  the debugger.** 
1d0c2 6f 72 20 66 72 6f 6d 20 74 65 6d 70 6f 72 61 72  or from temporar
1d0c3 79 20 22 70 72 69 6e 74 66 22 20 73 74 61 74 65  y "printf" state
1d0c4 6d 65 6e 74 73 20 69 6e 73 65 72 74 65 64 20 66  ments inserted f
1d0c5 6f 72 20 64 65 62 75 67 67 69 6e 67 2e 0a 2a 2f  or debugging..*/
1d0c6 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1d0c7 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 69 6e  void sqlite3Prin
1d0c8 74 45 78 70 72 28 45 78 70 72 20 2a 70 29 7b 0a  tExpr(Expr *p){.
1d0c9 20 20 69 66 28 20 70 2d 3e 74 6f 6b 65 6e 2e 7a    if( p->token.z
1d0ca 20 26 26 20 70 2d 3e 74 6f 6b 65 6e 2e 6e 3e 30   && p->token.n>0
1d0cb 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44   ){.    sqlite3D
1d0cc 65 62 75 67 50 72 69 6e 74 66 28 22 28 25 2e 2a  ebugPrintf("(%.*
1d0cd 73 22 2c 20 70 2d 3e 74 6f 6b 65 6e 2e 6e 2c 20  s", p->token.n, 
1d0ce 70 2d 3e 74 6f 6b 65 6e 2e 7a 29 3b 0a 20 20 7d  p->token.z);.  }
1d0cf 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
1d0d0 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 28 25  3DebugPrintf("(%
1d0d1 64 22 2c 20 70 2d 3e 6f 70 29 3b 0a 20 20 7d 0a  d", p->op);.  }.
1d0d2 20 20 69 66 28 20 70 2d 3e 70 4c 65 66 74 20 29    if( p->pLeft )
1d0d3 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62  {.    sqlite3Deb
1d0d4 75 67 50 72 69 6e 74 66 28 22 20 22 29 3b 0a 20  ugPrintf(" ");. 
1d0d5 20 20 20 73 71 6c 69 74 65 33 50 72 69 6e 74 45     sqlite3PrintE
1d0d6 78 70 72 28 70 2d 3e 70 4c 65 66 74 29 3b 0a 20  xpr(p->pLeft);. 
1d0d7 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 52 69 67   }.  if( p->pRig
1d0d8 68 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ht ){.    sqlite
1d0d9 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 20 22  3DebugPrintf(" "
1d0da 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 72  );.    sqlite3Pr
1d0db 69 6e 74 45 78 70 72 28 70 2d 3e 70 52 69 67 68  intExpr(p->pRigh
1d0dc 74 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  t);.  }.  sqlite
1d0dd 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 29 22  3DebugPrintf(")"
1d0de 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  );.}.SQLITE_PRIV
1d0df 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1d0e0 50 72 69 6e 74 45 78 70 72 4c 69 73 74 28 45 78  PrintExprList(Ex
1d0e1 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a  prList *pList){.
1d0e2 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69    int i;.  for(i
1d0e3 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78  =0; i<pList->nEx
1d0e4 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71  pr; i++){.    sq
1d0e5 6c 69 74 65 33 50 72 69 6e 74 45 78 70 72 28 70  lite3PrintExpr(p
1d0e6 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72  List->a[i].pExpr
1d0e7 29 3b 0a 20 20 20 20 69 66 28 20 69 3c 70 4c 69  );.    if( i<pLi
1d0e8 73 74 2d 3e 6e 45 78 70 72 2d 31 20 29 7b 0a 20  st->nExpr-1 ){. 
1d0e9 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75       sqlite3Debu
1d0ea 67 50 72 69 6e 74 66 28 22 2c 20 22 29 3b 0a 20  gPrintf(", ");. 
1d0eb 20 20 20 7d 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54     }.  }.}.SQLIT
1d0ec 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1d0ed 71 6c 69 74 65 33 50 72 69 6e 74 53 65 6c 65 63  qlite3PrintSelec
1d0ee 74 28 53 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74  t(Select *p, int
1d0ef 20 69 6e 64 65 6e 74 29 7b 0a 20 20 73 71 6c 69   indent){.  sqli
1d0f0 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
1d0f1 25 2a 73 53 45 4c 45 43 54 28 25 70 29 20 22 2c  %*sSELECT(%p) ",
1d0f2 20 69 6e 64 65 6e 74 2c 20 22 22 2c 20 70 29 3b   indent, "", p);
1d0f3 0a 20 20 73 71 6c 69 74 65 33 50 72 69 6e 74 45  .  sqlite3PrintE
1d0f4 78 70 72 4c 69 73 74 28 70 2d 3e 70 45 4c 69 73  xprList(p->pELis
1d0f5 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 62  t);.  sqlite3Deb
1d0f6 75 67 50 72 69 6e 74 66 28 22 5c 6e 22 29 3b 0a  ugPrintf("\n");.
1d0f7 20 20 69 66 28 20 70 2d 3e 70 53 72 63 20 29 7b    if( p->pSrc ){
1d0f8 0a 20 20 20 20 63 68 61 72 20 2a 7a 50 72 65 66  .    char *zPref
1d0f9 69 78 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20  ix;.    int i;. 
1d0fa 20 20 20 7a 50 72 65 66 69 78 20 3d 20 22 46 52     zPrefix = "FR
1d0fb 4f 4d 22 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  OM";.    for(i=0
1d0fc 3b 20 69 3c 70 2d 3e 70 53 72 63 2d 3e 6e 53 72  ; i<p->pSrc->nSr
1d0fd 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73  c; i++){.      s
1d0fe 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
1d0ff 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 2d 3e  em *pItem = &p->
1d100 70 53 72 63 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20  pSrc->a[i];.    
1d101 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
1d102 69 6e 74 66 28 22 25 2a 73 20 22 2c 20 69 6e 64  intf("%*s ", ind
1d103 65 6e 74 2b 36 2c 20 7a 50 72 65 66 69 78 29 3b  ent+6, zPrefix);
1d104 0a 20 20 20 20 20 20 7a 50 72 65 66 69 78 20 3d  .      zPrefix =
1d105 20 22 22 3b 0a 20 20 20 20 20 20 69 66 28 20 70   "";.      if( p
1d106 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 29 7b  Item->pSelect ){
1d107 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1d108 44 65 62 75 67 50 72 69 6e 74 66 28 22 28 5c 6e  DebugPrintf("(\n
1d109 22 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  ");.        sqli
1d10a 74 65 33 50 72 69 6e 74 53 65 6c 65 63 74 28 70  te3PrintSelect(p
1d10b 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 2c 20 69  Item->pSelect, i
1d10c 6e 64 65 6e 74 2b 31 30 29 3b 0a 20 20 20 20 20  ndent+10);.     
1d10d 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
1d10e 72 69 6e 74 66 28 22 25 2a 73 29 22 2c 20 69 6e  rintf("%*s)", in
1d10f 64 65 6e 74 2b 38 2c 20 22 22 29 3b 0a 20 20 20  dent+8, "");.   
1d110 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 49 74     }else if( pIt
1d111 65 6d 2d 3e 7a 4e 61 6d 65 20 29 7b 0a 20 20 20  em->zName ){.   
1d112 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75       sqlite3Debu
1d113 67 50 72 69 6e 74 66 28 22 25 73 22 2c 20 70 49  gPrintf("%s", pI
1d114 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  tem->zName);.   
1d115 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70     }.      if( p
1d116 49 74 65 6d 2d 3e 70 54 61 62 20 29 7b 0a 20 20  Item->pTab ){.  
1d117 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 62        sqlite3Deb
1d118 75 67 50 72 69 6e 74 66 28 22 28 74 61 62 6c 65  ugPrintf("(table
1d119 3a 20 25 73 29 22 2c 20 70 49 74 65 6d 2d 3e 70  : %s)", pItem->p
1d11a 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  Tab->zName);.   
1d11b 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70     }.      if( p
1d11c 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 29 7b 0a  Item->zAlias ){.
1d11d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
1d11e 65 62 75 67 50 72 69 6e 74 66 28 22 20 41 53 20  ebugPrintf(" AS 
1d11f 25 73 22 2c 20 70 49 74 65 6d 2d 3e 7a 41 6c 69  %s", pItem->zAli
1d120 61 73 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  as);.      }.   
1d121 20 20 20 69 66 28 20 69 3c 70 2d 3e 70 53 72 63     if( i<p->pSrc
1d122 2d 3e 6e 53 72 63 2d 31 20 29 7b 0a 20 20 20 20  ->nSrc-1 ){.    
1d123 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67      sqlite3Debug
1d124 50 72 69 6e 74 66 28 22 2c 22 29 3b 0a 20 20 20  Printf(",");.   
1d125 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
1d126 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 5c  e3DebugPrintf("\
1d127 6e 22 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  n");.    }.  }. 
1d128 20 69 66 28 20 70 2d 3e 70 57 68 65 72 65 20 29   if( p->pWhere )
1d129 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62  {.    sqlite3Deb
1d12a 75 67 50 72 69 6e 74 66 28 22 25 2a 73 20 57 48  ugPrintf("%*s WH
1d12b 45 52 45 20 22 2c 20 69 6e 64 65 6e 74 2c 20 22  ERE ", indent, "
1d12c 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50  ");.    sqlite3P
1d12d 72 69 6e 74 45 78 70 72 28 70 2d 3e 70 57 68 65  rintExpr(p->pWhe
1d12e 72 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  re);.    sqlite3
1d12f 44 65 62 75 67 50 72 69 6e 74 66 28 22 5c 6e 22  DebugPrintf("\n"
1d130 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e  );.  }.  if( p->
1d131 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 20  pGroupBy ){.    
1d132 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
1d133 74 66 28 22 25 2a 73 20 47 52 4f 55 50 20 42 59  tf("%*s GROUP BY
1d134 20 22 2c 20 69 6e 64 65 6e 74 2c 20 22 22 29 3b   ", indent, "");
1d135 0a 20 20 20 20 73 71 6c 69 74 65 33 50 72 69 6e  .    sqlite3Prin
1d136 74 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 47 72  tExprList(p->pGr
1d137 6f 75 70 42 79 29 3b 0a 20 20 20 20 73 71 6c 69  oupBy);.    sqli
1d138 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
1d139 5c 6e 22 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  \n");.  }.  if( 
1d13a 70 2d 3e 70 48 61 76 69 6e 67 20 29 7b 0a 20 20  p->pHaving ){.  
1d13b 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
1d13c 69 6e 74 66 28 22 25 2a 73 20 48 41 56 49 4e 47  intf("%*s HAVING
1d13d 20 22 2c 20 69 6e 64 65 6e 74 2c 20 22 22 29 3b   ", indent, "");
1d13e 0a 20 20 20 20 73 71 6c 69 74 65 33 50 72 69 6e  .    sqlite3Prin
1d13f 74 45 78 70 72 28 70 2d 3e 70 48 61 76 69 6e 67  tExpr(p->pHaving
1d140 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  );.    sqlite3De
1d141 62 75 67 50 72 69 6e 74 66 28 22 5c 6e 22 29 3b  bugPrintf("\n");
1d142 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 4f  .  }.  if( p->pO
1d143 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 73 71  rderBy ){.    sq
1d144 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
1d145 28 22 25 2a 73 20 4f 52 44 45 52 20 42 59 20 22  ("%*s ORDER BY "
1d146 2c 20 69 6e 64 65 6e 74 2c 20 22 22 29 3b 0a 20  , indent, "");. 
1d147 20 20 20 73 71 6c 69 74 65 33 50 72 69 6e 74 45     sqlite3PrintE
1d148 78 70 72 4c 69 73 74 28 70 2d 3e 70 4f 72 64 65  xprList(p->pOrde
1d149 72 42 79 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  rBy);.    sqlite
1d14a 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 5c 6e  3DebugPrintf("\n
1d14b 22 29 3b 0a 20 20 7d 0a 7d 0a 2f 2a 20 45 6e 64  ");.  }.}./* End
1d14c 20 6f 66 20 74 68 65 20 73 74 72 75 63 74 75 72   of the structur
1d14d 65 20 64 65 62 75 67 20 70 72 69 6e 74 69 6e 67  e debug printing
1d14e 20 63 6f 64 65 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   code.**********
1d14f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d151 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d152 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d153 2a 2a 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 64  ***/.#endif /* d
1d154 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45  efined(SQLITE_TE
1d155 53 54 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  ST) || defined(S
1d156 51 4c 49 54 45 5f 44 45 42 55 47 29 20 2a 2f 0a  QLITE_DEBUG) */.
1d157 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1d158 20 45 6e 64 20 6f 66 20 73 65 6c 65 63 74 2e 63   End of select.c
1d159 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1d15a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d15b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1d15c 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1d15d 20 42 65 67 69 6e 20 66 69 6c 65 20 74 61 62 6c   Begin file tabl
1d15e 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
1d15f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1d161 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74  ./*.** 2001 Sept
1d162 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54  ember 15.**.** T
1d163 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
1d164 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
1d165 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
1d166 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
1d167 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
1d168 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
1d169 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
1d16a 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
1d16b 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
1d16c 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
1d16d 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
1d16e 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
1d16f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
1d170 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
1d171 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
1d172 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
1d173 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
1d174 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d175 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d176 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d177 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d178 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
1d179 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68  file contains th
1d17a 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61  e sqlite3_get_ta
1d17b 62 6c 65 28 29 20 61 6e 64 20 73 71 6c 69 74 65  ble() and sqlite
1d17c 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 29 0a 2a  3_free_table().*
1d17d 2a 20 69 6e 74 65 72 66 61 63 65 20 72 6f 75 74  * interface rout
1d17e 69 6e 65 73 2e 20 20 54 68 65 73 65 20 61 72 65  ines.  These are
1d17f 20 6a 75 73 74 20 77 72 61 70 70 65 72 73 20 61   just wrappers a
1d180 72 6f 75 6e 64 20 74 68 65 20 6d 61 69 6e 0a 2a  round the main.*
1d181 2a 20 69 6e 74 65 72 66 61 63 65 20 72 6f 75 74  * interface rout
1d182 69 6e 65 20 6f 66 20 73 71 6c 69 74 65 33 5f 65  ine of sqlite3_e
1d183 78 65 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  xec()..**.** The
1d184 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20  se routines are 
1d185 69 6e 20 61 20 73 65 70 61 72 61 74 65 20 66 69  in a separate fi
1d186 6c 65 73 20 73 6f 20 74 68 61 74 20 74 68 65 79  les so that they
1d187 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 6c 69 6e   will not be lin
1d188 6b 65 64 0a 2a 2a 20 69 66 20 74 68 65 79 20 61  ked.** if they a
1d189 72 65 20 6e 6f 74 20 75 73 65 64 2e 0a 2a 2f 0a  re not used..*/.
1d18a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1d18b 4f 4d 49 54 5f 47 45 54 5f 54 41 42 4c 45 0a 0a  OMIT_GET_TABLE..
1d18c 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63  /*.** This struc
1d18d 74 75 72 65 20 69 73 20 75 73 65 64 20 74 6f 20  ture is used to 
1d18e 70 61 73 73 20 64 61 74 61 20 66 72 6f 6d 20 73  pass data from s
1d18f 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65  qlite3_get_table
1d190 28 29 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f  () through.** to
1d191 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75   the callback fu
1d192 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 73 20 74  nction is uses t
1d193 6f 20 62 75 69 6c 64 20 74 68 65 20 72 65 73 75  o build the resu
1d194 6c 74 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  lt..*/.typedef s
1d195 74 72 75 63 74 20 54 61 62 52 65 73 75 6c 74 20  truct TabResult 
1d196 7b 0a 20 20 63 68 61 72 20 2a 2a 61 7a 52 65 73  {.  char **azRes
1d197 75 6c 74 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72  ult;.  char *zEr
1d198 72 4d 73 67 3b 0a 20 20 69 6e 74 20 6e 52 65 73  rMsg;.  int nRes
1d199 75 6c 74 3b 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f  ult;.  int nAllo
1d19a 63 3b 0a 20 20 69 6e 74 20 6e 52 6f 77 3b 0a 20  c;.  int nRow;. 
1d19b 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 0a 20 20   int nColumn;.  
1d19c 69 6e 74 20 6e 44 61 74 61 3b 0a 20 20 69 6e 74  int nData;.  int
1d19d 20 72 63 3b 0a 7d 20 54 61 62 52 65 73 75 6c 74   rc;.} TabResult
1d19e 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  ;../*.** This ro
1d19f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
1d1a0 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 72 6f  once for each ro
1d1a1 77 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  w in the result 
1d1a2 74 61 62 6c 65 2e 20 20 49 74 73 20 6a 6f 62 0a  table.  Its job.
1d1a3 2a 2a 20 69 73 20 74 6f 20 66 69 6c 6c 20 69 6e  ** is to fill in
1d1a4 20 74 68 65 20 54 61 62 52 65 73 75 6c 74 20 73   the TabResult s
1d1a5 74 72 75 63 74 75 72 65 20 61 70 70 72 6f 70 72  tructure appropr
1d1a6 69 61 74 65 6c 79 2c 20 61 6c 6c 6f 63 61 74 69  iately, allocati
1d1a7 6e 67 20 6e 65 77 0a 2a 2a 20 6d 65 6d 6f 72 79  ng new.** memory
1d1a8 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a   as necessary..*
1d1a9 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c  /.static int sql
1d1aa 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 5f 63  ite3_get_table_c
1d1ab 62 28 76 6f 69 64 20 2a 70 41 72 67 2c 20 69 6e  b(void *pArg, in
1d1ac 74 20 6e 43 6f 6c 2c 20 63 68 61 72 20 2a 2a 61  t nCol, char **a
1d1ad 72 67 76 2c 20 63 68 61 72 20 2a 2a 63 6f 6c 76  rgv, char **colv
1d1ae 29 7b 0a 20 20 54 61 62 52 65 73 75 6c 74 20 2a  ){.  TabResult *
1d1af 70 20 3d 20 28 54 61 62 52 65 73 75 6c 74 2a 29  p = (TabResult*)
1d1b0 70 41 72 67 3b 0a 20 20 69 6e 74 20 6e 65 65 64  pArg;.  int need
1d1b1 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 68 61  ;.  int i;.  cha
1d1b2 72 20 2a 7a 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65  r *z;..  /* Make
1d1b3 20 73 75 72 65 20 74 68 65 72 65 20 69 73 20 65   sure there is e
1d1b4 6e 6f 75 67 68 20 73 70 61 63 65 20 69 6e 20 70  nough space in p
1d1b5 2d 3e 61 7a 52 65 73 75 6c 74 20 74 6f 20 68 6f  ->azResult to ho
1d1b6 6c 64 20 65 76 65 72 79 74 68 69 6e 67 0a 20 20  ld everything.  
1d1b7 2a 2a 20 77 65 20 6e 65 65 64 20 74 6f 20 72 65  ** we need to re
1d1b8 6d 65 6d 62 65 72 20 66 72 6f 6d 20 74 68 69 73  member from this
1d1b9 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74   invocation of t
1d1ba 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 20 20 2a  he callback..  *
1d1bb 2f 0a 20 20 69 66 28 20 70 2d 3e 6e 52 6f 77 3d  /.  if( p->nRow=
1d1bc 3d 30 20 26 26 20 61 72 67 76 21 3d 30 20 29 7b  =0 && argv!=0 ){
1d1bd 0a 20 20 20 20 6e 65 65 64 20 3d 20 6e 43 6f 6c  .    need = nCol
1d1be 2a 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  *2;.  }else{.   
1d1bf 20 6e 65 65 64 20 3d 20 6e 43 6f 6c 3b 0a 20 20   need = nCol;.  
1d1c0 7d 0a 20 20 69 66 28 20 70 2d 3e 6e 44 61 74 61  }.  if( p->nData
1d1c1 20 2b 20 6e 65 65 64 20 3e 3d 20 70 2d 3e 6e 41   + need >= p->nA
1d1c2 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 63 68 61 72  lloc ){.    char
1d1c3 20 2a 2a 61 7a 4e 65 77 3b 0a 20 20 20 20 70 2d   **azNew;.    p-
1d1c4 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 41 6c  >nAlloc = p->nAl
1d1c5 6c 6f 63 2a 32 20 2b 20 6e 65 65 64 20 2b 20 31  loc*2 + need + 1
1d1c6 3b 0a 20 20 20 20 61 7a 4e 65 77 20 3d 20 73 71  ;.    azNew = sq
1d1c7 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 20 70  lite3_realloc( p
1d1c8 2d 3e 61 7a 52 65 73 75 6c 74 2c 20 73 69 7a 65  ->azResult, size
1d1c9 6f 66 28 63 68 61 72 2a 29 2a 70 2d 3e 6e 41 6c  of(char*)*p->nAl
1d1ca 6c 6f 63 20 29 3b 0a 20 20 20 20 69 66 28 20 61  loc );.    if( a
1d1cb 7a 4e 65 77 3d 3d 30 20 29 20 67 6f 74 6f 20 6d  zNew==0 ) goto m
1d1cc 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b 0a 20 20  alloc_failed;.  
1d1cd 20 20 70 2d 3e 61 7a 52 65 73 75 6c 74 20 3d 20    p->azResult = 
1d1ce 61 7a 4e 65 77 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  azNew;.  }..  /*
1d1cf 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20   If this is the 
1d1d0 66 69 72 73 74 20 72 6f 77 2c 20 74 68 65 6e 20  first row, then 
1d1d1 67 65 6e 65 72 61 74 65 20 61 6e 20 65 78 74 72  generate an extr
1d1d2 61 20 72 6f 77 20 63 6f 6e 74 61 69 6e 69 6e 67  a row containing
1d1d3 0a 20 20 2a 2a 20 74 68 65 20 6e 61 6d 65 73 20  .  ** the names 
1d1d4 6f 66 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 2e 0a  of all columns..
1d1d5 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 6e 52    */.  if( p->nR
1d1d6 6f 77 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e  ow==0 ){.    p->
1d1d7 6e 43 6f 6c 75 6d 6e 20 3d 20 6e 43 6f 6c 3b 0a  nColumn = nCol;.
1d1d8 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
1d1d9 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Col; i++){.     
1d1da 20 69 66 28 20 63 6f 6c 76 5b 69 5d 3d 3d 30 20   if( colv[i]==0 
1d1db 29 7b 0a 20 20 20 20 20 20 20 20 7a 20 3d 20 73  ){.        z = s
1d1dc 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
1d1dd 22 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ");.      }else{
1d1de 0a 20 20 20 20 20 20 20 20 7a 20 3d 20 73 71 6c  .        z = sql
1d1df 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73  ite3_mprintf("%s
1d1e0 22 2c 20 63 6f 6c 76 5b 69 5d 29 3b 0a 20 20 20  ", colv[i]);.   
1d1e1 20 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e 61 7a     }.      p->az
1d1e2 52 65 73 75 6c 74 5b 70 2d 3e 6e 44 61 74 61 2b  Result[p->nData+
1d1e3 2b 5d 20 3d 20 7a 3b 0a 20 20 20 20 7d 0a 20 20  +] = z;.    }.  
1d1e4 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 6e 43 6f  }else if( p->nCo
1d1e5 6c 75 6d 6e 21 3d 6e 43 6f 6c 20 29 7b 0a 20 20  lumn!=nCol ){.  
1d1e6 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
1d1e7 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 0a  ng(&p->zErrMsg,.
1d1e8 20 20 20 20 20 20 20 22 73 71 6c 69 74 65 33 5f         "sqlite3_
1d1e9 67 65 74 5f 74 61 62 6c 65 28 29 20 63 61 6c 6c  get_table() call
1d1ea 65 64 20 77 69 74 68 20 74 77 6f 20 6f 72 20 6d  ed with two or m
1d1eb 6f 72 65 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65  ore incompatible
1d1ec 20 71 75 65 72 69 65 73 22 2c 0a 20 20 20 20 20   queries",.     
1d1ed 20 20 28 63 68 61 72 2a 29 30 29 3b 0a 20 20 20    (char*)0);.   
1d1ee 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f   p->rc = SQLITE_
1d1ef 45 52 52 4f 52 3b 0a 20 20 20 20 72 65 74 75 72  ERROR;.    retur
1d1f0 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43  n 1;.  }..  /* C
1d1f1 6f 70 79 20 6f 76 65 72 20 74 68 65 20 72 6f 77  opy over the row
1d1f2 20 64 61 74 61 0a 20 20 2a 2f 0a 20 20 69 66 28   data.  */.  if(
1d1f3 20 61 72 67 76 21 3d 30 20 29 7b 0a 20 20 20 20   argv!=0 ){.    
1d1f4 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b  for(i=0; i<nCol;
1d1f5 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   i++){.      if(
1d1f6 20 61 72 67 76 5b 69 5d 3d 3d 30 20 29 7b 0a 20   argv[i]==0 ){. 
1d1f7 20 20 20 20 20 20 20 7a 20 3d 20 30 3b 0a 20 20         z = 0;.  
1d1f8 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1d1f9 20 20 20 69 6e 74 20 6e 20 3d 20 73 74 72 6c 65     int n = strle
1d1fa 6e 28 61 72 67 76 5b 69 5d 29 2b 31 3b 0a 20 20  n(argv[i])+1;.  
1d1fb 20 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65        z = sqlite
1d1fc 33 5f 6d 61 6c 6c 6f 63 28 20 6e 20 29 3b 0a 20  3_malloc( n );. 
1d1fd 20 20 20 20 20 20 20 69 66 28 20 7a 3d 3d 30 20         if( z==0 
1d1fe 29 20 67 6f 74 6f 20 6d 61 6c 6c 6f 63 5f 66 61  ) goto malloc_fa
1d1ff 69 6c 65 64 3b 0a 20 20 20 20 20 20 20 20 6d 65  iled;.        me
1d200 6d 63 70 79 28 7a 2c 20 61 72 67 76 5b 69 5d 2c  mcpy(z, argv[i],
1d201 20 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   n);.      }.   
1d202 20 20 20 70 2d 3e 61 7a 52 65 73 75 6c 74 5b 70     p->azResult[p
1d203 2d 3e 6e 44 61 74 61 2b 2b 5d 20 3d 20 7a 3b 0a  ->nData++] = z;.
1d204 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 6e 52 6f      }.    p->nRo
1d205 77 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  w++;.  }.  retur
1d206 6e 20 30 3b 0a 0a 6d 61 6c 6c 6f 63 5f 66 61 69  n 0;..malloc_fai
1d207 6c 65 64 3a 0a 20 20 70 2d 3e 72 63 20 3d 20 53  led:.  p->rc = S
1d208 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 72  QLITE_NOMEM;.  r
1d209 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 1;.}../*.*
1d20a 2a 20 51 75 65 72 79 20 74 68 65 20 64 61 74 61  * Query the data
1d20b 62 61 73 65 2e 20 20 42 75 74 20 69 6e 73 74 65  base.  But inste
1d20c 61 64 20 6f 66 20 69 6e 76 6f 6b 69 6e 67 20 61  ad of invoking a
1d20d 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 65 61   callback for ea
1d20e 63 68 20 72 6f 77 2c 0a 2a 2a 20 6d 61 6c 6c 6f  ch row,.** mallo
1d20f 63 28 29 20 66 6f 72 20 73 70 61 63 65 20 74 6f  c() for space to
1d210 20 68 6f 6c 64 20 74 68 65 20 72 65 73 75 6c 74   hold the result
1d211 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20   and return the 
1d212 65 6e 74 69 72 65 20 72 65 73 75 6c 74 73 0a 2a  entire results.*
1d213 2a 20 61 74 20 74 68 65 20 63 6f 6e 63 6c 75 73  * at the conclus
1d214 69 6f 6e 20 6f 66 20 74 68 65 20 63 61 6c 6c 2e  ion of the call.
1d215 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c  .**.** The resul
1d216 74 20 74 68 61 74 20 69 73 20 77 72 69 74 74 65  t that is writte
1d217 6e 20 74 6f 20 2a 2a 2a 70 61 7a 52 65 73 75 6c  n to ***pazResul
1d218 74 20 69 73 20 68 65 6c 64 20 69 6e 20 6d 65 6d  t is held in mem
1d219 6f 72 79 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20  ory obtained.** 
1d21a 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 20 20  from malloc().  
1d21b 42 75 74 20 74 68 65 20 63 61 6c 6c 65 72 20 63  But the caller c
1d21c 61 6e 6e 6f 74 20 66 72 65 65 20 74 68 69 73 20  annot free this 
1d21d 6d 65 6d 6f 72 79 20 64 69 72 65 63 74 6c 79 2e  memory directly.
1d21e 20 20 0a 2a 2a 20 49 6e 73 74 65 61 64 2c 20 74    .** Instead, t
1d21f 68 65 20 65 6e 74 69 72 65 20 74 61 62 6c 65 20  he entire table 
1d220 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64  should be passed
1d221 20 74 6f 20 73 71 6c 69 74 65 33 5f 66 72 65 65   to sqlite3_free
1d222 5f 74 61 62 6c 65 28 29 20 77 68 65 6e 0a 2a 2a  _table() when.**
1d223 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f   the calling pro
1d224 63 65 64 75 72 65 20 69 73 20 66 69 6e 69 73 68  cedure is finish
1d225 65 64 20 75 73 69 6e 67 20 69 74 2e 0a 2a 2f 0a  ed using it..*/.
1d226 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 65 74 5f  int sqlite3_get_
1d227 74 61 62 6c 65 28 0a 20 20 73 71 6c 69 74 65 33  table(.  sqlite3
1d228 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
1d229 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
1d22a 62 61 73 65 20 6f 6e 20 77 68 69 63 68 20 74 68  base on which th
1d22b 65 20 53 51 4c 20 65 78 65 63 75 74 65 73 20 2a  e SQL executes *
1d22c 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
1d22d 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 20 20 20  zSql,           
1d22e 2f 2a 20 54 68 65 20 53 51 4c 20 74 6f 20 62 65  /* The SQL to be
1d22f 20 65 78 65 63 75 74 65 64 20 2a 2f 0a 20 20 63   executed */.  c
1d230 68 61 72 20 2a 2a 2a 70 61 7a 52 65 73 75 6c 74  har ***pazResult
1d231 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72  ,          /* Wr
1d232 69 74 65 20 74 68 65 20 72 65 73 75 6c 74 20 74  ite the result t
1d233 61 62 6c 65 20 68 65 72 65 20 2a 2f 0a 20 20 69  able here */.  i
1d234 6e 74 20 2a 70 6e 52 6f 77 2c 20 20 20 20 20 20  nt *pnRow,      
1d235 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72             /* Wr
1d236 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ite the number o
1d237 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20 72 65  f rows in the re
1d238 73 75 6c 74 20 68 65 72 65 20 2a 2f 0a 20 20 69  sult here */.  i
1d239 6e 74 20 2a 70 6e 43 6f 6c 75 6d 6e 2c 20 20 20  nt *pnColumn,   
1d23a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72             /* Wr
1d23b 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ite the number o
1d23c 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 72 65 73  f columns of res
1d23d 75 6c 74 20 68 65 72 65 20 2a 2f 0a 20 20 63 68  ult here */.  ch
1d23e 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20  ar **pzErrMsg   
1d23f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
1d240 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  te error message
1d241 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69  s here */.){.  i
1d242 6e 74 20 72 63 3b 0a 20 20 54 61 62 52 65 73 75  nt rc;.  TabResu
1d243 6c 74 20 72 65 73 3b 0a 20 20 69 66 28 20 70 61  lt res;.  if( pa
1d244 7a 52 65 73 75 6c 74 3d 3d 30 20 29 7b 20 72 65  zResult==0 ){ re
1d245 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
1d246 52 3b 20 7d 0a 20 20 2a 70 61 7a 52 65 73 75 6c  R; }.  *pazResul
1d247 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 6e 43  t = 0;.  if( pnC
1d248 6f 6c 75 6d 6e 20 29 20 2a 70 6e 43 6f 6c 75 6d  olumn ) *pnColum
1d249 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 6e 52  n = 0;.  if( pnR
1d24a 6f 77 20 29 20 2a 70 6e 52 6f 77 20 3d 20 30 3b  ow ) *pnRow = 0;
1d24b 0a 20 20 72 65 73 2e 7a 45 72 72 4d 73 67 20 3d  .  res.zErrMsg =
1d24c 20 30 3b 0a 20 20 72 65 73 2e 6e 52 65 73 75 6c   0;.  res.nResul
1d24d 74 20 3d 20 30 3b 0a 20 20 72 65 73 2e 6e 52 6f  t = 0;.  res.nRo
1d24e 77 20 3d 20 30 3b 0a 20 20 72 65 73 2e 6e 43 6f  w = 0;.  res.nCo
1d24f 6c 75 6d 6e 20 3d 20 30 3b 0a 20 20 72 65 73 2e  lumn = 0;.  res.
1d250 6e 44 61 74 61 20 3d 20 31 3b 0a 20 20 72 65 73  nData = 1;.  res
1d251 2e 6e 41 6c 6c 6f 63 20 3d 20 32 30 3b 0a 20 20  .nAlloc = 20;.  
1d252 72 65 73 2e 72 63 20 3d 20 53 51 4c 49 54 45 5f  res.rc = SQLITE_
1d253 4f 4b 3b 0a 20 20 72 65 73 2e 61 7a 52 65 73 75  OK;.  res.azResu
1d254 6c 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  lt = sqlite3_mal
1d255 6c 6f 63 28 20 73 69 7a 65 6f 66 28 63 68 61 72  loc( sizeof(char
1d256 2a 29 2a 72 65 73 2e 6e 41 6c 6c 6f 63 20 29 3b  *)*res.nAlloc );
1d257 0a 20 20 69 66 28 20 72 65 73 2e 61 7a 52 65 73  .  if( res.azRes
1d258 75 6c 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ult==0 ) return 
1d259 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
1d25a 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b 30 5d 20  res.azResult[0] 
1d25b 3d 20 30 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  = 0;.  rc = sqli
1d25c 74 65 33 5f 65 78 65 63 28 64 62 2c 20 7a 53 71  te3_exec(db, zSq
1d25d 6c 2c 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74  l, sqlite3_get_t
1d25e 61 62 6c 65 5f 63 62 2c 20 26 72 65 73 2c 20 70  able_cb, &res, p
1d25f 7a 45 72 72 4d 73 67 29 3b 0a 20 20 69 66 28 20  zErrMsg);.  if( 
1d260 72 65 73 2e 61 7a 52 65 73 75 6c 74 20 29 7b 0a  res.azResult ){.
1d261 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65      assert( size
1d262 6f 66 28 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b  of(res.azResult[
1d263 30 5d 29 3e 3d 20 73 69 7a 65 6f 66 28 72 65 73  0])>= sizeof(res
1d264 2e 6e 44 61 74 61 29 20 29 3b 0a 20 20 20 20 72  .nData) );.    r
1d265 65 73 2e 61 7a 52 65 73 75 6c 74 5b 30 5d 20 3d  es.azResult[0] =
1d266 20 28 63 68 61 72 2a 29 72 65 73 2e 6e 44 61 74   (char*)res.nDat
1d267 61 3b 0a 20 20 7d 0a 20 20 69 66 28 20 28 72 63  a;.  }.  if( (rc
1d268 26 30 78 66 66 29 3d 3d 53 51 4c 49 54 45 5f 41  &0xff)==SQLITE_A
1d269 42 4f 52 54 20 29 7b 0a 20 20 20 20 73 71 6c 69  BORT ){.    sqli
1d26a 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 26  te3_free_table(&
1d26b 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b 31 5d 29  res.azResult[1])
1d26c 3b 0a 20 20 20 20 69 66 28 20 72 65 73 2e 7a 45  ;.    if( res.zE
1d26d 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 69  rrMsg ){.      i
1d26e 66 28 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20  f( pzErrMsg ){. 
1d26f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66         sqlite3_f
1d270 72 65 65 28 2a 70 7a 45 72 72 4d 73 67 29 3b 0a  ree(*pzErrMsg);.
1d271 20 20 20 20 20 20 20 20 2a 70 7a 45 72 72 4d 73          *pzErrMs
1d272 67 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  g = sqlite3_mpri
1d273 6e 74 66 28 22 25 73 22 2c 72 65 73 2e 7a 45 72  ntf("%s",res.zEr
1d274 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20  rMsg);.      }. 
1d275 20 20 20 20 20 73 71 6c 69 74 65 46 72 65 65 28       sqliteFree(
1d276 72 65 73 2e 7a 45 72 72 4d 73 67 29 3b 0a 20 20  res.zErrMsg);.  
1d277 20 20 7d 0a 20 20 20 20 64 62 2d 3e 65 72 72 43    }.    db->errC
1d278 6f 64 65 20 3d 20 72 65 73 2e 72 63 3b 0a 20 20  ode = res.rc;.  
1d279 20 20 72 65 74 75 72 6e 20 72 65 73 2e 72 63 20    return res.rc 
1d27a 26 20 64 62 2d 3e 65 72 72 4d 61 73 6b 3b 0a 20  & db->errMask;. 
1d27b 20 7d 0a 20 20 73 71 6c 69 74 65 46 72 65 65 28   }.  sqliteFree(
1d27c 72 65 73 2e 7a 45 72 72 4d 73 67 29 3b 0a 20 20  res.zErrMsg);.  
1d27d 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1d27e 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  K ){.    sqlite3
1d27f 5f 66 72 65 65 5f 74 61 62 6c 65 28 26 72 65 73  _free_table(&res
1d280 2e 61 7a 52 65 73 75 6c 74 5b 31 5d 29 3b 0a 20  .azResult[1]);. 
1d281 20 20 20 72 65 74 75 72 6e 20 72 63 20 26 20 64     return rc & d
1d282 62 2d 3e 65 72 72 4d 61 73 6b 3b 0a 20 20 7d 0a  b->errMask;.  }.
1d283 20 20 69 66 28 20 72 65 73 2e 6e 41 6c 6c 6f 63    if( res.nAlloc
1d284 3e 72 65 73 2e 6e 44 61 74 61 20 29 7b 0a 20 20  >res.nData ){.  
1d285 20 20 63 68 61 72 20 2a 2a 61 7a 4e 65 77 3b 0a    char **azNew;.
1d286 20 20 20 20 61 7a 4e 65 77 20 3d 20 73 71 6c 69      azNew = sqli
1d287 74 65 33 5f 72 65 61 6c 6c 6f 63 28 20 72 65 73  te3_realloc( res
1d288 2e 61 7a 52 65 73 75 6c 74 2c 20 73 69 7a 65 6f  .azResult, sizeo
1d289 66 28 63 68 61 72 2a 29 2a 28 72 65 73 2e 6e 44  f(char*)*(res.nD
1d28a 61 74 61 2b 31 29 20 29 3b 0a 20 20 20 20 69 66  ata+1) );.    if
1d28b 28 20 61 7a 4e 65 77 3d 3d 30 20 29 7b 0a 20 20  ( azNew==0 ){.  
1d28c 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
1d28d 5f 74 61 62 6c 65 28 26 72 65 73 2e 61 7a 52 65  _table(&res.azRe
1d28e 73 75 6c 74 5b 31 5d 29 3b 0a 20 20 20 20 20 20  sult[1]);.      
1d28f 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
1d290 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  MEM;.    }.    r
1d291 65 73 2e 6e 41 6c 6c 6f 63 20 3d 20 72 65 73 2e  es.nAlloc = res.
1d292 6e 44 61 74 61 2b 31 3b 0a 20 20 20 20 72 65 73  nData+1;.    res
1d293 2e 61 7a 52 65 73 75 6c 74 20 3d 20 61 7a 4e 65  .azResult = azNe
1d294 77 3b 0a 20 20 7d 0a 20 20 2a 70 61 7a 52 65 73  w;.  }.  *pazRes
1d295 75 6c 74 20 3d 20 26 72 65 73 2e 61 7a 52 65 73  ult = &res.azRes
1d296 75 6c 74 5b 31 5d 3b 0a 20 20 69 66 28 20 70 6e  ult[1];.  if( pn
1d297 43 6f 6c 75 6d 6e 20 29 20 2a 70 6e 43 6f 6c 75  Column ) *pnColu
1d298 6d 6e 20 3d 20 72 65 73 2e 6e 43 6f 6c 75 6d 6e  mn = res.nColumn
1d299 3b 0a 20 20 69 66 28 20 70 6e 52 6f 77 20 29 20  ;.  if( pnRow ) 
1d29a 2a 70 6e 52 6f 77 20 3d 20 72 65 73 2e 6e 52 6f  *pnRow = res.nRo
1d29b 77 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 20 26  w;.  return rc &
1d29c 20 64 62 2d 3e 65 72 72 4d 61 73 6b 3b 0a 7d 0a   db->errMask;.}.
1d29d 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
1d29e 69 6e 65 20 66 72 65 65 73 20 74 68 65 20 73 70  ine frees the sp
1d29f 61 63 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f  ace the sqlite3_
1d2a0 67 65 74 5f 74 61 62 6c 65 28 29 20 6d 61 6c 6c  get_table() mall
1d2a1 6f 63 65 64 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71  oced..*/.void sq
1d2a2 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65  lite3_free_table
1d2a3 28 0a 20 20 63 68 61 72 20 2a 2a 61 7a 52 65 73  (.  char **azRes
1d2a4 75 6c 74 20 20 20 20 20 20 20 20 20 20 20 20 2f  ult            /
1d2a5 2a 20 52 65 73 75 6c 74 20 72 65 74 75 72 6e 65  * Result returne
1d2a6 64 20 66 72 6f 6d 20 66 72 6f 6d 20 73 71 6c 69  d from from sqli
1d2a7 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20  te3_get_table() 
1d2a8 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 61 7a 52 65  */.){.  if( azRe
1d2a9 73 75 6c 74 20 29 7b 0a 20 20 20 20 69 6e 74 20  sult ){.    int 
1d2aa 69 2c 20 6e 3b 0a 20 20 20 20 61 7a 52 65 73 75  i, n;.    azResu
1d2ab 6c 74 2d 2d 3b 0a 20 20 20 20 69 66 28 20 61 7a  lt--;.    if( az
1d2ac 52 65 73 75 6c 74 3d 3d 30 20 29 20 72 65 74 75  Result==0 ) retu
1d2ad 72 6e 3b 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74  rn;.    n = (int
1d2ae 29 61 7a 52 65 73 75 6c 74 5b 30 5d 3b 0a 20 20  )azResult[0];.  
1d2af 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 6e 3b 20    for(i=1; i<n; 
1d2b0 69 2b 2b 29 7b 20 69 66 28 20 61 7a 52 65 73 75  i++){ if( azResu
1d2b1 6c 74 5b 69 5d 20 29 20 73 71 6c 69 74 65 33 5f  lt[i] ) sqlite3_
1d2b2 66 72 65 65 28 61 7a 52 65 73 75 6c 74 5b 69 5d  free(azResult[i]
1d2b3 29 3b 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33  ); }.    sqlite3
1d2b4 5f 66 72 65 65 28 61 7a 52 65 73 75 6c 74 29 3b  _free(azResult);
1d2b5 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f  .  }.}..#endif /
1d2b6 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 47 45  * SQLITE_OMIT_GE
1d2b7 54 5f 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 2a 2a  T_TABLE */../***
1d2b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
1d2b9 6f 66 20 74 61 62 6c 65 2e 63 20 2a 2a 2a 2a 2a  of table.c *****
1d2ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d2bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d2bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
1d2bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
1d2be 6e 20 66 69 6c 65 20 74 72 69 67 67 65 72 2e 63  n file trigger.c
1d2bf 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1d2c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d2c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
1d2c2 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
1d2c3 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
1d2c4 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
1d2c5 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
1d2c6 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
1d2c7 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
1d2c8 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
1d2c9 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
1d2ca 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
1d2cb 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
1d2cc 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
1d2cd 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
1d2ce 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
1d2cf 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
1d2d0 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
1d2d1 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
1d2d2 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
1d2d3 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
1d2d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d2d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d2d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d2d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 0a  *************.*.
1d2d8 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
1d2d9 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a  TE_OMIT_TRIGGER.
1d2da 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 20 6c  /*.** Delete a l
1d2db 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 54 72  inked list of Tr
1d2dc 69 67 67 65 72 53 74 65 70 20 73 74 72 75 63 74  iggerStep struct
1d2dd 75 72 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ures..*/.SQLITE_
1d2de 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1d2df 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65  ite3DeleteTrigge
1d2e0 72 53 74 65 70 28 54 72 69 67 67 65 72 53 74 65  rStep(TriggerSte
1d2e1 70 20 2a 70 54 72 69 67 67 65 72 53 74 65 70 29  p *pTriggerStep)
1d2e2 7b 0a 20 20 77 68 69 6c 65 28 20 70 54 72 69 67  {.  while( pTrig
1d2e3 67 65 72 53 74 65 70 20 29 7b 0a 20 20 20 20 54  gerStep ){.    T
1d2e4 72 69 67 67 65 72 53 74 65 70 20 2a 20 70 54 6d  riggerStep * pTm
1d2e5 70 20 3d 20 70 54 72 69 67 67 65 72 53 74 65 70  p = pTriggerStep
1d2e6 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72 53 74  ;.    pTriggerSt
1d2e7 65 70 20 3d 20 70 54 72 69 67 67 65 72 53 74 65  ep = pTriggerSte
1d2e8 70 2d 3e 70 4e 65 78 74 3b 0a 0a 20 20 20 20 69  p->pNext;..    i
1d2e9 66 28 20 70 54 6d 70 2d 3e 74 61 72 67 65 74 2e  f( pTmp->target.
1d2ea 64 79 6e 20 29 20 73 71 6c 69 74 65 46 72 65 65  dyn ) sqliteFree
1d2eb 28 28 63 68 61 72 2a 29 70 54 6d 70 2d 3e 74 61  ((char*)pTmp->ta
1d2ec 72 67 65 74 2e 7a 29 3b 0a 20 20 20 20 73 71 6c  rget.z);.    sql
1d2ed 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 70  ite3ExprDelete(p
1d2ee 54 6d 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20  Tmp->pWhere);.  
1d2ef 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
1d2f0 74 44 65 6c 65 74 65 28 70 54 6d 70 2d 3e 70 45  tDelete(pTmp->pE
1d2f1 78 70 72 4c 69 73 74 29 3b 0a 20 20 20 20 73 71  xprList);.    sq
1d2f2 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74  lite3SelectDelet
1d2f3 65 28 70 54 6d 70 2d 3e 70 53 65 6c 65 63 74 29  e(pTmp->pSelect)
1d2f4 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 49 64 4c  ;.    sqlite3IdL
1d2f5 69 73 74 44 65 6c 65 74 65 28 70 54 6d 70 2d 3e  istDelete(pTmp->
1d2f6 70 49 64 4c 69 73 74 29 3b 0a 0a 20 20 20 20 73  pIdList);..    s
1d2f7 71 6c 69 74 65 46 72 65 65 28 70 54 6d 70 29 3b  qliteFree(pTmp);
1d2f8 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
1d2f9 69 73 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20  is is called by 
1d2fa 74 68 65 20 70 61 72 73 65 72 20 77 68 65 6e 20  the parser when 
1d2fb 69 74 20 73 65 65 73 20 61 20 43 52 45 41 54 45  it sees a CREATE
1d2fc 20 54 52 49 47 47 45 52 20 73 74 61 74 65 6d 65   TRIGGER stateme
1d2fd 6e 74 0a 2a 2a 20 75 70 20 74 6f 20 74 68 65 20  nt.** up to the 
1d2fe 70 6f 69 6e 74 20 6f 66 20 74 68 65 20 42 45 47  point of the BEG
1d2ff 49 4e 20 62 65 66 6f 72 65 20 74 68 65 20 74 72  IN before the tr
1d300 69 67 67 65 72 20 61 63 74 69 6f 6e 73 2e 20 20  igger actions.  
1d301 41 20 54 72 69 67 67 65 72 0a 2a 2a 20 73 74 72  A Trigger.** str
1d302 75 63 74 75 72 65 20 69 73 20 67 65 6e 65 72 61  ucture is genera
1d303 74 65 64 20 62 61 73 65 64 20 6f 6e 20 74 68 65  ted based on the
1d304 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 76 61   information ava
1d305 69 6c 61 62 6c 65 20 61 6e 64 20 73 74 6f 72 65  ilable and store
1d306 64 0a 2a 2a 20 69 6e 20 70 50 61 72 73 65 2d 3e  d.** in pParse->
1d307 70 4e 65 77 54 72 69 67 67 65 72 2e 20 20 41 66  pNewTrigger.  Af
1d308 74 65 72 20 74 68 65 20 74 72 69 67 67 65 72 20  ter the trigger 
1d309 61 63 74 69 6f 6e 73 20 68 61 76 65 20 62 65 65  actions have bee
1d30a 6e 20 70 61 72 73 65 64 2c 20 74 68 65 0a 2a 2a  n parsed, the.**
1d30b 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68 54 72   sqlite3FinishTr
1d30c 69 67 67 65 72 28 29 20 66 75 6e 63 74 69 6f 6e  igger() function
1d30d 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f   is called to co
1d30e 6d 70 6c 65 74 65 20 74 68 65 20 74 72 69 67 67  mplete the trigg
1d30f 65 72 0a 2a 2a 20 63 6f 6e 73 74 72 75 63 74 69  er.** constructi
1d310 6f 6e 20 70 72 6f 63 65 73 73 2e 0a 2a 2f 0a 53  on process..*/.S
1d311 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1d312 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 54  id sqlite3BeginT
1d313 72 69 67 67 65 72 28 0a 20 20 50 61 72 73 65 20  rigger(.  Parse 
1d314 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a  *pParse,      /*
1d315 20 54 68 65 20 70 61 72 73 65 20 63 6f 6e 74 65   The parse conte
1d316 78 74 20 6f 66 20 74 68 65 20 43 52 45 41 54 45  xt of the CREATE
1d317 20 54 52 49 47 47 45 52 20 73 74 61 74 65 6d 65   TRIGGER stateme
1d318 6e 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  nt */.  Token *p
1d319 4e 61 6d 65 31 2c 20 20 20 20 20 20 2f 2a 20 54  Name1,      /* T
1d31a 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74  he name of the t
1d31b 72 69 67 67 65 72 20 2a 2f 0a 20 20 54 6f 6b 65  rigger */.  Toke
1d31c 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20 20  n *pName2,      
1d31d 2f 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74  /* The name of t
1d31e 68 65 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20  he trigger */.  
1d31f 69 6e 74 20 74 72 5f 74 6d 2c 20 20 20 20 20 20  int tr_tm,      
1d320 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b      /* One of TK
1d321 5f 42 45 46 4f 52 45 2c 20 54 4b 5f 41 46 54 45  _BEFORE, TK_AFTE
1d322 52 2c 20 54 4b 5f 49 4e 53 54 45 41 44 20 2a 2f  R, TK_INSTEAD */
1d323 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20  .  int op,      
1d324 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66         /* One of
1d325 20 54 4b 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 55   TK_INSERT, TK_U
1d326 50 44 41 54 45 2c 20 54 4b 5f 44 45 4c 45 54 45  PDATE, TK_DELETE
1d327 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 43   */.  IdList *pC
1d328 6f 6c 75 6d 6e 73 2c 20 20 20 2f 2a 20 63 6f 6c  olumns,   /* col
1d329 75 6d 6e 20 6c 69 73 74 20 69 66 20 74 68 69 73  umn list if this
1d32a 20 69 73 20 61 6e 20 55 50 44 41 54 45 20 4f 46   is an UPDATE OF
1d32b 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 53 72   trigger */.  Sr
1d32c 63 4c 69 73 74 20 2a 70 54 61 62 6c 65 4e 61 6d  cList *pTableNam
1d32d 65 2c 2f 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66  e,/* The name of
1d32e 20 74 68 65 20 74 61 62 6c 65 2f 76 69 65 77 20   the table/view 
1d32f 74 68 65 20 74 72 69 67 67 65 72 20 61 70 70 6c  the trigger appl
1d330 69 65 73 20 74 6f 20 2a 2f 0a 20 20 45 78 70 72  ies to */.  Expr
1d331 20 2a 70 57 68 65 6e 2c 20 20 20 20 20 20 20 20   *pWhen,        
1d332 2f 2a 20 57 48 45 4e 20 63 6c 61 75 73 65 20 2a  /* WHEN clause *
1d333 2f 0a 20 20 69 6e 74 20 69 73 54 65 6d 70 2c 20  /.  int isTemp, 
1d334 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
1d335 69 66 20 74 68 65 20 54 45 4d 50 4f 52 41 52 59  if the TEMPORARY
1d336 20 6b 65 79 77 6f 72 64 20 69 73 20 70 72 65 73   keyword is pres
1d337 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 45  ent */.  int noE
1d338 72 72 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  rr           /* 
1d339 53 75 70 70 72 65 73 73 20 65 72 72 6f 72 73 20  Suppress errors 
1d33a 69 66 20 74 68 65 20 74 72 69 67 67 65 72 20 61  if the trigger a
1d33b 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 2a 2f  lready exists */
1d33c 0a 29 7b 0a 20 20 54 72 69 67 67 65 72 20 2a 70  .){.  Trigger *p
1d33d 54 72 69 67 67 65 72 20 3d 20 30 3b 0a 20 20 54  Trigger = 0;.  T
1d33e 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 63 68  able *pTab;.  ch
1d33f 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 20 20  ar *zName = 0;  
1d340 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
1d341 20 74 68 65 20 74 72 69 67 67 65 72 20 2a 2f 0a   the trigger */.
1d342 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
1d343 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e  pParse->db;.  in
1d344 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20  t iDb;          
1d345 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
1d346 61 62 61 73 65 20 74 6f 20 73 74 6f 72 65 20 74  abase to store t
1d347 68 65 20 74 72 69 67 67 65 72 20 69 6e 20 2a 2f  he trigger in */
1d348 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 3b  .  Token *pName;
1d349 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1d34a 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20 64 62  e unqualified db
1d34b 20 6e 61 6d 65 20 2a 2f 0a 20 20 44 62 46 69 78   name */.  DbFix
1d34c 65 72 20 73 46 69 78 3b 0a 20 20 69 6e 74 20 69  er sFix;.  int i
1d34d 54 61 62 44 62 3b 0a 0a 20 20 61 73 73 65 72 74  TabDb;..  assert
1d34e 28 20 70 4e 61 6d 65 31 21 3d 30 20 29 3b 20 20  ( pName1!=0 );  
1d34f 20 2f 2a 20 70 4e 61 6d 65 31 2d 3e 7a 20 6d 69   /* pName1->z mi
1d350 67 68 74 20 62 65 20 4e 55 4c 4c 2c 20 62 75 74  ght be NULL, but
1d351 20 6e 6f 74 20 70 4e 61 6d 65 31 20 69 74 73 65   not pName1 itse
1d352 6c 66 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  lf */.  assert( 
1d353 70 4e 61 6d 65 32 21 3d 30 20 29 3b 0a 20 20 69  pName2!=0 );.  i
1d354 66 28 20 69 73 54 65 6d 70 20 29 7b 0a 20 20 20  f( isTemp ){.   
1d355 20 2f 2a 20 49 66 20 54 45 4d 50 20 77 61 73 20   /* If TEMP was 
1d356 73 70 65 63 69 66 69 65 64 2c 20 74 68 65 6e 20  specified, then 
1d357 74 68 65 20 74 72 69 67 67 65 72 20 6e 61 6d 65  the trigger name
1d358 20 6d 61 79 20 6e 6f 74 20 62 65 20 71 75 61 6c   may not be qual
1d359 69 66 69 65 64 2e 20 2a 2f 0a 20 20 20 20 69 66  ified. */.    if
1d35a 28 20 70 4e 61 6d 65 32 2d 3e 6e 3e 30 20 29 7b  ( pName2->n>0 ){
1d35b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
1d35c 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
1d35d 74 65 6d 70 6f 72 61 72 79 20 74 72 69 67 67 65  temporary trigge
1d35e 72 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 71  r may not have q
1d35f 75 61 6c 69 66 69 65 64 20 6e 61 6d 65 22 29 3b  ualified name");
1d360 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 72 69 67  .      goto trig
1d361 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20  ger_cleanup;.   
1d362 20 7d 0a 20 20 20 20 69 44 62 20 3d 20 31 3b 0a   }.    iDb = 1;.
1d363 20 20 20 20 70 4e 61 6d 65 20 3d 20 70 4e 61 6d      pName = pNam
1d364 65 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  e1;.  }else{.   
1d365 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 74   /* Figure out t
1d366 68 65 20 64 62 20 74 68 61 74 20 74 68 65 20 74  he db that the t
1d367 68 65 20 74 72 69 67 67 65 72 20 77 69 6c 6c 20  he trigger will 
1d368 62 65 20 63 72 65 61 74 65 64 20 69 6e 20 2a 2f  be created in */
1d369 0a 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74  .    iDb = sqlit
1d36a 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 70 50  e3TwoPartName(pP
1d36b 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e  arse, pName1, pN
1d36c 61 6d 65 32 2c 20 26 70 4e 61 6d 65 29 3b 0a 20  ame2, &pName);. 
1d36d 20 20 20 69 66 28 20 69 44 62 3c 30 20 29 7b 0a     if( iDb<0 ){.
1d36e 20 20 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67        goto trigg
1d36f 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20  er_cleanup;.    
1d370 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  }.  }..  /* If t
1d371 68 65 20 74 72 69 67 67 65 72 20 6e 61 6d 65 20  he trigger name 
1d372 77 61 73 20 75 6e 71 75 61 6c 69 66 69 65 64 2c  was unqualified,
1d373 20 61 6e 64 20 74 68 65 20 74 61 62 6c 65 20 69   and the table i
1d374 73 20 61 20 74 65 6d 70 20 74 61 62 6c 65 2c 0a  s a temp table,.
1d375 20 20 2a 2a 20 74 68 65 6e 20 73 65 74 20 69 44    ** then set iD
1d376 62 20 74 6f 20 31 20 74 6f 20 63 72 65 61 74 65  b to 1 to create
1d377 20 74 68 65 20 74 72 69 67 67 65 72 20 69 6e 20   the trigger in 
1d378 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61  the temporary da
1d379 74 61 62 61 73 65 2e 0a 20 20 2a 2a 20 49 66 20  tabase..  ** If 
1d37a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 4c 6f  sqlite3SrcListLo
1d37b 6f 6b 75 70 28 29 20 72 65 74 75 72 6e 73 20 30  okup() returns 0
1d37c 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 65  , indicating the
1d37d 20 74 61 62 6c 65 20 64 6f 65 73 20 6e 6f 74 0a   table does not.
1d37e 20 20 2a 2a 20 65 78 69 73 74 2c 20 74 68 65 20    ** exist, the 
1d37f 65 72 72 6f 72 20 69 73 20 63 61 75 67 68 74 20  error is caught 
1d380 62 79 20 74 68 65 20 62 6c 6f 63 6b 20 62 65 6c  by the block bel
1d381 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  ow..  */.  if( !
1d382 70 54 61 62 6c 65 4e 61 6d 65 20 7c 7c 20 73 71  pTableName || sq
1d383 6c 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69 6c 65  lite3MallocFaile
1d384 64 28 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  d() ){.    goto 
1d385 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b  trigger_cleanup;
1d386 0a 20 20 7d 0a 20 20 70 54 61 62 20 3d 20 73 71  .  }.  pTab = sq
1d387 6c 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b  lite3SrcListLook
1d388 75 70 28 70 50 61 72 73 65 2c 20 70 54 61 62 6c  up(pParse, pTabl
1d389 65 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 70 4e  eName);.  if( pN
1d38a 61 6d 65 32 2d 3e 6e 3d 3d 30 20 26 26 20 70 54  ame2->n==0 && pT
1d38b 61 62 20 26 26 20 70 54 61 62 2d 3e 70 53 63 68  ab && pTab->pSch
1d38c 65 6d 61 3d 3d 64 62 2d 3e 61 44 62 5b 31 5d 2e  ema==db->aDb[1].
1d38d 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 69  pSchema ){.    i
1d38e 44 62 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f  Db = 1;.  }..  /
1d38f 2a 20 45 6e 73 75 72 65 20 74 68 65 20 74 61 62  * Ensure the tab
1d390 6c 65 20 6e 61 6d 65 20 6d 61 74 63 68 65 73 20  le name matches 
1d391 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 61 6e  database name an
1d392 64 20 74 68 61 74 20 74 68 65 20 74 61 62 6c 65  d that the table
1d393 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 69 66 28   exists */.  if(
1d394 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 46 61   sqlite3MallocFa
1d395 69 6c 65 64 28 29 20 29 20 67 6f 74 6f 20 74 72  iled() ) goto tr
1d396 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20  igger_cleanup;. 
1d397 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 4e   assert( pTableN
1d398 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a  ame->nSrc==1 );.
1d399 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78    if( sqlite3Fix
1d39a 49 6e 69 74 28 26 73 46 69 78 2c 20 70 50 61 72  Init(&sFix, pPar
1d39b 73 65 2c 20 69 44 62 2c 20 22 74 72 69 67 67 65  se, iDb, "trigge
1d39c 72 22 2c 20 70 4e 61 6d 65 29 20 26 26 20 0a 20  r", pName) && . 
1d39d 20 20 20 20 20 73 71 6c 69 74 65 33 46 69 78 53       sqlite3FixS
1d39e 72 63 4c 69 73 74 28 26 73 46 69 78 2c 20 70 54  rcList(&sFix, pT
1d39f 61 62 6c 65 4e 61 6d 65 29 20 29 7b 0a 20 20 20  ableName) ){.   
1d3a0 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c   goto trigger_cl
1d3a1 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 70 54 61  eanup;.  }.  pTa
1d3a2 62 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69  b = sqlite3SrcLi
1d3a3 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c  stLookup(pParse,
1d3a4 20 70 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20 20   pTableName);.  
1d3a5 69 66 28 20 21 70 54 61 62 20 29 7b 0a 20 20 20  if( !pTab ){.   
1d3a6 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 64 6f   /* The table do
1d3a7 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 20 2a 2f  es not exist. */
1d3a8 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65  .    goto trigge
1d3a9 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20  r_cleanup;.  }. 
1d3aa 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70   if( IsVirtual(p
1d3ab 54 61 62 29 20 29 7b 0a 20 20 20 20 73 71 6c 69  Tab) ){.    sqli
1d3ac 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1d3ad 73 65 2c 20 22 63 61 6e 6e 6f 74 20 63 72 65 61  se, "cannot crea
1d3ae 74 65 20 74 72 69 67 67 65 72 73 20 6f 6e 20 76  te triggers on v
1d3af 69 72 74 75 61 6c 20 74 61 62 6c 65 73 22 29 3b  irtual tables");
1d3b0 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65  .    goto trigge
1d3b1 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a  r_cleanup;.  }..
1d3b2 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20    /* Check that 
1d3b3 74 68 65 20 74 72 69 67 67 65 72 20 6e 61 6d 65  the trigger name
1d3b4 20 69 73 20 6e 6f 74 20 72 65 73 65 72 76 65 64   is not reserved
1d3b5 20 61 6e 64 20 74 68 61 74 20 6e 6f 20 74 72 69   and that no tri
1d3b6 67 67 65 72 20 6f 66 20 74 68 65 0a 20 20 2a 2a  gger of the.  **
1d3b7 20 73 70 65 63 69 66 69 65 64 20 6e 61 6d 65 20   specified name 
1d3b8 65 78 69 73 74 73 20 2a 2f 0a 20 20 7a 4e 61 6d  exists */.  zNam
1d3b9 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  e = sqlite3NameF
1d3ba 72 6f 6d 54 6f 6b 65 6e 28 70 4e 61 6d 65 29 3b  romToken(pName);
1d3bb 0a 20 20 69 66 28 20 21 7a 4e 61 6d 65 20 7c 7c  .  if( !zName ||
1d3bc 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69   SQLITE_OK!=sqli
1d3bd 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61  te3CheckObjectNa
1d3be 6d 65 28 70 50 61 72 73 65 2c 20 7a 4e 61 6d 65  me(pParse, zName
1d3bf 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 72  ) ){.    goto tr
1d3c0 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20  igger_cleanup;. 
1d3c1 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33   }.  if( sqlite3
1d3c2 48 61 73 68 46 69 6e 64 28 26 28 64 62 2d 3e 61  HashFind(&(db->a
1d3c3 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d  Db[iDb].pSchema-
1d3c4 3e 74 72 69 67 48 61 73 68 29 2c 20 7a 4e 61 6d  >trigHash), zNam
1d3c5 65 2c 73 74 72 6c 65 6e 28 7a 4e 61 6d 65 29 29  e,strlen(zName))
1d3c6 20 29 7b 0a 20 20 20 20 69 66 28 20 21 6e 6f 45   ){.    if( !noE
1d3c7 72 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  rr ){.      sqli
1d3c8 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1d3c9 73 65 2c 20 22 74 72 69 67 67 65 72 20 25 54 20  se, "trigger %T 
1d3ca 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 22 2c  already exists",
1d3cb 20 70 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20   pName);.    }. 
1d3cc 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f     goto trigger_
1d3cd 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20  cleanup;.  }..  
1d3ce 2f 2a 20 44 6f 20 6e 6f 74 20 63 72 65 61 74 65  /* Do not create
1d3cf 20 61 20 74 72 69 67 67 65 72 20 6f 6e 20 61 20   a trigger on a 
1d3d0 73 79 73 74 65 6d 20 74 61 62 6c 65 20 2a 2f 0a  system table */.
1d3d1 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
1d3d2 4e 49 43 6d 70 28 70 54 61 62 2d 3e 7a 4e 61 6d  NICmp(pTab->zNam
1d3d3 65 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20 37 29  e, "sqlite_", 7)
1d3d4 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
1d3d5 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
1d3d6 65 2c 20 22 63 61 6e 6e 6f 74 20 63 72 65 61 74  e, "cannot creat
1d3d7 65 20 74 72 69 67 67 65 72 20 6f 6e 20 73 79 73  e trigger on sys
1d3d8 74 65 6d 20 74 61 62 6c 65 22 29 3b 0a 20 20 20  tem table");.   
1d3d9 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b   pParse->nErr++;
1d3da 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65  .    goto trigge
1d3db 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a  r_cleanup;.  }..
1d3dc 20 20 2f 2a 20 49 4e 53 54 45 41 44 20 6f 66 20    /* INSTEAD of 
1d3dd 74 72 69 67 67 65 72 73 20 61 72 65 20 6f 6e 6c  triggers are onl
1d3de 79 20 66 6f 72 20 76 69 65 77 73 20 61 6e 64 20  y for views and 
1d3df 76 69 65 77 73 20 6f 6e 6c 79 20 73 75 70 70 6f  views only suppo
1d3e0 72 74 20 49 4e 53 54 45 41 44 0a 20 20 2a 2a 20  rt INSTEAD.  ** 
1d3e1 6f 66 20 74 72 69 67 67 65 72 73 2e 0a 20 20 2a  of triggers..  *
1d3e2 2f 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53  /.  if( pTab->pS
1d3e3 65 6c 65 63 74 20 26 26 20 74 72 5f 74 6d 21 3d  elect && tr_tm!=
1d3e4 54 4b 5f 49 4e 53 54 45 41 44 20 29 7b 0a 20 20  TK_INSTEAD ){.  
1d3e5 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
1d3e6 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f  g(pParse, "canno
1d3e7 74 20 63 72 65 61 74 65 20 25 73 20 74 72 69 67  t create %s trig
1d3e8 67 65 72 20 6f 6e 20 76 69 65 77 3a 20 25 53 22  ger on view: %S"
1d3e9 2c 20 0a 20 20 20 20 20 20 20 20 28 74 72 5f 74  , .        (tr_t
1d3ea 6d 20 3d 3d 20 54 4b 5f 42 45 46 4f 52 45 29 3f  m == TK_BEFORE)?
1d3eb 22 42 45 46 4f 52 45 22 3a 22 41 46 54 45 52 22  "BEFORE":"AFTER"
1d3ec 2c 20 70 54 61 62 6c 65 4e 61 6d 65 2c 20 30 29  , pTableName, 0)
1d3ed 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67  ;.    goto trigg
1d3ee 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a  er_cleanup;.  }.
1d3ef 20 20 69 66 28 20 21 70 54 61 62 2d 3e 70 53 65    if( !pTab->pSe
1d3f0 6c 65 63 74 20 26 26 20 74 72 5f 74 6d 3d 3d 54  lect && tr_tm==T
1d3f1 4b 5f 49 4e 53 54 45 41 44 20 29 7b 0a 20 20 20  K_INSTEAD ){.   
1d3f2 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
1d3f3 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74  (pParse, "cannot
1d3f4 20 63 72 65 61 74 65 20 49 4e 53 54 45 41 44 20   create INSTEAD 
1d3f5 4f 46 22 0a 20 20 20 20 20 20 20 20 22 20 74 72  OF".        " tr
1d3f6 69 67 67 65 72 20 6f 6e 20 74 61 62 6c 65 3a 20  igger on table: 
1d3f7 25 53 22 2c 20 70 54 61 62 6c 65 4e 61 6d 65 2c  %S", pTableName,
1d3f8 20 30 29 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72   0);.    goto tr
1d3f9 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20  igger_cleanup;. 
1d3fa 20 7d 0a 20 20 69 54 61 62 44 62 20 3d 20 73 71   }.  iTabDb = sq
1d3fb 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64  lite3SchemaToInd
1d3fc 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63  ex(db, pTab->pSc
1d3fd 68 65 6d 61 29 3b 0a 0a 23 69 66 6e 64 65 66 20  hema);..#ifndef 
1d3fe 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48  SQLITE_OMIT_AUTH
1d3ff 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20  ORIZATION.  {.  
1d400 20 20 69 6e 74 20 63 6f 64 65 20 3d 20 53 51 4c    int code = SQL
1d401 49 54 45 5f 43 52 45 41 54 45 5f 54 52 49 47 47  ITE_CREATE_TRIGG
1d402 45 52 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  ER;.    const ch
1d403 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44  ar *zDb = db->aD
1d404 62 5b 69 54 61 62 44 62 5d 2e 7a 4e 61 6d 65 3b  b[iTabDb].zName;
1d405 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
1d406 2a 7a 44 62 54 72 69 67 20 3d 20 69 73 54 65 6d  *zDbTrig = isTem
1d407 70 20 3f 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 7a  p ? db->aDb[1].z
1d408 4e 61 6d 65 20 3a 20 7a 44 62 3b 0a 20 20 20 20  Name : zDb;.    
1d409 69 66 28 20 69 54 61 62 44 62 3d 3d 31 20 7c 7c  if( iTabDb==1 ||
1d40a 20 69 73 54 65 6d 70 20 29 20 63 6f 64 65 20 3d   isTemp ) code =
1d40b 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54   SQLITE_CREATE_T
1d40c 45 4d 50 5f 54 52 49 47 47 45 52 3b 0a 20 20 20  EMP_TRIGGER;.   
1d40d 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68   if( sqlite3Auth
1d40e 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 63 6f  Check(pParse, co
1d40f 64 65 2c 20 7a 4e 61 6d 65 2c 20 70 54 61 62 2d  de, zName, pTab-
1d410 3e 7a 4e 61 6d 65 2c 20 7a 44 62 54 72 69 67 29  >zName, zDbTrig)
1d411 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74   ){.      goto t
1d412 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a  rigger_cleanup;.
1d413 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71      }.    if( sq
1d414 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
1d415 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 49 4e  Parse, SQLITE_IN
1d416 53 45 52 54 2c 20 53 43 48 45 4d 41 5f 54 41 42  SERT, SCHEMA_TAB
1d417 4c 45 28 69 54 61 62 44 62 29 2c 30 2c 7a 44 62  LE(iTabDb),0,zDb
1d418 29 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74  )){.      goto t
1d419 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a  rigger_cleanup;.
1d41a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
1d41b 0a 0a 20 20 2f 2a 20 49 4e 53 54 45 41 44 20 4f  ..  /* INSTEAD O
1d41c 46 20 74 72 69 67 67 65 72 73 20 63 61 6e 20 6f  F triggers can o
1d41d 6e 6c 79 20 61 70 70 65 61 72 20 6f 6e 20 76 69  nly appear on vi
1d41e 65 77 73 20 61 6e 64 20 42 45 46 4f 52 45 20 74  ews and BEFORE t
1d41f 72 69 67 67 65 72 73 0a 20 20 2a 2a 20 63 61 6e  riggers.  ** can
1d420 6e 6f 74 20 61 70 70 65 61 72 20 6f 6e 20 76 69  not appear on vi
1d421 65 77 73 2e 20 20 53 6f 20 77 65 20 6d 69 67 68  ews.  So we migh
1d422 74 20 61 73 20 77 65 6c 6c 20 74 72 61 6e 73 6c  t as well transl
1d423 61 74 65 20 65 76 65 72 79 0a 20 20 2a 2a 20 49  ate every.  ** I
1d424 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67 67 65  NSTEAD OF trigge
1d425 72 20 69 6e 74 6f 20 61 20 42 45 46 4f 52 45 20  r into a BEFORE 
1d426 74 72 69 67 67 65 72 2e 20 20 49 74 20 73 69 6d  trigger.  It sim
1d427 70 6c 69 66 69 65 73 20 63 6f 64 65 0a 20 20 2a  plifies code.  *
1d428 2a 20 65 6c 73 65 77 68 65 72 65 2e 0a 20 20 2a  * elsewhere..  *
1d429 2f 0a 20 20 69 66 20 28 74 72 5f 74 6d 20 3d 3d  /.  if (tr_tm ==
1d42a 20 54 4b 5f 49 4e 53 54 45 41 44 29 7b 0a 20 20   TK_INSTEAD){.  
1d42b 20 20 74 72 5f 74 6d 20 3d 20 54 4b 5f 42 45 46    tr_tm = TK_BEF
1d42c 4f 52 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42  ORE;.  }..  /* B
1d42d 75 69 6c 64 20 74 68 65 20 54 72 69 67 67 65 72  uild the Trigger
1d42e 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 70 54 72   object */.  pTr
1d42f 69 67 67 65 72 20 3d 20 28 54 72 69 67 67 65 72  igger = (Trigger
1d430 2a 29 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 73  *)sqliteMalloc(s
1d431 69 7a 65 6f 66 28 54 72 69 67 67 65 72 29 29 3b  izeof(Trigger));
1d432 0a 20 20 69 66 28 20 70 54 72 69 67 67 65 72 3d  .  if( pTrigger=
1d433 3d 30 20 29 20 67 6f 74 6f 20 74 72 69 67 67 65  =0 ) goto trigge
1d434 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 70 54 72  r_cleanup;.  pTr
1d435 69 67 67 65 72 2d 3e 6e 61 6d 65 20 3d 20 7a 4e  igger->name = zN
1d436 61 6d 65 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 30  ame;.  zName = 0
1d437 3b 0a 20 20 70 54 72 69 67 67 65 72 2d 3e 74 61  ;.  pTrigger->ta
1d438 62 6c 65 20 3d 20 73 71 6c 69 74 65 53 74 72 44  ble = sqliteStrD
1d439 75 70 28 70 54 61 62 6c 65 4e 61 6d 65 2d 3e 61  up(pTableName->a
1d43a 5b 30 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 70 54  [0].zName);.  pT
1d43b 72 69 67 67 65 72 2d 3e 70 53 63 68 65 6d 61 20  rigger->pSchema 
1d43c 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70  = db->aDb[iDb].p
1d43d 53 63 68 65 6d 61 3b 0a 20 20 70 54 72 69 67 67  Schema;.  pTrigg
1d43e 65 72 2d 3e 70 54 61 62 53 63 68 65 6d 61 20 3d  er->pTabSchema =
1d43f 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a   pTab->pSchema;.
1d440 20 20 70 54 72 69 67 67 65 72 2d 3e 6f 70 20 3d    pTrigger->op =
1d441 20 6f 70 3b 0a 20 20 70 54 72 69 67 67 65 72 2d   op;.  pTrigger-
1d442 3e 74 72 5f 74 6d 20 3d 20 74 72 5f 74 6d 3d 3d  >tr_tm = tr_tm==
1d443 54 4b 5f 42 45 46 4f 52 45 20 3f 20 54 52 49 47  TK_BEFORE ? TRIG
1d444 47 45 52 5f 42 45 46 4f 52 45 20 3a 20 54 52 49  GER_BEFORE : TRI
1d445 47 47 45 52 5f 41 46 54 45 52 3b 0a 20 20 70 54  GGER_AFTER;.  pT
1d446 72 69 67 67 65 72 2d 3e 70 57 68 65 6e 20 3d 20  rigger->pWhen = 
1d447 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 70  sqlite3ExprDup(p
1d448 57 68 65 6e 29 3b 0a 20 20 70 54 72 69 67 67 65  When);.  pTrigge
1d449 72 2d 3e 70 43 6f 6c 75 6d 6e 73 20 3d 20 73 71  r->pColumns = sq
1d44a 6c 69 74 65 33 49 64 4c 69 73 74 44 75 70 28 70  lite3IdListDup(p
1d44b 43 6f 6c 75 6d 6e 73 29 3b 0a 20 20 73 71 6c 69  Columns);.  sqli
1d44c 74 65 33 54 6f 6b 65 6e 43 6f 70 79 28 26 70 54  te3TokenCopy(&pT
1d44d 72 69 67 67 65 72 2d 3e 6e 61 6d 65 54 6f 6b 65  rigger->nameToke
1d44e 6e 2c 70 4e 61 6d 65 29 3b 0a 20 20 61 73 73 65  n,pName);.  asse
1d44f 72 74 28 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  rt( pParse->pNew
1d450 54 72 69 67 67 65 72 3d 3d 30 20 29 3b 0a 20 20  Trigger==0 );.  
1d451 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67  pParse->pNewTrig
1d452 67 65 72 20 3d 20 70 54 72 69 67 67 65 72 3b 0a  ger = pTrigger;.
1d453 0a 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70  .trigger_cleanup
1d454 3a 0a 20 20 73 71 6c 69 74 65 46 72 65 65 28 7a  :.  sqliteFree(z
1d455 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  Name);.  sqlite3
1d456 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 70 54  SrcListDelete(pT
1d457 61 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 73 71 6c  ableName);.  sql
1d458 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65  ite3IdListDelete
1d459 28 70 43 6f 6c 75 6d 6e 73 29 3b 0a 20 20 73 71  (pColumns);.  sq
1d45a 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
1d45b 70 57 68 65 6e 29 3b 0a 20 20 69 66 28 20 21 70  pWhen);.  if( !p
1d45c 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67  Parse->pNewTrigg
1d45d 65 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  er ){.    sqlite
1d45e 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 70  3DeleteTrigger(p
1d45f 54 72 69 67 67 65 72 29 3b 0a 20 20 7d 65 6c 73  Trigger);.  }els
1d460 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  e{.    assert( p
1d461 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67  Parse->pNewTrigg
1d462 65 72 3d 3d 70 54 72 69 67 67 65 72 20 29 3b 0a  er==pTrigger );.
1d463 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69    }.}../*.** Thi
1d464 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
1d465 6c 65 64 20 61 66 74 65 72 20 61 6c 6c 20 6f 66  led after all of
1d466 20 74 68 65 20 74 72 69 67 67 65 72 20 61 63 74   the trigger act
1d467 69 6f 6e 73 20 68 61 76 65 20 62 65 65 6e 20 70  ions have been p
1d468 61 72 73 65 64 0a 2a 2a 20 69 6e 20 6f 72 64 65  arsed.** in orde
1d469 72 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68  r to complete th
1d46a 65 20 70 72 6f 63 65 73 73 20 6f 66 20 62 75 69  e process of bui
1d46b 6c 64 69 6e 67 20 74 68 65 20 74 72 69 67 67 65  lding the trigge
1d46c 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  r..*/.SQLITE_PRI
1d46d 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1d46e 33 46 69 6e 69 73 68 54 72 69 67 67 65 72 28 0a  3FinishTrigger(.
1d46f 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
1d470 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72            /* Par
1d471 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ser context */. 
1d472 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 53   TriggerStep *pS
1d473 74 65 70 4c 69 73 74 2c 20 2f 2a 20 54 68 65 20  tepList, /* The 
1d474 74 72 69 67 67 65 72 65 64 20 70 72 6f 67 72 61  triggered progra
1d475 6d 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 41  m */.  Token *pA
1d476 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ll             /
1d477 2a 20 54 6f 6b 65 6e 20 74 68 61 74 20 64 65 73  * Token that des
1d478 63 72 69 62 65 73 20 74 68 65 20 63 6f 6d 70 6c  cribes the compl
1d479 65 74 65 20 43 52 45 41 54 45 20 54 52 49 47 47  ete CREATE TRIGG
1d47a 45 52 20 2a 2f 0a 29 7b 0a 20 20 54 72 69 67 67  ER */.){.  Trigg
1d47b 65 72 20 2a 70 54 72 69 67 20 3d 20 30 3b 20 20  er *pTrig = 0;  
1d47c 20 20 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65     /* The trigge
1d47d 72 20 77 68 6f 73 65 20 63 6f 6e 73 74 72 75 63  r whose construc
1d47e 74 69 6f 6e 20 69 73 20 66 69 6e 69 73 68 69 6e  tion is finishin
1d47f 67 20 75 70 20 2a 2f 0a 20 20 73 71 6c 69 74 65  g up */.  sqlite
1d480 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
1d481 64 62 3b 20 20 2f 2a 20 54 68 65 20 64 61 74 61  db;  /* The data
1d482 62 61 73 65 20 2a 2f 0a 20 20 44 62 46 69 78 65  base */.  DbFixe
1d483 72 20 73 46 69 78 3b 0a 20 20 69 6e 74 20 69 44  r sFix;.  int iD
1d484 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
1d485 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
1d486 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
1d487 74 72 69 67 67 65 72 20 2a 2f 0a 0a 20 20 70 54  trigger */..  pT
1d488 72 69 67 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e  rig = pParse->pN
1d489 65 77 54 72 69 67 67 65 72 3b 0a 20 20 70 50 61  ewTrigger;.  pPa
1d48a 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72  rse->pNewTrigger
1d48b 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 50 61 72   = 0;.  if( pPar
1d48c 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 21 70 54 72  se->nErr || !pTr
1d48d 69 67 20 29 20 67 6f 74 6f 20 74 72 69 67 67 65  ig ) goto trigge
1d48e 72 66 69 6e 69 73 68 5f 63 6c 65 61 6e 75 70 3b  rfinish_cleanup;
1d48f 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33  .  iDb = sqlite3
1d490 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50  SchemaToIndex(pP
1d491 61 72 73 65 2d 3e 64 62 2c 20 70 54 72 69 67 2d  arse->db, pTrig-
1d492 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 70 54 72  >pSchema);.  pTr
1d493 69 67 2d 3e 73 74 65 70 5f 6c 69 73 74 20 3d 20  ig->step_list = 
1d494 70 53 74 65 70 4c 69 73 74 3b 0a 20 20 77 68 69  pStepList;.  whi
1d495 6c 65 28 20 70 53 74 65 70 4c 69 73 74 20 29 7b  le( pStepList ){
1d496 0a 20 20 20 20 70 53 74 65 70 4c 69 73 74 2d 3e  .    pStepList->
1d497 70 54 72 69 67 20 3d 20 70 54 72 69 67 3b 0a 20  pTrig = pTrig;. 
1d498 20 20 20 70 53 74 65 70 4c 69 73 74 20 3d 20 70     pStepList = p
1d499 53 74 65 70 4c 69 73 74 2d 3e 70 4e 65 78 74 3b  StepList->pNext;
1d49a 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74  .  }.  if( sqlit
1d49b 65 33 46 69 78 49 6e 69 74 28 26 73 46 69 78 2c  e3FixInit(&sFix,
1d49c 20 70 50 61 72 73 65 2c 20 69 44 62 2c 20 22 74   pParse, iDb, "t
1d49d 72 69 67 67 65 72 22 2c 20 26 70 54 72 69 67 2d  rigger", &pTrig-
1d49e 3e 6e 61 6d 65 54 6f 6b 65 6e 29 20 0a 20 20 20  >nameToken) .   
1d49f 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65         && sqlite
1d4a0 33 46 69 78 54 72 69 67 67 65 72 53 74 65 70 28  3FixTriggerStep(
1d4a1 26 73 46 69 78 2c 20 70 54 72 69 67 2d 3e 73 74  &sFix, pTrig->st
1d4a2 65 70 5f 6c 69 73 74 29 20 29 7b 0a 20 20 20 20  ep_list) ){.    
1d4a3 67 6f 74 6f 20 74 72 69 67 67 65 72 66 69 6e 69  goto triggerfini
1d4a4 73 68 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a  sh_cleanup;.  }.
1d4a5 0a 20 20 2f 2a 20 69 66 20 77 65 20 61 72 65 20  .  /* if we are 
1d4a6 6e 6f 74 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67  not initializing
1d4a7 2c 20 61 6e 64 20 74 68 69 73 20 74 72 69 67 67  , and this trigg
1d4a8 65 72 20 69 73 20 6e 6f 74 20 6f 6e 20 61 20 54  er is not on a T
1d4a9 45 4d 50 20 74 61 62 6c 65 2c 20 0a 20 20 2a 2a  EMP table, .  **
1d4aa 20 62 75 69 6c 64 20 74 68 65 20 73 71 6c 69 74   build the sqlit
1d4ab 65 5f 6d 61 73 74 65 72 20 65 6e 74 72 79 0a 20  e_master entry. 
1d4ac 20 2a 2f 0a 20 20 69 66 28 20 21 64 62 2d 3e 69   */.  if( !db->i
1d4ad 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20  nit.busy ){.    
1d4ae 73 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62  static const Vdb
1d4af 65 4f 70 4c 69 73 74 20 69 6e 73 65 72 74 54 72  eOpList insertTr
1d4b0 69 67 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 7b  ig[] = {.      {
1d4b1 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 20 20   OP_NewRowid,   
1d4b2 30 2c 20 30 2c 20 20 30 20 20 20 20 20 20 20 20  0, 0,  0        
1d4b3 20 20 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f    },.      { OP_
1d4b4 53 74 72 69 6e 67 38 2c 20 20 20 20 30 2c 20 30  String8,    0, 0
1d4b5 2c 20 20 22 74 72 69 67 67 65 72 22 20 20 7d 2c  ,  "trigger"  },
1d4b6 0a 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69  .      { OP_Stri
1d4b7 6e 67 38 2c 20 20 20 20 30 2c 20 30 2c 20 20 30  ng8,    0, 0,  0
1d4b8 20 20 20 20 20 20 20 20 20 20 7d 2c 20 20 2f 2a            },  /*
1d4b9 20 32 3a 20 74 72 69 67 67 65 72 20 6e 61 6d 65   2: trigger name
1d4ba 20 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f 53   */.      { OP_S
1d4bb 74 72 69 6e 67 38 2c 20 20 20 20 30 2c 20 30 2c  tring8,    0, 0,
1d4bc 20 20 30 20 20 20 20 20 20 20 20 20 20 7d 2c 20    0          }, 
1d4bd 20 2f 2a 20 33 3a 20 74 61 62 6c 65 20 6e 61 6d   /* 3: table nam
1d4be 65 20 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f  e */.      { OP_
1d4bf 49 6e 74 65 67 65 72 2c 20 20 20 20 30 2c 20 30  Integer,    0, 0
1d4c0 2c 20 20 30 20 20 20 20 20 20 20 20 20 20 7d 2c  ,  0          },
1d4c1 0a 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69  .      { OP_Stri
1d4c2 6e 67 38 2c 20 20 20 20 30 2c 20 30 2c 20 20 22  ng8,    0, 0,  "
1d4c3 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20 22  CREATE TRIGGER "
1d4c4 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 53 74  },.      { OP_St
1d4c5 72 69 6e 67 38 2c 20 20 20 20 30 2c 20 30 2c 20  ring8,    0, 0, 
1d4c6 20 30 20 20 20 20 20 20 20 20 20 20 7d 2c 20 20   0          },  
1d4c7 2f 2a 20 36 3a 20 53 51 4c 20 2a 2f 0a 20 20 20  /* 6: SQL */.   
1d4c8 20 20 20 7b 20 4f 50 5f 43 6f 6e 63 61 74 2c 20     { OP_Concat, 
1d4c9 20 20 20 20 30 2c 20 30 2c 20 20 30 20 20 20 20      0, 0,  0    
1d4ca 20 20 20 20 20 20 7d 2c 20 0a 20 20 20 20 20 20        }, .      
1d4cb 7b 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c  { OP_MakeRecord,
1d4cc 20 35 2c 20 30 2c 20 20 22 61 61 61 64 61 22 20   5, 0,  "aaada" 
1d4cd 20 20 20 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50     },.      { OP
1d4ce 5f 49 6e 73 65 72 74 2c 20 20 20 20 20 30 2c 20  _Insert,     0, 
1d4cf 30 2c 20 20 30 20 20 20 20 20 20 20 20 20 20 7d  0,  0          }
1d4d0 2c 0a 20 20 20 20 7d 3b 0a 20 20 20 20 69 6e 74  ,.    };.    int
1d4d1 20 61 64 64 72 3b 0a 20 20 20 20 56 64 62 65 20   addr;.    Vdbe 
1d4d2 2a 76 3b 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b 65  *v;..    /* Make
1d4d3 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 74 68 65   an entry in the
1d4d4 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74   sqlite_master t
1d4d5 61 62 6c 65 20 2a 2f 0a 20 20 20 20 76 20 3d 20  able */.    v = 
1d4d6 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
1d4d7 50 61 72 73 65 29 3b 0a 20 20 20 20 69 66 28 20  Parse);.    if( 
1d4d8 76 3d 3d 30 20 29 20 67 6f 74 6f 20 74 72 69 67  v==0 ) goto trig
1d4d9 67 65 72 66 69 6e 69 73 68 5f 63 6c 65 61 6e 75  gerfinish_cleanu
1d4da 70 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65  p;.    sqlite3Be
1d4db 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f  ginWriteOperatio
1d4dc 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62  n(pParse, 0, iDb
1d4dd 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 70  );.    sqlite3Op
1d4de 65 6e 4d 61 73 74 65 72 54 61 62 6c 65 28 70 50  enMasterTable(pP
1d4df 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20  arse, iDb);.    
1d4e0 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
1d4e1 62 65 41 64 64 4f 70 4c 69 73 74 28 76 2c 20 41  beAddOpList(v, A
1d4e2 72 72 61 79 53 69 7a 65 28 69 6e 73 65 72 74 54  rraySize(insertT
1d4e3 72 69 67 29 2c 20 69 6e 73 65 72 74 54 72 69 67  rig), insertTrig
1d4e4 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1d4e5 62 65 43 68 61 6e 67 65 50 33 28 76 2c 20 61 64  beChangeP3(v, ad
1d4e6 64 72 2b 32 2c 20 70 54 72 69 67 2d 3e 6e 61 6d  dr+2, pTrig->nam
1d4e7 65 2c 20 30 29 3b 20 0a 20 20 20 20 73 71 6c 69  e, 0); .    sqli
1d4e8 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28  te3VdbeChangeP3(
1d4e9 76 2c 20 61 64 64 72 2b 33 2c 20 70 54 72 69 67  v, addr+3, pTrig
1d4ea 2d 3e 74 61 62 6c 65 2c 20 30 29 3b 20 0a 20 20  ->table, 0); .  
1d4eb 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
1d4ec 6e 67 65 50 33 28 76 2c 20 61 64 64 72 2b 36 2c  ngeP3(v, addr+6,
1d4ed 20 28 63 68 61 72 2a 29 70 41 6c 6c 2d 3e 7a 2c   (char*)pAll->z,
1d4ee 20 70 41 6c 6c 2d 3e 6e 29 3b 0a 20 20 20 20 73   pAll->n);.    s
1d4ef 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b  qlite3ChangeCook
1d4f0 69 65 28 64 62 2c 20 76 2c 20 69 44 62 29 3b 0a  ie(db, v, iDb);.
1d4f1 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1d4f2 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f 73 65  ddOp(v, OP_Close
1d4f3 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
1d4f4 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20 4f  ite3VdbeOp3(v, O
1d4f5 50 5f 50 61 72 73 65 53 63 68 65 6d 61 2c 20 69  P_ParseSchema, i
1d4f6 44 62 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 73  Db, 0, .       s
1d4f7 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 22 74  qlite3MPrintf("t
1d4f8 79 70 65 3d 27 74 72 69 67 67 65 72 27 20 41 4e  ype='trigger' AN
1d4f9 44 20 6e 61 6d 65 3d 27 25 71 27 22 2c 20 70 54  D name='%q'", pT
1d4fa 72 69 67 2d 3e 6e 61 6d 65 29 2c 20 50 33 5f 44  rig->name), P3_D
1d4fb 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 0a 20 20  YNAMIC);.  }..  
1d4fc 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73  if( db->init.bus
1d4fd 79 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 3b 0a  y ){.    int n;.
1d4fe 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b      Table *pTab;
1d4ff 0a 20 20 20 20 54 72 69 67 67 65 72 20 2a 70 44  .    Trigger *pD
1d500 65 6c 3b 0a 20 20 20 20 70 44 65 6c 20 3d 20 73  el;.    pDel = s
1d501 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74  qlite3HashInsert
1d502 28 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70  (&db->aDb[iDb].p
1d503 53 63 68 65 6d 61 2d 3e 74 72 69 67 48 61 73 68  Schema->trigHash
1d504 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
1d505 20 20 20 20 20 20 20 20 70 54 72 69 67 2d 3e 6e          pTrig->n
1d506 61 6d 65 2c 20 73 74 72 6c 65 6e 28 70 54 72 69  ame, strlen(pTri
1d507 67 2d 3e 6e 61 6d 65 29 2c 20 70 54 72 69 67 29  g->name), pTrig)
1d508 3b 0a 20 20 20 20 69 66 28 20 70 44 65 6c 20 29  ;.    if( pDel )
1d509 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
1d50a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69  sqlite3MallocFai
1d50b 6c 65 64 28 29 20 26 26 20 70 44 65 6c 3d 3d 70  led() && pDel==p
1d50c 54 72 69 67 20 29 3b 0a 20 20 20 20 20 20 67 6f  Trig );.      go
1d50d 74 6f 20 74 72 69 67 67 65 72 66 69 6e 69 73 68  to triggerfinish
1d50e 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a  _cleanup;.    }.
1d50f 20 20 20 20 6e 20 3d 20 73 74 72 6c 65 6e 28 70      n = strlen(p
1d510 54 72 69 67 2d 3e 74 61 62 6c 65 29 20 2b 20 31  Trig->table) + 1
1d511 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 73 71 6c  ;.    pTab = sql
1d512 69 74 65 33 48 61 73 68 46 69 6e 64 28 26 70 54  ite3HashFind(&pT
1d513 72 69 67 2d 3e 70 54 61 62 53 63 68 65 6d 61 2d  rig->pTabSchema-
1d514 3e 74 62 6c 48 61 73 68 2c 20 70 54 72 69 67 2d  >tblHash, pTrig-
1d515 3e 74 61 62 6c 65 2c 20 6e 29 3b 0a 20 20 20 20  >table, n);.    
1d516 61 73 73 65 72 74 28 20 70 54 61 62 21 3d 30 20  assert( pTab!=0 
1d517 29 3b 0a 20 20 20 20 70 54 72 69 67 2d 3e 70 4e  );.    pTrig->pN
1d518 65 78 74 20 3d 20 70 54 61 62 2d 3e 70 54 72 69  ext = pTab->pTri
1d519 67 67 65 72 3b 0a 20 20 20 20 70 54 61 62 2d 3e  gger;.    pTab->
1d51a 70 54 72 69 67 67 65 72 20 3d 20 70 54 72 69 67  pTrigger = pTrig
1d51b 3b 0a 20 20 20 20 70 54 72 69 67 20 3d 20 30 3b  ;.    pTrig = 0;
1d51c 0a 20 20 7d 0a 0a 74 72 69 67 67 65 72 66 69 6e  .  }..triggerfin
1d51d 69 73 68 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73  ish_cleanup:.  s
1d51e 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67  qlite3DeleteTrig
1d51f 67 65 72 28 70 54 72 69 67 29 3b 0a 20 20 61 73  ger(pTrig);.  as
1d520 73 65 72 74 28 20 21 70 50 61 72 73 65 2d 3e 70  sert( !pParse->p
1d521 4e 65 77 54 72 69 67 67 65 72 20 29 3b 0a 20 20  NewTrigger );.  
1d522 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69  sqlite3DeleteTri
1d523 67 67 65 72 53 74 65 70 28 70 53 74 65 70 4c 69  ggerStep(pStepLi
1d524 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61  st);.}../*.** Ma
1d525 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 61 6c 6c  ke a copy of all
1d526 20 63 6f 6d 70 6f 6e 65 6e 74 73 20 6f 66 20 74   components of t
1d527 68 65 20 67 69 76 65 6e 20 74 72 69 67 67 65 72  he given trigger
1d528 20 73 74 65 70 2e 20 20 54 68 69 73 20 68 61 73   step.  This has
1d529 0a 2a 2a 20 74 68 65 20 65 66 66 65 63 74 20 6f  .** the effect o
1d52a 66 20 63 6f 70 79 69 6e 67 20 61 6c 6c 20 45 78  f copying all Ex
1d52b 70 72 2e 74 6f 6b 65 6e 2e 7a 20 76 61 6c 75 65  pr.token.z value
1d52c 73 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62  s into memory ob
1d52d 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 73  tained.** from s
1d52e 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20  qliteMalloc().  
1d52f 41 73 20 69 6e 69 74 69 61 6c 6c 79 20 63 72 65  As initially cre
1d530 61 74 65 64 2c 20 74 68 65 20 45 78 70 72 2e 74  ated, the Expr.t
1d531 6f 6b 65 6e 2e 7a 20 76 61 6c 75 65 73 0a 2a 2a  oken.z values.**
1d532 20 61 6c 6c 20 70 6f 69 6e 74 20 74 6f 20 74 68   all point to th
1d533 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 74  e input string t
1d534 68 61 74 20 77 61 73 20 66 65 64 20 74 6f 20 74  hat was fed to t
1d535 68 65 20 70 61 72 73 65 72 2e 20 20 42 75 74 20  he parser.  But 
1d536 74 68 61 74 0a 2a 2a 20 73 74 72 69 6e 67 20 69  that.** string i
1d537 73 20 65 70 68 65 6d 65 72 61 6c 20 2d 20 69 74  s ephemeral - it
1d538 20 77 69 6c 6c 20 67 6f 20 61 77 61 79 20 61 73   will go away as
1d539 20 73 6f 6f 6e 20 61 73 20 74 68 65 20 73 71 6c   soon as the sql
1d53a 69 74 65 33 5f 65 78 65 63 28 29 0a 2a 2a 20 63  ite3_exec().** c
1d53b 61 6c 6c 20 74 68 61 74 20 73 74 61 72 74 65 64  all that started
1d53c 20 74 68 65 20 70 61 72 73 65 72 20 65 78 69 74   the parser exit
1d53d 73 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  s.  This routine
1d53e 20 6d 61 6b 65 73 20 61 20 70 65 72 73 69 73 74   makes a persist
1d53f 65 6e 74 0a 2a 2a 20 63 6f 70 79 20 6f 66 20 61  ent.** copy of a
1d540 6c 6c 20 74 68 65 20 45 78 70 72 2e 74 6f 6b 65  ll the Expr.toke
1d541 6e 2e 7a 20 73 74 72 69 6e 67 73 20 73 6f 20 74  n.z strings so t
1d542 68 61 74 20 74 68 65 20 54 72 69 67 67 65 72 53  hat the TriggerS
1d543 74 65 70 20 73 74 72 75 63 74 75 72 65 0a 2a 2a  tep structure.**
1d544 20 77 69 6c 6c 20 62 65 20 76 61 6c 69 64 20 65   will be valid e
1d545 76 65 6e 20 61 66 74 65 72 20 74 68 65 20 73 71  ven after the sq
1d546 6c 69 74 65 33 5f 65 78 65 63 28 29 20 63 61 6c  lite3_exec() cal
1d547 6c 20 72 65 74 75 72 6e 73 2e 0a 2a 2f 0a 73 74  l returns..*/.st
1d548 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65  atic void sqlite
1d549 50 65 72 73 69 73 74 54 72 69 67 67 65 72 53 74  PersistTriggerSt
1d54a 65 70 28 54 72 69 67 67 65 72 53 74 65 70 20 2a  ep(TriggerStep *
1d54b 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 74 61 72  p){.  if( p->tar
1d54c 67 65 74 2e 7a 20 29 7b 0a 20 20 20 20 70 2d 3e  get.z ){.    p->
1d54d 74 61 72 67 65 74 2e 7a 20 3d 20 28 75 38 2a 29  target.z = (u8*)
1d54e 73 71 6c 69 74 65 53 74 72 4e 44 75 70 28 28 63  sqliteStrNDup((c
1d54f 68 61 72 2a 29 70 2d 3e 74 61 72 67 65 74 2e 7a  har*)p->target.z
1d550 2c 20 70 2d 3e 74 61 72 67 65 74 2e 6e 29 3b 0a  , p->target.n);.
1d551 20 20 20 20 70 2d 3e 74 61 72 67 65 74 2e 64 79      p->target.dy
1d552 6e 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28  n = 1;.  }.  if(
1d553 20 70 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20   p->pSelect ){. 
1d554 20 20 20 53 65 6c 65 63 74 20 2a 70 4e 65 77 20     Select *pNew 
1d555 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44  = sqlite3SelectD
1d556 75 70 28 70 2d 3e 70 53 65 6c 65 63 74 29 3b 0a  up(p->pSelect);.
1d557 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
1d558 74 44 65 6c 65 74 65 28 70 2d 3e 70 53 65 6c 65  tDelete(p->pSele
1d559 63 74 29 3b 0a 20 20 20 20 70 2d 3e 70 53 65 6c  ct);.    p->pSel
1d55a 65 63 74 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a  ect = pNew;.  }.
1d55b 20 20 69 66 28 20 70 2d 3e 70 57 68 65 72 65 20    if( p->pWhere 
1d55c 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 4e 65  ){.    Expr *pNe
1d55d 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  w = sqlite3ExprD
1d55e 75 70 28 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20  up(p->pWhere);. 
1d55f 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65     sqlite3ExprDe
1d560 6c 65 74 65 28 70 2d 3e 70 57 68 65 72 65 29 3b  lete(p->pWhere);
1d561 0a 20 20 20 20 70 2d 3e 70 57 68 65 72 65 20 3d  .    p->pWhere =
1d562 20 70 4e 65 77 3b 0a 20 20 7d 0a 20 20 69 66 28   pNew;.  }.  if(
1d563 20 70 2d 3e 70 45 78 70 72 4c 69 73 74 20 29 7b   p->pExprList ){
1d564 0a 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70  .    ExprList *p
1d565 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70  New = sqlite3Exp
1d566 72 4c 69 73 74 44 75 70 28 70 2d 3e 70 45 78 70  rListDup(p->pExp
1d567 72 4c 69 73 74 29 3b 0a 20 20 20 20 73 71 6c 69  rList);.    sqli
1d568 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
1d569 65 28 70 2d 3e 70 45 78 70 72 4c 69 73 74 29 3b  e(p->pExprList);
1d56a 0a 20 20 20 20 70 2d 3e 70 45 78 70 72 4c 69 73  .    p->pExprLis
1d56b 74 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 20 20  t = pNew;.  }.  
1d56c 69 66 28 20 70 2d 3e 70 49 64 4c 69 73 74 20 29  if( p->pIdList )
1d56d 7b 0a 20 20 20 20 49 64 4c 69 73 74 20 2a 70 4e  {.    IdList *pN
1d56e 65 77 20 3d 20 73 71 6c 69 74 65 33 49 64 4c 69  ew = sqlite3IdLi
1d56f 73 74 44 75 70 28 70 2d 3e 70 49 64 4c 69 73 74  stDup(p->pIdList
1d570 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 49 64  );.    sqlite3Id
1d571 4c 69 73 74 44 65 6c 65 74 65 28 70 2d 3e 70 49  ListDelete(p->pI
1d572 64 4c 69 73 74 29 3b 0a 20 20 20 20 70 2d 3e 70  dList);.    p->p
1d573 49 64 4c 69 73 74 20 3d 20 70 4e 65 77 3b 0a 20  IdList = pNew;. 
1d574 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e   }.}../*.** Turn
1d575 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
1d576 65 6e 74 20 28 74 68 61 74 20 74 68 65 20 70 53  ent (that the pS
1d577 65 6c 65 63 74 20 70 61 72 61 6d 65 74 65 72 20  elect parameter 
1d578 70 6f 69 6e 74 73 20 74 6f 29 20 69 6e 74 6f 0a  points to) into.
1d579 2a 2a 20 61 20 74 72 69 67 67 65 72 20 73 74 65  ** a trigger ste
1d57a 70 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69  p.  Return a poi
1d57b 6e 74 65 72 20 74 6f 20 61 20 54 72 69 67 67 65  nter to a Trigge
1d57c 72 53 74 65 70 20 73 74 72 75 63 74 75 72 65 2e  rStep structure.
1d57d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73 65  .**.** The parse
1d57e 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75  r calls this rou
1d57f 74 69 6e 65 20 77 68 65 6e 20 69 74 20 66 69 6e  tine when it fin
1d580 64 73 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  ds a SELECT stat
1d581 65 6d 65 6e 74 20 69 6e 0a 2a 2a 20 62 6f 64 79  ement in.** body
1d582 20 6f 66 20 61 20 54 52 49 47 47 45 52 2e 20 20   of a TRIGGER.  
1d583 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1d584 54 45 20 54 72 69 67 67 65 72 53 74 65 70 20 2a  TE TriggerStep *
1d585 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 53 65  sqlite3TriggerSe
1d586 6c 65 63 74 53 74 65 70 28 53 65 6c 65 63 74 20  lectStep(Select 
1d587 2a 70 53 65 6c 65 63 74 29 7b 0a 20 20 54 72 69  *pSelect){.  Tri
1d588 67 67 65 72 53 74 65 70 20 2a 70 54 72 69 67 67  ggerStep *pTrigg
1d589 65 72 53 74 65 70 20 3d 20 73 71 6c 69 74 65 4d  erStep = sqliteM
1d58a 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 54 72 69  alloc(sizeof(Tri
1d58b 67 67 65 72 53 74 65 70 29 29 3b 0a 20 20 69 66  ggerStep));.  if
1d58c 28 20 70 54 72 69 67 67 65 72 53 74 65 70 3d 3d  ( pTriggerStep==
1d58d 30 20 29 20 7b 0a 20 20 20 20 73 71 6c 69 74 65  0 ) {.    sqlite
1d58e 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70 53  3SelectDelete(pS
1d58f 65 6c 65 63 74 29 3b 0a 20 20 20 20 72 65 74 75  elect);.    retu
1d590 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 70 54 72  rn 0;.  }..  pTr
1d591 69 67 67 65 72 53 74 65 70 2d 3e 6f 70 20 3d 20  iggerStep->op = 
1d592 54 4b 5f 53 45 4c 45 43 54 3b 0a 20 20 70 54 72  TK_SELECT;.  pTr
1d593 69 67 67 65 72 53 74 65 70 2d 3e 70 53 65 6c 65  iggerStep->pSele
1d594 63 74 20 3d 20 70 53 65 6c 65 63 74 3b 0a 20 20  ct = pSelect;.  
1d595 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 72  pTriggerStep->or
1d596 63 6f 6e 66 20 3d 20 4f 45 5f 44 65 66 61 75 6c  conf = OE_Defaul
1d597 74 3b 0a 20 20 73 71 6c 69 74 65 50 65 72 73 69  t;.  sqlitePersi
1d598 73 74 54 72 69 67 67 65 72 53 74 65 70 28 70 54  stTriggerStep(pT
1d599 72 69 67 67 65 72 53 74 65 70 29 3b 0a 0a 20 20  riggerStep);..  
1d59a 72 65 74 75 72 6e 20 70 54 72 69 67 67 65 72 53  return pTriggerS
1d59b 74 65 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 75  tep;.}../*.** Bu
1d59c 69 6c 64 20 61 20 74 72 69 67 67 65 72 20 73 74  ild a trigger st
1d59d 65 70 20 6f 75 74 20 6f 66 20 61 6e 20 49 4e 53  ep out of an INS
1d59e 45 52 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  ERT statement.  
1d59f 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
1d5a0 0a 2a 2a 20 74 6f 20 74 68 65 20 6e 65 77 20 74  .** to the new t
1d5a1 72 69 67 67 65 72 20 73 74 65 70 2e 0a 2a 2a 0a  rigger step..**.
1d5a2 2a 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 61  ** The parser ca
1d5a3 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65  lls this routine
1d5a4 20 77 68 65 6e 20 69 74 20 73 65 65 73 20 61 6e   when it sees an
1d5a5 20 49 4e 53 45 52 54 20 69 6e 73 69 64 65 20 74   INSERT inside t
1d5a6 68 65 0a 2a 2a 20 62 6f 64 79 20 6f 66 20 61 20  he.** body of a 
1d5a7 74 72 69 67 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49  trigger..*/.SQLI
1d5a8 54 45 5f 50 52 49 56 41 54 45 20 54 72 69 67 67  TE_PRIVATE Trigg
1d5a9 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54  erStep *sqlite3T
1d5aa 72 69 67 67 65 72 49 6e 73 65 72 74 53 74 65 70  riggerInsertStep
1d5ab 28 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c  (.  Token *pTabl
1d5ac 65 4e 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d 65 20  eName,  /* Name 
1d5ad 6f 66 20 74 68 65 20 74 61 62 6c 65 20 69 6e 74  of the table int
1d5ae 6f 20 77 68 69 63 68 20 77 65 20 69 6e 73 65 72  o which we inser
1d5af 74 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70  t */.  IdList *p
1d5b0 43 6f 6c 75 6d 6e 2c 20 20 20 20 2f 2a 20 4c 69  Column,    /* Li
1d5b1 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  st of columns in
1d5b2 20 70 54 61 62 6c 65 4e 61 6d 65 20 74 6f 20 69   pTableName to i
1d5b3 6e 73 65 72 74 20 69 6e 74 6f 20 2a 2f 0a 20 20  nsert into */.  
1d5b4 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
1d5b5 2c 20 20 20 2f 2a 20 54 68 65 20 56 41 4c 55 45  ,   /* The VALUE
1d5b6 20 63 6c 61 75 73 65 3a 20 61 20 6c 69 73 74 20   clause: a list 
1d5b7 6f 66 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20  of values to be 
1d5b8 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 53 65  inserted */.  Se
1d5b9 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20  lect *pSelect,  
1d5ba 20 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74    /* A SELECT st
1d5bb 61 74 65 6d 65 6e 74 20 74 68 61 74 20 73 75 70  atement that sup
1d5bc 70 6c 69 65 73 20 76 61 6c 75 65 73 20 2a 2f 0a  plies values */.
1d5bd 20 20 69 6e 74 20 6f 72 63 6f 6e 66 20 20 20 20    int orconf    
1d5be 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e        /* The con
1d5bf 66 6c 69 63 74 20 61 6c 67 6f 72 69 74 68 6d 20  flict algorithm 
1d5c0 28 4f 45 5f 41 62 6f 72 74 2c 20 4f 45 5f 52 65  (OE_Abort, OE_Re
1d5c1 70 6c 61 63 65 2c 20 65 74 63 2e 29 20 2a 2f 0a  place, etc.) */.
1d5c2 29 7b 0a 20 20 54 72 69 67 67 65 72 53 74 65 70  ){.  TriggerStep
1d5c3 20 2a 70 54 72 69 67 67 65 72 53 74 65 70 20 3d   *pTriggerStep =
1d5c4 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 73 69   sqliteMalloc(si
1d5c5 7a 65 6f 66 28 54 72 69 67 67 65 72 53 74 65 70  zeof(TriggerStep
1d5c6 29 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 70 45  ));..  assert(pE
1d5c7 4c 69 73 74 20 3d 3d 20 30 20 7c 7c 20 70 53 65  List == 0 || pSe
1d5c8 6c 65 63 74 20 3d 3d 20 30 29 3b 0a 20 20 61 73  lect == 0);.  as
1d5c9 73 65 72 74 28 70 45 4c 69 73 74 20 21 3d 20 30  sert(pEList != 0
1d5ca 20 7c 7c 20 70 53 65 6c 65 63 74 20 21 3d 20 30   || pSelect != 0
1d5cb 29 3b 0a 0a 20 20 69 66 28 20 70 54 72 69 67 67  );..  if( pTrigg
1d5cc 65 72 53 74 65 70 20 29 7b 0a 20 20 20 20 70 54  erStep ){.    pT
1d5cd 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 70 20 3d  riggerStep->op =
1d5ce 20 54 4b 5f 49 4e 53 45 52 54 3b 0a 20 20 20 20   TK_INSERT;.    
1d5cf 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70 53  pTriggerStep->pS
1d5d0 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63 74 3b  elect = pSelect;
1d5d1 0a 20 20 20 20 70 54 72 69 67 67 65 72 53 74 65  .    pTriggerSte
1d5d2 70 2d 3e 74 61 72 67 65 74 20 20 3d 20 2a 70 54  p->target  = *pT
1d5d3 61 62 6c 65 4e 61 6d 65 3b 0a 20 20 20 20 70 54  ableName;.    pT
1d5d4 72 69 67 67 65 72 53 74 65 70 2d 3e 70 49 64 4c  riggerStep->pIdL
1d5d5 69 73 74 20 3d 20 70 43 6f 6c 75 6d 6e 3b 0a 20  ist = pColumn;. 
1d5d6 20 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d     pTriggerStep-
1d5d7 3e 70 45 78 70 72 4c 69 73 74 20 3d 20 70 45 4c  >pExprList = pEL
1d5d8 69 73 74 3b 0a 20 20 20 20 70 54 72 69 67 67 65  ist;.    pTrigge
1d5d9 72 53 74 65 70 2d 3e 6f 72 63 6f 6e 66 20 3d 20  rStep->orconf = 
1d5da 6f 72 63 6f 6e 66 3b 0a 20 20 20 20 73 71 6c 69  orconf;.    sqli
1d5db 74 65 50 65 72 73 69 73 74 54 72 69 67 67 65 72  tePersistTrigger
1d5dc 53 74 65 70 28 70 54 72 69 67 67 65 72 53 74 65  Step(pTriggerSte
1d5dd 70 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  p);.  }else{.   
1d5de 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65   sqlite3IdListDe
1d5df 6c 65 74 65 28 70 43 6f 6c 75 6d 6e 29 3b 0a 20  lete(pColumn);. 
1d5e0 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69     sqlite3ExprLi
1d5e1 73 74 44 65 6c 65 74 65 28 70 45 4c 69 73 74 29  stDelete(pEList)
1d5e2 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c  ;.    sqlite3Sel
1d5e3 65 63 74 44 75 70 28 70 53 65 6c 65 63 74 29 3b  ectDup(pSelect);
1d5e4 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70  .  }..  return p
1d5e5 54 72 69 67 67 65 72 53 74 65 70 3b 0a 7d 0a 0a  TriggerStep;.}..
1d5e6 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20  /*.** Construct 
1d5e7 61 20 74 72 69 67 67 65 72 20 73 74 65 70 20 74  a trigger step t
1d5e8 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61  hat implements a
1d5e9 6e 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65  n UPDATE stateme
1d5ea 6e 74 20 61 6e 64 20 72 65 74 75 72 6e 0a 2a 2a  nt and return.**
1d5eb 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
1d5ec 61 74 20 74 72 69 67 67 65 72 20 73 74 65 70 2e  at trigger step.
1d5ed 20 20 54 68 65 20 70 61 72 73 65 72 20 63 61 6c    The parser cal
1d5ee 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ls this routine 
1d5ef 77 68 65 6e 20 69 74 0a 2a 2a 20 73 65 65 73 20  when it.** sees 
1d5f0 61 6e 20 55 50 44 41 54 45 20 73 74 61 74 65 6d  an UPDATE statem
1d5f1 65 6e 74 20 69 6e 73 69 64 65 20 74 68 65 20 62  ent inside the b
1d5f2 6f 64 79 20 6f 66 20 61 20 43 52 45 41 54 45 20  ody of a CREATE 
1d5f3 54 52 49 47 47 45 52 2e 0a 2a 2f 0a 53 51 4c 49  TRIGGER..*/.SQLI
1d5f4 54 45 5f 50 52 49 56 41 54 45 20 54 72 69 67 67  TE_PRIVATE Trigg
1d5f5 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54  erStep *sqlite3T
1d5f6 72 69 67 67 65 72 55 70 64 61 74 65 53 74 65 70  riggerUpdateStep
1d5f7 28 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c  (.  Token *pTabl
1d5f8 65 4e 61 6d 65 2c 20 20 20 2f 2a 20 4e 61 6d 65  eName,   /* Name
1d5f9 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f   of the table to
1d5fa 20 62 65 20 75 70 64 61 74 65 64 20 2a 2f 0a 20   be updated */. 
1d5fb 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
1d5fc 74 2c 20 20 20 20 2f 2a 20 54 68 65 20 53 45 54  t,    /* The SET
1d5fd 20 63 6c 61 75 73 65 3a 20 6c 69 73 74 20 6f 66   clause: list of
1d5fe 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 6e 65 77 20   column and new 
1d5ff 76 61 6c 75 65 73 20 2a 2f 0a 20 20 45 78 70 72  values */.  Expr
1d600 20 2a 70 57 68 65 72 65 2c 20 20 20 20 20 20 20   *pWhere,       
1d601 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c   /* The WHERE cl
1d602 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 72  ause */.  int or
1d603 63 6f 6e 66 20 20 20 20 20 20 20 20 20 20 20 2f  conf           /
1d604 2a 20 54 68 65 20 63 6f 6e 66 6c 69 63 74 20 61  * The conflict a
1d605 6c 67 6f 72 69 74 68 6d 2e 20 28 4f 45 5f 41 62  lgorithm. (OE_Ab
1d606 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20  ort, OE_Ignore, 
1d607 65 74 63 29 20 2a 2f 0a 29 7b 0a 20 20 54 72 69  etc) */.){.  Tri
1d608 67 67 65 72 53 74 65 70 20 2a 70 54 72 69 67 67  ggerStep *pTrigg
1d609 65 72 53 74 65 70 20 3d 20 73 71 6c 69 74 65 4d  erStep = sqliteM
1d60a 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 54 72 69  alloc(sizeof(Tri
1d60b 67 67 65 72 53 74 65 70 29 29 3b 0a 20 20 69 66  ggerStep));.  if
1d60c 28 20 70 54 72 69 67 67 65 72 53 74 65 70 3d 3d  ( pTriggerStep==
1d60d 30 20 29 7b 0a 20 20 20 20 20 73 71 6c 69 74 65  0 ){.     sqlite
1d60e 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28  3ExprListDelete(
1d60f 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 20 73 71  pEList);.     sq
1d610 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
1d611 70 57 68 65 72 65 29 3b 0a 20 20 20 20 20 72 65  pWhere);.     re
1d612 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 70  turn 0;.  }..  p
1d613 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 70 20  TriggerStep->op 
1d614 3d 20 54 4b 5f 55 50 44 41 54 45 3b 0a 20 20 70  = TK_UPDATE;.  p
1d615 54 72 69 67 67 65 72 53 74 65 70 2d 3e 74 61 72  TriggerStep->tar
1d616 67 65 74 20 20 3d 20 2a 70 54 61 62 6c 65 4e 61  get  = *pTableNa
1d617 6d 65 3b 0a 20 20 70 54 72 69 67 67 65 72 53 74  me;.  pTriggerSt
1d618 65 70 2d 3e 70 45 78 70 72 4c 69 73 74 20 3d 20  ep->pExprList = 
1d619 70 45 4c 69 73 74 3b 0a 20 20 70 54 72 69 67 67  pEList;.  pTrigg
1d61a 65 72 53 74 65 70 2d 3e 70 57 68 65 72 65 20 3d  erStep->pWhere =
1d61b 20 70 57 68 65 72 65 3b 0a 20 20 70 54 72 69 67   pWhere;.  pTrig
1d61c 67 65 72 53 74 65 70 2d 3e 6f 72 63 6f 6e 66 20  gerStep->orconf 
1d61d 3d 20 6f 72 63 6f 6e 66 3b 0a 20 20 73 71 6c 69  = orconf;.  sqli
1d61e 74 65 50 65 72 73 69 73 74 54 72 69 67 67 65 72  tePersistTrigger
1d61f 53 74 65 70 28 70 54 72 69 67 67 65 72 53 74 65  Step(pTriggerSte
1d620 70 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 70 54  p);..  return pT
1d621 72 69 67 67 65 72 53 74 65 70 3b 0a 7d 0a 0a 2f  riggerStep;.}../
1d622 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20 61  *.** Construct a
1d623 20 74 72 69 67 67 65 72 20 73 74 65 70 20 74 68   trigger step th
1d624 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20  at implements a 
1d625 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74  DELETE statement
1d626 20 61 6e 64 20 72 65 74 75 72 6e 0a 2a 2a 20 61   and return.** a
1d627 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 61 74   pointer to that
1d628 20 74 72 69 67 67 65 72 20 73 74 65 70 2e 20 20   trigger step.  
1d629 54 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c 73  The parser calls
1d62a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68   this routine wh
1d62b 65 6e 20 69 74 0a 2a 2a 20 73 65 65 73 20 61 20  en it.** sees a 
1d62c 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74  DELETE statement
1d62d 20 69 6e 73 69 64 65 20 74 68 65 20 62 6f 64 79   inside the body
1d62e 20 6f 66 20 61 20 43 52 45 41 54 45 20 54 52 49   of a CREATE TRI
1d62f 47 47 45 52 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  GGER..*/.SQLITE_
1d630 50 52 49 56 41 54 45 20 54 72 69 67 67 65 72 53  PRIVATE TriggerS
1d631 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72 69 67  tep *sqlite3Trig
1d632 67 65 72 44 65 6c 65 74 65 53 74 65 70 28 54 6f  gerDeleteStep(To
1d633 6b 65 6e 20 2a 70 54 61 62 6c 65 4e 61 6d 65 2c  ken *pTableName,
1d634 20 45 78 70 72 20 2a 70 57 68 65 72 65 29 7b 0a   Expr *pWhere){.
1d635 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 70    TriggerStep *p
1d636 54 72 69 67 67 65 72 53 74 65 70 20 3d 20 73 71  TriggerStep = sq
1d637 6c 69 74 65 4d 61 6c 6c 6f 63 28 73 69 7a 65 6f  liteMalloc(sizeo
1d638 66 28 54 72 69 67 67 65 72 53 74 65 70 29 29 3b  f(TriggerStep));
1d639 0a 20 20 69 66 28 20 70 54 72 69 67 67 65 72 53  .  if( pTriggerS
1d63a 74 65 70 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  tep==0 ){.    sq
1d63b 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
1d63c 70 57 68 65 72 65 29 3b 0a 20 20 20 20 72 65 74  pWhere);.    ret
1d63d 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 70 54  urn 0;.  }..  pT
1d63e 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 70 20 3d  riggerStep->op =
1d63f 20 54 4b 5f 44 45 4c 45 54 45 3b 0a 20 20 70 54   TK_DELETE;.  pT
1d640 72 69 67 67 65 72 53 74 65 70 2d 3e 74 61 72 67  riggerStep->targ
1d641 65 74 20 20 3d 20 2a 70 54 61 62 6c 65 4e 61 6d  et  = *pTableNam
1d642 65 3b 0a 20 20 70 54 72 69 67 67 65 72 53 74 65  e;.  pTriggerSte
1d643 70 2d 3e 70 57 68 65 72 65 20 3d 20 70 57 68 65  p->pWhere = pWhe
1d644 72 65 3b 0a 20 20 70 54 72 69 67 67 65 72 53 74  re;.  pTriggerSt
1d645 65 70 2d 3e 6f 72 63 6f 6e 66 20 3d 20 4f 45 5f  ep->orconf = OE_
1d646 44 65 66 61 75 6c 74 3b 0a 20 20 73 71 6c 69 74  Default;.  sqlit
1d647 65 50 65 72 73 69 73 74 54 72 69 67 67 65 72 53  ePersistTriggerS
1d648 74 65 70 28 70 54 72 69 67 67 65 72 53 74 65 70  tep(pTriggerStep
1d649 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 70 54 72  );..  return pTr
1d64a 69 67 67 65 72 53 74 65 70 3b 0a 7d 0a 0a 2f 2a  iggerStep;.}../*
1d64b 20 0a 2a 2a 20 52 65 63 75 72 73 69 76 65 6c 79   .** Recursively
1d64c 20 64 65 6c 65 74 65 20 61 20 54 72 69 67 67 65   delete a Trigge
1d64d 72 20 73 74 72 75 63 74 75 72 65 0a 2a 2f 0a 53  r structure.*/.S
1d64e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1d64f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65  id sqlite3Delete
1d650 54 72 69 67 67 65 72 28 54 72 69 67 67 65 72 20  Trigger(Trigger 
1d651 2a 70 54 72 69 67 67 65 72 29 7b 0a 20 20 69 66  *pTrigger){.  if
1d652 28 20 70 54 72 69 67 67 65 72 3d 3d 30 20 29 20  ( pTrigger==0 ) 
1d653 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65  return;.  sqlite
1d654 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 53 74  3DeleteTriggerSt
1d655 65 70 28 70 54 72 69 67 67 65 72 2d 3e 73 74 65  ep(pTrigger->ste
1d656 70 5f 6c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74  p_list);.  sqlit
1d657 65 46 72 65 65 28 70 54 72 69 67 67 65 72 2d 3e  eFree(pTrigger->
1d658 6e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 46  name);.  sqliteF
1d659 72 65 65 28 70 54 72 69 67 67 65 72 2d 3e 74 61  ree(pTrigger->ta
1d65a 62 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  ble);.  sqlite3E
1d65b 78 70 72 44 65 6c 65 74 65 28 70 54 72 69 67 67  xprDelete(pTrigg
1d65c 65 72 2d 3e 70 57 68 65 6e 29 3b 0a 20 20 73 71  er->pWhen);.  sq
1d65d 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74  lite3IdListDelet
1d65e 65 28 70 54 72 69 67 67 65 72 2d 3e 70 43 6f 6c  e(pTrigger->pCol
1d65f 75 6d 6e 73 29 3b 0a 20 20 69 66 28 20 70 54 72  umns);.  if( pTr
1d660 69 67 67 65 72 2d 3e 6e 61 6d 65 54 6f 6b 65 6e  igger->nameToken
1d661 2e 64 79 6e 20 29 20 73 71 6c 69 74 65 46 72 65  .dyn ) sqliteFre
1d662 65 28 28 63 68 61 72 2a 29 70 54 72 69 67 67 65  e((char*)pTrigge
1d663 72 2d 3e 6e 61 6d 65 54 6f 6b 65 6e 2e 7a 29 3b  r->nameToken.z);
1d664 0a 20 20 73 71 6c 69 74 65 46 72 65 65 28 70 54  .  sqliteFree(pT
1d665 72 69 67 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  rigger);.}../*.*
1d666 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
1d667 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 64 72 6f  is called to dro
1d668 70 20 61 20 74 72 69 67 67 65 72 20 66 72 6f 6d  p a trigger from
1d669 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
1d66a 68 65 6d 61 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69  hema. .**.** Thi
1d66b 73 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 20  s may be called 
1d66c 64 69 72 65 63 74 6c 79 20 66 72 6f 6d 20 74 68  directly from th
1d66d 65 20 70 61 72 73 65 72 20 61 6e 64 20 74 68 65  e parser and the
1d66e 72 65 66 6f 72 65 20 69 64 65 6e 74 69 66 69 65  refore identifie
1d66f 73 0a 2a 2a 20 74 68 65 20 74 72 69 67 67 65 72  s.** the trigger
1d670 20 62 79 20 6e 61 6d 65 2e 20 20 54 68 65 20 73   by name.  The s
1d671 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67 67 65  qlite3DropTrigge
1d672 72 50 74 72 28 29 20 72 6f 75 74 69 6e 65 20 64  rPtr() routine d
1d673 6f 65 73 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20  oes the.** same 
1d674 6a 6f 62 20 61 73 20 74 68 69 73 20 72 6f 75 74  job as this rout
1d675 69 6e 65 20 65 78 63 65 70 74 20 69 74 20 74 61  ine except it ta
1d676 6b 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  kes a pointer to
1d677 20 74 68 65 20 74 72 69 67 67 65 72 0a 2a 2a 20   the trigger.** 
1d678 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 74  instead of the t
1d679 72 69 67 67 65 72 20 6e 61 6d 65 2e 0a 2a 2a 2f  rigger name..**/
1d67a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1d67b 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70  void sqlite3Drop
1d67c 54 72 69 67 67 65 72 28 50 61 72 73 65 20 2a 70  Trigger(Parse *p
1d67d 50 61 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a  Parse, SrcList *
1d67e 70 4e 61 6d 65 2c 20 69 6e 74 20 6e 6f 45 72 72  pName, int noErr
1d67f 29 7b 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54  ){.  Trigger *pT
1d680 72 69 67 67 65 72 20 3d 20 30 3b 0a 20 20 69 6e  rigger = 0;.  in
1d681 74 20 69 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  t i;.  const cha
1d682 72 20 2a 7a 44 62 3b 0a 20 20 63 6f 6e 73 74 20  r *zDb;.  const 
1d683 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 69  char *zName;.  i
1d684 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 73 71 6c 69  nt nName;.  sqli
1d685 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
1d686 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20 73 71 6c  ->db;..  if( sql
1d687 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69 6c 65 64  ite3MallocFailed
1d688 28 29 20 29 20 67 6f 74 6f 20 64 72 6f 70 5f 74  () ) goto drop_t
1d689 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a  rigger_cleanup;.
1d68a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21    if( SQLITE_OK!
1d68b 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65  =sqlite3ReadSche
1d68c 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20  ma(pParse) ){.  
1d68d 20 20 67 6f 74 6f 20 64 72 6f 70 5f 74 72 69 67    goto drop_trig
1d68e 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  ger_cleanup;.  }
1d68f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d  ..  assert( pNam
1d690 65 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20  e->nSrc==1 );.  
1d691 7a 44 62 20 3d 20 70 4e 61 6d 65 2d 3e 61 5b 30  zDb = pName->a[0
1d692 5d 2e 7a 44 61 74 61 62 61 73 65 3b 0a 20 20 7a  ].zDatabase;.  z
1d693 4e 61 6d 65 20 3d 20 70 4e 61 6d 65 2d 3e 61 5b  Name = pName->a[
1d694 30 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 6e 4e 61 6d  0].zName;.  nNam
1d695 65 20 3d 20 73 74 72 6c 65 6e 28 7a 4e 61 6d 65  e = strlen(zName
1d696 29 3b 0a 20 20 66 6f 72 28 69 3d 4f 4d 49 54 5f  );.  for(i=OMIT_
1d697 54 45 4d 50 44 42 3b 20 69 3c 64 62 2d 3e 6e 44  TEMPDB; i<db->nD
1d698 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74  b; i++){.    int
1d699 20 6a 20 3d 20 28 69 3c 32 29 20 3f 20 69 5e 31   j = (i<2) ? i^1
1d69a 20 3a 20 69 3b 20 20 2f 2a 20 53 65 61 72 63 68   : i;  /* Search
1d69b 20 54 45 4d 50 20 62 65 66 6f 72 65 20 4d 41 49   TEMP before MAI
1d69c 4e 20 2a 2f 0a 20 20 20 20 69 66 28 20 7a 44 62  N */.    if( zDb
1d69d 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43   && sqlite3StrIC
1d69e 6d 70 28 64 62 2d 3e 61 44 62 5b 6a 5d 2e 7a 4e  mp(db->aDb[j].zN
1d69f 61 6d 65 2c 20 7a 44 62 29 20 29 20 63 6f 6e 74  ame, zDb) ) cont
1d6a0 69 6e 75 65 3b 0a 20 20 20 20 70 54 72 69 67 67  inue;.    pTrigg
1d6a1 65 72 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68  er = sqlite3Hash
1d6a2 46 69 6e 64 28 26 28 64 62 2d 3e 61 44 62 5b 6a  Find(&(db->aDb[j
1d6a3 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 72 69 67 48  ].pSchema->trigH
1d6a4 61 73 68 29 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61  ash), zName, nNa
1d6a5 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 70 54 72  me);.    if( pTr
1d6a6 69 67 67 65 72 20 29 20 62 72 65 61 6b 3b 0a 20  igger ) break;. 
1d6a7 20 7d 0a 20 20 69 66 28 20 21 70 54 72 69 67 67   }.  if( !pTrigg
1d6a8 65 72 20 29 7b 0a 20 20 20 20 69 66 28 20 21 6e  er ){.    if( !n
1d6a9 6f 45 72 72 20 29 7b 0a 20 20 20 20 20 20 73 71  oErr ){.      sq
1d6aa 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
1d6ab 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 74  arse, "no such t
1d6ac 72 69 67 67 65 72 3a 20 25 53 22 2c 20 70 4e 61  rigger: %S", pNa
1d6ad 6d 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20  me, 0);.    }.  
1d6ae 20 20 67 6f 74 6f 20 64 72 6f 70 5f 74 72 69 67    goto drop_trig
1d6af 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  ger_cleanup;.  }
1d6b0 0a 20 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72  .  sqlite3DropTr
1d6b1 69 67 67 65 72 50 74 72 28 70 50 61 72 73 65 2c  iggerPtr(pParse,
1d6b2 20 70 54 72 69 67 67 65 72 29 3b 0a 0a 64 72 6f   pTrigger);..dro
1d6b3 70 5f 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75  p_trigger_cleanu
1d6b4 70 3a 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c  p:.  sqlite3SrcL
1d6b5 69 73 74 44 65 6c 65 74 65 28 70 4e 61 6d 65 29  istDelete(pName)
1d6b6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
1d6b7 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
1d6b8 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75  he Table structu
1d6b9 72 65 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65  re for the table
1d6ba 20 74 68 61 74 20 61 20 74 72 69 67 67 65 72 0a   that a trigger.
1d6bb 2a 2a 20 69 73 20 73 65 74 20 6f 6e 2e 0a 2a 2f  ** is set on..*/
1d6bc 0a 73 74 61 74 69 63 20 54 61 62 6c 65 20 2a 74  .static Table *t
1d6bd 61 62 6c 65 4f 66 54 72 69 67 67 65 72 28 54 72  ableOfTrigger(Tr
1d6be 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 29  igger *pTrigger)
1d6bf 7b 0a 20 20 69 6e 74 20 6e 20 3d 20 73 74 72 6c  {.  int n = strl
1d6c0 65 6e 28 70 54 72 69 67 67 65 72 2d 3e 74 61 62  en(pTrigger->tab
1d6c1 6c 65 29 20 2b 20 31 3b 0a 20 20 72 65 74 75 72  le) + 1;.  retur
1d6c2 6e 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e  n sqlite3HashFin
1d6c3 64 28 26 70 54 72 69 67 67 65 72 2d 3e 70 54 61  d(&pTrigger->pTa
1d6c4 62 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68  bSchema->tblHash
1d6c5 2c 20 70 54 72 69 67 67 65 72 2d 3e 74 61 62 6c  , pTrigger->tabl
1d6c6 65 2c 20 6e 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  e, n);.}.../*.**
1d6c7 20 44 72 6f 70 20 61 20 74 72 69 67 67 65 72 20   Drop a trigger 
1d6c8 67 69 76 65 6e 20 61 20 70 6f 69 6e 74 65 72 20  given a pointer 
1d6c9 74 6f 20 74 68 61 74 20 74 72 69 67 67 65 72 2e  to that trigger.
1d6ca 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56   .*/.SQLITE_PRIV
1d6cb 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1d6cc 44 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 50  DropTriggerPtr(P
1d6cd 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 72  arse *pParse, Tr
1d6ce 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 29  igger *pTrigger)
1d6cf 7b 0a 20 20 54 61 62 6c 65 20 20 20 2a 70 54 61  {.  Table   *pTa
1d6d0 62 6c 65 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a  ble;.  Vdbe *v;.
1d6d1 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
1d6d2 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e  pParse->db;.  in
1d6d3 74 20 69 44 62 3b 0a 0a 20 20 69 44 62 20 3d 20  t iDb;..  iDb = 
1d6d4 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
1d6d5 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c  ndex(pParse->db,
1d6d6 20 70 54 72 69 67 67 65 72 2d 3e 70 53 63 68 65   pTrigger->pSche
1d6d7 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  ma);.  assert( i
1d6d8 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d  Db>=0 && iDb<db-
1d6d9 3e 6e 44 62 20 29 3b 0a 20 20 70 54 61 62 6c 65  >nDb );.  pTable
1d6da 20 3d 20 74 61 62 6c 65 4f 66 54 72 69 67 67 65   = tableOfTrigge
1d6db 72 28 70 54 72 69 67 67 65 72 29 3b 0a 20 20 61  r(pTrigger);.  a
1d6dc 73 73 65 72 74 28 20 70 54 61 62 6c 65 20 29 3b  ssert( pTable );
1d6dd 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62 6c  .  assert( pTabl
1d6de 65 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 72 69  e->pSchema==pTri
1d6df 67 67 65 72 2d 3e 70 53 63 68 65 6d 61 20 7c 7c  gger->pSchema ||
1d6e0 20 69 44 62 3d 3d 31 20 29 3b 0a 23 69 66 6e 64   iDb==1 );.#ifnd
1d6e1 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
1d6e2 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 7b  UTHORIZATION.  {
1d6e3 0a 20 20 20 20 69 6e 74 20 63 6f 64 65 20 3d 20  .    int code = 
1d6e4 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 52 49 47  SQLITE_DROP_TRIG
1d6e5 47 45 52 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63  GER;.    const c
1d6e6 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61  har *zDb = db->a
1d6e7 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20  Db[iDb].zName;. 
1d6e8 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
1d6e9 54 61 62 20 3d 20 53 43 48 45 4d 41 5f 54 41 42  Tab = SCHEMA_TAB
1d6ea 4c 45 28 69 44 62 29 3b 0a 20 20 20 20 69 66 28  LE(iDb);.    if(
1d6eb 20 69 44 62 3d 3d 31 20 29 20 63 6f 64 65 20 3d   iDb==1 ) code =
1d6ec 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d   SQLITE_DROP_TEM
1d6ed 50 5f 54 52 49 47 47 45 52 3b 0a 20 20 20 20 69  P_TRIGGER;.    i
1d6ee 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  f( sqlite3AuthCh
1d6ef 65 63 6b 28 70 50 61 72 73 65 2c 20 63 6f 64 65  eck(pParse, code
1d6f0 2c 20 70 54 72 69 67 67 65 72 2d 3e 6e 61 6d 65  , pTrigger->name
1d6f1 2c 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 2c  , pTable->zName,
1d6f2 20 7a 44 62 29 20 7c 7c 0a 20 20 20 20 20 20 73   zDb) ||.      s
1d6f3 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
1d6f4 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 44  pParse, SQLITE_D
1d6f5 45 4c 45 54 45 2c 20 7a 54 61 62 2c 20 30 2c 20  ELETE, zTab, 0, 
1d6f6 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20 72 65  zDb) ){.      re
1d6f7 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  turn;.    }.  }.
1d6f8 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 47 65 6e  #endif..  /* Gen
1d6f9 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 64 65  erate code to de
1d6fa 73 74 72 6f 79 20 74 68 65 20 64 61 74 61 62 61  stroy the databa
1d6fb 73 65 20 72 65 63 6f 72 64 20 6f 66 20 74 68 65  se record of the
1d6fc 20 74 72 69 67 67 65 72 2e 0a 20 20 2a 2f 0a 20   trigger..  */. 
1d6fd 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 21   assert( pTable!
1d6fe 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 76 20 3d  =0 );.  if( (v =
1d6ff 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
1d700 70 50 61 72 73 65 29 29 21 3d 30 20 29 7b 0a 20  pParse))!=0 ){. 
1d701 20 20 20 69 6e 74 20 62 61 73 65 3b 0a 20 20 20     int base;.   
1d702 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64   static const Vd
1d703 62 65 4f 70 4c 69 73 74 20 64 72 6f 70 54 72 69  beOpList dropTri
1d704 67 67 65 72 5b 5d 20 3d 20 7b 0a 20 20 20 20 20  gger[] = {.     
1d705 20 7b 20 4f 50 5f 52 65 77 69 6e 64 2c 20 20 20   { OP_Rewind,   
1d706 20 20 30 2c 20 41 44 44 52 28 39 29 2c 20 20 30    0, ADDR(9),  0
1d707 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 53 74  },.      { OP_St
1d708 72 69 6e 67 38 2c 20 20 20 20 30 2c 20 30 2c 20  ring8,    0, 0, 
1d709 20 20 20 20 20 20 20 30 7d 2c 20 2f 2a 20 31 20         0}, /* 1 
1d70a 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f 43 6f  */.      { OP_Co
1d70b 6c 75 6d 6e 2c 20 20 20 20 20 30 2c 20 31 2c 20  lumn,     0, 1, 
1d70c 20 20 20 20 20 20 20 30 7d 2c 0a 20 20 20 20 20         0},.     
1d70d 20 7b 20 4f 50 5f 4e 65 2c 20 20 20 20 20 20 20   { OP_Ne,       
1d70e 20 20 30 2c 20 41 44 44 52 28 38 29 2c 20 20 30    0, ADDR(8),  0
1d70f 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 53 74  },.      { OP_St
1d710 72 69 6e 67 38 2c 20 20 20 20 30 2c 20 30 2c 20  ring8,    0, 0, 
1d711 20 20 20 20 20 20 20 22 74 72 69 67 67 65 72 22         "trigger"
1d712 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 43 6f  },.      { OP_Co
1d713 6c 75 6d 6e 2c 20 20 20 20 20 30 2c 20 30 2c 20  lumn,     0, 0, 
1d714 20 20 20 20 20 20 20 30 7d 2c 0a 20 20 20 20 20         0},.     
1d715 20 7b 20 4f 50 5f 4e 65 2c 20 20 20 20 20 20 20   { OP_Ne,       
1d716 20 20 30 2c 20 41 44 44 52 28 38 29 2c 20 20 30    0, ADDR(8),  0
1d717 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 44 65  },.      { OP_De
1d718 6c 65 74 65 2c 20 20 20 20 20 30 2c 20 30 2c 20  lete,     0, 0, 
1d719 20 20 20 20 20 20 20 30 7d 2c 0a 20 20 20 20 20         0},.     
1d71a 20 7b 20 4f 50 5f 4e 65 78 74 2c 20 20 20 20 20   { OP_Next,     
1d71b 20 20 30 2c 20 41 44 44 52 28 31 29 2c 20 20 30    0, ADDR(1),  0
1d71c 7d 2c 20 2f 2a 20 38 20 2a 2f 0a 20 20 20 20 7d  }, /* 8 */.    }
1d71d 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65  ;..    sqlite3Be
1d71e 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f  ginWriteOperatio
1d71f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62  n(pParse, 0, iDb
1d720 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 70  );.    sqlite3Op
1d721 65 6e 4d 61 73 74 65 72 54 61 62 6c 65 28 70 50  enMasterTable(pP
1d722 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20  arse, iDb);.    
1d723 62 61 73 65 20 3d 20 73 71 6c 69 74 65 33 56 64  base = sqlite3Vd
1d724 62 65 41 64 64 4f 70 4c 69 73 74 28 76 2c 20 20  beAddOpList(v,  
1d725 41 72 72 61 79 53 69 7a 65 28 64 72 6f 70 54 72  ArraySize(dropTr
1d726 69 67 67 65 72 29 2c 20 64 72 6f 70 54 72 69 67  igger), dropTrig
1d727 67 65 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ger);.    sqlite
1d728 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 76 2c  3VdbeChangeP3(v,
1d729 20 62 61 73 65 2b 31 2c 20 70 54 72 69 67 67 65   base+1, pTrigge
1d72a 72 2d 3e 6e 61 6d 65 2c 20 30 29 3b 0a 20 20 20  r->name, 0);.   
1d72b 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f   sqlite3ChangeCo
1d72c 6f 6b 69 65 28 64 62 2c 20 76 2c 20 69 44 62 29  okie(db, v, iDb)
1d72d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
1d72e 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f  eAddOp(v, OP_Clo
1d72f 73 65 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  se, 0, 0);.    s
1d730 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c  qlite3VdbeOp3(v,
1d731 20 4f 50 5f 44 72 6f 70 54 72 69 67 67 65 72 2c   OP_DropTrigger,
1d732 20 69 44 62 2c 20 30 2c 20 70 54 72 69 67 67 65   iDb, 0, pTrigge
1d733 72 2d 3e 6e 61 6d 65 2c 20 30 29 3b 0a 20 20 7d  r->name, 0);.  }
1d734 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65  .}../*.** Remove
1d735 20 61 20 74 72 69 67 67 65 72 20 66 72 6f 6d 20   a trigger from 
1d736 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 73 20  the hash tables 
1d737 6f 66 20 74 68 65 20 73 71 6c 69 74 65 2a 20 70  of the sqlite* p
1d738 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54  ointer..*/.SQLIT
1d739 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1d73a 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44  qlite3UnlinkAndD
1d73b 65 6c 65 74 65 54 72 69 67 67 65 72 28 73 71 6c  eleteTrigger(sql
1d73c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44  ite3 *db, int iD
1d73d 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
1d73e 4e 61 6d 65 29 7b 0a 20 20 54 72 69 67 67 65 72  Name){.  Trigger
1d73f 20 2a 70 54 72 69 67 67 65 72 3b 0a 20 20 69 6e   *pTrigger;.  in
1d740 74 20 6e 4e 61 6d 65 20 3d 20 73 74 72 6c 65 6e  t nName = strlen
1d741 28 7a 4e 61 6d 65 29 3b 0a 20 20 70 54 72 69 67  (zName);.  pTrig
1d742 67 65 72 20 3d 20 73 71 6c 69 74 65 33 48 61 73  ger = sqlite3Has
1d743 68 49 6e 73 65 72 74 28 26 28 64 62 2d 3e 61 44  hInsert(&(db->aD
1d744 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e  b[iDb].pSchema->
1d745 74 72 69 67 48 61 73 68 29 2c 0a 20 20 20 20 20  trigHash),.     
1d746 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d747 20 20 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 2c            zName,
1d748 20 6e 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 69 66   nName, 0);.  if
1d749 28 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20  ( pTrigger ){.  
1d74a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 20    Table *pTable 
1d74b 3d 20 74 61 62 6c 65 4f 66 54 72 69 67 67 65 72  = tableOfTrigger
1d74c 28 70 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20  (pTrigger);.    
1d74d 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 21 3d  assert( pTable!=
1d74e 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61  0 );.    if( pTa
1d74f 62 6c 65 2d 3e 70 54 72 69 67 67 65 72 20 3d 3d  ble->pTrigger ==
1d750 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20   pTrigger ){.   
1d751 20 20 20 70 54 61 62 6c 65 2d 3e 70 54 72 69 67     pTable->pTrig
1d752 67 65 72 20 3d 20 70 54 72 69 67 67 65 72 2d 3e  ger = pTrigger->
1d753 70 4e 65 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65  pNext;.    }else
1d754 7b 0a 20 20 20 20 20 20 54 72 69 67 67 65 72 20  {.      Trigger 
1d755 2a 63 63 20 3d 20 70 54 61 62 6c 65 2d 3e 70 54  *cc = pTable->pT
1d756 72 69 67 67 65 72 3b 0a 20 20 20 20 20 20 77 68  rigger;.      wh
1d757 69 6c 65 28 20 63 63 20 29 7b 20 0a 20 20 20 20  ile( cc ){ .    
1d758 20 20 20 20 69 66 28 20 63 63 2d 3e 70 4e 65 78      if( cc->pNex
1d759 74 20 3d 3d 20 70 54 72 69 67 67 65 72 20 29 7b  t == pTrigger ){
1d75a 0a 20 20 20 20 20 20 20 20 20 20 63 63 2d 3e 70  .          cc->p
1d75b 4e 65 78 74 20 3d 20 63 63 2d 3e 70 4e 65 78 74  Next = cc->pNext
1d75c 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20  ->pNext;.       
1d75d 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
1d75e 20 20 7d 0a 20 20 20 20 20 20 20 20 63 63 20 3d    }.        cc =
1d75f 20 63 63 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20   cc->pNext;.    
1d760 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74    }.      assert
1d761 28 63 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  (cc);.    }.    
1d762 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69  sqlite3DeleteTri
1d763 67 67 65 72 28 70 54 72 69 67 67 65 72 29 3b 0a  gger(pTrigger);.
1d764 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d      db->flags |=
1d765 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68   SQLITE_InternCh
1d766 61 6e 67 65 73 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  anges;.  }.}../*
1d767 0a 2a 2a 20 70 45 4c 69 73 74 20 69 73 20 74 68  .** pEList is th
1d768 65 20 53 45 54 20 63 6c 61 75 73 65 20 6f 66 20  e SET clause of 
1d769 61 6e 20 55 50 44 41 54 45 20 73 74 61 74 65 6d  an UPDATE statem
1d76a 65 6e 74 2e 20 20 45 61 63 68 20 65 6e 74 72 79  ent.  Each entry
1d76b 0a 2a 2a 20 69 6e 20 70 45 4c 69 73 74 20 69 73  .** in pEList is
1d76c 20 6f 66 20 74 68 65 20 66 6f 72 6d 61 74 20 3c   of the format <
1d76d 69 64 3e 3d 3c 65 78 70 72 3e 2e 20 20 49 66 20  id>=<expr>.  If 
1d76e 61 6e 79 20 6f 66 20 74 68 65 20 65 6e 74 72 69  any of the entri
1d76f 65 73 0a 2a 2a 20 69 6e 20 70 45 4c 69 73 74 20  es.** in pEList 
1d770 68 61 76 65 20 61 6e 20 3c 69 64 3e 20 77 68 69  have an <id> whi
1d771 63 68 20 6d 61 74 63 68 65 73 20 61 6e 20 69 64  ch matches an id
1d772 65 6e 74 69 66 69 65 72 20 69 6e 20 70 49 64 4c  entifier in pIdL
1d773 69 73 74 2c 0a 2a 2a 20 74 68 65 6e 20 72 65 74  ist,.** then ret
1d774 75 72 6e 20 54 52 55 45 2e 20 20 49 66 20 70 49  urn TRUE.  If pI
1d775 64 4c 69 73 74 3d 3d 4e 55 4c 4c 2c 20 74 68 65  dList==NULL, the
1d776 6e 20 69 74 20 69 73 20 63 6f 6e 73 69 64 65 72  n it is consider
1d777 65 64 20 61 0a 2a 2a 20 77 69 6c 64 63 61 72 64  ed a.** wildcard
1d778 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 61 6e   that matches an
1d779 79 74 68 69 6e 67 2e 20 20 4c 69 6b 65 77 69 73  ything.  Likewis
1d77a 65 20 69 66 20 70 45 4c 69 73 74 3d 3d 4e 55 4c  e if pEList==NUL
1d77b 4c 20 74 68 65 6e 0a 2a 2a 20 69 74 20 6d 61 74  L then.** it mat
1d77c 63 68 65 73 20 61 6e 79 74 68 69 6e 67 20 73 6f  ches anything so
1d77d 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 20 74   always return t
1d77e 72 75 65 2e 20 20 52 65 74 75 72 6e 20 66 61 6c  rue.  Return fal
1d77f 73 65 20 6f 6e 6c 79 0a 2a 2a 20 69 66 20 74 68  se only.** if th
1d780 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 2e  ere is no match.
1d781 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
1d782 68 65 63 6b 43 6f 6c 75 6d 6e 4f 76 65 72 4c 61  heckColumnOverLa
1d783 70 28 49 64 4c 69 73 74 20 2a 70 49 64 4c 69 73  p(IdList *pIdLis
1d784 74 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c  t, ExprList *pEL
1d785 69 73 74 29 7b 0a 20 20 69 6e 74 20 65 3b 0a 20  ist){.  int e;. 
1d786 20 69 66 28 20 21 70 49 64 4c 69 73 74 20 7c 7c   if( !pIdList ||
1d787 20 21 70 45 4c 69 73 74 20 29 20 72 65 74 75 72   !pEList ) retur
1d788 6e 20 31 3b 0a 20 20 66 6f 72 28 65 3d 30 3b 20  n 1;.  for(e=0; 
1d789 65 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b  e<pEList->nExpr;
1d78a 20 65 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73   e++){.    if( s
1d78b 71 6c 69 74 65 33 49 64 4c 69 73 74 49 6e 64 65  qlite3IdListInde
1d78c 78 28 70 49 64 4c 69 73 74 2c 20 70 45 4c 69 73  x(pIdList, pELis
1d78d 74 2d 3e 61 5b 65 5d 2e 7a 4e 61 6d 65 29 3e 3d  t->a[e].zName)>=
1d78e 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  0 ) return 1;.  
1d78f 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 20 0a 7d  }.  return 0; .}
1d790 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ../*.** Return a
1d791 20 62 69 74 20 76 65 63 74 6f 72 20 74 6f 20 69   bit vector to i
1d792 6e 64 69 63 61 74 65 20 77 68 61 74 20 6b 69 6e  ndicate what kin
1d793 64 20 6f 66 20 74 72 69 67 67 65 72 73 20 65 78  d of triggers ex
1d794 69 73 74 20 66 6f 72 20 6f 70 65 72 61 74 69 6f  ist for operatio
1d795 6e 0a 2a 2a 20 22 6f 70 22 20 6f 6e 20 74 61 62  n.** "op" on tab
1d796 6c 65 20 70 54 61 62 2e 20 20 49 66 20 70 43 68  le pTab.  If pCh
1d797 61 6e 67 65 73 20 69 73 20 6e 6f 74 20 4e 55 4c  anges is not NUL
1d798 4c 20 74 68 65 6e 20 69 74 20 69 73 20 61 20 6c  L then it is a l
1d799 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 0a 2a  ist of columns.*
1d79a 2a 20 74 68 61 74 20 61 72 65 20 62 65 69 6e 67  * that are being
1d79b 20 75 70 64 61 74 65 64 2e 20 20 54 72 69 67 67   updated.  Trigg
1d79c 65 72 73 20 6f 6e 6c 79 20 6d 61 74 63 68 20 69  ers only match i
1d79d 66 20 74 68 65 20 4f 4e 20 63 6c 61 75 73 65 20  f the ON clause 
1d79e 6f 66 20 74 68 65 0a 2a 2a 20 74 72 69 67 67 65  of the.** trigge
1d79f 72 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 76 65  r definition ove
1d7a0 72 6c 61 70 73 20 74 68 65 20 73 65 74 20 6f 66  rlaps the set of
1d7a1 20 63 6f 6c 75 6d 6e 73 20 62 65 69 6e 67 20 75   columns being u
1d7a2 70 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  pdated..**.** Th
1d7a3 65 20 72 65 74 75 72 6e 65 64 20 62 69 74 20 76  e returned bit v
1d7a4 65 63 74 6f 72 20 69 73 20 73 6f 6d 65 20 63 6f  ector is some co
1d7a5 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 54 52 49  mbination of TRI
1d7a6 47 47 45 52 5f 42 45 46 4f 52 45 20 61 6e 64 0a  GGER_BEFORE and.
1d7a7 2a 2a 20 54 52 49 47 47 45 52 5f 41 46 54 45 52  ** TRIGGER_AFTER
1d7a8 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1d7a9 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 54  ATE int sqlite3T
1d7aa 72 69 67 67 65 72 73 45 78 69 73 74 28 0a 20 20  riggersExist(.  
1d7ab 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
1d7ac 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20          /* Used 
1d7ad 74 6f 20 63 68 65 63 6b 20 66 6f 72 20 72 65 63  to check for rec
1d7ae 75 72 73 69 76 65 20 74 72 69 67 67 65 72 73 20  ursive triggers 
1d7af 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  */.  Table *pTab
1d7b0 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
1d7b1 54 68 65 20 74 61 62 6c 65 20 74 68 65 20 63 6f  The table the co
1d7b2 6e 74 61 69 6e 73 20 74 68 65 20 74 72 69 67 67  ntains the trigg
1d7b3 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c  ers */.  int op,
1d7b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d7b5 20 2f 2a 20 6f 6e 65 20 6f 66 20 54 4b 5f 44 45   /* one of TK_DE
1d7b6 4c 45 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 2c  LETE, TK_INSERT,
1d7b7 20 54 4b 5f 55 50 44 41 54 45 20 2a 2f 0a 20 20   TK_UPDATE */.  
1d7b8 45 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e 67  ExprList *pChang
1d7b9 65 73 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d  es      /* Colum
1d7ba 6e 73 20 74 68 61 74 20 63 68 61 6e 67 65 20 69  ns that change i
1d7bb 6e 20 61 6e 20 55 50 44 41 54 45 20 73 74 61 74  n an UPDATE stat
1d7bc 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 54 72  ement */.){.  Tr
1d7bd 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b  igger *pTrigger;
1d7be 0a 20 20 69 6e 74 20 6d 61 73 6b 20 3d 20 30 3b  .  int mask = 0;
1d7bf 0a 0a 20 20 70 54 72 69 67 67 65 72 20 3d 20 49  ..  pTrigger = I
1d7c0 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 3f  sVirtual(pTab) ?
1d7c1 20 30 20 3a 20 70 54 61 62 2d 3e 70 54 72 69 67   0 : pTab->pTrig
1d7c2 67 65 72 3b 0a 20 20 77 68 69 6c 65 28 20 70 54  ger;.  while( pT
1d7c3 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20 69 66  rigger ){.    if
1d7c4 28 20 70 54 72 69 67 67 65 72 2d 3e 6f 70 3d 3d  ( pTrigger->op==
1d7c5 6f 70 20 26 26 20 63 68 65 63 6b 43 6f 6c 75 6d  op && checkColum
1d7c6 6e 4f 76 65 72 4c 61 70 28 70 54 72 69 67 67 65  nOverLap(pTrigge
1d7c7 72 2d 3e 70 43 6f 6c 75 6d 6e 73 2c 20 70 43 68  r->pColumns, pCh
1d7c8 61 6e 67 65 73 29 20 29 7b 0a 20 20 20 20 20 20  anges) ){.      
1d7c9 6d 61 73 6b 20 7c 3d 20 70 54 72 69 67 67 65 72  mask |= pTrigger
1d7ca 2d 3e 74 72 5f 74 6d 3b 0a 20 20 20 20 7d 0a 20  ->tr_tm;.    }. 
1d7cb 20 20 20 70 54 72 69 67 67 65 72 20 3d 20 70 54     pTrigger = pT
1d7cc 72 69 67 67 65 72 2d 3e 70 4e 65 78 74 3b 0a 20  rigger->pNext;. 
1d7cd 20 7d 0a 20 20 72 65 74 75 72 6e 20 6d 61 73 6b   }.  return mask
1d7ce 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  ;.}../*.** Conve
1d7cf 72 74 20 74 68 65 20 70 53 74 65 70 2d 3e 74 61  rt the pStep->ta
1d7d0 72 67 65 74 20 74 6f 6b 65 6e 20 69 6e 74 6f 20  rget token into 
1d7d1 61 20 53 72 63 4c 69 73 74 20 61 6e 64 20 72 65  a SrcList and re
1d7d2 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a 2a  turn a pointer.*
1d7d3 2a 20 74 6f 20 74 68 61 74 20 53 72 63 4c 69 73  * to that SrcLis
1d7d4 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  t..**.** This ro
1d7d5 75 74 69 6e 65 20 61 64 64 73 20 61 20 73 70 65  utine adds a spe
1d7d6 63 69 66 69 63 20 64 61 74 61 62 61 73 65 20 6e  cific database n
1d7d7 61 6d 65 2c 20 69 66 20 6e 65 65 64 65 64 2c 20  ame, if needed, 
1d7d8 74 6f 20 74 68 65 20 74 61 72 67 65 74 20 77 68  to the target wh
1d7d9 65 6e 0a 2a 2a 20 66 6f 72 6d 69 6e 67 20 74 68  en.** forming th
1d7da 65 20 53 72 63 4c 69 73 74 2e 20 20 54 68 69 73  e SrcList.  This
1d7db 20 70 72 65 76 65 6e 74 73 20 61 20 74 72 69 67   prevents a trig
1d7dc 67 65 72 20 69 6e 20 6f 6e 65 20 64 61 74 61 62  ger in one datab
1d7dd 61 73 65 20 66 72 6f 6d 0a 2a 2a 20 72 65 66 65  ase from.** refe
1d7de 72 72 69 6e 67 20 74 6f 20 61 20 74 61 72 67 65  rring to a targe
1d7df 74 20 69 6e 20 61 6e 6f 74 68 65 72 20 64 61 74  t in another dat
1d7e0 61 62 61 73 65 2e 20 20 41 6e 20 65 78 63 65 70  abase.  An excep
1d7e1 74 69 6f 6e 20 69 73 20 77 68 65 6e 20 74 68 65  tion is when the
1d7e2 0a 2a 2a 20 74 72 69 67 67 65 72 20 69 73 20 69  .** trigger is i
1d7e3 6e 20 54 45 4d 50 20 69 6e 20 77 68 69 63 68 20  n TEMP in which 
1d7e4 63 61 73 65 20 69 74 20 63 61 6e 20 72 65 66 65  case it can refe
1d7e5 72 20 74 6f 20 61 6e 79 20 6f 74 68 65 72 20 64  r to any other d
1d7e6 61 74 61 62 61 73 65 20 69 74 0a 2a 2a 20 77 61  atabase it.** wa
1d7e7 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53  nts..*/.static S
1d7e8 72 63 4c 69 73 74 20 2a 74 61 72 67 65 74 53 72  rcList *targetSr
1d7e9 63 4c 69 73 74 28 0a 20 20 50 61 72 73 65 20 2a  cList(.  Parse *
1d7ea 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a  pParse,       /*
1d7eb 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e   The parsing con
1d7ec 74 65 78 74 20 2a 2f 0a 20 20 54 72 69 67 67 65  text */.  Trigge
1d7ed 72 53 74 65 70 20 2a 70 53 74 65 70 20 20 20 2f  rStep *pStep   /
1d7ee 2a 20 54 68 65 20 74 72 69 67 67 65 72 20 63 6f  * The trigger co
1d7ef 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 72  ntaining the tar
1d7f0 67 65 74 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a  get token */.){.
1d7f1 20 20 54 6f 6b 65 6e 20 73 44 62 3b 20 20 20 20    Token sDb;    
1d7f2 20 20 20 20 20 20 20 2f 2a 20 44 75 6d 6d 79 20         /* Dummy 
1d7f3 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 74 6f  database name to
1d7f4 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62  ken */.  int iDb
1d7f5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
1d7f6 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61   Index of the da
1d7f7 74 61 62 61 73 65 20 74 6f 20 75 73 65 20 2a 2f  tabase to use */
1d7f8 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63  .  SrcList *pSrc
1d7f9 3b 20 20 20 20 20 20 20 2f 2a 20 53 72 63 4c 69  ;       /* SrcLi
1d7fa 73 74 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65  st to be returne
1d7fb 64 20 2a 2f 0a 0a 20 20 69 44 62 20 3d 20 73 71  d */..  iDb = sq
1d7fc 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64  lite3SchemaToInd
1d7fd 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  ex(pParse->db, p
1d7fe 53 74 65 70 2d 3e 70 54 72 69 67 2d 3e 70 53 63  Step->pTrig->pSc
1d7ff 68 65 6d 61 29 3b 0a 20 20 69 66 28 20 69 44 62  hema);.  if( iDb
1d800 3d 3d 30 20 7c 7c 20 69 44 62 3e 3d 32 20 29 7b  ==0 || iDb>=2 ){
1d801 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 44 62  .    assert( iDb
1d802 3c 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6e 44 62  <pParse->db->nDb
1d803 20 29 3b 0a 20 20 20 20 73 44 62 2e 7a 20 3d 20   );.    sDb.z = 
1d804 28 75 38 2a 29 70 50 61 72 73 65 2d 3e 64 62 2d  (u8*)pParse->db-
1d805 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b  >aDb[iDb].zName;
1d806 0a 20 20 20 20 73 44 62 2e 6e 20 3d 20 73 74 72  .    sDb.n = str
1d807 6c 65 6e 28 28 63 68 61 72 2a 29 73 44 62 2e 7a  len((char*)sDb.z
1d808 29 3b 0a 20 20 20 20 70 53 72 63 20 3d 20 73 71  );.    pSrc = sq
1d809 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65  lite3SrcListAppe
1d80a 6e 64 28 30 2c 20 26 73 44 62 2c 20 26 70 53 74  nd(0, &sDb, &pSt
1d80b 65 70 2d 3e 74 61 72 67 65 74 29 3b 0a 20 20 7d  ep->target);.  }
1d80c 20 65 6c 73 65 20 7b 0a 20 20 20 20 70 53 72 63   else {.    pSrc
1d80d 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73   = sqlite3SrcLis
1d80e 74 41 70 70 65 6e 64 28 30 2c 20 26 70 53 74 65  tAppend(0, &pSte
1d80f 70 2d 3e 74 61 72 67 65 74 2c 20 30 29 3b 0a 20  p->target, 0);. 
1d810 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 53 72 63   }.  return pSrc
1d811 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  ;.}../*.** Gener
1d812 61 74 65 20 56 44 42 45 20 63 6f 64 65 20 66 6f  ate VDBE code fo
1d813 72 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 73  r zero or more s
1d814 74 61 74 65 6d 65 6e 74 73 20 69 6e 73 69 64 65  tatements inside
1d815 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61 0a 2a   the body of a.*
1d816 2a 20 74 72 69 67 67 65 72 2e 20 20 0a 2a 2f 0a  * trigger.  .*/.
1d817 73 74 61 74 69 63 20 69 6e 74 20 63 6f 64 65 54  static int codeT
1d818 72 69 67 67 65 72 50 72 6f 67 72 61 6d 28 0a 20  riggerProgram(. 
1d819 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
1d81a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1d81b 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74  e parser context
1d81c 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 65   */.  TriggerSte
1d81d 70 20 2a 70 53 74 65 70 4c 69 73 74 2c 20 20 20  p *pStepList,   
1d81e 2f 2a 20 4c 69 73 74 20 6f 66 20 73 74 61 74 65  /* List of state
1d81f 6d 65 6e 74 73 20 69 6e 73 69 64 65 20 74 68 65  ments inside the
1d820 20 74 72 69 67 67 65 72 20 62 6f 64 79 20 2a 2f   trigger body */
1d821 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 69 6e 20  .  int orconfin 
1d822 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1d823 43 6f 6e 66 6c 69 63 74 20 61 6c 67 6f 72 69 74  Conflict algorit
1d824 68 6d 2e 20 28 4f 45 5f 41 62 6f 72 74 2c 20 65  hm. (OE_Abort, e
1d825 74 63 29 20 2a 2f 20 20 0a 29 7b 0a 20 20 54 72  tc) */  .){.  Tr
1d826 69 67 67 65 72 53 74 65 70 20 2a 20 70 54 72 69  iggerStep * pTri
1d827 67 67 65 72 53 74 65 70 20 3d 20 70 53 74 65 70  ggerStep = pStep
1d828 4c 69 73 74 3b 0a 20 20 69 6e 74 20 6f 72 63 6f  List;.  int orco
1d829 6e 66 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  nf;.  Vdbe *v = 
1d82a 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 0a  pParse->pVdbe;..
1d82b 20 20 61 73 73 65 72 74 28 20 70 54 72 69 67 67    assert( pTrigg
1d82c 65 72 53 74 65 70 21 3d 30 20 29 3b 0a 20 20 61  erStep!=0 );.  a
1d82d 73 73 65 72 74 28 20 76 21 3d 30 20 29 3b 0a 20  ssert( v!=0 );. 
1d82e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1d82f 70 28 76 2c 20 4f 50 5f 43 6f 6e 74 65 78 74 50  p(v, OP_ContextP
1d830 75 73 68 2c 20 30 2c 20 30 29 3b 0a 20 20 56 64  ush, 0, 0);.  Vd
1d831 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 23  beComment((v, "#
1d832 20 62 65 67 69 6e 20 74 72 69 67 67 65 72 20 25   begin trigger %
1d833 73 22 2c 20 70 53 74 65 70 4c 69 73 74 2d 3e 70  s", pStepList->p
1d834 54 72 69 67 2d 3e 6e 61 6d 65 29 29 3b 0a 20 20  Trig->name));.  
1d835 77 68 69 6c 65 28 20 70 54 72 69 67 67 65 72 53  while( pTriggerS
1d836 74 65 70 20 29 7b 0a 20 20 20 20 6f 72 63 6f 6e  tep ){.    orcon
1d837 66 20 3d 20 28 6f 72 63 6f 6e 66 69 6e 20 3d 3d  f = (orconfin ==
1d838 20 4f 45 5f 44 65 66 61 75 6c 74 29 3f 70 54 72   OE_Default)?pTr
1d839 69 67 67 65 72 53 74 65 70 2d 3e 6f 72 63 6f 6e  iggerStep->orcon
1d83a 66 3a 6f 72 63 6f 6e 66 69 6e 3b 0a 20 20 20 20  f:orconfin;.    
1d83b 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63  pParse->trigStac
1d83c 6b 2d 3e 6f 72 63 6f 6e 66 20 3d 20 6f 72 63 6f  k->orconf = orco
1d83d 6e 66 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20  nf;.    switch( 
1d83e 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 70  pTriggerStep->op
1d83f 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 54   ){.      case T
1d840 4b 5f 53 45 4c 45 43 54 3a 20 7b 0a 09 53 65 6c  K_SELECT: {..Sel
1d841 65 63 74 20 2a 73 73 20 3d 20 73 71 6c 69 74 65  ect *ss = sqlite
1d842 33 53 65 6c 65 63 74 44 75 70 28 70 54 72 69 67  3SelectDup(pTrig
1d843 67 65 72 53 74 65 70 2d 3e 70 53 65 6c 65 63 74  gerStep->pSelect
1d844 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73  );.        if( s
1d845 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  s ){.          s
1d846 71 6c 69 74 65 33 53 65 6c 65 63 74 52 65 73 6f  qlite3SelectReso
1d847 6c 76 65 28 70 50 61 72 73 65 2c 20 73 73 2c 20  lve(pParse, ss, 
1d848 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  0);.          sq
1d849 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72  lite3Select(pPar
1d84a 73 65 2c 20 73 73 2c 20 53 52 54 5f 44 69 73 63  se, ss, SRT_Disc
1d84b 61 72 64 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  ard, 0, 0, 0, 0,
1d84c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73   0);.          s
1d84d 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
1d84e 74 65 28 73 73 29 3b 0a 20 20 20 20 20 20 20 20  te(ss);.        
1d84f 7d 0a 09 62 72 65 61 6b 3b 0a 20 20 20 20 20 20  }..break;.      
1d850 7d 0a 20 20 20 20 20 20 63 61 73 65 20 54 4b 5f  }.      case TK_
1d851 55 50 44 41 54 45 3a 20 7b 0a 20 20 20 20 20 20  UPDATE: {.      
1d852 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b    SrcList *pSrc;
1d853 0a 20 20 20 20 20 20 20 20 70 53 72 63 20 3d 20  .        pSrc = 
1d854 74 61 72 67 65 74 53 72 63 4c 69 73 74 28 70 50  targetSrcList(pP
1d855 61 72 73 65 2c 20 70 54 72 69 67 67 65 72 53 74  arse, pTriggerSt
1d856 65 70 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ep);.        sql
1d857 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1d858 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74 2c 20   OP_ResetCount, 
1d859 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73  0, 0);.        s
1d85a 71 6c 69 74 65 33 55 70 64 61 74 65 28 70 50 61  qlite3Update(pPa
1d85b 72 73 65 2c 20 70 53 72 63 2c 0a 09 09 73 71 6c  rse, pSrc,...sql
1d85c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28  ite3ExprListDup(
1d85d 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70 45  pTriggerStep->pE
1d85e 78 70 72 4c 69 73 74 29 2c 20 0a 09 09 73 71 6c  xprList), ...sql
1d85f 69 74 65 33 45 78 70 72 44 75 70 28 70 54 72 69  ite3ExprDup(pTri
1d860 67 67 65 72 53 74 65 70 2d 3e 70 57 68 65 72 65  ggerStep->pWhere
1d861 29 2c 20 6f 72 63 6f 6e 66 29 3b 0a 20 20 20 20  ), orconf);.    
1d862 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1d863 64 64 4f 70 28 76 2c 20 4f 50 5f 52 65 73 65 74  ddOp(v, OP_Reset
1d864 43 6f 75 6e 74 2c 20 31 2c 20 30 29 3b 0a 20 20  Count, 1, 0);.  
1d865 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1d866 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20     }.      case 
1d867 54 4b 5f 49 4e 53 45 52 54 3a 20 7b 0a 20 20 20  TK_INSERT: {.   
1d868 20 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53       SrcList *pS
1d869 72 63 3b 0a 20 20 20 20 20 20 20 20 70 53 72 63  rc;.        pSrc
1d86a 20 3d 20 74 61 72 67 65 74 53 72 63 4c 69 73 74   = targetSrcList
1d86b 28 70 50 61 72 73 65 2c 20 70 54 72 69 67 67 65  (pParse, pTrigge
1d86c 72 53 74 65 70 29 3b 0a 20 20 20 20 20 20 20 20  rStep);.        
1d86d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1d86e 28 76 2c 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e  (v, OP_ResetCoun
1d86f 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20  t, 0, 0);.      
1d870 20 20 73 71 6c 69 74 65 33 49 6e 73 65 72 74 28    sqlite3Insert(
1d871 70 50 61 72 73 65 2c 20 70 53 72 63 2c 0a 20 20  pParse, pSrc,.  
1d872 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
1d873 78 70 72 4c 69 73 74 44 75 70 28 70 54 72 69 67  xprListDup(pTrig
1d874 67 65 72 53 74 65 70 2d 3e 70 45 78 70 72 4c 69  gerStep->pExprLi
1d875 73 74 29 2c 20 0a 20 20 20 20 20 20 20 20 20 20  st), .          
1d876 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70  sqlite3SelectDup
1d877 28 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70  (pTriggerStep->p
1d878 53 65 6c 65 63 74 29 2c 20 0a 20 20 20 20 20 20  Select), .      
1d879 20 20 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73      sqlite3IdLis
1d87a 74 44 75 70 28 70 54 72 69 67 67 65 72 53 74 65  tDup(pTriggerSte
1d87b 70 2d 3e 70 49 64 4c 69 73 74 29 2c 20 6f 72 63  p->pIdList), orc
1d87c 6f 6e 66 29 3b 0a 20 20 20 20 20 20 20 20 73 71  onf);.        sq
1d87d 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1d87e 2c 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74 2c  , OP_ResetCount,
1d87f 20 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20   1, 0);.        
1d880 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
1d881 20 20 20 20 20 63 61 73 65 20 54 4b 5f 44 45 4c       case TK_DEL
1d882 45 54 45 3a 20 7b 0a 20 20 20 20 20 20 20 20 53  ETE: {.        S
1d883 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 0a 20 20  rcList *pSrc;.  
1d884 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1d885 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 52 65 73  eAddOp(v, OP_Res
1d886 65 74 43 6f 75 6e 74 2c 20 30 2c 20 30 29 3b 0a  etCount, 0, 0);.
1d887 20 20 20 20 20 20 20 20 70 53 72 63 20 3d 20 74          pSrc = t
1d888 61 72 67 65 74 53 72 63 4c 69 73 74 28 70 50 61  argetSrcList(pPa
1d889 72 73 65 2c 20 70 54 72 69 67 67 65 72 53 74 65  rse, pTriggerSte
1d88a 70 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  p);.        sqli
1d88b 74 65 33 44 65 6c 65 74 65 46 72 6f 6d 28 70 50  te3DeleteFrom(pP
1d88c 61 72 73 65 2c 20 70 53 72 63 2c 20 73 71 6c 69  arse, pSrc, sqli
1d88d 74 65 33 45 78 70 72 44 75 70 28 70 54 72 69 67  te3ExprDup(pTrig
1d88e 67 65 72 53 74 65 70 2d 3e 70 57 68 65 72 65 29  gerStep->pWhere)
1d88f 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
1d890 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1d891 50 5f 52 65 73 65 74 43 6f 75 6e 74 2c 20 31 2c  P_ResetCount, 1,
1d892 20 30 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65   0);.        bre
1d893 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
1d894 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20    default:.     
1d895 20 20 20 61 73 73 65 72 74 28 30 29 3b 0a 20 20     assert(0);.  
1d896 20 20 7d 20 0a 20 20 20 20 70 54 72 69 67 67 65    } .    pTrigge
1d897 72 53 74 65 70 20 3d 20 70 54 72 69 67 67 65 72  rStep = pTrigger
1d898 53 74 65 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d  Step->pNext;.  }
1d899 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
1d89a 64 4f 70 28 76 2c 20 4f 50 5f 43 6f 6e 74 65 78  dOp(v, OP_Contex
1d89b 74 50 6f 70 2c 20 30 2c 20 30 29 3b 0a 20 20 56  tPop, 0, 0);.  V
1d89c 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
1d89d 23 20 65 6e 64 20 74 72 69 67 67 65 72 20 25 73  # end trigger %s
1d89e 22 2c 20 70 53 74 65 70 4c 69 73 74 2d 3e 70 54  ", pStepList->pT
1d89f 72 69 67 2d 3e 6e 61 6d 65 29 29 3b 0a 0a 20 20  rig->name));..  
1d8a0 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
1d8a1 2a 2a 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65  ** This is calle
1d8a2 64 20 74 6f 20 63 6f 64 65 20 46 4f 52 20 45 41  d to code FOR EA
1d8a3 43 48 20 52 4f 57 20 74 72 69 67 67 65 72 73 2e  CH ROW triggers.
1d8a4 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20  .**.** When the 
1d8a5 63 6f 64 65 20 74 68 61 74 20 74 68 69 73 20 66  code that this f
1d8a6 75 6e 63 74 69 6f 6e 20 67 65 6e 65 72 61 74 65  unction generate
1d8a7 73 20 69 73 20 65 78 65 63 75 74 65 64 2c 20 74  s is executed, t
1d8a8 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a  he following .**
1d8a9 20 6d 75 73 74 20 62 65 20 74 72 75 65 3a 0a 2a   must be true:.*
1d8aa 2a 0a 2a 2a 20 31 2e 20 4e 6f 20 63 75 72 73 6f  *.** 1. No curso
1d8ab 72 73 20 6d 61 79 20 62 65 20 6f 70 65 6e 20 69  rs may be open i
1d8ac 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  n the main datab
1d8ad 61 73 65 2e 20 20 28 42 75 74 20 6e 65 77 49 64  ase.  (But newId
1d8ae 78 20 61 6e 64 20 6f 6c 64 49 64 78 0a 2a 2a 20  x and oldIdx.** 
1d8af 20 20 20 63 61 6e 20 62 65 20 69 6e 64 69 63 65     can be indice
1d8b0 73 20 6f 66 20 63 75 72 73 6f 72 73 20 69 6e 20  s of cursors in 
1d8b1 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73  temporary tables
1d8b2 2e 20 20 53 65 65 20 62 65 6c 6f 77 2e 29 0a 2a  .  See below.).*
1d8b3 2a 0a 2a 2a 20 32 2e 20 49 66 20 74 68 65 20 74  *.** 2. If the t
1d8b4 72 69 67 67 65 72 73 20 62 65 69 6e 67 20 63 6f  riggers being co
1d8b5 64 65 64 20 61 72 65 20 4f 4e 20 49 4e 53 45 52  ded are ON INSER
1d8b6 54 20 6f 72 20 4f 4e 20 55 50 44 41 54 45 20 74  T or ON UPDATE t
1d8b7 72 69 67 67 65 72 73 2c 20 74 68 65 6e 0a 2a 2a  riggers, then.**
1d8b8 20 20 20 20 61 20 74 65 6d 70 6f 72 61 72 79 20      a temporary 
1d8b9 76 64 62 65 20 63 75 72 73 6f 72 20 28 69 6e 64  vdbe cursor (ind
1d8ba 65 78 20 6e 65 77 49 64 78 29 20 6d 75 73 74 20  ex newIdx) must 
1d8bb 62 65 20 6f 70 65 6e 20 61 6e 64 20 70 6f 69 6e  be open and poin
1d8bc 74 69 6e 67 20 61 74 0a 2a 2a 20 20 20 20 61 20  ting at.**    a 
1d8bd 72 6f 77 20 63 6f 6e 74 61 69 6e 69 6e 67 20 76  row containing v
1d8be 61 6c 75 65 73 20 74 6f 20 62 65 20 73 75 62 73  alues to be subs
1d8bf 74 69 74 75 74 65 64 20 66 6f 72 20 6e 65 77 2e  tituted for new.
1d8c0 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e  * expressions in
1d8c1 20 74 68 65 0a 2a 2a 20 20 20 20 74 72 69 67 67   the.**    trigg
1d8c2 65 72 20 70 72 6f 67 72 61 6d 28 73 29 2e 0a 2a  er program(s)..*
1d8c3 2a 0a 2a 2a 20 33 2e 20 49 66 20 74 68 65 20 74  *.** 3. If the t
1d8c4 72 69 67 67 65 72 73 20 62 65 69 6e 67 20 63 6f  riggers being co
1d8c5 64 65 64 20 61 72 65 20 4f 4e 20 44 45 4c 45 54  ded are ON DELET
1d8c6 45 20 6f 72 20 4f 4e 20 55 50 44 41 54 45 20 74  E or ON UPDATE t
1d8c7 72 69 67 67 65 72 73 2c 20 74 68 65 6e 0a 2a 2a  riggers, then.**
1d8c8 20 20 20 20 61 20 74 65 6d 70 6f 72 61 72 79 20      a temporary 
1d8c9 76 64 62 65 20 63 75 72 73 6f 72 20 28 69 6e 64  vdbe cursor (ind
1d8ca 65 78 20 6f 6c 64 49 64 78 29 20 6d 75 73 74 20  ex oldIdx) must 
1d8cb 62 65 20 6f 70 65 6e 20 61 6e 64 20 70 6f 69 6e  be open and poin
1d8cc 74 69 6e 67 20 61 74 0a 2a 2a 20 20 20 20 61 20  ting at.**    a 
1d8cd 72 6f 77 20 63 6f 6e 74 61 69 6e 69 6e 67 20 76  row containing v
1d8ce 61 6c 75 65 73 20 74 6f 20 62 65 20 73 75 62 73  alues to be subs
1d8cf 74 69 74 75 74 65 64 20 66 6f 72 20 6f 6c 64 2e  tituted for old.
1d8d0 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e  * expressions in
1d8d1 20 74 68 65 0a 2a 2a 20 20 20 20 74 72 69 67 67   the.**    trigg
1d8d2 65 72 20 70 72 6f 67 72 61 6d 28 73 29 2e 0a 2a  er program(s)..*
1d8d3 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  *.*/.SQLITE_PRIV
1d8d4 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43  ATE int sqlite3C
1d8d5 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 0a 20  odeRowTrigger(. 
1d8d6 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
1d8d7 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 20 63        /* Parse c
1d8d8 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20  ontext */.  int 
1d8d9 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  op,             
1d8da 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 55 50   /* One of TK_UP
1d8db 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 2c  DATE, TK_INSERT,
1d8dc 20 54 4b 5f 44 45 4c 45 54 45 20 2a 2f 0a 20 20   TK_DELETE */.  
1d8dd 45 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e 67  ExprList *pChang
1d8de 65 73 2c 20 20 2f 2a 20 43 68 61 6e 67 65 73 20  es,  /* Changes 
1d8df 6c 69 73 74 20 66 6f 72 20 61 6e 79 20 55 50 44  list for any UPD
1d8e0 41 54 45 20 4f 46 20 74 72 69 67 67 65 72 73 20  ATE OF triggers 
1d8e1 2a 2f 0a 20 20 69 6e 74 20 74 72 5f 74 6d 2c 20  */.  int tr_tm, 
1d8e2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
1d8e3 20 6f 66 20 54 52 49 47 47 45 52 5f 42 45 46 4f   of TRIGGER_BEFO
1d8e4 52 45 2c 20 54 52 49 47 47 45 52 5f 41 46 54 45  RE, TRIGGER_AFTE
1d8e5 52 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  R */.  Table *pT
1d8e6 61 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ab,         /* T
1d8e7 68 65 20 74 61 62 6c 65 20 74 6f 20 63 6f 64 65  he table to code
1d8e8 20 74 72 69 67 67 65 72 73 20 66 72 6f 6d 20 2a   triggers from *
1d8e9 2f 0a 20 20 69 6e 74 20 6e 65 77 49 64 78 2c 20  /.  int newIdx, 
1d8ea 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1d8eb 69 6e 64 69 63 65 20 6f 66 20 74 68 65 20 22 6e  indice of the "n
1d8ec 65 77 22 20 72 6f 77 20 74 6f 20 61 63 63 65 73  ew" row to acces
1d8ed 73 20 2a 2f 0a 20 20 69 6e 74 20 6f 6c 64 49 64  s */.  int oldId
1d8ee 78 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  x,          /* T
1d8ef 68 65 20 69 6e 64 69 63 65 20 6f 66 20 74 68 65  he indice of the
1d8f0 20 22 6f 6c 64 22 20 72 6f 77 20 74 6f 20 61 63   "old" row to ac
1d8f1 63 65 73 73 20 2a 2f 0a 20 20 69 6e 74 20 6f 72  cess */.  int or
1d8f2 63 6f 6e 66 2c 20 20 20 20 20 20 20 20 20 20 2f  conf,          /
1d8f3 2a 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f  * ON CONFLICT po
1d8f4 6c 69 63 79 20 2a 2f 0a 20 20 69 6e 74 20 69 67  licy */.  int ig
1d8f5 6e 6f 72 65 4a 75 6d 70 20 20 20 20 20 20 20 2f  noreJump       /
1d8f6 2a 20 49 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f  * Instruction to
1d8f7 20 6a 75 6d 70 20 74 6f 20 66 6f 72 20 52 41 49   jump to for RAI
1d8f8 53 45 28 49 47 4e 4f 52 45 29 20 2a 2f 0a 29 7b  SE(IGNORE) */.){
1d8f9 0a 20 20 54 72 69 67 67 65 72 20 2a 70 3b 0a 20  .  Trigger *p;. 
1d8fa 20 54 72 69 67 67 65 72 53 74 61 63 6b 20 74 72   TriggerStack tr
1d8fb 69 67 53 74 61 63 6b 45 6e 74 72 79 3b 0a 0a 20  igStackEntry;.. 
1d8fc 20 61 73 73 65 72 74 28 6f 70 20 3d 3d 20 54 4b   assert(op == TK
1d8fd 5f 55 50 44 41 54 45 20 7c 7c 20 6f 70 20 3d 3d  _UPDATE || op ==
1d8fe 20 54 4b 5f 49 4e 53 45 52 54 20 7c 7c 20 6f 70   TK_INSERT || op
1d8ff 20 3d 3d 20 54 4b 5f 44 45 4c 45 54 45 29 3b 0a   == TK_DELETE);.
1d900 20 20 61 73 73 65 72 74 28 74 72 5f 74 6d 20 3d    assert(tr_tm =
1d901 3d 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45  = TRIGGER_BEFORE
1d902 20 7c 7c 20 74 72 5f 74 6d 20 3d 3d 20 54 52 49   || tr_tm == TRI
1d903 47 47 45 52 5f 41 46 54 45 52 20 29 3b 0a 0a 20  GGER_AFTER );.. 
1d904 20 61 73 73 65 72 74 28 6e 65 77 49 64 78 20 21   assert(newIdx !
1d905 3d 20 2d 31 20 7c 7c 20 6f 6c 64 49 64 78 20 21  = -1 || oldIdx !
1d906 3d 20 2d 31 29 3b 0a 0a 20 20 66 6f 72 28 70 3d  = -1);..  for(p=
1d907 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72 3b 20  pTab->pTrigger; 
1d908 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a  p; p=p->pNext){.
1d909 20 20 20 20 69 6e 74 20 66 69 72 65 5f 74 68 69      int fire_thi
1d90a 73 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 44  s = 0;..    /* D
1d90b 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72  etermine whether
1d90c 20 77 65 20 73 68 6f 75 6c 64 20 63 6f 64 65 20   we should code 
1d90d 74 68 69 73 20 74 72 69 67 67 65 72 20 2a 2f 0a  this trigger */.
1d90e 20 20 20 20 69 66 28 20 0a 20 20 20 20 20 20 70      if( .      p
1d90f 2d 3e 6f 70 3d 3d 6f 70 20 26 26 20 0a 20 20 20  ->op==op && .   
1d910 20 20 20 70 2d 3e 74 72 5f 74 6d 3d 3d 74 72 5f     p->tr_tm==tr_
1d911 74 6d 20 26 26 20 0a 20 20 20 20 20 20 28 70 2d  tm && .      (p-
1d912 3e 70 53 63 68 65 6d 61 3d 3d 70 2d 3e 70 54 61  >pSchema==p->pTa
1d913 62 53 63 68 65 6d 61 20 7c 7c 20 70 2d 3e 70 53  bSchema || p->pS
1d914 63 68 65 6d 61 3d 3d 70 50 61 72 73 65 2d 3e 64  chema==pParse->d
1d915 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d  b->aDb[1].pSchem
1d916 61 29 20 26 26 0a 20 20 20 20 20 20 28 6f 70 21  a) &&.      (op!
1d917 3d 54 4b 5f 55 50 44 41 54 45 7c 7c 21 70 2d 3e  =TK_UPDATE||!p->
1d918 70 43 6f 6c 75 6d 6e 73 7c 7c 63 68 65 63 6b 43  pColumns||checkC
1d919 6f 6c 75 6d 6e 4f 76 65 72 4c 61 70 28 70 2d 3e  olumnOverLap(p->
1d91a 70 43 6f 6c 75 6d 6e 73 2c 70 43 68 61 6e 67 65  pColumns,pChange
1d91b 73 29 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  s)).    ){.     
1d91c 20 54 72 69 67 67 65 72 53 74 61 63 6b 20 2a 70   TriggerStack *p
1d91d 53 3b 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74  S;      /* Point
1d91e 65 72 20 74 6f 20 74 72 69 67 67 65 72 2d 73 74  er to trigger-st
1d91f 61 63 6b 20 65 6e 74 72 79 20 2a 2f 0a 20 20 20  ack entry */.   
1d920 20 20 20 66 6f 72 28 70 53 3d 70 50 61 72 73 65     for(pS=pParse
1d921 2d 3e 74 72 69 67 53 74 61 63 6b 3b 20 70 53 20  ->trigStack; pS 
1d922 26 26 20 70 21 3d 70 53 2d 3e 70 54 72 69 67 67  && p!=pS->pTrigg
1d923 65 72 3b 20 70 53 3d 70 53 2d 3e 70 4e 65 78 74  er; pS=pS->pNext
1d924 29 7b 7d 0a 20 20 20 20 20 20 69 66 28 20 21 70  ){}.      if( !p
1d925 53 20 29 7b 0a 20 20 20 20 20 20 20 20 66 69 72  S ){.        fir
1d926 65 5f 74 68 69 73 20 3d 20 31 3b 0a 20 20 20 20  e_this = 1;.    
1d927 20 20 7d 0a 23 69 66 20 30 20 20 20 20 2f 2a 20    }.#if 0    /* 
1d928 47 69 76 65 20 6e 6f 20 77 61 72 6e 69 6e 67 20  Give no warning 
1d929 66 6f 72 20 72 65 63 75 72 73 69 76 65 20 74 72  for recursive tr
1d92a 69 67 67 65 72 73 2e 20 20 4a 75 73 74 20 64 6f  iggers.  Just do
1d92b 20 6e 6f 74 20 64 6f 20 74 68 65 6d 20 2a 2f 0a   not do them */.
1d92c 20 20 20 20 20 20 65 6c 73 65 7b 0a 20 20 20 20        else{.    
1d92d 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1d92e 4d 73 67 28 70 50 61 72 73 65 2c 20 22 72 65 63  Msg(pParse, "rec
1d92f 75 72 73 69 76 65 20 74 72 69 67 67 65 72 73 20  ursive triggers 
1d930 6e 6f 74 20 73 75 70 70 6f 72 74 65 64 20 28 25  not supported (%
1d931 73 29 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  s)",.           
1d932 20 70 2d 3e 6e 61 6d 65 29 3b 0a 20 20 20 20 20   p->name);.     
1d933 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1d934 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 7d 0a  _ERROR;.      }.
1d935 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 0a 20  #endif.    }. . 
1d936 20 20 20 69 66 28 20 66 69 72 65 5f 74 68 69 73     if( fire_this
1d937 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 6e   ){.      int en
1d938 64 54 72 69 67 67 65 72 3b 0a 20 20 20 20 20 20  dTrigger;.      
1d939 45 78 70 72 20 2a 20 77 68 65 6e 45 78 70 72 3b  Expr * whenExpr;
1d93a 0a 20 20 20 20 20 20 41 75 74 68 43 6f 6e 74 65  .      AuthConte
1d93b 78 74 20 73 43 6f 6e 74 65 78 74 3b 0a 20 20 20  xt sContext;.   
1d93c 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73     NameContext s
1d93d 4e 43 3b 0a 0a 20 20 20 20 20 20 6d 65 6d 73 65  NC;..      memse
1d93e 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f  t(&sNC, 0, sizeo
1d93f 66 28 73 4e 43 29 29 3b 0a 20 20 20 20 20 20 73  f(sNC));.      s
1d940 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72  NC.pParse = pPar
1d941 73 65 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 50 75  se;..      /* Pu
1d942 73 68 20 61 6e 20 65 6e 74 72 79 20 6f 6e 20 74  sh an entry on t
1d943 6f 20 74 68 65 20 74 72 69 67 67 65 72 20 73 74  o the trigger st
1d944 61 63 6b 20 2a 2f 0a 20 20 20 20 20 20 74 72 69  ack */.      tri
1d945 67 53 74 61 63 6b 45 6e 74 72 79 2e 70 54 72 69  gStackEntry.pTri
1d946 67 67 65 72 20 3d 20 70 3b 0a 20 20 20 20 20 20  gger = p;.      
1d947 74 72 69 67 53 74 61 63 6b 45 6e 74 72 79 2e 6e  trigStackEntry.n
1d948 65 77 49 64 78 20 3d 20 6e 65 77 49 64 78 3b 0a  ewIdx = newIdx;.
1d949 20 20 20 20 20 20 74 72 69 67 53 74 61 63 6b 45        trigStackE
1d94a 6e 74 72 79 2e 6f 6c 64 49 64 78 20 3d 20 6f 6c  ntry.oldIdx = ol
1d94b 64 49 64 78 3b 0a 20 20 20 20 20 20 74 72 69 67  dIdx;.      trig
1d94c 53 74 61 63 6b 45 6e 74 72 79 2e 70 54 61 62 20  StackEntry.pTab 
1d94d 3d 20 70 54 61 62 3b 0a 20 20 20 20 20 20 74 72  = pTab;.      tr
1d94e 69 67 53 74 61 63 6b 45 6e 74 72 79 2e 70 4e 65  igStackEntry.pNe
1d94f 78 74 20 3d 20 70 50 61 72 73 65 2d 3e 74 72 69  xt = pParse->tri
1d950 67 53 74 61 63 6b 3b 0a 20 20 20 20 20 20 74 72  gStack;.      tr
1d951 69 67 53 74 61 63 6b 45 6e 74 72 79 2e 69 67 6e  igStackEntry.ign
1d952 6f 72 65 4a 75 6d 70 20 3d 20 69 67 6e 6f 72 65  oreJump = ignore
1d953 4a 75 6d 70 3b 0a 20 20 20 20 20 20 70 50 61 72  Jump;.      pPar
1d954 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 20 3d 20  se->trigStack = 
1d955 26 74 72 69 67 53 74 61 63 6b 45 6e 74 72 79 3b  &trigStackEntry;
1d956 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 41 75  .      sqlite3Au
1d957 74 68 43 6f 6e 74 65 78 74 50 75 73 68 28 70 50  thContextPush(pP
1d958 61 72 73 65 2c 20 26 73 43 6f 6e 74 65 78 74 2c  arse, &sContext,
1d959 20 70 2d 3e 6e 61 6d 65 29 3b 0a 0a 20 20 20 20   p->name);..    
1d95a 20 20 2f 2a 20 63 6f 64 65 20 74 68 65 20 57 48    /* code the WH
1d95b 45 4e 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20  EN clause */.   
1d95c 20 20 20 65 6e 64 54 72 69 67 67 65 72 20 3d 20     endTrigger = 
1d95d 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
1d95e 61 62 65 6c 28 70 50 61 72 73 65 2d 3e 70 56 64  abel(pParse->pVd
1d95f 62 65 29 3b 0a 20 20 20 20 20 20 77 68 65 6e 45  be);.      whenE
1d960 78 70 72 20 3d 20 73 71 6c 69 74 65 33 45 78 70  xpr = sqlite3Exp
1d961 72 44 75 70 28 70 2d 3e 70 57 68 65 6e 29 3b 0a  rDup(p->pWhen);.
1d962 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
1d963 33 45 78 70 72 52 65 73 6f 6c 76 65 4e 61 6d 65  3ExprResolveName
1d964 73 28 26 73 4e 43 2c 20 77 68 65 6e 45 78 70 72  s(&sNC, whenExpr
1d965 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50 61  ) ){.        pPa
1d966 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 20 3d  rse->trigStack =
1d967 20 74 72 69 67 53 74 61 63 6b 45 6e 74 72 79 2e   trigStackEntry.
1d968 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 73  pNext;.        s
1d969 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
1d96a 28 77 68 65 6e 45 78 70 72 29 3b 0a 20 20 20 20  (whenExpr);.    
1d96b 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
1d96c 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
1d96d 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70  te3ExprIfFalse(p
1d96e 50 61 72 73 65 2c 20 77 68 65 6e 45 78 70 72 2c  Parse, whenExpr,
1d96f 20 65 6e 64 54 72 69 67 67 65 72 2c 20 31 29 3b   endTrigger, 1);
1d970 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
1d971 70 72 44 65 6c 65 74 65 28 77 68 65 6e 45 78 70  prDelete(whenExp
1d972 72 29 3b 0a 0a 20 20 20 20 20 20 63 6f 64 65 54  r);..      codeT
1d973 72 69 67 67 65 72 50 72 6f 67 72 61 6d 28 70 50  riggerProgram(pP
1d974 61 72 73 65 2c 20 70 2d 3e 73 74 65 70 5f 6c 69  arse, p->step_li
1d975 73 74 2c 20 6f 72 63 6f 6e 66 29 3b 20 0a 0a 20  st, orconf); .. 
1d976 20 20 20 20 20 2f 2a 20 50 6f 70 20 74 68 65 20       /* Pop the 
1d977 65 6e 74 72 79 20 6f 66 66 20 74 68 65 20 74 72  entry off the tr
1d978 69 67 67 65 72 20 73 74 61 63 6b 20 2a 2f 0a 20  igger stack */. 
1d979 20 20 20 20 20 70 50 61 72 73 65 2d 3e 74 72 69       pParse->tri
1d97a 67 53 74 61 63 6b 20 3d 20 74 72 69 67 53 74 61  gStack = trigSta
1d97b 63 6b 45 6e 74 72 79 2e 70 4e 65 78 74 3b 0a 20  ckEntry.pNext;. 
1d97c 20 20 20 20 20 73 71 6c 69 74 65 33 41 75 74 68       sqlite3Auth
1d97d 43 6f 6e 74 65 78 74 50 6f 70 28 26 73 43 6f 6e  ContextPop(&sCon
1d97e 74 65 78 74 29 3b 0a 0a 20 20 20 20 20 20 73 71  text);..      sq
1d97f 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
1d980 4c 61 62 65 6c 28 70 50 61 72 73 65 2d 3e 70 56  Label(pParse->pV
1d981 64 62 65 2c 20 65 6e 64 54 72 69 67 67 65 72 29  dbe, endTrigger)
1d982 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
1d983 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66  turn 0;.}.#endif
1d984 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c   /* !defined(SQL
1d985 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
1d986 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ) */../*********
1d987 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 72 69  ***** End of tri
1d988 67 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  gger.c *********
1d989 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d98a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d98b 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
1d98c 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
1d98d 20 75 70 64 61 74 65 2e 63 20 2a 2a 2a 2a 2a 2a   update.c ******
1d98e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d98f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d990 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31  ****/./*.** 2001
1d991 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a   September 15.**
1d992 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
1d993 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
1d994 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
1d995 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
1d996 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
1d997 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
1d998 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
1d999 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
1d99a 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
1d99b 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
1d99c 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
1d99d 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
1d99e 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
1d99f 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
1d9a0 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
1d9a1 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
1d9a2 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
1d9a3 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
1d9a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d9a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d9a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d9a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
1d9a8 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
1d9a9 6e 73 20 43 20 63 6f 64 65 20 72 6f 75 74 69 6e  ns C code routin
1d9aa 65 73 20 74 68 61 74 20 61 72 65 20 63 61 6c 6c  es that are call
1d9ab 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72  ed by the parser
1d9ac 0a 2a 2a 20 74 6f 20 68 61 6e 64 6c 65 20 55 50  .** to handle UP
1d9ad 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 73 2e  DATE statements.
1d9ae 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 75 70 64 61  .**.** $Id: upda
1d9af 74 65 2e 63 2c 76 20 31 2e 31 33 38 20 32 30 30  te.c,v 1.138 200
1d9b0 37 2f 30 36 2f 32 35 20 31 36 3a 32 39 3a 33 34  7/06/25 16:29:34
1d9b1 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70   danielk1977 Exp
1d9b2 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53   $.*/..#ifndef S
1d9b3 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
1d9b4 41 4c 54 41 42 4c 45 0a 2f 2a 20 46 6f 72 77 61  ALTABLE./* Forwa
1d9b5 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 2a  rd declaration *
1d9b6 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 70  /.static void up
1d9b7 64 61 74 65 56 69 72 74 75 61 6c 54 61 62 6c 65  dateVirtualTable
1d9b8 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
1d9b9 65 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  e,       /* The 
1d9ba 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  parsing context 
1d9bb 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53  */.  SrcList *pS
1d9bc 72 63 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65  rc,       /* The
1d9bd 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 74   virtual table t
1d9be 6f 20 62 65 20 6d 6f 64 69 66 69 65 64 20 2a 2f  o be modified */
1d9bf 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20  .  Table *pTab, 
1d9c0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76          /* The v
1d9c1 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a  irtual table */.
1d9c2 20 20 45 78 70 72 4c 69 73 74 20 2a 70 43 68 61    ExprList *pCha
1d9c3 6e 67 65 73 2c 20 20 2f 2a 20 54 68 65 20 63 6f  nges,  /* The co
1d9c4 6c 75 6d 6e 73 20 74 6f 20 63 68 61 6e 67 65 20  lumns to change 
1d9c5 69 6e 20 74 68 65 20 55 50 44 41 54 45 20 73 74  in the UPDATE st
1d9c6 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 45 78 70  atement */.  Exp
1d9c7 72 20 2a 70 52 6f 77 69 64 45 78 70 72 2c 20 20  r *pRowidExpr,  
1d9c8 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20    /* Expression 
1d9c9 75 73 65 64 20 74 6f 20 72 65 63 6f 6d 70 75 74  used to recomput
1d9ca 65 20 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20  e the rowid */. 
1d9cb 20 69 6e 74 20 2a 61 58 52 65 66 2c 20 20 20 20   int *aXRef,    
1d9cc 20 20 20 20 20 20 2f 2a 20 4d 61 70 70 69 6e 67        /* Mapping
1d9cd 20 66 72 6f 6d 20 63 6f 6c 75 6d 6e 73 20 6f 66   from columns of
1d9ce 20 70 54 61 62 20 74 6f 20 65 6e 74 72 69 65 73   pTab to entries
1d9cf 20 69 6e 20 70 43 68 61 6e 67 65 73 20 2a 2f 0a   in pChanges */.
1d9d0 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 20 20    Expr *pWhere  
1d9d1 20 20 20 20 20 20 20 2f 2a 20 57 48 45 52 45 20         /* WHERE 
1d9d2 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 55 50  clause of the UP
1d9d3 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 2a  DATE statement *
1d9d4 2f 0a 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53  /.);.#endif /* S
1d9d5 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
1d9d6 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a  ALTABLE */../*.*
1d9d7 2a 20 54 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  * The most recen
1d9d8 74 6c 79 20 63 6f 64 65 64 20 69 6e 73 74 72 75  tly coded instru
1d9d9 63 74 69 6f 6e 20 77 61 73 20 61 6e 20 4f 50 5f  ction was an OP_
1d9da 43 6f 6c 75 6d 6e 20 74 6f 20 72 65 74 72 69 65  Column to retrie
1d9db 76 65 20 74 68 65 0a 2a 2a 20 69 2d 74 68 20 63  ve the.** i-th c
1d9dc 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 70  olumn of table p
1d9dd 54 61 62 2e 20 54 68 69 73 20 72 6f 75 74 69 6e  Tab. This routin
1d9de 65 20 73 65 74 73 20 74 68 65 20 50 33 20 70 61  e sets the P3 pa
1d9df 72 61 6d 65 74 65 72 20 6f 66 20 74 68 65 20 0a  rameter of the .
1d9e0 2a 2a 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74 6f 20  ** OP_Column to 
1d9e1 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75  the default valu
1d9e2 65 2c 20 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a  e, if any..**.**
1d9e3 20 54 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c   The default val
1d9e4 75 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 69  ue of a column i
1d9e5 73 20 73 70 65 63 69 66 69 65 64 20 62 79 20 61  s specified by a
1d9e6 20 44 45 46 41 55 4c 54 20 63 6c 61 75 73 65 20   DEFAULT clause 
1d9e7 69 6e 20 74 68 65 20 0a 2a 2a 20 63 6f 6c 75 6d  in the .** colum
1d9e8 6e 20 64 65 66 69 6e 69 74 69 6f 6e 2e 20 54 68  n definition. Th
1d9e9 69 73 20 77 61 73 20 65 69 74 68 65 72 20 73 75  is was either su
1d9ea 70 70 6c 69 65 64 20 62 79 20 74 68 65 20 75 73  pplied by the us
1d9eb 65 72 20 77 68 65 6e 20 74 68 65 20 74 61 62 6c  er when the tabl
1d9ec 65 0a 2a 2a 20 77 61 73 20 63 72 65 61 74 65 64  e.** was created
1d9ed 2c 20 6f 72 20 61 64 64 65 64 20 6c 61 74 65 72  , or added later
1d9ee 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 64 65   to the table de
1d9ef 66 69 6e 69 74 69 6f 6e 20 62 79 20 61 6e 20 41  finition by an A
1d9f0 4c 54 45 52 20 54 41 42 4c 45 0a 2a 2a 20 63 6f  LTER TABLE.** co
1d9f1 6d 6d 61 6e 64 2e 20 49 66 20 74 68 65 20 6c 61  mmand. If the la
1d9f2 74 74 65 72 2c 20 74 68 65 6e 20 74 68 65 20 72  tter, then the r
1d9f3 6f 77 2d 72 65 63 6f 72 64 73 20 69 6e 20 74 68  ow-records in th
1d9f4 65 20 74 61 62 6c 65 20 62 74 72 65 65 20 6f 6e  e table btree on
1d9f5 20 64 69 73 6b 0a 2a 2a 20 6d 61 79 20 6e 6f 74   disk.** may not
1d9f6 20 63 6f 6e 74 61 69 6e 20 61 20 76 61 6c 75 65   contain a value
1d9f7 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20   for the column 
1d9f8 61 6e 64 20 74 68 65 20 64 65 66 61 75 6c 74 20  and the default 
1d9f9 76 61 6c 75 65 2c 20 74 61 6b 65 6e 0a 2a 2a 20  value, taken.** 
1d9fa 66 72 6f 6d 20 74 68 65 20 50 33 20 70 61 72 61  from the P3 para
1d9fb 6d 65 74 65 72 20 6f 66 20 74 68 65 20 4f 50 5f  meter of the OP_
1d9fc 43 6f 6c 75 6d 6e 20 69 6e 73 74 72 75 63 74 69  Column instructi
1d9fd 6f 6e 2c 20 69 73 20 72 65 74 75 72 6e 65 64 20  on, is returned 
1d9fe 69 6e 73 74 65 61 64 2e 0a 2a 2a 20 49 66 20 74  instead..** If t
1d9ff 68 65 20 66 6f 72 6d 65 72 2c 20 74 68 65 6e 20  he former, then 
1da00 61 6c 6c 20 72 6f 77 2d 72 65 63 6f 72 64 73 20  all row-records 
1da01 61 72 65 20 67 75 61 72 61 6e 74 65 65 64 20 74  are guaranteed t
1da02 6f 20 69 6e 63 6c 75 64 65 20 61 20 76 61 6c 75  o include a valu
1da03 65 0a 2a 2a 20 66 6f 72 20 74 68 65 20 63 6f 6c  e.** for the col
1da04 75 6d 6e 20 61 6e 64 20 74 68 65 20 50 33 20 76  umn and the P3 v
1da05 61 6c 75 65 20 69 73 20 6e 6f 74 20 72 65 71 75  alue is not requ
1da06 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6c 75  ired..**.** Colu
1da07 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 63  mn definitions c
1da08 72 65 61 74 65 64 20 62 79 20 61 6e 20 41 4c 54  reated by an ALT
1da09 45 52 20 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64  ER TABLE command
1da0a 20 6d 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 0a   may only have .
1da0b 2a 2a 20 6c 69 74 65 72 61 6c 20 64 65 66 61 75  ** literal defau
1da0c 6c 74 20 76 61 6c 75 65 73 20 73 70 65 63 69 66  lt values specif
1da0d 69 65 64 3a 20 61 20 6e 75 6d 62 65 72 2c 20 6e  ied: a number, n
1da0e 75 6c 6c 20 6f 72 20 61 20 73 74 72 69 6e 67 2e  ull or a string.
1da0f 20 28 49 66 20 61 20 6d 6f 72 65 0a 2a 2a 20 63   (If a more.** c
1da10 6f 6d 70 6c 69 63 61 74 65 64 20 64 65 66 61 75  omplicated defau
1da11 6c 74 20 65 78 70 72 65 73 73 69 6f 6e 20 76 61  lt expression va
1da12 6c 75 65 20 77 61 73 20 70 72 6f 76 69 64 65 64  lue was provided
1da13 2c 20 69 74 20 69 73 20 65 76 61 6c 75 61 74 65  , it is evaluate
1da14 64 20 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 41  d .** when the A
1da15 4c 54 45 52 20 54 41 42 4c 45 20 69 73 20 65 78  LTER TABLE is ex
1da16 65 63 75 74 65 64 20 61 6e 64 20 6f 6e 65 20 6f  ecuted and one o
1da17 66 20 74 68 65 20 6c 69 74 65 72 61 6c 20 76 61  f the literal va
1da18 6c 75 65 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20  lues written.** 
1da19 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65 5f  into the sqlite_
1da1a 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 29 0a 2a  master table.).*
1da1b 2a 0a 2a 2a 20 54 68 65 72 65 66 6f 72 65 2c 20  *.** Therefore, 
1da1c 74 68 65 20 50 33 20 70 61 72 61 6d 65 74 65 72  the P3 parameter
1da1d 20 69 73 20 6f 6e 6c 79 20 72 65 71 75 69 72 65   is only require
1da1e 64 20 69 66 20 74 68 65 20 64 65 66 61 75 6c 74  d if the default
1da1f 20 76 61 6c 75 65 20 66 6f 72 0a 2a 2a 20 74 68   value for.** th
1da20 65 20 63 6f 6c 75 6d 6e 20 69 73 20 61 20 6c 69  e column is a li
1da21 74 65 72 61 6c 20 6e 75 6d 62 65 72 2c 20 73 74  teral number, st
1da22 72 69 6e 67 20 6f 72 20 6e 75 6c 6c 2e 20 54 68  ring or null. Th
1da23 65 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72  e sqlite3ValueFr
1da24 6f 6d 45 78 70 72 28 29 0a 2a 2a 20 66 75 6e 63  omExpr().** func
1da25 74 69 6f 6e 20 69 73 20 63 61 70 61 62 6c 65 20  tion is capable 
1da26 6f 66 20 74 72 61 6e 73 66 6f 72 6d 69 6e 67 20  of transforming 
1da27 74 68 65 73 65 20 74 79 70 65 73 20 6f 66 20 65  these types of e
1da28 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 74 6f 0a  xpressions into.
1da29 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ** sqlite3_value
1da2a 20 6f 62 6a 65 63 74 73 2e 0a 2a 2f 0a 53 51 4c   objects..*/.SQL
1da2b 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1da2c 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65   sqlite3ColumnDe
1da2d 66 61 75 6c 74 28 56 64 62 65 20 2a 76 2c 20 54  fault(Vdbe *v, T
1da2e 61 62 6c 65 20 2a 70 54 61 62 2c 20 69 6e 74 20  able *pTab, int 
1da2f 69 29 7b 0a 20 20 69 66 28 20 70 54 61 62 20 26  i){.  if( pTab &
1da30 26 20 21 70 54 61 62 2d 3e 70 53 65 6c 65 63 74  & !pTab->pSelect
1da31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
1da32 76 61 6c 75 65 20 2a 70 56 61 6c 75 65 3b 0a 20  value *pValue;. 
1da33 20 20 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28     u8 enc = ENC(
1da34 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29  sqlite3VdbeDb(v)
1da35 29 3b 0a 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70  );.    Column *p
1da36 43 6f 6c 20 3d 20 26 70 54 61 62 2d 3e 61 43 6f  Col = &pTab->aCo
1da37 6c 5b 69 5d 3b 0a 20 20 20 20 61 73 73 65 72 74  l[i];.    assert
1da38 28 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29  ( i<pTab->nCol )
1da39 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c  ;.    sqlite3Val
1da3a 75 65 46 72 6f 6d 45 78 70 72 28 70 43 6f 6c 2d  ueFromExpr(pCol-
1da3b 3e 70 44 66 6c 74 2c 20 65 6e 63 2c 20 70 43 6f  >pDflt, enc, pCo
1da3c 6c 2d 3e 61 66 66 69 6e 69 74 79 2c 20 26 70 56  l->affinity, &pV
1da3d 61 6c 75 65 29 3b 0a 20 20 20 20 69 66 28 20 70  alue);.    if( p
1da3e 56 61 6c 75 65 20 29 7b 0a 20 20 20 20 20 20 73  Value ){.      s
1da3f 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1da40 50 33 28 76 2c 20 2d 31 2c 20 28 63 6f 6e 73 74  P3(v, -1, (const
1da41 20 63 68 61 72 20 2a 29 70 56 61 6c 75 65 2c 20   char *)pValue, 
1da42 50 33 5f 4d 45 4d 29 3b 0a 20 20 20 20 7d 65 6c  P3_MEM);.    }el
1da43 73 65 7b 0a 20 20 20 20 20 20 56 64 62 65 43 6f  se{.      VdbeCo
1da44 6d 6d 65 6e 74 28 28 76 2c 20 22 23 20 25 73 2e  mment((v, "# %s.
1da45 25 73 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  %s", pTab->zName
1da46 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 29 3b  , pCol->zName));
1da47 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
1da48 0a 2a 2a 20 50 72 6f 63 65 73 73 20 61 6e 20 55  .** Process an U
1da49 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e  PDATE statement.
1da4a 0a 2a 2a 0a 2a 2a 20 20 20 55 50 44 41 54 45 20  .**.**   UPDATE 
1da4b 4f 52 20 49 47 4e 4f 52 45 20 74 61 62 6c 65 5f  OR IGNORE table_
1da4c 77 78 79 7a 20 53 45 54 20 61 3d 62 2c 20 63 3d  wxyz SET a=b, c=
1da4d 64 20 57 48 45 52 45 20 65 3c 35 20 41 4e 44 20  d WHERE e<5 AND 
1da4e 66 20 4e 4f 54 20 4e 55 4c 4c 3b 0a 2a 2a 20 20  f NOT NULL;.**  
1da4f 20 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f          \_______
1da50 2f 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20  / \________/    
1da51 20 5c 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 20 20   \______/       
1da52 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  \_______________
1da53 5f 2f 0a 2a 20 20 20 20 20 20 20 20 20 20 20 20  _/.*            
1da54 6f 6e 45 72 72 6f 72 20 20 20 70 54 61 62 4c 69  onError   pTabLi
1da55 73 74 20 20 20 20 20 20 70 43 68 61 6e 67 65 73  st      pChanges
1da56 20 20 20 20 20 20 20 20 20 20 20 20 20 70 57 68               pWh
1da57 65 72 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ere.*/.SQLITE_PR
1da58 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1da59 65 33 55 70 64 61 74 65 28 0a 20 20 50 61 72 73  e3Update(.  Pars
1da5a 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
1da5b 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72     /* The parser
1da5c 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72   context */.  Sr
1da5d 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 2c  cList *pTabList,
1da5e 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c       /* The tabl
1da5f 65 20 69 6e 20 77 68 69 63 68 20 77 65 20 73 68  e in which we sh
1da60 6f 75 6c 64 20 63 68 61 6e 67 65 20 74 68 69 6e  ould change thin
1da61 67 73 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  gs */.  ExprList
1da62 20 2a 70 43 68 61 6e 67 65 73 2c 20 20 20 20 2f   *pChanges,    /
1da63 2a 20 54 68 69 6e 67 73 20 74 6f 20 62 65 20 63  * Things to be c
1da64 68 61 6e 67 65 64 20 2a 2f 0a 20 20 45 78 70 72  hanged */.  Expr
1da65 20 2a 70 57 68 65 72 65 2c 20 20 20 20 20 20 20   *pWhere,       
1da66 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20     /* The WHERE 
1da67 63 6c 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20  clause.  May be 
1da68 6e 75 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e  null */.  int on
1da69 45 72 72 6f 72 20 20 20 20 20 20 20 20 20 20 20  Error           
1da6a 20 2f 2a 20 48 6f 77 20 74 6f 20 68 61 6e 64 6c   /* How to handl
1da6b 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 65 72 72  e constraint err
1da6c 6f 72 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ors */.){.  int 
1da6d 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20  i, j;           
1da6e 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
1da6f 65 72 73 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ers */.  Table *
1da70 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20  pTab;           
1da71 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20  /* The table to 
1da72 62 65 20 75 70 64 61 74 65 64 20 2a 2f 0a 20 20  be updated */.  
1da73 69 6e 74 20 61 64 64 72 20 3d 20 30 3b 20 20 20  int addr = 0;   
1da74 20 20 20 20 20 20 20 2f 2a 20 56 44 42 45 20 69         /* VDBE i
1da75 6e 73 74 72 75 63 74 69 6f 6e 20 61 64 64 72 65  nstruction addre
1da76 73 73 20 6f 66 20 74 68 65 20 73 74 61 72 74 20  ss of the start 
1da77 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20  of the loop */. 
1da78 20 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e   WhereInfo *pWIn
1da79 66 6f 3b 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72  fo;     /* Infor
1da7a 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65  mation about the
1da7b 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f   WHERE clause */
1da7c 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20  .  Vdbe *v;     
1da7d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
1da7e 20 76 69 72 74 75 61 6c 20 64 61 74 61 62 61 73   virtual databas
1da7f 65 20 65 6e 67 69 6e 65 20 2a 2f 0a 20 20 49 6e  e engine */.  In
1da80 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20  dex *pIdx;      
1da81 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70       /* For loop
1da82 69 6e 67 20 6f 76 65 72 20 69 6e 64 69 63 65 73  ing over indices
1da83 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 64 78 3b 20   */.  int nIdx; 
1da84 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1da85 4e 75 6d 62 65 72 20 6f 66 20 69 6e 64 69 63 65  Number of indice
1da86 73 20 74 68 61 74 20 6e 65 65 64 20 75 70 64 61  s that need upda
1da87 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6e 49  ting */.  int nI
1da88 64 78 54 6f 74 61 6c 3b 20 20 20 20 20 20 20 20  dxTotal;        
1da89 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72   /* Total number
1da8a 20 6f 66 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20   of indices */. 
1da8b 20 69 6e 74 20 69 43 75 72 3b 20 20 20 20 20 20   int iCur;      
1da8c 20 20 20 20 20 20 20 20 2f 2a 20 56 44 42 45 20          /* VDBE 
1da8d 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66  Cursor number of
1da8e 20 70 54 61 62 20 2a 2f 0a 20 20 73 71 6c 69 74   pTab */.  sqlit
1da8f 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20  e3 *db;         
1da90 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
1da91 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20  e structure */. 
1da92 20 49 6e 64 65 78 20 2a 2a 61 70 49 64 78 20 3d   Index **apIdx =
1da93 20 30 3b 20 20 20 20 20 2f 2a 20 41 6e 20 61 72   0;     /* An ar
1da94 72 61 79 20 6f 66 20 69 6e 64 69 63 65 73 20 74  ray of indices t
1da95 68 61 74 20 6e 65 65 64 20 75 70 64 61 74 69 6e  hat need updatin
1da96 67 20 74 6f 6f 20 2a 2f 0a 20 20 63 68 61 72 20  g too */.  char 
1da97 2a 61 49 64 78 55 73 65 64 20 3d 20 30 3b 20 20  *aIdxUsed = 0;  
1da98 20 20 2f 2a 20 61 49 64 78 55 73 65 64 5b 69 5d    /* aIdxUsed[i]
1da99 3d 3d 31 20 69 66 20 74 68 65 20 69 2d 74 68 20  ==1 if the i-th 
1da9a 69 6e 64 65 78 20 69 73 20 75 73 65 64 20 2a 2f  index is used */
1da9b 0a 20 20 69 6e 74 20 2a 61 58 52 65 66 20 3d 20  .  int *aXRef = 
1da9c 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 61 58 52  0;        /* aXR
1da9d 65 66 5b 69 5d 20 69 73 20 74 68 65 20 69 6e 64  ef[i] is the ind
1da9e 65 78 20 69 6e 20 70 43 68 61 6e 67 65 73 2d 3e  ex in pChanges->
1da9f 61 5b 5d 20 6f 66 20 74 68 65 0a 20 20 20 20 20  a[] of the.     
1daa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1daa1 20 20 20 20 2a 2a 20 61 6e 20 65 78 70 72 65 73      ** an expres
1daa2 73 69 6f 6e 20 66 6f 72 20 74 68 65 20 69 2d 74  sion for the i-t
1daa3 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  h column of the 
1daa4 74 61 62 6c 65 2e 0a 20 20 20 20 20 20 20 20 20  table..         
1daa5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1daa6 2a 2a 20 61 58 52 65 66 5b 69 5d 3d 3d 2d 31 20  ** aXRef[i]==-1 
1daa7 69 66 20 74 68 65 20 69 2d 74 68 20 63 6f 6c 75  if the i-th colu
1daa8 6d 6e 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65  mn is not change
1daa9 64 2e 20 2a 2f 0a 20 20 69 6e 74 20 63 68 6e 67  d. */.  int chng
1daaa 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20 2f  Rowid;         /
1daab 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 72 65  * True if the re
1daac 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73 20 62  cord number is b
1daad 65 69 6e 67 20 63 68 61 6e 67 65 64 20 2a 2f 0a  eing changed */.
1daae 20 20 45 78 70 72 20 2a 70 52 6f 77 69 64 45 78    Expr *pRowidEx
1daaf 70 72 20 3d 20 30 3b 20 20 2f 2a 20 45 78 70 72  pr = 0;  /* Expr
1dab0 65 73 73 69 6f 6e 20 64 65 66 69 6e 69 6e 67 20  ession defining 
1dab1 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 6e  the new record n
1dab2 75 6d 62 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6f  umber */.  int o
1dab3 70 65 6e 41 6c 6c 20 3d 20 30 3b 20 20 20 20 20  penAll = 0;     
1dab4 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6c 6c    /* True if all
1dab5 20 69 6e 64 69 63 65 73 20 6e 65 65 64 20 74 6f   indices need to
1dab6 20 62 65 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20   be opened */.  
1dab7 41 75 74 68 43 6f 6e 74 65 78 74 20 73 43 6f 6e  AuthContext sCon
1dab8 74 65 78 74 3b 20 20 2f 2a 20 54 68 65 20 61 75  text;  /* The au
1dab9 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 6f 6e 74  thorization cont
1daba 65 78 74 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e  ext */.  NameCon
1dabb 74 65 78 74 20 73 4e 43 3b 20 20 20 20 20 20 20  text sNC;       
1dabc 2f 2a 20 54 68 65 20 6e 61 6d 65 2d 63 6f 6e 74  /* The name-cont
1dabd 65 78 74 20 74 6f 20 72 65 73 6f 6c 76 65 20 65  ext to resolve e
1dabe 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 2a 2f  xpressions in */
1dabf 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20  .  int iDb;     
1dac0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
1dac1 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67  abase containing
1dac2 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   the table being
1dac3 20 75 70 64 61 74 65 64 20 2a 2f 0a 20 20 69 6e   updated */.  in
1dac4 74 20 6d 65 6d 43 6e 74 20 3d 20 30 3b 20 20 20  t memCnt = 0;   
1dac5 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63       /* Memory c
1dac6 65 6c 6c 20 75 73 65 64 20 66 6f 72 20 63 6f 75  ell used for cou
1dac7 6e 74 69 6e 67 20 72 6f 77 73 20 63 68 61 6e 67  nting rows chang
1dac8 65 64 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  ed */..#ifndef S
1dac9 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
1daca 45 52 0a 20 20 69 6e 74 20 69 73 56 69 65 77 3b  ER.  int isView;
1dacb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dacc 20 20 2f 2a 20 54 72 79 69 6e 67 20 74 6f 20 75    /* Trying to u
1dacd 70 64 61 74 65 20 61 20 76 69 65 77 20 2a 2f 0a  pdate a view */.
1dace 20 20 69 6e 74 20 74 72 69 67 67 65 72 73 5f 65    int triggers_e
1dacf 78 69 73 74 20 3d 20 30 3b 20 20 20 20 20 20 2f  xist = 0;      /
1dad0 2a 20 54 72 75 65 20 69 66 20 61 6e 79 20 72 6f  * True if any ro
1dad1 77 20 74 72 69 67 67 65 72 73 20 65 78 69 73 74  w triggers exist
1dad2 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 69 6e   */.#endif..  in
1dad3 74 20 6e 65 77 49 64 78 20 20 20 20 20 20 3d 20  t newIdx      = 
1dad4 2d 31 3b 20 20 2f 2a 20 69 6e 64 65 78 20 6f 66  -1;  /* index of
1dad5 20 74 72 69 67 67 65 72 20 22 6e 65 77 22 20 74   trigger "new" t
1dad6 65 6d 70 20 74 61 62 6c 65 20 20 20 20 20 20 20  emp table       
1dad7 2a 2f 0a 20 20 69 6e 74 20 6f 6c 64 49 64 78 20  */.  int oldIdx 
1dad8 20 20 20 20 20 3d 20 2d 31 3b 20 20 2f 2a 20 69       = -1;  /* i
1dad9 6e 64 65 78 20 6f 66 20 74 72 69 67 67 65 72 20  ndex of trigger 
1dada 22 6f 6c 64 22 20 74 65 6d 70 20 74 61 62 6c 65  "old" temp table
1dadb 20 20 20 20 20 20 20 2a 2f 0a 0a 20 20 73 43 6f         */..  sCo
1dadc 6e 74 65 78 74 2e 70 50 61 72 73 65 20 3d 20 30  ntext.pParse = 0
1dadd 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ;.  if( pParse->
1dade 6e 45 72 72 20 7c 7c 20 73 71 6c 69 74 65 33 4d  nErr || sqlite3M
1dadf 61 6c 6c 6f 63 46 61 69 6c 65 64 28 29 20 29 7b  allocFailed() ){
1dae0 0a 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65  .    goto update
1dae1 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20  _cleanup;.  }.  
1dae2 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
1dae3 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62 4c  .  assert( pTabL
1dae4 69 73 74 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a  ist->nSrc==1 );.
1dae5 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65  .  /* Locate the
1dae6 20 74 61 62 6c 65 20 77 68 69 63 68 20 77 65 20   table which we 
1dae7 77 61 6e 74 20 74 6f 20 75 70 64 61 74 65 2e 20  want to update. 
1dae8 0a 20 20 2a 2f 0a 20 20 70 54 61 62 20 3d 20 73  .  */.  pTab = s
1dae9 71 6c 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f  qlite3SrcListLoo
1daea 6b 75 70 28 70 50 61 72 73 65 2c 20 70 54 61 62  kup(pParse, pTab
1daeb 4c 69 73 74 29 3b 0a 20 20 69 66 28 20 70 54 61  List);.  if( pTa
1daec 62 3d 3d 30 20 29 20 67 6f 74 6f 20 75 70 64 61  b==0 ) goto upda
1daed 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 69 44  te_cleanup;.  iD
1daee 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
1daef 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d  aToIndex(pParse-
1daf0 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65  >db, pTab->pSche
1daf1 6d 61 29 3b 0a 0a 20 20 2f 2a 20 46 69 67 75 72  ma);..  /* Figur
1daf2 65 20 6f 75 74 20 69 66 20 77 65 20 68 61 76 65  e out if we have
1daf3 20 61 6e 79 20 74 72 69 67 67 65 72 73 20 61 6e   any triggers an
1daf4 64 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 62  d if the table b
1daf5 65 69 6e 67 0a 20 20 2a 2a 20 75 70 64 61 74 65  eing.  ** update
1daf6 64 20 69 73 20 61 20 76 69 65 77 0a 20 20 2a 2f  d is a view.  */
1daf7 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1daf8 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 74  OMIT_TRIGGER.  t
1daf9 72 69 67 67 65 72 73 5f 65 78 69 73 74 20 3d 20  riggers_exist = 
1dafa 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 73 45  sqlite3TriggersE
1dafb 78 69 73 74 28 70 50 61 72 73 65 2c 20 70 54 61  xist(pParse, pTa
1dafc 62 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 70 43  b, TK_UPDATE, pC
1dafd 68 61 6e 67 65 73 29 3b 0a 20 20 69 73 56 69 65  hanges);.  isVie
1dafe 77 20 3d 20 70 54 61 62 2d 3e 70 53 65 6c 65 63  w = pTab->pSelec
1daff 74 21 3d 30 3b 0a 23 65 6c 73 65 0a 23 20 64 65  t!=0;.#else.# de
1db00 66 69 6e 65 20 74 72 69 67 67 65 72 73 5f 65 78  fine triggers_ex
1db01 69 73 74 20 30 0a 23 20 64 65 66 69 6e 65 20 69  ist 0.# define i
1db02 73 56 69 65 77 20 30 0a 23 65 6e 64 69 66 0a 23  sView 0.#endif.#
1db03 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
1db04 54 5f 56 49 45 57 0a 23 20 75 6e 64 65 66 20 69  T_VIEW.# undef i
1db05 73 56 69 65 77 0a 23 20 64 65 66 69 6e 65 20 69  sView.# define i
1db06 73 56 69 65 77 20 30 0a 23 65 6e 64 69 66 0a 0a  sView 0.#endif..
1db07 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 52    if( sqlite3IsR
1db08 65 61 64 4f 6e 6c 79 28 70 50 61 72 73 65 2c 20  eadOnly(pParse, 
1db09 70 54 61 62 2c 20 74 72 69 67 67 65 72 73 5f 65  pTab, triggers_e
1db0a 78 69 73 74 29 20 29 7b 0a 20 20 20 20 67 6f 74  xist) ){.    got
1db0b 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70  o update_cleanup
1db0c 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69  ;.  }.  if( sqli
1db0d 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e  te3ViewGetColumn
1db0e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70 54  Names(pParse, pT
1db0f 61 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  ab) ){.    goto 
1db10 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a  update_cleanup;.
1db11 20 20 7d 0a 20 20 61 58 52 65 66 20 3d 20 73 71    }.  aXRef = sq
1db12 6c 69 74 65 4d 61 6c 6c 6f 63 52 61 77 28 20 73  liteMallocRaw( s
1db13 69 7a 65 6f 66 28 69 6e 74 29 20 2a 20 70 54 61  izeof(int) * pTa
1db14 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 69 66 28  b->nCol );.  if(
1db15 20 61 58 52 65 66 3d 3d 30 20 29 20 67 6f 74 6f   aXRef==0 ) goto
1db16 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b   update_cleanup;
1db17 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54  .  for(i=0; i<pT
1db18 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 20 61  ab->nCol; i++) a
1db19 58 52 65 66 5b 69 5d 20 3d 20 2d 31 3b 0a 0a 20  XRef[i] = -1;.. 
1db1a 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65   /* If there are
1db1b 20 46 4f 52 20 45 41 43 48 20 52 4f 57 20 74 72   FOR EACH ROW tr
1db1c 69 67 67 65 72 73 2c 20 61 6c 6c 6f 63 61 74 65  iggers, allocate
1db1d 20 63 75 72 73 6f 72 73 20 66 6f 72 20 74 68 65   cursors for the
1db1e 0a 20 20 2a 2a 20 73 70 65 63 69 61 6c 20 4f 4c  .  ** special OL
1db1f 44 20 61 6e 64 20 4e 45 57 20 74 61 62 6c 65 73  D and NEW tables
1db20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 74 72 69 67  .  */.  if( trig
1db21 67 65 72 73 5f 65 78 69 73 74 20 29 7b 0a 20 20  gers_exist ){.  
1db22 20 20 6e 65 77 49 64 78 20 3d 20 70 50 61 72 73    newIdx = pPars
1db23 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 6f  e->nTab++;.    o
1db24 6c 64 49 64 78 20 3d 20 70 50 61 72 73 65 2d 3e  ldIdx = pParse->
1db25 6e 54 61 62 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f  nTab++;.  }..  /
1db26 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 63 75 72  * Allocate a cur
1db27 73 6f 72 73 20 66 6f 72 20 74 68 65 20 6d 61 69  sors for the mai
1db28 6e 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65  n database table
1db29 20 61 6e 64 20 66 6f 72 20 61 6c 6c 20 69 6e 64   and for all ind
1db2a 69 63 65 73 2e 0a 20 20 2a 2a 20 54 68 65 20 69  ices..  ** The i
1db2b 6e 64 65 78 20 63 75 72 73 6f 72 73 20 6d 69 67  ndex cursors mig
1db2c 68 74 20 6e 6f 74 20 62 65 20 75 73 65 64 2c 20  ht not be used, 
1db2d 62 75 74 20 69 66 20 74 68 65 79 20 61 72 65 20  but if they are 
1db2e 75 73 65 64 20 74 68 65 79 0a 20 20 2a 2a 20 6e  used they.  ** n
1db2f 65 65 64 20 74 6f 20 6f 63 63 75 72 20 72 69 67  eed to occur rig
1db30 68 74 20 61 66 74 65 72 20 74 68 65 20 64 61 74  ht after the dat
1db31 61 62 61 73 65 20 63 75 72 73 6f 72 2e 20 20 53  abase cursor.  S
1db32 6f 20 67 6f 20 61 68 65 61 64 20 61 6e 64 0a 20  o go ahead and. 
1db33 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 65 6e 6f   ** allocate eno
1db34 75 67 68 20 73 70 61 63 65 2c 20 6a 75 73 74 20  ugh space, just 
1db35 69 6e 20 63 61 73 65 2e 0a 20 20 2a 2f 0a 20 20  in case..  */.  
1db36 70 54 61 62 4c 69 73 74 2d 3e 61 5b 30 5d 2e 69  pTabList->a[0].i
1db37 43 75 72 73 6f 72 20 3d 20 69 43 75 72 20 3d 20  Cursor = iCur = 
1db38 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a  pParse->nTab++;.
1db39 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d    for(pIdx=pTab-
1db3a 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70  >pIndex; pIdx; p
1db3b 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29  Idx=pIdx->pNext)
1db3c 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 54  {.    pParse->nT
1db3d 61 62 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ab++;.  }..  /* 
1db3e 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6e  Initialize the n
1db3f 61 6d 65 2d 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ame-context */. 
1db40 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c   memset(&sNC, 0,
1db41 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20   sizeof(sNC));. 
1db42 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 50   sNC.pParse = pP
1db43 61 72 73 65 3b 0a 20 20 73 4e 43 2e 70 53 72 63  arse;.  sNC.pSrc
1db44 4c 69 73 74 20 3d 20 70 54 61 62 4c 69 73 74 3b  List = pTabList;
1db45 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74  ..  /* Resolve t
1db46 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  he column names 
1db47 69 6e 20 61 6c 6c 20 74 68 65 20 65 78 70 72 65  in all the expre
1db48 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 0a 20 20  ssions of the.  
1db49 2a 2a 20 6f 66 20 74 68 65 20 55 50 44 41 54 45  ** of the UPDATE
1db4a 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 41 6c 73   statement.  Als
1db4b 6f 20 66 69 6e 64 20 74 68 65 20 63 6f 6c 75 6d  o find the colum
1db4c 6e 20 69 6e 64 65 78 0a 20 20 2a 2a 20 66 6f 72  n index.  ** for
1db4d 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 74 6f 20   each column to 
1db4e 62 65 20 75 70 64 61 74 65 64 20 69 6e 20 74 68  be updated in th
1db4f 65 20 70 43 68 61 6e 67 65 73 20 61 72 72 61 79  e pChanges array
1db50 2e 20 20 46 6f 72 20 65 61 63 68 0a 20 20 2a 2a  .  For each.  **
1db51 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 75 70   column to be up
1db52 64 61 74 65 64 2c 20 6d 61 6b 65 20 73 75 72 65  dated, make sure
1db53 20 77 65 20 68 61 76 65 20 61 75 74 68 6f 72 69   we have authori
1db54 7a 61 74 69 6f 6e 20 74 6f 20 63 68 61 6e 67 65  zation to change
1db55 0a 20 20 2a 2a 20 74 68 61 74 20 63 6f 6c 75 6d  .  ** that colum
1db56 6e 2e 0a 20 20 2a 2f 0a 20 20 63 68 6e 67 52 6f  n..  */.  chngRo
1db57 77 69 64 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69  wid = 0;.  for(i
1db58 3d 30 3b 20 69 3c 70 43 68 61 6e 67 65 73 2d 3e  =0; i<pChanges->
1db59 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20  nExpr; i++){.   
1db5a 20 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 72   if( sqlite3Expr
1db5b 52 65 73 6f 6c 76 65 4e 61 6d 65 73 28 26 73 4e  ResolveNames(&sN
1db5c 43 2c 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69  C, pChanges->a[i
1db5d 5d 2e 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20  ].pExpr) ){.    
1db5e 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c    goto update_cl
1db5f 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 20  eanup;.    }.   
1db60 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 54 61 62   for(j=0; j<pTab
1db61 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20  ->nCol; j++){.  
1db62 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
1db63 74 72 49 43 6d 70 28 70 54 61 62 2d 3e 61 43 6f  trICmp(pTab->aCo
1db64 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c 20 70 43 68 61  l[j].zName, pCha
1db65 6e 67 65 73 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65  nges->a[i].zName
1db66 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
1db67 69 66 28 20 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b  if( j==pTab->iPK
1db68 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ey ){.          
1db69 63 68 6e 67 52 6f 77 69 64 20 3d 20 31 3b 0a 20  chngRowid = 1;. 
1db6a 20 20 20 20 20 20 20 20 20 70 52 6f 77 69 64 45           pRowidE
1db6b 78 70 72 20 3d 20 70 43 68 61 6e 67 65 73 2d 3e  xpr = pChanges->
1db6c 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20  a[i].pExpr;.    
1db6d 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 58      }.        aX
1db6e 52 65 66 5b 6a 5d 20 3d 20 69 3b 0a 20 20 20 20  Ref[j] = i;.    
1db6f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
1db70 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
1db71 20 6a 3e 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20 29   j>=pTab->nCol )
1db72 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  {.      if( sqli
1db73 74 65 33 49 73 52 6f 77 69 64 28 70 43 68 61 6e  te3IsRowid(pChan
1db74 67 65 73 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29  ges->a[i].zName)
1db75 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 6e 67   ){.        chng
1db76 52 6f 77 69 64 20 3d 20 31 3b 0a 20 20 20 20 20  Rowid = 1;.     
1db77 20 20 20 70 52 6f 77 69 64 45 78 70 72 20 3d 20     pRowidExpr = 
1db78 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e 70  pChanges->a[i].p
1db79 45 78 70 72 3b 0a 20 20 20 20 20 20 7d 65 6c 73  Expr;.      }els
1db7a 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  e{.        sqlit
1db7b 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
1db7c 65 2c 20 22 6e 6f 20 73 75 63 68 20 63 6f 6c 75  e, "no such colu
1db7d 6d 6e 3a 20 25 73 22 2c 20 70 43 68 61 6e 67 65  mn: %s", pChange
1db7e 73 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a  s->a[i].zName);.
1db7f 20 20 20 20 20 20 20 20 67 6f 74 6f 20 75 70 64          goto upd
1db80 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20  ate_cleanup;.   
1db81 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 6e 64     }.    }.#ifnd
1db82 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
1db83 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20  UTHORIZATION.   
1db84 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 3b   {.      int rc;
1db85 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1db86 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
1db87 72 73 65 2c 20 53 51 4c 49 54 45 5f 55 50 44 41  rse, SQLITE_UPDA
1db88 54 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c  TE, pTab->zName,
1db89 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1db8a 20 20 20 20 20 20 20 20 20 20 20 20 70 54 61 62              pTab
1db8b 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c  ->aCol[j].zName,
1db8c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
1db8d 61 6d 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ame);.      if( 
1db8e 72 63 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20  rc==SQLITE_DENY 
1db8f 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  ){.        goto 
1db90 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a  update_cleanup;.
1db91 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
1db92 72 63 3d 3d 53 51 4c 49 54 45 5f 49 47 4e 4f 52  rc==SQLITE_IGNOR
1db93 45 20 29 7b 0a 20 20 20 20 20 20 20 20 61 58 52  E ){.        aXR
1db94 65 66 5b 6a 5d 20 3d 20 2d 31 3b 0a 20 20 20 20  ef[j] = -1;.    
1db95 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66    }.    }.#endif
1db96 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63  .  }..  /* Alloc
1db97 61 74 65 20 6d 65 6d 6f 72 79 20 66 6f 72 20 74  ate memory for t
1db98 68 65 20 61 72 72 61 79 20 61 70 49 64 78 5b 5d  he array apIdx[]
1db99 20 61 6e 64 20 66 69 6c 6c 20 69 74 20 77 69 74   and fill it wit
1db9a 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 65 76  h pointers to ev
1db9b 65 72 79 0a 20 20 2a 2a 20 69 6e 64 65 78 20 74  ery.  ** index t
1db9c 68 61 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20  hat needs to be 
1db9d 75 70 64 61 74 65 64 2e 20 20 49 6e 64 69 63 65  updated.  Indice
1db9e 73 20 6f 6e 6c 79 20 6e 65 65 64 20 75 70 64 61  s only need upda
1db9f 74 69 6e 67 20 69 66 20 74 68 65 69 72 0a 20 20  ting if their.  
1dba0 2a 2a 20 6b 65 79 20 69 6e 63 6c 75 64 65 73 20  ** key includes 
1dba1 6f 6e 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d  one of the colum
1dba2 6e 73 20 6e 61 6d 65 64 20 69 6e 20 70 43 68 61  ns named in pCha
1dba3 6e 67 65 73 20 6f 72 20 69 66 20 74 68 65 20 72  nges or if the r
1dba4 65 63 6f 72 64 0a 20 20 2a 2a 20 6e 75 6d 62 65  ecord.  ** numbe
1dba5 72 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61  r of the origina
1dba6 6c 20 74 61 62 6c 65 20 65 6e 74 72 79 20 69 73  l table entry is
1dba7 20 63 68 61 6e 67 69 6e 67 2e 0a 20 20 2a 2f 0a   changing..  */.
1dba8 20 20 66 6f 72 28 6e 49 64 78 3d 6e 49 64 78 54    for(nIdx=nIdxT
1dba9 6f 74 61 6c 3d 30 2c 20 70 49 64 78 3d 70 54 61  otal=0, pIdx=pTa
1dbaa 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b  b->pIndex; pIdx;
1dbab 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
1dbac 74 2c 20 6e 49 64 78 54 6f 74 61 6c 2b 2b 29 7b  t, nIdxTotal++){
1dbad 0a 20 20 20 20 69 66 28 20 63 68 6e 67 52 6f 77  .    if( chngRow
1dbae 69 64 20 29 7b 0a 20 20 20 20 20 20 69 20 3d 20  id ){.      i = 
1dbaf 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 7b 0a 20  0;.    }else {. 
1dbb0 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
1dbb1 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69  pIdx->nColumn; i
1dbb2 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ++){.        if(
1dbb3 20 61 58 52 65 66 5b 70 49 64 78 2d 3e 61 69 43   aXRef[pIdx->aiC
1dbb4 6f 6c 75 6d 6e 5b 69 5d 5d 3e 3d 30 20 29 20 62  olumn[i]]>=0 ) b
1dbb5 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
1dbb6 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3c 70 49    }.    if( i<pI
1dbb7 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 20 6e 49  dx->nColumn ) nI
1dbb8 64 78 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 28 20  dx++;.  }.  if( 
1dbb9 6e 49 64 78 54 6f 74 61 6c 3e 30 20 29 7b 0a 20  nIdxTotal>0 ){. 
1dbba 20 20 20 61 70 49 64 78 20 3d 20 73 71 6c 69 74     apIdx = sqlit
1dbbb 65 4d 61 6c 6c 6f 63 52 61 77 28 20 73 69 7a 65  eMallocRaw( size
1dbbc 6f 66 28 49 6e 64 65 78 2a 29 20 2a 20 6e 49 64  of(Index*) * nId
1dbbd 78 20 2b 20 6e 49 64 78 54 6f 74 61 6c 20 29 3b  x + nIdxTotal );
1dbbe 0a 20 20 20 20 69 66 28 20 61 70 49 64 78 3d 3d  .    if( apIdx==
1dbbf 30 20 29 20 67 6f 74 6f 20 75 70 64 61 74 65 5f  0 ) goto update_
1dbc0 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 61 49 64  cleanup;.    aId
1dbc1 78 55 73 65 64 20 3d 20 28 63 68 61 72 2a 29 26  xUsed = (char*)&
1dbc2 61 70 49 64 78 5b 6e 49 64 78 5d 3b 0a 20 20 7d  apIdx[nIdx];.  }
1dbc3 0a 20 20 66 6f 72 28 6e 49 64 78 3d 6a 3d 30 2c  .  for(nIdx=j=0,
1dbc4 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64   pIdx=pTab->pInd
1dbc5 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70  ex; pIdx; pIdx=p
1dbc6 49 64 78 2d 3e 70 4e 65 78 74 2c 20 6a 2b 2b 29  Idx->pNext, j++)
1dbc7 7b 0a 20 20 20 20 69 66 28 20 63 68 6e 67 52 6f  {.    if( chngRo
1dbc8 77 69 64 20 29 7b 0a 20 20 20 20 20 20 69 20 3d  wid ){.      i =
1dbc9 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   0;.    }else{. 
1dbca 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
1dbcb 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69  pIdx->nColumn; i
1dbcc 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ++){.        if(
1dbcd 20 61 58 52 65 66 5b 70 49 64 78 2d 3e 61 69 43   aXRef[pIdx->aiC
1dbce 6f 6c 75 6d 6e 5b 69 5d 5d 3e 3d 30 20 29 20 62  olumn[i]]>=0 ) b
1dbcf 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
1dbd0 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3c 70 49    }.    if( i<pI
1dbd1 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20  dx->nColumn ){. 
1dbd2 20 20 20 20 20 61 70 49 64 78 5b 6e 49 64 78 2b       apIdx[nIdx+
1dbd3 2b 5d 20 3d 20 70 49 64 78 3b 0a 20 20 20 20 20  +] = pIdx;.     
1dbd4 20 61 49 64 78 55 73 65 64 5b 6a 5d 20 3d 20 31   aIdxUsed[j] = 1
1dbd5 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
1dbd6 20 20 20 61 49 64 78 55 73 65 64 5b 6a 5d 20 3d     aIdxUsed[j] =
1dbd7 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20   0;.    }.  }.. 
1dbd8 20 2f 2a 20 42 65 67 69 6e 20 67 65 6e 65 72 61   /* Begin genera
1dbd9 74 69 6e 67 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a  ting code..  */.
1dbda 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
1dbdb 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
1dbdc 69 66 28 20 76 3d 3d 30 20 29 20 67 6f 74 6f 20  if( v==0 ) goto 
1dbdd 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a  update_cleanup;.
1dbde 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65    if( pParse->ne
1dbdf 73 74 65 64 3d 3d 30 20 29 20 73 71 6c 69 74 65  sted==0 ) sqlite
1dbe0 33 56 64 62 65 43 6f 75 6e 74 43 68 61 6e 67 65  3VdbeCountChange
1dbe1 73 28 76 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  s(v);.  sqlite3B
1dbe2 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
1dbe3 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c 20 69 44  on(pParse, 1, iD
1dbe4 62 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  b);..#ifndef SQL
1dbe5 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1dbe6 54 41 42 4c 45 0a 20 20 2f 2a 20 56 69 72 74 75  TABLE.  /* Virtu
1dbe7 61 6c 20 74 61 62 6c 65 73 20 6d 75 73 74 20 62  al tables must b
1dbe8 65 20 68 61 6e 64 6c 65 64 20 73 65 70 61 72 61  e handled separa
1dbe9 74 65 6c 79 20 2a 2f 0a 20 20 69 66 28 20 49 73  tely */.  if( Is
1dbea 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b  Virtual(pTab) ){
1dbeb 0a 20 20 20 20 75 70 64 61 74 65 56 69 72 74 75  .    updateVirtu
1dbec 61 6c 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  alTable(pParse, 
1dbed 70 54 61 62 4c 69 73 74 2c 20 70 54 61 62 2c 20  pTabList, pTab, 
1dbee 70 43 68 61 6e 67 65 73 2c 20 70 52 6f 77 69 64  pChanges, pRowid
1dbef 45 78 70 72 2c 20 61 58 52 65 66 2c 0a 20 20 20  Expr, aXRef,.   
1dbf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dbf1 20 20 20 20 70 57 68 65 72 65 29 3b 0a 20 20 20      pWhere);.   
1dbf2 20 70 57 68 65 72 65 20 3d 20 30 3b 0a 20 20 20   pWhere = 0;.   
1dbf3 20 70 54 61 62 4c 69 73 74 20 3d 20 30 3b 0a 20   pTabList = 0;. 
1dbf4 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63     goto update_c
1dbf5 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 23 65 6e 64  leanup;.  }.#end
1dbf6 69 66 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65  if..  /* Resolve
1dbf7 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65   the column name
1dbf8 73 20 69 6e 20 61 6c 6c 20 74 68 65 20 65 78 70  s in all the exp
1dbf9 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 0a  ressions in the.
1dbfa 20 20 2a 2a 20 57 48 45 52 45 20 63 6c 61 75 73    ** WHERE claus
1dbfb 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71  e..  */.  if( sq
1dbfc 6c 69 74 65 33 45 78 70 72 52 65 73 6f 6c 76 65  lite3ExprResolve
1dbfd 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 57 68 65  Names(&sNC, pWhe
1dbfe 72 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  re) ){.    goto 
1dbff 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a  update_cleanup;.
1dc00 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 61 72 74 20    }..  /* Start 
1dc01 74 68 65 20 76 69 65 77 20 63 6f 6e 74 65 78 74  the view context
1dc02 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 56 69  .  */.  if( isVi
1dc03 65 77 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ew ){.    sqlite
1dc04 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75 73 68  3AuthContextPush
1dc05 28 70 50 61 72 73 65 2c 20 26 73 43 6f 6e 74 65  (pParse, &sConte
1dc06 78 74 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  xt, pTab->zName)
1dc07 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77  ;.  }..  /* If w
1dc08 65 20 61 72 65 20 74 72 79 69 6e 67 20 74 6f 20  e are trying to 
1dc09 75 70 64 61 74 65 20 61 20 76 69 65 77 2c 20 72  update a view, r
1dc0a 65 61 6c 69 7a 65 20 74 68 61 74 20 76 69 65 77  ealize that view
1dc0b 20 69 6e 74 6f 0a 20 20 2a 2a 20 61 20 65 70 68   into.  ** a eph
1dc0c 65 6d 65 72 61 6c 20 74 61 62 6c 65 2e 0a 20 20  emeral table..  
1dc0d 2a 2f 0a 20 20 69 66 28 20 69 73 56 69 65 77 20  */.  if( isView 
1dc0e 29 7b 0a 20 20 20 20 53 65 6c 65 63 74 20 2a 70  ){.    Select *p
1dc0f 56 69 65 77 3b 0a 20 20 20 20 70 56 69 65 77 20  View;.    pView 
1dc10 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44  = sqlite3SelectD
1dc11 75 70 28 70 54 61 62 2d 3e 70 53 65 6c 65 63 74  up(pTab->pSelect
1dc12 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65  );.    sqlite3Se
1dc13 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 56 69  lect(pParse, pVi
1dc14 65 77 2c 20 53 52 54 5f 45 70 68 65 6d 54 61 62  ew, SRT_EphemTab
1dc15 2c 20 69 43 75 72 2c 20 30 2c 20 30 2c 20 30 2c  , iCur, 0, 0, 0,
1dc16 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
1dc17 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70 56 69  SelectDelete(pVi
1dc18 65 77 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42  ew);.  }..  /* B
1dc19 65 67 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  egin the databas
1dc1a 65 20 73 63 61 6e 0a 20 20 2a 2f 0a 20 20 70 57  e scan.  */.  pW
1dc1b 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 57 68  Info = sqlite3Wh
1dc1c 65 72 65 42 65 67 69 6e 28 70 50 61 72 73 65 2c  ereBegin(pParse,
1dc1d 20 70 54 61 62 4c 69 73 74 2c 20 70 57 68 65 72   pTabList, pWher
1dc1e 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 57 49  e, 0);.  if( pWI
1dc1f 6e 66 6f 3d 3d 30 20 29 20 67 6f 74 6f 20 75 70  nfo==0 ) goto up
1dc20 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 0a 20  date_cleanup;.. 
1dc21 20 2f 2a 20 52 65 6d 65 6d 62 65 72 20 74 68 65   /* Remember the
1dc22 20 72 6f 77 69 64 20 6f 66 20 65 76 65 72 79 20   rowid of every 
1dc23 69 74 65 6d 20 74 6f 20 62 65 20 75 70 64 61 74  item to be updat
1dc24 65 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  ed..  */.  sqlit
1dc25 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 49  e3VdbeAddOp(v, I
1dc26 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 3f  sVirtual(pTab) ?
1dc27 20 4f 50 5f 56 52 6f 77 69 64 20 3a 20 4f 50 5f   OP_VRowid : OP_
1dc28 52 6f 77 69 64 2c 20 69 43 75 72 2c 20 30 29 3b  Rowid, iCur, 0);
1dc29 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
1dc2a 64 4f 70 28 76 2c 20 4f 50 5f 46 69 66 6f 57 72  dOp(v, OP_FifoWr
1dc2b 69 74 65 2c 20 30 2c 20 30 29 3b 0a 0a 20 20 2f  ite, 0, 0);..  /
1dc2c 2a 20 45 6e 64 20 74 68 65 20 64 61 74 61 62 61  * End the databa
1dc2d 73 65 20 73 63 61 6e 20 6c 6f 6f 70 2e 0a 20 20  se scan loop..  
1dc2e 2a 2f 0a 20 20 73 71 6c 69 74 65 33 57 68 65 72  */.  sqlite3Wher
1dc2f 65 45 6e 64 28 70 57 49 6e 66 6f 29 3b 0a 0a 20  eEnd(pWInfo);.. 
1dc30 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74   /* Initialize t
1dc31 68 65 20 63 6f 75 6e 74 20 6f 66 20 75 70 64 61  he count of upda
1dc32 74 65 64 20 72 6f 77 73 0a 20 20 2a 2f 0a 20 20  ted rows.  */.  
1dc33 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 20 26 20  if( db->flags & 
1dc34 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73  SQLITE_CountRows
1dc35 20 26 26 20 21 70 50 61 72 73 65 2d 3e 74 72 69   && !pParse->tri
1dc36 67 53 74 61 63 6b 20 29 7b 0a 20 20 20 20 6d 65  gStack ){.    me
1dc37 6d 43 6e 74 20 3d 20 70 50 61 72 73 65 2d 3e 6e  mCnt = pParse->n
1dc38 4d 65 6d 2b 2b 3b 0a 20 20 20 20 73 71 6c 69 74  Mem++;.    sqlit
1dc39 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f  e3VdbeAddOp(v, O
1dc3a 50 5f 4d 65 6d 49 6e 74 2c 20 30 2c 20 6d 65 6d  P_MemInt, 0, mem
1dc3b 43 6e 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  Cnt);.  }..  if(
1dc3c 20 74 72 69 67 67 65 72 73 5f 65 78 69 73 74 20   triggers_exist 
1dc3d 29 7b 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65  ){.    /* Create
1dc3e 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 73 20 66   pseudo-tables f
1dc3f 6f 72 20 4e 45 57 20 61 6e 64 20 4f 4c 44 0a 20  or NEW and OLD. 
1dc40 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65     */.    sqlite
1dc41 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1dc42 5f 4f 70 65 6e 50 73 65 75 64 6f 2c 20 6f 6c 64  _OpenPseudo, old
1dc43 49 64 78 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  Idx, 0);.    sql
1dc44 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1dc45 20 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e   OP_SetNumColumn
1dc46 73 2c 20 6f 6c 64 49 64 78 2c 20 70 54 61 62 2d  s, oldIdx, pTab-
1dc47 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 73 71 6c 69  >nCol);.    sqli
1dc48 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1dc49 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f 2c 20 6e  OP_OpenPseudo, n
1dc4a 65 77 49 64 78 2c 20 30 29 3b 0a 20 20 20 20 73  ewIdx, 0);.    s
1dc4b 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1dc4c 76 2c 20 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75  v, OP_SetNumColu
1dc4d 6d 6e 73 2c 20 6e 65 77 49 64 78 2c 20 70 54 61  mns, newIdx, pTa
1dc4e 62 2d 3e 6e 43 6f 6c 29 3b 0a 0a 20 20 20 20 2f  b->nCol);..    /
1dc4f 2a 20 54 68 65 20 74 6f 70 20 6f 66 20 74 68 65  * The top of the
1dc50 20 75 70 64 61 74 65 20 6c 6f 6f 70 20 66 6f 72   update loop for
1dc51 20 77 68 65 6e 20 74 68 65 72 65 20 61 72 65 20   when there are 
1dc52 74 72 69 67 67 65 72 73 2e 0a 20 20 20 20 2a 2f  triggers..    */
1dc53 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69  .    addr = sqli
1dc54 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1dc55 4f 50 5f 46 69 66 6f 52 65 61 64 2c 20 30 2c 20  OP_FifoRead, 0, 
1dc56 30 29 3b 0a 0a 20 20 20 20 69 66 28 20 21 69 73  0);..    if( !is
1dc57 56 69 65 77 20 29 7b 0a 20 20 20 20 20 20 73 71  View ){.      sq
1dc58 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1dc59 2c 20 4f 50 5f 44 75 70 2c 20 30 2c 20 30 29 3b  , OP_Dup, 0, 0);
1dc5a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1dc5b 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 44 75  beAddOp(v, OP_Du
1dc5c 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20  p, 0, 0);.      
1dc5d 2f 2a 20 4f 70 65 6e 20 61 20 63 75 72 73 6f 72  /* Open a cursor
1dc5e 20 61 6e 64 20 6d 61 6b 65 20 69 74 20 70 6f 69   and make it poi
1dc5f 6e 74 20 74 6f 20 74 68 65 20 72 65 63 6f 72 64  nt to the record
1dc60 20 74 68 61 74 20 69 73 0a 20 20 20 20 20 20 2a   that is.      *
1dc61 2a 20 62 65 69 6e 67 20 75 70 64 61 74 65 64 2e  * being updated.
1dc62 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
1dc63 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65  sqlite3OpenTable
1dc64 28 70 50 61 72 73 65 2c 20 69 43 75 72 2c 20 69  (pParse, iCur, i
1dc65 44 62 2c 20 70 54 61 62 2c 20 4f 50 5f 4f 70 65  Db, pTab, OP_Ope
1dc66 6e 52 65 61 64 29 3b 0a 20 20 20 20 7d 0a 20 20  nRead);.    }.  
1dc67 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1dc68 4f 70 28 76 2c 20 4f 50 5f 4d 6f 76 65 47 65 2c  Op(v, OP_MoveGe,
1dc69 20 69 43 75 72 2c 20 30 29 3b 0a 0a 20 20 20 20   iCur, 0);..    
1dc6a 2f 2a 20 47 65 6e 65 72 61 74 65 20 74 68 65 20  /* Generate the 
1dc6b 4f 4c 44 20 74 61 62 6c 65 0a 20 20 20 20 2a 2f  OLD table.    */
1dc6c 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1dc6d 41 64 64 4f 70 28 76 2c 20 4f 50 5f 52 6f 77 69  AddOp(v, OP_Rowi
1dc6e 64 2c 20 69 43 75 72 2c 20 30 29 3b 0a 20 20 20  d, iCur, 0);.   
1dc6f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1dc70 70 28 76 2c 20 4f 50 5f 52 6f 77 44 61 74 61 2c  p(v, OP_RowData,
1dc71 20 69 43 75 72 2c 20 30 29 3b 0a 20 20 20 20 73   iCur, 0);.    s
1dc72 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1dc73 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 6f 6c  v, OP_Insert, ol
1dc74 64 49 64 78 2c 20 30 29 3b 0a 0a 20 20 20 20 2f  dIdx, 0);..    /
1dc75 2a 20 47 65 6e 65 72 61 74 65 20 74 68 65 20 4e  * Generate the N
1dc76 45 57 20 74 61 62 6c 65 0a 20 20 20 20 2a 2f 0a  EW table.    */.
1dc77 20 20 20 20 69 66 28 20 63 68 6e 67 52 6f 77 69      if( chngRowi
1dc78 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  d ){.      sqlit
1dc79 65 33 45 78 70 72 43 6f 64 65 41 6e 64 43 61 63  e3ExprCodeAndCac
1dc7a 68 65 28 70 50 61 72 73 65 2c 20 70 52 6f 77 69  he(pParse, pRowi
1dc7b 64 45 78 70 72 29 3b 0a 20 20 20 20 7d 65 6c 73  dExpr);.    }els
1dc7c 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
1dc7d 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1dc7e 52 6f 77 69 64 2c 20 69 43 75 72 2c 20 30 29 3b  Rowid, iCur, 0);
1dc7f 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69  .    }.    for(i
1dc80 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c  =0; i<pTab->nCol
1dc81 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; i++){.      if
1dc82 28 20 69 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79  ( i==pTab->iPKey
1dc83 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
1dc84 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1dc85 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 30 29 3b 0a  OP_Null, 0, 0);.
1dc86 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65          continue
1dc87 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1dc88 6a 20 3d 20 61 58 52 65 66 5b 69 5d 3b 0a 20 20  j = aXRef[i];.  
1dc89 20 20 20 20 69 66 28 20 6a 3c 30 20 29 7b 0a 20      if( j<0 ){. 
1dc8a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1dc8b 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6f  beAddOp(v, OP_Co
1dc8c 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 69 29 3b 0a  lumn, iCur, i);.
1dc8d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43          sqlite3C
1dc8e 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 76 2c 20  olumnDefault(v, 
1dc8f 70 54 61 62 2c 20 69 29 3b 0a 20 20 20 20 20 20  pTab, i);.      
1dc90 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73  }else{.        s
1dc91 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 41 6e  qlite3ExprCodeAn
1dc92 64 43 61 63 68 65 28 70 50 61 72 73 65 2c 20 70  dCache(pParse, p
1dc93 43 68 61 6e 67 65 73 2d 3e 61 5b 6a 5d 2e 70 45  Changes->a[j].pE
1dc94 78 70 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  xpr);.      }.  
1dc95 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
1dc96 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d  dbeAddOp(v, OP_M
1dc97 61 6b 65 52 65 63 6f 72 64 2c 20 70 54 61 62 2d  akeRecord, pTab-
1dc98 3e 6e 43 6f 6c 2c 20 30 29 3b 0a 20 20 20 20 69  >nCol, 0);.    i
1dc99 66 28 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20  f( !isView ){.  
1dc9a 20 20 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65      sqlite3Table
1dc9b 41 66 66 69 6e 69 74 79 53 74 72 28 76 2c 20 70  AffinityStr(v, p
1dc9c 54 61 62 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Tab);.    }.    
1dc9d 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72  if( pParse->nErr
1dc9e 20 29 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63   ) goto update_c
1dc9f 6c 65 61 6e 75 70 3b 0a 20 20 20 20 73 71 6c 69  leanup;.    sqli
1dca0 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1dca1 4f 50 5f 49 6e 73 65 72 74 2c 20 6e 65 77 49 64  OP_Insert, newId
1dca2 78 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 21  x, 0);.    if( !
1dca3 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 20 20  isView ){.      
1dca4 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1dca5 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43  (v, OP_Close, iC
1dca6 75 72 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 0a 20  ur, 0);.    }.. 
1dca7 20 20 20 2f 2a 20 46 69 72 65 20 74 68 65 20 42     /* Fire the B
1dca8 45 46 4f 52 45 20 61 6e 64 20 49 4e 53 54 45 41  EFORE and INSTEA
1dca9 44 20 4f 46 20 74 72 69 67 67 65 72 73 0a 20 20  D OF triggers.  
1dcaa 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c    */.    if( sql
1dcab 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67  ite3CodeRowTrigg
1dcac 65 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 55 50  er(pParse, TK_UP
1dcad 44 41 54 45 2c 20 70 43 68 61 6e 67 65 73 2c 20  DATE, pChanges, 
1dcae 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 2c 20  TRIGGER_BEFORE, 
1dcaf 70 54 61 62 2c 0a 20 20 20 20 20 20 20 20 20 20  pTab,.          
1dcb0 6e 65 77 49 64 78 2c 20 6f 6c 64 49 64 78 2c 20  newIdx, oldIdx, 
1dcb1 6f 6e 45 72 72 6f 72 2c 20 61 64 64 72 29 20 29  onError, addr) )
1dcb2 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 75 70 64  {.      goto upd
1dcb3 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20  ate_cleanup;.   
1dcb4 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 69   }.  }..  if( !i
1dcb5 73 56 69 65 77 20 26 26 20 21 49 73 56 69 72 74  sView && !IsVirt
1dcb6 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20  ual(pTab) ){.   
1dcb7 20 2f 2a 20 0a 20 20 20 20 2a 2a 20 4f 70 65 6e   /* .    ** Open
1dcb8 20 65 76 65 72 79 20 69 6e 64 65 78 20 74 68 61   every index tha
1dcb9 74 20 6e 65 65 64 73 20 75 70 64 61 74 69 6e 67  t needs updating
1dcba 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20  .  Note that if 
1dcbb 61 6e 79 0a 20 20 20 20 2a 2a 20 69 6e 64 65 78  any.    ** index
1dcbc 20 63 6f 75 6c 64 20 70 6f 74 65 6e 74 69 61 6c   could potential
1dcbd 6c 79 20 69 6e 76 6f 6b 65 20 61 20 52 45 50 4c  ly invoke a REPL
1dcbe 41 43 45 20 63 6f 6e 66 6c 69 63 74 20 72 65 73  ACE conflict res
1dcbf 6f 6c 75 74 69 6f 6e 20 0a 20 20 20 20 2a 2a 20  olution .    ** 
1dcc0 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 77 65 20  action, then we 
1dcc1 6e 65 65 64 20 74 6f 20 6f 70 65 6e 20 61 6c 6c  need to open all
1dcc2 20 69 6e 64 69 63 65 73 20 62 65 63 61 75 73 65   indices because
1dcc3 20 77 65 20 6d 69 67 68 74 20 6e 65 65 64 0a 20   we might need. 
1dcc4 20 20 20 2a 2a 20 74 6f 20 62 65 20 64 65 6c 65     ** to be dele
1dcc5 74 69 6e 67 20 73 6f 6d 65 20 72 65 63 6f 72 64  ting some record
1dcc6 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  s..    */.    sq
1dcc7 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70  lite3OpenTable(p
1dcc8 50 61 72 73 65 2c 20 69 43 75 72 2c 20 69 44 62  Parse, iCur, iDb
1dcc9 2c 20 70 54 61 62 2c 20 4f 50 5f 4f 70 65 6e 57  , pTab, OP_OpenW
1dcca 72 69 74 65 29 3b 20 0a 20 20 20 20 69 66 28 20  rite); .    if( 
1dccb 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c  onError==OE_Repl
1dccc 61 63 65 20 29 7b 0a 20 20 20 20 20 20 6f 70 65  ace ){.      ope
1dccd 6e 41 6c 6c 20 3d 20 31 3b 0a 20 20 20 20 7d 65  nAll = 1;.    }e
1dcce 6c 73 65 7b 0a 20 20 20 20 20 20 6f 70 65 6e 41  lse{.      openA
1dccf 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 66 6f  ll = 0;.      fo
1dcd0 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  r(pIdx=pTab->pIn
1dcd1 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
1dcd2 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  pIdx->pNext){.  
1dcd3 20 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e        if( pIdx->
1dcd4 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c  onError==OE_Repl
1dcd5 61 63 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ace ){.         
1dcd6 20 6f 70 65 6e 41 6c 6c 20 3d 20 31 3b 0a 20 20   openAll = 1;.  
1dcd7 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
1dcd8 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
1dcd9 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69  .    }.    for(i
1dcda 3d 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70  =0, pIdx=pTab->p
1dcdb 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64  Index; pIdx; pId
1dcdc 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 69  x=pIdx->pNext, i
1dcdd 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6f  ++){.      if( o
1dcde 70 65 6e 41 6c 6c 20 7c 7c 20 61 49 64 78 55 73  penAll || aIdxUs
1dcdf 65 64 5b 69 5d 20 29 7b 0a 20 20 20 20 20 20 20  ed[i] ){.       
1dce0 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d   KeyInfo *pKey =
1dce1 20 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79   sqlite3IndexKey
1dce2 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 49 64  info(pParse, pId
1dce3 78 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  x);.        sqli
1dce4 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1dce5 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 44 62 2c  OP_Integer, iDb,
1dce6 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c   0);.        sql
1dce7 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20 4f  ite3VdbeOp3(v, O
1dce8 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69 43 75  P_OpenWrite, iCu
1dce9 72 2b 69 2b 31 2c 20 70 49 64 78 2d 3e 74 6e 75  r+i+1, pIdx->tnu
1dcea 6d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  m,.             
1dceb 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a            (char*
1dcec 29 70 4b 65 79 2c 20 50 33 5f 4b 45 59 49 4e 46  )pKey, P3_KEYINF
1dced 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20  O_HANDOFF);.    
1dcee 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72      assert( pPar
1dcef 73 65 2d 3e 6e 54 61 62 3e 69 43 75 72 2b 69 2b  se->nTab>iCur+i+
1dcf0 31 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  1 );.      }.   
1dcf1 20 7d 0a 0a 20 20 20 20 2f 2a 20 4c 6f 6f 70 20   }..    /* Loop 
1dcf2 6f 76 65 72 20 65 76 65 72 79 20 72 65 63 6f 72  over every recor
1dcf3 64 20 74 68 61 74 20 6e 65 65 64 73 20 75 70 64  d that needs upd
1dcf4 61 74 69 6e 67 2e 20 20 57 65 20 68 61 76 65 20  ating.  We have 
1dcf5 74 6f 20 6c 6f 61 64 0a 20 20 20 20 2a 2a 20 74  to load.    ** t
1dcf6 68 65 20 6f 6c 64 20 64 61 74 61 20 66 6f 72 20  he old data for 
1dcf7 65 61 63 68 20 72 65 63 6f 72 64 20 74 6f 20 62  each record to b
1dcf8 65 20 75 70 64 61 74 65 64 20 62 65 63 61 75 73  e updated becaus
1dcf9 65 20 73 6f 6d 65 20 63 6f 6c 75 6d 6e 73 0a 20  e some columns. 
1dcfa 20 20 20 2a 2a 20 6d 69 67 68 74 20 6e 6f 74 20     ** might not 
1dcfb 63 68 61 6e 67 65 20 61 6e 64 20 77 65 20 77 69  change and we wi
1dcfc 6c 6c 20 6e 65 65 64 20 74 6f 20 63 6f 70 79 20  ll need to copy 
1dcfd 74 68 65 20 6f 6c 64 20 76 61 6c 75 65 2e 0a 20  the old value.. 
1dcfe 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 74 68 65 20     ** Also, the 
1dcff 6f 6c 64 20 64 61 74 61 20 69 73 20 6e 65 65 64  old data is need
1dd00 65 64 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65  ed to delete the
1dd01 20 6f 6c 64 20 69 6e 64 65 78 20 65 6e 74 72 69   old index entri
1dd02 65 73 2e 0a 20 20 20 20 2a 2a 20 53 6f 20 6d 61  es..    ** So ma
1dd03 6b 65 20 74 68 65 20 63 75 72 73 6f 72 20 70 6f  ke the cursor po
1dd04 69 6e 74 20 61 74 20 74 68 65 20 6f 6c 64 20 72  int at the old r
1dd05 65 63 6f 72 64 2e 0a 20 20 20 20 2a 2f 0a 20 20  ecord..    */.  
1dd06 20 20 69 66 28 20 21 74 72 69 67 67 65 72 73 5f    if( !triggers_
1dd07 65 78 69 73 74 20 29 7b 0a 20 20 20 20 20 20 61  exist ){.      a
1dd08 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
1dd09 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 46 69 66  eAddOp(v, OP_Fif
1dd0a 6f 52 65 61 64 2c 20 30 2c 20 30 29 3b 0a 20 20  oRead, 0, 0);.  
1dd0b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1dd0c 64 64 4f 70 28 76 2c 20 4f 50 5f 44 75 70 2c 20  ddOp(v, OP_Dup, 
1dd0d 30 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20  0, 0);.    }.   
1dd0e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1dd0f 70 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74  p(v, OP_NotExist
1dd10 73 2c 20 69 43 75 72 2c 20 61 64 64 72 29 3b 0a  s, iCur, addr);.
1dd11 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 72  .    /* If the r
1dd12 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 77 69 6c  ecord number wil
1dd13 6c 20 63 68 61 6e 67 65 2c 20 70 75 73 68 20 74  l change, push t
1dd14 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  he record number
1dd15 20 61 73 20 69 74 0a 20 20 20 20 2a 2a 20 77 69   as it.    ** wi
1dd16 6c 6c 20 62 65 20 61 66 74 65 72 20 74 68 65 20  ll be after the 
1dd17 75 70 64 61 74 65 2e 20 28 54 68 65 20 6f 6c 64  update. (The old
1dd18 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 69   record number i
1dd19 73 20 63 75 72 72 65 6e 74 6c 79 0a 20 20 20 20  s currently.    
1dd1a 2a 2a 20 6f 6e 20 74 6f 70 20 6f 66 20 74 68 65  ** on top of the
1dd1b 20 73 74 61 63 6b 2e 29 0a 20 20 20 20 2a 2f 0a   stack.).    */.
1dd1c 20 20 20 20 69 66 28 20 63 68 6e 67 52 6f 77 69      if( chngRowi
1dd1d 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  d ){.      sqlit
1dd1e 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
1dd1f 65 2c 20 70 52 6f 77 69 64 45 78 70 72 29 3b 0a  e, pRowidExpr);.
1dd20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1dd21 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 75 73  eAddOp(v, OP_Mus
1dd22 74 42 65 49 6e 74 2c 20 30 2c 20 30 29 3b 0a 20  tBeInt, 0, 0);. 
1dd23 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 6d     }..    /* Com
1dd24 70 75 74 65 20 6e 65 77 20 64 61 74 61 20 66 6f  pute new data fo
1dd25 72 20 74 68 69 73 20 72 65 63 6f 72 64 2e 20 20  r this record.  
1dd26 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28  .    */.    for(
1dd27 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f  i=0; i<pTab->nCo
1dd28 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  l; i++){.      i
1dd29 66 28 20 69 3d 3d 70 54 61 62 2d 3e 69 50 4b 65  f( i==pTab->iPKe
1dd2a 79 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  y ){.        sql
1dd2b 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1dd2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 30 29 3b   OP_Null, 0, 0);
1dd2d 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75  .        continu
1dd2e 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  e;.      }.     
1dd2f 20 6a 20 3d 20 61 58 52 65 66 5b 69 5d 3b 0a 20   j = aXRef[i];. 
1dd30 20 20 20 20 20 69 66 28 20 6a 3c 30 20 29 7b 0a       if( j<0 ){.
1dd31 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1dd32 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43  dbeAddOp(v, OP_C
1dd33 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 69 29 3b  olumn, iCur, i);
1dd34 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1dd35 43 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 76 2c  ColumnDefault(v,
1dd36 20 70 54 61 62 2c 20 69 29 3b 0a 20 20 20 20 20   pTab, i);.     
1dd37 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1dd38 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
1dd39 70 50 61 72 73 65 2c 20 70 43 68 61 6e 67 65 73  pParse, pChanges
1dd3a 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72 29 3b 0a 20  ->a[j].pExpr);. 
1dd3b 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
1dd3c 20 20 2f 2a 20 44 6f 20 63 6f 6e 73 74 72 61 69    /* Do constrai
1dd3d 6e 74 20 63 68 65 63 6b 73 0a 20 20 20 20 2a 2f  nt checks.    */
1dd3e 0a 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65  .    sqlite3Gene
1dd3f 72 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 68  rateConstraintCh
1dd40 65 63 6b 73 28 70 50 61 72 73 65 2c 20 70 54 61  ecks(pParse, pTa
1dd41 62 2c 20 69 43 75 72 2c 20 61 49 64 78 55 73 65  b, iCur, aIdxUse
1dd42 64 2c 20 63 68 6e 67 52 6f 77 69 64 2c 20 31 2c  d, chngRowid, 1,
1dd43 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1dd44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dd45 20 20 20 20 6f 6e 45 72 72 6f 72 2c 20 61 64 64      onError, add
1dd46 72 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65  r);..    /* Dele
1dd47 74 65 20 74 68 65 20 6f 6c 64 20 69 6e 64 69 63  te the old indic
1dd48 65 73 20 66 6f 72 20 74 68 65 20 63 75 72 72 65  es for the curre
1dd49 6e 74 20 72 65 63 6f 72 64 2e 0a 20 20 20 20 2a  nt record..    *
1dd4a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e  /.    sqlite3Gen
1dd4b 65 72 61 74 65 52 6f 77 49 6e 64 65 78 44 65 6c  erateRowIndexDel
1dd4c 65 74 65 28 76 2c 20 70 54 61 62 2c 20 69 43 75  ete(v, pTab, iCu
1dd4d 72 2c 20 61 49 64 78 55 73 65 64 29 3b 0a 0a 20  r, aIdxUsed);.. 
1dd4e 20 20 20 2f 2a 20 49 66 20 63 68 61 6e 67 69 6e     /* If changin
1dd4f 67 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d  g the record num
1dd50 62 65 72 2c 20 64 65 6c 65 74 65 20 74 68 65 20  ber, delete the 
1dd51 6f 6c 64 20 72 65 63 6f 72 64 2e 0a 20 20 20 20  old record..    
1dd52 2a 2f 0a 20 20 20 20 69 66 28 20 63 68 6e 67 52  */.    if( chngR
1dd53 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 73 71  owid ){.      sq
1dd54 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1dd55 2c 20 4f 50 5f 44 65 6c 65 74 65 2c 20 69 43 75  , OP_Delete, iCu
1dd56 72 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  r, 0);.    }..  
1dd57 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65 20    /* Create the 
1dd58 6e 65 77 20 69 6e 64 65 78 20 65 6e 74 72 69 65  new index entrie
1dd59 73 20 61 6e 64 20 74 68 65 20 6e 65 77 20 72 65  s and the new re
1dd5a 63 6f 72 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  cord..    */.   
1dd5b 20 73 71 6c 69 74 65 33 43 6f 6d 70 6c 65 74 65   sqlite3Complete
1dd5c 49 6e 73 65 72 74 69 6f 6e 28 70 50 61 72 73 65  Insertion(pParse
1dd5d 2c 20 70 54 61 62 2c 20 69 43 75 72 2c 20 61 49  , pTab, iCur, aI
1dd5e 64 78 55 73 65 64 2c 20 63 68 6e 67 52 6f 77 69  dxUsed, chngRowi
1dd5f 64 2c 20 31 2c 20 2d 31 2c 20 30 29 3b 0a 20 20  d, 1, -1, 0);.  
1dd60 7d 0a 0a 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e  }..  /* Incremen
1dd61 74 20 74 68 65 20 72 6f 77 20 63 6f 75 6e 74 65  t the row counte
1dd62 72 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62  r .  */.  if( db
1dd63 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
1dd64 5f 43 6f 75 6e 74 52 6f 77 73 20 26 26 20 21 70  _CountRows && !p
1dd65 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b  Parse->trigStack
1dd66 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
1dd67 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65  beAddOp(v, OP_Me
1dd68 6d 49 6e 63 72 2c 20 31 2c 20 6d 65 6d 43 6e 74  mIncr, 1, memCnt
1dd69 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  );.  }..  /* If 
1dd6a 74 68 65 72 65 20 61 72 65 20 74 72 69 67 67 65  there are trigge
1dd6b 72 73 2c 20 63 6c 6f 73 65 20 61 6c 6c 20 74 68  rs, close all th
1dd6c 65 20 63 75 72 73 6f 72 73 20 61 66 74 65 72 20  e cursors after 
1dd6d 65 61 63 68 20 69 74 65 72 61 74 69 6f 6e 0a 20  each iteration. 
1dd6e 20 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65 20   ** through the 
1dd6f 6c 6f 6f 70 2e 20 20 54 68 65 20 66 69 72 65 20  loop.  The fire 
1dd70 74 68 65 20 61 66 74 65 72 20 74 72 69 67 67 65  the after trigge
1dd71 72 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 74  rs..  */.  if( t
1dd72 72 69 67 67 65 72 73 5f 65 78 69 73 74 20 29 7b  riggers_exist ){
1dd73 0a 20 20 20 20 69 66 28 20 21 69 73 56 69 65 77  .    if( !isView
1dd74 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d   ){.      for(i=
1dd75 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  0, pIdx=pTab->pI
1dd76 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78  ndex; pIdx; pIdx
1dd77 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 69 2b  =pIdx->pNext, i+
1dd78 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  +){.        if( 
1dd79 6f 70 65 6e 41 6c 6c 20 7c 7c 20 61 49 64 78 55  openAll || aIdxU
1dd7a 73 65 64 5b 69 5d 20 29 0a 20 20 20 20 20 20 20  sed[i] ).       
1dd7b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1dd7c 64 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  dOp(v, OP_Close,
1dd7d 20 69 43 75 72 2b 69 2b 31 2c 20 30 29 3b 0a 20   iCur+i+1, 0);. 
1dd7e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
1dd7f 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1dd80 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72 2c   OP_Close, iCur,
1dd81 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69   0);.    }.    i
1dd82 66 28 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f  f( sqlite3CodeRo
1dd83 77 54 72 69 67 67 65 72 28 70 50 61 72 73 65 2c  wTrigger(pParse,
1dd84 20 54 4b 5f 55 50 44 41 54 45 2c 20 70 43 68 61   TK_UPDATE, pCha
1dd85 6e 67 65 73 2c 20 54 52 49 47 47 45 52 5f 41 46  nges, TRIGGER_AF
1dd86 54 45 52 2c 20 70 54 61 62 2c 20 0a 20 20 20 20  TER, pTab, .    
1dd87 20 20 20 20 20 20 6e 65 77 49 64 78 2c 20 6f 6c        newIdx, ol
1dd88 64 49 64 78 2c 20 6f 6e 45 72 72 6f 72 2c 20 61  dIdx, onError, a
1dd89 64 64 72 29 20 29 7b 0a 20 20 20 20 20 20 67 6f  ddr) ){.      go
1dd8a 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75  to update_cleanu
1dd8b 70 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  p;.    }.  }..  
1dd8c 2f 2a 20 52 65 70 65 61 74 20 74 68 65 20 61 62  /* Repeat the ab
1dd8d 6f 76 65 20 77 69 74 68 20 74 68 65 20 6e 65 78  ove with the nex
1dd8e 74 20 72 65 63 6f 72 64 20 74 6f 20 62 65 20 75  t record to be u
1dd8f 70 64 61 74 65 64 2c 20 75 6e 74 69 6c 0a 20 20  pdated, until.  
1dd90 2a 2a 20 61 6c 6c 20 72 65 63 6f 72 64 20 73 65  ** all record se
1dd91 6c 65 63 74 65 64 20 62 79 20 74 68 65 20 57 48  lected by the WH
1dd92 45 52 45 20 63 6c 61 75 73 65 20 68 61 76 65 20  ERE clause have 
1dd93 62 65 65 6e 20 75 70 64 61 74 65 64 2e 0a 20 20  been updated..  
1dd94 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  */.  sqlite3Vdbe
1dd95 41 64 64 4f 70 28 76 2c 20 4f 50 5f 47 6f 74 6f  AddOp(v, OP_Goto
1dd96 2c 20 30 2c 20 61 64 64 72 29 3b 0a 20 20 73 71  , 0, addr);.  sq
1dd97 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
1dd98 65 28 76 2c 20 61 64 64 72 29 3b 0a 0a 20 20 2f  e(v, addr);..  /
1dd99 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 74 61 62 6c  * Close all tabl
1dd9a 65 73 20 69 66 20 74 68 65 72 65 20 77 65 72 65  es if there were
1dd9b 20 6e 6f 20 46 4f 52 20 45 41 43 48 20 52 4f 57   no FOR EACH ROW
1dd9c 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 69   triggers */.  i
1dd9d 66 28 20 21 74 72 69 67 67 65 72 73 5f 65 78 69  f( !triggers_exi
1dd9e 73 74 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d  st ){.    for(i=
1dd9f 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  0, pIdx=pTab->pI
1dda0 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78  ndex; pIdx; pIdx
1dda1 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 69 2b  =pIdx->pNext, i+
1dda2 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6f 70  +){.      if( op
1dda3 65 6e 41 6c 6c 20 7c 7c 20 61 49 64 78 55 73 65  enAll || aIdxUse
1dda4 64 5b 69 5d 20 29 7b 0a 20 20 20 20 20 20 20 20  d[i] ){.        
1dda5 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1dda6 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43  (v, OP_Close, iC
1dda7 75 72 2b 69 2b 31 2c 20 30 29 3b 0a 20 20 20 20  ur+i+1, 0);.    
1dda8 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71    }.    }.    sq
1dda9 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1ddaa 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72  , OP_Close, iCur
1ddab 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  , 0);.  }else{. 
1ddac 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1ddad 64 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  dOp(v, OP_Close,
1ddae 20 6e 65 77 49 64 78 2c 20 30 29 3b 0a 20 20 20   newIdx, 0);.   
1ddaf 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1ddb0 70 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 6f  p(v, OP_Close, o
1ddb1 6c 64 49 64 78 2c 20 30 29 3b 0a 20 20 7d 0a 0a  ldIdx, 0);.  }..
1ddb2 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e    /*.  ** Return
1ddb3 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72   the number of r
1ddb4 6f 77 73 20 74 68 61 74 20 77 65 72 65 20 63 68  ows that were ch
1ddb5 61 6e 67 65 64 2e 20 49 66 20 74 68 69 73 20 72  anged. If this r
1ddb6 6f 75 74 69 6e 65 20 69 73 20 0a 20 20 2a 2a 20  outine is .  ** 
1ddb7 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 20  generating code 
1ddb8 62 65 63 61 75 73 65 20 6f 66 20 61 20 63 61 6c  because of a cal
1ddb9 6c 20 74 6f 20 73 71 6c 69 74 65 33 4e 65 73 74  l to sqlite3Nest
1ddba 65 64 50 61 72 73 65 28 29 2c 20 64 6f 20 6e 6f  edParse(), do no
1ddbb 74 0a 20 20 2a 2a 20 69 6e 76 6f 6b 65 20 74 68  t.  ** invoke th
1ddbc 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  e callback funct
1ddbd 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ion..  */.  if( 
1ddbe 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
1ddbf 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 26 26 20  TE_CountRows && 
1ddc0 21 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61  !pParse->trigSta
1ddc1 63 6b 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 65  ck && pParse->ne
1ddc2 73 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 73  sted==0 ){.    s
1ddc3 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1ddc4 76 2c 20 4f 50 5f 4d 65 6d 4c 6f 61 64 2c 20 6d  v, OP_MemLoad, m
1ddc5 65 6d 43 6e 74 2c 20 30 29 3b 0a 20 20 20 20 73  emCnt, 0);.    s
1ddc6 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1ddc7 76 2c 20 4f 50 5f 43 61 6c 6c 62 61 63 6b 2c 20  v, OP_Callback, 
1ddc8 31 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  1, 0);.    sqlit
1ddc9 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
1ddca 28 76 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69  (v, 1);.    sqli
1ddcb 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
1ddcc 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 0, COLNAME_
1ddcd 4e 41 4d 45 2c 20 22 72 6f 77 73 20 75 70 64 61  NAME, "rows upda
1ddce 74 65 64 22 2c 20 50 33 5f 53 54 41 54 49 43 29  ted", P3_STATIC)
1ddcf 3b 0a 20 20 7d 0a 0a 75 70 64 61 74 65 5f 63 6c  ;.  }..update_cl
1ddd0 65 61 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33  eanup:.  sqlite3
1ddd1 41 75 74 68 43 6f 6e 74 65 78 74 50 6f 70 28 26  AuthContextPop(&
1ddd2 73 43 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c  sContext);.  sql
1ddd3 69 74 65 46 72 65 65 28 61 70 49 64 78 29 3b 0a  iteFree(apIdx);.
1ddd4 20 20 73 71 6c 69 74 65 46 72 65 65 28 61 58 52    sqliteFree(aXR
1ddd5 65 66 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 72  ef);.  sqlite3Sr
1ddd6 63 4c 69 73 74 44 65 6c 65 74 65 28 70 54 61 62  cListDelete(pTab
1ddd7 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  List);.  sqlite3
1ddd8 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70  ExprListDelete(p
1ddd9 43 68 61 6e 67 65 73 29 3b 0a 20 20 73 71 6c 69  Changes);.  sqli
1ddda 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 70 57  te3ExprDelete(pW
1dddb 68 65 72 65 29 3b 0a 20 20 72 65 74 75 72 6e 3b  here);.  return;
1dddc 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
1dddd 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
1ddde 41 42 4c 45 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  ABLE./*.** Gener
1dddf 61 74 65 20 63 6f 64 65 20 66 6f 72 20 61 6e 20  ate code for an 
1dde0 55 50 44 41 54 45 20 6f 66 20 61 20 76 69 72 74  UPDATE of a virt
1dde1 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  ual table..**.**
1dde2 20 54 68 65 20 73 74 72 61 74 65 67 79 20 69 73   The strategy is
1dde3 20 74 68 61 74 20 77 65 20 63 72 65 61 74 65 20   that we create 
1dde4 61 6e 20 65 70 68 65 6d 65 72 69 61 6c 20 74 61  an ephemerial ta
1dde5 62 6c 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ble that contain
1dde6 73 0a 2a 2a 20 66 6f 72 20 65 61 63 68 20 72 6f  s.** for each ro
1dde7 77 20 74 6f 20 62 65 20 63 68 61 6e 67 65 64 3a  w to be changed:
1dde8 0a 2a 2a 0a 2a 2a 20 20 20 28 41 29 20 20 54 68  .**.**   (A)  Th
1dde9 65 20 6f 72 69 67 69 6e 61 6c 20 72 6f 77 69 64  e original rowid
1ddea 20 6f 66 20 74 68 61 74 20 72 6f 77 2e 0a 2a 2a   of that row..**
1ddeb 20 20 20 28 42 29 20 20 54 68 65 20 72 65 76 69     (B)  The revi
1ddec 73 65 64 20 72 6f 77 69 64 20 66 6f 72 20 74 68  sed rowid for th
1dded 65 20 72 6f 77 2e 20 28 6e 6f 74 65 31 29 0a 2a  e row. (note1).*
1ddee 2a 20 20 20 28 43 29 20 20 54 68 65 20 63 6f 6e  *   (C)  The con
1ddef 74 65 6e 74 20 6f 66 20 65 76 65 72 79 20 63 6f  tent of every co
1ddf0 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72 6f 77 2e  lumn in the row.
1ddf1 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 77 65 20 6c  .**.** Then we l
1ddf2 6f 6f 70 20 6f 76 65 72 20 74 68 69 73 20 65 70  oop over this ep
1ddf3 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 61 6e  hemeral table an
1ddf4 64 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 69  d for each row i
1ddf5 6e 0a 2a 2a 20 74 68 65 20 65 70 68 65 72 6d 65  n.** the epherme
1ddf6 72 61 6c 20 74 61 62 6c 65 20 63 61 6c 6c 20 56  ral table call V
1ddf7 55 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 57 68  Update..**.** Wh
1ddf8 65 6e 20 66 69 6e 69 73 68 65 64 2c 20 64 72 6f  en finished, dro
1ddf9 70 20 74 68 65 20 65 70 68 65 6d 65 72 61 6c 20  p the ephemeral 
1ddfa 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 28 6e 6f  table..**.** (no
1ddfb 74 65 31 29 20 41 63 74 75 61 6c 6c 79 2c 20 69  te1) Actually, i
1ddfc 66 20 77 65 20 6b 6e 6f 77 20 69 6e 20 61 64 76  f we know in adv
1ddfd 61 6e 63 65 20 74 68 61 74 20 28 41 29 20 69 73  ance that (A) is
1ddfe 20 61 6c 77 61 79 73 20 74 68 65 20 73 61 6d 65   always the same
1ddff 0a 2a 2a 20 61 73 20 28 42 29 20 77 65 20 6f 6e  .** as (B) we on
1de00 6c 79 20 73 74 6f 72 65 20 28 41 29 2c 20 74 68  ly store (A), th
1de01 65 6e 20 64 75 70 6c 69 63 61 74 65 20 28 41 29  en duplicate (A)
1de02 20 77 68 65 6e 20 70 75 6c 6c 69 6e 67 0a 2a 2a   when pulling.**
1de03 20 69 74 20 6f 75 74 20 6f 66 20 74 68 65 20 65   it out of the e
1de04 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 62  phemeral table b
1de05 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 56 55  efore calling VU
1de06 70 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  pdate..*/.static
1de07 20 76 6f 69 64 20 75 70 64 61 74 65 56 69 72 74   void updateVirt
1de08 75 61 6c 54 61 62 6c 65 28 0a 20 20 50 61 72 73  ualTable(.  Pars
1de09 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
1de0a 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20   /* The parsing 
1de0b 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63  context */.  Src
1de0c 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20  List *pSrc,     
1de0d 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c    /* The virtual
1de0e 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6d 6f 64   table to be mod
1de0f 69 66 69 65 64 20 2a 2f 0a 20 20 54 61 62 6c 65  ified */.  Table
1de10 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20 20   *pTab,         
1de11 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 74  /* The virtual t
1de12 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69  able */.  ExprLi
1de13 73 74 20 2a 70 43 68 61 6e 67 65 73 2c 20 20 2f  st *pChanges,  /
1de14 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20 74 6f  * The columns to
1de15 20 63 68 61 6e 67 65 20 69 6e 20 74 68 65 20 55   change in the U
1de16 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20  PDATE statement 
1de17 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 6f 77 69  */.  Expr *pRowi
1de18 64 2c 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70  d,        /* Exp
1de19 72 65 73 73 69 6f 6e 20 75 73 65 64 20 74 6f 20  ression used to 
1de1a 72 65 63 6f 6d 70 75 74 65 20 74 68 65 20 72 6f  recompute the ro
1de1b 77 69 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 58  wid */.  int *aX
1de1c 52 65 66 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  Ref,          /*
1de1d 20 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 63 6f   Mapping from co
1de1e 6c 75 6d 6e 73 20 6f 66 20 70 54 61 62 20 74 6f  lumns of pTab to
1de1f 20 65 6e 74 72 69 65 73 20 69 6e 20 70 43 68 61   entries in pCha
1de20 6e 67 65 73 20 2a 2f 0a 20 20 45 78 70 72 20 2a  nges */.  Expr *
1de21 70 57 68 65 72 65 20 20 20 20 20 20 20 20 20 2f  pWhere         /
1de22 2a 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 6f  * WHERE clause o
1de23 66 20 74 68 65 20 55 50 44 41 54 45 20 73 74 61  f the UPDATE sta
1de24 74 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 56  tement */.){.  V
1de25 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d  dbe *v = pParse-
1de26 3e 70 56 64 62 65 3b 20 20 2f 2a 20 56 69 72 74  >pVdbe;  /* Virt
1de27 75 61 6c 20 6d 61 63 68 69 6e 65 20 75 6e 64 65  ual machine unde
1de28 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a  r construction *
1de29 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
1de2a 4c 69 73 74 20 3d 20 30 3b 20 20 20 20 20 2f 2a  List = 0;     /*
1de2b 20 54 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   The result set 
1de2c 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 73 74  of the SELECT st
1de2d 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 53 65 6c  atement */.  Sel
1de2e 65 63 74 20 2a 70 53 65 6c 65 63 74 20 3d 20 30  ect *pSelect = 0
1de2f 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45  ;      /* The SE
1de30 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a  LECT statement *
1de31 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b  /.  Expr *pExpr;
1de32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1de33 20 54 65 6d 70 6f 72 61 72 79 20 65 78 70 72 65   Temporary expre
1de34 73 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 65  ssion */.  int e
1de35 70 68 65 6d 54 61 62 3b 20 20 20 20 20 20 20 20  phemTab;        
1de36 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 68 6f       /* Table ho
1de37 6c 64 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74  lding the result
1de38 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 2a   of the SELECT *
1de39 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  /.  int i;      
1de3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1de3b 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
1de3c 0a 20 20 69 6e 74 20 61 64 64 72 3b 20 20 20 20  .  int addr;    
1de3d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1de3e 41 64 64 72 65 73 73 20 6f 66 20 74 6f 70 20 6f  Address of top o
1de3f 66 20 6c 6f 6f 70 20 2a 2f 0a 0a 20 20 2f 2a 20  f loop */..  /* 
1de40 43 6f 6e 73 74 72 75 63 74 20 74 68 65 20 53 45  Construct the SE
1de41 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74  LECT statement t
1de42 68 61 74 20 77 69 6c 6c 20 66 69 6e 64 20 74 68  hat will find th
1de43 65 20 6e 65 77 20 76 61 6c 75 65 73 20 66 6f 72  e new values for
1de44 0a 20 20 2a 2a 20 61 6c 6c 20 75 70 64 61 74 65  .  ** all update
1de45 64 20 72 6f 77 73 2e 20 0a 20 20 2a 2f 0a 20 20  d rows. .  */.  
1de46 70 45 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33  pEList = sqlite3
1de47 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 30  ExprListAppend(0
1de48 2c 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 49  , sqlite3CreateI
1de49 64 45 78 70 72 28 22 5f 72 6f 77 69 64 5f 22 29  dExpr("_rowid_")
1de4a 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 52 6f 77  , 0);.  if( pRow
1de4b 69 64 20 29 7b 0a 20 20 20 20 70 45 4c 69 73 74  id ){.    pEList
1de4c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
1de4d 73 74 41 70 70 65 6e 64 28 70 45 4c 69 73 74 2c  stAppend(pEList,
1de4e 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
1de4f 70 52 6f 77 69 64 29 2c 20 30 29 3b 0a 20 20 7d  pRowid), 0);.  }
1de50 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d  .  assert( pTab-
1de51 3e 69 50 4b 65 79 3c 30 20 29 3b 0a 20 20 66 6f  >iPKey<0 );.  fo
1de52 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e  r(i=0; i<pTab->n
1de53 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  Col; i++){.    i
1de54 66 28 20 61 58 52 65 66 5b 69 5d 3e 3d 30 20 29  f( aXRef[i]>=0 )
1de55 7b 0a 20 20 20 20 20 20 70 45 78 70 72 20 3d 20  {.      pExpr = 
1de56 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 70  sqlite3ExprDup(p
1de57 43 68 61 6e 67 65 73 2d 3e 61 5b 61 58 52 65 66  Changes->a[aXRef
1de58 5b 69 5d 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20  [i]].pExpr);.   
1de59 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 45   }else{.      pE
1de5a 78 70 72 20 3d 20 73 71 6c 69 74 65 33 43 72 65  xpr = sqlite3Cre
1de5b 61 74 65 49 64 45 78 70 72 28 70 54 61 62 2d 3e  ateIdExpr(pTab->
1de5c 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a  aCol[i].zName);.
1de5d 20 20 20 20 7d 0a 20 20 20 20 70 45 4c 69 73 74      }.    pEList
1de5e 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
1de5f 73 74 41 70 70 65 6e 64 28 70 45 4c 69 73 74 2c  stAppend(pEList,
1de60 20 70 45 78 70 72 2c 20 30 29 3b 0a 20 20 7d 0a   pExpr, 0);.  }.
1de61 20 20 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69    pSelect = sqli
1de62 74 65 33 53 65 6c 65 63 74 4e 65 77 28 70 45 4c  te3SelectNew(pEL
1de63 69 73 74 2c 20 70 53 72 63 2c 20 70 57 68 65 72  ist, pSrc, pWher
1de64 65 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  e, 0, 0, 0, 0, 0
1de65 2c 20 30 29 3b 0a 20 20 0a 20 20 2f 2a 20 43 72  , 0);.  .  /* Cr
1de66 65 61 74 65 20 74 68 65 20 65 70 68 65 6d 65 72  eate the ephemer
1de67 61 6c 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68  al table into wh
1de68 69 63 68 20 74 68 65 20 75 70 64 61 74 65 20 72  ich the update r
1de69 65 73 75 6c 74 73 20 77 69 6c 6c 0a 20 20 2a 2a  esults will.  **
1de6a 20 62 65 20 73 74 6f 72 65 64 2e 0a 20 20 2a 2f   be stored..  */
1de6b 0a 20 20 61 73 73 65 72 74 28 20 76 20 29 3b 0a  .  assert( v );.
1de6c 20 20 65 70 68 65 6d 54 61 62 20 3d 20 70 50 61    ephemTab = pPa
1de6d 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 73  rse->nTab++;.  s
1de6e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1de6f 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  v, OP_OpenEpheme
1de70 72 61 6c 2c 20 65 70 68 65 6d 54 61 62 2c 20 70  ral, ephemTab, p
1de71 54 61 62 2d 3e 6e 43 6f 6c 2b 31 2b 28 70 52 6f  Tab->nCol+1+(pRo
1de72 77 69 64 21 3d 30 29 29 3b 0a 0a 20 20 2f 2a 20  wid!=0));..  /* 
1de73 66 69 6c 6c 20 74 68 65 20 65 70 68 65 6d 65 72  fill the ephemer
1de74 61 6c 20 74 61 62 6c 65 20 0a 20 20 2a 2f 0a 20  al table .  */. 
1de75 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70   sqlite3Select(p
1de76 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2c 20  Parse, pSelect, 
1de77 53 52 54 5f 54 61 62 6c 65 2c 20 65 70 68 65 6d  SRT_Table, ephem
1de78 54 61 62 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29  Tab, 0, 0, 0, 0)
1de79 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 47 65 6e  ;..  /*.  ** Gen
1de7a 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 73 63  erate code to sc
1de7b 61 6e 20 74 68 65 20 65 70 68 65 6d 65 72 61 6c  an the ephemeral
1de7c 20 74 61 62 6c 65 20 61 6e 64 20 63 61 6c 6c 20   table and call 
1de7d 56 44 65 6c 65 74 65 20 61 6e 64 0a 20 20 2a 2a  VDelete and.  **
1de7e 20 56 49 6e 73 65 72 74 0a 20 20 2a 2f 0a 20 20   VInsert.  */.  
1de7f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1de80 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 65  (v, OP_Rewind, e
1de81 70 68 65 6d 54 61 62 2c 20 30 29 3b 0a 20 20 61  phemTab, 0);.  a
1de82 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
1de83 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b  eCurrentAddr(v);
1de84 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
1de85 64 4f 70 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  dOp(v, OP_Column
1de86 2c 20 20 65 70 68 65 6d 54 61 62 2c 20 30 29 3b  ,  ephemTab, 0);
1de87 0a 20 20 69 66 28 20 70 52 6f 77 69 64 20 29 7b  .  if( pRowid ){
1de88 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1de89 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6f 6c 75  AddOp(v, OP_Colu
1de8a 6d 6e 2c 20 65 70 68 65 6d 54 61 62 2c 20 31 29  mn, ephemTab, 1)
1de8b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
1de8c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1de8d 76 2c 20 4f 50 5f 44 75 70 2c 20 30 2c 20 30 29  v, OP_Dup, 0, 0)
1de8e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b  ;.  }.  for(i=0;
1de8f 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69   i<pTab->nCol; i
1de90 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ++){.    sqlite3
1de91 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1de92 43 6f 6c 75 6d 6e 2c 20 65 70 68 65 6d 54 61 62  Column, ephemTab
1de93 2c 20 69 2b 31 2b 28 70 52 6f 77 69 64 21 3d 30  , i+1+(pRowid!=0
1de94 29 29 3b 0a 20 20 7d 0a 20 20 70 50 61 72 73 65  ));.  }.  pParse
1de95 2d 3e 70 56 69 72 74 75 61 6c 4c 6f 63 6b 20 3d  ->pVirtualLock =
1de96 20 70 54 61 62 3b 0a 20 20 73 71 6c 69 74 65 33   pTab;.  sqlite3
1de97 56 64 62 65 4f 70 33 28 76 2c 20 4f 50 5f 56 55  VdbeOp3(v, OP_VU
1de98 70 64 61 74 65 2c 20 30 2c 20 70 54 61 62 2d 3e  pdate, 0, pTab->
1de99 6e 43 6f 6c 2b 32 2c 20 0a 20 20 20 20 20 20 20  nCol+2, .       
1de9a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63                (c
1de9b 6f 6e 73 74 20 63 68 61 72 2a 29 70 54 61 62 2d  onst char*)pTab-
1de9c 3e 70 56 74 61 62 2c 20 50 33 5f 56 54 41 42 29  >pVtab, P3_VTAB)
1de9d 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
1de9e 64 64 4f 70 28 76 2c 20 4f 50 5f 4e 65 78 74 2c  ddOp(v, OP_Next,
1de9f 20 65 70 68 65 6d 54 61 62 2c 20 61 64 64 72 29   ephemTab, addr)
1dea0 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a  ;.  sqlite3VdbeJ
1dea1 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 2d  umpHere(v, addr-
1dea2 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  1);.  sqlite3Vdb
1dea3 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f  eAddOp(v, OP_Clo
1dea4 73 65 2c 20 65 70 68 65 6d 54 61 62 2c 20 30 29  se, ephemTab, 0)
1dea5 3b 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e 75 70 20  ;..  /* Cleanup 
1dea6 2a 2f 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65  */.  sqlite3Sele
1dea7 63 74 44 65 6c 65 74 65 28 70 53 65 6c 65 63 74  ctDelete(pSelect
1dea8 29 3b 20 20 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  );  .}.#endif /*
1dea9 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
1deaa 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a  TUALTABLE */../*
1deab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
1deac 64 20 6f 66 20 75 70 64 61 74 65 2e 63 20 2a 2a  d of update.c **
1dead 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1deae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1deaf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
1deb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
1deb1 67 69 6e 20 66 69 6c 65 20 76 61 63 75 75 6d 2e  gin file vacuum.
1deb2 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
1deb3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1deb4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
1deb5 0a 2a 2a 20 32 30 30 33 20 41 70 72 69 6c 20 36  .** 2003 April 6
1deb6 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
1deb7 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
1deb8 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
1deb9 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
1deba 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
1debb 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
1debc 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
1debd 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
1debe 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
1debf 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
1dec0 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
1dec1 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
1dec2 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
1dec3 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
1dec4 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
1dec5 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
1dec6 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
1dec7 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
1dec8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dec9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1deca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1decb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
1decc 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
1decd 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20  tains code used 
1dece 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
1decf 20 56 41 43 55 55 4d 20 63 6f 6d 6d 61 6e 64 2e   VACUUM command.
1ded0 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 6f 66 20 74  .**.** Most of t
1ded1 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20  he code in this 
1ded2 66 69 6c 65 20 6d 61 79 20 62 65 20 6f 6d 69 74  file may be omit
1ded3 74 65 64 20 62 79 20 64 65 66 69 6e 69 6e 67 20  ted by defining 
1ded4 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4d  the.** SQLITE_OM
1ded5 49 54 5f 56 41 43 55 55 4d 20 6d 61 63 72 6f 2e  IT_VACUUM macro.
1ded6 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 61 63 75  .**.** $Id: vacu
1ded7 75 6d 2e 63 2c 76 20 31 2e 36 39 20 32 30 30 37  um.c,v 1.69 2007
1ded8 2f 30 33 2f 32 37 20 31 36 3a 31 39 3a 35 32 20  /03/27 16:19:52 
1ded9 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20  danielk1977 Exp 
1deda 24 0a 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e  $.*/..#if !defin
1dedb 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
1dedc 41 43 55 55 4d 29 20 26 26 20 21 64 65 66 69 6e  ACUUM) && !defin
1dedd 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ed(SQLITE_OMIT_A
1dede 54 54 41 43 48 29 0a 2f 2a 0a 2a 2a 20 45 78 65  TTACH)./*.** Exe
1dedf 63 75 74 65 20 7a 53 71 6c 20 6f 6e 20 64 61 74  cute zSql on dat
1dee0 61 62 61 73 65 20 64 62 2e 20 52 65 74 75 72 6e  abase db. Return
1dee1 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a   an error code..
1dee2 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 65 78  */.static int ex
1dee3 65 63 53 71 6c 28 73 71 6c 69 74 65 33 20 2a 64  ecSql(sqlite3 *d
1dee4 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
1dee5 53 71 6c 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  Sql){.  sqlite3_
1dee6 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 20 20 69  stmt *pStmt;.  i
1dee7 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71  f( SQLITE_OK!=sq
1dee8 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 64 62  lite3_prepare(db
1dee9 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74  , zSql, -1, &pSt
1deea 6d 74 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65  mt, 0) ){.    re
1deeb 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 65 72 72  turn sqlite3_err
1deec 63 6f 64 65 28 64 62 29 3b 0a 20 20 7d 0a 20 20  code(db);.  }.  
1deed 77 68 69 6c 65 28 20 53 51 4c 49 54 45 5f 52 4f  while( SQLITE_RO
1deee 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65 70 28  W==sqlite3_step(
1deef 70 53 74 6d 74 29 20 29 7b 7d 0a 20 20 72 65 74  pStmt) ){}.  ret
1def0 75 72 6e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  urn sqlite3_fina
1def1 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a  lize(pStmt);.}..
1def2 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 7a 53  /*.** Execute zS
1def3 71 6c 20 6f 6e 20 64 61 74 61 62 61 73 65 20 64  ql on database d
1def4 62 2e 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74  b. The statement
1def5 20 72 65 74 75 72 6e 73 20 65 78 61 63 74 6c 79   returns exactly
1def6 0a 2a 2a 20 6f 6e 65 20 63 6f 6c 75 6d 6e 2e 20  .** one column. 
1def7 45 78 65 63 75 74 65 20 74 68 69 73 20 61 73 20  Execute this as 
1def8 53 51 4c 20 6f 6e 20 74 68 65 20 73 61 6d 65 20  SQL on the same 
1def9 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61  database..*/.sta
1defa 74 69 63 20 69 6e 74 20 65 78 65 63 45 78 65 63  tic int execExec
1defb 53 71 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  Sql(sqlite3 *db,
1defc 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71   const char *zSq
1defd 6c 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  l){.  sqlite3_st
1defe 6d 74 20 2a 70 53 74 6d 74 3b 0a 20 20 69 6e 74  mt *pStmt;.  int
1deff 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c   rc;..  rc = sql
1df00 69 74 65 33 5f 70 72 65 70 61 72 65 28 64 62 2c  ite3_prepare(db,
1df01 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d   zSql, -1, &pStm
1df02 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 21  t, 0);.  if( rc!
1df03 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
1df04 75 72 6e 20 72 63 3b 0a 0a 20 20 77 68 69 6c 65  urn rc;..  while
1df05 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d 73 71  ( SQLITE_ROW==sq
1df06 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74  lite3_step(pStmt
1df07 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 65 78  ) ){.    rc = ex
1df08 65 63 53 71 6c 28 64 62 2c 20 28 63 68 61 72 2a  ecSql(db, (char*
1df09 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  )sqlite3_column_
1df0a 74 65 78 74 28 70 53 74 6d 74 2c 20 30 29 29 3b  text(pStmt, 0));
1df0b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
1df0c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1df0d 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
1df0e 28 70 53 74 6d 74 29 3b 0a 20 20 20 20 20 20 72  (pStmt);.      r
1df0f 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
1df10 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 73 71    }..  return sq
1df11 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70  lite3_finalize(p
1df12 53 74 6d 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Stmt);.}../*.** 
1df13 54 68 65 20 6e 6f 6e 2d 73 74 61 6e 64 61 72 64  The non-standard
1df14 20 56 41 43 55 55 4d 20 63 6f 6d 6d 61 6e 64 20   VACUUM command 
1df15 69 73 20 75 73 65 64 20 74 6f 20 63 6c 65 61 6e  is used to clean
1df16 20 75 70 20 74 68 65 20 64 61 74 61 62 61 73 65   up the database
1df17 2c 0a 2a 2a 20 63 6f 6c 6c 61 70 73 65 20 66 72  ,.** collapse fr
1df18 65 65 20 73 70 61 63 65 2c 20 65 74 63 2e 20 20  ee space, etc.  
1df19 49 74 20 69 73 20 6d 6f 64 65 6c 6c 65 64 20 61  It is modelled a
1df1a 66 74 65 72 20 74 68 65 20 56 41 43 55 55 4d 20  fter the VACUUM 
1df1b 63 6f 6d 6d 61 6e 64 0a 2a 2a 20 69 6e 20 50 6f  command.** in Po
1df1c 73 74 67 72 65 53 51 4c 2e 0a 2a 2a 0a 2a 2a 20  stgreSQL..**.** 
1df1d 49 6e 20 76 65 72 73 69 6f 6e 20 31 2e 30 2e 78  In version 1.0.x
1df1e 20 6f 66 20 53 51 4c 69 74 65 2c 20 74 68 65 20   of SQLite, the 
1df1f 56 41 43 55 55 4d 20 63 6f 6d 6d 61 6e 64 20 77  VACUUM command w
1df20 6f 75 6c 64 20 63 61 6c 6c 0a 2a 2a 20 67 64 62  ould call.** gdb
1df21 6d 5f 72 65 6f 72 67 61 6e 69 7a 65 28 29 20 6f  m_reorganize() o
1df22 6e 20 61 6c 6c 20 74 68 65 20 64 61 74 61 62 61  n all the databa
1df23 73 65 20 74 61 62 6c 65 73 2e 20 20 42 75 74 20  se tables.  But 
1df24 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 77 69 74  beginning.** wit
1df25 68 20 32 2e 30 2e 30 2c 20 53 51 4c 69 74 65 20  h 2.0.0, SQLite 
1df26 6e 6f 20 6c 6f 6e 67 65 72 20 75 73 65 73 20 47  no longer uses G
1df27 44 42 4d 20 73 6f 20 74 68 69 73 20 63 6f 6d 6d  DBM so this comm
1df28 61 6e 64 20 68 61 73 0a 2a 2a 20 62 65 63 6f 6d  and has.** becom
1df29 65 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51  e a no-op..*/.SQ
1df2a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1df2b 64 20 73 71 6c 69 74 65 33 56 61 63 75 75 6d 28  d sqlite3Vacuum(
1df2c 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a  Parse *pParse){.
1df2d 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69    Vdbe *v = sqli
1df2e 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
1df2f 65 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20  e);.  if( v ){. 
1df30 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1df31 64 4f 70 28 76 2c 20 4f 50 5f 56 61 63 75 75 6d  dOp(v, OP_Vacuum
1df32 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72  , 0, 0);.  }.  r
1df33 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  eturn;.}../*.** 
1df34 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6d 70  This routine imp
1df35 6c 65 6d 65 6e 74 73 20 74 68 65 20 4f 50 5f 56  lements the OP_V
1df36 61 63 75 75 6d 20 6f 70 63 6f 64 65 20 6f 66 20  acuum opcode of 
1df37 74 68 65 20 56 44 42 45 2e 0a 2a 2f 0a 53 51 4c  the VDBE..*/.SQL
1df38 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1df39 73 71 6c 69 74 65 33 52 75 6e 56 61 63 75 75 6d  sqlite3RunVacuum
1df3a 28 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67  (char **pzErrMsg
1df3b 2c 20 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  , sqlite3 *db){.
1df3c 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
1df3d 45 5f 4f 4b 3b 20 20 20 20 20 2f 2a 20 52 65 74  E_OK;     /* Ret
1df3e 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 65  urn code from se
1df3f 72 76 69 63 65 20 72 6f 75 74 69 6e 65 73 20 2a  rvice routines *
1df40 2f 0a 20 20 42 74 72 65 65 20 2a 70 4d 61 69 6e  /.  Btree *pMain
1df41 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
1df42 68 65 20 64 61 74 61 62 61 73 65 20 62 65 69 6e  he database bein
1df43 67 20 76 61 63 75 75 6d 65 64 20 2a 2f 0a 20 20  g vacuumed */.  
1df44 42 74 72 65 65 20 2a 70 54 65 6d 70 3b 20 20 20  Btree *pTemp;   
1df45 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
1df46 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73  emporary databas
1df47 65 20 77 65 20 76 61 63 75 75 6d 20 69 6e 74 6f  e we vacuum into
1df48 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c   */.  char *zSql
1df49 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a   = 0;         /*
1df4a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20   SQL statements 
1df4b 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65 64 5f 66  */.  int saved_f
1df4c 6c 61 67 73 3b 20 20 20 20 20 20 20 20 2f 2a 20  lags;        /* 
1df4d 53 61 76 65 64 20 76 61 6c 75 65 20 6f 66 20 74  Saved value of t
1df4e 68 65 20 64 62 2d 3e 66 6c 61 67 73 20 2a 2f 0a  he db->flags */.
1df4f 20 20 44 62 20 2a 70 44 62 20 3d 20 30 3b 20 20    Db *pDb = 0;  
1df50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
1df51 61 62 61 73 65 20 74 6f 20 64 65 74 61 63 68 20  abase to detach 
1df52 61 74 20 65 6e 64 20 6f 66 20 76 61 63 75 75 6d  at end of vacuum
1df53 20 2a 2f 0a 0a 20 20 2f 2a 20 53 61 76 65 20 74   */..  /* Save t
1df54 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65  he current value
1df55 20 6f 66 20 74 68 65 20 77 72 69 74 65 2d 73 63   of the write-sc
1df56 68 65 6d 61 20 66 6c 61 67 20 62 65 66 6f 72 65  hema flag before
1df57 20 73 65 74 74 69 6e 67 20 69 74 2e 20 2a 2f 0a   setting it. */.
1df58 20 20 73 61 76 65 64 5f 66 6c 61 67 73 20 3d 20    saved_flags = 
1df59 64 62 2d 3e 66 6c 61 67 73 3b 0a 20 20 64 62 2d  db->flags;.  db-
1df5a 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45  >flags |= SQLITE
1df5b 5f 57 72 69 74 65 53 63 68 65 6d 61 20 7c 20 53  _WriteSchema | S
1df5c 51 4c 49 54 45 5f 49 67 6e 6f 72 65 43 68 65 63  QLITE_IgnoreChec
1df5d 6b 73 3b 0a 0a 20 20 69 66 28 20 21 64 62 2d 3e  ks;..  if( !db->
1df5e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20  autoCommit ){.  
1df5f 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
1df60 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20 22 63 61  ng(pzErrMsg, "ca
1df61 6e 6e 6f 74 20 56 41 43 55 55 4d 20 66 72 6f 6d  nnot VACUUM from
1df62 20 77 69 74 68 69 6e 20 61 20 74 72 61 6e 73 61   within a transa
1df63 63 74 69 6f 6e 22 2c 20 0a 20 20 20 20 20 20 20  ction", .       
1df64 28 63 68 61 72 2a 29 30 29 3b 0a 20 20 20 20 72  (char*)0);.    r
1df65 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
1df66 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6f  ;.    goto end_o
1df67 66 5f 76 61 63 75 75 6d 3b 0a 20 20 7d 0a 20 20  f_vacuum;.  }.  
1df68 70 4d 61 69 6e 20 3d 20 64 62 2d 3e 61 44 62 5b  pMain = db->aDb[
1df69 30 5d 2e 70 42 74 3b 0a 0a 20 20 2f 2a 20 41 74  0].pBt;..  /* At
1df6a 74 61 63 68 20 74 68 65 20 74 65 6d 70 6f 72 61  tach the tempora
1df6b 72 79 20 64 61 74 61 62 61 73 65 20 61 73 20 27  ry database as '
1df6c 76 61 63 75 75 6d 5f 64 62 27 2e 20 54 68 65 20  vacuum_db'. The 
1df6d 73 79 6e 63 68 72 6f 6e 6f 75 73 20 70 72 61 67  synchronous prag
1df6e 6d 61 0a 20 20 2a 2a 20 63 61 6e 20 62 65 20 73  ma.  ** can be s
1df6f 65 74 20 74 6f 20 27 6f 66 66 27 20 66 6f 72 20  et to 'off' for 
1df70 74 68 69 73 20 66 69 6c 65 2c 20 61 73 20 69 74  this file, as it
1df71 20 69 73 20 6e 6f 74 20 72 65 63 6f 76 65 72 65   is not recovere
1df72 64 20 69 66 20 61 20 63 72 61 73 68 0a 20 20 2a  d if a crash.  *
1df73 2a 20 6f 63 63 75 72 73 20 61 6e 79 77 61 79 2e  * occurs anyway.
1df74 20 54 68 65 20 69 6e 74 65 67 72 69 74 79 20 6f   The integrity o
1df75 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  f the database i
1df76 73 20 6d 61 69 6e 74 61 69 6e 65 64 20 62 79 20  s maintained by 
1df77 61 0a 20 20 2a 2a 20 28 70 6f 73 73 69 62 6c 79  a.  ** (possibly
1df78 20 73 79 6e 63 68 72 6f 6e 6f 75 73 29 20 74 72   synchronous) tr
1df79 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 65 64  ansaction opened
1df7a 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74   on the main dat
1df7b 61 62 61 73 65 20 62 65 66 6f 72 65 0a 20 20 2a  abase before.  *
1df7c 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  * sqlite3BtreeCo
1df7d 70 79 46 69 6c 65 28 29 20 69 73 20 63 61 6c 6c  pyFile() is call
1df7e 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e  ed..  **.  ** An
1df7f 20 6f 70 74 69 6d 69 73 61 74 69 6f 6e 20 77 6f   optimisation wo
1df80 75 6c 64 20 62 65 20 74 6f 20 75 73 65 20 61 20  uld be to use a 
1df81 6e 6f 6e 2d 6a 6f 75 72 6e 61 6c 65 64 20 70 61  non-journaled pa
1df82 67 65 72 2e 0a 20 20 2a 2f 0a 20 20 7a 53 71 6c  ger..  */.  zSql
1df83 20 3d 20 22 41 54 54 41 43 48 20 27 27 20 41 53   = "ATTACH '' AS
1df84 20 76 61 63 75 75 6d 5f 64 62 3b 22 3b 0a 20 20   vacuum_db;";.  
1df85 72 63 20 3d 20 65 78 65 63 53 71 6c 28 64 62 2c  rc = execSql(db,
1df86 20 7a 53 71 6c 29 3b 0a 20 20 69 66 28 20 72 63   zSql);.  if( rc
1df87 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
1df88 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d  to end_of_vacuum
1df89 3b 0a 20 20 70 44 62 20 3d 20 26 64 62 2d 3e 61  ;.  pDb = &db->a
1df8a 44 62 5b 64 62 2d 3e 6e 44 62 2d 31 5d 3b 0a 20  Db[db->nDb-1];. 
1df8b 20 61 73 73 65 72 74 28 20 73 74 72 63 6d 70 28   assert( strcmp(
1df8c 64 62 2d 3e 61 44 62 5b 64 62 2d 3e 6e 44 62 2d  db->aDb[db->nDb-
1df8d 31 5d 2e 7a 4e 61 6d 65 2c 22 76 61 63 75 75 6d  1].zName,"vacuum
1df8e 5f 64 62 22 29 3d 3d 30 20 29 3b 0a 20 20 70 54  _db")==0 );.  pT
1df8f 65 6d 70 20 3d 20 64 62 2d 3e 61 44 62 5b 64 62  emp = db->aDb[db
1df90 2d 3e 6e 44 62 2d 31 5d 2e 70 42 74 3b 0a 20 20  ->nDb-1].pBt;.  
1df91 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 50  sqlite3BtreeSetP
1df92 61 67 65 53 69 7a 65 28 70 54 65 6d 70 2c 20 73  ageSize(pTemp, s
1df93 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61  qlite3BtreeGetPa
1df94 67 65 53 69 7a 65 28 70 4d 61 69 6e 29 2c 0a 20  geSize(pMain),. 
1df95 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
1df96 47 65 74 52 65 73 65 72 76 65 28 70 4d 61 69 6e  GetReserve(pMain
1df97 29 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  ));.  if( sqlite
1df98 33 4d 61 6c 6c 6f 63 46 61 69 6c 65 64 28 29 20  3MallocFailed() 
1df99 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
1df9a 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f  TE_NOMEM;.    go
1df9b 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d  to end_of_vacuum
1df9c 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
1df9d 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50  sqlite3BtreeGetP
1df9e 61 67 65 53 69 7a 65 28 70 54 65 6d 70 29 3d 3d  ageSize(pTemp)==
1df9f 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50  sqlite3BtreeGetP
1dfa0 61 67 65 53 69 7a 65 28 70 4d 61 69 6e 29 20 29  ageSize(pMain) )
1dfa1 3b 0a 20 20 72 63 20 3d 20 65 78 65 63 53 71 6c  ;.  rc = execSql
1dfa2 28 64 62 2c 20 22 50 52 41 47 4d 41 20 76 61 63  (db, "PRAGMA vac
1dfa3 75 75 6d 5f 64 62 2e 73 79 6e 63 68 72 6f 6e 6f  uum_db.synchrono
1dfa4 75 73 3d 4f 46 46 22 29 3b 0a 20 20 69 66 28 20  us=OFF");.  if( 
1dfa5 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1dfa6 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6f 66  .    goto end_of
1dfa7 5f 76 61 63 75 75 6d 3b 0a 20 20 7d 0a 0a 23 69  _vacuum;.  }..#i
1dfa8 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1dfa9 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 73  T_AUTOVACUUM.  s
1dfaa 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 41 75  qlite3BtreeSetAu
1dfab 74 6f 56 61 63 75 75 6d 28 70 54 65 6d 70 2c 20  toVacuum(pTemp, 
1dfac 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 41  sqlite3BtreeGetA
1dfad 75 74 6f 56 61 63 75 75 6d 28 70 4d 61 69 6e 29  utoVacuum(pMain)
1dfae 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20  );.#endif..  /* 
1dfaf 42 65 67 69 6e 20 61 20 74 72 61 6e 73 61 63 74  Begin a transact
1dfb0 69 6f 6e 20 2a 2f 0a 20 20 72 63 20 3d 20 65 78  ion */.  rc = ex
1dfb1 65 63 53 71 6c 28 64 62 2c 20 22 42 45 47 49 4e  ecSql(db, "BEGIN
1dfb2 20 45 58 43 4c 55 53 49 56 45 3b 22 29 3b 0a 20   EXCLUSIVE;");. 
1dfb3 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1dfb4 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66  OK ) goto end_of
1dfb5 5f 76 61 63 75 75 6d 3b 0a 0a 20 20 2f 2a 20 51  _vacuum;..  /* Q
1dfb6 75 65 72 79 20 74 68 65 20 73 63 68 65 6d 61 20  uery the schema 
1dfb7 6f 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  of the main data
1dfb8 62 61 73 65 2e 20 43 72 65 61 74 65 20 61 20 6d  base. Create a m
1dfb9 69 72 72 6f 72 20 73 63 68 65 6d 61 0a 20 20 2a  irror schema.  *
1dfba 2a 20 69 6e 20 74 68 65 20 74 65 6d 70 6f 72 61  * in the tempora
1dfbb 72 79 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a  ry database..  *
1dfbc 2f 0a 20 20 72 63 20 3d 20 65 78 65 63 45 78 65  /.  rc = execExe
1dfbd 63 53 71 6c 28 64 62 2c 20 0a 20 20 20 20 20 20  cSql(db, .      
1dfbe 22 53 45 4c 45 43 54 20 27 43 52 45 41 54 45 20  "SELECT 'CREATE 
1dfbf 54 41 42 4c 45 20 76 61 63 75 75 6d 5f 64 62 2e  TABLE vacuum_db.
1dfc0 27 20 7c 7c 20 73 75 62 73 74 72 28 73 71 6c 2c  ' || substr(sql,
1dfc1 31 34 2c 31 30 30 30 30 30 30 30 30 29 20 22 0a  14,100000000) ".
1dfc2 20 20 20 20 20 20 22 20 20 46 52 4f 4d 20 73 71        "  FROM sq
1dfc3 6c 69 74 65 5f 6d 61 73 74 65 72 20 57 48 45 52  lite_master WHER
1dfc4 45 20 74 79 70 65 3d 27 74 61 62 6c 65 27 20 41  E type='table' A
1dfc5 4e 44 20 6e 61 6d 65 21 3d 27 73 71 6c 69 74 65  ND name!='sqlite
1dfc6 5f 73 65 71 75 65 6e 63 65 27 22 0a 20 20 20 20  _sequence'".    
1dfc7 20 20 22 20 20 20 41 4e 44 20 72 6f 6f 74 70 61    "   AND rootpa
1dfc8 67 65 3e 30 22 0a 20 20 29 3b 0a 20 20 69 66 28  ge>0".  );.  if(
1dfc9 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1dfca 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63   goto end_of_vac
1dfcb 75 75 6d 3b 0a 20 20 72 63 20 3d 20 65 78 65 63  uum;.  rc = exec
1dfcc 45 78 65 63 53 71 6c 28 64 62 2c 20 0a 20 20 20  ExecSql(db, .   
1dfcd 20 20 20 22 53 45 4c 45 43 54 20 27 43 52 45 41     "SELECT 'CREA
1dfce 54 45 20 49 4e 44 45 58 20 76 61 63 75 75 6d 5f  TE INDEX vacuum_
1dfcf 64 62 2e 27 20 7c 7c 20 73 75 62 73 74 72 28 73  db.' || substr(s
1dfd0 71 6c 2c 31 34 2c 31 30 30 30 30 30 30 30 30 29  ql,14,100000000)
1dfd1 22 0a 20 20 20 20 20 20 22 20 20 46 52 4f 4d 20  ".      "  FROM 
1dfd2 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 57 48  sqlite_master WH
1dfd3 45 52 45 20 73 71 6c 20 4c 49 4b 45 20 27 43 52  ERE sql LIKE 'CR
1dfd4 45 41 54 45 20 49 4e 44 45 58 20 25 27 20 22 29  EATE INDEX %' ")
1dfd5 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
1dfd6 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64  TE_OK ) goto end
1dfd7 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20 72 63  _of_vacuum;.  rc
1dfd8 20 3d 20 65 78 65 63 45 78 65 63 53 71 6c 28 64   = execExecSql(d
1dfd9 62 2c 20 0a 20 20 20 20 20 20 22 53 45 4c 45 43  b, .      "SELEC
1dfda 54 20 27 43 52 45 41 54 45 20 55 4e 49 51 55 45  T 'CREATE UNIQUE
1dfdb 20 49 4e 44 45 58 20 76 61 63 75 75 6d 5f 64 62   INDEX vacuum_db
1dfdc 2e 27 20 7c 7c 20 73 75 62 73 74 72 28 73 71 6c  .' || substr(sql
1dfdd 2c 32 31 2c 31 30 30 30 30 30 30 30 30 29 20 22  ,21,100000000) "
1dfde 0a 20 20 20 20 20 20 22 20 20 46 52 4f 4d 20 73  .      "  FROM s
1dfdf 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 57 48 45  qlite_master WHE
1dfe0 52 45 20 73 71 6c 20 4c 49 4b 45 20 27 43 52 45  RE sql LIKE 'CRE
1dfe1 41 54 45 20 55 4e 49 51 55 45 20 49 4e 44 45 58  ATE UNIQUE INDEX
1dfe2 20 25 27 22 29 3b 0a 20 20 69 66 28 20 72 63 21   %'");.  if( rc!
1dfe3 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
1dfe4 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b  o end_of_vacuum;
1dfe5 0a 0a 20 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f  ..  /* Loop thro
1dfe6 75 67 68 20 74 68 65 20 74 61 62 6c 65 73 20 69  ugh the tables i
1dfe7 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  n the main datab
1dfe8 61 73 65 2e 20 46 6f 72 20 65 61 63 68 2c 20 64  ase. For each, d
1dfe9 6f 0a 20 20 2a 2a 20 61 6e 20 22 49 4e 53 45 52  o.  ** an "INSER
1dfea 54 20 49 4e 54 4f 20 76 61 63 75 75 6d 5f 64 62  T INTO vacuum_db
1dfeb 2e 78 78 78 20 53 45 4c 45 43 54 20 2a 20 46 52  .xxx SELECT * FR
1dfec 4f 4d 20 78 78 78 3b 22 20 74 6f 20 63 6f 70 79  OM xxx;" to copy
1dfed 0a 20 20 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e  .  ** the conten
1dfee 74 73 20 74 6f 20 74 68 65 20 74 65 6d 70 6f 72  ts to the tempor
1dfef 61 72 79 20 64 61 74 61 62 61 73 65 2e 0a 20 20  ary database..  
1dff0 2a 2f 0a 20 20 72 63 20 3d 20 65 78 65 63 45 78  */.  rc = execEx
1dff1 65 63 53 71 6c 28 64 62 2c 20 0a 20 20 20 20 20  ecSql(db, .     
1dff2 20 22 53 45 4c 45 43 54 20 27 49 4e 53 45 52 54   "SELECT 'INSERT
1dff3 20 49 4e 54 4f 20 76 61 63 75 75 6d 5f 64 62 2e   INTO vacuum_db.
1dff4 27 20 7c 7c 20 71 75 6f 74 65 28 6e 61 6d 65 29  ' || quote(name)
1dff5 20 22 0a 20 20 20 20 20 20 22 7c 7c 20 27 20 53   ".      "|| ' S
1dff6 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 27 20 7c  ELECT * FROM ' |
1dff7 7c 20 71 75 6f 74 65 28 6e 61 6d 65 29 20 7c 7c  | quote(name) ||
1dff8 20 27 3b 27 22 0a 20 20 20 20 20 20 22 46 52 4f   ';'".      "FRO
1dff9 4d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  M sqlite_master 
1dffa 22 0a 20 20 20 20 20 20 22 57 48 45 52 45 20 74  ".      "WHERE t
1dffb 79 70 65 20 3d 20 27 74 61 62 6c 65 27 20 41 4e  ype = 'table' AN
1dffc 44 20 6e 61 6d 65 21 3d 27 73 71 6c 69 74 65 5f  D name!='sqlite_
1dffd 73 65 71 75 65 6e 63 65 27 20 22 0a 20 20 20 20  sequence' ".    
1dffe 20 20 22 20 20 41 4e 44 20 72 6f 6f 74 70 61 67    "  AND rootpag
1dfff 65 3e 30 22 0a 0a 20 20 29 3b 0a 20 20 69 66 28  e>0"..  );.  if(
1e000 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1e001 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63   goto end_of_vac
1e002 75 75 6d 3b 0a 0a 20 20 2f 2a 20 43 6f 70 79 20  uum;..  /* Copy 
1e003 6f 76 65 72 20 74 68 65 20 73 65 71 75 65 6e 63  over the sequenc
1e004 65 20 74 61 62 6c 65 0a 20 20 2a 2f 0a 20 20 72  e table.  */.  r
1e005 63 20 3d 20 65 78 65 63 45 78 65 63 53 71 6c 28  c = execExecSql(
1e006 64 62 2c 20 0a 20 20 20 20 20 20 22 53 45 4c 45  db, .      "SELE
1e007 43 54 20 27 44 45 4c 45 54 45 20 46 52 4f 4d 20  CT 'DELETE FROM 
1e008 76 61 63 75 75 6d 5f 64 62 2e 27 20 7c 7c 20 71  vacuum_db.' || q
1e009 75 6f 74 65 28 6e 61 6d 65 29 20 7c 7c 20 27 3b  uote(name) || ';
1e00a 27 20 22 0a 20 20 20 20 20 20 22 46 52 4f 4d 20  ' ".      "FROM 
1e00b 76 61 63 75 75 6d 5f 64 62 2e 73 71 6c 69 74 65  vacuum_db.sqlite
1e00c 5f 6d 61 73 74 65 72 20 57 48 45 52 45 20 6e 61  _master WHERE na
1e00d 6d 65 3d 27 73 71 6c 69 74 65 5f 73 65 71 75 65  me='sqlite_seque
1e00e 6e 63 65 27 20 22 0a 20 20 29 3b 0a 20 20 69 66  nce' ".  );.  if
1e00f 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1e010 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61  ) goto end_of_va
1e011 63 75 75 6d 3b 0a 20 20 72 63 20 3d 20 65 78 65  cuum;.  rc = exe
1e012 63 45 78 65 63 53 71 6c 28 64 62 2c 20 0a 20 20  cExecSql(db, .  
1e013 20 20 20 20 22 53 45 4c 45 43 54 20 27 49 4e 53      "SELECT 'INS
1e014 45 52 54 20 49 4e 54 4f 20 76 61 63 75 75 6d 5f  ERT INTO vacuum_
1e015 64 62 2e 27 20 7c 7c 20 71 75 6f 74 65 28 6e 61  db.' || quote(na
1e016 6d 65 29 20 22 0a 20 20 20 20 20 20 22 7c 7c 20  me) ".      "|| 
1e017 27 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20  ' SELECT * FROM 
1e018 27 20 7c 7c 20 71 75 6f 74 65 28 6e 61 6d 65 29  ' || quote(name)
1e019 20 7c 7c 20 27 3b 27 20 22 0a 20 20 20 20 20 20   || ';' ".      
1e01a 22 46 52 4f 4d 20 76 61 63 75 75 6d 5f 64 62 2e  "FROM vacuum_db.
1e01b 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 57 48  sqlite_master WH
1e01c 45 52 45 20 6e 61 6d 65 3d 3d 27 73 71 6c 69 74  ERE name=='sqlit
1e01d 65 5f 73 65 71 75 65 6e 63 65 27 3b 22 0a 20 20  e_sequence';".  
1e01e 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
1e01f 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e  ITE_OK ) goto en
1e020 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 0a 0a 20  d_of_vacuum;... 
1e021 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 74 72 69   /* Copy the tri
1e022 67 67 65 72 73 2c 20 76 69 65 77 73 2c 20 61 6e  ggers, views, an
1e023 64 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73  d virtual tables
1e024 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 64   from the main d
1e025 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 6f 76 65  atabase.  ** ove
1e026 72 20 74 6f 20 74 68 65 20 74 65 6d 70 6f 72 61  r to the tempora
1e027 72 79 20 64 61 74 61 62 61 73 65 2e 20 20 4e 6f  ry database.  No
1e028 6e 65 20 6f 66 20 74 68 65 73 65 20 6f 62 6a 65  ne of these obje
1e029 63 74 73 20 68 61 73 20 61 6e 79 0a 20 20 2a 2a  cts has any.  **
1e02a 20 61 73 73 6f 63 69 61 74 65 64 20 73 74 6f 72   associated stor
1e02b 61 67 65 2c 20 73 6f 20 61 6c 6c 20 77 65 20 68  age, so all we h
1e02c 61 76 65 20 74 6f 20 64 6f 20 69 73 20 63 6f 70  ave to do is cop
1e02d 79 20 74 68 65 69 72 20 65 6e 74 72 69 65 73 0a  y their entries.
1e02e 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 53 51    ** from the SQ
1e02f 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c  LITE_MASTER tabl
1e030 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 65  e..  */.  rc = e
1e031 78 65 63 53 71 6c 28 64 62 2c 0a 20 20 20 20 20  xecSql(db,.     
1e032 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 76 61   "INSERT INTO va
1e033 63 75 75 6d 5f 64 62 2e 73 71 6c 69 74 65 5f 6d  cuum_db.sqlite_m
1e034 61 73 74 65 72 20 22 0a 20 20 20 20 20 20 22 20  aster ".      " 
1e035 20 53 45 4c 45 43 54 20 74 79 70 65 2c 20 6e 61   SELECT type, na
1e036 6d 65 2c 20 74 62 6c 5f 6e 61 6d 65 2c 20 72 6f  me, tbl_name, ro
1e037 6f 74 70 61 67 65 2c 20 73 71 6c 22 0a 20 20 20  otpage, sql".   
1e038 20 20 20 22 20 20 20 20 46 52 4f 4d 20 73 71 6c     "    FROM sql
1e039 69 74 65 5f 6d 61 73 74 65 72 22 0a 20 20 20 20  ite_master".    
1e03a 20 20 22 20 20 20 57 48 45 52 45 20 74 79 70 65    "   WHERE type
1e03b 3d 27 76 69 65 77 27 20 4f 52 20 74 79 70 65 3d  ='view' OR type=
1e03c 27 74 72 69 67 67 65 72 27 22 0a 20 20 20 20 20  'trigger'".     
1e03d 20 22 20 20 20 20 20 20 4f 52 20 28 74 79 70 65   "      OR (type
1e03e 3d 27 74 61 62 6c 65 27 20 41 4e 44 20 72 6f 6f  ='table' AND roo
1e03f 74 70 61 67 65 3d 30 29 22 0a 20 20 29 3b 0a 20  tpage=0)".  );. 
1e040 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65   if( rc ) goto e
1e041 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 0a 20  nd_of_vacuum;.. 
1e042 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e   /* At this poin
1e043 74 2c 20 75 6e 6c 65 73 73 20 74 68 65 20 6d 61  t, unless the ma
1e044 69 6e 20 64 62 20 77 61 73 20 63 6f 6d 70 6c 65  in db was comple
1e045 74 65 6c 79 20 65 6d 70 74 79 2c 20 74 68 65 72  tely empty, ther
1e046 65 20 69 73 20 6e 6f 77 20 61 0a 20 20 2a 2a 20  e is now a.  ** 
1e047 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e  transaction open
1e048 20 6f 6e 20 74 68 65 20 76 61 63 75 75 6d 20 64   on the vacuum d
1e049 61 74 61 62 61 73 65 2c 20 62 75 74 20 6e 6f 74  atabase, but not
1e04a 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74   on the main dat
1e04b 61 62 61 73 65 2e 0a 20 20 2a 2a 20 4f 70 65 6e  abase..  ** Open
1e04c 20 61 20 62 74 72 65 65 20 6c 65 76 65 6c 20 74   a btree level t
1e04d 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68  ransaction on th
1e04e 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2e  e main database.
1e04f 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 61 0a 20   This allows a. 
1e050 20 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69   ** call to sqli
1e051 74 65 33 42 74 72 65 65 43 6f 70 79 46 69 6c 65  te3BtreeCopyFile
1e052 28 29 2e 20 54 68 65 20 6d 61 69 6e 20 64 61 74  (). The main dat
1e053 61 62 61 73 65 20 62 74 72 65 65 20 6c 65 76 65  abase btree leve
1e054 6c 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69  l.  ** transacti
1e055 6f 6e 20 69 73 20 74 68 65 6e 20 63 6f 6d 6d 69  on is then commi
1e056 74 74 65 64 2c 20 73 6f 20 74 68 65 20 53 51 4c  tted, so the SQL
1e057 20 6c 65 76 65 6c 20 6e 65 76 65 72 20 6b 6e 6f   level never kno
1e058 77 73 20 69 74 20 77 61 73 0a 20 20 2a 2a 20 6f  ws it was.  ** o
1e059 70 65 6e 65 64 20 66 6f 72 20 77 72 69 74 69 6e  pened for writin
1e05a 67 2e 20 54 68 69 73 20 77 61 79 2c 20 74 68 65  g. This way, the
1e05b 20 53 51 4c 20 74 72 61 6e 73 61 63 74 69 6f 6e   SQL transaction
1e05c 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20   used to create 
1e05d 74 68 65 0a 20 20 2a 2a 20 74 65 6d 70 6f 72 61  the.  ** tempora
1e05e 72 79 20 64 61 74 61 62 61 73 65 20 6e 65 76 65  ry database neve
1e05f 72 20 6e 65 65 64 73 20 74 6f 20 62 65 20 63 6f  r needs to be co
1e060 6d 6d 69 74 74 65 64 2e 0a 20 20 2a 2f 0a 20 20  mmitted..  */.  
1e061 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1e062 4b 20 29 7b 0a 20 20 20 20 75 33 32 20 6d 65 74  K ){.    u32 met
1e063 61 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 0a 20  a;.    int i;.. 
1e064 20 20 20 2f 2a 20 54 68 69 73 20 61 72 72 61 79     /* This array
1e065 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 69 63   determines whic
1e066 68 20 6d 65 74 61 20 6d 65 74 61 20 76 61 6c 75  h meta meta valu
1e067 65 73 20 61 72 65 20 70 72 65 73 65 72 76 65 64  es are preserved
1e068 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 76   in the.    ** v
1e069 61 63 75 75 6d 2e 20 20 45 76 65 6e 20 65 6e 74  acuum.  Even ent
1e06a 72 69 65 73 20 61 72 65 20 74 68 65 20 6d 65 74  ries are the met
1e06b 61 20 76 61 6c 75 65 20 6e 75 6d 62 65 72 20 61  a value number a
1e06c 6e 64 20 6f 64 64 20 65 6e 74 72 69 65 73 0a 20  nd odd entries. 
1e06d 20 20 20 2a 2a 20 61 72 65 20 61 6e 20 69 6e 63     ** are an inc
1e06e 72 65 6d 65 6e 74 20 74 6f 20 61 70 70 6c 79 20  rement to apply 
1e06f 74 6f 20 74 68 65 20 6d 65 74 61 20 76 61 6c 75  to the meta valu
1e070 65 20 61 66 74 65 72 20 74 68 65 20 76 61 63 75  e after the vacu
1e071 75 6d 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 69  um..    ** The i
1e072 6e 63 72 65 6d 65 6e 74 20 69 73 20 75 73 65 64  ncrement is used
1e073 20 74 6f 20 69 6e 63 72 65 61 73 65 20 74 68 65   to increase the
1e074 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 73   schema cookie s
1e075 6f 20 74 68 61 74 20 6f 74 68 65 72 0a 20 20 20  o that other.   
1e076 20 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20   ** connections 
1e077 74 6f 20 74 68 65 20 73 61 6d 65 20 64 61 74 61  to the same data
1e078 62 61 73 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 74  base will know t
1e079 6f 20 72 65 72 65 61 64 20 74 68 65 20 73 63 68  o reread the sch
1e07a 65 6d 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ema..    */.    
1e07b 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73  static const uns
1e07c 69 67 6e 65 64 20 63 68 61 72 20 61 43 6f 70 79  igned char aCopy
1e07d 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 31 2c  [] = {.       1,
1e07e 20 31 2c 20 20 20 20 2f 2a 20 41 64 64 20 6f 6e   1,    /* Add on
1e07f 65 20 74 6f 20 74 68 65 20 6f 6c 64 20 73 63 68  e to the old sch
1e080 65 6d 61 20 63 6f 6f 6b 69 65 20 2a 2f 0a 20 20  ema cookie */.  
1e081 20 20 20 20 20 33 2c 20 30 2c 20 20 20 20 2f 2a       3, 0,    /*
1e082 20 50 72 65 73 65 72 76 65 20 74 68 65 20 64 65   Preserve the de
1e083 66 61 75 6c 74 20 70 61 67 65 20 63 61 63 68 65  fault page cache
1e084 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 20 20 20   size */.       
1e085 35 2c 20 30 2c 20 20 20 20 2f 2a 20 50 72 65 73  5, 0,    /* Pres
1e086 65 72 76 65 20 74 68 65 20 64 65 66 61 75 6c 74  erve the default
1e087 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 2a   text encoding *
1e088 2f 0a 20 20 20 20 20 20 20 36 2c 20 30 2c 20 20  /.       6, 0,  
1e089 20 20 2f 2a 20 50 72 65 73 65 72 76 65 20 74 68    /* Preserve th
1e08a 65 20 75 73 65 72 20 76 65 72 73 69 6f 6e 20 2a  e user version *
1e08b 2f 0a 20 20 20 20 7d 3b 0a 0a 20 20 20 20 61 73  /.    };..    as
1e08c 73 65 72 74 28 20 31 3d 3d 73 71 6c 69 74 65 33  sert( 1==sqlite3
1e08d 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70  BtreeIsInTrans(p
1e08e 54 65 6d 70 29 20 29 3b 0a 20 20 20 20 61 73 73  Temp) );.    ass
1e08f 65 72 74 28 20 31 3d 3d 73 71 6c 69 74 65 33 42  ert( 1==sqlite3B
1e090 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70 4d  treeIsInTrans(pM
1e091 61 69 6e 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20  ain) );..    /* 
1e092 43 6f 70 79 20 42 74 72 65 65 20 6d 65 74 61 20  Copy Btree meta 
1e093 76 61 6c 75 65 73 20 2a 2f 0a 20 20 20 20 66 6f  values */.    fo
1e094 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28  r(i=0; i<sizeof(
1e095 61 43 6f 70 79 29 2f 73 69 7a 65 6f 66 28 61 43  aCopy)/sizeof(aC
1e096 6f 70 79 5b 30 5d 29 3b 20 69 2b 3d 32 29 7b 0a  opy[0]); i+=2){.
1e097 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1e098 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 70  e3BtreeGetMeta(p
1e099 4d 61 69 6e 2c 20 61 43 6f 70 79 5b 69 5d 2c 20  Main, aCopy[i], 
1e09a 26 6d 65 74 61 29 3b 0a 20 20 20 20 20 20 69 66  &meta);.      if
1e09b 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1e09c 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61  ) goto end_of_va
1e09d 63 75 75 6d 3b 0a 20 20 20 20 20 20 72 63 20 3d  cuum;.      rc =
1e09e 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64   sqlite3BtreeUpd
1e09f 61 74 65 4d 65 74 61 28 70 54 65 6d 70 2c 20 61  ateMeta(pTemp, a
1e0a0 43 6f 70 79 5b 69 5d 2c 20 6d 65 74 61 2b 61 43  Copy[i], meta+aC
1e0a1 6f 70 79 5b 69 2b 31 5d 29 3b 0a 20 20 20 20 20  opy[i+1]);.     
1e0a2 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1e0a3 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66  OK ) goto end_of
1e0a4 5f 76 61 63 75 75 6d 3b 0a 20 20 20 20 7d 0a 0a  _vacuum;.    }..
1e0a5 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1e0a6 42 74 72 65 65 43 6f 70 79 46 69 6c 65 28 70 4d  BtreeCopyFile(pM
1e0a7 61 69 6e 2c 20 70 54 65 6d 70 29 3b 0a 20 20 20  ain, pTemp);.   
1e0a8 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1e0a9 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66  OK ) goto end_of
1e0aa 5f 76 61 63 75 75 6d 3b 0a 20 20 20 20 72 63 20  _vacuum;.    rc 
1e0ab 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  = sqlite3BtreeCo
1e0ac 6d 6d 69 74 28 70 54 65 6d 70 29 3b 0a 20 20 20  mmit(pTemp);.   
1e0ad 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1e0ae 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66  OK ) goto end_of
1e0af 5f 76 61 63 75 75 6d 3b 0a 20 20 20 20 72 63 20  _vacuum;.    rc 
1e0b0 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  = sqlite3BtreeCo
1e0b1 6d 6d 69 74 28 70 4d 61 69 6e 29 3b 0a 20 20 7d  mmit(pMain);.  }
1e0b2 0a 0a 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3a  ..end_of_vacuum:
1e0b3 0a 20 20 2f 2a 20 52 65 73 74 6f 72 65 20 74 68  .  /* Restore th
1e0b4 65 20 6f 72 69 67 69 6e 61 6c 20 76 61 6c 75 65  e original value
1e0b5 20 6f 66 20 64 62 2d 3e 66 6c 61 67 73 20 2a 2f   of db->flags */
1e0b6 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 3d 20 73  .  db->flags = s
1e0b7 61 76 65 64 5f 66 6c 61 67 73 3b 0a 0a 20 20 2f  aved_flags;..  /
1e0b8 2a 20 43 75 72 72 65 6e 74 6c 79 20 74 68 65 72  * Currently ther
1e0b9 65 20 69 73 20 61 6e 20 53 51 4c 20 6c 65 76 65  e is an SQL leve
1e0ba 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70  l transaction op
1e0bb 65 6e 20 6f 6e 20 74 68 65 20 76 61 63 75 75 6d  en on the vacuum
1e0bc 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20  .  ** database. 
1e0bd 4e 6f 20 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c  No locks are hel
1e0be 64 20 6f 6e 20 61 6e 79 20 6f 74 68 65 72 20 66  d on any other f
1e0bf 69 6c 65 73 20 28 73 69 6e 63 65 20 74 68 65 20  iles (since the 
1e0c0 6d 61 69 6e 20 66 69 6c 65 0a 20 20 2a 2a 20 77  main file.  ** w
1e0c1 61 73 20 63 6f 6d 6d 69 74 74 65 64 20 61 74 20  as committed at 
1e0c2 74 68 65 20 62 74 72 65 65 20 6c 65 76 65 6c 29  the btree level)
1e0c3 2e 20 53 6f 20 69 74 20 73 61 66 65 20 74 6f 20  . So it safe to 
1e0c4 65 6e 64 20 74 68 65 20 74 72 61 6e 73 61 63 74  end the transact
1e0c5 69 6f 6e 0a 20 20 2a 2a 20 62 79 20 6d 61 6e 75  ion.  ** by manu
1e0c6 61 6c 6c 79 20 73 65 74 74 69 6e 67 20 74 68 65  ally setting the
1e0c7 20 61 75 74 6f 43 6f 6d 6d 69 74 20 66 6c 61 67   autoCommit flag
1e0c8 20 74 6f 20 74 72 75 65 20 61 6e 64 20 64 65 74   to true and det
1e0c9 61 63 68 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20  aching the.  ** 
1e0ca 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 2e  vacuum database.
1e0cb 20 54 68 65 20 76 61 63 75 75 6d 5f 64 62 20 6a   The vacuum_db j
1e0cc 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 64  ournal file is d
1e0cd 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68 65 20  eleted when the 
1e0ce 70 61 67 65 72 0a 20 20 2a 2a 20 69 73 20 63 6c  pager.  ** is cl
1e0cf 6f 73 65 64 20 62 79 20 74 68 65 20 44 45 54 41  osed by the DETA
1e0d0 43 48 2e 0a 20 20 2a 2f 0a 20 20 64 62 2d 3e 61  CH..  */.  db->a
1e0d1 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 0a  utoCommit = 1;..
1e0d2 20 20 69 66 28 20 70 44 62 20 29 7b 0a 20 20 20    if( pDb ){.   
1e0d3 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 44 69   sqlite3MallocDi
1e0d4 73 61 6c 6c 6f 77 28 29 3b 0a 20 20 20 20 73 71  sallow();.    sq
1e0d5 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28  lite3BtreeClose(
1e0d6 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20 73  pDb->pBt);.    s
1e0d7 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 6c 6f  qlite3MallocAllo
1e0d8 77 28 29 3b 0a 20 20 20 20 70 44 62 2d 3e 70 42  w();.    pDb->pB
1e0d9 74 20 3d 20 30 3b 0a 20 20 20 20 70 44 62 2d 3e  t = 0;.    pDb->
1e0da 70 53 63 68 65 6d 61 20 3d 20 30 3b 0a 20 20 7d  pSchema = 0;.  }
1e0db 0a 0a 20 20 73 71 6c 69 74 65 33 52 65 73 65 74  ..  sqlite3Reset
1e0dc 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64  InternalSchema(d
1e0dd 62 2c 20 30 29 3b 0a 0a 20 20 72 65 74 75 72 6e  b, 0);..  return
1e0de 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 20 2f   rc;.}.#endif  /
1e0df 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41  * SQLITE_OMIT_VA
1e0e0 43 55 55 4d 20 26 26 20 53 51 4c 49 54 45 5f 4f  CUUM && SQLITE_O
1e0e1 4d 49 54 5f 41 54 54 41 43 48 20 2a 2f 0a 0a 2f  MIT_ATTACH */../
1e0e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
1e0e3 6e 64 20 6f 66 20 76 61 63 75 75 6d 2e 63 20 2a  nd of vacuum.c *
1e0e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e0e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e0e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
1e0e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
1e0e8 65 67 69 6e 20 66 69 6c 65 20 76 74 61 62 2e 63  egin file vtab.c
1e0e9 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1e0ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e0eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
1e0ec 2a 0a 2a 2a 20 32 30 30 36 20 4a 75 6e 65 20 31  *.** 2006 June 1
1e0ed 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  0.**.** The auth
1e0ee 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
1e0ef 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
1e0f0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
1e0f1 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
1e0f2 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
1e0f3 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
1e0f4 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
1e0f5 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
1e0f6 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
1e0f7 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
1e0f8 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
1e0f9 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
1e0fa 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
1e0fb 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
1e0fc 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
1e0fd 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
1e0fe 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
1e0ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e101 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e102 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e103 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
1e104 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64  ntains code used
1e105 20 74 6f 20 68 65 6c 70 20 69 6d 70 6c 65 6d 65   to help impleme
1e106 6e 74 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  nt virtual table
1e107 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 74  s..**.** $Id: vt
1e108 61 62 2e 63 2c 76 20 31 2e 34 38 20 32 30 30 37  ab.c,v 1.48 2007
1e109 2f 30 36 2f 32 36 20 31 30 3a 33 38 3a 35 35 20  /06/26 10:38:55 
1e10a 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20  danielk1977 Exp 
1e10b 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  $.*/.#ifndef SQL
1e10c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1e10d 54 41 42 4c 45 0a 0a 73 74 61 74 69 63 20 69 6e  TABLE..static in
1e10e 74 20 63 72 65 61 74 65 4d 6f 64 75 6c 65 28 0a  t createModule(.
1e10f 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
1e110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e111 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 6e    /* Database in
1e112 20 77 68 69 63 68 20 6d 6f 64 75 6c 65 20 69 73   which module is
1e113 20 72 65 67 69 73 74 65 72 65 64 20 2a 2f 0a 20   registered */. 
1e114 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
1e115 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  me,             
1e116 20 2f 2a 20 4e 61 6d 65 20 61 73 73 69 67 6e 65   /* Name assigne
1e117 64 20 74 6f 20 74 68 69 73 20 6d 6f 64 75 6c 65  d to this module
1e118 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69   */.  const sqli
1e119 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64  te3_module *pMod
1e11a 75 6c 65 2c 20 20 2f 2a 20 54 68 65 20 64 65 66  ule,  /* The def
1e11b 69 6e 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d  inition of the m
1e11c 6f 64 75 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20  odule */.  void 
1e11d 2a 70 41 75 78 2c 20 20 20 20 20 20 20 20 20 20  *pAux,          
1e11e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
1e11f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 20 66 6f  ntext pointer fo
1e120 72 20 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e 65  r xCreate/xConne
1e121 63 74 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78  ct */.  void (*x
1e122 44 65 73 74 72 6f 79 29 28 76 6f 69 64 20 2a 29  Destroy)(void *)
1e123 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c          /* Modul
1e124 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e  e destructor fun
1e125 63 74 69 6f 6e 20 2a 2f 0a 29 20 7b 0a 20 20 69  ction */.) {.  i
1e126 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 74 72 6c 65  nt nName = strle
1e127 6e 28 7a 4e 61 6d 65 29 3b 0a 20 20 4d 6f 64 75  n(zName);.  Modu
1e128 6c 65 20 2a 70 4d 6f 64 20 3d 20 28 4d 6f 64 75  le *pMod = (Modu
1e129 6c 65 20 2a 29 73 71 6c 69 74 65 4d 61 6c 6c 6f  le *)sqliteMallo
1e12a 63 52 61 77 28 73 69 7a 65 6f 66 28 4d 6f 64 75  cRaw(sizeof(Modu
1e12b 6c 65 29 20 2b 20 6e 4e 61 6d 65 20 2b 20 31 29  le) + nName + 1)
1e12c 3b 0a 20 20 69 66 28 20 70 4d 6f 64 20 29 7b 0a  ;.  if( pMod ){.
1e12d 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 70 79 20      char *zCopy 
1e12e 3d 20 28 63 68 61 72 20 2a 29 28 26 70 4d 6f 64  = (char *)(&pMod
1e12f 5b 31 5d 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79  [1]);.    memcpy
1e130 28 7a 43 6f 70 79 2c 20 7a 4e 61 6d 65 2c 20 6e  (zCopy, zName, n
1e131 4e 61 6d 65 2b 31 29 3b 0a 20 20 20 20 70 4d 6f  Name+1);.    pMo
1e132 64 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 43 6f 70 79  d->zName = zCopy
1e133 3b 0a 20 20 20 20 70 4d 6f 64 2d 3e 70 4d 6f 64  ;.    pMod->pMod
1e134 75 6c 65 20 3d 20 70 4d 6f 64 75 6c 65 3b 0a 20  ule = pModule;. 
1e135 20 20 20 70 4d 6f 64 2d 3e 70 41 75 78 20 3d 20     pMod->pAux = 
1e136 70 41 75 78 3b 0a 20 20 20 20 70 4d 6f 64 2d 3e  pAux;.    pMod->
1e137 78 44 65 73 74 72 6f 79 20 3d 20 78 44 65 73 74  xDestroy = xDest
1e138 72 6f 79 3b 0a 20 20 20 20 70 4d 6f 64 20 3d 20  roy;.    pMod = 
1e139 28 4d 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74 65  (Module *)sqlite
1e13a 33 48 61 73 68 49 6e 73 65 72 74 28 26 64 62 2d  3HashInsert(&db-
1e13b 3e 61 4d 6f 64 75 6c 65 2c 20 7a 43 6f 70 79 2c  >aModule, zCopy,
1e13c 20 6e 4e 61 6d 65 2c 20 28 76 6f 69 64 2a 29 70   nName, (void*)p
1e13d 4d 6f 64 29 3b 0a 20 20 20 20 69 66 28 20 70 4d  Mod);.    if( pM
1e13e 6f 64 20 26 26 20 70 4d 6f 64 2d 3e 78 44 65 73  od && pMod->xDes
1e13f 74 72 6f 79 20 29 7b 0a 20 20 20 20 20 20 70 4d  troy ){.      pM
1e140 6f 64 2d 3e 78 44 65 73 74 72 6f 79 28 70 4d 6f  od->xDestroy(pMo
1e141 64 2d 3e 70 41 75 78 29 3b 0a 20 20 20 20 7d 0a  d->pAux);.    }.
1e142 20 20 20 20 73 71 6c 69 74 65 46 72 65 65 28 70      sqliteFree(p
1e143 4d 6f 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Mod);.    sqlite
1e144 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63  3ResetInternalSc
1e145 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 7d  hema(db, 0);.  }
1e146 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
1e147 33 41 70 69 45 78 69 74 28 64 62 2c 20 53 51 4c  3ApiExit(db, SQL
1e148 49 54 45 5f 4f 4b 29 3b 0a 7d 0a 0a 0a 2f 2a 0a  ITE_OK);.}.../*.
1e149 2a 2a 20 45 78 74 65 72 6e 61 6c 20 41 50 49 20  ** External API 
1e14a 66 75 6e 63 74 69 6f 6e 20 75 73 65 64 20 74 6f  function used to
1e14b 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 76 69   create a new vi
1e14c 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 6f 64 75  rtual-table modu
1e14d 6c 65 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74  le..*/.int sqlit
1e14e 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
1e14f 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
1e150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e151 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
1e152 69 6e 20 77 68 69 63 68 20 6d 6f 64 75 6c 65 20  in which module 
1e153 69 73 20 72 65 67 69 73 74 65 72 65 64 20 2a 2f  is registered */
1e154 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
1e155 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20  Name,           
1e156 20 20 20 2f 2a 20 4e 61 6d 65 20 61 73 73 69 67     /* Name assig
1e157 6e 65 64 20 74 6f 20 74 68 69 73 20 6d 6f 64 75  ned to this modu
1e158 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71  le */.  const sq
1e159 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d  lite3_module *pM
1e15a 6f 64 75 6c 65 2c 20 20 2f 2a 20 54 68 65 20 64  odule,  /* The d
1e15b 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 74 68 65  efinition of the
1e15c 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 76 6f 69   module */.  voi
1e15d 64 20 2a 70 41 75 78 20 20 20 20 20 20 20 20 20  d *pAux         
1e15e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1e15f 43 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 20  Context pointer 
1e160 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f 6e  for xCreate/xCon
1e161 6e 65 63 74 20 2a 2f 0a 29 7b 0a 20 20 72 65 74  nect */.){.  ret
1e162 75 72 6e 20 63 72 65 61 74 65 4d 6f 64 75 6c 65  urn createModule
1e163 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 70 4d 6f 64  (db, zName, pMod
1e164 75 6c 65 2c 20 70 41 75 78 2c 20 30 29 3b 0a 7d  ule, pAux, 0);.}
1e165 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 65 72 6e 61 6c  ../*.** External
1e166 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 20 75 73   API function us
1e167 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e  ed to create a n
1e168 65 77 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65  ew virtual-table
1e169 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 69 6e 74 20   module..*/.int 
1e16a 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d  sqlite3_create_m
1e16b 6f 64 75 6c 65 5f 76 32 28 0a 20 20 73 71 6c 69  odule_v2(.  sqli
1e16c 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
1e16d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
1e16e 61 74 61 62 61 73 65 20 69 6e 20 77 68 69 63 68  atabase in which
1e16f 20 6d 6f 64 75 6c 65 20 69 73 20 72 65 67 69 73   module is regis
1e170 74 65 72 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74  tered */.  const
1e171 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20   char *zName,   
1e172 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
1e173 6d 65 20 61 73 73 69 67 6e 65 64 20 74 6f 20 74  me assigned to t
1e174 68 69 73 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20  his module */.  
1e175 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f  const sqlite3_mo
1e176 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 2c 20 20  dule *pModule,  
1e177 2f 2a 20 54 68 65 20 64 65 66 69 6e 69 74 69 6f  /* The definitio
1e178 6e 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 20  n of the module 
1e179 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 2c  */.  void *pAux,
1e17a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e17b 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20       /* Context 
1e17c 70 6f 69 6e 74 65 72 20 66 6f 72 20 78 43 72 65  pointer for xCre
1e17d 61 74 65 2f 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a  ate/xConnect */.
1e17e 20 20 76 6f 69 64 20 28 2a 78 44 65 73 74 72 6f    void (*xDestro
1e17f 79 29 28 76 6f 69 64 20 2a 29 20 20 20 20 20 20  y)(void *)      
1e180 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 64 65 73 74    /* Module dest
1e181 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20  ructor function 
1e182 2a 2f 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 63  */.){.  return c
1e183 72 65 61 74 65 4d 6f 64 75 6c 65 28 64 62 2c 20  reateModule(db, 
1e184 7a 4e 61 6d 65 2c 20 70 4d 6f 64 75 6c 65 2c 20  zName, pModule, 
1e185 70 41 75 78 2c 20 78 44 65 73 74 72 6f 79 29 3b  pAux, xDestroy);
1e186 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74  .}../*.** Lock t
1e187 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
1e188 20 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e 6e   so that it cann
1e189 6f 74 20 62 65 20 64 69 73 63 6f 6e 6e 65 63 74  ot be disconnect
1e18a 65 64 2e 0a 2a 2a 20 4c 6f 63 6b 73 20 6e 65 73  ed..** Locks nes
1e18b 74 2e 20 20 45 76 65 72 79 20 6c 6f 63 6b 20 73  t.  Every lock s
1e18c 68 6f 75 6c 64 20 68 61 76 65 20 61 20 63 6f 72  hould have a cor
1e18d 72 65 73 70 6f 6e 64 69 6e 67 20 75 6e 6c 6f 63  responding unloc
1e18e 6b 2e 0a 2a 2a 20 49 66 20 61 6e 20 75 6e 6c 6f  k..** If an unlo
1e18f 63 6b 20 69 73 20 6f 6d 69 74 74 65 64 2c 20 72  ck is omitted, r
1e190 65 73 6f 75 72 63 65 73 20 6c 65 61 6b 73 20 77  esources leaks w
1e191 69 6c 6c 20 6f 63 63 75 72 2e 20 20 0a 2a 2a 0a  ill occur.  .**.
1e192 2a 2a 20 49 66 20 61 20 64 69 73 63 6f 6e 6e 65  ** If a disconne
1e193 63 74 20 69 73 20 61 74 74 65 6d 70 74 65 64 20  ct is attempted 
1e194 77 68 69 6c 65 20 61 20 76 69 72 74 75 61 6c 20  while a virtual 
1e195 74 61 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64 2c  table is locked,
1e196 0a 2a 2a 20 74 68 65 20 64 69 73 63 6f 6e 6e 65  .** the disconne
1e197 63 74 20 69 73 20 64 65 66 65 72 72 65 64 20 75  ct is deferred u
1e198 6e 74 69 6c 20 61 6c 6c 20 6c 6f 63 6b 73 20 68  ntil all locks h
1e199 61 76 65 20 62 65 65 6e 20 72 65 6d 6f 76 65 64  ave been removed
1e19a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1e19b 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1e19c 56 74 61 62 4c 6f 63 6b 28 73 71 6c 69 74 65 33  VtabLock(sqlite3
1e19d 5f 76 74 61 62 20 2a 70 56 74 61 62 29 7b 0a 20  _vtab *pVtab){. 
1e19e 20 70 56 74 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a   pVtab->nRef++;.
1e19f 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 6f 63 6b 20  }../*.** Unlock 
1e1a0 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  a virtual table.
1e1a1 20 20 57 68 65 6e 20 74 68 65 20 6c 61 73 74 20    When the last 
1e1a2 6c 6f 63 6b 20 69 73 20 72 65 6d 6f 76 65 64 2c  lock is removed,
1e1a3 0a 2a 2a 20 64 69 73 63 6f 6e 6e 65 63 74 20 74  .** disconnect t
1e1a4 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
1e1a5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1e1a6 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1e1a7 56 74 61 62 55 6e 6c 6f 63 6b 28 73 71 6c 69 74  VtabUnlock(sqlit
1e1a8 65 33 20 2a 64 62 2c 20 73 71 6c 69 74 65 33 5f  e3 *db, sqlite3_
1e1a9 76 74 61 62 20 2a 70 56 74 61 62 29 7b 0a 20 20  vtab *pVtab){.  
1e1aa 70 56 74 61 62 2d 3e 6e 52 65 66 2d 2d 3b 0a 20  pVtab->nRef--;. 
1e1ab 20 61 73 73 65 72 74 28 64 62 29 3b 0a 20 20 61   assert(db);.  a
1e1ac 73 73 65 72 74 28 21 73 71 6c 69 74 65 33 53 61  ssert(!sqlite3Sa
1e1ad 66 65 74 79 43 68 65 63 6b 28 64 62 29 29 3b 0a  fetyCheck(db));.
1e1ae 20 20 69 66 28 20 70 56 74 61 62 2d 3e 6e 52 65    if( pVtab->nRe
1e1af 66 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  f==0 ){.    if( 
1e1b0 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54  db->magic==SQLIT
1e1b1 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 7b 0a  E_MAGIC_BUSY ){.
1e1b2 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 61 66        sqlite3Saf
1e1b3 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20  etyOff(db);.    
1e1b4 20 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65    pVtab->pModule
1e1b5 2d 3e 78 44 69 73 63 6f 6e 6e 65 63 74 28 70 56  ->xDisconnect(pV
1e1b6 74 61 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  tab);.      sqli
1e1b7 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b  te3SafetyOn(db);
1e1b8 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20  .    } else {.  
1e1b9 20 20 20 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75      pVtab->pModu
1e1ba 6c 65 2d 3e 78 44 69 73 63 6f 6e 6e 65 63 74 28  le->xDisconnect(
1e1bb 70 56 74 61 62 29 3b 0a 20 20 20 20 7d 0a 20 20  pVtab);.    }.  
1e1bc 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72  }.}../*.** Clear
1e1bd 20 61 6e 79 20 61 6e 64 20 61 6c 6c 20 76 69 72   any and all vir
1e1be 74 75 61 6c 2d 74 61 62 6c 65 20 69 6e 66 6f 72  tual-table infor
1e1bf 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20  mation from the 
1e1c0 54 61 62 6c 65 20 72 65 63 6f 72 64 2e 0a 2a 2a  Table record..**
1e1c1 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
1e1c2 20 63 61 6c 6c 65 64 2c 20 66 6f 72 20 65 78 61   called, for exa
1e1c3 6d 70 6c 65 2c 20 6a 75 73 74 20 62 65 66 6f 72  mple, just befor
1e1c4 65 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 54  e deleting the T
1e1c5 61 62 6c 65 0a 2a 2a 20 72 65 63 6f 72 64 2e 0a  able.** record..
1e1c6 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1e1c7 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74  E void sqlite3Vt
1e1c8 61 62 43 6c 65 61 72 28 54 61 62 6c 65 20 2a 70  abClear(Table *p
1e1c9 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61  ){.  sqlite3_vta
1e1ca 62 20 2a 70 56 74 61 62 20 3d 20 70 2d 3e 70 56  b *pVtab = p->pV
1e1cb 74 61 62 3b 0a 20 20 69 66 28 20 70 56 74 61 62  tab;.  if( pVtab
1e1cc 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
1e1cd 70 2d 3e 70 4d 6f 64 20 26 26 20 70 2d 3e 70 4d  p->pMod && p->pM
1e1ce 6f 64 2d 3e 70 4d 6f 64 75 6c 65 20 29 3b 0a 20  od->pModule );. 
1e1cf 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e     sqlite3VtabUn
1e1d0 6c 6f 63 6b 28 70 2d 3e 70 53 63 68 65 6d 61 2d  lock(p->pSchema-
1e1d1 3e 64 62 2c 20 70 56 74 61 62 29 3b 0a 20 20 20  >db, pVtab);.   
1e1d2 20 70 2d 3e 70 56 74 61 62 20 3d 20 30 3b 0a 20   p->pVtab = 0;. 
1e1d3 20 7d 0a 20 20 69 66 28 20 70 2d 3e 61 7a 4d 6f   }.  if( p->azMo
1e1d4 64 75 6c 65 41 72 67 20 29 7b 0a 20 20 20 20 69  duleArg ){.    i
1e1d5 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d  nt i;.    for(i=
1e1d6 30 3b 20 69 3c 70 2d 3e 6e 4d 6f 64 75 6c 65 41  0; i<p->nModuleA
1e1d7 72 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  rg; i++){.      
1e1d8 73 71 6c 69 74 65 46 72 65 65 28 70 2d 3e 61 7a  sqliteFree(p->az
1e1d9 4d 6f 64 75 6c 65 41 72 67 5b 69 5d 29 3b 0a 20  ModuleArg[i]);. 
1e1da 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 46     }.    sqliteF
1e1db 72 65 65 28 70 2d 3e 61 7a 4d 6f 64 75 6c 65 41  ree(p->azModuleA
1e1dc 72 67 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  rg);.  }.}../*.*
1e1dd 2a 20 41 64 64 20 61 20 6e 65 77 20 6d 6f 64 75  * Add a new modu
1e1de 6c 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 70  le argument to p
1e1df 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41  Table->azModuleA
1e1e0 72 67 5b 5d 2e 0a 2a 2a 20 54 68 65 20 73 74 72  rg[]..** The str
1e1e1 69 6e 67 20 69 73 20 6e 6f 74 20 63 6f 70 69 65  ing is not copie
1e1e2 64 20 2d 20 74 68 65 20 70 6f 69 6e 74 65 72 20  d - the pointer 
1e1e3 69 73 20 73 74 6f 72 65 64 2e 20 20 54 68 65 0a  is stored.  The.
1e1e4 2a 2a 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 62  ** string will b
1e1e5 65 20 66 72 65 65 64 20 61 75 74 6f 6d 61 74 69  e freed automati
1e1e6 63 61 6c 6c 79 20 77 68 65 6e 20 74 68 65 20 74  cally when the t
1e1e7 61 62 6c 65 20 69 73 0a 2a 2a 20 64 65 6c 65 74  able is.** delet
1e1e8 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ed..*/.static vo
1e1e9 69 64 20 61 64 64 4d 6f 64 75 6c 65 41 72 67 75  id addModuleArgu
1e1ea 6d 65 6e 74 28 54 61 62 6c 65 20 2a 70 54 61 62  ment(Table *pTab
1e1eb 6c 65 2c 20 63 68 61 72 20 2a 7a 41 72 67 29 7b  le, char *zArg){
1e1ec 0a 20 20 69 6e 74 20 69 20 3d 20 70 54 61 62 6c  .  int i = pTabl
1e1ed 65 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 2b 2b 3b  e->nModuleArg++;
1e1ee 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 20 3d 20  .  int nBytes = 
1e1ef 73 69 7a 65 6f 66 28 63 68 61 72 20 2a 29 2a 28  sizeof(char *)*(
1e1f0 31 2b 70 54 61 62 6c 65 2d 3e 6e 4d 6f 64 75 6c  1+pTable->nModul
1e1f1 65 41 72 67 29 3b 0a 20 20 63 68 61 72 20 2a 2a  eArg);.  char **
1e1f2 61 7a 4d 6f 64 75 6c 65 41 72 67 3b 0a 20 20 61  azModuleArg;.  a
1e1f3 7a 4d 6f 64 75 6c 65 41 72 67 20 3d 20 73 71 6c  zModuleArg = sql
1e1f4 69 74 65 52 65 61 6c 6c 6f 63 28 70 54 61 62 6c  iteRealloc(pTabl
1e1f5 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 2c 20  e->azModuleArg, 
1e1f6 6e 42 79 74 65 73 29 3b 0a 20 20 69 66 28 20 61  nBytes);.  if( a
1e1f7 7a 4d 6f 64 75 6c 65 41 72 67 3d 3d 30 20 29 7b  zModuleArg==0 ){
1e1f8 0a 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20  .    int j;.    
1e1f9 66 6f 72 28 6a 3d 30 3b 20 6a 3c 69 3b 20 6a 2b  for(j=0; j<i; j+
1e1fa 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  +){.      sqlite
1e1fb 46 72 65 65 28 70 54 61 62 6c 65 2d 3e 61 7a 4d  Free(pTable->azM
1e1fc 6f 64 75 6c 65 41 72 67 5b 6a 5d 29 3b 0a 20 20  oduleArg[j]);.  
1e1fd 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 46 72    }.    sqliteFr
1e1fe 65 65 28 7a 41 72 67 29 3b 0a 20 20 20 20 73 71  ee(zArg);.    sq
1e1ff 6c 69 74 65 46 72 65 65 28 70 54 61 62 6c 65 2d  liteFree(pTable-
1e200 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 29 3b 0a 20  >azModuleArg);. 
1e201 20 20 20 70 54 61 62 6c 65 2d 3e 6e 4d 6f 64 75     pTable->nModu
1e202 6c 65 41 72 67 20 3d 20 30 3b 0a 20 20 7d 65 6c  leArg = 0;.  }el
1e203 73 65 7b 0a 20 20 20 20 61 7a 4d 6f 64 75 6c 65  se{.    azModule
1e204 41 72 67 5b 69 5d 20 3d 20 7a 41 72 67 3b 0a 20  Arg[i] = zArg;. 
1e205 20 20 20 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 69     azModuleArg[i
1e206 2b 31 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70  +1] = 0;.  }.  p
1e207 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41  Table->azModuleA
1e208 72 67 20 3d 20 61 7a 4d 6f 64 75 6c 65 41 72 67  rg = azModuleArg
1e209 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70  ;.}../*.** The p
1e20a 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73  arser calls this
1e20b 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 69 74   routine when it
1e20c 20 66 69 72 73 74 20 73 65 65 73 20 61 20 43 52   first sees a CR
1e20d 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42  EATE VIRTUAL TAB
1e20e 4c 45 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e  LE.** statement.
1e20f 20 20 54 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d    The module nam
1e210 65 20 68 61 73 20 62 65 65 6e 20 70 61 72 73 65  e has been parse
1e211 64 2c 20 62 75 74 20 74 68 65 20 6f 70 74 69 6f  d, but the optio
1e212 6e 61 6c 20 6c 69 73 74 0a 2a 2a 20 6f 66 20 70  nal list.** of p
1e213 61 72 61 6d 65 74 65 72 73 20 74 68 61 74 20 66  arameters that f
1e214 6f 6c 6c 6f 77 20 74 68 65 20 6d 6f 64 75 6c 65  ollow the module
1e215 20 6e 61 6d 65 20 61 72 65 20 73 74 69 6c 6c 20   name are still 
1e216 70 65 6e 64 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49  pending..*/.SQLI
1e217 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1e218 73 71 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e  sqlite3VtabBegin
1e219 50 61 72 73 65 28 0a 20 20 50 61 72 73 65 20 2a  Parse(.  Parse *
1e21a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f  pParse,        /
1e21b 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
1e21c 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e  t */.  Token *pN
1e21d 61 6d 65 31 2c 20 20 20 20 20 20 20 20 2f 2a 20  ame1,        /* 
1e21e 4e 61 6d 65 20 6f 66 20 6e 65 77 20 74 61 62 6c  Name of new tabl
1e21f 65 2c 20 6f 72 20 64 61 74 61 62 61 73 65 20 6e  e, or database n
1e220 61 6d 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  ame */.  Token *
1e221 70 4e 61 6d 65 32 2c 20 20 20 20 20 20 20 20 2f  pName2,        /
1e222 2a 20 4e 61 6d 65 20 6f 66 20 6e 65 77 20 74 61  * Name of new ta
1e223 62 6c 65 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20  ble or NULL */. 
1e224 20 54 6f 6b 65 6e 20 2a 70 4d 6f 64 75 6c 65 4e   Token *pModuleN
1e225 61 6d 65 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f  ame    /* Name o
1e226 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 66 6f 72  f the module for
1e227 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
1e228 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  le */.){.  int i
1e229 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
1e22a 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
1e22b 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 62 65   the table is be
1e22c 69 6e 67 20 63 72 65 61 74 65 64 20 69 6e 20 2a  ing created in *
1e22d 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 6c  /.  Table *pTabl
1e22e 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  e;        /* The
1e22f 20 6e 65 77 20 76 69 72 74 75 61 6c 20 74 61 62   new virtual tab
1e230 6c 65 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  le */..#ifndef S
1e231 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
1e232 44 5f 43 41 43 48 45 0a 20 20 69 66 28 20 73 71  D_CACHE.  if( sq
1e233 6c 69 74 65 33 54 68 72 65 61 64 44 61 74 61 52  lite3ThreadDataR
1e234 65 61 64 4f 6e 6c 79 28 29 2d 3e 75 73 65 53 68  eadOnly()->useSh
1e235 61 72 65 64 44 61 74 61 20 29 7b 0a 20 20 20 20  aredData ){.    
1e236 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1e237 70 50 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74 20  pParse, "Cannot 
1e238 75 73 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  use virtual tabl
1e239 65 73 20 69 6e 20 73 68 61 72 65 64 2d 63 61 63  es in shared-cac
1e23a 68 65 20 6d 6f 64 65 22 29 3b 0a 20 20 20 20 72  he mode");.    r
1e23b 65 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69  eturn;.  }.#endi
1e23c 66 0a 0a 20 20 73 71 6c 69 74 65 33 53 74 61 72  f..  sqlite3Star
1e23d 74 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70  tTable(pParse, p
1e23e 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 30  Name1, pName2, 0
1e23f 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 70 54  , 0, 1, 0);.  pT
1e240 61 62 6c 65 20 3d 20 70 50 61 72 73 65 2d 3e 70  able = pParse->p
1e241 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69 66 28 20  NewTable;.  if( 
1e242 70 54 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 50 61  pTable==0 || pPa
1e243 72 73 65 2d 3e 6e 45 72 72 20 29 20 72 65 74 75  rse->nErr ) retu
1e244 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 30 3d  rn;.  assert( 0=
1e245 3d 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65 78 20  =pTable->pIndex 
1e246 29 3b 0a 0a 20 20 69 44 62 20 3d 20 73 71 6c 69  );..  iDb = sqli
1e247 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
1e248 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61  (pParse->db, pTa
1e249 62 6c 65 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  ble->pSchema);. 
1e24a 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20   assert( iDb>=0 
1e24b 29 3b 0a 0a 20 20 70 54 61 62 6c 65 2d 3e 69 73  );..  pTable->is
1e24c 56 69 72 74 75 61 6c 20 3d 20 31 3b 0a 20 20 70  Virtual = 1;.  p
1e24d 54 61 62 6c 65 2d 3e 6e 4d 6f 64 75 6c 65 41 72  Table->nModuleAr
1e24e 67 20 3d 20 30 3b 0a 20 20 61 64 64 4d 6f 64 75  g = 0;.  addModu
1e24f 6c 65 41 72 67 75 6d 65 6e 74 28 70 54 61 62 6c  leArgument(pTabl
1e250 65 2c 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72  e, sqlite3NameFr
1e251 6f 6d 54 6f 6b 65 6e 28 70 4d 6f 64 75 6c 65 4e  omToken(pModuleN
1e252 61 6d 65 29 29 3b 0a 20 20 61 64 64 4d 6f 64 75  ame));.  addModu
1e253 6c 65 41 72 67 75 6d 65 6e 74 28 70 54 61 62 6c  leArgument(pTabl
1e254 65 2c 20 73 71 6c 69 74 65 33 53 74 72 44 75 70  e, sqlite3StrDup
1e255 28 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62  (pParse->db->aDb
1e256 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 29 3b 0a 20  [iDb].zName));. 
1e257 20 61 64 64 4d 6f 64 75 6c 65 41 72 67 75 6d 65   addModuleArgume
1e258 6e 74 28 70 54 61 62 6c 65 2c 20 73 71 6c 69 74  nt(pTable, sqlit
1e259 65 33 53 74 72 44 75 70 28 70 54 61 62 6c 65 2d  e3StrDup(pTable-
1e25a 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 70 50 61 72  >zName));.  pPar
1e25b 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 6e  se->sNameToken.n
1e25c 20 3d 20 70 4d 6f 64 75 6c 65 4e 61 6d 65 2d 3e   = pModuleName->
1e25d 7a 20 2b 20 70 4d 6f 64 75 6c 65 4e 61 6d 65 2d  z + pModuleName-
1e25e 3e 6e 20 2d 20 70 4e 61 6d 65 31 2d 3e 7a 3b 0a  >n - pName1->z;.
1e25f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1e260 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49  OMIT_AUTHORIZATI
1e261 4f 4e 0a 20 20 2f 2a 20 43 72 65 61 74 69 6e 67  ON.  /* Creating
1e262 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
1e263 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 61 75 74   invokes the aut
1e264 68 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62  horization callb
1e265 61 63 6b 20 74 77 69 63 65 2e 0a 20 20 2a 2a 20  ack twice..  ** 
1e266 54 68 65 20 66 69 72 73 74 20 69 6e 76 6f 63 61  The first invoca
1e267 74 69 6f 6e 2c 20 74 6f 20 6f 62 74 61 69 6e 20  tion, to obtain 
1e268 70 65 72 6d 69 73 73 69 6f 6e 20 74 6f 20 49 4e  permission to IN
1e269 53 45 52 54 20 61 20 72 6f 77 20 69 6e 74 6f 20  SERT a row into 
1e26a 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f  the.  ** sqlite_
1e26b 6d 61 73 74 65 72 20 74 61 62 6c 65 2c 20 68 61  master table, ha
1e26c 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6d  s already been m
1e26d 61 64 65 20 62 79 20 73 71 6c 69 74 65 33 53 74  ade by sqlite3St
1e26e 61 72 74 54 61 62 6c 65 28 29 2e 0a 20 20 2a 2a  artTable()..  **
1e26f 20 54 68 65 20 73 65 63 6f 6e 64 20 63 61 6c 6c   The second call
1e270 2c 20 74 6f 20 6f 62 74 61 69 6e 20 70 65 72 6d  , to obtain perm
1e271 69 73 73 69 6f 6e 20 74 6f 20 63 72 65 61 74 65  ission to create
1e272 20 74 68 65 20 74 61 62 6c 65 2c 20 69 73 20 6d   the table, is m
1e273 61 64 65 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20  ade now..  */.  
1e274 69 66 28 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f  if( pTable->azMo
1e275 64 75 6c 65 41 72 67 20 29 7b 0a 20 20 20 20 73  duleArg ){.    s
1e276 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
1e277 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 43  pParse, SQLITE_C
1e278 52 45 41 54 45 5f 56 54 41 42 4c 45 2c 20 70 54  REATE_VTABLE, pT
1e279 61 62 6c 65 2d 3e 7a 4e 61 6d 65 2c 20 0a 20 20  able->zName, .  
1e27a 20 20 20 20 20 20 20 20 20 20 70 54 61 62 6c 65            pTable
1e27b 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 30 5d  ->azModuleArg[0]
1e27c 2c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44  , pParse->db->aD
1e27d 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20  b[iDb].zName);. 
1e27e 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a   }.#endif.}../*.
1e27f 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
1e280 74 61 6b 65 73 20 74 68 65 20 6d 6f 64 75 6c 65  takes the module
1e281 20 61 72 67 75 6d 65 6e 74 20 74 68 61 74 20 68   argument that h
1e282 61 73 20 62 65 65 6e 20 61 63 63 75 6d 75 6c 61  as been accumula
1e283 74 69 6e 67 0a 2a 2a 20 69 6e 20 70 50 61 72 73  ting.** in pPars
1e284 65 2d 3e 7a 41 72 67 5b 5d 20 61 6e 64 20 61 70  e->zArg[] and ap
1e285 70 65 6e 64 73 20 69 74 20 74 6f 20 74 68 65 20  pends it to the 
1e286 6c 69 73 74 20 6f 66 20 61 72 67 75 6d 65 6e 74  list of argument
1e287 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 76 69 72 74  s on the.** virt
1e288 75 61 6c 20 74 61 62 6c 65 20 63 75 72 72 65 6e  ual table curren
1e289 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72  tly under constr
1e28a 75 63 74 69 6f 6e 20 69 6e 20 70 50 61 72 73 65  uction in pParse
1e28b 2d 3e 70 54 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61  ->pTable..*/.sta
1e28c 74 69 63 20 76 6f 69 64 20 61 64 64 41 72 67 75  tic void addArgu
1e28d 6d 65 6e 74 54 6f 56 74 61 62 28 50 61 72 73 65  mentToVtab(Parse
1e28e 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 69 66 28   *pParse){.  if(
1e28f 20 70 50 61 72 73 65 2d 3e 73 41 72 67 2e 7a 20   pParse->sArg.z 
1e290 26 26 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  && pParse->pNewT
1e291 61 62 6c 65 20 29 7b 0a 20 20 20 20 63 6f 6e 73  able ){.    cons
1e292 74 20 63 68 61 72 20 2a 7a 20 3d 20 28 63 6f 6e  t char *z = (con
1e293 73 74 20 63 68 61 72 2a 29 70 50 61 72 73 65 2d  st char*)pParse-
1e294 3e 73 41 72 67 2e 7a 3b 0a 20 20 20 20 69 6e 74  >sArg.z;.    int
1e295 20 6e 20 3d 20 70 50 61 72 73 65 2d 3e 73 41 72   n = pParse->sAr
1e296 67 2e 6e 3b 0a 20 20 20 20 61 64 64 4d 6f 64 75  g.n;.    addModu
1e297 6c 65 41 72 67 75 6d 65 6e 74 28 70 50 61 72 73  leArgument(pPars
1e298 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2c 20 73 71  e->pNewTable, sq
1e299 6c 69 74 65 53 74 72 4e 44 75 70 28 7a 2c 20 6e  liteStrNDup(z, n
1e29a 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  ));.  }.}../*.**
1e29b 20 54 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c   The parser call
1e29c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61  s this routine a
1e29d 66 74 65 72 20 74 68 65 20 43 52 45 41 54 45 20  fter the CREATE 
1e29e 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 73 74  VIRTUAL TABLE st
1e29f 61 74 65 6d 65 6e 74 0a 2a 2a 20 68 61 73 20 62  atement.** has b
1e2a0 65 65 6e 20 63 6f 6d 70 6c 65 74 65 6c 79 20 70  een completely p
1e2a1 61 72 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  arsed..*/.SQLITE
1e2a2 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1e2a3 6c 69 74 65 33 56 74 61 62 46 69 6e 69 73 68 50  lite3VtabFinishP
1e2a4 61 72 73 65 28 50 61 72 73 65 20 2a 70 50 61 72  arse(Parse *pPar
1e2a5 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 45 6e 64 29  se, Token *pEnd)
1e2a6 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b  {.  Table *pTab;
1e2a7 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
1e2a8 61 62 6c 65 20 62 65 69 6e 67 20 63 6f 6e 73 74  able being const
1e2a9 72 75 63 74 65 64 20 2a 2f 0a 20 20 73 71 6c 69  ructed */.  sqli
1e2aa 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20  te3 *db;        
1e2ab 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
1e2ac 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
1e2ad 63 68 61 72 20 2a 7a 4d 6f 64 75 6c 65 3b 20 20  char *zModule;  
1e2ae 20 20 20 20 2f 2a 20 54 68 65 20 6d 6f 64 75 6c      /* The modul
1e2af 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  e name of the ta
1e2b0 62 6c 65 3a 20 55 53 49 4e 47 20 6d 6f 64 75 6c  ble: USING modul
1e2b1 65 6e 61 6d 65 20 2a 2f 0a 20 20 4d 6f 64 75 6c  ename */.  Modul
1e2b2 65 20 2a 70 4d 6f 64 20 3d 20 30 3b 0a 0a 20 20  e *pMod = 0;..  
1e2b3 61 64 64 41 72 67 75 6d 65 6e 74 54 6f 56 74 61  addArgumentToVta
1e2b4 62 28 70 50 61 72 73 65 29 3b 0a 20 20 70 50 61  b(pParse);.  pPa
1e2b5 72 73 65 2d 3e 73 41 72 67 2e 7a 20 3d 20 30 3b  rse->sArg.z = 0;
1e2b6 0a 0a 20 20 2f 2a 20 4c 6f 6f 6b 75 70 20 74 68  ..  /* Lookup th
1e2b7 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 2e 20 2a  e module name. *
1e2b8 2f 0a 20 20 70 54 61 62 20 3d 20 70 50 61 72 73  /.  pTab = pPars
1e2b9 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20  e->pNewTable;.  
1e2ba 69 66 28 20 70 54 61 62 3d 3d 30 20 29 20 72 65  if( pTab==0 ) re
1e2bb 74 75 72 6e 3b 0a 20 20 64 62 20 3d 20 70 50 61  turn;.  db = pPa
1e2bc 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70  rse->db;.  if( p
1e2bd 54 61 62 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 3c  Tab->nModuleArg<
1e2be 31 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7a 4d  1 ) return;.  zM
1e2bf 6f 64 75 6c 65 20 3d 20 70 54 61 62 2d 3e 61 7a  odule = pTab->az
1e2c0 4d 6f 64 75 6c 65 41 72 67 5b 30 5d 3b 0a 20 20  ModuleArg[0];.  
1e2c1 70 4d 6f 64 20 3d 20 28 4d 6f 64 75 6c 65 20 2a  pMod = (Module *
1e2c2 29 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64  )sqlite3HashFind
1e2c3 28 26 64 62 2d 3e 61 4d 6f 64 75 6c 65 2c 20 7a  (&db->aModule, z
1e2c4 4d 6f 64 75 6c 65 2c 20 73 74 72 6c 65 6e 28 7a  Module, strlen(z
1e2c5 4d 6f 64 75 6c 65 29 29 3b 0a 20 20 70 54 61 62  Module));.  pTab
1e2c6 2d 3e 70 4d 6f 64 20 3d 20 70 4d 6f 64 3b 0a 20  ->pMod = pMod;. 
1e2c7 20 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 43 52   .  /* If the CR
1e2c8 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42  EATE VIRTUAL TAB
1e2c9 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  LE statement is 
1e2ca 62 65 69 6e 67 20 65 6e 74 65 72 65 64 20 66 6f  being entered fo
1e2cb 72 20 74 68 65 0a 20 20 2a 2a 20 66 69 72 73 74  r the.  ** first
1e2cc 20 74 69 6d 65 20 28 69 6e 20 6f 74 68 65 72 20   time (in other 
1e2cd 77 6f 72 64 73 20 69 66 20 74 68 65 20 76 69 72  words if the vir
1e2ce 74 75 61 6c 20 74 61 62 6c 65 20 69 73 20 61 63  tual table is ac
1e2cf 74 75 61 6c 6c 79 20 62 65 69 6e 67 0a 20 20 2a  tually being.  *
1e2d0 2a 20 63 72 65 61 74 65 64 20 6e 6f 77 20 69 6e  * created now in
1e2d1 73 74 65 61 64 20 6f 66 20 6a 75 73 74 20 62 65  stead of just be
1e2d2 69 6e 67 20 72 65 61 64 20 6f 75 74 20 6f 66 20  ing read out of 
1e2d3 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 29 20 74  sqlite_master) t
1e2d4 68 65 6e 0a 20 20 2a 2a 20 64 6f 20 61 64 64 69  hen.  ** do addi
1e2d5 74 69 6f 6e 61 6c 20 69 6e 69 74 69 61 6c 69 7a  tional initializ
1e2d6 61 74 69 6f 6e 20 77 6f 72 6b 20 61 6e 64 20 73  ation work and s
1e2d7 74 6f 72 65 20 74 68 65 20 73 74 61 74 65 6d 65  tore the stateme
1e2d8 6e 74 20 74 65 78 74 0a 20 20 2a 2a 20 69 6e 20  nt text.  ** in 
1e2d9 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65  the sqlite_maste
1e2da 72 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20  r table..  */.  
1e2db 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75  if( !db->init.bu
1e2dc 73 79 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a  sy ){.    char *
1e2dd 7a 53 74 6d 74 3b 0a 20 20 20 20 63 68 61 72 20  zStmt;.    char 
1e2de 2a 7a 57 68 65 72 65 3b 0a 20 20 20 20 69 6e 74  *zWhere;.    int
1e2df 20 69 44 62 3b 0a 20 20 20 20 56 64 62 65 20 2a   iDb;.    Vdbe *
1e2e0 76 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 75  v;..    /* Compu
1e2e1 74 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20  te the complete 
1e2e2 74 65 78 74 20 6f 66 20 74 68 65 20 43 52 45 41  text of the CREA
1e2e3 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45  TE VIRTUAL TABLE
1e2e4 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
1e2e5 20 20 69 66 28 20 70 45 6e 64 20 29 7b 0a 20 20    if( pEnd ){.  
1e2e6 20 20 20 20 70 50 61 72 73 65 2d 3e 73 4e 61 6d      pParse->sNam
1e2e7 65 54 6f 6b 65 6e 2e 6e 20 3d 20 70 45 6e 64 2d  eToken.n = pEnd-
1e2e8 3e 7a 20 2d 20 70 50 61 72 73 65 2d 3e 73 4e 61  >z - pParse->sNa
1e2e9 6d 65 54 6f 6b 65 6e 2e 7a 20 2b 20 70 45 6e 64  meToken.z + pEnd
1e2ea 2d 3e 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a  ->n;.    }.    z
1e2eb 53 74 6d 74 20 3d 20 73 71 6c 69 74 65 33 4d 50  Stmt = sqlite3MP
1e2ec 72 69 6e 74 66 28 22 43 52 45 41 54 45 20 56 49  rintf("CREATE VI
1e2ed 52 54 55 41 4c 20 54 41 42 4c 45 20 25 54 22 2c  RTUAL TABLE %T",
1e2ee 20 26 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54   &pParse->sNameT
1e2ef 6f 6b 65 6e 29 3b 0a 0a 20 20 20 20 2f 2a 20 41  oken);..    /* A
1e2f0 20 73 6c 6f 74 20 66 6f 72 20 74 68 65 20 72 65   slot for the re
1e2f1 63 6f 72 64 20 68 61 73 20 61 6c 72 65 61 64 79  cord has already
1e2f2 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20   been allocated 
1e2f3 69 6e 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 53  in the .    ** S
1e2f4 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62  QLITE_MASTER tab
1e2f5 6c 65 2e 20 20 57 65 20 6a 75 73 74 20 6e 65 65  le.  We just nee
1e2f6 64 20 74 6f 20 75 70 64 61 74 65 20 74 68 61 74  d to update that
1e2f7 20 73 6c 6f 74 20 77 69 74 68 20 61 6c 6c 0a 20   slot with all. 
1e2f8 20 20 20 2a 2a 20 74 68 65 20 69 6e 66 6f 72 6d     ** the inform
1e2f9 61 74 69 6f 6e 20 77 65 27 76 65 20 63 6f 6c 6c  ation we've coll
1e2fa 65 63 74 65 64 2e 20 20 0a 20 20 20 20 2a 2a 0a  ected.  .    **.
1e2fb 20 20 20 20 2a 2a 20 54 68 65 20 74 6f 70 20 6f      ** The top o
1e2fc 66 20 74 68 65 20 73 74 61 63 6b 20 69 73 20 74  f the stack is t
1e2fd 68 65 20 72 6f 6f 74 70 61 67 65 20 61 6c 6c 6f  he rootpage allo
1e2fe 63 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33  cated by sqlite3
1e2ff 53 74 61 72 74 54 61 62 6c 65 28 29 2e 0a 20 20  StartTable()..  
1e300 20 20 2a 2a 20 54 68 69 73 20 76 61 6c 75 65 20    ** This value 
1e301 69 73 20 61 6c 77 61 79 73 20 30 20 61 6e 64 20  is always 0 and 
1e302 69 73 20 69 67 6e 6f 72 65 64 2c 20 61 20 76 69  is ignored, a vi
1e303 72 74 75 61 6c 20 74 61 62 6c 65 20 64 6f 65 73  rtual table does
1e304 20 6e 6f 74 20 68 61 76 65 20 61 0a 20 20 20 20   not have a.    
1e305 2a 2a 20 72 6f 6f 74 70 61 67 65 2e 20 54 68 65  ** rootpage. The
1e306 20 6e 65 78 74 20 65 6e 74 72 79 20 6f 6e 20 74   next entry on t
1e307 68 65 20 73 74 61 63 6b 20 69 73 20 74 68 65 20  he stack is the 
1e308 72 6f 77 69 64 20 6f 66 20 74 68 65 20 72 65 63  rowid of the rec
1e309 6f 72 64 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68  ord.    ** in th
1e30a 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  e sqlite_master 
1e30b 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  table..    */.  
1e30c 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
1e30d 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
1e30e 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
1e30f 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74  .    sqlite3Nest
1e310 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a  edParse(pParse,.
1e311 20 20 20 20 20 20 22 55 50 44 41 54 45 20 25 51        "UPDATE %Q
1e312 2e 25 73 20 22 0a 20 20 20 20 20 20 20 20 20 22  .%s ".         "
1e313 53 45 54 20 74 79 70 65 3d 27 74 61 62 6c 65 27  SET type='table'
1e314 2c 20 6e 61 6d 65 3d 25 51 2c 20 74 62 6c 5f 6e  , name=%Q, tbl_n
1e315 61 6d 65 3d 25 51 2c 20 72 6f 6f 74 70 61 67 65  ame=%Q, rootpage
1e316 3d 30 2c 20 73 71 6c 3d 25 51 20 22 0a 20 20 20  =0, sql=%Q ".   
1e317 20 20 20 20 22 57 48 45 52 45 20 72 6f 77 69 64      "WHERE rowid
1e318 3d 23 31 22 2c 0a 20 20 20 20 20 20 64 62 2d 3e  =#1",.      db->
1e319 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20  aDb[iDb].zName, 
1e31a 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62  SCHEMA_TABLE(iDb
1e31b 29 2c 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 7a  ),.      pTab->z
1e31c 4e 61 6d 65 2c 0a 20 20 20 20 20 20 70 54 61 62  Name,.      pTab
1e31d 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 7a  ->zName,.      z
1e31e 53 74 6d 74 0a 20 20 20 20 29 3b 0a 20 20 20 20  Stmt.    );.    
1e31f 73 71 6c 69 74 65 46 72 65 65 28 7a 53 74 6d 74  sqliteFree(zStmt
1e320 29 3b 0a 20 20 20 20 76 20 3d 20 73 71 6c 69 74  );.    v = sqlit
1e321 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
1e322 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68  );.    sqlite3Ch
1e323 61 6e 67 65 43 6f 6f 6b 69 65 28 64 62 2c 20 76  angeCookie(db, v
1e324 2c 20 69 44 62 29 3b 0a 0a 20 20 20 20 73 71 6c  , iDb);..    sql
1e325 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1e326 20 4f 50 5f 45 78 70 69 72 65 2c 20 30 2c 20 30   OP_Expire, 0, 0
1e327 29 3b 0a 20 20 20 20 7a 57 68 65 72 65 20 3d 20  );.    zWhere = 
1e328 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 22  sqlite3MPrintf("
1e329 6e 61 6d 65 3d 27 25 71 27 22 2c 20 70 54 61 62  name='%q'", pTab
1e32a 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 73 71  ->zName);.    sq
1e32b 6c 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20  lite3VdbeOp3(v, 
1e32c 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61 2c 20  OP_ParseSchema, 
1e32d 69 44 62 2c 20 31 2c 20 7a 57 68 65 72 65 2c 20  iDb, 1, zWhere, 
1e32e 50 33 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20  P3_DYNAMIC);.   
1e32f 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 33 28   sqlite3VdbeOp3(
1e330 76 2c 20 4f 50 5f 56 43 72 65 61 74 65 2c 20 69  v, OP_VCreate, i
1e331 44 62 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61  Db, 0, pTab->zNa
1e332 6d 65 2c 20 73 74 72 6c 65 6e 28 70 54 61 62 2d  me, strlen(pTab-
1e333 3e 7a 4e 61 6d 65 29 20 2b 20 31 29 3b 0a 20 20  >zName) + 1);.  
1e334 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72  }..  /* If we ar
1e335 65 20 72 65 72 65 61 64 69 6e 67 20 74 68 65 20  e rereading the 
1e336 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61  sqlite_master ta
1e337 62 6c 65 20 63 72 65 61 74 65 20 74 68 65 20 69  ble create the i
1e338 6e 2d 6d 65 6d 6f 72 79 0a 20 20 2a 2a 20 72 65  n-memory.  ** re
1e339 63 6f 72 64 20 6f 66 20 74 68 65 20 74 61 62 6c  cord of the tabl
1e33a 65 2e 20 49 66 20 74 68 65 20 6d 6f 64 75 6c 65  e. If the module
1e33b 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65   has already bee
1e33c 6e 20 72 65 67 69 73 74 65 72 65 64 2c 0a 20 20  n registered,.  
1e33d 2a 2a 20 61 6c 73 6f 20 63 61 6c 6c 20 74 68 65  ** also call the
1e33e 20 78 43 6f 6e 6e 65 63 74 20 6d 65 74 68 6f 64   xConnect method
1e33f 20 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 65 6c   here..  */.  el
1e340 73 65 20 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a  se {.    Table *
1e341 70 4f 6c 64 3b 0a 20 20 20 20 53 63 68 65 6d 61  pOld;.    Schema
1e342 20 2a 70 53 63 68 65 6d 61 20 3d 20 70 54 61 62   *pSchema = pTab
1e343 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 63  ->pSchema;.    c
1e344 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
1e345 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a   = pTab->zName;.
1e346 20 20 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20      int nName = 
1e347 73 74 72 6c 65 6e 28 7a 4e 61 6d 65 29 20 2b 20  strlen(zName) + 
1e348 31 3b 0a 20 20 20 20 70 4f 6c 64 20 3d 20 73 71  1;.    pOld = sq
1e349 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28  lite3HashInsert(
1e34a 26 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73  &pSchema->tblHas
1e34b 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c  h, zName, nName,
1e34c 20 70 54 61 62 29 3b 0a 20 20 20 20 69 66 28 20   pTab);.    if( 
1e34d 70 4f 6c 64 20 29 7b 0a 20 20 20 20 20 20 61 73  pOld ){.      as
1e34e 73 65 72 74 28 20 70 54 61 62 3d 3d 70 4f 6c 64  sert( pTab==pOld
1e34f 20 29 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20 6d   );  /* Malloc m
1e350 75 73 74 20 68 61 76 65 20 66 61 69 6c 65 64 20  ust have failed 
1e351 69 6e 73 69 64 65 20 48 61 73 68 49 6e 73 65 72  inside HashInser
1e352 74 28 29 20 2a 2f 0a 20 20 20 20 20 20 72 65 74  t() */.      ret
1e353 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  urn;.    }.    p
1e354 53 63 68 65 6d 61 2d 3e 64 62 20 3d 20 70 50 61  Schema->db = pPa
1e355 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 70 50 61  rse->db;.    pPa
1e356 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 3d  rse->pNewTable =
1e357 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a   0;.  }.}../*.**
1e358 20 54 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c   The parser call
1e359 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  s this routine w
1e35a 68 65 6e 20 69 74 20 73 65 65 73 20 74 68 65 20  hen it sees the 
1e35b 66 69 72 73 74 20 74 6f 6b 65 6e 0a 2a 2a 20 6f  first token.** o
1e35c 66 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f  f an argument to
1e35d 20 74 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65   the module name
1e35e 20 69 6e 20 61 20 43 52 45 41 54 45 20 56 49 52   in a CREATE VIR
1e35f 54 55 41 4c 20 54 41 42 4c 45 20 73 74 61 74 65  TUAL TABLE state
1e360 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ment..*/.SQLITE_
1e361 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1e362 69 74 65 33 56 74 61 62 41 72 67 49 6e 69 74 28  ite3VtabArgInit(
1e363 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a  Parse *pParse){.
1e364 20 20 61 64 64 41 72 67 75 6d 65 6e 74 54 6f 56    addArgumentToV
1e365 74 61 62 28 70 50 61 72 73 65 29 3b 0a 20 20 70  tab(pParse);.  p
1e366 50 61 72 73 65 2d 3e 73 41 72 67 2e 7a 20 3d 20  Parse->sArg.z = 
1e367 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 73 41 72  0;.  pParse->sAr
1e368 67 2e 6e 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  g.n = 0;.}../*.*
1e369 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 61 6c  * The parser cal
1e36a 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ls this routine 
1e36b 66 6f 72 20 65 61 63 68 20 74 6f 6b 65 6e 20 61  for each token a
1e36c 66 74 65 72 20 74 68 65 20 66 69 72 73 74 20 74  fter the first t
1e36d 6f 6b 65 6e 0a 2a 2a 20 69 6e 20 61 6e 20 61 72  oken.** in an ar
1e36e 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 6d 6f  gument to the mo
1e36f 64 75 6c 65 20 6e 61 6d 65 20 69 6e 20 61 20 43  dule name in a C
1e370 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41  REATE VIRTUAL TA
1e371 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  BLE statement..*
1e372 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1e373 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61   void sqlite3Vta
1e374 62 41 72 67 45 78 74 65 6e 64 28 50 61 72 73 65  bArgExtend(Parse
1e375 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20   *pParse, Token 
1e376 2a 70 29 7b 0a 20 20 54 6f 6b 65 6e 20 2a 70 41  *p){.  Token *pA
1e377 72 67 20 3d 20 26 70 50 61 72 73 65 2d 3e 73 41  rg = &pParse->sA
1e378 72 67 3b 0a 20 20 69 66 28 20 70 41 72 67 2d 3e  rg;.  if( pArg->
1e379 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 70 41 72 67  z==0 ){.    pArg
1e37a 2d 3e 7a 20 3d 20 70 2d 3e 7a 3b 0a 20 20 20 20  ->z = p->z;.    
1e37b 70 41 72 67 2d 3e 6e 20 3d 20 70 2d 3e 6e 3b 0a  pArg->n = p->n;.
1e37c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
1e37d 65 72 74 28 70 41 72 67 2d 3e 7a 20 3c 20 70 2d  ert(pArg->z < p-
1e37e 3e 7a 29 3b 0a 20 20 20 20 70 41 72 67 2d 3e 6e  >z);.    pArg->n
1e37f 20 3d 20 28 70 2d 3e 7a 20 2b 20 70 2d 3e 6e 20   = (p->z + p->n 
1e380 2d 20 70 41 72 67 2d 3e 7a 29 3b 0a 20 20 7d 0a  - pArg->z);.  }.
1e381 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20  }../*.** Invoke 
1e382 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  a virtual table 
1e383 63 6f 6e 73 74 72 75 63 74 6f 72 20 28 65 69 74  constructor (eit
1e384 68 65 72 20 78 43 72 65 61 74 65 20 6f 72 20 78  her xCreate or x
1e385 43 6f 6e 6e 65 63 74 29 2e 20 54 68 65 0a 2a 2a  Connect). The.**
1e386 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
1e387 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e 76 6f  function to invo
1e388 6b 65 20 69 73 20 70 61 73 73 65 64 20 61 73 20  ke is passed as 
1e389 74 68 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d  the fourth param
1e38a 65 74 65 72 0a 2a 2a 20 74 6f 20 74 68 69 73 20  eter.** to this 
1e38b 70 72 6f 63 65 64 75 72 65 2e 0a 2a 2f 0a 73 74  procedure..*/.st
1e38c 61 74 69 63 20 69 6e 74 20 76 74 61 62 43 61 6c  atic int vtabCal
1e38d 6c 43 6f 6e 73 74 72 75 63 74 6f 72 28 0a 20 20  lConstructor(.  
1e38e 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20  sqlite3 *db, .  
1e38f 54 61 62 6c 65 20 2a 70 54 61 62 2c 0a 20 20 4d  Table *pTab,.  M
1e390 6f 64 75 6c 65 20 2a 70 4d 6f 64 2c 0a 20 20 69  odule *pMod,.  i
1e391 6e 74 20 28 2a 78 43 6f 6e 73 74 72 75 63 74 29  nt (*xConstruct)
1e392 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64 2a 2c  (sqlite3*,void*,
1e393 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 63  int,const char*c
1e394 6f 6e 73 74 2a 2c 73 71 6c 69 74 65 33 5f 76 74  onst*,sqlite3_vt
1e395 61 62 2a 2a 2c 63 68 61 72 2a 2a 29 2c 0a 20 20  ab**,char**),.  
1e396 63 68 61 72 20 2a 2a 70 7a 45 72 72 0a 29 7b 0a  char **pzErr.){.
1e397 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20    int rc;.  int 
1e398 72 63 32 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76  rc2;.  sqlite3_v
1e399 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 63 6f  tab *pVtab;.  co
1e39a 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a  nst char *const*
1e39b 61 7a 41 72 67 20 3d 20 28 63 6f 6e 73 74 20 63  azArg = (const c
1e39c 68 61 72 20 2a 63 6f 6e 73 74 2a 29 70 54 61 62  har *const*)pTab
1e39d 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 3b 0a 20  ->azModuleArg;. 
1e39e 20 69 6e 74 20 6e 41 72 67 20 3d 20 70 54 61 62   int nArg = pTab
1e39f 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 3b 0a 20 20  ->nModuleArg;.  
1e3a0 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 30 3b 0a  char *zErr = 0;.
1e3a1 20 20 63 68 61 72 20 2a 7a 4d 6f 64 75 6c 65 4e    char *zModuleN
1e3a2 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  ame = sqlite3MPr
1e3a3 69 6e 74 66 28 22 25 73 22 2c 20 70 54 61 62 2d  intf("%s", pTab-
1e3a4 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 69 66 28 20  >zName);..  if( 
1e3a5 21 7a 4d 6f 64 75 6c 65 4e 61 6d 65 20 29 7b 0a  !zModuleName ){.
1e3a6 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1e3a7 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20  E_NOMEM;.  }..  
1e3a8 61 73 73 65 72 74 28 20 21 64 62 2d 3e 70 56 54  assert( !db->pVT
1e3a9 61 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ab );.  assert( 
1e3aa 78 43 6f 6e 73 74 72 75 63 74 20 29 3b 0a 0a 20  xConstruct );.. 
1e3ab 20 64 62 2d 3e 70 56 54 61 62 20 3d 20 70 54 61   db->pVTab = pTa
1e3ac 62 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  b;.  rc = sqlite
1e3ad 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a  3SafetyOff(db);.
1e3ae 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
1e3af 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 72 63 20  LITE_OK );.  rc 
1e3b0 3d 20 78 43 6f 6e 73 74 72 75 63 74 28 64 62 2c  = xConstruct(db,
1e3b1 20 70 4d 6f 64 2d 3e 70 41 75 78 2c 20 6e 41 72   pMod->pAux, nAr
1e3b2 67 2c 20 61 7a 41 72 67 2c 20 26 70 54 61 62 2d  g, azArg, &pTab-
1e3b3 3e 70 56 74 61 62 2c 20 26 7a 45 72 72 29 3b 0a  >pVtab, &zErr);.
1e3b4 20 20 72 63 32 20 3d 20 73 71 6c 69 74 65 33 53    rc2 = sqlite3S
1e3b5 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 70  afetyOn(db);.  p
1e3b6 56 74 61 62 20 3d 20 70 54 61 62 2d 3e 70 56 74  Vtab = pTab->pVt
1e3b7 61 62 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  ab;.  if( rc==SQ
1e3b8 4c 49 54 45 5f 4f 4b 20 26 26 20 70 56 74 61 62  LITE_OK && pVtab
1e3b9 20 29 7b 0a 20 20 20 20 70 56 74 61 62 2d 3e 70   ){.    pVtab->p
1e3ba 4d 6f 64 75 6c 65 20 3d 20 70 4d 6f 64 2d 3e 70  Module = pMod->p
1e3bb 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 70 56 74 61  Module;.    pVta
1e3bc 62 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 7d  b->nRef = 1;.  }
1e3bd 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  ..  if( SQLITE_O
1e3be 4b 21 3d 72 63 20 29 7b 0a 20 20 20 20 69 66 28  K!=rc ){.    if(
1e3bf 20 7a 45 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20   zErr==0 ){.    
1e3c0 20 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74    *pzErr = sqlit
1e3c1 65 33 4d 50 72 69 6e 74 66 28 22 76 74 61 62 6c  e3MPrintf("vtabl
1e3c2 65 20 63 6f 6e 73 74 72 75 63 74 6f 72 20 66 61  e constructor fa
1e3c3 69 6c 65 64 3a 20 25 73 22 2c 20 7a 4d 6f 64 75  iled: %s", zModu
1e3c4 6c 65 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65 6c  leName);.    }el
1e3c5 73 65 20 7b 0a 20 20 20 20 20 20 2a 70 7a 45 72  se {.      *pzEr
1e3c6 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  r = sqlite3MPrin
1e3c7 74 66 28 22 25 73 22 2c 20 7a 45 72 72 29 3b 0a  tf("%s", zErr);.
1e3c8 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
1e3c9 65 65 28 7a 45 72 72 29 3b 0a 20 20 20 20 7d 0a  ee(zErr);.    }.
1e3ca 20 20 7d 65 6c 73 65 20 69 66 28 20 64 62 2d 3e    }else if( db->
1e3cb 70 56 54 61 62 20 29 7b 0a 20 20 20 20 63 6f 6e  pVTab ){.    con
1e3cc 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74  st char *zFormat
1e3cd 20 3d 20 22 76 74 61 62 6c 65 20 63 6f 6e 73 74   = "vtable const
1e3ce 72 75 63 74 6f 72 20 64 69 64 20 6e 6f 74 20 64  ructor did not d
1e3cf 65 63 6c 61 72 65 20 73 63 68 65 6d 61 3a 20 25  eclare schema: %
1e3d0 73 22 3b 0a 20 20 20 20 2a 70 7a 45 72 72 20 3d  s";.    *pzErr =
1e3d1 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
1e3d2 7a 46 6f 72 6d 61 74 2c 20 70 54 61 62 2d 3e 7a  zFormat, pTab->z
1e3d3 4e 61 6d 65 29 3b 0a 20 20 20 20 72 63 20 3d 20  Name);.    rc = 
1e3d4 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
1e3d5 7d 20 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  } .  if( rc==SQL
1e3d6 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63  ITE_OK ){.    rc
1e3d7 20 3d 20 72 63 32 3b 0a 20 20 7d 0a 20 20 64 62   = rc2;.  }.  db
1e3d8 2d 3e 70 56 54 61 62 20 3d 20 30 3b 0a 20 20 73  ->pVTab = 0;.  s
1e3d9 71 6c 69 74 65 46 72 65 65 28 7a 4d 6f 64 75 6c  qliteFree(zModul
1e3da 65 4e 61 6d 65 29 3b 0a 0a 20 20 2f 2a 20 49 66  eName);..  /* If
1e3db 20 65 76 65 72 79 74 68 69 6e 67 20 77 65 6e 74   everything went
1e3dc 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 70 6c   according to pl
1e3dd 61 6e 2c 20 6c 6f 6f 70 20 74 68 72 6f 75 67 68  an, loop through
1e3de 20 74 68 65 20 63 6f 6c 75 6d 6e 73 0a 20 20 2a   the columns.  *
1e3df 2a 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74  * of the table t
1e3e0 6f 20 73 65 65 20 69 66 20 61 6e 79 20 6f 66 20  o see if any of 
1e3e1 74 68 65 6d 20 63 6f 6e 74 61 69 6e 20 74 68 65  them contain the
1e3e2 20 74 6f 6b 65 6e 20 22 68 69 64 64 65 6e 22 2e   token "hidden".
1e3e3 0a 20 20 2a 2a 20 49 66 20 73 6f 2c 20 73 65 74  .  ** If so, set
1e3e4 20 74 68 65 20 43 6f 6c 75 6d 6e 2e 69 73 48 69   the Column.isHi
1e3e5 64 64 65 6e 20 66 6c 61 67 20 61 6e 64 20 72 65  dden flag and re
1e3e6 6d 6f 76 65 20 74 68 65 20 74 6f 6b 65 6e 20 66  move the token f
1e3e7 72 6f 6d 0a 20 20 2a 2a 20 74 68 65 20 74 79 70  rom.  ** the typ
1e3e8 65 20 73 74 72 69 6e 67 2e 0a 20 20 2a 2f 0a 20  e string..  */. 
1e3e9 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1e3ea 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 43  OK ){.    int iC
1e3eb 6f 6c 3b 0a 20 20 20 20 66 6f 72 28 69 43 6f 6c  ol;.    for(iCol
1e3ec 3d 30 3b 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e  =0; iCol<pTab->n
1e3ed 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20  Col; iCol++){.  
1e3ee 20 20 20 20 63 68 61 72 20 2a 7a 54 79 70 65 20      char *zType 
1e3ef 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f  = pTab->aCol[iCo
1e3f0 6c 5d 2e 7a 54 79 70 65 3b 0a 20 20 20 20 20 20  l].zType;.      
1e3f1 69 6e 74 20 6e 54 79 70 65 3b 0a 20 20 20 20 20  int nType;.     
1e3f2 20 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20 20 20   int i = 0;.    
1e3f3 20 20 69 66 28 20 21 7a 54 79 70 65 20 29 20 63    if( !zType ) c
1e3f4 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 6e  ontinue;.      n
1e3f5 54 79 70 65 20 3d 20 73 74 72 6c 65 6e 28 7a 54  Type = strlen(zT
1e3f6 79 70 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ype);.      if( 
1e3f7 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
1e3f8 22 68 69 64 64 65 6e 22 2c 20 7a 54 79 70 65 2c  "hidden", zType,
1e3f9 20 36 29 20 7c 7c 20 28 7a 54 79 70 65 5b 36 5d   6) || (zType[6]
1e3fa 20 26 26 20 7a 54 79 70 65 5b 36 5d 21 3d 27 20   && zType[6]!=' 
1e3fb 27 29 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6f  ') ){.        fo
1e3fc 72 28 69 3d 30 3b 20 69 3c 6e 54 79 70 65 3b 20  r(i=0; i<nType; 
1e3fd 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  i++){.          
1e3fe 69 66 28 20 28 30 3d 3d 73 71 6c 69 74 65 33 53  if( (0==sqlite3S
1e3ff 74 72 4e 49 43 6d 70 28 22 20 68 69 64 64 65 6e  trNICmp(" hidden
1e400 22 2c 20 26 7a 54 79 70 65 5b 69 5d 2c 20 37 29  ", &zType[i], 7)
1e401 29 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20  ).           && 
1e402 28 7a 54 79 70 65 5b 69 2b 37 5d 3d 3d 27 5c 30  (zType[i+7]=='\0
1e403 27 20 7c 7c 20 7a 54 79 70 65 5b 69 2b 37 5d 3d  ' || zType[i+7]=
1e404 3d 27 20 27 29 0a 20 20 20 20 20 20 20 20 20 20  =' ').          
1e405 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  ){.            i
1e406 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ++;.            
1e407 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
1e408 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
1e409 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69     }.      if( i
1e40a 3c 6e 54 79 70 65 20 29 7b 0a 20 20 20 20 20 20  <nType ){.      
1e40b 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20    int j;.       
1e40c 20 69 6e 74 20 6e 44 65 6c 20 3d 20 36 20 2b 20   int nDel = 6 + 
1e40d 28 7a 54 79 70 65 5b 69 2b 36 5d 20 3f 20 31 20  (zType[i+6] ? 1 
1e40e 3a 20 30 29 3b 0a 20 20 20 20 20 20 20 20 66 6f  : 0);.        fo
1e40f 72 28 6a 3d 69 3b 20 28 6a 2b 6e 44 65 6c 29 3c  r(j=i; (j+nDel)<
1e410 3d 6e 54 79 70 65 3b 20 6a 2b 2b 29 7b 0a 20 20  =nType; j++){.  
1e411 20 20 20 20 20 20 20 20 7a 54 79 70 65 5b 6a 5d          zType[j]
1e412 20 3d 20 7a 54 79 70 65 5b 6a 2b 6e 44 65 6c 5d   = zType[j+nDel]
1e413 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1e414 20 20 20 20 69 66 28 20 7a 54 79 70 65 5b 69 5d      if( zType[i]
1e415 3d 3d 27 5c 30 27 20 26 26 20 69 3e 30 20 29 7b  =='\0' && i>0 ){
1e416 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
1e417 74 28 7a 54 79 70 65 5b 69 2d 31 5d 3d 3d 27 20  t(zType[i-1]==' 
1e418 27 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 54  ');.          zT
1e419 79 70 65 5b 69 2d 31 5d 20 3d 20 27 5c 30 27 3b  ype[i-1] = '\0';
1e41a 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1e41b 20 20 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43     pTab->aCol[iC
1e41c 6f 6c 5d 2e 69 73 48 69 64 64 65 6e 20 3d 20 31  ol].isHidden = 1
1e41d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1e41e 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
1e41f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
1e420 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b  unction is invok
1e421 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72  ed by the parser
1e422 20 74 6f 20 63 61 6c 6c 20 74 68 65 20 78 43 6f   to call the xCo
1e423 6e 6e 65 63 74 28 29 20 6d 65 74 68 6f 64 0a 2a  nnect() method.*
1e424 2a 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c  * of the virtual
1e425 20 74 61 62 6c 65 20 70 54 61 62 2e 20 49 66 20   table pTab. If 
1e426 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
1e427 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69   an error code i
1e428 73 20 72 65 74 75 72 6e 65 64 20 0a 2a 2a 20 61  s returned .** a
1e429 6e 64 20 61 6e 20 65 72 72 6f 72 20 6c 65 66 74  nd an error left
1e42a 20 69 6e 20 70 50 61 72 73 65 2e 0a 2a 2a 0a 2a   in pParse..**.*
1e42b 2a 20 54 68 69 73 20 63 61 6c 6c 20 69 73 20 61  * This call is a
1e42c 20 6e 6f 2d 6f 70 20 69 66 20 74 61 62 6c 65 20   no-op if table 
1e42d 70 54 61 62 20 69 73 20 6e 6f 74 20 61 20 76 69  pTab is not a vi
1e42e 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a  rtual table..*/.
1e42f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1e430 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 61  nt sqlite3VtabCa
1e431 6c 6c 43 6f 6e 6e 65 63 74 28 50 61 72 73 65 20  llConnect(Parse 
1e432 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a  *pParse, Table *
1e433 70 54 61 62 29 7b 0a 20 20 4d 6f 64 75 6c 65 20  pTab){.  Module 
1e434 2a 70 4d 6f 64 3b 0a 20 20 69 6e 74 20 72 63 20  *pMod;.  int rc 
1e435 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20  = SQLITE_OK;..  
1e436 69 66 28 20 21 70 54 61 62 20 7c 7c 20 21 70 54  if( !pTab || !pT
1e437 61 62 2d 3e 69 73 56 69 72 74 75 61 6c 20 7c 7c  ab->isVirtual ||
1e438 20 70 54 61 62 2d 3e 70 56 74 61 62 20 29 7b 0a   pTab->pVtab ){.
1e439 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1e43a 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 70 4d 6f  E_OK;.  }..  pMo
1e43b 64 20 3d 20 70 54 61 62 2d 3e 70 4d 6f 64 3b 0a  d = pTab->pMod;.
1e43c 20 20 69 66 28 20 21 70 4d 6f 64 20 29 7b 0a 20    if( !pMod ){. 
1e43d 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
1e43e 4d 6f 64 75 6c 65 20 3d 20 70 54 61 62 2d 3e 61  Module = pTab->a
1e43f 7a 4d 6f 64 75 6c 65 41 72 67 5b 30 5d 3b 0a 20  zModuleArg[0];. 
1e440 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1e441 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73  sg(pParse, "no s
1e442 75 63 68 20 6d 6f 64 75 6c 65 3a 20 25 73 22 2c  uch module: %s",
1e443 20 7a 4d 6f 64 75 6c 65 29 3b 0a 20 20 20 20 72   zModule);.    r
1e444 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
1e445 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20  ;.  } else {.   
1e446 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 30 3b   char *zErr = 0;
1e447 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62  .    sqlite3 *db
1e448 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
1e449 20 20 20 72 63 20 3d 20 76 74 61 62 43 61 6c 6c     rc = vtabCall
1e44a 43 6f 6e 73 74 72 75 63 74 6f 72 28 64 62 2c 20  Constructor(db, 
1e44b 70 54 61 62 2c 20 70 4d 6f 64 2c 20 70 4d 6f 64  pTab, pMod, pMod
1e44c 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 43 6f 6e 6e  ->pModule->xConn
1e44d 65 63 74 2c 20 26 7a 45 72 72 29 3b 0a 20 20 20  ect, &zErr);.   
1e44e 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1e44f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  OK ){.      sqli
1e450 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1e451 73 65 2c 20 22 25 73 22 2c 20 7a 45 72 72 29 3b  se, "%s", zErr);
1e452 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
1e453 65 46 72 65 65 28 7a 45 72 72 29 3b 0a 20 20 7d  eFree(zErr);.  }
1e454 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
1e455 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20  ../*.** Add the 
1e456 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 70 56  virtual table pV
1e457 74 61 62 20 74 6f 20 74 68 65 20 61 72 72 61 79  tab to the array
1e458 20 73 71 6c 69 74 65 33 2e 61 56 54 72 61 6e 73   sqlite3.aVTrans
1e459 5b 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  []..*/.static in
1e45a 74 20 61 64 64 54 6f 56 54 72 61 6e 73 28 73 71  t addToVTrans(sq
1e45b 6c 69 74 65 33 20 2a 64 62 2c 20 73 71 6c 69 74  lite3 *db, sqlit
1e45c 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 29 7b  e3_vtab *pVtab){
1e45d 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 41 52 52  .  const int ARR
1e45e 41 59 5f 49 4e 43 52 20 3d 20 35 3b 0a 0a 20 20  AY_INCR = 5;..  
1e45f 2f 2a 20 47 72 6f 77 20 74 68 65 20 73 71 6c 69  /* Grow the sqli
1e460 74 65 33 2e 61 56 54 72 61 6e 73 20 61 72 72 61  te3.aVTrans arra
1e461 79 20 69 66 20 72 65 71 75 69 72 65 64 20 2a 2f  y if required */
1e462 0a 20 20 69 66 28 20 28 64 62 2d 3e 6e 56 54 72  .  if( (db->nVTr
1e463 61 6e 73 25 41 52 52 41 59 5f 49 4e 43 52 29 3d  ans%ARRAY_INCR)=
1e464 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
1e465 33 5f 76 74 61 62 20 2a 2a 61 56 54 72 61 6e 73  3_vtab **aVTrans
1e466 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 65 73  ;.    int nBytes
1e467 20 3d 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 65   = sizeof(sqlite
1e468 33 5f 76 74 61 62 20 2a 29 20 2a 20 28 64 62 2d  3_vtab *) * (db-
1e469 3e 6e 56 54 72 61 6e 73 20 2b 20 41 52 52 41 59  >nVTrans + ARRAY
1e46a 5f 49 4e 43 52 29 3b 0a 20 20 20 20 61 56 54 72  _INCR);.    aVTr
1e46b 61 6e 73 20 3d 20 73 71 6c 69 74 65 52 65 61 6c  ans = sqliteReal
1e46c 6c 6f 63 28 28 76 6f 69 64 20 2a 29 64 62 2d 3e  loc((void *)db->
1e46d 61 56 54 72 61 6e 73 2c 20 6e 42 79 74 65 73 29  aVTrans, nBytes)
1e46e 3b 0a 20 20 20 20 69 66 28 20 21 61 56 54 72 61  ;.    if( !aVTra
1e46f 6e 73 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  ns ){.      retu
1e470 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
1e471 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 65  .    }.    memse
1e472 74 28 26 61 56 54 72 61 6e 73 5b 64 62 2d 3e 6e  t(&aVTrans[db->n
1e473 56 54 72 61 6e 73 5d 2c 20 30 2c 20 73 69 7a 65  VTrans], 0, size
1e474 6f 66 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  of(sqlite3_vtab 
1e475 2a 29 2a 41 52 52 41 59 5f 49 4e 43 52 29 3b 0a  *)*ARRAY_INCR);.
1e476 20 20 20 20 64 62 2d 3e 61 56 54 72 61 6e 73 20      db->aVTrans 
1e477 3d 20 61 56 54 72 61 6e 73 3b 0a 20 20 7d 0a 0a  = aVTrans;.  }..
1e478 20 20 2f 2a 20 41 64 64 20 70 56 74 61 62 20 74    /* Add pVtab t
1e479 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 73 71 6c  o the end of sql
1e47a 69 74 65 33 2e 61 56 54 72 61 6e 73 20 2a 2f 0a  ite3.aVTrans */.
1e47b 20 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b 64 62    db->aVTrans[db
1e47c 2d 3e 6e 56 54 72 61 6e 73 2b 2b 5d 20 3d 20 70  ->nVTrans++] = p
1e47d 56 74 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 56  Vtab;.  sqlite3V
1e47e 74 61 62 4c 6f 63 6b 28 70 56 74 61 62 29 3b 0a  tabLock(pVtab);.
1e47f 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1e480 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  OK;.}../*.** Thi
1e481 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e  s function is in
1e482 76 6f 6b 65 64 20 62 79 20 74 68 65 20 76 64 62  voked by the vdb
1e483 65 20 74 6f 20 63 61 6c 6c 20 74 68 65 20 78 43  e to call the xC
1e484 72 65 61 74 65 20 6d 65 74 68 6f 64 0a 2a 2a 20  reate method.** 
1e485 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  of the virtual t
1e486 61 62 6c 65 20 6e 61 6d 65 64 20 7a 54 61 62 20  able named zTab 
1e487 69 6e 20 64 61 74 61 62 61 73 65 20 69 44 62 2e  in database iDb.
1e488 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72   .**.** If an er
1e489 72 6f 72 20 6f 63 63 75 72 73 2c 20 2a 70 7a 45  ror occurs, *pzE
1e48a 72 72 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69  rr is set to poi
1e48b 6e 74 20 61 6e 20 61 6e 20 45 6e 67 6c 69 73 68  nt an an English
1e48c 20 6c 61 6e 67 75 61 67 65 0a 2a 2a 20 64 65 73   language.** des
1e48d 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 65 20  cription of the 
1e48e 65 72 72 6f 72 20 61 6e 64 20 61 6e 20 53 51 4c  error and an SQL
1e48f 49 54 45 5f 58 58 58 20 65 72 72 6f 72 20 63 6f  ITE_XXX error co
1e490 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  de is returned..
1e491 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  ** In this case 
1e492 74 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20  the caller must 
1e493 63 61 6c 6c 20 73 71 6c 69 74 65 46 72 65 65 28  call sqliteFree(
1e494 29 20 6f 6e 20 2a 70 7a 45 72 72 2e 0a 2a 2f 0a  ) on *pzErr..*/.
1e495 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1e496 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 61  nt sqlite3VtabCa
1e497 6c 6c 43 72 65 61 74 65 28 73 71 6c 69 74 65 33  llCreate(sqlite3
1e498 20 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c 20 63   *db, int iDb, c
1e499 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 2c  onst char *zTab,
1e49a 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 29 7b 0a   char **pzErr){.
1e49b 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
1e49c 45 5f 4f 4b 3b 0a 20 20 54 61 62 6c 65 20 2a 70  E_OK;.  Table *p
1e49d 54 61 62 3b 0a 20 20 4d 6f 64 75 6c 65 20 2a 70  Tab;.  Module *p
1e49e 4d 6f 64 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  Mod;.  const cha
1e49f 72 20 2a 7a 4d 6f 64 75 6c 65 3b 0a 0a 20 20 70  r *zModule;..  p
1e4a0 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  Tab = sqlite3Fin
1e4a1 64 54 61 62 6c 65 28 64 62 2c 20 7a 54 61 62 2c  dTable(db, zTab,
1e4a2 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
1e4a3 61 6d 65 29 3b 0a 20 20 61 73 73 65 72 74 28 70  ame);.  assert(p
1e4a4 54 61 62 20 26 26 20 70 54 61 62 2d 3e 69 73 56  Tab && pTab->isV
1e4a5 69 72 74 75 61 6c 20 26 26 20 21 70 54 61 62 2d  irtual && !pTab-
1e4a6 3e 70 56 74 61 62 29 3b 0a 20 20 70 4d 6f 64 20  >pVtab);.  pMod 
1e4a7 3d 20 70 54 61 62 2d 3e 70 4d 6f 64 3b 0a 20 20  = pTab->pMod;.  
1e4a8 7a 4d 6f 64 75 6c 65 20 3d 20 70 54 61 62 2d 3e  zModule = pTab->
1e4a9 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 30 5d 3b 0a  azModuleArg[0];.
1e4aa 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6d 6f 64  .  /* If the mod
1e4ab 75 6c 65 20 68 61 73 20 62 65 65 6e 20 72 65 67  ule has been reg
1e4ac 69 73 74 65 72 65 64 20 61 6e 64 20 69 6e 63 6c  istered and incl
1e4ad 75 64 65 73 20 61 20 43 72 65 61 74 65 20 6d 65  udes a Create me
1e4ae 74 68 6f 64 2c 20 0a 20 20 2a 2a 20 69 6e 76 6f  thod, .  ** invo
1e4af 6b 65 20 69 74 20 6e 6f 77 2e 20 49 66 20 74 68  ke it now. If th
1e4b0 65 20 6d 6f 64 75 6c 65 20 68 61 73 20 6e 6f 74  e module has not
1e4b1 20 62 65 65 6e 20 72 65 67 69 73 74 65 72 65 64   been registered
1e4b2 2c 20 72 65 74 75 72 6e 20 61 6e 20 0a 20 20 2a  , return an .  *
1e4b3 2a 20 65 72 72 6f 72 2e 20 4f 74 68 65 72 77 69  * error. Otherwi
1e4b4 73 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 0a  se, do nothing..
1e4b5 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 4d 6f 64    */.  if( !pMod
1e4b6 20 29 7b 0a 20 20 20 20 2a 70 7a 45 72 72 20 3d   ){.    *pzErr =
1e4b7 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
1e4b8 22 6e 6f 20 73 75 63 68 20 6d 6f 64 75 6c 65 3a  "no such module:
1e4b9 20 25 73 22 2c 20 7a 4d 6f 64 75 6c 65 29 3b 0a   %s", zModule);.
1e4ba 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1e4bb 45 52 52 4f 52 3b 0a 20 20 7d 65 6c 73 65 7b 0a  ERROR;.  }else{.
1e4bc 20 20 20 20 72 63 20 3d 20 76 74 61 62 43 61 6c      rc = vtabCal
1e4bd 6c 43 6f 6e 73 74 72 75 63 74 6f 72 28 64 62 2c  lConstructor(db,
1e4be 20 70 54 61 62 2c 20 70 4d 6f 64 2c 20 70 4d 6f   pTab, pMod, pMo
1e4bf 64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 43 72 65  d->pModule->xCre
1e4c0 61 74 65 2c 20 70 7a 45 72 72 29 3b 0a 20 20 7d  ate, pzErr);.  }
1e4c1 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
1e4c2 54 45 5f 4f 4b 20 26 26 20 70 54 61 62 2d 3e 70  TE_OK && pTab->p
1e4c3 56 74 61 62 20 29 7b 0a 20 20 20 20 20 20 72 63  Vtab ){.      rc
1e4c4 20 3d 20 61 64 64 54 6f 56 54 72 61 6e 73 28 64   = addToVTrans(d
1e4c5 62 2c 20 70 54 61 62 2d 3e 70 56 74 61 62 29 3b  b, pTab->pVtab);
1e4c6 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
1e4c7 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  c;.}../*.** This
1e4c8 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
1e4c9 64 20 74 6f 20 73 65 74 20 74 68 65 20 73 63 68  d to set the sch
1e4ca 65 6d 61 20 6f 66 20 61 20 76 69 72 74 75 61 6c  ema of a virtual
1e4cb 20 74 61 62 6c 65 2e 20 20 49 74 20 69 73 20 6f   table.  It is o
1e4cc 6e 6c 79 0a 2a 2a 20 76 61 6c 69 64 20 74 6f 20  nly.** valid to 
1e4cd 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69  call this functi
1e4ce 6f 6e 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74  on from within t
1e4cf 68 65 20 78 43 72 65 61 74 65 28 29 20 6f 72 20  he xCreate() or 
1e4d0 78 43 6f 6e 6e 65 63 74 28 29 20 6f 66 20 61 0a  xConnect() of a.
1e4d1 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ** virtual table
1e4d2 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 69 6e 74 20   module..*/.int 
1e4d3 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f  sqlite3_declare_
1e4d4 76 74 61 62 28 73 71 6c 69 74 65 33 20 2a 64 62  vtab(sqlite3 *db
1e4d5 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43  , const char *zC
1e4d6 72 65 61 74 65 54 61 62 6c 65 29 7b 0a 20 20 50  reateTable){.  P
1e4d7 61 72 73 65 20 73 50 61 72 73 65 3b 0a 0a 20 20  arse sParse;..  
1e4d8 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
1e4d9 4f 4b 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61  OK;.  Table *pTa
1e4da 62 20 3d 20 64 62 2d 3e 70 56 54 61 62 3b 0a 20  b = db->pVTab;. 
1e4db 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 30 3b   char *zErr = 0;
1e4dc 0a 0a 20 20 69 66 28 20 21 70 54 61 62 20 29 7b  ..  if( !pTab ){
1e4dd 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
1e4de 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4d 49 53  r(db, SQLITE_MIS
1e4df 55 53 45 2c 20 30 29 3b 0a 20 20 20 20 72 65 74  USE, 0);.    ret
1e4e0 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  urn SQLITE_MISUS
1e4e1 45 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  E;.  }.  assert(
1e4e2 70 54 61 62 2d 3e 69 73 56 69 72 74 75 61 6c 20  pTab->isVirtual 
1e4e3 26 26 20 70 54 61 62 2d 3e 6e 43 6f 6c 3d 3d 30  && pTab->nCol==0
1e4e4 20 26 26 20 70 54 61 62 2d 3e 61 43 6f 6c 3d 3d   && pTab->aCol==
1e4e5 30 29 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 26 73  0);..  memset(&s
1e4e6 50 61 72 73 65 2c 20 30 2c 20 73 69 7a 65 6f 66  Parse, 0, sizeof
1e4e7 28 50 61 72 73 65 29 29 3b 0a 20 20 73 50 61 72  (Parse));.  sPar
1e4e8 73 65 2e 64 65 63 6c 61 72 65 56 74 61 62 20 3d  se.declareVtab =
1e4e9 20 31 3b 0a 20 20 73 50 61 72 73 65 2e 64 62 20   1;.  sParse.db 
1e4ea 3d 20 64 62 3b 0a 0a 20 20 69 66 28 20 0a 20 20  = db;..  if( .  
1e4eb 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 20 3d 3d      SQLITE_OK ==
1e4ec 20 73 71 6c 69 74 65 33 52 75 6e 50 61 72 73 65   sqlite3RunParse
1e4ed 72 28 26 73 50 61 72 73 65 2c 20 7a 43 72 65 61  r(&sParse, zCrea
1e4ee 74 65 54 61 62 6c 65 2c 20 26 7a 45 72 72 29 20  teTable, &zErr) 
1e4ef 26 26 20 0a 20 20 20 20 20 20 73 50 61 72 73 65  && .      sParse
1e4f0 2e 70 4e 65 77 54 61 62 6c 65 20 26 26 20 0a 20  .pNewTable && . 
1e4f1 20 20 20 20 20 21 73 50 61 72 73 65 2e 70 4e 65       !sParse.pNe
1e4f2 77 54 61 62 6c 65 2d 3e 70 53 65 6c 65 63 74 20  wTable->pSelect 
1e4f3 26 26 20 0a 20 20 20 20 20 20 21 73 50 61 72 73  && .      !sPars
1e4f4 65 2e 70 4e 65 77 54 61 62 6c 65 2d 3e 69 73 56  e.pNewTable->isV
1e4f5 69 72 74 75 61 6c 20 0a 20 20 29 7b 0a 20 20 20  irtual .  ){.   
1e4f6 20 70 54 61 62 2d 3e 61 43 6f 6c 20 3d 20 73 50   pTab->aCol = sP
1e4f7 61 72 73 65 2e 70 4e 65 77 54 61 62 6c 65 2d 3e  arse.pNewTable->
1e4f8 61 43 6f 6c 3b 0a 20 20 20 20 70 54 61 62 2d 3e  aCol;.    pTab->
1e4f9 6e 43 6f 6c 20 3d 20 73 50 61 72 73 65 2e 70 4e  nCol = sParse.pN
1e4fa 65 77 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3b 0a 20  ewTable->nCol;. 
1e4fb 20 20 20 73 50 61 72 73 65 2e 70 4e 65 77 54 61     sParse.pNewTa
1e4fc 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 20  ble->nCol = 0;. 
1e4fd 20 20 20 73 50 61 72 73 65 2e 70 4e 65 77 54 61     sParse.pNewTa
1e4fe 62 6c 65 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20  ble->aCol = 0;. 
1e4ff 20 20 20 64 62 2d 3e 70 56 54 61 62 20 3d 20 30     db->pVTab = 0
1e500 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20  ;.  } else {.   
1e501 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62   sqlite3Error(db
1e502 2c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20  , SQLITE_ERROR, 
1e503 7a 45 72 72 29 3b 0a 20 20 20 20 73 71 6c 69 74  zErr);.    sqlit
1e504 65 46 72 65 65 28 7a 45 72 72 29 3b 0a 20 20 20  eFree(zErr);.   
1e505 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
1e506 4f 52 3b 0a 20 20 7d 0a 20 20 73 50 61 72 73 65  OR;.  }.  sParse
1e507 2e 64 65 63 6c 61 72 65 56 74 61 62 20 3d 20 30  .declareVtab = 0
1e508 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6e  ;..  sqlite3_fin
1e509 61 6c 69 7a 65 28 28 73 71 6c 69 74 65 33 5f 73  alize((sqlite3_s
1e50a 74 6d 74 2a 29 73 50 61 72 73 65 2e 70 56 64 62  tmt*)sParse.pVdb
1e50b 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 6c  e);.  sqlite3Del
1e50c 65 74 65 54 61 62 6c 65 28 73 50 61 72 73 65 2e  eteTable(sParse.
1e50d 70 4e 65 77 54 61 62 6c 65 29 3b 0a 20 20 73 50  pNewTable);.  sP
1e50e 61 72 73 65 2e 70 4e 65 77 54 61 62 6c 65 20 3d  arse.pNewTable =
1e50f 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 28   0;..  assert( (
1e510 72 63 26 30 78 66 66 29 3d 3d 72 63 20 29 3b 0a  rc&0xff)==rc );.
1e511 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
1e512 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b  ApiExit(db, rc);
1e513 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
1e514 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b  unction is invok
1e515 65 64 20 62 79 20 74 68 65 20 76 64 62 65 20 74  ed by the vdbe t
1e516 6f 20 63 61 6c 6c 20 74 68 65 20 78 44 65 73 74  o call the xDest
1e517 72 6f 79 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66  roy method.** of
1e518 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
1e519 6c 65 20 6e 61 6d 65 64 20 7a 54 61 62 20 69 6e  le named zTab in
1e51a 20 64 61 74 61 62 61 73 65 20 69 44 62 2e 20 54   database iDb. T
1e51b 68 69 73 20 6f 63 63 75 72 73 0a 2a 2a 20 77 68  his occurs.** wh
1e51c 65 6e 20 61 20 44 52 4f 50 20 54 41 42 4c 45 20  en a DROP TABLE 
1e51d 69 73 20 6d 65 6e 74 69 6f 6e 65 64 2e 0a 2a 2a  is mentioned..**
1e51e 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 69 73  .** This call is
1e51f 20 61 20 6e 6f 2d 6f 70 20 69 66 20 7a 54 61 62   a no-op if zTab
1e520 20 69 73 20 6e 6f 74 20 61 20 76 69 72 74 75 61   is not a virtua
1e521 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49  l table..*/.SQLI
1e522 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1e523 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 44 65  qlite3VtabCallDe
1e524 73 74 72 6f 79 28 73 71 6c 69 74 65 33 20 2a 64  stroy(sqlite3 *d
1e525 62 2c 20 69 6e 74 20 69 44 62 2c 20 63 6f 6e 73  b, int iDb, cons
1e526 74 20 63 68 61 72 20 2a 7a 54 61 62 29 0a 7b 0a  t char *zTab).{.
1e527 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
1e528 45 5f 4f 4b 3b 0a 20 20 54 61 62 6c 65 20 2a 70  E_OK;.  Table *p
1e529 54 61 62 3b 0a 0a 20 20 70 54 61 62 20 3d 20 73  Tab;..  pTab = s
1e52a 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28  qlite3FindTable(
1e52b 64 62 2c 20 7a 54 61 62 2c 20 64 62 2d 3e 61 44  db, zTab, db->aD
1e52c 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20  b[iDb].zName);. 
1e52d 20 61 73 73 65 72 74 28 70 54 61 62 29 3b 0a 20   assert(pTab);. 
1e52e 20 69 66 28 20 70 54 61 62 2d 3e 70 56 74 61 62   if( pTab->pVtab
1e52f 20 29 7b 0a 20 20 20 20 69 6e 74 20 28 2a 78 44   ){.    int (*xD
1e530 65 73 74 72 6f 79 29 28 73 71 6c 69 74 65 33 5f  estroy)(sqlite3_
1e531 76 74 61 62 20 2a 70 56 54 61 62 29 20 3d 20 70  vtab *pVTab) = p
1e532 54 61 62 2d 3e 70 4d 6f 64 2d 3e 70 4d 6f 64 75  Tab->pMod->pModu
1e533 6c 65 2d 3e 78 44 65 73 74 72 6f 79 3b 0a 20 20  le->xDestroy;.  
1e534 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53 61    rc = sqlite3Sa
1e535 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20  fetyOff(db);.   
1e536 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
1e537 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 69 66  ITE_OK );.    if
1e538 28 20 78 44 65 73 74 72 6f 79 20 29 7b 0a 20 20  ( xDestroy ){.  
1e539 20 20 20 20 72 63 20 3d 20 78 44 65 73 74 72 6f      rc = xDestro
1e53a 79 28 70 54 61 62 2d 3e 70 56 74 61 62 29 3b 0a  y(pTab->pVtab);.
1e53b 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
1e53c 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20  3SafetyOn(db);. 
1e53d 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
1e53e 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 54  E_OK ){.      pT
1e53f 61 62 2d 3e 70 56 74 61 62 20 3d 20 30 3b 0a 20  ab->pVtab = 0;. 
1e540 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
1e541 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
1e542 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e  This function in
1e543 76 6f 6b 65 73 20 65 69 74 68 65 72 20 74 68 65  vokes either the
1e544 20 78 52 6f 6c 6c 62 61 63 6b 20 6f 72 20 78 43   xRollback or xC
1e545 6f 6d 6d 69 74 20 6d 65 74 68 6f 64 0a 2a 2a 20  ommit method.** 
1e546 6f 66 20 65 61 63 68 20 6f 66 20 74 68 65 20 76  of each of the v
1e547 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 69 6e  irtual tables in
1e548 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61 56 54   the sqlite3.aVT
1e549 72 61 6e 73 20 61 72 72 61 79 2e 20 54 68 65 20  rans array. The 
1e54a 6d 65 74 68 6f 64 0a 2a 2a 20 63 61 6c 6c 65 64  method.** called
1e54b 20 69 73 20 69 64 65 6e 74 69 66 69 65 64 20 62   is identified b
1e54c 79 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  y the second arg
1e54d 75 6d 65 6e 74 2c 20 22 6f 66 66 73 65 74 22 2c  ument, "offset",
1e54e 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 74 68 65   which is.** the
1e54f 20 6f 66 66 73 65 74 20 6f 66 20 74 68 65 20 6d   offset of the m
1e550 65 74 68 6f 64 20 74 6f 20 63 61 6c 6c 20 69 6e  ethod to call in
1e551 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6d 6f 64   the sqlite3_mod
1e552 75 6c 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  ule structure..*
1e553 2a 0a 2a 2a 20 54 68 65 20 61 72 72 61 79 20 69  *.** The array i
1e554 73 20 63 6c 65 61 72 65 64 20 61 66 74 65 72 20  s cleared after 
1e555 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 63 61 6c  invoking the cal
1e556 6c 62 61 63 6b 73 2e 20 0a 2a 2f 0a 73 74 61 74  lbacks. .*/.stat
1e557 69 63 20 76 6f 69 64 20 63 61 6c 6c 46 69 6e 61  ic void callFina
1e558 6c 69 73 65 72 28 73 71 6c 69 74 65 33 20 2a 64  liser(sqlite3 *d
1e559 62 2c 20 69 6e 74 20 6f 66 66 73 65 74 29 7b 0a  b, int offset){.
1e55a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 64    int i;.  if( d
1e55b 62 2d 3e 61 56 54 72 61 6e 73 20 29 7b 0a 20 20  b->aVTrans ){.  
1e55c 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
1e55d 3e 6e 56 54 72 61 6e 73 20 26 26 20 64 62 2d 3e  >nVTrans && db->
1e55e 61 56 54 72 61 6e 73 5b 69 5d 3b 20 69 2b 2b 29  aVTrans[i]; i++)
1e55f 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
1e560 76 74 61 62 20 2a 70 56 74 61 62 20 3d 20 64 62  vtab *pVtab = db
1e561 2d 3e 61 56 54 72 61 6e 73 5b 69 5d 3b 0a 20 20  ->aVTrans[i];.  
1e562 20 20 20 20 69 6e 74 20 28 2a 78 29 28 73 71 6c      int (*x)(sql
1e563 69 74 65 33 5f 76 74 61 62 20 2a 29 3b 0a 20 20  ite3_vtab *);.  
1e564 20 20 20 20 78 20 3d 20 2a 28 69 6e 74 20 28 2a      x = *(int (*
1e565 2a 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  *)(sqlite3_vtab 
1e566 2a 29 29 28 28 63 68 61 72 20 2a 29 70 56 74 61  *))((char *)pVta
1e567 62 2d 3e 70 4d 6f 64 75 6c 65 20 2b 20 6f 66 66  b->pModule + off
1e568 73 65 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20  set);.      if( 
1e569 78 20 29 20 78 28 70 56 74 61 62 29 3b 0a 20 20  x ) x(pVtab);.  
1e56a 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 55      sqlite3VtabU
1e56b 6e 6c 6f 63 6b 28 64 62 2c 20 70 56 74 61 62 29  nlock(db, pVtab)
1e56c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
1e56d 74 65 46 72 65 65 28 64 62 2d 3e 61 56 54 72 61  teFree(db->aVTra
1e56e 6e 73 29 3b 0a 20 20 20 20 64 62 2d 3e 6e 56 54  ns);.    db->nVT
1e56f 72 61 6e 73 20 3d 20 30 3b 0a 20 20 20 20 64 62  rans = 0;.    db
1e570 2d 3e 61 56 54 72 61 6e 73 20 3d 20 30 3b 0a 20  ->aVTrans = 0;. 
1e571 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 61   }.}../*.** If a
1e572 72 67 75 6d 65 6e 74 20 72 63 32 20 69 73 20 6e  rgument rc2 is n
1e573 6f 74 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 74 68  ot SQLITE_OK, th
1e574 65 6e 20 72 65 74 75 72 6e 20 69 74 20 61 6e 64  en return it and
1e575 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 0a 2a 2a   do nothing. .**
1e576 20 4f 74 68 65 72 77 69 73 65 2c 20 69 6e 76 6f   Otherwise, invo
1e577 6b 65 20 74 68 65 20 78 53 79 6e 63 20 6d 65 74  ke the xSync met
1e578 68 6f 64 20 6f 66 20 61 6c 6c 20 76 69 72 74 75  hod of all virtu
1e579 61 6c 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65  al tables in the
1e57a 20 0a 2a 2a 20 73 71 6c 69 74 65 33 2e 61 56 54   .** sqlite3.aVT
1e57b 72 61 6e 73 20 61 72 72 61 79 2e 20 52 65 74 75  rans array. Retu
1e57c 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  rn the error cod
1e57d 65 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20  e for the first 
1e57e 65 72 72 6f 72 20 0a 2a 2a 20 74 68 61 74 20 6f  error .** that o
1e57f 63 63 75 72 73 2c 20 6f 72 20 53 51 4c 49 54 45  ccurs, or SQLITE
1e580 5f 4f 4b 20 69 66 20 61 6c 6c 20 78 53 79 6e 63  _OK if all xSync
1e581 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65 20   operations are 
1e582 73 75 63 63 65 73 73 66 75 6c 2e 0a 2a 2f 0a 53  successful..*/.S
1e583 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1e584 74 20 73 71 6c 69 74 65 33 56 74 61 62 53 79 6e  t sqlite3VtabSyn
1e585 63 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69  c(sqlite3 *db, i
1e586 6e 74 20 72 63 32 29 7b 0a 20 20 69 6e 74 20 69  nt rc2){.  int i
1e587 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
1e588 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 63  ITE_OK;.  int rc
1e589 73 61 66 65 74 79 3b 0a 20 20 73 71 6c 69 74 65  safety;.  sqlite
1e58a 33 5f 76 74 61 62 20 2a 2a 61 56 54 72 61 6e 73  3_vtab **aVTrans
1e58b 20 3d 20 64 62 2d 3e 61 56 54 72 61 6e 73 3b 0a   = db->aVTrans;.
1e58c 20 20 69 66 28 20 72 63 32 21 3d 53 51 4c 49 54    if( rc2!=SQLIT
1e58d 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
1e58e 32 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  2;..  rc = sqlit
1e58f 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b  e3SafetyOff(db);
1e590 0a 20 20 64 62 2d 3e 61 56 54 72 61 6e 73 20 3d  .  db->aVTrans =
1e591 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 72   0;.  for(i=0; r
1e592 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
1e593 69 3c 64 62 2d 3e 6e 56 54 72 61 6e 73 20 26 26  i<db->nVTrans &&
1e594 20 61 56 54 72 61 6e 73 5b 69 5d 3b 20 69 2b 2b   aVTrans[i]; i++
1e595 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76  ){.    sqlite3_v
1e596 74 61 62 20 2a 70 56 74 61 62 20 3d 20 61 56 54  tab *pVtab = aVT
1e597 72 61 6e 73 5b 69 5d 3b 0a 20 20 20 20 69 6e 74  rans[i];.    int
1e598 20 28 2a 78 29 28 73 71 6c 69 74 65 33 5f 76 74   (*x)(sqlite3_vt
1e599 61 62 20 2a 29 3b 0a 20 20 20 20 78 20 3d 20 70  ab *);.    x = p
1e59a 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78  Vtab->pModule->x
1e59b 53 79 6e 63 3b 0a 20 20 20 20 69 66 28 20 78 20  Sync;.    if( x 
1e59c 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 78 28  ){.      rc = x(
1e59d 70 56 74 61 62 29 3b 0a 20 20 20 20 7d 0a 20 20  pVtab);.    }.  
1e59e 7d 0a 20 20 64 62 2d 3e 61 56 54 72 61 6e 73 20  }.  db->aVTrans 
1e59f 3d 20 61 56 54 72 61 6e 73 3b 0a 20 20 72 63 73  = aVTrans;.  rcs
1e5a0 61 66 65 74 79 20 3d 20 73 71 6c 69 74 65 33 53  afety = sqlite3S
1e5a1 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 0a 20 20  afetyOn(db);..  
1e5a2 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1e5a3 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 72 63  K ){.    rc = rc
1e5a4 73 61 66 65 74 79 3b 0a 20 20 7d 0a 20 20 72 65  safety;.  }.  re
1e5a5 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
1e5a6 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 78 52 6f  * Invoke the xRo
1e5a7 6c 6c 62 61 63 6b 20 6d 65 74 68 6f 64 20 6f 66  llback method of
1e5a8 20 61 6c 6c 20 76 69 72 74 75 61 6c 20 74 61 62   all virtual tab
1e5a9 6c 65 73 20 69 6e 20 74 68 65 20 0a 2a 2a 20 73  les in the .** s
1e5aa 71 6c 69 74 65 33 2e 61 56 54 72 61 6e 73 20 61  qlite3.aVTrans a
1e5ab 72 72 61 79 2e 20 54 68 65 6e 20 63 6c 65 61 72  rray. Then clear
1e5ac 20 74 68 65 20 61 72 72 61 79 20 69 74 73 65 6c   the array itsel
1e5ad 66 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  f..*/.SQLITE_PRI
1e5ae 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1e5af 56 74 61 62 52 6f 6c 6c 62 61 63 6b 28 73 71 6c  VtabRollback(sql
1e5b0 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 63 61 6c  ite3 *db){.  cal
1e5b1 6c 46 69 6e 61 6c 69 73 65 72 28 64 62 2c 20 28  lFinaliser(db, (
1e5b2 69 6e 74 29 28 26 28 28 73 71 6c 69 74 65 33 5f  int)(&((sqlite3_
1e5b3 6d 6f 64 75 6c 65 20 2a 29 30 29 2d 3e 78 52 6f  module *)0)->xRo
1e5b4 6c 6c 62 61 63 6b 29 29 3b 0a 20 20 72 65 74 75  llback));.  retu
1e5b5 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
1e5b6 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68  ./*.** Invoke th
1e5b7 65 20 78 43 6f 6d 6d 69 74 20 6d 65 74 68 6f 64  e xCommit method
1e5b8 20 6f 66 20 61 6c 6c 20 76 69 72 74 75 61 6c 20   of all virtual 
1e5b9 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 0a 2a  tables in the .*
1e5ba 2a 20 73 71 6c 69 74 65 33 2e 61 56 54 72 61 6e  * sqlite3.aVTran
1e5bb 73 20 61 72 72 61 79 2e 20 54 68 65 6e 20 63 6c  s array. Then cl
1e5bc 65 61 72 20 74 68 65 20 61 72 72 61 79 20 69 74  ear the array it
1e5bd 73 65 6c 66 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  self..*/.SQLITE_
1e5be 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1e5bf 74 65 33 56 74 61 62 43 6f 6d 6d 69 74 28 73 71  te3VtabCommit(sq
1e5c0 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 63 61  lite3 *db){.  ca
1e5c1 6c 6c 46 69 6e 61 6c 69 73 65 72 28 64 62 2c 20  llFinaliser(db, 
1e5c2 28 69 6e 74 29 28 26 28 28 73 71 6c 69 74 65 33  (int)(&((sqlite3
1e5c3 5f 6d 6f 64 75 6c 65 20 2a 29 30 29 2d 3e 78 43  _module *)0)->xC
1e5c4 6f 6d 6d 69 74 29 29 3b 0a 20 20 72 65 74 75 72  ommit));.  retur
1e5c5 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
1e5c6 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 76 69 72  /*.** If the vir
1e5c7 74 75 61 6c 20 74 61 62 6c 65 20 70 56 74 61 62  tual table pVtab
1e5c8 20 73 75 70 70 6f 72 74 73 20 74 68 65 20 74 72   supports the tr
1e5c9 61 6e 73 61 63 74 69 6f 6e 20 69 6e 74 65 72 66  ansaction interf
1e5ca 61 63 65 0a 2a 2a 20 28 78 42 65 67 69 6e 2f 78  ace.** (xBegin/x
1e5cb 52 6f 6c 6c 62 61 63 6b 2f 78 43 6f 6d 6d 69 74  Rollback/xCommit
1e5cc 20 61 6e 64 20 6f 70 74 69 6f 6e 61 6c 6c 79 20   and optionally 
1e5cd 78 53 79 6e 63 29 20 61 6e 64 20 61 20 74 72 61  xSync) and a tra
1e5ce 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 6e  nsaction is.** n
1e5cf 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 65  ot currently ope
1e5d0 6e 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 78 42  n, invoke the xB
1e5d1 65 67 69 6e 20 6d 65 74 68 6f 64 20 6e 6f 77 2e  egin method now.
1e5d2 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 78 42  .**.** If the xB
1e5d3 65 67 69 6e 20 63 61 6c 6c 20 69 73 20 73 75 63  egin call is suc
1e5d4 63 65 73 73 66 75 6c 2c 20 70 6c 61 63 65 20 74  cessful, place t
1e5d5 68 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  he sqlite3_vtab 
1e5d6 70 6f 69 6e 74 65 72 0a 2a 2a 20 69 6e 20 74 68  pointer.** in th
1e5d7 65 20 73 71 6c 69 74 65 33 2e 61 56 54 72 61 6e  e sqlite3.aVTran
1e5d8 73 20 61 72 72 61 79 2e 0a 2a 2f 0a 53 51 4c 49  s array..*/.SQLI
1e5d9 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1e5da 71 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e 28  qlite3VtabBegin(
1e5db 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 73 71 6c  sqlite3 *db, sql
1e5dc 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
1e5dd 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
1e5de 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63 6f 6e 73 74  LITE_OK;.  const
1e5df 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
1e5e0 2a 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20 2f 2a 20  *pModule;..  /* 
1e5e1 53 70 65 63 69 61 6c 20 63 61 73 65 3a 20 49 66  Special case: If
1e5e2 20 64 62 2d 3e 61 56 54 72 61 6e 73 20 69 73 20   db->aVTrans is 
1e5e3 4e 55 4c 4c 20 61 6e 64 20 64 62 2d 3e 6e 56 54  NULL and db->nVT
1e5e4 72 61 6e 73 20 69 73 20 67 72 65 61 74 65 72 0a  rans is greater.
1e5e5 20 20 2a 2a 20 74 68 61 6e 20 7a 65 72 6f 2c 20    ** than zero, 
1e5e6 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  then this functi
1e5e7 6f 6e 20 69 73 20 62 65 69 6e 67 20 63 61 6c 6c  on is being call
1e5e8 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61  ed from within a
1e5e9 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d 6f  .  ** virtual mo
1e5ea 64 75 6c 65 20 78 53 79 6e 63 28 29 20 63 61 6c  dule xSync() cal
1e5eb 6c 62 61 63 6b 2e 20 49 74 20 69 73 20 69 6c 6c  lback. It is ill
1e5ec 65 67 61 6c 20 74 6f 20 77 72 69 74 65 20 74 6f  egal to write to
1e5ed 20 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d   .  ** virtual m
1e5ee 6f 64 75 6c 65 20 74 61 62 6c 65 73 20 69 6e 20  odule tables in 
1e5ef 74 68 69 73 20 63 61 73 65 2c 20 73 6f 20 72 65  this case, so re
1e5f0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  turn SQLITE_LOCK
1e5f1 45 44 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 30  ED..  */.  if( 0
1e5f2 3d 3d 64 62 2d 3e 61 56 54 72 61 6e 73 20 26 26  ==db->aVTrans &&
1e5f3 20 64 62 2d 3e 6e 56 54 72 61 6e 73 3e 30 20 29   db->nVTrans>0 )
1e5f4 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
1e5f5 49 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 7d 0a  ITE_LOCKED;.  }.
1e5f6 20 20 69 66 28 20 21 70 56 74 61 62 20 29 7b 0a    if( !pVtab ){.
1e5f7 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1e5f8 45 5f 4f 4b 3b 0a 20 20 7d 20 0a 20 20 70 4d 6f  E_OK;.  } .  pMo
1e5f9 64 75 6c 65 20 3d 20 70 56 74 61 62 2d 3e 70 4d  dule = pVtab->pM
1e5fa 6f 64 75 6c 65 3b 0a 0a 20 20 69 66 28 20 70 4d  odule;..  if( pM
1e5fb 6f 64 75 6c 65 2d 3e 78 42 65 67 69 6e 20 29 7b  odule->xBegin ){
1e5fc 0a 20 20 20 20 69 6e 74 20 69 3b 0a 0a 0a 20 20  .    int i;...  
1e5fd 20 20 2f 2a 20 49 66 20 70 56 74 61 62 20 69 73    /* If pVtab is
1e5fe 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20   already in the 
1e5ff 61 56 54 72 61 6e 73 20 61 72 72 61 79 2c 20 72  aVTrans array, r
1e600 65 74 75 72 6e 20 65 61 72 6c 79 20 2a 2f 0a 20  eturn early */. 
1e601 20 20 20 66 6f 72 28 69 3d 30 3b 20 28 69 3c 64     for(i=0; (i<d
1e602 62 2d 3e 6e 56 54 72 61 6e 73 29 20 26 26 20 30  b->nVTrans) && 0
1e603 21 3d 64 62 2d 3e 61 56 54 72 61 6e 73 5b 69 5d  !=db->aVTrans[i]
1e604 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; i++){.      if
1e605 28 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b 69 5d  ( db->aVTrans[i]
1e606 3d 3d 70 56 74 61 62 20 29 7b 0a 20 20 20 20 20  ==pVtab ){.     
1e607 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1e608 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  _OK;.      }.   
1e609 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 6e 76 6f 6b   }..    /* Invok
1e60a 65 20 74 68 65 20 78 42 65 67 69 6e 20 6d 65 74  e the xBegin met
1e60b 68 6f 64 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  hod */.    rc = 
1e60c 70 4d 6f 64 75 6c 65 2d 3e 78 42 65 67 69 6e 28  pModule->xBegin(
1e60d 70 56 74 61 62 29 3b 0a 20 20 20 20 69 66 28 20  pVtab);.    if( 
1e60e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1e60f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
1e610 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72 63 20  ;.    }..    rc 
1e611 3d 20 61 64 64 54 6f 56 54 72 61 6e 73 28 64 62  = addToVTrans(db
1e612 2c 20 70 56 74 61 62 29 3b 0a 20 20 7d 0a 20 20  , pVtab);.  }.  
1e613 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1e614 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61  .** The first pa
1e615 72 61 6d 65 74 65 72 20 28 70 44 65 66 29 20 69  rameter (pDef) i
1e616 73 20 61 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70  s a function imp
1e617 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68  lementation.  Th
1e618 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 61 72 61  e.** second para
1e619 6d 65 74 65 72 20 28 70 45 78 70 72 29 20 69 73  meter (pExpr) is
1e61a 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
1e61b 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63  ent to this func
1e61c 74 69 6f 6e 2e 0a 2a 2a 20 49 66 20 70 45 78 70  tion..** If pExp
1e61d 72 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 69 6e  r is a column in
1e61e 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
1e61f 2c 20 74 68 65 6e 20 6c 65 74 20 74 68 65 20 76  , then let the v
1e620 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20  irtual.** table 
1e621 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 68  implementation h
1e622 61 76 65 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69  ave an opportuni
1e623 74 79 20 74 6f 20 6f 76 65 72 6c 6f 61 64 20 74  ty to overload t
1e624 68 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a  he function..**.
1e625 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
1e626 69 73 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 77  is used to allow
1e627 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69   virtual table i
1e628 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 74  mplementations t
1e629 6f 0a 2a 2a 20 6f 76 65 72 6c 6f 61 64 20 4d 41  o.** overload MA
1e62a 54 43 48 2c 20 4c 49 4b 45 2c 20 47 4c 4f 42 2c  TCH, LIKE, GLOB,
1e62b 20 61 6e 64 20 52 45 47 45 58 50 20 6f 70 65 72   and REGEXP oper
1e62c 61 74 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74  ators..**.** Ret
1e62d 75 72 6e 20 65 69 74 68 65 72 20 74 68 65 20 70  urn either the p
1e62e 44 65 66 20 61 72 67 75 6d 65 6e 74 20 28 69 6e  Def argument (in
1e62f 64 69 63 61 74 69 6e 67 20 6e 6f 20 63 68 61 6e  dicating no chan
1e630 67 65 29 20 6f 72 20 61 20 0a 2a 2a 20 6e 65 77  ge) or a .** new
1e631 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 75   FuncDef structu
1e632 72 65 20 74 68 61 74 20 69 73 20 6d 61 72 6b 65  re that is marke
1e633 64 20 61 73 20 65 70 68 65 6d 65 72 61 6c 20 75  d as ephemeral u
1e634 73 69 6e 67 20 74 68 65 0a 2a 2a 20 53 51 4c 49  sing the.** SQLI
1e635 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d 20 66 6c  TE_FUNC_EPHEM fl
1e636 61 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ag..*/.SQLITE_PR
1e637 49 56 41 54 45 20 46 75 6e 63 44 65 66 20 2a 73  IVATE FuncDef *s
1e638 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72 6c 6f  qlite3VtabOverlo
1e639 61 64 46 75 6e 63 74 69 6f 6e 28 0a 20 20 46 75  adFunction(.  Fu
1e63a 6e 63 44 65 66 20 2a 70 44 65 66 2c 20 20 2f 2a  ncDef *pDef,  /*
1e63b 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 70 6f 73   Function to pos
1e63c 73 69 62 6c 79 20 6f 76 65 72 6c 6f 61 64 20 2a  sibly overload *
1e63d 2f 0a 20 20 69 6e 74 20 6e 41 72 67 2c 20 20 20  /.  int nArg,   
1e63e 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1e63f 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68   arguments to th
1e640 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20  e function */.  
1e641 45 78 70 72 20 2a 70 45 78 70 72 20 20 20 20 20  Expr *pExpr     
1e642 2f 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e  /* First argumen
1e643 74 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f  t to the functio
1e644 6e 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20  n */.){.  Table 
1e645 2a 70 54 61 62 3b 0a 20 20 73 71 6c 69 74 65 33  *pTab;.  sqlite3
1e646 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20  _vtab *pVtab;.  
1e647 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a  sqlite3_module *
1e648 70 4d 6f 64 3b 0a 20 20 76 6f 69 64 20 28 2a 78  pMod;.  void (*x
1e649 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f  Func)(sqlite3_co
1e64a 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74  ntext*,int,sqlit
1e64b 65 33 5f 76 61 6c 75 65 2a 2a 29 3b 0a 20 20 76  e3_value**);.  v
1e64c 6f 69 64 20 2a 70 41 72 67 3b 0a 20 20 46 75 6e  oid *pArg;.  Fun
1e64d 63 44 65 66 20 2a 70 4e 65 77 3b 0a 20 20 69 6e  cDef *pNew;.  in
1e64e 74 20 72 63 3b 0a 20 20 63 68 61 72 20 2a 7a 4c  t rc;.  char *zL
1e64f 6f 77 65 72 4e 61 6d 65 3b 0a 20 20 75 6e 73 69  owerName;.  unsi
1e650 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a 0a 0a  gned char *z;...
1e651 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65    /* Check to se
1e652 65 20 74 68 65 20 6c 65 66 74 20 6f 70 65 72 61  e the left opera
1e653 6e 64 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 69  nd is a column i
1e654 6e 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  n a virtual tabl
1e655 65 20 2a 2f 0a 20 20 69 66 28 20 70 45 78 70 72  e */.  if( pExpr
1e656 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 70 44 65  ==0 ) return pDe
1e657 66 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e  f;.  if( pExpr->
1e658 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 20  op!=TK_COLUMN ) 
1e659 72 65 74 75 72 6e 20 70 44 65 66 3b 0a 20 20 70  return pDef;.  p
1e65a 54 61 62 20 3d 20 70 45 78 70 72 2d 3e 70 54 61  Tab = pExpr->pTa
1e65b 62 3b 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30  b;.  if( pTab==0
1e65c 20 29 20 72 65 74 75 72 6e 20 70 44 65 66 3b 0a   ) return pDef;.
1e65d 20 20 69 66 28 20 21 70 54 61 62 2d 3e 69 73 56    if( !pTab->isV
1e65e 69 72 74 75 61 6c 20 29 20 72 65 74 75 72 6e 20  irtual ) return 
1e65f 70 44 65 66 3b 0a 20 20 70 56 74 61 62 20 3d 20  pDef;.  pVtab = 
1e660 70 54 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20 61  pTab->pVtab;.  a
1e661 73 73 65 72 74 28 20 70 56 74 61 62 21 3d 30 20  ssert( pVtab!=0 
1e662 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 56 74  );.  assert( pVt
1e663 61 62 2d 3e 70 4d 6f 64 75 6c 65 21 3d 30 20 29  ab->pModule!=0 )
1e664 3b 0a 20 20 70 4d 6f 64 20 3d 20 28 73 71 6c 69  ;.  pMod = (sqli
1e665 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 29 70 56 74  te3_module *)pVt
1e666 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 69  ab->pModule;.  i
1e667 66 28 20 70 4d 6f 64 2d 3e 78 46 69 6e 64 46 75  f( pMod->xFindFu
1e668 6e 63 74 69 6f 6e 3d 3d 30 20 29 20 72 65 74 75  nction==0 ) retu
1e669 72 6e 20 70 44 65 66 3b 0a 20 0a 20 20 2f 2a 20  rn pDef;. .  /* 
1e66a 43 61 6c 6c 20 74 68 65 20 78 46 75 6e 63 46 75  Call the xFuncFu
1e66b 6e 63 74 69 6f 6e 20 6d 65 74 68 6f 64 20 6f 6e  nction method on
1e66c 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
1e66d 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  le implementatio
1e66e 6e 0a 20 20 2a 2a 20 74 6f 20 73 65 65 20 69 66  n.  ** to see if
1e66f 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
1e670 69 6f 6e 20 77 61 6e 74 73 20 74 6f 20 6f 76 65  ion wants to ove
1e671 72 6c 6f 61 64 20 74 68 69 73 20 66 75 6e 63 74  rload this funct
1e672 69 6f 6e 20 0a 20 20 2a 2f 0a 20 20 7a 4c 6f 77  ion .  */.  zLow
1e673 65 72 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33  erName = sqlite3
1e674 53 74 72 44 75 70 28 70 44 65 66 2d 3e 7a 4e 61  StrDup(pDef->zNa
1e675 6d 65 29 3b 0a 20 20 66 6f 72 28 7a 3d 28 75 6e  me);.  for(z=(un
1e676 73 69 67 6e 65 64 20 63 68 61 72 2a 29 7a 4c 6f  signed char*)zLo
1e677 77 65 72 4e 61 6d 65 3b 20 2a 7a 3b 20 7a 2b 2b  werName; *z; z++
1e678 29 7b 0a 20 20 20 20 2a 7a 20 3d 20 73 71 6c 69  ){.    *z = sqli
1e679 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b  te3UpperToLower[
1e67a 2a 7a 5d 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20  *z];.  }.  rc = 
1e67b 70 4d 6f 64 2d 3e 78 46 69 6e 64 46 75 6e 63 74  pMod->xFindFunct
1e67c 69 6f 6e 28 70 56 74 61 62 2c 20 6e 41 72 67 2c  ion(pVtab, nArg,
1e67d 20 7a 4c 6f 77 65 72 4e 61 6d 65 2c 20 26 78 46   zLowerName, &xF
1e67e 75 6e 63 2c 20 26 70 41 72 67 29 3b 0a 20 20 73  unc, &pArg);.  s
1e67f 71 6c 69 74 65 46 72 65 65 28 7a 4c 6f 77 65 72  qliteFree(zLower
1e680 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 72 63 3d  Name);.  if( rc=
1e681 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
1e682 20 70 44 65 66 3b 0a 20 20 7d 0a 0a 20 20 2f 2a   pDef;.  }..  /*
1e683 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 65 70   Create a new ep
1e684 68 65 6d 65 72 61 6c 20 66 75 6e 63 74 69 6f 6e  hemeral function
1e685 20 64 65 66 69 6e 69 74 69 6f 6e 20 66 6f 72 20   definition for 
1e686 74 68 65 20 6f 76 65 72 6c 6f 61 64 65 64 0a 20  the overloaded. 
1e687 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a   ** function */.
1e688 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 4d    pNew = sqliteM
1e689 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70  alloc( sizeof(*p
1e68a 4e 65 77 29 20 2b 20 73 74 72 6c 65 6e 28 70 44  New) + strlen(pD
1e68b 65 66 2d 3e 7a 4e 61 6d 65 29 20 29 3b 0a 20 20  ef->zName) );.  
1e68c 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20  if( pNew==0 ){. 
1e68d 20 20 20 72 65 74 75 72 6e 20 70 44 65 66 3b 0a     return pDef;.
1e68e 20 20 7d 0a 20 20 2a 70 4e 65 77 20 3d 20 2a 70    }.  *pNew = *p
1e68f 44 65 66 3b 0a 20 20 6d 65 6d 63 70 79 28 70 4e  Def;.  memcpy(pN
1e690 65 77 2d 3e 7a 4e 61 6d 65 2c 20 70 44 65 66 2d  ew->zName, pDef-
1e691 3e 7a 4e 61 6d 65 2c 20 73 74 72 6c 65 6e 28 70  >zName, strlen(p
1e692 44 65 66 2d 3e 7a 4e 61 6d 65 29 2b 31 29 3b 0a  Def->zName)+1);.
1e693 20 20 70 4e 65 77 2d 3e 78 46 75 6e 63 20 3d 20    pNew->xFunc = 
1e694 78 46 75 6e 63 3b 0a 20 20 70 4e 65 77 2d 3e 70  xFunc;.  pNew->p
1e695 55 73 65 72 44 61 74 61 20 3d 20 70 41 72 67 3b  UserData = pArg;
1e696 0a 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c  .  pNew->flags |
1e697 3d 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 45 50  = SQLITE_FUNC_EP
1e698 48 45 4d 3b 0a 20 20 72 65 74 75 72 6e 20 70 4e  HEM;.  return pN
1e699 65 77 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a  ew;.}..#endif /*
1e69a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
1e69b 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a  TUALTABLE */../*
1e69c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
1e69d 64 20 6f 66 20 76 74 61 62 2e 63 20 2a 2a 2a 2a  d of vtab.c ****
1e69e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e69f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
1e6a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
1e6a2 67 69 6e 20 66 69 6c 65 20 77 68 65 72 65 2e 63  gin file where.c
1e6a3 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1e6a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e6a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
1e6a6 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
1e6a7 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 15.**.** The 
1e6a8 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
1e6a9 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
1e6aa 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
1e6ab 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
1e6ac 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
1e6ad 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
1e6ae 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
1e6af 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
1e6b0 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
1e6b1 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
1e6b2 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
1e6b3 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
1e6b4 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
1e6b5 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
1e6b6 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
1e6b7 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
1e6b8 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
1e6b9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e6ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e6bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e6bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e6bd 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 6f 64  ****.** This mod
1e6be 75 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63  ule contains C c
1e6bf 6f 64 65 20 74 68 61 74 20 67 65 6e 65 72 61 74  ode that generat
1e6c0 65 73 20 56 44 42 45 20 63 6f 64 65 20 75 73 65  es VDBE code use
1e6c1 64 20 74 6f 20 70 72 6f 63 65 73 73 0a 2a 2a 20  d to process.** 
1e6c2 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
1e6c3 20 6f 66 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   of SQL statemen
1e6c4 74 73 2e 20 20 54 68 69 73 20 6d 6f 64 75 6c 65  ts.  This module
1e6c5 20 69 73 20 72 65 70 6f 6e 73 69 62 6c 65 20 66   is reponsible f
1e6c6 6f 72 0a 2a 2a 20 67 65 6e 65 72 61 74 69 6e 67  or.** generating
1e6c7 20 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 6c   the code that l
1e6c8 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 61 20 74  oops through a t
1e6c9 61 62 6c 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72  able looking for
1e6ca 20 61 70 70 6c 69 63 61 62 6c 65 0a 2a 2a 20 72   applicable.** r
1e6cb 6f 77 73 2e 20 20 49 6e 64 69 63 65 73 20 61 72  ows.  Indices ar
1e6cc 65 20 73 65 6c 65 63 74 65 64 20 61 6e 64 20 75  e selected and u
1e6cd 73 65 64 20 74 6f 20 73 70 65 65 64 20 74 68 65  sed to speed the
1e6ce 20 73 65 61 72 63 68 20 77 68 65 6e 20 64 6f 69   search when doi
1e6cf 6e 67 0a 2a 2a 20 73 6f 20 69 73 20 61 70 70 6c  ng.** so is appl
1e6d0 69 63 61 62 6c 65 2e 20 20 42 65 63 61 75 73 65  icable.  Because
1e6d1 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 73 20   this module is 
1e6d2 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20  responsible for 
1e6d3 73 65 6c 65 63 74 69 6e 67 0a 2a 2a 20 69 6e 64  selecting.** ind
1e6d4 69 63 65 73 2c 20 79 6f 75 20 6d 69 67 68 74 20  ices, you might 
1e6d5 61 6c 73 6f 20 74 68 69 6e 6b 20 6f 66 20 74 68  also think of th
1e6d6 69 73 20 6d 6f 64 75 6c 65 20 61 73 20 74 68 65  is module as the
1e6d7 20 22 71 75 65 72 79 20 6f 70 74 69 6d 69 7a 65   "query optimize
1e6d8 72 22 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 77  r"..**.** $Id: w
1e6d9 68 65 72 65 2e 63 2c 76 20 31 2e 32 35 33 20 32  here.c,v 1.253 2
1e6da 30 30 37 2f 30 36 2f 31 31 20 31 32 3a 35 36 3a  007/06/11 12:56:
1e6db 31 35 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a  15 drh Exp $.*/.
1e6dc 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65  ./*.** The numbe
1e6dd 72 20 6f 66 20 62 69 74 73 20 69 6e 20 61 20 42  r of bits in a B
1e6de 69 74 6d 61 73 6b 2e 20 20 22 42 4d 53 22 20 6d  itmask.  "BMS" m
1e6df 65 61 6e 73 20 22 42 69 74 4d 61 73 6b 20 53 69  eans "BitMask Si
1e6e0 7a 65 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ze"..*/.#define 
1e6e1 42 4d 53 20 20 28 73 69 7a 65 6f 66 28 42 69 74  BMS  (sizeof(Bit
1e6e2 6d 61 73 6b 29 2a 38 29 0a 0a 2f 2a 0a 2a 2a 20  mask)*8)../*.** 
1e6e3 54 72 61 63 65 20 6f 75 74 70 75 74 20 6d 61 63  Trace output mac
1e6e4 72 6f 73 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e  ros.*/.#if defin
1e6e5 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20  ed(SQLITE_TEST) 
1e6e6 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  || defined(SQLIT
1e6e7 45 5f 44 45 42 55 47 29 0a 69 6e 74 20 73 71 6c  E_DEBUG).int sql
1e6e8 69 74 65 33 5f 77 68 65 72 65 5f 74 72 61 63 65  ite3_where_trace
1e6e9 20 3d 20 30 3b 0a 23 20 64 65 66 69 6e 65 20 57   = 0;.# define W
1e6ea 48 45 52 45 54 52 41 43 45 28 58 29 20 20 69 66  HERETRACE(X)  if
1e6eb 28 73 71 6c 69 74 65 33 5f 77 68 65 72 65 5f 74  (sqlite3_where_t
1e6ec 72 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 62  race) sqlite3Deb
1e6ed 75 67 50 72 69 6e 74 66 20 58 0a 23 65 6c 73 65  ugPrintf X.#else
1e6ee 0a 23 20 64 65 66 69 6e 65 20 57 48 45 52 45 54  .# define WHERET
1e6ef 52 41 43 45 28 58 29 0a 23 65 6e 64 69 66 0a 0a  RACE(X).#endif..
1e6f0 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 72  /* Forward refer
1e6f1 65 6e 63 65 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ence.*/.typedef 
1e6f2 73 74 72 75 63 74 20 57 68 65 72 65 43 6c 61 75  struct WhereClau
1e6f3 73 65 20 57 68 65 72 65 43 6c 61 75 73 65 3b 0a  se WhereClause;.
1e6f4 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 45  typedef struct E
1e6f5 78 70 72 4d 61 73 6b 53 65 74 20 45 78 70 72 4d  xprMaskSet ExprM
1e6f6 61 73 6b 53 65 74 3b 0a 0a 2f 2a 0a 2a 2a 20 54  askSet;../*.** T
1e6f7 68 65 20 71 75 65 72 79 20 67 65 6e 65 72 61 74  he query generat
1e6f8 6f 72 20 75 73 65 73 20 61 6e 20 61 72 72 61 79  or uses an array
1e6f9 20 6f 66 20 69 6e 73 74 61 6e 63 65 73 20 6f 66   of instances of
1e6fa 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
1e6fb 74 6f 0a 2a 2a 20 68 65 6c 70 20 69 74 20 61 6e  to.** help it an
1e6fc 61 6c 79 7a 65 20 74 68 65 20 73 75 62 65 78 70  alyze the subexp
1e6fd 72 65 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 20  ressions of the 
1e6fe 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20 45  WHERE clause.  E
1e6ff 61 63 68 20 57 48 45 52 45 0a 2a 2a 20 63 6c 61  ach WHERE.** cla
1e700 75 73 65 20 73 75 62 65 78 70 72 65 73 73 69 6f  use subexpressio
1e701 6e 20 69 73 20 73 65 70 61 72 61 74 65 64 20 66  n is separated f
1e702 72 6f 6d 20 74 68 65 20 6f 74 68 65 72 73 20 62  rom the others b
1e703 79 20 61 6e 20 41 4e 44 20 6f 70 65 72 61 74 6f  y an AND operato
1e704 72 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 57 68 65  r..**.** All Whe
1e705 72 65 54 65 72 6d 73 20 61 72 65 20 63 6f 6c 6c  reTerms are coll
1e706 65 63 74 65 64 20 69 6e 74 6f 20 61 20 73 69 6e  ected into a sin
1e707 67 6c 65 20 57 68 65 72 65 43 6c 61 75 73 65 20  gle WhereClause 
1e708 73 74 72 75 63 74 75 72 65 2e 20 20 0a 2a 2a 20  structure.  .** 
1e709 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 64  The following id
1e70a 65 6e 74 69 74 79 20 68 6f 6c 64 73 3a 0a 2a 2a  entity holds:.**
1e70b 0a 2a 2a 20 20 20 20 20 20 20 20 57 68 65 72 65  .**        Where
1e70c 54 65 72 6d 2e 70 57 43 2d 3e 61 5b 57 68 65 72  Term.pWC->a[Wher
1e70d 65 54 65 72 6d 2e 69 64 78 5d 20 3d 3d 20 57 68  eTerm.idx] == Wh
1e70e 65 72 65 54 65 72 6d 0a 2a 2a 0a 2a 2a 20 57 68  ereTerm.**.** Wh
1e70f 65 6e 20 61 20 74 65 72 6d 20 69 73 20 6f 66 20  en a term is of 
1e710 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20  the form:.**.** 
1e711 20 20 20 20 20 20 20 20 20 20 20 20 20 58 20 3c               X <
1e712 6f 70 3e 20 3c 65 78 70 72 3e 0a 2a 2a 0a 2a 2a  op> <expr>.**.**
1e713 20 77 68 65 72 65 20 58 20 69 73 20 61 20 63 6f   where X is a co
1e714 6c 75 6d 6e 20 6e 61 6d 65 20 61 6e 64 20 3c 6f  lumn name and <o
1e715 70 3e 20 69 73 20 6f 6e 65 20 6f 66 20 63 65 72  p> is one of cer
1e716 74 61 69 6e 20 6f 70 65 72 61 74 6f 72 73 2c 0a  tain operators,.
1e717 2a 2a 20 74 68 65 6e 20 57 68 65 72 65 54 65 72  ** then WhereTer
1e718 6d 2e 6c 65 66 74 43 75 72 73 6f 72 20 61 6e 64  m.leftCursor and
1e719 20 57 68 65 72 65 54 65 72 6d 2e 6c 65 66 74 43   WhereTerm.leftC
1e71a 6f 6c 75 6d 6e 20 72 65 63 6f 72 64 20 74 68 65  olumn record the
1e71b 0a 2a 2a 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  .** cursor numbe
1e71c 72 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 6e 75 6d  r and column num
1e71d 62 65 72 20 66 6f 72 20 58 2e 20 20 57 68 65 72  ber for X.  Wher
1e71e 65 54 65 72 6d 2e 6f 70 65 72 61 74 6f 72 20 72  eTerm.operator r
1e71f 65 63 6f 72 64 73 0a 2a 2a 20 74 68 65 20 3c 6f  ecords.** the <o
1e720 70 3e 20 75 73 69 6e 67 20 61 20 62 69 74 6d 61  p> using a bitma
1e721 73 6b 20 65 6e 63 6f 64 69 6e 67 20 64 65 66 69  sk encoding defi
1e722 6e 65 64 20 62 79 20 57 4f 5f 78 78 78 20 62 65  ned by WO_xxx be
1e723 6c 6f 77 2e 20 20 54 68 65 0a 2a 2a 20 75 73 65  low.  The.** use
1e724 20 6f 66 20 61 20 62 69 74 6d 61 73 6b 20 65 6e   of a bitmask en
1e725 63 6f 64 69 6e 67 20 66 6f 72 20 74 68 65 20 6f  coding for the o
1e726 70 65 72 61 74 6f 72 20 61 6c 6c 6f 77 73 20 75  perator allows u
1e727 73 20 74 6f 20 73 65 61 72 63 68 0a 2a 2a 20 71  s to search.** q
1e728 75 69 63 6b 6c 79 20 66 6f 72 20 74 65 72 6d 73  uickly for terms
1e729 20 74 68 61 74 20 6d 61 74 63 68 20 61 6e 79 20   that match any 
1e72a 6f 66 20 73 65 76 65 72 61 6c 20 64 69 66 66 65  of several diffe
1e72b 72 65 6e 74 20 6f 70 65 72 61 74 6f 72 73 2e 0a  rent operators..
1e72c 2a 2a 0a 2a 2a 20 70 72 65 72 65 71 52 69 67 68  **.** prereqRigh
1e72d 74 20 61 6e 64 20 70 72 65 72 65 71 41 6c 6c 20  t and prereqAll 
1e72e 72 65 63 6f 72 64 20 73 65 74 73 20 6f 66 20 63  record sets of c
1e72f 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 2c 0a 2a  ursor numbers,.*
1e730 2a 20 62 75 74 20 74 68 65 79 20 64 6f 20 73 6f  * but they do so
1e731 20 69 6e 64 69 72 65 63 74 6c 79 2e 20 20 41 20   indirectly.  A 
1e732 73 69 6e 67 6c 65 20 45 78 70 72 4d 61 73 6b 53  single ExprMaskS
1e733 65 74 20 73 74 72 75 63 74 75 72 65 20 74 72 61  et structure tra
1e734 6e 73 6c 61 74 65 73 0a 2a 2a 20 63 75 72 73 6f  nslates.** curso
1e735 72 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 62 69  r number into bi
1e736 74 73 20 61 6e 64 20 74 68 65 20 74 72 61 6e 73  ts and the trans
1e737 6c 61 74 65 64 20 62 69 74 20 69 73 20 73 74 6f  lated bit is sto
1e738 72 65 64 20 69 6e 20 74 68 65 20 70 72 65 72 65  red in the prere
1e739 71 0a 2a 2a 20 66 69 65 6c 64 73 2e 20 20 54 68  q.** fields.  Th
1e73a 65 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 69 73  e translation is
1e73b 20 75 73 65 64 20 69 6e 20 6f 72 64 65 72 20 74   used in order t
1e73c 6f 20 6d 61 78 69 6d 69 7a 65 20 74 68 65 20 6e  o maximize the n
1e73d 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 69 74 73  umber of.** bits
1e73e 20 74 68 61 74 20 77 69 6c 6c 20 66 69 74 20 69   that will fit i
1e73f 6e 20 61 20 42 69 74 6d 61 73 6b 2e 20 20 54 68  n a Bitmask.  Th
1e740 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75  e VDBE cursor nu
1e741 6d 62 65 72 73 20 6d 69 67 68 74 20 62 65 0a 2a  mbers might be.*
1e742 2a 20 73 70 72 65 61 64 20 6f 75 74 20 6f 76 65  * spread out ove
1e743 72 20 74 68 65 20 6e 6f 6e 2d 6e 65 67 61 74 69  r the non-negati
1e744 76 65 20 69 6e 74 65 67 65 72 73 2e 20 20 46 6f  ve integers.  Fo
1e745 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20 63  r example, the c
1e746 75 72 73 6f 72 0a 2a 2a 20 6e 75 6d 62 65 72 73  ursor.** numbers
1e747 20 6d 69 67 68 74 20 62 65 20 33 2c 20 38 2c 20   might be 3, 8, 
1e748 39 2c 20 31 30 2c 20 32 30 2c 20 32 33 2c 20 34  9, 10, 20, 23, 4
1e749 31 2c 20 61 6e 64 20 34 35 2e 20 20 54 68 65 20  1, and 45.  The 
1e74a 45 78 70 72 4d 61 73 6b 53 65 74 0a 2a 2a 20 74  ExprMaskSet.** t
1e74b 72 61 6e 73 6c 61 74 65 73 20 74 68 65 73 65 20  ranslates these 
1e74c 73 70 61 72 73 65 20 63 75 72 73 6f 72 20 6e 75  sparse cursor nu
1e74d 6d 62 65 72 73 20 69 6e 74 6f 20 63 6f 6e 73 65  mbers into conse
1e74e 63 75 74 69 76 65 20 69 6e 74 65 67 65 72 73 0a  cutive integers.
1e74f 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74  ** beginning wit
1e750 68 20 30 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  h 0 in order to 
1e751 6d 61 6b 65 20 74 68 65 20 62 65 73 74 20 70 6f  make the best po
1e752 73 73 69 62 6c 65 20 75 73 65 20 6f 66 20 74 68  ssible use of th
1e753 65 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 62  e available.** b
1e754 69 74 73 20 69 6e 20 74 68 65 20 42 69 74 6d 61  its in the Bitma
1e755 73 6b 2e 20 20 53 6f 2c 20 69 6e 20 74 68 65 20  sk.  So, in the 
1e756 65 78 61 6d 70 6c 65 20 61 62 6f 76 65 2c 20 74  example above, t
1e757 68 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  he cursor number
1e758 73 0a 2a 2a 20 77 6f 75 6c 64 20 62 65 20 6d 61  s.** would be ma
1e759 70 70 65 64 20 69 6e 74 6f 20 69 6e 74 65 67 65  pped into intege
1e75a 72 73 20 30 20 74 68 72 6f 75 67 68 20 37 2e 0a  rs 0 through 7..
1e75b 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
1e75c 74 20 57 68 65 72 65 54 65 72 6d 20 57 68 65 72  t WhereTerm Wher
1e75d 65 54 65 72 6d 3b 0a 73 74 72 75 63 74 20 57 68  eTerm;.struct Wh
1e75e 65 72 65 54 65 72 6d 20 7b 0a 20 20 45 78 70 72  ereTerm {.  Expr
1e75f 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20   *pExpr;        
1e760 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
1e761 6f 20 74 68 65 20 73 75 62 65 78 70 72 65 73 73  o the subexpress
1e762 69 6f 6e 20 2a 2f 0a 20 20 69 31 36 20 69 50 61  ion */.  i16 iPa
1e763 72 65 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  rent;           
1e764 20 2f 2a 20 44 69 73 61 62 6c 65 20 70 57 43 2d   /* Disable pWC-
1e765 3e 61 5b 69 50 61 72 65 6e 74 5d 20 77 68 65 6e  >a[iParent] when
1e766 20 74 68 69 73 20 74 65 72 6d 20 64 69 73 61 62   this term disab
1e767 6c 65 64 20 2a 2f 0a 20 20 69 31 36 20 6c 65 66  led */.  i16 lef
1e768 74 43 75 72 73 6f 72 3b 20 20 20 20 20 20 20 20  tCursor;        
1e769 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65   /* Cursor numbe
1e76a 72 20 6f 66 20 58 20 69 6e 20 22 58 20 3c 6f 70  r of X in "X <op
1e76b 3e 20 3c 65 78 70 72 3e 22 20 2a 2f 0a 20 20 69  > <expr>" */.  i
1e76c 31 36 20 6c 65 66 74 43 6f 6c 75 6d 6e 3b 20 20  16 leftColumn;  
1e76d 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e         /* Column
1e76e 20 6e 75 6d 62 65 72 20 6f 66 20 58 20 69 6e 20   number of X in 
1e76f 22 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 22 20  "X <op> <expr>" 
1e770 2a 2f 0a 20 20 75 31 36 20 65 4f 70 65 72 61 74  */.  u16 eOperat
1e771 6f 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  or;          /* 
1e772 41 20 57 4f 5f 78 78 20 76 61 6c 75 65 20 64 65  A WO_xx value de
1e773 73 63 72 69 62 69 6e 67 20 3c 6f 70 3e 20 2a 2f  scribing <op> */
1e774 0a 20 20 75 38 20 66 6c 61 67 73 3b 20 20 20 20  .  u8 flags;    
1e775 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 69             /* Bi
1e776 74 20 66 6c 61 67 73 2e 20 20 53 65 65 20 62 65  t flags.  See be
1e777 6c 6f 77 20 2a 2f 0a 20 20 75 38 20 6e 43 68 69  low */.  u8 nChi
1e778 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ld;             
1e779 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68   /* Number of ch
1e77a 69 6c 64 72 65 6e 20 74 68 61 74 20 6d 75 73 74  ildren that must
1e77b 20 64 69 73 61 62 6c 65 20 75 73 20 2a 2f 0a 20   disable us */. 
1e77c 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57   WhereClause *pW
1e77d 43 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  C;       /* The 
1e77e 63 6c 61 75 73 65 20 74 68 69 73 20 74 65 72 6d  clause this term
1e77f 20 69 73 20 70 61 72 74 20 6f 66 20 2a 2f 0a 20   is part of */. 
1e780 20 42 69 74 6d 61 73 6b 20 70 72 65 72 65 71 52   Bitmask prereqR
1e781 69 67 68 74 3b 20 20 20 20 2f 2a 20 42 69 74 6d  ight;    /* Bitm
1e782 61 73 6b 20 6f 66 20 74 61 62 6c 65 73 20 75 73  ask of tables us
1e783 65 64 20 62 79 20 70 52 69 67 68 74 20 2a 2f 0a  ed by pRight */.
1e784 20 20 42 69 74 6d 61 73 6b 20 70 72 65 72 65 71    Bitmask prereq
1e785 41 6c 6c 3b 20 20 20 20 20 20 2f 2a 20 42 69 74  All;      /* Bit
1e786 6d 61 73 6b 20 6f 66 20 74 61 62 6c 65 73 20 72  mask of tables r
1e787 65 66 65 72 65 6e 63 65 64 20 62 79 20 70 20 2a  eferenced by p *
1e788 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  /.};../*.** Allo
1e789 77 65 64 20 76 61 6c 75 65 73 20 6f 66 20 57 68  wed values of Wh
1e78a 65 72 65 54 65 72 6d 2e 66 6c 61 67 73 0a 2a 2f  ereTerm.flags.*/
1e78b 0a 23 64 65 66 69 6e 65 20 54 45 52 4d 5f 44 59  .#define TERM_DY
1e78c 4e 41 4d 49 43 20 20 20 20 30 78 30 31 20 20 20  NAMIC    0x01   
1e78d 2f 2a 20 4e 65 65 64 20 74 6f 20 63 61 6c 6c 20  /* Need to call 
1e78e 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
1e78f 65 28 70 45 78 70 72 29 20 2a 2f 0a 23 64 65 66  e(pExpr) */.#def
1e790 69 6e 65 20 54 45 52 4d 5f 56 49 52 54 55 41 4c  ine TERM_VIRTUAL
1e791 20 20 20 20 30 78 30 32 20 20 20 2f 2a 20 41 64      0x02   /* Ad
1e792 64 65 64 20 62 79 20 74 68 65 20 6f 70 74 69 6d  ded by the optim
1e793 69 7a 65 72 2e 20 20 44 6f 20 6e 6f 74 20 63 6f  izer.  Do not co
1e794 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45  de */.#define TE
1e795 52 4d 5f 43 4f 44 45 44 20 20 20 20 20 20 30 78  RM_CODED      0x
1e796 30 34 20 20 20 2f 2a 20 54 68 69 73 20 74 65 72  04   /* This ter
1e797 6d 20 69 73 20 61 6c 72 65 61 64 79 20 63 6f 64  m is already cod
1e798 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45  ed */.#define TE
1e799 52 4d 5f 43 4f 50 49 45 44 20 20 20 20 20 30 78  RM_COPIED     0x
1e79a 30 38 20 20 20 2f 2a 20 48 61 73 20 61 20 63 68  08   /* Has a ch
1e79b 69 6c 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54  ild */.#define T
1e79c 45 52 4d 5f 4f 52 5f 4f 4b 20 20 20 20 20 20 30  ERM_OR_OK      0
1e79d 78 31 30 20 20 20 2f 2a 20 55 73 65 64 20 64 75  x10   /* Used du
1e79e 72 69 6e 67 20 4f 52 2d 63 6c 61 75 73 65 20 70  ring OR-clause p
1e79f 72 6f 63 65 73 73 69 6e 67 20 2a 2f 0a 0a 2f 2a  rocessing */../*
1e7a0 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
1e7a1 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
1e7a2 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73   structure holds
1e7a3 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e   all information
1e7a4 20 61 62 6f 75 74 20 61 0a 2a 2a 20 57 48 45 52   about a.** WHER
1e7a5 45 20 63 6c 61 75 73 65 2e 20 20 4d 6f 73 74 6c  E clause.  Mostl
1e7a6 79 20 74 68 69 73 20 69 73 20 61 20 63 6f 6e 74  y this is a cont
1e7a7 61 69 6e 65 72 20 66 6f 72 20 6f 6e 65 20 6f 72  ainer for one or
1e7a8 20 6d 6f 72 65 20 57 68 65 72 65 54 65 72 6d 73   more WhereTerms
1e7a9 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72  ..*/.struct Wher
1e7aa 65 43 6c 61 75 73 65 20 7b 0a 20 20 50 61 72 73  eClause {.  Pars
1e7ab 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20  e *pParse;      
1e7ac 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
1e7ad 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
1e7ae 45 78 70 72 4d 61 73 6b 53 65 74 20 2a 70 4d 61  ExprMaskSet *pMa
1e7af 73 6b 53 65 74 3b 20 20 20 2f 2a 20 4d 61 70 70  skSet;   /* Mapp
1e7b0 69 6e 67 20 6f 66 20 74 61 62 6c 65 20 69 6e 64  ing of table ind
1e7b1 69 63 65 73 20 74 6f 20 62 69 74 6d 61 73 6b 73  ices to bitmasks
1e7b2 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b   */.  int nTerm;
1e7b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1e7b4 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d  * Number of term
1e7b5 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 6c 6f 74  s */.  int nSlot
1e7b6 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1e7b7 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74  /* Number of ent
1e7b8 72 69 65 73 20 69 6e 20 61 5b 5d 20 2a 2f 0a 20  ries in a[] */. 
1e7b9 20 57 68 65 72 65 54 65 72 6d 20 2a 61 3b 20 20   WhereTerm *a;  
1e7ba 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 61 63            /* Eac
1e7bb 68 20 61 5b 5d 20 64 65 73 63 72 69 62 65 73 20  h a[] describes 
1e7bc 61 20 74 65 72 6d 20 6f 66 20 74 68 65 20 57 48  a term of the WH
1e7bd 45 52 45 20 63 6c 75 61 73 65 20 2a 2f 0a 20 20  ERE cluase */.  
1e7be 57 68 65 72 65 54 65 72 6d 20 61 53 74 61 74 69  WhereTerm aStati
1e7bf 63 5b 31 30 5d 3b 20 20 20 2f 2a 20 49 6e 69 74  c[10];   /* Init
1e7c0 69 61 6c 20 73 74 61 74 69 63 20 73 70 61 63 65  ial static space
1e7c1 20 66 6f 72 20 61 5b 5d 20 2a 2f 0a 7d 3b 0a 0a   for a[] */.};..
1e7c2 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
1e7c3 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
1e7c4 6e 67 20 73 74 72 75 63 74 75 72 65 20 6b 65 65  ng structure kee
1e7c5 70 73 20 74 72 61 63 6b 20 6f 66 20 61 20 6d 61  ps track of a ma
1e7c6 70 70 69 6e 67 0a 2a 2a 20 62 65 74 77 65 65 6e  pping.** between
1e7c7 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d   VDBE cursor num
1e7c8 62 65 72 73 20 61 6e 64 20 62 69 74 73 20 6f 66  bers and bits of
1e7c9 20 74 68 65 20 62 69 74 6d 61 73 6b 73 20 69 6e   the bitmasks in
1e7ca 20 57 68 65 72 65 54 65 72 6d 2e 0a 2a 2a 0a 2a   WhereTerm..**.*
1e7cb 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f  * The VDBE curso
1e7cc 72 20 6e 75 6d 62 65 72 73 20 61 72 65 20 73 6d  r numbers are sm
1e7cd 61 6c 6c 20 69 6e 74 65 67 65 72 73 20 63 6f 6e  all integers con
1e7ce 74 61 69 6e 65 64 20 69 6e 20 0a 2a 2a 20 53 72  tained in .** Sr
1e7cf 63 4c 69 73 74 5f 69 74 65 6d 2e 69 43 75 72 73  cList_item.iCurs
1e7d0 6f 72 20 61 6e 64 20 45 78 70 72 2e 69 54 61 62  or and Expr.iTab
1e7d1 6c 65 20 66 69 65 6c 64 73 2e 20 20 46 6f 72 20  le fields.  For 
1e7d2 61 6e 79 20 67 69 76 65 6e 20 57 48 45 52 45 20  any given WHERE 
1e7d3 0a 2a 2a 20 63 6c 61 75 73 65 2c 20 74 68 65 20  .** clause, the 
1e7d4 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 6d  cursor numbers m
1e7d5 69 67 68 74 20 6e 6f 74 20 62 65 67 69 6e 20 77  ight not begin w
1e7d6 69 74 68 20 30 20 61 6e 64 20 74 68 65 79 20 6d  ith 0 and they m
1e7d7 69 67 68 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e 20  ight.** contain 
1e7d8 67 61 70 73 20 69 6e 20 74 68 65 20 6e 75 6d 62  gaps in the numb
1e7d9 65 72 69 6e 67 20 73 65 71 75 65 6e 63 65 2e 20  ering sequence. 
1e7da 20 42 75 74 20 77 65 20 77 61 6e 74 20 74 6f 20   But we want to 
1e7db 6d 61 6b 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20  make maximum.** 
1e7dc 75 73 65 20 6f 66 20 74 68 65 20 62 69 74 73 20  use of the bits 
1e7dd 69 6e 20 6f 75 72 20 62 69 74 6d 61 73 6b 73 2e  in our bitmasks.
1e7de 20 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65    This structure
1e7df 20 70 72 6f 76 69 64 65 73 20 61 20 6d 61 70 70   provides a mapp
1e7e0 69 6e 67 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20  ing.** from the 
1e7e1 73 70 61 72 73 65 20 63 75 72 73 6f 72 20 6e 75  sparse cursor nu
1e7e2 6d 62 65 72 73 20 69 6e 74 6f 20 63 6f 6e 73 65  mbers into conse
1e7e3 63 75 74 69 76 65 20 69 6e 74 65 67 65 72 73 20  cutive integers 
1e7e4 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 77 69 74  beginning.** wit
1e7e5 68 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 45 78  h 0..**.** If Ex
1e7e6 70 72 4d 61 73 6b 53 65 74 2e 69 78 5b 41 5d 3d  prMaskSet.ix[A]=
1e7e7 3d 42 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74  =B it means that
1e7e8 20 54 68 65 20 41 2d 74 68 20 62 69 74 20 6f 66   The A-th bit of
1e7e9 20 61 20 42 69 74 6d 61 73 6b 0a 2a 2a 20 63 6f   a Bitmask.** co
1e7ea 72 72 65 73 70 6f 6e 64 73 20 56 44 42 45 20 63  rresponds VDBE c
1e7eb 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 42 2e 20  ursor number B. 
1e7ec 20 54 68 65 20 41 2d 74 68 20 62 69 74 20 6f 66   The A-th bit of
1e7ed 20 61 20 62 69 74 6d 61 73 6b 20 69 73 20 31 3c   a bitmask is 1<
1e7ee 3c 41 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78  <A..**.** For ex
1e7ef 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 57 48  ample, if the WH
1e7f0 45 52 45 20 63 6c 61 75 73 65 20 65 78 70 72 65  ERE clause expre
1e7f1 73 73 69 6f 6e 20 75 73 65 64 20 74 68 65 73 65  ssion used these
1e7f2 20 56 44 42 45 0a 2a 2a 20 63 75 72 73 6f 72 73   VDBE.** cursors
1e7f3 3a 20 20 34 2c 20 35 2c 20 38 2c 20 32 39 2c 20  :  4, 5, 8, 29, 
1e7f4 35 37 2c 20 37 33 2e 20 20 54 68 65 6e 20 74 68  57, 73.  Then th
1e7f5 65 20 20 45 78 70 72 4d 61 73 6b 53 65 74 20 73  e  ExprMaskSet s
1e7f6 74 72 75 63 74 75 72 65 0a 2a 2a 20 77 6f 75 6c  tructure.** woul
1e7f7 64 20 6d 61 70 20 74 68 6f 73 65 20 63 75 72 73  d map those curs
1e7f8 6f 72 20 6e 75 6d 62 65 72 73 20 69 6e 74 6f 20  or numbers into 
1e7f9 62 69 74 73 20 30 20 74 68 72 6f 75 67 68 20 35  bits 0 through 5
1e7fa 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ..**.** Note tha
1e7fb 74 20 74 68 65 20 6d 61 70 70 69 6e 67 20 69 73  t the mapping is
1e7fc 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79   not necessarily
1e7fd 20 6f 72 64 65 72 65 64 2e 20 20 49 6e 20 74 68   ordered.  In th
1e7fe 65 20 65 78 61 6d 70 6c 65 0a 2a 2a 20 61 62 6f  e example.** abo
1e7ff 76 65 2c 20 74 68 65 20 6d 61 70 70 69 6e 67 20  ve, the mapping 
1e800 6d 69 67 68 74 20 67 6f 20 6c 69 6b 65 20 74 68  might go like th
1e801 69 73 3a 20 20 34 2d 3e 33 2c 20 35 2d 3e 31 2c  is:  4->3, 5->1,
1e802 20 38 2d 3e 32 2c 20 32 39 2d 3e 30 2c 0a 2a 2a   8->2, 29->0,.**
1e803 20 35 37 2d 3e 35 2c 20 37 33 2d 3e 34 2e 20 20   57->5, 73->4.  
1e804 4f 72 20 6f 6e 65 20 6f 66 20 37 31 39 20 6f 74  Or one of 719 ot
1e805 68 65 72 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73  her combinations
1e806 20 6d 69 67 68 74 20 62 65 20 75 73 65 64 2e 20   might be used. 
1e807 49 74 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 72  It.** does not r
1e808 65 61 6c 6c 79 20 6d 61 74 74 65 72 2e 20 20 57  eally matter.  W
1e809 68 61 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74  hat is important
1e80a 20 69 73 20 74 68 61 74 20 73 70 61 72 73 65 20   is that sparse 
1e80b 63 75 72 73 6f 72 0a 2a 2a 20 6e 75 6d 62 65 72  cursor.** number
1e80c 73 20 61 6c 6c 20 67 65 74 20 6d 61 70 70 65 64  s all get mapped
1e80d 20 69 6e 74 6f 20 62 69 74 20 6e 75 6d 62 65 72   into bit number
1e80e 73 20 74 68 61 74 20 62 65 67 69 6e 20 77 69 74  s that begin wit
1e80f 68 20 30 20 61 6e 64 20 63 6f 6e 74 61 69 6e 0a  h 0 and contain.
1e810 2a 2a 20 6e 6f 20 67 61 70 73 2e 0a 2a 2f 0a 73  ** no gaps..*/.s
1e811 74 72 75 63 74 20 45 78 70 72 4d 61 73 6b 53 65  truct ExprMaskSe
1e812 74 20 7b 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20  t {.  int n;    
1e813 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e814 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1e815 20 61 73 73 69 67 6e 65 64 20 63 75 72 73 6f 72   assigned cursor
1e816 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 69 6e 74   values */.  int
1e817 20 69 78 5b 73 69 7a 65 6f 66 28 42 69 74 6d 61   ix[sizeof(Bitma
1e818 73 6b 29 2a 38 5d 3b 20 20 20 20 2f 2a 20 43 75  sk)*8];    /* Cu
1e819 72 73 6f 72 20 61 73 73 69 67 6e 65 64 20 74 6f  rsor assigned to
1e81a 20 65 61 63 68 20 62 69 74 20 2a 2f 0a 7d 3b 0a   each bit */.};.
1e81b 0a 0a 2f 2a 0a 2a 2a 20 42 69 74 6d 61 73 6b 73  ../*.** Bitmasks
1e81c 20 66 6f 72 20 74 68 65 20 6f 70 65 72 61 74 6f   for the operato
1e81d 72 73 20 74 68 61 74 20 69 6e 64 69 63 65 73 20  rs that indices 
1e81e 61 72 65 20 61 62 6c 65 20 74 6f 20 65 78 70 6c  are able to expl
1e81f 6f 69 74 2e 20 20 41 6e 0a 2a 2a 20 4f 52 2d 65  oit.  An.** OR-e
1e820 64 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66  d combination of
1e821 20 74 68 65 73 65 20 76 61 6c 75 65 73 20 63 61   these values ca
1e822 6e 20 62 65 20 75 73 65 64 20 77 68 65 6e 20 73  n be used when s
1e823 65 61 72 63 68 69 6e 67 20 66 6f 72 0a 2a 2a 20  earching for.** 
1e824 74 65 72 6d 73 20 69 6e 20 74 68 65 20 77 68 65  terms in the whe
1e825 72 65 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a 23 64  re clause..*/.#d
1e826 65 66 69 6e 65 20 57 4f 5f 49 4e 20 20 20 20 20  efine WO_IN     
1e827 31 0a 23 64 65 66 69 6e 65 20 57 4f 5f 45 51 20  1.#define WO_EQ 
1e828 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 57 4f      2.#define WO
1e829 5f 4c 54 20 20 20 20 20 28 57 4f 5f 45 51 3c 3c  _LT     (WO_EQ<<
1e82a 28 54 4b 5f 4c 54 2d 54 4b 5f 45 51 29 29 0a 23  (TK_LT-TK_EQ)).#
1e82b 64 65 66 69 6e 65 20 57 4f 5f 4c 45 20 20 20 20  define WO_LE    
1e82c 20 28 57 4f 5f 45 51 3c 3c 28 54 4b 5f 4c 45 2d   (WO_EQ<<(TK_LE-
1e82d 54 4b 5f 45 51 29 29 0a 23 64 65 66 69 6e 65 20  TK_EQ)).#define 
1e82e 57 4f 5f 47 54 20 20 20 20 20 28 57 4f 5f 45 51  WO_GT     (WO_EQ
1e82f 3c 3c 28 54 4b 5f 47 54 2d 54 4b 5f 45 51 29 29  <<(TK_GT-TK_EQ))
1e830 0a 23 64 65 66 69 6e 65 20 57 4f 5f 47 45 20 20  .#define WO_GE  
1e831 20 20 20 28 57 4f 5f 45 51 3c 3c 28 54 4b 5f 47     (WO_EQ<<(TK_G
1e832 45 2d 54 4b 5f 45 51 29 29 0a 23 64 65 66 69 6e  E-TK_EQ)).#defin
1e833 65 20 57 4f 5f 4d 41 54 43 48 20 20 36 34 0a 23  e WO_MATCH  64.#
1e834 64 65 66 69 6e 65 20 57 4f 5f 49 53 4e 55 4c 4c  define WO_ISNULL
1e835 20 31 32 38 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 75   128../*.** Valu
1e836 65 20 66 6f 72 20 66 6c 61 67 73 20 72 65 74 75  e for flags retu
1e837 72 6e 65 64 20 62 79 20 62 65 73 74 49 6e 64 65  rned by bestInde
1e838 78 28 29 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65  x().  .**.** The
1e839 20 6c 65 61 73 74 20 73 69 67 6e 69 66 69 63 61   least significa
1e83a 6e 74 20 62 79 74 65 20 69 73 20 72 65 73 65 72  nt byte is reser
1e83b 76 65 64 20 61 73 20 61 20 6d 61 73 6b 20 66 6f  ved as a mask fo
1e83c 72 20 57 4f 5f 20 76 61 6c 75 65 73 20 61 62 6f  r WO_ values abo
1e83d 76 65 2e 0a 2a 2a 20 54 68 65 20 57 68 65 72 65  ve..** The Where
1e83e 4c 65 76 65 6c 2e 66 6c 61 67 73 20 66 69 65 6c  Level.flags fiel
1e83f 64 20 69 73 20 75 73 75 61 6c 6c 79 20 73 65 74  d is usually set
1e840 20 74 6f 20 57 4f 5f 49 4e 7c 57 4f 5f 45 51 7c   to WO_IN|WO_EQ|
1e841 57 4f 5f 49 53 4e 55 4c 4c 2e 0a 2a 2a 20 42 75  WO_ISNULL..** Bu
1e842 74 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 69  t if the table i
1e843 73 20 74 68 65 20 72 69 67 68 74 20 74 61 62 6c  s the right tabl
1e844 65 20 6f 66 20 61 20 6c 65 66 74 20 6a 6f 69 6e  e of a left join
1e845 2c 20 57 68 65 72 65 4c 65 76 65 6c 2e 66 6c 61  , WhereLevel.fla
1e846 67 73 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20  gs.** is set to 
1e847 57 4f 5f 49 4e 7c 57 4f 5f 45 51 2e 20 20 54 68  WO_IN|WO_EQ.  Th
1e848 65 20 57 68 65 72 65 4c 65 76 65 6c 2e 66 6c 61  e WhereLevel.fla
1e849 67 73 20 66 69 65 6c 64 20 63 61 6e 20 74 68 65  gs field can the
1e84a 6e 20 62 65 20 75 73 65 64 20 61 73 0a 2a 2a 20  n be used as.** 
1e84b 74 68 65 20 22 6f 70 22 20 70 61 72 61 6d 65 74  the "op" paramet
1e84c 65 72 20 74 6f 20 66 69 6e 64 54 65 72 6d 20 77  er to findTerm w
1e84d 68 65 6e 20 77 65 20 61 72 65 20 72 65 73 6f 6c  hen we are resol
1e84e 76 69 6e 67 20 65 71 75 61 6c 69 74 79 20 63 6f  ving equality co
1e84f 6e 73 74 72 61 69 6e 74 73 2e 0a 2a 2a 20 49 53  nstraints..** IS
1e850 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 73  NULL constraints
1e851 20 77 69 6c 6c 20 74 68 65 6e 20 6e 6f 74 20 62   will then not b
1e852 65 20 75 73 65 64 20 6f 6e 20 74 68 65 20 72 69  e used on the ri
1e853 67 68 74 20 74 61 62 6c 65 20 6f 66 20 61 20 6c  ght table of a l
1e854 65 66 74 0a 2a 2a 20 6a 6f 69 6e 2e 20 20 54 69  eft.** join.  Ti
1e855 63 6b 65 74 73 20 23 32 31 37 37 20 61 6e 64 20  ckets #2177 and 
1e856 23 32 31 38 39 2e 0a 2a 2f 0a 23 64 65 66 69 6e  #2189..*/.#defin
1e857 65 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 45 51  e WHERE_ROWID_EQ
1e858 20 20 20 20 20 30 78 30 30 30 31 30 30 20 20 20       0x000100   
1e859 2f 2a 20 72 6f 77 69 64 3d 45 58 50 52 20 6f 72  /* rowid=EXPR or
1e85a 20 72 6f 77 69 64 20 49 4e 20 28 2e 2e 2e 29 20   rowid IN (...) 
1e85b 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45  */.#define WHERE
1e85c 5f 52 4f 57 49 44 5f 52 41 4e 47 45 20 20 30 78  _ROWID_RANGE  0x
1e85d 30 30 30 32 30 30 20 20 20 2f 2a 20 72 6f 77 69  000200   /* rowi
1e85e 64 3c 45 58 50 52 20 61 6e 64 2f 6f 72 20 72 6f  d<EXPR and/or ro
1e85f 77 69 64 3e 45 58 50 52 20 2a 2f 0a 23 64 65 66  wid>EXPR */.#def
1e860 69 6e 65 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e  ine WHERE_COLUMN
1e861 5f 45 51 20 20 20 20 30 78 30 30 31 30 30 30 20  _EQ    0x001000 
1e862 20 20 2f 2a 20 78 3d 45 58 50 52 20 6f 72 20 78    /* x=EXPR or x
1e863 20 49 4e 20 28 2e 2e 2e 29 20 2a 2f 0a 23 64 65   IN (...) */.#de
1e864 66 69 6e 65 20 57 48 45 52 45 5f 43 4f 4c 55 4d  fine WHERE_COLUM
1e865 4e 5f 52 41 4e 47 45 20 30 78 30 30 32 30 30 30  N_RANGE 0x002000
1e866 20 20 20 2f 2a 20 78 3c 45 58 50 52 20 61 6e 64     /* x<EXPR and
1e867 2f 6f 72 20 78 3e 45 58 50 52 20 2a 2f 0a 23 64  /or x>EXPR */.#d
1e868 65 66 69 6e 65 20 57 48 45 52 45 5f 43 4f 4c 55  efine WHERE_COLU
1e869 4d 4e 5f 49 4e 20 20 20 20 30 78 30 30 34 30 30  MN_IN    0x00400
1e86a 30 20 20 20 2f 2a 20 78 20 49 4e 20 28 2e 2e 2e  0   /* x IN (...
1e86b 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45  ) */.#define WHE
1e86c 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 20 20 20 20  RE_TOP_LIMIT    
1e86d 30 78 30 31 30 30 30 30 20 20 20 2f 2a 20 78 3c  0x010000   /* x<
1e86e 45 58 50 52 20 6f 72 20 78 3c 3d 45 58 50 52 20  EXPR or x<=EXPR 
1e86f 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 23 64  constraint */.#d
1e870 65 66 69 6e 65 20 57 48 45 52 45 5f 42 54 4d 5f  efine WHERE_BTM_
1e871 4c 49 4d 49 54 20 20 20 20 30 78 30 32 30 30 30  LIMIT    0x02000
1e872 30 20 20 20 2f 2a 20 78 3e 45 58 50 52 20 6f 72  0   /* x>EXPR or
1e873 20 78 3e 3d 45 58 50 52 20 63 6f 6e 73 74 72 61   x>=EXPR constra
1e874 69 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57  int */.#define W
1e875 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 20 20 20  HERE_IDX_ONLY   
1e876 20 20 30 78 30 38 30 30 30 30 20 20 20 2f 2a 20    0x080000   /* 
1e877 55 73 65 20 69 6e 64 65 78 20 6f 6e 6c 79 20 2d  Use index only -
1e878 20 6f 6d 69 74 20 74 61 62 6c 65 20 2a 2f 0a 23   omit table */.#
1e879 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44  define WHERE_ORD
1e87a 45 52 42 59 20 20 20 20 20 20 30 78 31 30 30 30  ERBY      0x1000
1e87b 30 30 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 77  00   /* Output w
1e87c 69 6c 6c 20 61 70 70 65 61 72 20 69 6e 20 63 6f  ill appear in co
1e87d 72 72 65 63 74 20 6f 72 64 65 72 20 2a 2f 0a 23  rrect order */.#
1e87e 64 65 66 69 6e 65 20 57 48 45 52 45 5f 52 45 56  define WHERE_REV
1e87f 45 52 53 45 20 20 20 20 20 20 30 78 32 30 30 30  ERSE      0x2000
1e880 30 30 20 20 20 2f 2a 20 53 63 61 6e 20 69 6e 20  00   /* Scan in 
1e881 72 65 76 65 72 73 65 20 6f 72 64 65 72 20 2a 2f  reverse order */
1e882 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 55  .#define WHERE_U
1e883 4e 49 51 55 45 20 20 20 20 20 20 20 30 78 34 30  NIQUE       0x40
1e884 30 30 30 30 20 20 20 2f 2a 20 53 65 6c 65 63 74  0000   /* Select
1e885 73 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e 20 6f  s no more than o
1e886 6e 65 20 72 6f 77 20 2a 2f 0a 23 64 65 66 69 6e  ne row */.#defin
1e887 65 20 57 48 45 52 45 5f 56 49 52 54 55 41 4c 54  e WHERE_VIRTUALT
1e888 41 42 4c 45 20 30 78 38 30 30 30 30 30 20 20 20  ABLE 0x800000   
1e889 2f 2a 20 55 73 65 20 76 69 72 74 75 61 6c 2d 74  /* Use virtual-t
1e88a 61 62 6c 65 20 70 72 6f 63 65 73 73 69 6e 67 20  able processing 
1e88b 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61  */../*.** Initia
1e88c 6c 69 7a 65 20 61 20 70 72 65 61 6c 6c 6f 63 61  lize a prealloca
1e88d 74 65 64 20 57 68 65 72 65 43 6c 61 75 73 65 20  ted WhereClause 
1e88e 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74  structure..*/.st
1e88f 61 74 69 63 20 76 6f 69 64 20 77 68 65 72 65 43  atic void whereC
1e890 6c 61 75 73 65 49 6e 69 74 28 0a 20 20 57 68 65  lauseInit(.  Whe
1e891 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 20  reClause *pWC,  
1e892 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 68 65        /* The Whe
1e893 72 65 43 6c 61 75 73 65 20 74 6f 20 62 65 20 69  reClause to be i
1e894 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20  nitialized */.  
1e895 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
1e896 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1e897 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  parsing context 
1e898 2a 2f 0a 20 20 45 78 70 72 4d 61 73 6b 53 65 74  */.  ExprMaskSet
1e899 20 2a 70 4d 61 73 6b 53 65 74 20 20 20 20 2f 2a   *pMaskSet    /*
1e89a 20 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 74 61   Mapping from ta
1e89b 62 6c 65 20 69 6e 64 69 63 65 73 20 74 6f 20 62  ble indices to b
1e89c 69 74 6d 61 73 6b 73 20 2a 2f 0a 29 7b 0a 20 20  itmasks */.){.  
1e89d 70 57 43 2d 3e 70 50 61 72 73 65 20 3d 20 70 50  pWC->pParse = pP
1e89e 61 72 73 65 3b 0a 20 20 70 57 43 2d 3e 70 4d 61  arse;.  pWC->pMa
1e89f 73 6b 53 65 74 20 3d 20 70 4d 61 73 6b 53 65 74  skSet = pMaskSet
1e8a0 3b 0a 20 20 70 57 43 2d 3e 6e 54 65 72 6d 20 3d  ;.  pWC->nTerm =
1e8a1 20 30 3b 0a 20 20 70 57 43 2d 3e 6e 53 6c 6f 74   0;.  pWC->nSlot
1e8a2 20 3d 20 41 72 72 61 79 53 69 7a 65 28 70 57 43   = ArraySize(pWC
1e8a3 2d 3e 61 53 74 61 74 69 63 29 3b 0a 20 20 70 57  ->aStatic);.  pW
1e8a4 43 2d 3e 61 20 3d 20 70 57 43 2d 3e 61 53 74 61  C->a = pWC->aSta
1e8a5 74 69 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  tic;.}../*.** De
1e8a6 61 6c 6c 6f 63 61 74 65 20 61 20 57 68 65 72 65  allocate a Where
1e8a7 43 6c 61 75 73 65 20 73 74 72 75 63 74 75 72 65  Clause structure
1e8a8 2e 20 20 54 68 65 20 57 68 65 72 65 43 6c 61 75  .  The WhereClau
1e8a9 73 65 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  se structure.** 
1e8aa 69 74 73 65 6c 66 20 69 73 20 6e 6f 74 20 66 72  itself is not fr
1e8ab 65 65 64 2e 20 20 54 68 69 73 20 72 6f 75 74 69  eed.  This routi
1e8ac 6e 65 20 69 73 20 74 68 65 20 69 6e 76 65 72 73  ne is the invers
1e8ad 65 20 6f 66 20 77 68 65 72 65 43 6c 61 75 73 65  e of whereClause
1e8ae 49 6e 69 74 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  Init()..*/.stati
1e8af 63 20 76 6f 69 64 20 77 68 65 72 65 43 6c 61 75  c void whereClau
1e8b0 73 65 43 6c 65 61 72 28 57 68 65 72 65 43 6c 61  seClear(WhereCla
1e8b1 75 73 65 20 2a 70 57 43 29 7b 0a 20 20 69 6e 74  use *pWC){.  int
1e8b2 20 69 3b 0a 20 20 57 68 65 72 65 54 65 72 6d 20   i;.  WhereTerm 
1e8b3 2a 61 3b 0a 20 20 66 6f 72 28 69 3d 70 57 43 2d  *a;.  for(i=pWC-
1e8b4 3e 6e 54 65 72 6d 2d 31 2c 20 61 3d 70 57 43 2d  >nTerm-1, a=pWC-
1e8b5 3e 61 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 61  >a; i>=0; i--, a
1e8b6 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 2d 3e  ++){.    if( a->
1e8b7 66 6c 61 67 73 20 26 20 54 45 52 4d 5f 44 59 4e  flags & TERM_DYN
1e8b8 41 4d 49 43 20 29 7b 0a 20 20 20 20 20 20 73 71  AMIC ){.      sq
1e8b9 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
1e8ba 61 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20 20 7d  a->pExpr);.    }
1e8bb 0a 20 20 7d 0a 20 20 69 66 28 20 70 57 43 2d 3e  .  }.  if( pWC->
1e8bc 61 21 3d 70 57 43 2d 3e 61 53 74 61 74 69 63 20  a!=pWC->aStatic 
1e8bd 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 46 72 65  ){.    sqliteFre
1e8be 65 28 70 57 43 2d 3e 61 29 3b 0a 20 20 7d 0a 7d  e(pWC->a);.  }.}
1e8bf 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65  ../*.** Add a ne
1e8c0 77 20 65 6e 74 72 69 65 73 20 74 6f 20 74 68 65  w entries to the
1e8c1 20 57 68 65 72 65 43 6c 61 75 73 65 20 73 74 72   WhereClause str
1e8c2 75 63 74 75 72 65 2e 20 20 49 6e 63 72 65 61 73  ucture.  Increas
1e8c3 65 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 0a  e the allocated.
1e8c4 2a 2a 20 73 70 61 63 65 20 61 73 20 6e 65 63 65  ** space as nece
1e8c5 73 73 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ssary..**.** If 
1e8c6 74 68 65 20 66 6c 61 67 73 20 61 72 67 75 6d 65  the flags argume
1e8c7 6e 74 20 69 6e 63 6c 75 64 65 73 20 54 45 52 4d  nt includes TERM
1e8c8 5f 44 59 4e 41 4d 49 43 2c 20 74 68 65 6e 20 72  _DYNAMIC, then r
1e8c9 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 0a 2a 2a  esponsibility.**
1e8ca 20 66 6f 72 20 66 72 65 65 69 6e 67 20 74 68 65   for freeing the
1e8cb 20 65 78 70 72 65 73 73 69 6f 6e 20 70 20 69 73   expression p is
1e8cc 20 61 73 73 75 6d 65 64 20 62 79 20 74 68 65 20   assumed by the 
1e8cd 57 68 65 72 65 43 6c 61 75 73 65 20 6f 62 6a 65  WhereClause obje
1e8ce 63 74 2e 0a 2a 2a 0a 2a 2a 20 57 41 52 4e 49 4e  ct..**.** WARNIN
1e8cf 47 3a 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  G:  This routine
1e8d0 20 6d 69 67 68 74 20 72 65 61 6c 6c 6f 63 61 74   might reallocat
1e8d1 65 20 74 68 65 20 73 70 61 63 65 20 75 73 65 64  e the space used
1e8d2 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 57 68 65   to store.** Whe
1e8d3 72 65 54 65 72 6d 73 2e 20 20 41 6c 6c 20 70 6f  reTerms.  All po
1e8d4 69 6e 74 65 72 73 20 74 6f 20 57 68 65 72 65 54  inters to WhereT
1e8d5 65 72 6d 73 20 73 68 6f 75 6c 64 20 62 65 20 69  erms should be i
1e8d6 6e 76 61 6c 69 64 65 64 20 61 66 74 65 72 0a 2a  nvalided after.*
1e8d7 2a 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72  * calling this r
1e8d8 6f 75 74 69 6e 65 2e 20 20 53 75 63 68 20 70 6f  outine.  Such po
1e8d9 69 6e 74 65 72 73 20 6d 61 79 20 62 65 20 72 65  inters may be re
1e8da 69 6e 69 74 69 61 6c 69 7a 65 64 20 62 79 20 72  initialized by r
1e8db 65 66 65 72 65 6e 63 69 6e 67 0a 2a 2a 20 74 68  eferencing.** th
1e8dc 65 20 70 57 43 2d 3e 61 5b 5d 20 61 72 72 61 79  e pWC->a[] array
1e8dd 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
1e8de 77 68 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72  whereClauseInser
1e8df 74 28 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70  t(WhereClause *p
1e8e0 57 43 2c 20 45 78 70 72 20 2a 70 2c 20 69 6e 74  WC, Expr *p, int
1e8e1 20 66 6c 61 67 73 29 7b 0a 20 20 57 68 65 72 65   flags){.  Where
1e8e2 54 65 72 6d 20 2a 70 54 65 72 6d 3b 0a 20 20 69  Term *pTerm;.  i
1e8e3 6e 74 20 69 64 78 3b 0a 20 20 69 66 28 20 70 57  nt idx;.  if( pW
1e8e4 43 2d 3e 6e 54 65 72 6d 3e 3d 70 57 43 2d 3e 6e  C->nTerm>=pWC->n
1e8e5 53 6c 6f 74 20 29 7b 0a 20 20 20 20 57 68 65 72  Slot ){.    Wher
1e8e6 65 54 65 72 6d 20 2a 70 4f 6c 64 20 3d 20 70 57  eTerm *pOld = pW
1e8e7 43 2d 3e 61 3b 0a 20 20 20 20 70 57 43 2d 3e 61  C->a;.    pWC->a
1e8e8 20 3d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28   = sqliteMalloc(
1e8e9 20 73 69 7a 65 6f 66 28 70 57 43 2d 3e 61 5b 30   sizeof(pWC->a[0
1e8ea 5d 29 2a 70 57 43 2d 3e 6e 53 6c 6f 74 2a 32 20  ])*pWC->nSlot*2 
1e8eb 29 3b 0a 20 20 20 20 69 66 28 20 70 57 43 2d 3e  );.    if( pWC->
1e8ec 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66  a==0 ){.      if
1e8ed 28 20 66 6c 61 67 73 20 26 20 54 45 52 4d 5f 44  ( flags & TERM_D
1e8ee 59 4e 41 4d 49 43 20 29 7b 0a 20 20 20 20 20 20  YNAMIC ){.      
1e8ef 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
1e8f0 65 74 65 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a  ete(p);.      }.
1e8f1 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
1e8f2 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79      }.    memcpy
1e8f3 28 70 57 43 2d 3e 61 2c 20 70 4f 6c 64 2c 20 73  (pWC->a, pOld, s
1e8f4 69 7a 65 6f 66 28 70 57 43 2d 3e 61 5b 30 5d 29  izeof(pWC->a[0])
1e8f5 2a 70 57 43 2d 3e 6e 54 65 72 6d 29 3b 0a 20 20  *pWC->nTerm);.  
1e8f6 20 20 69 66 28 20 70 4f 6c 64 21 3d 70 57 43 2d    if( pOld!=pWC-
1e8f7 3e 61 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20  >aStatic ){.    
1e8f8 20 20 73 71 6c 69 74 65 46 72 65 65 28 70 4f 6c    sqliteFree(pOl
1e8f9 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 57  d);.    }.    pW
1e8fa 43 2d 3e 6e 53 6c 6f 74 20 2a 3d 20 32 3b 0a 20  C->nSlot *= 2;. 
1e8fb 20 7d 0a 20 20 70 54 65 72 6d 20 3d 20 26 70 57   }.  pTerm = &pW
1e8fc 43 2d 3e 61 5b 69 64 78 20 3d 20 70 57 43 2d 3e  C->a[idx = pWC->
1e8fd 6e 54 65 72 6d 5d 3b 0a 20 20 70 57 43 2d 3e 6e  nTerm];.  pWC->n
1e8fe 54 65 72 6d 2b 2b 3b 0a 20 20 70 54 65 72 6d 2d  Term++;.  pTerm-
1e8ff 3e 70 45 78 70 72 20 3d 20 70 3b 0a 20 20 70 54  >pExpr = p;.  pT
1e900 65 72 6d 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61  erm->flags = fla
1e901 67 73 3b 0a 20 20 70 54 65 72 6d 2d 3e 70 57 43  gs;.  pTerm->pWC
1e902 20 3d 20 70 57 43 3b 0a 20 20 70 54 65 72 6d 2d   = pWC;.  pTerm-
1e903 3e 69 50 61 72 65 6e 74 20 3d 20 2d 31 3b 0a 20  >iParent = -1;. 
1e904 20 72 65 74 75 72 6e 20 69 64 78 3b 0a 7d 0a 0a   return idx;.}..
1e905 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
1e906 6e 65 20 69 64 65 6e 74 69 66 69 65 73 20 73 75  ne identifies su
1e907 62 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20  bexpressions in 
1e908 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
1e909 20 77 68 65 72 65 0a 2a 2a 20 65 61 63 68 20 73   where.** each s
1e90a 75 62 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  ubexpression is 
1e90b 73 65 70 61 72 61 74 65 64 20 62 79 20 74 68 65  separated by the
1e90c 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 20 6f 72   AND operator or
1e90d 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 6f   some other.** o
1e90e 70 65 72 61 74 6f 72 20 73 70 65 63 69 66 69 65  perator specifie
1e90f 64 20 69 6e 20 74 68 65 20 6f 70 20 70 61 72 61  d in the op para
1e910 6d 65 74 65 72 2e 20 20 54 68 65 20 57 68 65 72  meter.  The Wher
1e911 65 43 6c 61 75 73 65 20 73 74 72 75 63 74 75 72  eClause structur
1e912 65 0a 2a 2a 20 69 73 20 66 69 6c 6c 65 64 20 77  e.** is filled w
1e913 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20  ith pointers to 
1e914 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73 2e 20  subexpressions. 
1e915 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a   For example:.**
1e916 0a 2a 2a 20 20 20 20 57 48 45 52 45 20 20 61 3d  .**    WHERE  a=
1e917 3d 27 68 65 6c 6c 6f 27 20 41 4e 44 20 63 6f 61  ='hello' AND coa
1e918 6c 65 73 63 65 28 62 2c 31 31 29 3c 31 30 20 41  lesce(b,11)<10 A
1e919 4e 44 20 28 63 2b 31 32 21 3d 64 20 4f 52 20 63  ND (c+12!=d OR c
1e91a 3d 3d 32 32 29 0a 2a 2a 20 20 20 20 20 20 20 20  ==22).**        
1e91b 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20     \________/   
1e91c 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f    \_____________
1e91d 5f 5f 2f 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f  __/     \_______
1e91e 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a 2a 20 20 20  _________/.**   
1e91f 20 20 20 20 20 20 20 20 20 73 6c 6f 74 5b 30 5d           slot[0]
1e920 20 20 20 20 20 20 20 20 20 20 20 20 73 6c 6f 74              slot
1e921 5b 31 5d 20 20 20 20 20 20 20 20 20 20 20 20 20  [1]             
1e922 20 20 73 6c 6f 74 5b 32 5d 0a 2a 2a 0a 2a 2a 20    slot[2].**.** 
1e923 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 57 48 45  The original WHE
1e924 52 45 20 63 6c 61 75 73 65 20 69 6e 20 70 45 78  RE clause in pEx
1e925 70 72 20 69 73 20 75 6e 61 6c 74 65 72 65 64 2e  pr is unaltered.
1e926 20 20 41 6c 6c 20 74 68 69 73 20 72 6f 75 74 69    All this routi
1e927 6e 65 0a 2a 2a 20 64 6f 65 73 20 69 73 20 6d 61  ne.** does is ma
1e928 6b 65 20 73 6c 6f 74 5b 5d 20 65 6e 74 72 69 65  ke slot[] entrie
1e929 73 20 70 6f 69 6e 74 20 74 6f 20 73 75 62 73 74  s point to subst
1e92a 72 75 63 74 75 72 65 20 77 69 74 68 69 6e 20 70  ructure within p
1e92b 45 78 70 72 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74  Expr..**.** In t
1e92c 68 65 20 70 72 65 76 69 6f 75 73 20 73 65 6e 74  he previous sent
1e92d 65 6e 63 65 20 61 6e 64 20 69 6e 20 74 68 65 20  ence and in the 
1e92e 64 69 61 67 72 61 6d 2c 20 22 73 6c 6f 74 5b 5d  diagram, "slot[]
1e92f 22 20 72 65 66 65 72 73 20 74 6f 0a 2a 2a 20 74  " refers to.** t
1e930 68 65 20 57 68 65 72 65 43 6c 61 75 73 65 2e 61  he WhereClause.a
1e931 5b 5d 20 61 72 72 61 79 2e 20 20 54 68 69 73 20  [] array.  This 
1e932 61 72 72 61 79 20 67 72 6f 77 73 20 61 73 20 6e  array grows as n
1e933 65 65 64 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e  eeded to contain
1e934 0a 2a 2a 20 61 6c 6c 20 74 65 72 6d 73 20 6f 66  .** all terms of
1e935 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
1e936 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
1e937 64 20 77 68 65 72 65 53 70 6c 69 74 28 57 68 65  d whereSplit(Whe
1e938 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 45  reClause *pWC, E
1e939 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20  xpr *pExpr, int 
1e93a 6f 70 29 7b 0a 20 20 69 66 28 20 70 45 78 70 72  op){.  if( pExpr
1e93b 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
1e93c 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 6f  if( pExpr->op!=o
1e93d 70 20 29 7b 0a 20 20 20 20 77 68 65 72 65 43 6c  p ){.    whereCl
1e93e 61 75 73 65 49 6e 73 65 72 74 28 70 57 43 2c 20  auseInsert(pWC, 
1e93f 70 45 78 70 72 2c 20 30 29 3b 0a 20 20 7d 65 6c  pExpr, 0);.  }el
1e940 73 65 7b 0a 20 20 20 20 77 68 65 72 65 53 70 6c  se{.    whereSpl
1e941 69 74 28 70 57 43 2c 20 70 45 78 70 72 2d 3e 70  it(pWC, pExpr->p
1e942 4c 65 66 74 2c 20 6f 70 29 3b 0a 20 20 20 20 77  Left, op);.    w
1e943 68 65 72 65 53 70 6c 69 74 28 70 57 43 2c 20 70  hereSplit(pWC, p
1e944 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f 70  Expr->pRight, op
1e945 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
1e946 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 20 65 78  Initialize an ex
1e947 70 72 65 73 73 69 6f 6e 20 6d 61 73 6b 20 73 65  pression mask se
1e948 74 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 69 6e 69  t.*/.#define ini
1e949 74 4d 61 73 6b 53 65 74 28 50 29 20 20 6d 65 6d  tMaskSet(P)  mem
1e94a 73 65 74 28 50 2c 20 30 2c 20 73 69 7a 65 6f 66  set(P, 0, sizeof
1e94b 28 2a 50 29 29 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  (*P))../*.** Ret
1e94c 75 72 6e 20 74 68 65 20 62 69 74 6d 61 73 6b 20  urn the bitmask 
1e94d 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20 63 75  for the given cu
1e94e 72 73 6f 72 20 6e 75 6d 62 65 72 2e 20 20 52 65  rsor number.  Re
1e94f 74 75 72 6e 20 30 20 69 66 0a 2a 2a 20 69 43 75  turn 0 if.** iCu
1e950 72 73 6f 72 20 69 73 20 6e 6f 74 20 69 6e 20 74  rsor is not in t
1e951 68 65 20 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69  he set..*/.stati
1e952 63 20 42 69 74 6d 61 73 6b 20 67 65 74 4d 61 73  c Bitmask getMas
1e953 6b 28 45 78 70 72 4d 61 73 6b 53 65 74 20 2a 70  k(ExprMaskSet *p
1e954 4d 61 73 6b 53 65 74 2c 20 69 6e 74 20 69 43 75  MaskSet, int iCu
1e955 72 73 6f 72 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  rsor){.  int i;.
1e956 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4d 61    for(i=0; i<pMa
1e957 73 6b 53 65 74 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a  skSet->n; i++){.
1e958 20 20 20 20 69 66 28 20 70 4d 61 73 6b 53 65 74      if( pMaskSet
1e959 2d 3e 69 78 5b 69 5d 3d 3d 69 43 75 72 73 6f 72  ->ix[i]==iCursor
1e95a 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
1e95b 20 28 28 42 69 74 6d 61 73 6b 29 31 29 3c 3c 69   ((Bitmask)1)<<i
1e95c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
1e95d 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
1e95e 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 6d 61   Create a new ma
1e95f 73 6b 20 66 6f 72 20 63 75 72 73 6f 72 20 69 43  sk for cursor iC
1e960 75 72 73 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ursor..**.** The
1e961 72 65 20 69 73 20 6f 6e 65 20 63 75 72 73 6f 72  re is one cursor
1e962 20 70 65 72 20 74 61 62 6c 65 20 69 6e 20 74 68   per table in th
1e963 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20  e FROM clause.  
1e964 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a  The number of.**
1e965 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 46   tables in the F
1e966 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 6c 69  ROM clause is li
1e967 6d 69 74 65 64 20 62 79 20 61 20 74 65 73 74 20  mited by a test 
1e968 65 61 72 6c 79 20 69 6e 20 74 68 65 0a 2a 2a 20  early in the.** 
1e969 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69  sqlite3WhereBegi
1e96a 6e 28 29 20 72 6f 75 74 69 6e 65 2e 20 20 53 6f  n() routine.  So
1e96b 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 74 68   we know that th
1e96c 65 20 70 4d 61 73 6b 53 65 74 2d 3e 69 78 5b 5d  e pMaskSet->ix[]
1e96d 0a 2a 2a 20 61 72 72 61 79 20 77 69 6c 6c 20 6e  .** array will n
1e96e 65 76 65 72 20 6f 76 65 72 66 6c 6f 77 2e 0a 2a  ever overflow..*
1e96f 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 72  /.static void cr
1e970 65 61 74 65 4d 61 73 6b 28 45 78 70 72 4d 61 73  eateMask(ExprMas
1e971 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20  kSet *pMaskSet, 
1e972 69 6e 74 20 69 43 75 72 73 6f 72 29 7b 0a 20 20  int iCursor){.  
1e973 61 73 73 65 72 74 28 20 70 4d 61 73 6b 53 65 74  assert( pMaskSet
1e974 2d 3e 6e 20 3c 20 41 72 72 61 79 53 69 7a 65 28  ->n < ArraySize(
1e975 70 4d 61 73 6b 53 65 74 2d 3e 69 78 29 20 29 3b  pMaskSet->ix) );
1e976 0a 20 20 70 4d 61 73 6b 53 65 74 2d 3e 69 78 5b  .  pMaskSet->ix[
1e977 70 4d 61 73 6b 53 65 74 2d 3e 6e 2b 2b 5d 20 3d  pMaskSet->n++] =
1e978 20 69 43 75 72 73 6f 72 3b 0a 7d 0a 0a 2f 2a 0a   iCursor;.}../*.
1e979 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
1e97a 77 61 6c 6b 73 20 28 72 65 63 75 72 73 69 76 65  walks (recursive
1e97b 6c 79 29 20 61 6e 20 65 78 70 72 65 73 73 69 6f  ly) an expressio
1e97c 6e 20 74 72 65 65 20 61 6e 64 20 67 65 6e 65 72  n tree and gener
1e97d 61 74 65 73 0a 2a 2a 20 61 20 62 69 74 6d 61 73  ates.** a bitmas
1e97e 6b 20 69 6e 64 69 63 61 74 69 6e 67 20 77 68 69  k indicating whi
1e97f 63 68 20 74 61 62 6c 65 73 20 61 72 65 20 75 73  ch tables are us
1e980 65 64 20 69 6e 20 74 68 61 74 20 65 78 70 72 65  ed in that expre
1e981 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 65 2e 0a 2a  ssion.** tree..*
1e982 2a 0a 2a 2a 20 49 6e 20 6f 72 64 65 72 20 66 6f  *.** In order fo
1e983 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74  r this routine t
1e984 6f 20 77 6f 72 6b 2c 20 74 68 65 20 63 61 6c 6c  o work, the call
1e985 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73  ing function mus
1e986 74 20 68 61 76 65 0a 2a 2a 20 70 72 65 76 69 6f  t have.** previo
1e987 75 73 6c 79 20 69 6e 76 6f 6b 65 64 20 73 71 6c  usly invoked sql
1e988 69 74 65 33 45 78 70 72 52 65 73 6f 6c 76 65 4e  ite3ExprResolveN
1e989 61 6d 65 73 28 29 20 6f 6e 20 74 68 65 20 65 78  ames() on the ex
1e98a 70 72 65 73 73 69 6f 6e 2e 20 20 53 65 65 0a 2a  pression.  See.*
1e98b 2a 20 74 68 65 20 68 65 61 64 65 72 20 63 6f 6d  * the header com
1e98c 6d 65 6e 74 20 6f 6e 20 74 68 61 74 20 72 6f 75  ment on that rou
1e98d 74 69 6e 65 20 66 6f 72 20 61 64 64 69 74 69 6f  tine for additio
1e98e 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  nal information.
1e98f 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 45  .** The sqlite3E
1e990 78 70 72 52 65 73 6f 6c 76 65 4e 61 6d 65 73 28  xprResolveNames(
1e991 29 20 72 6f 75 74 69 6e 65 73 20 6c 6f 6f 6b 73  ) routines looks
1e992 20 66 6f 72 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65   for column name
1e993 73 20 61 6e 64 0a 2a 2a 20 73 65 74 73 20 74 68  s and.** sets th
1e994 65 69 72 20 6f 70 63 6f 64 65 73 20 74 6f 20 54  eir opcodes to T
1e995 4b 5f 43 4f 4c 55 4d 4e 20 61 6e 64 20 74 68 65  K_COLUMN and the
1e996 69 72 20 45 78 70 72 2e 69 54 61 62 6c 65 20 66  ir Expr.iTable f
1e997 69 65 6c 64 73 20 74 6f 0a 2a 2a 20 74 68 65 20  ields to.** the 
1e998 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62  VDBE cursor numb
1e999 65 72 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e  er of the table.
1e99a 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6a    This routine j
1e99b 75 73 74 20 68 61 73 20 74 6f 0a 2a 2a 20 74 72  ust has to.** tr
1e99c 61 6e 73 6c 61 74 65 20 74 68 65 20 63 75 72 73  anslate the curs
1e99d 6f 72 20 6e 75 6d 62 65 72 73 20 69 6e 74 6f 20  or numbers into 
1e99e 62 69 74 6d 61 73 6b 20 76 61 6c 75 65 73 20 61  bitmask values a
1e99f 6e 64 20 4f 52 20 61 6c 6c 0a 2a 2a 20 74 68 65  nd OR all.** the
1e9a0 20 62 69 74 6d 61 73 6b 73 20 74 6f 67 65 74 68   bitmasks togeth
1e9a1 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 42 69  er..*/.static Bi
1e9a2 74 6d 61 73 6b 20 65 78 70 72 4c 69 73 74 54 61  tmask exprListTa
1e9a3 62 6c 65 55 73 61 67 65 28 45 78 70 72 4d 61 73  bleUsage(ExprMas
1e9a4 6b 53 65 74 2a 2c 20 45 78 70 72 4c 69 73 74 2a  kSet*, ExprList*
1e9a5 29 3b 0a 73 74 61 74 69 63 20 42 69 74 6d 61 73  );.static Bitmas
1e9a6 6b 20 65 78 70 72 53 65 6c 65 63 74 54 61 62 6c  k exprSelectTabl
1e9a7 65 55 73 61 67 65 28 45 78 70 72 4d 61 73 6b 53  eUsage(ExprMaskS
1e9a8 65 74 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 73  et*, Select*);.s
1e9a9 74 61 74 69 63 20 42 69 74 6d 61 73 6b 20 65 78  tatic Bitmask ex
1e9aa 70 72 54 61 62 6c 65 55 73 61 67 65 28 45 78 70  prTableUsage(Exp
1e9ab 72 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53  rMaskSet *pMaskS
1e9ac 65 74 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 20  et, Expr *p){.  
1e9ad 42 69 74 6d 61 73 6b 20 6d 61 73 6b 20 3d 20 30  Bitmask mask = 0
1e9ae 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ;.  if( p==0 ) r
1e9af 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70  eturn 0;.  if( p
1e9b0 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20  ->op==TK_COLUMN 
1e9b1 29 7b 0a 20 20 20 20 6d 61 73 6b 20 3d 20 67 65  ){.    mask = ge
1e9b2 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20  tMask(pMaskSet, 
1e9b3 70 2d 3e 69 54 61 62 6c 65 29 3b 0a 20 20 20 20  p->iTable);.    
1e9b4 72 65 74 75 72 6e 20 6d 61 73 6b 3b 0a 20 20 7d  return mask;.  }
1e9b5 0a 20 20 6d 61 73 6b 20 3d 20 65 78 70 72 54 61  .  mask = exprTa
1e9b6 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65  bleUsage(pMaskSe
1e9b7 74 2c 20 70 2d 3e 70 52 69 67 68 74 29 3b 0a 20  t, p->pRight);. 
1e9b8 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 54 61 62   mask |= exprTab
1e9b9 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74  leUsage(pMaskSet
1e9ba 2c 20 70 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 6d  , p->pLeft);.  m
1e9bb 61 73 6b 20 7c 3d 20 65 78 70 72 4c 69 73 74 54  ask |= exprListT
1e9bc 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53  ableUsage(pMaskS
1e9bd 65 74 2c 20 70 2d 3e 70 4c 69 73 74 29 3b 0a 20  et, p->pList);. 
1e9be 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 53 65 6c   mask |= exprSel
1e9bf 65 63 74 54 61 62 6c 65 55 73 61 67 65 28 70 4d  ectTableUsage(pM
1e9c0 61 73 6b 53 65 74 2c 20 70 2d 3e 70 53 65 6c 65  askSet, p->pSele
1e9c1 63 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 6d 61  ct);.  return ma
1e9c2 73 6b 3b 0a 7d 0a 73 74 61 74 69 63 20 42 69 74  sk;.}.static Bit
1e9c3 6d 61 73 6b 20 65 78 70 72 4c 69 73 74 54 61 62  mask exprListTab
1e9c4 6c 65 55 73 61 67 65 28 45 78 70 72 4d 61 73 6b  leUsage(ExprMask
1e9c5 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 45  Set *pMaskSet, E
1e9c6 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b  xprList *pList){
1e9c7 0a 20 20 69 6e 74 20 69 3b 0a 20 20 42 69 74 6d  .  int i;.  Bitm
1e9c8 61 73 6b 20 6d 61 73 6b 20 3d 20 30 3b 0a 20 20  ask mask = 0;.  
1e9c9 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 20 20  if( pList ){.   
1e9ca 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73   for(i=0; i<pLis
1e9cb 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a  t->nExpr; i++){.
1e9cc 20 20 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78        mask |= ex
1e9cd 70 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61  prTableUsage(pMa
1e9ce 73 6b 53 65 74 2c 20 70 4c 69 73 74 2d 3e 61 5b  skSet, pList->a[
1e9cf 69 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 7d  i].pExpr);.    }
1e9d0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6d 61  .  }.  return ma
1e9d1 73 6b 3b 0a 7d 0a 73 74 61 74 69 63 20 42 69 74  sk;.}.static Bit
1e9d2 6d 61 73 6b 20 65 78 70 72 53 65 6c 65 63 74 54  mask exprSelectT
1e9d3 61 62 6c 65 55 73 61 67 65 28 45 78 70 72 4d 61  ableUsage(ExprMa
1e9d4 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c  skSet *pMaskSet,
1e9d5 20 53 65 6c 65 63 74 20 2a 70 53 29 7b 0a 20 20   Select *pS){.  
1e9d6 42 69 74 6d 61 73 6b 20 6d 61 73 6b 3b 0a 20 20  Bitmask mask;.  
1e9d7 69 66 28 20 70 53 3d 3d 30 20 29 7b 0a 20 20 20  if( pS==0 ){.   
1e9d8 20 6d 61 73 6b 20 3d 20 30 3b 0a 20 20 7d 65 6c   mask = 0;.  }el
1e9d9 73 65 7b 0a 20 20 20 20 6d 61 73 6b 20 3d 20 65  se{.    mask = e
1e9da 78 70 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67  xprListTableUsag
1e9db 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 53 2d 3e  e(pMaskSet, pS->
1e9dc 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 6d 61 73  pEList);.    mas
1e9dd 6b 20 7c 3d 20 65 78 70 72 4c 69 73 74 54 61 62  k |= exprListTab
1e9de 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74  leUsage(pMaskSet
1e9df 2c 20 70 53 2d 3e 70 47 72 6f 75 70 42 79 29 3b  , pS->pGroupBy);
1e9e0 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70  .    mask |= exp
1e9e1 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67 65 28  rListTableUsage(
1e9e2 70 4d 61 73 6b 53 65 74 2c 20 70 53 2d 3e 70 4f  pMaskSet, pS->pO
1e9e3 72 64 65 72 42 79 29 3b 0a 20 20 20 20 6d 61 73  rderBy);.    mas
1e9e4 6b 20 7c 3d 20 65 78 70 72 54 61 62 6c 65 55 73  k |= exprTableUs
1e9e5 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 53  age(pMaskSet, pS
1e9e6 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20 20 20 6d  ->pWhere);.    m
1e9e7 61 73 6b 20 7c 3d 20 65 78 70 72 54 61 62 6c 65  ask |= exprTable
1e9e8 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20  Usage(pMaskSet, 
1e9e9 70 53 2d 3e 70 48 61 76 69 6e 67 29 3b 0a 20 20  pS->pHaving);.  
1e9ea 7d 0a 20 20 72 65 74 75 72 6e 20 6d 61 73 6b 3b  }.  return mask;
1e9eb 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
1e9ec 20 54 52 55 45 20 69 66 20 74 68 65 20 67 69 76   TRUE if the giv
1e9ed 65 6e 20 6f 70 65 72 61 74 6f 72 20 69 73 20 6f  en operator is o
1e9ee 6e 65 20 6f 66 20 74 68 65 20 6f 70 65 72 61 74  ne of the operat
1e9ef 6f 72 73 20 74 68 61 74 20 69 73 0a 2a 2a 20 61  ors that is.** a
1e9f0 6c 6c 6f 77 65 64 20 66 6f 72 20 61 6e 20 69 6e  llowed for an in
1e9f1 64 65 78 61 62 6c 65 20 57 48 45 52 45 20 63 6c  dexable WHERE cl
1e9f2 61 75 73 65 20 74 65 72 6d 2e 20 20 54 68 65 20  ause term.  The 
1e9f3 61 6c 6c 6f 77 65 64 20 6f 70 65 72 61 74 6f 72  allowed operator
1e9f4 73 20 61 72 65 0a 2a 2a 20 22 3d 22 2c 20 22 3c  s are.** "=", "<
1e9f5 22 2c 20 22 3e 22 2c 20 22 3c 3d 22 2c 20 22 3e  ", ">", "<=", ">
1e9f6 3d 22 2c 20 61 6e 64 20 22 49 4e 22 2e 0a 2a 2f  =", and "IN"..*/
1e9f7 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f  .static int allo
1e9f8 77 65 64 4f 70 28 69 6e 74 20 6f 70 29 7b 0a 20  wedOp(int op){. 
1e9f9 20 61 73 73 65 72 74 28 20 54 4b 5f 47 54 3e 54   assert( TK_GT>T
1e9fa 4b 5f 45 51 20 26 26 20 54 4b 5f 47 54 3c 54 4b  K_EQ && TK_GT<TK
1e9fb 5f 47 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28  _GE );.  assert(
1e9fc 20 54 4b 5f 4c 54 3e 54 4b 5f 45 51 20 26 26 20   TK_LT>TK_EQ && 
1e9fd 54 4b 5f 4c 54 3c 54 4b 5f 47 45 20 29 3b 0a 20  TK_LT<TK_GE );. 
1e9fe 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 45 3e 54   assert( TK_LE>T
1e9ff 4b 5f 45 51 20 26 26 20 54 4b 5f 4c 45 3c 54 4b  K_EQ && TK_LE<TK
1ea00 5f 47 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28  _GE );.  assert(
1ea01 20 54 4b 5f 47 45 3d 3d 54 4b 5f 45 51 2b 34 20   TK_GE==TK_EQ+4 
1ea02 29 3b 0a 20 20 72 65 74 75 72 6e 20 6f 70 3d 3d  );.  return op==
1ea03 54 4b 5f 49 4e 20 7c 7c 20 28 6f 70 3e 3d 54 4b  TK_IN || (op>=TK
1ea04 5f 45 51 20 26 26 20 6f 70 3c 3d 54 4b 5f 47 45  _EQ && op<=TK_GE
1ea05 29 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55  ) || op==TK_ISNU
1ea06 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 77 61  LL;.}../*.** Swa
1ea07 70 20 74 77 6f 20 6f 62 6a 65 63 74 73 20 6f 66  p two objects of
1ea08 20 74 79 70 65 20 54 2e 0a 2a 2f 0a 23 64 65 66   type T..*/.#def
1ea09 69 6e 65 20 53 57 41 50 28 54 59 50 45 2c 41 2c  ine SWAP(TYPE,A,
1ea0a 42 29 20 7b 54 59 50 45 20 74 3d 41 3b 20 41 3d  B) {TYPE t=A; A=
1ea0b 42 3b 20 42 3d 74 3b 7d 0a 0a 2f 2a 0a 2a 2a 20  B; B=t;}../*.** 
1ea0c 43 6f 6d 6d 75 74 65 20 61 20 63 6f 6d 70 61 72  Commute a compar
1ea0d 69 73 69 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 20  ision operator. 
1ea0e 20 45 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20   Expressions of 
1ea0f 74 68 65 20 66 6f 72 6d 20 22 58 20 6f 70 20 59  the form "X op Y
1ea10 22 0a 2a 2a 20 61 72 65 20 63 6f 6e 76 65 72 74  ".** are convert
1ea11 65 64 20 69 6e 74 6f 20 22 59 20 6f 70 20 58 22  ed into "Y op X"
1ea12 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
1ea13 20 65 78 70 72 43 6f 6d 6d 75 74 65 28 45 78 70   exprCommute(Exp
1ea14 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 61 73 73  r *pExpr){.  ass
1ea15 65 72 74 28 20 61 6c 6c 6f 77 65 64 4f 70 28 70  ert( allowedOp(p
1ea16 45 78 70 72 2d 3e 6f 70 29 20 26 26 20 70 45 78  Expr->op) && pEx
1ea17 70 72 2d 3e 6f 70 21 3d 54 4b 5f 49 4e 20 29 3b  pr->op!=TK_IN );
1ea18 0a 20 20 53 57 41 50 28 43 6f 6c 6c 53 65 71 2a  .  SWAP(CollSeq*
1ea19 2c 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2d 3e  ,pExpr->pRight->
1ea1a 70 43 6f 6c 6c 2c 70 45 78 70 72 2d 3e 70 4c 65  pColl,pExpr->pLe
1ea1b 66 74 2d 3e 70 43 6f 6c 6c 29 3b 0a 20 20 53 57  ft->pColl);.  SW
1ea1c 41 50 28 45 78 70 72 2a 2c 70 45 78 70 72 2d 3e  AP(Expr*,pExpr->
1ea1d 70 52 69 67 68 74 2c 70 45 78 70 72 2d 3e 70 4c  pRight,pExpr->pL
1ea1e 65 66 74 29 3b 0a 20 20 69 66 28 20 70 45 78 70  eft);.  if( pExp
1ea1f 72 2d 3e 6f 70 3e 3d 54 4b 5f 47 54 20 29 7b 0a  r->op>=TK_GT ){.
1ea20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c      assert( TK_L
1ea21 54 3d 3d 54 4b 5f 47 54 2b 32 20 29 3b 0a 20 20  T==TK_GT+2 );.  
1ea22 20 20 61 73 73 65 72 74 28 20 54 4b 5f 47 45 3d    assert( TK_GE=
1ea23 3d 54 4b 5f 4c 45 2b 32 20 29 3b 0a 20 20 20 20  =TK_LE+2 );.    
1ea24 61 73 73 65 72 74 28 20 54 4b 5f 47 54 3e 54 4b  assert( TK_GT>TK
1ea25 5f 45 51 20 29 3b 0a 20 20 20 20 61 73 73 65 72  _EQ );.    asser
1ea26 74 28 20 54 4b 5f 47 54 3c 54 4b 5f 4c 45 20 29  t( TK_GT<TK_LE )
1ea27 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45  ;.    assert( pE
1ea28 78 70 72 2d 3e 6f 70 3e 3d 54 4b 5f 47 54 20 26  xpr->op>=TK_GT &
1ea29 26 20 70 45 78 70 72 2d 3e 6f 70 3c 3d 54 4b 5f  & pExpr->op<=TK_
1ea2a 47 45 20 29 3b 0a 20 20 20 20 70 45 78 70 72 2d  GE );.    pExpr-
1ea2b 3e 6f 70 20 3d 20 28 28 70 45 78 70 72 2d 3e 6f  >op = ((pExpr->o
1ea2c 70 2d 54 4b 5f 47 54 29 5e 32 29 2b 54 4b 5f 47  p-TK_GT)^2)+TK_G
1ea2d 54 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  T;.  }.}../*.** 
1ea2e 54 72 61 6e 73 6c 61 74 65 20 66 72 6f 6d 20 54  Translate from T
1ea2f 4b 5f 78 78 20 6f 70 65 72 61 74 6f 72 20 74 6f  K_xx operator to
1ea30 20 57 4f 5f 78 78 20 62 69 74 6d 61 73 6b 2e 0a   WO_xx bitmask..
1ea31 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70  */.static int op
1ea32 65 72 61 74 6f 72 4d 61 73 6b 28 69 6e 74 20 6f  eratorMask(int o
1ea33 70 29 7b 0a 20 20 69 6e 74 20 63 3b 0a 20 20 61  p){.  int c;.  a
1ea34 73 73 65 72 74 28 20 61 6c 6c 6f 77 65 64 4f 70  ssert( allowedOp
1ea35 28 6f 70 29 20 29 3b 0a 20 20 69 66 28 20 6f 70  (op) );.  if( op
1ea36 3d 3d 54 4b 5f 49 4e 20 29 7b 0a 20 20 20 20 63  ==TK_IN ){.    c
1ea37 20 3d 20 57 4f 5f 49 4e 3b 0a 20 20 7d 65 6c 73   = WO_IN;.  }els
1ea38 65 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e  e if( op==TK_ISN
1ea39 55 4c 4c 20 29 7b 0a 20 20 20 20 63 20 3d 20 57  ULL ){.    c = W
1ea3a 4f 5f 49 53 4e 55 4c 4c 3b 0a 20 20 7d 65 6c 73  O_ISNULL;.  }els
1ea3b 65 7b 0a 20 20 20 20 63 20 3d 20 57 4f 5f 45 51  e{.    c = WO_EQ
1ea3c 3c 3c 28 6f 70 2d 54 4b 5f 45 51 29 3b 0a 20 20  <<(op-TK_EQ);.  
1ea3d 7d 0a 20 20 61 73 73 65 72 74 28 20 6f 70 21 3d  }.  assert( op!=
1ea3e 54 4b 5f 49 53 4e 55 4c 4c 20 7c 7c 20 63 3d 3d  TK_ISNULL || c==
1ea3f 57 4f 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 61  WO_ISNULL );.  a
1ea40 73 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f 49 4e  ssert( op!=TK_IN
1ea41 20 7c 7c 20 63 3d 3d 57 4f 5f 49 4e 20 29 3b 0a   || c==WO_IN );.
1ea42 20 20 61 73 73 65 72 74 28 20 6f 70 21 3d 54 4b    assert( op!=TK
1ea43 5f 45 51 20 7c 7c 20 63 3d 3d 57 4f 5f 45 51 20  _EQ || c==WO_EQ 
1ea44 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 21  );.  assert( op!
1ea45 3d 54 4b 5f 4c 54 20 7c 7c 20 63 3d 3d 57 4f 5f  =TK_LT || c==WO_
1ea46 4c 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  LT );.  assert( 
1ea47 6f 70 21 3d 54 4b 5f 4c 45 20 7c 7c 20 63 3d 3d  op!=TK_LE || c==
1ea48 57 4f 5f 4c 45 20 29 3b 0a 20 20 61 73 73 65 72  WO_LE );.  asser
1ea49 74 28 20 6f 70 21 3d 54 4b 5f 47 54 20 7c 7c 20  t( op!=TK_GT || 
1ea4a 63 3d 3d 57 4f 5f 47 54 20 29 3b 0a 20 20 61 73  c==WO_GT );.  as
1ea4b 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f 47 45 20  sert( op!=TK_GE 
1ea4c 7c 7c 20 63 3d 3d 57 4f 5f 47 45 20 29 3b 0a 20  || c==WO_GE );. 
1ea4d 20 72 65 74 75 72 6e 20 63 3b 0a 7d 0a 0a 2f 2a   return c;.}../*
1ea4e 0a 2a 2a 20 53 65 61 72 63 68 20 66 6f 72 20 61  .** Search for a
1ea4f 20 74 65 72 6d 20 69 6e 20 74 68 65 20 57 48 45   term in the WHE
1ea50 52 45 20 63 6c 61 75 73 65 20 74 68 61 74 20 69  RE clause that i
1ea51 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 58  s of the form "X
1ea52 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 22 0a 2a 2a   <op> <expr>".**
1ea53 20 77 68 65 72 65 20 58 20 69 73 20 61 20 72 65   where X is a re
1ea54 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 69  ference to the i
1ea55 43 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20  Column of table 
1ea56 69 43 75 72 20 61 6e 64 20 3c 6f 70 3e 20 69 73  iCur and <op> is
1ea57 20 6f 6e 65 20 6f 66 0a 2a 2a 20 74 68 65 20 57   one of.** the W
1ea58 4f 5f 78 78 20 6f 70 65 72 61 74 6f 72 20 63 6f  O_xx operator co
1ea59 64 65 73 20 73 70 65 63 69 66 69 65 64 20 62 79  des specified by
1ea5a 20 74 68 65 20 6f 70 20 70 61 72 61 6d 65 74 65   the op paramete
1ea5b 72 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70  r..** Return a p
1ea5c 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 74 65  ointer to the te
1ea5d 72 6d 2e 20 20 52 65 74 75 72 6e 20 30 20 69 66  rm.  Return 0 if
1ea5e 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 73   not found..*/.s
1ea5f 74 61 74 69 63 20 57 68 65 72 65 54 65 72 6d 20  tatic WhereTerm 
1ea60 2a 66 69 6e 64 54 65 72 6d 28 0a 20 20 57 68 65  *findTerm(.  Whe
1ea61 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 20  reClause *pWC,  
1ea62 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20     /* The WHERE 
1ea63 63 6c 61 75 73 65 20 74 6f 20 62 65 20 73 65 61  clause to be sea
1ea64 72 63 68 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69  rched */.  int i
1ea65 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  Cur,            
1ea66 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65   /* Cursor numbe
1ea67 72 20 6f 66 20 4c 48 53 20 2a 2f 0a 20 20 69 6e  r of LHS */.  in
1ea68 74 20 69 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20  t iColumn,      
1ea69 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75      /* Column nu
1ea6a 6d 62 65 72 20 6f 66 20 4c 48 53 20 2a 2f 0a 20  mber of LHS */. 
1ea6b 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64   Bitmask notRead
1ea6c 79 2c 20 20 20 20 20 2f 2a 20 52 48 53 20 6d 75  y,     /* RHS mu
1ea6d 73 74 20 6e 6f 74 20 6f 76 65 72 6c 61 70 20 77  st not overlap w
1ea6e 69 74 68 20 74 68 69 73 20 6d 61 73 6b 20 2a 2f  ith this mask */
1ea6f 0a 20 20 75 31 36 20 6f 70 2c 20 20 20 20 20 20  .  u16 op,      
1ea70 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b           /* Mask
1ea71 20 6f 66 20 57 4f 5f 78 78 20 76 61 6c 75 65 73   of WO_xx values
1ea72 20 64 65 73 63 72 69 62 69 6e 67 20 6f 70 65 72   describing oper
1ea73 61 74 6f 72 20 2a 2f 0a 20 20 49 6e 64 65 78 20  ator */.  Index 
1ea74 2a 70 49 64 78 20 20 20 20 20 20 20 20 20 20 20  *pIdx           
1ea75 2f 2a 20 4d 75 73 74 20 62 65 20 63 6f 6d 70 61  /* Must be compa
1ea76 74 69 62 6c 65 20 77 69 74 68 20 74 68 69 73 20  tible with this 
1ea77 69 6e 64 65 78 2c 20 69 66 20 6e 6f 74 20 4e 55  index, if not NU
1ea78 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 57 68 65 72 65  LL */.){.  Where
1ea79 54 65 72 6d 20 2a 70 54 65 72 6d 3b 0a 20 20 69  Term *pTerm;.  i
1ea7a 6e 74 20 6b 3b 0a 20 20 66 6f 72 28 70 54 65 72  nt k;.  for(pTer
1ea7b 6d 3d 70 57 43 2d 3e 61 2c 20 6b 3d 70 57 43 2d  m=pWC->a, k=pWC-
1ea7c 3e 6e 54 65 72 6d 3b 20 6b 3b 20 6b 2d 2d 2c 20  >nTerm; k; k--, 
1ea7d 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 69 66  pTerm++){.    if
1ea7e 28 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72  ( pTerm->leftCur
1ea7f 73 6f 72 3d 3d 69 43 75 72 0a 20 20 20 20 20 20  sor==iCur.      
1ea80 20 26 26 20 28 70 54 65 72 6d 2d 3e 70 72 65 72   && (pTerm->prer
1ea81 65 71 52 69 67 68 74 20 26 20 6e 6f 74 52 65 61  eqRight & notRea
1ea82 64 79 29 3d 3d 30 0a 20 20 20 20 20 20 20 26 26  dy)==0.       &&
1ea83 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43 6f 6c 75   pTerm->leftColu
1ea84 6d 6e 3d 3d 69 43 6f 6c 75 6d 6e 0a 20 20 20 20  mn==iColumn.    
1ea85 20 20 20 26 26 20 28 70 54 65 72 6d 2d 3e 65 4f     && (pTerm->eO
1ea86 70 65 72 61 74 6f 72 20 26 20 6f 70 29 21 3d 30  perator & op)!=0
1ea87 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 66  .    ){.      if
1ea88 28 20 69 43 75 72 3e 3d 30 20 26 26 20 70 49 64  ( iCur>=0 && pId
1ea89 78 20 26 26 20 70 54 65 72 6d 2d 3e 65 4f 70 65  x && pTerm->eOpe
1ea8a 72 61 74 6f 72 21 3d 57 4f 5f 49 53 4e 55 4c 4c  rator!=WO_ISNULL
1ea8b 20 29 7b 0a 20 20 20 20 20 20 20 20 45 78 70 72   ){.        Expr
1ea8c 20 2a 70 58 20 3d 20 70 54 65 72 6d 2d 3e 70 45   *pX = pTerm->pE
1ea8d 78 70 72 3b 0a 20 20 20 20 20 20 20 20 43 6f 6c  xpr;.        Col
1ea8e 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 20  lSeq *pColl;.   
1ea8f 20 20 20 20 20 63 68 61 72 20 69 64 78 61 66 66       char idxaff
1ea90 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6a 3b  ;.        int j;
1ea91 0a 20 20 20 20 20 20 20 20 50 61 72 73 65 20 2a  .        Parse *
1ea92 70 50 61 72 73 65 20 3d 20 70 57 43 2d 3e 70 50  pParse = pWC->pP
1ea93 61 72 73 65 3b 0a 0a 20 20 20 20 20 20 20 20 69  arse;..        i
1ea94 64 78 61 66 66 20 3d 20 70 49 64 78 2d 3e 70 54  dxaff = pIdx->pT
1ea95 61 62 6c 65 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 75  able->aCol[iColu
1ea96 6d 6e 5d 2e 61 66 66 69 6e 69 74 79 3b 0a 20 20  mn].affinity;.  
1ea97 20 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 74        if( !sqlit
1ea98 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 4f  e3IndexAffinityO
1ea99 6b 28 70 58 2c 20 69 64 78 61 66 66 29 20 29 20  k(pX, idxaff) ) 
1ea9a 63 6f 6e 74 69 6e 75 65 3b 0a 0a 20 20 20 20 20  continue;..     
1ea9b 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74     /* Figure out
1ea9c 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73   the collation s
1ea9d 65 71 75 65 6e 63 65 20 72 65 71 75 69 72 65 64  equence required
1ea9e 20 66 72 6f 6d 20 61 6e 20 69 6e 64 65 78 20 66   from an index f
1ea9f 6f 72 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 74  or.        ** it
1eaa0 20 74 6f 20 62 65 20 75 73 65 66 75 6c 20 66 6f   to be useful fo
1eaa1 72 20 6f 70 74 69 6d 69 73 69 6e 67 20 65 78 70  r optimising exp
1eaa2 72 65 73 73 69 6f 6e 20 70 58 2e 20 53 74 6f 72  ression pX. Stor
1eaa3 65 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 2a  e this.        *
1eaa4 2a 20 76 61 6c 75 65 20 69 6e 20 76 61 72 69 61  * value in varia
1eaa5 62 6c 65 20 70 43 6f 6c 6c 2e 0a 20 20 20 20 20  ble pColl..     
1eaa6 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73     */.        as
1eaa7 73 65 72 74 28 70 58 2d 3e 70 4c 65 66 74 29 3b  sert(pX->pLeft);
1eaa8 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d  .        pColl =
1eaa9 20 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f   sqlite3BinaryCo
1eaaa 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 50 61  mpareCollSeq(pPa
1eaab 72 73 65 2c 20 70 58 2d 3e 70 4c 65 66 74 2c 20  rse, pX->pLeft, 
1eaac 70 58 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 20  pX->pRight);.   
1eaad 20 20 20 20 20 69 66 28 20 21 70 43 6f 6c 6c 20       if( !pColl 
1eaae 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 43 6f  ){.          pCo
1eaaf 6c 6c 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d  ll = pParse->db-
1eab0 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20 20 20  >pDfltColl;.    
1eab1 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 66      }..        f
1eab2 6f 72 28 6a 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e  or(j=0; j<pIdx->
1eab3 6e 43 6f 6c 75 6d 6e 20 26 26 20 70 49 64 78 2d  nColumn && pIdx-
1eab4 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 21 3d 69 43  >aiColumn[j]!=iC
1eab5 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 7d 0a 20 20  olumn; j++){}.  
1eab6 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6a 3c        assert( j<
1eab7 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 3b  pIdx->nColumn );
1eab8 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c  .        if( sql
1eab9 69 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f 6c  ite3StrICmp(pCol
1eaba 6c 2d 3e 7a 4e 61 6d 65 2c 20 70 49 64 78 2d 3e  l->zName, pIdx->
1eabb 61 7a 43 6f 6c 6c 5b 6a 5d 29 20 29 20 63 6f 6e  azColl[j]) ) con
1eabc 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20  tinue;.      }. 
1eabd 20 20 20 20 20 72 65 74 75 72 6e 20 70 54 65 72       return pTer
1eabe 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  m;.    }.  }.  r
1eabf 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 46  eturn 0;.}../* F
1eac0 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e 63 65  orward reference
1eac1 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20   */.static void 
1eac2 65 78 70 72 41 6e 61 6c 79 7a 65 28 53 72 63 4c  exprAnalyze(SrcL
1eac3 69 73 74 2a 2c 20 57 68 65 72 65 43 6c 61 75 73  ist*, WhereClaus
1eac4 65 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a  e*, int);../*.**
1eac5 20 43 61 6c 6c 20 65 78 70 72 41 6e 61 6c 79 7a   Call exprAnalyz
1eac6 65 20 6f 6e 20 61 6c 6c 20 74 65 72 6d 73 20 69  e on all terms i
1eac7 6e 20 61 20 57 48 45 52 45 20 63 6c 61 75 73 65  n a WHERE clause
1eac8 2e 20 20 0a 2a 2a 0a 2a 2a 0a 2a 2f 0a 73 74 61  .  .**.**.*/.sta
1eac9 74 69 63 20 76 6f 69 64 20 65 78 70 72 41 6e 61  tic void exprAna
1eaca 6c 79 7a 65 41 6c 6c 28 0a 20 20 53 72 63 4c 69  lyzeAll(.  SrcLi
1eacb 73 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 20 20  st *pTabList,   
1eacc 20 20 20 20 2f 2a 20 74 68 65 20 46 52 4f 4d 20      /* the FROM 
1eacd 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68 65 72  clause */.  Wher
1eace 65 43 6c 61 75 73 65 20 2a 70 57 43 20 20 20 20  eClause *pWC    
1eacf 20 20 20 20 20 2f 2a 20 74 68 65 20 57 48 45 52       /* the WHER
1ead0 45 20 63 6c 61 75 73 65 20 74 6f 20 62 65 20 61  E clause to be a
1ead1 6e 61 6c 79 7a 65 64 20 2a 2f 0a 29 7b 0a 20 20  nalyzed */.){.  
1ead2 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 70  int i;.  for(i=p
1ead3 57 43 2d 3e 6e 54 65 72 6d 2d 31 3b 20 69 3e 3d  WC->nTerm-1; i>=
1ead4 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 65 78 70  0; i--){.    exp
1ead5 72 41 6e 61 6c 79 7a 65 28 70 54 61 62 4c 69 73  rAnalyze(pTabLis
1ead6 74 2c 20 70 57 43 2c 20 69 29 3b 0a 20 20 7d 0a  t, pWC, i);.  }.
1ead7 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
1ead8 45 5f 4f 4d 49 54 5f 4c 49 4b 45 5f 4f 50 54 49  E_OMIT_LIKE_OPTI
1ead9 4d 49 5a 41 54 49 4f 4e 0a 2f 2a 0a 2a 2a 20 43  MIZATION./*.** C
1eada 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74  heck to see if t
1eadb 68 65 20 67 69 76 65 6e 20 65 78 70 72 65 73 73  he given express
1eadc 69 6f 6e 20 69 73 20 61 20 4c 49 4b 45 20 6f 72  ion is a LIKE or
1eadd 20 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72 20 74   GLOB operator t
1eade 68 61 74 0a 2a 2a 20 63 61 6e 20 62 65 20 6f 70  hat.** can be op
1eadf 74 69 6d 69 7a 65 64 20 75 73 69 6e 67 20 69 6e  timized using in
1eae0 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61  equality constra
1eae1 69 6e 74 73 2e 20 20 52 65 74 75 72 6e 20 54 52  ints.  Return TR
1eae2 55 45 20 69 66 20 69 74 20 69 73 0a 2a 2a 20 73  UE if it is.** s
1eae3 6f 20 61 6e 64 20 66 61 6c 73 65 20 69 66 20 6e  o and false if n
1eae4 6f 74 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 72 64  ot..**.** In ord
1eae5 65 72 20 66 6f 72 20 74 68 65 20 6f 70 65 72 61  er for the opera
1eae6 74 6f 72 20 74 6f 20 62 65 20 6f 70 74 69 6d 69  tor to be optimi
1eae7 7a 69 62 6c 65 2c 20 74 68 65 20 52 48 53 20 6d  zible, the RHS m
1eae8 75 73 74 20 62 65 20 61 20 73 74 72 69 6e 67 0a  ust be a string.
1eae9 2a 2a 20 6c 69 74 65 72 61 6c 20 74 68 61 74 20  ** literal that 
1eaea 64 6f 65 73 20 6e 6f 74 20 62 65 67 69 6e 20 77  does not begin w
1eaeb 69 74 68 20 61 20 77 69 6c 64 63 61 72 64 2e 20  ith a wildcard. 
1eaec 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20   .*/.static int 
1eaed 69 73 4c 69 6b 65 4f 72 47 6c 6f 62 28 0a 20 20  isLikeOrGlob(.  
1eaee 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
1eaef 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
1eaf0 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78  e */.  Expr *pEx
1eaf1 70 72 2c 20 20 20 20 20 20 2f 2a 20 54 65 73 74  pr,      /* Test
1eaf2 20 74 68 69 73 20 65 78 70 72 65 73 73 69 6f 6e   this expression
1eaf3 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 50 61 74   */.  int *pnPat
1eaf4 74 65 72 6e 2c 20 20 20 2f 2a 20 4e 75 6d 62 65  tern,   /* Numbe
1eaf5 72 20 6f 66 20 6e 6f 6e 2d 77 69 6c 64 63 61 72  r of non-wildcar
1eaf6 64 20 70 72 65 66 69 78 20 63 68 61 72 61 63 74  d prefix charact
1eaf7 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69  ers */.  int *pi
1eaf8 73 43 6f 6d 70 6c 65 74 65 20 20 2f 2a 20 54 72  sComplete  /* Tr
1eaf9 75 65 20 69 66 20 74 68 65 20 6f 6e 6c 79 20 77  ue if the only w
1eafa 69 6c 64 63 61 72 64 20 69 73 20 25 20 69 6e 20  ildcard is % in 
1eafb 74 68 65 20 6c 61 73 74 20 63 68 61 72 61 63 74  the last charact
1eafc 65 72 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74  er */.){.  const
1eafd 20 63 68 61 72 20 2a 7a 3b 0a 20 20 45 78 70 72   char *z;.  Expr
1eafe 20 2a 70 52 69 67 68 74 2c 20 2a 70 4c 65 66 74   *pRight, *pLeft
1eaff 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c  ;.  ExprList *pL
1eb00 69 73 74 3b 0a 20 20 69 6e 74 20 63 2c 20 63 6e  ist;.  int c, cn
1eb01 74 3b 0a 20 20 69 6e 74 20 6e 6f 43 61 73 65 3b  t;.  int noCase;
1eb02 0a 20 20 63 68 61 72 20 77 63 5b 33 5d 3b 0a 20  .  char wc[3];. 
1eb03 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b   CollSeq *pColl;
1eb04 0a 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33  ..  if( !sqlite3
1eb05 49 73 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 28 64  IsLikeFunction(d
1eb06 62 2c 20 70 45 78 70 72 2c 20 26 6e 6f 43 61 73  b, pExpr, &noCas
1eb07 65 2c 20 77 63 29 20 29 7b 0a 20 20 20 20 72 65  e, wc) ){.    re
1eb08 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4c  turn 0;.  }.  pL
1eb09 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69  ist = pExpr->pLi
1eb0a 73 74 3b 0a 20 20 70 52 69 67 68 74 20 3d 20 70  st;.  pRight = p
1eb0b 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72  List->a[0].pExpr
1eb0c 3b 0a 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e  ;.  if( pRight->
1eb0d 6f 70 21 3d 54 4b 5f 53 54 52 49 4e 47 20 29 7b  op!=TK_STRING ){
1eb0e 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
1eb0f 20 7d 0a 20 20 70 4c 65 66 74 20 3d 20 70 4c 69   }.  pLeft = pLi
1eb10 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 3b 0a  st->a[1].pExpr;.
1eb11 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 6f 70 21    if( pLeft->op!
1eb12 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20  =TK_COLUMN ){.  
1eb13 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
1eb14 20 20 70 43 6f 6c 6c 20 3d 20 70 4c 65 66 74 2d    pColl = pLeft-
1eb15 3e 70 43 6f 6c 6c 3b 0a 20 20 69 66 28 20 70 43  >pColl;.  if( pC
1eb16 6f 6c 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a  oll==0 ){.    /*
1eb17 20 54 4f 44 4f 3a 20 43 6f 76 65 72 61 67 65 20   TODO: Coverage 
1eb18 74 65 73 74 69 6e 67 20 64 6f 65 73 6e 27 74 20  testing doesn't 
1eb19 67 65 74 20 74 68 69 73 20 63 61 73 65 2e 20 49  get this case. I
1eb1a 73 20 69 74 20 61 63 74 75 61 6c 6c 79 20 70 6f  s it actually po
1eb1b 73 73 69 62 6c 65 0a 20 20 20 20 2a 2a 20 66 6f  ssible.    ** fo
1eb1c 72 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  r an expression 
1eb1d 6f 66 20 74 79 70 65 20 54 4b 5f 43 4f 4c 55 4d  of type TK_COLUM
1eb1e 4e 20 74 6f 20 6e 6f 74 20 68 61 76 65 20 61 6e  N to not have an
1eb1f 20 61 73 73 69 67 6e 65 64 20 63 6f 6c 6c 61 74   assigned collat
1eb20 69 6f 6e 20 0a 20 20 20 20 2a 2a 20 73 65 71 75  ion .    ** sequ
1eb21 65 6e 63 65 20 61 74 20 74 68 69 73 20 70 6f 69  ence at this poi
1eb22 6e 74 3f 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70  nt?.    */.    p
1eb23 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74  Coll = db->pDflt
1eb24 43 6f 6c 6c 3b 0a 20 20 7d 0a 20 20 69 66 28 20  Coll;.  }.  if( 
1eb25 28 70 43 6f 6c 6c 2d 3e 74 79 70 65 21 3d 53 51  (pColl->type!=SQ
1eb26 4c 49 54 45 5f 43 4f 4c 4c 5f 42 49 4e 41 52 59  LITE_COLL_BINARY
1eb27 20 7c 7c 20 6e 6f 43 61 73 65 29 20 26 26 0a 20   || noCase) &&. 
1eb28 20 20 20 20 20 28 70 43 6f 6c 6c 2d 3e 74 79 70       (pColl->typ
1eb29 65 21 3d 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 4e  e!=SQLITE_COLL_N
1eb2a 4f 43 41 53 45 20 7c 7c 20 21 6e 6f 43 61 73 65  OCASE || !noCase
1eb2b 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
1eb2c 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  0;.  }.  sqlite3
1eb2d 44 65 71 75 6f 74 65 45 78 70 72 28 70 52 69 67  DequoteExpr(pRig
1eb2e 68 74 29 3b 0a 20 20 7a 20 3d 20 28 63 68 61 72  ht);.  z = (char
1eb2f 20 2a 29 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e   *)pRight->token
1eb30 2e 7a 3b 0a 20 20 66 6f 72 28 63 6e 74 3d 30 3b  .z;.  for(cnt=0;
1eb31 20 28 63 3d 7a 5b 63 6e 74 5d 29 21 3d 30 20 26   (c=z[cnt])!=0 &
1eb32 26 20 63 21 3d 77 63 5b 30 5d 20 26 26 20 63 21  & c!=wc[0] && c!
1eb33 3d 77 63 5b 31 5d 20 26 26 20 63 21 3d 77 63 5b  =wc[1] && c!=wc[
1eb34 32 5d 3b 20 63 6e 74 2b 2b 29 7b 7d 0a 20 20 69  2]; cnt++){}.  i
1eb35 66 28 20 63 6e 74 3d 3d 30 20 7c 7c 20 32 35 35  f( cnt==0 || 255
1eb36 3d 3d 28 75 38 29 7a 5b 63 6e 74 5d 20 29 7b 0a  ==(u8)z[cnt] ){.
1eb37 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
1eb38 7d 0a 20 20 2a 70 69 73 43 6f 6d 70 6c 65 74 65  }.  *pisComplete
1eb39 20 3d 20 7a 5b 63 6e 74 5d 3d 3d 77 63 5b 30 5d   = z[cnt]==wc[0]
1eb3a 20 26 26 20 7a 5b 63 6e 74 2b 31 5d 3d 3d 30 3b   && z[cnt+1]==0;
1eb3b 0a 20 20 2a 70 6e 50 61 74 74 65 72 6e 20 3d 20  .  *pnPattern = 
1eb3c 63 6e 74 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b  cnt;.  return 1;
1eb3d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
1eb3e 49 54 45 5f 4f 4d 49 54 5f 4c 49 4b 45 5f 4f 50  ITE_OMIT_LIKE_OP
1eb3f 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 0a  TIMIZATION */...
1eb40 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1eb41 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1eb42 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20  ./*.** Check to 
1eb43 73 65 65 20 69 66 20 74 68 65 20 67 69 76 65 6e  see if the given
1eb44 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6f   expression is o
1eb45 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a  f the form.**.**
1eb46 20 20 20 20 20 20 20 20 20 63 6f 6c 75 6d 6e 20           column 
1eb47 4d 41 54 43 48 20 65 78 70 72 0a 2a 2a 0a 2a 2a  MATCH expr.**.**
1eb48 20 49 66 20 69 74 20 69 73 20 74 68 65 6e 20 72   If it is then r
1eb49 65 74 75 72 6e 20 54 52 55 45 2e 20 20 49 66 20  eturn TRUE.  If 
1eb4a 6e 6f 74 2c 20 72 65 74 75 72 6e 20 46 41 4c 53  not, return FALS
1eb4b 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  E..*/.static int
1eb4c 20 69 73 4d 61 74 63 68 4f 66 43 6f 6c 75 6d 6e   isMatchOfColumn
1eb4d 28 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 20  (.  Expr *pExpr 
1eb4e 20 20 20 20 20 2f 2a 20 54 65 73 74 20 74 68 69       /* Test thi
1eb4f 73 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a  s expression */.
1eb50 29 7b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  ){.  ExprList *p
1eb51 4c 69 73 74 3b 0a 0a 20 20 69 66 28 20 70 45 78  List;..  if( pEx
1eb52 70 72 2d 3e 6f 70 21 3d 54 4b 5f 46 55 4e 43 54  pr->op!=TK_FUNCT
1eb53 49 4f 4e 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ION ){.    retur
1eb54 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  n 0;.  }.  if( p
1eb55 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 21 3d 35  Expr->token.n!=5
1eb56 20 7c 7c 0a 20 20 20 20 20 20 20 73 71 6c 69 74   ||.       sqlit
1eb57 65 33 53 74 72 4e 49 43 6d 70 28 28 63 6f 6e 73  e3StrNICmp((cons
1eb58 74 20 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74  t char*)pExpr->t
1eb59 6f 6b 65 6e 2e 7a 2c 22 6d 61 74 63 68 22 2c 35  oken.z,"match",5
1eb5a 29 21 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  )!=0 ){.    retu
1eb5b 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4c 69 73  rn 0;.  }.  pLis
1eb5c 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74  t = pExpr->pList
1eb5d 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 6e  ;.  if( pList->n
1eb5e 45 78 70 72 21 3d 32 20 29 7b 0a 20 20 20 20 72  Expr!=2 ){.    r
1eb5f 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69  eturn 0;.  }.  i
1eb60 66 28 20 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70  f( pList->a[1].p
1eb61 45 78 70 72 2d 3e 6f 70 20 21 3d 20 54 4b 5f 43  Expr->op != TK_C
1eb62 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 72 65 74  OLUMN ){.    ret
1eb63 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74  urn 0;.  }.  ret
1eb64 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 20  urn 1;.}.#endif 
1eb65 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  /* SQLITE_OMIT_V
1eb66 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a  IRTUALTABLE */..
1eb67 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 42 61  /*.** If the pBa
1eb68 73 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72  se expression or
1eb69 69 67 69 6e 61 74 65 64 20 69 6e 20 74 68 65 20  iginated in the 
1eb6a 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75  ON or USING clau
1eb6b 73 65 20 6f 66 0a 2a 2a 20 61 20 6a 6f 69 6e 2c  se of.** a join,
1eb6c 20 74 68 65 6e 20 74 72 61 6e 73 66 65 72 20 74   then transfer t
1eb6d 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 6d  he appropriate m
1eb6e 61 72 6b 69 6e 67 73 20 6f 76 65 72 20 74 6f 20  arkings over to 
1eb6f 64 65 72 69 76 65 64 2e 0a 2a 2f 0a 73 74 61 74  derived..*/.stat
1eb70 69 63 20 76 6f 69 64 20 74 72 61 6e 73 66 65 72  ic void transfer
1eb71 4a 6f 69 6e 4d 61 72 6b 69 6e 67 73 28 45 78 70  JoinMarkings(Exp
1eb72 72 20 2a 70 44 65 72 69 76 65 64 2c 20 45 78 70  r *pDerived, Exp
1eb73 72 20 2a 70 42 61 73 65 29 7b 0a 20 20 70 44 65  r *pBase){.  pDe
1eb74 72 69 76 65 64 2d 3e 66 6c 61 67 73 20 7c 3d 20  rived->flags |= 
1eb75 70 42 61 73 65 2d 3e 66 6c 61 67 73 20 26 20 45  pBase->flags & E
1eb76 50 5f 46 72 6f 6d 4a 6f 69 6e 3b 0a 20 20 70 44  P_FromJoin;.  pD
1eb77 65 72 69 76 65 64 2d 3e 69 52 69 67 68 74 4a 6f  erived->iRightJo
1eb78 69 6e 54 61 62 6c 65 20 3d 20 70 42 61 73 65 2d  inTable = pBase-
1eb79 3e 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65  >iRightJoinTable
1eb7a 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65  ;.}..#if !define
1eb7b 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52  d(SQLITE_OMIT_OR
1eb7c 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 29 20 26  _OPTIMIZATION) &
1eb7d 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  & !defined(SQLIT
1eb7e 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29  E_OMIT_SUBQUERY)
1eb7f 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52  ./*.** Return TR
1eb80 55 45 20 69 66 20 74 68 65 20 67 69 76 65 6e 20  UE if the given 
1eb81 74 65 72 6d 20 6f 66 20 61 6e 20 4f 52 20 63 6c  term of an OR cl
1eb82 61 75 73 65 20 63 61 6e 20 62 65 20 63 6f 6e 76  ause can be conv
1eb83 65 72 74 65 64 0a 2a 2a 20 69 6e 74 6f 20 61 6e  erted.** into an
1eb84 20 49 4e 20 63 6c 61 75 73 65 2e 20 20 54 68 65   IN clause.  The
1eb85 20 69 43 75 72 73 6f 72 20 61 6e 64 20 69 43 6f   iCursor and iCo
1eb86 6c 75 6d 6e 20 64 65 66 69 6e 65 20 74 68 65 20  lumn define the 
1eb87 6c 65 66 74 2d 68 61 6e 64 0a 2a 2a 20 73 69 64  left-hand.** sid
1eb88 65 20 6f 66 20 74 68 65 20 49 4e 20 63 6c 61 75  e of the IN clau
1eb89 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  se..**.** The co
1eb8a 6e 74 65 78 74 20 69 73 20 74 68 61 74 20 77 65  ntext is that we
1eb8b 20 68 61 76 65 20 6d 75 6c 74 69 70 6c 65 20 4f   have multiple O
1eb8c 52 2d 63 6f 6e 6e 65 63 74 65 64 20 65 71 75 61  R-connected equa
1eb8d 6c 69 74 79 20 74 65 72 6d 73 0a 2a 2a 20 6c 69  lity terms.** li
1eb8e 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20  ke this:.**.**  
1eb8f 20 20 20 20 20 20 20 20 20 61 3d 3c 65 78 70 72           a=<expr
1eb90 31 3e 20 4f 52 20 20 61 3d 3c 65 78 70 72 32 3e  1> OR  a=<expr2>
1eb91 20 4f 52 20 62 3d 3c 65 78 70 72 33 3e 20 20 4f   OR b=<expr3>  O
1eb92 52 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  R ....**.** The 
1eb93 70 4f 72 54 65 72 6d 20 69 6e 70 75 74 20 74 6f  pOrTerm input to
1eb94 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6f   this routine co
1eb95 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 61 20 73  rresponds to a s
1eb96 69 6e 67 6c 65 20 74 65 72 6d 20 6f 66 0a 2a 2a  ingle term of.**
1eb97 20 74 68 69 73 20 4f 52 20 63 6c 61 75 73 65 2e   this OR clause.
1eb98 20 20 49 6e 20 6f 72 64 65 72 20 66 6f 72 20 74    In order for t
1eb99 68 65 20 74 65 72 6d 20 74 6f 20 62 65 20 61 20  he term to be a 
1eb9a 63 6f 6e 64 69 64 61 74 65 20 66 6f 72 0a 2a 2a  condidate for.**
1eb9b 20 63 6f 6e 76 65 72 73 69 6f 6e 20 74 6f 20 61   conversion to a
1eb9c 6e 20 49 4e 20 6f 70 65 72 61 74 6f 72 2c 20 74  n IN operator, t
1eb9d 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 75 73  he following mus
1eb9e 74 20 62 65 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a  t be true:.**.**
1eb9f 20 20 20 20 20 2a 20 20 54 68 65 20 6c 65 66 74       *  The left
1eba0 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74 68  -hand side of th
1eba1 65 20 74 65 72 6d 20 6d 75 73 74 20 62 65 20 74  e term must be t
1eba2 68 65 20 63 6f 6c 75 6d 6e 20 77 68 69 63 68 0a  he column which.
1eba3 2a 2a 20 20 20 20 20 20 20 20 69 73 20 69 64 65  **        is ide
1eba4 6e 74 69 66 69 65 64 20 62 79 20 69 43 75 72 73  ntified by iCurs
1eba5 6f 72 20 61 6e 64 20 69 43 6f 6c 75 6d 6e 2e 0a  or and iColumn..
1eba6 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 20 49 66 20  **.**     *  If 
1eba7 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73  the right-hand s
1eba8 69 64 65 20 69 73 20 61 6c 73 6f 20 61 20 63 6f  ide is also a co
1eba9 6c 75 6d 6e 2c 20 74 68 65 6e 20 74 68 65 20 61  lumn, then the a
1ebaa 66 66 69 6e 69 74 69 65 73 0a 2a 2a 20 20 20 20  ffinities.**    
1ebab 20 20 20 20 6f 66 20 62 6f 74 68 20 72 69 67 68      of both righ
1ebac 74 20 61 6e 64 20 6c 65 66 74 20 73 69 64 65 73  t and left sides
1ebad 20 6d 75 73 74 20 62 65 20 73 75 63 68 20 74 68   must be such th
1ebae 61 74 20 6e 6f 20 74 79 70 65 0a 2a 2a 20 20 20  at no type.**   
1ebaf 20 20 20 20 20 63 6f 6e 76 65 72 73 69 6f 6e 73       conversions
1ebb0 20 61 72 65 20 72 65 71 75 69 72 65 64 20 6f 6e   are required on
1ebb1 20 74 68 65 20 72 69 67 68 74 2e 20 20 28 54 69   the right.  (Ti
1ebb2 63 6b 65 74 20 23 32 32 34 39 29 0a 2a 2a 0a 2a  cket #2249).**.*
1ebb3 2a 20 49 66 20 62 6f 74 68 20 6f 66 20 74 68 65  * If both of the
1ebb4 73 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 72  se conditions ar
1ebb5 65 20 74 72 75 65 2c 20 74 68 65 6e 20 72 65 74  e true, then ret
1ebb6 75 72 6e 20 74 72 75 65 2e 20 20 4f 74 68 65 72  urn true.  Other
1ebb7 77 69 73 65 0a 2a 2a 20 72 65 74 75 72 6e 20 66  wise.** return f
1ebb8 61 6c 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  alse..*/.static 
1ebb9 69 6e 74 20 6f 72 54 65 72 6d 49 73 4f 70 74 43  int orTermIsOptC
1ebba 61 6e 64 69 64 61 74 65 28 57 68 65 72 65 54 65  andidate(WhereTe
1ebbb 72 6d 20 2a 70 4f 72 54 65 72 6d 2c 20 69 6e 74  rm *pOrTerm, int
1ebbc 20 69 43 75 72 73 6f 72 2c 20 69 6e 74 20 69 43   iCursor, int iC
1ebbd 6f 6c 75 6d 6e 29 7b 0a 20 20 69 6e 74 20 61 66  olumn){.  int af
1ebbe 66 4c 65 66 74 2c 20 61 66 66 52 69 67 68 74 3b  fLeft, affRight;
1ebbf 0a 20 20 61 73 73 65 72 74 28 20 70 4f 72 54 65  .  assert( pOrTe
1ebc0 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57  rm->eOperator==W
1ebc1 4f 5f 45 51 20 29 3b 0a 20 20 69 66 28 20 70 4f  O_EQ );.  if( pO
1ebc2 72 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f  rTerm->leftCurso
1ebc3 72 21 3d 69 43 75 72 73 6f 72 20 29 7b 0a 20 20  r!=iCursor ){.  
1ebc4 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
1ebc5 20 20 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 6c    if( pOrTerm->l
1ebc6 65 66 74 43 6f 6c 75 6d 6e 21 3d 69 43 6f 6c 75  eftColumn!=iColu
1ebc7 6d 6e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  mn ){.    return
1ebc8 20 30 3b 0a 20 20 7d 0a 20 20 61 66 66 52 69 67   0;.  }.  affRig
1ebc9 68 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ht = sqlite3Expr
1ebca 41 66 66 69 6e 69 74 79 28 70 4f 72 54 65 72 6d  Affinity(pOrTerm
1ebcb 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29  ->pExpr->pRight)
1ebcc 3b 0a 20 20 69 66 28 20 61 66 66 52 69 67 68 74  ;.  if( affRight
1ebcd 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
1ebce 6e 20 31 3b 0a 20 20 7d 0a 20 20 61 66 66 4c 65  n 1;.  }.  affLe
1ebcf 66 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ft = sqlite3Expr
1ebd0 41 66 66 69 6e 69 74 79 28 70 4f 72 54 65 72 6d  Affinity(pOrTerm
1ebd1 2d 3e 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b  ->pExpr->pLeft);
1ebd2 0a 20 20 69 66 28 20 61 66 66 52 69 67 68 74 21  .  if( affRight!
1ebd3 3d 61 66 66 4c 65 66 74 20 29 7b 0a 20 20 20 20  =affLeft ){.    
1ebd4 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
1ebd5 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a  return 1;.}../*.
1ebd6 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69  ** Return true i
1ebd7 66 20 74 68 65 20 67 69 76 65 6e 20 74 65 72 6d  f the given term
1ebd8 20 6f 66 20 61 6e 20 4f 52 20 63 6c 61 75 73 65   of an OR clause
1ebd9 20 63 61 6e 20 62 65 20 69 67 6e 6f 72 65 64 20   can be ignored 
1ebda 64 75 72 69 6e 67 0a 2a 2a 20 61 20 63 68 65 63  during.** a chec
1ebdb 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 61  k to make sure a
1ebdc 6c 6c 20 4f 52 20 74 65 72 6d 73 20 61 72 65 20  ll OR terms are 
1ebdd 63 61 6e 64 69 64 61 74 65 73 20 66 6f 72 20 6f  candidates for o
1ebde 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 0a 2a 2a 20  ptimization..** 
1ebdf 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
1ebe0 72 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 61  return true if a
1ebe1 20 63 61 6c 6c 20 74 6f 20 74 68 65 20 6f 72 54   call to the orT
1ebe2 65 72 6d 49 73 4f 70 74 43 61 6e 64 69 64 61 74  ermIsOptCandidat
1ebe3 65 28 29 0a 2a 2a 20 61 62 6f 76 65 20 72 65 74  e().** above ret
1ebe4 75 72 6e 65 64 20 66 61 6c 73 65 20 62 75 74 20  urned false but 
1ebe5 69 74 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73  it is not necess
1ebe6 61 72 79 20 74 6f 20 64 69 73 71 75 61 6c 69 66  ary to disqualif
1ebe7 79 20 74 68 65 0a 2a 2a 20 6f 70 74 69 6d 69 7a  y the.** optimiz
1ebe8 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 75 70  ation..**.** Sup
1ebe9 70 6f 73 65 20 74 68 65 20 6f 72 69 67 69 6e 61  pose the origina
1ebea 6c 20 4f 52 20 70 68 72 61 73 65 20 77 61 73 20  l OR phrase was 
1ebeb 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  this:.**.**     
1ebec 20 20 20 20 20 20 61 3d 34 20 20 4f 52 20 20 61        a=4  OR  a
1ebed 3d 31 31 20 20 4f 52 20 20 61 3d 62 0a 2a 2a 0a  =11  OR  a=b.**.
1ebee 2a 2a 20 44 75 72 69 6e 67 20 61 6e 61 6c 79 73  ** During analys
1ebef 69 73 2c 20 74 68 65 20 74 68 69 72 64 20 74 65  is, the third te
1ebf0 72 6d 20 67 65 74 73 20 66 6c 69 70 70 65 64 20  rm gets flipped 
1ebf1 61 72 6f 75 6e 64 20 61 6e 64 20 64 75 70 6c 69  around and dupli
1ebf2 63 61 74 65 0a 2a 2a 20 73 6f 20 74 68 61 74 20  cate.** so that 
1ebf3 77 65 20 61 72 65 20 6c 65 66 74 20 77 69 74 68  we are left with
1ebf4 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   this:.**.**    
1ebf5 20 20 20 20 20 20 20 61 3d 34 20 20 4f 52 20 20         a=4  OR  
1ebf6 61 3d 31 31 20 20 4f 52 20 20 61 3d 62 20 20 4f  a=11  OR  a=b  O
1ebf7 52 20 20 62 3d 61 0a 2a 2a 0a 2a 2a 20 53 69 6e  R  b=a.**.** Sin
1ebf8 63 65 20 74 68 65 20 6c 61 73 74 20 74 77 6f 20  ce the last two 
1ebf9 74 65 72 6d 73 20 61 72 65 20 64 75 70 6c 69 63  terms are duplic
1ebfa 61 74 65 73 2c 20 6f 6e 6c 79 20 6f 6e 65 20 6f  ates, only one o
1ebfb 66 20 74 68 65 6d 0a 2a 2a 20 68 61 73 20 74 6f  f them.** has to
1ebfc 20 71 75 61 6c 69 66 79 20 69 6e 20 6f 72 64 65   qualify in orde
1ebfd 72 20 66 6f 72 20 74 68 65 20 77 68 6f 6c 65 20  r for the whole 
1ebfe 70 68 72 61 73 65 20 74 6f 20 71 75 61 6c 69 66  phrase to qualif
1ebff 79 2e 20 20 57 68 65 6e 0a 2a 2a 20 74 68 69 73  y.  When.** this
1ec00 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
1ec01 65 64 2c 20 77 65 20 6b 6e 6f 77 20 74 68 61 74  ed, we know that
1ec02 20 70 4f 72 54 65 72 6d 20 64 69 64 20 6e 6f 74   pOrTerm did not
1ec03 20 71 75 61 6c 69 66 79 2e 0a 2a 2a 20 54 68 69   qualify..** Thi
1ec04 73 20 72 6f 75 74 69 6e 65 20 6d 65 72 65 6c 79  s routine merely
1ec05 20 63 68 65 63 6b 73 20 74 6f 20 73 65 65 20 69   checks to see i
1ec06 66 20 70 4f 72 54 65 72 6d 20 68 61 73 20 61 20  f pOrTerm has a 
1ec07 64 75 70 6c 69 63 61 74 65 20 74 68 61 74 0a 2a  duplicate that.*
1ec08 2a 20 6d 69 67 68 74 20 71 75 61 6c 69 66 79 2e  * might qualify.
1ec09 20 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20    If there is a 
1ec0a 64 75 70 6c 69 63 61 74 65 20 74 68 61 74 20 68  duplicate that h
1ec0b 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 0a  as not yet been.
1ec0c 2a 2a 20 64 69 73 71 75 61 6c 69 66 69 65 64 2c  ** disqualified,
1ec0d 20 74 68 65 6e 20 72 65 74 75 72 6e 20 74 72 75   then return tru
1ec0e 65 2e 20 20 49 66 20 74 68 65 72 65 20 61 72 65  e.  If there are
1ec0f 20 6e 6f 20 64 75 70 6c 69 63 61 74 65 73 2c 20   no duplicates, 
1ec10 6f 72 0a 2a 2a 20 74 68 65 20 64 75 70 6c 69 63  or.** the duplic
1ec11 61 74 65 20 68 61 73 20 61 6c 73 6f 20 62 65 65  ate has also bee
1ec12 6e 20 64 69 73 71 75 61 6c 69 66 65 64 2c 20 72  n disqualifed, r
1ec13 65 74 75 72 6e 20 66 61 6c 73 65 2e 0a 2a 2f 0a  eturn false..*/.
1ec14 73 74 61 74 69 63 20 69 6e 74 20 6f 72 54 65 72  static int orTer
1ec15 6d 48 61 73 4f 6b 44 75 70 6c 69 63 61 74 65 28  mHasOkDuplicate(
1ec16 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 4f 72  WhereClause *pOr
1ec17 2c 20 57 68 65 72 65 54 65 72 6d 20 2a 70 4f 72  , WhereTerm *pOr
1ec18 54 65 72 6d 29 7b 0a 20 20 69 66 28 20 70 4f 72  Term){.  if( pOr
1ec19 54 65 72 6d 2d 3e 66 6c 61 67 73 20 26 20 54 45  Term->flags & TE
1ec1a 52 4d 5f 43 4f 50 49 45 44 20 29 7b 0a 20 20 20  RM_COPIED ){.   
1ec1b 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65 20   /* This is the 
1ec1c 6f 72 69 67 69 6e 61 6c 20 74 65 72 6d 2e 20 20  original term.  
1ec1d 54 68 65 20 64 75 70 6c 69 63 61 74 65 20 69 73  The duplicate is
1ec1e 20 74 6f 20 74 68 65 20 6c 65 66 74 20 68 61 64   to the left had
1ec1f 0a 20 20 20 20 2a 2a 20 68 61 73 20 6e 6f 74 20  .    ** has not 
1ec20 79 65 74 20 62 65 65 6e 20 61 6e 61 6c 79 7a 65  yet been analyze
1ec21 64 20 61 6e 64 20 74 68 75 73 20 68 61 73 20 6e  d and thus has n
1ec22 6f 74 20 79 65 74 20 62 65 65 6e 20 64 69 73 71  ot yet been disq
1ec23 75 61 6c 69 66 69 65 64 2e 20 2a 2f 0a 20 20 20  ualified. */.   
1ec24 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20   return 1;.  }. 
1ec25 20 69 66 28 20 28 70 4f 72 54 65 72 6d 2d 3e 66   if( (pOrTerm->f
1ec26 6c 61 67 73 20 26 20 54 45 52 4d 5f 56 49 52 54  lags & TERM_VIRT
1ec27 55 41 4c 29 21 3d 30 0a 20 20 20 20 20 26 26 20  UAL)!=0.     && 
1ec28 28 70 4f 72 2d 3e 61 5b 70 4f 72 54 65 72 6d 2d  (pOr->a[pOrTerm-
1ec29 3e 69 50 61 72 65 6e 74 5d 2e 66 6c 61 67 73 20  >iParent].flags 
1ec2a 26 20 54 45 52 4d 5f 4f 52 5f 4f 4b 29 21 3d 30  & TERM_OR_OK)!=0
1ec2b 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20   ){.    /* This 
1ec2c 69 73 20 61 20 64 75 70 6c 69 63 61 74 65 20 74  is a duplicate t
1ec2d 65 72 6d 2e 20 20 54 68 65 20 6f 72 69 67 69 6e  erm.  The origin
1ec2e 61 6c 20 71 75 61 6c 69 66 69 65 64 20 73 6f 20  al qualified so 
1ec2f 74 68 69 73 20 6f 6e 65 0a 20 20 20 20 2a 2a 20  this one.    ** 
1ec30 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 74 6f  does not have to
1ec31 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20  . */.    return 
1ec32 31 3b 0a 20 20 7d 0a 20 20 2f 2a 20 54 68 69 73  1;.  }.  /* This
1ec33 20 69 73 20 65 69 74 68 65 72 20 61 20 73 69 6e   is either a sin
1ec34 67 6c 65 74 6f 6e 20 74 65 72 6d 20 6f 72 20 65  gleton term or e
1ec35 6c 73 65 20 69 74 20 69 73 20 61 20 64 75 70 6c  lse it is a dupl
1ec36 69 63 61 74 65 20 66 6f 72 0a 20 20 2a 2a 20 77  icate for.  ** w
1ec37 68 69 63 68 20 74 68 65 20 6f 72 69 67 69 6e 61  hich the origina
1ec38 6c 20 64 69 64 20 6e 6f 74 20 71 75 61 6c 69 66  l did not qualif
1ec39 79 2e 20 20 45 69 74 68 65 72 20 77 61 79 20 77  y.  Either way w
1ec3a 65 20 61 72 65 20 64 6f 6e 65 20 66 6f 72 2e 20  e are done for. 
1ec3b 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d  */.  return 0;.}
1ec3c 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49  .#endif /* !SQLI
1ec3d 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d  TE_OMIT_OR_OPTIM
1ec3e 49 5a 41 54 49 4f 4e 20 26 26 20 21 53 51 4c 49  IZATION && !SQLI
1ec3f 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59  TE_OMIT_SUBQUERY
1ec40 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69   */../*.** The i
1ec41 6e 70 75 74 20 74 6f 20 74 68 69 73 20 72 6f 75  nput to this rou
1ec42 74 69 6e 65 20 69 73 20 61 6e 20 57 68 65 72 65  tine is an Where
1ec43 54 65 72 6d 20 73 74 72 75 63 74 75 72 65 20 77  Term structure w
1ec44 69 74 68 20 6f 6e 6c 79 20 74 68 65 0a 2a 2a 20  ith only the.** 
1ec45 22 70 45 78 70 72 22 20 66 69 65 6c 64 20 66 69  "pExpr" field fi
1ec46 6c 6c 65 64 20 69 6e 2e 20 20 54 68 65 20 6a 6f  lled in.  The jo
1ec47 62 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e  b of this routin
1ec48 65 20 69 73 20 74 6f 20 61 6e 61 6c 79 7a 65 20  e is to analyze 
1ec49 74 68 65 0a 2a 2a 20 73 75 62 65 78 70 72 65 73  the.** subexpres
1ec4a 73 69 6f 6e 20 61 6e 64 20 70 6f 70 75 6c 61 74  sion and populat
1ec4b 65 20 61 6c 6c 20 74 68 65 20 6f 74 68 65 72 20  e all the other 
1ec4c 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 57 68  fields of the Wh
1ec4d 65 72 65 54 65 72 6d 0a 2a 2a 20 73 74 72 75 63  ereTerm.** struc
1ec4e 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ture..**.** If t
1ec4f 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
1ec50 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3c 65   of the form "<e
1ec51 78 70 72 3e 20 3c 6f 70 3e 20 58 22 20 69 74 20  xpr> <op> X" it 
1ec52 67 65 74 73 20 63 6f 6d 6d 75 74 65 64 0a 2a 2a  gets commuted.**
1ec53 20 74 6f 20 74 68 65 20 73 74 61 6e 64 61 72 64   to the standard
1ec54 20 66 6f 72 6d 20 6f 66 20 22 58 20 3c 6f 70 3e   form of "X <op>
1ec55 20 3c 65 78 70 72 3e 22 2e 20 20 49 66 20 74 68   <expr>".  If th
1ec56 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
1ec57 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 72 6d 20 22  of.** the form "
1ec58 58 20 3c 6f 70 3e 20 59 22 20 77 68 65 72 65 20  X <op> Y" where 
1ec59 62 6f 74 68 20 58 20 61 6e 64 20 59 20 61 72 65  both X and Y are
1ec5a 20 63 6f 6c 75 6d 6e 73 2c 20 74 68 65 6e 20 74   columns, then t
1ec5b 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 65  he original.** e
1ec5c 78 70 72 65 73 73 69 6f 6e 20 69 73 20 75 6e 63  xpression is unc
1ec5d 68 61 6e 67 65 64 20 61 6e 64 20 61 20 6e 65 77  hanged and a new
1ec5e 20 76 69 72 74 75 61 6c 20 65 78 70 72 65 73 73   virtual express
1ec5f 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a  ion of the form.
1ec60 2a 2a 20 22 59 20 3c 6f 70 3e 20 58 22 20 69 73  ** "Y <op> X" is
1ec61 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 57 48   added to the WH
1ec62 45 52 45 20 63 6c 61 75 73 65 20 61 6e 64 20 61  ERE clause and a
1ec63 6e 61 6c 79 7a 65 64 20 73 65 70 61 72 61 74 65  nalyzed separate
1ec64 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ly..*/.static vo
1ec65 69 64 20 65 78 70 72 41 6e 61 6c 79 7a 65 28 0a  id exprAnalyze(.
1ec66 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c    SrcList *pSrc,
1ec67 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74              /* t
1ec68 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a  he FROM clause *
1ec69 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20  /.  WhereClause 
1ec6a 2a 70 57 43 2c 20 20 20 20 20 20 20 20 20 2f 2a  *pWC,         /*
1ec6b 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
1ec6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 54 65  e */.  int idxTe
1ec6d 72 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20  rm              
1ec6e 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65   /* Index of the
1ec6f 20 74 65 72 6d 20 74 6f 20 62 65 20 61 6e 61 6c   term to be anal
1ec70 79 7a 65 64 20 2a 2f 0a 29 7b 0a 20 20 57 68 65  yzed */.){.  Whe
1ec71 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 20 3d 20  reTerm *pTerm = 
1ec72 26 70 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d  &pWC->a[idxTerm]
1ec73 3b 0a 20 20 45 78 70 72 4d 61 73 6b 53 65 74 20  ;.  ExprMaskSet 
1ec74 2a 70 4d 61 73 6b 53 65 74 20 3d 20 70 57 43 2d  *pMaskSet = pWC-
1ec75 3e 70 4d 61 73 6b 53 65 74 3b 0a 20 20 45 78 70  >pMaskSet;.  Exp
1ec76 72 20 2a 70 45 78 70 72 20 3d 20 70 54 65 72 6d  r *pExpr = pTerm
1ec77 2d 3e 70 45 78 70 72 3b 0a 20 20 42 69 74 6d 61  ->pExpr;.  Bitma
1ec78 73 6b 20 70 72 65 72 65 71 4c 65 66 74 3b 0a 20  sk prereqLeft;. 
1ec79 20 42 69 74 6d 61 73 6b 20 70 72 65 72 65 71 41   Bitmask prereqA
1ec7a 6c 6c 3b 0a 20 20 69 6e 74 20 6e 50 61 74 74 65  ll;.  int nPatte
1ec7b 72 6e 3b 0a 20 20 69 6e 74 20 69 73 43 6f 6d 70  rn;.  int isComp
1ec7c 6c 65 74 65 3b 0a 20 20 69 6e 74 20 6f 70 3b 0a  lete;.  int op;.
1ec7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 4d 61  .  if( sqlite3Ma
1ec7e 6c 6c 6f 63 46 61 69 6c 65 64 28 29 20 29 20 72  llocFailed() ) r
1ec7f 65 74 75 72 6e 3b 0a 20 20 70 72 65 72 65 71 4c  eturn;.  prereqL
1ec80 65 66 74 20 3d 20 65 78 70 72 54 61 62 6c 65 55  eft = exprTableU
1ec81 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70  sage(pMaskSet, p
1ec82 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20  Expr->pLeft);.  
1ec83 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a  op = pExpr->op;.
1ec84 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 4e 20    if( op==TK_IN 
1ec85 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
1ec86 45 78 70 72 2d 3e 70 52 69 67 68 74 3d 3d 30 20  Expr->pRight==0 
1ec87 29 3b 0a 20 20 20 20 70 54 65 72 6d 2d 3e 70 72  );.    pTerm->pr
1ec88 65 72 65 71 52 69 67 68 74 20 3d 20 65 78 70 72  ereqRight = expr
1ec89 4c 69 73 74 54 61 62 6c 65 55 73 61 67 65 28 70  ListTableUsage(p
1ec8a 4d 61 73 6b 53 65 74 2c 20 70 45 78 70 72 2d 3e  MaskSet, pExpr->
1ec8b 70 4c 69 73 74 29 0a 20 20 20 20 20 20 20 20 20  pList).         
1ec8c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ec8d 20 7c 20 65 78 70 72 53 65 6c 65 63 74 54 61 62   | exprSelectTab
1ec8e 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74  leUsage(pMaskSet
1ec8f 2c 20 70 45 78 70 72 2d 3e 70 53 65 6c 65 63 74  , pExpr->pSelect
1ec90 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6f  );.  }else if( o
1ec91 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29 7b 0a  p==TK_ISNULL ){.
1ec92 20 20 20 20 70 54 65 72 6d 2d 3e 70 72 65 72 65      pTerm->prere
1ec93 71 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 7d 65  qRight = 0;.  }e
1ec94 6c 73 65 7b 0a 20 20 20 20 70 54 65 72 6d 2d 3e  lse{.    pTerm->
1ec95 70 72 65 72 65 71 52 69 67 68 74 20 3d 20 65 78  prereqRight = ex
1ec96 70 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61  prTableUsage(pMa
1ec97 73 6b 53 65 74 2c 20 70 45 78 70 72 2d 3e 70 52  skSet, pExpr->pR
1ec98 69 67 68 74 29 3b 0a 20 20 7d 0a 20 20 70 72 65  ight);.  }.  pre
1ec99 72 65 71 41 6c 6c 20 3d 20 65 78 70 72 54 61 62  reqAll = exprTab
1ec9a 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74  leUsage(pMaskSet
1ec9b 2c 20 70 45 78 70 72 29 3b 0a 20 20 69 66 28 20  , pExpr);.  if( 
1ec9c 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
1ec9d 70 45 78 70 72 2c 20 45 50 5f 46 72 6f 6d 4a 6f  pExpr, EP_FromJo
1ec9e 69 6e 29 20 29 7b 0a 20 20 20 20 70 72 65 72 65  in) ){.    prere
1ec9f 71 41 6c 6c 20 7c 3d 20 67 65 74 4d 61 73 6b 28  qAll |= getMask(
1eca0 70 4d 61 73 6b 53 65 74 2c 20 70 45 78 70 72 2d  pMaskSet, pExpr-
1eca1 3e 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65  >iRightJoinTable
1eca2 29 3b 0a 20 20 7d 0a 20 20 70 54 65 72 6d 2d 3e  );.  }.  pTerm->
1eca3 70 72 65 72 65 71 41 6c 6c 20 3d 20 70 72 65 72  prereqAll = prer
1eca4 65 71 41 6c 6c 3b 0a 20 20 70 54 65 72 6d 2d 3e  eqAll;.  pTerm->
1eca5 6c 65 66 74 43 75 72 73 6f 72 20 3d 20 2d 31 3b  leftCursor = -1;
1eca6 0a 20 20 70 54 65 72 6d 2d 3e 69 50 61 72 65 6e  .  pTerm->iParen
1eca7 74 20 3d 20 2d 31 3b 0a 20 20 70 54 65 72 6d 2d  t = -1;.  pTerm-
1eca8 3e 65 4f 70 65 72 61 74 6f 72 20 3d 20 30 3b 0a  >eOperator = 0;.
1eca9 20 20 69 66 28 20 61 6c 6c 6f 77 65 64 4f 70 28    if( allowedOp(
1ecaa 6f 70 29 20 26 26 20 28 70 54 65 72 6d 2d 3e 70  op) && (pTerm->p
1ecab 72 65 72 65 71 52 69 67 68 74 20 26 20 70 72 65  rereqRight & pre
1ecac 72 65 71 4c 65 66 74 29 3d 3d 30 20 29 7b 0a 20  reqLeft)==0 ){. 
1ecad 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d     Expr *pLeft =
1ecae 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20   pExpr->pLeft;. 
1ecaf 20 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 20     Expr *pRight 
1ecb0 3d 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b  = pExpr->pRight;
1ecb1 0a 20 20 20 20 69 66 28 20 70 4c 65 66 74 2d 3e  .    if( pLeft->
1ecb2 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b  op==TK_COLUMN ){
1ecb3 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 6c 65  .      pTerm->le
1ecb4 66 74 43 75 72 73 6f 72 20 3d 20 70 4c 65 66 74  ftCursor = pLeft
1ecb5 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20 20 20  ->iTable;.      
1ecb6 70 54 65 72 6d 2d 3e 6c 65 66 74 43 6f 6c 75 6d  pTerm->leftColum
1ecb7 6e 20 3d 20 70 4c 65 66 74 2d 3e 69 43 6f 6c 75  n = pLeft->iColu
1ecb8 6d 6e 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d  mn;.      pTerm-
1ecb9 3e 65 4f 70 65 72 61 74 6f 72 20 3d 20 6f 70 65  >eOperator = ope
1ecba 72 61 74 6f 72 4d 61 73 6b 28 6f 70 29 3b 0a 20  ratorMask(op);. 
1ecbb 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 52 69     }.    if( pRi
1ecbc 67 68 74 20 26 26 20 70 52 69 67 68 74 2d 3e 6f  ght && pRight->o
1ecbd 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b 0a  p==TK_COLUMN ){.
1ecbe 20 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 20        WhereTerm 
1ecbf 2a 70 4e 65 77 3b 0a 20 20 20 20 20 20 45 78 70  *pNew;.      Exp
1ecc0 72 20 2a 70 44 75 70 3b 0a 20 20 20 20 20 20 69  r *pDup;.      i
1ecc1 66 28 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75  f( pTerm->leftCu
1ecc2 72 73 6f 72 3e 3d 30 20 29 7b 0a 20 20 20 20 20  rsor>=0 ){.     
1ecc3 20 20 20 69 6e 74 20 69 64 78 4e 65 77 3b 0a 20     int idxNew;. 
1ecc4 20 20 20 20 20 20 20 70 44 75 70 20 3d 20 73 71         pDup = sq
1ecc5 6c 69 74 65 33 45 78 70 72 44 75 70 28 70 45 78  lite3ExprDup(pEx
1ecc6 70 72 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  pr);.        if(
1ecc7 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 46 61   sqlite3MallocFa
1ecc8 69 6c 65 64 28 29 20 29 7b 0a 20 20 20 20 20 20  iled() ){.      
1ecc9 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
1ecca 65 6c 65 74 65 28 70 44 75 70 29 3b 0a 20 20 20  elete(pDup);.   
1eccb 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20         return;. 
1eccc 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1eccd 20 69 64 78 4e 65 77 20 3d 20 77 68 65 72 65 43   idxNew = whereC
1ecce 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57 43 2c  lauseInsert(pWC,
1eccf 20 70 44 75 70 2c 20 54 45 52 4d 5f 56 49 52 54   pDup, TERM_VIRT
1ecd0 55 41 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43  UAL|TERM_DYNAMIC
1ecd1 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  );.        if( i
1ecd2 64 78 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72  dxNew==0 ) retur
1ecd3 6e 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 20  n;.        pNew 
1ecd4 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77  = &pWC->a[idxNew
1ecd5 5d 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d  ];.        pNew-
1ecd6 3e 69 50 61 72 65 6e 74 20 3d 20 69 64 78 54 65  >iParent = idxTe
1ecd7 72 6d 3b 0a 20 20 20 20 20 20 20 20 70 54 65 72  rm;.        pTer
1ecd8 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 54  m = &pWC->a[idxT
1ecd9 65 72 6d 5d 3b 0a 20 20 20 20 20 20 20 20 70 54  erm];.        pT
1ecda 65 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20 31 3b  erm->nChild = 1;
1ecdb 0a 20 20 20 20 20 20 20 20 70 54 65 72 6d 2d 3e  .        pTerm->
1ecdc 66 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f 43 4f  flags |= TERM_CO
1ecdd 50 49 45 44 3b 0a 20 20 20 20 20 20 7d 65 6c 73  PIED;.      }els
1ecde 65 7b 0a 20 20 20 20 20 20 20 20 70 44 75 70 20  e{.        pDup 
1ecdf 3d 20 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20  = pExpr;.       
1ece0 20 70 4e 65 77 20 3d 20 70 54 65 72 6d 3b 0a 20   pNew = pTerm;. 
1ece1 20 20 20 20 20 7d 0a 20 20 20 20 20 20 65 78 70       }.      exp
1ece2 72 43 6f 6d 6d 75 74 65 28 70 44 75 70 29 3b 0a  rCommute(pDup);.
1ece3 20 20 20 20 20 20 70 4c 65 66 74 20 3d 20 70 44        pLeft = pD
1ece4 75 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20  up->pLeft;.     
1ece5 20 70 4e 65 77 2d 3e 6c 65 66 74 43 75 72 73 6f   pNew->leftCurso
1ece6 72 20 3d 20 70 4c 65 66 74 2d 3e 69 54 61 62 6c  r = pLeft->iTabl
1ece7 65 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 6c  e;.      pNew->l
1ece8 65 66 74 43 6f 6c 75 6d 6e 20 3d 20 70 4c 65 66  eftColumn = pLef
1ece9 74 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20  t->iColumn;.    
1ecea 20 20 70 4e 65 77 2d 3e 70 72 65 72 65 71 52 69    pNew->prereqRi
1eceb 67 68 74 20 3d 20 70 72 65 72 65 71 4c 65 66 74  ght = prereqLeft
1ecec 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 72  ;.      pNew->pr
1eced 65 72 65 71 41 6c 6c 20 3d 20 70 72 65 72 65 71  ereqAll = prereq
1ecee 41 6c 6c 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d  All;.      pNew-
1ecef 3e 65 4f 70 65 72 61 74 6f 72 20 3d 20 6f 70 65  >eOperator = ope
1ecf0 72 61 74 6f 72 4d 61 73 6b 28 70 44 75 70 2d 3e  ratorMask(pDup->
1ecf1 6f 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  op);.    }.  }..
1ecf2 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1ecf3 4d 49 54 5f 42 45 54 57 45 45 4e 5f 4f 50 54 49  MIT_BETWEEN_OPTI
1ecf4 4d 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 49 66  MIZATION.  /* If
1ecf5 20 61 20 74 65 72 6d 20 69 73 20 74 68 65 20 42   a term is the B
1ecf6 45 54 57 45 45 4e 20 6f 70 65 72 61 74 6f 72 2c  ETWEEN operator,
1ecf7 20 63 72 65 61 74 65 20 74 77 6f 20 6e 65 77 20   create two new 
1ecf8 76 69 72 74 75 61 6c 20 74 65 72 6d 73 0a 20 20  virtual terms.  
1ecf9 2a 2a 20 74 68 61 74 20 64 65 66 69 6e 65 20 74  ** that define t
1ecfa 68 65 20 72 61 6e 67 65 20 74 68 61 74 20 74 68  he range that th
1ecfb 65 20 42 45 54 57 45 45 4e 20 69 6d 70 6c 65 6d  e BETWEEN implem
1ecfc 65 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 65 6c 73  ents..  */.  els
1ecfd 65 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d  e if( pExpr->op=
1ecfe 3d 54 4b 5f 42 45 54 57 45 45 4e 20 29 7b 0a 20  =TK_BETWEEN ){. 
1ecff 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69     ExprList *pLi
1ed00 73 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73  st = pExpr->pLis
1ed01 74 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  t;.    int i;.  
1ed02 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
1ed03 38 20 6f 70 73 5b 5d 20 3d 20 7b 54 4b 5f 47 45  8 ops[] = {TK_GE
1ed04 2c 20 54 4b 5f 4c 45 7d 3b 0a 20 20 20 20 61 73  , TK_LE};.    as
1ed05 73 65 72 74 28 20 70 4c 69 73 74 21 3d 30 20 29  sert( pList!=0 )
1ed06 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c  ;.    assert( pL
1ed07 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 32 20 29 3b  ist->nExpr==2 );
1ed08 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
1ed09 32 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45  2; i++){.      E
1ed0a 78 70 72 20 2a 70 4e 65 77 45 78 70 72 3b 0a 20  xpr *pNewExpr;. 
1ed0b 20 20 20 20 20 69 6e 74 20 69 64 78 4e 65 77 3b       int idxNew;
1ed0c 0a 20 20 20 20 20 20 70 4e 65 77 45 78 70 72 20  .      pNewExpr 
1ed0d 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 6f 70  = sqlite3Expr(op
1ed0e 73 5b 69 5d 2c 20 73 71 6c 69 74 65 33 45 78 70  s[i], sqlite3Exp
1ed0f 72 44 75 70 28 70 45 78 70 72 2d 3e 70 4c 65 66  rDup(pExpr->pLef
1ed10 74 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  t),.            
1ed11 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ed12 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
1ed13 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70  pList->a[i].pExp
1ed14 72 29 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 64  r), 0);.      id
1ed15 78 4e 65 77 20 3d 20 77 68 65 72 65 43 6c 61 75  xNew = whereClau
1ed16 73 65 49 6e 73 65 72 74 28 70 57 43 2c 20 70 4e  seInsert(pWC, pN
1ed17 65 77 45 78 70 72 2c 20 54 45 52 4d 5f 56 49 52  ewExpr, TERM_VIR
1ed18 54 55 41 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49  TUAL|TERM_DYNAMI
1ed19 43 29 3b 0a 20 20 20 20 20 20 65 78 70 72 41 6e  C);.      exprAn
1ed1a 61 6c 79 7a 65 28 70 53 72 63 2c 20 70 57 43 2c  alyze(pSrc, pWC,
1ed1b 20 69 64 78 4e 65 77 29 3b 0a 20 20 20 20 20 20   idxNew);.      
1ed1c 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b  pTerm = &pWC->a[
1ed1d 69 64 78 54 65 72 6d 5d 3b 0a 20 20 20 20 20 20  idxTerm];.      
1ed1e 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77 5d 2e 69  pWC->a[idxNew].i
1ed1f 50 61 72 65 6e 74 20 3d 20 69 64 78 54 65 72 6d  Parent = idxTerm
1ed20 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 54 65 72  ;.    }.    pTer
1ed21 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20 32 3b 0a 20  m->nChild = 2;. 
1ed22 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c   }.#endif /* SQL
1ed23 49 54 45 5f 4f 4d 49 54 5f 42 45 54 57 45 45 4e  ITE_OMIT_BETWEEN
1ed24 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f  _OPTIMIZATION */
1ed25 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
1ed26 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50  QLITE_OMIT_OR_OP
1ed27 54 49 4d 49 5a 41 54 49 4f 4e 29 20 26 26 20 21  TIMIZATION) && !
1ed28 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
1ed29 4d 49 54 5f 53 55 42 51 55 45 52 59 29 0a 20 20  MIT_SUBQUERY).  
1ed2a 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 63 6f  /* Attempt to co
1ed2b 6e 76 65 72 74 20 4f 52 2d 63 6f 6e 6e 65 63 74  nvert OR-connect
1ed2c 65 64 20 74 65 72 6d 73 20 69 6e 74 6f 20 61 6e  ed terms into an
1ed2d 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 73 6f 20   IN operator so 
1ed2e 74 68 61 74 0a 20 20 2a 2a 20 74 68 65 79 20 63  that.  ** they c
1ed2f 61 6e 20 6d 61 6b 65 20 75 73 65 20 6f 66 20 69  an make use of i
1ed30 6e 64 69 63 65 73 2e 20 20 45 78 61 6d 70 6c 65  ndices.  Example
1ed31 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20  :.  **.  **     
1ed32 20 78 20 3d 20 65 78 70 72 31 20 20 4f 52 20 20   x = expr1  OR  
1ed33 65 78 70 72 32 20 3d 20 78 20 20 4f 52 20 20 78  expr2 = x  OR  x
1ed34 20 3d 20 65 78 70 72 33 0a 20 20 2a 2a 0a 20 20   = expr3.  **.  
1ed35 2a 2a 20 69 73 20 63 6f 6e 76 65 72 74 65 64 20  ** is converted 
1ed36 69 6e 74 6f 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20  into.  **.  **  
1ed37 20 20 20 20 78 20 49 4e 20 28 65 78 70 72 31 2c      x IN (expr1,
1ed38 65 78 70 72 32 2c 65 78 70 72 33 29 0a 20 20 2a  expr2,expr3).  *
1ed39 2a 0a 20 20 2a 2a 20 54 68 69 73 20 6f 70 74 69  *.  ** This opti
1ed3a 6d 69 7a 61 74 69 6f 6e 20 6d 75 73 74 20 62 65  mization must be
1ed3b 20 6f 6d 69 74 74 65 64 20 69 66 20 4f 4d 49 54   omitted if OMIT
1ed3c 5f 53 55 42 51 55 45 52 59 20 69 73 20 64 65 66  _SUBQUERY is def
1ed3d 69 6e 65 64 20 62 65 63 61 75 73 65 0a 20 20 2a  ined because.  *
1ed3e 2a 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 66  * the compiler f
1ed3f 6f 72 20 74 68 65 20 74 68 65 20 49 4e 20 6f 70  or the the IN op
1ed40 65 72 61 74 6f 72 20 69 73 20 70 61 72 74 20 6f  erator is part o
1ed41 66 20 73 75 62 2d 71 75 65 72 69 65 73 2e 0a 20  f sub-queries.. 
1ed42 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 28 20 70   */.  else if( p
1ed43 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4f 52 20  Expr->op==TK_OR 
1ed44 29 7b 0a 20 20 20 20 69 6e 74 20 6f 6b 3b 0a 20  ){.    int ok;. 
1ed45 20 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 20     int i, j;.   
1ed46 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 69 43   int iColumn, iC
1ed47 75 72 73 6f 72 3b 0a 20 20 20 20 57 68 65 72 65  ursor;.    Where
1ed48 43 6c 61 75 73 65 20 73 4f 72 3b 0a 20 20 20 20  Clause sOr;.    
1ed49 57 68 65 72 65 54 65 72 6d 20 2a 70 4f 72 54 65  WhereTerm *pOrTe
1ed4a 72 6d 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28  rm;..    assert(
1ed4b 20 28 70 54 65 72 6d 2d 3e 66 6c 61 67 73 20 26   (pTerm->flags &
1ed4c 20 54 45 52 4d 5f 44 59 4e 41 4d 49 43 29 3d 3d   TERM_DYNAMIC)==
1ed4d 30 20 29 3b 0a 20 20 20 20 77 68 65 72 65 43 6c  0 );.    whereCl
1ed4e 61 75 73 65 49 6e 69 74 28 26 73 4f 72 2c 20 70  auseInit(&sOr, p
1ed4f 57 43 2d 3e 70 50 61 72 73 65 2c 20 70 4d 61 73  WC->pParse, pMas
1ed50 6b 53 65 74 29 3b 0a 20 20 20 20 77 68 65 72 65  kSet);.    where
1ed51 53 70 6c 69 74 28 26 73 4f 72 2c 20 70 45 78 70  Split(&sOr, pExp
1ed52 72 2c 20 54 4b 5f 4f 52 29 3b 0a 20 20 20 20 65  r, TK_OR);.    e
1ed53 78 70 72 41 6e 61 6c 79 7a 65 41 6c 6c 28 70 53  xprAnalyzeAll(pS
1ed54 72 63 2c 20 26 73 4f 72 29 3b 0a 20 20 20 20 61  rc, &sOr);.    a
1ed55 73 73 65 72 74 28 20 73 4f 72 2e 6e 54 65 72 6d  ssert( sOr.nTerm
1ed56 3e 3d 32 20 29 3b 0a 20 20 20 20 6a 20 3d 20 30  >=2 );.    j = 0
1ed57 3b 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20  ;.    do{.      
1ed58 61 73 73 65 72 74 28 20 6a 3c 73 4f 72 2e 6e 54  assert( j<sOr.nT
1ed59 65 72 6d 20 29 3b 0a 20 20 20 20 20 20 69 43 6f  erm );.      iCo
1ed5a 6c 75 6d 6e 20 3d 20 73 4f 72 2e 61 5b 6a 5d 2e  lumn = sOr.a[j].
1ed5b 6c 65 66 74 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20  leftColumn;.    
1ed5c 20 20 69 43 75 72 73 6f 72 20 3d 20 73 4f 72 2e    iCursor = sOr.
1ed5d 61 5b 6a 5d 2e 6c 65 66 74 43 75 72 73 6f 72 3b  a[j].leftCursor;
1ed5e 0a 20 20 20 20 20 20 6f 6b 20 3d 20 69 43 75 72  .      ok = iCur
1ed5f 73 6f 72 3e 3d 30 3b 0a 20 20 20 20 20 20 66 6f  sor>=0;.      fo
1ed60 72 28 69 3d 73 4f 72 2e 6e 54 65 72 6d 2d 31 2c  r(i=sOr.nTerm-1,
1ed61 20 70 4f 72 54 65 72 6d 3d 73 4f 72 2e 61 3b 20   pOrTerm=sOr.a; 
1ed62 69 3e 3d 30 20 26 26 20 6f 6b 3b 20 69 2d 2d 2c  i>=0 && ok; i--,
1ed63 20 70 4f 72 54 65 72 6d 2b 2b 29 7b 0a 20 20 20   pOrTerm++){.   
1ed64 20 20 20 20 20 69 66 28 20 70 4f 72 54 65 72 6d       if( pOrTerm
1ed65 2d 3e 65 4f 70 65 72 61 74 6f 72 21 3d 57 4f 5f  ->eOperator!=WO_
1ed66 45 51 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  EQ ){.          
1ed67 67 6f 74 6f 20 6f 72 5f 6e 6f 74 5f 70 6f 73 73  goto or_not_poss
1ed68 69 62 6c 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ible;.        }.
1ed69 20 20 20 20 20 20 20 20 69 66 28 20 6f 72 54 65          if( orTe
1ed6a 72 6d 49 73 4f 70 74 43 61 6e 64 69 64 61 74 65  rmIsOptCandidate
1ed6b 28 70 4f 72 54 65 72 6d 2c 20 69 43 75 72 73 6f  (pOrTerm, iCurso
1ed6c 72 2c 20 69 43 6f 6c 75 6d 6e 29 20 29 7b 0a 20  r, iColumn) ){. 
1ed6d 20 20 20 20 20 20 20 20 20 70 4f 72 54 65 72 6d           pOrTerm
1ed6e 2d 3e 66 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f  ->flags |= TERM_
1ed6f 4f 52 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 7d  OR_OK;.        }
1ed70 65 6c 73 65 20 69 66 28 20 6f 72 54 65 72 6d 48  else if( orTermH
1ed71 61 73 4f 6b 44 75 70 6c 69 63 61 74 65 28 26 73  asOkDuplicate(&s
1ed72 4f 72 2c 20 70 4f 72 54 65 72 6d 29 20 29 7b 0a  Or, pOrTerm) ){.
1ed73 20 20 20 20 20 20 20 20 20 20 70 4f 72 54 65 72            pOrTer
1ed74 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 54 45 52  m->flags &= ~TER
1ed75 4d 5f 4f 52 5f 4f 4b 3b 0a 20 20 20 20 20 20 20  M_OR_OK;.       
1ed76 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1ed77 20 20 6f 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20    ok = 0;.      
1ed78 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
1ed79 7d 77 68 69 6c 65 28 20 21 6f 6b 20 26 26 20 28  }while( !ok && (
1ed7a 73 4f 72 2e 61 5b 6a 2b 2b 5d 2e 66 6c 61 67 73  sOr.a[j++].flags
1ed7b 20 26 20 54 45 52 4d 5f 43 4f 50 49 45 44 29 21   & TERM_COPIED)!
1ed7c 3d 30 20 26 26 20 6a 3c 32 20 29 3b 0a 20 20 20  =0 && j<2 );.   
1ed7d 20 69 66 28 20 6f 6b 20 29 7b 0a 20 20 20 20 20   if( ok ){.     
1ed7e 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
1ed7f 20 3d 20 30 3b 0a 20 20 20 20 20 20 45 78 70 72   = 0;.      Expr
1ed80 20 2a 70 4e 65 77 2c 20 2a 70 44 75 70 3b 0a 20   *pNew, *pDup;. 
1ed81 20 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74       Expr *pLeft
1ed82 20 3d 20 30 3b 0a 20 20 20 20 20 20 66 6f 72 28   = 0;.      for(
1ed83 69 3d 73 4f 72 2e 6e 54 65 72 6d 2d 31 2c 20 70  i=sOr.nTerm-1, p
1ed84 4f 72 54 65 72 6d 3d 73 4f 72 2e 61 3b 20 69 3e  OrTerm=sOr.a; i>
1ed85 3d 30 20 26 26 20 6f 6b 3b 20 69 2d 2d 2c 20 70  =0 && ok; i--, p
1ed86 4f 72 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 20  OrTerm++){.     
1ed87 20 20 20 69 66 28 20 28 70 4f 72 54 65 72 6d 2d     if( (pOrTerm-
1ed88 3e 66 6c 61 67 73 20 26 20 54 45 52 4d 5f 4f 52  >flags & TERM_OR
1ed89 5f 4f 4b 29 3d 3d 30 20 29 20 63 6f 6e 74 69 6e  _OK)==0 ) contin
1ed8a 75 65 3b 0a 20 20 20 20 20 20 20 20 70 44 75 70  ue;.        pDup
1ed8b 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
1ed8c 70 28 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72  p(pOrTerm->pExpr
1ed8d 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 20 20 20  ->pRight);.     
1ed8e 20 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74     pList = sqlit
1ed8f 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
1ed90 28 70 4c 69 73 74 2c 20 70 44 75 70 2c 20 30 29  (pList, pDup, 0)
1ed91 3b 0a 20 20 20 20 20 20 20 20 70 4c 65 66 74 20  ;.        pLeft 
1ed92 3d 20 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72  = pOrTerm->pExpr
1ed93 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 7d  ->pLeft;.      }
1ed94 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1ed95 4c 65 66 74 21 3d 30 20 29 3b 0a 20 20 20 20 20  Left!=0 );.     
1ed96 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45   pDup = sqlite3E
1ed97 78 70 72 44 75 70 28 70 4c 65 66 74 29 3b 0a 20  xprDup(pLeft);. 
1ed98 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69       pNew = sqli
1ed99 74 65 33 45 78 70 72 28 54 4b 5f 49 4e 2c 20 70  te3Expr(TK_IN, p
1ed9a 44 75 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  Dup, 0, 0);.    
1ed9b 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20    if( pNew ){.  
1ed9c 20 20 20 20 20 20 69 6e 74 20 69 64 78 4e 65 77        int idxNew
1ed9d 3b 0a 20 20 20 20 20 20 20 20 74 72 61 6e 73 66  ;.        transf
1ed9e 65 72 4a 6f 69 6e 4d 61 72 6b 69 6e 67 73 28 70  erJoinMarkings(p
1ed9f 4e 65 77 2c 20 70 45 78 70 72 29 3b 0a 20 20 20  New, pExpr);.   
1eda0 20 20 20 20 20 70 4e 65 77 2d 3e 70 4c 69 73 74       pNew->pList
1eda1 20 3d 20 70 4c 69 73 74 3b 0a 20 20 20 20 20 20   = pList;.      
1eda2 20 20 69 64 78 4e 65 77 20 3d 20 77 68 65 72 65    idxNew = where
1eda3 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57 43  ClauseInsert(pWC
1eda4 2c 20 70 4e 65 77 2c 20 54 45 52 4d 5f 56 49 52  , pNew, TERM_VIR
1eda5 54 55 41 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49  TUAL|TERM_DYNAMI
1eda6 43 29 3b 0a 20 20 20 20 20 20 20 20 65 78 70 72  C);.        expr
1eda7 41 6e 61 6c 79 7a 65 28 70 53 72 63 2c 20 70 57  Analyze(pSrc, pW
1eda8 43 2c 20 69 64 78 4e 65 77 29 3b 0a 20 20 20 20  C, idxNew);.    
1eda9 20 20 20 20 70 54 65 72 6d 20 3d 20 26 70 57 43      pTerm = &pWC
1edaa 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 20  ->a[idxTerm];.  
1edab 20 20 20 20 20 20 70 57 43 2d 3e 61 5b 69 64 78        pWC->a[idx
1edac 4e 65 77 5d 2e 69 50 61 72 65 6e 74 20 3d 20 69  New].iParent = i
1edad 64 78 54 65 72 6d 3b 0a 20 20 20 20 20 20 20 20  dxTerm;.        
1edae 70 54 65 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20  pTerm->nChild = 
1edaf 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  1;.      }else{.
1edb0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
1edb1 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 4c  xprListDelete(pL
1edb2 69 73 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ist);.      }.  
1edb3 20 20 7d 0a 6f 72 5f 6e 6f 74 5f 70 6f 73 73 69    }.or_not_possi
1edb4 62 6c 65 3a 0a 20 20 20 20 77 68 65 72 65 43 6c  ble:.    whereCl
1edb5 61 75 73 65 43 6c 65 61 72 28 26 73 4f 72 29 3b  auseClear(&sOr);
1edb6 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  .  }.#endif /* S
1edb7 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50  QLITE_OMIT_OR_OP
1edb8 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 23  TIMIZATION */..#
1edb9 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1edba 49 54 5f 4c 49 4b 45 5f 4f 50 54 49 4d 49 5a 41  IT_LIKE_OPTIMIZA
1edbb 54 49 4f 4e 0a 20 20 2f 2a 20 41 64 64 20 63 6f  TION.  /* Add co
1edbc 6e 73 74 72 61 69 6e 74 73 20 74 6f 20 72 65 64  nstraints to red
1edbd 75 63 65 20 74 68 65 20 73 65 61 72 63 68 20 73  uce the search s
1edbe 70 61 63 65 20 6f 6e 20 61 20 4c 49 4b 45 20 6f  pace on a LIKE o
1edbf 72 20 47 4c 4f 42 0a 20 20 2a 2a 20 6f 70 65 72  r GLOB.  ** oper
1edc0 61 74 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ator..  */.  if(
1edc1 20 69 73 4c 69 6b 65 4f 72 47 6c 6f 62 28 70 57   isLikeOrGlob(pW
1edc2 43 2d 3e 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  C->pParse->db, p
1edc3 45 78 70 72 2c 20 26 6e 50 61 74 74 65 72 6e 2c  Expr, &nPattern,
1edc4 20 26 69 73 43 6f 6d 70 6c 65 74 65 29 20 29 7b   &isComplete) ){
1edc5 0a 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74  .    Expr *pLeft
1edc6 2c 20 2a 70 52 69 67 68 74 3b 0a 20 20 20 20 45  , *pRight;.    E
1edc7 78 70 72 20 2a 70 53 74 72 31 2c 20 2a 70 53 74  xpr *pStr1, *pSt
1edc8 72 32 3b 0a 20 20 20 20 45 78 70 72 20 2a 70 4e  r2;.    Expr *pN
1edc9 65 77 45 78 70 72 31 2c 20 2a 70 4e 65 77 45 78  ewExpr1, *pNewEx
1edca 70 72 32 3b 0a 20 20 20 20 69 6e 74 20 69 64 78  pr2;.    int idx
1edcb 4e 65 77 31 2c 20 69 64 78 4e 65 77 32 3b 0a 0a  New1, idxNew2;..
1edcc 20 20 20 20 70 4c 65 66 74 20 3d 20 70 45 78 70      pLeft = pExp
1edcd 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70  r->pList->a[1].p
1edce 45 78 70 72 3b 0a 20 20 20 20 70 52 69 67 68 74  Expr;.    pRight
1edcf 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d   = pExpr->pList-
1edd0 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20  >a[0].pExpr;.   
1edd1 20 70 53 74 72 31 20 3d 20 73 71 6c 69 74 65 33   pStr1 = sqlite3
1edd2 45 78 70 72 28 54 4b 5f 53 54 52 49 4e 47 2c 20  Expr(TK_STRING, 
1edd3 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66  0, 0, 0);.    if
1edd4 28 20 70 53 74 72 31 20 29 7b 0a 20 20 20 20 20  ( pStr1 ){.     
1edd5 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e 43 6f 70   sqlite3TokenCop
1edd6 79 28 26 70 53 74 72 31 2d 3e 74 6f 6b 65 6e 2c  y(&pStr1->token,
1edd7 20 26 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e 29   &pRight->token)
1edd8 3b 0a 20 20 20 20 20 20 70 53 74 72 31 2d 3e 74  ;.      pStr1->t
1edd9 6f 6b 65 6e 2e 6e 20 3d 20 6e 50 61 74 74 65 72  oken.n = nPatter
1edda 6e 3b 0a 20 20 20 20 20 20 70 53 74 72 31 2d 3e  n;.      pStr1->
1eddb 66 6c 61 67 73 20 3d 20 45 50 5f 44 65 71 75 6f  flags = EP_Dequo
1eddc 74 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  ted;.    }.    p
1eddd 53 74 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78  Str2 = sqlite3Ex
1edde 70 72 44 75 70 28 70 53 74 72 31 29 3b 0a 20 20  prDup(pStr1);.  
1eddf 20 20 69 66 28 20 70 53 74 72 32 20 29 7b 0a 20    if( pStr2 ){. 
1ede0 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53 74       assert( pSt
1ede1 72 32 2d 3e 74 6f 6b 65 6e 2e 64 79 6e 20 29 3b  r2->token.dyn );
1ede2 0a 20 20 20 20 20 20 2b 2b 2a 28 75 38 2a 29 26  .      ++*(u8*)&
1ede3 70 53 74 72 32 2d 3e 74 6f 6b 65 6e 2e 7a 5b 6e  pStr2->token.z[n
1ede4 50 61 74 74 65 72 6e 2d 31 5d 3b 0a 20 20 20 20  Pattern-1];.    
1ede5 7d 0a 20 20 20 20 70 4e 65 77 45 78 70 72 31 20  }.    pNewExpr1 
1ede6 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 54 4b  = sqlite3Expr(TK
1ede7 5f 47 45 2c 20 73 71 6c 69 74 65 33 45 78 70 72  _GE, sqlite3Expr
1ede8 44 75 70 28 70 4c 65 66 74 29 2c 20 70 53 74 72  Dup(pLeft), pStr
1ede9 31 2c 20 30 29 3b 0a 20 20 20 20 69 64 78 4e 65  1, 0);.    idxNe
1edea 77 31 20 3d 20 77 68 65 72 65 43 6c 61 75 73 65  w1 = whereClause
1edeb 49 6e 73 65 72 74 28 70 57 43 2c 20 70 4e 65 77  Insert(pWC, pNew
1edec 45 78 70 72 31 2c 20 54 45 52 4d 5f 56 49 52 54  Expr1, TERM_VIRT
1eded 55 41 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43  UAL|TERM_DYNAMIC
1edee 29 3b 0a 20 20 20 20 65 78 70 72 41 6e 61 6c 79  );.    exprAnaly
1edef 7a 65 28 70 53 72 63 2c 20 70 57 43 2c 20 69 64  ze(pSrc, pWC, id
1edf0 78 4e 65 77 31 29 3b 0a 20 20 20 20 70 4e 65 77  xNew1);.    pNew
1edf1 45 78 70 72 32 20 3d 20 73 71 6c 69 74 65 33 45  Expr2 = sqlite3E
1edf2 78 70 72 28 54 4b 5f 4c 54 2c 20 73 71 6c 69 74  xpr(TK_LT, sqlit
1edf3 65 33 45 78 70 72 44 75 70 28 70 4c 65 66 74 29  e3ExprDup(pLeft)
1edf4 2c 20 70 53 74 72 32 2c 20 30 29 3b 0a 20 20 20  , pStr2, 0);.   
1edf5 20 69 64 78 4e 65 77 32 20 3d 20 77 68 65 72 65   idxNew2 = where
1edf6 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57 43  ClauseInsert(pWC
1edf7 2c 20 70 4e 65 77 45 78 70 72 32 2c 20 54 45 52  , pNewExpr2, TER
1edf8 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44  M_VIRTUAL|TERM_D
1edf9 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 65 78 70  YNAMIC);.    exp
1edfa 72 41 6e 61 6c 79 7a 65 28 70 53 72 63 2c 20 70  rAnalyze(pSrc, p
1edfb 57 43 2c 20 69 64 78 4e 65 77 32 29 3b 0a 20 20  WC, idxNew2);.  
1edfc 20 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e    pTerm = &pWC->
1edfd 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 20 20 20  a[idxTerm];.    
1edfe 69 66 28 20 69 73 43 6f 6d 70 6c 65 74 65 20 29  if( isComplete )
1edff 7b 0a 20 20 20 20 20 20 70 57 43 2d 3e 61 5b 69  {.      pWC->a[i
1ee00 64 78 4e 65 77 31 5d 2e 69 50 61 72 65 6e 74 20  dxNew1].iParent 
1ee01 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20 20 20 20  = idxTerm;.     
1ee02 20 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77 32 5d   pWC->a[idxNew2]
1ee03 2e 69 50 61 72 65 6e 74 20 3d 20 69 64 78 54 65  .iParent = idxTe
1ee04 72 6d 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d  rm;.      pTerm-
1ee05 3e 6e 43 68 69 6c 64 20 3d 20 32 3b 0a 20 20 20  >nChild = 2;.   
1ee06 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a   }.  }.#endif /*
1ee07 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 49 4b   SQLITE_OMIT_LIK
1ee08 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a  E_OPTIMIZATION *
1ee09 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
1ee0a 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
1ee0b 42 4c 45 0a 20 20 2f 2a 20 41 64 64 20 61 20 57  BLE.  /* Add a W
1ee0c 4f 5f 4d 41 54 43 48 20 61 75 78 69 6c 69 61 72  O_MATCH auxiliar
1ee0d 79 20 74 65 72 6d 20 74 6f 20 74 68 65 20 63 6f  y term to the co
1ee0e 6e 73 74 72 61 69 6e 74 20 73 65 74 20 69 66 20  nstraint set if 
1ee0f 74 68 65 0a 20 20 2a 2a 20 63 75 72 72 65 6e 74  the.  ** current
1ee10 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6f   expression is o
1ee11 66 20 74 68 65 20 66 6f 72 6d 3a 20 20 63 6f 6c  f the form:  col
1ee12 75 6d 6e 20 4d 41 54 43 48 20 65 78 70 72 2e 0a  umn MATCH expr..
1ee13 20 20 2a 2a 20 54 68 69 73 20 69 6e 66 6f 72 6d    ** This inform
1ee14 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 62 79  ation is used by
1ee15 20 74 68 65 20 78 42 65 73 74 49 6e 64 65 78 20   the xBestIndex 
1ee16 6d 65 74 68 6f 64 73 20 6f 66 0a 20 20 2a 2a 20  methods of.  ** 
1ee17 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 2e 20  virtual tables. 
1ee18 20 54 68 65 20 6e 61 74 69 76 65 20 71 75 65 72   The native quer
1ee19 79 20 6f 70 74 69 6d 69 7a 65 72 20 64 6f 65 73  y optimizer does
1ee1a 20 6e 6f 74 20 61 74 74 65 6d 70 74 0a 20 20 2a   not attempt.  *
1ee1b 2a 20 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e 67  * to do anything
1ee1c 20 77 69 74 68 20 4d 41 54 43 48 20 66 75 6e 63   with MATCH func
1ee1d 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66  tions..  */.  if
1ee1e 28 20 69 73 4d 61 74 63 68 4f 66 43 6f 6c 75 6d  ( isMatchOfColum
1ee1f 6e 28 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20  n(pExpr) ){.    
1ee20 69 6e 74 20 69 64 78 4e 65 77 3b 0a 20 20 20 20  int idxNew;.    
1ee21 45 78 70 72 20 2a 70 52 69 67 68 74 2c 20 2a 70  Expr *pRight, *p
1ee22 4c 65 66 74 3b 0a 20 20 20 20 57 68 65 72 65 54  Left;.    WhereT
1ee23 65 72 6d 20 2a 70 4e 65 77 54 65 72 6d 3b 0a 20  erm *pNewTerm;. 
1ee24 20 20 20 42 69 74 6d 61 73 6b 20 70 72 65 72 65     Bitmask prere
1ee25 71 43 6f 6c 75 6d 6e 2c 20 70 72 65 72 65 71 45  qColumn, prereqE
1ee26 78 70 72 3b 0a 0a 20 20 20 20 70 52 69 67 68 74  xpr;..    pRight
1ee27 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d   = pExpr->pList-
1ee28 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20  >a[0].pExpr;.   
1ee29 20 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e   pLeft = pExpr->
1ee2a 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70  pList->a[1].pExp
1ee2b 72 3b 0a 20 20 20 20 70 72 65 72 65 71 45 78 70  r;.    prereqExp
1ee2c 72 20 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61  r = exprTableUsa
1ee2d 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 52 69  ge(pMaskSet, pRi
1ee2e 67 68 74 29 3b 0a 20 20 20 20 70 72 65 72 65 71  ght);.    prereq
1ee2f 43 6f 6c 75 6d 6e 20 3d 20 65 78 70 72 54 61 62  Column = exprTab
1ee30 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74  leUsage(pMaskSet
1ee31 2c 20 70 4c 65 66 74 29 3b 0a 20 20 20 20 69 66  , pLeft);.    if
1ee32 28 20 28 70 72 65 72 65 71 45 78 70 72 20 26 20  ( (prereqExpr & 
1ee33 70 72 65 72 65 71 43 6f 6c 75 6d 6e 29 3d 3d 30  prereqColumn)==0
1ee34 20 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a   ){.      Expr *
1ee35 70 4e 65 77 45 78 70 72 3b 0a 20 20 20 20 20 20  pNewExpr;.      
1ee36 70 4e 65 77 45 78 70 72 20 3d 20 73 71 6c 69 74  pNewExpr = sqlit
1ee37 65 33 45 78 70 72 28 54 4b 5f 4d 41 54 43 48 2c  e3Expr(TK_MATCH,
1ee38 20 30 2c 20 73 71 6c 69 74 65 33 45 78 70 72 44   0, sqlite3ExprD
1ee39 75 70 28 70 52 69 67 68 74 29 2c 20 30 29 3b 0a  up(pRight), 0);.
1ee3a 20 20 20 20 20 20 69 64 78 4e 65 77 20 3d 20 77        idxNew = w
1ee3b 68 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72 74  hereClauseInsert
1ee3c 28 70 57 43 2c 20 70 4e 65 77 45 78 70 72 2c 20  (pWC, pNewExpr, 
1ee3d 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52  TERM_VIRTUAL|TER
1ee3e 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20  M_DYNAMIC);.    
1ee3f 20 20 70 4e 65 77 54 65 72 6d 20 3d 20 26 70 57    pNewTerm = &pW
1ee40 43 2d 3e 61 5b 69 64 78 4e 65 77 5d 3b 0a 20 20  C->a[idxNew];.  
1ee41 20 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e 70 72      pNewTerm->pr
1ee42 65 72 65 71 52 69 67 68 74 20 3d 20 70 72 65 72  ereqRight = prer
1ee43 65 71 45 78 70 72 3b 0a 20 20 20 20 20 20 70 4e  eqExpr;.      pN
1ee44 65 77 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73  ewTerm->leftCurs
1ee45 6f 72 20 3d 20 70 4c 65 66 74 2d 3e 69 54 61 62  or = pLeft->iTab
1ee46 6c 65 3b 0a 20 20 20 20 20 20 70 4e 65 77 54 65  le;.      pNewTe
1ee47 72 6d 2d 3e 6c 65 66 74 43 6f 6c 75 6d 6e 20 3d  rm->leftColumn =
1ee48 20 70 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3b   pLeft->iColumn;
1ee49 0a 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d  .      pNewTerm-
1ee4a 3e 65 4f 70 65 72 61 74 6f 72 20 3d 20 57 4f 5f  >eOperator = WO_
1ee4b 4d 41 54 43 48 3b 0a 20 20 20 20 20 20 70 4e 65  MATCH;.      pNe
1ee4c 77 54 65 72 6d 2d 3e 69 50 61 72 65 6e 74 20 3d  wTerm->iParent =
1ee4d 20 69 64 78 54 65 72 6d 3b 0a 20 20 20 20 20 20   idxTerm;.      
1ee4e 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b  pTerm = &pWC->a[
1ee4f 69 64 78 54 65 72 6d 5d 3b 0a 20 20 20 20 20 20  idxTerm];.      
1ee50 70 54 65 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20  pTerm->nChild = 
1ee51 31 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e  1;.      pTerm->
1ee52 66 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f 43 4f  flags |= TERM_CO
1ee53 50 49 45 44 3b 0a 20 20 20 20 20 20 70 4e 65 77  PIED;.      pNew
1ee54 54 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c 6c 20  Term->prereqAll 
1ee55 3d 20 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 41  = pTerm->prereqA
1ee56 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  ll;.    }.  }.#e
1ee57 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
1ee58 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1ee59 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74   */.}../*.** Ret
1ee5a 75 72 6e 20 54 52 55 45 20 69 66 20 61 6e 79 20  urn TRUE if any 
1ee5b 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  of the expressio
1ee5c 6e 73 20 69 6e 20 70 4c 69 73 74 2d 3e 61 5b 69  ns in pList->a[i
1ee5d 46 69 72 73 74 2e 2e 2e 5d 20 63 6f 6e 74 61 69  First...] contai
1ee5e 6e 0a 2a 2a 20 61 20 72 65 66 65 72 65 6e 63 65  n.** a reference
1ee5f 20 74 6f 20 61 6e 79 20 74 61 62 6c 65 20 6f 74   to any table ot
1ee60 68 65 72 20 74 68 61 6e 20 74 68 65 20 69 42 61  her than the iBa
1ee61 73 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61  se table..*/.sta
1ee62 74 69 63 20 69 6e 74 20 72 65 66 65 72 65 6e 63  tic int referenc
1ee63 65 73 4f 74 68 65 72 54 61 62 6c 65 73 28 0a 20  esOtherTables(. 
1ee64 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
1ee65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65  ,          /* Se
1ee66 61 72 63 68 20 65 78 70 72 65 73 73 69 6f 6e 73  arch expressions
1ee67 20 69 6e 20 74 68 73 20 6c 69 73 74 20 2a 2f 0a   in ths list */.
1ee68 20 20 45 78 70 72 4d 61 73 6b 53 65 74 20 2a 70    ExprMaskSet *p
1ee69 4d 61 73 6b 53 65 74 2c 20 20 20 20 2f 2a 20 4d  MaskSet,    /* M
1ee6a 61 70 70 69 6e 67 20 66 72 6f 6d 20 74 61 62 6c  apping from tabl
1ee6b 65 73 20 74 6f 20 62 69 74 6d 61 70 73 20 2a 2f  es to bitmaps */
1ee6c 0a 20 20 69 6e 74 20 69 46 69 72 73 74 2c 20 20  .  int iFirst,  
1ee6d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1ee6e 42 65 20 73 65 61 72 63 68 69 6e 67 20 77 69 74  Be searching wit
1ee6f 68 20 74 68 65 20 69 46 69 72 73 74 2d 74 68 20  h the iFirst-th 
1ee70 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20  expression */.  
1ee71 69 6e 74 20 69 42 61 73 65 20 20 20 20 20 20 20  int iBase       
1ee72 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 67 6e            /* Ign
1ee73 6f 72 65 20 72 65 66 65 72 65 6e 63 65 73 20 74  ore references t
1ee74 6f 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a  o this table */.
1ee75 29 7b 0a 20 20 42 69 74 6d 61 73 6b 20 61 6c 6c  ){.  Bitmask all
1ee76 6f 77 65 64 20 3d 20 7e 67 65 74 4d 61 73 6b 28  owed = ~getMask(
1ee77 70 4d 61 73 6b 53 65 74 2c 20 69 42 61 73 65 29  pMaskSet, iBase)
1ee78 3b 0a 20 20 77 68 69 6c 65 28 20 69 46 69 72 73  ;.  while( iFirs
1ee79 74 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29  t<pList->nExpr )
1ee7a 7b 0a 20 20 20 20 69 66 28 20 28 65 78 70 72 54  {.    if( (exprT
1ee7b 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53  ableUsage(pMaskS
1ee7c 65 74 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 46 69  et, pList->a[iFi
1ee7d 72 73 74 2b 2b 5d 2e 70 45 78 70 72 29 26 61 6c  rst++].pExpr)&al
1ee7e 6c 6f 77 65 64 29 21 3d 30 20 29 7b 0a 20 20 20  lowed)!=0 ){.   
1ee7f 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
1ee80 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
1ee81 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  0;.}.../*.** Thi
1ee82 73 20 72 6f 75 74 69 6e 65 20 64 65 63 69 64 65  s routine decide
1ee83 73 20 69 66 20 70 49 64 78 20 63 61 6e 20 62 65  s if pIdx can be
1ee84 20 75 73 65 64 20 74 6f 20 73 61 74 69 73 66 79   used to satisfy
1ee85 20 74 68 65 20 4f 52 44 45 52 20 42 59 0a 2a 2a   the ORDER BY.**
1ee86 20 63 6c 61 75 73 65 2e 20 20 49 66 20 69 74 20   clause.  If it 
1ee87 63 61 6e 2c 20 69 74 20 72 65 74 75 72 6e 73 20  can, it returns 
1ee88 31 2e 20 20 49 66 20 70 49 64 78 20 63 61 6e 6e  1.  If pIdx cann
1ee89 6f 74 20 73 61 74 69 73 66 79 20 74 68 65 0a 2a  ot satisfy the.*
1ee8a 2a 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  * ORDER BY claus
1ee8b 65 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  e, this routine 
1ee8c 72 65 74 75 72 6e 73 20 30 2e 0a 2a 2a 0a 2a 2a  returns 0..**.**
1ee8d 20 70 4f 72 64 65 72 42 79 20 69 73 20 61 6e 20   pOrderBy is an 
1ee8e 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
1ee8f 66 72 6f 6d 20 61 20 53 45 4c 45 43 54 20 73 74  from a SELECT st
1ee90 61 74 65 6d 65 6e 74 2e 20 20 70 54 61 62 20 69  atement.  pTab i
1ee91 73 20 74 68 65 0a 2a 2a 20 6c 65 66 74 2d 6d 6f  s the.** left-mo
1ee92 73 74 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20  st table in the 
1ee93 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74  FROM clause of t
1ee94 68 61 74 20 73 61 6d 65 20 53 45 4c 45 43 54 20  hat same SELECT 
1ee95 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 0a 2a 2a  statement and.**
1ee96 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 61   the table has a
1ee97 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f   cursor number o
1ee98 66 20 22 62 61 73 65 22 2e 20 20 70 49 64 78 20  f "base".  pIdx 
1ee99 69 73 20 61 6e 20 69 6e 64 65 78 20 6f 6e 20 70  is an index on p
1ee9a 54 61 62 2e 0a 2a 2a 0a 2a 2a 20 6e 45 71 43 6f  Tab..**.** nEqCo
1ee9b 6c 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  l is the number 
1ee9c 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 70 49  of columns of pI
1ee9d 64 78 20 74 68 61 74 20 61 72 65 20 75 73 65 64  dx that are used
1ee9e 20 61 73 20 65 71 75 61 6c 69 74 79 0a 2a 2a 20   as equality.** 
1ee9f 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 20 41 6e  constraints.  An
1eea0 79 20 6f 66 20 74 68 65 73 65 20 63 6f 6c 75 6d  y of these colum
1eea1 6e 73 20 6d 61 79 20 62 65 20 6d 69 73 73 69 6e  ns may be missin
1eea2 67 20 66 72 6f 6d 20 74 68 65 20 4f 52 44 45 52  g from the ORDER
1eea3 20 42 59 0a 2a 2a 20 63 6c 61 75 73 65 20 61 6e   BY.** clause an
1eea4 64 20 74 68 65 20 6d 61 74 63 68 20 63 61 6e 20  d the match can 
1eea5 73 74 69 6c 6c 20 62 65 20 61 20 73 75 63 63 65  still be a succe
1eea6 73 73 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 74 65  ss..**.** All te
1eea7 72 6d 73 20 6f 66 20 74 68 65 20 4f 52 44 45 52  rms of the ORDER
1eea8 20 42 59 20 74 68 61 74 20 6d 61 74 63 68 20 61   BY that match a
1eea9 67 61 69 6e 73 74 20 74 68 65 20 69 6e 64 65 78  gainst the index
1eeaa 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 0a   must be either.
1eeab 2a 2a 20 41 53 43 20 6f 72 20 44 45 53 43 2e 20  ** ASC or DESC. 
1eeac 20 28 54 65 72 6d 73 20 6f 66 20 74 68 65 20 4f   (Terms of the O
1eead 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 70  RDER BY clause p
1eeae 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 61  ast the end of a
1eeaf 20 55 4e 49 51 55 45 0a 2a 2a 20 69 6e 64 65 78   UNIQUE.** index
1eeb0 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20   do not need to 
1eeb1 73 61 74 69 73 66 79 20 74 68 69 73 20 63 6f 6e  satisfy this con
1eeb2 73 74 72 61 69 6e 74 2e 29 20 20 54 68 65 20 2a  straint.)  The *
1eeb3 70 62 52 65 76 20 76 61 6c 75 65 20 69 73 0a 2a  pbRev value is.*
1eeb4 2a 20 73 65 74 20 74 6f 20 31 20 69 66 20 74 68  * set to 1 if th
1eeb5 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
1eeb6 65 20 69 73 20 61 6c 6c 20 44 45 53 43 20 61 6e  e is all DESC an
1eeb7 64 20 69 74 20 69 73 20 73 65 74 20 74 6f 20 30  d it is set to 0
1eeb8 20 69 66 0a 2a 2a 20 74 68 65 20 4f 52 44 45 52   if.** the ORDER
1eeb9 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 61 6c   BY clause is al
1eeba 6c 20 41 53 43 2e 0a 2a 2f 0a 73 74 61 74 69 63  l ASC..*/.static
1eebb 20 69 6e 74 20 69 73 53 6f 72 74 69 6e 67 49 6e   int isSortingIn
1eebc 64 65 78 28 0a 20 20 50 61 72 73 65 20 2a 70 50  dex(.  Parse *pP
1eebd 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f  arse,          /
1eebe 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
1eebf 74 20 2a 2f 0a 20 20 45 78 70 72 4d 61 73 6b 53  t */.  ExprMaskS
1eec0 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 20 2f  et *pMaskSet,  /
1eec1 2a 20 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 74  * Mapping from t
1eec2 61 62 6c 65 20 69 6e 64 69 63 65 73 20 74 6f 20  able indices to 
1eec3 62 69 74 6d 61 70 73 20 2a 2f 0a 20 20 49 6e 64  bitmaps */.  Ind
1eec4 65 78 20 2a 70 49 64 78 2c 20 20 20 20 20 20 20  ex *pIdx,       
1eec5 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65       /* The inde
1eec6 78 20 77 65 20 61 72 65 20 74 65 73 74 69 6e 67  x we are testing
1eec7 20 2a 2f 0a 20 20 69 6e 74 20 62 61 73 65 2c 20   */.  int base, 
1eec8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1eec9 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66   Cursor number f
1eeca 6f 72 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20  or the table to 
1eecb 62 65 20 73 6f 72 74 65 64 20 2a 2f 0a 20 20 45  be sorted */.  E
1eecc 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42  xprList *pOrderB
1eecd 79 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 52  y,     /* The OR
1eece 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f  DER BY clause */
1eecf 0a 20 20 69 6e 74 20 6e 45 71 43 6f 6c 2c 20 20  .  int nEqCol,  
1eed0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
1eed1 6d 62 65 72 20 6f 66 20 69 6e 64 65 78 20 63 6f  mber of index co
1eed2 6c 75 6d 6e 73 20 77 69 74 68 20 3d 3d 20 63 6f  lumns with == co
1eed3 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 69  nstraints */.  i
1eed4 6e 74 20 2a 70 62 52 65 76 20 20 20 20 20 20 20  nt *pbRev       
1eed5 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 74 6f         /* Set to
1eed6 20 31 20 69 66 20 4f 52 44 45 52 20 42 59 20 69   1 if ORDER BY i
1eed7 73 20 44 45 53 43 20 2a 2f 0a 29 7b 0a 20 20 69  s DESC */.){.  i
1eed8 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20  nt i, j;        
1eed9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1eeda 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20  * Loop counters 
1eedb 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 4f 72 64  */.  int sortOrd
1eedc 65 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  er = 0;         
1eedd 20 20 20 20 20 2f 2a 20 58 4f 52 20 6f 66 20 69       /* XOR of i
1eede 6e 64 65 78 20 61 6e 64 20 4f 52 44 45 52 20 42  ndex and ORDER B
1eedf 59 20 73 6f 72 74 20 64 69 72 65 63 74 69 6f 6e  Y sort direction
1eee0 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b   */.  int nTerm;
1eee1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1eee2 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1eee3 6f 66 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d  of ORDER BY term
1eee4 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 45 78  s */.  struct Ex
1eee5 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 54 65  prList_item *pTe
1eee6 72 6d 3b 20 20 20 20 2f 2a 20 41 20 74 65 72 6d  rm;    /* A term
1eee7 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59   of the ORDER BY
1eee8 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 71 6c   clause */.  sql
1eee9 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
1eeea 65 2d 3e 64 62 3b 0a 0a 20 20 61 73 73 65 72 74  e->db;..  assert
1eeeb 28 20 70 4f 72 64 65 72 42 79 21 3d 30 20 29 3b  ( pOrderBy!=0 );
1eeec 0a 20 20 6e 54 65 72 6d 20 3d 20 70 4f 72 64 65  .  nTerm = pOrde
1eeed 72 42 79 2d 3e 6e 45 78 70 72 3b 0a 20 20 61 73  rBy->nExpr;.  as
1eeee 73 65 72 74 28 20 6e 54 65 72 6d 3e 30 20 29 3b  sert( nTerm>0 );
1eeef 0a 0a 20 20 2f 2a 20 4d 61 74 63 68 20 74 65 72  ..  /* Match ter
1eef0 6d 73 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20  ms of the ORDER 
1eef1 42 59 20 63 6c 61 75 73 65 20 61 67 61 69 6e 73  BY clause agains
1eef2 74 20 63 6f 6c 75 6d 6e 73 20 6f 66 0a 20 20 2a  t columns of.  *
1eef3 2a 20 74 68 65 20 69 6e 64 65 78 2e 0a 20 20 2a  * the index..  *
1eef4 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74  *.  ** Note that
1eef5 20 69 6e 64 69 63 65 73 20 68 61 76 65 20 70 49   indices have pI
1eef6 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 72 65 67 75  dx->nColumn regu
1eef7 6c 61 72 20 63 6f 6c 75 6d 6e 73 20 70 6c 75 73  lar columns plus
1eef8 0a 20 20 2a 2a 20 6f 6e 65 20 61 64 64 69 74 69  .  ** one additi
1eef9 6f 6e 61 6c 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74  onal column cont
1eefa 61 69 6e 69 6e 67 20 74 68 65 20 72 6f 77 69 64  aining the rowid
1eefb 2e 20 20 54 68 65 20 72 6f 77 69 64 20 63 6f 6c  .  The rowid col
1eefc 75 6d 6e 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20  umn.  ** of the 
1eefd 69 6e 64 65 78 20 69 73 20 61 6c 73 6f 20 61 6c  index is also al
1eefe 6c 6f 77 65 64 20 74 6f 20 6d 61 74 63 68 20 61  lowed to match a
1eeff 67 61 69 6e 73 74 20 74 68 65 20 4f 52 44 45 52  gainst the ORDER
1ef00 20 42 59 0a 20 20 2a 2a 20 63 6c 61 75 73 65 2e   BY.  ** clause.
1ef01 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 6a 3d  .  */.  for(i=j=
1ef02 30 2c 20 70 54 65 72 6d 3d 70 4f 72 64 65 72 42  0, pTerm=pOrderB
1ef03 79 2d 3e 61 3b 20 6a 3c 6e 54 65 72 6d 20 26 26  y->a; j<nTerm &&
1ef04 20 69 3c 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d   i<=pIdx->nColum
1ef05 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 45 78 70  n; i++){.    Exp
1ef06 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20  r *pExpr;       
1ef07 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f  /* The expressio
1ef08 6e 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42  n of the ORDER B
1ef09 59 20 70 54 65 72 6d 20 2a 2f 0a 20 20 20 20 43  Y pTerm */.    C
1ef0a 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20  ollSeq *pColl;  
1ef0b 20 20 2f 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69    /* The collati
1ef0c 6e 67 20 73 65 71 75 65 6e 63 65 20 6f 66 20 70  ng sequence of p
1ef0d 45 78 70 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20  Expr */.    int 
1ef0e 74 65 72 6d 53 6f 72 74 4f 72 64 65 72 3b 20 2f  termSortOrder; /
1ef0f 2a 20 53 6f 72 74 20 6f 72 64 65 72 20 66 6f 72  * Sort order for
1ef10 20 74 68 69 73 20 74 65 72 6d 20 2a 2f 0a 20 20   this term */.  
1ef11 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20    int iColumn;  
1ef12 20 20 20 20 20 2f 2a 20 54 68 65 20 69 2d 74 68       /* The i-th
1ef13 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 69   column of the i
1ef14 6e 64 65 78 2e 20 20 2d 31 20 66 6f 72 20 72 6f  ndex.  -1 for ro
1ef15 77 69 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  wid */.    int i
1ef16 53 6f 72 74 4f 72 64 65 72 3b 20 20 20 20 2f 2a  SortOrder;    /*
1ef17 20 31 20 66 6f 72 20 44 45 53 43 2c 20 30 20 66   1 for DESC, 0 f
1ef18 6f 72 20 41 53 43 20 6f 6e 20 74 68 65 20 69 2d  or ASC on the i-
1ef19 74 68 20 69 6e 64 65 78 20 74 65 72 6d 20 2a 2f  th index term */
1ef1a 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
1ef1b 2a 7a 43 6f 6c 6c 3b 20 2f 2a 20 4e 61 6d 65 20  *zColl; /* Name 
1ef1c 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67  of the collating
1ef1d 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 69 2d   sequence for i-
1ef1e 74 68 20 69 6e 64 65 78 20 74 65 72 6d 20 2a 2f  th index term */
1ef1f 0a 0a 20 20 20 20 70 45 78 70 72 20 3d 20 70 54  ..    pExpr = pT
1ef20 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20  erm->pExpr;.    
1ef21 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54  if( pExpr->op!=T
1ef22 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 70 45 78 70  K_COLUMN || pExp
1ef23 72 2d 3e 69 54 61 62 6c 65 21 3d 62 61 73 65 20  r->iTable!=base 
1ef24 29 7b 0a 20 20 20 20 20 20 2f 2a 20 43 61 6e 20  ){.      /* Can 
1ef25 6e 6f 74 20 75 73 65 20 61 6e 20 69 6e 64 65 78  not use an index
1ef26 20 73 6f 72 74 20 6f 6e 20 61 6e 79 74 68 69 6e   sort on anythin
1ef27 67 20 74 68 61 74 20 69 73 20 6e 6f 74 20 61 20  g that is not a 
1ef28 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 0a 20 20  column in the.  
1ef29 20 20 20 20 2a 2a 20 6c 65 66 74 2d 6d 6f 73 74      ** left-most
1ef2a 20 74 61 62 6c 65 20 6f 66 20 74 68 65 20 46 52   table of the FR
1ef2b 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20  OM clause */.   
1ef2c 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1ef2d 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69      pColl = sqli
1ef2e 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70  te3ExprCollSeq(p
1ef2f 50 61 72 73 65 2c 20 70 45 78 70 72 29 3b 0a 20  Parse, pExpr);. 
1ef30 20 20 20 69 66 28 20 21 70 43 6f 6c 6c 20 29 7b     if( !pColl ){
1ef31 0a 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 64  .      pColl = d
1ef32 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20  b->pDfltColl;.  
1ef33 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3c 70 49    }.    if( i<pI
1ef34 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20  dx->nColumn ){. 
1ef35 20 20 20 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 70       iColumn = p
1ef36 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d  Idx->aiColumn[i]
1ef37 3b 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c  ;.      if( iCol
1ef38 75 6d 6e 3d 3d 70 49 64 78 2d 3e 70 54 61 62 6c  umn==pIdx->pTabl
1ef39 65 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20  e->iPKey ){.    
1ef3a 20 20 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31      iColumn = -1
1ef3b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1ef3c 69 53 6f 72 74 4f 72 64 65 72 20 3d 20 70 49 64  iSortOrder = pId
1ef3d 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d  x->aSortOrder[i]
1ef3e 3b 0a 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20  ;.      zColl = 
1ef3f 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 3b  pIdx->azColl[i];
1ef40 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1ef41 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a    iColumn = -1;.
1ef42 20 20 20 20 20 20 69 53 6f 72 74 4f 72 64 65 72        iSortOrder
1ef43 20 3d 20 30 3b 0a 20 20 20 20 20 20 7a 43 6f 6c   = 0;.      zCol
1ef44 6c 20 3d 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65  l = pColl->zName
1ef45 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1ef46 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 21 3d  pExpr->iColumn!=
1ef47 69 43 6f 6c 75 6d 6e 20 7c 7c 20 73 71 6c 69 74  iColumn || sqlit
1ef48 65 33 53 74 72 49 43 6d 70 28 70 43 6f 6c 6c 2d  e3StrICmp(pColl-
1ef49 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 6c 29 20 29  >zName, zColl) )
1ef4a 7b 0a 20 20 20 20 20 20 2f 2a 20 54 65 72 6d 20  {.      /* Term 
1ef4b 6a 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42  j of the ORDER B
1ef4c 59 20 63 6c 61 75 73 65 20 64 6f 65 73 20 6e 6f  Y clause does no
1ef4d 74 20 6d 61 74 63 68 20 63 6f 6c 75 6d 6e 20 69  t match column i
1ef4e 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 2a 2f   of the index */
1ef4f 0a 20 20 20 20 20 20 69 66 28 20 69 3c 6e 45 71  .      if( i<nEq
1ef50 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  Col ){.        /
1ef51 2a 20 49 66 20 61 6e 20 69 6e 64 65 78 20 63 6f  * If an index co
1ef52 6c 75 6d 6e 20 74 68 61 74 20 69 73 20 63 6f 6e  lumn that is con
1ef53 73 74 72 61 69 6e 65 64 20 62 79 20 3d 3d 20 66  strained by == f
1ef54 61 69 6c 73 20 74 6f 20 6d 61 74 63 68 20 61 6e  ails to match an
1ef55 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f 52 44 45  .        ** ORDE
1ef56 52 20 42 59 20 74 65 72 6d 2c 20 74 68 61 74 20  R BY term, that 
1ef57 69 73 20 4f 4b 2e 20 20 4a 75 73 74 20 69 67 6e  is OK.  Just ign
1ef58 6f 72 65 20 74 68 61 74 20 63 6f 6c 75 6d 6e 20  ore that column 
1ef59 6f 66 20 74 68 65 20 69 6e 64 65 78 0a 20 20 20  of the index.   
1ef5a 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
1ef5b 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
1ef5c 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f  }else{.        /
1ef5d 2a 20 49 66 20 61 6e 20 69 6e 64 65 78 20 63 6f  * If an index co
1ef5e 6c 75 6d 6e 20 66 61 69 6c 73 20 74 6f 20 6d 61  lumn fails to ma
1ef5f 74 63 68 20 61 6e 64 20 69 73 20 6e 6f 74 20 63  tch and is not c
1ef60 6f 6e 73 74 72 61 69 6e 65 64 20 62 79 20 3d 3d  onstrained by ==
1ef61 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 6e  .        ** then
1ef62 20 74 68 65 20 69 6e 64 65 78 20 63 61 6e 6e 6f   the index canno
1ef63 74 20 73 61 74 69 73 66 79 20 74 68 65 20 4f 52  t satisfy the OR
1ef64 44 45 52 20 42 59 20 63 6f 6e 73 74 72 61 69 6e  DER BY constrain
1ef65 74 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  t..        */.  
1ef66 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
1ef67 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1ef68 20 20 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e    assert( pIdx->
1ef69 61 53 6f 72 74 4f 72 64 65 72 21 3d 30 20 29 3b  aSortOrder!=0 );
1ef6a 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 65  .    assert( pTe
1ef6b 72 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 3d 3d 30  rm->sortOrder==0
1ef6c 20 7c 7c 20 70 54 65 72 6d 2d 3e 73 6f 72 74 4f   || pTerm->sortO
1ef6d 72 64 65 72 3d 3d 31 20 29 3b 0a 20 20 20 20 61  rder==1 );.    a
1ef6e 73 73 65 72 74 28 20 69 53 6f 72 74 4f 72 64 65  ssert( iSortOrde
1ef6f 72 3d 3d 30 20 7c 7c 20 69 53 6f 72 74 4f 72 64  r==0 || iSortOrd
1ef70 65 72 3d 3d 31 20 29 3b 0a 20 20 20 20 74 65 72  er==1 );.    ter
1ef71 6d 53 6f 72 74 4f 72 64 65 72 20 3d 20 69 53 6f  mSortOrder = iSo
1ef72 72 74 4f 72 64 65 72 20 5e 20 70 54 65 72 6d 2d  rtOrder ^ pTerm-
1ef73 3e 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20  >sortOrder;.    
1ef74 69 66 28 20 69 3e 6e 45 71 43 6f 6c 20 29 7b 0a  if( i>nEqCol ){.
1ef75 20 20 20 20 20 20 69 66 28 20 74 65 72 6d 53 6f        if( termSo
1ef76 72 74 4f 72 64 65 72 21 3d 73 6f 72 74 4f 72 64  rtOrder!=sortOrd
1ef77 65 72 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  er ){.        /*
1ef78 20 49 6e 64 69 63 65 73 20 63 61 6e 20 6f 6e 6c   Indices can onl
1ef79 79 20 62 65 20 75 73 65 64 20 69 66 20 61 6c 6c  y be used if all
1ef7a 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73 20   ORDER BY terms 
1ef7b 70 61 73 74 20 74 68 65 0a 20 20 20 20 20 20 20  past the.       
1ef7c 20 2a 2a 20 65 71 75 61 6c 69 74 79 20 63 6f 6e   ** equality con
1ef7d 73 74 72 61 69 6e 74 73 20 61 72 65 20 61 6c 6c  straints are all
1ef7e 20 65 69 74 68 65 72 20 44 45 53 43 20 6f 72 20   either DESC or 
1ef7f 41 53 43 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  ASC. */.        
1ef80 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20  return 0;.      
1ef81 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
1ef82 20 20 20 73 6f 72 74 4f 72 64 65 72 20 3d 20 74     sortOrder = t
1ef83 65 72 6d 53 6f 72 74 4f 72 64 65 72 3b 0a 20 20  ermSortOrder;.  
1ef84 20 20 7d 0a 20 20 20 20 6a 2b 2b 3b 0a 20 20 20    }.    j++;.   
1ef85 20 70 54 65 72 6d 2b 2b 3b 0a 20 20 20 20 69 66   pTerm++;.    if
1ef86 28 20 69 43 6f 6c 75 6d 6e 3c 30 20 26 26 20 21  ( iColumn<0 && !
1ef87 72 65 66 65 72 65 6e 63 65 73 4f 74 68 65 72 54  referencesOtherT
1ef88 61 62 6c 65 73 28 70 4f 72 64 65 72 42 79 2c 20  ables(pOrderBy, 
1ef89 70 4d 61 73 6b 53 65 74 2c 20 6a 2c 20 62 61 73  pMaskSet, j, bas
1ef8a 65 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49  e) ){.      /* I
1ef8b 66 20 74 68 65 20 69 6e 64 65 78 65 64 20 63 6f  f the indexed co
1ef8c 6c 75 6d 6e 20 69 73 20 74 68 65 20 70 72 69 6d  lumn is the prim
1ef8d 61 72 79 20 6b 65 79 20 61 6e 64 20 65 76 65 72  ary key and ever
1ef8e 79 74 68 69 6e 67 20 6d 61 74 63 68 65 73 0a 20  ything matches. 
1ef8f 20 20 20 20 20 2a 2a 20 73 6f 20 66 61 72 20 61       ** so far a
1ef90 6e 64 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 4f  nd none of the O
1ef91 52 44 45 52 20 42 59 20 74 65 72 6d 73 20 74 6f  RDER BY terms to
1ef92 20 74 68 65 20 72 69 67 68 74 20 72 65 66 65 72   the right refer
1ef93 65 6e 63 65 20 6f 74 68 65 72 0a 20 20 20 20 20  ence other.     
1ef94 20 2a 2a 20 74 61 62 6c 65 73 20 69 6e 20 74 68   ** tables in th
1ef95 65 20 6a 6f 69 6e 2c 20 74 68 65 6e 20 77 65 20  e join, then we 
1ef96 61 72 65 20 61 73 73 75 72 65 64 20 74 68 61 74  are assured that
1ef97 20 74 68 65 20 69 6e 64 65 78 20 63 61 6e 20 62   the index can b
1ef98 65 20 75 73 65 64 20 0a 20 20 20 20 20 20 2a 2a  e used .      **
1ef99 20 74 6f 20 73 6f 72 74 20 62 65 63 61 75 73 65   to sort because
1ef9a 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79   the primary key
1ef9b 20 69 73 20 75 6e 69 71 75 65 20 61 6e 64 20 73   is unique and s
1ef9c 6f 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 6f 74  o none of the ot
1ef9d 68 65 72 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6c  her.      ** col
1ef9e 75 6d 6e 73 20 77 69 6c 6c 20 6d 61 6b 65 20 61  umns will make a
1ef9f 6e 79 20 64 69 66 66 65 72 65 6e 63 65 0a 20 20  ny difference.  
1efa0 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 6a 20 3d      */.      j =
1efa1 20 6e 54 65 72 6d 3b 0a 20 20 20 20 7d 0a 20 20   nTerm;.    }.  
1efa2 7d 0a 0a 20 20 2a 70 62 52 65 76 20 3d 20 73 6f  }..  *pbRev = so
1efa3 72 74 4f 72 64 65 72 21 3d 30 3b 0a 20 20 69 66  rtOrder!=0;.  if
1efa4 28 20 6a 3e 3d 6e 54 65 72 6d 20 29 7b 0a 20 20  ( j>=nTerm ){.  
1efa5 20 20 2f 2a 20 41 6c 6c 20 74 65 72 6d 73 20 6f    /* All terms o
1efa6 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  f the ORDER BY c
1efa7 6c 61 75 73 65 20 61 72 65 20 63 6f 76 65 72 65  lause are covere
1efa8 64 20 62 79 20 74 68 69 73 20 69 6e 64 65 78 20  d by this index 
1efa9 73 6f 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 69  so.    ** this i
1efaa 6e 64 65 78 20 63 61 6e 20 62 65 20 75 73 65 64  ndex can be used
1efab 20 66 6f 72 20 73 6f 72 74 69 6e 67 2e 20 2a 2f   for sorting. */
1efac 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
1efad 20 7d 0a 20 20 69 66 28 20 70 49 64 78 2d 3e 6f   }.  if( pIdx->o
1efae 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20  nError!=OE_None 
1efaf 26 26 20 69 3d 3d 70 49 64 78 2d 3e 6e 43 6f 6c  && i==pIdx->nCol
1efb0 75 6d 6e 0a 20 20 20 20 20 20 26 26 20 21 72 65  umn.      && !re
1efb1 66 65 72 65 6e 63 65 73 4f 74 68 65 72 54 61 62  ferencesOtherTab
1efb2 6c 65 73 28 70 4f 72 64 65 72 42 79 2c 20 70 4d  les(pOrderBy, pM
1efb3 61 73 6b 53 65 74 2c 20 6a 2c 20 62 61 73 65 29  askSet, j, base)
1efb4 20 29 7b 0a 20 20 20 20 2f 2a 20 41 6c 6c 20 74   ){.    /* All t
1efb5 65 72 6d 73 20 6f 66 20 74 68 69 73 20 69 6e 64  erms of this ind
1efb6 65 78 20 6d 61 74 63 68 20 73 6f 6d 65 20 70 72  ex match some pr
1efb7 65 66 69 78 20 6f 66 20 74 68 65 20 4f 52 44 45  efix of the ORDE
1efb8 52 20 42 59 20 63 6c 61 75 73 65 0a 20 20 20 20  R BY clause.    
1efb9 2a 2a 20 61 6e 64 20 74 68 65 20 69 6e 64 65 78  ** and the index
1efba 20 69 73 20 55 4e 49 51 55 45 20 61 6e 64 20 6e   is UNIQUE and n
1efbb 6f 20 74 65 72 6d 73 20 6f 6e 20 74 68 65 20 74  o terms on the t
1efbc 61 69 6c 20 6f 66 20 74 68 65 20 4f 52 44 45 52  ail of the ORDER
1efbd 20 42 59 0a 20 20 20 20 2a 2a 20 63 6c 61 75 73   BY.    ** claus
1efbe 65 20 72 65 66 65 72 65 6e 63 65 20 6f 74 68 65  e reference othe
1efbf 72 20 74 61 62 6c 65 73 20 69 6e 20 61 20 6a 6f  r tables in a jo
1efc0 69 6e 2e 20 20 49 66 20 74 68 69 73 20 69 73 20  in.  If this is 
1efc1 61 6c 6c 20 74 72 75 65 20 74 68 65 6e 0a 20 20  all true then.  
1efc2 20 20 2a 2a 20 74 68 65 20 6f 72 64 65 72 20 62    ** the order b
1efc3 79 20 63 6c 61 75 73 65 20 69 73 20 73 75 70 65  y clause is supe
1efc4 72 66 6c 75 6f 75 73 2e 20 2a 2f 0a 20 20 20 20  rfluous. */.    
1efc5 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20  return 1;.  }.  
1efc6 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
1efc7 2a 2a 20 43 68 65 63 6b 20 74 61 62 6c 65 20 74  ** Check table t
1efc8 6f 20 73 65 65 20 69 66 20 74 68 65 20 4f 52 44  o see if the ORD
1efc9 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 6e 20  ER BY clause in 
1efca 70 4f 72 64 65 72 42 79 20 63 61 6e 20 62 65 20  pOrderBy can be 
1efcb 73 61 74 69 73 66 69 65 64 0a 2a 2a 20 62 79 20  satisfied.** by 
1efcc 73 6f 72 74 69 6e 67 20 69 6e 20 6f 72 64 65 72  sorting in order
1efcd 20 6f 66 20 52 4f 57 49 44 2e 20 20 52 65 74 75   of ROWID.  Retu
1efce 72 6e 20 74 72 75 65 20 69 66 20 73 6f 20 61 6e  rn true if so an
1efcf 64 20 73 65 74 20 2a 70 62 52 65 76 20 74 6f 20  d set *pbRev to 
1efd0 62 65 0a 2a 2a 20 74 72 75 65 20 66 6f 72 20 72  be.** true for r
1efd1 65 76 65 72 73 65 20 52 4f 57 49 44 20 61 6e 64  everse ROWID and
1efd2 20 66 61 6c 73 65 20 66 6f 72 20 66 6f 72 77 61   false for forwa
1efd3 72 64 20 52 4f 57 49 44 20 6f 72 64 65 72 2e 0a  rd ROWID order..
1efd4 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 6f  */.static int so
1efd5 72 74 61 62 6c 65 42 79 52 6f 77 69 64 28 0a 20  rtableByRowid(. 
1efd6 20 69 6e 74 20 62 61 73 65 2c 20 20 20 20 20 20   int base,      
1efd7 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73           /* Curs
1efd8 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 61  or number for ta
1efd9 62 6c 65 20 74 6f 20 62 65 20 73 6f 72 74 65 64  ble to be sorted
1efda 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
1efdb 70 4f 72 64 65 72 42 79 2c 20 20 20 20 20 2f 2a  pOrderBy,     /*
1efdc 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   The ORDER BY cl
1efdd 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4d 61  ause */.  ExprMa
1efde 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c  skSet *pMaskSet,
1efdf 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 66 72 6f    /* Mapping fro
1efe0 6d 20 74 61 62 6c 65 73 20 74 6f 20 62 69 74 6d  m tables to bitm
1efe1 61 70 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 62  aps */.  int *pb
1efe2 52 65 76 20 20 20 20 20 20 20 20 20 20 20 20 20  Rev             
1efe3 20 2f 2a 20 53 65 74 20 74 6f 20 31 20 69 66 20   /* Set to 1 if 
1efe4 4f 52 44 45 52 20 42 59 20 69 73 20 44 45 53 43  ORDER BY is DESC
1efe5 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70   */.){.  Expr *p
1efe6 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 72  ;..  assert( pOr
1efe7 64 65 72 42 79 21 3d 30 20 29 3b 0a 20 20 61 73  derBy!=0 );.  as
1efe8 73 65 72 74 28 20 70 4f 72 64 65 72 42 79 2d 3e  sert( pOrderBy->
1efe9 6e 45 78 70 72 3e 30 20 29 3b 0a 20 20 70 20 3d  nExpr>0 );.  p =
1efea 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 30 5d 2e   pOrderBy->a[0].
1efeb 70 45 78 70 72 3b 0a 20 20 69 66 28 20 70 2d 3e  pExpr;.  if( p->
1efec 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26  op==TK_COLUMN &&
1efed 20 70 2d 3e 69 54 61 62 6c 65 3d 3d 62 61 73 65   p->iTable==base
1efee 20 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d   && p->iColumn==
1efef 2d 31 0a 20 20 20 20 26 26 20 21 72 65 66 65 72  -1.    && !refer
1eff0 65 6e 63 65 73 4f 74 68 65 72 54 61 62 6c 65 73  encesOtherTables
1eff1 28 70 4f 72 64 65 72 42 79 2c 20 70 4d 61 73 6b  (pOrderBy, pMask
1eff2 53 65 74 2c 20 31 2c 20 62 61 73 65 29 20 29 7b  Set, 1, base) ){
1eff3 0a 20 20 20 20 2a 70 62 52 65 76 20 3d 20 70 4f  .    *pbRev = pO
1eff4 72 64 65 72 42 79 2d 3e 61 5b 30 5d 2e 73 6f 72  rderBy->a[0].sor
1eff5 74 4f 72 64 65 72 3b 0a 20 20 20 20 72 65 74 75  tOrder;.    retu
1eff6 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rn 1;.  }.  retu
1eff7 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  rn 0;.}../*.** P
1eff8 72 65 70 61 72 65 20 61 20 63 72 75 64 65 20 65  repare a crude e
1eff9 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65 20 6c  stimate of the l
1effa 6f 67 61 72 69 74 68 6d 20 6f 66 20 74 68 65 20  ogarithm of the 
1effb 69 6e 70 75 74 20 76 61 6c 75 65 2e 0a 2a 2a 20  input value..** 
1effc 54 68 65 20 72 65 73 75 6c 74 73 20 6e 65 65 64  The results need
1effd 20 6e 6f 74 20 62 65 20 65 78 61 63 74 2e 20 20   not be exact.  
1effe 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 75 73 65  This is only use
1efff 64 20 66 6f 72 20 65 73 74 69 6d 61 74 69 6e 67  d for estimating
1f000 0a 2a 2a 20 74 68 65 20 74 6f 74 61 6c 20 63 6f  .** the total co
1f001 73 74 20 6f 66 20 70 65 72 66 6f 72 6d 69 6e 67  st of performing
1f002 20 6f 70 65 72 61 74 69 6e 67 73 20 77 69 74 68   operatings with
1f003 20 4f 28 6c 6f 67 4e 29 20 6f 72 20 4f 28 4e 6c   O(logN) or O(Nl
1f004 6f 67 4e 29 0a 2a 2a 20 63 6f 6d 70 6c 65 78 69  ogN).** complexi
1f005 74 79 2e 20 20 42 65 63 61 75 73 65 20 4e 20 69  ty.  Because N i
1f006 73 20 6a 75 73 74 20 61 20 67 75 65 73 73 2c 20  s just a guess, 
1f007 69 74 20 69 73 20 6e 6f 20 67 72 65 61 74 20 74  it is no great t
1f008 72 61 67 65 64 79 20 69 66 0a 2a 2a 20 6c 6f 67  ragedy if.** log
1f009 4e 20 69 73 20 61 20 6c 69 74 74 6c 65 20 6f 66  N is a little of
1f00a 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 64 6f 75  f..*/.static dou
1f00b 62 6c 65 20 65 73 74 4c 6f 67 28 64 6f 75 62 6c  ble estLog(doubl
1f00c 65 20 4e 29 7b 0a 20 20 64 6f 75 62 6c 65 20 6c  e N){.  double l
1f00d 6f 67 4e 20 3d 20 31 3b 0a 20 20 64 6f 75 62 6c  ogN = 1;.  doubl
1f00e 65 20 78 20 3d 20 31 30 3b 0a 20 20 77 68 69 6c  e x = 10;.  whil
1f00f 65 28 20 4e 3e 78 20 29 7b 0a 20 20 20 20 6c 6f  e( N>x ){.    lo
1f010 67 4e 20 2b 3d 20 31 3b 0a 20 20 20 20 78 20 2a  gN += 1;.    x *
1f011 3d 20 31 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75  = 10;.  }.  retu
1f012 72 6e 20 6c 6f 67 4e 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn logN;.}../*.*
1f013 2a 20 54 77 6f 20 72 6f 75 74 69 6e 65 73 20 66  * Two routines f
1f014 6f 72 20 70 72 69 6e 74 69 6e 67 20 74 68 65 20  or printing the 
1f015 63 6f 6e 74 65 6e 74 20 6f 66 20 61 6e 20 73 71  content of an sq
1f016 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f  lite3_index_info
1f017 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20  .** structure.  
1f018 55 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67  Used for testing
1f019 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20 6f   and debugging o
1f01a 6e 6c 79 2e 20 20 49 66 20 6e 65 69 74 68 65 72  nly.  If neither
1f01b 0a 2a 2a 20 53 51 4c 49 54 45 5f 54 45 53 54 20  .** SQLITE_TEST 
1f01c 6f 72 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20  or SQLITE_DEBUG 
1f01d 61 72 65 20 64 65 66 69 6e 65 64 2c 20 74 68 65  are defined, the
1f01e 6e 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  n these routines
1f01f 0a 2a 2a 20 61 72 65 20 6e 6f 2d 6f 70 73 2e 0a  .** are no-ops..
1f020 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  */.#if !defined(
1f021 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
1f022 55 41 4c 54 41 42 4c 45 29 20 26 26 20 64 65 66  UALTABLE) && def
1f023 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
1f024 47 29 0a 73 74 61 74 69 63 20 76 6f 69 64 20 54  G).static void T
1f025 52 41 43 45 5f 49 44 58 5f 49 4e 50 55 54 53 28  RACE_IDX_INPUTS(
1f026 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
1f027 66 6f 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b  fo *p){.  int i;
1f028 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f  .  if( !sqlite3_
1f029 77 68 65 72 65 5f 74 72 61 63 65 20 29 20 72 65  where_trace ) re
1f02a 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  turn;.  for(i=0;
1f02b 20 69 3c 70 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e   i<p->nConstrain
1f02c 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c  t; i++){.    sql
1f02d 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
1f02e 22 20 20 63 6f 6e 73 74 72 61 69 6e 74 5b 25 64  "  constraint[%d
1f02f 5d 3a 20 63 6f 6c 3d 25 64 20 74 65 72 6d 69 64  ]: col=%d termid
1f030 3d 25 64 20 6f 70 3d 25 64 20 75 73 61 62 6c 65  =%d op=%d usable
1f031 64 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20  d=%d\n",.       
1f032 69 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61 43 6f  i,.       p->aCo
1f033 6e 73 74 72 61 69 6e 74 5b 69 5d 2e 69 43 6f 6c  nstraint[i].iCol
1f034 75 6d 6e 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61  umn,.       p->a
1f035 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 2e 69 54  Constraint[i].iT
1f036 65 72 6d 4f 66 66 73 65 74 2c 0a 20 20 20 20 20  ermOffset,.     
1f037 20 20 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74    p->aConstraint
1f038 5b 69 5d 2e 6f 70 2c 0a 20 20 20 20 20 20 20 70  [i].op,.       p
1f039 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d  ->aConstraint[i]
1f03a 2e 75 73 61 62 6c 65 29 3b 0a 20 20 7d 0a 20 20  .usable);.  }.  
1f03b 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f  for(i=0; i<p->nO
1f03c 72 64 65 72 42 79 3b 20 69 2b 2b 29 7b 0a 20 20  rderBy; i++){.  
1f03d 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
1f03e 69 6e 74 66 28 22 20 20 6f 72 64 65 72 62 79 5b  intf("  orderby[
1f03f 25 64 5d 3a 20 63 6f 6c 3d 25 64 20 64 65 73 63  %d]: col=%d desc
1f040 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 69  =%d\n",.       i
1f041 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61 4f 72 64  ,.       p->aOrd
1f042 65 72 42 79 5b 69 5d 2e 69 43 6f 6c 75 6d 6e 2c  erBy[i].iColumn,
1f043 0a 20 20 20 20 20 20 20 70 2d 3e 61 4f 72 64 65  .       p->aOrde
1f044 72 42 79 5b 69 5d 2e 64 65 73 63 29 3b 0a 20 20  rBy[i].desc);.  
1f045 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  }.}.static void 
1f046 54 52 41 43 45 5f 49 44 58 5f 4f 55 54 50 55 54  TRACE_IDX_OUTPUT
1f047 53 28 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  S(sqlite3_index_
1f048 69 6e 66 6f 20 2a 70 29 7b 0a 20 20 69 6e 74 20  info *p){.  int 
1f049 69 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65  i;.  if( !sqlite
1f04a 33 5f 77 68 65 72 65 5f 74 72 61 63 65 20 29 20  3_where_trace ) 
1f04b 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d  return;.  for(i=
1f04c 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6e 73 74 72 61  0; i<p->nConstra
1f04d 69 6e 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  int; i++){.    s
1f04e 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
1f04f 66 28 22 20 20 75 73 61 67 65 5b 25 64 5d 3a 20  f("  usage[%d]: 
1f050 61 72 67 76 49 64 78 3d 25 64 20 6f 6d 69 74 3d  argvIdx=%d omit=
1f051 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 69 2c  %d\n",.       i,
1f052 0a 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e 73  .       p->aCons
1f053 74 72 61 69 6e 74 55 73 61 67 65 5b 69 5d 2e 61  traintUsage[i].a
1f054 72 67 76 49 6e 64 65 78 2c 0a 20 20 20 20 20 20  rgvIndex,.      
1f055 20 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55   p->aConstraintU
1f056 73 61 67 65 5b 69 5d 2e 6f 6d 69 74 29 3b 0a 20  sage[i].omit);. 
1f057 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 65 62 75   }.  sqlite3Debu
1f058 67 50 72 69 6e 74 66 28 22 20 20 69 64 78 4e 75  gPrintf("  idxNu
1f059 6d 3d 25 64 5c 6e 22 2c 20 70 2d 3e 69 64 78 4e  m=%d\n", p->idxN
1f05a 75 6d 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65  um);.  sqlite3De
1f05b 62 75 67 50 72 69 6e 74 66 28 22 20 20 69 64 78  bugPrintf("  idx
1f05c 53 74 72 3d 25 73 5c 6e 22 2c 20 70 2d 3e 69 64  Str=%s\n", p->id
1f05d 78 53 74 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  xStr);.  sqlite3
1f05e 44 65 62 75 67 50 72 69 6e 74 66 28 22 20 20 6f  DebugPrintf("  o
1f05f 72 64 65 72 42 79 43 6f 6e 73 75 6d 65 64 3d 25  rderByConsumed=%
1f060 64 5c 6e 22 2c 20 70 2d 3e 6f 72 64 65 72 42 79  d\n", p->orderBy
1f061 43 6f 6e 73 75 6d 65 64 29 3b 0a 20 20 73 71 6c  Consumed);.  sql
1f062 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
1f063 22 20 20 65 73 74 69 6d 61 74 65 64 43 6f 73 74  "  estimatedCost
1f064 3d 25 67 5c 6e 22 2c 20 70 2d 3e 65 73 74 69 6d  =%g\n", p->estim
1f065 61 74 65 64 43 6f 73 74 29 3b 0a 7d 0a 23 65 6c  atedCost);.}.#el
1f066 73 65 0a 23 64 65 66 69 6e 65 20 54 52 41 43 45  se.#define TRACE
1f067 5f 49 44 58 5f 49 4e 50 55 54 53 28 41 29 0a 23  _IDX_INPUTS(A).#
1f068 64 65 66 69 6e 65 20 54 52 41 43 45 5f 49 44 58  define TRACE_IDX
1f069 5f 4f 55 54 50 55 54 53 28 41 29 0a 23 65 6e 64  _OUTPUTS(A).#end
1f06a 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
1f06b 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
1f06c 41 42 4c 45 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75  ABLE./*.** Compu
1f06d 74 65 20 74 68 65 20 62 65 73 74 20 69 6e 64 65  te the best inde
1f06e 78 20 66 6f 72 20 61 20 76 69 72 74 75 61 6c 20  x for a virtual 
1f06f 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  table..**.** The
1f070 20 62 65 73 74 20 69 6e 64 65 78 20 69 73 20 63   best index is c
1f071 6f 6d 70 75 74 65 64 20 62 79 20 74 68 65 20 78  omputed by the x
1f072 42 65 73 74 49 6e 64 65 78 20 6d 65 74 68 6f 64  BestIndex method
1f073 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 0a   of the virtual.
1f074 2a 2a 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 2e  ** table module.
1f075 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69    This routine i
1f076 73 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 61 20  s really just a 
1f077 77 72 61 70 70 65 72 20 74 68 61 74 20 73 65 74  wrapper that set
1f078 73 20 75 70 0a 2a 2a 20 74 68 65 20 73 71 6c 69  s up.** the sqli
1f079 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73  te3_index_info s
1f07a 74 72 75 63 74 75 72 65 20 74 68 61 74 20 69 73  tructure that is
1f07b 20 75 73 65 64 20 74 6f 20 63 6f 6d 6d 75 6e 69   used to communi
1f07c 63 61 74 65 20 77 69 74 68 0a 2a 2a 20 78 42 65  cate with.** xBe
1f07d 73 74 49 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49  stIndex..**.** I
1f07e 6e 20 61 20 6a 6f 69 6e 2c 20 74 68 69 73 20 72  n a join, this r
1f07f 6f 75 74 69 6e 65 20 6d 69 67 68 74 20 62 65 20  outine might be 
1f080 63 61 6c 6c 65 64 20 6d 75 6c 74 69 70 6c 65 20  called multiple 
1f081 74 69 6d 65 73 20 66 6f 72 20 74 68 65 0a 2a 2a  times for the.**
1f082 20 73 61 6d 65 20 76 69 72 74 75 61 6c 20 74 61   same virtual ta
1f083 62 6c 65 2e 20 20 54 68 65 20 73 71 6c 69 74 65  ble.  The sqlite
1f084 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72  3_index_info str
1f085 75 63 74 75 72 65 20 69 73 20 63 72 65 61 74 65  ucture is create
1f086 64 0a 2a 2a 20 61 6e 64 20 69 6e 69 74 69 61 6c  d.** and initial
1f087 69 7a 65 64 20 6f 6e 20 74 68 65 20 66 69 72 73  ized on the firs
1f088 74 20 69 6e 76 6f 63 61 74 69 6f 6e 20 61 6e 64  t invocation and
1f089 20 72 65 75 73 65 64 20 6f 6e 20 61 6c 6c 20 73   reused on all s
1f08a 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 69 6e 76  ubsequent.** inv
1f08b 6f 63 61 74 69 6f 6e 73 2e 20 20 54 68 65 20 73  ocations.  The s
1f08c 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
1f08d 6f 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61  o structure is a
1f08e 6c 73 6f 20 75 73 65 64 20 77 68 65 6e 0a 2a 2a  lso used when.**
1f08f 20 63 6f 64 65 20 69 73 20 67 65 6e 65 72 61 74   code is generat
1f090 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 65  ed to access the
1f091 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20   virtual table. 
1f092 20 54 68 65 20 77 68 65 72 65 49 6e 66 6f 44 65   The whereInfoDe
1f093 6c 65 74 65 28 29 20 0a 2a 2a 20 72 6f 75 74 69  lete() .** routi
1f094 6e 65 20 74 61 6b 65 73 20 63 61 72 65 20 6f 66  ne takes care of
1f095 20 66 72 65 65 69 6e 67 20 74 68 65 20 73 71 6c   freeing the sql
1f096 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20  ite3_index_info 
1f097 73 74 72 75 63 74 75 72 65 20 61 66 74 65 72 0a  structure after.
1f098 2a 2a 20 65 76 65 72 79 62 6f 64 79 20 68 61 73  ** everybody has
1f099 20 66 69 6e 69 73 68 65 64 20 77 69 74 68 20 69   finished with i
1f09a 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 64 6f 75  t..*/.static dou
1f09b 62 6c 65 20 62 65 73 74 56 69 72 74 75 61 6c 49  ble bestVirtualI
1f09c 6e 64 65 78 28 0a 20 20 50 61 72 73 65 20 2a 70  ndex(.  Parse *p
1f09d 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
1f09e 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
1f09f 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
1f0a0 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a  .  WhereClause *
1f0a1 70 57 43 2c 20 20 20 20 20 20 20 20 20 20 20 20  pWC,            
1f0a2 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63    /* The WHERE c
1f0a3 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74 72 75 63  lause */.  struc
1f0a4 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
1f0a5 70 53 72 63 2c 20 20 20 20 20 2f 2a 20 54 68 65  pSrc,     /* The
1f0a6 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72   FROM clause ter
1f0a7 6d 20 74 6f 20 73 65 61 72 63 68 20 2a 2f 0a 20  m to search */. 
1f0a8 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64   Bitmask notRead
1f0a9 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y,              
1f0aa 2f 2a 20 4d 61 73 6b 20 6f 66 20 63 75 72 73 6f  /* Mask of curso
1f0ab 72 73 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20  rs that are not 
1f0ac 61 76 61 69 6c 61 62 6c 65 20 2a 2f 0a 20 20 45  available */.  E
1f0ad 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42  xprList *pOrderB
1f0ae 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  y,            /*
1f0af 20 54 68 65 20 6f 72 64 65 72 20 62 79 20 63 6c   The order by cl
1f0b0 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 72  ause */.  int or
1f0b1 64 65 72 42 79 55 73 61 62 6c 65 2c 20 20 20 20  derByUsable,    
1f0b2 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
1f0b3 20 69 66 20 77 65 20 63 61 6e 20 70 6f 74 65 6e   if we can poten
1f0b4 74 69 61 6c 20 73 6f 72 74 20 2a 2f 0a 20 20 73  tial sort */.  s
1f0b5 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
1f0b6 6f 20 2a 2a 70 70 49 64 78 49 6e 66 6f 20 2f 2a  o **ppIdxInfo /*
1f0b7 20 49 6e 64 65 78 20 69 6e 66 6f 72 6d 61 74 69   Index informati
1f0b8 6f 6e 20 70 61 73 73 65 64 20 74 6f 20 78 42 65  on passed to xBe
1f0b9 73 74 49 6e 64 65 78 20 2a 2f 0a 29 7b 0a 20 20  stIndex */.){.  
1f0ba 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 53  Table *pTab = pS
1f0bb 72 63 2d 3e 70 54 61 62 3b 0a 20 20 73 71 6c 69  rc->pTab;.  sqli
1f0bc 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a  te3_index_info *
1f0bd 70 49 64 78 49 6e 66 6f 3b 0a 20 20 73 74 72 75  pIdxInfo;.  stru
1f0be 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  ct sqlite3_index
1f0bf 5f 63 6f 6e 73 74 72 61 69 6e 74 20 2a 70 49 64  _constraint *pId
1f0c0 78 43 6f 6e 73 3b 0a 20 20 73 74 72 75 63 74 20  xCons;.  struct 
1f0c1 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 6f 72  sqlite3_index_or
1f0c2 64 65 72 62 79 20 2a 70 49 64 78 4f 72 64 65 72  derby *pIdxOrder
1f0c3 42 79 3b 0a 20 20 73 74 72 75 63 74 20 73 71 6c  By;.  struct sql
1f0c4 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74  ite3_index_const
1f0c5 72 61 69 6e 74 5f 75 73 61 67 65 20 2a 70 55 73  raint_usage *pUs
1f0c6 61 67 65 3b 0a 20 20 57 68 65 72 65 54 65 72 6d  age;.  WhereTerm
1f0c7 20 2a 70 54 65 72 6d 3b 0a 20 20 69 6e 74 20 69   *pTerm;.  int i
1f0c8 2c 20 6a 3b 0a 20 20 69 6e 74 20 6e 4f 72 64 65  , j;.  int nOrde
1f0c9 72 42 79 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  rBy;.  int rc;..
1f0ca 20 20 2f 2a 20 49 66 20 74 68 65 20 73 71 6c 69    /* If the sqli
1f0cb 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73  te3_index_info s
1f0cc 74 72 75 63 74 75 72 65 20 68 61 73 20 6e 6f 74  tructure has not
1f0cd 20 62 65 65 6e 20 70 72 65 76 69 6f 75 73 6c 79   been previously
1f0ce 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20  .  ** allocated 
1f0cf 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 64 20  and initialized 
1f0d0 66 6f 72 20 74 68 69 73 20 76 69 72 74 75 61 6c  for this virtual
1f0d1 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 61 6c 6c   table, then all
1f0d2 6f 63 61 74 65 0a 20 20 2a 2a 20 61 6e 64 20 69  ocate.  ** and i
1f0d3 6e 69 74 69 61 6c 69 7a 65 20 69 74 20 6e 6f 77  nitialize it now
1f0d4 0a 20 20 2a 2f 0a 20 20 70 49 64 78 49 6e 66 6f  .  */.  pIdxInfo
1f0d5 20 3d 20 2a 70 70 49 64 78 49 6e 66 6f 3b 0a 20   = *ppIdxInfo;. 
1f0d6 20 69 66 28 20 70 49 64 78 49 6e 66 6f 3d 3d 30   if( pIdxInfo==0
1f0d7 20 29 7b 0a 20 20 20 20 57 68 65 72 65 54 65 72   ){.    WhereTer
1f0d8 6d 20 2a 70 54 65 72 6d 3b 0a 20 20 20 20 69 6e  m *pTerm;.    in
1f0d9 74 20 6e 54 65 72 6d 3b 0a 20 20 20 20 57 48 45  t nTerm;.    WHE
1f0da 52 45 54 52 41 43 45 28 28 22 52 65 63 6f 6d 70  RETRACE(("Recomp
1f0db 75 74 69 6e 67 20 69 6e 64 65 78 20 69 6e 66 6f  uting index info
1f0dc 20 66 6f 72 20 25 73 2e 2e 2e 5c 6e 22 2c 20 70   for %s...\n", p
1f0dd 54 61 62 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 0a 20  Tab->zName));.. 
1f0de 20 20 20 2f 2a 20 43 6f 75 6e 74 20 74 68 65 20     /* Count the 
1f0df 6e 75 6d 62 65 72 20 6f 66 20 70 6f 73 73 69 62  number of possib
1f0e0 6c 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  le WHERE clause 
1f0e1 63 6f 6e 73 74 72 61 69 6e 74 73 20 72 65 66 65  constraints refe
1f0e2 72 72 69 6e 67 0a 20 20 20 20 2a 2a 20 74 6f 20  rring.    ** to 
1f0e3 74 68 69 73 20 76 69 72 74 75 61 6c 20 74 61 62  this virtual tab
1f0e4 6c 65 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d  le */.    for(i=
1f0e5 6e 54 65 72 6d 3d 30 2c 20 70 54 65 72 6d 3d 70  nTerm=0, pTerm=p
1f0e6 57 43 2d 3e 61 3b 20 69 3c 70 57 43 2d 3e 6e 54  WC->a; i<pWC->nT
1f0e7 65 72 6d 3b 20 69 2b 2b 2c 20 70 54 65 72 6d 2b  erm; i++, pTerm+
1f0e8 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 54  +){.      if( pT
1f0e9 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20  erm->leftCursor 
1f0ea 21 3d 20 70 53 72 63 2d 3e 69 43 75 72 73 6f 72  != pSrc->iCursor
1f0eb 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
1f0ec 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f     if( pTerm->eO
1f0ed 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 49 4e 20 29  perator==WO_IN )
1f0ee 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
1f0ef 20 6e 54 65 72 6d 2b 2b 3b 0a 20 20 20 20 7d 0a   nTerm++;.    }.
1f0f0 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 4f  .    /* If the O
1f0f1 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 63  RDER BY clause c
1f0f2 6f 6e 74 61 69 6e 73 20 6f 6e 6c 79 20 63 6f 6c  ontains only col
1f0f3 75 6d 6e 73 20 69 6e 20 74 68 65 20 63 75 72 72  umns in the curr
1f0f4 65 6e 74 20 0a 20 20 20 20 2a 2a 20 76 69 72 74  ent .    ** virt
1f0f5 75 61 6c 20 74 61 62 6c 65 20 74 68 65 6e 20 61  ual table then a
1f0f6 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f  llocate space fo
1f0f7 72 20 74 68 65 20 61 4f 72 64 65 72 42 79 20 70  r the aOrderBy p
1f0f8 61 72 74 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68  art of.    ** th
1f0f9 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  e sqlite3_index_
1f0fa 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e 0a  info structure..
1f0fb 20 20 20 20 2a 2f 0a 20 20 20 20 6e 4f 72 64 65      */.    nOrde
1f0fc 72 42 79 20 3d 20 30 3b 0a 20 20 20 20 69 66 28  rBy = 0;.    if(
1f0fd 20 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20   pOrderBy ){.   
1f0fe 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f     for(i=0; i<pO
1f0ff 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69  rderBy->nExpr; i
1f100 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 45 78 70  ++){.        Exp
1f101 72 20 2a 70 45 78 70 72 20 3d 20 70 4f 72 64 65  r *pExpr = pOrde
1f102 72 42 79 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b  rBy->a[i].pExpr;
1f103 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 45 78  .        if( pEx
1f104 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d  pr->op!=TK_COLUM
1f105 4e 20 7c 7c 20 70 45 78 70 72 2d 3e 69 54 61 62  N || pExpr->iTab
1f106 6c 65 21 3d 70 53 72 63 2d 3e 69 43 75 72 73 6f  le!=pSrc->iCurso
1f107 72 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  r ) break;.     
1f108 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d   }.      if( i==
1f109 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 20  pOrderBy->nExpr 
1f10a 29 7b 0a 20 20 20 20 20 20 20 20 6e 4f 72 64 65  ){.        nOrde
1f10b 72 42 79 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e  rBy = pOrderBy->
1f10c 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 7d 0a 20  nExpr;.      }. 
1f10d 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c     }..    /* All
1f10e 6f 63 61 74 65 20 74 68 65 20 73 71 6c 69 74 65  ocate the sqlite
1f10f 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72  3_index_info str
1f110 75 63 74 75 72 65 0a 20 20 20 20 2a 2f 0a 20 20  ucture.    */.  
1f111 20 20 70 49 64 78 49 6e 66 6f 20 3d 20 73 71 6c    pIdxInfo = sql
1f112 69 74 65 4d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f  iteMalloc( sizeo
1f113 66 28 2a 70 49 64 78 49 6e 66 6f 29 0a 20 20 20  f(*pIdxInfo).   
1f114 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f115 20 20 20 20 20 20 20 20 20 20 2b 20 28 73 69 7a            + (siz
1f116 65 6f 66 28 2a 70 49 64 78 43 6f 6e 73 29 20 2b  eof(*pIdxCons) +
1f117 20 73 69 7a 65 6f 66 28 2a 70 55 73 61 67 65 29   sizeof(*pUsage)
1f118 29 2a 6e 54 65 72 6d 0a 20 20 20 20 20 20 20 20  )*nTerm.        
1f119 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f11a 20 20 20 20 20 2b 20 73 69 7a 65 6f 66 28 2a 70       + sizeof(*p
1f11b 49 64 78 4f 72 64 65 72 42 79 29 2a 6e 4f 72 64  IdxOrderBy)*nOrd
1f11c 65 72 42 79 20 29 3b 0a 20 20 20 20 69 66 28 20  erBy );.    if( 
1f11d 70 49 64 78 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20  pIdxInfo==0 ){. 
1f11e 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
1f11f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6f 75  rMsg(pParse, "ou
1f120 74 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20  t of memory");. 
1f121 20 20 20 20 20 72 65 74 75 72 6e 20 30 2e 30 3b       return 0.0;
1f122 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 70 49 64  .    }.    *ppId
1f123 78 49 6e 66 6f 20 3d 20 70 49 64 78 49 6e 66 6f  xInfo = pIdxInfo
1f124 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 69 74 69 61  ;..    /* Initia
1f125 6c 69 7a 65 20 74 68 65 20 73 74 72 75 63 74 75  lize the structu
1f126 72 65 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33  re.  The sqlite3
1f127 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75  _index_info stru
1f128 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 0a 20  cture contains. 
1f129 20 20 20 2a 2a 20 6d 61 6e 79 20 66 69 65 6c 64     ** many field
1f12a 73 20 74 68 61 74 20 61 72 65 20 64 65 63 6c 61  s that are decla
1f12b 72 65 64 20 22 63 6f 6e 73 74 22 20 74 6f 20 70  red "const" to p
1f12c 72 65 76 65 6e 74 20 78 42 65 73 74 49 6e 64 65  revent xBestInde
1f12d 78 20 66 72 6f 6d 0a 20 20 20 20 2a 2a 20 63 68  x from.    ** ch
1f12e 61 6e 67 69 6e 67 20 74 68 65 6d 2e 20 20 57 65  anging them.  We
1f12f 20 68 61 76 65 20 74 6f 20 64 6f 20 73 6f 6d 65   have to do some
1f130 20 66 75 6e 6b 79 20 63 61 73 74 69 6e 67 20 69   funky casting i
1f131 6e 20 6f 72 64 65 72 20 74 6f 0a 20 20 20 20 2a  n order to.    *
1f132 2a 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 6f  * initialize tho
1f133 73 65 20 66 69 65 6c 64 73 2e 0a 20 20 20 20 2a  se fields..    *
1f134 2f 0a 20 20 20 20 70 49 64 78 43 6f 6e 73 20 3d  /.    pIdxCons =
1f135 20 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 33   (struct sqlite3
1f136 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e  _index_constrain
1f137 74 2a 29 26 70 49 64 78 49 6e 66 6f 5b 31 5d 3b  t*)&pIdxInfo[1];
1f138 0a 20 20 20 20 70 49 64 78 4f 72 64 65 72 42 79  .    pIdxOrderBy
1f139 20 3d 20 28 73 74 72 75 63 74 20 73 71 6c 69 74   = (struct sqlit
1f13a 65 33 5f 69 6e 64 65 78 5f 6f 72 64 65 72 62 79  e3_index_orderby
1f13b 2a 29 26 70 49 64 78 43 6f 6e 73 5b 6e 54 65 72  *)&pIdxCons[nTer
1f13c 6d 5d 3b 0a 20 20 20 20 70 55 73 61 67 65 20 3d  m];.    pUsage =
1f13d 20 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 33   (struct sqlite3
1f13e 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e  _index_constrain
1f13f 74 5f 75 73 61 67 65 2a 29 26 70 49 64 78 4f 72  t_usage*)&pIdxOr
1f140 64 65 72 42 79 5b 6e 4f 72 64 65 72 42 79 5d 3b  derBy[nOrderBy];
1f141 0a 20 20 20 20 2a 28 69 6e 74 2a 29 26 70 49 64  .    *(int*)&pId
1f142 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69  xInfo->nConstrai
1f143 6e 74 20 3d 20 6e 54 65 72 6d 3b 0a 20 20 20 20  nt = nTerm;.    
1f144 2a 28 69 6e 74 2a 29 26 70 49 64 78 49 6e 66 6f  *(int*)&pIdxInfo
1f145 2d 3e 6e 4f 72 64 65 72 42 79 20 3d 20 6e 4f 72  ->nOrderBy = nOr
1f146 64 65 72 42 79 3b 0a 20 20 20 20 2a 28 73 74 72  derBy;.    *(str
1f147 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
1f148 78 5f 63 6f 6e 73 74 72 61 69 6e 74 2a 2a 29 26  x_constraint**)&
1f149 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74  pIdxInfo->aConst
1f14a 72 61 69 6e 74 20 3d 20 70 49 64 78 43 6f 6e 73  raint = pIdxCons
1f14b 3b 0a 20 20 20 20 2a 28 73 74 72 75 63 74 20 73  ;.    *(struct s
1f14c 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 6f 72 64  qlite3_index_ord
1f14d 65 72 62 79 2a 2a 29 26 70 49 64 78 49 6e 66 6f  erby**)&pIdxInfo
1f14e 2d 3e 61 4f 72 64 65 72 42 79 20 3d 20 70 49 64  ->aOrderBy = pId
1f14f 78 4f 72 64 65 72 42 79 3b 0a 20 20 20 20 2a 28  xOrderBy;.    *(
1f150 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69  struct sqlite3_i
1f151 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f  ndex_constraint_
1f152 75 73 61 67 65 2a 2a 29 26 70 49 64 78 49 6e 66  usage**)&pIdxInf
1f153 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73  o->aConstraintUs
1f154 61 67 65 20 3d 0a 20 20 20 20 20 20 20 20 20 20  age =.          
1f155 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f156 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f157 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f158 20 20 20 20 20 20 20 20 20 20 20 70 55 73 61 67             pUsag
1f159 65 3b 0a 0a 20 20 20 20 66 6f 72 28 69 3d 6a 3d  e;..    for(i=j=
1f15a 30 2c 20 70 54 65 72 6d 3d 70 57 43 2d 3e 61 3b  0, pTerm=pWC->a;
1f15b 20 69 3c 70 57 43 2d 3e 6e 54 65 72 6d 3b 20 69   i<pWC->nTerm; i
1f15c 2b 2b 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20  ++, pTerm++){.  
1f15d 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 6c      if( pTerm->l
1f15e 65 66 74 43 75 72 73 6f 72 20 21 3d 20 70 53 72  eftCursor != pSr
1f15f 63 2d 3e 69 43 75 72 73 6f 72 20 29 20 63 6f 6e  c->iCursor ) con
1f160 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 69 66 28  tinue;.      if(
1f161 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f   pTerm->eOperato
1f162 72 3d 3d 57 4f 5f 49 4e 20 29 20 63 6f 6e 74 69  r==WO_IN ) conti
1f163 6e 75 65 3b 0a 20 20 20 20 20 20 70 49 64 78 43  nue;.      pIdxC
1f164 6f 6e 73 5b 6a 5d 2e 69 43 6f 6c 75 6d 6e 20 3d  ons[j].iColumn =
1f165 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43 6f 6c 75   pTerm->leftColu
1f166 6d 6e 3b 0a 20 20 20 20 20 20 70 49 64 78 43 6f  mn;.      pIdxCo
1f167 6e 73 5b 6a 5d 2e 69 54 65 72 6d 4f 66 66 73 65  ns[j].iTermOffse
1f168 74 20 3d 20 69 3b 0a 20 20 20 20 20 20 70 49 64  t = i;.      pId
1f169 78 43 6f 6e 73 5b 6a 5d 2e 6f 70 20 3d 20 70 54  xCons[j].op = pT
1f16a 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3b 0a  erm->eOperator;.
1f16b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 69 72        /* The dir
1f16c 65 63 74 20 61 73 73 69 67 6e 6d 65 6e 74 20 69  ect assignment i
1f16d 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 6c  n the previous l
1f16e 69 6e 65 20 69 73 20 70 6f 73 73 69 62 6c 65 20  ine is possible 
1f16f 6f 6e 6c 79 20 62 65 63 61 75 73 65 0a 20 20 20  only because.   
1f170 20 20 20 2a 2a 20 74 68 65 20 57 4f 5f 20 61 6e     ** the WO_ an
1f171 64 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43  d SQLITE_INDEX_C
1f172 4f 4e 53 54 52 41 49 4e 54 5f 20 63 6f 64 65 73  ONSTRAINT_ codes
1f173 20 61 72 65 20 69 64 65 6e 74 69 63 61 6c 2e 20   are identical. 
1f174 20 54 68 65 0a 20 20 20 20 20 20 2a 2a 20 66 6f   The.      ** fo
1f175 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 73 20  llowing asserts 
1f176 76 65 72 69 66 79 20 74 68 69 73 20 66 61 63 74  verify this fact
1f177 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72  . */.      asser
1f178 74 28 20 57 4f 5f 45 51 3d 3d 53 51 4c 49 54 45  t( WO_EQ==SQLITE
1f179 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e  _INDEX_CONSTRAIN
1f17a 54 5f 45 51 20 29 3b 0a 20 20 20 20 20 20 61 73  T_EQ );.      as
1f17b 73 65 72 74 28 20 57 4f 5f 4c 54 3d 3d 53 51 4c  sert( WO_LT==SQL
1f17c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52  ITE_INDEX_CONSTR
1f17d 41 49 4e 54 5f 4c 54 20 29 3b 0a 20 20 20 20 20  AINT_LT );.     
1f17e 20 61 73 73 65 72 74 28 20 57 4f 5f 4c 45 3d 3d   assert( WO_LE==
1f17f 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e  SQLITE_INDEX_CON
1f180 53 54 52 41 49 4e 54 5f 4c 45 20 29 3b 0a 20 20  STRAINT_LE );.  
1f181 20 20 20 20 61 73 73 65 72 74 28 20 57 4f 5f 47      assert( WO_G
1f182 54 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f  T==SQLITE_INDEX_
1f183 43 4f 4e 53 54 52 41 49 4e 54 5f 47 54 20 29 3b  CONSTRAINT_GT );
1f184 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 57  .      assert( W
1f185 4f 5f 47 45 3d 3d 53 51 4c 49 54 45 5f 49 4e 44  O_GE==SQLITE_IND
1f186 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 45  EX_CONSTRAINT_GE
1f187 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
1f188 28 20 57 4f 5f 4d 41 54 43 48 3d 3d 53 51 4c 49  ( WO_MATCH==SQLI
1f189 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41  TE_INDEX_CONSTRA
1f18a 49 4e 54 5f 4d 41 54 43 48 20 29 3b 0a 20 20 20  INT_MATCH );.   
1f18b 20 20 20 61 73 73 65 72 74 28 20 70 54 65 72 6d     assert( pTerm
1f18c 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 28 57  ->eOperator & (W
1f18d 4f 5f 45 51 7c 57 4f 5f 4c 54 7c 57 4f 5f 4c 45  O_EQ|WO_LT|WO_LE
1f18e 7c 57 4f 5f 47 54 7c 57 4f 5f 47 45 7c 57 4f 5f  |WO_GT|WO_GE|WO_
1f18f 4d 41 54 43 48 29 20 29 3b 0a 20 20 20 20 20 20  MATCH) );.      
1f190 6a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66  j++;.    }.    f
1f191 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 72 64 65 72  or(i=0; i<nOrder
1f192 42 79 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  By; i++){.      
1f193 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20 70 4f  Expr *pExpr = pO
1f194 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 70 45 78  rderBy->a[i].pEx
1f195 70 72 3b 0a 20 20 20 20 20 20 70 49 64 78 4f 72  pr;.      pIdxOr
1f196 64 65 72 42 79 5b 69 5d 2e 69 43 6f 6c 75 6d 6e  derBy[i].iColumn
1f197 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d   = pExpr->iColum
1f198 6e 3b 0a 20 20 20 20 20 20 70 49 64 78 4f 72 64  n;.      pIdxOrd
1f199 65 72 42 79 5b 69 5d 2e 64 65 73 63 20 3d 20 70  erBy[i].desc = p
1f19a 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 73 6f  OrderBy->a[i].so
1f19b 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 7d 0a 20  rtOrder;.    }. 
1f19c 20 7d 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73   }..  /* At this
1f19d 20 70 6f 69 6e 74 2c 20 74 68 65 20 73 71 6c 69   point, the sqli
1f19e 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73  te3_index_info s
1f19f 74 72 75 63 74 75 72 65 20 74 68 61 74 20 70 49  tructure that pI
1f1a0 64 78 49 6e 66 6f 20 70 6f 69 6e 74 73 0a 20 20  dxInfo points.  
1f1a1 2a 2a 20 74 6f 20 77 69 6c 6c 20 68 61 76 65 20  ** to will have 
1f1a2 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64  been initialized
1f1a3 2c 20 65 69 74 68 65 72 20 64 75 72 69 6e 67 20  , either during 
1f1a4 74 68 65 20 63 75 72 72 65 6e 74 20 69 6e 76 6f  the current invo
1f1a5 63 61 74 69 6f 6e 20 6f 72 0a 20 20 2a 2a 20 64  cation or.  ** d
1f1a6 75 72 69 6e 67 20 73 6f 6d 65 20 70 72 69 6f 72  uring some prior
1f1a7 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 20 20 4e 6f   invocation.  No
1f1a8 77 20 77 65 20 6a 75 73 74 20 68 61 76 65 20 74  w we just have t
1f1a9 6f 20 63 75 73 74 6f 6d 69 7a 65 20 74 68 65 0a  o customize the.
1f1aa 20 20 2a 2a 20 64 65 74 61 69 6c 73 20 6f 66 20    ** details of 
1f1ab 70 49 64 78 49 6e 66 6f 20 66 6f 72 20 74 68 65  pIdxInfo for the
1f1ac 20 63 75 72 72 65 6e 74 20 69 6e 76 6f 63 61 74   current invocat
1f1ad 69 6f 6e 20 61 6e 64 20 70 61 73 73 20 69 74 20  ion and pass it 
1f1ae 74 6f 0a 20 20 2a 2a 20 78 42 65 73 74 49 6e 64  to.  ** xBestInd
1f1af 65 78 2e 0a 20 20 2a 2f 0a 0a 20 20 2f 2a 20 54  ex..  */..  /* T
1f1b0 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 6d  he module name m
1f1b1 75 73 74 20 62 65 20 64 65 66 69 6e 65 64 2e 20  ust be defined. 
1f1b2 41 6c 73 6f 2c 20 62 79 20 74 68 69 73 20 70 6f  Also, by this po
1f1b3 69 6e 74 20 74 68 65 72 65 20 6d 75 73 74 0a 20  int there must. 
1f1b4 20 2a 2a 20 62 65 20 61 20 70 6f 69 6e 74 65 72   ** be a pointer
1f1b5 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f 76   to an sqlite3_v
1f1b6 74 61 62 20 73 74 72 75 63 74 75 72 65 2e 20 4f  tab structure. O
1f1b7 74 68 65 72 77 69 73 65 0a 20 20 2a 2a 20 73 71  therwise.  ** sq
1f1b8 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75  lite3ViewGetColu
1f1b9 6d 6e 4e 61 6d 65 73 28 29 20 77 6f 75 6c 64 20  mnNames() would 
1f1ba 68 61 76 65 20 70 69 63 6b 65 64 20 75 70 20 74  have picked up t
1f1bb 68 65 20 65 72 72 6f 72 2e 20 0a 20 20 2a 2f 0a  he error. .  */.
1f1bc 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e    assert( pTab->
1f1bd 61 7a 4d 6f 64 75 6c 65 41 72 67 20 26 26 20 70  azModuleArg && p
1f1be 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67  Tab->azModuleArg
1f1bf 5b 30 5d 20 29 3b 0a 20 20 61 73 73 65 72 74 28  [0] );.  assert(
1f1c0 20 70 54 61 62 2d 3e 70 56 74 61 62 20 29 3b 0a   pTab->pVtab );.
1f1c1 23 69 66 20 30 0a 20 20 69 66 28 20 70 54 61 62  #if 0.  if( pTab
1f1c2 2d 3e 70 56 74 61 62 3d 3d 30 20 29 7b 0a 20 20  ->pVtab==0 ){.  
1f1c3 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
1f1c4 67 28 70 50 61 72 73 65 2c 20 22 75 6e 64 65 66  g(pParse, "undef
1f1c5 69 6e 65 64 20 6d 6f 64 75 6c 65 20 25 73 20 66  ined module %s f
1f1c6 6f 72 20 74 61 62 6c 65 20 25 73 22 2c 0a 20 20  or table %s",.  
1f1c7 20 20 20 20 20 20 70 54 61 62 2d 3e 61 7a 4d 6f        pTab->azMo
1f1c8 64 75 6c 65 41 72 67 5b 30 5d 2c 20 70 54 61 62  duleArg[0], pTab
1f1c9 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65  ->zName);.    re
1f1ca 74 75 72 6e 20 30 2e 30 3b 0a 20 20 7d 0a 23 65  turn 0.0;.  }.#e
1f1cb 6e 64 69 66 0a 0a 20 20 2f 2a 20 53 65 74 20 74  ndif..  /* Set t
1f1cc 68 65 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d  he aConstraint[]
1f1cd 2e 75 73 61 62 6c 65 20 66 69 65 6c 64 73 20 61  .usable fields a
1f1ce 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 61 6c  nd initialize al
1f1cf 6c 20 0a 20 20 2a 2a 20 6f 75 74 70 75 74 20 76  l .  ** output v
1f1d0 61 72 69 61 62 6c 65 73 20 74 6f 20 7a 65 72 6f  ariables to zero
1f1d1 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 61 43 6f 6e  ..  **.  ** aCon
1f1d2 73 74 72 61 69 6e 74 5b 5d 2e 75 73 61 62 6c 65  straint[].usable
1f1d3 20 69 73 20 74 72 75 65 20 66 6f 72 20 63 6f 6e   is true for con
1f1d4 73 74 72 61 69 6e 74 73 20 77 68 65 72 65 20 74  straints where t
1f1d5 68 65 20 72 69 67 68 74 2d 68 61 6e 64 0a 20 20  he right-hand.  
1f1d6 2a 2a 20 73 69 64 65 20 63 6f 6e 74 61 69 6e 73  ** side contains
1f1d7 20 6f 6e 6c 79 20 72 65 66 65 72 65 6e 63 65 73   only references
1f1d8 20 74 6f 20 74 61 62 6c 65 73 20 74 6f 20 74 68   to tables to th
1f1d9 65 20 6c 65 66 74 20 6f 66 20 74 68 65 20 63 75  e left of the cu
1f1da 72 72 65 6e 74 0a 20 20 2a 2a 20 74 61 62 6c 65  rrent.  ** table
1f1db 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  .  In other word
1f1dc 73 2c 20 69 66 20 74 68 65 20 63 6f 6e 73 74 72  s, if the constr
1f1dd 61 69 6e 74 20 69 73 20 6f 66 20 74 68 65 20 66  aint is of the f
1f1de 6f 72 6d 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20  orm:.  **.  **  
1f1df 20 20 20 20 20 20 20 20 20 63 6f 6c 75 6d 6e 20           column 
1f1e0 3d 20 65 78 70 72 0a 20 20 2a 2a 0a 20 20 2a 2a  = expr.  **.  **
1f1e1 20 61 6e 64 20 77 65 20 61 72 65 20 65 76 61 6c   and we are eval
1f1e2 75 61 74 69 6e 67 20 61 20 6a 6f 69 6e 2c 20 74  uating a join, t
1f1e3 68 65 6e 20 74 68 65 20 63 6f 6e 73 74 72 61 69  hen the constrai
1f1e4 6e 74 20 6f 6e 20 63 6f 6c 75 6d 6e 20 69 73 20  nt on column is 
1f1e5 0a 20 20 2a 2a 20 6f 6e 6c 79 20 76 61 6c 69 64  .  ** only valid
1f1e6 20 69 66 20 61 6c 6c 20 74 61 62 6c 65 73 20 72   if all tables r
1f1e7 65 66 65 72 65 6e 63 65 64 20 69 6e 20 65 78 70  eferenced in exp
1f1e8 72 20 6f 63 63 75 72 20 74 6f 20 74 68 65 20 6c  r occur to the l
1f1e9 65 66 74 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20  eft.  ** of the 
1f1ea 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67  table containing
1f1eb 20 63 6f 6c 75 6d 6e 2e 0a 20 20 2a 2a 0a 20 20   column..  **.  
1f1ec 2a 2a 20 54 68 65 20 61 43 6f 6e 73 74 72 61 69  ** The aConstrai
1f1ed 6e 74 73 5b 5d 20 61 72 72 61 79 20 63 6f 6e 74  nts[] array cont
1f1ee 61 69 6e 73 20 65 6e 74 72 69 65 73 20 66 6f 72  ains entries for
1f1ef 20 61 6c 6c 20 63 6f 6e 73 74 72 61 69 6e 74 73   all constraints
1f1f0 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 63 75 72  .  ** on the cur
1f1f1 72 65 6e 74 20 74 61 62 6c 65 2e 20 20 54 68 61  rent table.  Tha
1f1f2 74 20 77 61 79 20 77 65 20 6f 6e 6c 79 20 68 61  t way we only ha
1f1f3 76 65 20 74 6f 20 63 6f 6d 70 75 74 65 20 69 74  ve to compute it
1f1f4 20 6f 6e 63 65 0a 20 20 2a 2a 20 65 76 65 6e 20   once.  ** even 
1f1f5 74 68 6f 75 67 68 20 77 65 20 6d 69 67 68 74 20  though we might 
1f1f6 74 72 79 20 74 6f 20 70 69 63 6b 20 74 68 65 20  try to pick the 
1f1f7 62 65 73 74 20 69 6e 64 65 78 20 6d 75 6c 74 69  best index multi
1f1f8 70 6c 65 20 74 69 6d 65 73 2e 0a 20 20 2a 2a 20  ple times..  ** 
1f1f9 46 6f 72 20 65 61 63 68 20 61 74 74 65 6d 70 74  For each attempt
1f1fa 20 61 74 20 70 69 63 6b 69 6e 67 20 61 6e 20 69   at picking an i
1f1fb 6e 64 65 78 2c 20 74 68 65 20 6f 72 64 65 72 20  ndex, the order 
1f1fc 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65  of tables in the
1f1fd 0a 20 20 2a 2a 20 6a 6f 69 6e 20 6d 69 67 68 74  .  ** join might
1f1fe 20 62 65 20 64 69 66 66 65 72 65 6e 74 20 73 6f   be different so
1f1ff 20 77 65 20 68 61 76 65 20 74 6f 20 72 65 63 6f   we have to reco
1f200 6d 70 75 74 65 20 74 68 65 20 75 73 61 62 6c 65  mpute the usable
1f201 20 66 6c 61 67 0a 20 20 2a 2a 20 65 61 63 68 20   flag.  ** each 
1f202 74 69 6d 65 2e 0a 20 20 2a 2f 0a 20 20 70 49 64  time..  */.  pId
1f203 78 43 6f 6e 73 20 3d 20 2a 28 73 74 72 75 63 74  xCons = *(struct
1f204 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63   sqlite3_index_c
1f205 6f 6e 73 74 72 61 69 6e 74 2a 2a 29 26 70 49 64  onstraint**)&pId
1f206 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69  xInfo->aConstrai
1f207 6e 74 3b 0a 20 20 70 55 73 61 67 65 20 3d 20 70  nt;.  pUsage = p
1f208 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72  IdxInfo->aConstr
1f209 61 69 6e 74 55 73 61 67 65 3b 0a 20 20 66 6f 72  aintUsage;.  for
1f20a 28 69 3d 30 3b 20 69 3c 70 49 64 78 49 6e 66 6f  (i=0; i<pIdxInfo
1f20b 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69  ->nConstraint; i
1f20c 2b 2b 2c 20 70 49 64 78 43 6f 6e 73 2b 2b 29 7b  ++, pIdxCons++){
1f20d 0a 20 20 20 20 6a 20 3d 20 70 49 64 78 43 6f 6e  .    j = pIdxCon
1f20e 73 2d 3e 69 54 65 72 6d 4f 66 66 73 65 74 3b 0a  s->iTermOffset;.
1f20f 20 20 20 20 70 54 65 72 6d 20 3d 20 26 70 57 43      pTerm = &pWC
1f210 2d 3e 61 5b 6a 5d 3b 0a 20 20 20 20 70 49 64 78  ->a[j];.    pIdx
1f211 43 6f 6e 73 2d 3e 75 73 61 62 6c 65 20 3d 20 20  Cons->usable =  
1f212 28 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69  (pTerm->prereqRi
1f213 67 68 74 20 26 20 6e 6f 74 52 65 61 64 79 29 3d  ght & notReady)=
1f214 3d 30 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74  =0;.  }.  memset
1f215 28 70 55 73 61 67 65 2c 20 30 2c 20 73 69 7a 65  (pUsage, 0, size
1f216 6f 66 28 70 55 73 61 67 65 5b 30 5d 29 2a 70 49  of(pUsage[0])*pI
1f217 64 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61  dxInfo->nConstra
1f218 69 6e 74 29 3b 0a 20 20 69 66 28 20 70 49 64 78  int);.  if( pIdx
1f219 49 6e 66 6f 2d 3e 6e 65 65 64 54 6f 46 72 65 65  Info->needToFree
1f21a 49 64 78 53 74 72 20 29 7b 0a 20 20 20 20 73 71  IdxStr ){.    sq
1f21b 6c 69 74 65 33 5f 66 72 65 65 28 70 49 64 78 49  lite3_free(pIdxI
1f21c 6e 66 6f 2d 3e 69 64 78 53 74 72 29 3b 0a 20 20  nfo->idxStr);.  
1f21d 7d 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 69 64  }.  pIdxInfo->id
1f21e 78 53 74 72 20 3d 20 30 3b 0a 20 20 70 49 64 78  xStr = 0;.  pIdx
1f21f 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20 30  Info->idxNum = 0
1f220 3b 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 6e 65  ;.  pIdxInfo->ne
1f221 65 64 54 6f 46 72 65 65 49 64 78 53 74 72 20 3d  edToFreeIdxStr =
1f222 20 30 3b 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e   0;.  pIdxInfo->
1f223 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d 65 64 20  orderByConsumed 
1f224 3d 20 30 3b 0a 20 20 70 49 64 78 49 6e 66 6f 2d  = 0;.  pIdxInfo-
1f225 3e 65 73 74 69 6d 61 74 65 64 43 6f 73 74 20 3d  >estimatedCost =
1f226 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 20   SQLITE_BIG_DBL 
1f227 2f 20 32 2e 30 3b 0a 20 20 6e 4f 72 64 65 72 42  / 2.0;.  nOrderB
1f228 79 20 3d 20 70 49 64 78 49 6e 66 6f 2d 3e 6e 4f  y = pIdxInfo->nO
1f229 72 64 65 72 42 79 3b 0a 20 20 69 66 28 20 70 49  rderBy;.  if( pI
1f22a 64 78 49 6e 66 6f 2d 3e 6e 4f 72 64 65 72 42 79  dxInfo->nOrderBy
1f22b 20 26 26 20 21 6f 72 64 65 72 42 79 55 73 61 62   && !orderByUsab
1f22c 6c 65 20 29 7b 0a 20 20 20 20 2a 28 69 6e 74 2a  le ){.    *(int*
1f22d 29 26 70 49 64 78 49 6e 66 6f 2d 3e 6e 4f 72 64  )&pIdxInfo->nOrd
1f22e 65 72 42 79 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20  erBy = 0;.  }.. 
1f22f 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
1f230 66 28 70 50 61 72 73 65 2d 3e 64 62 29 3b 0a 20  f(pParse->db);. 
1f231 20 57 48 45 52 45 54 52 41 43 45 28 28 22 78 42   WHERETRACE(("xB
1f232 65 73 74 49 6e 64 65 78 20 66 6f 72 20 25 73 5c  estIndex for %s\
1f233 6e 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  n", pTab->zName)
1f234 29 3b 0a 20 20 54 52 41 43 45 5f 49 44 58 5f 49  );.  TRACE_IDX_I
1f235 4e 50 55 54 53 28 70 49 64 78 49 6e 66 6f 29 3b  NPUTS(pIdxInfo);
1f236 0a 20 20 72 63 20 3d 20 70 54 61 62 2d 3e 70 56  .  rc = pTab->pV
1f237 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 42  tab->pModule->xB
1f238 65 73 74 49 6e 64 65 78 28 70 54 61 62 2d 3e 70  estIndex(pTab->p
1f239 56 74 61 62 2c 20 70 49 64 78 49 6e 66 6f 29 3b  Vtab, pIdxInfo);
1f23a 0a 20 20 54 52 41 43 45 5f 49 44 58 5f 4f 55 54  .  TRACE_IDX_OUT
1f23b 50 55 54 53 28 70 49 64 78 49 6e 66 6f 29 3b 0a  PUTS(pIdxInfo);.
1f23c 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1f23d 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 72  _OK ){.    if( r
1f23e 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
1f23f 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1f240 46 61 69 6c 65 64 4d 61 6c 6c 6f 63 28 29 3b 0a  FailedMalloc();.
1f241 20 20 20 20 7d 65 6c 73 65 20 7b 0a 20 20 20 20      }else {.    
1f242 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
1f243 67 28 70 50 61 72 73 65 2c 20 22 25 73 22 2c 20  g(pParse, "%s", 
1f244 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 72 63  sqlite3ErrStr(rc
1f245 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  ));.    }.    sq
1f246 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 70 50  lite3SafetyOn(pP
1f247 61 72 73 65 2d 3e 64 62 29 3b 0a 20 20 7d 65 6c  arse->db);.  }el
1f248 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  se{.    rc = sql
1f249 69 74 65 33 53 61 66 65 74 79 4f 6e 28 70 50 61  ite3SafetyOn(pPa
1f24a 72 73 65 2d 3e 64 62 29 3b 0a 20 20 7d 0a 20 20  rse->db);.  }.  
1f24b 2a 28 69 6e 74 2a 29 26 70 49 64 78 49 6e 66 6f  *(int*)&pIdxInfo
1f24c 2d 3e 6e 4f 72 64 65 72 42 79 20 3d 20 6e 4f 72  ->nOrderBy = nOr
1f24d 64 65 72 42 79 3b 0a 0a 20 20 72 65 74 75 72 6e  derBy;..  return
1f24e 20 70 49 64 78 49 6e 66 6f 2d 3e 65 73 74 69 6d   pIdxInfo->estim
1f24f 61 74 65 64 43 6f 73 74 3b 0a 7d 0a 23 65 6e 64  atedCost;.}.#end
1f250 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
1f251 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a  T_VIRTUALTABLE *
1f252 2f 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68  /../*.** Find th
1f253 65 20 62 65 73 74 20 69 6e 64 65 78 20 66 6f 72  e best index for
1f254 20 61 63 63 65 73 73 69 6e 67 20 61 20 70 61 72   accessing a par
1f255 74 69 63 75 6c 61 72 20 74 61 62 6c 65 2e 20 20  ticular table.  
1f256 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
1f257 0a 2a 2a 20 74 6f 20 74 68 65 20 69 6e 64 65 78  .** to the index
1f258 2c 20 66 6c 61 67 73 20 74 68 61 74 20 64 65 73  , flags that des
1f259 63 72 69 62 65 20 68 6f 77 20 74 68 65 20 69 6e  cribe how the in
1f25a 64 65 78 20 73 68 6f 75 6c 64 20 62 65 20 75 73  dex should be us
1f25b 65 64 2c 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65  ed, the.** numbe
1f25c 72 20 6f 66 20 65 71 75 61 6c 69 74 79 20 63 6f  r of equality co
1f25d 6e 73 74 72 61 69 6e 74 73 2c 20 61 6e 64 20 74  nstraints, and t
1f25e 68 65 20 22 63 6f 73 74 22 20 66 6f 72 20 74 68  he "cost" for th
1f25f 69 73 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20  is index..**.** 
1f260 54 68 65 20 6c 6f 77 65 73 74 20 63 6f 73 74 20  The lowest cost 
1f261 69 6e 64 65 78 20 77 69 6e 73 2e 20 20 54 68 65  index wins.  The
1f262 20 63 6f 73 74 20 69 73 20 61 6e 20 65 73 74 69   cost is an esti
1f263 6d 61 74 65 20 6f 66 20 74 68 65 20 61 6d 6f 75  mate of the amou
1f264 6e 74 20 6f 66 0a 2a 2a 20 43 50 55 20 61 6e 64  nt of.** CPU and
1f265 20 64 69 73 6b 20 49 2f 4f 20 6e 65 65 64 20 74   disk I/O need t
1f266 6f 20 70 72 6f 63 65 73 73 20 74 68 65 20 72 65  o process the re
1f267 71 75 65 73 74 20 75 73 69 6e 67 20 74 68 65 20  quest using the 
1f268 73 65 6c 65 63 74 65 64 20 69 6e 64 65 78 2e 0a  selected index..
1f269 2a 2a 20 46 61 63 74 6f 72 73 20 74 68 61 74 20  ** Factors that 
1f26a 69 6e 66 6c 75 65 6e 63 65 20 63 6f 73 74 20 69  influence cost i
1f26b 6e 63 6c 75 64 65 3a 0a 2a 2a 0a 2a 2a 20 20 20  nclude:.**.**   
1f26c 20 2a 20 20 54 68 65 20 65 73 74 69 6d 61 74 65   *  The estimate
1f26d 64 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  d number of rows
1f26e 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 72 65   that will be re
1f26f 74 72 69 65 76 65 64 2e 20 20 28 54 68 65 0a 2a  trieved.  (The.*
1f270 2a 20 20 20 20 20 20 20 66 65 77 65 72 20 74 68  *       fewer th
1f271 65 20 62 65 74 74 65 72 2e 29 0a 2a 2a 0a 2a 2a  e better.).**.**
1f272 20 20 20 20 2a 20 20 57 68 65 74 68 65 72 20 6f      *  Whether o
1f273 72 20 6e 6f 74 20 73 6f 72 74 69 6e 67 20 6d 75  r not sorting mu
1f274 73 74 20 6f 63 63 75 72 2e 0a 2a 2a 0a 2a 2a 20  st occur..**.** 
1f275 20 20 20 2a 20 20 57 68 65 74 68 65 72 20 6f 72     *  Whether or
1f276 20 6e 6f 74 20 74 68 65 72 65 20 6d 75 73 74 20   not there must 
1f277 62 65 20 73 65 70 61 72 61 74 65 20 6c 6f 6f 6b  be separate look
1f278 75 70 73 20 69 6e 20 74 68 65 0a 2a 2a 20 20 20  ups in the.**   
1f279 20 20 20 20 69 6e 64 65 78 20 61 6e 64 20 69 6e      index and in
1f27a 20 74 68 65 20 6d 61 69 6e 20 74 61 62 6c 65 2e   the main table.
1f27b 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 20 64 6f  .**.*/.static do
1f27c 75 62 6c 65 20 62 65 73 74 49 6e 64 65 78 28 0a  uble bestIndex(.
1f27d 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
1f27e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1f27f 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e   The parsing con
1f280 74 65 78 74 20 2a 2f 0a 20 20 57 68 65 72 65 43  text */.  WhereC
1f281 6c 61 75 73 65 20 2a 70 57 43 2c 20 20 20 20 20  lause *pWC,     
1f282 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45        /* The WHE
1f283 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73  RE clause */.  s
1f284 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
1f285 65 6d 20 2a 70 53 72 63 2c 20 20 2f 2a 20 54 68  em *pSrc,  /* Th
1f286 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65  e FROM clause te
1f287 72 6d 20 74 6f 20 73 65 61 72 63 68 20 2a 2f 0a  rm to search */.
1f288 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61    Bitmask notRea
1f289 64 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  dy,           /*
1f28a 20 4d 61 73 6b 20 6f 66 20 63 75 72 73 6f 72 73   Mask of cursors
1f28b 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20 61 76   that are not av
1f28c 61 69 6c 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70  ailable */.  Exp
1f28d 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c  rList *pOrderBy,
1f28e 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1f28f 6f 72 64 65 72 20 62 79 20 63 6c 61 75 73 65 20  order by clause 
1f290 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 2a 70 70 49  */.  Index **ppI
1f291 6e 64 65 78 2c 20 20 20 20 20 20 20 20 20 20 20  ndex,           
1f292 20 2f 2a 20 4d 61 6b 65 20 2a 70 70 49 6e 64 65   /* Make *ppInde
1f293 78 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 62  x point to the b
1f294 65 73 74 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69  est index */.  i
1f295 6e 74 20 2a 70 46 6c 61 67 73 2c 20 20 20 20 20  nt *pFlags,     
1f296 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 75             /* Pu
1f297 74 20 66 6c 61 67 73 20 64 65 73 63 72 69 62 69  t flags describi
1f298 6e 67 20 74 68 69 73 20 63 68 6f 69 63 65 20 69  ng this choice i
1f299 6e 20 2a 70 46 6c 61 67 73 20 2a 2f 0a 20 20 69  n *pFlags */.  i
1f29a 6e 74 20 2a 70 6e 45 71 20 20 20 20 20 20 20 20  nt *pnEq        
1f29b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 75             /* Pu
1f29c 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  t the number of 
1f29d 3d 3d 20 6f 72 20 49 4e 20 63 6f 6e 73 74 72 61  == or IN constra
1f29e 69 6e 74 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a  ints here */.){.
1f29f 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65    WhereTerm *pTe
1f2a0 72 6d 3b 0a 20 20 49 6e 64 65 78 20 2a 62 65 73  rm;.  Index *bes
1f2a1 74 49 64 78 20 3d 20 30 3b 20 20 20 20 20 20 20  tIdx = 0;       
1f2a2 20 20 2f 2a 20 49 6e 64 65 78 20 74 68 61 74 20    /* Index that 
1f2a3 67 69 76 65 73 20 74 68 65 20 6c 6f 77 65 73 74  gives the lowest
1f2a4 20 63 6f 73 74 20 2a 2f 0a 20 20 64 6f 75 62 6c   cost */.  doubl
1f2a5 65 20 6c 6f 77 65 73 74 43 6f 73 74 3b 20 20 20  e lowestCost;   
1f2a6 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f         /* The co
1f2a7 73 74 20 6f 66 20 75 73 69 6e 67 20 62 65 73 74  st of using best
1f2a8 49 64 78 20 2a 2f 0a 20 20 69 6e 74 20 62 65 73  Idx */.  int bes
1f2a9 74 46 6c 61 67 73 20 3d 20 30 3b 20 20 20 20 20  tFlags = 0;     
1f2aa 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 61 73       /* Flags as
1f2ab 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 62 65  sociated with be
1f2ac 73 74 49 64 78 20 2a 2f 0a 20 20 69 6e 74 20 62  stIdx */.  int b
1f2ad 65 73 74 4e 45 71 20 3d 20 30 3b 20 20 20 20 20  estNEq = 0;     
1f2ae 20 20 20 20 20 20 20 2f 2a 20 42 65 73 74 20 76         /* Best v
1f2af 61 6c 75 65 20 66 6f 72 20 6e 45 71 20 2a 2f 0a  alue for nEq */.
1f2b0 20 20 69 6e 74 20 69 43 75 72 20 3d 20 70 53 72    int iCur = pSr
1f2b1 63 2d 3e 69 43 75 72 73 6f 72 3b 20 20 20 2f 2a  c->iCursor;   /*
1f2b2 20 54 68 65 20 63 75 72 73 6f 72 20 6f 66 20 74   The cursor of t
1f2b3 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 61  he table to be a
1f2b4 63 63 65 73 73 65 64 20 2a 2f 0a 20 20 49 6e 64  ccessed */.  Ind
1f2b5 65 78 20 2a 70 50 72 6f 62 65 3b 20 20 20 20 20  ex *pProbe;     
1f2b6 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 69           /* An i
1f2b7 6e 64 65 78 20 77 65 20 61 72 65 20 65 76 61 6c  ndex we are eval
1f2b8 75 61 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20  uating */.  int 
1f2b9 72 65 76 3b 20 20 20 20 20 20 20 20 20 20 20 20  rev;            
1f2ba 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
1f2bb 74 6f 20 73 63 61 6e 20 69 6e 20 72 65 76 65 72  to scan in rever
1f2bc 73 65 20 6f 72 64 65 72 20 2a 2f 0a 20 20 69 6e  se order */.  in
1f2bd 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20  t flags;        
1f2be 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61            /* Fla
1f2bf 67 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  gs associated wi
1f2c0 74 68 20 70 50 72 6f 62 65 20 2a 2f 0a 20 20 69  th pProbe */.  i
1f2c1 6e 74 20 6e 45 71 3b 20 20 20 20 20 20 20 20 20  nt nEq;         
1f2c2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
1f2c3 6d 62 65 72 20 6f 66 20 3d 3d 20 6f 72 20 49 4e  mber of == or IN
1f2c4 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a   constraints */.
1f2c5 20 20 69 6e 74 20 65 71 54 65 72 6d 4d 61 73 6b    int eqTermMask
1f2c6 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
1f2c7 20 4d 61 73 6b 20 6f 66 20 76 61 6c 69 64 20 65   Mask of valid e
1f2c8 71 75 61 6c 69 74 79 20 6f 70 65 72 61 74 6f 72  quality operator
1f2c9 73 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 63 6f  s */.  double co
1f2ca 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  st;             
1f2cb 20 20 20 2f 2a 20 43 6f 73 74 20 6f 66 20 75 73     /* Cost of us
1f2cc 69 6e 67 20 70 50 72 6f 62 65 20 2a 2f 0a 0a 20  ing pProbe */.. 
1f2cd 20 57 48 45 52 45 54 52 41 43 45 28 28 22 62 65   WHERETRACE(("be
1f2ce 73 74 49 6e 64 65 78 3a 20 74 62 6c 3d 25 73 20  stIndex: tbl=%s 
1f2cf 6e 6f 74 52 65 61 64 79 3d 25 78 5c 6e 22 2c 20  notReady=%x\n", 
1f2d0 70 53 72 63 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d  pSrc->pTab->zNam
1f2d1 65 2c 20 6e 6f 74 52 65 61 64 79 29 29 3b 0a 20  e, notReady));. 
1f2d2 20 6c 6f 77 65 73 74 43 6f 73 74 20 3d 20 53 51   lowestCost = SQ
1f2d3 4c 49 54 45 5f 42 49 47 5f 44 42 4c 3b 0a 20 20  LITE_BIG_DBL;.  
1f2d4 70 50 72 6f 62 65 20 3d 20 70 53 72 63 2d 3e 70  pProbe = pSrc->p
1f2d5 54 61 62 2d 3e 70 49 6e 64 65 78 3b 0a 0a 20 20  Tab->pIndex;..  
1f2d6 2f 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 20  /* If the table 
1f2d7 68 61 73 20 6e 6f 20 69 6e 64 69 63 65 73 20 61  has no indices a
1f2d8 6e 64 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20  nd there are no 
1f2d9 74 65 72 6d 73 20 69 6e 20 74 68 65 20 77 68 65  terms in the whe
1f2da 72 65 0a 20 20 2a 2a 20 63 6c 61 75 73 65 20 74  re.  ** clause t
1f2db 68 61 74 20 72 65 66 65 72 20 74 6f 20 74 68 65  hat refer to the
1f2dc 20 52 4f 57 49 44 2c 20 74 68 65 6e 20 77 65 20   ROWID, then we 
1f2dd 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 61 62  will never be ab
1f2de 6c 65 20 74 6f 20 64 6f 0a 20 20 2a 2a 20 61 6e  le to do.  ** an
1f2df 79 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61  ything other tha
1f2e0 6e 20 61 20 66 75 6c 6c 20 74 61 62 6c 65 20 73  n a full table s
1f2e1 63 61 6e 20 6f 6e 20 74 68 69 73 20 74 61 62 6c  can on this tabl
1f2e2 65 2e 20 20 57 65 20 6d 69 67 68 74 20 61 73 0a  e.  We might as.
1f2e3 20 20 2a 2a 20 77 65 6c 6c 20 70 75 74 20 69 74    ** well put it
1f2e4 20 66 69 72 73 74 20 69 6e 20 74 68 65 20 6a 6f   first in the jo
1f2e5 69 6e 20 6f 72 64 65 72 2e 20 20 54 68 61 74 20  in order.  That 
1f2e6 77 61 79 2c 20 70 65 72 68 61 70 73 20 69 74 20  way, perhaps it 
1f2e7 63 61 6e 20 62 65 0a 20 20 2a 2a 20 72 65 66 65  can be.  ** refe
1f2e8 72 65 6e 63 65 64 20 62 79 20 6f 74 68 65 72 20  renced by other 
1f2e9 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 6a 6f  tables in the jo
1f2ea 69 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  in..  */.  if( p
1f2eb 50 72 6f 62 65 3d 3d 30 20 26 26 0a 20 20 20 20  Probe==0 &&.    
1f2ec 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69   findTerm(pWC, i
1f2ed 43 75 72 2c 20 2d 31 2c 20 30 2c 20 57 4f 5f 45  Cur, -1, 0, WO_E
1f2ee 51 7c 57 4f 5f 49 4e 7c 57 4f 5f 4c 54 7c 57 4f  Q|WO_IN|WO_LT|WO
1f2ef 5f 4c 45 7c 57 4f 5f 47 54 7c 57 4f 5f 47 45 2c  _LE|WO_GT|WO_GE,
1f2f0 30 29 3d 3d 30 20 26 26 0a 20 20 20 20 20 28 70  0)==0 &&.     (p
1f2f1 4f 72 64 65 72 42 79 3d 3d 30 20 7c 7c 20 21 73  OrderBy==0 || !s
1f2f2 6f 72 74 61 62 6c 65 42 79 52 6f 77 69 64 28 69  ortableByRowid(i
1f2f3 43 75 72 2c 20 70 4f 72 64 65 72 42 79 2c 20 70  Cur, pOrderBy, p
1f2f4 57 43 2d 3e 70 4d 61 73 6b 53 65 74 2c 20 26 72  WC->pMaskSet, &r
1f2f5 65 76 29 29 20 29 7b 0a 20 20 20 20 2a 70 46 6c  ev)) ){.    *pFl
1f2f6 61 67 73 20 3d 20 30 3b 0a 20 20 20 20 2a 70 70  ags = 0;.    *pp
1f2f7 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20 20 20 2a  Index = 0;.    *
1f2f8 70 6e 45 71 20 3d 20 30 3b 0a 20 20 20 20 72 65  pnEq = 0;.    re
1f2f9 74 75 72 6e 20 30 2e 30 3b 0a 20 20 7d 0a 0a 20  turn 0.0;.  }.. 
1f2fa 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 61 20   /* Check for a 
1f2fb 72 6f 77 69 64 3d 45 58 50 52 20 6f 72 20 72 6f  rowid=EXPR or ro
1f2fc 77 69 64 20 49 4e 20 28 2e 2e 2e 29 20 63 6f 6e  wid IN (...) con
1f2fd 73 74 72 61 69 6e 74 73 0a 20 20 2a 2f 0a 20 20  straints.  */.  
1f2fe 70 54 65 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d  pTerm = findTerm
1f2ff 28 70 57 43 2c 20 69 43 75 72 2c 20 2d 31 2c 20  (pWC, iCur, -1, 
1f300 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f 45 51 7c  notReady, WO_EQ|
1f301 57 4f 5f 49 4e 2c 20 30 29 3b 0a 20 20 69 66 28  WO_IN, 0);.  if(
1f302 20 70 54 65 72 6d 20 29 7b 0a 20 20 20 20 45 78   pTerm ){.    Ex
1f303 70 72 20 2a 70 45 78 70 72 3b 0a 20 20 20 20 2a  pr *pExpr;.    *
1f304 70 70 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20 20  ppIndex = 0;.   
1f305 20 62 65 73 74 46 6c 61 67 73 20 3d 20 57 48 45   bestFlags = WHE
1f306 52 45 5f 52 4f 57 49 44 5f 45 51 3b 0a 20 20 20  RE_ROWID_EQ;.   
1f307 20 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65   if( pTerm->eOpe
1f308 72 61 74 6f 72 20 26 20 57 4f 5f 45 51 20 29 7b  rator & WO_EQ ){
1f309 0a 20 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 3d  .      /* Rowid=
1f30a 3d 20 69 73 20 61 6c 77 61 79 73 20 74 68 65 20  = is always the 
1f30b 62 65 73 74 20 70 69 63 6b 2e 20 20 4c 6f 6f 6b  best pick.  Look
1f30c 20 6e 6f 20 66 75 72 74 68 65 72 2e 20 20 42 65   no further.  Be
1f30d 63 61 75 73 65 20 6f 6e 6c 79 0a 20 20 20 20 20  cause only.     
1f30e 20 2a 2a 20 61 20 73 69 6e 67 6c 65 20 72 6f 77   ** a single row
1f30f 20 69 73 20 67 65 6e 65 72 61 74 65 64 2c 20 6f   is generated, o
1f310 75 74 70 75 74 20 69 73 20 61 6c 77 61 79 73 20  utput is always 
1f311 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 20  in sorted order 
1f312 2a 2f 0a 20 20 20 20 20 20 2a 70 46 6c 61 67 73  */.      *pFlags
1f313 20 3d 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 45   = WHERE_ROWID_E
1f314 51 20 7c 20 57 48 45 52 45 5f 55 4e 49 51 55 45  Q | WHERE_UNIQUE
1f315 3b 0a 20 20 20 20 20 20 2a 70 6e 45 71 20 3d 20  ;.      *pnEq = 
1f316 31 3b 0a 20 20 20 20 20 20 57 48 45 52 45 54 52  1;.      WHERETR
1f317 41 43 45 28 28 22 2e 2e 2e 20 62 65 73 74 20 69  ACE(("... best i
1f318 73 20 72 6f 77 69 64 5c 6e 22 29 29 3b 0a 20 20  s rowid\n"));.  
1f319 20 20 20 20 72 65 74 75 72 6e 20 30 2e 30 3b 0a      return 0.0;.
1f31a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70      }else if( (p
1f31b 45 78 70 72 20 3d 20 70 54 65 72 6d 2d 3e 70 45  Expr = pTerm->pE
1f31c 78 70 72 29 2d 3e 70 4c 69 73 74 21 3d 30 20 29  xpr)->pList!=0 )
1f31d 7b 0a 20 20 20 20 20 20 2f 2a 20 52 6f 77 69 64  {.      /* Rowid
1f31e 20 49 4e 20 28 4c 49 53 54 29 3a 20 63 6f 73 74   IN (LIST): cost
1f31f 20 69 73 20 4e 6c 6f 67 4e 20 77 68 65 72 65 20   is NlogN where 
1f320 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  N is the number 
1f321 6f 66 20 6c 69 73 74 0a 20 20 20 20 20 20 2a 2a  of list.      **
1f322 20 65 6c 65 6d 65 6e 74 73 2e 20 20 2a 2f 0a 20   elements.  */. 
1f323 20 20 20 20 20 6c 6f 77 65 73 74 43 6f 73 74 20       lowestCost 
1f324 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e  = pExpr->pList->
1f325 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 6c 6f 77  nExpr;.      low
1f326 65 73 74 43 6f 73 74 20 2a 3d 20 65 73 74 4c 6f  estCost *= estLo
1f327 67 28 6c 6f 77 65 73 74 43 6f 73 74 29 3b 0a 20  g(lowestCost);. 
1f328 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1f329 2f 2a 20 52 6f 77 69 64 20 49 4e 20 28 53 45 4c  /* Rowid IN (SEL
1f32a 45 43 54 29 3a 20 63 6f 73 74 20 69 73 20 4e 6c  ECT): cost is Nl
1f32b 6f 67 4e 20 77 68 65 72 65 20 4e 20 69 73 20 74  ogN where N is t
1f32c 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  he number of row
1f32d 73 0a 20 20 20 20 20 20 2a 2a 20 69 6e 20 74 68  s.      ** in th
1f32e 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20  e result of the 
1f32f 69 6e 6e 65 72 20 73 65 6c 65 63 74 2e 20 20 57  inner select.  W
1f330 65 20 68 61 76 65 20 6e 6f 20 77 61 79 20 74 6f  e have no way to
1f331 20 65 73 74 69 6d 61 74 65 0a 20 20 20 20 20 20   estimate.      
1f332 2a 2a 20 74 68 61 74 20 76 61 6c 75 65 20 73 6f  ** that value so
1f333 20 6d 61 6b 65 20 61 20 77 69 6c 64 20 67 75 65   make a wild gue
1f334 73 73 2e 20 2a 2f 0a 20 20 20 20 20 20 6c 6f 77  ss. */.      low
1f335 65 73 74 43 6f 73 74 20 3d 20 32 30 30 3b 0a 20  estCost = 200;. 
1f336 20 20 20 7d 0a 20 20 20 20 57 48 45 52 45 54 52     }.    WHERETR
1f337 41 43 45 28 28 22 2e 2e 2e 20 72 6f 77 69 64 20  ACE(("... rowid 
1f338 49 4e 20 63 6f 73 74 3a 20 25 2e 39 67 5c 6e 22  IN cost: %.9g\n"
1f339 2c 20 6c 6f 77 65 73 74 43 6f 73 74 29 29 3b 0a  , lowestCost));.
1f33a 20 20 7d 0a 0a 20 20 2f 2a 20 45 73 74 69 6d 61    }..  /* Estima
1f33b 74 65 20 74 68 65 20 63 6f 73 74 20 6f 66 20 61  te the cost of a
1f33c 20 74 61 62 6c 65 20 73 63 61 6e 2e 20 20 49 66   table scan.  If
1f33d 20 77 65 20 64 6f 20 6e 6f 74 20 6b 6e 6f 77 20   we do not know 
1f33e 68 6f 77 20 6d 61 6e 79 0a 20 20 2a 2a 20 65 6e  how many.  ** en
1f33f 74 72 69 65 73 20 61 72 65 20 69 6e 20 74 68 65  tries are in the
1f340 20 74 61 62 6c 65 2c 20 75 73 65 20 31 20 6d 69   table, use 1 mi
1f341 6c 6c 69 6f 6e 20 61 73 20 61 20 67 75 65 73 73  llion as a guess
1f342 2e 0a 20 20 2a 2f 0a 20 20 63 6f 73 74 20 3d 20  ..  */.  cost = 
1f343 70 50 72 6f 62 65 20 3f 20 70 50 72 6f 62 65 2d  pProbe ? pProbe-
1f344 3e 61 69 52 6f 77 45 73 74 5b 30 5d 20 3a 20 31  >aiRowEst[0] : 1
1f345 30 30 30 30 30 30 3b 0a 20 20 57 48 45 52 45 54  000000;.  WHERET
1f346 52 41 43 45 28 28 22 2e 2e 2e 20 74 61 62 6c 65  RACE(("... table
1f347 20 73 63 61 6e 20 62 61 73 65 20 63 6f 73 74 3a   scan base cost:
1f348 20 25 2e 39 67 5c 6e 22 2c 20 63 6f 73 74 29 29   %.9g\n", cost))
1f349 3b 0a 20 20 66 6c 61 67 73 20 3d 20 57 48 45 52  ;.  flags = WHER
1f34a 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45 3b 0a 0a  E_ROWID_RANGE;..
1f34b 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 63    /* Check for c
1f34c 6f 6e 73 74 72 61 69 6e 74 73 20 6f 6e 20 61 20  onstraints on a 
1f34d 72 61 6e 67 65 20 6f 66 20 72 6f 77 69 64 73 20  range of rowids 
1f34e 69 6e 20 61 20 74 61 62 6c 65 20 73 63 61 6e 2e  in a table scan.
1f34f 0a 20 20 2a 2f 0a 20 20 70 54 65 72 6d 20 3d 20  .  */.  pTerm = 
1f350 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43  findTerm(pWC, iC
1f351 75 72 2c 20 2d 31 2c 20 6e 6f 74 52 65 61 64 79  ur, -1, notReady
1f352 2c 20 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 7c 57 4f  , WO_LT|WO_LE|WO
1f353 5f 47 54 7c 57 4f 5f 47 45 2c 20 30 29 3b 0a 20  _GT|WO_GE, 0);. 
1f354 20 69 66 28 20 70 54 65 72 6d 20 29 7b 0a 20 20   if( pTerm ){.  
1f355 20 20 69 66 28 20 66 69 6e 64 54 65 72 6d 28 70    if( findTerm(p
1f356 57 43 2c 20 69 43 75 72 2c 20 2d 31 2c 20 6e 6f  WC, iCur, -1, no
1f357 74 52 65 61 64 79 2c 20 57 4f 5f 4c 54 7c 57 4f  tReady, WO_LT|WO
1f358 5f 4c 45 2c 20 30 29 20 29 7b 0a 20 20 20 20 20  _LE, 0) ){.     
1f359 20 66 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f   flags |= WHERE_
1f35a 54 4f 50 5f 4c 49 4d 49 54 3b 0a 20 20 20 20 20  TOP_LIMIT;.     
1f35b 20 63 6f 73 74 20 2f 3d 20 33 3b 20 20 2f 2a 20   cost /= 3;  /* 
1f35c 47 75 65 73 73 20 74 68 61 74 20 72 6f 77 69 64  Guess that rowid
1f35d 3c 45 58 50 52 20 65 6c 69 6d 69 6e 61 74 65 73  <EXPR eliminates
1f35e 20 74 77 6f 2d 74 68 69 72 64 73 20 6f 72 20 72   two-thirds or r
1f35f 6f 77 73 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20  ows */.    }.   
1f360 20 69 66 28 20 66 69 6e 64 54 65 72 6d 28 70 57   if( findTerm(pW
1f361 43 2c 20 69 43 75 72 2c 20 2d 31 2c 20 6e 6f 74  C, iCur, -1, not
1f362 52 65 61 64 79 2c 20 57 4f 5f 47 54 7c 57 4f 5f  Ready, WO_GT|WO_
1f363 47 45 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 20  GE, 0) ){.      
1f364 66 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 42  flags |= WHERE_B
1f365 54 4d 5f 4c 49 4d 49 54 3b 0a 20 20 20 20 20 20  TM_LIMIT;.      
1f366 63 6f 73 74 20 2f 3d 20 33 3b 20 20 2f 2a 20 47  cost /= 3;  /* G
1f367 75 65 73 73 20 74 68 61 74 20 72 6f 77 69 64 3e  uess that rowid>
1f368 45 58 50 52 20 65 6c 69 6d 69 6e 61 74 65 73 20  EXPR eliminates 
1f369 74 77 6f 2d 74 68 69 72 64 73 20 6f 66 20 72 6f  two-thirds of ro
1f36a 77 73 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20  ws */.    }.    
1f36b 57 48 45 52 45 54 52 41 43 45 28 28 22 2e 2e 2e  WHERETRACE(("...
1f36c 20 72 6f 77 69 64 20 72 61 6e 67 65 20 72 65 64   rowid range red
1f36d 75 63 65 73 20 63 6f 73 74 20 74 6f 20 25 2e 39  uces cost to %.9
1f36e 67 5c 6e 22 2c 20 63 6f 73 74 29 29 3b 0a 20 20  g\n", cost));.  
1f36f 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6c 61 67 73  }else{.    flags
1f370 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20   = 0;.  }..  /* 
1f371 49 66 20 74 68 65 20 74 61 62 6c 65 20 73 63 61  If the table sca
1f372 6e 20 64 6f 65 73 20 6e 6f 74 20 73 61 74 69 73  n does not satis
1f373 66 79 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  fy the ORDER BY 
1f374 63 6c 61 75 73 65 2c 20 69 6e 63 72 65 61 73 65  clause, increase
1f375 0a 20 20 2a 2a 20 74 68 65 20 63 6f 73 74 20 62  .  ** the cost b
1f376 79 20 4e 6c 6f 67 4e 20 74 6f 20 63 6f 76 65 72  y NlogN to cover
1f377 20 74 68 65 20 65 78 70 65 6e 73 65 20 6f 66 20   the expense of 
1f378 73 6f 72 74 69 6e 67 2e 20 2a 2f 0a 20 20 69 66  sorting. */.  if
1f379 28 20 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20  ( pOrderBy ){.  
1f37a 20 20 69 66 28 20 73 6f 72 74 61 62 6c 65 42 79    if( sortableBy
1f37b 52 6f 77 69 64 28 69 43 75 72 2c 20 70 4f 72 64  Rowid(iCur, pOrd
1f37c 65 72 42 79 2c 20 70 57 43 2d 3e 70 4d 61 73 6b  erBy, pWC->pMask
1f37d 53 65 74 2c 20 26 72 65 76 29 20 29 7b 0a 20 20  Set, &rev) ){.  
1f37e 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 57 48 45      flags |= WHE
1f37f 52 45 5f 4f 52 44 45 52 42 59 7c 57 48 45 52 45  RE_ORDERBY|WHERE
1f380 5f 52 4f 57 49 44 5f 52 41 4e 47 45 3b 0a 20 20  _ROWID_RANGE;.  
1f381 20 20 20 20 69 66 28 20 72 65 76 20 29 7b 0a 20      if( rev ){. 
1f382 20 20 20 20 20 20 20 66 6c 61 67 73 20 7c 3d 20         flags |= 
1f383 57 48 45 52 45 5f 52 45 56 45 52 53 45 3b 0a 20  WHERE_REVERSE;. 
1f384 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
1f385 7b 0a 20 20 20 20 20 20 63 6f 73 74 20 2b 3d 20  {.      cost += 
1f386 63 6f 73 74 2a 65 73 74 4c 6f 67 28 63 6f 73 74  cost*estLog(cost
1f387 29 3b 0a 20 20 20 20 20 20 57 48 45 52 45 54 52  );.      WHERETR
1f388 41 43 45 28 28 22 2e 2e 2e 20 73 6f 72 74 69 6e  ACE(("... sortin
1f389 67 20 69 6e 63 72 65 61 73 65 73 20 63 6f 73 74  g increases cost
1f38a 20 74 6f 20 25 2e 39 67 5c 6e 22 2c 20 63 6f 73   to %.9g\n", cos
1f38b 74 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  t));.    }.  }. 
1f38c 20 69 66 28 20 63 6f 73 74 3c 6c 6f 77 65 73 74   if( cost<lowest
1f38d 43 6f 73 74 20 29 7b 0a 20 20 20 20 6c 6f 77 65  Cost ){.    lowe
1f38e 73 74 43 6f 73 74 20 3d 20 63 6f 73 74 3b 0a 20  stCost = cost;. 
1f38f 20 20 20 62 65 73 74 46 6c 61 67 73 20 3d 20 66     bestFlags = f
1f390 6c 61 67 73 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  lags;.  }..  /* 
1f391 49 66 20 74 68 65 20 70 53 72 63 20 74 61 62 6c  If the pSrc tabl
1f392 65 20 69 73 20 74 68 65 20 72 69 67 68 74 20 74  e is the right t
1f393 61 62 6c 65 20 6f 66 20 61 20 4c 45 46 54 20 4a  able of a LEFT J
1f394 4f 49 4e 20 74 68 65 6e 20 77 65 20 6d 61 79 20  OIN then we may 
1f395 6e 6f 74 0a 20 20 2a 2a 20 75 73 65 20 61 6e 20  not.  ** use an 
1f396 69 6e 64 65 78 20 74 6f 20 73 61 74 69 73 66 79  index to satisfy
1f397 20 49 53 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61   IS NULL constra
1f398 69 6e 74 73 20 6f 6e 20 74 68 61 74 20 74 61 62  ints on that tab
1f399 6c 65 2e 20 20 54 68 69 73 20 69 73 0a 20 20 2a  le.  This is.  *
1f39a 2a 20 62 65 63 61 75 73 65 20 63 6f 6c 75 6d 6e  * because column
1f39b 73 20 6d 69 67 68 74 20 65 6e 64 20 75 70 20 62  s might end up b
1f39c 65 69 6e 67 20 4e 55 4c 4c 20 69 66 20 74 68 65  eing NULL if the
1f39d 20 74 61 62 6c 65 20 64 6f 65 73 20 6e 6f 74 20   table does not 
1f39e 6d 61 74 63 68 20 2d 0a 20 20 2a 2a 20 61 20 63  match -.  ** a c
1f39f 69 72 63 75 6d 73 74 61 6e 63 65 20 77 68 69 63  ircumstance whic
1f3a0 68 20 74 68 65 20 69 6e 64 65 78 20 63 61 6e 6e  h the index cann
1f3a1 6f 74 20 68 65 6c 70 20 75 73 20 64 69 73 63 6f  ot help us disco
1f3a2 76 65 72 2e 20 20 54 69 63 6b 65 74 20 23 32 31  ver.  Ticket #21
1f3a3 37 37 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  77..  */.  if( (
1f3a4 70 53 72 63 2d 3e 6a 6f 69 6e 74 79 70 65 20 26  pSrc->jointype &
1f3a5 20 4a 54 5f 4c 45 46 54 29 21 3d 30 20 29 7b 0a   JT_LEFT)!=0 ){.
1f3a6 20 20 20 20 65 71 54 65 72 6d 4d 61 73 6b 20 3d      eqTermMask =
1f3a7 20 57 4f 5f 45 51 7c 57 4f 5f 49 4e 3b 0a 20 20   WO_EQ|WO_IN;.  
1f3a8 7d 65 6c 73 65 7b 0a 20 20 20 20 65 71 54 65 72  }else{.    eqTer
1f3a9 6d 4d 61 73 6b 20 3d 20 57 4f 5f 45 51 7c 57 4f  mMask = WO_EQ|WO
1f3aa 5f 49 4e 7c 57 4f 5f 49 53 4e 55 4c 4c 3b 0a 20  _IN|WO_ISNULL;. 
1f3ab 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 6f 6b 20 61 74   }..  /* Look at
1f3ac 20 65 61 63 68 20 69 6e 64 65 78 2e 0a 20 20 2a   each index..  *
1f3ad 2f 0a 20 20 66 6f 72 28 3b 20 70 50 72 6f 62 65  /.  for(; pProbe
1f3ae 3b 20 70 50 72 6f 62 65 3d 70 50 72 6f 62 65 2d  ; pProbe=pProbe-
1f3af 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 6e 74  >pNext){.    int
1f3b0 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
1f3b1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
1f3b2 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20  p counter */.   
1f3b3 20 64 6f 75 62 6c 65 20 69 6e 4d 75 6c 74 69 70   double inMultip
1f3b4 6c 69 65 72 20 3d 20 31 3b 0a 0a 20 20 20 20 57  lier = 1;..    W
1f3b5 48 45 52 45 54 52 41 43 45 28 28 22 2e 2e 2e 20  HERETRACE(("... 
1f3b6 69 6e 64 65 78 20 25 73 3a 5c 6e 22 2c 20 70 50  index %s:\n", pP
1f3b7 72 6f 62 65 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 0a  robe->zName));..
1f3b8 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20 74 68 65      /* Count the
1f3b9 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
1f3ba 6e 73 20 69 6e 20 74 68 65 20 69 6e 64 65 78 20  ns in the index 
1f3bb 74 68 61 74 20 61 72 65 20 73 61 74 69 73 66 69  that are satisfi
1f3bc 65 64 0a 20 20 20 20 2a 2a 20 62 79 20 78 3d 45  ed.    ** by x=E
1f3bd 58 50 52 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  XPR constraints 
1f3be 6f 72 20 78 20 49 4e 20 28 2e 2e 2e 29 20 63 6f  or x IN (...) co
1f3bf 6e 73 74 72 61 69 6e 74 73 2e 0a 20 20 20 20 2a  nstraints..    *
1f3c0 2f 0a 20 20 20 20 66 6c 61 67 73 20 3d 20 30 3b  /.    flags = 0;
1f3c1 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
1f3c2 70 50 72 6f 62 65 2d 3e 6e 43 6f 6c 75 6d 6e 3b  pProbe->nColumn;
1f3c3 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74   i++){.      int
1f3c4 20 6a 20 3d 20 70 50 72 6f 62 65 2d 3e 61 69 43   j = pProbe->aiC
1f3c5 6f 6c 75 6d 6e 5b 69 5d 3b 0a 20 20 20 20 20 20  olumn[i];.      
1f3c6 70 54 65 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d  pTerm = findTerm
1f3c7 28 70 57 43 2c 20 69 43 75 72 2c 20 6a 2c 20 6e  (pWC, iCur, j, n
1f3c8 6f 74 52 65 61 64 79 2c 20 65 71 54 65 72 6d 4d  otReady, eqTermM
1f3c9 61 73 6b 2c 20 70 50 72 6f 62 65 29 3b 0a 20 20  ask, pProbe);.  
1f3ca 20 20 20 20 69 66 28 20 70 54 65 72 6d 3d 3d 30      if( pTerm==0
1f3cb 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
1f3cc 66 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 43  flags |= WHERE_C
1f3cd 4f 4c 55 4d 4e 5f 45 51 3b 0a 20 20 20 20 20 20  OLUMN_EQ;.      
1f3ce 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72  if( pTerm->eOper
1f3cf 61 74 6f 72 20 26 20 57 4f 5f 49 4e 20 29 7b 0a  ator & WO_IN ){.
1f3d0 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 45          Expr *pE
1f3d1 78 70 72 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78  xpr = pTerm->pEx
1f3d2 70 72 3b 0a 20 20 20 20 20 20 20 20 66 6c 61 67  pr;.        flag
1f3d3 73 20 7c 3d 20 57 48 45 52 45 5f 43 4f 4c 55 4d  s |= WHERE_COLUM
1f3d4 4e 5f 49 4e 3b 0a 20 20 20 20 20 20 20 20 69 66  N_IN;.        if
1f3d5 28 20 70 45 78 70 72 2d 3e 70 53 65 6c 65 63 74  ( pExpr->pSelect
1f3d6 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  !=0 ){.         
1f3d7 20 69 6e 4d 75 6c 74 69 70 6c 69 65 72 20 2a 3d   inMultiplier *=
1f3d8 20 32 35 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c   25;.        }el
1f3d9 73 65 20 69 66 28 20 70 45 78 70 72 2d 3e 70 4c  se if( pExpr->pL
1f3da 69 73 74 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  ist!=0 ){.      
1f3db 20 20 20 20 69 6e 4d 75 6c 74 69 70 6c 69 65 72      inMultiplier
1f3dc 20 2a 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74   *= pExpr->pList
1f3dd 2d 3e 6e 45 78 70 72 20 2b 20 31 3b 0a 20 20 20  ->nExpr + 1;.   
1f3de 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
1f3df 20 20 20 7d 0a 20 20 20 20 63 6f 73 74 20 3d 20     }.    cost = 
1f3e0 70 50 72 6f 62 65 2d 3e 61 69 52 6f 77 45 73 74  pProbe->aiRowEst
1f3e1 5b 69 5d 20 2a 20 69 6e 4d 75 6c 74 69 70 6c 69  [i] * inMultipli
1f3e2 65 72 20 2a 20 65 73 74 4c 6f 67 28 69 6e 4d 75  er * estLog(inMu
1f3e3 6c 74 69 70 6c 69 65 72 29 3b 0a 20 20 20 20 6e  ltiplier);.    n
1f3e4 45 71 20 3d 20 69 3b 0a 20 20 20 20 69 66 28 20  Eq = i;.    if( 
1f3e5 70 50 72 6f 62 65 2d 3e 6f 6e 45 72 72 6f 72 21  pProbe->onError!
1f3e6 3d 4f 45 5f 4e 6f 6e 65 20 26 26 20 28 66 6c 61  =OE_None && (fla
1f3e7 67 73 20 26 20 57 48 45 52 45 5f 43 4f 4c 55 4d  gs & WHERE_COLUM
1f3e8 4e 5f 49 4e 29 3d 3d 30 0a 20 20 20 20 20 20 20  N_IN)==0.       
1f3e9 20 20 26 26 20 6e 45 71 3d 3d 70 50 72 6f 62 65    && nEq==pProbe
1f3ea 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  ->nColumn ){.   
1f3eb 20 20 20 66 6c 61 67 73 20 7c 3d 20 57 48 45 52     flags |= WHER
1f3ec 45 5f 55 4e 49 51 55 45 3b 0a 20 20 20 20 7d 0a  E_UNIQUE;.    }.
1f3ed 20 20 20 20 57 48 45 52 45 54 52 41 43 45 28 28      WHERETRACE((
1f3ee 22 2e 2e 2e 2e 2e 2e 20 6e 45 71 3d 25 64 20 69  "...... nEq=%d i
1f3ef 6e 4d 75 6c 74 3d 25 2e 39 67 20 63 6f 73 74 3d  nMult=%.9g cost=
1f3f0 25 2e 39 67 5c 6e 22 2c 20 6e 45 71 2c 20 69 6e  %.9g\n", nEq, in
1f3f1 4d 75 6c 74 69 70 6c 69 65 72 2c 20 63 6f 73 74  Multiplier, cost
1f3f2 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 4c 6f 6f 6b  ));..    /* Look
1f3f3 20 66 6f 72 20 72 61 6e 67 65 20 63 6f 6e 73 74   for range const
1f3f4 72 61 69 6e 74 73 0a 20 20 20 20 2a 2f 0a 20 20  raints.    */.  
1f3f5 20 20 69 66 28 20 6e 45 71 3c 70 50 72 6f 62 65    if( nEq<pProbe
1f3f6 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  ->nColumn ){.   
1f3f7 20 20 20 69 6e 74 20 6a 20 3d 20 70 50 72 6f 62     int j = pProb
1f3f8 65 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 45 71 5d  e->aiColumn[nEq]
1f3f9 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 20 3d 20  ;.      pTerm = 
1f3fa 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43  findTerm(pWC, iC
1f3fb 75 72 2c 20 6a 2c 20 6e 6f 74 52 65 61 64 79 2c  ur, j, notReady,
1f3fc 20 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 7c 57 4f 5f   WO_LT|WO_LE|WO_
1f3fd 47 54 7c 57 4f 5f 47 45 2c 20 70 50 72 6f 62 65  GT|WO_GE, pProbe
1f3fe 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 65  );.      if( pTe
1f3ff 72 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6c  rm ){.        fl
1f400 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 43 4f 4c  ags |= WHERE_COL
1f401 55 4d 4e 5f 52 41 4e 47 45 3b 0a 20 20 20 20 20  UMN_RANGE;.     
1f402 20 20 20 69 66 28 20 66 69 6e 64 54 65 72 6d 28     if( findTerm(
1f403 70 57 43 2c 20 69 43 75 72 2c 20 6a 2c 20 6e 6f  pWC, iCur, j, no
1f404 74 52 65 61 64 79 2c 20 57 4f 5f 4c 54 7c 57 4f  tReady, WO_LT|WO
1f405 5f 4c 45 2c 20 70 50 72 6f 62 65 29 20 29 7b 0a  _LE, pProbe) ){.
1f406 20 20 20 20 20 20 20 20 20 20 66 6c 61 67 73 20            flags 
1f407 7c 3d 20 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d  |= WHERE_TOP_LIM
1f408 49 54 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6f  IT;.          co
1f409 73 74 20 2f 3d 20 33 3b 0a 20 20 20 20 20 20 20  st /= 3;.       
1f40a 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 66   }.        if( f
1f40b 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75  indTerm(pWC, iCu
1f40c 72 2c 20 6a 2c 20 6e 6f 74 52 65 61 64 79 2c 20  r, j, notReady, 
1f40d 57 4f 5f 47 54 7c 57 4f 5f 47 45 2c 20 70 50 72  WO_GT|WO_GE, pPr
1f40e 6f 62 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20  obe) ){.        
1f40f 20 20 66 6c 61 67 73 20 7c 3d 20 57 48 45 52 45    flags |= WHERE
1f410 5f 42 54 4d 5f 4c 49 4d 49 54 3b 0a 20 20 20 20  _BTM_LIMIT;.    
1f411 20 20 20 20 20 20 63 6f 73 74 20 2f 3d 20 33 3b        cost /= 3;
1f412 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1f413 20 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22     WHERETRACE(("
1f414 2e 2e 2e 2e 2e 2e 20 72 61 6e 67 65 20 72 65 64  ...... range red
1f415 75 63 65 73 20 63 6f 73 74 20 74 6f 20 25 2e 39  uces cost to %.9
1f416 67 5c 6e 22 2c 20 63 6f 73 74 29 29 3b 0a 20 20  g\n", cost));.  
1f417 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
1f418 20 2f 2a 20 41 64 64 20 74 68 65 20 61 64 64 69   /* Add the addi
1f419 74 69 6f 6e 61 6c 20 63 6f 73 74 20 6f 66 20 73  tional cost of s
1f41a 6f 72 74 69 6e 67 20 69 66 20 74 68 61 74 20 69  orting if that i
1f41b 73 20 61 20 66 61 63 74 6f 72 2e 0a 20 20 20 20  s a factor..    
1f41c 2a 2f 0a 20 20 20 20 69 66 28 20 70 4f 72 64 65  */.    if( pOrde
1f41d 72 42 79 20 29 7b 0a 20 20 20 20 20 20 69 66 28  rBy ){.      if(
1f41e 20 28 66 6c 61 67 73 20 26 20 57 48 45 52 45 5f   (flags & WHERE_
1f41f 43 4f 4c 55 4d 4e 5f 49 4e 29 3d 3d 30 20 26 26  COLUMN_IN)==0 &&
1f420 0a 20 20 20 20 20 20 20 20 20 20 20 69 73 53 6f  .           isSo
1f421 72 74 69 6e 67 49 6e 64 65 78 28 70 50 61 72 73  rtingIndex(pPars
1f422 65 2c 70 57 43 2d 3e 70 4d 61 73 6b 53 65 74 2c  e,pWC->pMaskSet,
1f423 70 50 72 6f 62 65 2c 69 43 75 72 2c 70 4f 72 64  pProbe,iCur,pOrd
1f424 65 72 42 79 2c 6e 45 71 2c 26 72 65 76 29 20 29  erBy,nEq,&rev) )
1f425 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 6c  {.        if( fl
1f426 61 67 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ags==0 ){.      
1f427 20 20 20 20 66 6c 61 67 73 20 3d 20 57 48 45 52      flags = WHER
1f428 45 5f 43 4f 4c 55 4d 4e 5f 52 41 4e 47 45 3b 0a  E_COLUMN_RANGE;.
1f429 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1f42a 20 20 66 6c 61 67 73 20 7c 3d 20 57 48 45 52 45    flags |= WHERE
1f42b 5f 4f 52 44 45 52 42 59 3b 0a 20 20 20 20 20 20  _ORDERBY;.      
1f42c 20 20 69 66 28 20 72 65 76 20 29 7b 0a 20 20 20    if( rev ){.   
1f42d 20 20 20 20 20 20 20 66 6c 61 67 73 20 7c 3d 20         flags |= 
1f42e 57 48 45 52 45 5f 52 45 56 45 52 53 45 3b 0a 20  WHERE_REVERSE;. 
1f42f 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
1f430 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f  else{.        co
1f431 73 74 20 2b 3d 20 63 6f 73 74 2a 65 73 74 4c 6f  st += cost*estLo
1f432 67 28 63 6f 73 74 29 3b 0a 20 20 20 20 20 20 20  g(cost);.       
1f433 20 57 48 45 52 45 54 52 41 43 45 28 28 22 2e 2e   WHERETRACE(("..
1f434 2e 2e 2e 2e 20 6f 72 64 65 72 62 79 20 69 6e 63  .... orderby inc
1f435 72 65 61 73 65 73 20 63 6f 73 74 20 74 6f 20 25  reases cost to %
1f436 2e 39 67 5c 6e 22 2c 20 63 6f 73 74 29 29 3b 0a  .9g\n", cost));.
1f437 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
1f438 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73     /* Check to s
1f439 65 65 20 69 66 20 77 65 20 63 61 6e 20 67 65 74  ee if we can get
1f43a 20 61 77 61 79 20 77 69 74 68 20 75 73 69 6e 67   away with using
1f43b 20 6a 75 73 74 20 74 68 65 20 69 6e 64 65 78 20   just the index 
1f43c 77 69 74 68 6f 75 74 0a 20 20 20 20 2a 2a 20 65  without.    ** e
1f43d 76 65 72 20 72 65 61 64 69 6e 67 20 74 68 65 20  ver reading the 
1f43e 74 61 62 6c 65 2e 20 20 49 66 20 74 68 61 74 20  table.  If that 
1f43f 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 68 65  is the case, the
1f440 6e 20 68 61 6c 76 65 20 74 68 65 0a 20 20 20 20  n halve the.    
1f441 2a 2a 20 63 6f 73 74 20 6f 66 20 74 68 69 73 20  ** cost of this 
1f442 69 6e 64 65 78 2e 0a 20 20 20 20 2a 2f 0a 20 20  index..    */.  
1f443 20 20 69 66 28 20 66 6c 61 67 73 20 26 26 20 70    if( flags && p
1f444 53 72 63 2d 3e 63 6f 6c 55 73 65 64 20 3c 20 28  Src->colUsed < (
1f445 28 28 42 69 74 6d 61 73 6b 29 31 29 3c 3c 28 42  ((Bitmask)1)<<(B
1f446 4d 53 2d 31 29 29 20 29 7b 0a 20 20 20 20 20 20  MS-1)) ){.      
1f447 42 69 74 6d 61 73 6b 20 6d 20 3d 20 70 53 72 63  Bitmask m = pSrc
1f448 2d 3e 63 6f 6c 55 73 65 64 3b 0a 20 20 20 20 20  ->colUsed;.     
1f449 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 66 6f   int j;.      fo
1f44a 72 28 6a 3d 30 3b 20 6a 3c 70 50 72 6f 62 65 2d  r(j=0; j<pProbe-
1f44b 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a  >nColumn; j++){.
1f44c 20 20 20 20 20 20 20 20 69 6e 74 20 78 20 3d 20          int x = 
1f44d 70 50 72 6f 62 65 2d 3e 61 69 43 6f 6c 75 6d 6e  pProbe->aiColumn
1f44e 5b 6a 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28  [j];.        if(
1f44f 20 78 3c 42 4d 53 2d 31 20 29 7b 0a 20 20 20 20   x<BMS-1 ){.    
1f450 20 20 20 20 20 20 6d 20 26 3d 20 7e 28 28 28 42        m &= ~(((B
1f451 69 74 6d 61 73 6b 29 31 29 3c 3c 78 29 3b 0a 20  itmask)1)<<x);. 
1f452 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
1f453 0a 20 20 20 20 20 20 69 66 28 20 6d 3d 3d 30 20  .      if( m==0 
1f454 29 7b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 73  ){.        flags
1f455 20 7c 3d 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e   |= WHERE_IDX_ON
1f456 4c 59 3b 0a 20 20 20 20 20 20 20 20 63 6f 73 74  LY;.        cost
1f457 20 2f 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 57   /= 2;.        W
1f458 48 45 52 45 54 52 41 43 45 28 28 22 2e 2e 2e 2e  HERETRACE(("....
1f459 2e 2e 20 69 64 78 2d 6f 6e 6c 79 20 72 65 64 75  .. idx-only redu
1f45a 63 65 73 20 63 6f 73 74 20 74 6f 20 25 2e 39 67  ces cost to %.9g
1f45b 5c 6e 22 2c 20 63 6f 73 74 29 29 3b 0a 20 20 20  \n", cost));.   
1f45c 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
1f45d 2f 2a 20 49 66 20 74 68 69 73 20 69 6e 64 65 78  /* If this index
1f45e 20 68 61 73 20 61 63 68 69 65 76 65 64 20 74 68   has achieved th
1f45f 65 20 6c 6f 77 65 73 74 20 63 6f 73 74 20 73 6f  e lowest cost so
1f460 20 66 61 72 2c 20 74 68 65 6e 20 75 73 65 20 69   far, then use i
1f461 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  t..    */.    if
1f462 28 20 63 6f 73 74 20 3c 20 6c 6f 77 65 73 74 43  ( cost < lowestC
1f463 6f 73 74 20 29 7b 0a 20 20 20 20 20 20 62 65 73  ost ){.      bes
1f464 74 49 64 78 20 3d 20 70 50 72 6f 62 65 3b 0a 20  tIdx = pProbe;. 
1f465 20 20 20 20 20 6c 6f 77 65 73 74 43 6f 73 74 20       lowestCost 
1f466 3d 20 63 6f 73 74 3b 0a 20 20 20 20 20 20 61 73  = cost;.      as
1f467 73 65 72 74 28 20 66 6c 61 67 73 21 3d 30 20 29  sert( flags!=0 )
1f468 3b 0a 20 20 20 20 20 20 62 65 73 74 46 6c 61 67  ;.      bestFlag
1f469 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20 20 20 20  s = flags;.     
1f46a 20 62 65 73 74 4e 45 71 20 3d 20 6e 45 71 3b 0a   bestNEq = nEq;.
1f46b 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
1f46c 52 65 70 6f 72 74 20 74 68 65 20 62 65 73 74 20  Report the best 
1f46d 72 65 73 75 6c 74 0a 20 20 2a 2f 0a 20 20 2a 70  result.  */.  *p
1f46e 70 49 6e 64 65 78 20 3d 20 62 65 73 74 49 64 78  pIndex = bestIdx
1f46f 3b 0a 20 20 57 48 45 52 45 54 52 41 43 45 28 28  ;.  WHERETRACE((
1f470 22 62 65 73 74 20 69 6e 64 65 78 20 69 73 20 25  "best index is %
1f471 73 2c 20 63 6f 73 74 3d 25 2e 39 67 2c 20 66 6c  s, cost=%.9g, fl
1f472 61 67 73 3d 25 78 2c 20 6e 45 71 3d 25 64 5c 6e  ags=%x, nEq=%d\n
1f473 22 2c 0a 20 20 20 20 20 20 20 20 62 65 73 74 49  ",.        bestI
1f474 64 78 20 3f 20 62 65 73 74 49 64 78 2d 3e 7a 4e  dx ? bestIdx->zN
1f475 61 6d 65 20 3a 20 22 28 6e 6f 6e 65 29 22 2c 20  ame : "(none)", 
1f476 6c 6f 77 65 73 74 43 6f 73 74 2c 20 62 65 73 74  lowestCost, best
1f477 46 6c 61 67 73 2c 20 62 65 73 74 4e 45 71 29 29  Flags, bestNEq))
1f478 3b 0a 20 20 2a 70 46 6c 61 67 73 20 3d 20 62 65  ;.  *pFlags = be
1f479 73 74 46 6c 61 67 73 20 7c 20 65 71 54 65 72 6d  stFlags | eqTerm
1f47a 4d 61 73 6b 3b 0a 20 20 2a 70 6e 45 71 20 3d 20  Mask;.  *pnEq = 
1f47b 62 65 73 74 4e 45 71 3b 0a 20 20 72 65 74 75 72  bestNEq;.  retur
1f47c 6e 20 6c 6f 77 65 73 74 43 6f 73 74 3b 0a 7d 0a  n lowestCost;.}.
1f47d 0a 0a 2f 2a 0a 2a 2a 20 44 69 73 61 62 6c 65 20  ../*.** Disable 
1f47e 61 20 74 65 72 6d 20 69 6e 20 74 68 65 20 57 48  a term in the WH
1f47f 45 52 45 20 63 6c 61 75 73 65 2e 20 20 45 78 63  ERE clause.  Exc
1f480 65 70 74 2c 20 64 6f 20 6e 6f 74 20 64 69 73 61  ept, do not disa
1f481 62 6c 65 20 74 68 65 20 74 65 72 6d 0a 2a 2a 20  ble the term.** 
1f482 69 66 20 69 74 20 63 6f 6e 74 72 6f 6c 73 20 61  if it controls a
1f483 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e   LEFT OUTER JOIN
1f484 20 61 6e 64 20 69 74 20 64 69 64 20 6e 6f 74 20   and it did not 
1f485 6f 72 69 67 69 6e 61 74 65 20 69 6e 20 74 68 65  originate in the
1f486 20 4f 4e 0a 2a 2a 20 6f 72 20 55 53 49 4e 47 20   ON.** or USING 
1f487 63 6c 61 75 73 65 20 6f 66 20 74 68 61 74 20 6a  clause of that j
1f488 6f 69 6e 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 73 69  oin..**.** Consi
1f489 64 65 72 20 74 68 65 20 74 65 72 6d 20 74 32 2e  der the term t2.
1f48a 7a 3d 27 6f 6b 27 20 69 6e 20 74 68 65 20 66 6f  z='ok' in the fo
1f48b 6c 6c 6f 77 69 6e 67 20 71 75 65 72 69 65 73 3a  llowing queries:
1f48c 0a 2a 2a 0a 2a 2a 20 20 20 28 31 29 20 20 53 45  .**.**   (1)  SE
1f48d 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 4c  LECT * FROM t1 L
1f48e 45 46 54 20 4a 4f 49 4e 20 74 32 20 4f 4e 20 74  EFT JOIN t2 ON t
1f48f 31 2e 61 3d 74 32 2e 78 20 57 48 45 52 45 20 74  1.a=t2.x WHERE t
1f490 32 2e 7a 3d 27 6f 6b 27 0a 2a 2a 20 20 20 28 32  2.z='ok'.**   (2
1f491 29 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d  )  SELECT * FROM
1f492 20 74 31 20 4c 45 46 54 20 4a 4f 49 4e 20 74 32   t1 LEFT JOIN t2
1f493 20 4f 4e 20 74 31 2e 61 3d 74 32 2e 78 20 41 4e   ON t1.a=t2.x AN
1f494 44 20 74 32 2e 7a 3d 27 6f 6b 27 0a 2a 2a 20 20  D t2.z='ok'.**  
1f495 20 28 33 29 20 20 53 45 4c 45 43 54 20 2a 20 46   (3)  SELECT * F
1f496 52 4f 4d 20 74 31 2c 20 74 32 20 57 48 45 52 45  ROM t1, t2 WHERE
1f497 20 74 31 2e 61 3d 74 32 2e 78 20 41 4e 44 20 74   t1.a=t2.x AND t
1f498 32 2e 7a 3d 27 6f 6b 27 0a 2a 2a 0a 2a 2a 20 54  2.z='ok'.**.** T
1f499 68 65 20 74 32 2e 7a 3d 27 6f 6b 27 20 69 73 20  he t2.z='ok' is 
1f49a 64 69 73 61 62 6c 65 64 20 69 6e 20 74 68 65 20  disabled in the 
1f49b 69 6e 20 28 32 29 20 62 65 63 61 75 73 65 20 69  in (2) because i
1f49c 74 20 6f 72 69 67 69 6e 61 74 65 73 0a 2a 2a 20  t originates.** 
1f49d 69 6e 20 74 68 65 20 4f 4e 20 63 6c 61 75 73 65  in the ON clause
1f49e 2e 20 20 54 68 65 20 74 65 72 6d 20 69 73 20 64  .  The term is d
1f49f 69 73 61 62 6c 65 64 20 69 6e 20 28 33 29 20 62  isabled in (3) b
1f4a0 65 63 61 75 73 65 20 69 74 20 69 73 20 6e 6f 74  ecause it is not
1f4a1 20 70 61 72 74 0a 2a 2a 20 6f 66 20 61 20 4c 45   part.** of a LE
1f4a2 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 2e 20 20  FT OUTER JOIN.  
1f4a3 49 6e 20 28 31 29 2c 20 74 68 65 20 74 65 72 6d  In (1), the term
1f4a4 20 69 73 20 6e 6f 74 20 64 69 73 61 62 6c 65 64   is not disabled
1f4a5 2e 0a 2a 2a 0a 2a 2a 20 44 69 73 61 62 6c 69 6e  ..**.** Disablin
1f4a6 67 20 61 20 74 65 72 6d 20 63 61 75 73 65 73 20  g a term causes 
1f4a7 74 68 61 74 20 74 65 72 6d 20 74 6f 20 6e 6f 74  that term to not
1f4a8 20 62 65 20 74 65 73 74 65 64 20 69 6e 20 74 68   be tested in th
1f4a9 65 20 69 6e 6e 65 72 20 6c 6f 6f 70 0a 2a 2a 20  e inner loop.** 
1f4aa 6f 66 20 74 68 65 20 6a 6f 69 6e 2e 20 20 44 69  of the join.  Di
1f4ab 73 61 62 6c 69 6e 67 20 69 73 20 61 6e 20 6f 70  sabling is an op
1f4ac 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 57 68 65  timization.  Whe
1f4ad 6e 20 74 65 72 6d 73 20 61 72 65 20 73 61 74 69  n terms are sati
1f4ae 73 66 69 65 64 0a 2a 2a 20 62 79 20 69 6e 64 69  sfied.** by indi
1f4af 63 65 73 2c 20 77 65 20 64 69 73 61 62 6c 65 20  ces, we disable 
1f4b0 74 68 65 6d 20 74 6f 20 70 72 65 76 65 6e 74 20  them to prevent 
1f4b1 72 65 64 75 6e 64 61 6e 74 20 74 65 73 74 73 20  redundant tests 
1f4b2 69 6e 20 74 68 65 20 69 6e 6e 65 72 0a 2a 2a 20  in the inner.** 
1f4b3 6c 6f 6f 70 2e 20 20 57 65 20 77 6f 75 6c 64 20  loop.  We would 
1f4b4 67 65 74 20 74 68 65 20 63 6f 72 72 65 63 74 20  get the correct 
1f4b5 72 65 73 75 6c 74 73 20 69 66 20 6e 6f 74 68 69  results if nothi
1f4b6 6e 67 20 77 65 72 65 20 65 76 65 72 20 64 69 73  ng were ever dis
1f4b7 61 62 6c 65 64 2c 0a 2a 2a 20 62 75 74 20 6a 6f  abled,.** but jo
1f4b8 69 6e 73 20 6d 69 67 68 74 20 72 75 6e 20 61 20  ins might run a 
1f4b9 6c 69 74 74 6c 65 20 73 6c 6f 77 65 72 2e 20 20  little slower.  
1f4ba 54 68 65 20 74 72 69 63 6b 20 69 73 20 74 6f 20  The trick is to 
1f4bb 64 69 73 61 62 6c 65 20 61 73 20 6d 75 63 68 0a  disable as much.
1f4bc 2a 2a 20 61 73 20 77 65 20 63 61 6e 20 77 69 74  ** as we can wit
1f4bd 68 6f 75 74 20 64 69 73 61 62 6c 69 6e 67 20 74  hout disabling t
1f4be 6f 6f 20 6d 75 63 68 2e 20 20 49 66 20 77 65 20  oo much.  If we 
1f4bf 64 69 73 61 62 6c 65 64 20 69 6e 20 28 31 29 2c  disabled in (1),
1f4c0 20 77 65 27 64 20 67 65 74 0a 2a 2a 20 74 68 65   we'd get.** the
1f4c1 20 77 72 6f 6e 67 20 61 6e 73 77 65 72 2e 20 20   wrong answer.  
1f4c2 53 65 65 20 74 69 63 6b 65 74 20 23 38 31 33 2e  See ticket #813.
1f4c3 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1f4c4 64 69 73 61 62 6c 65 54 65 72 6d 28 57 68 65 72  disableTerm(Wher
1f4c5 65 4c 65 76 65 6c 20 2a 70 4c 65 76 65 6c 2c 20  eLevel *pLevel, 
1f4c6 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d  WhereTerm *pTerm
1f4c7 29 7b 0a 20 20 69 66 28 20 70 54 65 72 6d 0a 20  ){.  if( pTerm. 
1f4c8 20 20 20 20 20 26 26 20 28 70 54 65 72 6d 2d 3e       && (pTerm->
1f4c9 66 6c 61 67 73 20 26 20 54 45 52 4d 5f 43 4f 44  flags & TERM_COD
1f4ca 45 44 29 3d 3d 30 0a 20 20 20 20 20 20 26 26 20  ED)==0.      && 
1f4cb 28 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f  (pLevel->iLeftJo
1f4cc 69 6e 3d 3d 30 20 7c 7c 20 45 78 70 72 48 61 73  in==0 || ExprHas
1f4cd 50 72 6f 70 65 72 74 79 28 70 54 65 72 6d 2d 3e  Property(pTerm->
1f4ce 70 45 78 70 72 2c 20 45 50 5f 46 72 6f 6d 4a 6f  pExpr, EP_FromJo
1f4cf 69 6e 29 29 0a 20 20 29 7b 0a 20 20 20 20 70 54  in)).  ){.    pT
1f4d0 65 72 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 54 45  erm->flags |= TE
1f4d1 52 4d 5f 43 4f 44 45 44 3b 0a 20 20 20 20 69 66  RM_CODED;.    if
1f4d2 28 20 70 54 65 72 6d 2d 3e 69 50 61 72 65 6e 74  ( pTerm->iParent
1f4d3 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 57 68 65  >=0 ){.      Whe
1f4d4 72 65 54 65 72 6d 20 2a 70 4f 74 68 65 72 20 3d  reTerm *pOther =
1f4d5 20 26 70 54 65 72 6d 2d 3e 70 57 43 2d 3e 61 5b   &pTerm->pWC->a[
1f4d6 70 54 65 72 6d 2d 3e 69 50 61 72 65 6e 74 5d 3b  pTerm->iParent];
1f4d7 0a 20 20 20 20 20 20 69 66 28 20 28 2d 2d 70 4f  .      if( (--pO
1f4d8 74 68 65 72 2d 3e 6e 43 68 69 6c 64 29 3d 3d 30  ther->nChild)==0
1f4d9 20 29 7b 0a 20 20 20 20 20 20 20 20 64 69 73 61   ){.        disa
1f4da 62 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c 20  bleTerm(pLevel, 
1f4db 70 4f 74 68 65 72 29 3b 0a 20 20 20 20 20 20 7d  pOther);.      }
1f4dc 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
1f4dd 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
1f4de 65 20 74 68 61 74 20 62 75 69 6c 64 73 20 61 20  e that builds a 
1f4df 70 72 6f 62 65 20 66 6f 72 20 61 6e 20 69 6e 64  probe for an ind
1f4e0 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20  ex..**.** There 
1f4e1 73 68 6f 75 6c 64 20 62 65 20 6e 43 6f 6c 75 6d  should be nColum
1f4e2 6e 20 76 61 6c 75 65 73 20 6f 6e 20 74 68 65 20  n values on the 
1f4e3 73 74 61 63 6b 2e 20 20 54 68 65 20 69 6e 64 65  stack.  The inde
1f4e4 78 0a 2a 2a 20 74 6f 20 62 65 20 70 72 6f 62 65  x.** to be probe
1f4e5 64 20 69 73 20 70 49 64 78 2e 20 20 50 6f 70 20  d is pIdx.  Pop 
1f4e6 74 68 65 20 76 61 6c 75 65 73 20 66 72 6f 6d 20  the values from 
1f4e7 74 68 65 20 73 74 61 63 6b 20 61 6e 64 0a 2a 2a  the stack and.**
1f4e8 20 72 65 70 6c 61 63 65 20 74 68 65 6d 20 61 6c   replace them al
1f4e9 6c 20 77 69 74 68 20 61 20 73 69 6e 67 6c 65 20  l with a single 
1f4ea 72 65 63 6f 72 64 20 74 68 61 74 20 69 73 20 74  record that is t
1f4eb 68 65 20 69 6e 64 65 78 0a 2a 2a 20 70 72 6f 62  he index.** prob
1f4ec 6c 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  lem..*/.static v
1f4ed 6f 69 64 20 62 75 69 6c 64 49 6e 64 65 78 50 72  oid buildIndexPr
1f4ee 6f 62 65 28 0a 20 20 56 64 62 65 20 2a 76 2c 20  obe(.  Vdbe *v, 
1f4ef 20 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61         /* Genera
1f4f0 74 65 20 63 6f 64 65 20 69 6e 74 6f 20 74 68 69  te code into thi
1f4f1 73 20 56 4d 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  s VM */.  int nC
1f4f2 6f 6c 75 6d 6e 2c 20 20 20 20 2f 2a 20 54 68 65  olumn,    /* The
1f4f3 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
1f4f4 6e 73 20 74 6f 20 63 68 65 63 6b 20 66 6f 72 20  ns to check for 
1f4f5 4e 55 4c 4c 20 2a 2f 0a 20 20 49 6e 64 65 78 20  NULL */.  Index 
1f4f6 2a 70 49 64 78 20 20 20 20 20 2f 2a 20 49 6e 64  *pIdx     /* Ind
1f4f7 65 78 20 74 68 61 74 20 77 65 20 77 69 6c 6c 20  ex that we will 
1f4f8 62 65 20 73 65 61 72 63 68 69 6e 67 20 2a 2f 0a  be searching */.
1f4f9 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  ){.  sqlite3Vdbe
1f4fa 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 61 6b 65  AddOp(v, OP_Make
1f4fb 52 65 63 6f 72 64 2c 20 6e 43 6f 6c 75 6d 6e 2c  Record, nColumn,
1f4fc 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 49 6e   0);.  sqlite3In
1f4fd 64 65 78 41 66 66 69 6e 69 74 79 53 74 72 28 76  dexAffinityStr(v
1f4fe 2c 20 70 49 64 78 29 3b 0a 7d 0a 0a 0a 2f 2a 0a  , pIdx);.}.../*.
1f4ff 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
1f500 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 65 71   for a single eq
1f501 75 61 6c 69 74 79 20 74 65 72 6d 20 6f 66 20 74  uality term of t
1f502 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e  he WHERE clause.
1f503 20 20 41 6e 20 65 71 75 61 6c 69 74 79 0a 2a 2a    An equality.**
1f504 20 74 65 72 6d 20 63 61 6e 20 62 65 20 65 69 74   term can be eit
1f505 68 65 72 20 58 3d 65 78 70 72 20 6f 72 20 58 20  her X=expr or X 
1f506 49 4e 20 28 2e 2e 2e 29 2e 20 20 20 70 54 65 72  IN (...).   pTer
1f507 6d 20 69 73 20 74 68 65 20 74 65 72 6d 20 74 6f  m is the term to
1f508 20 62 65 20 0a 2a 2a 20 63 6f 64 65 64 2e 0a 2a   be .** coded..*
1f509 2a 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74  *.** The current
1f50a 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 63   value for the c
1f50b 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 6c 65 66  onstraint is lef
1f50c 74 20 6f 6e 20 74 68 65 20 74 6f 70 20 6f 66 20  t on the top of 
1f50d 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a  the stack..**.**
1f50e 20 46 6f 72 20 61 20 63 6f 6e 73 74 72 61 69 6e   For a constrain
1f50f 74 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 58 3d  t of the form X=
1f510 65 78 70 72 2c 20 74 68 65 20 65 78 70 72 65 73  expr, the expres
1f511 73 69 6f 6e 20 69 73 20 65 76 61 6c 75 61 74 65  sion is evaluate
1f512 64 20 61 6e 64 20 69 74 73 0a 2a 2a 20 72 65 73  d and its.** res
1f513 75 6c 74 20 69 73 20 6c 65 66 74 20 6f 6e 20 74  ult is left on t
1f514 68 65 20 73 74 61 63 6b 2e 20 20 46 6f 72 20 63  he stack.  For c
1f515 6f 6e 73 74 72 61 69 6e 74 73 20 6f 66 20 74 68  onstraints of th
1f516 65 20 66 6f 72 6d 20 58 20 49 4e 20 28 2e 2e 2e  e form X IN (...
1f517 29 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e  ).** this routin
1f518 65 20 73 65 74 73 20 75 70 20 61 20 6c 6f 6f 70  e sets up a loop
1f519 20 74 68 61 74 20 77 69 6c 6c 20 69 74 65 72 61   that will itera
1f51a 74 65 20 6f 76 65 72 20 61 6c 6c 20 76 61 6c 75  te over all valu
1f51b 65 73 20 6f 66 20 58 2e 0a 2a 2f 0a 73 74 61 74  es of X..*/.stat
1f51c 69 63 20 76 6f 69 64 20 63 6f 64 65 45 71 75 61  ic void codeEqua
1f51d 6c 69 74 79 54 65 72 6d 28 0a 20 20 50 61 72 73  lityTerm(.  Pars
1f51e 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
1f51f 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63  /* The parsing c
1f520 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68 65 72  ontext */.  Wher
1f521 65 54 65 72 6d 20 2a 70 54 65 72 6d 2c 20 20 20  eTerm *pTerm,   
1f522 2f 2a 20 54 68 65 20 74 65 72 6d 20 6f 66 20 74  /* The term of t
1f523 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
1f524 74 6f 20 62 65 20 63 6f 64 65 64 20 2a 2f 0a 20  to be coded */. 
1f525 20 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65   WhereLevel *pLe
1f526 76 65 6c 20 20 2f 2a 20 57 68 65 6e 20 6c 65 76  vel  /* When lev
1f527 65 6c 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63  el of the FROM c
1f528 6c 61 75 73 65 20 77 65 20 61 72 65 20 77 6f 72  lause we are wor
1f529 6b 69 6e 67 20 6f 6e 20 2a 2f 0a 29 7b 0a 20 20  king on */.){.  
1f52a 45 78 70 72 20 2a 70 58 20 3d 20 70 54 65 72 6d  Expr *pX = pTerm
1f52b 2d 3e 70 45 78 70 72 3b 0a 20 20 56 64 62 65 20  ->pExpr;.  Vdbe 
1f52c 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
1f52d 62 65 3b 0a 20 20 69 66 28 20 70 58 2d 3e 6f 70  be;.  if( pX->op
1f52e 3d 3d 54 4b 5f 45 51 20 29 7b 0a 20 20 20 20 73  ==TK_EQ ){.    s
1f52f 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
1f530 50 61 72 73 65 2c 20 70 58 2d 3e 70 52 69 67 68  Parse, pX->pRigh
1f531 74 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  t);.  }else if( 
1f532 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c  pX->op==TK_ISNUL
1f533 4c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  L ){.    sqlite3
1f534 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1f535 4e 75 6c 6c 2c 20 30 2c 20 30 29 3b 0a 23 69 66  Null, 0, 0);.#if
1f536 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1f537 5f 53 55 42 51 55 45 52 59 0a 20 20 7d 65 6c 73  _SUBQUERY.  }els
1f538 65 7b 0a 20 20 20 20 69 6e 74 20 69 54 61 62 3b  e{.    int iTab;
1f539 0a 20 20 20 20 73 74 72 75 63 74 20 49 6e 4c 6f  .    struct InLo
1f53a 6f 70 20 2a 70 49 6e 3b 0a 0a 20 20 20 20 61 73  op *pIn;..    as
1f53b 73 65 72 74 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b  sert( pX->op==TK
1f53c 5f 49 4e 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  _IN );.    sqlit
1f53d 65 33 43 6f 64 65 53 75 62 73 65 6c 65 63 74 28  e3CodeSubselect(
1f53e 70 50 61 72 73 65 2c 20 70 58 29 3b 0a 20 20 20  pParse, pX);.   
1f53f 20 69 54 61 62 20 3d 20 70 58 2d 3e 69 54 61 62   iTab = pX->iTab
1f540 6c 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  le;.    sqlite3V
1f541 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 52  dbeAddOp(v, OP_R
1f542 65 77 69 6e 64 2c 20 69 54 61 62 2c 20 30 29 3b  ewind, iTab, 0);
1f543 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74  .    VdbeComment
1f544 28 28 76 2c 20 22 23 20 25 2e 2a 73 22 2c 20 70  ((v, "# %.*s", p
1f545 58 2d 3e 73 70 61 6e 2e 6e 2c 20 70 58 2d 3e 73  X->span.n, pX->s
1f546 70 61 6e 2e 7a 29 29 3b 0a 20 20 20 20 69 66 28  pan.z));.    if(
1f547 20 70 4c 65 76 65 6c 2d 3e 6e 49 6e 3d 3d 30 20   pLevel->nIn==0 
1f548 29 7b 0a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d  ){.      pLevel-
1f549 3e 6e 78 74 20 3d 20 73 71 6c 69 74 65 33 56 64  >nxt = sqlite3Vd
1f54a 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
1f54b 20 20 20 20 7d 0a 20 20 20 20 70 4c 65 76 65 6c      }.    pLevel
1f54c 2d 3e 6e 49 6e 2b 2b 3b 0a 20 20 20 20 70 4c 65  ->nIn++;.    pLe
1f54d 76 65 6c 2d 3e 61 49 6e 4c 6f 6f 70 20 3d 20 73  vel->aInLoop = s
1f54e 71 6c 69 74 65 52 65 61 6c 6c 6f 63 4f 72 46 72  qliteReallocOrFr
1f54f 65 65 28 70 4c 65 76 65 6c 2d 3e 61 49 6e 4c 6f  ee(pLevel->aInLo
1f550 6f 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  op,.            
1f551 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f552 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 70          sizeof(p
1f553 4c 65 76 65 6c 2d 3e 61 49 6e 4c 6f 6f 70 5b 30  Level->aInLoop[0
1f554 5d 29 2a 70 4c 65 76 65 6c 2d 3e 6e 49 6e 29 3b  ])*pLevel->nIn);
1f555 0a 20 20 20 20 70 49 6e 20 3d 20 70 4c 65 76 65  .    pIn = pLeve
1f556 6c 2d 3e 61 49 6e 4c 6f 6f 70 3b 0a 20 20 20 20  l->aInLoop;.    
1f557 69 66 28 20 70 49 6e 20 29 7b 0a 20 20 20 20 20  if( pIn ){.     
1f558 20 70 49 6e 20 2b 3d 20 70 4c 65 76 65 6c 2d 3e   pIn += pLevel->
1f559 6e 49 6e 20 2d 20 31 3b 0a 20 20 20 20 20 20 70  nIn - 1;.      p
1f55a 49 6e 2d 3e 69 43 75 72 20 3d 20 69 54 61 62 3b  In->iCur = iTab;
1f55b 0a 20 20 20 20 20 20 70 49 6e 2d 3e 74 6f 70 41  .      pIn->topA
1f55c 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
1f55d 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6f 6c  eAddOp(v, OP_Col
1f55e 75 6d 6e 2c 20 69 54 61 62 2c 20 30 29 3b 0a 20  umn, iTab, 0);. 
1f55f 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1f560 41 64 64 4f 70 28 76 2c 20 4f 50 5f 49 73 4e 75  AddOp(v, OP_IsNu
1f561 6c 6c 2c 20 2d 31 2c 20 30 29 3b 0a 20 20 20 20  ll, -1, 0);.    
1f562 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 4c 65  }else{.      pLe
1f563 76 65 6c 2d 3e 6e 49 6e 20 3d 20 30 3b 0a 20 20  vel->nIn = 0;.  
1f564 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20    }.#endif.  }. 
1f565 20 64 69 73 61 62 6c 65 54 65 72 6d 28 70 4c 65   disableTerm(pLe
1f566 76 65 6c 2c 20 70 54 65 72 6d 29 3b 0a 7d 0a 0a  vel, pTerm);.}..
1f567 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
1f568 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 65 76  ode that will ev
1f569 61 6c 75 61 74 65 20 61 6c 6c 20 3d 3d 20 61 6e  aluate all == an
1f56a 64 20 49 4e 20 63 6f 6e 73 74 72 61 69 6e 74 73  d IN constraints
1f56b 20 66 6f 72 20 61 6e 0a 2a 2a 20 69 6e 64 65 78   for an.** index
1f56c 2e 20 20 54 68 65 20 76 61 6c 75 65 73 20 66 6f  .  The values fo
1f56d 72 20 61 6c 6c 20 63 6f 6e 73 74 72 61 69 6e 74  r all constraint
1f56e 73 20 61 72 65 20 6c 65 66 74 20 6f 6e 20 74 68  s are left on th
1f56f 65 20 73 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46  e stack..**.** F
1f570 6f 72 20 65 78 61 6d 70 6c 65 2c 20 63 6f 6e 73  or example, cons
1f571 69 64 65 72 20 74 61 62 6c 65 20 74 31 28 61 2c  ider table t1(a,
1f572 62 2c 63 2c 64 2c 65 2c 66 29 20 77 69 74 68 20  b,c,d,e,f) with 
1f573 69 6e 64 65 78 20 69 31 28 61 2c 62 2c 63 29 2e  index i1(a,b,c).
1f574 0a 2a 2a 20 53 75 70 70 6f 73 65 20 74 68 65 20  .** Suppose the 
1f575 57 48 45 52 45 20 63 6c 61 75 73 65 20 69 73 20  WHERE clause is 
1f576 74 68 69 73 3a 20 20 61 3d 3d 35 20 41 4e 44 20  this:  a==5 AND 
1f577 62 20 49 4e 20 28 31 2c 32 2c 33 29 20 41 4e 44  b IN (1,2,3) AND
1f578 20 63 3e 35 20 41 4e 44 20 63 3c 31 30 0a 2a 2a   c>5 AND c<10.**
1f579 20 54 68 65 20 69 6e 64 65 78 20 68 61 73 20 61   The index has a
1f57a 73 20 6d 61 6e 79 20 61 73 20 74 68 72 65 65 20  s many as three 
1f57b 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61  equality constra
1f57c 69 6e 74 73 2c 20 62 75 74 20 69 6e 20 74 68 69  ints, but in thi
1f57d 73 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 74 68  s.** example, th
1f57e 65 20 74 68 69 72 64 20 22 63 22 20 76 61 6c 75  e third "c" valu
1f57f 65 20 69 73 20 61 6e 20 69 6e 65 71 75 61 6c 69  e is an inequali
1f580 74 79 2e 20 20 53 6f 20 6f 6e 6c 79 20 74 77 6f  ty.  So only two
1f581 20 0a 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 73   .** constraints
1f582 20 61 72 65 20 63 6f 64 65 64 2e 20 20 54 68 69   are coded.  Thi
1f583 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 67  s routine will g
1f584 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
1f585 65 76 61 6c 75 61 74 65 0a 2a 2a 20 61 3d 3d 35  evaluate.** a==5
1f586 20 61 6e 64 20 62 20 49 4e 20 28 31 2c 32 2c 33   and b IN (1,2,3
1f587 29 2e 20 20 54 68 65 20 63 75 72 72 65 6e 74 20  ).  The current 
1f588 76 61 6c 75 65 73 20 66 6f 72 20 61 20 61 6e 64  values for a and
1f589 20 62 20 77 69 6c 6c 20 62 65 20 6c 65 66 74 0a   b will be left.
1f58a 2a 2a 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 20  ** on the stack 
1f58b 2d 20 61 20 69 73 20 74 68 65 20 64 65 65 70 65  - a is the deepe
1f58c 73 74 20 61 6e 64 20 62 20 74 68 65 20 73 68 61  st and b the sha
1f58d 6c 6c 6f 77 65 73 74 2e 0a 2a 2a 0a 2a 2a 20 49  llowest..**.** I
1f58e 6e 20 74 68 65 20 65 78 61 6d 70 6c 65 20 61 62  n the example ab
1f58f 6f 76 65 20 6e 45 71 3d 3d 32 2e 20 20 42 75 74  ove nEq==2.  But
1f590 20 74 68 69 73 20 73 75 62 72 6f 75 74 69 6e 65   this subroutine
1f591 20 77 6f 72 6b 73 20 66 6f 72 20 61 6e 79 20 76   works for any v
1f592 61 6c 75 65 0a 2a 2a 20 6f 66 20 6e 45 71 20 69  alue.** of nEq i
1f593 6e 63 6c 75 64 69 6e 67 20 30 2e 20 20 49 66 20  ncluding 0.  If 
1f594 6e 45 71 3d 3d 30 2c 20 74 68 69 73 20 72 6f 75  nEq==0, this rou
1f595 74 69 6e 65 20 69 73 20 6e 65 61 72 6c 79 20 61  tine is nearly a
1f596 20 6e 6f 2d 6f 70 2e 0a 2a 2a 20 54 68 65 20 6f   no-op..** The o
1f597 6e 6c 79 20 74 68 69 6e 67 20 69 74 20 64 6f 65  nly thing it doe
1f598 73 20 69 73 20 61 6c 6c 6f 63 61 74 65 20 74 68  s is allocate th
1f599 65 20 70 4c 65 76 65 6c 2d 3e 69 4d 65 6d 20 6d  e pLevel->iMem m
1f59a 65 6d 6f 72 79 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a  emory cell..**.*
1f59b 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61  * This routine a
1f59c 6c 77 61 79 73 20 61 6c 6c 6f 63 61 74 65 73 20  lways allocates 
1f59d 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 6d 65 6d  at least one mem
1f59e 6f 72 79 20 63 65 6c 6c 20 61 6e 64 20 70 75 74  ory cell and put
1f59f 73 0a 2a 2a 20 74 68 65 20 61 64 64 72 65 73 73  s.** the address
1f5a0 20 6f 66 20 74 68 61 74 20 6d 65 6d 6f 72 79 20   of that memory 
1f5a1 63 65 6c 6c 20 69 6e 20 70 4c 65 76 65 6c 2d 3e  cell in pLevel->
1f5a2 69 4d 65 6d 2e 20 20 54 68 65 20 63 6f 64 65 20  iMem.  The code 
1f5a3 74 68 61 74 0a 2a 2a 20 63 61 6c 6c 73 20 74 68  that.** calls th
1f5a4 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  is routine will 
1f5a5 75 73 65 20 70 4c 65 76 65 6c 2d 3e 69 4d 65 6d  use pLevel->iMem
1f5a6 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 74 65   to store the te
1f5a7 72 6d 69 6e 61 74 69 6f 6e 0a 2a 2a 20 6b 65 79  rmination.** key
1f5a8 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6c 6f   value of the lo
1f5a9 6f 70 2e 20 20 49 66 20 6f 6e 65 20 6f 72 20 6d  op.  If one or m
1f5aa 6f 72 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 73  ore IN operators
1f5ab 20 61 70 70 65 61 72 2c 20 74 68 65 6e 0a 2a 2a   appear, then.**
1f5ac 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c   this routine al
1f5ad 6c 6f 63 61 74 65 73 20 61 6e 20 61 64 64 69 74  locates an addit
1f5ae 69 6f 6e 61 6c 20 6e 45 71 20 6d 65 6d 6f 72 79  ional nEq memory
1f5af 20 63 65 6c 6c 73 20 66 6f 72 20 69 6e 74 65 72   cells for inter
1f5b0 6e 61 6c 0a 2a 2a 20 75 73 65 2e 0a 2a 2f 0a 73  nal.** use..*/.s
1f5b1 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65 41  tatic void codeA
1f5b2 6c 6c 45 71 75 61 6c 69 74 79 54 65 72 6d 73 28  llEqualityTerms(
1f5b3 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
1f5b4 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73  ,        /* Pars
1f5b5 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
1f5b6 20 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65   WhereLevel *pLe
1f5b7 76 65 6c 2c 20 20 20 2f 2a 20 57 68 69 63 68 20  vel,   /* Which 
1f5b8 6e 65 73 74 65 64 20 6c 6f 6f 70 20 6f 66 20 74  nested loop of t
1f5b9 68 65 20 46 52 4f 4d 20 77 65 20 61 72 65 20 63  he FROM we are c
1f5ba 6f 64 69 6e 67 20 2a 2f 0a 20 20 57 68 65 72 65  oding */.  Where
1f5bb 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 20 20 20  Clause *pWC,    
1f5bc 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c   /* The WHERE cl
1f5bd 61 75 73 65 20 2a 2f 0a 20 20 42 69 74 6d 61 73  ause */.  Bitmas
1f5be 6b 20 6e 6f 74 52 65 61 64 79 20 20 20 20 20 20  k notReady      
1f5bf 2f 2a 20 57 68 69 63 68 20 70 61 72 74 73 20 6f  /* Which parts o
1f5c0 66 20 46 52 4f 4d 20 68 61 76 65 20 6e 6f 74 20  f FROM have not 
1f5c1 79 65 74 20 62 65 65 6e 20 63 6f 64 65 64 20 2a  yet been coded *
1f5c2 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 45 71 20 3d  /.){.  int nEq =
1f5c3 20 70 4c 65 76 65 6c 2d 3e 6e 45 71 3b 20 20 20   pLevel->nEq;   
1f5c4 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 75 6d 62       /* The numb
1f5c5 65 72 20 6f 66 20 3d 3d 20 6f 72 20 49 4e 20 63  er of == or IN c
1f5c6 6f 6e 73 74 72 61 69 6e 74 73 20 74 6f 20 63 6f  onstraints to co
1f5c7 64 65 20 2a 2f 0a 20 20 69 6e 74 20 74 65 72 6d  de */.  int term
1f5c8 73 49 6e 4d 65 6d 20 3d 20 30 3b 20 20 20 20 20  sInMem = 0;     
1f5c9 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65        /* If true
1f5ca 2c 20 73 74 6f 72 65 20 76 61 6c 75 65 20 69 6e  , store value in
1f5cb 20 6d 65 6d 5b 5d 20 63 65 6c 6c 73 20 2a 2f 0a   mem[] cells */.
1f5cc 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
1f5cd 73 65 2d 3e 70 56 64 62 65 3b 20 20 20 20 20 20  se->pVdbe;      
1f5ce 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 6d  /* The virtual m
1f5cf 61 63 68 69 6e 65 20 75 6e 64 65 72 20 63 6f 6e  achine under con
1f5d0 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 49  struction */.  I
1f5d1 6e 64 65 78 20 2a 70 49 64 78 20 3d 20 70 4c 65  ndex *pIdx = pLe
1f5d2 76 65 6c 2d 3e 70 49 64 78 3b 20 20 20 2f 2a 20  vel->pIdx;   /* 
1f5d3 54 68 65 20 69 6e 64 65 78 20 62 65 69 6e 67 20  The index being 
1f5d4 75 73 65 64 20 66 6f 72 20 74 68 69 73 20 6c 6f  used for this lo
1f5d5 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72  op */.  int iCur
1f5d6 20 3d 20 70 4c 65 76 65 6c 2d 3e 69 54 61 62 43   = pLevel->iTabC
1f5d7 75 72 3b 20 20 20 2f 2a 20 54 68 65 20 63 75 72  ur;   /* The cur
1f5d8 73 6f 72 20 6f 66 20 74 68 65 20 74 61 62 6c 65  sor of the table
1f5d9 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72 6d 20   */.  WhereTerm 
1f5da 2a 70 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20  *pTerm;         
1f5db 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20      /* A single 
1f5dc 63 6f 6e 73 74 72 61 69 6e 74 20 74 65 72 6d 20  constraint term 
1f5dd 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20 20 20 20 20  */.  int j;     
1f5de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f5df 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
1f5e0 65 72 20 2a 2f 0a 0a 20 20 2f 2a 20 46 69 67 75  er */..  /* Figu
1f5e1 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20  re out how many 
1f5e2 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 77 65 20  memory cells we 
1f5e3 77 69 6c 6c 20 6e 65 65 64 20 74 68 65 6e 20 61  will need then a
1f5e4 6c 6c 6f 63 61 74 65 20 74 68 65 6d 2e 0a 20 20  llocate them..  
1f5e5 2a 2a 20 57 65 20 61 6c 77 61 79 73 20 6e 65 65  ** We always nee
1f5e6 64 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 75  d at least one u
1f5e7 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65  sed to store the
1f5e8 20 6c 6f 6f 70 20 74 65 72 6d 69 6e 61 74 6f 72   loop terminator
1f5e9 0a 20 20 2a 2a 20 76 61 6c 75 65 2e 20 20 49 66  .  ** value.  If
1f5ea 20 74 68 65 72 65 20 61 72 65 20 49 4e 20 6f 70   there are IN op
1f5eb 65 72 61 74 6f 72 73 20 77 65 27 6c 6c 20 6e 65  erators we'll ne
1f5ec 65 64 20 6f 6e 65 20 66 6f 72 20 65 61 63 68 20  ed one for each 
1f5ed 3d 3d 20 6f 72 0a 20 20 2a 2a 20 49 4e 20 63 6f  == or.  ** IN co
1f5ee 6e 73 74 72 61 69 6e 74 2e 0a 20 20 2a 2f 0a 20  nstraint..  */. 
1f5ef 20 70 4c 65 76 65 6c 2d 3e 69 4d 65 6d 20 3d 20   pLevel->iMem = 
1f5f0 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 2b 3b 0a  pParse->nMem++;.
1f5f1 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 66 6c    if( pLevel->fl
1f5f2 61 67 73 20 26 20 57 48 45 52 45 5f 43 4f 4c 55  ags & WHERE_COLU
1f5f3 4d 4e 5f 49 4e 20 29 7b 0a 20 20 20 20 70 50 61  MN_IN ){.    pPa
1f5f4 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 4c 65  rse->nMem += pLe
1f5f5 76 65 6c 2d 3e 6e 45 71 3b 0a 20 20 20 20 74 65  vel->nEq;.    te
1f5f6 72 6d 73 49 6e 4d 65 6d 20 3d 20 31 3b 0a 20 20  rmsInMem = 1;.  
1f5f7 7d 0a 0a 20 20 2f 2a 20 45 76 61 6c 75 61 74 65  }..  /* Evaluate
1f5f8 20 74 68 65 20 65 71 75 61 6c 69 74 79 20 63 6f   the equality co
1f5f9 6e 73 74 72 61 69 6e 74 73 0a 20 20 2a 2f 0a 20  nstraints.  */. 
1f5fa 20 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e 6e   assert( pIdx->n
1f5fb 43 6f 6c 75 6d 6e 3e 3d 6e 45 71 20 29 3b 0a 20  Column>=nEq );. 
1f5fc 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 45 71 3b   for(j=0; j<nEq;
1f5fd 20 6a 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6b   j++){.    int k
1f5fe 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d   = pIdx->aiColum
1f5ff 6e 5b 6a 5d 3b 0a 20 20 20 20 70 54 65 72 6d 20  n[j];.    pTerm 
1f600 3d 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20  = findTerm(pWC, 
1f601 69 43 75 72 2c 20 6b 2c 20 6e 6f 74 52 65 61 64  iCur, k, notRead
1f602 79 2c 20 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73  y, pLevel->flags
1f603 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 69 66 28  , pIdx);.    if(
1f604 20 70 54 65 72 6d 3d 3d 30 20 29 20 62 72 65 61   pTerm==0 ) brea
1f605 6b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  k;.    assert( (
1f606 70 54 65 72 6d 2d 3e 66 6c 61 67 73 20 26 20 54  pTerm->flags & T
1f607 45 52 4d 5f 43 4f 44 45 44 29 3d 3d 30 20 29 3b  ERM_CODED)==0 );
1f608 0a 20 20 20 20 63 6f 64 65 45 71 75 61 6c 69 74  .    codeEqualit
1f609 79 54 65 72 6d 28 70 50 61 72 73 65 2c 20 70 54  yTerm(pParse, pT
1f60a 65 72 6d 2c 20 70 4c 65 76 65 6c 29 3b 0a 20 20  erm, pLevel);.  
1f60b 20 20 69 66 28 20 28 70 54 65 72 6d 2d 3e 65 4f    if( (pTerm->eO
1f60c 70 65 72 61 74 6f 72 20 26 20 28 57 4f 5f 49 53  perator & (WO_IS
1f60d 4e 55 4c 4c 7c 57 4f 5f 49 4e 29 29 3d 3d 30 20  NULL|WO_IN))==0 
1f60e 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1f60f 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1f610 49 73 4e 75 6c 6c 2c 20 74 65 72 6d 73 49 6e 4d  IsNull, termsInM
1f611 65 6d 20 3f 20 2d 31 20 3a 20 2d 28 6a 2b 31 29  em ? -1 : -(j+1)
1f612 2c 20 70 4c 65 76 65 6c 2d 3e 62 72 6b 29 3b 0a  , pLevel->brk);.
1f613 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 74 65      }.    if( te
1f614 72 6d 73 49 6e 4d 65 6d 20 29 7b 0a 20 20 20 20  rmsInMem ){.    
1f615 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1f616 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 53 74 6f 72  Op(v, OP_MemStor
1f617 65 2c 20 70 4c 65 76 65 6c 2d 3e 69 4d 65 6d 2b  e, pLevel->iMem+
1f618 6a 2b 31 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 20  j+1, 1);.    }. 
1f619 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75   }..  /* Make su
1f61a 72 65 20 61 6c 6c 20 74 68 65 20 63 6f 6e 73 74  re all the const
1f61b 72 61 69 6e 74 20 76 61 6c 75 65 73 20 61 72 65  raint values are
1f61c 20 6f 6e 20 74 68 65 20 74 6f 70 20 6f 66 20 74   on the top of t
1f61d 68 65 20 73 74 61 63 6b 0a 20 20 2a 2f 0a 20 20  he stack.  */.  
1f61e 69 66 28 20 74 65 72 6d 73 49 6e 4d 65 6d 20 29  if( termsInMem )
1f61f 7b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a  {.    for(j=0; j
1f620 3c 6e 45 71 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20  <nEq; j++){.    
1f621 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1f622 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 4c 6f 61 64  Op(v, OP_MemLoad
1f623 2c 20 70 4c 65 76 65 6c 2d 3e 69 4d 65 6d 2b 6a  , pLevel->iMem+j
1f624 2b 31 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20  +1, 0);.    }.  
1f625 7d 0a 7d 0a 0a 23 69 66 20 64 65 66 69 6e 65 64  }.}..#if defined
1f626 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 2f 2a  (SQLITE_TEST)./*
1f627 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
1f628 67 20 76 61 72 69 61 62 6c 65 20 68 6f 6c 64 73  g variable holds
1f629 20 61 20 74 65 78 74 20 64 65 73 63 72 69 70 74   a text descript
1f62a 69 6f 6e 20 6f 66 20 71 75 65 72 79 20 70 6c 61  ion of query pla
1f62b 6e 20 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 62  n generated.** b
1f62c 79 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  y the most recen
1f62d 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  t call to sqlite
1f62e 33 57 68 65 72 65 42 65 67 69 6e 28 29 2e 20 20  3WhereBegin().  
1f62f 45 61 63 68 20 63 61 6c 6c 20 74 6f 20 57 68 65  Each call to Whe
1f630 72 65 42 65 67 69 6e 0a 2a 2a 20 6f 76 65 72 77  reBegin.** overw
1f631 72 69 74 65 73 20 74 68 65 20 70 72 65 76 69 6f  rites the previo
1f632 75 73 2e 20 20 54 68 69 73 20 69 6e 66 6f 72 6d  us.  This inform
1f633 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 66 6f  ation is used fo
1f634 72 20 74 65 73 74 69 6e 67 20 61 6e 64 0a 2a 2a  r testing and.**
1f635 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 2e 0a   analysis only..
1f636 2a 2f 0a 63 68 61 72 20 73 71 6c 69 74 65 33 5f  */.char sqlite3_
1f637 71 75 65 72 79 5f 70 6c 61 6e 5b 42 4d 53 2a 32  query_plan[BMS*2
1f638 2a 34 30 5d 3b 20 20 2f 2a 20 54 65 78 74 20 6f  *40];  /* Text o
1f639 66 20 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a 73 74  f the join */.st
1f63a 61 74 69 63 20 69 6e 74 20 6e 51 50 6c 61 6e 20  atic int nQPlan 
1f63b 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
1f63c 20 20 2f 2a 20 4e 65 78 74 20 66 72 65 65 20 73    /* Next free s
1f63d 6c 6f 77 20 69 6e 20 5f 71 75 65 72 79 5f 70 6c  low in _query_pl
1f63e 61 6e 5b 5d 20 2a 2f 0a 0a 23 65 6e 64 69 66 20  an[] */..#endif 
1f63f 2f 2a 20 53 51 4c 49 54 45 5f 54 45 53 54 20 2a  /* SQLITE_TEST *
1f640 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61  /.../*.** Free a
1f641 20 57 68 65 72 65 49 6e 66 6f 20 73 74 72 75 63   WhereInfo struc
1f642 74 75 72 65 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ture.*/.static v
1f643 6f 69 64 20 77 68 65 72 65 49 6e 66 6f 46 72 65  oid whereInfoFre
1f644 65 28 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49  e(WhereInfo *pWI
1f645 6e 66 6f 29 7b 0a 20 20 69 66 28 20 70 57 49 6e  nfo){.  if( pWIn
1f646 66 6f 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  fo ){.    int i;
1f647 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
1f648 70 57 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c 3b 20  pWInfo->nLevel; 
1f649 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  i++){.      sqli
1f64a 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a  te3_index_info *
1f64b 70 49 6e 66 6f 20 3d 20 70 57 49 6e 66 6f 2d 3e  pInfo = pWInfo->
1f64c 61 5b 69 5d 2e 70 49 64 78 49 6e 66 6f 3b 0a 20  a[i].pIdxInfo;. 
1f64d 20 20 20 20 20 69 66 28 20 70 49 6e 66 6f 20 29       if( pInfo )
1f64e 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 49  {.        if( pI
1f64f 6e 66 6f 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49  nfo->needToFreeI
1f650 64 78 53 74 72 20 29 7b 0a 20 20 20 20 20 20 20  dxStr ){.       
1f651 20 20 20 2f 2a 20 43 6f 76 65 72 61 67 65 3a 20     /* Coverage: 
1f652 44 6f 6e 27 74 20 74 68 69 6e 6b 20 74 68 69 73  Don't think this
1f653 20 63 61 6e 20 62 65 20 72 65 61 63 68 65 64 2e   can be reached.
1f654 20 42 79 20 74 68 65 20 74 69 6d 65 20 74 68 69   By the time thi
1f655 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66  s.          ** f
1f656 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
1f657 64 2c 20 74 68 65 20 69 6e 64 65 78 2d 73 74 72  d, the index-str
1f658 69 6e 67 73 20 68 61 76 65 20 62 65 65 6e 20 70  ings have been p
1f659 61 73 73 65 64 0a 20 20 20 20 20 20 20 20 20 20  assed.          
1f65a 2a 2a 20 74 6f 20 74 68 65 20 76 64 62 65 20 6c  ** to the vdbe l
1f65b 61 79 65 72 20 66 6f 72 20 64 65 6c 65 74 69 6f  ayer for deletio
1f65c 6e 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  n..          */.
1f65d 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1f65e 33 5f 66 72 65 65 28 70 49 6e 66 6f 2d 3e 69 64  3_free(pInfo->id
1f65f 78 53 74 72 29 3b 0a 20 20 20 20 20 20 20 20 7d  xStr);.        }
1f660 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 46  .        sqliteF
1f661 72 65 65 28 70 49 6e 66 6f 29 3b 0a 20 20 20 20  ree(pInfo);.    
1f662 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71    }.    }.    sq
1f663 6c 69 74 65 46 72 65 65 28 70 57 49 6e 66 6f 29  liteFree(pWInfo)
1f664 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  ;.  }.}.../*.** 
1f665 47 65 6e 65 72 61 74 65 20 74 68 65 20 62 65 67  Generate the beg
1f666 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 6c 6f  inning of the lo
1f667 6f 70 20 75 73 65 64 20 66 6f 72 20 57 48 45 52  op used for WHER
1f668 45 20 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73  E clause process
1f669 69 6e 67 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75  ing..** The retu
1f66a 72 6e 20 76 61 6c 75 65 20 69 73 20 61 20 70 6f  rn value is a po
1f66b 69 6e 74 65 72 20 74 6f 20 61 6e 20 6f 70 61 71  inter to an opaq
1f66c 75 65 20 73 74 72 75 63 74 75 72 65 20 74 68 61  ue structure tha
1f66d 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 69 6e  t contains.** in
1f66e 66 6f 72 6d 61 74 69 6f 6e 20 6e 65 65 64 65 64  formation needed
1f66f 20 74 6f 20 74 65 72 6d 69 6e 61 74 65 20 74 68   to terminate th
1f670 65 20 6c 6f 6f 70 2e 20 20 4c 61 74 65 72 2c 20  e loop.  Later, 
1f671 74 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74  the calling rout
1f672 69 6e 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 69 6e  ine.** should in
1f673 76 6f 6b 65 20 73 71 6c 69 74 65 33 57 68 65 72  voke sqlite3Wher
1f674 65 45 6e 64 28 29 20 77 69 74 68 20 74 68 65 20  eEnd() with the 
1f675 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20  return value of 
1f676 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a  this function.**
1f677 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 6f 6d   in order to com
1f678 70 6c 65 74 65 20 74 68 65 20 57 48 45 52 45 20  plete the WHERE 
1f679 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e  clause processin
1f67a 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65  g..**.** If an e
1f67b 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 69  rror occurs, thi
1f67c 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  s routine return
1f67d 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68  s NULL..**.** Th
1f67e 65 20 62 61 73 69 63 20 69 64 65 61 20 69 73 20  e basic idea is 
1f67f 74 6f 20 64 6f 20 61 20 6e 65 73 74 65 64 20 6c  to do a nested l
1f680 6f 6f 70 2c 20 6f 6e 65 20 6c 6f 6f 70 20 66 6f  oop, one loop fo
1f681 72 20 65 61 63 68 20 74 61 62 6c 65 20 69 6e 0a  r each table in.
1f682 2a 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  ** the FROM clau
1f683 73 65 20 6f 66 20 61 20 73 65 6c 65 63 74 2e 20  se of a select. 
1f684 20 28 49 4e 53 45 52 54 20 61 6e 64 20 55 50 44   (INSERT and UPD
1f685 41 54 45 20 73 74 61 74 65 6d 65 6e 74 73 20 61  ATE statements a
1f686 72 65 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 61  re the.** same a
1f687 73 20 61 20 53 45 4c 45 43 54 20 77 69 74 68 20  s a SELECT with 
1f688 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 74 61  only a single ta
1f689 62 6c 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  ble in the FROM 
1f68a 63 6c 61 75 73 65 2e 29 20 20 46 6f 72 0a 2a 2a  clause.)  For.**
1f68b 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65   example, if the
1f68c 20 53 51 4c 20 69 73 20 74 68 69 73 3a 0a 2a 2a   SQL is this:.**
1f68d 0a 2a 2a 20 20 20 20 20 20 20 53 45 4c 45 43 54  .**       SELECT
1f68e 20 2a 20 46 52 4f 4d 20 74 31 2c 20 74 32 2c 20   * FROM t1, t2, 
1f68f 74 33 20 57 48 45 52 45 20 2e 2e 2e 3b 0a 2a 2a  t3 WHERE ...;.**
1f690 0a 2a 2a 20 54 68 65 6e 20 74 68 65 20 63 6f 64  .** Then the cod
1f691 65 20 67 65 6e 65 72 61 74 65 64 20 69 73 20 63  e generated is c
1f692 6f 6e 63 65 70 74 75 61 6c 6c 79 20 6c 69 6b 65  onceptually like
1f693 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
1f694 2a 2a 0a 2a 2a 20 20 20 20 20 20 66 6f 72 65 61  **.**      forea
1f695 63 68 20 72 6f 77 31 20 69 6e 20 74 31 20 64 6f  ch row1 in t1 do
1f696 20 20 20 20 20 20 20 5c 20 20 20 20 43 6f 64 65         \    Code
1f697 20 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 20 20   generated.**   
1f698 20 20 20 20 20 66 6f 72 65 61 63 68 20 72 6f 77       foreach row
1f699 32 20 69 6e 20 74 32 20 64 6f 20 20 20 20 20 20  2 in t2 do      
1f69a 7c 2d 2d 20 62 79 20 73 71 6c 69 74 65 33 57 68  |-- by sqlite3Wh
1f69b 65 72 65 42 65 67 69 6e 28 29 0a 2a 2a 20 20 20  ereBegin().**   
1f69c 20 20 20 20 20 20 20 66 6f 72 65 61 63 68 20 72         foreach r
1f69d 6f 77 33 20 69 6e 20 74 33 20 64 6f 20 20 20 2f  ow3 in t3 do   /
1f69e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 2e  .**            .
1f69f 2e 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 65  ...**          e
1f6a0 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20  nd              
1f6a1 20 20 20 20 20 20 20 5c 20 20 20 20 43 6f 64 65         \    Code
1f6a2 20 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 20 20   generated.**   
1f6a3 20 20 20 20 20 65 6e 64 20 20 20 20 20 20 20 20       end        
1f6a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f6a5 7c 2d 2d 20 62 79 20 73 71 6c 69 74 65 33 57 68  |-- by sqlite3Wh
1f6a6 65 72 65 45 6e 64 28 29 0a 2a 2a 20 20 20 20 20  ereEnd().**     
1f6a7 20 65 6e 64 20 20 20 20 20 20 20 20 20 20 20 20   end            
1f6a8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 0a 2a               /.*
1f6a9 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74  *.** Note that t
1f6aa 68 65 20 6c 6f 6f 70 73 20 6d 69 67 68 74 20 6e  he loops might n
1f6ab 6f 74 20 62 65 20 6e 65 73 74 65 64 20 69 6e 20  ot be nested in 
1f6ac 74 68 65 20 6f 72 64 65 72 20 69 6e 20 77 68 69  the order in whi
1f6ad 63 68 20 74 68 65 79 0a 2a 2a 20 61 70 70 65 61  ch they.** appea
1f6ae 72 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  r in the FROM cl
1f6af 61 75 73 65 20 69 66 20 61 20 64 69 66 66 65 72  ause if a differ
1f6b0 65 6e 74 20 6f 72 64 65 72 20 69 73 20 62 65 74  ent order is bet
1f6b1 74 65 72 20 61 62 6c 65 20 74 6f 20 6d 61 6b 65  ter able to make
1f6b2 0a 2a 2a 20 75 73 65 20 6f 66 20 69 6e 64 69 63  .** use of indic
1f6b3 65 73 2e 20 20 4e 6f 74 65 20 61 6c 73 6f 20 74  es.  Note also t
1f6b4 68 61 74 20 77 68 65 6e 20 74 68 65 20 49 4e 20  hat when the IN 
1f6b5 6f 70 65 72 61 74 6f 72 20 61 70 70 65 61 72 73  operator appears
1f6b6 20 69 6e 0a 2a 2a 20 74 68 65 20 57 48 45 52 45   in.** the WHERE
1f6b7 20 63 6c 61 75 73 65 2c 20 69 74 20 6d 69 67 68   clause, it migh
1f6b8 74 20 72 65 73 75 6c 74 20 69 6e 20 61 64 64 69  t result in addi
1f6b9 74 69 6f 6e 61 6c 20 6e 65 73 74 65 64 20 6c 6f  tional nested lo
1f6ba 6f 70 73 20 66 6f 72 0a 2a 2a 20 73 63 61 6e 6e  ops for.** scann
1f6bb 69 6e 67 20 74 68 72 6f 75 67 68 20 61 6c 6c 20  ing through all 
1f6bc 76 61 6c 75 65 73 20 6f 6e 20 74 68 65 20 72 69  values on the ri
1f6bd 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66  ght-hand side of
1f6be 20 74 68 65 20 49 4e 2e 0a 2a 2a 0a 2a 2a 20 54   the IN..**.** T
1f6bf 68 65 72 65 20 61 72 65 20 42 74 72 65 65 20 63  here are Btree c
1f6c0 75 72 73 6f 72 73 20 61 73 73 6f 63 69 61 74 65  ursors associate
1f6c1 64 20 77 69 74 68 20 65 61 63 68 20 74 61 62 6c  d with each tabl
1f6c2 65 2e 20 20 74 31 20 75 73 65 73 20 63 75 72 73  e.  t1 uses curs
1f6c3 6f 72 0a 2a 2a 20 6e 75 6d 62 65 72 20 70 54 61  or.** number pTa
1f6c4 62 4c 69 73 74 2d 3e 61 5b 30 5d 2e 69 43 75 72  bList->a[0].iCur
1f6c5 73 6f 72 2e 20 20 74 32 20 75 73 65 73 20 74 68  sor.  t2 uses th
1f6c6 65 20 63 75 72 73 6f 72 20 70 54 61 62 4c 69 73  e cursor pTabLis
1f6c7 74 2d 3e 61 5b 31 5d 2e 69 43 75 72 73 6f 72 2e  t->a[1].iCursor.
1f6c8 0a 2a 2a 20 41 6e 64 20 73 6f 20 66 6f 72 74 68  .** And so forth
1f6c9 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
1f6ca 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20 74  generates code t
1f6cb 6f 20 6f 70 65 6e 20 74 68 6f 73 65 20 56 44 42  o open those VDB
1f6cc 45 20 63 75 72 73 6f 72 73 0a 2a 2a 20 61 6e 64  E cursors.** and
1f6cd 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64   sqlite3WhereEnd
1f6ce 28 29 20 67 65 6e 65 72 61 74 65 73 20 74 68 65  () generates the
1f6cf 20 63 6f 64 65 20 74 6f 20 63 6c 6f 73 65 20 74   code to close t
1f6d0 68 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  hem..**.** The c
1f6d1 6f 64 65 20 74 68 61 74 20 73 71 6c 69 74 65 33  ode that sqlite3
1f6d2 57 68 65 72 65 42 65 67 69 6e 28 29 20 67 65 6e  WhereBegin() gen
1f6d3 65 72 61 74 65 73 20 6c 65 61 76 65 73 20 74 68  erates leaves th
1f6d4 65 20 63 75 72 73 6f 72 73 20 6e 61 6d 65 64 0a  e cursors named.
1f6d5 2a 2a 20 69 6e 20 70 54 61 62 4c 69 73 74 20 70  ** in pTabList p
1f6d6 6f 69 6e 74 69 6e 67 20 61 74 20 74 68 65 69 72  ointing at their
1f6d7 20 61 70 70 72 6f 70 72 69 61 74 65 20 65 6e 74   appropriate ent
1f6d8 72 69 65 73 2e 20 20 54 68 65 20 5b 2e 2e 2e 5d  ries.  The [...]
1f6d9 20 63 6f 64 65 0a 2a 2a 20 63 61 6e 20 75 73 65   code.** can use
1f6da 20 4f 50 5f 43 6f 6c 75 6d 6e 20 61 6e 64 20 4f   OP_Column and O
1f6db 50 5f 52 6f 77 69 64 20 6f 70 63 6f 64 65 73 20  P_Rowid opcodes 
1f6dc 6f 6e 20 74 68 65 73 65 20 63 75 72 73 6f 72 73  on these cursors
1f6dd 20 74 6f 20 65 78 74 72 61 63 74 0a 2a 2a 20 64   to extract.** d
1f6de 61 74 61 20 66 72 6f 6d 20 74 68 65 20 76 61 72  ata from the var
1f6df 69 6f 75 73 20 74 61 62 6c 65 73 20 6f 66 20 74  ious tables of t
1f6e0 68 65 20 6c 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49  he loop..**.** I
1f6e1 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  f the WHERE clau
1f6e2 73 65 20 69 73 20 65 6d 70 74 79 2c 20 74 68 65  se is empty, the
1f6e3 20 66 6f 72 65 61 63 68 20 6c 6f 6f 70 73 20 6d   foreach loops m
1f6e4 75 73 74 20 65 61 63 68 20 73 63 61 6e 20 74 68  ust each scan th
1f6e5 65 69 72 0a 2a 2a 20 65 6e 74 69 72 65 20 74 61  eir.** entire ta
1f6e6 62 6c 65 73 2e 20 20 54 68 75 73 20 61 20 74 68  bles.  Thus a th
1f6e7 72 65 65 2d 77 61 79 20 6a 6f 69 6e 20 69 73 20  ree-way join is 
1f6e8 61 6e 20 4f 28 4e 5e 33 29 20 6f 70 65 72 61 74  an O(N^3) operat
1f6e9 69 6f 6e 2e 20 20 42 75 74 20 69 66 0a 2a 2a 20  ion.  But if.** 
1f6ea 74 68 65 20 74 61 62 6c 65 73 20 68 61 76 65 20  the tables have 
1f6eb 69 6e 64 69 63 65 73 20 61 6e 64 20 74 68 65 72  indices and ther
1f6ec 65 20 61 72 65 20 74 65 72 6d 73 20 69 6e 20 74  e are terms in t
1f6ed 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
1f6ee 74 68 61 74 0a 2a 2a 20 72 65 66 65 72 20 74 6f  that.** refer to
1f6ef 20 74 68 6f 73 65 20 69 6e 64 69 63 65 73 2c 20   those indices, 
1f6f0 61 20 63 6f 6d 70 6c 65 74 65 20 74 61 62 6c 65  a complete table
1f6f1 20 73 63 61 6e 20 63 61 6e 20 62 65 20 61 76 6f   scan can be avo
1f6f2 69 64 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20  ided and the.** 
1f6f3 63 6f 64 65 20 77 69 6c 6c 20 72 75 6e 20 6d 75  code will run mu
1f6f4 63 68 20 66 61 73 74 65 72 2e 20 20 4d 6f 73 74  ch faster.  Most
1f6f5 20 6f 66 20 74 68 65 20 77 6f 72 6b 20 6f 66 20   of the work of 
1f6f6 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
1f6f7 63 68 65 63 6b 69 6e 67 0a 2a 2a 20 74 6f 20 73  checking.** to s
1f6f8 65 65 20 69 66 20 74 68 65 72 65 20 61 72 65 20  ee if there are 
1f6f9 69 6e 64 69 63 65 73 20 74 68 61 74 20 63 61 6e  indices that can
1f6fa 20 62 65 20 75 73 65 64 20 74 6f 20 73 70 65 65   be used to spee
1f6fb 64 20 75 70 20 74 68 65 20 6c 6f 6f 70 2e 0a 2a  d up the loop..*
1f6fc 2a 0a 2a 2a 20 54 65 72 6d 73 20 6f 66 20 74 68  *.** Terms of th
1f6fd 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 61  e WHERE clause a
1f6fe 72 65 20 61 6c 73 6f 20 75 73 65 64 20 74 6f 20  re also used to 
1f6ff 6c 69 6d 69 74 20 77 68 69 63 68 20 72 6f 77 73  limit which rows
1f700 20 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 6d 61 6b   actually.** mak
1f701 65 20 69 74 20 74 6f 20 74 68 65 20 22 2e 2e 2e  e it to the "...
1f702 22 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  " in the middle 
1f703 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 20 20 41 66  of the loop.  Af
1f704 74 65 72 20 65 61 63 68 20 22 66 6f 72 65 61 63  ter each "foreac
1f705 68 22 2c 0a 2a 2a 20 74 65 72 6d 73 20 6f 66 20  h",.** terms of 
1f706 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
1f707 20 74 68 61 74 20 75 73 65 20 6f 6e 6c 79 20 74   that use only t
1f708 65 72 6d 73 20 69 6e 20 74 68 61 74 20 6c 6f 6f  erms in that loo
1f709 70 20 61 6e 64 20 6f 75 74 65 72 0a 2a 2a 20 6c  p and outer.** l
1f70a 6f 6f 70 73 20 61 72 65 20 65 76 61 6c 75 61 74  oops are evaluat
1f70b 65 64 20 61 6e 64 20 69 66 20 66 61 6c 73 65 20  ed and if false 
1f70c 61 20 6a 75 6d 70 20 69 73 20 6d 61 64 65 20 61  a jump is made a
1f70d 72 6f 75 6e 64 20 61 6c 6c 20 73 75 62 73 65 71  round all subseq
1f70e 75 65 6e 74 0a 2a 2a 20 69 6e 6e 65 72 20 6c 6f  uent.** inner lo
1f70f 6f 70 73 20 28 6f 72 20 61 72 6f 75 6e 64 20 74  ops (or around t
1f710 68 65 20 22 2e 2e 2e 22 20 69 66 20 74 68 65 20  he "..." if the 
1f711 74 65 73 74 20 6f 63 63 75 72 73 20 77 69 74 68  test occurs with
1f712 69 6e 20 74 68 65 20 69 6e 6e 65 72 2d 0a 2a 2a  in the inner-.**
1f713 20 6d 6f 73 74 20 6c 6f 6f 70 29 0a 2a 2a 0a 2a   most loop).**.*
1f714 2a 20 4f 55 54 45 52 20 4a 4f 49 4e 53 0a 2a 2a  * OUTER JOINS.**
1f715 0a 2a 2a 20 41 6e 20 6f 75 74 65 72 20 6a 6f 69  .** An outer joi
1f716 6e 20 6f 66 20 74 61 62 6c 65 73 20 74 31 20 61  n of tables t1 a
1f717 6e 64 20 74 32 20 69 73 20 63 6f 6e 63 65 70 74  nd t2 is concept
1f718 61 6c 6c 79 20 63 6f 64 65 64 20 61 73 20 66 6f  ally coded as fo
1f719 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  llows:.**.**    
1f71a 66 6f 72 65 61 63 68 20 72 6f 77 31 20 69 6e 20  foreach row1 in 
1f71b 74 31 20 64 6f 0a 2a 2a 20 20 20 20 20 20 66 6c  t1 do.**      fl
1f71c 61 67 20 3d 20 30 0a 2a 2a 20 20 20 20 20 20 66  ag = 0.**      f
1f71d 6f 72 65 61 63 68 20 72 6f 77 32 20 69 6e 20 74  oreach row2 in t
1f71e 32 20 64 6f 0a 2a 2a 20 20 20 20 20 20 20 20 73  2 do.**        s
1f71f 74 61 72 74 3a 0a 2a 2a 20 20 20 20 20 20 20 20  tart:.**        
1f720 20 20 2e 2e 2e 0a 2a 2a 20 20 20 20 20 20 20 20    ....**        
1f721 20 20 66 6c 61 67 20 3d 20 31 0a 2a 2a 20 20 20    flag = 1.**   
1f722 20 20 20 65 6e 64 0a 2a 2a 20 20 20 20 20 20 69     end.**      i
1f723 66 20 66 6c 61 67 3d 3d 30 20 74 68 65 6e 0a 2a  f flag==0 then.*
1f724 2a 20 20 20 20 20 20 20 20 6d 6f 76 65 20 74 68  *        move th
1f725 65 20 72 6f 77 32 20 63 75 72 73 6f 72 20 74 6f  e row2 cursor to
1f726 20 61 20 6e 75 6c 6c 20 72 6f 77 0a 2a 2a 20 20   a null row.**  
1f727 20 20 20 20 20 20 67 6f 74 6f 20 73 74 61 72 74        goto start
1f728 0a 2a 2a 20 20 20 20 20 20 66 69 0a 2a 2a 20 20  .**      fi.**  
1f729 20 20 65 6e 64 0a 2a 2a 0a 2a 2a 20 4f 52 44 45    end.**.** ORDE
1f72a 52 20 42 59 20 43 4c 41 55 53 45 20 50 52 4f 43  R BY CLAUSE PROC
1f72b 45 53 53 49 4e 47 0a 2a 2a 0a 2a 2a 20 2a 70 70  ESSING.**.** *pp
1f72c 4f 72 64 65 72 42 79 20 69 73 20 61 20 70 6f 69  OrderBy is a poi
1f72d 6e 74 65 72 20 74 6f 20 74 68 65 20 4f 52 44 45  nter to the ORDE
1f72e 52 20 42 59 20 63 6c 61 75 73 65 20 6f 66 20 61  R BY clause of a
1f72f 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
1f730 74 2c 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 69  t,.** if there i
1f731 73 20 6f 6e 65 2e 20 20 49 66 20 74 68 65 72 65  s one.  If there
1f732 20 69 73 20 6e 6f 20 4f 52 44 45 52 20 42 59 20   is no ORDER BY 
1f733 63 6c 61 75 73 65 20 6f 72 20 69 66 20 74 68 69  clause or if thi
1f734 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20  s routine.** is 
1f735 63 61 6c 6c 65 64 20 66 72 6f 6d 20 61 6e 20 55  called from an U
1f736 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 20  PDATE or DELETE 
1f737 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20  statement, then 
1f738 70 70 4f 72 64 65 72 42 79 20 69 73 20 4e 55 4c  ppOrderBy is NUL
1f739 4c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 69  L..**.** If an i
1f73a 6e 64 65 78 20 63 61 6e 20 62 65 20 75 73 65 64  ndex can be used
1f73b 20 73 6f 20 74 68 61 74 20 74 68 65 20 6e 61 74   so that the nat
1f73c 75 72 61 6c 20 6f 75 74 70 75 74 20 6f 72 64 65  ural output orde
1f73d 72 20 6f 66 20 74 68 65 20 74 61 62 6c 65 0a 2a  r of the table.*
1f73e 2a 20 73 63 61 6e 20 69 73 20 63 6f 72 72 65 63  * scan is correc
1f73f 74 20 66 6f 72 20 74 68 65 20 4f 52 44 45 52 20  t for the ORDER 
1f740 42 59 20 63 6c 61 75 73 65 2c 20 74 68 65 6e 20  BY clause, then 
1f741 74 68 61 74 20 69 6e 64 65 78 20 69 73 20 75 73  that index is us
1f742 65 64 20 61 6e 64 0a 2a 2a 20 2a 70 70 4f 72 64  ed and.** *ppOrd
1f743 65 72 42 79 20 69 73 20 73 65 74 20 74 6f 20 4e  erBy is set to N
1f744 55 4c 4c 2e 20 20 54 68 69 73 20 69 73 20 61 6e  ULL.  This is an
1f745 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 74 68   optimization th
1f746 61 74 20 70 72 65 76 65 6e 74 73 20 61 6e 0a 2a  at prevents an.*
1f747 2a 20 75 6e 6e 65 63 65 73 73 61 72 79 20 73 6f  * unnecessary so
1f748 72 74 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  rt of the result
1f749 20 73 65 74 20 69 66 20 61 6e 20 69 6e 64 65 78   set if an index
1f74a 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72   appropriate for
1f74b 20 74 68 65 0a 2a 2a 20 4f 52 44 45 52 20 42 59   the.** ORDER BY
1f74c 20 63 6c 61 75 73 65 20 61 6c 72 65 61 64 79 20   clause already 
1f74d 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a 2a 20 49 66  exists..**.** If
1f74e 20 74 68 65 20 77 68 65 72 65 20 63 6c 61 75 73   the where claus
1f74f 65 20 6c 6f 6f 70 73 20 63 61 6e 6e 6f 74 20 62  e loops cannot b
1f750 65 20 61 72 72 61 6e 67 65 64 20 74 6f 20 70 72  e arranged to pr
1f751 6f 76 69 64 65 20 74 68 65 20 63 6f 72 72 65 63  ovide the correc
1f752 74 0a 2a 2a 20 6f 75 74 70 75 74 20 6f 72 64 65  t.** output orde
1f753 72 2c 20 74 68 65 6e 20 74 68 65 20 2a 70 70 4f  r, then the *ppO
1f754 72 64 65 72 42 79 20 69 73 20 75 6e 63 68 61 6e  rderBy is unchan
1f755 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ged..*/.SQLITE_P
1f756 52 49 56 41 54 45 20 57 68 65 72 65 49 6e 66 6f  RIVATE WhereInfo
1f757 20 2a 73 71 6c 69 74 65 33 57 68 65 72 65 42 65   *sqlite3WhereBe
1f758 67 69 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50  gin(.  Parse *pP
1f759 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  arse,        /* 
1f75a 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65  The parser conte
1f75b 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20  xt */.  SrcList 
1f75c 2a 70 54 61 62 4c 69 73 74 2c 20 20 20 20 2f 2a  *pTabList,    /*
1f75d 20 41 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 74   A list of all t
1f75e 61 62 6c 65 73 20 74 6f 20 62 65 20 73 63 61 6e  ables to be scan
1f75f 6e 65 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ned */.  Expr *p
1f760 57 68 65 72 65 2c 20 20 20 20 20 20 20 20 20 2f  Where,         /
1f761 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75  * The WHERE clau
1f762 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  se */.  ExprList
1f763 20 2a 2a 70 70 4f 72 64 65 72 42 79 20 20 2f 2a   **ppOrderBy  /*
1f764 20 41 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61   An ORDER BY cla
1f765 75 73 65 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a  use, or NULL */.
1f766 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  ){.  int i;     
1f767 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f768 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
1f769 2a 2f 0a 20 20 57 68 65 72 65 49 6e 66 6f 20 2a  */.  WhereInfo *
1f76a 70 57 49 6e 66 6f 3b 20 20 20 20 20 20 20 20 20  pWInfo;         
1f76b 2f 2a 20 57 69 6c 6c 20 62 65 63 6f 6d 65 20 74  /* Will become t
1f76c 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
1f76d 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  of this function
1f76e 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 20 3d 20   */.  Vdbe *v = 
1f76f 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 20  pParse->pVdbe;  
1f770 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20   /* The virtual 
1f771 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65 20  database engine 
1f772 2a 2f 0a 20 20 69 6e 74 20 62 72 6b 2c 20 63 6f  */.  int brk, co
1f773 6e 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  nt = 0;         
1f774 2f 2a 20 41 64 64 72 65 73 73 65 73 20 75 73 65  /* Addresses use
1f775 64 20 64 75 72 69 6e 67 20 63 6f 64 65 20 67 65  d during code ge
1f776 6e 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 42 69  neration */.  Bi
1f777 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64 79 3b 20  tmask notReady; 
1f778 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73           /* Curs
1f779 6f 72 73 20 74 68 61 74 20 61 72 65 20 6e 6f 74  ors that are not
1f77a 20 79 65 74 20 70 6f 73 69 74 69 6f 6e 65 64 20   yet positioned 
1f77b 2a 2f 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a  */.  WhereTerm *
1f77c 70 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20  pTerm;          
1f77d 2f 2a 20 41 20 73 69 6e 67 6c 65 20 74 65 72 6d  /* A single term
1f77e 20 69 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c   in the WHERE cl
1f77f 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4d 61  ause */.  ExprMa
1f780 73 6b 53 65 74 20 6d 61 73 6b 53 65 74 3b 20 20  skSet maskSet;  
1f781 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72       /* The expr
1f782 65 73 73 69 6f 6e 20 6d 61 73 6b 20 73 65 74 20  ession mask set 
1f783 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65  */.  WhereClause
1f784 20 77 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   wc;            
1f785 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61  /* The WHERE cla
1f786 75 73 65 20 69 73 20 64 69 76 69 64 65 64 20 69  use is divided i
1f787 6e 74 6f 20 74 68 65 73 65 20 74 65 72 6d 73 20  nto these terms 
1f788 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c  */.  struct SrcL
1f789 69 73 74 5f 69 74 65 6d 20 2a 70 54 61 62 49 74  ist_item *pTabIt
1f78a 65 6d 3b 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65  em;  /* A single
1f78b 20 65 6e 74 72 79 20 66 72 6f 6d 20 70 54 61 62   entry from pTab
1f78c 4c 69 73 74 20 2a 2f 0a 20 20 57 68 65 72 65 4c  List */.  WhereL
1f78d 65 76 65 6c 20 2a 70 4c 65 76 65 6c 3b 20 20 20  evel *pLevel;   
1f78e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73            /* A s
1f78f 69 6e 67 6c 65 20 6c 65 76 65 6c 20 69 6e 20 74  ingle level in t
1f790 68 65 20 70 57 49 6e 66 6f 20 6c 69 73 74 20 2a  he pWInfo list *
1f791 2f 0a 20 20 69 6e 74 20 69 46 72 6f 6d 3b 20 20  /.  int iFrom;  
1f792 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f793 20 20 20 20 2f 2a 20 46 69 72 73 74 20 75 6e 75      /* First unu
1f794 73 65 64 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  sed FROM clause 
1f795 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74  element */.  int
1f796 20 61 6e 64 46 6c 61 67 73 3b 20 20 20 20 20 20   andFlags;      
1f797 20 20 20 20 20 20 20 20 2f 2a 20 41 4e 44 2d 65          /* AND-e
1f798 64 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66  d combination of
1f799 20 61 6c 6c 20 77 63 2e 61 5b 5d 2e 66 6c 61 67   all wc.a[].flag
1f79a 73 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 6e  s */..  /* The n
1f79b 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20  umber of tables 
1f79c 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  in the FROM clau
1f79d 73 65 20 69 73 20 6c 69 6d 69 74 65 64 20 62 79  se is limited by
1f79e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 20   the number of. 
1f79f 20 2a 2a 20 62 69 74 73 20 69 6e 20 61 20 42 69   ** bits in a Bi
1f7a0 74 6d 61 73 6b 20 0a 20 20 2a 2f 0a 20 20 69 66  tmask .  */.  if
1f7a1 28 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  ( pTabList->nSrc
1f7a2 3e 42 4d 53 20 29 7b 0a 20 20 20 20 73 71 6c 69  >BMS ){.    sqli
1f7a3 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1f7a4 73 65 2c 20 22 61 74 20 6d 6f 73 74 20 25 64 20  se, "at most %d 
1f7a5 74 61 62 6c 65 73 20 69 6e 20 61 20 6a 6f 69 6e  tables in a join
1f7a6 22 2c 20 42 4d 53 29 3b 0a 20 20 20 20 72 65 74  ", BMS);.    ret
1f7a7 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  urn 0;.  }..  /*
1f7a8 20 53 70 6c 69 74 20 74 68 65 20 57 48 45 52 45   Split the WHERE
1f7a9 20 63 6c 61 75 73 65 20 69 6e 74 6f 20 73 65 70   clause into sep
1f7aa 61 72 61 74 65 20 73 75 62 65 78 70 72 65 73 73  arate subexpress
1f7ab 69 6f 6e 73 20 77 68 65 72 65 20 65 61 63 68 0a  ions where each.
1f7ac 20 20 2a 2a 20 73 75 62 65 78 70 72 65 73 73 69    ** subexpressi
1f7ad 6f 6e 20 69 73 20 73 65 70 61 72 61 74 65 64 20  on is separated 
1f7ae 62 79 20 61 6e 20 41 4e 44 20 6f 70 65 72 61 74  by an AND operat
1f7af 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 6e 69 74 4d  or..  */.  initM
1f7b0 61 73 6b 53 65 74 28 26 6d 61 73 6b 53 65 74 29  askSet(&maskSet)
1f7b1 3b 0a 20 20 77 68 65 72 65 43 6c 61 75 73 65 49  ;.  whereClauseI
1f7b2 6e 69 74 28 26 77 63 2c 20 70 50 61 72 73 65 2c  nit(&wc, pParse,
1f7b3 20 26 6d 61 73 6b 53 65 74 29 3b 0a 20 20 77 68   &maskSet);.  wh
1f7b4 65 72 65 53 70 6c 69 74 28 26 77 63 2c 20 70 57  ereSplit(&wc, pW
1f7b5 68 65 72 65 2c 20 54 4b 5f 41 4e 44 29 3b 0a 20  here, TK_AND);. 
1f7b6 20 20 20 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74     .  /* Allocat
1f7b7 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65  e and initialize
1f7b8 20 74 68 65 20 57 68 65 72 65 49 6e 66 6f 20 73   the WhereInfo s
1f7b9 74 72 75 63 74 75 72 65 20 74 68 61 74 20 77 69  tructure that wi
1f7ba 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 0a 20 20  ll become the.  
1f7bb 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c 75 65 2e  ** return value.
1f7bc 0a 20 20 2a 2f 0a 20 20 70 57 49 6e 66 6f 20 3d  .  */.  pWInfo =
1f7bd 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 20 73   sqliteMalloc( s
1f7be 69 7a 65 6f 66 28 57 68 65 72 65 49 6e 66 6f 29  izeof(WhereInfo)
1f7bf 20 2b 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72   + pTabList->nSr
1f7c0 63 2a 73 69 7a 65 6f 66 28 57 68 65 72 65 4c 65  c*sizeof(WhereLe
1f7c1 76 65 6c 29 29 3b 0a 20 20 69 66 28 20 73 71 6c  vel));.  if( sql
1f7c2 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69 6c 65 64  ite3MallocFailed
1f7c3 28 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 77  () ){.    goto w
1f7c4 68 65 72 65 42 65 67 69 6e 4e 6f 4d 65 6d 3b 0a  hereBeginNoMem;.
1f7c5 20 20 7d 0a 20 20 70 57 49 6e 66 6f 2d 3e 6e 4c    }.  pWInfo->nL
1f7c6 65 76 65 6c 20 3d 20 70 54 61 62 4c 69 73 74 2d  evel = pTabList-
1f7c7 3e 6e 53 72 63 3b 0a 20 20 70 57 49 6e 66 6f 2d  >nSrc;.  pWInfo-
1f7c8 3e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65  >pParse = pParse
1f7c9 3b 0a 20 20 70 57 49 6e 66 6f 2d 3e 70 54 61 62  ;.  pWInfo->pTab
1f7ca 4c 69 73 74 20 3d 20 70 54 61 62 4c 69 73 74 3b  List = pTabList;
1f7cb 0a 20 20 70 57 49 6e 66 6f 2d 3e 69 42 72 65 61  .  pWInfo->iBrea
1f7cc 6b 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  k = sqlite3VdbeM
1f7cd 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 0a 20 20  akeLabel(v);..  
1f7ce 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73 65 3a  /* Special case:
1f7cf 20 61 20 57 48 45 52 45 20 63 6c 61 75 73 65 20   a WHERE clause 
1f7d0 74 68 61 74 20 69 73 20 63 6f 6e 73 74 61 6e 74  that is constant
1f7d1 2e 20 20 45 76 61 6c 75 61 74 65 20 74 68 65 0a  .  Evaluate the.
1f7d2 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20    ** expression 
1f7d3 61 6e 64 20 65 69 74 68 65 72 20 6a 75 6d 70 20  and either jump 
1f7d4 6f 76 65 72 20 61 6c 6c 20 6f 66 20 74 68 65 20  over all of the 
1f7d5 63 6f 64 65 20 6f 72 20 66 61 6c 6c 20 74 68 72  code or fall thr
1f7d6 75 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 57  u..  */.  if( pW
1f7d7 68 65 72 65 20 26 26 20 28 70 54 61 62 4c 69 73  here && (pTabLis
1f7d8 74 2d 3e 6e 53 72 63 3d 3d 30 20 7c 7c 20 73 71  t->nSrc==0 || sq
1f7d9 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74  lite3ExprIsConst
1f7da 61 6e 74 4e 6f 74 4a 6f 69 6e 28 70 57 68 65 72  antNotJoin(pWher
1f7db 65 29 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  e)) ){.    sqlit
1f7dc 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50  e3ExprIfFalse(pP
1f7dd 61 72 73 65 2c 20 70 57 68 65 72 65 2c 20 70 57  arse, pWhere, pW
1f7de 49 6e 66 6f 2d 3e 69 42 72 65 61 6b 2c 20 31 29  Info->iBreak, 1)
1f7df 3b 0a 20 20 20 20 70 57 68 65 72 65 20 3d 20 30  ;.    pWhere = 0
1f7e0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6e 61 6c  ;.  }..  /* Anal
1f7e1 79 7a 65 20 61 6c 6c 20 6f 66 20 74 68 65 20 73  yze all of the s
1f7e2 75 62 65 78 70 72 65 73 73 69 6f 6e 73 2e 20 20  ubexpressions.  
1f7e3 4e 6f 74 65 20 74 68 61 74 20 65 78 70 72 41 6e  Note that exprAn
1f7e4 61 6c 79 7a 65 28 29 20 6d 69 67 68 74 0a 20 20  alyze() might.  
1f7e5 2a 2a 20 61 64 64 20 6e 65 77 20 76 69 72 74 75  ** add new virtu
1f7e6 61 6c 20 74 65 72 6d 73 20 6f 6e 74 6f 20 74 68  al terms onto th
1f7e7 65 20 65 6e 64 20 6f 66 20 74 68 65 20 57 48 45  e end of the WHE
1f7e8 52 45 20 63 6c 61 75 73 65 2e 20 20 57 65 20 64  RE clause.  We d
1f7e9 6f 20 6e 6f 74 0a 20 20 2a 2a 20 77 61 6e 74 20  o not.  ** want 
1f7ea 74 6f 20 61 6e 61 6c 79 7a 65 20 74 68 65 73 65  to analyze these
1f7eb 20 76 69 72 74 75 61 6c 20 74 65 72 6d 73 2c 20   virtual terms, 
1f7ec 73 6f 20 73 74 61 72 74 20 61 6e 61 6c 79 7a 69  so start analyzi
1f7ed 6e 67 20 61 74 20 74 68 65 20 65 6e 64 0a 20 20  ng at the end.  
1f7ee 2a 2a 20 61 6e 64 20 77 6f 72 6b 20 66 6f 72 77  ** and work forw
1f7ef 61 72 64 20 73 6f 20 74 68 61 74 20 74 68 65 20  ard so that the 
1f7f0 61 64 64 65 64 20 76 69 72 74 75 61 6c 20 74 65  added virtual te
1f7f1 72 6d 73 20 61 72 65 20 6e 65 76 65 72 20 70 72  rms are never pr
1f7f2 6f 63 65 73 73 65 64 2e 0a 20 20 2a 2f 0a 20 20  ocessed..  */.  
1f7f3 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 4c  for(i=0; i<pTabL
1f7f4 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b  ist->nSrc; i++){
1f7f5 0a 20 20 20 20 63 72 65 61 74 65 4d 61 73 6b 28  .    createMask(
1f7f6 26 6d 61 73 6b 53 65 74 2c 20 70 54 61 62 4c 69  &maskSet, pTabLi
1f7f7 73 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72  st->a[i].iCursor
1f7f8 29 3b 0a 20 20 7d 0a 20 20 65 78 70 72 41 6e 61  );.  }.  exprAna
1f7f9 6c 79 7a 65 41 6c 6c 28 70 54 61 62 4c 69 73 74  lyzeAll(pTabList
1f7fa 2c 20 26 77 63 29 3b 0a 20 20 69 66 28 20 73 71  , &wc);.  if( sq
1f7fb 6c 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69 6c 65  lite3MallocFaile
1f7fc 64 28 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  d() ){.    goto 
1f7fd 77 68 65 72 65 42 65 67 69 6e 4e 6f 4d 65 6d 3b  whereBeginNoMem;
1f7fe 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 6f 73 65  .  }..  /* Chose
1f7ff 20 74 68 65 20 62 65 73 74 20 69 6e 64 65 78 20   the best index 
1f800 74 6f 20 75 73 65 20 66 6f 72 20 65 61 63 68 20  to use for each 
1f801 74 61 62 6c 65 20 69 6e 20 74 68 65 20 46 52 4f  table in the FRO
1f802 4d 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2a 0a 20  M clause..  **. 
1f803 20 2a 2a 20 54 68 69 73 20 6c 6f 6f 70 20 66 69   ** This loop fi
1f804 6c 6c 73 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f  lls in the follo
1f805 77 69 6e 67 20 66 69 65 6c 64 73 3a 0a 20 20 2a  wing fields:.  *
1f806 2a 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d  *.  **   pWInfo-
1f807 3e 61 5b 5d 2e 70 49 64 78 20 20 20 20 20 20 54  >a[].pIdx      T
1f808 68 65 20 69 6e 64 65 78 20 74 6f 20 75 73 65 20  he index to use 
1f809 66 6f 72 20 74 68 69 73 20 6c 65 76 65 6c 20 6f  for this level o
1f80a 66 20 74 68 65 20 6c 6f 6f 70 2e 0a 20 20 2a 2a  f the loop..  **
1f80b 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d 2e 66     pWInfo->a[].f
1f80c 6c 61 67 73 20 20 20 20 20 57 48 45 52 45 5f 78  lags     WHERE_x
1f80d 78 78 20 66 6c 61 67 73 20 61 73 73 6f 63 69 61  xx flags associa
1f80e 74 65 64 20 77 69 74 68 20 70 49 64 78 0a 20 20  ted with pIdx.  
1f80f 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d  **   pWInfo->a[]
1f810 2e 6e 45 71 20 20 20 20 20 20 20 54 68 65 20 6e  .nEq       The n
1f811 75 6d 62 65 72 20 6f 66 20 3d 3d 20 61 6e 64 20  umber of == and 
1f812 49 4e 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a 20  IN constraints. 
1f813 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b   **   pWInfo->a[
1f814 5d 2e 69 46 72 6f 6d 20 20 20 20 20 57 68 65 6e  ].iFrom     When
1f815 20 74 65 72 6d 20 6f 66 20 74 68 65 20 46 52 4f   term of the FRO
1f816 4d 20 63 6c 61 75 73 65 20 69 73 20 62 65 69 6e  M clause is bein
1f817 67 20 63 6f 64 65 64 0a 20 20 2a 2a 20 20 20 70  g coded.  **   p
1f818 57 49 6e 66 6f 2d 3e 61 5b 5d 2e 69 54 61 62 43  WInfo->a[].iTabC
1f819 75 72 20 20 20 54 68 65 20 56 44 42 45 20 63 75  ur   The VDBE cu
1f81a 72 73 6f 72 20 66 6f 72 20 74 68 65 20 64 61 74  rsor for the dat
1f81b 61 62 61 73 65 20 74 61 62 6c 65 0a 20 20 2a 2a  abase table.  **
1f81c 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d 2e 69     pWInfo->a[].i
1f81d 49 64 78 43 75 72 20 20 20 54 68 65 20 56 44 42  IdxCur   The VDB
1f81e 45 20 63 75 72 73 6f 72 20 66 6f 72 20 74 68 65  E cursor for the
1f81f 20 69 6e 64 65 78 0a 20 20 2a 2a 0a 20 20 2a 2a   index.  **.  **
1f820 20 54 68 69 73 20 6c 6f 6f 70 20 61 6c 73 6f 20   This loop also 
1f821 66 69 67 75 72 65 73 20 6f 75 74 20 74 68 65 20  figures out the 
1f822 6e 65 73 74 69 6e 67 20 6f 72 64 65 72 20 6f 66  nesting order of
1f823 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 46   tables in the F
1f824 52 4f 4d 0a 20 20 2a 2a 20 63 6c 61 75 73 65 2e  ROM.  ** clause.
1f825 0a 20 20 2a 2f 0a 20 20 6e 6f 74 52 65 61 64 79  .  */.  notReady
1f826 20 3d 20 7e 28 42 69 74 6d 61 73 6b 29 30 3b 0a   = ~(Bitmask)0;.
1f827 20 20 70 54 61 62 49 74 65 6d 20 3d 20 70 54 61    pTabItem = pTa
1f828 62 4c 69 73 74 2d 3e 61 3b 0a 20 20 70 4c 65 76  bList->a;.  pLev
1f829 65 6c 20 3d 20 70 57 49 6e 66 6f 2d 3e 61 3b 0a  el = pWInfo->a;.
1f82a 20 20 61 6e 64 46 6c 61 67 73 20 3d 20 7e 30 3b    andFlags = ~0;
1f82b 0a 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22  .  WHERETRACE(("
1f82c 2a 2a 2a 20 4f 70 74 69 6d 69 7a 65 72 20 53 74  *** Optimizer St
1f82d 61 72 74 20 2a 2a 2a 5c 6e 22 29 29 3b 0a 20 20  art ***\n"));.  
1f82e 66 6f 72 28 69 3d 69 46 72 6f 6d 3d 30 2c 20 70  for(i=iFrom=0, p
1f82f 4c 65 76 65 6c 3d 70 57 49 6e 66 6f 2d 3e 61 3b  Level=pWInfo->a;
1f830 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72   i<pTabList->nSr
1f831 63 3b 20 69 2b 2b 2c 20 70 4c 65 76 65 6c 2b 2b  c; i++, pLevel++
1f832 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49  ){.    Index *pI
1f833 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dx;             
1f834 20 20 20 2f 2a 20 49 6e 64 65 78 20 66 6f 72 20     /* Index for 
1f835 46 52 4f 4d 20 74 61 62 6c 65 20 61 74 20 70 54  FROM table at pT
1f836 61 62 49 74 65 6d 20 2a 2f 0a 20 20 20 20 69 6e  abItem */.    in
1f837 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20  t flags;        
1f838 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61            /* Fla
1f839 67 73 20 61 73 73 73 6f 63 69 61 74 65 64 20 77  gs asssociated w
1f83a 69 74 68 20 70 49 64 78 20 2a 2f 0a 20 20 20 20  ith pIdx */.    
1f83b 69 6e 74 20 6e 45 71 3b 20 20 20 20 20 20 20 20  int nEq;        
1f83c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
1f83d 75 6d 62 65 72 20 6f 66 20 3d 3d 20 6f 72 20 49  umber of == or I
1f83e 4e 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f  N constraints */
1f83f 0a 20 20 20 20 64 6f 75 62 6c 65 20 63 6f 73 74  .    double cost
1f840 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1f841 20 2f 2a 20 54 68 65 20 63 6f 73 74 20 66 6f 72   /* The cost for
1f842 20 70 49 64 78 20 2a 2f 0a 20 20 20 20 69 6e 74   pIdx */.    int
1f843 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   j;             
1f844 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20           /* For 
1f845 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 46 52 4f  looping over FRO
1f846 4d 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 20 20  M tables */.    
1f847 49 6e 64 65 78 20 2a 70 42 65 73 74 20 3d 20 30  Index *pBest = 0
1f848 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
1f849 68 65 20 62 65 73 74 20 69 6e 64 65 78 20 73 65  he best index se
1f84a 65 6e 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 20  en so far */.   
1f84b 20 69 6e 74 20 62 65 73 74 46 6c 61 67 73 20 3d   int bestFlags =
1f84c 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20   0;          /* 
1f84d 46 6c 61 67 73 20 61 73 73 6f 63 69 61 74 65 64  Flags associated
1f84e 20 77 69 74 68 20 70 42 65 73 74 20 2a 2f 0a 20   with pBest */. 
1f84f 20 20 20 69 6e 74 20 62 65 73 74 4e 45 71 20 3d     int bestNEq =
1f850 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f   0;            /
1f851 2a 20 6e 45 71 20 61 73 73 6f 63 69 61 74 65 64  * nEq associated
1f852 20 77 69 74 68 20 70 42 65 73 74 20 2a 2f 0a 20   with pBest */. 
1f853 20 20 20 64 6f 75 62 6c 65 20 6c 6f 77 65 73 74     double lowest
1f854 43 6f 73 74 3b 20 20 20 20 20 20 20 20 20 20 2f  Cost;          /
1f855 2a 20 43 6f 73 74 20 6f 66 20 74 68 65 20 70 42  * Cost of the pB
1f856 65 73 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 62  est */.    int b
1f857 65 73 74 4a 20 3d 20 30 3b 20 20 20 20 20 20 20  estJ = 0;       
1f858 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 61         /* The va
1f859 6c 75 65 20 6f 66 20 6a 20 2a 2f 0a 20 20 20 20  lue of j */.    
1f85a 42 69 74 6d 61 73 6b 20 6d 3b 20 20 20 20 20 20  Bitmask m;      
1f85b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
1f85c 69 74 6d 61 73 6b 20 76 61 6c 75 65 20 66 6f 72  itmask value for
1f85d 20 6a 20 6f 72 20 62 65 73 74 4a 20 2a 2f 0a 20   j or bestJ */. 
1f85e 20 20 20 69 6e 74 20 6f 6e 63 65 20 3d 20 30 3b     int once = 0;
1f85f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1f860 2a 20 54 72 75 65 20 77 68 65 6e 20 66 69 72 73  * True when firs
1f861 74 20 74 61 62 6c 65 20 69 73 20 73 65 65 6e 20  t table is seen 
1f862 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 69  */.    sqlite3_i
1f863 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 49 6e 64 65  ndex_info *pInde
1f864 78 3b 20 2f 2a 20 43 75 72 72 65 6e 74 20 76 69  x; /* Current vi
1f865 72 74 75 61 6c 20 69 6e 64 65 78 20 2a 2f 0a 0a  rtual index */..
1f866 20 20 20 20 6c 6f 77 65 73 74 43 6f 73 74 20 3d      lowestCost =
1f867 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 3b   SQLITE_BIG_DBL;
1f868 0a 20 20 20 20 66 6f 72 28 6a 3d 69 46 72 6f 6d  .    for(j=iFrom
1f869 2c 20 70 54 61 62 49 74 65 6d 3d 26 70 54 61 62  , pTabItem=&pTab
1f86a 4c 69 73 74 2d 3e 61 5b 6a 5d 3b 20 6a 3c 70 54  List->a[j]; j<pT
1f86b 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 6a 2b  abList->nSrc; j+
1f86c 2b 2c 20 70 54 61 62 49 74 65 6d 2b 2b 29 7b 0a  +, pTabItem++){.
1f86d 20 20 20 20 20 20 69 6e 74 20 64 6f 4e 6f 74 52        int doNotR
1f86e 65 6f 72 64 65 72 3b 20 20 2f 2a 20 54 72 75 65  eorder;  /* True
1f86f 20 69 66 20 74 68 69 73 20 74 61 62 6c 65 20 73   if this table s
1f870 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 72 65 6f  hould not be reo
1f871 72 64 65 72 65 64 20 2a 2f 0a 0a 20 20 20 20 20  rdered */..     
1f872 20 64 6f 4e 6f 74 52 65 6f 72 64 65 72 20 3d 20   doNotReorder = 
1f873 20 28 70 54 61 62 49 74 65 6d 2d 3e 6a 6f 69 6e   (pTabItem->join
1f874 74 79 70 65 20 26 20 28 4a 54 5f 4c 45 46 54 7c  type & (JT_LEFT|
1f875 4a 54 5f 43 52 4f 53 53 29 29 21 3d 30 3b 0a 20  JT_CROSS))!=0;. 
1f876 20 20 20 20 20 69 66 28 20 6f 6e 63 65 20 26 26       if( once &&
1f877 20 64 6f 4e 6f 74 52 65 6f 72 64 65 72 20 29 20   doNotReorder ) 
1f878 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 6d 20 3d  break;.      m =
1f879 20 67 65 74 4d 61 73 6b 28 26 6d 61 73 6b 53 65   getMask(&maskSe
1f87a 74 2c 20 70 54 61 62 49 74 65 6d 2d 3e 69 43 75  t, pTabItem->iCu
1f87b 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 69 66 28  rsor);.      if(
1f87c 20 28 6d 20 26 20 6e 6f 74 52 65 61 64 79 29 3d   (m & notReady)=
1f87d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  =0 ){.        if
1f87e 28 20 6a 3d 3d 69 46 72 6f 6d 20 29 20 69 46 72  ( j==iFrom ) iFr
1f87f 6f 6d 2b 2b 3b 0a 20 20 20 20 20 20 20 20 63 6f  om++;.        co
1f880 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a  ntinue;.      }.
1f881 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54        assert( pT
1f882 61 62 49 74 65 6d 2d 3e 70 54 61 62 20 29 3b 0a  abItem->pTab );.
1f883 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1f884 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1f885 0a 20 20 20 20 20 20 69 66 28 20 49 73 56 69 72  .      if( IsVir
1f886 74 75 61 6c 28 70 54 61 62 49 74 65 6d 2d 3e 70  tual(pTabItem->p
1f887 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 20 20  Tab) ){.        
1f888 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
1f889 66 6f 20 2a 2a 70 70 49 64 78 49 6e 66 6f 20 3d  fo **ppIdxInfo =
1f88a 20 26 70 57 49 6e 66 6f 2d 3e 61 5b 6a 5d 2e 70   &pWInfo->a[j].p
1f88b 49 64 78 49 6e 66 6f 3b 0a 20 20 20 20 20 20 20  IdxInfo;.       
1f88c 20 63 6f 73 74 20 3d 20 62 65 73 74 56 69 72 74   cost = bestVirt
1f88d 75 61 6c 49 6e 64 65 78 28 70 50 61 72 73 65 2c  ualIndex(pParse,
1f88e 20 26 77 63 2c 20 70 54 61 62 49 74 65 6d 2c 20   &wc, pTabItem, 
1f88f 6e 6f 74 52 65 61 64 79 2c 0a 20 20 20 20 20 20  notReady,.      
1f890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f891 20 20 20 20 20 20 20 20 20 20 70 70 4f 72 64 65            ppOrde
1f892 72 42 79 20 3f 20 2a 70 70 4f 72 64 65 72 42 79  rBy ? *ppOrderBy
1f893 20 3a 20 30 2c 20 69 3d 3d 30 2c 0a 20 20 20 20   : 0, i==0,.    
1f894 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f895 20 20 20 20 20 20 20 20 20 20 20 20 70 70 49 64              ppId
1f896 78 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20  xInfo);.        
1f897 66 6c 61 67 73 20 3d 20 57 48 45 52 45 5f 56 49  flags = WHERE_VI
1f898 52 54 55 41 4c 54 41 42 4c 45 3b 0a 20 20 20 20  RTUALTABLE;.    
1f899 20 20 20 20 70 49 6e 64 65 78 20 3d 20 2a 70 70      pIndex = *pp
1f89a 49 64 78 49 6e 66 6f 3b 0a 20 20 20 20 20 20 20  IdxInfo;.       
1f89b 20 69 66 28 20 70 49 6e 64 65 78 20 26 26 20 70   if( pIndex && p
1f89c 49 6e 64 65 78 2d 3e 6f 72 64 65 72 42 79 43 6f  Index->orderByCo
1f89d 6e 73 75 6d 65 64 20 29 7b 0a 20 20 20 20 20 20  nsumed ){.      
1f89e 20 20 20 20 66 6c 61 67 73 20 3d 20 57 48 45 52      flags = WHER
1f89f 45 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 7c  E_VIRTUALTABLE |
1f8a0 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59 3b 0a   WHERE_ORDERBY;.
1f8a1 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1f8a2 20 20 70 49 64 78 20 3d 20 30 3b 0a 20 20 20 20    pIdx = 0;.    
1f8a3 20 20 20 20 6e 45 71 20 3d 20 30 3b 0a 20 20 20      nEq = 0;.   
1f8a4 20 20 20 20 20 69 66 28 20 28 53 51 4c 49 54 45       if( (SQLITE
1f8a5 5f 42 49 47 5f 44 42 4c 2f 32 2e 30 29 3c 63 6f  _BIG_DBL/2.0)<co
1f8a6 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  st ){.          
1f8a7 2f 2a 20 54 68 65 20 63 6f 73 74 20 69 73 20 6e  /* The cost is n
1f8a8 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 65  ot allowed to be
1f8a9 20 6c 61 72 67 65 72 20 74 68 61 6e 20 53 51 4c   larger than SQL
1f8aa 49 54 45 5f 42 49 47 5f 44 42 4c 20 28 74 68 65  ITE_BIG_DBL (the
1f8ab 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 6e  .          ** in
1f8ac 69 74 61 6c 20 76 61 6c 75 65 20 6f 66 20 6c 6f  ital value of lo
1f8ad 77 65 73 74 43 6f 73 74 20 69 6e 20 74 68 69 73  westCost in this
1f8ae 20 6c 6f 6f 70 2e 20 49 66 20 69 74 20 69 73 2c   loop. If it is,
1f8af 20 74 68 65 6e 0a 20 20 20 20 20 20 20 20 20 20   then.          
1f8b0 2a 2a 20 74 68 65 20 28 63 6f 73 74 3c 6c 6f 77  ** the (cost<low
1f8b1 65 73 74 43 6f 73 74 29 20 74 65 73 74 20 62 65  estCost) test be
1f8b2 6c 6f 77 20 77 69 6c 6c 20 6e 65 76 65 72 20 62  low will never b
1f8b3 65 20 74 72 75 65 20 61 6e 64 0a 20 20 20 20 20  e true and.     
1f8b4 20 20 20 20 20 2a 2a 20 70 4c 65 76 65 6c 2d 3e       ** pLevel->
1f8b5 70 42 65 73 74 49 64 78 20 6e 65 76 65 72 20 73  pBestIdx never s
1f8b6 65 74 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f  et..          */
1f8b7 20 0a 20 20 20 20 20 20 20 20 20 20 63 6f 73 74   .          cost
1f8b8 20 3d 20 28 53 51 4c 49 54 45 5f 42 49 47 5f 44   = (SQLITE_BIG_D
1f8b9 42 4c 2f 32 2e 30 29 3b 0a 20 20 20 20 20 20 20  BL/2.0);.       
1f8ba 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 0a   }.      }else .
1f8bb 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a 20  #endif.      {. 
1f8bc 20 20 20 20 20 20 20 63 6f 73 74 20 3d 20 62 65         cost = be
1f8bd 73 74 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20  stIndex(pParse, 
1f8be 26 77 63 2c 20 70 54 61 62 49 74 65 6d 2c 20 6e  &wc, pTabItem, n
1f8bf 6f 74 52 65 61 64 79 2c 0a 20 20 20 20 20 20 20  otReady,.       
1f8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f8c1 20 20 28 69 3d 3d 30 20 26 26 20 70 70 4f 72 64    (i==0 && ppOrd
1f8c2 65 72 42 79 29 20 3f 20 2a 70 70 4f 72 64 65 72  erBy) ? *ppOrder
1f8c3 42 79 20 3a 20 30 2c 0a 20 20 20 20 20 20 20 20  By : 0,.        
1f8c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f8c5 20 26 70 49 64 78 2c 20 26 66 6c 61 67 73 2c 20   &pIdx, &flags, 
1f8c6 26 6e 45 71 29 3b 0a 20 20 20 20 20 20 20 20 70  &nEq);.        p
1f8c7 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20 20 20 20  Index = 0;.     
1f8c8 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63 6f 73   }.      if( cos
1f8c9 74 3c 6c 6f 77 65 73 74 43 6f 73 74 20 29 7b 0a  t<lowestCost ){.
1f8ca 20 20 20 20 20 20 20 20 6f 6e 63 65 20 3d 20 31          once = 1
1f8cb 3b 0a 20 20 20 20 20 20 20 20 6c 6f 77 65 73 74  ;.        lowest
1f8cc 43 6f 73 74 20 3d 20 63 6f 73 74 3b 0a 20 20 20  Cost = cost;.   
1f8cd 20 20 20 20 20 70 42 65 73 74 20 3d 20 70 49 64       pBest = pId
1f8ce 78 3b 0a 20 20 20 20 20 20 20 20 62 65 73 74 46  x;.        bestF
1f8cf 6c 61 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20  lags = flags;.  
1f8d0 20 20 20 20 20 20 62 65 73 74 4e 45 71 20 3d 20        bestNEq = 
1f8d1 6e 45 71 3b 0a 20 20 20 20 20 20 20 20 62 65 73  nEq;.        bes
1f8d2 74 4a 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20  tJ = j;.        
1f8d3 70 4c 65 76 65 6c 2d 3e 70 42 65 73 74 49 64 78  pLevel->pBestIdx
1f8d4 20 3d 20 70 49 6e 64 65 78 3b 0a 20 20 20 20 20   = pIndex;.     
1f8d5 20 7d 0a 20 20 20 20 20 20 69 66 28 20 64 6f 4e   }.      if( doN
1f8d6 6f 74 52 65 6f 72 64 65 72 20 29 20 62 72 65 61  otReorder ) brea
1f8d7 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 57 48 45  k;.    }.    WHE
1f8d8 52 45 54 52 41 43 45 28 28 22 2a 2a 2a 20 4f 70  RETRACE(("*** Op
1f8d9 74 69 6d 69 7a 65 72 20 63 68 6f 6f 73 65 20 74  timizer choose t
1f8da 61 62 6c 65 20 25 64 20 66 6f 72 20 6c 6f 6f 70  able %d for loop
1f8db 20 25 64 5c 6e 22 2c 20 62 65 73 74 4a 2c 0a 20   %d\n", bestJ,. 
1f8dc 20 20 20 20 20 20 20 20 20 20 70 4c 65 76 65 6c            pLevel
1f8dd 2d 70 57 49 6e 66 6f 2d 3e 61 29 29 3b 0a 20 20  -pWInfo->a));.  
1f8de 20 20 69 66 28 20 28 62 65 73 74 46 6c 61 67 73    if( (bestFlags
1f8df 20 26 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59   & WHERE_ORDERBY
1f8e0 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 2a 70  )!=0 ){.      *p
1f8e1 70 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20  pOrderBy = 0;.  
1f8e2 20 20 7d 0a 20 20 20 20 61 6e 64 46 6c 61 67 73    }.    andFlags
1f8e3 20 26 3d 20 62 65 73 74 46 6c 61 67 73 3b 0a 20   &= bestFlags;. 
1f8e4 20 20 20 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73     pLevel->flags
1f8e5 20 3d 20 62 65 73 74 46 6c 61 67 73 3b 0a 20 20   = bestFlags;.  
1f8e6 20 20 70 4c 65 76 65 6c 2d 3e 70 49 64 78 20 3d    pLevel->pIdx =
1f8e7 20 70 42 65 73 74 3b 0a 20 20 20 20 70 4c 65 76   pBest;.    pLev
1f8e8 65 6c 2d 3e 6e 45 71 20 3d 20 62 65 73 74 4e 45  el->nEq = bestNE
1f8e9 71 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 61  q;.    pLevel->a
1f8ea 49 6e 4c 6f 6f 70 20 3d 20 30 3b 0a 20 20 20 20  InLoop = 0;.    
1f8eb 70 4c 65 76 65 6c 2d 3e 6e 49 6e 20 3d 20 30 3b  pLevel->nIn = 0;
1f8ec 0a 20 20 20 20 69 66 28 20 70 42 65 73 74 20 29  .    if( pBest )
1f8ed 7b 0a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e  {.      pLevel->
1f8ee 69 49 64 78 43 75 72 20 3d 20 70 50 61 72 73 65  iIdxCur = pParse
1f8ef 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 7d 65  ->nTab++;.    }e
1f8f0 6c 73 65 7b 0a 20 20 20 20 20 20 70 4c 65 76 65  lse{.      pLeve
1f8f1 6c 2d 3e 69 49 64 78 43 75 72 20 3d 20 2d 31 3b  l->iIdxCur = -1;
1f8f2 0a 20 20 20 20 7d 0a 20 20 20 20 6e 6f 74 52 65  .    }.    notRe
1f8f3 61 64 79 20 26 3d 20 7e 67 65 74 4d 61 73 6b 28  ady &= ~getMask(
1f8f4 26 6d 61 73 6b 53 65 74 2c 20 70 54 61 62 4c 69  &maskSet, pTabLi
1f8f5 73 74 2d 3e 61 5b 62 65 73 74 4a 5d 2e 69 43 75  st->a[bestJ].iCu
1f8f6 72 73 6f 72 29 3b 0a 20 20 20 20 70 4c 65 76 65  rsor);.    pLeve
1f8f7 6c 2d 3e 69 46 72 6f 6d 20 3d 20 62 65 73 74 4a  l->iFrom = bestJ
1f8f8 3b 0a 20 20 7d 0a 20 20 57 48 45 52 45 54 52 41  ;.  }.  WHERETRA
1f8f9 43 45 28 28 22 2a 2a 2a 20 4f 70 74 69 6d 69 7a  CE(("*** Optimiz
1f8fa 65 72 20 46 69 6e 69 73 68 65 64 20 2a 2a 2a 5c  er Finished ***\
1f8fb 6e 22 29 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  n"));..  /* If t
1f8fc 68 65 20 74 6f 74 61 6c 20 71 75 65 72 79 20 6f  he total query o
1f8fd 6e 6c 79 20 73 65 6c 65 63 74 73 20 61 20 73 69  nly selects a si
1f8fe 6e 67 6c 65 20 72 6f 77 2c 20 74 68 65 6e 20 74  ngle row, then t
1f8ff 68 65 20 4f 52 44 45 52 20 42 59 0a 20 20 2a 2a  he ORDER BY.  **
1f900 20 63 6c 61 75 73 65 20 69 73 20 69 72 72 65 6c   clause is irrel
1f901 65 76 61 6e 74 2e 0a 20 20 2a 2f 0a 20 20 69 66  evant..  */.  if
1f902 28 20 28 61 6e 64 46 6c 61 67 73 20 26 20 57 48  ( (andFlags & WH
1f903 45 52 45 5f 55 4e 49 51 55 45 29 21 3d 30 20 26  ERE_UNIQUE)!=0 &
1f904 26 20 70 70 4f 72 64 65 72 42 79 20 29 7b 0a 20  & ppOrderBy ){. 
1f905 20 20 20 2a 70 70 4f 72 64 65 72 42 79 20 3d 20     *ppOrderBy = 
1f906 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65  0;.  }..  /* Ope
1f907 6e 20 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e 20  n all tables in 
1f908 74 68 65 20 70 54 61 62 4c 69 73 74 20 61 6e 64  the pTabList and
1f909 20 61 6e 79 20 69 6e 64 69 63 65 73 20 73 65 6c   any indices sel
1f90a 65 63 74 65 64 20 66 6f 72 0a 20 20 2a 2a 20 73  ected for.  ** s
1f90b 65 61 72 63 68 69 6e 67 20 74 68 6f 73 65 20 74  earching those t
1f90c 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 73 71  ables..  */.  sq
1f90d 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53  lite3CodeVerifyS
1f90e 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 2d 31  chema(pParse, -1
1f90f 29 3b 20 2f 2a 20 49 6e 73 65 72 74 20 74 68 65  ); /* Insert the
1f910 20 63 6f 6f 6b 69 65 20 76 65 72 69 66 69 65 72   cookie verifier
1f911 20 47 6f 74 6f 20 2a 2f 0a 20 20 66 6f 72 28 69   Goto */.  for(i
1f912 3d 30 2c 20 70 4c 65 76 65 6c 3d 70 57 49 6e 66  =0, pLevel=pWInf
1f913 6f 2d 3e 61 3b 20 69 3c 70 54 61 62 4c 69 73 74  o->a; i<pTabList
1f914 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 4c 65  ->nSrc; i++, pLe
1f915 76 65 6c 2b 2b 29 7b 0a 20 20 20 20 54 61 62 6c  vel++){.    Tabl
1f916 65 20 2a 70 54 61 62 3b 20 20 20 20 20 2f 2a 20  e *pTab;     /* 
1f917 54 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f  Table to open */
1f918 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 78 3b  .    Index *pIx;
1f919 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 75        /* Index u
1f91a 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 70 54  sed to access pT
1f91b 61 62 20 28 69 66 20 61 6e 79 29 20 2a 2f 0a 20  ab (if any) */. 
1f91c 20 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20     int iDb;     
1f91d 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
1f91e 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e  database contain
1f91f 69 6e 67 20 74 61 62 6c 65 2f 69 6e 64 65 78 20  ing table/index 
1f920 2a 2f 0a 20 20 20 20 69 6e 74 20 69 49 64 78 43  */.    int iIdxC
1f921 75 72 20 3d 20 70 4c 65 76 65 6c 2d 3e 69 49 64  ur = pLevel->iId
1f922 78 43 75 72 3b 0a 0a 23 69 66 6e 64 65 66 20 53  xCur;..#ifndef S
1f923 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41  QLITE_OMIT_EXPLA
1f924 49 4e 0a 20 20 20 20 69 66 28 20 70 50 61 72 73  IN.    if( pPars
1f925 65 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 29 7b  e->explain==2 ){
1f926 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4d 73  .      char *zMs
1f927 67 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20  g;.      struct 
1f928 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  SrcList_item *pI
1f929 74 65 6d 20 3d 20 26 70 54 61 62 4c 69 73 74 2d  tem = &pTabList-
1f92a 3e 61 5b 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d  >a[pLevel->iFrom
1f92b 5d 3b 0a 20 20 20 20 20 20 7a 4d 73 67 20 3d 20  ];.      zMsg = 
1f92c 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 22  sqlite3MPrintf("
1f92d 54 41 42 4c 45 20 25 73 22 2c 20 70 49 74 65 6d  TABLE %s", pItem
1f92e 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  ->zName);.      
1f92f 69 66 28 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61  if( pItem->zAlia
1f930 73 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 4d 73  s ){.        zMs
1f931 67 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  g = sqlite3MPrin
1f932 74 66 28 22 25 7a 20 41 53 20 25 73 22 2c 20 7a  tf("%z AS %s", z
1f933 4d 73 67 2c 20 70 49 74 65 6d 2d 3e 7a 41 6c 69  Msg, pItem->zAli
1f934 61 73 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  as);.      }.   
1f935 20 20 20 69 66 28 20 28 70 49 78 20 3d 20 70 4c     if( (pIx = pL
1f936 65 76 65 6c 2d 3e 70 49 64 78 29 21 3d 30 20 29  evel->pIdx)!=0 )
1f937 7b 0a 20 20 20 20 20 20 20 20 7a 4d 73 67 20 3d  {.        zMsg =
1f938 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
1f939 22 25 7a 20 57 49 54 48 20 49 4e 44 45 58 20 25  "%z WITH INDEX %
1f93a 73 22 2c 20 7a 4d 73 67 2c 20 70 49 78 2d 3e 7a  s", zMsg, pIx->z
1f93b 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c  Name);.      }el
1f93c 73 65 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 66  se if( pLevel->f
1f93d 6c 61 67 73 20 26 20 28 57 48 45 52 45 5f 52 4f  lags & (WHERE_RO
1f93e 57 49 44 5f 45 51 7c 57 48 45 52 45 5f 52 4f 57  WID_EQ|WHERE_ROW
1f93f 49 44 5f 52 41 4e 47 45 29 20 29 7b 0a 20 20 20  ID_RANGE) ){.   
1f940 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69       zMsg = sqli
1f941 74 65 33 4d 50 72 69 6e 74 66 28 22 25 7a 20 55  te3MPrintf("%z U
1f942 53 49 4e 47 20 50 52 49 4d 41 52 59 20 4b 45 59  SING PRIMARY KEY
1f943 22 2c 20 7a 4d 73 67 29 3b 0a 20 20 20 20 20 20  ", zMsg);.      
1f944 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
1f945 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
1f946 4c 45 0a 20 20 20 20 20 20 65 6c 73 65 20 69 66  LE.      else if
1f947 28 20 70 4c 65 76 65 6c 2d 3e 70 42 65 73 74 49  ( pLevel->pBestI
1f948 64 78 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  dx ){.        sq
1f949 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f  lite3_index_info
1f94a 20 2a 70 42 65 73 74 49 64 78 20 3d 20 70 4c 65   *pBestIdx = pLe
1f94b 76 65 6c 2d 3e 70 42 65 73 74 49 64 78 3b 0a 20  vel->pBestIdx;. 
1f94c 20 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71         zMsg = sq
1f94d 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 22 25 7a  lite3MPrintf("%z
1f94e 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 49   VIRTUAL TABLE I
1f94f 4e 44 45 58 20 25 64 3a 25 73 22 2c 20 7a 4d 73  NDEX %d:%s", zMs
1f950 67 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  g,.             
1f951 20 20 20 20 20 20 20 70 42 65 73 74 49 64 78 2d         pBestIdx-
1f952 3e 69 64 78 4e 75 6d 2c 20 70 42 65 73 74 49 64  >idxNum, pBestId
1f953 78 2d 3e 69 64 78 53 74 72 29 3b 0a 20 20 20 20  x->idxStr);.    
1f954 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20    }.#endif.     
1f955 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 66 6c 61   if( pLevel->fla
1f956 67 73 20 26 20 57 48 45 52 45 5f 4f 52 44 45 52  gs & WHERE_ORDER
1f957 42 59 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 4d  BY ){.        zM
1f958 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  sg = sqlite3MPri
1f959 6e 74 66 28 22 25 7a 20 4f 52 44 45 52 20 42 59  ntf("%z ORDER BY
1f95a 22 2c 20 7a 4d 73 67 29 3b 0a 20 20 20 20 20 20  ", zMsg);.      
1f95b 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  }.      sqlite3V
1f95c 64 62 65 4f 70 33 28 76 2c 20 4f 50 5f 45 78 70  dbeOp3(v, OP_Exp
1f95d 6c 61 69 6e 2c 20 69 2c 20 70 4c 65 76 65 6c 2d  lain, i, pLevel-
1f95e 3e 69 46 72 6f 6d 2c 20 7a 4d 73 67 2c 20 50 33  >iFrom, zMsg, P3
1f95f 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 7d  _DYNAMIC);.    }
1f960 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1f961 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 20 2a  E_OMIT_EXPLAIN *
1f962 2f 0a 20 20 20 20 70 54 61 62 49 74 65 6d 20 3d  /.    pTabItem =
1f963 20 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70 4c   &pTabList->a[pL
1f964 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20  evel->iFrom];.  
1f965 20 20 70 54 61 62 20 3d 20 70 54 61 62 49 74 65    pTab = pTabIte
1f966 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 69 44 62  m->pTab;.    iDb
1f967 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
1f968 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e  ToIndex(pParse->
1f969 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
1f96a 61 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62  a);.    if( pTab
1f96b 2d 3e 69 73 45 70 68 65 6d 20 7c 7c 20 70 54 61  ->isEphem || pTa
1f96c 62 2d 3e 70 53 65 6c 65 63 74 20 29 20 63 6f 6e  b->pSelect ) con
1f96d 74 69 6e 75 65 3b 0a 23 69 66 6e 64 65 66 20 53  tinue;.#ifndef S
1f96e 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
1f96f 41 4c 54 41 42 4c 45 0a 20 20 20 20 69 66 28 20  ALTABLE.    if( 
1f970 70 4c 65 76 65 6c 2d 3e 70 42 65 73 74 49 64 78  pLevel->pBestIdx
1f971 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 43   ){.      int iC
1f972 75 72 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 69  ur = pTabItem->i
1f973 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 73 71  Cursor;.      sq
1f974 6c 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20  lite3VdbeOp3(v, 
1f975 4f 50 5f 56 4f 70 65 6e 2c 20 69 43 75 72 2c 20  OP_VOpen, iCur, 
1f976 30 2c 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29  0, (const char*)
1f977 70 54 61 62 2d 3e 70 56 74 61 62 2c 20 50 33 5f  pTab->pVtab, P3_
1f978 56 54 41 42 29 3b 0a 20 20 20 20 7d 65 6c 73 65  VTAB);.    }else
1f979 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20  .#endif.    if( 
1f97a 28 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73 20 26  (pLevel->flags &
1f97b 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 29   WHERE_IDX_ONLY)
1f97c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ==0 ){.      sql
1f97d 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50  ite3OpenTable(pP
1f97e 61 72 73 65 2c 20 70 54 61 62 49 74 65 6d 2d 3e  arse, pTabItem->
1f97f 69 43 75 72 73 6f 72 2c 20 69 44 62 2c 20 70 54  iCursor, iDb, pT
1f980 61 62 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29  ab, OP_OpenRead)
1f981 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 61 62  ;.      if( pTab
1f982 2d 3e 6e 43 6f 6c 3c 28 73 69 7a 65 6f 66 28 42  ->nCol<(sizeof(B
1f983 69 74 6d 61 73 6b 29 2a 38 29 20 29 7b 0a 20 20  itmask)*8) ){.  
1f984 20 20 20 20 20 20 42 69 74 6d 61 73 6b 20 62 20        Bitmask b 
1f985 3d 20 70 54 61 62 49 74 65 6d 2d 3e 63 6f 6c 55  = pTabItem->colU
1f986 73 65 64 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  sed;.        int
1f987 20 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20   n = 0;.        
1f988 66 6f 72 28 3b 20 62 3b 20 62 3d 62 3e 3e 31 2c  for(; b; b=b>>1,
1f989 20 6e 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 20 20   n++){}.        
1f98a 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1f98b 65 50 32 28 76 2c 20 73 71 6c 69 74 65 33 56 64  eP2(v, sqlite3Vd
1f98c 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
1f98d 2d 31 2c 20 6e 29 3b 0a 20 20 20 20 20 20 20 20  -1, n);.        
1f98e 61 73 73 65 72 74 28 20 6e 3c 3d 70 54 61 62 2d  assert( n<=pTab-
1f98f 3e 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 20 20 7d  >nCol );.      }
1f990 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1f991 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f    sqlite3TableLo
1f992 63 6b 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20  ck(pParse, iDb, 
1f993 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 30 2c 20 70  pTab->tnum, 0, p
1f994 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  Tab->zName);.   
1f995 20 7d 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 69   }.    pLevel->i
1f996 54 61 62 43 75 72 20 3d 20 70 54 61 62 49 74 65  TabCur = pTabIte
1f997 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20  m->iCursor;.    
1f998 69 66 28 20 28 70 49 78 20 3d 20 70 4c 65 76 65  if( (pIx = pLeve
1f999 6c 2d 3e 70 49 64 78 29 21 3d 30 20 29 7b 0a 20  l->pIdx)!=0 ){. 
1f99a 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b       KeyInfo *pK
1f99b 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65  ey = sqlite3Inde
1f99c 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c  xKeyinfo(pParse,
1f99d 20 70 49 78 29 3b 0a 20 20 20 20 20 20 61 73 73   pIx);.      ass
1f99e 65 72 74 28 20 70 49 78 2d 3e 70 53 63 68 65 6d  ert( pIx->pSchem
1f99f 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  a==pTab->pSchema
1f9a0 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
1f9a1 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1f9a2 5f 49 6e 74 65 67 65 72 2c 20 69 44 62 2c 20 30  _Integer, iDb, 0
1f9a3 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d  );.      VdbeCom
1f9a4 6d 65 6e 74 28 28 76 2c 20 22 23 20 25 73 22 2c  ment((v, "# %s",
1f9a5 20 70 49 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20   pIx->zName));. 
1f9a6 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1f9a7 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 65  Op3(v, OP_OpenRe
1f9a8 61 64 2c 20 69 49 64 78 43 75 72 2c 20 70 49 78  ad, iIdxCur, pIx
1f9a9 2d 3e 74 6e 75 6d 2c 0a 20 20 20 20 20 20 20 20  ->tnum,.        
1f9aa 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63 68               (ch
1f9ab 61 72 2a 29 70 4b 65 79 2c 20 50 33 5f 4b 45 59  ar*)pKey, P3_KEY
1f9ac 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20  INFO_HANDOFF);. 
1f9ad 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 70 4c     }.    if( (pL
1f9ae 65 76 65 6c 2d 3e 66 6c 61 67 73 20 26 20 28 57  evel->flags & (W
1f9af 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 7c 57 48  HERE_IDX_ONLY|WH
1f9b0 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41 4e 47 45  ERE_COLUMN_RANGE
1f9b1 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f  ))!=0 ){.      /
1f9b2 2a 20 4f 6e 6c 79 20 63 61 6c 6c 20 4f 50 5f 53  * Only call OP_S
1f9b3 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 20 6f 6e 20  etNumColumns on 
1f9b4 74 68 65 20 69 6e 64 65 78 20 69 66 20 77 65 20  the index if we 
1f9b5 6d 69 67 68 74 20 6c 61 74 65 72 20 75 73 65 0a  might later use.
1f9b6 20 20 20 20 20 20 2a 2a 20 4f 50 5f 43 6f 6c 75        ** OP_Colu
1f9b7 6d 6e 20 6f 6e 20 74 68 65 20 69 6e 64 65 78 2e  mn on the index.
1f9b8 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
1f9b9 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1f9ba 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20  _SetNumColumns, 
1f9bb 69 49 64 78 43 75 72 2c 20 70 49 78 2d 3e 6e 43  iIdxCur, pIx->nC
1f9bc 6f 6c 75 6d 6e 2b 31 29 3b 0a 20 20 20 20 7d 0a  olumn+1);.    }.
1f9bd 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 56      sqlite3CodeV
1f9be 65 72 69 66 79 53 63 68 65 6d 61 28 70 50 61 72  erifySchema(pPar
1f9bf 73 65 2c 20 69 44 62 29 3b 0a 20 20 7d 0a 20 20  se, iDb);.  }.  
1f9c0 70 57 49 6e 66 6f 2d 3e 69 54 6f 70 20 3d 20 73  pWInfo->iTop = s
1f9c1 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
1f9c2 74 41 64 64 72 28 76 29 3b 0a 0a 20 20 2f 2a 20  tAddr(v);..  /* 
1f9c3 47 65 6e 65 72 61 74 65 20 74 68 65 20 63 6f 64  Generate the cod
1f9c4 65 20 74 6f 20 64 6f 20 74 68 65 20 73 65 61 72  e to do the sear
1f9c5 63 68 2e 20 20 45 61 63 68 20 69 74 65 72 61 74  ch.  Each iterat
1f9c6 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 72 0a 20  ion of the for. 
1f9c7 20 2a 2a 20 6c 6f 6f 70 20 62 65 6c 6f 77 20 67   ** loop below g
1f9c8 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20 66 6f  enerates code fo
1f9c9 72 20 61 20 73 69 6e 67 6c 65 20 6e 65 73 74 65  r a single neste
1f9ca 64 20 6c 6f 6f 70 20 6f 66 20 74 68 65 20 56 4d  d loop of the VM
1f9cb 0a 20 20 2a 2a 20 70 72 6f 67 72 61 6d 2e 0a 20  .  ** program.. 
1f9cc 20 2a 2f 0a 20 20 6e 6f 74 52 65 61 64 79 20 3d   */.  notReady =
1f9cd 20 7e 28 42 69 74 6d 61 73 6b 29 30 3b 0a 20 20   ~(Bitmask)0;.  
1f9ce 66 6f 72 28 69 3d 30 2c 20 70 4c 65 76 65 6c 3d  for(i=0, pLevel=
1f9cf 70 57 49 6e 66 6f 2d 3e 61 3b 20 69 3c 70 54 61  pWInfo->a; i<pTa
1f9d0 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b  bList->nSrc; i++
1f9d1 2c 20 70 4c 65 76 65 6c 2b 2b 29 7b 0a 20 20 20  , pLevel++){.   
1f9d2 20 69 6e 74 20 6a 3b 0a 20 20 20 20 69 6e 74 20   int j;.    int 
1f9d3 69 43 75 72 20 3d 20 70 54 61 62 49 74 65 6d 2d  iCur = pTabItem-
1f9d4 3e 69 43 75 72 73 6f 72 3b 20 20 2f 2a 20 54 68  >iCursor;  /* Th
1f9d5 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 66 6f  e VDBE cursor fo
1f9d6 72 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20  r the table */. 
1f9d7 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20     Index *pIdx; 
1f9d8 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64        /* The ind
1f9d9 65 78 20 77 65 20 77 69 6c 6c 20 62 65 20 75 73  ex we will be us
1f9da 69 6e 67 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e  ing */.    int n
1f9db 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  xt;           /*
1f9dc 20 57 68 65 72 65 20 74 6f 20 6a 75 6d 70 20 74   Where to jump t
1f9dd 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20  o continue with 
1f9de 74 68 65 20 6e 65 78 74 20 49 4e 20 63 61 73 65  the next IN case
1f9df 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 49 64 78   */.    int iIdx
1f9e0 43 75 72 3b 20 20 20 20 20 20 20 2f 2a 20 54 68  Cur;       /* Th
1f9e1 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 66 6f  e VDBE cursor fo
1f9e2 72 20 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20  r the index */. 
1f9e3 20 20 20 69 6e 74 20 6f 6d 69 74 54 61 62 6c 65     int omitTable
1f9e4 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66  ;     /* True if
1f9e5 20 77 65 20 75 73 65 20 74 68 65 20 69 6e 64 65   we use the inde
1f9e6 78 20 6f 6e 6c 79 20 2a 2f 0a 20 20 20 20 69 6e  x only */.    in
1f9e7 74 20 62 52 65 76 3b 20 20 20 20 20 20 20 20 20  t bRev;         
1f9e8 20 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20 6e   /* True if we n
1f9e9 65 65 64 20 74 6f 20 73 63 61 6e 20 69 6e 20 72  eed to scan in r
1f9ea 65 76 65 72 73 65 20 6f 72 64 65 72 20 2a 2f 0a  everse order */.
1f9eb 0a 20 20 20 20 70 54 61 62 49 74 65 6d 20 3d 20  .    pTabItem = 
1f9ec 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70 4c 65  &pTabList->a[pLe
1f9ed 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20 20  vel->iFrom];.   
1f9ee 20 69 43 75 72 20 3d 20 70 54 61 62 49 74 65 6d   iCur = pTabItem
1f9ef 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 70  ->iCursor;.    p
1f9f0 49 64 78 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 49  Idx = pLevel->pI
1f9f1 64 78 3b 0a 20 20 20 20 69 49 64 78 43 75 72 20  dx;.    iIdxCur 
1f9f2 3d 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75  = pLevel->iIdxCu
1f9f3 72 3b 0a 20 20 20 20 62 52 65 76 20 3d 20 28 70  r;.    bRev = (p
1f9f4 4c 65 76 65 6c 2d 3e 66 6c 61 67 73 20 26 20 57  Level->flags & W
1f9f5 48 45 52 45 5f 52 45 56 45 52 53 45 29 21 3d 30  HERE_REVERSE)!=0
1f9f6 3b 0a 20 20 20 20 6f 6d 69 74 54 61 62 6c 65 20  ;.    omitTable 
1f9f7 3d 20 28 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73  = (pLevel->flags
1f9f8 20 26 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c   & WHERE_IDX_ONL
1f9f9 59 29 21 3d 30 3b 0a 0a 20 20 20 20 2f 2a 20 43  Y)!=0;..    /* C
1f9fa 72 65 61 74 65 20 6c 61 62 65 6c 73 20 66 6f 72  reate labels for
1f9fb 20 74 68 65 20 22 62 72 65 61 6b 22 20 61 6e 64   the "break" and
1f9fc 20 22 63 6f 6e 74 69 6e 75 65 22 20 69 6e 73 74   "continue" inst
1f9fd 72 75 63 74 69 6f 6e 73 0a 20 20 20 20 2a 2a 20  ructions.    ** 
1f9fe 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20  for the current 
1f9ff 6c 6f 6f 70 2e 20 20 4a 75 6d 70 20 74 6f 20 62  loop.  Jump to b
1fa00 72 6b 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 20  rk to break out 
1fa01 6f 66 20 61 20 6c 6f 6f 70 2e 0a 20 20 20 20 2a  of a loop..    *
1fa02 2a 20 4a 75 6d 70 20 74 6f 20 63 6f 6e 74 20 74  * Jump to cont t
1fa03 6f 20 67 6f 20 69 6d 6d 65 64 69 61 74 65 6c 79  o go immediately
1fa04 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 74 65   to the next ite
1fa05 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 20 20  ration of the.  
1fa06 20 20 2a 2a 20 6c 6f 6f 70 2e 0a 20 20 20 20 2a    ** loop..    *
1fa07 2a 0a 20 20 20 20 2a 2a 20 57 68 65 6e 20 74 68  *.    ** When th
1fa08 65 72 65 20 69 73 20 61 6e 20 49 4e 20 6f 70 65  ere is an IN ope
1fa09 72 61 74 6f 72 2c 20 77 65 20 61 6c 73 6f 20 68  rator, we also h
1fa0a 61 76 65 20 61 20 22 6e 78 74 22 20 6c 61 62 65  ave a "nxt" labe
1fa0b 6c 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 6d 65  l that.    ** me
1fa0c 61 6e 73 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20  ans to continue 
1fa0d 77 69 74 68 20 74 68 65 20 6e 65 78 74 20 49 4e  with the next IN
1fa0e 20 76 61 6c 75 65 20 63 6f 6d 62 69 6e 61 74 69   value combinati
1fa0f 6f 6e 2e 20 20 57 68 65 6e 0a 20 20 20 20 2a 2a  on.  When.    **
1fa10 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 49 4e   there are no IN
1fa11 20 6f 70 65 72 61 74 6f 72 73 20 69 6e 20 74 68   operators in th
1fa12 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 20 74  e constraints, t
1fa13 68 65 20 22 6e 78 74 22 20 6c 61 62 65 6c 0a 20  he "nxt" label. 
1fa14 20 20 20 2a 2a 20 69 73 20 74 68 65 20 73 61 6d     ** is the sam
1fa15 65 20 61 73 20 22 62 72 6b 22 2e 0a 20 20 20 20  e as "brk"..    
1fa16 2a 2f 0a 20 20 20 20 62 72 6b 20 3d 20 70 4c 65  */.    brk = pLe
1fa17 76 65 6c 2d 3e 62 72 6b 20 3d 20 70 4c 65 76 65  vel->brk = pLeve
1fa18 6c 2d 3e 6e 78 74 20 3d 20 73 71 6c 69 74 65 33  l->nxt = sqlite3
1fa19 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29  VdbeMakeLabel(v)
1fa1a 3b 0a 20 20 20 20 63 6f 6e 74 20 3d 20 70 4c 65  ;.    cont = pLe
1fa1b 76 65 6c 2d 3e 63 6f 6e 74 20 3d 20 73 71 6c 69  vel->cont = sqli
1fa1c 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
1fa1d 28 76 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20  (v);..    /* If 
1fa1e 74 68 69 73 20 69 73 20 74 68 65 20 72 69 67 68  this is the righ
1fa1f 74 20 74 61 62 6c 65 20 6f 66 20 61 20 4c 45 46  t table of a LEF
1fa20 54 20 4f 55 54 45 52 20 4a 4f 49 4e 2c 20 61 6c  T OUTER JOIN, al
1fa21 6c 6f 63 61 74 65 20 61 6e 64 0a 20 20 20 20 2a  locate and.    *
1fa22 2a 20 69 6e 69 74 69 61 6c 69 7a 65 20 61 20 6d  * initialize a m
1fa23 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68 61 74 20  emory cell that 
1fa24 72 65 63 6f 72 64 73 20 69 66 20 74 68 69 73 20  records if this 
1fa25 74 61 62 6c 65 20 6d 61 74 63 68 65 73 20 61 6e  table matches an
1fa26 79 0a 20 20 20 20 2a 2a 20 72 6f 77 20 6f 66 20  y.    ** row of 
1fa27 74 68 65 20 6c 65 66 74 20 74 61 62 6c 65 20 6f  the left table o
1fa28 66 20 74 68 65 20 6a 6f 69 6e 2e 0a 20 20 20 20  f the join..    
1fa29 2a 2f 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65  */.    if( pLeve
1fa2a 6c 2d 3e 69 46 72 6f 6d 3e 30 20 26 26 20 28 70  l->iFrom>0 && (p
1fa2b 54 61 62 49 74 65 6d 5b 30 5d 2e 6a 6f 69 6e 74  TabItem[0].joint
1fa2c 79 70 65 20 26 20 4a 54 5f 4c 45 46 54 29 21 3d  ype & JT_LEFT)!=
1fa2d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21  0 ){.      if( !
1fa2e 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 29 20 70  pParse->nMem ) p
1fa2f 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 2b 3b 0a 20  Parse->nMem++;. 
1fa30 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 69 4c 65       pLevel->iLe
1fa31 66 74 4a 6f 69 6e 20 3d 20 70 50 61 72 73 65 2d  ftJoin = pParse-
1fa32 3e 6e 4d 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 73  >nMem++;.      s
1fa33 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1fa34 76 2c 20 4f 50 5f 4d 65 6d 49 6e 74 2c 20 30 2c  v, OP_MemInt, 0,
1fa35 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f   pLevel->iLeftJo
1fa36 69 6e 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43  in);.      VdbeC
1fa37 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 23 20 69 6e  omment((v, "# in
1fa38 69 74 20 4c 45 46 54 20 4a 4f 49 4e 20 6e 6f 2d  it LEFT JOIN no-
1fa39 6d 61 74 63 68 20 66 6c 61 67 22 29 29 3b 0a 20  match flag"));. 
1fa3a 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51     }..#ifndef SQ
1fa3b 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
1fa3c 4c 54 41 42 4c 45 0a 20 20 20 20 69 66 28 20 70  LTABLE.    if( p
1fa3d 4c 65 76 65 6c 2d 3e 70 42 65 73 74 49 64 78 20  Level->pBestIdx 
1fa3e 29 7b 0a 20 20 20 20 20 20 2f 2a 20 43 61 73 65  ){.      /* Case
1fa3f 20 30 3a 20 20 54 68 65 20 74 61 62 6c 65 20 69   0:  The table i
1fa40 73 20 61 20 76 69 72 74 75 61 6c 2d 74 61 62 6c  s a virtual-tabl
1fa41 65 2e 20 20 55 73 65 20 74 68 65 20 56 46 69 6c  e.  Use the VFil
1fa42 74 65 72 20 61 6e 64 20 56 4e 65 78 74 0a 20 20  ter and VNext.  
1fa43 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
1fa44 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 64 61  to access the da
1fa45 74 61 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ta..      */.   
1fa46 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20     int j;.      
1fa47 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
1fa48 66 6f 20 2a 70 42 65 73 74 49 64 78 20 3d 20 70  fo *pBestIdx = p
1fa49 4c 65 76 65 6c 2d 3e 70 42 65 73 74 49 64 78 3b  Level->pBestIdx;
1fa4a 0a 20 20 20 20 20 20 69 6e 74 20 6e 43 6f 6e 73  .      int nCons
1fa4b 74 72 61 69 6e 74 20 3d 20 70 42 65 73 74 49 64  traint = pBestId
1fa4c 78 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 0a  x->nConstraint;.
1fa4d 20 20 20 20 20 20 73 74 72 75 63 74 20 73 71 6c        struct sql
1fa4e 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74  ite3_index_const
1fa4f 72 61 69 6e 74 5f 75 73 61 67 65 20 2a 61 55 73  raint_usage *aUs
1fa50 61 67 65 20 3d 0a 20 20 20 20 20 20 20 20 20 20  age =.          
1fa51 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa53 20 20 20 20 20 20 20 20 70 42 65 73 74 49 64 78          pBestIdx
1fa54 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61  ->aConstraintUsa
1fa55 67 65 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20  ge;.      const 
1fa56 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69  struct sqlite3_i
1fa57 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20  ndex_constraint 
1fa58 2a 61 43 6f 6e 73 74 72 61 69 6e 74 20 3d 0a 20  *aConstraint =. 
1fa59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa5b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa5c 20 70 42 65 73 74 49 64 78 2d 3e 61 43 6f 6e 73   pBestIdx->aCons
1fa5d 74 72 61 69 6e 74 3b 0a 0a 20 20 20 20 20 20 66  traint;..      f
1fa5e 6f 72 28 6a 3d 31 3b 20 6a 3c 3d 6e 43 6f 6e 73  or(j=1; j<=nCons
1fa5f 74 72 61 69 6e 74 3b 20 6a 2b 2b 29 7b 0a 20 20  traint; j++){.  
1fa60 20 20 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20 20        int k;.   
1fa61 20 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c       for(k=0; k<
1fa62 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 6b 2b 2b  nConstraint; k++
1fa63 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
1fa64 20 61 55 73 61 67 65 5b 6b 5d 2e 61 72 67 76 49   aUsage[k].argvI
1fa65 6e 64 65 78 3d 3d 6a 20 29 7b 0a 20 20 20 20 20  ndex==j ){.     
1fa66 20 20 20 20 20 20 20 69 6e 74 20 69 54 65 72 6d         int iTerm
1fa67 20 3d 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 6b   = aConstraint[k
1fa68 5d 2e 69 54 65 72 6d 4f 66 66 73 65 74 3b 0a 20  ].iTermOffset;. 
1fa69 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1fa6a 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
1fa6b 65 2c 20 77 63 2e 61 5b 69 54 65 72 6d 5d 2e 70  e, wc.a[iTerm].p
1fa6c 45 78 70 72 2d 3e 70 52 69 67 68 74 29 3b 0a 20  Expr->pRight);. 
1fa6d 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
1fa6e 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
1fa6f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1fa70 69 66 28 20 6b 3d 3d 6e 43 6f 6e 73 74 72 61 69  if( k==nConstrai
1fa71 6e 74 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  nt ) break;.    
1fa72 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
1fa73 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1fa74 5f 49 6e 74 65 67 65 72 2c 20 6a 2d 31 2c 20 30  _Integer, j-1, 0
1fa75 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1fa76 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1fa77 49 6e 74 65 67 65 72 2c 20 70 42 65 73 74 49 64  Integer, pBestId
1fa78 78 2d 3e 69 64 78 4e 75 6d 2c 20 30 29 3b 0a 20  x->idxNum, 0);. 
1fa79 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1fa7a 4f 70 33 28 76 2c 20 4f 50 5f 56 46 69 6c 74 65  Op3(v, OP_VFilte
1fa7b 72 2c 20 69 43 75 72 2c 20 62 72 6b 2c 20 70 42  r, iCur, brk, pB
1fa7c 65 73 74 49 64 78 2d 3e 69 64 78 53 74 72 2c 0a  estIdx->idxStr,.
1fa7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa7e 20 20 20 20 20 20 70 42 65 73 74 49 64 78 2d 3e        pBestIdx->
1fa7f 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 74 72  needToFreeIdxStr
1fa80 20 3f 20 50 33 5f 4d 50 52 49 4e 54 46 20 3a 20   ? P3_MPRINTF : 
1fa81 50 33 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  P3_STATIC);.    
1fa82 20 20 70 42 65 73 74 49 64 78 2d 3e 6e 65 65 64    pBestIdx->need
1fa83 54 6f 46 72 65 65 49 64 78 53 74 72 20 3d 20 30  ToFreeIdxStr = 0
1fa84 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b  ;.      for(j=0;
1fa85 20 6a 3c 70 42 65 73 74 49 64 78 2d 3e 6e 43 6f   j<pBestIdx->nCo
1fa86 6e 73 74 72 61 69 6e 74 3b 20 6a 2b 2b 29 7b 0a  nstraint; j++){.
1fa87 20 20 20 20 20 20 20 20 69 66 28 20 61 55 73 61          if( aUsa
1fa88 67 65 5b 6a 5d 2e 6f 6d 69 74 20 29 7b 0a 20 20  ge[j].omit ){.  
1fa89 20 20 20 20 20 20 20 20 69 6e 74 20 69 54 65 72          int iTer
1fa8a 6d 20 3d 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b  m = aConstraint[
1fa8b 6a 5d 2e 69 54 65 72 6d 4f 66 66 73 65 74 3b 0a  j].iTermOffset;.
1fa8c 20 20 20 20 20 20 20 20 20 20 64 69 73 61 62 6c            disabl
1fa8d 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c 20 26 77  eTerm(pLevel, &w
1fa8e 63 2e 61 5b 69 54 65 72 6d 5d 29 3b 0a 20 20 20  c.a[iTerm]);.   
1fa8f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
1fa90 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20       pLevel->op 
1fa91 3d 20 4f 50 5f 56 4e 65 78 74 3b 0a 20 20 20 20  = OP_VNext;.    
1fa92 20 20 70 4c 65 76 65 6c 2d 3e 70 31 20 3d 20 69    pLevel->p1 = i
1fa93 43 75 72 3b 0a 20 20 20 20 20 20 70 4c 65 76 65  Cur;.      pLeve
1fa94 6c 2d 3e 70 32 20 3d 20 73 71 6c 69 74 65 33 56  l->p2 = sqlite3V
1fa95 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
1fa96 29 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e  );.    }else.#en
1fa97 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
1fa98 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20  IT_VIRTUALTABLE 
1fa99 2a 2f 0a 0a 20 20 20 20 69 66 28 20 70 4c 65 76  */..    if( pLev
1fa9a 65 6c 2d 3e 66 6c 61 67 73 20 26 20 57 48 45 52  el->flags & WHER
1fa9b 45 5f 52 4f 57 49 44 5f 45 51 20 29 7b 0a 20 20  E_ROWID_EQ ){.  
1fa9c 20 20 20 20 2f 2a 20 43 61 73 65 20 31 3a 20 20      /* Case 1:  
1fa9d 57 65 20 63 61 6e 20 64 69 72 65 63 74 6c 79 20  We can directly 
1fa9e 72 65 66 65 72 65 6e 63 65 20 61 20 73 69 6e 67  reference a sing
1fa9f 6c 65 20 72 6f 77 20 75 73 69 6e 67 20 61 6e 0a  le row using an.
1faa0 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20        **        
1faa1 20 20 65 71 75 61 6c 69 74 79 20 63 6f 6d 70 61    equality compa
1faa2 72 69 73 6f 6e 20 61 67 61 69 6e 73 74 20 74 68  rison against th
1faa3 65 20 52 4f 57 49 44 20 66 69 65 6c 64 2e 20 20  e ROWID field.  
1faa4 4f 72 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20  Or.      **     
1faa5 20 20 20 20 20 77 65 20 72 65 66 65 72 65 6e 63       we referenc
1faa6 65 20 6d 75 6c 74 69 70 6c 65 20 72 6f 77 73 20  e multiple rows 
1faa7 75 73 69 6e 67 20 61 20 22 72 6f 77 69 64 20 49  using a "rowid I
1faa8 4e 20 28 2e 2e 2e 29 22 0a 20 20 20 20 20 20 2a  N (...)".      *
1faa9 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74  *          const
1faaa 72 75 63 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  ruct..      */. 
1faab 20 20 20 20 20 70 54 65 72 6d 20 3d 20 66 69 6e       pTerm = fin
1faac 64 54 65 72 6d 28 26 77 63 2c 20 69 43 75 72 2c  dTerm(&wc, iCur,
1faad 20 2d 31 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57   -1, notReady, W
1faae 4f 5f 45 51 7c 57 4f 5f 49 4e 2c 20 30 29 3b 0a  O_EQ|WO_IN, 0);.
1faaf 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54        assert( pT
1fab0 65 72 6d 21 3d 30 20 29 3b 0a 20 20 20 20 20 20  erm!=0 );.      
1fab1 61 73 73 65 72 74 28 20 70 54 65 72 6d 2d 3e 70  assert( pTerm->p
1fab2 45 78 70 72 21 3d 30 20 29 3b 0a 20 20 20 20 20  Expr!=0 );.     
1fab3 20 61 73 73 65 72 74 28 20 70 54 65 72 6d 2d 3e   assert( pTerm->
1fab4 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69 43 75 72  leftCursor==iCur
1fab5 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
1fab6 28 20 6f 6d 69 74 54 61 62 6c 65 3d 3d 30 20 29  ( omitTable==0 )
1fab7 3b 0a 20 20 20 20 20 20 63 6f 64 65 45 71 75 61  ;.      codeEqua
1fab8 6c 69 74 79 54 65 72 6d 28 70 50 61 72 73 65 2c  lityTerm(pParse,
1fab9 20 70 54 65 72 6d 2c 20 70 4c 65 76 65 6c 29 3b   pTerm, pLevel);
1faba 0a 20 20 20 20 20 20 6e 78 74 20 3d 20 70 4c 65  .      nxt = pLe
1fabb 76 65 6c 2d 3e 6e 78 74 3b 0a 20 20 20 20 20 20  vel->nxt;.      
1fabc 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1fabd 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74  (v, OP_MustBeInt
1fabe 2c 20 31 2c 20 6e 78 74 29 3b 0a 20 20 20 20 20  , 1, nxt);.     
1fabf 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1fac0 70 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74  p(v, OP_NotExist
1fac1 73 2c 20 69 43 75 72 2c 20 6e 78 74 29 3b 0a 20  s, iCur, nxt);. 
1fac2 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74       VdbeComment
1fac3 28 28 76 2c 20 22 70 6b 22 29 29 3b 0a 20 20 20  ((v, "pk"));.   
1fac4 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20     pLevel->op = 
1fac5 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20 7d 65 6c  OP_Noop;.    }el
1fac6 73 65 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 66  se if( pLevel->f
1fac7 6c 61 67 73 20 26 20 57 48 45 52 45 5f 52 4f 57  lags & WHERE_ROW
1fac8 49 44 5f 52 41 4e 47 45 20 29 7b 0a 20 20 20 20  ID_RANGE ){.    
1fac9 20 20 2f 2a 20 43 61 73 65 20 32 3a 20 20 57 65    /* Case 2:  We
1faca 20 68 61 76 65 20 61 6e 20 69 6e 65 71 75 61 6c   have an inequal
1facb 69 74 79 20 63 6f 6d 70 61 72 69 73 6f 6e 20 61  ity comparison a
1facc 67 61 69 6e 73 74 20 74 68 65 20 52 4f 57 49 44  gainst the ROWID
1facd 20 66 69 65 6c 64 2e 0a 20 20 20 20 20 20 2a 2f   field..      */
1face 0a 20 20 20 20 20 20 69 6e 74 20 74 65 73 74 4f  .      int testO
1facf 70 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20  p = OP_Noop;.   
1fad0 20 20 20 69 6e 74 20 73 74 61 72 74 3b 0a 20 20     int start;.  
1fad1 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70      WhereTerm *p
1fad2 53 74 61 72 74 2c 20 2a 70 45 6e 64 3b 0a 0a 20  Start, *pEnd;.. 
1fad3 20 20 20 20 20 61 73 73 65 72 74 28 20 6f 6d 69       assert( omi
1fad4 74 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20  tTable==0 );.   
1fad5 20 20 20 70 53 74 61 72 74 20 3d 20 66 69 6e 64     pStart = find
1fad6 54 65 72 6d 28 26 77 63 2c 20 69 43 75 72 2c 20  Term(&wc, iCur, 
1fad7 2d 31 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f  -1, notReady, WO
1fad8 5f 47 54 7c 57 4f 5f 47 45 2c 20 30 29 3b 0a 20  _GT|WO_GE, 0);. 
1fad9 20 20 20 20 20 70 45 6e 64 20 3d 20 66 69 6e 64       pEnd = find
1fada 54 65 72 6d 28 26 77 63 2c 20 69 43 75 72 2c 20  Term(&wc, iCur, 
1fadb 2d 31 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f  -1, notReady, WO
1fadc 5f 4c 54 7c 57 4f 5f 4c 45 2c 20 30 29 3b 0a 20  _LT|WO_LE, 0);. 
1fadd 20 20 20 20 20 69 66 28 20 62 52 65 76 20 29 7b       if( bRev ){
1fade 0a 20 20 20 20 20 20 20 20 70 54 65 72 6d 20 3d  .        pTerm =
1fadf 20 70 53 74 61 72 74 3b 0a 20 20 20 20 20 20 20   pStart;.       
1fae0 20 70 53 74 61 72 74 20 3d 20 70 45 6e 64 3b 0a   pStart = pEnd;.
1fae1 20 20 20 20 20 20 20 20 70 45 6e 64 20 3d 20 70          pEnd = p
1fae2 54 65 72 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Term;.      }.  
1fae3 20 20 20 20 69 66 28 20 70 53 74 61 72 74 20 29      if( pStart )
1fae4 7b 0a 20 20 20 20 20 20 20 20 45 78 70 72 20 2a  {.        Expr *
1fae5 70 58 3b 0a 20 20 20 20 20 20 20 20 70 58 20 3d  pX;.        pX =
1fae6 20 70 53 74 61 72 74 2d 3e 70 45 78 70 72 3b 0a   pStart->pExpr;.
1fae7 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1fae8 70 58 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  pX!=0 );.       
1fae9 20 61 73 73 65 72 74 28 20 70 53 74 61 72 74 2d   assert( pStart-
1faea 3e 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69 43 75  >leftCursor==iCu
1faeb 72 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  r );.        sql
1faec 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
1faed 72 73 65 2c 20 70 58 2d 3e 70 52 69 67 68 74 29  rse, pX->pRight)
1faee 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
1faef 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1faf0 5f 46 6f 72 63 65 49 6e 74 2c 20 70 58 2d 3e 6f  _ForceInt, pX->o
1faf1 70 3d 3d 54 4b 5f 4c 45 20 7c 7c 20 70 58 2d 3e  p==TK_LE || pX->
1faf2 6f 70 3d 3d 54 4b 5f 47 54 2c 20 62 72 6b 29 3b  op==TK_GT, brk);
1faf3 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1faf4 56 64 62 65 41 64 64 4f 70 28 76 2c 20 62 52 65  VdbeAddOp(v, bRe
1faf5 76 20 3f 20 4f 50 5f 4d 6f 76 65 4c 74 20 3a 20  v ? OP_MoveLt : 
1faf6 4f 50 5f 4d 6f 76 65 47 65 2c 20 69 43 75 72 2c  OP_MoveGe, iCur,
1faf7 20 62 72 6b 29 3b 0a 20 20 20 20 20 20 20 20 56   brk);.        V
1faf8 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
1faf9 70 6b 22 29 29 3b 0a 20 20 20 20 20 20 20 20 64  pk"));.        d
1fafa 69 73 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65  isableTerm(pLeve
1fafb 6c 2c 20 70 53 74 61 72 74 29 3b 0a 20 20 20 20  l, pStart);.    
1fafc 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1fafd 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1fafe 70 28 76 2c 20 62 52 65 76 20 3f 20 4f 50 5f 4c  p(v, bRev ? OP_L
1faff 61 73 74 20 3a 20 4f 50 5f 52 65 77 69 6e 64 2c  ast : OP_Rewind,
1fb00 20 69 43 75 72 2c 20 62 72 6b 29 3b 0a 20 20 20   iCur, brk);.   
1fb01 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70     }.      if( p
1fb02 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 20 20 45  End ){.        E
1fb03 78 70 72 20 2a 70 58 3b 0a 20 20 20 20 20 20 20  xpr *pX;.       
1fb04 20 70 58 20 3d 20 70 45 6e 64 2d 3e 70 45 78 70   pX = pEnd->pExp
1fb05 72 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  r;.        asser
1fb06 74 28 20 70 58 21 3d 30 20 29 3b 0a 20 20 20 20  t( pX!=0 );.    
1fb07 20 20 20 20 61 73 73 65 72 74 28 20 70 45 6e 64      assert( pEnd
1fb08 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69 43  ->leftCursor==iC
1fb09 75 72 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71  ur );.        sq
1fb0a 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
1fb0b 61 72 73 65 2c 20 70 58 2d 3e 70 52 69 67 68 74  arse, pX->pRight
1fb0c 29 3b 0a 20 20 20 20 20 20 20 20 70 4c 65 76 65  );.        pLeve
1fb0d 6c 2d 3e 69 4d 65 6d 20 3d 20 70 50 61 72 73 65  l->iMem = pParse
1fb0e 2d 3e 6e 4d 65 6d 2b 2b 3b 0a 20 20 20 20 20 20  ->nMem++;.      
1fb0f 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1fb10 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 53 74 6f 72  Op(v, OP_MemStor
1fb11 65 2c 20 70 4c 65 76 65 6c 2d 3e 69 4d 65 6d 2c  e, pLevel->iMem,
1fb12 20 31 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28   1);.        if(
1fb13 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 4c 54 20 7c   pX->op==TK_LT |
1fb14 7c 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 47 54 20  | pX->op==TK_GT 
1fb15 29 7b 0a 20 20 20 20 20 20 20 20 20 20 74 65 73  ){.          tes
1fb16 74 4f 70 20 3d 20 62 52 65 76 20 3f 20 4f 50 5f  tOp = bRev ? OP_
1fb17 4c 65 20 3a 20 4f 50 5f 47 65 3b 0a 20 20 20 20  Le : OP_Ge;.    
1fb18 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1fb19 20 20 20 20 20 74 65 73 74 4f 70 20 3d 20 62 52       testOp = bR
1fb1a 65 76 20 3f 20 4f 50 5f 4c 74 20 3a 20 4f 50 5f  ev ? OP_Lt : OP_
1fb1b 47 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  Gt;.        }.  
1fb1c 20 20 20 20 20 20 64 69 73 61 62 6c 65 54 65 72        disableTer
1fb1d 6d 28 70 4c 65 76 65 6c 2c 20 70 45 6e 64 29 3b  m(pLevel, pEnd);
1fb1e 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
1fb1f 74 61 72 74 20 3d 20 73 71 6c 69 74 65 33 56 64  tart = sqlite3Vd
1fb20 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
1fb21 3b 0a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e  ;.      pLevel->
1fb22 6f 70 20 3d 20 62 52 65 76 20 3f 20 4f 50 5f 50  op = bRev ? OP_P
1fb23 72 65 76 20 3a 20 4f 50 5f 4e 65 78 74 3b 0a 20  rev : OP_Next;. 
1fb24 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 31 20       pLevel->p1 
1fb25 3d 20 69 43 75 72 3b 0a 20 20 20 20 20 20 70 4c  = iCur;.      pL
1fb26 65 76 65 6c 2d 3e 70 32 20 3d 20 73 74 61 72 74  evel->p2 = start
1fb27 3b 0a 20 20 20 20 20 20 69 66 28 20 74 65 73 74  ;.      if( test
1fb28 4f 70 21 3d 4f 50 5f 4e 6f 6f 70 20 29 7b 0a 20  Op!=OP_Noop ){. 
1fb29 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1fb2a 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 52 6f  beAddOp(v, OP_Ro
1fb2b 77 69 64 2c 20 69 43 75 72 2c 20 30 29 3b 0a 20  wid, iCur, 0);. 
1fb2c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1fb2d 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65  beAddOp(v, OP_Me
1fb2e 6d 4c 6f 61 64 2c 20 70 4c 65 76 65 6c 2d 3e 69  mLoad, pLevel->i
1fb2f 4d 65 6d 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  Mem, 0);.       
1fb30 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1fb31 70 28 76 2c 20 74 65 73 74 4f 70 2c 20 53 51 4c  p(v, testOp, SQL
1fb32 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 7c  ITE_AFF_NUMERIC|
1fb33 30 78 31 30 30 2c 20 62 72 6b 29 3b 0a 20 20 20  0x100, brk);.   
1fb34 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69     }.    }else i
1fb35 66 28 20 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73  f( pLevel->flags
1fb36 20 26 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f   & WHERE_COLUMN_
1fb37 52 41 4e 47 45 20 29 7b 0a 20 20 20 20 20 20 2f  RANGE ){.      /
1fb38 2a 20 43 61 73 65 20 33 3a 20 54 68 65 20 57 48  * Case 3: The WH
1fb39 45 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d 20  ERE clause term 
1fb3a 74 68 61 74 20 72 65 66 65 72 73 20 74 6f 20 74  that refers to t
1fb3b 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 0a 20 20  he right-most.  
1fb3c 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 63      **         c
1fb3d 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 69 6e 64  olumn of the ind
1fb3e 65 78 20 69 73 20 61 6e 20 69 6e 65 71 75 61 6c  ex is an inequal
1fb3f 69 74 79 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c  ity.  For exampl
1fb40 65 2c 20 69 66 0a 20 20 20 20 20 20 2a 2a 20 20  e, if.      **  
1fb41 20 20 20 20 20 20 20 74 68 65 20 69 6e 64 65 78         the index
1fb42 20 69 73 20 6f 6e 20 28 78 2c 79 2c 7a 29 20 61   is on (x,y,z) a
1fb43 6e 64 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  nd the WHERE cla
1fb44 75 73 65 20 69 73 20 6f 66 20 74 68 65 0a 20 20  use is of the.  
1fb45 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 66      **         f
1fb46 6f 72 6d 20 22 78 3d 35 20 41 4e 44 20 79 3c 31  orm "x=5 AND y<1
1fb47 30 22 20 74 68 65 6e 20 74 68 69 73 20 63 61 73  0" then this cas
1fb48 65 20 69 73 20 75 73 65 64 2e 20 20 4f 6e 6c 79  e is used.  Only
1fb49 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 20 20   the.      **   
1fb4a 20 20 20 20 20 20 72 69 67 68 74 2d 6d 6f 73 74        right-most
1fb4b 20 63 6f 6c 75 6d 6e 20 63 61 6e 20 62 65 20 61   column can be a
1fb4c 6e 20 69 6e 65 71 75 61 6c 69 74 79 20 2d 20 74  n inequality - t
1fb4d 68 65 20 72 65 73 74 20 6d 75 73 74 0a 20 20 20  he rest must.   
1fb4e 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 75 73     **         us
1fb4f 65 20 74 68 65 20 22 3d 3d 22 20 61 6e 64 20 22  e the "==" and "
1fb50 49 4e 22 20 6f 70 65 72 61 74 6f 72 73 2e 0a 20  IN" operators.. 
1fb51 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
1fb52 20 20 20 20 20 20 20 20 20 54 68 69 73 20 63 61           This ca
1fb53 73 65 20 69 73 20 61 6c 73 6f 20 75 73 65 64 20  se is also used 
1fb54 77 68 65 6e 20 74 68 65 72 65 20 61 72 65 20 6e  when there are n
1fb55 6f 20 57 48 45 52 45 20 63 6c 61 75 73 65 0a 20  o WHERE clause. 
1fb56 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20       **         
1fb57 63 6f 6e 73 74 72 61 69 6e 74 73 20 62 75 74 20  constraints but 
1fb58 61 6e 20 69 6e 64 65 78 20 69 73 20 73 65 6c 65  an index is sele
1fb59 63 74 65 64 20 61 6e 79 77 61 79 2c 20 69 6e 20  cted anyway, in 
1fb5a 6f 72 64 65 72 0a 20 20 20 20 20 20 2a 2a 20 20  order.      **  
1fb5b 20 20 20 20 20 20 20 74 6f 20 66 6f 72 63 65 20         to force 
1fb5c 74 68 65 20 6f 75 74 70 75 74 20 6f 72 64 65 72  the output order
1fb5d 20 74 6f 20 63 6f 6e 66 6f 72 6d 20 74 6f 20 61   to conform to a
1fb5e 6e 20 4f 52 44 45 52 20 42 59 2e 0a 20 20 20 20  n ORDER BY..    
1fb5f 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 73    */.      int s
1fb60 74 61 72 74 3b 0a 20 20 20 20 20 20 69 6e 74 20  tart;.      int 
1fb61 6e 45 71 20 3d 20 70 4c 65 76 65 6c 2d 3e 6e 45  nEq = pLevel->nE
1fb62 71 3b 0a 20 20 20 20 20 20 69 6e 74 20 74 6f 70  q;.      int top
1fb63 45 71 3d 30 3b 20 20 20 20 20 20 20 20 2f 2a 20  Eq=0;        /* 
1fb64 54 72 75 65 20 69 66 20 74 6f 70 20 6c 69 6d 69  True if top limi
1fb65 74 20 75 73 65 73 20 3d 3d 2e 20 46 61 6c 73 65  t uses ==. False
1fb66 20 69 73 20 73 74 72 69 63 74 6c 79 20 3c 20 2a   is strictly < *
1fb67 2f 0a 20 20 20 20 20 20 69 6e 74 20 62 74 6d 45  /.      int btmE
1fb68 71 3d 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  q=0;        /* T
1fb69 72 75 65 20 69 66 20 62 74 6d 20 6c 69 6d 69 74  rue if btm limit
1fb6a 20 75 73 65 73 20 3d 3d 2e 20 46 61 6c 73 65 20   uses ==. False 
1fb6b 69 66 20 73 74 72 69 63 74 6c 79 20 3e 20 2a 2f  if strictly > */
1fb6c 0a 20 20 20 20 20 20 69 6e 74 20 74 6f 70 4f 70  .      int topOp
1fb6d 2c 20 62 74 6d 4f 70 3b 20 20 20 2f 2a 20 4f 70  , btmOp;   /* Op
1fb6e 65 72 61 74 6f 72 73 20 66 6f 72 20 74 68 65 20  erators for the 
1fb6f 74 6f 70 20 61 6e 64 20 62 6f 74 74 6f 6d 20 73  top and bottom s
1fb70 65 61 72 63 68 20 62 6f 75 6e 64 73 20 2a 2f 0a  earch bounds */.
1fb71 20 20 20 20 20 20 69 6e 74 20 74 65 73 74 4f 70        int testOp
1fb72 3b 0a 20 20 20 20 20 20 69 6e 74 20 74 6f 70 4c  ;.      int topL
1fb73 69 6d 69 74 20 3d 20 28 70 4c 65 76 65 6c 2d 3e  imit = (pLevel->
1fb74 66 6c 61 67 73 20 26 20 57 48 45 52 45 5f 54 4f  flags & WHERE_TO
1fb75 50 5f 4c 49 4d 49 54 29 21 3d 30 3b 0a 20 20 20  P_LIMIT)!=0;.   
1fb76 20 20 20 69 6e 74 20 62 74 6d 4c 69 6d 69 74 20     int btmLimit 
1fb77 3d 20 28 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73  = (pLevel->flags
1fb78 20 26 20 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d   & WHERE_BTM_LIM
1fb79 49 54 29 21 3d 30 3b 0a 0a 20 20 20 20 20 20 2f  IT)!=0;..      /
1fb7a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
1fb7b 74 6f 20 65 76 61 6c 75 61 74 65 20 61 6c 6c 20  to evaluate all 
1fb7c 63 6f 6e 73 74 72 61 69 6e 74 20 74 65 72 6d 73  constraint terms
1fb7d 20 75 73 69 6e 67 20 3d 3d 20 6f 72 20 49 4e 0a   using == or IN.
1fb7e 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 6c 65 76        ** and lev
1fb7f 65 6c 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66  el the values of
1fb80 20 74 68 6f 73 65 20 74 65 72 6d 73 20 6f 6e 20   those terms on 
1fb81 74 68 65 20 73 74 61 63 6b 2e 0a 20 20 20 20 20  the stack..     
1fb82 20 2a 2f 0a 20 20 20 20 20 20 63 6f 64 65 41 6c   */.      codeAl
1fb83 6c 45 71 75 61 6c 69 74 79 54 65 72 6d 73 28 70  lEqualityTerms(p
1fb84 50 61 72 73 65 2c 20 70 4c 65 76 65 6c 2c 20 26  Parse, pLevel, &
1fb85 77 63 2c 20 6e 6f 74 52 65 61 64 79 29 3b 0a 0a  wc, notReady);..
1fb86 20 20 20 20 20 20 2f 2a 20 44 75 70 6c 69 63 61        /* Duplica
1fb87 74 65 20 74 68 65 20 65 71 75 61 6c 69 74 79 20  te the equality 
1fb88 74 65 72 6d 20 76 61 6c 75 65 73 20 62 65 63 61  term values beca
1fb89 75 73 65 20 74 68 65 79 20 77 69 6c 6c 20 61 6c  use they will al
1fb8a 6c 20 62 65 0a 20 20 20 20 20 20 2a 2a 20 75 73  l be.      ** us
1fb8b 65 64 20 74 77 69 63 65 3a 20 6f 6e 63 65 20 74  ed twice: once t
1fb8c 6f 20 6d 61 6b 65 20 74 68 65 20 74 65 72 6d 69  o make the termi
1fb8d 6e 61 74 69 6f 6e 20 6b 65 79 20 61 6e 64 20 6f  nation key and o
1fb8e 6e 63 65 20 74 6f 20 6d 61 6b 65 20 74 68 65 0a  nce to make the.
1fb8f 20 20 20 20 20 20 2a 2a 20 73 74 61 72 74 20 6b        ** start k
1fb90 65 79 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ey..      */.   
1fb91 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 45     for(j=0; j<nE
1fb92 71 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  q; j++){.       
1fb93 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1fb94 70 28 76 2c 20 4f 50 5f 44 75 70 2c 20 6e 45 71  p(v, OP_Dup, nEq
1fb95 2d 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a  -1, 0);.      }.
1fb96 0a 20 20 20 20 20 20 2f 2a 20 46 69 67 75 72 65  .      /* Figure
1fb97 20 6f 75 74 20 77 68 61 74 20 63 6f 6d 70 61 72   out what compar
1fb98 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 73 20 74  ison operators t
1fb99 6f 20 75 73 65 20 66 6f 72 20 74 6f 70 20 61 6e  o use for top an
1fb9a 64 20 62 6f 74 74 6f 6d 20 0a 20 20 20 20 20 20  d bottom .      
1fb9b 2a 2a 20 73 65 61 72 63 68 20 62 6f 75 6e 64 73  ** search bounds
1fb9c 2e 20 46 6f 72 20 61 6e 20 61 73 63 65 6e 64 69  . For an ascendi
1fb9d 6e 67 20 69 6e 64 65 78 2c 20 74 68 65 20 62 6f  ng index, the bo
1fb9e 74 74 6f 6d 20 62 6f 75 6e 64 20 69 73 20 61 20  ttom bound is a 
1fb9f 3e 20 6f 72 20 3e 3d 0a 20 20 20 20 20 20 2a 2a  > or >=.      **
1fba0 20 6f 70 65 72 61 74 6f 72 20 61 6e 64 20 74 68   operator and th
1fba1 65 20 74 6f 70 20 62 6f 75 6e 64 20 69 73 20 61  e top bound is a
1fba2 20 3c 20 6f 72 20 3c 3d 20 6f 70 65 72 61 74 6f   < or <= operato
1fba3 72 2e 20 20 46 6f 72 20 61 20 64 65 73 63 65 6e  r.  For a descen
1fba4 64 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 69 6e  ding.      ** in
1fba5 64 65 78 20 74 68 65 20 6f 70 65 72 61 74 6f 72  dex the operator
1fba6 73 20 61 72 65 20 72 65 76 65 72 73 65 64 2e 0a  s are reversed..
1fba7 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
1fba8 66 28 20 70 49 64 78 2d 3e 61 53 6f 72 74 4f 72  f( pIdx->aSortOr
1fba9 64 65 72 5b 6e 45 71 5d 3d 3d 53 51 4c 49 54 45  der[nEq]==SQLITE
1fbaa 5f 53 4f 5f 41 53 43 20 29 7b 0a 20 20 20 20 20  _SO_ASC ){.     
1fbab 20 20 20 74 6f 70 4f 70 20 3d 20 57 4f 5f 4c 54     topOp = WO_LT
1fbac 7c 57 4f 5f 4c 45 3b 0a 20 20 20 20 20 20 20 20  |WO_LE;.        
1fbad 62 74 6d 4f 70 20 3d 20 57 4f 5f 47 54 7c 57 4f  btmOp = WO_GT|WO
1fbae 5f 47 45 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  _GE;.      }else
1fbaf 7b 0a 20 20 20 20 20 20 20 20 74 6f 70 4f 70 20  {.        topOp 
1fbb0 3d 20 57 4f 5f 47 54 7c 57 4f 5f 47 45 3b 0a 20  = WO_GT|WO_GE;. 
1fbb1 20 20 20 20 20 20 20 62 74 6d 4f 70 20 3d 20 57         btmOp = W
1fbb2 4f 5f 4c 54 7c 57 4f 5f 4c 45 3b 0a 20 20 20 20  O_LT|WO_LE;.    
1fbb3 20 20 20 20 53 57 41 50 28 69 6e 74 2c 20 74 6f      SWAP(int, to
1fbb4 70 4c 69 6d 69 74 2c 20 62 74 6d 4c 69 6d 69 74  pLimit, btmLimit
1fbb5 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  );.      }..    
1fbb6 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 74 68    /* Generate th
1fbb7 65 20 74 65 72 6d 69 6e 61 74 69 6f 6e 20 6b 65  e termination ke
1fbb8 79 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20  y.  This is the 
1fbb9 6b 65 79 20 76 61 6c 75 65 20 74 68 61 74 0a 20  key value that. 
1fbba 20 20 20 20 20 2a 2a 20 77 69 6c 6c 20 65 6e 64       ** will end
1fbbb 20 74 68 65 20 73 65 61 72 63 68 2e 20 20 54 68   the search.  Th
1fbbc 65 72 65 20 69 73 20 6e 6f 20 74 65 72 6d 69 6e  ere is no termin
1fbbd 61 74 69 6f 6e 20 6b 65 79 20 69 66 20 74 68 65  ation key if the
1fbbe 72 65 0a 20 20 20 20 20 20 2a 2a 20 61 72 65 20  re.      ** are 
1fbbf 6e 6f 20 65 71 75 61 6c 69 74 79 20 74 65 72 6d  no equality term
1fbc0 73 20 61 6e 64 20 6e 6f 20 22 58 3c 2e 2e 2e 22  s and no "X<..."
1fbc1 20 74 65 72 6d 2e 0a 20 20 20 20 20 20 2a 2a 0a   term..      **.
1fbc2 20 20 20 20 20 20 2a 2a 20 32 30 30 32 2d 44 65        ** 2002-De
1fbc3 63 2d 30 34 3a 20 4f 6e 20 61 20 72 65 76 65 72  c-04: On a rever
1fbc4 73 65 2d 6f 72 64 65 72 20 73 63 61 6e 2c 20 74  se-order scan, t
1fbc5 68 65 20 73 6f 2d 63 61 6c 6c 65 64 20 22 74 65  he so-called "te
1fbc6 72 6d 69 6e 61 74 69 6f 6e 22 0a 20 20 20 20 20  rmination".     
1fbc7 20 2a 2a 20 6b 65 79 20 63 6f 6d 70 75 74 65 64   ** key computed
1fbc8 20 68 65 72 65 20 72 65 61 6c 6c 79 20 65 6e 64   here really end
1fbc9 73 20 75 70 20 62 65 69 6e 67 20 74 68 65 20 73  s up being the s
1fbca 74 61 72 74 20 6b 65 79 2e 0a 20 20 20 20 20 20  tart key..      
1fbcb 2a 2f 0a 20 20 20 20 20 20 6e 78 74 20 3d 20 70  */.      nxt = p
1fbcc 4c 65 76 65 6c 2d 3e 6e 78 74 3b 0a 20 20 20 20  Level->nxt;.    
1fbcd 20 20 69 66 28 20 74 6f 70 4c 69 6d 69 74 20 29    if( topLimit )
1fbce 7b 0a 20 20 20 20 20 20 20 20 45 78 70 72 20 2a  {.        Expr *
1fbcf 70 58 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  pX;.        int 
1fbd0 6b 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75  k = pIdx->aiColu
1fbd1 6d 6e 5b 6a 5d 3b 0a 20 20 20 20 20 20 20 20 70  mn[j];.        p
1fbd2 54 65 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28  Term = findTerm(
1fbd3 26 77 63 2c 20 69 43 75 72 2c 20 6b 2c 20 6e 6f  &wc, iCur, k, no
1fbd4 74 52 65 61 64 79 2c 20 74 6f 70 4f 70 2c 20 70  tReady, topOp, p
1fbd5 49 64 78 29 3b 0a 20 20 20 20 20 20 20 20 61 73  Idx);.        as
1fbd6 73 65 72 74 28 20 70 54 65 72 6d 21 3d 30 20 29  sert( pTerm!=0 )
1fbd7 3b 0a 20 20 20 20 20 20 20 20 70 58 20 3d 20 70  ;.        pX = p
1fbd8 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20  Term->pExpr;.   
1fbd9 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 54       assert( (pT
1fbda 65 72 6d 2d 3e 66 6c 61 67 73 20 26 20 54 45 52  erm->flags & TER
1fbdb 4d 5f 43 4f 44 45 44 29 3d 3d 30 20 29 3b 0a 20  M_CODED)==0 );. 
1fbdc 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
1fbdd 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70  prCode(pParse, p
1fbde 58 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 20 20  X->pRight);.    
1fbdf 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1fbe0 64 64 4f 70 28 76 2c 20 4f 50 5f 49 73 4e 75 6c  ddOp(v, OP_IsNul
1fbe1 6c 2c 20 2d 28 6e 45 71 2a 32 2b 31 29 2c 20 6e  l, -(nEq*2+1), n
1fbe2 78 74 29 3b 0a 20 20 20 20 20 20 20 20 74 6f 70  xt);.        top
1fbe3 45 71 20 3d 20 70 54 65 72 6d 2d 3e 65 4f 70 65  Eq = pTerm->eOpe
1fbe4 72 61 74 6f 72 20 26 20 28 57 4f 5f 4c 45 7c 57  rator & (WO_LE|W
1fbe5 4f 5f 47 45 29 3b 0a 20 20 20 20 20 20 20 20 64  O_GE);.        d
1fbe6 69 73 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65  isableTerm(pLeve
1fbe7 6c 2c 20 70 54 65 72 6d 29 3b 0a 20 20 20 20 20  l, pTerm);.     
1fbe8 20 20 20 74 65 73 74 4f 70 20 3d 20 4f 50 5f 49     testOp = OP_I
1fbe9 64 78 47 45 3b 0a 20 20 20 20 20 20 7d 65 6c 73  dxGE;.      }els
1fbea 65 7b 0a 20 20 20 20 20 20 20 20 74 65 73 74 4f  e{.        testO
1fbeb 70 20 3d 20 6e 45 71 3e 30 20 3f 20 4f 50 5f 49  p = nEq>0 ? OP_I
1fbec 64 78 47 45 20 3a 20 4f 50 5f 4e 6f 6f 70 3b 0a  dxGE : OP_Noop;.
1fbed 20 20 20 20 20 20 20 20 74 6f 70 45 71 20 3d 20          topEq = 
1fbee 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  1;.      }.     
1fbef 20 69 66 28 20 74 65 73 74 4f 70 21 3d 4f 50 5f   if( testOp!=OP_
1fbf0 4e 6f 6f 70 20 29 7b 0a 20 20 20 20 20 20 20 20  Noop ){.        
1fbf1 69 6e 74 20 6e 43 6f 6c 20 3d 20 6e 45 71 20 2b  int nCol = nEq +
1fbf2 20 74 6f 70 4c 69 6d 69 74 3b 0a 20 20 20 20 20   topLimit;.     
1fbf3 20 20 20 70 4c 65 76 65 6c 2d 3e 69 4d 65 6d 20     pLevel->iMem 
1fbf4 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 2b  = pParse->nMem++
1fbf5 3b 0a 20 20 20 20 20 20 20 20 62 75 69 6c 64 49  ;.        buildI
1fbf6 6e 64 65 78 50 72 6f 62 65 28 76 2c 20 6e 43 6f  ndexProbe(v, nCo
1fbf7 6c 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 20 20  l, pIdx);.      
1fbf8 20 20 69 66 28 20 62 52 65 76 20 29 7b 0a 20 20    if( bRev ){.  
1fbf9 20 20 20 20 20 20 20 20 69 6e 74 20 6f 70 20 3d          int op =
1fbfa 20 74 6f 70 45 71 20 3f 20 4f 50 5f 4d 6f 76 65   topEq ? OP_Move
1fbfb 4c 65 20 3a 20 4f 50 5f 4d 6f 76 65 4c 74 3b 0a  Le : OP_MoveLt;.
1fbfc 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1fbfd 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 6f 70  3VdbeAddOp(v, op
1fbfe 2c 20 69 49 64 78 43 75 72 2c 20 6e 78 74 29 3b  , iIdxCur, nxt);
1fbff 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
1fc00 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1fc01 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50  3VdbeAddOp(v, OP
1fc02 5f 4d 65 6d 53 74 6f 72 65 2c 20 70 4c 65 76 65  _MemStore, pLeve
1fc03 6c 2d 3e 69 4d 65 6d 2c 20 31 29 3b 0a 20 20 20  l->iMem, 1);.   
1fc04 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
1fc05 73 65 20 69 66 28 20 62 52 65 76 20 29 7b 0a 20  se if( bRev ){. 
1fc06 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1fc07 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4c 61  beAddOp(v, OP_La
1fc08 73 74 2c 20 69 49 64 78 43 75 72 2c 20 62 72 6b  st, iIdxCur, brk
1fc09 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  );.      }..    
1fc0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 74 68    /* Generate th
1fc0b 65 20 73 74 61 72 74 20 6b 65 79 2e 20 20 54 68  e start key.  Th
1fc0c 69 73 20 69 73 20 74 68 65 20 6b 65 79 20 74 68  is is the key th
1fc0d 61 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 6c  at defines the l
1fc0e 6f 77 65 72 0a 20 20 20 20 20 20 2a 2a 20 62 6f  ower.      ** bo
1fc0f 75 6e 64 20 6f 6e 20 74 68 65 20 73 65 61 72 63  und on the searc
1fc10 68 2e 20 20 54 68 65 72 65 20 69 73 20 6e 6f 20  h.  There is no 
1fc11 73 74 61 72 74 20 6b 65 79 20 69 66 20 74 68 65  start key if the
1fc12 72 65 20 61 72 65 20 6e 6f 0a 20 20 20 20 20 20  re are no.      
1fc13 2a 2a 20 65 71 75 61 6c 69 74 79 20 74 65 72 6d  ** equality term
1fc14 73 20 61 6e 64 20 69 66 20 74 68 65 72 65 20 69  s and if there i
1fc15 73 20 6e 6f 20 22 58 3e 2e 2e 2e 22 20 74 65 72  s no "X>..." ter
1fc16 6d 2e 20 20 49 6e 0a 20 20 20 20 20 20 2a 2a 20  m.  In.      ** 
1fc17 74 68 61 74 20 63 61 73 65 2c 20 67 65 6e 65 72  that case, gener
1fc18 61 74 65 20 61 20 22 52 65 77 69 6e 64 22 20 69  ate a "Rewind" i
1fc19 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 20 70 6c  nstruction in pl
1fc1a 61 63 65 20 6f 66 20 74 68 65 0a 20 20 20 20 20  ace of the.     
1fc1b 20 2a 2a 20 73 74 61 72 74 20 6b 65 79 20 73 65   ** start key se
1fc1c 61 72 63 68 2e 0a 20 20 20 20 20 20 2a 2a 0a 20  arch..      **. 
1fc1d 20 20 20 20 20 2a 2a 20 32 30 30 32 2d 44 65 63       ** 2002-Dec
1fc1e 2d 30 34 3a 20 49 6e 20 74 68 65 20 63 61 73 65  -04: In the case
1fc1f 20 6f 66 20 61 20 72 65 76 65 72 73 65 2d 6f 72   of a reverse-or
1fc20 64 65 72 20 73 65 61 72 63 68 2c 20 74 68 65 20  der search, the 
1fc21 73 6f 2d 63 61 6c 6c 65 64 0a 20 20 20 20 20 20  so-called.      
1fc22 2a 2a 20 22 73 74 61 72 74 22 20 6b 65 79 20 72  ** "start" key r
1fc23 65 61 6c 6c 79 20 65 6e 64 73 20 75 70 20 62 65  eally ends up be
1fc24 69 6e 67 20 75 73 65 64 20 61 73 20 74 68 65 20  ing used as the 
1fc25 74 65 72 6d 69 6e 61 74 69 6f 6e 20 6b 65 79 2e  termination key.
1fc26 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
1fc27 69 66 28 20 62 74 6d 4c 69 6d 69 74 20 29 7b 0a  if( btmLimit ){.
1fc28 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 58          Expr *pX
1fc29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6b 20  ;.        int k 
1fc2a 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e  = pIdx->aiColumn
1fc2b 5b 6a 5d 3b 0a 20 20 20 20 20 20 20 20 70 54 65  [j];.        pTe
1fc2c 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28 26 77  rm = findTerm(&w
1fc2d 63 2c 20 69 43 75 72 2c 20 6b 2c 20 6e 6f 74 52  c, iCur, k, notR
1fc2e 65 61 64 79 2c 20 62 74 6d 4f 70 2c 20 70 49 64  eady, btmOp, pId
1fc2f 78 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  x);.        asse
1fc30 72 74 28 20 70 54 65 72 6d 21 3d 30 20 29 3b 0a  rt( pTerm!=0 );.
1fc31 20 20 20 20 20 20 20 20 70 58 20 3d 20 70 54 65          pX = pTe
1fc32 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20  rm->pExpr;.     
1fc33 20 20 20 61 73 73 65 72 74 28 20 28 70 54 65 72     assert( (pTer
1fc34 6d 2d 3e 66 6c 61 67 73 20 26 20 54 45 52 4d 5f  m->flags & TERM_
1fc35 43 4f 44 45 44 29 3d 3d 30 20 29 3b 0a 20 20 20  CODED)==0 );.   
1fc36 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
1fc37 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 58 2d  Code(pParse, pX-
1fc38 3e 70 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20  >pRight);.      
1fc39 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1fc3a 4f 70 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c  Op(v, OP_IsNull,
1fc3b 20 2d 28 6e 45 71 2b 31 29 2c 20 6e 78 74 29 3b   -(nEq+1), nxt);
1fc3c 0a 20 20 20 20 20 20 20 20 62 74 6d 45 71 20 3d  .        btmEq =
1fc3d 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f   pTerm->eOperato
1fc3e 72 20 26 20 28 57 4f 5f 4c 45 7c 57 4f 5f 47 45  r & (WO_LE|WO_GE
1fc3f 29 3b 0a 20 20 20 20 20 20 20 20 64 69 73 61 62  );.        disab
1fc40 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70  leTerm(pLevel, p
1fc41 54 65 72 6d 29 3b 0a 20 20 20 20 20 20 7d 65 6c  Term);.      }el
1fc42 73 65 7b 0a 20 20 20 20 20 20 20 20 62 74 6d 45  se{.        btmE
1fc43 71 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  q = 1;.      }. 
1fc44 20 20 20 20 20 69 66 28 20 6e 45 71 3e 30 20 7c       if( nEq>0 |
1fc45 7c 20 62 74 6d 4c 69 6d 69 74 20 29 7b 0a 20 20  | btmLimit ){.  
1fc46 20 20 20 20 20 20 69 6e 74 20 6e 43 6f 6c 20 3d        int nCol =
1fc47 20 6e 45 71 20 2b 20 62 74 6d 4c 69 6d 69 74 3b   nEq + btmLimit;
1fc48 0a 20 20 20 20 20 20 20 20 62 75 69 6c 64 49 6e  .        buildIn
1fc49 64 65 78 50 72 6f 62 65 28 76 2c 20 6e 43 6f 6c  dexProbe(v, nCol
1fc4a 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 20 20 20  , pIdx);.       
1fc4b 20 69 66 28 20 62 52 65 76 20 29 7b 0a 20 20 20   if( bRev ){.   
1fc4c 20 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 69         pLevel->i
1fc4d 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d  Mem = pParse->nM
1fc4e 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20  em++;.          
1fc4f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1fc50 28 76 2c 20 4f 50 5f 4d 65 6d 53 74 6f 72 65 2c  (v, OP_MemStore,
1fc51 20 70 4c 65 76 65 6c 2d 3e 69 4d 65 6d 2c 20 31   pLevel->iMem, 1
1fc52 29 3b 0a 20 20 20 20 20 20 20 20 20 20 74 65 73  );.          tes
1fc53 74 4f 70 20 3d 20 4f 50 5f 49 64 78 4c 54 3b 0a  tOp = OP_IdxLT;.
1fc54 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
1fc55 20 20 20 20 20 20 20 20 20 69 6e 74 20 6f 70 20           int op 
1fc56 3d 20 62 74 6d 45 71 20 3f 20 4f 50 5f 4d 6f 76  = btmEq ? OP_Mov
1fc57 65 47 65 20 3a 20 4f 50 5f 4d 6f 76 65 47 74 3b  eGe : OP_MoveGt;
1fc58 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
1fc59 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20 6f  e3VdbeAddOp(v, o
1fc5a 70 2c 20 69 49 64 78 43 75 72 2c 20 6e 78 74 29  p, iIdxCur, nxt)
1fc5b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1fc5c 20 20 7d 65 6c 73 65 20 69 66 28 20 62 52 65 76    }else if( bRev
1fc5d 20 29 7b 0a 20 20 20 20 20 20 20 20 74 65 73 74   ){.        test
1fc5e 4f 70 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20  Op = OP_Noop;.  
1fc5f 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1fc60 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1fc61 64 4f 70 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64  dOp(v, OP_Rewind
1fc62 2c 20 69 49 64 78 43 75 72 2c 20 62 72 6b 29 3b  , iIdxCur, brk);
1fc63 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
1fc64 2f 2a 20 47 65 6e 65 72 61 74 65 20 74 68 65 20  /* Generate the 
1fc65 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 6c  the top of the l
1fc66 6f 6f 70 2e 20 20 49 66 20 74 68 65 72 65 20 69  oop.  If there i
1fc67 73 20 61 20 74 65 72 6d 69 6e 61 74 69 6f 6e 0a  s a termination.
1fc68 20 20 20 20 20 20 2a 2a 20 6b 65 79 20 77 65 20        ** key we 
1fc69 68 61 76 65 20 74 6f 20 74 65 73 74 20 66 6f 72  have to test for
1fc6a 20 74 68 61 74 20 6b 65 79 20 61 6e 64 20 61 62   that key and ab
1fc6b 6f 72 74 20 61 74 20 74 68 65 20 74 6f 70 20 6f  ort at the top o
1fc6c 66 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 6c  f the.      ** l
1fc6d 6f 6f 70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  oop..      */.  
1fc6e 20 20 20 20 73 74 61 72 74 20 3d 20 73 71 6c 69      start = sqli
1fc6f 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
1fc70 64 72 28 76 29 3b 0a 20 20 20 20 20 20 69 66 28  dr(v);.      if(
1fc71 20 74 65 73 74 4f 70 21 3d 4f 50 5f 4e 6f 6f 70   testOp!=OP_Noop
1fc72 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
1fc73 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1fc74 4f 50 5f 4d 65 6d 4c 6f 61 64 2c 20 70 4c 65 76  OP_MemLoad, pLev
1fc75 65 6c 2d 3e 69 4d 65 6d 2c 20 30 29 3b 0a 20 20  el->iMem, 0);.  
1fc76 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1fc77 65 41 64 64 4f 70 28 76 2c 20 74 65 73 74 4f 70  eAddOp(v, testOp
1fc78 2c 20 69 49 64 78 43 75 72 2c 20 6e 78 74 29 3b  , iIdxCur, nxt);
1fc79 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 74 6f  .        if( (to
1fc7a 70 45 71 20 26 26 20 21 62 52 65 76 29 20 7c 7c  pEq && !bRev) ||
1fc7b 20 28 21 62 74 6d 45 71 20 26 26 20 62 52 65 76   (!btmEq && bRev
1fc7c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  ) ){.          s
1fc7d 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1fc7e 50 33 28 76 2c 20 2d 31 2c 20 22 2b 22 2c 20 50  P3(v, -1, "+", P
1fc7f 33 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  3_STATIC);.     
1fc80 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
1fc81 20 20 20 69 66 28 20 74 6f 70 4c 69 6d 69 74 20     if( topLimit 
1fc82 7c 20 62 74 6d 4c 69 6d 69 74 20 29 7b 0a 20 20  | btmLimit ){.  
1fc83 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1fc84 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 43 6f 6c  eAddOp(v, OP_Col
1fc85 75 6d 6e 2c 20 69 49 64 78 43 75 72 2c 20 6e 45  umn, iIdxCur, nE
1fc86 71 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  q);.        sqli
1fc87 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c 20  te3VdbeAddOp(v, 
1fc88 4f 50 5f 49 73 4e 75 6c 6c 2c 20 31 2c 20 63 6f  OP_IsNull, 1, co
1fc89 6e 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  nt);.      }.   
1fc8a 20 20 20 69 66 28 20 21 6f 6d 69 74 54 61 62 6c     if( !omitTabl
1fc8b 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  e ){.        sql
1fc8c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1fc8d 20 4f 50 5f 49 64 78 52 6f 77 69 64 2c 20 69 49   OP_IdxRowid, iI
1fc8e 64 78 43 75 72 2c 20 30 29 3b 0a 20 20 20 20 20  dxCur, 0);.     
1fc8f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1fc90 64 4f 70 28 76 2c 20 4f 50 5f 4d 6f 76 65 47 65  dOp(v, OP_MoveGe
1fc91 2c 20 69 43 75 72 2c 20 30 29 3b 0a 20 20 20 20  , iCur, 0);.    
1fc92 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 52 65    }..      /* Re
1fc93 63 6f 72 64 20 74 68 65 20 69 6e 73 74 72 75 63  cord the instruc
1fc94 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 74 65 72  tion used to ter
1fc95 6d 69 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70 2e  minate the loop.
1fc96 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
1fc97 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 62 52 65  pLevel->op = bRe
1fc98 76 20 3f 20 4f 50 5f 50 72 65 76 20 3a 20 4f 50  v ? OP_Prev : OP
1fc99 5f 4e 65 78 74 3b 0a 20 20 20 20 20 20 70 4c 65  _Next;.      pLe
1fc9a 76 65 6c 2d 3e 70 31 20 3d 20 69 49 64 78 43 75  vel->p1 = iIdxCu
1fc9b 72 3b 0a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d  r;.      pLevel-
1fc9c 3e 70 32 20 3d 20 73 74 61 72 74 3b 0a 20 20 20  >p2 = start;.   
1fc9d 20 7d 65 6c 73 65 20 69 66 28 20 70 4c 65 76 65   }else if( pLeve
1fc9e 6c 2d 3e 66 6c 61 67 73 20 26 20 57 48 45 52 45  l->flags & WHERE
1fc9f 5f 43 4f 4c 55 4d 4e 5f 45 51 20 29 7b 0a 20 20  _COLUMN_EQ ){.  
1fca0 20 20 20 20 2f 2a 20 43 61 73 65 20 34 3a 20 20      /* Case 4:  
1fca1 54 68 65 72 65 20 69 73 20 61 6e 20 69 6e 64 65  There is an inde
1fca2 78 20 61 6e 64 20 61 6c 6c 20 74 65 72 6d 73 20  x and all terms 
1fca3 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  of the WHERE cla
1fca4 75 73 65 20 74 68 61 74 0a 20 20 20 20 20 20 2a  use that.      *
1fca5 2a 20 20 20 20 20 20 20 20 20 20 72 65 66 65 72  *          refer
1fca6 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 75 73   to the index us
1fca7 69 6e 67 20 74 68 65 20 22 3d 3d 22 20 6f 72 20  ing the "==" or 
1fca8 22 49 4e 22 20 6f 70 65 72 61 74 6f 72 73 2e 0a  "IN" operators..
1fca9 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
1fcaa 6e 74 20 73 74 61 72 74 3b 0a 20 20 20 20 20 20  nt start;.      
1fcab 69 6e 74 20 6e 45 71 20 3d 20 70 4c 65 76 65 6c  int nEq = pLevel
1fcac 2d 3e 6e 45 71 3b 0a 0a 20 20 20 20 20 20 2f 2a  ->nEq;..      /*
1fcad 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
1fcae 6f 20 65 76 61 6c 75 61 74 65 20 61 6c 6c 20 63  o evaluate all c
1fcaf 6f 6e 73 74 72 61 69 6e 74 20 74 65 72 6d 73 20  onstraint terms 
1fcb0 75 73 69 6e 67 20 3d 3d 20 6f 72 20 49 4e 0a 20  using == or IN. 
1fcb1 20 20 20 20 20 2a 2a 20 61 6e 64 20 6c 65 61 76       ** and leav
1fcb2 65 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20  e the values of 
1fcb3 74 68 6f 73 65 20 74 65 72 6d 73 20 6f 6e 20 74  those terms on t
1fcb4 68 65 20 73 74 61 63 6b 2e 0a 20 20 20 20 20 20  he stack..      
1fcb5 2a 2f 0a 20 20 20 20 20 20 63 6f 64 65 41 6c 6c  */.      codeAll
1fcb6 45 71 75 61 6c 69 74 79 54 65 72 6d 73 28 70 50  EqualityTerms(pP
1fcb7 61 72 73 65 2c 20 70 4c 65 76 65 6c 2c 20 26 77  arse, pLevel, &w
1fcb8 63 2c 20 6e 6f 74 52 65 61 64 79 29 3b 0a 20 20  c, notReady);.  
1fcb9 20 20 20 20 6e 78 74 20 3d 20 70 4c 65 76 65 6c      nxt = pLevel
1fcba 2d 3e 6e 78 74 3b 0a 0a 20 20 20 20 20 20 2f 2a  ->nxt;..      /*
1fcbb 20 47 65 6e 65 72 61 74 65 20 61 20 73 69 6e 67   Generate a sing
1fcbc 6c 65 20 6b 65 79 20 74 68 61 74 20 77 69 6c 6c  le key that will
1fcbd 20 62 65 20 75 73 65 64 20 74 6f 20 62 6f 74 68   be used to both
1fcbe 20 73 74 61 72 74 20 61 6e 64 20 74 65 72 6d 69   start and termi
1fcbf 6e 61 74 65 0a 20 20 20 20 20 20 2a 2a 20 74 68  nate.      ** th
1fcc0 65 20 73 65 61 72 63 68 0a 20 20 20 20 20 20 2a  e search.      *
1fcc1 2f 0a 20 20 20 20 20 20 62 75 69 6c 64 49 6e 64  /.      buildInd
1fcc2 65 78 50 72 6f 62 65 28 76 2c 20 6e 45 71 2c 20  exProbe(v, nEq, 
1fcc3 70 49 64 78 29 3b 0a 20 20 20 20 20 20 73 71 6c  pIdx);.      sql
1fcc4 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76 2c  ite3VdbeAddOp(v,
1fcc5 20 4f 50 5f 4d 65 6d 53 74 6f 72 65 2c 20 70 4c   OP_MemStore, pL
1fcc6 65 76 65 6c 2d 3e 69 4d 65 6d 2c 20 30 29 3b 0a  evel->iMem, 0);.
1fcc7 0a 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61  .      /* Genera
1fcc8 74 65 20 63 6f 64 65 20 28 31 29 20 74 6f 20 6d  te code (1) to m
1fcc9 6f 76 65 20 74 6f 20 74 68 65 20 66 69 72 73 74  ove to the first
1fcca 20 6d 61 74 63 68 69 6e 67 20 65 6c 65 6d 65 6e   matching elemen
1fccb 74 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 0a  t of the table..
1fccc 20 20 20 20 20 20 2a 2a 20 54 68 65 6e 20 67 65        ** Then ge
1fccd 6e 65 72 61 74 65 20 63 6f 64 65 20 28 32 29 20  nerate code (2) 
1fcce 74 68 61 74 20 6a 75 6d 70 73 20 74 6f 20 22 6e  that jumps to "n
1fccf 78 74 22 20 61 66 74 65 72 20 74 68 65 20 63 75  xt" after the cu
1fcd0 72 73 6f 72 20 69 73 20 70 61 73 74 0a 20 20 20  rsor is past.   
1fcd1 20 20 20 2a 2a 20 74 68 65 20 6c 61 73 74 20 6d     ** the last m
1fcd2 61 74 63 68 69 6e 67 20 65 6c 65 6d 65 6e 74 20  atching element 
1fcd3 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 20 54  of the table.  T
1fcd4 68 65 20 63 6f 64 65 20 28 31 29 20 69 73 20 65  he code (1) is e
1fcd5 78 65 63 75 74 65 64 0a 20 20 20 20 20 20 2a 2a  xecuted.      **
1fcd6 20 6f 6e 63 65 20 74 6f 20 69 6e 69 74 69 61 6c   once to initial
1fcd7 69 7a 65 20 74 68 65 20 73 65 61 72 63 68 2c 20  ize the search, 
1fcd8 74 68 65 20 63 6f 64 65 20 28 32 29 20 69 73 20  the code (2) is 
1fcd9 65 78 65 63 75 74 65 64 20 62 65 66 6f 72 65 20  executed before 
1fcda 65 61 63 68 0a 20 20 20 20 20 20 2a 2a 20 69 74  each.      ** it
1fcdb 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73  eration of the s
1fcdc 63 61 6e 20 74 6f 20 73 65 65 20 69 66 20 74 68  can to see if th
1fcdd 65 20 73 63 61 6e 20 68 61 73 20 66 69 6e 69 73  e scan has finis
1fcde 68 65 64 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66  hed. */.      if
1fcdf 28 20 62 52 65 76 20 29 7b 0a 20 20 20 20 20 20  ( bRev ){.      
1fce0 20 20 2f 2a 20 53 63 61 6e 20 69 6e 20 72 65 76    /* Scan in rev
1fce1 65 72 73 65 20 6f 72 64 65 72 20 2a 2f 0a 20 20  erse order */.  
1fce2 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1fce3 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 6f 76  eAddOp(v, OP_Mov
1fce4 65 4c 65 2c 20 69 49 64 78 43 75 72 2c 20 6e 78  eLe, iIdxCur, nx
1fce5 74 29 3b 0a 20 20 20 20 20 20 20 20 73 74 61 72  t);.        star
1fce6 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  t = sqlite3VdbeA
1fce7 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 4c 6f  ddOp(v, OP_MemLo
1fce8 61 64 2c 20 70 4c 65 76 65 6c 2d 3e 69 4d 65 6d  ad, pLevel->iMem
1fce9 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71  , 0);.        sq
1fcea 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1fceb 2c 20 4f 50 5f 49 64 78 4c 54 2c 20 69 49 64 78  , OP_IdxLT, iIdx
1fcec 43 75 72 2c 20 6e 78 74 29 3b 0a 20 20 20 20 20  Cur, nxt);.     
1fced 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20     pLevel->op = 
1fcee 4f 50 5f 50 72 65 76 3b 0a 20 20 20 20 20 20 7d  OP_Prev;.      }
1fcef 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a  else{.        /*
1fcf0 20 53 63 61 6e 20 69 6e 20 74 68 65 20 66 6f 72   Scan in the for
1fcf1 77 61 72 64 20 6f 72 64 65 72 20 2a 2f 0a 20 20  ward order */.  
1fcf2 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1fcf3 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 6f 76  eAddOp(v, OP_Mov
1fcf4 65 47 65 2c 20 69 49 64 78 43 75 72 2c 20 6e 78  eGe, iIdxCur, nx
1fcf5 74 29 3b 0a 20 20 20 20 20 20 20 20 73 74 61 72  t);.        star
1fcf6 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  t = sqlite3VdbeA
1fcf7 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 65 6d 4c 6f  ddOp(v, OP_MemLo
1fcf8 61 64 2c 20 70 4c 65 76 65 6c 2d 3e 69 4d 65 6d  ad, pLevel->iMem
1fcf9 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71  , 0);.        sq
1fcfa 6c 69 74 65 33 56 64 62 65 4f 70 33 28 76 2c 20  lite3VdbeOp3(v, 
1fcfb 4f 50 5f 49 64 78 47 45 2c 20 69 49 64 78 43 75  OP_IdxGE, iIdxCu
1fcfc 72 2c 20 6e 78 74 2c 20 22 2b 22 2c 20 50 33 5f  r, nxt, "+", P3_
1fcfd 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20  STATIC);.       
1fcfe 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 4f 50   pLevel->op = OP
1fcff 5f 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20  _Next;.      }. 
1fd00 20 20 20 20 20 69 66 28 20 21 6f 6d 69 74 54 61       if( !omitTa
1fd01 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ble ){.        s
1fd02 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1fd03 76 2c 20 4f 50 5f 49 64 78 52 6f 77 69 64 2c 20  v, OP_IdxRowid, 
1fd04 69 49 64 78 43 75 72 2c 20 30 29 3b 0a 20 20 20  iIdxCur, 0);.   
1fd05 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1fd06 41 64 64 4f 70 28 76 2c 20 4f 50 5f 4d 6f 76 65  AddOp(v, OP_Move
1fd07 47 65 2c 20 69 43 75 72 2c 20 30 29 3b 0a 20 20  Ge, iCur, 0);.  
1fd08 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4c 65 76      }.      pLev
1fd09 65 6c 2d 3e 70 31 20 3d 20 69 49 64 78 43 75 72  el->p1 = iIdxCur
1fd0a 3b 0a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e  ;.      pLevel->
1fd0b 70 32 20 3d 20 73 74 61 72 74 3b 0a 20 20 20 20  p2 = start;.    
1fd0c 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20  }else{.      /* 
1fd0d 43 61 73 65 20 35 3a 20 20 54 68 65 72 65 20 69  Case 5:  There i
1fd0e 73 20 6e 6f 20 75 73 61 62 6c 65 20 69 6e 64 65  s no usable inde
1fd0f 78 2e 20 20 57 65 20 6d 75 73 74 20 64 6f 20 61  x.  We must do a
1fd10 20 63 6f 6d 70 6c 65 74 65 0a 20 20 20 20 20 20   complete.      
1fd11 2a 2a 20 20 20 20 20 20 20 20 20 20 73 63 61 6e  **          scan
1fd12 20 6f 66 20 74 68 65 20 65 6e 74 69 72 65 20 74   of the entire t
1fd13 61 62 6c 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  able..      */. 
1fd14 20 20 20 20 20 61 73 73 65 72 74 28 20 6f 6d 69       assert( omi
1fd15 74 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20  tTable==0 );.   
1fd16 20 20 20 61 73 73 65 72 74 28 20 62 52 65 76 3d     assert( bRev=
1fd17 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 4c 65 76  =0 );.      pLev
1fd18 65 6c 2d 3e 6f 70 20 3d 20 4f 50 5f 4e 65 78 74  el->op = OP_Next
1fd19 3b 0a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e  ;.      pLevel->
1fd1a 70 31 20 3d 20 69 43 75 72 3b 0a 20 20 20 20 20  p1 = iCur;.     
1fd1b 20 70 4c 65 76 65 6c 2d 3e 70 32 20 3d 20 31 20   pLevel->p2 = 1 
1fd1c 2b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  + sqlite3VdbeAdd
1fd1d 4f 70 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c  Op(v, OP_Rewind,
1fd1e 20 69 43 75 72 2c 20 62 72 6b 29 3b 0a 20 20 20   iCur, brk);.   
1fd1f 20 7d 0a 20 20 20 20 6e 6f 74 52 65 61 64 79 20   }.    notReady 
1fd20 26 3d 20 7e 67 65 74 4d 61 73 6b 28 26 6d 61 73  &= ~getMask(&mas
1fd21 6b 53 65 74 2c 20 69 43 75 72 29 3b 0a 0a 20 20  kSet, iCur);..  
1fd22 20 20 2f 2a 20 49 6e 73 65 72 74 20 63 6f 64 65    /* Insert code
1fd23 20 74 6f 20 74 65 73 74 20 65 76 65 72 79 20 73   to test every s
1fd24 75 62 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61  ubexpression tha
1fd25 74 20 63 61 6e 20 62 65 20 63 6f 6d 70 6c 65 74  t can be complet
1fd26 65 6c 79 0a 20 20 20 20 2a 2a 20 63 6f 6d 70 75  ely.    ** compu
1fd27 74 65 64 20 75 73 69 6e 67 20 74 68 65 20 63 75  ted using the cu
1fd28 72 72 65 6e 74 20 73 65 74 20 6f 66 20 74 61 62  rrent set of tab
1fd29 6c 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  les..    */.    
1fd2a 66 6f 72 28 70 54 65 72 6d 3d 77 63 2e 61 2c 20  for(pTerm=wc.a, 
1fd2b 6a 3d 77 63 2e 6e 54 65 72 6d 3b 20 6a 3e 30 3b  j=wc.nTerm; j>0;
1fd2c 20 6a 2d 2d 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a   j--, pTerm++){.
1fd2d 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 3b 0a        Expr *pE;.
1fd2e 20 20 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d        if( pTerm-
1fd2f 3e 66 6c 61 67 73 20 26 20 28 54 45 52 4d 5f 56  >flags & (TERM_V
1fd30 49 52 54 55 41 4c 7c 54 45 52 4d 5f 43 4f 44 45  IRTUAL|TERM_CODE
1fd31 44 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  D) ) continue;. 
1fd32 20 20 20 20 20 69 66 28 20 28 70 54 65 72 6d 2d       if( (pTerm-
1fd33 3e 70 72 65 72 65 71 41 6c 6c 20 26 20 6e 6f 74  >prereqAll & not
1fd34 52 65 61 64 79 29 21 3d 30 20 29 20 63 6f 6e 74  Ready)!=0 ) cont
1fd35 69 6e 75 65 3b 0a 20 20 20 20 20 20 70 45 20 3d  inue;.      pE =
1fd36 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20   pTerm->pExpr;. 
1fd37 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 21       assert( pE!
1fd38 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  =0 );.      if( 
1fd39 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69  pLevel->iLeftJoi
1fd3a 6e 20 26 26 20 21 45 78 70 72 48 61 73 50 72 6f  n && !ExprHasPro
1fd3b 70 65 72 74 79 28 70 45 2c 20 45 50 5f 46 72 6f  perty(pE, EP_Fro
1fd3c 6d 4a 6f 69 6e 29 20 29 7b 0a 20 20 20 20 20 20  mJoin) ){.      
1fd3d 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20    continue;.    
1fd3e 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
1fd3f 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50 61  3ExprIfFalse(pPa
1fd40 72 73 65 2c 20 70 45 2c 20 63 6f 6e 74 2c 20 31  rse, pE, cont, 1
1fd41 29 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e  );.      pTerm->
1fd42 66 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f 43 4f  flags |= TERM_CO
1fd43 44 45 44 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  DED;.    }..    
1fd44 2f 2a 20 46 6f 72 20 61 20 4c 45 46 54 20 4f 55  /* For a LEFT OU
1fd45 54 45 52 20 4a 4f 49 4e 2c 20 67 65 6e 65 72 61  TER JOIN, genera
1fd46 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c  te code that wil
1fd47 6c 20 72 65 63 6f 72 64 20 74 68 65 20 66 61 63  l record the fac
1fd48 74 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 61 74  t that.    ** at
1fd49 20 6c 65 61 73 74 20 6f 6e 65 20 72 6f 77 20 6f   least one row o
1fd4a 66 20 74 68 65 20 72 69 67 68 74 20 74 61 62 6c  f the right tabl
1fd4b 65 20 68 61 73 20 6d 61 74 63 68 65 64 20 74 68  e has matched th
1fd4c 65 20 6c 65 66 74 20 74 61 62 6c 65 2e 20 20 0a  e left table.  .
1fd4d 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
1fd4e 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e  Level->iLeftJoin
1fd4f 20 29 7b 0a 20 20 20 20 20 20 70 4c 65 76 65 6c   ){.      pLevel
1fd50 2d 3e 74 6f 70 20 3d 20 73 71 6c 69 74 65 33 56  ->top = sqlite3V
1fd51 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
1fd52 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1fd53 56 64 62 65 41 64 64 4f 70 28 76 2c 20 4f 50 5f  VdbeAddOp(v, OP_
1fd54 4d 65 6d 49 6e 74 2c 20 31 2c 20 70 4c 65 76 65  MemInt, 1, pLeve
1fd55 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 29 3b 0a 20  l->iLeftJoin);. 
1fd56 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74       VdbeComment
1fd57 28 28 76 2c 20 22 23 20 72 65 63 6f 72 64 20 4c  ((v, "# record L
1fd58 45 46 54 20 4a 4f 49 4e 20 68 69 74 22 29 29 3b  EFT JOIN hit"));
1fd59 0a 20 20 20 20 20 20 66 6f 72 28 70 54 65 72 6d  .      for(pTerm
1fd5a 3d 77 63 2e 61 2c 20 6a 3d 30 3b 20 6a 3c 77 63  =wc.a, j=0; j<wc
1fd5b 2e 6e 54 65 72 6d 3b 20 6a 2b 2b 2c 20 70 54 65  .nTerm; j++, pTe
1fd5c 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69  rm++){.        i
1fd5d 66 28 20 70 54 65 72 6d 2d 3e 66 6c 61 67 73 20  f( pTerm->flags 
1fd5e 26 20 28 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c  & (TERM_VIRTUAL|
1fd5f 54 45 52 4d 5f 43 4f 44 45 44 29 20 29 20 63 6f  TERM_CODED) ) co
1fd60 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20  ntinue;.        
1fd61 69 66 28 20 28 70 54 65 72 6d 2d 3e 70 72 65 72  if( (pTerm->prer
1fd62 65 71 41 6c 6c 20 26 20 6e 6f 74 52 65 61 64 79  eqAll & notReady
1fd63 29 21 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b  )!=0 ) continue;
1fd64 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1fd65 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 20 29 3b   pTerm->pExpr );
1fd66 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1fd67 45 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72  ExprIfFalse(pPar
1fd68 73 65 2c 20 70 54 65 72 6d 2d 3e 70 45 78 70 72  se, pTerm->pExpr
1fd69 2c 20 63 6f 6e 74 2c 20 31 29 3b 0a 20 20 20 20  , cont, 1);.    
1fd6a 20 20 20 20 70 54 65 72 6d 2d 3e 66 6c 61 67 73      pTerm->flags
1fd6b 20 7c 3d 20 54 45 52 4d 5f 43 4f 44 45 44 3b 0a   |= TERM_CODED;.
1fd6c 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1fd6d 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  }..#ifdef SQLITE
1fd6e 5f 54 45 53 54 20 20 2f 2a 20 46 6f 72 20 74 65  _TEST  /* For te
1fd6f 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67  sting and debugg
1fd70 69 6e 67 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a  ing use only */.
1fd71 20 20 2f 2a 20 52 65 63 6f 72 64 20 69 6e 20 74    /* Record in t
1fd72 68 65 20 71 75 65 72 79 20 70 6c 61 6e 20 69 6e  he query plan in
1fd73 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
1fd74 74 68 65 20 63 75 72 72 65 6e 74 20 74 61 62 6c  the current tabl
1fd75 65 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 69  e.  ** and the i
1fd76 6e 64 65 78 20 75 73 65 64 20 74 6f 20 61 63 63  ndex used to acc
1fd77 65 73 73 20 69 74 20 28 69 66 20 61 6e 79 29 2e  ess it (if any).
1fd78 20 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 69    If the table i
1fd79 74 73 65 6c 66 0a 20 20 2a 2a 20 69 73 20 6e 6f  tself.  ** is no
1fd7a 74 20 75 73 65 64 2c 20 69 74 73 20 6e 61 6d 65  t used, its name
1fd7b 20 69 73 20 6a 75 73 74 20 27 7b 7d 27 2e 20 20   is just '{}'.  
1fd7c 49 66 20 6e 6f 20 69 6e 64 65 78 20 69 73 20 75  If no index is u
1fd7d 73 65 64 0a 20 20 2a 2a 20 74 68 65 20 69 6e 64  sed.  ** the ind
1fd7e 65 78 20 69 73 20 6c 69 73 74 65 64 20 61 73 20  ex is listed as 
1fd7f 22 7b 7d 22 2e 20 20 49 66 20 74 68 65 20 70 72  "{}".  If the pr
1fd80 69 6d 61 72 79 20 6b 65 79 20 69 73 20 75 73 65  imary key is use
1fd81 64 20 74 68 65 0a 20 20 2a 2a 20 69 6e 64 65 78  d the.  ** index
1fd82 20 6e 61 6d 65 20 69 73 20 27 2a 27 2e 0a 20 20   name is '*'..  
1fd83 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  */.  for(i=0; i<
1fd84 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20  pTabList->nSrc; 
1fd85 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72 20 2a  i++){.    char *
1fd86 7a 3b 0a 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20  z;.    int n;.  
1fd87 20 20 70 4c 65 76 65 6c 20 3d 20 26 70 57 49 6e    pLevel = &pWIn
1fd88 66 6f 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 70 54  fo->a[i];.    pT
1fd89 61 62 49 74 65 6d 20 3d 20 26 70 54 61 62 4c 69  abItem = &pTabLi
1fd8a 73 74 2d 3e 61 5b 70 4c 65 76 65 6c 2d 3e 69 46  st->a[pLevel->iF
1fd8b 72 6f 6d 5d 3b 0a 20 20 20 20 7a 20 3d 20 70 54  rom];.    z = pT
1fd8c 61 62 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 3b 0a  abItem->zAlias;.
1fd8d 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 7a      if( z==0 ) z
1fd8e 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 70 54 61   = pTabItem->pTa
1fd8f 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 6e 20  b->zName;.    n 
1fd90 3d 20 73 74 72 6c 65 6e 28 7a 29 3b 0a 20 20 20  = strlen(z);.   
1fd91 20 69 66 28 20 6e 2b 6e 51 50 6c 61 6e 20 3c 20   if( n+nQPlan < 
1fd92 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 71  sizeof(sqlite3_q
1fd93 75 65 72 79 5f 70 6c 61 6e 29 2d 31 30 20 29 7b  uery_plan)-10 ){
1fd94 0a 20 20 20 20 20 20 69 66 28 20 70 4c 65 76 65  .      if( pLeve
1fd95 6c 2d 3e 66 6c 61 67 73 20 26 20 57 48 45 52 45  l->flags & WHERE
1fd96 5f 49 44 58 5f 4f 4e 4c 59 20 29 7b 0a 20 20 20  _IDX_ONLY ){.   
1fd97 20 20 20 20 20 6d 65 6d 63 70 79 28 26 73 71 6c       memcpy(&sql
1fd98 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b  ite3_query_plan[
1fd99 6e 51 50 6c 61 6e 5d 2c 20 22 7b 7d 22 2c 20 32  nQPlan], "{}", 2
1fd9a 29 3b 0a 20 20 20 20 20 20 20 20 6e 51 50 6c 61  );.        nQPla
1fd9b 6e 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 7d 65  n += 2;.      }e
1fd9c 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d  lse{.        mem
1fd9d 63 70 79 28 26 73 71 6c 69 74 65 33 5f 71 75 65  cpy(&sqlite3_que
1fd9e 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 2c  ry_plan[nQPlan],
1fd9f 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20 20 20   z, n);.        
1fda0 6e 51 50 6c 61 6e 20 2b 3d 20 6e 3b 0a 20 20 20  nQPlan += n;.   
1fda1 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
1fda2 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51  e3_query_plan[nQ
1fda3 50 6c 61 6e 2b 2b 5d 20 3d 20 27 20 27 3b 0a 20  Plan++] = ' ';. 
1fda4 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4c 65     }.    if( pLe
1fda5 76 65 6c 2d 3e 66 6c 61 67 73 20 26 20 28 57 48  vel->flags & (WH
1fda6 45 52 45 5f 52 4f 57 49 44 5f 45 51 7c 57 48 45  ERE_ROWID_EQ|WHE
1fda7 52 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45 29 20  RE_ROWID_RANGE) 
1fda8 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  ){.      memcpy(
1fda9 26 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70  &sqlite3_query_p
1fdaa 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 2c 20 22 2a 20  lan[nQPlan], "* 
1fdab 22 2c 20 32 29 3b 0a 20 20 20 20 20 20 6e 51 50  ", 2);.      nQP
1fdac 6c 61 6e 20 2b 3d 20 32 3b 0a 20 20 20 20 7d 65  lan += 2;.    }e
1fdad 6c 73 65 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e  lse if( pLevel->
1fdae 70 49 64 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20  pIdx==0 ){.     
1fdaf 20 6d 65 6d 63 70 79 28 26 73 71 6c 69 74 65 33   memcpy(&sqlite3
1fdb0 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c  _query_plan[nQPl
1fdb1 61 6e 5d 2c 20 22 7b 7d 20 22 2c 20 33 29 3b 0a  an], "{} ", 3);.
1fdb2 20 20 20 20 20 20 6e 51 50 6c 61 6e 20 2b 3d 20        nQPlan += 
1fdb3 33 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  3;.    }else{.  
1fdb4 20 20 20 20 6e 20 3d 20 73 74 72 6c 65 6e 28 70      n = strlen(p
1fdb5 4c 65 76 65 6c 2d 3e 70 49 64 78 2d 3e 7a 4e 61  Level->pIdx->zNa
1fdb6 6d 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e  me);.      if( n
1fdb7 2b 6e 51 50 6c 61 6e 20 3c 20 73 69 7a 65 6f 66  +nQPlan < sizeof
1fdb8 28 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70  (sqlite3_query_p
1fdb9 6c 61 6e 29 2d 32 20 29 7b 0a 20 20 20 20 20 20  lan)-2 ){.      
1fdba 20 20 6d 65 6d 63 70 79 28 26 73 71 6c 69 74 65    memcpy(&sqlite
1fdbb 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50  3_query_plan[nQP
1fdbc 6c 61 6e 5d 2c 20 70 4c 65 76 65 6c 2d 3e 70 49  lan], pLevel->pI
1fdbd 64 78 2d 3e 7a 4e 61 6d 65 2c 20 6e 29 3b 0a 20  dx->zName, n);. 
1fdbe 20 20 20 20 20 20 20 6e 51 50 6c 61 6e 20 2b 3d         nQPlan +=
1fdbf 20 6e 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   n;.        sqli
1fdc0 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e  te3_query_plan[n
1fdc1 51 50 6c 61 6e 2b 2b 5d 20 3d 20 27 20 27 3b 0a  QPlan++] = ' ';.
1fdc2 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1fdc3 7d 0a 20 20 77 68 69 6c 65 28 20 6e 51 50 6c 61  }.  while( nQPla
1fdc4 6e 3e 30 20 26 26 20 73 71 6c 69 74 65 33 5f 71  n>0 && sqlite3_q
1fdc5 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e  uery_plan[nQPlan
1fdc6 2d 31 5d 3d 3d 27 20 27 20 29 7b 0a 20 20 20 20  -1]==' ' ){.    
1fdc7 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c  sqlite3_query_pl
1fdc8 61 6e 5b 2d 2d 6e 51 50 6c 61 6e 5d 20 3d 20 30  an[--nQPlan] = 0
1fdc9 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
1fdca 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61  query_plan[nQPla
1fdcb 6e 5d 20 3d 20 30 3b 0a 20 20 6e 51 50 6c 61 6e  n] = 0;.  nQPlan
1fdcc 20 3d 20 30 3b 0a 23 65 6e 64 69 66 20 2f 2a 20   = 0;.#endif /* 
1fdcd 53 51 4c 49 54 45 5f 54 45 53 54 20 2f 2f 20 54  SQLITE_TEST // T
1fdce 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67  esting and debug
1fdcf 67 69 6e 67 20 75 73 65 20 6f 6e 6c 79 20 2a 2f  ging use only */
1fdd0 0a 0a 20 20 2f 2a 20 52 65 63 6f 72 64 20 74 68  ..  /* Record th
1fdd1 65 20 63 6f 6e 74 69 6e 75 61 74 69 6f 6e 20 61  e continuation a
1fdd2 64 64 72 65 73 73 20 69 6e 20 74 68 65 20 57 68  ddress in the Wh
1fdd3 65 72 65 49 6e 66 6f 20 73 74 72 75 63 74 75 72  ereInfo structur
1fdd4 65 2e 20 20 54 68 65 6e 0a 20 20 2a 2a 20 63 6c  e.  Then.  ** cl
1fdd5 65 61 6e 20 75 70 20 61 6e 64 20 72 65 74 75 72  ean up and retur
1fdd6 6e 2e 0a 20 20 2a 2f 0a 20 20 70 57 49 6e 66 6f  n..  */.  pWInfo
1fdd7 2d 3e 69 43 6f 6e 74 69 6e 75 65 20 3d 20 63 6f  ->iContinue = co
1fdd8 6e 74 3b 0a 20 20 77 68 65 72 65 43 6c 61 75 73  nt;.  whereClaus
1fdd9 65 43 6c 65 61 72 28 26 77 63 29 3b 0a 20 20 72  eClear(&wc);.  r
1fdda 65 74 75 72 6e 20 70 57 49 6e 66 6f 3b 0a 0a 20  eturn pWInfo;.. 
1fddb 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69 66   /* Jump here if
1fddc 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 20 2a 2f   malloc fails */
1fddd 0a 77 68 65 72 65 42 65 67 69 6e 4e 6f 4d 65 6d  .whereBeginNoMem
1fdde 3a 0a 20 20 77 68 65 72 65 43 6c 61 75 73 65 43  :.  whereClauseC
1fddf 6c 65 61 72 28 26 77 63 29 3b 0a 20 20 77 68 65  lear(&wc);.  whe
1fde0 72 65 49 6e 66 6f 46 72 65 65 28 70 57 49 6e 66  reInfoFree(pWInf
1fde1 6f 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a  o);.  return 0;.
1fde2 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
1fde3 65 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  e the end of the
1fde4 20 57 48 45 52 45 20 6c 6f 6f 70 2e 20 20 53 65   WHERE loop.  Se
1fde5 65 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 0a 2a  e comments on .*
1fde6 2a 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65  * sqlite3WhereBe
1fde7 67 69 6e 28 29 20 66 6f 72 20 61 64 64 69 74 69  gin() for additi
1fde8 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
1fde9 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1fdea 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1fdeb 57 68 65 72 65 45 6e 64 28 57 68 65 72 65 49 6e  WhereEnd(WhereIn
1fdec 66 6f 20 2a 70 57 49 6e 66 6f 29 7b 0a 20 20 56  fo *pWInfo){.  V
1fded 64 62 65 20 2a 76 20 3d 20 70 57 49 6e 66 6f 2d  dbe *v = pWInfo-
1fdee 3e 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a  >pParse->pVdbe;.
1fdef 20 20 69 6e 74 20 69 3b 0a 20 20 57 68 65 72 65    int i;.  Where
1fdf0 4c 65 76 65 6c 20 2a 70 4c 65 76 65 6c 3b 0a 20  Level *pLevel;. 
1fdf1 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69   SrcList *pTabLi
1fdf2 73 74 20 3d 20 70 57 49 6e 66 6f 2d 3e 70 54 61  st = pWInfo->pTa
1fdf3 62 4c 69 73 74 3b 0a 0a 20 20 2f 2a 20 47 65 6e  bList;..  /* Gen
1fdf4 65 72 61 74 65 20 6c 6f 6f 70 20 74 65 72 6d 69  erate loop termi
1fdf5 6e 61 74 69 6f 6e 20 63 6f 64 65 2e 0a 20 20 2a  nation code..  *
1fdf6 2f 0a 20 20 66 6f 72 28 69 3d 70 54 61 62 4c 69  /.  for(i=pTabLi
1fdf7 73 74 2d 3e 6e 53 72 63 2d 31 3b 20 69 3e 3d 30  st->nSrc-1; i>=0
1fdf8 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 70 4c 65 76  ; i--){.    pLev
1fdf9 65 6c 20 3d 20 26 70 57 49 6e 66 6f 2d 3e 61 5b  el = &pWInfo->a[
1fdfa 69 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  i];.    sqlite3V
1fdfb 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
1fdfc 76 2c 20 70 4c 65 76 65 6c 2d 3e 63 6f 6e 74 29  v, pLevel->cont)
1fdfd 3b 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c  ;.    if( pLevel
1fdfe 2d 3e 6f 70 21 3d 4f 50 5f 4e 6f 6f 70 20 29 7b  ->op!=OP_Noop ){
1fdff 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1fe00 62 65 41 64 64 4f 70 28 76 2c 20 70 4c 65 76 65  beAddOp(v, pLeve
1fe01 6c 2d 3e 6f 70 2c 20 70 4c 65 76 65 6c 2d 3e 70  l->op, pLevel->p
1fe02 31 2c 20 70 4c 65 76 65 6c 2d 3e 70 32 29 3b 0a  1, pLevel->p2);.
1fe03 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4c      }.    if( pL
1fe04 65 76 65 6c 2d 3e 6e 49 6e 20 29 7b 0a 20 20 20  evel->nIn ){.   
1fe05 20 20 20 73 74 72 75 63 74 20 49 6e 4c 6f 6f 70     struct InLoop
1fe06 20 2a 70 49 6e 3b 0a 20 20 20 20 20 20 69 6e 74   *pIn;.      int
1fe07 20 6a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   j;.      sqlite
1fe08 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
1fe09 6c 28 76 2c 20 70 4c 65 76 65 6c 2d 3e 6e 78 74  l(v, pLevel->nxt
1fe0a 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 70  );.      for(j=p
1fe0b 4c 65 76 65 6c 2d 3e 6e 49 6e 2c 20 70 49 6e 3d  Level->nIn, pIn=
1fe0c 26 70 4c 65 76 65 6c 2d 3e 61 49 6e 4c 6f 6f 70  &pLevel->aInLoop
1fe0d 5b 6a 2d 31 5d 3b 20 6a 3e 30 3b 20 6a 2d 2d 2c  [j-1]; j>0; j--,
1fe0e 20 70 49 6e 2d 2d 29 7b 0a 20 20 20 20 20 20 20   pIn--){.       
1fe0f 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
1fe10 48 65 72 65 28 76 2c 20 70 49 6e 2d 3e 74 6f 70  Here(v, pIn->top
1fe11 41 64 64 72 2b 31 29 3b 0a 20 20 20 20 20 20 20  Addr+1);.       
1fe12 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1fe13 70 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 70 49  p(v, OP_Next, pI
1fe14 6e 2d 3e 69 43 75 72 2c 20 70 49 6e 2d 3e 74 6f  n->iCur, pIn->to
1fe15 70 41 64 64 72 29 3b 0a 20 20 20 20 20 20 20 20  pAddr);.        
1fe16 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
1fe17 65 72 65 28 76 2c 20 70 49 6e 2d 3e 74 6f 70 41  ere(v, pIn->topA
1fe18 64 64 72 2d 31 29 3b 0a 20 20 20 20 20 20 7d 0a  ddr-1);.      }.
1fe19 20 20 20 20 20 20 73 71 6c 69 74 65 46 72 65 65        sqliteFree
1fe1a 28 70 4c 65 76 65 6c 2d 3e 61 49 6e 4c 6f 6f 70  (pLevel->aInLoop
1fe1b 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
1fe1c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
1fe1d 61 62 65 6c 28 76 2c 20 70 4c 65 76 65 6c 2d 3e  abel(v, pLevel->
1fe1e 62 72 6b 29 3b 0a 20 20 20 20 69 66 28 20 70 4c  brk);.    if( pL
1fe1f 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 20  evel->iLeftJoin 
1fe20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64  ){.      int add
1fe21 72 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20  r;.      addr = 
1fe22 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1fe23 28 76 2c 20 4f 50 5f 49 66 4d 65 6d 50 6f 73 2c  (v, OP_IfMemPos,
1fe24 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f   pLevel->iLeftJo
1fe25 69 6e 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71  in, 0);.      sq
1fe26 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28 76  lite3VdbeAddOp(v
1fe27 2c 20 4f 50 5f 4e 75 6c 6c 52 6f 77 2c 20 70 54  , OP_NullRow, pT
1fe28 61 62 4c 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 75  abList->a[i].iCu
1fe29 72 73 6f 72 2c 20 30 29 3b 0a 20 20 20 20 20 20  rsor, 0);.      
1fe2a 69 66 28 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78  if( pLevel->iIdx
1fe2b 43 75 72 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20  Cur>=0 ){.      
1fe2c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1fe2d 4f 70 28 76 2c 20 4f 50 5f 4e 75 6c 6c 52 6f 77  Op(v, OP_NullRow
1fe2e 2c 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75  , pLevel->iIdxCu
1fe2f 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20  r, 0);.      }. 
1fe30 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1fe31 41 64 64 4f 70 28 76 2c 20 4f 50 5f 47 6f 74 6f  AddOp(v, OP_Goto
1fe32 2c 20 30 2c 20 70 4c 65 76 65 6c 2d 3e 74 6f 70  , 0, pLevel->top
1fe33 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1fe34 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
1fe35 61 64 64 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  addr);.    }.  }
1fe36 0a 0a 20 20 2f 2a 20 54 68 65 20 22 62 72 65 61  ..  /* The "brea
1fe37 6b 22 20 70 6f 69 6e 74 20 69 73 20 68 65 72 65  k" point is here
1fe38 2c 20 6a 75 73 74 20 70 61 73 74 20 74 68 65 20  , just past the 
1fe39 65 6e 64 20 6f 66 20 74 68 65 20 6f 75 74 65 72  end of the outer
1fe3a 20 6c 6f 6f 70 2e 0a 20 20 2a 2a 20 53 65 74 20   loop..  ** Set 
1fe3b 69 74 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  it..  */.  sqlit
1fe3c 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
1fe3d 65 6c 28 76 2c 20 70 57 49 6e 66 6f 2d 3e 69 42  el(v, pWInfo->iB
1fe3e 72 65 61 6b 29 3b 0a 0a 20 20 2f 2a 20 43 6c 6f  reak);..  /* Clo
1fe3f 73 65 20 61 6c 6c 20 6f 66 20 74 68 65 20 63 75  se all of the cu
1fe40 72 73 6f 72 73 20 74 68 61 74 20 77 65 72 65 20  rsors that were 
1fe41 6f 70 65 6e 65 64 20 62 79 20 73 71 6c 69 74 65  opened by sqlite
1fe42 33 57 68 65 72 65 42 65 67 69 6e 2e 0a 20 20 2a  3WhereBegin..  *
1fe43 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 4c 65  /.  for(i=0, pLe
1fe44 76 65 6c 3d 70 57 49 6e 66 6f 2d 3e 61 3b 20 69  vel=pWInfo->a; i
1fe45 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b  <pTabList->nSrc;
1fe46 20 69 2b 2b 2c 20 70 4c 65 76 65 6c 2b 2b 29 7b   i++, pLevel++){
1fe47 0a 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c  .    struct SrcL
1fe48 69 73 74 5f 69 74 65 6d 20 2a 70 54 61 62 49 74  ist_item *pTabIt
1fe49 65 6d 20 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e  em = &pTabList->
1fe4a 61 5b 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d  a[pLevel->iFrom]
1fe4b 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61  ;.    Table *pTa
1fe4c 62 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 70 54  b = pTabItem->pT
1fe4d 61 62 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ab;.    assert( 
1fe4e 70 54 61 62 21 3d 30 20 29 3b 0a 20 20 20 20 69  pTab!=0 );.    i
1fe4f 66 28 20 70 54 61 62 2d 3e 69 73 45 70 68 65 6d  f( pTab->isEphem
1fe50 20 7c 7c 20 70 54 61 62 2d 3e 70 53 65 6c 65 63   || pTab->pSelec
1fe51 74 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  t ) continue;.  
1fe52 20 20 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e 66    if( (pLevel->f
1fe53 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 44 58  lags & WHERE_IDX
1fe54 5f 4f 4e 4c 59 29 3d 3d 30 20 29 7b 0a 20 20 20  _ONLY)==0 ){.   
1fe55 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1fe56 64 4f 70 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  dOp(v, OP_Close,
1fe57 20 70 54 61 62 49 74 65 6d 2d 3e 69 43 75 72 73   pTabItem->iCurs
1fe58 6f 72 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20  or, 0);.    }.  
1fe59 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 49    if( pLevel->pI
1fe5a 64 78 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73  dx!=0 ){.      s
1fe5b 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 28  qlite3VdbeAddOp(
1fe5c 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 70 4c 65  v, OP_Close, pLe
1fe5d 76 65 6c 2d 3e 69 49 64 78 43 75 72 2c 20 30 29  vel->iIdxCur, 0)
1fe5e 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
1fe5f 4d 61 6b 65 20 63 75 72 73 6f 72 20 73 75 62 73  Make cursor subs
1fe60 74 69 74 75 74 69 6f 6e 73 20 66 6f 72 20 63 61  titutions for ca
1fe61 73 65 73 20 77 68 65 72 65 20 77 65 20 77 61 6e  ses where we wan
1fe62 74 20 74 6f 20 75 73 65 0a 20 20 20 20 2a 2a 20  t to use.    ** 
1fe63 6a 75 73 74 20 74 68 65 20 69 6e 64 65 78 20 61  just the index a
1fe64 6e 64 20 6e 65 76 65 72 20 72 65 66 65 72 65 6e  nd never referen
1fe65 63 65 20 74 68 65 20 74 61 62 6c 65 2e 0a 20 20  ce the table..  
1fe66 20 20 2a 2a 20 0a 20 20 20 20 2a 2a 20 43 61 6c    ** .    ** Cal
1fe67 6c 73 20 74 6f 20 74 68 65 20 63 6f 64 65 20 67  ls to the code g
1fe68 65 6e 65 72 61 74 6f 72 20 69 6e 20 62 65 74 77  enerator in betw
1fe69 65 65 6e 20 73 71 6c 69 74 65 33 57 68 65 72 65  een sqlite3Where
1fe6a 42 65 67 69 6e 20 61 6e 64 0a 20 20 20 20 2a 2a  Begin and.    **
1fe6b 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64   sqlite3WhereEnd
1fe6c 20 77 69 6c 6c 20 68 61 76 65 20 63 72 65 61 74   will have creat
1fe6d 65 64 20 63 6f 64 65 20 74 68 61 74 20 72 65 66  ed code that ref
1fe6e 65 72 65 6e 63 65 73 20 74 68 65 20 74 61 62 6c  erences the tabl
1fe6f 65 0a 20 20 20 20 2a 2a 20 64 69 72 65 63 74 6c  e.    ** directl
1fe70 79 2e 20 20 54 68 69 73 20 6c 6f 6f 70 20 73 63  y.  This loop sc
1fe71 61 6e 73 20 61 6c 6c 20 74 68 61 74 20 63 6f 64  ans all that cod
1fe72 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 6f 70  e looking for op
1fe73 63 6f 64 65 73 0a 20 20 20 20 2a 2a 20 74 68 61  codes.    ** tha
1fe74 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 20  t reference the 
1fe75 74 61 62 6c 65 20 61 6e 64 20 63 6f 6e 76 65 72  table and conver
1fe76 74 73 20 74 68 65 6d 20 69 6e 74 6f 20 6f 70 63  ts them into opc
1fe77 6f 64 65 73 20 74 68 61 74 0a 20 20 20 20 2a 2a  odes that.    **
1fe78 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 20 69   reference the i
1fe79 6e 64 65 78 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ndex..    */.   
1fe7a 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 66 6c 61   if( pLevel->fla
1fe7b 67 73 20 26 20 57 48 45 52 45 5f 49 44 58 5f 4f  gs & WHERE_IDX_O
1fe7c 4e 4c 59 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  NLY ){.      int
1fe7d 20 6b 2c 20 6a 2c 20 6c 61 73 74 3b 0a 20 20 20   k, j, last;.   
1fe7e 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a     VdbeOp *pOp;.
1fe7f 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64        Index *pId
1fe80 78 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 49 64 78  x = pLevel->pIdx
1fe81 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
1fe82 20 70 49 64 78 21 3d 30 20 29 3b 0a 20 20 20 20   pIdx!=0 );.    
1fe83 20 20 70 4f 70 20 3d 20 73 71 6c 69 74 65 33 56    pOp = sqlite3V
1fe84 64 62 65 47 65 74 4f 70 28 76 2c 20 70 57 49 6e  dbeGetOp(v, pWIn
1fe85 66 6f 2d 3e 69 54 6f 70 29 3b 0a 20 20 20 20 20  fo->iTop);.     
1fe86 20 6c 61 73 74 20 3d 20 73 71 6c 69 74 65 33 56   last = sqlite3V
1fe87 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
1fe88 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 6b 3d 70  );.      for(k=p
1fe89 57 49 6e 66 6f 2d 3e 69 54 6f 70 3b 20 6b 3c 6c  WInfo->iTop; k<l
1fe8a 61 73 74 3b 20 6b 2b 2b 2c 20 70 4f 70 2b 2b 29  ast; k++, pOp++)
1fe8b 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4f  {.        if( pO
1fe8c 70 2d 3e 70 31 21 3d 70 4c 65 76 65 6c 2d 3e 69  p->p1!=pLevel->i
1fe8d 54 61 62 43 75 72 20 29 20 63 6f 6e 74 69 6e 75  TabCur ) continu
1fe8e 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  e;.        if( p
1fe8f 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 43  Op->opcode==OP_C
1fe90 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20  olumn ){.       
1fe91 20 20 20 70 4f 70 2d 3e 70 31 20 3d 20 70 4c 65     pOp->p1 = pLe
1fe92 76 65 6c 2d 3e 69 49 64 78 43 75 72 3b 0a 20 20  vel->iIdxCur;.  
1fe93 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b          for(j=0;
1fe94 20 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e   j<pIdx->nColumn
1fe95 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; j++){.        
1fe96 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d      if( pOp->p2=
1fe97 3d 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b  =pIdx->aiColumn[
1fe98 6a 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  j] ){.          
1fe99 20 20 20 20 70 4f 70 2d 3e 70 32 20 3d 20 6a 3b      pOp->p2 = j;
1fe9a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62  .              b
1fe9b 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
1fe9c 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a    }.          }.
1fe9d 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66          }else if
1fe9e 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f  ( pOp->opcode==O
1fe9f 50 5f 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20  P_Rowid ){.     
1fea0 20 20 20 20 20 70 4f 70 2d 3e 70 31 20 3d 20 70       pOp->p1 = p
1fea1 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75 72 3b 0a  Level->iIdxCur;.
1fea2 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 6f            pOp->o
1fea3 70 63 6f 64 65 20 3d 20 4f 50 5f 49 64 78 52 6f  pcode = OP_IdxRo
1fea4 77 69 64 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  wid;.        }el
1fea5 73 65 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f  se if( pOp->opco
1fea6 64 65 3d 3d 4f 50 5f 4e 75 6c 6c 52 6f 77 20 29  de==OP_NullRow )
1fea7 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d  {.          pOp-
1fea8 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 4e 6f 6f  >opcode = OP_Noo
1fea9 70 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  p;.        }.   
1feaa 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
1feab 20 20 2f 2a 20 46 69 6e 61 6c 20 63 6c 65 61 6e    /* Final clean
1feac 75 70 0a 20 20 2a 2f 0a 20 20 77 68 65 72 65 49  up.  */.  whereI
1fead 6e 66 6f 46 72 65 65 28 70 57 49 6e 66 6f 29 3b  nfoFree(pWInfo);
1feae 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a  .  return;.}../*
1feaf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
1feb0 64 20 6f 66 20 77 68 65 72 65 2e 63 20 2a 2a 2a  d of where.c ***
1feb1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1feb2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1feb3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
1feb4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
1feb5 67 69 6e 20 66 69 6c 65 20 70 61 72 73 65 2e 63  gin file parse.c
1feb6 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1feb7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1feb8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
1feb9 20 44 72 69 76 65 72 20 74 65 6d 70 6c 61 74 65   Driver template
1feba 20 66 6f 72 20 74 68 65 20 4c 45 4d 4f 4e 20 70   for the LEMON p
1febb 61 72 73 65 72 20 67 65 6e 65 72 61 74 6f 72 2e  arser generator.
1febc 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
1febd 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
1febe 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
1febf 65 20 63 6f 64 65 2e 0a 2a 2f 0a 2f 2a 20 46 69  e code..*/./* Fi
1fec0 72 73 74 20 6f 66 66 2c 20 63 6f 64 65 20 69 73  rst off, code is
1fec1 20 69 6e 63 6c 75 64 65 20 77 68 69 63 68 20 66   include which f
1fec2 6f 6c 6c 6f 77 73 20 74 68 65 20 22 69 6e 63 6c  ollows the "incl
1fec3 75 64 65 22 20 64 65 63 6c 61 72 61 74 69 6f 6e  ude" declaration
1fec4 0a 2a 2a 20 69 6e 20 74 68 65 20 69 6e 70 75 74  .** in the input
1fec5 20 66 69 6c 65 2e 20 2a 2f 0a 0a 0a 2f 2a 0a 2a   file. */.../*.*
1fec6 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
1fec7 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
1fec8 68 6f 6c 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f  holds informatio
1fec9 6e 20 61 62 6f 75 74 20 74 68 65 0a 2a 2a 20 4c  n about the.** L
1feca 49 4d 49 54 20 63 6c 61 75 73 65 20 6f 66 20 61  IMIT clause of a
1fecb 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
1fecc 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4c 69 6d  t..*/.struct Lim
1fecd 69 74 56 61 6c 20 7b 0a 20 20 45 78 70 72 20 2a  itVal {.  Expr *
1fece 70 4c 69 6d 69 74 3b 20 20 20 20 2f 2a 20 54 68  pLimit;    /* Th
1fecf 65 20 4c 49 4d 49 54 20 65 78 70 72 65 73 73 69  e LIMIT expressi
1fed0 6f 6e 2e 20 20 4e 55 4c 4c 20 69 66 20 74 68 65  on.  NULL if the
1fed1 72 65 20 69 73 20 6e 6f 20 6c 69 6d 69 74 20 2a  re is no limit *
1fed2 2f 0a 20 20 45 78 70 72 20 2a 70 4f 66 66 73 65  /.  Expr *pOffse
1fed3 74 3b 20 20 20 2f 2a 20 54 68 65 20 4f 46 46 53  t;   /* The OFFS
1fed4 45 54 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20  ET expression.  
1fed5 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20 69 73  NULL if there is
1fed6 20 6e 6f 6e 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a   none */.};../*.
1fed7 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
1fed8 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
1fed9 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72   is used to stor
1feda 65 20 74 68 65 20 4c 49 4b 45 2c 0a 2a 2a 20 47  e the LIKE,.** G
1fedb 4c 4f 42 2c 20 4e 4f 54 20 4c 49 4b 45 2c 20 61  LOB, NOT LIKE, a
1fedc 6e 64 20 4e 4f 54 20 47 4c 4f 42 20 6f 70 65 72  nd NOT GLOB oper
1fedd 61 74 6f 72 73 2e 0a 2a 2f 0a 73 74 72 75 63 74  ators..*/.struct
1fede 20 4c 69 6b 65 4f 70 20 7b 0a 20 20 54 6f 6b 65   LikeOp {.  Toke
1fedf 6e 20 65 4f 70 65 72 61 74 6f 72 3b 20 20 2f 2a  n eOperator;  /*
1fee0 20 22 6c 69 6b 65 22 20 6f 72 20 22 67 6c 6f 62   "like" or "glob
1fee1 22 20 6f 72 20 22 72 65 67 65 78 70 22 20 2a 2f  " or "regexp" */
1fee2 0a 20 20 69 6e 74 20 6e 6f 74 3b 20 20 20 20 20  .  int not;     
1fee3 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
1fee4 68 65 20 4e 4f 54 20 6b 65 79 77 6f 72 64 20 69  he NOT keyword i
1fee5 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 7d 3b 0a  s present */.};.
1fee6 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  ./*.** An instan
1fee7 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
1fee8 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 64 65  ing structure de
1fee9 73 63 72 69 62 65 73 20 74 68 65 20 65 76 65 6e  scribes the even
1feea 74 20 6f 66 20 61 0a 2a 2a 20 54 52 49 47 47 45  t of a.** TRIGGE
1feeb 52 2e 20 20 22 61 22 20 69 73 20 74 68 65 20 65  R.  "a" is the e
1feec 76 65 6e 74 20 74 79 70 65 2c 20 6f 6e 65 20 6f  vent type, one o
1feed 66 20 54 4b 5f 55 50 44 41 54 45 2c 20 54 4b 5f  f TK_UPDATE, TK_
1feee 49 4e 53 45 52 54 2c 0a 2a 2a 20 54 4b 5f 44 45  INSERT,.** TK_DE
1feef 4c 45 54 45 2c 20 6f 72 20 54 4b 5f 49 4e 53 54  LETE, or TK_INST
1fef0 45 41 44 2e 20 20 49 66 20 74 68 65 20 65 76 65  EAD.  If the eve
1fef1 6e 74 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72  nt is of the for
1fef2 6d 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 55 50 44  m.**.**      UPD
1fef3 41 54 45 20 4f 4e 20 28 61 2c 62 2c 63 29 0a 2a  ATE ON (a,b,c).*
1fef4 2a 0a 2a 2a 20 54 68 65 6e 20 74 68 65 20 22 62  *.** Then the "b
1fef5 22 20 49 64 4c 69 73 74 20 72 65 63 6f 72 64 73  " IdList records
1fef6 20 74 68 65 20 6c 69 73 74 20 22 61 2c 62 2c 63   the list "a,b,c
1fef7 22 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 54 72 69  "..*/.struct Tri
1fef8 67 45 76 65 6e 74 20 7b 20 69 6e 74 20 61 3b 20  gEvent { int a; 
1fef9 49 64 4c 69 73 74 20 2a 20 62 3b 20 7d 3b 0a 0a  IdList * b; };..
1fefa 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
1fefb 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  e of this struct
1fefc 75 72 65 20 68 6f 6c 64 73 20 74 68 65 20 41 54  ure holds the AT
1fefd 54 41 43 48 20 6b 65 79 20 61 6e 64 20 74 68 65  TACH key and the
1fefe 20 6b 65 79 20 74 79 70 65 2e 0a 2a 2f 0a 73 74   key type..*/.st
1feff 72 75 63 74 20 41 74 74 61 63 68 4b 65 79 20 7b  ruct AttachKey {
1ff00 20 69 6e 74 20 74 79 70 65 3b 20 20 54 6f 6b 65   int type;  Toke
1ff01 6e 20 6b 65 79 3b 20 7d 3b 0a 0a 2f 2a 20 4e 65  n key; };../* Ne
1ff02 78 74 20 69 73 20 61 6c 6c 20 74 6f 6b 65 6e 20  xt is all token 
1ff03 76 61 6c 75 65 73 2c 20 69 6e 20 61 20 66 6f 72  values, in a for
1ff04 6d 20 73 75 69 74 61 62 6c 65 20 66 6f 72 20 75  m suitable for u
1ff05 73 65 20 62 79 20 6d 61 6b 65 68 65 61 64 65 72  se by makeheader
1ff06 73 2e 0a 2a 2a 20 54 68 69 73 20 73 65 63 74 69  s..** This secti
1ff07 6f 6e 20 77 69 6c 6c 20 62 65 20 6e 75 6c 6c 20  on will be null 
1ff08 75 6e 6c 65 73 73 20 6c 65 6d 6f 6e 20 69 73 20  unless lemon is 
1ff09 72 75 6e 20 77 69 74 68 20 74 68 65 20 2d 6d 20  run with the -m 
1ff0a 73 77 69 74 63 68 2e 0a 2a 2f 0a 2f 2a 20 0a 2a  switch..*/./* .*
1ff0b 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74  * These constant
1ff0c 73 20 28 61 6c 6c 20 67 65 6e 65 72 61 74 65 64  s (all generated
1ff0d 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 62   automatically b
1ff0e 79 20 74 68 65 20 70 61 72 73 65 72 20 67 65 6e  y the parser gen
1ff0f 65 72 61 74 6f 72 29 0a 2a 2a 20 73 70 65 63 69  erator).** speci
1ff10 66 79 20 74 68 65 20 76 61 72 69 6f 75 73 20 6b  fy the various k
1ff11 69 6e 64 73 20 6f 66 20 74 6f 6b 65 6e 73 20 28  inds of tokens (
1ff12 74 65 72 6d 69 6e 61 6c 73 29 20 74 68 61 74 20  terminals) that 
1ff13 74 68 65 20 70 61 72 73 65 72 0a 2a 2a 20 75 6e  the parser.** un
1ff14 64 65 72 73 74 61 6e 64 73 2e 20 0a 2a 2a 0a 2a  derstands. .**.*
1ff15 2a 20 45 61 63 68 20 73 79 6d 62 6f 6c 20 68 65  * Each symbol he
1ff16 72 65 20 69 73 20 61 20 74 65 72 6d 69 6e 61 6c  re is a terminal
1ff17 20 73 79 6d 62 6f 6c 20 69 6e 20 74 68 65 20 67   symbol in the g
1ff18 72 61 6d 6d 61 72 2e 0a 2a 2f 0a 2f 2a 20 4d 61  rammar..*/./* Ma
1ff19 6b 65 20 73 75 72 65 20 74 68 65 20 49 4e 54 45  ke sure the INTE
1ff1a 52 46 41 43 45 20 6d 61 63 72 6f 20 69 73 20 64  RFACE macro is d
1ff1b 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64  efined..*/.#ifnd
1ff1c 65 66 20 49 4e 54 45 52 46 41 43 45 0a 23 20 64  ef INTERFACE.# d
1ff1d 65 66 69 6e 65 20 49 4e 54 45 52 46 41 43 45 20  efine INTERFACE 
1ff1e 31 0a 23 65 6e 64 69 66 0a 2f 2a 20 54 68 65 20  1.#endif./* The 
1ff1f 6e 65 78 74 20 74 68 69 6e 67 20 69 6e 63 6c 75  next thing inclu
1ff20 64 65 64 20 69 73 20 73 65 72 69 65 73 20 6f 66  ded is series of
1ff21 20 64 65 66 69 6e 65 73 20 77 68 69 63 68 20 63   defines which c
1ff22 6f 6e 74 72 6f 6c 0a 2a 2a 20 76 61 72 69 6f 75  ontrol.** variou
1ff23 73 20 61 73 70 65 63 74 73 20 6f 66 20 74 68 65  s aspects of the
1ff24 20 67 65 6e 65 72 61 74 65 64 20 70 61 72 73 65   generated parse
1ff25 72 2e 0a 2a 2a 20 20 20 20 59 59 43 4f 44 45 54  r..**    YYCODET
1ff26 59 50 45 20 20 20 20 20 20 20 20 20 69 73 20 74  YPE         is t
1ff27 68 65 20 64 61 74 61 20 74 79 70 65 20 75 73 65  he data type use
1ff28 64 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 65  d for storing te
1ff29 72 6d 69 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20  rminal.**       
1ff2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ff2b 61 6e 64 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 20  and nonterminal 
1ff2c 6e 75 6d 62 65 72 73 2e 20 20 22 75 6e 73 69 67  numbers.  "unsig
1ff2d 6e 65 64 20 63 68 61 72 22 20 69 73 0a 2a 2a 20  ned char" is.** 
1ff2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ff2f 20 20 20 20 20 20 75 73 65 64 20 69 66 20 74 68        used if th
1ff30 65 72 65 20 61 72 65 20 66 65 77 65 72 20 74 68  ere are fewer th
1ff31 61 6e 20 32 35 30 20 74 65 72 6d 69 6e 61 6c 73  an 250 terminals
1ff32 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
1ff33 20 20 20 20 20 20 20 20 20 20 61 6e 64 20 6e 6f            and no
1ff34 6e 74 65 72 6d 69 6e 61 6c 73 2e 20 20 22 69 6e  nterminals.  "in
1ff35 74 22 20 69 73 20 75 73 65 64 20 6f 74 68 65 72  t" is used other
1ff36 77 69 73 65 2e 0a 2a 2a 20 20 20 20 59 59 4e 4f  wise..**    YYNO
1ff37 43 4f 44 45 20 20 20 20 20 20 20 20 20 20 20 69  CODE           i
1ff38 73 20 61 20 6e 75 6d 62 65 72 20 6f 66 20 74 79  s a number of ty
1ff39 70 65 20 59 59 43 4f 44 45 54 59 50 45 20 77 68  pe YYCODETYPE wh
1ff3a 69 63 68 20 63 6f 72 72 65 73 70 6f 6e 64 73 0a  ich corresponds.
1ff3b 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
1ff3c 20 20 20 20 20 20 20 20 20 74 6f 20 6e 6f 20 6c           to no l
1ff3d 65 67 61 6c 20 74 65 72 6d 69 6e 61 6c 20 6f 72  egal terminal or
1ff3e 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 20 6e 75 6d   nonterminal num
1ff3f 62 65 72 2e 20 20 54 68 69 73 0a 2a 2a 20 20 20  ber.  This.**   
1ff40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ff41 20 20 20 20 6e 75 6d 62 65 72 20 69 73 20 75 73      number is us
1ff42 65 64 20 74 6f 20 66 69 6c 6c 20 69 6e 20 65 6d  ed to fill in em
1ff43 70 74 79 20 73 6c 6f 74 73 20 6f 66 20 74 68 65  pty slots of the
1ff44 20 68 61 73 68 20 0a 2a 2a 20 20 20 20 20 20 20   hash .**       
1ff45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ff46 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 20 59 59 46  table..**    YYF
1ff47 41 4c 4c 42 41 43 4b 20 20 20 20 20 20 20 20 20  ALLBACK         
1ff48 49 66 20 64 65 66 69 6e 65 64 2c 20 74 68 69 73  If defined, this
1ff49 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20   indicates that 
1ff4a 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 6f 6b 65  one or more toke
1ff4b 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ns.**           
1ff4c 20 20 20 20 20 20 20 20 20 20 20 20 68 61 76 65              have
1ff4d 20 66 61 6c 6c 2d 62 61 63 6b 20 76 61 6c 75 65   fall-back value
1ff4e 73 20 77 68 69 63 68 20 73 68 6f 75 6c 64 20 62  s which should b
1ff4f 65 20 75 73 65 64 20 69 66 20 74 68 65 0a 2a 2a  e used if the.**
1ff50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ff51 20 20 20 20 20 20 20 6f 72 69 67 69 6e 61 6c 20         original 
1ff52 76 61 6c 75 65 20 6f 66 20 74 68 65 20 74 6f 6b  value of the tok
1ff53 65 6e 20 77 69 6c 6c 20 6e 6f 74 20 70 61 72 73  en will not pars
1ff54 65 2e 0a 2a 2a 20 20 20 20 59 59 41 43 54 49 4f  e..**    YYACTIO
1ff55 4e 54 59 50 45 20 20 20 20 20 20 20 69 73 20 74  NTYPE       is t
1ff56 68 65 20 64 61 74 61 20 74 79 70 65 20 75 73 65  he data type use
1ff57 64 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 65  d for storing te
1ff58 72 6d 69 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20  rminal.**       
1ff59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ff5a 61 6e 64 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 20  and nonterminal 
1ff5b 6e 75 6d 62 65 72 73 2e 20 20 22 75 6e 73 69 67  numbers.  "unsig
1ff5c 6e 65 64 20 63 68 61 72 22 20 69 73 0a 2a 2a 20  ned char" is.** 
1ff5d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ff5e 20 20 20 20 20 20 75 73 65 64 20 69 66 20 74 68        used if th
1ff5f 65 72 65 20 61 72 65 20 66 65 77 65 72 20 74 68  ere are fewer th
1ff60 61 6e 20 32 35 30 20 72 75 6c 65 73 20 61 6e 64  an 250 rules and
1ff61 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
1ff62 20 20 20 20 20 20 20 20 20 20 73 74 61 74 65 73            states
1ff63 20 63 6f 6d 62 69 6e 65 64 2e 20 20 22 69 6e 74   combined.  "int
1ff64 22 20 69 73 20 75 73 65 64 20 6f 74 68 65 72 77  " is used otherw
1ff65 69 73 65 2e 0a 2a 2a 20 20 20 20 73 71 6c 69 74  ise..**    sqlit
1ff66 65 33 50 61 72 73 65 72 54 4f 4b 45 4e 54 59 50  e3ParserTOKENTYP
1ff67 45 20 20 20 20 20 69 73 20 74 68 65 20 64 61 74  E     is the dat
1ff68 61 20 74 79 70 65 20 75 73 65 64 20 66 6f 72 20  a type used for 
1ff69 6d 69 6e 6f 72 20 74 6f 6b 65 6e 73 20 67 69 76  minor tokens giv
1ff6a 65 6e 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  en .**          
1ff6b 20 20 20 20 20 20 20 20 20 20 20 20 20 64 69 72               dir
1ff6c 65 63 74 6c 79 20 74 6f 20 74 68 65 20 70 61 72  ectly to the par
1ff6d 73 65 72 20 66 72 6f 6d 20 74 68 65 20 74 6f 6b  ser from the tok
1ff6e 65 6e 69 7a 65 72 2e 0a 2a 2a 20 20 20 20 59 59  enizer..**    YY
1ff6f 4d 49 4e 4f 52 54 59 50 45 20 20 20 20 20 20 20  MINORTYPE       
1ff70 20 69 73 20 74 68 65 20 64 61 74 61 20 74 79 70   is the data typ
1ff71 65 20 75 73 65 64 20 66 6f 72 20 61 6c 6c 20 6d  e used for all m
1ff72 69 6e 6f 72 20 74 6f 6b 65 6e 73 2e 0a 2a 2a 20  inor tokens..** 
1ff73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ff74 20 20 20 20 20 20 54 68 69 73 20 69 73 20 74 79        This is ty
1ff75 70 69 63 61 6c 6c 79 20 61 20 75 6e 69 6f 6e 20  pically a union 
1ff76 6f 66 20 6d 61 6e 79 20 74 79 70 65 73 2c 20 6f  of many types, o
1ff77 6e 65 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20  ne of.**        
1ff78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77                 w
1ff79 68 69 63 68 20 69 73 20 73 71 6c 69 74 65 33 50  hich is sqlite3P
1ff7a 61 72 73 65 72 54 4f 4b 45 4e 54 59 50 45 2e 20  arserTOKENTYPE. 
1ff7b 20 54 68 65 20 65 6e 74 72 79 20 69 6e 20 74 68   The entry in th
1ff7c 65 20 75 6e 69 6f 6e 0a 2a 2a 20 20 20 20 20 20  e union.**      
1ff7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ff7e 20 66 6f 72 20 62 61 73 65 20 74 6f 6b 65 6e 73   for base tokens
1ff7f 20 69 73 20 63 61 6c 6c 65 64 20 22 79 79 30 22   is called "yy0"
1ff80 2e 0a 2a 2a 20 20 20 20 59 59 53 54 41 43 4b 44  ..**    YYSTACKD
1ff81 45 50 54 48 20 20 20 20 20 20 20 69 73 20 74 68  EPTH       is th
1ff82 65 20 6d 61 78 69 6d 75 6d 20 64 65 70 74 68 20  e maximum depth 
1ff83 6f 66 20 74 68 65 20 70 61 72 73 65 72 27 73 20  of the parser's 
1ff84 73 74 61 63 6b 2e 20 20 49 66 0a 2a 2a 20 20 20  stack.  If.**   
1ff85 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ff86 20 20 20 20 7a 65 72 6f 20 74 68 65 20 73 74 61      zero the sta
1ff87 63 6b 20 69 73 20 64 79 6e 61 6d 69 63 61 6c 6c  ck is dynamicall
1ff88 79 20 73 69 7a 65 64 20 75 73 69 6e 67 20 72 65  y sized using re
1ff89 61 6c 6c 6f 63 28 29 0a 2a 2a 20 20 20 20 73 71  alloc().**    sq
1ff8a 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 53  lite3ParserARG_S
1ff8b 44 45 43 4c 20 20 20 20 20 41 20 73 74 61 74 69  DECL     A stati
1ff8c 63 20 76 61 72 69 61 62 6c 65 20 64 65 63 6c 61  c variable decla
1ff8d 72 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 25  ration for the %
1ff8e 65 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 0a 2a  extra_argument.*
1ff8f 2a 20 20 20 20 73 71 6c 69 74 65 33 50 61 72 73  *    sqlite3Pars
1ff90 65 72 41 52 47 5f 50 44 45 43 4c 20 20 20 20 20  erARG_PDECL     
1ff91 41 20 70 61 72 61 6d 65 74 65 72 20 64 65 63 6c  A parameter decl
1ff92 61 72 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20  aration for the 
1ff93 25 65 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 0a  %extra_argument.
1ff94 2a 2a 20 20 20 20 73 71 6c 69 74 65 33 50 61 72  **    sqlite3Par
1ff95 73 65 72 41 52 47 5f 53 54 4f 52 45 20 20 20 20  serARG_STORE    
1ff96 20 43 6f 64 65 20 74 6f 20 73 74 6f 72 65 20 25   Code to store %
1ff97 65 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 69  extra_argument i
1ff98 6e 74 6f 20 79 79 70 50 61 72 73 65 72 0a 2a 2a  nto yypParser.**
1ff99 20 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65      sqlite3Parse
1ff9a 72 41 52 47 5f 46 45 54 43 48 20 20 20 20 20 43  rARG_FETCH     C
1ff9b 6f 64 65 20 74 6f 20 65 78 74 72 61 63 74 20 25  ode to extract %
1ff9c 65 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 66  extra_argument f
1ff9d 72 6f 6d 20 79 79 70 50 61 72 73 65 72 0a 2a 2a  rom yypParser.**
1ff9e 20 20 20 20 59 59 4e 53 54 41 54 45 20 20 20 20      YYNSTATE    
1ff9f 20 20 20 20 20 20 20 74 68 65 20 63 6f 6d 62 69         the combi
1ffa0 6e 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 73 74  ned number of st
1ffa1 61 74 65 73 2e 0a 2a 2a 20 20 20 20 59 59 4e 52  ates..**    YYNR
1ffa2 55 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 74  ULE            t
1ffa3 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 75 6c  he number of rul
1ffa4 65 73 20 69 6e 20 74 68 65 20 67 72 61 6d 6d 61  es in the gramma
1ffa5 72 0a 2a 2a 20 20 20 20 59 59 45 52 52 4f 52 53  r.**    YYERRORS
1ffa6 59 4d 42 4f 4c 20 20 20 20 20 20 69 73 20 74 68  YMBOL      is th
1ffa7 65 20 63 6f 64 65 20 6e 75 6d 62 65 72 20 6f 66  e code number of
1ffa8 20 74 68 65 20 65 72 72 6f 72 20 73 79 6d 62 6f   the error symbo
1ffa9 6c 2e 20 20 49 66 20 6e 6f 74 0a 2a 2a 20 20 20  l.  If not.**   
1ffaa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ffab 20 20 20 20 64 65 66 69 6e 65 64 2c 20 74 68 65      defined, the
1ffac 6e 20 64 6f 20 6e 6f 20 65 72 72 6f 72 20 70 72  n do no error pr
1ffad 6f 63 65 73 73 69 6e 67 2e 0a 2a 2f 0a 23 64 65  ocessing..*/.#de
1ffae 66 69 6e 65 20 59 59 43 4f 44 45 54 59 50 45 20  fine YYCODETYPE 
1ffaf 75 6e 73 69 67 6e 65 64 20 63 68 61 72 0a 23 64  unsigned char.#d
1ffb0 65 66 69 6e 65 20 59 59 4e 4f 43 4f 44 45 20 32  efine YYNOCODE 2
1ffb1 34 38 0a 23 64 65 66 69 6e 65 20 59 59 41 43 54  48.#define YYACT
1ffb2 49 4f 4e 54 59 50 45 20 75 6e 73 69 67 6e 65 64  IONTYPE unsigned
1ffb3 20 73 68 6f 72 74 20 69 6e 74 0a 23 64 65 66 69   short int.#defi
1ffb4 6e 65 20 59 59 57 49 4c 44 43 41 52 44 20 35 39  ne YYWILDCARD 59
1ffb5 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
1ffb6 50 61 72 73 65 72 54 4f 4b 45 4e 54 59 50 45 20  ParserTOKENTYPE 
1ffb7 54 6f 6b 65 6e 0a 74 79 70 65 64 65 66 20 75 6e  Token.typedef un
1ffb8 69 6f 6e 20 7b 0a 20 20 73 71 6c 69 74 65 33 50  ion {.  sqlite3P
1ffb9 61 72 73 65 72 54 4f 4b 45 4e 54 59 50 45 20 79  arserTOKENTYPE y
1ffba 79 30 3b 0a 20 20 69 6e 74 20 79 79 34 36 3b 0a  y0;.  int yy46;.
1ffbb 20 20 73 74 72 75 63 74 20 4c 69 6b 65 4f 70 20    struct LikeOp 
1ffbc 79 79 37 32 3b 0a 20 20 45 78 70 72 2a 20 79 79  yy72;.  Expr* yy
1ffbd 31 37 32 3b 0a 20 20 45 78 70 72 4c 69 73 74 2a  172;.  ExprList*
1ffbe 20 79 79 31 37 34 3b 0a 20 20 53 65 6c 65 63 74   yy174;.  Select
1ffbf 2a 20 79 79 32 31 39 3b 0a 20 20 73 74 72 75 63  * yy219;.  struc
1ffc0 74 20 4c 69 6d 69 74 56 61 6c 20 79 79 32 33 34  t LimitVal yy234
1ffc1 3b 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 2a  ;.  TriggerStep*
1ffc2 20 79 79 32 34 33 3b 0a 20 20 73 74 72 75 63 74   yy243;.  struct
1ffc3 20 54 72 69 67 45 76 65 6e 74 20 79 79 33 37 30   TrigEvent yy370
1ffc4 3b 0a 20 20 53 72 63 4c 69 73 74 2a 20 79 79 33  ;.  SrcList* yy3
1ffc5 37 33 3b 0a 20 20 45 78 70 72 20 2a 20 79 79 33  73;.  Expr * yy3
1ffc6 38 36 3b 0a 20 20 73 74 72 75 63 74 20 7b 69 6e  86;.  struct {in
1ffc7 74 20 76 61 6c 75 65 3b 20 69 6e 74 20 6d 61 73  t value; int mas
1ffc8 6b 3b 7d 20 79 79 34 30 35 3b 0a 20 20 54 6f 6b  k;} yy405;.  Tok
1ffc9 65 6e 20 79 79 34 31 30 3b 0a 20 20 49 64 4c 69  en yy410;.  IdLi
1ffca 73 74 2a 20 79 79 34 33 32 3b 0a 20 20 69 6e 74  st* yy432;.  int
1ffcb 20 79 79 34 39 35 3b 0a 7d 20 59 59 4d 49 4e 4f   yy495;.} YYMINO
1ffcc 52 54 59 50 45 3b 0a 23 69 66 6e 64 65 66 20 59  RTYPE;.#ifndef Y
1ffcd 59 53 54 41 43 4b 44 45 50 54 48 0a 23 64 65 66  YSTACKDEPTH.#def
1ffce 69 6e 65 20 59 59 53 54 41 43 4b 44 45 50 54 48  ine YYSTACKDEPTH
1ffcf 20 31 30 30 0a 23 65 6e 64 69 66 0a 23 64 65 66   100.#endif.#def
1ffd0 69 6e 65 20 73 71 6c 69 74 65 33 50 61 72 73 65  ine sqlite3Parse
1ffd1 72 41 52 47 5f 53 44 45 43 4c 20 50 61 72 73 65  rARG_SDECL Parse
1ffd2 20 2a 70 50 61 72 73 65 3b 0a 23 64 65 66 69 6e   *pParse;.#defin
1ffd3 65 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41  e sqlite3ParserA
1ffd4 52 47 5f 50 44 45 43 4c 20 2c 50 61 72 73 65 20  RG_PDECL ,Parse 
1ffd5 2a 70 50 61 72 73 65 0a 23 64 65 66 69 6e 65 20  *pParse.#define 
1ffd6 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47  sqlite3ParserARG
1ffd7 5f 46 45 54 43 48 20 50 61 72 73 65 20 2a 70 50  _FETCH Parse *pP
1ffd8 61 72 73 65 20 3d 20 79 79 70 50 61 72 73 65 72  arse = yypParser
1ffd9 2d 3e 70 50 61 72 73 65 0a 23 64 65 66 69 6e 65  ->pParse.#define
1ffda 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52   sqlite3ParserAR
1ffdb 47 5f 53 54 4f 52 45 20 79 79 70 50 61 72 73 65  G_STORE yypParse
1ffdc 72 2d 3e 70 50 61 72 73 65 20 3d 20 70 50 61 72  r->pParse = pPar
1ffdd 73 65 0a 23 64 65 66 69 6e 65 20 59 59 4e 53 54  se.#define YYNST
1ffde 41 54 45 20 35 38 36 0a 23 64 65 66 69 6e 65 20  ATE 586.#define 
1ffdf 59 59 4e 52 55 4c 45 20 33 31 31 0a 23 64 65 66  YYNRULE 311.#def
1ffe0 69 6e 65 20 59 59 45 52 52 4f 52 53 59 4d 42 4f  ine YYERRORSYMBO
1ffe1 4c 20 31 33 38 0a 23 64 65 66 69 6e 65 20 59 59  L 138.#define YY
1ffe2 45 52 52 53 59 4d 44 54 20 79 79 34 39 35 0a 23  ERRSYMDT yy495.#
1ffe3 64 65 66 69 6e 65 20 59 59 46 41 4c 4c 42 41 43  define YYFALLBAC
1ffe4 4b 20 31 0a 23 64 65 66 69 6e 65 20 59 59 5f 4e  K 1.#define YY_N
1ffe5 4f 5f 41 43 54 49 4f 4e 20 20 20 20 20 20 28 59  O_ACTION      (Y
1ffe6 59 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c 45 2b  YNSTATE+YYNRULE+
1ffe7 32 29 0a 23 64 65 66 69 6e 65 20 59 59 5f 41 43  2).#define YY_AC
1ffe8 43 45 50 54 5f 41 43 54 49 4f 4e 20 20 28 59 59  CEPT_ACTION  (YY
1ffe9 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c 45 2b 31  NSTATE+YYNRULE+1
1ffea 29 0a 23 64 65 66 69 6e 65 20 59 59 5f 45 52 52  ).#define YY_ERR
1ffeb 4f 52 5f 41 43 54 49 4f 4e 20 20 20 28 59 59 4e  OR_ACTION   (YYN
1ffec 53 54 41 54 45 2b 59 59 4e 52 55 4c 45 29 0a 0a  STATE+YYNRULE)..
1ffed 2f 2a 20 4e 65 78 74 20 61 72 65 20 74 68 61 74  /* Next are that
1ffee 20 74 61 62 6c 65 73 20 75 73 65 64 20 74 6f 20   tables used to 
1ffef 64 65 74 65 72 6d 69 6e 65 20 77 68 61 74 20 61  determine what a
1fff0 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65 20 62 61  ction to take ba
1fff1 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 63 75  sed on the.** cu
1fff2 72 72 65 6e 74 20 73 74 61 74 65 20 61 6e 64 20  rrent state and 
1fff3 6c 6f 6f 6b 61 68 65 61 64 20 74 6f 6b 65 6e 2e  lookahead token.
1fff4 20 20 54 68 65 73 65 20 74 61 62 6c 65 73 20 61    These tables a
1fff5 72 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  re used to imple
1fff6 6d 65 6e 74 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  ment.** function
1fff7 73 20 74 68 61 74 20 74 61 6b 65 20 61 20 73 74  s that take a st
1fff8 61 74 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 6c  ate number and l
1fff9 6f 6f 6b 61 68 65 61 64 20 76 61 6c 75 65 20 61  ookahead value a
1fffa 6e 64 20 72 65 74 75 72 6e 20 61 6e 0a 2a 2a 20  nd return an.** 
1fffb 61 63 74 69 6f 6e 20 69 6e 74 65 67 65 72 2e 20  action integer. 
1fffc 20 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73 65 20   .**.** Suppose 
1fffd 74 68 65 20 61 63 74 69 6f 6e 20 69 6e 74 65 67  the action integ
1fffe 65 72 20 69 73 20 4e 2e 20 20 54 68 65 6e 20 74  er is N.  Then t
1ffff 68 65 20 61 63 74 69 6f 6e 20 69 73 20 64 65 74  he action is det
20000 65 72 6d 69 6e 65 64 20 61 73 0a 2a 2a 20 66 6f  ermined as.** fo
20001 6c 6c 6f 77 73 0a 2a 2a 0a 2a 2a 20 20 20 30 20  llows.**.**   0 
20002 3c 3d 20 4e 20 3c 20 59 59 4e 53 54 41 54 45 20  <= N < YYNSTATE 
20003 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20004 20 53 68 69 66 74 20 4e 2e 20 20 54 68 61 74 20   Shift N.  That 
20005 69 73 2c 20 70 75 73 68 20 74 68 65 20 6c 6f 6f  is, push the loo
20006 6b 61 68 65 61 64 0a 2a 2a 20 20 20 20 20 20 20  kahead.**       
20007 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20008 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
20009 6f 6b 65 6e 20 6f 6e 74 6f 20 74 68 65 20 73 74  oken onto the st
2000a 61 63 6b 20 61 6e 64 20 67 6f 74 6f 20 73 74 61  ack and goto sta
2000b 74 65 20 4e 2e 0a 2a 2a 0a 2a 2a 20 20 20 59 59  te N..**.**   YY
2000c 4e 53 54 41 54 45 20 3c 3d 20 4e 20 3c 20 59 59  NSTATE <= N < YY
2000d 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c 45 20 20  NSTATE+YYNRULE  
2000e 20 52 65 64 75 63 65 20 62 79 20 72 75 6c 65 20   Reduce by rule 
2000f 4e 2d 59 59 4e 53 54 41 54 45 2e 0a 2a 2a 0a 2a  N-YYNSTATE..**.*
20010 2a 20 20 20 4e 20 3d 3d 20 59 59 4e 53 54 41 54  *   N == YYNSTAT
20011 45 2b 59 59 4e 52 55 4c 45 20 20 20 20 20 20 20  E+YYNRULE       
20012 20 20 20 20 20 20 20 41 20 73 79 6e 74 61 78 20         A syntax 
20013 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72  error has occurr
20014 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 4e 20 3d 3d  ed..**.**   N ==
20015 20 59 59 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c   YYNSTATE+YYNRUL
20016 45 2b 31 20 20 20 20 20 20 20 20 20 20 20 20 54  E+1            T
20017 68 65 20 70 61 72 73 65 72 20 61 63 63 65 70 74  he parser accept
20018 73 20 69 74 73 20 69 6e 70 75 74 2e 0a 2a 2a 0a  s its input..**.
20019 2a 2a 20 20 20 4e 20 3d 3d 20 59 59 4e 53 54 41  **   N == YYNSTA
2001a 54 45 2b 59 59 4e 52 55 4c 45 2b 32 20 20 20 20  TE+YYNRULE+2    
2001b 20 20 20 20 20 20 20 20 4e 6f 20 73 75 63 68 20          No such 
2001c 61 63 74 69 6f 6e 2e 20 20 44 65 6e 6f 74 65 73  action.  Denotes
2001d 20 75 6e 75 73 65 64 0a 2a 2a 20 20 20 20 20 20   unused.**      
2001e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2001f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20020 73 6c 6f 74 73 20 69 6e 20 74 68 65 20 79 79 5f  slots in the yy_
20021 61 63 74 69 6f 6e 5b 5d 20 74 61 62 6c 65 2e 0a  action[] table..
20022 2a 2a 0a 2a 2a 20 54 68 65 20 61 63 74 69 6f 6e  **.** The action
20023 20 74 61 62 6c 65 20 69 73 20 63 6f 6e 73 74 72   table is constr
20024 75 63 74 65 64 20 61 73 20 61 20 73 69 6e 67 6c  ucted as a singl
20025 65 20 6c 61 72 67 65 20 74 61 62 6c 65 20 6e 61  e large table na
20026 6d 65 64 20 79 79 5f 61 63 74 69 6f 6e 5b 5d 2e  med yy_action[].
20027 0a 2a 2a 20 47 69 76 65 6e 20 73 74 61 74 65 20  .** Given state 
20028 53 20 61 6e 64 20 6c 6f 6f 6b 61 68 65 61 64 20  S and lookahead 
20029 58 2c 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73  X, the action is
2002a 20 63 6f 6d 70 75 74 65 64 20 61 73 0a 2a 2a 0a   computed as.**.
2002b 2a 2a 20 20 20 20 20 20 79 79 5f 61 63 74 69 6f  **      yy_actio
2002c 6e 5b 20 79 79 5f 73 68 69 66 74 5f 6f 66 73 74  n[ yy_shift_ofst
2002d 5b 53 5d 20 2b 20 58 20 5d 0a 2a 2a 0a 2a 2a 20  [S] + X ].**.** 
2002e 49 66 20 74 68 65 20 69 6e 64 65 78 20 76 61 6c  If the index val
2002f 75 65 20 79 79 5f 73 68 69 66 74 5f 6f 66 73 74  ue yy_shift_ofst
20030 5b 53 5d 2b 58 20 69 73 20 6f 75 74 20 6f 66 20  [S]+X is out of 
20031 72 61 6e 67 65 20 6f 72 20 69 66 20 74 68 65 20  range or if the 
20032 76 61 6c 75 65 0a 2a 2a 20 79 79 5f 6c 6f 6f 6b  value.** yy_look
20033 61 68 65 61 64 5b 79 79 5f 73 68 69 66 74 5f 6f  ahead[yy_shift_o
20034 66 73 74 5b 53 5d 2b 58 5d 20 69 73 20 6e 6f 74  fst[S]+X] is not
20035 20 65 71 75 61 6c 20 74 6f 20 58 20 6f 72 20 69   equal to X or i
20036 66 20 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b  f yy_shift_ofst[
20037 53 5d 0a 2a 2a 20 69 73 20 65 71 75 61 6c 20 74  S].** is equal t
20038 6f 20 59 59 5f 53 48 49 46 54 5f 55 53 45 5f 44  o YY_SHIFT_USE_D
20039 46 4c 54 2c 20 69 74 20 6d 65 61 6e 73 20 74 68  FLT, it means th
2003a 61 74 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73  at the action is
2003b 20 6e 6f 74 20 69 6e 20 74 68 65 20 74 61 62 6c   not in the tabl
2003c 65 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20 79 79  e.** and that yy
2003d 5f 64 65 66 61 75 6c 74 5b 53 5d 20 73 68 6f 75  _default[S] shou
2003e 6c 64 20 62 65 20 75 73 65 64 20 69 6e 73 74 65  ld be used inste
2003f 61 64 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ad.  .**.** The 
20040 66 6f 72 6d 75 6c 61 20 61 62 6f 76 65 20 69 73  formula above is
20041 20 66 6f 72 20 63 6f 6d 70 75 74 69 6e 67 20 74   for computing t
20042 68 65 20 61 63 74 69 6f 6e 20 77 68 65 6e 20 74  he action when t
20043 68 65 20 6c 6f 6f 6b 61 68 65 61 64 20 69 73 0a  he lookahead is.
20044 2a 2a 20 61 20 74 65 72 6d 69 6e 61 6c 20 73 79  ** a terminal sy
20045 6d 62 6f 6c 2e 20 20 49 66 20 74 68 65 20 6c 6f  mbol.  If the lo
20046 6f 6b 61 68 65 61 64 20 69 73 20 61 20 6e 6f 6e  okahead is a non
20047 2d 74 65 72 6d 69 6e 61 6c 20 28 61 73 20 6f 63  -terminal (as oc
20048 63 75 72 73 20 61 66 74 65 72 0a 2a 2a 20 61 20  curs after.** a 
20049 72 65 64 75 63 65 20 61 63 74 69 6f 6e 29 20 74  reduce action) t
2004a 68 65 6e 20 74 68 65 20 79 79 5f 72 65 64 75 63  hen the yy_reduc
2004b 65 5f 6f 66 73 74 5b 5d 20 61 72 72 61 79 20 69  e_ofst[] array i
2004c 73 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65 20  s used in place 
2004d 6f 66 0a 2a 2a 20 74 68 65 20 79 79 5f 73 68 69  of.** the yy_shi
2004e 66 74 5f 6f 66 73 74 5b 5d 20 61 72 72 61 79 20  ft_ofst[] array 
2004f 61 6e 64 20 59 59 5f 52 45 44 55 43 45 5f 55 53  and YY_REDUCE_US
20050 45 5f 44 46 4c 54 20 69 73 20 75 73 65 64 20 69  E_DFLT is used i
20051 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 59 59  n place of.** YY
20052 5f 53 48 49 46 54 5f 55 53 45 5f 44 46 4c 54 2e  _SHIFT_USE_DFLT.
20053 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  .**.** The follo
20054 77 69 6e 67 20 61 72 65 20 74 68 65 20 74 61 62  wing are the tab
20055 6c 65 73 20 67 65 6e 65 72 61 74 65 64 20 69 6e  les generated in
20056 20 74 68 69 73 20 73 65 63 74 69 6f 6e 3a 0a 2a   this section:.*
20057 2a 0a 2a 2a 20 20 79 79 5f 61 63 74 69 6f 6e 5b  *.**  yy_action[
20058 5d 20 20 20 20 20 20 20 20 41 20 73 69 6e 67 6c  ]        A singl
20059 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69  e table containi
2005a 6e 67 20 61 6c 6c 20 61 63 74 69 6f 6e 73 2e 0a  ng all actions..
2005b 2a 2a 20 20 79 79 5f 6c 6f 6f 6b 61 68 65 61 64  **  yy_lookahead
2005c 5b 5d 20 20 20 20 20 41 20 74 61 62 6c 65 20 63  []     A table c
2005d 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6c 6f  ontaining the lo
2005e 6f 6b 61 68 65 61 64 20 66 6f 72 20 65 61 63 68  okahead for each
2005f 20 65 6e 74 72 79 20 69 6e 0a 2a 2a 20 20 20 20   entry in.**    
20060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20061 20 79 79 5f 61 63 74 69 6f 6e 2e 20 20 55 73 65   yy_action.  Use
20062 64 20 74 6f 20 64 65 74 65 63 74 20 68 61 73 68  d to detect hash
20063 20 63 6f 6c 6c 69 73 69 6f 6e 73 2e 0a 2a 2a 20   collisions..** 
20064 20 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b 5d   yy_shift_ofst[]
20065 20 20 20 20 46 6f 72 20 65 61 63 68 20 73 74 61      For each sta
20066 74 65 2c 20 74 68 65 20 6f 66 66 73 65 74 20 69  te, the offset i
20067 6e 74 6f 20 79 79 5f 61 63 74 69 6f 6e 20 66 6f  nto yy_action fo
20068 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  r.**            
20069 20 20 20 20 20 20 20 20 20 73 68 69 66 74 69 6e           shiftin
2006a 67 20 74 65 72 6d 69 6e 61 6c 73 2e 0a 2a 2a 20  g terminals..** 
2006b 20 79 79 5f 72 65 64 75 63 65 5f 6f 66 73 74 5b   yy_reduce_ofst[
2006c 5d 20 20 20 46 6f 72 20 65 61 63 68 20 73 74 61  ]   For each sta
2006d 74 65 2c 20 74 68 65 20 6f 66 66 73 65 74 20 69  te, the offset i
2006e 6e 74 6f 20 79 79 5f 61 63 74 69 6f 6e 20 66 6f  nto yy_action fo
2006f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  r.**            
20070 20 20 20 20 20 20 20 20 20 73 68 69 66 74 69 6e           shiftin
20071 67 20 6e 6f 6e 2d 74 65 72 6d 69 6e 61 6c 73 20  g non-terminals 
20072 61 66 74 65 72 20 61 20 72 65 64 75 63 65 2e 0a  after a reduce..
20073 2a 2a 20 20 79 79 5f 64 65 66 61 75 6c 74 5b 5d  **  yy_default[]
20074 20 20 20 20 20 20 20 44 65 66 61 75 6c 74 20 61         Default a
20075 63 74 69 6f 6e 20 66 6f 72 20 65 61 63 68 20 73  ction for each s
20076 74 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tate..*/.static 
20077 63 6f 6e 73 74 20 59 59 41 43 54 49 4f 4e 54 59  const YYACTIONTY
20078 50 45 20 79 79 5f 61 63 74 69 6f 6e 5b 5d 20 3d  PE yy_action[] =
20079 20 7b 0a 20 2f 2a 20 20 20 20 20 30 20 2a 2f 20   {. /*     0 */ 
2007a 20 20 32 39 30 2c 20 20 38 39 38 2c 20 20 31 32    290,  898,  12
2007b 32 2c 20 20 35 38 35 2c 20 20 34 30 37 2c 20 20  2,  585,  407,  
2007c 31 37 30 2c 20 20 20 20 32 2c 20 20 34 33 37 2c  170,    2,  437,
2007d 20 20 20 36 31 2c 20 20 20 36 31 2c 0a 20 2f 2a     61,   61,. /*
2007e 20 20 20 20 31 30 20 2a 2f 20 20 20 20 36 31 2c      10 */    61,
2007f 20 20 20 36 31 2c 20 20 35 31 37 2c 20 20 20 36     61,  517,   6
20080 33 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20  3,   63,   63,  
20081 20 36 33 2c 20 20 20 36 34 2c 20 20 20 36 34 2c   63,   64,   64,
20082 20 20 20 36 35 2c 0a 20 2f 2a 20 20 20 20 32 30     65,. /*    20
20083 20 2a 2f 20 20 20 20 36 35 2c 20 20 20 36 35 2c   */    65,   65,
20084 20 20 20 36 36 2c 20 20 32 33 31 2c 20 20 34 34     66,  231,  44
20085 35 2c 20 20 32 30 39 2c 20 20 34 32 32 2c 20 20  5,  209,  422,  
20086 34 32 38 2c 20 20 20 36 38 2c 20 20 20 36 33 2c  428,   68,   63,
20087 0a 20 2f 2a 20 20 20 20 33 30 20 2a 2f 20 20 20  . /*    30 */   
20088 20 36 33 2c 20 20 20 36 33 2c 20 20 20 36 33 2c   63,   63,   63,
20089 20 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20 36     64,   64,   6
2008a 35 2c 20 20 20 36 35 2c 20 20 20 36 35 2c 20 20  5,   65,   65,  
2008b 20 36 36 2c 20 20 32 33 31 2c 0a 20 2f 2a 20 20   66,  231,. /*  
2008c 20 20 34 30 20 2a 2f 20 20 20 33 38 39 2c 20 20    40 */   389,  
2008d 33 38 36 2c 20 20 33 39 34 2c 20 20 34 34 39 2c  386,  394,  449,
2008e 20 20 20 36 30 2c 20 20 20 35 39 2c 20 20 32 39     60,   59,  29
2008f 35 2c 20 20 34 33 32 2c 20 20 34 33 33 2c 20 20  5,  432,  433,  
20090 34 32 39 2c 0a 20 2f 2a 20 20 20 20 35 30 20 2a  429,. /*    50 *
20091 2f 20 20 20 34 32 39 2c 20 20 20 36 32 2c 20 20  /   429,   62,  
20092 20 36 32 2c 20 20 20 36 31 2c 20 20 20 36 31 2c   62,   61,   61,
20093 20 20 20 36 31 2c 20 20 20 36 31 2c 20 20 32 36     61,   61,  26
20094 31 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 0a 20  1,   63,   63,. 
20095 2f 2a 20 20 20 20 36 30 20 2a 2f 20 20 20 20 36  /*    60 */    6
20096 33 2c 20 20 20 36 33 2c 20 20 20 36 34 2c 20 20  3,   63,   64,  
20097 20 36 34 2c 20 20 20 36 35 2c 20 20 20 36 35 2c   64,   65,   65,
20098 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20 32 33     65,   66,  23
20099 31 2c 20 20 32 39 30 2c 0a 20 2f 2a 20 20 20 20  1,  290,. /*    
2009a 37 30 20 2a 2f 20 20 20 34 39 31 2c 20 20 34 39  70 */   491,  49
2009b 32 2c 20 20 34 33 37 2c 20 20 34 38 37 2c 20 20  2,  437,  487,  
2009c 32 30 36 2c 20 20 20 38 31 2c 20 20 20 36 37 2c  206,   81,   67,
2009d 20 20 34 31 37 2c 20 20 20 36 39 2c 20 20 31 35    417,   69,  15
2009e 32 2c 0a 20 2f 2a 20 20 20 20 38 30 20 2a 2f 20  2,. /*    80 */ 
2009f 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20 20 36     63,   63,   6
200a0 33 2c 20 20 20 36 33 2c 20 20 20 36 34 2c 20 20  3,   63,   64,  
200a1 20 36 34 2c 20 20 20 36 35 2c 20 20 20 36 35 2c   64,   65,   65,
200a2 20 20 20 36 35 2c 20 20 20 36 36 2c 0a 20 2f 2a     65,   66,. /*
200a3 20 20 20 20 39 30 20 2a 2f 20 20 20 32 33 31 2c      90 */   231,
200a4 20 20 20 36 37 2c 20 20 34 36 30 2c 20 20 20 36     67,  460,   6
200a5 39 2c 20 20 31 35 32 2c 20 20 34 32 32 2c 20 20  9,  152,  422,  
200a6 34 32 38 2c 20 20 35 37 31 2c 20 20 32 36 32 2c  428,  571,  262,
200a7 20 20 20 35 38 2c 0a 20 2f 2a 20 20 20 31 30 30     58,. /*   100
200a8 20 2a 2f 20 20 20 20 36 34 2c 20 20 20 36 34 2c   */    64,   64,
200a9 20 20 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36     65,   65,   6
200aa 35 2c 20 20 20 36 36 2c 20 20 32 33 31 2c 20 20  5,   66,  231,  
200ab 33 39 35 2c 20 20 33 39 36 2c 20 20 34 31 39 2c  395,  396,  419,
200ac 0a 20 2f 2a 20 20 20 31 31 30 20 2a 2f 20 20 20  . /*   110 */   
200ad 34 31 39 2c 20 20 34 31 39 2c 20 20 32 39 30 2c  419,  419,  290,
200ae 20 20 20 36 30 2c 20 20 20 35 39 2c 20 20 32 39     60,   59,  29
200af 35 2c 20 20 34 33 32 2c 20 20 34 33 33 2c 20 20  5,  432,  433,  
200b0 34 32 39 2c 20 20 34 32 39 2c 0a 20 2f 2a 20 20  429,  429,. /*  
200b1 20 31 32 30 20 2a 2f 20 20 20 20 36 32 2c 20 20   120 */    62,  
200b2 20 36 32 2c 20 20 20 36 31 2c 20 20 20 36 31 2c   62,   61,   61,
200b3 20 20 20 36 31 2c 20 20 20 36 31 2c 20 20 33 31     61,   61,  31
200b4 35 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20  5,   63,   63,  
200b5 20 36 33 2c 0a 20 2f 2a 20 20 20 31 33 30 20 2a   63,. /*   130 *
200b6 2f 20 20 20 20 36 33 2c 20 20 20 36 34 2c 20 20  /    63,   64,  
200b7 20 36 34 2c 20 20 20 36 35 2c 20 20 20 36 35 2c   64,   65,   65,
200b8 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20 32 33     65,   66,  23
200b9 31 2c 20 20 34 32 32 2c 20 20 34 32 38 2c 0a 20  1,  422,  428,. 
200ba 2f 2a 20 20 20 31 34 30 20 2a 2f 20 20 20 20 39  /*   140 */    9
200bb 33 2c 20 20 20 36 35 2c 20 20 20 36 35 2c 20 20  3,   65,   65,  
200bc 20 36 35 2c 20 20 20 36 36 2c 20 20 32 33 31 2c   65,   66,  231,
200bd 20 20 33 39 34 2c 20 20 32 33 31 2c 20 20 34 31    394,  231,  41
200be 32 2c 20 20 20 33 34 2c 0a 20 2f 2a 20 20 20 31  2,   34,. /*   1
200bf 35 30 20 2a 2f 20 20 20 20 35 36 2c 20 20 32 39  50 */    56,  29
200c0 36 2c 20 20 34 34 30 2c 20 20 34 34 31 2c 20 20  6,  440,  441,  
200c1 34 30 38 2c 20 20 34 38 36 2c 20 20 20 36 30 2c  408,  486,   60,
200c2 20 20 20 35 39 2c 20 20 32 39 35 2c 20 20 34 33     59,  295,  43
200c3 32 2c 0a 20 2f 2a 20 20 20 31 36 30 20 2a 2f 20  2,. /*   160 */ 
200c4 20 20 34 33 33 2c 20 20 34 32 39 2c 20 20 34 32    433,  429,  42
200c5 39 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20 20  9,   62,   62,  
200c6 20 36 31 2c 20 20 20 36 31 2c 20 20 20 36 31 2c   61,   61,   61,
200c7 20 20 20 36 31 2c 20 20 34 38 38 2c 0a 20 2f 2a     61,  488,. /*
200c8 20 20 20 31 37 30 20 2a 2f 20 20 20 20 36 33 2c     170 */    63,
200c9 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20 20 36     63,   63,   6
200ca 33 2c 20 20 20 36 34 2c 20 20 20 36 34 2c 20 20  3,   64,   64,  
200cb 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36 35 2c   65,   65,   65,
200cc 20 20 20 36 36 2c 0a 20 2f 2a 20 20 20 31 38 30     66,. /*   180
200cd 20 2a 2f 20 20 20 32 33 31 2c 20 20 32 39 30 2c   */   231,  290,
200ce 20 20 32 35 35 2c 20 20 35 32 32 2c 20 20 32 39    255,  522,  29
200cf 33 2c 20 20 35 36 39 2c 20 20 31 31 32 2c 20 20  3,  569,  112,  
200d0 34 30 36 2c 20 20 35 32 30 2c 20 20 34 34 39 2c  406,  520,  449,
200d1 0a 20 2f 2a 20 20 20 31 39 30 20 2a 2f 20 20 20  . /*   190 */   
200d2 33 32 39 2c 20 20 33 31 35 2c 20 20 34 30 35 2c  329,  315,  405,
200d3 20 20 20 32 30 2c 20 20 34 33 37 2c 20 20 33 33     20,  437,  33
200d4 38 2c 20 20 35 31 37 2c 20 20 33 39 34 2c 20 20  8,  517,  394,  
200d5 35 33 30 2c 20 20 35 32 39 2c 0a 20 2f 2a 20 20  530,  529,. /*  
200d6 20 32 30 30 20 2a 2f 20 20 20 35 30 33 2c 20 20   200 */   503,  
200d7 34 34 35 2c 20 20 32 30 39 2c 20 20 35 36 38 2c  445,  209,  568,
200d8 20 20 35 36 37 2c 20 20 32 30 36 2c 20 20 35 32    567,  206,  52
200d9 38 2c 20 20 34 32 32 2c 20 20 34 32 38 2c 20 20  8,  422,  428,  
200da 31 34 37 2c 0a 20 2f 2a 20 20 20 32 31 30 20 2a  147,. /*   210 *
200db 2f 20 20 20 31 34 38 2c 20 20 33 39 35 2c 20 20  /   148,  395,  
200dc 33 39 36 2c 20 20 34 31 32 2c 20 20 20 34 31 2c  396,  412,   41,
200dd 20 20 32 30 38 2c 20 20 31 34 39 2c 20 20 35 33    208,  149,  53
200de 31 2c 20 20 33 37 30 2c 20 20 34 38 37 2c 0a 20  1,  370,  487,. 
200df 2f 2a 20 20 20 32 32 30 20 2a 2f 20 20 20 32 35  /*   220 */   25
200e0 39 2c 20 20 35 36 36 2c 20 20 32 35 37 2c 20 20  9,  566,  257,  
200e1 34 31 37 2c 20 20 32 39 30 2c 20 20 20 36 30 2c  417,  290,   60,
200e2 20 20 20 35 39 2c 20 20 32 39 35 2c 20 20 34 33     59,  295,  43
200e3 32 2c 20 20 34 33 33 2c 0a 20 2f 2a 20 20 20 32  2,  433,. /*   2
200e4 33 30 20 2a 2f 20 20 20 34 32 39 2c 20 20 34 32  30 */   429,  42
200e5 39 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20 20  9,   62,   62,  
200e6 20 36 31 2c 20 20 20 36 31 2c 20 20 20 36 31 2c   61,   61,   61,
200e7 20 20 20 36 31 2c 20 20 33 31 35 2c 20 20 20 36     61,  315,   6
200e8 33 2c 0a 20 2f 2a 20 20 20 32 34 30 20 2a 2f 20  3,. /*   240 */ 
200e9 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20 20 36     63,   63,   6
200ea 33 2c 20 20 20 36 34 2c 20 20 20 36 34 2c 20 20  3,   64,   64,  
200eb 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36 35 2c   65,   65,   65,
200ec 20 20 20 36 36 2c 20 20 32 33 31 2c 0a 20 2f 2a     66,  231,. /*
200ed 20 20 20 32 35 30 20 2a 2f 20 20 20 34 32 32 2c     250 */   422,
200ee 20 20 34 32 38 2c 20 20 34 34 35 2c 20 20 33 33    428,  445,  33
200ef 31 2c 20 20 32 31 32 2c 20 20 34 31 39 2c 20 20  1,  212,  419,  
200f0 34 31 39 2c 20 20 34 31 39 2c 20 20 33 36 31 2c  419,  419,  361,
200f1 20 20 34 33 37 2c 0a 20 2f 2a 20 20 20 32 36 30    437,. /*   260
200f2 20 2a 2f 20 20 20 34 31 32 2c 20 20 20 34 31 2c   */   412,   41,
200f3 20 20 33 39 35 2c 20 20 33 39 36 2c 20 20 33 36    395,  396,  36
200f4 34 2c 20 20 35 36 35 2c 20 20 32 30 38 2c 20 20  4,  565,  208,  
200f5 32 39 30 2c 20 20 20 36 30 2c 20 20 20 35 39 2c  290,   60,   59,
200f6 0a 20 2f 2a 20 20 20 32 37 30 20 2a 2f 20 20 20  . /*   270 */   
200f7 32 39 35 2c 20 20 34 33 32 2c 20 20 34 33 33 2c  295,  432,  433,
200f8 20 20 34 32 39 2c 20 20 34 32 39 2c 20 20 20 36    429,  429,   6
200f9 32 2c 20 20 20 36 32 2c 20 20 20 36 31 2c 20 20  2,   62,   61,  
200fa 20 36 31 2c 20 20 20 36 31 2c 0a 20 2f 2a 20 20   61,   61,. /*  
200fb 20 32 38 30 20 2a 2f 20 20 20 20 36 31 2c 20 20   280 */    61,  
200fc 33 39 34 2c 20 20 20 36 33 2c 20 20 20 36 33 2c  394,   63,   63,
200fd 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20 20 36     63,   63,   6
200fe 34 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20  4,   64,   65,  
200ff 20 36 35 2c 0a 20 2f 2a 20 20 20 32 39 30 20 2a   65,. /*   290 *
20100 2f 20 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20  /    65,   66,  
20101 32 33 31 2c 20 20 34 32 32 2c 20 20 34 32 38 2c  231,  422,  428,
20102 20 20 34 38 39 2c 20 20 32 39 38 2c 20 20 35 32    489,  298,  52
20103 32 2c 20 20 34 37 32 2c 20 20 20 36 36 2c 0a 20  2,  472,   66,. 
20104 2f 2a 20 20 20 33 30 30 20 2a 2f 20 20 20 32 33  /*   300 */   23
20105 31 2c 20 20 32 31 31 2c 20 20 34 37 32 2c 20 20  1,  211,  472,  
20106 32 32 34 2c 20 20 34 30 39 2c 20 20 32 38 34 2c  224,  409,  284,
20107 20 20 35 33 32 2c 20 20 20 32 30 2c 20 20 34 34    532,   20,  44
20108 37 2c 20 20 35 32 31 2c 0a 20 2f 2a 20 20 20 33  7,  521,. /*   3
20109 31 30 20 2a 2f 20 20 20 31 36 36 2c 20 20 20 36  10 */   166,   6
2010a 30 2c 20 20 20 35 39 2c 20 20 32 39 35 2c 20 20  0,   59,  295,  
2010b 34 33 32 2c 20 20 34 33 33 2c 20 20 34 32 39 2c  432,  433,  429,
2010c 20 20 34 32 39 2c 20 20 20 36 32 2c 20 20 20 36    429,   62,   6
2010d 32 2c 0a 20 2f 2a 20 20 20 33 32 30 20 2a 2f 20  2,. /*   320 */ 
2010e 20 20 20 36 31 2c 20 20 20 36 31 2c 20 20 20 36     61,   61,   6
2010f 31 2c 20 20 20 36 31 2c 20 20 34 37 32 2c 20 20  1,   61,  472,  
20110 20 36 33 2c 20 20 20 36 33 2c 20 20 20 36 33 2c   63,   63,   63,
20111 20 20 20 36 33 2c 20 20 20 36 34 2c 0a 20 2f 2a     63,   64,. /*
20112 20 20 20 33 33 30 20 2a 2f 20 20 20 20 36 34 2c     330 */    64,
20113 20 20 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36     65,   65,   6
20114 35 2c 20 20 20 36 36 2c 20 20 32 33 31 2c 20 20  5,   66,  231,  
20115 32 30 37 2c 20 20 34 37 38 2c 20 20 33 31 35 2c  207,  478,  315,
20116 20 20 20 37 36 2c 0a 20 2f 2a 20 20 20 33 34 30     76,. /*   340
20117 20 2a 2f 20 20 20 32 39 30 2c 20 20 32 33 35 2c   */   290,  235,
20118 20 20 32 39 38 2c 20 20 20 35 35 2c 20 20 34 38    298,   55,  48
20119 32 2c 20 20 32 32 35 2c 20 20 33 39 35 2c 20 20  2,  225,  395,  
2011a 33 39 36 2c 20 20 31 37 39 2c 20 20 35 34 35 2c  396,  179,  545,
2011b 0a 20 2f 2a 20 20 20 33 35 30 20 2a 2f 20 20 20  . /*   350 */   
2011c 34 39 32 2c 20 20 33 34 33 2c 20 20 33 34 36 2c  492,  343,  346,
2011d 20 20 33 34 37 2c 20 20 20 36 37 2c 20 20 31 35    347,   67,  15
2011e 30 2c 20 20 20 36 39 2c 20 20 31 35 32 2c 20 20  0,   69,  152,  
2011f 33 33 37 2c 20 20 35 32 32 2c 0a 20 2f 2a 20 20  337,  522,. /*  
20120 20 33 36 30 20 2a 2f 20 20 20 34 31 32 2c 20 20   360 */   412,  
20121 20 33 35 2c 20 20 33 34 38 2c 20 20 32 33 37 2c   35,  348,  237,
20122 20 20 32 34 39 2c 20 20 33 36 38 2c 20 20 34 32    249,  368,  42
20123 32 2c 20 20 34 32 38 2c 20 20 35 37 36 2c 20 20  2,  428,  576,  
20124 20 32 30 2c 0a 20 2f 2a 20 20 20 33 37 30 20 2a   20,. /*   370 *
20125 2f 20 20 20 31 36 32 2c 20 20 31 31 36 2c 20 20  /   162,  116,  
20126 32 33 39 2c 20 20 33 34 31 2c 20 20 32 34 34 2c  239,  341,  244,
20127 20 20 33 34 32 2c 20 20 31 37 34 2c 20 20 33 32    342,  174,  32
20128 30 2c 20 20 34 34 30 2c 20 20 34 34 31 2c 0a 20  0,  440,  441,. 
20129 2f 2a 20 20 20 33 38 30 20 2a 2f 20 20 20 34 31  /*   380 */   41
2012a 32 2c 20 20 20 20 33 2c 20 20 20 37 39 2c 20 20  2,    3,   79,  
2012b 32 35 30 2c 20 20 20 36 30 2c 20 20 20 35 39 2c  250,   60,   59,
2012c 20 20 32 39 35 2c 20 20 34 33 32 2c 20 20 34 33    295,  432,  43
2012d 33 2c 20 20 34 32 39 2c 0a 20 2f 2a 20 20 20 33  3,  429,. /*   3
2012e 39 30 20 2a 2f 20 20 20 34 32 39 2c 20 20 20 36  90 */   429,   6
2012f 32 2c 20 20 20 36 32 2c 20 20 20 36 31 2c 20 20  2,   62,   61,  
20130 20 36 31 2c 20 20 20 36 31 2c 20 20 20 36 31 2c   61,   61,   61,
20131 20 20 31 37 32 2c 20 20 20 36 33 2c 20 20 20 36    172,   63,   6
20132 33 2c 0a 20 2f 2a 20 20 20 34 30 30 20 2a 2f 20  3,. /*   400 */ 
20133 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20 20 36     63,   63,   6
20134 34 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20  4,   64,   65,  
20135 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36 36 2c   65,   65,   66,
20136 20 20 32 33 31 2c 20 20 32 39 30 2c 0a 20 2f 2a    231,  290,. /*
20137 20 20 20 34 31 30 20 2a 2f 20 20 20 32 34 39 2c     410 */   249,
20138 20 20 35 34 38 2c 20 20 32 33 32 2c 20 20 34 38    548,  232,  48
20139 35 2c 20 20 35 30 38 2c 20 20 33 35 31 2c 20 20  5,  508,  351,  
2013a 33 31 35 2c 20 20 31 31 36 2c 20 20 32 33 39 2c  315,  116,  239,
2013b 20 20 33 34 31 2c 0a 20 2f 2a 20 20 20 34 32 30    341,. /*   420
2013c 20 2a 2f 20 20 20 32 34 34 2c 20 20 33 34 32 2c   */   244,  342,
2013d 20 20 31 37 34 2c 20 20 31 37 39 2c 20 20 33 31    174,  179,  31
2013e 35 2c 20 20 35 32 33 2c 20 20 33 34 33 2c 20 20  5,  523,  343,  
2013f 33 34 36 2c 20 20 33 34 37 2c 20 20 32 35 30 2c  346,  347,  250,
20140 0a 20 2f 2a 20 20 20 34 33 30 20 2a 2f 20 20 20  . /*   430 */   
20141 32 31 38 2c 20 20 34 31 33 2c 20 20 31 35 33 2c  218,  413,  153,
20142 20 20 34 36 32 2c 20 20 35 30 39 2c 20 20 34 32    462,  509,  42
20143 32 2c 20 20 34 32 38 2c 20 20 33 34 38 2c 20 20  2,  428,  348,  
20144 34 31 32 2c 20 20 20 33 34 2c 0a 20 2f 2a 20 20  412,   34,. /*  
20145 20 34 34 30 20 2a 2f 20 20 20 34 36 33 2c 20 20   440 */   463,  
20146 32 30 38 2c 20 20 31 37 35 2c 20 20 31 37 33 2c  208,  175,  173,
20147 20 20 31 35 38 2c 20 20 32 33 33 2c 20 20 34 31    158,  233,  41
20148 32 2c 20 20 20 33 34 2c 20 20 33 33 36 2c 20 20  2,   34,  336,  
20149 35 34 37 2c 0a 20 2f 2a 20 20 20 34 35 30 20 2a  547,. /*   450 *
2014a 2f 20 20 20 34 34 37 2c 20 20 33 32 31 2c 20 20  /   447,  321,  
2014b 31 36 36 2c 20 20 20 36 30 2c 20 20 20 35 39 2c  166,   60,   59,
2014c 20 20 32 39 35 2c 20 20 34 33 32 2c 20 20 34 33    295,  432,  43
2014d 33 2c 20 20 34 32 39 2c 20 20 34 32 39 2c 0a 20  3,  429,  429,. 
2014e 2f 2a 20 20 20 34 36 30 20 2a 2f 20 20 20 20 36  /*   460 */    6
2014f 32 2c 20 20 20 36 32 2c 20 20 20 36 31 2c 20 20  2,   62,   61,  
20150 20 36 31 2c 20 20 20 36 31 2c 20 20 20 36 31 2c   61,   61,   61,
20151 20 20 34 31 33 2c 20 20 20 36 33 2c 20 20 20 36    413,   63,   6
20152 33 2c 20 20 20 36 33 2c 0a 20 2f 2a 20 20 20 34  3,   63,. /*   4
20153 37 30 20 2a 2f 20 20 20 20 36 33 2c 20 20 20 36  70 */    63,   6
20154 34 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20  4,   64,   65,  
20155 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36 36 2c   65,   65,   66,
20156 20 20 32 33 31 2c 20 20 32 39 30 2c 20 20 35 34    231,  290,  54
20157 30 2c 0a 20 2f 2a 20 20 20 34 38 30 20 2a 2f 20  0,. /*   480 */ 
20158 20 20 33 33 33 2c 20 20 35 31 35 2c 20 20 35 30    333,  515,  50
20159 32 2c 20 20 35 33 39 2c 20 20 34 35 34 2c 20 20  2,  539,  454,  
2015a 35 36 39 2c 20 20 33 30 30 2c 20 20 20 31 39 2c  569,  300,   19,
2015b 20 20 33 32 39 2c 20 20 31 34 32 2c 0a 20 2f 2a    329,  142,. /*
2015c 20 20 20 34 39 30 20 2a 2f 20 20 20 33 31 35 2c     490 */   315,
2015d 20 20 33 38 38 2c 20 20 33 31 35 2c 20 20 33 32    388,  315,  32
2015e 38 2c 20 20 20 20 32 2c 20 20 33 36 30 2c 20 20  8,    2,  360,  
2015f 34 35 35 2c 20 20 32 39 32 2c 20 20 34 38 31 2c  455,  292,  481,
20160 20 20 33 37 31 2c 0a 20 2f 2a 20 20 20 35 30 30    371,. /*   500
20161 20 2a 2f 20 20 20 32 36 37 2c 20 20 32 36 36 2c   */   267,  266,
20162 20 20 32 35 30 2c 20 20 35 36 38 2c 20 20 34 32    250,  568,  42
20163 32 2c 20 20 34 32 38 2c 20 20 35 38 36 2c 20 20  2,  428,  586,  
20164 33 38 39 2c 20 20 33 38 36 2c 20 20 34 35 36 2c  389,  386,  456,
20165 0a 20 2f 2a 20 20 20 35 31 30 20 2a 2f 20 20 20  . /*   510 */   
20166 32 30 36 2c 20 20 34 39 33 2c 20 20 34 31 32 2c  206,  493,  412,
20167 20 20 20 34 39 2c 20 20 34 31 32 2c 20 20 20 34     49,  412,   4
20168 39 2c 20 20 33 30 31 2c 20 20 35 38 33 2c 20 20  9,  301,  583,  
20169 38 38 39 2c 20 20 31 35 37 2c 0a 20 2f 2a 20 20  889,  157,. /*  
2016a 20 35 32 30 20 2a 2f 20 20 20 38 38 39 2c 20 20   520 */   889,  
2016b 34 39 34 2c 20 20 20 36 30 2c 20 20 20 35 39 2c  494,   60,   59,
2016c 20 20 32 39 35 2c 20 20 34 33 32 2c 20 20 34 33    295,  432,  43
2016d 33 2c 20 20 34 32 39 2c 20 20 34 32 39 2c 20 20  3,  429,  429,  
2016e 20 36 32 2c 0a 20 2f 2a 20 20 20 35 33 30 20 2a   62,. /*   530 *
2016f 2f 20 20 20 20 36 32 2c 20 20 20 36 31 2c 20 20  /    62,   61,  
20170 20 36 31 2c 20 20 20 36 31 2c 20 20 20 36 31 2c   61,   61,   61,
20171 20 20 31 39 39 2c 20 20 20 36 33 2c 20 20 20 36    199,   63,   6
20172 33 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 0a 20  3,   63,   63,. 
20173 2f 2a 20 20 20 35 34 30 20 2a 2f 20 20 20 20 36  /*   540 */    6
20174 34 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20  4,   64,   65,  
20175 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36 36 2c   65,   65,   66,
20176 20 20 32 33 31 2c 20 20 32 39 30 2c 20 20 33 31    231,  290,  31
20177 35 2c 20 20 31 37 39 2c 0a 20 2f 2a 20 20 20 35  5,  179,. /*   5
20178 35 30 20 2a 2f 20 20 20 34 33 36 2c 20 20 32 35  50 */   436,  25
20179 33 2c 20 20 33 34 33 2c 20 20 33 34 36 2c 20 20  3,  343,  346,  
2017a 33 34 37 2c 20 20 33 36 38 2c 20 20 31 35 31 2c  347,  368,  151,
2017b 20 20 35 38 30 2c 20 20 33 30 36 2c 20 20 32 34    580,  306,  24
2017c 38 2c 0a 20 2f 2a 20 20 20 35 36 30 20 2a 2f 20  8,. /*   560 */ 
2017d 20 20 33 30 37 2c 20 20 34 35 30 2c 20 20 20 37    307,  450,   7
2017e 35 2c 20 20 33 34 38 2c 20 20 20 37 37 2c 20 20  5,  348,   77,  
2017f 33 38 30 2c 20 20 32 30 38 2c 20 20 34 32 33 2c  380,  208,  423,
20180 20 20 34 32 34 2c 20 20 34 31 33 2c 0a 20 2f 2a    424,  413,. /*
20181 20 20 20 35 37 30 20 2a 2f 20 20 20 34 31 32 2c     570 */   412,
20182 20 20 20 32 37 2c 20 20 33 31 37 2c 20 20 34 32     27,  317,  42
20183 32 2c 20 20 34 32 38 2c 20 20 34 33 38 2c 20 20  2,  428,  438,  
20184 20 20 31 2c 20 20 20 32 32 2c 20 20 35 38 33 2c    1,   22,  583,
20185 20 20 38 38 38 2c 0a 20 2f 2a 20 20 20 35 38 30    888,. /*   580
20186 20 2a 2f 20 20 20 33 39 34 2c 20 20 38 38 38 2c   */   394,  888,
20187 20 20 35 34 32 2c 20 20 34 37 36 2c 20 20 33 31    542,  476,  31
20188 38 2c 20 20 32 36 31 2c 20 20 34 33 35 2c 20 20  8,  261,  435,  
20189 34 33 35 2c 20 20 34 32 36 2c 20 20 34 32 37 2c  435,  426,  427,
2018a 0a 20 2f 2a 20 20 20 35 39 30 20 2a 2f 20 20 20  . /*   590 */   
2018b 34 31 33 2c 20 20 20 36 30 2c 20 20 20 35 39 2c  413,   60,   59,
2018c 20 20 32 39 35 2c 20 20 34 33 32 2c 20 20 34 33    295,  432,  43
2018d 33 2c 20 20 34 32 39 2c 20 20 34 32 39 2c 20 20  3,  429,  429,  
2018e 20 36 32 2c 20 20 20 36 32 2c 0a 20 2f 2a 20 20   62,   62,. /*  
2018f 20 36 30 30 20 2a 2f 20 20 20 20 36 31 2c 20 20   600 */    61,  
20190 20 36 31 2c 20 20 20 36 31 2c 20 20 20 36 31 2c   61,   61,   61,
20191 20 20 33 32 36 2c 20 20 20 36 33 2c 20 20 20 36    326,   63,   6
20192 33 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20  3,   63,   63,  
20193 20 36 34 2c 0a 20 2f 2a 20 20 20 36 31 30 20 2a   64,. /*   610 *
20194 2f 20 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20  /    64,   65,  
20195 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36 36 2c   65,   65,   66,
20196 20 20 32 33 31 2c 20 20 32 39 30 2c 20 20 34 32    231,  290,  42
20197 35 2c 20 20 35 38 30 2c 20 20 33 37 32 2c 0a 20  5,  580,  372,. 
20198 2f 2a 20 20 20 36 32 30 20 2a 2f 20 20 20 32 31  /*   620 */   21
20199 39 2c 20 20 20 39 32 2c 20 20 35 31 35 2c 20 20  9,   92,  515,  
2019a 20 20 39 2c 20 20 33 33 34 2c 20 20 33 39 34 2c    9,  334,  394,
2019b 20 20 35 35 35 2c 20 20 33 39 34 2c 20 20 34 35    555,  394,  45
2019c 34 2c 20 20 20 36 37 2c 0a 20 2f 2a 20 20 20 36  4,   67,. /*   6
2019d 33 30 20 2a 2f 20 20 20 33 39 34 2c 20 20 20 36  30 */   394,   6
2019e 39 2c 20 20 31 35 32 2c 20 20 33 39 37 2c 20 20  9,  152,  397,  
2019f 33 39 38 2c 20 20 33 39 39 2c 20 20 33 31 38 2c  398,  399,  318,
201a0 20 20 32 33 34 2c 20 20 34 33 35 2c 20 20 34 33    234,  435,  43
201a1 35 2c 0a 20 2f 2a 20 20 20 36 34 30 20 2a 2f 20  5,. /*   640 */ 
201a2 20 20 34 35 35 2c 20 20 33 31 36 2c 20 20 34 32    455,  316,  42
201a3 32 2c 20 20 34 32 38 2c 20 20 32 39 37 2c 20 20  2,  428,  297,  
201a4 33 39 35 2c 20 20 33 39 36 2c 20 20 33 31 38 2c  395,  396,  318,
201a5 20 20 34 33 30 2c 20 20 34 33 35 2c 0a 20 2f 2a    430,  435,. /*
201a6 20 20 20 36 35 30 20 2a 2f 20 20 20 34 33 35 2c     650 */   435,
201a7 20 20 35 37 39 2c 20 20 32 38 39 2c 20 20 34 35    579,  289,  45
201a8 36 2c 20 20 32 32 30 2c 20 20 33 32 35 2c 20 20  6,  220,  325,  
201a9 20 20 35 2c 20 20 32 31 37 2c 20 20 35 34 34 2c    5,  217,  544,
201aa 20 20 32 39 30 2c 0a 20 2f 2a 20 20 20 36 36 30    290,. /*   660
201ab 20 2a 2f 20 20 20 20 36 30 2c 20 20 20 35 39 2c   */    60,   59,
201ac 20 20 32 39 35 2c 20 20 34 33 32 2c 20 20 34 33    295,  432,  43
201ad 33 2c 20 20 34 32 39 2c 20 20 34 32 39 2c 20 20  3,  429,  429,  
201ae 20 36 32 2c 20 20 20 36 32 2c 20 20 20 36 31 2c   62,   62,   61,
201af 0a 20 2f 2a 20 20 20 36 37 30 20 2a 2f 20 20 20  . /*   670 */   
201b0 20 36 31 2c 20 20 20 36 31 2c 20 20 20 36 31 2c   61,   61,   61,
201b1 20 20 33 39 33 2c 20 20 20 36 33 2c 20 20 20 36    393,   63,   6
201b2 33 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20  3,   63,   63,  
201b3 20 36 34 2c 20 20 20 36 34 2c 0a 20 2f 2a 20 20   64,   64,. /*  
201b4 20 36 38 30 20 2a 2f 20 20 20 20 36 35 2c 20 20   680 */    65,  
201b5 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36 36 2c   65,   65,   66,
201b6 20 20 32 33 31 2c 20 20 34 32 32 2c 20 20 34 32    231,  422,  42
201b7 38 2c 20 20 34 38 30 2c 20 20 33 31 31 2c 20 20  8,  480,  311,  
201b8 33 39 30 2c 0a 20 2f 2a 20 20 20 36 39 30 20 2a  390,. /*   690 *
201b9 2f 20 20 20 33 39 35 2c 20 20 33 39 36 2c 20 20  /   395,  396,  
201ba 33 39 35 2c 20 20 33 39 36 2c 20 20 32 30 35 2c  395,  396,  205,
201bb 20 20 33 39 35 2c 20 20 33 39 36 2c 20 20 38 32    395,  396,  82
201bc 31 2c 20 20 32 37 31 2c 20 20 35 31 35 2c 0a 20  1,  271,  515,. 
201bd 2f 2a 20 20 20 37 30 30 20 2a 2f 20 20 20 32 34  /*   700 */   24
201be 38 2c 20 20 31 39 38 2c 20 20 32 39 30 2c 20 20  8,  198,  290,  
201bf 20 36 30 2c 20 20 20 35 39 2c 20 20 32 39 35 2c   60,   59,  295,
201c0 20 20 34 33 32 2c 20 20 34 33 33 2c 20 20 34 32    432,  433,  42
201c1 39 2c 20 20 34 32 39 2c 0a 20 2f 2a 20 20 20 37  9,  429,. /*   7
201c2 31 30 20 2a 2f 20 20 20 20 36 32 2c 20 20 20 36  10 */    62,   6
201c3 32 2c 20 20 20 36 31 2c 20 20 20 36 31 2c 20 20  2,   61,   61,  
201c4 20 36 31 2c 20 20 20 36 31 2c 20 20 34 36 38 2c   61,   61,  468,
201c5 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20 20 36     63,   63,   6
201c6 33 2c 0a 20 2f 2a 20 20 20 37 32 30 20 2a 2f 20  3,. /*   720 */ 
201c7 20 20 20 36 33 2c 20 20 20 36 34 2c 20 20 20 36     63,   64,   6
201c8 34 2c 20 20 20 36 35 2c 20 20 20 36 35 2c 20 20  4,   65,   65,  
201c9 20 36 35 2c 20 20 20 36 36 2c 20 20 32 33 31 2c   65,   66,  231,
201ca 20 20 34 32 32 2c 20 20 34 32 38 2c 0a 20 2f 2a    422,  428,. /*
201cb 20 20 20 37 33 30 20 2a 2f 20 20 20 31 36 39 2c     730 */   169,
201cc 20 20 31 35 38 2c 20 20 32 36 31 2c 20 20 32 36    158,  261,  26
201cd 31 2c 20 20 33 30 32 2c 20 20 34 31 33 2c 20 20  1,  302,  413,  
201ce 32 37 34 2c 20 20 31 31 37 2c 20 20 32 37 32 2c  274,  117,  272,
201cf 20 20 32 36 31 2c 0a 20 2f 2a 20 20 20 37 34 30    261,. /*   740
201d0 20 2a 2f 20 20 20 35 31 35 2c 20 20 35 31 35 2c   */   515,  515,
201d1 20 20 32 36 31 2c 20 20 35 31 35 2c 20 20 31 39    261,  515,  19
201d2 30 2c 20 20 32 39 30 2c 20 20 20 36 30 2c 20 20  0,  290,   60,  
201d3 20 37 30 2c 20 20 32 39 35 2c 20 20 34 33 32 2c   70,  295,  432,
201d4 0a 20 2f 2a 20 20 20 37 35 30 20 2a 2f 20 20 20  . /*   750 */   
201d5 34 33 33 2c 20 20 34 32 39 2c 20 20 34 32 39 2c  433,  429,  429,
201d6 20 20 20 36 32 2c 20 20 20 36 32 2c 20 20 20 36     62,   62,   6
201d7 31 2c 20 20 20 36 31 2c 20 20 20 36 31 2c 20 20  1,   61,   61,  
201d8 20 36 31 2c 20 20 33 37 37 2c 0a 20 2f 2a 20 20   61,  377,. /*  
201d9 20 37 36 30 20 2a 2f 20 20 20 20 36 33 2c 20 20   760 */    63,  
201da 20 36 33 2c 20 20 20 36 33 2c 20 20 20 36 33 2c   63,   63,   63,
201db 20 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20 36     64,   64,   6
201dc 35 2c 20 20 20 36 35 2c 20 20 20 36 35 2c 20 20  5,   65,   65,  
201dd 20 36 36 2c 0a 20 2f 2a 20 20 20 37 37 30 20 2a   66,. /*   770 *
201de 2f 20 20 20 32 33 31 2c 20 20 34 32 32 2c 20 20  /   231,  422,  
201df 34 32 38 2c 20 20 33 38 32 2c 20 20 35 35 37 2c  428,  382,  557,
201e0 20 20 33 30 33 2c 20 20 33 30 34 2c 20 20 32 34    303,  304,  24
201e1 38 2c 20 20 34 31 33 2c 20 20 33 31 38 2c 0a 20  8,  413,  318,. 
201e2 2f 2a 20 20 20 37 38 30 20 2a 2f 20 20 20 35 35  /*   780 */   55
201e3 38 2c 20 20 34 33 35 2c 20 20 34 33 35 2c 20 20  8,  435,  435,  
201e4 35 35 39 2c 20 20 35 33 38 2c 20 20 33 35 38 2c  559,  538,  358,
201e5 20 20 35 33 38 2c 20 20 33 38 35 2c 20 20 32 39    538,  385,  29
201e6 30 2c 20 20 31 39 34 2c 0a 20 2f 2a 20 20 20 37  0,  194,. /*   7
201e7 39 30 20 2a 2f 20 20 20 20 35 39 2c 20 20 32 39  90 */    59,  29
201e8 35 2c 20 20 34 33 32 2c 20 20 34 33 33 2c 20 20  5,  432,  433,  
201e9 34 32 39 2c 20 20 34 32 39 2c 20 20 20 36 32 2c  429,  429,   62,
201ea 20 20 20 36 32 2c 20 20 20 36 31 2c 20 20 20 36     62,   61,   6
201eb 31 2c 0a 20 2f 2a 20 20 20 38 30 30 20 2a 2f 20  1,. /*   800 */ 
201ec 20 20 20 36 31 2c 20 20 20 36 31 2c 20 20 33 36     61,   61,  36
201ed 39 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20  9,   63,   63,  
201ee 20 36 33 2c 20 20 20 36 33 2c 20 20 20 36 34 2c   63,   63,   64,
201ef 20 20 20 36 34 2c 20 20 20 36 35 2c 0a 20 2f 2a     64,   65,. /*
201f0 20 20 20 38 31 30 20 2a 2f 20 20 20 20 36 35 2c     810 */    65,
201f1 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20 32 33     65,   66,  23
201f2 31 2c 20 20 34 32 32 2c 20 20 34 32 38 2c 20 20  1,  422,  428,  
201f3 33 39 34 2c 20 20 32 37 33 2c 20 20 32 34 38 2c  394,  273,  248,
201f4 20 20 32 34 38 2c 0a 20 2f 2a 20 20 20 38 32 30    248,. /*   820
201f5 20 2a 2f 20 20 20 31 37 30 2c 20 20 32 34 36 2c   */   170,  246,
201f6 20 20 34 33 37 2c 20 20 34 31 33 2c 20 20 33 38    437,  413,  38
201f7 34 2c 20 20 33 36 35 2c 20 20 31 37 36 2c 20 20  4,  365,  176,  
201f8 31 37 37 2c 20 20 31 37 38 2c 20 20 34 36 37 2c  177,  178,  467,
201f9 0a 20 2f 2a 20 20 20 38 33 30 20 2a 2f 20 20 20  . /*   830 */   
201fa 33 30 39 2c 20 20 31 32 31 2c 20 20 31 35 34 2c  309,  121,  154,
201fb 20 20 31 32 36 2c 20 20 32 39 35 2c 20 20 34 33    126,  295,  43
201fc 32 2c 20 20 34 33 33 2c 20 20 34 32 39 2c 20 20  2,  433,  429,  
201fd 34 32 39 2c 20 20 20 36 32 2c 0a 20 2f 2a 20 20  429,   62,. /*  
201fe 20 38 34 30 20 2a 2f 20 20 20 20 36 32 2c 20 20   840 */    62,  
201ff 20 36 31 2c 20 20 20 36 31 2c 20 20 20 36 31 2c   61,   61,   61,
20200 20 20 20 36 31 2c 20 20 33 31 35 2c 20 20 20 36     61,  315,   6
20201 33 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20  3,   63,   63,  
20202 20 36 33 2c 0a 20 2f 2a 20 20 20 38 35 30 20 2a   63,. /*   850 *
20203 2f 20 20 20 20 36 34 2c 20 20 20 36 34 2c 20 20  /    64,   64,  
20204 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36 35 2c   65,   65,   65,
20205 20 20 20 36 36 2c 20 20 32 33 31 2c 20 20 20 37     66,  231,   7
20206 32 2c 20 20 33 32 32 2c 20 20 31 37 35 2c 0a 20  2,  322,  175,. 
20207 2f 2a 20 20 20 38 36 30 20 2a 2f 20 20 20 20 20  /*   860 */     
20208 34 2c 20 20 33 31 35 2c 20 20 32 36 31 2c 20 20  4,  315,  261,  
20209 33 31 35 2c 20 20 32 39 34 2c 20 20 32 36 31 2c  315,  294,  261,
2020a 20 20 34 31 33 2c 20 20 34 31 32 2c 20 20 20 32    413,  412,   2
2020b 38 2c 20 20 33 31 35 2c 0a 20 2f 2a 20 20 20 38  8,  315,. /*   8
2020c 37 30 20 2a 2f 20 20 20 32 36 31 2c 20 20 33 31  70 */   261,  31
2020d 35 2c 20 20 33 31 39 2c 20 20 20 37 32 2c 20 20  5,  319,   72,  
2020e 33 32 32 2c 20 20 33 31 35 2c 20 20 20 20 34 2c  322,  315,    4,
2020f 20 20 34 31 38 2c 20 20 34 34 33 2c 20 20 34 34    418,  443,  44
20210 33 2c 0a 20 2f 2a 20 20 20 38 38 30 20 2a 2f 20  3,. /*   880 */ 
20211 20 20 32 39 34 2c 20 20 33 39 35 2c 20 20 33 39    294,  395,  39
20212 36 2c 20 20 34 31 32 2c 20 20 20 32 33 2c 20 20  6,  412,   23,  
20213 34 31 32 2c 20 20 20 33 32 2c 20 20 34 33 37 2c  412,   32,  437,
20214 20 20 33 31 39 2c 20 20 33 32 34 2c 0a 20 2f 2a    319,  324,. /*
20215 20 20 20 38 39 30 20 2a 2f 20 20 20 33 32 37 2c     890 */   327,
20216 20 20 34 31 32 2c 20 20 20 35 33 2c 20 20 34 31    412,   53,  41
20217 32 2c 20 20 20 35 32 2c 20 20 33 31 35 2c 20 20  2,   52,  315,  
20218 31 35 36 2c 20 20 34 31 32 2c 20 20 20 39 37 2c  156,  412,   97,
20219 20 20 34 34 39 2c 0a 20 2f 2a 20 20 20 39 30 30    449,. /*   900
2021a 20 2a 2f 20 20 20 33 31 35 2c 20 20 31 39 32 2c   */   315,  192,
2021b 20 20 33 31 35 2c 20 20 32 37 35 2c 20 20 33 31    315,  275,  31
2021c 35 2c 20 20 33 32 34 2c 20 20 33 37 36 2c 20 20  5,  324,  376,  
2021d 34 36 39 2c 20 20 35 30 30 2c 20 20 33 31 35 2c  469,  500,  315,
2021e 0a 20 2f 2a 20 20 20 39 31 30 20 2a 2f 20 20 20  . /*   910 */   
2021f 34 37 36 2c 20 20 32 37 37 2c 20 20 34 37 36 2c  476,  277,  476,
20220 20 20 31 36 33 2c 20 20 32 39 32 2c 20 20 34 34    163,  292,  44
20221 39 2c 20 20 33 31 35 2c 20 20 34 31 32 2c 20 20  9,  315,  412,  
20222 20 39 35 2c 20 20 20 37 34 2c 0a 20 2f 2a 20 20   95,   74,. /*  
20223 20 39 32 30 20 2a 2f 20 20 20 20 37 33 2c 20 20   920 */    73,  
20224 34 36 37 2c 20 20 34 31 32 2c 20 20 31 30 30 2c  467,  412,  100,
20225 20 20 34 31 32 2c 20 20 31 30 31 2c 20 20 34 31    412,  101,  41
20226 32 2c 20 20 31 31 31 2c 20 20 20 37 32 2c 20 20  2,  111,   72,  
20227 33 31 33 2c 0a 20 2f 2a 20 20 20 39 33 30 20 2a  313,. /*   930 *
20228 2f 20 20 20 33 31 34 2c 20 20 34 31 32 2c 20 20  /   314,  412,  
20229 31 31 33 2c 20 20 34 31 37 2c 20 20 34 34 36 2c  113,  417,  446,
2022a 20 20 20 37 34 2c 20 20 20 37 33 2c 20 20 34 37     74,   73,  47
2022b 39 2c 20 20 34 31 32 2c 20 20 20 31 36 2c 0a 20  9,  412,   16,. 
2022c 2f 2a 20 20 20 39 34 30 20 2a 2f 20 20 20 33 37  /*   940 */   37
2022d 39 2c 20 20 33 31 35 2c 20 20 31 38 31 2c 20 20  9,  315,  181,  
2022e 34 36 35 2c 20 20 20 37 32 2c 20 20 33 31 33 2c  465,   72,  313,
2022f 20 20 33 31 34 2c 20 20 20 37 32 2c 20 20 33 32    314,   72,  32
20230 32 2c 20 20 34 31 37 2c 0a 20 2f 2a 20 20 20 39  2,  417,. /*   9
20231 35 30 20 2a 2f 20 20 20 20 20 34 2c 20 20 32 30  50 */     4,  20
20232 36 2c 20 20 33 31 35 2c 20 20 31 38 34 2c 20 20  6,  315,  184,  
20233 32 39 34 2c 20 20 33 31 35 2c 20 20 34 39 37 2c  294,  315,  497,
20234 20 20 34 39 38 2c 20 20 34 37 34 2c 20 20 32 30    498,  474,  20
20235 36 2c 0a 20 2f 2a 20 20 20 39 36 30 20 2a 2f 20  6,. /*   960 */ 
20236 20 20 31 37 31 2c 20 20 33 33 39 2c 20 20 33 31    171,  339,  31
20237 39 2c 20 20 34 31 32 2c 20 20 20 39 38 2c 20 20  9,  412,   98,  
20238 34 31 39 2c 20 20 34 31 39 2c 20 20 34 31 39 2c  419,  419,  419,
20239 20 20 34 32 30 2c 20 20 34 32 31 2c 0a 20 2f 2a    420,  421,. /*
2023a 20 20 20 39 37 30 20 2a 2f 20 20 20 20 31 31 2c     970 */    11,
2023b 20 20 33 35 39 2c 20 20 33 37 38 2c 20 20 33 30    359,  378,  30
2023c 35 2c 20 20 34 31 32 2c 20 20 20 33 33 2c 20 20  5,  412,   33,  
2023d 34 31 33 2c 20 20 34 31 32 2c 20 20 20 39 36 2c  413,  412,   96,
2023e 20 20 33 32 34 2c 0a 20 2f 2a 20 20 20 39 38 30    324,. /*   980
2023f 20 2a 2f 20 20 20 34 35 38 2c 20 20 34 31 39 2c   */   458,  419,
20240 20 20 34 31 39 2c 20 20 34 31 39 2c 20 20 34 32    419,  419,  42
20241 30 2c 20 20 34 32 31 2c 20 20 20 31 31 2c 20 20  0,  421,   11,  
20242 34 31 33 2c 20 20 34 31 31 2c 20 20 34 34 39 2c  413,  411,  449,
20243 0a 20 2f 2a 20 20 20 39 39 30 20 2a 2f 20 20 20  . /*   990 */   
20244 34 31 31 2c 20 20 31 36 30 2c 20 20 34 31 30 2c  411,  160,  410,
20245 20 20 33 31 35 2c 20 20 34 31 30 2c 20 20 34 36    315,  410,  46
20246 36 2c 20 20 32 32 31 2c 20 20 32 32 32 2c 20 20  6,  221,  222,  
20247 32 32 33 2c 20 20 31 30 33 2c 0a 20 2f 2a 20 20  223,  103,. /*  
20248 31 30 30 30 20 2a 2f 20 20 20 20 38 33 2c 20 20  1000 */    83,  
20249 34 37 31 2c 20 20 33 31 35 2c 20 20 35 30 37 2c  471,  315,  507,
2024a 20 20 35 30 36 2c 20 20 33 31 35 2c 20 20 36 32    506,  315,  62
2024b 30 2c 20 20 34 37 35 2c 20 20 33 31 35 2c 20 20  0,  475,  315,  
2024c 20 37 34 2c 0a 20 2f 2a 20 20 31 30 31 30 20 2a   74,. /*  1010 *
2024d 2f 20 20 20 20 37 33 2c 20 20 32 34 35 2c 20 20  /    73,  245,  
2024e 32 30 33 2c 20 20 20 32 31 2c 20 20 32 37 39 2c  203,   21,  279,
2024f 20 20 34 31 32 2c 20 20 20 32 34 2c 20 20 34 33    412,   24,  43
20250 37 2c 20 20 20 37 32 2c 20 20 33 31 33 2c 0a 20  7,   72,  313,. 
20251 2f 2a 20 20 31 30 32 30 20 2a 2f 20 20 20 33 31  /*  1020 */   31
20252 34 2c 20 20 32 38 30 2c 20 20 33 31 35 2c 20 20  4,  280,  315,  
20253 34 31 37 2c 20 20 34 31 32 2c 20 20 20 35 34 2c  417,  412,   54,
20254 20 20 35 30 35 2c 20 20 34 31 32 2c 20 20 31 31    505,  412,  11
20255 34 2c 20 20 33 31 35 2c 0a 20 2f 2a 20 20 31 30  4,  315,. /*  10
20256 33 30 20 2a 2f 20 20 20 34 31 32 2c 20 20 31 31  30 */   412,  11
20257 35 2c 20 20 35 30 34 2c 20 20 32 30 31 2c 20 20  5,  504,  201,  
20258 31 34 35 2c 20 20 35 34 37 2c 20 20 32 34 30 2c  145,  547,  240,
20259 20 20 35 31 30 2c 20 20 35 32 34 2c 20 20 32 30    510,  524,  20
2025a 30 2c 0a 20 2f 2a 20 20 31 30 34 30 20 2a 2f 20  0,. /*  1040 */ 
2025b 20 20 33 31 35 2c 20 20 35 31 31 2c 20 20 32 30    315,  511,  20
2025c 32 2c 20 20 33 31 35 2c 20 20 34 31 32 2c 20 20  2,  315,  412,  
2025d 20 32 35 2c 20 20 33 31 35 2c 20 20 32 34 31 2c   25,  315,  241,
2025e 20 20 33 31 35 2c 20 20 20 31 38 2c 0a 20 2f 2a    315,   18,. /*
2025f 20 20 31 30 35 30 20 2a 2f 20 20 20 33 31 35 2c    1050 */   315,
20260 20 20 34 31 32 2c 20 20 20 33 36 2c 20 20 33 31    412,   36,  31
20261 35 2c 20 20 32 35 34 2c 20 20 34 31 39 2c 20 20  5,  254,  419,  
20262 34 31 39 2c 20 20 34 31 39 2c 20 20 34 32 30 2c  419,  419,  420,
20263 20 20 34 32 31 2c 0a 20 2f 2a 20 20 31 30 36 30    421,. /*  1060
20264 20 2a 2f 20 20 20 20 31 31 2c 20 20 32 35 36 2c   */    11,  256,
20265 20 20 34 31 32 2c 20 20 20 33 37 2c 20 20 32 35    412,   37,  25
20266 38 2c 20 20 34 31 32 2c 20 20 20 32 36 2c 20 20  8,  412,   26,  
20267 33 31 35 2c 20 20 34 31 32 2c 20 20 20 33 38 2c  315,  412,   38,
20268 0a 20 2f 2a 20 20 31 30 37 30 20 2a 2f 20 20 20  . /*  1070 */   
20269 34 31 32 2c 20 20 20 33 39 2c 20 20 34 31 32 2c  412,   39,  412,
2026a 20 20 20 34 30 2c 20 20 32 36 30 2c 20 20 34 31     40,  260,  41
2026b 32 2c 20 20 20 34 32 2c 20 20 33 31 35 2c 20 20  2,   42,  315,  
2026c 35 31 32 2c 20 20 33 31 35 2c 0a 20 2f 2a 20 20  512,  315,. /*  
2026d 31 30 38 30 20 2a 2f 20 20 20 31 32 36 2c 20 20  1080 */   126,  
2026e 33 31 35 2c 20 20 34 33 37 2c 20 20 33 31 35 2c  315,  437,  315,
2026f 20 20 31 38 37 2c 20 20 33 37 35 2c 20 20 32 37    187,  375,  27
20270 36 2c 20 20 32 36 36 2c 20 20 32 36 35 2c 20 20  6,  266,  265,  
20271 34 31 32 2c 0a 20 2f 2a 20 20 31 30 39 30 20 2a  412,. /*  1090 *
20272 2f 20 20 20 20 34 33 2c 20 20 32 39 31 2c 20 20  /    43,  291,  
20273 33 31 35 2c 20 20 32 35 32 2c 20 20 33 31 35 2c  315,  252,  315,
20274 20 20 31 32 36 2c 20 20 32 30 36 2c 20 20 35 38    126,  206,  58
20275 31 2c 20 20 20 20 38 2c 20 20 34 31 32 2c 0a 20  1,    8,  412,. 
20276 2f 2a 20 20 31 31 30 30 20 2a 2f 20 20 20 20 34  /*  1100 */    4
20277 34 2c 20 20 34 31 32 2c 20 20 20 32 39 2c 20 20  4,  412,   29,  
20278 34 31 32 2c 20 20 20 33 30 2c 20 20 34 31 32 2c  412,   30,  412,
20279 20 20 20 34 35 2c 20 20 33 35 30 2c 20 20 33 36     45,  350,  36
2027a 33 2c 20 20 31 32 36 2c 0a 20 2f 2a 20 20 31 31  3,  126,. /*  11
2027b 31 30 20 2a 2f 20 20 20 33 31 35 2c 20 20 35 34  10 */   315,  54
2027c 33 2c 20 20 33 31 35 2c 20 20 31 32 36 2c 20 20  3,  315,  126,  
2027d 34 31 32 2c 20 20 20 34 36 2c 20 20 34 31 32 2c  412,   46,  412,
2027e 20 20 20 34 37 2c 20 20 33 31 35 2c 20 20 33 35     47,  315,  35
2027f 35 2c 0a 20 2f 2a 20 20 31 31 32 30 20 2a 2f 20  5,. /*  1120 */ 
20280 20 20 33 38 31 2c 20 20 35 35 31 2c 20 20 33 31    381,  551,  31
20281 35 2c 20 20 31 37 31 2c 20 20 35 35 32 2c 20 20  5,  171,  552,  
20282 33 31 35 2c 20 20 20 39 30 2c 20 20 35 36 32 2c  315,   90,  562,
20283 20 20 35 37 38 2c 20 20 20 39 30 2c 0a 20 2f 2a    578,   90,. /*
20284 20 20 31 31 33 30 20 2a 2f 20 20 20 32 38 38 2c    1130 */   288,
20285 20 20 33 36 36 2c 20 20 34 31 32 2c 20 20 20 34    366,  412,   4
20286 38 2c 20 20 34 31 32 2c 20 20 20 33 31 2c 20 20  8,  412,   31,  
20287 35 38 32 2c 20 20 33 36 37 2c 20 20 32 36 38 2c  582,  367,  268,
20288 20 20 32 36 39 2c 0a 20 2f 2a 20 20 31 31 34 30    269,. /*  1140
20289 20 2a 2f 20 20 20 34 31 32 2c 20 20 20 31 30 2c   */   412,   10,
2028a 20 20 32 37 30 2c 20 20 35 35 34 2c 20 20 34 31    270,  554,  41
2028b 32 2c 20 20 20 35 30 2c 20 20 35 36 34 2c 20 20  2,   50,  564,  
2028c 34 31 32 2c 20 20 20 35 31 2c 20 20 32 37 38 2c  412,   51,  278,
2028d 0a 20 2f 2a 20 20 31 31 35 30 20 2a 2f 20 20 20  . /*  1150 */   
2028e 32 38 31 2c 20 20 32 38 32 2c 20 20 35 37 35 2c  281,  282,  575,
2028f 20 20 31 34 34 2c 20 20 34 34 32 2c 20 20 34 30    144,  442,  40
20290 33 2c 20 20 33 32 33 2c 20 20 32 32 36 2c 20 20  3,  323,  226,  
20291 34 34 34 2c 20 20 34 36 31 2c 0a 20 2f 2a 20 20  444,  461,. /*  
20292 31 31 36 30 20 2a 2f 20 20 20 34 36 34 2c 20 20  1160 */   464,  
20293 32 34 32 2c 20 20 35 30 33 2c 20 20 35 35 30 2c  242,  503,  550,
20294 20 20 35 36 31 2c 20 20 35 31 33 2c 20 20 31 36    561,  513,  16
20295 31 2c 20 20 33 39 32 2c 20 20 34 30 30 2c 20 20  1,  392,  400,  
20296 35 31 36 2c 0a 20 2f 2a 20 20 31 31 37 30 20 2a  516,. /*  1170 *
20297 2f 20 20 20 34 30 31 2c 20 20 33 34 35 2c 20 20  /   401,  345,  
20298 34 30 32 2c 20 20 20 20 37 2c 20 20 33 31 32 2c  402,    7,  312,
20299 20 20 20 38 33 2c 20 20 32 32 37 2c 20 20 33 33     83,  227,  33
2029a 32 2c 20 20 32 32 38 2c 20 20 20 38 32 2c 0a 20  2,  228,   82,. 
2029b 2f 2a 20 20 31 31 38 30 20 2a 2f 20 20 20 33 33  /*  1180 */   33
2029c 30 2c 20 20 20 35 37 2c 20 20 34 30 38 2c 20 20  0,   57,  408,  
2029d 34 31 36 2c 20 20 31 36 38 2c 20 20 20 37 38 2c  416,  168,   78,
2029e 20 20 34 35 39 2c 20 20 31 32 33 2c 20 20 32 31    459,  123,  21
2029f 30 2c 20 20 34 31 34 2c 0a 20 2f 2a 20 20 31 31  0,  414,. /*  11
202a0 39 30 20 2a 2f 20 20 20 20 38 34 2c 20 20 33 33  90 */    84,  33
202a1 35 2c 20 20 33 34 30 2c 20 20 32 39 39 2c 20 20  5,  340,  299,  
202a2 34 39 36 2c 20 20 35 30 30 2c 20 20 34 39 30 2c  496,  500,  490,
202a3 20 20 32 32 39 2c 20 20 34 39 35 2c 20 20 32 34    229,  495,  24
202a4 33 2c 0a 20 2f 2a 20 20 31 32 30 30 20 2a 2f 20  3,. /*  1200 */ 
202a5 20 20 31 30 34 2c 20 20 32 34 37 2c 20 20 34 39    104,  247,  49
202a6 39 2c 20 20 35 30 31 2c 20 20 32 33 30 2c 20 20  9,  501,  230,  
202a7 32 38 35 2c 20 20 34 31 35 2c 20 20 32 31 35 2c  285,  415,  215,
202a8 20 20 35 31 34 2c 20 20 35 31 38 2c 0a 20 2f 2a    514,  518,. /*
202a9 20 20 31 32 31 30 20 2a 2f 20 20 20 35 32 35 2c    1210 */   525,
202aa 20 20 35 32 36 2c 20 20 35 31 39 2c 20 20 32 33    526,  519,  23
202ab 36 2c 20 20 35 32 37 2c 20 20 34 37 33 2c 20 20  6,  527,  473,  
202ac 32 33 38 2c 20 20 33 35 32 2c 20 20 34 37 37 2c  238,  352,  477,
202ad 20 20 32 38 36 2c 0a 20 2f 2a 20 20 31 32 32 30    286,. /*  1220
202ae 20 2a 2f 20 20 20 31 38 32 2c 20 20 33 35 36 2c   */   182,  356,
202af 20 20 35 33 33 2c 20 20 33 35 34 2c 20 20 31 31    533,  354,  11
202b0 39 2c 20 20 31 38 33 2c 20 20 31 38 35 2c 20 20  9,  183,  185,  
202b1 20 38 37 2c 20 20 35 34 36 2c 20 20 31 33 30 2c   87,  546,  130,
202b2 0a 20 2f 2a 20 20 31 32 33 30 20 2a 2f 20 20 20  . /*  1230 */   
202b3 31 38 36 2c 20 20 35 33 35 2c 20 20 31 38 38 2c  186,  535,  188,
202b4 20 20 31 34 30 2c 20 20 33 36 32 2c 20 20 31 39    140,  362,  19
202b5 31 2c 20 20 35 35 33 2c 20 20 32 31 36 2c 20 20  1,  553,  216,  
202b6 33 37 33 2c 20 20 33 37 34 2c 0a 20 2f 2a 20 20  373,  374,. /*  
202b7 31 32 34 30 20 2a 2f 20 20 20 31 33 31 2c 20 20  1240 */   131,  
202b8 35 36 30 2c 20 20 33 30 38 2c 20 20 31 33 32 2c  560,  308,  132,
202b9 20 20 31 33 33 2c 20 20 35 37 32 2c 20 20 35 37    133,  572,  57
202ba 37 2c 20 20 31 33 36 2c 20 20 32 36 33 2c 20 20  7,  136,  263,  
202bb 31 33 34 2c 0a 20 2f 2a 20 20 31 32 35 30 20 2a  134,. /*  1250 *
202bc 2f 20 20 20 31 33 39 2c 20 20 35 33 36 2c 20 20  /   139,  536,  
202bd 35 37 33 2c 20 20 33 39 31 2c 20 20 20 39 31 2c  573,  391,   91,
202be 20 20 20 39 34 2c 20 20 34 30 34 2c 20 20 35 37     94,  404,  57
202bf 34 2c 20 20 20 39 39 2c 20 20 32 31 34 2c 0a 20  4,   99,  214,. 
202c0 2f 2a 20 20 31 32 36 30 20 2a 2f 20 20 20 31 30  /*  1260 */   10
202c1 32 2c 20 20 36 32 31 2c 20 20 36 32 32 2c 20 20  2,  621,  622,  
202c2 34 33 31 2c 20 20 31 36 34 2c 20 20 34 33 34 2c  431,  164,  434,
202c3 20 20 31 36 35 2c 20 20 20 37 31 2c 20 20 31 34    165,   71,  14
202c4 31 2c 20 20 20 31 37 2c 0a 20 2f 2a 20 20 31 32  1,   17,. /*  12
202c5 37 30 20 2a 2f 20 20 20 34 33 39 2c 20 20 34 34  70 */   439,  44
202c6 38 2c 20 20 34 35 31 2c 20 20 31 35 35 2c 20 20  8,  451,  155,  
202c7 20 20 36 2c 20 20 34 35 32 2c 20 20 34 37 30 2c    6,  452,  470,
202c8 20 20 31 31 30 2c 20 20 31 36 37 2c 20 20 34 35    110,  167,  45
202c9 33 2c 0a 20 2f 2a 20 20 31 32 38 30 20 2a 2f 20  3,. /*  1280 */ 
202ca 20 20 34 35 37 2c 20 20 31 32 34 2c 20 20 20 31    457,  124,   1
202cb 33 2c 20 20 32 31 33 2c 20 20 31 32 30 2c 20 20  3,  213,  120,  
202cc 20 38 30 2c 20 20 20 31 32 2c 20 20 31 32 35 2c   80,   12,  125,
202cd 20 20 31 35 39 2c 20 20 34 38 33 2c 0a 20 2f 2a    159,  483,. /*
202ce 20 20 31 32 39 30 20 2a 2f 20 20 20 34 38 34 2c    1290 */   484,
202cf 20 20 20 38 35 2c 20 20 33 31 30 2c 20 20 31 30     85,  310,  10
202d0 35 2c 20 20 31 38 30 2c 20 20 32 35 31 2c 20 20  5,  180,  251,  
202d1 31 30 36 2c 20 20 31 31 38 2c 20 20 20 38 36 2c  106,  118,   86,
202d2 20 20 31 30 37 2c 0a 20 2f 2a 20 20 31 33 30 30    107,. /*  1300
202d3 20 2a 2f 20 20 20 32 34 31 2c 20 20 33 34 34 2c   */   241,  344,
202d4 20 20 33 34 39 2c 20 20 33 35 33 2c 20 20 31 34    349,  353,  14
202d5 33 2c 20 20 35 33 34 2c 20 20 31 32 37 2c 20 20  3,  534,  127,  
202d6 33 35 37 2c 20 20 31 37 31 2c 20 20 31 38 39 2c  357,  171,  189,
202d7 0a 20 2f 2a 20 20 31 33 31 30 20 2a 2f 20 20 20  . /*  1310 */   
202d8 32 36 34 2c 20 20 31 30 38 2c 20 20 32 38 37 2c  264,  108,  287,
202d9 20 20 31 32 38 2c 20 20 35 34 39 2c 20 20 31 32    128,  549,  12
202da 39 2c 20 20 31 39 33 2c 20 20 35 33 37 2c 20 20  9,  193,  537,  
202db 35 34 31 2c 20 20 20 31 34 2c 0a 20 2f 2a 20 20  541,   14,. /*  
202dc 31 33 32 30 20 2a 2f 20 20 20 31 39 35 2c 20 20  1320 */   195,  
202dd 20 38 38 2c 20 20 31 39 36 2c 20 20 35 35 36 2c   88,  196,  556,
202de 20 20 31 39 37 2c 20 20 31 33 37 2c 20 20 31 33    197,  137,  13
202df 38 2c 20 20 31 33 35 2c 20 20 20 31 35 2c 20 20  8,  135,   15,  
202e0 35 36 33 2c 0a 20 2f 2a 20 20 31 33 33 30 20 2a  563,. /*  1330 *
202e1 2f 20 20 20 35 37 30 2c 20 20 31 30 39 2c 20 20  /   570,  109,  
202e2 32 38 33 2c 20 20 31 34 36 2c 20 20 32 30 34 2c  283,  146,  204,
202e3 20 20 33 38 33 2c 20 20 33 38 37 2c 20 20 38 39    383,  387,  89
202e4 39 2c 20 20 35 38 34 2c 20 20 38 39 39 2c 0a 20  9,  584,  899,. 
202e5 2f 2a 20 20 31 33 34 30 20 2a 2f 20 20 20 38 39  /*  1340 */   89
202e6 39 2c 20 20 38 39 39 2c 20 20 38 39 39 2c 20 20  9,  899,  899,  
202e7 38 39 39 2c 20 20 20 38 39 2c 0a 7d 3b 0a 73 74  899,   89,.};.st
202e8 61 74 69 63 20 63 6f 6e 73 74 20 59 59 43 4f 44  atic const YYCOD
202e9 45 54 59 50 45 20 79 79 5f 6c 6f 6f 6b 61 68 65  ETYPE yy_lookahe
202ea 61 64 5b 5d 20 3d 20 7b 0a 20 2f 2a 20 20 20 20  ad[] = {. /*    
202eb 20 30 20 2a 2f 20 20 20 20 31 36 2c 20 20 31 33   0 */    16,  13
202ec 39 2c 20 20 31 34 30 2c 20 20 31 34 31 2c 20 20  9,  140,  141,  
202ed 31 36 38 2c 20 20 20 32 31 2c 20 20 31 34 34 2c  168,   21,  144,
202ee 20 20 20 32 33 2c 20 20 20 36 39 2c 20 20 20 37     23,   69,   7
202ef 30 2c 0a 20 2f 2a 20 20 20 20 31 30 20 2a 2f 20  0,. /*    10 */ 
202f0 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20 31 37     71,   72,  17
202f1 36 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20  6,   74,   75,  
202f2 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c   76,   77,   78,
202f3 20 20 20 37 39 2c 20 20 20 38 30 2c 0a 20 2f 2a     79,   80,. /*
202f4 20 20 20 20 32 30 20 2a 2f 20 20 20 20 38 31 2c      20 */    81,
202f5 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38     82,   83,   8
202f6 34 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20  4,   78,   79,  
202f7 20 34 32 2c 20 20 20 34 33 2c 20 20 20 37 33 2c   42,   43,   73,
202f8 20 20 20 37 34 2c 0a 20 2f 2a 20 20 20 20 33 30     74,. /*    30
202f9 20 2a 2f 20 20 20 20 37 35 2c 20 20 20 37 36 2c   */    75,   76,
202fa 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37     77,   78,   7
202fb 39 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 20 20  9,   80,   81,  
202fc 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c   82,   83,   84,
202fd 0a 20 2f 2a 20 20 20 20 34 30 20 2a 2f 20 20 20  . /*    40 */   
202fe 20 20 31 2c 20 20 20 20 32 2c 20 20 20 32 33 2c    1,    2,   23,
202ff 20 20 20 35 38 2c 20 20 20 36 30 2c 20 20 20 36     58,   60,   6
20300 31 2c 20 20 20 36 32 2c 20 20 20 36 33 2c 20 20  1,   62,   63,  
20301 20 36 34 2c 20 20 20 36 35 2c 0a 20 2f 2a 20 20   64,   65,. /*  
20302 20 20 35 30 20 2a 2f 20 20 20 20 36 36 2c 20 20    50 */    66,  
20303 20 36 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c   67,   68,   69,
20304 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37     70,   71,   7
20305 32 2c 20 20 31 34 37 2c 20 20 20 37 34 2c 20 20  2,  147,   74,  
20306 20 37 35 2c 0a 20 2f 2a 20 20 20 20 36 30 20 2a   75,. /*    60 *
20307 2f 20 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20  /    76,   77,  
20308 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c   78,   79,   80,
20309 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38     81,   82,   8
2030a 33 2c 20 20 20 38 34 2c 20 20 20 31 36 2c 0a 20  3,   84,   16,. 
2030b 2f 2a 20 20 20 20 37 30 20 2a 2f 20 20 20 31 38  /*    70 */   18
2030c 35 2c 20 20 31 38 36 2c 20 20 20 38 38 2c 20 20  5,  186,   88,  
2030d 20 38 38 2c 20 20 31 31 30 2c 20 20 20 32 32 2c   88,  110,   22,
2030e 20 20 32 31 37 2c 20 20 20 39 32 2c 20 20 32 31    217,   92,  21
2030f 39 2c 20 20 32 32 30 2c 0a 20 2f 2a 20 20 20 20  9,  220,. /*    
20310 38 30 20 2a 2f 20 20 20 20 37 34 2c 20 20 20 37  80 */    74,   7
20311 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20  5,   76,   77,  
20312 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c   78,   79,   80,
20313 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38     81,   82,   8
20314 33 2c 0a 20 2f 2a 20 20 20 20 39 30 20 2a 2f 20  3,. /*    90 */ 
20315 20 20 20 38 34 2c 20 20 32 31 37 2c 20 20 32 31     84,  217,  21
20316 38 2c 20 20 32 31 39 2c 20 20 32 32 30 2c 20 20  8,  219,  220,  
20317 20 34 32 2c 20 20 20 34 33 2c 20 20 32 33 38 2c   42,   43,  238,
20318 20 20 31 38 38 2c 20 20 20 34 36 2c 0a 20 2f 2a    188,   46,. /*
20319 20 20 20 31 30 30 20 2a 2f 20 20 20 20 37 38 2c     100 */    78,
2031a 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20 20 38     79,   80,   8
2031b 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20  1,   82,   83,  
2031c 20 38 34 2c 20 20 20 38 38 2c 20 20 20 38 39 2c   84,   88,   89,
2031d 20 20 31 32 34 2c 0a 20 2f 2a 20 20 20 31 31 30    124,. /*   110
2031e 20 2a 2f 20 20 20 31 32 35 2c 20 20 31 32 36 2c   */   125,  126,
2031f 20 20 20 31 36 2c 20 20 20 36 30 2c 20 20 20 36     16,   60,   6
20320 31 2c 20 20 20 36 32 2c 20 20 20 36 33 2c 20 20  1,   62,   63,  
20321 20 36 34 2c 20 20 20 36 35 2c 20 20 20 36 36 2c   64,   65,   66,
20322 0a 20 2f 2a 20 20 20 31 32 30 20 2a 2f 20 20 20  . /*   120 */   
20323 20 36 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c   67,   68,   69,
20324 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37     70,   71,   7
20325 32 2c 20 20 31 34 37 2c 20 20 20 37 34 2c 20 20  2,  147,   74,  
20326 20 37 35 2c 20 20 20 37 36 2c 0a 20 2f 2a 20 20   75,   76,. /*  
20327 20 31 33 30 20 2a 2f 20 20 20 20 37 37 2c 20 20   130 */    77,  
20328 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c   78,   79,   80,
20329 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38     81,   82,   8
2032a 33 2c 20 20 20 38 34 2c 20 20 20 34 32 2c 20 20  3,   84,   42,  
2032b 20 34 33 2c 0a 20 2f 2a 20 20 20 31 34 30 20 2a   43,. /*   140 *
2032c 2f 20 20 20 20 34 34 2c 20 20 20 38 30 2c 20 20  /    44,   80,  
2032d 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c   81,   82,   83,
2032e 20 20 20 38 34 2c 20 20 20 32 33 2c 20 20 20 38     84,   23,   8
2032f 34 2c 20 20 31 36 39 2c 20 20 31 37 30 2c 0a 20  4,  169,  170,. 
20330 2f 2a 20 20 20 31 35 30 20 2a 2f 20 20 20 20 31  /*   150 */    1
20331 39 2c 20 20 31 36 34 2c 20 20 31 36 35 2c 20 20  9,  164,  165,  
20332 31 36 36 2c 20 20 20 32 33 2c 20 20 31 36 39 2c  166,   23,  169,
20333 20 20 20 36 30 2c 20 20 20 36 31 2c 20 20 20 36     60,   61,   6
20334 32 2c 20 20 20 36 33 2c 0a 20 2f 2a 20 20 20 31  2,   63,. /*   1
20335 36 30 20 2a 2f 20 20 20 20 36 34 2c 20 20 20 36  60 */    64,   6
20336 35 2c 20 20 20 36 36 2c 20 20 20 36 37 2c 20 20  5,   66,   67,  
20337 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c   68,   69,   70,
20338 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20 31 36     71,   72,  16
20339 39 2c 0a 20 2f 2a 20 20 20 31 37 30 20 2a 2f 20  9,. /*   170 */ 
2033a 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37     74,   75,   7
2033b 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20  6,   77,   78,  
2033c 20 37 39 2c 20 20 20 38 30 2c 20 20 20 38 31 2c   79,   80,   81,
2033d 20 20 20 38 32 2c 20 20 20 38 33 2c 0a 20 2f 2a     82,   83,. /*
2033e 20 20 20 31 38 30 20 2a 2f 20 20 20 20 38 34 2c     180 */    84,
2033f 20 20 20 31 36 2c 20 20 20 31 34 2c 20 20 31 34     16,   14,  14
20340 37 2c 20 20 31 35 30 2c 20 20 31 34 37 2c 20 20  7,  150,  147,  
20341 20 32 31 2c 20 20 31 36 37 2c 20 20 31 36 38 2c   21,  167,  168,
20342 20 20 20 35 38 2c 0a 20 2f 2a 20 20 20 31 39 30     58,. /*   190
20343 20 2a 2f 20 20 20 32 31 31 2c 20 20 31 34 37 2c   */   211,  147,
20344 20 20 31 35 36 2c 20 20 31 35 37 2c 20 20 20 32    156,  157,   2
20345 33 2c 20 20 32 31 36 2c 20 20 31 37 36 2c 20 20  3,  216,  176,  
20346 20 32 33 2c 20 20 31 38 31 2c 20 20 31 37 36 2c   23,  181,  176,
20347 0a 20 2f 2a 20 20 20 32 30 30 20 2a 2f 20 20 20  . /*   200 */   
20348 31 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c  177,   78,   79,
20349 20 20 31 36 35 2c 20 20 31 36 36 2c 20 20 31 31    165,  166,  11
2034a 30 2c 20 20 31 38 33 2c 20 20 20 34 32 2c 20 20  0,  183,   42,  
2034b 20 34 33 2c 20 20 20 37 38 2c 0a 20 2f 2a 20 20   43,   78,. /*  
2034c 20 32 31 30 20 2a 2f 20 20 20 20 37 39 2c 20 20   210 */    79,  
2034d 20 38 38 2c 20 20 20 38 39 2c 20 20 31 36 39 2c   88,   89,  169,
2034e 20 20 31 37 30 2c 20 20 32 32 38 2c 20 20 31 38    170,  228,  18
2034f 30 2c 20 20 31 38 31 2c 20 20 31 32 33 2c 20 20  0,  181,  123,  
20350 20 38 38 2c 0a 20 2f 2a 20 20 20 32 32 30 20 2a   88,. /*   220 *
20351 2f 20 20 20 20 35 32 2c 20 20 20 39 38 2c 20 20  /    52,   98,  
20352 20 35 34 2c 20 20 20 39 32 2c 20 20 20 31 36 2c   54,   92,   16,
20353 20 20 20 36 30 2c 20 20 20 36 31 2c 20 20 20 36     60,   61,   6
20354 32 2c 20 20 20 36 33 2c 20 20 20 36 34 2c 0a 20  2,   63,   64,. 
20355 2f 2a 20 20 20 32 33 30 20 2a 2f 20 20 20 20 36  /*   230 */    6
20356 35 2c 20 20 20 36 36 2c 20 20 20 36 37 2c 20 20  5,   66,   67,  
20357 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c   68,   69,   70,
20358 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20 31 34     71,   72,  14
20359 37 2c 20 20 20 37 34 2c 0a 20 2f 2a 20 20 20 32  7,   74,. /*   2
2035a 34 30 20 2a 2f 20 20 20 20 37 35 2c 20 20 20 37  40 */    75,   7
2035b 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20  6,   77,   78,  
2035c 20 37 39 2c 20 20 20 38 30 2c 20 20 20 38 31 2c   79,   80,   81,
2035d 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38     82,   83,   8
2035e 34 2c 0a 20 2f 2a 20 20 20 32 35 30 20 2a 2f 20  4,. /*   250 */ 
2035f 20 20 20 34 32 2c 20 20 20 34 33 2c 20 20 20 37     42,   43,   7
20360 38 2c 20 20 32 30 39 2c 20 20 32 31 30 2c 20 20  8,  209,  210,  
20361 31 32 34 2c 20 20 31 32 35 2c 20 20 31 32 36 2c  124,  125,  126,
20362 20 20 32 32 34 2c 20 20 20 38 38 2c 0a 20 2f 2a    224,   88,. /*
20363 20 20 20 32 36 30 20 2a 2f 20 20 20 31 36 39 2c     260 */   169,
20364 20 20 31 37 30 2c 20 20 20 38 38 2c 20 20 20 38    170,   88,   8
20365 39 2c 20 20 32 33 30 2c 20 20 32 32 37 2c 20 20  9,  230,  227,  
20366 32 32 38 2c 20 20 20 31 36 2c 20 20 20 36 30 2c  228,   16,   60,
20367 20 20 20 36 31 2c 0a 20 2f 2a 20 20 20 32 37 30     61,. /*   270
20368 20 2a 2f 20 20 20 20 36 32 2c 20 20 20 36 33 2c   */    62,   63,
20369 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20 20 36     64,   65,   6
2036a 36 2c 20 20 20 36 37 2c 20 20 20 36 38 2c 20 20  6,   67,   68,  
2036b 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c   69,   70,   71,
2036c 0a 20 2f 2a 20 20 20 32 38 30 20 2a 2f 20 20 20  . /*   280 */   
2036d 20 37 32 2c 20 20 20 32 33 2c 20 20 20 37 34 2c   72,   23,   74,
2036e 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37     75,   76,   7
2036f 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20  7,   78,   79,  
20370 20 38 30 2c 20 20 20 38 31 2c 0a 20 2f 2a 20 20   80,   81,. /*  
20371 20 32 39 30 20 2a 2f 20 20 20 20 38 32 2c 20 20   290 */    82,  
20372 20 38 33 2c 20 20 20 38 34 2c 20 20 20 34 32 2c   83,   84,   42,
20373 20 20 20 34 33 2c 20 20 31 36 30 2c 20 20 20 31     43,  160,   1
20374 36 2c 20 20 31 34 37 2c 20 20 31 36 31 2c 20 20  6,  147,  161,  
20375 20 38 33 2c 0a 20 2f 2a 20 20 20 33 30 30 20 2a   83,. /*   300 *
20376 2f 20 20 20 20 38 34 2c 20 20 32 31 30 2c 20 20  /    84,  210,  
20377 31 36 31 2c 20 20 31 35 33 2c 20 20 31 36 39 2c  161,  153,  169,
20378 20 20 31 35 38 2c 20 20 31 35 36 2c 20 20 31 35    158,  156,  15
20379 37 2c 20 20 31 36 31 2c 20 20 31 36 32 2c 0a 20  7,  161,  162,. 
2037a 2f 2a 20 20 20 33 31 30 20 2a 2f 20 20 20 31 36  /*   310 */   16
2037b 33 2c 20 20 20 36 30 2c 20 20 20 36 31 2c 20 20  3,   60,   61,  
2037c 20 36 32 2c 20 20 20 36 33 2c 20 20 20 36 34 2c   62,   63,   64,
2037d 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20 20 36     65,   66,   6
2037e 37 2c 20 20 20 36 38 2c 0a 20 2f 2a 20 20 20 33  7,   68,. /*   3
2037f 32 30 20 2a 2f 20 20 20 20 36 39 2c 20 20 20 37  20 */    69,   7
20380 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20  0,   71,   72,  
20381 31 36 31 2c 20 20 20 37 34 2c 20 20 20 37 35 2c  161,   74,   75,
20382 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37     76,   77,   7
20383 38 2c 0a 20 2f 2a 20 20 20 33 33 30 20 2a 2f 20  8,. /*   330 */ 
20384 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20 20 38     79,   80,   8
20385 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20  1,   82,   83,  
20386 20 38 34 2c 20 20 31 39 32 2c 20 20 32 30 30 2c   84,  192,  200,
20387 20 20 31 34 37 2c 20 20 31 33 31 2c 0a 20 2f 2a    147,  131,. /*
20388 20 20 20 33 34 30 20 2a 2f 20 20 20 20 31 36 2c     340 */    16,
20389 20 20 32 30 30 2c 20 20 20 31 36 2c 20 20 31 39    200,   16,  19
2038a 39 2c 20 20 20 32 30 2c 20 20 31 39 30 2c 20 20  9,   20,  190,  
2038b 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c   88,   89,   90,
2038c 20 20 31 38 35 2c 0a 20 2f 2a 20 20 20 33 35 30    185,. /*   350
2038d 20 2a 2f 20 20 20 31 38 36 2c 20 20 20 39 33 2c   */   186,   93,
2038e 20 20 20 39 34 2c 20 20 20 39 35 2c 20 20 32 31     94,   95,  21
2038f 37 2c 20 20 20 32 32 2c 20 20 32 31 39 2c 20 20  7,   22,  219,  
20390 32 32 30 2c 20 20 31 34 37 2c 20 20 31 34 37 2c  220,  147,  147,
20391 0a 20 2f 2a 20 20 20 33 36 30 20 2a 2f 20 20 20  . /*   360 */   
20392 31 36 39 2c 20 20 31 37 30 2c 20 20 31 30 34 2c  169,  170,  104,
20393 20 20 32 30 30 2c 20 20 20 38 34 2c 20 20 31 34    200,   84,  14
20394 37 2c 20 20 20 34 32 2c 20 20 20 34 33 2c 20 20  7,   42,   43,  
20395 31 35 36 2c 20 20 31 35 37 2c 0a 20 2f 2a 20 20  156,  157,. /*  
20396 20 33 37 30 20 2a 2f 20 20 20 20 39 30 2c 20 20   370 */    90,  
20397 20 39 31 2c 20 20 20 39 32 2c 20 20 20 39 33 2c   91,   92,   93,
20398 20 20 20 39 34 2c 20 20 20 39 35 2c 20 20 20 39     94,   95,   9
20399 36 2c 20 20 31 36 34 2c 20 20 31 36 35 2c 20 20  6,  164,  165,  
2039a 31 36 36 2c 0a 20 2f 2a 20 20 20 33 38 30 20 2a  166,. /*   380 *
2039b 2f 20 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20  /   169,  170,  
2039c 31 33 31 2c 20 20 31 30 33 2c 20 20 20 36 30 2c  131,  103,   60,
2039d 20 20 20 36 31 2c 20 20 20 36 32 2c 20 20 20 36     61,   62,   6
2039e 33 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 0a 20  3,   64,   65,. 
2039f 2f 2a 20 20 20 33 39 30 20 2a 2f 20 20 20 20 36  /*   390 */    6
203a0 36 2c 20 20 20 36 37 2c 20 20 20 36 38 2c 20 20  6,   67,   68,  
203a1 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c   69,   70,   71,
203a2 20 20 20 37 32 2c 20 20 31 35 35 2c 20 20 20 37     72,  155,   7
203a3 34 2c 20 20 20 37 35 2c 0a 20 2f 2a 20 20 20 34  4,   75,. /*   4
203a4 30 30 20 2a 2f 20 20 20 20 37 36 2c 20 20 20 37  00 */    76,   7
203a5 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20  7,   78,   79,  
203a6 20 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c   80,   81,   82,
203a7 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 31     83,   84,   1
203a8 36 2c 0a 20 2f 2a 20 20 20 34 31 30 20 2a 2f 20  6,. /*   410 */ 
203a9 20 20 20 38 34 2c 20 20 20 31 31 2c 20 20 32 32     84,   11,  22
203aa 31 2c 20 20 20 32 30 2c 20 20 20 33 30 2c 20 20  1,   20,   30,  
203ab 20 31 36 2c 20 20 31 34 37 2c 20 20 20 39 31 2c   16,  147,   91,
203ac 20 20 20 39 32 2c 20 20 20 39 33 2c 0a 20 2f 2a     92,   93,. /*
203ad 20 20 20 34 32 30 20 2a 2f 20 20 20 20 39 34 2c     420 */    94,
203ae 20 20 20 39 35 2c 20 20 20 39 36 2c 20 20 20 39     95,   96,   9
203af 30 2c 20 20 31 34 37 2c 20 20 31 38 31 2c 20 20  0,  147,  181,  
203b0 20 39 33 2c 20 20 20 39 34 2c 20 20 20 39 35 2c   93,   94,   95,
203b1 20 20 31 30 33 2c 0a 20 2f 2a 20 20 20 34 33 30    103,. /*   430
203b2 20 2a 2f 20 20 20 32 31 32 2c 20 20 31 38 39 2c   */   212,  189,
203b3 20 20 31 35 35 2c 20 20 20 32 37 2c 20 20 20 35    155,   27,   5
203b4 30 2c 20 20 20 34 32 2c 20 20 20 34 33 2c 20 20  0,   42,   43,  
203b5 31 30 34 2c 20 20 31 36 39 2c 20 20 31 37 30 2c  104,  169,  170,
203b6 0a 20 2f 2a 20 20 20 34 34 30 20 2a 2f 20 20 20  . /*   440 */   
203b7 20 33 34 2c 20 20 32 32 38 2c 20 20 20 34 33 2c   34,  228,   43,
203b8 20 20 32 30 31 2c 20 20 32 30 32 2c 20 20 31 34    201,  202,  14
203b9 37 2c 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20  7,  169,  170,  
203ba 32 30 36 2c 20 20 20 34 39 2c 0a 20 2f 2a 20 20  206,   49,. /*  
203bb 20 34 35 30 20 2a 2f 20 20 20 31 36 31 2c 20 20   450 */   161,  
203bc 31 36 32 2c 20 20 31 36 33 2c 20 20 20 36 30 2c  162,  163,   60,
203bd 20 20 20 36 31 2c 20 20 20 36 32 2c 20 20 20 36     61,   62,   6
203be 33 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20  3,   64,   65,  
203bf 20 36 36 2c 0a 20 2f 2a 20 20 20 34 36 30 20 2a   66,. /*   460 *
203c0 2f 20 20 20 20 36 37 2c 20 20 20 36 38 2c 20 20  /    67,   68,  
203c1 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c   69,   70,   71,
203c2 20 20 20 37 32 2c 20 20 31 38 39 2c 20 20 20 37     72,  189,   7
203c3 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 0a 20  4,   75,   76,. 
203c4 2f 2a 20 20 20 34 37 30 20 2a 2f 20 20 20 20 37  /*   470 */    7
203c5 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20  7,   78,   79,  
203c6 20 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c   80,   81,   82,
203c7 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 31     83,   84,   1
203c8 36 2c 20 20 20 32 35 2c 0a 20 2f 2a 20 20 20 34  6,   25,. /*   4
203c9 38 30 20 2a 2f 20 20 20 32 31 31 2c 20 20 31 34  80 */   211,  14
203ca 37 2c 20 20 20 32 30 2c 20 20 20 32 39 2c 20 20  7,   20,   29,  
203cb 20 31 32 2c 20 20 31 34 37 2c 20 20 31 30 32 2c   12,  147,  102,
203cc 20 20 20 31 39 2c 20 20 32 31 31 2c 20 20 20 32     19,  211,   2
203cd 31 2c 0a 20 2f 2a 20 20 20 34 39 30 20 2a 2f 20  1,. /*   490 */ 
203ce 20 20 31 34 37 2c 20 20 31 34 31 2c 20 20 31 34    147,  141,  14
203cf 37 2c 20 20 32 31 36 2c 20 20 31 34 34 2c 20 20  7,  216,  144,  
203d0 20 34 31 2c 20 20 20 32 34 2c 20 20 20 39 38 2c   41,   24,   98,
203d1 20 20 20 32 30 2c 20 20 20 39 39 2c 0a 20 2f 2a     20,   99,. /*
203d2 20 20 20 35 30 30 20 2a 2f 20 20 20 31 30 30 2c     500 */   100,
203d3 20 20 31 30 31 2c 20 20 31 30 33 2c 20 20 31 36    101,  103,  16
203d4 35 2c 20 20 20 34 32 2c 20 20 20 34 33 2c 20 20  5,   42,   43,  
203d5 20 20 30 2c 20 20 20 20 31 2c 20 20 20 20 32 2c    0,    1,    2,
203d6 20 20 20 33 37 2c 0a 20 2f 2a 20 20 20 35 31 30     37,. /*   510
203d7 20 2a 2f 20 20 20 31 31 30 2c 20 20 20 33 39 2c   */   110,   39,
203d8 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20 31 36    169,  170,  16
203d9 39 2c 20 20 31 37 30 2c 20 20 31 38 32 2c 20 20  9,  170,  182,  
203da 20 31 39 2c 20 20 20 32 30 2c 20 20 31 34 37 2c   19,   20,  147,
203db 0a 20 2f 2a 20 20 20 35 32 30 20 2a 2f 20 20 20  . /*   520 */   
203dc 20 32 32 2c 20 20 20 34 39 2c 20 20 20 36 30 2c   22,   49,   60,
203dd 20 20 20 36 31 2c 20 20 20 36 32 2c 20 20 20 36     61,   62,   6
203de 33 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20  3,   64,   65,  
203df 20 36 36 2c 20 20 20 36 37 2c 0a 20 2f 2a 20 20   66,   67,. /*  
203e0 20 35 33 30 20 2a 2f 20 20 20 20 36 38 2c 20 20   530 */    68,  
203e1 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c   69,   70,   71,
203e2 20 20 20 37 32 2c 20 20 31 35 35 2c 20 20 20 37     72,  155,   7
203e3 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20  4,   75,   76,  
203e4 20 37 37 2c 0a 20 2f 2a 20 20 20 35 34 30 20 2a   77,. /*   540 *
203e5 2f 20 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20  /    78,   79,  
203e6 20 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c   80,   81,   82,
203e7 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 31     83,   84,   1
203e8 36 2c 20 20 31 34 37 2c 20 20 20 39 30 2c 0a 20  6,  147,   90,. 
203e9 2f 2a 20 20 20 35 35 30 20 2a 2f 20 20 20 20 32  /*   550 */    2
203ea 30 2c 20 20 20 32 30 2c 20 20 20 39 33 2c 20 20  0,   20,   93,  
203eb 20 39 34 2c 20 20 20 39 35 2c 20 20 31 34 37 2c   94,   95,  147,
203ec 20 20 31 35 35 2c 20 20 20 35 39 2c 20 20 32 31    155,   59,  21
203ed 35 2c 20 20 32 32 35 2c 0a 20 2f 2a 20 20 20 35  5,  225,. /*   5
203ee 36 30 20 2a 2f 20 20 20 32 31 35 2c 20 20 20 32  60 */   215,   2
203ef 30 2c 20 20 31 33 30 2c 20 20 31 30 34 2c 20 20  0,  130,  104,  
203f0 31 33 32 2c 20 20 32 32 37 2c 20 20 32 32 38 2c  132,  227,  228,
203f1 20 20 20 34 32 2c 20 20 20 34 33 2c 20 20 31 38     42,   43,  18
203f2 39 2c 0a 20 2f 2a 20 20 20 35 37 30 20 2a 2f 20  9,. /*   570 */ 
203f3 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20 20 31    169,  170,   1
203f4 36 2c 20 20 20 34 32 2c 20 20 20 34 33 2c 20 20  6,   42,   43,  
203f5 20 32 30 2c 20 20 20 31 39 2c 20 20 20 32 32 2c   20,   19,   22,
203f6 20 20 20 31 39 2c 20 20 20 32 30 2c 0a 20 2f 2a     19,   20,. /*
203f7 20 20 20 35 38 30 20 2a 2f 20 20 20 20 32 33 2c     580 */    23,
203f8 20 20 20 32 32 2c 20 20 20 31 38 2c 20 20 31 34     22,   18,  14
203f9 37 2c 20 20 31 30 36 2c 20 20 31 34 37 2c 20 20  7,  106,  147,  
203fa 31 30 38 2c 20 20 31 30 39 2c 20 20 20 36 33 2c  108,  109,   63,
203fb 20 20 20 36 34 2c 0a 20 2f 2a 20 20 20 35 39 30     64,. /*   590
203fc 20 2a 2f 20 20 20 31 38 39 2c 20 20 20 36 30 2c   */   189,   60,
203fd 20 20 20 36 31 2c 20 20 20 36 32 2c 20 20 20 36     61,   62,   6
203fe 33 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20  3,   64,   65,  
203ff 20 36 36 2c 20 20 20 36 37 2c 20 20 20 36 38 2c   66,   67,   68,
20400 0a 20 2f 2a 20 20 20 36 30 30 20 2a 2f 20 20 20  . /*   600 */   
20401 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c   69,   70,   71,
20402 20 20 20 37 32 2c 20 20 31 38 36 2c 20 20 20 37     72,  186,   7
20403 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20  4,   75,   76,  
20404 20 37 37 2c 20 20 20 37 38 2c 0a 20 2f 2a 20 20   77,   78,. /*  
20405 20 36 31 30 20 2a 2f 20 20 20 20 37 39 2c 20 20   610 */    79,  
20406 20 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c   80,   81,   82,
20407 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 31     83,   84,   1
20408 36 2c 20 20 20 39 32 2c 20 20 20 35 39 2c 20 20  6,   92,   59,  
20409 20 35 35 2c 0a 20 2f 2a 20 20 20 36 32 30 20 2a   55,. /*   620 *
2040a 2f 20 20 20 32 31 32 2c 20 20 20 32 31 2c 20 20  /   212,   21,  
2040b 31 34 37 2c 20 20 20 31 39 2c 20 20 31 34 37 2c  147,   19,  147,
2040c 20 20 20 32 33 2c 20 20 31 38 38 2c 20 20 20 32     23,  188,   2
2040d 33 2c 20 20 20 31 32 2c 20 20 32 31 37 2c 0a 20  3,   12,  217,. 
2040e 2f 2a 20 20 20 36 33 30 20 2a 2f 20 20 20 20 32  /*   630 */    2
2040f 33 2c 20 20 32 31 39 2c 20 20 32 32 30 2c 20 20  3,  219,  220,  
20410 20 20 37 2c 20 20 20 20 38 2c 20 20 20 20 39 2c    7,    8,    9,
20411 20 20 31 30 36 2c 20 20 31 34 37 2c 20 20 31 30    106,  147,  10
20412 38 2c 20 20 31 30 39 2c 0a 20 2f 2a 20 20 20 36  8,  109,. /*   6
20413 34 30 20 2a 2f 20 20 20 20 32 34 2c 20 20 31 34  40 */    24,  14
20414 37 2c 20 20 20 34 32 2c 20 20 20 34 33 2c 20 20  7,   42,   43,  
20415 32 30 38 2c 20 20 20 38 38 2c 20 20 20 38 39 2c  208,   88,   89,
20416 20 20 31 30 36 2c 20 20 20 39 32 2c 20 20 31 30    106,   92,  10
20417 38 2c 0a 20 2f 2a 20 20 20 36 35 30 20 2a 2f 20  8,. /*   650 */ 
20418 20 20 31 30 39 2c 20 20 32 34 34 2c 20 20 32 34    109,  244,  24
20419 35 2c 20 20 20 33 37 2c 20 20 31 34 35 2c 20 20  5,   37,  145,  
2041a 20 33 39 2c 20 20 31 39 31 2c 20 20 31 38 32 2c   39,  191,  182,
2041b 20 20 20 39 34 2c 20 20 20 31 36 2c 0a 20 2f 2a     94,   16,. /*
2041c 20 20 20 36 36 30 20 2a 2f 20 20 20 20 36 30 2c     660 */    60,
2041d 20 20 20 36 31 2c 20 20 20 36 32 2c 20 20 20 36     61,   62,   6
2041e 33 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20  3,   64,   65,  
2041f 20 36 36 2c 20 20 20 36 37 2c 20 20 20 36 38 2c   66,   67,   68,
20420 20 20 20 36 39 2c 0a 20 2f 2a 20 20 20 36 37 30     69,. /*   670
20421 20 2a 2f 20 20 20 20 37 30 2c 20 20 20 37 31 2c   */    70,   71,
20422 20 20 20 37 32 2c 20 20 31 34 37 2c 20 20 20 37     72,  147,   7
20423 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20  4,   75,   76,  
20424 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c   77,   78,   79,
20425 0a 20 2f 2a 20 20 20 36 38 30 20 2a 2f 20 20 20  . /*   680 */   
20426 20 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c   80,   81,   82,
20427 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 34     83,   84,   4
20428 32 2c 20 20 20 34 33 2c 20 20 20 38 30 2c 20 20  2,   43,   80,  
20429 31 34 32 2c 20 20 31 34 33 2c 0a 20 2f 2a 20 20  142,  143,. /*  
2042a 20 36 39 30 20 2a 2f 20 20 20 20 38 38 2c 20 20   690 */    88,  
2042b 20 38 39 2c 20 20 20 38 38 2c 20 20 20 38 39 2c   89,   88,   89,
2042c 20 20 31 34 38 2c 20 20 20 38 38 2c 20 20 20 38    148,   88,   8
2042d 39 2c 20 20 31 33 33 2c 20 20 20 31 34 2c 20 20  9,  133,   14,  
2042e 31 34 37 2c 0a 20 2f 2a 20 20 20 37 30 30 20 2a  147,. /*   700 *
2042f 2f 20 20 20 32 32 35 2c 20 20 31 35 35 2c 20 20  /   225,  155,  
20430 20 31 36 2c 20 20 20 36 30 2c 20 20 20 36 31 2c   16,   60,   61,
20431 20 20 20 36 32 2c 20 20 20 36 33 2c 20 20 20 36     62,   63,   6
20432 34 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 0a 20  4,   65,   66,. 
20433 2f 2a 20 20 20 37 31 30 20 2a 2f 20 20 20 20 36  /*   710 */    6
20434 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20  7,   68,   69,  
20435 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c   70,   71,   72,
20436 20 20 31 31 34 2c 20 20 20 37 34 2c 20 20 20 37    114,   74,   7
20437 35 2c 20 20 20 37 36 2c 0a 20 2f 2a 20 20 20 37  5,   76,. /*   7
20438 32 30 20 2a 2f 20 20 20 20 37 37 2c 20 20 20 37  20 */    77,   7
20439 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20  8,   79,   80,  
2043a 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c   81,   82,   83,
2043b 20 20 20 38 34 2c 20 20 20 34 32 2c 20 20 20 34     84,   42,   4
2043c 33 2c 0a 20 2f 2a 20 20 20 37 33 30 20 2a 2f 20  3,. /*   730 */ 
2043d 20 20 32 30 31 2c 20 20 32 30 32 2c 20 20 31 34    201,  202,  14
2043e 37 2c 20 20 31 34 37 2c 20 20 31 38 32 2c 20 20  7,  147,  182,  
2043f 31 38 39 2c 20 20 20 35 32 2c 20 20 31 34 37 2c  189,   52,  147,
20440 20 20 20 35 34 2c 20 20 31 34 37 2c 0a 20 2f 2a     54,  147,. /*
20441 20 20 20 37 34 30 20 2a 2f 20 20 20 31 34 37 2c     740 */   147,
20442 20 20 31 34 37 2c 20 20 31 34 37 2c 20 20 31 34    147,  147,  14
20443 37 2c 20 20 31 35 35 2c 20 20 20 31 36 2c 20 20  7,  155,   16,  
20444 20 36 30 2c 20 20 20 36 31 2c 20 20 20 36 32 2c   60,   61,   62,
20445 20 20 20 36 33 2c 0a 20 2f 2a 20 20 20 37 35 30     63,. /*   750
20446 20 2a 2f 20 20 20 20 36 34 2c 20 20 20 36 35 2c   */    64,   65,
20447 20 20 20 36 36 2c 20 20 20 36 37 2c 20 20 20 36     66,   67,   6
20448 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20  8,   69,   70,  
20449 20 37 31 2c 20 20 20 37 32 2c 20 20 32 31 33 2c   71,   72,  213,
2044a 0a 20 2f 2a 20 20 20 37 36 30 20 2a 2f 20 20 20  . /*   760 */   
2044b 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c   74,   75,   76,
2044c 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37     77,   78,   7
2044d 39 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 20 20  9,   80,   81,  
2044e 20 38 32 2c 20 20 20 38 33 2c 0a 20 2f 2a 20 20   82,   83,. /*  
2044f 20 37 37 30 20 2a 2f 20 20 20 20 38 34 2c 20 20   770 */    84,  
20450 20 34 32 2c 20 20 20 34 33 2c 20 20 31 38 38 2c   42,   43,  188,
20451 20 20 31 38 38 2c 20 20 31 38 32 2c 20 20 31 38    188,  182,  18
20452 32 2c 20 20 32 32 35 2c 20 20 31 38 39 2c 20 20  2,  225,  189,  
20453 31 30 36 2c 0a 20 2f 2a 20 20 20 37 38 30 20 2a  106,. /*   780 *
20454 2f 20 20 20 31 38 38 2c 20 20 31 30 38 2c 20 20  /   188,  108,  
20455 31 30 39 2c 20 20 31 38 38 2c 20 20 20 39 39 2c  109,  188,   99,
20456 20 20 31 30 30 2c 20 20 31 30 31 2c 20 20 32 34    100,  101,  24
20457 31 2c 20 20 20 31 36 2c 20 20 31 35 35 2c 0a 20  1,   16,  155,. 
20458 2f 2a 20 20 20 37 39 30 20 2a 2f 20 20 20 20 36  /*   790 */    6
20459 31 2c 20 20 20 36 32 2c 20 20 20 36 33 2c 20 20  1,   62,   63,  
2045a 20 36 34 2c 20 20 20 36 35 2c 20 20 20 36 36 2c   64,   65,   66,
2045b 20 20 20 36 37 2c 20 20 20 36 38 2c 20 20 20 36     67,   68,   6
2045c 39 2c 20 20 20 37 30 2c 0a 20 2f 2a 20 20 20 38  9,   70,. /*   8
2045d 30 30 20 2a 2f 20 20 20 20 37 31 2c 20 20 20 37  00 */    71,   7
2045e 32 2c 20 20 32 31 33 2c 20 20 20 37 34 2c 20 20  2,  213,   74,  
2045f 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c   75,   76,   77,
20460 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38     78,   79,   8
20461 30 2c 0a 20 2f 2a 20 20 20 38 31 30 20 2a 2f 20  0,. /*   810 */ 
20462 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38     81,   82,   8
20463 33 2c 20 20 20 38 34 2c 20 20 20 34 32 2c 20 20  3,   84,   42,  
20464 20 34 33 2c 20 20 20 32 33 2c 20 20 31 33 33 2c   43,   23,  133,
20465 20 20 32 32 35 2c 20 20 32 32 35 2c 0a 20 2f 2a    225,  225,. /*
20466 20 20 20 38 32 30 20 2a 2f 20 20 20 20 32 31 2c     820 */    21,
20467 20 20 32 32 35 2c 20 20 20 32 33 2c 20 20 31 38    225,   23,  18
20468 39 2c 20 20 32 33 39 2c 20 20 32 33 36 2c 20 20  9,  239,  236,  
20469 20 39 39 2c 20 20 31 30 30 2c 20 20 31 30 31 2c   99,  100,  101,
2046a 20 20 20 32 32 2c 0a 20 2f 2a 20 20 20 38 33 30     22,. /*   830
2046b 20 2a 2f 20 20 20 32 34 32 2c 20 20 32 34 33 2c   */   242,  243,
2046c 20 20 31 35 35 2c 20 20 20 32 32 2c 20 20 20 36    155,   22,   6
2046d 32 2c 20 20 20 36 33 2c 20 20 20 36 34 2c 20 20  2,   63,   64,  
2046e 20 36 35 2c 20 20 20 36 36 2c 20 20 20 36 37 2c   65,   66,   67,
2046f 0a 20 2f 2a 20 20 20 38 34 30 20 2a 2f 20 20 20  . /*   840 */   
20470 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c   68,   69,   70,
20471 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20 31 34     71,   72,  14
20472 37 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20  7,   74,   75,  
20473 20 37 36 2c 20 20 20 37 37 2c 0a 20 2f 2a 20 20   76,   77,. /*  
20474 20 38 35 30 20 2a 2f 20 20 20 20 37 38 2c 20 20   850 */    78,  
20475 20 37 39 2c 20 20 20 38 30 2c 20 20 20 38 31 2c   79,   80,   81,
20476 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38     82,   83,   8
20477 34 2c 20 20 20 31 36 2c 20 20 20 31 37 2c 20 20  4,   16,   17,  
20478 20 34 33 2c 0a 20 2f 2a 20 20 20 38 36 30 20 2a   43,. /*   860 *
20479 2f 20 20 20 20 31 39 2c 20 20 31 34 37 2c 20 20  /    19,  147,  
2047a 31 34 37 2c 20 20 31 34 37 2c 20 20 20 32 33 2c  147,  147,   23,
2047b 20 20 31 34 37 2c 20 20 31 38 39 2c 20 20 31 36    147,  189,  16
2047c 39 2c 20 20 31 37 30 2c 20 20 31 34 37 2c 0a 20  9,  170,  147,. 
2047d 2f 2a 20 20 20 38 37 30 20 2a 2f 20 20 20 31 34  /*   870 */   14
2047e 37 2c 20 20 31 34 37 2c 20 20 20 33 31 2c 20 20  7,  147,   31,  
2047f 20 31 36 2c 20 20 20 31 37 2c 20 20 31 34 37 2c   16,   17,  147,
20480 20 20 20 31 39 2c 20 20 31 34 37 2c 20 20 31 32     19,  147,  12
20481 34 2c 20 20 31 32 35 2c 0a 20 2f 2a 20 20 20 38  4,  125,. /*   8
20482 38 30 20 2a 2f 20 20 20 20 32 33 2c 20 20 20 38  80 */    23,   8
20483 38 2c 20 20 20 38 39 2c 20 20 31 36 39 2c 20 20  8,   89,  169,  
20484 31 37 30 2c 20 20 31 36 39 2c 20 20 31 37 30 2c  170,  169,  170,
20485 20 20 20 38 38 2c 20 20 20 33 31 2c 20 20 20 34     88,   31,   4
20486 38 2c 0a 20 2f 2a 20 20 20 38 39 30 20 2a 2f 20  8,. /*   890 */ 
20487 20 20 31 34 37 2c 20 20 31 36 39 2c 20 20 31 37    147,  169,  17
20488 30 2c 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20  0,  169,  170,  
20489 31 34 37 2c 20 20 20 38 39 2c 20 20 31 36 39 2c  147,   89,  169,
2048a 20 20 31 37 30 2c 20 20 20 35 38 2c 0a 20 2f 2a    170,   58,. /*
2048b 20 20 20 39 30 30 20 2a 2f 20 20 20 31 34 37 2c     900 */   147,
2048c 20 20 20 32 32 2c 20 20 31 34 37 2c 20 20 31 38     22,  147,  18
2048d 38 2c 20 20 31 34 37 2c 20 20 20 34 38 2c 20 20  8,  147,   48,  
2048e 31 38 38 2c 20 20 31 31 34 2c 20 20 20 39 37 2c  188,  114,   97,
2048f 20 20 31 34 37 2c 0a 20 2f 2a 20 20 20 39 31 30    147,. /*   910
20490 20 2a 2f 20 20 20 31 34 37 2c 20 20 31 38 38 2c   */   147,  188,
20491 20 20 31 34 37 2c 20 20 20 31 39 2c 20 20 20 39    147,   19,   9
20492 38 2c 20 20 20 35 38 2c 20 20 31 34 37 2c 20 20  8,   58,  147,  
20493 31 36 39 2c 20 20 31 37 30 2c 20 20 20 37 38 2c  169,  170,   78,
20494 0a 20 2f 2a 20 20 20 39 32 30 20 2a 2f 20 20 20  . /*   920 */   
20495 20 37 39 2c 20 20 31 31 34 2c 20 20 31 36 39 2c   79,  114,  169,
20496 20 20 31 37 30 2c 20 20 31 36 39 2c 20 20 31 37    170,  169,  17
20497 30 2c 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20  0,  169,  170,  
20498 20 38 37 2c 20 20 20 38 38 2c 0a 20 2f 2a 20 20   87,   88,. /*  
20499 20 39 33 30 20 2a 2f 20 20 20 20 38 39 2c 20 20   930 */    89,  
2049a 31 36 39 2c 20 20 31 37 30 2c 20 20 20 39 32 2c  169,  170,   92,
2049b 20 20 31 36 31 2c 20 20 20 37 38 2c 20 20 20 37    161,   78,   7
2049c 39 2c 20 20 20 38 30 2c 20 20 31 36 39 2c 20 20  9,   80,  169,  
2049d 31 37 30 2c 0a 20 2f 2a 20 20 20 39 34 30 20 2a  170,. /*   940 *
2049e 2f 20 20 20 20 39 31 2c 20 20 31 34 37 2c 20 20  /    91,  147,  
2049f 31 35 35 2c 20 20 20 32 32 2c 20 20 20 38 37 2c  155,   22,   87,
204a0 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 31     88,   89,   1
204a1 36 2c 20 20 20 31 37 2c 20 20 20 39 32 2c 0a 20  6,   17,   92,. 
204a2 2f 2a 20 20 20 39 35 30 20 2a 2f 20 20 20 20 31  /*   950 */    1
204a3 39 2c 20 20 31 31 30 2c 20 20 31 34 37 2c 20 20  9,  110,  147,  
204a4 31 35 35 2c 20 20 20 32 33 2c 20 20 31 34 37 2c  155,   23,  147,
204a5 20 20 20 20 37 2c 20 20 20 20 38 2c 20 20 20 32      7,    8,   2
204a6 30 2c 20 20 31 31 30 2c 0a 20 2f 2a 20 20 20 39  0,  110,. /*   9
204a7 36 30 20 2a 2f 20 20 20 20 32 32 2c 20 20 20 38  60 */    22,   8
204a8 30 2c 20 20 20 33 31 2c 20 20 31 36 39 2c 20 20  0,   31,  169,  
204a9 31 37 30 2c 20 20 31 32 34 2c 20 20 31 32 35 2c  170,  124,  125,
204aa 20 20 31 32 36 2c 20 20 31 32 37 2c 20 20 31 32    126,  127,  12
204ab 38 2c 0a 20 2f 2a 20 20 20 39 37 30 20 2a 2f 20  8,. /*   970 */ 
204ac 20 20 31 32 39 2c 20 20 32 30 38 2c 20 20 31 32    129,  208,  12
204ad 33 2c 20 20 32 30 38 2c 20 20 31 36 39 2c 20 20  3,  208,  169,  
204ae 31 37 30 2c 20 20 31 38 39 2c 20 20 31 36 39 2c  170,  189,  169,
204af 20 20 31 37 30 2c 20 20 20 34 38 2c 0a 20 2f 2a    170,   48,. /*
204b0 20 20 20 39 38 30 20 2a 2f 20 20 20 31 34 37 2c     980 */   147,
204b1 20 20 31 32 34 2c 20 20 31 32 35 2c 20 20 31 32    124,  125,  12
204b2 36 2c 20 20 31 32 37 2c 20 20 31 32 38 2c 20 20  6,  127,  128,  
204b3 31 32 39 2c 20 20 31 38 39 2c 20 20 31 30 37 2c  129,  189,  107,
204b4 20 20 20 35 38 2c 0a 20 2f 2a 20 20 20 39 39 30     58,. /*   990
204b5 20 2a 2f 20 20 20 31 30 37 2c 20 20 20 20 35 2c   */   107,    5,
204b6 20 20 31 31 31 2c 20 20 31 34 37 2c 20 20 31 31    111,  147,  11
204b7 31 2c 20 20 32 30 33 2c 20 20 20 31 30 2c 20 20  1,  203,   10,  
204b8 20 31 31 2c 20 20 20 31 32 2c 20 20 20 31 33 2c   11,   12,   13,
204b9 0a 20 2f 2a 20 20 31 30 30 30 20 2a 2f 20 20 20  . /*  1000 */   
204ba 31 32 31 2c 20 20 31 34 37 2c 20 20 31 34 37 2c  121,  147,  147,
204bb 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20 31 34     91,   92,  14
204bc 37 2c 20 20 31 31 32 2c 20 20 31 34 37 2c 20 20  7,  112,  147,  
204bd 31 34 37 2c 20 20 20 37 38 2c 0a 20 2f 2a 20 20  147,   78,. /*  
204be 31 30 31 30 20 2a 2f 20 20 20 20 37 39 2c 20 20  1010 */    79,  
204bf 31 34 37 2c 20 20 20 32 36 2c 20 20 20 31 39 2c  147,   26,   19,
204c0 20 20 20 32 38 2c 20 20 31 36 39 2c 20 20 31 37     28,  169,  17
204c1 30 2c 20 20 20 32 33 2c 20 20 20 38 37 2c 20 20  0,   23,   87,  
204c2 20 38 38 2c 0a 20 2f 2a 20 20 31 30 32 30 20 2a   88,. /*  1020 *
204c3 2f 20 20 20 20 38 39 2c 20 20 20 33 35 2c 20 20  /    89,   35,  
204c4 31 34 37 2c 20 20 20 39 32 2c 20 20 31 36 39 2c  147,   92,  169,
204c5 20 20 31 37 30 2c 20 20 31 37 38 2c 20 20 31 36    170,  178,  16
204c6 39 2c 20 20 31 37 30 2c 20 20 31 34 37 2c 0a 20  9,  170,  147,. 
204c7 2f 2a 20 20 31 30 33 30 20 2a 2f 20 20 20 31 36  /*  1030 */   16
204c8 39 2c 20 20 31 37 30 2c 20 20 31 34 37 2c 20 20  9,  170,  147,  
204c9 20 34 37 2c 20 20 31 31 33 2c 20 20 20 34 39 2c   47,  113,   49,
204ca 20 20 20 39 32 2c 20 20 31 37 38 2c 20 20 31 34     92,  178,  14
204cb 37 2c 20 20 20 35 33 2c 0a 20 2f 2a 20 20 31 30  7,   53,. /*  10
204cc 34 30 20 2a 2f 20 20 20 31 34 37 2c 20 20 31 37  40 */   147,  17
204cd 38 2c 20 20 20 35 36 2c 20 20 31 34 37 2c 20 20  8,   56,  147,  
204ce 31 36 39 2c 20 20 31 37 30 2c 20 20 31 34 37 2c  169,  170,  147,
204cf 20 20 31 30 33 2c 20 20 31 34 37 2c 20 20 20 31    103,  147,   1
204d0 39 2c 0a 20 2f 2a 20 20 31 30 35 30 20 2a 2f 20  9,. /*  1050 */ 
204d1 20 20 31 34 37 2c 20 20 31 36 39 2c 20 20 31 37    147,  169,  17
204d2 30 2c 20 20 31 34 37 2c 20 20 31 34 37 2c 20 20  0,  147,  147,  
204d3 31 32 34 2c 20 20 31 32 35 2c 20 20 31 32 36 2c  124,  125,  126,
204d4 20 20 31 32 37 2c 20 20 31 32 38 2c 0a 20 2f 2a    127,  128,. /*
204d5 20 20 31 30 36 30 20 2a 2f 20 20 20 31 32 39 2c    1060 */   129,
204d6 20 20 31 34 37 2c 20 20 31 36 39 2c 20 20 31 37    147,  169,  17
204d7 30 2c 20 20 31 34 37 2c 20 20 31 36 39 2c 20 20  0,  147,  169,  
204d8 31 37 30 2c 20 20 31 34 37 2c 20 20 31 36 39 2c  170,  147,  169,
204d9 20 20 31 37 30 2c 0a 20 2f 2a 20 20 31 30 37 30    170,. /*  1070
204da 20 2a 2f 20 20 20 31 36 39 2c 20 20 31 37 30 2c   */   169,  170,
204db 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20 31 34    169,  170,  14
204dc 37 2c 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20  7,  169,  170,  
204dd 31 34 37 2c 20 20 20 32 30 2c 20 20 31 34 37 2c  147,   20,  147,
204de 0a 20 2f 2a 20 20 31 30 38 30 20 2a 2f 20 20 20  . /*  1080 */   
204df 20 32 32 2c 20 20 31 34 37 2c 20 20 20 38 38 2c   22,  147,   88,
204e0 20 20 31 34 37 2c 20 20 32 33 32 2c 20 20 20 39    147,  232,   9
204e1 39 2c 20 20 31 30 30 2c 20 20 31 30 31 2c 20 20  9,  100,  101,  
204e2 31 34 37 2c 20 20 31 36 39 2c 0a 20 2f 2a 20 20  147,  169,. /*  
204e3 31 30 39 30 20 2a 2f 20 20 20 31 37 30 2c 20 20  1090 */   170,  
204e4 31 30 35 2c 20 20 31 34 37 2c 20 20 20 32 30 2c  105,  147,   20,
204e5 20 20 31 34 37 2c 20 20 20 32 32 2c 20 20 31 31    147,   22,  11
204e6 30 2c 20 20 20 32 30 2c 20 20 20 36 38 2c 20 20  0,   20,   68,  
204e7 31 36 39 2c 0a 20 2f 2a 20 20 31 31 30 30 20 2a  169,. /*  1100 *
204e8 2f 20 20 20 31 37 30 2c 20 20 31 36 39 2c 20 20  /   170,  169,  
204e9 31 37 30 2c 20 20 31 36 39 2c 20 20 31 37 30 2c  170,  169,  170,
204ea 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20 20 32    169,  170,   2
204eb 30 2c 20 20 31 34 37 2c 20 20 20 32 32 2c 0a 20  0,  147,   22,. 
204ec 2f 2a 20 20 31 31 31 30 20 2a 2f 20 20 20 31 34  /*  1110 */   14
204ed 37 2c 20 20 20 32 30 2c 20 20 31 34 37 2c 20 20  7,   20,  147,  
204ee 20 32 32 2c 20 20 31 36 39 2c 20 20 31 37 30 2c   22,  169,  170,
204ef 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20 31 34    169,  170,  14
204f0 37 2c 20 20 32 33 33 2c 0a 20 2f 2a 20 20 31 31  7,  233,. /*  11
204f1 32 30 20 2a 2f 20 20 20 31 33 34 2c 20 20 20 32  20 */   134,   2
204f2 30 2c 20 20 31 34 37 2c 20 20 20 32 32 2c 20 20  0,  147,   22,  
204f3 20 32 30 2c 20 20 31 34 37 2c 20 20 20 32 32 2c   20,  147,   22,
204f4 20 20 20 32 30 2c 20 20 20 32 30 2c 20 20 20 32     20,   20,   2
204f5 32 2c 0a 20 2f 2a 20 20 31 31 33 30 20 2a 2f 20  2,. /*  1130 */ 
204f6 20 20 20 32 32 2c 20 20 31 34 37 2c 20 20 31 36     22,  147,  16
204f7 39 2c 20 20 31 37 30 2c 20 20 31 36 39 2c 20 20  9,  170,  169,  
204f8 31 37 30 2c 20 20 20 35 39 2c 20 20 31 34 37 2c  170,   59,  147,
204f9 20 20 31 34 37 2c 20 20 31 34 37 2c 0a 20 2f 2a    147,  147,. /*
204fa 20 20 31 31 34 30 20 2a 2f 20 20 20 31 36 39 2c    1140 */   169,
204fb 20 20 31 37 30 2c 20 20 31 34 37 2c 20 20 31 34    170,  147,  14
204fc 37 2c 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20  7,  169,  170,  
204fd 31 34 37 2c 20 20 31 36 39 2c 20 20 31 37 30 2c  147,  169,  170,
204fe 20 20 31 34 37 2c 0a 20 2f 2a 20 20 31 31 35 30    147,. /*  1150
204ff 20 2a 2f 20 20 20 31 34 37 2c 20 20 31 34 37 2c   */   147,  147,
20500 20 20 31 34 37 2c 20 20 31 39 31 2c 20 20 32 32    147,  191,  22
20501 39 2c 20 20 31 34 39 2c 20 20 32 32 33 2c 20 20  9,  149,  223,  
20502 31 39 33 2c 20 20 32 32 39 2c 20 20 31 37 32 2c  193,  229,  172,
20503 0a 20 2f 2a 20 20 31 31 36 30 20 2a 2f 20 20 20  . /*  1160 */   
20504 31 37 32 2c 20 20 31 37 32 2c 20 20 31 37 37 2c  172,  172,  177,
20505 20 20 31 39 34 2c 20 20 31 39 34 2c 20 20 31 37    194,  194,  17
20506 32 2c 20 20 20 20 36 2c 20 20 31 34 36 2c 20 20  2,    6,  146,  
20507 31 34 36 2c 20 20 31 37 32 2c 0a 20 2f 2a 20 20  146,  172,. /*  
20508 31 31 37 30 20 2a 2f 20 20 20 31 34 36 2c 20 20  1170 */   146,  
20509 31 37 33 2c 20 20 31 34 36 2c 20 20 20 32 32 2c  173,  146,   22,
2050a 20 20 31 35 34 2c 20 20 31 32 31 2c 20 20 31 39    154,  121,  19
2050b 34 2c 20 20 31 31 38 2c 20 20 31 39 35 2c 20 20  4,  118,  195,  
2050c 31 31 39 2c 0a 20 2f 2a 20 20 31 31 38 30 20 2a  119,. /*  1180 *
2050d 2f 20 20 20 31 31 36 2c 20 20 31 32 30 2c 20 20  /   116,  120,  
2050e 20 32 33 2c 20 20 31 36 30 2c 20 20 31 31 32 2c   23,  160,  112,
2050f 20 20 31 33 30 2c 20 20 31 35 32 2c 20 20 31 35    130,  152,  15
20510 32 2c 20 20 32 32 32 2c 20 20 31 38 39 2c 0a 20  2,  222,  189,. 
20511 2f 2a 20 20 31 31 39 30 20 2a 2f 20 20 20 20 39  /*  1190 */    9
20512 38 2c 20 20 31 31 35 2c 20 20 20 39 38 2c 20 20  8,  115,   98,  
20513 20 34 30 2c 20 20 31 37 39 2c 20 20 20 39 37 2c   40,  179,   97,
20514 20 20 31 37 31 2c 20 20 31 39 36 2c 20 20 31 37    171,  196,  17
20515 31 2c 20 20 31 37 31 2c 0a 20 2f 2a 20 20 31 32  1,  171,. /*  12
20516 30 30 20 2a 2f 20 20 20 20 31 39 2c 20 20 20 38  00 */    19,   8
20517 34 2c 20 20 31 37 33 2c 20 20 31 37 31 2c 20 20  4,  173,  171,  
20518 31 39 37 2c 20 20 31 37 34 2c 20 20 31 39 38 2c  197,  174,  198,
20519 20 20 32 32 36 2c 20 20 31 36 30 2c 20 20 31 36    226,  160,  16
2051a 30 2c 0a 20 2f 2a 20 20 31 32 31 30 20 2a 2f 20  0,. /*  1210 */ 
2051b 20 20 31 37 31 2c 20 20 31 37 31 2c 20 20 31 37    171,  171,  17
2051c 39 2c 20 20 32 30 34 2c 20 20 31 37 31 2c 20 20  9,  204,  171,  
2051d 32 30 35 2c 20 20 32 30 34 2c 20 20 20 31 35 2c  205,  204,   15,
2051e 20 20 32 30 35 2c 20 20 31 37 34 2c 0a 20 2f 2a    205,  174,. /*
2051f 20 20 31 32 32 30 20 2a 2f 20 20 20 31 35 31 2c    1220 */   151,
20520 20 20 20 33 38 2c 20 20 31 35 32 2c 20 20 31 35     38,  152,  15
20521 32 2c 20 20 20 36 30 2c 20 20 31 35 31 2c 20 20  2,   60,  151,  
20522 31 35 31 2c 20 20 31 33 30 2c 20 20 31 38 34 2c  151,  130,  184,
20523 20 20 20 31 39 2c 0a 20 2f 2a 20 20 31 32 33 30     19,. /*  1230
20524 20 2a 2f 20 20 20 31 35 32 2c 20 20 31 35 32 2c   */   152,  152,
20525 20 20 31 35 31 2c 20 20 32 31 34 2c 20 20 31 35    151,  214,  15
20526 32 2c 20 20 31 38 34 2c 20 20 31 39 34 2c 20 20  2,  184,  194,  
20527 32 32 36 2c 20 20 31 35 32 2c 20 20 20 31 35 2c  226,  152,   15,
20528 0a 20 2f 2a 20 20 31 32 34 30 20 2a 2f 20 20 20  . /*  1240 */   
20529 31 38 37 2c 20 20 31 39 34 2c 20 20 31 35 32 2c  187,  194,  152,
2052a 20 20 31 38 37 2c 20 20 31 38 37 2c 20 20 20 33    187,  187,   3
2052b 33 2c 20 20 31 33 37 2c 20 20 31 38 34 2c 20 20  3,  137,  184,  
2052c 32 33 34 2c 20 20 31 38 37 2c 0a 20 2f 2a 20 20  234,  187,. /*  
2052d 31 32 35 30 20 2a 2f 20 20 20 32 31 34 2c 20 20  1250 */   214,  
2052e 32 33 35 2c 20 20 31 35 32 2c 20 20 20 20 31 2c  235,  152,    1,
2052f 20 20 32 33 37 2c 20 20 32 33 37 2c 20 20 20 32    237,  237,   2
20530 30 2c 20 20 31 35 32 2c 20 20 31 35 39 2c 20 20  0,  152,  159,  
20531 31 37 35 2c 0a 20 2f 2a 20 20 31 32 36 30 20 2a  175,. /*  1260 *
20532 2f 20 20 20 31 37 35 2c 20 20 31 31 32 2c 20 20  /   175,  112,  
20533 31 31 32 2c 20 20 20 39 32 2c 20 20 31 31 32 2c  112,   92,  112,
20534 20 20 31 30 37 2c 20 20 31 31 32 2c 20 20 20 31    107,  112,   1
20535 39 2c 20 20 20 31 39 2c 20 20 32 33 31 2c 0a 20  9,   19,  231,. 
20536 2f 2a 20 20 31 32 37 30 20 2a 2f 20 20 20 20 32  /*  1270 */    2
20537 30 2c 20 20 20 32 30 2c 20 20 20 31 31 2c 20 20  0,   20,   11,  
20538 20 31 39 2c 20 20 31 31 37 2c 20 20 20 32 30 2c   19,  117,   20,
20539 20 20 31 31 34 2c 20 20 32 34 30 2c 20 20 20 32    114,  240,   2
2053a 32 2c 20 20 20 32 30 2c 0a 20 2f 2a 20 20 31 32  2,   20,. /*  12
2053b 38 30 20 2a 2f 20 20 20 20 32 30 2c 20 20 20 31  80 */    20,   1
2053c 39 2c 20 20 31 31 37 2c 20 20 20 34 34 2c 20 20  9,  117,   44,  
2053d 32 34 33 2c 20 20 20 32 32 2c 20 20 20 32 32 2c  243,   22,   22,
2053e 20 20 20 32 30 2c 20 20 31 31 32 2c 20 20 20 32     20,  112,   2
2053f 30 2c 0a 20 2f 2a 20 20 31 32 39 30 20 2a 2f 20  0,. /*  1290 */ 
20540 20 20 20 32 30 2c 20 20 20 31 39 2c 20 20 32 34     20,   19,  24
20541 36 2c 20 20 20 31 39 2c 20 20 20 39 36 2c 20 20  6,   19,   96,  
20542 20 32 30 2c 20 20 20 31 39 2c 20 20 20 33 32 2c   20,   19,   32,
20543 20 20 20 31 39 2c 20 20 20 31 39 2c 0a 20 2f 2a     19,   19,. /*
20544 20 20 31 33 30 30 20 2a 2f 20 20 20 31 30 33 2c    1300 */   103,
20545 20 20 20 34 34 2c 20 20 20 34 34 2c 20 20 20 31     44,   44,   1
20546 36 2c 20 20 20 32 31 2c 20 20 20 31 37 2c 20 20  6,   21,   17,  
20547 20 39 38 2c 20 20 20 33 36 2c 20 20 20 32 32 2c   98,   36,   22,
20548 20 20 20 39 38 2c 0a 20 2f 2a 20 20 31 33 31 30     98,. /*  1310
20549 20 2a 2f 20 20 20 31 33 33 2c 20 20 20 31 39 2c   */   133,   19,
2054a 20 20 20 20 35 2c 20 20 20 34 35 2c 20 20 20 20      5,   45,    
2054b 31 2c 20 20 31 30 32 2c 20 20 31 32 32 2c 20 20  1,  102,  122,  
2054c 20 35 31 2c 20 20 20 34 35 2c 20 20 20 31 39 2c   51,   45,   19,
2054d 0a 20 2f 2a 20 20 31 33 32 30 20 2a 2f 20 20 20  . /*  1320 */   
2054e 31 31 33 2c 20 20 20 36 38 2c 20 20 20 31 34 2c  113,   68,   14,
2054f 20 20 20 31 37 2c 20 20 31 31 35 2c 20 20 31 30     17,  115,  10
20550 32 2c 20 20 31 32 32 2c 20 20 31 31 33 2c 20 20  2,  122,  113,  
20551 20 31 39 2c 20 20 31 32 33 2c 0a 20 2f 2a 20 20   19,  123,. /*  
20552 31 33 33 30 20 2a 2f 20 20 20 20 32 30 2c 20 20  1330 */    20,  
20553 20 31 34 2c 20 20 31 33 36 2c 20 20 20 31 39 2c   14,  136,   19,
20554 20 20 31 33 35 2c 20 20 20 35 37 2c 20 20 20 20    135,   57,    
20555 33 2c 20 20 32 34 37 2c 20 20 20 20 34 2c 20 20  3,  247,    4,  
20556 32 34 37 2c 0a 20 2f 2a 20 20 31 33 34 30 20 2a  247,. /*  1340 *
20557 2f 20 20 20 32 34 37 2c 20 20 32 34 37 2c 20 20  /   247,  247,  
20558 32 34 37 2c 20 20 32 34 37 2c 20 20 20 36 38 2c  247,  247,   68,
20559 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 59 59 5f 53  .};.#define YY_S
2055a 48 49 46 54 5f 55 53 45 5f 44 46 4c 54 20 28 2d  HIFT_USE_DFLT (-
2055b 36 32 29 0a 23 64 65 66 69 6e 65 20 59 59 5f 53  62).#define YY_S
2055c 48 49 46 54 5f 4d 41 58 20 33 38 37 0a 73 74 61  HIFT_MAX 387.sta
2055d 74 69 63 20 63 6f 6e 73 74 20 73 68 6f 72 74 20  tic const short 
2055e 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b 5d 20  yy_shift_ofst[] 
2055f 3d 20 7b 0a 20 2f 2a 20 20 20 20 20 30 20 2a 2f  = {. /*     0 */
20560 20 20 20 20 33 39 2c 20 20 38 34 31 2c 20 20 39      39,  841,  9
20561 38 36 2c 20 20 2d 31 36 2c 20 20 38 34 31 2c 20  86,  -16,  841, 
20562 20 39 33 31 2c 20 20 39 33 31 2c 20 20 32 35 38   931,  931,  258
20563 2c 20 20 31 32 33 2c 20 20 2d 33 36 2c 0a 20 2f  ,  123,  -36,. /
20564 2a 20 20 20 20 31 30 20 2a 2f 20 20 20 20 39 36  *    10 */    96
20565 2c 20 20 39 33 31 2c 20 20 39 33 31 2c 20 20 39  ,  931,  931,  9
20566 33 31 2c 20 20 39 33 31 2c 20 20 39 33 31 2c 20  31,  931,  931, 
20567 20 2d 34 35 2c 20 20 34 30 30 2c 20 20 31 37 34   -45,  400,  174
20568 2c 20 20 20 31 39 2c 0a 20 2f 2a 20 20 20 20 32  ,   19,. /*    2
20569 30 20 2a 2f 20 20 20 31 37 31 2c 20 20 2d 35 34  0 */   171,  -54
2056a 2c 20 20 2d 35 34 2c 20 20 20 35 33 2c 20 20 31  ,  -54,   53,  1
2056b 36 35 2c 20 20 32 30 38 2c 20 20 32 35 31 2c 20  65,  208,  251, 
2056c 20 33 32 34 2c 20 20 33 39 33 2c 20 20 34 36 32   324,  393,  462
2056d 2c 0a 20 2f 2a 20 20 20 20 33 30 20 2a 2f 20 20  ,. /*    30 */  
2056e 20 35 33 31 2c 20 20 36 30 30 2c 20 20 36 34 33   531,  600,  643
2056f 2c 20 20 36 38 36 2c 20 20 36 34 33 2c 20 20 36  ,  686,  643,  6
20570 34 33 2c 20 20 36 34 33 2c 20 20 36 34 33 2c 20  43,  643,  643, 
20571 20 36 34 33 2c 20 20 36 34 33 2c 0a 20 2f 2a 20   643,  643,. /* 
20572 20 20 20 34 30 20 2a 2f 20 20 20 36 34 33 2c 20     40 */   643, 
20573 20 36 34 33 2c 20 20 36 34 33 2c 20 20 36 34 33   643,  643,  643
20574 2c 20 20 36 34 33 2c 20 20 36 34 33 2c 20 20 36  ,  643,  643,  6
20575 34 33 2c 20 20 36 34 33 2c 20 20 36 34 33 2c 20  43,  643,  643, 
20576 20 36 34 33 2c 0a 20 2f 2a 20 20 20 20 35 30 20   643,. /*    50 
20577 2a 2f 20 20 20 36 34 33 2c 20 20 36 34 33 2c 20  */   643,  643, 
20578 20 37 32 39 2c 20 20 37 37 32 2c 20 20 37 37 32   729,  772,  772
20579 2c 20 20 38 35 37 2c 20 20 39 33 31 2c 20 20 39  ,  857,  931,  9
2057a 33 31 2c 20 20 39 33 31 2c 20 20 39 33 31 2c 0a  31,  931,  931,.
2057b 20 2f 2a 20 20 20 20 36 30 20 2a 2f 20 20 20 39   /*    60 */   9
2057c 33 31 2c 20 20 39 33 31 2c 20 20 39 33 31 2c 20  31,  931,  931, 
2057d 20 39 33 31 2c 20 20 39 33 31 2c 20 20 39 33 31   931,  931,  931
2057e 2c 20 20 39 33 31 2c 20 20 39 33 31 2c 20 20 39  ,  931,  931,  9
2057f 33 31 2c 20 20 39 33 31 2c 0a 20 2f 2a 20 20 20  31,  931,. /*   
20580 20 37 30 20 2a 2f 20 20 20 39 33 31 2c 20 20 39   70 */   931,  9
20581 33 31 2c 20 20 39 33 31 2c 20 20 39 33 31 2c 20  31,  931,  931, 
20582 20 39 33 31 2c 20 20 39 33 31 2c 20 20 39 33 31   931,  931,  931
20583 2c 20 20 39 33 31 2c 20 20 39 33 31 2c 20 20 39  ,  931,  931,  9
20584 33 31 2c 0a 20 2f 2a 20 20 20 20 38 30 20 2a 2f  31,. /*    80 */
20585 20 20 20 39 33 31 2c 20 20 39 33 31 2c 20 20 39     931,  931,  9
20586 33 31 2c 20 20 39 33 31 2c 20 20 39 33 31 2c 20  31,  931,  931, 
20587 20 39 33 31 2c 20 20 39 33 31 2c 20 20 39 33 31   931,  931,  931
20588 2c 20 20 39 33 31 2c 20 20 39 33 31 2c 0a 20 2f  ,  931,  931,. /
20589 2a 20 20 20 20 39 30 20 2a 2f 20 20 20 39 33 31  *    90 */   931
2058a 2c 20 20 39 33 31 2c 20 20 39 33 31 2c 20 20 39  ,  931,  931,  9
2058b 33 31 2c 20 20 39 33 31 2c 20 20 2d 36 31 2c 20  31,  931,  -61, 
2058c 20 2d 36 31 2c 20 20 20 20 36 2c 20 20 20 20 36   -61,    6,    6
2058d 2c 20 20 32 38 30 2c 0a 20 2f 2a 20 20 20 31 30  ,  280,. /*   10
2058e 30 20 2a 2f 20 20 20 20 32 32 2c 20 20 20 36 31  0 */    22,   61
2058f 2c 20 20 33 39 39 2c 20 20 35 36 34 2c 20 20 20  ,  399,  564,   
20590 31 39 2c 20 20 20 31 39 2c 20 20 20 31 39 2c 20  19,   19,   19, 
20591 20 20 31 39 2c 20 20 20 31 39 2c 20 20 20 31 39    19,   19,   19
20592 2c 0a 20 2f 2a 20 20 20 31 31 30 20 2a 2f 20 20  ,. /*   110 */  
20593 20 20 31 39 2c 20 20 32 31 36 2c 20 20 31 37 31    19,  216,  171
20594 2c 20 20 20 36 33 2c 20 20 2d 36 32 2c 20 20 2d  ,   63,  -62,  -
20595 36 32 2c 20 20 31 33 31 2c 20 20 33 32 36 2c 20  62,  131,  326, 
20596 20 34 37 32 2c 20 20 34 37 32 2c 0a 20 2f 2a 20   472,  472,. /* 
20597 20 20 31 32 30 20 2a 2f 20 20 20 34 39 38 2c 20    120 */   498, 
20598 20 35 35 39 2c 20 20 35 30 36 2c 20 20 37 39 39   559,  506,  799
20599 2c 20 20 20 31 39 2c 20 20 37 39 39 2c 20 20 20  ,   19,  799,   
2059a 31 39 2c 20 20 20 31 39 2c 20 20 20 31 39 2c 20  19,   19,   19, 
2059b 20 20 31 39 2c 0a 20 2f 2a 20 20 20 31 33 30 20    19,. /*   130 
2059c 2a 2f 20 20 20 20 31 39 2c 20 20 20 31 39 2c 20  */    19,   19, 
2059d 20 20 31 39 2c 20 20 20 31 39 2c 20 20 20 31 39    19,   19,   19
2059e 2c 20 20 20 31 39 2c 20 20 20 31 39 2c 20 20 20  ,   19,   19,   
2059f 31 39 2c 20 20 20 31 39 2c 20 20 38 34 39 2c 0a  19,   19,  849,.
205a0 20 2f 2a 20 20 20 31 34 30 20 2a 2f 20 20 20 20   /*   140 */    
205a1 39 35 2c 20 20 2d 33 36 2c 20 20 2d 33 36 2c 20  95,  -36,  -36, 
205a2 20 2d 33 36 2c 20 20 2d 36 32 2c 20 20 2d 36 32   -36,  -62,  -62
205a3 2c 20 20 2d 36 32 2c 20 20 2d 31 35 2c 20 20 2d  ,  -62,  -15,  -
205a4 31 35 2c 20 20 33 33 33 2c 0a 20 2f 2a 20 20 20  15,  333,. /*   
205a5 31 35 30 20 2a 2f 20 20 20 34 35 39 2c 20 20 34  150 */   459,  4
205a6 37 38 2c 20 20 35 35 37 2c 20 20 35 33 30 2c 20  78,  557,  530, 
205a7 20 35 34 31 2c 20 20 36 31 36 2c 20 20 36 30 32   541,  616,  602
205a8 2c 20 20 37 39 33 2c 20 20 36 30 34 2c 20 20 36  ,  793,  604,  6
205a9 30 37 2c 0a 20 2f 2a 20 20 20 31 36 30 20 2a 2f  07,. /*   160 */
205aa 20 20 20 36 32 36 2c 20 20 20 31 39 2c 20 20 20     626,   19,   
205ab 31 39 2c 20 20 38 38 31 2c 20 20 20 31 39 2c 20  19,  881,   19, 
205ac 20 20 31 39 2c 20 20 39 39 34 2c 20 20 20 31 39    19,  994,   19
205ad 2c 20 20 20 31 39 2c 20 20 38 30 37 2c 0a 20 2f  ,   19,  807,. /
205ae 2a 20 20 20 31 37 30 20 2a 2f 20 20 20 20 31 39  *   170 */    19
205af 2c 20 20 20 31 39 2c 20 20 36 37 33 2c 20 20 38  ,   19,  673,  8
205b0 30 37 2c 20 20 20 31 39 2c 20 20 20 31 39 2c 20  07,   19,   19, 
205b1 20 33 38 34 2c 20 20 33 38 34 2c 20 20 33 38 34   384,  384,  384
205b2 2c 20 20 20 31 39 2c 0a 20 2f 2a 20 20 20 31 38  ,   19,. /*   18
205b3 30 20 2a 2f 20 20 20 20 31 39 2c 20 20 36 37 33  0 */    19,  673
205b4 2c 20 20 20 31 39 2c 20 20 20 31 39 2c 20 20 36  ,   19,   19,  6
205b5 37 33 2c 20 20 20 31 39 2c 20 20 34 35 34 2c 20  73,   19,  454, 
205b6 20 36 38 35 2c 20 20 20 31 39 2c 20 20 20 31 39   685,   19,   19
205b7 2c 0a 20 2f 2a 20 20 20 31 39 30 20 2a 2f 20 20  ,. /*   190 */  
205b8 20 36 37 33 2c 20 20 20 31 39 2c 20 20 20 31 39   673,   19,   19
205b9 2c 20 20 20 31 39 2c 20 20 36 37 33 2c 20 20 20  ,   19,  673,   
205ba 31 39 2c 20 20 20 31 39 2c 20 20 20 31 39 2c 20  19,   19,   19, 
205bb 20 36 37 33 2c 20 20 36 37 33 2c 0a 20 2f 2a 20   673,  673,. /* 
205bc 20 20 32 30 30 20 2a 2f 20 20 20 20 31 39 2c 20    200 */    19, 
205bd 20 20 31 39 2c 20 20 20 31 39 2c 20 20 20 31 39    19,   19,   19
205be 2c 20 20 20 31 39 2c 20 20 34 36 38 2c 20 20 38  ,   19,  468,  8
205bf 38 33 2c 20 20 39 32 31 2c 20 20 37 35 34 2c 20  83,  921,  754, 
205c0 20 37 35 34 2c 0a 20 2f 2a 20 20 20 32 31 30 20   754,. /*   210 
205c1 2a 2f 20 20 20 34 33 32 2c 20 20 34 30 36 2c 20  */   432,  406, 
205c2 20 34 30 36 2c 20 20 34 30 36 2c 20 20 38 31 36   406,  406,  816
205c3 2c 20 20 34 30 36 2c 20 20 34 30 36 2c 20 20 38  ,  406,  406,  8
205c4 31 31 2c 20 20 38 37 39 2c 20 20 38 37 39 2c 0a  11,  879,  879,.
205c5 20 2f 2a 20 20 20 32 32 30 20 2a 2f 20 20 31 31   /*   220 */  11
205c6 36 30 2c 20 31 31 36 30 2c 20 31 31 36 30 2c 20  60, 1160, 1160, 
205c7 31 31 36 30 2c 20 31 31 35 31 2c 20 20 2d 33 36  1160, 1151,  -36
205c8 2c 20 31 30 35 34 2c 20 31 30 35 39 2c 20 31 30  , 1054, 1059, 10
205c9 36 30 2c 20 31 30 36 34 2c 0a 20 2f 2a 20 20 20  60, 1064,. /*   
205ca 32 33 30 20 2a 2f 20 20 31 30 36 31 2c 20 31 31  230 */  1061, 11
205cb 35 39 2c 20 31 30 35 35 2c 20 31 30 37 32 2c 20  59, 1055, 1072, 
205cc 31 30 37 32 2c 20 31 30 39 32 2c 20 31 30 37 36  1072, 1092, 1076
205cd 2c 20 31 30 39 32 2c 20 31 30 37 36 2c 20 31 30  , 1092, 1076, 10
205ce 39 34 2c 0a 20 2f 2a 20 20 20 32 34 30 20 2a 2f  94,. /*   240 */
205cf 20 20 31 30 39 34 2c 20 31 31 35 33 2c 20 31 30    1094, 1153, 10
205d0 39 34 2c 20 31 30 39 38 2c 20 31 30 39 34 2c 20  94, 1098, 1094, 
205d1 31 31 38 31 2c 20 31 31 31 37 2c 20 31 31 35 39  1181, 1117, 1159
205d2 2c 20 31 31 31 37 2c 20 31 31 35 39 2c 0a 20 2f  , 1117, 1159,. /
205d3 2a 20 20 20 32 35 30 20 2a 2f 20 20 31 31 35 33  *   250 */  1153
205d4 2c 20 31 30 39 34 2c 20 31 30 39 34 2c 20 31 30  , 1094, 1094, 10
205d5 39 34 2c 20 31 31 38 31 2c 20 31 32 30 32 2c 20  94, 1181, 1202, 
205d6 31 30 37 32 2c 20 31 32 30 32 2c 20 31 30 37 32  1072, 1202, 1072
205d7 2c 20 31 32 30 32 2c 0a 20 2f 2a 20 20 20 32 36  , 1202,. /*   26
205d8 30 20 2a 2f 20 20 31 30 37 32 2c 20 31 30 37 32  0 */  1072, 1072
205d9 2c 20 31 31 38 33 2c 20 31 30 39 37 2c 20 31 32  , 1183, 1097, 12
205da 30 32 2c 20 31 30 37 32 2c 20 31 31 36 34 2c 20  02, 1072, 1164, 
205db 31 31 36 34 2c 20 31 32 31 30 2c 20 31 30 35 34  1164, 1210, 1054
205dc 2c 0a 20 2f 2a 20 20 20 32 37 30 20 2a 2f 20 20  ,. /*   270 */  
205dd 31 30 37 32 2c 20 31 32 32 34 2c 20 31 32 32 34  1072, 1224, 1224
205de 2c 20 31 32 32 34 2c 20 31 32 32 34 2c 20 31 30  , 1224, 1224, 10
205df 35 34 2c 20 31 31 36 34 2c 20 31 32 31 30 2c 20  54, 1164, 1210, 
205e0 31 30 37 32 2c 20 31 32 31 32 2c 0a 20 2f 2a 20  1072, 1212,. /* 
205e1 20 20 32 38 30 20 2a 2f 20 20 31 32 31 32 2c 20    280 */  1212, 
205e2 31 30 37 32 2c 20 31 30 37 32 2c 20 31 31 30 39  1072, 1072, 1109
205e3 2c 20 20 2d 36 32 2c 20 20 2d 36 32 2c 20 20 2d  ,  -62,  -62,  -
205e4 36 32 2c 20 20 2d 36 32 2c 20 20 2d 36 32 2c 20  62,  -62,  -62, 
205e5 20 2d 36 32 2c 0a 20 2f 2a 20 20 20 32 39 30 20   -62,. /*   290 
205e6 2a 2f 20 20 20 35 32 35 2c 20 20 36 38 34 2c 20  */   525,  684, 
205e7 20 37 32 37 2c 20 20 31 36 38 2c 20 20 38 39 34   727,  168,  894
205e8 2c 20 20 35 35 36 2c 20 20 35 35 35 2c 20 20 39  ,  556,  555,  9
205e9 33 38 2c 20 20 39 34 34 2c 20 20 39 34 39 2c 0a  38,  944,  949,.
205ea 20 2f 2a 20 20 20 33 30 30 20 2a 2f 20 20 20 39   /*   300 */   9
205eb 31 32 2c 20 31 30 35 38 2c 20 31 30 37 33 2c 20  12, 1058, 1073, 
205ec 31 30 38 37 2c 20 31 30 39 31 2c 20 31 31 30 31  1087, 1091, 1101
205ed 2c 20 31 31 30 34 2c 20 31 31 30 37 2c 20 31 30  , 1104, 1107, 10
205ee 33 30 2c 20 31 31 30 38 2c 0a 20 2f 2a 20 20 20  30, 1108,. /*   
205ef 33 31 30 20 2a 2f 20 20 31 30 37 37 2c 20 31 32  310 */  1077, 12
205f0 35 32 2c 20 31 32 33 36 2c 20 31 31 34 39 2c 20  52, 1236, 1149, 
205f1 31 31 35 30 2c 20 31 31 35 32 2c 20 31 31 35 34  1150, 1152, 1154
205f2 2c 20 31 31 37 31 2c 20 31 31 35 38 2c 20 31 32  , 1171, 1158, 12
205f3 34 38 2c 0a 20 2f 2a 20 20 20 33 32 30 20 2a 2f  48,. /*   320 */
205f4 20 20 31 32 35 30 2c 20 31 32 35 31 2c 20 31 32    1250, 1251, 12
205f5 34 39 2c 20 31 32 36 31 2c 20 31 32 35 34 2c 20  49, 1261, 1254, 
205f6 31 32 35 35 2c 20 31 32 35 36 2c 20 31 32 35 39  1255, 1256, 1259
205f7 2c 20 31 32 36 30 2c 20 31 32 36 33 2c 0a 20 2f  , 1260, 1263,. /
205f8 2a 20 20 20 33 33 30 20 2a 2f 20 20 31 31 35 37  *   330 */  1157
205f9 2c 20 31 32 36 34 2c 20 31 31 36 35 2c 20 31 32  , 1264, 1165, 12
205fa 36 33 2c 20 31 31 36 32 2c 20 31 32 36 32 2c 20  63, 1162, 1262, 
205fb 31 32 36 37 2c 20 31 31 37 36 2c 20 31 32 36 39  1267, 1176, 1269
205fc 2c 20 31 32 37 30 2c 0a 20 2f 2a 20 20 20 33 34  , 1270,. /*   34
205fd 30 20 2a 2f 20 20 31 32 36 35 2c 20 31 32 33 39  0 */  1265, 1239
205fe 2c 20 31 32 37 32 2c 20 31 32 35 37 2c 20 31 32  , 1272, 1257, 12
205ff 37 34 2c 20 31 32 37 35 2c 20 31 32 37 37 2c 20  74, 1275, 1277, 
20600 31 32 37 39 2c 20 31 32 35 38 2c 20 31 32 38 30  1279, 1258, 1280
20601 2c 0a 20 2f 2a 20 20 20 33 35 30 20 2a 2f 20 20  ,. /*   350 */  
20602 31 31 39 38 2c 20 31 31 39 37 2c 20 31 32 38 37  1198, 1197, 1287
20603 2c 20 31 32 38 38 2c 20 31 32 38 33 2c 20 31 32  , 1288, 1283, 12
20604 30 38 2c 20 31 32 37 31 2c 20 31 32 36 36 2c 20  08, 1271, 1266, 
20605 31 32 36 38 2c 20 31 32 38 36 2c 0a 20 2f 2a 20  1268, 1286,. /* 
20606 20 20 33 36 30 20 2a 2f 20 20 31 32 37 33 2c 20    360 */  1273, 
20607 31 31 37 37 2c 20 31 32 31 31 2c 20 31 32 39 32  1177, 1211, 1292
20608 2c 20 31 33 30 37 2c 20 31 33 31 33 2c 20 31 32  , 1307, 1313, 12
20609 31 33 2c 20 31 32 35 33 2c 20 31 32 37 36 2c 20  13, 1253, 1276, 
2060a 31 31 39 34 2c 0a 20 2f 2a 20 20 20 33 37 30 20  1194,. /*   370 
2060b 2a 2f 20 20 31 33 30 30 2c 20 31 32 30 37 2c 20  */  1300, 1207, 
2060c 31 33 30 38 2c 20 31 32 30 39 2c 20 31 33 30 36  1308, 1209, 1306
2060d 2c 20 31 32 31 34 2c 20 31 32 32 33 2c 20 31 32  , 1214, 1223, 12
2060e 30 34 2c 20 31 33 30 39 2c 20 31 32 30 36 2c 0a  04, 1309, 1206,.
2060f 20 2f 2a 20 20 20 33 38 30 20 2a 2f 20 20 31 33   /*   380 */  13
20610 31 30 2c 20 31 33 31 37 2c 20 31 32 37 38 2c 20  10, 1317, 1278, 
20611 31 31 39 39 2c 20 31 31 39 36 2c 20 31 33 31 34  1199, 1196, 1314
20612 2c 20 31 33 33 33 2c 20 31 33 33 34 2c 0a 7d 3b  , 1333, 1334,.};
20613 0a 23 64 65 66 69 6e 65 20 59 59 5f 52 45 44 55  .#define YY_REDU
20614 43 45 5f 55 53 45 5f 44 46 4c 54 20 28 2d 31 36  CE_USE_DFLT (-16
20615 35 29 0a 23 64 65 66 69 6e 65 20 59 59 5f 52 45  5).#define YY_RE
20616 44 55 43 45 5f 4d 41 58 20 32 38 39 0a 73 74 61  DUCE_MAX 289.sta
20617 74 69 63 20 63 6f 6e 73 74 20 73 68 6f 72 74 20  tic const short 
20618 79 79 5f 72 65 64 75 63 65 5f 6f 66 73 74 5b 5d  yy_reduce_ofst[]
20619 20 3d 20 7b 0a 20 2f 2a 20 20 20 20 20 30 20 2a   = {. /*     0 *
2061a 2f 20 20 2d 31 33 38 2c 20 20 32 37 37 2c 20 20  /  -138,  277,  
2061b 35 34 36 2c 20 20 31 33 37 2c 20 20 34 30 31 2c  546,  137,  401,
2061c 20 20 2d 32 31 2c 20 20 20 34 34 2c 20 20 20 33    -21,   44,   3
2061d 36 2c 20 20 20 33 38 2c 20 20 32 34 32 2c 0a 20  6,   38,  242,. 
2061e 2f 2a 20 20 20 20 31 30 20 2a 2f 20 20 2d 31 34  /*    10 */  -14
2061f 31 2c 20 20 31 39 31 2c 20 20 20 39 31 2c 20 20  1,  191,   91,  
20620 32 36 39 2c 20 20 33 34 33 2c 20 20 33 34 35 2c  269,  343,  345,
20621 20 2d 31 32 36 2c 20 20 35 38 39 2c 20 20 33 33   -126,  589,  33
20622 38 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20 20 20  8,  150,. /*    
20623 32 30 20 2a 2f 20 20 20 31 34 37 2c 20 20 2d 31  20 */   147,  -1
20624 33 2c 20 20 32 31 33 2c 20 20 34 31 32 2c 20 20  3,  213,  412,  
20625 34 31 32 2c 20 20 34 31 32 2c 20 20 34 31 32 2c  412,  412,  412,
20626 20 20 34 31 32 2c 20 20 34 31 32 2c 20 20 34 31    412,  412,  41
20627 32 2c 0a 20 2f 2a 20 20 20 20 33 30 20 2a 2f 20  2,. /*    30 */ 
20628 20 20 34 31 32 2c 20 20 34 31 32 2c 20 20 34 31    412,  412,  41
20629 32 2c 20 20 34 31 32 2c 20 20 34 31 32 2c 20 20  2,  412,  412,  
2062a 34 31 32 2c 20 20 34 31 32 2c 20 20 34 31 32 2c  412,  412,  412,
2062b 20 20 34 31 32 2c 20 20 34 31 32 2c 0a 20 2f 2a    412,  412,. /*
2062c 20 20 20 20 34 30 20 2a 2f 20 20 20 34 31 32 2c      40 */   412,
2062d 20 20 34 31 32 2c 20 20 34 31 32 2c 20 20 34 31    412,  412,  41
2062e 32 2c 20 20 34 31 32 2c 20 20 34 31 32 2c 20 20  2,  412,  412,  
2062f 34 31 32 2c 20 20 34 31 32 2c 20 20 34 31 32 2c  412,  412,  412,
20630 20 20 34 31 32 2c 0a 20 2f 2a 20 20 20 20 35 30    412,. /*    50
20631 20 2a 2f 20 20 20 34 31 32 2c 20 20 34 31 32 2c   */   412,  412,
20632 20 20 34 31 32 2c 20 20 34 31 32 2c 20 20 34 31    412,  412,  41
20633 32 2c 20 20 32 31 31 2c 20 20 36 39 38 2c 20 20  2,  211,  698,  
20634 37 31 34 2c 20 20 37 31 36 2c 20 20 37 32 32 2c  714,  716,  722,
20635 0a 20 2f 2a 20 20 20 20 36 30 20 2a 2f 20 20 20  . /*    60 */   
20636 37 32 34 2c 20 20 37 32 38 2c 20 20 37 34 38 2c  724,  728,  748,
20637 20 20 37 35 33 2c 20 20 37 35 35 2c 20 20 37 35    753,  755,  75
20638 37 2c 20 20 37 36 32 2c 20 20 37 36 39 2c 20 20  7,  762,  769,  
20639 37 39 34 2c 20 20 38 30 35 2c 0a 20 2f 2a 20 20  794,  805,. /*  
2063a 20 20 37 30 20 2a 2f 20 20 20 38 30 38 2c 20 20    70 */   808,  
2063b 38 34 36 2c 20 20 38 35 35 2c 20 20 38 35 38 2c  846,  855,  858,
2063c 20 20 38 36 31 2c 20 20 38 37 35 2c 20 20 38 38    861,  875,  88
2063d 32 2c 20 20 38 39 33 2c 20 20 38 39 36 2c 20 20  2,  893,  896,  
2063e 38 39 39 2c 0a 20 2f 2a 20 20 20 20 38 30 20 2a  899,. /*    80 *
2063f 2f 20 20 20 39 30 31 2c 20 20 39 30 33 2c 20 20  /   901,  903,  
20640 39 30 36 2c 20 20 39 32 30 2c 20 20 39 33 30 2c  906,  920,  930,
20641 20 20 39 33 32 2c 20 20 39 33 34 2c 20 20 39 33    932,  934,  93
20642 36 2c 20 20 39 34 35 2c 20 20 39 34 37 2c 0a 20  6,  945,  947,. 
20643 2f 2a 20 20 20 20 39 30 20 2a 2f 20 20 20 39 36  /*    90 */   96
20644 33 2c 20 20 39 36 35 2c 20 20 39 37 31 2c 20 20  3,  965,  971,  
20645 39 37 35 2c 20 20 39 37 38 2c 20 20 34 31 32 2c  975,  978,  412,
20646 20 20 34 31 32 2c 20 20 34 31 32 2c 20 20 34 31    412,  412,  41
20647 32 2c 20 20 20 32 30 2c 0a 20 2f 2a 20 20 20 31  2,   20,. /*   1
20648 30 30 20 2a 2f 20 20 20 34 31 32 2c 20 20 34 31  00 */   412,  41
20649 32 2c 20 20 20 32 33 2c 20 20 20 33 34 2c 20 20  2,   23,   34,  
2064a 33 33 34 2c 20 20 34 37 35 2c 20 20 35 35 32 2c  334,  475,  552,
2064b 20 20 35 39 33 2c 20 20 35 39 34 2c 20 20 35 38    593,  594,  58
2064c 35 2c 0a 20 2f 2a 20 20 20 31 31 30 20 2a 2f 20  5,. /*   110 */ 
2064d 20 20 32 31 32 2c 20 20 34 31 32 2c 20 20 32 38    212,  412,  28
2064e 39 2c 20 20 34 31 32 2c 20 20 34 31 32 2c 20 20  9,  412,  412,  
2064f 34 31 32 2c 20 20 31 33 35 2c 20 2d 31 36 34 2c  412,  135, -164,
20650 20 2d 31 31 35 2c 20 20 31 36 34 2c 0a 20 2f 2a   -115,  164,. /*
20651 20 20 20 31 32 30 20 2a 2f 20 20 20 34 30 37 2c     120 */   407,
20652 20 20 34 30 37 2c 20 20 33 35 30 2c 20 20 31 34    407,  350,  14
20653 31 2c 20 20 34 33 36 2c 20 20 31 36 33 2c 20 20  1,  436,  163,  
20654 35 39 36 2c 20 20 2d 39 30 2c 20 20 37 36 33 2c  596,  -90,  763,
20655 20 20 32 31 38 2c 0a 20 2f 2a 20 20 20 31 33 30    218,. /*   130
20656 20 2a 2f 20 20 20 37 36 35 2c 20 20 34 33 38 2c   */   765,  438,
20657 20 20 35 38 36 2c 20 20 35 39 32 2c 20 20 35 39    586,  592,  59
20658 35 2c 20 20 37 31 35 2c 20 20 37 31 38 2c 20 20  5,  715,  718,  
20659 34 30 38 2c 20 20 37 32 33 2c 20 20 33 38 30 2c  408,  723,  380,
2065a 0a 20 2f 2a 20 20 20 31 34 30 20 2a 2f 20 20 20  . /*   140 */   
2065b 36 33 34 2c 20 20 36 37 37 2c 20 20 37 38 37 2c  634,  677,  787,
2065c 20 20 37 39 38 2c 20 20 31 34 34 2c 20 20 35 32    798,  144,  52
2065d 39 2c 20 20 35 38 38 2c 20 20 2d 31 34 2c 20 20  9,  588,  -14,  
2065e 20 20 30 2c 20 20 20 31 37 2c 0a 20 2f 2a 20 20    0,   17,. /*  
2065f 20 31 35 30 20 2a 2f 20 20 20 32 34 34 2c 20 20   150 */   244,  
20660 31 35 35 2c 20 20 32 39 38 2c 20 20 31 35 35 2c  155,  298,  155,
20661 20 20 31 35 35 2c 20 20 34 31 38 2c 20 20 33 37    155,  418,  37
20662 32 2c 20 20 34 37 37 2c 20 20 34 39 30 2c 20 20  2,  477,  490,  
20663 34 39 34 2c 0a 20 2f 2a 20 20 20 31 36 30 20 2a  494,. /*   160 *
20664 2f 20 20 20 35 30 39 2c 20 20 35 32 36 2c 20 20  /   509,  526,  
20665 35 39 30 2c 20 20 34 36 35 2c 20 20 34 39 34 2c  590,  465,  494,
20666 20 20 37 33 30 2c 20 20 37 37 33 2c 20 20 37 34    730,  773,  74
20667 33 2c 20 20 38 33 33 2c 20 20 37 39 32 2c 0a 20  3,  833,  792,. 
20668 2f 2a 20 20 20 31 37 30 20 2a 2f 20 20 20 38 35  /*   170 */   85
20669 34 2c 20 20 38 36 30 2c 20 20 31 35 35 2c 20 20  4,  860,  155,  
2066a 37 39 32 2c 20 20 38 36 34 2c 20 20 38 38 35 2c  792,  864,  885,
2066b 20 20 38 34 38 2c 20 20 38 35 39 2c 20 20 38 36    848,  859,  86
2066c 33 2c 20 20 38 39 31 2c 0a 20 2f 2a 20 20 20 31  3,  891,. /*   1
2066d 38 30 20 2a 2f 20 20 20 39 30 37 2c 20 20 31 35  80 */   907,  15
2066e 35 2c 20 20 39 31 34 2c 20 20 39 31 37 2c 20 20  5,  914,  917,  
2066f 31 35 35 2c 20 20 39 32 37 2c 20 20 38 35 32 2c  155,  927,  852,
20670 20 20 38 38 36 2c 20 20 39 34 31 2c 20 20 39 36    886,  941,  96
20671 31 2c 0a 20 2f 2a 20 20 20 31 39 30 20 2a 2f 20  1,. /*   190 */ 
20672 20 20 31 35 35 2c 20 20 39 38 34 2c 20 20 39 39    155,  984,  99
20673 30 2c 20 20 39 39 31 2c 20 20 31 35 35 2c 20 20  0,  991,  155,  
20674 39 39 32 2c 20 20 39 39 35 2c 20 20 39 39 36 2c  992,  995,  996,
20675 20 20 31 35 35 2c 20 20 31 35 35 2c 0a 20 2f 2a    155,  155,. /*
20676 20 20 20 32 30 30 20 2a 2f 20 20 20 39 39 39 2c     200 */   999,
20677 20 31 30 30 32 2c 20 31 30 30 33 2c 20 31 30 30   1002, 1003, 100
20678 34 2c 20 31 30 30 35 2c 20 31 30 30 36 2c 20 20  4, 1005, 1006,  
20679 39 36 32 2c 20 20 39 36 34 2c 20 20 39 32 35 2c  962,  964,  925,
2067a 20 20 39 32 39 2c 0a 20 2f 2a 20 20 20 32 31 30    929,. /*   210
2067b 20 2a 2f 20 20 20 39 33 33 2c 20 20 39 38 37 2c   */   933,  987,
2067c 20 20 39 38 38 2c 20 20 39 38 39 2c 20 20 39 38    988,  989,  98
2067d 35 2c 20 20 39 39 33 2c 20 20 39 39 37 2c 20 20  5,  993,  997,  
2067e 39 39 38 2c 20 20 39 36 39 2c 20 20 39 37 30 2c  998,  969,  970,
2067f 0a 20 2f 2a 20 20 20 32 32 30 20 2a 2f 20 20 31  . /*   220 */  1
20680 30 32 31 2c 20 31 30 32 32 2c 20 31 30 32 34 2c  021, 1022, 1024,
20681 20 31 30 32 36 2c 20 31 30 32 30 2c 20 31 30 30   1026, 1020, 100
20682 30 2c 20 20 39 38 32 2c 20 20 39 38 33 2c 20 31  0,  982,  983, 1
20683 30 30 31 2c 20 31 30 30 37 2c 0a 20 2f 2a 20 20  001, 1007,. /*  
20684 20 32 33 30 20 2a 2f 20 20 31 30 30 38 2c 20 31   230 */  1008, 1
20685 30 32 33 2c 20 20 39 36 36 2c 20 31 30 33 34 2c  023,  966, 1034,
20686 20 31 30 33 35 2c 20 31 30 30 39 2c 20 31 30 31   1035, 1009, 101
20687 30 2c 20 31 30 31 32 2c 20 31 30 31 33 2c 20 31  0, 1012, 1013, 1
20688 30 32 35 2c 0a 20 2f 2a 20 20 20 32 34 30 20 2a  025,. /*   240 *
20689 2f 20 20 31 30 32 37 2c 20 31 30 31 35 2c 20 31  /  1027, 1015, 1
2068a 30 32 38 2c 20 31 30 32 39 2c 20 31 30 33 32 2c  028, 1029, 1032,
2068b 20 31 30 33 31 2c 20 20 39 38 31 2c 20 31 30 34   1031,  981, 104
2068c 38 2c 20 31 30 31 31 2c 20 31 30 34 39 2c 0a 20  8, 1011, 1049,. 
2068d 2f 2a 20 20 20 32 35 30 20 2a 2f 20 20 31 30 33  /*   250 */  103
2068e 33 2c 20 31 30 33 39 2c 20 31 30 34 30 2c 20 31  3, 1039, 1040, 1
2068f 30 34 33 2c 20 31 30 34 35 2c 20 31 30 36 39 2c  043, 1045, 1069,
20690 20 31 30 37 30 2c 20 31 30 37 34 2c 20 31 30 37   1070, 1074, 107
20691 31 2c 20 31 30 37 35 2c 0a 20 2f 2a 20 20 20 32  1, 1075,. /*   2
20692 36 30 20 2a 2f 20 20 31 30 37 38 2c 20 31 30 37  60 */  1078, 107
20693 39 2c 20 31 30 31 34 2c 20 31 30 31 36 2c 20 31  9, 1014, 1016, 1
20694 30 38 31 2c 20 31 30 38 32 2c 20 31 30 34 34 2c  081, 1082, 1044,
20695 20 31 30 35 31 2c 20 31 30 31 39 2c 20 31 30 34   1051, 1019, 104
20696 32 2c 0a 20 2f 2a 20 20 20 32 37 30 20 2a 2f 20  2,. /*   270 */ 
20697 20 31 30 38 36 2c 20 31 30 35 33 2c 20 31 30 35   1086, 1053, 105
20698 36 2c 20 31 30 35 37 2c 20 31 30 36 32 2c 20 31  6, 1057, 1062, 1
20699 30 34 37 2c 20 31 30 36 33 2c 20 31 30 33 36 2c  047, 1063, 1036,
2069a 20 31 30 39 30 2c 20 31 30 31 37 2c 0a 20 2f 2a   1090, 1017,. /*
2069b 20 20 20 32 38 30 20 2a 2f 20 20 31 30 31 38 2c     280 */  1018,
2069c 20 31 31 30 30 2c 20 31 31 30 35 2c 20 31 30 33   1100, 1105, 103
2069d 37 2c 20 31 30 39 39 2c 20 31 30 38 34 2c 20 31  7, 1099, 1084, 1
2069e 30 38 35 2c 20 31 30 33 38 2c 20 31 30 34 31 2c  085, 1038, 1041,
2069f 20 31 30 34 36 2c 0a 7d 3b 0a 73 74 61 74 69 63   1046,.};.static
206a0 20 63 6f 6e 73 74 20 59 59 41 43 54 49 4f 4e 54   const YYACTIONT
206a1 59 50 45 20 79 79 5f 64 65 66 61 75 6c 74 5b 5d  YPE yy_default[]
206a2 20 3d 20 7b 0a 20 2f 2a 20 20 20 20 20 30 20 2a   = {. /*     0 *
206a3 2f 20 20 20 35 39 32 2c 20 20 38 31 36 2c 20 20  /   592,  816,  
206a4 38 39 37 2c 20 20 37 30 37 2c 20 20 38 39 37 2c  897,  707,  897,
206a5 20 20 38 31 36 2c 20 20 38 39 37 2c 20 20 38 39    816,  897,  89
206a6 37 2c 20 20 38 34 33 2c 20 20 37 31 31 2c 0a 20  7,  843,  711,. 
206a7 2f 2a 20 20 20 20 31 30 20 2a 2f 20 20 20 38 37  /*    10 */   87
206a8 32 2c 20 20 38 31 34 2c 20 20 38 39 37 2c 20 20  2,  814,  897,  
206a9 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
206aa 20 20 37 38 39 2c 20 20 38 39 37 2c 20 20 38 34    789,  897,  84
206ab 33 2c 20 20 38 39 37 2c 0a 20 2f 2a 20 20 20 20  3,  897,. /*    
206ac 32 30 20 2a 2f 20 20 20 36 32 33 2c 20 20 38 34  20 */   623,  84
206ad 33 2c 20 20 38 34 33 2c 20 20 37 34 30 2c 20 20  3,  843,  740,  
206ae 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
206af 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
206b0 37 2c 0a 20 2f 2a 20 20 20 20 33 30 20 2a 2f 20  7,. /*    30 */ 
206b1 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 37 34    897,  897,  74
206b2 31 2c 20 20 38 39 37 2c 20 20 38 31 38 2c 20 20  1,  897,  818,  
206b3 38 31 33 2c 20 20 38 30 39 2c 20 20 38 31 31 2c  813,  809,  811,
206b4 20 20 38 31 30 2c 20 20 38 31 37 2c 0a 20 2f 2a    810,  817,. /*
206b5 20 20 20 20 34 30 20 2a 2f 20 20 20 37 34 32 2c      40 */   742,
206b6 20 20 37 33 31 2c 20 20 37 33 38 2c 20 20 37 34    731,  738,  74
206b7 35 2c 20 20 37 32 33 2c 20 20 38 35 36 2c 20 20  5,  723,  856,  
206b8 37 34 37 2c 20 20 37 34 38 2c 20 20 37 35 34 2c  747,  748,  754,
206b9 20 20 37 35 35 2c 0a 20 2f 2a 20 20 20 20 35 30    755,. /*    50
206ba 20 2a 2f 20 20 20 38 37 33 2c 20 20 38 37 31 2c   */   873,  871,
206bb 20 20 37 37 37 2c 20 20 37 37 36 2c 20 20 37 39    777,  776,  79
206bc 35 2c 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  5,  897,  897,  
206bd 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
206be 0a 20 2f 2a 20 20 20 20 36 30 20 2a 2f 20 20 20  . /*    60 */   
206bf 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
206c0 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
206c1 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  7,  897,  897,  
206c2 38 39 37 2c 20 20 38 39 37 2c 0a 20 2f 2a 20 20  897,  897,. /*  
206c3 20 20 37 30 20 2a 2f 20 20 20 38 39 37 2c 20 20    70 */   897,  
206c4 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
206c5 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
206c6 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  7,  897,  897,  
206c7 38 39 37 2c 0a 20 2f 2a 20 20 20 20 38 30 20 2a  897,. /*    80 *
206c8 2f 20 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  /   897,  897,  
206c9 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
206ca 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
206cb 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c 0a 20  7,  897,  897,. 
206cc 2f 2a 20 20 20 20 39 30 20 2a 2f 20 20 20 38 39  /*    90 */   89
206cd 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  7,  897,  897,  
206ce 38 39 37 2c 20 20 38 39 37 2c 20 20 37 37 39 2c  897,  897,  779,
206cf 20 20 38 30 30 2c 20 20 37 37 38 2c 20 20 37 38    800,  778,  78
206d0 38 2c 20 20 36 31 36 2c 0a 20 2f 2a 20 20 20 31  8,  616,. /*   1
206d1 30 30 20 2a 2f 20 20 20 37 38 30 2c 20 20 37 38  00 */   780,  78
206d2 31 2c 20 20 36 37 36 2c 20 20 36 31 31 2c 20 20  1,  676,  611,  
206d3 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
206d4 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
206d5 37 2c 0a 20 2f 2a 20 20 20 31 31 30 20 2a 2f 20  7,. /*   110 */ 
206d6 20 20 38 39 37 2c 20 20 37 38 32 2c 20 20 38 39    897,  782,  89
206d7 37 2c 20 20 37 38 33 2c 20 20 37 39 36 2c 20 20  7,  783,  796,  
206d8 37 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  797,  897,  897,
206d9 20 20 38 39 37 2c 20 20 38 39 37 2c 0a 20 2f 2a    897,  897,. /*
206da 20 20 20 31 32 30 20 2a 2f 20 20 20 38 39 37 2c     120 */   897,
206db 20 20 38 39 37 2c 20 20 35 39 32 2c 20 20 37 30    897,  592,  70
206dc 37 2c 20 20 38 39 37 2c 20 20 37 30 37 2c 20 20  7,  897,  707,  
206dd 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
206de 20 20 38 39 37 2c 0a 20 2f 2a 20 20 20 31 33 30    897,. /*   130
206df 20 2a 2f 20 20 20 38 39 37 2c 20 20 38 39 37 2c   */   897,  897,
206e0 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
206e1 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  7,  897,  897,  
206e2 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
206e3 0a 20 2f 2a 20 20 20 31 34 30 20 2a 2f 20 20 20  . /*   140 */   
206e4 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
206e5 20 20 38 39 37 2c 20 20 37 30 31 2c 20 20 37 31    897,  701,  71
206e6 31 2c 20 20 38 39 30 2c 20 20 38 39 37 2c 20 20  1,  890,  897,  
206e7 38 39 37 2c 20 20 36 36 37 2c 0a 20 2f 2a 20 20  897,  667,. /*  
206e8 20 31 35 30 20 2a 2f 20 20 20 38 39 37 2c 20 20   150 */   897,  
206e9 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
206ea 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
206eb 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  7,  897,  897,  
206ec 38 39 37 2c 0a 20 2f 2a 20 20 20 31 36 30 20 2a  897,. /*   160 *
206ed 2f 20 20 20 35 39 39 2c 20 20 35 39 37 2c 20 20  /   599,  597,  
206ee 38 39 37 2c 20 20 36 39 39 2c 20 20 38 39 37 2c  897,  699,  897,
206ef 20 20 38 39 37 2c 20 20 36 32 35 2c 20 20 38 39    897,  625,  89
206f0 37 2c 20 20 38 39 37 2c 20 20 37 30 39 2c 0a 20  7,  897,  709,. 
206f1 2f 2a 20 20 20 31 37 30 20 2a 2f 20 20 20 38 39  /*   170 */   89
206f2 37 2c 20 20 38 39 37 2c 20 20 37 31 34 2c 20 20  7,  897,  714,  
206f3 37 31 35 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  715,  897,  897,
206f4 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
206f5 37 2c 20 20 38 39 37 2c 0a 20 2f 2a 20 20 20 31  7,  897,. /*   1
206f6 38 30 20 2a 2f 20 20 20 38 39 37 2c 20 20 36 31  80 */   897,  61
206f7 33 2c 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  3,  897,  897,  
206f8 36 38 38 2c 20 20 38 39 37 2c 20 20 38 34 39 2c  688,  897,  849,
206f9 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
206fa 37 2c 0a 20 2f 2a 20 20 20 31 39 30 20 2a 2f 20  7,. /*   190 */ 
206fb 20 20 38 36 33 2c 20 20 38 39 37 2c 20 20 38 39    863,  897,  89
206fc 37 2c 20 20 38 39 37 2c 20 20 38 36 31 2c 20 20  7,  897,  861,  
206fd 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
206fe 20 20 36 39 30 2c 20 20 37 35 30 2c 0a 20 2f 2a    690,  750,. /*
206ff 20 20 20 32 30 30 20 2a 2f 20 20 20 38 33 30 2c     200 */   830,
20700 20 20 38 39 37 2c 20 20 38 37 36 2c 20 20 38 37    897,  876,  87
20701 38 2c 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  8,  897,  897,  
20702 36 39 39 2c 20 20 37 30 38 2c 20 20 38 39 37 2c  699,  708,  897,
20703 20 20 38 39 37 2c 0a 20 2f 2a 20 20 20 32 31 30    897,. /*   210
20704 20 2a 2f 20 20 20 38 31 32 2c 20 20 37 33 34 2c   */   812,  734,
20705 20 20 37 33 34 2c 20 20 37 33 34 2c 20 20 36 34    734,  734,  64
20706 36 2c 20 20 37 33 34 2c 20 20 37 33 34 2c 20 20  6,  734,  734,  
20707 36 34 39 2c 20 20 37 34 34 2c 20 20 37 34 34 2c  649,  744,  744,
20708 0a 20 2f 2a 20 20 20 32 32 30 20 2a 2f 20 20 20  . /*   220 */   
20709 35 39 36 2c 20 20 35 39 36 2c 20 20 35 39 36 2c  596,  596,  596,
2070a 20 20 35 39 36 2c 20 20 36 36 36 2c 20 20 38 39    596,  666,  89
2070b 37 2c 20 20 37 34 34 2c 20 20 37 33 35 2c 20 20  7,  744,  735,  
2070c 37 33 37 2c 20 20 37 32 37 2c 0a 20 2f 2a 20 20  737,  727,. /*  
2070d 20 32 33 30 20 2a 2f 20 20 20 37 33 39 2c 20 20   230 */   739,  
2070e 38 39 37 2c 20 20 38 39 37 2c 20 20 37 31 36 2c  897,  897,  716,
2070f 20 20 37 31 36 2c 20 20 37 32 34 2c 20 20 37 32    716,  724,  72
20710 36 2c 20 20 37 32 34 2c 20 20 37 32 36 2c 20 20  6,  724,  726,  
20711 36 37 38 2c 0a 20 2f 2a 20 20 20 32 34 30 20 2a  678,. /*   240 *
20712 2f 20 20 20 36 37 38 2c 20 20 36 36 33 2c 20 20  /   678,  663,  
20713 36 37 38 2c 20 20 36 34 39 2c 20 20 36 37 38 2c  678,  649,  678,
20714 20 20 38 32 32 2c 20 20 38 32 37 2c 20 20 38 39    822,  827,  89
20715 37 2c 20 20 38 32 37 2c 20 20 38 39 37 2c 0a 20  7,  827,  897,. 
20716 2f 2a 20 20 20 32 35 30 20 2a 2f 20 20 20 36 36  /*   250 */   66
20717 33 2c 20 20 36 37 38 2c 20 20 36 37 38 2c 20 20  3,  678,  678,  
20718 36 37 38 2c 20 20 38 32 32 2c 20 20 36 30 38 2c  678,  822,  608,
20719 20 20 37 31 36 2c 20 20 36 30 38 2c 20 20 37 31    716,  608,  71
2071a 36 2c 20 20 36 30 38 2c 0a 20 2f 2a 20 20 20 32  6,  608,. /*   2
2071b 36 30 20 2a 2f 20 20 20 37 31 36 2c 20 20 37 31  60 */   716,  71
2071c 36 2c 20 20 38 35 33 2c 20 20 38 35 35 2c 20 20  6,  853,  855,  
2071d 36 30 38 2c 20 20 37 31 36 2c 20 20 36 38 30 2c  608,  716,  680,
2071e 20 20 36 38 30 2c 20 20 37 35 36 2c 20 20 37 34    680,  756,  74
2071f 34 2c 0a 20 2f 2a 20 20 20 32 37 30 20 2a 2f 20  4,. /*   270 */ 
20720 20 20 37 31 36 2c 20 20 36 38 37 2c 20 20 36 38    716,  687,  68
20721 37 2c 20 20 36 38 37 2c 20 20 36 38 37 2c 20 20  7,  687,  687,  
20722 37 34 34 2c 20 20 36 38 30 2c 20 20 37 35 36 2c  744,  680,  756,
20723 20 20 37 31 36 2c 20 20 38 37 35 2c 0a 20 2f 2a    716,  875,. /*
20724 20 20 20 32 38 30 20 2a 2f 20 20 20 38 37 35 2c     280 */   875,
20725 20 20 37 31 36 2c 20 20 37 31 36 2c 20 20 38 38    716,  716,  88
20726 33 2c 20 20 36 33 33 2c 20 20 36 35 31 2c 20 20  3,  633,  651,  
20727 36 35 31 2c 20 20 38 35 38 2c 20 20 38 39 30 2c  651,  858,  890,
20728 20 20 38 39 35 2c 0a 20 2f 2a 20 20 20 32 39 30    895,. /*   290
20729 20 2a 2f 20 20 20 38 39 37 2c 20 20 38 39 37 2c   */   897,  897,
2072a 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 37 36    897,  897,  76
2072b 33 2c 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  3,  897,  897,  
2072c 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
2072d 0a 20 2f 2a 20 20 20 33 30 30 20 2a 2f 20 20 20  . /*   300 */   
2072e 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
2072f 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
20730 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  7,  897,  897,  
20731 38 33 36 2c 20 20 38 39 37 2c 0a 20 2f 2a 20 20  836,  897,. /*  
20732 20 33 31 30 20 2a 2f 20 20 20 38 39 37 2c 20 20   310 */   897,  
20733 38 39 37 2c 20 20 38 39 37 2c 20 20 37 36 38 2c  897,  897,  768,
20734 20 20 37 36 34 2c 20 20 38 39 37 2c 20 20 37 36    764,  897,  76
20735 35 2c 20 20 38 39 37 2c 20 20 36 39 33 2c 20 20  5,  897,  693,  
20736 38 39 37 2c 0a 20 2f 2a 20 20 20 33 32 30 20 2a  897,. /*   320 *
20737 2f 20 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  /   897,  897,  
20738 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
20739 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
2073a 37 2c 20 20 38 39 37 2c 20 20 38 31 35 2c 0a 20  7,  897,  815,. 
2073b 2f 2a 20 20 20 33 33 30 20 2a 2f 20 20 20 38 39  /*   330 */   89
2073c 37 2c 20 20 37 32 38 2c 20 20 38 39 37 2c 20 20  7,  728,  897,  
2073d 37 33 36 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  736,  897,  897,
2073e 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
2073f 37 2c 20 20 38 39 37 2c 0a 20 2f 2a 20 20 20 33  7,  897,. /*   3
20740 34 30 20 2a 2f 20 20 20 38 39 37 2c 20 20 38 39  40 */   897,  89
20741 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  7,  897,  897,  
20742 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
20743 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
20744 37 2c 0a 20 2f 2a 20 20 20 33 35 30 20 2a 2f 20  7,. /*   350 */ 
20745 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
20746 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  7,  897,  897,  
20747 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
20748 20 20 38 35 31 2c 20 20 38 35 32 2c 0a 20 2f 2a    851,  852,. /*
20749 20 20 20 33 36 30 20 2a 2f 20 20 20 38 39 37 2c     360 */   897,
2074a 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
2074b 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  7,  897,  897,  
2074c 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
2074d 20 20 38 39 37 2c 0a 20 2f 2a 20 20 20 33 37 30    897,. /*   370
2074e 20 2a 2f 20 20 20 38 39 37 2c 20 20 38 39 37 2c   */   897,  897,
2074f 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39    897,  897,  89
20750 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20  7,  897,  897,  
20751 38 39 37 2c 20 20 38 39 37 2c 20 20 38 39 37 2c  897,  897,  897,
20752 0a 20 2f 2a 20 20 20 33 38 30 20 2a 2f 20 20 20  . /*   380 */   
20753 38 39 37 2c 20 20 38 39 37 2c 20 20 38 38 32 2c  897,  897,  882,
20754 20 20 38 39 37 2c 20 20 38 39 37 2c 20 20 38 38    897,  897,  88
20755 35 2c 20 20 35 39 33 2c 20 20 38 39 37 2c 20 20  5,  593,  897,  
20756 35 38 37 2c 20 20 35 39 30 2c 0a 20 2f 2a 20 20  587,  590,. /*  
20757 20 33 39 30 20 2a 2f 20 20 20 35 38 39 2c 20 20   390 */   589,  
20758 35 39 31 2c 20 20 35 39 35 2c 20 20 35 39 38 2c  591,  595,  598,
20759 20 20 36 32 30 2c 20 20 36 32 31 2c 20 20 36 32    620,  621,  62
2075a 32 2c 20 20 36 30 30 2c 20 20 36 30 31 2c 20 20  2,  600,  601,  
2075b 36 30 32 2c 0a 20 2f 2a 20 20 20 34 30 30 20 2a  602,. /*   400 *
2075c 2f 20 20 20 36 30 33 2c 20 20 36 30 34 2c 20 20  /   603,  604,  
2075d 36 30 35 2c 20 20 36 30 36 2c 20 20 36 31 32 2c  605,  606,  612,
2075e 20 20 36 31 34 2c 20 20 36 33 32 2c 20 20 36 33    614,  632,  63
2075f 34 2c 20 20 36 31 38 2c 20 20 36 33 36 2c 0a 20  4,  618,  636,. 
20760 2f 2a 20 20 20 34 31 30 20 2a 2f 20 20 20 36 39  /*   410 */   69
20761 37 2c 20 20 36 39 38 2c 20 20 37 36 30 2c 20 20  7,  698,  760,  
20762 36 39 31 2c 20 20 36 39 32 2c 20 20 36 39 36 2c  691,  692,  696,
20763 20 20 37 37 31 2c 20 20 37 36 32 2c 20 20 37 36    771,  762,  76
20764 36 2c 20 20 37 36 37 2c 0a 20 2f 2a 20 20 20 34  6,  767,. /*   4
20765 32 30 20 2a 2f 20 20 20 37 36 39 2c 20 20 37 37  20 */   769,  77
20766 30 2c 20 20 37 38 34 2c 20 20 37 38 35 2c 20 20  0,  784,  785,  
20767 37 38 37 2c 20 20 37 39 33 2c 20 20 37 39 39 2c  787,  793,  799,
20768 20 20 38 30 32 2c 20 20 37 38 36 2c 20 20 37 39    802,  786,  79
20769 31 2c 0a 20 2f 2a 20 20 20 34 33 30 20 2a 2f 20  1,. /*   430 */ 
2076a 20 20 37 39 32 2c 20 20 37 39 34 2c 20 20 37 39    792,  794,  79
2076b 38 2c 20 20 38 30 31 2c 20 20 36 39 34 2c 20 20  8,  801,  694,  
2076c 36 39 35 2c 20 20 38 30 35 2c 20 20 36 31 39 2c  695,  805,  619,
2076d 20 20 36 32 36 2c 20 20 36 32 37 2c 0a 20 2f 2a    626,  627,. /*
2076e 20 20 20 34 34 30 20 2a 2f 20 20 20 36 33 30 2c     440 */   630,
2076f 20 20 36 33 31 2c 20 20 38 33 39 2c 20 20 38 34    631,  839,  84
20770 31 2c 20 20 38 34 30 2c 20 20 38 34 32 2c 20 20  1,  840,  842,  
20771 36 32 39 2c 20 20 36 32 38 2c 20 20 37 37 32 2c  629,  628,  772,
20772 20 20 37 37 35 2c 0a 20 2f 2a 20 20 20 34 35 30    775,. /*   450
20773 20 2a 2f 20 20 20 38 30 37 2c 20 20 38 30 38 2c   */   807,  808,
20774 20 20 38 36 34 2c 20 20 38 36 35 2c 20 20 38 36    864,  865,  86
20775 36 2c 20 20 38 36 37 2c 20 20 38 36 38 2c 20 20  6,  867,  868,  
20776 38 30 33 2c 20 20 37 31 37 2c 20 20 38 30 36 2c  803,  717,  806,
20777 0a 20 2f 2a 20 20 20 34 36 30 20 2a 2f 20 20 20  . /*   460 */   
20778 37 39 30 2c 20 20 37 32 39 2c 20 20 37 33 32 2c  790,  729,  732,
20779 20 20 37 33 33 2c 20 20 37 33 30 2c 20 20 37 30    733,  730,  70
2077a 30 2c 20 20 37 31 30 2c 20 20 37 31 39 2c 20 20  0,  710,  719,  
2077b 37 32 30 2c 20 20 37 32 31 2c 0a 20 2f 2a 20 20  720,  721,. /*  
2077c 20 34 37 30 20 2a 2f 20 20 20 37 32 32 2c 20 20   470 */   722,  
2077d 37 30 35 2c 20 20 37 30 36 2c 20 20 37 31 32 2c  705,  706,  712,
2077e 20 20 37 32 35 2c 20 20 37 35 38 2c 20 20 37 35    725,  758,  75
2077f 39 2c 20 20 37 31 33 2c 20 20 37 30 32 2c 20 20  9,  713,  702,  
20780 37 30 33 2c 0a 20 2f 2a 20 20 20 34 38 30 20 2a  703,. /*   480 *
20781 2f 20 20 20 37 30 34 2c 20 20 38 30 34 2c 20 20  /   704,  804,  
20782 37 36 31 2c 20 20 37 37 33 2c 20 20 37 37 34 2c  761,  773,  774,
20783 20 20 36 33 37 2c 20 20 36 33 38 2c 20 20 37 36    637,  638,  76
20784 38 2c 20 20 36 33 39 2c 20 20 36 34 30 2c 0a 20  8,  639,  640,. 
20785 2f 2a 20 20 20 34 39 30 20 2a 2f 20 20 20 36 34  /*   490 */   64
20786 31 2c 20 20 36 37 39 2c 20 20 36 38 32 2c 20 20  1,  679,  682,  
20787 36 38 33 2c 20 20 36 38 34 2c 20 20 36 34 32 2c  683,  684,  642,
20788 20 20 36 36 31 2c 20 20 36 36 34 2c 20 20 36 36    661,  664,  66
20789 35 2c 20 20 36 34 33 2c 0a 20 2f 2a 20 20 20 35  5,  643,. /*   5
2078a 30 30 20 2a 2f 20 20 20 36 35 30 2c 20 20 36 34  00 */   650,  64
2078b 34 2c 20 20 36 34 35 2c 20 20 36 35 32 2c 20 20  4,  645,  652,  
2078c 36 35 33 2c 20 20 36 35 34 2c 20 20 36 35 37 2c  653,  654,  657,
2078d 20 20 36 35 38 2c 20 20 36 35 39 2c 20 20 36 36    658,  659,  66
2078e 30 2c 0a 20 2f 2a 20 20 20 35 31 30 20 2a 2f 20  0,. /*   510 */ 
2078f 20 20 36 35 35 2c 20 20 36 35 36 2c 20 20 38 32    655,  656,  82
20790 33 2c 20 20 38 32 34 2c 20 20 38 32 38 2c 20 20  3,  824,  828,  
20791 38 32 36 2c 20 20 38 32 35 2c 20 20 36 34 37 2c  826,  825,  647,
20792 20 20 36 34 38 2c 20 20 36 36 32 2c 0a 20 2f 2a    648,  662,. /*
20793 20 20 20 35 32 30 20 2a 2f 20 20 20 36 33 35 2c     520 */   635,
20794 20 20 36 32 34 2c 20 20 36 31 37 2c 20 20 36 36    624,  617,  66
20795 38 2c 20 20 36 37 31 2c 20 20 36 37 32 2c 20 20  8,  671,  672,  
20796 36 37 33 2c 20 20 36 37 34 2c 20 20 36 37 35 2c  673,  674,  675,
20797 20 20 36 37 37 2c 0a 20 2f 2a 20 20 20 35 33 30    677,. /*   530
20798 20 2a 2f 20 20 20 36 36 39 2c 20 20 36 37 30 2c   */   669,  670,
20799 20 20 36 31 35 2c 20 20 36 30 37 2c 20 20 36 30    615,  607,  60
2079a 39 2c 20 20 37 31 38 2c 20 20 38 34 35 2c 20 20  9,  718,  845,  
2079b 38 35 34 2c 20 20 38 35 30 2c 20 20 38 34 36 2c  854,  850,  846,
2079c 0a 20 2f 2a 20 20 20 35 34 30 20 2a 2f 20 20 20  . /*   540 */   
2079d 38 34 37 2c 20 20 38 34 38 2c 20 20 36 31 30 2c  847,  848,  610,
2079e 20 20 38 31 39 2c 20 20 38 32 30 2c 20 20 36 38    819,  820,  68
2079f 31 2c 20 20 37 35 32 2c 20 20 37 35 33 2c 20 20  1,  752,  753,  
207a0 38 34 34 2c 20 20 38 35 37 2c 0a 20 2f 2a 20 20  844,  857,. /*  
207a1 20 35 35 30 20 2a 2f 20 20 20 38 35 39 2c 20 20   550 */   859,  
207a2 37 35 37 2c 20 20 38 36 30 2c 20 20 38 36 32 2c  757,  860,  862,
207a3 20 20 38 38 37 2c 20 20 36 38 35 2c 20 20 36 38    887,  685,  68
207a4 36 2c 20 20 36 38 39 2c 20 20 38 32 39 2c 20 20  6,  689,  829,  
207a5 38 36 39 2c 0a 20 2f 2a 20 20 20 35 36 30 20 2a  869,. /*   560 *
207a6 2f 20 20 20 37 34 33 2c 20 20 37 34 36 2c 20 20  /   743,  746,  
207a7 37 34 39 2c 20 20 37 35 31 2c 20 20 38 33 31 2c  749,  751,  831,
207a8 20 20 38 33 32 2c 20 20 38 33 33 2c 20 20 38 33    832,  833,  83
207a9 34 2c 20 20 38 33 37 2c 20 20 38 33 38 2c 0a 20  4,  837,  838,. 
207aa 2f 2a 20 20 20 35 37 30 20 2a 2f 20 20 20 38 33  /*   570 */   83
207ab 35 2c 20 20 38 37 30 2c 20 20 38 37 34 2c 20 20  5,  870,  874,  
207ac 38 37 37 2c 20 20 38 37 39 2c 20 20 38 38 30 2c  877,  879,  880,
207ad 20 20 38 38 31 2c 20 20 38 38 34 2c 20 20 38 38    881,  884,  88
207ae 36 2c 20 20 38 39 31 2c 0a 20 2f 2a 20 20 20 35  6,  891,. /*   5
207af 38 30 20 2a 2f 20 20 20 38 39 32 2c 20 20 38 39  80 */   892,  89
207b0 33 2c 20 20 38 39 36 2c 20 20 38 39 34 2c 20 20  3,  896,  894,  
207b1 35 39 34 2c 20 20 35 38 38 2c 0a 7d 3b 0a 23 64  594,  588,.};.#d
207b2 65 66 69 6e 65 20 59 59 5f 53 5a 5f 41 43 54 54  efine YY_SZ_ACTT
207b3 41 42 20 28 69 6e 74 29 28 73 69 7a 65 6f 66 28  AB (int)(sizeof(
207b4 79 79 5f 61 63 74 69 6f 6e 29 2f 73 69 7a 65 6f  yy_action)/sizeo
207b5 66 28 79 79 5f 61 63 74 69 6f 6e 5b 30 5d 29 29  f(yy_action[0]))
207b6 0a 0a 2f 2a 20 54 68 65 20 6e 65 78 74 20 74 61  ../* The next ta
207b7 62 6c 65 20 6d 61 70 73 20 74 6f 6b 65 6e 73 20  ble maps tokens 
207b8 69 6e 74 6f 20 66 61 6c 6c 62 61 63 6b 20 74 6f  into fallback to
207b9 6b 65 6e 73 2e 20 20 49 66 20 61 20 63 6f 6e 73  kens.  If a cons
207ba 74 72 75 63 74 0a 2a 2a 20 6c 69 6b 65 20 74 68  truct.** like th
207bb 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 20  e following:.** 
207bc 0a 2a 2a 20 20 20 20 20 20 25 66 61 6c 6c 62 61  .**      %fallba
207bd 63 6b 20 49 44 20 58 20 59 20 5a 2e 0a 2a 2a 0a  ck ID X Y Z..**.
207be 2a 2a 20 61 70 70 65 61 72 73 20 69 6e 20 74 68  ** appears in th
207bf 65 20 67 72 61 6d 6d 65 72 2c 20 74 68 65 6e 20  e grammer, then 
207c0 49 44 20 62 65 63 6f 6d 65 73 20 61 20 66 61 6c  ID becomes a fal
207c1 6c 62 61 63 6b 20 74 6f 6b 65 6e 20 66 6f 72 20  lback token for 
207c2 58 2c 20 59 2c 0a 2a 2a 20 61 6e 64 20 5a 2e 20  X, Y,.** and Z. 
207c3 20 57 68 65 6e 65 76 65 72 20 6f 6e 65 20 6f 66   Whenever one of
207c4 20 74 68 65 20 74 6f 6b 65 6e 73 20 58 2c 20 59   the tokens X, Y
207c5 2c 20 6f 72 20 5a 20 69 73 20 69 6e 70 75 74 20  , or Z is input 
207c6 74 6f 20 74 68 65 20 70 61 72 73 65 72 0a 2a 2a  to the parser.**
207c7 20 62 75 74 20 69 74 20 64 6f 65 73 20 6e 6f 74   but it does not
207c8 20 70 61 72 73 65 2c 20 74 68 65 20 74 79 70 65   parse, the type
207c9 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 69 73   of the token is
207ca 20 63 68 61 6e 67 65 64 20 74 6f 20 49 44 20 61   changed to ID a
207cb 6e 64 0a 2a 2a 20 74 68 65 20 70 61 72 73 65 20  nd.** the parse 
207cc 69 73 20 72 65 74 72 69 65 64 20 62 65 66 6f 72  is retried befor
207cd 65 20 61 6e 20 65 72 72 6f 72 20 69 73 20 74 68  e an error is th
207ce 72 6f 77 6e 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  rown..*/.#ifdef 
207cf 59 59 46 41 4c 4c 42 41 43 4b 0a 73 74 61 74 69  YYFALLBACK.stati
207d0 63 20 63 6f 6e 73 74 20 59 59 43 4f 44 45 54 59  c const YYCODETY
207d1 50 45 20 79 79 46 61 6c 6c 62 61 63 6b 5b 5d 20  PE yyFallback[] 
207d2 3d 20 7b 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20  = {.    0,  /*  
207d3 20 20 20 20 20 20 20 20 24 20 3d 3e 20 6e 6f 74          $ => not
207d4 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
207d5 2f 2a 20 20 20 20 20 20 20 53 45 4d 49 20 3d 3e  /*       SEMI =>
207d6 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 32   nothing */.   2
207d7 33 2c 20 20 2f 2a 20 20 20 20 45 58 50 4c 41 49  3,  /*    EXPLAI
207d8 4e 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33  N => ID */.   23
207d9 2c 20 20 2f 2a 20 20 20 20 20 20 51 55 45 52 59  ,  /*      QUERY
207da 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c   => ID */.   23,
207db 20 20 2f 2a 20 20 20 20 20 20 20 50 4c 41 4e 20    /*       PLAN 
207dc 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20  => ID */.   23, 
207dd 20 2f 2a 20 20 20 20 20 20 42 45 47 49 4e 20 3d   /*      BEGIN =
207de 3e 20 49 44 20 2a 2f 0a 20 20 20 20 30 2c 20 20  > ID */.    0,  
207df 2f 2a 20 54 52 41 4e 53 41 43 54 49 4f 4e 20 3d  /* TRANSACTION =
207e0 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20  > nothing */.   
207e1 32 33 2c 20 20 2f 2a 20 20 20 44 45 46 45 52 52  23,  /*   DEFERR
207e2 45 44 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  ED => ID */.   2
207e3 33 2c 20 20 2f 2a 20 20 49 4d 4d 45 44 49 41 54  3,  /*  IMMEDIAT
207e4 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33  E => ID */.   23
207e5 2c 20 20 2f 2a 20 20 45 58 43 4c 55 53 49 56 45  ,  /*  EXCLUSIVE
207e6 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 20 30 2c   => ID */.    0,
207e7 20 20 2f 2a 20 20 20 20 20 43 4f 4d 4d 49 54 20    /*     COMMIT 
207e8 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
207e9 20 32 33 2c 20 20 2f 2a 20 20 20 20 20 20 20 20   23,  /*        
207ea 45 4e 44 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  END => ID */.   
207eb 20 30 2c 20 20 2f 2a 20 20 20 52 4f 4c 4c 42 41   0,  /*   ROLLBA
207ec 43 4b 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f  CK => nothing */
207ed 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20  .    0,  /*     
207ee 43 52 45 41 54 45 20 3d 3e 20 6e 6f 74 68 69 6e  CREATE => nothin
207ef 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20  g */.    0,  /* 
207f0 20 20 20 20 20 54 41 42 4c 45 20 3d 3e 20 6e 6f       TABLE => no
207f1 74 68 69 6e 67 20 2a 2f 0a 20 20 20 32 33 2c 20  thing */.   23, 
207f2 20 2f 2a 20 20 20 20 20 20 20 20 20 49 46 20 3d   /*         IF =
207f3 3e 20 49 44 20 2a 2f 0a 20 20 20 20 30 2c 20 20  > ID */.    0,  
207f4 2f 2a 20 20 20 20 20 20 20 20 4e 4f 54 20 3d 3e  /*        NOT =>
207f5 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
207f6 30 2c 20 20 2f 2a 20 20 20 20 20 45 58 49 53 54  0,  /*     EXIST
207f7 53 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  S => nothing */.
207f8 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 20 20     23,  /*      
207f9 20 54 45 4d 50 20 3d 3e 20 49 44 20 2a 2f 0a 20   TEMP => ID */. 
207fa 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
207fb 20 20 4c 50 20 3d 3e 20 6e 6f 74 68 69 6e 67 20    LP => nothing 
207fc 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
207fd 20 20 20 20 20 20 52 50 20 3d 3e 20 6e 6f 74 68        RP => noth
207fe 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
207ff 2a 20 20 20 20 20 20 20 20 20 41 53 20 3d 3e 20  *         AS => 
20800 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
20801 2c 20 20 2f 2a 20 20 20 20 20 20 43 4f 4d 4d 41  ,  /*      COMMA
20802 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
20803 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
20804 20 20 49 44 20 3d 3e 20 6e 6f 74 68 69 6e 67 20    ID => nothing 
20805 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20  */.   23,  /*   
20806 20 20 20 41 42 4f 52 54 20 3d 3e 20 49 44 20 2a     ABORT => ID *
20807 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20  /.   23,  /*    
20808 20 20 41 46 54 45 52 20 3d 3e 20 49 44 20 2a 2f    AFTER => ID */
20809 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 41  .   23,  /*    A
2080a 4e 41 4c 59 5a 45 20 3d 3e 20 49 44 20 2a 2f 0a  NALYZE => ID */.
2080b 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 20 20     23,  /*      
2080c 20 20 41 53 43 20 3d 3e 20 49 44 20 2a 2f 0a 20    ASC => ID */. 
2080d 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 20 41 54    23,  /*     AT
2080e 54 41 43 48 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  TACH => ID */.  
2080f 20 32 33 2c 20 20 2f 2a 20 20 20 20 20 42 45 46   23,  /*     BEF
20810 4f 52 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  ORE => ID */.   
20811 32 33 2c 20 20 2f 2a 20 20 20 20 43 41 53 43 41  23,  /*    CASCA
20812 44 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  DE => ID */.   2
20813 33 2c 20 20 2f 2a 20 20 20 20 20 20 20 43 41 53  3,  /*       CAS
20814 54 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33  T => ID */.   23
20815 2c 20 20 2f 2a 20 20 20 43 4f 4e 46 4c 49 43 54  ,  /*   CONFLICT
20816 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c   => ID */.   23,
20817 20 20 2f 2a 20 20 20 44 41 54 41 42 41 53 45 20    /*   DATABASE 
20818 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20  => ID */.   23, 
20819 20 2f 2a 20 20 20 20 20 20 20 44 45 53 43 20 3d   /*       DESC =
2081a 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20  > ID */.   23,  
2081b 2f 2a 20 20 20 20 20 44 45 54 41 43 48 20 3d 3e  /*     DETACH =>
2081c 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f   ID */.   23,  /
2081d 2a 20 20 20 20 20 20 20 45 41 43 48 20 3d 3e 20  *       EACH => 
2081e 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a  ID */.   23,  /*
2081f 20 20 20 20 20 20 20 46 41 49 4c 20 3d 3e 20 49         FAIL => I
20820 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20  D */.   23,  /* 
20821 20 20 20 20 20 20 20 46 4f 52 20 3d 3e 20 49 44         FOR => ID
20822 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20   */.   23,  /*  
20823 20 20 20 49 47 4e 4f 52 45 20 3d 3e 20 49 44 20     IGNORE => ID 
20824 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 49  */.   23,  /*  I
20825 4e 49 54 49 41 4c 4c 59 20 3d 3e 20 49 44 20 2a  NITIALLY => ID *
20826 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20  /.   23,  /*    
20827 49 4e 53 54 45 41 44 20 3d 3e 20 49 44 20 2a 2f  INSTEAD => ID */
20828 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 4c  .   23,  /*    L
20829 49 4b 45 5f 4b 57 20 3d 3e 20 49 44 20 2a 2f 0a  IKE_KW => ID */.
2082a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 20 20     23,  /*      
2082b 4d 41 54 43 48 20 3d 3e 20 49 44 20 2a 2f 0a 20  MATCH => ID */. 
2082c 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 20 20 20    23,  /*       
2082d 20 4b 45 59 20 3d 3e 20 49 44 20 2a 2f 0a 20 20   KEY => ID */.  
2082e 20 32 33 2c 20 20 2f 2a 20 20 20 20 20 20 20 20   23,  /*        
2082f 20 4f 46 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20   OF => ID */.   
20830 32 33 2c 20 20 2f 2a 20 20 20 20 20 4f 46 46 53  23,  /*     OFFS
20831 45 54 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  ET => ID */.   2
20832 33 2c 20 20 2f 2a 20 20 20 20 20 50 52 41 47 4d  3,  /*     PRAGM
20833 41 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33  A => ID */.   23
20834 2c 20 20 2f 2a 20 20 20 20 20 20 52 41 49 53 45  ,  /*      RAISE
20835 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c   => ID */.   23,
20836 20 20 2f 2a 20 20 20 20 52 45 50 4c 41 43 45 20    /*    REPLACE 
20837 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20  => ID */.   23, 
20838 20 2f 2a 20 20 20 52 45 53 54 52 49 43 54 20 3d   /*   RESTRICT =
20839 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20  > ID */.   23,  
2083a 2f 2a 20 20 20 20 20 20 20 20 52 4f 57 20 3d 3e  /*        ROW =>
2083b 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f   ID */.   23,  /
2083c 2a 20 20 20 20 54 52 49 47 47 45 52 20 3d 3e 20  *    TRIGGER => 
2083d 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a  ID */.   23,  /*
2083e 20 20 20 20 20 56 41 43 55 55 4d 20 3d 3e 20 49       VACUUM => I
2083f 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20  D */.   23,  /* 
20840 20 20 20 20 20 20 56 49 45 57 20 3d 3e 20 49 44        VIEW => ID
20841 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20   */.   23,  /*  
20842 20 20 56 49 52 54 55 41 4c 20 3d 3e 20 49 44 20    VIRTUAL => ID 
20843 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20  */.   23,  /*   
20844 20 52 45 49 4e 44 45 58 20 3d 3e 20 49 44 20 2a   REINDEX => ID *
20845 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20  /.   23,  /*    
20846 20 52 45 4e 41 4d 45 20 3d 3e 20 49 44 20 2a 2f   RENAME => ID */
20847 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 43 54  .   23,  /*   CT
20848 49 4d 45 5f 4b 57 20 3d 3e 20 49 44 20 2a 2f 0a  IME_KW => ID */.
20849 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20      0,  /*      
2084a 20 20 41 4e 59 20 3d 3e 20 6e 6f 74 68 69 6e 67    ANY => nothing
2084b 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
2084c 20 20 20 20 20 20 20 4f 52 20 3d 3e 20 6e 6f 74         OR => not
2084d 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
2084e 2f 2a 20 20 20 20 20 20 20 20 41 4e 44 20 3d 3e  /*        AND =>
2084f 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
20850 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 49  0,  /*         I
20851 53 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  S => nothing */.
20852 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 42 45      0,  /*    BE
20853 54 57 45 45 4e 20 3d 3e 20 6e 6f 74 68 69 6e 67  TWEEN => nothing
20854 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
20855 20 20 20 20 20 20 20 49 4e 20 3d 3e 20 6e 6f 74         IN => not
20856 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
20857 2f 2a 20 20 20 20 20 49 53 4e 55 4c 4c 20 3d 3e  /*     ISNULL =>
20858 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
20859 30 2c 20 20 2f 2a 20 20 20 20 4e 4f 54 4e 55 4c  0,  /*    NOTNUL
2085a 4c 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  L => nothing */.
2085b 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20      0,  /*      
2085c 20 20 20 4e 45 20 3d 3e 20 6e 6f 74 68 69 6e 67     NE => nothing
2085d 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
2085e 20 20 20 20 20 20 20 45 51 20 3d 3e 20 6e 6f 74         EQ => not
2085f 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
20860 2f 2a 20 20 20 20 20 20 20 20 20 47 54 20 3d 3e  /*         GT =>
20861 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
20862 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 4c  0,  /*         L
20863 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  E => nothing */.
20864 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20      0,  /*      
20865 20 20 20 4c 54 20 3d 3e 20 6e 6f 74 68 69 6e 67     LT => nothing
20866 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
20867 20 20 20 20 20 20 20 47 45 20 3d 3e 20 6e 6f 74         GE => not
20868 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
20869 2f 2a 20 20 20 20 20 45 53 43 41 50 45 20 3d 3e  /*     ESCAPE =>
2086a 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
2086b 30 2c 20 20 2f 2a 20 20 20 20 20 42 49 54 41 4e  0,  /*     BITAN
2086c 44 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  D => nothing */.
2086d 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20      0,  /*      
2086e 42 49 54 4f 52 20 3d 3e 20 6e 6f 74 68 69 6e 67  BITOR => nothing
2086f 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
20870 20 20 20 4c 53 48 49 46 54 20 3d 3e 20 6e 6f 74     LSHIFT => not
20871 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
20872 2f 2a 20 20 20 20 20 52 53 48 49 46 54 20 3d 3e  /*     RSHIFT =>
20873 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
20874 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 50 4c 55  0,  /*       PLU
20875 53 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  S => nothing */.
20876 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20      0,  /*      
20877 4d 49 4e 55 53 20 3d 3e 20 6e 6f 74 68 69 6e 67  MINUS => nothing
20878 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
20879 20 20 20 20 20 53 54 41 52 20 3d 3e 20 6e 6f 74       STAR => not
2087a 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
2087b 2f 2a 20 20 20 20 20 20 53 4c 41 53 48 20 3d 3e  /*      SLASH =>
2087c 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
2087d 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 52 45  0,  /*        RE
2087e 4d 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  M => nothing */.
2087f 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 43      0,  /*     C
20880 4f 4e 43 41 54 20 3d 3e 20 6e 6f 74 68 69 6e 67  ONCAT => nothing
20881 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
20882 20 20 43 4f 4c 4c 41 54 45 20 3d 3e 20 6e 6f 74    COLLATE => not
20883 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
20884 2f 2a 20 20 20 20 20 55 4d 49 4e 55 53 20 3d 3e  /*     UMINUS =>
20885 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
20886 30 2c 20 20 2f 2a 20 20 20 20 20 20 55 50 4c 55  0,  /*      UPLU
20887 53 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  S => nothing */.
20888 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 42      0,  /*     B
20889 49 54 4e 4f 54 20 3d 3e 20 6e 6f 74 68 69 6e 67  ITNOT => nothing
2088a 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
2088b 20 20 20 53 54 52 49 4e 47 20 3d 3e 20 6e 6f 74     STRING => not
2088c 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
2088d 2f 2a 20 20 20 20 4a 4f 49 4e 5f 4b 57 20 3d 3e  /*    JOIN_KW =>
2088e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
2088f 30 2c 20 20 2f 2a 20 43 4f 4e 53 54 52 41 49 4e  0,  /* CONSTRAIN
20890 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  T => nothing */.
20891 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 44 45      0,  /*    DE
20892 46 41 55 4c 54 20 3d 3e 20 6e 6f 74 68 69 6e 67  FAULT => nothing
20893 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
20894 20 20 20 20 20 4e 55 4c 4c 20 3d 3e 20 6e 6f 74       NULL => not
20895 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
20896 2f 2a 20 20 20 20 50 52 49 4d 41 52 59 20 3d 3e  /*    PRIMARY =>
20897 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
20898 30 2c 20 20 2f 2a 20 20 20 20 20 55 4e 49 51 55  0,  /*     UNIQU
20899 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  E => nothing */.
2089a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20      0,  /*      
2089b 43 48 45 43 4b 20 3d 3e 20 6e 6f 74 68 69 6e 67  CHECK => nothing
2089c 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 52   */.    0,  /* R
2089d 45 46 45 52 45 4e 43 45 53 20 3d 3e 20 6e 6f 74  EFERENCES => not
2089e 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
2089f 2f 2a 20 20 20 41 55 54 4f 49 4e 43 52 20 3d 3e  /*   AUTOINCR =>
208a0 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
208a1 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 4f  0,  /*         O
208a2 4e 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  N => nothing */.
208a3 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 44      0,  /*     D
208a4 45 4c 45 54 45 20 3d 3e 20 6e 6f 74 68 69 6e 67  ELETE => nothing
208a5 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
208a6 20 20 20 55 50 44 41 54 45 20 3d 3e 20 6e 6f 74     UPDATE => not
208a7 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
208a8 2f 2a 20 20 20 20 20 49 4e 53 45 52 54 20 3d 3e  /*     INSERT =>
208a9 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
208aa 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 53 45  0,  /*        SE
208ab 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  T => nothing */.
208ac 20 20 20 20 30 2c 20 20 2f 2a 20 44 45 46 45 52      0,  /* DEFER
208ad 52 41 42 4c 45 20 3d 3e 20 6e 6f 74 68 69 6e 67  RABLE => nothing
208ae 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
208af 20 20 46 4f 52 45 49 47 4e 20 3d 3e 20 6e 6f 74    FOREIGN => not
208b0 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
208b1 2f 2a 20 20 20 20 20 20 20 44 52 4f 50 20 3d 3e  /*       DROP =>
208b2 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
208b3 30 2c 20 20 2f 2a 20 20 20 20 20 20 55 4e 49 4f  0,  /*      UNIO
208b4 4e 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  N => nothing */.
208b5 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20      0,  /*      
208b6 20 20 41 4c 4c 20 3d 3e 20 6e 6f 74 68 69 6e 67    ALL => nothing
208b7 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
208b8 20 20 20 45 58 43 45 50 54 20 3d 3e 20 6e 6f 74     EXCEPT => not
208b9 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
208ba 2f 2a 20 20 49 4e 54 45 52 53 45 43 54 20 3d 3e  /*  INTERSECT =>
208bb 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
208bc 30 2c 20 20 2f 2a 20 20 20 20 20 53 45 4c 45 43  0,  /*     SELEC
208bd 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  T => nothing */.
208be 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 44 49 53      0,  /*   DIS
208bf 54 49 4e 43 54 20 3d 3e 20 6e 6f 74 68 69 6e 67  TINCT => nothing
208c0 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
208c1 20 20 20 20 20 20 44 4f 54 20 3d 3e 20 6e 6f 74        DOT => not
208c2 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
208c3 2f 2a 20 20 20 20 20 20 20 46 52 4f 4d 20 3d 3e  /*       FROM =>
208c4 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
208c5 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 4a 4f 49  0,  /*       JOI
208c6 4e 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  N => nothing */.
208c7 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20      0,  /*      
208c8 55 53 49 4e 47 20 3d 3e 20 6e 6f 74 68 69 6e 67  USING => nothing
208c9 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
208ca 20 20 20 20 4f 52 44 45 52 20 3d 3e 20 6e 6f 74      ORDER => not
208cb 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
208cc 2f 2a 20 20 20 20 20 20 20 20 20 42 59 20 3d 3e  /*         BY =>
208cd 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
208ce 30 2c 20 20 2f 2a 20 20 20 20 20 20 47 52 4f 55  0,  /*      GROU
208cf 50 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  P => nothing */.
208d0 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 48      0,  /*     H
208d1 41 56 49 4e 47 20 3d 3e 20 6e 6f 74 68 69 6e 67  AVING => nothing
208d2 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
208d3 20 20 20 20 4c 49 4d 49 54 20 3d 3e 20 6e 6f 74      LIMIT => not
208d4 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
208d5 2f 2a 20 20 20 20 20 20 57 48 45 52 45 20 3d 3e  /*      WHERE =>
208d6 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
208d7 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 49 4e 54  0,  /*       INT
208d8 4f 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  O => nothing */.
208d9 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 56      0,  /*     V
208da 41 4c 55 45 53 20 3d 3e 20 6e 6f 74 68 69 6e 67  ALUES => nothing
208db 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
208dc 20 20 49 4e 54 45 47 45 52 20 3d 3e 20 6e 6f 74    INTEGER => not
208dd 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
208de 2f 2a 20 20 20 20 20 20 46 4c 4f 41 54 20 3d 3e  /*      FLOAT =>
208df 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
208e0 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 42 4c 4f  0,  /*       BLO
208e1 42 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  B => nothing */.
208e2 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 52 45 47      0,  /*   REG
208e3 49 53 54 45 52 20 3d 3e 20 6e 6f 74 68 69 6e 67  ISTER => nothing
208e4 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
208e5 20 56 41 52 49 41 42 4c 45 20 3d 3e 20 6e 6f 74   VARIABLE => not
208e6 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
208e7 2f 2a 20 20 20 20 20 20 20 43 41 53 45 20 3d 3e  /*       CASE =>
208e8 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
208e9 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 57 48 45  0,  /*       WHE
208ea 4e 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  N => nothing */.
208eb 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20      0,  /*      
208ec 20 54 48 45 4e 20 3d 3e 20 6e 6f 74 68 69 6e 67   THEN => nothing
208ed 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
208ee 20 20 20 20 20 45 4c 53 45 20 3d 3e 20 6e 6f 74       ELSE => not
208ef 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
208f0 2f 2a 20 20 20 20 20 20 49 4e 44 45 58 20 3d 3e  /*      INDEX =>
208f1 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
208f2 30 2c 20 20 2f 2a 20 20 20 20 20 20 41 4c 54 45  0,  /*      ALTE
208f3 52 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  R => nothing */.
208f4 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20      0,  /*      
208f5 20 20 20 54 4f 20 3d 3e 20 6e 6f 74 68 69 6e 67     TO => nothing
208f6 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
208f7 20 20 20 20 20 20 41 44 44 20 3d 3e 20 6e 6f 74        ADD => not
208f8 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
208f9 2f 2a 20 20 20 43 4f 4c 55 4d 4e 4b 57 20 3d 3e  /*   COLUMNKW =>
208fa 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 7d 3b 0a 23   nothing */.};.#
208fb 65 6e 64 69 66 20 2f 2a 20 59 59 46 41 4c 4c 42  endif /* YYFALLB
208fc 41 43 4b 20 2a 2f 0a 0a 2f 2a 20 54 68 65 20 66  ACK */../* The f
208fd 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
208fe 72 65 20 72 65 70 72 65 73 65 6e 74 73 20 61 20  re represents a 
208ff 73 69 6e 67 6c 65 20 65 6c 65 6d 65 6e 74 20 6f  single element o
20900 66 20 74 68 65 0a 2a 2a 20 70 61 72 73 65 72 27  f the.** parser'
20901 73 20 73 74 61 63 6b 2e 20 20 49 6e 66 6f 72 6d  s stack.  Inform
20902 61 74 69 6f 6e 20 73 74 6f 72 65 64 20 69 6e 63  ation stored inc
20903 6c 75 64 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2b  ludes:.**.**   +
20904 20 20 54 68 65 20 73 74 61 74 65 20 6e 75 6d 62    The state numb
20905 65 72 20 66 6f 72 20 74 68 65 20 70 61 72 73 65  er for the parse
20906 72 20 61 74 20 74 68 69 73 20 6c 65 76 65 6c 20  r at this level 
20907 6f 66 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2a  of the stack..**
20908 0a 2a 2a 20 20 20 2b 20 20 54 68 65 20 76 61 6c  .**   +  The val
20909 75 65 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20  ue of the token 
2090a 73 74 6f 72 65 64 20 61 74 20 74 68 69 73 20 6c  stored at this l
2090b 65 76 65 6c 20 6f 66 20 74 68 65 20 73 74 61 63  evel of the stac
2090c 6b 2e 0a 2a 2a 20 20 20 20 20 20 28 49 6e 20 6f  k..**      (In o
2090d 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20  ther words, the 
2090e 22 6d 61 6a 6f 72 22 20 74 6f 6b 65 6e 2e 29 0a  "major" token.).
2090f 2a 2a 0a 2a 2a 20 20 20 2b 20 20 54 68 65 20 73  **.**   +  The s
20910 65 6d 61 6e 74 69 63 20 76 61 6c 75 65 20 73 74  emantic value st
20911 6f 72 65 64 20 61 74 20 74 68 69 73 20 6c 65 76  ored at this lev
20912 65 6c 20 6f 66 20 74 68 65 20 73 74 61 63 6b 2e  el of the stack.
20913 20 20 54 68 69 73 20 69 73 0a 2a 2a 20 20 20 20    This is.**    
20914 20 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f    the informatio
20915 6e 20 75 73 65 64 20 62 79 20 74 68 65 20 61 63  n used by the ac
20916 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 69 6e  tion routines in
20917 20 74 68 65 20 67 72 61 6d 6d 61 72 2e 0a 2a 2a   the grammar..**
20918 20 20 20 20 20 20 49 74 20 69 73 20 73 6f 6d 65        It is some
20919 74 69 6d 65 73 20 63 61 6c 6c 65 64 20 74 68 65  times called the
2091a 20 22 6d 69 6e 6f 72 22 20 74 6f 6b 65 6e 2e 0a   "minor" token..
2091b 2a 2f 0a 73 74 72 75 63 74 20 79 79 53 74 61 63  */.struct yyStac
2091c 6b 45 6e 74 72 79 20 7b 0a 20 20 69 6e 74 20 73  kEntry {.  int s
2091d 74 61 74 65 6e 6f 3b 20 20 20 20 20 20 20 2f 2a  tateno;       /*
2091e 20 54 68 65 20 73 74 61 74 65 2d 6e 75 6d 62 65   The state-numbe
2091f 72 20 2a 2f 0a 20 20 69 6e 74 20 6d 61 6a 6f 72  r */.  int major
20920 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ;         /* The
20921 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 76 61 6c   major token val
20922 75 65 2e 20 20 54 68 69 73 20 69 73 20 74 68 65  ue.  This is the
20923 20 63 6f 64 65 0a 20 20 20 20 20 20 20 20 20 20   code.          
20924 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6e 75             ** nu
20925 6d 62 65 72 20 66 6f 72 20 74 68 65 20 74 6f 6b  mber for the tok
20926 65 6e 20 61 74 20 74 68 69 73 20 73 74 61 63 6b  en at this stack
20927 20 6c 65 76 65 6c 20 2a 2f 0a 20 20 59 59 4d 49   level */.  YYMI
20928 4e 4f 52 54 59 50 45 20 6d 69 6e 6f 72 3b 20 2f  NORTYPE minor; /
20929 2a 20 54 68 65 20 75 73 65 72 2d 73 75 70 70 6c  * The user-suppl
2092a 69 65 64 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e 20  ied minor token 
2092b 76 61 6c 75 65 2e 20 20 54 68 69 73 0a 20 20 20  value.  This.   
2092c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2092d 20 20 2a 2a 20 69 73 20 74 68 65 20 76 61 6c 75    ** is the valu
2092e 65 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 20  e of the token  
2092f 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74  */.};.typedef st
20930 72 75 63 74 20 79 79 53 74 61 63 6b 45 6e 74 72  ruct yyStackEntr
20931 79 20 79 79 53 74 61 63 6b 45 6e 74 72 79 3b 0a  y yyStackEntry;.
20932 0a 2f 2a 20 54 68 65 20 73 74 61 74 65 20 6f 66  ./* The state of
20933 20 74 68 65 20 70 61 72 73 65 72 20 69 73 20 63   the parser is c
20934 6f 6d 70 6c 65 74 65 6c 79 20 63 6f 6e 74 61 69  ompletely contai
20935 6e 65 64 20 69 6e 20 61 6e 20 69 6e 73 74 61 6e  ned in an instan
20936 63 65 20 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 6c  ce of.** the fol
20937 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
20938 20 2a 2f 0a 73 74 72 75 63 74 20 79 79 50 61 72   */.struct yyPar
20939 73 65 72 20 7b 0a 20 20 69 6e 74 20 79 79 69 64  ser {.  int yyid
2093a 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x;              
2093b 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
2093c 66 20 74 6f 70 20 65 6c 65 6d 65 6e 74 20 69 6e  f top element in
2093d 20 73 74 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20   stack */.  int 
2093e 79 79 65 72 72 63 6e 74 3b 20 20 20 20 20 20 20  yyerrcnt;       
2093f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68 69            /* Shi
20940 66 74 73 20 6c 65 66 74 20 62 65 66 6f 72 65 20  fts left before 
20941 6f 75 74 20 6f 66 20 74 68 65 20 65 72 72 6f 72  out of the error
20942 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72   */.  sqlite3Par
20943 73 65 72 41 52 47 5f 53 44 45 43 4c 20 20 20 20  serARG_SDECL    
20944 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
20945 20 70 6c 61 63 65 20 74 6f 20 68 6f 6c 64 20 25   place to hold %
20946 65 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 2a  extra_argument *
20947 2f 0a 23 69 66 20 59 59 53 54 41 43 4b 44 45 50  /.#if YYSTACKDEP
20948 54 48 3c 3d 30 0a 20 20 69 6e 74 20 79 79 73 74  TH<=0.  int yyst
20949 6b 73 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20  ksz;            
2094a 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74        /* Current
2094b 20 73 69 64 65 20 6f 66 20 74 68 65 20 73 74 61   side of the sta
2094c 63 6b 20 2a 2f 0a 20 20 79 79 53 74 61 63 6b 45  ck */.  yyStackE
2094d 6e 74 72 79 20 2a 79 79 73 74 61 63 6b 3b 20 20  ntry *yystack;  
2094e 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
2094f 73 65 72 27 73 20 73 74 61 63 6b 20 2a 2f 0a 23  ser's stack */.#
20950 65 6c 73 65 0a 20 20 79 79 53 74 61 63 6b 45 6e  else.  yyStackEn
20951 74 72 79 20 79 79 73 74 61 63 6b 5b 59 59 53 54  try yystack[YYST
20952 41 43 4b 44 45 50 54 48 5d 3b 20 20 2f 2a 20 54  ACKDEPTH];  /* T
20953 68 65 20 70 61 72 73 65 72 27 73 20 73 74 61 63  he parser's stac
20954 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 74  k */.#endif.};.t
20955 79 70 65 64 65 66 20 73 74 72 75 63 74 20 79 79  ypedef struct yy
20956 50 61 72 73 65 72 20 79 79 50 61 72 73 65 72 3b  Parser yyParser;
20957 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
20958 0a 73 74 61 74 69 63 20 46 49 4c 45 20 2a 79 79  .static FILE *yy
20959 54 72 61 63 65 46 49 4c 45 20 3d 20 30 3b 0a 73  TraceFILE = 0;.s
2095a 74 61 74 69 63 20 63 68 61 72 20 2a 79 79 54 72  tatic char *yyTr
2095b 61 63 65 50 72 6f 6d 70 74 20 3d 20 30 3b 0a 23  acePrompt = 0;.#
2095c 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20  endif /* NDEBUG 
2095d 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  */..#ifndef NDEB
2095e 55 47 0a 2f 2a 20 0a 2a 2a 20 54 75 72 6e 20 70  UG./* .** Turn p
2095f 61 72 73 65 72 20 74 72 61 63 69 6e 67 20 6f 6e  arser tracing on
20960 20 62 79 20 67 69 76 69 6e 67 20 61 20 73 74 72   by giving a str
20961 65 61 6d 20 74 6f 20 77 68 69 63 68 20 74 6f 20  eam to which to 
20962 77 72 69 74 65 20 74 68 65 20 74 72 61 63 65 0a  write the trace.
20963 2a 2a 20 61 6e 64 20 61 20 70 72 6f 6d 70 74 20  ** and a prompt 
20964 74 6f 20 70 72 65 66 61 63 65 20 65 61 63 68 20  to preface each 
20965 74 72 61 63 65 20 6d 65 73 73 61 67 65 2e 20 20  trace message.  
20966 54 72 61 63 69 6e 67 20 69 73 20 74 75 72 6e 65  Tracing is turne
20967 64 20 6f 66 66 0a 2a 2a 20 62 79 20 6d 61 6b 69  d off.** by maki
20968 6e 67 20 65 69 74 68 65 72 20 61 72 67 75 6d 65  ng either argume
20969 6e 74 20 4e 55 4c 4c 20 0a 2a 2a 0a 2a 2a 20 49  nt NULL .**.** I
2096a 6e 70 75 74 73 3a 0a 2a 2a 20 3c 75 6c 3e 0a 2a  nputs:.** <ul>.*
2096b 2a 20 3c 6c 69 3e 20 41 20 46 49 4c 45 2a 20 74  * <li> A FILE* t
2096c 6f 20 77 68 69 63 68 20 74 72 61 63 65 20 6f 75  o which trace ou
2096d 74 70 75 74 20 73 68 6f 75 6c 64 20 62 65 20 77  tput should be w
2096e 72 69 74 74 65 6e 2e 0a 2a 2a 20 20 20 20 20 20  ritten..**      
2096f 49 66 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 72  If NULL, then tr
20970 61 63 69 6e 67 20 69 73 20 74 75 72 6e 65 64 20  acing is turned 
20971 6f 66 66 2e 0a 2a 2a 20 3c 6c 69 3e 20 41 20 70  off..** <li> A p
20972 72 65 66 69 78 20 73 74 72 69 6e 67 20 77 72 69  refix string wri
20973 74 74 65 6e 20 61 74 20 74 68 65 20 62 65 67 69  tten at the begi
20974 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72 79 0a 2a  nning of every.*
20975 2a 20 20 20 20 20 20 6c 69 6e 65 20 6f 66 20 74  *      line of t
20976 72 61 63 65 20 6f 75 74 70 75 74 2e 20 20 49 66  race output.  If
20977 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 72 61 63   NULL, then trac
20978 69 6e 67 20 69 73 0a 2a 2a 20 20 20 20 20 20 74  ing is.**      t
20979 75 72 6e 65 64 20 6f 66 66 2e 0a 2a 2a 20 3c 2f  urned off..** </
2097a 75 6c 3e 0a 2a 2a 0a 2a 2a 20 4f 75 74 70 75 74  ul>.**.** Output
2097b 73 3a 0a 2a 2a 20 4e 6f 6e 65 2e 0a 2a 2f 0a 53  s:.** None..*/.S
2097c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2097d 69 64 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  id sqlite3Parser
2097e 54 72 61 63 65 28 46 49 4c 45 20 2a 54 72 61 63  Trace(FILE *Trac
2097f 65 46 49 4c 45 2c 20 63 68 61 72 20 2a 7a 54 72  eFILE, char *zTr
20980 61 63 65 50 72 6f 6d 70 74 29 7b 0a 20 20 79 79  acePrompt){.  yy
20981 54 72 61 63 65 46 49 4c 45 20 3d 20 54 72 61 63  TraceFILE = Trac
20982 65 46 49 4c 45 3b 0a 20 20 79 79 54 72 61 63 65  eFILE;.  yyTrace
20983 50 72 6f 6d 70 74 20 3d 20 7a 54 72 61 63 65 50  Prompt = zTraceP
20984 72 6f 6d 70 74 3b 0a 20 20 69 66 28 20 79 79 54  rompt;.  if( yyT
20985 72 61 63 65 46 49 4c 45 3d 3d 30 20 29 20 79 79  raceFILE==0 ) yy
20986 54 72 61 63 65 50 72 6f 6d 70 74 20 3d 20 30 3b  TracePrompt = 0;
20987 0a 20 20 65 6c 73 65 20 69 66 28 20 79 79 54 72  .  else if( yyTr
20988 61 63 65 50 72 6f 6d 70 74 3d 3d 30 20 29 20 79  acePrompt==0 ) y
20989 79 54 72 61 63 65 46 49 4c 45 20 3d 20 30 3b 0a  yTraceFILE = 0;.
2098a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42  }.#endif /* NDEB
2098b 55 47 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e  UG */..#ifndef N
2098c 44 45 42 55 47 0a 2f 2a 20 46 6f 72 20 74 72 61  DEBUG./* For tra
2098d 63 69 6e 67 20 73 68 69 66 74 73 2c 20 74 68 65  cing shifts, the
2098e 20 6e 61 6d 65 73 20 6f 66 20 61 6c 6c 20 74 65   names of all te
2098f 72 6d 69 6e 61 6c 73 20 61 6e 64 20 6e 6f 6e 74  rminals and nont
20990 65 72 6d 69 6e 61 6c 73 0a 2a 2a 20 61 72 65 20  erminals.** are 
20991 72 65 71 75 69 72 65 64 2e 20 20 54 68 65 20 66  required.  The f
20992 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 73  ollowing table s
20993 75 70 70 6c 69 65 73 20 74 68 65 73 65 20 6e 61  upplies these na
20994 6d 65 73 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f  mes */.static co
20995 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 20  nst char *const 
20996 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 5d 20 3d 20  yyTokenName[] = 
20997 7b 20 0a 20 20 22 24 22 2c 20 20 20 20 20 20 20  { .  "$",       
20998 20 20 20 20 20 20 22 53 45 4d 49 22 2c 20 20 20        "SEMI",   
20999 20 20 20 20 20 20 20 22 45 58 50 4c 41 49 4e 22         "EXPLAIN"
2099a 2c 20 20 20 20 20 20 20 22 51 55 45 52 59 22 2c  ,       "QUERY",
2099b 20 20 20 20 20 20 20 0a 20 20 22 50 4c 41 4e 22         .  "PLAN"
2099c 2c 20 20 20 20 20 20 20 20 20 20 22 42 45 47 49  ,          "BEGI
2099d 4e 22 2c 20 20 20 20 20 20 20 20 20 22 54 52 41  N",         "TRA
2099e 4e 53 41 43 54 49 4f 4e 22 2c 20 20 20 22 44 45  NSACTION",   "DE
2099f 46 45 52 52 45 44 22 2c 20 20 20 20 0a 20 20 22  FERRED",    .  "
209a0 49 4d 4d 45 44 49 41 54 45 22 2c 20 20 20 20 20  IMMEDIATE",     
209a1 22 45 58 43 4c 55 53 49 56 45 22 2c 20 20 20 20  "EXCLUSIVE",    
209a2 20 22 43 4f 4d 4d 49 54 22 2c 20 20 20 20 20 20   "COMMIT",      
209a3 20 20 22 45 4e 44 22 2c 20 20 20 20 20 20 20 20    "END",        
209a4 20 0a 20 20 22 52 4f 4c 4c 42 41 43 4b 22 2c 20   .  "ROLLBACK", 
209a5 20 20 20 20 20 22 43 52 45 41 54 45 22 2c 20 20       "CREATE",  
209a6 20 20 20 20 20 20 22 54 41 42 4c 45 22 2c 20 20        "TABLE",  
209a7 20 20 20 20 20 20 20 22 49 46 22 2c 20 20 20 20         "IF",    
209a8 20 20 20 20 20 20 0a 20 20 22 4e 4f 54 22 2c 20        .  "NOT", 
209a9 20 20 20 20 20 20 20 20 20 20 22 45 58 49 53 54            "EXIST
209aa 53 22 2c 20 20 20 20 20 20 20 20 22 54 45 4d 50  S",        "TEMP
209ab 22 2c 20 20 20 20 20 20 20 20 20 20 22 4c 50 22  ",          "LP"
209ac 2c 20 20 20 20 20 20 20 20 20 20 0a 20 20 22 52  ,          .  "R
209ad 50 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22  P",            "
209ae 41 53 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  AS",            
209af 22 43 4f 4d 4d 41 22 2c 20 20 20 20 20 20 20 20  "COMMA",        
209b0 20 22 49 44 22 2c 20 20 20 20 20 20 20 20 20 20   "ID",          
209b1 0a 20 20 22 41 42 4f 52 54 22 2c 20 20 20 20 20  .  "ABORT",     
209b2 20 20 20 20 22 41 46 54 45 52 22 2c 20 20 20 20      "AFTER",    
209b3 20 20 20 20 20 22 41 4e 41 4c 59 5a 45 22 2c 20       "ANALYZE", 
209b4 20 20 20 20 20 20 22 41 53 43 22 2c 20 20 20 20        "ASC",    
209b5 20 20 20 20 20 0a 20 20 22 41 54 54 41 43 48 22       .  "ATTACH"
209b6 2c 20 20 20 20 20 20 20 20 22 42 45 46 4f 52 45  ,        "BEFORE
209b7 22 2c 20 20 20 20 20 20 20 20 22 43 41 53 43 41  ",        "CASCA
209b8 44 45 22 2c 20 20 20 20 20 20 20 22 43 41 53 54  DE",       "CAST
209b9 22 2c 20 20 20 20 20 20 20 20 0a 20 20 22 43 4f  ",        .  "CO
209ba 4e 46 4c 49 43 54 22 2c 20 20 20 20 20 20 22 44  NFLICT",      "D
209bb 41 54 41 42 41 53 45 22 2c 20 20 20 20 20 20 22  ATABASE",      "
209bc 44 45 53 43 22 2c 20 20 20 20 20 20 20 20 20 20  DESC",          
209bd 22 44 45 54 41 43 48 22 2c 20 20 20 20 20 20 0a  "DETACH",      .
209be 20 20 22 45 41 43 48 22 2c 20 20 20 20 20 20 20    "EACH",       
209bf 20 20 20 22 46 41 49 4c 22 2c 20 20 20 20 20 20     "FAIL",      
209c0 20 20 20 20 22 46 4f 52 22 2c 20 20 20 20 20 20      "FOR",      
209c1 20 20 20 20 20 22 49 47 4e 4f 52 45 22 2c 20 20       "IGNORE",  
209c2 20 20 20 20 0a 20 20 22 49 4e 49 54 49 41 4c 4c      .  "INITIALL
209c3 59 22 2c 20 20 20 20 20 22 49 4e 53 54 45 41 44  Y",     "INSTEAD
209c4 22 2c 20 20 20 20 20 20 20 22 4c 49 4b 45 5f 4b  ",       "LIKE_K
209c5 57 22 2c 20 20 20 20 20 20 20 22 4d 41 54 43 48  W",       "MATCH
209c6 22 2c 20 20 20 20 20 20 20 0a 20 20 22 4b 45 59  ",       .  "KEY
209c7 22 2c 20 20 20 20 20 20 20 20 20 20 20 22 4f 46  ",           "OF
209c8 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 4f  ",            "O
209c9 46 46 53 45 54 22 2c 20 20 20 20 20 20 20 20 22  FFSET",        "
209ca 50 52 41 47 4d 41 22 2c 20 20 20 20 20 20 0a 20  PRAGMA",      . 
209cb 20 22 52 41 49 53 45 22 2c 20 20 20 20 20 20 20   "RAISE",       
209cc 20 20 22 52 45 50 4c 41 43 45 22 2c 20 20 20 20    "REPLACE",    
209cd 20 20 20 22 52 45 53 54 52 49 43 54 22 2c 20 20     "RESTRICT",  
209ce 20 20 20 20 22 52 4f 57 22 2c 20 20 20 20 20 20      "ROW",      
209cf 20 20 20 0a 20 20 22 54 52 49 47 47 45 52 22 2c     .  "TRIGGER",
209d0 20 20 20 20 20 20 20 22 56 41 43 55 55 4d 22 2c         "VACUUM",
209d1 20 20 20 20 20 20 20 20 22 56 49 45 57 22 2c 20          "VIEW", 
209d2 20 20 20 20 20 20 20 20 20 22 56 49 52 54 55 41           "VIRTUA
209d3 4c 22 2c 20 20 20 20 20 0a 20 20 22 52 45 49 4e  L",     .  "REIN
209d4 44 45 58 22 2c 20 20 20 20 20 20 20 22 52 45 4e  DEX",       "REN
209d5 41 4d 45 22 2c 20 20 20 20 20 20 20 20 22 43 54  AME",        "CT
209d6 49 4d 45 5f 4b 57 22 2c 20 20 20 20 20 20 22 41  IME_KW",      "A
209d7 4e 59 22 2c 20 20 20 20 20 20 20 20 20 0a 20 20  NY",         .  
209d8 22 4f 52 22 2c 20 20 20 20 20 20 20 20 20 20 20  "OR",           
209d9 20 22 41 4e 44 22 2c 20 20 20 20 20 20 20 20 20   "AND",         
209da 20 20 22 49 53 22 2c 20 20 20 20 20 20 20 20 20    "IS",         
209db 20 20 20 22 42 45 54 57 45 45 4e 22 2c 20 20 20     "BETWEEN",   
209dc 20 20 0a 20 20 22 49 4e 22 2c 20 20 20 20 20 20    .  "IN",      
209dd 20 20 20 20 20 20 22 49 53 4e 55 4c 4c 22 2c 20        "ISNULL", 
209de 20 20 20 20 20 20 20 22 4e 4f 54 4e 55 4c 4c 22         "NOTNULL"
209df 2c 20 20 20 20 20 20 20 22 4e 45 22 2c 20 20 20  ,       "NE",   
209e0 20 20 20 20 20 20 20 0a 20 20 22 45 51 22 2c 20         .  "EQ", 
209e1 20 20 20 20 20 20 20 20 20 20 20 22 47 54 22 2c             "GT",
209e2 20 20 20 20 20 20 20 20 20 20 20 20 22 4c 45 22              "LE"
209e3 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 4c 54  ,            "LT
209e4 22 2c 20 20 20 20 20 20 20 20 20 20 0a 20 20 22  ",          .  "
209e5 47 45 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  GE",            
209e6 22 45 53 43 41 50 45 22 2c 20 20 20 20 20 20 20  "ESCAPE",       
209e7 20 22 42 49 54 41 4e 44 22 2c 20 20 20 20 20 20   "BITAND",      
209e8 20 20 22 42 49 54 4f 52 22 2c 20 20 20 20 20 20    "BITOR",      
209e9 20 0a 20 20 22 4c 53 48 49 46 54 22 2c 20 20 20   .  "LSHIFT",   
209ea 20 20 20 20 20 22 52 53 48 49 46 54 22 2c 20 20       "RSHIFT",  
209eb 20 20 20 20 20 20 22 50 4c 55 53 22 2c 20 20 20        "PLUS",   
209ec 20 20 20 20 20 20 20 22 4d 49 4e 55 53 22 2c 20         "MINUS", 
209ed 20 20 20 20 20 20 0a 20 20 22 53 54 41 52 22 2c        .  "STAR",
209ee 20 20 20 20 20 20 20 20 20 20 22 53 4c 41 53 48            "SLASH
209ef 22 2c 20 20 20 20 20 20 20 20 20 22 52 45 4d 22  ",         "REM"
209f0 2c 20 20 20 20 20 20 20 20 20 20 20 22 43 4f 4e  ,           "CON
209f1 43 41 54 22 2c 20 20 20 20 20 20 0a 20 20 22 43  CAT",      .  "C
209f2 4f 4c 4c 41 54 45 22 2c 20 20 20 20 20 20 20 22  OLLATE",       "
209f3 55 4d 49 4e 55 53 22 2c 20 20 20 20 20 20 20 20  UMINUS",        
209f4 22 55 50 4c 55 53 22 2c 20 20 20 20 20 20 20 20  "UPLUS",        
209f5 20 22 42 49 54 4e 4f 54 22 2c 20 20 20 20 20 20   "BITNOT",      
209f6 0a 20 20 22 53 54 52 49 4e 47 22 2c 20 20 20 20  .  "STRING",    
209f7 20 20 20 20 22 4a 4f 49 4e 5f 4b 57 22 2c 20 20      "JOIN_KW",  
209f8 20 20 20 20 20 22 43 4f 4e 53 54 52 41 49 4e 54       "CONSTRAINT
209f9 22 2c 20 20 20 20 22 44 45 46 41 55 4c 54 22 2c  ",    "DEFAULT",
209fa 20 20 20 20 20 0a 20 20 22 4e 55 4c 4c 22 2c 20       .  "NULL", 
209fb 20 20 20 20 20 20 20 20 20 22 50 52 49 4d 41 52           "PRIMAR
209fc 59 22 2c 20 20 20 20 20 20 20 22 55 4e 49 51 55  Y",       "UNIQU
209fd 45 22 2c 20 20 20 20 20 20 20 20 22 43 48 45 43  E",        "CHEC
209fe 4b 22 2c 20 20 20 20 20 20 20 0a 20 20 22 52 45  K",       .  "RE
209ff 46 45 52 45 4e 43 45 53 22 2c 20 20 20 20 22 41  FERENCES",    "A
20a00 55 54 4f 49 4e 43 52 22 2c 20 20 20 20 20 20 22  UTOINCR",      "
20a01 4f 4e 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  ON",            
20a02 22 44 45 4c 45 54 45 22 2c 20 20 20 20 20 20 0a  "DELETE",      .
20a03 20 20 22 55 50 44 41 54 45 22 2c 20 20 20 20 20    "UPDATE",     
20a04 20 20 20 22 49 4e 53 45 52 54 22 2c 20 20 20 20     "INSERT",    
20a05 20 20 20 20 22 53 45 54 22 2c 20 20 20 20 20 20      "SET",      
20a06 20 20 20 20 20 22 44 45 46 45 52 52 41 42 4c 45       "DEFERRABLE
20a07 22 2c 20 20 0a 20 20 22 46 4f 52 45 49 47 4e 22  ",  .  "FOREIGN"
20a08 2c 20 20 20 20 20 20 20 22 44 52 4f 50 22 2c 20  ,       "DROP", 
20a09 20 20 20 20 20 20 20 20 20 22 55 4e 49 4f 4e 22           "UNION"
20a0a 2c 20 20 20 20 20 20 20 20 20 22 41 4c 4c 22 2c  ,         "ALL",
20a0b 20 20 20 20 20 20 20 20 20 0a 20 20 22 45 58 43           .  "EXC
20a0c 45 50 54 22 2c 20 20 20 20 20 20 20 20 22 49 4e  EPT",        "IN
20a0d 54 45 52 53 45 43 54 22 2c 20 20 20 20 20 22 53  TERSECT",     "S
20a0e 45 4c 45 43 54 22 2c 20 20 20 20 20 20 20 20 22  ELECT",        "
20a0f 44 49 53 54 49 4e 43 54 22 2c 20 20 20 20 0a 20  DISTINCT",    . 
20a10 20 22 44 4f 54 22 2c 20 20 20 20 20 20 20 20 20   "DOT",         
20a11 20 20 22 46 52 4f 4d 22 2c 20 20 20 20 20 20 20    "FROM",       
20a12 20 20 20 22 4a 4f 49 4e 22 2c 20 20 20 20 20 20     "JOIN",      
20a13 20 20 20 20 22 55 53 49 4e 47 22 2c 20 20 20 20      "USING",    
20a14 20 20 20 0a 20 20 22 4f 52 44 45 52 22 2c 20 20     .  "ORDER",  
20a15 20 20 20 20 20 20 20 22 42 59 22 2c 20 20 20 20         "BY",    
20a16 20 20 20 20 20 20 20 20 22 47 52 4f 55 50 22 2c          "GROUP",
20a17 20 20 20 20 20 20 20 20 20 22 48 41 56 49 4e 47           "HAVING
20a18 22 2c 20 20 20 20 20 20 0a 20 20 22 4c 49 4d 49  ",      .  "LIMI
20a19 54 22 2c 20 20 20 20 20 20 20 20 20 22 57 48 45  T",         "WHE
20a1a 52 45 22 2c 20 20 20 20 20 20 20 20 20 22 49 4e  RE",         "IN
20a1b 54 4f 22 2c 20 20 20 20 20 20 20 20 20 20 22 56  TO",          "V
20a1c 41 4c 55 45 53 22 2c 20 20 20 20 20 20 0a 20 20  ALUES",      .  
20a1d 22 49 4e 54 45 47 45 52 22 2c 20 20 20 20 20 20  "INTEGER",      
20a1e 20 22 46 4c 4f 41 54 22 2c 20 20 20 20 20 20 20   "FLOAT",       
20a1f 20 20 22 42 4c 4f 42 22 2c 20 20 20 20 20 20 20    "BLOB",       
20a20 20 20 20 22 52 45 47 49 53 54 45 52 22 2c 20 20     "REGISTER",  
20a21 20 20 0a 20 20 22 56 41 52 49 41 42 4c 45 22 2c    .  "VARIABLE",
20a22 20 20 20 20 20 20 22 43 41 53 45 22 2c 20 20 20        "CASE",   
20a23 20 20 20 20 20 20 20 22 57 48 45 4e 22 2c 20 20         "WHEN",  
20a24 20 20 20 20 20 20 20 20 22 54 48 45 4e 22 2c 20          "THEN", 
20a25 20 20 20 20 20 20 20 0a 20 20 22 45 4c 53 45 22         .  "ELSE"
20a26 2c 20 20 20 20 20 20 20 20 20 20 22 49 4e 44 45  ,          "INDE
20a27 58 22 2c 20 20 20 20 20 20 20 20 20 22 41 4c 54  X",         "ALT
20a28 45 52 22 2c 20 20 20 20 20 20 20 20 20 22 54 4f  ER",         "TO
20a29 22 2c 20 20 20 20 20 20 20 20 20 20 0a 20 20 22  ",          .  "
20a2a 41 44 44 22 2c 20 20 20 20 20 20 20 20 20 20 20  ADD",           
20a2b 22 43 4f 4c 55 4d 4e 4b 57 22 2c 20 20 20 20 20  "COLUMNKW",     
20a2c 20 22 65 72 72 6f 72 22 2c 20 20 20 20 20 20 20   "error",       
20a2d 20 20 22 69 6e 70 75 74 22 2c 20 20 20 20 20 20    "input",      
20a2e 20 0a 20 20 22 63 6d 64 6c 69 73 74 22 2c 20 20   .  "cmdlist",  
20a2f 20 20 20 20 20 22 65 63 6d 64 22 2c 20 20 20 20       "ecmd",    
20a30 20 20 20 20 20 20 22 63 6d 64 78 22 2c 20 20 20        "cmdx",   
20a31 20 20 20 20 20 20 20 22 63 6d 64 22 2c 20 20 20         "cmd",   
20a32 20 20 20 20 20 20 0a 20 20 22 65 78 70 6c 61 69        .  "explai
20a33 6e 22 2c 20 20 20 20 20 20 20 22 74 72 61 6e 73  n",       "trans
20a34 74 79 70 65 22 2c 20 20 20 20 20 22 74 72 61 6e  type",     "tran
20a35 73 5f 6f 70 74 22 2c 20 20 20 20 20 22 6e 6d 22  s_opt",     "nm"
20a36 2c 20 20 20 20 20 20 20 20 20 20 0a 20 20 22 63  ,          .  "c
20a37 72 65 61 74 65 5f 74 61 62 6c 65 22 2c 20 20 22  reate_table",  "
20a38 63 72 65 61 74 65 5f 74 61 62 6c 65 5f 61 72 67  create_table_arg
20a39 73 22 2c 20 20 22 74 65 6d 70 22 2c 20 20 20 20  s",  "temp",    
20a3a 20 20 20 20 20 20 22 69 66 6e 6f 74 65 78 69 73        "ifnotexis
20a3b 74 73 22 2c 20 0a 20 20 22 64 62 6e 6d 22 2c 20  ts", .  "dbnm", 
20a3c 20 20 20 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e           "column
20a3d 6c 69 73 74 22 2c 20 20 20 20 22 63 6f 6e 73 6c  list",    "consl
20a3e 69 73 74 5f 6f 70 74 22 2c 20 20 22 73 65 6c 65  ist_opt",  "sele
20a3f 63 74 22 2c 20 20 20 20 20 20 0a 20 20 22 63 6f  ct",      .  "co
20a40 6c 75 6d 6e 22 2c 20 20 20 20 20 20 20 20 22 63  lumn",        "c
20a41 6f 6c 75 6d 6e 69 64 22 2c 20 20 20 20 20 20 22  olumnid",      "
20a42 74 79 70 65 22 2c 20 20 20 20 20 20 20 20 20 20  type",          
20a43 22 63 61 72 67 6c 69 73 74 22 2c 20 20 20 20 0a  "carglist",    .
20a44 20 20 22 69 64 22 2c 20 20 20 20 20 20 20 20 20    "id",         
20a45 20 20 20 22 69 64 73 22 2c 20 20 20 20 20 20 20     "ids",       
20a46 20 20 20 20 22 74 79 70 65 74 6f 6b 65 6e 22 2c      "typetoken",
20a47 20 20 20 20 20 22 74 79 70 65 6e 61 6d 65 22 2c       "typename",
20a48 20 20 20 20 0a 20 20 22 73 69 67 6e 65 64 22 2c      .  "signed",
20a49 20 20 20 20 20 20 20 20 22 70 6c 75 73 5f 6e 75          "plus_nu
20a4a 6d 22 2c 20 20 20 20 20 20 22 6d 69 6e 75 73 5f  m",      "minus_
20a4b 6e 75 6d 22 2c 20 20 20 20 20 22 63 61 72 67 22  num",     "carg"
20a4c 2c 20 20 20 20 20 20 20 20 0a 20 20 22 63 63 6f  ,        .  "cco
20a4d 6e 73 22 2c 20 20 20 20 20 20 20 20 20 22 74 65  ns",         "te
20a4e 72 6d 22 2c 20 20 20 20 20 20 20 20 20 20 22 65  rm",          "e
20a4f 78 70 72 22 2c 20 20 20 20 20 20 20 20 20 20 22  xpr",          "
20a50 6f 6e 63 6f 6e 66 22 2c 20 20 20 20 20 20 0a 20  onconf",      . 
20a51 20 22 73 6f 72 74 6f 72 64 65 72 22 2c 20 20 20   "sortorder",   
20a52 20 20 22 61 75 74 6f 69 6e 63 22 2c 20 20 20 20    "autoinc",    
20a53 20 20 20 22 69 64 78 6c 69 73 74 5f 6f 70 74 22     "idxlist_opt"
20a54 2c 20 20 20 22 72 65 66 61 72 67 73 22 2c 20 20  ,   "refargs",  
20a55 20 20 20 0a 20 20 22 64 65 66 65 72 5f 73 75 62     .  "defer_sub
20a56 63 6c 61 75 73 65 22 2c 20 20 22 72 65 66 61 72  clause",  "refar
20a57 67 22 2c 20 20 20 20 20 20 20 20 22 72 65 66 61  g",        "refa
20a58 63 74 22 2c 20 20 20 20 20 20 20 20 22 69 6e 69  ct",        "ini
20a59 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f  t_deferred_pred_
20a5a 6f 70 74 22 2c 0a 20 20 22 63 6f 6e 73 6c 69 73  opt",.  "conslis
20a5b 74 22 2c 20 20 20 20 20 20 22 74 63 6f 6e 73 22  t",      "tcons"
20a5c 2c 20 20 20 20 20 20 20 20 20 22 69 64 78 6c 69  ,         "idxli
20a5d 73 74 22 2c 20 20 20 20 20 20 20 22 64 65 66 65  st",       "defe
20a5e 72 5f 73 75 62 63 6c 61 75 73 65 5f 6f 70 74 22  r_subclause_opt"
20a5f 2c 0a 20 20 22 6f 72 63 6f 6e 66 22 2c 20 20 20  ,.  "orconf",   
20a60 20 20 20 20 20 22 72 65 73 6f 6c 76 65 74 79 70       "resolvetyp
20a61 65 22 2c 20 20 20 22 72 61 69 73 65 74 79 70 65  e",   "raisetype
20a62 22 2c 20 20 20 20 20 22 69 66 65 78 69 73 74 73  ",     "ifexists
20a63 22 2c 20 20 20 20 0a 20 20 22 66 75 6c 6c 6e 61  ",    .  "fullna
20a64 6d 65 22 2c 20 20 20 20 20 20 22 6f 6e 65 73 65  me",      "onese
20a65 6c 65 63 74 22 2c 20 20 20 20 20 22 6d 75 6c 74  lect",     "mult
20a66 69 73 65 6c 65 63 74 5f 6f 70 22 2c 20 20 22 64  iselect_op",  "d
20a67 69 73 74 69 6e 63 74 22 2c 20 20 20 20 0a 20 20  istinct",    .  
20a68 22 73 65 6c 63 6f 6c 6c 69 73 74 22 2c 20 20 20  "selcollist",   
20a69 20 22 66 72 6f 6d 22 2c 20 20 20 20 20 20 20 20   "from",        
20a6a 20 20 22 77 68 65 72 65 5f 6f 70 74 22 2c 20 20    "where_opt",  
20a6b 20 20 20 22 67 72 6f 75 70 62 79 5f 6f 70 74 22     "groupby_opt"
20a6c 2c 20 0a 20 20 22 68 61 76 69 6e 67 5f 6f 70 74  , .  "having_opt
20a6d 22 2c 20 20 20 20 22 6f 72 64 65 72 62 79 5f 6f  ",    "orderby_o
20a6e 70 74 22 2c 20 20 20 22 6c 69 6d 69 74 5f 6f 70  pt",   "limit_op
20a6f 74 22 2c 20 20 20 20 20 22 73 63 6c 70 22 2c 20  t",     "sclp", 
20a70 20 20 20 20 20 20 20 0a 20 20 22 61 73 22 2c 20         .  "as", 
20a71 20 20 20 20 20 20 20 20 20 20 20 22 73 65 6c 74             "selt
20a72 61 62 6c 69 73 74 22 2c 20 20 20 20 22 73 74 6c  ablist",    "stl
20a73 5f 70 72 65 66 69 78 22 2c 20 20 20 20 22 6a 6f  _prefix",    "jo
20a74 69 6e 6f 70 22 2c 20 20 20 20 20 20 0a 20 20 22  inop",      .  "
20a75 6f 6e 5f 6f 70 74 22 2c 20 20 20 20 20 20 20 20  on_opt",        
20a76 22 75 73 69 6e 67 5f 6f 70 74 22 2c 20 20 20 20  "using_opt",    
20a77 20 22 73 65 6c 74 61 62 6c 69 73 74 5f 70 61 72   "seltablist_par
20a78 65 6e 22 2c 20 20 22 6a 6f 69 6e 6f 70 32 22 2c  en",  "joinop2",
20a79 20 20 20 20 20 0a 20 20 22 69 6e 73 63 6f 6c 6c       .  "inscoll
20a7a 69 73 74 22 2c 20 20 20 20 22 73 6f 72 74 6c 69  ist",    "sortli
20a7b 73 74 22 2c 20 20 20 20 20 20 22 73 6f 72 74 69  st",      "sorti
20a7c 74 65 6d 22 2c 20 20 20 20 20 20 22 6e 65 78 70  tem",      "nexp
20a7d 72 6c 69 73 74 22 2c 20 20 20 0a 20 20 22 73 65  rlist",   .  "se
20a7e 74 6c 69 73 74 22 2c 20 20 20 20 20 20 20 22 69  tlist",       "i
20a7f 6e 73 65 72 74 5f 63 6d 64 22 2c 20 20 20 20 22  nsert_cmd",    "
20a80 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 22 2c  inscollist_opt",
20a81 20 20 22 69 74 65 6d 6c 69 73 74 22 2c 20 20 20    "itemlist",   
20a82 20 0a 20 20 22 65 78 70 72 6c 69 73 74 22 2c 20   .  "exprlist", 
20a83 20 20 20 20 20 22 6c 69 6b 65 6f 70 22 2c 20 20       "likeop",  
20a84 20 20 20 20 20 20 22 65 73 63 61 70 65 22 2c 20        "escape", 
20a85 20 20 20 20 20 20 20 22 62 65 74 77 65 65 6e 5f         "between_
20a86 6f 70 22 2c 20 20 0a 20 20 22 69 6e 5f 6f 70 22  op",  .  "in_op"
20a87 2c 20 20 20 20 20 20 20 20 20 22 63 61 73 65 5f  ,         "case_
20a88 6f 70 65 72 61 6e 64 22 2c 20 20 22 63 61 73 65  operand",  "case
20a89 5f 65 78 70 72 6c 69 73 74 22 2c 20 20 22 63 61  _exprlist",  "ca
20a8a 73 65 5f 65 6c 73 65 22 2c 20 20 20 0a 20 20 22  se_else",   .  "
20a8b 75 6e 69 71 75 65 66 6c 61 67 22 2c 20 20 20 20  uniqueflag",    
20a8c 22 69 64 78 69 74 65 6d 22 2c 20 20 20 20 20 20  "idxitem",      
20a8d 20 22 63 6f 6c 6c 61 74 65 22 2c 20 20 20 20 20   "collate",     
20a8e 20 20 22 6e 6d 6e 75 6d 22 2c 20 20 20 20 20 20    "nmnum",      
20a8f 20 0a 20 20 22 70 6c 75 73 5f 6f 70 74 22 2c 20   .  "plus_opt", 
20a90 20 20 20 20 20 22 6e 75 6d 62 65 72 22 2c 20 20       "number",  
20a91 20 20 20 20 20 20 22 74 72 69 67 67 65 72 5f 64        "trigger_d
20a92 65 63 6c 22 2c 20 20 22 74 72 69 67 67 65 72 5f  ecl",  "trigger_
20a93 63 6d 64 5f 6c 69 73 74 22 2c 0a 20 20 22 74 72  cmd_list",.  "tr
20a94 69 67 67 65 72 5f 74 69 6d 65 22 2c 20 20 22 74  igger_time",  "t
20a95 72 69 67 67 65 72 5f 65 76 65 6e 74 22 2c 20 20  rigger_event",  
20a96 22 66 6f 72 65 61 63 68 5f 63 6c 61 75 73 65 22  "foreach_clause"
20a97 2c 20 20 22 77 68 65 6e 5f 63 6c 61 75 73 65 22  ,  "when_clause"
20a98 2c 20 0a 20 20 22 74 72 69 67 67 65 72 5f 63 6d  , .  "trigger_cm
20a99 64 22 2c 20 20 20 22 64 61 74 61 62 61 73 65 5f  d",   "database_
20a9a 6b 77 5f 6f 70 74 22 2c 20 20 22 6b 65 79 5f 6f  kw_opt",  "key_o
20a9b 70 74 22 2c 20 20 20 20 20 20 20 22 61 64 64 5f  pt",       "add_
20a9c 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61 6d 65 22  column_fullname"
20a9d 2c 0a 20 20 22 6b 77 63 6f 6c 75 6d 6e 5f 6f 70  ,.  "kwcolumn_op
20a9e 74 22 2c 20 20 22 63 72 65 61 74 65 5f 76 74 61  t",  "create_vta
20a9f 62 22 2c 20 20 20 22 76 74 61 62 61 72 67 6c 69  b",   "vtabargli
20aa0 73 74 22 2c 20 20 20 22 76 74 61 62 61 72 67 22  st",   "vtabarg"
20aa1 2c 20 20 20 20 20 0a 20 20 22 76 74 61 62 61 72  ,     .  "vtabar
20aa2 67 74 6f 6b 65 6e 22 2c 20 20 22 6c 70 22 2c 20  gtoken",  "lp", 
20aa3 20 20 20 20 20 20 20 20 20 20 20 22 61 6e 79 6c             "anyl
20aa4 69 73 74 22 2c 20 20 20 20 20 0a 7d 3b 0a 23 65  ist",     .};.#e
20aa5 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a  ndif /* NDEBUG *
20aa6 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  /..#ifndef NDEBU
20aa7 47 0a 2f 2a 20 46 6f 72 20 74 72 61 63 69 6e 67  G./* For tracing
20aa8 20 72 65 64 75 63 65 20 61 63 74 69 6f 6e 73 2c   reduce actions,
20aa9 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 61 6c   the names of al
20aaa 6c 20 72 75 6c 65 73 20 61 72 65 20 72 65 71 75  l rules are requ
20aab 69 72 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ired..*/.static 
20aac 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73  const char *cons
20aad 74 20 79 79 52 75 6c 65 4e 61 6d 65 5b 5d 20 3d  t yyRuleName[] =
20aae 20 7b 0a 20 2f 2a 20 20 20 30 20 2a 2f 20 22 69   {. /*   0 */ "i
20aaf 6e 70 75 74 20 3a 3a 3d 20 63 6d 64 6c 69 73 74  nput ::= cmdlist
20ab0 22 2c 0a 20 2f 2a 20 20 20 31 20 2a 2f 20 22 63  ",. /*   1 */ "c
20ab1 6d 64 6c 69 73 74 20 3a 3a 3d 20 63 6d 64 6c 69  mdlist ::= cmdli
20ab2 73 74 20 65 63 6d 64 22 2c 0a 20 2f 2a 20 20 20  st ecmd",. /*   
20ab3 32 20 2a 2f 20 22 63 6d 64 6c 69 73 74 20 3a 3a  2 */ "cmdlist ::
20ab4 3d 20 65 63 6d 64 22 2c 0a 20 2f 2a 20 20 20 33  = ecmd",. /*   3
20ab5 20 2a 2f 20 22 63 6d 64 78 20 3a 3a 3d 20 63 6d   */ "cmdx ::= cm
20ab6 64 22 2c 0a 20 2f 2a 20 20 20 34 20 2a 2f 20 22  d",. /*   4 */ "
20ab7 65 63 6d 64 20 3a 3a 3d 20 53 45 4d 49 22 2c 0a  ecmd ::= SEMI",.
20ab8 20 2f 2a 20 20 20 35 20 2a 2f 20 22 65 63 6d 64   /*   5 */ "ecmd
20ab9 20 3a 3a 3d 20 65 78 70 6c 61 69 6e 20 63 6d 64   ::= explain cmd
20aba 78 20 53 45 4d 49 22 2c 0a 20 2f 2a 20 20 20 36  x SEMI",. /*   6
20abb 20 2a 2f 20 22 65 78 70 6c 61 69 6e 20 3a 3a 3d   */ "explain ::=
20abc 22 2c 0a 20 2f 2a 20 20 20 37 20 2a 2f 20 22 65  ",. /*   7 */ "e
20abd 78 70 6c 61 69 6e 20 3a 3a 3d 20 45 58 50 4c 41  xplain ::= EXPLA
20abe 49 4e 22 2c 0a 20 2f 2a 20 20 20 38 20 2a 2f 20  IN",. /*   8 */ 
20abf 22 65 78 70 6c 61 69 6e 20 3a 3a 3d 20 45 58 50  "explain ::= EXP
20ac0 4c 41 49 4e 20 51 55 45 52 59 20 50 4c 41 4e 22  LAIN QUERY PLAN"
20ac1 2c 0a 20 2f 2a 20 20 20 39 20 2a 2f 20 22 63 6d  ,. /*   9 */ "cm
20ac2 64 20 3a 3a 3d 20 42 45 47 49 4e 20 74 72 61 6e  d ::= BEGIN tran
20ac3 73 74 79 70 65 20 74 72 61 6e 73 5f 6f 70 74 22  stype trans_opt"
20ac4 2c 0a 20 2f 2a 20 20 31 30 20 2a 2f 20 22 74 72  ,. /*  10 */ "tr
20ac5 61 6e 73 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f  ans_opt ::=",. /
20ac6 2a 20 20 31 31 20 2a 2f 20 22 74 72 61 6e 73 5f  *  11 */ "trans_
20ac7 6f 70 74 20 3a 3a 3d 20 54 52 41 4e 53 41 43 54  opt ::= TRANSACT
20ac8 49 4f 4e 22 2c 0a 20 2f 2a 20 20 31 32 20 2a 2f  ION",. /*  12 */
20ac9 20 22 74 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d 20   "trans_opt ::= 
20aca 54 52 41 4e 53 41 43 54 49 4f 4e 20 6e 6d 22 2c  TRANSACTION nm",
20acb 0a 20 2f 2a 20 20 31 33 20 2a 2f 20 22 74 72 61  . /*  13 */ "tra
20acc 6e 73 74 79 70 65 20 3a 3a 3d 22 2c 0a 20 2f 2a  nstype ::=",. /*
20acd 20 20 31 34 20 2a 2f 20 22 74 72 61 6e 73 74 79    14 */ "transty
20ace 70 65 20 3a 3a 3d 20 44 45 46 45 52 52 45 44 22  pe ::= DEFERRED"
20acf 2c 0a 20 2f 2a 20 20 31 35 20 2a 2f 20 22 74 72  ,. /*  15 */ "tr
20ad0 61 6e 73 74 79 70 65 20 3a 3a 3d 20 49 4d 4d 45  anstype ::= IMME
20ad1 44 49 41 54 45 22 2c 0a 20 2f 2a 20 20 31 36 20  DIATE",. /*  16 
20ad2 2a 2f 20 22 74 72 61 6e 73 74 79 70 65 20 3a 3a  */ "transtype ::
20ad3 3d 20 45 58 43 4c 55 53 49 56 45 22 2c 0a 20 2f  = EXCLUSIVE",. /
20ad4 2a 20 20 31 37 20 2a 2f 20 22 63 6d 64 20 3a 3a  *  17 */ "cmd ::
20ad5 3d 20 43 4f 4d 4d 49 54 20 74 72 61 6e 73 5f 6f  = COMMIT trans_o
20ad6 70 74 22 2c 0a 20 2f 2a 20 20 31 38 20 2a 2f 20  pt",. /*  18 */ 
20ad7 22 63 6d 64 20 3a 3a 3d 20 45 4e 44 20 74 72 61  "cmd ::= END tra
20ad8 6e 73 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 31 39  ns_opt",. /*  19
20ad9 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 52 4f 4c   */ "cmd ::= ROL
20ada 4c 42 41 43 4b 20 74 72 61 6e 73 5f 6f 70 74 22  LBACK trans_opt"
20adb 2c 0a 20 2f 2a 20 20 32 30 20 2a 2f 20 22 63 6d  ,. /*  20 */ "cm
20adc 64 20 3a 3a 3d 20 63 72 65 61 74 65 5f 74 61 62  d ::= create_tab
20add 6c 65 20 63 72 65 61 74 65 5f 74 61 62 6c 65 5f  le create_table_
20ade 61 72 67 73 22 2c 0a 20 2f 2a 20 20 32 31 20 2a  args",. /*  21 *
20adf 2f 20 22 63 72 65 61 74 65 5f 74 61 62 6c 65 20  / "create_table 
20ae0 3a 3a 3d 20 43 52 45 41 54 45 20 74 65 6d 70 20  ::= CREATE temp 
20ae1 54 41 42 4c 45 20 69 66 6e 6f 74 65 78 69 73 74  TABLE ifnotexist
20ae2 73 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a 20  s nm dbnm",. /* 
20ae3 20 32 32 20 2a 2f 20 22 69 66 6e 6f 74 65 78 69   22 */ "ifnotexi
20ae4 73 74 73 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 32  sts ::=",. /*  2
20ae5 33 20 2a 2f 20 22 69 66 6e 6f 74 65 78 69 73 74  3 */ "ifnotexist
20ae6 73 20 3a 3a 3d 20 49 46 20 4e 4f 54 20 45 58 49  s ::= IF NOT EXI
20ae7 53 54 53 22 2c 0a 20 2f 2a 20 20 32 34 20 2a 2f  STS",. /*  24 */
20ae8 20 22 74 65 6d 70 20 3a 3a 3d 20 54 45 4d 50 22   "temp ::= TEMP"
20ae9 2c 0a 20 2f 2a 20 20 32 35 20 2a 2f 20 22 74 65  ,. /*  25 */ "te
20aea 6d 70 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 32 36  mp ::=",. /*  26
20aeb 20 2a 2f 20 22 63 72 65 61 74 65 5f 74 61 62 6c   */ "create_tabl
20aec 65 5f 61 72 67 73 20 3a 3a 3d 20 4c 50 20 63 6f  e_args ::= LP co
20aed 6c 75 6d 6e 6c 69 73 74 20 63 6f 6e 73 6c 69 73  lumnlist conslis
20aee 74 5f 6f 70 74 20 52 50 22 2c 0a 20 2f 2a 20 20  t_opt RP",. /*  
20aef 32 37 20 2a 2f 20 22 63 72 65 61 74 65 5f 74 61  27 */ "create_ta
20af0 62 6c 65 5f 61 72 67 73 20 3a 3a 3d 20 41 53 20  ble_args ::= AS 
20af1 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 20 32 38  select",. /*  28
20af2 20 2a 2f 20 22 63 6f 6c 75 6d 6e 6c 69 73 74 20   */ "columnlist 
20af3 3a 3a 3d 20 63 6f 6c 75 6d 6e 6c 69 73 74 20 43  ::= columnlist C
20af4 4f 4d 4d 41 20 63 6f 6c 75 6d 6e 22 2c 0a 20 2f  OMMA column",. /
20af5 2a 20 20 32 39 20 2a 2f 20 22 63 6f 6c 75 6d 6e  *  29 */ "column
20af6 6c 69 73 74 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 22  list ::= column"
20af7 2c 0a 20 2f 2a 20 20 33 30 20 2a 2f 20 22 63 6f  ,. /*  30 */ "co
20af8 6c 75 6d 6e 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 69  lumn ::= columni
20af9 64 20 74 79 70 65 20 63 61 72 67 6c 69 73 74 22  d type carglist"
20afa 2c 0a 20 2f 2a 20 20 33 31 20 2a 2f 20 22 63 6f  ,. /*  31 */ "co
20afb 6c 75 6d 6e 69 64 20 3a 3a 3d 20 6e 6d 22 2c 0a  lumnid ::= nm",.
20afc 20 2f 2a 20 20 33 32 20 2a 2f 20 22 69 64 20 3a   /*  32 */ "id :
20afd 3a 3d 20 49 44 22 2c 0a 20 2f 2a 20 20 33 33 20  := ID",. /*  33 
20afe 2a 2f 20 22 69 64 73 20 3a 3a 3d 20 49 44 7c 53  */ "ids ::= ID|S
20aff 54 52 49 4e 47 22 2c 0a 20 2f 2a 20 20 33 34 20  TRING",. /*  34 
20b00 2a 2f 20 22 6e 6d 20 3a 3a 3d 20 49 44 22 2c 0a  */ "nm ::= ID",.
20b01 20 2f 2a 20 20 33 35 20 2a 2f 20 22 6e 6d 20 3a   /*  35 */ "nm :
20b02 3a 3d 20 53 54 52 49 4e 47 22 2c 0a 20 2f 2a 20  := STRING",. /* 
20b03 20 33 36 20 2a 2f 20 22 6e 6d 20 3a 3a 3d 20 4a   36 */ "nm ::= J
20b04 4f 49 4e 5f 4b 57 22 2c 0a 20 2f 2a 20 20 33 37  OIN_KW",. /*  37
20b05 20 2a 2f 20 22 74 79 70 65 20 3a 3a 3d 22 2c 0a   */ "type ::=",.
20b06 20 2f 2a 20 20 33 38 20 2a 2f 20 22 74 79 70 65   /*  38 */ "type
20b07 20 3a 3a 3d 20 74 79 70 65 74 6f 6b 65 6e 22 2c   ::= typetoken",
20b08 0a 20 2f 2a 20 20 33 39 20 2a 2f 20 22 74 79 70  . /*  39 */ "typ
20b09 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70 65 6e  etoken ::= typen
20b0a 61 6d 65 22 2c 0a 20 2f 2a 20 20 34 30 20 2a 2f  ame",. /*  40 */
20b0b 20 22 74 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20   "typetoken ::= 
20b0c 74 79 70 65 6e 61 6d 65 20 4c 50 20 73 69 67 6e  typename LP sign
20b0d 65 64 20 52 50 22 2c 0a 20 2f 2a 20 20 34 31 20  ed RP",. /*  41 
20b0e 2a 2f 20 22 74 79 70 65 74 6f 6b 65 6e 20 3a 3a  */ "typetoken ::
20b0f 3d 20 74 79 70 65 6e 61 6d 65 20 4c 50 20 73 69  = typename LP si
20b10 67 6e 65 64 20 43 4f 4d 4d 41 20 73 69 67 6e 65  gned COMMA signe
20b11 64 20 52 50 22 2c 0a 20 2f 2a 20 20 34 32 20 2a  d RP",. /*  42 *
20b12 2f 20 22 74 79 70 65 6e 61 6d 65 20 3a 3a 3d 20  / "typename ::= 
20b13 69 64 73 22 2c 0a 20 2f 2a 20 20 34 33 20 2a 2f  ids",. /*  43 */
20b14 20 22 74 79 70 65 6e 61 6d 65 20 3a 3a 3d 20 74   "typename ::= t
20b15 79 70 65 6e 61 6d 65 20 69 64 73 22 2c 0a 20 2f  ypename ids",. /
20b16 2a 20 20 34 34 20 2a 2f 20 22 73 69 67 6e 65 64  *  44 */ "signed
20b17 20 3a 3a 3d 20 70 6c 75 73 5f 6e 75 6d 22 2c 0a   ::= plus_num",.
20b18 20 2f 2a 20 20 34 35 20 2a 2f 20 22 73 69 67 6e   /*  45 */ "sign
20b19 65 64 20 3a 3a 3d 20 6d 69 6e 75 73 5f 6e 75 6d  ed ::= minus_num
20b1a 22 2c 0a 20 2f 2a 20 20 34 36 20 2a 2f 20 22 63  ",. /*  46 */ "c
20b1b 61 72 67 6c 69 73 74 20 3a 3a 3d 20 63 61 72 67  arglist ::= carg
20b1c 6c 69 73 74 20 63 61 72 67 22 2c 0a 20 2f 2a 20  list carg",. /* 
20b1d 20 34 37 20 2a 2f 20 22 63 61 72 67 6c 69 73 74   47 */ "carglist
20b1e 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 34 38 20 2a   ::=",. /*  48 *
20b1f 2f 20 22 63 61 72 67 20 3a 3a 3d 20 43 4f 4e 53  / "carg ::= CONS
20b20 54 52 41 49 4e 54 20 6e 6d 20 63 63 6f 6e 73 22  TRAINT nm ccons"
20b21 2c 0a 20 2f 2a 20 20 34 39 20 2a 2f 20 22 63 61  ,. /*  49 */ "ca
20b22 72 67 20 3a 3a 3d 20 63 63 6f 6e 73 22 2c 0a 20  rg ::= ccons",. 
20b23 2f 2a 20 20 35 30 20 2a 2f 20 22 63 63 6f 6e 73  /*  50 */ "ccons
20b24 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 74 65 72   ::= DEFAULT ter
20b25 6d 22 2c 0a 20 2f 2a 20 20 35 31 20 2a 2f 20 22  m",. /*  51 */ "
20b26 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c  ccons ::= DEFAUL
20b27 54 20 4c 50 20 65 78 70 72 20 52 50 22 2c 0a 20  T LP expr RP",. 
20b28 2f 2a 20 20 35 32 20 2a 2f 20 22 63 63 6f 6e 73  /*  52 */ "ccons
20b29 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 50 4c 55   ::= DEFAULT PLU
20b2a 53 20 74 65 72 6d 22 2c 0a 20 2f 2a 20 20 35 33  S term",. /*  53
20b2b 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 44   */ "ccons ::= D
20b2c 45 46 41 55 4c 54 20 4d 49 4e 55 53 20 74 65 72  EFAULT MINUS ter
20b2d 6d 22 2c 0a 20 2f 2a 20 20 35 34 20 2a 2f 20 22  m",. /*  54 */ "
20b2e 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c  ccons ::= DEFAUL
20b2f 54 20 69 64 22 2c 0a 20 2f 2a 20 20 35 35 20 2a  T id",. /*  55 *
20b30 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 4e 55 4c  / "ccons ::= NUL
20b31 4c 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20  L onconf",. /*  
20b32 35 36 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d  56 */ "ccons ::=
20b33 20 4e 4f 54 20 4e 55 4c 4c 20 6f 6e 63 6f 6e 66   NOT NULL onconf
20b34 22 2c 0a 20 2f 2a 20 20 35 37 20 2a 2f 20 22 63  ",. /*  57 */ "c
20b35 63 6f 6e 73 20 3a 3a 3d 20 50 52 49 4d 41 52 59  cons ::= PRIMARY
20b36 20 4b 45 59 20 73 6f 72 74 6f 72 64 65 72 20 6f   KEY sortorder o
20b37 6e 63 6f 6e 66 20 61 75 74 6f 69 6e 63 22 2c 0a  nconf autoinc",.
20b38 20 2f 2a 20 20 35 38 20 2a 2f 20 22 63 63 6f 6e   /*  58 */ "ccon
20b39 73 20 3a 3a 3d 20 55 4e 49 51 55 45 20 6f 6e 63  s ::= UNIQUE onc
20b3a 6f 6e 66 22 2c 0a 20 2f 2a 20 20 35 39 20 2a 2f  onf",. /*  59 */
20b3b 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 43 48 45 43   "ccons ::= CHEC
20b3c 4b 20 4c 50 20 65 78 70 72 20 52 50 22 2c 0a 20  K LP expr RP",. 
20b3d 2f 2a 20 20 36 30 20 2a 2f 20 22 63 63 6f 6e 73  /*  60 */ "ccons
20b3e 20 3a 3a 3d 20 52 45 46 45 52 45 4e 43 45 53 20   ::= REFERENCES 
20b3f 6e 6d 20 69 64 78 6c 69 73 74 5f 6f 70 74 20 72  nm idxlist_opt r
20b40 65 66 61 72 67 73 22 2c 0a 20 2f 2a 20 20 36 31  efargs",. /*  61
20b41 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 64   */ "ccons ::= d
20b42 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 22 2c  efer_subclause",
20b43 0a 20 2f 2a 20 20 36 32 20 2a 2f 20 22 63 63 6f  . /*  62 */ "cco
20b44 6e 73 20 3a 3a 3d 20 43 4f 4c 4c 41 54 45 20 69  ns ::= COLLATE i
20b45 64 22 2c 0a 20 2f 2a 20 20 36 33 20 2a 2f 20 22  d",. /*  63 */ "
20b46 61 75 74 6f 69 6e 63 20 3a 3a 3d 22 2c 0a 20 2f  autoinc ::=",. /
20b47 2a 20 20 36 34 20 2a 2f 20 22 61 75 74 6f 69 6e  *  64 */ "autoin
20b48 63 20 3a 3a 3d 20 41 55 54 4f 49 4e 43 52 22 2c  c ::= AUTOINCR",
20b49 0a 20 2f 2a 20 20 36 35 20 2a 2f 20 22 72 65 66  . /*  65 */ "ref
20b4a 61 72 67 73 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20  args ::=",. /*  
20b4b 36 36 20 2a 2f 20 22 72 65 66 61 72 67 73 20 3a  66 */ "refargs :
20b4c 3a 3d 20 72 65 66 61 72 67 73 20 72 65 66 61 72  := refargs refar
20b4d 67 22 2c 0a 20 2f 2a 20 20 36 37 20 2a 2f 20 22  g",. /*  67 */ "
20b4e 72 65 66 61 72 67 20 3a 3a 3d 20 4d 41 54 43 48  refarg ::= MATCH
20b4f 20 6e 6d 22 2c 0a 20 2f 2a 20 20 36 38 20 2a 2f   nm",. /*  68 */
20b50 20 22 72 65 66 61 72 67 20 3a 3a 3d 20 4f 4e 20   "refarg ::= ON 
20b51 44 45 4c 45 54 45 20 72 65 66 61 63 74 22 2c 0a  DELETE refact",.
20b52 20 2f 2a 20 20 36 39 20 2a 2f 20 22 72 65 66 61   /*  69 */ "refa
20b53 72 67 20 3a 3a 3d 20 4f 4e 20 55 50 44 41 54 45  rg ::= ON UPDATE
20b54 20 72 65 66 61 63 74 22 2c 0a 20 2f 2a 20 20 37   refact",. /*  7
20b55 30 20 2a 2f 20 22 72 65 66 61 72 67 20 3a 3a 3d  0 */ "refarg ::=
20b56 20 4f 4e 20 49 4e 53 45 52 54 20 72 65 66 61 63   ON INSERT refac
20b57 74 22 2c 0a 20 2f 2a 20 20 37 31 20 2a 2f 20 22  t",. /*  71 */ "
20b58 72 65 66 61 63 74 20 3a 3a 3d 20 53 45 54 20 4e  refact ::= SET N
20b59 55 4c 4c 22 2c 0a 20 2f 2a 20 20 37 32 20 2a 2f  ULL",. /*  72 */
20b5a 20 22 72 65 66 61 63 74 20 3a 3a 3d 20 53 45 54   "refact ::= SET
20b5b 20 44 45 46 41 55 4c 54 22 2c 0a 20 2f 2a 20 20   DEFAULT",. /*  
20b5c 37 33 20 2a 2f 20 22 72 65 66 61 63 74 20 3a 3a  73 */ "refact ::
20b5d 3d 20 43 41 53 43 41 44 45 22 2c 0a 20 2f 2a 20  = CASCADE",. /* 
20b5e 20 37 34 20 2a 2f 20 22 72 65 66 61 63 74 20 3a   74 */ "refact :
20b5f 3a 3d 20 52 45 53 54 52 49 43 54 22 2c 0a 20 2f  := RESTRICT",. /
20b60 2a 20 20 37 35 20 2a 2f 20 22 64 65 66 65 72 5f  *  75 */ "defer_
20b61 73 75 62 63 6c 61 75 73 65 20 3a 3a 3d 20 4e 4f  subclause ::= NO
20b62 54 20 44 45 46 45 52 52 41 42 4c 45 20 69 6e 69  T DEFERRABLE ini
20b63 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f  t_deferred_pred_
20b64 6f 70 74 22 2c 0a 20 2f 2a 20 20 37 36 20 2a 2f  opt",. /*  76 */
20b65 20 22 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73   "defer_subclaus
20b66 65 20 3a 3a 3d 20 44 45 46 45 52 52 41 42 4c 45  e ::= DEFERRABLE
20b67 20 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70   init_deferred_p
20b68 72 65 64 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 37  red_opt",. /*  7
20b69 37 20 2a 2f 20 22 69 6e 69 74 5f 64 65 66 65 72  7 */ "init_defer
20b6a 72 65 64 5f 70 72 65 64 5f 6f 70 74 20 3a 3a 3d  red_pred_opt ::=
20b6b 22 2c 0a 20 2f 2a 20 20 37 38 20 2a 2f 20 22 69  ",. /*  78 */ "i
20b6c 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65  nit_deferred_pre
20b6d 64 5f 6f 70 74 20 3a 3a 3d 20 49 4e 49 54 49 41  d_opt ::= INITIA
20b6e 4c 4c 59 20 44 45 46 45 52 52 45 44 22 2c 0a 20  LLY DEFERRED",. 
20b6f 2f 2a 20 20 37 39 20 2a 2f 20 22 69 6e 69 74 5f  /*  79 */ "init_
20b70 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70  deferred_pred_op
20b71 74 20 3a 3a 3d 20 49 4e 49 54 49 41 4c 4c 59 20  t ::= INITIALLY 
20b72 49 4d 4d 45 44 49 41 54 45 22 2c 0a 20 2f 2a 20  IMMEDIATE",. /* 
20b73 20 38 30 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74   80 */ "conslist
20b74 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20  _opt ::=",. /*  
20b75 38 31 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 5f  81 */ "conslist_
20b76 6f 70 74 20 3a 3a 3d 20 43 4f 4d 4d 41 20 63 6f  opt ::= COMMA co
20b77 6e 73 6c 69 73 74 22 2c 0a 20 2f 2a 20 20 38 32  nslist",. /*  82
20b78 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 20 3a 3a   */ "conslist ::
20b79 3d 20 63 6f 6e 73 6c 69 73 74 20 43 4f 4d 4d 41  = conslist COMMA
20b7a 20 74 63 6f 6e 73 22 2c 0a 20 2f 2a 20 20 38 33   tcons",. /*  83
20b7b 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 20 3a 3a   */ "conslist ::
20b7c 3d 20 63 6f 6e 73 6c 69 73 74 20 74 63 6f 6e 73  = conslist tcons
20b7d 22 2c 0a 20 2f 2a 20 20 38 34 20 2a 2f 20 22 63  ",. /*  84 */ "c
20b7e 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20 74 63 6f 6e  onslist ::= tcon
20b7f 73 22 2c 0a 20 2f 2a 20 20 38 35 20 2a 2f 20 22  s",. /*  85 */ "
20b80 74 63 6f 6e 73 20 3a 3a 3d 20 43 4f 4e 53 54 52  tcons ::= CONSTR
20b81 41 49 4e 54 20 6e 6d 22 2c 0a 20 2f 2a 20 20 38  AINT nm",. /*  8
20b82 36 20 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d 20  6 */ "tcons ::= 
20b83 50 52 49 4d 41 52 59 20 4b 45 59 20 4c 50 20 69  PRIMARY KEY LP i
20b84 64 78 6c 69 73 74 20 61 75 74 6f 69 6e 63 20 52  dxlist autoinc R
20b85 50 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20  P onconf",. /*  
20b86 38 37 20 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d  87 */ "tcons ::=
20b87 20 55 4e 49 51 55 45 20 4c 50 20 69 64 78 6c 69   UNIQUE LP idxli
20b88 73 74 20 52 50 20 6f 6e 63 6f 6e 66 22 2c 0a 20  st RP onconf",. 
20b89 2f 2a 20 20 38 38 20 2a 2f 20 22 74 63 6f 6e 73  /*  88 */ "tcons
20b8a 20 3a 3a 3d 20 43 48 45 43 4b 20 4c 50 20 65 78   ::= CHECK LP ex
20b8b 70 72 20 52 50 20 6f 6e 63 6f 6e 66 22 2c 0a 20  pr RP onconf",. 
20b8c 2f 2a 20 20 38 39 20 2a 2f 20 22 74 63 6f 6e 73  /*  89 */ "tcons
20b8d 20 3a 3a 3d 20 46 4f 52 45 49 47 4e 20 4b 45 59   ::= FOREIGN KEY
20b8e 20 4c 50 20 69 64 78 6c 69 73 74 20 52 50 20 52   LP idxlist RP R
20b8f 45 46 45 52 45 4e 43 45 53 20 6e 6d 20 69 64 78  EFERENCES nm idx
20b90 6c 69 73 74 5f 6f 70 74 20 72 65 66 61 72 67 73  list_opt refargs
20b91 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65   defer_subclause
20b92 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 39 30 20 2a  _opt",. /*  90 *
20b93 2f 20 22 64 65 66 65 72 5f 73 75 62 63 6c 61 75  / "defer_subclau
20b94 73 65 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a  se_opt ::=",. /*
20b95 20 20 39 31 20 2a 2f 20 22 64 65 66 65 72 5f 73    91 */ "defer_s
20b96 75 62 63 6c 61 75 73 65 5f 6f 70 74 20 3a 3a 3d  ubclause_opt ::=
20b97 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65   defer_subclause
20b98 22 2c 0a 20 2f 2a 20 20 39 32 20 2a 2f 20 22 6f  ",. /*  92 */ "o
20b99 6e 63 6f 6e 66 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  nconf ::=",. /* 
20b9a 20 39 33 20 2a 2f 20 22 6f 6e 63 6f 6e 66 20 3a   93 */ "onconf :
20b9b 3a 3d 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 72  := ON CONFLICT r
20b9c 65 73 6f 6c 76 65 74 79 70 65 22 2c 0a 20 2f 2a  esolvetype",. /*
20b9d 20 20 39 34 20 2a 2f 20 22 6f 72 63 6f 6e 66 20    94 */ "orconf 
20b9e 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 39 35 20 2a 2f  ::=",. /*  95 */
20b9f 20 22 6f 72 63 6f 6e 66 20 3a 3a 3d 20 4f 52 20   "orconf ::= OR 
20ba0 72 65 73 6f 6c 76 65 74 79 70 65 22 2c 0a 20 2f  resolvetype",. /
20ba1 2a 20 20 39 36 20 2a 2f 20 22 72 65 73 6f 6c 76  *  96 */ "resolv
20ba2 65 74 79 70 65 20 3a 3a 3d 20 72 61 69 73 65 74  etype ::= raiset
20ba3 79 70 65 22 2c 0a 20 2f 2a 20 20 39 37 20 2a 2f  ype",. /*  97 */
20ba4 20 22 72 65 73 6f 6c 76 65 74 79 70 65 20 3a 3a   "resolvetype ::
20ba5 3d 20 49 47 4e 4f 52 45 22 2c 0a 20 2f 2a 20 20  = IGNORE",. /*  
20ba6 39 38 20 2a 2f 20 22 72 65 73 6f 6c 76 65 74 79  98 */ "resolvety
20ba7 70 65 20 3a 3a 3d 20 52 45 50 4c 41 43 45 22 2c  pe ::= REPLACE",
20ba8 0a 20 2f 2a 20 20 39 39 20 2a 2f 20 22 63 6d 64  . /*  99 */ "cmd
20ba9 20 3a 3a 3d 20 44 52 4f 50 20 54 41 42 4c 45 20   ::= DROP TABLE 
20baa 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d  ifexists fullnam
20bab 65 22 2c 0a 20 2f 2a 20 31 30 30 20 2a 2f 20 22  e",. /* 100 */ "
20bac 69 66 65 78 69 73 74 73 20 3a 3a 3d 20 49 46 20  ifexists ::= IF 
20bad 45 58 49 53 54 53 22 2c 0a 20 2f 2a 20 31 30 31  EXISTS",. /* 101
20bae 20 2a 2f 20 22 69 66 65 78 69 73 74 73 20 3a 3a   */ "ifexists ::
20baf 3d 22 2c 0a 20 2f 2a 20 31 30 32 20 2a 2f 20 22  =",. /* 102 */ "
20bb0 63 6d 64 20 3a 3a 3d 20 43 52 45 41 54 45 20 74  cmd ::= CREATE t
20bb1 65 6d 70 20 56 49 45 57 20 69 66 6e 6f 74 65 78  emp VIEW ifnotex
20bb2 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20 41 53 20  ists nm dbnm AS 
20bb3 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31 30 33  select",. /* 103
20bb4 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44 52 4f   */ "cmd ::= DRO
20bb5 50 20 56 49 45 57 20 69 66 65 78 69 73 74 73 20  P VIEW ifexists 
20bb6 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20 2f 2a 20 31  fullname",. /* 1
20bb7 30 34 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 73  04 */ "cmd ::= s
20bb8 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31 30 35 20  elect",. /* 105 
20bb9 2a 2f 20 22 73 65 6c 65 63 74 20 3a 3a 3d 20 6f  */ "select ::= o
20bba 6e 65 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31  neselect",. /* 1
20bbb 30 36 20 2a 2f 20 22 73 65 6c 65 63 74 20 3a 3a  06 */ "select ::
20bbc 3d 20 73 65 6c 65 63 74 20 6d 75 6c 74 69 73 65  = select multise
20bbd 6c 65 63 74 5f 6f 70 20 6f 6e 65 73 65 6c 65 63  lect_op oneselec
20bbe 74 22 2c 0a 20 2f 2a 20 31 30 37 20 2a 2f 20 22  t",. /* 107 */ "
20bbf 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20 3a  multiselect_op :
20bc0 3a 3d 20 55 4e 49 4f 4e 22 2c 0a 20 2f 2a 20 31  := UNION",. /* 1
20bc1 30 38 20 2a 2f 20 22 6d 75 6c 74 69 73 65 6c 65  08 */ "multisele
20bc2 63 74 5f 6f 70 20 3a 3a 3d 20 55 4e 49 4f 4e 20  ct_op ::= UNION 
20bc3 41 4c 4c 22 2c 0a 20 2f 2a 20 31 30 39 20 2a 2f  ALL",. /* 109 */
20bc4 20 22 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70   "multiselect_op
20bc5 20 3a 3a 3d 20 45 58 43 45 50 54 7c 49 4e 54 45   ::= EXCEPT|INTE
20bc6 52 53 45 43 54 22 2c 0a 20 2f 2a 20 31 31 30 20  RSECT",. /* 110 
20bc7 2a 2f 20 22 6f 6e 65 73 65 6c 65 63 74 20 3a 3a  */ "oneselect ::
20bc8 3d 20 53 45 4c 45 43 54 20 64 69 73 74 69 6e 63  = SELECT distinc
20bc9 74 20 73 65 6c 63 6f 6c 6c 69 73 74 20 66 72 6f  t selcollist fro
20bca 6d 20 77 68 65 72 65 5f 6f 70 74 20 67 72 6f 75  m where_opt grou
20bcb 70 62 79 5f 6f 70 74 20 68 61 76 69 6e 67 5f 6f  pby_opt having_o
20bcc 70 74 20 6f 72 64 65 72 62 79 5f 6f 70 74 20 6c  pt orderby_opt l
20bcd 69 6d 69 74 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31  imit_opt",. /* 1
20bce 31 31 20 2a 2f 20 22 64 69 73 74 69 6e 63 74 20  11 */ "distinct 
20bcf 3a 3a 3d 20 44 49 53 54 49 4e 43 54 22 2c 0a 20  ::= DISTINCT",. 
20bd0 2f 2a 20 31 31 32 20 2a 2f 20 22 64 69 73 74 69  /* 112 */ "disti
20bd1 6e 63 74 20 3a 3a 3d 20 41 4c 4c 22 2c 0a 20 2f  nct ::= ALL",. /
20bd2 2a 20 31 31 33 20 2a 2f 20 22 64 69 73 74 69 6e  * 113 */ "distin
20bd3 63 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 31 34  ct ::=",. /* 114
20bd4 20 2a 2f 20 22 73 63 6c 70 20 3a 3a 3d 20 73 65   */ "sclp ::= se
20bd5 6c 63 6f 6c 6c 69 73 74 20 43 4f 4d 4d 41 22 2c  lcollist COMMA",
20bd6 0a 20 2f 2a 20 31 31 35 20 2a 2f 20 22 73 63 6c  . /* 115 */ "scl
20bd7 70 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 31 36 20  p ::=",. /* 116 
20bd8 2a 2f 20 22 73 65 6c 63 6f 6c 6c 69 73 74 20 3a  */ "selcollist :
20bd9 3a 3d 20 73 63 6c 70 20 65 78 70 72 20 61 73 22  := sclp expr as"
20bda 2c 0a 20 2f 2a 20 31 31 37 20 2a 2f 20 22 73 65  ,. /* 117 */ "se
20bdb 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 73 63 6c  lcollist ::= scl
20bdc 70 20 53 54 41 52 22 2c 0a 20 2f 2a 20 31 31 38  p STAR",. /* 118
20bdd 20 2a 2f 20 22 73 65 6c 63 6f 6c 6c 69 73 74 20   */ "selcollist 
20bde 3a 3a 3d 20 73 63 6c 70 20 6e 6d 20 44 4f 54 20  ::= sclp nm DOT 
20bdf 53 54 41 52 22 2c 0a 20 2f 2a 20 31 31 39 20 2a  STAR",. /* 119 *
20be0 2f 20 22 61 73 20 3a 3a 3d 20 41 53 20 6e 6d 22  / "as ::= AS nm"
20be1 2c 0a 20 2f 2a 20 31 32 30 20 2a 2f 20 22 61 73  ,. /* 120 */ "as
20be2 20 3a 3a 3d 20 69 64 73 22 2c 0a 20 2f 2a 20 31   ::= ids",. /* 1
20be3 32 31 20 2a 2f 20 22 61 73 20 3a 3a 3d 22 2c 0a  21 */ "as ::=",.
20be4 20 2f 2a 20 31 32 32 20 2a 2f 20 22 66 72 6f 6d   /* 122 */ "from
20be5 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 32 33 20 2a   ::=",. /* 123 *
20be6 2f 20 22 66 72 6f 6d 20 3a 3a 3d 20 46 52 4f 4d  / "from ::= FROM
20be7 20 73 65 6c 74 61 62 6c 69 73 74 22 2c 0a 20 2f   seltablist",. /
20be8 2a 20 31 32 34 20 2a 2f 20 22 73 74 6c 5f 70 72  * 124 */ "stl_pr
20be9 65 66 69 78 20 3a 3a 3d 20 73 65 6c 74 61 62 6c  efix ::= seltabl
20bea 69 73 74 20 6a 6f 69 6e 6f 70 22 2c 0a 20 2f 2a  ist joinop",. /*
20beb 20 31 32 35 20 2a 2f 20 22 73 74 6c 5f 70 72 65   125 */ "stl_pre
20bec 66 69 78 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 32  fix ::=",. /* 12
20bed 36 20 2a 2f 20 22 73 65 6c 74 61 62 6c 69 73 74  6 */ "seltablist
20bee 20 3a 3a 3d 20 73 74 6c 5f 70 72 65 66 69 78 20   ::= stl_prefix 
20bef 6e 6d 20 64 62 6e 6d 20 61 73 20 6f 6e 5f 6f 70  nm dbnm as on_op
20bf0 74 20 75 73 69 6e 67 5f 6f 70 74 22 2c 0a 20 2f  t using_opt",. /
20bf1 2a 20 31 32 37 20 2a 2f 20 22 73 65 6c 74 61 62  * 127 */ "seltab
20bf2 6c 69 73 74 20 3a 3a 3d 20 73 74 6c 5f 70 72 65  list ::= stl_pre
20bf3 66 69 78 20 4c 50 20 73 65 6c 74 61 62 6c 69 73  fix LP seltablis
20bf4 74 5f 70 61 72 65 6e 20 52 50 20 61 73 20 6f 6e  t_paren RP as on
20bf5 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70 74 22 2c  _opt using_opt",
20bf6 0a 20 2f 2a 20 31 32 38 20 2a 2f 20 22 73 65 6c  . /* 128 */ "sel
20bf7 74 61 62 6c 69 73 74 5f 70 61 72 65 6e 20 3a 3a  tablist_paren ::
20bf8 3d 20 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31  = select",. /* 1
20bf9 32 39 20 2a 2f 20 22 73 65 6c 74 61 62 6c 69 73  29 */ "seltablis
20bfa 74 5f 70 61 72 65 6e 20 3a 3a 3d 20 73 65 6c 74  t_paren ::= selt
20bfb 61 62 6c 69 73 74 22 2c 0a 20 2f 2a 20 31 33 30  ablist",. /* 130
20bfc 20 2a 2f 20 22 64 62 6e 6d 20 3a 3a 3d 22 2c 0a   */ "dbnm ::=",.
20bfd 20 2f 2a 20 31 33 31 20 2a 2f 20 22 64 62 6e 6d   /* 131 */ "dbnm
20bfe 20 3a 3a 3d 20 44 4f 54 20 6e 6d 22 2c 0a 20 2f   ::= DOT nm",. /
20bff 2a 20 31 33 32 20 2a 2f 20 22 66 75 6c 6c 6e 61  * 132 */ "fullna
20c00 6d 65 20 3a 3a 3d 20 6e 6d 20 64 62 6e 6d 22 2c  me ::= nm dbnm",
20c01 0a 20 2f 2a 20 31 33 33 20 2a 2f 20 22 6a 6f 69  . /* 133 */ "joi
20c02 6e 6f 70 20 3a 3a 3d 20 43 4f 4d 4d 41 7c 4a 4f  nop ::= COMMA|JO
20c03 49 4e 22 2c 0a 20 2f 2a 20 31 33 34 20 2a 2f 20  IN",. /* 134 */ 
20c04 22 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e  "joinop ::= JOIN
20c05 5f 4b 57 20 4a 4f 49 4e 22 2c 0a 20 2f 2a 20 31  _KW JOIN",. /* 1
20c06 33 35 20 2a 2f 20 22 6a 6f 69 6e 6f 70 20 3a 3a  35 */ "joinop ::
20c07 3d 20 4a 4f 49 4e 5f 4b 57 20 6e 6d 20 4a 4f 49  = JOIN_KW nm JOI
20c08 4e 22 2c 0a 20 2f 2a 20 31 33 36 20 2a 2f 20 22  N",. /* 136 */ "
20c09 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f  joinop ::= JOIN_
20c0a 4b 57 20 6e 6d 20 6e 6d 20 4a 4f 49 4e 22 2c 0a  KW nm nm JOIN",.
20c0b 20 2f 2a 20 31 33 37 20 2a 2f 20 22 6f 6e 5f 6f   /* 137 */ "on_o
20c0c 70 74 20 3a 3a 3d 20 4f 4e 20 65 78 70 72 22 2c  pt ::= ON expr",
20c0d 0a 20 2f 2a 20 31 33 38 20 2a 2f 20 22 6f 6e 5f  . /* 138 */ "on_
20c0e 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 33  opt ::=",. /* 13
20c0f 39 20 2a 2f 20 22 75 73 69 6e 67 5f 6f 70 74 20  9 */ "using_opt 
20c10 3a 3a 3d 20 55 53 49 4e 47 20 4c 50 20 69 6e 73  ::= USING LP ins
20c11 63 6f 6c 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a  collist RP",. /*
20c12 20 31 34 30 20 2a 2f 20 22 75 73 69 6e 67 5f 6f   140 */ "using_o
20c13 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 34 31  pt ::=",. /* 141
20c14 20 2a 2f 20 22 6f 72 64 65 72 62 79 5f 6f 70 74   */ "orderby_opt
20c15 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 34 32 20 2a   ::=",. /* 142 *
20c16 2f 20 22 6f 72 64 65 72 62 79 5f 6f 70 74 20 3a  / "orderby_opt :
20c17 3a 3d 20 4f 52 44 45 52 20 42 59 20 73 6f 72 74  := ORDER BY sort
20c18 6c 69 73 74 22 2c 0a 20 2f 2a 20 31 34 33 20 2a  list",. /* 143 *
20c19 2f 20 22 73 6f 72 74 6c 69 73 74 20 3a 3a 3d 20  / "sortlist ::= 
20c1a 73 6f 72 74 6c 69 73 74 20 43 4f 4d 4d 41 20 73  sortlist COMMA s
20c1b 6f 72 74 69 74 65 6d 20 73 6f 72 74 6f 72 64 65  ortitem sortorde
20c1c 72 22 2c 0a 20 2f 2a 20 31 34 34 20 2a 2f 20 22  r",. /* 144 */ "
20c1d 73 6f 72 74 6c 69 73 74 20 3a 3a 3d 20 73 6f 72  sortlist ::= sor
20c1e 74 69 74 65 6d 20 73 6f 72 74 6f 72 64 65 72 22  titem sortorder"
20c1f 2c 0a 20 2f 2a 20 31 34 35 20 2a 2f 20 22 73 6f  ,. /* 145 */ "so
20c20 72 74 69 74 65 6d 20 3a 3a 3d 20 65 78 70 72 22  rtitem ::= expr"
20c21 2c 0a 20 2f 2a 20 31 34 36 20 2a 2f 20 22 73 6f  ,. /* 146 */ "so
20c22 72 74 6f 72 64 65 72 20 3a 3a 3d 20 41 53 43 22  rtorder ::= ASC"
20c23 2c 0a 20 2f 2a 20 31 34 37 20 2a 2f 20 22 73 6f  ,. /* 147 */ "so
20c24 72 74 6f 72 64 65 72 20 3a 3a 3d 20 44 45 53 43  rtorder ::= DESC
20c25 22 2c 0a 20 2f 2a 20 31 34 38 20 2a 2f 20 22 73  ",. /* 148 */ "s
20c26 6f 72 74 6f 72 64 65 72 20 3a 3a 3d 22 2c 0a 20  ortorder ::=",. 
20c27 2f 2a 20 31 34 39 20 2a 2f 20 22 67 72 6f 75 70  /* 149 */ "group
20c28 62 79 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a  by_opt ::=",. /*
20c29 20 31 35 30 20 2a 2f 20 22 67 72 6f 75 70 62 79   150 */ "groupby
20c2a 5f 6f 70 74 20 3a 3a 3d 20 47 52 4f 55 50 20 42  _opt ::= GROUP B
20c2b 59 20 6e 65 78 70 72 6c 69 73 74 22 2c 0a 20 2f  Y nexprlist",. /
20c2c 2a 20 31 35 31 20 2a 2f 20 22 68 61 76 69 6e 67  * 151 */ "having
20c2d 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31  _opt ::=",. /* 1
20c2e 35 32 20 2a 2f 20 22 68 61 76 69 6e 67 5f 6f 70  52 */ "having_op
20c2f 74 20 3a 3a 3d 20 48 41 56 49 4e 47 20 65 78 70  t ::= HAVING exp
20c30 72 22 2c 0a 20 2f 2a 20 31 35 33 20 2a 2f 20 22  r",. /* 153 */ "
20c31 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 22 2c 0a  limit_opt ::=",.
20c32 20 2f 2a 20 31 35 34 20 2a 2f 20 22 6c 69 6d 69   /* 154 */ "limi
20c33 74 5f 6f 70 74 20 3a 3a 3d 20 4c 49 4d 49 54 20  t_opt ::= LIMIT 
20c34 65 78 70 72 22 2c 0a 20 2f 2a 20 31 35 35 20 2a  expr",. /* 155 *
20c35 2f 20 22 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d  / "limit_opt ::=
20c36 20 4c 49 4d 49 54 20 65 78 70 72 20 4f 46 46 53   LIMIT expr OFFS
20c37 45 54 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 35  ET expr",. /* 15
20c38 36 20 2a 2f 20 22 6c 69 6d 69 74 5f 6f 70 74 20  6 */ "limit_opt 
20c39 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70 72 20 43  ::= LIMIT expr C
20c3a 4f 4d 4d 41 20 65 78 70 72 22 2c 0a 20 2f 2a 20  OMMA expr",. /* 
20c3b 31 35 37 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  157 */ "cmd ::= 
20c3c 44 45 4c 45 54 45 20 46 52 4f 4d 20 66 75 6c 6c  DELETE FROM full
20c3d 6e 61 6d 65 20 77 68 65 72 65 5f 6f 70 74 22 2c  name where_opt",
20c3e 0a 20 2f 2a 20 31 35 38 20 2a 2f 20 22 77 68 65  . /* 158 */ "whe
20c3f 72 65 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a  re_opt ::=",. /*
20c40 20 31 35 39 20 2a 2f 20 22 77 68 65 72 65 5f 6f   159 */ "where_o
20c41 70 74 20 3a 3a 3d 20 57 48 45 52 45 20 65 78 70  pt ::= WHERE exp
20c42 72 22 2c 0a 20 2f 2a 20 31 36 30 20 2a 2f 20 22  r",. /* 160 */ "
20c43 63 6d 64 20 3a 3a 3d 20 55 50 44 41 54 45 20 6f  cmd ::= UPDATE o
20c44 72 63 6f 6e 66 20 66 75 6c 6c 6e 61 6d 65 20 53  rconf fullname S
20c45 45 54 20 73 65 74 6c 69 73 74 20 77 68 65 72 65  ET setlist where
20c46 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31 36 31 20 2a  _opt",. /* 161 *
20c47 2f 20 22 73 65 74 6c 69 73 74 20 3a 3a 3d 20 73  / "setlist ::= s
20c48 65 74 6c 69 73 74 20 43 4f 4d 4d 41 20 6e 6d 20  etlist COMMA nm 
20c49 45 51 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 36  EQ expr",. /* 16
20c4a 32 20 2a 2f 20 22 73 65 74 6c 69 73 74 20 3a 3a  2 */ "setlist ::
20c4b 3d 20 6e 6d 20 45 51 20 65 78 70 72 22 2c 0a 20  = nm EQ expr",. 
20c4c 2f 2a 20 31 36 33 20 2a 2f 20 22 63 6d 64 20 3a  /* 163 */ "cmd :
20c4d 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e  := insert_cmd IN
20c4e 54 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 73 63  TO fullname insc
20c4f 6f 6c 6c 69 73 74 5f 6f 70 74 20 56 41 4c 55 45  ollist_opt VALUE
20c50 53 20 4c 50 20 69 74 65 6d 6c 69 73 74 20 52 50  S LP itemlist RP
20c51 22 2c 0a 20 2f 2a 20 31 36 34 20 2a 2f 20 22 63  ",. /* 164 */ "c
20c52 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d  md ::= insert_cm
20c53 64 20 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65 20  d INTO fullname 
20c54 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 73  inscollist_opt s
20c55 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31 36 35 20  elect",. /* 165 
20c56 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 69 6e 73 65  */ "cmd ::= inse
20c57 72 74 5f 63 6d 64 20 49 4e 54 4f 20 66 75 6c 6c  rt_cmd INTO full
20c58 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c 69 73 74 5f  name inscollist_
20c59 6f 70 74 20 44 45 46 41 55 4c 54 20 56 41 4c 55  opt DEFAULT VALU
20c5a 45 53 22 2c 0a 20 2f 2a 20 31 36 36 20 2a 2f 20  ES",. /* 166 */ 
20c5b 22 69 6e 73 65 72 74 5f 63 6d 64 20 3a 3a 3d 20  "insert_cmd ::= 
20c5c 49 4e 53 45 52 54 20 6f 72 63 6f 6e 66 22 2c 0a  INSERT orconf",.
20c5d 20 2f 2a 20 31 36 37 20 2a 2f 20 22 69 6e 73 65   /* 167 */ "inse
20c5e 72 74 5f 63 6d 64 20 3a 3a 3d 20 52 45 50 4c 41  rt_cmd ::= REPLA
20c5f 43 45 22 2c 0a 20 2f 2a 20 31 36 38 20 2a 2f 20  CE",. /* 168 */ 
20c60 22 69 74 65 6d 6c 69 73 74 20 3a 3a 3d 20 69 74  "itemlist ::= it
20c61 65 6d 6c 69 73 74 20 43 4f 4d 4d 41 20 65 78 70  emlist COMMA exp
20c62 72 22 2c 0a 20 2f 2a 20 31 36 39 20 2a 2f 20 22  r",. /* 169 */ "
20c63 69 74 65 6d 6c 69 73 74 20 3a 3a 3d 20 65 78 70  itemlist ::= exp
20c64 72 22 2c 0a 20 2f 2a 20 31 37 30 20 2a 2f 20 22  r",. /* 170 */ "
20c65 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 3a  inscollist_opt :
20c66 3a 3d 22 2c 0a 20 2f 2a 20 31 37 31 20 2a 2f 20  :=",. /* 171 */ 
20c67 22 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20  "inscollist_opt 
20c68 3a 3a 3d 20 4c 50 20 69 6e 73 63 6f 6c 6c 69 73  ::= LP inscollis
20c69 74 20 52 50 22 2c 0a 20 2f 2a 20 31 37 32 20 2a  t RP",. /* 172 *
20c6a 2f 20 22 69 6e 73 63 6f 6c 6c 69 73 74 20 3a 3a  / "inscollist ::
20c6b 3d 20 69 6e 73 63 6f 6c 6c 69 73 74 20 43 4f 4d  = inscollist COM
20c6c 4d 41 20 6e 6d 22 2c 0a 20 2f 2a 20 31 37 33 20  MA nm",. /* 173 
20c6d 2a 2f 20 22 69 6e 73 63 6f 6c 6c 69 73 74 20 3a  */ "inscollist :
20c6e 3a 3d 20 6e 6d 22 2c 0a 20 2f 2a 20 31 37 34 20  := nm",. /* 174 
20c6f 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 74 65 72  */ "expr ::= ter
20c70 6d 22 2c 0a 20 2f 2a 20 31 37 35 20 2a 2f 20 22  m",. /* 175 */ "
20c71 65 78 70 72 20 3a 3a 3d 20 4c 50 20 65 78 70 72  expr ::= LP expr
20c72 20 52 50 22 2c 0a 20 2f 2a 20 31 37 36 20 2a 2f   RP",. /* 176 */
20c73 20 22 74 65 72 6d 20 3a 3a 3d 20 4e 55 4c 4c 22   "term ::= NULL"
20c74 2c 0a 20 2f 2a 20 31 37 37 20 2a 2f 20 22 65 78  ,. /* 177 */ "ex
20c75 70 72 20 3a 3a 3d 20 49 44 22 2c 0a 20 2f 2a 20  pr ::= ID",. /* 
20c76 31 37 38 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  178 */ "expr ::=
20c77 20 4a 4f 49 4e 5f 4b 57 22 2c 0a 20 2f 2a 20 31   JOIN_KW",. /* 1
20c78 37 39 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  79 */ "expr ::= 
20c79 6e 6d 20 44 4f 54 20 6e 6d 22 2c 0a 20 2f 2a 20  nm DOT nm",. /* 
20c7a 31 38 30 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  180 */ "expr ::=
20c7b 20 6e 6d 20 44 4f 54 20 6e 6d 20 44 4f 54 20 6e   nm DOT nm DOT n
20c7c 6d 22 2c 0a 20 2f 2a 20 31 38 31 20 2a 2f 20 22  m",. /* 181 */ "
20c7d 74 65 72 6d 20 3a 3a 3d 20 49 4e 54 45 47 45 52  term ::= INTEGER
20c7e 7c 46 4c 4f 41 54 7c 42 4c 4f 42 22 2c 0a 20 2f  |FLOAT|BLOB",. /
20c7f 2a 20 31 38 32 20 2a 2f 20 22 74 65 72 6d 20 3a  * 182 */ "term :
20c80 3a 3d 20 53 54 52 49 4e 47 22 2c 0a 20 2f 2a 20  := STRING",. /* 
20c81 31 38 33 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  183 */ "expr ::=
20c82 20 52 45 47 49 53 54 45 52 22 2c 0a 20 2f 2a 20   REGISTER",. /* 
20c83 31 38 34 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  184 */ "expr ::=
20c84 20 56 41 52 49 41 42 4c 45 22 2c 0a 20 2f 2a 20   VARIABLE",. /* 
20c85 31 38 35 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  185 */ "expr ::=
20c86 20 65 78 70 72 20 43 4f 4c 4c 41 54 45 20 69 64   expr COLLATE id
20c87 22 2c 0a 20 2f 2a 20 31 38 36 20 2a 2f 20 22 65  ",. /* 186 */ "e
20c88 78 70 72 20 3a 3a 3d 20 43 41 53 54 20 4c 50 20  xpr ::= CAST LP 
20c89 65 78 70 72 20 41 53 20 74 79 70 65 74 6f 6b 65  expr AS typetoke
20c8a 6e 20 52 50 22 2c 0a 20 2f 2a 20 31 38 37 20 2a  n RP",. /* 187 *
20c8b 2f 20 22 65 78 70 72 20 3a 3a 3d 20 49 44 20 4c  / "expr ::= ID L
20c8c 50 20 64 69 73 74 69 6e 63 74 20 65 78 70 72 6c  P distinct exprl
20c8d 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 31 38 38  ist RP",. /* 188
20c8e 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 49 44   */ "expr ::= ID
20c8f 20 4c 50 20 53 54 41 52 20 52 50 22 2c 0a 20 2f   LP STAR RP",. /
20c90 2a 20 31 38 39 20 2a 2f 20 22 74 65 72 6d 20 3a  * 189 */ "term :
20c91 3a 3d 20 43 54 49 4d 45 5f 4b 57 22 2c 0a 20 2f  := CTIME_KW",. /
20c92 2a 20 31 39 30 20 2a 2f 20 22 65 78 70 72 20 3a  * 190 */ "expr :
20c93 3a 3d 20 65 78 70 72 20 41 4e 44 20 65 78 70 72  := expr AND expr
20c94 22 2c 0a 20 2f 2a 20 31 39 31 20 2a 2f 20 22 65  ",. /* 191 */ "e
20c95 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 4f 52 20  xpr ::= expr OR 
20c96 65 78 70 72 22 2c 0a 20 2f 2a 20 31 39 32 20 2a  expr",. /* 192 *
20c97 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  / "expr ::= expr
20c98 20 4c 54 7c 47 54 7c 47 45 7c 4c 45 20 65 78 70   LT|GT|GE|LE exp
20c99 72 22 2c 0a 20 2f 2a 20 31 39 33 20 2a 2f 20 22  r",. /* 193 */ "
20c9a 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 45 51  expr ::= expr EQ
20c9b 7c 4e 45 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31  |NE expr",. /* 1
20c9c 39 34 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  94 */ "expr ::= 
20c9d 65 78 70 72 20 42 49 54 41 4e 44 7c 42 49 54 4f  expr BITAND|BITO
20c9e 52 7c 4c 53 48 49 46 54 7c 52 53 48 49 46 54 20  R|LSHIFT|RSHIFT 
20c9f 65 78 70 72 22 2c 0a 20 2f 2a 20 31 39 35 20 2a  expr",. /* 195 *
20ca0 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  / "expr ::= expr
20ca1 20 50 4c 55 53 7c 4d 49 4e 55 53 20 65 78 70 72   PLUS|MINUS expr
20ca2 22 2c 0a 20 2f 2a 20 31 39 36 20 2a 2f 20 22 65  ",. /* 196 */ "e
20ca3 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 53 54 41  xpr ::= expr STA
20ca4 52 7c 53 4c 41 53 48 7c 52 45 4d 20 65 78 70 72  R|SLASH|REM expr
20ca5 22 2c 0a 20 2f 2a 20 31 39 37 20 2a 2f 20 22 65  ",. /* 197 */ "e
20ca6 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 43 4f 4e  xpr ::= expr CON
20ca7 43 41 54 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31  CAT expr",. /* 1
20ca8 39 38 20 2a 2f 20 22 6c 69 6b 65 6f 70 20 3a 3a  98 */ "likeop ::
20ca9 3d 20 4c 49 4b 45 5f 4b 57 22 2c 0a 20 2f 2a 20  = LIKE_KW",. /* 
20caa 31 39 39 20 2a 2f 20 22 6c 69 6b 65 6f 70 20 3a  199 */ "likeop :
20cab 3a 3d 20 4e 4f 54 20 4c 49 4b 45 5f 4b 57 22 2c  := NOT LIKE_KW",
20cac 0a 20 2f 2a 20 32 30 30 20 2a 2f 20 22 6c 69 6b  . /* 200 */ "lik
20cad 65 6f 70 20 3a 3a 3d 20 4d 41 54 43 48 22 2c 0a  eop ::= MATCH",.
20cae 20 2f 2a 20 32 30 31 20 2a 2f 20 22 6c 69 6b 65   /* 201 */ "like
20caf 6f 70 20 3a 3a 3d 20 4e 4f 54 20 4d 41 54 43 48  op ::= NOT MATCH
20cb0 22 2c 0a 20 2f 2a 20 32 30 32 20 2a 2f 20 22 65  ",. /* 202 */ "e
20cb1 73 63 61 70 65 20 3a 3a 3d 20 45 53 43 41 50 45  scape ::= ESCAPE
20cb2 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 30 33 20   expr",. /* 203 
20cb3 2a 2f 20 22 65 73 63 61 70 65 20 3a 3a 3d 22 2c  */ "escape ::=",
20cb4 0a 20 2f 2a 20 32 30 34 20 2a 2f 20 22 65 78 70  . /* 204 */ "exp
20cb5 72 20 3a 3a 3d 20 65 78 70 72 20 6c 69 6b 65 6f  r ::= expr likeo
20cb6 70 20 65 78 70 72 20 65 73 63 61 70 65 22 2c 0a  p expr escape",.
20cb7 20 2f 2a 20 32 30 35 20 2a 2f 20 22 65 78 70 72   /* 205 */ "expr
20cb8 20 3a 3a 3d 20 65 78 70 72 20 49 53 4e 55 4c 4c   ::= expr ISNULL
20cb9 7c 4e 4f 54 4e 55 4c 4c 22 2c 0a 20 2f 2a 20 32  |NOTNULL",. /* 2
20cba 30 36 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  06 */ "expr ::= 
20cbb 65 78 70 72 20 49 53 20 4e 55 4c 4c 22 2c 0a 20  expr IS NULL",. 
20cbc 2f 2a 20 32 30 37 20 2a 2f 20 22 65 78 70 72 20  /* 207 */ "expr 
20cbd 3a 3a 3d 20 65 78 70 72 20 4e 4f 54 20 4e 55 4c  ::= expr NOT NUL
20cbe 4c 22 2c 0a 20 2f 2a 20 32 30 38 20 2a 2f 20 22  L",. /* 208 */ "
20cbf 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 49 53  expr ::= expr IS
20cc0 20 4e 4f 54 20 4e 55 4c 4c 22 2c 0a 20 2f 2a 20   NOT NULL",. /* 
20cc1 32 30 39 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  209 */ "expr ::=
20cc2 20 4e 4f 54 7c 42 49 54 4e 4f 54 20 65 78 70 72   NOT|BITNOT expr
20cc3 22 2c 0a 20 2f 2a 20 32 31 30 20 2a 2f 20 22 65  ",. /* 210 */ "e
20cc4 78 70 72 20 3a 3a 3d 20 4d 49 4e 55 53 20 65 78  xpr ::= MINUS ex
20cc5 70 72 22 2c 0a 20 2f 2a 20 32 31 31 20 2a 2f 20  pr",. /* 211 */ 
20cc6 22 65 78 70 72 20 3a 3a 3d 20 50 4c 55 53 20 65  "expr ::= PLUS e
20cc7 78 70 72 22 2c 0a 20 2f 2a 20 32 31 32 20 2a 2f  xpr",. /* 212 */
20cc8 20 22 62 65 74 77 65 65 6e 5f 6f 70 20 3a 3a 3d   "between_op ::=
20cc9 20 42 45 54 57 45 45 4e 22 2c 0a 20 2f 2a 20 32   BETWEEN",. /* 2
20cca 31 33 20 2a 2f 20 22 62 65 74 77 65 65 6e 5f 6f  13 */ "between_o
20ccb 70 20 3a 3a 3d 20 4e 4f 54 20 42 45 54 57 45 45  p ::= NOT BETWEE
20ccc 4e 22 2c 0a 20 2f 2a 20 32 31 34 20 2a 2f 20 22  N",. /* 214 */ "
20ccd 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 62 65  expr ::= expr be
20cce 74 77 65 65 6e 5f 6f 70 20 65 78 70 72 20 41 4e  tween_op expr AN
20ccf 44 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 31 35  D expr",. /* 215
20cd0 20 2a 2f 20 22 69 6e 5f 6f 70 20 3a 3a 3d 20 49   */ "in_op ::= I
20cd1 4e 22 2c 0a 20 2f 2a 20 32 31 36 20 2a 2f 20 22  N",. /* 216 */ "
20cd2 69 6e 5f 6f 70 20 3a 3a 3d 20 4e 4f 54 20 49 4e  in_op ::= NOT IN
20cd3 22 2c 0a 20 2f 2a 20 32 31 37 20 2a 2f 20 22 65  ",. /* 217 */ "e
20cd4 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 69 6e 5f  xpr ::= expr in_
20cd5 6f 70 20 4c 50 20 65 78 70 72 6c 69 73 74 20 52  op LP exprlist R
20cd6 50 22 2c 0a 20 2f 2a 20 32 31 38 20 2a 2f 20 22  P",. /* 218 */ "
20cd7 65 78 70 72 20 3a 3a 3d 20 4c 50 20 73 65 6c 65  expr ::= LP sele
20cd8 63 74 20 52 50 22 2c 0a 20 2f 2a 20 32 31 39 20  ct RP",. /* 219 
20cd9 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70  */ "expr ::= exp
20cda 72 20 69 6e 5f 6f 70 20 4c 50 20 73 65 6c 65 63  r in_op LP selec
20cdb 74 20 52 50 22 2c 0a 20 2f 2a 20 32 32 30 20 2a  t RP",. /* 220 *
20cdc 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  / "expr ::= expr
20cdd 20 69 6e 5f 6f 70 20 6e 6d 20 64 62 6e 6d 22 2c   in_op nm dbnm",
20cde 0a 20 2f 2a 20 32 32 31 20 2a 2f 20 22 65 78 70  . /* 221 */ "exp
20cdf 72 20 3a 3a 3d 20 45 58 49 53 54 53 20 4c 50 20  r ::= EXISTS LP 
20ce0 73 65 6c 65 63 74 20 52 50 22 2c 0a 20 2f 2a 20  select RP",. /* 
20ce1 32 32 32 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  222 */ "expr ::=
20ce2 20 43 41 53 45 20 63 61 73 65 5f 6f 70 65 72 61   CASE case_opera
20ce3 6e 64 20 63 61 73 65 5f 65 78 70 72 6c 69 73 74  nd case_exprlist
20ce4 20 63 61 73 65 5f 65 6c 73 65 20 45 4e 44 22 2c   case_else END",
20ce5 0a 20 2f 2a 20 32 32 33 20 2a 2f 20 22 63 61 73  . /* 223 */ "cas
20ce6 65 5f 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 63  e_exprlist ::= c
20ce7 61 73 65 5f 65 78 70 72 6c 69 73 74 20 57 48 45  ase_exprlist WHE
20ce8 4e 20 65 78 70 72 20 54 48 45 4e 20 65 78 70 72  N expr THEN expr
20ce9 22 2c 0a 20 2f 2a 20 32 32 34 20 2a 2f 20 22 63  ",. /* 224 */ "c
20cea 61 73 65 5f 65 78 70 72 6c 69 73 74 20 3a 3a 3d  ase_exprlist ::=
20ceb 20 57 48 45 4e 20 65 78 70 72 20 54 48 45 4e 20   WHEN expr THEN 
20cec 65 78 70 72 22 2c 0a 20 2f 2a 20 32 32 35 20 2a  expr",. /* 225 *
20ced 2f 20 22 63 61 73 65 5f 65 6c 73 65 20 3a 3a 3d  / "case_else ::=
20cee 20 45 4c 53 45 20 65 78 70 72 22 2c 0a 20 2f 2a   ELSE expr",. /*
20cef 20 32 32 36 20 2a 2f 20 22 63 61 73 65 5f 65 6c   226 */ "case_el
20cf0 73 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 32 37  se ::=",. /* 227
20cf1 20 2a 2f 20 22 63 61 73 65 5f 6f 70 65 72 61 6e   */ "case_operan
20cf2 64 20 3a 3a 3d 20 65 78 70 72 22 2c 0a 20 2f 2a  d ::= expr",. /*
20cf3 20 32 32 38 20 2a 2f 20 22 63 61 73 65 5f 6f 70   228 */ "case_op
20cf4 65 72 61 6e 64 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  erand ::=",. /* 
20cf5 32 32 39 20 2a 2f 20 22 65 78 70 72 6c 69 73 74  229 */ "exprlist
20cf6 20 3a 3a 3d 20 6e 65 78 70 72 6c 69 73 74 22 2c   ::= nexprlist",
20cf7 0a 20 2f 2a 20 32 33 30 20 2a 2f 20 22 65 78 70  . /* 230 */ "exp
20cf8 72 6c 69 73 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  rlist ::=",. /* 
20cf9 32 33 31 20 2a 2f 20 22 6e 65 78 70 72 6c 69 73  231 */ "nexprlis
20cfa 74 20 3a 3a 3d 20 6e 65 78 70 72 6c 69 73 74 20  t ::= nexprlist 
20cfb 43 4f 4d 4d 41 20 65 78 70 72 22 2c 0a 20 2f 2a  COMMA expr",. /*
20cfc 20 32 33 32 20 2a 2f 20 22 6e 65 78 70 72 6c 69   232 */ "nexprli
20cfd 73 74 20 3a 3a 3d 20 65 78 70 72 22 2c 0a 20 2f  st ::= expr",. /
20cfe 2a 20 32 33 33 20 2a 2f 20 22 63 6d 64 20 3a 3a  * 233 */ "cmd ::
20cff 3d 20 43 52 45 41 54 45 20 75 6e 69 71 75 65 66  = CREATE uniquef
20d00 6c 61 67 20 49 4e 44 45 58 20 69 66 6e 6f 74 65  lag INDEX ifnote
20d01 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20 4f 4e  xists nm dbnm ON
20d02 20 6e 6d 20 4c 50 20 69 64 78 6c 69 73 74 20 52   nm LP idxlist R
20d03 50 22 2c 0a 20 2f 2a 20 32 33 34 20 2a 2f 20 22  P",. /* 234 */ "
20d04 75 6e 69 71 75 65 66 6c 61 67 20 3a 3a 3d 20 55  uniqueflag ::= U
20d05 4e 49 51 55 45 22 2c 0a 20 2f 2a 20 32 33 35 20  NIQUE",. /* 235 
20d06 2a 2f 20 22 75 6e 69 71 75 65 66 6c 61 67 20 3a  */ "uniqueflag :
20d07 3a 3d 22 2c 0a 20 2f 2a 20 32 33 36 20 2a 2f 20  :=",. /* 236 */ 
20d08 22 69 64 78 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d  "idxlist_opt ::=
20d09 22 2c 0a 20 2f 2a 20 32 33 37 20 2a 2f 20 22 69  ",. /* 237 */ "i
20d0a 64 78 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 4c  dxlist_opt ::= L
20d0b 50 20 69 64 78 6c 69 73 74 20 52 50 22 2c 0a 20  P idxlist RP",. 
20d0c 2f 2a 20 32 33 38 20 2a 2f 20 22 69 64 78 6c 69  /* 238 */ "idxli
20d0d 73 74 20 3a 3a 3d 20 69 64 78 6c 69 73 74 20 43  st ::= idxlist C
20d0e 4f 4d 4d 41 20 69 64 78 69 74 65 6d 20 63 6f 6c  OMMA idxitem col
20d0f 6c 61 74 65 20 73 6f 72 74 6f 72 64 65 72 22 2c  late sortorder",
20d10 0a 20 2f 2a 20 32 33 39 20 2a 2f 20 22 69 64 78  . /* 239 */ "idx
20d11 6c 69 73 74 20 3a 3a 3d 20 69 64 78 69 74 65 6d  list ::= idxitem
20d12 20 63 6f 6c 6c 61 74 65 20 73 6f 72 74 6f 72 64   collate sortord
20d13 65 72 22 2c 0a 20 2f 2a 20 32 34 30 20 2a 2f 20  er",. /* 240 */ 
20d14 22 69 64 78 69 74 65 6d 20 3a 3a 3d 20 6e 6d 22  "idxitem ::= nm"
20d15 2c 0a 20 2f 2a 20 32 34 31 20 2a 2f 20 22 63 6f  ,. /* 241 */ "co
20d16 6c 6c 61 74 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  llate ::=",. /* 
20d17 32 34 32 20 2a 2f 20 22 63 6f 6c 6c 61 74 65 20  242 */ "collate 
20d18 3a 3a 3d 20 43 4f 4c 4c 41 54 45 20 69 64 22 2c  ::= COLLATE id",
20d19 0a 20 2f 2a 20 32 34 33 20 2a 2f 20 22 63 6d 64  . /* 243 */ "cmd
20d1a 20 3a 3a 3d 20 44 52 4f 50 20 49 4e 44 45 58 20   ::= DROP INDEX 
20d1b 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d  ifexists fullnam
20d1c 65 22 2c 0a 20 2f 2a 20 32 34 34 20 2a 2f 20 22  e",. /* 244 */ "
20d1d 63 6d 64 20 3a 3a 3d 20 56 41 43 55 55 4d 22 2c  cmd ::= VACUUM",
20d1e 0a 20 2f 2a 20 32 34 35 20 2a 2f 20 22 63 6d 64  . /* 245 */ "cmd
20d1f 20 3a 3a 3d 20 56 41 43 55 55 4d 20 6e 6d 22 2c   ::= VACUUM nm",
20d20 0a 20 2f 2a 20 32 34 36 20 2a 2f 20 22 63 6d 64  . /* 246 */ "cmd
20d21 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64   ::= PRAGMA nm d
20d22 62 6e 6d 20 45 51 20 6e 6d 6e 75 6d 22 2c 0a 20  bnm EQ nmnum",. 
20d23 2f 2a 20 32 34 37 20 2a 2f 20 22 63 6d 64 20 3a  /* 247 */ "cmd :
20d24 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e  := PRAGMA nm dbn
20d25 6d 20 45 51 20 4f 4e 22 2c 0a 20 2f 2a 20 32 34  m EQ ON",. /* 24
20d26 38 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 50 52  8 */ "cmd ::= PR
20d27 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 45 51 20  AGMA nm dbnm EQ 
20d28 6d 69 6e 75 73 5f 6e 75 6d 22 2c 0a 20 2f 2a 20  minus_num",. /* 
20d29 32 34 39 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  249 */ "cmd ::= 
20d2a 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 4c  PRAGMA nm dbnm L
20d2b 50 20 6e 6d 6e 75 6d 20 52 50 22 2c 0a 20 2f 2a  P nmnum RP",. /*
20d2c 20 32 35 30 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   250 */ "cmd ::=
20d2d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 22   PRAGMA nm dbnm"
20d2e 2c 0a 20 2f 2a 20 32 35 31 20 2a 2f 20 22 6e 6d  ,. /* 251 */ "nm
20d2f 6e 75 6d 20 3a 3a 3d 20 70 6c 75 73 5f 6e 75 6d  num ::= plus_num
20d30 22 2c 0a 20 2f 2a 20 32 35 32 20 2a 2f 20 22 6e  ",. /* 252 */ "n
20d31 6d 6e 75 6d 20 3a 3a 3d 20 6e 6d 22 2c 0a 20 2f  mnum ::= nm",. /
20d32 2a 20 32 35 33 20 2a 2f 20 22 70 6c 75 73 5f 6e  * 253 */ "plus_n
20d33 75 6d 20 3a 3a 3d 20 70 6c 75 73 5f 6f 70 74 20  um ::= plus_opt 
20d34 6e 75 6d 62 65 72 22 2c 0a 20 2f 2a 20 32 35 34  number",. /* 254
20d35 20 2a 2f 20 22 6d 69 6e 75 73 5f 6e 75 6d 20 3a   */ "minus_num :
20d36 3a 3d 20 4d 49 4e 55 53 20 6e 75 6d 62 65 72 22  := MINUS number"
20d37 2c 0a 20 2f 2a 20 32 35 35 20 2a 2f 20 22 6e 75  ,. /* 255 */ "nu
20d38 6d 62 65 72 20 3a 3a 3d 20 49 4e 54 45 47 45 52  mber ::= INTEGER
20d39 7c 46 4c 4f 41 54 22 2c 0a 20 2f 2a 20 32 35 36  |FLOAT",. /* 256
20d3a 20 2a 2f 20 22 70 6c 75 73 5f 6f 70 74 20 3a 3a   */ "plus_opt ::
20d3b 3d 20 50 4c 55 53 22 2c 0a 20 2f 2a 20 32 35 37  = PLUS",. /* 257
20d3c 20 2a 2f 20 22 70 6c 75 73 5f 6f 70 74 20 3a 3a   */ "plus_opt ::
20d3d 3d 22 2c 0a 20 2f 2a 20 32 35 38 20 2a 2f 20 22  =",. /* 258 */ "
20d3e 63 6d 64 20 3a 3a 3d 20 43 52 45 41 54 45 20 74  cmd ::= CREATE t
20d3f 72 69 67 67 65 72 5f 64 65 63 6c 20 42 45 47 49  rigger_decl BEGI
20d40 4e 20 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69  N trigger_cmd_li
20d41 73 74 20 45 4e 44 22 2c 0a 20 2f 2a 20 32 35 39  st END",. /* 259
20d42 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 64 65 63   */ "trigger_dec
20d43 6c 20 3a 3a 3d 20 74 65 6d 70 20 54 52 49 47 47  l ::= temp TRIGG
20d44 45 52 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e  ER ifnotexists n
20d45 6d 20 64 62 6e 6d 20 74 72 69 67 67 65 72 5f 74  m dbnm trigger_t
20d46 69 6d 65 20 74 72 69 67 67 65 72 5f 65 76 65 6e  ime trigger_even
20d47 74 20 4f 4e 20 66 75 6c 6c 6e 61 6d 65 20 66 6f  t ON fullname fo
20d48 72 65 61 63 68 5f 63 6c 61 75 73 65 20 77 68 65  reach_clause whe
20d49 6e 5f 63 6c 61 75 73 65 22 2c 0a 20 2f 2a 20 32  n_clause",. /* 2
20d4a 36 30 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 74  60 */ "trigger_t
20d4b 69 6d 65 20 3a 3a 3d 20 42 45 46 4f 52 45 22 2c  ime ::= BEFORE",
20d4c 0a 20 2f 2a 20 32 36 31 20 2a 2f 20 22 74 72 69  . /* 261 */ "tri
20d4d 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20 41 46  gger_time ::= AF
20d4e 54 45 52 22 2c 0a 20 2f 2a 20 32 36 32 20 2a 2f  TER",. /* 262 */
20d4f 20 22 74 72 69 67 67 65 72 5f 74 69 6d 65 20 3a   "trigger_time :
20d50 3a 3d 20 49 4e 53 54 45 41 44 20 4f 46 22 2c 0a  := INSTEAD OF",.
20d51 20 2f 2a 20 32 36 33 20 2a 2f 20 22 74 72 69 67   /* 263 */ "trig
20d52 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 22 2c 0a 20  ger_time ::=",. 
20d53 2f 2a 20 32 36 34 20 2a 2f 20 22 74 72 69 67 67  /* 264 */ "trigg
20d54 65 72 5f 65 76 65 6e 74 20 3a 3a 3d 20 44 45 4c  er_event ::= DEL
20d55 45 54 45 7c 49 4e 53 45 52 54 22 2c 0a 20 2f 2a  ETE|INSERT",. /*
20d56 20 32 36 35 20 2a 2f 20 22 74 72 69 67 67 65 72   265 */ "trigger
20d57 5f 65 76 65 6e 74 20 3a 3a 3d 20 55 50 44 41 54  _event ::= UPDAT
20d58 45 22 2c 0a 20 2f 2a 20 32 36 36 20 2a 2f 20 22  E",. /* 266 */ "
20d59 74 72 69 67 67 65 72 5f 65 76 65 6e 74 20 3a 3a  trigger_event ::
20d5a 3d 20 55 50 44 41 54 45 20 4f 46 20 69 6e 73 63  = UPDATE OF insc
20d5b 6f 6c 6c 69 73 74 22 2c 0a 20 2f 2a 20 32 36 37  ollist",. /* 267
20d5c 20 2a 2f 20 22 66 6f 72 65 61 63 68 5f 63 6c 61   */ "foreach_cla
20d5d 75 73 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 36  use ::=",. /* 26
20d5e 38 20 2a 2f 20 22 66 6f 72 65 61 63 68 5f 63 6c  8 */ "foreach_cl
20d5f 61 75 73 65 20 3a 3a 3d 20 46 4f 52 20 45 41 43  ause ::= FOR EAC
20d60 48 20 52 4f 57 22 2c 0a 20 2f 2a 20 32 36 39 20  H ROW",. /* 269 
20d61 2a 2f 20 22 77 68 65 6e 5f 63 6c 61 75 73 65 20  */ "when_clause 
20d62 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 37 30 20 2a 2f  ::=",. /* 270 */
20d63 20 22 77 68 65 6e 5f 63 6c 61 75 73 65 20 3a 3a   "when_clause ::
20d64 3d 20 57 48 45 4e 20 65 78 70 72 22 2c 0a 20 2f  = WHEN expr",. /
20d65 2a 20 32 37 31 20 2a 2f 20 22 74 72 69 67 67 65  * 271 */ "trigge
20d66 72 5f 63 6d 64 5f 6c 69 73 74 20 3a 3a 3d 20 74  r_cmd_list ::= t
20d67 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20  rigger_cmd_list 
20d68 74 72 69 67 67 65 72 5f 63 6d 64 20 53 45 4d 49  trigger_cmd SEMI
20d69 22 2c 0a 20 2f 2a 20 32 37 32 20 2a 2f 20 22 74  ",. /* 272 */ "t
20d6a 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20  rigger_cmd_list 
20d6b 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 37 33 20 2a 2f  ::=",. /* 273 */
20d6c 20 22 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a   "trigger_cmd ::
20d6d 3d 20 55 50 44 41 54 45 20 6f 72 63 6f 6e 66 20  = UPDATE orconf 
20d6e 6e 6d 20 53 45 54 20 73 65 74 6c 69 73 74 20 77  nm SET setlist w
20d6f 68 65 72 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20 32  here_opt",. /* 2
20d70 37 34 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 63  74 */ "trigger_c
20d71 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d  md ::= insert_cm
20d72 64 20 49 4e 54 4f 20 6e 6d 20 69 6e 73 63 6f 6c  d INTO nm inscol
20d73 6c 69 73 74 5f 6f 70 74 20 56 41 4c 55 45 53 20  list_opt VALUES 
20d74 4c 50 20 69 74 65 6d 6c 69 73 74 20 52 50 22 2c  LP itemlist RP",
20d75 0a 20 2f 2a 20 32 37 35 20 2a 2f 20 22 74 72 69  . /* 275 */ "tri
20d76 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 69 6e 73  gger_cmd ::= ins
20d77 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20 6e 6d 20  ert_cmd INTO nm 
20d78 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 73  inscollist_opt s
20d79 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 32 37 36 20  elect",. /* 276 
20d7a 2a 2f 20 22 74 72 69 67 67 65 72 5f 63 6d 64 20  */ "trigger_cmd 
20d7b 3a 3a 3d 20 44 45 4c 45 54 45 20 46 52 4f 4d 20  ::= DELETE FROM 
20d7c 6e 6d 20 77 68 65 72 65 5f 6f 70 74 22 2c 0a 20  nm where_opt",. 
20d7d 2f 2a 20 32 37 37 20 2a 2f 20 22 74 72 69 67 67  /* 277 */ "trigg
20d7e 65 72 5f 63 6d 64 20 3a 3a 3d 20 73 65 6c 65 63  er_cmd ::= selec
20d7f 74 22 2c 0a 20 2f 2a 20 32 37 38 20 2a 2f 20 22  t",. /* 278 */ "
20d80 65 78 70 72 20 3a 3a 3d 20 52 41 49 53 45 20 4c  expr ::= RAISE L
20d81 50 20 49 47 4e 4f 52 45 20 52 50 22 2c 0a 20 2f  P IGNORE RP",. /
20d82 2a 20 32 37 39 20 2a 2f 20 22 65 78 70 72 20 3a  * 279 */ "expr :
20d83 3a 3d 20 52 41 49 53 45 20 4c 50 20 72 61 69 73  := RAISE LP rais
20d84 65 74 79 70 65 20 43 4f 4d 4d 41 20 6e 6d 20 52  etype COMMA nm R
20d85 50 22 2c 0a 20 2f 2a 20 32 38 30 20 2a 2f 20 22  P",. /* 280 */ "
20d86 72 61 69 73 65 74 79 70 65 20 3a 3a 3d 20 52 4f  raisetype ::= RO
20d87 4c 4c 42 41 43 4b 22 2c 0a 20 2f 2a 20 32 38 31  LLBACK",. /* 281
20d88 20 2a 2f 20 22 72 61 69 73 65 74 79 70 65 20 3a   */ "raisetype :
20d89 3a 3d 20 41 42 4f 52 54 22 2c 0a 20 2f 2a 20 32  := ABORT",. /* 2
20d8a 38 32 20 2a 2f 20 22 72 61 69 73 65 74 79 70 65  82 */ "raisetype
20d8b 20 3a 3a 3d 20 46 41 49 4c 22 2c 0a 20 2f 2a 20   ::= FAIL",. /* 
20d8c 32 38 33 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  283 */ "cmd ::= 
20d8d 44 52 4f 50 20 54 52 49 47 47 45 52 20 69 66 65  DROP TRIGGER ife
20d8e 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65 22 2c  xists fullname",
20d8f 0a 20 2f 2a 20 32 38 34 20 2a 2f 20 22 63 6d 64  . /* 284 */ "cmd
20d90 20 3a 3a 3d 20 41 54 54 41 43 48 20 64 61 74 61   ::= ATTACH data
20d91 62 61 73 65 5f 6b 77 5f 6f 70 74 20 65 78 70 72  base_kw_opt expr
20d92 20 41 53 20 65 78 70 72 20 6b 65 79 5f 6f 70 74   AS expr key_opt
20d93 22 2c 0a 20 2f 2a 20 32 38 35 20 2a 2f 20 22 63  ",. /* 285 */ "c
20d94 6d 64 20 3a 3a 3d 20 44 45 54 41 43 48 20 64 61  md ::= DETACH da
20d95 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20 65 78  tabase_kw_opt ex
20d96 70 72 22 2c 0a 20 2f 2a 20 32 38 36 20 2a 2f 20  pr",. /* 286 */ 
20d97 22 6b 65 79 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20  "key_opt ::=",. 
20d98 2f 2a 20 32 38 37 20 2a 2f 20 22 6b 65 79 5f 6f  /* 287 */ "key_o
20d99 70 74 20 3a 3a 3d 20 4b 45 59 20 65 78 70 72 22  pt ::= KEY expr"
20d9a 2c 0a 20 2f 2a 20 32 38 38 20 2a 2f 20 22 64 61  ,. /* 288 */ "da
20d9b 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20 3a 3a  tabase_kw_opt ::
20d9c 3d 20 44 41 54 41 42 41 53 45 22 2c 0a 20 2f 2a  = DATABASE",. /*
20d9d 20 32 38 39 20 2a 2f 20 22 64 61 74 61 62 61 73   289 */ "databas
20d9e 65 5f 6b 77 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20  e_kw_opt ::=",. 
20d9f 2f 2a 20 32 39 30 20 2a 2f 20 22 63 6d 64 20 3a  /* 290 */ "cmd :
20da0 3a 3d 20 52 45 49 4e 44 45 58 22 2c 0a 20 2f 2a  := REINDEX",. /*
20da1 20 32 39 31 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   291 */ "cmd ::=
20da2 20 52 45 49 4e 44 45 58 20 6e 6d 20 64 62 6e 6d   REINDEX nm dbnm
20da3 22 2c 0a 20 2f 2a 20 32 39 32 20 2a 2f 20 22 63  ",. /* 292 */ "c
20da4 6d 64 20 3a 3a 3d 20 41 4e 41 4c 59 5a 45 22 2c  md ::= ANALYZE",
20da5 0a 20 2f 2a 20 32 39 33 20 2a 2f 20 22 63 6d 64  . /* 293 */ "cmd
20da6 20 3a 3a 3d 20 41 4e 41 4c 59 5a 45 20 6e 6d 20   ::= ANALYZE nm 
20da7 64 62 6e 6d 22 2c 0a 20 2f 2a 20 32 39 34 20 2a  dbnm",. /* 294 *
20da8 2f 20 22 63 6d 64 20 3a 3a 3d 20 41 4c 54 45 52  / "cmd ::= ALTER
20da9 20 54 41 42 4c 45 20 66 75 6c 6c 6e 61 6d 65 20   TABLE fullname 
20daa 52 45 4e 41 4d 45 20 54 4f 20 6e 6d 22 2c 0a 20  RENAME TO nm",. 
20dab 2f 2a 20 32 39 35 20 2a 2f 20 22 63 6d 64 20 3a  /* 295 */ "cmd :
20dac 3a 3d 20 41 4c 54 45 52 20 54 41 42 4c 45 20 61  := ALTER TABLE a
20dad 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61  dd_column_fullna
20dae 6d 65 20 41 44 44 20 6b 77 63 6f 6c 75 6d 6e 5f  me ADD kwcolumn_
20daf 6f 70 74 20 63 6f 6c 75 6d 6e 22 2c 0a 20 2f 2a  opt column",. /*
20db0 20 32 39 36 20 2a 2f 20 22 61 64 64 5f 63 6f 6c   296 */ "add_col
20db1 75 6d 6e 5f 66 75 6c 6c 6e 61 6d 65 20 3a 3a 3d  umn_fullname ::=
20db2 20 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20 2f 2a 20   fullname",. /* 
20db3 32 39 37 20 2a 2f 20 22 6b 77 63 6f 6c 75 6d 6e  297 */ "kwcolumn
20db4 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32  _opt ::=",. /* 2
20db5 39 38 20 2a 2f 20 22 6b 77 63 6f 6c 75 6d 6e 5f  98 */ "kwcolumn_
20db6 6f 70 74 20 3a 3a 3d 20 43 4f 4c 55 4d 4e 4b 57  opt ::= COLUMNKW
20db7 22 2c 0a 20 2f 2a 20 32 39 39 20 2a 2f 20 22 63  ",. /* 299 */ "c
20db8 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 5f 76 74  md ::= create_vt
20db9 61 62 22 2c 0a 20 2f 2a 20 33 30 30 20 2a 2f 20  ab",. /* 300 */ 
20dba 22 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 5f  "cmd ::= create_
20dbb 76 74 61 62 20 4c 50 20 76 74 61 62 61 72 67 6c  vtab LP vtabargl
20dbc 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 33 30 31  ist RP",. /* 301
20dbd 20 2a 2f 20 22 63 72 65 61 74 65 5f 76 74 61 62   */ "create_vtab
20dbe 20 3a 3a 3d 20 43 52 45 41 54 45 20 56 49 52 54   ::= CREATE VIRT
20dbf 55 41 4c 20 54 41 42 4c 45 20 6e 6d 20 64 62 6e  UAL TABLE nm dbn
20dc0 6d 20 55 53 49 4e 47 20 6e 6d 22 2c 0a 20 2f 2a  m USING nm",. /*
20dc1 20 33 30 32 20 2a 2f 20 22 76 74 61 62 61 72 67   302 */ "vtabarg
20dc2 6c 69 73 74 20 3a 3a 3d 20 76 74 61 62 61 72 67  list ::= vtabarg
20dc3 22 2c 0a 20 2f 2a 20 33 30 33 20 2a 2f 20 22 76  ",. /* 303 */ "v
20dc4 74 61 62 61 72 67 6c 69 73 74 20 3a 3a 3d 20 76  tabarglist ::= v
20dc5 74 61 62 61 72 67 6c 69 73 74 20 43 4f 4d 4d 41  tabarglist COMMA
20dc6 20 76 74 61 62 61 72 67 22 2c 0a 20 2f 2a 20 33   vtabarg",. /* 3
20dc7 30 34 20 2a 2f 20 22 76 74 61 62 61 72 67 20 3a  04 */ "vtabarg :
20dc8 3a 3d 22 2c 0a 20 2f 2a 20 33 30 35 20 2a 2f 20  :=",. /* 305 */ 
20dc9 22 76 74 61 62 61 72 67 20 3a 3a 3d 20 76 74 61  "vtabarg ::= vta
20dca 62 61 72 67 20 76 74 61 62 61 72 67 74 6f 6b 65  barg vtabargtoke
20dcb 6e 22 2c 0a 20 2f 2a 20 33 30 36 20 2a 2f 20 22  n",. /* 306 */ "
20dcc 76 74 61 62 61 72 67 74 6f 6b 65 6e 20 3a 3a 3d  vtabargtoken ::=
20dcd 20 41 4e 59 22 2c 0a 20 2f 2a 20 33 30 37 20 2a   ANY",. /* 307 *
20dce 2f 20 22 76 74 61 62 61 72 67 74 6f 6b 65 6e 20  / "vtabargtoken 
20dcf 3a 3a 3d 20 6c 70 20 61 6e 79 6c 69 73 74 20 52  ::= lp anylist R
20dd0 50 22 2c 0a 20 2f 2a 20 33 30 38 20 2a 2f 20 22  P",. /* 308 */ "
20dd1 6c 70 20 3a 3a 3d 20 4c 50 22 2c 0a 20 2f 2a 20  lp ::= LP",. /* 
20dd2 33 30 39 20 2a 2f 20 22 61 6e 79 6c 69 73 74 20  309 */ "anylist 
20dd3 3a 3a 3d 22 2c 0a 20 2f 2a 20 33 31 30 20 2a 2f  ::=",. /* 310 */
20dd4 20 22 61 6e 79 6c 69 73 74 20 3a 3a 3d 20 61 6e   "anylist ::= an
20dd5 79 6c 69 73 74 20 41 4e 59 22 2c 0a 7d 3b 0a 23  ylist ANY",.};.#
20dd6 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20  endif /* NDEBUG 
20dd7 2a 2f 0a 0a 0a 23 69 66 20 59 59 53 54 41 43 4b  */...#if YYSTACK
20dd8 44 45 50 54 48 3c 3d 30 0a 2f 2a 0a 2a 2a 20 54  DEPTH<=0./*.** T
20dd9 72 79 20 74 6f 20 69 6e 63 72 65 61 73 65 20 74  ry to increase t
20dda 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 70  he size of the p
20ddb 61 72 73 65 72 20 73 74 61 63 6b 2e 0a 2a 2f 0a  arser stack..*/.
20ddc 73 74 61 74 69 63 20 76 6f 69 64 20 79 79 47 72  static void yyGr
20ddd 6f 77 53 74 61 63 6b 28 79 79 50 61 72 73 65 72  owStack(yyParser
20dde 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 65 77 53   *p){.  int newS
20ddf 69 7a 65 3b 0a 20 20 79 79 53 74 61 63 6b 45 6e  ize;.  yyStackEn
20de0 74 72 79 20 2a 70 4e 65 77 3b 0a 0a 20 20 6e 65  try *pNew;..  ne
20de1 77 53 69 7a 65 20 3d 20 70 2d 3e 79 79 73 74 6b  wSize = p->yystk
20de2 73 7a 2a 32 20 2b 20 31 30 30 3b 0a 20 20 70 4e  sz*2 + 100;.  pN
20de3 65 77 20 3d 20 72 65 61 6c 6c 6f 63 28 70 2d 3e  ew = realloc(p->
20de4 79 79 73 74 61 63 6b 2c 20 6e 65 77 53 69 7a 65  yystack, newSize
20de5 2a 73 69 7a 65 6f 66 28 70 4e 65 77 5b 30 5d 29  *sizeof(pNew[0])
20de6 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 20 29 7b  );.  if( pNew ){
20de7 0a 20 20 20 20 70 2d 3e 79 79 73 74 61 63 6b 20  .    p->yystack 
20de8 3d 20 70 4e 65 77 3b 0a 20 20 20 20 70 2d 3e 79  = pNew;.    p->y
20de9 79 73 74 6b 73 7a 20 3d 20 6e 65 77 53 69 7a 65  ystksz = newSize
20dea 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ;.#ifndef NDEBUG
20deb 0a 20 20 20 20 69 66 28 20 79 79 54 72 61 63 65  .    if( yyTrace
20dec 46 49 4c 45 20 29 7b 0a 20 20 20 20 20 20 66 70  FILE ){.      fp
20ded 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c  rintf(yyTraceFIL
20dee 45 2c 22 25 73 53 74 61 63 6b 20 67 72 6f 77 73  E,"%sStack grows
20def 20 74 6f 20 25 64 20 65 6e 74 72 69 65 73 21 5c   to %d entries!\
20df0 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
20df1 20 20 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c    yyTracePrompt,
20df2 20 70 2d 3e 79 79 73 74 6b 73 7a 29 3b 0a 20 20   p->yystksz);.  
20df3 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d    }.#endif.  }.}
20df4 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 0a 2a 2a 20  .#endif../* .** 
20df5 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c  This function al
20df6 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 20 70 61  locates a new pa
20df7 72 73 65 72 2e 0a 2a 2a 20 54 68 65 20 6f 6e 6c  rser..** The onl
20df8 79 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20  y argument is a 
20df9 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e  pointer to a fun
20dfa 63 74 69 6f 6e 20 77 68 69 63 68 20 77 6f 72 6b  ction which work
20dfb 73 20 6c 69 6b 65 0a 2a 2a 20 6d 61 6c 6c 6f 63  s like.** malloc
20dfc 2e 0a 2a 2a 0a 2a 2a 20 49 6e 70 75 74 73 3a 0a  ..**.** Inputs:.
20dfd 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** A pointer to 
20dfe 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 75 73 65  the function use
20dff 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65  d to allocate me
20e00 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 4f 75 74 70  mory..**.** Outp
20e01 75 74 73 3a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65  uts:.** A pointe
20e02 72 20 74 6f 20 61 20 70 61 72 73 65 72 2e 20 20  r to a parser.  
20e03 54 68 69 73 20 70 6f 69 6e 74 65 72 20 69 73 20  This pointer is 
20e04 75 73 65 64 20 69 6e 20 73 75 62 73 65 71 75 65  used in subseque
20e05 6e 74 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 73  nt calls.** to s
20e06 71 6c 69 74 65 33 50 61 72 73 65 72 20 61 6e 64  qlite3Parser and
20e07 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 46 72   sqlite3ParserFr
20e08 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ee..*/.SQLITE_PR
20e09 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
20e0a 74 65 33 50 61 72 73 65 72 41 6c 6c 6f 63 28 76  te3ParserAlloc(v
20e0b 6f 69 64 20 2a 28 2a 6d 61 6c 6c 6f 63 50 72 6f  oid *(*mallocPro
20e0c 63 29 28 73 69 7a 65 5f 74 29 29 7b 0a 20 20 79  c)(size_t)){.  y
20e0d 79 50 61 72 73 65 72 20 2a 70 50 61 72 73 65 72  yParser *pParser
20e0e 3b 0a 20 20 70 50 61 72 73 65 72 20 3d 20 28 79  ;.  pParser = (y
20e0f 79 50 61 72 73 65 72 2a 29 28 2a 6d 61 6c 6c 6f  yParser*)(*mallo
20e10 63 50 72 6f 63 29 28 20 28 73 69 7a 65 5f 74 29  cProc)( (size_t)
20e11 73 69 7a 65 6f 66 28 79 79 50 61 72 73 65 72 29  sizeof(yyParser)
20e12 20 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65   );.  if( pParse
20e13 72 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 72  r ){.    pParser
20e14 2d 3e 79 79 69 64 78 20 3d 20 2d 31 3b 0a 23 69  ->yyidx = -1;.#i
20e15 66 20 59 59 53 54 41 43 4b 44 45 50 54 48 3c 3d  f YYSTACKDEPTH<=
20e16 30 0a 20 20 20 20 79 79 47 72 6f 77 53 74 61 63  0.    yyGrowStac
20e17 6b 28 70 50 61 72 73 65 72 29 3b 0a 23 65 6e 64  k(pParser);.#end
20e18 69 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  if.  }.  return 
20e19 70 50 61 72 73 65 72 3b 0a 7d 0a 0a 2f 2a 20 54  pParser;.}../* T
20e1a 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e  he following fun
20e1b 63 74 69 6f 6e 20 64 65 6c 65 74 65 73 20 74 68  ction deletes th
20e1c 65 20 76 61 6c 75 65 20 61 73 73 6f 63 69 61 74  e value associat
20e1d 65 64 20 77 69 74 68 20 61 0a 2a 2a 20 73 79 6d  ed with a.** sym
20e1e 62 6f 6c 2e 20 20 54 68 65 20 73 79 6d 62 6f 6c  bol.  The symbol
20e1f 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 61   can be either a
20e20 20 74 65 72 6d 69 6e 61 6c 20 6f 72 20 6e 6f 6e   terminal or non
20e21 74 65 72 6d 69 6e 61 6c 2e 0a 2a 2a 20 22 79 79  terminal..** "yy
20e22 6d 61 6a 6f 72 22 20 69 73 20 74 68 65 20 73 79  major" is the sy
20e23 6d 62 6f 6c 20 63 6f 64 65 2c 20 61 6e 64 20 22  mbol code, and "
20e24 79 79 70 6d 69 6e 6f 72 22 20 69 73 20 61 20 70  yypminor" is a p
20e25 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 74 68 65  ointer to.** the
20e26 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69   value..*/.stati
20e27 63 20 76 6f 69 64 20 79 79 5f 64 65 73 74 72 75  c void yy_destru
20e28 63 74 6f 72 28 59 59 43 4f 44 45 54 59 50 45 20  ctor(YYCODETYPE 
20e29 79 79 6d 61 6a 6f 72 2c 20 59 59 4d 49 4e 4f 52  yymajor, YYMINOR
20e2a 54 59 50 45 20 2a 79 79 70 6d 69 6e 6f 72 29 7b  TYPE *yypminor){
20e2b 0a 20 20 73 77 69 74 63 68 28 20 79 79 6d 61 6a  .  switch( yymaj
20e2c 6f 72 20 29 7b 0a 20 20 20 20 2f 2a 20 48 65 72  or ){.    /* Her
20e2d 65 20 69 73 20 69 6e 73 65 72 74 65 64 20 74 68  e is inserted th
20e2e 65 20 61 63 74 69 6f 6e 73 20 77 68 69 63 68 20  e actions which 
20e2f 74 61 6b 65 20 70 6c 61 63 65 20 77 68 65 6e 20  take place when 
20e30 61 0a 20 20 20 20 2a 2a 20 74 65 72 6d 69 6e 61  a.    ** termina
20e31 6c 20 6f 72 20 6e 6f 6e 2d 74 65 72 6d 69 6e 61  l or non-termina
20e32 6c 20 69 73 20 64 65 73 74 72 6f 79 65 64 2e 20  l is destroyed. 
20e33 20 54 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e   This can happen
20e34 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20 74 68 65  .    ** when the
20e35 20 73 79 6d 62 6f 6c 20 69 73 20 70 6f 70 70 65   symbol is poppe
20e36 64 20 66 72 6f 6d 20 74 68 65 20 73 74 61 63 6b  d from the stack
20e37 20 64 75 72 69 6e 67 20 61 0a 20 20 20 20 2a 2a   during a.    **
20e38 20 72 65 64 75 63 65 20 6f 72 20 64 75 72 69 6e   reduce or durin
20e39 67 20 65 72 72 6f 72 20 70 72 6f 63 65 73 73 69  g error processi
20e3a 6e 67 20 6f 72 20 77 68 65 6e 20 61 20 70 61 72  ng or when a par
20e3b 73 65 72 20 69 73 20 0a 20 20 20 20 2a 2a 20 62  ser is .    ** b
20e3c 65 69 6e 67 20 64 65 73 74 72 6f 79 65 64 20 62  eing destroyed b
20e3d 65 66 6f 72 65 20 69 74 20 69 73 20 66 69 6e 69  efore it is fini
20e3e 73 68 65 64 20 70 61 72 73 69 6e 67 2e 0a 20 20  shed parsing..  
20e3f 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f 74 65    **.    ** Note
20e40 3a 20 64 75 72 69 6e 67 20 61 20 72 65 64 75 63  : during a reduc
20e41 65 2c 20 74 68 65 20 6f 6e 6c 79 20 73 79 6d 62  e, the only symb
20e42 6f 6c 73 20 64 65 73 74 72 6f 79 65 64 20 61 72  ols destroyed ar
20e43 65 20 74 68 6f 73 65 0a 20 20 20 20 2a 2a 20 77  e those.    ** w
20e44 68 69 63 68 20 61 70 70 65 61 72 20 6f 6e 20 74  hich appear on t
20e45 68 65 20 52 48 53 20 6f 66 20 74 68 65 20 72 75  he RHS of the ru
20e46 6c 65 2c 20 62 75 74 20 77 68 69 63 68 20 61 72  le, but which ar
20e47 65 20 6e 6f 74 20 75 73 65 64 0a 20 20 20 20 2a  e not used.    *
20e48 2a 20 69 6e 73 69 64 65 20 74 68 65 20 43 20 63  * inside the C c
20e49 6f 64 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ode..    */.    
20e4a 63 61 73 65 20 31 35 35 3a 0a 20 20 20 20 63 61  case 155:.    ca
20e4b 73 65 20 31 38 39 3a 0a 20 20 20 20 63 61 73 65  se 189:.    case
20e4c 20 32 30 36 3a 0a 7b 73 71 6c 69 74 65 33 53 65   206:.{sqlite3Se
20e4d 6c 65 63 74 44 65 6c 65 74 65 28 28 79 79 70 6d  lectDelete((yypm
20e4e 69 6e 6f 72 2d 3e 79 79 32 31 39 29 29 3b 7d 0a  inor->yy219));}.
20e4f 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
20e50 20 63 61 73 65 20 31 36 39 3a 0a 20 20 20 20 63   case 169:.    c
20e51 61 73 65 20 31 37 30 3a 0a 20 20 20 20 63 61 73  ase 170:.    cas
20e52 65 20 31 39 34 3a 0a 20 20 20 20 63 61 73 65 20  e 194:.    case 
20e53 31 39 36 3a 0a 20 20 20 20 63 61 73 65 20 32 30  196:.    case 20
20e54 34 3a 0a 20 20 20 20 63 61 73 65 20 32 31 30 3a  4:.    case 210:
20e55 0a 20 20 20 20 63 61 73 65 20 32 31 38 3a 0a 20  .    case 218:. 
20e56 20 20 20 63 61 73 65 20 32 32 31 3a 0a 20 20 20     case 221:.   
20e57 20 63 61 73 65 20 32 32 33 3a 0a 20 20 20 20 63   case 223:.    c
20e58 61 73 65 20 32 33 35 3a 0a 7b 73 71 6c 69 74 65  ase 235:.{sqlite
20e59 33 45 78 70 72 44 65 6c 65 74 65 28 28 79 79 70  3ExprDelete((yyp
20e5a 6d 69 6e 6f 72 2d 3e 79 79 31 37 32 29 29 3b 7d  minor->yy172));}
20e5b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
20e5c 20 20 63 61 73 65 20 31 37 34 3a 0a 20 20 20 20    case 174:.    
20e5d 63 61 73 65 20 31 38 32 3a 0a 20 20 20 20 63 61  case 182:.    ca
20e5e 73 65 20 31 39 32 3a 0a 20 20 20 20 63 61 73 65  se 192:.    case
20e5f 20 31 39 35 3a 0a 20 20 20 20 63 61 73 65 20 31   195:.    case 1
20e60 39 37 3a 0a 20 20 20 20 63 61 73 65 20 31 39 39  97:.    case 199
20e61 3a 0a 20 20 20 20 63 61 73 65 20 32 30 39 3a 0a  :.    case 209:.
20e62 20 20 20 20 63 61 73 65 20 32 31 31 3a 0a 20 20      case 211:.  
20e63 20 20 63 61 73 65 20 32 31 32 3a 0a 20 20 20 20    case 212:.    
20e64 63 61 73 65 20 32 31 35 3a 0a 20 20 20 20 63 61  case 215:.    ca
20e65 73 65 20 32 31 36 3a 0a 20 20 20 20 63 61 73 65  se 216:.    case
20e66 20 32 32 32 3a 0a 7b 73 71 6c 69 74 65 33 45 78   222:.{sqlite3Ex
20e67 70 72 4c 69 73 74 44 65 6c 65 74 65 28 28 79 79  prListDelete((yy
20e68 70 6d 69 6e 6f 72 2d 3e 79 79 31 37 34 29 29 3b  pminor->yy174));
20e69 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
20e6a 20 20 20 63 61 73 65 20 31 38 38 3a 0a 20 20 20     case 188:.   
20e6b 20 63 61 73 65 20 31 39 33 3a 0a 20 20 20 20 63   case 193:.    c
20e6c 61 73 65 20 32 30 31 3a 0a 20 20 20 20 63 61 73  ase 201:.    cas
20e6d 65 20 32 30 32 3a 0a 7b 73 71 6c 69 74 65 33 53  e 202:.{sqlite3S
20e6e 72 63 4c 69 73 74 44 65 6c 65 74 65 28 28 79 79  rcListDelete((yy
20e6f 70 6d 69 6e 6f 72 2d 3e 79 79 33 37 33 29 29 3b  pminor->yy373));
20e70 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
20e71 20 20 20 63 61 73 65 20 32 30 35 3a 0a 20 20 20     case 205:.   
20e72 20 63 61 73 65 20 32 30 38 3a 0a 20 20 20 20 63   case 208:.    c
20e73 61 73 65 20 32 31 34 3a 0a 7b 73 71 6c 69 74 65  ase 214:.{sqlite
20e74 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 28 79  3IdListDelete((y
20e75 79 70 6d 69 6e 6f 72 2d 3e 79 79 34 33 32 29 29  ypminor->yy432))
20e76 3b 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  ;}.      break;.
20e77 20 20 20 20 63 61 73 65 20 32 33 31 3a 0a 20 20      case 231:.  
20e78 20 20 63 61 73 65 20 32 33 36 3a 0a 7b 73 71 6c    case 236:.{sql
20e79 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65  ite3DeleteTrigge
20e7a 72 53 74 65 70 28 28 79 79 70 6d 69 6e 6f 72 2d  rStep((yypminor-
20e7b 3e 79 79 32 34 33 29 29 3b 7d 0a 20 20 20 20 20  >yy243));}.     
20e7c 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
20e7d 20 32 33 33 3a 0a 7b 73 71 6c 69 74 65 33 49 64   233:.{sqlite3Id
20e7e 4c 69 73 74 44 65 6c 65 74 65 28 28 79 79 70 6d  ListDelete((yypm
20e7f 69 6e 6f 72 2d 3e 79 79 33 37 30 29 2e 62 29 3b  inor->yy370).b);
20e80 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
20e81 20 20 20 63 61 73 65 20 32 33 38 3a 0a 7b 73 71     case 238:.{sq
20e82 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
20e83 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 33 38 36  (yypminor->yy386
20e84 29 29 3b 7d 0a 20 20 20 20 20 20 62 72 65 61 6b  ));}.      break
20e85 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20  ;.    default:  
20e86 62 72 65 61 6b 3b 20 20 20 2f 2a 20 49 66 20 6e  break;   /* If n
20e87 6f 20 64 65 73 74 72 75 63 74 6f 72 20 61 63 74  o destructor act
20e88 69 6f 6e 20 73 70 65 63 69 66 69 65 64 3a 20 64  ion specified: d
20e89 6f 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 7d  o nothing */.  }
20e8a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70 20 74 68  .}../*.** Pop th
20e8b 65 20 70 61 72 73 65 72 27 73 20 73 74 61 63 6b  e parser's stack
20e8c 20 6f 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   once..**.** If 
20e8d 74 68 65 72 65 20 69 73 20 61 20 64 65 73 74 72  there is a destr
20e8e 75 63 74 6f 72 20 72 6f 75 74 69 6e 65 20 61 73  uctor routine as
20e8f 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
20e90 65 20 74 6f 6b 65 6e 20 77 68 69 63 68 0a 2a 2a  e token which.**
20e91 20 69 73 20 70 6f 70 70 65 64 20 66 72 6f 6d 20   is popped from 
20e92 74 68 65 20 73 74 61 63 6b 2c 20 74 68 65 6e 20  the stack, then 
20e93 63 61 6c 6c 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 52  call it..**.** R
20e94 65 74 75 72 6e 20 74 68 65 20 6d 61 6a 6f 72 20  eturn the major 
20e95 74 6f 6b 65 6e 20 6e 75 6d 62 65 72 20 66 6f 72  token number for
20e96 20 74 68 65 20 73 79 6d 62 6f 6c 20 70 6f 70 70   the symbol popp
20e97 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ed..*/.static in
20e98 74 20 79 79 5f 70 6f 70 5f 70 61 72 73 65 72 5f  t yy_pop_parser_
20e99 73 74 61 63 6b 28 79 79 50 61 72 73 65 72 20 2a  stack(yyParser *
20e9a 70 50 61 72 73 65 72 29 7b 0a 20 20 59 59 43 4f  pParser){.  YYCO
20e9b 44 45 54 59 50 45 20 79 79 6d 61 6a 6f 72 3b 0a  DETYPE yymajor;.
20e9c 20 20 79 79 53 74 61 63 6b 45 6e 74 72 79 20 2a    yyStackEntry *
20e9d 79 79 74 6f 73 20 3d 20 26 70 50 61 72 73 65 72  yytos = &pParser
20e9e 2d 3e 79 79 73 74 61 63 6b 5b 70 50 61 72 73 65  ->yystack[pParse
20e9f 72 2d 3e 79 79 69 64 78 5d 3b 0a 0a 20 20 69 66  r->yyidx];..  if
20ea0 28 20 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  ( pParser->yyidx
20ea1 3c 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23  <0 ) return 0;.#
20ea2 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20  ifndef NDEBUG.  
20ea3 69 66 28 20 79 79 54 72 61 63 65 46 49 4c 45 20  if( yyTraceFILE 
20ea4 26 26 20 70 50 61 72 73 65 72 2d 3e 79 79 69 64  && pParser->yyid
20ea5 78 3e 3d 30 20 29 7b 0a 20 20 20 20 66 70 72 69  x>=0 ){.    fpri
20ea6 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c  ntf(yyTraceFILE,
20ea7 22 25 73 50 6f 70 70 69 6e 67 20 25 73 5c 6e 22  "%sPopping %s\n"
20ea8 2c 0a 20 20 20 20 20 20 79 79 54 72 61 63 65 50  ,.      yyTraceP
20ea9 72 6f 6d 70 74 2c 0a 20 20 20 20 20 20 79 79 54  rompt,.      yyT
20eaa 6f 6b 65 6e 4e 61 6d 65 5b 79 79 74 6f 73 2d 3e  okenName[yytos->
20eab 6d 61 6a 6f 72 5d 29 3b 0a 20 20 7d 0a 23 65 6e  major]);.  }.#en
20eac 64 69 66 0a 20 20 79 79 6d 61 6a 6f 72 20 3d 20  dif.  yymajor = 
20ead 79 79 74 6f 73 2d 3e 6d 61 6a 6f 72 3b 0a 20 20  yytos->major;.  
20eae 79 79 5f 64 65 73 74 72 75 63 74 6f 72 28 20 79  yy_destructor( y
20eaf 79 6d 61 6a 6f 72 2c 20 26 79 79 74 6f 73 2d 3e  ymajor, &yytos->
20eb0 6d 69 6e 6f 72 29 3b 0a 20 20 70 50 61 72 73 65  minor);.  pParse
20eb1 72 2d 3e 79 79 69 64 78 2d 2d 3b 0a 20 20 72 65  r->yyidx--;.  re
20eb2 74 75 72 6e 20 79 79 6d 61 6a 6f 72 3b 0a 7d 0a  turn yymajor;.}.
20eb3 0a 2f 2a 20 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61  ./* .** Dealloca
20eb4 74 65 20 61 6e 64 20 64 65 73 74 72 6f 79 20 61  te and destroy a
20eb5 20 70 61 72 73 65 72 2e 20 20 44 65 73 74 72 75   parser.  Destru
20eb6 63 74 6f 72 73 20 61 72 65 20 61 6c 6c 20 63 61  ctors are all ca
20eb7 6c 6c 65 64 20 66 6f 72 0a 2a 2a 20 61 6c 6c 20  lled for.** all 
20eb8 73 74 61 63 6b 20 65 6c 65 6d 65 6e 74 73 20 62  stack elements b
20eb9 65 66 6f 72 65 20 73 68 75 74 74 69 6e 67 20 74  efore shutting t
20eba 68 65 20 70 61 72 73 65 72 20 64 6f 77 6e 2e 0a  he parser down..
20ebb 2a 2a 0a 2a 2a 20 49 6e 70 75 74 73 3a 0a 2a 2a  **.** Inputs:.**
20ebc 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 41   <ul>.** <li>  A
20ebd 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
20ebe 70 61 72 73 65 72 2e 20 20 54 68 69 73 20 73 68  parser.  This sh
20ebf 6f 75 6c 64 20 62 65 20 61 20 70 6f 69 6e 74 65  ould be a pointe
20ec0 72 0a 2a 2a 20 20 20 20 20 20 20 6f 62 74 61 69  r.**       obtai
20ec1 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
20ec2 50 61 72 73 65 72 41 6c 6c 6f 63 2e 0a 2a 2a 20  ParserAlloc..** 
20ec3 3c 6c 69 3e 20 20 41 20 70 6f 69 6e 74 65 72 20  <li>  A pointer 
20ec4 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 75 73  to a function us
20ec5 65 64 20 74 6f 20 72 65 63 6c 61 69 6d 20 6d 65  ed to reclaim me
20ec6 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 0a 2a 2a  mory obtained.**
20ec7 20 20 20 20 20 20 20 66 72 6f 6d 20 6d 61 6c 6c         from mall
20ec8 6f 63 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2f 0a  oc..** </ul>.*/.
20ec9 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
20eca 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72 73 65  oid sqlite3Parse
20ecb 72 46 72 65 65 28 0a 20 20 76 6f 69 64 20 2a 70  rFree(.  void *p
20ecc 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
20ecd 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
20ece 65 72 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64  er to be deleted
20ecf 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 66 72 65   */.  void (*fre
20ed0 65 50 72 6f 63 29 28 76 6f 69 64 2a 29 20 20 20  eProc)(void*)   
20ed1 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 75 73    /* Function us
20ed2 65 64 20 74 6f 20 72 65 63 6c 61 69 6d 20 6d 65  ed to reclaim me
20ed3 6d 6f 72 79 20 2a 2f 0a 29 7b 0a 20 20 79 79 50  mory */.){.  yyP
20ed4 61 72 73 65 72 20 2a 70 50 61 72 73 65 72 20 3d  arser *pParser =
20ed5 20 28 79 79 50 61 72 73 65 72 2a 29 70 3b 0a 20   (yyParser*)p;. 
20ed6 20 69 66 28 20 70 50 61 72 73 65 72 3d 3d 30 20   if( pParser==0 
20ed7 29 20 72 65 74 75 72 6e 3b 0a 20 20 77 68 69 6c  ) return;.  whil
20ed8 65 28 20 70 50 61 72 73 65 72 2d 3e 79 79 69 64  e( pParser->yyid
20ed9 78 3e 3d 30 20 29 20 79 79 5f 70 6f 70 5f 70 61  x>=0 ) yy_pop_pa
20eda 72 73 65 72 5f 73 74 61 63 6b 28 70 50 61 72 73  rser_stack(pPars
20edb 65 72 29 3b 0a 23 69 66 20 59 59 53 54 41 43 4b  er);.#if YYSTACK
20edc 44 45 50 54 48 3c 3d 30 0a 20 20 66 72 65 65 28  DEPTH<=0.  free(
20edd 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b  pParser->yystack
20ede 29 3b 0a 23 65 6e 64 69 66 0a 20 20 28 2a 66 72  );.#endif.  (*fr
20edf 65 65 50 72 6f 63 29 28 28 76 6f 69 64 2a 29 70  eeProc)((void*)p
20ee0 50 61 72 73 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  Parser);.}../*.*
20ee1 2a 20 46 69 6e 64 20 74 68 65 20 61 70 70 72 6f  * Find the appro
20ee2 70 72 69 61 74 65 20 61 63 74 69 6f 6e 20 66 6f  priate action fo
20ee3 72 20 61 20 70 61 72 73 65 72 20 67 69 76 65 6e  r a parser given
20ee4 20 74 68 65 20 74 65 72 6d 69 6e 61 6c 0a 2a 2a   the terminal.**
20ee5 20 6c 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65   look-ahead toke
20ee6 6e 20 69 4c 6f 6f 6b 41 68 65 61 64 2e 0a 2a 2a  n iLookAhead..**
20ee7 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 6f 6b 2d  .** If the look-
20ee8 61 68 65 61 64 20 74 6f 6b 65 6e 20 69 73 20 59  ahead token is Y
20ee9 59 4e 4f 43 4f 44 45 2c 20 74 68 65 6e 20 63 68  YNOCODE, then ch
20eea 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68  eck to see if th
20eeb 65 20 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 69  e action is.** i
20eec 6e 64 65 70 65 6e 64 65 6e 74 20 6f 66 20 74 68  ndependent of th
20eed 65 20 6c 6f 6f 6b 2d 61 68 65 61 64 2e 20 20 49  e look-ahead.  I
20eee 66 20 69 74 20 69 73 2c 20 72 65 74 75 72 6e 20  f it is, return 
20eef 74 68 65 20 61 63 74 69 6f 6e 2c 20 6f 74 68 65  the action, othe
20ef0 72 77 69 73 65 0a 2a 2a 20 72 65 74 75 72 6e 20  rwise.** return 
20ef1 59 59 5f 4e 4f 5f 41 43 54 49 4f 4e 2e 0a 2a 2f  YY_NO_ACTION..*/
20ef2 0a 73 74 61 74 69 63 20 69 6e 74 20 79 79 5f 66  .static int yy_f
20ef3 69 6e 64 5f 73 68 69 66 74 5f 61 63 74 69 6f 6e  ind_shift_action
20ef4 28 0a 20 20 79 79 50 61 72 73 65 72 20 2a 70 50  (.  yyParser *pP
20ef5 61 72 73 65 72 2c 20 20 20 20 20 20 20 20 2f 2a  arser,        /*
20ef6 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 20   The parser */. 
20ef7 20 59 59 43 4f 44 45 54 59 50 45 20 69 4c 6f 6f   YYCODETYPE iLoo
20ef8 6b 41 68 65 61 64 20 20 20 20 20 2f 2a 20 54 68  kAhead     /* Th
20ef9 65 20 6c 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b  e look-ahead tok
20efa 65 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  en */.){.  int i
20efb 3b 0a 20 20 69 6e 74 20 73 74 61 74 65 6e 6f 20  ;.  int stateno 
20efc 3d 20 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61  = pParser->yysta
20efd 63 6b 5b 70 50 61 72 73 65 72 2d 3e 79 79 69 64  ck[pParser->yyid
20efe 78 5d 2e 73 74 61 74 65 6e 6f 3b 0a 20 0a 20 20  x].stateno;. .  
20eff 69 66 28 20 73 74 61 74 65 6e 6f 3e 59 59 5f 53  if( stateno>YY_S
20f00 48 49 46 54 5f 4d 41 58 20 7c 7c 20 28 69 20 3d  HIFT_MAX || (i =
20f01 20 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b 73   yy_shift_ofst[s
20f02 74 61 74 65 6e 6f 5d 29 3d 3d 59 59 5f 53 48 49  tateno])==YY_SHI
20f03 46 54 5f 55 53 45 5f 44 46 4c 54 20 29 7b 0a 20  FT_USE_DFLT ){. 
20f04 20 20 20 72 65 74 75 72 6e 20 79 79 5f 64 65 66     return yy_def
20f05 61 75 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20  ault[stateno];. 
20f06 20 7d 0a 20 20 69 66 28 20 69 4c 6f 6f 6b 41 68   }.  if( iLookAh
20f07 65 61 64 3d 3d 59 59 4e 4f 43 4f 44 45 20 29 7b  ead==YYNOCODE ){
20f08 0a 20 20 20 20 72 65 74 75 72 6e 20 59 59 5f 4e  .    return YY_N
20f09 4f 5f 41 43 54 49 4f 4e 3b 0a 20 20 7d 0a 20 20  O_ACTION;.  }.  
20f0a 69 20 2b 3d 20 69 4c 6f 6f 6b 41 68 65 61 64 3b  i += iLookAhead;
20f0b 0a 20 20 69 66 28 20 69 3c 30 20 7c 7c 20 69 3e  .  if( i<0 || i>
20f0c 3d 59 59 5f 53 5a 5f 41 43 54 54 41 42 20 7c 7c  =YY_SZ_ACTTAB ||
20f0d 20 79 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b 69 5d   yy_lookahead[i]
20f0e 21 3d 69 4c 6f 6f 6b 41 68 65 61 64 20 29 7b 0a  !=iLookAhead ){.
20f0f 20 20 20 20 69 66 28 20 69 4c 6f 6f 6b 41 68 65      if( iLookAhe
20f10 61 64 3e 30 20 29 7b 0a 23 69 66 64 65 66 20 59  ad>0 ){.#ifdef Y
20f11 59 46 41 4c 4c 42 41 43 4b 0a 20 20 20 20 20 20  YFALLBACK.      
20f12 69 6e 74 20 69 46 61 6c 6c 62 61 63 6b 3b 20 20  int iFallback;  
20f13 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6c            /* Fal
20f14 6c 62 61 63 6b 20 74 6f 6b 65 6e 20 2a 2f 0a 20  lback token */. 
20f15 20 20 20 20 20 69 66 28 20 69 4c 6f 6f 6b 41 68       if( iLookAh
20f16 65 61 64 3c 73 69 7a 65 6f 66 28 79 79 46 61 6c  ead<sizeof(yyFal
20f17 6c 62 61 63 6b 29 2f 73 69 7a 65 6f 66 28 79 79  lback)/sizeof(yy
20f18 46 61 6c 6c 62 61 63 6b 5b 30 5d 29 0a 20 20 20  Fallback[0]).   
20f19 20 20 20 20 20 20 20 20 20 20 26 26 20 28 69 46            && (iF
20f1a 61 6c 6c 62 61 63 6b 20 3d 20 79 79 46 61 6c 6c  allback = yyFall
20f1b 62 61 63 6b 5b 69 4c 6f 6f 6b 41 68 65 61 64 5d  back[iLookAhead]
20f1c 29 21 3d 30 20 29 7b 0a 23 69 66 6e 64 65 66 20  )!=0 ){.#ifndef 
20f1d 4e 44 45 42 55 47 0a 20 20 20 20 20 20 20 20 69  NDEBUG.        i
20f1e 66 28 20 79 79 54 72 61 63 65 46 49 4c 45 20 29  f( yyTraceFILE )
20f1f 7b 0a 20 20 20 20 20 20 20 20 20 20 66 70 72 69  {.          fpri
20f20 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c  ntf(yyTraceFILE,
20f21 20 22 25 73 46 41 4c 4c 42 41 43 4b 20 25 73 20   "%sFALLBACK %s 
20f22 3d 3e 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20  => %s\n",.      
20f23 20 20 20 20 20 20 20 79 79 54 72 61 63 65 50 72         yyTracePr
20f24 6f 6d 70 74 2c 20 79 79 54 6f 6b 65 6e 4e 61 6d  ompt, yyTokenNam
20f25 65 5b 69 4c 6f 6f 6b 41 68 65 61 64 5d 2c 20 79  e[iLookAhead], y
20f26 79 54 6f 6b 65 6e 4e 61 6d 65 5b 69 46 61 6c 6c  yTokenName[iFall
20f27 62 61 63 6b 5d 29 3b 0a 20 20 20 20 20 20 20 20  back]);.        
20f28 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20  }.#endif.       
20f29 20 72 65 74 75 72 6e 20 79 79 5f 66 69 6e 64 5f   return yy_find_
20f2a 73 68 69 66 74 5f 61 63 74 69 6f 6e 28 70 50 61  shift_action(pPa
20f2b 72 73 65 72 2c 20 69 46 61 6c 6c 62 61 63 6b 29  rser, iFallback)
20f2c 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66  ;.      }.#endif
20f2d 0a 23 69 66 64 65 66 20 59 59 57 49 4c 44 43 41  .#ifdef YYWILDCA
20f2e 52 44 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20  RD.      {.     
20f2f 20 20 20 69 6e 74 20 6a 20 3d 20 69 20 2d 20 69     int j = i - i
20f30 4c 6f 6f 6b 41 68 65 61 64 20 2b 20 59 59 57 49  LookAhead + YYWI
20f31 4c 44 43 41 52 44 3b 0a 20 20 20 20 20 20 20 20  LDCARD;.        
20f32 69 66 28 20 6a 3e 3d 30 20 26 26 20 6a 3c 59 59  if( j>=0 && j<YY
20f33 5f 53 5a 5f 41 43 54 54 41 42 20 26 26 20 79 79  _SZ_ACTTAB && yy
20f34 5f 6c 6f 6f 6b 61 68 65 61 64 5b 6a 5d 3d 3d 59  _lookahead[j]==Y
20f35 59 57 49 4c 44 43 41 52 44 20 29 7b 0a 23 69 66  YWILDCARD ){.#if
20f36 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20  ndef NDEBUG.    
20f37 20 20 20 20 20 20 69 66 28 20 79 79 54 72 61 63        if( yyTrac
20f38 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 20 20 20  eFILE ){.       
20f39 20 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54       fprintf(yyT
20f3a 72 61 63 65 46 49 4c 45 2c 20 22 25 73 57 49 4c  raceFILE, "%sWIL
20f3b 44 43 41 52 44 20 25 73 20 3d 3e 20 25 73 5c 6e  DCARD %s => %s\n
20f3c 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
20f3d 20 20 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c    yyTracePrompt,
20f3e 20 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 69 4c 6f   yyTokenName[iLo
20f3f 6f 6b 41 68 65 61 64 5d 2c 20 79 79 54 6f 6b 65  okAhead], yyToke
20f40 6e 4e 61 6d 65 5b 59 59 57 49 4c 44 43 41 52 44  nName[YYWILDCARD
20f41 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ]);.          }.
20f42 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47  #endif /* NDEBUG
20f43 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 65   */.          re
20f44 74 75 72 6e 20 79 79 5f 61 63 74 69 6f 6e 5b 6a  turn yy_action[j
20f45 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  ];.        }.   
20f46 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 59     }.#endif /* Y
20f47 59 57 49 4c 44 43 41 52 44 20 2a 2f 0a 20 20 20  YWILDCARD */.   
20f48 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 79 79   }.    return yy
20f49 5f 64 65 66 61 75 6c 74 5b 73 74 61 74 65 6e 6f  _default[stateno
20f4a 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  ];.  }else{.    
20f4b 72 65 74 75 72 6e 20 79 79 5f 61 63 74 69 6f 6e  return yy_action
20f4c 5b 69 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  [i];.  }.}../*.*
20f4d 2a 20 46 69 6e 64 20 74 68 65 20 61 70 70 72 6f  * Find the appro
20f4e 70 72 69 61 74 65 20 61 63 74 69 6f 6e 20 66 6f  priate action fo
20f4f 72 20 61 20 70 61 72 73 65 72 20 67 69 76 65 6e  r a parser given
20f50 20 74 68 65 20 6e 6f 6e 2d 74 65 72 6d 69 6e 61   the non-termina
20f51 6c 0a 2a 2a 20 6c 6f 6f 6b 2d 61 68 65 61 64 20  l.** look-ahead 
20f52 74 6f 6b 65 6e 20 69 4c 6f 6f 6b 41 68 65 61 64  token iLookAhead
20f53 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c  ..**.** If the l
20f54 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20  ook-ahead token 
20f55 69 73 20 59 59 4e 4f 43 4f 44 45 2c 20 74 68 65  is YYNOCODE, the
20f56 6e 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69  n check to see i
20f57 66 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73 0a  f the action is.
20f58 2a 2a 20 69 6e 64 65 70 65 6e 64 65 6e 74 20 6f  ** independent o
20f59 66 20 74 68 65 20 6c 6f 6f 6b 2d 61 68 65 61 64  f the look-ahead
20f5a 2e 20 20 49 66 20 69 74 20 69 73 2c 20 72 65 74  .  If it is, ret
20f5b 75 72 6e 20 74 68 65 20 61 63 74 69 6f 6e 2c 20  urn the action, 
20f5c 6f 74 68 65 72 77 69 73 65 0a 2a 2a 20 72 65 74  otherwise.** ret
20f5d 75 72 6e 20 59 59 5f 4e 4f 5f 41 43 54 49 4f 4e  urn YY_NO_ACTION
20f5e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
20f5f 79 79 5f 66 69 6e 64 5f 72 65 64 75 63 65 5f 61  yy_find_reduce_a
20f60 63 74 69 6f 6e 28 0a 20 20 69 6e 74 20 73 74 61  ction(.  int sta
20f61 74 65 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20  teno,           
20f62 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 73 74     /* Current st
20f63 61 74 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  ate number */.  
20f64 59 59 43 4f 44 45 54 59 50 45 20 69 4c 6f 6f 6b  YYCODETYPE iLook
20f65 41 68 65 61 64 20 20 20 20 20 2f 2a 20 54 68 65  Ahead     /* The
20f66 20 6c 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65   look-ahead toke
20f67 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b  n */.){.  int i;
20f68 0a 20 20 2f 2a 20 69 6e 74 20 73 74 61 74 65 6e  .  /* int staten
20f69 6f 20 3d 20 70 50 61 72 73 65 72 2d 3e 79 79 73  o = pParser->yys
20f6a 74 61 63 6b 5b 70 50 61 72 73 65 72 2d 3e 79 79  tack[pParser->yy
20f6b 69 64 78 5d 2e 73 74 61 74 65 6e 6f 3b 20 2a 2f  idx].stateno; */
20f6c 0a 20 0a 20 20 69 66 28 20 73 74 61 74 65 6e 6f  . .  if( stateno
20f6d 3e 59 59 5f 52 45 44 55 43 45 5f 4d 41 58 20 7c  >YY_REDUCE_MAX |
20f6e 7c 0a 20 20 20 20 20 20 28 69 20 3d 20 79 79 5f  |.      (i = yy_
20f6f 72 65 64 75 63 65 5f 6f 66 73 74 5b 73 74 61 74  reduce_ofst[stat
20f70 65 6e 6f 5d 29 3d 3d 59 59 5f 52 45 44 55 43 45  eno])==YY_REDUCE
20f71 5f 55 53 45 5f 44 46 4c 54 20 29 7b 0a 20 20 20  _USE_DFLT ){.   
20f72 20 72 65 74 75 72 6e 20 79 79 5f 64 65 66 61 75   return yy_defau
20f73 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20 20 7d  lt[stateno];.  }
20f74 0a 20 20 69 66 28 20 69 4c 6f 6f 6b 41 68 65 61  .  if( iLookAhea
20f75 64 3d 3d 59 59 4e 4f 43 4f 44 45 20 29 7b 0a 20  d==YYNOCODE ){. 
20f76 20 20 20 72 65 74 75 72 6e 20 59 59 5f 4e 4f 5f     return YY_NO_
20f77 41 43 54 49 4f 4e 3b 0a 20 20 7d 0a 20 20 69 20  ACTION;.  }.  i 
20f78 2b 3d 20 69 4c 6f 6f 6b 41 68 65 61 64 3b 0a 20  += iLookAhead;. 
20f79 20 69 66 28 20 69 3c 30 20 7c 7c 20 69 3e 3d 59   if( i<0 || i>=Y
20f7a 59 5f 53 5a 5f 41 43 54 54 41 42 20 7c 7c 20 79  Y_SZ_ACTTAB || y
20f7b 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b 69 5d 21 3d  y_lookahead[i]!=
20f7c 69 4c 6f 6f 6b 41 68 65 61 64 20 29 7b 0a 20 20  iLookAhead ){.  
20f7d 20 20 72 65 74 75 72 6e 20 79 79 5f 64 65 66 61    return yy_defa
20f7e 75 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20 20  ult[stateno];.  
20f7f 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72  }else{.    retur
20f80 6e 20 79 79 5f 61 63 74 69 6f 6e 5b 69 5d 3b 0a  n yy_action[i];.
20f81 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65    }.}../*.** The
20f82 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69   following routi
20f83 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 69 66 20  ne is called if 
20f84 74 68 65 20 73 74 61 63 6b 20 6f 76 65 72 66 6c  the stack overfl
20f85 6f 77 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ows..*/.static v
20f86 6f 69 64 20 79 79 53 74 61 63 6b 4f 76 65 72 66  oid yyStackOverf
20f87 6c 6f 77 28 79 79 50 61 72 73 65 72 20 2a 79 79  low(yyParser *yy
20f88 70 50 61 72 73 65 72 2c 20 59 59 4d 49 4e 4f 52  pParser, YYMINOR
20f89 54 59 50 45 20 2a 79 79 70 4d 69 6e 6f 72 29 7b  TYPE *yypMinor){
20f8a 0a 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65  .   sqlite3Parse
20f8b 72 41 52 47 5f 46 45 54 43 48 3b 0a 20 20 20 79  rARG_FETCH;.   y
20f8c 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 2d  ypParser->yyidx-
20f8d 2d 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  -;.#ifndef NDEBU
20f8e 47 0a 20 20 20 69 66 28 20 79 79 54 72 61 63 65  G.   if( yyTrace
20f8f 46 49 4c 45 20 29 7b 0a 20 20 20 20 20 66 70 72  FILE ){.     fpr
20f90 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45  intf(yyTraceFILE
20f91 2c 22 25 73 53 74 61 63 6b 20 4f 76 65 72 66 6c  ,"%sStack Overfl
20f92 6f 77 21 5c 6e 22 2c 79 79 54 72 61 63 65 50 72  ow!\n",yyTracePr
20f93 6f 6d 70 74 29 3b 0a 20 20 20 7d 0a 23 65 6e 64  ompt);.   }.#end
20f94 69 66 0a 20 20 20 77 68 69 6c 65 28 20 79 79 70  if.   while( yyp
20f95 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d 30  Parser->yyidx>=0
20f96 20 29 20 79 79 5f 70 6f 70 5f 70 61 72 73 65 72   ) yy_pop_parser
20f97 5f 73 74 61 63 6b 28 79 79 70 50 61 72 73 65 72  _stack(yypParser
20f98 29 3b 0a 20 20 20 2f 2a 20 48 65 72 65 20 63 6f  );.   /* Here co
20f99 64 65 20 69 73 20 69 6e 73 65 72 74 65 64 20 77  de is inserted w
20f9a 68 69 63 68 20 77 69 6c 6c 20 65 78 65 63 75 74  hich will execut
20f9b 65 20 69 66 20 74 68 65 20 70 61 72 73 65 72 0a  e if the parser.
20f9c 20 20 20 2a 2a 20 73 74 61 63 6b 20 65 76 65 72     ** stack ever
20f9d 79 20 6f 76 65 72 66 6c 6f 77 73 20 2a 2f 0a 0a  y overflows */..
20f9e 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
20f9f 67 28 70 50 61 72 73 65 2c 20 22 70 61 72 73 65  g(pParse, "parse
20fa0 72 20 73 74 61 63 6b 20 6f 76 65 72 66 6c 6f 77  r stack overflow
20fa1 22 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70 61  ");.  pParse->pa
20fa2 72 73 65 45 72 72 6f 72 20 3d 20 31 3b 0a 20 20  rseError = 1;.  
20fa3 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52   sqlite3ParserAR
20fa4 47 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75 70 70  G_STORE; /* Supp
20fa5 72 65 73 73 20 77 61 72 6e 69 6e 67 20 61 62 6f  ress warning abo
20fa6 75 74 20 75 6e 75 73 65 64 20 25 65 78 74 72 61  ut unused %extra
20fa7 5f 61 72 67 75 6d 65 6e 74 20 76 61 72 20 2a 2f  _argument var */
20fa8 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72  .}../*.** Perfor
20fa9 6d 20 61 20 73 68 69 66 74 20 61 63 74 69 6f 6e  m a shift action
20faa 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
20fab 20 79 79 5f 73 68 69 66 74 28 0a 20 20 79 79 50   yy_shift(.  yyP
20fac 61 72 73 65 72 20 2a 79 79 70 50 61 72 73 65 72  arser *yypParser
20fad 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ,          /* Th
20fae 65 20 70 61 72 73 65 72 20 74 6f 20 62 65 20 73  e parser to be s
20faf 68 69 66 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20  hifted */.  int 
20fb0 79 79 4e 65 77 53 74 61 74 65 2c 20 20 20 20 20  yyNewState,     
20fb1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
20fb2 20 6e 65 77 20 73 74 61 74 65 20 74 6f 20 73 68   new state to sh
20fb3 69 66 74 20 69 6e 20 2a 2f 0a 20 20 69 6e 74 20  ift in */.  int 
20fb4 79 79 4d 61 6a 6f 72 2c 20 20 20 20 20 20 20 20  yyMajor,        
20fb5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
20fb6 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 74 6f 20   major token to 
20fb7 73 68 69 66 74 20 69 6e 20 2a 2f 0a 20 20 59 59  shift in */.  YY
20fb8 4d 49 4e 4f 52 54 59 50 45 20 2a 79 79 70 4d 69  MINORTYPE *yypMi
20fb9 6e 6f 72 20 20 20 20 20 20 20 20 20 2f 2a 20 50  nor         /* P
20fba 6f 69 6e 74 65 72 20 6f 74 20 74 68 65 20 6d 69  ointer ot the mi
20fbb 6e 6f 72 20 74 6f 6b 65 6e 20 74 6f 20 73 68 69  nor token to shi
20fbc 66 74 20 69 6e 20 2a 2f 0a 29 7b 0a 20 20 79 79  ft in */.){.  yy
20fbd 53 74 61 63 6b 45 6e 74 72 79 20 2a 79 79 74 6f  StackEntry *yyto
20fbe 73 3b 0a 20 20 79 79 70 50 61 72 73 65 72 2d 3e  s;.  yypParser->
20fbf 79 79 69 64 78 2b 2b 3b 0a 23 69 66 20 59 59 53  yyidx++;.#if YYS
20fc0 54 41 43 4b 44 45 50 54 48 3e 30 20 0a 20 20 69  TACKDEPTH>0 .  i
20fc1 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79  f( yypParser->yy
20fc2 69 64 78 3e 3d 59 59 53 54 41 43 4b 44 45 50 54  idx>=YYSTACKDEPT
20fc3 48 20 29 7b 0a 20 20 20 20 79 79 53 74 61 63 6b  H ){.    yyStack
20fc4 4f 76 65 72 66 6c 6f 77 28 79 79 70 50 61 72 73  Overflow(yypPars
20fc5 65 72 2c 20 79 79 70 4d 69 6e 6f 72 29 3b 0a 20  er, yypMinor);. 
20fc6 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 23     return;.  }.#
20fc7 65 6c 73 65 0a 20 20 69 66 28 20 79 79 70 50 61  else.  if( yypPa
20fc8 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d 79 79 70  rser->yyidx>=yyp
20fc9 50 61 72 73 65 72 2d 3e 79 79 73 74 6b 73 7a 20  Parser->yystksz 
20fca 29 7b 0a 20 20 20 20 79 79 47 72 6f 77 53 74 61  ){.    yyGrowSta
20fcb 63 6b 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20  ck(yypParser);. 
20fcc 20 20 20 69 66 28 20 79 79 70 50 61 72 73 65 72     if( yypParser
20fcd 2d 3e 79 79 69 64 78 3e 3d 79 79 70 50 61 72 73  ->yyidx>=yypPars
20fce 65 72 2d 3e 79 79 73 74 6b 73 7a 20 29 7b 0a 20  er->yystksz ){. 
20fcf 20 20 20 20 20 79 79 53 74 61 63 6b 4f 76 65 72       yyStackOver
20fd0 66 6c 6f 77 28 79 79 70 50 61 72 73 65 72 2c 20  flow(yypParser, 
20fd1 79 79 70 4d 69 6e 6f 72 29 3b 0a 20 20 20 20 20  yypMinor);.     
20fd2 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
20fd3 20 7d 0a 23 65 6e 64 69 66 0a 20 20 79 79 74 6f   }.#endif.  yyto
20fd4 73 20 3d 20 26 79 79 70 50 61 72 73 65 72 2d 3e  s = &yypParser->
20fd5 79 79 73 74 61 63 6b 5b 79 79 70 50 61 72 73 65  yystack[yypParse
20fd6 72 2d 3e 79 79 69 64 78 5d 3b 0a 20 20 79 79 74  r->yyidx];.  yyt
20fd7 6f 73 2d 3e 73 74 61 74 65 6e 6f 20 3d 20 79 79  os->stateno = yy
20fd8 4e 65 77 53 74 61 74 65 3b 0a 20 20 79 79 74 6f  NewState;.  yyto
20fd9 73 2d 3e 6d 61 6a 6f 72 20 3d 20 79 79 4d 61 6a  s->major = yyMaj
20fda 6f 72 3b 0a 20 20 79 79 74 6f 73 2d 3e 6d 69 6e  or;.  yytos->min
20fdb 6f 72 20 3d 20 2a 79 79 70 4d 69 6e 6f 72 3b 0a  or = *yypMinor;.
20fdc 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
20fdd 20 69 66 28 20 79 79 54 72 61 63 65 46 49 4c 45   if( yyTraceFILE
20fde 20 26 26 20 79 79 70 50 61 72 73 65 72 2d 3e 79   && yypParser->y
20fdf 79 69 64 78 3e 30 20 29 7b 0a 20 20 20 20 69 6e  yidx>0 ){.    in
20fe0 74 20 69 3b 0a 20 20 20 20 66 70 72 69 6e 74 66  t i;.    fprintf
20fe1 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22 25 73  (yyTraceFILE,"%s
20fe2 53 68 69 66 74 20 25 64 5c 6e 22 2c 79 79 54 72  Shift %d\n",yyTr
20fe3 61 63 65 50 72 6f 6d 70 74 2c 79 79 4e 65 77 53  acePrompt,yyNewS
20fe4 74 61 74 65 29 3b 0a 20 20 20 20 66 70 72 69 6e  tate);.    fprin
20fe5 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22  tf(yyTraceFILE,"
20fe6 25 73 53 74 61 63 6b 3a 22 2c 79 79 54 72 61 63  %sStack:",yyTrac
20fe7 65 50 72 6f 6d 70 74 29 3b 0a 20 20 20 20 66 6f  ePrompt);.    fo
20fe8 72 28 69 3d 31 3b 20 69 3c 3d 79 79 70 50 61 72  r(i=1; i<=yypPar
20fe9 73 65 72 2d 3e 79 79 69 64 78 3b 20 69 2b 2b 29  ser->yyidx; i++)
20fea 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 79  .      fprintf(y
20feb 79 54 72 61 63 65 46 49 4c 45 2c 22 20 25 73 22  yTraceFILE," %s"
20fec 2c 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 79 79 70  ,yyTokenName[yyp
20fed 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b  Parser->yystack[
20fee 69 5d 2e 6d 61 6a 6f 72 5d 29 3b 0a 20 20 20 20  i].major]);.    
20fef 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46  fprintf(yyTraceF
20ff0 49 4c 45 2c 22 5c 6e 22 29 3b 0a 20 20 7d 0a 23  ILE,"\n");.  }.#
20ff1 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 20 54 68 65 20  endif.}../* The 
20ff2 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20  following table 
20ff3 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61  contains informa
20ff4 74 69 6f 6e 20 61 62 6f 75 74 20 65 76 65 72 79  tion about every
20ff5 20 72 75 6c 65 20 74 68 61 74 0a 2a 2a 20 69 73   rule that.** is
20ff6 20 75 73 65 64 20 64 75 72 69 6e 67 20 74 68 65   used during the
20ff7 20 72 65 64 75 63 65 2e 0a 2a 2f 0a 73 74 61 74   reduce..*/.stat
20ff8 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20  ic const struct 
20ff9 7b 0a 20 20 59 59 43 4f 44 45 54 59 50 45 20 6c  {.  YYCODETYPE l
20ffa 68 73 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53  hs;         /* S
20ffb 79 6d 62 6f 6c 20 6f 6e 20 74 68 65 20 6c 65 66  ymbol on the lef
20ffc 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74  t-hand side of t
20ffd 68 65 20 72 75 6c 65 20 2a 2f 0a 20 20 75 6e 73  he rule */.  uns
20ffe 69 67 6e 65 64 20 63 68 61 72 20 6e 72 68 73 3b  igned char nrhs;
20fff 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
21000 66 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64  f right-hand sid
21001 65 20 73 79 6d 62 6f 6c 73 20 69 6e 20 74 68 65  e symbols in the
21002 20 72 75 6c 65 20 2a 2f 0a 7d 20 79 79 52 75 6c   rule */.} yyRul
21003 65 49 6e 66 6f 5b 5d 20 3d 20 7b 0a 20 20 7b 20  eInfo[] = {.  { 
21004 31 33 39 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34  139, 1 },.  { 14
21005 30 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 30 2c  0, 2 },.  { 140,
21006 20 31 20 7d 2c 0a 20 20 7b 20 31 34 32 2c 20 31   1 },.  { 142, 1
21007 20 7d 2c 0a 20 20 7b 20 31 34 31 2c 20 31 20 7d   },.  { 141, 1 }
21008 2c 0a 20 20 7b 20 31 34 31 2c 20 33 20 7d 2c 0a  ,.  { 141, 3 },.
21009 20 20 7b 20 31 34 34 2c 20 30 20 7d 2c 0a 20 20    { 144, 0 },.  
2100a 7b 20 31 34 34 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 144, 1 },.  { 
2100b 31 34 34 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34  144, 3 },.  { 14
2100c 33 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34 36 2c  3, 3 },.  { 146,
2100d 20 30 20 7d 2c 0a 20 20 7b 20 31 34 36 2c 20 31   0 },.  { 146, 1
2100e 20 7d 2c 0a 20 20 7b 20 31 34 36 2c 20 32 20 7d   },.  { 146, 2 }
2100f 2c 0a 20 20 7b 20 31 34 35 2c 20 30 20 7d 2c 0a  ,.  { 145, 0 },.
21010 20 20 7b 20 31 34 35 2c 20 31 20 7d 2c 0a 20 20    { 145, 1 },.  
21011 7b 20 31 34 35 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 145, 1 },.  { 
21012 31 34 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34  145, 1 },.  { 14
21013 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 33 2c  3, 2 },.  { 143,
21014 20 32 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 32   2 },.  { 143, 2
21015 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 32 20 7d   },.  { 143, 2 }
21016 2c 0a 20 20 7b 20 31 34 38 2c 20 36 20 7d 2c 0a  ,.  { 148, 6 },.
21017 20 20 7b 20 31 35 31 2c 20 30 20 7d 2c 0a 20 20    { 151, 0 },.  
21018 7b 20 31 35 31 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 151, 3 },.  { 
21019 31 35 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 35  150, 1 },.  { 15
2101a 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 34 39 2c  0, 0 },.  { 149,
2101b 20 34 20 7d 2c 0a 20 20 7b 20 31 34 39 2c 20 32   4 },.  { 149, 2
2101c 20 7d 2c 0a 20 20 7b 20 31 35 33 2c 20 33 20 7d   },.  { 153, 3 }
2101d 2c 0a 20 20 7b 20 31 35 33 2c 20 31 20 7d 2c 0a  ,.  { 153, 1 },.
2101e 20 20 7b 20 31 35 36 2c 20 33 20 7d 2c 0a 20 20    { 156, 3 },.  
2101f 7b 20 31 35 37 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 157, 1 },.  { 
21020 31 36 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36  160, 1 },.  { 16
21021 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  1, 1 },.  { 147,
21022 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 31   1 },.  { 147, 1
21023 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 31 20 7d   },.  { 147, 1 }
21024 2c 0a 20 20 7b 20 31 35 38 2c 20 30 20 7d 2c 0a  ,.  { 158, 0 },.
21025 20 20 7b 20 31 35 38 2c 20 31 20 7d 2c 0a 20 20    { 158, 1 },.  
21026 7b 20 31 36 32 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 162, 1 },.  { 
21027 31 36 32 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 36  162, 4 },.  { 16
21028 32 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 36 33 2c  2, 6 },.  { 163,
21029 20 31 20 7d 2c 0a 20 20 7b 20 31 36 33 2c 20 32   1 },.  { 163, 2
2102a 20 7d 2c 0a 20 20 7b 20 31 36 34 2c 20 31 20 7d   },.  { 164, 1 }
2102b 2c 0a 20 20 7b 20 31 36 34 2c 20 31 20 7d 2c 0a  ,.  { 164, 1 },.
2102c 20 20 7b 20 31 35 39 2c 20 32 20 7d 2c 0a 20 20    { 159, 2 },.  
2102d 7b 20 31 35 39 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 159, 0 },.  { 
2102e 31 36 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 36  167, 3 },.  { 16
2102f 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36 38 2c  7, 1 },.  { 168,
21030 20 32 20 7d 2c 0a 20 20 7b 20 31 36 38 2c 20 34   2 },.  { 168, 4
21031 20 7d 2c 0a 20 20 7b 20 31 36 38 2c 20 33 20 7d   },.  { 168, 3 }
21032 2c 0a 20 20 7b 20 31 36 38 2c 20 33 20 7d 2c 0a  ,.  { 168, 3 },.
21033 20 20 7b 20 31 36 38 2c 20 32 20 7d 2c 0a 20 20    { 168, 2 },.  
21034 7b 20 31 36 38 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 168, 2 },.  { 
21035 31 36 38 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 36  168, 3 },.  { 16
21036 38 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 36 38 2c  8, 5 },.  { 168,
21037 20 32 20 7d 2c 0a 20 20 7b 20 31 36 38 2c 20 34   2 },.  { 168, 4
21038 20 7d 2c 0a 20 20 7b 20 31 36 38 2c 20 34 20 7d   },.  { 168, 4 }
21039 2c 0a 20 20 7b 20 31 36 38 2c 20 31 20 7d 2c 0a  ,.  { 168, 1 },.
2103a 20 20 7b 20 31 36 38 2c 20 32 20 7d 2c 0a 20 20    { 168, 2 },.  
2103b 7b 20 31 37 33 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 173, 0 },.  { 
2103c 31 37 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37  173, 1 },.  { 17
2103d 35 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 37 35 2c  5, 0 },.  { 175,
2103e 20 32 20 7d 2c 0a 20 20 7b 20 31 37 37 2c 20 32   2 },.  { 177, 2
2103f 20 7d 2c 0a 20 20 7b 20 31 37 37 2c 20 33 20 7d   },.  { 177, 3 }
21040 2c 0a 20 20 7b 20 31 37 37 2c 20 33 20 7d 2c 0a  ,.  { 177, 3 },.
21041 20 20 7b 20 31 37 37 2c 20 33 20 7d 2c 0a 20 20    { 177, 3 },.  
21042 7b 20 31 37 38 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 178, 2 },.  { 
21043 31 37 38 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37  178, 2 },.  { 17
21044 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 38 2c  8, 1 },.  { 178,
21045 20 31 20 7d 2c 0a 20 20 7b 20 31 37 36 2c 20 33   1 },.  { 176, 3
21046 20 7d 2c 0a 20 20 7b 20 31 37 36 2c 20 32 20 7d   },.  { 176, 2 }
21047 2c 0a 20 20 7b 20 31 37 39 2c 20 30 20 7d 2c 0a  ,.  { 179, 0 },.
21048 20 20 7b 20 31 37 39 2c 20 32 20 7d 2c 0a 20 20    { 179, 2 },.  
21049 7b 20 31 37 39 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 179, 2 },.  { 
2104a 31 35 34 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 35  154, 0 },.  { 15
2104b 34 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38 30 2c  4, 2 },.  { 180,
2104c 20 33 20 7d 2c 0a 20 20 7b 20 31 38 30 2c 20 32   3 },.  { 180, 2
2104d 20 7d 2c 0a 20 20 7b 20 31 38 30 2c 20 31 20 7d   },.  { 180, 1 }
2104e 2c 0a 20 20 7b 20 31 38 31 2c 20 32 20 7d 2c 0a  ,.  { 181, 2 },.
2104f 20 20 7b 20 31 38 31 2c 20 37 20 7d 2c 0a 20 20    { 181, 7 },.  
21050 7b 20 31 38 31 2c 20 35 20 7d 2c 0a 20 20 7b 20  { 181, 5 },.  { 
21051 31 38 31 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 38  181, 5 },.  { 18
21052 31 2c 20 31 30 20 7d 2c 0a 20 20 7b 20 31 38 33  1, 10 },.  { 183
21053 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 38 33 2c 20  , 0 },.  { 183, 
21054 31 20 7d 2c 0a 20 20 7b 20 31 37 31 2c 20 30 20  1 },.  { 171, 0 
21055 7d 2c 0a 20 20 7b 20 31 37 31 2c 20 33 20 7d 2c  },.  { 171, 3 },
21056 0a 20 20 7b 20 31 38 34 2c 20 30 20 7d 2c 0a 20  .  { 184, 0 },. 
21057 20 7b 20 31 38 34 2c 20 32 20 7d 2c 0a 20 20 7b   { 184, 2 },.  {
21058 20 31 38 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   185, 1 },.  { 1
21059 38 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 38 35  85, 1 },.  { 185
2105a 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20  , 1 },.  { 143, 
2105b 34 20 7d 2c 0a 20 20 7b 20 31 38 37 2c 20 32 20  4 },.  { 187, 2 
2105c 7d 2c 0a 20 20 7b 20 31 38 37 2c 20 30 20 7d 2c  },.  { 187, 0 },
2105d 0a 20 20 7b 20 31 34 33 2c 20 38 20 7d 2c 0a 20  .  { 143, 8 },. 
2105e 20 7b 20 31 34 33 2c 20 34 20 7d 2c 0a 20 20 7b   { 143, 4 },.  {
2105f 20 31 34 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   143, 1 },.  { 1
21060 35 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 35 35  55, 1 },.  { 155
21061 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 39 30 2c 20  , 3 },.  { 190, 
21062 31 20 7d 2c 0a 20 20 7b 20 31 39 30 2c 20 32 20  1 },.  { 190, 2 
21063 7d 2c 0a 20 20 7b 20 31 39 30 2c 20 31 20 7d 2c  },.  { 190, 1 },
21064 0a 20 20 7b 20 31 38 39 2c 20 39 20 7d 2c 0a 20  .  { 189, 9 },. 
21065 20 7b 20 31 39 31 2c 20 31 20 7d 2c 0a 20 20 7b   { 191, 1 },.  {
21066 20 31 39 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   191, 1 },.  { 1
21067 39 31 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39 39  91, 0 },.  { 199
21068 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 39 39 2c 20  , 2 },.  { 199, 
21069 30 20 7d 2c 0a 20 20 7b 20 31 39 32 2c 20 33 20  0 },.  { 192, 3 
2106a 7d 2c 0a 20 20 7b 20 31 39 32 2c 20 32 20 7d 2c  },.  { 192, 2 },
2106b 0a 20 20 7b 20 31 39 32 2c 20 34 20 7d 2c 0a 20  .  { 192, 4 },. 
2106c 20 7b 20 32 30 30 2c 20 32 20 7d 2c 0a 20 20 7b   { 200, 2 },.  {
2106d 20 32 30 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   200, 1 },.  { 2
2106e 30 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39 33  00, 0 },.  { 193
2106f 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39 33 2c 20  , 0 },.  { 193, 
21070 32 20 7d 2c 0a 20 20 7b 20 32 30 32 2c 20 32 20  2 },.  { 202, 2 
21071 7d 2c 0a 20 20 7b 20 32 30 32 2c 20 30 20 7d 2c  },.  { 202, 0 },
21072 0a 20 20 7b 20 32 30 31 2c 20 36 20 7d 2c 0a 20  .  { 201, 6 },. 
21073 20 7b 20 32 30 31 2c 20 37 20 7d 2c 0a 20 20 7b   { 201, 7 },.  {
21074 20 32 30 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   206, 1 },.  { 2
21075 30 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 35 32  06, 1 },.  { 152
21076 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 35 32 2c 20  , 0 },.  { 152, 
21077 32 20 7d 2c 0a 20 20 7b 20 31 38 38 2c 20 32 20  2 },.  { 188, 2 
21078 7d 2c 0a 20 20 7b 20 32 30 33 2c 20 31 20 7d 2c  },.  { 203, 1 },
21079 0a 20 20 7b 20 32 30 33 2c 20 32 20 7d 2c 0a 20  .  { 203, 2 },. 
2107a 20 7b 20 32 30 33 2c 20 33 20 7d 2c 0a 20 20 7b   { 203, 3 },.  {
2107b 20 32 30 33 2c 20 34 20 7d 2c 0a 20 20 7b 20 32   203, 4 },.  { 2
2107c 30 34 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30 34  04, 2 },.  { 204
2107d 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 35 2c 20  , 0 },.  { 205, 
2107e 34 20 7d 2c 0a 20 20 7b 20 32 30 35 2c 20 30 20  4 },.  { 205, 0 
2107f 7d 2c 0a 20 20 7b 20 31 39 37 2c 20 30 20 7d 2c  },.  { 197, 0 },
21080 0a 20 20 7b 20 31 39 37 2c 20 33 20 7d 2c 0a 20  .  { 197, 3 },. 
21081 20 7b 20 32 30 39 2c 20 34 20 7d 2c 0a 20 20 7b   { 209, 4 },.  {
21082 20 32 30 39 2c 20 32 20 7d 2c 0a 20 20 7b 20 32   209, 2 },.  { 2
21083 31 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 32  10, 1 },.  { 172
21084 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 32 2c 20  , 1 },.  { 172, 
21085 31 20 7d 2c 0a 20 20 7b 20 31 37 32 2c 20 30 20  1 },.  { 172, 0 
21086 7d 2c 0a 20 20 7b 20 31 39 35 2c 20 30 20 7d 2c  },.  { 195, 0 },
21087 0a 20 20 7b 20 31 39 35 2c 20 33 20 7d 2c 0a 20  .  { 195, 3 },. 
21088 20 7b 20 31 39 36 2c 20 30 20 7d 2c 0a 20 20 7b   { 196, 0 },.  {
21089 20 31 39 36 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   196, 2 },.  { 1
2108a 39 38 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39 38  98, 0 },.  { 198
2108b 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 39 38 2c 20  , 2 },.  { 198, 
2108c 34 20 7d 2c 0a 20 20 7b 20 31 39 38 2c 20 34 20  4 },.  { 198, 4 
2108d 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 34 20 7d 2c  },.  { 143, 4 },
2108e 0a 20 20 7b 20 31 39 34 2c 20 30 20 7d 2c 0a 20  .  { 194, 0 },. 
2108f 20 7b 20 31 39 34 2c 20 32 20 7d 2c 0a 20 20 7b   { 194, 2 },.  {
21090 20 31 34 33 2c 20 36 20 7d 2c 0a 20 20 7b 20 32   143, 6 },.  { 2
21091 31 32 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 31 32  12, 5 },.  { 212
21092 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20  , 3 },.  { 143, 
21093 38 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 35 20  8 },.  { 143, 5 
21094 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 36 20 7d 2c  },.  { 143, 6 },
21095 0a 20 20 7b 20 32 31 33 2c 20 32 20 7d 2c 0a 20  .  { 213, 2 },. 
21096 20 7b 20 32 31 33 2c 20 31 20 7d 2c 0a 20 20 7b   { 213, 1 },.  {
21097 20 32 31 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 32   215, 3 },.  { 2
21098 31 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 31 34  15, 1 },.  { 214
21099 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 31 34 2c 20  , 0 },.  { 214, 
2109a 33 20 7d 2c 0a 20 20 7b 20 32 30 38 2c 20 33 20  3 },.  { 208, 3 
2109b 7d 2c 0a 20 20 7b 20 32 30 38 2c 20 31 20 7d 2c  },.  { 208, 1 },
2109c 0a 20 20 7b 20 31 37 30 2c 20 31 20 7d 2c 0a 20  .  { 170, 1 },. 
2109d 20 7b 20 31 37 30 2c 20 33 20 7d 2c 0a 20 20 7b   { 170, 3 },.  {
2109e 20 31 36 39 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   169, 1 },.  { 1
2109f 37 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 30  70, 1 },.  { 170
210a0 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20  , 1 },.  { 170, 
210a1 33 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20 35 20  3 },.  { 170, 5 
210a2 7d 2c 0a 20 20 7b 20 31 36 39 2c 20 31 20 7d 2c  },.  { 169, 1 },
210a3 0a 20 20 7b 20 31 36 39 2c 20 31 20 7d 2c 0a 20  .  { 169, 1 },. 
210a4 20 7b 20 31 37 30 2c 20 31 20 7d 2c 0a 20 20 7b   { 170, 1 },.  {
210a5 20 31 37 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   170, 1 },.  { 1
210a6 37 30 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 30  70, 3 },.  { 170
210a7 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20  , 6 },.  { 170, 
210a8 35 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20 34 20  5 },.  { 170, 4 
210a9 7d 2c 0a 20 20 7b 20 31 36 39 2c 20 31 20 7d 2c  },.  { 169, 1 },
210aa 0a 20 20 7b 20 31 37 30 2c 20 33 20 7d 2c 0a 20  .  { 170, 3 },. 
210ab 20 7b 20 31 37 30 2c 20 33 20 7d 2c 0a 20 20 7b   { 170, 3 },.  {
210ac 20 31 37 30 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   170, 3 },.  { 1
210ad 37 30 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 30  70, 3 },.  { 170
210ae 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20  , 3 },.  { 170, 
210af 33 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20 33 20  3 },.  { 170, 3 
210b0 7d 2c 0a 20 20 7b 20 31 37 30 2c 20 33 20 7d 2c  },.  { 170, 3 },
210b1 0a 20 20 7b 20 32 31 37 2c 20 31 20 7d 2c 0a 20  .  { 217, 1 },. 
210b2 20 7b 20 32 31 37 2c 20 32 20 7d 2c 0a 20 20 7b   { 217, 2 },.  {
210b3 20 32 31 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   217, 1 },.  { 2
210b4 31 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 31 38  17, 2 },.  { 218
210b5 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 31 38 2c 20  , 2 },.  { 218, 
210b6 30 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20 34 20  0 },.  { 170, 4 
210b7 7d 2c 0a 20 20 7b 20 31 37 30 2c 20 32 20 7d 2c  },.  { 170, 2 },
210b8 0a 20 20 7b 20 31 37 30 2c 20 33 20 7d 2c 0a 20  .  { 170, 3 },. 
210b9 20 7b 20 31 37 30 2c 20 33 20 7d 2c 0a 20 20 7b   { 170, 3 },.  {
210ba 20 31 37 30 2c 20 34 20 7d 2c 0a 20 20 7b 20 31   170, 4 },.  { 1
210bb 37 30 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 30  70, 2 },.  { 170
210bc 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20  , 2 },.  { 170, 
210bd 32 20 7d 2c 0a 20 20 7b 20 32 31 39 2c 20 31 20  2 },.  { 219, 1 
210be 7d 2c 0a 20 20 7b 20 32 31 39 2c 20 32 20 7d 2c  },.  { 219, 2 },
210bf 0a 20 20 7b 20 31 37 30 2c 20 35 20 7d 2c 0a 20  .  { 170, 5 },. 
210c0 20 7b 20 32 32 30 2c 20 31 20 7d 2c 0a 20 20 7b   { 220, 1 },.  {
210c1 20 32 32 30 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   220, 2 },.  { 1
210c2 37 30 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 37 30  70, 5 },.  { 170
210c3 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20  , 3 },.  { 170, 
210c4 35 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20 34 20  5 },.  { 170, 4 
210c5 7d 2c 0a 20 20 7b 20 31 37 30 2c 20 34 20 7d 2c  },.  { 170, 4 },
210c6 0a 20 20 7b 20 31 37 30 2c 20 35 20 7d 2c 0a 20  .  { 170, 5 },. 
210c7 20 7b 20 32 32 32 2c 20 35 20 7d 2c 0a 20 20 7b   { 222, 5 },.  {
210c8 20 32 32 32 2c 20 34 20 7d 2c 0a 20 20 7b 20 32   222, 4 },.  { 2
210c9 32 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 32 33  23, 2 },.  { 223
210ca 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 32 31 2c 20  , 0 },.  { 221, 
210cb 31 20 7d 2c 0a 20 20 7b 20 32 32 31 2c 20 30 20  1 },.  { 221, 0 
210cc 7d 2c 0a 20 20 7b 20 32 31 36 2c 20 31 20 7d 2c  },.  { 216, 1 },
210cd 0a 20 20 7b 20 32 31 36 2c 20 30 20 7d 2c 0a 20  .  { 216, 0 },. 
210ce 20 7b 20 32 31 31 2c 20 33 20 7d 2c 0a 20 20 7b   { 211, 3 },.  {
210cf 20 32 31 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   211, 1 },.  { 1
210d0 34 33 2c 20 31 31 20 7d 2c 0a 20 20 7b 20 32 32  43, 11 },.  { 22
210d1 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32 34 2c  4, 1 },.  { 224,
210d2 20 30 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20 30   0 },.  { 174, 0
210d3 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20 33 20 7d   },.  { 174, 3 }
210d4 2c 0a 20 20 7b 20 31 38 32 2c 20 35 20 7d 2c 0a  ,.  { 182, 5 },.
210d5 20 20 7b 20 31 38 32 2c 20 33 20 7d 2c 0a 20 20    { 182, 3 },.  
210d6 7b 20 32 32 35 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 225, 1 },.  { 
210d7 32 32 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 32  226, 0 },.  { 22
210d8 36 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 33 2c  6, 2 },.  { 143,
210d9 20 34 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 31   4 },.  { 143, 1
210da 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 32 20 7d   },.  { 143, 2 }
210db 2c 0a 20 20 7b 20 31 34 33 2c 20 35 20 7d 2c 0a  ,.  { 143, 5 },.
210dc 20 20 7b 20 31 34 33 2c 20 35 20 7d 2c 0a 20 20    { 143, 5 },.  
210dd 7b 20 31 34 33 2c 20 35 20 7d 2c 0a 20 20 7b 20  { 143, 5 },.  { 
210de 31 34 33 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 34  143, 6 },.  { 14
210df 33 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 32 37 2c  3, 3 },.  { 227,
210e0 20 31 20 7d 2c 0a 20 20 7b 20 32 32 37 2c 20 31   1 },.  { 227, 1
210e1 20 7d 2c 0a 20 20 7b 20 31 36 35 2c 20 32 20 7d   },.  { 165, 2 }
210e2 2c 0a 20 20 7b 20 31 36 36 2c 20 32 20 7d 2c 0a  ,.  { 166, 2 },.
210e3 20 20 7b 20 32 32 39 2c 20 31 20 7d 2c 0a 20 20    { 229, 1 },.  
210e4 7b 20 32 32 38 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 228, 1 },.  { 
210e5 32 32 38 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 34  228, 0 },.  { 14
210e6 33 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 33 30 2c  3, 5 },.  { 230,
210e7 20 31 31 20 7d 2c 0a 20 20 7b 20 32 33 32 2c 20   11 },.  { 232, 
210e8 31 20 7d 2c 0a 20 20 7b 20 32 33 32 2c 20 31 20  1 },.  { 232, 1 
210e9 7d 2c 0a 20 20 7b 20 32 33 32 2c 20 32 20 7d 2c  },.  { 232, 2 },
210ea 0a 20 20 7b 20 32 33 32 2c 20 30 20 7d 2c 0a 20  .  { 232, 0 },. 
210eb 20 7b 20 32 33 33 2c 20 31 20 7d 2c 0a 20 20 7b   { 233, 1 },.  {
210ec 20 32 33 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   233, 1 },.  { 2
210ed 33 33 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 33 34  33, 3 },.  { 234
210ee 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 33 34 2c 20  , 0 },.  { 234, 
210ef 33 20 7d 2c 0a 20 20 7b 20 32 33 35 2c 20 30 20  3 },.  { 235, 0 
210f0 7d 2c 0a 20 20 7b 20 32 33 35 2c 20 32 20 7d 2c  },.  { 235, 2 },
210f1 0a 20 20 7b 20 32 33 31 2c 20 33 20 7d 2c 0a 20  .  { 231, 3 },. 
210f2 20 7b 20 32 33 31 2c 20 30 20 7d 2c 0a 20 20 7b   { 231, 0 },.  {
210f3 20 32 33 36 2c 20 36 20 7d 2c 0a 20 20 7b 20 32   236, 6 },.  { 2
210f4 33 36 2c 20 38 20 7d 2c 0a 20 20 7b 20 32 33 36  36, 8 },.  { 236
210f5 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 33 36 2c 20  , 5 },.  { 236, 
210f6 34 20 7d 2c 0a 20 20 7b 20 32 33 36 2c 20 31 20  4 },.  { 236, 1 
210f7 7d 2c 0a 20 20 7b 20 31 37 30 2c 20 34 20 7d 2c  },.  { 170, 4 },
210f8 0a 20 20 7b 20 31 37 30 2c 20 36 20 7d 2c 0a 20  .  { 170, 6 },. 
210f9 20 7b 20 31 38 36 2c 20 31 20 7d 2c 0a 20 20 7b   { 186, 1 },.  {
210fa 20 31 38 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   186, 1 },.  { 1
210fb 38 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 33  86, 1 },.  { 143
210fc 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20  , 4 },.  { 143, 
210fd 36 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 33 20  6 },.  { 143, 3 
210fe 7d 2c 0a 20 20 7b 20 32 33 38 2c 20 30 20 7d 2c  },.  { 238, 0 },
210ff 0a 20 20 7b 20 32 33 38 2c 20 32 20 7d 2c 0a 20  .  { 238, 2 },. 
21100 20 7b 20 32 33 37 2c 20 31 20 7d 2c 0a 20 20 7b   { 237, 1 },.  {
21101 20 32 33 37 2c 20 30 20 7d 2c 0a 20 20 7b 20 31   237, 0 },.  { 1
21102 34 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 33  43, 1 },.  { 143
21103 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20  , 3 },.  { 143, 
21104 31 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 33 20  1 },.  { 143, 3 
21105 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 36 20 7d 2c  },.  { 143, 6 },
21106 0a 20 20 7b 20 31 34 33 2c 20 36 20 7d 2c 0a 20  .  { 143, 6 },. 
21107 20 7b 20 32 33 39 2c 20 31 20 7d 2c 0a 20 20 7b   { 239, 1 },.  {
21108 20 32 34 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 32   240, 0 },.  { 2
21109 34 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 33  40, 1 },.  { 143
2110a 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20  , 1 },.  { 143, 
2110b 34 20 7d 2c 0a 20 20 7b 20 32 34 31 2c 20 37 20  4 },.  { 241, 7 
2110c 7d 2c 0a 20 20 7b 20 32 34 32 2c 20 31 20 7d 2c  },.  { 242, 1 },
2110d 0a 20 20 7b 20 32 34 32 2c 20 33 20 7d 2c 0a 20  .  { 242, 3 },. 
2110e 20 7b 20 32 34 33 2c 20 30 20 7d 2c 0a 20 20 7b   { 243, 0 },.  {
2110f 20 32 34 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 32   243, 2 },.  { 2
21110 34 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 34 34  44, 1 },.  { 244
21111 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 34 35 2c 20  , 3 },.  { 245, 
21112 31 20 7d 2c 0a 20 20 7b 20 32 34 36 2c 20 30 20  1 },.  { 246, 0 
21113 7d 2c 0a 20 20 7b 20 32 34 36 2c 20 32 20 7d 2c  },.  { 246, 2 },
21114 0a 7d 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  .};..static void
21115 20 79 79 5f 61 63 63 65 70 74 28 79 79 50 61 72   yy_accept(yyPar
21116 73 65 72 2a 29 3b 20 20 2f 2a 20 46 6f 72 77 61  ser*);  /* Forwa
21117 72 64 20 44 65 63 6c 61 72 61 74 69 6f 6e 20 2a  rd Declaration *
21118 2f 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d  /../*.** Perform
21119 20 61 20 72 65 64 75 63 65 20 61 63 74 69 6f 6e   a reduce action
2111a 20 61 6e 64 20 74 68 65 20 73 68 69 66 74 20 74   and the shift t
2111b 68 61 74 20 6d 75 73 74 20 69 6d 6d 65 64 69 61  hat must immedia
2111c 74 65 6c 79 0a 2a 2a 20 66 6f 6c 6c 6f 77 20 74  tely.** follow t
2111d 68 65 20 72 65 64 75 63 65 2e 0a 2a 2f 0a 73 74  he reduce..*/.st
2111e 61 74 69 63 20 76 6f 69 64 20 79 79 5f 72 65 64  atic void yy_red
2111f 75 63 65 28 0a 20 20 79 79 50 61 72 73 65 72 20  uce(.  yyParser 
21120 2a 79 79 70 50 61 72 73 65 72 2c 20 20 20 20 20  *yypParser,     
21121 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65      /* The parse
21122 72 20 2a 2f 0a 20 20 69 6e 74 20 79 79 72 75 6c  r */.  int yyrul
21123 65 6e 6f 20 20 20 20 20 20 20 20 20 20 20 20 20  eno             
21124 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
21125 20 74 68 65 20 72 75 6c 65 20 62 79 20 77 68 69   the rule by whi
21126 63 68 20 74 6f 20 72 65 64 75 63 65 20 2a 2f 0a  ch to reduce */.
21127 29 7b 0a 20 20 69 6e 74 20 79 79 67 6f 74 6f 3b  ){.  int yygoto;
21128 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21129 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74       /* The next
2112a 20 73 74 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20   state */.  int 
2112b 79 79 61 63 74 3b 20 20 20 20 20 20 20 20 20 20  yyact;          
2112c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2112d 68 65 20 6e 65 78 74 20 61 63 74 69 6f 6e 20 2a  he next action *
2112e 2f 0a 20 20 59 59 4d 49 4e 4f 52 54 59 50 45 20  /.  YYMINORTYPE 
2112f 79 79 67 6f 74 6f 6d 69 6e 6f 72 3b 20 20 20 20  yygotominor;    
21130 20 20 20 20 2f 2a 20 54 68 65 20 4c 48 53 20 6f      /* The LHS o
21131 66 20 74 68 65 20 72 75 6c 65 20 72 65 64 75 63  f the rule reduc
21132 65 64 20 2a 2f 0a 20 20 79 79 53 74 61 63 6b 45  ed */.  yyStackE
21133 6e 74 72 79 20 2a 79 79 6d 73 70 3b 20 20 20 20  ntry *yymsp;    
21134 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
21135 6f 70 20 6f 66 20 74 68 65 20 70 61 72 73 65 72  op of the parser
21136 27 73 20 73 74 61 63 6b 20 2a 2f 0a 20 20 69 6e  's stack */.  in
21137 74 20 79 79 73 69 7a 65 3b 20 20 20 20 20 20 20  t yysize;       
21138 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
21139 20 41 6d 6f 75 6e 74 20 74 6f 20 70 6f 70 20 74   Amount to pop t
2113a 68 65 20 73 74 61 63 6b 20 2a 2f 0a 20 20 73 71  he stack */.  sq
2113b 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 46  lite3ParserARG_F
2113c 45 54 43 48 3b 0a 20 20 79 79 6d 73 70 20 3d 20  ETCH;.  yymsp = 
2113d 26 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74  &yypParser->yyst
2113e 61 63 6b 5b 79 79 70 50 61 72 73 65 72 2d 3e 79  ack[yypParser->y
2113f 79 69 64 78 5d 3b 0a 23 69 66 6e 64 65 66 20 4e  yidx];.#ifndef N
21140 44 45 42 55 47 0a 20 20 69 66 28 20 79 79 54 72  DEBUG.  if( yyTr
21141 61 63 65 46 49 4c 45 20 26 26 20 79 79 72 75 6c  aceFILE && yyrul
21142 65 6e 6f 3e 3d 30 20 0a 20 20 20 20 20 20 20 20  eno>=0 .        
21143 26 26 20 79 79 72 75 6c 65 6e 6f 3c 28 69 6e 74  && yyruleno<(int
21144 29 28 73 69 7a 65 6f 66 28 79 79 52 75 6c 65 4e  )(sizeof(yyRuleN
21145 61 6d 65 29 2f 73 69 7a 65 6f 66 28 79 79 52 75  ame)/sizeof(yyRu
21146 6c 65 4e 61 6d 65 5b 30 5d 29 29 20 29 7b 0a 20  leName[0])) ){. 
21147 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61     fprintf(yyTra
21148 63 65 46 49 4c 45 2c 20 22 25 73 52 65 64 75 63  ceFILE, "%sReduc
21149 65 20 5b 25 73 5d 2e 5c 6e 22 2c 20 79 79 54 72  e [%s].\n", yyTr
2114a 61 63 65 50 72 6f 6d 70 74 2c 0a 20 20 20 20 20  acePrompt,.     
2114b 20 79 79 52 75 6c 65 4e 61 6d 65 5b 79 79 72 75   yyRuleName[yyru
2114c 6c 65 6e 6f 5d 29 3b 0a 20 20 7d 0a 23 65 6e 64  leno]);.  }.#end
2114d 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a  if /* NDEBUG */.
2114e 0a 20 20 2f 2a 20 53 69 6c 65 6e 63 65 20 63 6f  .  /* Silence co
2114f 6d 70 6c 61 69 6e 74 73 20 66 72 6f 6d 20 70 75  mplaints from pu
21150 72 69 66 79 20 61 62 6f 75 74 20 79 79 67 6f 74  rify about yygot
21151 6f 6d 69 6e 6f 72 20 62 65 69 6e 67 20 75 6e 69  ominor being uni
21152 6e 69 74 69 61 6c 69 7a 65 64 0a 20 20 2a 2a 20  nitialized.  ** 
21153 69 6e 20 73 6f 6d 65 20 63 61 73 65 73 20 77 68  in some cases wh
21154 65 6e 20 69 74 20 69 73 20 63 6f 70 69 65 64 20  en it is copied 
21155 69 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 20 61  into the stack a
21156 66 74 65 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69  fter the followi
21157 6e 67 0a 20 20 2a 2a 20 73 77 69 74 63 68 2e 20  ng.  ** switch. 
21158 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 20 69 73 20   yygotominor is 
21159 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 77 68  uninitialized wh
2115a 65 6e 20 61 20 72 75 6c 65 20 72 65 64 75 63 65  en a rule reduce
2115b 73 20 74 68 61 74 20 64 6f 65 73 0a 20 20 2a 2a  s that does.  **
2115c 20 6e 6f 74 20 73 65 74 20 74 68 65 20 76 61 6c   not set the val
2115d 75 65 20 6f 66 20 69 74 73 20 6c 65 66 74 2d 68  ue of its left-h
2115e 61 6e 64 20 73 69 64 65 20 6e 6f 6e 74 65 72 6d  and side nonterm
2115f 69 6e 61 6c 2e 20 20 4c 65 61 76 69 6e 67 20 74  inal.  Leaving t
21160 68 65 0a 20 20 2a 2a 20 76 61 6c 75 65 20 6f 66  he.  ** value of
21161 20 74 68 65 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c   the nonterminal
21162 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 69   uninitialized i
21163 73 20 75 74 74 65 72 6c 79 20 68 61 72 6d 6c 65  s utterly harmle
21164 73 73 20 61 73 20 6c 6f 6e 67 0a 20 20 2a 2a 20  ss as long.  ** 
21165 61 73 20 74 68 65 20 76 61 6c 75 65 20 69 73 20  as the value is 
21166 6e 65 76 65 72 20 75 73 65 64 2e 20 20 53 6f 20  never used.  So 
21167 72 65 61 6c 6c 79 20 74 68 65 20 6f 6e 6c 79 20  really the only 
21168 74 68 69 6e 67 20 74 68 69 73 20 63 6f 64 65 0a  thing this code.
21169 20 20 2a 2a 20 61 63 63 6f 6d 70 6c 69 73 68 65    ** accomplishe
2116a 73 20 69 73 20 74 6f 20 71 75 69 65 74 65 6e 20  s is to quieten 
2116b 70 75 72 69 66 79 2e 20 20 0a 20 20 2a 2a 0a 20  purify.  .  **. 
2116c 20 2a 2a 20 32 30 30 37 2d 30 31 2d 31 36 3a 20   ** 2007-01-16: 
2116d 20 54 68 65 20 77 69 72 65 73 68 61 72 6b 20 70   The wireshark p
2116e 72 6f 6a 65 63 74 20 28 77 77 77 2e 77 69 72 65  roject (www.wire
2116f 73 68 61 72 6b 2e 6f 72 67 29 20 72 65 70 6f 72  shark.org) repor
21170 74 73 20 74 68 61 74 0a 20 20 2a 2a 20 77 69 74  ts that.  ** wit
21171 68 6f 75 74 20 74 68 69 73 20 63 6f 64 65 2c 20  hout this code, 
21172 74 68 65 69 72 20 70 61 72 73 65 72 20 73 65 67  their parser seg
21173 66 61 75 6c 74 73 2e 20 20 49 27 6d 20 6e 6f 74  faults.  I'm not
21174 20 73 75 72 65 20 77 68 61 74 20 74 68 65 72 65   sure what there
21175 0a 20 20 2a 2a 20 70 61 72 73 65 72 20 69 73 20  .  ** parser is 
21176 64 6f 69 6e 67 20 74 6f 20 6d 61 6b 65 20 74 68  doing to make th
21177 69 73 20 68 61 70 70 65 6e 2e 20 20 54 68 69 73  is happen.  This
21178 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 62   is the second b
21179 75 67 20 72 65 70 6f 72 74 0a 20 20 2a 2a 20 66  ug report.  ** f
2117a 72 6f 6d 20 77 69 72 65 73 68 61 72 6b 20 74 68  rom wireshark th
2117b 69 73 20 77 65 65 6b 2e 20 20 43 6c 65 61 72 6c  is week.  Clearl
2117c 79 20 74 68 65 79 20 61 72 65 20 73 74 72 65 73  y they are stres
2117d 73 69 6e 67 20 4c 65 6d 6f 6e 20 69 6e 20 77 61  sing Lemon in wa
2117e 79 73 0a 20 20 2a 2a 20 74 68 61 74 20 69 74 20  ys.  ** that it 
2117f 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 70 72 65  has not been pre
21180 76 69 6f 75 73 6c 79 20 73 74 72 65 73 73 65 64  viously stressed
21181 2e 2e 2e 20 20 28 53 51 4c 69 74 65 20 74 69 63  ...  (SQLite tic
21182 6b 65 74 20 23 32 31 37 32 29 0a 20 20 2a 2f 0a  ket #2172).  */.
21183 20 20 6d 65 6d 73 65 74 28 26 79 79 67 6f 74 6f    memset(&yygoto
21184 6d 69 6e 6f 72 2c 20 30 2c 20 73 69 7a 65 6f 66  minor, 0, sizeof
21185 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 29 29 3b 0a  (yygotominor));.
21186 0a 0a 20 20 73 77 69 74 63 68 28 20 79 79 72 75  ..  switch( yyru
21187 6c 65 6e 6f 20 29 7b 0a 20 20 2f 2a 20 42 65 67  leno ){.  /* Beg
21188 69 6e 6e 69 6e 67 20 68 65 72 65 20 61 72 65 20  inning here are 
21189 74 68 65 20 72 65 64 75 63 74 69 6f 6e 20 63 61  the reduction ca
2118a 73 65 73 2e 20 20 41 20 74 79 70 69 63 61 6c 20  ses.  A typical 
2118b 65 78 61 6d 70 6c 65 0a 20 20 2a 2a 20 66 6f 6c  example.  ** fol
2118c 6c 6f 77 73 3a 0a 20 20 2a 2a 20 20 20 63 61 73  lows:.  **   cas
2118d 65 20 30 3a 0a 20 20 2a 2a 20 20 23 6c 69 6e 65  e 0:.  **  #line
2118e 20 3c 6c 69 6e 65 6e 6f 3e 20 3c 67 72 61 6d 6d   <lineno> <gramm
2118f 61 72 66 69 6c 65 3e 0a 20 20 2a 2a 20 20 20 20  arfile>.  **    
21190 20 7b 20 2e 2e 2e 20 7d 20 20 20 20 20 20 20 20   { ... }        
21191 20 20 20 2f 2f 20 55 73 65 72 20 73 75 70 70 6c     // User suppl
21192 69 65 64 20 63 6f 64 65 0a 20 20 2a 2a 20 20 23  ied code.  **  #
21193 6c 69 6e 65 20 3c 6c 69 6e 65 6e 6f 3e 20 3c 74  line <lineno> <t
21194 68 69 73 66 69 6c 65 3e 0a 20 20 2a 2a 20 20 20  hisfile>.  **   
21195 20 20 62 72 65 61 6b 3b 0a 20 20 2a 2f 0a 20 20    break;.  */.  
21196 20 20 20 20 63 61 73 65 20 30 3a 0a 20 20 20 20      case 0:.    
21197 20 20 63 61 73 65 20 31 3a 0a 20 20 20 20 20 20    case 1:.      
21198 63 61 73 65 20 32 3a 0a 20 20 20 20 20 20 63 61  case 2:.      ca
21199 73 65 20 34 3a 0a 20 20 20 20 20 20 63 61 73 65  se 4:.      case
2119a 20 35 3a 0a 20 20 20 20 20 20 63 61 73 65 20 31   5:.      case 1
2119b 30 3a 0a 20 20 20 20 20 20 63 61 73 65 20 31 31  0:.      case 11
2119c 3a 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 3a  :.      case 12:
2119d 0a 20 20 20 20 20 20 63 61 73 65 20 32 30 3a 0a  .      case 20:.
2119e 20 20 20 20 20 20 63 61 73 65 20 32 38 3a 0a 20        case 28:. 
2119f 20 20 20 20 20 63 61 73 65 20 32 39 3a 0a 20 20       case 29:.  
211a0 20 20 20 20 63 61 73 65 20 33 37 3a 0a 20 20 20      case 37:.   
211a1 20 20 20 63 61 73 65 20 34 34 3a 0a 20 20 20 20     case 44:.    
211a2 20 20 63 61 73 65 20 34 35 3a 0a 20 20 20 20 20    case 45:.     
211a3 20 63 61 73 65 20 34 36 3a 0a 20 20 20 20 20 20   case 46:.      
211a4 63 61 73 65 20 34 37 3a 0a 20 20 20 20 20 20 63  case 47:.      c
211a5 61 73 65 20 34 38 3a 0a 20 20 20 20 20 20 63 61  ase 48:.      ca
211a6 73 65 20 34 39 3a 0a 20 20 20 20 20 20 63 61 73  se 49:.      cas
211a7 65 20 35 35 3a 0a 20 20 20 20 20 20 63 61 73 65  e 55:.      case
211a8 20 38 32 3a 0a 20 20 20 20 20 20 63 61 73 65 20   82:.      case 
211a9 38 33 3a 0a 20 20 20 20 20 20 63 61 73 65 20 38  83:.      case 8
211aa 34 3a 0a 20 20 20 20 20 20 63 61 73 65 20 38 35  4:.      case 85
211ab 3a 0a 20 20 20 20 20 20 63 61 73 65 20 32 35 36  :.      case 256
211ac 3a 0a 20 20 20 20 20 20 63 61 73 65 20 32 35 37  :.      case 257
211ad 3a 0a 20 20 20 20 20 20 63 61 73 65 20 32 36 37  :.      case 267
211ae 3a 0a 20 20 20 20 20 20 63 61 73 65 20 32 36 38  :.      case 268
211af 3a 0a 20 20 20 20 20 20 63 61 73 65 20 32 38 38  :.      case 288
211b0 3a 0a 20 20 20 20 20 20 63 61 73 65 20 32 38 39  :.      case 289
211b1 3a 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 37  :.      case 297
211b2 3a 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 38  :.      case 298
211b3 3a 0a 20 20 20 20 20 20 63 61 73 65 20 33 30 32  :.      case 302
211b4 3a 0a 20 20 20 20 20 20 63 61 73 65 20 33 30 33  :.      case 303
211b5 3a 0a 20 20 20 20 20 20 63 61 73 65 20 33 30 35  :.      case 305
211b6 3a 0a 20 20 20 20 20 20 63 61 73 65 20 33 30 39  :.      case 309
211b7 3a 0a 7b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  :.{.}.        br
211b8 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
211b9 33 3a 0a 7b 20 73 71 6c 69 74 65 33 46 69 6e 69  3:.{ sqlite3Fini
211ba 73 68 43 6f 64 69 6e 67 28 70 50 61 72 73 65 29  shCoding(pParse)
211bb 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ; }.        brea
211bc 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36 3a  k;.      case 6:
211bd 0a 7b 20 73 71 6c 69 74 65 33 42 65 67 69 6e 50  .{ sqlite3BeginP
211be 61 72 73 65 28 70 50 61 72 73 65 2c 20 30 29 3b  arse(pParse, 0);
211bf 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
211c0 3b 0a 20 20 20 20 20 20 63 61 73 65 20 37 3a 0a  ;.      case 7:.
211c1 7b 20 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61  { sqlite3BeginPa
211c2 72 73 65 28 70 50 61 72 73 65 2c 20 31 29 3b 20  rse(pParse, 1); 
211c3 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
211c4 0a 20 20 20 20 20 20 63 61 73 65 20 38 3a 0a 7b  .      case 8:.{
211c5 20 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61 72   sqlite3BeginPar
211c6 73 65 28 70 50 61 72 73 65 2c 20 32 29 3b 20 7d  se(pParse, 2); }
211c7 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
211c8 20 20 20 20 20 20 63 61 73 65 20 39 3a 0a 7b 73        case 9:.{s
211c9 71 6c 69 74 65 33 42 65 67 69 6e 54 72 61 6e 73  qlite3BeginTrans
211ca 61 63 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 79  action(pParse, y
211cb 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
211cc 79 34 36 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  y46);}.        b
211cd 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
211ce 20 31 33 3a 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f   13:.{yygotomino
211cf 72 2e 79 79 34 36 20 3d 20 54 4b 5f 44 45 46 45  r.yy46 = TK_DEFE
211d0 52 52 45 44 3b 7d 0a 20 20 20 20 20 20 20 20 62  RRED;}.        b
211d1 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
211d2 20 31 34 3a 0a 20 20 20 20 20 20 63 61 73 65 20   14:.      case 
211d3 31 35 3a 0a 20 20 20 20 20 20 63 61 73 65 20 31  15:.      case 1
211d4 36 3a 0a 20 20 20 20 20 20 63 61 73 65 20 31 30  6:.      case 10
211d5 37 3a 0a 20 20 20 20 20 20 63 61 73 65 20 31 30  7:.      case 10
211d6 39 3a 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  9:.{yygotominor.
211d7 79 79 34 36 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  yy46 = yymsp[0].
211d8 6d 61 6a 6f 72 3b 7d 0a 20 20 20 20 20 20 20 20  major;}.        
211d9 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
211da 65 20 31 37 3a 0a 20 20 20 20 20 20 63 61 73 65  e 17:.      case
211db 20 31 38 3a 0a 7b 73 71 6c 69 74 65 33 43 6f 6d   18:.{sqlite3Com
211dc 6d 69 74 54 72 61 6e 73 61 63 74 69 6f 6e 28 70  mitTransaction(p
211dd 50 61 72 73 65 29 3b 7d 0a 20 20 20 20 20 20 20  Parse);}.       
211de 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
211df 73 65 20 31 39 3a 0a 7b 73 71 6c 69 74 65 33 52  se 19:.{sqlite3R
211e0 6f 6c 6c 62 61 63 6b 54 72 61 6e 73 61 63 74 69  ollbackTransacti
211e1 6f 6e 28 70 50 61 72 73 65 29 3b 7d 0a 20 20 20  on(pParse);}.   
211e2 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
211e3 20 20 63 61 73 65 20 32 31 3a 0a 7b 0a 20 20 20    case 21:.{.   
211e4 73 71 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c  sqlite3StartTabl
211e5 65 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b  e(pParse,&yymsp[
211e6 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c  -1].minor.yy410,
211e7 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
211e8 79 79 34 31 30 2c 79 79 6d 73 70 5b 2d 34 5d 2e  yy410,yymsp[-4].
211e9 6d 69 6e 6f 72 2e 79 79 34 36 2c 30 2c 30 2c 79  minor.yy46,0,0,y
211ea 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
211eb 79 34 36 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  y46);.}.        
211ec 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
211ed 65 20 32 32 3a 0a 20 20 20 20 20 20 63 61 73 65  e 22:.      case
211ee 20 32 35 3a 0a 20 20 20 20 20 20 63 61 73 65 20   25:.      case 
211ef 36 33 3a 0a 20 20 20 20 20 20 63 61 73 65 20 37  63:.      case 7
211f0 37 3a 0a 20 20 20 20 20 20 63 61 73 65 20 37 39  7:.      case 79
211f1 3a 0a 20 20 20 20 20 20 63 61 73 65 20 39 30 3a  :.      case 90:
211f2 0a 20 20 20 20 20 20 63 61 73 65 20 31 30 31 3a  .      case 101:
211f3 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 32 3a  .      case 112:
211f4 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 33 3a  .      case 113:
211f5 0a 20 20 20 20 20 20 63 61 73 65 20 32 31 32 3a  .      case 212:
211f6 0a 20 20 20 20 20 20 63 61 73 65 20 32 31 35 3a  .      case 215:
211f7 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
211f8 34 36 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20  46 = 0;}.       
211f9 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
211fa 73 65 20 32 33 3a 0a 20 20 20 20 20 20 63 61 73  se 23:.      cas
211fb 65 20 32 34 3a 0a 20 20 20 20 20 20 63 61 73 65  e 24:.      case
211fc 20 36 34 3a 0a 20 20 20 20 20 20 63 61 73 65 20   64:.      case 
211fd 37 38 3a 0a 20 20 20 20 20 20 63 61 73 65 20 31  78:.      case 1
211fe 30 30 3a 0a 20 20 20 20 20 20 63 61 73 65 20 31  00:.      case 1
211ff 31 31 3a 0a 20 20 20 20 20 20 63 61 73 65 20 32  11:.      case 2
21200 31 33 3a 0a 20 20 20 20 20 20 63 61 73 65 20 32  13:.      case 2
21201 31 36 3a 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72  16:.{yygotominor
21202 2e 79 79 34 36 20 3d 20 31 3b 7d 0a 20 20 20 20  .yy46 = 1;}.    
21203 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
21204 20 63 61 73 65 20 32 36 3a 0a 7b 0a 20 20 73 71   case 26:.{.  sq
21205 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 28 70 50  lite3EndTable(pP
21206 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e  arse,&yymsp[-1].
21207 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 26 79 79 6d  minor.yy410,&yym
21208 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  sp[0].minor.yy0,
21209 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  0);.}.        br
2120a 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2120b 32 37 3a 0a 7b 0a 20 20 73 71 6c 69 74 65 33 45  27:.{.  sqlite3E
2120c 6e 64 54 61 62 6c 65 28 70 50 61 72 73 65 2c 30  ndTable(pParse,0
2120d 2c 30 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  ,0,yymsp[0].mino
2120e 72 2e 79 79 32 31 39 29 3b 0a 20 20 73 71 6c 69  r.yy219);.  sqli
2120f 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
21210 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
21211 79 32 31 39 29 3b 0a 7d 0a 20 20 20 20 20 20 20  y219);.}.       
21212 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
21213 73 65 20 33 30 3a 0a 7b 0a 20 20 79 79 67 6f 74  se 30:.{.  yygot
21214 6f 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 7a 20 3d  ominor.yy410.z =
21215 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72   yymsp[-2].minor
21216 2e 79 79 34 31 30 2e 7a 3b 0a 20 20 79 79 67 6f  .yy410.z;.  yygo
21217 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 6e 20  tominor.yy410.n 
21218 3d 20 28 70 50 61 72 73 65 2d 3e 73 4c 61 73 74  = (pParse->sLast
21219 54 6f 6b 65 6e 2e 7a 2d 79 79 6d 73 70 5b 2d 32  Token.z-yymsp[-2
2121a 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 7a 29  ].minor.yy410.z)
2121b 20 2b 20 70 50 61 72 73 65 2d 3e 73 4c 61 73 74   + pParse->sLast
2121c 54 6f 6b 65 6e 2e 6e 3b 0a 7d 0a 20 20 20 20 20  Token.n;.}.     
2121d 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2121e 63 61 73 65 20 33 31 3a 0a 7b 0a 20 20 73 71 6c  case 31:.{.  sql
2121f 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 28 70 50  ite3AddColumn(pP
21220 61 72 73 65 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  arse,&yymsp[0].m
21221 69 6e 6f 72 2e 79 79 34 31 30 29 3b 0a 20 20 79  inor.yy410);.  y
21222 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 30  ygotominor.yy410
21223 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
21224 72 2e 79 79 34 31 30 3b 0a 7d 0a 20 20 20 20 20  r.yy410;.}.     
21225 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
21226 63 61 73 65 20 33 32 3a 0a 20 20 20 20 20 20 63  case 32:.      c
21227 61 73 65 20 33 33 3a 0a 20 20 20 20 20 20 63 61  ase 33:.      ca
21228 73 65 20 33 34 3a 0a 20 20 20 20 20 20 63 61 73  se 34:.      cas
21229 65 20 33 35 3a 0a 20 20 20 20 20 20 63 61 73 65  e 35:.      case
2122a 20 33 36 3a 0a 20 20 20 20 20 20 63 61 73 65 20   36:.      case 
2122b 32 35 35 3a 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  255:.{yygotomino
2122c 72 2e 79 79 34 31 30 20 3d 20 79 79 6d 73 70 5b  r.yy410 = yymsp[
2122d 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 7d 0a 20  0].minor.yy0;}. 
2122e 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2122f 20 20 20 20 63 61 73 65 20 33 38 3a 0a 7b 73 71      case 38:.{sq
21230 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 54 79  lite3AddColumnTy
21231 70 65 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70  pe(pParse,&yymsp
21232 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 29  [0].minor.yy410)
21233 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
21234 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 39 3a  ;.      case 39:
21235 0a 20 20 20 20 20 20 63 61 73 65 20 34 32 3a 0a  .      case 42:.
21236 20 20 20 20 20 20 63 61 73 65 20 31 31 39 3a 0a        case 119:.
21237 20 20 20 20 20 20 63 61 73 65 20 31 32 30 3a 0a        case 120:.
21238 20 20 20 20 20 20 63 61 73 65 20 31 33 31 3a 0a        case 131:.
21239 20 20 20 20 20 20 63 61 73 65 20 32 34 30 3a 0a        case 240:.
2123a 20 20 20 20 20 20 63 61 73 65 20 32 34 32 3a 0a        case 242:.
2123b 20 20 20 20 20 20 63 61 73 65 20 32 35 31 3a 0a        case 251:.
2123c 20 20 20 20 20 20 63 61 73 65 20 32 35 32 3a 0a        case 252:.
2123d 20 20 20 20 20 20 63 61 73 65 20 32 35 33 3a 0a        case 253:.
2123e 20 20 20 20 20 20 63 61 73 65 20 32 35 34 3a 0a        case 254:.
2123f 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34  {yygotominor.yy4
21240 31 30 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  10 = yymsp[0].mi
21241 6e 6f 72 2e 79 79 34 31 30 3b 7d 0a 20 20 20 20  nor.yy410;}.    
21242 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
21243 20 63 61 73 65 20 34 30 3a 0a 7b 0a 20 20 79 79   case 40:.{.  yy
21244 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 30 2e  gotominor.yy410.
21245 7a 20 3d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  z = yymsp[-3].mi
21246 6e 6f 72 2e 79 79 34 31 30 2e 7a 3b 0a 20 20 79  nor.yy410.z;.  y
21247 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 30  ygotominor.yy410
21248 2e 6e 20 3d 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  .n = &yymsp[0].m
21249 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70  inor.yy0.z[yymsp
2124a 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d  [0].minor.yy0.n]
2124b 20 2d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e   - yymsp[-3].min
2124c 6f 72 2e 79 79 34 31 30 2e 7a 3b 0a 7d 0a 20 20  or.yy410.z;.}.  
2124d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2124e 20 20 20 63 61 73 65 20 34 31 3a 0a 7b 0a 20 20     case 41:.{.  
2124f 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 31  yygotominor.yy41
21250 30 2e 7a 20 3d 20 79 79 6d 73 70 5b 2d 35 5d 2e  0.z = yymsp[-5].
21251 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 7a 3b 0a 20  minor.yy410.z;. 
21252 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34   yygotominor.yy4
21253 31 30 2e 6e 20 3d 20 26 79 79 6d 73 70 5b 30 5d  10.n = &yymsp[0]
21254 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d  .minor.yy0.z[yym
21255 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  sp[0].minor.yy0.
21256 6e 5d 20 2d 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d  n] - yymsp[-5].m
21257 69 6e 6f 72 2e 79 79 34 31 30 2e 7a 3b 0a 7d 0a  inor.yy410.z;.}.
21258 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
21259 20 20 20 20 20 63 61 73 65 20 34 33 3a 0a 7b 79       case 43:.{y
2125a 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 30  ygotominor.yy410
2125b 2e 7a 3d 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  .z=yymsp[-1].min
2125c 6f 72 2e 79 79 34 31 30 2e 7a 3b 20 79 79 67 6f  or.yy410.z; yygo
2125d 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 6e 3d  tominor.yy410.n=
2125e 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2125f 79 34 31 30 2e 6e 2b 28 79 79 6d 73 70 5b 30 5d  y410.n+(yymsp[0]
21260 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 7a 2d 79  .minor.yy410.z-y
21261 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
21262 79 34 31 30 2e 7a 29 3b 7d 0a 20 20 20 20 20 20  y410.z);}.      
21263 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
21264 61 73 65 20 35 30 3a 0a 20 20 20 20 20 20 63 61  ase 50:.      ca
21265 73 65 20 35 32 3a 0a 7b 73 71 6c 69 74 65 33 41  se 52:.{sqlite3A
21266 64 64 44 65 66 61 75 6c 74 56 61 6c 75 65 28 70  ddDefaultValue(p
21267 50 61 72 73 65 2c 79 79 6d 73 70 5b 30 5d 2e 6d  Parse,yymsp[0].m
21268 69 6e 6f 72 2e 79 79 31 37 32 29 3b 7d 0a 20 20  inor.yy172);}.  
21269 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2126a 20 20 20 63 61 73 65 20 35 31 3a 0a 7b 73 71 6c     case 51:.{sql
2126b 69 74 65 33 41 64 64 44 65 66 61 75 6c 74 56 61  ite3AddDefaultVa
2126c 6c 75 65 28 70 50 61 72 73 65 2c 79 79 6d 73 70  lue(pParse,yymsp
2126d 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32  [-1].minor.yy172
2126e 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
2126f 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 35 33  k;.      case 53
21270 3a 0a 7b 0a 20 20 45 78 70 72 20 2a 70 20 3d 20  :.{.  Expr *p = 
21271 73 71 6c 69 74 65 33 45 78 70 72 28 54 4b 5f 55  sqlite3Expr(TK_U
21272 4d 49 4e 55 53 2c 20 79 79 6d 73 70 5b 30 5d 2e  MINUS, yymsp[0].
21273 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 20 30 2c 20  minor.yy172, 0, 
21274 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 41 64 64  0);.  sqlite3Add
21275 44 65 66 61 75 6c 74 56 61 6c 75 65 28 70 50 61  DefaultValue(pPa
21276 72 73 65 2c 70 29 3b 0a 7d 0a 20 20 20 20 20 20  rse,p);.}.      
21277 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
21278 61 73 65 20 35 34 3a 0a 7b 0a 20 20 45 78 70 72  ase 54:.{.  Expr
21279 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70   *p = sqlite3Exp
2127a 72 28 54 4b 5f 53 54 52 49 4e 47 2c 20 30 2c 20  r(TK_STRING, 0, 
2127b 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  0, &yymsp[0].min
2127c 6f 72 2e 79 79 34 31 30 29 3b 0a 20 20 73 71 6c  or.yy410);.  sql
2127d 69 74 65 33 41 64 64 44 65 66 61 75 6c 74 56 61  ite3AddDefaultVa
2127e 6c 75 65 28 70 50 61 72 73 65 2c 70 29 3b 0a 7d  lue(pParse,p);.}
2127f 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
21280 20 20 20 20 20 20 63 61 73 65 20 35 36 3a 0a 7b        case 56:.{
21281 73 71 6c 69 74 65 33 41 64 64 4e 6f 74 4e 75 6c  sqlite3AddNotNul
21282 6c 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b  l(pParse, yymsp[
21283 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 29 3b 7d  0].minor.yy46);}
21284 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
21285 20 20 20 20 20 20 63 61 73 65 20 35 37 3a 0a 7b        case 57:.{
21286 73 71 6c 69 74 65 33 41 64 64 50 72 69 6d 61 72  sqlite3AddPrimar
21287 79 4b 65 79 28 70 50 61 72 73 65 2c 30 2c 79 79  yKey(pParse,0,yy
21288 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
21289 34 36 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  46,yymsp[0].mino
2128a 72 2e 79 79 34 36 2c 79 79 6d 73 70 5b 2d 32 5d  r.yy46,yymsp[-2]
2128b 2e 6d 69 6e 6f 72 2e 79 79 34 36 29 3b 7d 0a 20  .minor.yy46);}. 
2128c 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2128d 20 20 20 20 63 61 73 65 20 35 38 3a 0a 7b 73 71      case 58:.{sq
2128e 6c 69 74 65 33 43 72 65 61 74 65 49 6e 64 65 78  lite3CreateIndex
2128f 28 70 50 61 72 73 65 2c 30 2c 30 2c 30 2c 30 2c  (pParse,0,0,0,0,
21290 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
21291 79 34 36 2c 30 2c 30 2c 30 2c 30 29 3b 7d 0a 20  y46,0,0,0,0);}. 
21292 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
21293 20 20 20 20 63 61 73 65 20 35 39 3a 0a 7b 73 71      case 59:.{sq
21294 6c 69 74 65 33 41 64 64 43 68 65 63 6b 43 6f 6e  lite3AddCheckCon
21295 73 74 72 61 69 6e 74 28 70 50 61 72 73 65 2c 79  straint(pParse,y
21296 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
21297 79 31 37 32 29 3b 7d 0a 20 20 20 20 20 20 20 20  y172);}.        
21298 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
21299 65 20 36 30 3a 0a 7b 73 71 6c 69 74 65 33 43 72  e 60:.{sqlite3Cr
2129a 65 61 74 65 46 6f 72 65 69 67 6e 4b 65 79 28 70  eateForeignKey(p
2129b 50 61 72 73 65 2c 30 2c 26 79 79 6d 73 70 5b 2d  Parse,0,&yymsp[-
2129c 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 79  2].minor.yy410,y
2129d 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2129e 79 31 37 34 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69  y174,yymsp[0].mi
2129f 6e 6f 72 2e 79 79 34 36 29 3b 7d 0a 20 20 20 20  nor.yy46);}.    
212a0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
212a1 20 63 61 73 65 20 36 31 3a 0a 7b 73 71 6c 69 74   case 61:.{sqlit
212a2 65 33 44 65 66 65 72 46 6f 72 65 69 67 6e 4b 65  e3DeferForeignKe
212a3 79 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 30  y(pParse,yymsp[0
212a4 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 29 3b 7d 0a  ].minor.yy46);}.
212a5 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
212a6 20 20 20 20 20 63 61 73 65 20 36 32 3a 0a 7b 73       case 62:.{s
212a7 71 6c 69 74 65 33 41 64 64 43 6f 6c 6c 61 74 65  qlite3AddCollate
212a8 54 79 70 65 28 70 50 61 72 73 65 2c 20 28 63 68  Type(pParse, (ch
212a9 61 72 2a 29 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  ar*)yymsp[0].min
212aa 6f 72 2e 79 79 34 31 30 2e 7a 2c 20 79 79 6d 73  or.yy410.z, yyms
212ab 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30  p[0].minor.yy410
212ac 2e 6e 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  .n);}.        br
212ad 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
212ae 36 35 3a 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f  65:.{ yygotomino
212af 72 2e 79 79 34 36 20 3d 20 4f 45 5f 52 65 73 74  r.yy46 = OE_Rest
212b0 72 69 63 74 20 2a 20 30 78 30 31 30 31 30 31 3b  rict * 0x010101;
212b1 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
212b2 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36 36 3a  ;.      case 66:
212b3 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .{ yygotominor.y
212b4 79 34 36 20 3d 20 28 79 79 6d 73 70 5b 2d 31 5d  y46 = (yymsp[-1]
212b5 2e 6d 69 6e 6f 72 2e 79 79 34 36 20 26 20 79 79  .minor.yy46 & yy
212b6 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34  msp[0].minor.yy4
212b7 30 35 2e 6d 61 73 6b 29 20 7c 20 79 79 6d 73 70  05.mask) | yymsp
212b8 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30 35 2e  [0].minor.yy405.
212b9 76 61 6c 75 65 3b 20 7d 0a 20 20 20 20 20 20 20  value; }.       
212ba 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
212bb 73 65 20 36 37 3a 0a 7b 20 79 79 67 6f 74 6f 6d  se 67:.{ yygotom
212bc 69 6e 6f 72 2e 79 79 34 30 35 2e 76 61 6c 75 65  inor.yy405.value
212bd 20 3d 20 30 3b 20 20 20 20 20 79 79 67 6f 74 6f   = 0;     yygoto
212be 6d 69 6e 6f 72 2e 79 79 34 30 35 2e 6d 61 73 6b  minor.yy405.mask
212bf 20 3d 20 30 78 30 30 30 30 30 30 3b 20 7d 0a 20   = 0x000000; }. 
212c0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
212c1 20 20 20 20 63 61 73 65 20 36 38 3a 0a 7b 20 79      case 68:.{ y
212c2 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 30 35  ygotominor.yy405
212c3 2e 76 61 6c 75 65 20 3d 20 79 79 6d 73 70 5b 30  .value = yymsp[0
212c4 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 3b 20 20 20  ].minor.yy46;   
212c5 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
212c6 34 30 35 2e 6d 61 73 6b 20 3d 20 30 78 30 30 30  405.mask = 0x000
212c7 30 66 66 3b 20 7d 0a 20 20 20 20 20 20 20 20 62  0ff; }.        b
212c8 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
212c9 20 36 39 3a 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e   69:.{ yygotomin
212ca 6f 72 2e 79 79 34 30 35 2e 76 61 6c 75 65 20 3d  or.yy405.value =
212cb 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
212cc 79 79 34 36 3c 3c 38 3b 20 20 79 79 67 6f 74 6f  yy46<<8;  yygoto
212cd 6d 69 6e 6f 72 2e 79 79 34 30 35 2e 6d 61 73 6b  minor.yy405.mask
212ce 20 3d 20 30 78 30 30 66 66 30 30 3b 20 7d 0a 20   = 0x00ff00; }. 
212cf 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
212d0 20 20 20 20 63 61 73 65 20 37 30 3a 0a 7b 20 79      case 70:.{ y
212d1 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 30 35  ygotominor.yy405
212d2 2e 76 61 6c 75 65 20 3d 20 79 79 6d 73 70 5b 30  .value = yymsp[0
212d3 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 3c 3c 31 36  ].minor.yy46<<16
212d4 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ; yygotominor.yy
212d5 34 30 35 2e 6d 61 73 6b 20 3d 20 30 78 66 66 30  405.mask = 0xff0
212d6 30 30 30 3b 20 7d 0a 20 20 20 20 20 20 20 20 62  000; }.        b
212d7 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
212d8 20 37 31 3a 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e   71:.{ yygotomin
212d9 6f 72 2e 79 79 34 36 20 3d 20 4f 45 5f 53 65 74  or.yy46 = OE_Set
212da 4e 75 6c 6c 3b 20 7d 0a 20 20 20 20 20 20 20 20  Null; }.        
212db 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
212dc 65 20 37 32 3a 0a 7b 20 79 79 67 6f 74 6f 6d 69  e 72:.{ yygotomi
212dd 6e 6f 72 2e 79 79 34 36 20 3d 20 4f 45 5f 53 65  nor.yy46 = OE_Se
212de 74 44 66 6c 74 3b 20 7d 0a 20 20 20 20 20 20 20  tDflt; }.       
212df 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
212e0 73 65 20 37 33 3a 0a 7b 20 79 79 67 6f 74 6f 6d  se 73:.{ yygotom
212e1 69 6e 6f 72 2e 79 79 34 36 20 3d 20 4f 45 5f 43  inor.yy46 = OE_C
212e2 61 73 63 61 64 65 3b 20 7d 0a 20 20 20 20 20 20  ascade; }.      
212e3 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
212e4 61 73 65 20 37 34 3a 0a 7b 20 79 79 67 6f 74 6f  ase 74:.{ yygoto
212e5 6d 69 6e 6f 72 2e 79 79 34 36 20 3d 20 4f 45 5f  minor.yy46 = OE_
212e6 52 65 73 74 72 69 63 74 3b 20 7d 0a 20 20 20 20  Restrict; }.    
212e7 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
212e8 20 63 61 73 65 20 37 35 3a 0a 20 20 20 20 20 20   case 75:.      
212e9 63 61 73 65 20 37 36 3a 0a 20 20 20 20 20 20 63  case 76:.      c
212ea 61 73 65 20 39 31 3a 0a 20 20 20 20 20 20 63 61  ase 91:.      ca
212eb 73 65 20 39 33 3a 0a 20 20 20 20 20 20 63 61 73  se 93:.      cas
212ec 65 20 39 35 3a 0a 20 20 20 20 20 20 63 61 73 65  e 95:.      case
212ed 20 39 36 3a 0a 20 20 20 20 20 20 63 61 73 65 20   96:.      case 
212ee 31 36 36 3a 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  166:.{yygotomino
212ef 72 2e 79 79 34 36 20 3d 20 79 79 6d 73 70 5b 30  r.yy46 = yymsp[0
212f0 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 3b 7d 0a 20  ].minor.yy46;}. 
212f1 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
212f2 20 20 20 20 63 61 73 65 20 38 30 3a 0a 7b 79 79      case 80:.{yy
212f3 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 30 2e  gotominor.yy410.
212f4 6e 20 3d 20 30 3b 20 79 79 67 6f 74 6f 6d 69 6e  n = 0; yygotomin
212f5 6f 72 2e 79 79 34 31 30 2e 7a 20 3d 20 30 3b 7d  or.yy410.z = 0;}
212f6 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
212f7 20 20 20 20 20 20 63 61 73 65 20 38 31 3a 0a 7b        case 81:.{
212f8 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 31  yygotominor.yy41
212f9 30 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  0 = yymsp[-1].mi
212fa 6e 6f 72 2e 79 79 30 3b 7d 0a 20 20 20 20 20 20  nor.yy0;}.      
212fb 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
212fc 61 73 65 20 38 36 3a 0a 7b 73 71 6c 69 74 65 33  ase 86:.{sqlite3
212fd 41 64 64 50 72 69 6d 61 72 79 4b 65 79 28 70 50  AddPrimaryKey(pP
212fe 61 72 73 65 2c 79 79 6d 73 70 5b 2d 33 5d 2e 6d  arse,yymsp[-3].m
212ff 69 6e 6f 72 2e 79 79 31 37 34 2c 79 79 6d 73 70  inor.yy174,yymsp
21300 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 2c 79  [0].minor.yy46,y
21301 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
21302 79 34 36 2c 30 29 3b 7d 0a 20 20 20 20 20 20 20  y46,0);}.       
21303 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
21304 73 65 20 38 37 3a 0a 7b 73 71 6c 69 74 65 33 43  se 87:.{sqlite3C
21305 72 65 61 74 65 49 6e 64 65 78 28 70 50 61 72 73  reateIndex(pPars
21306 65 2c 30 2c 30 2c 30 2c 79 79 6d 73 70 5b 2d 32  e,0,0,0,yymsp[-2
21307 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 34 2c 79 79  ].minor.yy174,yy
21308 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34  msp[0].minor.yy4
21309 36 2c 30 2c 30 2c 30 2c 30 29 3b 7d 0a 20 20 20  6,0,0,0,0);}.   
2130a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2130b 20 20 63 61 73 65 20 38 38 3a 0a 7b 73 71 6c 69    case 88:.{sqli
2130c 74 65 33 41 64 64 43 68 65 63 6b 43 6f 6e 73 74  te3AddCheckConst
2130d 72 61 69 6e 74 28 70 50 61 72 73 65 2c 79 79 6d  raint(pParse,yym
2130e 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-2].minor.yy1
2130f 37 32 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  72);}.        br
21310 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
21311 38 39 3a 0a 7b 0a 20 20 20 20 73 71 6c 69 74 65  89:.{.    sqlite
21312 33 43 72 65 61 74 65 46 6f 72 65 69 67 6e 4b 65  3CreateForeignKe
21313 79 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b  y(pParse, yymsp[
21314 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 34 2c  -6].minor.yy174,
21315 20 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f   &yymsp[-3].mino
21316 72 2e 79 79 34 31 30 2c 20 79 79 6d 73 70 5b 2d  r.yy410, yymsp[-
21317 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 34 2c 20  2].minor.yy174, 
21318 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
21319 79 79 34 36 29 3b 0a 20 20 20 20 73 71 6c 69 74  yy46);.    sqlit
2131a 65 33 44 65 66 65 72 46 6f 72 65 69 67 6e 4b 65  e3DeferForeignKe
2131b 79 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b  y(pParse, yymsp[
2131c 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 29 3b 0a  0].minor.yy46);.
2131d 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2131e 0a 20 20 20 20 20 20 63 61 73 65 20 39 32 3a 0a  .      case 92:.
2131f 20 20 20 20 20 20 63 61 73 65 20 39 34 3a 0a 7b        case 94:.{
21320 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 36  yygotominor.yy46
21321 20 3d 20 4f 45 5f 44 65 66 61 75 6c 74 3b 7d 0a   = OE_Default;}.
21322 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
21323 20 20 20 20 20 63 61 73 65 20 39 37 3a 0a 7b 79       case 97:.{y
21324 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 36 20  ygotominor.yy46 
21325 3d 20 4f 45 5f 49 67 6e 6f 72 65 3b 7d 0a 20 20  = OE_Ignore;}.  
21326 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
21327 20 20 20 63 61 73 65 20 39 38 3a 0a 20 20 20 20     case 98:.    
21328 20 20 63 61 73 65 20 31 36 37 3a 0a 7b 79 79 67    case 167:.{yyg
21329 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 36 20 3d 20  otominor.yy46 = 
2132a 4f 45 5f 52 65 70 6c 61 63 65 3b 7d 0a 20 20 20  OE_Replace;}.   
2132b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2132c 20 20 63 61 73 65 20 39 39 3a 0a 7b 0a 20 20 73    case 99:.{.  s
2132d 71 6c 69 74 65 33 44 72 6f 70 54 61 62 6c 65 28  qlite3DropTable(
2132e 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d  pParse, yymsp[0]
2132f 2e 6d 69 6e 6f 72 2e 79 79 33 37 33 2c 20 30 2c  .minor.yy373, 0,
21330 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
21331 2e 79 79 34 36 29 3b 0a 7d 0a 20 20 20 20 20 20  .yy46);.}.      
21332 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
21333 61 73 65 20 31 30 32 3a 0a 7b 0a 20 20 73 71 6c  ase 102:.{.  sql
21334 69 74 65 33 43 72 65 61 74 65 56 69 65 77 28 70  ite3CreateView(p
21335 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d 37  Parse, &yymsp[-7
21336 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79  ].minor.yy0, &yy
21337 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
21338 34 31 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e  410, &yymsp[-2].
21339 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 20 79 79 6d  minor.yy410, yym
2133a 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32 31  sp[0].minor.yy21
2133b 39 2c 20 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e  9, yymsp[-6].min
2133c 6f 72 2e 79 79 34 36 2c 20 79 79 6d 73 70 5b 2d  or.yy46, yymsp[-
2133d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 29 3b 0a  4].minor.yy46);.
2133e 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2133f 0a 20 20 20 20 20 20 63 61 73 65 20 31 30 33 3a  .      case 103:
21340 0a 7b 0a 20 20 73 71 6c 69 74 65 33 44 72 6f 70  .{.  sqlite3Drop
21341 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 79 79  Table(pParse, yy
21342 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
21343 37 33 2c 20 31 2c 20 79 79 6d 73 70 5b 2d 31 5d  73, 1, yymsp[-1]
21344 2e 6d 69 6e 6f 72 2e 79 79 34 36 29 3b 0a 7d 0a  .minor.yy46);.}.
21345 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
21346 20 20 20 20 20 63 61 73 65 20 31 30 34 3a 0a 7b       case 104:.{
21347 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  .  sqlite3Select
21348 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 30  (pParse, yymsp[0
21349 5d 2e 6d 69 6e 6f 72 2e 79 79 32 31 39 2c 20 53  ].minor.yy219, S
2134a 52 54 5f 43 61 6c 6c 62 61 63 6b 2c 20 30 2c 20  RT_Callback, 0, 
2134b 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 73  0, 0, 0, 0);.  s
2134c 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
2134d 74 65 28 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  te(yymsp[0].mino
2134e 72 2e 79 79 32 31 39 29 3b 0a 7d 0a 20 20 20 20  r.yy219);.}.    
2134f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
21350 20 63 61 73 65 20 31 30 35 3a 0a 20 20 20 20 20   case 105:.     
21351 20 63 61 73 65 20 31 32 38 3a 0a 7b 79 79 67 6f   case 128:.{yygo
21352 74 6f 6d 69 6e 6f 72 2e 79 79 32 31 39 20 3d 20  tominor.yy219 = 
21353 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
21354 79 32 31 39 3b 7d 0a 20 20 20 20 20 20 20 20 62  y219;}.        b
21355 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
21356 20 31 30 36 3a 0a 7b 0a 20 20 69 66 28 20 79 79   106:.{.  if( yy
21357 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32  msp[0].minor.yy2
21358 31 39 20 29 7b 0a 20 20 20 20 79 79 6d 73 70 5b  19 ){.    yymsp[
21359 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32 31 39 2d 3e  0].minor.yy219->
2135a 6f 70 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  op = yymsp[-1].m
2135b 69 6e 6f 72 2e 79 79 34 36 3b 0a 20 20 20 20 79  inor.yy46;.    y
2135c 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2135d 32 31 39 2d 3e 70 50 72 69 6f 72 20 3d 20 79 79  219->pPrior = yy
2135e 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
2135f 32 31 39 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  219;.  }else{.  
21360 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
21361 65 6c 65 74 65 28 79 79 6d 73 70 5b 2d 32 5d 2e  elete(yymsp[-2].
21362 6d 69 6e 6f 72 2e 79 79 32 31 39 29 3b 0a 20 20  minor.yy219);.  
21363 7d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  }.  yygotominor.
21364 79 79 32 31 39 20 3d 20 79 79 6d 73 70 5b 30 5d  yy219 = yymsp[0]
21365 2e 6d 69 6e 6f 72 2e 79 79 32 31 39 3b 0a 7d 0a  .minor.yy219;.}.
21366 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
21367 20 20 20 20 20 63 61 73 65 20 31 30 38 3a 0a 7b       case 108:.{
21368 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 36  yygotominor.yy46
21369 20 3d 20 54 4b 5f 41 4c 4c 3b 7d 0a 20 20 20 20   = TK_ALL;}.    
2136a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2136b 20 63 61 73 65 20 31 31 30 3a 0a 7b 0a 20 20 79   case 110:.{.  y
2136c 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 31 39  ygotominor.yy219
2136d 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
2136e 4e 65 77 28 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69  New(yymsp[-6].mi
2136f 6e 6f 72 2e 79 79 31 37 34 2c 79 79 6d 73 70 5b  nor.yy174,yymsp[
21370 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 33 37 33 2c  -5].minor.yy373,
21371 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
21372 79 79 31 37 32 2c 79 79 6d 73 70 5b 2d 33 5d 2e  yy172,yymsp[-3].
21373 6d 69 6e 6f 72 2e 79 79 31 37 34 2c 79 79 6d 73  minor.yy174,yyms
21374 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37  p[-2].minor.yy17
21375 32 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  2,yymsp[-1].mino
21376 72 2e 79 79 31 37 34 2c 79 79 6d 73 70 5b 2d 37  r.yy174,yymsp[-7
21377 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 2c 79 79 6d  ].minor.yy46,yym
21378 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32 33  sp[0].minor.yy23
21379 34 2e 70 4c 69 6d 69 74 2c 79 79 6d 73 70 5b 30  4.pLimit,yymsp[0
2137a 5d 2e 6d 69 6e 6f 72 2e 79 79 32 33 34 2e 70 4f  ].minor.yy234.pO
2137b 66 66 73 65 74 29 3b 0a 7d 0a 20 20 20 20 20 20  ffset);.}.      
2137c 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2137d 61 73 65 20 31 31 34 3a 0a 20 20 20 20 20 20 63  ase 114:.      c
2137e 61 73 65 20 32 33 37 3a 0a 7b 79 79 67 6f 74 6f  ase 237:.{yygoto
2137f 6d 69 6e 6f 72 2e 79 79 31 37 34 20 3d 20 79 79  minor.yy174 = yy
21380 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
21381 31 37 34 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  174;}.        br
21382 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
21383 31 31 35 3a 0a 20 20 20 20 20 20 63 61 73 65 20  115:.      case 
21384 31 34 31 3a 0a 20 20 20 20 20 20 63 61 73 65 20  141:.      case 
21385 31 34 39 3a 0a 20 20 20 20 20 20 63 61 73 65 20  149:.      case 
21386 32 33 30 3a 0a 20 20 20 20 20 20 63 61 73 65 20  230:.      case 
21387 32 33 36 3a 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  236:.{yygotomino
21388 72 2e 79 79 31 37 34 20 3d 20 30 3b 7d 0a 20 20  r.yy174 = 0;}.  
21389 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2138a 20 20 20 63 61 73 65 20 31 31 36 3a 0a 7b 0a 20     case 116:.{. 
2138b 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
2138c 31 37 34 20 3d 20 73 71 6c 69 74 65 33 45 78 70  174 = sqlite3Exp
2138d 72 4c 69 73 74 41 70 70 65 6e 64 28 79 79 6d 73  rListAppend(yyms
2138e 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37  p[-2].minor.yy17
2138f 34 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  4,yymsp[-1].mino
21390 72 2e 79 79 31 37 32 2c 79 79 6d 73 70 5b 30 5d  r.yy172,yymsp[0]
21391 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 6e 3f 26  .minor.yy410.n?&
21392 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
21393 79 34 31 30 3a 30 29 3b 0a 7d 0a 20 20 20 20 20  y410:0);.}.     
21394 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
21395 63 61 73 65 20 31 31 37 3a 0a 7b 0a 20 20 79 79  case 117:.{.  yy
21396 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 34 20  gotominor.yy174 
21397 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
21398 74 41 70 70 65 6e 64 28 79 79 6d 73 70 5b 2d 31  tAppend(yymsp[-1
21399 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 34 2c 20 73  ].minor.yy174, s
2139a 71 6c 69 74 65 33 45 78 70 72 28 54 4b 5f 41 4c  qlite3Expr(TK_AL
2139b 4c 2c 20 30 2c 20 30 2c 20 30 29 2c 20 30 29 3b  L, 0, 0, 0), 0);
2139c 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
2139d 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 38  ;.      case 118
2139e 3a 0a 7b 0a 20 20 45 78 70 72 20 2a 70 52 69 67  :.{.  Expr *pRig
2139f 68 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ht = sqlite3Expr
213a0 28 54 4b 5f 41 4c 4c 2c 20 30 2c 20 30 2c 20 30  (TK_ALL, 0, 0, 0
213a1 29 3b 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 74  );.  Expr *pLeft
213a2 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 54   = sqlite3Expr(T
213a3 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79 79 6d  K_ID, 0, 0, &yym
213a4 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-2].minor.yy4
213a5 31 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  10);.  yygotomin
213a6 6f 72 2e 79 79 31 37 34 20 3d 20 73 71 6c 69 74  or.yy174 = sqlit
213a7 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
213a8 28 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72  (yymsp[-3].minor
213a9 2e 79 79 31 37 34 2c 20 73 71 6c 69 74 65 33 45  .yy174, sqlite3E
213aa 78 70 72 28 54 4b 5f 44 4f 54 2c 20 70 4c 65 66  xpr(TK_DOT, pLef
213ab 74 2c 20 70 52 69 67 68 74 2c 20 30 29 2c 20 30  t, pRight, 0), 0
213ac 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
213ad 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
213ae 32 31 3a 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72  21:.{yygotominor
213af 2e 79 79 34 31 30 2e 6e 20 3d 20 30 3b 7d 0a 20  .yy410.n = 0;}. 
213b0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
213b1 20 20 20 20 63 61 73 65 20 31 32 32 3a 0a 7b 79      case 122:.{y
213b2 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 37 33  ygotominor.yy373
213b3 20 3d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28   = sqliteMalloc(
213b4 73 69 7a 65 6f 66 28 2a 79 79 67 6f 74 6f 6d 69  sizeof(*yygotomi
213b5 6e 6f 72 2e 79 79 33 37 33 29 29 3b 7d 0a 20 20  nor.yy373));}.  
213b6 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
213b7 20 20 20 63 61 73 65 20 31 32 33 3a 0a 7b 0a 20     case 123:.{. 
213b8 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
213b9 37 33 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  73 = yymsp[0].mi
213ba 6e 6f 72 2e 79 79 33 37 33 3b 0a 20 20 73 71 6c  nor.yy373;.  sql
213bb 69 74 65 33 53 72 63 4c 69 73 74 53 68 69 66 74  ite3SrcListShift
213bc 4a 6f 69 6e 54 79 70 65 28 79 79 67 6f 74 6f 6d  JoinType(yygotom
213bd 69 6e 6f 72 2e 79 79 33 37 33 29 3b 0a 7d 0a 20  inor.yy373);.}. 
213be 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
213bf 20 20 20 20 63 61 73 65 20 31 32 34 3a 0a 7b 0a      case 124:.{.
213c0 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
213c1 79 33 37 33 20 3d 20 79 79 6d 73 70 5b 2d 31 5d  y373 = yymsp[-1]
213c2 2e 6d 69 6e 6f 72 2e 79 79 33 37 33 3b 0a 20 20  .minor.yy373;.  
213c3 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72   if( yygotominor
213c4 2e 79 79 33 37 33 20 26 26 20 79 79 67 6f 74 6f  .yy373 && yygoto
213c5 6d 69 6e 6f 72 2e 79 79 33 37 33 2d 3e 6e 53 72  minor.yy373->nSr
213c6 63 3e 30 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f  c>0 ) yygotomino
213c7 72 2e 79 79 33 37 33 2d 3e 61 5b 79 79 67 6f 74  r.yy373->a[yygot
213c8 6f 6d 69 6e 6f 72 2e 79 79 33 37 33 2d 3e 6e 53  ominor.yy373->nS
213c9 72 63 2d 31 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d  rc-1].jointype =
213ca 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
213cb 79 79 34 36 3b 0a 7d 0a 20 20 20 20 20 20 20 20  yy46;.}.        
213cc 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
213cd 65 20 31 32 35 3a 0a 7b 79 79 67 6f 74 6f 6d 69  e 125:.{yygotomi
213ce 6e 6f 72 2e 79 79 33 37 33 20 3d 20 30 3b 7d 0a  nor.yy373 = 0;}.
213cf 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
213d0 20 20 20 20 20 63 61 73 65 20 31 32 36 3a 0a 7b       case 126:.{
213d1 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
213d2 79 33 37 33 20 3d 20 73 71 6c 69 74 65 33 53 72  y373 = sqlite3Sr
213d3 63 4c 69 73 74 41 70 70 65 6e 64 46 72 6f 6d 54  cListAppendFromT
213d4 65 72 6d 28 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69  erm(yymsp[-5].mi
213d5 6e 6f 72 2e 79 79 33 37 33 2c 26 79 79 6d 73 70  nor.yy373,&yymsp
213d6 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30  [-4].minor.yy410
213d7 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  ,&yymsp[-3].mino
213d8 72 2e 79 79 34 31 30 2c 26 79 79 6d 73 70 5b 2d  r.yy410,&yymsp[-
213d9 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 30  2].minor.yy410,0
213da 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  ,yymsp[-1].minor
213db 2e 79 79 31 37 32 2c 79 79 6d 73 70 5b 30 5d 2e  .yy172,yymsp[0].
213dc 6d 69 6e 6f 72 2e 79 79 34 33 32 29 3b 0a 7d 0a  minor.yy432);.}.
213dd 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
213de 20 20 20 20 20 63 61 73 65 20 31 32 37 3a 0a 7b       case 127:.{
213df 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .    yygotominor
213e0 2e 79 79 33 37 33 20 3d 20 73 71 6c 69 74 65 33  .yy373 = sqlite3
213e1 53 72 63 4c 69 73 74 41 70 70 65 6e 64 46 72 6f  SrcListAppendFro
213e2 6d 54 65 72 6d 28 79 79 6d 73 70 5b 2d 36 5d 2e  mTerm(yymsp[-6].
213e3 6d 69 6e 6f 72 2e 79 79 33 37 33 2c 30 2c 30 2c  minor.yy373,0,0,
213e4 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  &yymsp[-2].minor
213e5 2e 79 79 34 31 30 2c 79 79 6d 73 70 5b 2d 34 5d  .yy410,yymsp[-4]
213e6 2e 6d 69 6e 6f 72 2e 79 79 32 31 39 2c 79 79 6d  .minor.yy219,yym
213e7 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-1].minor.yy1
213e8 37 32 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  72,yymsp[0].mino
213e9 72 2e 79 79 34 33 32 29 3b 0a 20 20 7d 0a 20 20  r.yy432);.  }.  
213ea 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
213eb 20 20 20 63 61 73 65 20 31 32 39 3a 0a 7b 0a 20     case 129:.{. 
213ec 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69      sqlite3SrcLi
213ed 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 65 28  stShiftJoinType(
213ee 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
213ef 79 33 37 33 29 3b 0a 20 20 20 20 20 79 79 67 6f  y373);.     yygo
213f0 74 6f 6d 69 6e 6f 72 2e 79 79 32 31 39 20 3d 20  tominor.yy219 = 
213f1 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77  sqlite3SelectNew
213f2 28 30 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  (0,yymsp[0].mino
213f3 72 2e 79 79 33 37 33 2c 30 2c 30 2c 30 2c 30 2c  r.yy373,0,0,0,0,
213f4 30 2c 30 2c 30 29 3b 0a 20 20 7d 0a 20 20 20 20  0,0,0);.  }.    
213f5 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
213f6 20 63 61 73 65 20 31 33 30 3a 0a 7b 79 79 67 6f   case 130:.{yygo
213f7 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 7a 3d  tominor.yy410.z=
213f8 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  0; yygotominor.y
213f9 79 34 31 30 2e 6e 3d 30 3b 7d 0a 20 20 20 20 20  y410.n=0;}.     
213fa 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
213fb 63 61 73 65 20 31 33 32 3a 0a 7b 79 79 67 6f 74  case 132:.{yygot
213fc 6f 6d 69 6e 6f 72 2e 79 79 33 37 33 20 3d 20 73  ominor.yy373 = s
213fd 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70  qlite3SrcListApp
213fe 65 6e 64 28 30 2c 26 79 79 6d 73 70 5b 2d 31 5d  end(0,&yymsp[-1]
213ff 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 26 79 79  .minor.yy410,&yy
21400 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34  msp[0].minor.yy4
21401 31 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  10);}.        br
21402 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
21403 31 33 33 3a 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e  133:.{ yygotomin
21404 6f 72 2e 79 79 34 36 20 3d 20 4a 54 5f 49 4e 4e  or.yy46 = JT_INN
21405 45 52 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72  ER; }.        br
21406 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
21407 31 33 34 3a 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e  134:.{ yygotomin
21408 6f 72 2e 79 79 34 36 20 3d 20 73 71 6c 69 74 65  or.yy46 = sqlite
21409 33 4a 6f 69 6e 54 79 70 65 28 70 50 61 72 73 65  3JoinType(pParse
2140a 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  ,&yymsp[-1].mino
2140b 72 2e 79 79 30 2c 30 2c 30 29 3b 20 7d 0a 20 20  r.yy0,0,0); }.  
2140c 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2140d 20 20 20 63 61 73 65 20 31 33 35 3a 0a 7b 20 79     case 135:.{ y
2140e 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 36 20  ygotominor.yy46 
2140f 3d 20 73 71 6c 69 74 65 33 4a 6f 69 6e 54 79 70  = sqlite3JoinTyp
21410 65 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b  e(pParse,&yymsp[
21411 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -2].minor.yy0,&y
21412 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
21413 79 34 31 30 2c 30 29 3b 20 7d 0a 20 20 20 20 20  y410,0); }.     
21414 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
21415 63 61 73 65 20 31 33 36 3a 0a 7b 20 79 79 67 6f  case 136:.{ yygo
21416 74 6f 6d 69 6e 6f 72 2e 79 79 34 36 20 3d 20 73  tominor.yy46 = s
21417 71 6c 69 74 65 33 4a 6f 69 6e 54 79 70 65 28 70  qlite3JoinType(p
21418 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 33 5d  Parse,&yymsp[-3]
21419 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73  .minor.yy0,&yyms
2141a 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31  p[-2].minor.yy41
2141b 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  0,&yymsp[-1].min
2141c 6f 72 2e 79 79 34 31 30 29 3b 20 7d 0a 20 20 20  or.yy410); }.   
2141d 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2141e 20 20 63 61 73 65 20 31 33 37 3a 0a 20 20 20 20    case 137:.    
2141f 20 20 63 61 73 65 20 31 34 35 3a 0a 20 20 20 20    case 145:.    
21420 20 20 63 61 73 65 20 31 35 32 3a 0a 20 20 20 20    case 152:.    
21421 20 20 63 61 73 65 20 31 35 39 3a 0a 20 20 20 20    case 159:.    
21422 20 20 63 61 73 65 20 31 37 34 3a 0a 20 20 20 20    case 174:.    
21423 20 20 63 61 73 65 20 32 30 32 3a 0a 20 20 20 20    case 202:.    
21424 20 20 63 61 73 65 20 32 32 35 3a 0a 20 20 20 20    case 225:.    
21425 20 20 63 61 73 65 20 32 32 37 3a 0a 7b 79 79 67    case 227:.{yyg
21426 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20 3d  otominor.yy172 =
21427 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
21428 79 79 31 37 32 3b 7d 0a 20 20 20 20 20 20 20 20  yy172;}.        
21429 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2142a 65 20 31 33 38 3a 0a 20 20 20 20 20 20 63 61 73  e 138:.      cas
2142b 65 20 31 35 31 3a 0a 20 20 20 20 20 20 63 61 73  e 151:.      cas
2142c 65 20 31 35 38 3a 0a 20 20 20 20 20 20 63 61 73  e 158:.      cas
2142d 65 20 32 30 33 3a 0a 20 20 20 20 20 20 63 61 73  e 203:.      cas
2142e 65 20 32 32 36 3a 0a 20 20 20 20 20 20 63 61 73  e 226:.      cas
2142f 65 20 32 32 38 3a 0a 7b 79 79 67 6f 74 6f 6d 69  e 228:.{yygotomi
21430 6e 6f 72 2e 79 79 31 37 32 20 3d 20 30 3b 7d 0a  nor.yy172 = 0;}.
21431 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
21432 20 20 20 20 20 63 61 73 65 20 31 33 39 3a 0a 20       case 139:. 
21433 20 20 20 20 20 63 61 73 65 20 31 37 31 3a 0a 7b       case 171:.{
21434 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 33  yygotominor.yy43
21435 32 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  2 = yymsp[-1].mi
21436 6e 6f 72 2e 79 79 34 33 32 3b 7d 0a 20 20 20 20  nor.yy432;}.    
21437 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
21438 20 63 61 73 65 20 31 34 30 3a 0a 20 20 20 20 20   case 140:.     
21439 20 63 61 73 65 20 31 37 30 3a 0a 7b 79 79 67 6f   case 170:.{yygo
2143a 74 6f 6d 69 6e 6f 72 2e 79 79 34 33 32 20 3d 20  tominor.yy432 = 
2143b 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  0;}.        brea
2143c 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 34  k;.      case 14
2143d 32 3a 0a 20 20 20 20 20 20 63 61 73 65 20 31 35  2:.      case 15
2143e 30 3a 0a 20 20 20 20 20 20 63 61 73 65 20 32 32  0:.      case 22
2143f 39 3a 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  9:.{yygotominor.
21440 79 79 31 37 34 20 3d 20 79 79 6d 73 70 5b 30 5d  yy174 = yymsp[0]
21441 2e 6d 69 6e 6f 72 2e 79 79 31 37 34 3b 7d 0a 20  .minor.yy174;}. 
21442 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
21443 20 20 20 20 63 61 73 65 20 31 34 33 3a 0a 7b 0a      case 143:.{.
21444 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
21445 31 37 34 20 3d 20 73 71 6c 69 74 65 33 45 78 70  174 = sqlite3Exp
21446 72 4c 69 73 74 41 70 70 65 6e 64 28 79 79 6d 73  rListAppend(yyms
21447 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37  p[-3].minor.yy17
21448 34 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  4,yymsp[-1].mino
21449 72 2e 79 79 31 37 32 2c 30 29 3b 0a 20 20 69 66  r.yy172,0);.  if
2144a 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ( yygotominor.yy
2144b 31 37 34 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f  174 ) yygotomino
2144c 72 2e 79 79 31 37 34 2d 3e 61 5b 79 79 67 6f 74  r.yy174->a[yygot
2144d 6f 6d 69 6e 6f 72 2e 79 79 31 37 34 2d 3e 6e 45  ominor.yy174->nE
2144e 78 70 72 2d 31 5d 2e 73 6f 72 74 4f 72 64 65 72  xpr-1].sortOrder
2144f 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
21450 72 2e 79 79 34 36 3b 0a 7d 0a 20 20 20 20 20 20  r.yy46;.}.      
21451 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
21452 61 73 65 20 31 34 34 3a 0a 7b 0a 20 20 79 79 67  ase 144:.{.  yyg
21453 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 34 20 3d  otominor.yy174 =
21454 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
21455 41 70 70 65 6e 64 28 30 2c 79 79 6d 73 70 5b 2d  Append(0,yymsp[-
21456 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 30  1].minor.yy172,0
21457 29 3b 0a 20 20 69 66 28 20 79 79 67 6f 74 6f 6d  );.  if( yygotom
21458 69 6e 6f 72 2e 79 79 31 37 34 20 26 26 20 79 79  inor.yy174 && yy
21459 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 34 2d  gotominor.yy174-
2145a 3e 61 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  >a ) yygotominor
2145b 2e 79 79 31 37 34 2d 3e 61 5b 30 5d 2e 73 6f 72  .yy174->a[0].sor
2145c 74 4f 72 64 65 72 20 3d 20 79 79 6d 73 70 5b 30  tOrder = yymsp[0
2145d 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 3b 0a 7d 0a  ].minor.yy46;.}.
2145e 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2145f 20 20 20 20 20 63 61 73 65 20 31 34 36 3a 0a 20       case 146:. 
21460 20 20 20 20 20 63 61 73 65 20 31 34 38 3a 0a 7b       case 148:.{
21461 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 36  yygotominor.yy46
21462 20 3d 20 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43   = SQLITE_SO_ASC
21463 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
21464 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 34 37  ;.      case 147
21465 3a 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  :.{yygotominor.y
21466 79 34 36 20 3d 20 53 51 4c 49 54 45 5f 53 4f 5f  y46 = SQLITE_SO_
21467 44 45 53 43 3b 7d 0a 20 20 20 20 20 20 20 20 62  DESC;}.        b
21468 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
21469 20 31 35 33 3a 0a 7b 79 79 67 6f 74 6f 6d 69 6e   153:.{yygotomin
2146a 6f 72 2e 79 79 32 33 34 2e 70 4c 69 6d 69 74 20  or.yy234.pLimit 
2146b 3d 20 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  = 0; yygotominor
2146c 2e 79 79 32 33 34 2e 70 4f 66 66 73 65 74 20 3d  .yy234.pOffset =
2146d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65   0;}.        bre
2146e 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2146f 35 34 3a 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72  54:.{yygotominor
21470 2e 79 79 32 33 34 2e 70 4c 69 6d 69 74 20 3d 20  .yy234.pLimit = 
21471 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
21472 79 31 37 32 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f  y172; yygotomino
21473 72 2e 79 79 32 33 34 2e 70 4f 66 66 73 65 74 20  r.yy234.pOffset 
21474 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  = 0;}.        br
21475 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
21476 31 35 35 3a 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  155:.{yygotomino
21477 72 2e 79 79 32 33 34 2e 70 4c 69 6d 69 74 20 3d  r.yy234.pLimit =
21478 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72   yymsp[-2].minor
21479 2e 79 79 31 37 32 3b 20 79 79 67 6f 74 6f 6d 69  .yy172; yygotomi
2147a 6e 6f 72 2e 79 79 32 33 34 2e 70 4f 66 66 73 65  nor.yy234.pOffse
2147b 74 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  t = yymsp[0].min
2147c 6f 72 2e 79 79 31 37 32 3b 7d 0a 20 20 20 20 20  or.yy172;}.     
2147d 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2147e 63 61 73 65 20 31 35 36 3a 0a 7b 79 79 67 6f 74  case 156:.{yygot
2147f 6f 6d 69 6e 6f 72 2e 79 79 32 33 34 2e 70 4f 66  ominor.yy234.pOf
21480 66 73 65 74 20 3d 20 79 79 6d 73 70 5b 2d 32 5d  fset = yymsp[-2]
21481 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 3b 20 79 79  .minor.yy172; yy
21482 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 33 34 2e  gotominor.yy234.
21483 70 4c 69 6d 69 74 20 3d 20 79 79 6d 73 70 5b 30  pLimit = yymsp[0
21484 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 3b 7d 0a  ].minor.yy172;}.
21485 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
21486 20 20 20 20 20 63 61 73 65 20 31 35 37 3a 0a 7b       case 157:.{
21487 73 71 6c 69 74 65 33 44 65 6c 65 74 65 46 72 6f  sqlite3DeleteFro
21488 6d 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d  m(pParse,yymsp[-
21489 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 37 33 2c 79  1].minor.yy373,y
2148a 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2148b 31 37 32 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  172);}.        b
2148c 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2148d 20 31 36 30 3a 0a 7b 0a 20 20 73 71 6c 69 74 65   160:.{.  sqlite
2148e 33 45 78 70 72 4c 69 73 74 43 68 65 63 6b 4c 65  3ExprListCheckLe
2148f 6e 67 74 68 28 70 50 61 72 73 65 2c 79 79 6d 73  ngth(pParse,yyms
21490 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37  p[-1].minor.yy17
21491 34 2c 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c  4,SQLITE_MAX_COL
21492 55 4d 4e 2c 22 73 65 74 20 6c 69 73 74 22 29 3b  UMN,"set list");
21493 20 0a 20 20 73 71 6c 69 74 65 33 55 70 64 61 74   .  sqlite3Updat
21494 65 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d  e(pParse,yymsp[-
21495 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 37 33 2c 79  3].minor.yy373,y
21496 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
21497 79 31 37 34 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69  y174,yymsp[0].mi
21498 6e 6f 72 2e 79 79 31 37 32 2c 79 79 6d 73 70 5b  nor.yy172,yymsp[
21499 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 29 3b  -4].minor.yy46);
2149a 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
2149b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 36 31  ;.      case 161
2149c 3a 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  :.{yygotominor.y
2149d 79 31 37 34 20 3d 20 73 71 6c 69 74 65 33 45 78  y174 = sqlite3Ex
2149e 70 72 4c 69 73 74 41 70 70 65 6e 64 28 79 79 6d  prListAppend(yym
2149f 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-4].minor.yy1
214a0 37 34 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  74,yymsp[0].mino
214a1 72 2e 79 79 31 37 32 2c 26 79 79 6d 73 70 5b 2d  r.yy172,&yymsp[-
214a2 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 29 3b  2].minor.yy410);
214a3 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
214a4 0a 20 20 20 20 20 20 63 61 73 65 20 31 36 32 3a  .      case 162:
214a5 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
214a6 31 37 34 20 3d 20 73 71 6c 69 74 65 33 45 78 70  174 = sqlite3Exp
214a7 72 4c 69 73 74 41 70 70 65 6e 64 28 30 2c 79 79  rListAppend(0,yy
214a8 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31  msp[0].minor.yy1
214a9 37 32 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  72,&yymsp[-2].mi
214aa 6e 6f 72 2e 79 79 34 31 30 29 3b 7d 0a 20 20 20  nor.yy410);}.   
214ab 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
214ac 20 20 63 61 73 65 20 31 36 33 3a 0a 7b 73 71 6c    case 163:.{sql
214ad 69 74 65 33 49 6e 73 65 72 74 28 70 50 61 72 73  ite3Insert(pPars
214ae 65 2c 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e  e, yymsp[-5].min
214af 6f 72 2e 79 79 33 37 33 2c 20 79 79 6d 73 70 5b  or.yy373, yymsp[
214b0 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 34 2c  -1].minor.yy174,
214b1 20 30 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69   0, yymsp[-4].mi
214b2 6e 6f 72 2e 79 79 34 33 32 2c 20 79 79 6d 73 70  nor.yy432, yymsp
214b3 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 29  [-7].minor.yy46)
214b4 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
214b5 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 36 34  ;.      case 164
214b6 3a 0a 7b 73 71 6c 69 74 65 33 49 6e 73 65 72 74  :.{sqlite3Insert
214b7 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d  (pParse, yymsp[-
214b8 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 37 33 2c 20  2].minor.yy373, 
214b9 30 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  0, yymsp[0].mino
214ba 72 2e 79 79 32 31 39 2c 20 79 79 6d 73 70 5b 2d  r.yy219, yymsp[-
214bb 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 33 32 2c 20  1].minor.yy432, 
214bc 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
214bd 79 79 34 36 29 3b 7d 0a 20 20 20 20 20 20 20 20  yy46);}.        
214be 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
214bf 65 20 31 36 35 3a 0a 7b 73 71 6c 69 74 65 33 49  e 165:.{sqlite3I
214c0 6e 73 65 72 74 28 70 50 61 72 73 65 2c 20 79 79  nsert(pParse, yy
214c1 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
214c2 33 37 33 2c 20 30 2c 20 30 2c 20 79 79 6d 73 70  373, 0, 0, yymsp
214c3 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 33 32  [-2].minor.yy432
214c4 2c 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f  , yymsp[-5].mino
214c5 72 2e 79 79 34 36 29 3b 7d 0a 20 20 20 20 20 20  r.yy46);}.      
214c6 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
214c7 61 73 65 20 31 36 38 3a 0a 20 20 20 20 20 20 63  ase 168:.      c
214c8 61 73 65 20 32 33 31 3a 0a 7b 79 79 67 6f 74 6f  ase 231:.{yygoto
214c9 6d 69 6e 6f 72 2e 79 79 31 37 34 20 3d 20 73 71  minor.yy174 = sq
214ca 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
214cb 65 6e 64 28 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  end(yymsp[-2].mi
214cc 6e 6f 72 2e 79 79 31 37 34 2c 79 79 6d 73 70 5b  nor.yy174,yymsp[
214cd 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 30  0].minor.yy172,0
214ce 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
214cf 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 36  k;.      case 16
214d0 39 3a 0a 20 20 20 20 20 20 63 61 73 65 20 32 33  9:.      case 23
214d1 32 3a 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  2:.{yygotominor.
214d2 79 79 31 37 34 20 3d 20 73 71 6c 69 74 65 33 45  yy174 = sqlite3E
214d3 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 30 2c  xprListAppend(0,
214d4 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
214d5 79 31 37 32 2c 30 29 3b 7d 0a 20 20 20 20 20 20  y172,0);}.      
214d6 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
214d7 61 73 65 20 31 37 32 3a 0a 7b 79 79 67 6f 74 6f  ase 172:.{yygoto
214d8 6d 69 6e 6f 72 2e 79 79 34 33 32 20 3d 20 73 71  minor.yy432 = sq
214d9 6c 69 74 65 33 49 64 4c 69 73 74 41 70 70 65 6e  lite3IdListAppen
214da 64 28 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  d(yymsp[-2].mino
214db 72 2e 79 79 34 33 32 2c 26 79 79 6d 73 70 5b 30  r.yy432,&yymsp[0
214dc 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 29 3b 7d  ].minor.yy410);}
214dd 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
214de 20 20 20 20 20 20 63 61 73 65 20 31 37 33 3a 0a        case 173:.
214df 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34  {yygotominor.yy4
214e0 33 32 20 3d 20 73 71 6c 69 74 65 33 49 64 4c 69  32 = sqlite3IdLi
214e1 73 74 41 70 70 65 6e 64 28 30 2c 26 79 79 6d 73  stAppend(0,&yyms
214e2 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30  p[0].minor.yy410
214e3 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
214e4 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 37  k;.      case 17
214e5 35 3a 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  5:.{yygotominor.
214e6 79 79 31 37 32 20 3d 20 79 79 6d 73 70 5b 2d 31  yy172 = yymsp[-1
214e7 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 3b 20 73  ].minor.yy172; s
214e8 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28 79  qlite3ExprSpan(y
214e9 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32  ygotominor.yy172
214ea 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  ,&yymsp[-2].mino
214eb 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e  r.yy0,&yymsp[0].
214ec 6d 69 6e 6f 72 2e 79 79 30 29 3b 20 7d 0a 20 20  minor.yy0); }.  
214ed 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
214ee 20 20 20 63 61 73 65 20 31 37 36 3a 0a 20 20 20     case 176:.   
214ef 20 20 20 63 61 73 65 20 31 38 31 3a 0a 20 20 20     case 181:.   
214f0 20 20 20 63 61 73 65 20 31 38 32 3a 0a 7b 79 79     case 182:.{yy
214f1 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20  gotominor.yy172 
214f2 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 79 79  = sqlite3Expr(yy
214f3 6d 73 70 5b 30 5d 2e 6d 61 6a 6f 72 2c 20 30 2c  msp[0].major, 0,
214f4 20 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69   0, &yymsp[0].mi
214f5 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20  nor.yy0);}.     
214f6 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
214f7 63 61 73 65 20 31 37 37 3a 0a 20 20 20 20 20 20  case 177:.      
214f8 63 61 73 65 20 31 37 38 3a 0a 7b 79 79 67 6f 74  case 178:.{yygot
214f9 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73  ominor.yy172 = s
214fa 71 6c 69 74 65 33 45 78 70 72 28 54 4b 5f 49 44  qlite3Expr(TK_ID
214fb 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b 30  , 0, 0, &yymsp[0
214fc 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20  ].minor.yy0);}. 
214fd 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
214fe 20 20 20 20 63 61 73 65 20 31 37 39 3a 0a 7b 0a      case 179:.{.
214ff 20 20 45 78 70 72 20 2a 74 65 6d 70 31 20 3d 20    Expr *temp1 = 
21500 73 71 6c 69 74 65 33 45 78 70 72 28 54 4b 5f 49  sqlite3Expr(TK_I
21501 44 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b  D, 0, 0, &yymsp[
21502 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 29  -2].minor.yy410)
21503 3b 0a 20 20 45 78 70 72 20 2a 74 65 6d 70 32 20  ;.  Expr *temp2 
21504 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 54 4b  = sqlite3Expr(TK
21505 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73  _ID, 0, 0, &yyms
21506 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30  p[0].minor.yy410
21507 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  );.  yygotominor
21508 2e 79 79 31 37 32 20 3d 20 73 71 6c 69 74 65 33  .yy172 = sqlite3
21509 45 78 70 72 28 54 4b 5f 44 4f 54 2c 20 74 65 6d  Expr(TK_DOT, tem
2150a 70 31 2c 20 74 65 6d 70 32 2c 20 30 29 3b 0a 7d  p1, temp2, 0);.}
2150b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2150c 20 20 20 20 20 20 63 61 73 65 20 31 38 30 3a 0a        case 180:.
2150d 7b 0a 20 20 45 78 70 72 20 2a 74 65 6d 70 31 20  {.  Expr *temp1 
2150e 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 54 4b  = sqlite3Expr(TK
2150f 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73  _ID, 0, 0, &yyms
21510 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31  p[-4].minor.yy41
21511 30 29 3b 0a 20 20 45 78 70 72 20 2a 74 65 6d 70  0);.  Expr *temp
21512 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28  2 = sqlite3Expr(
21513 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79 79  TK_ID, 0, 0, &yy
21514 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
21515 34 31 30 29 3b 0a 20 20 45 78 70 72 20 2a 74 65  410);.  Expr *te
21516 6d 70 33 20 3d 20 73 71 6c 69 74 65 33 45 78 70  mp3 = sqlite3Exp
21517 72 28 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26  r(TK_ID, 0, 0, &
21518 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
21519 79 34 31 30 29 3b 0a 20 20 45 78 70 72 20 2a 74  y410);.  Expr *t
2151a 65 6d 70 34 20 3d 20 73 71 6c 69 74 65 33 45 78  emp4 = sqlite3Ex
2151b 70 72 28 54 4b 5f 44 4f 54 2c 20 74 65 6d 70 32  pr(TK_DOT, temp2
2151c 2c 20 74 65 6d 70 33 2c 20 30 29 3b 0a 20 20 79  , temp3, 0);.  y
2151d 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32  ygotominor.yy172
2151e 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 54   = sqlite3Expr(T
2151f 4b 5f 44 4f 54 2c 20 74 65 6d 70 31 2c 20 74 65  K_DOT, temp1, te
21520 6d 70 34 2c 20 30 29 3b 0a 7d 0a 20 20 20 20 20  mp4, 0);.}.     
21521 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
21522 63 61 73 65 20 31 38 33 3a 0a 7b 79 79 67 6f 74  case 183:.{yygot
21523 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73  ominor.yy172 = s
21524 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 45 78  qlite3RegisterEx
21525 70 72 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73  pr(pParse, &yyms
21526 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
21527 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
21528 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 34 3a  .      case 184:
21529 0a 7b 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b  .{.  Token *pTok
2152a 65 6e 20 3d 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  en = &yymsp[0].m
2152b 69 6e 6f 72 2e 79 79 30 3b 0a 20 20 45 78 70 72  inor.yy0;.  Expr
2152c 20 2a 70 45 78 70 72 20 3d 20 79 79 67 6f 74 6f   *pExpr = yygoto
2152d 6d 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73 71  minor.yy172 = sq
2152e 6c 69 74 65 33 45 78 70 72 28 54 4b 5f 56 41 52  lite3Expr(TK_VAR
2152f 49 41 42 4c 45 2c 20 30 2c 20 30 2c 20 70 54 6f  IABLE, 0, 0, pTo
21530 6b 65 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  ken);.  sqlite3E
21531 78 70 72 41 73 73 69 67 6e 56 61 72 4e 75 6d 62  xprAssignVarNumb
21532 65 72 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  er(pParse, pExpr
21533 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
21534 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
21535 38 35 3a 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69  85:.{.  yygotomi
21536 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73 71 6c 69  nor.yy172 = sqli
21537 74 65 33 45 78 70 72 53 65 74 43 6f 6c 6c 28 70  te3ExprSetColl(p
21538 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 32 5d  Parse, yymsp[-2]
21539 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 20 26 79  .minor.yy172, &y
2153a 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2153b 34 31 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  410);.}.        
2153c 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2153d 65 20 31 38 36 3a 0a 7b 0a 20 20 79 79 67 6f 74  e 186:.{.  yygot
2153e 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73  ominor.yy172 = s
2153f 71 6c 69 74 65 33 45 78 70 72 28 54 4b 5f 43 41  qlite3Expr(TK_CA
21540 53 54 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  ST, yymsp[-3].mi
21541 6e 6f 72 2e 79 79 31 37 32 2c 20 30 2c 20 26 79  nor.yy172, 0, &y
21542 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
21543 79 34 31 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  y410);.  sqlite3
21544 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d  ExprSpan(yygotom
21545 69 6e 6f 72 2e 79 79 31 37 32 2c 26 79 79 6d 73  inor.yy172,&yyms
21546 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-5].minor.yy0,
21547 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
21548 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  yy0);.}.        
21549 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2154a 65 20 31 38 37 3a 0a 7b 0a 20 20 69 66 28 20 79  e 187:.{.  if( y
2154b 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2154c 79 31 37 34 20 26 26 20 79 79 6d 73 70 5b 2d 31  y174 && yymsp[-1
2154d 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 34 2d 3e 6e  ].minor.yy174->n
2154e 45 78 70 72 3e 53 51 4c 49 54 45 5f 4d 41 58 5f  Expr>SQLITE_MAX_
2154f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 20 29 7b 0a  FUNCTION_ARG ){.
21550 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
21551 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f  Msg(pParse, "too
21552 20 6d 61 6e 79 20 61 72 67 75 6d 65 6e 74 73 20   many arguments 
21553 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 25 54 22 2c  on function %T",
21554 20 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f   &yymsp[-4].mino
21555 72 2e 79 79 30 29 3b 0a 20 20 7d 0a 20 20 79 79  r.yy0);.  }.  yy
21556 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20  gotominor.yy172 
21557 3d 20 73 71 6c 69 74 65 33 45 78 70 72 46 75 6e  = sqlite3ExprFun
21558 63 74 69 6f 6e 28 79 79 6d 73 70 5b 2d 31 5d 2e  ction(yymsp[-1].
21559 6d 69 6e 6f 72 2e 79 79 31 37 34 2c 20 26 79 79  minor.yy174, &yy
2155a 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
2155b 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  0);.  sqlite3Exp
2155c 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f  rSpan(yygotomino
2155d 72 2e 79 79 31 37 32 2c 26 79 79 6d 73 70 5b 2d  r.yy172,&yymsp[-
2155e 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79  4].minor.yy0,&yy
2155f 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
21560 29 3b 0a 20 20 69 66 28 20 79 79 6d 73 70 5b 2d  );.  if( yymsp[-
21561 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 20 26 26  2].minor.yy46 &&
21562 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
21563 37 32 20 29 7b 0a 20 20 20 20 79 79 67 6f 74 6f  72 ){.    yygoto
21564 6d 69 6e 6f 72 2e 79 79 31 37 32 2d 3e 66 6c 61  minor.yy172->fla
21565 67 73 20 7c 3d 20 45 50 5f 44 69 73 74 69 6e 63  gs |= EP_Distinc
21566 74 3b 0a 20 20 7d 0a 7d 0a 20 20 20 20 20 20 20  t;.  }.}.       
21567 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
21568 73 65 20 31 38 38 3a 0a 7b 0a 20 20 79 79 67 6f  se 188:.{.  yygo
21569 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20  tominor.yy172 = 
2156a 73 71 6c 69 74 65 33 45 78 70 72 46 75 6e 63 74  sqlite3ExprFunct
2156b 69 6f 6e 28 30 2c 20 26 79 79 6d 73 70 5b 2d 33  ion(0, &yymsp[-3
2156c 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20  ].minor.yy0);.  
2156d 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28  sqlite3ExprSpan(
2156e 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37  yygotominor.yy17
2156f 32 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  2,&yymsp[-3].min
21570 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d  or.yy0,&yymsp[0]
21571 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20  .minor.yy0);.}. 
21572 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
21573 20 20 20 20 63 61 73 65 20 31 38 39 3a 0a 7b 0a      case 189:.{.
21574 20 20 2f 2a 20 54 68 65 20 43 55 52 52 45 4e 54    /* The CURRENT
21575 5f 54 49 4d 45 2c 20 43 55 52 52 45 4e 54 5f 44  _TIME, CURRENT_D
21576 41 54 45 2c 20 61 6e 64 20 43 55 52 52 45 4e 54  ATE, and CURRENT
21577 5f 54 49 4d 45 53 54 41 4d 50 20 76 61 6c 75 65  _TIMESTAMP value
21578 73 20 61 72 65 0a 20 20 2a 2a 20 74 72 65 61 74  s are.  ** treat
21579 65 64 20 61 73 20 66 75 6e 63 74 69 6f 6e 73 20  ed as functions 
2157a 74 68 61 74 20 72 65 74 75 72 6e 20 63 6f 6e 73  that return cons
2157b 74 61 6e 74 73 20 2a 2f 0a 20 20 79 79 67 6f 74  tants */.  yygot
2157c 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73  ominor.yy172 = s
2157d 71 6c 69 74 65 33 45 78 70 72 46 75 6e 63 74 69  qlite3ExprFuncti
2157e 6f 6e 28 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  on(0,&yymsp[0].m
2157f 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 69 66 28  inor.yy0);.  if(
21580 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
21581 37 32 20 29 7b 0a 20 20 20 20 79 79 67 6f 74 6f  72 ){.    yygoto
21582 6d 69 6e 6f 72 2e 79 79 31 37 32 2d 3e 6f 70 20  minor.yy172->op 
21583 3d 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 3b  = TK_CONST_FUNC;
21584 20 20 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e    .    yygotomin
21585 6f 72 2e 79 79 31 37 32 2d 3e 73 70 61 6e 20 3d  or.yy172->span =
21586 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
21587 79 79 30 3b 0a 20 20 7d 0a 7d 0a 20 20 20 20 20  yy0;.  }.}.     
21588 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
21589 63 61 73 65 20 31 39 30 3a 0a 20 20 20 20 20 20  case 190:.      
2158a 63 61 73 65 20 31 39 31 3a 0a 20 20 20 20 20 20  case 191:.      
2158b 63 61 73 65 20 31 39 32 3a 0a 20 20 20 20 20 20  case 192:.      
2158c 63 61 73 65 20 31 39 33 3a 0a 20 20 20 20 20 20  case 193:.      
2158d 63 61 73 65 20 31 39 34 3a 0a 20 20 20 20 20 20  case 194:.      
2158e 63 61 73 65 20 31 39 35 3a 0a 20 20 20 20 20 20  case 195:.      
2158f 63 61 73 65 20 31 39 36 3a 0a 20 20 20 20 20 20  case 196:.      
21590 63 61 73 65 20 31 39 37 3a 0a 7b 79 79 67 6f 74  case 197:.{yygot
21591 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73  ominor.yy172 = s
21592 71 6c 69 74 65 33 45 78 70 72 28 79 79 6d 73 70  qlite3Expr(yymsp
21593 5b 2d 31 5d 2e 6d 61 6a 6f 72 2c 20 79 79 6d 73  [-1].major, yyms
21594 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37  p[-2].minor.yy17
21595 32 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  2, yymsp[0].mino
21596 72 2e 79 79 31 37 32 2c 20 30 29 3b 7d 0a 20 20  r.yy172, 0);}.  
21597 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
21598 20 20 20 63 61 73 65 20 31 39 38 3a 0a 20 20 20     case 198:.   
21599 20 20 20 63 61 73 65 20 32 30 30 3a 0a 7b 79 79     case 200:.{yy
2159a 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2e 65  gotominor.yy72.e
2159b 4f 70 65 72 61 74 6f 72 20 3d 20 79 79 6d 73 70  Operator = yymsp
2159c 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 20 79  [0].minor.yy0; y
2159d 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2e  ygotominor.yy72.
2159e 6e 6f 74 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20  not = 0;}.      
2159f 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
215a0 61 73 65 20 31 39 39 3a 0a 20 20 20 20 20 20 63  ase 199:.      c
215a1 61 73 65 20 32 30 31 3a 0a 7b 79 79 67 6f 74 6f  ase 201:.{yygoto
215a2 6d 69 6e 6f 72 2e 79 79 37 32 2e 65 4f 70 65 72  minor.yy72.eOper
215a3 61 74 6f 72 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  ator = yymsp[0].
215a4 6d 69 6e 6f 72 2e 79 79 30 3b 20 79 79 67 6f 74  minor.yy0; yygot
215a5 6f 6d 69 6e 6f 72 2e 79 79 37 32 2e 6e 6f 74 20  ominor.yy72.not 
215a6 3d 20 31 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  = 1;}.        br
215a7 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
215a8 32 30 34 3a 0a 7b 0a 20 20 45 78 70 72 4c 69 73  204:.{.  ExprLis
215a9 74 20 2a 70 4c 69 73 74 3b 0a 20 20 70 4c 69 73  t *pList;.  pLis
215aa 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  t = sqlite3ExprL
215ab 69 73 74 41 70 70 65 6e 64 28 30 2c 20 79 79 6d  istAppend(0, yym
215ac 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-1].minor.yy1
215ad 37 32 2c 20 30 29 3b 0a 20 20 70 4c 69 73 74 20  72, 0);.  pList 
215ae 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
215af 74 41 70 70 65 6e 64 28 70 4c 69 73 74 2c 20 79  tAppend(pList, y
215b0 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
215b1 79 31 37 32 2c 20 30 29 3b 0a 20 20 69 66 28 20  y172, 0);.  if( 
215b2 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
215b3 79 31 37 32 20 29 7b 0a 20 20 20 20 70 4c 69 73  y172 ){.    pLis
215b4 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  t = sqlite3ExprL
215b5 69 73 74 41 70 70 65 6e 64 28 70 4c 69 73 74 2c  istAppend(pList,
215b6 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
215b7 79 79 31 37 32 2c 20 30 29 3b 0a 20 20 7d 0a 20  yy172, 0);.  }. 
215b8 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
215b9 37 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  72 = sqlite3Expr
215ba 46 75 6e 63 74 69 6f 6e 28 70 4c 69 73 74 2c 20  Function(pList, 
215bb 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  &yymsp[-2].minor
215bc 2e 79 79 37 32 2e 65 4f 70 65 72 61 74 6f 72 29  .yy72.eOperator)
215bd 3b 0a 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 32  ;.  if( yymsp[-2
215be 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2e 6e 6f 74  ].minor.yy72.not
215bf 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79   ) yygotominor.y
215c0 79 31 37 32 20 3d 20 73 71 6c 69 74 65 33 45 78  y172 = sqlite3Ex
215c1 70 72 28 54 4b 5f 4e 4f 54 2c 20 79 79 67 6f 74  pr(TK_NOT, yygot
215c2 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 20 30 2c  ominor.yy172, 0,
215c3 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78   0);.  sqlite3Ex
215c4 70 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e  prSpan(yygotomin
215c5 6f 72 2e 79 79 31 37 32 2c 20 26 79 79 6d 73 70  or.yy172, &yymsp
215c6 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32  [-3].minor.yy172
215c7 2d 3e 73 70 61 6e 2c 20 26 79 79 6d 73 70 5b 2d  ->span, &yymsp[-
215c8 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2d 3e  1].minor.yy172->
215c9 73 70 61 6e 29 3b 0a 20 20 69 66 28 20 79 79 67  span);.  if( yyg
215ca 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20 29  otominor.yy172 )
215cb 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
215cc 37 32 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f  72->flags |= EP_
215cd 49 6e 66 69 78 46 75 6e 63 3b 0a 7d 0a 20 20 20  InfixFunc;.}.   
215ce 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
215cf 20 20 63 61 73 65 20 32 30 35 3a 0a 7b 0a 20 20    case 205:.{.  
215d0 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37  yygotominor.yy17
215d1 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28  2 = sqlite3Expr(
215d2 79 79 6d 73 70 5b 30 5d 2e 6d 61 6a 6f 72 2c 20  yymsp[0].major, 
215d3 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
215d4 79 79 31 37 32 2c 20 30 2c 20 30 29 3b 0a 20 20  yy172, 0, 0);.  
215d5 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28  sqlite3ExprSpan(
215d6 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37  yygotominor.yy17
215d7 32 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  2,&yymsp[-1].min
215d8 6f 72 2e 79 79 31 37 32 2d 3e 73 70 61 6e 2c 26  or.yy172->span,&
215d9 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
215da 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  y0);.}.        b
215db 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
215dc 20 32 30 36 3a 0a 7b 0a 20 20 79 79 67 6f 74 6f   206:.{.  yygoto
215dd 6d 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73 71  minor.yy172 = sq
215de 6c 69 74 65 33 45 78 70 72 28 54 4b 5f 49 53 4e  lite3Expr(TK_ISN
215df 55 4c 4c 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d  ULL, yymsp[-2].m
215e0 69 6e 6f 72 2e 79 79 31 37 32 2c 20 30 2c 20 30  inor.yy172, 0, 0
215e1 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
215e2 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72  Span(yygotominor
215e3 2e 79 79 31 37 32 2c 26 79 79 6d 73 70 5b 2d 32  .yy172,&yymsp[-2
215e4 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2d 3e 73  ].minor.yy172->s
215e5 70 61 6e 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  pan,&yymsp[0].mi
215e6 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20  nor.yy0);.}.    
215e7 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
215e8 20 63 61 73 65 20 32 30 37 3a 0a 7b 0a 20 20 79   case 207:.{.  y
215e9 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32  ygotominor.yy172
215ea 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 54   = sqlite3Expr(T
215eb 4b 5f 4e 4f 54 4e 55 4c 4c 2c 20 79 79 6d 73 70  K_NOTNULL, yymsp
215ec 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32  [-2].minor.yy172
215ed 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74  , 0, 0);.  sqlit
215ee 65 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74  e3ExprSpan(yygot
215ef 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 26 79 79  ominor.yy172,&yy
215f0 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
215f1 31 37 32 2d 3e 73 70 61 6e 2c 26 79 79 6d 73 70  172->span,&yymsp
215f2 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  [0].minor.yy0);.
215f3 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
215f4 0a 20 20 20 20 20 20 63 61 73 65 20 32 30 38 3a  .      case 208:
215f5 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .{.  yygotominor
215f6 2e 79 79 31 37 32 20 3d 20 73 71 6c 69 74 65 33  .yy172 = sqlite3
215f7 45 78 70 72 28 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c  Expr(TK_NOTNULL,
215f8 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72   yymsp[-3].minor
215f9 2e 79 79 31 37 32 2c 20 30 2c 20 30 29 3b 0a 20  .yy172, 0, 0);. 
215fa 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e   sqlite3ExprSpan
215fb 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  (yygotominor.yy1
215fc 37 32 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  72,&yymsp[-3].mi
215fd 6e 6f 72 2e 79 79 31 37 32 2d 3e 73 70 61 6e 2c  nor.yy172->span,
215fe 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
215ff 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  yy0);.}.        
21600 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
21601 65 20 32 30 39 3a 0a 7b 0a 20 20 79 79 67 6f 74  e 209:.{.  yygot
21602 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73  ominor.yy172 = s
21603 71 6c 69 74 65 33 45 78 70 72 28 79 79 6d 73 70  qlite3Expr(yymsp
21604 5b 2d 31 5d 2e 6d 61 6a 6f 72 2c 20 79 79 6d 73  [-1].major, yyms
21605 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32  p[0].minor.yy172
21606 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74  , 0, 0);.  sqlit
21607 65 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74  e3ExprSpan(yygot
21608 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 26 79 79  ominor.yy172,&yy
21609 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
2160a 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  0,&yymsp[0].mino
2160b 72 2e 79 79 31 37 32 2d 3e 73 70 61 6e 29 3b 0a  r.yy172->span);.
2160c 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2160d 0a 20 20 20 20 20 20 63 61 73 65 20 32 31 30 3a  .      case 210:
2160e 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .{.  yygotominor
2160f 2e 79 79 31 37 32 20 3d 20 73 71 6c 69 74 65 33  .yy172 = sqlite3
21610 45 78 70 72 28 54 4b 5f 55 4d 49 4e 55 53 2c 20  Expr(TK_UMINUS, 
21611 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
21612 79 31 37 32 2c 20 30 2c 20 30 29 3b 0a 20 20 73  y172, 0, 0);.  s
21613 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28 79  qlite3ExprSpan(y
21614 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32  ygotominor.yy172
21615 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  ,&yymsp[-1].mino
21616 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e  r.yy0,&yymsp[0].
21617 6d 69 6e 6f 72 2e 79 79 31 37 32 2d 3e 73 70 61  minor.yy172->spa
21618 6e 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  n);.}.        br
21619 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2161a 32 31 31 3a 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d  211:.{.  yygotom
2161b 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73 71 6c  inor.yy172 = sql
2161c 69 74 65 33 45 78 70 72 28 54 4b 5f 55 50 4c 55  ite3Expr(TK_UPLU
2161d 53 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  S, yymsp[0].mino
2161e 72 2e 79 79 31 37 32 2c 20 30 2c 20 30 29 3b 0a  r.yy172, 0, 0);.
2161f 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61    sqlite3ExprSpa
21620 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  n(yygotominor.yy
21621 31 37 32 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d  172,&yymsp[-1].m
21622 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b  inor.yy0,&yymsp[
21623 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2d 3e  0].minor.yy172->
21624 73 70 61 6e 29 3b 0a 7d 0a 20 20 20 20 20 20 20  span);.}.       
21625 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
21626 73 65 20 32 31 34 3a 0a 7b 0a 20 20 45 78 70 72  se 214:.{.  Expr
21627 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 73 71  List *pList = sq
21628 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
21629 65 6e 64 28 30 2c 20 79 79 6d 73 70 5b 2d 32 5d  end(0, yymsp[-2]
2162a 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 20 30 29  .minor.yy172, 0)
2162b 3b 0a 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69  ;.  pList = sqli
2162c 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
2162d 64 28 70 4c 69 73 74 2c 20 79 79 6d 73 70 5b 30  d(pList, yymsp[0
2162e 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 20 30  ].minor.yy172, 0
2162f 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  );.  yygotominor
21630 2e 79 79 31 37 32 20 3d 20 73 71 6c 69 74 65 33  .yy172 = sqlite3
21631 45 78 70 72 28 54 4b 5f 42 45 54 57 45 45 4e 2c  Expr(TK_BETWEEN,
21632 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72   yymsp[-4].minor
21633 2e 79 79 31 37 32 2c 20 30 2c 20 30 29 3b 0a 20  .yy172, 0, 0);. 
21634 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72   if( yygotominor
21635 2e 79 79 31 37 32 20 29 7b 0a 20 20 20 20 79 79  .yy172 ){.    yy
21636 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 2d  gotominor.yy172-
21637 3e 70 4c 69 73 74 20 3d 20 70 4c 69 73 74 3b 0a  >pList = pList;.
21638 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
21639 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
2163a 74 65 28 70 4c 69 73 74 29 3b 0a 20 20 7d 20 0a  te(pList);.  } .
2163b 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 33 5d 2e    if( yymsp[-3].
2163c 6d 69 6e 6f 72 2e 79 79 34 36 20 29 20 79 79 67  minor.yy46 ) yyg
2163d 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20 3d  otominor.yy172 =
2163e 20 73 71 6c 69 74 65 33 45 78 70 72 28 54 4b 5f   sqlite3Expr(TK_
2163f 4e 4f 54 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  NOT, yygotominor
21640 2e 79 79 31 37 32 2c 20 30 2c 20 30 29 3b 0a 20  .yy172, 0, 0);. 
21641 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e   sqlite3ExprSpan
21642 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  (yygotominor.yy1
21643 37 32 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  72,&yymsp[-4].mi
21644 6e 6f 72 2e 79 79 31 37 32 2d 3e 73 70 61 6e 2c  nor.yy172->span,
21645 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
21646 79 79 31 37 32 2d 3e 73 70 61 6e 29 3b 0a 7d 0a  yy172->span);.}.
21647 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
21648 20 20 20 20 20 63 61 73 65 20 32 31 37 3a 0a 7b       case 217:.{
21649 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .    yygotominor
2164a 2e 79 79 31 37 32 20 3d 20 73 71 6c 69 74 65 33  .yy172 = sqlite3
2164b 45 78 70 72 28 54 4b 5f 49 4e 2c 20 79 79 6d 73  Expr(TK_IN, yyms
2164c 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37  p[-4].minor.yy17
2164d 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66  2, 0, 0);.    if
2164e 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ( yygotominor.yy
2164f 31 37 32 20 29 7b 0a 20 20 20 20 20 20 79 79 67  172 ){.      yyg
21650 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 2d 3e  otominor.yy172->
21651 70 4c 69 73 74 20 3d 20 79 79 6d 73 70 5b 2d 31  pList = yymsp[-1
21652 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 34 3b 0a 20  ].minor.yy174;. 
21653 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
21654 53 65 74 48 65 69 67 68 74 28 79 79 67 6f 74 6f  SetHeight(yygoto
21655 6d 69 6e 6f 72 2e 79 79 31 37 32 29 3b 0a 20 20  minor.yy172);.  
21656 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
21657 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65  qlite3ExprListDe
21658 6c 65 74 65 28 79 79 6d 73 70 5b 2d 31 5d 2e 6d  lete(yymsp[-1].m
21659 69 6e 6f 72 2e 79 79 31 37 34 29 3b 0a 20 20 20  inor.yy174);.   
2165a 20 7d 0a 20 20 20 20 69 66 28 20 79 79 6d 73 70   }.    if( yymsp
2165b 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 20  [-3].minor.yy46 
2165c 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ) yygotominor.yy
2165d 31 37 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70  172 = sqlite3Exp
2165e 72 28 54 4b 5f 4e 4f 54 2c 20 79 79 67 6f 74 6f  r(TK_NOT, yygoto
2165f 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 20 30 2c 20  minor.yy172, 0, 
21660 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  0);.    sqlite3E
21661 78 70 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69  xprSpan(yygotomi
21662 6e 6f 72 2e 79 79 31 37 32 2c 26 79 79 6d 73 70  nor.yy172,&yymsp
21663 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32  [-4].minor.yy172
21664 2d 3e 73 70 61 6e 2c 26 79 79 6d 73 70 5b 30 5d  ->span,&yymsp[0]
21665 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 7d  .minor.yy0);.  }
21666 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
21667 20 20 20 20 20 20 63 61 73 65 20 32 31 38 3a 0a        case 218:.
21668 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  {.    yygotomino
21669 72 2e 79 79 31 37 32 20 3d 20 73 71 6c 69 74 65  r.yy172 = sqlite
2166a 33 45 78 70 72 28 54 4b 5f 53 45 4c 45 43 54 2c  3Expr(TK_SELECT,
2166b 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69   0, 0, 0);.    i
2166c 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  f( yygotominor.y
2166d 79 31 37 32 20 29 7b 0a 20 20 20 20 20 20 79 79  y172 ){.      yy
2166e 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 2d  gotominor.yy172-
2166f 3e 70 53 65 6c 65 63 74 20 3d 20 79 79 6d 73 70  >pSelect = yymsp
21670 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 32 31 39  [-1].minor.yy219
21671 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
21672 78 70 72 53 65 74 48 65 69 67 68 74 28 79 79 67  xprSetHeight(yyg
21673 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 29 3b  otominor.yy172);
21674 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
21675 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
21676 65 6c 65 74 65 28 79 79 6d 73 70 5b 2d 31 5d 2e  elete(yymsp[-1].
21677 6d 69 6e 6f 72 2e 79 79 32 31 39 29 3b 0a 20 20  minor.yy219);.  
21678 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 45    }.    sqlite3E
21679 78 70 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69  xprSpan(yygotomi
2167a 6e 6f 72 2e 79 79 31 37 32 2c 26 79 79 6d 73 70  nor.yy172,&yymsp
2167b 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26  [-2].minor.yy0,&
2167c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2167d 79 30 29 3b 0a 20 20 7d 0a 20 20 20 20 20 20 20  y0);.  }.       
2167e 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2167f 73 65 20 32 31 39 3a 0a 7b 0a 20 20 20 20 79 79  se 219:.{.    yy
21680 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20  gotominor.yy172 
21681 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 54 4b  = sqlite3Expr(TK
21682 5f 49 4e 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d  _IN, yymsp[-4].m
21683 69 6e 6f 72 2e 79 79 31 37 32 2c 20 30 2c 20 30  inor.yy172, 0, 0
21684 29 3b 0a 20 20 20 20 69 66 28 20 79 79 67 6f 74  );.    if( yygot
21685 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20 29 7b 0a  ominor.yy172 ){.
21686 20 20 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f        yygotomino
21687 72 2e 79 79 31 37 32 2d 3e 70 53 65 6c 65 63 74  r.yy172->pSelect
21688 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e   = yymsp[-1].min
21689 6f 72 2e 79 79 32 31 39 3b 0a 20 20 20 20 20 20  or.yy219;.      
2168a 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65  sqlite3ExprSetHe
2168b 69 67 68 74 28 79 79 67 6f 74 6f 6d 69 6e 6f 72  ight(yygotominor
2168c 2e 79 79 31 37 32 29 3b 0a 20 20 20 20 7d 65 6c  .yy172);.    }el
2168d 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
2168e 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 79 79  3SelectDelete(yy
2168f 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
21690 32 31 39 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  219);.    }.    
21691 69 66 28 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  if( yymsp[-3].mi
21692 6e 6f 72 2e 79 79 34 36 20 29 20 79 79 67 6f 74  nor.yy46 ) yygot
21693 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73  ominor.yy172 = s
21694 71 6c 69 74 65 33 45 78 70 72 28 54 4b 5f 4e 4f  qlite3Expr(TK_NO
21695 54 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  T, yygotominor.y
21696 79 31 37 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20  y172, 0, 0);.   
21697 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e   sqlite3ExprSpan
21698 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  (yygotominor.yy1
21699 37 32 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  72,&yymsp[-4].mi
2169a 6e 6f 72 2e 79 79 31 37 32 2d 3e 73 70 61 6e 2c  nor.yy172->span,
2169b 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
2169c 79 79 30 29 3b 0a 20 20 7d 0a 20 20 20 20 20 20  yy0);.  }.      
2169d 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2169e 61 73 65 20 32 32 30 3a 0a 7b 0a 20 20 20 20 53  ase 220:.{.    S
2169f 72 63 4c 69 73 74 20 2a 70 53 72 63 20 3d 20 73  rcList *pSrc = s
216a0 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70  qlite3SrcListApp
216a1 65 6e 64 28 30 2c 26 79 79 6d 73 70 5b 2d 31 5d  end(0,&yymsp[-1]
216a2 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 26 79 79  .minor.yy410,&yy
216a3 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34  msp[0].minor.yy4
216a4 31 30 29 3b 0a 20 20 20 20 79 79 67 6f 74 6f 6d  10);.    yygotom
216a5 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73 71 6c  inor.yy172 = sql
216a6 69 74 65 33 45 78 70 72 28 54 4b 5f 49 4e 2c 20  ite3Expr(TK_IN, 
216a7 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
216a8 79 79 31 37 32 2c 20 30 2c 20 30 29 3b 0a 20 20  yy172, 0, 0);.  
216a9 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f    if( yygotomino
216aa 72 2e 79 79 31 37 32 20 29 7b 0a 20 20 20 20 20  r.yy172 ){.     
216ab 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
216ac 37 32 2d 3e 70 53 65 6c 65 63 74 20 3d 20 73 71  72->pSelect = sq
216ad 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28 30  lite3SelectNew(0
216ae 2c 70 53 72 63 2c 30 2c 30 2c 30 2c 30 2c 30 2c  ,pSrc,0,0,0,0,0,
216af 30 2c 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  0,0);.      sqli
216b0 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74  te3ExprSetHeight
216b1 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  (yygotominor.yy1
216b2 37 32 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  72);.    }else{.
216b3 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 72 63        sqlite3Src
216b4 4c 69 73 74 44 65 6c 65 74 65 28 70 53 72 63 29  ListDelete(pSrc)
216b5 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
216b6 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
216b7 79 79 34 36 20 29 20 79 79 67 6f 74 6f 6d 69 6e  yy46 ) yygotomin
216b8 6f 72 2e 79 79 31 37 32 20 3d 20 73 71 6c 69 74  or.yy172 = sqlit
216b9 65 33 45 78 70 72 28 54 4b 5f 4e 4f 54 2c 20 79  e3Expr(TK_NOT, y
216ba 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32  ygotominor.yy172
216bb 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
216bc 69 74 65 33 45 78 70 72 53 70 61 6e 28 79 79 67  ite3ExprSpan(yyg
216bd 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 26  otominor.yy172,&
216be 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
216bf 79 79 31 37 32 2d 3e 73 70 61 6e 2c 79 79 6d 73  yy172->span,yyms
216c0 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30  p[0].minor.yy410
216c1 2e 7a 3f 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  .z?&yymsp[0].min
216c2 6f 72 2e 79 79 34 31 30 3a 26 79 79 6d 73 70 5b  or.yy410:&yymsp[
216c3 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 29  -1].minor.yy410)
216c4 3b 0a 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72  ;.  }.        br
216c5 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
216c6 32 32 31 3a 0a 7b 0a 20 20 20 20 45 78 70 72 20  221:.{.    Expr 
216c7 2a 70 20 3d 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  *p = yygotominor
216c8 2e 79 79 31 37 32 20 3d 20 73 71 6c 69 74 65 33  .yy172 = sqlite3
216c9 45 78 70 72 28 54 4b 5f 45 58 49 53 54 53 2c 20  Expr(TK_EXISTS, 
216ca 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66  0, 0, 0);.    if
216cb 28 20 70 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  ( p ){.      p->
216cc 70 53 65 6c 65 63 74 20 3d 20 79 79 6d 73 70 5b  pSelect = yymsp[
216cd 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 32 31 39 3b  -1].minor.yy219;
216ce 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
216cf 70 72 53 70 61 6e 28 70 2c 26 79 79 6d 73 70 5b  prSpan(p,&yymsp[
216d0 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -3].minor.yy0,&y
216d1 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
216d2 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  0);.      sqlite
216d3 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28 79  3ExprSetHeight(y
216d4 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32  ygotominor.yy172
216d5 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
216d6 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
216d7 74 44 65 6c 65 74 65 28 79 79 6d 73 70 5b 2d 31  tDelete(yymsp[-1
216d8 5d 2e 6d 69 6e 6f 72 2e 79 79 32 31 39 29 3b 0a  ].minor.yy219);.
216d9 20 20 20 20 7d 0a 20 20 7d 0a 20 20 20 20 20 20      }.  }.      
216da 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
216db 61 73 65 20 32 32 32 3a 0a 7b 0a 20 20 79 79 67  ase 222:.{.  yyg
216dc 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20 3d  otominor.yy172 =
216dd 20 73 71 6c 69 74 65 33 45 78 70 72 28 54 4b 5f   sqlite3Expr(TK_
216de 43 41 53 45 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e  CASE, yymsp[-3].
216df 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 20 79 79 6d  minor.yy172, yym
216e0 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-1].minor.yy1
216e1 37 32 2c 20 30 29 3b 0a 20 20 69 66 28 20 79 79  72, 0);.  if( yy
216e2 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20  gotominor.yy172 
216e3 29 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e  ){.    yygotomin
216e4 6f 72 2e 79 79 31 37 32 2d 3e 70 4c 69 73 74 20  or.yy172->pList 
216e5 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  = yymsp[-2].mino
216e6 72 2e 79 79 31 37 34 3b 0a 20 20 20 20 73 71 6c  r.yy174;.    sql
216e7 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68  ite3ExprSetHeigh
216e8 74 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  t(yygotominor.yy
216e9 31 37 32 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  172);.  }else{. 
216ea 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69     sqlite3ExprLi
216eb 73 74 44 65 6c 65 74 65 28 79 79 6d 73 70 5b 2d  stDelete(yymsp[-
216ec 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 34 29 3b  2].minor.yy174);
216ed 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 78  .  }.  sqlite3Ex
216ee 70 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e  prSpan(yygotomin
216ef 6f 72 2e 79 79 31 37 32 2c 20 26 79 79 6d 73 70  or.yy172, &yymsp
216f0 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20  [-4].minor.yy0, 
216f1 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
216f2 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  yy0);.}.        
216f3 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
216f4 65 20 32 32 33 3a 0a 7b 0a 20 20 79 79 67 6f 74  e 223:.{.  yygot
216f5 6f 6d 69 6e 6f 72 2e 79 79 31 37 34 20 3d 20 73  ominor.yy174 = s
216f6 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
216f7 70 65 6e 64 28 79 79 6d 73 70 5b 2d 34 5d 2e 6d  pend(yymsp[-4].m
216f8 69 6e 6f 72 2e 79 79 31 37 34 2c 20 79 79 6d 73  inor.yy174, yyms
216f9 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37  p[-2].minor.yy17
216fa 32 2c 20 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d  2, 0);.  yygotom
216fb 69 6e 6f 72 2e 79 79 31 37 34 20 3d 20 73 71 6c  inor.yy174 = sql
216fc 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
216fd 6e 64 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  nd(yygotominor.y
216fe 79 31 37 34 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d  y174, yymsp[0].m
216ff 69 6e 6f 72 2e 79 79 31 37 32 2c 20 30 29 3b 0a  inor.yy172, 0);.
21700 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
21701 0a 20 20 20 20 20 20 63 61 73 65 20 32 32 34 3a  .      case 224:
21702 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .{.  yygotominor
21703 2e 79 79 31 37 34 20 3d 20 73 71 6c 69 74 65 33  .yy174 = sqlite3
21704 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 30  ExprListAppend(0
21705 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  , yymsp[-2].mino
21706 72 2e 79 79 31 37 32 2c 20 30 29 3b 0a 20 20 79  r.yy172, 0);.  y
21707 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 34  ygotominor.yy174
21708 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
21709 73 74 41 70 70 65 6e 64 28 79 79 67 6f 74 6f 6d  stAppend(yygotom
2170a 69 6e 6f 72 2e 79 79 31 37 34 2c 20 79 79 6d 73  inor.yy174, yyms
2170b 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32  p[0].minor.yy172
2170c 2c 20 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  , 0);.}.        
2170d 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2170e 65 20 32 33 33 3a 0a 7b 0a 20 20 73 71 6c 69 74  e 233:.{.  sqlit
2170f 65 33 43 72 65 61 74 65 49 6e 64 65 78 28 70 50  e3CreateIndex(pP
21710 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d 36 5d  arse, &yymsp[-6]
21711 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 20 26 79  .minor.yy410, &y
21712 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-5].minor.y
21713 79 34 31 30 2c 20 73 71 6c 69 74 65 33 53 72 63  y410, sqlite3Src
21714 4c 69 73 74 41 70 70 65 6e 64 28 30 2c 26 79 79  ListAppend(0,&yy
21715 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
21716 34 31 30 2c 30 29 2c 20 79 79 6d 73 70 5b 2d 31  410,0), yymsp[-1
21717 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 34 2c 20 79  ].minor.yy174, y
21718 79 6d 73 70 5b 2d 39 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-9].minor.y
21719 79 34 36 2c 0a 20 20 20 20 20 20 20 20 20 20 20  y46,.           
2171a 20 20 20 20 20 20 20 20 20 20 20 26 79 79 6d 73             &yyms
2171b 70 5b 2d 31 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  p[-10].minor.yy0
2171c 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  , &yymsp[0].mino
2171d 72 2e 79 79 30 2c 20 53 51 4c 49 54 45 5f 53 4f  r.yy0, SQLITE_SO
2171e 5f 41 53 43 2c 20 79 79 6d 73 70 5b 2d 37 5d 2e  _ASC, yymsp[-7].
2171f 6d 69 6e 6f 72 2e 79 79 34 36 29 3b 0a 7d 0a 20  minor.yy46);.}. 
21720 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
21721 20 20 20 20 63 61 73 65 20 32 33 34 3a 0a 20 20      case 234:.  
21722 20 20 20 20 63 61 73 65 20 32 38 31 3a 0a 7b 79      case 281:.{y
21723 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 36 20  ygotominor.yy46 
21724 3d 20 4f 45 5f 41 62 6f 72 74 3b 7d 0a 20 20 20  = OE_Abort;}.   
21725 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
21726 20 20 63 61 73 65 20 32 33 35 3a 0a 7b 79 79 67    case 235:.{yyg
21727 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 36 20 3d 20  otominor.yy46 = 
21728 4f 45 5f 4e 6f 6e 65 3b 7d 0a 20 20 20 20 20 20  OE_None;}.      
21729 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2172a 61 73 65 20 32 33 38 3a 0a 7b 0a 20 20 45 78 70  ase 238:.{.  Exp
2172b 72 20 2a 70 20 3d 20 30 3b 0a 20 20 69 66 28 20  r *p = 0;.  if( 
2172c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2172d 79 79 34 31 30 2e 6e 3e 30 20 29 7b 0a 20 20 20  yy410.n>0 ){.   
2172e 20 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72   p = sqlite3Expr
2172f 28 54 4b 5f 43 4f 4c 55 4d 4e 2c 20 30 2c 20 30  (TK_COLUMN, 0, 0
21730 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 20  , 0);.    if( p 
21731 29 20 70 2d 3e 70 43 6f 6c 6c 20 3d 20 73 71 6c  ) p->pColl = sql
21732 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65  ite3LocateCollSe
21733 71 28 70 50 61 72 73 65 2c 20 28 63 68 61 72 2a  q(pParse, (char*
21734 29 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  )yymsp[-1].minor
21735 2e 79 79 34 31 30 2e 7a 2c 20 79 79 6d 73 70 5b  .yy410.z, yymsp[
21736 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2e  -1].minor.yy410.
21737 6e 29 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f  n);.  }.  yygoto
21738 6d 69 6e 6f 72 2e 79 79 31 37 34 20 3d 20 73 71  minor.yy174 = sq
21739 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
2173a 65 6e 64 28 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  end(yymsp[-4].mi
2173b 6e 6f 72 2e 79 79 31 37 34 2c 20 70 2c 20 26 79  nor.yy174, p, &y
2173c 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
2173d 79 34 31 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  y410);.  sqlite3
2173e 45 78 70 72 4c 69 73 74 43 68 65 63 6b 4c 65 6e  ExprListCheckLen
2173f 67 74 68 28 70 50 61 72 73 65 2c 20 79 79 67 6f  gth(pParse, yygo
21740 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 34 2c 20 53  tominor.yy174, S
21741 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e  QLITE_MAX_COLUMN
21742 2c 20 22 69 6e 64 65 78 22 29 3b 0a 20 20 69 66  , "index");.  if
21743 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ( yygotominor.yy
21744 31 37 34 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f  174 ) yygotomino
21745 72 2e 79 79 31 37 34 2d 3e 61 5b 79 79 67 6f 74  r.yy174->a[yygot
21746 6f 6d 69 6e 6f 72 2e 79 79 31 37 34 2d 3e 6e 45  ominor.yy174->nE
21747 78 70 72 2d 31 5d 2e 73 6f 72 74 4f 72 64 65 72  xpr-1].sortOrder
21748 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
21749 72 2e 79 79 34 36 3b 0a 7d 0a 20 20 20 20 20 20  r.yy46;.}.      
2174a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2174b 61 73 65 20 32 33 39 3a 0a 7b 0a 20 20 45 78 70  ase 239:.{.  Exp
2174c 72 20 2a 70 20 3d 20 30 3b 0a 20 20 69 66 28 20  r *p = 0;.  if( 
2174d 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2174e 79 79 34 31 30 2e 6e 3e 30 20 29 7b 0a 20 20 20  yy410.n>0 ){.   
2174f 20 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72   p = sqlite3Expr
21750 28 54 4b 5f 43 4f 4c 55 4d 4e 2c 20 30 2c 20 30  (TK_COLUMN, 0, 0
21751 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 20  , 0);.    if( p 
21752 29 20 70 2d 3e 70 43 6f 6c 6c 20 3d 20 73 71 6c  ) p->pColl = sql
21753 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65  ite3LocateCollSe
21754 71 28 70 50 61 72 73 65 2c 20 28 63 68 61 72 2a  q(pParse, (char*
21755 29 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  )yymsp[-1].minor
21756 2e 79 79 34 31 30 2e 7a 2c 20 79 79 6d 73 70 5b  .yy410.z, yymsp[
21757 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2e  -1].minor.yy410.
21758 6e 29 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f  n);.  }.  yygoto
21759 6d 69 6e 6f 72 2e 79 79 31 37 34 20 3d 20 73 71  minor.yy174 = sq
2175a 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
2175b 65 6e 64 28 30 2c 20 70 2c 20 26 79 79 6d 73 70  end(0, p, &yymsp
2175c 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30  [-2].minor.yy410
2175d 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
2175e 4c 69 73 74 43 68 65 63 6b 4c 65 6e 67 74 68 28  ListCheckLength(
2175f 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69  pParse, yygotomi
21760 6e 6f 72 2e 79 79 31 37 34 2c 20 53 51 4c 49 54  nor.yy174, SQLIT
21761 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 2c 20 22 69  E_MAX_COLUMN, "i
21762 6e 64 65 78 22 29 3b 0a 20 20 69 66 28 20 79 79  ndex");.  if( yy
21763 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 34 20  gotominor.yy174 
21764 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ) yygotominor.yy
21765 31 37 34 2d 3e 61 5b 79 79 67 6f 74 6f 6d 69 6e  174->a[yygotomin
21766 6f 72 2e 79 79 31 37 34 2d 3e 6e 45 78 70 72 2d  or.yy174->nExpr-
21767 31 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20 79  1].sortOrder = y
21768 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
21769 34 36 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  46;.}.        br
2176a 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2176b 32 34 31 3a 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  241:.{yygotomino
2176c 72 2e 79 79 34 31 30 2e 7a 20 3d 20 30 3b 20 79  r.yy410.z = 0; y
2176d 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 30  ygotominor.yy410
2176e 2e 6e 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20  .n = 0;}.       
2176f 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
21770 73 65 20 32 34 33 3a 0a 7b 73 71 6c 69 74 65 33  se 243:.{sqlite3
21771 44 72 6f 70 49 6e 64 65 78 28 70 50 61 72 73 65  DropIndex(pParse
21772 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  , yymsp[0].minor
21773 2e 79 79 33 37 33 2c 20 79 79 6d 73 70 5b 2d 31  .yy373, yymsp[-1
21774 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 29 3b 7d 0a  ].minor.yy46);}.
21775 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
21776 20 20 20 20 20 63 61 73 65 20 32 34 34 3a 0a 20       case 244:. 
21777 20 20 20 20 20 63 61 73 65 20 32 34 35 3a 0a 7b       case 245:.{
21778 73 71 6c 69 74 65 33 56 61 63 75 75 6d 28 70 50  sqlite3Vacuum(pP
21779 61 72 73 65 29 3b 7d 0a 20 20 20 20 20 20 20 20  arse);}.        
2177a 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2177b 65 20 32 34 36 3a 0a 7b 73 71 6c 69 74 65 33 50  e 246:.{sqlite3P
2177c 72 61 67 6d 61 28 70 50 61 72 73 65 2c 26 79 79  ragma(pParse,&yy
2177d 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
2177e 34 31 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d  410,&yymsp[-2].m
2177f 69 6e 6f 72 2e 79 79 34 31 30 2c 26 79 79 6d 73  inor.yy410,&yyms
21780 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30  p[0].minor.yy410
21781 2c 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  ,0);}.        br
21782 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
21783 32 34 37 3a 0a 7b 73 71 6c 69 74 65 33 50 72 61  247:.{sqlite3Pra
21784 67 6d 61 28 70 50 61 72 73 65 2c 26 79 79 6d 73  gma(pParse,&yyms
21785 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31  p[-3].minor.yy41
21786 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  0,&yymsp[-2].min
21787 6f 72 2e 79 79 34 31 30 2c 26 79 79 6d 73 70 5b  or.yy410,&yymsp[
21788 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 29 3b  0].minor.yy0,0);
21789 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2178a 0a 20 20 20 20 20 20 63 61 73 65 20 32 34 38 3a  .      case 248:
2178b 0a 7b 0a 20 20 73 71 6c 69 74 65 33 50 72 61 67  .{.  sqlite3Prag
2178c 6d 61 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70  ma(pParse,&yymsp
2178d 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30  [-3].minor.yy410
2178e 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  ,&yymsp[-2].mino
2178f 72 2e 79 79 34 31 30 2c 26 79 79 6d 73 70 5b 30  r.yy410,&yymsp[0
21790 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 31 29  ].minor.yy410,1)
21791 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
21792 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 34  k;.      case 24
21793 39 3a 0a 7b 73 71 6c 69 74 65 33 50 72 61 67 6d  9:.{sqlite3Pragm
21794 61 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b  a(pParse,&yymsp[
21795 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c  -4].minor.yy410,
21796 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72  &yymsp[-3].minor
21797 2e 79 79 34 31 30 2c 26 79 79 6d 73 70 5b 2d 31  .yy410,&yymsp[-1
21798 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 30 29  ].minor.yy410,0)
21799 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
2179a 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 35 30  ;.      case 250
2179b 3a 0a 7b 73 71 6c 69 74 65 33 50 72 61 67 6d 61  :.{sqlite3Pragma
2179c 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d  (pParse,&yymsp[-
2179d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 26  1].minor.yy410,&
2179e 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2179f 79 34 31 30 2c 30 2c 30 29 3b 7d 0a 20 20 20 20  y410,0,0);}.    
217a0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
217a1 20 63 61 73 65 20 32 35 38 3a 0a 7b 0a 20 20 54   case 258:.{.  T
217a2 6f 6b 65 6e 20 61 6c 6c 3b 0a 20 20 61 6c 6c 2e  oken all;.  all.
217a3 7a 20 3d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  z = yymsp[-3].mi
217a4 6e 6f 72 2e 79 79 34 31 30 2e 7a 3b 0a 20 20 61  nor.yy410.z;.  a
217a5 6c 6c 2e 6e 20 3d 20 28 79 79 6d 73 70 5b 30 5d  ll.n = (yymsp[0]
217a6 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20 2d 20 79  .minor.yy0.z - y
217a7 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
217a8 79 34 31 30 2e 7a 29 20 2b 20 79 79 6d 73 70 5b  y410.z) + yymsp[
217a9 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3b 0a  0].minor.yy0.n;.
217aa 20 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68 54    sqlite3FinishT
217ab 72 69 67 67 65 72 28 70 50 61 72 73 65 2c 20 79  rigger(pParse, y
217ac 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
217ad 79 32 34 33 2c 20 26 61 6c 6c 29 3b 0a 7d 0a 20  y243, &all);.}. 
217ae 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
217af 20 20 20 20 63 61 73 65 20 32 35 39 3a 0a 7b 0a      case 259:.{.
217b0 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72    sqlite3BeginTr
217b1 69 67 67 65 72 28 70 50 61 72 73 65 2c 20 26 79  igger(pParse, &y
217b2 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-7].minor.y
217b3 79 34 31 30 2c 20 26 79 79 6d 73 70 5b 2d 36 5d  y410, &yymsp[-6]
217b4 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 20 79 79  .minor.yy410, yy
217b5 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-5].minor.yy
217b6 34 36 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  46, yymsp[-4].mi
217b7 6e 6f 72 2e 79 79 33 37 30 2e 61 2c 20 79 79 6d  nor.yy370.a, yym
217b8 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-4].minor.yy3
217b9 37 30 2e 62 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e  70.b, yymsp[-2].
217ba 6d 69 6e 6f 72 2e 79 79 33 37 33 2c 20 79 79 6d  minor.yy373, yym
217bb 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37  sp[0].minor.yy17
217bc 32 2c 20 79 79 6d 73 70 5b 2d 31 30 5d 2e 6d 69  2, yymsp[-10].mi
217bd 6e 6f 72 2e 79 79 34 36 2c 20 79 79 6d 73 70 5b  nor.yy46, yymsp[
217be 2d 38 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 29 3b  -8].minor.yy46);
217bf 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
217c0 79 34 31 30 20 3d 20 28 79 79 6d 73 70 5b 2d 36  y410 = (yymsp[-6
217c1 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 6e 3d  ].minor.yy410.n=
217c2 3d 30 3f 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e  =0?yymsp[-7].min
217c3 6f 72 2e 79 79 34 31 30 3a 79 79 6d 73 70 5b 2d  or.yy410:yymsp[-
217c4 36 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 29 3b  6].minor.yy410);
217c5 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
217c6 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 36 30  ;.      case 260
217c7 3a 0a 20 20 20 20 20 20 63 61 73 65 20 32 36 33  :.      case 263
217c8 3a 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  :.{ yygotominor.
217c9 79 79 34 36 20 3d 20 54 4b 5f 42 45 46 4f 52 45  yy46 = TK_BEFORE
217ca 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ; }.        brea
217cb 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 36  k;.      case 26
217cc 31 3a 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  1:.{ yygotominor
217cd 2e 79 79 34 36 20 3d 20 54 4b 5f 41 46 54 45 52  .yy46 = TK_AFTER
217ce 3b 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  ;  }.        bre
217cf 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
217d0 36 32 3a 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f  62:.{ yygotomino
217d1 72 2e 79 79 34 36 20 3d 20 54 4b 5f 49 4e 53 54  r.yy46 = TK_INST
217d2 45 41 44 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  EAD;}.        br
217d3 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
217d4 32 36 34 3a 0a 20 20 20 20 20 20 63 61 73 65 20  264:.      case 
217d5 32 36 35 3a 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  265:.{yygotomino
217d6 72 2e 79 79 33 37 30 2e 61 20 3d 20 79 79 6d 73  r.yy370.a = yyms
217d7 70 5b 30 5d 2e 6d 61 6a 6f 72 3b 20 79 79 67 6f  p[0].major; yygo
217d8 74 6f 6d 69 6e 6f 72 2e 79 79 33 37 30 2e 62 20  tominor.yy370.b 
217d9 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  = 0;}.        br
217da 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
217db 32 36 36 3a 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  266:.{yygotomino
217dc 72 2e 79 79 33 37 30 2e 61 20 3d 20 54 4b 5f 55  r.yy370.a = TK_U
217dd 50 44 41 54 45 3b 20 79 79 67 6f 74 6f 6d 69 6e  PDATE; yygotomin
217de 6f 72 2e 79 79 33 37 30 2e 62 20 3d 20 79 79 6d  or.yy370.b = yym
217df 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 33  sp[0].minor.yy43
217e0 32 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  2;}.        brea
217e1 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 36  k;.      case 26
217e2 39 3a 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  9:.{ yygotominor
217e3 2e 79 79 31 37 32 20 3d 20 30 3b 20 7d 0a 20 20  .yy172 = 0; }.  
217e4 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
217e5 20 20 20 63 61 73 65 20 32 37 30 3a 0a 7b 20 79     case 270:.{ y
217e6 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32  ygotominor.yy172
217e7 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
217e8 72 2e 79 79 31 37 32 3b 20 7d 0a 20 20 20 20 20  r.yy172; }.     
217e9 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
217ea 63 61 73 65 20 32 37 31 3a 0a 7b 0a 20 20 69 66  case 271:.{.  if
217eb 28 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  ( yymsp[-2].mino
217ec 72 2e 79 79 32 34 33 20 29 7b 0a 20 20 20 20 79  r.yy243 ){.    y
217ed 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
217ee 79 32 34 33 2d 3e 70 4c 61 73 74 2d 3e 70 4e 65  y243->pLast->pNe
217ef 78 74 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  xt = yymsp[-1].m
217f0 69 6e 6f 72 2e 79 79 32 34 33 3b 0a 20 20 7d 65  inor.yy243;.  }e
217f1 6c 73 65 7b 0a 20 20 20 20 79 79 6d 73 70 5b 2d  lse{.    yymsp[-
217f2 32 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 33 20 3d  2].minor.yy243 =
217f3 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
217f4 2e 79 79 32 34 33 3b 0a 20 20 7d 0a 20 20 79 79  .yy243;.  }.  yy
217f5 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
217f6 32 34 33 2d 3e 70 4c 61 73 74 20 3d 20 79 79 6d  243->pLast = yym
217f7 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 32  sp[-1].minor.yy2
217f8 34 33 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  43;.  yygotomino
217f9 72 2e 79 79 32 34 33 20 3d 20 79 79 6d 73 70 5b  r.yy243 = yymsp[
217fa 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 33 3b  -2].minor.yy243;
217fb 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
217fc 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 37 32  ;.      case 272
217fd 3a 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  :.{ yygotominor.
217fe 79 79 32 34 33 20 3d 20 30 3b 20 7d 0a 20 20 20  yy243 = 0; }.   
217ff 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
21800 20 20 63 61 73 65 20 32 37 33 3a 0a 7b 20 79 79    case 273:.{ yy
21801 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 34 33 20  gotominor.yy243 
21802 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72  = sqlite3Trigger
21803 55 70 64 61 74 65 53 74 65 70 28 26 79 79 6d 73  UpdateStep(&yyms
21804 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31  p[-3].minor.yy41
21805 30 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  0, yymsp[-1].min
21806 6f 72 2e 79 79 31 37 34 2c 20 79 79 6d 73 70 5b  or.yy174, yymsp[
21807 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 20  0].minor.yy172, 
21808 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
21809 79 79 34 36 29 3b 20 7d 0a 20 20 20 20 20 20 20  yy46); }.       
2180a 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2180b 73 65 20 32 37 34 3a 0a 7b 79 79 67 6f 74 6f 6d  se 274:.{yygotom
2180c 69 6e 6f 72 2e 79 79 32 34 33 20 3d 20 73 71 6c  inor.yy243 = sql
2180d 69 74 65 33 54 72 69 67 67 65 72 49 6e 73 65 72  ite3TriggerInser
2180e 74 53 74 65 70 28 26 79 79 6d 73 70 5b 2d 35 5d  tStep(&yymsp[-5]
2180f 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 20 79 79  .minor.yy410, yy
21810 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
21811 34 33 32 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  432, yymsp[-1].m
21812 69 6e 6f 72 2e 79 79 31 37 34 2c 20 30 2c 20 79  inor.yy174, 0, y
21813 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-7].minor.y
21814 79 34 36 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  y46);}.        b
21815 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
21816 20 32 37 35 3a 0a 7b 79 79 67 6f 74 6f 6d 69 6e   275:.{yygotomin
21817 6f 72 2e 79 79 32 34 33 20 3d 20 73 71 6c 69 74  or.yy243 = sqlit
21818 65 33 54 72 69 67 67 65 72 49 6e 73 65 72 74 53  e3TriggerInsertS
21819 74 65 70 28 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d  tep(&yymsp[-2].m
2181a 69 6e 6f 72 2e 79 79 34 31 30 2c 20 79 79 6d 73  inor.yy410, yyms
2181b 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 33  p[-1].minor.yy43
2181c 32 2c 20 30 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d  2, 0, yymsp[0].m
2181d 69 6e 6f 72 2e 79 79 32 31 39 2c 20 79 79 6d 73  inor.yy219, yyms
2181e 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36  p[-4].minor.yy46
2181f 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
21820 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 37  k;.      case 27
21821 36 3a 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  6:.{yygotominor.
21822 79 79 32 34 33 20 3d 20 73 71 6c 69 74 65 33 54  yy243 = sqlite3T
21823 72 69 67 67 65 72 44 65 6c 65 74 65 53 74 65 70  riggerDeleteStep
21824 28 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  (&yymsp[-1].mino
21825 72 2e 79 79 34 31 30 2c 20 79 79 6d 73 70 5b 30  r.yy410, yymsp[0
21826 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 29 3b 7d  ].minor.yy172);}
21827 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
21828 20 20 20 20 20 20 63 61 73 65 20 32 37 37 3a 0a        case 277:.
21829 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32  {yygotominor.yy2
2182a 34 33 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67  43 = sqlite3Trig
2182b 67 65 72 53 65 6c 65 63 74 53 74 65 70 28 79 79  gerSelectStep(yy
2182c 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32  msp[0].minor.yy2
2182d 31 39 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 62  19); }.        b
2182e 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2182f 20 32 37 38 3a 0a 7b 0a 20 20 79 79 67 6f 74 6f   278:.{.  yygoto
21830 6d 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73 71  minor.yy172 = sq
21831 6c 69 74 65 33 45 78 70 72 28 54 4b 5f 52 41 49  lite3Expr(TK_RAI
21832 53 45 2c 20 30 2c 20 30 2c 20 30 29 3b 20 0a 20  SE, 0, 0, 0); . 
21833 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72   if( yygotominor
21834 2e 79 79 31 37 32 20 29 7b 0a 20 20 20 20 79 79  .yy172 ){.    yy
21835 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 2d  gotominor.yy172-
21836 3e 69 43 6f 6c 75 6d 6e 20 3d 20 4f 45 5f 49 67  >iColumn = OE_Ig
21837 6e 6f 72 65 3b 0a 20 20 20 20 73 71 6c 69 74 65  nore;.    sqlite
21838 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74 6f  3ExprSpan(yygoto
21839 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 20 26 79 79  minor.yy172, &yy
2183a 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
2183b 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  0, &yymsp[0].min
2183c 6f 72 2e 79 79 30 29 3b 0a 20 20 7d 0a 7d 0a 20  or.yy0);.  }.}. 
2183d 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2183e 20 20 20 20 63 61 73 65 20 32 37 39 3a 0a 7b 0a      case 279:.{.
2183f 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
21840 31 37 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70  172 = sqlite3Exp
21841 72 28 54 4b 5f 52 41 49 53 45 2c 20 30 2c 20 30  r(TK_RAISE, 0, 0
21842 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  , &yymsp[-1].min
21843 6f 72 2e 79 79 34 31 30 29 3b 20 0a 20 20 69 66  or.yy410); .  if
21844 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ( yygotominor.yy
21845 31 37 32 20 29 20 7b 0a 20 20 20 20 79 79 67 6f  172 ) {.    yygo
21846 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 2d 3e 69  tominor.yy172->i
21847 43 6f 6c 75 6d 6e 20 3d 20 79 79 6d 73 70 5b 2d  Column = yymsp[-
21848 33 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 3b 0a 20  3].minor.yy46;. 
21849 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 70     sqlite3ExprSp
2184a 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  an(yygotominor.y
2184b 79 31 37 32 2c 20 26 79 79 6d 73 70 5b 2d 35 5d  y172, &yymsp[-5]
2184c 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d  .minor.yy0, &yym
2184d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
2184e 3b 0a 20 20 7d 0a 7d 0a 20 20 20 20 20 20 20 20  ;.  }.}.        
2184f 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
21850 65 20 32 38 30 3a 0a 7b 79 79 67 6f 74 6f 6d 69  e 280:.{yygotomi
21851 6e 6f 72 2e 79 79 34 36 20 3d 20 4f 45 5f 52 6f  nor.yy46 = OE_Ro
21852 6c 6c 62 61 63 6b 3b 7d 0a 20 20 20 20 20 20 20  llback;}.       
21853 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
21854 73 65 20 32 38 32 3a 0a 7b 79 79 67 6f 74 6f 6d  se 282:.{yygotom
21855 69 6e 6f 72 2e 79 79 34 36 20 3d 20 4f 45 5f 46  inor.yy46 = OE_F
21856 61 69 6c 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  ail;}.        br
21857 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
21858 32 38 33 3a 0a 7b 0a 20 20 73 71 6c 69 74 65 33  283:.{.  sqlite3
21859 44 72 6f 70 54 72 69 67 67 65 72 28 70 50 61 72  DropTrigger(pPar
2185a 73 65 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  se,yymsp[0].mino
2185b 72 2e 79 79 33 37 33 2c 79 79 6d 73 70 5b 2d 31  r.yy373,yymsp[-1
2185c 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 29 3b 0a 7d  ].minor.yy46);.}
2185d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2185e 20 20 20 20 20 20 63 61 73 65 20 32 38 34 3a 0a        case 284:.
2185f 7b 0a 20 20 73 71 6c 69 74 65 33 41 74 74 61 63  {.  sqlite3Attac
21860 68 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b  h(pParse, yymsp[
21861 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2c  -3].minor.yy172,
21862 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
21863 2e 79 79 31 37 32 2c 20 79 79 6d 73 70 5b 30 5d  .yy172, yymsp[0]
21864 2e 6d 69 6e 6f 72 2e 79 79 33 38 36 29 3b 0a 7d  .minor.yy386);.}
21865 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
21866 20 20 20 20 20 20 63 61 73 65 20 32 38 35 3a 0a        case 285:.
21867 7b 0a 20 20 73 71 6c 69 74 65 33 44 65 74 61 63  {.  sqlite3Detac
21868 68 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b  h(pParse, yymsp[
21869 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 29 3b  0].minor.yy172);
2186a 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
2186b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 38 36  ;.      case 286
2186c 3a 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  :.{ yygotominor.
2186d 79 79 33 38 36 20 3d 20 30 3b 20 7d 0a 20 20 20  yy386 = 0; }.   
2186e 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2186f 20 20 63 61 73 65 20 32 38 37 3a 0a 7b 20 79 79    case 287:.{ yy
21870 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 38 36 20  gotominor.yy386 
21871 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  = yymsp[0].minor
21872 2e 79 79 31 37 32 3b 20 7d 0a 20 20 20 20 20 20  .yy172; }.      
21873 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
21874 61 73 65 20 32 39 30 3a 0a 7b 73 71 6c 69 74 65  ase 290:.{sqlite
21875 33 52 65 69 6e 64 65 78 28 70 50 61 72 73 65 2c  3Reindex(pParse,
21876 20 30 2c 20 30 29 3b 7d 0a 20 20 20 20 20 20 20   0, 0);}.       
21877 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
21878 73 65 20 32 39 31 3a 0a 7b 73 71 6c 69 74 65 33  se 291:.{sqlite3
21879 52 65 69 6e 64 65 78 28 70 50 61 72 73 65 2c 20  Reindex(pParse, 
2187a 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  &yymsp[-1].minor
2187b 2e 79 79 34 31 30 2c 20 26 79 79 6d 73 70 5b 30  .yy410, &yymsp[0
2187c 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 29 3b 7d  ].minor.yy410);}
2187d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2187e 20 20 20 20 20 20 63 61 73 65 20 32 39 32 3a 0a        case 292:.
2187f 7b 73 71 6c 69 74 65 33 41 6e 61 6c 79 7a 65 28  {sqlite3Analyze(
21880 70 50 61 72 73 65 2c 20 30 2c 20 30 29 3b 7d 0a  pParse, 0, 0);}.
21881 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
21882 20 20 20 20 20 63 61 73 65 20 32 39 33 3a 0a 7b       case 293:.{
21883 73 71 6c 69 74 65 33 41 6e 61 6c 79 7a 65 28 70  sqlite3Analyze(p
21884 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d 31  Parse, &yymsp[-1
21885 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 20 26  ].minor.yy410, &
21886 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
21887 79 34 31 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  y410);}.        
21888 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
21889 65 20 32 39 34 3a 0a 7b 0a 20 20 73 71 6c 69 74  e 294:.{.  sqlit
2188a 65 33 41 6c 74 65 72 52 65 6e 61 6d 65 54 61 62  e3AlterRenameTab
2188b 6c 65 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b  le(pParse,yymsp[
2188c 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 37 33 2c  -3].minor.yy373,
2188d 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
2188e 79 79 34 31 30 29 3b 0a 7d 0a 20 20 20 20 20 20  yy410);.}.      
2188f 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
21890 61 73 65 20 32 39 35 3a 0a 7b 0a 20 20 73 71 6c  ase 295:.{.  sql
21891 69 74 65 33 41 6c 74 65 72 46 69 6e 69 73 68 41  ite3AlterFinishA
21892 64 64 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c  ddColumn(pParse,
21893 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
21894 2e 79 79 34 31 30 29 3b 0a 7d 0a 20 20 20 20 20  .yy410);.}.     
21895 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
21896 63 61 73 65 20 32 39 36 3a 0a 7b 0a 20 20 73 71  case 296:.{.  sq
21897 6c 69 74 65 33 41 6c 74 65 72 42 65 67 69 6e 41  lite3AlterBeginA
21898 64 64 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c  ddColumn(pParse,
21899 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
2189a 79 79 33 37 33 29 3b 0a 7d 0a 20 20 20 20 20 20  yy373);.}.      
2189b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2189c 61 73 65 20 32 39 39 3a 0a 7b 73 71 6c 69 74 65  ase 299:.{sqlite
2189d 33 56 74 61 62 46 69 6e 69 73 68 50 61 72 73 65  3VtabFinishParse
2189e 28 70 50 61 72 73 65 2c 30 29 3b 7d 0a 20 20 20  (pParse,0);}.   
2189f 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
218a0 20 20 63 61 73 65 20 33 30 30 3a 0a 7b 73 71 6c    case 300:.{sql
218a1 69 74 65 33 56 74 61 62 46 69 6e 69 73 68 50 61  ite3VtabFinishPa
218a2 72 73 65 28 70 50 61 72 73 65 2c 26 79 79 6d 73  rse(pParse,&yyms
218a3 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
218a4 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
218a5 0a 20 20 20 20 20 20 63 61 73 65 20 33 30 31 3a  .      case 301:
218a6 0a 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 74  .{.    sqlite3Vt
218a7 61 62 42 65 67 69 6e 50 61 72 73 65 28 70 50 61  abBeginParse(pPa
218a8 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d 33 5d 2e  rse, &yymsp[-3].
218a9 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 20 26 79 79  minor.yy410, &yy
218aa 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
218ab 34 31 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  410, &yymsp[0].m
218ac 69 6e 6f 72 2e 79 79 34 31 30 29 3b 0a 7d 0a 20  inor.yy410);.}. 
218ad 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
218ae 20 20 20 20 63 61 73 65 20 33 30 34 3a 0a 7b 73      case 304:.{s
218af 71 6c 69 74 65 33 56 74 61 62 41 72 67 49 6e 69  qlite3VtabArgIni
218b0 74 28 70 50 61 72 73 65 29 3b 7d 0a 20 20 20 20  t(pParse);}.    
218b1 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
218b2 20 63 61 73 65 20 33 30 36 3a 0a 20 20 20 20 20   case 306:.     
218b3 20 63 61 73 65 20 33 30 37 3a 0a 20 20 20 20 20   case 307:.     
218b4 20 63 61 73 65 20 33 30 38 3a 0a 20 20 20 20 20   case 308:.     
218b5 20 63 61 73 65 20 33 31 30 3a 0a 7b 73 71 6c 69   case 310:.{sqli
218b6 74 65 33 56 74 61 62 41 72 67 45 78 74 65 6e 64  te3VtabArgExtend
218b7 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 30  (pParse,&yymsp[0
218b8 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20  ].minor.yy0);}. 
218b9 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
218ba 7d 3b 0a 20 20 79 79 67 6f 74 6f 20 3d 20 79 79  };.  yygoto = yy
218bb 52 75 6c 65 49 6e 66 6f 5b 79 79 72 75 6c 65 6e  RuleInfo[yyrulen
218bc 6f 5d 2e 6c 68 73 3b 0a 20 20 79 79 73 69 7a 65  o].lhs;.  yysize
218bd 20 3d 20 79 79 52 75 6c 65 49 6e 66 6f 5b 79 79   = yyRuleInfo[yy
218be 72 75 6c 65 6e 6f 5d 2e 6e 72 68 73 3b 0a 20 20  ruleno].nrhs;.  
218bf 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  yypParser->yyidx
218c0 20 2d 3d 20 79 79 73 69 7a 65 3b 0a 20 20 79 79   -= yysize;.  yy
218c1 61 63 74 20 3d 20 79 79 5f 66 69 6e 64 5f 72 65  act = yy_find_re
218c2 64 75 63 65 5f 61 63 74 69 6f 6e 28 79 79 6d 73  duce_action(yyms
218c3 70 5b 2d 79 79 73 69 7a 65 5d 2e 73 74 61 74 65  p[-yysize].state
218c4 6e 6f 2c 79 79 67 6f 74 6f 29 3b 0a 20 20 69 66  no,yygoto);.  if
218c5 28 20 79 79 61 63 74 20 3c 20 59 59 4e 53 54 41  ( yyact < YYNSTA
218c6 54 45 20 29 7b 0a 23 69 66 64 65 66 20 4e 44 45  TE ){.#ifdef NDE
218c7 42 55 47 0a 20 20 20 20 2f 2a 20 49 66 20 77 65  BUG.    /* If we
218c8 20 61 72 65 20 6e 6f 74 20 64 65 62 75 67 67 69   are not debuggi
218c9 6e 67 20 61 6e 64 20 74 68 65 20 72 65 64 75 63  ng and the reduc
218ca 65 20 61 63 74 69 6f 6e 20 70 6f 70 70 65 64 20  e action popped 
218cb 61 74 20 6c 65 61 73 74 0a 20 20 20 20 2a 2a 20  at least.    ** 
218cc 6f 6e 65 20 65 6c 65 6d 65 6e 74 20 6f 66 66 20  one element off 
218cd 74 68 65 20 73 74 61 63 6b 2c 20 74 68 65 6e 20  the stack, then 
218ce 77 65 20 63 61 6e 20 70 75 73 68 20 74 68 65 20  we can push the 
218cf 6e 65 77 20 65 6c 65 6d 65 6e 74 20 62 61 63 6b  new element back
218d0 0a 20 20 20 20 2a 2a 20 6f 6e 74 6f 20 74 68 65  .    ** onto the
218d1 20 73 74 61 63 6b 20 68 65 72 65 2c 20 61 6e 64   stack here, and
218d2 20 73 6b 69 70 20 74 68 65 20 73 74 61 63 6b 20   skip the stack 
218d3 6f 76 65 72 66 6c 6f 77 20 74 65 73 74 20 69 6e  overflow test in
218d4 20 79 79 5f 73 68 69 66 74 28 29 2e 0a 20 20 20   yy_shift()..   
218d5 20 2a 2a 20 54 68 61 74 20 67 69 76 65 73 20 61   ** That gives a
218d6 20 73 69 67 6e 69 66 69 63 61 6e 74 20 73 70 65   significant spe
218d7 65 64 20 69 6d 70 72 6f 76 65 6d 65 6e 74 2e 20  ed improvement. 
218d8 2a 2f 0a 20 20 20 20 69 66 28 20 79 79 73 69 7a  */.    if( yysiz
218d9 65 20 29 7b 0a 20 20 20 20 20 20 79 79 70 50 61  e ){.      yypPa
218da 72 73 65 72 2d 3e 79 79 69 64 78 2b 2b 3b 0a 20  rser->yyidx++;. 
218db 20 20 20 20 20 79 79 6d 73 70 20 2d 3d 20 79 79       yymsp -= yy
218dc 73 69 7a 65 2d 31 3b 0a 20 20 20 20 20 20 79 79  size-1;.      yy
218dd 6d 73 70 2d 3e 73 74 61 74 65 6e 6f 20 3d 20 79  msp->stateno = y
218de 79 61 63 74 3b 0a 20 20 20 20 20 20 79 79 6d 73  yact;.      yyms
218df 70 2d 3e 6d 61 6a 6f 72 20 3d 20 79 79 67 6f 74  p->major = yygot
218e0 6f 3b 0a 20 20 20 20 20 20 79 79 6d 73 70 2d 3e  o;.      yymsp->
218e1 6d 69 6e 6f 72 20 3d 20 79 79 67 6f 74 6f 6d 69  minor = yygotomi
218e2 6e 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 23  nor;.    }else.#
218e3 65 6e 64 69 66 0a 20 20 20 20 7b 0a 20 20 20 20  endif.    {.    
218e4 20 20 79 79 5f 73 68 69 66 74 28 79 79 70 50 61    yy_shift(yypPa
218e5 72 73 65 72 2c 79 79 61 63 74 2c 79 79 67 6f 74  rser,yyact,yygot
218e6 6f 2c 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 29 3b  o,&yygotominor);
218e7 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69  .    }.  }else i
218e8 66 28 20 79 79 61 63 74 20 3d 3d 20 59 59 4e 53  f( yyact == YYNS
218e9 54 41 54 45 20 2b 20 59 59 4e 52 55 4c 45 20 2b  TATE + YYNRULE +
218ea 20 31 20 29 7b 0a 20 20 20 20 79 79 5f 61 63 63   1 ){.    yy_acc
218eb 65 70 74 28 79 79 70 50 61 72 73 65 72 29 3b 0a  ept(yypParser);.
218ec 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65    }.}../*.** The
218ed 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20   following code 
218ee 65 78 65 63 75 74 65 73 20 77 68 65 6e 20 74 68  executes when th
218ef 65 20 70 61 72 73 65 20 66 61 69 6c 73 0a 2a 2f  e parse fails.*/
218f0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 79 79 5f  .static void yy_
218f1 70 61 72 73 65 5f 66 61 69 6c 65 64 28 0a 20 20  parse_failed(.  
218f2 79 79 50 61 72 73 65 72 20 2a 79 79 70 50 61 72  yyParser *yypPar
218f3 73 65 72 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ser           /*
218f4 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 29   The parser */.)
218f5 7b 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65  {.  sqlite3Parse
218f6 72 41 52 47 5f 46 45 54 43 48 3b 0a 23 69 66 6e  rARG_FETCH;.#ifn
218f7 64 65 66 20 4e 44 45 42 55 47 0a 20 20 69 66 28  def NDEBUG.  if(
218f8 20 79 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0a   yyTraceFILE ){.
218f9 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72      fprintf(yyTr
218fa 61 63 65 46 49 4c 45 2c 22 25 73 46 61 69 6c 21  aceFILE,"%sFail!
218fb 5c 6e 22 2c 79 79 54 72 61 63 65 50 72 6f 6d 70  \n",yyTracePromp
218fc 74 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20  t);.  }.#endif. 
218fd 20 77 68 69 6c 65 28 20 79 79 70 50 61 72 73 65   while( yypParse
218fe 72 2d 3e 79 79 69 64 78 3e 3d 30 20 29 20 79 79  r->yyidx>=0 ) yy
218ff 5f 70 6f 70 5f 70 61 72 73 65 72 5f 73 74 61 63  _pop_parser_stac
21900 6b 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20 20  k(yypParser);.  
21901 2f 2a 20 48 65 72 65 20 63 6f 64 65 20 69 73 20  /* Here code is 
21902 69 6e 73 65 72 74 65 64 20 77 68 69 63 68 20 77  inserted which w
21903 69 6c 6c 20 62 65 20 65 78 65 63 75 74 65 64 20  ill be executed 
21904 77 68 65 6e 65 76 65 72 20 74 68 65 0a 20 20 2a  whenever the.  *
21905 2a 20 70 61 72 73 65 72 20 66 61 69 6c 73 20 2a  * parser fails *
21906 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65  /.  sqlite3Parse
21907 72 41 52 47 5f 53 54 4f 52 45 3b 20 2f 2a 20 53  rARG_STORE; /* S
21908 75 70 70 72 65 73 73 20 77 61 72 6e 69 6e 67 20  uppress warning 
21909 61 62 6f 75 74 20 75 6e 75 73 65 64 20 25 65 78  about unused %ex
2190a 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 76 61 72  tra_argument var
2190b 69 61 62 6c 65 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a  iable */.}../*.*
2190c 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
2190d 63 6f 64 65 20 65 78 65 63 75 74 65 73 20 77 68  code executes wh
2190e 65 6e 20 61 20 73 79 6e 74 61 78 20 65 72 72 6f  en a syntax erro
2190f 72 20 66 69 72 73 74 20 6f 63 63 75 72 73 2e 0a  r first occurs..
21910 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 79  */.static void y
21911 79 5f 73 79 6e 74 61 78 5f 65 72 72 6f 72 28 0a  y_syntax_error(.
21912 20 20 79 79 50 61 72 73 65 72 20 2a 79 79 70 50    yyParser *yypP
21913 61 72 73 65 72 2c 20 20 20 20 20 20 20 20 20 20  arser,          
21914 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a   /* The parser *
21915 2f 0a 20 20 69 6e 74 20 79 79 6d 61 6a 6f 72 2c  /.  int yymajor,
21916 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21917 20 20 20 2f 2a 20 54 68 65 20 6d 61 6a 6f 72 20     /* The major 
21918 74 79 70 65 20 6f 66 20 74 68 65 20 65 72 72 6f  type of the erro
21919 72 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 59 59 4d  r token */.  YYM
2191a 49 4e 4f 52 54 59 50 45 20 79 79 6d 69 6e 6f 72  INORTYPE yyminor
2191b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2191c 68 65 20 6d 69 6e 6f 72 20 74 79 70 65 20 6f 66  he minor type of
2191d 20 74 68 65 20 65 72 72 6f 72 20 74 6f 6b 65 6e   the error token
2191e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   */.){.  sqlite3
2191f 50 61 72 73 65 72 41 52 47 5f 46 45 54 43 48 3b  ParserARG_FETCH;
21920 0a 23 64 65 66 69 6e 65 20 54 4f 4b 45 4e 20 28  .#define TOKEN (
21921 79 79 6d 69 6e 6f 72 2e 79 79 30 29 0a 0a 20 20  yyminor.yy0)..  
21922 69 66 28 20 21 70 50 61 72 73 65 2d 3e 70 61 72  if( !pParse->par
21923 73 65 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 69  seError ){.    i
21924 66 28 20 54 4f 4b 45 4e 2e 7a 5b 30 5d 20 29 7b  f( TOKEN.z[0] ){
21925 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
21926 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
21927 6e 65 61 72 20 5c 22 25 54 5c 22 3a 20 73 79 6e  near \"%T\": syn
21928 74 61 78 20 65 72 72 6f 72 22 2c 20 26 54 4f 4b  tax error", &TOK
21929 45 4e 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  EN);.    }else{.
2192a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
2192b 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 69  orMsg(pParse, "i
2192c 6e 63 6f 6d 70 6c 65 74 65 20 53 51 4c 20 73 74  ncomplete SQL st
2192d 61 74 65 6d 65 6e 74 22 29 3b 0a 20 20 20 20 7d  atement");.    }
2192e 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 70 61 72  .    pParse->par
2192f 73 65 45 72 72 6f 72 20 3d 20 31 3b 0a 20 20 7d  seError = 1;.  }
21930 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  .  sqlite3Parser
21931 41 52 47 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75  ARG_STORE; /* Su
21932 70 70 72 65 73 73 20 77 61 72 6e 69 6e 67 20 61  ppress warning a
21933 62 6f 75 74 20 75 6e 75 73 65 64 20 25 65 78 74  bout unused %ext
21934 72 61 5f 61 72 67 75 6d 65 6e 74 20 76 61 72 69  ra_argument vari
21935 61 62 6c 65 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a  able */.}../*.**
21936 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69   The following i
21937 73 20 65 78 65 63 75 74 65 64 20 77 68 65 6e 20  s executed when 
21938 74 68 65 20 70 61 72 73 65 72 20 61 63 63 65 70  the parser accep
21939 74 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  ts.*/.static voi
2193a 64 20 79 79 5f 61 63 63 65 70 74 28 0a 20 20 79  d yy_accept(.  y
2193b 79 50 61 72 73 65 72 20 2a 79 79 70 50 61 72 73  yParser *yypPars
2193c 65 72 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  er           /* 
2193d 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 29 7b  The parser */.){
2193e 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  .  sqlite3Parser
2193f 41 52 47 5f 46 45 54 43 48 3b 0a 23 69 66 6e 64  ARG_FETCH;.#ifnd
21940 65 66 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20  ef NDEBUG.  if( 
21941 79 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20  yyTraceFILE ){. 
21942 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61     fprintf(yyTra
21943 63 65 46 49 4c 45 2c 22 25 73 41 63 63 65 70 74  ceFILE,"%sAccept
21944 21 5c 6e 22 2c 79 79 54 72 61 63 65 50 72 6f 6d  !\n",yyTraceProm
21945 70 74 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  pt);.  }.#endif.
21946 20 20 77 68 69 6c 65 28 20 79 79 70 50 61 72 73    while( yypPars
21947 65 72 2d 3e 79 79 69 64 78 3e 3d 30 20 29 20 79  er->yyidx>=0 ) y
21948 79 5f 70 6f 70 5f 70 61 72 73 65 72 5f 73 74 61  y_pop_parser_sta
21949 63 6b 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20  ck(yypParser);. 
2194a 20 2f 2a 20 48 65 72 65 20 63 6f 64 65 20 69 73   /* Here code is
2194b 20 69 6e 73 65 72 74 65 64 20 77 68 69 63 68 20   inserted which 
2194c 77 69 6c 6c 20 62 65 20 65 78 65 63 75 74 65 64  will be executed
2194d 20 77 68 65 6e 65 76 65 72 20 74 68 65 0a 20 20   whenever the.  
2194e 2a 2a 20 70 61 72 73 65 72 20 61 63 63 65 70 74  ** parser accept
2194f 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61  s */.  sqlite3Pa
21950 72 73 65 72 41 52 47 5f 53 54 4f 52 45 3b 20 2f  rserARG_STORE; /
21951 2a 20 53 75 70 70 72 65 73 73 20 77 61 72 6e 69  * Suppress warni
21952 6e 67 20 61 62 6f 75 74 20 75 6e 75 73 65 64 20  ng about unused 
21953 25 65 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 20  %extra_argument 
21954 76 61 72 69 61 62 6c 65 20 2a 2f 0a 7d 0a 0a 2f  variable */.}../
21955 2a 20 54 68 65 20 6d 61 69 6e 20 70 61 72 73 65  * The main parse
21956 72 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 20 54 68  r program..** Th
21957 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
21958 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
21959 20 61 20 73 74 72 75 63 74 75 72 65 20 6f 62 74   a structure obt
2195a 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 22 73  ained from.** "s
2195b 71 6c 69 74 65 33 50 61 72 73 65 72 41 6c 6c 6f  qlite3ParserAllo
2195c 63 22 20 77 68 69 63 68 20 64 65 73 63 72 69 62  c" which describ
2195d 65 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 73  es the current s
2195e 74 61 74 65 20 6f 66 20 74 68 65 20 70 61 72 73  tate of the pars
2195f 65 72 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e  er..** The secon
21960 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68  d argument is th
21961 65 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 6e 75  e major token nu
21962 6d 62 65 72 2e 20 20 54 68 65 20 74 68 69 72 64  mber.  The third
21963 20 69 73 0a 2a 2a 20 74 68 65 20 6d 69 6e 6f 72   is.** the minor
21964 20 74 6f 6b 65 6e 2e 20 20 54 68 65 20 66 6f 75   token.  The fou
21965 72 74 68 20 6f 70 74 69 6f 6e 61 6c 20 61 72 67  rth optional arg
21966 75 6d 65 6e 74 20 69 73 20 77 68 61 74 65 76 65  ument is whateve
21967 72 20 74 68 65 0a 2a 2a 20 75 73 65 72 20 77 61  r the.** user wa
21968 6e 74 73 20 28 61 6e 64 20 73 70 65 63 69 66 69  nts (and specifi
21969 65 64 20 69 6e 20 74 68 65 20 67 72 61 6d 6d 61  ed in the gramma
2196a 72 29 20 61 6e 64 20 69 73 20 61 76 61 69 6c 61  r) and is availa
2196b 62 6c 65 20 66 6f 72 0a 2a 2a 20 75 73 65 20 62  ble for.** use b
2196c 79 20 74 68 65 20 61 63 74 69 6f 6e 20 72 6f 75  y the action rou
2196d 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 6e 70  tines..**.** Inp
2196e 75 74 73 3a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20  uts:.** <ul>.** 
2196f 3c 6c 69 3e 20 41 20 70 6f 69 6e 74 65 72 20 74  <li> A pointer t
21970 6f 20 74 68 65 20 70 61 72 73 65 72 20 28 61 6e  o the parser (an
21971 20 6f 70 61 71 75 65 20 73 74 72 75 63 74 75 72   opaque structur
21972 65 2e 29 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20  e.).** <li> The 
21973 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 6e 75 6d 62  major token numb
21974 65 72 2e 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20  er..** <li> The 
21975 6d 69 6e 6f 72 20 74 6f 6b 65 6e 20 6e 75 6d 62  minor token numb
21976 65 72 2e 0a 2a 2a 20 3c 6c 69 3e 20 41 6e 20 6f  er..** <li> An o
21977 70 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 6f  ption argument o
21978 66 20 61 20 67 72 61 6d 6d 61 72 2d 73 70 65 63  f a grammar-spec
21979 69 66 69 65 64 20 74 79 70 65 2e 0a 2a 2a 20 3c  ified type..** <
2197a 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 4f 75 74 70 75  /ul>.**.** Outpu
2197b 74 73 3a 0a 2a 2a 20 4e 6f 6e 65 2e 0a 2a 2f 0a  ts:.** None..*/.
2197c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2197d 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72 73 65  oid sqlite3Parse
2197e 72 28 0a 20 20 76 6f 69 64 20 2a 79 79 70 2c 20  r(.  void *yyp, 
2197f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21980 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20    /* The parser 
21981 2a 2f 0a 20 20 69 6e 74 20 79 79 6d 61 6a 6f 72  */.  int yymajor
21982 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
21983 20 20 2f 2a 20 54 68 65 20 6d 61 6a 6f 72 20 74    /* The major t
21984 6f 6b 65 6e 20 63 6f 64 65 20 6e 75 6d 62 65 72  oken code number
21985 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72   */.  sqlite3Par
21986 73 65 72 54 4f 4b 45 4e 54 59 50 45 20 79 79 6d  serTOKENTYPE yym
21987 69 6e 6f 72 20 20 20 20 20 20 20 2f 2a 20 54 68  inor       /* Th
21988 65 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20  e value for the 
21989 74 6f 6b 65 6e 20 2a 2f 0a 20 20 73 71 6c 69 74  token */.  sqlit
2198a 65 33 50 61 72 73 65 72 41 52 47 5f 50 44 45 43  e3ParserARG_PDEC
2198b 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  L               
2198c 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 25 65 78 74  /* Optional %ext
2198d 72 61 5f 61 72 67 75 6d 65 6e 74 20 70 61 72 61  ra_argument para
2198e 6d 65 74 65 72 20 2a 2f 0a 29 7b 0a 20 20 59 59  meter */.){.  YY
2198f 4d 49 4e 4f 52 54 59 50 45 20 79 79 6d 69 6e 6f  MINORTYPE yymino
21990 72 75 6e 69 6f 6e 3b 0a 20 20 69 6e 74 20 79 79  runion;.  int yy
21991 61 63 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  act;            
21992 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 61 63  /* The parser ac
21993 74 69 6f 6e 2e 20 2a 2f 0a 20 20 69 6e 74 20 79  tion. */.  int y
21994 79 65 6e 64 6f 66 69 6e 70 75 74 3b 20 20 20 20  yendofinput;    
21995 20 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20 61   /* True if we a
21996 72 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66  re at the end of
21997 20 69 6e 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20   input */.  int 
21998 79 79 65 72 72 6f 72 68 69 74 20 3d 20 30 3b 20  yyerrorhit = 0; 
21999 20 20 2f 2a 20 54 72 75 65 20 69 66 20 79 79 6d    /* True if yym
2199a 61 6a 6f 72 20 68 61 73 20 69 6e 76 6f 6b 65 64  ajor has invoked
2199b 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 79   an error */.  y
2199c 79 50 61 72 73 65 72 20 2a 79 79 70 50 61 72 73  yParser *yypPars
2199d 65 72 3b 20 20 2f 2a 20 54 68 65 20 70 61 72 73  er;  /* The pars
2199e 65 72 20 2a 2f 0a 0a 20 20 2f 2a 20 28 72 65 29  er */..  /* (re)
2199f 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 70  initialize the p
219a0 61 72 73 65 72 2c 20 69 66 20 6e 65 63 65 73 73  arser, if necess
219a1 61 72 79 20 2a 2f 0a 20 20 79 79 70 50 61 72 73  ary */.  yypPars
219a2 65 72 20 3d 20 28 79 79 50 61 72 73 65 72 2a 29  er = (yyParser*)
219a3 79 79 70 3b 0a 20 20 69 66 28 20 79 79 70 50 61  yyp;.  if( yypPa
219a4 72 73 65 72 2d 3e 79 79 69 64 78 3c 30 20 29 7b  rser->yyidx<0 ){
219a5 0a 23 69 66 20 59 59 53 54 41 43 4b 44 45 50 54  .#if YYSTACKDEPT
219a6 48 3c 3d 30 0a 20 20 20 20 69 66 28 20 79 79 70  H<=0.    if( yyp
219a7 50 61 72 73 65 72 2d 3e 79 79 73 74 6b 73 7a 20  Parser->yystksz 
219a8 3c 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d  <=0 ){.      mem
219a9 73 65 74 28 26 79 79 6d 69 6e 6f 72 75 6e 69 6f  set(&yyminorunio
219aa 6e 2c 20 30 2c 20 73 69 7a 65 6f 66 28 79 79 6d  n, 0, sizeof(yym
219ab 69 6e 6f 72 75 6e 69 6f 6e 29 29 3b 0a 20 20 20  inorunion));.   
219ac 20 20 20 79 79 53 74 61 63 6b 4f 76 65 72 66 6c     yyStackOverfl
219ad 6f 77 28 79 79 70 50 61 72 73 65 72 2c 20 26 79  ow(yypParser, &y
219ae 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20  yminorunion);.  
219af 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
219b0 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 79 79 70  }.#endif.    yyp
219b1 50 61 72 73 65 72 2d 3e 79 79 69 64 78 20 3d 20  Parser->yyidx = 
219b2 30 3b 0a 20 20 20 20 79 79 70 50 61 72 73 65 72  0;.    yypParser
219b3 2d 3e 79 79 65 72 72 63 6e 74 20 3d 20 2d 31 3b  ->yyerrcnt = -1;
219b4 0a 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e  .    yypParser->
219b5 79 79 73 74 61 63 6b 5b 30 5d 2e 73 74 61 74 65  yystack[0].state
219b6 6e 6f 20 3d 20 30 3b 0a 20 20 20 20 79 79 70 50  no = 0;.    yypP
219b7 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 30  arser->yystack[0
219b8 5d 2e 6d 61 6a 6f 72 20 3d 20 30 3b 0a 20 20 7d  ].major = 0;.  }
219b9 0a 20 20 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 2e  .  yyminorunion.
219ba 79 79 30 20 3d 20 79 79 6d 69 6e 6f 72 3b 0a 20  yy0 = yyminor;. 
219bb 20 79 79 65 6e 64 6f 66 69 6e 70 75 74 20 3d 20   yyendofinput = 
219bc 28 79 79 6d 61 6a 6f 72 3d 3d 30 29 3b 0a 20 20  (yymajor==0);.  
219bd 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47  sqlite3ParserARG
219be 5f 53 54 4f 52 45 3b 0a 0a 23 69 66 6e 64 65 66  _STORE;..#ifndef
219bf 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20 79 79   NDEBUG.  if( yy
219c0 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20  TraceFILE ){.   
219c1 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65   fprintf(yyTrace
219c2 46 49 4c 45 2c 22 25 73 49 6e 70 75 74 20 25 73  FILE,"%sInput %s
219c3 5c 6e 22 2c 79 79 54 72 61 63 65 50 72 6f 6d 70  \n",yyTracePromp
219c4 74 2c 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 79 79  t,yyTokenName[yy
219c5 6d 61 6a 6f 72 5d 29 3b 0a 20 20 7d 0a 23 65 6e  major]);.  }.#en
219c6 64 69 66 0a 0a 20 20 64 6f 7b 0a 20 20 20 20 79  dif..  do{.    y
219c7 79 61 63 74 20 3d 20 79 79 5f 66 69 6e 64 5f 73  yact = yy_find_s
219c8 68 69 66 74 5f 61 63 74 69 6f 6e 28 79 79 70 50  hift_action(yypP
219c9 61 72 73 65 72 2c 79 79 6d 61 6a 6f 72 29 3b 0a  arser,yymajor);.
219ca 20 20 20 20 69 66 28 20 79 79 61 63 74 3c 59 59      if( yyact<YY
219cb 4e 53 54 41 54 45 20 29 7b 0a 20 20 20 20 20 20  NSTATE ){.      
219cc 79 79 5f 73 68 69 66 74 28 79 79 70 50 61 72 73  yy_shift(yypPars
219cd 65 72 2c 79 79 61 63 74 2c 79 79 6d 61 6a 6f 72  er,yyact,yymajor
219ce 2c 26 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b  ,&yyminorunion);
219cf 0a 20 20 20 20 20 20 79 79 70 50 61 72 73 65 72  .      yypParser
219d0 2d 3e 79 79 65 72 72 63 6e 74 2d 2d 3b 0a 20 20  ->yyerrcnt--;.  
219d1 20 20 20 20 69 66 28 20 79 79 65 6e 64 6f 66 69      if( yyendofi
219d2 6e 70 75 74 20 26 26 20 79 79 70 50 61 72 73 65  nput && yypParse
219d3 72 2d 3e 79 79 69 64 78 3e 3d 30 20 29 7b 0a 20  r->yyidx>=0 ){. 
219d4 20 20 20 20 20 20 20 79 79 6d 61 6a 6f 72 20 3d         yymajor =
219d5 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b   0;.      }else{
219d6 0a 20 20 20 20 20 20 20 20 79 79 6d 61 6a 6f 72  .        yymajor
219d7 20 3d 20 59 59 4e 4f 43 4f 44 45 3b 0a 20 20 20   = YYNOCODE;.   
219d8 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69     }.    }else i
219d9 66 28 20 79 79 61 63 74 20 3c 20 59 59 4e 53 54  f( yyact < YYNST
219da 41 54 45 20 2b 20 59 59 4e 52 55 4c 45 20 29 7b  ATE + YYNRULE ){
219db 0a 20 20 20 20 20 20 79 79 5f 72 65 64 75 63 65  .      yy_reduce
219dc 28 79 79 70 50 61 72 73 65 72 2c 79 79 61 63 74  (yypParser,yyact
219dd 2d 59 59 4e 53 54 41 54 45 29 3b 0a 20 20 20 20  -YYNSTATE);.    
219de 7d 65 6c 73 65 20 69 66 28 20 79 79 61 63 74 20  }else if( yyact 
219df 3d 3d 20 59 59 5f 45 52 52 4f 52 5f 41 43 54 49  == YY_ERROR_ACTI
219e0 4f 4e 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  ON ){.      int 
219e1 79 79 6d 78 3b 0a 23 69 66 6e 64 65 66 20 4e 44  yymx;.#ifndef ND
219e2 45 42 55 47 0a 20 20 20 20 20 20 69 66 28 20 79  EBUG.      if( y
219e3 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20  yTraceFILE ){.  
219e4 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 79 79        fprintf(yy
219e5 54 72 61 63 65 46 49 4c 45 2c 22 25 73 53 79 6e  TraceFILE,"%sSyn
219e6 74 61 78 20 45 72 72 6f 72 21 5c 6e 22 2c 79 79  tax Error!\n",yy
219e7 54 72 61 63 65 50 72 6f 6d 70 74 29 3b 0a 20 20  TracePrompt);.  
219e8 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66      }.#endif.#if
219e9 64 65 66 20 59 59 45 52 52 4f 52 53 59 4d 42 4f  def YYERRORSYMBO
219ea 4c 0a 20 20 20 20 20 20 2f 2a 20 41 20 73 79 6e  L.      /* A syn
219eb 74 61 78 20 65 72 72 6f 72 20 68 61 73 20 6f 63  tax error has oc
219ec 63 75 72 72 65 64 2e 0a 20 20 20 20 20 20 2a 2a  curred..      **
219ed 20 54 68 65 20 72 65 73 70 6f 6e 73 65 20 74 6f   The response to
219ee 20 61 6e 20 65 72 72 6f 72 20 64 65 70 65 6e 64   an error depend
219ef 73 20 75 70 6f 6e 20 77 68 65 74 68 65 72 20 6f  s upon whether o
219f0 72 20 6e 6f 74 20 74 68 65 0a 20 20 20 20 20 20  r not the.      
219f1 2a 2a 20 67 72 61 6d 6d 61 72 20 64 65 66 69 6e  ** grammar defin
219f2 65 73 20 61 6e 20 65 72 72 6f 72 20 74 6f 6b 65  es an error toke
219f3 6e 20 22 45 52 52 4f 52 22 2e 20 20 0a 20 20 20  n "ERROR".  .   
219f4 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54     **.      ** T
219f5 68 69 73 20 69 73 20 77 68 61 74 20 77 65 20 64  his is what we d
219f6 6f 20 69 66 20 74 68 65 20 67 72 61 6d 6d 61 72  o if the grammar
219f7 20 64 6f 65 73 20 64 65 66 69 6e 65 20 45 52 52   does define ERR
219f8 4f 52 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  OR:.      **.   
219f9 20 20 20 2a 2a 20 20 2a 20 43 61 6c 6c 20 74 68     **  * Call th
219fa 65 20 25 73 79 6e 74 61 78 5f 65 72 72 6f 72 20  e %syntax_error 
219fb 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20  function..      
219fc 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 2a 20 42  **.      **  * B
219fd 65 67 69 6e 20 70 6f 70 70 69 6e 67 20 74 68 65  egin popping the
219fe 20 73 74 61 63 6b 20 75 6e 74 69 6c 20 77 65 20   stack until we 
219ff 65 6e 74 65 72 20 61 20 73 74 61 74 65 20 77 68  enter a state wh
21a00 65 72 65 0a 20 20 20 20 20 20 2a 2a 20 20 20 20  ere.      **    
21a01 69 74 20 69 73 20 6c 65 67 61 6c 20 74 6f 20 73  it is legal to s
21a02 68 69 66 74 20 74 68 65 20 65 72 72 6f 72 20 73  hift the error s
21a03 79 6d 62 6f 6c 2c 20 74 68 65 6e 20 73 68 69 66  ymbol, then shif
21a04 74 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 74 68  t.      **    th
21a05 65 20 65 72 72 6f 72 20 73 79 6d 62 6f 6c 2e 0a  e error symbol..
21a06 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
21a07 2a 20 20 2a 20 53 65 74 20 74 68 65 20 65 72 72  *  * Set the err
21a08 6f 72 20 63 6f 75 6e 74 20 74 6f 20 74 68 72 65  or count to thre
21a09 65 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  e..      **.    
21a0a 20 20 2a 2a 20 20 2a 20 42 65 67 69 6e 20 61 63    **  * Begin ac
21a0b 63 65 70 74 69 6e 67 20 61 6e 64 20 73 68 69 66  cepting and shif
21a0c 74 69 6e 67 20 6e 65 77 20 74 6f 6b 65 6e 73 2e  ting new tokens.
21a0d 20 20 4e 6f 20 6e 65 77 20 65 72 72 6f 72 0a 20    No new error. 
21a0e 20 20 20 20 20 2a 2a 20 20 20 20 70 72 6f 63 65       **    proce
21a0f 73 73 69 6e 67 20 77 69 6c 6c 20 6f 63 63 75 72  ssing will occur
21a10 20 75 6e 74 69 6c 20 74 68 72 65 65 20 74 6f 6b   until three tok
21a11 65 6e 73 20 68 61 76 65 20 62 65 65 6e 0a 20 20  ens have been.  
21a12 20 20 20 20 2a 2a 20 20 20 20 73 68 69 66 74 65      **    shifte
21a13 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 0a  d successfully..
21a14 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
21a15 2f 0a 20 20 20 20 20 20 69 66 28 20 79 79 70 50  /.      if( yypP
21a16 61 72 73 65 72 2d 3e 79 79 65 72 72 63 6e 74 3c  arser->yyerrcnt<
21a17 30 20 29 7b 0a 20 20 20 20 20 20 20 20 79 79 5f  0 ){.        yy_
21a18 73 79 6e 74 61 78 5f 65 72 72 6f 72 28 79 79 70  syntax_error(yyp
21a19 50 61 72 73 65 72 2c 79 79 6d 61 6a 6f 72 2c 79  Parser,yymajor,y
21a1a 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20  yminorunion);.  
21a1b 20 20 20 20 7d 0a 20 20 20 20 20 20 79 79 6d 78      }.      yymx
21a1c 20 3d 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79   = yypParser->yy
21a1d 73 74 61 63 6b 5b 79 79 70 50 61 72 73 65 72 2d  stack[yypParser-
21a1e 3e 79 79 69 64 78 5d 2e 6d 61 6a 6f 72 3b 0a 20  >yyidx].major;. 
21a1f 20 20 20 20 20 69 66 28 20 79 79 6d 78 3d 3d 59       if( yymx==Y
21a20 59 45 52 52 4f 52 53 59 4d 42 4f 4c 20 7c 7c 20  YERRORSYMBOL || 
21a21 79 79 65 72 72 6f 72 68 69 74 20 29 7b 0a 23 69  yyerrorhit ){.#i
21a22 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20  fndef NDEBUG.   
21a23 20 20 20 20 20 69 66 28 20 79 79 54 72 61 63 65       if( yyTrace
21a24 46 49 4c 45 20 29 7b 0a 20 20 20 20 20 20 20 20  FILE ){.        
21a25 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63    fprintf(yyTrac
21a26 65 46 49 4c 45 2c 22 25 73 44 69 73 63 61 72 64  eFILE,"%sDiscard
21a27 20 69 6e 70 75 74 20 74 6f 6b 65 6e 20 25 73 5c   input token %s\
21a28 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
21a29 20 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 79   yyTracePrompt,y
21a2a 79 54 6f 6b 65 6e 4e 61 6d 65 5b 79 79 6d 61 6a  yTokenName[yymaj
21a2b 6f 72 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  or]);.        }.
21a2c 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 79  #endif.        y
21a2d 79 5f 64 65 73 74 72 75 63 74 6f 72 28 79 79 6d  y_destructor(yym
21a2e 61 6a 6f 72 2c 26 79 79 6d 69 6e 6f 72 75 6e 69  ajor,&yyminoruni
21a2f 6f 6e 29 3b 0a 20 20 20 20 20 20 20 20 79 79 6d  on);.        yym
21a30 61 6a 6f 72 20 3d 20 59 59 4e 4f 43 4f 44 45 3b  ajor = YYNOCODE;
21a31 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
21a32 20 20 20 20 20 20 20 77 68 69 6c 65 28 0a 20 20         while(.  
21a33 20 20 20 20 20 20 20 20 79 79 70 50 61 72 73 65          yypParse
21a34 72 2d 3e 79 79 69 64 78 20 3e 3d 20 30 20 26 26  r->yyidx >= 0 &&
21a35 0a 20 20 20 20 20 20 20 20 20 20 79 79 6d 78 20  .          yymx 
21a36 21 3d 20 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c  != YYERRORSYMBOL
21a37 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 28 79   &&.          (y
21a38 79 61 63 74 20 3d 20 79 79 5f 66 69 6e 64 5f 72  yact = yy_find_r
21a39 65 64 75 63 65 5f 61 63 74 69 6f 6e 28 0a 20 20  educe_action(.  
21a3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21a3b 20 20 20 20 20 20 79 79 70 50 61 72 73 65 72 2d        yypParser-
21a3c 3e 79 79 73 74 61 63 6b 5b 79 79 70 50 61 72 73  >yystack[yypPars
21a3d 65 72 2d 3e 79 79 69 64 78 5d 2e 73 74 61 74 65  er->yyidx].state
21a3e 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  no,.            
21a3f 20 20 20 20 20 20 20 20 20 20 20 20 59 59 45 52              YYER
21a40 52 4f 52 53 59 4d 42 4f 4c 29 29 20 3e 3d 20 59  RORSYMBOL)) >= Y
21a41 59 4e 53 54 41 54 45 0a 20 20 20 20 20 20 20 20  YNSTATE.        
21a42 29 7b 0a 20 20 20 20 20 20 20 20 20 20 79 79 5f  ){.          yy_
21a43 70 6f 70 5f 70 61 72 73 65 72 5f 73 74 61 63 6b  pop_parser_stack
21a44 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20 20 20  (yypParser);.   
21a45 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
21a46 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79  f( yypParser->yy
21a47 69 64 78 20 3c 20 30 20 7c 7c 20 79 79 6d 61 6a  idx < 0 || yymaj
21a48 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  or==0 ){.       
21a49 20 20 20 79 79 5f 64 65 73 74 72 75 63 74 6f 72     yy_destructor
21a4a 28 79 79 6d 61 6a 6f 72 2c 26 79 79 6d 69 6e 6f  (yymajor,&yymino
21a4b 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20 20  runion);.       
21a4c 20 20 20 79 79 5f 70 61 72 73 65 5f 66 61 69 6c     yy_parse_fail
21a4d 65 64 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20  ed(yypParser);. 
21a4e 20 20 20 20 20 20 20 20 20 79 79 6d 61 6a 6f 72           yymajor
21a4f 20 3d 20 59 59 4e 4f 43 4f 44 45 3b 0a 20 20 20   = YYNOCODE;.   
21a50 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 79       }else if( y
21a51 79 6d 78 21 3d 59 59 45 52 52 4f 52 53 59 4d 42  ymx!=YYERRORSYMB
21a52 4f 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OL ){.          
21a53 59 59 4d 49 4e 4f 52 54 59 50 45 20 75 32 3b 0a  YYMINORTYPE u2;.
21a54 20 20 20 20 20 20 20 20 20 20 75 32 2e 59 59 45            u2.YYE
21a55 52 52 53 59 4d 44 54 20 3d 20 30 3b 0a 20 20 20  RRSYMDT = 0;.   
21a56 20 20 20 20 20 20 20 79 79 5f 73 68 69 66 74 28         yy_shift(
21a57 79 79 70 50 61 72 73 65 72 2c 79 79 61 63 74 2c  yypParser,yyact,
21a58 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 2c 26 75  YYERRORSYMBOL,&u
21a59 32 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  2);.        }.  
21a5a 20 20 20 20 7d 0a 20 20 20 20 20 20 79 79 70 50      }.      yypP
21a5b 61 72 73 65 72 2d 3e 79 79 65 72 72 63 6e 74 20  arser->yyerrcnt 
21a5c 3d 20 33 3b 0a 20 20 20 20 20 20 79 79 65 72 72  = 3;.      yyerr
21a5d 6f 72 68 69 74 20 3d 20 31 3b 0a 23 65 6c 73 65  orhit = 1;.#else
21a5e 20 20 2f 2a 20 59 59 45 52 52 4f 52 53 59 4d 42    /* YYERRORSYMB
21a5f 4f 4c 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65  OL is not define
21a60 64 20 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 54 68  d */.      /* Th
21a61 69 73 20 69 73 20 77 68 61 74 20 77 65 20 64 6f  is is what we do
21a62 20 69 66 20 74 68 65 20 67 72 61 6d 6d 61 72 20   if the grammar 
21a63 64 6f 65 73 20 6e 6f 74 20 64 65 66 69 6e 65 20  does not define 
21a64 45 52 52 4f 52 3a 0a 20 20 20 20 20 20 2a 2a 0a  ERROR:.      **.
21a65 20 20 20 20 20 20 2a 2a 20 20 2a 20 52 65 70 6f        **  * Repo
21a66 72 74 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  rt an error mess
21a67 61 67 65 2c 20 61 6e 64 20 74 68 72 6f 77 20 61  age, and throw a
21a68 77 61 79 20 74 68 65 20 69 6e 70 75 74 20 74 6f  way the input to
21a69 6b 65 6e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20  ken..      **.  
21a6a 20 20 20 20 2a 2a 20 20 2a 20 49 66 20 74 68 65      **  * If the
21a6b 20 69 6e 70 75 74 20 74 6f 6b 65 6e 20 69 73 20   input token is 
21a6c 24 2c 20 74 68 65 6e 20 66 61 69 6c 20 74 68 65  $, then fail the
21a6d 20 70 61 72 73 65 2e 0a 20 20 20 20 20 20 2a 2a   parse..      **
21a6e 0a 20 20 20 20 20 20 2a 2a 20 41 73 20 62 65 66  .      ** As bef
21a6f 6f 72 65 2c 20 73 75 62 73 65 71 75 65 6e 74 20  ore, subsequent 
21a70 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 61  error messages a
21a71 72 65 20 73 75 70 70 72 65 73 73 65 64 20 75 6e  re suppressed un
21a72 74 69 6c 0a 20 20 20 20 20 20 2a 2a 20 74 68 72  til.      ** thr
21a73 65 65 20 69 6e 70 75 74 20 74 6f 6b 65 6e 73 20  ee input tokens 
21a74 68 61 76 65 20 62 65 65 6e 20 73 75 63 63 65 73  have been succes
21a75 73 66 75 6c 6c 79 20 73 68 69 66 74 65 64 2e 0a  sfully shifted..
21a76 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
21a77 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79  f( yypParser->yy
21a78 65 72 72 63 6e 74 3c 3d 30 20 29 7b 0a 20 20 20  errcnt<=0 ){.   
21a79 20 20 20 20 20 79 79 5f 73 79 6e 74 61 78 5f 65       yy_syntax_e
21a7a 72 72 6f 72 28 79 79 70 50 61 72 73 65 72 2c 79  rror(yypParser,y
21a7b 79 6d 61 6a 6f 72 2c 79 79 6d 69 6e 6f 72 75 6e  ymajor,yyminorun
21a7c 69 6f 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ion);.      }.  
21a7d 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79      yypParser->y
21a7e 79 65 72 72 63 6e 74 20 3d 20 33 3b 0a 20 20 20  yerrcnt = 3;.   
21a7f 20 20 20 79 79 5f 64 65 73 74 72 75 63 74 6f 72     yy_destructor
21a80 28 79 79 6d 61 6a 6f 72 2c 26 79 79 6d 69 6e 6f  (yymajor,&yymino
21a81 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20 69  runion);.      i
21a82 66 28 20 79 79 65 6e 64 6f 66 69 6e 70 75 74 20  f( yyendofinput 
21a83 29 7b 0a 20 20 20 20 20 20 20 20 79 79 5f 70 61  ){.        yy_pa
21a84 72 73 65 5f 66 61 69 6c 65 64 28 79 79 70 50 61  rse_failed(yypPa
21a85 72 73 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20  rser);.      }. 
21a86 20 20 20 20 20 79 79 6d 61 6a 6f 72 20 3d 20 59       yymajor = Y
21a87 59 4e 4f 43 4f 44 45 3b 0a 23 65 6e 64 69 66 0a  YNOCODE;.#endif.
21a88 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
21a89 20 79 79 5f 61 63 63 65 70 74 28 79 79 70 50 61   yy_accept(yypPa
21a8a 72 73 65 72 29 3b 0a 20 20 20 20 20 20 79 79 6d  rser);.      yym
21a8b 61 6a 6f 72 20 3d 20 59 59 4e 4f 43 4f 44 45 3b  ajor = YYNOCODE;
21a8c 0a 20 20 20 20 7d 0a 20 20 7d 77 68 69 6c 65 28  .    }.  }while(
21a8d 20 79 79 6d 61 6a 6f 72 21 3d 59 59 4e 4f 43 4f   yymajor!=YYNOCO
21a8e 44 45 20 26 26 20 79 79 70 50 61 72 73 65 72 2d  DE && yypParser-
21a8f 3e 79 79 69 64 78 3e 3d 30 20 29 3b 0a 20 20 72  >yyidx>=0 );.  r
21a90 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  eturn;.}../*****
21a91 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
21a92 20 70 61 72 73 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a   parse.c *******
21a93 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21a94 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21a95 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
21a96 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
21a97 66 69 6c 65 20 74 6f 6b 65 6e 69 7a 65 2e 63 20  file tokenize.c 
21a98 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21a99 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21a9a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
21a9b 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
21a9c 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
21a9d 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
21a9e 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
21a9f 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
21aa0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
21aa1 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
21aa2 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
21aa3 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
21aa4 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
21aa5 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
21aa6 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
21aa7 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
21aa8 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
21aa9 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
21aaa 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
21aab 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
21aac 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
21aad 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
21aae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21aaf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21ab1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21ab2 0a 2a 2a 20 41 6e 20 74 6f 6b 65 6e 69 7a 65 72  .** An tokenizer
21ab3 20 66 6f 72 20 53 51 4c 0a 2a 2a 0a 2a 2a 20 54   for SQL.**.** T
21ab4 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
21ab5 73 20 43 20 63 6f 64 65 20 74 68 61 74 20 73 70  s C code that sp
21ab6 6c 69 74 73 20 61 6e 20 53 51 4c 20 69 6e 70 75  lits an SQL inpu
21ab7 74 20 73 74 72 69 6e 67 20 75 70 20 69 6e 74 6f  t string up into
21ab8 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 74  .** individual t
21ab9 6f 6b 65 6e 73 20 61 6e 64 20 73 65 6e 64 73 20  okens and sends 
21aba 74 68 6f 73 65 20 74 6f 6b 65 6e 73 20 6f 6e 65  those tokens one
21abb 2d 62 79 2d 6f 6e 65 20 6f 76 65 72 20 74 6f 20  -by-one over to 
21abc 74 68 65 0a 2a 2a 20 70 61 72 73 65 72 20 66 6f  the.** parser fo
21abd 72 20 61 6e 61 6c 79 73 69 73 2e 0a 2a 2a 0a 2a  r analysis..**.*
21abe 2a 20 24 49 64 3a 20 74 6f 6b 65 6e 69 7a 65 2e  * $Id: tokenize.
21abf 63 2c 76 20 31 2e 31 33 31 20 32 30 30 37 2f 30  c,v 1.131 2007/0
21ac0 37 2f 32 33 20 31 39 3a 33 31 3a 31 37 20 64 72  7/23 19:31:17 dr
21ac1 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a  h Exp $.*/../*.*
21ac2 2a 20 54 68 65 20 63 68 61 72 4d 61 70 28 29 20  * The charMap() 
21ac3 6d 61 63 72 6f 20 6d 61 70 73 20 61 6c 70 68 61  macro maps alpha
21ac4 62 65 74 69 63 20 63 68 61 72 61 63 74 65 72 73  betic characters
21ac5 20 69 6e 74 6f 20 74 68 65 69 72 0a 2a 2a 20 6c   into their.** l
21ac6 6f 77 65 72 2d 63 61 73 65 20 41 53 43 49 49 20  ower-case ASCII 
21ac7 65 71 75 69 76 61 6c 65 6e 74 2e 20 20 4f 6e 20  equivalent.  On 
21ac8 41 53 43 49 49 20 6d 61 63 68 69 6e 65 73 2c 20  ASCII machines, 
21ac9 74 68 69 73 20 69 73 20 6a 75 73 74 0a 2a 2a 20  this is just.** 
21aca 61 6e 20 75 70 70 65 72 2d 74 6f 2d 6c 6f 77 65  an upper-to-lowe
21acb 72 20 63 61 73 65 20 6d 61 70 2e 20 20 4f 6e 20  r case map.  On 
21acc 45 42 43 44 49 43 20 6d 61 63 68 69 6e 65 73 20  EBCDIC machines 
21acd 77 65 20 61 6c 73 6f 20 6e 65 65 64 0a 2a 2a 20  we also need.** 
21ace 74 6f 20 61 64 6a 75 73 74 20 74 68 65 20 65 6e  to adjust the en
21acf 63 6f 64 69 6e 67 2e 20 20 4f 6e 6c 79 20 61 6c  coding.  Only al
21ad0 70 68 61 62 65 74 69 63 20 63 68 61 72 61 63 74  phabetic charact
21ad1 65 72 73 20 61 6e 64 20 75 6e 64 65 72 73 63 6f  ers and undersco
21ad2 72 65 73 0a 2a 2a 20 6e 65 65 64 20 74 6f 20 62  res.** need to b
21ad3 65 20 74 72 61 6e 73 6c 61 74 65 64 2e 0a 2a 2f  e translated..*/
21ad4 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41  .#ifdef SQLITE_A
21ad5 53 43 49 49 0a 23 20 64 65 66 69 6e 65 20 63 68  SCII.# define ch
21ad6 61 72 4d 61 70 28 58 29 20 73 71 6c 69 74 65 33  arMap(X) sqlite3
21ad7 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 6e  UpperToLower[(un
21ad8 73 69 67 6e 65 64 20 63 68 61 72 29 58 5d 0a 23  signed char)X].#
21ad9 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c  endif.#ifdef SQL
21ada 49 54 45 5f 45 42 43 44 49 43 0a 23 20 64 65 66  ITE_EBCDIC.# def
21adb 69 6e 65 20 63 68 61 72 4d 61 70 28 58 29 20 65  ine charMap(X) e
21adc 62 63 64 69 63 54 6f 41 73 63 69 69 5b 28 75 6e  bcdicToAscii[(un
21add 73 69 67 6e 65 64 20 63 68 61 72 29 58 5d 0a 63  signed char)X].c
21ade 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
21adf 61 72 20 65 62 63 64 69 63 54 6f 41 73 63 69 69  ar ebcdicToAscii
21ae0 5b 5d 20 3d 20 7b 0a 2f 2a 20 30 20 20 20 31 20  [] = {./* 0   1 
21ae1 20 20 32 20 20 20 33 20 20 20 34 20 20 20 35 20    2   3   4   5 
21ae2 20 20 36 20 20 20 37 20 20 20 38 20 20 20 39 20    6   7   8   9 
21ae3 20 20 41 20 20 20 42 20 20 20 43 20 20 20 44 20    A   B   C   D 
21ae4 20 20 45 20 20 20 46 20 2a 2f 0a 20 20 20 30 2c    E   F */.   0,
21ae5 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21ae6 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21ae7 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21ae8 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a    0,  0,  0,  /*
21ae9 20 30 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c   0x */.   0,  0,
21aea 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21aeb 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21aec 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21aed 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 31 78 20    0,  0,  /* 1x 
21aee 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c  */.   0,  0,  0,
21aef 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21af0 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21af1 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21af2 20 20 30 2c 20 20 2f 2a 20 32 78 20 2a 2f 0a 20    0,  /* 2x */. 
21af3 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21af4 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21af5 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21af6 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21af7 20 20 2f 2a 20 33 78 20 2a 2f 0a 20 20 20 30 2c    /* 3x */.   0,
21af8 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21af9 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21afa 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21afb 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a    0,  0,  0,  /*
21afc 20 34 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c   4x */.   0,  0,
21afd 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21afe 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21aff 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21b00 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 35 78 20    0,  0,  /* 5x 
21b01 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c  */.   0,  0,  0,
21b02 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21b03 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21b04 20 20 30 2c 20 20 30 2c 20 39 35 2c 20 20 30 2c    0,  0, 95,  0,
21b05 20 20 30 2c 20 20 2f 2a 20 36 78 20 2a 2f 0a 20    0,  /* 6x */. 
21b06 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21b07 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21b08 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21b09 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21b0a 20 20 2f 2a 20 37 78 20 2a 2f 0a 20 20 20 30 2c    /* 7x */.   0,
21b0b 20 39 37 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c   97, 98, 99,100,
21b0c 31 30 31 2c 31 30 32 2c 31 30 33 2c 31 30 34 2c  101,102,103,104,
21b0d 31 30 35 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c  105,  0,  0,  0,
21b0e 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a    0,  0,  0,  /*
21b0f 20 38 78 20 2a 2f 0a 20 20 20 30 2c 31 30 36 2c   8x */.   0,106,
21b10 31 30 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c  107,108,109,110,
21b11 31 31 31 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c  111,112,113,114,
21b12 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21b13 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 39 78 20    0,  0,  /* 9x 
21b14 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 31 31 35 2c  */.   0,  0,115,
21b15 31 31 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c  116,117,118,119,
21b16 31 32 30 2c 31 32 31 2c 31 32 32 2c 20 20 30 2c  120,121,122,  0,
21b17 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21b18 20 20 30 2c 20 20 2f 2a 20 41 78 20 2a 2f 0a 20    0,  /* Ax */. 
21b19 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21b1a 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21b1b 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21b1c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21b1d 20 20 2f 2a 20 42 78 20 2a 2f 0a 20 20 20 30 2c    /* Bx */.   0,
21b1e 20 39 37 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c   97, 98, 99,100,
21b1f 31 30 31 2c 31 30 32 2c 31 30 33 2c 31 30 34 2c  101,102,103,104,
21b20 31 30 35 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c  105,  0,  0,  0,
21b21 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a    0,  0,  0,  /*
21b22 20 43 78 20 2a 2f 0a 20 20 20 30 2c 31 30 36 2c   Cx */.   0,106,
21b23 31 30 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c  107,108,109,110,
21b24 31 31 31 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c  111,112,113,114,
21b25 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21b26 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 44 78 20    0,  0,  /* Dx 
21b27 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 31 31 35 2c  */.   0,  0,115,
21b28 31 31 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c  116,117,118,119,
21b29 31 32 30 2c 31 32 31 2c 31 32 32 2c 20 20 30 2c  120,121,122,  0,
21b2a 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21b2b 20 20 30 2c 20 20 2f 2a 20 45 78 20 2a 2f 0a 20    0,  /* Ex */. 
21b2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21b2d 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21b2e 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21b2f 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
21b30 20 20 2f 2a 20 46 78 20 2a 2f 0a 7d 3b 0a 23 65    /* Fx */.};.#e
21b31 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
21b32 73 71 6c 69 74 65 33 4b 65 79 77 6f 72 64 43 6f  sqlite3KeywordCo
21b33 64 65 20 66 75 6e 63 74 69 6f 6e 20 6c 6f 6f 6b  de function look
21b34 73 20 75 70 20 61 6e 20 69 64 65 6e 74 69 66 69  s up an identifi
21b35 65 72 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  er to determine 
21b36 69 66 0a 2a 2a 20 69 74 20 69 73 20 61 20 6b 65  if.** it is a ke
21b37 79 77 6f 72 64 2e 20 20 49 66 20 69 74 20 69 73  yword.  If it is
21b38 20 61 20 6b 65 79 77 6f 72 64 2c 20 74 68 65 20   a keyword, the 
21b39 74 6f 6b 65 6e 20 63 6f 64 65 20 6f 66 20 74 68  token code of th
21b3a 61 74 20 6b 65 79 77 6f 72 64 20 69 73 20 0a 2a  at keyword is .*
21b3b 2a 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20  * returned.  If 
21b3c 74 68 65 20 69 6e 70 75 74 20 69 73 20 6e 6f 74  the input is not
21b3d 20 61 20 6b 65 79 77 6f 72 64 2c 20 54 4b 5f 49   a keyword, TK_I
21b3e 44 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  D is returned..*
21b3f 2a 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65 6d 65  *.** The impleme
21b40 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20  ntation of this 
21b41 72 6f 75 74 69 6e 65 20 77 61 73 20 67 65 6e 65  routine was gene
21b42 72 61 74 65 64 20 62 79 20 61 20 70 72 6f 67 72  rated by a progr
21b43 61 6d 2c 0a 2a 2a 20 6d 6b 6b 65 79 77 6f 72 64  am,.** mkkeyword
21b44 68 61 73 68 2e 68 2c 20 6c 6f 63 61 74 65 64 20  hash.h, located 
21b45 69 6e 20 74 68 65 20 74 6f 6f 6c 20 73 75 62 64  in the tool subd
21b46 69 72 65 63 74 6f 72 79 20 6f 66 20 74 68 65 20  irectory of the 
21b47 64 69 73 74 72 69 62 75 74 69 6f 6e 2e 0a 2a 2a  distribution..**
21b48 20 54 68 65 20 6f 75 74 70 75 74 20 6f 66 20 74   The output of t
21b49 68 65 20 6d 6b 6b 65 79 77 6f 72 64 68 61 73 68  he mkkeywordhash
21b4a 2e 63 20 70 72 6f 67 72 61 6d 20 69 73 20 77 72  .c program is wr
21b4b 69 74 74 65 6e 20 69 6e 74 6f 20 61 20 66 69 6c  itten into a fil
21b4c 65 0a 2a 2a 20 6e 61 6d 65 64 20 6b 65 79 77 6f  e.** named keywo
21b4d 72 64 68 61 73 68 2e 68 20 61 6e 64 20 74 68 65  rdhash.h and the
21b4e 6e 20 69 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20  n included into 
21b4f 74 68 69 73 20 73 6f 75 72 63 65 20 66 69 6c 65  this source file
21b50 20 62 79 0a 2a 2a 20 74 68 65 20 23 69 6e 63 6c   by.** the #incl
21b51 75 64 65 20 62 65 6c 6f 77 2e 0a 2a 2f 0a 2f 2a  ude below..*/./*
21b52 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
21b53 63 6c 75 64 65 20 6b 65 79 77 6f 72 64 68 61 73  clude keywordhas
21b54 68 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  h.h in the middl
21b55 65 20 6f 66 20 74 6f 6b 65 6e 69 7a 65 2e 63 20  e of tokenize.c 
21b56 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
21b57 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
21b58 67 69 6e 20 66 69 6c 65 20 6b 65 79 77 6f 72 64  gin file keyword
21b59 68 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  hash.h *********
21b5a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21b5b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
21b5c 2a 2a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  **** This file c
21b5d 6f 6e 74 61 69 6e 73 20 61 75 74 6f 6d 61 74 69  ontains automati
21b5e 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 20  cally generated 
21b5f 63 6f 64 65 20 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  code ******.**.*
21b60 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68  * The code in th
21b61 69 73 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e  is file has been
21b62 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67   automatically g
21b63 65 6e 65 72 61 74 65 64 20 62 79 0a 2a 2a 0a 2a  enerated by.**.*
21b64 2a 20 20 20 20 20 24 48 65 61 64 65 72 3a 20 2f  *     $Header: /
21b65 73 71 6c 69 74 65 2f 73 71 6c 69 74 65 2f 74 6f  sqlite/sqlite/to
21b66 6f 6c 2f 6d 6b 6b 65 79 77 6f 72 64 68 61 73 68  ol/mkkeywordhash
21b67 2e 63 2c 76 20 31 2e 33 30 20 32 30 30 37 2f 30  .c,v 1.30 2007/0
21b68 35 2f 30 34 20 31 38 3a 33 30 3a 34 31 20 64 72  5/04 18:30:41 dr
21b69 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68  h Exp $.**.** Th
21b6a 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66  e code in this f
21b6b 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61  ile implements a
21b6c 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 64   function that d
21b6d 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74 68 65  etermines whethe
21b6e 72 0a 2a 2a 20 6f 72 20 6e 6f 74 20 61 20 67 69  r.** or not a gi
21b6f 76 65 6e 20 69 64 65 6e 74 69 66 69 65 72 20 69  ven identifier i
21b70 73 20 72 65 61 6c 6c 79 20 61 6e 20 53 51 4c 20  s really an SQL 
21b71 6b 65 79 77 6f 72 64 2e 20 20 54 68 65 20 73 61  keyword.  The sa
21b72 6d 65 20 74 68 69 6e 67 0a 2a 2a 20 6d 69 67 68  me thing.** migh
21b73 74 20 62 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64  t be implemented
21b74 20 6d 6f 72 65 20 64 69 72 65 63 74 6c 79 20 75   more directly u
21b75 73 69 6e 67 20 61 20 68 61 6e 64 2d 77 72 69 74  sing a hand-writ
21b76 74 65 6e 20 68 61 73 68 20 74 61 62 6c 65 2e 0a  ten hash table..
21b77 2a 2a 20 42 75 74 20 62 79 20 75 73 69 6e 67 20  ** But by using 
21b78 74 68 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c  this automatical
21b79 6c 79 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64  ly generated cod
21b7a 65 2c 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  e, the size of t
21b7b 68 65 20 63 6f 64 65 0a 2a 2a 20 69 73 20 73 75  he code.** is su
21b7c 62 73 74 61 6e 74 69 61 6c 6c 79 20 72 65 64 75  bstantially redu
21b7d 63 65 64 2e 20 20 54 68 69 73 20 69 73 20 69 6d  ced.  This is im
21b7e 70 6f 72 74 61 6e 74 20 66 6f 72 20 65 6d 62 65  portant for embe
21b7f 64 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e  dded application
21b80 73 0a 2a 2a 20 6f 6e 20 70 6c 61 74 66 6f 72 6d  s.** on platform
21b81 73 20 77 69 74 68 20 6c 69 6d 69 74 65 64 20 6d  s with limited m
21b82 65 6d 6f 72 79 2e 0a 2a 2f 0a 2f 2a 20 48 61 73  emory..*/./* Has
21b83 68 20 73 63 6f 72 65 3a 20 31 36 35 20 2a 2f 0a  h score: 165 */.
21b84 73 74 61 74 69 63 20 69 6e 74 20 6b 65 79 77 6f  static int keywo
21b85 72 64 43 6f 64 65 28 63 6f 6e 73 74 20 63 68 61  rdCode(const cha
21b86 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20  r *z, int n){.  
21b87 2f 2a 20 7a 54 65 78 74 5b 5d 20 65 6e 63 6f 64  /* zText[] encod
21b88 65 73 20 37 37 35 20 62 79 74 65 73 20 6f 66 20  es 775 bytes of 
21b89 6b 65 79 77 6f 72 64 73 20 69 6e 20 35 32 36 20  keywords in 526 
21b8a 62 79 74 65 73 20 2a 2f 0a 20 20 73 74 61 74 69  bytes */.  stati
21b8b 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 54 65  c const char zTe
21b8c 78 74 5b 35 32 36 5d 20 3d 0a 20 20 20 20 22 42  xt[526] =.    "B
21b8d 45 46 4f 52 45 49 47 4e 4f 52 45 47 45 58 50 4c  EFOREIGNOREGEXPL
21b8e 41 49 4e 53 54 45 41 44 44 45 53 43 41 50 45 41  AINSTEADDESCAPEA
21b8f 43 48 45 43 4b 45 59 43 4f 4e 53 54 52 41 49 4e  CHECKEYCONSTRAIN
21b90 54 45 52 53 45 43 54 41 42 4c 45 46 54 22 0a 20  TERSECTABLEFT". 
21b91 20 20 20 22 48 45 4e 44 41 54 41 42 41 53 45 4c     "HENDATABASEL
21b92 45 43 54 52 41 4e 53 41 43 54 49 4f 4e 41 54 55  ECTRANSACTIONATU
21b93 52 41 4c 54 45 52 41 49 53 45 4c 53 45 58 43 45  RALTERAISELSEXCE
21b94 50 54 52 49 47 47 45 52 45 46 45 52 45 4e 43 45  PTRIGGEREFERENCE
21b95 53 22 0a 20 20 20 20 22 55 4e 49 51 55 45 52 59  S".    "UNIQUERY
21b96 41 54 54 41 43 48 41 56 49 4e 47 52 4f 55 50 44  ATTACHAVINGROUPD
21b97 41 54 45 4d 50 4f 52 41 52 59 42 45 47 49 4e 4e  ATEMPORARYBEGINN
21b98 45 52 45 49 4e 44 45 58 43 4c 55 53 49 56 45 58  EREINDEXCLUSIVEX
21b99 49 53 54 53 42 45 54 57 45 45 4e 22 0a 20 20 20  ISTSBETWEEN".   
21b9a 20 22 4f 54 4e 55 4c 4c 49 4b 45 43 41 53 43 41   "OTNULLIKECASCA
21b9b 44 45 46 45 52 52 41 42 4c 45 43 41 53 45 43 4f  DEFERRABLECASECO
21b9c 4c 4c 41 54 45 43 52 45 41 54 45 43 55 52 52 45  LLATECREATECURRE
21b9d 4e 54 5f 44 41 54 45 44 45 4c 45 54 45 44 45 54  NT_DATEDELETEDET
21b9e 41 43 48 22 0a 20 20 20 20 22 49 4d 4d 45 44 49  ACH".    "IMMEDI
21b9f 41 54 45 4a 4f 49 4e 53 45 52 54 4d 41 54 43 48  ATEJOINSERTMATCH
21ba0 50 4c 41 4e 41 4c 59 5a 45 50 52 41 47 4d 41 42  PLANALYZEPRAGMAB
21ba1 4f 52 54 56 41 4c 55 45 53 56 49 52 54 55 41 4c  ORTVALUESVIRTUAL
21ba2 49 4d 49 54 57 48 45 4e 22 0a 20 20 20 20 22 57  IMITWHEN".    "W
21ba3 48 45 52 45 4e 41 4d 45 41 46 54 45 52 45 50 4c  HERENAMEAFTEREPL
21ba4 41 43 45 41 4e 44 45 46 41 55 4c 54 41 55 54 4f  ACEANDEFAULTAUTO
21ba5 49 4e 43 52 45 4d 45 4e 54 43 41 53 54 43 4f 4c  INCREMENTCASTCOL
21ba6 55 4d 4e 43 4f 4d 4d 49 54 43 4f 4e 46 4c 49 43  UMNCOMMITCONFLIC
21ba7 54 22 0a 20 20 20 20 22 43 52 4f 53 53 43 55 52  T".    "CROSSCUR
21ba8 52 45 4e 54 5f 54 49 4d 45 53 54 41 4d 50 52 49  RENT_TIMESTAMPRI
21ba9 4d 41 52 59 44 45 46 45 52 52 45 44 49 53 54 49  MARYDEFERREDISTI
21baa 4e 43 54 44 52 4f 50 46 41 49 4c 46 52 4f 4d 46  NCTDROPFAILFROMF
21bab 55 4c 4c 47 4c 4f 42 22 0a 20 20 20 20 22 59 49  ULLGLOB".    "YI
21bac 46 49 4e 54 4f 46 46 53 45 54 49 53 4e 55 4c 4c  FINTOFFSETISNULL
21bad 4f 52 44 45 52 45 53 54 52 49 43 54 4f 55 54 45  ORDERESTRICTOUTE
21bae 52 49 47 48 54 52 4f 4c 4c 42 41 43 4b 52 4f 57  RIGHTROLLBACKROW
21baf 55 4e 49 4f 4e 55 53 49 4e 47 56 41 43 55 55 4d  UNIONUSINGVACUUM
21bb0 22 0a 20 20 20 20 22 56 49 45 57 49 4e 49 54 49  ".    "VIEWINITI
21bb1 41 4c 4c 59 22 3b 0a 20 20 73 74 61 74 69 63 20  ALLY";.  static 
21bb2 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
21bb3 68 61 72 20 61 48 61 73 68 5b 31 32 37 5d 20 3d  har aHash[127] =
21bb4 20 7b 0a 20 20 20 20 20 20 36 33 2c 20 20 39 32   {.      63,  92
21bb5 2c 20 31 30 39 2c 20 20 36 31 2c 20 20 20 30 2c  , 109,  61,   0,
21bb6 20 20 33 38 2c 20 20 20 30 2c 20 20 20 30 2c 20    38,   0,   0, 
21bb7 20 36 39 2c 20 20 20 30 2c 20 20 36 34 2c 20 20   69,   0,  64,  
21bb8 20 30 2c 20 20 20 30 2c 0a 20 20 20 20 20 31 30   0,   0,.     10
21bb9 32 2c 20 20 20 34 2c 20 20 36 35 2c 20 20 20 37  2,   4,  65,   7
21bba 2c 20 20 20 30 2c 20 31 30 38 2c 20 20 37 32 2c  ,   0, 108,  72,
21bbb 20 31 30 33 2c 20 20 39 39 2c 20 20 20 30 2c 20   103,  99,   0, 
21bbc 20 32 32 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20   22,   0,   0,. 
21bbd 20 20 20 20 31 31 33 2c 20 20 20 30 2c 20 31 31      113,   0, 11
21bbe 31 2c 20 31 30 36 2c 20 20 20 30 2c 20 20 31 38  1, 106,   0,  18
21bbf 2c 20 20 38 30 2c 20 20 20 30 2c 20 20 20 31 2c  ,  80,   0,   1,
21bc0 20 20 20 30 2c 20 20 20 30 2c 20 20 35 36 2c 20     0,   0,  56, 
21bc1 20 35 37 2c 0a 20 20 20 20 20 20 20 30 2c 20 20   57,.       0,  
21bc2 35 35 2c 20 20 31 31 2c 20 20 20 30 2c 20 20 33  55,  11,   0,  3
21bc3 33 2c 20 20 37 37 2c 20 20 38 39 2c 20 20 20 30  3,  77,  89,   0
21bc4 2c 20 31 31 30 2c 20 20 38 38 2c 20 20 20 30 2c  , 110,  88,   0,
21bc5 20 20 20 30 2c 20 20 34 35 2c 0a 20 20 20 20 20     0,  45,.     
21bc6 20 20 30 2c 20 20 39 30 2c 20 20 35 34 2c 20 20    0,  90,  54,  
21bc7 20 30 2c 20 20 32 30 2c 20 20 20 30 2c 20 31 31   0,  20,   0, 11
21bc8 34 2c 20 20 33 34 2c 20 20 31 39 2c 20 20 20 30  4,  34,  19,   0
21bc9 2c 20 20 31 30 2c 20 20 39 37 2c 20 20 32 38 2c  ,  10,  97,  28,
21bca 0a 20 20 20 20 20 20 38 33 2c 20 20 20 30 2c 20  .      83,   0, 
21bcb 20 20 30 2c 20 31 31 36 2c 20 20 39 33 2c 20 20    0, 116,  93,  
21bcc 34 37 2c 20 31 31 35 2c 20 20 34 31 2c 20 20 31  47, 115,  41,  1
21bcd 32 2c 20 20 34 34 2c 20 20 20 30 2c 20 20 37 38  2,  44,   0,  78
21bce 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 38 37 2c  ,   0,.      87,
21bcf 20 20 32 39 2c 20 20 20 30 2c 20 20 38 36 2c 20    29,   0,  86, 
21bd0 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
21bd1 38 32 2c 20 20 37 39 2c 20 20 38 34 2c 20 20 37  82,  79,  84,  7
21bd2 35 2c 20 20 39 36 2c 20 20 20 36 2c 0a 20 20 20  5,  96,   6,.   
21bd3 20 20 20 31 34 2c 20 20 39 35 2c 20 20 20 30 2c     14,  95,   0,
21bd4 20 20 36 38 2c 20 20 20 30 2c 20 20 32 31 2c 20    68,   0,  21, 
21bd5 20 37 36 2c 20 20 39 38 2c 20 20 32 37 2c 20 20   76,  98,  27,  
21bd6 20 30 2c 20 31 31 32 2c 20 20 36 37 2c 20 31 30   0, 112,  67, 10
21bd7 34 2c 0a 20 20 20 20 20 20 34 39 2c 20 20 34 30  4,.      49,  40
21bd8 2c 20 20 37 31 2c 20 20 20 30 2c 20 20 20 30 2c  ,  71,   0,   0,
21bd9 20 20 38 31 2c 20 31 30 30 2c 20 20 20 30 2c 20    81, 100,   0, 
21bda 31 30 37 2c 20 20 20 30 2c 20 20 31 35 2c 20 20  107,   0,  15,  
21bdb 20 30 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 32   0,   0,.      2
21bdc 34 2c 20 20 20 30 2c 20 20 37 33 2c 20 20 34 32  4,   0,  73,  42
21bdd 2c 20 20 35 30 2c 20 20 20 30 2c 20 20 31 36 2c  ,  50,   0,  16,
21bde 20 20 34 38 2c 20 20 20 30 2c 20 20 33 37 2c 0a    48,   0,  37,.
21bdf 20 20 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f    };.  static co
21be0 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
21be1 72 20 61 4e 65 78 74 5b 31 31 36 5d 20 3d 20 7b  r aNext[116] = {
21be2 0a 20 20 20 20 20 20 20 30 2c 20 20 20 30 2c 20  .       0,   0, 
21be3 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
21be4 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   0,   0,   0,   
21be5 30 2c 20 20 20 39 2c 20 20 20 30 2c 20 20 20 30  0,   9,   0,   0
21be6 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 20 30 2c  ,   0,.       0,
21be7 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
21be8 20 20 35 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    5,   0,   0,  
21be9 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   0,   0,   0,   
21bea 30 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20 20 20  0,   0,   0,.   
21beb 20 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c      0,   0,   0,
21bec 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
21bed 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
21bee 20 30 2c 20 20 33 32 2c 20 20 20 30 2c 20 20 20   0,  32,   0,   
21bef 30 2c 0a 20 20 20 20 20 20 31 37 2c 20 20 20 30  0,.      17,   0
21bf0 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 33 36 2c  ,   0,   0,  36,
21bf1 20 20 33 39 2c 20 20 20 30 2c 20 20 20 30 2c 20    39,   0,   0, 
21bf2 20 32 35 2c 20 20 20 30 2c 20 20 20 30 2c 20 20   25,   0,   0,  
21bf3 33 31 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 20  31,   0,.       
21bf4 30 2c 20 20 20 30 2c 20 20 34 33 2c 20 20 35 32  0,   0,  43,  52
21bf5 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
21bf6 20 20 35 33 2c 20 20 20 30 2c 20 20 20 30 2c 20    53,   0,   0, 
21bf7 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20    0,   0,   0,. 
21bf8 20 20 20 20 20 20 30 2c 20 20 20 30 2c 20 20 20        0,   0,   
21bf9 30 2c 20 20 20 30 2c 20 20 35 31 2c 20 20 20 30  0,   0,  51,   0
21bfa 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
21bfb 20 20 32 36 2c 20 20 20 30 2c 20 20 20 38 2c 20    26,   0,   8, 
21bfc 20 34 36 2c 0a 20 20 20 20 20 20 20 32 2c 20 20   46,.       2,  
21bfd 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   0,   0,   0,   
21bfe 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
21bff 2c 20 20 20 33 2c 20 20 35 38 2c 20 20 36 36 2c  ,   3,  58,  66,
21c00 20 20 20 30 2c 20 20 31 33 2c 0a 20 20 20 20 20     0,  13,.     
21c01 20 20 30 2c 20 20 39 31 2c 20 20 38 35 2c 20 20    0,  91,  85,  
21c02 20 30 2c 20 20 39 34 2c 20 20 20 30 2c 20 20 37   0,  94,   0,  7
21c03 34 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 36 32  4,   0,   0,  62
21c04 2c 20 20 20 30 2c 20 20 33 35 2c 20 31 30 31 2c  ,   0,  35, 101,
21c05 0a 20 20 20 20 20 20 20 30 2c 20 20 20 30 2c 20  .       0,   0, 
21c06 31 30 35 2c 20 20 32 33 2c 20 20 33 30 2c 20 20  105,  23,  30,  
21c07 36 30 2c 20 20 37 30 2c 20 20 20 30 2c 20 20 20  60,  70,   0,   
21c08 30 2c 20 20 35 39 2c 20 20 20 30 2c 20 20 20 30  0,  59,   0,   0
21c09 2c 0a 20 20 7d 3b 0a 20 20 73 74 61 74 69 63 20  ,.  };.  static 
21c0a 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
21c0b 68 61 72 20 61 4c 65 6e 5b 31 31 36 5d 20 3d 20  har aLen[116] = 
21c0c 7b 0a 20 20 20 20 20 20 20 36 2c 20 20 20 37 2c  {.       6,   7,
21c0d 20 20 20 33 2c 20 20 20 36 2c 20 20 20 36 2c 20     3,   6,   6, 
21c0e 20 20 37 2c 20 20 20 37 2c 20 20 20 33 2c 20 20    7,   7,   3,  
21c0f 20 34 2c 20 20 20 36 2c 20 20 20 34 2c 20 20 20   4,   6,   4,   
21c10 35 2c 20 20 20 33 2c 0a 20 20 20 20 20 20 31 30  5,   3,.      10
21c11 2c 20 20 20 39 2c 20 20 20 35 2c 20 20 20 34 2c  ,   9,   5,   4,
21c12 20 20 20 34 2c 20 20 20 33 2c 20 20 20 38 2c 20     4,   3,   8, 
21c13 20 20 32 2c 20 20 20 36 2c 20 20 31 31 2c 20 20    2,   6,  11,  
21c14 20 32 2c 20 20 20 37 2c 20 20 20 35 2c 0a 20 20   2,   7,   5,.  
21c15 20 20 20 20 20 35 2c 20 20 20 34 2c 20 20 20 36       5,   4,   6
21c16 2c 20 20 20 37 2c 20 20 31 30 2c 20 20 20 36 2c  ,   7,  10,   6,
21c17 20 20 20 35 2c 20 20 20 36 2c 20 20 20 36 2c 20     5,   6,   6, 
21c18 20 20 35 2c 20 20 20 36 2c 20 20 20 34 2c 20 20    5,   6,   4,  
21c19 20 39 2c 0a 20 20 20 20 20 20 20 32 2c 20 20 20   9,.       2,   
21c1a 35 2c 20 20 20 35 2c 20 20 20 37 2c 20 20 20 35  5,   5,   7,   5
21c1b 2c 20 20 20 39 2c 20 20 20 36 2c 20 20 20 37 2c  ,   9,   6,   7,
21c1c 20 20 20 37 2c 20 20 20 33 2c 20 20 20 34 2c 20     7,   3,   4, 
21c1d 20 20 34 2c 20 20 20 37 2c 0a 20 20 20 20 20 20    4,   7,.      
21c1e 20 33 2c 20 20 31 30 2c 20 20 20 34 2c 20 20 20   3,  10,   4,   
21c1f 37 2c 20 20 20 36 2c 20 20 31 32 2c 20 20 20 36  7,   6,  12,   6
21c20 2c 20 20 20 36 2c 20 20 20 39 2c 20 20 20 34 2c  ,   6,   9,   4,
21c21 20 20 20 36 2c 20 20 20 35 2c 20 20 20 34 2c 0a     6,   5,   4,.
21c22 20 20 20 20 20 20 20 37 2c 20 20 20 36 2c 20 20         7,   6,  
21c23 20 35 2c 20 20 20 36 2c 20 20 20 37 2c 20 20 20   5,   6,   7,   
21c24 35 2c 20 20 20 34 2c 20 20 20 35 2c 20 20 20 36  5,   4,   5,   6
21c25 2c 20 20 20 35 2c 20 20 20 37 2c 20 20 20 33 2c  ,   5,   7,   3,
21c26 20 20 20 37 2c 0a 20 20 20 20 20 20 31 33 2c 20     7,.      13, 
21c27 20 20 32 2c 20 20 20 32 2c 20 20 20 34 2c 20 20    2,   2,   4,  
21c28 20 36 2c 20 20 20 36 2c 20 20 20 38 2c 20 20 20   6,   6,   8,   
21c29 35 2c 20 20 31 37 2c 20 20 31 32 2c 20 20 20 37  5,  17,  12,   7
21c2a 2c 20 20 20 38 2c 20 20 20 38 2c 0a 20 20 20 20  ,   8,   8,.    
21c2b 20 20 20 32 2c 20 20 20 34 2c 20 20 20 34 2c 20     2,   4,   4, 
21c2c 20 20 34 2c 20 20 20 34 2c 20 20 20 34 2c 20 20    4,   4,   4,  
21c2d 20 32 2c 20 20 20 32 2c 20 20 20 34 2c 20 20 20   2,   2,   4,   
21c2e 36 2c 20 20 20 32 2c 20 20 20 33 2c 20 20 20 36  6,   2,   3,   6
21c2f 2c 0a 20 20 20 20 20 20 20 35 2c 20 20 20 38 2c  ,.       5,   8,
21c30 20 20 20 35 2c 20 20 20 35 2c 20 20 20 38 2c 20     5,   5,   8, 
21c31 20 20 33 2c 20 20 20 35 2c 20 20 20 35 2c 20 20    3,   5,   5,  
21c32 20 36 2c 20 20 20 34 2c 20 20 20 39 2c 20 20 20   6,   4,   9,   
21c33 33 2c 0a 20 20 7d 3b 0a 20 20 73 74 61 74 69 63  3,.  };.  static
21c34 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
21c35 73 68 6f 72 74 20 69 6e 74 20 61 4f 66 66 73 65  short int aOffse
21c36 74 5b 31 31 36 5d 20 3d 20 7b 0a 20 20 20 20 20  t[116] = {.     
21c37 20 20 30 2c 20 20 20 32 2c 20 20 20 32 2c 20 20    0,   2,   2,  
21c38 20 36 2c 20 20 31 30 2c 20 20 31 33 2c 20 20 31   6,  10,  13,  1
21c39 38 2c 20 20 32 33 2c 20 20 32 35 2c 20 20 32 36  8,  23,  25,  26
21c3a 2c 20 20 33 31 2c 20 20 33 33 2c 20 20 33 37 2c  ,  31,  33,  37,
21c3b 0a 20 20 20 20 20 20 34 30 2c 20 20 34 37 2c 20  .      40,  47, 
21c3c 20 35 35 2c 20 20 35 38 2c 20 20 36 31 2c 20 20   55,  58,  61,  
21c3d 36 33 2c 20 20 36 35 2c 20 20 37 30 2c 20 20 37  63,  65,  70,  7
21c3e 31 2c 20 20 37 36 2c 20 20 38 35 2c 20 20 38 36  1,  76,  85,  86
21c3f 2c 20 20 39 31 2c 0a 20 20 20 20 20 20 39 35 2c  ,  91,.      95,
21c40 20 20 39 39 2c 20 31 30 32 2c 20 31 30 37 2c 20    99, 102, 107, 
21c41 31 31 33 2c 20 31 32 33 2c 20 31 32 36 2c 20 31  113, 123, 126, 1
21c42 33 31 2c 20 31 33 36 2c 20 31 34 31 2c 20 31 34  31, 136, 141, 14
21c43 34 2c 20 31 34 38 2c 20 31 34 38 2c 0a 20 20 20  4, 148, 148,.   
21c44 20 20 31 35 32 2c 20 31 35 37 2c 20 31 36 30 2c    152, 157, 160,
21c45 20 31 36 34 2c 20 31 36 36 2c 20 31 36 39 2c 20   164, 166, 169, 
21c46 31 37 37 2c 20 31 38 33 2c 20 31 38 39 2c 20 31  177, 183, 189, 1
21c47 38 39 2c 20 31 39 32 2c 20 31 39 35 2c 20 31 39  89, 192, 195, 19
21c48 39 2c 0a 20 20 20 20 20 32 30 30 2c 20 32 30 34  9,.     200, 204
21c49 2c 20 32 31 34 2c 20 32 31 38 2c 20 32 32 35 2c  , 214, 218, 225,
21c4a 20 32 33 31 2c 20 32 34 33 2c 20 32 34 39 2c 20   231, 243, 249, 
21c4b 32 35 35 2c 20 32 36 34 2c 20 32 36 36 2c 20 32  255, 264, 266, 2
21c4c 37 32 2c 20 32 37 37 2c 0a 20 20 20 20 20 32 37  72, 277,.     27
21c4d 39 2c 20 32 38 36 2c 20 32 39 31 2c 20 32 39 36  9, 286, 291, 296
21c4e 2c 20 33 30 32 2c 20 33 30 38 2c 20 33 31 33 2c  , 302, 308, 313,
21c4f 20 33 31 37 2c 20 33 32 30 2c 20 33 32 36 2c 20   317, 320, 326, 
21c50 33 33 30 2c 20 33 33 37 2c 20 33 33 39 2c 0a 20  330, 337, 339,. 
21c51 20 20 20 20 33 34 36 2c 20 33 34 38 2c 20 33 35      346, 348, 35
21c52 30 2c 20 33 35 39 2c 20 33 36 33 2c 20 33 36 39  0, 359, 363, 369
21c53 2c 20 33 37 35 2c 20 33 38 33 2c 20 33 38 38 2c  , 375, 383, 388,
21c54 20 33 38 38 2c 20 34 30 34 2c 20 34 31 31 2c 20   388, 404, 411, 
21c55 34 31 38 2c 0a 20 20 20 20 20 34 31 39 2c 20 34  418,.     419, 4
21c56 32 36 2c 20 34 33 30 2c 20 34 33 34 2c 20 34 33  26, 430, 434, 43
21c57 38 2c 20 34 34 32 2c 20 34 34 35 2c 20 34 34 37  8, 442, 445, 447
21c58 2c 20 34 34 39 2c 20 34 35 32 2c 20 34 35 32 2c  , 449, 452, 452,
21c59 20 34 35 35 2c 20 34 35 38 2c 0a 20 20 20 20 20   455, 458,.     
21c5a 34 36 34 2c 20 34 36 38 2c 20 34 37 36 2c 20 34  464, 468, 476, 4
21c5b 38 30 2c 20 34 38 35 2c 20 34 39 33 2c 20 34 39  80, 485, 493, 49
21c5c 36 2c 20 35 30 31 2c 20 35 30 36 2c 20 35 31 32  6, 501, 506, 512
21c5d 2c 20 35 31 36 2c 20 35 32 31 2c 0a 20 20 7d 3b  , 516, 521,.  };
21c5e 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
21c5f 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61 43  unsigned char aC
21c60 6f 64 65 5b 31 31 36 5d 20 3d 20 7b 0a 20 20 20  ode[116] = {.   
21c61 20 54 4b 5f 42 45 46 4f 52 45 2c 20 20 20 20 20   TK_BEFORE,     
21c62 54 4b 5f 46 4f 52 45 49 47 4e 2c 20 20 20 20 54  TK_FOREIGN,    T
21c63 4b 5f 46 4f 52 2c 20 20 20 20 20 20 20 20 54 4b  K_FOR,        TK
21c64 5f 49 47 4e 4f 52 45 2c 20 20 20 20 20 54 4b 5f  _IGNORE,     TK_
21c65 4c 49 4b 45 5f 4b 57 2c 20 20 20 20 0a 20 20 20  LIKE_KW,    .   
21c66 20 54 4b 5f 45 58 50 4c 41 49 4e 2c 20 20 20 20   TK_EXPLAIN,    
21c67 54 4b 5f 49 4e 53 54 45 41 44 2c 20 20 20 20 54  TK_INSTEAD,    T
21c68 4b 5f 41 44 44 2c 20 20 20 20 20 20 20 20 54 4b  K_ADD,        TK
21c69 5f 44 45 53 43 2c 20 20 20 20 20 20 20 54 4b 5f  _DESC,       TK_
21c6a 45 53 43 41 50 45 2c 20 20 20 20 20 0a 20 20 20  ESCAPE,     .   
21c6b 20 54 4b 5f 45 41 43 48 2c 20 20 20 20 20 20 20   TK_EACH,       
21c6c 54 4b 5f 43 48 45 43 4b 2c 20 20 20 20 20 20 54  TK_CHECK,      T
21c6d 4b 5f 4b 45 59 2c 20 20 20 20 20 20 20 20 54 4b  K_KEY,        TK
21c6e 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20 54 4b 5f  _CONSTRAINT, TK_
21c6f 49 4e 54 45 52 53 45 43 54 2c 20 20 0a 20 20 20  INTERSECT,  .   
21c70 20 54 4b 5f 54 41 42 4c 45 2c 20 20 20 20 20 20   TK_TABLE,      
21c71 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 54  TK_JOIN_KW,    T
21c72 4b 5f 54 48 45 4e 2c 20 20 20 20 20 20 20 54 4b  K_THEN,       TK
21c73 5f 45 4e 44 2c 20 20 20 20 20 20 20 20 54 4b 5f  _END,        TK_
21c74 44 41 54 41 42 41 53 45 2c 20 20 20 0a 20 20 20  DATABASE,   .   
21c75 20 54 4b 5f 41 53 2c 20 20 20 20 20 20 20 20 20   TK_AS,         
21c76 54 4b 5f 53 45 4c 45 43 54 2c 20 20 20 20 20 54  TK_SELECT,     T
21c77 4b 5f 54 52 41 4e 53 41 43 54 49 4f 4e 2c 54 4b  K_TRANSACTION,TK
21c78 5f 4f 4e 2c 20 20 20 20 20 20 20 20 20 54 4b 5f  _ON,         TK_
21c79 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 0a 20 20 20  JOIN_KW,    .   
21c7a 20 54 4b 5f 41 4c 54 45 52 2c 20 20 20 20 20 20   TK_ALTER,      
21c7b 54 4b 5f 52 41 49 53 45 2c 20 20 20 20 20 20 54  TK_RAISE,      T
21c7c 4b 5f 45 4c 53 45 2c 20 20 20 20 20 20 20 54 4b  K_ELSE,       TK
21c7d 5f 45 58 43 45 50 54 2c 20 20 20 20 20 54 4b 5f  _EXCEPT,     TK_
21c7e 54 52 49 47 47 45 52 2c 20 20 20 20 0a 20 20 20  TRIGGER,    .   
21c7f 20 54 4b 5f 52 45 46 45 52 45 4e 43 45 53 2c 20   TK_REFERENCES, 
21c80 54 4b 5f 55 4e 49 51 55 45 2c 20 20 20 20 20 54  TK_UNIQUE,     T
21c81 4b 5f 51 55 45 52 59 2c 20 20 20 20 20 20 54 4b  K_QUERY,      TK
21c82 5f 41 54 54 41 43 48 2c 20 20 20 20 20 54 4b 5f  _ATTACH,     TK_
21c83 48 41 56 49 4e 47 2c 20 20 20 20 20 0a 20 20 20  HAVING,     .   
21c84 20 54 4b 5f 47 52 4f 55 50 2c 20 20 20 20 20 20   TK_GROUP,      
21c85 54 4b 5f 55 50 44 41 54 45 2c 20 20 20 20 20 54  TK_UPDATE,     T
21c86 4b 5f 54 45 4d 50 2c 20 20 20 20 20 20 20 54 4b  K_TEMP,       TK
21c87 5f 54 45 4d 50 2c 20 20 20 20 20 20 20 54 4b 5f  _TEMP,       TK_
21c88 4f 52 2c 20 20 20 20 20 20 20 20 20 0a 20 20 20  OR,         .   
21c89 20 54 4b 5f 42 45 47 49 4e 2c 20 20 20 20 20 20   TK_BEGIN,      
21c8a 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 54  TK_JOIN_KW,    T
21c8b 4b 5f 52 45 49 4e 44 45 58 2c 20 20 20 20 54 4b  K_REINDEX,    TK
21c8c 5f 49 4e 44 45 58 2c 20 20 20 20 20 20 54 4b 5f  _INDEX,      TK_
21c8d 45 58 43 4c 55 53 49 56 45 2c 20 20 0a 20 20 20  EXCLUSIVE,  .   
21c8e 20 54 4b 5f 45 58 49 53 54 53 2c 20 20 20 20 20   TK_EXISTS,     
21c8f 54 4b 5f 42 45 54 57 45 45 4e 2c 20 20 20 20 54  TK_BETWEEN,    T
21c90 4b 5f 4e 4f 54 4e 55 4c 4c 2c 20 20 20 20 54 4b  K_NOTNULL,    TK
21c91 5f 4e 4f 54 2c 20 20 20 20 20 20 20 20 54 4b 5f  _NOT,        TK_
21c92 4e 55 4c 4c 2c 20 20 20 20 20 20 20 0a 20 20 20  NULL,       .   
21c93 20 54 4b 5f 4c 49 4b 45 5f 4b 57 2c 20 20 20 20   TK_LIKE_KW,    
21c94 54 4b 5f 43 41 53 43 41 44 45 2c 20 20 20 20 54  TK_CASCADE,    T
21c95 4b 5f 41 53 43 2c 20 20 20 20 20 20 20 20 54 4b  K_ASC,        TK
21c96 5f 44 45 46 45 52 52 41 42 4c 45 2c 20 54 4b 5f  _DEFERRABLE, TK_
21c97 43 41 53 45 2c 20 20 20 20 20 20 20 0a 20 20 20  CASE,       .   
21c98 20 54 4b 5f 43 4f 4c 4c 41 54 45 2c 20 20 20 20   TK_COLLATE,    
21c99 54 4b 5f 43 52 45 41 54 45 2c 20 20 20 20 20 54  TK_CREATE,     T
21c9a 4b 5f 43 54 49 4d 45 5f 4b 57 2c 20 20 20 54 4b  K_CTIME_KW,   TK
21c9b 5f 44 45 4c 45 54 45 2c 20 20 20 20 20 54 4b 5f  _DELETE,     TK_
21c9c 44 45 54 41 43 48 2c 20 20 20 20 20 0a 20 20 20  DETACH,     .   
21c9d 20 54 4b 5f 49 4d 4d 45 44 49 41 54 45 2c 20 20   TK_IMMEDIATE,  
21c9e 54 4b 5f 4a 4f 49 4e 2c 20 20 20 20 20 20 20 54  TK_JOIN,       T
21c9f 4b 5f 49 4e 53 45 52 54 2c 20 20 20 20 20 54 4b  K_INSERT,     TK
21ca0 5f 4d 41 54 43 48 2c 20 20 20 20 20 20 54 4b 5f  _MATCH,      TK_
21ca1 50 4c 41 4e 2c 20 20 20 20 20 20 20 0a 20 20 20  PLAN,       .   
21ca2 20 54 4b 5f 41 4e 41 4c 59 5a 45 2c 20 20 20 20   TK_ANALYZE,    
21ca3 54 4b 5f 50 52 41 47 4d 41 2c 20 20 20 20 20 54  TK_PRAGMA,     T
21ca4 4b 5f 41 42 4f 52 54 2c 20 20 20 20 20 20 54 4b  K_ABORT,      TK
21ca5 5f 56 41 4c 55 45 53 2c 20 20 20 20 20 54 4b 5f  _VALUES,     TK_
21ca6 56 49 52 54 55 41 4c 2c 20 20 20 20 0a 20 20 20  VIRTUAL,    .   
21ca7 20 54 4b 5f 4c 49 4d 49 54 2c 20 20 20 20 20 20   TK_LIMIT,      
21ca8 54 4b 5f 57 48 45 4e 2c 20 20 20 20 20 20 20 54  TK_WHEN,       T
21ca9 4b 5f 57 48 45 52 45 2c 20 20 20 20 20 20 54 4b  K_WHERE,      TK
21caa 5f 52 45 4e 41 4d 45 2c 20 20 20 20 20 54 4b 5f  _RENAME,     TK_
21cab 41 46 54 45 52 2c 20 20 20 20 20 20 0a 20 20 20  AFTER,      .   
21cac 20 54 4b 5f 52 45 50 4c 41 43 45 2c 20 20 20 20   TK_REPLACE,    
21cad 54 4b 5f 41 4e 44 2c 20 20 20 20 20 20 20 20 54  TK_AND,        T
21cae 4b 5f 44 45 46 41 55 4c 54 2c 20 20 20 20 54 4b  K_DEFAULT,    TK
21caf 5f 41 55 54 4f 49 4e 43 52 2c 20 20 20 54 4b 5f  _AUTOINCR,   TK_
21cb0 54 4f 2c 20 20 20 20 20 20 20 20 20 0a 20 20 20  TO,         .   
21cb1 20 54 4b 5f 49 4e 2c 20 20 20 20 20 20 20 20 20   TK_IN,         
21cb2 54 4b 5f 43 41 53 54 2c 20 20 20 20 20 20 20 54  TK_CAST,       T
21cb3 4b 5f 43 4f 4c 55 4d 4e 4b 57 2c 20 20 20 54 4b  K_COLUMNKW,   TK
21cb4 5f 43 4f 4d 4d 49 54 2c 20 20 20 20 20 54 4b 5f  _COMMIT,     TK_
21cb5 43 4f 4e 46 4c 49 43 54 2c 20 20 20 0a 20 20 20  CONFLICT,   .   
21cb6 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20   TK_JOIN_KW,    
21cb7 54 4b 5f 43 54 49 4d 45 5f 4b 57 2c 20 20 20 54  TK_CTIME_KW,   T
21cb8 4b 5f 43 54 49 4d 45 5f 4b 57 2c 20 20 20 54 4b  K_CTIME_KW,   TK
21cb9 5f 50 52 49 4d 41 52 59 2c 20 20 20 20 54 4b 5f  _PRIMARY,    TK_
21cba 44 45 46 45 52 52 45 44 2c 20 20 20 0a 20 20 20  DEFERRED,   .   
21cbb 20 54 4b 5f 44 49 53 54 49 4e 43 54 2c 20 20 20   TK_DISTINCT,   
21cbc 54 4b 5f 49 53 2c 20 20 20 20 20 20 20 20 20 54  TK_IS,         T
21cbd 4b 5f 44 52 4f 50 2c 20 20 20 20 20 20 20 54 4b  K_DROP,       TK
21cbe 5f 46 41 49 4c 2c 20 20 20 20 20 20 20 54 4b 5f  _FAIL,       TK_
21cbf 46 52 4f 4d 2c 20 20 20 20 20 20 20 0a 20 20 20  FROM,       .   
21cc0 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20   TK_JOIN_KW,    
21cc1 54 4b 5f 4c 49 4b 45 5f 4b 57 2c 20 20 20 20 54  TK_LIKE_KW,    T
21cc2 4b 5f 42 59 2c 20 20 20 20 20 20 20 20 20 54 4b  K_BY,         TK
21cc3 5f 49 46 2c 20 20 20 20 20 20 20 20 20 54 4b 5f  _IF,         TK_
21cc4 49 4e 54 4f 2c 20 20 20 20 20 20 20 0a 20 20 20  INTO,       .   
21cc5 20 54 4b 5f 4f 46 46 53 45 54 2c 20 20 20 20 20   TK_OFFSET,     
21cc6 54 4b 5f 4f 46 2c 20 20 20 20 20 20 20 20 20 54  TK_OF,         T
21cc7 4b 5f 53 45 54 2c 20 20 20 20 20 20 20 20 54 4b  K_SET,        TK
21cc8 5f 49 53 4e 55 4c 4c 2c 20 20 20 20 20 54 4b 5f  _ISNULL,     TK_
21cc9 4f 52 44 45 52 2c 20 20 20 20 20 20 0a 20 20 20  ORDER,      .   
21cca 20 54 4b 5f 52 45 53 54 52 49 43 54 2c 20 20 20   TK_RESTRICT,   
21ccb 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 54  TK_JOIN_KW,    T
21ccc 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 54 4b  K_JOIN_KW,    TK
21ccd 5f 52 4f 4c 4c 42 41 43 4b 2c 20 20 20 54 4b 5f  _ROLLBACK,   TK_
21cce 52 4f 57 2c 20 20 20 20 20 20 20 20 0a 20 20 20  ROW,        .   
21ccf 20 54 4b 5f 55 4e 49 4f 4e 2c 20 20 20 20 20 20   TK_UNION,      
21cd0 54 4b 5f 55 53 49 4e 47 2c 20 20 20 20 20 20 54  TK_USING,      T
21cd1 4b 5f 56 41 43 55 55 4d 2c 20 20 20 20 20 54 4b  K_VACUUM,     TK
21cd2 5f 56 49 45 57 2c 20 20 20 20 20 20 20 54 4b 5f  _VIEW,       TK_
21cd3 49 4e 49 54 49 41 4c 4c 59 2c 20 20 0a 20 20 20  INITIALLY,  .   
21cd4 20 54 4b 5f 41 4c 4c 2c 20 20 20 20 20 20 20 20   TK_ALL,        
21cd5 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 68 2c 20 69  .  };.  int h, i
21cd6 3b 0a 20 20 69 66 28 20 6e 3c 32 20 29 20 72 65  ;.  if( n<2 ) re
21cd7 74 75 72 6e 20 54 4b 5f 49 44 3b 0a 20 20 68 20  turn TK_ID;.  h 
21cd8 3d 20 28 28 63 68 61 72 4d 61 70 28 7a 5b 30 5d  = ((charMap(z[0]
21cd9 29 2a 34 29 20 5e 0a 20 20 20 20 20 20 28 63 68  )*4) ^.      (ch
21cda 61 72 4d 61 70 28 7a 5b 6e 2d 31 5d 29 2a 33 29  arMap(z[n-1])*3)
21cdb 20 5e 0a 20 20 20 20 20 20 6e 29 20 25 20 31 32   ^.      n) % 12
21cdc 37 3b 0a 20 20 66 6f 72 28 69 3d 28 28 69 6e 74  7;.  for(i=((int
21cdd 29 61 48 61 73 68 5b 68 5d 29 2d 31 3b 20 69 3e  )aHash[h])-1; i>
21cde 3d 30 3b 20 69 3d 28 28 69 6e 74 29 61 4e 65 78  =0; i=((int)aNex
21cdf 74 5b 69 5d 29 2d 31 29 7b 0a 20 20 20 20 69 66  t[i])-1){.    if
21ce0 28 20 61 4c 65 6e 5b 69 5d 3d 3d 6e 20 26 26 20  ( aLen[i]==n && 
21ce1 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
21ce2 26 7a 54 65 78 74 5b 61 4f 66 66 73 65 74 5b 69  &zText[aOffset[i
21ce3 5d 5d 2c 7a 2c 6e 29 3d 3d 30 20 29 7b 0a 20 20  ]],z,n)==0 ){.  
21ce4 20 20 20 20 72 65 74 75 72 6e 20 61 43 6f 64 65      return aCode
21ce5 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  [i];.    }.  }. 
21ce6 20 72 65 74 75 72 6e 20 54 4b 5f 49 44 3b 0a 7d   return TK_ID;.}
21ce7 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
21ce8 69 6e 74 20 73 71 6c 69 74 65 33 4b 65 79 77 6f  int sqlite3Keywo
21ce9 72 64 43 6f 64 65 28 63 6f 6e 73 74 20 75 6e 73  rdCode(const uns
21cea 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 2c 20 69  igned char *z, i
21ceb 6e 74 20 6e 29 7b 0a 20 20 72 65 74 75 72 6e 20  nt n){.  return 
21cec 6b 65 79 77 6f 72 64 43 6f 64 65 28 28 63 68 61  keywordCode((cha
21ced 72 2a 29 7a 2c 20 6e 29 3b 0a 7d 0a 0a 2f 2a 2a  r*)z, n);.}../**
21cee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
21cef 20 6f 66 20 6b 65 79 77 6f 72 64 68 61 73 68 2e   of keywordhash.
21cf0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
21cf1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21cf2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
21cf3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
21cf4 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
21cf5 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 74 6f 6b   left off in tok
21cf6 65 6e 69 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  enize.c ********
21cf7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f  ***********/.../
21cf8 2a 0a 2a 2a 20 49 66 20 58 20 69 73 20 61 20 63  *.** If X is a c
21cf9 68 61 72 61 63 74 65 72 20 74 68 61 74 20 63 61  haracter that ca
21cfa 6e 20 62 65 20 75 73 65 64 20 69 6e 20 61 6e 20  n be used in an 
21cfb 69 64 65 6e 74 69 66 69 65 72 20 74 68 65 6e 0a  identifier then.
21cfc 2a 2a 20 49 64 43 68 61 72 28 58 29 20 77 69 6c  ** IdChar(X) wil
21cfd 6c 20 62 65 20 74 72 75 65 2e 20 20 4f 74 68 65  l be true.  Othe
21cfe 72 77 69 73 65 20 69 74 20 69 73 20 66 61 6c 73  rwise it is fals
21cff 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 41 53 43  e..**.** For ASC
21d00 49 49 2c 20 61 6e 79 20 63 68 61 72 61 63 74 65  II, any characte
21d01 72 20 77 69 74 68 20 74 68 65 20 68 69 67 68 2d  r with the high-
21d02 6f 72 64 65 72 20 62 69 74 20 73 65 74 20 69 73  order bit set is
21d03 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 69 6e 20 61  .** allowed in a
21d04 6e 20 69 64 65 6e 74 69 66 69 65 72 2e 20 20 46  n identifier.  F
21d05 6f 72 20 37 2d 62 69 74 20 63 68 61 72 61 63 74  or 7-bit charact
21d06 65 72 73 2c 20 0a 2a 2a 20 73 71 6c 69 74 65 33  ers, .** sqlite3
21d07 49 73 49 64 43 68 61 72 5b 58 5d 20 6d 75 73 74  IsIdChar[X] must
21d08 20 62 65 20 31 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72   be 1..**.** For
21d09 20 45 42 43 44 49 43 2c 20 74 68 65 20 72 75 6c   EBCDIC, the rul
21d0a 65 73 20 61 72 65 20 6d 6f 72 65 20 63 6f 6d 70  es are more comp
21d0b 6c 65 78 20 62 75 74 20 68 61 76 65 20 74 68 65  lex but have the
21d0c 20 73 61 6d 65 0a 2a 2a 20 65 6e 64 20 72 65 73   same.** end res
21d0d 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65  ult..**.** Ticke
21d0e 74 20 23 31 30 36 36 2e 20 20 74 68 65 20 53 51  t #1066.  the SQ
21d0f 4c 20 73 74 61 6e 64 61 72 64 20 64 6f 65 73 20  L standard does 
21d10 6e 6f 74 20 61 6c 6c 6f 77 20 27 24 27 20 69 6e  not allow '$' in
21d11 20 74 68 65 0a 2a 2a 20 6d 69 64 64 6c 65 20 6f   the.** middle o
21d12 66 20 69 64 65 6e 74 66 69 65 72 73 2e 20 20 42  f identfiers.  B
21d13 75 74 20 6d 61 6e 79 20 53 51 4c 20 69 6d 70 6c  ut many SQL impl
21d14 65 6d 65 6e 74 61 74 69 6f 6e 73 20 64 6f 2e 20  ementations do. 
21d15 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  .** SQLite will 
21d16 61 6c 6c 6f 77 20 27 24 27 20 69 6e 20 69 64 65  allow '$' in ide
21d17 6e 74 69 66 69 65 72 73 20 66 6f 72 20 63 6f 6d  ntifiers for com
21d18 70 61 74 69 62 69 6c 69 74 79 2e 0a 2a 2a 20 42  patibility..** B
21d19 75 74 20 74 68 65 20 66 65 61 74 75 72 65 20 69  ut the feature i
21d1a 73 20 75 6e 64 6f 63 75 6d 65 6e 74 65 64 2e 0a  s undocumented..
21d1b 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
21d1c 5f 41 53 43 49 49 0a 63 6f 6e 73 74 20 63 68 61  _ASCII.const cha
21d1d 72 20 73 71 6c 69 74 65 33 49 73 49 64 43 68 61  r sqlite3IsIdCha
21d1e 72 5b 5d 20 3d 20 7b 0a 2f 2a 20 78 30 20 78 31  r[] = {./* x0 x1
21d1f 20 78 32 20 78 33 20 78 34 20 78 35 20 78 36 20   x2 x3 x4 x5 x6 
21d20 78 37 20 78 38 20 78 39 20 78 41 20 78 42 20 78  x7 x8 x9 xA xB x
21d21 43 20 78 44 20 78 45 20 78 46 20 2a 2f 0a 20 20  C xD xE xF */.  
21d22 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c    0, 0, 0, 0, 1,
21d23 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
21d24 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
21d25 2c 20 20 2f 2a 20 32 78 20 2a 2f 0a 20 20 20 20  ,  /* 2x */.    
21d26 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
21d27 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
21d28 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
21d29 20 2f 2a 20 33 78 20 2a 2f 0a 20 20 20 20 30 2c   /* 3x */.    0,
21d2a 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
21d2b 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
21d2c 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f  , 1, 1, 1, 1,  /
21d2d 2a 20 34 78 20 2a 2f 0a 20 20 20 20 31 2c 20 31  * 4x */.    1, 1
21d2e 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
21d2f 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20   1, 1, 1, 1, 0, 
21d30 30 2c 20 30 2c 20 30 2c 20 31 2c 20 20 2f 2a 20  0, 0, 0, 1,  /* 
21d31 35 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20  5x */.    0, 1, 
21d32 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
21d33 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
21d34 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 36 78   1, 1, 1,  /* 6x
21d35 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c   */.    1, 1, 1,
21d36 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
21d37 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30  1, 1, 1, 0, 0, 0
21d38 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 37 78 20 2a  , 0, 0,  /* 7x *
21d39 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 49 64 43  /.};.#define IdC
21d3a 68 61 72 28 43 29 20 20 28 28 28 63 3d 43 29 26  har(C)  (((c=C)&
21d3b 30 78 38 30 29 21 3d 30 20 7c 7c 20 28 63 3e 30  0x80)!=0 || (c>0
21d3c 78 31 66 20 26 26 20 73 71 6c 69 74 65 33 49 73  x1f && sqlite3Is
21d3d 49 64 43 68 61 72 5b 63 2d 30 78 32 30 5d 29 29  IdChar[c-0x20]))
21d3e 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
21d3f 51 4c 49 54 45 5f 45 42 43 44 49 43 0a 63 6f 6e  QLITE_EBCDIC.con
21d40 73 74 20 63 68 61 72 20 73 71 6c 69 74 65 33 49  st char sqlite3I
21d41 73 49 64 43 68 61 72 5b 5d 20 3d 20 7b 0a 2f 2a  sIdChar[] = {./*
21d42 20 78 30 20 78 31 20 78 32 20 78 33 20 78 34 20   x0 x1 x2 x3 x4 
21d43 78 35 20 78 36 20 78 37 20 78 38 20 78 39 20 78  x5 x6 x7 x8 x9 x
21d44 41 20 78 42 20 78 43 20 78 44 20 78 45 20 78 46  A xB xC xD xE xF
21d45 20 2a 2f 0a 20 20 20 20 30 2c 20 30 2c 20 31 2c   */.    0, 0, 1,
21d46 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
21d47 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30  1, 1, 0, 0, 0, 0
21d48 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 34 78 20 2a  , 0, 0,  /* 4x *
21d49 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31  /.    0, 1, 1, 1
21d4a 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
21d4b 20 31 2c 20 30 2c 20 31 2c 20 30 2c 20 30 2c 20   1, 0, 1, 0, 0, 
21d4c 30 2c 20 30 2c 20 20 2f 2a 20 35 78 20 2a 2f 0a  0, 0,  /* 5x */.
21d4d 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 31 2c 20      0, 0, 1, 1, 
21d4e 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
21d4f 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20 30 2c  , 0, 0, 0, 1, 0,
21d50 20 30 2c 20 20 2f 2a 20 36 78 20 2a 2f 0a 20 20   0,  /* 6x */.  
21d51 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    0, 1, 1, 1, 1,
21d52 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20   1, 1, 1, 1, 0, 
21d53 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
21d54 2c 20 20 2f 2a 20 37 78 20 2a 2f 0a 20 20 20 20  ,  /* 7x */.    
21d55 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 1, 1, 1, 1, 1
21d56 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
21d57 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20   0, 1, 1, 1, 0, 
21d58 20 2f 2a 20 38 78 20 2a 2f 0a 20 20 20 20 30 2c   /* 8x */.    0,
21d59 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
21d5a 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30  1, 1, 1, 1, 0, 0
21d5b 2c 20 31 2c 20 30 2c 20 31 2c 20 30 2c 20 20 2f  , 1, 0, 1, 0,  /
21d5c 2a 20 39 78 20 2a 2f 0a 20 20 20 20 31 2c 20 30  * 9x */.    1, 0
21d5d 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
21d5e 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20   1, 1, 1, 1, 0, 
21d5f 31 2c 20 31 2c 20 31 2c 20 30 2c 20 20 2f 2a 20  1, 1, 1, 0,  /* 
21d60 41 78 20 2a 2f 0a 20 20 20 20 30 2c 20 30 2c 20  Ax */.    0, 0, 
21d61 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
21d62 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
21d63 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 42 78   0, 0, 0,  /* Bx
21d64 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c   */.    0, 1, 1,
21d65 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
21d66 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31  1, 1, 0, 1, 1, 1
21d67 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 43 78 20 2a  , 1, 1,  /* Cx *
21d68 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31  /.    0, 1, 1, 1
21d69 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
21d6a 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 0, 1, 1, 1, 
21d6b 31 2c 20 31 2c 20 20 2f 2a 20 44 78 20 2a 2f 0a  1, 1,  /* Dx */.
21d6c 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 31 2c 20      0, 0, 1, 1, 
21d6d 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
21d6e 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 0, 1, 1, 1, 1,
21d6f 20 31 2c 20 20 2f 2a 20 45 78 20 2a 2f 0a 20 20   1,  /* Ex */.  
21d70 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    1, 1, 1, 1, 1,
21d71 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
21d72 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30  0, 1, 1, 1, 1, 0
21d73 2c 20 20 2f 2a 20 46 78 20 2a 2f 0a 7d 3b 0a 23  ,  /* Fx */.};.#
21d74 64 65 66 69 6e 65 20 49 64 43 68 61 72 28 43 29  define IdChar(C)
21d75 20 20 28 28 28 63 3d 43 29 3e 3d 30 78 34 32 20    (((c=C)>=0x42 
21d76 26 26 20 73 71 6c 69 74 65 33 49 73 49 64 43 68  && sqlite3IsIdCh
21d77 61 72 5b 63 2d 30 78 34 30 5d 29 29 0a 23 65 6e  ar[c-0x40])).#en
21d78 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  dif.../*.** Retu
21d79 72 6e 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66  rn the length of
21d7a 20 74 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20   the token that 
21d7b 62 65 67 69 6e 73 20 61 74 20 7a 5b 30 5d 2e 20  begins at z[0]. 
21d7c 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 74 6f  .** Store the to
21d7d 6b 65 6e 20 74 79 70 65 20 69 6e 20 2a 74 6f 6b  ken type in *tok
21d7e 65 6e 54 79 70 65 20 62 65 66 6f 72 65 20 72 65  enType before re
21d7f 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74  turning..*/.stat
21d80 69 63 20 69 6e 74 20 67 65 74 54 6f 6b 65 6e 28  ic int getToken(
21d81 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
21d82 68 61 72 20 2a 7a 2c 20 69 6e 74 20 2a 74 6f 6b  har *z, int *tok
21d83 65 6e 54 79 70 65 29 7b 0a 20 20 69 6e 74 20 69  enType){.  int i
21d84 2c 20 63 3b 0a 20 20 73 77 69 74 63 68 28 20 2a  , c;.  switch( *
21d85 7a 20 29 7b 0a 20 20 20 20 63 61 73 65 20 27 20  z ){.    case ' 
21d86 27 3a 20 63 61 73 65 20 27 5c 74 27 3a 20 63 61  ': case '\t': ca
21d87 73 65 20 27 5c 6e 27 3a 20 63 61 73 65 20 27 5c  se '\n': case '\
21d88 66 27 3a 20 63 61 73 65 20 27 5c 72 27 3a 20 7b  f': case '\r': {
21d89 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20  .      for(i=1; 
21d8a 69 73 73 70 61 63 65 28 7a 5b 69 5d 29 3b 20 69  isspace(z[i]); i
21d8b 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 2a 74 6f 6b  ++){}.      *tok
21d8c 65 6e 54 79 70 65 20 3d 20 54 4b 5f 53 50 41 43  enType = TK_SPAC
21d8d 45 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  E;.      return 
21d8e 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  i;.    }.    cas
21d8f 65 20 27 2d 27 3a 20 7b 0a 20 20 20 20 20 20 69  e '-': {.      i
21d90 66 28 20 7a 5b 31 5d 3d 3d 27 2d 27 20 29 7b 0a  f( z[1]=='-' ){.
21d91 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 32 3b          for(i=2;
21d92 20 28 63 3d 7a 5b 69 5d 29 21 3d 30 20 26 26 20   (c=z[i])!=0 && 
21d93 63 21 3d 27 5c 6e 27 3b 20 69 2b 2b 29 7b 7d 0a  c!='\n'; i++){}.
21d94 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79          *tokenTy
21d95 70 65 20 3d 20 54 4b 5f 43 4f 4d 4d 45 4e 54 3b  pe = TK_COMMENT;
21d96 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
21d97 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  i;.      }.     
21d98 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
21d99 5f 4d 49 4e 55 53 3b 0a 20 20 20 20 20 20 72 65  _MINUS;.      re
21d9a 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
21d9b 20 20 63 61 73 65 20 27 28 27 3a 20 7b 0a 20 20    case '(': {.  
21d9c 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
21d9d 20 54 4b 5f 4c 50 3b 0a 20 20 20 20 20 20 72 65   TK_LP;.      re
21d9e 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
21d9f 20 20 63 61 73 65 20 27 29 27 3a 20 7b 0a 20 20    case ')': {.  
21da0 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
21da1 20 54 4b 5f 52 50 3b 0a 20 20 20 20 20 20 72 65   TK_RP;.      re
21da2 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
21da3 20 20 63 61 73 65 20 27 3b 27 3a 20 7b 0a 20 20    case ';': {.  
21da4 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
21da5 20 54 4b 5f 53 45 4d 49 3b 0a 20 20 20 20 20 20   TK_SEMI;.      
21da6 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
21da7 20 20 20 20 63 61 73 65 20 27 2b 27 3a 20 7b 0a      case '+': {.
21da8 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
21da9 20 3d 20 54 4b 5f 50 4c 55 53 3b 0a 20 20 20 20   = TK_PLUS;.    
21daa 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
21dab 7d 0a 20 20 20 20 63 61 73 65 20 27 2a 27 3a 20  }.    case '*': 
21dac 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79  {.      *tokenTy
21dad 70 65 20 3d 20 54 4b 5f 53 54 41 52 3b 0a 20 20  pe = TK_STAR;.  
21dae 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
21daf 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 2f 27    }.    case '/'
21db0 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b  : {.      if( z[
21db1 31 5d 21 3d 27 2a 27 20 7c 7c 20 7a 5b 32 5d 3d  1]!='*' || z[2]=
21db2 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74  =0 ){.        *t
21db3 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 53 4c  okenType = TK_SL
21db4 41 53 48 3b 0a 20 20 20 20 20 20 20 20 72 65 74  ASH;.        ret
21db5 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  urn 1;.      }. 
21db6 20 20 20 20 20 66 6f 72 28 69 3d 33 2c 20 63 3d       for(i=3, c=
21db7 7a 5b 32 5d 3b 20 28 63 21 3d 27 2a 27 20 7c 7c  z[2]; (c!='*' ||
21db8 20 7a 5b 69 5d 21 3d 27 2f 27 29 20 26 26 20 28   z[i]!='/') && (
21db9 63 3d 7a 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29  c=z[i])!=0; i++)
21dba 7b 7d 0a 20 20 20 20 20 20 69 66 28 20 63 20 29  {}.      if( c )
21dbb 20 69 2b 2b 3b 0a 20 20 20 20 20 20 2a 74 6f 6b   i++;.      *tok
21dbc 65 6e 54 79 70 65 20 3d 20 54 4b 5f 43 4f 4d 4d  enType = TK_COMM
21dbd 45 4e 54 3b 0a 20 20 20 20 20 20 72 65 74 75 72  ENT;.      retur
21dbe 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  n i;.    }.    c
21dbf 61 73 65 20 27 25 27 3a 20 7b 0a 20 20 20 20 20  ase '%': {.     
21dc0 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
21dc1 5f 52 45 4d 3b 0a 20 20 20 20 20 20 72 65 74 75  _REM;.      retu
21dc2 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 1;.    }.    
21dc3 63 61 73 65 20 27 3d 27 3a 20 7b 0a 20 20 20 20  case '=': {.    
21dc4 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
21dc5 4b 5f 45 51 3b 0a 20 20 20 20 20 20 72 65 74 75  K_EQ;.      retu
21dc6 72 6e 20 31 20 2b 20 28 7a 5b 31 5d 3d 3d 27 3d  rn 1 + (z[1]=='=
21dc7 27 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ');.    }.    ca
21dc8 73 65 20 27 3c 27 3a 20 7b 0a 20 20 20 20 20 20  se '<': {.      
21dc9 69 66 28 20 28 63 3d 7a 5b 31 5d 29 3d 3d 27 3d  if( (c=z[1])=='=
21dca 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f  ' ){.        *to
21dcb 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4c 45 3b  kenType = TK_LE;
21dcc 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
21dcd 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  2;.      }else i
21dce 66 28 20 63 3d 3d 27 3e 27 20 29 7b 0a 20 20 20  f( c=='>' ){.   
21dcf 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
21dd0 3d 20 54 4b 5f 4e 45 3b 0a 20 20 20 20 20 20 20  = TK_NE;.       
21dd1 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 20   return 2;.     
21dd2 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 27 3c   }else if( c=='<
21dd3 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f  ' ){.        *to
21dd4 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4c 53 48  kenType = TK_LSH
21dd5 49 46 54 3b 0a 20 20 20 20 20 20 20 20 72 65 74  IFT;.        ret
21dd6 75 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c  urn 2;.      }el
21dd7 73 65 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b  se{.        *tok
21dd8 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4c 54 3b 0a  enType = TK_LT;.
21dd9 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31          return 1
21dda 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
21ddb 20 20 20 20 63 61 73 65 20 27 3e 27 3a 20 7b 0a      case '>': {.
21ddc 20 20 20 20 20 20 69 66 28 20 28 63 3d 7a 5b 31        if( (c=z[1
21ddd 5d 29 3d 3d 27 3d 27 20 29 7b 0a 20 20 20 20 20  ])=='=' ){.     
21dde 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
21ddf 54 4b 5f 47 45 3b 0a 20 20 20 20 20 20 20 20 72  TK_GE;.        r
21de0 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d  eturn 2;.      }
21de1 65 6c 73 65 20 69 66 28 20 63 3d 3d 27 3e 27 20  else if( c=='>' 
21de2 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65  ){.        *toke
21de3 6e 54 79 70 65 20 3d 20 54 4b 5f 52 53 48 49 46  nType = TK_RSHIF
21de4 54 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  T;.        retur
21de5 6e 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  n 2;.      }else
21de6 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  {.        *token
21de7 54 79 70 65 20 3d 20 54 4b 5f 47 54 3b 0a 20 20  Type = TK_GT;.  
21de8 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
21de9 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
21dea 20 20 63 61 73 65 20 27 21 27 3a 20 7b 0a 20 20    case '!': {.  
21deb 20 20 20 20 69 66 28 20 7a 5b 31 5d 21 3d 27 3d      if( z[1]!='=
21dec 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f  ' ){.        *to
21ded 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c  kenType = TK_ILL
21dee 45 47 41 4c 3b 0a 20 20 20 20 20 20 20 20 72 65  EGAL;.        re
21def 74 75 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d 65  turn 2;.      }e
21df0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f  lse{.        *to
21df1 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4e 45 3b  kenType = TK_NE;
21df2 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
21df3 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  2;.      }.    }
21df4 0a 20 20 20 20 63 61 73 65 20 27 7c 27 3a 20 7b  .    case '|': {
21df5 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 31 5d 21  .      if( z[1]!
21df6 3d 27 7c 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='|' ){.        
21df7 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
21df8 42 49 54 4f 52 3b 0a 20 20 20 20 20 20 20 20 72  BITOR;.        r
21df9 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d  eturn 1;.      }
21dfa 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2a 74  else{.        *t
21dfb 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 43 4f  okenType = TK_CO
21dfc 4e 43 41 54 3b 0a 20 20 20 20 20 20 20 20 72 65  NCAT;.        re
21dfd 74 75 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d 0a  turn 2;.      }.
21dfe 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27      }.    case '
21dff 2c 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b  ,': {.      *tok
21e00 65 6e 54 79 70 65 20 3d 20 54 4b 5f 43 4f 4d 4d  enType = TK_COMM
21e01 41 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  A;.      return 
21e02 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  1;.    }.    cas
21e03 65 20 27 26 27 3a 20 7b 0a 20 20 20 20 20 20 2a  e '&': {.      *
21e04 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 42  tokenType = TK_B
21e05 49 54 41 4e 44 3b 0a 20 20 20 20 20 20 72 65 74  ITAND;.      ret
21e06 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
21e07 20 63 61 73 65 20 27 7e 27 3a 20 7b 0a 20 20 20   case '~': {.   
21e08 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
21e09 54 4b 5f 42 49 54 4e 4f 54 3b 0a 20 20 20 20 20  TK_BITNOT;.     
21e0a 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
21e0b 0a 20 20 20 20 63 61 73 65 20 27 60 27 3a 0a 20  .    case '`':. 
21e0c 20 20 20 63 61 73 65 20 27 5c 27 27 3a 0a 20 20     case '\'':.  
21e0d 20 20 63 61 73 65 20 27 22 27 3a 20 7b 0a 20 20    case '"': {.  
21e0e 20 20 20 20 69 6e 74 20 64 65 6c 69 6d 20 3d 20      int delim = 
21e0f 7a 5b 30 5d 3b 0a 20 20 20 20 20 20 66 6f 72 28  z[0];.      for(
21e10 69 3d 31 3b 20 28 63 3d 7a 5b 69 5d 29 21 3d 30  i=1; (c=z[i])!=0
21e11 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
21e12 69 66 28 20 63 3d 3d 64 65 6c 69 6d 20 29 7b 0a  if( c==delim ){.
21e13 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 5b            if( z[
21e14 69 2b 31 5d 3d 3d 64 65 6c 69 6d 20 29 7b 0a 20  i+1]==delim ){. 
21e15 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a             i++;.
21e16 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
21e17 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65  .            bre
21e18 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ak;.          }.
21e19 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
21e1a 7d 0a 20 20 20 20 20 20 69 66 28 20 63 20 29 7b  }.      if( c ){
21e1b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
21e1c 79 70 65 20 3d 20 54 4b 5f 53 54 52 49 4e 47 3b  ype = TK_STRING;
21e1d 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
21e1e 69 2b 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  i+1;.      }else
21e1f 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  {.        *token
21e20 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41  Type = TK_ILLEGA
21e21 4c 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  L;.        retur
21e22 6e 20 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n i;.      }.   
21e23 20 7d 0a 20 20 20 20 63 61 73 65 20 27 2e 27 3a   }.    case '.':
21e24 20 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   {.#ifndef SQLIT
21e25 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
21e26 50 4f 49 4e 54 0a 20 20 20 20 20 20 69 66 28 20  POINT.      if( 
21e27 21 69 73 64 69 67 69 74 28 7a 5b 31 5d 29 20 29  !isdigit(z[1]) )
21e28 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a  .#endif.      {.
21e29 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79          *tokenTy
21e2a 70 65 20 3d 20 54 4b 5f 44 4f 54 3b 0a 20 20 20  pe = TK_DOT;.   
21e2b 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
21e2c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20       }.      /* 
21e2d 49 66 20 74 68 65 20 6e 65 78 74 20 63 68 61 72  If the next char
21e2e 61 63 74 65 72 20 69 73 20 61 20 64 69 67 69 74  acter is a digit
21e2f 2c 20 74 68 69 73 20 69 73 20 61 20 66 6c 6f 61  , this is a floa
21e30 74 69 6e 67 20 70 6f 69 6e 74 0a 20 20 20 20 20  ting point.     
21e31 20 2a 2a 20 6e 75 6d 62 65 72 20 74 68 61 74 20   ** number that 
21e32 62 65 67 69 6e 73 20 77 69 74 68 20 22 2e 22 2e  begins with ".".
21e33 20 20 46 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f    Fall thru into
21e34 20 74 68 65 20 6e 65 78 74 20 63 61 73 65 20 2a   the next case *
21e35 2f 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  /.    }.    case
21e36 20 27 30 27 3a 20 63 61 73 65 20 27 31 27 3a 20   '0': case '1': 
21e37 63 61 73 65 20 27 32 27 3a 20 63 61 73 65 20 27  case '2': case '
21e38 33 27 3a 20 63 61 73 65 20 27 34 27 3a 0a 20 20  3': case '4':.  
21e39 20 20 63 61 73 65 20 27 35 27 3a 20 63 61 73 65    case '5': case
21e3a 20 27 36 27 3a 20 63 61 73 65 20 27 37 27 3a 20   '6': case '7': 
21e3b 63 61 73 65 20 27 38 27 3a 20 63 61 73 65 20 27  case '8': case '
21e3c 39 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b  9': {.      *tok
21e3d 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4e 54 45  enType = TK_INTE
21e3e 47 45 52 3b 0a 20 20 20 20 20 20 66 6f 72 28 69  GER;.      for(i
21e3f 3d 30 3b 20 69 73 64 69 67 69 74 28 7a 5b 69 5d  =0; isdigit(z[i]
21e40 29 3b 20 69 2b 2b 29 7b 7d 0a 23 69 66 6e 64 65  ); i++){}.#ifnde
21e41 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  f SQLITE_OMIT_FL
21e42 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20  OATING_POINT.   
21e43 20 20 20 69 66 28 20 7a 5b 69 5d 3d 3d 27 2e 27     if( z[i]=='.'
21e44 20 29 7b 0a 20 20 20 20 20 20 20 20 69 2b 2b 3b   ){.        i++;
21e45 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
21e46 69 73 64 69 67 69 74 28 7a 5b 69 5d 29 20 29 7b  isdigit(z[i]) ){
21e47 20 69 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20 20   i++; }.        
21e48 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
21e49 46 4c 4f 41 54 3b 0a 20 20 20 20 20 20 7d 0a 20  FLOAT;.      }. 
21e4a 20 20 20 20 20 69 66 28 20 28 7a 5b 69 5d 3d 3d       if( (z[i]==
21e4b 27 65 27 20 7c 7c 20 7a 5b 69 5d 3d 3d 27 45 27  'e' || z[i]=='E'
21e4c 29 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20  ) &&.           
21e4d 28 20 69 73 64 69 67 69 74 28 7a 5b 69 2b 31 5d  ( isdigit(z[i+1]
21e4e 29 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c  ) .            |
21e4f 7c 20 28 28 7a 5b 69 2b 31 5d 3d 3d 27 2b 27 20  | ((z[i+1]=='+' 
21e50 7c 7c 20 7a 5b 69 2b 31 5d 3d 3d 27 2d 27 29 20  || z[i+1]=='-') 
21e51 26 26 20 69 73 64 69 67 69 74 28 7a 5b 69 2b 32  && isdigit(z[i+2
21e52 5d 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 29  ])).           )
21e53 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20  .      ){.      
21e54 20 20 69 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20    i += 2;.      
21e55 20 20 77 68 69 6c 65 28 20 69 73 64 69 67 69 74    while( isdigit
21e56 28 7a 5b 69 5d 29 20 29 7b 20 69 2b 2b 3b 20 7d  (z[i]) ){ i++; }
21e57 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
21e58 79 70 65 20 3d 20 54 4b 5f 46 4c 4f 41 54 3b 0a  ype = TK_FLOAT;.
21e59 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20        }.#endif. 
21e5a 20 20 20 20 20 77 68 69 6c 65 28 20 49 64 43 68       while( IdCh
21e5b 61 72 28 7a 5b 69 5d 29 20 29 7b 0a 20 20 20 20  ar(z[i]) ){.    
21e5c 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
21e5d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20   TK_ILLEGAL;.   
21e5e 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20       i++;.      
21e5f 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69  }.      return i
21e60 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
21e61 20 27 5b 27 3a 20 7b 0a 20 20 20 20 20 20 66 6f   '[': {.      fo
21e62 72 28 69 3d 31 2c 20 63 3d 7a 5b 30 5d 3b 20 63  r(i=1, c=z[0]; c
21e63 21 3d 27 5d 27 20 26 26 20 28 63 3d 7a 5b 69 5d  !=']' && (c=z[i]
21e64 29 21 3d 30 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20  )!=0; i++){}.   
21e65 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
21e66 54 4b 5f 49 44 3b 0a 20 20 20 20 20 20 72 65 74  TK_ID;.      ret
21e67 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20 20  urn i;.    }.   
21e68 20 63 61 73 65 20 27 3f 27 3a 20 7b 0a 20 20 20   case '?': {.   
21e69 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
21e6a 54 4b 5f 56 41 52 49 41 42 4c 45 3b 0a 20 20 20  TK_VARIABLE;.   
21e6b 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 73 64 69     for(i=1; isdi
21e6c 67 69 74 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b  git(z[i]); i++){
21e6d 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69  }.      return i
21e6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
21e6f 20 27 23 27 3a 20 7b 0a 20 20 20 20 20 20 66 6f   '#': {.      fo
21e70 72 28 69 3d 31 3b 20 69 73 64 69 67 69 74 28 7a  r(i=1; isdigit(z
21e71 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20  [i]); i++){}.   
21e72 20 20 20 69 66 28 20 69 3e 31 20 29 7b 0a 20 20     if( i>1 ){.  
21e73 20 20 20 20 20 20 2f 2a 20 50 61 72 61 6d 65 74        /* Paramet
21e74 65 72 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  ers of the form 
21e75 23 4e 4e 4e 20 28 77 68 65 72 65 20 4e 4e 4e 20  #NNN (where NNN 
21e76 69 73 20 61 20 6e 75 6d 62 65 72 29 20 61 72 65  is a number) are
21e77 20 75 73 65 64 0a 20 20 20 20 20 20 20 20 2a 2a   used.        **
21e78 20 69 6e 74 65 72 6e 61 6c 6c 79 20 62 79 20 73   internally by s
21e79 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73  qlite3NestedPars
21e7a 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 2a  e.  */.        *
21e7b 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 52  tokenType = TK_R
21e7c 45 47 49 53 54 45 52 3b 0a 20 20 20 20 20 20 20  EGISTER;.       
21e7d 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 20   return i;.     
21e7e 20 7d 0a 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c   }.      /* Fall
21e7f 20 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 74 68   through into th
21e80 65 20 6e 65 78 74 20 63 61 73 65 20 69 66 20 74  e next case if t
21e81 68 65 20 27 23 27 20 69 73 20 6e 6f 74 20 66 6f  he '#' is not fo
21e82 6c 6c 6f 77 65 64 20 62 79 0a 20 20 20 20 20 20  llowed by.      
21e83 2a 2a 20 61 20 64 69 67 69 74 2e 20 54 72 79 20  ** a digit. Try 
21e84 74 6f 20 6d 61 74 63 68 20 23 41 41 41 41 20 77  to match #AAAA w
21e85 68 65 72 65 20 41 41 41 41 20 69 73 20 61 20 70  here AAAA is a p
21e86 61 72 61 6d 65 74 65 72 20 6e 61 6d 65 2e 20 2a  arameter name. *
21e87 2f 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20  /.    }.#ifndef 
21e88 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 43 4c 5f  SQLITE_OMIT_TCL_
21e89 56 41 52 49 41 42 4c 45 0a 20 20 20 20 63 61 73  VARIABLE.    cas
21e8a 65 20 27 24 27 3a 0a 23 65 6e 64 69 66 0a 20 20  e '$':.#endif.  
21e8b 20 20 63 61 73 65 20 27 40 27 3a 20 20 2f 2a 20    case '@':  /* 
21e8c 46 6f 72 20 63 6f 6d 70 61 74 69 62 69 6c 69 74  For compatibilit
21e8d 79 20 77 69 74 68 20 4d 53 20 53 51 4c 20 53 65  y with MS SQL Se
21e8e 72 76 65 72 20 2a 2f 0a 20 20 20 20 63 61 73 65  rver */.    case
21e8f 20 27 3a 27 3a 20 7b 0a 20 20 20 20 20 20 69 6e   ':': {.      in
21e90 74 20 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 2a  t n = 0;.      *
21e91 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 56  tokenType = TK_V
21e92 41 52 49 41 42 4c 45 3b 0a 20 20 20 20 20 20 66  ARIABLE;.      f
21e93 6f 72 28 69 3d 31 3b 20 28 63 3d 7a 5b 69 5d 29  or(i=1; (c=z[i])
21e94 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  !=0; i++){.     
21e95 20 20 20 69 66 28 20 49 64 43 68 61 72 28 63 29     if( IdChar(c)
21e96 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 2b   ){.          n+
21e97 2b 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  +;.#ifndef SQLIT
21e98 45 5f 4f 4d 49 54 5f 54 43 4c 5f 56 41 52 49 41  E_OMIT_TCL_VARIA
21e99 42 4c 45 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  BLE.        }els
21e9a 65 20 69 66 28 20 63 3d 3d 27 28 27 20 26 26 20  e if( c=='(' && 
21e9b 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  n>0 ){.         
21e9c 20 64 6f 7b 0a 20 20 20 20 20 20 20 20 20 20 20   do{.           
21e9d 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20   i++;.          
21e9e 7d 77 68 69 6c 65 28 20 28 63 3d 7a 5b 69 5d 29  }while( (c=z[i])
21e9f 21 3d 30 20 26 26 20 21 69 73 73 70 61 63 65 28  !=0 && !isspace(
21ea0 63 29 20 26 26 20 63 21 3d 27 29 27 20 29 3b 0a  c) && c!=')' );.
21ea1 20 20 20 20 20 20 20 20 20 20 69 66 28 20 63 3d            if( c=
21ea2 3d 27 29 27 20 29 7b 0a 20 20 20 20 20 20 20 20  =')' ){.        
21ea3 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20      i++;.       
21ea4 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
21ea5 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
21ea6 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20   = TK_ILLEGAL;. 
21ea7 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
21ea8 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
21ea9 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d      }else if( c=
21eaa 3d 27 3a 27 20 26 26 20 7a 5b 69 2b 31 5d 3d 3d  =':' && z[i+1]==
21eab 27 3a 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ':' ){.         
21eac 20 69 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20   i++;.#endif.   
21ead 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
21eae 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
21eaf 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
21eb0 20 20 20 20 20 69 66 28 20 6e 3d 3d 30 20 29 20       if( n==0 ) 
21eb1 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
21eb2 49 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20 72  ILLEGAL;.      r
21eb3 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 23  eturn i;.    }.#
21eb4 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
21eb5 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 0a  IT_BLOB_LITERAL.
21eb6 20 20 20 20 63 61 73 65 20 27 78 27 3a 20 63 61      case 'x': ca
21eb7 73 65 20 27 58 27 3a 20 7b 0a 20 20 20 20 20 20  se 'X': {.      
21eb8 69 66 28 20 28 63 3d 7a 5b 31 5d 29 3d 3d 27 5c  if( (c=z[1])=='\
21eb9 27 27 20 7c 7c 20 63 3d 3d 27 22 27 20 29 7b 0a  '' || c=='"' ){.
21eba 20 20 20 20 20 20 20 20 69 6e 74 20 64 65 6c 69          int deli
21ebb 6d 20 3d 20 63 3b 0a 20 20 20 20 20 20 20 20 2a  m = c;.        *
21ebc 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 42  tokenType = TK_B
21ebd 4c 4f 42 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  LOB;.        for
21ebe 28 69 3d 32 3b 20 28 63 3d 7a 5b 69 5d 29 21 3d  (i=2; (c=z[i])!=
21ebf 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  0; i++){.       
21ec0 20 20 20 69 66 28 20 63 3d 3d 64 65 6c 69 6d 20     if( c==delim 
21ec1 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  ){.            i
21ec2 66 28 20 69 25 32 20 29 20 2a 74 6f 6b 65 6e 54  f( i%2 ) *tokenT
21ec3 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c  ype = TK_ILLEGAL
21ec4 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72  ;.            br
21ec5 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  eak;.          }
21ec6 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21  .          if( !
21ec7 69 73 78 64 69 67 69 74 28 63 29 20 29 7b 0a 20  isxdigit(c) ){. 
21ec8 20 20 20 20 20 20 20 20 20 20 20 2a 74 6f 6b 65             *toke
21ec9 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47  nType = TK_ILLEG
21eca 41 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  AL;.            
21ecb 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 20 20  return i;.      
21ecc 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
21ecd 20 20 20 20 20 20 20 20 69 66 28 20 63 20 29 20          if( c ) 
21ece 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 72 65 74  i++;.        ret
21ecf 75 72 6e 20 69 3b 0a 20 20 20 20 20 20 7d 0a 20  urn i;.      }. 
21ed0 20 20 20 20 20 2f 2a 20 4f 74 68 65 72 77 69 73       /* Otherwis
21ed1 65 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74  e fall through t
21ed2 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73 65 20  o the next case 
21ed3 2a 2f 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  */.    }.#endif.
21ed4 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
21ed5 20 20 20 20 20 69 66 28 20 21 49 64 43 68 61 72       if( !IdChar
21ed6 28 2a 7a 29 20 29 7b 0a 20 20 20 20 20 20 20 20  (*z) ){.        
21ed7 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
21ed8 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 49 64       for(i=1; Id
21ed9 43 68 61 72 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29  Char(z[i]); i++)
21eda 7b 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  {}.      *tokenT
21edb 79 70 65 20 3d 20 6b 65 79 77 6f 72 64 43 6f 64  ype = keywordCod
21edc 65 28 28 63 68 61 72 2a 29 7a 2c 20 69 29 3b 0a  e((char*)z, i);.
21edd 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a        return i;.
21ede 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 74 6f 6b      }.  }.  *tok
21edf 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45  enType = TK_ILLE
21ee0 47 41 4c 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b  GAL;.  return 1;
21ee1 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
21ee2 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74  E int sqlite3Get
21ee3 54 6f 6b 65 6e 28 63 6f 6e 73 74 20 75 6e 73 69  Token(const unsi
21ee4 67 6e 65 64 20 63 68 61 72 20 2a 7a 2c 20 69 6e  gned char *z, in
21ee5 74 20 2a 74 6f 6b 65 6e 54 79 70 65 29 7b 0a 20  t *tokenType){. 
21ee6 20 72 65 74 75 72 6e 20 67 65 74 54 6f 6b 65 6e   return getToken
21ee7 28 7a 2c 20 74 6f 6b 65 6e 54 79 70 65 29 3b 0a  (z, tokenType);.
21ee8 7d 0a 0a 2f 2a 0a 2a 2a 20 52 75 6e 20 74 68 65  }../*.** Run the
21ee9 20 70 61 72 73 65 72 20 6f 6e 20 74 68 65 20 67   parser on the g
21eea 69 76 65 6e 20 53 51 4c 20 73 74 72 69 6e 67 2e  iven SQL string.
21eeb 20 20 54 68 65 20 70 61 72 73 65 72 20 73 74 72    The parser str
21eec 75 63 74 75 72 65 20 69 73 0a 2a 2a 20 70 61 73  ucture is.** pas
21eed 73 65 64 20 69 6e 2e 20 20 41 6e 20 53 51 4c 49  sed in.  An SQLI
21eee 54 45 5f 20 73 74 61 74 75 73 20 63 6f 64 65 20  TE_ status code 
21eef 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66  is returned.  If
21ef0 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
21ef1 0a 2a 2a 20 61 6e 64 20 70 7a 45 72 72 4d 73 67  .** and pzErrMsg
21ef2 21 3d 4e 55 4c 4c 20 74 68 65 6e 20 61 6e 20 65  !=NULL then an e
21ef3 72 72 6f 72 20 6d 65 73 73 61 67 65 20 6d 69 67  rror message mig
21ef4 68 74 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e  ht be written in
21ef5 74 6f 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 6f 62  to .** memory ob
21ef6 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c  tained from mall
21ef7 6f 63 28 29 20 61 6e 64 20 2a 70 7a 45 72 72 4d  oc() and *pzErrM
21ef8 73 67 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74  sg made to point
21ef9 20 74 6f 20 74 68 61 74 0a 2a 2a 20 65 72 72 6f   to that.** erro
21efa 72 20 6d 65 73 73 61 67 65 2e 20 20 4f 72 20 6d  r message.  Or m
21efb 61 79 62 65 20 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c  aybe not..*/.SQL
21efc 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
21efd 73 71 6c 69 74 65 33 52 75 6e 50 61 72 73 65 72  sqlite3RunParser
21efe 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
21eff 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c  const char *zSql
21f00 2c 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73  , char **pzErrMs
21f01 67 29 7b 0a 20 20 69 6e 74 20 6e 45 72 72 20 3d  g){.  int nErr =
21f02 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 76   0;.  int i;.  v
21f03 6f 69 64 20 2a 70 45 6e 67 69 6e 65 3b 0a 20 20  oid *pEngine;.  
21f04 69 6e 74 20 74 6f 6b 65 6e 54 79 70 65 3b 0a 20  int tokenType;. 
21f05 20 69 6e 74 20 6c 61 73 74 54 6f 6b 65 6e 50 61   int lastTokenPa
21f06 72 73 65 64 20 3d 20 2d 31 3b 0a 20 20 73 71 6c  rsed = -1;.  sql
21f07 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
21f08 65 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20 64 62  e->db;..  if( db
21f09 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3d  ->activeVdbeCnt=
21f0a 3d 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e 75 31  =0 ){.    db->u1
21f0b 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 3d  .isInterrupted =
21f0c 20 30 3b 0a 20 20 7d 0a 20 20 70 50 61 72 73 65   0;.  }.  pParse
21f0d 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  ->rc = SQLITE_OK
21f0e 3b 0a 20 20 69 20 3d 20 30 3b 0a 20 20 70 45 6e  ;.  i = 0;.  pEn
21f0f 67 69 6e 65 20 3d 20 73 71 6c 69 74 65 33 50 61  gine = sqlite3Pa
21f10 72 73 65 72 41 6c 6c 6f 63 28 28 76 6f 69 64 2a  rserAlloc((void*
21f11 28 2a 29 28 73 69 7a 65 5f 74 29 29 73 71 6c 69  (*)(size_t))sqli
21f12 74 65 33 4d 61 6c 6c 6f 63 58 29 3b 0a 20 20 69  te3MallocX);.  i
21f13 66 28 20 70 45 6e 67 69 6e 65 3d 3d 30 20 29 7b  f( pEngine==0 ){
21f14 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
21f15 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
21f16 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e  assert( pParse->
21f17 73 4c 61 73 74 54 6f 6b 65 6e 2e 64 79 6e 3d 3d  sLastToken.dyn==
21f18 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
21f19 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
21f1a 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
21f1b 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69   pParse->pNewTri
21f1c 67 67 65 72 3d 3d 30 20 29 3b 0a 20 20 61 73 73  gger==0 );.  ass
21f1d 65 72 74 28 20 70 50 61 72 73 65 2d 3e 6e 56 61  ert( pParse->nVa
21f1e 72 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  r==0 );.  assert
21f1f 28 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78  ( pParse->nVarEx
21f20 70 72 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  pr==0 );.  asser
21f21 74 28 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45  t( pParse->nVarE
21f22 78 70 72 41 6c 6c 6f 63 3d 3d 30 20 29 3b 0a 20  xprAlloc==0 );. 
21f23 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d   assert( pParse-
21f24 3e 61 70 56 61 72 45 78 70 72 3d 3d 30 20 29 3b  >apVarExpr==0 );
21f25 0a 20 20 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c  .  pParse->zTail
21f26 20 3d 20 70 50 61 72 73 65 2d 3e 7a 53 71 6c 20   = pParse->zSql 
21f27 3d 20 7a 53 71 6c 3b 0a 20 20 77 68 69 6c 65 28  = zSql;.  while(
21f28 20 21 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 46   !sqlite3MallocF
21f29 61 69 6c 65 64 28 29 20 26 26 20 7a 53 71 6c 5b  ailed() && zSql[
21f2a 69 5d 21 3d 30 20 29 7b 0a 20 20 20 20 61 73 73  i]!=0 ){.    ass
21f2b 65 72 74 28 20 69 3e 3d 30 20 29 3b 0a 20 20 20  ert( i>=0 );.   
21f2c 20 70 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f   pParse->sLastTo
21f2d 6b 65 6e 2e 7a 20 3d 20 28 75 38 2a 29 26 7a 53  ken.z = (u8*)&zS
21f2e 71 6c 5b 69 5d 3b 0a 20 20 20 20 61 73 73 65 72  ql[i];.    asser
21f2f 74 28 20 70 50 61 72 73 65 2d 3e 73 4c 61 73 74  t( pParse->sLast
21f30 54 6f 6b 65 6e 2e 64 79 6e 3d 3d 30 20 29 3b 0a  Token.dyn==0 );.
21f31 20 20 20 20 70 50 61 72 73 65 2d 3e 73 4c 61 73      pParse->sLas
21f32 74 54 6f 6b 65 6e 2e 6e 20 3d 20 67 65 74 54 6f  tToken.n = getTo
21f33 6b 65 6e 28 28 75 6e 73 69 67 6e 65 64 20 63 68  ken((unsigned ch
21f34 61 72 2a 29 26 7a 53 71 6c 5b 69 5d 2c 26 74 6f  ar*)&zSql[i],&to
21f35 6b 65 6e 54 79 70 65 29 3b 0a 20 20 20 20 69 20  kenType);.    i 
21f36 2b 3d 20 70 50 61 72 73 65 2d 3e 73 4c 61 73 74  += pParse->sLast
21f37 54 6f 6b 65 6e 2e 6e 3b 0a 20 20 20 20 69 66 28  Token.n;.    if(
21f38 20 69 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51   i>SQLITE_MAX_SQ
21f39 4c 5f 4c 45 4e 47 54 48 20 29 7b 0a 20 20 20 20  L_LENGTH ){.    
21f3a 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53    pParse->rc = S
21f3b 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20  QLITE_TOOBIG;.  
21f3c 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
21f3d 0a 20 20 20 20 73 77 69 74 63 68 28 20 74 6f 6b  .    switch( tok
21f3e 65 6e 54 79 70 65 20 29 7b 0a 20 20 20 20 20 20  enType ){.      
21f3f 63 61 73 65 20 54 4b 5f 53 50 41 43 45 3a 0a 20  case TK_SPACE:. 
21f40 20 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4d       case TK_COM
21f41 4d 45 4e 54 3a 20 7b 0a 20 20 20 20 20 20 20 20  MENT: {.        
21f42 69 66 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74  if( db->u1.isInt
21f43 65 72 72 75 70 74 65 64 20 29 7b 0a 20 20 20 20  errupted ){.    
21f44 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63        pParse->rc
21f45 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52   = SQLITE_INTERR
21f46 55 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 73  UPT;.          s
21f47 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
21f48 70 7a 45 72 72 4d 73 67 2c 20 22 69 6e 74 65 72  pzErrMsg, "inter
21f49 72 75 70 74 22 2c 20 28 63 68 61 72 2a 29 30 29  rupt", (char*)0)
21f4a 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  ;.          goto
21f4b 20 61 62 6f 72 74 5f 70 61 72 73 65 3b 0a 20 20   abort_parse;.  
21f4c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
21f4d 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
21f4e 20 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4c 4c       case TK_ILL
21f4f 45 47 41 4c 3a 20 7b 0a 20 20 20 20 20 20 20 20  EGAL: {.        
21f50 69 66 28 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a  if( pzErrMsg ){.
21f51 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
21f52 46 72 65 65 28 2a 70 7a 45 72 72 4d 73 67 29 3b  Free(*pzErrMsg);
21f53 0a 20 20 20 20 20 20 20 20 20 20 2a 70 7a 45 72  .          *pzEr
21f54 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 50  rMsg = sqlite3MP
21f55 72 69 6e 74 66 28 22 75 6e 72 65 63 6f 67 6e 69  rintf("unrecogni
21f56 7a 65 64 20 74 6f 6b 65 6e 3a 20 5c 22 25 54 5c  zed token: \"%T\
21f57 22 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  "",.            
21f58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 70                &p
21f59 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65  Parse->sLastToke
21f5a 6e 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  n);.        }.  
21f5b 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20        nErr++;.  
21f5c 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74        goto abort
21f5d 5f 70 61 72 73 65 3b 0a 20 20 20 20 20 20 7d 0a  _parse;.      }.
21f5e 20 20 20 20 20 20 63 61 73 65 20 54 4b 5f 53 45        case TK_SE
21f5f 4d 49 3a 20 7b 0a 20 20 20 20 20 20 20 20 70 50  MI: {.        pP
21f60 61 72 73 65 2d 3e 7a 54 61 69 6c 20 3d 20 26 7a  arse->zTail = &z
21f61 53 71 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20  Sql[i];.        
21f62 2f 2a 20 46 61 6c 6c 20 74 68 72 75 20 69 6e 74  /* Fall thru int
21f63 6f 20 74 68 65 20 64 65 66 61 75 6c 74 20 63 61  o the default ca
21f64 73 65 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20  se */.      }.  
21f65 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
21f66 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61         sqlite3Pa
21f67 72 73 65 72 28 70 45 6e 67 69 6e 65 2c 20 74 6f  rser(pEngine, to
21f68 6b 65 6e 54 79 70 65 2c 20 70 50 61 72 73 65 2d  kenType, pParse-
21f69 3e 73 4c 61 73 74 54 6f 6b 65 6e 2c 20 70 50 61  >sLastToken, pPa
21f6a 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 6c 61  rse);.        la
21f6b 73 74 54 6f 6b 65 6e 50 61 72 73 65 64 20 3d 20  stTokenParsed = 
21f6c 74 6f 6b 65 6e 54 79 70 65 3b 0a 20 20 20 20 20  tokenType;.     
21f6d 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 72     if( pParse->r
21f6e 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
21f6f 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61            goto a
21f70 62 6f 72 74 5f 70 61 72 73 65 3b 0a 20 20 20 20  bort_parse;.    
21f71 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72      }.        br
21f72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
21f73 20 7d 0a 20 20 7d 0a 61 62 6f 72 74 5f 70 61 72   }.  }.abort_par
21f74 73 65 3a 0a 20 20 69 66 28 20 7a 53 71 6c 5b 69  se:.  if( zSql[i
21f75 5d 3d 3d 30 20 26 26 20 6e 45 72 72 3d 3d 30 20  ]==0 && nErr==0 
21f76 26 26 20 70 50 61 72 73 65 2d 3e 72 63 3d 3d 53  && pParse->rc==S
21f77 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
21f78 69 66 28 20 6c 61 73 74 54 6f 6b 65 6e 50 61 72  if( lastTokenPar
21f79 73 65 64 21 3d 54 4b 5f 53 45 4d 49 20 29 7b 0a  sed!=TK_SEMI ){.
21f7a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 72        sqlite3Par
21f7b 73 65 72 28 70 45 6e 67 69 6e 65 2c 20 54 4b 5f  ser(pEngine, TK_
21f7c 53 45 4d 49 2c 20 70 50 61 72 73 65 2d 3e 73 4c  SEMI, pParse->sL
21f7d 61 73 74 54 6f 6b 65 6e 2c 20 70 50 61 72 73 65  astToken, pParse
21f7e 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  );.      pParse-
21f7f 3e 7a 54 61 69 6c 20 3d 20 26 7a 53 71 6c 5b 69  >zTail = &zSql[i
21f80 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  ];.    }.    sql
21f81 69 74 65 33 50 61 72 73 65 72 28 70 45 6e 67 69  ite3Parser(pEngi
21f82 6e 65 2c 20 30 2c 20 70 50 61 72 73 65 2d 3e 73  ne, 0, pParse->s
21f83 4c 61 73 74 54 6f 6b 65 6e 2c 20 70 50 61 72 73  LastToken, pPars
21f84 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  e);.  }.  sqlite
21f85 33 50 61 72 73 65 72 46 72 65 65 28 70 45 6e 67  3ParserFree(pEng
21f86 69 6e 65 2c 20 73 71 6c 69 74 65 33 46 72 65 65  ine, sqlite3Free
21f87 58 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  X);.  if( sqlite
21f88 33 4d 61 6c 6c 6f 63 46 61 69 6c 65 64 28 29 20  3MallocFailed() 
21f89 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72  ){.    pParse->r
21f8a 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
21f8b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 72  ;.  }.  if( pPar
21f8c 73 65 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 4f  se->rc!=SQLITE_O
21f8d 4b 20 26 26 20 70 50 61 72 73 65 2d 3e 72 63 21  K && pParse->rc!
21f8e 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 26 26 20  =SQLITE_DONE && 
21f8f 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 3d  pParse->zErrMsg=
21f90 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
21f91 33 53 65 74 53 74 72 69 6e 67 28 26 70 50 61 72  3SetString(&pPar
21f92 73 65 2d 3e 7a 45 72 72 4d 73 67 2c 20 73 71 6c  se->zErrMsg, sql
21f93 69 74 65 33 45 72 72 53 74 72 28 70 50 61 72 73  ite3ErrStr(pPars
21f94 65 2d 3e 72 63 29 2c 20 28 63 68 61 72 2a 29 30  e->rc), (char*)0
21f95 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61  );.  }.  if( pPa
21f96 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 29 7b 0a  rse->zErrMsg ){.
21f97 20 20 20 20 69 66 28 20 70 7a 45 72 72 4d 73 67      if( pzErrMsg
21f98 20 26 26 20 2a 70 7a 45 72 72 4d 73 67 3d 3d 30   && *pzErrMsg==0
21f99 20 29 7b 0a 20 20 20 20 20 20 2a 70 7a 45 72 72   ){.      *pzErr
21f9a 4d 73 67 20 3d 20 70 50 61 72 73 65 2d 3e 7a 45  Msg = pParse->zE
21f9b 72 72 4d 73 67 3b 0a 20 20 20 20 7d 65 6c 73 65  rrMsg;.    }else
21f9c 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 46 72  {.      sqliteFr
21f9d 65 65 28 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d  ee(pParse->zErrM
21f9e 73 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  sg);.    }.    p
21f9f 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 3d  Parse->zErrMsg =
21fa0 20 30 3b 0a 20 20 20 20 69 66 28 20 21 6e 45 72   0;.    if( !nEr
21fa1 72 20 29 20 6e 45 72 72 2b 2b 3b 0a 20 20 7d 0a  r ) nErr++;.  }.
21fa2 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 70 56    if( pParse->pV
21fa3 64 62 65 20 26 26 20 70 50 61 72 73 65 2d 3e 6e  dbe && pParse->n
21fa4 45 72 72 3e 30 20 26 26 20 70 50 61 72 73 65 2d  Err>0 && pParse-
21fa5 3e 6e 65 73 74 65 64 3d 3d 30 20 29 7b 0a 20 20  >nested==0 ){.  
21fa6 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c    sqlite3VdbeDel
21fa7 65 74 65 28 70 50 61 72 73 65 2d 3e 70 56 64 62  ete(pParse->pVdb
21fa8 65 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  e);.    pParse->
21fa9 70 56 64 62 65 20 3d 20 30 3b 0a 20 20 7d 0a 23  pVdbe = 0;.  }.#
21faa 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
21fab 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a  IT_SHARED_CACHE.
21fac 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65    if( pParse->ne
21fad 73 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 73  sted==0 ){.    s
21fae 71 6c 69 74 65 46 72 65 65 28 70 50 61 72 73 65  qliteFree(pParse
21faf 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 29 3b 0a 20  ->aTableLock);. 
21fb0 20 20 20 70 50 61 72 73 65 2d 3e 61 54 61 62 6c     pParse->aTabl
21fb1 65 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20 70  eLock = 0;.    p
21fb2 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f 63  Parse->nTableLoc
21fb3 6b 20 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e 64 69  k = 0;.  }.#endi
21fb4 66 0a 0a 20 20 69 66 28 20 21 49 4e 5f 44 45 43  f..  if( !IN_DEC
21fb5 4c 41 52 45 5f 56 54 41 42 20 29 7b 0a 20 20 20  LARE_VTAB ){.   
21fb6 20 2f 2a 20 49 66 20 74 68 65 20 70 50 61 72 73   /* If the pPars
21fb7 65 2d 3e 64 65 63 6c 61 72 65 56 74 61 62 20 66  e->declareVtab f
21fb8 6c 61 67 20 69 73 20 73 65 74 2c 20 64 6f 20 6e  lag is set, do n
21fb9 6f 74 20 64 65 6c 65 74 65 20 61 6e 79 20 74 61  ot delete any ta
21fba 62 6c 65 20 0a 20 20 20 20 2a 2a 20 73 74 72 75  ble .    ** stru
21fbb 63 74 75 72 65 20 62 75 69 6c 74 20 75 70 20 69  cture built up i
21fbc 6e 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  n pParse->pNewTa
21fbd 62 6c 65 2e 20 54 68 65 20 63 61 6c 6c 69 6e 67  ble. The calling
21fbe 20 63 6f 64 65 20 28 73 65 65 20 76 74 61 62 2e   code (see vtab.
21fbf 63 29 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 74  c).    ** will t
21fc0 61 6b 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69  ake responsibili
21fc1 74 79 20 66 6f 72 20 66 72 65 65 69 6e 67 20 74  ty for freeing t
21fc2 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75  he Table structu
21fc3 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73  re..    */.    s
21fc4 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c  qlite3DeleteTabl
21fc5 65 28 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  e(pParse->pNewTa
21fc6 62 6c 65 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c  ble);.  }..  sql
21fc7 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65  ite3DeleteTrigge
21fc8 72 28 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72  r(pParse->pNewTr
21fc9 69 67 67 65 72 29 3b 0a 20 20 73 71 6c 69 74 65  igger);.  sqlite
21fca 46 72 65 65 28 70 50 61 72 73 65 2d 3e 61 70 56  Free(pParse->apV
21fcb 61 72 45 78 70 72 29 3b 0a 20 20 69 66 28 20 6e  arExpr);.  if( n
21fcc 45 72 72 3e 30 20 26 26 20 28 70 50 61 72 73 65  Err>0 && (pParse
21fcd 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ->rc==SQLITE_OK 
21fce 7c 7c 20 70 50 61 72 73 65 2d 3e 72 63 3d 3d 53  || pParse->rc==S
21fcf 51 4c 49 54 45 5f 44 4f 4e 45 29 20 29 7b 0a 20  QLITE_DONE) ){. 
21fd0 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20     pParse->rc = 
21fd1 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
21fd2 7d 0a 20 20 72 65 74 75 72 6e 20 6e 45 72 72 3b  }.  return nErr;
21fd3 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
21fd4 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 6f 6b 65 6e  *** End of token
21fd5 69 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ize.c **********
21fd6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21fd7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21fd8 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
21fd9 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d  *** Begin file m
21fda 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ain.c **********
21fdb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21fdc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21fdd 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53  **/./*.** 2001 S
21fde 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a  eptember 15.**.*
21fdf 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
21fe0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
21fe1 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
21fe2 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
21fe3 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
21fe4 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
21fe5 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
21fe6 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
21fe7 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
21fe8 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
21fe9 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
21fea 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
21feb 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
21fec 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
21fed 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
21fee 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
21fef 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
21ff0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
21ff1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21ff2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21ff3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21ff4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 4d 61  **********.** Ma
21ff5 69 6e 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20  in file for the 
21ff6 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 20  SQLite library. 
21ff7 20 54 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e   The routines in
21ff8 20 74 68 69 73 20 66 69 6c 65 0a 2a 2a 20 69 6d   this file.** im
21ff9 70 6c 65 6d 65 6e 74 20 74 68 65 20 70 72 6f 67  plement the prog
21ffa 72 61 6d 6d 65 72 20 69 6e 74 65 72 66 61 63 65  rammer interface
21ffb 20 74 6f 20 74 68 65 20 6c 69 62 72 61 72 79 2e   to the library.
21ffc 20 20 52 6f 75 74 69 6e 65 73 20 69 6e 0a 2a 2a    Routines in.**
21ffd 20 6f 74 68 65 72 20 66 69 6c 65 73 20 61 72 65   other files are
21ffe 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73   for internal us
21fff 65 20 62 79 20 53 51 4c 69 74 65 20 61 6e 64 20  e by SQLite and 
22000 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a  should not be.**
22001 20 61 63 63 65 73 73 65 64 20 62 79 20 75 73 65   accessed by use
22002 72 73 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72  rs of the librar
22003 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 61  y..**.** $Id: ma
22004 69 6e 2e 63 2c 76 20 31 2e 33 37 37 20 32 30 30  in.c,v 1.377 200
22005 37 2f 30 36 2f 32 32 20 31 35 3a 32 31 3a 31 36  7/06/22 15:21:16
22006 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70   danielk1977 Exp
22007 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65   $.*/../*.** The
22008 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20   version of the 
22009 6c 69 62 72 61 72 79 0a 2a 2f 0a 63 6f 6e 73 74  library.*/.const
2200a 20 63 68 61 72 20 73 71 6c 69 74 65 33 5f 76 65   char sqlite3_ve
2200b 72 73 69 6f 6e 5b 5d 20 3d 20 53 51 4c 49 54 45  rsion[] = SQLITE
2200c 5f 56 45 52 53 49 4f 4e 3b 0a 53 51 4c 49 54 45  _VERSION;.SQLITE
2200d 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20  _API const char 
2200e 2a 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73  *sqlite3_libvers
2200f 69 6f 6e 28 76 6f 69 64 29 7b 20 72 65 74 75 72  ion(void){ retur
22010 6e 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f  n sqlite3_versio
22011 6e 3b 20 7d 0a 69 6e 74 20 73 71 6c 69 74 65 33  n; }.int sqlite3
22012 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62  _libversion_numb
22013 65 72 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e  er(void){ return
22014 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f   SQLITE_VERSION_
22015 4e 55 4d 42 45 52 3b 20 7d 0a 0a 2f 2a 0a 2a 2a  NUMBER; }../*.**
22016 20 49 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   If the followin
22017 67 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74  g function point
22018 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 61  er is not NULL a
22019 6e 64 20 69 66 0a 2a 2a 20 53 51 4c 49 54 45 5f  nd if.** SQLITE_
2201a 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 20 69  ENABLE_IOTRACE i
2201b 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 6e 20  s enabled, then 
2201c 6d 65 73 73 61 67 65 73 20 64 65 73 63 72 69 62  messages describ
2201d 69 6e 67 0a 2a 2a 20 49 2f 4f 20 61 63 74 69 76  ing.** I/O activ
2201e 65 20 61 72 65 20 77 72 69 74 74 65 6e 20 75 73  e are written us
2201f 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ing this functio
22020 6e 2e 20 20 54 68 65 73 65 20 6d 65 73 73 61 67  n.  These messag
22021 65 73 0a 2a 2a 20 61 72 65 20 69 6e 74 65 6e 64  es.** are intend
22022 65 64 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67  ed for debugging
22023 20 61 63 74 69 76 69 74 79 20 6f 6e 6c 79 2e 0a   activity only..
22024 2a 2f 0a 76 6f 69 64 20 28 2a 73 71 6c 69 74 65  */.void (*sqlite
22025 33 5f 69 6f 5f 74 72 61 63 65 29 28 63 6f 6e 73  3_io_trace)(cons
22026 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 20 3d 20  t char*, ...) = 
22027 30 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  0;../*.** If the
22028 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61   following globa
22029 6c 20 76 61 72 69 61 62 6c 65 20 70 6f 69 6e 74  l variable point
2202a 73 20 74 6f 20 61 20 73 74 72 69 6e 67 20 77 68  s to a string wh
2202b 69 63 68 20 69 73 20 74 68 65 0a 2a 2a 20 6e 61  ich is the.** na
2202c 6d 65 20 6f 66 20 61 20 64 69 72 65 63 74 6f 72  me of a director
2202d 79 2c 20 74 68 65 6e 20 74 68 61 74 20 64 69 72  y, then that dir
2202e 65 63 74 6f 72 79 20 77 69 6c 6c 20 62 65 20 75  ectory will be u
2202f 73 65 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20  sed to store.** 
22030 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2e  temporary files.
22031 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20  .**.** See also 
22032 74 68 65 20 22 50 52 41 47 4d 41 20 74 65 6d 70  the "PRAGMA temp
22033 5f 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79  _store_directory
22034 22 20 53 51 4c 20 63 6f 6d 6d 61 6e 64 2e 0a 2a  " SQL command..*
22035 2f 0a 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  /.char *sqlite3_
22036 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 3d  temp_directory =
22037 20 30 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73   0;.../*.** This
22038 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 20   is the default 
22039 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69  collating functi
2203a 6f 6e 20 6e 61 6d 65 64 20 22 42 49 4e 41 52 59  on named "BINARY
2203b 22 20 77 68 69 63 68 20 69 73 20 61 6c 77 61 79  " which is alway
2203c 73 0a 2a 2a 20 61 76 61 69 6c 61 62 6c 65 2e 0a  s.** available..
2203d 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 69  */.static int bi
2203e 6e 43 6f 6c 6c 46 75 6e 63 28 0a 20 20 76 6f 69  nCollFunc(.  voi
2203f 64 20 2a 4e 6f 74 55 73 65 64 2c 0a 20 20 69 6e  d *NotUsed,.  in
22040 74 20 6e 4b 65 79 31 2c 20 63 6f 6e 73 74 20 76  t nKey1, const v
22041 6f 69 64 20 2a 70 4b 65 79 31 2c 0a 20 20 69 6e  oid *pKey1,.  in
22042 74 20 6e 4b 65 79 32 2c 20 63 6f 6e 73 74 20 76  t nKey2, const v
22043 6f 69 64 20 2a 70 4b 65 79 32 0a 29 7b 0a 20 20  oid *pKey2.){.  
22044 69 6e 74 20 72 63 2c 20 6e 3b 0a 20 20 6e 20 3d  int rc, n;.  n =
22045 20 6e 4b 65 79 31 3c 6e 4b 65 79 32 20 3f 20 6e   nKey1<nKey2 ? n
22046 4b 65 79 31 20 3a 20 6e 4b 65 79 32 3b 0a 20 20  Key1 : nKey2;.  
22047 72 63 20 3d 20 6d 65 6d 63 6d 70 28 70 4b 65 79  rc = memcmp(pKey
22048 31 2c 20 70 4b 65 79 32 2c 20 6e 29 3b 0a 20 20  1, pKey2, n);.  
22049 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20  if( rc==0 ){.   
2204a 20 72 63 20 3d 20 6e 4b 65 79 31 20 2d 20 6e 4b   rc = nKey1 - nK
2204b 65 79 32 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ey2;.  }.  retur
2204c 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  n rc;.}../*.** A
2204d 6e 6f 74 68 65 72 20 62 75 69 6c 74 2d 69 6e 20  nother built-in 
2204e 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
2204f 63 65 3a 20 4e 4f 43 41 53 45 2e 20 0a 2a 2a 0a  ce: NOCASE. .**.
22050 2a 2a 20 54 68 69 73 20 63 6f 6c 6c 61 74 69 6e  ** This collatin
22051 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 69 6e  g sequence is in
22052 74 65 6e 64 65 64 20 74 6f 20 62 65 20 75 73 65  tended to be use
22053 64 20 66 6f 72 20 22 63 61 73 65 20 69 6e 64 65  d for "case inde
22054 70 65 6e 64 61 6e 74 0a 2a 2a 20 63 6f 6d 70 61  pendant.** compa
22055 72 69 73 6f 6e 22 2e 20 53 51 4c 69 74 65 27 73  rison". SQLite's
22056 20 6b 6e 6f 77 6c 65 64 67 65 20 6f 66 20 75 70   knowledge of up
22057 70 65 72 20 61 6e 64 20 6c 6f 77 65 72 20 63 61  per and lower ca
22058 73 65 20 65 71 75 69 76 61 6c 65 6e 74 73 0a 2a  se equivalents.*
22059 2a 20 65 78 74 65 6e 64 73 20 6f 6e 6c 79 20 74  * extends only t
2205a 6f 20 74 68 65 20 32 36 20 63 68 61 72 61 63 74  o the 26 charact
2205b 65 72 73 20 75 73 65 64 20 69 6e 20 74 68 65 20  ers used in the 
2205c 45 6e 67 6c 69 73 68 20 6c 61 6e 67 75 61 67 65  English language
2205d 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 74 68 65 20 6d  ..**.** At the m
2205e 6f 6d 65 6e 74 20 74 68 65 72 65 20 69 73 20 6f  oment there is o
2205f 6e 6c 79 20 61 20 55 54 46 2d 38 20 69 6d 70 6c  nly a UTF-8 impl
22060 65 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2f 0a 73  ementation..*/.s
22061 74 61 74 69 63 20 69 6e 74 20 6e 6f 63 61 73 65  tatic int nocase
22062 43 6f 6c 6c 61 74 69 6e 67 46 75 6e 63 28 0a 20  CollatingFunc(. 
22063 20 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 2c 0a   void *NotUsed,.
22064 20 20 69 6e 74 20 6e 4b 65 79 31 2c 20 63 6f 6e    int nKey1, con
22065 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 31 2c 0a  st void *pKey1,.
22066 20 20 69 6e 74 20 6e 4b 65 79 32 2c 20 63 6f 6e    int nKey2, con
22067 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 32 0a 29  st void *pKey2.)
22068 7b 0a 20 20 69 6e 74 20 72 20 3d 20 73 71 6c 69  {.  int r = sqli
22069 74 65 33 53 74 72 4e 49 43 6d 70 28 0a 20 20 20  te3StrNICmp(.   
2206a 20 20 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a     (const char *
2206b 29 70 4b 65 79 31 2c 20 28 63 6f 6e 73 74 20 63  )pKey1, (const c
2206c 68 61 72 20 2a 29 70 4b 65 79 32 2c 20 28 6e 4b  har *)pKey2, (nK
2206d 65 79 31 3c 6e 4b 65 79 32 29 3f 6e 4b 65 79 31  ey1<nKey2)?nKey1
2206e 3a 6e 4b 65 79 32 29 3b 0a 20 20 69 66 28 20 30  :nKey2);.  if( 0
2206f 3d 3d 72 20 29 7b 0a 20 20 20 20 72 20 3d 20 6e  ==r ){.    r = n
22070 4b 65 79 31 2d 6e 4b 65 79 32 3b 0a 20 20 7d 0a  Key1-nKey2;.  }.
22071 20 20 72 65 74 75 72 6e 20 72 3b 0a 7d 0a 0a 2f    return r;.}../
22072 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
22073 52 4f 57 49 44 20 6f 66 20 74 68 65 20 6d 6f 73  ROWID of the mos
22074 74 20 72 65 63 65 6e 74 20 69 6e 73 65 72 74 0a  t recent insert.
22075 2a 2f 0a 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  */.sqlite_int64 
22076 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73  sqlite3_last_ins
22077 65 72 74 5f 72 6f 77 69 64 28 73 71 6c 69 74 65  ert_rowid(sqlite
22078 33 20 2a 64 62 29 7b 0a 20 20 72 65 74 75 72 6e  3 *db){.  return
22079 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a   db->lastRowid;.
2207a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
2207b 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68  the number of ch
2207c 61 6e 67 65 73 20 69 6e 20 74 68 65 20 6d 6f 73  anges in the mos
2207d 74 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f  t recent call to
2207e 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 2e   sqlite3_exec().
2207f 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
22080 6e 74 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67  nt sqlite3_chang
22081 65 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  es(sqlite3 *db){
22082 0a 20 20 72 65 74 75 72 6e 20 64 62 2d 3e 6e 43  .  return db->nC
22083 68 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  hange;.}../*.** 
22084 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
22085 72 20 6f 66 20 63 68 61 6e 67 65 73 20 73 69 6e  r of changes sin
22086 63 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ce the database 
22087 68 61 6e 64 6c 65 20 77 61 73 20 6f 70 65 6e 65  handle was opene
22088 64 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65  d..*/.int sqlite
22089 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28  3_total_changes(
2208a 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
2208b 72 65 74 75 72 6e 20 64 62 2d 3e 6e 54 6f 74 61  return db->nTota
2208c 6c 43 68 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  lChange;.}../*.*
2208d 2a 20 43 6c 6f 73 65 20 61 6e 20 65 78 69 73 74  * Close an exist
2208e 69 6e 67 20 53 51 4c 69 74 65 20 64 61 74 61 62  ing SQLite datab
2208f 61 73 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ase.*/.SQLITE_AP
22090 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6c  I int sqlite3_cl
22091 6f 73 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29  ose(sqlite3 *db)
22092 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 69 3b  {.  HashElem *i;
22093 0a 20 20 69 6e 74 20 6a 3b 0a 0a 20 20 69 66 28  .  int j;..  if(
22094 20 21 64 62 20 29 7b 0a 20 20 20 20 72 65 74 75   !db ){.    retu
22095 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
22096 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  }.  if( sqlite3S
22097 61 66 65 74 79 43 68 65 63 6b 28 64 62 29 20 29  afetyCheck(db) )
22098 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
22099 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a  ITE_MISUSE;.  }.
2209a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53  .#ifdef SQLITE_S
2209b 53 45 0a 20 20 7b 0a 20 20 20 20 65 78 74 65 72  SE.  {.    exter
2209c 6e 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 73  n void sqlite3Ss
2209d 65 43 6c 65 61 6e 75 70 28 73 71 6c 69 74 65 33  eCleanup(sqlite3
2209e 2a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53  *);.    sqlite3S
2209f 73 65 43 6c 65 61 6e 75 70 28 64 62 29 3b 0a 20  seCleanup(db);. 
220a0 20 7d 0a 23 65 6e 64 69 66 20 0a 0a 20 20 73 71   }.#endif ..  sq
220a1 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e  lite3ResetIntern
220a2 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b  alSchema(db, 0);
220a3 0a 0a 20 20 2f 2a 20 49 66 20 61 20 74 72 61 6e  ..  /* If a tran
220a4 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 2c  saction is open,
220a5 20 74 68 65 20 52 65 73 65 74 49 6e 74 65 72 6e   the ResetIntern
220a6 61 6c 53 63 68 65 6d 61 28 29 20 63 61 6c 6c 20  alSchema() call 
220a7 61 62 6f 76 65 0a 20 20 2a 2a 20 77 69 6c 6c 20  above.  ** will 
220a8 6e 6f 74 20 68 61 76 65 20 63 61 6c 6c 65 64 20  not have called 
220a9 74 68 65 20 78 44 69 73 63 6f 6e 6e 65 63 74 28  the xDisconnect(
220aa 29 20 6d 65 74 68 6f 64 20 6f 6e 20 61 6e 79 20  ) method on any 
220ab 76 69 72 74 75 61 6c 0a 20 20 2a 2a 20 74 61 62  virtual.  ** tab
220ac 6c 65 73 20 69 6e 20 74 68 65 20 64 62 2d 3e 61  les in the db->a
220ad 56 54 72 61 6e 73 5b 5d 20 61 72 72 61 79 2e 20  VTrans[] array. 
220ae 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 71  The following sq
220af 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 61 63  lite3VtabRollbac
220b0 6b 28 29 0a 20 20 2a 2a 20 63 61 6c 6c 20 77 69  k().  ** call wi
220b1 6c 6c 20 64 6f 20 73 6f 2e 20 57 65 20 6e 65 65  ll do so. We nee
220b2 64 20 74 6f 20 64 6f 20 74 68 69 73 20 62 65 66  d to do this bef
220b3 6f 72 65 20 74 68 65 20 63 68 65 63 6b 20 66 6f  ore the check fo
220b4 72 20 61 63 74 69 76 65 0a 20 20 2a 2a 20 53 51  r active.  ** SQ
220b5 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 62 65 6c  L statements bel
220b6 6f 77 2c 20 61 73 20 74 68 65 20 76 2d 74 61 62  ow, as the v-tab
220b7 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  le implementatio
220b8 6e 20 6d 61 79 20 62 65 20 73 74 6f 72 69 6e 67  n may be storing
220b9 0a 20 20 2a 2a 20 73 6f 6d 65 20 70 72 65 70 61  .  ** some prepa
220ba 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20 69  red statements i
220bb 6e 74 65 72 6e 61 6c 6c 79 2e 0a 20 20 2a 2f 0a  nternally..  */.
220bc 20 20 73 71 6c 69 74 65 33 56 74 61 62 52 6f 6c    sqlite3VtabRol
220bd 6c 62 61 63 6b 28 64 62 29 3b 0a 0a 20 20 2f 2a  lback(db);..  /*
220be 20 49 66 20 74 68 65 72 65 20 61 72 65 20 61 6e   If there are an
220bf 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 56 4d  y outstanding VM
220c0 73 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  s, return SQLITE
220c1 5f 42 55 53 59 2e 20 2a 2f 0a 20 20 69 66 28 20  _BUSY. */.  if( 
220c2 64 62 2d 3e 70 56 64 62 65 20 29 7b 0a 20 20 20  db->pVdbe ){.   
220c3 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62   sqlite3Error(db
220c4 2c 20 53 51 4c 49 54 45 5f 42 55 53 59 2c 20 0a  , SQLITE_BUSY, .
220c5 20 20 20 20 20 20 20 20 22 55 6e 61 62 6c 65 20          "Unable 
220c6 74 6f 20 63 6c 6f 73 65 20 64 75 65 20 74 6f 20  to close due to 
220c7 75 6e 66 69 6e 61 6c 69 73 65 64 20 73 74 61 74  unfinalised stat
220c8 65 6d 65 6e 74 73 22 29 3b 0a 20 20 20 20 72 65  ements");.    re
220c9 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59  turn SQLITE_BUSY
220ca 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
220cb 21 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68  !sqlite3SafetyCh
220cc 65 63 6b 28 64 62 29 20 29 3b 0a 0a 20 20 2f 2a  eck(db) );..  /*
220cd 20 46 49 58 20 4d 45 3a 20 64 62 2d 3e 6d 61 67   FIX ME: db->mag
220ce 69 63 20 6d 61 79 20 62 65 20 73 65 74 20 74 6f  ic may be set to
220cf 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 43 4c   SQLITE_MAGIC_CL
220d0 4f 53 45 44 20 69 66 20 74 68 65 20 64 61 74 61  OSED if the data
220d1 62 61 73 65 0a 20 20 2a 2a 20 63 61 6e 6e 6f 74  base.  ** cannot
220d2 20 62 65 20 6f 70 65 6e 65 64 20 66 6f 72 20 73   be opened for s
220d3 6f 6d 65 20 72 65 61 73 6f 6e 2e 20 53 6f 20 74  ome reason. So t
220d4 68 69 73 20 72 6f 75 74 69 6e 65 20 6e 65 65 64  his routine need
220d5 73 20 74 6f 20 72 75 6e 20 69 6e 0a 20 20 2a 2a  s to run in.  **
220d6 20 74 68 61 74 20 63 61 73 65 2e 20 42 75 74 20   that case. But 
220d7 6d 61 79 62 65 20 74 68 65 72 65 20 73 68 6f 75  maybe there shou
220d8 6c 64 20 62 65 20 61 6e 20 65 78 74 72 61 20 6d  ld be an extra m
220d9 61 67 69 63 20 76 61 6c 75 65 20 66 6f 72 20 74  agic value for t
220da 68 65 0a 20 20 2a 2a 20 22 66 61 69 6c 65 64 20  he.  ** "failed 
220db 74 6f 20 6f 70 65 6e 22 20 73 74 61 74 65 2e 0a  to open" state..
220dc 20 20 2a 2a 0a 20 20 2a 2a 20 54 4f 44 4f 3a 20    **.  ** TODO: 
220dd 43 6f 76 65 72 61 67 65 20 74 65 73 74 73 20 64  Coverage tests d
220de 6f 20 6e 6f 74 20 74 65 73 74 20 74 68 65 20 63  o not test the c
220df 61 73 65 20 77 68 65 72 65 20 74 68 69 73 20 63  ase where this c
220e0 6f 6e 64 69 74 69 6f 6e 20 69 73 0a 20 20 2a 2a  ondition is.  **
220e1 20 74 72 75 65 2e 20 49 74 27 73 20 68 61 72 64   true. It's hard
220e2 20 74 6f 20 73 65 65 20 68 6f 77 20 74 6f 20 63   to see how to c
220e3 61 75 73 65 20 69 74 20 77 69 74 68 6f 75 74 20  ause it without 
220e4 6d 65 73 73 69 6e 67 20 77 69 74 68 20 74 68 72  messing with thr
220e5 65 61 64 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  eads..  */.  if(
220e6 20 64 62 2d 3e 6d 61 67 69 63 21 3d 53 51 4c 49   db->magic!=SQLI
220e7 54 45 5f 4d 41 47 49 43 5f 43 4c 4f 53 45 44 20  TE_MAGIC_CLOSED 
220e8 26 26 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  && sqlite3Safety
220e9 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 2f 2a  On(db) ){.    /*
220ea 20 70 72 69 6e 74 66 28 22 44 49 44 20 4e 4f 54   printf("DID NOT
220eb 20 43 4c 4f 53 45 5c 6e 22 29 3b 20 66 66 6c 75   CLOSE\n"); fflu
220ec 73 68 28 73 74 64 6f 75 74 29 3b 20 2a 2f 0a 20  sh(stdout); */. 
220ed 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
220ee 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 66  _ERROR;.  }..  f
220ef 6f 72 28 6a 3d 30 3b 20 6a 3c 64 62 2d 3e 6e 44  or(j=0; j<db->nD
220f0 62 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 73 74 72  b; j++){.    str
220f1 75 63 74 20 44 62 20 2a 70 44 62 20 3d 20 26 64  uct Db *pDb = &d
220f2 62 2d 3e 61 44 62 5b 6a 5d 3b 0a 20 20 20 20 69  b->aDb[j];.    i
220f3 66 28 20 70 44 62 2d 3e 70 42 74 20 29 7b 0a 20  f( pDb->pBt ){. 
220f4 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
220f5 65 43 6c 6f 73 65 28 70 44 62 2d 3e 70 42 74 29  eClose(pDb->pBt)
220f6 3b 0a 20 20 20 20 20 20 70 44 62 2d 3e 70 42 74  ;.      pDb->pBt
220f7 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20   = 0;.      if( 
220f8 6a 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20  j!=1 ){.        
220f9 70 44 62 2d 3e 70 53 63 68 65 6d 61 20 3d 20 30  pDb->pSchema = 0
220fa 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
220fb 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 65 73    }.  sqlite3Res
220fc 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61  etInternalSchema
220fd 28 64 62 2c 20 30 29 3b 0a 20 20 61 73 73 65 72  (db, 0);.  asser
220fe 74 28 20 64 62 2d 3e 6e 44 62 3c 3d 32 20 29 3b  t( db->nDb<=2 );
220ff 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61  .  assert( db->a
22100 44 62 3d 3d 64 62 2d 3e 61 44 62 53 74 61 74 69  Db==db->aDbStati
22101 63 20 29 3b 0a 20 20 66 6f 72 28 69 3d 73 71 6c  c );.  for(i=sql
22102 69 74 65 48 61 73 68 46 69 72 73 74 28 26 64 62  iteHashFirst(&db
22103 2d 3e 61 46 75 6e 63 29 3b 20 69 3b 20 69 3d 73  ->aFunc); i; i=s
22104 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 69 29  qliteHashNext(i)
22105 29 7b 0a 20 20 20 20 46 75 6e 63 44 65 66 20 2a  ){.    FuncDef *
22106 70 46 75 6e 63 2c 20 2a 70 4e 65 78 74 3b 0a 20  pFunc, *pNext;. 
22107 20 20 20 66 6f 72 28 70 46 75 6e 63 20 3d 20 28     for(pFunc = (
22108 46 75 6e 63 44 65 66 2a 29 73 71 6c 69 74 65 48  FuncDef*)sqliteH
22109 61 73 68 44 61 74 61 28 69 29 3b 20 70 46 75 6e  ashData(i); pFun
2210a 63 3b 20 70 46 75 6e 63 3d 70 4e 65 78 74 29 7b  c; pFunc=pNext){
2210b 0a 20 20 20 20 20 20 70 4e 65 78 74 20 3d 20 70  .      pNext = p
2210c 46 75 6e 63 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  Func->pNext;.   
2210d 20 20 20 73 71 6c 69 74 65 46 72 65 65 28 70 46     sqliteFree(pF
2210e 75 6e 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  unc);.    }.  }.
2210f 0a 20 20 66 6f 72 28 69 3d 73 71 6c 69 74 65 48  .  for(i=sqliteH
22110 61 73 68 46 69 72 73 74 28 26 64 62 2d 3e 61 43  ashFirst(&db->aC
22111 6f 6c 6c 53 65 71 29 3b 20 69 3b 20 69 3d 73 71  ollSeq); i; i=sq
22112 6c 69 74 65 48 61 73 68 4e 65 78 74 28 69 29 29  liteHashNext(i))
22113 7b 0a 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70  {.    CollSeq *p
22114 43 6f 6c 6c 20 3d 20 28 43 6f 6c 6c 53 65 71 20  Coll = (CollSeq 
22115 2a 29 73 71 6c 69 74 65 48 61 73 68 44 61 74 61  *)sqliteHashData
22116 28 69 29 3b 0a 20 20 20 20 2f 2a 20 49 6e 76 6f  (i);.    /* Invo
22117 6b 65 20 61 6e 79 20 64 65 73 74 72 75 63 74 6f  ke any destructo
22118 72 73 20 72 65 67 69 73 74 65 72 65 64 20 66 6f  rs registered fo
22119 72 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  r collation sequ
2211a 65 6e 63 65 20 75 73 65 72 20 64 61 74 61 2e 20  ence user data. 
2211b 2a 2f 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20  */.    for(j=0; 
2211c 6a 3c 33 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  j<3; j++){.     
2211d 20 69 66 28 20 70 43 6f 6c 6c 5b 6a 5d 2e 78 44   if( pColl[j].xD
2211e 65 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43  el ){.        pC
2211f 6f 6c 6c 5b 6a 5d 2e 78 44 65 6c 28 70 43 6f 6c  oll[j].xDel(pCol
22120 6c 5b 6a 5d 2e 70 55 73 65 72 29 3b 0a 20 20 20  l[j].pUser);.   
22121 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73     }.    }.    s
22122 71 6c 69 74 65 46 72 65 65 28 70 43 6f 6c 6c 29  qliteFree(pColl)
22123 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 48  ;.  }.  sqlite3H
22124 61 73 68 43 6c 65 61 72 28 26 64 62 2d 3e 61 43  ashClear(&db->aC
22125 6f 6c 6c 53 65 71 29 3b 0a 23 69 66 6e 64 65 66  ollSeq);.#ifndef
22126 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
22127 54 55 41 4c 54 41 42 4c 45 0a 20 20 66 6f 72 28  TUALTABLE.  for(
22128 69 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73  i=sqliteHashFirs
22129 74 28 26 64 62 2d 3e 61 4d 6f 64 75 6c 65 29 3b  t(&db->aModule);
2212a 20 69 3b 20 69 3d 73 71 6c 69 74 65 48 61 73 68   i; i=sqliteHash
2212b 4e 65 78 74 28 69 29 29 7b 0a 20 20 20 20 4d 6f  Next(i)){.    Mo
2212c 64 75 6c 65 20 2a 70 4d 6f 64 20 3d 20 28 4d 6f  dule *pMod = (Mo
2212d 64 75 6c 65 20 2a 29 73 71 6c 69 74 65 48 61 73  dule *)sqliteHas
2212e 68 44 61 74 61 28 69 29 3b 0a 20 20 20 20 69 66  hData(i);.    if
2212f 28 20 70 4d 6f 64 2d 3e 78 44 65 73 74 72 6f 79  ( pMod->xDestroy
22130 20 29 7b 0a 20 20 20 20 20 20 70 4d 6f 64 2d 3e   ){.      pMod->
22131 78 44 65 73 74 72 6f 79 28 70 4d 6f 64 2d 3e 70  xDestroy(pMod->p
22132 41 75 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Aux);.    }.    
22133 73 71 6c 69 74 65 46 72 65 65 28 70 4d 6f 64 29  sqliteFree(pMod)
22134 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 48  ;.  }.  sqlite3H
22135 61 73 68 43 6c 65 61 72 28 26 64 62 2d 3e 61 4d  ashClear(&db->aM
22136 6f 64 75 6c 65 29 3b 0a 23 65 6e 64 69 66 0a 0a  odule);.#endif..
22137 20 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65    sqlite3HashCle
22138 61 72 28 26 64 62 2d 3e 61 46 75 6e 63 29 3b 0a  ar(&db->aFunc);.
22139 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
2213a 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 30 29  b, SQLITE_OK, 0)
2213b 3b 20 2f 2a 20 44 65 61 6c 6c 6f 63 61 74 65 73  ; /* Deallocates
2213c 20 61 6e 79 20 63 61 63 68 65 64 20 65 72 72 6f   any cached erro
2213d 72 20 73 74 72 69 6e 67 73 2e 20 2a 2f 0a 20 20  r strings. */.  
2213e 69 66 28 20 64 62 2d 3e 70 45 72 72 20 29 7b 0a  if( db->pErr ){.
2213f 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65      sqlite3Value
22140 46 72 65 65 28 64 62 2d 3e 70 45 72 72 29 3b 0a  Free(db->pErr);.
22141 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 43 6c 6f    }.  sqlite3Clo
22142 73 65 45 78 74 65 6e 73 69 6f 6e 73 28 64 62 29  seExtensions(db)
22143 3b 0a 0a 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d  ;..  db->magic =
22144 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 45 52   SQLITE_MAGIC_ER
22145 52 4f 52 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 74  ROR;..  /* The t
22146 65 6d 70 2d 64 61 74 61 62 61 73 65 20 73 63 68  emp-database sch
22147 65 6d 61 20 69 73 20 61 6c 6c 6f 63 61 74 65 64  ema is allocated
22148 20 64 69 66 66 65 72 65 6e 74 6c 79 20 66 72 6f   differently fro
22149 6d 20 74 68 65 20 6f 74 68 65 72 20 73 63 68 65  m the other sche
2214a 6d 61 0a 20 20 2a 2a 20 6f 62 6a 65 63 74 73 20  ma.  ** objects 
2214b 28 75 73 69 6e 67 20 73 71 6c 69 74 65 4d 61 6c  (using sqliteMal
2214c 6c 6f 63 28 29 20 64 69 72 65 63 74 6c 79 2c 20  loc() directly, 
2214d 69 6e 73 74 65 61 64 20 6f 66 20 73 71 6c 69 74  instead of sqlit
2214e 65 33 42 74 72 65 65 53 63 68 65 6d 61 28 29 29  e3BtreeSchema())
2214f 2e 0a 20 20 2a 2a 20 53 6f 20 69 74 20 6e 65 65  ..  ** So it nee
22150 64 73 20 74 6f 20 62 65 20 66 72 65 65 64 20 68  ds to be freed h
22151 65 72 65 2e 20 54 6f 64 6f 3a 20 57 68 79 20 6e  ere. Todo: Why n
22152 6f 74 20 72 6f 6c 6c 20 74 68 65 20 74 65 6d 70  ot roll the temp
22153 20 73 63 68 65 6d 61 20 69 6e 74 6f 0a 20 20 2a   schema into.  *
22154 2a 20 74 68 65 20 73 61 6d 65 20 73 71 6c 69 74  * the same sqlit
22155 65 4d 61 6c 6c 6f 63 28 29 20 61 73 20 74 68 65  eMalloc() as the
22156 20 6f 6e 65 20 74 68 61 74 20 61 6c 6c 6f 63 61   one that alloca
22157 74 65 73 20 74 68 65 20 64 61 74 61 62 61 73 65  tes the database
22158 20 0a 20 20 2a 2a 20 73 74 72 75 63 74 75 72 65   .  ** structure
22159 3f 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 46  ?.  */.  sqliteF
2215a 72 65 65 28 64 62 2d 3e 61 44 62 5b 31 5d 2e 70  ree(db->aDb[1].p
2215b 53 63 68 65 6d 61 29 3b 0a 20 20 73 71 6c 69 74  Schema);.  sqlit
2215c 65 46 72 65 65 28 64 62 29 3b 0a 20 20 73 71 6c  eFree(db);.  sql
2215d 69 74 65 33 52 65 6c 65 61 73 65 54 68 72 65 61  ite3ReleaseThrea
2215e 64 44 61 74 61 28 29 3b 0a 20 20 72 65 74 75 72  dData();.  retur
2215f 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
22160 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 61  /*.** Rollback a
22161 6c 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ll database file
22162 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
22163 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
22164 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 73 71 6c  3RollbackAll(sql
22165 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74  ite3 *db){.  int
22166 20 69 3b 0a 20 20 69 6e 74 20 69 6e 54 72 61 6e   i;.  int inTran
22167 73 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30  s = 0;.  for(i=0
22168 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
22169 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61  ){.    if( db->a
2216a 44 62 5b 69 5d 2e 70 42 74 20 29 7b 0a 20 20 20  Db[i].pBt ){.   
2216b 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 74     if( sqlite3Bt
2216c 72 65 65 49 73 49 6e 54 72 61 6e 73 28 64 62 2d  reeIsInTrans(db-
2216d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 20 29 7b 0a  >aDb[i].pBt) ){.
2216e 20 20 20 20 20 20 20 20 69 6e 54 72 61 6e 73 20          inTrans 
2216f 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 1;.      }.   
22170 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 52     sqlite3BtreeR
22171 6f 6c 6c 62 61 63 6b 28 64 62 2d 3e 61 44 62 5b  ollback(db->aDb[
22172 69 5d 2e 70 42 74 29 3b 0a 20 20 20 20 20 20 64  i].pBt);.      d
22173 62 2d 3e 61 44 62 5b 69 5d 2e 69 6e 54 72 61 6e  b->aDb[i].inTran
22174 73 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  s = 0;.    }.  }
22175 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62 52 6f  .  sqlite3VtabRo
22176 6c 6c 62 61 63 6b 28 64 62 29 3b 0a 20 20 69 66  llback(db);.  if
22177 28 20 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49  ( db->flags&SQLI
22178 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73  TE_InternChanges
22179 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52   ){.    sqlite3R
2217a 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65  esetInternalSche
2217b 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 7d 0a 0a  ma(db, 0);.  }..
2217c 20 20 2f 2a 20 49 66 20 6f 6e 65 20 68 61 73 20    /* If one has 
2217d 62 65 65 6e 20 63 6f 6e 66 69 67 75 72 65 64 2c  been configured,
2217e 20 69 6e 76 6f 6b 65 20 74 68 65 20 72 6f 6c 6c   invoke the roll
2217f 62 61 63 6b 2d 68 6f 6f 6b 20 63 61 6c 6c 62 61  back-hook callba
22180 63 6b 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e  ck */.  if( db->
22181 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63  xRollbackCallbac
22182 6b 20 26 26 20 28 69 6e 54 72 61 6e 73 20 7c 7c  k && (inTrans ||
22183 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74   !db->autoCommit
22184 29 20 29 7b 0a 20 20 20 20 64 62 2d 3e 78 52 6f  ) ){.    db->xRo
22185 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b 28 64  llbackCallback(d
22186 62 2d 3e 70 52 6f 6c 6c 62 61 63 6b 41 72 67 29  b->pRollbackArg)
22187 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ;.  }.}../*.** R
22188 65 74 75 72 6e 20 61 20 73 74 61 74 69 63 20 73  eturn a static s
22189 74 72 69 6e 67 20 74 68 61 74 20 64 65 73 63 72  tring that descr
2218a 69 62 65 73 20 74 68 65 20 6b 69 6e 64 20 6f 66  ibes the kind of
2218b 20 65 72 72 6f 72 20 73 70 65 63 69 66 69 65 64   error specified
2218c 20 69 6e 20 74 68 65 0a 2a 2a 20 61 72 67 75 6d   in the.** argum
2218d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ent..*/.SQLITE_P
2218e 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61  RIVATE const cha
2218f 72 20 2a 73 71 6c 69 74 65 33 45 72 72 53 74 72  r *sqlite3ErrStr
22190 28 69 6e 74 20 72 63 29 7b 0a 20 20 63 6f 6e 73  (int rc){.  cons
22191 74 20 63 68 61 72 20 2a 7a 3b 0a 20 20 73 77 69  t char *z;.  swi
22192 74 63 68 28 20 72 63 20 26 20 30 78 66 66 20 29  tch( rc & 0xff )
22193 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  {.    case SQLIT
22194 45 5f 52 4f 57 3a 0a 20 20 20 20 63 61 73 65 20  E_ROW:.    case 
22195 53 51 4c 49 54 45 5f 44 4f 4e 45 3a 0a 20 20 20  SQLITE_DONE:.   
22196 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4f 4b 3a   case SQLITE_OK:
22197 20 20 20 20 20 20 20 20 20 7a 20 3d 20 22 6e 6f           z = "no
22198 74 20 61 6e 20 65 72 72 6f 72 22 3b 20 20 20 20  t an error";    
22199 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2219a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2219b 20 63 61 73 65 20 53 51 4c 49 54 45 5f 45 52 52   case SQLITE_ERR
2219c 4f 52 3a 20 20 20 20 20 20 7a 20 3d 20 22 53 51  OR:      z = "SQ
2219d 4c 20 6c 6f 67 69 63 20 65 72 72 6f 72 20 6f 72  L logic error or
2219e 20 6d 69 73 73 69 6e 67 20 64 61 74 61 62 61 73   missing databas
2219f 65 22 3b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  e";   break;.   
221a0 20 63 61 73 65 20 53 51 4c 49 54 45 5f 50 45 52   case SQLITE_PER
221a1 4d 3a 20 20 20 20 20 20 20 7a 20 3d 20 22 61 63  M:       z = "ac
221a2 63 65 73 73 20 70 65 72 6d 69 73 73 69 6f 6e 20  cess permission 
221a3 64 65 6e 69 65 64 22 3b 20 20 20 20 20 20 20 20  denied";        
221a4 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
221a5 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 42 4f   case SQLITE_ABO
221a6 52 54 3a 20 20 20 20 20 20 7a 20 3d 20 22 63 61  RT:      z = "ca
221a7 6c 6c 62 61 63 6b 20 72 65 71 75 65 73 74 65 64  llback requested
221a8 20 71 75 65 72 79 20 61 62 6f 72 74 22 3b 20 20   query abort";  
221a9 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
221aa 20 63 61 73 65 20 53 51 4c 49 54 45 5f 42 55 53   case SQLITE_BUS
221ab 59 3a 20 20 20 20 20 20 20 7a 20 3d 20 22 64 61  Y:       z = "da
221ac 74 61 62 61 73 65 20 69 73 20 6c 6f 63 6b 65 64  tabase is locked
221ad 22 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ";              
221ae 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
221af 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4c 4f 43   case SQLITE_LOC
221b0 4b 45 44 3a 20 20 20 20 20 7a 20 3d 20 22 64 61  KED:     z = "da
221b1 74 61 62 61 73 65 20 74 61 62 6c 65 20 69 73 20  tabase table is 
221b2 6c 6f 63 6b 65 64 22 3b 20 20 20 20 20 20 20 20  locked";        
221b3 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
221b4 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4e 4f 4d   case SQLITE_NOM
221b5 45 4d 3a 20 20 20 20 20 20 7a 20 3d 20 22 6f 75  EM:      z = "ou
221b6 74 20 6f 66 20 6d 65 6d 6f 72 79 22 3b 20 20 20  t of memory";   
221b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
221b8 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
221b9 20 63 61 73 65 20 53 51 4c 49 54 45 5f 52 45 41   case SQLITE_REA
221ba 44 4f 4e 4c 59 3a 20 20 20 7a 20 3d 20 22 61 74  DONLY:   z = "at
221bb 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 20 61  tempt to write a
221bc 20 72 65 61 64 6f 6e 6c 79 20 64 61 74 61 62 61   readonly databa
221bd 73 65 22 3b 20 20 62 72 65 61 6b 3b 0a 20 20 20  se";  break;.   
221be 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 54   case SQLITE_INT
221bf 45 52 52 55 50 54 3a 20 20 7a 20 3d 20 22 69 6e  ERRUPT:  z = "in
221c0 74 65 72 72 75 70 74 65 64 22 3b 20 20 20 20 20  terrupted";     
221c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
221c2 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
221c3 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4f 45   case SQLITE_IOE
221c4 52 52 3a 20 20 20 20 20 20 7a 20 3d 20 22 64 69  RR:      z = "di
221c5 73 6b 20 49 2f 4f 20 65 72 72 6f 72 22 3b 20 20  sk I/O error";  
221c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
221c7 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
221c8 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 52   case SQLITE_COR
221c9 52 55 50 54 3a 20 20 20 20 7a 20 3d 20 22 64 61  RUPT:    z = "da
221ca 74 61 62 61 73 65 20 64 69 73 6b 20 69 6d 61 67  tabase disk imag
221cb 65 20 69 73 20 6d 61 6c 66 6f 72 6d 65 64 22 3b  e is malformed";
221cc 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
221cd 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 55 4c   case SQLITE_FUL
221ce 4c 3a 20 20 20 20 20 20 20 7a 20 3d 20 22 64 61  L:       z = "da
221cf 74 61 62 61 73 65 20 6f 72 20 64 69 73 6b 20 69  tabase or disk i
221d0 73 20 66 75 6c 6c 22 3b 20 20 20 20 20 20 20 20  s full";        
221d1 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
221d2 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 41 4e   case SQLITE_CAN
221d3 54 4f 50 45 4e 3a 20 20 20 7a 20 3d 20 22 75 6e  TOPEN:   z = "un
221d4 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 64 61 74  able to open dat
221d5 61 62 61 73 65 20 66 69 6c 65 22 3b 20 20 20 20  abase file";    
221d6 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
221d7 20 63 61 73 65 20 53 51 4c 49 54 45 5f 45 4d 50   case SQLITE_EMP
221d8 54 59 3a 20 20 20 20 20 20 7a 20 3d 20 22 74 61  TY:      z = "ta
221d9 62 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20  ble contains no 
221da 64 61 74 61 22 3b 20 20 20 20 20 20 20 20 20 20  data";          
221db 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
221dc 20 63 61 73 65 20 53 51 4c 49 54 45 5f 53 43 48   case SQLITE_SCH
221dd 45 4d 41 3a 20 20 20 20 20 7a 20 3d 20 22 64 61  EMA:     z = "da
221de 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 68 61  tabase schema ha
221df 73 20 63 68 61 6e 67 65 64 22 3b 20 20 20 20 20  s changed";     
221e0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
221e1 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 4f 4f   case SQLITE_TOO
221e2 42 49 47 3a 20 20 20 20 20 7a 20 3d 20 22 53 74  BIG:     z = "St
221e3 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 65 78 63  ring or BLOB exc
221e4 65 65 64 65 64 20 73 69 7a 65 20 6c 69 6d 69 74  eeded size limit
221e5 22 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  ";    break;.   
221e6 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e   case SQLITE_CON
221e7 53 54 52 41 49 4e 54 3a 20 7a 20 3d 20 22 63 6f  STRAINT: z = "co
221e8 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 22  nstraint failed"
221e9 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
221ea 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
221eb 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 49 53   case SQLITE_MIS
221ec 4d 41 54 43 48 3a 20 20 20 7a 20 3d 20 22 64 61  MATCH:   z = "da
221ed 74 61 74 79 70 65 20 6d 69 73 6d 61 74 63 68 22  tatype mismatch"
221ee 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
221ef 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
221f0 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 49 53   case SQLITE_MIS
221f1 55 53 45 3a 20 20 20 20 20 7a 20 3d 20 22 6c 69  USE:     z = "li
221f2 62 72 61 72 79 20 72 6f 75 74 69 6e 65 20 63 61  brary routine ca
221f3 6c 6c 65 64 20 6f 75 74 20 6f 66 20 73 65 71 75  lled out of sequ
221f4 65 6e 63 65 22 3b 62 72 65 61 6b 3b 0a 20 20 20  ence";break;.   
221f5 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4e 4f 4c   case SQLITE_NOL
221f6 46 53 3a 20 20 20 20 20 20 7a 20 3d 20 22 6b 65  FS:      z = "ke
221f7 72 6e 65 6c 20 6c 61 63 6b 73 20 6c 61 72 67 65  rnel lacks large
221f8 20 66 69 6c 65 20 73 75 70 70 6f 72 74 22 3b 20   file support"; 
221f9 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
221fa 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 55 54   case SQLITE_AUT
221fb 48 3a 20 20 20 20 20 20 20 7a 20 3d 20 22 61 75  H:       z = "au
221fc 74 68 6f 72 69 7a 61 74 69 6f 6e 20 64 65 6e 69  thorization deni
221fd 65 64 22 3b 20 20 20 20 20 20 20 20 20 20 20 20  ed";            
221fe 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
221ff 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 4f 52   case SQLITE_FOR
22200 4d 41 54 3a 20 20 20 20 20 7a 20 3d 20 22 61 75  MAT:     z = "au
22201 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65  xiliary database
22202 20 66 6f 72 6d 61 74 20 65 72 72 6f 72 22 3b 20   format error"; 
22203 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
22204 20 63 61 73 65 20 53 51 4c 49 54 45 5f 52 41 4e   case SQLITE_RAN
22205 47 45 3a 20 20 20 20 20 20 7a 20 3d 20 22 62 69  GE:      z = "bi
22206 6e 64 20 6f 72 20 63 6f 6c 75 6d 6e 20 69 6e 64  nd or column ind
22207 65 78 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 22  ex out of range"
22208 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  ;     break;.   
22209 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4e 4f 54   case SQLITE_NOT
2220a 41 44 42 3a 20 20 20 20 20 7a 20 3d 20 22 66 69  ADB:     z = "fi
2220b 6c 65 20 69 73 20 65 6e 63 72 79 70 74 65 64 20  le is encrypted 
2220c 6f 72 20 69 73 20 6e 6f 74 20 61 20 64 61 74 61  or is not a data
2220d 62 61 73 65 22 3b 62 72 65 61 6b 3b 0a 20 20 20  base";break;.   
2220e 20 64 65 66 61 75 6c 74 3a 20 20 20 20 20 20 20   default:       
2220f 20 20 20 20 20 20 20 20 20 7a 20 3d 20 22 75 6e           z = "un
22210 6b 6e 6f 77 6e 20 65 72 72 6f 72 22 3b 20 20 20  known error";   
22211 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22212 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d        break;.  }
22213 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a  .  return z;.}..
22214 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
22215 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20  ne implements a 
22216 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 74 68  busy callback th
22217 61 74 20 73 6c 65 65 70 73 20 61 6e 64 20 74 72  at sleeps and tr
22218 69 65 73 0a 2a 2a 20 61 67 61 69 6e 20 75 6e 74  ies.** again unt
22219 69 6c 20 61 20 74 69 6d 65 6f 75 74 20 76 61 6c  il a timeout val
2221a 75 65 20 69 73 20 72 65 61 63 68 65 64 2e 20 20  ue is reached.  
2221b 54 68 65 20 74 69 6d 65 6f 75 74 20 76 61 6c 75  The timeout valu
2221c 65 20 69 73 0a 2a 2a 20 61 6e 20 69 6e 74 65 67  e is.** an integ
2221d 65 72 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6c  er number of mil
2221e 6c 69 73 65 63 6f 6e 64 73 20 70 61 73 73 65 64  liseconds passed
2221f 20 69 6e 20 61 73 20 74 68 65 20 66 69 72 73 74   in as the first
22220 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f  .** argument..*/
22221 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69  .static int sqli
22222 74 65 44 65 66 61 75 6c 74 42 75 73 79 43 61 6c  teDefaultBusyCal
22223 6c 62 61 63 6b 28 0a 20 76 6f 69 64 20 2a 70 74  lback(. void *pt
22224 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
22225 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e   /* Database con
22226 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 69 6e 74 20  nection */. int 
22227 63 6f 75 6e 74 20 20 20 20 20 20 20 20 20 20 20  count           
22228 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
22229 66 20 74 69 6d 65 73 20 74 61 62 6c 65 20 68 61  f times table ha
2222a 73 20 62 65 65 6e 20 62 75 73 79 20 2a 2f 0a 29  s been busy */.)
2222b 7b 0a 23 69 66 20 4f 53 5f 57 49 4e 20 7c 7c 20  {.#if OS_WIN || 
2222c 28 64 65 66 69 6e 65 64 28 48 41 56 45 5f 55 53  (defined(HAVE_US
2222d 4c 45 45 50 29 20 26 26 20 48 41 56 45 5f 55 53  LEEP) && HAVE_US
2222e 4c 45 45 50 29 0a 20 20 73 74 61 74 69 63 20 63  LEEP).  static c
2222f 6f 6e 73 74 20 75 38 20 64 65 6c 61 79 73 5b 5d  onst u8 delays[]
22230 20 3d 0a 20 20 20 20 20 7b 20 31 2c 20 32 2c 20   =.     { 1, 2, 
22231 35 2c 20 31 30 2c 20 31 35 2c 20 32 30 2c 20 32  5, 10, 15, 20, 2
22232 35 2c 20 32 35 2c 20 20 32 35 2c 20 20 35 30 2c  5, 25,  25,  50,
22233 20 20 35 30 2c 20 31 30 30 20 7d 3b 0a 20 20 73    50, 100 };.  s
22234 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20 74  tatic const u8 t
22235 6f 74 61 6c 73 5b 5d 20 3d 0a 20 20 20 20 20 7b  otals[] =.     {
22236 20 30 2c 20 31 2c 20 33 2c 20 20 38 2c 20 31 38   0, 1, 3,  8, 18
22237 2c 20 33 33 2c 20 35 33 2c 20 37 38 2c 20 31 30  , 33, 53, 78, 10
22238 33 2c 20 31 32 38 2c 20 31 37 38 2c 20 32 32 38  3, 128, 178, 228
22239 20 7d 3b 0a 23 20 64 65 66 69 6e 65 20 4e 44 45   };.# define NDE
2223a 4c 41 59 20 28 73 69 7a 65 6f 66 28 64 65 6c 61  LAY (sizeof(dela
2223b 79 73 29 2f 73 69 7a 65 6f 66 28 64 65 6c 61 79  ys)/sizeof(delay
2223c 73 5b 30 5d 29 29 0a 20 20 69 6e 74 20 74 69 6d  s[0])).  int tim
2223d 65 6f 75 74 20 3d 20 28 28 73 71 6c 69 74 65 33  eout = ((sqlite3
2223e 20 2a 29 70 74 72 29 2d 3e 62 75 73 79 54 69 6d   *)ptr)->busyTim
2223f 65 6f 75 74 3b 0a 20 20 69 6e 74 20 64 65 6c 61  eout;.  int dela
22240 79 2c 20 70 72 69 6f 72 3b 0a 0a 20 20 61 73 73  y, prior;..  ass
22241 65 72 74 28 20 63 6f 75 6e 74 3e 3d 30 20 29 3b  ert( count>=0 );
22242 0a 20 20 69 66 28 20 63 6f 75 6e 74 20 3c 20 4e  .  if( count < N
22243 44 45 4c 41 59 20 29 7b 0a 20 20 20 20 64 65 6c  DELAY ){.    del
22244 61 79 20 3d 20 64 65 6c 61 79 73 5b 63 6f 75 6e  ay = delays[coun
22245 74 5d 3b 0a 20 20 20 20 70 72 69 6f 72 20 3d 20  t];.    prior = 
22246 74 6f 74 61 6c 73 5b 63 6f 75 6e 74 5d 3b 0a 20  totals[count];. 
22247 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 65 6c 61   }else{.    dela
22248 79 20 3d 20 64 65 6c 61 79 73 5b 4e 44 45 4c 41  y = delays[NDELA
22249 59 2d 31 5d 3b 0a 20 20 20 20 70 72 69 6f 72 20  Y-1];.    prior 
2224a 3d 20 74 6f 74 61 6c 73 5b 4e 44 45 4c 41 59 2d  = totals[NDELAY-
2224b 31 5d 20 2b 20 64 65 6c 61 79 2a 28 63 6f 75 6e  1] + delay*(coun
2224c 74 2d 28 4e 44 45 4c 41 59 2d 31 29 29 3b 0a 20  t-(NDELAY-1));. 
2224d 20 7d 0a 20 20 69 66 28 20 70 72 69 6f 72 20 2b   }.  if( prior +
2224e 20 64 65 6c 61 79 20 3e 20 74 69 6d 65 6f 75 74   delay > timeout
2224f 20 29 7b 0a 20 20 20 20 64 65 6c 61 79 20 3d 20   ){.    delay = 
22250 74 69 6d 65 6f 75 74 20 2d 20 70 72 69 6f 72 3b  timeout - prior;
22251 0a 20 20 20 20 69 66 28 20 64 65 6c 61 79 3c 3d  .    if( delay<=
22252 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
22253 7d 0a 20 20 73 71 6c 69 74 65 33 4f 73 53 6c 65  }.  sqlite3OsSle
22254 65 70 28 64 65 6c 61 79 29 3b 0a 20 20 72 65 74  ep(delay);.  ret
22255 75 72 6e 20 31 3b 0a 23 65 6c 73 65 0a 20 20 69  urn 1;.#else.  i
22256 6e 74 20 74 69 6d 65 6f 75 74 20 3d 20 28 28 73  nt timeout = ((s
22257 71 6c 69 74 65 33 20 2a 29 70 74 72 29 2d 3e 62  qlite3 *)ptr)->b
22258 75 73 79 54 69 6d 65 6f 75 74 3b 0a 20 20 69 66  usyTimeout;.  if
22259 28 20 28 63 6f 75 6e 74 2b 31 29 2a 31 30 30 30  ( (count+1)*1000
2225a 20 3e 20 74 69 6d 65 6f 75 74 20 29 7b 0a 20 20   > timeout ){.  
2225b 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
2225c 20 20 73 71 6c 69 74 65 33 4f 73 53 6c 65 65 70    sqlite3OsSleep
2225d 28 31 30 30 30 29 3b 0a 20 20 72 65 74 75 72 6e  (1000);.  return
2225e 20 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a   1;.#endif.}../*
2225f 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 67  .** Invoke the g
22260 69 76 65 6e 20 62 75 73 79 20 68 61 6e 64 6c 65  iven busy handle
22261 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  r..**.** This ro
22262 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
22263 77 68 65 6e 20 61 6e 20 6f 70 65 72 61 74 69 6f  when an operatio
22264 6e 20 66 61 69 6c 65 64 20 77 69 74 68 20 61 20  n failed with a 
22265 6c 6f 63 6b 2e 0a 2a 2a 20 49 66 20 74 68 69 73  lock..** If this
22266 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
22267 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 20 6c   non-zero, the l
22268 6f 63 6b 20 69 73 20 72 65 74 72 69 65 64 2e 20  ock is retried. 
22269 20 49 66 20 69 74 0a 2a 2a 20 72 65 74 75 72 6e   If it.** return
2226a 73 20 30 2c 20 74 68 65 20 6f 70 65 72 61 74 69  s 0, the operati
2226b 6f 6e 20 61 62 6f 72 74 73 20 77 69 74 68 20 61  on aborts with a
2226c 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 20 65 72  n SQLITE_BUSY er
2226d 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ror..*/.SQLITE_P
2226e 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2226f 65 33 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64  e3InvokeBusyHand
22270 6c 65 72 28 42 75 73 79 48 61 6e 64 6c 65 72 20  ler(BusyHandler 
22271 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  *p){.  int rc;. 
22272 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e   if( p==0 || p->
22273 78 46 75 6e 63 3d 3d 30 20 7c 7c 20 70 2d 3e 6e  xFunc==0 || p->n
22274 42 75 73 79 3c 30 20 29 20 72 65 74 75 72 6e 20  Busy<0 ) return 
22275 30 3b 0a 20 20 72 63 20 3d 20 70 2d 3e 78 46 75  0;.  rc = p->xFu
22276 6e 63 28 70 2d 3e 70 41 72 67 2c 20 70 2d 3e 6e  nc(p->pArg, p->n
22277 42 75 73 79 29 3b 0a 20 20 69 66 28 20 72 63 3d  Busy);.  if( rc=
22278 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 42 75  =0 ){.    p->nBu
22279 73 79 20 3d 20 2d 31 3b 0a 20 20 7d 65 6c 73 65  sy = -1;.  }else
2227a 7b 0a 20 20 20 20 70 2d 3e 6e 42 75 73 79 2b 2b  {.    p->nBusy++
2227b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
2227c 63 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  c; .}../*.** Thi
2227d 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74  s routine sets t
2227e 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b  he busy callback
2227f 20 66 6f 72 20 61 6e 20 53 71 6c 69 74 65 20 64   for an Sqlite d
22280 61 74 61 62 61 73 65 20 74 6f 20 74 68 65 0a 2a  atabase to the.*
22281 2a 20 67 69 76 65 6e 20 63 61 6c 6c 62 61 63 6b  * given callback
22282 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74   function with t
22283 68 65 20 67 69 76 65 6e 20 61 72 67 75 6d 65 6e  he given argumen
22284 74 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65  t..*/.int sqlite
22285 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 0a  3_busy_handler(.
22286 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20    sqlite3 *db,. 
22287 20 69 6e 74 20 28 2a 78 42 75 73 79 29 28 76 6f   int (*xBusy)(vo
22288 69 64 2a 2c 69 6e 74 29 2c 0a 20 20 76 6f 69 64  id*,int),.  void
22289 20 2a 70 41 72 67 0a 29 7b 0a 20 20 69 66 28 20   *pArg.){.  if( 
2228a 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65  sqlite3SafetyChe
2228b 63 6b 28 64 62 29 20 29 7b 0a 20 20 20 20 72 65  ck(db) ){.    re
2228c 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55  turn SQLITE_MISU
2228d 53 45 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 62 75  SE;.  }.  db->bu
2228e 73 79 48 61 6e 64 6c 65 72 2e 78 46 75 6e 63 20  syHandler.xFunc 
2228f 3d 20 78 42 75 73 79 3b 0a 20 20 64 62 2d 3e 62  = xBusy;.  db->b
22290 75 73 79 48 61 6e 64 6c 65 72 2e 70 41 72 67 20  usyHandler.pArg 
22291 3d 20 70 41 72 67 3b 0a 20 20 64 62 2d 3e 62 75  = pArg;.  db->bu
22292 73 79 48 61 6e 64 6c 65 72 2e 6e 42 75 73 79 20  syHandler.nBusy 
22293 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  = 0;.  return SQ
22294 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e  LITE_OK;.}..#ifn
22295 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
22296 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43  PROGRESS_CALLBAC
22297 4b 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  K./*.** This rou
22298 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 70 72  tine sets the pr
22299 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20  ogress callback 
2229a 66 6f 72 20 61 6e 20 53 71 6c 69 74 65 20 64 61  for an Sqlite da
2229b 74 61 62 61 73 65 20 74 6f 20 74 68 65 0a 2a 2a  tabase to the.**
2229c 20 67 69 76 65 6e 20 63 61 6c 6c 62 61 63 6b 20   given callback 
2229d 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68  function with th
2229e 65 20 67 69 76 65 6e 20 61 72 67 75 6d 65 6e 74  e given argument
2229f 2e 20 54 68 65 20 70 72 6f 67 72 65 73 73 20 63  . The progress c
222a0 61 6c 6c 62 61 63 6b 20 77 69 6c 6c 0a 2a 2a 20  allback will.** 
222a1 62 65 20 69 6e 76 6f 6b 65 64 20 65 76 65 72 79  be invoked every
222a2 20 6e 4f 70 73 20 6f 70 63 6f 64 65 73 2e 0a 2a   nOps opcodes..*
222a3 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 70  /.void sqlite3_p
222a4 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65 72 28  rogress_handler(
222a5 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
222a6 0a 20 20 69 6e 74 20 6e 4f 70 73 2c 0a 20 20 69  .  int nOps,.  i
222a7 6e 74 20 28 2a 78 50 72 6f 67 72 65 73 73 29 28  nt (*xProgress)(
222a8 76 6f 69 64 2a 29 2c 20 0a 20 20 76 6f 69 64 20  void*), .  void 
222a9 2a 70 41 72 67 0a 29 7b 0a 20 20 69 66 28 20 21  *pArg.){.  if( !
222aa 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65  sqlite3SafetyChe
222ab 63 6b 28 64 62 29 20 29 7b 0a 20 20 20 20 69 66  ck(db) ){.    if
222ac 28 20 6e 4f 70 73 3e 30 20 29 7b 0a 20 20 20 20  ( nOps>0 ){.    
222ad 20 20 64 62 2d 3e 78 50 72 6f 67 72 65 73 73 20    db->xProgress 
222ae 3d 20 78 50 72 6f 67 72 65 73 73 3b 0a 20 20 20  = xProgress;.   
222af 20 20 20 64 62 2d 3e 6e 50 72 6f 67 72 65 73 73     db->nProgress
222b0 4f 70 73 20 3d 20 6e 4f 70 73 3b 0a 20 20 20 20  Ops = nOps;.    
222b1 20 20 64 62 2d 3e 70 50 72 6f 67 72 65 73 73 41    db->pProgressA
222b2 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 20 20 7d  rg = pArg;.    }
222b3 65 6c 73 65 7b 0a 20 20 20 20 20 20 64 62 2d 3e  else{.      db->
222b4 78 50 72 6f 67 72 65 73 73 20 3d 20 30 3b 0a 20  xProgress = 0;. 
222b5 20 20 20 20 20 64 62 2d 3e 6e 50 72 6f 67 72 65       db->nProgre
222b6 73 73 4f 70 73 20 3d 20 30 3b 0a 20 20 20 20 20  ssOps = 0;.     
222b7 20 64 62 2d 3e 70 50 72 6f 67 72 65 73 73 41 72   db->pProgressAr
222b8 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  g = 0;.    }.  }
222b9 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a  .}.#endif.../*.*
222ba 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
222bb 6e 73 74 61 6c 6c 73 20 61 20 64 65 66 61 75 6c  nstalls a defaul
222bc 74 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 74  t busy handler t
222bd 68 61 74 20 77 61 69 74 73 20 66 6f 72 20 74 68  hat waits for th
222be 65 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 6e  e.** specified n
222bf 75 6d 62 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65  umber of millise
222c0 63 6f 6e 64 73 20 62 65 66 6f 72 65 20 72 65 74  conds before ret
222c1 75 72 6e 69 6e 67 20 30 2e 0a 2a 2f 0a 69 6e 74  urning 0..*/.int
222c2 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69   sqlite3_busy_ti
222c3 6d 65 6f 75 74 28 73 71 6c 69 74 65 33 20 2a 64  meout(sqlite3 *d
222c4 62 2c 20 69 6e 74 20 6d 73 29 7b 0a 20 20 69 66  b, int ms){.  if
222c5 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43  ( sqlite3SafetyC
222c6 68 65 63 6b 28 64 62 29 20 29 7b 0a 20 20 20 20  heck(db) ){.    
222c7 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
222c8 53 55 53 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20  SUSE;.  }.  if( 
222c9 6d 73 3e 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e  ms>0 ){.    db->
222ca 62 75 73 79 54 69 6d 65 6f 75 74 20 3d 20 6d 73  busyTimeout = ms
222cb 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62 75  ;.    sqlite3_bu
222cc 73 79 5f 68 61 6e 64 6c 65 72 28 64 62 2c 20 73  sy_handler(db, s
222cd 71 6c 69 74 65 44 65 66 61 75 6c 74 42 75 73 79  qliteDefaultBusy
222ce 43 61 6c 6c 62 61 63 6b 2c 20 28 76 6f 69 64 2a  Callback, (void*
222cf 29 64 62 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  )db);.  }else{. 
222d0 20 20 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f     sqlite3_busy_
222d1 68 61 6e 64 6c 65 72 28 64 62 2c 20 30 2c 20 30  handler(db, 0, 0
222d2 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
222d3 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
222d4 0a 2a 2a 20 43 61 75 73 65 20 61 6e 79 20 70 65  .** Cause any pe
222d5 6e 64 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20  nding operation 
222d6 74 6f 20 73 74 6f 70 20 61 74 20 69 74 73 20 65  to stop at its e
222d7 61 72 6c 69 65 73 74 20 6f 70 70 6f 72 74 75 6e  arliest opportun
222d8 69 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ity..*/.SQLITE_A
222d9 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
222da 69 6e 74 65 72 72 75 70 74 28 73 71 6c 69 74 65  interrupt(sqlite
222db 33 20 2a 64 62 29 7b 0a 20 20 69 66 28 20 64 62  3 *db){.  if( db
222dc 20 26 26 20 28 64 62 2d 3e 6d 61 67 69 63 3d 3d   && (db->magic==
222dd 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45  SQLITE_MAGIC_OPE
222de 4e 20 7c 7c 20 64 62 2d 3e 6d 61 67 69 63 3d 3d  N || db->magic==
222df 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53  SQLITE_MAGIC_BUS
222e0 59 29 20 29 7b 0a 20 20 20 20 64 62 2d 3e 75 31  Y) ){.    db->u1
222e1 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 3d  .isInterrupted =
222e2 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a   1;.  }.}../*.**
222e3 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   Memory allocati
222e4 6f 6e 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74  on routines that
222e5 20 75 73 65 20 53 51 4c 69 74 65 73 20 69 6e 74   use SQLites int
222e6 65 72 6e 61 6c 20 6d 65 6d 6f 72 79 0a 2a 2a 20  ernal memory.** 
222e7 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72  memory allocator
222e8 2e 20 20 44 65 70 65 6e 64 69 6e 67 20 6f 6e 20  .  Depending on 
222e9 68 6f 77 20 53 51 4c 69 74 65 20 69 73 20 63 6f  how SQLite is co
222ea 6d 70 69 6c 65 64 2c 20 74 68 65 0a 2a 2a 20 69  mpiled, the.** i
222eb 6e 74 65 72 6e 61 6c 20 6d 65 6d 6f 72 79 20 61  nternal memory a
222ec 6c 6c 6f 63 61 74 6f 72 20 6d 69 67 68 74 20 62  llocator might b
222ed 65 20 6a 75 73 74 20 61 6e 20 61 6c 69 61 73 20  e just an alias 
222ee 66 6f 72 20 74 68 65 0a 2a 2a 20 73 79 73 74 65  for the.** syste
222ef 6d 20 64 65 66 61 75 6c 74 20 6d 61 6c 6c 6f 63  m default malloc
222f0 2f 72 65 61 6c 6c 6f 63 2f 66 72 65 65 2e 20 20  /realloc/free.  
222f1 4f 72 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20  Or the built-in 
222f2 61 6c 6c 6f 63 61 74 6f 72 0a 2a 2a 20 6d 69 67  allocator.** mig
222f3 68 74 20 64 6f 20 65 78 74 72 61 20 73 74 75 66  ht do extra stuf
222f4 66 20 6c 69 6b 65 20 70 75 74 20 73 65 6e 74 69  f like put senti
222f5 6e 61 6c 73 20 61 72 6f 75 6e 64 20 62 75 66 66  nals around buff
222f6 65 72 73 20 74 6f 20 0a 2a 2a 20 63 68 65 63 6b  ers to .** check
222f7 20 66 6f 72 20 6f 76 65 72 72 75 6e 73 20 6f 72   for overruns or
222f8 20 6c 6f 6f 6b 20 66 6f 72 20 6d 65 6d 6f 72 79   look for memory
222f9 20 6c 65 61 6b 73 2e 0a 2a 2a 0a 2a 2a 20 55 73   leaks..**.** Us
222fa 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29  e sqlite3_free()
222fb 20 74 6f 20 66 72 65 65 20 6d 65 6d 6f 72 79 20   to free memory 
222fc 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69  returned by sqli
222fd 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 2e 0a 2a  te3_mprintf()..*
222fe 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  /.SQLITE_API voi
222ff 64 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 76  d sqlite3_free(v
22300 6f 69 64 20 2a 70 29 7b 20 69 66 28 20 70 20 29  oid *p){ if( p )
22301 20 73 71 6c 69 74 65 33 4f 73 46 72 65 65 28 70   sqlite3OsFree(p
22302 29 3b 20 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  ); }.SQLITE_API 
22303 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 6d 61  void *sqlite3_ma
22304 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b  lloc(int nByte){
22305 20 72 65 74 75 72 6e 20 6e 42 79 74 65 3e 30 20   return nByte>0 
22306 3f 20 73 71 6c 69 74 65 33 4f 73 4d 61 6c 6c 6f  ? sqlite3OsMallo
22307 63 28 6e 42 79 74 65 29 20 3a 20 30 3b 20 7d 0a  c(nByte) : 0; }.
22308 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
22309 2a 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63  *sqlite3_realloc
2230a 28 76 6f 69 64 20 2a 70 4f 6c 64 2c 20 69 6e 74  (void *pOld, int
2230b 20 6e 42 79 74 65 29 7b 20 0a 20 20 69 66 28 20   nByte){ .  if( 
2230c 70 4f 6c 64 20 29 7b 0a 20 20 20 20 69 66 28 20  pOld ){.    if( 
2230d 6e 42 79 74 65 3e 30 20 29 7b 0a 20 20 20 20 20  nByte>0 ){.     
2230e 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4f   return sqlite3O
2230f 73 52 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e  sRealloc(pOld, n
22310 42 79 74 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Byte);.    }else
22311 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f  {.      sqlite3O
22312 73 46 72 65 65 28 70 4f 6c 64 29 3b 0a 20 20 20  sFree(pOld);.   
22313 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20     return 0;.   
22314 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
22315 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 6d  return sqlite3_m
22316 61 6c 6c 6f 63 28 6e 42 79 74 65 29 3b 0a 20 20  alloc(nByte);.  
22317 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  }.}../*.** This 
22318 66 75 6e 63 74 69 6f 6e 20 69 73 20 65 78 61 63  function is exac
22319 74 6c 79 20 74 68 65 20 73 61 6d 65 20 61 73 20  tly the same as 
2231a 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
2231b 75 6e 63 74 69 6f 6e 28 29 2c 20 65 78 63 65 70  unction(), excep
2231c 74 0a 2a 2a 20 74 68 61 74 20 69 74 20 69 73 20  t.** that it is 
2231d 64 65 73 69 67 6e 65 64 20 74 6f 20 62 65 20 63  designed to be c
2231e 61 6c 6c 65 64 20 62 79 20 69 6e 74 65 72 6e 61  alled by interna
2231f 6c 20 63 6f 64 65 2e 20 54 68 65 20 64 69 66 66  l code. The diff
22320 65 72 65 6e 63 65 20 69 73 0a 2a 2a 20 74 68 61  erence is.** tha
22321 74 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20  t if a malloc() 
22322 66 61 69 6c 73 20 69 6e 20 73 71 6c 69 74 65 33  fails in sqlite3
22323 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
22324 28 29 2c 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  (), an error cod
22325 65 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64  e.** is returned
22326 20 61 6e 64 20 74 68 65 20 6d 61 6c 6c 6f 63 46   and the mallocF
22327 61 69 6c 65 64 20 66 6c 61 67 20 63 6c 65 61 72  ailed flag clear
22328 65 64 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ed. .*/.SQLITE_P
22329 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2232a 65 33 43 72 65 61 74 65 46 75 6e 63 28 0a 20 20  e3CreateFunc(.  
2232b 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63  sqlite3 *db,.  c
2232c 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 75 6e 63  onst char *zFunc
2232d 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20  tionName,.  int 
2232e 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65 6e 63 2c  nArg,.  int enc,
2232f 0a 20 20 76 6f 69 64 20 2a 70 55 73 65 72 44 61  .  void *pUserDa
22330 74 61 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 75  ta,.  void (*xFu
22331 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  nc)(sqlite3_cont
22332 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
22333 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a 20 20 76 6f  _value **),.  vo
22334 69 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69  id (*xStep)(sqli
22335 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
22336 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a  ,sqlite3_value *
22337 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 69  *),.  void (*xFi
22338 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  nal)(sqlite3_con
22339 74 65 78 74 2a 29 0a 29 7b 0a 20 20 46 75 6e 63  text*).){.  Func
2233a 44 65 66 20 2a 70 3b 0a 20 20 69 6e 74 20 6e 4e  Def *p;.  int nN
2233b 61 6d 65 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69  ame;..  if( sqli
2233c 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 28 64  te3SafetyCheck(d
2233d 62 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  b) ){.    return
2233e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a   SQLITE_MISUSE;.
2233f 20 20 7d 0a 20 20 69 66 28 20 7a 46 75 6e 63 74    }.  if( zFunct
22340 69 6f 6e 4e 61 6d 65 3d 3d 30 20 7c 7c 0a 20 20  ionName==0 ||.  
22341 20 20 20 20 28 78 46 75 6e 63 20 26 26 20 28 78      (xFunc && (x
22342 46 69 6e 61 6c 20 7c 7c 20 78 53 74 65 70 29 29  Final || xStep))
22343 20 7c 7c 20 0a 20 20 20 20 20 20 28 21 78 46 75   || .      (!xFu
22344 6e 63 20 26 26 20 28 78 46 69 6e 61 6c 20 26 26  nc && (xFinal &&
22345 20 21 78 53 74 65 70 29 29 20 7c 7c 0a 20 20 20   !xStep)) ||.   
22346 20 20 20 28 21 78 46 75 6e 63 20 26 26 20 28 21     (!xFunc && (!
22347 78 46 69 6e 61 6c 20 26 26 20 78 53 74 65 70 29  xFinal && xStep)
22348 29 20 7c 7c 0a 20 20 20 20 20 20 28 6e 41 72 67  ) ||.      (nArg
22349 3c 2d 31 20 7c 7c 20 6e 41 72 67 3e 31 32 37 29  <-1 || nArg>127)
2234a 20 7c 7c 0a 20 20 20 20 20 20 28 32 35 35 3c 28   ||.      (255<(
2234b 6e 4e 61 6d 65 20 3d 20 73 74 72 6c 65 6e 28 7a  nName = strlen(z
2234c 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 29 29 29 20  FunctionName))) 
2234d 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
2234e 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 45  ror(db, SQLITE_E
2234f 52 52 4f 52 2c 20 22 62 61 64 20 70 61 72 61 6d  RROR, "bad param
22350 65 74 65 72 73 22 29 3b 0a 20 20 20 20 72 65 74  eters");.    ret
22351 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
22352 3b 0a 20 20 7d 0a 20 20 0a 23 69 66 6e 64 65 66  ;.  }.  .#ifndef
22353 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
22354 31 36 0a 20 20 2f 2a 20 49 66 20 53 51 4c 49 54  16.  /* If SQLIT
22355 45 5f 55 54 46 31 36 20 69 73 20 73 70 65 63 69  E_UTF16 is speci
22356 66 69 65 64 20 61 73 20 74 68 65 20 65 6e 63 6f  fied as the enco
22357 64 69 6e 67 20 74 79 70 65 2c 20 74 72 61 6e 73  ding type, trans
22358 66 6f 72 6d 20 74 68 69 73 0a 20 20 2a 2a 20 74  form this.  ** t
22359 6f 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f  o one of SQLITE_
2235a 55 54 46 31 36 4c 45 20 6f 72 20 53 51 4c 49 54  UTF16LE or SQLIT
2235b 45 5f 55 54 46 31 36 42 45 20 75 73 69 6e 67 20  E_UTF16BE using 
2235c 74 68 65 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f  the.  ** SQLITE_
2235d 55 54 46 31 36 4e 41 54 49 56 45 20 6d 61 63 72  UTF16NATIVE macr
2235e 6f 2e 20 53 51 4c 49 54 45 5f 55 54 46 31 36 20  o. SQLITE_UTF16 
2235f 69 73 20 6e 6f 74 20 75 73 65 64 20 69 6e 74 65  is not used inte
22360 72 6e 61 6c 6c 79 2e 0a 20 20 2a 2a 0a 20 20 2a  rnally..  **.  *
22361 2a 20 49 66 20 53 51 4c 49 54 45 5f 41 4e 59 20  * If SQLITE_ANY 
22362 69 73 20 73 70 65 63 69 66 69 65 64 2c 20 61 64  is specified, ad
22363 64 20 74 68 72 65 65 20 76 65 72 73 69 6f 6e 73  d three versions
22364 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   of the function
22365 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 68 61 73  .  ** to the has
22366 68 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20  h table..  */.  
22367 69 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f  if( enc==SQLITE_
22368 55 54 46 31 36 20 29 7b 0a 20 20 20 20 65 6e 63  UTF16 ){.    enc
22369 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e   = SQLITE_UTF16N
2236a 41 54 49 56 45 3b 0a 20 20 7d 65 6c 73 65 20 69  ATIVE;.  }else i
2236b 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 41  f( enc==SQLITE_A
2236c 4e 59 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63  NY ){.    int rc
2236d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
2236e 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c  e3CreateFunc(db,
2236f 20 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20   zFunctionName, 
22370 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46  nArg, SQLITE_UTF
22371 38 2c 0a 20 20 20 20 20 20 20 20 20 70 55 73 65  8,.         pUse
22372 72 44 61 74 61 2c 20 78 46 75 6e 63 2c 20 78 53  rData, xFunc, xS
22373 74 65 70 2c 20 78 46 69 6e 61 6c 29 3b 0a 20 20  tep, xFinal);.  
22374 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
22375 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
22376 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
22377 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20  3CreateFunc(db, 
22378 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20 6e  zFunctionName, n
22379 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  Arg, SQLITE_UTF1
2237a 36 4c 45 2c 0a 20 20 20 20 20 20 20 20 70 55 73  6LE,.        pUs
2237b 65 72 44 61 74 61 2c 20 78 46 75 6e 63 2c 20 78  erData, xFunc, x
2237c 53 74 65 70 2c 20 78 46 69 6e 61 6c 29 3b 0a 20  Step, xFinal);. 
2237d 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
2237e 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
2237f 3b 0a 20 20 20 20 65 6e 63 20 3d 20 53 51 4c 49  ;.    enc = SQLI
22380 54 45 5f 55 54 46 31 36 42 45 3b 0a 20 20 7d 0a  TE_UTF16BE;.  }.
22381 23 65 6c 73 65 0a 20 20 65 6e 63 20 3d 20 53 51  #else.  enc = SQ
22382 4c 49 54 45 5f 55 54 46 38 3b 0a 23 65 6e 64 69  LITE_UTF8;.#endi
22383 66 0a 20 20 0a 20 20 2f 2a 20 43 68 65 63 6b 20  f.  .  /* Check 
22384 69 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20 66  if an existing f
22385 75 6e 63 74 69 6f 6e 20 69 73 20 62 65 69 6e 67  unction is being
22386 20 6f 76 65 72 72 69 64 64 65 6e 20 6f 72 20 64   overridden or d
22387 65 6c 65 74 65 64 2e 20 49 66 20 73 6f 2c 0a 20  eleted. If so,. 
22388 20 2a 2a 20 61 6e 64 20 74 68 65 72 65 20 61 72   ** and there ar
22389 65 20 61 63 74 69 76 65 20 56 4d 73 2c 20 74 68  e active VMs, th
2238a 65 6e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  en return SQLITE
2238b 5f 42 55 53 59 2e 20 49 66 20 61 20 66 75 6e 63  _BUSY. If a func
2238c 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 62 65 69  tion.  ** is bei
2238d 6e 67 20 6f 76 65 72 72 69 64 64 65 6e 2f 64 65  ng overridden/de
2238e 6c 65 74 65 64 20 62 75 74 20 74 68 65 72 65 20  leted but there 
2238f 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20 56 4d  are no active VM
22390 73 2c 20 61 6c 6c 6f 77 20 74 68 65 0a 20 20 2a  s, allow the.  *
22391 2a 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 63  * operation to c
22392 6f 6e 74 69 6e 75 65 20 62 75 74 20 69 6e 76 61  ontinue but inva
22393 6c 69 64 61 74 65 20 61 6c 6c 20 70 72 65 63 6f  lidate all preco
22394 6d 70 69 6c 65 64 20 73 74 61 74 65 6d 65 6e 74  mpiled statement
22395 73 2e 0a 20 20 2a 2f 0a 20 20 70 20 3d 20 73 71  s..  */.  p = sq
22396 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f  lite3FindFunctio
22397 6e 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e  n(db, zFunctionN
22398 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 6e 41 72 67  ame, nName, nArg
22399 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20 69 66 28  , enc, 0);.  if(
2239a 20 70 20 26 26 20 70 2d 3e 69 50 72 65 66 45 6e   p && p->iPrefEn
2239b 63 3d 3d 65 6e 63 20 26 26 20 70 2d 3e 6e 41 72  c==enc && p->nAr
2239c 67 3d 3d 6e 41 72 67 20 29 7b 0a 20 20 20 20 69  g==nArg ){.    i
2239d 66 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62  f( db->activeVdb
2239e 65 43 6e 74 20 29 7b 0a 20 20 20 20 20 20 73 71  eCnt ){.      sq
2239f 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53  lite3Error(db, S
223a0 51 4c 49 54 45 5f 42 55 53 59 2c 20 0a 20 20 20  QLITE_BUSY, .   
223a1 20 20 20 20 20 22 55 6e 61 62 6c 65 20 74 6f 20       "Unable to 
223a2 64 65 6c 65 74 65 2f 6d 6f 64 69 66 79 20 75 73  delete/modify us
223a3 65 72 2d 66 75 6e 63 74 69 6f 6e 20 64 75 65 20  er-function due 
223a4 74 6f 20 61 63 74 69 76 65 20 73 74 61 74 65 6d  to active statem
223a5 65 6e 74 73 22 29 3b 0a 20 20 20 20 20 20 61 73  ents");.      as
223a6 73 65 72 74 28 20 21 73 71 6c 69 74 65 33 4d 61  sert( !sqlite3Ma
223a7 6c 6c 6f 63 46 61 69 6c 65 64 28 29 20 29 3b 0a  llocFailed() );.
223a8 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
223a9 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 65  ITE_BUSY;.    }e
223aa 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
223ab 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64  e3ExpirePrepared
223ac 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a  Statements(db);.
223ad 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 20 3d      }.  }..  p =
223ae 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63   sqlite3FindFunc
223af 74 69 6f 6e 28 64 62 2c 20 7a 46 75 6e 63 74 69  tion(db, zFuncti
223b0 6f 6e 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 6e  onName, nName, n
223b1 41 72 67 2c 20 65 6e 63 2c 20 31 29 3b 0a 20 20  Arg, enc, 1);.  
223b2 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e  if( p ){.    p->
223b3 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20 20 70  flags = 0;.    p
223b4 2d 3e 78 46 75 6e 63 20 3d 20 78 46 75 6e 63 3b  ->xFunc = xFunc;
223b5 0a 20 20 20 20 70 2d 3e 78 53 74 65 70 20 3d 20  .    p->xStep = 
223b6 78 53 74 65 70 3b 0a 20 20 20 20 70 2d 3e 78 46  xStep;.    p->xF
223b7 69 6e 61 6c 69 7a 65 20 3d 20 78 46 69 6e 61 6c  inalize = xFinal
223b8 3b 0a 20 20 20 20 70 2d 3e 70 55 73 65 72 44 61  ;.    p->pUserDa
223b9 74 61 20 3d 20 70 55 73 65 72 44 61 74 61 3b 0a  ta = pUserData;.
223ba 20 20 20 20 70 2d 3e 6e 41 72 67 20 3d 20 6e 41      p->nArg = nA
223bb 72 67 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  rg;.  }.  return
223bc 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
223bd 2a 0a 2a 2a 20 43 72 65 61 74 65 20 6e 65 77 20  *.** Create new 
223be 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 73 2e 0a  user functions..
223bf 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  */.int sqlite3_c
223c0 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 0a  reate_function(.
223c1 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20    sqlite3 *db,. 
223c2 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 75   const char *zFu
223c3 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e  nctionName,.  in
223c4 74 20 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65 6e  t nArg,.  int en
223c5 63 2c 0a 20 20 76 6f 69 64 20 2a 70 2c 0a 20 20  c,.  void *p,.  
223c6 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71  void (*xFunc)(sq
223c7 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
223c8 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt,sqlite3_value
223c9 20 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78   **),.  void (*x
223ca 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f  Step)(sqlite3_co
223cb 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74  ntext*,int,sqlit
223cc 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a 20 20  e3_value **),.  
223cd 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73  void (*xFinal)(s
223ce 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29  qlite3_context*)
223cf 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
223d0 61 73 73 65 72 74 28 20 21 73 71 6c 69 74 65 33  assert( !sqlite3
223d1 4d 61 6c 6c 6f 63 46 61 69 6c 65 64 28 29 20 29  MallocFailed() )
223d2 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
223d3 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 7a  CreateFunc(db, z
223d4 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20 6e 41  FunctionName, nA
223d5 72 67 2c 20 65 6e 63 2c 20 70 2c 20 78 46 75 6e  rg, enc, p, xFun
223d6 63 2c 20 78 53 74 65 70 2c 20 78 46 69 6e 61 6c  c, xStep, xFinal
223d7 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  );..  return sql
223d8 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20  ite3ApiExit(db, 
223d9 72 63 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  rc);.}..#ifndef 
223da 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
223db 36 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72  6.int sqlite3_cr
223dc 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28  eate_function16(
223dd 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a  .  sqlite3 *db,.
223de 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 46    const void *zF
223df 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69  unctionName,.  i
223e0 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65  nt nArg,.  int e
223e1 54 65 78 74 52 65 70 2c 0a 20 20 76 6f 69 64 20  TextRep,.  void 
223e2 2a 70 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 75  *p,.  void (*xFu
223e3 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  nc)(sqlite3_cont
223e4 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
223e5 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69  _value**),.  voi
223e6 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74  d (*xStep)(sqlit
223e7 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c  e3_context*,int,
223e8 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29  sqlite3_value**)
223e9 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61  ,.  void (*xFina
223ea 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  l)(sqlite3_conte
223eb 78 74 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 72 63  xt*).){.  int rc
223ec 3b 0a 20 20 63 68 61 72 20 2a 7a 46 75 6e 63 38  ;.  char *zFunc8
223ed 3b 0a 20 20 61 73 73 65 72 74 28 20 21 73 71 6c  ;.  assert( !sql
223ee 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69 6c 65 64  ite3MallocFailed
223ef 28 29 20 29 3b 0a 0a 20 20 7a 46 75 6e 63 38 20  () );..  zFunc8 
223f0 3d 20 73 71 6c 69 74 65 33 55 74 66 31 36 74 6f  = sqlite3Utf16to
223f1 38 28 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c  8(zFunctionName,
223f2 20 2d 31 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c   -1);.  rc = sql
223f3 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 64  ite3CreateFunc(d
223f4 62 2c 20 7a 46 75 6e 63 38 2c 20 6e 41 72 67 2c  b, zFunc8, nArg,
223f5 20 65 54 65 78 74 52 65 70 2c 20 70 2c 20 78 46   eTextRep, p, xF
223f6 75 6e 63 2c 20 78 53 74 65 70 2c 20 78 46 69 6e  unc, xStep, xFin
223f7 61 6c 29 3b 0a 20 20 73 71 6c 69 74 65 46 72 65  al);.  sqliteFre
223f8 65 28 7a 46 75 6e 63 38 29 3b 0a 0a 20 20 72 65  e(zFunc8);..  re
223f9 74 75 72 6e 20 73 71 6c 69 74 65 33 41 70 69 45  turn sqlite3ApiE
223fa 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 7d 0a 23  xit(db, rc);.}.#
223fb 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65  endif.../*.** De
223fc 63 6c 61 72 65 20 74 68 61 74 20 61 20 66 75 6e  clare that a fun
223fd 63 74 69 6f 6e 20 68 61 73 20 62 65 65 6e 20 6f  ction has been o
223fe 76 65 72 6c 6f 61 64 65 64 20 62 79 20 61 20 76  verloaded by a v
223ff 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a  irtual table..**
22400 0a 2a 2a 20 49 66 20 74 68 65 20 66 75 6e 63 74  .** If the funct
22401 69 6f 6e 20 61 6c 72 65 61 64 79 20 65 78 69 73  ion already exis
22402 74 73 20 61 73 20 61 20 72 65 67 75 6c 61 72 20  ts as a regular 
22403 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69 6f 6e 2c  global function,
22404 20 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20 72 6f   then.** this ro
22405 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70  utine is a no-op
22406 2e 20 20 49 66 20 74 68 65 20 66 75 6e 63 74 69  .  If the functi
22407 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73  on does not exis
22408 74 2c 20 74 68 65 6e 20 63 72 65 61 74 65 0a 2a  t, then create.*
22409 2a 20 61 20 6e 65 77 20 6f 6e 65 20 74 68 61 74  * a new one that
2240a 20 61 6c 77 61 79 73 20 74 68 72 6f 77 73 20 61   always throws a
2240b 20 72 75 6e 2d 74 69 6d 65 20 65 72 72 6f 72 2e   run-time error.
2240c 20 20 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 76 69    .**.** When vi
2240d 72 74 75 61 6c 20 74 61 62 6c 65 73 20 69 6e 74  rtual tables int
2240e 65 6e 64 20 74 6f 20 70 72 6f 76 69 64 65 20 61  end to provide a
2240f 6e 20 6f 76 65 72 6c 6f 61 64 65 64 20 66 75 6e  n overloaded fun
22410 63 74 69 6f 6e 2c 20 74 68 65 79 0a 2a 2a 20 73  ction, they.** s
22411 68 6f 75 6c 64 20 63 61 6c 6c 20 74 68 69 73 20  hould call this 
22412 72 6f 75 74 69 6e 65 20 74 6f 20 6d 61 6b 65 20  routine to make 
22413 73 75 72 65 20 74 68 65 20 67 6c 6f 62 61 6c 20  sure the global 
22414 66 75 6e 63 74 69 6f 6e 20 65 78 69 73 74 73 2e  function exists.
22415 0a 2a 2a 20 41 20 67 6c 6f 62 61 6c 20 66 75 6e  .** A global fun
22416 63 74 69 6f 6e 20 6d 75 73 74 20 65 78 69 73 74  ction must exist
22417 20 69 6e 20 6f 72 64 65 72 20 66 6f 72 20 6e 61   in order for na
22418 6d 65 20 72 65 73 6f 6c 75 74 69 6f 6e 20 74 6f  me resolution to
22419 20 77 6f 72 6b 0a 2a 2a 20 70 72 6f 70 65 72 6c   work.** properl
2241a 79 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65  y..*/.int sqlite
2241b 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74  3_overload_funct
2241c 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ion(.  sqlite3 *
2241d 64 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  db,.  const char
2241e 20 2a 7a 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e   *zName,.  int n
2241f 41 72 67 0a 29 7b 0a 20 20 69 6e 74 20 6e 4e 61  Arg.){.  int nNa
22420 6d 65 20 3d 20 73 74 72 6c 65 6e 28 7a 4e 61 6d  me = strlen(zNam
22421 65 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  e);.  if( sqlite
22422 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62  3FindFunction(db
22423 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20  , zName, nName, 
22424 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46  nArg, SQLITE_UTF
22425 38 2c 20 30 29 3d 3d 30 20 29 7b 0a 20 20 20 20  8, 0)==0 ){.    
22426 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e  sqlite3CreateFun
22427 63 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 6e 41 72  c(db, zName, nAr
22428 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 0a  g, SQLITE_UTF8,.
22429 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2242a 20 20 20 20 20 20 30 2c 20 73 71 6c 69 74 65 33        0, sqlite3
2242b 49 6e 76 61 6c 69 64 46 75 6e 63 74 69 6f 6e 2c  InvalidFunction,
2242c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65   0, 0);.  }.  re
2242d 74 75 72 6e 20 73 71 6c 69 74 65 33 41 70 69 45  turn sqlite3ApiE
2242e 78 69 74 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f  xit(db, SQLITE_O
2242f 4b 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53  K);.}..#ifndef S
22430 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45  QLITE_OMIT_TRACE
22431 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20  ./*.** Register 
22432 61 20 74 72 61 63 65 20 66 75 6e 63 74 69 6f 6e  a trace function
22433 2e 20 20 54 68 65 20 70 41 72 67 20 66 72 6f 6d  .  The pArg from
22434 20 74 68 65 20 70 72 65 76 69 6f 75 73 6c 79 20   the previously 
22435 72 65 67 69 73 74 65 72 65 64 20 74 72 61 63 65  registered trace
22436 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e  .** is returned.
22437 20 20 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20    .**.** A NULL 
22438 74 72 61 63 65 20 66 75 6e 63 74 69 6f 6e 20 6d  trace function m
22439 65 61 6e 73 20 74 68 61 74 20 6e 6f 20 74 72 61  eans that no tra
2243a 63 69 6e 67 20 69 73 20 65 78 65 63 75 74 65 73  cing is executes
2243b 2e 20 20 41 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a  .  A non-NULL.**
2243c 20 74 72 61 63 65 20 69 73 20 61 20 70 6f 69 6e   trace is a poin
2243d 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f  ter to a functio
2243e 6e 20 74 68 61 74 20 69 73 20 69 6e 76 6f 6b 65  n that is invoke
2243f 64 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f  d at the start o
22440 66 20 65 61 63 68 0a 2a 2a 20 53 51 4c 20 73 74  f each.** SQL st
22441 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49  atement..*/.SQLI
22442 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
22443 69 74 65 33 5f 74 72 61 63 65 28 73 71 6c 69 74  ite3_trace(sqlit
22444 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 28 2a 78  e3 *db, void (*x
22445 54 72 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e  Trace)(void*,con
22446 73 74 20 63 68 61 72 2a 29 2c 20 76 6f 69 64 20  st char*), void 
22447 2a 70 41 72 67 29 7b 0a 20 20 76 6f 69 64 20 2a  *pArg){.  void *
22448 70 4f 6c 64 20 3d 20 64 62 2d 3e 70 54 72 61 63  pOld = db->pTrac
22449 65 41 72 67 3b 0a 20 20 64 62 2d 3e 78 54 72 61  eArg;.  db->xTra
2244a 63 65 20 3d 20 78 54 72 61 63 65 3b 0a 20 20 64  ce = xTrace;.  d
2244b 62 2d 3e 70 54 72 61 63 65 41 72 67 20 3d 20 70  b->pTraceArg = p
2244c 41 72 67 3b 0a 20 20 72 65 74 75 72 6e 20 70 4f  Arg;.  return pO
2244d 6c 64 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 52 65 67 69  ld;.}./*.** Regi
2244e 73 74 65 72 20 61 20 70 72 6f 66 69 6c 65 20 66  ster a profile f
2244f 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 70 41  unction.  The pA
22450 72 67 20 66 72 6f 6d 20 74 68 65 20 70 72 65 76  rg from the prev
22451 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 65  iously registere
22452 64 20 0a 2a 2a 20 70 72 6f 66 69 6c 65 20 66 75  d .** profile fu
22453 6e 63 74 69 6f 6e 20 69 73 20 72 65 74 75 72 6e  nction is return
22454 65 64 2e 20 20 0a 2a 2a 0a 2a 2a 20 41 20 4e 55  ed.  .**.** A NU
22455 4c 4c 20 70 72 6f 66 69 6c 65 20 66 75 6e 63 74  LL profile funct
22456 69 6f 6e 20 6d 65 61 6e 73 20 74 68 61 74 20 6e  ion means that n
22457 6f 20 70 72 6f 66 69 6c 69 6e 67 20 69 73 20 65  o profiling is e
22458 78 65 63 75 74 65 73 2e 20 20 41 20 6e 6f 6e 2d  xecutes.  A non-
22459 4e 55 4c 4c 0a 2a 2a 20 70 72 6f 66 69 6c 65 20  NULL.** profile 
2245a 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
2245b 61 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20  a function that 
2245c 69 73 20 69 6e 76 6f 6b 65 64 20 61 74 20 74 68  is invoked at th
2245d 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 0a  e conclusion of.
2245e 2a 2a 20 65 61 63 68 20 53 51 4c 20 73 74 61 74  ** each SQL stat
2245f 65 6d 65 6e 74 20 74 68 61 74 20 69 73 20 72 75  ement that is ru
22460 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  n..*/.SQLITE_API
22461 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 70   void *sqlite3_p
22462 72 6f 66 69 6c 65 28 0a 20 20 73 71 6c 69 74 65  rofile(.  sqlite
22463 33 20 2a 64 62 2c 0a 20 20 76 6f 69 64 20 28 2a  3 *db,.  void (*
22464 78 50 72 6f 66 69 6c 65 29 28 76 6f 69 64 2a 2c  xProfile)(void*,
22465 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c 69  const char*,sqli
22466 74 65 5f 75 69 6e 74 36 34 29 2c 0a 20 20 76 6f  te_uint64),.  vo
22467 69 64 20 2a 70 41 72 67 0a 29 7b 0a 20 20 76 6f  id *pArg.){.  vo
22468 69 64 20 2a 70 4f 6c 64 20 3d 20 64 62 2d 3e 70  id *pOld = db->p
22469 50 72 6f 66 69 6c 65 41 72 67 3b 0a 20 20 64 62  ProfileArg;.  db
2246a 2d 3e 78 50 72 6f 66 69 6c 65 20 3d 20 78 50 72  ->xProfile = xPr
2246b 6f 66 69 6c 65 3b 0a 20 20 64 62 2d 3e 70 50 72  ofile;.  db->pPr
2246c 6f 66 69 6c 65 41 72 67 20 3d 20 70 41 72 67 3b  ofileArg = pArg;
2246d 0a 20 20 72 65 74 75 72 6e 20 70 4f 6c 64 3b 0a  .  return pOld;.
2246e 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
2246f 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 20 2a 2f  TE_OMIT_TRACE */
22470 0a 0a 2f 2a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  ../*** EXPERIMEN
22471 54 41 4c 20 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 52 65  TAL ***.**.** Re
22472 67 69 73 74 65 72 20 61 20 66 75 6e 63 74 69 6f  gister a functio
22473 6e 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20  n to be invoked 
22474 77 68 65 6e 20 61 20 74 72 61 6e 73 61 63 74 69  when a transacti
22475 6f 6e 20 63 6f 6d 6d 65 6e 74 73 2e 0a 2a 2a 20  on comments..** 
22476 49 66 20 74 68 65 20 69 6e 76 6f 6b 65 64 20 66  If the invoked f
22477 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
22478 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74  non-zero, then t
22479 68 65 20 63 6f 6d 6d 69 74 20 62 65 63 6f 6d 65  he commit become
2247a 73 20 61 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 2e  s a.** rollback.
2247b 0a 2a 2f 0a 76 6f 69 64 20 2a 73 71 6c 69 74 65  .*/.void *sqlite
2247c 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 0a 20  3_commit_hook(. 
2247d 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
2247e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 74             /* At
2247f 74 61 63 68 20 74 68 65 20 68 6f 6f 6b 20 74 6f  tach the hook to
22480 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 2a   this database *
22481 2f 0a 20 20 69 6e 74 20 28 2a 78 43 61 6c 6c 62  /.  int (*xCallb
22482 61 63 6b 29 28 76 6f 69 64 2a 29 2c 20 20 2f 2a  ack)(void*),  /*
22483 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e 76   Function to inv
22484 6f 6b 65 20 6f 6e 20 65 61 63 68 20 63 6f 6d 6d  oke on each comm
22485 69 74 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41  it */.  void *pA
22486 72 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20  rg              
22487 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f    /* Argument to
22488 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f   the function */
22489 0a 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4f 6c 64  .){.  void *pOld
2248a 20 3d 20 64 62 2d 3e 70 43 6f 6d 6d 69 74 41 72   = db->pCommitAr
2248b 67 3b 0a 20 20 64 62 2d 3e 78 43 6f 6d 6d 69 74  g;.  db->xCommit
2248c 43 61 6c 6c 62 61 63 6b 20 3d 20 78 43 61 6c 6c  Callback = xCall
2248d 62 61 63 6b 3b 0a 20 20 64 62 2d 3e 70 43 6f 6d  back;.  db->pCom
2248e 6d 69 74 41 72 67 20 3d 20 70 41 72 67 3b 0a 20  mitArg = pArg;. 
2248f 20 72 65 74 75 72 6e 20 70 4f 6c 64 3b 0a 7d 0a   return pOld;.}.
22490 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20  ./*.** Register 
22491 61 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 62 65  a callback to be
22492 20 69 6e 76 6f 6b 65 64 20 65 61 63 68 20 74 69   invoked each ti
22493 6d 65 20 61 20 72 6f 77 20 69 73 20 75 70 64 61  me a row is upda
22494 74 65 64 2c 0a 2a 2a 20 69 6e 73 65 72 74 65 64  ted,.** inserted
22495 20 6f 72 20 64 65 6c 65 74 65 64 20 75 73 69 6e   or deleted usin
22496 67 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20  g this database 
22497 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 76  connection..*/.v
22498 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 75 70 64  oid *sqlite3_upd
22499 61 74 65 5f 68 6f 6f 6b 28 0a 20 20 73 71 6c 69  ate_hook(.  sqli
2249a 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
2249b 20 20 20 20 20 20 2f 2a 20 41 74 74 61 63 68 20        /* Attach 
2249c 74 68 65 20 68 6f 6f 6b 20 74 6f 20 74 68 69 73  the hook to this
2249d 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 76   database */.  v
2249e 6f 69 64 20 28 2a 78 43 61 6c 6c 62 61 63 6b 29  oid (*xCallback)
2249f 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 68 61 72 20  (void*,int,char 
224a0 63 6f 6e 73 74 20 2a 2c 63 68 61 72 20 63 6f 6e  const *,char con
224a1 73 74 20 2a 2c 73 71 6c 69 74 65 5f 69 6e 74 36  st *,sqlite_int6
224a2 34 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67  4),.  void *pArg
224a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
224a4 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74  /* Argument to t
224a5 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29  he function */.)
224a6 7b 0a 20 20 76 6f 69 64 20 2a 70 52 65 74 20 3d  {.  void *pRet =
224a7 20 64 62 2d 3e 70 55 70 64 61 74 65 41 72 67 3b   db->pUpdateArg;
224a8 0a 20 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61  .  db->xUpdateCa
224a9 6c 6c 62 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61  llback = xCallba
224aa 63 6b 3b 0a 20 20 64 62 2d 3e 70 55 70 64 61 74  ck;.  db->pUpdat
224ab 65 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 72  eArg = pArg;.  r
224ac 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a 2f  eturn pRet;.}../
224ad 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20  *.** Register a 
224ae 63 61 6c 6c 62 61 63 6b 20 74 6f 20 62 65 20 69  callback to be i
224af 6e 76 6f 6b 65 64 20 65 61 63 68 20 74 69 6d 65  nvoked each time
224b0 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69   a transaction i
224b1 73 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62 61 63 6b  s rolled.** back
224b2 20 62 79 20 74 68 69 73 20 64 61 74 61 62 61 73   by this databas
224b3 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f  e connection..*/
224b4 0a 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72  .void *sqlite3_r
224b5 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 0a 20 20  ollback_hook(.  
224b6 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
224b7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 74 74            /* Att
224b8 61 63 68 20 74 68 65 20 68 6f 6f 6b 20 74 6f 20  ach the hook to 
224b9 74 68 69 73 20 64 61 74 61 62 61 73 65 20 2a 2f  this database */
224ba 0a 20 20 76 6f 69 64 20 28 2a 78 43 61 6c 6c 62  .  void (*xCallb
224bb 61 63 6b 29 28 76 6f 69 64 2a 29 2c 20 2f 2a 20  ack)(void*), /* 
224bc 43 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f  Callback functio
224bd 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72  n */.  void *pAr
224be 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g               
224bf 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20   /* Argument to 
224c0 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  the function */.
224c1 29 7b 0a 20 20 76 6f 69 64 20 2a 70 52 65 74 20  ){.  void *pRet 
224c2 3d 20 64 62 2d 3e 70 52 6f 6c 6c 62 61 63 6b 41  = db->pRollbackA
224c3 72 67 3b 0a 20 20 64 62 2d 3e 78 52 6f 6c 6c 62  rg;.  db->xRollb
224c4 61 63 6b 43 61 6c 6c 62 61 63 6b 20 3d 20 78 43  ackCallback = xC
224c5 61 6c 6c 62 61 63 6b 3b 0a 20 20 64 62 2d 3e 70  allback;.  db->p
224c6 52 6f 6c 6c 62 61 63 6b 41 72 67 20 3d 20 70 41  RollbackArg = pA
224c7 72 67 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65  rg;.  return pRe
224c8 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  t;.}../*.** This
224c9 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
224ca 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 63  ed to create a c
224cb 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 61 20 64  onnection to a d
224cc 61 74 61 62 61 73 65 20 42 54 72 65 65 0a 2a 2a  atabase BTree.**
224cd 20 64 72 69 76 65 72 2e 20 20 49 66 20 7a 46 69   driver.  If zFi
224ce 6c 65 6e 61 6d 65 20 69 73 20 74 68 65 20 6e 61  lename is the na
224cf 6d 65 20 6f 66 20 61 20 66 69 6c 65 2c 20 74 68  me of a file, th
224d0 65 6e 20 74 68 61 74 20 66 69 6c 65 20 69 73 0a  en that file is.
224d1 2a 2a 20 6f 70 65 6e 65 64 20 61 6e 64 20 75 73  ** opened and us
224d2 65 64 2e 20 20 49 66 20 7a 46 69 6c 65 6e 61 6d  ed.  If zFilenam
224d3 65 20 69 73 20 74 68 65 20 6d 61 67 69 63 20 6e  e is the magic n
224d4 61 6d 65 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 74  ame ":memory:" t
224d5 68 65 6e 0a 2a 2a 20 74 68 65 20 64 61 74 61 62  hen.** the datab
224d6 61 73 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ase is stored in
224d7 20 6d 65 6d 6f 72 79 20 28 61 6e 64 20 69 73 20   memory (and is 
224d8 74 68 75 73 20 66 6f 72 67 6f 74 74 65 6e 20 61  thus forgotten a
224d9 73 20 73 6f 6f 6e 20 61 73 0a 2a 2a 20 74 68 65  s soon as.** the
224da 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63   connection is c
224db 6c 6f 73 65 64 2e 29 20 20 49 66 20 7a 46 69 6c  losed.)  If zFil
224dc 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c 20 74 68  ename is NULL th
224dd 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 0a  en the database.
224de 2a 2a 20 69 73 20 61 20 22 76 69 72 74 75 61 6c  ** is a "virtual
224df 22 20 64 61 74 61 62 61 73 65 20 66 6f 72 20 74  " database for t
224e0 72 61 6e 73 69 65 6e 74 20 75 73 65 20 6f 6e 6c  ransient use onl
224e1 79 20 61 6e 64 20 69 73 20 64 65 6c 65 74 65 64  y and is deleted
224e2 20 61 73 0a 2a 2a 20 73 6f 6f 6e 20 61 73 20 74   as.** soon as t
224e3 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73  he connection is
224e4 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 41   closed..**.** A
224e5 20 76 69 72 74 75 61 6c 20 64 61 74 61 62 61 73   virtual databas
224e6 65 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20  e can be either 
224e7 61 20 64 69 73 6b 20 66 69 6c 65 20 28 74 68 61  a disk file (tha
224e8 74 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c  t is automatical
224e9 6c 79 0a 2a 2a 20 64 65 6c 65 74 65 64 20 77 68  ly.** deleted wh
224ea 65 6e 20 74 68 65 20 66 69 6c 65 20 69 73 20 63  en the file is c
224eb 6c 6f 73 65 64 29 20 6f 72 20 69 74 20 61 6e 20  losed) or it an 
224ec 62 65 20 68 65 6c 64 20 65 6e 74 69 72 65 6c 79  be held entirely
224ed 20 69 6e 20 6d 65 6d 6f 72 79 2c 0a 2a 2a 20 64   in memory,.** d
224ee 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20  epending on the 
224ef 76 61 6c 75 65 73 20 6f 66 20 74 68 65 20 54 45  values of the TE
224f0 4d 50 5f 53 54 4f 52 45 20 63 6f 6d 70 69 6c 65  MP_STORE compile
224f1 2d 74 69 6d 65 20 6d 61 63 72 6f 20 61 6e 64 20  -time macro and 
224f2 74 68 65 0a 2a 2a 20 64 62 2d 3e 74 65 6d 70 5f  the.** db->temp_
224f3 73 74 6f 72 65 20 76 61 72 69 61 62 6c 65 2c 20  store variable, 
224f4 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65  according to the
224f5 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74   following chart
224f6 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 54 45  :.**.**       TE
224f7 4d 50 5f 53 54 4f 52 45 20 20 20 20 20 64 62 2d  MP_STORE     db-
224f8 3e 74 65 6d 70 5f 73 74 6f 72 65 20 20 20 20 20  >temp_store     
224f9 4c 6f 63 61 74 69 6f 6e 20 6f 66 20 74 65 6d 70  Location of temp
224fa 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 0a 2a  orary database.*
224fb 2a 20 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d  *       --------
224fc 2d 2d 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  --     ---------
224fd 2d 2d 2d 2d 2d 20 20 20 20 20 2d 2d 2d 2d 2d 2d  -----     ------
224fe 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
224ff 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20  --------.**     
22500 20 20 20 20 20 20 30 20 20 20 20 20 20 20 20 20        0         
22501 20 20 20 20 20 20 61 6e 79 20 20 20 20 20 20 20        any       
22502 20 20 20 20 20 20 66 69 6c 65 0a 2a 2a 20 20 20        file.**   
22503 20 20 20 20 20 20 20 20 31 20 20 20 20 20 20 20          1       
22504 20 20 20 20 20 20 20 20 20 31 20 20 20 20 20 20           1      
22505 20 20 20 20 20 20 20 20 66 69 6c 65 0a 2a 2a 20          file.** 
22506 20 20 20 20 20 20 20 20 20 20 31 20 20 20 20 20            1     
22507 20 20 20 20 20 20 20 20 20 20 20 32 20 20 20 20             2    
22508 20 20 20 20 20 20 20 20 20 20 6d 65 6d 6f 72 79            memory
22509 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 31 20  .**           1 
2250a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
2250b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 69                fi
2250c 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  le.**           
2250d 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  2               
2250e 20 31 20 20 20 20 20 20 20 20 20 20 20 20 20 20   1              
2250f 66 69 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  file.**         
22510 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20 20    2             
22511 20 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20     2            
22512 20 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 20 20    memory.**     
22513 20 20 20 20 20 20 32 20 20 20 20 20 20 20 20 20        2         
22514 20 20 20 20 20 20 20 30 20 20 20 20 20 20 20 20         0        
22515 20 20 20 20 20 20 6d 65 6d 6f 72 79 0a 2a 2a 20        memory.** 
22516 20 20 20 20 20 20 20 20 20 20 33 20 20 20 20 20            3     
22517 20 20 20 20 20 20 20 20 20 20 61 6e 79 20 20 20            any   
22518 20 20 20 20 20 20 20 20 20 20 6d 65 6d 6f 72 79            memory
22519 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2251a 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
2251b 72 65 65 46 61 63 74 6f 72 79 28 0a 20 20 63 6f  reeFactory(.  co
2251c 6e 73 74 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  nst sqlite3 *db,
2251d 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 69 6e 20          /* Main 
2251e 64 61 74 61 62 61 73 65 20 77 68 65 6e 20 6f 70  database when op
2251f 65 6e 69 6e 67 20 61 75 78 20 6f 74 68 65 72 77  ening aux otherw
22520 69 73 65 20 30 20 2a 2f 0a 20 20 63 6f 6e 73 74  ise 0 */.  const
22521 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
22522 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20  ,    /* Name of 
22523 74 68 65 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  the file contain
22524 69 6e 67 20 74 68 65 20 42 54 72 65 65 20 64 61  ing the BTree da
22525 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20  tabase */.  int 
22526 6f 6d 69 74 4a 6f 75 72 6e 61 6c 2c 20 20 20 20  omitJournal,    
22527 20 20 20 20 20 20 2f 2a 20 69 66 20 54 52 55 45        /* if TRUE
22528 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 6a 6f 75   then do not jou
22529 72 6e 61 6c 20 74 68 69 73 20 66 69 6c 65 20 2a  rnal this file *
2252a 2f 0a 20 20 69 6e 74 20 6e 43 61 63 68 65 2c 20  /.  int nCache, 
2252b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2252c 20 48 6f 77 20 6d 61 6e 79 20 70 61 67 65 73 20   How many pages 
2252d 69 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68  in the page cach
2252e 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 2a 70  e */.  Btree **p
2252f 70 42 74 72 65 65 20 20 20 20 20 20 20 20 20 20  pBtree          
22530 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6e   /* Pointer to n
22531 65 77 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20  ew Btree object 
22532 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a  written here */.
22533 29 7b 0a 20 20 69 6e 74 20 62 74 72 65 65 5f 66  ){.  int btree_f
22534 6c 61 67 73 20 3d 20 30 3b 0a 20 20 69 6e 74 20  lags = 0;.  int 
22535 72 63 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28  rc;.  .  assert(
22536 20 70 70 42 74 72 65 65 20 21 3d 20 30 29 3b 0a   ppBtree != 0);.
22537 20 20 69 66 28 20 6f 6d 69 74 4a 6f 75 72 6e 61    if( omitJourna
22538 6c 20 29 7b 0a 20 20 20 20 62 74 72 65 65 5f 66  l ){.    btree_f
22539 6c 61 67 73 20 7c 3d 20 42 54 52 45 45 5f 4f 4d  lags |= BTREE_OM
2253a 49 54 5f 4a 4f 55 52 4e 41 4c 3b 0a 20 20 7d 0a  IT_JOURNAL;.  }.
2253b 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 20    if( db->flags 
2253c 26 20 53 51 4c 49 54 45 5f 4e 6f 52 65 61 64 6c  & SQLITE_NoReadl
2253d 6f 63 6b 20 29 7b 0a 20 20 20 20 62 74 72 65 65  ock ){.    btree
2253e 5f 66 6c 61 67 73 20 7c 3d 20 42 54 52 45 45 5f  _flags |= BTREE_
2253f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 3b 0a 20 20 7d  NO_READLOCK;.  }
22540 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65  .  if( zFilename
22541 3d 3d 30 20 29 7b 0a 23 69 66 20 54 45 4d 50 5f  ==0 ){.#if TEMP_
22542 53 54 4f 52 45 3d 3d 30 0a 20 20 20 20 2f 2a 20  STORE==0.    /* 
22543 44 6f 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 23 65  Do nothing */.#e
22544 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c  ndif.#ifndef SQL
22545 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44  ITE_OMIT_MEMORYD
22546 42 0a 23 69 66 20 54 45 4d 50 5f 53 54 4f 52 45  B.#if TEMP_STORE
22547 3d 3d 31 0a 20 20 20 20 69 66 28 20 64 62 2d 3e  ==1.    if( db->
22548 74 65 6d 70 5f 73 74 6f 72 65 3d 3d 32 20 29 20  temp_store==2 ) 
22549 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 22 3a 6d 65  zFilename = ":me
2254a 6d 6f 72 79 3a 22 3b 0a 23 65 6e 64 69 66 0a 23  mory:";.#endif.#
2254b 69 66 20 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 32  if TEMP_STORE==2
2254c 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 74 65 6d  .    if( db->tem
2254d 70 5f 73 74 6f 72 65 21 3d 31 20 29 20 7a 46 69  p_store!=1 ) zFi
2254e 6c 65 6e 61 6d 65 20 3d 20 22 3a 6d 65 6d 6f 72  lename = ":memor
2254f 79 3a 22 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20  y:";.#endif.#if 
22550 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 33 0a 20 20  TEMP_STORE==3.  
22551 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 22 3a    zFilename = ":
22552 6d 65 6d 6f 72 79 3a 22 3b 0a 23 65 6e 64 69 66  memory:";.#endif
22553 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
22554 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 20  E_OMIT_MEMORYDB 
22555 2a 2f 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73  */.  }..  rc = s
22556 71 6c 69 74 65 33 42 74 72 65 65 4f 70 65 6e 28  qlite3BtreeOpen(
22557 7a 46 69 6c 65 6e 61 6d 65 2c 20 28 73 71 6c 69  zFilename, (sqli
22558 74 65 33 20 2a 29 64 62 2c 20 70 70 42 74 72 65  te3 *)db, ppBtre
22559 65 2c 20 62 74 72 65 65 5f 66 6c 61 67 73 29 3b  e, btree_flags);
2255a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
2255b 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69  E_OK ){.    sqli
2255c 74 65 33 42 74 72 65 65 53 65 74 42 75 73 79 48  te3BtreeSetBusyH
2255d 61 6e 64 6c 65 72 28 2a 70 70 42 74 72 65 65 2c  andler(*ppBtree,
2255e 20 28 76 6f 69 64 2a 29 26 64 62 2d 3e 62 75 73   (void*)&db->bus
2255f 79 48 61 6e 64 6c 65 72 29 3b 0a 20 20 20 20 73  yHandler);.    s
22560 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61  qlite3BtreeSetCa
22561 63 68 65 53 69 7a 65 28 2a 70 70 42 74 72 65 65  cheSize(*ppBtree
22562 2c 20 6e 43 61 63 68 65 29 3b 0a 20 20 7d 0a 20  , nCache);.  }. 
22563 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
22564 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 55 54 46 2d  *.** Return UTF-
22565 38 20 65 6e 63 6f 64 65 64 20 45 6e 67 6c 69 73  8 encoded Englis
22566 68 20 6c 61 6e 67 75 61 67 65 20 65 78 70 6c 61  h language expla
22567 6e 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f  nation of the mo
22568 73 74 20 72 65 63 65 6e 74 0a 2a 2a 20 65 72 72  st recent.** err
22569 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  or..*/.SQLITE_AP
2256a 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  I const char *sq
2256b 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 73 71 6c  lite3_errmsg(sql
2256c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 63 6f 6e  ite3 *db){.  con
2256d 73 74 20 63 68 61 72 20 2a 7a 3b 0a 20 20 61 73  st char *z;.  as
2256e 73 65 72 74 28 20 21 73 71 6c 69 74 65 33 4d 61  sert( !sqlite3Ma
2256f 6c 6c 6f 63 46 61 69 6c 65 64 28 29 20 29 3b 0a  llocFailed() );.
22570 20 20 69 66 28 20 21 64 62 20 29 7b 0a 20 20 20    if( !db ){.   
22571 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 45   return sqlite3E
22572 72 72 53 74 72 28 53 51 4c 49 54 45 5f 4e 4f 4d  rrStr(SQLITE_NOM
22573 45 4d 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73  EM);.  }.  if( s
22574 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63  qlite3SafetyChec
22575 6b 28 64 62 29 20 7c 7c 20 64 62 2d 3e 65 72 72  k(db) || db->err
22576 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4d 49 53  Code==SQLITE_MIS
22577 55 53 45 20 29 7b 0a 20 20 20 20 72 65 74 75 72  USE ){.    retur
22578 6e 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 28  n sqlite3ErrStr(
22579 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 29 3b 0a  SQLITE_MISUSE);.
2257a 20 20 7d 0a 20 20 7a 20 3d 20 28 63 68 61 72 2a    }.  z = (char*
2257b 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  )sqlite3_value_t
2257c 65 78 74 28 64 62 2d 3e 70 45 72 72 29 3b 0a 20  ext(db->pErr);. 
2257d 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20   if( z==0 ){.   
2257e 20 7a 20 3d 20 73 71 6c 69 74 65 33 45 72 72 53   z = sqlite3ErrS
2257f 74 72 28 64 62 2d 3e 65 72 72 43 6f 64 65 29 3b  tr(db->errCode);
22580 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 3b  .  }.  return z;
22581 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
22582 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a  TE_OMIT_UTF16./*
22583 0a 2a 2a 20 52 65 74 75 72 6e 20 55 54 46 2d 31  .** Return UTF-1
22584 36 20 65 6e 63 6f 64 65 64 20 45 6e 67 6c 69 73  6 encoded Englis
22585 68 20 6c 61 6e 67 75 61 67 65 20 65 78 70 6c 61  h language expla
22586 6e 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f  nation of the mo
22587 73 74 20 72 65 63 65 6e 74 0a 2a 2a 20 65 72 72  st recent.** err
22588 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  or..*/.SQLITE_AP
22589 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  I const void *sq
2258a 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 73  lite3_errmsg16(s
2258b 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 2f  qlite3 *db){.  /
2258c 2a 20 42 65 63 61 75 73 65 20 61 6c 6c 20 74 68  * Because all th
2258d 65 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20  e characters in 
2258e 74 68 65 20 73 74 72 69 6e 67 20 61 72 65 20 69  the string are i
2258f 6e 20 74 68 65 20 75 6e 69 63 6f 64 65 0a 20 20  n the unicode.  
22590 2a 2a 20 72 61 6e 67 65 20 30 78 30 30 2d 30 78  ** range 0x00-0x
22591 46 46 2c 20 69 66 20 77 65 20 70 61 64 20 74 68  FF, if we pad th
22592 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 73 74 72  e big-endian str
22593 69 6e 67 20 77 69 74 68 20 61 20 0a 20 20 2a 2a  ing with a .  **
22594 20 7a 65 72 6f 20 62 79 74 65 2c 20 77 65 20 63   zero byte, we c
22595 61 6e 20 6f 62 74 61 69 6e 20 74 68 65 20 6c 69  an obtain the li
22596 74 74 6c 65 2d 65 6e 64 69 61 6e 20 73 74 72 69  ttle-endian stri
22597 6e 67 20 77 69 74 68 0a 20 20 2a 2a 20 26 62 69  ng with.  ** &bi
22598 67 5f 65 6e 64 69 61 6e 5b 31 5d 2e 0a 20 20 2a  g_endian[1]..  *
22599 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  /.  static const
2259a 20 63 68 61 72 20 6f 75 74 4f 66 4d 65 6d 42 65   char outOfMemBe
2259b 5b 5d 20 3d 20 7b 0a 20 20 20 20 30 2c 20 27 6f  [] = {.    0, 'o
2259c 27 2c 20 30 2c 20 27 75 27 2c 20 30 2c 20 27 74  ', 0, 'u', 0, 't
2259d 27 2c 20 30 2c 20 27 20 27 2c 20 0a 20 20 20 20  ', 0, ' ', .    
2259e 30 2c 20 27 6f 27 2c 20 30 2c 20 27 66 27 2c 20  0, 'o', 0, 'f', 
2259f 30 2c 20 27 20 27 2c 20 0a 20 20 20 20 30 2c 20  0, ' ', .    0, 
225a0 27 6d 27 2c 20 30 2c 20 27 65 27 2c 20 30 2c 20  'm', 0, 'e', 0, 
225a1 27 6d 27 2c 20 30 2c 20 27 6f 27 2c 20 30 2c 20  'm', 0, 'o', 0, 
225a2 27 72 27 2c 20 30 2c 20 27 79 27 2c 20 30 2c 20  'r', 0, 'y', 0, 
225a3 30 2c 20 30 0a 20 20 7d 3b 0a 20 20 73 74 61 74  0, 0.  };.  stat
225a4 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 6d 69  ic const char mi
225a5 73 75 73 65 42 65 20 5b 5d 20 3d 20 7b 0a 20 20  suseBe [] = {.  
225a6 20 20 30 2c 20 27 6c 27 2c 20 30 2c 20 27 69 27    0, 'l', 0, 'i'
225a7 2c 20 30 2c 20 27 62 27 2c 20 30 2c 20 27 72 27  , 0, 'b', 0, 'r'
225a8 2c 20 30 2c 20 27 61 27 2c 20 30 2c 20 27 72 27  , 0, 'a', 0, 'r'
225a9 2c 20 30 2c 20 27 79 27 2c 20 30 2c 20 27 20 27  , 0, 'y', 0, ' '
225aa 2c 20 0a 20 20 20 20 30 2c 20 27 72 27 2c 20 30  , .    0, 'r', 0
225ab 2c 20 27 6f 27 2c 20 30 2c 20 27 75 27 2c 20 30  , 'o', 0, 'u', 0
225ac 2c 20 27 74 27 2c 20 30 2c 20 27 69 27 2c 20 30  , 't', 0, 'i', 0
225ad 2c 20 27 6e 27 2c 20 30 2c 20 27 65 27 2c 20 30  , 'n', 0, 'e', 0
225ae 2c 20 27 20 27 2c 20 0a 20 20 20 20 30 2c 20 27  , ' ', .    0, '
225af 63 27 2c 20 30 2c 20 27 61 27 2c 20 30 2c 20 27  c', 0, 'a', 0, '
225b0 6c 27 2c 20 30 2c 20 27 6c 27 2c 20 30 2c 20 27  l', 0, 'l', 0, '
225b1 65 27 2c 20 30 2c 20 27 64 27 2c 20 30 2c 20 27  e', 0, 'd', 0, '
225b2 20 27 2c 20 0a 20 20 20 20 30 2c 20 27 6f 27 2c   ', .    0, 'o',
225b3 20 30 2c 20 27 75 27 2c 20 30 2c 20 27 74 27 2c   0, 'u', 0, 't',
225b4 20 30 2c 20 27 20 27 2c 20 0a 20 20 20 20 30 2c   0, ' ', .    0,
225b5 20 27 6f 27 2c 20 30 2c 20 27 66 27 2c 20 30 2c   'o', 0, 'f', 0,
225b6 20 27 20 27 2c 20 0a 20 20 20 20 30 2c 20 27 73   ' ', .    0, 's
225b7 27 2c 20 30 2c 20 27 65 27 2c 20 30 2c 20 27 71  ', 0, 'e', 0, 'q
225b8 27 2c 20 30 2c 20 27 75 27 2c 20 30 2c 20 27 65  ', 0, 'u', 0, 'e
225b9 27 2c 20 30 2c 20 27 6e 27 2c 20 30 2c 20 27 63  ', 0, 'n', 0, 'c
225ba 27 2c 20 30 2c 20 27 65 27 2c 20 30 2c 20 30 2c  ', 0, 'e', 0, 0,
225bb 20 30 0a 20 20 7d 3b 0a 0a 20 20 63 6f 6e 73 74   0.  };..  const
225bc 20 76 6f 69 64 20 2a 7a 3b 0a 20 20 61 73 73 65   void *z;.  asse
225bd 72 74 28 20 21 73 71 6c 69 74 65 33 4d 61 6c 6c  rt( !sqlite3Mall
225be 6f 63 46 61 69 6c 65 64 28 29 20 29 3b 0a 20 20  ocFailed() );.  
225bf 69 66 28 20 21 64 62 20 29 7b 0a 20 20 20 20 72  if( !db ){.    r
225c0 65 74 75 72 6e 20 28 76 6f 69 64 20 2a 29 28 26  eturn (void *)(&
225c1 6f 75 74 4f 66 4d 65 6d 42 65 5b 53 51 4c 49 54  outOfMemBe[SQLIT
225c2 45 5f 55 54 46 31 36 4e 41 54 49 56 45 3d 3d 53  E_UTF16NATIVE==S
225c3 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 3f 31 3a  QLITE_UTF16LE?1:
225c4 30 5d 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73  0]);.  }.  if( s
225c5 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63  qlite3SafetyChec
225c6 6b 28 64 62 29 20 7c 7c 20 64 62 2d 3e 65 72 72  k(db) || db->err
225c7 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4d 49 53  Code==SQLITE_MIS
225c8 55 53 45 20 29 7b 0a 20 20 20 20 72 65 74 75 72  USE ){.    retur
225c9 6e 20 28 76 6f 69 64 20 2a 29 28 26 6d 69 73 75  n (void *)(&misu
225ca 73 65 42 65 5b 53 51 4c 49 54 45 5f 55 54 46 31  seBe[SQLITE_UTF1
225cb 36 4e 41 54 49 56 45 3d 3d 53 51 4c 49 54 45 5f  6NATIVE==SQLITE_
225cc 55 54 46 31 36 4c 45 3f 31 3a 30 5d 29 3b 0a 20  UTF16LE?1:0]);. 
225cd 20 7d 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33   }.  z = sqlite3
225ce 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 64 62  _value_text16(db
225cf 2d 3e 70 45 72 72 29 3b 0a 20 20 69 66 28 20 7a  ->pErr);.  if( z
225d0 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
225d1 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 64 62  e3ValueSetStr(db
225d2 2d 3e 70 45 72 72 2c 20 2d 31 2c 20 73 71 6c 69  ->pErr, -1, sqli
225d3 74 65 33 45 72 72 53 74 72 28 64 62 2d 3e 65 72  te3ErrStr(db->er
225d4 72 43 6f 64 65 29 2c 0a 20 20 20 20 20 20 20 20  rCode),.        
225d5 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51   SQLITE_UTF8, SQ
225d6 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
225d7 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61    z = sqlite3_va
225d8 6c 75 65 5f 74 65 78 74 31 36 28 64 62 2d 3e 70  lue_text16(db->p
225d9 45 72 72 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  Err);.  }.  sqli
225da 74 65 33 41 70 69 45 78 69 74 28 30 2c 20 30 29  te3ApiExit(0, 0)
225db 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a  ;.  return z;.}.
225dc 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
225dd 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a  _OMIT_UTF16 */..
225de 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
225df 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72   most recent err
225e0 6f 72 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65  or code generate
225e1 64 20 62 79 20 61 6e 20 53 51 4c 69 74 65 20 72  d by an SQLite r
225e2 6f 75 74 69 6e 65 2e 20 49 66 20 4e 55 4c 4c 20  outine. If NULL 
225e3 69 73 0a 2a 2a 20 70 61 73 73 65 64 20 74 6f 20  is.** passed to 
225e4 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 77  this function, w
225e5 65 20 61 73 73 75 6d 65 20 61 20 6d 61 6c 6c 6f  e assume a mallo
225e6 63 28 29 20 66 61 69 6c 65 64 20 64 75 72 69 6e  c() failed durin
225e7 67 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29  g sqlite3_open()
225e8 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
225e9 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 72 72 63  int sqlite3_errc
225ea 6f 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29  ode(sqlite3 *db)
225eb 7b 0a 20 20 69 66 28 20 21 64 62 20 7c 7c 20 73  {.  if( !db || s
225ec 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69 6c  qlite3MallocFail
225ed 65 64 28 29 20 29 7b 0a 20 20 20 20 72 65 74 75  ed() ){.    retu
225ee 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
225ef 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74  .  }.  if( sqlit
225f0 65 33 53 61 66 65 74 79 43 68 65 63 6b 28 64 62  e3SafetyCheck(db
225f1 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
225f2 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
225f3 20 7d 0a 20 20 72 65 74 75 72 6e 20 64 62 2d 3e   }.  return db->
225f4 65 72 72 43 6f 64 65 20 26 20 64 62 2d 3e 65 72  errCode & db->er
225f5 72 4d 61 73 6b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rMask;.}../*.** 
225f6 43 72 65 61 74 65 20 61 20 6e 65 77 20 63 6f 6c  Create a new col
225f7 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  lating function 
225f8 66 6f 72 20 64 61 74 61 62 61 73 65 20 22 64 62  for database "db
225f9 22 2e 20 20 54 68 65 20 6e 61 6d 65 20 69 73 20  ".  The name is 
225fa 7a 4e 61 6d 65 0a 2a 2a 20 61 6e 64 20 74 68 65  zName.** and the
225fb 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 65 6e 63   encoding is enc
225fc 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
225fd 63 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28  createCollation(
225fe 0a 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 20  .  sqlite3* db, 
225ff 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
22600 4e 61 6d 65 2c 20 0a 20 20 69 6e 74 20 65 6e 63  Name, .  int enc
22601 2c 20 0a 20 20 76 6f 69 64 2a 20 70 43 74 78 2c  , .  void* pCtx,
22602 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65  .  int(*xCompare
22603 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  )(void*,int,cons
22604 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  t void*,int,cons
22605 74 20 76 6f 69 64 2a 29 2c 0a 20 20 76 6f 69 64  t void*),.  void
22606 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29  (*xDel)(void*).)
22607 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  {.  CollSeq *pCo
22608 6c 6c 3b 0a 20 20 69 6e 74 20 65 6e 63 32 3b 0a  ll;.  int enc2;.
22609 20 20 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33    .  if( sqlite3
2260a 53 61 66 65 74 79 43 68 65 63 6b 28 64 62 29 20  SafetyCheck(db) 
2260b 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
2260c 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d  LITE_MISUSE;.  }
2260d 0a 0a 20 20 2f 2a 20 49 66 20 53 51 4c 49 54 45  ..  /* If SQLITE
2260e 5f 55 54 46 31 36 20 69 73 20 73 70 65 63 69 66  _UTF16 is specif
2260f 69 65 64 20 61 73 20 74 68 65 20 65 6e 63 6f 64  ied as the encod
22610 69 6e 67 20 74 79 70 65 2c 20 74 72 61 6e 73 66  ing type, transf
22611 6f 72 6d 20 74 68 69 73 0a 20 20 2a 2a 20 74 6f  orm this.  ** to
22612 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 55   one of SQLITE_U
22613 54 46 31 36 4c 45 20 6f 72 20 53 51 4c 49 54 45  TF16LE or SQLITE
22614 5f 55 54 46 31 36 42 45 20 75 73 69 6e 67 20 74  _UTF16BE using t
22615 68 65 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 55  he.  ** SQLITE_U
22616 54 46 31 36 4e 41 54 49 56 45 20 6d 61 63 72 6f  TF16NATIVE macro
22617 2e 20 53 51 4c 49 54 45 5f 55 54 46 31 36 20 69  . SQLITE_UTF16 i
22618 73 20 6e 6f 74 20 75 73 65 64 20 69 6e 74 65 72  s not used inter
22619 6e 61 6c 6c 79 2e 0a 20 20 2a 2f 0a 20 20 65 6e  nally..  */.  en
2261a 63 32 20 3d 20 65 6e 63 20 26 20 7e 53 51 4c 49  c2 = enc & ~SQLI
2261b 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44  TE_UTF16_ALIGNED
2261c 3b 0a 20 20 69 66 28 20 65 6e 63 32 3d 3d 53 51  ;.  if( enc2==SQ
2261d 4c 49 54 45 5f 55 54 46 31 36 20 29 7b 0a 20 20  LITE_UTF16 ){.  
2261e 20 20 65 6e 63 32 20 3d 20 53 51 4c 49 54 45 5f    enc2 = SQLITE_
2261f 55 54 46 31 36 4e 41 54 49 56 45 3b 0a 20 20 7d  UTF16NATIVE;.  }
22620 0a 0a 20 20 69 66 28 20 28 65 6e 63 32 26 7e 33  ..  if( (enc2&~3
22621 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  )!=0 ){.    sqli
22622 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c  te3Error(db, SQL
22623 49 54 45 5f 45 52 52 4f 52 2c 20 22 75 6e 6b 6e  ITE_ERROR, "unkn
22624 6f 77 6e 20 65 6e 63 6f 64 69 6e 67 22 29 3b 0a  own encoding");.
22625 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
22626 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20  E_ERROR;.  }..  
22627 2f 2a 20 43 68 65 63 6b 20 69 66 20 74 68 69 73  /* Check if this
22628 20 63 61 6c 6c 20 69 73 20 72 65 6d 6f 76 69 6e   call is removin
22629 67 20 6f 72 20 72 65 70 6c 61 63 69 6e 67 20 61  g or replacing a
2262a 6e 20 65 78 69 73 74 69 6e 67 20 63 6f 6c 6c 61  n existing colla
2262b 74 69 6f 6e 20 0a 20 20 2a 2a 20 73 65 71 75 65  tion .  ** seque
2262c 6e 63 65 2e 20 49 66 20 73 6f 2c 20 61 6e 64 20  nce. If so, and 
2262d 74 68 65 72 65 20 61 72 65 20 61 63 74 69 76 65  there are active
2262e 20 56 4d 73 2c 20 72 65 74 75 72 6e 20 62 75 73   VMs, return bus
2262f 79 2e 20 49 66 20 74 68 65 72 65 0a 20 20 2a 2a  y. If there.  **
22630 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20 56   are no active V
22631 4d 73 2c 20 69 6e 76 61 6c 69 64 61 74 65 20 61  Ms, invalidate a
22632 6e 79 20 70 72 65 2d 63 6f 6d 70 69 6c 65 64 20  ny pre-compiled 
22633 73 74 61 74 65 6d 65 6e 74 73 2e 0a 20 20 2a 2f  statements..  */
22634 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74  .  pColl = sqlit
22635 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62  e3FindCollSeq(db
22636 2c 20 28 75 38 29 65 6e 63 32 2c 20 7a 4e 61 6d  , (u8)enc2, zNam
22637 65 2c 20 73 74 72 6c 65 6e 28 7a 4e 61 6d 65 29  e, strlen(zName)
22638 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 43 6f 6c  , 0);.  if( pCol
22639 6c 20 26 26 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70  l && pColl->xCmp
2263a 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e   ){.    if( db->
2263b 61 63 74 69 76 65 56 64 62 65 43 6e 74 20 29 7b  activeVdbeCnt ){
2263c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
2263d 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 42  ror(db, SQLITE_B
2263e 55 53 59 2c 20 0a 20 20 20 20 20 20 20 20 22 55  USY, .        "U
2263f 6e 61 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 2f  nable to delete/
22640 6d 6f 64 69 66 79 20 63 6f 6c 6c 61 74 69 6f 6e  modify collation
22641 20 73 65 71 75 65 6e 63 65 20 64 75 65 20 74 6f   sequence due to
22642 20 61 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e   active statemen
22643 74 73 22 29 3b 0a 20 20 20 20 20 20 72 65 74 75  ts");.      retu
22644 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a  rn SQLITE_BUSY;.
22645 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
22646 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53  3ExpirePreparedS
22647 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a 0a  tatements(db);..
22648 20 20 20 20 2f 2a 20 49 66 20 63 6f 6c 6c 61 74      /* If collat
22649 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 70 43 6f  ion sequence pCo
2264a 6c 6c 20 77 61 73 20 63 72 65 61 74 65 64 20 64  ll was created d
2264b 69 72 65 63 74 6c 79 20 62 79 20 61 20 63 61 6c  irectly by a cal
2264c 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c 69  l to.    ** sqli
2264d 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
2264e 74 69 6f 6e 2c 20 61 6e 64 20 6e 6f 74 20 67 65  tion, and not ge
2264f 6e 65 72 61 74 65 64 20 62 79 20 73 79 6e 74 68  nerated by synth
22650 43 6f 6c 6c 53 65 71 28 29 2c 0a 20 20 20 20 2a  CollSeq(),.    *
22651 2a 20 74 68 65 6e 20 61 6e 79 20 63 6f 70 69 65  * then any copie
22652 73 20 6d 61 64 65 20 62 79 20 73 79 6e 74 68 43  s made by synthC
22653 6f 6c 6c 53 65 71 28 29 20 6e 65 65 64 20 74 6f  ollSeq() need to
22654 20 62 65 20 69 6e 76 61 6c 69 64 61 74 65 64 2e   be invalidated.
22655 0a 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 63 6f  .    ** Also, co
22656 6c 6c 61 74 69 6f 6e 20 64 65 73 74 72 75 63 74  llation destruct
22657 6f 72 20 2d 20 43 6f 6c 6c 53 65 71 2e 78 44 65  or - CollSeq.xDe
22658 6c 28 29 20 2d 20 66 75 6e 63 74 69 6f 6e 20 6d  l() - function m
22659 61 79 20 6e 65 65 64 0a 20 20 20 20 2a 2a 20 74  ay need.    ** t
2265a 6f 20 62 65 20 63 61 6c 6c 65 64 2e 0a 20 20 20  o be called..   
2265b 20 2a 2f 20 0a 20 20 20 20 69 66 28 20 28 70 43   */ .    if( (pC
2265c 6f 6c 6c 2d 3e 65 6e 63 20 26 20 7e 53 51 4c 49  oll->enc & ~SQLI
2265d 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44  TE_UTF16_ALIGNED
2265e 29 3d 3d 65 6e 63 32 20 29 7b 0a 20 20 20 20 20  )==enc2 ){.     
2265f 20 43 6f 6c 6c 53 65 71 20 2a 61 43 6f 6c 6c 20   CollSeq *aColl 
22660 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e  = sqlite3HashFin
22661 64 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 2c  d(&db->aCollSeq,
22662 20 7a 4e 61 6d 65 2c 20 73 74 72 6c 65 6e 28 7a   zName, strlen(z
22663 4e 61 6d 65 29 29 3b 0a 20 20 20 20 20 20 69 6e  Name));.      in
22664 74 20 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a  t j;.      for(j
22665 3d 30 3b 20 6a 3c 33 3b 20 6a 2b 2b 29 7b 0a 20  =0; j<3; j++){. 
22666 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a         CollSeq *
22667 70 20 3d 20 26 61 43 6f 6c 6c 5b 6a 5d 3b 0a 20  p = &aColl[j];. 
22668 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 65 6e         if( p->en
22669 63 3d 3d 70 43 6f 6c 6c 2d 3e 65 6e 63 20 29 7b  c==pColl->enc ){
2266a 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70  .          if( p
2266b 2d 3e 78 44 65 6c 20 29 7b 0a 20 20 20 20 20 20  ->xDel ){.      
2266c 20 20 20 20 20 20 70 2d 3e 78 44 65 6c 28 70 2d        p->xDel(p-
2266d 3e 70 55 73 65 72 29 3b 0a 20 20 20 20 20 20 20  >pUser);.       
2266e 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70     }.          p
2266f 2d 3e 78 43 6d 70 20 3d 20 30 3b 0a 20 20 20 20  ->xCmp = 0;.    
22670 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
22671 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 43 6f 6c 6c    }.  }..  pColl
22672 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f   = sqlite3FindCo
22673 6c 6c 53 65 71 28 64 62 2c 20 28 75 38 29 65 6e  llSeq(db, (u8)en
22674 63 32 2c 20 7a 4e 61 6d 65 2c 20 73 74 72 6c 65  c2, zName, strle
22675 6e 28 7a 4e 61 6d 65 29 2c 20 31 29 3b 0a 20 20  n(zName), 1);.  
22676 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20  if( pColl ){.   
22677 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 20 3d 20 78   pColl->xCmp = x
22678 43 6f 6d 70 61 72 65 3b 0a 20 20 20 20 70 43 6f  Compare;.    pCo
22679 6c 6c 2d 3e 70 55 73 65 72 20 3d 20 70 43 74 78  ll->pUser = pCtx
2267a 3b 0a 20 20 20 20 70 43 6f 6c 6c 2d 3e 78 44 65  ;.    pColl->xDe
2267b 6c 20 3d 20 78 44 65 6c 3b 0a 20 20 20 20 70 43  l = xDel;.    pC
2267c 6f 6c 6c 2d 3e 65 6e 63 20 3d 20 65 6e 63 32 20  oll->enc = enc2 
2267d 7c 20 28 65 6e 63 20 26 20 53 51 4c 49 54 45 5f  | (enc & SQLITE_
2267e 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 3b 0a  UTF16_ALIGNED);.
2267f 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 72 72    }.  sqlite3Err
22680 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b  or(db, SQLITE_OK
22681 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  , 0);.  return S
22682 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a  QLITE_OK;.}.../*
22683 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
22684 20 64 6f 65 73 20 74 68 65 20 77 6f 72 6b 20 6f   does the work o
22685 66 20 6f 70 65 6e 69 6e 67 20 61 20 64 61 74 61  f opening a data
22686 62 61 73 65 20 6f 6e 20 62 65 68 61 6c 66 20 6f  base on behalf o
22687 66 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65  f.** sqlite3_ope
22688 6e 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  n() and sqlite3_
22689 6f 70 65 6e 31 36 28 29 2e 20 54 68 65 20 64 61  open16(). The da
2268a 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20  tabase filename 
2268b 22 7a 46 69 6c 65 6e 61 6d 65 22 20 20 0a 2a 2a  "zFilename"  .**
2268c 20 69 73 20 55 54 46 2d 38 20 65 6e 63 6f 64 65   is UTF-8 encode
2268d 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
2268e 20 6f 70 65 6e 44 61 74 61 62 61 73 65 28 0a 20   openDatabase(. 
2268f 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
22690 6c 65 6e 61 6d 65 2c 20 2f 2a 20 44 61 74 61 62  lename, /* Datab
22691 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 55 54 46  ase filename UTF
22692 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20  -8 encoded */.  
22693 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 20 20  sqlite3 **ppDb  
22694 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 52         /* OUT: R
22695 65 74 75 72 6e 65 64 20 64 61 74 61 62 61 73 65  eturned database
22696 20 68 61 6e 64 6c 65 20 2a 2f 0a 29 7b 0a 20 20   handle */.){.  
22697 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 69  sqlite3 *db;.  i
22698 6e 74 20 72 63 3b 0a 20 20 43 6f 6c 6c 53 65 71  nt rc;.  CollSeq
22699 20 2a 70 43 6f 6c 6c 3b 0a 0a 20 20 61 73 73 65   *pColl;..  asse
2269a 72 74 28 20 21 73 71 6c 69 74 65 33 4d 61 6c 6c  rt( !sqlite3Mall
2269b 6f 63 46 61 69 6c 65 64 28 29 20 29 3b 0a 0a 20  ocFailed() );.. 
2269c 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68 65   /* Allocate the
2269d 20 73 71 6c 69 74 65 20 64 61 74 61 20 73 74 72   sqlite data str
2269e 75 63 74 75 72 65 20 2a 2f 0a 20 20 64 62 20 3d  ucture */.  db =
2269f 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 20 73   sqliteMalloc( s
226a0 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 29 20 29  izeof(sqlite3) )
226a1 3b 0a 20 20 69 66 28 20 64 62 3d 3d 30 20 29 20  ;.  if( db==0 ) 
226a2 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b  goto opendb_out;
226a3 0a 20 20 64 62 2d 3e 65 72 72 4d 61 73 6b 20 3d  .  db->errMask =
226a4 20 30 78 66 66 3b 0a 20 20 64 62 2d 3e 70 72 69   0xff;.  db->pri
226a5 6f 72 4e 65 77 52 6f 77 69 64 20 3d 20 30 3b 0a  orNewRowid = 0;.
226a6 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51    db->magic = SQ
226a7 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 3b  LITE_MAGIC_BUSY;
226a8 0a 20 20 64 62 2d 3e 6e 44 62 20 3d 20 32 3b 0a  .  db->nDb = 2;.
226a9 20 20 64 62 2d 3e 61 44 62 20 3d 20 64 62 2d 3e    db->aDb = db->
226aa 61 44 62 53 74 61 74 69 63 3b 0a 20 20 64 62 2d  aDbStatic;.  db-
226ab 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b  >autoCommit = 1;
226ac 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20  .  db->flags |= 
226ad 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c 4e  SQLITE_ShortColN
226ae 61 6d 65 73 0a 23 69 66 20 53 51 4c 49 54 45 5f  ames.#if SQLITE_
226af 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 46 4f 52  DEFAULT_FILE_FOR
226b0 4d 41 54 3c 34 0a 20 20 20 20 20 20 20 20 20 20  MAT<4.          
226b1 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f         | SQLITE_
226b2 4c 65 67 61 63 79 46 69 6c 65 46 6d 74 0a 23 65  LegacyFileFmt.#e
226b3 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49  ndif.#ifdef SQLI
226b4 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 41 44 5f 45  TE_ENABLE_LOAD_E
226b5 58 54 45 4e 53 49 4f 4e 0a 20 20 20 20 20 20 20  XTENSION.       
226b6 20 20 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49            | SQLI
226b7 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e  TE_LoadExtension
226b8 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 3b 0a  .#endif.      ;.
226b9 20 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69    sqlite3HashIni
226ba 74 28 26 64 62 2d 3e 61 46 75 6e 63 2c 20 53 51  t(&db->aFunc, SQ
226bb 4c 49 54 45 5f 48 41 53 48 5f 53 54 52 49 4e 47  LITE_HASH_STRING
226bc 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 48  , 0);.  sqlite3H
226bd 61 73 68 49 6e 69 74 28 26 64 62 2d 3e 61 43 6f  ashInit(&db->aCo
226be 6c 6c 53 65 71 2c 20 53 51 4c 49 54 45 5f 48 41  llSeq, SQLITE_HA
226bf 53 48 5f 53 54 52 49 4e 47 2c 20 30 29 3b 0a 23  SH_STRING, 0);.#
226c0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
226c1 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
226c2 20 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69    sqlite3HashIni
226c3 74 28 26 64 62 2d 3e 61 4d 6f 64 75 6c 65 2c 20  t(&db->aModule, 
226c4 53 51 4c 49 54 45 5f 48 41 53 48 5f 53 54 52 49  SQLITE_HASH_STRI
226c5 4e 47 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 0a  NG, 0);.#endif..
226c6 20 20 2f 2a 20 41 64 64 20 74 68 65 20 64 65 66    /* Add the def
226c7 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  ault collation s
226c8 65 71 75 65 6e 63 65 20 42 49 4e 41 52 59 2e 20  equence BINARY. 
226c9 42 49 4e 41 52 59 20 77 6f 72 6b 73 20 66 6f 72  BINARY works for
226ca 20 62 6f 74 68 20 55 54 46 2d 38 0a 20 20 2a 2a   both UTF-8.  **
226cb 20 61 6e 64 20 55 54 46 2d 31 36 2c 20 73 6f 20   and UTF-16, so 
226cc 61 64 64 20 61 20 76 65 72 73 69 6f 6e 20 66 6f  add a version fo
226cd 72 20 65 61 63 68 20 74 6f 20 61 76 6f 69 64 20  r each to avoid 
226ce 61 6e 79 20 75 6e 6e 65 63 65 73 73 61 72 79 0a  any unnecessary.
226cf 20 20 2a 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 73    ** conversions
226d0 2e 20 54 68 65 20 6f 6e 6c 79 20 65 72 72 6f 72  . The only error
226d1 20 74 68 61 74 20 63 61 6e 20 6f 63 63 75 72 20   that can occur 
226d2 68 65 72 65 20 69 73 20 61 20 6d 61 6c 6c 6f 63  here is a malloc
226d3 28 29 20 66 61 69 6c 75 72 65 2e 0a 20 20 2a 2f  () failure..  */
226d4 0a 20 20 69 66 28 20 63 72 65 61 74 65 43 6f 6c  .  if( createCol
226d5 6c 61 74 69 6f 6e 28 64 62 2c 20 22 42 49 4e 41  lation(db, "BINA
226d6 52 59 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  RY", SQLITE_UTF8
226d7 2c 20 30 2c 20 62 69 6e 43 6f 6c 6c 46 75 6e 63  , 0, binCollFunc
226d8 2c 20 30 29 20 7c 7c 0a 20 20 20 20 20 20 63 72  , 0) ||.      cr
226d9 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62  eateCollation(db
226da 2c 20 22 42 49 4e 41 52 59 22 2c 20 53 51 4c 49  , "BINARY", SQLI
226db 54 45 5f 55 54 46 31 36 42 45 2c 20 30 2c 20 62  TE_UTF16BE, 0, b
226dc 69 6e 43 6f 6c 6c 46 75 6e 63 2c 20 30 29 20 7c  inCollFunc, 0) |
226dd 7c 0a 20 20 20 20 20 20 63 72 65 61 74 65 43 6f  |.      createCo
226de 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 22 42 49 4e  llation(db, "BIN
226df 41 52 59 22 2c 20 53 51 4c 49 54 45 5f 55 54 46  ARY", SQLITE_UTF
226e0 31 36 4c 45 2c 20 30 2c 20 62 69 6e 43 6f 6c 6c  16LE, 0, binColl
226e1 46 75 6e 63 2c 20 30 29 20 7c 7c 0a 20 20 20 20  Func, 0) ||.    
226e2 20 20 28 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c    (db->pDfltColl
226e3 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f   = sqlite3FindCo
226e4 6c 6c 53 65 71 28 64 62 2c 20 53 51 4c 49 54 45  llSeq(db, SQLITE
226e5 5f 55 54 46 38 2c 20 22 42 49 4e 41 52 59 22 2c  _UTF8, "BINARY",
226e6 20 36 2c 20 30 29 29 3d 3d 30 20 0a 20 20 29 7b   6, 0))==0 .  ){
226e7 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c  .    assert( sql
226e8 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69 6c 65 64  ite3MallocFailed
226e9 28 29 20 29 3b 0a 20 20 20 20 64 62 2d 3e 6d 61  () );.    db->ma
226ea 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47  gic = SQLITE_MAG
226eb 49 43 5f 43 4c 4f 53 45 44 3b 0a 20 20 20 20 67  IC_CLOSED;.    g
226ec 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a  oto opendb_out;.
226ed 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 73 6f 20 61    }..  /* Also a
226ee 64 64 20 61 20 55 54 46 2d 38 20 63 61 73 65 2d  dd a UTF-8 case-
226ef 69 6e 73 65 6e 73 69 74 69 76 65 20 63 6f 6c 6c  insensitive coll
226f0 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 2e 20  ation sequence. 
226f1 2a 2f 0a 20 20 63 72 65 61 74 65 43 6f 6c 6c 61  */.  createColla
226f2 74 69 6f 6e 28 64 62 2c 20 22 4e 4f 43 41 53 45  tion(db, "NOCASE
226f3 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  ", SQLITE_UTF8, 
226f4 30 2c 20 6e 6f 63 61 73 65 43 6f 6c 6c 61 74 69  0, nocaseCollati
226f5 6e 67 46 75 6e 63 2c 20 30 29 3b 0a 0a 20 20 2f  ngFunc, 0);..  /
226f6 2a 20 53 65 74 20 66 6c 61 67 73 20 6f 6e 20 74  * Set flags on t
226f7 68 65 20 62 75 69 6c 74 2d 69 6e 20 63 6f 6c 6c  he built-in coll
226f8 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20  ating sequences 
226f9 2a 2f 0a 20 20 64 62 2d 3e 70 44 66 6c 74 43 6f  */.  db->pDfltCo
226fa 6c 6c 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54  ll->type = SQLIT
226fb 45 5f 43 4f 4c 4c 5f 42 49 4e 41 52 59 3b 0a 20  E_COLL_BINARY;. 
226fc 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33   pColl = sqlite3
226fd 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20  FindCollSeq(db, 
226fe 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 22 4e 4f  SQLITE_UTF8, "NO
226ff 43 41 53 45 22 2c 20 36 2c 20 30 29 3b 0a 20 20  CASE", 6, 0);.  
22700 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20  if( pColl ){.   
22701 20 70 43 6f 6c 6c 2d 3e 74 79 70 65 20 3d 20 53   pColl->type = S
22702 51 4c 49 54 45 5f 43 4f 4c 4c 5f 4e 4f 43 41 53  QLITE_COLL_NOCAS
22703 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65  E;.  }..  /* Ope
22704 6e 20 74 68 65 20 62 61 63 6b 65 6e 64 20 64 61  n the backend da
22705 74 61 62 61 73 65 20 64 72 69 76 65 72 20 2a 2f  tabase driver */
22706 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42  .  rc = sqlite3B
22707 74 72 65 65 46 61 63 74 6f 72 79 28 64 62 2c 20  treeFactory(db, 
22708 7a 46 69 6c 65 6e 61 6d 65 2c 20 30 2c 20 53 51  zFilename, 0, SQ
22709 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43  LITE_DEFAULT_CAC
2270a 48 45 5f 53 49 5a 45 2c 0a 20 20 20 20 20 20 20  HE_SIZE,.       
2270b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2270c 20 20 20 20 26 64 62 2d 3e 61 44 62 5b 30 5d 2e      &db->aDb[0].
2270d 70 42 74 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  pBt);.  if( rc!=
2270e 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
2270f 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62   sqlite3Error(db
22710 2c 20 72 63 2c 20 30 29 3b 0a 20 20 20 20 64 62  , rc, 0);.    db
22711 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45  ->magic = SQLITE
22712 5f 4d 41 47 49 43 5f 43 4c 4f 53 45 44 3b 0a 20  _MAGIC_CLOSED;. 
22713 20 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f     goto opendb_o
22714 75 74 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 61 44  ut;.  }.  db->aD
22715 62 5b 30 5d 2e 70 53 63 68 65 6d 61 20 3d 20 73  b[0].pSchema = s
22716 71 6c 69 74 65 33 53 63 68 65 6d 61 47 65 74 28  qlite3SchemaGet(
22717 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 29 3b  db->aDb[0].pBt);
22718 0a 20 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53  .  db->aDb[1].pS
22719 63 68 65 6d 61 20 3d 20 73 71 6c 69 74 65 33 53  chema = sqlite3S
2271a 63 68 65 6d 61 47 65 74 28 30 29 3b 0a 0a 0a 20  chemaGet(0);... 
2271b 20 2f 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20   /* The default 
2271c 73 61 66 65 74 79 5f 6c 65 76 65 6c 20 66 6f 72  safety_level for
2271d 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
2271e 73 65 20 69 73 20 27 66 75 6c 6c 27 3b 20 66 6f  se is 'full'; fo
2271f 72 20 74 68 65 20 74 65 6d 70 0a 20 20 2a 2a 20  r the temp.  ** 
22720 64 61 74 61 62 61 73 65 20 69 74 20 69 73 20 27  database it is '
22721 4e 4f 4e 45 27 2e 20 54 68 69 73 20 6d 61 74 63  NONE'. This matc
22722 68 65 73 20 74 68 65 20 70 61 67 65 72 20 6c 61  hes the pager la
22723 79 65 72 20 64 65 66 61 75 6c 74 73 2e 20 20 0a  yer defaults.  .
22724 20 20 2a 2f 0a 20 20 64 62 2d 3e 61 44 62 5b 30    */.  db->aDb[0
22725 5d 2e 7a 4e 61 6d 65 20 3d 20 22 6d 61 69 6e 22  ].zName = "main"
22726 3b 0a 20 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 73  ;.  db->aDb[0].s
22727 61 66 65 74 79 5f 6c 65 76 65 6c 20 3d 20 33 3b  afety_level = 3;
22728 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
22729 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 20 20 64 62  OMIT_TEMPDB.  db
2272a 2d 3e 61 44 62 5b 31 5d 2e 7a 4e 61 6d 65 20 3d  ->aDb[1].zName =
2272b 20 22 74 65 6d 70 22 3b 0a 20 20 64 62 2d 3e 61   "temp";.  db->a
2272c 44 62 5b 31 5d 2e 73 61 66 65 74 79 5f 6c 65 76  Db[1].safety_lev
2272d 65 6c 20 3d 20 31 3b 0a 23 65 6e 64 69 66 0a 0a  el = 1;.#endif..
2272e 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51    db->magic = SQ
2272f 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 3b  LITE_MAGIC_OPEN;
22730 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 4d 61  .  if( sqlite3Ma
22731 6c 6c 6f 63 46 61 69 6c 65 64 28 29 20 29 7b 0a  llocFailed() ){.
22732 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f      goto opendb_
22733 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52  out;.  }..  /* R
22734 65 67 69 73 74 65 72 20 61 6c 6c 20 62 75 69 6c  egister all buil
22735 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e 73 2c 20  t-in functions, 
22736 62 75 74 20 64 6f 20 6e 6f 74 20 61 74 74 65 6d  but do not attem
22737 70 74 20 74 6f 20 72 65 61 64 20 74 68 65 0a 20  pt to read the. 
22738 20 2a 2a 20 64 61 74 61 62 61 73 65 20 73 63 68   ** database sch
22739 65 6d 61 20 79 65 74 2e 20 54 68 69 73 20 69 73  ema yet. This is
2273a 20 64 65 6c 61 79 65 64 20 75 6e 74 69 6c 20 74   delayed until t
2273b 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68  he first time th
2273c 65 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20  e database.  ** 
2273d 69 73 20 61 63 63 65 73 73 65 64 2e 0a 20 20 2a  is accessed..  *
2273e 2f 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72  /.  sqlite3Error
2273f 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20  (db, SQLITE_OK, 
22740 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 67  0);.  sqlite3Reg
22741 69 73 74 65 72 42 75 69 6c 74 69 6e 46 75 6e 63  isterBuiltinFunc
22742 74 69 6f 6e 73 28 64 62 29 3b 0a 0a 20 20 2f 2a  tions(db);..  /*
22743 20 4c 6f 61 64 20 61 75 74 6f 6d 61 74 69 63 20   Load automatic 
22744 65 78 74 65 6e 73 69 6f 6e 73 20 2d 20 65 78 74  extensions - ext
22745 65 6e 73 69 6f 6e 73 20 74 68 61 74 20 68 61 76  ensions that hav
22746 65 20 62 65 65 6e 20 72 65 67 69 73 74 65 72 65  e been registere
22747 64 0a 20 20 2a 2a 20 75 73 69 6e 67 20 74 68 65  d.  ** using the
22748 20 73 71 6c 69 74 65 33 5f 61 75 74 6f 6d 61 74   sqlite3_automat
22749 69 63 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 41  ic_extension() A
2274a 50 49 2e 0a 20 20 2a 2f 0a 20 20 28 76 6f 69 64  PI..  */.  (void
2274b 29 73 71 6c 69 74 65 33 41 75 74 6f 4c 6f 61 64  )sqlite3AutoLoad
2274c 45 78 74 65 6e 73 69 6f 6e 73 28 64 62 29 3b 0a  Extensions(db);.
2274d 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 65 72    if( sqlite3_er
2274e 72 63 6f 64 65 28 64 62 29 21 3d 53 51 4c 49 54  rcode(db)!=SQLIT
2274f 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f  E_OK ){.    goto
22750 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a 20 20 7d   opendb_out;.  }
22751 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
22752 45 4e 41 42 4c 45 5f 46 54 53 31 0a 20 20 69 66  ENABLE_FTS1.  if
22753 28 20 21 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  ( !sqlite3Malloc
22754 46 61 69 6c 65 64 28 29 20 29 7b 0a 20 20 20 20  Failed() ){.    
22755 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74  extern int sqlit
22756 65 33 46 74 73 31 49 6e 69 74 28 73 71 6c 69 74  e3Fts1Init(sqlit
22757 65 33 2a 29 3b 0a 20 20 20 20 72 63 20 3d 20 73  e3*);.    rc = s
22758 71 6c 69 74 65 33 46 74 73 31 49 6e 69 74 28 64  qlite3Fts1Init(d
22759 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  b);.  }.#endif..
2275a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
2275b 41 42 4c 45 5f 46 54 53 32 0a 20 20 69 66 28 20  ABLE_FTS2.  if( 
2275c 21 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 46 61  !sqlite3MallocFa
2275d 69 6c 65 64 28 29 20 26 26 20 72 63 3d 3d 53 51  iled() && rc==SQ
2275e 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 65  LITE_OK ){.    e
2275f 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65  xtern int sqlite
22760 33 46 74 73 32 49 6e 69 74 28 73 71 6c 69 74 65  3Fts2Init(sqlite
22761 33 2a 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  3*);.    rc = sq
22762 6c 69 74 65 33 46 74 73 32 49 6e 69 74 28 64 62  lite3Fts2Init(db
22763 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23  );.  }.#endif..#
22764 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
22765 42 4c 45 5f 49 43 55 0a 20 20 69 66 28 20 21 73  BLE_ICU.  if( !s
22766 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69 6c  qlite3MallocFail
22767 65 64 28 29 20 26 26 20 72 63 3d 3d 53 51 4c 49  ed() && rc==SQLI
22768 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 65 78 74  TE_OK ){.    ext
22769 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ern int sqlite3I
2276a 63 75 49 6e 69 74 28 73 71 6c 69 74 65 33 2a 29  cuInit(sqlite3*)
2276b 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
2276c 65 33 49 63 75 49 6e 69 74 28 64 62 29 3b 0a 20  e3IcuInit(db);. 
2276d 20 7d 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69   }.#endif.  sqli
2276e 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c  te3Error(db, rc,
2276f 20 30 29 3b 0a 0a 20 20 2f 2a 20 2d 44 53 51 4c   0);..  /* -DSQL
22770 49 54 45 5f 44 45 46 41 55 4c 54 5f 4c 4f 43 4b  ITE_DEFAULT_LOCK
22771 49 4e 47 5f 4d 4f 44 45 3d 31 20 6d 61 6b 65 73  ING_MODE=1 makes
22772 20 45 58 43 4c 55 53 49 56 45 20 74 68 65 20 64   EXCLUSIVE the d
22773 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67 0a 20  efault locking. 
22774 20 2a 2a 20 6d 6f 64 65 2e 20 20 2d 44 53 51 4c   ** mode.  -DSQL
22775 49 54 45 5f 44 45 46 41 55 4c 54 5f 4c 4f 43 4b  ITE_DEFAULT_LOCK
22776 49 4e 47 5f 4d 4f 44 45 3d 30 20 6d 61 6b 65 20  ING_MODE=0 make 
22777 4e 4f 52 4d 41 4c 20 74 68 65 20 64 65 66 61 75  NORMAL the defau
22778 6c 74 20 6c 6f 63 6b 69 6e 67 0a 20 20 2a 2a 20  lt locking.  ** 
22779 6d 6f 64 65 2e 20 20 44 6f 69 6e 67 20 6e 6f 74  mode.  Doing not
2277a 68 69 6e 67 20 61 74 20 61 6c 6c 20 61 6c 73 6f  hing at all also
2277b 20 6d 61 6b 65 73 20 4e 4f 52 4d 41 4c 20 74 68   makes NORMAL th
2277c 65 20 64 65 66 61 75 6c 74 2e 0a 20 20 2a 2f 0a  e default..  */.
2277d 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
2277e 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f  FAULT_LOCKING_MO
2277f 44 45 0a 20 20 64 62 2d 3e 64 66 6c 74 4c 6f 63  DE.  db->dfltLoc
22780 6b 4d 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44  kMode = SQLITE_D
22781 45 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d  EFAULT_LOCKING_M
22782 4f 44 45 3b 0a 20 20 73 71 6c 69 74 65 33 50 61  ODE;.  sqlite3Pa
22783 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 73  gerLockingMode(s
22784 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72  qlite3BtreePager
22785 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 29  (db->aDb[0].pBt)
22786 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
22787 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49              SQLI
22788 54 45 5f 44 45 46 41 55 4c 54 5f 4c 4f 43 4b 49  TE_DEFAULT_LOCKI
22789 4e 47 5f 4d 4f 44 45 29 3b 0a 23 65 6e 64 69 66  NG_MODE);.#endif
2278a 0a 0a 6f 70 65 6e 64 62 5f 6f 75 74 3a 0a 20 20  ..opendb_out:.  
2278b 69 66 28 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  if( SQLITE_NOMEM
2278c 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  ==(rc = sqlite3_
2278d 65 72 72 63 6f 64 65 28 64 62 29 29 20 29 7b 0a  errcode(db)) ){.
2278e 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73      sqlite3_clos
2278f 65 28 64 62 29 3b 0a 20 20 20 20 64 62 20 3d 20  e(db);.    db = 
22790 30 3b 0a 20 20 7d 0a 20 20 2a 70 70 44 62 20 3d  0;.  }.  *ppDb =
22791 20 64 62 3b 0a 20 20 72 65 74 75 72 6e 20 73 71   db;.  return sq
22792 6c 69 74 65 33 41 70 69 45 78 69 74 28 30 2c 20  lite3ApiExit(0, 
22793 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70  rc);.}../*.** Op
22794 65 6e 20 61 20 6e 65 77 20 64 61 74 61 62 61 73  en a new databas
22795 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c  e handle..*/.SQL
22796 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
22797 74 65 33 5f 6f 70 65 6e 28 0a 20 20 63 6f 6e 73  te3_open(.  cons
22798 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d  t char *zFilenam
22799 65 2c 20 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a  e, .  sqlite3 **
2279a 70 70 44 62 20 0a 29 7b 0a 20 20 72 65 74 75 72  ppDb .){.  retur
2279b 6e 20 6f 70 65 6e 44 61 74 61 62 61 73 65 28 7a  n openDatabase(z
2279c 46 69 6c 65 6e 61 6d 65 2c 20 70 70 44 62 29 3b  Filename, ppDb);
2279d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
2279e 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a  TE_OMIT_UTF16./*
2279f 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e 65 77 20 64  .** Open a new d
227a0 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 0a  atabase handle..
227a1 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
227a2 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36  t sqlite3_open16
227a3 28 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  (.  const void *
227a4 7a 46 69 6c 65 6e 61 6d 65 2c 20 0a 20 20 73 71  zFilename, .  sq
227a5 6c 69 74 65 33 20 2a 2a 70 70 44 62 0a 29 7b 0a  lite3 **ppDb.){.
227a6 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 46    char const *zF
227a7 69 6c 65 6e 61 6d 65 38 3b 20 20 20 2f 2a 20 7a  ilename8;   /* z
227a8 46 69 6c 65 6e 61 6d 65 20 65 6e 63 6f 64 65 64  Filename encoded
227a9 20 69 6e 20 55 54 46 2d 38 20 69 6e 73 74 65 61   in UTF-8 instea
227aa 64 20 6f 66 20 55 54 46 2d 31 36 20 2a 2f 0a 20  d of UTF-16 */. 
227ab 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
227ac 5f 4f 4b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76  _OK;.  sqlite3_v
227ad 61 6c 75 65 20 2a 70 56 61 6c 3b 0a 0a 20 20 61  alue *pVal;..  a
227ae 73 73 65 72 74 28 20 7a 46 69 6c 65 6e 61 6d 65  ssert( zFilename
227af 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 70   );.  assert( pp
227b0 44 62 20 29 3b 0a 20 20 2a 70 70 44 62 20 3d 20  Db );.  *ppDb = 
227b1 30 3b 0a 20 20 70 56 61 6c 20 3d 20 73 71 6c 69  0;.  pVal = sqli
227b2 74 65 33 56 61 6c 75 65 4e 65 77 28 29 3b 0a 20  te3ValueNew();. 
227b3 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74   sqlite3ValueSet
227b4 53 74 72 28 70 56 61 6c 2c 20 2d 31 2c 20 7a 46  Str(pVal, -1, zF
227b5 69 6c 65 6e 61 6d 65 2c 20 53 51 4c 49 54 45 5f  ilename, SQLITE_
227b6 55 54 46 31 36 4e 41 54 49 56 45 2c 20 53 51 4c  UTF16NATIVE, SQL
227b7 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7a  ITE_STATIC);.  z
227b8 46 69 6c 65 6e 61 6d 65 38 20 3d 20 73 71 6c 69  Filename8 = sqli
227b9 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 61  te3ValueText(pVa
227ba 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b  l, SQLITE_UTF8);
227bb 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65  .  if( zFilename
227bc 38 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 6f 70  8 ){.    rc = op
227bd 65 6e 44 61 74 61 62 61 73 65 28 7a 46 69 6c 65  enDatabase(zFile
227be 6e 61 6d 65 38 2c 20 70 70 44 62 29 3b 0a 20 20  name8, ppDb);.  
227bf 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
227c0 5f 4f 4b 20 26 26 20 2a 70 70 44 62 20 29 7b 0a  _OK && *ppDb ){.
227c1 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
227c2 65 33 5f 65 78 65 63 28 2a 70 70 44 62 2c 20 22  e3_exec(*ppDb, "
227c3 50 52 41 47 4d 41 20 65 6e 63 6f 64 69 6e 67 20  PRAGMA encoding 
227c4 3d 20 27 55 54 46 2d 31 36 27 22 2c 20 30 2c 20  = 'UTF-16'", 0, 
227c5 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28  0, 0);.      if(
227c6 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
227c7 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
227c8 33 5f 63 6c 6f 73 65 28 2a 70 70 44 62 29 3b 0a  3_close(*ppDb);.
227c9 20 20 20 20 20 20 20 20 2a 70 70 44 62 20 3d 20          *ppDb = 
227ca 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  0;.      }.    }
227cb 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 61  .  }.  sqlite3Va
227cc 6c 75 65 46 72 65 65 28 70 56 61 6c 29 3b 0a 0a  lueFree(pVal);..
227cd 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
227ce 41 70 69 45 78 69 74 28 30 2c 20 72 63 29 3b 0a  ApiExit(0, rc);.
227cf 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
227d0 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f  TE_OMIT_UTF16 */
227d1 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
227d2 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 64 65  owing routine de
227d3 73 74 72 6f 79 73 20 61 20 76 69 72 74 75 61 6c  stroys a virtual
227d4 20 6d 61 63 68 69 6e 65 20 74 68 61 74 20 69 73   machine that is
227d5 20 63 72 65 61 74 65 64 20 62 79 0a 2a 2a 20 74   created by.** t
227d6 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 69  he sqlite3_compi
227d7 6c 65 28 29 20 72 6f 75 74 69 6e 65 2e 20 54 68  le() routine. Th
227d8 65 20 69 6e 74 65 67 65 72 20 72 65 74 75 72 6e  e integer return
227d9 65 64 20 69 73 20 61 6e 20 53 51 4c 49 54 45 5f  ed is an SQLITE_
227da 0a 2a 2a 20 73 75 63 63 65 73 73 2f 66 61 69 6c  .** success/fail
227db 75 72 65 20 63 6f 64 65 20 74 68 61 74 20 64 65  ure code that de
227dc 73 63 72 69 62 65 73 20 74 68 65 20 72 65 73 75  scribes the resu
227dd 6c 74 20 6f 66 20 65 78 65 63 75 74 69 6e 67 20  lt of executing 
227de 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a 20 6d  the virtual.** m
227df 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  achine..**.** Th
227e0 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20  is routine sets 
227e1 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 61  the error code a
227e2 6e 64 20 73 74 72 69 6e 67 20 72 65 74 75 72 6e  nd string return
227e3 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33  ed by.** sqlite3
227e4 5f 65 72 72 63 6f 64 65 28 29 2c 20 73 71 6c 69  _errcode(), sqli
227e5 74 65 33 5f 65 72 72 6d 73 67 28 29 20 61 6e 64  te3_errmsg() and
227e6 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31   sqlite3_errmsg1
227e7 36 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  6()..*/.SQLITE_A
227e8 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66  PI int sqlite3_f
227e9 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f  inalize(sqlite3_
227ea 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20  stmt *pStmt){.  
227eb 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 70 53  int rc;.  if( pS
227ec 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63  tmt==0 ){.    rc
227ed 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
227ee 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
227ef 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c  sqlite3VdbeFinal
227f0 69 7a 65 28 28 56 64 62 65 2a 29 70 53 74 6d 74  ize((Vdbe*)pStmt
227f1 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
227f2 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65 72  rc;.}../*.** Ter
227f3 6d 69 6e 61 74 65 20 74 68 65 20 63 75 72 72 65  minate the curre
227f4 6e 74 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20  nt execution of 
227f5 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  an SQL statement
227f6 20 61 6e 64 20 72 65 73 65 74 20 69 74 0a 2a 2a   and reset it.**
227f7 20 62 61 63 6b 20 74 6f 20 69 74 73 20 73 74 61   back to its sta
227f8 72 74 69 6e 67 20 73 74 61 74 65 20 73 6f 20 74  rting state so t
227f9 68 61 74 20 69 74 20 63 61 6e 20 62 65 20 72 65  hat it can be re
227fa 75 73 65 64 2e 20 41 20 73 75 63 63 65 73 73 20  used. A success 
227fb 63 6f 64 65 20 66 72 6f 6d 0a 2a 2a 20 74 68 65  code from.** the
227fc 20 70 72 69 6f 72 20 65 78 65 63 75 74 69 6f 6e   prior execution
227fd 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
227fe 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
227ff 20 73 65 74 73 20 74 68 65 20 65 72 72 6f 72 20   sets the error 
22800 63 6f 64 65 20 61 6e 64 20 73 74 72 69 6e 67 20  code and string 
22801 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73  returned by.** s
22802 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29  qlite3_errcode()
22803 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67  , sqlite3_errmsg
22804 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 65  () and sqlite3_e
22805 72 72 6d 73 67 31 36 28 29 2e 0a 2a 2f 0a 53 51  rrmsg16()..*/.SQ
22806 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
22807 69 74 65 33 5f 72 65 73 65 74 28 73 71 6c 69 74  ite3_reset(sqlit
22808 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b  e3_stmt *pStmt){
22809 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28  .  int rc;.  if(
2280a 20 70 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20   pStmt==0 ){.   
2280b 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
2280c 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63  .  }else{.    rc
2280d 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65   = sqlite3VdbeRe
2280e 73 65 74 28 28 56 64 62 65 2a 29 70 53 74 6d 74  set((Vdbe*)pStmt
2280f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
22810 62 65 4d 61 6b 65 52 65 61 64 79 28 28 56 64 62  beMakeReady((Vdb
22811 65 2a 29 70 53 74 6d 74 2c 20 2d 31 2c 20 30 2c  e*)pStmt, -1, 0,
22812 20 30 2c 20 30 29 3b 0a 20 20 20 20 61 73 73 65   0, 0);.    asse
22813 72 74 28 20 28 72 63 20 26 20 28 73 71 6c 69 74  rt( (rc & (sqlit
22814 65 33 5f 64 62 5f 68 61 6e 64 6c 65 28 70 53 74  e3_db_handle(pSt
22815 6d 74 29 2d 3e 65 72 72 4d 61 73 6b 29 29 3d 3d  mt)->errMask))==
22816 72 63 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rc );.  }.  retu
22817 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
22818 52 65 67 69 73 74 65 72 20 61 20 6e 65 77 20 63  Register a new c
22819 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
2281a 65 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62  e with the datab
2281b 61 73 65 20 68 61 6e 64 6c 65 20 64 62 2e 0a 2a  ase handle db..*
2281c 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72  /.int sqlite3_cr
2281d 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 0a  eate_collation(.
2281e 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 0a    sqlite3* db, .
2281f 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
22820 61 6d 65 2c 20 0a 20 20 69 6e 74 20 65 6e 63 2c  ame, .  int enc,
22821 20 0a 20 20 76 6f 69 64 2a 20 70 43 74 78 2c 0a   .  void* pCtx,.
22822 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29    int(*xCompare)
22823 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74  (void*,int,const
22824 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74   void*,int,const
22825 20 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 69 6e 74   void*).){.  int
22826 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 21   rc;.  assert( !
22827 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 46 61 69  sqlite3MallocFai
22828 6c 65 64 28 29 20 29 3b 0a 20 20 72 63 20 3d 20  led() );.  rc = 
22829 63 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28  createCollation(
2282a 64 62 2c 20 7a 4e 61 6d 65 2c 20 65 6e 63 2c 20  db, zName, enc, 
2282b 70 43 74 78 2c 20 78 43 6f 6d 70 61 72 65 2c 20  pCtx, xCompare, 
2282c 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  0);.  return sql
2282d 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20  ite3ApiExit(db, 
2282e 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  rc);.}../*.** Re
2282f 67 69 73 74 65 72 20 61 20 6e 65 77 20 63 6f 6c  gister a new col
22830 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
22831 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61 73  with the databas
22832 65 20 68 61 6e 64 6c 65 20 64 62 2e 0a 2a 2f 0a  e handle db..*/.
22833 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61  int sqlite3_crea
22834 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28  te_collation_v2(
22835 0a 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 20  .  sqlite3* db, 
22836 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
22837 4e 61 6d 65 2c 20 0a 20 20 69 6e 74 20 65 6e 63  Name, .  int enc
22838 2c 20 0a 20 20 76 6f 69 64 2a 20 70 43 74 78 2c  , .  void* pCtx,
22839 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65  .  int(*xCompare
2283a 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  )(void*,int,cons
2283b 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  t void*,int,cons
2283c 74 20 76 6f 69 64 2a 29 2c 0a 20 20 76 6f 69 64  t void*),.  void
2283d 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29  (*xDel)(void*).)
2283e 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73  {.  int rc;.  as
2283f 73 65 72 74 28 20 21 73 71 6c 69 74 65 33 4d 61  sert( !sqlite3Ma
22840 6c 6c 6f 63 46 61 69 6c 65 64 28 29 20 29 3b 0a  llocFailed() );.
22841 20 20 72 63 20 3d 20 63 72 65 61 74 65 43 6f 6c    rc = createCol
22842 6c 61 74 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65  lation(db, zName
22843 2c 20 65 6e 63 2c 20 70 43 74 78 2c 20 78 43 6f  , enc, pCtx, xCo
22844 6d 70 61 72 65 2c 20 78 44 65 6c 29 3b 0a 20 20  mpare, xDel);.  
22845 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41 70  return sqlite3Ap
22846 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 7d  iExit(db, rc);.}
22847 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
22848 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a  _OMIT_UTF16./*.*
22849 2a 20 52 65 67 69 73 74 65 72 20 61 20 6e 65 77  * Register a new
2284a 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
2284b 6e 63 65 20 77 69 74 68 20 74 68 65 20 64 61 74  nce with the dat
2284c 61 62 61 73 65 20 68 61 6e 64 6c 65 20 64 62 2e  abase handle db.
2284d 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f  .*/.int sqlite3_
2284e 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
2284f 31 36 28 0a 20 20 73 71 6c 69 74 65 33 2a 20 64  16(.  sqlite3* d
22850 62 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  b, .  const char
22851 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20 69 6e 74 20   *zName, .  int 
22852 65 6e 63 2c 20 0a 20 20 76 6f 69 64 2a 20 70 43  enc, .  void* pC
22853 74 78 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70  tx,.  int(*xComp
22854 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63  are)(void*,int,c
22855 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63  onst void*,int,c
22856 6f 6e 73 74 20 76 6f 69 64 2a 29 0a 29 7b 0a 20  onst void*).){. 
22857 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
22858 5f 4f 4b 3b 0a 20 20 63 68 61 72 20 2a 7a 4e 61  _OK;.  char *zNa
22859 6d 65 38 3b 20 0a 20 20 61 73 73 65 72 74 28 20  me8; .  assert( 
2285a 21 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 46 61  !sqlite3MallocFa
2285b 69 6c 65 64 28 29 20 29 3b 0a 20 20 7a 4e 61 6d  iled() );.  zNam
2285c 65 38 20 3d 20 73 71 6c 69 74 65 33 55 74 66 31  e8 = sqlite3Utf1
2285d 36 74 6f 38 28 7a 4e 61 6d 65 2c 20 2d 31 29 3b  6to8(zName, -1);
2285e 0a 20 20 69 66 28 20 7a 4e 61 6d 65 38 20 29 7b  .  if( zName8 ){
2285f 0a 20 20 20 20 72 63 20 3d 20 63 72 65 61 74 65  .    rc = create
22860 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 7a 4e  Collation(db, zN
22861 61 6d 65 38 2c 20 65 6e 63 2c 20 70 43 74 78 2c  ame8, enc, pCtx,
22862 20 78 43 6f 6d 70 61 72 65 2c 20 30 29 3b 0a 20   xCompare, 0);. 
22863 20 20 20 73 71 6c 69 74 65 46 72 65 65 28 7a 4e     sqliteFree(zN
22864 61 6d 65 38 29 3b 0a 20 20 7d 0a 20 20 72 65 74  ame8);.  }.  ret
22865 75 72 6e 20 73 71 6c 69 74 65 33 41 70 69 45 78  urn sqlite3ApiEx
22866 69 74 28 64 62 2c 20 72 63 29 3b 0a 7d 0a 23 65  it(db, rc);.}.#e
22867 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
22868 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a  MIT_UTF16 */../*
22869 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 63  .** Register a c
2286a 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
2286b 65 20 66 61 63 74 6f 72 79 20 63 61 6c 6c 62 61  e factory callba
2286c 63 6b 20 77 69 74 68 20 74 68 65 20 64 61 74 61  ck with the data
2286d 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a 2a 20 64  base handle.** d
2286e 62 2e 20 52 65 70 6c 61 63 65 20 61 6e 79 20 70  b. Replace any p
2286f 72 65 76 69 6f 75 73 6c 79 20 69 6e 73 74 61 6c  reviously instal
22870 6c 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  led collation se
22871 71 75 65 6e 63 65 20 66 61 63 74 6f 72 79 2e 0a  quence factory..
22872 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  */.int sqlite3_c
22873 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28  ollation_needed(
22874 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
22875 0a 20 20 76 6f 69 64 20 2a 70 43 6f 6c 6c 4e 65  .  void *pCollNe
22876 65 64 65 64 41 72 67 2c 20 0a 20 20 76 6f 69 64  ededArg, .  void
22877 28 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 29 28 76  (*xCollNeeded)(v
22878 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e  oid*,sqlite3*,in
22879 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74  t eTextRep,const
2287a 20 63 68 61 72 2a 29 0a 29 7b 0a 20 20 69 66 28   char*).){.  if(
2287b 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68   sqlite3SafetyCh
2287c 65 63 6b 28 64 62 29 20 29 7b 0a 20 20 20 20 72  eck(db) ){.    r
2287d 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53  eturn SQLITE_MIS
2287e 55 53 45 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 78  USE;.  }.  db->x
2287f 43 6f 6c 6c 4e 65 65 64 65 64 20 3d 20 78 43 6f  CollNeeded = xCo
22880 6c 6c 4e 65 65 64 65 64 3b 0a 20 20 64 62 2d 3e  llNeeded;.  db->
22881 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 20 3d 20  xCollNeeded16 = 
22882 30 3b 0a 20 20 64 62 2d 3e 70 43 6f 6c 6c 4e 65  0;.  db->pCollNe
22883 65 64 65 64 41 72 67 20 3d 20 70 43 6f 6c 6c 4e  ededArg = pCollN
22884 65 65 64 65 64 41 72 67 3b 0a 20 20 72 65 74 75  eededArg;.  retu
22885 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
22886 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
22887 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a  OMIT_UTF16./*.**
22888 20 52 65 67 69 73 74 65 72 20 61 20 63 6f 6c 6c   Register a coll
22889 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66  ation sequence f
2288a 61 63 74 6f 72 79 20 63 61 6c 6c 62 61 63 6b 20  actory callback 
2288b 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61 73  with the databas
2288c 65 20 68 61 6e 64 6c 65 0a 2a 2a 20 64 62 2e 20  e handle.** db. 
2288d 52 65 70 6c 61 63 65 20 61 6e 79 20 70 72 65 76  Replace any prev
2288e 69 6f 75 73 6c 79 20 69 6e 73 74 61 6c 6c 65 64  iously installed
2288f 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
22890 6e 63 65 20 66 61 63 74 6f 72 79 2e 0a 2a 2f 0a  nce factory..*/.
22891 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c  int sqlite3_coll
22892 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28 0a  ation_needed16(.
22893 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a    sqlite3 *db, .
22894 20 20 76 6f 69 64 20 2a 70 43 6f 6c 6c 4e 65 65    void *pCollNee
22895 64 65 64 41 72 67 2c 20 0a 20 20 76 6f 69 64 28  dedArg, .  void(
22896 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 29 28  *xCollNeeded16)(
22897 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69  void*,sqlite3*,i
22898 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73  nt eTextRep,cons
22899 74 20 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 69 66  t void*).){.  if
2289a 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43  ( sqlite3SafetyC
2289b 68 65 63 6b 28 64 62 29 20 29 7b 0a 20 20 20 20  heck(db) ){.    
2289c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
2289d 53 55 53 45 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e  SUSE;.  }.  db->
2289e 78 43 6f 6c 6c 4e 65 65 64 65 64 20 3d 20 30 3b  xCollNeeded = 0;
2289f 0a 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64  .  db->xCollNeed
228a0 65 64 31 36 20 3d 20 78 43 6f 6c 6c 4e 65 65 64  ed16 = xCollNeed
228a1 65 64 31 36 3b 0a 20 20 64 62 2d 3e 70 43 6f 6c  ed16;.  db->pCol
228a2 6c 4e 65 65 64 65 64 41 72 67 20 3d 20 70 43 6f  lNeededArg = pCo
228a3 6c 6c 4e 65 65 64 65 64 41 72 67 3b 0a 20 20 72  llNeededArg;.  r
228a4 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
228a5 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
228a6 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a  ITE_OMIT_UTF16 *
228a7 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
228a8 45 5f 4f 4d 49 54 5f 47 4c 4f 42 41 4c 52 45 43  E_OMIT_GLOBALREC
228a9 4f 56 45 52 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  OVER./*.** This 
228aa 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 77 20  function is now 
228ab 61 6e 20 61 6e 61 63 68 72 6f 6e 69 73 6d 2e 20  an anachronism. 
228ac 49 74 20 75 73 65 64 20 74 6f 20 62 65 20 75 73  It used to be us
228ad 65 64 20 74 6f 20 72 65 63 6f 76 65 72 20 66 72  ed to recover fr
228ae 6f 6d 20 61 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29  om a.** malloc()
228af 20 66 61 69 6c 75 72 65 2c 20 62 75 74 20 53 51   failure, but SQ
228b0 4c 69 74 65 20 6e 6f 77 20 64 6f 65 73 20 74 68  Lite now does th
228b1 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  is automatically
228b2 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33  ..*/.int sqlite3
228b3 5f 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72 28  _global_recover(
228b4 29 7b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  ){.  return SQLI
228b5 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a  TE_OK;.}.#endif.
228b6 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 74 6f 20 73  ./*.** Test to s
228b7 65 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  ee whether or no
228b8 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  t the database c
228b9 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 69 6e 20  onnection is in 
228ba 61 75 74 6f 63 6f 6d 6d 69 74 0a 2a 2a 20 6d 6f  autocommit.** mo
228bb 64 65 2e 20 20 52 65 74 75 72 6e 20 54 52 55 45  de.  Return TRUE
228bc 20 69 66 20 69 74 20 69 73 20 61 6e 64 20 46 41   if it is and FA
228bd 4c 53 45 20 69 66 20 6e 6f 74 2e 20 20 41 75 74  LSE if not.  Aut
228be 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20  ocommit mode is 
228bf 6f 6e 0a 2a 2a 20 62 79 20 64 65 66 61 75 6c 74  on.** by default
228c0 2e 20 20 41 75 74 6f 63 6f 6d 6d 69 74 20 69 73  .  Autocommit is
228c1 20 64 69 73 61 62 6c 65 64 20 62 79 20 61 20 42   disabled by a B
228c2 45 47 49 4e 20 73 74 61 74 65 6d 65 6e 74 20 61  EGIN statement a
228c3 6e 64 20 72 65 65 6e 61 62 6c 65 64 0a 2a 2a 20  nd reenabled.** 
228c4 62 79 20 74 68 65 20 6e 65 78 74 20 43 4f 4d 4d  by the next COMM
228c5 49 54 20 6f 72 20 52 4f 4c 4c 42 41 43 4b 2e 0a  IT or ROLLBACK..
228c6 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 20 54 48 49 53 20  **.******* THIS 
228c7 49 53 20 41 4e 20 45 58 50 45 52 49 4d 45 4e 54  IS AN EXPERIMENT
228c8 41 4c 20 41 50 49 20 41 4e 44 20 49 53 20 53 55  AL API AND IS SU
228c9 42 4a 45 43 54 20 54 4f 20 43 48 41 4e 47 45 20  BJECT TO CHANGE 
228ca 2a 2a 2a 2a 2a 2a 0a 2a 2f 0a 69 6e 74 20 73 71  ******.*/.int sq
228cb 6c 69 74 65 33 5f 67 65 74 5f 61 75 74 6f 63 6f  lite3_get_autoco
228cc 6d 6d 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62  mmit(sqlite3 *db
228cd 29 7b 0a 20 20 72 65 74 75 72 6e 20 64 62 2d 3e  ){.  return db->
228ce 61 75 74 6f 43 6f 6d 6d 69 74 3b 0a 7d 0a 0a 23  autoCommit;.}..#
228cf 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
228d0 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  UG./*.** The fol
228d1 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 69  lowing routine i
228d2 73 20 73 75 62 74 69 74 75 74 65 64 20 66 6f 72  s subtituted for
228d3 20 63 6f 6e 73 74 61 6e 74 20 53 51 4c 49 54 45   constant SQLITE
228d4 5f 43 4f 52 52 55 50 54 20 69 6e 0a 2a 2a 20 64  _CORRUPT in.** d
228d5 65 62 75 67 67 69 6e 67 20 62 75 69 6c 64 73 2e  ebugging builds.
228d6 20 20 54 68 69 73 20 70 72 6f 76 69 64 65 73 20    This provides 
228d7 61 20 77 61 79 20 74 6f 20 73 65 74 20 61 20 62  a way to set a b
228d8 72 65 61 6b 70 6f 69 6e 74 20 66 6f 72 20 77 68  reakpoint for wh
228d9 65 6e 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e  en.** corruption
228da 20 69 73 20 66 69 72 73 74 20 64 65 74 65 63 74   is first detect
228db 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
228dc 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
228dd 33 43 6f 72 72 75 70 74 28 76 6f 69 64 29 7b 0a  3Corrupt(void){.
228de 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
228df 43 4f 52 52 55 50 54 3b 0a 7d 0a 23 65 6e 64 69  CORRUPT;.}.#endi
228e0 66 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  f...#ifndef SQLI
228e1 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
228e2 41 43 48 45 0a 2f 2a 0a 2a 2a 20 45 6e 61 62 6c  ACHE./*.** Enabl
228e3 65 20 6f 72 20 64 69 73 61 62 6c 65 20 74 68 65  e or disable the
228e4 20 73 68 61 72 65 64 20 70 61 67 65 72 20 61 6e   shared pager an
228e5 64 20 73 63 68 65 6d 61 20 66 65 61 74 75 72 65  d schema feature
228e6 73 20 66 6f 72 20 74 68 65 0a 2a 2a 20 63 75 72  s for the.** cur
228e7 72 65 6e 74 20 74 68 72 65 61 64 2e 0a 2a 2a 0a  rent thread..**.
228e8 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
228e9 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 20 63  should only be c
228ea 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 72 65  alled when there
228eb 20 61 72 65 20 6e 6f 20 6f 70 65 6e 0a 2a 2a 20   are no open.** 
228ec 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
228ed 69 6f 6e 73 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c  ions..*/.int sql
228ee 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72  ite3_enable_shar
228ef 65 64 5f 63 61 63 68 65 28 69 6e 74 20 65 6e 61  ed_cache(int ena
228f0 62 6c 65 29 7b 0a 20 20 54 68 72 65 61 64 44 61  ble){.  ThreadDa
228f1 74 61 20 2a 70 54 64 20 3d 20 73 71 6c 69 74 65  ta *pTd = sqlite
228f2 33 54 68 72 65 61 64 44 61 74 61 28 29 3b 0a 20  3ThreadData();. 
228f3 20 69 66 28 20 70 54 64 20 29 7b 0a 20 20 20 20   if( pTd ){.    
228f4 2f 2a 20 49 74 20 69 73 20 6f 6e 6c 79 20 6c 65  /* It is only le
228f5 67 61 6c 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69  gal to call sqli
228f6 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65  te3_enable_share
228f7 64 5f 63 61 63 68 65 28 29 20 77 68 65 6e 20 74  d_cache() when t
228f8 68 65 72 65 0a 20 20 20 20 2a 2a 20 61 72 65 20  here.    ** are 
228f9 6e 6f 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 65  no currently ope
228fa 6e 20 62 2d 74 72 65 65 73 20 74 68 61 74 20 77  n b-trees that w
228fb 65 72 65 20 6f 70 65 6e 65 64 20 62 79 20 74 68  ere opened by th
228fc 65 20 63 61 6c 6c 69 6e 67 20 74 68 72 65 61 64  e calling thread
228fd 2e 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 63 6f  ..    ** This co
228fe 6e 64 69 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20  ndition is only 
228ff 65 61 73 79 20 74 6f 20 64 65 74 65 63 74 20 69  easy to detect i
22900 66 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 63  f the shared-cac
22901 68 65 20 77 65 72 65 20 0a 20 20 20 20 2a 2a 20  he were .    ** 
22902 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 61 62 6c  previously enabl
22903 65 64 20 28 61 6e 64 20 69 73 20 62 65 69 6e 67  ed (and is being
22904 20 64 69 73 61 62 6c 65 64 29 2e 20 0a 20 20 20   disabled). .   
22905 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 54 64 2d   */.    if( pTd-
22906 3e 70 42 74 72 65 65 20 26 26 20 21 65 6e 61 62  >pBtree && !enab
22907 6c 65 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  le ){.      asse
22908 72 74 28 20 70 54 64 2d 3e 75 73 65 53 68 61 72  rt( pTd->useShar
22909 65 64 44 61 74 61 20 29 3b 0a 20 20 20 20 20 20  edData );.      
2290a 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
2290b 53 55 53 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  SUSE;.    }..   
2290c 20 70 54 64 2d 3e 75 73 65 53 68 61 72 65 64 44   pTd->useSharedD
2290d 61 74 61 20 3d 20 65 6e 61 62 6c 65 3b 0a 20 20  ata = enable;.  
2290e 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
2290f 54 68 72 65 61 64 44 61 74 61 28 29 3b 0a 20 20  ThreadData();.  
22910 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  }.  return sqlit
22911 65 33 41 70 69 45 78 69 74 28 30 2c 20 53 51 4c  e3ApiExit(0, SQL
22912 49 54 45 5f 4f 4b 29 3b 0a 7d 0a 23 65 6e 64 69  ITE_OK);.}.#endi
22913 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73  f../*.** This is
22914 20 61 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 72   a convenience r
22915 6f 75 74 69 6e 65 20 74 68 61 74 20 6d 61 6b 65  outine that make
22916 73 20 73 75 72 65 20 74 68 61 74 20 61 6c 6c 20  s sure that all 
22917 74 68 72 65 61 64 2d 73 70 65 63 69 66 69 63 0a  thread-specific.
22918 2a 2a 20 64 61 74 61 20 66 6f 72 20 74 68 69 73  ** data for this
22919 20 74 68 72 65 61 64 20 68 61 73 20 62 65 65 6e   thread has been
2291a 20 64 65 61 6c 6c 6f 63 61 74 65 64 2e 0a 2a 2f   deallocated..*/
2291b 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 74 68  .void sqlite3_th
2291c 72 65 61 64 5f 63 6c 65 61 6e 75 70 28 76 6f 69  read_cleanup(voi
2291d 64 29 7b 0a 20 20 54 68 72 65 61 64 44 61 74 61  d){.  ThreadData
2291e 20 2a 70 54 64 20 3d 20 73 71 6c 69 74 65 33 4f   *pTd = sqlite3O
2291f 73 54 68 72 65 61 64 53 70 65 63 69 66 69 63 44  sThreadSpecificD
22920 61 74 61 28 30 29 3b 0a 20 20 69 66 28 20 70 54  ata(0);.  if( pT
22921 64 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28  d ){.    memset(
22922 70 54 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a  pTd, 0, sizeof(*
22923 70 54 64 29 29 3b 0a 20 20 20 20 73 71 6c 69 74  pTd));.    sqlit
22924 65 33 4f 73 54 68 72 65 61 64 53 70 65 63 69 66  e3OsThreadSpecif
22925 69 63 44 61 74 61 28 2d 31 29 3b 0a 20 20 7d 0a  icData(-1);.  }.
22926 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
22927 6d 65 74 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  meta information
22928 20 61 62 6f 75 74 20 61 20 73 70 65 63 69 66 69   about a specifi
22929 63 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 64 61  c column of a da
2292a 74 61 62 61 73 65 20 74 61 62 6c 65 2e 0a 2a 2a  tabase table..**
2292b 20 53 65 65 20 63 6f 6d 6d 65 6e 74 20 69 6e 20   See comment in 
2292c 73 71 6c 69 74 65 33 2e 68 20 28 73 71 6c 69 74  sqlite3.h (sqlit
2292d 65 2e 68 2e 69 6e 29 20 66 6f 72 20 64 65 74 61  e.h.in) for deta
2292e 69 6c 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  ils..*/.#ifdef S
2292f 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c  QLITE_ENABLE_COL
22930 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a 69 6e 74  UMN_METADATA.int
22931 20 73 71 6c 69 74 65 33 5f 74 61 62 6c 65 5f 63   sqlite3_table_c
22932 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61 28 0a  olumn_metadata(.
22933 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
22934 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
22935 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64   Connection hand
22936 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  le */.  const ch
22937 61 72 20 2a 7a 44 62 4e 61 6d 65 2c 20 20 20 20  ar *zDbName,    
22938 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
22939 6e 61 6d 65 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a  name or NULL */.
2293a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
2293b 61 62 6c 65 4e 61 6d 65 2c 20 20 20 20 20 2f 2a  ableName,     /*
2293c 20 54 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20   Table name */. 
2293d 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f   const char *zCo
2293e 6c 75 6d 6e 4e 61 6d 65 2c 20 20 20 20 2f 2a 20  lumnName,    /* 
2293f 43 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20  Column name */. 
22940 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a 70 7a   char const **pz
22941 44 61 74 61 54 79 70 65 2c 20 20 20 20 2f 2a 20  DataType,    /* 
22942 4f 55 54 50 55 54 3a 20 44 65 63 6c 61 72 65 64  OUTPUT: Declared
22943 20 64 61 74 61 20 74 79 70 65 20 2a 2f 0a 20 20   data type */.  
22944 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a 70 7a 43  char const **pzC
22945 6f 6c 6c 53 65 71 2c 20 20 20 20 20 2f 2a 20 4f  ollSeq,     /* O
22946 55 54 50 55 54 3a 20 43 6f 6c 6c 61 74 69 6f 6e  UTPUT: Collation
22947 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 20 2a   sequence name *
22948 2f 0a 20 20 69 6e 74 20 2a 70 4e 6f 74 4e 75 6c  /.  int *pNotNul
22949 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l,              
2294a 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 65 20  /* OUTPUT: True 
2294b 69 66 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73  if NOT NULL cons
2294c 74 72 61 69 6e 74 20 65 78 69 73 74 73 20 2a 2f  traint exists */
2294d 0a 20 20 69 6e 74 20 2a 70 50 72 69 6d 61 72 79  .  int *pPrimary
2294e 4b 65 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f  Key,           /
2294f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 65 20 69  * OUTPUT: True i
22950 66 20 63 6f 6c 75 6d 6e 20 70 61 72 74 20 6f 66  f column part of
22951 20 50 4b 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 41   PK */.  int *pA
22952 75 74 6f 69 6e 63 20 20 20 20 20 20 20 20 20 20  utoinc          
22953 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20       /* OUTPUT: 
22954 54 72 75 65 20 69 66 20 63 6f 6c 75 6d 73 20 69  True if colums i
22955 73 20 61 75 74 6f 2d 69 6e 63 72 65 6d 65 6e 74  s auto-increment
22956 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b   */.){.  int rc;
22957 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67  .  char *zErrMsg
22958 20 3d 20 30 3b 0a 20 20 54 61 62 6c 65 20 2a 70   = 0;.  Table *p
22959 54 61 62 20 3d 20 30 3b 0a 20 20 43 6f 6c 75 6d  Tab = 0;.  Colum
2295a 6e 20 2a 70 43 6f 6c 20 3d 20 30 3b 0a 20 20 69  n *pCol = 0;.  i
2295b 6e 74 20 69 43 6f 6c 3b 0a 0a 20 20 63 68 61 72  nt iCol;..  char
2295c 20 63 6f 6e 73 74 20 2a 7a 44 61 74 61 54 79 70   const *zDataTyp
2295d 65 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 63 6f  e = 0;.  char co
2295e 6e 73 74 20 2a 7a 43 6f 6c 6c 53 65 71 20 3d 20  nst *zCollSeq = 
2295f 30 3b 0a 20 20 69 6e 74 20 6e 6f 74 6e 75 6c 6c  0;.  int notnull
22960 20 3d 20 30 3b 0a 20 20 69 6e 74 20 70 72 69 6d   = 0;.  int prim
22961 61 72 79 6b 65 79 20 3d 20 30 3b 0a 20 20 69 6e  arykey = 0;.  in
22962 74 20 61 75 74 6f 69 6e 63 20 3d 20 30 3b 0a 0a  t autoinc = 0;..
22963 20 20 2f 2a 20 45 6e 73 75 72 65 20 74 68 65 20    /* Ensure the 
22964 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20  database schema 
22965 68 61 73 20 62 65 65 6e 20 6c 6f 61 64 65 64 20  has been loaded 
22966 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
22967 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a  SafetyOn(db) ){.
22968 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
22969 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20  E_MISUSE;.  }.  
2296a 72 63 20 3d 20 73 71 6c 69 74 65 33 49 6e 69 74  rc = sqlite3Init
2296b 28 64 62 2c 20 26 7a 45 72 72 4d 73 67 29 3b 0a  (db, &zErrMsg);.
2296c 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21    if( SQLITE_OK!
2296d 3d 72 63 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  =rc ){.    goto 
2296e 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d 0a 0a  error_out;.  }..
2296f 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20    /* Locate the 
22970 74 61 62 6c 65 20 69 6e 20 71 75 65 73 74 69 6f  table in questio
22971 6e 20 2a 2f 0a 20 20 70 54 61 62 20 3d 20 73 71  n */.  pTab = sq
22972 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64  lite3FindTable(d
22973 62 2c 20 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 7a  b, zTableName, z
22974 44 62 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 21  DbName);.  if( !
22975 70 54 61 62 20 7c 7c 20 70 54 61 62 2d 3e 70 53  pTab || pTab->pS
22976 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 70 54 61  elect ){.    pTa
22977 62 20 3d 20 30 3b 0a 20 20 20 20 67 6f 74 6f 20  b = 0;.    goto 
22978 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d 0a 0a  error_out;.  }..
22979 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 63 6f    /* Find the co
2297a 6c 75 6d 6e 20 66 6f 72 20 77 68 69 63 68 20 69  lumn for which i
2297b 6e 66 6f 20 69 73 20 72 65 71 75 65 73 74 65 64  nfo is requested
2297c 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
2297d 33 49 73 52 6f 77 69 64 28 7a 43 6f 6c 75 6d 6e  3IsRowid(zColumn
2297e 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 69 43 6f  Name) ){.    iCo
2297f 6c 20 3d 20 70 54 61 62 2d 3e 69 50 4b 65 79 3b  l = pTab->iPKey;
22980 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30  .    if( iCol>=0
22981 20 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c 20 3d   ){.      pCol =
22982 20 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f   &pTab->aCol[iCo
22983 6c 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  l];.    }.  }els
22984 65 7b 0a 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d  e{.    for(iCol=
22985 30 3b 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43  0; iCol<pTab->nC
22986 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20  ol; iCol++){.   
22987 20 20 20 70 43 6f 6c 20 3d 20 26 70 54 61 62 2d     pCol = &pTab-
22988 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 3b 0a 20 20 20  >aCol[iCol];.   
22989 20 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65     if( 0==sqlite
2298a 33 53 74 72 49 43 6d 70 28 70 43 6f 6c 2d 3e 7a  3StrICmp(pCol->z
2298b 4e 61 6d 65 2c 20 7a 43 6f 6c 75 6d 6e 4e 61 6d  Name, zColumnNam
2298c 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72  e) ){.        br
2298d 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
2298e 20 7d 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3d   }.    if( iCol=
2298f 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20  =pTab->nCol ){. 
22990 20 20 20 20 20 70 54 61 62 20 3d 20 30 3b 0a 20       pTab = 0;. 
22991 20 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72 5f       goto error_
22992 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  out;.    }.  }..
22993 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69    /* The followi
22994 6e 67 20 62 6c 6f 63 6b 20 73 74 6f 72 65 73 20  ng block stores 
22995 74 68 65 20 6d 65 74 61 20 69 6e 66 6f 72 6d 61  the meta informa
22996 74 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 62  tion that will b
22997 65 20 72 65 74 75 72 6e 65 64 0a 20 20 2a 2a 20  e returned.  ** 
22998 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 69 6e  to the caller in
22999 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
2299a 20 7a 44 61 74 61 54 79 70 65 2c 20 7a 43 6f 6c   zDataType, zCol
2299b 6c 53 65 71 2c 20 6e 6f 74 6e 75 6c 6c 2c 20 70  lSeq, notnull, p
2299c 72 69 6d 61 72 79 6b 65 79 0a 20 20 2a 2a 20 61  rimarykey.  ** a
2299d 6e 64 20 61 75 74 6f 69 6e 63 2e 20 41 74 20 74  nd autoinc. At t
2299e 68 69 73 20 70 6f 69 6e 74 20 74 68 65 72 65 20  his point there 
2299f 61 72 65 20 74 77 6f 20 70 6f 73 73 69 62 69 6c  are two possibil
229a0 69 74 69 65 73 3a 0a 20 20 2a 2a 20 0a 20 20 2a  ities:.  ** .  *
229a1 2a 20 20 20 20 20 31 2e 20 54 68 65 20 73 70 65  *     1. The spe
229a2 63 69 66 69 65 64 20 63 6f 6c 75 6d 6e 20 6e 61  cified column na
229a3 6d 65 20 77 61 73 20 72 6f 77 69 64 22 2c 20 22  me was rowid", "
229a4 6f 69 64 22 20 6f 72 20 22 5f 72 6f 77 69 64 5f  oid" or "_rowid_
229a5 22 20 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 61  " .  **        a
229a6 6e 64 20 74 68 65 72 65 20 69 73 20 6e 6f 20 65  nd there is no e
229a7 78 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72  xplicitly declar
229a8 65 64 20 49 50 4b 20 63 6f 6c 75 6d 6e 2e 20 0a  ed IPK column. .
229a9 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 32 2e    **.  **     2.
229aa 20 54 68 65 20 74 61 62 6c 65 20 69 73 20 6e 6f   The table is no
229ab 74 20 61 20 76 69 65 77 20 61 6e 64 20 74 68 65  t a view and the
229ac 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 69 64 65   column name ide
229ad 6e 74 69 66 69 65 64 20 61 6e 20 0a 20 20 2a 2a  ntified an .  **
229ae 20 20 20 20 20 20 20 20 65 78 70 6c 69 63 69 74          explicit
229af 6c 79 20 64 65 63 6c 61 72 65 64 20 63 6f 6c 75  ly declared colu
229b0 6d 6e 2e 20 43 6f 70 79 20 6d 65 74 61 20 69 6e  mn. Copy meta in
229b1 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 2a  formation from *
229b2 70 43 6f 6c 2e 0a 20 20 2a 2f 20 0a 20 20 69 66  pCol..  */ .  if
229b3 28 20 70 43 6f 6c 20 29 7b 0a 20 20 20 20 7a 44  ( pCol ){.    zD
229b4 61 74 61 54 79 70 65 20 3d 20 70 43 6f 6c 2d 3e  ataType = pCol->
229b5 7a 54 79 70 65 3b 0a 20 20 20 20 7a 43 6f 6c 6c  zType;.    zColl
229b6 53 65 71 20 3d 20 70 43 6f 6c 2d 3e 7a 43 6f 6c  Seq = pCol->zCol
229b7 6c 3b 0a 20 20 20 20 6e 6f 74 6e 75 6c 6c 20 3d  l;.    notnull =
229b8 20 28 70 43 6f 6c 2d 3e 6e 6f 74 4e 75 6c 6c 3f   (pCol->notNull?
229b9 31 3a 30 29 3b 0a 20 20 20 20 70 72 69 6d 61 72  1:0);.    primar
229ba 79 6b 65 79 20 20 3d 20 28 70 43 6f 6c 2d 3e 69  ykey  = (pCol->i
229bb 73 50 72 69 6d 4b 65 79 3f 31 3a 30 29 3b 0a 20  sPrimKey?1:0);. 
229bc 20 20 20 61 75 74 6f 69 6e 63 20 3d 20 28 28 70     autoinc = ((p
229bd 54 61 62 2d 3e 69 50 4b 65 79 3d 3d 69 43 6f 6c  Tab->iPKey==iCol
229be 20 26 26 20 70 54 61 62 2d 3e 61 75 74 6f 49 6e   && pTab->autoIn
229bf 63 29 3f 31 3a 30 29 3b 0a 20 20 7d 65 6c 73 65  c)?1:0);.  }else
229c0 7b 0a 20 20 20 20 7a 44 61 74 61 54 79 70 65 20  {.    zDataType 
229c1 3d 20 22 49 4e 54 45 47 45 52 22 3b 0a 20 20 20  = "INTEGER";.   
229c2 20 70 72 69 6d 61 72 79 6b 65 79 20 3d 20 31 3b   primarykey = 1;
229c3 0a 20 20 7d 0a 20 20 69 66 28 20 21 7a 43 6f 6c  .  }.  if( !zCol
229c4 6c 53 65 71 20 29 7b 0a 20 20 20 20 7a 43 6f 6c  lSeq ){.    zCol
229c5 6c 53 65 71 20 3d 20 22 42 49 4e 41 52 59 22 3b  lSeq = "BINARY";
229c6 0a 20 20 7d 0a 0a 65 72 72 6f 72 5f 6f 75 74 3a  .  }..error_out:
229c7 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61  .  if( sqlite3Sa
229c8 66 65 74 79 4f 66 66 28 64 62 29 20 29 7b 0a 20  fetyOff(db) ){. 
229c9 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d     rc = SQLITE_M
229ca 49 53 55 53 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  ISUSE;.  }..  /*
229cb 20 57 68 65 74 68 65 72 20 74 68 65 20 66 75 6e   Whether the fun
229cc 63 74 69 6f 6e 20 63 61 6c 6c 20 73 75 63 63 65  ction call succe
229cd 65 64 65 64 20 6f 72 20 66 61 69 6c 65 64 2c 20  eded or failed, 
229ce 73 65 74 20 74 68 65 20 6f 75 74 70 75 74 20 70  set the output p
229cf 61 72 61 6d 65 74 65 72 73 0a 20 20 2a 2a 20 74  arameters.  ** t
229d0 6f 20 77 68 61 74 65 76 65 72 20 74 68 65 69 72  o whatever their
229d1 20 6c 6f 63 61 6c 20 63 6f 75 6e 74 65 72 70 61   local counterpa
229d2 72 74 73 20 63 6f 6e 74 61 69 6e 2e 20 49 66 20  rts contain. If 
229d3 61 6e 20 65 72 72 6f 72 20 64 69 64 20 6f 63 63  an error did occ
229d4 75 72 2c 0a 20 20 2a 2a 20 74 68 69 73 20 68 61  ur,.  ** this ha
229d5 73 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 20  s the effect of 
229d6 7a 65 72 6f 69 6e 67 20 61 6c 6c 20 6f 75 74 70  zeroing all outp
229d7 75 74 20 70 61 72 61 6d 65 74 65 72 73 2e 0a 20  ut parameters.. 
229d8 20 2a 2f 0a 20 20 69 66 28 20 70 7a 44 61 74 61   */.  if( pzData
229d9 54 79 70 65 20 29 20 2a 70 7a 44 61 74 61 54 79  Type ) *pzDataTy
229da 70 65 20 3d 20 7a 44 61 74 61 54 79 70 65 3b 0a  pe = zDataType;.
229db 20 20 69 66 28 20 70 7a 43 6f 6c 6c 53 65 71 20    if( pzCollSeq 
229dc 29 20 2a 70 7a 43 6f 6c 6c 53 65 71 20 3d 20 7a  ) *pzCollSeq = z
229dd 43 6f 6c 6c 53 65 71 3b 0a 20 20 69 66 28 20 70  CollSeq;.  if( p
229de 4e 6f 74 4e 75 6c 6c 20 29 20 2a 70 4e 6f 74 4e  NotNull ) *pNotN
229df 75 6c 6c 20 3d 20 6e 6f 74 6e 75 6c 6c 3b 0a 20  ull = notnull;. 
229e0 20 69 66 28 20 70 50 72 69 6d 61 72 79 4b 65 79   if( pPrimaryKey
229e1 20 29 20 2a 70 50 72 69 6d 61 72 79 4b 65 79 20   ) *pPrimaryKey 
229e2 3d 20 70 72 69 6d 61 72 79 6b 65 79 3b 0a 20 20  = primarykey;.  
229e3 69 66 28 20 70 41 75 74 6f 69 6e 63 20 29 20 2a  if( pAutoinc ) *
229e4 70 41 75 74 6f 69 6e 63 20 3d 20 61 75 74 6f 69  pAutoinc = autoi
229e5 6e 63 3b 0a 0a 20 20 69 66 28 20 53 51 4c 49 54  nc;..  if( SQLIT
229e6 45 5f 4f 4b 3d 3d 72 63 20 26 26 20 21 70 54 61  E_OK==rc && !pTa
229e7 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  b ){.    sqlite3
229e8 53 65 74 53 74 72 69 6e 67 28 26 7a 45 72 72 4d  SetString(&zErrM
229e9 73 67 2c 20 22 6e 6f 20 73 75 63 68 20 74 61 62  sg, "no such tab
229ea 6c 65 20 63 6f 6c 75 6d 6e 3a 20 22 2c 20 7a 54  le column: ", zT
229eb 61 62 6c 65 4e 61 6d 65 2c 20 22 2e 22 2c 20 0a  ableName, ".", .
229ec 20 20 20 20 20 20 20 20 7a 43 6f 6c 75 6d 6e 4e          zColumnN
229ed 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 72 63 20  ame, 0);.    rc 
229ee 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
229ef 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 72 72    }.  sqlite3Err
229f0 6f 72 28 64 62 2c 20 72 63 2c 20 28 7a 45 72 72  or(db, rc, (zErr
229f1 4d 73 67 3f 22 25 73 22 3a 30 29 2c 20 7a 45 72  Msg?"%s":0), zEr
229f2 72 4d 73 67 29 3b 0a 20 20 73 71 6c 69 74 65 46  rMsg);.  sqliteF
229f3 72 65 65 28 7a 45 72 72 4d 73 67 29 3b 0a 20 20  ree(zErrMsg);.  
229f4 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41 70  return sqlite3Ap
229f5 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 7d  iExit(db, rc);.}
229f6 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53  .#endif../*.** S
229f7 65 74 20 61 6c 6c 20 74 68 65 20 70 61 72 61 6d  et all the param
229f8 65 74 65 72 73 20 69 6e 20 74 68 65 20 63 6f 6d  eters in the com
229f9 70 69 6c 65 64 20 53 51 4c 20 73 74 61 74 65 6d  piled SQL statem
229fa 65 6e 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a  ent to NULL..*/.
229fb 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6c 65 61  int sqlite3_clea
229fc 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 74  r_bindings(sqlit
229fd 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b  e3_stmt *pStmt){
229fe 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
229ff 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
22a00 20 20 66 6f 72 28 69 3d 31 3b 20 72 63 3d 3d 53    for(i=1; rc==S
22a01 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 3d 73  QLITE_OK && i<=s
22a02 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61  qlite3_bind_para
22a03 6d 65 74 65 72 5f 63 6f 75 6e 74 28 70 53 74 6d  meter_count(pStm
22a04 74 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 72 63  t); i++){.    rc
22a05 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f   = sqlite3_bind_
22a06 6e 75 6c 6c 28 70 53 74 6d 74 2c 20 69 29 3b 0a  null(pStmt, i);.
22a07 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
22a08 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20  .}../*.** Sleep 
22a09 66 6f 72 20 61 20 6c 69 74 74 6c 65 20 77 68 69  for a little whi
22a0a 6c 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20  le.  Return the 
22a0b 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20 73  amount of time s
22a0c 6c 65 70 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  lept..*/.SQLITE_
22a0d 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
22a0e 73 6c 65 65 70 28 69 6e 74 20 6d 73 29 7b 0a 20  sleep(int ms){. 
22a0f 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4f   return sqlite3O
22a10 73 53 6c 65 65 70 28 6d 73 29 3b 0a 7d 0a 0a 2f  sSleep(ms);.}../
22a11 2a 0a 2a 2a 20 45 6e 61 62 6c 65 20 6f 72 20 64  *.** Enable or d
22a12 69 73 61 62 6c 65 20 74 68 65 20 65 78 74 65 6e  isable the exten
22a13 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73  ded result codes
22a14 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33  ..*/.int sqlite3
22a15 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75 6c 74  _extended_result
22a16 5f 63 6f 64 65 73 28 73 71 6c 69 74 65 33 20 2a  _codes(sqlite3 *
22a17 64 62 2c 20 69 6e 74 20 6f 6e 6f 66 66 29 7b 0a  db, int onoff){.
22a18 20 20 64 62 2d 3e 65 72 72 4d 61 73 6b 20 3d 20    db->errMask = 
22a19 6f 6e 6f 66 66 20 3f 20 30 78 66 66 66 66 66 66  onoff ? 0xffffff
22a1a 66 66 20 3a 20 30 78 66 66 3b 0a 20 20 72 65 74  ff : 0xff;.  ret
22a1b 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
22a1c 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
22a1d 2a 20 45 6e 64 20 6f 66 20 6d 61 69 6e 2e 63 20  * End of main.c 
22a1e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22a1f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22a21 2f 0a                                            /.